Home | History | Annotate | Line # | Download | only in netipsec
xform_ipip.c revision 1.21
      1 /*	$NetBSD: xform_ipip.c,v 1.21 2008/02/10 21:42:20 degroote 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 (at) tla.org),
      7  * Angelos D. Keromytis (kermit (at) csd.uch.gr) and
      8  * Niels Provos (provos (at) 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.21 2008/02/10 21:42:20 degroote 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/xform.h>
     79 
     80 #include <netipsec/ipip_var.h>
     81 
     82 #ifdef MROUTING
     83 #include <netinet/ip_mroute.h>
     84 #endif
     85 
     86 #ifdef INET6
     87 #include <netinet/ip6.h>
     88 #include <netipsec/ipsec6.h>
     89 #  ifdef __FreeBSD__
     90 #  include <netinet6/ip6_ecn.h>
     91 #  endif
     92 #include <netinet6/in6_var.h>
     93 #include <netinet6/ip6protosw.h>
     94 #endif
     95 
     96 #include <netipsec/key.h>
     97 #include <netipsec/key_debug.h>
     98 #include <netipsec/ipsec_osdep.h>
     99 
    100 #include <machine/stdarg.h>
    101 
    102 #ifdef __FreeBSD__
    103 typedef void	pr_in_input_t (struct mbuf *, int, int); /* XXX FIX THIS */
    104 #else
    105 typedef void	pr_in_input_t (struct mbuf *m, ...);
    106 #endif
    107 
    108 /*
    109  * We can control the acceptance of IP4 packets by altering the sysctl
    110  * net.inet.ipip.allow value.  Zero means drop them, all else is acceptance.
    111  */
    112 int	ipip_allow = 0;
    113 struct	ipipstat ipipstat;
    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 		ipipstat.ipips_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 		ipipstat.ipips_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 	struct ifqueue *ifq = 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 nxt;
    205 	int isr;
    206 	u_int8_t otos;
    207 	u_int8_t v;
    208 	int hlen;
    209 
    210 	ipipstat.ipips_ipackets++;
    211 
    212 	m_copydata(m, 0, 1, &v);
    213 
    214 	switch (v >> 4) {
    215 #ifdef INET
    216         case 4:
    217 		hlen = sizeof(struct ip);
    218 		break;
    219 #endif /* INET */
    220 #ifdef INET6
    221         case 6:
    222 		hlen = sizeof(struct ip6_hdr);
    223 		break;
    224 #endif
    225         default:
    226 		DPRINTF(("_ipip_input: bad protocol version 0x%x (%u) "
    227 			"for outer header\n", v, v>>4));
    228 		ipipstat.ipips_family++;
    229 		m_freem(m);
    230 		return /* EAFNOSUPPORT */;
    231 	}
    232 
    233 	/* Bring the IP header in the first mbuf, if not there already */
    234 	if (m->m_len < hlen) {
    235 		if ((m = m_pullup(m, hlen)) == NULL) {
    236 			DPRINTF(("ipip_input: m_pullup (1) failed\n"));
    237 			ipipstat.ipips_hdrops++;
    238 			return;
    239 		}
    240 	}
    241 
    242 	ipo = mtod(m, struct ip *);
    243 
    244 #ifdef MROUTING
    245 	if (ipo->ip_v == IPVERSION && ipo->ip_p == IPPROTO_IPV4) {
    246 		if (IN_MULTICAST(((struct ip *)((char *) ipo + iphlen))->ip_dst.s_addr)) {
    247 			ipip_mroute_input (m, iphlen);
    248 			return;
    249 		}
    250 	}
    251 #endif /* MROUTING */
    252 
    253 	/* Keep outer ecn field. */
    254 	switch (v >> 4) {
    255 #ifdef INET
    256 	case 4:
    257 		otos = ipo->ip_tos;
    258 		break;
    259 #endif /* INET */
    260 #ifdef INET6
    261 	case 6:
    262 		otos = (ntohl(mtod(m, struct ip6_hdr *)->ip6_flow) >> 20) & 0xff;
    263 		break;
    264 #endif
    265 	default:
    266 		panic("ipip_input: unknown ip version %u (outer)", v>>4);
    267 	}
    268 
    269 	/* Remove outer IP header */
    270 	m_adj(m, iphlen);
    271 
    272 	/* Sanity check */
    273 	if (m->m_pkthdr.len < sizeof(struct ip))  {
    274 		ipipstat.ipips_hdrops++;
    275 		m_freem(m);
    276 		return;
    277 	}
    278 
    279 	m_copydata(m, 0, 1, &v);
    280 
    281 	switch (v >> 4) {
    282 #ifdef INET
    283         case 4:
    284 		hlen = sizeof(struct ip);
    285 		break;
    286 #endif /* INET */
    287 
    288 #ifdef INET6
    289         case 6:
    290 		hlen = sizeof(struct ip6_hdr);
    291 		break;
    292 #endif
    293 	default:
    294 		DPRINTF(("_ipip_input: bad protocol version 0x%x (%u) "
    295 			"for inner header\n", v, v>>4));
    296 		ipipstat.ipips_family++;
    297 		m_freem(m);
    298 		return; /* EAFNOSUPPORT */
    299 	}
    300 
    301 	/*
    302 	 * Bring the inner IP header in the first mbuf, if not there already.
    303 	 */
    304 	if (m->m_len < hlen) {
    305 		if ((m = m_pullup(m, hlen)) == NULL) {
    306 			DPRINTF(("ipip_input: m_pullup (2) failed\n"));
    307 			ipipstat.ipips_hdrops++;
    308 			return;
    309 		}
    310 	}
    311 
    312 	/*
    313 	 * RFC 1853 specifies that the inner TTL should not be touched on
    314 	 * decapsulation. There's no reason this comment should be here, but
    315 	 * this is as good as any a position.
    316 	 */
    317 
    318 	/* Some sanity checks in the inner IP header */
    319 	switch (v >> 4) {
    320 #ifdef INET
    321     	case 4:
    322                 ipo = mtod(m, struct ip *);
    323                 nxt = ipo->ip_p;
    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                 nxt = ip6->ip6_nxt;
    331 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    332 		ip_ecn_egress(ip6_ipsec_ecn, &otos, &itos);
    333 		ip6->ip6_flow &= ~htonl(0xff << 20);
    334 		ip6->ip6_flow |= htonl((u_int32_t) itos << 20);
    335                 break;
    336 #endif
    337 	default:
    338 		panic("ipip_input: unknown ip version %u (inner)", v>>4);
    339 	}
    340 
    341 	/* Check for local address spoofing. */
    342 	if ((m->m_pkthdr.rcvif == NULL ||
    343 	    !(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK)) &&
    344 	    ipip_allow != 2) {
    345 		IFNET_FOREACH(ifp) {
    346 			IFADDR_FOREACH(ifa, ifp) {
    347 #ifdef INET
    348 				if (ipo) {
    349 					if (ifa->ifa_addr->sa_family !=
    350 					    AF_INET)
    351 						continue;
    352 
    353 					sin = (struct sockaddr_in *) ifa->ifa_addr;
    354 
    355 					if (sin->sin_addr.s_addr ==
    356 					    ipo->ip_src.s_addr)	{
    357 						ipipstat.ipips_spoof++;
    358 						m_freem(m);
    359 						return;
    360 					}
    361 				}
    362 #endif /* INET */
    363 
    364 #ifdef INET6
    365 				if (ip6) {
    366 					if (ifa->ifa_addr->sa_family !=
    367 					    AF_INET6)
    368 						continue;
    369 
    370 					sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
    371 
    372 					if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_src)) {
    373 						ipipstat.ipips_spoof++;
    374 						m_freem(m);
    375 						return;
    376 					}
    377 
    378 				}
    379 #endif /* INET6 */
    380 			}
    381 		}
    382 	}
    383 
    384 	/* Statistics */
    385 	ipipstat.ipips_ibytes += m->m_pkthdr.len - iphlen;
    386 
    387 	/*
    388 	 * Interface pointer stays the same; if no IPsec processing has
    389 	 * been done (or will be done), this will point to a normal
    390 	 * interface. Otherwise, it'll point to an enc interface, which
    391 	 * will allow a packet filter to distinguish between secure and
    392 	 * untrusted packets.
    393 	 */
    394 
    395 	switch (v >> 4) {
    396 #ifdef INET
    397 	case 4:
    398 		ifq = &ipintrq;
    399 		isr = NETISR_IP;
    400 		break;
    401 #endif
    402 #ifdef INET6
    403 	case 6:
    404 		ifq = &ip6intrq;
    405 		isr = NETISR_IPV6;
    406 		break;
    407 #endif
    408 	default:
    409 		panic("ipip_input: should never reach here");
    410 	}
    411 
    412 	if (!IF_HANDOFF(ifq, m, NULL)) {
    413 		ipipstat.ipips_qfull++;
    414 
    415 		DPRINTF(("ipip_input: packet dropped because of full queue\n"));
    416 	} else {
    417 		schednetisr(isr);
    418 	}
    419 }
    420 
    421 int
    422 ipip_output(
    423     struct mbuf *m,
    424     struct ipsecrequest *isr,
    425     struct mbuf **mp,
    426     int skip,
    427     int protoff
    428 )
    429 {
    430 	struct secasvar *sav;
    431 	u_int8_t tp, otos;
    432 	struct secasindex *saidx;
    433 	int error;
    434 #ifdef INET
    435 	u_int8_t itos;
    436 	struct ip *ipo;
    437 #endif /* INET */
    438 #ifdef INET6
    439 	struct ip6_hdr *ip6, *ip6o;
    440 #endif /* INET6 */
    441 
    442 	IPSEC_SPLASSERT_SOFTNET("ipip_output");
    443 
    444 	sav = isr->sav;
    445 	IPSEC_ASSERT(sav != NULL, ("ipip_output: null SA"));
    446 	IPSEC_ASSERT(sav->sah != NULL, ("ipip_output: null SAH"));
    447 
    448 	/* XXX Deal with empty TDB source/destination addresses. */
    449 
    450 	m_copydata(m, 0, 1, &tp);
    451 	tp = (tp >> 4) & 0xff;  /* Get the IP version number. */
    452 
    453 	saidx = &sav->sah->saidx;
    454 	switch (saidx->dst.sa.sa_family) {
    455 #ifdef INET
    456 	case AF_INET:
    457 		if (saidx->src.sa.sa_family != AF_INET ||
    458 		    saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
    459 		    saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) {
    460 			DPRINTF(("ipip_output: unspecified tunnel endpoint "
    461 			    "address in SA %s/%08lx\n",
    462 			    ipsec_address(&saidx->dst),
    463 			    (u_long) ntohl(sav->spi)));
    464 			ipipstat.ipips_unspec++;
    465 			error = EINVAL;
    466 			goto bad;
    467 		}
    468 
    469 		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
    470 		if (m == 0) {
    471 			DPRINTF(("ipip_output: M_PREPEND failed\n"));
    472 			ipipstat.ipips_hdrops++;
    473 			error = ENOBUFS;
    474 			goto bad;
    475 		}
    476 
    477 		ipo = mtod(m, struct ip *);
    478 
    479 		ipo->ip_v = IPVERSION;
    480 		ipo->ip_hl = 5;
    481 		ipo->ip_len = htons(m->m_pkthdr.len);
    482 		ipo->ip_ttl = ip_defttl;
    483 		ipo->ip_sum = 0;
    484 		ipo->ip_src = saidx->src.sin.sin_addr;
    485 		ipo->ip_dst = saidx->dst.sin.sin_addr;
    486 
    487 #if defined(__NetBSD__)
    488 		ipo->ip_id = ip_newid(NULL);
    489 #elif defined(RANDOM_IP_ID)
    490 		ipo->ip_id = ip_randomid();
    491 #else
    492 		ipo->ip_id = htons(ip_id++);
    493 #endif
    494 
    495 		/* If the inner protocol is IP... */
    496 		if (tp == IPVERSION) {
    497 			/* Save ECN notification */
    498 			m_copydata(m, sizeof(struct ip) +
    499 			    offsetof(struct ip, ip_tos),
    500 			    sizeof(u_int8_t), &itos);
    501 
    502 			ipo->ip_p = IPPROTO_IPIP;
    503 
    504 			/*
    505 			 * We should be keeping tunnel soft-state and
    506 			 * send back ICMPs if needed.
    507 			 */
    508 			m_copydata(m, sizeof(struct ip) +
    509 			    offsetof(struct ip, ip_off),
    510 			    sizeof(u_int16_t), &ipo->ip_off);
    511 			ipo->ip_off &= ~ IP_OFF_CONVERT(IP_DF | IP_MF | IP_OFFMASK);
    512 		}
    513 #ifdef INET6
    514 		else if (tp == (IPV6_VERSION >> 4)) {
    515 			u_int32_t itos32;
    516 
    517 			/* Save ECN notification. */
    518 			m_copydata(m, sizeof(struct ip) +
    519 			    offsetof(struct ip6_hdr, ip6_flow),
    520 			    sizeof(u_int32_t), &itos32);
    521 			itos = ntohl(itos32) >> 20;
    522 			ipo->ip_p = IPPROTO_IPV6;
    523 			ipo->ip_off = 0;
    524 		}
    525 #endif /* INET6 */
    526 		else {
    527 			goto nofamily;
    528 		}
    529 
    530 		otos = 0;
    531 		ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
    532 		ipo->ip_tos = otos;
    533 		break;
    534 #endif /* INET */
    535 
    536 #ifdef INET6
    537 	case AF_INET6:
    538 		if (IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr) ||
    539 		    saidx->src.sa.sa_family != AF_INET6 ||
    540 		    IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr)) {
    541 			DPRINTF(("ipip_output: unspecified tunnel endpoint "
    542 			    "address in SA %s/%08lx\n",
    543 			    ipsec_address(&saidx->dst),
    544 			    (u_long) ntohl(sav->spi)));
    545 			ipipstat.ipips_unspec++;
    546 			error = ENOBUFS;
    547 			goto bad;
    548 		}
    549 
    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 		M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
    558 		if (m == 0) {
    559 			DPRINTF(("ipip_output: M_PREPEND failed\n"));
    560 			ipipstat.ipips_hdrops++;
    561 			error = ENOBUFS;
    562 			goto bad;
    563 		}
    564 
    565 		/* Initialize IPv6 header */
    566 		ip6o = mtod(m, struct ip6_hdr *);
    567 		ip6o->ip6_flow = 0;
    568 		ip6o->ip6_vfc &= ~IPV6_VERSION_MASK;
    569 		ip6o->ip6_vfc |= IPV6_VERSION;
    570 		ip6o->ip6_plen = htons(m->m_pkthdr.len);
    571 		ip6o->ip6_hlim = ip_defttl;
    572 		ip6o->ip6_dst = saidx->dst.sin6.sin6_addr;
    573 		ip6o->ip6_src = saidx->src.sin6.sin6_addr;
    574 
    575 #ifdef INET
    576 		if (tp == IPVERSION) {
    577 			/* Save ECN notification */
    578 			m_copydata(m, sizeof(struct ip6_hdr) +
    579 			    offsetof(struct ip, ip_tos), sizeof(u_int8_t),
    580 			    &itos);
    581 
    582 			/* This is really IPVERSION. */
    583 			ip6o->ip6_nxt = IPPROTO_IPIP;
    584 		} else
    585 #endif /* INET */
    586 			if (tp == (IPV6_VERSION >> 4)) {
    587 				u_int32_t itos32;
    588 
    589 				/* Save ECN notification. */
    590 				m_copydata(m, sizeof(struct ip6_hdr) +
    591 				    offsetof(struct ip6_hdr, ip6_flow),
    592 				    sizeof(u_int32_t), &itos32);
    593 				itos = ntohl(itos32) >> 20;
    594 
    595 				ip6o->ip6_nxt = IPPROTO_IPV6;
    596 			} else {
    597 				goto nofamily;
    598 			}
    599 
    600 		otos = 0;
    601 		ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
    602 		ip6o->ip6_flow |= htonl((u_int32_t) otos << 20);
    603 		break;
    604 #endif /* INET6 */
    605 
    606 	default:
    607 nofamily:
    608 		DPRINTF(("ipip_output: unsupported protocol family %u\n",
    609 		    saidx->dst.sa.sa_family));
    610 		ipipstat.ipips_family++;
    611 		error = EAFNOSUPPORT;		/* XXX diffs from openbsd */
    612 		goto bad;
    613 	}
    614 
    615 	ipipstat.ipips_opackets++;
    616 	*mp = m;
    617 
    618 #ifdef INET
    619 	if (saidx->dst.sa.sa_family == AF_INET) {
    620 #if 0
    621 		if (sav->tdb_xform->xf_type == XF_IP4)
    622 			tdb->tdb_cur_bytes +=
    623 			    m->m_pkthdr.len - sizeof(struct ip);
    624 #endif
    625 		ipipstat.ipips_obytes += 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 		ipipstat.ipips_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 #ifdef FAST_IPSEC
    650 static int
    651 ipe4_init(struct secasvar *sav, struct xformsw *xsp)
    652 {
    653 	sav->tdb_xform = xsp;
    654 	return 0;
    655 }
    656 
    657 static int
    658 ipe4_zeroize(struct secasvar *sav)
    659 {
    660 	sav->tdb_xform = NULL;
    661 	return 0;
    662 }
    663 
    664 static int
    665 ipe4_input(
    666     struct mbuf *m,
    667     struct secasvar *sav,
    668     int skip,
    669     int protoff
    670 )
    671 {
    672 	/* This is a rather serious mistake, so no conditional printing. */
    673 	printf("ipe4_input: should never be called\n");
    674 	if (m)
    675 		m_freem(m);
    676 	return EOPNOTSUPP;
    677 }
    678 
    679 static struct xformsw ipe4_xformsw = {
    680 	XF_IP4,		0,		"IPv4 Simple Encapsulation",
    681 	ipe4_init,	ipe4_zeroize,	ipe4_input,	ipip_output,
    682 	NULL,
    683 };
    684 
    685 #ifdef INET
    686 extern struct domain inetdomain;
    687 static struct ipprotosw ipe4_protosw = {
    688  .pr_type = SOCK_RAW,
    689  .pr_domain = &inetdomain,
    690  .pr_protocol = IPPROTO_IPV4,
    691  .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    692  .pr_input = ip4_input,
    693  .pr_output = 0,
    694  .pr_ctlinput = 0,
    695  .pr_ctloutput = rip_ctloutput,
    696  .pr_usrreq = rip_usrreq,
    697  .pr_init = 0,
    698  .pr_fasttimo = 0,
    699  .pr_slowtimo =	0,
    700  .pr_drain = 0,
    701 };
    702 #endif
    703 #ifdef INET6
    704 extern struct domain inet6domain;
    705 static struct ip6protosw ipe4_protosw6 = {
    706  .pr_type = SOCK_RAW,
    707  .pr_domain = &inet6domain,
    708  .pr_protocol = IPPROTO_IPV6,
    709  .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    710  .pr_input = ip4_input6,
    711  .pr_output = 0,
    712  .pr_ctlinput = 0,
    713  .pr_ctloutput = rip6_ctloutput,
    714  .pr_usrreq = rip6_usrreq,
    715  .pr_init = 0,
    716  .pr_fasttimo = 0,
    717  .pr_slowtimo = 0,
    718  .pr_drain = 0,
    719 };
    720 #endif
    721 
    722 #endif /* FAST_IPSEC */
    723 
    724 /*
    725  * Check the encapsulated packet to see if we want it
    726  */
    727 static int
    728 ipe4_encapcheck(struct mbuf *m,
    729     int off,
    730     int proto,
    731     void *arg
    732 )
    733 {
    734 	/*
    735 	 * Only take packets coming from IPSEC tunnels; the rest
    736 	 * must be handled by the gif tunnel code.  Note that we
    737 	 * also return a minimum priority when we want the packet
    738 	 * so any explicit gif tunnels take precedence.
    739 	 */
    740 	return ((m->m_flags & M_IPSEC) != 0 ? 1 : 0);
    741 }
    742 
    743 INITFN void
    744 ipe4_attach(void)
    745 {
    746 	xform_register(&ipe4_xformsw);
    747 	/* attach to encapsulation framework */
    748 	/* XXX save return cookie for detach on module remove */
    749 #ifdef INET
    750 	(void) encap_attach_func(AF_INET, -1,
    751 		ipe4_encapcheck, (struct protosw*) &ipe4_protosw, NULL);
    752 #endif
    753 #ifdef INET6
    754 	(void) encap_attach_func(AF_INET6, -1,
    755 		ipe4_encapcheck, (struct protosw*) &ipe4_protosw6, NULL);
    756 #endif
    757 }
    758 
    759 #ifdef SYSINIT
    760 SYSINIT(ipe4_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipe4_attach, NULL);
    761 #endif
    762 
    763