Home | History | Annotate | Line # | Download | only in netipsec
ipsec_output.c revision 1.3
      1 /*	$NetBSD: ipsec_output.c,v 1.3 2003/08/15 17:14:31 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.3 2003/08/15 17:14:31 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 = ip->ip_off;
    359 #ifndef __FreeBSD__
    360 		/* On FreeBSD, ip_off and ip_len assumed in host endian. */
    361 				setdf = ntohs(setdf);
    362 #endif
    363 				setdf = htons(setdf & IP_DF);
    364 				break;
    365 			}
    366 		} else {
    367 			ip = NULL;		/* keep compiler happy */
    368 			setdf = 0;
    369 		}
    370 		/* Do the appropriate encapsulation, if necessary */
    371 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
    372 		    dst->sa.sa_family != AF_INET ||	    /* PF mismatch */
    373 #if 0
    374 		    (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
    375 		    sav->tdb_xform->xf_type == XF_IP4 ||    /* ditto */
    376 #endif
    377 		    (dst->sa.sa_family == AF_INET &&	    /* Proxy */
    378 		     dst->sin.sin_addr.s_addr != INADDR_ANY &&
    379 		     dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
    380 			struct mbuf *mp;
    381 
    382 			/* Fix IPv4 header checksum and length */
    383 			if (m->m_len < sizeof (struct ip) &&
    384 			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
    385 				error = ENOBUFS;
    386 				goto bad;
    387 			}
    388 			ip = mtod(m, struct ip *);
    389 			ip->ip_len = htons(m->m_pkthdr.len);
    390 			ip->ip_sum = 0;
    391 #ifdef _IP_VHL
    392 			if (ip->ip_vhl == IP_VHL_BORING)
    393 				ip->ip_sum = in_cksum_hdr(ip);
    394 			else
    395 				ip->ip_sum = in_cksum(m,
    396 					_IP_VHL_HL(ip->ip_vhl) << 2);
    397 #else
    398 			ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
    399 #endif
    400 
    401 			/* Encapsulate the packet */
    402 			error = ipip_output(m, isr, &mp, 0, 0);
    403 			if (mp == NULL && !error) {
    404 				/* Should never happen. */
    405 				DPRINTF(("ipsec4_process_packet: ipip_output "
    406 					"returns no mbuf and no error!"));
    407 				error = EFAULT;
    408 			}
    409 			if (error) {
    410 				if (mp)
    411 					m_freem(mp);
    412 				goto bad;
    413 			}
    414 			m = mp, mp = NULL;
    415 			/*
    416 			 * ipip_output clears IP_DF in the new header.  If
    417 			 * we need to propagate IP_DF from the outer header,
    418 			 * then we have to do it here.
    419 			 *
    420 			 * XXX shouldn't assume what ipip_output does.
    421 			 */
    422 			if (dst->sa.sa_family == AF_INET && setdf) {
    423 				if (m->m_len < sizeof (struct ip) &&
    424 				    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
    425 					error = ENOBUFS;
    426 					goto bad;
    427 				}
    428 				ip = mtod(m, struct ip *);
    429 				ip->ip_off = ntohs(ip->ip_off);
    430 				ip->ip_off |= IP_DF;
    431 				ip->ip_off = htons(ip->ip_off);
    432 			}
    433 		}
    434 	}
    435 
    436 	/*
    437 	 * Dispatch to the appropriate IPsec transform logic.  The
    438 	 * packet will be returned for transmission after crypto
    439 	 * processing, etc. are completed.  For encapsulation we
    440 	 * bypass this call because of the explicit call done above
    441 	 * (necessary to deal with IP_DF handling for IPv4).
    442 	 *
    443 	 * NB: m & sav are ``passed to caller'' who's reponsible for
    444 	 *     for reclaiming their resources.
    445 	 */
    446 	if (sav->tdb_xform->xf_type != XF_IP4) {
    447 		ip = mtod(m, struct ip *);
    448 		i = ip->ip_hl << 2;
    449 		off = offsetof(struct ip, ip_p);
    450 		error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
    451 	} else {
    452 		error = ipsec_process_done(m, isr);
    453 	}
    454 	splx(s);
    455 	return error;
    456 bad:
    457 	splx(s);
    458 	if (m)
    459 		m_freem(m);
    460 	return error;
    461 }
    462 #endif
    463 
    464 #ifdef INET6
    465 /*
    466  * Chop IP6 header from the payload.
    467  */
    468 static struct mbuf *
    469 ipsec6_splithdr(struct mbuf *m)
    470 {
    471 	struct mbuf *mh;
    472 	struct ip6_hdr *ip6;
    473 	int hlen;
    474 
    475 	IPSEC_ASSERT(m->m_len >= sizeof (struct ip6_hdr),
    476 		("ipsec6_splithdr: first mbuf too short, len %u", m->m_len));
    477 	ip6 = mtod(m, struct ip6_hdr *);
    478 	hlen = sizeof(struct ip6_hdr);
    479 	if (m->m_len > hlen) {
    480 		MGETHDR(mh, M_DONTWAIT, MT_HEADER);
    481 		if (!mh) {
    482 			m_freem(m);
    483 			return NULL;
    484 		}
    485 		M_MOVE_PKTHDR(mh, m);
    486 		MH_ALIGN(mh, hlen);
    487 		m->m_len -= hlen;
    488 		m->m_data += hlen;
    489 		mh->m_next = m;
    490 		m = mh;
    491 		m->m_len = hlen;
    492 		bcopy((caddr_t)ip6, mtod(m, caddr_t), hlen);
    493 	} else if (m->m_len < hlen) {
    494 		m = m_pullup(m, hlen);
    495 		if (!m)
    496 			return NULL;
    497 	}
    498 	return m;
    499 }
    500 
    501 /*
    502  * IPsec output logic for IPv6, transport mode.
    503  */
    504 int
    505 ipsec6_output_trans(
    506 	struct ipsec_output_state *state,
    507 	u_char *nexthdrp,
    508 	struct mbuf *mprev,
    509 	struct secpolicy *sp,
    510 	int flags,
    511 	int *tun)
    512 {
    513 	struct ipsecrequest *isr;
    514 	struct secasindex saidx;
    515 	int error = 0;
    516 	struct mbuf *m;
    517 
    518 	IPSEC_ASSERT(state != NULL, ("ipsec6_output: null state"));
    519 	IPSEC_ASSERT(state->m != NULL, ("ipsec6_output: null m"));
    520 	IPSEC_ASSERT(nexthdrp != NULL, ("ipsec6_output: null nexthdrp"));
    521 	IPSEC_ASSERT(mprev != NULL, ("ipsec6_output: null mprev"));
    522 	IPSEC_ASSERT(sp != NULL, ("ipsec6_output: null sp"));
    523 	IPSEC_ASSERT(tun != NULL, ("ipsec6_output: null tun"));
    524 
    525 	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
    526 		printf("ipsec6_output_trans: applyed SP\n");
    527 		kdebug_secpolicy(sp));
    528 
    529 	isr = sp->req;
    530 	if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
    531 		/* the rest will be handled by ipsec6_output_tunnel() */
    532 		*tun = 1;		/* need tunnel-mode processing */
    533 		return 0;
    534 	}
    535 
    536 	*tun = 0;
    537 	m = state->m;
    538 
    539 	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
    540 	if (isr == NULL) {
    541 #ifdef notdef
    542 		/* XXX should notification be done for all errors ? */
    543 		/*
    544 		 * Notify the fact that the packet is discarded
    545 		 * to ourselves. I believe this is better than
    546 		 * just silently discarding. (jinmei (at) kame.net)
    547 		 * XXX: should we restrict the error to TCP packets?
    548 		 * XXX: should we directly notify sockets via
    549 		 *      pfctlinputs?
    550 		 */
    551 		icmp6_error(m, ICMP6_DST_UNREACH,
    552 			    ICMP6_DST_UNREACH_ADMIN, 0);
    553 		m = NULL;	/* NB: icmp6_error frees mbuf */
    554 #endif
    555 		goto bad;
    556 	}
    557 
    558 	return (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
    559 		sizeof (struct ip6_hdr),
    560 		offsetof(struct ip6_hdr, ip6_nxt));
    561 bad:
    562 	if (m)
    563 		m_freem(m);
    564 	state->m = NULL;
    565 	return error;
    566 }
    567 
    568 static int
    569 ipsec6_encapsulate(struct mbuf *m, struct secasvar *sav)
    570 {
    571 	struct ip6_hdr *oip6;
    572 	struct ip6_hdr *ip6;
    573 	size_t plen;
    574 
    575 	/* can't tunnel between different AFs */
    576 	if (sav->sah->saidx.src.sa.sa_family != AF_INET6 ||
    577 	    sav->sah->saidx.dst.sa.sa_family != AF_INET6) {
    578 		m_freem(m);
    579 		return EINVAL;
    580 	}
    581 	IPSEC_ASSERT(m->m_len != sizeof (struct ip6_hdr),
    582 		("ipsec6_encapsulate: mbuf wrong size; len %u", m->m_len));
    583 
    584 
    585 	/*
    586 	 * grow the mbuf to accomodate the new IPv6 header.
    587 	 */
    588 	plen = m->m_pkthdr.len;
    589 	if (M_LEADINGSPACE(m->m_next) < sizeof(struct ip6_hdr)) {
    590 		struct mbuf *n;
    591 		MGET(n, M_DONTWAIT, MT_DATA);
    592 		if (!n) {
    593 			m_freem(m);
    594 			return ENOBUFS;
    595 		}
    596 		n->m_len = sizeof(struct ip6_hdr);
    597 		n->m_next = m->m_next;
    598 		m->m_next = n;
    599 		m->m_pkthdr.len += sizeof(struct ip6_hdr);
    600 		oip6 = mtod(n, struct ip6_hdr *);
    601 	} else {
    602 		m->m_next->m_len += sizeof(struct ip6_hdr);
    603 		m->m_next->m_data -= sizeof(struct ip6_hdr);
    604 		m->m_pkthdr.len += sizeof(struct ip6_hdr);
    605 		oip6 = mtod(m->m_next, struct ip6_hdr *);
    606 	}
    607 	ip6 = mtod(m, struct ip6_hdr *);
    608 	ovbcopy((caddr_t)ip6, (caddr_t)oip6, sizeof(struct ip6_hdr));
    609 
    610 	/* Fake link-local scope-class addresses */
    611 	if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
    612 		oip6->ip6_src.s6_addr16[1] = 0;
    613 	if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
    614 		oip6->ip6_dst.s6_addr16[1] = 0;
    615 
    616 	/* construct new IPv6 header. see RFC 2401 5.1.2.2 */
    617 	/* ECN consideration. */
    618 	ip6_ecn_ingress(ip6_ipsec_ecn, &ip6->ip6_flow, &oip6->ip6_flow);
    619 	if (plen < IPV6_MAXPACKET - sizeof(struct ip6_hdr))
    620 		ip6->ip6_plen = htons(plen);
    621 	else {
    622 		/* ip6->ip6_plen will be updated in ip6_output() */
    623 	}
    624 	ip6->ip6_nxt = IPPROTO_IPV6;
    625 	sav->sah->saidx.src.sin6.sin6_addr = ip6->ip6_src;
    626 	sav->sah->saidx.dst.sin6.sin6_addr = ip6->ip6_dst;
    627 	ip6->ip6_hlim = IPV6_DEFHLIM;
    628 
    629 	/* XXX Should ip6_src be updated later ? */
    630 
    631 	return 0;
    632 }
    633 
    634 /*
    635  * IPsec output logic for IPv6, tunnel mode.
    636  */
    637 int
    638 ipsec6_output_tunnel(struct ipsec_output_state *state, struct secpolicy *sp, int flags)
    639 {
    640 	struct ip6_hdr *ip6;
    641 	struct ipsecrequest *isr;
    642 	struct secasindex saidx;
    643 	int error;
    644 	struct sockaddr_in6* dst6;
    645 	struct mbuf *m;
    646 
    647 	IPSEC_ASSERT(state != NULL, ("ipsec6_output: null state"));
    648 	IPSEC_ASSERT(state->m != NULL, ("ipsec6_output: null m"));
    649 	IPSEC_ASSERT(sp != NULL, ("ipsec6_output: null sp"));
    650 
    651 	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
    652 		printf("ipsec6_output_tunnel: applyed SP\n");
    653 		kdebug_secpolicy(sp));
    654 
    655 	m = state->m;
    656 	/*
    657 	 * transport mode ipsec (before the 1st tunnel mode) is already
    658 	 * processed by ipsec6_output_trans().
    659 	 */
    660 	for (isr = sp->req; isr; isr = isr->next) {
    661 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
    662 			break;
    663 	}
    664 	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
    665 	if (isr == NULL)
    666 		goto bad;
    667 
    668 	/*
    669 	 * There may be the case that SA status will be changed when
    670 	 * we are refering to one. So calling splsoftnet().
    671 	 */
    672 	if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
    673 		/*
    674 		 * build IPsec tunnel.
    675 		 */
    676 		/* XXX should be processed with other familiy */
    677 		if (isr->sav->sah->saidx.src.sa.sa_family != AF_INET6) {
    678 			ipseclog((LOG_ERR, "ipsec6_output_tunnel: "
    679 			    "family mismatched between inner and outer, spi=%lu\n",
    680 			    ntohl(isr->sav->spi)));
    681 			newipsecstat.ips_out_inval++;
    682 			error = EAFNOSUPPORT;
    683 			goto bad;
    684 		}
    685 
    686 		m = ipsec6_splithdr(m);
    687 		if (!m) {
    688 			newipsecstat.ips_out_nomem++;
    689 			error = ENOMEM;
    690 			goto bad;
    691 		}
    692 		error = ipsec6_encapsulate(m, isr->sav);
    693 		if (error) {
    694 			m = NULL;
    695 			goto bad;
    696 		}
    697 		ip6 = mtod(m, struct ip6_hdr *);
    698 
    699 		state->ro = &isr->sav->sah->sa_route;
    700 		state->dst = (struct sockaddr *)&state->ro->ro_dst;
    701 		dst6 = (struct sockaddr_in6 *)state->dst;
    702 		if (state->ro->ro_rt
    703 		 && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0
    704 		  || !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_dst))) {
    705 			RTFREE(state->ro->ro_rt);
    706 			state->ro->ro_rt = NULL;
    707 		}
    708 		if (state->ro->ro_rt == 0) {
    709 			bzero(dst6, sizeof(*dst6));
    710 			dst6->sin6_family = AF_INET6;
    711 			dst6->sin6_len = sizeof(*dst6);
    712 			dst6->sin6_addr = ip6->ip6_dst;
    713 			rtalloc(state->ro);
    714 		}
    715 		if (state->ro->ro_rt == 0) {
    716 			ip6stat.ip6s_noroute++;
    717 			newipsecstat.ips_out_noroute++;
    718 			error = EHOSTUNREACH;
    719 			goto bad;
    720 		}
    721 
    722 		/* adjust state->dst if tunnel endpoint is offlink */
    723 		if (state->ro->ro_rt->rt_flags & RTF_GATEWAY) {
    724 			state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway;
    725 			dst6 = (struct sockaddr_in6 *)state->dst;
    726 		}
    727 	}
    728 
    729 	m = ipsec6_splithdr(m);
    730 	if (!m) {
    731 		newipsecstat.ips_out_nomem++;
    732 		error = ENOMEM;
    733 		goto bad;
    734 	}
    735 	ip6 = mtod(m, struct ip6_hdr *);
    736 	return (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
    737 		sizeof (struct ip6_hdr),
    738 		offsetof(struct ip6_hdr, ip6_nxt));
    739 bad:
    740 	if (m)
    741 		m_freem(m);
    742 	state->m = NULL;
    743 	return error;
    744 }
    745 #endif /*INET6*/
    746