ipsec_output.c revision 181627
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 181627 2008-08-12 09:05:01Z 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> 47171497Sbz#include <net/pfil.h> 48105197Ssam#include <net/route.h> 49105197Ssam 50105197Ssam#include <netinet/in.h> 51105197Ssam#include <netinet/in_systm.h> 52105197Ssam#include <netinet/ip.h> 53105197Ssam#include <netinet/ip_var.h> 54105197Ssam#include <netinet/in_var.h> 55105197Ssam#include <netinet/ip_ecn.h> 56105197Ssam#ifdef INET6 57105197Ssam#include <netinet6/ip6_ecn.h> 58105197Ssam#endif 59105197Ssam 60105197Ssam#include <netinet/ip6.h> 61105197Ssam#ifdef INET6 62105197Ssam#include <netinet6/ip6_var.h> 63105197Ssam#endif 64105197Ssam#include <netinet/in_pcb.h> 65105197Ssam#ifdef INET6 66105197Ssam#include <netinet/icmp6.h> 67105197Ssam#endif 68105197Ssam 69105197Ssam#include <netipsec/ipsec.h> 70105197Ssam#ifdef INET6 71105197Ssam#include <netipsec/ipsec6.h> 72105197Ssam#endif 73105197Ssam#include <netipsec/ah_var.h> 74105197Ssam#include <netipsec/esp_var.h> 75105197Ssam#include <netipsec/ipcomp_var.h> 76105197Ssam 77105197Ssam#include <netipsec/xform.h> 78105197Ssam 79105197Ssam#include <netipsec/key.h> 80105197Ssam#include <netipsec/keydb.h> 81105197Ssam#include <netipsec/key_debug.h> 82105197Ssam 83105197Ssam#include <machine/in_cksum.h> 84105197Ssam 85181627Svanhu#ifdef DEV_ENC 86181627Svanhu#include <net/if_enc.h> 87181627Svanhu#endif 88181627Svanhu 89181627Svanhu 90105197Ssamint 91105197Ssamipsec_process_done(struct mbuf *m, struct ipsecrequest *isr) 92105197Ssam{ 93105197Ssam struct tdb_ident *tdbi; 94105197Ssam struct m_tag *mtag; 95105197Ssam struct secasvar *sav; 96105197Ssam struct secasindex *saidx; 97105197Ssam int error; 98105197Ssam 99120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 100120585Ssam IPSEC_ASSERT(isr != NULL, ("null ISR")); 101105197Ssam sav = isr->sav; 102120585Ssam IPSEC_ASSERT(sav != NULL, ("null SA")); 103120585Ssam IPSEC_ASSERT(sav->sah != NULL, ("null SAH")); 104105197Ssam 105105197Ssam saidx = &sav->sah->saidx; 106105197Ssam switch (saidx->dst.sa.sa_family) { 107105197Ssam#ifdef INET 108105197Ssam case AF_INET: 109105197Ssam /* Fix the header length, for AH processing. */ 110105197Ssam mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len); 111105197Ssam break; 112105197Ssam#endif /* INET */ 113105197Ssam#ifdef INET6 114105197Ssam case AF_INET6: 115105197Ssam /* Fix the header length, for AH processing. */ 116105197Ssam if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) { 117105197Ssam error = ENXIO; 118105197Ssam goto bad; 119105197Ssam } 120105197Ssam if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) { 121105197Ssam /* No jumbogram support. */ 122105197Ssam error = ENXIO; /*?*/ 123105197Ssam goto bad; 124105197Ssam } 125105197Ssam mtod(m, struct ip6_hdr *)->ip6_plen = 126105197Ssam htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 127105197Ssam break; 128105197Ssam#endif /* INET6 */ 129105197Ssam default: 130120585Ssam DPRINTF(("%s: unknown protocol family %u\n", __func__, 131105197Ssam saidx->dst.sa.sa_family)); 132105197Ssam error = ENXIO; 133105197Ssam goto bad; 134105197Ssam } 135105197Ssam 136105197Ssam /* 137105197Ssam * Add a record of what we've done or what needs to be done to the 138105197Ssam * packet. 139105197Ssam */ 140105197Ssam mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, 141105197Ssam sizeof(struct tdb_ident), M_NOWAIT); 142105197Ssam if (mtag == NULL) { 143120585Ssam DPRINTF(("%s: could not get packet tag\n", __func__)); 144105197Ssam error = ENOMEM; 145105197Ssam goto bad; 146105197Ssam } 147105197Ssam 148105197Ssam tdbi = (struct tdb_ident *)(mtag + 1); 149105197Ssam tdbi->dst = saidx->dst; 150105197Ssam tdbi->proto = saidx->proto; 151105197Ssam tdbi->spi = sav->spi; 152105197Ssam m_tag_prepend(m, mtag); 153105197Ssam 154105197Ssam /* 155105197Ssam * If there's another (bundled) SA to apply, do so. 156105197Ssam * Note that this puts a burden on the kernel stack size. 157105197Ssam * If this is a problem we'll need to introduce a queue 158105197Ssam * to set the packet on so we can unwind the stack before 159105197Ssam * doing further processing. 160105197Ssam */ 161105197Ssam if (isr->next) { 162171133Sgnn ipsec4stat.ips_out_bundlesa++; 163105197Ssam return ipsec4_process_packet(m, isr->next, 0, 0); 164105197Ssam } 165117056Ssam key_sa_recordxfer(sav, m); /* record data transfer */ 166105197Ssam 167105197Ssam /* 168105197Ssam * We're done with IPsec processing, transmit the packet using the 169105197Ssam * appropriate network protocol (IP or IPv6). SPD lookup will be 170105197Ssam * performed again there. 171105197Ssam */ 172105197Ssam switch (saidx->dst.sa.sa_family) { 173105197Ssam#ifdef INET 174105197Ssam struct ip *ip; 175105197Ssam case AF_INET: 176105197Ssam ip = mtod(m, struct ip *); 177105197Ssam ip->ip_len = ntohs(ip->ip_len); 178105197Ssam ip->ip_off = ntohs(ip->ip_off); 179105197Ssam 180105197Ssam return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL); 181105197Ssam#endif /* INET */ 182105197Ssam#ifdef INET6 183105197Ssam case AF_INET6: 184105197Ssam /* 185105197Ssam * We don't need massage, IPv6 header fields are always in 186105197Ssam * net endian. 187105197Ssam */ 188105197Ssam return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL); 189105197Ssam#endif /* INET6 */ 190105197Ssam } 191105197Ssam panic("ipsec_process_done"); 192105197Ssambad: 193105197Ssam m_freem(m); 194105197Ssam KEY_FREESAV(&sav); 195105197Ssam return (error); 196105197Ssam} 197105197Ssam 198105197Ssamstatic struct ipsecrequest * 199105197Ssamipsec_nextisr( 200105197Ssam struct mbuf *m, 201105197Ssam struct ipsecrequest *isr, 202105197Ssam int af, 203105197Ssam struct secasindex *saidx, 204105197Ssam int *error 205105197Ssam) 206105197Ssam{ 207105197Ssam#define IPSEC_OSTAT(x,y,z) (isr->saidx.proto == IPPROTO_ESP ? (x)++ : \ 208105197Ssam isr->saidx.proto == IPPROTO_AH ? (y)++ : (z)++) 209105197Ssam struct secasvar *sav; 210105197Ssam 211120585Ssam IPSECREQUEST_LOCK_ASSERT(isr); 212120585Ssam 213120585Ssam IPSEC_ASSERT(af == AF_INET || af == AF_INET6, 214120585Ssam ("invalid address family %u", af)); 215105197Ssamagain: 216105197Ssam /* 217105197Ssam * Craft SA index to search for proper SA. Note that 218105197Ssam * we only fillin unspecified SA peers for transport 219105197Ssam * mode; for tunnel mode they must already be filled in. 220105197Ssam */ 221105197Ssam *saidx = isr->saidx; 222105197Ssam if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) { 223105197Ssam /* Fillin unspecified SA peers only for transport mode */ 224105197Ssam if (af == AF_INET) { 225105197Ssam struct sockaddr_in *sin; 226105197Ssam struct ip *ip = mtod(m, struct ip *); 227105197Ssam 228105197Ssam if (saidx->src.sa.sa_len == 0) { 229105197Ssam sin = &saidx->src.sin; 230105197Ssam sin->sin_len = sizeof(*sin); 231105197Ssam sin->sin_family = AF_INET; 232105197Ssam sin->sin_port = IPSEC_PORT_ANY; 233105197Ssam sin->sin_addr = ip->ip_src; 234105197Ssam } 235105197Ssam if (saidx->dst.sa.sa_len == 0) { 236105197Ssam sin = &saidx->dst.sin; 237105197Ssam sin->sin_len = sizeof(*sin); 238105197Ssam sin->sin_family = AF_INET; 239105197Ssam sin->sin_port = IPSEC_PORT_ANY; 240105197Ssam sin->sin_addr = ip->ip_dst; 241105197Ssam } 242105197Ssam } else { 243105197Ssam struct sockaddr_in6 *sin6; 244105197Ssam struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 245105197Ssam 246105197Ssam if (saidx->src.sin6.sin6_len == 0) { 247105197Ssam sin6 = (struct sockaddr_in6 *)&saidx->src; 248105197Ssam sin6->sin6_len = sizeof(*sin6); 249105197Ssam sin6->sin6_family = AF_INET6; 250105197Ssam sin6->sin6_port = IPSEC_PORT_ANY; 251105197Ssam sin6->sin6_addr = ip6->ip6_src; 252105197Ssam if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) { 253105197Ssam /* fix scope id for comparing SPD */ 254105197Ssam sin6->sin6_addr.s6_addr16[1] = 0; 255105197Ssam sin6->sin6_scope_id = 256105197Ssam ntohs(ip6->ip6_src.s6_addr16[1]); 257105197Ssam } 258105197Ssam } 259105197Ssam if (saidx->dst.sin6.sin6_len == 0) { 260105197Ssam sin6 = (struct sockaddr_in6 *)&saidx->dst; 261105197Ssam sin6->sin6_len = sizeof(*sin6); 262105197Ssam sin6->sin6_family = AF_INET6; 263105197Ssam sin6->sin6_port = IPSEC_PORT_ANY; 264105197Ssam sin6->sin6_addr = ip6->ip6_dst; 265105197Ssam if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) { 266105197Ssam /* fix scope id for comparing SPD */ 267105197Ssam sin6->sin6_addr.s6_addr16[1] = 0; 268105197Ssam sin6->sin6_scope_id = 269105197Ssam ntohs(ip6->ip6_dst.s6_addr16[1]); 270105197Ssam } 271105197Ssam } 272105197Ssam } 273105197Ssam } 274105197Ssam 275105197Ssam /* 276105197Ssam * Lookup SA and validate it. 277105197Ssam */ 278105197Ssam *error = key_checkrequest(isr, saidx); 279105197Ssam if (*error != 0) { 280105197Ssam /* 281105197Ssam * IPsec processing is required, but no SA found. 282105197Ssam * I assume that key_acquire() had been called 283105197Ssam * to get/establish the SA. Here I discard 284105197Ssam * this packet because it is responsibility for 285105197Ssam * upper layer to retransmit the packet. 286105197Ssam */ 287171133Sgnn ipsec4stat.ips_out_nosa++; 288105197Ssam goto bad; 289105197Ssam } 290105197Ssam sav = isr->sav; 291177175Sbz if (sav == NULL) { 292120585Ssam IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE, 293120585Ssam ("no SA found, but required; level %u", 294105197Ssam ipsec_get_reqlevel(isr))); 295120585Ssam IPSECREQUEST_UNLOCK(isr); 296105197Ssam isr = isr->next; 297177175Sbz /* 298177175Sbz * If isr is NULL, we found a 'use' policy w/o SA. 299177175Sbz * Return w/o error and w/o isr so we can drop out 300177175Sbz * and continue w/o IPsec processing. 301177175Sbz */ 302177175Sbz if (isr == NULL) 303105197Ssam return isr; 304120585Ssam IPSECREQUEST_LOCK(isr); 305105197Ssam goto again; 306105197Ssam } 307105197Ssam 308105197Ssam /* 309105197Ssam * Check system global policy controls. 310105197Ssam */ 311105197Ssam if ((isr->saidx.proto == IPPROTO_ESP && !esp_enable) || 312105197Ssam (isr->saidx.proto == IPPROTO_AH && !ah_enable) || 313105197Ssam (isr->saidx.proto == IPPROTO_IPCOMP && !ipcomp_enable)) { 314120585Ssam DPRINTF(("%s: IPsec outbound packet dropped due" 315120585Ssam " to policy (check your sysctls)\n", __func__)); 316105197Ssam IPSEC_OSTAT(espstat.esps_pdrops, ahstat.ahs_pdrops, 317105197Ssam ipcompstat.ipcomps_pdrops); 318105197Ssam *error = EHOSTUNREACH; 319105197Ssam goto bad; 320105197Ssam } 321105197Ssam 322105197Ssam /* 323105197Ssam * Sanity check the SA contents for the caller 324105197Ssam * before they invoke the xform output method. 325105197Ssam */ 326105197Ssam if (sav->tdb_xform == NULL) { 327120585Ssam DPRINTF(("%s: no transform for SA\n", __func__)); 328105197Ssam IPSEC_OSTAT(espstat.esps_noxform, ahstat.ahs_noxform, 329105197Ssam ipcompstat.ipcomps_noxform); 330105197Ssam *error = EHOSTUNREACH; 331105197Ssam goto bad; 332105197Ssam } 333105197Ssam return isr; 334105197Ssambad: 335120585Ssam IPSEC_ASSERT(*error != 0, ("error return w/ no error code")); 336120585Ssam IPSECREQUEST_UNLOCK(isr); 337105197Ssam return NULL; 338105197Ssam#undef IPSEC_OSTAT 339105197Ssam} 340105197Ssam 341105197Ssam#ifdef INET 342105197Ssam/* 343105197Ssam * IPsec output logic for IPv4. 344105197Ssam */ 345105197Ssamint 346105197Ssamipsec4_process_packet( 347105197Ssam struct mbuf *m, 348105197Ssam struct ipsecrequest *isr, 349105197Ssam int flags, 350105197Ssam int tunalready) 351105197Ssam{ 352105197Ssam struct secasindex saidx; 353105197Ssam struct secasvar *sav; 354105197Ssam struct ip *ip; 355119643Ssam int error, i, off; 356105197Ssam 357120585Ssam IPSEC_ASSERT(m != NULL, ("null mbuf")); 358120585Ssam IPSEC_ASSERT(isr != NULL, ("null isr")); 359105197Ssam 360120585Ssam IPSECREQUEST_LOCK(isr); /* insure SA contents don't change */ 361105197Ssam 362105197Ssam isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error); 363177175Sbz if (isr == NULL) { 364177175Sbz if (error != 0) 365177175Sbz goto bad; 366177175Sbz return EJUSTRETURN; 367177175Sbz } 368105197Ssam 369105197Ssam sav = isr->sav; 370159965Sthompsa 371159965Sthompsa#ifdef DEV_ENC 372181627Svanhu encif->if_opackets++; 373181627Svanhu encif->if_obytes += m->m_pkthdr.len; 374181627Svanhu 375174054Sbz /* pass the mbuf to enc0 for bpf processing */ 376174054Sbz ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_BEFORE); 377159965Sthompsa /* pass the mbuf to enc0 for packet filtering */ 378174054Sbz if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0) 379159965Sthompsa goto bad; 380159965Sthompsa#endif 381159965Sthompsa 382105197Ssam if (!tunalready) { 383105197Ssam union sockaddr_union *dst = &sav->sah->saidx.dst; 384105197Ssam int setdf; 385105197Ssam 386105197Ssam /* 387105197Ssam * Collect IP_DF state from the outer header. 388105197Ssam */ 389105197Ssam if (dst->sa.sa_family == AF_INET) { 390105197Ssam if (m->m_len < sizeof (struct ip) && 391105197Ssam (m = m_pullup(m, sizeof (struct ip))) == NULL) { 392105197Ssam error = ENOBUFS; 393105197Ssam goto bad; 394105197Ssam } 395105197Ssam ip = mtod(m, struct ip *); 396105197Ssam /* Honor system-wide control of how to handle IP_DF */ 397105197Ssam switch (ip4_ipsec_dfbit) { 398105197Ssam case 0: /* clear in outer header */ 399105197Ssam case 1: /* set in outer header */ 400105197Ssam setdf = ip4_ipsec_dfbit; 401105197Ssam break; 402105197Ssam default: /* propagate to outer header */ 403105197Ssam setdf = ntohs(ip->ip_off & IP_DF); 404105197Ssam break; 405105197Ssam } 406105197Ssam } else { 407105197Ssam ip = NULL; /* keep compiler happy */ 408105197Ssam setdf = 0; 409105197Ssam } 410105197Ssam /* Do the appropriate encapsulation, if necessary */ 411105197Ssam if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */ 412105197Ssam dst->sa.sa_family != AF_INET || /* PF mismatch */ 413105197Ssam#if 0 414105197Ssam (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */ 415105197Ssam sav->tdb_xform->xf_type == XF_IP4 || /* ditto */ 416105197Ssam#endif 417105197Ssam (dst->sa.sa_family == AF_INET && /* Proxy */ 418105197Ssam dst->sin.sin_addr.s_addr != INADDR_ANY && 419105197Ssam dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) { 420105197Ssam struct mbuf *mp; 421105197Ssam 422105197Ssam /* Fix IPv4 header checksum and length */ 423105197Ssam if (m->m_len < sizeof (struct ip) && 424105197Ssam (m = m_pullup(m, sizeof (struct ip))) == NULL) { 425105197Ssam error = ENOBUFS; 426105197Ssam goto bad; 427105197Ssam } 428105197Ssam ip = mtod(m, struct ip *); 429105197Ssam ip->ip_len = htons(m->m_pkthdr.len); 430105197Ssam ip->ip_sum = 0; 431105197Ssam#ifdef _IP_VHL 432105197Ssam if (ip->ip_vhl == IP_VHL_BORING) 433105197Ssam ip->ip_sum = in_cksum_hdr(ip); 434105197Ssam else 435105197Ssam ip->ip_sum = in_cksum(m, 436105197Ssam _IP_VHL_HL(ip->ip_vhl) << 2); 437105197Ssam#else 438105197Ssam ip->ip_sum = in_cksum(m, ip->ip_hl << 2); 439105197Ssam#endif 440105197Ssam 441105197Ssam /* Encapsulate the packet */ 442105197Ssam error = ipip_output(m, isr, &mp, 0, 0); 443105197Ssam if (mp == NULL && !error) { 444105197Ssam /* Should never happen. */ 445120585Ssam DPRINTF(("%s: ipip_output returns no mbuf and " 446120585Ssam "no error!", __func__)); 447105197Ssam error = EFAULT; 448105197Ssam } 449105197Ssam if (error) { 450124765Ssam if (mp) { 451124765Ssam /* XXX: Should never happen! */ 452105197Ssam m_freem(mp); 453124765Ssam } 454124765Ssam m = NULL; /* ipip_output() already freed it */ 455105197Ssam goto bad; 456105197Ssam } 457105197Ssam m = mp, mp = NULL; 458105197Ssam /* 459105197Ssam * ipip_output clears IP_DF in the new header. If 460105197Ssam * we need to propagate IP_DF from the outer header, 461105197Ssam * then we have to do it here. 462105197Ssam * 463105197Ssam * XXX shouldn't assume what ipip_output does. 464105197Ssam */ 465105197Ssam if (dst->sa.sa_family == AF_INET && setdf) { 466105197Ssam if (m->m_len < sizeof (struct ip) && 467105197Ssam (m = m_pullup(m, sizeof (struct ip))) == NULL) { 468105197Ssam error = ENOBUFS; 469105197Ssam goto bad; 470105197Ssam } 471105197Ssam ip = mtod(m, struct ip *); 472105197Ssam ip->ip_off = ntohs(ip->ip_off); 473105197Ssam ip->ip_off |= IP_DF; 474105197Ssam ip->ip_off = htons(ip->ip_off); 475105197Ssam } 476105197Ssam } 477105197Ssam } 478105197Ssam 479159965Sthompsa#ifdef DEV_ENC 480159965Sthompsa /* pass the mbuf to enc0 for bpf processing */ 481174054Sbz ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_AFTER); 482174054Sbz /* pass the mbuf to enc0 for packet filtering */ 483174054Sbz if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0) 484174054Sbz goto bad; 485159965Sthompsa#endif 486159965Sthompsa 487105197Ssam /* 488105197Ssam * Dispatch to the appropriate IPsec transform logic. The 489105197Ssam * packet will be returned for transmission after crypto 490105197Ssam * processing, etc. are completed. For encapsulation we 491105197Ssam * bypass this call because of the explicit call done above 492105197Ssam * (necessary to deal with IP_DF handling for IPv4). 493105197Ssam * 494105197Ssam * NB: m & sav are ``passed to caller'' who's reponsible for 495105197Ssam * for reclaiming their resources. 496105197Ssam */ 497105197Ssam if (sav->tdb_xform->xf_type != XF_IP4) { 498105197Ssam ip = mtod(m, struct ip *); 499105197Ssam i = ip->ip_hl << 2; 500105197Ssam off = offsetof(struct ip, ip_p); 501105197Ssam error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off); 502105197Ssam } else { 503105197Ssam error = ipsec_process_done(m, isr); 504105197Ssam } 505120585Ssam IPSECREQUEST_UNLOCK(isr); 506105197Ssam return error; 507105197Ssambad: 508120585Ssam if (isr) 509120585Ssam IPSECREQUEST_UNLOCK(isr); 510105197Ssam if (m) 511105197Ssam m_freem(m); 512105197Ssam return error; 513105197Ssam} 514105197Ssam#endif 515105197Ssam 516105197Ssam#ifdef INET6 517105197Ssam/* 518105197Ssam * Chop IP6 header from the payload. 519105197Ssam */ 520105197Ssamstatic struct mbuf * 521105197Ssamipsec6_splithdr(struct mbuf *m) 522105197Ssam{ 523105197Ssam struct mbuf *mh; 524105197Ssam struct ip6_hdr *ip6; 525105197Ssam int hlen; 526105197Ssam 527120585Ssam IPSEC_ASSERT(m->m_len >= sizeof (struct ip6_hdr), 528120585Ssam ("first mbuf too short, len %u", m->m_len)); 529105197Ssam ip6 = mtod(m, struct ip6_hdr *); 530105197Ssam hlen = sizeof(struct ip6_hdr); 531105197Ssam if (m->m_len > hlen) { 532151967Sandre MGETHDR(mh, M_DONTWAIT, MT_DATA); 533105197Ssam if (!mh) { 534105197Ssam m_freem(m); 535105197Ssam return NULL; 536105197Ssam } 537108466Ssam M_MOVE_PKTHDR(mh, m); 538105197Ssam MH_ALIGN(mh, hlen); 539105197Ssam m->m_len -= hlen; 540105197Ssam m->m_data += hlen; 541105197Ssam mh->m_next = m; 542105197Ssam m = mh; 543105197Ssam m->m_len = hlen; 544105197Ssam bcopy((caddr_t)ip6, mtod(m, caddr_t), hlen); 545105197Ssam } else if (m->m_len < hlen) { 546105197Ssam m = m_pullup(m, hlen); 547105197Ssam if (!m) 548105197Ssam return NULL; 549105197Ssam } 550105197Ssam return m; 551105197Ssam} 552105197Ssam 553105197Ssam/* 554105197Ssam * IPsec output logic for IPv6, transport mode. 555105197Ssam */ 556105197Ssamint 557105197Ssamipsec6_output_trans( 558105197Ssam struct ipsec_output_state *state, 559105197Ssam u_char *nexthdrp, 560105197Ssam struct mbuf *mprev, 561105197Ssam struct secpolicy *sp, 562105197Ssam int flags, 563105197Ssam int *tun) 564105197Ssam{ 565105197Ssam struct ipsecrequest *isr; 566105197Ssam struct secasindex saidx; 567105197Ssam int error = 0; 568105197Ssam struct mbuf *m; 569105197Ssam 570120585Ssam IPSEC_ASSERT(state != NULL, ("null state")); 571120585Ssam IPSEC_ASSERT(state->m != NULL, ("null m")); 572120585Ssam IPSEC_ASSERT(nexthdrp != NULL, ("null nexthdrp")); 573120585Ssam IPSEC_ASSERT(mprev != NULL, ("null mprev")); 574120585Ssam IPSEC_ASSERT(sp != NULL, ("null sp")); 575120585Ssam IPSEC_ASSERT(tun != NULL, ("null tun")); 576105197Ssam 577105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_DATA, 578170122Sbz printf("%s: applied SP\n", __func__); 579105197Ssam kdebug_secpolicy(sp)); 580105197Ssam 581105197Ssam isr = sp->req; 582105197Ssam if (isr->saidx.mode == IPSEC_MODE_TUNNEL) { 583105197Ssam /* the rest will be handled by ipsec6_output_tunnel() */ 584105197Ssam *tun = 1; /* need tunnel-mode processing */ 585105197Ssam return 0; 586105197Ssam } 587105197Ssam 588105197Ssam *tun = 0; 589105197Ssam m = state->m; 590105197Ssam 591171133Sgnn IPSECREQUEST_LOCK(isr); /* insure SA contents don't change */ 592105197Ssam isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error); 593105197Ssam if (isr == NULL) { 594177175Sbz if (error != 0) { 595105197Ssam#ifdef notdef 596177175Sbz /* XXX should notification be done for all errors ? */ 597177175Sbz /* 598177175Sbz * Notify the fact that the packet is discarded 599177175Sbz * to ourselves. I believe this is better than 600177175Sbz * just silently discarding. (jinmei@kame.net) 601177175Sbz * XXX: should we restrict the error to TCP packets? 602177175Sbz * XXX: should we directly notify sockets via 603177175Sbz * pfctlinputs? 604177175Sbz */ 605177175Sbz icmp6_error(m, ICMP6_DST_UNREACH, 606177175Sbz ICMP6_DST_UNREACH_ADMIN, 0); 607177175Sbz m = NULL; /* NB: icmp6_error frees mbuf */ 608105197Ssam#endif 609177175Sbz goto bad; 610177175Sbz } 611177175Sbz return EJUSTRETURN; 612105197Ssam } 613105197Ssam 614171133Sgnn error = (*isr->sav->tdb_xform->xf_output)(m, isr, NULL, 615171133Sgnn sizeof (struct ip6_hdr), 616171133Sgnn offsetof(struct ip6_hdr, 617171133Sgnn ip6_nxt)); 618171133Sgnn IPSECREQUEST_UNLOCK(isr); 619171133Sgnn return error; 620105197Ssambad: 621171133Sgnn if (isr) 622171133Sgnn IPSECREQUEST_UNLOCK(isr); 623105197Ssam if (m) 624105197Ssam m_freem(m); 625105197Ssam state->m = NULL; 626105197Ssam return error; 627105197Ssam} 628105197Ssam 629105197Ssamstatic int 630105197Ssamipsec6_encapsulate(struct mbuf *m, struct secasvar *sav) 631105197Ssam{ 632105197Ssam struct ip6_hdr *oip6; 633105197Ssam struct ip6_hdr *ip6; 634105197Ssam size_t plen; 635105197Ssam 636105197Ssam /* can't tunnel between different AFs */ 637105197Ssam if (sav->sah->saidx.src.sa.sa_family != AF_INET6 || 638105197Ssam sav->sah->saidx.dst.sa.sa_family != AF_INET6) { 639105197Ssam m_freem(m); 640105197Ssam return EINVAL; 641105197Ssam } 642171133Sgnn IPSEC_ASSERT(m->m_len == sizeof (struct ip6_hdr), 643120585Ssam ("mbuf wrong size; len %u", m->m_len)); 644105197Ssam 645105197Ssam 646105197Ssam /* 647105197Ssam * grow the mbuf to accomodate the new IPv6 header. 648105197Ssam */ 649105197Ssam plen = m->m_pkthdr.len; 650105197Ssam if (M_LEADINGSPACE(m->m_next) < sizeof(struct ip6_hdr)) { 651105197Ssam struct mbuf *n; 652111119Simp MGET(n, M_DONTWAIT, MT_DATA); 653105197Ssam if (!n) { 654105197Ssam m_freem(m); 655105197Ssam return ENOBUFS; 656105197Ssam } 657105197Ssam n->m_len = sizeof(struct ip6_hdr); 658105197Ssam n->m_next = m->m_next; 659105197Ssam m->m_next = n; 660105197Ssam m->m_pkthdr.len += sizeof(struct ip6_hdr); 661105197Ssam oip6 = mtod(n, struct ip6_hdr *); 662105197Ssam } else { 663105197Ssam m->m_next->m_len += sizeof(struct ip6_hdr); 664105197Ssam m->m_next->m_data -= sizeof(struct ip6_hdr); 665105197Ssam m->m_pkthdr.len += sizeof(struct ip6_hdr); 666105197Ssam oip6 = mtod(m->m_next, struct ip6_hdr *); 667105197Ssam } 668105197Ssam ip6 = mtod(m, struct ip6_hdr *); 669113076Sdes bcopy((caddr_t)ip6, (caddr_t)oip6, sizeof(struct ip6_hdr)); 670105197Ssam 671105197Ssam /* Fake link-local scope-class addresses */ 672105197Ssam if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src)) 673105197Ssam oip6->ip6_src.s6_addr16[1] = 0; 674105197Ssam if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst)) 675105197Ssam oip6->ip6_dst.s6_addr16[1] = 0; 676105197Ssam 677105197Ssam /* construct new IPv6 header. see RFC 2401 5.1.2.2 */ 678105197Ssam /* ECN consideration. */ 679105197Ssam ip6_ecn_ingress(ip6_ipsec_ecn, &ip6->ip6_flow, &oip6->ip6_flow); 680105197Ssam if (plen < IPV6_MAXPACKET - sizeof(struct ip6_hdr)) 681105197Ssam ip6->ip6_plen = htons(plen); 682105197Ssam else { 683105197Ssam /* ip6->ip6_plen will be updated in ip6_output() */ 684105197Ssam } 685105197Ssam ip6->ip6_nxt = IPPROTO_IPV6; 686171133Sgnn ip6->ip6_src = sav->sah->saidx.src.sin6.sin6_addr; 687171133Sgnn ip6->ip6_dst = sav->sah->saidx.dst.sin6.sin6_addr; 688105197Ssam ip6->ip6_hlim = IPV6_DEFHLIM; 689105197Ssam 690105197Ssam /* XXX Should ip6_src be updated later ? */ 691105197Ssam 692105197Ssam return 0; 693105197Ssam} 694105197Ssam 695105197Ssam/* 696105197Ssam * IPsec output logic for IPv6, tunnel mode. 697105197Ssam */ 698105197Ssamint 699105197Ssamipsec6_output_tunnel(struct ipsec_output_state *state, struct secpolicy *sp, int flags) 700105197Ssam{ 701105197Ssam struct ip6_hdr *ip6; 702105197Ssam struct ipsecrequest *isr; 703105197Ssam struct secasindex saidx; 704105197Ssam int error; 705105197Ssam struct sockaddr_in6* dst6; 706105197Ssam struct mbuf *m; 707105197Ssam 708120585Ssam IPSEC_ASSERT(state != NULL, ("null state")); 709120585Ssam IPSEC_ASSERT(state->m != NULL, ("null m")); 710120585Ssam IPSEC_ASSERT(sp != NULL, ("null sp")); 711105197Ssam 712105197Ssam KEYDEBUG(KEYDEBUG_IPSEC_DATA, 713170122Sbz printf("%s: applied SP\n", __func__); 714105197Ssam kdebug_secpolicy(sp)); 715105197Ssam 716105197Ssam m = state->m; 717105197Ssam /* 718105197Ssam * transport mode ipsec (before the 1st tunnel mode) is already 719105197Ssam * processed by ipsec6_output_trans(). 720105197Ssam */ 721105197Ssam for (isr = sp->req; isr; isr = isr->next) { 722105197Ssam if (isr->saidx.mode == IPSEC_MODE_TUNNEL) 723105197Ssam break; 724105197Ssam } 725170123Sbz 726170123Sbz IPSECREQUEST_LOCK(isr); /* insure SA contents don't change */ 727105197Ssam isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error); 728177175Sbz if (isr == NULL) { 729177175Sbz if (error != 0) 730177175Sbz goto bad; 731177175Sbz return EJUSTRETURN; 732177175Sbz } 733105197Ssam 734174054Sbz#ifdef DEV_ENC 735181627Svanhu encif->if_opackets++; 736181627Svanhu encif->if_obytes += m->m_pkthdr.len; 737181627Svanhu 738174054Sbz /* pass the mbuf to enc0 for bpf processing */ 739174054Sbz ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_BEFORE); 740174054Sbz /* pass the mbuf to enc0 for packet filtering */ 741174054Sbz if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0) 742174054Sbz goto bad; 743174054Sbz#endif 744174054Sbz 745105197Ssam /* 746105197Ssam * There may be the case that SA status will be changed when 747105197Ssam * we are refering to one. So calling splsoftnet(). 748105197Ssam */ 749105197Ssam if (isr->saidx.mode == IPSEC_MODE_TUNNEL) { 750105197Ssam /* 751105197Ssam * build IPsec tunnel. 752105197Ssam */ 753105197Ssam /* XXX should be processed with other familiy */ 754105197Ssam if (isr->sav->sah->saidx.src.sa.sa_family != AF_INET6) { 755120585Ssam ipseclog((LOG_ERR, "%s: family mismatched between " 756120585Ssam "inner and outer, spi=%u\n", __func__, 757105197Ssam ntohl(isr->sav->spi))); 758171133Sgnn ipsec6stat.ips_out_inval++; 759105197Ssam error = EAFNOSUPPORT; 760105197Ssam goto bad; 761105197Ssam } 762105197Ssam 763105197Ssam m = ipsec6_splithdr(m); 764105197Ssam if (!m) { 765171133Sgnn ipsec6stat.ips_out_nomem++; 766105197Ssam error = ENOMEM; 767105197Ssam goto bad; 768105197Ssam } 769105197Ssam error = ipsec6_encapsulate(m, isr->sav); 770105197Ssam if (error) { 771105197Ssam m = NULL; 772105197Ssam goto bad; 773105197Ssam } 774105197Ssam ip6 = mtod(m, struct ip6_hdr *); 775105197Ssam 776105197Ssam state->ro = &isr->sav->sah->sa_route; 777105197Ssam state->dst = (struct sockaddr *)&state->ro->ro_dst; 778105197Ssam dst6 = (struct sockaddr_in6 *)state->dst; 779105197Ssam if (state->ro->ro_rt 780105197Ssam && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0 781105197Ssam || !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_dst))) { 782105197Ssam RTFREE(state->ro->ro_rt); 783105197Ssam state->ro->ro_rt = NULL; 784105197Ssam } 785105197Ssam if (state->ro->ro_rt == 0) { 786105197Ssam bzero(dst6, sizeof(*dst6)); 787105197Ssam dst6->sin6_family = AF_INET6; 788105197Ssam dst6->sin6_len = sizeof(*dst6); 789105197Ssam dst6->sin6_addr = ip6->ip6_dst; 790105197Ssam rtalloc(state->ro); 791105197Ssam } 792105197Ssam if (state->ro->ro_rt == 0) { 793105197Ssam ip6stat.ip6s_noroute++; 794171133Sgnn ipsec6stat.ips_out_noroute++; 795105197Ssam error = EHOSTUNREACH; 796105197Ssam goto bad; 797105197Ssam } 798105197Ssam 799105197Ssam /* adjust state->dst if tunnel endpoint is offlink */ 800105197Ssam if (state->ro->ro_rt->rt_flags & RTF_GATEWAY) { 801105197Ssam state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway; 802105197Ssam dst6 = (struct sockaddr_in6 *)state->dst; 803105197Ssam } 804105197Ssam } 805105197Ssam 806105197Ssam m = ipsec6_splithdr(m); 807105197Ssam if (!m) { 808171133Sgnn ipsec6stat.ips_out_nomem++; 809105197Ssam error = ENOMEM; 810105197Ssam goto bad; 811105197Ssam } 812105197Ssam ip6 = mtod(m, struct ip6_hdr *); 813174054Sbz 814174054Sbz#ifdef DEV_ENC 815174054Sbz /* pass the mbuf to enc0 for bpf processing */ 816174054Sbz ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_AFTER); 817174054Sbz /* pass the mbuf to enc0 for packet filtering */ 818174054Sbz if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0) 819174054Sbz goto bad; 820174054Sbz#endif 821174054Sbz 822170123Sbz error = (*isr->sav->tdb_xform->xf_output)(m, isr, NULL, 823105197Ssam sizeof (struct ip6_hdr), 824105197Ssam offsetof(struct ip6_hdr, ip6_nxt)); 825170123Sbz IPSECREQUEST_UNLOCK(isr); 826170123Sbz return error; 827105197Ssambad: 828170123Sbz if (isr) 829170123Sbz IPSECREQUEST_UNLOCK(isr); 830105197Ssam if (m) 831105197Ssam m_freem(m); 832105197Ssam state->m = NULL; 833105197Ssam return error; 834105197Ssam} 835105197Ssam#endif /*INET6*/ 836