ipsec_output.c revision 281693
1/*- 2 * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/sys/netipsec/ipsec_output.c 281693 2015-04-18 16:46:31Z ae $ 27 */ 28 29/* 30 * IPsec output processing. 31 */ 32#include "opt_inet.h" 33#include "opt_inet6.h" 34#include "opt_ipsec.h" 35#include "opt_enc.h" 36 37#include <sys/param.h> 38#include <sys/systm.h> 39#include <sys/mbuf.h> 40#include <sys/domain.h> 41#include <sys/protosw.h> 42#include <sys/socket.h> 43#include <sys/errno.h> 44#include <sys/syslog.h> 45 46#include <net/if.h> 47#include <net/if_var.h> 48#include <net/pfil.h> 49#include <net/vnet.h> 50 51#include <netinet/in.h> 52#include <netinet/in_systm.h> 53#include <netinet/ip.h> 54#include <netinet/ip_var.h> 55#include <netinet/in_var.h> 56#include <netinet/ip_ecn.h> 57#ifdef INET6 58#include <netinet6/ip6_ecn.h> 59#endif 60 61#include <netinet/ip6.h> 62#ifdef INET6 63#include <netinet6/ip6_var.h> 64#include <netinet6/scope6_var.h> 65#endif 66#include <netinet/in_pcb.h> 67#ifdef INET6 68#include <netinet/icmp6.h> 69#endif 70 71#include <netipsec/ipsec.h> 72#ifdef INET6 73#include <netipsec/ipsec6.h> 74#endif 75#include <netipsec/ah_var.h> 76#include <netipsec/esp_var.h> 77#include <netipsec/ipcomp_var.h> 78 79#include <netipsec/xform.h> 80 81#include <netipsec/key.h> 82#include <netipsec/keydb.h> 83#include <netipsec/key_debug.h> 84 85#include <machine/in_cksum.h> 86 87#ifdef IPSEC_NAT_T 88#include <netinet/udp.h> 89#endif 90 91#ifdef DEV_ENC 92#include <net/if_enc.h> 93#endif 94 95 96int 97ipsec_process_done(struct mbuf *m, struct ipsecrequest *isr) 98{ 99 struct tdb_ident *tdbi; 100 struct m_tag *mtag; 101 struct secasvar *sav; 102 struct secasindex *saidx; 103 int error; 104 105 IPSEC_ASSERT(m != NULL, ("null mbuf")); 106 IPSEC_ASSERT(isr != NULL, ("null ISR")); 107 sav = isr->sav; 108 IPSEC_ASSERT(sav != NULL, ("null SA")); 109 IPSEC_ASSERT(sav->sah != NULL, ("null SAH")); 110 111 saidx = &sav->sah->saidx; 112 switch (saidx->dst.sa.sa_family) { 113#ifdef INET 114 case AF_INET: 115 /* Fix the header length, for AH processing. */ 116 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len); 117 break; 118#endif /* INET */ 119#ifdef INET6 120 case AF_INET6: 121 /* Fix the header length, for AH processing. */ 122 if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) { 123 error = ENXIO; 124 goto bad; 125 } 126 if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) { 127 /* No jumbogram support. */ 128 error = ENXIO; /*?*/ 129 goto bad; 130 } 131 mtod(m, struct ip6_hdr *)->ip6_plen = 132 htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 133 break; 134#endif /* INET6 */ 135 default: 136 DPRINTF(("%s: unknown protocol family %u\n", __func__, 137 saidx->dst.sa.sa_family)); 138 error = ENXIO; 139 goto bad; 140 } 141 142 /* 143 * Add a record of what we've done or what needs to be done to the 144 * packet. 145 */ 146 mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, 147 sizeof(struct tdb_ident), M_NOWAIT); 148 if (mtag == NULL) { 149 DPRINTF(("%s: could not get packet tag\n", __func__)); 150 error = ENOMEM; 151 goto bad; 152 } 153 154 tdbi = (struct tdb_ident *)(mtag + 1); 155 tdbi->dst = saidx->dst; 156 tdbi->proto = saidx->proto; 157 tdbi->spi = sav->spi; 158 m_tag_prepend(m, mtag); 159 160 /* 161 * If there's another (bundled) SA to apply, do so. 162 * Note that this puts a burden on the kernel stack size. 163 * If this is a problem we'll need to introduce a queue 164 * to set the packet on so we can unwind the stack before 165 * doing further processing. 166 */ 167 if (isr->next) { 168 /* XXX-BZ currently only support same AF bundles. */ 169 switch (saidx->dst.sa.sa_family) { 170#ifdef INET 171 case AF_INET: 172 IPSECSTAT_INC(ips_out_bundlesa); 173 return ipsec4_process_packet(m, isr->next); 174 /* NOTREACHED */ 175#endif 176#ifdef notyet 177#ifdef INET6 178 case AF_INET6: 179 /* XXX */ 180 IPSEC6STAT_INC(ips_out_bundlesa); 181 return ipsec6_process_packet(m, isr->next); 182 /* NOTREACHED */ 183#endif /* INET6 */ 184#endif 185 default: 186 DPRINTF(("%s: unknown protocol family %u\n", __func__, 187 saidx->dst.sa.sa_family)); 188 error = ENXIO; 189 goto bad; 190 } 191 } 192 key_sa_recordxfer(sav, m); /* record data transfer */ 193 194 /* 195 * We're done with IPsec processing, transmit the packet using the 196 * appropriate network protocol (IP or IPv6). SPD lookup will be 197 * performed again there. 198 */ 199 switch (saidx->dst.sa.sa_family) { 200#ifdef INET 201 case AF_INET: 202#ifdef IPSEC_NAT_T 203 /* 204 * If NAT-T is enabled, now that all IPsec processing is done 205 * insert UDP encapsulation header after IP header. 206 */ 207 if (sav->natt_type) { 208 struct ip *ip = mtod(m, struct ip *); 209 const int hlen = (ip->ip_hl << 2); 210 int size, off; 211 struct mbuf *mi; 212 struct udphdr *udp; 213 214 size = sizeof(struct udphdr); 215 if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) { 216 /* 217 * draft-ietf-ipsec-nat-t-ike-0[01].txt and 218 * draft-ietf-ipsec-udp-encaps-(00/)01.txt, 219 * ignoring possible AH mode 220 * non-IKE marker + non-ESP marker 221 * from draft-ietf-ipsec-udp-encaps-00.txt. 222 */ 223 size += sizeof(u_int64_t); 224 } 225 mi = m_makespace(m, hlen, size, &off); 226 if (mi == NULL) { 227 DPRINTF(("%s: m_makespace for udphdr failed\n", 228 __func__)); 229 error = ENOBUFS; 230 goto bad; 231 } 232 233 udp = (struct udphdr *)(mtod(mi, caddr_t) + off); 234 if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) 235 udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT); 236 else 237 udp->uh_sport = 238 KEY_PORTFROMSADDR(&sav->sah->saidx.src); 239 udp->uh_dport = KEY_PORTFROMSADDR(&sav->sah->saidx.dst); 240 udp->uh_sum = 0; 241 udp->uh_ulen = htons(m->m_pkthdr.len - hlen); 242 ip->ip_len = htons(m->m_pkthdr.len); 243 ip->ip_p = IPPROTO_UDP; 244 245 if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) 246 *(u_int64_t *)(udp + 1) = 0; 247 } 248#endif /* IPSEC_NAT_T */ 249 250 return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL); 251#endif /* INET */ 252#ifdef INET6 253 case AF_INET6: 254 /* 255 * We don't need massage, IPv6 header fields are always in 256 * net endian. 257 */ 258 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL); 259#endif /* INET6 */ 260 } 261 panic("ipsec_process_done"); 262bad: 263 m_freem(m); 264 return (error); 265} 266 267static struct ipsecrequest * 268ipsec_nextisr( 269 struct mbuf *m, 270 struct ipsecrequest *isr, 271 int af, 272 struct secasindex *saidx, 273 int *error 274) 275{ 276#define IPSEC_OSTAT(name) do { \ 277 if (isr->saidx.proto == IPPROTO_ESP) \ 278 ESPSTAT_INC(esps_##name); \ 279 else if (isr->saidx.proto == IPPROTO_AH)\ 280 AHSTAT_INC(ahs_##name); \ 281 else \ 282 IPCOMPSTAT_INC(ipcomps_##name); \ 283} while (0) 284 struct secasvar *sav; 285 286 IPSECREQUEST_LOCK_ASSERT(isr); 287 288 IPSEC_ASSERT(af == AF_INET || af == AF_INET6, 289 ("invalid address family %u", af)); 290again: 291 /* 292 * Craft SA index to search for proper SA. Note that 293 * we only fillin unspecified SA peers for transport 294 * mode; for tunnel mode they must already be filled in. 295 */ 296 *saidx = isr->saidx; 297 if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) { 298 /* Fillin unspecified SA peers only for transport mode */ 299 if (af == AF_INET) { 300 struct sockaddr_in *sin; 301 struct ip *ip = mtod(m, struct ip *); 302 303 if (saidx->src.sa.sa_len == 0) { 304 sin = &saidx->src.sin; 305 sin->sin_len = sizeof(*sin); 306 sin->sin_family = AF_INET; 307 sin->sin_port = IPSEC_PORT_ANY; 308 sin->sin_addr = ip->ip_src; 309 } 310 if (saidx->dst.sa.sa_len == 0) { 311 sin = &saidx->dst.sin; 312 sin->sin_len = sizeof(*sin); 313 sin->sin_family = AF_INET; 314 sin->sin_port = IPSEC_PORT_ANY; 315 sin->sin_addr = ip->ip_dst; 316 } 317 } else { 318 struct sockaddr_in6 *sin6; 319 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 320 321 if (saidx->src.sin6.sin6_len == 0) { 322 sin6 = (struct sockaddr_in6 *)&saidx->src; 323 sin6->sin6_len = sizeof(*sin6); 324 sin6->sin6_family = AF_INET6; 325 sin6->sin6_port = IPSEC_PORT_ANY; 326 sin6->sin6_addr = ip6->ip6_src; 327 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) { 328 /* fix scope id for comparing SPD */ 329 sin6->sin6_addr.s6_addr16[1] = 0; 330 sin6->sin6_scope_id = 331 ntohs(ip6->ip6_src.s6_addr16[1]); 332 } 333 } 334 if (saidx->dst.sin6.sin6_len == 0) { 335 sin6 = (struct sockaddr_in6 *)&saidx->dst; 336 sin6->sin6_len = sizeof(*sin6); 337 sin6->sin6_family = AF_INET6; 338 sin6->sin6_port = IPSEC_PORT_ANY; 339 sin6->sin6_addr = ip6->ip6_dst; 340 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) { 341 /* fix scope id for comparing SPD */ 342 sin6->sin6_addr.s6_addr16[1] = 0; 343 sin6->sin6_scope_id = 344 ntohs(ip6->ip6_dst.s6_addr16[1]); 345 } 346 } 347 } 348 } 349 350 /* 351 * Lookup SA and validate it. 352 */ 353 *error = key_checkrequest(isr, saidx); 354 if (*error != 0) { 355 /* 356 * IPsec processing is required, but no SA found. 357 * I assume that key_acquire() had been called 358 * to get/establish the SA. Here I discard 359 * this packet because it is responsibility for 360 * upper layer to retransmit the packet. 361 */ 362 switch(af) { 363 case AF_INET: 364 IPSECSTAT_INC(ips_out_nosa); 365 break; 366#ifdef INET6 367 case AF_INET6: 368 IPSEC6STAT_INC(ips_out_nosa); 369 break; 370#endif 371 } 372 goto bad; 373 } 374 sav = isr->sav; 375 if (sav == NULL) { 376 IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE, 377 ("no SA found, but required; level %u", 378 ipsec_get_reqlevel(isr))); 379 IPSECREQUEST_UNLOCK(isr); 380 isr = isr->next; 381 /* 382 * If isr is NULL, we found a 'use' policy w/o SA. 383 * Return w/o error and w/o isr so we can drop out 384 * and continue w/o IPsec processing. 385 */ 386 if (isr == NULL) 387 return isr; 388 IPSECREQUEST_LOCK(isr); 389 goto again; 390 } 391 392 /* 393 * Check system global policy controls. 394 */ 395 if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) || 396 (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) || 397 (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) { 398 DPRINTF(("%s: IPsec outbound packet dropped due" 399 " to policy (check your sysctls)\n", __func__)); 400 IPSEC_OSTAT(pdrops); 401 *error = EHOSTUNREACH; 402 goto bad; 403 } 404 405 /* 406 * Sanity check the SA contents for the caller 407 * before they invoke the xform output method. 408 */ 409 if (sav->tdb_xform == NULL) { 410 DPRINTF(("%s: no transform for SA\n", __func__)); 411 IPSEC_OSTAT(noxform); 412 *error = EHOSTUNREACH; 413 goto bad; 414 } 415 return isr; 416bad: 417 IPSEC_ASSERT(*error != 0, ("error return w/ no error code")); 418 IPSECREQUEST_UNLOCK(isr); 419 return NULL; 420#undef IPSEC_OSTAT 421} 422 423static int 424ipsec_encap(struct mbuf **mp, struct secasindex *saidx) 425{ 426#ifdef INET6 427 struct ip6_hdr *ip6; 428#endif 429 struct ip *ip; 430 int setdf; 431 uint8_t itos, proto; 432 433 ip = mtod(*mp, struct ip *); 434 switch (ip->ip_v) { 435#ifdef INET 436 case IPVERSION: 437 proto = IPPROTO_IPIP; 438 /* 439 * Collect IP_DF state from the inner header 440 * and honor system-wide control of how to handle it. 441 */ 442 switch (V_ip4_ipsec_dfbit) { 443 case 0: /* clear in outer header */ 444 case 1: /* set in outer header */ 445 setdf = V_ip4_ipsec_dfbit; 446 break; 447 default:/* propagate to outer header */ 448 setdf = (ip->ip_off & ntohs(IP_DF)) != 0; 449 } 450 itos = ip->ip_tos; 451 break; 452#endif 453#ifdef INET6 454 case (IPV6_VERSION >> 4): 455 proto = IPPROTO_IPV6; 456 ip6 = mtod(*mp, struct ip6_hdr *); 457 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 458 setdf = V_ip4_ipsec_dfbit ? 1: 0; 459 /* scoped address handling */ 460 in6_clearscope(&ip6->ip6_src); 461 in6_clearscope(&ip6->ip6_dst); 462 break; 463#endif 464 default: 465 return (EAFNOSUPPORT); 466 } 467 switch (saidx->dst.sa.sa_family) { 468#ifdef INET 469 case AF_INET: 470 if (saidx->src.sa.sa_family != AF_INET || 471 saidx->src.sin.sin_addr.s_addr == INADDR_ANY || 472 saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) 473 return (EINVAL); 474 M_PREPEND(*mp, sizeof(struct ip), M_NOWAIT); 475 if (*mp == NULL) 476 return (ENOBUFS); 477 ip = mtod(*mp, struct ip *); 478 ip->ip_v = IPVERSION; 479 ip->ip_hl = sizeof(struct ip) >> 2; 480 ip->ip_p = proto; 481 ip->ip_len = htons((*mp)->m_pkthdr.len); 482 ip->ip_ttl = V_ip_defttl; 483 ip->ip_sum = 0; 484 ip->ip_off = setdf ? htons(IP_DF): 0; 485 ip->ip_src = saidx->src.sin.sin_addr; 486 ip->ip_dst = saidx->dst.sin.sin_addr; 487 ip_ecn_ingress(V_ip4_ipsec_ecn, &ip->ip_tos, &itos); 488 ip_fillid(ip); 489 break; 490#endif /* INET */ 491#ifdef INET6 492 case AF_INET6: 493 if (saidx->src.sa.sa_family != AF_INET6 || 494 IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr) || 495 IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr)) 496 return (EINVAL); 497 M_PREPEND(*mp, sizeof(struct ip6_hdr), M_NOWAIT); 498 if (*mp == NULL) 499 return (ENOBUFS); 500 ip6 = mtod(*mp, struct ip6_hdr *); 501 ip6->ip6_flow = 0; 502 ip6->ip6_vfc = IPV6_VERSION; 503 ip6->ip6_hlim = V_ip6_defhlim; 504 ip6->ip6_nxt = proto; 505 ip6->ip6_dst = saidx->dst.sin6.sin6_addr; 506 /* For link-local address embed scope zone id */ 507 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 508 ip6->ip6_dst.s6_addr16[1] = 509 htons(saidx->dst.sin6.sin6_scope_id & 0xffff); 510 ip6->ip6_src = saidx->src.sin6.sin6_addr; 511 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 512 ip6->ip6_src.s6_addr16[1] = 513 htons(saidx->src.sin6.sin6_scope_id & 0xffff); 514 ip6->ip6_plen = htons((*mp)->m_pkthdr.len - sizeof(*ip6)); 515 ip_ecn_ingress(V_ip6_ipsec_ecn, &proto, &itos); 516 ip6->ip6_flow |= htonl((uint32_t)proto << 20); 517 break; 518#endif /* INET6 */ 519 default: 520 return (EAFNOSUPPORT); 521 } 522 return (0); 523} 524 525#ifdef INET 526/* 527 * IPsec output logic for IPv4. 528 */ 529int 530ipsec4_process_packet(struct mbuf *m, struct ipsecrequest *isr) 531{ 532 union sockaddr_union *dst; 533 struct secasindex saidx; 534 struct secasvar *sav; 535 struct ip *ip; 536 int error, i, off; 537 538 IPSEC_ASSERT(m != NULL, ("null mbuf")); 539 IPSEC_ASSERT(isr != NULL, ("null isr")); 540 541 IPSECREQUEST_LOCK(isr); /* insure SA contents don't change */ 542 543 isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error); 544 if (isr == NULL) { 545 if (error != 0) 546 goto bad; 547 return EJUSTRETURN; 548 } 549 550 sav = isr->sav; 551 if (m->m_len < sizeof(struct ip) && 552 (m = m_pullup(m, sizeof (struct ip))) == NULL) { 553 error = ENOBUFS; 554 goto bad; 555 } 556 ip = mtod(m, struct ip *); 557 dst = &sav->sah->saidx.dst; 558#ifdef DEV_ENC 559 if_inc_counter(encif, IFCOUNTER_OPACKETS, 1); 560 if_inc_counter(encif, IFCOUNTER_OBYTES, m->m_pkthdr.len); 561 562 /* pass the mbuf to enc0 for bpf processing */ 563 ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_BEFORE); 564 /* pass the mbuf to enc0 for packet filtering */ 565 if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0) 566 goto bad; 567#endif 568 /* Do the appropriate encapsulation, if necessary */ 569 if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */ 570 dst->sa.sa_family != AF_INET || /* PF mismatch */ 571 (dst->sa.sa_family == AF_INET && /* Proxy */ 572 dst->sin.sin_addr.s_addr != INADDR_ANY && 573 dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) { 574 /* Fix IPv4 header checksum and length */ 575 ip->ip_len = htons(m->m_pkthdr.len); 576 ip->ip_sum = 0; 577 ip->ip_sum = in_cksum(m, ip->ip_hl << 2); 578 error = ipsec_encap(&m, &sav->sah->saidx); 579 if (error != 0) { 580 DPRINTF(("%s: encapsulation for SA %s->%s " 581 "SPI 0x%08x failed with error %d\n", __func__, 582 ipsec_address(&sav->sah->saidx.src), 583 ipsec_address(&sav->sah->saidx.dst), 584 ntohl(sav->spi), error)); 585 goto bad; 586 } 587 } 588#ifdef DEV_ENC 589 /* pass the mbuf to enc0 for bpf processing */ 590 ipsec_bpf(m, sav, sav->sah->saidx.dst.sa.sa_family, ENC_OUT|ENC_AFTER); 591 /* pass the mbuf to enc0 for packet filtering */ 592 if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0) 593 goto bad; 594#endif 595 596 /* 597 * Dispatch to the appropriate IPsec transform logic. The 598 * packet will be returned for transmission after crypto 599 * processing, etc. are completed. 600 * 601 * NB: m & sav are ``passed to caller'' who's reponsible for 602 * for reclaiming their resources. 603 */ 604 switch(dst->sa.sa_family) { 605 case AF_INET: 606 ip = mtod(m, struct ip *); 607 i = ip->ip_hl << 2; 608 off = offsetof(struct ip, ip_p); 609 break; 610#ifdef INET6 611 case AF_INET6: 612 i = sizeof(struct ip6_hdr); 613 off = offsetof(struct ip6_hdr, ip6_nxt); 614 break; 615#endif /* INET6 */ 616 default: 617 DPRINTF(("%s: unsupported protocol family %u\n", 618 __func__, dst->sa.sa_family)); 619 error = EPFNOSUPPORT; 620 IPSECSTAT_INC(ips_out_inval); 621 goto bad; 622 } 623 error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off); 624 IPSECREQUEST_UNLOCK(isr); 625 return (error); 626bad: 627 if (isr) 628 IPSECREQUEST_UNLOCK(isr); 629 if (m) 630 m_freem(m); 631 return error; 632} 633#endif 634 635 636#ifdef INET6 637static int 638in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa, const struct in6_addr *ia) 639{ 640 struct in6_addr ia2; 641 642 memcpy(&ia2, &sa->sin6_addr, sizeof(ia2)); 643 if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr)) 644 ia2.s6_addr16[1] = htons(sa->sin6_scope_id); 645 646 return IN6_ARE_ADDR_EQUAL(ia, &ia2); 647} 648 649/* 650 * IPsec output logic for IPv6. 651 */ 652int 653ipsec6_process_packet( 654 struct mbuf *m, 655 struct ipsecrequest *isr 656 ) 657{ 658 struct secasindex saidx; 659 struct secasvar *sav; 660 struct ip6_hdr *ip6; 661 int error, i, off; 662 union sockaddr_union *dst; 663 664 IPSEC_ASSERT(m != NULL, ("ipsec6_process_packet: null mbuf")); 665 IPSEC_ASSERT(isr != NULL, ("ipsec6_process_packet: null isr")); 666 667 IPSECREQUEST_LOCK(isr); /* insure SA contents don't change */ 668 669 isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error); 670 if (isr == NULL) { 671 if (error != 0) 672 goto bad; 673 return EJUSTRETURN; 674 } 675 sav = isr->sav; 676 dst = &sav->sah->saidx.dst; 677 678 ip6 = mtod(m, struct ip6_hdr *); 679 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 680#ifdef DEV_ENC 681 if_inc_counter(encif, IFCOUNTER_OPACKETS, 1); 682 if_inc_counter(encif, IFCOUNTER_OBYTES, m->m_pkthdr.len); 683 684 /* pass the mbuf to enc0 for bpf processing */ 685 ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_BEFORE); 686 /* pass the mbuf to enc0 for packet filtering */ 687 if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0) 688 goto bad; 689#endif /* DEV_ENC */ 690 691 /* Do the appropriate encapsulation, if necessary */ 692 if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */ 693 dst->sa.sa_family != AF_INET6 || /* PF mismatch */ 694 ((dst->sa.sa_family == AF_INET6) && 695 (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) && 696 (!in6_sa_equal_addrwithscope(&dst->sin6, 697 &ip6->ip6_dst)))) { 698 if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) { 699 /* No jumbogram support. */ 700 error = ENXIO; /*XXX*/ 701 goto bad; 702 } 703 error = ipsec_encap(&m, &sav->sah->saidx); 704 if (error != 0) { 705 DPRINTF(("%s: encapsulation for SA %s->%s " 706 "SPI 0x%08x failed with error %d\n", __func__, 707 ipsec_address(&sav->sah->saidx.src), 708 ipsec_address(&sav->sah->saidx.dst), 709 ntohl(sav->spi), error)); 710 goto bad; 711 } 712 } 713 714#ifdef DEV_ENC 715 ipsec_bpf(m, isr->sav, dst->sa.sa_family, ENC_OUT|ENC_AFTER); 716 /* pass the mbuf to enc0 for packet filtering */ 717 if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0) 718 goto bad; 719#endif /* DEV_ENC */ 720 721 switch(dst->sa.sa_family) { 722#ifdef INET 723 case AF_INET: 724 { 725 struct ip *ip; 726 ip = mtod(m, struct ip *); 727 i = ip->ip_hl << 2; 728 off = offsetof(struct ip, ip_p); 729 } 730 break; 731#endif /* AF_INET */ 732 case AF_INET6: 733 i = sizeof(struct ip6_hdr); 734 off = offsetof(struct ip6_hdr, ip6_nxt); 735 break; 736 default: 737 DPRINTF(("%s: unsupported protocol family %u\n", 738 __func__, dst->sa.sa_family)); 739 error = EPFNOSUPPORT; 740 IPSEC6STAT_INC(ips_out_inval); 741 goto bad; 742 } 743 error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off); 744 IPSECREQUEST_UNLOCK(isr); 745 return error; 746bad: 747 748 if (isr) 749 IPSECREQUEST_UNLOCK(isr); 750 if (m) 751 m_freem(m); 752 return error; 753} 754#endif /*INET6*/ 755