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