xref: /openbsd-src/sys/netinet/ipsec_input.c (revision c7e8ea31cd41a963f06f0a8ba93948b06aa6b4a4)
1 /*	$OpenBSD: ipsec_input.c,v 1.156 2017/07/05 11:34:10 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 "bpfilter.h"
81 
82 void ipsec_common_ctlinput(u_int, int, struct sockaddr *, void *, int);
83 
84 #ifdef ENCDEBUG
85 #define DPRINTF(x)	if (encdebug) printf x
86 #else
87 #define DPRINTF(x)
88 #endif
89 
90 /* sysctl variables */
91 int esp_enable = 1;
92 int ah_enable = 1;
93 int ipcomp_enable = 0;
94 
95 int *espctl_vars[ESPCTL_MAXID] = ESPCTL_VARS;
96 int *ahctl_vars[AHCTL_MAXID] = AHCTL_VARS;
97 int *ipcompctl_vars[IPCOMPCTL_MAXID] = IPCOMPCTL_VARS;
98 
99 /*
100  * ipsec_common_input() gets called when we receive an IPsec-protected packet
101  * in IPv4 or IPv6. All it does is find the right TDB and call the appropriate
102  * transform. The callback takes care of further processing (like ingress
103  * filtering).
104  */
105 int
106 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto,
107     int udpencap)
108 {
109 #define IPSEC_ISTAT(x,y,z) (sproto == IPPROTO_ESP ? (x)++ : \
110 			    sproto == IPPROTO_AH ? (y)++ : (z)++)
111 
112 	union sockaddr_union dst_address;
113 	struct tdb *tdbp;
114 	struct ifnet *encif;
115 	u_int32_t spi;
116 	u_int16_t cpi;
117 	int error;
118 #ifdef ENCDEBUG
119 	char buf[INET6_ADDRSTRLEN];
120 #endif
121 
122 	NET_ASSERT_LOCKED();
123 
124 	IPSEC_ISTAT(espstat.esps_input, ahstat.ahs_input,
125 	    ipcompstat.ipcomps_input);
126 
127 	if (m == NULL) {
128 		DPRINTF(("ipsec_common_input(): NULL packet received\n"));
129 		IPSEC_ISTAT(espstat.esps_hdrops, ahstat.ahs_hdrops,
130 		    ipcompstat.ipcomps_hdrops);
131 		return EINVAL;
132 	}
133 
134 	if ((sproto == IPPROTO_ESP && !esp_enable) ||
135 	    (sproto == IPPROTO_AH && !ah_enable) ||
136 #if NPF > 0
137 	    (m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) ||
138 #endif
139 	    (sproto == IPPROTO_IPCOMP && !ipcomp_enable)) {
140 		switch (af) {
141 		case AF_INET:
142 			rip_input(&m, &skip, sproto, af);
143 			break;
144 #ifdef INET6
145 		case AF_INET6:
146 			rip6_input(&m, &skip, sproto, af);
147 			break;
148 #endif /* INET6 */
149 		default:
150 			DPRINTF(("ipsec_common_input(): unsupported protocol "
151 			    "family %d\n", af));
152 			m_freem(m);
153 			IPSEC_ISTAT(espstat.esps_nopf, ahstat.ahs_nopf,
154 			    ipcompstat.ipcomps_nopf);
155 			return EPFNOSUPPORT;
156 		}
157 		return 0;
158 	}
159 	if ((sproto == IPPROTO_IPCOMP) && (m->m_flags & M_COMP)) {
160 		m_freem(m);
161 		ipcompstat.ipcomps_pdrops++;
162 		DPRINTF(("ipsec_common_input(): repeated decompression\n"));
163 		return EINVAL;
164 	}
165 
166 	if (m->m_pkthdr.len - skip < 2 * sizeof(u_int32_t)) {
167 		m_freem(m);
168 		IPSEC_ISTAT(espstat.esps_hdrops, ahstat.ahs_hdrops,
169 		    ipcompstat.ipcomps_hdrops);
170 		DPRINTF(("ipsec_common_input(): packet too small\n"));
171 		return EINVAL;
172 	}
173 
174 	/* Retrieve the SPI from the relevant IPsec header */
175 	switch (sproto) {
176 	case IPPROTO_ESP:
177 		m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
178 		break;
179 	case IPPROTO_AH:
180 		m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
181 		    (caddr_t) &spi);
182 		break;
183 	case IPPROTO_IPCOMP:
184 		m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
185 		    (caddr_t) &cpi);
186 		spi = ntohl(htons(cpi));
187 		break;
188 	default:
189 		panic("%s: unknown/unsupported security protocol %d",
190 		    __func__, sproto);
191 	}
192 
193 	/*
194 	 * Find tunnel control block and (indirectly) call the appropriate
195 	 * kernel crypto routine. The resulting mbuf chain is a valid
196 	 * IP packet ready to go through input processing.
197 	 */
198 
199 	memset(&dst_address, 0, sizeof(dst_address));
200 	dst_address.sa.sa_family = af;
201 
202 	switch (af) {
203 	case AF_INET:
204 		dst_address.sin.sin_len = sizeof(struct sockaddr_in);
205 		m_copydata(m, offsetof(struct ip, ip_dst),
206 		    sizeof(struct in_addr),
207 		    (caddr_t) &(dst_address.sin.sin_addr));
208 		break;
209 
210 #ifdef INET6
211 	case AF_INET6:
212 		dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
213 		m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
214 		    sizeof(struct in6_addr),
215 		    (caddr_t) &(dst_address.sin6.sin6_addr));
216 		in6_recoverscope(&dst_address.sin6,
217 		    &dst_address.sin6.sin6_addr);
218 		break;
219 #endif /* INET6 */
220 
221 	default:
222 		DPRINTF(("ipsec_common_input(): unsupported protocol "
223 		    "family %d\n", af));
224 		m_freem(m);
225 		IPSEC_ISTAT(espstat.esps_nopf, ahstat.ahs_nopf,
226 		    ipcompstat.ipcomps_nopf);
227 		return EPFNOSUPPORT;
228 	}
229 
230 	tdbp = gettdb(rtable_l2(m->m_pkthdr.ph_rtableid),
231 	    spi, &dst_address, sproto);
232 	if (tdbp == NULL) {
233 		DPRINTF(("ipsec_common_input(): could not find SA for "
234 		    "packet to %s, spi %08x\n",
235 		    ipsp_address(&dst_address, buf, sizeof(buf)), ntohl(spi)));
236 		m_freem(m);
237 		IPSEC_ISTAT(espstat.esps_notdb, ahstat.ahs_notdb,
238 		    ipcompstat.ipcomps_notdb);
239 		return ENOENT;
240 	}
241 
242 	if (tdbp->tdb_flags & TDBF_INVALID) {
243 		DPRINTF(("ipsec_common_input(): attempted to use invalid "
244 		    "SA %s/%08x/%u\n", ipsp_address(&dst_address, buf,
245 		    sizeof(buf)), ntohl(spi), tdbp->tdb_sproto));
246 		m_freem(m);
247 		IPSEC_ISTAT(espstat.esps_invalid, ahstat.ahs_invalid,
248 		    ipcompstat.ipcomps_invalid);
249 		return EINVAL;
250 	}
251 
252 	if (udpencap && !(tdbp->tdb_flags & TDBF_UDPENCAP)) {
253 		DPRINTF(("ipsec_common_input(): attempted to use non-udpencap "
254 		    "SA %s/%08x/%u\n", ipsp_address(&dst_address, buf,
255 		    sizeof(buf)), ntohl(spi), tdbp->tdb_sproto));
256 		m_freem(m);
257 		espstat.esps_udpinval++;
258 		return EINVAL;
259 	}
260 
261 	if (!udpencap && (tdbp->tdb_flags & TDBF_UDPENCAP)) {
262 		DPRINTF(("ipsec_common_input(): attempted to use udpencap "
263 		    "SA %s/%08x/%u\n", ipsp_address(&dst_address, buf,
264 		    sizeof(buf)), ntohl(spi), tdbp->tdb_sproto));
265 		m_freem(m);
266 		espstat.esps_udpneeded++;
267 		return EINVAL;
268 	}
269 
270 	if (tdbp->tdb_xform == NULL) {
271 		DPRINTF(("ipsec_common_input(): attempted to use uninitialized "
272 		    "SA %s/%08x/%u\n", ipsp_address(&dst_address, buf,
273 		    sizeof(buf)), ntohl(spi), tdbp->tdb_sproto));
274 		m_freem(m);
275 		IPSEC_ISTAT(espstat.esps_noxform, ahstat.ahs_noxform,
276 		    ipcompstat.ipcomps_noxform);
277 		return ENXIO;
278 	}
279 
280 	if (sproto != IPPROTO_IPCOMP) {
281 		if ((encif = enc_getif(tdbp->tdb_rdomain,
282 		    tdbp->tdb_tap)) == NULL) {
283 			DPRINTF(("ipsec_common_input(): "
284 			    "no enc%u interface for SA %s/%08x/%u\n",
285 			    tdbp->tdb_tap, ipsp_address(&dst_address, buf,
286 			    sizeof(buf)), ntohl(spi), tdbp->tdb_sproto));
287 			m_freem(m);
288 
289 			IPSEC_ISTAT(espstat.esps_pdrops,
290 			    ahstat.ahs_pdrops,
291 			    ipcompstat.ipcomps_pdrops);
292 			return EACCES;
293 		}
294 
295 		/* XXX This conflicts with the scoped nature of IPv6 */
296 		m->m_pkthdr.ph_ifidx = encif->if_index;
297 	}
298 
299 	/* Register first use, setup expiration timer. */
300 	if (tdbp->tdb_first_use == 0) {
301 		tdbp->tdb_first_use = time_second;
302 		if (tdbp->tdb_flags & TDBF_FIRSTUSE)
303 			timeout_add_sec(&tdbp->tdb_first_tmo,
304 			    tdbp->tdb_exp_first_use);
305 		if (tdbp->tdb_flags & TDBF_SOFT_FIRSTUSE)
306 			timeout_add_sec(&tdbp->tdb_sfirst_tmo,
307 			    tdbp->tdb_soft_first_use);
308 	}
309 
310 	/*
311 	 * Call appropriate transform and return -- callback takes care of
312 	 * everything else.
313 	 */
314 	error = (*(tdbp->tdb_xform->xf_input))(m, tdbp, skip, protoff);
315 	return error;
316 }
317 
318 /*
319  * IPsec input callback, called by the transform callback. Takes care of
320  * filtering and other sanity checks on the processed packet.
321  */
322 void
323 ipsec_common_input_cb(struct mbuf *m, struct tdb *tdbp, int skip, int protoff)
324 {
325 	int af, sproto;
326 	u_int8_t prot;
327 
328 #if NBPFILTER > 0
329 	struct ifnet *encif;
330 #endif
331 
332 	struct ip *ip, ipn;
333 
334 #ifdef INET6
335 	struct ip6_hdr *ip6, ip6n;
336 #endif /* INET6 */
337 	struct m_tag *mtag;
338 	struct tdb_ident *tdbi;
339 
340 #ifdef ENCDEBUG
341 	char buf[INET6_ADDRSTRLEN];
342 #endif
343 
344 	af = tdbp->tdb_dst.sa.sa_family;
345 	sproto = tdbp->tdb_sproto;
346 
347 	tdbp->tdb_last_used = time_second;
348 
349 	/* Sanity check */
350 	if (m == NULL) {
351 		/* The called routine will print a message if necessary */
352 		IPSEC_ISTAT(espstat.esps_badkcr, ahstat.ahs_badkcr,
353 		    ipcompstat.ipcomps_badkcr);
354 		return;
355 	}
356 
357 	/* Fix IPv4 header */
358 	if (af == AF_INET) {
359 		if ((m->m_len < skip) && ((m = m_pullup(m, skip)) == NULL)) {
360 			DPRINTF(("ipsec_common_input_cb(): processing failed "
361 			    "for SA %s/%08x\n", ipsp_address(&tdbp->tdb_dst,
362 			    buf, sizeof(buf)), ntohl(tdbp->tdb_spi)));
363 			IPSEC_ISTAT(espstat.esps_hdrops, ahstat.ahs_hdrops,
364 			    ipcompstat.ipcomps_hdrops);
365 			return;
366 		}
367 
368 		ip = mtod(m, struct ip *);
369 		ip->ip_len = htons(m->m_pkthdr.len);
370 		ip->ip_sum = 0;
371 		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
372 		prot = ip->ip_p;
373 
374 		/* IP-in-IP encapsulation */
375 		if (prot == IPPROTO_IPIP) {
376 			if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
377 				m_freem(m);
378 				IPSEC_ISTAT(espstat.esps_hdrops,
379 				    ahstat.ahs_hdrops,
380 				    ipcompstat.ipcomps_hdrops);
381 				return;
382 			}
383 			/* ipn will now contain the inner IPv4 header */
384 			m_copydata(m, skip, sizeof(struct ip),
385 			    (caddr_t) &ipn);
386 		}
387 
388 #ifdef INET6
389 		/* IPv6-in-IP encapsulation. */
390 		if (prot == IPPROTO_IPV6) {
391 			if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
392 				m_freem(m);
393 				IPSEC_ISTAT(espstat.esps_hdrops,
394 				    ahstat.ahs_hdrops,
395 				    ipcompstat.ipcomps_hdrops);
396 				return;
397 			}
398 			/* ip6n will now contain the inner IPv6 header. */
399 			m_copydata(m, skip, sizeof(struct ip6_hdr),
400 			    (caddr_t) &ip6n);
401 		}
402 #endif /* INET6 */
403 	}
404 
405 #ifdef INET6
406 	/* Fix IPv6 header */
407 	if (af == AF_INET6)
408 	{
409 		if (m->m_len < sizeof(struct ip6_hdr) &&
410 		    (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
411 
412 			DPRINTF(("ipsec_common_input_cb(): processing failed "
413 			    "for SA %s/%08x\n", ipsp_address(&tdbp->tdb_dst,
414 			    buf, sizeof(buf)), ntohl(tdbp->tdb_spi)));
415 
416 			IPSEC_ISTAT(espstat.esps_hdrops, ahstat.ahs_hdrops,
417 			    ipcompstat.ipcomps_hdrops);
418 			return;
419 		}
420 
421 		ip6 = mtod(m, struct ip6_hdr *);
422 		ip6->ip6_plen = htons(m->m_pkthdr.len - skip);
423 
424 		/* Save protocol */
425 		m_copydata(m, protoff, 1, (caddr_t) &prot);
426 
427 		/* IP-in-IP encapsulation */
428 		if (prot == IPPROTO_IPIP) {
429 			if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
430 				m_freem(m);
431 				IPSEC_ISTAT(espstat.esps_hdrops,
432 				    ahstat.ahs_hdrops,
433 				    ipcompstat.ipcomps_hdrops);
434 				return;
435 			}
436 			/* ipn will now contain the inner IPv4 header */
437 			m_copydata(m, skip, sizeof(struct ip), (caddr_t) &ipn);
438 		}
439 
440 		/* IPv6-in-IP encapsulation */
441 		if (prot == IPPROTO_IPV6) {
442 			if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
443 				m_freem(m);
444 				IPSEC_ISTAT(espstat.esps_hdrops,
445 				    ahstat.ahs_hdrops,
446 				    ipcompstat.ipcomps_hdrops);
447 				return;
448 			}
449 			/* ip6n will now contain the inner IPv6 header. */
450 			m_copydata(m, skip, sizeof(struct ip6_hdr),
451 			    (caddr_t) &ip6n);
452 		}
453 	}
454 #endif /* INET6 */
455 
456 	/*
457 	 * Fix TCP/UDP checksum of UDP encapsulated transport mode ESP packet.
458 	 * (RFC3948 3.1.2)
459 	 */
460 	if ((af == AF_INET || af == AF_INET6) &&
461 	    (tdbp->tdb_flags & TDBF_UDPENCAP) &&
462 	    (tdbp->tdb_flags & TDBF_TUNNELING) == 0) {
463 		u_int16_t cksum;
464 
465 		switch (prot) {
466 		case IPPROTO_UDP:
467 			if (m->m_pkthdr.len < skip + sizeof(struct udphdr)) {
468 				m_freem(m);
469 				IPSEC_ISTAT(espstat.esps_hdrops,
470 				    ahstat.ahs_hdrops,
471 				    ipcompstat.ipcomps_hdrops);
472 				return;
473 			}
474 			cksum = 0;
475 			m_copyback(m, skip + offsetof(struct udphdr, uh_sum),
476 			    sizeof(cksum), &cksum, M_NOWAIT);
477 #ifdef INET6
478 			if (af == AF_INET6) {
479 				cksum = in6_cksum(m, IPPROTO_UDP, skip,
480 				    m->m_pkthdr.len - skip);
481 				m_copyback(m, skip + offsetof(struct udphdr,
482 				    uh_sum), sizeof(cksum), &cksum, M_NOWAIT);
483 			}
484 #endif
485 			break;
486 		case IPPROTO_TCP:
487 			if (m->m_pkthdr.len < skip + sizeof(struct tcphdr)) {
488 				m_freem(m);
489 				IPSEC_ISTAT(espstat.esps_hdrops,
490 				    ahstat.ahs_hdrops,
491 				    ipcompstat.ipcomps_hdrops);
492 				return;
493 			}
494 			cksum = 0;
495 			m_copyback(m, skip + offsetof(struct tcphdr, th_sum),
496 			    sizeof(cksum), &cksum, M_NOWAIT);
497 			if (af == AF_INET)
498 				cksum = in4_cksum(m, IPPROTO_TCP, skip,
499 				    m->m_pkthdr.len - skip);
500 #ifdef INET6
501 			else if (af == AF_INET6)
502 				cksum = in6_cksum(m, IPPROTO_TCP, skip,
503 				    m->m_pkthdr.len - skip);
504 #endif
505 			m_copyback(m, skip + offsetof(struct tcphdr, th_sum),
506 			    sizeof(cksum), &cksum, M_NOWAIT);
507 			break;
508 		}
509 	}
510 
511 	/*
512 	 * Record what we've done to the packet (under what SA it was
513 	 * processed).
514 	 */
515 	if (tdbp->tdb_sproto != IPPROTO_IPCOMP) {
516 		mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
517 		    sizeof(struct tdb_ident), M_NOWAIT);
518 		if (mtag == NULL) {
519 			m_freem(m);
520 			DPRINTF(("ipsec_common_input_cb(): failed to "
521 			    "get tag\n"));
522 			IPSEC_ISTAT(espstat.esps_hdrops, ahstat.ahs_hdrops,
523 			    ipcompstat.ipcomps_hdrops);
524 			return;
525 		}
526 
527 		tdbi = (struct tdb_ident *)(mtag + 1);
528 		tdbi->dst = tdbp->tdb_dst;
529 		tdbi->proto = tdbp->tdb_sproto;
530 		tdbi->spi = tdbp->tdb_spi;
531 		tdbi->rdomain = tdbp->tdb_rdomain;
532 
533 		m_tag_prepend(m, mtag);
534 	}
535 
536 	switch (sproto) {
537 	case IPPROTO_ESP:
538 		/* Packet is confidential ? */
539 		if (tdbp->tdb_encalgxform)
540 			m->m_flags |= M_CONF;
541 
542 		/* Check if we had authenticated ESP. */
543 		if (tdbp->tdb_authalgxform)
544 			m->m_flags |= M_AUTH;
545 		break;
546 	case IPPROTO_AH:
547 		m->m_flags |= M_AUTH;
548 		break;
549 	case IPPROTO_IPCOMP:
550 		m->m_flags |= M_COMP;
551 		break;
552 	default:
553 		panic("%s: unknown/unsupported security protocol %d",
554 		    __func__, sproto);
555 	}
556 
557 #if NPF > 0
558 	/* Add pf tag if requested. */
559 	pf_tag_packet(m, tdbp->tdb_tag, -1);
560 	pf_pkt_addr_changed(m);
561 #endif
562 
563 	if (tdbp->tdb_flags & TDBF_TUNNELING)
564 		m->m_flags |= M_TUNNEL;
565 
566 #if NBPFILTER > 0
567 	if ((encif = enc_getif(tdbp->tdb_rdomain, tdbp->tdb_tap)) != NULL) {
568 		encif->if_ipackets++;
569 		encif->if_ibytes += m->m_pkthdr.len;
570 
571 		if (encif->if_bpf) {
572 			struct enchdr hdr;
573 
574 			hdr.af = af;
575 			hdr.spi = tdbp->tdb_spi;
576 			hdr.flags = m->m_flags & (M_AUTH|M_CONF);
577 
578 			bpf_mtap_hdr(encif->if_bpf, (char *)&hdr,
579 			    ENC_HDRLEN, m, BPF_DIRECTION_IN, NULL);
580 		}
581 	}
582 #endif
583 
584 #if NPF > 0
585 	/*
586 	 * The ip_deliver() shortcut avoids running through ip_input() with the
587 	 * same IP header twice.  Packets in transport mode have to be be
588 	 * passed to pf explicitly.  In tunnel mode the inner IP header will
589 	 * run through ip_input() and pf anyway.
590 	 */
591 	if ((tdbp->tdb_flags & TDBF_TUNNELING) == 0) {
592 		struct ifnet *ifp;
593 
594 		/* This is the enc0 interface unless for ipcomp. */
595 		if ((ifp = if_get(m->m_pkthdr.ph_ifidx)) == NULL) {
596 			m_freem(m);
597 			return;
598 		}
599 		if (pf_test(af, PF_IN, ifp, &m) != PF_PASS) {
600 			if_put(ifp);
601 			m_freem(m);
602 			return;
603 		}
604 		if_put(ifp);
605 		if (m == NULL)
606 			return;
607 	}
608 #endif
609 	/* Call the appropriate IPsec transform callback. */
610 	ip_deliver(&m, &skip, prot, af);
611 #undef IPSEC_ISTAT
612 }
613 
614 int
615 esp_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
616     size_t newlen)
617 {
618 	/* All sysctl names at this level are terminal. */
619 	if (namelen != 1)
620 		return (ENOTDIR);
621 
622 	switch (name[0]) {
623 	case ESPCTL_STATS:
624 		if (newp != NULL)
625 			return (EPERM);
626 		return (sysctl_struct(oldp, oldlenp, newp, newlen,
627 		    &espstat, sizeof(espstat)));
628 	default:
629 		if (name[0] < ESPCTL_MAXID)
630 			return (sysctl_int_arr(espctl_vars, name, namelen,
631 			    oldp, oldlenp, newp, newlen));
632 		return (ENOPROTOOPT);
633 	}
634 }
635 
636 int
637 ah_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
638     size_t newlen)
639 {
640 	/* All sysctl names at this level are terminal. */
641 	if (namelen != 1)
642 		return (ENOTDIR);
643 
644 	switch (name[0]) {
645 	case AHCTL_STATS:
646 		if (newp != NULL)
647 			return (EPERM);
648 		return (sysctl_struct(oldp, oldlenp, newp, newlen,
649 		    &ahstat, sizeof(ahstat)));
650 	default:
651 		if (name[0] < AHCTL_MAXID)
652 			return (sysctl_int_arr(ahctl_vars, name, namelen,
653 			    oldp, oldlenp, newp, newlen));
654 		return (ENOPROTOOPT);
655 	}
656 }
657 
658 int
659 ipcomp_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
660     size_t newlen)
661 {
662 	/* All sysctl names at this level are terminal. */
663 	if (namelen != 1)
664 		return (ENOTDIR);
665 
666 	switch (name[0]) {
667 	case IPCOMPCTL_STATS:
668 		if (newp != NULL)
669 			return (EPERM);
670 		return (sysctl_struct(oldp, oldlenp, newp, newlen,
671 		    &ipcompstat, sizeof(ipcompstat)));
672 	default:
673 		if (name[0] < IPCOMPCTL_MAXID)
674 			return (sysctl_int_arr(ipcompctl_vars, name, namelen,
675 			    oldp, oldlenp, newp, newlen));
676 		return (ENOPROTOOPT);
677 	}
678 }
679 
680 /* IPv4 AH wrapper. */
681 int
682 ah4_input(struct mbuf **mp, int *offp, int proto, int af)
683 {
684 	ipsec_common_input(*mp, *offp, offsetof(struct ip, ip_p), AF_INET,
685 	    proto, 0);
686 	return IPPROTO_DONE;
687 }
688 
689 /* XXX rdomain */
690 void
691 ah4_ctlinput(int cmd, struct sockaddr *sa, u_int rdomain, void *v)
692 {
693 	if (sa->sa_family != AF_INET ||
694 	    sa->sa_len != sizeof(struct sockaddr_in))
695 		return;
696 
697 	ipsec_common_ctlinput(rdomain, cmd, sa, v, IPPROTO_AH);
698 }
699 
700 /* IPv4 ESP wrapper. */
701 int
702 esp4_input(struct mbuf **mp, int *offp, int proto, int af)
703 {
704 	ipsec_common_input(*mp, *offp, offsetof(struct ip, ip_p), AF_INET,
705 	    proto, 0);
706 	return IPPROTO_DONE;
707 }
708 
709 /* IPv4 IPCOMP wrapper */
710 int
711 ipcomp4_input(struct mbuf **mp, int *offp, int proto, int af)
712 {
713 	ipsec_common_input(*mp, *offp, offsetof(struct ip, ip_p), AF_INET,
714 	    proto, 0);
715 	return IPPROTO_DONE;
716 }
717 
718 void
719 ipsec_common_ctlinput(u_int rdomain, int cmd, struct sockaddr *sa,
720     void *v, int proto)
721 {
722 	struct ip *ip = v;
723 
724 	if (cmd == PRC_MSGSIZE && ip && ip_mtudisc && ip->ip_v == 4) {
725 		struct tdb *tdbp;
726 		struct sockaddr_in dst;
727 		struct icmp *icp;
728 		int hlen = ip->ip_hl << 2;
729 		u_int32_t spi, mtu;
730 		ssize_t adjust;
731 
732 		/* Find the right MTU. */
733 		icp = (struct icmp *)((caddr_t) ip -
734 		    offsetof(struct icmp, icmp_ip));
735 		mtu = ntohs(icp->icmp_nextmtu);
736 
737 		/*
738 		 * Ignore the packet, if we do not receive a MTU
739 		 * or the MTU is too small to be acceptable.
740 		 */
741 		if (mtu < 296)
742 			return;
743 
744 		memset(&dst, 0, sizeof(struct sockaddr_in));
745 		dst.sin_family = AF_INET;
746 		dst.sin_len = sizeof(struct sockaddr_in);
747 		dst.sin_addr.s_addr = ip->ip_dst.s_addr;
748 
749 		memcpy(&spi, (caddr_t)ip + hlen, sizeof(u_int32_t));
750 
751 		tdbp = gettdb(rdomain, spi, (union sockaddr_union *)&dst,
752 		    proto);
753 		if (tdbp == NULL || tdbp->tdb_flags & TDBF_INVALID)
754 			return;
755 
756 		/* Walk the chain backwards to the first tdb */
757 		for (; tdbp; tdbp = tdbp->tdb_inext) {
758 			if (tdbp->tdb_flags & TDBF_INVALID ||
759 			    (adjust = ipsec_hdrsz(tdbp)) == -1)
760 				return;
761 
762 			mtu -= adjust;
763 
764 			/* Store adjusted MTU in tdb */
765 			tdbp->tdb_mtu = mtu;
766 			tdbp->tdb_mtutimeout = time_second +
767 			    ip_mtudisc_timeout;
768 			DPRINTF(("ipsec_common_ctlinput: "
769 			    "spi %08x mtu %d adjust %ld\n",
770 			    ntohl(tdbp->tdb_spi), tdbp->tdb_mtu,
771 			    adjust));
772 		}
773 	}
774 }
775 
776 /* XXX rdomain */
777 void
778 udpencap_ctlinput(int cmd, struct sockaddr *sa, u_int rdomain, void *v)
779 {
780 	struct ip *ip = v;
781 	struct tdb *tdbp;
782 	struct icmp *icp;
783 	u_int32_t mtu;
784 	ssize_t adjust;
785 	struct sockaddr_in dst, src;
786 	union sockaddr_union *su_dst, *su_src;
787 
788 	NET_ASSERT_LOCKED();
789 
790 	icp = (struct icmp *)((caddr_t) ip - offsetof(struct icmp, icmp_ip));
791 	mtu = ntohs(icp->icmp_nextmtu);
792 
793 	/*
794 	 * Ignore the packet, if we do not receive a MTU
795 	 * or the MTU is too small to be acceptable.
796 	 */
797 	if (mtu < 296)
798 		return;
799 
800 	memset(&dst, 0, sizeof(dst));
801 	dst.sin_family = AF_INET;
802 	dst.sin_len = sizeof(struct sockaddr_in);
803 	dst.sin_addr.s_addr = ip->ip_dst.s_addr;
804 	su_dst = (union sockaddr_union *)&dst;
805 	memset(&src, 0, sizeof(src));
806 	src.sin_family = AF_INET;
807 	src.sin_len = sizeof(struct sockaddr_in);
808 	src.sin_addr.s_addr = ip->ip_src.s_addr;
809 	su_src = (union sockaddr_union *)&src;
810 
811 	tdbp = gettdbbysrcdst(rdomain, 0, su_src, su_dst, IPPROTO_ESP);
812 
813 	for (; tdbp != NULL; tdbp = tdbp->tdb_snext) {
814 		if (tdbp->tdb_sproto == IPPROTO_ESP &&
815 		    ((tdbp->tdb_flags & (TDBF_INVALID|TDBF_UDPENCAP)) ==
816 		    TDBF_UDPENCAP) &&
817 		    !memcmp(&tdbp->tdb_dst, &dst, su_dst->sa.sa_len) &&
818 		    !memcmp(&tdbp->tdb_src, &src, su_src->sa.sa_len)) {
819 			if ((adjust = ipsec_hdrsz(tdbp)) != -1) {
820 				/* Store adjusted MTU in tdb */
821 				tdbp->tdb_mtu = mtu - adjust;
822 				tdbp->tdb_mtutimeout = time_second +
823 				    ip_mtudisc_timeout;
824 				DPRINTF(("udpencap_ctlinput: "
825 				    "spi %08x mtu %d adjust %ld\n",
826 				    ntohl(tdbp->tdb_spi), tdbp->tdb_mtu,
827 				    adjust));
828 			}
829 		}
830 	}
831 }
832 
833 /* XXX rdomain */
834 void
835 esp4_ctlinput(int cmd, struct sockaddr *sa, u_int rdomain, void *v)
836 {
837 	if (sa->sa_family != AF_INET ||
838 	    sa->sa_len != sizeof(struct sockaddr_in))
839 		return;
840 
841 	ipsec_common_ctlinput(rdomain, cmd, sa, v, IPPROTO_ESP);
842 }
843 
844 #ifdef INET6
845 /* IPv6 AH wrapper. */
846 int
847 ah6_input(struct mbuf **mp, int *offp, int proto, int af)
848 {
849 	int l = 0;
850 	int protoff, nxt;
851 	struct ip6_ext ip6e;
852 
853 	if (*offp < sizeof(struct ip6_hdr)) {
854 		DPRINTF(("ah6_input(): bad offset\n"));
855 		ahstat.ahs_hdrops++;
856 		m_freemp(mp);
857 		return IPPROTO_DONE;
858 	} else if (*offp == sizeof(struct ip6_hdr)) {
859 		protoff = offsetof(struct ip6_hdr, ip6_nxt);
860 	} else {
861 		/* Chase down the header chain... */
862 		protoff = sizeof(struct ip6_hdr);
863 		nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt;
864 
865 		do {
866 			protoff += l;
867 			m_copydata(*mp, protoff, sizeof(ip6e),
868 			    (caddr_t) &ip6e);
869 
870 			if (nxt == IPPROTO_AH)
871 				l = (ip6e.ip6e_len + 2) << 2;
872 			else
873 				l = (ip6e.ip6e_len + 1) << 3;
874 #ifdef DIAGNOSTIC
875 			if (l <= 0)
876 				panic("ah6_input: l went zero or negative");
877 #endif
878 
879 			nxt = ip6e.ip6e_nxt;
880 		} while (protoff + l < *offp);
881 
882 		/* Malformed packet check */
883 		if (protoff + l != *offp) {
884 			DPRINTF(("ah6_input(): bad packet header chain\n"));
885 			ahstat.ahs_hdrops++;
886 			m_freemp(mp);
887 			return IPPROTO_DONE;
888 		}
889 		protoff += offsetof(struct ip6_ext, ip6e_nxt);
890 	}
891 	ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto, 0);
892 	return IPPROTO_DONE;
893 }
894 
895 /* IPv6 ESP wrapper. */
896 int
897 esp6_input(struct mbuf **mp, int *offp, int proto, int af)
898 {
899 	int l = 0;
900 	int protoff, nxt;
901 	struct ip6_ext ip6e;
902 
903 	if (*offp < sizeof(struct ip6_hdr)) {
904 		DPRINTF(("esp6_input(): bad offset\n"));
905 		espstat.esps_hdrops++;
906 		m_freemp(mp);
907 		return IPPROTO_DONE;
908 	} else if (*offp == sizeof(struct ip6_hdr)) {
909 		protoff = offsetof(struct ip6_hdr, ip6_nxt);
910 	} else {
911 		/* Chase down the header chain... */
912 		protoff = sizeof(struct ip6_hdr);
913 		nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt;
914 
915 		do {
916 			protoff += l;
917 			m_copydata(*mp, protoff, sizeof(ip6e),
918 			    (caddr_t) &ip6e);
919 
920 			if (nxt == IPPROTO_AH)
921 				l = (ip6e.ip6e_len + 2) << 2;
922 			else
923 				l = (ip6e.ip6e_len + 1) << 3;
924 #ifdef DIAGNOSTIC
925 			if (l <= 0)
926 				panic("esp6_input: l went zero or negative");
927 #endif
928 
929 			nxt = ip6e.ip6e_nxt;
930 		} while (protoff + l < *offp);
931 
932 		/* Malformed packet check */
933 		if (protoff + l != *offp) {
934 			DPRINTF(("esp6_input(): bad packet header chain\n"));
935 			espstat.esps_hdrops++;
936 			m_freemp(mp);
937 			return IPPROTO_DONE;
938 		}
939 		protoff += offsetof(struct ip6_ext, ip6e_nxt);
940 	}
941 	ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto, 0);
942 	return IPPROTO_DONE;
943 
944 }
945 
946 /* IPv6 IPcomp wrapper */
947 int
948 ipcomp6_input(struct mbuf **mp, int *offp, int proto, int af)
949 {
950 	int l = 0;
951 	int protoff, nxt;
952 	struct ip6_ext ip6e;
953 
954 	if (*offp < sizeof(struct ip6_hdr)) {
955 		DPRINTF(("ipcomp6_input(): bad offset\n"));
956 		ipcompstat.ipcomps_hdrops++;
957 		m_freemp(mp);
958 		return IPPROTO_DONE;
959 	} else if (*offp == sizeof(struct ip6_hdr)) {
960 		protoff = offsetof(struct ip6_hdr, ip6_nxt);
961 	} else {
962 		/* Chase down the header chain... */
963 		protoff = sizeof(struct ip6_hdr);
964 		nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt;
965 
966 		do {
967 			protoff += l;
968 			m_copydata(*mp, protoff, sizeof(ip6e),
969 			    (caddr_t) &ip6e);
970 			if (nxt == IPPROTO_AH)
971 				l = (ip6e.ip6e_len + 2) << 2;
972 			else
973 				l = (ip6e.ip6e_len + 1) << 3;
974 #ifdef DIAGNOSTIC
975 			if (l <= 0)
976 				panic("ipcomp6_input: l went zero or negative");
977 #endif
978 
979 			nxt = ip6e.ip6e_nxt;
980 		} while (protoff + l < *offp);
981 
982 		/* Malformed packet check */
983 		if (protoff + l != *offp) {
984 			DPRINTF(("ipcomp6_input(): bad packet header chain\n"));
985 			ipcompstat.ipcomps_hdrops++;
986 			m_freemp(mp);
987 			return IPPROTO_DONE;
988 		}
989 
990 		protoff += offsetof(struct ip6_ext, ip6e_nxt);
991 	}
992 	ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto, 0);
993 	return IPPROTO_DONE;
994 }
995 #endif /* INET6 */
996 
997 int
998 ipsec_forward_check(struct mbuf *m, int hlen, int af)
999 {
1000 	struct tdb *tdb;
1001 	struct tdb_ident *tdbi;
1002 	struct m_tag *mtag;
1003 	int error = 0;
1004 
1005 	/*
1006 	 * IPsec policy check for forwarded packets. Look at
1007 	 * inner-most IPsec SA used.
1008 	 */
1009 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
1010 	if (mtag != NULL) {
1011 		tdbi = (struct tdb_ident *)(mtag + 1);
1012 		tdb = gettdb(tdbi->rdomain, tdbi->spi, &tdbi->dst, tdbi->proto);
1013 	} else
1014 		tdb = NULL;
1015 	ipsp_spd_lookup(m, af, hlen, &error, IPSP_DIRECTION_IN, tdb, NULL, 0);
1016 
1017 	return error;
1018 }
1019 
1020 int
1021 ipsec_local_check(struct mbuf *m, int hlen, int proto, int af)
1022 {
1023 	struct tdb *tdb;
1024 	struct tdb_ident *tdbi;
1025 	struct m_tag *mtag;
1026 	int error = 0;
1027 
1028 	/*
1029 	 * If it's a protected packet for us, skip the policy check.
1030 	 * That's because we really only care about the properties of
1031 	 * the protected packet, and not the intermediate versions.
1032 	 * While this is not the most paranoid setting, it allows
1033 	 * some flexibility in handling nested tunnels (in setting up
1034 	 * the policies).
1035 	 */
1036 	if ((proto == IPPROTO_ESP) || (proto == IPPROTO_AH) ||
1037 	    (proto == IPPROTO_IPCOMP))
1038 		return 0;
1039 
1040 	/*
1041 	 * If the protected packet was tunneled, then we need to
1042 	 * verify the protected packet's information, not the
1043 	 * external headers. Thus, skip the policy lookup for the
1044 	 * external packet, and keep the IPsec information linked on
1045 	 * the packet header (the encapsulation routines know how
1046 	 * to deal with that).
1047 	 */
1048 	if ((proto == IPPROTO_IPV4) || (proto == IPPROTO_IPV6))
1049 		return 0;
1050 
1051 	/*
1052 	 * When processing IPv6 header chains, do not look at the
1053 	 * outer header.  The inner protocol is relevant and will
1054 	 * be checked by the local delivery loop later.
1055 	 */
1056 	if ((af == AF_INET6) && ((proto == IPPROTO_DSTOPTS) ||
1057 	    (proto == IPPROTO_ROUTING) || (proto == IPPROTO_FRAGMENT)))
1058 		return 0;
1059 
1060 	/*
1061 	 * If the protected packet is TCP or UDP, we'll do the
1062 	 * policy check in the respective input routine, so we can
1063 	 * check for bypass sockets.
1064 	 */
1065 	if ((proto == IPPROTO_TCP) || (proto == IPPROTO_UDP))
1066 		return 0;
1067 
1068 	/*
1069 	 * IPsec policy check for local-delivery packets. Look at the
1070 	 * inner-most SA that protected the packet. This is in fact
1071 	 * a bit too restrictive (it could end up causing packets to
1072 	 * be dropped that semantically follow the policy, e.g., in
1073 	 * certain SA-bundle configurations); but the alternative is
1074 	 * very complicated (and requires keeping track of what
1075 	 * kinds of tunneling headers have been seen in-between the
1076 	 * IPsec headers), and I don't think we lose much functionality
1077 	 * that's needed in the real world (who uses bundles anyway ?).
1078 	 */
1079 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
1080 	if (mtag) {
1081 		tdbi = (struct tdb_ident *)(mtag + 1);
1082 		tdb = gettdb(tdbi->rdomain, tdbi->spi, &tdbi->dst,
1083 		    tdbi->proto);
1084 	} else
1085 		tdb = NULL;
1086 	ipsp_spd_lookup(m, af, hlen, &error, IPSP_DIRECTION_IN,
1087 	    tdb, NULL, 0);
1088 
1089 	return error;
1090 }
1091