Home | History | Annotate | Line # | Download | only in netipsec
xform_ipip.c revision 1.43
      1 /*	$NetBSD: xform_ipip.c,v 1.43 2017/04/06 09:20:07 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 (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.43 2017/04/06 09:20:07 ozaki-r Exp $");
     43 
     44 /*
     45  * IP-inside-IP processing
     46  */
     47 #if defined(_KERNEL_OPT)
     48 #include "opt_inet.h"
     49 #ifdef __FreeBSD__
     50 #include "opt_inet6.h"
     51 #include "opt_random_ip_id.h"
     52 #endif /* __FreeBSD__ */
     53 #endif
     54 
     55 #include <sys/param.h>
     56 #include <sys/systm.h>
     57 #include <sys/mbuf.h>
     58 #include <sys/socket.h>
     59 #include <sys/kernel.h>
     60 #include <sys/protosw.h>
     61 #include <sys/sysctl.h>
     62 
     63 #include <net/if.h>
     64 #include <net/route.h>
     65 #include <net/netisr.h>
     66 
     67 #include <netinet/in.h>
     68 #include <netinet/in_systm.h>
     69 #include <netinet/in_var.h>
     70 #include <netinet/ip.h>
     71 #include <netinet/ip_ecn.h>
     72 #include <netinet/ip_var.h>
     73 #include <netinet/ip_encap.h>
     74 #ifdef __FreeBSD__
     75 #include <netinet/ipprotosw.h>
     76 #endif
     77 
     78 #include <netipsec/ipsec.h>
     79 #include <netipsec/ipsec_private.h>
     80 #include <netipsec/xform.h>
     81 
     82 #include <netipsec/ipip_var.h>
     83 
     84 #ifdef MROUTING
     85 #include <netinet/ip_mroute.h>
     86 #endif
     87 
     88 #ifdef INET6
     89 #include <netinet/ip6.h>
     90 #include <netipsec/ipsec6.h>
     91 #  ifdef __FreeBSD__
     92 #  include <netinet6/ip6_ecn.h>
     93 #  endif
     94 #include <netinet6/in6_var.h>
     95 #include <netinet6/ip6protosw.h>
     96 #endif
     97 
     98 #include <netipsec/key.h>
     99 #include <netipsec/key_debug.h>
    100 #include <netipsec/ipsec_osdep.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 
    114 percpu_t *ipipstat_percpu;
    115 
    116 #ifdef SYSCTL_DECL
    117 SYSCTL_DECL(_net_inet_ipip);
    118 
    119 SYSCTL_INT(_net_inet_ipip, OID_AUTO,
    120 	ipip_allow,	CTLFLAG_RW,	&ipip_allow,	0, "");
    121 SYSCTL_STRUCT(_net_inet_ipip, IPSECCTL_STATS,
    122 	stats,		CTLFLAG_RD,	&ipipstat,	ipipstat, "");
    123 
    124 #endif
    125 
    126 #ifdef __FreeBSD__
    127 static
    128 #endif
    129 void ipe4_attach(void);
    130 
    131 
    132 /* XXX IPCOMP */
    133 #define	M_IPSEC	(M_AUTHIPHDR|M_AUTHIPDGM|M_DECRYPTED)
    134 
    135 static void _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp);
    136 
    137 #ifdef INET6
    138 /*
    139  * Really only a wrapper for ipip_input(), for use with IPv6.
    140  */
    141 int
    142 ip4_input6(struct mbuf **m, int *offp, int proto)
    143 {
    144 #if 0
    145 	/* If we do not accept IP-in-IP explicitly, drop.  */
    146 	if (!ipip_allow && ((*m)->m_flags & M_IPSEC) == 0) {
    147 		DPRINTF(("ip4_input6: dropped due to policy\n"));
    148 		IPIP_STATINC(IPIP_STAT_PDROPS);
    149 		m_freem(*m);
    150 		return IPPROTO_DONE;
    151 	}
    152 #endif
    153 	_ipip_input(*m, *offp, NULL);
    154 	return IPPROTO_DONE;
    155 }
    156 #endif /* INET6 */
    157 
    158 #ifdef INET
    159 /*
    160  * Really only a wrapper for ipip_input(), for use with IPv4.
    161  */
    162 void
    163 ip4_input(struct mbuf *m, int off, int proto)
    164 {
    165 
    166 #if 0
    167 	/* If we do not accept IP-in-IP explicitly, drop.  */
    168 	if (!ipip_allow && (m->m_flags & M_IPSEC) == 0) {
    169 		DPRINTF(("ip4_input: dropped due to policy\n"));
    170 		IPIP_STATINC(IPIP_STAT_PDROPS);
    171 		m_freem(m);
    172 		return;
    173 	}
    174 #endif
    175 
    176 	_ipip_input(m, off, NULL);
    177 }
    178 #endif /* INET */
    179 
    180 /*
    181  * ipip_input gets called when we receive an IP{46} encapsulated packet,
    182  * either because we got it at a real interface, or because AH or ESP
    183  * were being used in tunnel mode (in which case the rcvif element will
    184  * contain the address of the encX interface associated with the tunnel.
    185  */
    186 
    187 static void
    188 _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp)
    189 {
    190 	register struct sockaddr_in *sin;
    191 	register struct ifnet *ifp;
    192 	register struct ifaddr *ifa;
    193 	pktqueue_t *pktq = NULL;
    194 	struct ip *ipo;
    195 #ifdef INET6
    196 	register struct sockaddr_in6 *sin6;
    197 	struct ip6_hdr *ip6 = NULL;
    198 	u_int8_t itos;
    199 #endif
    200 	u_int8_t otos;
    201 	u_int8_t v;
    202 	int hlen;
    203 
    204 	IPIP_STATINC(IPIP_STAT_IPACKETS);
    205 
    206 	m_copydata(m, 0, 1, &v);
    207 
    208 	switch (v >> 4) {
    209 #ifdef INET
    210         case 4:
    211 		hlen = sizeof(struct ip);
    212 		break;
    213 #endif /* INET */
    214 #ifdef INET6
    215         case 6:
    216 		hlen = sizeof(struct ip6_hdr);
    217 		break;
    218 #endif
    219         default:
    220 		DPRINTF(("_ipip_input: bad protocol version 0x%x (%u) "
    221 			"for outer header\n", v, v>>4));
    222 		IPIP_STATINC(IPIP_STAT_FAMILY);
    223 		m_freem(m);
    224 		return /* EAFNOSUPPORT */;
    225 	}
    226 
    227 	/* Bring the IP header in the first mbuf, if not there already */
    228 	if (m->m_len < hlen) {
    229 		if ((m = m_pullup(m, hlen)) == NULL) {
    230 			DPRINTF(("ipip_input: m_pullup (1) failed\n"));
    231 			IPIP_STATINC(IPIP_STAT_HDROPS);
    232 			return;
    233 		}
    234 	}
    235 
    236 	ipo = mtod(m, struct ip *);
    237 
    238 #ifdef MROUTING
    239 	if (ipo->ip_v == IPVERSION && ipo->ip_p == IPPROTO_IPV4) {
    240 		if (IN_MULTICAST(((struct ip *)((char *) ipo + iphlen))->ip_dst.s_addr)) {
    241 			ipip_mroute_input (m, iphlen);
    242 			return;
    243 		}
    244 	}
    245 #endif /* MROUTING */
    246 
    247 	/* Keep outer ecn field. */
    248 	switch (v >> 4) {
    249 #ifdef INET
    250 	case 4:
    251 		otos = ipo->ip_tos;
    252 		break;
    253 #endif /* INET */
    254 #ifdef INET6
    255 	case 6:
    256 		otos = (ntohl(mtod(m, struct ip6_hdr *)->ip6_flow) >> 20) & 0xff;
    257 		break;
    258 #endif
    259 	default:
    260 		panic("ipip_input: unknown ip version %u (outer)", v>>4);
    261 	}
    262 
    263 	/* Remove outer IP header */
    264 	m_adj(m, iphlen);
    265 
    266 	/* Sanity check */
    267 	if (m->m_pkthdr.len < sizeof(struct ip))  {
    268 		IPIP_STATINC(IPIP_STAT_HDROPS);
    269 		m_freem(m);
    270 		return;
    271 	}
    272 
    273 	m_copydata(m, 0, 1, &v);
    274 
    275 	switch (v >> 4) {
    276 #ifdef INET
    277         case 4:
    278 		hlen = sizeof(struct ip);
    279 		break;
    280 #endif /* INET */
    281 
    282 #ifdef INET6
    283         case 6:
    284 		hlen = sizeof(struct ip6_hdr);
    285 		break;
    286 #endif
    287 	default:
    288 		DPRINTF(("_ipip_input: bad protocol version 0x%x (%u) "
    289 			"for inner header\n", v, v>>4));
    290 		IPIP_STATINC(IPIP_STAT_FAMILY);
    291 		m_freem(m);
    292 		return; /* EAFNOSUPPORT */
    293 	}
    294 
    295 	/*
    296 	 * Bring the inner IP header in the first mbuf, if not there already.
    297 	 */
    298 	if (m->m_len < hlen) {
    299 		if ((m = m_pullup(m, hlen)) == NULL) {
    300 			DPRINTF(("ipip_input: m_pullup (2) failed\n"));
    301 			IPIP_STATINC(IPIP_STAT_HDROPS);
    302 			return;
    303 		}
    304 	}
    305 
    306 	/*
    307 	 * RFC 1853 specifies that the inner TTL should not be touched on
    308 	 * decapsulation. There's no reason this comment should be here, but
    309 	 * this is as good as any a position.
    310 	 */
    311 
    312 	/* Some sanity checks in the inner IP header */
    313 	switch (v >> 4) {
    314 #ifdef INET
    315     	case 4:
    316                 ipo = mtod(m, struct ip *);
    317 		ip_ecn_egress(ip4_ipsec_ecn, &otos, &ipo->ip_tos);
    318                 break;
    319 #endif /* INET */
    320 #ifdef INET6
    321     	case 6:
    322                 ip6 = (struct ip6_hdr *) ipo;
    323 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    324 		ip_ecn_egress(ip6_ipsec_ecn, &otos, &itos);
    325 		ip6->ip6_flow &= ~htonl(0xff << 20);
    326 		ip6->ip6_flow |= htonl((u_int32_t) itos << 20);
    327                 break;
    328 #endif
    329 	default:
    330 		panic("ipip_input: unknown ip version %u (inner)", v>>4);
    331 	}
    332 
    333 	/* Check for local address spoofing. */
    334 	if ((m_get_rcvif_NOMPSAFE(m) == NULL ||
    335 	    !(m_get_rcvif_NOMPSAFE(m)->if_flags & IFF_LOOPBACK)) &&
    336 	    ipip_allow != 2) {
    337 		int s = pserialize_read_enter();
    338 		IFNET_READER_FOREACH(ifp) {
    339 			IFADDR_READER_FOREACH(ifa, ifp) {
    340 #ifdef INET
    341 				if (ipo) {
    342 					if (ifa->ifa_addr->sa_family !=
    343 					    AF_INET)
    344 						continue;
    345 
    346 					sin = (struct sockaddr_in *) ifa->ifa_addr;
    347 
    348 					if (sin->sin_addr.s_addr ==
    349 					    ipo->ip_src.s_addr)	{
    350 						pserialize_read_exit(s);
    351 						IPIP_STATINC(IPIP_STAT_SPOOF);
    352 						m_freem(m);
    353 						return;
    354 					}
    355 				}
    356 #endif /* INET */
    357 
    358 #ifdef INET6
    359 				if (ip6) {
    360 					if (ifa->ifa_addr->sa_family !=
    361 					    AF_INET6)
    362 						continue;
    363 
    364 					sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
    365 
    366 					if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_src)) {
    367 						pserialize_read_exit(s);
    368 						IPIP_STATINC(IPIP_STAT_SPOOF);
    369 						m_freem(m);
    370 						return;
    371 					}
    372 
    373 				}
    374 #endif /* INET6 */
    375 			}
    376 		}
    377 		pserialize_read_exit(s);
    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 static struct encapsw ipe4_encapsw = {
    686 	.encapsw4 = {
    687 		.pr_input = ip4_input,
    688 		.pr_ctlinput = NULL,
    689 	}
    690 };
    691 #endif
    692 #ifdef INET6
    693 static struct encapsw ipe4_encapsw6 = {
    694 	.encapsw6 = {
    695 		.pr_input = ip4_input6,
    696 		.pr_ctlinput = NULL,
    697 	}
    698 };
    699 #endif
    700 
    701 /*
    702  * Check the encapsulated packet to see if we want it
    703  */
    704 static int
    705 ipe4_encapcheck(struct mbuf *m,
    706     int off,
    707     int proto,
    708     void *arg
    709 )
    710 {
    711 	/*
    712 	 * Only take packets coming from IPSEC tunnels; the rest
    713 	 * must be handled by the gif tunnel code.  Note that we
    714 	 * also return a minimum priority when we want the packet
    715 	 * so any explicit gif tunnels take precedence.
    716 	 */
    717 	return ((m->m_flags & M_IPSEC) != 0 ? 1 : 0);
    718 }
    719 
    720 INITFN void
    721 ipe4_attach(void)
    722 {
    723 
    724 	ipipstat_percpu = percpu_alloc(sizeof(uint64_t) * IPIP_NSTATS);
    725 
    726 	xform_register(&ipe4_xformsw);
    727 	/* attach to encapsulation framework */
    728 	/* XXX save return cookie for detach on module remove */
    729 
    730 	encapinit();
    731 	/* This function is called before ifinit(). Who else gets lock? */
    732 	(void)encap_lock_enter();
    733 	/* ipe4_encapsw and ipe4_encapsw must be added atomically */
    734 #ifdef INET
    735 	(void) encap_attach_func(AF_INET, -1,
    736 		ipe4_encapcheck, &ipe4_encapsw, NULL);
    737 #endif
    738 #ifdef INET6
    739 	(void) encap_attach_func(AF_INET6, -1,
    740 		ipe4_encapcheck, &ipe4_encapsw6, NULL);
    741 #endif
    742 	encap_lock_exit();
    743 }
    744 
    745 #ifdef SYSINIT
    746 SYSINIT(ipe4_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipe4_attach, NULL);
    747 #endif
    748 
    749