xref: /freebsd/sys/netipsec/ipsec_input.c (revision 076ad2f8)
1 /*	$OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $	*/
2 /*-
3  * The authors of this code are John Ioannidis (ji@tla.org),
4  * Angelos D. Keromytis (kermit@csd.uch.gr) and
5  * Niels Provos (provos@physnet.uni-hamburg.de).
6  *
7  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
8  * in November 1995.
9  *
10  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
11  * by Angelos D. Keromytis.
12  *
13  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
14  * and Niels Provos.
15  *
16  * Additional features in 1999 by Angelos D. Keromytis.
17  *
18  * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
19  * Angelos D. Keromytis and Niels Provos.
20  * Copyright (c) 2001, Angelos D. Keromytis.
21  * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org>
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 <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 #include "opt_inet.h"
47 #include "opt_inet6.h"
48 #include "opt_ipsec.h"
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/malloc.h>
53 #include <sys/mbuf.h>
54 #include <sys/domain.h>
55 #include <sys/protosw.h>
56 #include <sys/socket.h>
57 #include <sys/errno.h>
58 #include <sys/hhook.h>
59 #include <sys/syslog.h>
60 
61 #include <net/if.h>
62 #include <net/if_var.h>
63 #include <net/if_enc.h>
64 #include <net/netisr.h>
65 #include <net/vnet.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 
73 #include <netinet/ip6.h>
74 #ifdef INET6
75 #include <netinet6/ip6_var.h>
76 #endif
77 #include <netinet/in_pcb.h>
78 #ifdef INET6
79 #include <netinet/icmp6.h>
80 #endif
81 
82 #include <netipsec/ipsec.h>
83 #ifdef INET6
84 #include <netipsec/ipsec6.h>
85 #endif
86 #include <netipsec/ah_var.h>
87 #include <netipsec/esp.h>
88 #include <netipsec/esp_var.h>
89 #include <netipsec/ipcomp_var.h>
90 
91 #include <netipsec/key.h>
92 #include <netipsec/keydb.h>
93 #include <netipsec/key_debug.h>
94 
95 #include <netipsec/xform.h>
96 #include <netinet6/ip6protosw.h>
97 
98 #include <machine/in_cksum.h>
99 #include <machine/stdarg.h>
100 
101 
102 #define	IPSEC_ISTAT(proto, name)	do {	\
103 	if ((proto) == IPPROTO_ESP)		\
104 		ESPSTAT_INC(esps_##name);	\
105 	else if ((proto) == IPPROTO_AH)		\
106 		AHSTAT_INC(ahs_##name);		\
107 	else					\
108 		IPCOMPSTAT_INC(ipcomps_##name);	\
109 } while (0)
110 
111 /*
112  * ipsec_common_input gets called when an IPsec-protected packet
113  * is received by IPv4 or IPv6.  Its job is to find the right SA
114  * and call the appropriate transform.  The transform callback
115  * takes care of further processing (like ingress filtering).
116  */
117 static int
118 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
119 {
120 	char buf[IPSEC_ADDRSTRLEN];
121 	union sockaddr_union dst_address;
122 	struct secasvar *sav;
123 	uint32_t spi;
124 	int error;
125 
126 	IPSEC_ISTAT(sproto, 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, pdrops);
139 		return EOPNOTSUPP;
140 	}
141 
142 	if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
143 		m_freem(m);
144 		IPSEC_ISTAT(sproto, hdrops);
145 		DPRINTF(("%s: packet too small\n", __func__));
146 		return EINVAL;
147 	}
148 
149 	/* Retrieve the SPI from the relevant IPsec header */
150 	if (sproto == IPPROTO_ESP)
151 		m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
152 	else if (sproto == IPPROTO_AH)
153 		m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
154 		    (caddr_t) &spi);
155 	else if (sproto == IPPROTO_IPCOMP) {
156 		u_int16_t cpi;
157 		m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
158 		    (caddr_t) &cpi);
159 		spi = ntohl(htons(cpi));
160 	}
161 
162 	/*
163 	 * Find the SA and (indirectly) call the appropriate
164 	 * kernel crypto routine. The resulting mbuf chain is a valid
165 	 * IP packet ready to go through input processing.
166 	 */
167 	bzero(&dst_address, sizeof (dst_address));
168 	dst_address.sa.sa_family = af;
169 	switch (af) {
170 #ifdef INET
171 	case AF_INET:
172 		dst_address.sin.sin_len = sizeof(struct sockaddr_in);
173 		m_copydata(m, offsetof(struct ip, ip_dst),
174 		    sizeof(struct in_addr),
175 		    (caddr_t) &dst_address.sin.sin_addr);
176 		break;
177 #endif /* INET */
178 #ifdef INET6
179 	case AF_INET6:
180 		dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
181 		m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
182 		    sizeof(struct in6_addr),
183 		    (caddr_t) &dst_address.sin6.sin6_addr);
184 		/* We keep addresses in SADB without embedded scope id */
185 		if (IN6_IS_SCOPE_LINKLOCAL(&dst_address.sin6.sin6_addr)) {
186 			/* XXX: sa6_recoverscope() */
187 			dst_address.sin6.sin6_scope_id =
188 			    ntohs(dst_address.sin6.sin6_addr.s6_addr16[1]);
189 			dst_address.sin6.sin6_addr.s6_addr16[1] = 0;
190 		}
191 		break;
192 #endif /* INET6 */
193 	default:
194 		DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
195 		m_freem(m);
196 		IPSEC_ISTAT(sproto, nopf);
197 		return EPFNOSUPPORT;
198 	}
199 
200 	/* NB: only pass dst since key_allocsa follows RFC2401 */
201 	sav = key_allocsa(&dst_address, sproto, spi);
202 	if (sav == NULL) {
203 		DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
204 		    __func__, ipsec_address(&dst_address, buf, sizeof(buf)),
205 		    (u_long) ntohl(spi), sproto));
206 		IPSEC_ISTAT(sproto, notdb);
207 		m_freem(m);
208 		return ENOENT;
209 	}
210 
211 	if (sav->tdb_xform == NULL) {
212 		DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
213 		    __func__, ipsec_address(&dst_address, buf, sizeof(buf)),
214 		    (u_long) ntohl(spi), sproto));
215 		IPSEC_ISTAT(sproto, noxform);
216 		key_freesav(&sav);
217 		m_freem(m);
218 		return ENXIO;
219 	}
220 
221 	/*
222 	 * Call appropriate transform and return -- callback takes care of
223 	 * everything else.
224 	 */
225 	error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
226 	if (error != 0)
227 		key_freesav(&sav);
228 	return (error);
229 }
230 
231 #ifdef INET
232 extern struct protosw inetsw[];
233 
234 /*
235  * IPSEC_INPUT() method implementation for IPv4.
236  *  0 - Permitted by inbound security policy for further processing.
237  *  EACCES - Forbidden by inbound security policy.
238  *  EINPROGRESS - consumed by IPsec.
239  */
240 int
241 ipsec4_input(struct mbuf *m, int offset, int proto)
242 {
243 
244 	switch (proto) {
245 	case IPPROTO_AH:
246 	case IPPROTO_ESP:
247 	case IPPROTO_IPCOMP:
248 		/* Do inbound IPsec processing for AH/ESP/IPCOMP */
249 		ipsec_common_input(m, offset,
250 		    offsetof(struct ip, ip_p), AF_INET, proto);
251 		return (EINPROGRESS); /* mbuf consumed by IPsec */
252 	default:
253 		/*
254 		 * Protocols with further headers get their IPsec treatment
255 		 * within the protocol specific processing.
256 		 */
257 		if ((inetsw[ip_protox[proto]].pr_flags & PR_LASTHDR) == 0)
258 			return (0);
259 		/* FALLTHROUGH */
260 	};
261 	/*
262 	 * Enforce IPsec policy checking if we are seeing last header.
263 	 */
264 	if (ipsec4_in_reject(m, NULL) != 0) {
265 		/* Forbidden by inbound security policy */
266 		m_freem(m);
267 		return (EACCES);
268 	}
269 	return (0);
270 }
271 
272 /*
273  * IPsec input callback for INET protocols.
274  * This routine is called as the transform callback.
275  * Takes care of filtering and other sanity checks on
276  * the processed packet.
277  */
278 int
279 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
280     int protoff)
281 {
282 	char buf[IPSEC_ADDRSTRLEN];
283 	struct ipsec_ctx_data ctx;
284 	struct xform_history *xh;
285 	struct secasindex *saidx;
286 	struct m_tag *mtag;
287 	struct ip *ip;
288 	int error, prot, af, sproto, isr_prot;
289 
290 	IPSEC_ASSERT(sav != NULL, ("null SA"));
291 	IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
292 	saidx = &sav->sah->saidx;
293 	af = saidx->dst.sa.sa_family;
294 	IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
295 	sproto = saidx->proto;
296 	IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
297 		sproto == IPPROTO_IPCOMP,
298 		("unexpected security protocol %u", sproto));
299 
300 	if (skip != 0) {
301 		/*
302 		 * Fix IPv4 header
303 		 */
304 		if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
305 			DPRINTF(("%s: processing failed for SA %s/%08lx\n",
306 			    __func__, ipsec_address(&sav->sah->saidx.dst,
307 			    buf, sizeof(buf)), (u_long) ntohl(sav->spi)));
308 			IPSEC_ISTAT(sproto, hdrops);
309 			error = ENOBUFS;
310 			goto bad;
311 		}
312 
313 		ip = mtod(m, struct ip *);
314 		ip->ip_len = htons(m->m_pkthdr.len);
315 		ip->ip_sum = 0;
316 		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
317 	} else {
318 		ip = mtod(m, struct ip *);
319 	}
320 	prot = ip->ip_p;
321 	/*
322 	 * Check that we have NAT-T enabled and apply transport mode
323 	 * decapsulation NAT procedure (RFC3948).
324 	 * Do this before invoking into the PFIL.
325 	 */
326 	if (sav->natt != NULL &&
327 	    (prot == IPPROTO_UDP || prot == IPPROTO_TCP))
328 		udp_ipsec_adjust_cksum(m, sav, prot, skip);
329 
330 	IPSEC_INIT_CTX(&ctx, &m, sav, AF_INET, IPSEC_ENC_BEFORE);
331 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
332 		goto bad;
333 	ip = mtod(m, struct ip *);	/* update pointer */
334 
335 	/* IP-in-IP encapsulation */
336 	if (prot == IPPROTO_IPIP &&
337 	    saidx->mode != IPSEC_MODE_TRANSPORT) {
338 		if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
339 			IPSEC_ISTAT(sproto, hdrops);
340 			error = EINVAL;
341 			goto bad;
342 		}
343 		/* enc0: strip outer IPv4 header */
344 		m_striphdr(m, 0, ip->ip_hl << 2);
345 	}
346 #ifdef INET6
347 	/* IPv6-in-IP encapsulation. */
348 	else if (prot == IPPROTO_IPV6 &&
349 	    saidx->mode != IPSEC_MODE_TRANSPORT) {
350 		if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
351 			IPSEC_ISTAT(sproto, hdrops);
352 			error = EINVAL;
353 			goto bad;
354 		}
355 		/* enc0: strip IPv4 header, keep IPv6 header only */
356 		m_striphdr(m, 0, ip->ip_hl << 2);
357 	}
358 #endif /* INET6 */
359 	else if (prot != IPPROTO_IPV6 && saidx->mode == IPSEC_MODE_ANY) {
360 		/*
361 		 * When mode is wildcard, inner protocol is IPv6 and
362 		 * we have no INET6 support - drop this packet a bit later.
363 		 * In other cases we assume transport mode. Set prot to
364 		 * correctly choose netisr.
365 		 */
366 		prot = IPPROTO_IPIP;
367 	}
368 
369 	/*
370 	 * Record what we've done to the packet (under what SA it was
371 	 * processed).
372 	 */
373 	if (sproto != IPPROTO_IPCOMP) {
374 		mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
375 		    sizeof(struct xform_history), M_NOWAIT);
376 		if (mtag == NULL) {
377 			DPRINTF(("%s: failed to get tag\n", __func__));
378 			IPSEC_ISTAT(sproto, hdrops);
379 			error = ENOMEM;
380 			goto bad;
381 		}
382 
383 		xh = (struct xform_history *)(mtag + 1);
384 		bcopy(&saidx->dst, &xh->dst, saidx->dst.sa.sa_len);
385 		xh->spi = sav->spi;
386 		xh->proto = sproto;
387 		xh->mode = saidx->mode;
388 		m_tag_prepend(m, mtag);
389 	}
390 
391 	key_sa_recordxfer(sav, m);		/* record data transfer */
392 
393 	/*
394 	 * In transport mode requeue decrypted mbuf back to IPv4 protocol
395 	 * handler. This is necessary to correctly expose rcvif.
396 	 */
397 	if (saidx->mode == IPSEC_MODE_TRANSPORT)
398 		prot = IPPROTO_IPIP;
399 	/*
400 	 * Re-dispatch via software interrupt.
401 	 */
402 	switch (prot) {
403 	case IPPROTO_IPIP:
404 		isr_prot = NETISR_IP;
405 		af = AF_INET;
406 		break;
407 #ifdef INET6
408 	case IPPROTO_IPV6:
409 		isr_prot = NETISR_IPV6;
410 		af = AF_INET6;
411 		break;
412 #endif
413 	default:
414 		DPRINTF(("%s: cannot handle inner ip proto %d\n",
415 			    __func__, prot));
416 		IPSEC_ISTAT(sproto, nopf);
417 		error = EPFNOSUPPORT;
418 		goto bad;
419 	}
420 
421 	IPSEC_INIT_CTX(&ctx, &m, sav, af, IPSEC_ENC_AFTER);
422 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
423 		goto bad;
424 
425 	/* Handle virtual tunneling interfaces */
426 	if (saidx->mode == IPSEC_MODE_TUNNEL)
427 		error = ipsec_if_input(m, sav, af);
428 	if (error == 0) {
429 		error = netisr_queue_src(isr_prot, (uintptr_t)sav->spi, m);
430 		if (error) {
431 			IPSEC_ISTAT(sproto, qfull);
432 			DPRINTF(("%s: queue full; proto %u packet dropped\n",
433 			    __func__, sproto));
434 		}
435 	}
436 	key_freesav(&sav);
437 	return (error);
438 bad:
439 	key_freesav(&sav);
440 	if (m != NULL)
441 		m_freem(m);
442 	return (error);
443 }
444 #endif /* INET */
445 
446 #ifdef INET6
447 /*
448  * IPSEC_INPUT() method implementation for IPv6.
449  *  0 - Permitted by inbound security policy for further processing.
450  *  EACCES - Forbidden by inbound security policy.
451  *  EINPROGRESS - consumed by IPsec.
452  */
453 int
454 ipsec6_input(struct mbuf *m, int offset, int proto)
455 {
456 
457 	switch (proto) {
458 	case IPPROTO_AH:
459 	case IPPROTO_ESP:
460 	case IPPROTO_IPCOMP:
461 		/* Do inbound IPsec processing for AH/ESP/IPCOMP */
462 		ipsec_common_input(m, offset,
463 		    offsetof(struct ip6_hdr, ip6_nxt), AF_INET6, proto);
464 		return (EINPROGRESS); /* mbuf consumed by IPsec */
465 	default:
466 		/*
467 		 * Protocols with further headers get their IPsec treatment
468 		 * within the protocol specific processing.
469 		 */
470 		if ((inet6sw[ip6_protox[proto]].pr_flags & PR_LASTHDR) == 0)
471 			return (0);
472 		/* FALLTHROUGH */
473 	};
474 	/*
475 	 * Enforce IPsec policy checking if we are seeing last header.
476 	 */
477 	if (ipsec6_in_reject(m, NULL) != 0) {
478 		/* Forbidden by inbound security policy */
479 		m_freem(m);
480 		return (EACCES);
481 	}
482 	return (0);
483 }
484 
485 /*
486  * IPsec input callback, called by the transform callback. Takes care of
487  * filtering and other sanity checks on the processed packet.
488  */
489 int
490 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
491     int protoff)
492 {
493 	char buf[IPSEC_ADDRSTRLEN];
494 	struct ipsec_ctx_data ctx;
495 	struct xform_history *xh;
496 	struct secasindex *saidx;
497 	struct ip6_hdr *ip6;
498 	struct m_tag *mtag;
499 	int prot, af, sproto;
500 	int nxt, isr_prot;
501 	int error, nest;
502 	uint8_t nxt8;
503 
504 	IPSEC_ASSERT(sav != NULL, ("null SA"));
505 	IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
506 	saidx = &sav->sah->saidx;
507 	af = saidx->dst.sa.sa_family;
508 	IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
509 	sproto = saidx->proto;
510 	IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
511 		sproto == IPPROTO_IPCOMP,
512 		("unexpected security protocol %u", sproto));
513 
514 	/* Fix IPv6 header */
515 	if (m->m_len < sizeof(struct ip6_hdr) &&
516 	    (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
517 
518 		DPRINTF(("%s: processing failed for SA %s/%08lx\n",
519 		    __func__, ipsec_address(&sav->sah->saidx.dst, buf,
520 		    sizeof(buf)), (u_long) ntohl(sav->spi)));
521 
522 		IPSEC_ISTAT(sproto, hdrops);
523 		error = EACCES;
524 		goto bad;
525 	}
526 
527 	IPSEC_INIT_CTX(&ctx, &m, sav, af, IPSEC_ENC_BEFORE);
528 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
529 		goto bad;
530 
531 	ip6 = mtod(m, struct ip6_hdr *);
532 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
533 
534 	/* Save protocol */
535 	m_copydata(m, protoff, 1, &nxt8);
536 	prot = nxt8;
537 
538 	/* IPv6-in-IP encapsulation */
539 	if (prot == IPPROTO_IPV6 &&
540 	    saidx->mode != IPSEC_MODE_TRANSPORT) {
541 		if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
542 			IPSEC_ISTAT(sproto, hdrops);
543 			error = EINVAL;
544 			goto bad;
545 		}
546 		/* ip6n will now contain the inner IPv6 header. */
547 		m_striphdr(m, 0, skip);
548 		skip = 0;
549 	}
550 #ifdef INET
551 	/* IP-in-IP encapsulation */
552 	else if (prot == IPPROTO_IPIP &&
553 	    saidx->mode != IPSEC_MODE_TRANSPORT) {
554 		if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
555 			IPSEC_ISTAT(sproto, hdrops);
556 			error = EINVAL;
557 			goto bad;
558 		}
559 		/* ipn will now contain the inner IPv4 header */
560 		m_striphdr(m, 0, skip);
561 		skip = 0;
562 	}
563 #endif /* INET */
564 	else {
565 		prot = IPPROTO_IPV6; /* for correct BPF processing */
566 	}
567 
568 	/*
569 	 * Record what we've done to the packet (under what SA it was
570 	 * processed).
571 	 */
572 	if (sproto != IPPROTO_IPCOMP) {
573 		mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
574 		    sizeof(struct xform_history), M_NOWAIT);
575 		if (mtag == NULL) {
576 			DPRINTF(("%s: failed to get tag\n", __func__));
577 			IPSEC_ISTAT(sproto, hdrops);
578 			error = ENOMEM;
579 			goto bad;
580 		}
581 
582 		xh = (struct xform_history *)(mtag + 1);
583 		bcopy(&saidx->dst, &xh->dst, saidx->dst.sa.sa_len);
584 		xh->spi = sav->spi;
585 		xh->proto = sproto;
586 		xh->mode = saidx->mode;
587 		m_tag_prepend(m, mtag);
588 	}
589 
590 	key_sa_recordxfer(sav, m);
591 
592 #ifdef INET
593 	if (prot == IPPROTO_IPIP)
594 		af = AF_INET;
595 	else
596 #endif
597 		af = AF_INET6;
598 	IPSEC_INIT_CTX(&ctx, &m, sav, af, IPSEC_ENC_AFTER);
599 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
600 		goto bad;
601 	if (skip == 0) {
602 		/*
603 		 * We stripped outer IPv6 header.
604 		 * Now we should requeue decrypted packet via netisr.
605 		 */
606 		switch (prot) {
607 #ifdef INET
608 		case IPPROTO_IPIP:
609 			isr_prot = NETISR_IP;
610 			break;
611 #endif
612 		case IPPROTO_IPV6:
613 			isr_prot = NETISR_IPV6;
614 			break;
615 		default:
616 			DPRINTF(("%s: cannot handle inner ip proto %d\n",
617 			    __func__, prot));
618 			IPSEC_ISTAT(sproto, nopf);
619 			error = EPFNOSUPPORT;
620 			goto bad;
621 		}
622 		/* Handle virtual tunneling interfaces */
623 		if (saidx->mode == IPSEC_MODE_TUNNEL)
624 			error = ipsec_if_input(m, sav, af);
625 		if (error == 0) {
626 			error = netisr_queue_src(isr_prot,
627 			    (uintptr_t)sav->spi, m);
628 			if (error) {
629 				IPSEC_ISTAT(sproto, qfull);
630 				DPRINTF(("%s: queue full; proto %u packet"
631 				    " dropped\n", __func__, sproto));
632 			}
633 		}
634 		key_freesav(&sav);
635 		return (error);
636 	}
637 	/*
638 	 * See the end of ip6_input for this logic.
639 	 * IPPROTO_IPV[46] case will be processed just like other ones
640 	 */
641 	nest = 0;
642 	nxt = nxt8;
643 	while (nxt != IPPROTO_DONE) {
644 		if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
645 			IP6STAT_INC(ip6s_toomanyhdr);
646 			error = EINVAL;
647 			goto bad;
648 		}
649 
650 		/*
651 		 * Protection against faulty packet - there should be
652 		 * more sanity checks in header chain processing.
653 		 */
654 		if (m->m_pkthdr.len < skip) {
655 			IP6STAT_INC(ip6s_tooshort);
656 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
657 			error = EINVAL;
658 			goto bad;
659 		}
660 		/*
661 		 * Enforce IPsec policy checking if we are seeing last header.
662 		 * note that we do not visit this with protocols with pcb layer
663 		 * code - like udp/tcp/raw ip.
664 		 */
665 		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
666 		    ipsec6_in_reject(m, NULL)) {
667 			error = EINVAL;
668 			goto bad;
669 		}
670 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
671 	}
672 	key_freesav(&sav);
673 	return (0);
674 bad:
675 	key_freesav(&sav);
676 	if (m)
677 		m_freem(m);
678 	return (error);
679 }
680 #endif /* INET6 */
681