xref: /openbsd-src/sys/netinet/ipsec_input.c (revision 3e8b1db74c103e81d872d6148750233658a8adf7)
1 /*	$OpenBSD: ipsec_input.c,v 1.180 2021/09/29 22:08:13 bluhm 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  *
22  * Permission to use, copy, and modify this software with or without fee
23  * is hereby granted, provided that this entire notice is included in
24  * all copies of any software which is or includes a copy or
25  * modification of this software.
26  * You may use this code under the GNU public license if you so wish. Please
27  * contribute changes back to the authors under this freer than GPL license
28  * so that we may further the use of strong encryption without limitations to
29  * all.
30  *
31  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
32  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
33  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
34  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
35  * PURPOSE.
36  */
37 
38 #include "pf.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/protosw.h>
43 #include <sys/mbuf.h>
44 #include <sys/socket.h>
45 #include <sys/sysctl.h>
46 #include <sys/kernel.h>
47 #include <sys/timeout.h>
48 
49 #include <net/if.h>
50 #include <net/if_var.h>
51 #include <net/netisr.h>
52 #include <net/bpf.h>
53 #include <net/route.h>
54 
55 #include <netinet/in.h>
56 #include <netinet/ip.h>
57 #include <netinet/ip_var.h>
58 #include <netinet/ip_icmp.h>
59 #include <netinet/tcp.h>
60 #include <netinet/udp.h>
61 
62 #if NPF > 0
63 #include <net/pfvar.h>
64 #endif
65 
66 #ifdef INET6
67 #include <netinet6/in6_var.h>
68 #include <netinet/ip6.h>
69 #include <netinet6/ip6_var.h>
70 #include <netinet6/ip6protosw.h>
71 #endif /* INET6 */
72 
73 #include <netinet/ip_ipsp.h>
74 #include <netinet/ip_esp.h>
75 #include <netinet/ip_ah.h>
76 #include <netinet/ip_ipcomp.h>
77 
78 #include <net/if_enc.h>
79 
80 #include <crypto/cryptodev.h>
81 #include <crypto/xform.h>
82 
83 #include "bpfilter.h"
84 
85 void ipsec_common_ctlinput(u_int, int, struct sockaddr *, void *, int);
86 
87 #ifdef ENCDEBUG
88 #define DPRINTF(fmt, args...)						\
89 	do {								\
90 		if (encdebug)						\
91 			printf("%s: " fmt "\n", __func__, ## args);	\
92 	} while (0)
93 #else
94 #define DPRINTF(fmt, args...)						\
95 	do { } while (0)
96 #endif
97 
98 /* sysctl variables */
99 int encdebug = 0;
100 int ipsec_keep_invalid = IPSEC_DEFAULT_EMBRYONIC_SA_TIMEOUT;
101 int ipsec_require_pfs = IPSEC_DEFAULT_PFS;
102 int ipsec_soft_allocations = IPSEC_DEFAULT_SOFT_ALLOCATIONS;
103 int ipsec_exp_allocations = IPSEC_DEFAULT_EXP_ALLOCATIONS;
104 int ipsec_soft_bytes = IPSEC_DEFAULT_SOFT_BYTES;
105 int ipsec_exp_bytes = IPSEC_DEFAULT_EXP_BYTES;
106 int ipsec_soft_timeout = IPSEC_DEFAULT_SOFT_TIMEOUT;
107 int ipsec_exp_timeout = IPSEC_DEFAULT_EXP_TIMEOUT;
108 int ipsec_soft_first_use = IPSEC_DEFAULT_SOFT_FIRST_USE;
109 int ipsec_exp_first_use = IPSEC_DEFAULT_EXP_FIRST_USE;
110 int ipsec_expire_acquire = IPSEC_DEFAULT_EXPIRE_ACQUIRE;
111 
112 int esp_enable = 1;
113 int ah_enable = 1;
114 int ipcomp_enable = 0;
115 
116 const struct sysctl_bounded_args espctl_vars[] = {
117 	{ESPCTL_ENABLE, &esp_enable, 0, 1},
118 	{ESPCTL_UDPENCAP_ENABLE, &udpencap_enable, 0, 1},
119 	{ESPCTL_UDPENCAP_PORT, &udpencap_port, 0, 65535},
120 };
121 const struct sysctl_bounded_args ahctl_vars[] = {
122 	{AHCTL_ENABLE, &ah_enable, 0, 1},
123 };
124 const struct sysctl_bounded_args ipcompctl_vars[] = {
125 	{IPCOMPCTL_ENABLE, &ipcomp_enable, 0, 1},
126 };
127 
128 struct cpumem *espcounters;
129 struct cpumem *ahcounters;
130 struct cpumem *ipcompcounters;
131 struct cpumem *ipseccounters;
132 
133 char ipsec_def_enc[20];
134 char ipsec_def_auth[20];
135 char ipsec_def_comp[20];
136 
137 const struct sysctl_bounded_args ipsecctl_vars[] = {
138 	{ IPSEC_ENCDEBUG, &encdebug, 0, 1 },
139 	{ IPSEC_EXPIRE_ACQUIRE, &ipsec_expire_acquire, 0, INT_MAX },
140 	{ IPSEC_EMBRYONIC_SA_TIMEOUT, &ipsec_keep_invalid, 0, INT_MAX },
141 	{ IPSEC_REQUIRE_PFS, &ipsec_require_pfs, 0, 1 },
142 	{ IPSEC_SOFT_ALLOCATIONS, &ipsec_soft_allocations, 0, INT_MAX },
143 	{ IPSEC_ALLOCATIONS, &ipsec_exp_allocations, 0, INT_MAX },
144 	{ IPSEC_SOFT_BYTES, &ipsec_soft_bytes, 0, INT_MAX },
145 	{ IPSEC_BYTES, &ipsec_exp_bytes, 0, INT_MAX },
146 	{ IPSEC_TIMEOUT, &ipsec_exp_timeout, 0, INT_MAX },
147 	{ IPSEC_SOFT_TIMEOUT, &ipsec_soft_timeout,0, INT_MAX },
148 	{ IPSEC_SOFT_FIRSTUSE, &ipsec_soft_first_use, 0, INT_MAX },
149 	{ IPSEC_FIRSTUSE, &ipsec_exp_first_use, 0, INT_MAX },
150 };
151 
152 int esp_sysctl_espstat(void *, size_t *, void *);
153 int ah_sysctl_ahstat(void *, size_t *, void *);
154 int ipcomp_sysctl_ipcompstat(void *, size_t *, void *);
155 int ipsec_sysctl_ipsecstat(void *, size_t *, void *);
156 
157 void
158 ipsec_init(void)
159 {
160 	espcounters = counters_alloc(esps_ncounters);
161 	ahcounters = counters_alloc(ahs_ncounters);
162 	ipcompcounters = counters_alloc(ipcomps_ncounters);
163 	ipseccounters = counters_alloc(ipsec_ncounters);
164 
165 	strlcpy(ipsec_def_enc, IPSEC_DEFAULT_DEF_ENC, sizeof(ipsec_def_enc));
166 	strlcpy(ipsec_def_auth, IPSEC_DEFAULT_DEF_AUTH, sizeof(ipsec_def_auth));
167 	strlcpy(ipsec_def_comp, IPSEC_DEFAULT_DEF_COMP, sizeof(ipsec_def_comp));
168 
169 	ipsp_init();
170 }
171 
172 /*
173  * ipsec_common_input() gets called when we receive an IPsec-protected packet
174  * in IPv4 or IPv6. All it does is find the right TDB and call the appropriate
175  * transform. The callback takes care of further processing (like ingress
176  * filtering).
177  */
178 int
179 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto,
180     int udpencap)
181 {
182 #define IPSEC_ISTAT(x,y,z) do {			\
183 	if (sproto == IPPROTO_ESP)		\
184 		espstat_inc(x);			\
185 	else if (sproto == IPPROTO_AH)		\
186 		ahstat_inc(y);			\
187 	else					\
188 		ipcompstat_inc(z);		\
189 } while (0)
190 
191 	union sockaddr_union dst_address;
192 	struct tdb *tdbp = NULL;
193 	struct ifnet *encif;
194 	u_int32_t spi;
195 	u_int16_t cpi;
196 	int error;
197 #ifdef ENCDEBUG
198 	char buf[INET6_ADDRSTRLEN];
199 #endif
200 
201 	NET_ASSERT_LOCKED();
202 
203 	ipsecstat_inc(ipsec_ipackets);
204 	ipsecstat_add(ipsec_ibytes, m->m_pkthdr.len);
205 	IPSEC_ISTAT(esps_input, ahs_input, ipcomps_input);
206 
207 	if (m == NULL) {
208 		DPRINTF("NULL packet received");
209 		IPSEC_ISTAT(esps_hdrops, ahs_hdrops, ipcomps_hdrops);
210 		return EINVAL;
211 	}
212 
213 	if ((sproto == IPPROTO_IPCOMP) && (m->m_flags & M_COMP)) {
214 		DPRINTF("repeated decompression");
215 		ipcompstat_inc(ipcomps_pdrops);
216 		error = EINVAL;
217 		goto drop;
218 	}
219 
220 	if (m->m_pkthdr.len - skip < 2 * sizeof(u_int32_t)) {
221 		DPRINTF("packet too small");
222 		IPSEC_ISTAT(esps_hdrops, ahs_hdrops, ipcomps_hdrops);
223 		error = EINVAL;
224 		goto drop;
225 	}
226 
227 	/* Retrieve the SPI from the relevant IPsec header */
228 	switch (sproto) {
229 	case IPPROTO_ESP:
230 		m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
231 		break;
232 	case IPPROTO_AH:
233 		m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
234 		    (caddr_t) &spi);
235 		break;
236 	case IPPROTO_IPCOMP:
237 		m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
238 		    (caddr_t) &cpi);
239 		spi = ntohl(htons(cpi));
240 		break;
241 	default:
242 		panic("%s: unknown/unsupported security protocol %d",
243 		    __func__, sproto);
244 	}
245 
246 	/*
247 	 * Find tunnel control block and (indirectly) call the appropriate
248 	 * kernel crypto routine. The resulting mbuf chain is a valid
249 	 * IP packet ready to go through input processing.
250 	 */
251 
252 	memset(&dst_address, 0, sizeof(dst_address));
253 	dst_address.sa.sa_family = af;
254 
255 	switch (af) {
256 	case AF_INET:
257 		dst_address.sin.sin_len = sizeof(struct sockaddr_in);
258 		m_copydata(m, offsetof(struct ip, ip_dst),
259 		    sizeof(struct in_addr),
260 		    (caddr_t) &(dst_address.sin.sin_addr));
261 		break;
262 
263 #ifdef INET6
264 	case AF_INET6:
265 		dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
266 		m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
267 		    sizeof(struct in6_addr),
268 		    (caddr_t) &(dst_address.sin6.sin6_addr));
269 		in6_recoverscope(&dst_address.sin6,
270 		    &dst_address.sin6.sin6_addr);
271 		break;
272 #endif /* INET6 */
273 
274 	default:
275 		DPRINTF("unsupported protocol family %d", af);
276 		IPSEC_ISTAT(esps_nopf, ahs_nopf, ipcomps_nopf);
277 		error = EPFNOSUPPORT;
278 		goto drop;
279 	}
280 
281 	tdbp = gettdb(rtable_l2(m->m_pkthdr.ph_rtableid),
282 	    spi, &dst_address, sproto);
283 	if (tdbp == NULL) {
284 		DPRINTF("could not find SA for packet to %s, spi %08x",
285 		    ipsp_address(&dst_address, buf, sizeof(buf)), ntohl(spi));
286 		IPSEC_ISTAT(esps_notdb, ahs_notdb, ipcomps_notdb);
287 		error = ENOENT;
288 		goto drop;
289 	}
290 
291 	if (tdbp->tdb_flags & TDBF_INVALID) {
292 		DPRINTF("attempted to use invalid SA %s/%08x/%u",
293 		    ipsp_address(&dst_address, buf, sizeof(buf)),
294 		    ntohl(spi), tdbp->tdb_sproto);
295 		IPSEC_ISTAT(esps_invalid, ahs_invalid, ipcomps_invalid);
296 		error = EINVAL;
297 		goto drop;
298 	}
299 
300 	if (udpencap && !(tdbp->tdb_flags & TDBF_UDPENCAP)) {
301 		DPRINTF("attempted to use non-udpencap SA %s/%08x/%u",
302 		    ipsp_address(&dst_address, buf, sizeof(buf)),
303 		    ntohl(spi), tdbp->tdb_sproto);
304 		espstat_inc(esps_udpinval);
305 		error = EINVAL;
306 		goto drop;
307 	}
308 
309 	if (!udpencap && (tdbp->tdb_flags & TDBF_UDPENCAP)) {
310 		DPRINTF("attempted to use udpencap SA %s/%08x/%u",
311 		    ipsp_address(&dst_address, buf, sizeof(buf)),
312 		    ntohl(spi), tdbp->tdb_sproto);
313 		espstat_inc(esps_udpneeded);
314 		error = EINVAL;
315 		goto drop;
316 	}
317 
318 	if (tdbp->tdb_xform == NULL) {
319 		DPRINTF("attempted to use uninitialized SA %s/%08x/%u",
320 		    ipsp_address(&dst_address, buf, sizeof(buf)),
321 		    ntohl(spi), tdbp->tdb_sproto);
322 		IPSEC_ISTAT(esps_noxform, ahs_noxform, ipcomps_noxform);
323 		error = ENXIO;
324 		goto drop;
325 	}
326 
327 	if (sproto != IPPROTO_IPCOMP) {
328 		if ((encif = enc_getif(tdbp->tdb_rdomain_post,
329 		    tdbp->tdb_tap)) == NULL) {
330 			DPRINTF("no enc%u interface for SA %s/%08x/%u",
331 			    tdbp->tdb_tap,
332 			    ipsp_address(&dst_address, buf, sizeof(buf)),
333 			    ntohl(spi), tdbp->tdb_sproto);
334 			IPSEC_ISTAT(esps_pdrops, ahs_pdrops, ipcomps_pdrops);
335 			error = EACCES;
336 			goto drop;
337 		}
338 
339 		/* XXX This conflicts with the scoped nature of IPv6 */
340 		m->m_pkthdr.ph_ifidx = encif->if_index;
341 	}
342 
343 	/* Register first use, setup expiration timer. */
344 	if (tdbp->tdb_first_use == 0) {
345 		tdbp->tdb_first_use = gettime();
346 		if (tdbp->tdb_flags & TDBF_FIRSTUSE)
347 			timeout_add_sec(&tdbp->tdb_first_tmo,
348 			    tdbp->tdb_exp_first_use);
349 		if (tdbp->tdb_flags & TDBF_SOFT_FIRSTUSE)
350 			timeout_add_sec(&tdbp->tdb_sfirst_tmo,
351 			    tdbp->tdb_soft_first_use);
352 	}
353 
354 	tdbp->tdb_ipackets++;
355 	tdbp->tdb_ibytes += m->m_pkthdr.len;
356 
357 	/*
358 	 * Call appropriate transform and return -- callback takes care of
359 	 * everything else.
360 	 */
361 	error = (*(tdbp->tdb_xform->xf_input))(m, tdbp, skip, protoff);
362 	if (error) {
363 		ipsecstat_inc(ipsec_idrops);
364 		tdbp->tdb_idrops++;
365 	}
366 	return error;
367 
368  drop:
369 	ipsecstat_inc(ipsec_idrops);
370 	if (tdbp != NULL)
371 		tdbp->tdb_idrops++;
372 	m_freem(m);
373 	return error;
374 }
375 
376 void
377 ipsec_input_cb(struct cryptop *crp)
378 {
379 	struct tdb_crypto *tc = (struct tdb_crypto *) crp->crp_opaque;
380 	struct mbuf *m = (struct mbuf *) crp->crp_buf;
381 	struct tdb *tdb = NULL;
382 	int clen, error;
383 
384 	NET_ASSERT_LOCKED();
385 
386 	if (m == NULL) {
387 		DPRINTF("bogus returned buffer from crypto");
388 		ipsecstat_inc(ipsec_crypto);
389 		goto drop;
390 	}
391 
392 	tdb = gettdb(tc->tc_rdomain, tc->tc_spi, &tc->tc_dst, tc->tc_proto);
393 	if (tdb == NULL) {
394 		DPRINTF("TDB is expired while in crypto");
395 		ipsecstat_inc(ipsec_notdb);
396 		goto drop;
397 	}
398 
399 	/* Check for crypto errors */
400 	if (crp->crp_etype) {
401 		if (crp->crp_etype == EAGAIN) {
402 			/* Reset the session ID */
403 			if (tdb->tdb_cryptoid != 0)
404 				tdb->tdb_cryptoid = crp->crp_sid;
405 			error = crypto_dispatch(crp);
406 			if (error) {
407 				DPRINTF("crypto dispatch error %d", error);
408 				goto drop;
409 			}
410 			return;
411 		}
412 		DPRINTF("crypto error %d", crp->crp_etype);
413 		ipsecstat_inc(ipsec_noxform);
414 		goto drop;
415 	}
416 
417 	/* Length of data after processing */
418 	clen = crp->crp_olen;
419 
420 	/* Release the crypto descriptors */
421 	crypto_freereq(crp);
422 
423 	switch (tdb->tdb_sproto) {
424 	case IPPROTO_ESP:
425 		error = esp_input_cb(tdb, tc, m, clen);
426 		break;
427 	case IPPROTO_AH:
428 		error = ah_input_cb(tdb, tc, m, clen);
429 		break;
430 	case IPPROTO_IPCOMP:
431 		error = ipcomp_input_cb(tdb, tc, m, clen);
432 		break;
433 	default:
434 		panic("%s: unknown/unsupported security protocol %d",
435 		    __func__, tdb->tdb_sproto);
436 	}
437 
438 	if (error) {
439 		ipsecstat_inc(ipsec_idrops);
440 		tdb->tdb_idrops++;
441 	}
442 	return;
443 
444  drop:
445 	ipsecstat_inc(ipsec_idrops);
446 	if (tdb != NULL)
447 		tdb->tdb_idrops++;
448 	free(tc, M_XDATA, 0);
449 	m_freem(m);
450 	crypto_freereq(crp);
451 }
452 
453 /*
454  * IPsec input callback, called by the transform callback. Takes care of
455  * filtering and other sanity checks on the processed packet.
456  */
457 int
458 ipsec_common_input_cb(struct mbuf *m, struct tdb *tdbp, int skip, int protoff)
459 {
460 	int af, sproto;
461 	u_int8_t prot;
462 
463 #if NBPFILTER > 0
464 	struct ifnet *encif;
465 #endif
466 
467 	struct ip *ip, ipn;
468 
469 #ifdef INET6
470 	struct ip6_hdr *ip6, ip6n;
471 #endif /* INET6 */
472 	struct m_tag *mtag;
473 	struct tdb_ident *tdbi;
474 
475 #ifdef ENCDEBUG
476 	char buf[INET6_ADDRSTRLEN];
477 #endif
478 
479 	af = tdbp->tdb_dst.sa.sa_family;
480 	sproto = tdbp->tdb_sproto;
481 
482 	tdbp->tdb_last_used = gettime();
483 
484 	/* Sanity check */
485 	if (m == NULL) {
486 		/* The called routine will print a message if necessary */
487 		IPSEC_ISTAT(esps_badkcr, ahs_badkcr, ipcomps_badkcr);
488 		return -1;
489 	}
490 
491 	/* Fix IPv4 header */
492 	if (af == AF_INET) {
493 		if ((m->m_len < skip) && ((m = m_pullup(m, skip)) == NULL)) {
494 			DPRINTF("processing failed for SA %s/%08x",
495 			    ipsp_address(&tdbp->tdb_dst, buf, sizeof(buf)),
496 			    ntohl(tdbp->tdb_spi));
497 			IPSEC_ISTAT(esps_hdrops, ahs_hdrops, ipcomps_hdrops);
498 			return -1;
499 		}
500 
501 		ip = mtod(m, struct ip *);
502 		ip->ip_len = htons(m->m_pkthdr.len);
503 		ip->ip_sum = 0;
504 		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
505 		prot = ip->ip_p;
506 
507 		/* IP-in-IP encapsulation */
508 		if (prot == IPPROTO_IPIP) {
509 			if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
510 				m_freem(m);
511 				IPSEC_ISTAT(esps_hdrops, ahs_hdrops,
512 				    ipcomps_hdrops);
513 				return -1;
514 			}
515 			/* ipn will now contain the inner IPv4 header */
516 			m_copydata(m, skip, sizeof(struct ip),
517 			    (caddr_t) &ipn);
518 		}
519 
520 #ifdef INET6
521 		/* IPv6-in-IP encapsulation. */
522 		if (prot == IPPROTO_IPV6) {
523 			if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
524 				m_freem(m);
525 				IPSEC_ISTAT(esps_hdrops, ahs_hdrops,
526 				    ipcomps_hdrops);
527 				return -1;
528 			}
529 			/* ip6n will now contain the inner IPv6 header. */
530 			m_copydata(m, skip, sizeof(struct ip6_hdr),
531 			    (caddr_t) &ip6n);
532 		}
533 #endif /* INET6 */
534 	}
535 
536 #ifdef INET6
537 	/* Fix IPv6 header */
538 	if (af == AF_INET6)
539 	{
540 		if (m->m_len < sizeof(struct ip6_hdr) &&
541 		    (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
542 
543 			DPRINTF("processing failed for SA %s/%08x",
544 			    ipsp_address(&tdbp->tdb_dst, buf, sizeof(buf)),
545 			    ntohl(tdbp->tdb_spi));
546 			IPSEC_ISTAT(esps_hdrops, ahs_hdrops, ipcomps_hdrops);
547 			return -1;
548 		}
549 
550 		ip6 = mtod(m, struct ip6_hdr *);
551 		ip6->ip6_plen = htons(m->m_pkthdr.len - skip);
552 
553 		/* Save protocol */
554 		m_copydata(m, protoff, 1, (caddr_t) &prot);
555 
556 		/* IP-in-IP encapsulation */
557 		if (prot == IPPROTO_IPIP) {
558 			if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
559 				m_freem(m);
560 				IPSEC_ISTAT(esps_hdrops, ahs_hdrops,
561 				    ipcomps_hdrops);
562 				return -1;
563 			}
564 			/* ipn will now contain the inner IPv4 header */
565 			m_copydata(m, skip, sizeof(struct ip), (caddr_t) &ipn);
566 		}
567 
568 		/* IPv6-in-IP encapsulation */
569 		if (prot == IPPROTO_IPV6) {
570 			if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
571 				m_freem(m);
572 				IPSEC_ISTAT(esps_hdrops, ahs_hdrops,
573 				    ipcomps_hdrops);
574 				return -1;
575 			}
576 			/* ip6n will now contain the inner IPv6 header. */
577 			m_copydata(m, skip, sizeof(struct ip6_hdr),
578 			    (caddr_t) &ip6n);
579 		}
580 	}
581 #endif /* INET6 */
582 
583 	/*
584 	 * Fix TCP/UDP checksum of UDP encapsulated transport mode ESP packet.
585 	 * (RFC3948 3.1.2)
586 	 */
587 	if ((af == AF_INET || af == AF_INET6) &&
588 	    (tdbp->tdb_flags & TDBF_UDPENCAP) &&
589 	    (tdbp->tdb_flags & TDBF_TUNNELING) == 0) {
590 		u_int16_t cksum;
591 
592 		switch (prot) {
593 		case IPPROTO_UDP:
594 			if (m->m_pkthdr.len < skip + sizeof(struct udphdr)) {
595 				m_freem(m);
596 				IPSEC_ISTAT(esps_hdrops, ahs_hdrops,
597 				    ipcomps_hdrops);
598 				return -1;
599 			}
600 			cksum = 0;
601 			m_copyback(m, skip + offsetof(struct udphdr, uh_sum),
602 			    sizeof(cksum), &cksum, M_NOWAIT);
603 #ifdef INET6
604 			if (af == AF_INET6) {
605 				cksum = in6_cksum(m, IPPROTO_UDP, skip,
606 				    m->m_pkthdr.len - skip);
607 				m_copyback(m, skip + offsetof(struct udphdr,
608 				    uh_sum), sizeof(cksum), &cksum, M_NOWAIT);
609 			}
610 #endif
611 			break;
612 		case IPPROTO_TCP:
613 			if (m->m_pkthdr.len < skip + sizeof(struct tcphdr)) {
614 				m_freem(m);
615 				IPSEC_ISTAT(esps_hdrops, ahs_hdrops,
616 				    ipcomps_hdrops);
617 				return -1;
618 			}
619 			cksum = 0;
620 			m_copyback(m, skip + offsetof(struct tcphdr, th_sum),
621 			    sizeof(cksum), &cksum, M_NOWAIT);
622 			if (af == AF_INET)
623 				cksum = in4_cksum(m, IPPROTO_TCP, skip,
624 				    m->m_pkthdr.len - skip);
625 #ifdef INET6
626 			else if (af == AF_INET6)
627 				cksum = in6_cksum(m, IPPROTO_TCP, skip,
628 				    m->m_pkthdr.len - skip);
629 #endif
630 			m_copyback(m, skip + offsetof(struct tcphdr, th_sum),
631 			    sizeof(cksum), &cksum, M_NOWAIT);
632 			break;
633 		}
634 	}
635 
636 	/*
637 	 * Record what we've done to the packet (under what SA it was
638 	 * processed).
639 	 */
640 	if (tdbp->tdb_sproto != IPPROTO_IPCOMP) {
641 		mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
642 		    sizeof(struct tdb_ident), M_NOWAIT);
643 		if (mtag == NULL) {
644 			m_freem(m);
645 			DPRINTF("failed to get tag");
646 			IPSEC_ISTAT(esps_hdrops, ahs_hdrops, ipcomps_hdrops);
647 			return -1;
648 		}
649 
650 		tdbi = (struct tdb_ident *)(mtag + 1);
651 		tdbi->dst = tdbp->tdb_dst;
652 		tdbi->proto = tdbp->tdb_sproto;
653 		tdbi->spi = tdbp->tdb_spi;
654 		tdbi->rdomain = tdbp->tdb_rdomain;
655 
656 		m_tag_prepend(m, mtag);
657 	}
658 
659 	switch (sproto) {
660 	case IPPROTO_ESP:
661 		/* Packet is confidential ? */
662 		if (tdbp->tdb_encalgxform)
663 			m->m_flags |= M_CONF;
664 
665 		/* Check if we had authenticated ESP. */
666 		if (tdbp->tdb_authalgxform)
667 			m->m_flags |= M_AUTH;
668 		break;
669 	case IPPROTO_AH:
670 		m->m_flags |= M_AUTH;
671 		break;
672 	case IPPROTO_IPCOMP:
673 		m->m_flags |= M_COMP;
674 		break;
675 	default:
676 		panic("%s: unknown/unsupported security protocol %d",
677 		    __func__, sproto);
678 	}
679 
680 #if NPF > 0
681 	/* Add pf tag if requested. */
682 	pf_tag_packet(m, tdbp->tdb_tag, -1);
683 	pf_pkt_addr_changed(m);
684 #endif
685 	if (tdbp->tdb_rdomain != tdbp->tdb_rdomain_post)
686 		m->m_pkthdr.ph_rtableid = tdbp->tdb_rdomain_post;
687 
688 	if (tdbp->tdb_flags & TDBF_TUNNELING)
689 		m->m_flags |= M_TUNNEL;
690 
691 	ipsecstat_add(ipsec_idecompbytes, m->m_pkthdr.len);
692 	tdbp->tdb_idecompbytes += m->m_pkthdr.len;
693 
694 #if NBPFILTER > 0
695 	if ((encif = enc_getif(tdbp->tdb_rdomain_post, tdbp->tdb_tap)) != NULL) {
696 		encif->if_ipackets++;
697 		encif->if_ibytes += m->m_pkthdr.len;
698 
699 		if (encif->if_bpf) {
700 			struct enchdr hdr;
701 
702 			hdr.af = af;
703 			hdr.spi = tdbp->tdb_spi;
704 			hdr.flags = m->m_flags & (M_AUTH|M_CONF);
705 
706 			bpf_mtap_hdr(encif->if_bpf, (char *)&hdr,
707 			    ENC_HDRLEN, m, BPF_DIRECTION_IN);
708 		}
709 	}
710 #endif
711 
712 #if NPF > 0
713 	/*
714 	 * The ip_deliver() shortcut avoids running through ip_input() with the
715 	 * same IP header twice.  Packets in transport mode have to be be
716 	 * passed to pf explicitly.  In tunnel mode the inner IP header will
717 	 * run through ip_input() and pf anyway.
718 	 */
719 	if ((tdbp->tdb_flags & TDBF_TUNNELING) == 0) {
720 		struct ifnet *ifp;
721 
722 		/* This is the enc0 interface unless for ipcomp. */
723 		if ((ifp = if_get(m->m_pkthdr.ph_ifidx)) == NULL) {
724 			m_freem(m);
725 			return -1;
726 		}
727 		if (pf_test(af, PF_IN, ifp, &m) != PF_PASS) {
728 			if_put(ifp);
729 			m_freem(m);
730 			return -1;
731 		}
732 		if_put(ifp);
733 		if (m == NULL)
734 			return -1;
735 	}
736 #endif
737 	/* Call the appropriate IPsec transform callback. */
738 	ip_deliver(&m, &skip, prot, af);
739 	return 0;
740 #undef IPSEC_ISTAT
741 }
742 
743 int
744 ipsec_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
745     size_t newlen)
746 {
747 	int error;
748 
749 	switch (name[0]) {
750 	case IPCTL_IPSEC_ENC_ALGORITHM:
751 		NET_LOCK();
752 		error = sysctl_tstring(oldp, oldlenp, newp, newlen,
753 		    ipsec_def_enc, sizeof(ipsec_def_enc));
754 		NET_UNLOCK();
755 		return (error);
756 	case IPCTL_IPSEC_AUTH_ALGORITHM:
757 		NET_LOCK();
758 		error = sysctl_tstring(oldp, oldlenp, newp, newlen,
759 		    ipsec_def_auth, sizeof(ipsec_def_auth));
760 		NET_UNLOCK();
761 		return (error);
762 	case IPCTL_IPSEC_IPCOMP_ALGORITHM:
763 		NET_LOCK();
764 		error = sysctl_tstring(oldp, oldlenp, newp, newlen,
765 		    ipsec_def_comp, sizeof(ipsec_def_comp));
766 		NET_UNLOCK();
767 		return (error);
768 	case IPCTL_IPSEC_STATS:
769 		return (ipsec_sysctl_ipsecstat(oldp, oldlenp, newp));
770 	default:
771 		NET_LOCK();
772 		error = sysctl_bounded_arr(ipsecctl_vars, nitems(ipsecctl_vars),
773 		    name, namelen, oldp, oldlenp, newp, newlen);
774 		NET_UNLOCK();
775 		return (error);
776 	}
777 }
778 
779 int
780 esp_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
781     size_t newlen)
782 {
783 	int error;
784 
785 	/* All sysctl names at this level are terminal. */
786 	if (namelen != 1)
787 		return (ENOTDIR);
788 
789 	switch (name[0]) {
790 	case ESPCTL_STATS:
791 		return (esp_sysctl_espstat(oldp, oldlenp, newp));
792 	default:
793 		NET_LOCK();
794 		error = sysctl_bounded_arr(espctl_vars, nitems(espctl_vars),
795 		    name, namelen, oldp, oldlenp, newp, newlen);
796 		NET_UNLOCK();
797 		return (error);
798 	}
799 }
800 
801 int
802 esp_sysctl_espstat(void *oldp, size_t *oldlenp, void *newp)
803 {
804 	struct espstat espstat;
805 
806 	CTASSERT(sizeof(espstat) == (esps_ncounters * sizeof(uint64_t)));
807 	memset(&espstat, 0, sizeof espstat);
808 	counters_read(espcounters, (uint64_t *)&espstat, esps_ncounters);
809 	return (sysctl_rdstruct(oldp, oldlenp, newp, &espstat,
810 	    sizeof(espstat)));
811 }
812 
813 int
814 ah_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
815     size_t newlen)
816 {
817 	int error;
818 
819 	/* All sysctl names at this level are terminal. */
820 	if (namelen != 1)
821 		return (ENOTDIR);
822 
823 	switch (name[0]) {
824 	case AHCTL_STATS:
825 		return ah_sysctl_ahstat(oldp, oldlenp, newp);
826 	default:
827 		NET_LOCK();
828 		error = sysctl_bounded_arr(ahctl_vars, nitems(ahctl_vars), name,
829 		    namelen, oldp, oldlenp, newp, newlen);
830 		NET_UNLOCK();
831 		return (error);
832 	}
833 }
834 
835 int
836 ah_sysctl_ahstat(void *oldp, size_t *oldlenp, void *newp)
837 {
838 	struct ahstat ahstat;
839 
840 	CTASSERT(sizeof(ahstat) == (ahs_ncounters * sizeof(uint64_t)));
841 	memset(&ahstat, 0, sizeof ahstat);
842 	counters_read(ahcounters, (uint64_t *)&ahstat, ahs_ncounters);
843 	return (sysctl_rdstruct(oldp, oldlenp, newp, &ahstat, sizeof(ahstat)));
844 }
845 
846 int
847 ipcomp_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
848     size_t newlen)
849 {
850 	int error;
851 
852 	/* All sysctl names at this level are terminal. */
853 	if (namelen != 1)
854 		return (ENOTDIR);
855 
856 	switch (name[0]) {
857 	case IPCOMPCTL_STATS:
858 		return ipcomp_sysctl_ipcompstat(oldp, oldlenp, newp);
859 	default:
860 		NET_LOCK();
861 		error = sysctl_bounded_arr(ipcompctl_vars,
862 		    nitems(ipcompctl_vars), name, namelen, oldp, oldlenp,
863 		    newp, newlen);
864 		NET_UNLOCK();
865 		return (error);
866 	}
867 }
868 
869 int
870 ipcomp_sysctl_ipcompstat(void *oldp, size_t *oldlenp, void *newp)
871 {
872 	struct ipcompstat ipcompstat;
873 
874 	CTASSERT(sizeof(ipcompstat) == (ipcomps_ncounters * sizeof(uint64_t)));
875 	memset(&ipcompstat, 0, sizeof ipcompstat);
876 	counters_read(ipcompcounters, (uint64_t *)&ipcompstat,
877 	    ipcomps_ncounters);
878 	return (sysctl_rdstruct(oldp, oldlenp, newp, &ipcompstat,
879 	    sizeof(ipcompstat)));
880 }
881 
882 int
883 ipsec_sysctl_ipsecstat(void *oldp, size_t *oldlenp, void *newp)
884 {
885 	struct ipsecstat ipsecstat;
886 
887 	CTASSERT(sizeof(ipsecstat) == (ipsec_ncounters * sizeof(uint64_t)));
888 	memset(&ipsecstat, 0, sizeof ipsecstat);
889 	counters_read(ipseccounters, (uint64_t *)&ipsecstat, ipsec_ncounters);
890 	return (sysctl_rdstruct(oldp, oldlenp, newp, &ipsecstat,
891 	    sizeof(ipsecstat)));
892 }
893 
894 /* IPv4 AH wrapper. */
895 int
896 ah4_input(struct mbuf **mp, int *offp, int proto, int af)
897 {
898 	if (
899 #if NPF > 0
900 	    ((*mp)->m_pkthdr.pf.flags & PF_TAG_DIVERTED) ||
901 #endif
902 	    !ah_enable)
903 		return rip_input(mp, offp, proto, af);
904 
905 	ipsec_common_input(*mp, *offp, offsetof(struct ip, ip_p), AF_INET,
906 	    proto, 0);
907 	return IPPROTO_DONE;
908 }
909 
910 void
911 ah4_ctlinput(int cmd, struct sockaddr *sa, u_int rdomain, void *v)
912 {
913 	if (sa->sa_family != AF_INET ||
914 	    sa->sa_len != sizeof(struct sockaddr_in))
915 		return;
916 
917 	ipsec_common_ctlinput(rdomain, cmd, sa, v, IPPROTO_AH);
918 }
919 
920 /* IPv4 ESP wrapper. */
921 int
922 esp4_input(struct mbuf **mp, int *offp, int proto, int af)
923 {
924 	if (
925 #if NPF > 0
926 	    ((*mp)->m_pkthdr.pf.flags & PF_TAG_DIVERTED) ||
927 #endif
928 	    !esp_enable)
929 		return rip_input(mp, offp, proto, af);
930 
931 	ipsec_common_input(*mp, *offp, offsetof(struct ip, ip_p), AF_INET,
932 	    proto, 0);
933 	return IPPROTO_DONE;
934 }
935 
936 /* IPv4 IPCOMP wrapper */
937 int
938 ipcomp4_input(struct mbuf **mp, int *offp, int proto, int af)
939 {
940 	if (
941 #if NPF > 0
942 	    ((*mp)->m_pkthdr.pf.flags & PF_TAG_DIVERTED) ||
943 #endif
944 	    !ipcomp_enable)
945 		return rip_input(mp, offp, proto, af);
946 
947 	ipsec_common_input(*mp, *offp, offsetof(struct ip, ip_p), AF_INET,
948 	    proto, 0);
949 	return IPPROTO_DONE;
950 }
951 
952 void
953 ipsec_common_ctlinput(u_int rdomain, int cmd, struct sockaddr *sa,
954     void *v, int proto)
955 {
956 	struct ip *ip = v;
957 
958 	if (cmd == PRC_MSGSIZE && ip && ip_mtudisc && ip->ip_v == 4) {
959 		struct tdb *tdbp;
960 		struct sockaddr_in dst;
961 		struct icmp *icp;
962 		int hlen = ip->ip_hl << 2;
963 		u_int32_t spi, mtu;
964 		ssize_t adjust;
965 
966 		/* Find the right MTU. */
967 		icp = (struct icmp *)((caddr_t) ip -
968 		    offsetof(struct icmp, icmp_ip));
969 		mtu = ntohs(icp->icmp_nextmtu);
970 
971 		/*
972 		 * Ignore the packet, if we do not receive a MTU
973 		 * or the MTU is too small to be acceptable.
974 		 */
975 		if (mtu < 296)
976 			return;
977 
978 		memset(&dst, 0, sizeof(struct sockaddr_in));
979 		dst.sin_family = AF_INET;
980 		dst.sin_len = sizeof(struct sockaddr_in);
981 		dst.sin_addr.s_addr = ip->ip_dst.s_addr;
982 
983 		memcpy(&spi, (caddr_t)ip + hlen, sizeof(u_int32_t));
984 
985 		tdbp = gettdb_rev(rdomain, spi, (union sockaddr_union *)&dst,
986 		    proto);
987 		if (tdbp == NULL || tdbp->tdb_flags & TDBF_INVALID)
988 			return;
989 
990 		/* Walk the chain backwards to the first tdb */
991 		NET_ASSERT_LOCKED();
992 		for (; tdbp; tdbp = tdbp->tdb_inext) {
993 			if (tdbp->tdb_flags & TDBF_INVALID ||
994 			    (adjust = ipsec_hdrsz(tdbp)) == -1)
995 				return;
996 
997 			mtu -= adjust;
998 
999 			/* Store adjusted MTU in tdb */
1000 			tdbp->tdb_mtu = mtu;
1001 			tdbp->tdb_mtutimeout = gettime() +
1002 			    ip_mtudisc_timeout;
1003 			DPRINTF("spi %08x mtu %d adjust %ld",
1004 			    ntohl(tdbp->tdb_spi), tdbp->tdb_mtu, adjust);
1005 		}
1006 	}
1007 }
1008 
1009 void
1010 udpencap_ctlinput(int cmd, struct sockaddr *sa, u_int rdomain, void *v)
1011 {
1012 	struct ip *ip = v;
1013 	struct tdb *tdbp;
1014 	struct icmp *icp;
1015 	u_int32_t mtu;
1016 	ssize_t adjust;
1017 	struct sockaddr_in dst, src;
1018 	union sockaddr_union *su_dst, *su_src;
1019 
1020 	NET_ASSERT_LOCKED();
1021 
1022 	icp = (struct icmp *)((caddr_t) ip - offsetof(struct icmp, icmp_ip));
1023 	mtu = ntohs(icp->icmp_nextmtu);
1024 
1025 	/*
1026 	 * Ignore the packet, if we do not receive a MTU
1027 	 * or the MTU is too small to be acceptable.
1028 	 */
1029 	if (mtu < 296)
1030 		return;
1031 
1032 	memset(&dst, 0, sizeof(dst));
1033 	dst.sin_family = AF_INET;
1034 	dst.sin_len = sizeof(struct sockaddr_in);
1035 	dst.sin_addr.s_addr = ip->ip_dst.s_addr;
1036 	su_dst = (union sockaddr_union *)&dst;
1037 	memset(&src, 0, sizeof(src));
1038 	src.sin_family = AF_INET;
1039 	src.sin_len = sizeof(struct sockaddr_in);
1040 	src.sin_addr.s_addr = ip->ip_src.s_addr;
1041 	su_src = (union sockaddr_union *)&src;
1042 
1043 	tdbp = gettdbbysrcdst_rev(rdomain, 0, su_src, su_dst,
1044 	    IPPROTO_ESP);
1045 
1046 	for (; tdbp != NULL; tdbp = tdbp->tdb_snext) {
1047 		if (tdbp->tdb_sproto == IPPROTO_ESP &&
1048 		    ((tdbp->tdb_flags & (TDBF_INVALID|TDBF_UDPENCAP)) ==
1049 		    TDBF_UDPENCAP) &&
1050 		    !memcmp(&tdbp->tdb_dst, &dst, su_dst->sa.sa_len) &&
1051 		    !memcmp(&tdbp->tdb_src, &src, su_src->sa.sa_len)) {
1052 			if ((adjust = ipsec_hdrsz(tdbp)) != -1) {
1053 				/* Store adjusted MTU in tdb */
1054 				tdbp->tdb_mtu = mtu - adjust;
1055 				tdbp->tdb_mtutimeout = gettime() +
1056 				    ip_mtudisc_timeout;
1057 				DPRINTF("spi %08x mtu %d adjust %ld",
1058 				    ntohl(tdbp->tdb_spi), tdbp->tdb_mtu,
1059 				    adjust);
1060 			}
1061 		}
1062 	}
1063 }
1064 
1065 void
1066 esp4_ctlinput(int cmd, struct sockaddr *sa, u_int rdomain, void *v)
1067 {
1068 	if (sa->sa_family != AF_INET ||
1069 	    sa->sa_len != sizeof(struct sockaddr_in))
1070 		return;
1071 
1072 	ipsec_common_ctlinput(rdomain, cmd, sa, v, IPPROTO_ESP);
1073 }
1074 
1075 #ifdef INET6
1076 /* IPv6 AH wrapper. */
1077 int
1078 ah6_input(struct mbuf **mp, int *offp, int proto, int af)
1079 {
1080 	int l = 0;
1081 	int protoff, nxt;
1082 	struct ip6_ext ip6e;
1083 
1084 	if (
1085 #if NPF > 0
1086 	    ((*mp)->m_pkthdr.pf.flags & PF_TAG_DIVERTED) ||
1087 #endif
1088 	    !ah_enable)
1089 		return rip6_input(mp, offp, proto, af);
1090 
1091 	if (*offp < sizeof(struct ip6_hdr)) {
1092 		DPRINTF("bad offset");
1093 		ahstat_inc(ahs_hdrops);
1094 		m_freemp(mp);
1095 		return IPPROTO_DONE;
1096 	} else if (*offp == sizeof(struct ip6_hdr)) {
1097 		protoff = offsetof(struct ip6_hdr, ip6_nxt);
1098 	} else {
1099 		/* Chase down the header chain... */
1100 		protoff = sizeof(struct ip6_hdr);
1101 		nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt;
1102 
1103 		do {
1104 			protoff += l;
1105 			m_copydata(*mp, protoff, sizeof(ip6e),
1106 			    (caddr_t) &ip6e);
1107 
1108 			if (nxt == IPPROTO_AH)
1109 				l = (ip6e.ip6e_len + 2) << 2;
1110 			else
1111 				l = (ip6e.ip6e_len + 1) << 3;
1112 #ifdef DIAGNOSTIC
1113 			if (l <= 0)
1114 				panic("ah6_input: l went zero or negative");
1115 #endif
1116 
1117 			nxt = ip6e.ip6e_nxt;
1118 		} while (protoff + l < *offp);
1119 
1120 		/* Malformed packet check */
1121 		if (protoff + l != *offp) {
1122 			DPRINTF("bad packet header chain");
1123 			ahstat_inc(ahs_hdrops);
1124 			m_freemp(mp);
1125 			return IPPROTO_DONE;
1126 		}
1127 		protoff += offsetof(struct ip6_ext, ip6e_nxt);
1128 	}
1129 	ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto, 0);
1130 	return IPPROTO_DONE;
1131 }
1132 
1133 /* IPv6 ESP wrapper. */
1134 int
1135 esp6_input(struct mbuf **mp, int *offp, int proto, int af)
1136 {
1137 	int l = 0;
1138 	int protoff, nxt;
1139 	struct ip6_ext ip6e;
1140 
1141 	if (
1142 #if NPF > 0
1143 	    ((*mp)->m_pkthdr.pf.flags & PF_TAG_DIVERTED) ||
1144 #endif
1145 	    !esp_enable)
1146 		return rip6_input(mp, offp, proto, af);
1147 
1148 	if (*offp < sizeof(struct ip6_hdr)) {
1149 		DPRINTF("bad offset");
1150 		espstat_inc(esps_hdrops);
1151 		m_freemp(mp);
1152 		return IPPROTO_DONE;
1153 	} else if (*offp == sizeof(struct ip6_hdr)) {
1154 		protoff = offsetof(struct ip6_hdr, ip6_nxt);
1155 	} else {
1156 		/* Chase down the header chain... */
1157 		protoff = sizeof(struct ip6_hdr);
1158 		nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt;
1159 
1160 		do {
1161 			protoff += l;
1162 			m_copydata(*mp, protoff, sizeof(ip6e),
1163 			    (caddr_t) &ip6e);
1164 
1165 			if (nxt == IPPROTO_AH)
1166 				l = (ip6e.ip6e_len + 2) << 2;
1167 			else
1168 				l = (ip6e.ip6e_len + 1) << 3;
1169 #ifdef DIAGNOSTIC
1170 			if (l <= 0)
1171 				panic("esp6_input: l went zero or negative");
1172 #endif
1173 
1174 			nxt = ip6e.ip6e_nxt;
1175 		} while (protoff + l < *offp);
1176 
1177 		/* Malformed packet check */
1178 		if (protoff + l != *offp) {
1179 			DPRINTF("bad packet header chain");
1180 			espstat_inc(esps_hdrops);
1181 			m_freemp(mp);
1182 			return IPPROTO_DONE;
1183 		}
1184 		protoff += offsetof(struct ip6_ext, ip6e_nxt);
1185 	}
1186 	ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto, 0);
1187 	return IPPROTO_DONE;
1188 
1189 }
1190 
1191 /* IPv6 IPcomp wrapper */
1192 int
1193 ipcomp6_input(struct mbuf **mp, int *offp, int proto, int af)
1194 {
1195 	int l = 0;
1196 	int protoff, nxt;
1197 	struct ip6_ext ip6e;
1198 
1199 	if (
1200 #if NPF > 0
1201 	    ((*mp)->m_pkthdr.pf.flags & PF_TAG_DIVERTED) ||
1202 #endif
1203 	    !ipcomp_enable)
1204 		return rip6_input(mp, offp, proto, af);
1205 
1206 	if (*offp < sizeof(struct ip6_hdr)) {
1207 		DPRINTF("bad offset");
1208 		ipcompstat_inc(ipcomps_hdrops);
1209 		m_freemp(mp);
1210 		return IPPROTO_DONE;
1211 	} else if (*offp == sizeof(struct ip6_hdr)) {
1212 		protoff = offsetof(struct ip6_hdr, ip6_nxt);
1213 	} else {
1214 		/* Chase down the header chain... */
1215 		protoff = sizeof(struct ip6_hdr);
1216 		nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt;
1217 
1218 		do {
1219 			protoff += l;
1220 			m_copydata(*mp, protoff, sizeof(ip6e),
1221 			    (caddr_t) &ip6e);
1222 			if (nxt == IPPROTO_AH)
1223 				l = (ip6e.ip6e_len + 2) << 2;
1224 			else
1225 				l = (ip6e.ip6e_len + 1) << 3;
1226 #ifdef DIAGNOSTIC
1227 			if (l <= 0)
1228 				panic("l went zero or negative");
1229 #endif
1230 
1231 			nxt = ip6e.ip6e_nxt;
1232 		} while (protoff + l < *offp);
1233 
1234 		/* Malformed packet check */
1235 		if (protoff + l != *offp) {
1236 			DPRINTF("bad packet header chain");
1237 			ipcompstat_inc(ipcomps_hdrops);
1238 			m_freemp(mp);
1239 			return IPPROTO_DONE;
1240 		}
1241 
1242 		protoff += offsetof(struct ip6_ext, ip6e_nxt);
1243 	}
1244 	ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto, 0);
1245 	return IPPROTO_DONE;
1246 }
1247 #endif /* INET6 */
1248 
1249 int
1250 ipsec_forward_check(struct mbuf *m, int hlen, int af)
1251 {
1252 	struct tdb *tdb;
1253 	struct tdb_ident *tdbi;
1254 	struct m_tag *mtag;
1255 	int error = 0;
1256 
1257 	/*
1258 	 * IPsec policy check for forwarded packets. Look at
1259 	 * inner-most IPsec SA used.
1260 	 */
1261 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
1262 	if (mtag != NULL) {
1263 		tdbi = (struct tdb_ident *)(mtag + 1);
1264 		tdb = gettdb(tdbi->rdomain, tdbi->spi, &tdbi->dst, tdbi->proto);
1265 	} else
1266 		tdb = NULL;
1267 	ipsp_spd_lookup(m, af, hlen, &error, IPSP_DIRECTION_IN, tdb, NULL, 0);
1268 
1269 	return error;
1270 }
1271 
1272 int
1273 ipsec_local_check(struct mbuf *m, int hlen, int proto, int af)
1274 {
1275 	struct tdb *tdb;
1276 	struct tdb_ident *tdbi;
1277 	struct m_tag *mtag;
1278 	int error = 0;
1279 
1280 	/*
1281 	 * If it's a protected packet for us, skip the policy check.
1282 	 * That's because we really only care about the properties of
1283 	 * the protected packet, and not the intermediate versions.
1284 	 * While this is not the most paranoid setting, it allows
1285 	 * some flexibility in handling nested tunnels (in setting up
1286 	 * the policies).
1287 	 */
1288 	if ((proto == IPPROTO_ESP) || (proto == IPPROTO_AH) ||
1289 	    (proto == IPPROTO_IPCOMP))
1290 		return 0;
1291 
1292 	/*
1293 	 * If the protected packet was tunneled, then we need to
1294 	 * verify the protected packet's information, not the
1295 	 * external headers. Thus, skip the policy lookup for the
1296 	 * external packet, and keep the IPsec information linked on
1297 	 * the packet header (the encapsulation routines know how
1298 	 * to deal with that).
1299 	 */
1300 	if ((proto == IPPROTO_IPV4) || (proto == IPPROTO_IPV6))
1301 		return 0;
1302 
1303 	/*
1304 	 * When processing IPv6 header chains, do not look at the
1305 	 * outer header.  The inner protocol is relevant and will
1306 	 * be checked by the local delivery loop later.
1307 	 */
1308 	if ((af == AF_INET6) && ((proto == IPPROTO_DSTOPTS) ||
1309 	    (proto == IPPROTO_ROUTING) || (proto == IPPROTO_FRAGMENT)))
1310 		return 0;
1311 
1312 	/*
1313 	 * If the protected packet is TCP or UDP, we'll do the
1314 	 * policy check in the respective input routine, so we can
1315 	 * check for bypass sockets.
1316 	 */
1317 	if ((proto == IPPROTO_TCP) || (proto == IPPROTO_UDP))
1318 		return 0;
1319 
1320 	/*
1321 	 * IPsec policy check for local-delivery packets. Look at the
1322 	 * inner-most SA that protected the packet. This is in fact
1323 	 * a bit too restrictive (it could end up causing packets to
1324 	 * be dropped that semantically follow the policy, e.g., in
1325 	 * certain SA-bundle configurations); but the alternative is
1326 	 * very complicated (and requires keeping track of what
1327 	 * kinds of tunneling headers have been seen in-between the
1328 	 * IPsec headers), and I don't think we lose much functionality
1329 	 * that's needed in the real world (who uses bundles anyway ?).
1330 	 */
1331 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
1332 	if (mtag) {
1333 		tdbi = (struct tdb_ident *)(mtag + 1);
1334 		tdb = gettdb(tdbi->rdomain, tdbi->spi, &tdbi->dst,
1335 		    tdbi->proto);
1336 	} else
1337 		tdb = NULL;
1338 	ipsp_spd_lookup(m, af, hlen, &error, IPSP_DIRECTION_IN,
1339 	    tdb, NULL, 0);
1340 
1341 	return error;
1342 }
1343