1 /* $OpenBSD: ipsec_input.c,v 1.179 2021/07/27 17:13:03 mvs 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 } 170 171 /* 172 * ipsec_common_input() gets called when we receive an IPsec-protected packet 173 * in IPv4 or IPv6. All it does is find the right TDB and call the appropriate 174 * transform. The callback takes care of further processing (like ingress 175 * filtering). 176 */ 177 int 178 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto, 179 int udpencap) 180 { 181 #define IPSEC_ISTAT(x,y,z) do { \ 182 if (sproto == IPPROTO_ESP) \ 183 espstat_inc(x); \ 184 else if (sproto == IPPROTO_AH) \ 185 ahstat_inc(y); \ 186 else \ 187 ipcompstat_inc(z); \ 188 } while (0) 189 190 union sockaddr_union dst_address; 191 struct tdb *tdbp = NULL; 192 struct ifnet *encif; 193 u_int32_t spi; 194 u_int16_t cpi; 195 int error; 196 #ifdef ENCDEBUG 197 char buf[INET6_ADDRSTRLEN]; 198 #endif 199 200 NET_ASSERT_LOCKED(); 201 202 ipsecstat_inc(ipsec_ipackets); 203 ipsecstat_add(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_common_ctlinput(u_int rdomain, int cmd, struct sockaddr *sa, 953 void *v, int proto) 954 { 955 struct ip *ip = v; 956 957 if (cmd == PRC_MSGSIZE && ip && ip_mtudisc && ip->ip_v == 4) { 958 struct tdb *tdbp; 959 struct sockaddr_in dst; 960 struct icmp *icp; 961 int hlen = ip->ip_hl << 2; 962 u_int32_t spi, mtu; 963 ssize_t adjust; 964 965 /* Find the right MTU. */ 966 icp = (struct icmp *)((caddr_t) ip - 967 offsetof(struct icmp, icmp_ip)); 968 mtu = ntohs(icp->icmp_nextmtu); 969 970 /* 971 * Ignore the packet, if we do not receive a MTU 972 * or the MTU is too small to be acceptable. 973 */ 974 if (mtu < 296) 975 return; 976 977 memset(&dst, 0, sizeof(struct sockaddr_in)); 978 dst.sin_family = AF_INET; 979 dst.sin_len = sizeof(struct sockaddr_in); 980 dst.sin_addr.s_addr = ip->ip_dst.s_addr; 981 982 memcpy(&spi, (caddr_t)ip + hlen, sizeof(u_int32_t)); 983 984 tdbp = gettdb_rev(rdomain, spi, (union sockaddr_union *)&dst, 985 proto); 986 if (tdbp == NULL || tdbp->tdb_flags & TDBF_INVALID) 987 return; 988 989 /* Walk the chain backwards to the first tdb */ 990 NET_ASSERT_LOCKED(); 991 for (; tdbp; tdbp = tdbp->tdb_inext) { 992 if (tdbp->tdb_flags & TDBF_INVALID || 993 (adjust = ipsec_hdrsz(tdbp)) == -1) 994 return; 995 996 mtu -= adjust; 997 998 /* Store adjusted MTU in tdb */ 999 tdbp->tdb_mtu = mtu; 1000 tdbp->tdb_mtutimeout = gettime() + 1001 ip_mtudisc_timeout; 1002 DPRINTF("spi %08x mtu %d adjust %ld", 1003 ntohl(tdbp->tdb_spi), tdbp->tdb_mtu, adjust); 1004 } 1005 } 1006 } 1007 1008 void 1009 udpencap_ctlinput(int cmd, struct sockaddr *sa, u_int rdomain, void *v) 1010 { 1011 struct ip *ip = v; 1012 struct tdb *tdbp; 1013 struct icmp *icp; 1014 u_int32_t mtu; 1015 ssize_t adjust; 1016 struct sockaddr_in dst, src; 1017 union sockaddr_union *su_dst, *su_src; 1018 1019 NET_ASSERT_LOCKED(); 1020 1021 icp = (struct icmp *)((caddr_t) ip - offsetof(struct icmp, icmp_ip)); 1022 mtu = ntohs(icp->icmp_nextmtu); 1023 1024 /* 1025 * Ignore the packet, if we do not receive a MTU 1026 * or the MTU is too small to be acceptable. 1027 */ 1028 if (mtu < 296) 1029 return; 1030 1031 memset(&dst, 0, sizeof(dst)); 1032 dst.sin_family = AF_INET; 1033 dst.sin_len = sizeof(struct sockaddr_in); 1034 dst.sin_addr.s_addr = ip->ip_dst.s_addr; 1035 su_dst = (union sockaddr_union *)&dst; 1036 memset(&src, 0, sizeof(src)); 1037 src.sin_family = AF_INET; 1038 src.sin_len = sizeof(struct sockaddr_in); 1039 src.sin_addr.s_addr = ip->ip_src.s_addr; 1040 su_src = (union sockaddr_union *)&src; 1041 1042 tdbp = gettdbbysrcdst_rev(rdomain, 0, su_src, su_dst, 1043 IPPROTO_ESP); 1044 1045 for (; tdbp != NULL; tdbp = tdbp->tdb_snext) { 1046 if (tdbp->tdb_sproto == IPPROTO_ESP && 1047 ((tdbp->tdb_flags & (TDBF_INVALID|TDBF_UDPENCAP)) == 1048 TDBF_UDPENCAP) && 1049 !memcmp(&tdbp->tdb_dst, &dst, su_dst->sa.sa_len) && 1050 !memcmp(&tdbp->tdb_src, &src, su_src->sa.sa_len)) { 1051 if ((adjust = ipsec_hdrsz(tdbp)) != -1) { 1052 /* Store adjusted MTU in tdb */ 1053 tdbp->tdb_mtu = mtu - adjust; 1054 tdbp->tdb_mtutimeout = gettime() + 1055 ip_mtudisc_timeout; 1056 DPRINTF("spi %08x mtu %d adjust %ld", 1057 ntohl(tdbp->tdb_spi), tdbp->tdb_mtu, 1058 adjust); 1059 } 1060 } 1061 } 1062 } 1063 1064 void 1065 esp4_ctlinput(int cmd, struct sockaddr *sa, u_int rdomain, void *v) 1066 { 1067 if (sa->sa_family != AF_INET || 1068 sa->sa_len != sizeof(struct sockaddr_in)) 1069 return; 1070 1071 ipsec_common_ctlinput(rdomain, cmd, sa, v, IPPROTO_ESP); 1072 } 1073 1074 #ifdef INET6 1075 /* IPv6 AH wrapper. */ 1076 int 1077 ah6_input(struct mbuf **mp, int *offp, int proto, int af) 1078 { 1079 int l = 0; 1080 int protoff, nxt; 1081 struct ip6_ext ip6e; 1082 1083 if ( 1084 #if NPF > 0 1085 ((*mp)->m_pkthdr.pf.flags & PF_TAG_DIVERTED) || 1086 #endif 1087 !ah_enable) 1088 return rip6_input(mp, offp, proto, af); 1089 1090 if (*offp < sizeof(struct ip6_hdr)) { 1091 DPRINTF("bad offset"); 1092 ahstat_inc(ahs_hdrops); 1093 m_freemp(mp); 1094 return IPPROTO_DONE; 1095 } else if (*offp == sizeof(struct ip6_hdr)) { 1096 protoff = offsetof(struct ip6_hdr, ip6_nxt); 1097 } else { 1098 /* Chase down the header chain... */ 1099 protoff = sizeof(struct ip6_hdr); 1100 nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt; 1101 1102 do { 1103 protoff += l; 1104 m_copydata(*mp, protoff, sizeof(ip6e), 1105 (caddr_t) &ip6e); 1106 1107 if (nxt == IPPROTO_AH) 1108 l = (ip6e.ip6e_len + 2) << 2; 1109 else 1110 l = (ip6e.ip6e_len + 1) << 3; 1111 #ifdef DIAGNOSTIC 1112 if (l <= 0) 1113 panic("ah6_input: l went zero or negative"); 1114 #endif 1115 1116 nxt = ip6e.ip6e_nxt; 1117 } while (protoff + l < *offp); 1118 1119 /* Malformed packet check */ 1120 if (protoff + l != *offp) { 1121 DPRINTF("bad packet header chain"); 1122 ahstat_inc(ahs_hdrops); 1123 m_freemp(mp); 1124 return IPPROTO_DONE; 1125 } 1126 protoff += offsetof(struct ip6_ext, ip6e_nxt); 1127 } 1128 ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto, 0); 1129 return IPPROTO_DONE; 1130 } 1131 1132 /* IPv6 ESP wrapper. */ 1133 int 1134 esp6_input(struct mbuf **mp, int *offp, int proto, int af) 1135 { 1136 int l = 0; 1137 int protoff, nxt; 1138 struct ip6_ext ip6e; 1139 1140 if ( 1141 #if NPF > 0 1142 ((*mp)->m_pkthdr.pf.flags & PF_TAG_DIVERTED) || 1143 #endif 1144 !esp_enable) 1145 return rip6_input(mp, offp, proto, af); 1146 1147 if (*offp < sizeof(struct ip6_hdr)) { 1148 DPRINTF("bad offset"); 1149 espstat_inc(esps_hdrops); 1150 m_freemp(mp); 1151 return IPPROTO_DONE; 1152 } else if (*offp == sizeof(struct ip6_hdr)) { 1153 protoff = offsetof(struct ip6_hdr, ip6_nxt); 1154 } else { 1155 /* Chase down the header chain... */ 1156 protoff = sizeof(struct ip6_hdr); 1157 nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt; 1158 1159 do { 1160 protoff += l; 1161 m_copydata(*mp, protoff, sizeof(ip6e), 1162 (caddr_t) &ip6e); 1163 1164 if (nxt == IPPROTO_AH) 1165 l = (ip6e.ip6e_len + 2) << 2; 1166 else 1167 l = (ip6e.ip6e_len + 1) << 3; 1168 #ifdef DIAGNOSTIC 1169 if (l <= 0) 1170 panic("esp6_input: l went zero or negative"); 1171 #endif 1172 1173 nxt = ip6e.ip6e_nxt; 1174 } while (protoff + l < *offp); 1175 1176 /* Malformed packet check */ 1177 if (protoff + l != *offp) { 1178 DPRINTF("bad packet header chain"); 1179 espstat_inc(esps_hdrops); 1180 m_freemp(mp); 1181 return IPPROTO_DONE; 1182 } 1183 protoff += offsetof(struct ip6_ext, ip6e_nxt); 1184 } 1185 ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto, 0); 1186 return IPPROTO_DONE; 1187 1188 } 1189 1190 /* IPv6 IPcomp wrapper */ 1191 int 1192 ipcomp6_input(struct mbuf **mp, int *offp, int proto, int af) 1193 { 1194 int l = 0; 1195 int protoff, nxt; 1196 struct ip6_ext ip6e; 1197 1198 if ( 1199 #if NPF > 0 1200 ((*mp)->m_pkthdr.pf.flags & PF_TAG_DIVERTED) || 1201 #endif 1202 !ipcomp_enable) 1203 return rip6_input(mp, offp, proto, af); 1204 1205 if (*offp < sizeof(struct ip6_hdr)) { 1206 DPRINTF("bad offset"); 1207 ipcompstat_inc(ipcomps_hdrops); 1208 m_freemp(mp); 1209 return IPPROTO_DONE; 1210 } else if (*offp == sizeof(struct ip6_hdr)) { 1211 protoff = offsetof(struct ip6_hdr, ip6_nxt); 1212 } else { 1213 /* Chase down the header chain... */ 1214 protoff = sizeof(struct ip6_hdr); 1215 nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt; 1216 1217 do { 1218 protoff += l; 1219 m_copydata(*mp, protoff, sizeof(ip6e), 1220 (caddr_t) &ip6e); 1221 if (nxt == IPPROTO_AH) 1222 l = (ip6e.ip6e_len + 2) << 2; 1223 else 1224 l = (ip6e.ip6e_len + 1) << 3; 1225 #ifdef DIAGNOSTIC 1226 if (l <= 0) 1227 panic("l went zero or negative"); 1228 #endif 1229 1230 nxt = ip6e.ip6e_nxt; 1231 } while (protoff + l < *offp); 1232 1233 /* Malformed packet check */ 1234 if (protoff + l != *offp) { 1235 DPRINTF("bad packet header chain"); 1236 ipcompstat_inc(ipcomps_hdrops); 1237 m_freemp(mp); 1238 return IPPROTO_DONE; 1239 } 1240 1241 protoff += offsetof(struct ip6_ext, ip6e_nxt); 1242 } 1243 ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto, 0); 1244 return IPPROTO_DONE; 1245 } 1246 #endif /* INET6 */ 1247 1248 int 1249 ipsec_forward_check(struct mbuf *m, int hlen, int af) 1250 { 1251 struct tdb *tdb; 1252 struct tdb_ident *tdbi; 1253 struct m_tag *mtag; 1254 int error = 0; 1255 1256 /* 1257 * IPsec policy check for forwarded packets. Look at 1258 * inner-most IPsec SA used. 1259 */ 1260 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 1261 if (mtag != NULL) { 1262 tdbi = (struct tdb_ident *)(mtag + 1); 1263 tdb = gettdb(tdbi->rdomain, tdbi->spi, &tdbi->dst, tdbi->proto); 1264 } else 1265 tdb = NULL; 1266 ipsp_spd_lookup(m, af, hlen, &error, IPSP_DIRECTION_IN, tdb, NULL, 0); 1267 1268 return error; 1269 } 1270 1271 int 1272 ipsec_local_check(struct mbuf *m, int hlen, int proto, int af) 1273 { 1274 struct tdb *tdb; 1275 struct tdb_ident *tdbi; 1276 struct m_tag *mtag; 1277 int error = 0; 1278 1279 /* 1280 * If it's a protected packet for us, skip the policy check. 1281 * That's because we really only care about the properties of 1282 * the protected packet, and not the intermediate versions. 1283 * While this is not the most paranoid setting, it allows 1284 * some flexibility in handling nested tunnels (in setting up 1285 * the policies). 1286 */ 1287 if ((proto == IPPROTO_ESP) || (proto == IPPROTO_AH) || 1288 (proto == IPPROTO_IPCOMP)) 1289 return 0; 1290 1291 /* 1292 * If the protected packet was tunneled, then we need to 1293 * verify the protected packet's information, not the 1294 * external headers. Thus, skip the policy lookup for the 1295 * external packet, and keep the IPsec information linked on 1296 * the packet header (the encapsulation routines know how 1297 * to deal with that). 1298 */ 1299 if ((proto == IPPROTO_IPV4) || (proto == IPPROTO_IPV6)) 1300 return 0; 1301 1302 /* 1303 * When processing IPv6 header chains, do not look at the 1304 * outer header. The inner protocol is relevant and will 1305 * be checked by the local delivery loop later. 1306 */ 1307 if ((af == AF_INET6) && ((proto == IPPROTO_DSTOPTS) || 1308 (proto == IPPROTO_ROUTING) || (proto == IPPROTO_FRAGMENT))) 1309 return 0; 1310 1311 /* 1312 * If the protected packet is TCP or UDP, we'll do the 1313 * policy check in the respective input routine, so we can 1314 * check for bypass sockets. 1315 */ 1316 if ((proto == IPPROTO_TCP) || (proto == IPPROTO_UDP)) 1317 return 0; 1318 1319 /* 1320 * IPsec policy check for local-delivery packets. Look at the 1321 * inner-most SA that protected the packet. This is in fact 1322 * a bit too restrictive (it could end up causing packets to 1323 * be dropped that semantically follow the policy, e.g., in 1324 * certain SA-bundle configurations); but the alternative is 1325 * very complicated (and requires keeping track of what 1326 * kinds of tunneling headers have been seen in-between the 1327 * IPsec headers), and I don't think we lose much functionality 1328 * that's needed in the real world (who uses bundles anyway ?). 1329 */ 1330 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 1331 if (mtag) { 1332 tdbi = (struct tdb_ident *)(mtag + 1); 1333 tdb = gettdb(tdbi->rdomain, tdbi->spi, &tdbi->dst, 1334 tdbi->proto); 1335 } else 1336 tdb = NULL; 1337 ipsp_spd_lookup(m, af, hlen, &error, IPSP_DIRECTION_IN, 1338 tdb, NULL, 0); 1339 1340 return error; 1341 } 1342