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