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