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