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