xref: /netbsd-src/sys/netipsec/ipsec_input.c (revision dd3ee07da436799d8de85f3055253118b76bf345)
1 /*	$NetBSD: ipsec_input.c,v 1.77 2022/05/24 20:50:20 andvar Exp $	*/
2 /*	$FreeBSD: ipsec_input.c,v 1.2.4.2 2003/03/28 20:32:53 sam Exp $	*/
3 /*	$OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $	*/
4 
5 /*
6  * The authors of this code are John Ioannidis (ji@tla.org),
7  * Angelos D. Keromytis (kermit@csd.uch.gr) and
8  * Niels Provos (provos@physnet.uni-hamburg.de).
9  *
10  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
11  * in November 1995.
12  *
13  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
14  * by Angelos D. Keromytis.
15  *
16  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
17  * and Niels Provos.
18  *
19  * Additional features in 1999 by Angelos D. Keromytis.
20  *
21  * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
22  * Angelos D. Keromytis and Niels Provos.
23  * Copyright (c) 2001, Angelos D. Keromytis.
24  *
25  * Permission to use, copy, and modify this software with or without fee
26  * is hereby granted, provided that this entire notice is included in
27  * all copies of any software which is or includes a copy or
28  * modification of this software.
29  * You may use this code under the GNU public license if you so wish. Please
30  * contribute changes back to the authors under this freer than GPL license
31  * so that we may further the use of strong encryption without limitations to
32  * all.
33  *
34  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
35  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
36  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
37  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
38  * PURPOSE.
39  */
40 
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: ipsec_input.c,v 1.77 2022/05/24 20:50:20 andvar Exp $");
43 
44 /*
45  * IPsec input processing.
46  */
47 
48 #if defined(_KERNEL_OPT)
49 #include "opt_inet.h"
50 #endif
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/mbuf.h>
55 #include <sys/domain.h>
56 #include <sys/protosw.h>
57 #include <sys/socket.h>
58 #include <sys/errno.h>
59 #include <sys/syslog.h>
60 
61 #include <net/if.h>
62 #include <net/route.h>
63 
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/in_var.h>
69 #include <netinet/in_proto.h>
70 #include <netinet/udp.h>
71 #include <netinet/tcp.h>
72 
73 #include <netinet/ip6.h>
74 #ifdef INET6
75 #include <netinet6/in6.h>
76 #include <netinet6/ip6_var.h>
77 #include <netinet6/ip6_private.h>
78 #include <netinet6/scope6_var.h>
79 #endif
80 #include <netinet/in_pcb.h>
81 
82 #include <netipsec/ipsec.h>
83 #include <netipsec/ipsec_private.h>
84 #ifdef INET6
85 #include <netipsec/ipsec6.h>
86 #endif
87 #include <netipsec/ah_var.h>
88 #include <netipsec/esp.h>
89 #include <netipsec/esp_var.h>
90 #include <netipsec/ipcomp_var.h>
91 
92 #include <netipsec/key.h>
93 #include <netipsec/keydb.h>
94 
95 #include <netipsec/xform.h>
96 #include <netinet6/ip6protosw.h>
97 
98 #define	IPSEC_ISTAT(p, x, y, z)						\
99 do {									\
100 	switch (p) {							\
101 	case IPPROTO_ESP:						\
102 		ESP_STATINC(x);						\
103 		break;							\
104 	case IPPROTO_AH:						\
105 		AH_STATINC(y);						\
106 		break;							\
107 	default:							\
108 		IPCOMP_STATINC(z);					\
109 		break;							\
110 	}								\
111 } while (/*CONSTCOND*/0)
112 
113 /*
114  * fixup TCP/UDP checksum
115  *
116  * XXX: if we have NAT-OA payload from IKE server,
117  *      we must do the differential update of checksum.
118  *
119  * XXX: NAT-OAi/NAT-OAr derived from IKE initiator/responder.
120  *      how to know the IKE side from kernel?
121  */
122 static struct mbuf *
123 ipsec4_fixup_checksum(struct mbuf *m)
124 {
125 	struct ip *ip;
126 	struct tcphdr *th;
127 	struct udphdr *uh;
128 	int poff, off;
129 	int plen;
130 
131 	if (m->m_len < sizeof(*ip)) {
132 		m = m_pullup(m, sizeof(*ip));
133 		if (m == NULL)
134 			return NULL;
135 	}
136 	ip = mtod(m, struct ip *);
137 	poff = ip->ip_hl << 2;
138 	plen = ntohs(ip->ip_len) - poff;
139 
140 	switch (ip->ip_p) {
141 	case IPPROTO_TCP:
142 		M_REGION_GET(th, struct tcphdr *, m, poff, sizeof(*th));
143 		if (th == NULL)
144 			return NULL;
145 		off = th->th_off << 2;
146 		if (off < sizeof(*th) || off > plen) {
147 			m_freem(m);
148 			return NULL;
149 		}
150 		th->th_sum = 0;
151 		th->th_sum = in4_cksum(m, IPPROTO_TCP, poff, plen);
152 		break;
153 	case IPPROTO_UDP:
154 		M_REGION_GET(uh, struct udphdr *, m, poff, sizeof(*uh));
155 		if (uh == NULL)
156 			return NULL;
157 		off = sizeof(*uh);
158 		if (off > plen) {
159 			m_freem(m);
160 			return NULL;
161 		}
162 		uh->uh_sum = 0;
163 		uh->uh_sum = in4_cksum(m, IPPROTO_UDP, poff, plen);
164 		break;
165 	default:
166 		/* no checksum */
167 		return m;
168 	}
169 
170 	return m;
171 }
172 
173 static void
174 nat_t_ports_get(struct mbuf *m, uint16_t *dport, uint16_t *sport)
175 {
176 	struct m_tag *tag;
177 
178 	if ((tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS))) {
179 		*sport = ((uint16_t *)(tag + 1))[0];
180 		*dport = ((uint16_t *)(tag + 1))[1];
181 	} else
182 		*sport = *dport = 0;
183 }
184 
185 static uint32_t
186 spi_get(struct mbuf *m, int sproto, int skip)
187 {
188 	uint32_t spi;
189 	uint16_t cpi;
190 
191 	switch (sproto) {
192 	case IPPROTO_ESP:
193 		m_copydata(m, skip, sizeof(spi), &spi);
194 		return spi;
195 	case IPPROTO_AH:
196 		m_copydata(m, skip + sizeof(spi), sizeof(spi), &spi);
197 		return spi;
198 	case IPPROTO_IPCOMP:
199 		m_copydata(m, skip + sizeof(cpi), sizeof(cpi), &cpi);
200 		return htonl(ntohs(cpi));
201 	default:
202 		panic("%s called with bad protocol number: %d\n", __func__,
203 		    sproto);
204 	}
205 }
206 
207 
208 /*
209  * ipsec_common_input gets called when an IPsec-protected packet
210  * is received by IPv4 or IPv6.  Its job is to find the right SA
211  * and call the appropriate transform.  The transform callback
212  * takes care of further processing (like ingress filtering).
213  */
214 static int
215 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
216 {
217 	char buf[IPSEC_ADDRSTRLEN];
218 	union sockaddr_union dst_address;
219 	struct secasvar *sav;
220 	u_int32_t spi;
221 	u_int16_t sport;
222 	u_int16_t dport;
223 	int s, error;
224 
225 	IPSEC_ISTAT(sproto, ESP_STAT_INPUT, AH_STAT_INPUT,
226 		IPCOMP_STAT_INPUT);
227 
228 	KASSERT(m != NULL);
229 
230 	if ((sproto == IPPROTO_ESP && !esp_enable) ||
231 	    (sproto == IPPROTO_AH && !ah_enable) ||
232 	    (sproto == IPPROTO_IPCOMP && !ipcomp_enable)) {
233 		m_freem(m);
234 		IPSEC_ISTAT(sproto, ESP_STAT_PDROPS, AH_STAT_PDROPS,
235 		    IPCOMP_STAT_PDROPS);
236 		return EOPNOTSUPP;
237 	}
238 
239 	if (m->m_pkthdr.len - skip < 2 * sizeof(u_int32_t)) {
240 		m_freem(m);
241 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
242 		    IPCOMP_STAT_HDROPS);
243 		IPSECLOG(LOG_DEBUG, "packet too small\n");
244 		return EINVAL;
245 	}
246 
247 	/* Retrieve the SPI from the relevant IPsec header */
248 	spi = spi_get(m, sproto, skip);
249 
250 	/* find the source port for NAT-T */
251 	nat_t_ports_get(m, &dport, &sport);
252 
253 	/*
254 	 * Find the SA and (indirectly) call the appropriate
255 	 * kernel crypto routine. The resulting mbuf chain is a valid
256 	 * IP packet ready to go through input processing.
257 	 */
258 	memset(&dst_address, 0, sizeof(dst_address));
259 	dst_address.sa.sa_family = af;
260 	switch (af) {
261 #ifdef INET
262 	case AF_INET:
263 		dst_address.sin.sin_len = sizeof(struct sockaddr_in);
264 		m_copydata(m, offsetof(struct ip, ip_dst),
265 		    sizeof(struct in_addr),
266 		    &dst_address.sin.sin_addr);
267 		break;
268 #endif
269 #ifdef INET6
270 	case AF_INET6:
271 		dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
272 		m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
273 		    sizeof(struct in6_addr),
274 		    &dst_address.sin6.sin6_addr);
275 		if (sa6_recoverscope(&dst_address.sin6)) {
276 			m_freem(m);
277 			return EINVAL;
278 		}
279 		break;
280 #endif
281 	default:
282 		IPSECLOG(LOG_DEBUG, "unsupported protocol family %u\n", af);
283 		m_freem(m);
284 		IPSEC_ISTAT(sproto, ESP_STAT_NOPF, AH_STAT_NOPF,
285 		    IPCOMP_STAT_NOPF);
286 		return EPFNOSUPPORT;
287 	}
288 
289 	s = splsoftnet();
290 
291 	/* NB: only pass dst since key_lookup_sa follows RFC2401 */
292 	sav = KEY_LOOKUP_SA(&dst_address, sproto, spi, sport, dport);
293 	if (sav == NULL) {
294 		IPSECLOG(LOG_DEBUG,
295 		    "no key association found for SA %s/%08lx/%u/%u\n",
296 		    ipsec_address(&dst_address, buf, sizeof(buf)),
297 		    (u_long) ntohl(spi), sproto, ntohs(dport));
298 		IPSEC_ISTAT(sproto, ESP_STAT_NOTDB, AH_STAT_NOTDB,
299 		    IPCOMP_STAT_NOTDB);
300 		splx(s);
301 		m_freem(m);
302 		return ENOENT;
303 	}
304 
305 	KASSERT(sav->tdb_xform != NULL);
306 
307 	/*
308 	 * Call appropriate transform and return -- callback takes care of
309 	 * everything else.
310 	 */
311 	error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
312 	KEY_SA_UNREF(&sav);
313 	splx(s);
314 	return error;
315 }
316 
317 #ifdef INET
318 /*
319  * Common input handler for IPv4 AH, ESP, and IPCOMP.
320  */
321 void
322 ipsec4_common_input(struct mbuf *m, int off, int proto)
323 {
324 	(void)ipsec_common_input(m, off, offsetof(struct ip, ip_p),
325 	    AF_INET, proto);
326 }
327 
328 /*
329  * IPsec input callback for INET protocols.
330  * This routine is called as the transform callback.
331  * Takes care of filtering and other sanity checks on
332  * the processed packet.
333  */
334 int
335 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
336     int skip, int protoff)
337 {
338 	int prot, af __diagused, sproto;
339 	struct ip *ip;
340 	struct secasindex *saidx;
341 	int error;
342 
343 	if (__predict_false(m == NULL)) {
344 		panic("%s: NULL mbuf", __func__);
345 	}
346 	if (__predict_false(skip < sizeof(struct ip))) {
347 		panic("%s: short skip", __func__);
348 	}
349 
350 	KASSERT(sav != NULL);
351 	saidx = &sav->sah->saidx;
352 	af = saidx->dst.sa.sa_family;
353 	KASSERTMSG(af == AF_INET, "unexpected af %u", af);
354 	sproto = saidx->proto;
355 	KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
356 	    sproto == IPPROTO_IPCOMP,
357 	    "unexpected security protocol %u", sproto);
358 
359 	/*
360 	 * Update the IPv4 header. The length of the packet may have changed,
361 	 * so fix it, and recompute the checksum.
362 	 */
363 	if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
364 		char buf[IPSEC_ADDRSTRLEN];
365 cantpull:
366 		IPSECLOG(LOG_DEBUG,
367 		    "processing failed for SA %s/%08lx\n",
368 		    ipsec_address(&sav->sah->saidx.dst, buf,
369 		    sizeof(buf)), (u_long) ntohl(sav->spi));
370 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
371 		    IPCOMP_STAT_HDROPS);
372 		error = ENOBUFS;
373 		goto bad;
374 	}
375 	ip = mtod(m, struct ip *);
376 	ip->ip_len = htons(m->m_pkthdr.len);
377 	ip->ip_sum = 0;
378 	ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
379 
380 	/*
381 	 * Update TCP/UDP checksum
382 	 * XXX: should only do it in NAT-T case
383 	 * XXX: should do it incrementally, see FreeBSD code.
384 	 */
385 	m = ipsec4_fixup_checksum(m);
386 	if (m == NULL)
387 		goto cantpull;
388 	ip = mtod(m, struct ip *);
389 
390 	prot = ip->ip_p;
391 
392 	M_VERIFY_PACKET(m);
393 
394 	key_sa_recordxfer(sav, m);
395 
396 	if ((inetsw[ip_protox[prot]].pr_flags & PR_LASTHDR) != 0 &&
397 	    ipsec_in_reject(m, NULL)) {
398 		error = EINVAL;
399 		goto bad;
400 	}
401 
402 	/*
403 	 * There is no struct ifnet for tunnel mode IP-IP tunnel connecttion,
404 	 * so we cannot write filtering rule to the inner packet.
405 	 */
406 	if (saidx->mode == IPSEC_MODE_TUNNEL)
407 		m->m_pkthdr.pkthdr_flags |= PKTHDR_FLAG_IPSEC_SKIP_PFIL;
408 
409 	(*inetsw[ip_protox[prot]].pr_input)(m, skip, prot);
410 	return 0;
411 
412 bad:
413 	m_freem(m);
414 	return error;
415 }
416 #endif /* INET */
417 
418 #ifdef INET6
419 int
420 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
421 {
422 	int l = 0;
423 	int protoff, nxt;
424 	struct ip6_ext ip6e;
425 
426 	if (*offp < sizeof(struct ip6_hdr)) {
427 		IPSECLOG(LOG_DEBUG, "bad offset %u\n", *offp);
428 		IPSEC_ISTAT(proto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
429 			    IPCOMP_STAT_HDROPS);
430 		m_freem(*mp);
431 		return IPPROTO_DONE;
432 	} else if (*offp == sizeof(struct ip6_hdr)) {
433 		protoff = offsetof(struct ip6_hdr, ip6_nxt);
434 	} else {
435 		/* Chase down the header chain... */
436 		protoff = sizeof(struct ip6_hdr);
437 		nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt;
438 
439 		do {
440 			protoff += l;
441 			m_copydata(*mp, protoff, sizeof(ip6e), &ip6e);
442 
443 			if (nxt == IPPROTO_AH)
444 				l = (ip6e.ip6e_len + 2) << 2;
445 			else if (nxt == IPPROTO_FRAGMENT)
446 				l = sizeof(struct ip6_frag);
447 			else
448 				l = (ip6e.ip6e_len + 1) << 3;
449 			KASSERT(l > 0);
450 
451 			nxt = ip6e.ip6e_nxt;
452 		} while (protoff + l < *offp);
453 
454 		/* Malformed packet check */
455 		if (protoff + l != *offp) {
456 			IPSECLOG(LOG_DEBUG, "bad packet header chain, "
457 			    "protoff %u, l %u, off %u\n", protoff, l, *offp);
458 			IPSEC_ISTAT(proto, ESP_STAT_HDROPS,
459 				    AH_STAT_HDROPS,
460 				    IPCOMP_STAT_HDROPS);
461 			m_freem(*mp);
462 			*mp = NULL;
463 			return IPPROTO_DONE;
464 		}
465 		protoff += offsetof(struct ip6_ext, ip6e_nxt);
466 	}
467 	(void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
468 	return IPPROTO_DONE;
469 }
470 
471 /*
472  * IPsec input callback, called by the transform callback. Takes care of
473  * filtering and other sanity checks on the processed packet.
474  */
475 int
476 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
477     int protoff)
478 {
479 	int af __diagused, sproto;
480 	struct ip6_hdr *ip6;
481 	struct secasindex *saidx;
482 	int nxt;
483 	u_int8_t prot;
484 	int error, nest;
485 
486 	if (__predict_false(m == NULL)) {
487 		panic("%s: NULL mbuf", __func__);
488 	}
489 
490 	KASSERT(sav != NULL);
491 	saidx = &sav->sah->saidx;
492 	af = saidx->dst.sa.sa_family;
493 	KASSERTMSG(af == AF_INET6, "unexpected af %u", af);
494 	sproto = saidx->proto;
495 	KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
496 	    sproto == IPPROTO_IPCOMP,
497 	    "unexpected security protocol %u", sproto);
498 
499 	/* Fix IPv6 header */
500 	if (m->m_len < sizeof(struct ip6_hdr) &&
501 	    (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
502 		char buf[IPSEC_ADDRSTRLEN];
503 		IPSECLOG(LOG_DEBUG, "processing failed for SA %s/%08lx\n",
504 		    ipsec_address(&sav->sah->saidx.dst,
505 		    buf, sizeof(buf)), (u_long) ntohl(sav->spi));
506 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
507 		    IPCOMP_STAT_HDROPS);
508 		error = EACCES;
509 		goto bad;
510 	}
511 
512 	ip6 = mtod(m, struct ip6_hdr *);
513 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
514 
515 	m_copydata(m, protoff, sizeof(prot), &prot);
516 
517 	key_sa_recordxfer(sav, m);
518 
519 	/*
520 	 * See the end of ip6_input for this logic.
521 	 * IPPROTO_IPV[46] case will be processed just like other ones
522 	 */
523 	nest = 0;
524 	nxt = prot;
525 	while (nxt != IPPROTO_DONE) {
526 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
527 			IP6_STATINC(IP6_STAT_TOOMANYHDR);
528 			error = EINVAL;
529 			goto bad;
530 		}
531 
532 		M_VERIFY_PACKET(m);
533 
534 		/*
535 		 * Protection against faulty packet - there should be
536 		 * more sanity checks in header chain processing.
537 		 */
538 		if (m->m_pkthdr.len < skip) {
539 			IP6_STATINC(IP6_STAT_TOOSHORT);
540 			in6_ifstat_inc(m_get_rcvif_NOMPSAFE(m),
541 			    ifs6_in_truncated);
542 			error = EINVAL;
543 			goto bad;
544 		}
545 
546 		/*
547 		 * Enforce IPsec policy checking if we are seeing last header.
548 		 * Note that we do not visit this with protocols with pcb layer
549 		 * code - like udp/tcp/raw ip.
550 		 */
551 		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
552 		    ipsec_in_reject(m, NULL)) {
553 			error = EINVAL;
554 			goto bad;
555 		}
556 
557 		/*
558 		 * There is no struct ifnet for tunnel mode IP-IP tunnel connecttion,
559 		 * so we cannot write filtering rule to the inner packet.
560 		 */
561 		if (saidx->mode == IPSEC_MODE_TUNNEL)
562 			m->m_pkthdr.pkthdr_flags |= PKTHDR_FLAG_IPSEC_SKIP_PFIL;
563 
564 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
565 	}
566 	return 0;
567 
568 bad:
569 	if (m)
570 		m_freem(m);
571 	return error;
572 }
573 #endif /* INET6 */
574