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