Home | History | Annotate | Line # | Download | only in netipsec
ipsec_output.c revision 1.21
      1  1.21  degroote /*	$NetBSD: ipsec_output.c,v 1.21 2007/02/10 09:43:05 degroote 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.21  degroote __KERNEL_RCSID(0, "$NetBSD: ipsec_output.c,v 1.21 2007/02/10 09:43:05 degroote Exp $");
     33   1.1  jonathan 
     34   1.1  jonathan /*
     35   1.1  jonathan  * IPsec output processing.
     36   1.1  jonathan  */
     37   1.1  jonathan #include "opt_inet.h"
     38   1.4  jonathan #ifdef __FreeBSD__
     39   1.1  jonathan #include "opt_inet6.h"
     40   1.4  jonathan #endif
     41   1.1  jonathan #include "opt_ipsec.h"
     42   1.1  jonathan 
     43   1.1  jonathan #include <sys/param.h>
     44   1.1  jonathan #include <sys/systm.h>
     45   1.1  jonathan #include <sys/mbuf.h>
     46   1.1  jonathan #include <sys/domain.h>
     47   1.1  jonathan #include <sys/protosw.h>
     48   1.1  jonathan #include <sys/socket.h>
     49   1.1  jonathan #include <sys/errno.h>
     50   1.1  jonathan #include <sys/syslog.h>
     51   1.1  jonathan 
     52   1.1  jonathan #include <net/if.h>
     53   1.1  jonathan #include <net/route.h>
     54   1.1  jonathan 
     55   1.1  jonathan #include <netinet/in.h>
     56   1.1  jonathan #include <netinet/in_systm.h>
     57   1.1  jonathan #include <netinet/ip.h>
     58   1.1  jonathan #include <netinet/ip_var.h>
     59   1.1  jonathan #include <netinet/in_var.h>
     60   1.1  jonathan #include <netinet/ip_ecn.h>
     61   1.1  jonathan #ifdef INET6
     62  1.12  jonathan #  ifdef __FreeBSD__
     63  1.12  jonathan #  include <netinet6/ip6_ecn.h>
     64  1.12  jonathan #  endif
     65   1.1  jonathan #endif
     66   1.1  jonathan 
     67   1.1  jonathan #include <netinet/ip6.h>
     68   1.1  jonathan #ifdef INET6
     69   1.1  jonathan #include <netinet6/ip6_var.h>
     70   1.1  jonathan #endif
     71   1.1  jonathan #include <netinet/in_pcb.h>
     72   1.1  jonathan #ifdef INET6
     73   1.1  jonathan #include <netinet/icmp6.h>
     74   1.1  jonathan #endif
     75   1.1  jonathan 
     76   1.1  jonathan #include <netipsec/ipsec.h>
     77  1.13  jonathan #include <netipsec/ipsec_var.h>
     78   1.1  jonathan #ifdef INET6
     79   1.1  jonathan #include <netipsec/ipsec6.h>
     80   1.1  jonathan #endif
     81   1.1  jonathan #include <netipsec/ah_var.h>
     82   1.1  jonathan #include <netipsec/esp_var.h>
     83   1.1  jonathan #include <netipsec/ipcomp_var.h>
     84   1.1  jonathan 
     85   1.1  jonathan #include <netipsec/xform.h>
     86   1.1  jonathan 
     87   1.7       tls #include <netipsec/key.h>
     88   1.7       tls #include <netipsec/keydb.h>
     89   1.7       tls #include <netipsec/key_debug.h>
     90   1.1  jonathan #include <netipsec/ipsec_osdep.h>
     91   1.1  jonathan 
     92  1.10  jonathan #include <net/net_osdep.h>		/* ovbcopy() in ipsec6_encapsulate() */
     93  1.10  jonathan 
     94   1.1  jonathan int
     95   1.1  jonathan ipsec_process_done(struct mbuf *m, struct ipsecrequest *isr)
     96   1.1  jonathan {
     97   1.1  jonathan 	struct tdb_ident *tdbi;
     98   1.1  jonathan 	struct m_tag *mtag;
     99   1.1  jonathan 	struct secasvar *sav;
    100   1.1  jonathan 	struct secasindex *saidx;
    101   1.1  jonathan 	int error;
    102   1.1  jonathan 
    103   1.1  jonathan 	IPSEC_SPLASSERT_SOFTNET("ipsec_process_done");
    104   1.1  jonathan 
    105   1.1  jonathan 	IPSEC_ASSERT(m != NULL, ("ipsec_process_done: null mbuf"));
    106   1.1  jonathan 	IPSEC_ASSERT(isr != NULL, ("ipsec_process_done: null ISR"));
    107   1.1  jonathan 	sav = isr->sav;
    108   1.1  jonathan 	IPSEC_ASSERT(sav != NULL, ("ipsec_process_done: null SA"));
    109   1.1  jonathan 	IPSEC_ASSERT(sav->sah != NULL, ("ipsec_process_done: null SAH"));
    110   1.1  jonathan 
    111   1.1  jonathan 	saidx = &sav->sah->saidx;
    112   1.1  jonathan 	switch (saidx->dst.sa.sa_family) {
    113   1.1  jonathan #ifdef INET
    114   1.1  jonathan 	case AF_INET:
    115   1.1  jonathan 		/* Fix the header length, for AH processing. */
    116   1.1  jonathan 		mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
    117   1.1  jonathan 		break;
    118   1.1  jonathan #endif /* INET */
    119   1.1  jonathan #ifdef INET6
    120   1.1  jonathan 	case AF_INET6:
    121   1.1  jonathan 		/* Fix the header length, for AH processing. */
    122   1.1  jonathan 		if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
    123   1.1  jonathan 			error = ENXIO;
    124   1.1  jonathan 			goto bad;
    125   1.1  jonathan 		}
    126   1.1  jonathan 		if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
    127   1.1  jonathan 			/* No jumbogram support. */
    128   1.1  jonathan 			error = ENXIO;	/*?*/
    129   1.1  jonathan 			goto bad;
    130   1.1  jonathan 		}
    131   1.1  jonathan 		mtod(m, struct ip6_hdr *)->ip6_plen =
    132   1.1  jonathan 			htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
    133   1.1  jonathan 		break;
    134   1.1  jonathan #endif /* INET6 */
    135   1.1  jonathan 	default:
    136   1.1  jonathan 		DPRINTF(("ipsec_process_done: unknown protocol family %u\n",
    137   1.1  jonathan 		    saidx->dst.sa.sa_family));
    138   1.1  jonathan 		error = ENXIO;
    139   1.1  jonathan 		goto bad;
    140   1.1  jonathan 	}
    141   1.1  jonathan 
    142   1.1  jonathan 	/*
    143   1.1  jonathan 	 * Add a record of what we've done or what needs to be done to the
    144   1.1  jonathan 	 * packet.
    145   1.1  jonathan 	 */
    146   1.1  jonathan 	mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE,
    147   1.1  jonathan 			sizeof(struct tdb_ident), M_NOWAIT);
    148   1.1  jonathan 	if (mtag == NULL) {
    149   1.1  jonathan 		DPRINTF(("ipsec_process_done: could not get packet tag\n"));
    150   1.1  jonathan 		error = ENOMEM;
    151   1.1  jonathan 		goto bad;
    152   1.1  jonathan 	}
    153   1.1  jonathan 
    154   1.1  jonathan 	tdbi = (struct tdb_ident *)(mtag + 1);
    155   1.1  jonathan 	tdbi->dst = saidx->dst;
    156   1.1  jonathan 	tdbi->proto = saidx->proto;
    157   1.1  jonathan 	tdbi->spi = sav->spi;
    158   1.1  jonathan 	m_tag_prepend(m, mtag);
    159   1.1  jonathan 
    160   1.1  jonathan 	/*
    161   1.1  jonathan 	 * If there's another (bundled) SA to apply, do so.
    162   1.1  jonathan 	 * Note that this puts a burden on the kernel stack size.
    163   1.1  jonathan 	 * If this is a problem we'll need to introduce a queue
    164   1.1  jonathan 	 * to set the packet on so we can unwind the stack before
    165   1.1  jonathan 	 * doing further processing.
    166   1.1  jonathan 	 */
    167   1.1  jonathan 	if (isr->next) {
    168   1.1  jonathan 		newipsecstat.ips_out_bundlesa++;
    169  1.21  degroote         switch ( saidx->dst.sa.sa_family ) {
    170  1.21  degroote #ifdef INET
    171  1.21  degroote         case AF_INET:
    172  1.21  degroote 			return ipsec4_process_packet(m, isr->next, 0,0);
    173  1.21  degroote #endif /* INET */
    174  1.21  degroote #ifdef INET6
    175  1.21  degroote 		case AF_INET6:
    176  1.21  degroote         	return ipsec6_process_packet(m,isr->next);
    177  1.21  degroote #endif /* INET6 */
    178  1.21  degroote 		default :
    179  1.21  degroote 			DPRINTF(("ipsec_process_done: unknown protocol family %u\n",
    180  1.21  degroote                                saidx->dst.sa.sa_family));
    181  1.21  degroote 			error = ENXIO;
    182  1.21  degroote 			goto bad;
    183  1.21  degroote         }
    184   1.1  jonathan 	}
    185   1.1  jonathan 
    186   1.1  jonathan 	/*
    187   1.1  jonathan 	 * We're done with IPsec processing, transmit the packet using the
    188   1.1  jonathan 	 * appropriate network protocol (IP or IPv6). SPD lookup will be
    189   1.1  jonathan 	 * performed again there.
    190   1.1  jonathan 	 */
    191   1.1  jonathan 	switch (saidx->dst.sa.sa_family) {
    192   1.1  jonathan #ifdef INET
    193   1.1  jonathan 	struct ip *ip;
    194   1.1  jonathan 	case AF_INET:
    195   1.1  jonathan 		ip = mtod(m, struct ip *);
    196   1.1  jonathan #ifdef __FreeBSD__
    197   1.1  jonathan 		/* FreeBSD ip_output() expects ip_len, ip_off in host endian */
    198   1.1  jonathan 		ip->ip_len = ntohs(ip->ip_len);
    199   1.1  jonathan 		ip->ip_off = ntohs(ip->ip_off);
    200   1.1  jonathan #endif /* __FreeBSD_ */
    201   1.2  jonathan 		return ip_output(m, NULL, NULL, IP_RAWOUTPUT,
    202   1.5    itojun 		    (struct ip_moptions *)NULL, (struct socket *)NULL);
    203   1.2  jonathan 
    204   1.1  jonathan #endif /* INET */
    205   1.1  jonathan #ifdef INET6
    206   1.1  jonathan 	case AF_INET6:
    207   1.1  jonathan 		/*
    208   1.1  jonathan 		 * We don't need massage, IPv6 header fields are always in
    209   1.1  jonathan 		 * net endian.
    210   1.1  jonathan 		 */
    211   1.1  jonathan 		return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
    212   1.1  jonathan #endif /* INET6 */
    213   1.1  jonathan 	}
    214   1.1  jonathan 	panic("ipsec_process_done");
    215   1.1  jonathan bad:
    216   1.1  jonathan 	m_freem(m);
    217   1.1  jonathan 	KEY_FREESAV(&sav);
    218   1.1  jonathan 	return (error);
    219   1.1  jonathan }
    220   1.1  jonathan 
    221   1.1  jonathan static struct ipsecrequest *
    222   1.1  jonathan ipsec_nextisr(
    223   1.1  jonathan 	struct mbuf *m,
    224   1.1  jonathan 	struct ipsecrequest *isr,
    225   1.1  jonathan 	int af,
    226   1.1  jonathan 	struct secasindex *saidx,
    227   1.1  jonathan 	int *error
    228   1.1  jonathan )
    229   1.1  jonathan {
    230   1.1  jonathan #define IPSEC_OSTAT(x,y,z) (isr->saidx.proto == IPPROTO_ESP ? (x)++ : \
    231   1.1  jonathan 			    isr->saidx.proto == IPPROTO_AH ? (y)++ : (z)++)
    232   1.1  jonathan 	struct secasvar *sav;
    233   1.1  jonathan 
    234   1.1  jonathan 	IPSEC_SPLASSERT_SOFTNET("ipsec_nextisr");
    235   1.1  jonathan 	IPSEC_ASSERT(af == AF_INET || af == AF_INET6,
    236   1.1  jonathan 		("ipsec_nextisr: invalid address family %u", af));
    237   1.1  jonathan again:
    238   1.1  jonathan 	/*
    239   1.1  jonathan 	 * Craft SA index to search for proper SA.  Note that
    240   1.1  jonathan 	 * we only fillin unspecified SA peers for transport
    241   1.1  jonathan 	 * mode; for tunnel mode they must already be filled in.
    242   1.1  jonathan 	 */
    243   1.1  jonathan 	*saidx = isr->saidx;
    244   1.1  jonathan 	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
    245   1.1  jonathan 		/* Fillin unspecified SA peers only for transport mode */
    246   1.1  jonathan 		if (af == AF_INET) {
    247   1.1  jonathan 			struct sockaddr_in *sin;
    248   1.1  jonathan 			struct ip *ip = mtod(m, struct ip *);
    249   1.1  jonathan 
    250   1.1  jonathan 			if (saidx->src.sa.sa_len == 0) {
    251   1.1  jonathan 				sin = &saidx->src.sin;
    252   1.1  jonathan 				sin->sin_len = sizeof(*sin);
    253   1.1  jonathan 				sin->sin_family = AF_INET;
    254   1.1  jonathan 				sin->sin_port = IPSEC_PORT_ANY;
    255   1.1  jonathan 				sin->sin_addr = ip->ip_src;
    256   1.1  jonathan 			}
    257   1.1  jonathan 			if (saidx->dst.sa.sa_len == 0) {
    258   1.1  jonathan 				sin = &saidx->dst.sin;
    259   1.1  jonathan 				sin->sin_len = sizeof(*sin);
    260   1.1  jonathan 				sin->sin_family = AF_INET;
    261   1.1  jonathan 				sin->sin_port = IPSEC_PORT_ANY;
    262   1.1  jonathan 				sin->sin_addr = ip->ip_dst;
    263   1.1  jonathan 			}
    264   1.1  jonathan 		} else {
    265   1.1  jonathan 			struct sockaddr_in6 *sin6;
    266   1.1  jonathan 			struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    267   1.1  jonathan 
    268   1.1  jonathan 			if (saidx->src.sin6.sin6_len == 0) {
    269   1.1  jonathan 				sin6 = (struct sockaddr_in6 *)&saidx->src;
    270   1.1  jonathan 				sin6->sin6_len = sizeof(*sin6);
    271   1.1  jonathan 				sin6->sin6_family = AF_INET6;
    272   1.1  jonathan 				sin6->sin6_port = IPSEC_PORT_ANY;
    273   1.1  jonathan 				sin6->sin6_addr = ip6->ip6_src;
    274   1.1  jonathan 				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
    275   1.1  jonathan 					/* fix scope id for comparing SPD */
    276   1.1  jonathan 					sin6->sin6_addr.s6_addr16[1] = 0;
    277   1.1  jonathan 					sin6->sin6_scope_id =
    278   1.1  jonathan 					    ntohs(ip6->ip6_src.s6_addr16[1]);
    279   1.1  jonathan 				}
    280   1.1  jonathan 			}
    281   1.1  jonathan 			if (saidx->dst.sin6.sin6_len == 0) {
    282   1.1  jonathan 				sin6 = (struct sockaddr_in6 *)&saidx->dst;
    283   1.1  jonathan 				sin6->sin6_len = sizeof(*sin6);
    284   1.1  jonathan 				sin6->sin6_family = AF_INET6;
    285   1.1  jonathan 				sin6->sin6_port = IPSEC_PORT_ANY;
    286   1.1  jonathan 				sin6->sin6_addr = ip6->ip6_dst;
    287   1.1  jonathan 				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
    288   1.1  jonathan 					/* fix scope id for comparing SPD */
    289   1.1  jonathan 					sin6->sin6_addr.s6_addr16[1] = 0;
    290   1.1  jonathan 					sin6->sin6_scope_id =
    291   1.1  jonathan 					    ntohs(ip6->ip6_dst.s6_addr16[1]);
    292   1.1  jonathan 				}
    293   1.1  jonathan 			}
    294   1.1  jonathan 		}
    295   1.1  jonathan 	}
    296   1.1  jonathan 
    297   1.1  jonathan 	/*
    298   1.1  jonathan 	 * Lookup SA and validate it.
    299   1.1  jonathan 	 */
    300   1.1  jonathan 	*error = key_checkrequest(isr, saidx);
    301   1.1  jonathan 	if (*error != 0) {
    302   1.1  jonathan 		/*
    303   1.1  jonathan 		 * IPsec processing is required, but no SA found.
    304   1.1  jonathan 		 * I assume that key_acquire() had been called
    305   1.1  jonathan 		 * to get/establish the SA. Here I discard
    306   1.1  jonathan 		 * this packet because it is responsibility for
    307   1.1  jonathan 		 * upper layer to retransmit the packet.
    308   1.1  jonathan 		 */
    309   1.1  jonathan 		newipsecstat.ips_out_nosa++;
    310   1.1  jonathan 		goto bad;
    311   1.1  jonathan 	}
    312   1.1  jonathan 	sav = isr->sav;
    313   1.1  jonathan 	if (sav == NULL) {		/* XXX valid return */
    314   1.1  jonathan 		IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
    315   1.1  jonathan 			("ipsec_nextisr: no SA found, but required; level %u",
    316   1.1  jonathan 			ipsec_get_reqlevel(isr)));
    317   1.1  jonathan 		isr = isr->next;
    318   1.1  jonathan 		if (isr == NULL) {
    319   1.1  jonathan 			/*XXXstatistic??*/
    320   1.1  jonathan 			*error = EINVAL;		/*XXX*/
    321   1.1  jonathan 			return isr;
    322   1.1  jonathan 		}
    323   1.1  jonathan 		goto again;
    324   1.1  jonathan 	}
    325   1.1  jonathan 
    326   1.1  jonathan 	/*
    327   1.1  jonathan 	 * Check system global policy controls.
    328   1.1  jonathan 	 */
    329   1.1  jonathan 	if ((isr->saidx.proto == IPPROTO_ESP && !esp_enable) ||
    330   1.1  jonathan 	    (isr->saidx.proto == IPPROTO_AH && !ah_enable) ||
    331   1.1  jonathan 	    (isr->saidx.proto == IPPROTO_IPCOMP && !ipcomp_enable)) {
    332   1.1  jonathan 		DPRINTF(("ipsec_nextisr: IPsec outbound packet dropped due"
    333   1.1  jonathan 			" to policy (check your sysctls)\n"));
    334   1.1  jonathan 		IPSEC_OSTAT(espstat.esps_pdrops, ahstat.ahs_pdrops,
    335   1.1  jonathan 		    ipcompstat.ipcomps_pdrops);
    336   1.1  jonathan 		*error = EHOSTUNREACH;
    337   1.1  jonathan 		goto bad;
    338   1.1  jonathan 	}
    339   1.1  jonathan 
    340   1.1  jonathan 	/*
    341   1.1  jonathan 	 * Sanity check the SA contents for the caller
    342   1.1  jonathan 	 * before they invoke the xform output method.
    343   1.1  jonathan 	 */
    344   1.1  jonathan 	if (sav->tdb_xform == NULL) {
    345   1.1  jonathan 		DPRINTF(("ipsec_nextisr: no transform for SA\n"));
    346   1.1  jonathan 		IPSEC_OSTAT(espstat.esps_noxform, ahstat.ahs_noxform,
    347   1.1  jonathan 		    ipcompstat.ipcomps_noxform);
    348   1.1  jonathan 		*error = EHOSTUNREACH;
    349   1.1  jonathan 		goto bad;
    350   1.1  jonathan 	}
    351   1.1  jonathan 	return isr;
    352   1.1  jonathan bad:
    353   1.1  jonathan 	IPSEC_ASSERT(*error != 0, ("ipsec_nextisr: error return w/ no error code"));
    354   1.1  jonathan 	return NULL;
    355   1.1  jonathan #undef IPSEC_OSTAT
    356   1.1  jonathan }
    357   1.1  jonathan 
    358   1.1  jonathan #ifdef INET
    359   1.1  jonathan /*
    360   1.1  jonathan  * IPsec output logic for IPv4.
    361   1.1  jonathan  */
    362   1.1  jonathan int
    363   1.1  jonathan ipsec4_process_packet(
    364  1.15  christos     struct mbuf *m,
    365  1.15  christos     struct ipsecrequest *isr,
    366  1.16  christos     int flags,
    367  1.15  christos     int tunalready
    368  1.15  christos )
    369   1.1  jonathan {
    370   1.1  jonathan 	struct secasindex saidx;
    371   1.1  jonathan 	struct secasvar *sav;
    372   1.1  jonathan 	struct ip *ip;
    373   1.1  jonathan 	int s, error, i, off;
    374   1.1  jonathan 
    375   1.1  jonathan 	IPSEC_ASSERT(m != NULL, ("ipsec4_process_packet: null mbuf"));
    376   1.1  jonathan 	IPSEC_ASSERT(isr != NULL, ("ipsec4_process_packet: null isr"));
    377   1.1  jonathan 
    378   1.1  jonathan 	s = splsoftnet();			/* insure SA contents don't change */
    379   1.1  jonathan 
    380   1.1  jonathan 	isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
    381   1.1  jonathan 	if (isr == NULL)
    382   1.1  jonathan 		goto bad;
    383   1.1  jonathan 
    384   1.1  jonathan 	sav = isr->sav;
    385   1.1  jonathan 	if (!tunalready) {
    386   1.1  jonathan 		union sockaddr_union *dst = &sav->sah->saidx.dst;
    387   1.1  jonathan 		int setdf;
    388   1.1  jonathan 
    389   1.1  jonathan 		/*
    390   1.1  jonathan 		 * Collect IP_DF state from the outer header.
    391   1.1  jonathan 		 */
    392   1.1  jonathan 		if (dst->sa.sa_family == AF_INET) {
    393   1.1  jonathan 			if (m->m_len < sizeof (struct ip) &&
    394   1.1  jonathan 			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
    395   1.1  jonathan 				error = ENOBUFS;
    396   1.1  jonathan 				goto bad;
    397   1.1  jonathan 			}
    398   1.1  jonathan 			ip = mtod(m, struct ip *);
    399   1.1  jonathan 			/* Honor system-wide control of how to handle IP_DF */
    400   1.1  jonathan 			switch (ip4_ipsec_dfbit) {
    401   1.1  jonathan 			case 0:			/* clear in outer header */
    402   1.1  jonathan 			case 1:			/* set in outer header */
    403   1.1  jonathan 				setdf = ip4_ipsec_dfbit;
    404   1.1  jonathan 				break;
    405   1.1  jonathan 			default:		/* propagate to outer header */
    406   1.3  jonathan 				setdf = ip->ip_off;
    407   1.3  jonathan #ifndef __FreeBSD__
    408   1.3  jonathan 		/* On FreeBSD, ip_off and ip_len assumed in host endian. */
    409   1.3  jonathan 				setdf = ntohs(setdf);
    410   1.3  jonathan #endif
    411   1.3  jonathan 				setdf = htons(setdf & IP_DF);
    412   1.1  jonathan 				break;
    413   1.1  jonathan 			}
    414   1.1  jonathan 		} else {
    415   1.1  jonathan 			ip = NULL;		/* keep compiler happy */
    416   1.1  jonathan 			setdf = 0;
    417   1.1  jonathan 		}
    418   1.1  jonathan 		/* Do the appropriate encapsulation, if necessary */
    419   1.1  jonathan 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
    420   1.1  jonathan 		    dst->sa.sa_family != AF_INET ||	    /* PF mismatch */
    421   1.1  jonathan #if 0
    422   1.1  jonathan 		    (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
    423   1.1  jonathan 		    sav->tdb_xform->xf_type == XF_IP4 ||    /* ditto */
    424   1.1  jonathan #endif
    425   1.1  jonathan 		    (dst->sa.sa_family == AF_INET &&	    /* Proxy */
    426   1.1  jonathan 		     dst->sin.sin_addr.s_addr != INADDR_ANY &&
    427   1.1  jonathan 		     dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
    428   1.1  jonathan 			struct mbuf *mp;
    429   1.1  jonathan 
    430   1.1  jonathan 			/* Fix IPv4 header checksum and length */
    431   1.1  jonathan 			if (m->m_len < sizeof (struct ip) &&
    432   1.1  jonathan 			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
    433   1.1  jonathan 				error = ENOBUFS;
    434   1.1  jonathan 				goto bad;
    435   1.1  jonathan 			}
    436   1.1  jonathan 			ip = mtod(m, struct ip *);
    437   1.1  jonathan 			ip->ip_len = htons(m->m_pkthdr.len);
    438   1.1  jonathan 			ip->ip_sum = 0;
    439   1.1  jonathan #ifdef _IP_VHL
    440   1.1  jonathan 			if (ip->ip_vhl == IP_VHL_BORING)
    441   1.1  jonathan 				ip->ip_sum = in_cksum_hdr(ip);
    442   1.1  jonathan 			else
    443   1.1  jonathan 				ip->ip_sum = in_cksum(m,
    444   1.1  jonathan 					_IP_VHL_HL(ip->ip_vhl) << 2);
    445   1.1  jonathan #else
    446   1.1  jonathan 			ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
    447   1.1  jonathan #endif
    448   1.1  jonathan 
    449   1.1  jonathan 			/* Encapsulate the packet */
    450   1.1  jonathan 			error = ipip_output(m, isr, &mp, 0, 0);
    451   1.1  jonathan 			if (mp == NULL && !error) {
    452   1.1  jonathan 				/* Should never happen. */
    453   1.1  jonathan 				DPRINTF(("ipsec4_process_packet: ipip_output "
    454   1.1  jonathan 					"returns no mbuf and no error!"));
    455   1.1  jonathan 				error = EFAULT;
    456   1.1  jonathan 			}
    457   1.1  jonathan 			if (error) {
    458   1.8       scw 				if (mp) {
    459   1.8       scw 					/* XXX: Should never happen! */
    460   1.1  jonathan 					m_freem(mp);
    461   1.8       scw 				}
    462   1.8       scw 				m = NULL; /* ipip_output() already freed it */
    463   1.1  jonathan 				goto bad;
    464   1.1  jonathan 			}
    465   1.1  jonathan 			m = mp, mp = NULL;
    466   1.1  jonathan 			/*
    467   1.1  jonathan 			 * ipip_output clears IP_DF in the new header.  If
    468   1.1  jonathan 			 * we need to propagate IP_DF from the outer header,
    469   1.1  jonathan 			 * then we have to do it here.
    470   1.1  jonathan 			 *
    471   1.1  jonathan 			 * XXX shouldn't assume what ipip_output does.
    472   1.1  jonathan 			 */
    473   1.1  jonathan 			if (dst->sa.sa_family == AF_INET && setdf) {
    474   1.1  jonathan 				if (m->m_len < sizeof (struct ip) &&
    475   1.1  jonathan 				    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
    476   1.1  jonathan 					error = ENOBUFS;
    477   1.1  jonathan 					goto bad;
    478   1.1  jonathan 				}
    479   1.1  jonathan 				ip = mtod(m, struct ip *);
    480   1.1  jonathan 				ip->ip_off = ntohs(ip->ip_off);
    481   1.1  jonathan 				ip->ip_off |= IP_DF;
    482   1.1  jonathan 				ip->ip_off = htons(ip->ip_off);
    483   1.1  jonathan 			}
    484   1.1  jonathan 		}
    485   1.1  jonathan 	}
    486   1.1  jonathan 
    487   1.1  jonathan 	/*
    488   1.1  jonathan 	 * Dispatch to the appropriate IPsec transform logic.  The
    489   1.1  jonathan 	 * packet will be returned for transmission after crypto
    490   1.1  jonathan 	 * processing, etc. are completed.  For encapsulation we
    491   1.1  jonathan 	 * bypass this call because of the explicit call done above
    492   1.1  jonathan 	 * (necessary to deal with IP_DF handling for IPv4).
    493   1.1  jonathan 	 *
    494   1.1  jonathan 	 * NB: m & sav are ``passed to caller'' who's reponsible for
    495   1.1  jonathan 	 *     for reclaiming their resources.
    496   1.1  jonathan 	 */
    497   1.1  jonathan 	if (sav->tdb_xform->xf_type != XF_IP4) {
    498   1.1  jonathan 		ip = mtod(m, struct ip *);
    499   1.1  jonathan 		i = ip->ip_hl << 2;
    500   1.1  jonathan 		off = offsetof(struct ip, ip_p);
    501   1.1  jonathan 		error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
    502   1.1  jonathan 	} else {
    503   1.1  jonathan 		error = ipsec_process_done(m, isr);
    504   1.1  jonathan 	}
    505   1.1  jonathan 	splx(s);
    506   1.1  jonathan 	return error;
    507   1.1  jonathan bad:
    508   1.1  jonathan 	splx(s);
    509   1.1  jonathan 	if (m)
    510   1.1  jonathan 		m_freem(m);
    511   1.1  jonathan 	return error;
    512   1.1  jonathan }
    513   1.1  jonathan #endif
    514   1.1  jonathan 
    515   1.1  jonathan #ifdef INET6
    516   1.1  jonathan int
    517  1.21  degroote ipsec6_process_packet(
    518  1.21  degroote 	struct mbuf *m,
    519  1.21  degroote  	struct ipsecrequest *isr
    520  1.21  degroote     )
    521   1.1  jonathan {
    522   1.1  jonathan 	struct secasindex saidx;
    523  1.21  degroote 	struct secasvar *sav;
    524  1.21  degroote 	struct ip6_hdr *ip6;
    525  1.21  degroote 	int s, error, i, off;
    526   1.1  jonathan 
    527  1.21  degroote 	IPSEC_ASSERT(m != NULL, ("ipsec6_process_packet: null mbuf"));
    528  1.21  degroote 	IPSEC_ASSERT(isr != NULL, ("ipsec6_process_packet: null isr"));
    529   1.1  jonathan 
    530  1.21  degroote 	s = splsoftnet();   /* insure SA contents don't change */
    531   1.1  jonathan 	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
    532   1.1  jonathan 	if (isr == NULL) {
    533  1.21  degroote 		// XXX Should we send a notification ?
    534   1.1  jonathan 		goto bad;
    535   1.1  jonathan 	}
    536   1.1  jonathan 
    537  1.21  degroote 	sav = isr->sav;
    538  1.21  degroote 	if (sav->tdb_xform->xf_type != XF_IP4) {
    539  1.21  degroote 		i = sizeof(struct ip6_hdr);
    540  1.21  degroote 		off = offsetof(struct ip6_hdr, ip6_nxt);
    541  1.21  degroote 		error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
    542  1.21  degroote        } else {
    543  1.21  degroote 		union sockaddr_union *dst = &sav->sah->saidx.dst;
    544   1.1  jonathan 
    545  1.21  degroote         ip6 = mtod(m, struct ip6_hdr *);
    546   1.1  jonathan 
    547  1.21  degroote 		/* Do the appropriate encapsulation, if necessary */
    548  1.21  degroote 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
    549  1.21  degroote                dst->sa.sa_family != AF_INET6 ||        /* PF mismatch */
    550  1.21  degroote             ((dst->sa.sa_family == AF_INET6) &&
    551  1.21  degroote                 (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
    552  1.21  degroote                 (!IN6_ARE_ADDR_EQUAL(&dst->sin6.sin6_addr,
    553  1.21  degroote                 &ip6->ip6_dst)))
    554  1.21  degroote             )
    555  1.21  degroote 		{
    556  1.21  degroote 			struct mbuf *mp;
    557  1.21  degroote             /* Fix IPv6 header payload length. */
    558  1.21  degroote             if (m->m_len < sizeof(struct ip6_hdr))
    559  1.21  degroote                 if ((m = m_pullup(m,sizeof(struct ip6_hdr))) == NULL)
    560  1.21  degroote                    return ENOBUFS;
    561  1.21  degroote 
    562  1.21  degroote             if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
    563  1.21  degroote                 /* No jumbogram support. */
    564  1.21  degroote                 m_freem(m);
    565  1.21  degroote                 return ENXIO;   /*XXX*/
    566  1.21  degroote             }
    567  1.21  degroote             ip6 = mtod(m, struct ip6_hdr *);
    568  1.21  degroote             ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
    569   1.1  jonathan 
    570  1.21  degroote 			/* Encapsulate the packet */
    571  1.21  degroote 			error = ipip_output(m, isr, &mp, 0, 0);
    572  1.21  degroote 			if (mp == NULL && !error) {
    573  1.21  degroote 				/* Should never happen. */
    574  1.21  degroote 				DPRINTF(("ipsec6_process_packet: ipip_output "
    575  1.21  degroote 						 "returns no mbuf and no error!"));
    576  1.21  degroote 				 error = EFAULT;
    577  1.21  degroote 			}
    578   1.1  jonathan 
    579  1.21  degroote 			if (error) {
    580  1.21  degroote 				if (mp) {
    581  1.21  degroote 					/* XXX: Should never happen! */
    582  1.21  degroote 					m_freem(mp);
    583  1.21  degroote 				}
    584  1.21  degroote 				m = NULL; /* ipip_output() already freed it */
    585  1.19     joerg 				goto bad;
    586  1.19     joerg 			}
    587  1.21  degroote 
    588  1.21  degroote 			m = mp;
    589  1.21  degroote 			mp = NULL;
    590   1.1  jonathan 		}
    591  1.21  degroote 
    592  1.21  degroote 		error = ipsec_process_done(m,isr);
    593   1.1  jonathan 		}
    594  1.21  degroote 		splx(s);
    595  1.21  degroote 		return error;
    596   1.1  jonathan bad:
    597  1.21  degroote 	splx(s);
    598   1.1  jonathan 	if (m)
    599   1.1  jonathan 		m_freem(m);
    600   1.1  jonathan 	return error;
    601   1.1  jonathan }
    602   1.1  jonathan #endif /*INET6*/
    603