xref: /freebsd/sys/netipsec/ipsec_input.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*	$FreeBSD$	*/
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 
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
66 #include <netinet/ip_var.h>
67 #include <netinet/in_var.h>
68 
69 #include <netinet/ip6.h>
70 #ifdef INET6
71 #include <netinet6/ip6_var.h>
72 #endif
73 #include <netinet/in_pcb.h>
74 #ifdef INET6
75 #include <netinet/icmp6.h>
76 #endif
77 
78 #include <netipsec/ipsec.h>
79 #ifdef INET6
80 #include <netipsec/ipsec6.h>
81 #endif
82 #include <netipsec/ah_var.h>
83 #include <netipsec/esp.h>
84 #include <netipsec/esp_var.h>
85 #include <netipsec/ipcomp_var.h>
86 
87 #include <netipsec/key.h>
88 #include <netipsec/keydb.h>
89 
90 #include <netipsec/xform.h>
91 #include <netinet6/ip6protosw.h>
92 
93 #include <machine/in_cksum.h>
94 #include <machine/stdarg.h>
95 
96 #define IPSEC_ISTAT(p,x,y,z) ((p) == IPPROTO_ESP ? (x)++ : \
97 			    (p) == IPPROTO_AH ? (y)++ : (z)++)
98 
99 static void ipsec4_common_ctlinput(int, struct sockaddr *, void *, int);
100 
101 /*
102  * ipsec_common_input gets called when an IPsec-protected packet
103  * is received by IPv4 or IPv6.  It's job is to find the right SA
104  * and call the appropriate transform.  The transform callback
105  * takes care of further processing (like ingress filtering).
106  */
107 static int
108 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
109 {
110 	union sockaddr_union dst_address;
111 	struct secasvar *sav;
112 	u_int32_t spi;
113 	int error;
114 
115 	IPSEC_ISTAT(sproto, espstat.esps_input, ahstat.ahs_input,
116 		ipcompstat.ipcomps_input);
117 
118 	IPSEC_ASSERT(m != NULL, ("null packet"));
119 
120 	IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
121 		sproto == IPPROTO_IPCOMP,
122 		("unexpected security protocol %u", sproto));
123 
124 	if ((sproto == IPPROTO_ESP && !esp_enable) ||
125 	    (sproto == IPPROTO_AH && !ah_enable) ||
126 	    (sproto == IPPROTO_IPCOMP && !ipcomp_enable)) {
127 		m_freem(m);
128 		IPSEC_ISTAT(sproto, espstat.esps_pdrops, ahstat.ahs_pdrops,
129 		    ipcompstat.ipcomps_pdrops);
130 		return EOPNOTSUPP;
131 	}
132 
133 	if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
134 		m_freem(m);
135 		IPSEC_ISTAT(sproto, espstat.esps_hdrops, ahstat.ahs_hdrops,
136 		    ipcompstat.ipcomps_hdrops);
137 		DPRINTF(("%s: packet too small\n", __func__));
138 		return EINVAL;
139 	}
140 
141 	/* Retrieve the SPI from the relevant IPsec header */
142 	if (sproto == IPPROTO_ESP)
143 		m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
144 	else if (sproto == IPPROTO_AH)
145 		m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
146 		    (caddr_t) &spi);
147 	else if (sproto == IPPROTO_IPCOMP) {
148 		u_int16_t cpi;
149 		m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
150 		    (caddr_t) &cpi);
151 		spi = ntohl(htons(cpi));
152 	}
153 
154 	/*
155 	 * Find the SA and (indirectly) call the appropriate
156 	 * kernel crypto routine. The resulting mbuf chain is a valid
157 	 * IP packet ready to go through input processing.
158 	 */
159 	bzero(&dst_address, sizeof (dst_address));
160 	dst_address.sa.sa_family = af;
161 	switch (af) {
162 #ifdef INET
163 	case AF_INET:
164 		dst_address.sin.sin_len = sizeof(struct sockaddr_in);
165 		m_copydata(m, offsetof(struct ip, ip_dst),
166 		    sizeof(struct in_addr),
167 		    (caddr_t) &dst_address.sin.sin_addr);
168 		break;
169 #endif /* INET */
170 #ifdef INET6
171 	case AF_INET6:
172 		dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
173 		m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
174 		    sizeof(struct in6_addr),
175 		    (caddr_t) &dst_address.sin6.sin6_addr);
176 		break;
177 #endif /* INET6 */
178 	default:
179 		DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
180 		m_freem(m);
181 		IPSEC_ISTAT(sproto, espstat.esps_nopf, ahstat.ahs_nopf,
182 		    ipcompstat.ipcomps_nopf);
183 		return EPFNOSUPPORT;
184 	}
185 
186 	/* NB: only pass dst since key_allocsa follows RFC2401 */
187 	sav = KEY_ALLOCSA(&dst_address, sproto, spi);
188 	if (sav == NULL) {
189 		DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
190 			  __func__, ipsec_address(&dst_address),
191 			  (u_long) ntohl(spi), sproto));
192 		IPSEC_ISTAT(sproto, espstat.esps_notdb, ahstat.ahs_notdb,
193 		    ipcompstat.ipcomps_notdb);
194 		m_freem(m);
195 		return ENOENT;
196 	}
197 
198 	if (sav->tdb_xform == NULL) {
199 		DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
200 			 __func__, ipsec_address(&dst_address),
201 			 (u_long) ntohl(spi), sproto));
202 		IPSEC_ISTAT(sproto, espstat.esps_noxform, ahstat.ahs_noxform,
203 		    ipcompstat.ipcomps_noxform);
204 		KEY_FREESAV(&sav);
205 		m_freem(m);
206 		return ENXIO;
207 	}
208 
209 	/*
210 	 * Call appropriate transform and return -- callback takes care of
211 	 * everything else.
212 	 */
213 	error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
214 	KEY_FREESAV(&sav);
215 	return error;
216 }
217 
218 #ifdef INET
219 /*
220  * Common input handler for IPv4 AH, ESP, and IPCOMP.
221  */
222 int
223 ipsec4_common_input(struct mbuf *m, ...)
224 {
225 	va_list ap;
226 	int off, nxt;
227 
228 	va_start(ap, m);
229 	off = va_arg(ap, int);
230 	nxt = va_arg(ap, int);
231 	va_end(ap);
232 
233 	return ipsec_common_input(m, off, offsetof(struct ip, ip_p),
234 				  AF_INET, nxt);
235 }
236 
237 void
238 ah4_input(struct mbuf *m, int off)
239 {
240 	ipsec4_common_input(m, off, IPPROTO_AH);
241 }
242 void
243 ah4_ctlinput(int cmd, struct sockaddr *sa, void *v)
244 {
245 	if (sa->sa_family == AF_INET &&
246 	    sa->sa_len == sizeof(struct sockaddr_in))
247 		ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_AH);
248 }
249 
250 void
251 esp4_input(struct mbuf *m, int off)
252 {
253 	ipsec4_common_input(m, off, IPPROTO_ESP);
254 }
255 void
256 esp4_ctlinput(int cmd, struct sockaddr *sa, void *v)
257 {
258 	if (sa->sa_family == AF_INET &&
259 	    sa->sa_len == sizeof(struct sockaddr_in))
260 		ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_ESP);
261 }
262 
263 void
264 ipcomp4_input(struct mbuf *m, int off)
265 {
266 	ipsec4_common_input(m, off, IPPROTO_IPCOMP);
267 }
268 
269 /*
270  * IPsec input callback for INET protocols.
271  * This routine is called as the transform callback.
272  * Takes care of filtering and other sanity checks on
273  * the processed packet.
274  */
275 int
276 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
277 			int skip, int protoff, struct m_tag *mt)
278 {
279 	int prot, af, sproto;
280 	struct ip *ip;
281 	struct m_tag *mtag;
282 	struct tdb_ident *tdbi;
283 	struct secasindex *saidx;
284 	int error;
285 #ifdef INET6
286 #ifdef notyet
287 	char ip6buf[INET6_ADDRSTRLEN];
288 #endif
289 #endif
290 
291 	IPSEC_SPLASSERT_SOFTNET(__func__);
292 
293 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
294 	IPSEC_ASSERT(sav != NULL, ("null SA"));
295 	IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
296 	saidx = &sav->sah->saidx;
297 	af = saidx->dst.sa.sa_family;
298 	IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
299 	sproto = saidx->proto;
300 	IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
301 		sproto == IPPROTO_IPCOMP,
302 		("unexpected security protocol %u", sproto));
303 
304 	/* Sanity check */
305 	if (m == NULL) {
306 		DPRINTF(("%s: null mbuf", __func__));
307 		IPSEC_ISTAT(sproto, espstat.esps_badkcr, ahstat.ahs_badkcr,
308 		    ipcompstat.ipcomps_badkcr);
309 		KEY_FREESAV(&sav);
310 		return EINVAL;
311 	}
312 
313 	if (skip != 0) {
314 		/* Fix IPv4 header */
315 		if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
316 			DPRINTF(("%s: processing failed for SA %s/%08lx\n",
317 			    __func__, ipsec_address(&sav->sah->saidx.dst),
318 			    (u_long) ntohl(sav->spi)));
319 			IPSEC_ISTAT(sproto, espstat.esps_hdrops, ahstat.ahs_hdrops,
320 			    ipcompstat.ipcomps_hdrops);
321 			error = ENOBUFS;
322 			goto bad;
323 		}
324 
325 		ip = mtod(m, struct ip *);
326 		ip->ip_len = htons(m->m_pkthdr.len);
327 		ip->ip_off = htons(ip->ip_off);
328 		ip->ip_sum = 0;
329 		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
330 	} else {
331 		ip = mtod(m, struct ip *);
332 	}
333 	prot = ip->ip_p;
334 
335 #ifdef notyet
336 	/* IP-in-IP encapsulation */
337 	if (prot == IPPROTO_IPIP) {
338 		struct ip ipn;
339 
340 		if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
341 			IPSEC_ISTAT(sproto, espstat.esps_hdrops,
342 			    ahstat.ahs_hdrops,
343 			    ipcompstat.ipcomps_hdrops);
344 			error = EINVAL;
345 			goto bad;
346 		}
347 		/* ipn will now contain the inner IPv4 header */
348 		m_copydata(m, ip->ip_hl << 2, sizeof(struct ip),
349 		    (caddr_t) &ipn);
350 
351 		/* XXX PROXY address isn't recorded in SAH */
352 		/*
353 		 * Check that the inner source address is the same as
354 		 * the proxy address, if available.
355 		 */
356 		if ((saidx->proxy.sa.sa_family == AF_INET &&
357 		    saidx->proxy.sin.sin_addr.s_addr !=
358 		    INADDR_ANY &&
359 		    ipn.ip_src.s_addr !=
360 		    saidx->proxy.sin.sin_addr.s_addr) ||
361 		    (saidx->proxy.sa.sa_family != AF_INET &&
362 			saidx->proxy.sa.sa_family != 0)) {
363 
364 			DPRINTF(("%s: inner source address %s doesn't "
365 			    "correspond to expected proxy source %s, "
366 			    "SA %s/%08lx\n", __func__,
367 			    inet_ntoa4(ipn.ip_src),
368 			    ipsp_address(saidx->proxy),
369 			    ipsp_address(saidx->dst),
370 			    (u_long) ntohl(sav->spi)));
371 
372 			IPSEC_ISTAT(sproto, espstat.esps_pdrops,
373 			    ahstat.ahs_pdrops,
374 			    ipcompstat.ipcomps_pdrops);
375 			error = EACCES;
376 			goto bad;
377 		}
378 	}
379 #ifdef INET6
380 	/* IPv6-in-IP encapsulation. */
381 	if (prot == IPPROTO_IPV6) {
382 		struct ip6_hdr ip6n;
383 
384 		if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
385 			IPSEC_ISTAT(sproto, espstat.esps_hdrops,
386 			    ahstat.ahs_hdrops,
387 			    ipcompstat.ipcomps_hdrops);
388 			error = EINVAL;
389 			goto bad;
390 		}
391 		/* ip6n will now contain the inner IPv6 header. */
392 		m_copydata(m, ip->ip_hl << 2, sizeof(struct ip6_hdr),
393 		    (caddr_t) &ip6n);
394 
395 		/*
396 		 * Check that the inner source address is the same as
397 		 * the proxy address, if available.
398 		 */
399 		if ((saidx->proxy.sa.sa_family == AF_INET6 &&
400 		    !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
401 		    !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
402 			&saidx->proxy.sin6.sin6_addr)) ||
403 		    (saidx->proxy.sa.sa_family != AF_INET6 &&
404 			saidx->proxy.sa.sa_family != 0)) {
405 
406 			DPRINTF(("%s: inner source address %s doesn't "
407 			    "correspond to expected proxy source %s, "
408 			    "SA %s/%08lx\n", __func__,
409 			    ip6_sprintf(ip6buf, &ip6n.ip6_src),
410 			    ipsec_address(&saidx->proxy),
411 			    ipsec_address(&saidx->dst),
412 			    (u_long) ntohl(sav->spi)));
413 
414 			IPSEC_ISTAT(sproto, espstat.esps_pdrops,
415 			    ahstat.ahs_pdrops,
416 			    ipcompstat.ipcomps_pdrops);
417 			error = EACCES;
418 			goto bad;
419 		}
420 	}
421 #endif /* INET6 */
422 #endif /*XXX*/
423 
424 	/*
425 	 * Record what we've done to the packet (under what SA it was
426 	 * processed). If we've been passed an mtag, it means the packet
427 	 * was already processed by an ethernet/crypto combo card and
428 	 * thus has a tag attached with all the right information, but
429 	 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
430 	 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
431 	 */
432 	if (mt == NULL && sproto != IPPROTO_IPCOMP) {
433 		mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
434 		    sizeof(struct tdb_ident), M_NOWAIT);
435 		if (mtag == NULL) {
436 			DPRINTF(("%s: failed to get tag\n", __func__));
437 			IPSEC_ISTAT(sproto, espstat.esps_hdrops,
438 			    ahstat.ahs_hdrops, ipcompstat.ipcomps_hdrops);
439 			error = ENOMEM;
440 			goto bad;
441 		}
442 
443 		tdbi = (struct tdb_ident *)(mtag + 1);
444 		bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len);
445 		tdbi->proto = sproto;
446 		tdbi->spi = sav->spi;
447 		/* Cache those two for enc(4) in xform_ipip. */
448 		tdbi->alg_auth = sav->alg_auth;
449 		tdbi->alg_enc = sav->alg_enc;
450 
451 		m_tag_prepend(m, mtag);
452 	} else if (mt != NULL) {
453 		mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
454 		/* XXX do we need to mark m_flags??? */
455 	}
456 
457 	key_sa_recordxfer(sav, m);		/* record data transfer */
458 
459 #ifdef DEV_ENC
460 	/*
461 	 * Pass the mbuf to enc0 for bpf and pfil. We will filter the IPIP
462 	 * packet later after it has been decapsulated.
463 	 */
464 	ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_BEFORE);
465 
466 	if (prot != IPPROTO_IPIP)
467 		if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
468 			return (error);
469 #endif
470 
471 	/*
472 	 * Re-dispatch via software interrupt.
473 	 */
474 	if ((error = netisr_queue(NETISR_IP, m))) {
475 		IPSEC_ISTAT(sproto, espstat.esps_qfull, ahstat.ahs_qfull,
476 			    ipcompstat.ipcomps_qfull);
477 
478 		DPRINTF(("%s: queue full; proto %u packet dropped\n",
479 			__func__, sproto));
480 		return error;
481 	}
482 	return 0;
483 bad:
484 	m_freem(m);
485 	return error;
486 }
487 
488 void
489 ipsec4_common_ctlinput(int cmd, struct sockaddr *sa, void *v, int proto)
490 {
491 	/* XXX nothing just yet */
492 }
493 #endif /* INET */
494 
495 #ifdef INET6
496 /* IPv6 AH wrapper. */
497 int
498 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
499 {
500 	int l = 0;
501 	int protoff;
502 	struct ip6_ext ip6e;
503 
504 	if (*offp < sizeof(struct ip6_hdr)) {
505 		DPRINTF(("%s: bad offset %u\n", __func__, *offp));
506 		return IPPROTO_DONE;
507 	} else if (*offp == sizeof(struct ip6_hdr)) {
508 		protoff = offsetof(struct ip6_hdr, ip6_nxt);
509 	} else {
510 		/* Chase down the header chain... */
511 		protoff = sizeof(struct ip6_hdr);
512 
513 		do {
514 			protoff += l;
515 			m_copydata(*mp, protoff, sizeof(ip6e),
516 			    (caddr_t) &ip6e);
517 
518 			if (ip6e.ip6e_nxt == IPPROTO_AH)
519 				l = (ip6e.ip6e_len + 2) << 2;
520 			else
521 				l = (ip6e.ip6e_len + 1) << 3;
522 			IPSEC_ASSERT(l > 0, ("l went zero or negative"));
523 		} while (protoff + l < *offp);
524 
525 		/* Malformed packet check */
526 		if (protoff + l != *offp) {
527 			DPRINTF(("%s: bad packet header chain, protoff %u, "
528 				"l %u, off %u\n", __func__, protoff, l, *offp));
529 			IPSEC_ISTAT(proto, espstat.esps_hdrops,
530 				    ahstat.ahs_hdrops,
531 				    ipcompstat.ipcomps_hdrops);
532 			m_freem(*mp);
533 			*mp = NULL;
534 			return IPPROTO_DONE;
535 		}
536 		protoff += offsetof(struct ip6_ext, ip6e_nxt);
537 	}
538 	(void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
539 	return IPPROTO_DONE;
540 }
541 
542 /*
543  * IPsec input callback, called by the transform callback. Takes care of
544  * filtering and other sanity checks on the processed packet.
545  */
546 int
547 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip, int protoff,
548     struct m_tag *mt)
549 {
550 	int prot, af, sproto;
551 	struct ip6_hdr *ip6;
552 	struct m_tag *mtag;
553 	struct tdb_ident *tdbi;
554 	struct secasindex *saidx;
555 	int nxt;
556 	u_int8_t nxt8;
557 	int error, nest;
558 #ifdef notyet
559 	char ip6buf[INET6_ADDRSTRLEN];
560 #endif
561 
562 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
563 	IPSEC_ASSERT(sav != NULL, ("null SA"));
564 	IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
565 	saidx = &sav->sah->saidx;
566 	af = saidx->dst.sa.sa_family;
567 	IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
568 	sproto = saidx->proto;
569 	IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
570 		sproto == IPPROTO_IPCOMP,
571 		("unexpected security protocol %u", sproto));
572 
573 	/* Sanity check */
574 	if (m == NULL) {
575 		DPRINTF(("%s: null mbuf", __func__));
576 		IPSEC_ISTAT(sproto, espstat.esps_badkcr, ahstat.ahs_badkcr,
577 		    ipcompstat.ipcomps_badkcr);
578 		error = EINVAL;
579 		goto bad;
580 	}
581 
582 	/* Fix IPv6 header */
583 	if (m->m_len < sizeof(struct ip6_hdr) &&
584 	    (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
585 
586 		DPRINTF(("%s: processing failed for SA %s/%08lx\n",
587 		    __func__, ipsec_address(&sav->sah->saidx.dst),
588 		    (u_long) ntohl(sav->spi)));
589 
590 		IPSEC_ISTAT(sproto, espstat.esps_hdrops, ahstat.ahs_hdrops,
591 		    ipcompstat.ipcomps_hdrops);
592 		error = EACCES;
593 		goto bad;
594 	}
595 
596 	ip6 = mtod(m, struct ip6_hdr *);
597 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
598 
599 	/* Save protocol */
600 	m_copydata(m, protoff, 1, (unsigned char *) &prot);
601 
602 #ifdef notyet
603 #ifdef INET
604 	/* IP-in-IP encapsulation */
605 	if (prot == IPPROTO_IPIP) {
606 		struct ip ipn;
607 
608 		if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
609 			IPSEC_ISTAT(sproto, espstat.esps_hdrops,
610 			    ahstat.ahs_hdrops,
611 			    ipcompstat.ipcomps_hdrops);
612 			error = EINVAL;
613 			goto bad;
614 		}
615 		/* ipn will now contain the inner IPv4 header */
616 		m_copydata(m, skip, sizeof(struct ip), (caddr_t) &ipn);
617 
618 		/*
619 		 * Check that the inner source address is the same as
620 		 * the proxy address, if available.
621 		 */
622 		if ((saidx->proxy.sa.sa_family == AF_INET &&
623 		    saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
624 		    ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
625 		    (saidx->proxy.sa.sa_family != AF_INET &&
626 			saidx->proxy.sa.sa_family != 0)) {
627 
628 			DPRINTF(("%s: inner source address %s doesn't "
629 			    "correspond to expected proxy source %s, "
630 			    "SA %s/%08lx\n", __func__,
631 			    inet_ntoa4(ipn.ip_src),
632 			    ipsec_address(&saidx->proxy),
633 			    ipsec_address(&saidx->dst),
634 			    (u_long) ntohl(sav->spi)));
635 
636 			IPSEC_ISTATsproto, (espstat.esps_pdrops,
637 			    ahstat.ahs_pdrops, ipcompstat.ipcomps_pdrops);
638 			error = EACCES;
639 			goto bad;
640 		}
641 	}
642 #endif /* INET */
643 
644 	/* IPv6-in-IP encapsulation */
645 	if (prot == IPPROTO_IPV6) {
646 		struct ip6_hdr ip6n;
647 
648 		if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
649 			IPSEC_ISTAT(sproto, espstat.esps_hdrops,
650 			    ahstat.ahs_hdrops,
651 			    ipcompstat.ipcomps_hdrops);
652 			error = EINVAL;
653 			goto bad;
654 		}
655 		/* ip6n will now contain the inner IPv6 header. */
656 		m_copydata(m, skip, sizeof(struct ip6_hdr),
657 		    (caddr_t) &ip6n);
658 
659 		/*
660 		 * Check that the inner source address is the same as
661 		 * the proxy address, if available.
662 		 */
663 		if ((saidx->proxy.sa.sa_family == AF_INET6 &&
664 		    !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
665 		    !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
666 			&saidx->proxy.sin6.sin6_addr)) ||
667 		    (saidx->proxy.sa.sa_family != AF_INET6 &&
668 			saidx->proxy.sa.sa_family != 0)) {
669 
670 			DPRINTF(("%s: inner source address %s doesn't "
671 			    "correspond to expected proxy source %s, "
672 			    "SA %s/%08lx\n", __func__,
673 			    ip6_sprintf(ip6buf, &ip6n.ip6_src),
674 			    ipsec_address(&saidx->proxy),
675 			    ipsec_address(&saidx->dst),
676 			    (u_long) ntohl(sav->spi)));
677 
678 			IPSEC_ISTAT(sproto, espstat.esps_pdrops,
679 			    ahstat.ahs_pdrops, ipcompstat.ipcomps_pdrops);
680 			error = EACCES;
681 			goto bad;
682 		}
683 	}
684 #endif /*XXX*/
685 
686 	/*
687 	 * Record what we've done to the packet (under what SA it was
688 	 * processed). If we've been passed an mtag, it means the packet
689 	 * was already processed by an ethernet/crypto combo card and
690 	 * thus has a tag attached with all the right information, but
691 	 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
692 	 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
693 	 */
694 	if (mt == NULL && sproto != IPPROTO_IPCOMP) {
695 		mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
696 		    sizeof(struct tdb_ident), M_NOWAIT);
697 		if (mtag == NULL) {
698 			DPRINTF(("%s: failed to get tag\n", __func__));
699 			IPSEC_ISTAT(sproto, espstat.esps_hdrops,
700 			    ahstat.ahs_hdrops, ipcompstat.ipcomps_hdrops);
701 			error = ENOMEM;
702 			goto bad;
703 		}
704 
705 		tdbi = (struct tdb_ident *)(mtag + 1);
706 		bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union));
707 		tdbi->proto = sproto;
708 		tdbi->spi = sav->spi;
709 		/* Cache those two for enc(4) in xform_ipip. */
710 		tdbi->alg_auth = sav->alg_auth;
711 		tdbi->alg_enc = sav->alg_enc;
712 
713 		m_tag_prepend(m, mtag);
714 	} else {
715 		if (mt != NULL)
716 			mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
717 		/* XXX do we need to mark m_flags??? */
718 	}
719 
720 	key_sa_recordxfer(sav, m);
721 
722 #ifdef DEV_ENC
723 	/*
724 	 * Pass the mbuf to enc0 for bpf and pfil. We will filter the IPIP
725 	 * packet later after it has been decapsulated.
726 	 */
727 	ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_BEFORE);
728 
729 	/* XXX-BZ does not make sense. */
730 	if (prot != IPPROTO_IPIP)
731 		if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
732 			return (error);
733 #endif
734 
735 	/* Retrieve new protocol */
736 	m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &nxt8);
737 
738 	/*
739 	 * See the end of ip6_input for this logic.
740 	 * IPPROTO_IPV[46] case will be processed just like other ones
741 	 */
742 	nest = 0;
743 	nxt = nxt8;
744 	while (nxt != IPPROTO_DONE) {
745 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
746 			ip6stat.ip6s_toomanyhdr++;
747 			error = EINVAL;
748 			goto bad;
749 		}
750 
751 		/*
752 		 * Protection against faulty packet - there should be
753 		 * more sanity checks in header chain processing.
754 		 */
755 		if (m->m_pkthdr.len < skip) {
756 			ip6stat.ip6s_tooshort++;
757 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
758 			error = EINVAL;
759 			goto bad;
760 		}
761 		/*
762 		 * Enforce IPsec policy checking if we are seeing last header.
763 		 * note that we do not visit this with protocols with pcb layer
764 		 * code - like udp/tcp/raw ip.
765 		 */
766 		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
767 		    ipsec6_in_reject(m, NULL)) {
768 			error = EINVAL;
769 			goto bad;
770 		}
771 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
772 	}
773 	return 0;
774 bad:
775 	if (m)
776 		m_freem(m);
777 	return error;
778 }
779 
780 void
781 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
782 {
783 	struct ip6ctlparam *ip6cp = NULL;
784 	struct mbuf *m = NULL;
785 	struct ip6_hdr *ip6;
786 	int off;
787 
788 	if (sa->sa_family != AF_INET6 ||
789 	    sa->sa_len != sizeof(struct sockaddr_in6))
790 		return;
791 	if ((unsigned)cmd >= PRC_NCMDS)
792 		return;
793 
794 	/* if the parameter is from icmp6, decode it. */
795 	if (d != NULL) {
796 		ip6cp = (struct ip6ctlparam *)d;
797 		m = ip6cp->ip6c_m;
798 		ip6 = ip6cp->ip6c_ip6;
799 		off = ip6cp->ip6c_off;
800 	} else {
801 		m = NULL;
802 		ip6 = NULL;
803 		off = 0;	/* calm gcc */
804 	}
805 
806 	if (ip6 != NULL) {
807 
808 		struct ip6ctlparam ip6cp1;
809 
810 		/*
811 		 * Notify the error to all possible sockets via pfctlinput2.
812 		 * Since the upper layer information (such as protocol type,
813 		 * source and destination ports) is embedded in the encrypted
814 		 * data and might have been cut, we can't directly call
815 		 * an upper layer ctlinput function. However, the pcbnotify
816 		 * function will consider source and destination addresses
817 		 * as well as the flow info value, and may be able to find
818 		 * some PCB that should be notified.
819 		 * Although pfctlinput2 will call esp6_ctlinput(), there is
820 		 * no possibility of an infinite loop of function calls,
821 		 * because we don't pass the inner IPv6 header.
822 		 */
823 		bzero(&ip6cp1, sizeof(ip6cp1));
824 		ip6cp1.ip6c_src = ip6cp->ip6c_src;
825 		pfctlinput2(cmd, sa, (void *)&ip6cp1);
826 
827 		/*
828 		 * Then go to special cases that need ESP header information.
829 		 * XXX: We assume that when ip6 is non NULL,
830 		 * M and OFF are valid.
831 		 */
832 
833 		if (cmd == PRC_MSGSIZE) {
834 			struct secasvar *sav;
835 			u_int32_t spi;
836 			int valid;
837 
838 			/* check header length before using m_copydata */
839 			if (m->m_pkthdr.len < off + sizeof (struct esp))
840 				return;
841 			m_copydata(m, off + offsetof(struct esp, esp_spi),
842 				sizeof(u_int32_t), (caddr_t) &spi);
843 			/*
844 			 * Check to see if we have a valid SA corresponding to
845 			 * the address in the ICMP message payload.
846 			 */
847 			sav = KEY_ALLOCSA((union sockaddr_union *)sa,
848 					IPPROTO_ESP, spi);
849 			valid = (sav != NULL);
850 			if (sav)
851 				KEY_FREESAV(&sav);
852 
853 			/* XXX Further validation? */
854 
855 			/*
856 			 * Depending on whether the SA is "valid" and
857 			 * routing table size (mtudisc_{hi,lo}wat), we will:
858 			 * - recalcurate the new MTU and create the
859 			 *   corresponding routing entry, or
860 			 * - ignore the MTU change notification.
861 			 */
862 			icmp6_mtudisc_update(ip6cp, valid);
863 		}
864 	} else {
865 		/* we normally notify any pcb here */
866 	}
867 }
868 #endif /* INET6 */
869