ipsec_output.c revision 238700
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 238700 2012-07-22 17:46:05Z bz $ 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> 47171497Sbz#include <net/pfil.h> 48105197Ssam#include <net/route.h> 49195699Srwatson#include <net/vnet.h> 50105197Ssam 51105197Ssam#include <netinet/in.h> 52105197Ssam#include <netinet/in_systm.h> 53105197Ssam#include <netinet/ip.h> 54105197Ssam#include <netinet/ip_var.h> 55105197Ssam#include <netinet/in_var.h> 56105197Ssam#include <netinet/ip_ecn.h> 57105197Ssam#ifdef INET6 58105197Ssam#include <netinet6/ip6_ecn.h> 59105197Ssam#endif 60105197Ssam 61105197Ssam#include <netinet/ip6.h> 62105197Ssam#ifdef INET6 63105197Ssam#include <netinet6/ip6_var.h> 64105197Ssam#endif 65105197Ssam#include <netinet/in_pcb.h> 66105197Ssam#ifdef INET6 67105197Ssam#include <netinet/icmp6.h> 68105197Ssam#endif 69105197Ssam 70105197Ssam#include <netipsec/ipsec.h> 71105197Ssam#ifdef INET6 72105197Ssam#include <netipsec/ipsec6.h> 73105197Ssam#endif 74105197Ssam#include <netipsec/ah_var.h> 75105197Ssam#include <netipsec/esp_var.h> 76105197Ssam#include <netipsec/ipcomp_var.h> 77105197Ssam 78105197Ssam#include <netipsec/xform.h> 79105197Ssam 80105197Ssam#include <netipsec/key.h> 81105197Ssam#include <netipsec/keydb.h> 82105197Ssam#include <netipsec/key_debug.h> 83105197Ssam 84105197Ssam#include <machine/in_cksum.h> 85105197Ssam 86194062Svanhu#ifdef IPSEC_NAT_T 87194062Svanhu#include <netinet/udp.h> 88194062Svanhu#endif 89194062Svanhu 90181627Svanhu#ifdef DEV_ENC 91181627Svanhu#include <net/if_enc.h> 92181627Svanhu#endif 93181627Svanhu 94181627Svanhu 95105197Ssamint 96105197Ssamipsec_process_done(struct mbuf *m, struct ipsecrequest *isr) 97105197Ssam{ 98105197Ssam struct tdb_ident *tdbi; 99105197Ssam struct m_tag *mtag; 100105197Ssam struct secasvar *sav; 101105197Ssam struct secasindex *saidx; 102105197Ssam int error; 103105197Ssam 104120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 105120585Ssam IPSEC_ASSERT(isr != NULL, ("null ISR")); 106105197Ssam sav = isr->sav; 107120585Ssam IPSEC_ASSERT(sav != NULL, ("null SA")); 108120585Ssam IPSEC_ASSERT(sav->sah != NULL, ("null SAH")); 109105197Ssam 110105197Ssam saidx = &sav->sah->saidx; 111105197Ssam switch (saidx->dst.sa.sa_family) { 112105197Ssam#ifdef INET 113105197Ssam case AF_INET: 114105197Ssam /* Fix the header length, for AH processing. */ 115105197Ssam mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len); 116105197Ssam break; 117105197Ssam#endif /* INET */ 118105197Ssam#ifdef INET6 119105197Ssam case AF_INET6: 120105197Ssam /* Fix the header length, for AH processing. */ 121105197Ssam if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) { 122105197Ssam error = ENXIO; 123105197Ssam goto bad; 124105197Ssam } 125105197Ssam if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) { 126105197Ssam /* No jumbogram support. */ 127105197Ssam error = ENXIO; /*?*/ 128105197Ssam goto bad; 129105197Ssam } 130105197Ssam mtod(m, struct ip6_hdr *)->ip6_plen = 131105197Ssam htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 132105197Ssam break; 133105197Ssam#endif /* INET6 */ 134105197Ssam default: 135120585Ssam DPRINTF(("%s: unknown protocol family %u\n", __func__, 136105197Ssam saidx->dst.sa.sa_family)); 137105197Ssam error = ENXIO; 138105197Ssam goto bad; 139105197Ssam } 140105197Ssam 141105197Ssam /* 142105197Ssam * Add a record of what we've done or what needs to be done to the 143105197Ssam * packet. 144105197Ssam */ 145105197Ssam mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, 146105197Ssam sizeof(struct tdb_ident), M_NOWAIT); 147105197Ssam if (mtag == NULL) { 148120585Ssam DPRINTF(("%s: could not get packet tag\n", __func__)); 149105197Ssam error = ENOMEM; 150105197Ssam goto bad; 151105197Ssam } 152105197Ssam 153105197Ssam tdbi = (struct tdb_ident *)(mtag + 1); 154105197Ssam tdbi->dst = saidx->dst; 155105197Ssam tdbi->proto = saidx->proto; 156105197Ssam tdbi->spi = sav->spi; 157105197Ssam m_tag_prepend(m, mtag); 158105197Ssam 159105197Ssam /* 160105197Ssam * If there's another (bundled) SA to apply, do so. 161105197Ssam * Note that this puts a burden on the kernel stack size. 162105197Ssam * If this is a problem we'll need to introduce a queue 163105197Ssam * to set the packet on so we can unwind the stack before 164105197Ssam * doing further processing. 165105197Ssam */ 166105197Ssam if (isr->next) { 167181803Sbz V_ipsec4stat.ips_out_bundlesa++; 168238700Sbz /* XXX-BZ currently only support same AF bundles. */ 169221129Sbz switch (saidx->dst.sa.sa_family) { 170221129Sbz#ifdef INET 171221129Sbz case AF_INET: 172221129Sbz return ipsec4_process_packet(m, isr->next, 0, 0); 173221129Sbz /* NOTREACHED */ 174221129Sbz#endif 175221129Sbz#ifdef notyet 176221129Sbz#ifdef INET6 177221129Sbz case AF_INET6: 178221129Sbz /* XXX */ 179221129Sbz ipsec6_output_trans() 180221129Sbz ipsec6_output_tunnel() 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 193223637Sbz m_addr_changed(m); 194223637Sbz 195105197Ssam /* 196105197Ssam * We're done with IPsec processing, transmit the packet using the 197105197Ssam * appropriate network protocol (IP or IPv6). SPD lookup will be 198105197Ssam * performed again there. 199105197Ssam */ 200105197Ssam switch (saidx->dst.sa.sa_family) { 201105197Ssam#ifdef INET 202105197Ssam struct ip *ip; 203105197Ssam case AF_INET: 204105197Ssam ip = mtod(m, struct ip *); 205105197Ssam ip->ip_len = ntohs(ip->ip_len); 206105197Ssam ip->ip_off = ntohs(ip->ip_off); 207105197Ssam 208194062Svanhu#ifdef IPSEC_NAT_T 209194062Svanhu /* 210194062Svanhu * If NAT-T is enabled, now that all IPsec processing is done 211194062Svanhu * insert UDP encapsulation header after IP header. 212194062Svanhu */ 213194062Svanhu if (sav->natt_type) { 214194062Svanhu#ifdef _IP_VHL 215194062Svanhu const int hlen = IP_VHL_HL(ip->ip_vhl); 216194062Svanhu#else 217194062Svanhu const int hlen = (ip->ip_hl << 2); 218194062Svanhu#endif 219194062Svanhu int size, off; 220194062Svanhu struct mbuf *mi; 221194062Svanhu struct udphdr *udp; 222194062Svanhu 223194062Svanhu size = sizeof(struct udphdr); 224194062Svanhu if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) { 225194062Svanhu /* 226194062Svanhu * draft-ietf-ipsec-nat-t-ike-0[01].txt and 227194062Svanhu * draft-ietf-ipsec-udp-encaps-(00/)01.txt, 228194062Svanhu * ignoring possible AH mode 229194062Svanhu * non-IKE marker + non-ESP marker 230194062Svanhu * from draft-ietf-ipsec-udp-encaps-00.txt. 231194062Svanhu */ 232194062Svanhu size += sizeof(u_int64_t); 233194062Svanhu } 234194062Svanhu mi = m_makespace(m, hlen, size, &off); 235194062Svanhu if (mi == NULL) { 236194062Svanhu DPRINTF(("%s: m_makespace for udphdr failed\n", 237194062Svanhu __func__)); 238194062Svanhu error = ENOBUFS; 239194062Svanhu goto bad; 240194062Svanhu } 241194062Svanhu 242194062Svanhu udp = (struct udphdr *)(mtod(mi, caddr_t) + off); 243194062Svanhu if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) 244194062Svanhu udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT); 245194062Svanhu else 246194062Svanhu udp->uh_sport = 247194062Svanhu KEY_PORTFROMSADDR(&sav->sah->saidx.src); 248194062Svanhu udp->uh_dport = KEY_PORTFROMSADDR(&sav->sah->saidx.dst); 249194062Svanhu udp->uh_sum = 0; 250194062Svanhu udp->uh_ulen = htons(m->m_pkthdr.len - hlen); 251194062Svanhu ip->ip_len = m->m_pkthdr.len; 252194062Svanhu ip->ip_p = IPPROTO_UDP; 253194062Svanhu 254194062Svanhu if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) 255194062Svanhu *(u_int64_t *)(udp + 1) = 0; 256194062Svanhu } 257194062Svanhu#endif /* IPSEC_NAT_T */ 258194062Svanhu 259105197Ssam return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL); 260105197Ssam#endif /* INET */ 261105197Ssam#ifdef INET6 262105197Ssam case AF_INET6: 263105197Ssam /* 264105197Ssam * We don't need massage, IPv6 header fields are always in 265105197Ssam * net endian. 266105197Ssam */ 267105197Ssam return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL); 268105197Ssam#endif /* INET6 */ 269105197Ssam } 270105197Ssam panic("ipsec_process_done"); 271105197Ssambad: 272105197Ssam m_freem(m); 273105197Ssam return (error); 274105197Ssam} 275105197Ssam 276105197Ssamstatic struct ipsecrequest * 277105197Ssamipsec_nextisr( 278105197Ssam struct mbuf *m, 279105197Ssam struct ipsecrequest *isr, 280105197Ssam int af, 281105197Ssam struct secasindex *saidx, 282105197Ssam int *error 283105197Ssam) 284105197Ssam{ 285105197Ssam#define IPSEC_OSTAT(x,y,z) (isr->saidx.proto == IPPROTO_ESP ? (x)++ : \ 286105197Ssam isr->saidx.proto == IPPROTO_AH ? (y)++ : (z)++) 287105197Ssam struct secasvar *sav; 288105197Ssam 289120585Ssam IPSECREQUEST_LOCK_ASSERT(isr); 290120585Ssam 291120585Ssam IPSEC_ASSERT(af == AF_INET || af == AF_INET6, 292120585Ssam ("invalid address family %u", af)); 293105197Ssamagain: 294105197Ssam /* 295105197Ssam * Craft SA index to search for proper SA. Note that 296105197Ssam * we only fillin unspecified SA peers for transport 297105197Ssam * mode; for tunnel mode they must already be filled in. 298105197Ssam */ 299105197Ssam *saidx = isr->saidx; 300105197Ssam if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) { 301105197Ssam /* Fillin unspecified SA peers only for transport mode */ 302105197Ssam if (af == AF_INET) { 303105197Ssam struct sockaddr_in *sin; 304105197Ssam struct ip *ip = mtod(m, struct ip *); 305105197Ssam 306105197Ssam if (saidx->src.sa.sa_len == 0) { 307105197Ssam sin = &saidx->src.sin; 308105197Ssam sin->sin_len = sizeof(*sin); 309105197Ssam sin->sin_family = AF_INET; 310105197Ssam sin->sin_port = IPSEC_PORT_ANY; 311105197Ssam sin->sin_addr = ip->ip_src; 312105197Ssam } 313105197Ssam if (saidx->dst.sa.sa_len == 0) { 314105197Ssam sin = &saidx->dst.sin; 315105197Ssam sin->sin_len = sizeof(*sin); 316105197Ssam sin->sin_family = AF_INET; 317105197Ssam sin->sin_port = IPSEC_PORT_ANY; 318105197Ssam sin->sin_addr = ip->ip_dst; 319105197Ssam } 320105197Ssam } else { 321105197Ssam struct sockaddr_in6 *sin6; 322105197Ssam struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 323105197Ssam 324105197Ssam if (saidx->src.sin6.sin6_len == 0) { 325105197Ssam sin6 = (struct sockaddr_in6 *)&saidx->src; 326105197Ssam sin6->sin6_len = sizeof(*sin6); 327105197Ssam sin6->sin6_family = AF_INET6; 328105197Ssam sin6->sin6_port = IPSEC_PORT_ANY; 329105197Ssam sin6->sin6_addr = ip6->ip6_src; 330105197Ssam if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) { 331105197Ssam /* fix scope id for comparing SPD */ 332105197Ssam sin6->sin6_addr.s6_addr16[1] = 0; 333105197Ssam sin6->sin6_scope_id = 334105197Ssam ntohs(ip6->ip6_src.s6_addr16[1]); 335105197Ssam } 336105197Ssam } 337105197Ssam if (saidx->dst.sin6.sin6_len == 0) { 338105197Ssam sin6 = (struct sockaddr_in6 *)&saidx->dst; 339105197Ssam sin6->sin6_len = sizeof(*sin6); 340105197Ssam sin6->sin6_family = AF_INET6; 341105197Ssam sin6->sin6_port = IPSEC_PORT_ANY; 342105197Ssam sin6->sin6_addr = ip6->ip6_dst; 343105197Ssam if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) { 344105197Ssam /* fix scope id for comparing SPD */ 345105197Ssam sin6->sin6_addr.s6_addr16[1] = 0; 346105197Ssam sin6->sin6_scope_id = 347105197Ssam ntohs(ip6->ip6_dst.s6_addr16[1]); 348105197Ssam } 349105197Ssam } 350105197Ssam } 351105197Ssam } 352105197Ssam 353105197Ssam /* 354105197Ssam * Lookup SA and validate it. 355105197Ssam */ 356105197Ssam *error = key_checkrequest(isr, saidx); 357105197Ssam if (*error != 0) { 358105197Ssam /* 359105197Ssam * IPsec processing is required, but no SA found. 360105197Ssam * I assume that key_acquire() had been called 361105197Ssam * to get/establish the SA. Here I discard 362105197Ssam * this packet because it is responsibility for 363105197Ssam * upper layer to retransmit the packet. 364105197Ssam */ 365181803Sbz V_ipsec4stat.ips_out_nosa++; 366105197Ssam goto bad; 367105197Ssam } 368105197Ssam sav = isr->sav; 369177175Sbz if (sav == NULL) { 370120585Ssam IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE, 371120585Ssam ("no SA found, but required; level %u", 372105197Ssam ipsec_get_reqlevel(isr))); 373120585Ssam IPSECREQUEST_UNLOCK(isr); 374105197Ssam isr = isr->next; 375177175Sbz /* 376177175Sbz * If isr is NULL, we found a 'use' policy w/o SA. 377177175Sbz * Return w/o error and w/o isr so we can drop out 378177175Sbz * and continue w/o IPsec processing. 379177175Sbz */ 380177175Sbz if (isr == NULL) 381105197Ssam return isr; 382120585Ssam IPSECREQUEST_LOCK(isr); 383105197Ssam goto again; 384105197Ssam } 385105197Ssam 386105197Ssam /* 387105197Ssam * Check system global policy controls. 388105197Ssam */ 389181803Sbz if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) || 390181803Sbz (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) || 391181803Sbz (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) { 392120585Ssam DPRINTF(("%s: IPsec outbound packet dropped due" 393120585Ssam " to policy (check your sysctls)\n", __func__)); 394181803Sbz IPSEC_OSTAT(V_espstat.esps_pdrops, V_ahstat.ahs_pdrops, 395181803Sbz V_ipcompstat.ipcomps_pdrops); 396105197Ssam *error = EHOSTUNREACH; 397105197Ssam goto bad; 398105197Ssam } 399105197Ssam 400105197Ssam /* 401105197Ssam * Sanity check the SA contents for the caller 402105197Ssam * before they invoke the xform output method. 403105197Ssam */ 404105197Ssam if (sav->tdb_xform == NULL) { 405120585Ssam DPRINTF(("%s: no transform for SA\n", __func__)); 406181803Sbz IPSEC_OSTAT(V_espstat.esps_noxform, V_ahstat.ahs_noxform, 407181803Sbz V_ipcompstat.ipcomps_noxform); 408105197Ssam *error = EHOSTUNREACH; 409105197Ssam goto bad; 410105197Ssam } 411105197Ssam return isr; 412105197Ssambad: 413120585Ssam IPSEC_ASSERT(*error != 0, ("error return w/ no error code")); 414120585Ssam IPSECREQUEST_UNLOCK(isr); 415105197Ssam return NULL; 416105197Ssam#undef IPSEC_OSTAT 417105197Ssam} 418105197Ssam 419105197Ssam#ifdef INET 420105197Ssam/* 421105197Ssam * IPsec output logic for IPv4. 422105197Ssam */ 423105197Ssamint 424105197Ssamipsec4_process_packet( 425105197Ssam struct mbuf *m, 426105197Ssam struct ipsecrequest *isr, 427105197Ssam int flags, 428105197Ssam int tunalready) 429105197Ssam{ 430105197Ssam struct secasindex saidx; 431105197Ssam struct secasvar *sav; 432105197Ssam struct ip *ip; 433119643Ssam int error, i, off; 434105197Ssam 435120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 436120585Ssam IPSEC_ASSERT(isr != NULL, ("null isr")); 437105197Ssam 438120585Ssam IPSECREQUEST_LOCK(isr); /* insure SA contents don't change */ 439105197Ssam 440105197Ssam isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error); 441177175Sbz if (isr == NULL) { 442177175Sbz if (error != 0) 443177175Sbz goto bad; 444177175Sbz return EJUSTRETURN; 445177175Sbz } 446105197Ssam 447105197Ssam sav = isr->sav; 448159965Sthompsa 449159965Sthompsa#ifdef DEV_ENC 450181627Svanhu encif->if_opackets++; 451181627Svanhu encif->if_obytes += m->m_pkthdr.len; 452181627Svanhu 453174054Sbz /* pass the mbuf to enc0 for bpf processing */ 454174054Sbz ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_BEFORE); 455159965Sthompsa /* pass the mbuf to enc0 for packet filtering */ 456174054Sbz if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0) 457159965Sthompsa goto bad; 458159965Sthompsa#endif 459159965Sthompsa 460105197Ssam if (!tunalready) { 461105197Ssam union sockaddr_union *dst = &sav->sah->saidx.dst; 462105197Ssam int setdf; 463105197Ssam 464105197Ssam /* 465105197Ssam * Collect IP_DF state from the outer header. 466105197Ssam */ 467105197Ssam if (dst->sa.sa_family == AF_INET) { 468105197Ssam if (m->m_len < sizeof (struct ip) && 469105197Ssam (m = m_pullup(m, sizeof (struct ip))) == NULL) { 470105197Ssam error = ENOBUFS; 471105197Ssam goto bad; 472105197Ssam } 473105197Ssam ip = mtod(m, struct ip *); 474105197Ssam /* Honor system-wide control of how to handle IP_DF */ 475181803Sbz switch (V_ip4_ipsec_dfbit) { 476105197Ssam case 0: /* clear in outer header */ 477105197Ssam case 1: /* set in outer header */ 478181803Sbz setdf = V_ip4_ipsec_dfbit; 479105197Ssam break; 480105197Ssam default: /* propagate to outer header */ 481105197Ssam setdf = ntohs(ip->ip_off & IP_DF); 482105197Ssam break; 483105197Ssam } 484105197Ssam } else { 485105197Ssam ip = NULL; /* keep compiler happy */ 486105197Ssam setdf = 0; 487105197Ssam } 488105197Ssam /* Do the appropriate encapsulation, if necessary */ 489105197Ssam if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */ 490105197Ssam dst->sa.sa_family != AF_INET || /* PF mismatch */ 491105197Ssam#if 0 492105197Ssam (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */ 493105197Ssam sav->tdb_xform->xf_type == XF_IP4 || /* ditto */ 494105197Ssam#endif 495105197Ssam (dst->sa.sa_family == AF_INET && /* Proxy */ 496105197Ssam dst->sin.sin_addr.s_addr != INADDR_ANY && 497105197Ssam dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) { 498105197Ssam struct mbuf *mp; 499105197Ssam 500105197Ssam /* Fix IPv4 header checksum and length */ 501105197Ssam if (m->m_len < sizeof (struct ip) && 502105197Ssam (m = m_pullup(m, sizeof (struct ip))) == NULL) { 503105197Ssam error = ENOBUFS; 504105197Ssam goto bad; 505105197Ssam } 506105197Ssam ip = mtod(m, struct ip *); 507105197Ssam ip->ip_len = htons(m->m_pkthdr.len); 508105197Ssam ip->ip_sum = 0; 509105197Ssam#ifdef _IP_VHL 510105197Ssam if (ip->ip_vhl == IP_VHL_BORING) 511105197Ssam ip->ip_sum = in_cksum_hdr(ip); 512105197Ssam else 513105197Ssam ip->ip_sum = in_cksum(m, 514105197Ssam _IP_VHL_HL(ip->ip_vhl) << 2); 515105197Ssam#else 516105197Ssam ip->ip_sum = in_cksum(m, ip->ip_hl << 2); 517105197Ssam#endif 518105197Ssam 519105197Ssam /* Encapsulate the packet */ 520105197Ssam error = ipip_output(m, isr, &mp, 0, 0); 521105197Ssam if (mp == NULL && !error) { 522105197Ssam /* Should never happen. */ 523120585Ssam DPRINTF(("%s: ipip_output returns no mbuf and " 524120585Ssam "no error!", __func__)); 525105197Ssam error = EFAULT; 526105197Ssam } 527105197Ssam if (error) { 528124765Ssam if (mp) { 529124765Ssam /* XXX: Should never happen! */ 530105197Ssam m_freem(mp); 531124765Ssam } 532124765Ssam m = NULL; /* ipip_output() already freed it */ 533105197Ssam goto bad; 534105197Ssam } 535105197Ssam m = mp, mp = NULL; 536105197Ssam /* 537105197Ssam * ipip_output clears IP_DF in the new header. If 538105197Ssam * we need to propagate IP_DF from the outer header, 539105197Ssam * then we have to do it here. 540105197Ssam * 541105197Ssam * XXX shouldn't assume what ipip_output does. 542105197Ssam */ 543105197Ssam if (dst->sa.sa_family == AF_INET && setdf) { 544105197Ssam if (m->m_len < sizeof (struct ip) && 545105197Ssam (m = m_pullup(m, sizeof (struct ip))) == NULL) { 546105197Ssam error = ENOBUFS; 547105197Ssam goto bad; 548105197Ssam } 549105197Ssam ip = mtod(m, struct ip *); 550105197Ssam ip->ip_off = ntohs(ip->ip_off); 551105197Ssam ip->ip_off |= IP_DF; 552105197Ssam ip->ip_off = htons(ip->ip_off); 553105197Ssam } 554105197Ssam } 555105197Ssam } 556105197Ssam 557159965Sthompsa#ifdef DEV_ENC 558159965Sthompsa /* pass the mbuf to enc0 for bpf processing */ 559174054Sbz ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_AFTER); 560174054Sbz /* pass the mbuf to enc0 for packet filtering */ 561174054Sbz if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0) 562174054Sbz goto bad; 563159965Sthompsa#endif 564159965Sthompsa 565105197Ssam /* 566105197Ssam * Dispatch to the appropriate IPsec transform logic. The 567105197Ssam * packet will be returned for transmission after crypto 568105197Ssam * processing, etc. are completed. For encapsulation we 569105197Ssam * bypass this call because of the explicit call done above 570105197Ssam * (necessary to deal with IP_DF handling for IPv4). 571105197Ssam * 572105197Ssam * NB: m & sav are ``passed to caller'' who's reponsible for 573105197Ssam * for reclaiming their resources. 574105197Ssam */ 575105197Ssam if (sav->tdb_xform->xf_type != XF_IP4) { 576105197Ssam ip = mtod(m, struct ip *); 577105197Ssam i = ip->ip_hl << 2; 578105197Ssam off = offsetof(struct ip, ip_p); 579105197Ssam error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off); 580105197Ssam } else { 581105197Ssam error = ipsec_process_done(m, isr); 582105197Ssam } 583120585Ssam IPSECREQUEST_UNLOCK(isr); 584105197Ssam return error; 585105197Ssambad: 586120585Ssam if (isr) 587120585Ssam IPSECREQUEST_UNLOCK(isr); 588105197Ssam if (m) 589105197Ssam m_freem(m); 590105197Ssam return error; 591105197Ssam} 592105197Ssam#endif 593105197Ssam 594105197Ssam#ifdef INET6 595105197Ssam/* 596105197Ssam * Chop IP6 header from the payload. 597105197Ssam */ 598105197Ssamstatic struct mbuf * 599105197Ssamipsec6_splithdr(struct mbuf *m) 600105197Ssam{ 601105197Ssam struct mbuf *mh; 602105197Ssam struct ip6_hdr *ip6; 603105197Ssam int hlen; 604105197Ssam 605120585Ssam IPSEC_ASSERT(m->m_len >= sizeof (struct ip6_hdr), 606120585Ssam ("first mbuf too short, len %u", m->m_len)); 607105197Ssam ip6 = mtod(m, struct ip6_hdr *); 608105197Ssam hlen = sizeof(struct ip6_hdr); 609105197Ssam if (m->m_len > hlen) { 610151967Sandre MGETHDR(mh, M_DONTWAIT, MT_DATA); 611105197Ssam if (!mh) { 612105197Ssam m_freem(m); 613105197Ssam return NULL; 614105197Ssam } 615108466Ssam M_MOVE_PKTHDR(mh, m); 616105197Ssam MH_ALIGN(mh, hlen); 617105197Ssam m->m_len -= hlen; 618105197Ssam m->m_data += hlen; 619105197Ssam mh->m_next = m; 620105197Ssam m = mh; 621105197Ssam m->m_len = hlen; 622105197Ssam bcopy((caddr_t)ip6, mtod(m, caddr_t), hlen); 623105197Ssam } else if (m->m_len < hlen) { 624105197Ssam m = m_pullup(m, hlen); 625105197Ssam if (!m) 626105197Ssam return NULL; 627105197Ssam } 628105197Ssam return m; 629105197Ssam} 630105197Ssam 631105197Ssam/* 632105197Ssam * IPsec output logic for IPv6, transport mode. 633105197Ssam */ 634105197Ssamint 635105197Ssamipsec6_output_trans( 636105197Ssam struct ipsec_output_state *state, 637105197Ssam u_char *nexthdrp, 638105197Ssam struct mbuf *mprev, 639105197Ssam struct secpolicy *sp, 640105197Ssam int flags, 641105197Ssam int *tun) 642105197Ssam{ 643105197Ssam struct ipsecrequest *isr; 644105197Ssam struct secasindex saidx; 645105197Ssam int error = 0; 646105197Ssam struct mbuf *m; 647105197Ssam 648120585Ssam IPSEC_ASSERT(state != NULL, ("null state")); 649120585Ssam IPSEC_ASSERT(state->m != NULL, ("null m")); 650120585Ssam IPSEC_ASSERT(nexthdrp != NULL, ("null nexthdrp")); 651120585Ssam IPSEC_ASSERT(mprev != NULL, ("null mprev")); 652120585Ssam IPSEC_ASSERT(sp != NULL, ("null sp")); 653120585Ssam IPSEC_ASSERT(tun != NULL, ("null tun")); 654105197Ssam 655105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_DATA, 656170122Sbz printf("%s: applied SP\n", __func__); 657105197Ssam kdebug_secpolicy(sp)); 658105197Ssam 659105197Ssam isr = sp->req; 660105197Ssam if (isr->saidx.mode == IPSEC_MODE_TUNNEL) { 661105197Ssam /* the rest will be handled by ipsec6_output_tunnel() */ 662105197Ssam *tun = 1; /* need tunnel-mode processing */ 663105197Ssam return 0; 664105197Ssam } 665105197Ssam 666105197Ssam *tun = 0; 667105197Ssam m = state->m; 668105197Ssam 669171133Sgnn IPSECREQUEST_LOCK(isr); /* insure SA contents don't change */ 670105197Ssam isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error); 671105197Ssam if (isr == NULL) { 672177175Sbz if (error != 0) { 673105197Ssam#ifdef notdef 674177175Sbz /* XXX should notification be done for all errors ? */ 675177175Sbz /* 676177175Sbz * Notify the fact that the packet is discarded 677177175Sbz * to ourselves. I believe this is better than 678177175Sbz * just silently discarding. (jinmei@kame.net) 679177175Sbz * XXX: should we restrict the error to TCP packets? 680177175Sbz * XXX: should we directly notify sockets via 681177175Sbz * pfctlinputs? 682177175Sbz */ 683177175Sbz icmp6_error(m, ICMP6_DST_UNREACH, 684177175Sbz ICMP6_DST_UNREACH_ADMIN, 0); 685177175Sbz m = NULL; /* NB: icmp6_error frees mbuf */ 686105197Ssam#endif 687177175Sbz goto bad; 688177175Sbz } 689177175Sbz return EJUSTRETURN; 690105197Ssam } 691105197Ssam 692171133Sgnn error = (*isr->sav->tdb_xform->xf_output)(m, isr, NULL, 693171133Sgnn sizeof (struct ip6_hdr), 694171133Sgnn offsetof(struct ip6_hdr, 695171133Sgnn ip6_nxt)); 696171133Sgnn IPSECREQUEST_UNLOCK(isr); 697171133Sgnn return error; 698105197Ssambad: 699171133Sgnn if (isr) 700171133Sgnn IPSECREQUEST_UNLOCK(isr); 701105197Ssam if (m) 702105197Ssam m_freem(m); 703105197Ssam state->m = NULL; 704105197Ssam return error; 705105197Ssam} 706105197Ssam 707105197Ssamstatic int 708105197Ssamipsec6_encapsulate(struct mbuf *m, struct secasvar *sav) 709105197Ssam{ 710105197Ssam struct ip6_hdr *oip6; 711105197Ssam struct ip6_hdr *ip6; 712105197Ssam size_t plen; 713105197Ssam 714105197Ssam /* can't tunnel between different AFs */ 715105197Ssam if (sav->sah->saidx.src.sa.sa_family != AF_INET6 || 716105197Ssam sav->sah->saidx.dst.sa.sa_family != AF_INET6) { 717105197Ssam m_freem(m); 718105197Ssam return EINVAL; 719105197Ssam } 720171133Sgnn IPSEC_ASSERT(m->m_len == sizeof (struct ip6_hdr), 721120585Ssam ("mbuf wrong size; len %u", m->m_len)); 722105197Ssam 723105197Ssam 724105197Ssam /* 725105197Ssam * grow the mbuf to accomodate the new IPv6 header. 726105197Ssam */ 727105197Ssam plen = m->m_pkthdr.len; 728105197Ssam if (M_LEADINGSPACE(m->m_next) < sizeof(struct ip6_hdr)) { 729105197Ssam struct mbuf *n; 730111119Simp MGET(n, M_DONTWAIT, MT_DATA); 731105197Ssam if (!n) { 732105197Ssam m_freem(m); 733105197Ssam return ENOBUFS; 734105197Ssam } 735105197Ssam n->m_len = sizeof(struct ip6_hdr); 736105197Ssam n->m_next = m->m_next; 737105197Ssam m->m_next = n; 738105197Ssam m->m_pkthdr.len += sizeof(struct ip6_hdr); 739105197Ssam oip6 = mtod(n, struct ip6_hdr *); 740105197Ssam } else { 741105197Ssam m->m_next->m_len += sizeof(struct ip6_hdr); 742105197Ssam m->m_next->m_data -= sizeof(struct ip6_hdr); 743105197Ssam m->m_pkthdr.len += sizeof(struct ip6_hdr); 744105197Ssam oip6 = mtod(m->m_next, struct ip6_hdr *); 745105197Ssam } 746105197Ssam ip6 = mtod(m, struct ip6_hdr *); 747113076Sdes bcopy((caddr_t)ip6, (caddr_t)oip6, sizeof(struct ip6_hdr)); 748105197Ssam 749105197Ssam /* Fake link-local scope-class addresses */ 750105197Ssam if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src)) 751105197Ssam oip6->ip6_src.s6_addr16[1] = 0; 752105197Ssam if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst)) 753105197Ssam oip6->ip6_dst.s6_addr16[1] = 0; 754105197Ssam 755105197Ssam /* construct new IPv6 header. see RFC 2401 5.1.2.2 */ 756105197Ssam /* ECN consideration. */ 757181803Sbz ip6_ecn_ingress(V_ip6_ipsec_ecn, &ip6->ip6_flow, &oip6->ip6_flow); 758105197Ssam if (plen < IPV6_MAXPACKET - sizeof(struct ip6_hdr)) 759105197Ssam ip6->ip6_plen = htons(plen); 760105197Ssam else { 761105197Ssam /* ip6->ip6_plen will be updated in ip6_output() */ 762105197Ssam } 763105197Ssam ip6->ip6_nxt = IPPROTO_IPV6; 764171133Sgnn ip6->ip6_src = sav->sah->saidx.src.sin6.sin6_addr; 765171133Sgnn ip6->ip6_dst = sav->sah->saidx.dst.sin6.sin6_addr; 766105197Ssam ip6->ip6_hlim = IPV6_DEFHLIM; 767105197Ssam 768105197Ssam /* XXX Should ip6_src be updated later ? */ 769105197Ssam 770105197Ssam return 0; 771105197Ssam} 772105197Ssam 773105197Ssam/* 774105197Ssam * IPsec output logic for IPv6, tunnel mode. 775105197Ssam */ 776105197Ssamint 777105197Ssamipsec6_output_tunnel(struct ipsec_output_state *state, struct secpolicy *sp, int flags) 778105197Ssam{ 779105197Ssam struct ip6_hdr *ip6; 780105197Ssam struct ipsecrequest *isr; 781105197Ssam struct secasindex saidx; 782105197Ssam int error; 783213836Sbz struct sockaddr_in6 *dst6; 784105197Ssam struct mbuf *m; 785105197Ssam 786120585Ssam IPSEC_ASSERT(state != NULL, ("null state")); 787120585Ssam IPSEC_ASSERT(state->m != NULL, ("null m")); 788120585Ssam IPSEC_ASSERT(sp != NULL, ("null sp")); 789105197Ssam 790105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_DATA, 791170122Sbz printf("%s: applied SP\n", __func__); 792105197Ssam kdebug_secpolicy(sp)); 793105197Ssam 794105197Ssam m = state->m; 795105197Ssam /* 796105197Ssam * transport mode ipsec (before the 1st tunnel mode) is already 797105197Ssam * processed by ipsec6_output_trans(). 798105197Ssam */ 799105197Ssam for (isr = sp->req; isr; isr = isr->next) { 800105197Ssam if (isr->saidx.mode == IPSEC_MODE_TUNNEL) 801105197Ssam break; 802105197Ssam } 803170123Sbz 804170123Sbz IPSECREQUEST_LOCK(isr); /* insure SA contents don't change */ 805105197Ssam isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error); 806177175Sbz if (isr == NULL) { 807177175Sbz if (error != 0) 808177175Sbz goto bad; 809177175Sbz return EJUSTRETURN; 810177175Sbz } 811105197Ssam 812174054Sbz#ifdef DEV_ENC 813181627Svanhu encif->if_opackets++; 814181627Svanhu encif->if_obytes += m->m_pkthdr.len; 815181627Svanhu 816174054Sbz /* pass the mbuf to enc0 for bpf processing */ 817174054Sbz ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_BEFORE); 818174054Sbz /* pass the mbuf to enc0 for packet filtering */ 819174054Sbz if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0) 820174054Sbz goto bad; 821174054Sbz#endif 822174054Sbz 823105197Ssam /* 824105197Ssam * There may be the case that SA status will be changed when 825105197Ssam * we are refering to one. So calling splsoftnet(). 826105197Ssam */ 827105197Ssam if (isr->saidx.mode == IPSEC_MODE_TUNNEL) { 828105197Ssam /* 829105197Ssam * build IPsec tunnel. 830105197Ssam */ 831105197Ssam /* XXX should be processed with other familiy */ 832105197Ssam if (isr->sav->sah->saidx.src.sa.sa_family != AF_INET6) { 833120585Ssam ipseclog((LOG_ERR, "%s: family mismatched between " 834120585Ssam "inner and outer, spi=%u\n", __func__, 835105197Ssam ntohl(isr->sav->spi))); 836181803Sbz V_ipsec6stat.ips_out_inval++; 837105197Ssam error = EAFNOSUPPORT; 838105197Ssam goto bad; 839105197Ssam } 840105197Ssam 841105197Ssam m = ipsec6_splithdr(m); 842105197Ssam if (!m) { 843181803Sbz V_ipsec6stat.ips_out_nomem++; 844105197Ssam error = ENOMEM; 845105197Ssam goto bad; 846105197Ssam } 847105197Ssam error = ipsec6_encapsulate(m, isr->sav); 848105197Ssam if (error) { 849105197Ssam m = NULL; 850105197Ssam goto bad; 851105197Ssam } 852105197Ssam ip6 = mtod(m, struct ip6_hdr *); 853105197Ssam 854214250Sbz state->ro = 855214250Sbz (struct route *)&isr->sav->sah->route_cache.sin6_route; 856105197Ssam state->dst = (struct sockaddr *)&state->ro->ro_dst; 857105197Ssam dst6 = (struct sockaddr_in6 *)state->dst; 858105197Ssam if (state->ro->ro_rt 859105197Ssam && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0 860105197Ssam || !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_dst))) { 861105197Ssam RTFREE(state->ro->ro_rt); 862105197Ssam state->ro->ro_rt = NULL; 863105197Ssam } 864187936Sbz if (state->ro->ro_rt == NULL) { 865105197Ssam bzero(dst6, sizeof(*dst6)); 866105197Ssam dst6->sin6_family = AF_INET6; 867105197Ssam dst6->sin6_len = sizeof(*dst6); 868105197Ssam dst6->sin6_addr = ip6->ip6_dst; 869231852Sbz rtalloc_ign_fib(state->ro, 0UL, M_GETFIB(m)); 870105197Ssam } 871187936Sbz if (state->ro->ro_rt == NULL) { 872181803Sbz V_ip6stat.ip6s_noroute++; 873181803Sbz V_ipsec6stat.ips_out_noroute++; 874105197Ssam error = EHOSTUNREACH; 875105197Ssam goto bad; 876105197Ssam } 877105197Ssam 878105197Ssam /* adjust state->dst if tunnel endpoint is offlink */ 879213837Sbz if (state->ro->ro_rt->rt_flags & RTF_GATEWAY) 880105197Ssam state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway; 881105197Ssam } 882105197Ssam 883105197Ssam m = ipsec6_splithdr(m); 884105197Ssam if (!m) { 885181803Sbz V_ipsec6stat.ips_out_nomem++; 886105197Ssam error = ENOMEM; 887105197Ssam goto bad; 888105197Ssam } 889105197Ssam ip6 = mtod(m, struct ip6_hdr *); 890174054Sbz 891174054Sbz#ifdef DEV_ENC 892174054Sbz /* pass the mbuf to enc0 for bpf processing */ 893174054Sbz ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_AFTER); 894174054Sbz /* pass the mbuf to enc0 for packet filtering */ 895174054Sbz if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0) 896174054Sbz goto bad; 897174054Sbz#endif 898174054Sbz 899170123Sbz error = (*isr->sav->tdb_xform->xf_output)(m, isr, NULL, 900105197Ssam sizeof (struct ip6_hdr), 901105197Ssam offsetof(struct ip6_hdr, ip6_nxt)); 902170123Sbz IPSECREQUEST_UNLOCK(isr); 903170123Sbz return error; 904105197Ssambad: 905170123Sbz if (isr) 906170123Sbz IPSECREQUEST_UNLOCK(isr); 907105197Ssam if (m) 908105197Ssam m_freem(m); 909105197Ssam state->m = NULL; 910105197Ssam return error; 911105197Ssam} 912105197Ssam#endif /*INET6*/ 913