ipsec_output.c revision 274132
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: stable/10/sys/netipsec/ipsec_output.c 274132 2014-11-05 09:23:29Z ae $
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/pfil.h>
48#include <net/route.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
95int
96ipsec_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		IPSECSTAT_INC(ips_out_bundlesa);
168		/* XXX-BZ currently only support same AF bundles. */
169		switch (saidx->dst.sa.sa_family) {
170#ifdef INET
171		case AF_INET:
172			return ipsec4_process_packet(m, isr->next, 0, 0);
173			/* NOTREACHED */
174#endif
175#ifdef notyet
176#ifdef INET6
177		case AF_INET6:
178			/* XXX */
179			return ipsec6_process_packet(m, isr->next);
180			/* NOTREACHED */
181#endif /* INET6 */
182#endif
183		default:
184			DPRINTF(("%s: unknown protocol family %u\n", __func__,
185			    saidx->dst.sa.sa_family));
186			error = ENXIO;
187			goto bad;
188		}
189	}
190	key_sa_recordxfer(sav, m);		/* record data transfer */
191
192	/*
193	 * We're done with IPsec processing, transmit the packet using the
194	 * appropriate network protocol (IP or IPv6). SPD lookup will be
195	 * performed again there.
196	 */
197	switch (saidx->dst.sa.sa_family) {
198#ifdef INET
199	case AF_INET:
200#ifdef IPSEC_NAT_T
201		/*
202		 * If NAT-T is enabled, now that all IPsec processing is done
203		 * insert UDP encapsulation header after IP header.
204		 */
205		if (sav->natt_type) {
206			struct ip *ip = mtod(m, struct ip *);
207			const int hlen = (ip->ip_hl << 2);
208			int size, off;
209			struct mbuf *mi;
210			struct udphdr *udp;
211
212			size = sizeof(struct udphdr);
213			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) {
214				/*
215				 * draft-ietf-ipsec-nat-t-ike-0[01].txt and
216				 * draft-ietf-ipsec-udp-encaps-(00/)01.txt,
217				 * ignoring possible AH mode
218				 * non-IKE marker + non-ESP marker
219				 * from draft-ietf-ipsec-udp-encaps-00.txt.
220				 */
221				size += sizeof(u_int64_t);
222			}
223			mi = m_makespace(m, hlen, size, &off);
224			if (mi == NULL) {
225				DPRINTF(("%s: m_makespace for udphdr failed\n",
226				    __func__));
227				error = ENOBUFS;
228				goto bad;
229			}
230
231			udp = (struct udphdr *)(mtod(mi, caddr_t) + off);
232			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
233				udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT);
234			else
235				udp->uh_sport =
236					KEY_PORTFROMSADDR(&sav->sah->saidx.src);
237			udp->uh_dport = KEY_PORTFROMSADDR(&sav->sah->saidx.dst);
238			udp->uh_sum = 0;
239			udp->uh_ulen = htons(m->m_pkthdr.len - hlen);
240			ip->ip_len = htons(m->m_pkthdr.len);
241			ip->ip_p = IPPROTO_UDP;
242
243			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
244				*(u_int64_t *)(udp + 1) = 0;
245		}
246#endif /* IPSEC_NAT_T */
247
248		return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
249#endif /* INET */
250#ifdef INET6
251	case AF_INET6:
252		/*
253		 * We don't need massage, IPv6 header fields are always in
254		 * net endian.
255		 */
256		return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
257#endif /* INET6 */
258	}
259	panic("ipsec_process_done");
260bad:
261	m_freem(m);
262	return (error);
263}
264
265static struct ipsecrequest *
266ipsec_nextisr(
267	struct mbuf *m,
268	struct ipsecrequest *isr,
269	int af,
270	struct secasindex *saidx,
271	int *error
272)
273{
274#define	IPSEC_OSTAT(name)	do {		\
275	if (isr->saidx.proto == IPPROTO_ESP)	\
276		ESPSTAT_INC(esps_##name);	\
277	else if (isr->saidx.proto == IPPROTO_AH)\
278		AHSTAT_INC(ahs_##name);		\
279	else					\
280		IPCOMPSTAT_INC(ipcomps_##name);	\
281} while (0)
282	struct secasvar *sav;
283
284	IPSECREQUEST_LOCK_ASSERT(isr);
285
286	IPSEC_ASSERT(af == AF_INET || af == AF_INET6,
287		("invalid address family %u", af));
288again:
289	/*
290	 * Craft SA index to search for proper SA.  Note that
291	 * we only fillin unspecified SA peers for transport
292	 * mode; for tunnel mode they must already be filled in.
293	 */
294	*saidx = isr->saidx;
295	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
296		/* Fillin unspecified SA peers only for transport mode */
297		if (af == AF_INET) {
298			struct sockaddr_in *sin;
299			struct ip *ip = mtod(m, struct ip *);
300
301			if (saidx->src.sa.sa_len == 0) {
302				sin = &saidx->src.sin;
303				sin->sin_len = sizeof(*sin);
304				sin->sin_family = AF_INET;
305				sin->sin_port = IPSEC_PORT_ANY;
306				sin->sin_addr = ip->ip_src;
307			}
308			if (saidx->dst.sa.sa_len == 0) {
309				sin = &saidx->dst.sin;
310				sin->sin_len = sizeof(*sin);
311				sin->sin_family = AF_INET;
312				sin->sin_port = IPSEC_PORT_ANY;
313				sin->sin_addr = ip->ip_dst;
314			}
315		} else {
316			struct sockaddr_in6 *sin6;
317			struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
318
319			if (saidx->src.sin6.sin6_len == 0) {
320				sin6 = (struct sockaddr_in6 *)&saidx->src;
321				sin6->sin6_len = sizeof(*sin6);
322				sin6->sin6_family = AF_INET6;
323				sin6->sin6_port = IPSEC_PORT_ANY;
324				sin6->sin6_addr = ip6->ip6_src;
325				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
326					/* fix scope id for comparing SPD */
327					sin6->sin6_addr.s6_addr16[1] = 0;
328					sin6->sin6_scope_id =
329					    ntohs(ip6->ip6_src.s6_addr16[1]);
330				}
331			}
332			if (saidx->dst.sin6.sin6_len == 0) {
333				sin6 = (struct sockaddr_in6 *)&saidx->dst;
334				sin6->sin6_len = sizeof(*sin6);
335				sin6->sin6_family = AF_INET6;
336				sin6->sin6_port = IPSEC_PORT_ANY;
337				sin6->sin6_addr = ip6->ip6_dst;
338				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
339					/* fix scope id for comparing SPD */
340					sin6->sin6_addr.s6_addr16[1] = 0;
341					sin6->sin6_scope_id =
342					    ntohs(ip6->ip6_dst.s6_addr16[1]);
343				}
344			}
345		}
346	}
347
348	/*
349	 * Lookup SA and validate it.
350	 */
351	*error = key_checkrequest(isr, saidx);
352	if (*error != 0) {
353		/*
354		 * IPsec processing is required, but no SA found.
355		 * I assume that key_acquire() had been called
356		 * to get/establish the SA. Here I discard
357		 * this packet because it is responsibility for
358		 * upper layer to retransmit the packet.
359		 */
360		IPSECSTAT_INC(ips_out_nosa);
361		goto bad;
362	}
363	sav = isr->sav;
364	if (sav == NULL) {
365		IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
366			("no SA found, but required; level %u",
367			ipsec_get_reqlevel(isr)));
368		IPSECREQUEST_UNLOCK(isr);
369		isr = isr->next;
370		/*
371		 * If isr is NULL, we found a 'use' policy w/o SA.
372		 * Return w/o error and w/o isr so we can drop out
373		 * and continue w/o IPsec processing.
374		 */
375		if (isr == NULL)
376			return isr;
377		IPSECREQUEST_LOCK(isr);
378		goto again;
379	}
380
381	/*
382	 * Check system global policy controls.
383	 */
384	if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
385	    (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
386	    (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
387		DPRINTF(("%s: IPsec outbound packet dropped due"
388			" to policy (check your sysctls)\n", __func__));
389		IPSEC_OSTAT(pdrops);
390		*error = EHOSTUNREACH;
391		goto bad;
392	}
393
394	/*
395	 * Sanity check the SA contents for the caller
396	 * before they invoke the xform output method.
397	 */
398	if (sav->tdb_xform == NULL) {
399		DPRINTF(("%s: no transform for SA\n", __func__));
400		IPSEC_OSTAT(noxform);
401		*error = EHOSTUNREACH;
402		goto bad;
403	}
404	return isr;
405bad:
406	IPSEC_ASSERT(*error != 0, ("error return w/ no error code"));
407	IPSECREQUEST_UNLOCK(isr);
408	return NULL;
409#undef IPSEC_OSTAT
410}
411
412#ifdef INET
413/*
414 * IPsec output logic for IPv4.
415 */
416int
417ipsec4_process_packet(
418	struct mbuf *m,
419	struct ipsecrequest *isr,
420	int flags,
421	int tunalready)
422{
423	struct secasindex saidx;
424	struct secasvar *sav;
425	struct ip *ip;
426	int error, i, off;
427
428	IPSEC_ASSERT(m != NULL, ("null mbuf"));
429	IPSEC_ASSERT(isr != NULL, ("null isr"));
430
431	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
432
433	isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
434	if (isr == NULL) {
435		if (error != 0)
436			goto bad;
437		return EJUSTRETURN;
438	}
439
440	sav = isr->sav;
441
442#ifdef DEV_ENC
443	encif->if_opackets++;
444	encif->if_obytes += m->m_pkthdr.len;
445
446	/* pass the mbuf to enc0 for bpf processing */
447	ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_BEFORE);
448	/* pass the mbuf to enc0 for packet filtering */
449	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
450		goto bad;
451#endif
452
453	if (!tunalready) {
454		union sockaddr_union *dst = &sav->sah->saidx.dst;
455		int setdf;
456
457		/*
458		 * Collect IP_DF state from the outer header.
459		 */
460		if (dst->sa.sa_family == AF_INET) {
461			if (m->m_len < sizeof (struct ip) &&
462			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
463				error = ENOBUFS;
464				goto bad;
465			}
466			ip = mtod(m, struct ip *);
467			/* Honor system-wide control of how to handle IP_DF */
468			switch (V_ip4_ipsec_dfbit) {
469			case 0:			/* clear in outer header */
470			case 1:			/* set in outer header */
471				setdf = V_ip4_ipsec_dfbit;
472				break;
473			default:		/* propagate to outer header */
474				setdf = ntohs(ip->ip_off & IP_DF);
475				break;
476			}
477		} else {
478			ip = NULL;		/* keep compiler happy */
479			setdf = 0;
480		}
481		/* Do the appropriate encapsulation, if necessary */
482		if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
483		    dst->sa.sa_family != AF_INET ||	    /* PF mismatch */
484#if 0
485		    (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
486		    sav->tdb_xform->xf_type == XF_IP4 ||    /* ditto */
487#endif
488		    (dst->sa.sa_family == AF_INET &&	    /* Proxy */
489		     dst->sin.sin_addr.s_addr != INADDR_ANY &&
490		     dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
491			struct mbuf *mp;
492
493			/* Fix IPv4 header checksum and length */
494			if (m->m_len < sizeof (struct ip) &&
495			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
496				error = ENOBUFS;
497				goto bad;
498			}
499			ip = mtod(m, struct ip *);
500			if (ip->ip_v == IPVERSION) {
501				ip->ip_len = htons(m->m_pkthdr.len);
502				ip->ip_sum = 0;
503				ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
504			}
505
506			/* Encapsulate the packet */
507			error = ipip_output(m, isr, &mp, 0, 0);
508			if (mp == NULL && !error) {
509				/* Should never happen. */
510				DPRINTF(("%s: ipip_output returns no mbuf and "
511					"no error!", __func__));
512				error = EFAULT;
513			}
514			if (error) {
515				if (mp) {
516					/* XXX: Should never happen! */
517					m_freem(mp);
518				}
519				m = NULL; /* ipip_output() already freed it */
520				goto bad;
521			}
522			m = mp, mp = NULL;
523			/*
524			 * ipip_output clears IP_DF in the new header.  If
525			 * we need to propagate IP_DF from the outer header,
526			 * then we have to do it here.
527			 *
528			 * XXX shouldn't assume what ipip_output does.
529			 */
530			if (dst->sa.sa_family == AF_INET && setdf) {
531				if (m->m_len < sizeof (struct ip) &&
532				    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
533					error = ENOBUFS;
534					goto bad;
535				}
536				ip = mtod(m, struct ip *);
537				ip->ip_off = ntohs(ip->ip_off);
538				ip->ip_off |= IP_DF;
539				ip->ip_off = htons(ip->ip_off);
540			}
541		}
542	}
543
544#ifdef DEV_ENC
545	/* pass the mbuf to enc0 for bpf processing */
546	ipsec_bpf(m, sav, sav->sah->saidx.dst.sa.sa_family, ENC_OUT|ENC_AFTER);
547	/* pass the mbuf to enc0 for packet filtering */
548	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
549		goto bad;
550#endif
551
552	/*
553	 * Dispatch to the appropriate IPsec transform logic.  The
554	 * packet will be returned for transmission after crypto
555	 * processing, etc. are completed.  For encapsulation we
556	 * bypass this call because of the explicit call done above
557	 * (necessary to deal with IP_DF handling for IPv4).
558	 *
559	 * NB: m & sav are ``passed to caller'' who's reponsible for
560	 *     for reclaiming their resources.
561	 */
562	if (sav->tdb_xform->xf_type != XF_IP4) {
563		union sockaddr_union *dst = &sav->sah->saidx.dst;
564		switch(dst->sa.sa_family) {
565		case AF_INET:
566			ip = mtod(m, struct ip *);
567			i = ip->ip_hl << 2;
568			off = offsetof(struct ip, ip_p);
569			break;
570#ifdef INET6
571		case AF_INET6:
572			i = sizeof(struct ip6_hdr);
573			off = offsetof(struct ip6_hdr, ip6_nxt);
574			break;
575#endif /* INET6 */
576		default:
577		DPRINTF(("%s: unsupported protocol family %u\n",
578				 __func__, dst->sa.sa_family));
579			error = EPFNOSUPPORT;
580			IPSECSTAT_INC(ips_out_inval);
581			goto bad;
582		}
583		error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
584	} else {
585		error = ipsec_process_done(m, isr);
586	}
587	IPSECREQUEST_UNLOCK(isr);
588	return error;
589bad:
590	if (isr)
591		IPSECREQUEST_UNLOCK(isr);
592	if (m)
593		m_freem(m);
594	return error;
595}
596#endif
597
598
599#ifdef INET6
600static int
601in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa, const struct in6_addr *ia)
602{
603	struct in6_addr ia2;
604
605	memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
606	if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr))
607		ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
608
609	return IN6_ARE_ADDR_EQUAL(ia, &ia2);
610}
611
612/*
613 * IPsec output logic for IPv6.
614 */
615int
616ipsec6_process_packet(
617	struct mbuf *m,
618 	struct ipsecrequest *isr
619    )
620{
621	struct secasindex saidx;
622	struct secasvar *sav;
623	struct ip6_hdr *ip6;
624	int error, i, off;
625	union sockaddr_union *dst;
626
627	IPSEC_ASSERT(m != NULL, ("ipsec6_process_packet: null mbuf"));
628	IPSEC_ASSERT(isr != NULL, ("ipsec6_process_packet: null isr"));
629
630	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
631
632	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
633	if (isr == NULL) {
634		if (error != 0)
635			goto bad;
636		return EJUSTRETURN;
637	}
638
639	sav = isr->sav;
640	dst = &sav->sah->saidx.dst;
641
642#ifdef DEV_ENC
643	encif->if_opackets++;
644	encif->if_obytes += m->m_pkthdr.len;
645
646	/* pass the mbuf to enc0 for bpf processing */
647	ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_BEFORE);
648	/* pass the mbuf to enc0 for packet filtering */
649	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
650		goto bad;
651#endif /* DEV_ENC */
652
653	ip6 = mtod(m, struct ip6_hdr *); /* XXX */
654
655	/* Do the appropriate encapsulation, if necessary */
656	if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
657	    dst->sa.sa_family != AF_INET6 ||        /* PF mismatch */
658	    ((dst->sa.sa_family == AF_INET6) &&
659	     (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
660	     (!in6_sa_equal_addrwithscope(&dst->sin6,
661				  &ip6->ip6_dst)))) {
662		struct mbuf *mp;
663
664		/* Fix IPv6 header payload length. */
665		if (m->m_len < sizeof(struct ip6_hdr))
666			if ((m = m_pullup(m,sizeof(struct ip6_hdr))) == NULL) {
667				error = ENOBUFS;
668				goto bad;
669			}
670
671		if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
672			/* No jumbogram support. */
673			error = ENXIO;   /*XXX*/
674			goto bad;
675		}
676
677		ip6 = mtod(m, struct ip6_hdr *);
678		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
679
680		/* Encapsulate the packet */
681		error = ipip_output(m, isr, &mp, 0, 0);
682		if (mp == NULL && !error) {
683			/* Should never happen. */
684			DPRINTF(("ipsec6_process_packet: ipip_output "
685				 "returns no mbuf and no error!"));
686			error = EFAULT;
687			goto bad;
688		}
689
690		if (error) {
691			if (mp) {
692				/* XXX: Should never happen! */
693				m_freem(mp);
694			}
695			m = NULL; /* ipip_output() already freed it */
696			goto bad;
697		}
698
699		m = mp;
700		mp = NULL;
701	}
702
703#ifdef DEV_ENC
704	ipsec_bpf(m, isr->sav, dst->sa.sa_family, ENC_OUT|ENC_AFTER);
705	/* pass the mbuf to enc0 for packet filtering */
706	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
707		goto bad;
708#endif /* DEV_ENC */
709
710	switch(dst->sa.sa_family) {
711#ifdef INET
712	case AF_INET:
713		{
714		struct ip *ip;
715		ip = mtod(m, struct ip *);
716		i = ip->ip_hl << 2;
717		off = offsetof(struct ip, ip_p);
718		}
719		break;
720#endif /* AF_INET */
721	case AF_INET6:
722		i = sizeof(struct ip6_hdr);
723		off = offsetof(struct ip6_hdr, ip6_nxt);
724		break;
725	default:
726		DPRINTF(("%s: unsupported protocol family %u\n",
727				 __func__, dst->sa.sa_family));
728		error = EPFNOSUPPORT;
729		IPSEC6STAT_INC(ips_out_inval);
730		goto bad;
731	}
732	error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
733	IPSECREQUEST_UNLOCK(isr);
734	return error;
735bad:
736
737	if (isr)
738		IPSECREQUEST_UNLOCK(isr);
739	if (m)
740		m_freem(m);
741	return error;
742}
743#endif /*INET6*/
744