Home | History | Annotate | Line # | Download | only in netipsec
ipsec_output.c revision 1.44
      1  1.44     ozaki /*	$NetBSD: ipsec_output.c,v 1.44 2017/04/18 05:26:42 ozaki-r Exp $	*/
      2   1.9   thorpej 
      3   1.9   thorpej /*-
      4   1.9   thorpej  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
      5   1.9   thorpej  * All rights reserved.
      6   1.9   thorpej  *
      7   1.9   thorpej  * Redistribution and use in source and binary forms, with or without
      8   1.9   thorpej  * modification, are permitted provided that the following conditions
      9   1.9   thorpej  * are met:
     10   1.9   thorpej  * 1. Redistributions of source code must retain the above copyright
     11   1.9   thorpej  *    notice, this list of conditions and the following disclaimer.
     12   1.9   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.9   thorpej  *    notice, this list of conditions and the following disclaimer in the
     14   1.9   thorpej  *    documentation and/or other materials provided with the distribution.
     15   1.9   thorpej  *
     16   1.9   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17   1.9   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18   1.9   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19   1.9   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20   1.9   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21   1.9   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22   1.9   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23   1.9   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24   1.9   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.9   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.9   thorpej  * SUCH DAMAGE.
     27   1.9   thorpej  *
     28   1.9   thorpej  * $FreeBSD: /repoman/r/ncvs/src/sys/netipsec/ipsec_output.c,v 1.3.2.2 2003/03/28 20:32:53 sam Exp $
     29   1.9   thorpej  */
     30   1.1  jonathan 
     31   1.1  jonathan #include <sys/cdefs.h>
     32  1.44     ozaki __KERNEL_RCSID(0, "$NetBSD: ipsec_output.c,v 1.44 2017/04/18 05:26:42 ozaki-r Exp $");
     33   1.1  jonathan 
     34   1.1  jonathan /*
     35   1.1  jonathan  * IPsec output processing.
     36   1.1  jonathan  */
     37  1.42     ozaki #if defined(_KERNEL_OPT)
     38   1.1  jonathan #include "opt_inet.h"
     39  1.42     ozaki #endif
     40   1.1  jonathan 
     41   1.1  jonathan #include <sys/param.h>
     42   1.1  jonathan #include <sys/systm.h>
     43   1.1  jonathan #include <sys/mbuf.h>
     44   1.1  jonathan #include <sys/domain.h>
     45   1.1  jonathan #include <sys/protosw.h>
     46   1.1  jonathan #include <sys/socket.h>
     47   1.1  jonathan #include <sys/errno.h>
     48   1.1  jonathan #include <sys/syslog.h>
     49   1.1  jonathan 
     50   1.1  jonathan #include <net/if.h>
     51   1.1  jonathan #include <net/route.h>
     52   1.1  jonathan 
     53   1.1  jonathan #include <netinet/in.h>
     54   1.1  jonathan #include <netinet/in_systm.h>
     55   1.1  jonathan #include <netinet/ip.h>
     56   1.1  jonathan #include <netinet/ip_var.h>
     57   1.1  jonathan #include <netinet/in_var.h>
     58   1.1  jonathan #include <netinet/ip_ecn.h>
     59   1.1  jonathan 
     60   1.1  jonathan #include <netinet/ip6.h>
     61   1.1  jonathan #ifdef INET6
     62   1.1  jonathan #include <netinet6/ip6_var.h>
     63   1.1  jonathan #endif
     64   1.1  jonathan #include <netinet/in_pcb.h>
     65   1.1  jonathan #ifdef INET6
     66   1.1  jonathan #include <netinet/icmp6.h>
     67   1.1  jonathan #endif
     68  1.22  degroote #include <netinet/udp.h>
     69   1.1  jonathan 
     70   1.1  jonathan #include <netipsec/ipsec.h>
     71  1.13  jonathan #include <netipsec/ipsec_var.h>
     72  1.27   thorpej #include <netipsec/ipsec_private.h>
     73   1.1  jonathan #ifdef INET6
     74   1.1  jonathan #include <netipsec/ipsec6.h>
     75   1.1  jonathan #endif
     76   1.1  jonathan #include <netipsec/ah_var.h>
     77   1.1  jonathan #include <netipsec/esp_var.h>
     78   1.1  jonathan #include <netipsec/ipcomp_var.h>
     79   1.1  jonathan 
     80   1.1  jonathan #include <netipsec/xform.h>
     81   1.1  jonathan 
     82   1.7       tls #include <netipsec/key.h>
     83   1.7       tls #include <netipsec/keydb.h>
     84   1.7       tls #include <netipsec/key_debug.h>
     85   1.1  jonathan #include <netipsec/ipsec_osdep.h>
     86   1.1  jonathan 
     87  1.10  jonathan #include <net/net_osdep.h>		/* ovbcopy() in ipsec6_encapsulate() */
     88  1.10  jonathan 
     89  1.25  degroote 
     90  1.25  degroote /*
     91  1.25  degroote  * Add a IPSEC_OUT_DONE tag to mark that we have finished the ipsec processing
     92  1.25  degroote  * It will be used by ip{,6}_output to check if we have already or not
     93  1.25  degroote  * processed this packet.
     94  1.25  degroote  */
     95  1.25  degroote static int
     96  1.25  degroote ipsec_register_done(struct mbuf *m, int * error)
     97  1.25  degroote {
     98  1.25  degroote 	struct m_tag *mtag;
     99  1.25  degroote 
    100  1.25  degroote 	mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, 0, M_NOWAIT);
    101  1.25  degroote 	if (mtag == NULL) {
    102  1.25  degroote 		DPRINTF(("ipsec_register_done: could not get packet tag\n"));
    103  1.25  degroote 		*error = ENOMEM;
    104  1.25  degroote 		return -1;
    105  1.25  degroote 	}
    106  1.25  degroote 
    107  1.25  degroote 	m_tag_prepend(m, mtag);
    108  1.25  degroote 	return 0;
    109  1.25  degroote }
    110  1.25  degroote 
    111  1.26  degroote static int
    112  1.26  degroote ipsec_reinject_ipstack(struct mbuf *m, int af)
    113  1.26  degroote {
    114  1.30  drochner #if defined(INET) || defined(INET6)
    115  1.30  drochner 	int rv;
    116  1.30  drochner #endif
    117  1.26  degroote 
    118  1.26  degroote 	switch (af) {
    119  1.26  degroote #ifdef INET
    120  1.26  degroote 	case AF_INET:
    121  1.30  drochner 		KERNEL_LOCK(1, NULL);
    122  1.31  drochner 		rv = ip_output(m, NULL, NULL, IP_RAWOUTPUT|IP_NOIPNEWID,
    123  1.37    plunky 		    NULL, NULL);
    124  1.30  drochner 		KERNEL_UNLOCK_ONE(NULL);
    125  1.30  drochner 		return rv;
    126  1.26  degroote 
    127  1.26  degroote #endif /* INET */
    128  1.26  degroote #ifdef INET6
    129  1.26  degroote 	case AF_INET6:
    130  1.26  degroote 		/*
    131  1.26  degroote 		 * We don't need massage, IPv6 header fields are always in
    132  1.26  degroote 		 * net endian.
    133  1.26  degroote 		 */
    134  1.30  drochner 		KERNEL_LOCK(1, NULL);
    135  1.30  drochner 		rv = ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
    136  1.30  drochner 		KERNEL_UNLOCK_ONE(NULL);
    137  1.30  drochner 		return rv;
    138  1.26  degroote #endif /* INET6 */
    139  1.26  degroote 	}
    140  1.26  degroote 
    141  1.26  degroote 	panic("ipsec_reinject_ipstack : iunknown protocol family %u\n", af);
    142  1.26  degroote 	return -1; /* NOTREACHED */
    143  1.26  degroote }
    144  1.26  degroote 
    145   1.1  jonathan int
    146   1.1  jonathan ipsec_process_done(struct mbuf *m, struct ipsecrequest *isr)
    147   1.1  jonathan {
    148   1.1  jonathan 	struct secasvar *sav;
    149   1.1  jonathan 	struct secasindex *saidx;
    150   1.1  jonathan 	int error;
    151  1.22  degroote #ifdef INET
    152  1.22  degroote 	struct ip * ip;
    153  1.22  degroote #endif /* INET */
    154  1.22  degroote #ifdef INET6
    155  1.22  degroote 	struct ip6_hdr * ip6;
    156  1.22  degroote #endif /* INET6 */
    157  1.22  degroote 	struct mbuf * mo;
    158  1.22  degroote 	struct udphdr *udp = NULL;
    159  1.22  degroote 	uint64_t * data = NULL;
    160  1.22  degroote 	int hlen, roff;
    161   1.1  jonathan 
    162   1.1  jonathan 	IPSEC_SPLASSERT_SOFTNET("ipsec_process_done");
    163   1.1  jonathan 
    164  1.44     ozaki 	KASSERT(m != NULL);
    165  1.44     ozaki 	KASSERT(isr != NULL);
    166   1.1  jonathan 	sav = isr->sav;
    167  1.44     ozaki 	KASSERT(sav != NULL);
    168  1.44     ozaki 	KASSERT(sav->sah != NULL);
    169   1.1  jonathan 
    170   1.1  jonathan 	saidx = &sav->sah->saidx;
    171  1.22  degroote 
    172  1.22  degroote 	if(sav->natt_type != 0) {
    173  1.22  degroote 		ip = mtod(m, struct ip *);
    174  1.22  degroote 
    175  1.22  degroote 		hlen = sizeof(struct udphdr);
    176  1.22  degroote 		if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
    177  1.22  degroote 			hlen += sizeof(uint64_t);
    178  1.22  degroote 
    179  1.22  degroote 		mo = m_makespace(m, sizeof(struct ip), hlen, &roff);
    180  1.22  degroote 		if (mo == NULL) {
    181  1.22  degroote 			DPRINTF(("ipsec_process_done : failed to inject"
    182  1.22  degroote 				 "%u byte UDP for SA %s/%08lx\n",
    183  1.22  degroote 					 hlen, ipsec_address(&saidx->dst),
    184  1.22  degroote 					 (u_long) ntohl(sav->spi)));
    185  1.22  degroote 			error = ENOBUFS;
    186  1.22  degroote 			goto bad;
    187  1.22  degroote 		}
    188  1.22  degroote 
    189  1.22  degroote 		udp = (struct udphdr*) (mtod(mo, char*) + roff);
    190  1.22  degroote 		data = (uint64_t*) (udp + 1);
    191  1.22  degroote 
    192  1.22  degroote 		if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
    193  1.22  degroote 			*data = 0; /* NON-IKE Marker */
    194  1.22  degroote 
    195  1.22  degroote 		if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
    196  1.22  degroote 			udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT);
    197  1.22  degroote 		else
    198  1.22  degroote 			udp->uh_sport = key_portfromsaddr(&saidx->src);
    199  1.22  degroote 
    200  1.22  degroote 		udp->uh_dport = key_portfromsaddr(&saidx->dst);
    201  1.22  degroote 		udp->uh_sum = 0;
    202  1.29    dyoung 		udp->uh_ulen = htons(m->m_pkthdr.len - (ip->ip_hl << 2));
    203  1.22  degroote 	}
    204  1.22  degroote 
    205   1.1  jonathan 	switch (saidx->dst.sa.sa_family) {
    206   1.1  jonathan #ifdef INET
    207   1.1  jonathan 	case AF_INET:
    208   1.1  jonathan 		/* Fix the header length, for AH processing. */
    209  1.22  degroote 		ip = mtod(m, struct ip *);
    210  1.22  degroote 		ip->ip_len = htons(m->m_pkthdr.len);
    211  1.22  degroote 		if (sav->natt_type != 0)
    212  1.22  degroote 			ip->ip_p = IPPROTO_UDP;
    213   1.1  jonathan 		break;
    214   1.1  jonathan #endif /* INET */
    215   1.1  jonathan #ifdef INET6
    216   1.1  jonathan 	case AF_INET6:
    217   1.1  jonathan 		/* Fix the header length, for AH processing. */
    218   1.1  jonathan 		if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
    219   1.1  jonathan 			error = ENXIO;
    220   1.1  jonathan 			goto bad;
    221   1.1  jonathan 		}
    222   1.1  jonathan 		if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
    223   1.1  jonathan 			/* No jumbogram support. */
    224   1.1  jonathan 			error = ENXIO;	/*?*/
    225   1.1  jonathan 			goto bad;
    226   1.1  jonathan 		}
    227  1.22  degroote 		ip6 = mtod(m, struct ip6_hdr *);
    228  1.22  degroote 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
    229  1.22  degroote 		if (sav->natt_type != 0)
    230  1.22  degroote 			ip6->ip6_nxt = IPPROTO_UDP;
    231   1.1  jonathan 		break;
    232   1.1  jonathan #endif /* INET6 */
    233   1.1  jonathan 	default:
    234   1.1  jonathan 		DPRINTF(("ipsec_process_done: unknown protocol family %u\n",
    235   1.1  jonathan 		    saidx->dst.sa.sa_family));
    236   1.1  jonathan 		error = ENXIO;
    237   1.1  jonathan 		goto bad;
    238   1.1  jonathan 	}
    239   1.1  jonathan 
    240  1.32  drochner 	key_sa_recordxfer(sav, m);
    241  1.32  drochner 
    242   1.1  jonathan 	/*
    243   1.1  jonathan 	 * If there's another (bundled) SA to apply, do so.
    244   1.1  jonathan 	 * Note that this puts a burden on the kernel stack size.
    245   1.1  jonathan 	 * If this is a problem we'll need to introduce a queue
    246   1.1  jonathan 	 * to set the packet on so we can unwind the stack before
    247   1.1  jonathan 	 * doing further processing.
    248   1.1  jonathan 	 */
    249   1.1  jonathan 	if (isr->next) {
    250  1.27   thorpej 		IPSEC_STATINC(IPSEC_STAT_OUT_BUNDLESA);
    251  1.29    dyoung 		switch ( saidx->dst.sa.sa_family ) {
    252  1.21  degroote #ifdef INET
    253  1.29    dyoung 		case AF_INET:
    254  1.21  degroote 			return ipsec4_process_packet(m, isr->next, 0,0);
    255  1.21  degroote #endif /* INET */
    256  1.21  degroote #ifdef INET6
    257  1.21  degroote 		case AF_INET6:
    258  1.29    dyoung 			return ipsec6_process_packet(m,isr->next);
    259  1.21  degroote #endif /* INET6 */
    260  1.21  degroote 		default :
    261  1.21  degroote 			DPRINTF(("ipsec_process_done: unknown protocol family %u\n",
    262  1.29    dyoung 			       saidx->dst.sa.sa_family));
    263  1.21  degroote 			error = ENXIO;
    264  1.21  degroote 			goto bad;
    265  1.29    dyoung 		}
    266   1.1  jonathan 	}
    267   1.1  jonathan 
    268   1.1  jonathan 	/*
    269  1.25  degroote 	 * We're done with IPsec processing,
    270  1.25  degroote 	 * mark that we have already processed the packet
    271  1.25  degroote 	 * transmit it packet using the appropriate network protocol (IP or IPv6).
    272   1.1  jonathan 	 */
    273  1.25  degroote 
    274  1.25  degroote 	if (ipsec_register_done(m, &error) < 0)
    275  1.25  degroote 		goto bad;
    276  1.25  degroote 
    277  1.26  degroote 	return ipsec_reinject_ipstack(m, saidx->dst.sa.sa_family);
    278   1.1  jonathan bad:
    279   1.1  jonathan 	m_freem(m);
    280   1.1  jonathan 	KEY_FREESAV(&sav);
    281   1.1  jonathan 	return (error);
    282   1.1  jonathan }
    283   1.1  jonathan 
    284  1.26  degroote /*
    285  1.26  degroote  * ipsec_nextisr can return :
    286  1.26  degroote  * - isr == NULL and error != 0 => something is bad : the packet must be
    287  1.26  degroote  *   discarded
    288  1.26  degroote  * - isr == NULL and error == 0 => no more rules to apply, ipsec processing
    289  1.26  degroote  *   is done, reinject it in ip stack
    290  1.26  degroote  * - isr != NULL (error == 0) => we need to apply one rule to the packet
    291  1.26  degroote  */
    292   1.1  jonathan static struct ipsecrequest *
    293   1.1  jonathan ipsec_nextisr(
    294   1.1  jonathan 	struct mbuf *m,
    295   1.1  jonathan 	struct ipsecrequest *isr,
    296   1.1  jonathan 	int af,
    297   1.1  jonathan 	struct secasindex *saidx,
    298   1.1  jonathan 	int *error
    299   1.1  jonathan )
    300   1.1  jonathan {
    301  1.27   thorpej #define	IPSEC_OSTAT(x, y, z)						\
    302  1.27   thorpej do {									\
    303  1.27   thorpej 	switch (isr->saidx.proto) {					\
    304  1.27   thorpej 	case IPPROTO_ESP:						\
    305  1.27   thorpej 		ESP_STATINC(x);						\
    306  1.27   thorpej 		break;							\
    307  1.27   thorpej 	case IPPROTO_AH:						\
    308  1.27   thorpej 		AH_STATINC(y);						\
    309  1.27   thorpej 		break;							\
    310  1.27   thorpej 	default:							\
    311  1.27   thorpej 		IPCOMP_STATINC(z);					\
    312  1.27   thorpej 		break;							\
    313  1.27   thorpej 	}								\
    314  1.27   thorpej } while (/*CONSTCOND*/0)
    315  1.27   thorpej 
    316   1.1  jonathan 	struct secasvar *sav;
    317   1.1  jonathan 
    318   1.1  jonathan 	IPSEC_SPLASSERT_SOFTNET("ipsec_nextisr");
    319  1.44     ozaki 	KASSERTMSG(af == AF_INET || af == AF_INET6,
    320  1.44     ozaki 	    "invalid address family %u", af);
    321   1.1  jonathan again:
    322   1.1  jonathan 	/*
    323   1.1  jonathan 	 * Craft SA index to search for proper SA.  Note that
    324   1.1  jonathan 	 * we only fillin unspecified SA peers for transport
    325   1.1  jonathan 	 * mode; for tunnel mode they must already be filled in.
    326   1.1  jonathan 	 */
    327   1.1  jonathan 	*saidx = isr->saidx;
    328   1.1  jonathan 	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
    329   1.1  jonathan 		/* Fillin unspecified SA peers only for transport mode */
    330   1.1  jonathan 		if (af == AF_INET) {
    331   1.1  jonathan 			struct sockaddr_in *sin;
    332   1.1  jonathan 			struct ip *ip = mtod(m, struct ip *);
    333   1.1  jonathan 
    334   1.1  jonathan 			if (saidx->src.sa.sa_len == 0) {
    335   1.1  jonathan 				sin = &saidx->src.sin;
    336   1.1  jonathan 				sin->sin_len = sizeof(*sin);
    337   1.1  jonathan 				sin->sin_family = AF_INET;
    338   1.1  jonathan 				sin->sin_port = IPSEC_PORT_ANY;
    339   1.1  jonathan 				sin->sin_addr = ip->ip_src;
    340   1.1  jonathan 			}
    341   1.1  jonathan 			if (saidx->dst.sa.sa_len == 0) {
    342   1.1  jonathan 				sin = &saidx->dst.sin;
    343   1.1  jonathan 				sin->sin_len = sizeof(*sin);
    344   1.1  jonathan 				sin->sin_family = AF_INET;
    345   1.1  jonathan 				sin->sin_port = IPSEC_PORT_ANY;
    346   1.1  jonathan 				sin->sin_addr = ip->ip_dst;
    347   1.1  jonathan 			}
    348   1.1  jonathan 		} else {
    349   1.1  jonathan 			struct sockaddr_in6 *sin6;
    350   1.1  jonathan 			struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    351   1.1  jonathan 
    352   1.1  jonathan 			if (saidx->src.sin6.sin6_len == 0) {
    353   1.1  jonathan 				sin6 = (struct sockaddr_in6 *)&saidx->src;
    354   1.1  jonathan 				sin6->sin6_len = sizeof(*sin6);
    355   1.1  jonathan 				sin6->sin6_family = AF_INET6;
    356   1.1  jonathan 				sin6->sin6_port = IPSEC_PORT_ANY;
    357   1.1  jonathan 				sin6->sin6_addr = ip6->ip6_src;
    358   1.1  jonathan 				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
    359   1.1  jonathan 					/* fix scope id for comparing SPD */
    360   1.1  jonathan 					sin6->sin6_addr.s6_addr16[1] = 0;
    361   1.1  jonathan 					sin6->sin6_scope_id =
    362   1.1  jonathan 					    ntohs(ip6->ip6_src.s6_addr16[1]);
    363   1.1  jonathan 				}
    364   1.1  jonathan 			}
    365   1.1  jonathan 			if (saidx->dst.sin6.sin6_len == 0) {
    366   1.1  jonathan 				sin6 = (struct sockaddr_in6 *)&saidx->dst;
    367   1.1  jonathan 				sin6->sin6_len = sizeof(*sin6);
    368   1.1  jonathan 				sin6->sin6_family = AF_INET6;
    369   1.1  jonathan 				sin6->sin6_port = IPSEC_PORT_ANY;
    370   1.1  jonathan 				sin6->sin6_addr = ip6->ip6_dst;
    371   1.1  jonathan 				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
    372   1.1  jonathan 					/* fix scope id for comparing SPD */
    373   1.1  jonathan 					sin6->sin6_addr.s6_addr16[1] = 0;
    374   1.1  jonathan 					sin6->sin6_scope_id =
    375   1.1  jonathan 					    ntohs(ip6->ip6_dst.s6_addr16[1]);
    376   1.1  jonathan 				}
    377   1.1  jonathan 			}
    378   1.1  jonathan 		}
    379   1.1  jonathan 	}
    380   1.1  jonathan 
    381   1.1  jonathan 	/*
    382   1.1  jonathan 	 * Lookup SA and validate it.
    383   1.1  jonathan 	 */
    384   1.1  jonathan 	*error = key_checkrequest(isr, saidx);
    385   1.1  jonathan 	if (*error != 0) {
    386   1.1  jonathan 		/*
    387   1.1  jonathan 		 * IPsec processing is required, but no SA found.
    388   1.1  jonathan 		 * I assume that key_acquire() had been called
    389   1.1  jonathan 		 * to get/establish the SA. Here I discard
    390   1.1  jonathan 		 * this packet because it is responsibility for
    391   1.1  jonathan 		 * upper layer to retransmit the packet.
    392   1.1  jonathan 		 */
    393  1.27   thorpej 		IPSEC_STATINC(IPSEC_STAT_OUT_NOSA);
    394   1.1  jonathan 		goto bad;
    395   1.1  jonathan 	}
    396   1.1  jonathan 	sav = isr->sav;
    397  1.26  degroote 	/* sav may be NULL here if we have an USE rule */
    398  1.26  degroote 	if (sav == NULL) {
    399  1.44     ozaki 		KASSERTMSG(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
    400  1.44     ozaki 		    "no SA found, but required; level %u",
    401  1.44     ozaki 		    ipsec_get_reqlevel(isr));
    402   1.1  jonathan 		isr = isr->next;
    403  1.26  degroote 		/*
    404  1.26  degroote 		 * No more rules to apply, return NULL isr and no error
    405  1.26  degroote 		 * It can happen when the last rules are USE rules
    406  1.26  degroote 		 * */
    407   1.1  jonathan 		if (isr == NULL) {
    408  1.26  degroote 			*error = 0;
    409   1.1  jonathan 			return isr;
    410   1.1  jonathan 		}
    411   1.1  jonathan 		goto again;
    412   1.1  jonathan 	}
    413   1.1  jonathan 
    414   1.1  jonathan 	/*
    415   1.1  jonathan 	 * Check system global policy controls.
    416   1.1  jonathan 	 */
    417   1.1  jonathan 	if ((isr->saidx.proto == IPPROTO_ESP && !esp_enable) ||
    418   1.1  jonathan 	    (isr->saidx.proto == IPPROTO_AH && !ah_enable) ||
    419   1.1  jonathan 	    (isr->saidx.proto == IPPROTO_IPCOMP && !ipcomp_enable)) {
    420   1.1  jonathan 		DPRINTF(("ipsec_nextisr: IPsec outbound packet dropped due"
    421   1.1  jonathan 			" to policy (check your sysctls)\n"));
    422  1.27   thorpej 		IPSEC_OSTAT(ESP_STAT_PDROPS, AH_STAT_PDROPS,
    423  1.27   thorpej 		    IPCOMP_STAT_PDROPS);
    424   1.1  jonathan 		*error = EHOSTUNREACH;
    425   1.1  jonathan 		goto bad;
    426   1.1  jonathan 	}
    427   1.1  jonathan 
    428   1.1  jonathan 	/*
    429   1.1  jonathan 	 * Sanity check the SA contents for the caller
    430   1.1  jonathan 	 * before they invoke the xform output method.
    431   1.1  jonathan 	 */
    432   1.1  jonathan 	if (sav->tdb_xform == NULL) {
    433   1.1  jonathan 		DPRINTF(("ipsec_nextisr: no transform for SA\n"));
    434  1.27   thorpej 		IPSEC_OSTAT(ESP_STAT_NOXFORM, AH_STAT_NOXFORM,
    435  1.27   thorpej 		    IPCOMP_STAT_NOXFORM);
    436   1.1  jonathan 		*error = EHOSTUNREACH;
    437   1.1  jonathan 		goto bad;
    438   1.1  jonathan 	}
    439   1.1  jonathan 	return isr;
    440   1.1  jonathan bad:
    441  1.44     ozaki 	KASSERTMSG(*error != 0, "error return w/ no error code");
    442   1.1  jonathan 	return NULL;
    443   1.1  jonathan #undef IPSEC_OSTAT
    444   1.1  jonathan }
    445   1.1  jonathan 
    446   1.1  jonathan #ifdef INET
    447   1.1  jonathan /*
    448   1.1  jonathan  * IPsec output logic for IPv4.
    449   1.1  jonathan  */
    450   1.1  jonathan int
    451   1.1  jonathan ipsec4_process_packet(
    452  1.15  christos     struct mbuf *m,
    453  1.15  christos     struct ipsecrequest *isr,
    454  1.16  christos     int flags,
    455  1.15  christos     int tunalready
    456  1.15  christos )
    457   1.1  jonathan {
    458   1.1  jonathan 	struct secasindex saidx;
    459   1.1  jonathan 	struct secasvar *sav;
    460   1.1  jonathan 	struct ip *ip;
    461   1.1  jonathan 	int s, error, i, off;
    462   1.1  jonathan 
    463  1.44     ozaki 	KASSERT(m != NULL);
    464  1.44     ozaki 	KASSERT(isr != NULL);
    465   1.1  jonathan 
    466   1.1  jonathan 	s = splsoftnet();			/* insure SA contents don't change */
    467   1.1  jonathan 
    468   1.1  jonathan 	isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
    469  1.26  degroote 	if (isr == NULL) {
    470  1.26  degroote 		if (error != 0) {
    471  1.26  degroote 			goto bad;
    472  1.26  degroote 		} else {
    473  1.26  degroote 			if (ipsec_register_done(m, &error) < 0)
    474  1.26  degroote 				goto bad;
    475  1.26  degroote 
    476  1.26  degroote 			splx(s);
    477  1.26  degroote 			return ipsec_reinject_ipstack(m, AF_INET);
    478  1.26  degroote 		}
    479  1.26  degroote 	}
    480   1.1  jonathan 
    481   1.1  jonathan 	sav = isr->sav;
    482   1.1  jonathan 	if (!tunalready) {
    483   1.1  jonathan 		union sockaddr_union *dst = &sav->sah->saidx.dst;
    484   1.1  jonathan 		int setdf;
    485   1.1  jonathan 
    486   1.1  jonathan 		/*
    487   1.1  jonathan 		 * Collect IP_DF state from the outer header.
    488   1.1  jonathan 		 */
    489   1.1  jonathan 		if (dst->sa.sa_family == AF_INET) {
    490   1.1  jonathan 			if (m->m_len < sizeof (struct ip) &&
    491   1.1  jonathan 			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
    492   1.1  jonathan 				error = ENOBUFS;
    493   1.1  jonathan 				goto bad;
    494   1.1  jonathan 			}
    495   1.1  jonathan 			ip = mtod(m, struct ip *);
    496   1.1  jonathan 			/* Honor system-wide control of how to handle IP_DF */
    497   1.1  jonathan 			switch (ip4_ipsec_dfbit) {
    498   1.1  jonathan 			case 0:			/* clear in outer header */
    499   1.1  jonathan 			case 1:			/* set in outer header */
    500   1.1  jonathan 				setdf = ip4_ipsec_dfbit;
    501   1.1  jonathan 				break;
    502   1.1  jonathan 			default:		/* propagate to outer header */
    503   1.3  jonathan 				setdf = ip->ip_off;
    504   1.3  jonathan 				setdf = ntohs(setdf);
    505   1.3  jonathan 				setdf = htons(setdf & IP_DF);
    506   1.1  jonathan 				break;
    507   1.1  jonathan 			}
    508   1.1  jonathan 		} else {
    509   1.1  jonathan 			ip = NULL;		/* keep compiler happy */
    510   1.1  jonathan 			setdf = 0;
    511   1.1  jonathan 		}
    512   1.1  jonathan 		/* Do the appropriate encapsulation, if necessary */
    513   1.1  jonathan 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
    514   1.1  jonathan 		    dst->sa.sa_family != AF_INET ||	    /* PF mismatch */
    515   1.1  jonathan #if 0
    516   1.1  jonathan 		    (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
    517   1.1  jonathan 		    sav->tdb_xform->xf_type == XF_IP4 ||    /* ditto */
    518   1.1  jonathan #endif
    519   1.1  jonathan 		    (dst->sa.sa_family == AF_INET &&	    /* Proxy */
    520   1.1  jonathan 		     dst->sin.sin_addr.s_addr != INADDR_ANY &&
    521   1.1  jonathan 		     dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
    522   1.1  jonathan 			struct mbuf *mp;
    523   1.1  jonathan 
    524   1.1  jonathan 			/* Fix IPv4 header checksum and length */
    525   1.1  jonathan 			if (m->m_len < sizeof (struct ip) &&
    526   1.1  jonathan 			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
    527   1.1  jonathan 				error = ENOBUFS;
    528   1.1  jonathan 				goto bad;
    529   1.1  jonathan 			}
    530   1.1  jonathan 			ip = mtod(m, struct ip *);
    531   1.1  jonathan 			ip->ip_len = htons(m->m_pkthdr.len);
    532   1.1  jonathan 			ip->ip_sum = 0;
    533   1.1  jonathan 			ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
    534   1.1  jonathan 
    535   1.1  jonathan 			/* Encapsulate the packet */
    536   1.1  jonathan 			error = ipip_output(m, isr, &mp, 0, 0);
    537   1.1  jonathan 			if (mp == NULL && !error) {
    538   1.1  jonathan 				/* Should never happen. */
    539   1.1  jonathan 				DPRINTF(("ipsec4_process_packet: ipip_output "
    540   1.1  jonathan 					"returns no mbuf and no error!"));
    541   1.1  jonathan 				error = EFAULT;
    542   1.1  jonathan 			}
    543   1.1  jonathan 			if (error) {
    544   1.8       scw 				if (mp) {
    545   1.8       scw 					/* XXX: Should never happen! */
    546   1.1  jonathan 					m_freem(mp);
    547   1.8       scw 				}
    548   1.8       scw 				m = NULL; /* ipip_output() already freed it */
    549   1.1  jonathan 				goto bad;
    550   1.1  jonathan 			}
    551   1.1  jonathan 			m = mp, mp = NULL;
    552   1.1  jonathan 			/*
    553   1.1  jonathan 			 * ipip_output clears IP_DF in the new header.  If
    554   1.1  jonathan 			 * we need to propagate IP_DF from the outer header,
    555   1.1  jonathan 			 * then we have to do it here.
    556   1.1  jonathan 			 *
    557   1.1  jonathan 			 * XXX shouldn't assume what ipip_output does.
    558   1.1  jonathan 			 */
    559   1.1  jonathan 			if (dst->sa.sa_family == AF_INET && setdf) {
    560   1.1  jonathan 				if (m->m_len < sizeof (struct ip) &&
    561   1.1  jonathan 				    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
    562   1.1  jonathan 					error = ENOBUFS;
    563   1.1  jonathan 					goto bad;
    564   1.1  jonathan 				}
    565   1.1  jonathan 				ip = mtod(m, struct ip *);
    566  1.23   adrianp 				ip->ip_off |= IP_OFF_CONVERT(IP_DF);
    567   1.1  jonathan 			}
    568   1.1  jonathan 		}
    569   1.1  jonathan 	}
    570   1.1  jonathan 
    571   1.1  jonathan 	/*
    572   1.1  jonathan 	 * Dispatch to the appropriate IPsec transform logic.  The
    573   1.1  jonathan 	 * packet will be returned for transmission after crypto
    574   1.1  jonathan 	 * processing, etc. are completed.  For encapsulation we
    575   1.1  jonathan 	 * bypass this call because of the explicit call done above
    576   1.1  jonathan 	 * (necessary to deal with IP_DF handling for IPv4).
    577   1.1  jonathan 	 *
    578   1.1  jonathan 	 * NB: m & sav are ``passed to caller'' who's reponsible for
    579   1.1  jonathan 	 *     for reclaiming their resources.
    580   1.1  jonathan 	 */
    581   1.1  jonathan 	if (sav->tdb_xform->xf_type != XF_IP4) {
    582  1.33  drochner 		union sockaddr_union *dst = &sav->sah->saidx.dst;
    583  1.33  drochner 		if (dst->sa.sa_family == AF_INET) {
    584  1.33  drochner 			ip = mtod(m, struct ip *);
    585  1.33  drochner 			i = ip->ip_hl << 2;
    586  1.33  drochner 			off = offsetof(struct ip, ip_p);
    587  1.33  drochner 		} else {
    588  1.33  drochner 			i = sizeof(struct ip6_hdr);
    589  1.33  drochner 			off = offsetof(struct ip6_hdr, ip6_nxt);
    590  1.33  drochner 		}
    591   1.1  jonathan 		error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
    592   1.1  jonathan 	} else {
    593   1.1  jonathan 		error = ipsec_process_done(m, isr);
    594   1.1  jonathan 	}
    595   1.1  jonathan 	splx(s);
    596   1.1  jonathan 	return error;
    597   1.1  jonathan bad:
    598   1.1  jonathan 	splx(s);
    599   1.1  jonathan 	if (m)
    600   1.1  jonathan 		m_freem(m);
    601   1.1  jonathan 	return error;
    602   1.1  jonathan }
    603   1.1  jonathan #endif
    604   1.1  jonathan 
    605   1.1  jonathan #ifdef INET6
    606  1.38  drochner static void
    607  1.38  drochner compute_ipsec_pos(struct mbuf *m, int *i, int *off)
    608  1.38  drochner {
    609  1.38  drochner 	int nxt;
    610  1.38  drochner 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr*);
    611  1.38  drochner 	struct ip6_ext ip6e;
    612  1.38  drochner 	int dstopt = 0;
    613  1.38  drochner 
    614  1.38  drochner 	*i = sizeof(struct ip6_hdr);
    615  1.38  drochner 	*off = offsetof(struct ip6_hdr, ip6_nxt);
    616  1.38  drochner 	nxt = ip6->ip6_nxt;
    617  1.38  drochner 
    618  1.38  drochner 	/*
    619  1.38  drochner 	 * chase mbuf chain to find the appropriate place to
    620  1.38  drochner 	 * put AH/ESP/IPcomp header.
    621  1.38  drochner 	 *  IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
    622  1.38  drochner 	 */
    623  1.38  drochner 	do {
    624  1.38  drochner 		switch (nxt) {
    625  1.38  drochner 		case IPPROTO_AH:
    626  1.38  drochner 		case IPPROTO_ESP:
    627  1.38  drochner 		case IPPROTO_IPCOMP:
    628  1.38  drochner 		/*
    629  1.38  drochner 		 * we should not skip security header added
    630  1.38  drochner 		 * beforehand.
    631  1.38  drochner 		 */
    632  1.38  drochner 			return;
    633  1.38  drochner 
    634  1.38  drochner 		case IPPROTO_HOPOPTS:
    635  1.38  drochner 		case IPPROTO_DSTOPTS:
    636  1.38  drochner 		case IPPROTO_ROUTING:
    637  1.38  drochner 		/*
    638  1.38  drochner 		 * if we see 2nd destination option header,
    639  1.38  drochner 		 * we should stop there.
    640  1.38  drochner 		 */
    641  1.38  drochner 			if (nxt == IPPROTO_DSTOPTS && dstopt)
    642  1.38  drochner 				return;
    643  1.38  drochner 
    644  1.38  drochner 			if (nxt == IPPROTO_DSTOPTS) {
    645  1.38  drochner 				/*
    646  1.38  drochner 				 * seen 1st or 2nd destination option.
    647  1.38  drochner 				 * next time we see one, it must be 2nd.
    648  1.38  drochner 				 */
    649  1.38  drochner 				dstopt = 1;
    650  1.38  drochner 			} else if (nxt == IPPROTO_ROUTING) {
    651  1.38  drochner 				/*
    652  1.38  drochner 				 * if we see destionation option next
    653  1.38  drochner 				 * time, it must be dest2.
    654  1.38  drochner 				 */
    655  1.38  drochner 				dstopt = 2;
    656  1.38  drochner 			}
    657  1.38  drochner 
    658  1.38  drochner 			/* skip this header */
    659  1.38  drochner 			m_copydata(m, *i, sizeof(ip6e), &ip6e);
    660  1.38  drochner 			nxt = ip6e.ip6e_nxt;
    661  1.38  drochner 			*off = *i + offsetof(struct ip6_ext, ip6e_nxt);
    662  1.38  drochner 			/*
    663  1.38  drochner 			 * we will never see nxt == IPPROTO_AH
    664  1.38  drochner 			 * so it is safe to omit AH case.
    665  1.38  drochner 			 */
    666  1.38  drochner 			*i += (ip6e.ip6e_len + 1) << 3;
    667  1.38  drochner 			break;
    668  1.38  drochner 		default:
    669  1.38  drochner 			return;
    670  1.38  drochner 		}
    671  1.38  drochner 	} while (*i < m->m_pkthdr.len);
    672  1.38  drochner }
    673  1.38  drochner 
    674  1.36  drochner static int
    675  1.36  drochner in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa, const struct in6_addr *ia)
    676  1.36  drochner {
    677  1.36  drochner 	struct in6_addr ia2;
    678  1.36  drochner 
    679  1.36  drochner 	memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
    680  1.36  drochner 	if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr))
    681  1.36  drochner 		ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
    682  1.36  drochner 
    683  1.36  drochner 	return IN6_ARE_ADDR_EQUAL(ia, &ia2);
    684  1.36  drochner }
    685  1.36  drochner 
    686   1.1  jonathan int
    687  1.21  degroote ipsec6_process_packet(
    688  1.21  degroote 	struct mbuf *m,
    689  1.21  degroote  	struct ipsecrequest *isr
    690  1.21  degroote     )
    691   1.1  jonathan {
    692   1.1  jonathan 	struct secasindex saidx;
    693  1.21  degroote 	struct secasvar *sav;
    694  1.21  degroote 	struct ip6_hdr *ip6;
    695  1.35  drochner 	int s, error, i, off;
    696  1.35  drochner 	union sockaddr_union *dst;
    697   1.1  jonathan 
    698  1.44     ozaki 	KASSERT(m != NULL);
    699  1.44     ozaki 	KASSERT(isr != NULL);
    700   1.1  jonathan 
    701  1.21  degroote 	s = splsoftnet();   /* insure SA contents don't change */
    702  1.34  drochner 
    703   1.1  jonathan 	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
    704   1.1  jonathan 	if (isr == NULL) {
    705  1.26  degroote 		if (error != 0) {
    706  1.34  drochner 			/* XXX Should we send a notification ? */
    707  1.26  degroote 			goto bad;
    708  1.26  degroote 		} else {
    709  1.26  degroote 			if (ipsec_register_done(m, &error) < 0)
    710  1.26  degroote 				goto bad;
    711  1.26  degroote 
    712  1.26  degroote 			splx(s);
    713  1.28  degroote 			return ipsec_reinject_ipstack(m, AF_INET6);
    714  1.26  degroote 		}
    715   1.1  jonathan 	}
    716   1.1  jonathan 
    717  1.21  degroote 	sav = isr->sav;
    718  1.35  drochner 	dst = &sav->sah->saidx.dst;
    719   1.1  jonathan 
    720  1.35  drochner 	ip6 = mtod(m, struct ip6_hdr *); /* XXX */
    721   1.1  jonathan 
    722  1.35  drochner 	/* Do the appropriate encapsulation, if necessary */
    723  1.35  drochner 	if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
    724  1.35  drochner 	    dst->sa.sa_family != AF_INET6 ||        /* PF mismatch */
    725  1.35  drochner 	    ((dst->sa.sa_family == AF_INET6) &&
    726  1.35  drochner 	     (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
    727  1.36  drochner 	     (!in6_sa_equal_addrwithscope(&dst->sin6,
    728  1.35  drochner 				  &ip6->ip6_dst)))) {
    729  1.35  drochner 		struct mbuf *mp;
    730  1.35  drochner 
    731  1.35  drochner 		/* Fix IPv6 header payload length. */
    732  1.35  drochner 		if (m->m_len < sizeof(struct ip6_hdr))
    733  1.35  drochner 			if ((m = m_pullup(m,sizeof(struct ip6_hdr))) == NULL)
    734  1.35  drochner 				return ENOBUFS;
    735  1.34  drochner 
    736  1.35  drochner 		if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
    737  1.35  drochner 			/* No jumbogram support. */
    738  1.35  drochner 			m_freem(m);
    739  1.35  drochner 			return ENXIO;   /*XXX*/
    740  1.35  drochner 		}
    741  1.34  drochner 
    742  1.35  drochner 		ip6 = mtod(m, struct ip6_hdr *);
    743  1.35  drochner 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
    744   1.1  jonathan 
    745  1.35  drochner 		/* Encapsulate the packet */
    746  1.35  drochner 		error = ipip_output(m, isr, &mp, 0, 0);
    747  1.35  drochner 		if (mp == NULL && !error) {
    748  1.35  drochner 			/* Should never happen. */
    749  1.35  drochner 			DPRINTF(("ipsec6_process_packet: ipip_output "
    750  1.35  drochner 				 "returns no mbuf and no error!"));
    751  1.35  drochner 			error = EFAULT;
    752  1.35  drochner 		}
    753  1.35  drochner 
    754  1.35  drochner 		if (error) {
    755  1.35  drochner 			if (mp) {
    756  1.35  drochner 				/* XXX: Should never happen! */
    757  1.35  drochner 				m_freem(mp);
    758  1.21  degroote 			}
    759  1.35  drochner 			m = NULL; /* ipip_output() already freed it */
    760  1.35  drochner 			goto bad;
    761  1.35  drochner 		}
    762  1.35  drochner 
    763  1.35  drochner 		m = mp;
    764  1.35  drochner 		mp = NULL;
    765  1.35  drochner 	}
    766   1.1  jonathan 
    767  1.35  drochner 	if (dst->sa.sa_family == AF_INET) {
    768  1.35  drochner 		struct ip *ip;
    769  1.35  drochner 		ip = mtod(m, struct ip *);
    770  1.35  drochner 		i = ip->ip_hl << 2;
    771  1.35  drochner 		off = offsetof(struct ip, ip_p);
    772  1.35  drochner 	} else {
    773  1.38  drochner 		compute_ipsec_pos(m, &i, &off);
    774  1.34  drochner 	}
    775  1.35  drochner 	error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
    776  1.34  drochner 	splx(s);
    777  1.34  drochner 	return error;
    778   1.1  jonathan bad:
    779  1.21  degroote 	splx(s);
    780   1.1  jonathan 	if (m)
    781   1.1  jonathan 		m_freem(m);
    782   1.1  jonathan 	return error;
    783   1.1  jonathan }
    784   1.1  jonathan #endif /*INET6*/
    785