xref: /netbsd-src/sys/netipsec/xform_ipip.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: xform_ipip.c,v 1.32 2015/03/27 07:47:10 ozaki-r Exp $	*/
2 /*	$FreeBSD: src/sys/netipsec/xform_ipip.c,v 1.3.2.1 2003/01/24 05:11:36 sam Exp $	*/
3 /*	$OpenBSD: ip_ipip.c,v 1.25 2002/06/10 18:04:55 itojun Exp $ */
4 
5 /*
6  * The authors of this code are John Ioannidis (ji@tla.org),
7  * Angelos D. Keromytis (kermit@csd.uch.gr) and
8  * Niels Provos (provos@physnet.uni-hamburg.de).
9  *
10  * The original version of this code was written by John Ioannidis
11  * for BSD/OS in Athens, Greece, in November 1995.
12  *
13  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
14  * by Angelos D. Keromytis.
15  *
16  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
17  * and Niels Provos.
18  *
19  * Additional features in 1999 by Angelos D. Keromytis.
20  *
21  * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
22  * Angelos D. Keromytis and Niels Provos.
23  * Copyright (c) 2001, Angelos D. Keromytis.
24  *
25  * Permission to use, copy, and modify this software with or without fee
26  * is hereby granted, provided that this entire notice is included in
27  * all copies of any software which is or includes a copy or
28  * modification of this software.
29  * You may use this code under the GNU public license if you so wish. Please
30  * contribute changes back to the authors under this freer than GPL license
31  * so that we may further the use of strong encryption without limitations to
32  * all.
33  *
34  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
35  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
36  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
37  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
38  * PURPOSE.
39  */
40 
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: xform_ipip.c,v 1.32 2015/03/27 07:47:10 ozaki-r Exp $");
43 
44 /*
45  * IP-inside-IP processing
46  */
47 #include "opt_inet.h"
48 #ifdef __FreeBSD__
49 #include "opt_inet6.h"
50 #include "opt_random_ip_id.h"
51 #endif /* __FreeBSD__ */
52 
53 
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/mbuf.h>
57 #include <sys/socket.h>
58 #include <sys/kernel.h>
59 #include <sys/protosw.h>
60 #include <sys/sysctl.h>
61 
62 #include <net/if.h>
63 #include <net/route.h>
64 #include <net/netisr.h>
65 
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_ecn.h>
71 #include <netinet/ip_var.h>
72 #include <netinet/ip_encap.h>
73 #ifdef __FreeBSD__
74 #include <netinet/ipprotosw.h>
75 #endif
76 
77 #include <netipsec/ipsec.h>
78 #include <netipsec/ipsec_private.h>
79 #include <netipsec/xform.h>
80 
81 #include <netipsec/ipip_var.h>
82 
83 #ifdef MROUTING
84 #include <netinet/ip_mroute.h>
85 #endif
86 
87 #ifdef INET6
88 #include <netinet/ip6.h>
89 #include <netipsec/ipsec6.h>
90 #  ifdef __FreeBSD__
91 #  include <netinet6/ip6_ecn.h>
92 #  endif
93 #include <netinet6/in6_var.h>
94 #include <netinet6/ip6protosw.h>
95 #endif
96 
97 #include <netipsec/key.h>
98 #include <netipsec/key_debug.h>
99 #include <netipsec/ipsec_osdep.h>
100 
101 #ifdef __FreeBSD__
102 typedef void	pr_in_input_t (struct mbuf *, int, int); /* XXX FIX THIS */
103 #else
104 typedef void	pr_in_input_t (struct mbuf *m, ...);
105 #endif
106 
107 /*
108  * We can control the acceptance of IP4 packets by altering the sysctl
109  * net.inet.ipip.allow value.  Zero means drop them, all else is acceptance.
110  */
111 int	ipip_allow = 0;
112 
113 percpu_t *ipipstat_percpu;
114 
115 #ifdef SYSCTL_DECL
116 SYSCTL_DECL(_net_inet_ipip);
117 
118 SYSCTL_INT(_net_inet_ipip, OID_AUTO,
119 	ipip_allow,	CTLFLAG_RW,	&ipip_allow,	0, "");
120 SYSCTL_STRUCT(_net_inet_ipip, IPSECCTL_STATS,
121 	stats,		CTLFLAG_RD,	&ipipstat,	ipipstat, "");
122 
123 #endif
124 
125 #ifdef __FreeBSD__
126 static
127 #endif
128 void ipe4_attach(void);
129 
130 
131 /* XXX IPCOMP */
132 #define	M_IPSEC	(M_AUTHIPHDR|M_AUTHIPDGM|M_DECRYPTED)
133 
134 static void _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp);
135 
136 #ifdef INET6
137 /*
138  * Really only a wrapper for ipip_input(), for use with IPv6.
139  */
140 int
141 ip4_input6(struct mbuf **m, int *offp, int proto)
142 {
143 #if 0
144 	/* If we do not accept IP-in-IP explicitly, drop.  */
145 	if (!ipip_allow && ((*m)->m_flags & M_IPSEC) == 0) {
146 		DPRINTF(("ip4_input6: dropped due to policy\n"));
147 		IPIP_STATINC(IPIP_STAT_PDROPS);
148 		m_freem(*m);
149 		return IPPROTO_DONE;
150 	}
151 #endif
152 	_ipip_input(*m, *offp, NULL);
153 	return IPPROTO_DONE;
154 }
155 #endif /* INET6 */
156 
157 #ifdef INET
158 /*
159  * Really only a wrapper for ipip_input(), for use with IPv4.
160  */
161 void
162 ip4_input(struct mbuf *m, ...)
163 {
164 	va_list ap;
165 	int iphlen;
166 
167 #if 0
168 	/* If we do not accept IP-in-IP explicitly, drop.  */
169 	if (!ipip_allow && (m->m_flags & M_IPSEC) == 0) {
170 		DPRINTF(("ip4_input: dropped due to policy\n"));
171 		IPIP_STATINC(IPIP_STAT_PDROPS);
172 		m_freem(m);
173 		return;
174 	}
175 #endif
176 	va_start(ap, m);
177 	iphlen = va_arg(ap, int);
178 	va_end(ap);
179 
180 	_ipip_input(m, iphlen, NULL);
181 }
182 #endif /* INET */
183 
184 /*
185  * ipip_input gets called when we receive an IP{46} encapsulated packet,
186  * either because we got it at a real interface, or because AH or ESP
187  * were being used in tunnel mode (in which case the rcvif element will
188  * contain the address of the encX interface associated with the tunnel.
189  */
190 
191 static void
192 _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp)
193 {
194 	register struct sockaddr_in *sin;
195 	register struct ifnet *ifp;
196 	register struct ifaddr *ifa;
197 	pktqueue_t *pktq = NULL;
198 	struct ip *ipo;
199 #ifdef INET6
200 	register struct sockaddr_in6 *sin6;
201 	struct ip6_hdr *ip6 = NULL;
202 	u_int8_t itos;
203 #endif
204 	u_int8_t otos;
205 	u_int8_t v;
206 	int hlen;
207 
208 	IPIP_STATINC(IPIP_STAT_IPACKETS);
209 
210 	m_copydata(m, 0, 1, &v);
211 
212 	switch (v >> 4) {
213 #ifdef INET
214         case 4:
215 		hlen = sizeof(struct ip);
216 		break;
217 #endif /* INET */
218 #ifdef INET6
219         case 6:
220 		hlen = sizeof(struct ip6_hdr);
221 		break;
222 #endif
223         default:
224 		DPRINTF(("_ipip_input: bad protocol version 0x%x (%u) "
225 			"for outer header\n", v, v>>4));
226 		IPIP_STATINC(IPIP_STAT_FAMILY);
227 		m_freem(m);
228 		return /* EAFNOSUPPORT */;
229 	}
230 
231 	/* Bring the IP header in the first mbuf, if not there already */
232 	if (m->m_len < hlen) {
233 		if ((m = m_pullup(m, hlen)) == NULL) {
234 			DPRINTF(("ipip_input: m_pullup (1) failed\n"));
235 			IPIP_STATINC(IPIP_STAT_HDROPS);
236 			return;
237 		}
238 	}
239 
240 	ipo = mtod(m, struct ip *);
241 
242 #ifdef MROUTING
243 	if (ipo->ip_v == IPVERSION && ipo->ip_p == IPPROTO_IPV4) {
244 		if (IN_MULTICAST(((struct ip *)((char *) ipo + iphlen))->ip_dst.s_addr)) {
245 			ipip_mroute_input (m, iphlen);
246 			return;
247 		}
248 	}
249 #endif /* MROUTING */
250 
251 	/* Keep outer ecn field. */
252 	switch (v >> 4) {
253 #ifdef INET
254 	case 4:
255 		otos = ipo->ip_tos;
256 		break;
257 #endif /* INET */
258 #ifdef INET6
259 	case 6:
260 		otos = (ntohl(mtod(m, struct ip6_hdr *)->ip6_flow) >> 20) & 0xff;
261 		break;
262 #endif
263 	default:
264 		panic("ipip_input: unknown ip version %u (outer)", v>>4);
265 	}
266 
267 	/* Remove outer IP header */
268 	m_adj(m, iphlen);
269 
270 	/* Sanity check */
271 	if (m->m_pkthdr.len < sizeof(struct ip))  {
272 		IPIP_STATINC(IPIP_STAT_HDROPS);
273 		m_freem(m);
274 		return;
275 	}
276 
277 	m_copydata(m, 0, 1, &v);
278 
279 	switch (v >> 4) {
280 #ifdef INET
281         case 4:
282 		hlen = sizeof(struct ip);
283 		break;
284 #endif /* INET */
285 
286 #ifdef INET6
287         case 6:
288 		hlen = sizeof(struct ip6_hdr);
289 		break;
290 #endif
291 	default:
292 		DPRINTF(("_ipip_input: bad protocol version 0x%x (%u) "
293 			"for inner header\n", v, v>>4));
294 		IPIP_STATINC(IPIP_STAT_FAMILY);
295 		m_freem(m);
296 		return; /* EAFNOSUPPORT */
297 	}
298 
299 	/*
300 	 * Bring the inner IP header in the first mbuf, if not there already.
301 	 */
302 	if (m->m_len < hlen) {
303 		if ((m = m_pullup(m, hlen)) == NULL) {
304 			DPRINTF(("ipip_input: m_pullup (2) failed\n"));
305 			IPIP_STATINC(IPIP_STAT_HDROPS);
306 			return;
307 		}
308 	}
309 
310 	/*
311 	 * RFC 1853 specifies that the inner TTL should not be touched on
312 	 * decapsulation. There's no reason this comment should be here, but
313 	 * this is as good as any a position.
314 	 */
315 
316 	/* Some sanity checks in the inner IP header */
317 	switch (v >> 4) {
318 #ifdef INET
319     	case 4:
320                 ipo = mtod(m, struct ip *);
321 		ip_ecn_egress(ip4_ipsec_ecn, &otos, &ipo->ip_tos);
322                 break;
323 #endif /* INET */
324 #ifdef INET6
325     	case 6:
326                 ip6 = (struct ip6_hdr *) ipo;
327 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
328 		ip_ecn_egress(ip6_ipsec_ecn, &otos, &itos);
329 		ip6->ip6_flow &= ~htonl(0xff << 20);
330 		ip6->ip6_flow |= htonl((u_int32_t) itos << 20);
331                 break;
332 #endif
333 	default:
334 		panic("ipip_input: unknown ip version %u (inner)", v>>4);
335 	}
336 
337 	/* Check for local address spoofing. */
338 	if ((m->m_pkthdr.rcvif == NULL ||
339 	    !(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK)) &&
340 	    ipip_allow != 2) {
341 		IFNET_FOREACH(ifp) {
342 			IFADDR_FOREACH(ifa, ifp) {
343 #ifdef INET
344 				if (ipo) {
345 					if (ifa->ifa_addr->sa_family !=
346 					    AF_INET)
347 						continue;
348 
349 					sin = (struct sockaddr_in *) ifa->ifa_addr;
350 
351 					if (sin->sin_addr.s_addr ==
352 					    ipo->ip_src.s_addr)	{
353 						IPIP_STATINC(IPIP_STAT_SPOOF);
354 						m_freem(m);
355 						return;
356 					}
357 				}
358 #endif /* INET */
359 
360 #ifdef INET6
361 				if (ip6) {
362 					if (ifa->ifa_addr->sa_family !=
363 					    AF_INET6)
364 						continue;
365 
366 					sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
367 
368 					if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_src)) {
369 						IPIP_STATINC(IPIP_STAT_SPOOF);
370 						m_freem(m);
371 						return;
372 					}
373 
374 				}
375 #endif /* INET6 */
376 			}
377 		}
378 	}
379 
380 	/* Statistics */
381 	IPIP_STATADD(IPIP_STAT_IBYTES, m->m_pkthdr.len - iphlen);
382 
383 	/*
384 	 * Interface pointer stays the same; if no IPsec processing has
385 	 * been done (or will be done), this will point to a normal
386 	 * interface. Otherwise, it'll point to an enc interface, which
387 	 * will allow a packet filter to distinguish between secure and
388 	 * untrusted packets.
389 	 */
390 
391 	switch (v >> 4) {
392 #ifdef INET
393 	case 4:
394 		pktq = ip_pktq;
395 		break;
396 #endif
397 #ifdef INET6
398 	case 6:
399 		pktq = ip6_pktq;
400 		break;
401 #endif
402 	default:
403 		panic("ipip_input: should never reach here");
404 	}
405 
406 	int s = splnet();
407 	if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
408 		IPIP_STATINC(IPIP_STAT_QFULL);
409 		m_freem(m);
410 	}
411 	splx(s);
412 }
413 
414 int
415 ipip_output(
416     struct mbuf *m,
417     struct ipsecrequest *isr,
418     struct mbuf **mp,
419     int skip,
420     int protoff
421 )
422 {
423 	const struct secasvar *sav;
424 	u_int8_t tp, otos;
425 	struct secasindex *saidx;
426 	int error;
427 #ifdef INET
428 	u_int8_t itos;
429 	struct ip *ipo;
430 #endif /* INET */
431 #ifdef INET6
432 	struct ip6_hdr *ip6, *ip6o;
433 #endif /* INET6 */
434 
435 	IPSEC_SPLASSERT_SOFTNET("ipip_output");
436 
437 	sav = isr->sav;
438 	IPSEC_ASSERT(sav != NULL, ("ipip_output: null SA"));
439 	IPSEC_ASSERT(sav->sah != NULL, ("ipip_output: null SAH"));
440 
441 	/* XXX Deal with empty TDB source/destination addresses. */
442 
443 	m_copydata(m, 0, 1, &tp);
444 	tp = (tp >> 4) & 0xff;  /* Get the IP version number. */
445 
446 	saidx = &sav->sah->saidx;
447 	switch (saidx->dst.sa.sa_family) {
448 #ifdef INET
449 	case AF_INET:
450 		if (saidx->src.sa.sa_family != AF_INET ||
451 		    saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
452 		    saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) {
453 			DPRINTF(("ipip_output: unspecified tunnel endpoint "
454 			    "address in SA %s/%08lx\n",
455 			    ipsec_address(&saidx->dst),
456 			    (u_long) ntohl(sav->spi)));
457 			IPIP_STATINC(IPIP_STAT_UNSPEC);
458 			error = EINVAL;
459 			goto bad;
460 		}
461 
462 		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
463 		if (m == 0) {
464 			DPRINTF(("ipip_output: M_PREPEND failed\n"));
465 			IPIP_STATINC(IPIP_STAT_HDROPS);
466 			error = ENOBUFS;
467 			goto bad;
468 		}
469 
470 		ipo = mtod(m, struct ip *);
471 
472 		ipo->ip_v = IPVERSION;
473 		ipo->ip_hl = 5;
474 		ipo->ip_len = htons(m->m_pkthdr.len);
475 		ipo->ip_ttl = ip_defttl;
476 		ipo->ip_sum = 0;
477 		ipo->ip_src = saidx->src.sin.sin_addr;
478 		ipo->ip_dst = saidx->dst.sin.sin_addr;
479 
480 #if defined(__NetBSD__)
481 		ipo->ip_id = ip_newid(NULL);
482 #elif defined(RANDOM_IP_ID)
483 		ipo->ip_id = ip_randomid();
484 #else
485 		ipo->ip_id = htons(ip_id++);
486 #endif
487 
488 		/* If the inner protocol is IP... */
489 		if (tp == IPVERSION) {
490 			/* Save ECN notification */
491 			m_copydata(m, sizeof(struct ip) +
492 			    offsetof(struct ip, ip_tos),
493 			    sizeof(u_int8_t), &itos);
494 
495 			ipo->ip_p = IPPROTO_IPIP;
496 
497 			/*
498 			 * We should be keeping tunnel soft-state and
499 			 * send back ICMPs if needed.
500 			 */
501 			m_copydata(m, sizeof(struct ip) +
502 			    offsetof(struct ip, ip_off),
503 			    sizeof(u_int16_t), &ipo->ip_off);
504 			ipo->ip_off &= ~ IP_OFF_CONVERT(IP_DF | IP_MF | IP_OFFMASK);
505 		}
506 #ifdef INET6
507 		else if (tp == (IPV6_VERSION >> 4)) {
508 			u_int32_t itos32;
509 
510 			/* Save ECN notification. */
511 			m_copydata(m, sizeof(struct ip) +
512 			    offsetof(struct ip6_hdr, ip6_flow),
513 			    sizeof(u_int32_t), &itos32);
514 			itos = ntohl(itos32) >> 20;
515 			ipo->ip_p = IPPROTO_IPV6;
516 			ipo->ip_off = 0;
517 		}
518 #endif /* INET6 */
519 		else {
520 			goto nofamily;
521 		}
522 
523 		otos = 0;
524 		ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
525 		ipo->ip_tos = otos;
526 		break;
527 #endif /* INET */
528 
529 #ifdef INET6
530 	case AF_INET6:
531 		if (IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr) ||
532 		    saidx->src.sa.sa_family != AF_INET6 ||
533 		    IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr)) {
534 			DPRINTF(("ipip_output: unspecified tunnel endpoint "
535 			    "address in SA %s/%08lx\n",
536 			    ipsec_address(&saidx->dst),
537 			    (u_long) ntohl(sav->spi)));
538 			IPIP_STATINC(IPIP_STAT_UNSPEC);
539 			error = ENOBUFS;
540 			goto bad;
541 		}
542 
543 		if (tp == (IPV6_VERSION >> 4)) {
544 			/* scoped address handling */
545 			ip6 = mtod(m, struct ip6_hdr *);
546 			if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
547 				ip6->ip6_src.s6_addr16[1] = 0;
548 			if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
549 				ip6->ip6_dst.s6_addr16[1] = 0;
550 		}
551 
552 		M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
553 		if (m == 0) {
554 			DPRINTF(("ipip_output: M_PREPEND failed\n"));
555 			IPIP_STATINC(IPIP_STAT_HDROPS);
556 			error = ENOBUFS;
557 			goto bad;
558 		}
559 
560 		/* Initialize IPv6 header */
561 		ip6o = mtod(m, struct ip6_hdr *);
562 		ip6o->ip6_flow = 0;
563 		ip6o->ip6_vfc &= ~IPV6_VERSION_MASK;
564 		ip6o->ip6_vfc |= IPV6_VERSION;
565 		ip6o->ip6_plen = htons(m->m_pkthdr.len);
566 		ip6o->ip6_hlim = ip_defttl;
567 		ip6o->ip6_dst = saidx->dst.sin6.sin6_addr;
568 		ip6o->ip6_src = saidx->src.sin6.sin6_addr;
569 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6o->ip6_dst))
570 			ip6o->ip6_dst.s6_addr16[1] = htons(saidx->dst.sin6.sin6_scope_id);
571 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6o->ip6_src))
572 			ip6o->ip6_src.s6_addr16[1] = htons(saidx->src.sin6.sin6_scope_id);
573 
574 #ifdef INET
575 		if (tp == IPVERSION) {
576 			/* Save ECN notification */
577 			m_copydata(m, sizeof(struct ip6_hdr) +
578 			    offsetof(struct ip, ip_tos), sizeof(u_int8_t),
579 			    &itos);
580 
581 			/* This is really IPVERSION. */
582 			ip6o->ip6_nxt = IPPROTO_IPIP;
583 		} else
584 #endif /* INET */
585 			if (tp == (IPV6_VERSION >> 4)) {
586 				u_int32_t itos32;
587 
588 				/* Save ECN notification. */
589 				m_copydata(m, sizeof(struct ip6_hdr) +
590 				    offsetof(struct ip6_hdr, ip6_flow),
591 				    sizeof(u_int32_t), &itos32);
592 				itos = ntohl(itos32) >> 20;
593 
594 				ip6o->ip6_nxt = IPPROTO_IPV6;
595 			} else {
596 				goto nofamily;
597 			}
598 
599 		otos = 0;
600 		ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
601 		ip6o->ip6_flow |= htonl((u_int32_t) otos << 20);
602 		break;
603 #endif /* INET6 */
604 
605 	default:
606 nofamily:
607 		DPRINTF(("ipip_output: unsupported protocol family %u\n",
608 		    saidx->dst.sa.sa_family));
609 		IPIP_STATINC(IPIP_STAT_FAMILY);
610 		error = EAFNOSUPPORT;		/* XXX diffs from openbsd */
611 		goto bad;
612 	}
613 
614 	IPIP_STATINC(IPIP_STAT_OPACKETS);
615 	*mp = m;
616 
617 #ifdef INET
618 	if (saidx->dst.sa.sa_family == AF_INET) {
619 #if 0
620 		if (sav->tdb_xform->xf_type == XF_IP4)
621 			tdb->tdb_cur_bytes +=
622 			    m->m_pkthdr.len - sizeof(struct ip);
623 #endif
624 		IPIP_STATADD(IPIP_STAT_OBYTES,
625 			     m->m_pkthdr.len - sizeof(struct ip));
626 	}
627 #endif /* INET */
628 
629 #ifdef INET6
630 	if (saidx->dst.sa.sa_family == AF_INET6) {
631 #if 0
632 		if (sav->tdb_xform->xf_type == XF_IP4)
633 			tdb->tdb_cur_bytes +=
634 			    m->m_pkthdr.len - sizeof(struct ip6_hdr);
635 #endif
636 		IPIP_STATADD(IPIP_STAT_OBYTES,
637 		    m->m_pkthdr.len - sizeof(struct ip6_hdr));
638 	}
639 #endif /* INET6 */
640 
641 	return 0;
642 bad:
643 	if (m)
644 		m_freem(m);
645 	*mp = NULL;
646 	return (error);
647 }
648 
649 static int
650 ipe4_init(struct secasvar *sav, const struct xformsw *xsp)
651 {
652 	sav->tdb_xform = xsp;
653 	return 0;
654 }
655 
656 static int
657 ipe4_zeroize(struct secasvar *sav)
658 {
659 	sav->tdb_xform = NULL;
660 	return 0;
661 }
662 
663 static int
664 ipe4_input(
665     struct mbuf *m,
666     const struct secasvar *sav,
667     int skip,
668     int protoff
669 )
670 {
671 	/* This is a rather serious mistake, so no conditional printing. */
672 	printf("ipe4_input: should never be called\n");
673 	if (m)
674 		m_freem(m);
675 	return EOPNOTSUPP;
676 }
677 
678 static struct xformsw ipe4_xformsw = {
679 	XF_IP4,		0,		"IPv4 Simple Encapsulation",
680 	ipe4_init,	ipe4_zeroize,	ipe4_input,	ipip_output,
681 	NULL,
682 };
683 
684 #ifdef INET
685 PR_WRAP_CTLOUTPUT(rip_ctloutput)
686 #define	rip_ctloutput	rip_ctloutput_wrapper
687 
688 extern struct domain inetdomain;
689 static struct ipprotosw ipe4_protosw = {
690  .pr_type = SOCK_RAW,
691  .pr_domain = &inetdomain,
692  .pr_protocol = IPPROTO_IPV4,
693  .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
694  .pr_input = ip4_input,
695  .pr_output = 0,
696  .pr_ctlinput = 0,
697  .pr_ctloutput = rip_ctloutput,
698  .pr_usrreqs = &rip_usrreqs,
699  .pr_init = 0,
700  .pr_fasttimo = 0,
701  .pr_slowtimo =	0,
702  .pr_drain = 0,
703 };
704 #endif
705 #ifdef INET6
706 PR_WRAP_CTLOUTPUT(rip6_ctloutput)
707 #define	rip6_ctloutput	rip6_ctloutput_wrapper
708 
709 extern struct domain inet6domain;
710 static struct ip6protosw ipe4_protosw6 = {
711  .pr_type = SOCK_RAW,
712  .pr_domain = &inet6domain,
713  .pr_protocol = IPPROTO_IPV6,
714  .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
715  .pr_input = ip4_input6,
716  .pr_output = 0,
717  .pr_ctlinput = 0,
718  .pr_ctloutput = rip6_ctloutput,
719  .pr_usrreqs = &rip6_usrreqs,
720  .pr_init = 0,
721  .pr_fasttimo = 0,
722  .pr_slowtimo = 0,
723  .pr_drain = 0,
724 };
725 #endif
726 
727 /*
728  * Check the encapsulated packet to see if we want it
729  */
730 static int
731 ipe4_encapcheck(struct mbuf *m,
732     int off,
733     int proto,
734     void *arg
735 )
736 {
737 	/*
738 	 * Only take packets coming from IPSEC tunnels; the rest
739 	 * must be handled by the gif tunnel code.  Note that we
740 	 * also return a minimum priority when we want the packet
741 	 * so any explicit gif tunnels take precedence.
742 	 */
743 	return ((m->m_flags & M_IPSEC) != 0 ? 1 : 0);
744 }
745 
746 INITFN void
747 ipe4_attach(void)
748 {
749 
750 	ipipstat_percpu = percpu_alloc(sizeof(uint64_t) * IPIP_NSTATS);
751 
752 	xform_register(&ipe4_xformsw);
753 	/* attach to encapsulation framework */
754 	/* XXX save return cookie for detach on module remove */
755 #ifdef INET
756 	(void) encap_attach_func(AF_INET, -1,
757 		ipe4_encapcheck, (struct protosw*) &ipe4_protosw, NULL);
758 #endif
759 #ifdef INET6
760 	(void) encap_attach_func(AF_INET6, -1,
761 		ipe4_encapcheck, (struct protosw*) &ipe4_protosw6, NULL);
762 #endif
763 }
764 
765 #ifdef SYSINIT
766 SYSINIT(ipe4_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipe4_attach, NULL);
767 #endif
768 
769