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