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