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