xref: /netbsd-src/sys/netipsec/ipsec_input.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: ipsec_input.c,v 1.75 2019/01/27 02:08:48 pgoyette 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.75 2019/01/27 02:08:48 pgoyette 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 drived 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 /*
186  * ipsec_common_input gets called when an IPsec-protected packet
187  * is received by IPv4 or IPv6.  Its job is to find the right SA
188  * and call the appropriate transform.  The transform callback
189  * takes care of further processing (like ingress filtering).
190  */
191 static int
192 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
193 {
194 	char buf[IPSEC_ADDRSTRLEN];
195 	union sockaddr_union dst_address;
196 	struct secasvar *sav;
197 	u_int32_t spi;
198 	u_int16_t sport;
199 	u_int16_t dport;
200 	int s, error;
201 
202 	IPSEC_ISTAT(sproto, ESP_STAT_INPUT, AH_STAT_INPUT,
203 		IPCOMP_STAT_INPUT);
204 
205 	KASSERT(m != NULL);
206 
207 	if ((sproto == IPPROTO_ESP && !esp_enable) ||
208 	    (sproto == IPPROTO_AH && !ah_enable) ||
209 	    (sproto == IPPROTO_IPCOMP && !ipcomp_enable)) {
210 		m_freem(m);
211 		IPSEC_ISTAT(sproto, ESP_STAT_PDROPS, AH_STAT_PDROPS,
212 		    IPCOMP_STAT_PDROPS);
213 		return EOPNOTSUPP;
214 	}
215 
216 	if (m->m_pkthdr.len - skip < 2 * sizeof(u_int32_t)) {
217 		m_freem(m);
218 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
219 		    IPCOMP_STAT_HDROPS);
220 		IPSECLOG(LOG_DEBUG, "packet too small\n");
221 		return EINVAL;
222 	}
223 
224 	/* Retrieve the SPI from the relevant IPsec header */
225 	if (sproto == IPPROTO_ESP) {
226 		m_copydata(m, skip, sizeof(u_int32_t), &spi);
227 	} else if (sproto == IPPROTO_AH) {
228 		m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t), &spi);
229 	} else if (sproto == IPPROTO_IPCOMP) {
230 		u_int16_t cpi;
231 		m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t), &cpi);
232 		spi = ntohl(htons(cpi));
233 	} else {
234 		panic("%s called with bad protocol number: %d\n", __func__,
235 		    sproto);
236 	}
237 
238 	/* find the source port for NAT-T */
239 	nat_t_ports_get(m, &dport, &sport);
240 
241 	/*
242 	 * Find the SA and (indirectly) call the appropriate
243 	 * kernel crypto routine. The resulting mbuf chain is a valid
244 	 * IP packet ready to go through input processing.
245 	 */
246 	memset(&dst_address, 0, sizeof(dst_address));
247 	dst_address.sa.sa_family = af;
248 	switch (af) {
249 #ifdef INET
250 	case AF_INET:
251 		dst_address.sin.sin_len = sizeof(struct sockaddr_in);
252 		m_copydata(m, offsetof(struct ip, ip_dst),
253 		    sizeof(struct in_addr),
254 		    &dst_address.sin.sin_addr);
255 		break;
256 #endif
257 #ifdef INET6
258 	case AF_INET6:
259 		dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
260 		m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
261 		    sizeof(struct in6_addr),
262 		    &dst_address.sin6.sin6_addr);
263 		if (sa6_recoverscope(&dst_address.sin6)) {
264 			m_freem(m);
265 			return EINVAL;
266 		}
267 		break;
268 #endif
269 	default:
270 		IPSECLOG(LOG_DEBUG, "unsupported protocol family %u\n", af);
271 		m_freem(m);
272 		IPSEC_ISTAT(sproto, ESP_STAT_NOPF, AH_STAT_NOPF,
273 		    IPCOMP_STAT_NOPF);
274 		return EPFNOSUPPORT;
275 	}
276 
277 	s = splsoftnet();
278 
279 	/* NB: only pass dst since key_lookup_sa follows RFC2401 */
280 	sav = KEY_LOOKUP_SA(&dst_address, sproto, spi, sport, dport);
281 	if (sav == NULL) {
282 		IPSECLOG(LOG_DEBUG,
283 		    "no key association found for SA %s/%08lx/%u/%u\n",
284 		    ipsec_address(&dst_address, buf, sizeof(buf)),
285 		    (u_long) ntohl(spi), sproto, ntohs(dport));
286 		IPSEC_ISTAT(sproto, ESP_STAT_NOTDB, AH_STAT_NOTDB,
287 		    IPCOMP_STAT_NOTDB);
288 		splx(s);
289 		m_freem(m);
290 		return ENOENT;
291 	}
292 
293 	KASSERT(sav->tdb_xform != NULL);
294 
295 	/*
296 	 * Call appropriate transform and return -- callback takes care of
297 	 * everything else.
298 	 */
299 	error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
300 	KEY_SA_UNREF(&sav);
301 	splx(s);
302 	return error;
303 }
304 
305 #ifdef INET
306 /*
307  * Common input handler for IPv4 AH, ESP, and IPCOMP.
308  */
309 void
310 ipsec4_common_input(struct mbuf *m, int off, int proto)
311 {
312 	(void)ipsec_common_input(m, off, offsetof(struct ip, ip_p),
313 	    AF_INET, proto);
314 }
315 
316 /*
317  * IPsec input callback for INET protocols.
318  * This routine is called as the transform callback.
319  * Takes care of filtering and other sanity checks on
320  * the processed packet.
321  */
322 int
323 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
324     int skip, int protoff)
325 {
326 	int prot, af __diagused, sproto;
327 	struct ip *ip;
328 	struct secasindex *saidx;
329 	int error;
330 
331 	if (__predict_false(m == NULL)) {
332 		panic("%s: NULL mbuf", __func__);
333 	}
334 	if (__predict_false(skip < sizeof(struct ip))) {
335 		panic("%s: short skip", __func__);
336 	}
337 
338 	KASSERT(sav != NULL);
339 	saidx = &sav->sah->saidx;
340 	af = saidx->dst.sa.sa_family;
341 	KASSERTMSG(af == AF_INET, "unexpected af %u", af);
342 	sproto = saidx->proto;
343 	KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
344 	    sproto == IPPROTO_IPCOMP,
345 	    "unexpected security protocol %u", sproto);
346 
347 	/*
348 	 * Update the IPv4 header. The length of the packet may have changed,
349 	 * so fix it, and recompute the checksum.
350 	 */
351 	if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
352 		char buf[IPSEC_ADDRSTRLEN];
353 cantpull:
354 		IPSECLOG(LOG_DEBUG,
355 		    "processing failed for SA %s/%08lx\n",
356 		    ipsec_address(&sav->sah->saidx.dst, buf,
357 		    sizeof(buf)), (u_long) ntohl(sav->spi));
358 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
359 		    IPCOMP_STAT_HDROPS);
360 		error = ENOBUFS;
361 		goto bad;
362 	}
363 	ip = mtod(m, struct ip *);
364 	ip->ip_len = htons(m->m_pkthdr.len);
365 	ip->ip_sum = 0;
366 	ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
367 
368 	/*
369 	 * Update TCP/UDP checksum
370 	 * XXX: should only do it in NAT-T case
371 	 * XXX: should do it incrementally, see FreeBSD code.
372 	 */
373 	m = ipsec4_fixup_checksum(m);
374 	if (m == NULL)
375 		goto cantpull;
376 	ip = mtod(m, struct ip *);
377 
378 	prot = ip->ip_p;
379 
380 	M_VERIFY_PACKET(m);
381 
382 	key_sa_recordxfer(sav, m);
383 
384 	if ((inetsw[ip_protox[prot]].pr_flags & PR_LASTHDR) != 0 &&
385 	    ipsec_in_reject(m, NULL)) {
386 		error = EINVAL;
387 		goto bad;
388 	}
389 
390 	/*
391 	 * There is no struct ifnet for tunnel mode IP-IP tunnel connecttion,
392 	 * so we cannot write filtering rule to the inner packet.
393 	 */
394 	if (saidx->mode == IPSEC_MODE_TUNNEL)
395 		m->m_pkthdr.pkthdr_flags |= PKTHDR_FLAG_IPSEC_SKIP_PFIL;
396 
397 	(*inetsw[ip_protox[prot]].pr_input)(m, skip, prot);
398 	return 0;
399 
400 bad:
401 	m_freem(m);
402 	return error;
403 }
404 #endif /* INET */
405 
406 #ifdef INET6
407 int
408 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
409 {
410 	int l = 0;
411 	int protoff, nxt;
412 	struct ip6_ext ip6e;
413 
414 	if (*offp < sizeof(struct ip6_hdr)) {
415 		IPSECLOG(LOG_DEBUG, "bad offset %u\n", *offp);
416 		IPSEC_ISTAT(proto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
417 			    IPCOMP_STAT_HDROPS);
418 		m_freem(*mp);
419 		return IPPROTO_DONE;
420 	} else if (*offp == sizeof(struct ip6_hdr)) {
421 		protoff = offsetof(struct ip6_hdr, ip6_nxt);
422 	} else {
423 		/* Chase down the header chain... */
424 		protoff = sizeof(struct ip6_hdr);
425 		nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt;
426 
427 		do {
428 			protoff += l;
429 			m_copydata(*mp, protoff, sizeof(ip6e), &ip6e);
430 
431 			if (nxt == IPPROTO_AH)
432 				l = (ip6e.ip6e_len + 2) << 2;
433 			else if (nxt == IPPROTO_FRAGMENT)
434 				l = sizeof(struct ip6_frag);
435 			else
436 				l = (ip6e.ip6e_len + 1) << 3;
437 			KASSERT(l > 0);
438 
439 			nxt = ip6e.ip6e_nxt;
440 		} while (protoff + l < *offp);
441 
442 		/* Malformed packet check */
443 		if (protoff + l != *offp) {
444 			IPSECLOG(LOG_DEBUG, "bad packet header chain, "
445 			    "protoff %u, l %u, off %u\n", protoff, l, *offp);
446 			IPSEC_ISTAT(proto, ESP_STAT_HDROPS,
447 				    AH_STAT_HDROPS,
448 				    IPCOMP_STAT_HDROPS);
449 			m_freem(*mp);
450 			*mp = NULL;
451 			return IPPROTO_DONE;
452 		}
453 		protoff += offsetof(struct ip6_ext, ip6e_nxt);
454 	}
455 	(void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
456 	return IPPROTO_DONE;
457 }
458 
459 /*
460  * IPsec input callback, called by the transform callback. Takes care of
461  * filtering and other sanity checks on the processed packet.
462  */
463 int
464 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
465     int protoff)
466 {
467 	int af __diagused, sproto;
468 	struct ip6_hdr *ip6;
469 	struct secasindex *saidx;
470 	int nxt;
471 	u_int8_t prot;
472 	int error, nest;
473 
474 	if (__predict_false(m == NULL)) {
475 		panic("%s: NULL mbuf", __func__);
476 	}
477 
478 	KASSERT(sav != NULL);
479 	saidx = &sav->sah->saidx;
480 	af = saidx->dst.sa.sa_family;
481 	KASSERTMSG(af == AF_INET6, "unexpected af %u", af);
482 	sproto = saidx->proto;
483 	KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
484 	    sproto == IPPROTO_IPCOMP,
485 	    "unexpected security protocol %u", sproto);
486 
487 	/* Fix IPv6 header */
488 	if (m->m_len < sizeof(struct ip6_hdr) &&
489 	    (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
490 		char buf[IPSEC_ADDRSTRLEN];
491 		IPSECLOG(LOG_DEBUG, "processing failed for SA %s/%08lx\n",
492 		    ipsec_address(&sav->sah->saidx.dst,
493 		    buf, sizeof(buf)), (u_long) ntohl(sav->spi));
494 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
495 		    IPCOMP_STAT_HDROPS);
496 		error = EACCES;
497 		goto bad;
498 	}
499 
500 	ip6 = mtod(m, struct ip6_hdr *);
501 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
502 
503 	m_copydata(m, protoff, sizeof(prot), &prot);
504 
505 	key_sa_recordxfer(sav, m);
506 
507 	/*
508 	 * See the end of ip6_input for this logic.
509 	 * IPPROTO_IPV[46] case will be processed just like other ones
510 	 */
511 	nest = 0;
512 	nxt = prot;
513 	while (nxt != IPPROTO_DONE) {
514 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
515 			IP6_STATINC(IP6_STAT_TOOMANYHDR);
516 			error = EINVAL;
517 			goto bad;
518 		}
519 
520 		M_VERIFY_PACKET(m);
521 
522 		/*
523 		 * Protection against faulty packet - there should be
524 		 * more sanity checks in header chain processing.
525 		 */
526 		if (m->m_pkthdr.len < skip) {
527 			IP6_STATINC(IP6_STAT_TOOSHORT);
528 			in6_ifstat_inc(m_get_rcvif_NOMPSAFE(m),
529 			    ifs6_in_truncated);
530 			error = EINVAL;
531 			goto bad;
532 		}
533 
534 		/*
535 		 * Enforce IPsec policy checking if we are seeing last header.
536 		 * Note that we do not visit this with protocols with pcb layer
537 		 * code - like udp/tcp/raw ip.
538 		 */
539 		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
540 		    ipsec_in_reject(m, NULL)) {
541 			error = EINVAL;
542 			goto bad;
543 		}
544 
545 		/*
546 		 * There is no struct ifnet for tunnel mode IP-IP tunnel connecttion,
547 		 * so we cannot write filtering rule to the inner packet.
548 		 */
549 		if (saidx->mode == IPSEC_MODE_TUNNEL)
550 			m->m_pkthdr.pkthdr_flags |= PKTHDR_FLAG_IPSEC_SKIP_PFIL;
551 
552 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
553 	}
554 	return 0;
555 
556 bad:
557 	if (m)
558 		m_freem(m);
559 	return error;
560 }
561 #endif /* INET6 */
562