xref: /freebsd/sys/netipsec/ipsec_output.c (revision bd1da0a002e9a43cfb5220835c7a42804d90dc56)
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 #include <netinet6/scope6_var.h>
65 #endif
66 #include <netinet/in_pcb.h>
67 #ifdef INET6
68 #include <netinet/icmp6.h>
69 #endif
70 
71 #include <netipsec/ipsec.h>
72 #ifdef INET6
73 #include <netipsec/ipsec6.h>
74 #endif
75 #include <netipsec/ah_var.h>
76 #include <netipsec/esp_var.h>
77 #include <netipsec/ipcomp_var.h>
78 
79 #include <netipsec/xform.h>
80 
81 #include <netipsec/key.h>
82 #include <netipsec/keydb.h>
83 #include <netipsec/key_debug.h>
84 
85 #include <machine/in_cksum.h>
86 
87 #ifdef IPSEC_NAT_T
88 #include <netinet/udp.h>
89 #endif
90 
91 #ifdef DEV_ENC
92 #include <net/if_enc.h>
93 #endif
94 
95 
96 int
97 ipsec_process_done(struct mbuf *m, struct ipsecrequest *isr)
98 {
99 	struct tdb_ident *tdbi;
100 	struct m_tag *mtag;
101 	struct secasvar *sav;
102 	struct secasindex *saidx;
103 	int error;
104 
105 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
106 	IPSEC_ASSERT(isr != NULL, ("null ISR"));
107 	IPSEC_ASSERT(isr->sp != NULL, ("NULL isr->sp"));
108 	sav = isr->sav;
109 	IPSEC_ASSERT(sav != NULL, ("null SA"));
110 	IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
111 
112 	saidx = &sav->sah->saidx;
113 	switch (saidx->dst.sa.sa_family) {
114 #ifdef INET
115 	case AF_INET:
116 		/* Fix the header length, for AH processing. */
117 		mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
118 		break;
119 #endif /* INET */
120 #ifdef INET6
121 	case AF_INET6:
122 		/* Fix the header length, for AH processing. */
123 		if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
124 			error = ENXIO;
125 			goto bad;
126 		}
127 		if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
128 			/* No jumbogram support. */
129 			error = ENXIO;	/*?*/
130 			goto bad;
131 		}
132 		mtod(m, struct ip6_hdr *)->ip6_plen =
133 			htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
134 		break;
135 #endif /* INET6 */
136 	default:
137 		DPRINTF(("%s: unknown protocol family %u\n", __func__,
138 		    saidx->dst.sa.sa_family));
139 		error = ENXIO;
140 		goto bad;
141 	}
142 
143 	/*
144 	 * Add a record of what we've done or what needs to be done to the
145 	 * packet.
146 	 */
147 	mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE,
148 			sizeof(struct tdb_ident), M_NOWAIT);
149 	if (mtag == NULL) {
150 		DPRINTF(("%s: could not get packet tag\n", __func__));
151 		error = ENOMEM;
152 		goto bad;
153 	}
154 
155 	tdbi = (struct tdb_ident *)(mtag + 1);
156 	tdbi->dst = saidx->dst;
157 	tdbi->proto = saidx->proto;
158 	tdbi->spi = sav->spi;
159 	m_tag_prepend(m, mtag);
160 
161 	/*
162 	 * If there's another (bundled) SA to apply, do so.
163 	 * Note that this puts a burden on the kernel stack size.
164 	 * If this is a problem we'll need to introduce a queue
165 	 * to set the packet on so we can unwind the stack before
166 	 * doing further processing.
167 	 *
168 	 * If ipsec[46]_process_packet() will successfully queue
169 	 * the request, we need to take additional reference to SP,
170 	 * because xform callback will release reference.
171 	 */
172 	if (isr->next) {
173 		/* XXX-BZ currently only support same AF bundles. */
174 		switch (saidx->dst.sa.sa_family) {
175 #ifdef INET
176 		case AF_INET:
177 			IPSECSTAT_INC(ips_out_bundlesa);
178 			key_addref(isr->sp);
179 			error = ipsec4_process_packet(m, isr->next);
180 			if (error != 0)
181 				KEY_FREESP(&isr->sp);
182 			return (error);
183 			/* NOTREACHED */
184 #endif
185 #ifdef notyet
186 #ifdef INET6
187 		case AF_INET6:
188 			/* XXX */
189 			IPSEC6STAT_INC(ips_out_bundlesa);
190 			key_addref(isr->sp);
191 			error = ipsec6_process_packet(m, isr->next);
192 			if (error != 0)
193 				KEY_FREESP(&isr->sp);
194 			return (error);
195 			/* NOTREACHED */
196 #endif /* INET6 */
197 #endif
198 		default:
199 			DPRINTF(("%s: unknown protocol family %u\n", __func__,
200 			    saidx->dst.sa.sa_family));
201 			error = ENXIO;
202 			goto bad;
203 		}
204 	}
205 	key_sa_recordxfer(sav, m);		/* record data transfer */
206 
207 	/*
208 	 * We're done with IPsec processing, transmit the packet using the
209 	 * appropriate network protocol (IP or IPv6).
210 	 */
211 	switch (saidx->dst.sa.sa_family) {
212 #ifdef INET
213 	case AF_INET:
214 #ifdef IPSEC_NAT_T
215 		/*
216 		 * If NAT-T is enabled, now that all IPsec processing is done
217 		 * insert UDP encapsulation header after IP header.
218 		 */
219 		if (sav->natt_type) {
220 			struct ip *ip = mtod(m, struct ip *);
221 			const int hlen = (ip->ip_hl << 2);
222 			int size, off;
223 			struct mbuf *mi;
224 			struct udphdr *udp;
225 
226 			size = sizeof(struct udphdr);
227 			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) {
228 				/*
229 				 * draft-ietf-ipsec-nat-t-ike-0[01].txt and
230 				 * draft-ietf-ipsec-udp-encaps-(00/)01.txt,
231 				 * ignoring possible AH mode
232 				 * non-IKE marker + non-ESP marker
233 				 * from draft-ietf-ipsec-udp-encaps-00.txt.
234 				 */
235 				size += sizeof(u_int64_t);
236 			}
237 			mi = m_makespace(m, hlen, size, &off);
238 			if (mi == NULL) {
239 				DPRINTF(("%s: m_makespace for udphdr failed\n",
240 				    __func__));
241 				error = ENOBUFS;
242 				goto bad;
243 			}
244 
245 			udp = (struct udphdr *)(mtod(mi, caddr_t) + off);
246 			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
247 				udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT);
248 			else
249 				udp->uh_sport =
250 					KEY_PORTFROMSADDR(&sav->sah->saidx.src);
251 			udp->uh_dport = KEY_PORTFROMSADDR(&sav->sah->saidx.dst);
252 			udp->uh_sum = 0;
253 			udp->uh_ulen = htons(m->m_pkthdr.len - hlen);
254 			ip->ip_len = htons(m->m_pkthdr.len);
255 			ip->ip_p = IPPROTO_UDP;
256 
257 			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
258 				*(u_int64_t *)(udp + 1) = 0;
259 		}
260 #endif /* IPSEC_NAT_T */
261 
262 		return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
263 #endif /* INET */
264 #ifdef INET6
265 	case AF_INET6:
266 		/*
267 		 * We don't need massage, IPv6 header fields are always in
268 		 * net endian.
269 		 */
270 		return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
271 #endif /* INET6 */
272 	}
273 	panic("ipsec_process_done");
274 bad:
275 	m_freem(m);
276 	return (error);
277 }
278 
279 static struct ipsecrequest *
280 ipsec_nextisr(
281 	struct mbuf *m,
282 	struct ipsecrequest *isr,
283 	int af,
284 	struct secasindex *saidx,
285 	int *error
286 )
287 {
288 #define	IPSEC_OSTAT(name)	do {		\
289 	if (isr->saidx.proto == IPPROTO_ESP)	\
290 		ESPSTAT_INC(esps_##name);	\
291 	else if (isr->saidx.proto == IPPROTO_AH)\
292 		AHSTAT_INC(ahs_##name);		\
293 	else					\
294 		IPCOMPSTAT_INC(ipcomps_##name);	\
295 } while (0)
296 	struct secasvar *sav;
297 
298 	IPSECREQUEST_LOCK_ASSERT(isr);
299 
300 	IPSEC_ASSERT(af == AF_INET || af == AF_INET6,
301 		("invalid address family %u", af));
302 again:
303 	/*
304 	 * Craft SA index to search for proper SA.  Note that
305 	 * we only fillin unspecified SA peers for transport
306 	 * mode; for tunnel mode they must already be filled in.
307 	 */
308 	*saidx = isr->saidx;
309 	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
310 		/* Fillin unspecified SA peers only for transport mode */
311 		if (af == AF_INET) {
312 			struct sockaddr_in *sin;
313 			struct ip *ip = mtod(m, struct ip *);
314 
315 			if (saidx->src.sa.sa_len == 0) {
316 				sin = &saidx->src.sin;
317 				sin->sin_len = sizeof(*sin);
318 				sin->sin_family = AF_INET;
319 				sin->sin_port = IPSEC_PORT_ANY;
320 				sin->sin_addr = ip->ip_src;
321 			}
322 			if (saidx->dst.sa.sa_len == 0) {
323 				sin = &saidx->dst.sin;
324 				sin->sin_len = sizeof(*sin);
325 				sin->sin_family = AF_INET;
326 				sin->sin_port = IPSEC_PORT_ANY;
327 				sin->sin_addr = ip->ip_dst;
328 			}
329 		} else {
330 			struct sockaddr_in6 *sin6;
331 			struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
332 
333 			if (saidx->src.sin6.sin6_len == 0) {
334 				sin6 = (struct sockaddr_in6 *)&saidx->src;
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_src;
339 				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
340 					/* fix scope id for comparing SPD */
341 					sin6->sin6_addr.s6_addr16[1] = 0;
342 					sin6->sin6_scope_id =
343 					    ntohs(ip6->ip6_src.s6_addr16[1]);
344 				}
345 			}
346 			if (saidx->dst.sin6.sin6_len == 0) {
347 				sin6 = (struct sockaddr_in6 *)&saidx->dst;
348 				sin6->sin6_len = sizeof(*sin6);
349 				sin6->sin6_family = AF_INET6;
350 				sin6->sin6_port = IPSEC_PORT_ANY;
351 				sin6->sin6_addr = ip6->ip6_dst;
352 				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
353 					/* fix scope id for comparing SPD */
354 					sin6->sin6_addr.s6_addr16[1] = 0;
355 					sin6->sin6_scope_id =
356 					    ntohs(ip6->ip6_dst.s6_addr16[1]);
357 				}
358 			}
359 		}
360 	}
361 
362 	/*
363 	 * Lookup SA and validate it.
364 	 */
365 	*error = key_checkrequest(isr, saidx);
366 	if (*error != 0) {
367 		/*
368 		 * IPsec processing is required, but no SA found.
369 		 * I assume that key_acquire() had been called
370 		 * to get/establish the SA. Here I discard
371 		 * this packet because it is responsibility for
372 		 * upper layer to retransmit the packet.
373 		 */
374 		switch(af) {
375 		case AF_INET:
376 			IPSECSTAT_INC(ips_out_nosa);
377 			break;
378 #ifdef INET6
379 		case AF_INET6:
380 			IPSEC6STAT_INC(ips_out_nosa);
381 			break;
382 #endif
383 		}
384 		goto bad;
385 	}
386 	sav = isr->sav;
387 	if (sav == NULL) {
388 		IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
389 			("no SA found, but required; level %u",
390 			ipsec_get_reqlevel(isr)));
391 		IPSECREQUEST_UNLOCK(isr);
392 		isr = isr->next;
393 		/*
394 		 * If isr is NULL, we found a 'use' policy w/o SA.
395 		 * Return w/o error and w/o isr so we can drop out
396 		 * and continue w/o IPsec processing.
397 		 */
398 		if (isr == NULL)
399 			return isr;
400 		IPSECREQUEST_LOCK(isr);
401 		goto again;
402 	}
403 
404 	/*
405 	 * Check system global policy controls.
406 	 */
407 	if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
408 	    (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
409 	    (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
410 		DPRINTF(("%s: IPsec outbound packet dropped due"
411 			" to policy (check your sysctls)\n", __func__));
412 		IPSEC_OSTAT(pdrops);
413 		*error = EHOSTUNREACH;
414 		goto bad;
415 	}
416 
417 	/*
418 	 * Sanity check the SA contents for the caller
419 	 * before they invoke the xform output method.
420 	 */
421 	if (sav->tdb_xform == NULL) {
422 		DPRINTF(("%s: no transform for SA\n", __func__));
423 		IPSEC_OSTAT(noxform);
424 		*error = EHOSTUNREACH;
425 		goto bad;
426 	}
427 	return isr;
428 bad:
429 	IPSEC_ASSERT(*error != 0, ("error return w/ no error code"));
430 	IPSECREQUEST_UNLOCK(isr);
431 	return NULL;
432 #undef IPSEC_OSTAT
433 }
434 
435 static int
436 ipsec_encap(struct mbuf **mp, struct secasindex *saidx)
437 {
438 #ifdef INET6
439 	struct ip6_hdr *ip6;
440 #endif
441 	struct ip *ip;
442 	int setdf;
443 	uint8_t itos, proto;
444 
445 	ip = mtod(*mp, struct ip *);
446 	switch (ip->ip_v) {
447 #ifdef INET
448 	case IPVERSION:
449 		proto = IPPROTO_IPIP;
450 		/*
451 		 * Collect IP_DF state from the inner header
452 		 * and honor system-wide control of how to handle it.
453 		 */
454 		switch (V_ip4_ipsec_dfbit) {
455 		case 0:	/* clear in outer header */
456 		case 1:	/* set in outer header */
457 			setdf = V_ip4_ipsec_dfbit;
458 			break;
459 		default:/* propagate to outer header */
460 			setdf = (ip->ip_off & ntohs(IP_DF)) != 0;
461 		}
462 		itos = ip->ip_tos;
463 		break;
464 #endif
465 #ifdef INET6
466 	case (IPV6_VERSION >> 4):
467 		proto = IPPROTO_IPV6;
468 		ip6 = mtod(*mp, struct ip6_hdr *);
469 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
470 		setdf = V_ip4_ipsec_dfbit ? 1: 0;
471 		/* scoped address handling */
472 		in6_clearscope(&ip6->ip6_src);
473 		in6_clearscope(&ip6->ip6_dst);
474 		break;
475 #endif
476 	default:
477 		return (EAFNOSUPPORT);
478 	}
479 	switch (saidx->dst.sa.sa_family) {
480 #ifdef INET
481 	case AF_INET:
482 		if (saidx->src.sa.sa_family != AF_INET ||
483 		    saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
484 		    saidx->dst.sin.sin_addr.s_addr == INADDR_ANY)
485 			return (EINVAL);
486 		M_PREPEND(*mp, sizeof(struct ip), M_NOWAIT);
487 		if (*mp == NULL)
488 			return (ENOBUFS);
489 		ip = mtod(*mp, struct ip *);
490 		ip->ip_v = IPVERSION;
491 		ip->ip_hl = sizeof(struct ip) >> 2;
492 		ip->ip_p = proto;
493 		ip->ip_len = htons((*mp)->m_pkthdr.len);
494 		ip->ip_ttl = V_ip_defttl;
495 		ip->ip_sum = 0;
496 		ip->ip_off = setdf ? htons(IP_DF): 0;
497 		ip->ip_src = saidx->src.sin.sin_addr;
498 		ip->ip_dst = saidx->dst.sin.sin_addr;
499 		ip_ecn_ingress(V_ip4_ipsec_ecn, &ip->ip_tos, &itos);
500 		ip_fillid(ip);
501 		break;
502 #endif /* INET */
503 #ifdef INET6
504 	case AF_INET6:
505 		if (saidx->src.sa.sa_family != AF_INET6 ||
506 		    IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr) ||
507 		    IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr))
508 			return (EINVAL);
509 		M_PREPEND(*mp, sizeof(struct ip6_hdr), M_NOWAIT);
510 		if (*mp == NULL)
511 			return (ENOBUFS);
512 		ip6 = mtod(*mp, struct ip6_hdr *);
513 		ip6->ip6_flow = 0;
514 		ip6->ip6_vfc = IPV6_VERSION;
515 		ip6->ip6_hlim = V_ip6_defhlim;
516 		ip6->ip6_nxt = proto;
517 		ip6->ip6_dst = saidx->dst.sin6.sin6_addr;
518 		/* For link-local address embed scope zone id */
519 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
520 			ip6->ip6_dst.s6_addr16[1] =
521 			    htons(saidx->dst.sin6.sin6_scope_id & 0xffff);
522 		ip6->ip6_src = saidx->src.sin6.sin6_addr;
523 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
524 			ip6->ip6_src.s6_addr16[1] =
525 			    htons(saidx->src.sin6.sin6_scope_id & 0xffff);
526 		ip6->ip6_plen = htons((*mp)->m_pkthdr.len - sizeof(*ip6));
527 		ip_ecn_ingress(V_ip6_ipsec_ecn, &proto, &itos);
528 		ip6->ip6_flow |= htonl((uint32_t)proto << 20);
529 		break;
530 #endif /* INET6 */
531 	default:
532 		return (EAFNOSUPPORT);
533 	}
534 	return (0);
535 }
536 
537 #ifdef INET
538 /*
539  * IPsec output logic for IPv4.
540  */
541 int
542 ipsec4_process_packet(struct mbuf *m, struct ipsecrequest *isr)
543 {
544 	char sbuf[INET6_ADDRSTRLEN], dbuf[INET6_ADDRSTRLEN];
545 	union sockaddr_union *dst;
546 	struct secasindex saidx;
547 	struct secasvar *sav;
548 	struct ip *ip;
549 	int error, i, off;
550 
551 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
552 	IPSEC_ASSERT(isr != NULL, ("null isr"));
553 
554 	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
555 
556 	isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
557 	if (isr == NULL) {
558 		if (error != 0)
559 			goto bad;
560 		return EJUSTRETURN;
561 	}
562 
563 	sav = isr->sav;
564 	if (m->m_len < sizeof(struct ip) &&
565 	    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
566 		error = ENOBUFS;
567 		goto bad;
568 	}
569 	ip = mtod(m, struct ip *);
570 	dst = &sav->sah->saidx.dst;
571 #ifdef DEV_ENC
572 	if_inc_counter(encif, IFCOUNTER_OPACKETS, 1);
573 	if_inc_counter(encif, IFCOUNTER_OBYTES, m->m_pkthdr.len);
574 
575 	/* pass the mbuf to enc0 for bpf processing */
576 	ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_BEFORE);
577 	/* pass the mbuf to enc0 for packet filtering */
578 	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
579 		goto bad;
580 	ip = mtod(m, struct ip *);
581 #endif
582 	/* Do the appropriate encapsulation, if necessary */
583 	if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
584 	    dst->sa.sa_family != AF_INET ||	    /* PF mismatch */
585 	    (dst->sa.sa_family == AF_INET &&	    /* Proxy */
586 	     dst->sin.sin_addr.s_addr != INADDR_ANY &&
587 	     dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
588 		/* Fix IPv4 header checksum and length */
589 		ip->ip_len = htons(m->m_pkthdr.len);
590 		ip->ip_sum = 0;
591 		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
592 		error = ipsec_encap(&m, &sav->sah->saidx);
593 		if (error != 0) {
594 			DPRINTF(("%s: encapsulation for SA %s->%s "
595 			    "SPI 0x%08x failed with error %d\n", __func__,
596 			    ipsec_address(&sav->sah->saidx.src, sbuf,
597 				sizeof(sbuf)),
598 			    ipsec_address(&sav->sah->saidx.dst, dbuf,
599 				sizeof(dbuf)), ntohl(sav->spi), error));
600 			goto bad;
601 		}
602 	}
603 #ifdef DEV_ENC
604 	/* pass the mbuf to enc0 for bpf processing */
605 	ipsec_bpf(m, sav, sav->sah->saidx.dst.sa.sa_family, ENC_OUT|ENC_AFTER);
606 	/* pass the mbuf to enc0 for packet filtering */
607 	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
608 		goto bad;
609 #endif
610 
611 	/*
612 	 * Dispatch to the appropriate IPsec transform logic.  The
613 	 * packet will be returned for transmission after crypto
614 	 * processing, etc. are completed.
615 	 *
616 	 * NB: m & sav are ``passed to caller'' who's reponsible for
617 	 *     for reclaiming their resources.
618 	 */
619 	switch(dst->sa.sa_family) {
620 	case AF_INET:
621 		ip = mtod(m, struct ip *);
622 		i = ip->ip_hl << 2;
623 		off = offsetof(struct ip, ip_p);
624 		break;
625 #ifdef INET6
626 	case AF_INET6:
627 		i = sizeof(struct ip6_hdr);
628 		off = offsetof(struct ip6_hdr, ip6_nxt);
629 		break;
630 #endif /* INET6 */
631 	default:
632 		DPRINTF(("%s: unsupported protocol family %u\n",
633 		    __func__, dst->sa.sa_family));
634 		error = EPFNOSUPPORT;
635 		IPSECSTAT_INC(ips_out_inval);
636 		goto bad;
637 	}
638 	error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
639 	IPSECREQUEST_UNLOCK(isr);
640 	return (error);
641 bad:
642 	if (isr)
643 		IPSECREQUEST_UNLOCK(isr);
644 	if (m)
645 		m_freem(m);
646 	return error;
647 }
648 #endif
649 
650 
651 #ifdef INET6
652 static int
653 in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa, const struct in6_addr *ia)
654 {
655 	struct in6_addr ia2;
656 
657 	memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
658 	if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr))
659 		ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
660 
661 	return IN6_ARE_ADDR_EQUAL(ia, &ia2);
662 }
663 
664 /*
665  * IPsec output logic for IPv6.
666  */
667 int
668 ipsec6_process_packet(struct mbuf *m, struct ipsecrequest *isr)
669 {
670 	char sbuf[INET6_ADDRSTRLEN], dbuf[INET6_ADDRSTRLEN];
671 	struct secasindex saidx;
672 	struct secasvar *sav;
673 	struct ip6_hdr *ip6;
674 	int error, i, off;
675 	union sockaddr_union *dst;
676 
677 	IPSEC_ASSERT(m != NULL, ("ipsec6_process_packet: null mbuf"));
678 	IPSEC_ASSERT(isr != NULL, ("ipsec6_process_packet: null isr"));
679 
680 	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
681 
682 	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
683 	if (isr == NULL) {
684 		if (error != 0)
685 			goto bad;
686 		return EJUSTRETURN;
687 	}
688 	sav = isr->sav;
689 	dst = &sav->sah->saidx.dst;
690 
691 	ip6 = mtod(m, struct ip6_hdr *);
692 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
693 #ifdef DEV_ENC
694 	if_inc_counter(encif, IFCOUNTER_OPACKETS, 1);
695 	if_inc_counter(encif, IFCOUNTER_OBYTES, m->m_pkthdr.len);
696 
697 	/* pass the mbuf to enc0 for bpf processing */
698 	ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_BEFORE);
699 	/* pass the mbuf to enc0 for packet filtering */
700 	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
701 		goto bad;
702 	ip6 = mtod(m, struct ip6_hdr *);
703 #endif /* DEV_ENC */
704 
705 	/* Do the appropriate encapsulation, if necessary */
706 	if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
707 	    dst->sa.sa_family != AF_INET6 ||        /* PF mismatch */
708 	    ((dst->sa.sa_family == AF_INET6) &&
709 	     (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
710 	     (!in6_sa_equal_addrwithscope(&dst->sin6,
711 				  &ip6->ip6_dst)))) {
712 		if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
713 			/* No jumbogram support. */
714 			error = ENXIO;   /*XXX*/
715 			goto bad;
716 		}
717 		error = ipsec_encap(&m, &sav->sah->saidx);
718 		if (error != 0) {
719 			DPRINTF(("%s: encapsulation for SA %s->%s "
720 			    "SPI 0x%08x failed with error %d\n", __func__,
721 			    ipsec_address(&sav->sah->saidx.src, sbuf,
722 				sizeof(sbuf)),
723 			    ipsec_address(&sav->sah->saidx.dst, dbuf,
724 				sizeof(dbuf)), ntohl(sav->spi), error));
725 			goto bad;
726 		}
727 	}
728 
729 #ifdef DEV_ENC
730 	ipsec_bpf(m, isr->sav, dst->sa.sa_family, ENC_OUT|ENC_AFTER);
731 	/* pass the mbuf to enc0 for packet filtering */
732 	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
733 		goto bad;
734 #endif /* DEV_ENC */
735 
736 	switch(dst->sa.sa_family) {
737 #ifdef INET
738 	case AF_INET:
739 		{
740 		struct ip *ip;
741 		ip = mtod(m, struct ip *);
742 		i = ip->ip_hl << 2;
743 		off = offsetof(struct ip, ip_p);
744 		}
745 		break;
746 #endif /* AF_INET */
747 	case AF_INET6:
748 		i = sizeof(struct ip6_hdr);
749 		off = offsetof(struct ip6_hdr, ip6_nxt);
750 		break;
751 	default:
752 		DPRINTF(("%s: unsupported protocol family %u\n",
753 				 __func__, dst->sa.sa_family));
754 		error = EPFNOSUPPORT;
755 		IPSEC6STAT_INC(ips_out_inval);
756 		goto bad;
757 	}
758 	error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
759 	IPSECREQUEST_UNLOCK(isr);
760 	return error;
761 bad:
762 
763 	if (isr)
764 		IPSECREQUEST_UNLOCK(isr);
765 	if (m)
766 		m_freem(m);
767 	return error;
768 }
769 #endif /*INET6*/
770