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