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