Home | History | Annotate | Line # | Download | only in netipsec
ipsec_output.c revision 1.2
      1 /*	$NetBSD: ipsec_output.c,v 1.2 2003/08/15 03:42:07 jonathan Exp $	*/
      2 /*	$FreeBSD: src/sys/netipsec/ipsec_output.c,v 1.3.2.1 2003/01/24 05:11:35 sam Exp $	*/
      3 /*	$KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $	*/
      4 
      5 #include <sys/cdefs.h>
      6 __KERNEL_RCSID(0, "$NetBSD: ipsec_output.c,v 1.2 2003/08/15 03:42:07 jonathan Exp $");
      7 
      8 /*
      9  * IPsec output processing.
     10  */
     11 #include "opt_inet.h"
     12 #include "opt_inet6.h"
     13 #include "opt_ipsec.h"
     14 
     15 #include <sys/param.h>
     16 #include <sys/systm.h>
     17 #include <sys/mbuf.h>
     18 #include <sys/domain.h>
     19 #include <sys/protosw.h>
     20 #include <sys/socket.h>
     21 #include <sys/errno.h>
     22 #include <sys/syslog.h>
     23 
     24 #include <net/if.h>
     25 #include <net/route.h>
     26 
     27 #include <netinet/in.h>
     28 #include <netinet/in_systm.h>
     29 #include <netinet/ip.h>
     30 #include <netinet/ip_var.h>
     31 #include <netinet/in_var.h>
     32 #include <netinet/ip_ecn.h>
     33 #ifdef INET6
     34 #include <netinet6/ip6_ecn.h>
     35 #endif
     36 
     37 #include <netinet/ip6.h>
     38 #ifdef INET6
     39 #include <netinet6/ip6_var.h>
     40 #endif
     41 #include <netinet/in_pcb.h>
     42 #ifdef INET6
     43 #include <netinet/icmp6.h>
     44 #endif
     45 
     46 #include <netipsec/ipsec.h>
     47 #ifdef INET6
     48 #include <netipsec/ipsec6.h>
     49 #endif
     50 #include <netipsec/ah_var.h>
     51 #include <netipsec/esp_var.h>
     52 #include <netipsec/ipcomp_var.h>
     53 
     54 #include <netipsec/xform.h>
     55 
     56 #include <netipsec/key.h>
     57 #include <netipsec/keydb.h>
     58 #include <netipsec/key_debug.h>
     59 #include <netipsec/ipsec_osdep.h>
     60 
     61 int
     62 ipsec_process_done(struct mbuf *m, struct ipsecrequest *isr)
     63 {
     64 	struct tdb_ident *tdbi;
     65 	struct m_tag *mtag;
     66 	struct secasvar *sav;
     67 	struct secasindex *saidx;
     68 	int error;
     69 
     70 	IPSEC_SPLASSERT_SOFTNET("ipsec_process_done");
     71 
     72 	IPSEC_ASSERT(m != NULL, ("ipsec_process_done: null mbuf"));
     73 	IPSEC_ASSERT(isr != NULL, ("ipsec_process_done: null ISR"));
     74 	sav = isr->sav;
     75 	IPSEC_ASSERT(sav != NULL, ("ipsec_process_done: null SA"));
     76 	IPSEC_ASSERT(sav->sah != NULL, ("ipsec_process_done: null SAH"));
     77 
     78 	saidx = &sav->sah->saidx;
     79 	switch (saidx->dst.sa.sa_family) {
     80 #ifdef INET
     81 	case AF_INET:
     82 		/* Fix the header length, for AH processing. */
     83 		mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
     84 		break;
     85 #endif /* INET */
     86 #ifdef INET6
     87 	case AF_INET6:
     88 		/* Fix the header length, for AH processing. */
     89 		if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
     90 			error = ENXIO;
     91 			goto bad;
     92 		}
     93 		if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
     94 			/* No jumbogram support. */
     95 			error = ENXIO;	/*?*/
     96 			goto bad;
     97 		}
     98 		mtod(m, struct ip6_hdr *)->ip6_plen =
     99 			htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
    100 		break;
    101 #endif /* INET6 */
    102 	default:
    103 		DPRINTF(("ipsec_process_done: unknown protocol family %u\n",
    104 		    saidx->dst.sa.sa_family));
    105 		error = ENXIO;
    106 		goto bad;
    107 	}
    108 
    109 	/*
    110 	 * Add a record of what we've done or what needs to be done to the
    111 	 * packet.
    112 	 */
    113 	mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE,
    114 			sizeof(struct tdb_ident), M_NOWAIT);
    115 	if (mtag == NULL) {
    116 		DPRINTF(("ipsec_process_done: could not get packet tag\n"));
    117 		error = ENOMEM;
    118 		goto bad;
    119 	}
    120 
    121 	tdbi = (struct tdb_ident *)(mtag + 1);
    122 	tdbi->dst = saidx->dst;
    123 	tdbi->proto = saidx->proto;
    124 	tdbi->spi = sav->spi;
    125 	m_tag_prepend(m, mtag);
    126 
    127 	/*
    128 	 * If there's another (bundled) SA to apply, do so.
    129 	 * Note that this puts a burden on the kernel stack size.
    130 	 * If this is a problem we'll need to introduce a queue
    131 	 * to set the packet on so we can unwind the stack before
    132 	 * doing further processing.
    133 	 */
    134 	if (isr->next) {
    135 		newipsecstat.ips_out_bundlesa++;
    136 		return ipsec4_process_packet(m, isr->next, 0, 0);
    137 	}
    138 
    139 	/*
    140 	 * We're done with IPsec processing, transmit the packet using the
    141 	 * appropriate network protocol (IP or IPv6). SPD lookup will be
    142 	 * performed again there.
    143 	 */
    144 	switch (saidx->dst.sa.sa_family) {
    145 #ifdef INET
    146 	struct ip *ip;
    147 	case AF_INET:
    148 		ip = mtod(m, struct ip *);
    149 #ifdef __FreeBSD__
    150 		/* FreeBSD ip_output() expects ip_len, ip_off in host endian */
    151 		ip->ip_len = ntohs(ip->ip_len);
    152 		ip->ip_off = ntohs(ip->ip_off);
    153 #endif /* __FreeBSD_ */
    154 		return ip_output(m, NULL, NULL, IP_RAWOUTPUT,
    155 		    (struct ip_moptions *)0, (struct inpcb*)0);
    156 
    157 #endif /* INET */
    158 #ifdef INET6
    159 	case AF_INET6:
    160 		/*
    161 		 * We don't need massage, IPv6 header fields are always in
    162 		 * net endian.
    163 		 */
    164 		return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
    165 #endif /* INET6 */
    166 	}
    167 	panic("ipsec_process_done");
    168 bad:
    169 	m_freem(m);
    170 	KEY_FREESAV(&sav);
    171 	return (error);
    172 }
    173 
    174 static struct ipsecrequest *
    175 ipsec_nextisr(
    176 	struct mbuf *m,
    177 	struct ipsecrequest *isr,
    178 	int af,
    179 	struct secasindex *saidx,
    180 	int *error
    181 )
    182 {
    183 #define IPSEC_OSTAT(x,y,z) (isr->saidx.proto == IPPROTO_ESP ? (x)++ : \
    184 			    isr->saidx.proto == IPPROTO_AH ? (y)++ : (z)++)
    185 	struct secasvar *sav;
    186 
    187 	IPSEC_SPLASSERT_SOFTNET("ipsec_nextisr");
    188 	IPSEC_ASSERT(af == AF_INET || af == AF_INET6,
    189 		("ipsec_nextisr: invalid address family %u", af));
    190 again:
    191 	/*
    192 	 * Craft SA index to search for proper SA.  Note that
    193 	 * we only fillin unspecified SA peers for transport
    194 	 * mode; for tunnel mode they must already be filled in.
    195 	 */
    196 	*saidx = isr->saidx;
    197 	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
    198 		/* Fillin unspecified SA peers only for transport mode */
    199 		if (af == AF_INET) {
    200 			struct sockaddr_in *sin;
    201 			struct ip *ip = mtod(m, struct ip *);
    202 
    203 			if (saidx->src.sa.sa_len == 0) {
    204 				sin = &saidx->src.sin;
    205 				sin->sin_len = sizeof(*sin);
    206 				sin->sin_family = AF_INET;
    207 				sin->sin_port = IPSEC_PORT_ANY;
    208 				sin->sin_addr = ip->ip_src;
    209 			}
    210 			if (saidx->dst.sa.sa_len == 0) {
    211 				sin = &saidx->dst.sin;
    212 				sin->sin_len = sizeof(*sin);
    213 				sin->sin_family = AF_INET;
    214 				sin->sin_port = IPSEC_PORT_ANY;
    215 				sin->sin_addr = ip->ip_dst;
    216 			}
    217 		} else {
    218 			struct sockaddr_in6 *sin6;
    219 			struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    220 
    221 			if (saidx->src.sin6.sin6_len == 0) {
    222 				sin6 = (struct sockaddr_in6 *)&saidx->src;
    223 				sin6->sin6_len = sizeof(*sin6);
    224 				sin6->sin6_family = AF_INET6;
    225 				sin6->sin6_port = IPSEC_PORT_ANY;
    226 				sin6->sin6_addr = ip6->ip6_src;
    227 				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
    228 					/* fix scope id for comparing SPD */
    229 					sin6->sin6_addr.s6_addr16[1] = 0;
    230 					sin6->sin6_scope_id =
    231 					    ntohs(ip6->ip6_src.s6_addr16[1]);
    232 				}
    233 			}
    234 			if (saidx->dst.sin6.sin6_len == 0) {
    235 				sin6 = (struct sockaddr_in6 *)&saidx->dst;
    236 				sin6->sin6_len = sizeof(*sin6);
    237 				sin6->sin6_family = AF_INET6;
    238 				sin6->sin6_port = IPSEC_PORT_ANY;
    239 				sin6->sin6_addr = ip6->ip6_dst;
    240 				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
    241 					/* fix scope id for comparing SPD */
    242 					sin6->sin6_addr.s6_addr16[1] = 0;
    243 					sin6->sin6_scope_id =
    244 					    ntohs(ip6->ip6_dst.s6_addr16[1]);
    245 				}
    246 			}
    247 		}
    248 	}
    249 
    250 	/*
    251 	 * Lookup SA and validate it.
    252 	 */
    253 	*error = key_checkrequest(isr, saidx);
    254 	if (*error != 0) {
    255 		/*
    256 		 * IPsec processing is required, but no SA found.
    257 		 * I assume that key_acquire() had been called
    258 		 * to get/establish the SA. Here I discard
    259 		 * this packet because it is responsibility for
    260 		 * upper layer to retransmit the packet.
    261 		 */
    262 		newipsecstat.ips_out_nosa++;
    263 		goto bad;
    264 	}
    265 	sav = isr->sav;
    266 	if (sav == NULL) {		/* XXX valid return */
    267 		IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
    268 			("ipsec_nextisr: no SA found, but required; level %u",
    269 			ipsec_get_reqlevel(isr)));
    270 		isr = isr->next;
    271 		if (isr == NULL) {
    272 			/*XXXstatistic??*/
    273 			*error = EINVAL;		/*XXX*/
    274 			return isr;
    275 		}
    276 		goto again;
    277 	}
    278 
    279 	/*
    280 	 * Check system global policy controls.
    281 	 */
    282 	if ((isr->saidx.proto == IPPROTO_ESP && !esp_enable) ||
    283 	    (isr->saidx.proto == IPPROTO_AH && !ah_enable) ||
    284 	    (isr->saidx.proto == IPPROTO_IPCOMP && !ipcomp_enable)) {
    285 		DPRINTF(("ipsec_nextisr: IPsec outbound packet dropped due"
    286 			" to policy (check your sysctls)\n"));
    287 		IPSEC_OSTAT(espstat.esps_pdrops, ahstat.ahs_pdrops,
    288 		    ipcompstat.ipcomps_pdrops);
    289 		*error = EHOSTUNREACH;
    290 		goto bad;
    291 	}
    292 
    293 	/*
    294 	 * Sanity check the SA contents for the caller
    295 	 * before they invoke the xform output method.
    296 	 */
    297 	if (sav->tdb_xform == NULL) {
    298 		DPRINTF(("ipsec_nextisr: no transform for SA\n"));
    299 		IPSEC_OSTAT(espstat.esps_noxform, ahstat.ahs_noxform,
    300 		    ipcompstat.ipcomps_noxform);
    301 		*error = EHOSTUNREACH;
    302 		goto bad;
    303 	}
    304 	return isr;
    305 bad:
    306 	IPSEC_ASSERT(*error != 0, ("ipsec_nextisr: error return w/ no error code"));
    307 	return NULL;
    308 #undef IPSEC_OSTAT
    309 }
    310 
    311 #ifdef INET
    312 /*
    313  * IPsec output logic for IPv4.
    314  */
    315 int
    316 ipsec4_process_packet(
    317 	struct mbuf *m,
    318 	struct ipsecrequest *isr,
    319 	int flags,
    320 	int tunalready)
    321 {
    322 	struct secasindex saidx;
    323 	struct secasvar *sav;
    324 	struct ip *ip;
    325 	int s, error, i, off;
    326 
    327 	IPSEC_ASSERT(m != NULL, ("ipsec4_process_packet: null mbuf"));
    328 	IPSEC_ASSERT(isr != NULL, ("ipsec4_process_packet: null isr"));
    329 
    330 	s = splsoftnet();			/* insure SA contents don't change */
    331 
    332 	isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
    333 	if (isr == NULL)
    334 		goto bad;
    335 
    336 	sav = isr->sav;
    337 	if (!tunalready) {
    338 		union sockaddr_union *dst = &sav->sah->saidx.dst;
    339 		int setdf;
    340 
    341 		/*
    342 		 * Collect IP_DF state from the outer header.
    343 		 */
    344 		if (dst->sa.sa_family == AF_INET) {
    345 			if (m->m_len < sizeof (struct ip) &&
    346 			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
    347 				error = ENOBUFS;
    348 				goto bad;
    349 			}
    350 			ip = mtod(m, struct ip *);
    351 			/* Honor system-wide control of how to handle IP_DF */
    352 			switch (ip4_ipsec_dfbit) {
    353 			case 0:			/* clear in outer header */
    354 			case 1:			/* set in outer header */
    355 				setdf = ip4_ipsec_dfbit;
    356 				break;
    357 			default:		/* propagate to outer header */
    358 				setdf = ntohs(ip->ip_off & IP_DF);
    359 				break;
    360 			}
    361 		} else {
    362 			ip = NULL;		/* keep compiler happy */
    363 			setdf = 0;
    364 		}
    365 		/* Do the appropriate encapsulation, if necessary */
    366 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
    367 		    dst->sa.sa_family != AF_INET ||	    /* PF mismatch */
    368 #if 0
    369 		    (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
    370 		    sav->tdb_xform->xf_type == XF_IP4 ||    /* ditto */
    371 #endif
    372 		    (dst->sa.sa_family == AF_INET &&	    /* Proxy */
    373 		     dst->sin.sin_addr.s_addr != INADDR_ANY &&
    374 		     dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
    375 			struct mbuf *mp;
    376 
    377 			/* Fix IPv4 header checksum and length */
    378 			if (m->m_len < sizeof (struct ip) &&
    379 			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
    380 				error = ENOBUFS;
    381 				goto bad;
    382 			}
    383 			ip = mtod(m, struct ip *);
    384 			ip->ip_len = htons(m->m_pkthdr.len);
    385 			ip->ip_sum = 0;
    386 #ifdef _IP_VHL
    387 			if (ip->ip_vhl == IP_VHL_BORING)
    388 				ip->ip_sum = in_cksum_hdr(ip);
    389 			else
    390 				ip->ip_sum = in_cksum(m,
    391 					_IP_VHL_HL(ip->ip_vhl) << 2);
    392 #else
    393 			ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
    394 #endif
    395 
    396 			/* Encapsulate the packet */
    397 			error = ipip_output(m, isr, &mp, 0, 0);
    398 			if (mp == NULL && !error) {
    399 				/* Should never happen. */
    400 				DPRINTF(("ipsec4_process_packet: ipip_output "
    401 					"returns no mbuf and no error!"));
    402 				error = EFAULT;
    403 			}
    404 			if (error) {
    405 				if (mp)
    406 					m_freem(mp);
    407 				goto bad;
    408 			}
    409 			m = mp, mp = NULL;
    410 			/*
    411 			 * ipip_output clears IP_DF in the new header.  If
    412 			 * we need to propagate IP_DF from the outer header,
    413 			 * then we have to do it here.
    414 			 *
    415 			 * XXX shouldn't assume what ipip_output does.
    416 			 */
    417 			if (dst->sa.sa_family == AF_INET && setdf) {
    418 				if (m->m_len < sizeof (struct ip) &&
    419 				    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
    420 					error = ENOBUFS;
    421 					goto bad;
    422 				}
    423 				ip = mtod(m, struct ip *);
    424 				ip->ip_off = ntohs(ip->ip_off);
    425 				ip->ip_off |= IP_DF;
    426 				ip->ip_off = htons(ip->ip_off);
    427 			}
    428 		}
    429 	}
    430 
    431 	/*
    432 	 * Dispatch to the appropriate IPsec transform logic.  The
    433 	 * packet will be returned for transmission after crypto
    434 	 * processing, etc. are completed.  For encapsulation we
    435 	 * bypass this call because of the explicit call done above
    436 	 * (necessary to deal with IP_DF handling for IPv4).
    437 	 *
    438 	 * NB: m & sav are ``passed to caller'' who's reponsible for
    439 	 *     for reclaiming their resources.
    440 	 */
    441 	if (sav->tdb_xform->xf_type != XF_IP4) {
    442 		ip = mtod(m, struct ip *);
    443 		i = ip->ip_hl << 2;
    444 		off = offsetof(struct ip, ip_p);
    445 		error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
    446 	} else {
    447 		error = ipsec_process_done(m, isr);
    448 	}
    449 	splx(s);
    450 	return error;
    451 bad:
    452 	splx(s);
    453 	if (m)
    454 		m_freem(m);
    455 	return error;
    456 }
    457 #endif
    458 
    459 #ifdef INET6
    460 /*
    461  * Chop IP6 header from the payload.
    462  */
    463 static struct mbuf *
    464 ipsec6_splithdr(struct mbuf *m)
    465 {
    466 	struct mbuf *mh;
    467 	struct ip6_hdr *ip6;
    468 	int hlen;
    469 
    470 	IPSEC_ASSERT(m->m_len >= sizeof (struct ip6_hdr),
    471 		("ipsec6_splithdr: first mbuf too short, len %u", m->m_len));
    472 	ip6 = mtod(m, struct ip6_hdr *);
    473 	hlen = sizeof(struct ip6_hdr);
    474 	if (m->m_len > hlen) {
    475 		MGETHDR(mh, M_DONTWAIT, MT_HEADER);
    476 		if (!mh) {
    477 			m_freem(m);
    478 			return NULL;
    479 		}
    480 		M_MOVE_PKTHDR(mh, m);
    481 		MH_ALIGN(mh, hlen);
    482 		m->m_len -= hlen;
    483 		m->m_data += hlen;
    484 		mh->m_next = m;
    485 		m = mh;
    486 		m->m_len = hlen;
    487 		bcopy((caddr_t)ip6, mtod(m, caddr_t), hlen);
    488 	} else if (m->m_len < hlen) {
    489 		m = m_pullup(m, hlen);
    490 		if (!m)
    491 			return NULL;
    492 	}
    493 	return m;
    494 }
    495 
    496 /*
    497  * IPsec output logic for IPv6, transport mode.
    498  */
    499 int
    500 ipsec6_output_trans(
    501 	struct ipsec_output_state *state,
    502 	u_char *nexthdrp,
    503 	struct mbuf *mprev,
    504 	struct secpolicy *sp,
    505 	int flags,
    506 	int *tun)
    507 {
    508 	struct ipsecrequest *isr;
    509 	struct secasindex saidx;
    510 	int error = 0;
    511 	struct mbuf *m;
    512 
    513 	IPSEC_ASSERT(state != NULL, ("ipsec6_output: null state"));
    514 	IPSEC_ASSERT(state->m != NULL, ("ipsec6_output: null m"));
    515 	IPSEC_ASSERT(nexthdrp != NULL, ("ipsec6_output: null nexthdrp"));
    516 	IPSEC_ASSERT(mprev != NULL, ("ipsec6_output: null mprev"));
    517 	IPSEC_ASSERT(sp != NULL, ("ipsec6_output: null sp"));
    518 	IPSEC_ASSERT(tun != NULL, ("ipsec6_output: null tun"));
    519 
    520 	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
    521 		printf("ipsec6_output_trans: applyed SP\n");
    522 		kdebug_secpolicy(sp));
    523 
    524 	isr = sp->req;
    525 	if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
    526 		/* the rest will be handled by ipsec6_output_tunnel() */
    527 		*tun = 1;		/* need tunnel-mode processing */
    528 		return 0;
    529 	}
    530 
    531 	*tun = 0;
    532 	m = state->m;
    533 
    534 	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
    535 	if (isr == NULL) {
    536 #ifdef notdef
    537 		/* XXX should notification be done for all errors ? */
    538 		/*
    539 		 * Notify the fact that the packet is discarded
    540 		 * to ourselves. I believe this is better than
    541 		 * just silently discarding. (jinmei (at) kame.net)
    542 		 * XXX: should we restrict the error to TCP packets?
    543 		 * XXX: should we directly notify sockets via
    544 		 *      pfctlinputs?
    545 		 */
    546 		icmp6_error(m, ICMP6_DST_UNREACH,
    547 			    ICMP6_DST_UNREACH_ADMIN, 0);
    548 		m = NULL;	/* NB: icmp6_error frees mbuf */
    549 #endif
    550 		goto bad;
    551 	}
    552 
    553 	return (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
    554 		sizeof (struct ip6_hdr),
    555 		offsetof(struct ip6_hdr, ip6_nxt));
    556 bad:
    557 	if (m)
    558 		m_freem(m);
    559 	state->m = NULL;
    560 	return error;
    561 }
    562 
    563 static int
    564 ipsec6_encapsulate(struct mbuf *m, struct secasvar *sav)
    565 {
    566 	struct ip6_hdr *oip6;
    567 	struct ip6_hdr *ip6;
    568 	size_t plen;
    569 
    570 	/* can't tunnel between different AFs */
    571 	if (sav->sah->saidx.src.sa.sa_family != AF_INET6 ||
    572 	    sav->sah->saidx.dst.sa.sa_family != AF_INET6) {
    573 		m_freem(m);
    574 		return EINVAL;
    575 	}
    576 	IPSEC_ASSERT(m->m_len != sizeof (struct ip6_hdr),
    577 		("ipsec6_encapsulate: mbuf wrong size; len %u", m->m_len));
    578 
    579 
    580 	/*
    581 	 * grow the mbuf to accomodate the new IPv6 header.
    582 	 */
    583 	plen = m->m_pkthdr.len;
    584 	if (M_LEADINGSPACE(m->m_next) < sizeof(struct ip6_hdr)) {
    585 		struct mbuf *n;
    586 		MGET(n, M_DONTWAIT, MT_DATA);
    587 		if (!n) {
    588 			m_freem(m);
    589 			return ENOBUFS;
    590 		}
    591 		n->m_len = sizeof(struct ip6_hdr);
    592 		n->m_next = m->m_next;
    593 		m->m_next = n;
    594 		m->m_pkthdr.len += sizeof(struct ip6_hdr);
    595 		oip6 = mtod(n, struct ip6_hdr *);
    596 	} else {
    597 		m->m_next->m_len += sizeof(struct ip6_hdr);
    598 		m->m_next->m_data -= sizeof(struct ip6_hdr);
    599 		m->m_pkthdr.len += sizeof(struct ip6_hdr);
    600 		oip6 = mtod(m->m_next, struct ip6_hdr *);
    601 	}
    602 	ip6 = mtod(m, struct ip6_hdr *);
    603 	ovbcopy((caddr_t)ip6, (caddr_t)oip6, sizeof(struct ip6_hdr));
    604 
    605 	/* Fake link-local scope-class addresses */
    606 	if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
    607 		oip6->ip6_src.s6_addr16[1] = 0;
    608 	if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
    609 		oip6->ip6_dst.s6_addr16[1] = 0;
    610 
    611 	/* construct new IPv6 header. see RFC 2401 5.1.2.2 */
    612 	/* ECN consideration. */
    613 	ip6_ecn_ingress(ip6_ipsec_ecn, &ip6->ip6_flow, &oip6->ip6_flow);
    614 	if (plen < IPV6_MAXPACKET - sizeof(struct ip6_hdr))
    615 		ip6->ip6_plen = htons(plen);
    616 	else {
    617 		/* ip6->ip6_plen will be updated in ip6_output() */
    618 	}
    619 	ip6->ip6_nxt = IPPROTO_IPV6;
    620 	sav->sah->saidx.src.sin6.sin6_addr = ip6->ip6_src;
    621 	sav->sah->saidx.dst.sin6.sin6_addr = ip6->ip6_dst;
    622 	ip6->ip6_hlim = IPV6_DEFHLIM;
    623 
    624 	/* XXX Should ip6_src be updated later ? */
    625 
    626 	return 0;
    627 }
    628 
    629 /*
    630  * IPsec output logic for IPv6, tunnel mode.
    631  */
    632 int
    633 ipsec6_output_tunnel(struct ipsec_output_state *state, struct secpolicy *sp, int flags)
    634 {
    635 	struct ip6_hdr *ip6;
    636 	struct ipsecrequest *isr;
    637 	struct secasindex saidx;
    638 	int error;
    639 	struct sockaddr_in6* dst6;
    640 	struct mbuf *m;
    641 
    642 	IPSEC_ASSERT(state != NULL, ("ipsec6_output: null state"));
    643 	IPSEC_ASSERT(state->m != NULL, ("ipsec6_output: null m"));
    644 	IPSEC_ASSERT(sp != NULL, ("ipsec6_output: null sp"));
    645 
    646 	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
    647 		printf("ipsec6_output_tunnel: applyed SP\n");
    648 		kdebug_secpolicy(sp));
    649 
    650 	m = state->m;
    651 	/*
    652 	 * transport mode ipsec (before the 1st tunnel mode) is already
    653 	 * processed by ipsec6_output_trans().
    654 	 */
    655 	for (isr = sp->req; isr; isr = isr->next) {
    656 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
    657 			break;
    658 	}
    659 	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
    660 	if (isr == NULL)
    661 		goto bad;
    662 
    663 	/*
    664 	 * There may be the case that SA status will be changed when
    665 	 * we are refering to one. So calling splsoftnet().
    666 	 */
    667 	if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
    668 		/*
    669 		 * build IPsec tunnel.
    670 		 */
    671 		/* XXX should be processed with other familiy */
    672 		if (isr->sav->sah->saidx.src.sa.sa_family != AF_INET6) {
    673 			ipseclog((LOG_ERR, "ipsec6_output_tunnel: "
    674 			    "family mismatched between inner and outer, spi=%lu\n",
    675 			    ntohl(isr->sav->spi)));
    676 			newipsecstat.ips_out_inval++;
    677 			error = EAFNOSUPPORT;
    678 			goto bad;
    679 		}
    680 
    681 		m = ipsec6_splithdr(m);
    682 		if (!m) {
    683 			newipsecstat.ips_out_nomem++;
    684 			error = ENOMEM;
    685 			goto bad;
    686 		}
    687 		error = ipsec6_encapsulate(m, isr->sav);
    688 		if (error) {
    689 			m = NULL;
    690 			goto bad;
    691 		}
    692 		ip6 = mtod(m, struct ip6_hdr *);
    693 
    694 		state->ro = &isr->sav->sah->sa_route;
    695 		state->dst = (struct sockaddr *)&state->ro->ro_dst;
    696 		dst6 = (struct sockaddr_in6 *)state->dst;
    697 		if (state->ro->ro_rt
    698 		 && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0
    699 		  || !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_dst))) {
    700 			RTFREE(state->ro->ro_rt);
    701 			state->ro->ro_rt = NULL;
    702 		}
    703 		if (state->ro->ro_rt == 0) {
    704 			bzero(dst6, sizeof(*dst6));
    705 			dst6->sin6_family = AF_INET6;
    706 			dst6->sin6_len = sizeof(*dst6);
    707 			dst6->sin6_addr = ip6->ip6_dst;
    708 			rtalloc(state->ro);
    709 		}
    710 		if (state->ro->ro_rt == 0) {
    711 			ip6stat.ip6s_noroute++;
    712 			newipsecstat.ips_out_noroute++;
    713 			error = EHOSTUNREACH;
    714 			goto bad;
    715 		}
    716 
    717 		/* adjust state->dst if tunnel endpoint is offlink */
    718 		if (state->ro->ro_rt->rt_flags & RTF_GATEWAY) {
    719 			state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway;
    720 			dst6 = (struct sockaddr_in6 *)state->dst;
    721 		}
    722 	}
    723 
    724 	m = ipsec6_splithdr(m);
    725 	if (!m) {
    726 		newipsecstat.ips_out_nomem++;
    727 		error = ENOMEM;
    728 		goto bad;
    729 	}
    730 	ip6 = mtod(m, struct ip6_hdr *);
    731 	return (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
    732 		sizeof (struct ip6_hdr),
    733 		offsetof(struct ip6_hdr, ip6_nxt));
    734 bad:
    735 	if (m)
    736 		m_freem(m);
    737 	state->m = NULL;
    738 	return error;
    739 }
    740 #endif /*INET6*/
    741