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