ipsec_output.c revision 266800
1112758Ssam/*- 2112758Ssam * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting 3112758Ssam * All rights reserved. 4112758Ssam * 5112758Ssam * Redistribution and use in source and binary forms, with or without 6112758Ssam * modification, are permitted provided that the following conditions 7112758Ssam * are met: 8112758Ssam * 1. Redistributions of source code must retain the above copyright 9112758Ssam * notice, this list of conditions and the following disclaimer. 10112758Ssam * 2. Redistributions in binary form must reproduce the above copyright 11112758Ssam * notice, this list of conditions and the following disclaimer in the 12112758Ssam * documentation and/or other materials provided with the distribution. 13112758Ssam * 14112758Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15112758Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16112758Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17112758Ssam * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18112758Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19112758Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20112758Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21112758Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22112758Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23112758Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24112758Ssam * SUCH DAMAGE. 25112758Ssam * 26112758Ssam * $FreeBSD: head/sys/netipsec/ipsec_output.c 266800 2014-05-28 12:45:27Z vanhu $ 27112758Ssam */ 28105197Ssam 29105197Ssam/* 30105197Ssam * IPsec output processing. 31105197Ssam */ 32105197Ssam#include "opt_inet.h" 33105197Ssam#include "opt_inet6.h" 34105197Ssam#include "opt_ipsec.h" 35159965Sthompsa#include "opt_enc.h" 36105197Ssam 37105197Ssam#include <sys/param.h> 38105197Ssam#include <sys/systm.h> 39105197Ssam#include <sys/mbuf.h> 40105197Ssam#include <sys/domain.h> 41105197Ssam#include <sys/protosw.h> 42105197Ssam#include <sys/socket.h> 43105197Ssam#include <sys/errno.h> 44105197Ssam#include <sys/syslog.h> 45105197Ssam 46105197Ssam#include <net/if.h> 47257176Sglebius#include <net/if_var.h> 48171497Sbz#include <net/pfil.h> 49105197Ssam#include <net/route.h> 50195699Srwatson#include <net/vnet.h> 51105197Ssam 52105197Ssam#include <netinet/in.h> 53105197Ssam#include <netinet/in_systm.h> 54105197Ssam#include <netinet/ip.h> 55105197Ssam#include <netinet/ip_var.h> 56105197Ssam#include <netinet/in_var.h> 57105197Ssam#include <netinet/ip_ecn.h> 58105197Ssam#ifdef INET6 59105197Ssam#include <netinet6/ip6_ecn.h> 60105197Ssam#endif 61105197Ssam 62105197Ssam#include <netinet/ip6.h> 63105197Ssam#ifdef INET6 64105197Ssam#include <netinet6/ip6_var.h> 65105197Ssam#endif 66105197Ssam#include <netinet/in_pcb.h> 67105197Ssam#ifdef INET6 68105197Ssam#include <netinet/icmp6.h> 69105197Ssam#endif 70105197Ssam 71105197Ssam#include <netipsec/ipsec.h> 72105197Ssam#ifdef INET6 73105197Ssam#include <netipsec/ipsec6.h> 74105197Ssam#endif 75105197Ssam#include <netipsec/ah_var.h> 76105197Ssam#include <netipsec/esp_var.h> 77105197Ssam#include <netipsec/ipcomp_var.h> 78105197Ssam 79105197Ssam#include <netipsec/xform.h> 80105197Ssam 81105197Ssam#include <netipsec/key.h> 82105197Ssam#include <netipsec/keydb.h> 83105197Ssam#include <netipsec/key_debug.h> 84105197Ssam 85105197Ssam#include <machine/in_cksum.h> 86105197Ssam 87194062Svanhu#ifdef IPSEC_NAT_T 88194062Svanhu#include <netinet/udp.h> 89194062Svanhu#endif 90194062Svanhu 91181627Svanhu#ifdef DEV_ENC 92181627Svanhu#include <net/if_enc.h> 93181627Svanhu#endif 94181627Svanhu 95181627Svanhu 96105197Ssamint 97105197Ssamipsec_process_done(struct mbuf *m, struct ipsecrequest *isr) 98105197Ssam{ 99105197Ssam struct tdb_ident *tdbi; 100105197Ssam struct m_tag *mtag; 101105197Ssam struct secasvar *sav; 102105197Ssam struct secasindex *saidx; 103105197Ssam int error; 104105197Ssam 105120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 106120585Ssam IPSEC_ASSERT(isr != NULL, ("null ISR")); 107105197Ssam sav = isr->sav; 108120585Ssam IPSEC_ASSERT(sav != NULL, ("null SA")); 109120585Ssam IPSEC_ASSERT(sav->sah != NULL, ("null SAH")); 110105197Ssam 111105197Ssam saidx = &sav->sah->saidx; 112105197Ssam switch (saidx->dst.sa.sa_family) { 113105197Ssam#ifdef INET 114105197Ssam case AF_INET: 115105197Ssam /* Fix the header length, for AH processing. */ 116105197Ssam mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len); 117105197Ssam break; 118105197Ssam#endif /* INET */ 119105197Ssam#ifdef INET6 120105197Ssam case AF_INET6: 121105197Ssam /* Fix the header length, for AH processing. */ 122105197Ssam if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) { 123105197Ssam error = ENXIO; 124105197Ssam goto bad; 125105197Ssam } 126105197Ssam if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) { 127105197Ssam /* No jumbogram support. */ 128105197Ssam error = ENXIO; /*?*/ 129105197Ssam goto bad; 130105197Ssam } 131105197Ssam mtod(m, struct ip6_hdr *)->ip6_plen = 132105197Ssam htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 133105197Ssam break; 134105197Ssam#endif /* INET6 */ 135105197Ssam default: 136120585Ssam DPRINTF(("%s: unknown protocol family %u\n", __func__, 137105197Ssam saidx->dst.sa.sa_family)); 138105197Ssam error = ENXIO; 139105197Ssam goto bad; 140105197Ssam } 141105197Ssam 142105197Ssam /* 143105197Ssam * Add a record of what we've done or what needs to be done to the 144105197Ssam * packet. 145105197Ssam */ 146105197Ssam mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, 147105197Ssam sizeof(struct tdb_ident), M_NOWAIT); 148105197Ssam if (mtag == NULL) { 149120585Ssam DPRINTF(("%s: could not get packet tag\n", __func__)); 150105197Ssam error = ENOMEM; 151105197Ssam goto bad; 152105197Ssam } 153105197Ssam 154105197Ssam tdbi = (struct tdb_ident *)(mtag + 1); 155105197Ssam tdbi->dst = saidx->dst; 156105197Ssam tdbi->proto = saidx->proto; 157105197Ssam tdbi->spi = sav->spi; 158105197Ssam m_tag_prepend(m, mtag); 159105197Ssam 160105197Ssam /* 161105197Ssam * If there's another (bundled) SA to apply, do so. 162105197Ssam * Note that this puts a burden on the kernel stack size. 163105197Ssam * If this is a problem we'll need to introduce a queue 164105197Ssam * to set the packet on so we can unwind the stack before 165105197Ssam * doing further processing. 166105197Ssam */ 167105197Ssam if (isr->next) { 168252026Sae IPSECSTAT_INC(ips_out_bundlesa); 169238700Sbz /* XXX-BZ currently only support same AF bundles. */ 170221129Sbz switch (saidx->dst.sa.sa_family) { 171221129Sbz#ifdef INET 172221129Sbz case AF_INET: 173221129Sbz return ipsec4_process_packet(m, isr->next, 0, 0); 174221129Sbz /* NOTREACHED */ 175221129Sbz#endif 176221129Sbz#ifdef notyet 177221129Sbz#ifdef INET6 178221129Sbz case AF_INET6: 179221129Sbz /* XXX */ 180266800Svanhu return ipsec6_process_packet(m, isr->next); 181221129Sbz /* NOTREACHED */ 182221129Sbz#endif /* INET6 */ 183221129Sbz#endif 184221129Sbz default: 185221129Sbz DPRINTF(("%s: unknown protocol family %u\n", __func__, 186221129Sbz saidx->dst.sa.sa_family)); 187221129Sbz error = ENXIO; 188221129Sbz goto bad; 189221129Sbz } 190105197Ssam } 191117056Ssam key_sa_recordxfer(sav, m); /* record data transfer */ 192105197Ssam 193105197Ssam /* 194105197Ssam * We're done with IPsec processing, transmit the packet using the 195105197Ssam * appropriate network protocol (IP or IPv6). SPD lookup will be 196105197Ssam * performed again there. 197105197Ssam */ 198105197Ssam switch (saidx->dst.sa.sa_family) { 199105197Ssam#ifdef INET 200105197Ssam case AF_INET: 201194062Svanhu#ifdef IPSEC_NAT_T 202194062Svanhu /* 203194062Svanhu * If NAT-T is enabled, now that all IPsec processing is done 204194062Svanhu * insert UDP encapsulation header after IP header. 205194062Svanhu */ 206194062Svanhu if (sav->natt_type) { 207241919Sglebius struct ip *ip = mtod(m, struct ip *); 208194062Svanhu const int hlen = (ip->ip_hl << 2); 209194062Svanhu int size, off; 210194062Svanhu struct mbuf *mi; 211194062Svanhu struct udphdr *udp; 212194062Svanhu 213194062Svanhu size = sizeof(struct udphdr); 214194062Svanhu if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) { 215194062Svanhu /* 216194062Svanhu * draft-ietf-ipsec-nat-t-ike-0[01].txt and 217194062Svanhu * draft-ietf-ipsec-udp-encaps-(00/)01.txt, 218194062Svanhu * ignoring possible AH mode 219194062Svanhu * non-IKE marker + non-ESP marker 220194062Svanhu * from draft-ietf-ipsec-udp-encaps-00.txt. 221194062Svanhu */ 222194062Svanhu size += sizeof(u_int64_t); 223194062Svanhu } 224194062Svanhu mi = m_makespace(m, hlen, size, &off); 225194062Svanhu if (mi == NULL) { 226194062Svanhu DPRINTF(("%s: m_makespace for udphdr failed\n", 227194062Svanhu __func__)); 228194062Svanhu error = ENOBUFS; 229194062Svanhu goto bad; 230194062Svanhu } 231194062Svanhu 232194062Svanhu udp = (struct udphdr *)(mtod(mi, caddr_t) + off); 233194062Svanhu if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) 234194062Svanhu udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT); 235194062Svanhu else 236194062Svanhu udp->uh_sport = 237194062Svanhu KEY_PORTFROMSADDR(&sav->sah->saidx.src); 238194062Svanhu udp->uh_dport = KEY_PORTFROMSADDR(&sav->sah->saidx.dst); 239194062Svanhu udp->uh_sum = 0; 240194062Svanhu udp->uh_ulen = htons(m->m_pkthdr.len - hlen); 241241919Sglebius ip->ip_len = htons(m->m_pkthdr.len); 242194062Svanhu ip->ip_p = IPPROTO_UDP; 243194062Svanhu 244194062Svanhu if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) 245194062Svanhu *(u_int64_t *)(udp + 1) = 0; 246194062Svanhu } 247194062Svanhu#endif /* IPSEC_NAT_T */ 248194062Svanhu 249105197Ssam return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL); 250105197Ssam#endif /* INET */ 251105197Ssam#ifdef INET6 252105197Ssam case AF_INET6: 253105197Ssam /* 254105197Ssam * We don't need massage, IPv6 header fields are always in 255105197Ssam * net endian. 256105197Ssam */ 257105197Ssam return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL); 258105197Ssam#endif /* INET6 */ 259105197Ssam } 260105197Ssam panic("ipsec_process_done"); 261105197Ssambad: 262105197Ssam m_freem(m); 263105197Ssam return (error); 264105197Ssam} 265105197Ssam 266105197Ssamstatic struct ipsecrequest * 267105197Ssamipsec_nextisr( 268105197Ssam struct mbuf *m, 269105197Ssam struct ipsecrequest *isr, 270105197Ssam int af, 271105197Ssam struct secasindex *saidx, 272105197Ssam int *error 273105197Ssam) 274105197Ssam{ 275252028Sae#define IPSEC_OSTAT(name) do { \ 276252028Sae if (isr->saidx.proto == IPPROTO_ESP) \ 277252028Sae ESPSTAT_INC(esps_##name); \ 278252028Sae else if (isr->saidx.proto == IPPROTO_AH)\ 279252028Sae AHSTAT_INC(ahs_##name); \ 280252028Sae else \ 281252028Sae IPCOMPSTAT_INC(ipcomps_##name); \ 282252028Sae} while (0) 283105197Ssam struct secasvar *sav; 284105197Ssam 285120585Ssam IPSECREQUEST_LOCK_ASSERT(isr); 286120585Ssam 287120585Ssam IPSEC_ASSERT(af == AF_INET || af == AF_INET6, 288120585Ssam ("invalid address family %u", af)); 289105197Ssamagain: 290105197Ssam /* 291105197Ssam * Craft SA index to search for proper SA. Note that 292105197Ssam * we only fillin unspecified SA peers for transport 293105197Ssam * mode; for tunnel mode they must already be filled in. 294105197Ssam */ 295105197Ssam *saidx = isr->saidx; 296105197Ssam if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) { 297105197Ssam /* Fillin unspecified SA peers only for transport mode */ 298105197Ssam if (af == AF_INET) { 299105197Ssam struct sockaddr_in *sin; 300105197Ssam struct ip *ip = mtod(m, struct ip *); 301105197Ssam 302105197Ssam if (saidx->src.sa.sa_len == 0) { 303105197Ssam sin = &saidx->src.sin; 304105197Ssam sin->sin_len = sizeof(*sin); 305105197Ssam sin->sin_family = AF_INET; 306105197Ssam sin->sin_port = IPSEC_PORT_ANY; 307105197Ssam sin->sin_addr = ip->ip_src; 308105197Ssam } 309105197Ssam if (saidx->dst.sa.sa_len == 0) { 310105197Ssam sin = &saidx->dst.sin; 311105197Ssam sin->sin_len = sizeof(*sin); 312105197Ssam sin->sin_family = AF_INET; 313105197Ssam sin->sin_port = IPSEC_PORT_ANY; 314105197Ssam sin->sin_addr = ip->ip_dst; 315105197Ssam } 316105197Ssam } else { 317105197Ssam struct sockaddr_in6 *sin6; 318105197Ssam struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 319105197Ssam 320105197Ssam if (saidx->src.sin6.sin6_len == 0) { 321105197Ssam sin6 = (struct sockaddr_in6 *)&saidx->src; 322105197Ssam sin6->sin6_len = sizeof(*sin6); 323105197Ssam sin6->sin6_family = AF_INET6; 324105197Ssam sin6->sin6_port = IPSEC_PORT_ANY; 325105197Ssam sin6->sin6_addr = ip6->ip6_src; 326105197Ssam if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) { 327105197Ssam /* fix scope id for comparing SPD */ 328105197Ssam sin6->sin6_addr.s6_addr16[1] = 0; 329105197Ssam sin6->sin6_scope_id = 330105197Ssam ntohs(ip6->ip6_src.s6_addr16[1]); 331105197Ssam } 332105197Ssam } 333105197Ssam if (saidx->dst.sin6.sin6_len == 0) { 334105197Ssam sin6 = (struct sockaddr_in6 *)&saidx->dst; 335105197Ssam sin6->sin6_len = sizeof(*sin6); 336105197Ssam sin6->sin6_family = AF_INET6; 337105197Ssam sin6->sin6_port = IPSEC_PORT_ANY; 338105197Ssam sin6->sin6_addr = ip6->ip6_dst; 339105197Ssam if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) { 340105197Ssam /* fix scope id for comparing SPD */ 341105197Ssam sin6->sin6_addr.s6_addr16[1] = 0; 342105197Ssam sin6->sin6_scope_id = 343105197Ssam ntohs(ip6->ip6_dst.s6_addr16[1]); 344105197Ssam } 345105197Ssam } 346105197Ssam } 347105197Ssam } 348105197Ssam 349105197Ssam /* 350105197Ssam * Lookup SA and validate it. 351105197Ssam */ 352105197Ssam *error = key_checkrequest(isr, saidx); 353105197Ssam if (*error != 0) { 354105197Ssam /* 355105197Ssam * IPsec processing is required, but no SA found. 356105197Ssam * I assume that key_acquire() had been called 357105197Ssam * to get/establish the SA. Here I discard 358105197Ssam * this packet because it is responsibility for 359105197Ssam * upper layer to retransmit the packet. 360105197Ssam */ 361252026Sae IPSECSTAT_INC(ips_out_nosa); 362105197Ssam goto bad; 363105197Ssam } 364105197Ssam sav = isr->sav; 365177175Sbz if (sav == NULL) { 366120585Ssam IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE, 367120585Ssam ("no SA found, but required; level %u", 368105197Ssam ipsec_get_reqlevel(isr))); 369120585Ssam IPSECREQUEST_UNLOCK(isr); 370105197Ssam isr = isr->next; 371177175Sbz /* 372177175Sbz * If isr is NULL, we found a 'use' policy w/o SA. 373177175Sbz * Return w/o error and w/o isr so we can drop out 374177175Sbz * and continue w/o IPsec processing. 375177175Sbz */ 376177175Sbz if (isr == NULL) 377105197Ssam return isr; 378120585Ssam IPSECREQUEST_LOCK(isr); 379105197Ssam goto again; 380105197Ssam } 381105197Ssam 382105197Ssam /* 383105197Ssam * Check system global policy controls. 384105197Ssam */ 385181803Sbz if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) || 386181803Sbz (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) || 387181803Sbz (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) { 388120585Ssam DPRINTF(("%s: IPsec outbound packet dropped due" 389120585Ssam " to policy (check your sysctls)\n", __func__)); 390252028Sae IPSEC_OSTAT(pdrops); 391105197Ssam *error = EHOSTUNREACH; 392105197Ssam goto bad; 393105197Ssam } 394105197Ssam 395105197Ssam /* 396105197Ssam * Sanity check the SA contents for the caller 397105197Ssam * before they invoke the xform output method. 398105197Ssam */ 399105197Ssam if (sav->tdb_xform == NULL) { 400120585Ssam DPRINTF(("%s: no transform for SA\n", __func__)); 401252028Sae IPSEC_OSTAT(noxform); 402105197Ssam *error = EHOSTUNREACH; 403105197Ssam goto bad; 404105197Ssam } 405105197Ssam return isr; 406105197Ssambad: 407120585Ssam IPSEC_ASSERT(*error != 0, ("error return w/ no error code")); 408120585Ssam IPSECREQUEST_UNLOCK(isr); 409105197Ssam return NULL; 410105197Ssam#undef IPSEC_OSTAT 411105197Ssam} 412105197Ssam 413105197Ssam#ifdef INET 414105197Ssam/* 415105197Ssam * IPsec output logic for IPv4. 416105197Ssam */ 417105197Ssamint 418105197Ssamipsec4_process_packet( 419105197Ssam struct mbuf *m, 420105197Ssam struct ipsecrequest *isr, 421105197Ssam int flags, 422105197Ssam int tunalready) 423105197Ssam{ 424105197Ssam struct secasindex saidx; 425105197Ssam struct secasvar *sav; 426105197Ssam struct ip *ip; 427119643Ssam int error, i, off; 428105197Ssam 429120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 430120585Ssam IPSEC_ASSERT(isr != NULL, ("null isr")); 431105197Ssam 432120585Ssam IPSECREQUEST_LOCK(isr); /* insure SA contents don't change */ 433105197Ssam 434105197Ssam isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error); 435177175Sbz if (isr == NULL) { 436177175Sbz if (error != 0) 437177175Sbz goto bad; 438177175Sbz return EJUSTRETURN; 439177175Sbz } 440105197Ssam 441105197Ssam sav = isr->sav; 442159965Sthompsa 443159965Sthompsa#ifdef DEV_ENC 444181627Svanhu encif->if_opackets++; 445181627Svanhu encif->if_obytes += m->m_pkthdr.len; 446181627Svanhu 447174054Sbz /* pass the mbuf to enc0 for bpf processing */ 448174054Sbz ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_BEFORE); 449159965Sthompsa /* pass the mbuf to enc0 for packet filtering */ 450174054Sbz if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0) 451159965Sthompsa goto bad; 452159965Sthompsa#endif 453159965Sthompsa 454105197Ssam if (!tunalready) { 455105197Ssam union sockaddr_union *dst = &sav->sah->saidx.dst; 456105197Ssam int setdf; 457105197Ssam 458105197Ssam /* 459105197Ssam * Collect IP_DF state from the outer header. 460105197Ssam */ 461105197Ssam if (dst->sa.sa_family == AF_INET) { 462105197Ssam if (m->m_len < sizeof (struct ip) && 463105197Ssam (m = m_pullup(m, sizeof (struct ip))) == NULL) { 464105197Ssam error = ENOBUFS; 465105197Ssam goto bad; 466105197Ssam } 467105197Ssam ip = mtod(m, struct ip *); 468105197Ssam /* Honor system-wide control of how to handle IP_DF */ 469181803Sbz switch (V_ip4_ipsec_dfbit) { 470105197Ssam case 0: /* clear in outer header */ 471105197Ssam case 1: /* set in outer header */ 472181803Sbz setdf = V_ip4_ipsec_dfbit; 473105197Ssam break; 474105197Ssam default: /* propagate to outer header */ 475105197Ssam setdf = ntohs(ip->ip_off & IP_DF); 476105197Ssam break; 477105197Ssam } 478105197Ssam } else { 479105197Ssam ip = NULL; /* keep compiler happy */ 480105197Ssam setdf = 0; 481105197Ssam } 482105197Ssam /* Do the appropriate encapsulation, if necessary */ 483105197Ssam if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */ 484105197Ssam dst->sa.sa_family != AF_INET || /* PF mismatch */ 485105197Ssam#if 0 486105197Ssam (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */ 487105197Ssam sav->tdb_xform->xf_type == XF_IP4 || /* ditto */ 488105197Ssam#endif 489105197Ssam (dst->sa.sa_family == AF_INET && /* Proxy */ 490105197Ssam dst->sin.sin_addr.s_addr != INADDR_ANY && 491105197Ssam dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) { 492105197Ssam struct mbuf *mp; 493105197Ssam 494105197Ssam /* Fix IPv4 header checksum and length */ 495105197Ssam if (m->m_len < sizeof (struct ip) && 496105197Ssam (m = m_pullup(m, sizeof (struct ip))) == NULL) { 497105197Ssam error = ENOBUFS; 498105197Ssam goto bad; 499105197Ssam } 500105197Ssam ip = mtod(m, struct ip *); 501105197Ssam ip->ip_len = htons(m->m_pkthdr.len); 502105197Ssam ip->ip_sum = 0; 503105197Ssam ip->ip_sum = in_cksum(m, ip->ip_hl << 2); 504105197Ssam 505105197Ssam /* Encapsulate the packet */ 506105197Ssam error = ipip_output(m, isr, &mp, 0, 0); 507105197Ssam if (mp == NULL && !error) { 508105197Ssam /* Should never happen. */ 509120585Ssam DPRINTF(("%s: ipip_output returns no mbuf and " 510120585Ssam "no error!", __func__)); 511105197Ssam error = EFAULT; 512105197Ssam } 513105197Ssam if (error) { 514124765Ssam if (mp) { 515124765Ssam /* XXX: Should never happen! */ 516105197Ssam m_freem(mp); 517124765Ssam } 518124765Ssam m = NULL; /* ipip_output() already freed it */ 519105197Ssam goto bad; 520105197Ssam } 521105197Ssam m = mp, mp = NULL; 522105197Ssam /* 523105197Ssam * ipip_output clears IP_DF in the new header. If 524105197Ssam * we need to propagate IP_DF from the outer header, 525105197Ssam * then we have to do it here. 526105197Ssam * 527105197Ssam * XXX shouldn't assume what ipip_output does. 528105197Ssam */ 529105197Ssam if (dst->sa.sa_family == AF_INET && setdf) { 530105197Ssam if (m->m_len < sizeof (struct ip) && 531105197Ssam (m = m_pullup(m, sizeof (struct ip))) == NULL) { 532105197Ssam error = ENOBUFS; 533105197Ssam goto bad; 534105197Ssam } 535105197Ssam ip = mtod(m, struct ip *); 536105197Ssam ip->ip_off = ntohs(ip->ip_off); 537105197Ssam ip->ip_off |= IP_DF; 538105197Ssam ip->ip_off = htons(ip->ip_off); 539105197Ssam } 540105197Ssam } 541105197Ssam } 542105197Ssam 543159965Sthompsa#ifdef DEV_ENC 544159965Sthompsa /* pass the mbuf to enc0 for bpf processing */ 545266800Svanhu ipsec_bpf(m, sav, sav->sah->saidx.dst.sa.sa_family, ENC_OUT|ENC_AFTER); 546174054Sbz /* pass the mbuf to enc0 for packet filtering */ 547174054Sbz if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0) 548174054Sbz goto bad; 549159965Sthompsa#endif 550159965Sthompsa 551105197Ssam /* 552105197Ssam * Dispatch to the appropriate IPsec transform logic. The 553105197Ssam * packet will be returned for transmission after crypto 554105197Ssam * processing, etc. are completed. For encapsulation we 555105197Ssam * bypass this call because of the explicit call done above 556105197Ssam * (necessary to deal with IP_DF handling for IPv4). 557105197Ssam * 558105197Ssam * NB: m & sav are ``passed to caller'' who's reponsible for 559105197Ssam * for reclaiming their resources. 560105197Ssam */ 561105197Ssam if (sav->tdb_xform->xf_type != XF_IP4) { 562266800Svanhu union sockaddr_union *dst = &sav->sah->saidx.dst; 563266800Svanhu switch(dst->sa.sa_family) { 564266800Svanhu case AF_INET: 565266800Svanhu ip = mtod(m, struct ip *); 566266800Svanhu i = ip->ip_hl << 2; 567266800Svanhu off = offsetof(struct ip, ip_p); 568266800Svanhu break; 569266800Svanhu#ifdef INET6 570266800Svanhu case AF_INET6: 571266800Svanhu i = sizeof(struct ip6_hdr); 572266800Svanhu off = offsetof(struct ip6_hdr, ip6_nxt); 573266800Svanhu break; 574266800Svanhu#endif /* INET6 */ 575266800Svanhu default: 576266800Svanhu DPRINTF(("%s: unsupported protocol family %u\n", 577266800Svanhu __func__, dst->sa.sa_family)); 578266800Svanhu error = EPFNOSUPPORT; 579266800Svanhu IPSEC6STAT_INC(ips_out_inval); 580266800Svanhu goto bad; 581266800Svanhu } 582105197Ssam error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off); 583105197Ssam } else { 584105197Ssam error = ipsec_process_done(m, isr); 585105197Ssam } 586120585Ssam IPSECREQUEST_UNLOCK(isr); 587105197Ssam return error; 588105197Ssambad: 589120585Ssam if (isr) 590120585Ssam IPSECREQUEST_UNLOCK(isr); 591105197Ssam if (m) 592105197Ssam m_freem(m); 593105197Ssam return error; 594105197Ssam} 595105197Ssam#endif 596105197Ssam 597266800Svanhu 598105197Ssam#ifdef INET6 599105197Ssamstatic int 600266800Svanhuin6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa, const struct in6_addr *ia) 601105197Ssam{ 602266800Svanhu struct in6_addr ia2; 603105197Ssam 604266800Svanhu memcpy(&ia2, &sa->sin6_addr, sizeof(ia2)); 605266800Svanhu if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr)) 606266800Svanhu ia2.s6_addr16[1] = htons(sa->sin6_scope_id); 607105197Ssam 608266800Svanhu return IN6_ARE_ADDR_EQUAL(ia, &ia2); 609105197Ssam} 610105197Ssam 611105197Ssam/* 612266800Svanhu * IPsec output logic for IPv6. 613105197Ssam */ 614105197Ssamint 615266800Svanhuipsec6_process_packet( 616266800Svanhu struct mbuf *m, 617266800Svanhu struct ipsecrequest *isr 618266800Svanhu ) 619105197Ssam{ 620266800Svanhu struct secasindex saidx; 621266800Svanhu struct secasvar *sav; 622105197Ssam struct ip6_hdr *ip6; 623266800Svanhu int error, i, off; 624266800Svanhu union sockaddr_union *dst; 625105197Ssam 626266800Svanhu IPSEC_ASSERT(m != NULL, ("ipsec6_process_packet: null mbuf")); 627266800Svanhu IPSEC_ASSERT(isr != NULL, ("ipsec6_process_packet: null isr")); 628105197Ssam 629266800Svanhu IPSECREQUEST_LOCK(isr); /* insure SA contents don't change */ 630105197Ssam 631105197Ssam isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error); 632177175Sbz if (isr == NULL) { 633177175Sbz if (error != 0) 634177175Sbz goto bad; 635266800Svanhu return EJUSTRETURN; 636177175Sbz } 637105197Ssam 638266800Svanhu sav = isr->sav; 639266800Svanhu dst = &sav->sah->saidx.dst; 640266800Svanhu 641174054Sbz#ifdef DEV_ENC 642181627Svanhu encif->if_opackets++; 643181627Svanhu encif->if_obytes += m->m_pkthdr.len; 644181627Svanhu 645174054Sbz /* pass the mbuf to enc0 for bpf processing */ 646174054Sbz ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_BEFORE); 647174054Sbz /* pass the mbuf to enc0 for packet filtering */ 648174054Sbz if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0) 649174054Sbz goto bad; 650266800Svanhu#endif /* DEV_ENC */ 651174054Sbz 652266800Svanhu ip6 = mtod(m, struct ip6_hdr *); /* XXX */ 653266800Svanhu 654266800Svanhu /* Do the appropriate encapsulation, if necessary */ 655266800Svanhu if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */ 656266800Svanhu dst->sa.sa_family != AF_INET6 || /* PF mismatch */ 657266800Svanhu ((dst->sa.sa_family == AF_INET6) && 658266800Svanhu (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) && 659266800Svanhu (!in6_sa_equal_addrwithscope(&dst->sin6, 660266800Svanhu &ip6->ip6_dst)))) { 661266800Svanhu struct mbuf *mp; 662266800Svanhu 663266800Svanhu /* Fix IPv6 header payload length. */ 664266800Svanhu if (m->m_len < sizeof(struct ip6_hdr)) 665266800Svanhu if ((m = m_pullup(m,sizeof(struct ip6_hdr))) == NULL) { 666266800Svanhu error = ENOBUFS; 667266800Svanhu goto bad; 668266800Svanhu } 669266800Svanhu 670266800Svanhu if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) { 671266800Svanhu /* No jumbogram support. */ 672266800Svanhu error = ENXIO; /*XXX*/ 673105197Ssam goto bad; 674105197Ssam } 675105197Ssam 676266800Svanhu ip6 = mtod(m, struct ip6_hdr *); 677266800Svanhu ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 678266800Svanhu 679266800Svanhu /* Encapsulate the packet */ 680266800Svanhu error = ipip_output(m, isr, &mp, 0, 0); 681266800Svanhu if (mp == NULL && !error) { 682266800Svanhu /* Should never happen. */ 683266800Svanhu DPRINTF(("ipsec6_process_packet: ipip_output " 684266800Svanhu "returns no mbuf and no error!")); 685266800Svanhu error = EFAULT; 686105197Ssam goto bad; 687105197Ssam } 688266800Svanhu 689105197Ssam if (error) { 690266800Svanhu if (mp) { 691266800Svanhu /* XXX: Should never happen! */ 692266800Svanhu m_freem(mp); 693266800Svanhu } 694266800Svanhu m = NULL; /* ipip_output() already freed it */ 695105197Ssam goto bad; 696105197Ssam } 697105197Ssam 698266800Svanhu m = mp; 699266800Svanhu mp = NULL; 700105197Ssam } 701105197Ssam 702174054Sbz#ifdef DEV_ENC 703266800Svanhu ipsec_bpf(m, isr->sav, dst->sa.sa_family, ENC_OUT|ENC_AFTER); 704174054Sbz /* pass the mbuf to enc0 for packet filtering */ 705174054Sbz if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0) 706174054Sbz goto bad; 707266800Svanhu#endif /* DEV_ENC */ 708174054Sbz 709266800Svanhu switch(dst->sa.sa_family) { 710266800Svanhu#ifdef INET 711266800Svanhu case AF_INET: 712266800Svanhu { 713266800Svanhu struct ip *ip; 714266800Svanhu ip = mtod(m, struct ip *); 715266800Svanhu i = ip->ip_hl << 2; 716266800Svanhu off = offsetof(struct ip, ip_p); 717266800Svanhu } 718266800Svanhu break; 719266800Svanhu#endif /* AF_INET */ 720266800Svanhu case AF_INET6: 721266800Svanhu i = sizeof(struct ip6_hdr); 722266800Svanhu off = offsetof(struct ip6_hdr, ip6_nxt); 723266800Svanhu break; 724266800Svanhu default: 725266800Svanhu DPRINTF(("%s: unsupported protocol family %u\n", 726266800Svanhu __func__, dst->sa.sa_family)); 727266800Svanhu error = EPFNOSUPPORT; 728266800Svanhu IPSEC6STAT_INC(ips_out_inval); 729266800Svanhu goto bad; 730266800Svanhu } 731266800Svanhu error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off); 732170123Sbz IPSECREQUEST_UNLOCK(isr); 733170123Sbz return error; 734105197Ssambad: 735266800Svanhu 736170123Sbz if (isr) 737170123Sbz IPSECREQUEST_UNLOCK(isr); 738105197Ssam if (m) 739105197Ssam m_freem(m); 740105197Ssam return error; 741105197Ssam} 742266800Svanhu#endif /*INET6*/