xref: /netbsd-src/sys/netipsec/ipsec_input.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1 /*	$NetBSD: ipsec_input.c,v 1.29 2012/01/25 21:58:10 drochner 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.29 2012/01/25 21:58:10 drochner 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 
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/in_var.h>
72 #include <netinet/in_proto.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 	/* Fix IPv4 header */
320 	if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
321 		DPRINTF(("ipsec4_common_input_cb: processing failed "
322 		    "for SA %s/%08lx\n",
323 		    ipsec_address(&sav->sah->saidx.dst),
324 		    (u_long) ntohl(sav->spi)));
325 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
326 		    IPCOMP_STAT_HDROPS);
327 		error = ENOBUFS;
328 		goto bad;
329 	}
330 
331 	ip = mtod(m, struct ip *);
332 	ip->ip_len = htons(m->m_pkthdr.len);
333 	prot = ip->ip_p;
334 
335 	/* IP-in-IP encapsulation */
336 	if (prot == IPPROTO_IPIP) {
337 		struct ip ipn;
338 
339 		/* ipn will now contain the inner IPv4 header */
340 		m_copydata(m, ip->ip_hl << 2, sizeof(struct ip), &ipn);
341 
342 #ifdef notyet
343 		/* XXX PROXY address isn't recorded in SAH */
344 		/*
345 		 * Check that the inner source address is the same as
346 		 * the proxy address, if available.
347 		 */
348 		if ((saidx->proxy.sa.sa_family == AF_INET &&
349 		    saidx->proxy.sin.sin_addr.s_addr !=
350 		    INADDR_ANY &&
351 		    ipn.ip_src.s_addr !=
352 		    saidx->proxy.sin.sin_addr.s_addr) ||
353 		    (saidx->proxy.sa.sa_family != AF_INET &&
354 			saidx->proxy.sa.sa_family != 0)) {
355 
356 			DPRINTF(("ipsec4_common_input_cb: inner "
357 			    "source address %s doesn't correspond to "
358 			    "expected proxy source %s, SA %s/%08lx\n",
359 			    inet_ntoa4(ipn.ip_src),
360 			    ipsp_address(saidx->proxy),
361 			    ipsp_address(saidx->dst),
362 			    (u_long) ntohl(sav->spi)));
363 
364 			IPSEC_ISTAT(sproto, ESP_STAT_PDROPS,
365 			    AH_STAT_PDROPS,
366 			    IPCOMP_STAT_PDROPS);
367 			error = EACCES;
368 			goto bad;
369 		}
370 #endif /*XXX*/
371 	}
372 #if INET6
373 	/* IPv6-in-IP encapsulation. */
374 	if (prot == IPPROTO_IPV6) {
375 		struct ip6_hdr ip6n;
376 
377 		/* ip6n will now contain the inner IPv6 header. */
378 		m_copydata(m, ip->ip_hl << 2, sizeof(struct ip6_hdr), &ip6n);
379 
380 #ifdef notyet
381 		/*
382 		 * Check that the inner source address is the same as
383 		 * the proxy address, if available.
384 		 */
385 		if ((saidx->proxy.sa.sa_family == AF_INET6 &&
386 		    !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
387 		    !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
388 			&saidx->proxy.sin6.sin6_addr)) ||
389 		    (saidx->proxy.sa.sa_family != AF_INET6 &&
390 			saidx->proxy.sa.sa_family != 0)) {
391 
392 			DPRINTF(("ipsec4_common_input_cb: inner "
393 			    "source address %s doesn't correspond to "
394 			    "expected proxy source %s, SA %s/%08lx\n",
395 			    ip6_sprintf(&ip6n.ip6_src),
396 			    ipsec_address(&saidx->proxy),
397 			    ipsec_address(&saidx->dst),
398 			    (u_long) ntohl(sav->spi)));
399 
400 			IPSEC_ISTAT(sproto, ESP_STAT_PDROPS,
401 			    AH_STAT_PDROPS,
402 			    IPCOMP_STAT_PDROPS);
403 			error = EACCES;
404 			goto bad;
405 		}
406 #endif /*XXX*/
407 	}
408 #endif /* INET6 */
409 
410 	/*
411 	 * Record what we've done to the packet (under what SA it was
412 	 * processed). If we've been passed an mtag, it means the packet
413 	 * was already processed by an ethernet/crypto combo card and
414 	 * thus has a tag attached with all the right information, but
415 	 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
416 	 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
417 	 */
418 	if (mt == NULL && sproto != IPPROTO_IPCOMP) {
419 		mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
420 		    sizeof(struct tdb_ident), M_NOWAIT);
421 		if (mtag == NULL) {
422 			DPRINTF(("ipsec4_common_input_cb: failed to get tag\n"));
423 			IPSEC_ISTAT(sproto, ESP_STAT_HDROPS,
424 			    AH_STAT_HDROPS, IPCOMP_STAT_HDROPS);
425 			error = ENOMEM;
426 			goto bad;
427 		}
428 
429 		tdbi = (struct tdb_ident *)(mtag + 1);
430 		memcpy(&tdbi->dst, &saidx->dst, saidx->dst.sa.sa_len);
431 		tdbi->proto = sproto;
432 		tdbi->spi = sav->spi;
433 
434 		m_tag_prepend(m, mtag);
435 	} else {
436 		if (mt != NULL)
437 			mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
438 			/* XXX do we need to mark m_flags??? */
439 	}
440 
441 	key_sa_recordxfer(sav, m);		/* record data transfer */
442 
443 	if ((inetsw[ip_protox[prot]].pr_flags & PR_LASTHDR) != 0 &&
444 				ipsec4_in_reject(m, NULL)) {
445 		error = EINVAL;
446 		goto bad;
447 	}
448 	(*inetsw[ip_protox[prot]].pr_input)(m, skip, prot);
449 	return 0;
450 bad:
451 	m_freem(m);
452 	return error;
453 }
454 #endif /* INET */
455 
456 #ifdef INET6
457 /* IPv6 AH wrapper. */
458 int
459 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
460 {
461 	int l = 0;
462 	int protoff, nxt;
463 	struct ip6_ext ip6e;
464 
465 	if (*offp < sizeof(struct ip6_hdr)) {
466 		DPRINTF(("ipsec6_common_input: bad offset %u\n", *offp));
467 		IPSEC_ISTAT(proto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
468 			    IPCOMP_STAT_HDROPS);
469 		m_freem(*mp);
470 		return IPPROTO_DONE;
471 	} else if (*offp == sizeof(struct ip6_hdr)) {
472 		protoff = offsetof(struct ip6_hdr, ip6_nxt);
473 	} else {
474 		/* Chase down the header chain... */
475 		protoff = sizeof(struct ip6_hdr);
476 		nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt;
477 
478 		do {
479 			protoff += l;
480 			m_copydata(*mp, protoff, sizeof(ip6e), &ip6e);
481 
482 			if (nxt == IPPROTO_AH)
483 				l = (ip6e.ip6e_len + 2) << 2;
484 			else
485 				l = (ip6e.ip6e_len + 1) << 3;
486 			IPSEC_ASSERT(l > 0,
487 			  ("ipsec6_common_input: l went zero or negative"));
488 
489 			nxt = ip6e.ip6e_nxt;
490 		} while (protoff + l < *offp);
491 
492 		/* Malformed packet check */
493 		if (protoff + l != *offp) {
494 			DPRINTF(("ipsec6_common_input: bad packet header chain, "
495 				"protoff %u, l %u, off %u\n", protoff, l, *offp));
496 			IPSEC_ISTAT(proto, ESP_STAT_HDROPS,
497 				    AH_STAT_HDROPS,
498 				    IPCOMP_STAT_HDROPS);
499 			m_freem(*mp);
500 			*mp = NULL;
501 			return IPPROTO_DONE;
502 		}
503 		protoff += offsetof(struct ip6_ext, ip6e_nxt);
504 	}
505 	(void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
506 	return IPPROTO_DONE;
507 }
508 
509 /*
510  * NB: ipsec_netbsd.c has a duplicate definition of esp6_ctlinput(),
511  * with slightly ore recent multicast tests. These should be merged.
512  * For now, ifdef accordingly.
513  */
514 #ifdef __FreeBSD__
515 void
516 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
517 {
518 	if (sa->sa_family != AF_INET6 ||
519 	    sa->sa_len != sizeof(struct sockaddr_in6))
520 		return;
521 	if ((unsigned)cmd >= PRC_NCMDS)
522 		return;
523 
524 	/* if the parameter is from icmp6, decode it. */
525 	if (d !=  NULL) {
526 		struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
527 		struct mbuf *m = ip6cp->ip6c_m;
528 		int off = ip6cp->ip6c_off;
529 
530 		struct ip6ctlparam ip6cp1;
531 
532 		/*
533 		 * Notify the error to all possible sockets via pfctlinput2.
534 		 * Since the upper layer information (such as protocol type,
535 		 * source and destination ports) is embedded in the encrypted
536 		 * data and might have been cut, we can't directly call
537 		 * an upper layer ctlinput function. However, the pcbnotify
538 		 * function will consider source and destination addresses
539 		 * as well as the flow info value, and may be able to find
540 		 * some PCB that should be notified.
541 		 * Although pfctlinput2 will call esp6_ctlinput(), there is
542 		 * no possibility of an infinite loop of function calls,
543 		 * because we don't pass the inner IPv6 header.
544 		 */
545 		memset(&ip6cp1, 0, sizeof(ip6cp1));
546 		ip6cp1.ip6c_src = ip6cp->ip6c_src;
547 		pfctlinput2(cmd, sa, &ip6cp1);
548 
549 		/*
550 		 * Then go to special cases that need ESP header information.
551 		 * XXX: We assume that when ip6 is non NULL,
552 		 * M and OFF are valid.
553 		 */
554 
555 		if (cmd == PRC_MSGSIZE) {
556 			struct secasvar *sav;
557 			u_int32_t spi;
558 			int valid;
559 
560 			/* check header length before using m_copydata */
561 			if (m->m_pkthdr.len < off + sizeof (struct esp))
562 				return;
563 			m_copydata(m, off + offsetof(struct esp, esp_spi),
564 				sizeof(u_int32_t), &spi);
565 			/*
566 			 * Check to see if we have a valid SA corresponding to
567 			 * the address in the ICMP message payload.
568 			 */
569 			sav = KEY_ALLOCSA((union sockaddr_union *)sa,
570 					IPPROTO_ESP, spi);
571 			valid = (sav != NULL);
572 			if (sav)
573 				KEY_FREESAV(&sav);
574 
575 			/* XXX Further validation? */
576 
577 			/*
578 			 * Depending on whether the SA is "valid" and
579 			 * routing table size (mtudisc_{hi,lo}wat), we will:
580 			 * - recalcurate the new MTU and create the
581 			 *   corresponding routing entry, or
582 			 * - ignore the MTU change notification.
583 			 */
584 			icmp6_mtudisc_update(ip6cp, valid);
585 		}
586 	} else {
587 		/* we normally notify any pcb here */
588 	}
589 }
590 #endif /* __FreeBSD__ */
591 
592 extern	const struct ip6protosw inet6sw[];
593 extern	u_char ip6_protox[];
594 
595 /*
596  * IPsec input callback, called by the transform callback. Takes care of
597  * filtering and other sanity checks on the processed packet.
598  */
599 int
600 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip, int protoff,
601     struct m_tag *mt)
602 {
603 	int af, sproto;
604 	struct ip6_hdr *ip6;
605 	struct m_tag *mtag;
606 	struct tdb_ident *tdbi;
607 	struct secasindex *saidx;
608 	int nxt;
609 	u_int8_t prot, nxt8;
610 	int error, nest;
611 
612 	IPSEC_ASSERT(m != NULL, ("ipsec6_common_input_cb: null mbuf"));
613 	IPSEC_ASSERT(sav != NULL, ("ipsec6_common_input_cb: null SA"));
614 	IPSEC_ASSERT(sav->sah != NULL, ("ipsec6_common_input_cb: null SAH"));
615 	saidx = &sav->sah->saidx;
616 	af = saidx->dst.sa.sa_family;
617 	IPSEC_ASSERT(af == AF_INET6,
618 		("ipsec6_common_input_cb: unexpected af %u", af));
619 	sproto = saidx->proto;
620 	IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
621 		sproto == IPPROTO_IPCOMP,
622 		("ipsec6_common_input_cb: unexpected security protocol %u",
623 		sproto));
624 
625 	/* Sanity check */
626 	if (m == NULL) {
627 		DPRINTF(("ipsec6_common_input_cb: null mbuf"));
628 		IPSEC_ISTAT(sproto, ESP_STAT_BADKCR, AH_STAT_BADKCR,
629 		    IPCOMP_STAT_BADKCR);
630 		error = EINVAL;
631 		goto bad;
632 	}
633 
634 	/* Fix IPv6 header */
635 	if (m->m_len < sizeof(struct ip6_hdr) &&
636 	    (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
637 
638 		DPRINTF(("ipsec6_common_input_cb: processing failed "
639 		    "for SA %s/%08lx\n", ipsec_address(&sav->sah->saidx.dst),
640 		    (u_long) ntohl(sav->spi)));
641 
642 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
643 		    IPCOMP_STAT_HDROPS);
644 		error = EACCES;
645 		goto bad;
646 	}
647 
648 	ip6 = mtod(m, struct ip6_hdr *);
649 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
650 
651 	/* Save protocol */
652 	m_copydata(m, protoff, 1, &prot);
653 
654 #ifdef INET
655 	/* IP-in-IP encapsulation */
656 	if (prot == IPPROTO_IPIP) {
657 		struct ip ipn;
658 
659 		/* ipn will now contain the inner IPv4 header */
660 		m_copydata(m, skip, sizeof(struct ip), &ipn);
661 
662 #ifdef notyet
663 		/*
664 		 * Check that the inner source address is the same as
665 		 * the proxy address, if available.
666 		 */
667 		if ((saidx->proxy.sa.sa_family == AF_INET &&
668 		    saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
669 		    ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
670 		    (saidx->proxy.sa.sa_family != AF_INET &&
671 			saidx->proxy.sa.sa_family != 0)) {
672 
673 			DPRINTF(("ipsec6_common_input_cb: inner "
674 			    "source address %s doesn't correspond to "
675 			    "expected proxy source %s, SA %s/%08lx\n",
676 			    inet_ntoa4(ipn.ip_src),
677 			    ipsec_address(&saidx->proxy),
678 			    ipsec_address(&saidx->dst),
679 			    (u_long) ntohl(sav->spi)));
680 
681 			IPSEC_ISTAT(sproto, ESP_STAT_PDROPS,
682 			    AH_STAT_PDROPS, IPCOMP_STAT_PDROPS);
683 			error = EACCES;
684 			goto bad;
685 		}
686 #endif /*XXX*/
687 	}
688 #endif /* INET */
689 
690 	/* IPv6-in-IP encapsulation */
691 	if (prot == IPPROTO_IPV6) {
692 		struct ip6_hdr ip6n;
693 
694 		/* ip6n will now contain the inner IPv6 header. */
695 		m_copydata(m, skip, sizeof(struct ip6_hdr), &ip6n);
696 
697 #ifdef notyet
698 		/*
699 		 * Check that the inner source address is the same as
700 		 * the proxy address, if available.
701 		 */
702 		if ((saidx->proxy.sa.sa_family == AF_INET6 &&
703 		    !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
704 		    !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
705 			&saidx->proxy.sin6.sin6_addr)) ||
706 		    (saidx->proxy.sa.sa_family != AF_INET6 &&
707 			saidx->proxy.sa.sa_family != 0)) {
708 
709 			DPRINTF(("ipsec6_common_input_cb: inner "
710 			    "source address %s doesn't correspond to "
711 			    "expected proxy source %s, SA %s/%08lx\n",
712 			    ip6_sprintf(&ip6n.ip6_src),
713 			    ipsec_address(&saidx->proxy),
714 			    ipsec_address(&saidx->dst),
715 			    (u_long) ntohl(sav->spi)));
716 
717 			IPSEC_ISTAT(sproto, ESP_STAT_PDROPS,
718 			    AH_STAT_PDROPS, IPCOMP_STAT_PDROPS);
719 			error = EACCES;
720 			goto bad;
721 		}
722 #endif /*XXX*/
723 	}
724 
725 	/*
726 	 * Record what we've done to the packet (under what SA it was
727 	 * processed). If we've been passed an mtag, it means the packet
728 	 * was already processed by an ethernet/crypto combo card and
729 	 * thus has a tag attached with all the right information, but
730 	 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
731 	 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
732 	 */
733 	if (mt == NULL && sproto != IPPROTO_IPCOMP) {
734 		mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
735 		    sizeof(struct tdb_ident), M_NOWAIT);
736 		if (mtag == NULL) {
737 			DPRINTF(("ipsec_common_input_cb: failed to "
738 			    "get tag\n"));
739 			IPSEC_ISTAT(sproto, ESP_STAT_HDROPS,
740 			    AH_STAT_HDROPS, IPCOMP_STAT_HDROPS);
741 			error = ENOMEM;
742 			goto bad;
743 		}
744 
745 		tdbi = (struct tdb_ident *)(mtag + 1);
746 		memcpy(&tdbi->dst, &saidx->dst, sizeof(union sockaddr_union));
747 		tdbi->proto = sproto;
748 		tdbi->spi = sav->spi;
749 
750 		m_tag_prepend(m, mtag);
751 	} else {
752 		if (mt != NULL)
753 			mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
754 			/* XXX do we need to mark m_flags??? */
755 	}
756 
757 	key_sa_recordxfer(sav, m);
758 
759 	/* Retrieve new protocol */
760 	m_copydata(m, protoff, sizeof(u_int8_t), &nxt8);
761 
762 	/*
763 	 * See the end of ip6_input for this logic.
764 	 * IPPROTO_IPV[46] case will be processed just like other ones
765 	 */
766 	nest = 0;
767 	nxt = nxt8;
768 	while (nxt != IPPROTO_DONE) {
769 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
770 			IP6_STATINC(IP6_STAT_TOOMANYHDR);
771 			error = EINVAL;
772 			goto bad;
773 		}
774 
775 		/*
776 		 * Protection against faulty packet - there should be
777 		 * more sanity checks in header chain processing.
778 		 */
779 		if (m->m_pkthdr.len < skip) {
780 			IP6_STATINC(IP6_STAT_TOOSHORT);
781 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
782 			error = EINVAL;
783 			goto bad;
784 		}
785 		/*
786 		 * Enforce IPsec policy checking if we are seeing last header.
787 		 * note that we do not visit this with protocols with pcb layer
788 		 * code - like udp/tcp/raw ip.
789 		 */
790 		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
791 		    ipsec6_in_reject(m, NULL)) {
792 			error = EINVAL;
793 			goto bad;
794 		}
795 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
796 	}
797 	return 0;
798 bad:
799 	if (m)
800 		m_freem(m);
801 	return error;
802 }
803 #endif /* INET6 */
804