ipsec_input.c revision 1.78 1 /* $NetBSD: ipsec_input.c,v 1.78 2022/08/23 09:25:10 knakahara 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 (at) tla.org),
7 * Angelos D. Keromytis (kermit (at) csd.uch.gr) and
8 * Niels Provos (provos (at) 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.78 2022/08/23 09:25:10 knakahara 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], buf2[IPSEC_ADDRSTRLEN];
218 union sockaddr_union src_address, 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(&src_address, 0, sizeof (src_address));
259 memset(&dst_address, 0, sizeof(dst_address));
260 src_address.sa.sa_family = af;
261 dst_address.sa.sa_family = af;
262 switch (af) {
263 #ifdef INET
264 case AF_INET:
265 src_address.sin.sin_len = sizeof(struct sockaddr_in);
266 dst_address.sin.sin_len = sizeof(struct sockaddr_in);
267 m_copydata(m, offsetof(struct ip, ip_src),
268 sizeof(struct in_addr),
269 &src_address.sin.sin_addr);
270 m_copydata(m, offsetof(struct ip, ip_dst),
271 sizeof(struct in_addr),
272 &dst_address.sin.sin_addr);
273 break;
274 #endif
275 #ifdef INET6
276 case AF_INET6:
277 src_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
278 dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
279 m_copydata(m, offsetof(struct ip6_hdr, ip6_src),
280 sizeof(struct in6_addr),
281 &src_address.sin6.sin6_addr);
282 m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
283 sizeof(struct in6_addr),
284 &dst_address.sin6.sin6_addr);
285 if (sa6_recoverscope(&dst_address.sin6)) {
286 m_freem(m);
287 return EINVAL;
288 }
289 break;
290 #endif
291 default:
292 IPSECLOG(LOG_DEBUG, "unsupported protocol family %u\n", af);
293 m_freem(m);
294 IPSEC_ISTAT(sproto, ESP_STAT_NOPF, AH_STAT_NOPF,
295 IPCOMP_STAT_NOPF);
296 return EPFNOSUPPORT;
297 }
298
299 s = splsoftnet();
300
301 /* NB: only pass dst since key_lookup_sa follows RFC2401 */
302 sav = KEY_LOOKUP_SA(&dst_address, sproto, spi, sport, dport);
303 if (sav == NULL) {
304 static struct timeval lasttime = {0, 0};
305 static int curpps = 0;
306
307 if (!ipsec_debug && ppsratecheck(&lasttime, &curpps, 1)) {
308 if (sport || dport) {
309 log(LOG_INFO,
310 "no key association found for SA"
311 " %s[%u]-%s[%u]/SPI 0x%08lx\n",
312 ipsec_address(&src_address, buf, sizeof(buf)),
313 ntohs(sport),
314 ipsec_address(&dst_address, buf2, sizeof(buf2)),
315 ntohs(dport),
316 (u_long) ntohl(spi));
317 } else {
318 log(LOG_INFO,
319 "no key association found for"
320 " SA %s-%s/SPI 0x%08lx\n",
321 ipsec_address(&src_address, buf, sizeof(buf)),
322 ipsec_address(&src_address, buf2, sizeof(buf2)),
323 (u_long) ntohl(spi));
324 }
325 } else if (ipsec_debug) {
326 IPSECLOG(LOG_DEBUG,
327 "no key association found for SA "
328 "%s-%s/SPI 0x%08lx/PROTO %u/PORT %u-%u\n",
329 ipsec_address(&src_address, buf, sizeof(buf)),
330 ipsec_address(&dst_address, buf2, sizeof(buf2)),
331 (u_long) ntohl(spi), sproto, ntohs(dport), ntohs(sport));
332 }
333 IPSEC_ISTAT(sproto, ESP_STAT_NOTDB, AH_STAT_NOTDB,
334 IPCOMP_STAT_NOTDB);
335 splx(s);
336 m_freem(m);
337 return ENOENT;
338 }
339
340 KASSERT(sav->tdb_xform != NULL);
341
342 /*
343 * Call appropriate transform and return -- callback takes care of
344 * everything else.
345 */
346 error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
347 KEY_SA_UNREF(&sav);
348 splx(s);
349 return error;
350 }
351
352 #ifdef INET
353 /*
354 * Common input handler for IPv4 AH, ESP, and IPCOMP.
355 */
356 void
357 ipsec4_common_input(struct mbuf *m, int off, int proto)
358 {
359 (void)ipsec_common_input(m, off, offsetof(struct ip, ip_p),
360 AF_INET, proto);
361 }
362
363 /*
364 * IPsec input callback for INET protocols.
365 * This routine is called as the transform callback.
366 * Takes care of filtering and other sanity checks on
367 * the processed packet.
368 */
369 int
370 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
371 int skip, int protoff)
372 {
373 int prot, af __diagused, sproto;
374 struct ip *ip;
375 struct secasindex *saidx;
376 int error;
377
378 if (__predict_false(m == NULL)) {
379 panic("%s: NULL mbuf", __func__);
380 }
381 if (__predict_false(skip < sizeof(struct ip))) {
382 panic("%s: short skip", __func__);
383 }
384
385 KASSERT(sav != NULL);
386 saidx = &sav->sah->saidx;
387 af = saidx->dst.sa.sa_family;
388 KASSERTMSG(af == AF_INET, "unexpected af %u", af);
389 sproto = saidx->proto;
390 KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
391 sproto == IPPROTO_IPCOMP,
392 "unexpected security protocol %u", sproto);
393
394 /*
395 * Update the IPv4 header. The length of the packet may have changed,
396 * so fix it, and recompute the checksum.
397 */
398 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
399 char buf[IPSEC_ADDRSTRLEN];
400 cantpull:
401 IPSECLOG(LOG_DEBUG,
402 "processing failed for SA %s/%08lx\n",
403 ipsec_address(&sav->sah->saidx.dst, buf,
404 sizeof(buf)), (u_long) ntohl(sav->spi));
405 IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
406 IPCOMP_STAT_HDROPS);
407 error = ENOBUFS;
408 goto bad;
409 }
410 ip = mtod(m, struct ip *);
411 ip->ip_len = htons(m->m_pkthdr.len);
412 ip->ip_sum = 0;
413 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
414
415 /*
416 * Update TCP/UDP checksum
417 * XXX: should only do it in NAT-T case
418 * XXX: should do it incrementally, see FreeBSD code.
419 */
420 m = ipsec4_fixup_checksum(m);
421 if (m == NULL)
422 goto cantpull;
423 ip = mtod(m, struct ip *);
424
425 prot = ip->ip_p;
426
427 M_VERIFY_PACKET(m);
428
429 key_sa_recordxfer(sav, m);
430
431 if ((inetsw[ip_protox[prot]].pr_flags & PR_LASTHDR) != 0 &&
432 ipsec_in_reject(m, NULL)) {
433 error = EINVAL;
434 goto bad;
435 }
436
437 /*
438 * There is no struct ifnet for tunnel mode IP-IP tunnel connecttion,
439 * so we cannot write filtering rule to the inner packet.
440 */
441 if (saidx->mode == IPSEC_MODE_TUNNEL)
442 m->m_pkthdr.pkthdr_flags |= PKTHDR_FLAG_IPSEC_SKIP_PFIL;
443
444 (*inetsw[ip_protox[prot]].pr_input)(m, skip, prot);
445 return 0;
446
447 bad:
448 m_freem(m);
449 return error;
450 }
451 #endif /* INET */
452
453 #ifdef INET6
454 int
455 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
456 {
457 int l = 0;
458 int protoff, nxt;
459 struct ip6_ext ip6e;
460
461 if (*offp < sizeof(struct ip6_hdr)) {
462 IPSECLOG(LOG_DEBUG, "bad offset %u\n", *offp);
463 IPSEC_ISTAT(proto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
464 IPCOMP_STAT_HDROPS);
465 m_freem(*mp);
466 return IPPROTO_DONE;
467 } else if (*offp == sizeof(struct ip6_hdr)) {
468 protoff = offsetof(struct ip6_hdr, ip6_nxt);
469 } else {
470 /* Chase down the header chain... */
471 protoff = sizeof(struct ip6_hdr);
472 nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt;
473
474 do {
475 protoff += l;
476 m_copydata(*mp, protoff, sizeof(ip6e), &ip6e);
477
478 if (nxt == IPPROTO_AH)
479 l = (ip6e.ip6e_len + 2) << 2;
480 else if (nxt == IPPROTO_FRAGMENT)
481 l = sizeof(struct ip6_frag);
482 else
483 l = (ip6e.ip6e_len + 1) << 3;
484 KASSERT(l > 0);
485
486 nxt = ip6e.ip6e_nxt;
487 } while (protoff + l < *offp);
488
489 /* Malformed packet check */
490 if (protoff + l != *offp) {
491 IPSECLOG(LOG_DEBUG, "bad packet header chain, "
492 "protoff %u, l %u, off %u\n", protoff, l, *offp);
493 IPSEC_ISTAT(proto, ESP_STAT_HDROPS,
494 AH_STAT_HDROPS,
495 IPCOMP_STAT_HDROPS);
496 m_freem(*mp);
497 *mp = NULL;
498 return IPPROTO_DONE;
499 }
500 protoff += offsetof(struct ip6_ext, ip6e_nxt);
501 }
502 (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
503 return IPPROTO_DONE;
504 }
505
506 /*
507 * IPsec input callback, called by the transform callback. Takes care of
508 * filtering and other sanity checks on the processed packet.
509 */
510 int
511 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
512 int protoff)
513 {
514 int af __diagused, sproto;
515 struct ip6_hdr *ip6;
516 struct secasindex *saidx;
517 int nxt;
518 u_int8_t prot;
519 int error, nest;
520
521 if (__predict_false(m == NULL)) {
522 panic("%s: NULL mbuf", __func__);
523 }
524
525 KASSERT(sav != NULL);
526 saidx = &sav->sah->saidx;
527 af = saidx->dst.sa.sa_family;
528 KASSERTMSG(af == AF_INET6, "unexpected af %u", af);
529 sproto = saidx->proto;
530 KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
531 sproto == IPPROTO_IPCOMP,
532 "unexpected security protocol %u", sproto);
533
534 /* Fix IPv6 header */
535 if (m->m_len < sizeof(struct ip6_hdr) &&
536 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
537 char buf[IPSEC_ADDRSTRLEN];
538 IPSECLOG(LOG_DEBUG, "processing failed for SA %s/%08lx\n",
539 ipsec_address(&sav->sah->saidx.dst,
540 buf, sizeof(buf)), (u_long) ntohl(sav->spi));
541 IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
542 IPCOMP_STAT_HDROPS);
543 error = EACCES;
544 goto bad;
545 }
546
547 ip6 = mtod(m, struct ip6_hdr *);
548 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
549
550 m_copydata(m, protoff, sizeof(prot), &prot);
551
552 key_sa_recordxfer(sav, m);
553
554 /*
555 * See the end of ip6_input for this logic.
556 * IPPROTO_IPV[46] case will be processed just like other ones
557 */
558 nest = 0;
559 nxt = prot;
560 while (nxt != IPPROTO_DONE) {
561 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
562 IP6_STATINC(IP6_STAT_TOOMANYHDR);
563 error = EINVAL;
564 goto bad;
565 }
566
567 M_VERIFY_PACKET(m);
568
569 /*
570 * Protection against faulty packet - there should be
571 * more sanity checks in header chain processing.
572 */
573 if (m->m_pkthdr.len < skip) {
574 IP6_STATINC(IP6_STAT_TOOSHORT);
575 in6_ifstat_inc(m_get_rcvif_NOMPSAFE(m),
576 ifs6_in_truncated);
577 error = EINVAL;
578 goto bad;
579 }
580
581 /*
582 * Enforce IPsec policy checking if we are seeing last header.
583 * Note that we do not visit this with protocols with pcb layer
584 * code - like udp/tcp/raw ip.
585 */
586 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
587 ipsec_in_reject(m, NULL)) {
588 error = EINVAL;
589 goto bad;
590 }
591
592 /*
593 * There is no struct ifnet for tunnel mode IP-IP tunnel connecttion,
594 * so we cannot write filtering rule to the inner packet.
595 */
596 if (saidx->mode == IPSEC_MODE_TUNNEL)
597 m->m_pkthdr.pkthdr_flags |= PKTHDR_FLAG_IPSEC_SKIP_PFIL;
598
599 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
600 }
601 return 0;
602
603 bad:
604 if (m)
605 m_freem(m);
606 return error;
607 }
608 #endif /* INET6 */
609