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/route.h> 60 #include <net/netisr.h> 61 62 #include <netinet/in.h> 63 #include <netinet/in_systm.h> 64 #include <netinet/ip.h> 65 #include <netinet/ip_var.h> 66 #include <netinet/in_var.h> 67 68 #include <netinet/ip6.h> 69 #ifdef INET6 70 #include <netinet6/ip6_var.h> 71 #endif 72 #include <netinet/in_pcb.h> 73 #ifdef INET6 74 #include <netinet/icmp6.h> 75 #endif 76 77 #include <netipsec/ipsec.h> 78 #ifdef INET6 79 #include <netipsec/ipsec6.h> 80 #endif 81 #include <netipsec/ah_var.h> 82 #include <netipsec/esp.h> 83 #include <netipsec/esp_var.h> 84 #include <netipsec/ipcomp_var.h> 85 86 #include <netipsec/key.h> 87 #include <netipsec/keydb.h> 88 89 #include <netipsec/xform.h> 90 #include <netinet6/ip6protosw.h> 91 92 #include <machine/in_cksum.h> 93 #include <machine/stdarg.h> 94 95 #define IPSEC_ISTAT(p,x,y,z) ((p) == IPPROTO_ESP ? (x)++ : \ 96 (p) == IPPROTO_AH ? (y)++ : (z)++) 97 98 static void ipsec4_common_ctlinput(int, struct sockaddr *, void *, int); 99 100 /* 101 * ipsec_common_input gets called when an IPsec-protected packet 102 * is received by IPv4 or IPv6. It's job is to find the right SA 103 # and call the appropriate transform. The transform callback 104 * takes care of further processing (like ingress filtering). 105 */ 106 static int 107 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto) 108 { 109 union sockaddr_union dst_address; 110 struct secasvar *sav; 111 u_int32_t spi; 112 int error; 113 114 IPSEC_ISTAT(sproto, espstat.esps_input, ahstat.ahs_input, 115 ipcompstat.ipcomps_input); 116 117 IPSEC_ASSERT(m != NULL, ("null packet")); 118 119 if ((sproto == IPPROTO_ESP && !esp_enable) || 120 (sproto == IPPROTO_AH && !ah_enable) || 121 (sproto == IPPROTO_IPCOMP && !ipcomp_enable)) { 122 m_freem(m); 123 IPSEC_ISTAT(sproto, espstat.esps_pdrops, ahstat.ahs_pdrops, 124 ipcompstat.ipcomps_pdrops); 125 return EOPNOTSUPP; 126 } 127 128 if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) { 129 m_freem(m); 130 IPSEC_ISTAT(sproto, espstat.esps_hdrops, ahstat.ahs_hdrops, 131 ipcompstat.ipcomps_hdrops); 132 DPRINTF(("%s: packet too small\n", __func__)); 133 return EINVAL; 134 } 135 136 /* Retrieve the SPI from the relevant IPsec header */ 137 if (sproto == IPPROTO_ESP) 138 m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi); 139 else if (sproto == IPPROTO_AH) 140 m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t), 141 (caddr_t) &spi); 142 else if (sproto == IPPROTO_IPCOMP) { 143 u_int16_t cpi; 144 m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t), 145 (caddr_t) &cpi); 146 spi = ntohl(htons(cpi)); 147 } 148 149 /* 150 * Find the SA and (indirectly) call the appropriate 151 * kernel crypto routine. The resulting mbuf chain is a valid 152 * IP packet ready to go through input processing. 153 */ 154 bzero(&dst_address, sizeof (dst_address)); 155 dst_address.sa.sa_family = af; 156 switch (af) { 157 #ifdef INET 158 case AF_INET: 159 dst_address.sin.sin_len = sizeof(struct sockaddr_in); 160 m_copydata(m, offsetof(struct ip, ip_dst), 161 sizeof(struct in_addr), 162 (caddr_t) &dst_address.sin.sin_addr); 163 break; 164 #endif /* INET */ 165 #ifdef INET6 166 case AF_INET6: 167 dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6); 168 m_copydata(m, offsetof(struct ip6_hdr, ip6_dst), 169 sizeof(struct in6_addr), 170 (caddr_t) &dst_address.sin6.sin6_addr); 171 break; 172 #endif /* INET6 */ 173 default: 174 DPRINTF(("%s: unsupported protocol family %u\n", __func__, af)); 175 m_freem(m); 176 IPSEC_ISTAT(sproto, espstat.esps_nopf, ahstat.ahs_nopf, 177 ipcompstat.ipcomps_nopf); 178 return EPFNOSUPPORT; 179 } 180 181 /* NB: only pass dst since key_allocsa follows RFC2401 */ 182 sav = KEY_ALLOCSA(&dst_address, sproto, spi); 183 if (sav == NULL) { 184 DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n", 185 __func__, ipsec_address(&dst_address), 186 (u_long) ntohl(spi), sproto)); 187 IPSEC_ISTAT(sproto, espstat.esps_notdb, ahstat.ahs_notdb, 188 ipcompstat.ipcomps_notdb); 189 m_freem(m); 190 return ENOENT; 191 } 192 193 if (sav->tdb_xform == NULL) { 194 DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n", 195 __func__, ipsec_address(&dst_address), 196 (u_long) ntohl(spi), sproto)); 197 IPSEC_ISTAT(sproto, espstat.esps_noxform, ahstat.ahs_noxform, 198 ipcompstat.ipcomps_noxform); 199 KEY_FREESAV(&sav); 200 m_freem(m); 201 return ENXIO; 202 } 203 204 /* 205 * Call appropriate transform and return -- callback takes care of 206 * everything else. 207 */ 208 error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff); 209 KEY_FREESAV(&sav); 210 return error; 211 } 212 213 #ifdef INET 214 /* 215 * Common input handler for IPv4 AH, ESP, and IPCOMP. 216 */ 217 int 218 ipsec4_common_input(struct mbuf *m, ...) 219 { 220 va_list ap; 221 int off, nxt; 222 223 va_start(ap, m); 224 off = va_arg(ap, int); 225 nxt = va_arg(ap, int); 226 va_end(ap); 227 228 return ipsec_common_input(m, off, offsetof(struct ip, ip_p), 229 AF_INET, nxt); 230 } 231 232 void 233 ah4_input(struct mbuf *m, int off) 234 { 235 ipsec4_common_input(m, off, IPPROTO_AH); 236 } 237 void 238 ah4_ctlinput(int cmd, struct sockaddr *sa, void *v) 239 { 240 if (sa->sa_family == AF_INET && 241 sa->sa_len == sizeof(struct sockaddr_in)) 242 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_AH); 243 } 244 245 void 246 esp4_input(struct mbuf *m, int off) 247 { 248 ipsec4_common_input(m, off, IPPROTO_ESP); 249 } 250 void 251 esp4_ctlinput(int cmd, struct sockaddr *sa, void *v) 252 { 253 if (sa->sa_family == AF_INET && 254 sa->sa_len == sizeof(struct sockaddr_in)) 255 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_ESP); 256 } 257 258 void 259 ipcomp4_input(struct mbuf *m, int off) 260 { 261 ipsec4_common_input(m, off, IPPROTO_IPCOMP); 262 } 263 264 /* 265 * IPsec input callback for INET protocols. 266 * This routine is called as the transform callback. 267 * Takes care of filtering and other sanity checks on 268 * the processed packet. 269 */ 270 int 271 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav, 272 int skip, int protoff, struct m_tag *mt) 273 { 274 int prot, af, sproto; 275 struct ip *ip; 276 struct m_tag *mtag; 277 struct tdb_ident *tdbi; 278 struct secasindex *saidx; 279 int error; 280 #ifdef INET6 281 #ifdef notyet 282 char ip6buf[INET6_ADDRSTRLEN]; 283 #endif 284 #endif 285 286 IPSEC_SPLASSERT_SOFTNET(__func__); 287 288 IPSEC_ASSERT(m != NULL, ("null mbuf")); 289 IPSEC_ASSERT(sav != NULL, ("null SA")); 290 IPSEC_ASSERT(sav->sah != NULL, ("null SAH")); 291 saidx = &sav->sah->saidx; 292 af = saidx->dst.sa.sa_family; 293 IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af)); 294 sproto = saidx->proto; 295 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH || 296 sproto == IPPROTO_IPCOMP, 297 ("unexpected security protocol %u", sproto)); 298 299 /* Sanity check */ 300 if (m == NULL) { 301 DPRINTF(("%s: null mbuf", __func__)); 302 IPSEC_ISTAT(sproto, espstat.esps_badkcr, ahstat.ahs_badkcr, 303 ipcompstat.ipcomps_badkcr); 304 KEY_FREESAV(&sav); 305 return EINVAL; 306 } 307 308 if (skip != 0) { 309 /* Fix IPv4 header */ 310 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) { 311 DPRINTF(("%s: processing failed for SA %s/%08lx\n", 312 __func__, ipsec_address(&sav->sah->saidx.dst), 313 (u_long) ntohl(sav->spi))); 314 IPSEC_ISTAT(sproto, espstat.esps_hdrops, ahstat.ahs_hdrops, 315 ipcompstat.ipcomps_hdrops); 316 error = ENOBUFS; 317 goto bad; 318 } 319 320 ip = mtod(m, struct ip *); 321 ip->ip_len = htons(m->m_pkthdr.len); 322 ip->ip_off = htons(ip->ip_off); 323 ip->ip_sum = 0; 324 ip->ip_sum = in_cksum(m, ip->ip_hl << 2); 325 } else { 326 ip = mtod(m, struct ip *); 327 } 328 prot = ip->ip_p; 329 330 #ifdef notyet 331 /* IP-in-IP encapsulation */ 332 if (prot == IPPROTO_IPIP) { 333 struct ip ipn; 334 335 if (m->m_pkthdr.len - skip < sizeof(struct ip)) { 336 IPSEC_ISTAT(sproto, espstat.esps_hdrops, 337 ahstat.ahs_hdrops, 338 ipcompstat.ipcomps_hdrops); 339 error = EINVAL; 340 goto bad; 341 } 342 /* ipn will now contain the inner IPv4 header */ 343 m_copydata(m, ip->ip_hl << 2, sizeof(struct ip), 344 (caddr_t) &ipn); 345 346 /* XXX PROXY address isn't recorded in SAH */ 347 /* 348 * Check that the inner source address is the same as 349 * the proxy address, if available. 350 */ 351 if ((saidx->proxy.sa.sa_family == AF_INET && 352 saidx->proxy.sin.sin_addr.s_addr != 353 INADDR_ANY && 354 ipn.ip_src.s_addr != 355 saidx->proxy.sin.sin_addr.s_addr) || 356 (saidx->proxy.sa.sa_family != AF_INET && 357 saidx->proxy.sa.sa_family != 0)) { 358 359 DPRINTF(("%s: inner source address %s doesn't " 360 "correspond to expected proxy source %s, " 361 "SA %s/%08lx\n", __func__, 362 inet_ntoa4(ipn.ip_src), 363 ipsp_address(saidx->proxy), 364 ipsp_address(saidx->dst), 365 (u_long) ntohl(sav->spi))); 366 367 IPSEC_ISTAT(sproto, espstat.esps_pdrops, 368 ahstat.ahs_pdrops, 369 ipcompstat.ipcomps_pdrops); 370 error = EACCES; 371 goto bad; 372 } 373 } 374 #ifdef INET6 375 /* IPv6-in-IP encapsulation. */ 376 if (prot == IPPROTO_IPV6) { 377 struct ip6_hdr ip6n; 378 379 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) { 380 IPSEC_ISTAT(sproto, espstat.esps_hdrops, 381 ahstat.ahs_hdrops, 382 ipcompstat.ipcomps_hdrops); 383 error = EINVAL; 384 goto bad; 385 } 386 /* ip6n will now contain the inner IPv6 header. */ 387 m_copydata(m, ip->ip_hl << 2, sizeof(struct ip6_hdr), 388 (caddr_t) &ip6n); 389 390 /* 391 * Check that the inner source address is the same as 392 * the proxy address, if available. 393 */ 394 if ((saidx->proxy.sa.sa_family == AF_INET6 && 395 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) && 396 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src, 397 &saidx->proxy.sin6.sin6_addr)) || 398 (saidx->proxy.sa.sa_family != AF_INET6 && 399 saidx->proxy.sa.sa_family != 0)) { 400 401 DPRINTF(("%s: inner source address %s doesn't " 402 "correspond to expected proxy source %s, " 403 "SA %s/%08lx\n", __func__, 404 ip6_sprintf(ip6buf, &ip6n.ip6_src), 405 ipsec_address(&saidx->proxy), 406 ipsec_address(&saidx->dst), 407 (u_long) ntohl(sav->spi))); 408 409 IPSEC_ISTAT(sproto, espstat.esps_pdrops, 410 ahstat.ahs_pdrops, 411 ipcompstat.ipcomps_pdrops); 412 error = EACCES; 413 goto bad; 414 } 415 } 416 #endif /* INET6 */ 417 #endif /*XXX*/ 418 419 /* 420 * Record what we've done to the packet (under what SA it was 421 * processed). If we've been passed an mtag, it means the packet 422 * was already processed by an ethernet/crypto combo card and 423 * thus has a tag attached with all the right information, but 424 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to 425 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type. 426 */ 427 if (mt == NULL && sproto != IPPROTO_IPCOMP) { 428 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE, 429 sizeof(struct tdb_ident), M_NOWAIT); 430 if (mtag == NULL) { 431 DPRINTF(("%s: failed to get tag\n", __func__)); 432 IPSEC_ISTAT(sproto, espstat.esps_hdrops, 433 ahstat.ahs_hdrops, ipcompstat.ipcomps_hdrops); 434 error = ENOMEM; 435 goto bad; 436 } 437 438 tdbi = (struct tdb_ident *)(mtag + 1); 439 bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len); 440 tdbi->proto = sproto; 441 tdbi->spi = sav->spi; 442 443 m_tag_prepend(m, mtag); 444 } else { 445 mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE; 446 /* XXX do we need to mark m_flags??? */ 447 } 448 449 key_sa_recordxfer(sav, m); /* record data transfer */ 450 451 #ifdef DEV_ENC 452 /* 453 * Pass the mbuf to enc0 for bpf and pfil. We will filter the IPIP 454 * packet later after it has been decapsulated. 455 */ 456 ipsec_bpf(m, sav, AF_INET); 457 458 if (prot != IPPROTO_IPIP) 459 if ((error = ipsec_filter(&m, 1)) != 0) 460 return (error); 461 #endif 462 463 /* 464 * Re-dispatch via software interrupt. 465 */ 466 if ((error = netisr_queue(NETISR_IP, m))) { 467 IPSEC_ISTAT(sproto, espstat.esps_qfull, ahstat.ahs_qfull, 468 ipcompstat.ipcomps_qfull); 469 470 DPRINTF(("%s: queue full; proto %u packet dropped\n", 471 __func__, sproto)); 472 return error; 473 } 474 return 0; 475 bad: 476 m_freem(m); 477 return error; 478 } 479 480 void 481 ipsec4_common_ctlinput(int cmd, struct sockaddr *sa, void *v, int proto) 482 { 483 /* XXX nothing just yet */ 484 } 485 #endif /* INET */ 486 487 #ifdef INET6 488 /* IPv6 AH wrapper. */ 489 int 490 ipsec6_common_input(struct mbuf **mp, int *offp, int proto) 491 { 492 int l = 0; 493 int protoff; 494 struct ip6_ext ip6e; 495 496 if (*offp < sizeof(struct ip6_hdr)) { 497 DPRINTF(("%s: bad offset %u\n", __func__, *offp)); 498 return IPPROTO_DONE; 499 } else if (*offp == sizeof(struct ip6_hdr)) { 500 protoff = offsetof(struct ip6_hdr, ip6_nxt); 501 } else { 502 /* Chase down the header chain... */ 503 protoff = sizeof(struct ip6_hdr); 504 505 do { 506 protoff += l; 507 m_copydata(*mp, protoff, sizeof(ip6e), 508 (caddr_t) &ip6e); 509 510 if (ip6e.ip6e_nxt == IPPROTO_AH) 511 l = (ip6e.ip6e_len + 2) << 2; 512 else 513 l = (ip6e.ip6e_len + 1) << 3; 514 IPSEC_ASSERT(l > 0, ("l went zero or negative")); 515 } while (protoff + l < *offp); 516 517 /* Malformed packet check */ 518 if (protoff + l != *offp) { 519 DPRINTF(("%s: bad packet header chain, protoff %u, " 520 "l %u, off %u\n", __func__, protoff, l, *offp)); 521 IPSEC_ISTAT(proto, espstat.esps_hdrops, 522 ahstat.ahs_hdrops, 523 ipcompstat.ipcomps_hdrops); 524 m_freem(*mp); 525 *mp = NULL; 526 return IPPROTO_DONE; 527 } 528 protoff += offsetof(struct ip6_ext, ip6e_nxt); 529 } 530 (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto); 531 return IPPROTO_DONE; 532 } 533 534 /* 535 * IPsec input callback, called by the transform callback. Takes care of 536 * filtering and other sanity checks on the processed packet. 537 */ 538 int 539 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip, int protoff, 540 struct m_tag *mt) 541 { 542 int prot, af, sproto; 543 struct ip6_hdr *ip6; 544 struct m_tag *mtag; 545 struct tdb_ident *tdbi; 546 struct secasindex *saidx; 547 int nxt; 548 u_int8_t nxt8; 549 int error, nest; 550 #ifdef notyet 551 char ip6buf[INET6_ADDRSTRLEN]; 552 #endif 553 554 IPSEC_ASSERT(m != NULL, ("null mbuf")); 555 IPSEC_ASSERT(sav != NULL, ("null SA")); 556 IPSEC_ASSERT(sav->sah != NULL, ("null SAH")); 557 saidx = &sav->sah->saidx; 558 af = saidx->dst.sa.sa_family; 559 IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af)); 560 sproto = saidx->proto; 561 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH || 562 sproto == IPPROTO_IPCOMP, 563 ("unexpected security protocol %u", sproto)); 564 565 /* Sanity check */ 566 if (m == NULL) { 567 DPRINTF(("%s: null mbuf", __func__)); 568 IPSEC_ISTAT(sproto, espstat.esps_badkcr, ahstat.ahs_badkcr, 569 ipcompstat.ipcomps_badkcr); 570 error = EINVAL; 571 goto bad; 572 } 573 574 /* Fix IPv6 header */ 575 if (m->m_len < sizeof(struct ip6_hdr) && 576 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 577 578 DPRINTF(("%s: processing failed for SA %s/%08lx\n", 579 __func__, ipsec_address(&sav->sah->saidx.dst), 580 (u_long) ntohl(sav->spi))); 581 582 IPSEC_ISTAT(sproto, espstat.esps_hdrops, ahstat.ahs_hdrops, 583 ipcompstat.ipcomps_hdrops); 584 error = EACCES; 585 goto bad; 586 } 587 588 ip6 = mtod(m, struct ip6_hdr *); 589 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 590 591 /* Save protocol */ 592 m_copydata(m, protoff, 1, (unsigned char *) &prot); 593 594 #ifdef notyet 595 #ifdef INET 596 /* IP-in-IP encapsulation */ 597 if (prot == IPPROTO_IPIP) { 598 struct ip ipn; 599 600 if (m->m_pkthdr.len - skip < sizeof(struct ip)) { 601 IPSEC_ISTAT(sproto, espstat.esps_hdrops, 602 ahstat.ahs_hdrops, 603 ipcompstat.ipcomps_hdrops); 604 error = EINVAL; 605 goto bad; 606 } 607 /* ipn will now contain the inner IPv4 header */ 608 m_copydata(m, skip, sizeof(struct ip), (caddr_t) &ipn); 609 610 /* 611 * Check that the inner source address is the same as 612 * the proxy address, if available. 613 */ 614 if ((saidx->proxy.sa.sa_family == AF_INET && 615 saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY && 616 ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) || 617 (saidx->proxy.sa.sa_family != AF_INET && 618 saidx->proxy.sa.sa_family != 0)) { 619 620 DPRINTF(("%s: inner source address %s doesn't " 621 "correspond to expected proxy source %s, " 622 "SA %s/%08lx\n", __func__, 623 inet_ntoa4(ipn.ip_src), 624 ipsec_address(&saidx->proxy), 625 ipsec_address(&saidx->dst), 626 (u_long) ntohl(sav->spi))); 627 628 IPSEC_ISTATsproto, (espstat.esps_pdrops, 629 ahstat.ahs_pdrops, ipcompstat.ipcomps_pdrops); 630 error = EACCES; 631 goto bad; 632 } 633 } 634 #endif /* INET */ 635 636 /* IPv6-in-IP encapsulation */ 637 if (prot == IPPROTO_IPV6) { 638 struct ip6_hdr ip6n; 639 640 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) { 641 IPSEC_ISTAT(sproto, espstat.esps_hdrops, 642 ahstat.ahs_hdrops, 643 ipcompstat.ipcomps_hdrops); 644 error = EINVAL; 645 goto bad; 646 } 647 /* ip6n will now contain the inner IPv6 header. */ 648 m_copydata(m, skip, sizeof(struct ip6_hdr), 649 (caddr_t) &ip6n); 650 651 /* 652 * Check that the inner source address is the same as 653 * the proxy address, if available. 654 */ 655 if ((saidx->proxy.sa.sa_family == AF_INET6 && 656 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) && 657 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src, 658 &saidx->proxy.sin6.sin6_addr)) || 659 (saidx->proxy.sa.sa_family != AF_INET6 && 660 saidx->proxy.sa.sa_family != 0)) { 661 662 DPRINTF(("%s: inner source address %s doesn't " 663 "correspond to expected proxy source %s, " 664 "SA %s/%08lx\n", __func__, 665 ip6_sprintf(ip6buf, &ip6n.ip6_src), 666 ipsec_address(&saidx->proxy), 667 ipsec_address(&saidx->dst), 668 (u_long) ntohl(sav->spi))); 669 670 IPSEC_ISTAT(sproto, espstat.esps_pdrops, 671 ahstat.ahs_pdrops, ipcompstat.ipcomps_pdrops); 672 error = EACCES; 673 goto bad; 674 } 675 } 676 #endif /*XXX*/ 677 678 /* 679 * Record what we've done to the packet (under what SA it was 680 * processed). If we've been passed an mtag, it means the packet 681 * was already processed by an ethernet/crypto combo card and 682 * thus has a tag attached with all the right information, but 683 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to 684 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type. 685 */ 686 if (mt == NULL && sproto != IPPROTO_IPCOMP) { 687 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE, 688 sizeof(struct tdb_ident), M_NOWAIT); 689 if (mtag == NULL) { 690 DPRINTF(("%s: failed to get tag\n", __func__)); 691 IPSEC_ISTAT(sproto, espstat.esps_hdrops, 692 ahstat.ahs_hdrops, ipcompstat.ipcomps_hdrops); 693 error = ENOMEM; 694 goto bad; 695 } 696 697 tdbi = (struct tdb_ident *)(mtag + 1); 698 bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union)); 699 tdbi->proto = sproto; 700 tdbi->spi = sav->spi; 701 702 m_tag_prepend(m, mtag); 703 } else { 704 if (mt != NULL) 705 mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE; 706 /* XXX do we need to mark m_flags??? */ 707 } 708 709 key_sa_recordxfer(sav, m); 710 711 /* Retrieve new protocol */ 712 m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &nxt8); 713 714 /* 715 * See the end of ip6_input for this logic. 716 * IPPROTO_IPV[46] case will be processed just like other ones 717 */ 718 nest = 0; 719 nxt = nxt8; 720 while (nxt != IPPROTO_DONE) { 721 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) { 722 ip6stat.ip6s_toomanyhdr++; 723 error = EINVAL; 724 goto bad; 725 } 726 727 /* 728 * Protection against faulty packet - there should be 729 * more sanity checks in header chain processing. 730 */ 731 if (m->m_pkthdr.len < skip) { 732 ip6stat.ip6s_tooshort++; 733 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 734 error = EINVAL; 735 goto bad; 736 } 737 /* 738 * Enforce IPsec policy checking if we are seeing last header. 739 * note that we do not visit this with protocols with pcb layer 740 * code - like udp/tcp/raw ip. 741 */ 742 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 && 743 ipsec6_in_reject(m, NULL)) { 744 error = EINVAL; 745 goto bad; 746 } 747 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt); 748 } 749 return 0; 750 bad: 751 if (m) 752 m_freem(m); 753 return error; 754 } 755 756 void 757 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d) 758 { 759 if (sa->sa_family != AF_INET6 || 760 sa->sa_len != sizeof(struct sockaddr_in6)) 761 return; 762 if ((unsigned)cmd >= PRC_NCMDS) 763 return; 764 765 /* if the parameter is from icmp6, decode it. */ 766 if (d != NULL) { 767 struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d; 768 struct mbuf *m = ip6cp->ip6c_m; 769 int off = ip6cp->ip6c_off; 770 771 struct ip6ctlparam ip6cp1; 772 773 /* 774 * Notify the error to all possible sockets via pfctlinput2. 775 * Since the upper layer information (such as protocol type, 776 * source and destination ports) is embedded in the encrypted 777 * data and might have been cut, we can't directly call 778 * an upper layer ctlinput function. However, the pcbnotify 779 * function will consider source and destination addresses 780 * as well as the flow info value, and may be able to find 781 * some PCB that should be notified. 782 * Although pfctlinput2 will call esp6_ctlinput(), there is 783 * no possibility of an infinite loop of function calls, 784 * because we don't pass the inner IPv6 header. 785 */ 786 bzero(&ip6cp1, sizeof(ip6cp1)); 787 ip6cp1.ip6c_src = ip6cp->ip6c_src; 788 pfctlinput2(cmd, sa, (void *)&ip6cp1); 789 790 /* 791 * Then go to special cases that need ESP header information. 792 * XXX: We assume that when ip6 is non NULL, 793 * M and OFF are valid. 794 */ 795 796 if (cmd == PRC_MSGSIZE) { 797 struct secasvar *sav; 798 u_int32_t spi; 799 int valid; 800 801 /* check header length before using m_copydata */ 802 if (m->m_pkthdr.len < off + sizeof (struct esp)) 803 return; 804 m_copydata(m, off + offsetof(struct esp, esp_spi), 805 sizeof(u_int32_t), (caddr_t) &spi); 806 /* 807 * Check to see if we have a valid SA corresponding to 808 * the address in the ICMP message payload. 809 */ 810 sav = KEY_ALLOCSA((union sockaddr_union *)sa, 811 IPPROTO_ESP, spi); 812 valid = (sav != NULL); 813 if (sav) 814 KEY_FREESAV(&sav); 815 816 /* XXX Further validation? */ 817 818 /* 819 * Depending on whether the SA is "valid" and 820 * routing table size (mtudisc_{hi,lo}wat), we will: 821 * - recalcurate the new MTU and create the 822 * corresponding routing entry, or 823 * - ignore the MTU change notification. 824 */ 825 icmp6_mtudisc_update(ip6cp, valid); 826 } 827 } else { 828 /* we normally notify any pcb here */ 829 } 830 } 831 #endif /* INET6 */ 832