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