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