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