1 /* $OpenBSD: ipsec_input.c,v 1.103 2011/04/26 22:30:38 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/proc.h> 46 #include <sys/sysctl.h> 47 #include <sys/kernel.h> 48 49 #include <net/if.h> 50 #include <net/netisr.h> 51 #include <net/bpf.h> 52 #include <net/route.h> 53 54 #if NPF > 0 55 #include <net/pfvar.h> 56 #endif 57 58 #include <netinet/in.h> 59 #include <netinet/in_systm.h> 60 #include <netinet/ip.h> 61 #include <netinet/ip_var.h> 62 #include <netinet/in_var.h> 63 #include <netinet/ip_icmp.h> 64 #include <netinet/tcp.h> 65 #include <netinet/udp.h> 66 67 #ifdef INET6 68 #ifndef INET 69 #include <netinet/in.h> 70 #endif 71 #include <netinet/ip6.h> 72 #include <netinet6/ip6_var.h> 73 #include <netinet6/ip6protosw.h> 74 #endif /* INET6 */ 75 76 #include <netinet/ip_ipsp.h> 77 #include <netinet/ip_esp.h> 78 #include <netinet/ip_ah.h> 79 #include <netinet/ip_ipcomp.h> 80 81 #include <net/if_enc.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(x) if (encdebug) printf x 89 #else 90 #define DPRINTF(x) 91 #endif 92 93 /* sysctl variables */ 94 int esp_enable = 1; 95 int ah_enable = 1; 96 int ipcomp_enable = 0; 97 98 int *espctl_vars[ESPCTL_MAXID] = ESPCTL_VARS; 99 int *ahctl_vars[AHCTL_MAXID] = AHCTL_VARS; 100 int *ipcompctl_vars[IPCOMPCTL_MAXID] = IPCOMPCTL_VARS; 101 102 #ifdef INET6 103 extern struct ip6protosw inet6sw[]; 104 extern u_char ip6_protox[]; 105 #endif 106 107 /* 108 * ipsec_common_input() gets called when we receive an IPsec-protected packet 109 * in IPv4 or IPv6. All it does is find the right TDB and call the appropriate 110 * transform. The callback takes care of further processing (like ingress 111 * filtering). 112 */ 113 int 114 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto, 115 int udpencap) 116 { 117 #define IPSEC_ISTAT(x,y,z) (sproto == IPPROTO_ESP ? (x)++ : \ 118 sproto == IPPROTO_AH ? (y)++ : (z)++) 119 120 union sockaddr_union dst_address; 121 struct timeval tv; 122 struct tdb *tdbp; 123 struct ifnet *encif; 124 u_int32_t spi; 125 u_int16_t cpi; 126 int s, error; 127 128 IPSEC_ISTAT(espstat.esps_input, ahstat.ahs_input, 129 ipcompstat.ipcomps_input); 130 131 if (m == 0) { 132 DPRINTF(("ipsec_common_input(): NULL packet received\n")); 133 IPSEC_ISTAT(espstat.esps_hdrops, ahstat.ahs_hdrops, 134 ipcompstat.ipcomps_hdrops); 135 return EINVAL; 136 } 137 138 if ((sproto == IPPROTO_ESP && !esp_enable) || 139 (sproto == IPPROTO_AH && !ah_enable) || 140 (sproto == IPPROTO_IPCOMP && !ipcomp_enable)) { 141 switch (af) { 142 #ifdef INET 143 case AF_INET: 144 rip_input(m, skip, sproto); 145 break; 146 #endif /* INET */ 147 #ifdef INET6 148 case AF_INET6: 149 rip6_input(&m, &skip, sproto); 150 break; 151 #endif /* INET6 */ 152 default: 153 DPRINTF(("ipsec_common_input(): unsupported protocol " 154 "family %d\n", af)); 155 m_freem(m); 156 IPSEC_ISTAT(espstat.esps_nopf, ahstat.ahs_nopf, 157 ipcompstat.ipcomps_nopf); 158 return EPFNOSUPPORT; 159 } 160 return 0; 161 } 162 if ((sproto == IPPROTO_IPCOMP) && (m->m_flags & M_COMP)) { 163 m_freem(m); 164 ipcompstat.ipcomps_pdrops++; 165 DPRINTF(("ipsec_common_input(): repeated decompression\n")); 166 return EINVAL; 167 } 168 169 if (m->m_pkthdr.len - skip < 2 * sizeof(u_int32_t)) { 170 m_freem(m); 171 IPSEC_ISTAT(espstat.esps_hdrops, ahstat.ahs_hdrops, 172 ipcompstat.ipcomps_hdrops); 173 DPRINTF(("ipsec_common_input(): packet too small\n")); 174 return EINVAL; 175 } 176 177 /* Retrieve the SPI from the relevant IPsec header */ 178 if (sproto == IPPROTO_ESP) 179 m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi); 180 else if (sproto == IPPROTO_AH) 181 m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t), 182 (caddr_t) &spi); 183 else if (sproto == IPPROTO_IPCOMP) { 184 m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t), 185 (caddr_t) &cpi); 186 spi = ntohl(htons(cpi)); 187 } 188 189 /* 190 * Find tunnel control block and (indirectly) call the appropriate 191 * kernel crypto routine. The resulting mbuf chain is a valid 192 * IP packet ready to go through input processing. 193 */ 194 195 bzero(&dst_address, sizeof(dst_address)); 196 dst_address.sa.sa_family = af; 197 198 switch (af) { 199 #ifdef INET 200 case AF_INET: 201 dst_address.sin.sin_len = sizeof(struct sockaddr_in); 202 m_copydata(m, offsetof(struct ip, ip_dst), 203 sizeof(struct in_addr), 204 (caddr_t) &(dst_address.sin.sin_addr)); 205 break; 206 #endif /* INET */ 207 208 #ifdef INET6 209 case AF_INET6: 210 dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6); 211 m_copydata(m, offsetof(struct ip6_hdr, ip6_dst), 212 sizeof(struct in6_addr), 213 (caddr_t) &(dst_address.sin6.sin6_addr)); 214 in6_recoverscope(&dst_address.sin6, &dst_address.sin6.sin6_addr, 215 NULL); 216 break; 217 #endif /* INET6 */ 218 219 default: 220 DPRINTF(("ipsec_common_input(): unsupported protocol " 221 "family %d\n", af)); 222 m_freem(m); 223 IPSEC_ISTAT(espstat.esps_nopf, ahstat.ahs_nopf, 224 ipcompstat.ipcomps_nopf); 225 return EPFNOSUPPORT; 226 } 227 228 s = spltdb(); 229 tdbp = gettdb(rtable_l2(m->m_pkthdr.rdomain), 230 spi, &dst_address, sproto); 231 if (tdbp == NULL) { 232 splx(s); 233 DPRINTF(("ipsec_common_input(): could not find SA for " 234 "packet to %s, spi %08x\n", 235 ipsp_address(dst_address), ntohl(spi))); 236 m_freem(m); 237 IPSEC_ISTAT(espstat.esps_notdb, ahstat.ahs_notdb, 238 ipcompstat.ipcomps_notdb); 239 return ENOENT; 240 } 241 242 if (tdbp->tdb_flags & TDBF_INVALID) { 243 splx(s); 244 DPRINTF(("ipsec_common_input(): attempted to use invalid SA %s/%08x/%u\n", ipsp_address(dst_address), ntohl(spi), tdbp->tdb_sproto)); 245 m_freem(m); 246 IPSEC_ISTAT(espstat.esps_invalid, ahstat.ahs_invalid, 247 ipcompstat.ipcomps_invalid); 248 return EINVAL; 249 } 250 251 if (udpencap && !(tdbp->tdb_flags & TDBF_UDPENCAP)) { 252 splx(s); 253 DPRINTF(("ipsec_common_input(): attempted to use non-udpencap SA %s/%08x/%u\n", ipsp_address(dst_address), ntohl(spi), tdbp->tdb_sproto)); 254 m_freem(m); 255 espstat.esps_udpinval++; 256 return EINVAL; 257 } 258 259 if (tdbp->tdb_xform == NULL) { 260 splx(s); 261 DPRINTF(("ipsec_common_input(): attempted to use uninitialized SA %s/%08x/%u\n", ipsp_address(dst_address), ntohl(spi), tdbp->tdb_sproto)); 262 m_freem(m); 263 IPSEC_ISTAT(espstat.esps_noxform, ahstat.ahs_noxform, 264 ipcompstat.ipcomps_noxform); 265 return ENXIO; 266 } 267 268 if (sproto != IPPROTO_IPCOMP) { 269 if ((encif = enc_getif(tdbp->tdb_rdomain, 270 tdbp->tdb_tap)) == NULL) { 271 splx(s); 272 DPRINTF(("ipsec_common_input(): " 273 "no enc%u interface for SA %s/%08x/%u\n", 274 tdbp->tdb_tap, ipsp_address(dst_address), 275 ntohl(spi), tdbp->tdb_sproto)); 276 m_freem(m); 277 278 IPSEC_ISTAT(espstat.esps_pdrops, 279 ahstat.ahs_pdrops, 280 ipcompstat.ipcomps_pdrops); 281 return EACCES; 282 } 283 284 /* XXX This conflicts with the scoped nature of IPv6 */ 285 m->m_pkthdr.rcvif = encif; 286 } 287 288 /* Register first use, setup expiration timer. */ 289 if (tdbp->tdb_first_use == 0) { 290 tdbp->tdb_first_use = time_second; 291 292 tv.tv_usec = 0; 293 294 tv.tv_sec = tdbp->tdb_exp_first_use + tdbp->tdb_first_use; 295 if (tdbp->tdb_flags & TDBF_FIRSTUSE) 296 timeout_add(&tdbp->tdb_first_tmo, hzto(&tv)); 297 298 tv.tv_sec = tdbp->tdb_first_use + tdbp->tdb_soft_first_use; 299 if (tdbp->tdb_flags & TDBF_SOFT_FIRSTUSE) 300 timeout_add(&tdbp->tdb_sfirst_tmo, hzto(&tv)); 301 } 302 303 /* 304 * Call appropriate transform and return -- callback takes care of 305 * everything else. 306 */ 307 error = (*(tdbp->tdb_xform->xf_input))(m, tdbp, skip, protoff); 308 splx(s); 309 return error; 310 } 311 312 /* 313 * IPsec input callback, called by the transform callback. Takes care of 314 * filtering and other sanity checks on the processed packet. 315 */ 316 int 317 ipsec_common_input_cb(struct mbuf *m, struct tdb *tdbp, int skip, int protoff, 318 struct m_tag *mt) 319 { 320 int af, sproto; 321 u_char prot; 322 323 #if NBPFILTER > 0 324 struct ifnet *encif; 325 #endif 326 327 #ifdef INET 328 struct ip *ip, ipn; 329 #endif /* INET */ 330 331 #ifdef INET6 332 struct ip6_hdr *ip6, ip6n; 333 #endif /* INET6 */ 334 struct m_tag *mtag; 335 struct tdb_ident *tdbi; 336 337 af = tdbp->tdb_dst.sa.sa_family; 338 sproto = tdbp->tdb_sproto; 339 340 tdbp->tdb_last_used = time_second; 341 342 /* Sanity check */ 343 if (m == NULL) { 344 /* The called routine will print a message if necessary */ 345 IPSEC_ISTAT(espstat.esps_badkcr, ahstat.ahs_badkcr, 346 ipcompstat.ipcomps_badkcr); 347 return EINVAL; 348 } 349 350 #ifdef INET 351 /* Fix IPv4 header */ 352 if (af == AF_INET) { 353 if ((m->m_len < skip) && ((m = m_pullup(m, skip)) == NULL)) { 354 DPRINTF(("ipsec_common_input_cb(): processing failed " 355 "for SA %s/%08x\n", ipsp_address(tdbp->tdb_dst), 356 ntohl(tdbp->tdb_spi))); 357 IPSEC_ISTAT(espstat.esps_hdrops, ahstat.ahs_hdrops, 358 ipcompstat.ipcomps_hdrops); 359 return ENOBUFS; 360 } 361 362 ip = mtod(m, struct ip *); 363 ip->ip_len = htons(m->m_pkthdr.len); 364 ip->ip_sum = 0; 365 ip->ip_sum = in_cksum(m, ip->ip_hl << 2); 366 prot = ip->ip_p; 367 368 /* IP-in-IP encapsulation */ 369 if (prot == IPPROTO_IPIP) { 370 if (m->m_pkthdr.len - skip < sizeof(struct ip)) { 371 m_freem(m); 372 IPSEC_ISTAT(espstat.esps_hdrops, 373 ahstat.ahs_hdrops, 374 ipcompstat.ipcomps_hdrops); 375 return EINVAL; 376 } 377 /* ipn will now contain the inner IPv4 header */ 378 m_copydata(m, skip, sizeof(struct ip), 379 (caddr_t) &ipn); 380 381 /* 382 * Check that the inner source address is the same as 383 * the proxy address, if available. 384 */ 385 if ((tdbp->tdb_proxy.sa.sa_family == AF_INET && 386 tdbp->tdb_proxy.sin.sin_addr.s_addr != 387 INADDR_ANY && 388 ipn.ip_src.s_addr != 389 tdbp->tdb_proxy.sin.sin_addr.s_addr) || 390 (tdbp->tdb_proxy.sa.sa_family != AF_INET && 391 tdbp->tdb_proxy.sa.sa_family != 0)) { 392 393 DPRINTF(("ipsec_common_input_cb(): inner " 394 "source address %s doesn't correspond to " 395 "expected proxy source %s, SA %s/%08x\n", 396 inet_ntoa4(ipn.ip_src), 397 ipsp_address(tdbp->tdb_proxy), 398 ipsp_address(tdbp->tdb_dst), 399 ntohl(tdbp->tdb_spi))); 400 401 m_freem(m); 402 IPSEC_ISTAT(espstat.esps_pdrops, 403 ahstat.ahs_pdrops, 404 ipcompstat.ipcomps_pdrops); 405 return EACCES; 406 } 407 } 408 409 #ifdef INET6 410 /* IPv6-in-IP encapsulation. */ 411 if (prot == IPPROTO_IPV6) { 412 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) { 413 m_freem(m); 414 IPSEC_ISTAT(espstat.esps_hdrops, 415 ahstat.ahs_hdrops, 416 ipcompstat.ipcomps_hdrops); 417 return EINVAL; 418 } 419 /* ip6n will now contain the inner IPv6 header. */ 420 m_copydata(m, skip, sizeof(struct ip6_hdr), 421 (caddr_t) &ip6n); 422 423 /* 424 * Check that the inner source address is the same as 425 * the proxy address, if available. 426 */ 427 if ((tdbp->tdb_proxy.sa.sa_family == AF_INET6 && 428 !IN6_IS_ADDR_UNSPECIFIED(&tdbp->tdb_proxy.sin6.sin6_addr) && 429 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src, 430 &tdbp->tdb_proxy.sin6.sin6_addr)) || 431 (tdbp->tdb_proxy.sa.sa_family != AF_INET6 && 432 tdbp->tdb_proxy.sa.sa_family != 0)) { 433 434 DPRINTF(("ipsec_common_input_cb(): inner " 435 "source address %s doesn't correspond to " 436 "expected proxy source %s, SA %s/%08x\n", 437 ip6_sprintf(&ip6n.ip6_src), 438 ipsp_address(tdbp->tdb_proxy), 439 ipsp_address(tdbp->tdb_dst), 440 ntohl(tdbp->tdb_spi))); 441 442 m_freem(m); 443 IPSEC_ISTAT(espstat.esps_pdrops, 444 ahstat.ahs_pdrops, 445 ipcompstat.ipcomps_pdrops); 446 return EACCES; 447 } 448 } 449 #endif /* INET6 */ 450 } 451 #endif /* INET */ 452 453 #ifdef INET6 454 /* Fix IPv6 header */ 455 if (af == AF_INET6) 456 { 457 if (m->m_len < sizeof(struct ip6_hdr) && 458 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 459 460 DPRINTF(("ipsec_common_input_cb(): processing failed " 461 "for SA %s/%08x\n", ipsp_address(tdbp->tdb_dst), 462 ntohl(tdbp->tdb_spi))); 463 464 IPSEC_ISTAT(espstat.esps_hdrops, ahstat.ahs_hdrops, 465 ipcompstat.ipcomps_hdrops); 466 return EACCES; 467 } 468 469 ip6 = mtod(m, struct ip6_hdr *); 470 ip6->ip6_plen = htons(m->m_pkthdr.len - skip); 471 472 /* Save protocol */ 473 m_copydata(m, protoff, 1, (caddr_t) &prot); 474 475 #ifdef INET 476 /* IP-in-IP encapsulation */ 477 if (prot == IPPROTO_IPIP) { 478 if (m->m_pkthdr.len - skip < sizeof(struct ip)) { 479 m_freem(m); 480 IPSEC_ISTAT(espstat.esps_hdrops, 481 ahstat.ahs_hdrops, 482 ipcompstat.ipcomps_hdrops); 483 return EINVAL; 484 } 485 /* ipn will now contain the inner IPv4 header */ 486 m_copydata(m, skip, sizeof(struct ip), (caddr_t) &ipn); 487 488 /* 489 * Check that the inner source address is the same as 490 * the proxy address, if available. 491 */ 492 if ((tdbp->tdb_proxy.sa.sa_family == AF_INET && 493 tdbp->tdb_proxy.sin.sin_addr.s_addr != 494 INADDR_ANY && 495 ipn.ip_src.s_addr != 496 tdbp->tdb_proxy.sin.sin_addr.s_addr) || 497 (tdbp->tdb_proxy.sa.sa_family != AF_INET && 498 tdbp->tdb_proxy.sa.sa_family != 0)) { 499 500 DPRINTF(("ipsec_common_input_cb(): inner " 501 "source address %s doesn't correspond to " 502 "expected proxy source %s, SA %s/%08x\n", 503 inet_ntoa4(ipn.ip_src), 504 ipsp_address(tdbp->tdb_proxy), 505 ipsp_address(tdbp->tdb_dst), 506 ntohl(tdbp->tdb_spi))); 507 508 m_freem(m); 509 IPSEC_ISTAT(espstat.esps_pdrops, 510 ahstat.ahs_pdrops, 511 ipcompstat.ipcomps_pdrops); 512 return EACCES; 513 } 514 } 515 #endif /* INET */ 516 517 /* IPv6-in-IP encapsulation */ 518 if (prot == IPPROTO_IPV6) { 519 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) { 520 m_freem(m); 521 IPSEC_ISTAT(espstat.esps_hdrops, 522 ahstat.ahs_hdrops, 523 ipcompstat.ipcomps_hdrops); 524 return EINVAL; 525 } 526 /* ip6n will now contain the inner IPv6 header. */ 527 m_copydata(m, skip, sizeof(struct ip6_hdr), 528 (caddr_t) &ip6n); 529 530 /* 531 * Check that the inner source address is the same as 532 * the proxy address, if available. 533 */ 534 if ((tdbp->tdb_proxy.sa.sa_family == AF_INET6 && 535 !IN6_IS_ADDR_UNSPECIFIED(&tdbp->tdb_proxy.sin6.sin6_addr) && 536 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src, 537 &tdbp->tdb_proxy.sin6.sin6_addr)) || 538 (tdbp->tdb_proxy.sa.sa_family != AF_INET6 && 539 tdbp->tdb_proxy.sa.sa_family != 0)) { 540 541 DPRINTF(("ipsec_common_input_cb(): inner " 542 "source address %s doesn't correspond to " 543 "expected proxy source %s, SA %s/%08x\n", 544 ip6_sprintf(&ip6n.ip6_src), 545 ipsp_address(tdbp->tdb_proxy), 546 ipsp_address(tdbp->tdb_dst), 547 ntohl(tdbp->tdb_spi))); 548 549 m_freem(m); 550 IPSEC_ISTAT(espstat.esps_pdrops, 551 ahstat.ahs_pdrops, 552 ipcompstat.ipcomps_pdrops); 553 return EACCES; 554 } 555 } 556 } 557 #endif /* INET6 */ 558 559 /* 560 * Record what we've done to the packet (under what SA it was 561 * processed). If we've been passed an mtag, it means the packet 562 * was already processed by an ethernet/crypto combo card and 563 * thus has a tag attached with all the right information, but 564 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to 565 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type. 566 */ 567 if (tdbp->tdb_sproto != IPPROTO_IPCOMP) { 568 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE, 569 sizeof(struct tdb_ident), M_NOWAIT); 570 if (mtag == NULL) { 571 m_freem(m); 572 DPRINTF(("ipsec_common_input_cb(): failed to " 573 "get tag\n")); 574 IPSEC_ISTAT(espstat.esps_hdrops, ahstat.ahs_hdrops, 575 ipcompstat.ipcomps_hdrops); 576 return ENOMEM; 577 } 578 579 tdbi = (struct tdb_ident *)(mtag + 1); 580 bcopy(&tdbp->tdb_dst, &tdbi->dst, 581 sizeof(union sockaddr_union)); 582 tdbi->proto = tdbp->tdb_sproto; 583 tdbi->spi = tdbp->tdb_spi; 584 tdbi->rdomain = tdbp->tdb_rdomain; 585 586 m_tag_prepend(m, mtag); 587 } 588 589 if (sproto == IPPROTO_ESP) { 590 /* Packet is confidential ? */ 591 if (tdbp->tdb_encalgxform) 592 m->m_flags |= M_CONF; 593 594 /* Check if we had authenticated ESP. */ 595 if (tdbp->tdb_authalgxform) 596 m->m_flags |= M_AUTH; 597 } else if (sproto == IPPROTO_AH) { 598 m->m_flags |= M_AUTH | M_AUTH_AH; 599 } else if (sproto == IPPROTO_IPCOMP) { 600 m->m_flags |= M_COMP; 601 } 602 603 #if NPF > 0 604 /* Add pf tag if requested. */ 605 pf_tag_packet(m, tdbp->tdb_tag, -1); 606 pf_pkt_addr_changed(m); 607 #endif 608 609 if (tdbp->tdb_flags & TDBF_TUNNELING) 610 m->m_flags |= M_TUNNEL; 611 612 #if NBPFILTER > 0 613 if ((encif = enc_getif(tdbp->tdb_rdomain, tdbp->tdb_tap)) != NULL) { 614 encif->if_ipackets++; 615 encif->if_ibytes += m->m_pkthdr.len; 616 617 if (encif->if_bpf) { 618 struct enchdr hdr; 619 620 hdr.af = af; 621 hdr.spi = tdbp->tdb_spi; 622 hdr.flags = m->m_flags & (M_AUTH|M_CONF|M_AUTH_AH); 623 624 bpf_mtap_hdr(encif->if_bpf, (char *)&hdr, 625 ENC_HDRLEN, m, BPF_DIRECTION_IN); 626 } 627 } 628 #endif 629 630 /* Call the appropriate IPsec transform callback. */ 631 switch (af) { 632 #ifdef INET 633 case AF_INET: 634 switch (sproto) 635 { 636 case IPPROTO_ESP: 637 return esp4_input_cb(m); 638 639 case IPPROTO_AH: 640 return ah4_input_cb(m); 641 642 case IPPROTO_IPCOMP: 643 return ipcomp4_input_cb(m); 644 645 default: 646 DPRINTF(("ipsec_common_input_cb(): unknown/unsupported" 647 " security protocol %d\n", sproto)); 648 m_freem(m); 649 return EPFNOSUPPORT; 650 } 651 break; 652 #endif /* INET */ 653 654 #ifdef INET6 655 case AF_INET6: 656 switch (sproto) { 657 case IPPROTO_ESP: 658 return esp6_input_cb(m, skip, protoff); 659 660 case IPPROTO_AH: 661 return ah6_input_cb(m, skip, protoff); 662 663 case IPPROTO_IPCOMP: 664 return ipcomp6_input_cb(m, skip, protoff); 665 666 default: 667 DPRINTF(("ipsec_common_input_cb(): unknown/unsupported" 668 " security protocol %d\n", sproto)); 669 m_freem(m); 670 return EPFNOSUPPORT; 671 } 672 break; 673 #endif /* INET6 */ 674 675 default: 676 DPRINTF(("ipsec_common_input_cb(): unknown/unsupported " 677 "protocol family %d\n", af)); 678 m_freem(m); 679 return EPFNOSUPPORT; 680 } 681 #undef IPSEC_ISTAT 682 } 683 684 int 685 esp_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 686 size_t newlen) 687 { 688 /* All sysctl names at this level are terminal. */ 689 if (namelen != 1) 690 return (ENOTDIR); 691 692 switch (name[0]) { 693 case ESPCTL_STATS: 694 if (newp != NULL) 695 return (EPERM); 696 return (sysctl_struct(oldp, oldlenp, newp, newlen, 697 &espstat, sizeof(espstat))); 698 default: 699 if (name[0] < ESPCTL_MAXID) 700 return (sysctl_int_arr(espctl_vars, name, namelen, 701 oldp, oldlenp, newp, newlen)); 702 return (ENOPROTOOPT); 703 } 704 } 705 706 int 707 ah_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 708 size_t newlen) 709 { 710 /* All sysctl names at this level are terminal. */ 711 if (namelen != 1) 712 return (ENOTDIR); 713 714 switch (name[0]) { 715 case AHCTL_STATS: 716 if (newp != NULL) 717 return (EPERM); 718 return (sysctl_struct(oldp, oldlenp, newp, newlen, 719 &ahstat, sizeof(ahstat))); 720 default: 721 if (name[0] < AHCTL_MAXID) 722 return (sysctl_int_arr(ahctl_vars, name, namelen, 723 oldp, oldlenp, newp, newlen)); 724 return (ENOPROTOOPT); 725 } 726 } 727 728 int 729 ipcomp_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 730 size_t newlen) 731 { 732 /* All sysctl names at this level are terminal. */ 733 if (namelen != 1) 734 return (ENOTDIR); 735 736 switch (name[0]) { 737 case IPCOMPCTL_STATS: 738 if (newp != NULL) 739 return (EPERM); 740 return (sysctl_struct(oldp, oldlenp, newp, newlen, 741 &ipcompstat, sizeof(ipcompstat))); 742 default: 743 if (name[0] < IPCOMPCTL_MAXID) 744 return (sysctl_int_arr(ipcompctl_vars, name, namelen, 745 oldp, oldlenp, newp, newlen)); 746 return (ENOPROTOOPT); 747 } 748 } 749 750 #ifdef INET 751 /* IPv4 AH wrapper. */ 752 void 753 ah4_input(struct mbuf *m, ...) 754 { 755 int skip; 756 757 va_list ap; 758 va_start(ap, m); 759 skip = va_arg(ap, int); 760 va_end(ap); 761 762 ipsec_common_input(m, skip, offsetof(struct ip, ip_p), AF_INET, 763 IPPROTO_AH, 0); 764 return; 765 } 766 767 /* IPv4 AH callback. */ 768 int 769 ah4_input_cb(struct mbuf *m, ...) 770 { 771 struct ifqueue *ifq = &ipintrq; 772 int s = splnet(); 773 774 /* 775 * Interface pointer is already in first mbuf; chop off the 776 * `outer' header and reschedule. 777 */ 778 779 if (IF_QFULL(ifq)) { 780 IF_DROP(ifq); 781 ahstat.ahs_qfull++; 782 splx(s); 783 784 m_freem(m); 785 DPRINTF(("ah4_input_cb(): dropped packet because of full " 786 "IP queue\n")); 787 return ENOBUFS; 788 } 789 790 IF_ENQUEUE(ifq, m); 791 schednetisr(NETISR_IP); 792 splx(s); 793 return 0; 794 } 795 796 797 /* XXX rdomain */ 798 void * 799 ah4_ctlinput(int cmd, struct sockaddr *sa, u_int rdomain, void *v) 800 { 801 if (sa->sa_family != AF_INET || 802 sa->sa_len != sizeof(struct sockaddr_in)) 803 return (NULL); 804 805 return (ipsec_common_ctlinput(rdomain, cmd, sa, v, IPPROTO_AH)); 806 } 807 808 /* IPv4 ESP wrapper. */ 809 void 810 esp4_input(struct mbuf *m, ...) 811 { 812 int skip; 813 814 va_list ap; 815 va_start(ap, m); 816 skip = va_arg(ap, int); 817 va_end(ap); 818 819 ipsec_common_input(m, skip, offsetof(struct ip, ip_p), AF_INET, 820 IPPROTO_ESP, 0); 821 } 822 823 /* IPv4 ESP callback. */ 824 int 825 esp4_input_cb(struct mbuf *m, ...) 826 { 827 struct ifqueue *ifq = &ipintrq; 828 int s = splnet(); 829 830 /* 831 * Interface pointer is already in first mbuf; chop off the 832 * `outer' header and reschedule. 833 */ 834 if (IF_QFULL(ifq)) { 835 IF_DROP(ifq); 836 espstat.esps_qfull++; 837 splx(s); 838 839 m_freem(m); 840 DPRINTF(("esp4_input_cb(): dropped packet because of full " 841 "IP queue\n")); 842 return ENOBUFS; 843 } 844 845 IF_ENQUEUE(ifq, m); 846 schednetisr(NETISR_IP); 847 splx(s); 848 return 0; 849 } 850 851 /* IPv4 IPCOMP wrapper */ 852 void 853 ipcomp4_input(struct mbuf *m, ...) 854 { 855 int skip; 856 va_list ap; 857 va_start(ap, m); 858 skip = va_arg(ap, int); 859 va_end(ap); 860 861 ipsec_common_input(m, skip, offsetof(struct ip, ip_p), AF_INET, 862 IPPROTO_IPCOMP, 0); 863 } 864 865 /* IPv4 IPCOMP callback */ 866 int 867 ipcomp4_input_cb(struct mbuf *m, ...) 868 { 869 struct ifqueue *ifq = &ipintrq; 870 int s = splnet(); 871 872 /* 873 * Interface pointer is already in first mbuf; chop off the 874 * `outer' header and reschedule. 875 */ 876 if (IF_QFULL(ifq)) { 877 IF_DROP(ifq); 878 ipcompstat.ipcomps_qfull++; 879 splx(s); 880 881 m_freem(m); 882 DPRINTF(("ipcomp4_input_cb(): dropped packet because of full IP queue\n")); 883 return ENOBUFS; 884 } 885 886 IF_ENQUEUE(ifq, m); 887 schednetisr(NETISR_IP); 888 splx(s); 889 890 return 0; 891 } 892 893 void * 894 ipsec_common_ctlinput(u_int rdomain, int cmd, struct sockaddr *sa, 895 void *v, int proto) 896 { 897 extern u_int ip_mtudisc_timeout; 898 struct ip *ip = v; 899 int s; 900 901 if (cmd == PRC_MSGSIZE && ip && ip_mtudisc && ip->ip_v == 4) { 902 struct tdb *tdbp; 903 struct sockaddr_in dst; 904 struct icmp *icp; 905 int hlen = ip->ip_hl << 2; 906 u_int32_t spi, mtu; 907 ssize_t adjust; 908 909 /* Find the right MTU. */ 910 icp = (struct icmp *)((caddr_t) ip - 911 offsetof(struct icmp, icmp_ip)); 912 mtu = ntohs(icp->icmp_nextmtu); 913 914 /* 915 * Ignore the packet, if we do not receive a MTU 916 * or the MTU is too small to be acceptable. 917 */ 918 if (mtu < 296) 919 return (NULL); 920 921 bzero(&dst, sizeof(struct sockaddr_in)); 922 dst.sin_family = AF_INET; 923 dst.sin_len = sizeof(struct sockaddr_in); 924 dst.sin_addr.s_addr = ip->ip_dst.s_addr; 925 926 bcopy((caddr_t)ip + hlen, &spi, sizeof(u_int32_t)); 927 928 s = spltdb(); 929 tdbp = gettdb(rdomain, spi, (union sockaddr_union *)&dst, 930 proto); 931 if (tdbp == NULL || tdbp->tdb_flags & TDBF_INVALID) { 932 splx(s); 933 return (NULL); 934 } 935 936 /* Walk the chain backswards to the first tdb */ 937 for (; tdbp; tdbp = tdbp->tdb_inext) { 938 if (tdbp->tdb_flags & TDBF_INVALID || 939 (adjust = ipsec_hdrsz(tdbp)) == -1) { 940 splx(s); 941 return (NULL); 942 } 943 944 mtu -= adjust; 945 946 /* Store adjusted MTU in tdb */ 947 tdbp->tdb_mtu = mtu; 948 tdbp->tdb_mtutimeout = time_second + 949 ip_mtudisc_timeout; 950 DPRINTF(("ipsec_common_ctlinput: " 951 "spi %08x mtu %d adjust %d\n", 952 ntohl(tdbp->tdb_spi), tdbp->tdb_mtu, 953 adjust)); 954 } 955 splx(s); 956 return (NULL); 957 } 958 return (NULL); 959 } 960 961 /* XXX rdomain */ 962 void * 963 udpencap_ctlinput(int cmd, struct sockaddr *sa, u_int rdomain, void *v) 964 { 965 struct ip *ip = v; 966 struct tdb *tdbp; 967 struct icmp *icp; 968 u_int32_t mtu; 969 ssize_t adjust; 970 struct sockaddr_in dst, src; 971 union sockaddr_union *su_dst, *su_src; 972 int s; 973 974 icp = (struct icmp *)((caddr_t) ip - offsetof(struct icmp, icmp_ip)); 975 mtu = ntohs(icp->icmp_nextmtu); 976 977 /* 978 * Ignore the packet, if we do not receive a MTU 979 * or the MTU is too small to be acceptable. 980 */ 981 if (mtu < 296) 982 return (NULL); 983 984 bzero(&dst, sizeof(dst)); 985 dst.sin_family = AF_INET; 986 dst.sin_len = sizeof(struct sockaddr_in); 987 dst.sin_addr.s_addr = ip->ip_dst.s_addr; 988 su_dst = (union sockaddr_union *)&dst; 989 bzero(&src, sizeof(src)); 990 src.sin_family = AF_INET; 991 src.sin_len = sizeof(struct sockaddr_in); 992 src.sin_addr.s_addr = ip->ip_src.s_addr; 993 su_src = (union sockaddr_union *)&src; 994 995 s = spltdb(); 996 tdbp = gettdbbysrcdst(rdomain, 0, su_src, su_dst, IPPROTO_ESP); 997 998 for (; tdbp != NULL; tdbp = tdbp->tdb_snext) { 999 if (tdbp->tdb_sproto == IPPROTO_ESP && 1000 ((tdbp->tdb_flags & (TDBF_INVALID|TDBF_UDPENCAP)) 1001 == TDBF_UDPENCAP) && 1002 !bcmp(&tdbp->tdb_dst, &dst, SA_LEN(&su_dst->sa)) && 1003 !bcmp(&tdbp->tdb_src, &src, SA_LEN(&su_src->sa))) { 1004 if ((adjust = ipsec_hdrsz(tdbp)) != -1) { 1005 /* Store adjusted MTU in tdb */ 1006 tdbp->tdb_mtu = mtu - adjust; 1007 tdbp->tdb_mtutimeout = time_second + 1008 ip_mtudisc_timeout; 1009 DPRINTF(("udpencap_ctlinput: " 1010 "spi %08x mtu %d adjust %d\n", 1011 ntohl(tdbp->tdb_spi), tdbp->tdb_mtu, 1012 adjust)); 1013 } 1014 } 1015 } 1016 splx(s); 1017 return (NULL); 1018 } 1019 1020 /* XXX rdomain */ 1021 void * 1022 esp4_ctlinput(int cmd, struct sockaddr *sa, u_int rdomain, void *v) 1023 { 1024 if (sa->sa_family != AF_INET || 1025 sa->sa_len != sizeof(struct sockaddr_in)) 1026 return (NULL); 1027 1028 return (ipsec_common_ctlinput(rdomain, cmd, sa, v, IPPROTO_ESP)); 1029 } 1030 #endif /* INET */ 1031 1032 #ifdef INET6 1033 /* IPv6 AH wrapper. */ 1034 int 1035 ah6_input(struct mbuf **mp, int *offp, int proto) 1036 { 1037 int l = 0; 1038 int protoff, nxt; 1039 struct ip6_ext ip6e; 1040 1041 if (*offp < sizeof(struct ip6_hdr)) { 1042 DPRINTF(("ah6_input(): bad offset\n")); 1043 ahstat.ahs_hdrops++; 1044 m_freem(*mp); 1045 *mp = NULL; 1046 return IPPROTO_DONE; 1047 } else if (*offp == sizeof(struct ip6_hdr)) { 1048 protoff = offsetof(struct ip6_hdr, ip6_nxt); 1049 } else { 1050 /* Chase down the header chain... */ 1051 protoff = sizeof(struct ip6_hdr); 1052 nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt; 1053 1054 do { 1055 protoff += l; 1056 m_copydata(*mp, protoff, sizeof(ip6e), 1057 (caddr_t) &ip6e); 1058 1059 if (nxt == IPPROTO_AH) 1060 l = (ip6e.ip6e_len + 2) << 2; 1061 else 1062 l = (ip6e.ip6e_len + 1) << 3; 1063 #ifdef DIAGNOSTIC 1064 if (l <= 0) 1065 panic("ah6_input: l went zero or negative"); 1066 #endif 1067 1068 nxt = ip6e.ip6e_nxt; 1069 } while (protoff + l < *offp); 1070 1071 /* Malformed packet check */ 1072 if (protoff + l != *offp) { 1073 DPRINTF(("ah6_input(): bad packet header chain\n")); 1074 ahstat.ahs_hdrops++; 1075 m_freem(*mp); 1076 *mp = NULL; 1077 return IPPROTO_DONE; 1078 } 1079 protoff += offsetof(struct ip6_ext, ip6e_nxt); 1080 } 1081 ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto, 0); 1082 return IPPROTO_DONE; 1083 } 1084 1085 /* IPv6 AH callback. */ 1086 int 1087 ah6_input_cb(struct mbuf *m, int off, int protoff) 1088 { 1089 int nxt; 1090 u_int8_t nxt8; 1091 int nest = 0; 1092 1093 /* Retrieve new protocol */ 1094 m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &nxt8); 1095 nxt = nxt8; 1096 1097 /* 1098 * see the end of ip6_input for this logic. 1099 * IPPROTO_IPV[46] case will be processed just like other ones 1100 */ 1101 while (nxt != IPPROTO_DONE) { 1102 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) { 1103 ip6stat.ip6s_toomanyhdr++; 1104 goto bad; 1105 } 1106 1107 /* 1108 * Protection against faulty packet - there should be 1109 * more sanity checks in header chain processing. 1110 */ 1111 if (m->m_pkthdr.len < off) { 1112 ip6stat.ip6s_tooshort++; 1113 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 1114 goto bad; 1115 } 1116 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt); 1117 } 1118 return 0; 1119 1120 bad: 1121 m_freem(m); 1122 return EINVAL; 1123 } 1124 1125 /* IPv6 ESP wrapper. */ 1126 int 1127 esp6_input(struct mbuf **mp, int *offp, int proto) 1128 { 1129 int l = 0; 1130 int protoff, nxt; 1131 struct ip6_ext ip6e; 1132 1133 if (*offp < sizeof(struct ip6_hdr)) { 1134 DPRINTF(("esp6_input(): bad offset\n")); 1135 espstat.esps_hdrops++; 1136 m_freem(*mp); 1137 *mp = NULL; 1138 return IPPROTO_DONE; 1139 } else if (*offp == sizeof(struct ip6_hdr)) { 1140 protoff = offsetof(struct ip6_hdr, ip6_nxt); 1141 } else { 1142 /* Chase down the header chain... */ 1143 protoff = sizeof(struct ip6_hdr); 1144 nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt; 1145 1146 do { 1147 protoff += l; 1148 m_copydata(*mp, protoff, sizeof(ip6e), 1149 (caddr_t) &ip6e); 1150 1151 if (nxt == IPPROTO_AH) 1152 l = (ip6e.ip6e_len + 2) << 2; 1153 else 1154 l = (ip6e.ip6e_len + 1) << 3; 1155 #ifdef DIAGNOSTIC 1156 if (l <= 0) 1157 panic("esp6_input: l went zero or negative"); 1158 #endif 1159 1160 nxt = ip6e.ip6e_nxt; 1161 } while (protoff + l < *offp); 1162 1163 /* Malformed packet check */ 1164 if (protoff + l != *offp) { 1165 DPRINTF(("esp6_input(): bad packet header chain\n")); 1166 espstat.esps_hdrops++; 1167 m_freem(*mp); 1168 *mp = NULL; 1169 return IPPROTO_DONE; 1170 } 1171 protoff += offsetof(struct ip6_ext, ip6e_nxt); 1172 } 1173 ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto, 0); 1174 return IPPROTO_DONE; 1175 1176 } 1177 1178 /* IPv6 ESP callback */ 1179 int 1180 esp6_input_cb(struct mbuf *m, int skip, int protoff) 1181 { 1182 return ah6_input_cb(m, skip, protoff); 1183 } 1184 1185 /* IPv6 IPcomp wrapper */ 1186 int 1187 ipcomp6_input(struct mbuf **mp, int *offp, int proto) 1188 { 1189 int l = 0; 1190 int protoff, nxt; 1191 struct ip6_ext ip6e; 1192 1193 if (*offp < sizeof(struct ip6_hdr)) { 1194 DPRINTF(("ipcomp6_input(): bad offset\n")); 1195 ipcompstat.ipcomps_hdrops++; 1196 m_freem(*mp); 1197 *mp = NULL; 1198 return IPPROTO_DONE; 1199 } else if (*offp == sizeof(struct ip6_hdr)) { 1200 protoff = offsetof(struct ip6_hdr, ip6_nxt); 1201 } else { 1202 /* Chase down the header chain... */ 1203 protoff = sizeof(struct ip6_hdr); 1204 nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt; 1205 1206 do { 1207 protoff += l; 1208 m_copydata(*mp, protoff, sizeof(ip6e), 1209 (caddr_t) &ip6e); 1210 if (nxt == IPPROTO_AH) 1211 l = (ip6e.ip6e_len + 2) << 2; 1212 else 1213 l = (ip6e.ip6e_len + 1) << 3; 1214 #ifdef DIAGNOSTIC 1215 if (l <= 0) 1216 panic("ipcomp6_input: l went zero or negative"); 1217 #endif 1218 1219 nxt = ip6e.ip6e_nxt; 1220 } while (protoff + l < *offp); 1221 1222 /* Malformed packet check */ 1223 if (protoff + l != *offp) { 1224 DPRINTF(("ipcomp6_input(): bad packet header chain\n")); 1225 ipcompstat.ipcomps_hdrops++; 1226 m_freem(*mp); 1227 *mp = NULL; 1228 return IPPROTO_DONE; 1229 } 1230 1231 protoff += offsetof(struct ip6_ext, ip6e_nxt); 1232 } 1233 ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto, 0); 1234 return IPPROTO_DONE; 1235 } 1236 1237 /* IPv6 IPcomp callback */ 1238 int 1239 ipcomp6_input_cb(struct mbuf *m, int skip, int protoff) 1240 { 1241 return ah6_input_cb(m, skip, protoff); 1242 } 1243 1244 #endif /* INET6 */ 1245