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