ipsec_input.c revision 274755
1/*	$FreeBSD: stable/10/sys/netipsec/ipsec_input.c 274755 2014-11-20 18:49:11Z ae $	*/
2/*	$OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $	*/
3/*-
4 * The authors of this code are John Ioannidis (ji@tla.org),
5 * Angelos D. Keromytis (kermit@csd.uch.gr) and
6 * Niels Provos (provos@physnet.uni-hamburg.de).
7 *
8 * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
9 * in November 1995.
10 *
11 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12 * by Angelos D. Keromytis.
13 *
14 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
15 * and Niels Provos.
16 *
17 * Additional features in 1999 by Angelos D. Keromytis.
18 *
19 * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
20 * Angelos D. Keromytis and Niels Provos.
21 * Copyright (c) 2001, Angelos D. Keromytis.
22 *
23 * Permission to use, copy, and modify this software with or without fee
24 * is hereby granted, provided that this entire notice is included in
25 * all copies of any software which is or includes a copy or
26 * modification of this software.
27 * You may use this code under the GNU public license if you so wish. Please
28 * contribute changes back to the authors under this freer than GPL license
29 * so that we may further the use of strong encryption without limitations to
30 * all.
31 *
32 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
33 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
34 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
35 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
36 * PURPOSE.
37 */
38
39/*
40 * IPsec input processing.
41 */
42
43#include "opt_inet.h"
44#include "opt_inet6.h"
45#include "opt_ipsec.h"
46#include "opt_enc.h"
47
48#include <sys/param.h>
49#include <sys/systm.h>
50#include <sys/malloc.h>
51#include <sys/mbuf.h>
52#include <sys/domain.h>
53#include <sys/protosw.h>
54#include <sys/socket.h>
55#include <sys/errno.h>
56#include <sys/syslog.h>
57
58#include <net/if.h>
59#include <net/pfil.h>
60#include <net/route.h>
61#include <net/netisr.h>
62#include <net/vnet.h>
63
64#include <netinet/in.h>
65#include <netinet/in_systm.h>
66#include <netinet/ip.h>
67#include <netinet/ip_var.h>
68#include <netinet/in_var.h>
69
70#include <netinet/ip6.h>
71#ifdef INET6
72#include <netinet6/ip6_var.h>
73#endif
74#include <netinet/in_pcb.h>
75#ifdef INET6
76#include <netinet/icmp6.h>
77#endif
78
79#include <netipsec/ipsec.h>
80#ifdef INET6
81#include <netipsec/ipsec6.h>
82#endif
83#include <netipsec/ah_var.h>
84#include <netipsec/esp.h>
85#include <netipsec/esp_var.h>
86#include <netipsec/ipcomp_var.h>
87
88#include <netipsec/key.h>
89#include <netipsec/keydb.h>
90
91#include <netipsec/xform.h>
92#include <netinet6/ip6protosw.h>
93
94#include <machine/in_cksum.h>
95#include <machine/stdarg.h>
96
97#ifdef DEV_ENC
98#include <net/if_enc.h>
99#endif
100
101
102#define	IPSEC_ISTAT(proto, name)	do {	\
103	if ((proto) == IPPROTO_ESP)		\
104		ESPSTAT_INC(esps_##name);	\
105	else if ((proto) == IPPROTO_AH)		\
106		AHSTAT_INC(ahs_##name);		\
107	else					\
108		IPCOMPSTAT_INC(ipcomps_##name);	\
109} while (0)
110
111#ifdef INET
112static void ipsec4_common_ctlinput(int, struct sockaddr *, void *, int);
113#endif
114
115/*
116 * ipsec_common_input gets called when an IPsec-protected packet
117 * is received by IPv4 or IPv6.  Its job is to find the right SA
118 * and call the appropriate transform.  The transform callback
119 * takes care of further processing (like ingress filtering).
120 */
121static int
122ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
123{
124	union sockaddr_union dst_address;
125	struct secasvar *sav;
126	u_int32_t spi;
127	int error;
128#ifdef INET
129#ifdef IPSEC_NAT_T
130	struct m_tag *tag;
131#endif
132#endif
133
134	IPSEC_ISTAT(sproto, input);
135
136	IPSEC_ASSERT(m != NULL, ("null packet"));
137
138	IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
139		sproto == IPPROTO_IPCOMP,
140		("unexpected security protocol %u", sproto));
141
142	if ((sproto == IPPROTO_ESP && !V_esp_enable) ||
143	    (sproto == IPPROTO_AH && !V_ah_enable) ||
144	    (sproto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
145		m_freem(m);
146		IPSEC_ISTAT(sproto, pdrops);
147		return EOPNOTSUPP;
148	}
149
150	if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
151		m_freem(m);
152		IPSEC_ISTAT(sproto, hdrops);
153		DPRINTF(("%s: packet too small\n", __func__));
154		return EINVAL;
155	}
156
157	/* Retrieve the SPI from the relevant IPsec header */
158	if (sproto == IPPROTO_ESP)
159		m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
160	else if (sproto == IPPROTO_AH)
161		m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
162		    (caddr_t) &spi);
163	else if (sproto == IPPROTO_IPCOMP) {
164		u_int16_t cpi;
165		m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
166		    (caddr_t) &cpi);
167		spi = ntohl(htons(cpi));
168	}
169
170	/*
171	 * Find the SA and (indirectly) call the appropriate
172	 * kernel crypto routine. The resulting mbuf chain is a valid
173	 * IP packet ready to go through input processing.
174	 */
175	bzero(&dst_address, sizeof (dst_address));
176	dst_address.sa.sa_family = af;
177	switch (af) {
178#ifdef INET
179	case AF_INET:
180		dst_address.sin.sin_len = sizeof(struct sockaddr_in);
181		m_copydata(m, offsetof(struct ip, ip_dst),
182		    sizeof(struct in_addr),
183		    (caddr_t) &dst_address.sin.sin_addr);
184#ifdef IPSEC_NAT_T
185		/* Find the source port for NAT-T; see udp*_espdecap. */
186		tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL);
187		if (tag != NULL)
188			dst_address.sin.sin_port = ((u_int16_t *)(tag + 1))[1];
189#endif /* IPSEC_NAT_T */
190		break;
191#endif /* INET */
192#ifdef INET6
193	case AF_INET6:
194		dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
195		m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
196		    sizeof(struct in6_addr),
197		    (caddr_t) &dst_address.sin6.sin6_addr);
198		break;
199#endif /* INET6 */
200	default:
201		DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
202		m_freem(m);
203		IPSEC_ISTAT(sproto, nopf);
204		return EPFNOSUPPORT;
205	}
206
207	/* NB: only pass dst since key_allocsa follows RFC2401 */
208	sav = KEY_ALLOCSA(&dst_address, sproto, spi);
209	if (sav == NULL) {
210		DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
211			  __func__, ipsec_address(&dst_address),
212			  (u_long) ntohl(spi), sproto));
213		IPSEC_ISTAT(sproto, notdb);
214		m_freem(m);
215		return ENOENT;
216	}
217
218	if (sav->tdb_xform == NULL) {
219		DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
220			 __func__, ipsec_address(&dst_address),
221			 (u_long) ntohl(spi), sproto));
222		IPSEC_ISTAT(sproto, noxform);
223		KEY_FREESAV(&sav);
224		m_freem(m);
225		return ENXIO;
226	}
227
228	/*
229	 * Call appropriate transform and return -- callback takes care of
230	 * everything else.
231	 */
232	error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
233	KEY_FREESAV(&sav);
234	return error;
235}
236
237#ifdef INET
238/*
239 * Common input handler for IPv4 AH, ESP, and IPCOMP.
240 */
241int
242ipsec4_common_input(struct mbuf *m, ...)
243{
244	va_list ap;
245	int off, nxt;
246
247	va_start(ap, m);
248	off = va_arg(ap, int);
249	nxt = va_arg(ap, int);
250	va_end(ap);
251
252	return ipsec_common_input(m, off, offsetof(struct ip, ip_p),
253				  AF_INET, nxt);
254}
255
256void
257ah4_input(struct mbuf *m, int off)
258{
259	ipsec4_common_input(m, off, IPPROTO_AH);
260}
261void
262ah4_ctlinput(int cmd, struct sockaddr *sa, void *v)
263{
264	if (sa->sa_family == AF_INET &&
265	    sa->sa_len == sizeof(struct sockaddr_in))
266		ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_AH);
267}
268
269void
270esp4_input(struct mbuf *m, int off)
271{
272	ipsec4_common_input(m, off, IPPROTO_ESP);
273}
274void
275esp4_ctlinput(int cmd, struct sockaddr *sa, void *v)
276{
277	if (sa->sa_family == AF_INET &&
278	    sa->sa_len == sizeof(struct sockaddr_in))
279		ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_ESP);
280}
281
282void
283ipcomp4_input(struct mbuf *m, int off)
284{
285	ipsec4_common_input(m, off, IPPROTO_IPCOMP);
286}
287
288/*
289 * IPsec input callback for INET protocols.
290 * This routine is called as the transform callback.
291 * Takes care of filtering and other sanity checks on
292 * the processed packet.
293 */
294int
295ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
296			int skip, int protoff, struct m_tag *mt)
297{
298	int prot, af, sproto, isr_prot;
299	struct ip *ip;
300	struct m_tag *mtag;
301	struct tdb_ident *tdbi;
302	struct secasindex *saidx;
303	int error;
304#ifdef INET6
305#ifdef notyet
306	char ip6buf[INET6_ADDRSTRLEN];
307#endif
308#endif
309
310	IPSEC_ASSERT(m != NULL, ("null mbuf"));
311	IPSEC_ASSERT(sav != NULL, ("null SA"));
312	IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
313	saidx = &sav->sah->saidx;
314	af = saidx->dst.sa.sa_family;
315	IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
316	sproto = saidx->proto;
317	IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
318		sproto == IPPROTO_IPCOMP,
319		("unexpected security protocol %u", sproto));
320
321	/* Sanity check */
322	if (m == NULL) {
323		DPRINTF(("%s: null mbuf", __func__));
324		IPSEC_ISTAT(sproto, badkcr);
325		KEY_FREESAV(&sav);
326		return EINVAL;
327	}
328
329	if (skip != 0) {
330		/*
331		 * Fix IPv4 header
332		 * XXXGL: do we need this entire block?
333		 */
334		if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
335			DPRINTF(("%s: processing failed for SA %s/%08lx\n",
336			    __func__, ipsec_address(&sav->sah->saidx.dst),
337			    (u_long) ntohl(sav->spi)));
338			IPSEC_ISTAT(sproto, hdrops);
339			error = ENOBUFS;
340			goto bad;
341		}
342
343		ip = mtod(m, struct ip *);
344		ip->ip_len = htons(m->m_pkthdr.len);
345		ip->ip_sum = 0;
346		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
347	} else {
348		ip = mtod(m, struct ip *);
349	}
350	prot = ip->ip_p;
351
352#ifdef DEV_ENC
353	encif->if_ipackets++;
354	encif->if_ibytes += m->m_pkthdr.len;
355
356	/*
357	 * Pass the mbuf to enc0 for bpf and pfil. We will filter the IPIP
358	 * packet later after it has been decapsulated.
359	 */
360	ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_BEFORE);
361
362	if (prot != IPPROTO_IPIP)
363		if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
364			return (error);
365#endif /* DEV_ENC */
366
367	/* IP-in-IP encapsulation */
368	if (prot == IPPROTO_IPIP &&
369	    saidx->mode != IPSEC_MODE_TRANSPORT) {
370
371		if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
372			IPSEC_ISTAT(sproto, hdrops);
373			error = EINVAL;
374			goto bad;
375		}
376		/* enc0: strip outer IPv4 header */
377		m_striphdr(m, 0, ip->ip_hl << 2);
378
379#ifdef notyet
380		/* XXX PROXY address isn't recorded in SAH */
381		/*
382		 * Check that the inner source address is the same as
383		 * the proxy address, if available.
384		 */
385		if ((saidx->proxy.sa.sa_family == AF_INET &&
386		    saidx->proxy.sin.sin_addr.s_addr !=
387		    INADDR_ANY &&
388		    ipn.ip_src.s_addr !=
389		    saidx->proxy.sin.sin_addr.s_addr) ||
390		    (saidx->proxy.sa.sa_family != AF_INET &&
391			saidx->proxy.sa.sa_family != 0)) {
392
393			DPRINTF(("%s: inner source address %s doesn't "
394			    "correspond to expected proxy source %s, "
395			    "SA %s/%08lx\n", __func__,
396			    inet_ntoa4(ipn.ip_src),
397			    ipsp_address(saidx->proxy),
398			    ipsp_address(saidx->dst),
399			    (u_long) ntohl(sav->spi)));
400
401			IPSEC_ISTAT(sproto, pdrops);
402			error = EACCES;
403			goto bad;
404		}
405#endif /* notyet */
406	}
407#ifdef INET6
408	/* IPv6-in-IP encapsulation. */
409	else if (prot == IPPROTO_IPV6 &&
410	    saidx->mode != IPSEC_MODE_TRANSPORT) {
411
412		if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
413			IPSEC_ISTAT(sproto, hdrops);
414			error = EINVAL;
415			goto bad;
416		}
417		/* enc0: strip IPv4 header, keep IPv6 header only */
418		m_striphdr(m, 0, ip->ip_hl << 2);
419#ifdef notyet
420		/*
421		 * Check that the inner source address is the same as
422		 * the proxy address, if available.
423		 */
424		if ((saidx->proxy.sa.sa_family == AF_INET6 &&
425		    !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
426		    !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
427			&saidx->proxy.sin6.sin6_addr)) ||
428		    (saidx->proxy.sa.sa_family != AF_INET6 &&
429			saidx->proxy.sa.sa_family != 0)) {
430
431			DPRINTF(("%s: inner source address %s doesn't "
432			    "correspond to expected proxy source %s, "
433			    "SA %s/%08lx\n", __func__,
434			    ip6_sprintf(ip6buf, &ip6n.ip6_src),
435			    ipsec_address(&saidx->proxy),
436			    ipsec_address(&saidx->dst),
437			    (u_long) ntohl(sav->spi)));
438
439			IPSEC_ISTAT(sproto, pdrops);
440			error = EACCES;
441			goto bad;
442		}
443#endif /* notyet */
444	}
445#endif /* INET6 */
446	else if (prot != IPPROTO_IPV6 && saidx->mode == IPSEC_MODE_ANY) {
447		/*
448		 * When mode is wildcard, inner protocol is IPv6 and
449		 * we have no INET6 support - drop this packet a bit later.
450		 * In other cases we assume transport mode and outer
451		 * header was already stripped in xform_xxx_cb.
452		 */
453		prot = IPPROTO_IPIP;
454	}
455
456	/*
457	 * Record what we've done to the packet (under what SA it was
458	 * processed). If we've been passed an mtag, it means the packet
459	 * was already processed by an ethernet/crypto combo card and
460	 * thus has a tag attached with all the right information, but
461	 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
462	 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
463	 */
464	if (mt == NULL && sproto != IPPROTO_IPCOMP) {
465		mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
466		    sizeof(struct tdb_ident), M_NOWAIT);
467		if (mtag == NULL) {
468			DPRINTF(("%s: failed to get tag\n", __func__));
469			IPSEC_ISTAT(sproto, hdrops);
470			error = ENOMEM;
471			goto bad;
472		}
473
474		tdbi = (struct tdb_ident *)(mtag + 1);
475		bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len);
476		tdbi->proto = sproto;
477		tdbi->spi = sav->spi;
478		/* Cache those two for enc(4) in xform_ipip. */
479		tdbi->alg_auth = sav->alg_auth;
480		tdbi->alg_enc = sav->alg_enc;
481
482		m_tag_prepend(m, mtag);
483	} else if (mt != NULL) {
484		mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
485		/* XXX do we need to mark m_flags??? */
486	}
487
488	key_sa_recordxfer(sav, m);		/* record data transfer */
489
490	/*
491	 * In transport mode requeue decrypted mbuf back to IPv4 protocol
492	 * handler. This is necessary to correctly expose rcvif.
493	 */
494	if (saidx->mode == IPSEC_MODE_TRANSPORT)
495		prot = IPPROTO_IPIP;
496#ifdef DEV_ENC
497	/*
498	 * Pass the mbuf to enc0 for bpf and pfil.
499	 */
500	if (prot == IPPROTO_IPIP)
501		ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_AFTER);
502#ifdef INET6
503	if (prot == IPPROTO_IPV6)
504		ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_AFTER);
505#endif
506
507	if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER)) != 0)
508		return (error);
509#endif /* DEV_ENC */
510
511	/*
512	 * Re-dispatch via software interrupt.
513	 */
514
515	switch (prot) {
516	case IPPROTO_IPIP:
517		isr_prot = NETISR_IP;
518		break;
519#ifdef INET6
520	case IPPROTO_IPV6:
521		isr_prot = NETISR_IPV6;
522		break;
523#endif
524	default:
525		DPRINTF(("%s: cannot handle inner ip proto %d\n",
526			    __func__, prot));
527		IPSEC_ISTAT(sproto, nopf);
528		error = EPFNOSUPPORT;
529		goto bad;
530	}
531
532	error = netisr_queue_src(isr_prot, (uintptr_t)sav->spi, m);
533	if (error) {
534		IPSEC_ISTAT(sproto, qfull);
535		DPRINTF(("%s: queue full; proto %u packet dropped\n",
536			__func__, sproto));
537		return error;
538	}
539	return 0;
540bad:
541	m_freem(m);
542	return error;
543}
544
545void
546ipsec4_common_ctlinput(int cmd, struct sockaddr *sa, void *v, int proto)
547{
548	/* XXX nothing just yet */
549}
550#endif /* INET */
551
552#ifdef INET6
553/* IPv6 AH wrapper. */
554int
555ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
556{
557	int l = 0;
558	int protoff;
559	struct ip6_ext ip6e;
560
561	if (*offp < sizeof(struct ip6_hdr)) {
562		DPRINTF(("%s: bad offset %u\n", __func__, *offp));
563		return IPPROTO_DONE;
564	} else if (*offp == sizeof(struct ip6_hdr)) {
565		protoff = offsetof(struct ip6_hdr, ip6_nxt);
566	} else {
567		/* Chase down the header chain... */
568		protoff = sizeof(struct ip6_hdr);
569
570		do {
571			protoff += l;
572			m_copydata(*mp, protoff, sizeof(ip6e),
573			    (caddr_t) &ip6e);
574
575			if (ip6e.ip6e_nxt == IPPROTO_AH)
576				l = (ip6e.ip6e_len + 2) << 2;
577			else
578				l = (ip6e.ip6e_len + 1) << 3;
579			IPSEC_ASSERT(l > 0, ("l went zero or negative"));
580		} while (protoff + l < *offp);
581
582		/* Malformed packet check */
583		if (protoff + l != *offp) {
584			DPRINTF(("%s: bad packet header chain, protoff %u, "
585				"l %u, off %u\n", __func__, protoff, l, *offp));
586			IPSEC_ISTAT(proto, hdrops);
587			m_freem(*mp);
588			*mp = NULL;
589			return IPPROTO_DONE;
590		}
591		protoff += offsetof(struct ip6_ext, ip6e_nxt);
592	}
593	(void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
594	return IPPROTO_DONE;
595}
596
597/*
598 * IPsec input callback, called by the transform callback. Takes care of
599 * filtering and other sanity checks on the processed packet.
600 */
601int
602ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip, int protoff,
603    struct m_tag *mt)
604{
605	int prot, af, sproto;
606	struct ip6_hdr *ip6;
607	struct m_tag *mtag;
608	struct tdb_ident *tdbi;
609	struct secasindex *saidx;
610	int nxt;
611	u_int8_t nxt8;
612	int error, nest;
613#ifdef notyet
614	char ip6buf[INET6_ADDRSTRLEN];
615#endif
616
617	IPSEC_ASSERT(m != NULL, ("null mbuf"));
618	IPSEC_ASSERT(sav != NULL, ("null SA"));
619	IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
620	saidx = &sav->sah->saidx;
621	af = saidx->dst.sa.sa_family;
622	IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
623	sproto = saidx->proto;
624	IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
625		sproto == IPPROTO_IPCOMP,
626		("unexpected security protocol %u", sproto));
627
628	/* Sanity check */
629	if (m == NULL) {
630		DPRINTF(("%s: null mbuf", __func__));
631		IPSEC_ISTAT(sproto, badkcr);
632		error = EINVAL;
633		goto bad;
634	}
635
636	/* Fix IPv6 header */
637	if (m->m_len < sizeof(struct ip6_hdr) &&
638	    (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
639
640		DPRINTF(("%s: processing failed for SA %s/%08lx\n",
641		    __func__, ipsec_address(&sav->sah->saidx.dst),
642		    (u_long) ntohl(sav->spi)));
643
644		IPSEC_ISTAT(sproto, hdrops);
645		error = EACCES;
646		goto bad;
647	}
648
649	ip6 = mtod(m, struct ip6_hdr *);
650	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
651
652	/* Save protocol */
653	m_copydata(m, protoff, 1, &nxt8);
654	prot = nxt8;
655
656#ifdef DEV_ENC
657	encif->if_ipackets++;
658	encif->if_ibytes += m->m_pkthdr.len;
659
660	/*
661	 * Pass the mbuf to enc0 for bpf and pfil. We will filter the IPIP
662	 * packet later after it has been decapsulated.
663	 */
664	ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_BEFORE);
665
666	/* XXX-BZ does not make sense. */
667	if (prot != IPPROTO_IPIP)
668		if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
669			return (error);
670#endif /* DEV_ENC */
671
672	/* IPv6-in-IP encapsulation */
673	if (prot == IPPROTO_IPV6 &&
674	    saidx->mode != IPSEC_MODE_TRANSPORT) {
675		if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
676			IPSEC_ISTAT(sproto, hdrops);
677			error = EINVAL;
678			goto bad;
679		}
680		/* ip6n will now contain the inner IPv6 header. */
681		m_striphdr(m, 0, skip);
682		skip = 0;
683#ifdef notyet
684		/*
685		 * Check that the inner source address is the same as
686		 * the proxy address, if available.
687		 */
688		if ((saidx->proxy.sa.sa_family == AF_INET6 &&
689		    !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
690		    !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
691			&saidx->proxy.sin6.sin6_addr)) ||
692		    (saidx->proxy.sa.sa_family != AF_INET6 &&
693			saidx->proxy.sa.sa_family != 0)) {
694
695			DPRINTF(("%s: inner source address %s doesn't "
696			    "correspond to expected proxy source %s, "
697			    "SA %s/%08lx\n", __func__,
698			    ip6_sprintf(ip6buf, &ip6n.ip6_src),
699			    ipsec_address(&saidx->proxy),
700			    ipsec_address(&saidx->dst),
701			    (u_long) ntohl(sav->spi)));
702
703			IPSEC_ISTAT(sproto, pdrops);
704			error = EACCES;
705			goto bad;
706		}
707#endif /* notyet */
708	}
709#ifdef INET
710	/* IP-in-IP encapsulation */
711	else if (prot == IPPROTO_IPIP &&
712	    saidx->mode != IPSEC_MODE_TRANSPORT) {
713		if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
714			IPSEC_ISTAT(sproto, hdrops);
715			error = EINVAL;
716			goto bad;
717		}
718		/* ipn will now contain the inner IPv4 header */
719	 	m_striphdr(m, 0, skip);
720		skip = 0;
721#ifdef notyet
722		/*
723		 * Check that the inner source address is the same as
724		 * the proxy address, if available.
725		 */
726		if ((saidx->proxy.sa.sa_family == AF_INET &&
727		    saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
728		    ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
729		    (saidx->proxy.sa.sa_family != AF_INET &&
730			saidx->proxy.sa.sa_family != 0)) {
731
732			DPRINTF(("%s: inner source address %s doesn't "
733			    "correspond to expected proxy source %s, "
734			    "SA %s/%08lx\n", __func__,
735			    inet_ntoa4(ipn.ip_src),
736			    ipsec_address(&saidx->proxy),
737			    ipsec_address(&saidx->dst),
738			    (u_long) ntohl(sav->spi)));
739
740			IPSEC_ISTAT(sproto, pdrops);
741			error = EACCES;
742			goto bad;
743		}
744#endif /* notyet */
745	}
746#endif /* INET */
747	else {
748		prot = IPPROTO_IPV6; /* for correct BPF processing */
749	}
750
751	/*
752	 * Record what we've done to the packet (under what SA it was
753	 * processed). If we've been passed an mtag, it means the packet
754	 * was already processed by an ethernet/crypto combo card and
755	 * thus has a tag attached with all the right information, but
756	 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
757	 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
758	 */
759	if (mt == NULL && sproto != IPPROTO_IPCOMP) {
760		mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
761		    sizeof(struct tdb_ident), M_NOWAIT);
762		if (mtag == NULL) {
763			DPRINTF(("%s: failed to get tag\n", __func__));
764			IPSEC_ISTAT(sproto, hdrops);
765			error = ENOMEM;
766			goto bad;
767		}
768
769		tdbi = (struct tdb_ident *)(mtag + 1);
770		bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union));
771		tdbi->proto = sproto;
772		tdbi->spi = sav->spi;
773		/* Cache those two for enc(4) in xform_ipip. */
774		tdbi->alg_auth = sav->alg_auth;
775		tdbi->alg_enc = sav->alg_enc;
776
777		m_tag_prepend(m, mtag);
778	} else {
779		if (mt != NULL)
780			mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
781		/* XXX do we need to mark m_flags??? */
782	}
783
784	key_sa_recordxfer(sav, m);
785
786#ifdef DEV_ENC
787	/*
788	 * Pass the mbuf to enc0 for bpf and pfil.
789	 */
790#ifdef INET
791	if (prot == IPPROTO_IPIP)
792		ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_AFTER);
793#endif
794	if (prot == IPPROTO_IPV6)
795		ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_AFTER);
796
797	if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER)) != 0)
798		return (error);
799#endif /* DEV_ENC */
800	/*
801	 * See the end of ip6_input for this logic.
802	 * IPPROTO_IPV[46] case will be processed just like other ones
803	 */
804	nest = 0;
805	nxt = nxt8;
806	while (nxt != IPPROTO_DONE) {
807		if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
808			IP6STAT_INC(ip6s_toomanyhdr);
809			error = EINVAL;
810			goto bad;
811		}
812
813		/*
814		 * Protection against faulty packet - there should be
815		 * more sanity checks in header chain processing.
816		 */
817		if (m->m_pkthdr.len < skip) {
818			IP6STAT_INC(ip6s_tooshort);
819			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
820			error = EINVAL;
821			goto bad;
822		}
823		/*
824		 * Enforce IPsec policy checking if we are seeing last header.
825		 * note that we do not visit this with protocols with pcb layer
826		 * code - like udp/tcp/raw ip.
827		 */
828		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
829		    ipsec6_in_reject(m, NULL)) {
830			error = EINVAL;
831			goto bad;
832		}
833		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
834	}
835	return 0;
836bad:
837	if (m)
838		m_freem(m);
839	return error;
840}
841
842void
843esp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
844{
845	struct ip6ctlparam *ip6cp = NULL;
846	struct mbuf *m = NULL;
847	struct ip6_hdr *ip6;
848	int off;
849
850	if (sa->sa_family != AF_INET6 ||
851	    sa->sa_len != sizeof(struct sockaddr_in6))
852		return;
853	if ((unsigned)cmd >= PRC_NCMDS)
854		return;
855
856	/* if the parameter is from icmp6, decode it. */
857	if (d != NULL) {
858		ip6cp = (struct ip6ctlparam *)d;
859		m = ip6cp->ip6c_m;
860		ip6 = ip6cp->ip6c_ip6;
861		off = ip6cp->ip6c_off;
862	} else {
863		m = NULL;
864		ip6 = NULL;
865		off = 0;	/* calm gcc */
866	}
867
868	if (ip6 != NULL) {
869
870		struct ip6ctlparam ip6cp1;
871
872		/*
873		 * Notify the error to all possible sockets via pfctlinput2.
874		 * Since the upper layer information (such as protocol type,
875		 * source and destination ports) is embedded in the encrypted
876		 * data and might have been cut, we can't directly call
877		 * an upper layer ctlinput function. However, the pcbnotify
878		 * function will consider source and destination addresses
879		 * as well as the flow info value, and may be able to find
880		 * some PCB that should be notified.
881		 * Although pfctlinput2 will call esp6_ctlinput(), there is
882		 * no possibility of an infinite loop of function calls,
883		 * because we don't pass the inner IPv6 header.
884		 */
885		bzero(&ip6cp1, sizeof(ip6cp1));
886		ip6cp1.ip6c_src = ip6cp->ip6c_src;
887		pfctlinput2(cmd, sa, (void *)&ip6cp1);
888
889		/*
890		 * Then go to special cases that need ESP header information.
891		 * XXX: We assume that when ip6 is non NULL,
892		 * M and OFF are valid.
893		 */
894
895		if (cmd == PRC_MSGSIZE) {
896			struct secasvar *sav;
897			u_int32_t spi;
898			int valid;
899
900			/* check header length before using m_copydata */
901			if (m->m_pkthdr.len < off + sizeof (struct esp))
902				return;
903			m_copydata(m, off + offsetof(struct esp, esp_spi),
904				sizeof(u_int32_t), (caddr_t) &spi);
905			/*
906			 * Check to see if we have a valid SA corresponding to
907			 * the address in the ICMP message payload.
908			 */
909			sav = KEY_ALLOCSA((union sockaddr_union *)sa,
910					IPPROTO_ESP, spi);
911			valid = (sav != NULL);
912			if (sav)
913				KEY_FREESAV(&sav);
914
915			/* XXX Further validation? */
916
917			/*
918			 * Depending on whether the SA is "valid" and
919			 * routing table size (mtudisc_{hi,lo}wat), we will:
920			 * - recalcurate the new MTU and create the
921			 *   corresponding routing entry, or
922			 * - ignore the MTU change notification.
923			 */
924			icmp6_mtudisc_update(ip6cp, valid);
925		}
926	} else {
927		/* we normally notify any pcb here */
928	}
929}
930#endif /* INET6 */
931