Home | History | Annotate | Line # | Download | only in netipsec
xform_ipip.c revision 1.68
      1  1.68      maxv /*	$NetBSD: xform_ipip.c,v 1.68 2018/04/24 08:07:06 maxv Exp $	*/
      2  1.66      maxv /*	$FreeBSD: xform_ipip.c,v 1.3.2.1 2003/01/24 05:11:36 sam Exp $	*/
      3   1.1  jonathan /*	$OpenBSD: ip_ipip.c,v 1.25 2002/06/10 18:04:55 itojun Exp $ */
      4   1.1  jonathan 
      5   1.1  jonathan /*
      6   1.1  jonathan  * The authors of this code are John Ioannidis (ji (at) tla.org),
      7   1.1  jonathan  * Angelos D. Keromytis (kermit (at) csd.uch.gr) and
      8   1.1  jonathan  * Niels Provos (provos (at) physnet.uni-hamburg.de).
      9   1.1  jonathan  *
     10   1.1  jonathan  * The original version of this code was written by John Ioannidis
     11   1.1  jonathan  * for BSD/OS in Athens, Greece, in November 1995.
     12   1.1  jonathan  *
     13   1.1  jonathan  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
     14   1.1  jonathan  * by Angelos D. Keromytis.
     15   1.1  jonathan  *
     16   1.1  jonathan  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
     17   1.1  jonathan  * and Niels Provos.
     18   1.1  jonathan  *
     19   1.1  jonathan  * Additional features in 1999 by Angelos D. Keromytis.
     20   1.1  jonathan  *
     21   1.1  jonathan  * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
     22   1.1  jonathan  * Angelos D. Keromytis and Niels Provos.
     23   1.1  jonathan  * Copyright (c) 2001, Angelos D. Keromytis.
     24   1.1  jonathan  *
     25   1.1  jonathan  * Permission to use, copy, and modify this software with or without fee
     26   1.1  jonathan  * is hereby granted, provided that this entire notice is included in
     27   1.1  jonathan  * all copies of any software which is or includes a copy or
     28   1.1  jonathan  * modification of this software.
     29   1.1  jonathan  * You may use this code under the GNU public license if you so wish. Please
     30   1.1  jonathan  * contribute changes back to the authors under this freer than GPL license
     31   1.1  jonathan  * so that we may further the use of strong encryption without limitations to
     32   1.1  jonathan  * all.
     33   1.1  jonathan  *
     34   1.1  jonathan  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
     35   1.1  jonathan  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
     36   1.1  jonathan  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
     37   1.1  jonathan  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
     38   1.1  jonathan  * PURPOSE.
     39   1.1  jonathan  */
     40   1.1  jonathan 
     41   1.1  jonathan #include <sys/cdefs.h>
     42  1.68      maxv __KERNEL_RCSID(0, "$NetBSD: xform_ipip.c,v 1.68 2018/04/24 08:07:06 maxv Exp $");
     43   1.1  jonathan 
     44   1.1  jonathan /*
     45   1.1  jonathan  * IP-inside-IP processing
     46   1.1  jonathan  */
     47  1.43     ozaki #if defined(_KERNEL_OPT)
     48   1.1  jonathan #include "opt_inet.h"
     49  1.43     ozaki #endif
     50   1.1  jonathan 
     51   1.1  jonathan #include <sys/param.h>
     52   1.1  jonathan #include <sys/systm.h>
     53   1.1  jonathan #include <sys/mbuf.h>
     54   1.1  jonathan #include <sys/socket.h>
     55   1.1  jonathan #include <sys/kernel.h>
     56  1.35  riastrad #include <sys/protosw.h>
     57   1.1  jonathan #include <sys/sysctl.h>
     58   1.1  jonathan 
     59   1.1  jonathan #include <net/if.h>
     60   1.1  jonathan #include <net/route.h>
     61   1.1  jonathan #include <net/netisr.h>
     62   1.1  jonathan 
     63   1.1  jonathan #include <netinet/in.h>
     64   1.1  jonathan #include <netinet/in_systm.h>
     65   1.1  jonathan #include <netinet/in_var.h>
     66   1.1  jonathan #include <netinet/ip.h>
     67   1.1  jonathan #include <netinet/ip_ecn.h>
     68   1.1  jonathan #include <netinet/ip_var.h>
     69   1.1  jonathan #include <netinet/ip_encap.h>
     70   1.1  jonathan 
     71   1.1  jonathan #include <netipsec/ipsec.h>
     72  1.22   thorpej #include <netipsec/ipsec_private.h>
     73   1.1  jonathan #include <netipsec/xform.h>
     74   1.1  jonathan 
     75   1.1  jonathan #include <netipsec/ipip_var.h>
     76   1.1  jonathan 
     77   1.1  jonathan #ifdef INET6
     78   1.1  jonathan #include <netinet/ip6.h>
     79   1.1  jonathan #include <netipsec/ipsec6.h>
     80   1.1  jonathan #include <netinet6/in6_var.h>
     81  1.35  riastrad #include <netinet6/ip6protosw.h>
     82   1.1  jonathan #endif
     83   1.1  jonathan 
     84   1.5       tls #include <netipsec/key.h>
     85   1.5       tls #include <netipsec/key_debug.h>
     86   1.1  jonathan 
     87  1.59      maxv /* XXX IPCOMP */
     88  1.68      maxv #define	M_IPSEC	(M_AUTHIPHDR|M_DECRYPTED)
     89   1.1  jonathan 
     90  1.67      maxv int ipip_spoofcheck = 1;
     91  1.22   thorpej percpu_t *ipipstat_percpu;
     92   1.1  jonathan 
     93   1.1  jonathan void ipe4_attach(void);
     94   1.1  jonathan 
     95  1.65      maxv static void _ipip_input(struct mbuf *, int);
     96   1.1  jonathan 
     97   1.1  jonathan #ifdef INET6
     98   1.1  jonathan int
     99  1.55  knakahar ip4_input6(struct mbuf **m, int *offp, int proto, void *eparg __unused)
    100   1.1  jonathan {
    101  1.65      maxv 	_ipip_input(*m, *offp);
    102   1.1  jonathan 	return IPPROTO_DONE;
    103   1.1  jonathan }
    104  1.59      maxv #endif
    105   1.1  jonathan 
    106   1.1  jonathan #ifdef INET
    107   1.1  jonathan void
    108  1.55  knakahar ip4_input(struct mbuf *m, int off, int proto, void *eparg __unused)
    109   1.1  jonathan {
    110  1.65      maxv 	_ipip_input(m, off);
    111   1.1  jonathan }
    112  1.59      maxv #endif
    113   1.1  jonathan 
    114   1.1  jonathan /*
    115  1.65      maxv  * _ipip_input gets called when we receive an IP{46} encapsulated packet,
    116  1.65      maxv  * because AH or ESP were being used in tunnel mode.
    117   1.1  jonathan  */
    118   1.1  jonathan static void
    119  1.65      maxv _ipip_input(struct mbuf *m, int iphlen)
    120   1.1  jonathan {
    121   1.1  jonathan 	register struct sockaddr_in *sin;
    122   1.1  jonathan 	register struct ifnet *ifp;
    123   1.1  jonathan 	register struct ifaddr *ifa;
    124  1.31     rmind 	pktqueue_t *pktq = NULL;
    125  1.63      maxv 	struct ip *ip4 = NULL;
    126   1.1  jonathan #ifdef INET6
    127   1.1  jonathan 	register struct sockaddr_in6 *sin6;
    128   1.1  jonathan 	struct ip6_hdr *ip6 = NULL;
    129  1.45  christos 	uint8_t itos;
    130   1.1  jonathan #endif
    131  1.45  christos 	uint8_t otos;
    132  1.45  christos 	uint8_t v;
    133   1.1  jonathan 	int hlen;
    134   1.1  jonathan 
    135  1.22   thorpej 	IPIP_STATINC(IPIP_STAT_IPACKETS);
    136   1.1  jonathan 
    137   1.1  jonathan 	m_copydata(m, 0, 1, &v);
    138   1.1  jonathan 
    139   1.1  jonathan 	switch (v >> 4) {
    140   1.1  jonathan #ifdef INET
    141  1.58      maxv 	case 4:
    142   1.1  jonathan 		hlen = sizeof(struct ip);
    143   1.1  jonathan 		break;
    144  1.59      maxv #endif
    145   1.1  jonathan #ifdef INET6
    146  1.58      maxv 	case 6:
    147   1.1  jonathan 		hlen = sizeof(struct ip6_hdr);
    148   1.1  jonathan 		break;
    149   1.1  jonathan #endif
    150  1.58      maxv 	default:
    151  1.45  christos 		DPRINTF(("%s: bad protocol version 0x%x (%u) "
    152  1.45  christos 		    "for outer header\n", __func__, v, v>>4));
    153  1.22   thorpej 		IPIP_STATINC(IPIP_STAT_FAMILY);
    154   1.1  jonathan 		m_freem(m);
    155  1.59      maxv 		return;
    156   1.1  jonathan 	}
    157   1.1  jonathan 
    158   1.1  jonathan 	/* Bring the IP header in the first mbuf, if not there already */
    159   1.1  jonathan 	if (m->m_len < hlen) {
    160   1.1  jonathan 		if ((m = m_pullup(m, hlen)) == NULL) {
    161  1.45  christos 			DPRINTF(("%s: m_pullup (1) failed\n", __func__));
    162  1.22   thorpej 			IPIP_STATINC(IPIP_STAT_HDROPS);
    163   1.1  jonathan 			return;
    164   1.1  jonathan 		}
    165   1.1  jonathan 	}
    166   1.1  jonathan 
    167   1.1  jonathan 	/* Keep outer ecn field. */
    168   1.1  jonathan 	switch (v >> 4) {
    169   1.1  jonathan #ifdef INET
    170   1.1  jonathan 	case 4:
    171  1.63      maxv 		otos = mtod(m, struct ip *)->ip_tos;
    172   1.1  jonathan 		break;
    173  1.59      maxv #endif
    174   1.1  jonathan #ifdef INET6
    175   1.1  jonathan 	case 6:
    176   1.1  jonathan 		otos = (ntohl(mtod(m, struct ip6_hdr *)->ip6_flow) >> 20) & 0xff;
    177   1.1  jonathan 		break;
    178   1.1  jonathan #endif
    179   1.1  jonathan 	default:
    180  1.65      maxv 		panic("%s: impossible (1)", __func__);
    181   1.1  jonathan 	}
    182   1.1  jonathan 
    183   1.1  jonathan 	/* Remove outer IP header */
    184   1.1  jonathan 	m_adj(m, iphlen);
    185   1.1  jonathan 
    186   1.1  jonathan 	/* Sanity check */
    187   1.1  jonathan 	if (m->m_pkthdr.len < sizeof(struct ip))  {
    188  1.22   thorpej 		IPIP_STATINC(IPIP_STAT_HDROPS);
    189   1.1  jonathan 		m_freem(m);
    190   1.1  jonathan 		return;
    191   1.1  jonathan 	}
    192   1.1  jonathan 
    193   1.1  jonathan 	m_copydata(m, 0, 1, &v);
    194   1.1  jonathan 
    195   1.1  jonathan 	switch (v >> 4) {
    196   1.1  jonathan #ifdef INET
    197  1.58      maxv 	case 4:
    198   1.1  jonathan 		hlen = sizeof(struct ip);
    199  1.61      maxv 		pktq = ip_pktq;
    200   1.1  jonathan 		break;
    201  1.59      maxv #endif
    202   1.1  jonathan #ifdef INET6
    203  1.58      maxv 	case 6:
    204   1.1  jonathan 		hlen = sizeof(struct ip6_hdr);
    205  1.61      maxv 		pktq = ip6_pktq;
    206   1.1  jonathan 		break;
    207   1.1  jonathan #endif
    208   1.1  jonathan 	default:
    209  1.45  christos 		DPRINTF(("%s: bad protocol version %#x (%u) "
    210  1.45  christos 		    "for inner header\n", __func__, v, v >> 4));
    211  1.22   thorpej 		IPIP_STATINC(IPIP_STAT_FAMILY);
    212   1.1  jonathan 		m_freem(m);
    213  1.59      maxv 		return;
    214   1.1  jonathan 	}
    215   1.1  jonathan 
    216   1.1  jonathan 	/*
    217   1.1  jonathan 	 * Bring the inner IP header in the first mbuf, if not there already.
    218   1.1  jonathan 	 */
    219   1.1  jonathan 	if (m->m_len < hlen) {
    220   1.1  jonathan 		if ((m = m_pullup(m, hlen)) == NULL) {
    221  1.45  christos 			DPRINTF(("%s: m_pullup (2) failed\n", __func__));
    222  1.22   thorpej 			IPIP_STATINC(IPIP_STAT_HDROPS);
    223   1.1  jonathan 			return;
    224   1.1  jonathan 		}
    225   1.1  jonathan 	}
    226   1.1  jonathan 
    227   1.1  jonathan 	/*
    228   1.1  jonathan 	 * RFC 1853 specifies that the inner TTL should not be touched on
    229   1.1  jonathan 	 * decapsulation. There's no reason this comment should be here, but
    230   1.1  jonathan 	 * this is as good as any a position.
    231   1.1  jonathan 	 */
    232   1.1  jonathan 
    233   1.1  jonathan 	/* Some sanity checks in the inner IP header */
    234   1.1  jonathan 	switch (v >> 4) {
    235   1.1  jonathan #ifdef INET
    236  1.58      maxv 	case 4:
    237  1.63      maxv 		ip4 = mtod(m, struct ip *);
    238  1.63      maxv 		ip_ecn_egress(ip4_ipsec_ecn, &otos, &ip4->ip_tos);
    239  1.58      maxv 		break;
    240  1.59      maxv #endif
    241   1.1  jonathan #ifdef INET6
    242  1.58      maxv 	case 6:
    243  1.57      maxv 		ip6 = mtod(m, struct ip6_hdr *);
    244   1.1  jonathan 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    245   1.1  jonathan 		ip_ecn_egress(ip6_ipsec_ecn, &otos, &itos);
    246   1.1  jonathan 		ip6->ip6_flow &= ~htonl(0xff << 20);
    247  1.63      maxv 		ip6->ip6_flow |= htonl((uint32_t)itos << 20);
    248  1.58      maxv 		break;
    249   1.1  jonathan #endif
    250   1.1  jonathan 	default:
    251  1.65      maxv 		panic("%s: impossible (2)", __func__);
    252   1.1  jonathan 	}
    253   1.1  jonathan 
    254   1.1  jonathan 	/* Check for local address spoofing. */
    255  1.39     ozaki 	if ((m_get_rcvif_NOMPSAFE(m) == NULL ||
    256  1.39     ozaki 	    !(m_get_rcvif_NOMPSAFE(m)->if_flags & IFF_LOOPBACK)) &&
    257  1.67      maxv 	    ipip_spoofcheck) {
    258  1.38     ozaki 		int s = pserialize_read_enter();
    259  1.38     ozaki 		IFNET_READER_FOREACH(ifp) {
    260  1.42     ozaki 			IFADDR_READER_FOREACH(ifa, ifp) {
    261   1.1  jonathan #ifdef INET
    262  1.63      maxv 				if (ip4) {
    263   1.1  jonathan 					if (ifa->ifa_addr->sa_family !=
    264   1.1  jonathan 					    AF_INET)
    265   1.1  jonathan 						continue;
    266   1.1  jonathan 
    267  1.64      maxv 					sin = (struct sockaddr_in *)ifa->ifa_addr;
    268   1.1  jonathan 
    269   1.1  jonathan 					if (sin->sin_addr.s_addr ==
    270  1.63      maxv 					    ip4->ip_src.s_addr)	{
    271  1.38     ozaki 						pserialize_read_exit(s);
    272  1.22   thorpej 						IPIP_STATINC(IPIP_STAT_SPOOF);
    273   1.1  jonathan 						m_freem(m);
    274   1.1  jonathan 						return;
    275   1.1  jonathan 					}
    276   1.1  jonathan 				}
    277  1.64      maxv #endif
    278   1.1  jonathan 
    279   1.1  jonathan #ifdef INET6
    280   1.1  jonathan 				if (ip6) {
    281   1.1  jonathan 					if (ifa->ifa_addr->sa_family !=
    282   1.1  jonathan 					    AF_INET6)
    283   1.1  jonathan 						continue;
    284   1.1  jonathan 
    285  1.64      maxv 					sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
    286   1.1  jonathan 
    287   1.1  jonathan 					if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_src)) {
    288  1.38     ozaki 						pserialize_read_exit(s);
    289  1.22   thorpej 						IPIP_STATINC(IPIP_STAT_SPOOF);
    290   1.1  jonathan 						m_freem(m);
    291   1.1  jonathan 						return;
    292   1.1  jonathan 					}
    293   1.1  jonathan 
    294   1.1  jonathan 				}
    295  1.64      maxv #endif
    296   1.1  jonathan 			}
    297   1.1  jonathan 		}
    298  1.38     ozaki 		pserialize_read_exit(s);
    299   1.1  jonathan 	}
    300   1.1  jonathan 
    301  1.62      maxv 	/* Statistics: m->m_pkthdr.len is the length of the inner packet */
    302  1.62      maxv 	IPIP_STATADD(IPIP_STAT_IBYTES, m->m_pkthdr.len);
    303   1.1  jonathan 
    304   1.1  jonathan 	/*
    305   1.1  jonathan 	 * Interface pointer stays the same; if no IPsec processing has
    306   1.1  jonathan 	 * been done (or will be done), this will point to a normal
    307   1.1  jonathan 	 * interface. Otherwise, it'll point to an enc interface, which
    308   1.1  jonathan 	 * will allow a packet filter to distinguish between secure and
    309   1.1  jonathan 	 * untrusted packets.
    310   1.1  jonathan 	 */
    311   1.1  jonathan 
    312  1.31     rmind 	int s = splnet();
    313  1.31     rmind 	if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
    314  1.22   thorpej 		IPIP_STATINC(IPIP_STAT_QFULL);
    315  1.31     rmind 		m_freem(m);
    316   1.1  jonathan 	}
    317  1.31     rmind 	splx(s);
    318   1.1  jonathan }
    319   1.1  jonathan 
    320   1.1  jonathan int
    321  1.59      maxv ipip_output(struct mbuf *m, const struct ipsecrequest *isr,
    322  1.59      maxv     struct secasvar *sav, struct mbuf **mp, int skip, int protoff)
    323   1.1  jonathan {
    324  1.49       ryo 	char buf[IPSEC_ADDRSTRLEN];
    325  1.45  christos 	uint8_t tp, otos;
    326   1.1  jonathan 	struct secasindex *saidx;
    327  1.60      maxv 	int error, iphlen;
    328   1.1  jonathan #ifdef INET
    329  1.45  christos 	uint8_t itos;
    330   1.1  jonathan 	struct ip *ipo;
    331  1.59      maxv #endif
    332   1.1  jonathan #ifdef INET6
    333   1.1  jonathan 	struct ip6_hdr *ip6, *ip6o;
    334  1.59      maxv #endif
    335   1.1  jonathan 
    336  1.45  christos 	IPSEC_SPLASSERT_SOFTNET(__func__);
    337  1.53     ozaki 	KASSERT(sav != NULL);
    338   1.1  jonathan 
    339   1.1  jonathan 	/* XXX Deal with empty TDB source/destination addresses. */
    340   1.1  jonathan 
    341   1.1  jonathan 	m_copydata(m, 0, 1, &tp);
    342   1.1  jonathan 	tp = (tp >> 4) & 0xff;  /* Get the IP version number. */
    343   1.1  jonathan 
    344   1.1  jonathan 	saidx = &sav->sah->saidx;
    345   1.1  jonathan 	switch (saidx->dst.sa.sa_family) {
    346   1.1  jonathan #ifdef INET
    347   1.1  jonathan 	case AF_INET:
    348   1.1  jonathan 		if (saidx->src.sa.sa_family != AF_INET ||
    349   1.1  jonathan 		    saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
    350   1.1  jonathan 		    saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) {
    351  1.45  christos 			DPRINTF(("%s: unspecified tunnel endpoint "
    352  1.45  christos 			    "address in SA %s/%08lx\n", __func__,
    353  1.49       ryo 			    ipsec_address(&saidx->dst, buf, sizeof(buf)),
    354  1.64      maxv 			    (u_long)ntohl(sav->spi)));
    355  1.22   thorpej 			IPIP_STATINC(IPIP_STAT_UNSPEC);
    356   1.1  jonathan 			error = EINVAL;
    357   1.1  jonathan 			goto bad;
    358   1.1  jonathan 		}
    359   1.1  jonathan 
    360   1.1  jonathan 		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
    361  1.59      maxv 		if (m == NULL) {
    362  1.45  christos 			DPRINTF(("%s: M_PREPEND failed\n", __func__));
    363  1.22   thorpej 			IPIP_STATINC(IPIP_STAT_HDROPS);
    364   1.1  jonathan 			error = ENOBUFS;
    365   1.1  jonathan 			goto bad;
    366   1.1  jonathan 		}
    367   1.1  jonathan 
    368  1.60      maxv 		iphlen = sizeof(struct ip);
    369  1.60      maxv 
    370   1.1  jonathan 		ipo = mtod(m, struct ip *);
    371   1.1  jonathan 		ipo->ip_v = IPVERSION;
    372   1.1  jonathan 		ipo->ip_hl = 5;
    373   1.1  jonathan 		ipo->ip_len = htons(m->m_pkthdr.len);
    374   1.1  jonathan 		ipo->ip_ttl = ip_defttl;
    375   1.1  jonathan 		ipo->ip_sum = 0;
    376   1.1  jonathan 		ipo->ip_src = saidx->src.sin.sin_addr;
    377   1.1  jonathan 		ipo->ip_dst = saidx->dst.sin.sin_addr;
    378  1.21  degroote 		ipo->ip_id = ip_newid(NULL);
    379   1.1  jonathan 
    380   1.1  jonathan 		/* If the inner protocol is IP... */
    381   1.1  jonathan 		if (tp == IPVERSION) {
    382   1.1  jonathan 			/* Save ECN notification */
    383   1.1  jonathan 			m_copydata(m, sizeof(struct ip) +
    384   1.1  jonathan 			    offsetof(struct ip, ip_tos),
    385  1.45  christos 			    sizeof(uint8_t), &itos);
    386   1.1  jonathan 
    387   1.1  jonathan 			ipo->ip_p = IPPROTO_IPIP;
    388   1.1  jonathan 
    389   1.1  jonathan 			/*
    390   1.1  jonathan 			 * We should be keeping tunnel soft-state and
    391   1.1  jonathan 			 * send back ICMPs if needed.
    392   1.1  jonathan 			 */
    393   1.1  jonathan 			m_copydata(m, sizeof(struct ip) +
    394   1.1  jonathan 			    offsetof(struct ip, ip_off),
    395  1.45  christos 			    sizeof(uint16_t), &ipo->ip_off);
    396  1.48     ozaki 			ipo->ip_off &= ~ htons(IP_DF | IP_MF | IP_OFFMASK);
    397   1.1  jonathan 		}
    398   1.1  jonathan #ifdef INET6
    399   1.1  jonathan 		else if (tp == (IPV6_VERSION >> 4)) {
    400  1.45  christos 			uint32_t itos32;
    401   1.1  jonathan 
    402   1.1  jonathan 			/* Save ECN notification. */
    403   1.1  jonathan 			m_copydata(m, sizeof(struct ip) +
    404   1.1  jonathan 			    offsetof(struct ip6_hdr, ip6_flow),
    405  1.45  christos 			    sizeof(uint32_t), &itos32);
    406   1.1  jonathan 			itos = ntohl(itos32) >> 20;
    407   1.1  jonathan 			ipo->ip_p = IPPROTO_IPV6;
    408   1.1  jonathan 			ipo->ip_off = 0;
    409   1.1  jonathan 		}
    410  1.64      maxv #endif
    411   1.1  jonathan 		else {
    412   1.1  jonathan 			goto nofamily;
    413   1.1  jonathan 		}
    414   1.1  jonathan 
    415   1.1  jonathan 		otos = 0;
    416   1.1  jonathan 		ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
    417   1.1  jonathan 		ipo->ip_tos = otos;
    418   1.1  jonathan 		break;
    419   1.1  jonathan #endif /* INET */
    420   1.1  jonathan 
    421   1.1  jonathan #ifdef INET6
    422   1.1  jonathan 	case AF_INET6:
    423   1.1  jonathan 		if (IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr) ||
    424   1.1  jonathan 		    saidx->src.sa.sa_family != AF_INET6 ||
    425   1.1  jonathan 		    IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr)) {
    426  1.45  christos 			DPRINTF(("%s: unspecified tunnel endpoint "
    427  1.45  christos 			    "address in SA %s/%08lx\n", __func__,
    428  1.49       ryo 			    ipsec_address(&saidx->dst, buf, sizeof(buf)),
    429  1.64      maxv 			    (u_long)ntohl(sav->spi)));
    430  1.22   thorpej 			IPIP_STATINC(IPIP_STAT_UNSPEC);
    431   1.1  jonathan 			error = ENOBUFS;
    432   1.1  jonathan 			goto bad;
    433   1.1  jonathan 		}
    434   1.1  jonathan 
    435  1.27  drochner 		if (tp == (IPV6_VERSION >> 4)) {
    436  1.27  drochner 			/* scoped address handling */
    437  1.27  drochner 			ip6 = mtod(m, struct ip6_hdr *);
    438  1.27  drochner 			if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
    439  1.27  drochner 				ip6->ip6_src.s6_addr16[1] = 0;
    440  1.27  drochner 			if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
    441  1.27  drochner 				ip6->ip6_dst.s6_addr16[1] = 0;
    442  1.27  drochner 		}
    443   1.1  jonathan 
    444   1.1  jonathan 		M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
    445  1.59      maxv 		if (m == NULL) {
    446  1.45  christos 			DPRINTF(("%s: M_PREPEND failed\n", __func__));
    447  1.22   thorpej 			IPIP_STATINC(IPIP_STAT_HDROPS);
    448   1.1  jonathan 			error = ENOBUFS;
    449   1.1  jonathan 			goto bad;
    450   1.1  jonathan 		}
    451   1.1  jonathan 
    452  1.60      maxv 		iphlen = sizeof(struct ip6_hdr);
    453  1.60      maxv 
    454   1.1  jonathan 		/* Initialize IPv6 header */
    455   1.1  jonathan 		ip6o = mtod(m, struct ip6_hdr *);
    456   1.1  jonathan 		ip6o->ip6_flow = 0;
    457   1.1  jonathan 		ip6o->ip6_vfc &= ~IPV6_VERSION_MASK;
    458   1.1  jonathan 		ip6o->ip6_vfc |= IPV6_VERSION;
    459  1.44  christos 		ip6o->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6o));
    460   1.1  jonathan 		ip6o->ip6_hlim = ip_defttl;
    461   1.1  jonathan 		ip6o->ip6_dst = saidx->dst.sin6.sin6_addr;
    462   1.1  jonathan 		ip6o->ip6_src = saidx->src.sin6.sin6_addr;
    463  1.27  drochner 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6o->ip6_dst))
    464  1.27  drochner 			ip6o->ip6_dst.s6_addr16[1] = htons(saidx->dst.sin6.sin6_scope_id);
    465  1.27  drochner 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6o->ip6_src))
    466  1.27  drochner 			ip6o->ip6_src.s6_addr16[1] = htons(saidx->src.sin6.sin6_scope_id);
    467   1.1  jonathan 
    468   1.1  jonathan #ifdef INET
    469   1.1  jonathan 		if (tp == IPVERSION) {
    470   1.1  jonathan 			/* Save ECN notification */
    471   1.1  jonathan 			m_copydata(m, sizeof(struct ip6_hdr) +
    472  1.45  christos 			    offsetof(struct ip, ip_tos), sizeof(uint8_t),
    473  1.17  degroote 			    &itos);
    474   1.1  jonathan 
    475   1.1  jonathan 			/* This is really IPVERSION. */
    476   1.1  jonathan 			ip6o->ip6_nxt = IPPROTO_IPIP;
    477   1.1  jonathan 		} else
    478  1.64      maxv #endif
    479  1.59      maxv 		if (tp == (IPV6_VERSION >> 4)) {
    480  1.59      maxv 			uint32_t itos32;
    481  1.59      maxv 
    482  1.59      maxv 			/* Save ECN notification. */
    483  1.59      maxv 			m_copydata(m, sizeof(struct ip6_hdr) +
    484  1.59      maxv 			    offsetof(struct ip6_hdr, ip6_flow),
    485  1.59      maxv 			    sizeof(uint32_t), &itos32);
    486  1.59      maxv 			itos = ntohl(itos32) >> 20;
    487   1.1  jonathan 
    488  1.59      maxv 			ip6o->ip6_nxt = IPPROTO_IPV6;
    489  1.59      maxv 		} else {
    490  1.59      maxv 			goto nofamily;
    491  1.59      maxv 		}
    492   1.1  jonathan 
    493   1.1  jonathan 		otos = 0;
    494   1.1  jonathan 		ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
    495  1.64      maxv 		ip6o->ip6_flow |= htonl((uint32_t)otos << 20);
    496   1.1  jonathan 		break;
    497   1.1  jonathan #endif /* INET6 */
    498   1.1  jonathan 
    499   1.1  jonathan 	default:
    500   1.1  jonathan nofamily:
    501  1.45  christos 		DPRINTF(("%s: unsupported protocol family %u\n", __func__,
    502   1.1  jonathan 		    saidx->dst.sa.sa_family));
    503  1.22   thorpej 		IPIP_STATINC(IPIP_STAT_FAMILY);
    504  1.60      maxv 		error = EAFNOSUPPORT;
    505   1.1  jonathan 		goto bad;
    506   1.1  jonathan 	}
    507   1.1  jonathan 
    508  1.22   thorpej 	IPIP_STATINC(IPIP_STAT_OPACKETS);
    509  1.60      maxv 	IPIP_STATADD(IPIP_STAT_OBYTES, m->m_pkthdr.len - iphlen);
    510   1.1  jonathan #if 0
    511  1.60      maxv 	if (sav->tdb_xform->xf_type == XF_IP4)
    512  1.60      maxv 		tdb->tdb_cur_bytes += m->m_pkthdr.len - iphlen;
    513   1.1  jonathan #endif
    514   1.1  jonathan 
    515  1.60      maxv 	*mp = m;
    516   1.1  jonathan 	return 0;
    517  1.59      maxv 
    518   1.1  jonathan bad:
    519   1.1  jonathan 	if (m)
    520   1.8       scw 		m_freem(m);
    521   1.8       scw 	*mp = NULL;
    522  1.59      maxv 	return error;
    523   1.1  jonathan }
    524   1.1  jonathan 
    525   1.1  jonathan static int
    526  1.26  drochner ipe4_init(struct secasvar *sav, const struct xformsw *xsp)
    527   1.1  jonathan {
    528   1.1  jonathan 	sav->tdb_xform = xsp;
    529   1.1  jonathan 	return 0;
    530   1.1  jonathan }
    531   1.1  jonathan 
    532   1.1  jonathan static int
    533   1.1  jonathan ipe4_zeroize(struct secasvar *sav)
    534   1.1  jonathan {
    535   1.1  jonathan 	sav->tdb_xform = NULL;
    536   1.1  jonathan 	return 0;
    537   1.1  jonathan }
    538   1.1  jonathan 
    539   1.1  jonathan static int
    540  1.59      maxv ipe4_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
    541   1.1  jonathan {
    542   1.1  jonathan 	/* This is a rather serious mistake, so no conditional printing. */
    543  1.45  christos 	printf("%s: should never be called\n", __func__);
    544   1.1  jonathan 	if (m)
    545   1.1  jonathan 		m_freem(m);
    546   1.1  jonathan 	return EOPNOTSUPP;
    547   1.1  jonathan }
    548   1.1  jonathan 
    549   1.1  jonathan static struct xformsw ipe4_xformsw = {
    550  1.50     ozaki 	.xf_type	= XF_IP4,
    551  1.50     ozaki 	.xf_flags	= 0,
    552  1.50     ozaki 	.xf_name	= "IPv4 Simple Encapsulation",
    553  1.50     ozaki 	.xf_init	= ipe4_init,
    554  1.50     ozaki 	.xf_zeroize	= ipe4_zeroize,
    555  1.50     ozaki 	.xf_input	= ipe4_input,
    556  1.50     ozaki 	.xf_output	= ipip_output,
    557  1.50     ozaki 	.xf_next	= NULL,
    558   1.1  jonathan };
    559   1.1  jonathan 
    560  1.15  degroote #ifdef INET
    561  1.36  knakahar static struct encapsw ipe4_encapsw = {
    562  1.36  knakahar 	.encapsw4 = {
    563  1.36  knakahar 		.pr_input = ip4_input,
    564  1.36  knakahar 		.pr_ctlinput = NULL,
    565  1.36  knakahar 	}
    566  1.15  degroote };
    567  1.15  degroote #endif
    568   1.1  jonathan #ifdef INET6
    569  1.36  knakahar static struct encapsw ipe4_encapsw6 = {
    570  1.36  knakahar 	.encapsw6 = {
    571  1.36  knakahar 		.pr_input = ip4_input6,
    572  1.36  knakahar 		.pr_ctlinput = NULL,
    573  1.36  knakahar 	}
    574  1.15  degroote };
    575   1.1  jonathan #endif
    576  1.15  degroote 
    577   1.1  jonathan /*
    578   1.1  jonathan  * Check the encapsulated packet to see if we want it
    579   1.1  jonathan  */
    580   1.1  jonathan static int
    581  1.59      maxv ipe4_encapcheck(struct mbuf *m, int off, int proto, void *arg)
    582   1.1  jonathan {
    583   1.1  jonathan 	/*
    584   1.1  jonathan 	 * Only take packets coming from IPSEC tunnels; the rest
    585   1.1  jonathan 	 * must be handled by the gif tunnel code.  Note that we
    586   1.1  jonathan 	 * also return a minimum priority when we want the packet
    587   1.1  jonathan 	 * so any explicit gif tunnels take precedence.
    588   1.1  jonathan 	 */
    589   1.1  jonathan 	return ((m->m_flags & M_IPSEC) != 0 ? 1 : 0);
    590   1.1  jonathan }
    591   1.1  jonathan 
    592  1.48     ozaki void
    593   1.1  jonathan ipe4_attach(void)
    594   1.1  jonathan {
    595  1.22   thorpej 
    596  1.22   thorpej 	ipipstat_percpu = percpu_alloc(sizeof(uint64_t) * IPIP_NSTATS);
    597  1.22   thorpej 
    598   1.1  jonathan 	xform_register(&ipe4_xformsw);
    599   1.1  jonathan 	/* attach to encapsulation framework */
    600   1.1  jonathan 	/* XXX save return cookie for detach on module remove */
    601  1.41  knakahar 
    602  1.41  knakahar 	encapinit();
    603  1.41  knakahar 	/* This function is called before ifinit(). Who else gets lock? */
    604  1.41  knakahar 	(void)encap_lock_enter();
    605  1.40  knakahar 	/* ipe4_encapsw and ipe4_encapsw must be added atomically */
    606  1.15  degroote #ifdef INET
    607  1.59      maxv 	(void)encap_attach_func(AF_INET, -1, ipe4_encapcheck, &ipe4_encapsw,
    608  1.59      maxv 	    NULL);
    609  1.15  degroote #endif
    610   1.1  jonathan #ifdef INET6
    611  1.59      maxv 	(void)encap_attach_func(AF_INET6, -1, ipe4_encapcheck, &ipe4_encapsw6,
    612  1.59      maxv 	    NULL);
    613   1.1  jonathan #endif
    614  1.40  knakahar 	encap_lock_exit();
    615   1.1  jonathan }
    616