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