1 /* $FreeBSD$ */ 2 /* $OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $ */ 3 /*- 4 * The authors of this code are John Ioannidis (ji@tla.org), 5 * Angelos D. Keromytis (kermit@csd.uch.gr) and 6 * Niels Provos (provos@physnet.uni-hamburg.de). 7 * 8 * This code was written by John Ioannidis for BSD/OS in Athens, Greece, 9 * in November 1995. 10 * 11 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996, 12 * by Angelos D. Keromytis. 13 * 14 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis 15 * and Niels Provos. 16 * 17 * Additional features in 1999 by Angelos D. Keromytis. 18 * 19 * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis, 20 * Angelos D. Keromytis and Niels Provos. 21 * Copyright (c) 2001, Angelos D. Keromytis. 22 * 23 * Permission to use, copy, and modify this software with or without fee 24 * is hereby granted, provided that this entire notice is included in 25 * all copies of any software which is or includes a copy or 26 * modification of this software. 27 * You may use this code under the GNU public license if you so wish. Please 28 * contribute changes back to the authors under this freer than GPL license 29 * so that we may further the use of strong encryption without limitations to 30 * all. 31 * 32 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 33 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 34 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 35 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 36 * PURPOSE. 37 */ 38 39 /* 40 * IPsec input processing. 41 */ 42 43 #include "opt_inet.h" 44 #include "opt_inet6.h" 45 #include "opt_ipsec.h" 46 #include "opt_enc.h" 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/malloc.h> 51 #include <sys/mbuf.h> 52 #include <sys/domain.h> 53 #include <sys/protosw.h> 54 #include <sys/socket.h> 55 #include <sys/errno.h> 56 #include <sys/syslog.h> 57 58 #include <net/if.h> 59 #include <net/if_var.h> 60 #include <net/pfil.h> 61 #include <net/netisr.h> 62 #include <net/vnet.h> 63 64 #include <netinet/in.h> 65 #include <netinet/in_systm.h> 66 #include <netinet/ip.h> 67 #include <netinet/ip_var.h> 68 #include <netinet/in_var.h> 69 70 #include <netinet/ip6.h> 71 #ifdef INET6 72 #include <netinet6/ip6_var.h> 73 #endif 74 #include <netinet/in_pcb.h> 75 #ifdef INET6 76 #include <netinet/icmp6.h> 77 #endif 78 79 #include <netipsec/ipsec.h> 80 #ifdef INET6 81 #include <netipsec/ipsec6.h> 82 #endif 83 #include <netipsec/ah_var.h> 84 #include <netipsec/esp.h> 85 #include <netipsec/esp_var.h> 86 #include <netipsec/ipcomp_var.h> 87 88 #include <netipsec/key.h> 89 #include <netipsec/keydb.h> 90 91 #include <netipsec/xform.h> 92 #include <netinet6/ip6protosw.h> 93 94 #include <machine/in_cksum.h> 95 #include <machine/stdarg.h> 96 97 #ifdef DEV_ENC 98 #include <net/if_enc.h> 99 #endif 100 101 102 #define IPSEC_ISTAT(proto, name) do { \ 103 if ((proto) == IPPROTO_ESP) \ 104 ESPSTAT_INC(esps_##name); \ 105 else if ((proto) == IPPROTO_AH) \ 106 AHSTAT_INC(ahs_##name); \ 107 else \ 108 IPCOMPSTAT_INC(ipcomps_##name); \ 109 } while (0) 110 111 #ifdef INET 112 static void ipsec4_common_ctlinput(int, struct sockaddr *, void *, int); 113 #endif 114 115 /* 116 * ipsec_common_input gets called when an IPsec-protected packet 117 * is received by IPv4 or IPv6. Its job is to find the right SA 118 * and call the appropriate transform. The transform callback 119 * takes care of further processing (like ingress filtering). 120 */ 121 static int 122 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto) 123 { 124 union sockaddr_union dst_address; 125 struct secasvar *sav; 126 u_int32_t spi; 127 int error; 128 #ifdef INET 129 #ifdef IPSEC_NAT_T 130 struct m_tag *tag; 131 #endif 132 #endif 133 134 IPSEC_ISTAT(sproto, input); 135 136 IPSEC_ASSERT(m != NULL, ("null packet")); 137 138 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH || 139 sproto == IPPROTO_IPCOMP, 140 ("unexpected security protocol %u", sproto)); 141 142 if ((sproto == IPPROTO_ESP && !V_esp_enable) || 143 (sproto == IPPROTO_AH && !V_ah_enable) || 144 (sproto == IPPROTO_IPCOMP && !V_ipcomp_enable)) { 145 m_freem(m); 146 IPSEC_ISTAT(sproto, pdrops); 147 return EOPNOTSUPP; 148 } 149 150 if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) { 151 m_freem(m); 152 IPSEC_ISTAT(sproto, hdrops); 153 DPRINTF(("%s: packet too small\n", __func__)); 154 return EINVAL; 155 } 156 157 /* Retrieve the SPI from the relevant IPsec header */ 158 if (sproto == IPPROTO_ESP) 159 m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi); 160 else if (sproto == IPPROTO_AH) 161 m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t), 162 (caddr_t) &spi); 163 else if (sproto == IPPROTO_IPCOMP) { 164 u_int16_t cpi; 165 m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t), 166 (caddr_t) &cpi); 167 spi = ntohl(htons(cpi)); 168 } 169 170 /* 171 * Find the SA and (indirectly) call the appropriate 172 * kernel crypto routine. The resulting mbuf chain is a valid 173 * IP packet ready to go through input processing. 174 */ 175 bzero(&dst_address, sizeof (dst_address)); 176 dst_address.sa.sa_family = af; 177 switch (af) { 178 #ifdef INET 179 case AF_INET: 180 dst_address.sin.sin_len = sizeof(struct sockaddr_in); 181 m_copydata(m, offsetof(struct ip, ip_dst), 182 sizeof(struct in_addr), 183 (caddr_t) &dst_address.sin.sin_addr); 184 #ifdef IPSEC_NAT_T 185 /* Find the source port for NAT-T; see udp*_espdecap. */ 186 tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL); 187 if (tag != NULL) 188 dst_address.sin.sin_port = ((u_int16_t *)(tag + 1))[1]; 189 #endif /* IPSEC_NAT_T */ 190 break; 191 #endif /* INET */ 192 #ifdef INET6 193 case AF_INET6: 194 dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6); 195 m_copydata(m, offsetof(struct ip6_hdr, ip6_dst), 196 sizeof(struct in6_addr), 197 (caddr_t) &dst_address.sin6.sin6_addr); 198 break; 199 #endif /* INET6 */ 200 default: 201 DPRINTF(("%s: unsupported protocol family %u\n", __func__, af)); 202 m_freem(m); 203 IPSEC_ISTAT(sproto, nopf); 204 return EPFNOSUPPORT; 205 } 206 207 /* NB: only pass dst since key_allocsa follows RFC2401 */ 208 sav = KEY_ALLOCSA(&dst_address, sproto, spi); 209 if (sav == NULL) { 210 DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n", 211 __func__, ipsec_address(&dst_address), 212 (u_long) ntohl(spi), sproto)); 213 IPSEC_ISTAT(sproto, notdb); 214 m_freem(m); 215 return ENOENT; 216 } 217 218 if (sav->tdb_xform == NULL) { 219 DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n", 220 __func__, ipsec_address(&dst_address), 221 (u_long) ntohl(spi), sproto)); 222 IPSEC_ISTAT(sproto, noxform); 223 KEY_FREESAV(&sav); 224 m_freem(m); 225 return ENXIO; 226 } 227 228 /* 229 * Call appropriate transform and return -- callback takes care of 230 * everything else. 231 */ 232 error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff); 233 KEY_FREESAV(&sav); 234 return error; 235 } 236 237 #ifdef INET 238 /* 239 * Common input handler for IPv4 AH, ESP, and IPCOMP. 240 */ 241 int 242 ipsec4_common_input(struct mbuf *m, ...) 243 { 244 va_list ap; 245 int off, nxt; 246 247 va_start(ap, m); 248 off = va_arg(ap, int); 249 nxt = va_arg(ap, int); 250 va_end(ap); 251 252 return ipsec_common_input(m, off, offsetof(struct ip, ip_p), 253 AF_INET, nxt); 254 } 255 256 int 257 ah4_input(struct mbuf **mp, int *offp, int proto) 258 { 259 struct mbuf *m; 260 int off; 261 262 m = *mp; 263 off = *offp; 264 *mp = NULL; 265 266 ipsec4_common_input(m, off, IPPROTO_AH); 267 return (IPPROTO_DONE); 268 } 269 void 270 ah4_ctlinput(int cmd, struct sockaddr *sa, void *v) 271 { 272 if (sa->sa_family == AF_INET && 273 sa->sa_len == sizeof(struct sockaddr_in)) 274 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_AH); 275 } 276 277 int 278 esp4_input(struct mbuf **mp, int *offp, int proto) 279 { 280 struct mbuf *m; 281 int off; 282 283 m = *mp; 284 off = *offp; 285 mp = NULL; 286 287 ipsec4_common_input(m, off, IPPROTO_ESP); 288 return (IPPROTO_DONE); 289 } 290 291 void 292 esp4_ctlinput(int cmd, struct sockaddr *sa, void *v) 293 { 294 if (sa->sa_family == AF_INET && 295 sa->sa_len == sizeof(struct sockaddr_in)) 296 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_ESP); 297 } 298 299 int 300 ipcomp4_input(struct mbuf **mp, int *offp, int proto) 301 { 302 struct mbuf *m; 303 int off; 304 305 m = *mp; 306 off = *offp; 307 mp = NULL; 308 309 ipsec4_common_input(m, off, IPPROTO_IPCOMP); 310 return (IPPROTO_DONE); 311 } 312 313 /* 314 * IPsec input callback for INET protocols. 315 * This routine is called as the transform callback. 316 * Takes care of filtering and other sanity checks on 317 * the processed packet. 318 */ 319 int 320 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav, 321 int skip, int protoff, struct m_tag *mt) 322 { 323 int prot, af, sproto, isr_prot; 324 struct ip *ip; 325 struct m_tag *mtag; 326 struct tdb_ident *tdbi; 327 struct secasindex *saidx; 328 int error; 329 #ifdef INET6 330 #ifdef notyet 331 char ip6buf[INET6_ADDRSTRLEN]; 332 #endif 333 #endif 334 335 IPSEC_ASSERT(m != NULL, ("null mbuf")); 336 IPSEC_ASSERT(sav != NULL, ("null SA")); 337 IPSEC_ASSERT(sav->sah != NULL, ("null SAH")); 338 saidx = &sav->sah->saidx; 339 af = saidx->dst.sa.sa_family; 340 IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af)); 341 sproto = saidx->proto; 342 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH || 343 sproto == IPPROTO_IPCOMP, 344 ("unexpected security protocol %u", sproto)); 345 346 /* Sanity check */ 347 if (m == NULL) { 348 DPRINTF(("%s: null mbuf", __func__)); 349 IPSEC_ISTAT(sproto, badkcr); 350 KEY_FREESAV(&sav); 351 return EINVAL; 352 } 353 354 if (skip != 0) { 355 /* 356 * Fix IPv4 header 357 * XXXGL: do we need this entire block? 358 */ 359 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) { 360 DPRINTF(("%s: processing failed for SA %s/%08lx\n", 361 __func__, ipsec_address(&sav->sah->saidx.dst), 362 (u_long) ntohl(sav->spi))); 363 IPSEC_ISTAT(sproto, hdrops); 364 error = ENOBUFS; 365 goto bad; 366 } 367 368 ip = mtod(m, struct ip *); 369 ip->ip_len = htons(m->m_pkthdr.len); 370 ip->ip_sum = 0; 371 ip->ip_sum = in_cksum(m, ip->ip_hl << 2); 372 } else { 373 ip = mtod(m, struct ip *); 374 } 375 prot = ip->ip_p; 376 377 #ifdef DEV_ENC 378 if_inc_counter(encif, IFCOUNTER_IPACKETS, 1); 379 if_inc_counter(encif, IFCOUNTER_IBYTES, m->m_pkthdr.len); 380 381 /* Pass the mbuf to enc0 for bpf and pfil. */ 382 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_BEFORE); 383 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0) 384 return (error); 385 #endif /* DEV_ENC */ 386 387 /* IP-in-IP encapsulation */ 388 if (prot == IPPROTO_IPIP && 389 saidx->mode != IPSEC_MODE_TRANSPORT) { 390 391 if (m->m_pkthdr.len - skip < sizeof(struct ip)) { 392 IPSEC_ISTAT(sproto, hdrops); 393 error = EINVAL; 394 goto bad; 395 } 396 /* enc0: strip outer IPv4 header */ 397 m_striphdr(m, 0, ip->ip_hl << 2); 398 399 #ifdef notyet 400 /* XXX PROXY address isn't recorded in SAH */ 401 /* 402 * Check that the inner source address is the same as 403 * the proxy address, if available. 404 */ 405 if ((saidx->proxy.sa.sa_family == AF_INET && 406 saidx->proxy.sin.sin_addr.s_addr != 407 INADDR_ANY && 408 ipn.ip_src.s_addr != 409 saidx->proxy.sin.sin_addr.s_addr) || 410 (saidx->proxy.sa.sa_family != AF_INET && 411 saidx->proxy.sa.sa_family != 0)) { 412 413 DPRINTF(("%s: inner source address %s doesn't " 414 "correspond to expected proxy source %s, " 415 "SA %s/%08lx\n", __func__, 416 inet_ntoa4(ipn.ip_src), 417 ipsp_address(saidx->proxy), 418 ipsp_address(saidx->dst), 419 (u_long) ntohl(sav->spi))); 420 421 IPSEC_ISTAT(sproto, pdrops); 422 error = EACCES; 423 goto bad; 424 } 425 #endif /* notyet */ 426 } 427 #ifdef INET6 428 /* IPv6-in-IP encapsulation. */ 429 else if (prot == IPPROTO_IPV6 && 430 saidx->mode != IPSEC_MODE_TRANSPORT) { 431 432 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) { 433 IPSEC_ISTAT(sproto, hdrops); 434 error = EINVAL; 435 goto bad; 436 } 437 /* enc0: strip IPv4 header, keep IPv6 header only */ 438 m_striphdr(m, 0, ip->ip_hl << 2); 439 #ifdef notyet 440 /* 441 * Check that the inner source address is the same as 442 * the proxy address, if available. 443 */ 444 if ((saidx->proxy.sa.sa_family == AF_INET6 && 445 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) && 446 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src, 447 &saidx->proxy.sin6.sin6_addr)) || 448 (saidx->proxy.sa.sa_family != AF_INET6 && 449 saidx->proxy.sa.sa_family != 0)) { 450 451 DPRINTF(("%s: inner source address %s doesn't " 452 "correspond to expected proxy source %s, " 453 "SA %s/%08lx\n", __func__, 454 ip6_sprintf(ip6buf, &ip6n.ip6_src), 455 ipsec_address(&saidx->proxy), 456 ipsec_address(&saidx->dst), 457 (u_long) ntohl(sav->spi))); 458 459 IPSEC_ISTAT(sproto, pdrops); 460 error = EACCES; 461 goto bad; 462 } 463 #endif /* notyet */ 464 } 465 #endif /* INET6 */ 466 else if (prot != IPPROTO_IPV6 && saidx->mode == IPSEC_MODE_ANY) { 467 /* 468 * When mode is wildcard, inner protocol is IPv6 and 469 * we have no INET6 support - drop this packet a bit later. 470 * In other cases we assume transport mode and outer 471 * header was already stripped in xform_xxx_cb. 472 */ 473 prot = IPPROTO_IPIP; 474 } 475 476 /* 477 * Record what we've done to the packet (under what SA it was 478 * processed). If we've been passed an mtag, it means the packet 479 * was already processed by an ethernet/crypto combo card and 480 * thus has a tag attached with all the right information, but 481 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to 482 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type. 483 */ 484 if (mt == NULL && sproto != IPPROTO_IPCOMP) { 485 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE, 486 sizeof(struct tdb_ident), M_NOWAIT); 487 if (mtag == NULL) { 488 DPRINTF(("%s: failed to get tag\n", __func__)); 489 IPSEC_ISTAT(sproto, hdrops); 490 error = ENOMEM; 491 goto bad; 492 } 493 494 tdbi = (struct tdb_ident *)(mtag + 1); 495 bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len); 496 tdbi->proto = sproto; 497 tdbi->spi = sav->spi; 498 /* Cache those two for enc(4) in xform_ipip. */ 499 tdbi->alg_auth = sav->alg_auth; 500 tdbi->alg_enc = sav->alg_enc; 501 502 m_tag_prepend(m, mtag); 503 } else if (mt != NULL) { 504 mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE; 505 /* XXX do we need to mark m_flags??? */ 506 } 507 508 key_sa_recordxfer(sav, m); /* record data transfer */ 509 510 /* 511 * In transport mode requeue decrypted mbuf back to IPv4 protocol 512 * handler. This is necessary to correctly expose rcvif. 513 */ 514 if (saidx->mode == IPSEC_MODE_TRANSPORT) 515 prot = IPPROTO_IPIP; 516 #ifdef DEV_ENC 517 /* 518 * Pass the mbuf to enc0 for bpf and pfil. 519 */ 520 if (prot == IPPROTO_IPIP) 521 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_AFTER); 522 #ifdef INET6 523 if (prot == IPPROTO_IPV6) 524 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_AFTER); 525 #endif 526 527 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER)) != 0) 528 return (error); 529 #endif /* DEV_ENC */ 530 531 /* 532 * Re-dispatch via software interrupt. 533 */ 534 535 switch (prot) { 536 case IPPROTO_IPIP: 537 isr_prot = NETISR_IP; 538 break; 539 #ifdef INET6 540 case IPPROTO_IPV6: 541 isr_prot = NETISR_IPV6; 542 break; 543 #endif 544 default: 545 DPRINTF(("%s: cannot handle inner ip proto %d\n", 546 __func__, prot)); 547 IPSEC_ISTAT(sproto, nopf); 548 error = EPFNOSUPPORT; 549 goto bad; 550 } 551 552 error = netisr_queue_src(isr_prot, (uintptr_t)sav->spi, m); 553 if (error) { 554 IPSEC_ISTAT(sproto, qfull); 555 DPRINTF(("%s: queue full; proto %u packet dropped\n", 556 __func__, sproto)); 557 return error; 558 } 559 return 0; 560 bad: 561 m_freem(m); 562 return error; 563 } 564 565 void 566 ipsec4_common_ctlinput(int cmd, struct sockaddr *sa, void *v, int proto) 567 { 568 /* XXX nothing just yet */ 569 } 570 #endif /* INET */ 571 572 #ifdef INET6 573 /* IPv6 AH wrapper. */ 574 int 575 ipsec6_common_input(struct mbuf **mp, int *offp, int proto) 576 { 577 int l = 0; 578 int protoff; 579 struct ip6_ext ip6e; 580 581 if (*offp < sizeof(struct ip6_hdr)) { 582 DPRINTF(("%s: bad offset %u\n", __func__, *offp)); 583 return IPPROTO_DONE; 584 } else if (*offp == sizeof(struct ip6_hdr)) { 585 protoff = offsetof(struct ip6_hdr, ip6_nxt); 586 } else { 587 /* Chase down the header chain... */ 588 protoff = sizeof(struct ip6_hdr); 589 590 do { 591 protoff += l; 592 m_copydata(*mp, protoff, sizeof(ip6e), 593 (caddr_t) &ip6e); 594 595 if (ip6e.ip6e_nxt == IPPROTO_AH) 596 l = (ip6e.ip6e_len + 2) << 2; 597 else 598 l = (ip6e.ip6e_len + 1) << 3; 599 IPSEC_ASSERT(l > 0, ("l went zero or negative")); 600 } while (protoff + l < *offp); 601 602 /* Malformed packet check */ 603 if (protoff + l != *offp) { 604 DPRINTF(("%s: bad packet header chain, protoff %u, " 605 "l %u, off %u\n", __func__, protoff, l, *offp)); 606 IPSEC_ISTAT(proto, hdrops); 607 m_freem(*mp); 608 *mp = NULL; 609 return IPPROTO_DONE; 610 } 611 protoff += offsetof(struct ip6_ext, ip6e_nxt); 612 } 613 (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto); 614 return IPPROTO_DONE; 615 } 616 617 /* 618 * IPsec input callback, called by the transform callback. Takes care of 619 * filtering and other sanity checks on the processed packet. 620 */ 621 int 622 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip, int protoff, 623 struct m_tag *mt) 624 { 625 int prot, af, sproto; 626 struct ip6_hdr *ip6; 627 struct m_tag *mtag; 628 struct tdb_ident *tdbi; 629 struct secasindex *saidx; 630 int nxt; 631 u_int8_t nxt8; 632 int error, nest; 633 #ifdef notyet 634 char ip6buf[INET6_ADDRSTRLEN]; 635 #endif 636 637 IPSEC_ASSERT(m != NULL, ("null mbuf")); 638 IPSEC_ASSERT(sav != NULL, ("null SA")); 639 IPSEC_ASSERT(sav->sah != NULL, ("null SAH")); 640 saidx = &sav->sah->saidx; 641 af = saidx->dst.sa.sa_family; 642 IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af)); 643 sproto = saidx->proto; 644 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH || 645 sproto == IPPROTO_IPCOMP, 646 ("unexpected security protocol %u", sproto)); 647 648 /* Sanity check */ 649 if (m == NULL) { 650 DPRINTF(("%s: null mbuf", __func__)); 651 IPSEC_ISTAT(sproto, badkcr); 652 error = EINVAL; 653 goto bad; 654 } 655 656 /* Fix IPv6 header */ 657 if (m->m_len < sizeof(struct ip6_hdr) && 658 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 659 660 DPRINTF(("%s: processing failed for SA %s/%08lx\n", 661 __func__, ipsec_address(&sav->sah->saidx.dst), 662 (u_long) ntohl(sav->spi))); 663 664 IPSEC_ISTAT(sproto, hdrops); 665 error = EACCES; 666 goto bad; 667 } 668 669 ip6 = mtod(m, struct ip6_hdr *); 670 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 671 672 /* Save protocol */ 673 m_copydata(m, protoff, 1, &nxt8); 674 prot = nxt8; 675 676 #ifdef DEV_ENC 677 if_inc_counter(encif, IFCOUNTER_IPACKETS, 1); 678 if_inc_counter(encif, IFCOUNTER_IBYTES, m->m_pkthdr.len); 679 680 /* Pass the mbuf to enc0 for bpf and pfil. */ 681 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_BEFORE); 682 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0) 683 return (error); 684 #endif /* DEV_ENC */ 685 686 /* IPv6-in-IP encapsulation */ 687 if (prot == IPPROTO_IPV6 && 688 saidx->mode != IPSEC_MODE_TRANSPORT) { 689 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) { 690 IPSEC_ISTAT(sproto, hdrops); 691 error = EINVAL; 692 goto bad; 693 } 694 /* ip6n will now contain the inner IPv6 header. */ 695 m_striphdr(m, 0, skip); 696 skip = 0; 697 #ifdef notyet 698 /* 699 * Check that the inner source address is the same as 700 * the proxy address, if available. 701 */ 702 if ((saidx->proxy.sa.sa_family == AF_INET6 && 703 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) && 704 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src, 705 &saidx->proxy.sin6.sin6_addr)) || 706 (saidx->proxy.sa.sa_family != AF_INET6 && 707 saidx->proxy.sa.sa_family != 0)) { 708 709 DPRINTF(("%s: inner source address %s doesn't " 710 "correspond to expected proxy source %s, " 711 "SA %s/%08lx\n", __func__, 712 ip6_sprintf(ip6buf, &ip6n.ip6_src), 713 ipsec_address(&saidx->proxy), 714 ipsec_address(&saidx->dst), 715 (u_long) ntohl(sav->spi))); 716 717 IPSEC_ISTAT(sproto, pdrops); 718 error = EACCES; 719 goto bad; 720 } 721 #endif /* notyet */ 722 } 723 #ifdef INET 724 /* IP-in-IP encapsulation */ 725 else if (prot == IPPROTO_IPIP && 726 saidx->mode != IPSEC_MODE_TRANSPORT) { 727 if (m->m_pkthdr.len - skip < sizeof(struct ip)) { 728 IPSEC_ISTAT(sproto, hdrops); 729 error = EINVAL; 730 goto bad; 731 } 732 /* ipn will now contain the inner IPv4 header */ 733 m_striphdr(m, 0, skip); 734 skip = 0; 735 #ifdef notyet 736 /* 737 * Check that the inner source address is the same as 738 * the proxy address, if available. 739 */ 740 if ((saidx->proxy.sa.sa_family == AF_INET && 741 saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY && 742 ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) || 743 (saidx->proxy.sa.sa_family != AF_INET && 744 saidx->proxy.sa.sa_family != 0)) { 745 746 DPRINTF(("%s: inner source address %s doesn't " 747 "correspond to expected proxy source %s, " 748 "SA %s/%08lx\n", __func__, 749 inet_ntoa4(ipn.ip_src), 750 ipsec_address(&saidx->proxy), 751 ipsec_address(&saidx->dst), 752 (u_long) ntohl(sav->spi))); 753 754 IPSEC_ISTAT(sproto, pdrops); 755 error = EACCES; 756 goto bad; 757 } 758 #endif /* notyet */ 759 } 760 #endif /* INET */ 761 else { 762 prot = IPPROTO_IPV6; /* for correct BPF processing */ 763 } 764 765 /* 766 * Record what we've done to the packet (under what SA it was 767 * processed). If we've been passed an mtag, it means the packet 768 * was already processed by an ethernet/crypto combo card and 769 * thus has a tag attached with all the right information, but 770 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to 771 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type. 772 */ 773 if (mt == NULL && sproto != IPPROTO_IPCOMP) { 774 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE, 775 sizeof(struct tdb_ident), M_NOWAIT); 776 if (mtag == NULL) { 777 DPRINTF(("%s: failed to get tag\n", __func__)); 778 IPSEC_ISTAT(sproto, hdrops); 779 error = ENOMEM; 780 goto bad; 781 } 782 783 tdbi = (struct tdb_ident *)(mtag + 1); 784 bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union)); 785 tdbi->proto = sproto; 786 tdbi->spi = sav->spi; 787 /* Cache those two for enc(4) in xform_ipip. */ 788 tdbi->alg_auth = sav->alg_auth; 789 tdbi->alg_enc = sav->alg_enc; 790 791 m_tag_prepend(m, mtag); 792 } else { 793 if (mt != NULL) 794 mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE; 795 /* XXX do we need to mark m_flags??? */ 796 } 797 798 key_sa_recordxfer(sav, m); 799 800 #ifdef DEV_ENC 801 /* 802 * Pass the mbuf to enc0 for bpf and pfil. 803 */ 804 #ifdef INET 805 if (prot == IPPROTO_IPIP) 806 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_AFTER); 807 #endif 808 if (prot == IPPROTO_IPV6) 809 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_AFTER); 810 811 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER)) != 0) 812 return (error); 813 #endif /* DEV_ENC */ 814 /* 815 * See the end of ip6_input for this logic. 816 * IPPROTO_IPV[46] case will be processed just like other ones 817 */ 818 nest = 0; 819 nxt = nxt8; 820 while (nxt != IPPROTO_DONE) { 821 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) { 822 IP6STAT_INC(ip6s_toomanyhdr); 823 error = EINVAL; 824 goto bad; 825 } 826 827 /* 828 * Protection against faulty packet - there should be 829 * more sanity checks in header chain processing. 830 */ 831 if (m->m_pkthdr.len < skip) { 832 IP6STAT_INC(ip6s_tooshort); 833 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 834 error = EINVAL; 835 goto bad; 836 } 837 /* 838 * Enforce IPsec policy checking if we are seeing last header. 839 * note that we do not visit this with protocols with pcb layer 840 * code - like udp/tcp/raw ip. 841 */ 842 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 && 843 ipsec6_in_reject(m, NULL)) { 844 error = EINVAL; 845 goto bad; 846 } 847 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt); 848 } 849 return 0; 850 bad: 851 if (m) 852 m_freem(m); 853 return error; 854 } 855 856 void 857 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d) 858 { 859 struct ip6ctlparam *ip6cp = NULL; 860 struct mbuf *m = NULL; 861 struct ip6_hdr *ip6; 862 int off; 863 864 if (sa->sa_family != AF_INET6 || 865 sa->sa_len != sizeof(struct sockaddr_in6)) 866 return; 867 if ((unsigned)cmd >= PRC_NCMDS) 868 return; 869 870 /* if the parameter is from icmp6, decode it. */ 871 if (d != NULL) { 872 ip6cp = (struct ip6ctlparam *)d; 873 m = ip6cp->ip6c_m; 874 ip6 = ip6cp->ip6c_ip6; 875 off = ip6cp->ip6c_off; 876 } else { 877 m = NULL; 878 ip6 = NULL; 879 off = 0; /* calm gcc */ 880 } 881 882 if (ip6 != NULL) { 883 884 struct ip6ctlparam ip6cp1; 885 886 /* 887 * Notify the error to all possible sockets via pfctlinput2. 888 * Since the upper layer information (such as protocol type, 889 * source and destination ports) is embedded in the encrypted 890 * data and might have been cut, we can't directly call 891 * an upper layer ctlinput function. However, the pcbnotify 892 * function will consider source and destination addresses 893 * as well as the flow info value, and may be able to find 894 * some PCB that should be notified. 895 * Although pfctlinput2 will call esp6_ctlinput(), there is 896 * no possibility of an infinite loop of function calls, 897 * because we don't pass the inner IPv6 header. 898 */ 899 bzero(&ip6cp1, sizeof(ip6cp1)); 900 ip6cp1.ip6c_src = ip6cp->ip6c_src; 901 pfctlinput2(cmd, sa, (void *)&ip6cp1); 902 903 /* 904 * Then go to special cases that need ESP header information. 905 * XXX: We assume that when ip6 is non NULL, 906 * M and OFF are valid. 907 */ 908 909 if (cmd == PRC_MSGSIZE) { 910 struct secasvar *sav; 911 u_int32_t spi; 912 int valid; 913 914 /* check header length before using m_copydata */ 915 if (m->m_pkthdr.len < off + sizeof (struct esp)) 916 return; 917 m_copydata(m, off + offsetof(struct esp, esp_spi), 918 sizeof(u_int32_t), (caddr_t) &spi); 919 /* 920 * Check to see if we have a valid SA corresponding to 921 * the address in the ICMP message payload. 922 */ 923 sav = KEY_ALLOCSA((union sockaddr_union *)sa, 924 IPPROTO_ESP, spi); 925 valid = (sav != NULL); 926 if (sav) 927 KEY_FREESAV(&sav); 928 929 /* XXX Further validation? */ 930 931 /* 932 * Depending on whether the SA is "valid" and 933 * routing table size (mtudisc_{hi,lo}wat), we will: 934 * - recalcurate the new MTU and create the 935 * corresponding routing entry, or 936 * - ignore the MTU change notification. 937 */ 938 icmp6_mtudisc_update(ip6cp, valid); 939 } 940 } else { 941 /* we normally notify any pcb here */ 942 } 943 } 944 #endif /* INET6 */ 945