1 /* $NetBSD: ipsec_output.c,v 1.39 2013/06/04 22:47:37 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD: /repoman/r/ncvs/src/sys/netipsec/ipsec_output.c,v 1.3.2.2 2003/03/28 20:32:53 sam Exp $ 29 */ 30 31 #include <sys/cdefs.h> 32 __KERNEL_RCSID(0, "$NetBSD: ipsec_output.c,v 1.39 2013/06/04 22:47:37 christos Exp $"); 33 34 /* 35 * IPsec output processing. 36 */ 37 #include "opt_inet.h" 38 #ifdef __FreeBSD__ 39 #include "opt_inet6.h" 40 #endif 41 #include "opt_ipsec.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/mbuf.h> 46 #include <sys/domain.h> 47 #include <sys/protosw.h> 48 #include <sys/socket.h> 49 #include <sys/errno.h> 50 #include <sys/syslog.h> 51 52 #include <net/if.h> 53 #include <net/route.h> 54 55 #include <netinet/in.h> 56 #include <netinet/in_systm.h> 57 #include <netinet/ip.h> 58 #include <netinet/ip_var.h> 59 #include <netinet/in_var.h> 60 #include <netinet/ip_ecn.h> 61 #ifdef INET6 62 # ifdef __FreeBSD__ 63 # include <netinet6/ip6_ecn.h> 64 # endif 65 #endif 66 67 #include <netinet/ip6.h> 68 #ifdef INET6 69 #include <netinet6/ip6_var.h> 70 #endif 71 #include <netinet/in_pcb.h> 72 #ifdef INET6 73 #include <netinet/icmp6.h> 74 #endif 75 #include <netinet/udp.h> 76 77 #include <netipsec/ipsec.h> 78 #include <netipsec/ipsec_var.h> 79 #include <netipsec/ipsec_private.h> 80 #ifdef INET6 81 #include <netipsec/ipsec6.h> 82 #endif 83 #include <netipsec/ah_var.h> 84 #include <netipsec/esp_var.h> 85 #include <netipsec/ipcomp_var.h> 86 87 #include <netipsec/xform.h> 88 89 #include <netipsec/key.h> 90 #include <netipsec/keydb.h> 91 #include <netipsec/key_debug.h> 92 #include <netipsec/ipsec_osdep.h> 93 94 #include <net/net_osdep.h> /* ovbcopy() in ipsec6_encapsulate() */ 95 96 97 /* 98 * Add a IPSEC_OUT_DONE tag to mark that we have finished the ipsec processing 99 * It will be used by ip{,6}_output to check if we have already or not 100 * processed this packet. 101 */ 102 static int 103 ipsec_register_done(struct mbuf *m, int * error) 104 { 105 struct m_tag *mtag; 106 107 mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, 0, M_NOWAIT); 108 if (mtag == NULL) { 109 DPRINTF(("ipsec_register_done: could not get packet tag\n")); 110 *error = ENOMEM; 111 return -1; 112 } 113 114 m_tag_prepend(m, mtag); 115 return 0; 116 } 117 118 static int 119 ipsec_reinject_ipstack(struct mbuf *m, int af) 120 { 121 #ifdef INET 122 struct ip * ip; 123 #endif /* INET */ 124 #if defined(INET) || defined(INET6) 125 int rv; 126 #endif 127 128 switch (af) { 129 #ifdef INET 130 case AF_INET: 131 ip = mtod(m, struct ip *); 132 #ifdef __FreeBSD__ 133 /* FreeBSD ip_output() expects ip_len, ip_off in host endian */ 134 ip->ip_len = ntohs(ip->ip_len); 135 ip->ip_off = ntohs(ip->ip_off); 136 #endif /* __FreeBSD_ */ 137 KERNEL_LOCK(1, NULL); 138 rv = ip_output(m, NULL, NULL, IP_RAWOUTPUT|IP_NOIPNEWID, 139 NULL, NULL); 140 KERNEL_UNLOCK_ONE(NULL); 141 return rv; 142 143 #endif /* INET */ 144 #ifdef INET6 145 case AF_INET6: 146 /* 147 * We don't need massage, IPv6 header fields are always in 148 * net endian. 149 */ 150 KERNEL_LOCK(1, NULL); 151 rv = ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL); 152 KERNEL_UNLOCK_ONE(NULL); 153 return rv; 154 #endif /* INET6 */ 155 } 156 157 panic("ipsec_reinject_ipstack : iunknown protocol family %u\n", af); 158 return -1; /* NOTREACHED */ 159 } 160 161 int 162 ipsec_process_done(struct mbuf *m, struct ipsecrequest *isr) 163 { 164 struct secasvar *sav; 165 struct secasindex *saidx; 166 int error; 167 #ifdef INET 168 struct ip * ip; 169 #endif /* INET */ 170 #ifdef INET6 171 struct ip6_hdr * ip6; 172 #endif /* INET6 */ 173 struct mbuf * mo; 174 struct udphdr *udp = NULL; 175 uint64_t * data = NULL; 176 int hlen, roff; 177 178 IPSEC_SPLASSERT_SOFTNET("ipsec_process_done"); 179 180 IPSEC_ASSERT(m != NULL, ("ipsec_process_done: null mbuf")); 181 IPSEC_ASSERT(isr != NULL, ("ipsec_process_done: null ISR")); 182 sav = isr->sav; 183 IPSEC_ASSERT(sav != NULL, ("ipsec_process_done: null SA")); 184 IPSEC_ASSERT(sav->sah != NULL, ("ipsec_process_done: null SAH")); 185 186 saidx = &sav->sah->saidx; 187 188 if(sav->natt_type != 0) { 189 ip = mtod(m, struct ip *); 190 191 hlen = sizeof(struct udphdr); 192 if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) 193 hlen += sizeof(uint64_t); 194 195 mo = m_makespace(m, sizeof(struct ip), hlen, &roff); 196 if (mo == NULL) { 197 DPRINTF(("ipsec_process_done : failed to inject" 198 "%u byte UDP for SA %s/%08lx\n", 199 hlen, ipsec_address(&saidx->dst), 200 (u_long) ntohl(sav->spi))); 201 error = ENOBUFS; 202 goto bad; 203 } 204 205 udp = (struct udphdr*) (mtod(mo, char*) + roff); 206 data = (uint64_t*) (udp + 1); 207 208 if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) 209 *data = 0; /* NON-IKE Marker */ 210 211 if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) 212 udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT); 213 else 214 udp->uh_sport = key_portfromsaddr(&saidx->src); 215 216 udp->uh_dport = key_portfromsaddr(&saidx->dst); 217 udp->uh_sum = 0; 218 udp->uh_ulen = htons(m->m_pkthdr.len - (ip->ip_hl << 2)); 219 } 220 221 switch (saidx->dst.sa.sa_family) { 222 #ifdef INET 223 case AF_INET: 224 /* Fix the header length, for AH processing. */ 225 ip = mtod(m, struct ip *); 226 ip->ip_len = htons(m->m_pkthdr.len); 227 if (sav->natt_type != 0) 228 ip->ip_p = IPPROTO_UDP; 229 break; 230 #endif /* INET */ 231 #ifdef INET6 232 case AF_INET6: 233 /* Fix the header length, for AH processing. */ 234 if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) { 235 error = ENXIO; 236 goto bad; 237 } 238 if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) { 239 /* No jumbogram support. */ 240 error = ENXIO; /*?*/ 241 goto bad; 242 } 243 ip6 = mtod(m, struct ip6_hdr *); 244 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 245 if (sav->natt_type != 0) 246 ip6->ip6_nxt = IPPROTO_UDP; 247 break; 248 #endif /* INET6 */ 249 default: 250 DPRINTF(("ipsec_process_done: unknown protocol family %u\n", 251 saidx->dst.sa.sa_family)); 252 error = ENXIO; 253 goto bad; 254 } 255 256 key_sa_recordxfer(sav, m); 257 258 /* 259 * If there's another (bundled) SA to apply, do so. 260 * Note that this puts a burden on the kernel stack size. 261 * If this is a problem we'll need to introduce a queue 262 * to set the packet on so we can unwind the stack before 263 * doing further processing. 264 */ 265 if (isr->next) { 266 IPSEC_STATINC(IPSEC_STAT_OUT_BUNDLESA); 267 switch ( saidx->dst.sa.sa_family ) { 268 #ifdef INET 269 case AF_INET: 270 return ipsec4_process_packet(m, isr->next, 0,0); 271 #endif /* INET */ 272 #ifdef INET6 273 case AF_INET6: 274 return ipsec6_process_packet(m,isr->next); 275 #endif /* INET6 */ 276 default : 277 DPRINTF(("ipsec_process_done: unknown protocol family %u\n", 278 saidx->dst.sa.sa_family)); 279 error = ENXIO; 280 goto bad; 281 } 282 } 283 284 /* 285 * We're done with IPsec processing, 286 * mark that we have already processed the packet 287 * transmit it packet using the appropriate network protocol (IP or IPv6). 288 */ 289 290 if (ipsec_register_done(m, &error) < 0) 291 goto bad; 292 293 return ipsec_reinject_ipstack(m, saidx->dst.sa.sa_family); 294 bad: 295 m_freem(m); 296 KEY_FREESAV(&sav); 297 return (error); 298 } 299 300 /* 301 * ipsec_nextisr can return : 302 * - isr == NULL and error != 0 => something is bad : the packet must be 303 * discarded 304 * - isr == NULL and error == 0 => no more rules to apply, ipsec processing 305 * is done, reinject it in ip stack 306 * - isr != NULL (error == 0) => we need to apply one rule to the packet 307 */ 308 static struct ipsecrequest * 309 ipsec_nextisr( 310 struct mbuf *m, 311 struct ipsecrequest *isr, 312 int af, 313 struct secasindex *saidx, 314 int *error 315 ) 316 { 317 #define IPSEC_OSTAT(x, y, z) \ 318 do { \ 319 switch (isr->saidx.proto) { \ 320 case IPPROTO_ESP: \ 321 ESP_STATINC(x); \ 322 break; \ 323 case IPPROTO_AH: \ 324 AH_STATINC(y); \ 325 break; \ 326 default: \ 327 IPCOMP_STATINC(z); \ 328 break; \ 329 } \ 330 } while (/*CONSTCOND*/0) 331 332 struct secasvar *sav; 333 334 IPSEC_SPLASSERT_SOFTNET("ipsec_nextisr"); 335 IPSEC_ASSERT(af == AF_INET || af == AF_INET6, 336 ("ipsec_nextisr: invalid address family %u", af)); 337 again: 338 /* 339 * Craft SA index to search for proper SA. Note that 340 * we only fillin unspecified SA peers for transport 341 * mode; for tunnel mode they must already be filled in. 342 */ 343 *saidx = isr->saidx; 344 if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) { 345 /* Fillin unspecified SA peers only for transport mode */ 346 if (af == AF_INET) { 347 struct sockaddr_in *sin; 348 struct ip *ip = mtod(m, struct ip *); 349 350 if (saidx->src.sa.sa_len == 0) { 351 sin = &saidx->src.sin; 352 sin->sin_len = sizeof(*sin); 353 sin->sin_family = AF_INET; 354 sin->sin_port = IPSEC_PORT_ANY; 355 sin->sin_addr = ip->ip_src; 356 } 357 if (saidx->dst.sa.sa_len == 0) { 358 sin = &saidx->dst.sin; 359 sin->sin_len = sizeof(*sin); 360 sin->sin_family = AF_INET; 361 sin->sin_port = IPSEC_PORT_ANY; 362 sin->sin_addr = ip->ip_dst; 363 } 364 } else { 365 struct sockaddr_in6 *sin6; 366 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 367 368 if (saidx->src.sin6.sin6_len == 0) { 369 sin6 = (struct sockaddr_in6 *)&saidx->src; 370 sin6->sin6_len = sizeof(*sin6); 371 sin6->sin6_family = AF_INET6; 372 sin6->sin6_port = IPSEC_PORT_ANY; 373 sin6->sin6_addr = ip6->ip6_src; 374 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) { 375 /* fix scope id for comparing SPD */ 376 sin6->sin6_addr.s6_addr16[1] = 0; 377 sin6->sin6_scope_id = 378 ntohs(ip6->ip6_src.s6_addr16[1]); 379 } 380 } 381 if (saidx->dst.sin6.sin6_len == 0) { 382 sin6 = (struct sockaddr_in6 *)&saidx->dst; 383 sin6->sin6_len = sizeof(*sin6); 384 sin6->sin6_family = AF_INET6; 385 sin6->sin6_port = IPSEC_PORT_ANY; 386 sin6->sin6_addr = ip6->ip6_dst; 387 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) { 388 /* fix scope id for comparing SPD */ 389 sin6->sin6_addr.s6_addr16[1] = 0; 390 sin6->sin6_scope_id = 391 ntohs(ip6->ip6_dst.s6_addr16[1]); 392 } 393 } 394 } 395 } 396 397 /* 398 * Lookup SA and validate it. 399 */ 400 *error = key_checkrequest(isr, saidx); 401 if (*error != 0) { 402 /* 403 * IPsec processing is required, but no SA found. 404 * I assume that key_acquire() had been called 405 * to get/establish the SA. Here I discard 406 * this packet because it is responsibility for 407 * upper layer to retransmit the packet. 408 */ 409 IPSEC_STATINC(IPSEC_STAT_OUT_NOSA); 410 goto bad; 411 } 412 sav = isr->sav; 413 /* sav may be NULL here if we have an USE rule */ 414 if (sav == NULL) { 415 IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE, 416 ("ipsec_nextisr: no SA found, but required; level %u", 417 ipsec_get_reqlevel(isr))); 418 isr = isr->next; 419 /* 420 * No more rules to apply, return NULL isr and no error 421 * It can happen when the last rules are USE rules 422 * */ 423 if (isr == NULL) { 424 *error = 0; 425 return isr; 426 } 427 goto again; 428 } 429 430 /* 431 * Check system global policy controls. 432 */ 433 if ((isr->saidx.proto == IPPROTO_ESP && !esp_enable) || 434 (isr->saidx.proto == IPPROTO_AH && !ah_enable) || 435 (isr->saidx.proto == IPPROTO_IPCOMP && !ipcomp_enable)) { 436 DPRINTF(("ipsec_nextisr: IPsec outbound packet dropped due" 437 " to policy (check your sysctls)\n")); 438 IPSEC_OSTAT(ESP_STAT_PDROPS, AH_STAT_PDROPS, 439 IPCOMP_STAT_PDROPS); 440 *error = EHOSTUNREACH; 441 goto bad; 442 } 443 444 /* 445 * Sanity check the SA contents for the caller 446 * before they invoke the xform output method. 447 */ 448 if (sav->tdb_xform == NULL) { 449 DPRINTF(("ipsec_nextisr: no transform for SA\n")); 450 IPSEC_OSTAT(ESP_STAT_NOXFORM, AH_STAT_NOXFORM, 451 IPCOMP_STAT_NOXFORM); 452 *error = EHOSTUNREACH; 453 goto bad; 454 } 455 return isr; 456 bad: 457 IPSEC_ASSERT(*error != 0, ("ipsec_nextisr: error return w/ no error code")); 458 return NULL; 459 #undef IPSEC_OSTAT 460 } 461 462 #ifdef INET 463 /* 464 * IPsec output logic for IPv4. 465 */ 466 int 467 ipsec4_process_packet( 468 struct mbuf *m, 469 struct ipsecrequest *isr, 470 int flags, 471 int tunalready 472 ) 473 { 474 struct secasindex saidx; 475 struct secasvar *sav; 476 struct ip *ip; 477 int s, error, i, off; 478 479 IPSEC_ASSERT(m != NULL, ("ipsec4_process_packet: null mbuf")); 480 IPSEC_ASSERT(isr != NULL, ("ipsec4_process_packet: null isr")); 481 482 s = splsoftnet(); /* insure SA contents don't change */ 483 484 isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error); 485 if (isr == NULL) { 486 if (error != 0) { 487 goto bad; 488 } else { 489 if (ipsec_register_done(m, &error) < 0) 490 goto bad; 491 492 splx(s); 493 return ipsec_reinject_ipstack(m, AF_INET); 494 } 495 } 496 497 sav = isr->sav; 498 if (!tunalready) { 499 union sockaddr_union *dst = &sav->sah->saidx.dst; 500 int setdf; 501 502 /* 503 * Collect IP_DF state from the outer header. 504 */ 505 if (dst->sa.sa_family == AF_INET) { 506 if (m->m_len < sizeof (struct ip) && 507 (m = m_pullup(m, sizeof (struct ip))) == NULL) { 508 error = ENOBUFS; 509 goto bad; 510 } 511 ip = mtod(m, struct ip *); 512 /* Honor system-wide control of how to handle IP_DF */ 513 switch (ip4_ipsec_dfbit) { 514 case 0: /* clear in outer header */ 515 case 1: /* set in outer header */ 516 setdf = ip4_ipsec_dfbit; 517 break; 518 default: /* propagate to outer header */ 519 setdf = ip->ip_off; 520 #ifndef __FreeBSD__ 521 /* On FreeBSD, ip_off and ip_len assumed in host endian. */ 522 setdf = ntohs(setdf); 523 #endif 524 setdf = htons(setdf & IP_DF); 525 break; 526 } 527 } else { 528 ip = NULL; /* keep compiler happy */ 529 setdf = 0; 530 } 531 /* Do the appropriate encapsulation, if necessary */ 532 if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */ 533 dst->sa.sa_family != AF_INET || /* PF mismatch */ 534 #if 0 535 (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */ 536 sav->tdb_xform->xf_type == XF_IP4 || /* ditto */ 537 #endif 538 (dst->sa.sa_family == AF_INET && /* Proxy */ 539 dst->sin.sin_addr.s_addr != INADDR_ANY && 540 dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) { 541 struct mbuf *mp; 542 543 /* Fix IPv4 header checksum and length */ 544 if (m->m_len < sizeof (struct ip) && 545 (m = m_pullup(m, sizeof (struct ip))) == NULL) { 546 error = ENOBUFS; 547 goto bad; 548 } 549 ip = mtod(m, struct ip *); 550 ip->ip_len = htons(m->m_pkthdr.len); 551 ip->ip_sum = 0; 552 ip->ip_sum = in_cksum(m, ip->ip_hl << 2); 553 554 /* Encapsulate the packet */ 555 error = ipip_output(m, isr, &mp, 0, 0); 556 if (mp == NULL && !error) { 557 /* Should never happen. */ 558 DPRINTF(("ipsec4_process_packet: ipip_output " 559 "returns no mbuf and no error!")); 560 error = EFAULT; 561 } 562 if (error) { 563 if (mp) { 564 /* XXX: Should never happen! */ 565 m_freem(mp); 566 } 567 m = NULL; /* ipip_output() already freed it */ 568 goto bad; 569 } 570 m = mp, mp = NULL; 571 /* 572 * ipip_output clears IP_DF in the new header. If 573 * we need to propagate IP_DF from the outer header, 574 * then we have to do it here. 575 * 576 * XXX shouldn't assume what ipip_output does. 577 */ 578 if (dst->sa.sa_family == AF_INET && setdf) { 579 if (m->m_len < sizeof (struct ip) && 580 (m = m_pullup(m, sizeof (struct ip))) == NULL) { 581 error = ENOBUFS; 582 goto bad; 583 } 584 ip = mtod(m, struct ip *); 585 ip->ip_off |= IP_OFF_CONVERT(IP_DF); 586 } 587 } 588 } 589 590 /* 591 * Dispatch to the appropriate IPsec transform logic. The 592 * packet will be returned for transmission after crypto 593 * processing, etc. are completed. For encapsulation we 594 * bypass this call because of the explicit call done above 595 * (necessary to deal with IP_DF handling for IPv4). 596 * 597 * NB: m & sav are ``passed to caller'' who's reponsible for 598 * for reclaiming their resources. 599 */ 600 if (sav->tdb_xform->xf_type != XF_IP4) { 601 union sockaddr_union *dst = &sav->sah->saidx.dst; 602 if (dst->sa.sa_family == AF_INET) { 603 ip = mtod(m, struct ip *); 604 i = ip->ip_hl << 2; 605 off = offsetof(struct ip, ip_p); 606 } else { 607 i = sizeof(struct ip6_hdr); 608 off = offsetof(struct ip6_hdr, ip6_nxt); 609 } 610 error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off); 611 } else { 612 error = ipsec_process_done(m, isr); 613 } 614 splx(s); 615 return error; 616 bad: 617 splx(s); 618 if (m) 619 m_freem(m); 620 return error; 621 } 622 #endif 623 624 #ifdef INET6 625 static void 626 compute_ipsec_pos(struct mbuf *m, int *i, int *off) 627 { 628 int nxt; 629 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr*); 630 struct ip6_ext ip6e; 631 int dstopt = 0; 632 633 *i = sizeof(struct ip6_hdr); 634 *off = offsetof(struct ip6_hdr, ip6_nxt); 635 nxt = ip6->ip6_nxt; 636 637 /* 638 * chase mbuf chain to find the appropriate place to 639 * put AH/ESP/IPcomp header. 640 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload] 641 */ 642 do { 643 switch (nxt) { 644 case IPPROTO_AH: 645 case IPPROTO_ESP: 646 case IPPROTO_IPCOMP: 647 /* 648 * we should not skip security header added 649 * beforehand. 650 */ 651 return; 652 653 case IPPROTO_HOPOPTS: 654 case IPPROTO_DSTOPTS: 655 case IPPROTO_ROUTING: 656 /* 657 * if we see 2nd destination option header, 658 * we should stop there. 659 */ 660 if (nxt == IPPROTO_DSTOPTS && dstopt) 661 return; 662 663 if (nxt == IPPROTO_DSTOPTS) { 664 /* 665 * seen 1st or 2nd destination option. 666 * next time we see one, it must be 2nd. 667 */ 668 dstopt = 1; 669 } else if (nxt == IPPROTO_ROUTING) { 670 /* 671 * if we see destionation option next 672 * time, it must be dest2. 673 */ 674 dstopt = 2; 675 } 676 677 /* skip this header */ 678 m_copydata(m, *i, sizeof(ip6e), &ip6e); 679 nxt = ip6e.ip6e_nxt; 680 *off = *i + offsetof(struct ip6_ext, ip6e_nxt); 681 /* 682 * we will never see nxt == IPPROTO_AH 683 * so it is safe to omit AH case. 684 */ 685 *i += (ip6e.ip6e_len + 1) << 3; 686 break; 687 default: 688 return; 689 } 690 } while (*i < m->m_pkthdr.len); 691 } 692 693 static int 694 in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa, const struct in6_addr *ia) 695 { 696 struct in6_addr ia2; 697 698 memcpy(&ia2, &sa->sin6_addr, sizeof(ia2)); 699 if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr)) 700 ia2.s6_addr16[1] = htons(sa->sin6_scope_id); 701 702 return IN6_ARE_ADDR_EQUAL(ia, &ia2); 703 } 704 705 int 706 ipsec6_process_packet( 707 struct mbuf *m, 708 struct ipsecrequest *isr 709 ) 710 { 711 struct secasindex saidx; 712 struct secasvar *sav; 713 struct ip6_hdr *ip6; 714 int s, error, i, off; 715 union sockaddr_union *dst; 716 717 IPSEC_ASSERT(m != NULL, ("ipsec6_process_packet: null mbuf")); 718 IPSEC_ASSERT(isr != NULL, ("ipsec6_process_packet: null isr")); 719 720 s = splsoftnet(); /* insure SA contents don't change */ 721 722 isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error); 723 if (isr == NULL) { 724 if (error != 0) { 725 /* XXX Should we send a notification ? */ 726 goto bad; 727 } else { 728 if (ipsec_register_done(m, &error) < 0) 729 goto bad; 730 731 splx(s); 732 return ipsec_reinject_ipstack(m, AF_INET6); 733 } 734 } 735 736 sav = isr->sav; 737 dst = &sav->sah->saidx.dst; 738 739 ip6 = mtod(m, struct ip6_hdr *); /* XXX */ 740 741 /* Do the appropriate encapsulation, if necessary */ 742 if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */ 743 dst->sa.sa_family != AF_INET6 || /* PF mismatch */ 744 ((dst->sa.sa_family == AF_INET6) && 745 (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) && 746 (!in6_sa_equal_addrwithscope(&dst->sin6, 747 &ip6->ip6_dst)))) { 748 struct mbuf *mp; 749 750 /* Fix IPv6 header payload length. */ 751 if (m->m_len < sizeof(struct ip6_hdr)) 752 if ((m = m_pullup(m,sizeof(struct ip6_hdr))) == NULL) 753 return ENOBUFS; 754 755 if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) { 756 /* No jumbogram support. */ 757 m_freem(m); 758 return ENXIO; /*XXX*/ 759 } 760 761 ip6 = mtod(m, struct ip6_hdr *); 762 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 763 764 /* Encapsulate the packet */ 765 error = ipip_output(m, isr, &mp, 0, 0); 766 if (mp == NULL && !error) { 767 /* Should never happen. */ 768 DPRINTF(("ipsec6_process_packet: ipip_output " 769 "returns no mbuf and no error!")); 770 error = EFAULT; 771 } 772 773 if (error) { 774 if (mp) { 775 /* XXX: Should never happen! */ 776 m_freem(mp); 777 } 778 m = NULL; /* ipip_output() already freed it */ 779 goto bad; 780 } 781 782 m = mp; 783 mp = NULL; 784 } 785 786 if (dst->sa.sa_family == AF_INET) { 787 struct ip *ip; 788 ip = mtod(m, struct ip *); 789 i = ip->ip_hl << 2; 790 off = offsetof(struct ip, ip_p); 791 } else { 792 compute_ipsec_pos(m, &i, &off); 793 } 794 error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off); 795 splx(s); 796 return error; 797 bad: 798 splx(s); 799 if (m) 800 m_freem(m); 801 return error; 802 } 803 #endif /*INET6*/ 804