1aaea26efSSam Leffler /*- 2aaea26efSSam Leffler * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting 3fcf59617SAndrey V. Elsukov * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org> 4aaea26efSSam Leffler * All rights reserved. 5aaea26efSSam Leffler * 6aaea26efSSam Leffler * Redistribution and use in source and binary forms, with or without 7aaea26efSSam Leffler * modification, are permitted provided that the following conditions 8aaea26efSSam Leffler * are met: 9aaea26efSSam Leffler * 1. Redistributions of source code must retain the above copyright 10aaea26efSSam Leffler * notice, this list of conditions and the following disclaimer. 11aaea26efSSam Leffler * 2. Redistributions in binary form must reproduce the above copyright 12aaea26efSSam Leffler * notice, this list of conditions and the following disclaimer in the 13aaea26efSSam Leffler * documentation and/or other materials provided with the distribution. 14aaea26efSSam Leffler * 15aaea26efSSam Leffler * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16aaea26efSSam Leffler * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17aaea26efSSam Leffler * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18aaea26efSSam Leffler * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19aaea26efSSam Leffler * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20aaea26efSSam Leffler * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21aaea26efSSam Leffler * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22aaea26efSSam Leffler * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23aaea26efSSam Leffler * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24aaea26efSSam Leffler * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25aaea26efSSam Leffler * SUCH DAMAGE. 26aaea26efSSam Leffler * 27aaea26efSSam Leffler * $FreeBSD$ 28aaea26efSSam Leffler */ 2988768458SSam Leffler 3088768458SSam Leffler /* 3188768458SSam Leffler * IPsec output processing. 3288768458SSam Leffler */ 3388768458SSam Leffler #include "opt_inet.h" 3488768458SSam Leffler #include "opt_inet6.h" 3588768458SSam Leffler #include "opt_ipsec.h" 36fcf59617SAndrey V. Elsukov #include "opt_sctp.h" 3788768458SSam Leffler 3888768458SSam Leffler #include <sys/param.h> 3988768458SSam Leffler #include <sys/systm.h> 4088768458SSam Leffler #include <sys/mbuf.h> 4188768458SSam Leffler #include <sys/domain.h> 4288768458SSam Leffler #include <sys/protosw.h> 4388768458SSam Leffler #include <sys/socket.h> 4488768458SSam Leffler #include <sys/errno.h> 45ef91a976SAndrey V. Elsukov #include <sys/hhook.h> 4688768458SSam Leffler #include <sys/syslog.h> 4788768458SSam Leffler 4888768458SSam Leffler #include <net/if.h> 49ef91a976SAndrey V. Elsukov #include <net/if_enc.h> 5076039bc8SGleb Smirnoff #include <net/if_var.h> 51eddfbb76SRobert Watson #include <net/vnet.h> 5288768458SSam Leffler 5388768458SSam Leffler #include <netinet/in.h> 5488768458SSam Leffler #include <netinet/in_systm.h> 5588768458SSam Leffler #include <netinet/ip.h> 5688768458SSam Leffler #include <netinet/ip_var.h> 5788768458SSam Leffler #include <netinet/in_var.h> 5888768458SSam Leffler #include <netinet/ip_ecn.h> 5988768458SSam Leffler #ifdef INET6 6088768458SSam Leffler #include <netinet6/ip6_ecn.h> 6188768458SSam Leffler #endif 6288768458SSam Leffler 6388768458SSam Leffler #include <netinet/ip6.h> 6488768458SSam Leffler #ifdef INET6 6588768458SSam Leffler #include <netinet6/ip6_var.h> 6661f37615SAndrey V. Elsukov #include <netinet6/scope6_var.h> 6788768458SSam Leffler #endif 6888768458SSam Leffler #include <netinet/in_pcb.h> 6988768458SSam Leffler #ifdef INET6 7088768458SSam Leffler #include <netinet/icmp6.h> 7188768458SSam Leffler #endif 72fcf59617SAndrey V. Elsukov #ifdef SCTP 73fcf59617SAndrey V. Elsukov #include <netinet/sctp_crc32.h> 74fcf59617SAndrey V. Elsukov #endif 7588768458SSam Leffler 76fcf59617SAndrey V. Elsukov #include <netinet/udp.h> 77fcf59617SAndrey V. Elsukov #include <netipsec/ah.h> 78fcf59617SAndrey V. Elsukov #include <netipsec/esp.h> 7988768458SSam Leffler #include <netipsec/ipsec.h> 8088768458SSam Leffler #ifdef INET6 8188768458SSam Leffler #include <netipsec/ipsec6.h> 8288768458SSam Leffler #endif 8388768458SSam Leffler #include <netipsec/ah_var.h> 8488768458SSam Leffler #include <netipsec/esp_var.h> 8588768458SSam Leffler #include <netipsec/ipcomp_var.h> 8688768458SSam Leffler 8788768458SSam Leffler #include <netipsec/xform.h> 8888768458SSam Leffler 8988768458SSam Leffler #include <netipsec/key.h> 9088768458SSam Leffler #include <netipsec/keydb.h> 9188768458SSam Leffler #include <netipsec/key_debug.h> 9288768458SSam Leffler 9388768458SSam Leffler #include <machine/in_cksum.h> 9488768458SSam Leffler 95fcf59617SAndrey V. Elsukov #define IPSEC_OSTAT_INC(proto, name) do { \ 96fcf59617SAndrey V. Elsukov if ((proto) == IPPROTO_ESP) \ 97fcf59617SAndrey V. Elsukov ESPSTAT_INC(esps_##name); \ 98fcf59617SAndrey V. Elsukov else if ((proto) == IPPROTO_AH)\ 99fcf59617SAndrey V. Elsukov AHSTAT_INC(ahs_##name); \ 100fcf59617SAndrey V. Elsukov else \ 101fcf59617SAndrey V. Elsukov IPCOMPSTAT_INC(ipcomps_##name); \ 102fcf59617SAndrey V. Elsukov } while (0) 103fcf59617SAndrey V. Elsukov 104fcf59617SAndrey V. Elsukov static int ipsec_encap(struct mbuf **mp, struct secasindex *saidx); 105fcf59617SAndrey V. Elsukov 106fcf59617SAndrey V. Elsukov #ifdef INET 107fcf59617SAndrey V. Elsukov static struct secasvar * 108fcf59617SAndrey V. Elsukov ipsec4_allocsa(struct mbuf *m, struct secpolicy *sp, u_int *pidx, int *error) 109fcf59617SAndrey V. Elsukov { 110fcf59617SAndrey V. Elsukov struct secasindex *saidx, tmpsaidx; 111fcf59617SAndrey V. Elsukov struct ipsecrequest *isr; 112fcf59617SAndrey V. Elsukov struct sockaddr_in *sin; 113fcf59617SAndrey V. Elsukov struct secasvar *sav; 114fcf59617SAndrey V. Elsukov struct ip *ip; 115fcf59617SAndrey V. Elsukov 116fcf59617SAndrey V. Elsukov /* 117fcf59617SAndrey V. Elsukov * Check system global policy controls. 118fcf59617SAndrey V. Elsukov */ 119fcf59617SAndrey V. Elsukov next: 120fcf59617SAndrey V. Elsukov isr = sp->req[*pidx]; 121fcf59617SAndrey V. Elsukov if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) || 122fcf59617SAndrey V. Elsukov (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) || 123fcf59617SAndrey V. Elsukov (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) { 124fcf59617SAndrey V. Elsukov DPRINTF(("%s: IPsec outbound packet dropped due" 125fcf59617SAndrey V. Elsukov " to policy (check your sysctls)\n", __func__)); 126fcf59617SAndrey V. Elsukov IPSEC_OSTAT_INC(isr->saidx.proto, pdrops); 127fcf59617SAndrey V. Elsukov *error = EHOSTUNREACH; 128fcf59617SAndrey V. Elsukov return (NULL); 129fcf59617SAndrey V. Elsukov } 130fcf59617SAndrey V. Elsukov /* 131fcf59617SAndrey V. Elsukov * Craft SA index to search for proper SA. Note that 132fcf59617SAndrey V. Elsukov * we only initialize unspecified SA peers for transport 133fcf59617SAndrey V. Elsukov * mode; for tunnel mode they must already be filled in. 134fcf59617SAndrey V. Elsukov */ 135fcf59617SAndrey V. Elsukov if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) { 136fcf59617SAndrey V. Elsukov saidx = &tmpsaidx; 137fcf59617SAndrey V. Elsukov *saidx = isr->saidx; 138fcf59617SAndrey V. Elsukov ip = mtod(m, struct ip *); 139fcf59617SAndrey V. Elsukov if (saidx->src.sa.sa_len == 0) { 140fcf59617SAndrey V. Elsukov sin = &saidx->src.sin; 141fcf59617SAndrey V. Elsukov sin->sin_len = sizeof(*sin); 142fcf59617SAndrey V. Elsukov sin->sin_family = AF_INET; 143fcf59617SAndrey V. Elsukov sin->sin_port = IPSEC_PORT_ANY; 144fcf59617SAndrey V. Elsukov sin->sin_addr = ip->ip_src; 145fcf59617SAndrey V. Elsukov } 146fcf59617SAndrey V. Elsukov if (saidx->dst.sa.sa_len == 0) { 147fcf59617SAndrey V. Elsukov sin = &saidx->dst.sin; 148fcf59617SAndrey V. Elsukov sin->sin_len = sizeof(*sin); 149fcf59617SAndrey V. Elsukov sin->sin_family = AF_INET; 150fcf59617SAndrey V. Elsukov sin->sin_port = IPSEC_PORT_ANY; 151fcf59617SAndrey V. Elsukov sin->sin_addr = ip->ip_dst; 152fcf59617SAndrey V. Elsukov } 153fcf59617SAndrey V. Elsukov } else 154fcf59617SAndrey V. Elsukov saidx = &sp->req[*pidx]->saidx; 155fcf59617SAndrey V. Elsukov /* 156fcf59617SAndrey V. Elsukov * Lookup SA and validate it. 157fcf59617SAndrey V. Elsukov */ 158fcf59617SAndrey V. Elsukov sav = key_allocsa_policy(sp, saidx, error); 159fcf59617SAndrey V. Elsukov if (sav == NULL) { 160fcf59617SAndrey V. Elsukov IPSECSTAT_INC(ips_out_nosa); 161fcf59617SAndrey V. Elsukov if (*error != 0) 162fcf59617SAndrey V. Elsukov return (NULL); 163fcf59617SAndrey V. Elsukov if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) { 164fcf59617SAndrey V. Elsukov /* 165fcf59617SAndrey V. Elsukov * We have no SA and policy that doesn't require 166fcf59617SAndrey V. Elsukov * this IPsec transform, thus we can continue w/o 167fcf59617SAndrey V. Elsukov * IPsec processing, i.e. return EJUSTRETURN. 168fcf59617SAndrey V. Elsukov * But first check if there is some bundled transform. 169fcf59617SAndrey V. Elsukov */ 170fcf59617SAndrey V. Elsukov if (sp->tcount > ++(*pidx)) 171fcf59617SAndrey V. Elsukov goto next; 172fcf59617SAndrey V. Elsukov *error = EJUSTRETURN; 173fcf59617SAndrey V. Elsukov } 174fcf59617SAndrey V. Elsukov return (NULL); 175fcf59617SAndrey V. Elsukov } 176fcf59617SAndrey V. Elsukov IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform")); 177fcf59617SAndrey V. Elsukov return (sav); 178fcf59617SAndrey V. Elsukov } 179fcf59617SAndrey V. Elsukov 180fcf59617SAndrey V. Elsukov /* 181fcf59617SAndrey V. Elsukov * IPsec output logic for IPv4. 182fcf59617SAndrey V. Elsukov */ 183fcf59617SAndrey V. Elsukov static int 184*1a01e0e7SAndrey V. Elsukov ipsec4_perform_request(struct mbuf *m, struct secpolicy *sp, 185*1a01e0e7SAndrey V. Elsukov struct inpcb *inp, u_int idx) 186fcf59617SAndrey V. Elsukov { 187fcf59617SAndrey V. Elsukov struct ipsec_ctx_data ctx; 188fcf59617SAndrey V. Elsukov union sockaddr_union *dst; 189fcf59617SAndrey V. Elsukov struct secasvar *sav; 190fcf59617SAndrey V. Elsukov struct ip *ip; 191fcf59617SAndrey V. Elsukov int error, i, off; 192fcf59617SAndrey V. Elsukov 193fcf59617SAndrey V. Elsukov IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx)); 194fcf59617SAndrey V. Elsukov 195fcf59617SAndrey V. Elsukov /* 196fcf59617SAndrey V. Elsukov * We hold the reference to SP. Content of SP couldn't be changed. 197fcf59617SAndrey V. Elsukov * Craft secasindex and do lookup for suitable SA. 198fcf59617SAndrey V. Elsukov * Then do encapsulation if needed and call xform's output. 199fcf59617SAndrey V. Elsukov * We need to store SP in the xform callback parameters. 200fcf59617SAndrey V. Elsukov * In xform callback we will extract SP and it can be used to 201fcf59617SAndrey V. Elsukov * determine next transform. At the end of transform we can 202fcf59617SAndrey V. Elsukov * release reference to SP. 203fcf59617SAndrey V. Elsukov */ 204fcf59617SAndrey V. Elsukov sav = ipsec4_allocsa(m, sp, &idx, &error); 205fcf59617SAndrey V. Elsukov if (sav == NULL) { 206fcf59617SAndrey V. Elsukov if (error == EJUSTRETURN) { /* No IPsec required */ 207fcf59617SAndrey V. Elsukov key_freesp(&sp); 208fcf59617SAndrey V. Elsukov return (error); 209fcf59617SAndrey V. Elsukov } 210fcf59617SAndrey V. Elsukov goto bad; 211fcf59617SAndrey V. Elsukov } 212fcf59617SAndrey V. Elsukov /* 213fcf59617SAndrey V. Elsukov * XXXAE: most likely ip_sum at this point is wrong. 214fcf59617SAndrey V. Elsukov */ 215*1a01e0e7SAndrey V. Elsukov IPSEC_INIT_CTX(&ctx, &m, inp, sav, AF_INET, IPSEC_ENC_BEFORE); 216fcf59617SAndrey V. Elsukov if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0) 217fcf59617SAndrey V. Elsukov goto bad; 218fcf59617SAndrey V. Elsukov 219fcf59617SAndrey V. Elsukov ip = mtod(m, struct ip *); 220fcf59617SAndrey V. Elsukov dst = &sav->sah->saidx.dst; 221fcf59617SAndrey V. Elsukov /* Do the appropriate encapsulation, if necessary */ 222fcf59617SAndrey V. Elsukov if (sp->req[idx]->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */ 223fcf59617SAndrey V. Elsukov dst->sa.sa_family != AF_INET || /* PF mismatch */ 224fcf59617SAndrey V. Elsukov (dst->sa.sa_family == AF_INET && /* Proxy */ 225fcf59617SAndrey V. Elsukov dst->sin.sin_addr.s_addr != INADDR_ANY && 226fcf59617SAndrey V. Elsukov dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) { 227fcf59617SAndrey V. Elsukov /* Fix IPv4 header checksum and length */ 228fcf59617SAndrey V. Elsukov ip->ip_len = htons(m->m_pkthdr.len); 229fcf59617SAndrey V. Elsukov ip->ip_sum = 0; 230fcf59617SAndrey V. Elsukov ip->ip_sum = in_cksum(m, ip->ip_hl << 2); 231fcf59617SAndrey V. Elsukov error = ipsec_encap(&m, &sav->sah->saidx); 232fcf59617SAndrey V. Elsukov if (error != 0) { 2337f1f6591SAndrey V. Elsukov DPRINTF(("%s: encapsulation for SPI 0x%08x failed " 2347f1f6591SAndrey V. Elsukov "with error %d\n", __func__, ntohl(sav->spi), 2357f1f6591SAndrey V. Elsukov error)); 236fcf59617SAndrey V. Elsukov /* XXXAE: IPSEC_OSTAT_INC(tunnel); */ 237fcf59617SAndrey V. Elsukov goto bad; 238fcf59617SAndrey V. Elsukov } 239*1a01e0e7SAndrey V. Elsukov inp = NULL; 240fcf59617SAndrey V. Elsukov } 241fcf59617SAndrey V. Elsukov 242*1a01e0e7SAndrey V. Elsukov IPSEC_INIT_CTX(&ctx, &m, inp, sav, dst->sa.sa_family, IPSEC_ENC_AFTER); 243fcf59617SAndrey V. Elsukov if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0) 244fcf59617SAndrey V. Elsukov goto bad; 245fcf59617SAndrey V. Elsukov 246fcf59617SAndrey V. Elsukov /* 247fcf59617SAndrey V. Elsukov * Dispatch to the appropriate IPsec transform logic. The 248fcf59617SAndrey V. Elsukov * packet will be returned for transmission after crypto 249fcf59617SAndrey V. Elsukov * processing, etc. are completed. 250fcf59617SAndrey V. Elsukov * 251fcf59617SAndrey V. Elsukov * NB: m & sav are ``passed to caller'' who's responsible for 252fcf59617SAndrey V. Elsukov * reclaiming their resources. 253fcf59617SAndrey V. Elsukov */ 254fcf59617SAndrey V. Elsukov switch(dst->sa.sa_family) { 255fcf59617SAndrey V. Elsukov case AF_INET: 256fcf59617SAndrey V. Elsukov ip = mtod(m, struct ip *); 257fcf59617SAndrey V. Elsukov i = ip->ip_hl << 2; 258fcf59617SAndrey V. Elsukov off = offsetof(struct ip, ip_p); 259fcf59617SAndrey V. Elsukov break; 260fcf59617SAndrey V. Elsukov #ifdef INET6 261fcf59617SAndrey V. Elsukov case AF_INET6: 262fcf59617SAndrey V. Elsukov i = sizeof(struct ip6_hdr); 263fcf59617SAndrey V. Elsukov off = offsetof(struct ip6_hdr, ip6_nxt); 264fcf59617SAndrey V. Elsukov break; 265fcf59617SAndrey V. Elsukov #endif /* INET6 */ 266fcf59617SAndrey V. Elsukov default: 267fcf59617SAndrey V. Elsukov DPRINTF(("%s: unsupported protocol family %u\n", 268fcf59617SAndrey V. Elsukov __func__, dst->sa.sa_family)); 269fcf59617SAndrey V. Elsukov error = EPFNOSUPPORT; 270fcf59617SAndrey V. Elsukov IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf); 271fcf59617SAndrey V. Elsukov goto bad; 272fcf59617SAndrey V. Elsukov } 273fcf59617SAndrey V. Elsukov error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off); 274fcf59617SAndrey V. Elsukov return (error); 275fcf59617SAndrey V. Elsukov bad: 276fcf59617SAndrey V. Elsukov IPSECSTAT_INC(ips_out_inval); 277fcf59617SAndrey V. Elsukov if (m != NULL) 278fcf59617SAndrey V. Elsukov m_freem(m); 279fcf59617SAndrey V. Elsukov if (sav != NULL) 280fcf59617SAndrey V. Elsukov key_freesav(&sav); 281fcf59617SAndrey V. Elsukov key_freesp(&sp); 282fcf59617SAndrey V. Elsukov return (error); 283fcf59617SAndrey V. Elsukov } 2847b495c44SVANHULLEBUS Yvan 28588768458SSam Leffler int 286fcf59617SAndrey V. Elsukov ipsec4_process_packet(struct mbuf *m, struct secpolicy *sp, 287fcf59617SAndrey V. Elsukov struct inpcb *inp) 28888768458SSam Leffler { 289fcf59617SAndrey V. Elsukov 290*1a01e0e7SAndrey V. Elsukov return (ipsec4_perform_request(m, sp, inp, 0)); 291fcf59617SAndrey V. Elsukov } 292fcf59617SAndrey V. Elsukov 293fcf59617SAndrey V. Elsukov static int 294fcf59617SAndrey V. Elsukov ipsec4_common_output(struct mbuf *m, struct inpcb *inp, int forwarding) 295fcf59617SAndrey V. Elsukov { 296fcf59617SAndrey V. Elsukov struct secpolicy *sp; 29788768458SSam Leffler int error; 29888768458SSam Leffler 299fcf59617SAndrey V. Elsukov /* Lookup for the corresponding outbound security policy */ 300fcf59617SAndrey V. Elsukov sp = ipsec4_checkpolicy(m, inp, &error); 301fcf59617SAndrey V. Elsukov if (sp == NULL) { 302fcf59617SAndrey V. Elsukov if (error == -EINVAL) { 303fcf59617SAndrey V. Elsukov /* Discarded by policy. */ 304fcf59617SAndrey V. Elsukov m_freem(m); 305fcf59617SAndrey V. Elsukov return (EACCES); 306fcf59617SAndrey V. Elsukov } 307fcf59617SAndrey V. Elsukov return (0); /* No IPsec required. */ 308fcf59617SAndrey V. Elsukov } 309fcf59617SAndrey V. Elsukov 310fcf59617SAndrey V. Elsukov /* 311fcf59617SAndrey V. Elsukov * Usually we have to have tunnel mode IPsec security policy 312fcf59617SAndrey V. Elsukov * when we are forwarding a packet. Otherwise we could not handle 313fcf59617SAndrey V. Elsukov * encrypted replies, because they are not destined for us. But 314fcf59617SAndrey V. Elsukov * some users are doing source address translation for forwarded 315fcf59617SAndrey V. Elsukov * packets, and thus, even if they are forwarded, the replies will 316fcf59617SAndrey V. Elsukov * return back to us. 317fcf59617SAndrey V. Elsukov */ 318fcf59617SAndrey V. Elsukov if (!forwarding) { 319fcf59617SAndrey V. Elsukov /* 320fcf59617SAndrey V. Elsukov * Do delayed checksums now because we send before 321fcf59617SAndrey V. Elsukov * this is done in the normal processing path. 322fcf59617SAndrey V. Elsukov */ 323fcf59617SAndrey V. Elsukov if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 324fcf59617SAndrey V. Elsukov in_delayed_cksum(m); 325fcf59617SAndrey V. Elsukov m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 326fcf59617SAndrey V. Elsukov } 327fcf59617SAndrey V. Elsukov #ifdef SCTP 328fcf59617SAndrey V. Elsukov if (m->m_pkthdr.csum_flags & CSUM_SCTP) { 329fcf59617SAndrey V. Elsukov struct ip *ip = mtod(m, struct ip *); 330fcf59617SAndrey V. Elsukov 331fcf59617SAndrey V. Elsukov sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2)); 332fcf59617SAndrey V. Elsukov m->m_pkthdr.csum_flags &= ~CSUM_SCTP; 333fcf59617SAndrey V. Elsukov } 334fcf59617SAndrey V. Elsukov #endif 335fcf59617SAndrey V. Elsukov } 336fcf59617SAndrey V. Elsukov /* NB: callee frees mbuf and releases reference to SP */ 337fcf59617SAndrey V. Elsukov error = ipsec4_process_packet(m, sp, inp); 338fcf59617SAndrey V. Elsukov if (error == EJUSTRETURN) { 339fcf59617SAndrey V. Elsukov /* 340fcf59617SAndrey V. Elsukov * We had a SP with a level of 'use' and no SA. We 341fcf59617SAndrey V. Elsukov * will just continue to process the packet without 342fcf59617SAndrey V. Elsukov * IPsec processing and return without error. 343fcf59617SAndrey V. Elsukov */ 344fcf59617SAndrey V. Elsukov return (0); 345fcf59617SAndrey V. Elsukov } 346fcf59617SAndrey V. Elsukov if (error == 0) 347fcf59617SAndrey V. Elsukov return (EINPROGRESS); /* consumed by IPsec */ 348fcf59617SAndrey V. Elsukov return (error); 349fcf59617SAndrey V. Elsukov } 350fcf59617SAndrey V. Elsukov 351fcf59617SAndrey V. Elsukov /* 352fcf59617SAndrey V. Elsukov * IPSEC_OUTPUT() method implementation for IPv4. 353fcf59617SAndrey V. Elsukov * 0 - no IPsec handling needed 354fcf59617SAndrey V. Elsukov * other values - mbuf consumed by IPsec. 355fcf59617SAndrey V. Elsukov */ 356fcf59617SAndrey V. Elsukov int 357fcf59617SAndrey V. Elsukov ipsec4_output(struct mbuf *m, struct inpcb *inp) 358fcf59617SAndrey V. Elsukov { 359fcf59617SAndrey V. Elsukov 360fcf59617SAndrey V. Elsukov /* 361fcf59617SAndrey V. Elsukov * If the packet is resubmitted to ip_output (e.g. after 362fcf59617SAndrey V. Elsukov * AH, ESP, etc. processing), there will be a tag to bypass 363fcf59617SAndrey V. Elsukov * the lookup and related policy checking. 364fcf59617SAndrey V. Elsukov */ 365fcf59617SAndrey V. Elsukov if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL) 366fcf59617SAndrey V. Elsukov return (0); 367fcf59617SAndrey V. Elsukov 368fcf59617SAndrey V. Elsukov return (ipsec4_common_output(m, inp, 0)); 369fcf59617SAndrey V. Elsukov } 370fcf59617SAndrey V. Elsukov 371fcf59617SAndrey V. Elsukov /* 372fcf59617SAndrey V. Elsukov * IPSEC_FORWARD() method implementation for IPv4. 373fcf59617SAndrey V. Elsukov * 0 - no IPsec handling needed 374fcf59617SAndrey V. Elsukov * other values - mbuf consumed by IPsec. 375fcf59617SAndrey V. Elsukov */ 376fcf59617SAndrey V. Elsukov int 377fcf59617SAndrey V. Elsukov ipsec4_forward(struct mbuf *m) 378fcf59617SAndrey V. Elsukov { 379fcf59617SAndrey V. Elsukov 380fcf59617SAndrey V. Elsukov /* 381fcf59617SAndrey V. Elsukov * Check if this packet has an active inbound SP and needs to be 382fcf59617SAndrey V. Elsukov * dropped instead of forwarded. 383fcf59617SAndrey V. Elsukov */ 384fcf59617SAndrey V. Elsukov if (ipsec4_in_reject(m, NULL) != 0) { 385fcf59617SAndrey V. Elsukov m_freem(m); 386fcf59617SAndrey V. Elsukov return (EACCES); 387fcf59617SAndrey V. Elsukov } 388fcf59617SAndrey V. Elsukov return (ipsec4_common_output(m, NULL, 1)); 389fcf59617SAndrey V. Elsukov } 390fcf59617SAndrey V. Elsukov #endif 391fcf59617SAndrey V. Elsukov 392fcf59617SAndrey V. Elsukov #ifdef INET6 393fcf59617SAndrey V. Elsukov static int 394fcf59617SAndrey V. Elsukov in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa, 395fcf59617SAndrey V. Elsukov const struct in6_addr *ia) 396fcf59617SAndrey V. Elsukov { 397fcf59617SAndrey V. Elsukov struct in6_addr ia2; 398fcf59617SAndrey V. Elsukov 399fcf59617SAndrey V. Elsukov if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr)) { 400fcf59617SAndrey V. Elsukov memcpy(&ia2, &sa->sin6_addr, sizeof(ia2)); 401fcf59617SAndrey V. Elsukov ia2.s6_addr16[1] = htons(sa->sin6_scope_id); 402fcf59617SAndrey V. Elsukov return (IN6_ARE_ADDR_EQUAL(ia, &ia2)); 403fcf59617SAndrey V. Elsukov } 404fcf59617SAndrey V. Elsukov return (IN6_ARE_ADDR_EQUAL(&sa->sin6_addr, ia)); 405fcf59617SAndrey V. Elsukov } 406fcf59617SAndrey V. Elsukov 407fcf59617SAndrey V. Elsukov static struct secasvar * 408fcf59617SAndrey V. Elsukov ipsec6_allocsa(struct mbuf *m, struct secpolicy *sp, u_int *pidx, int *error) 409fcf59617SAndrey V. Elsukov { 410fcf59617SAndrey V. Elsukov struct secasindex *saidx, tmpsaidx; 411fcf59617SAndrey V. Elsukov struct ipsecrequest *isr; 412fcf59617SAndrey V. Elsukov struct sockaddr_in6 *sin6; 413fcf59617SAndrey V. Elsukov struct secasvar *sav; 414fcf59617SAndrey V. Elsukov struct ip6_hdr *ip6; 415fcf59617SAndrey V. Elsukov 416fcf59617SAndrey V. Elsukov /* 417fcf59617SAndrey V. Elsukov * Check system global policy controls. 418fcf59617SAndrey V. Elsukov */ 419fcf59617SAndrey V. Elsukov next: 420fcf59617SAndrey V. Elsukov isr = sp->req[*pidx]; 421fcf59617SAndrey V. Elsukov if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) || 422fcf59617SAndrey V. Elsukov (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) || 423fcf59617SAndrey V. Elsukov (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) { 424fcf59617SAndrey V. Elsukov DPRINTF(("%s: IPsec outbound packet dropped due" 425fcf59617SAndrey V. Elsukov " to policy (check your sysctls)\n", __func__)); 426fcf59617SAndrey V. Elsukov IPSEC_OSTAT_INC(isr->saidx.proto, pdrops); 427fcf59617SAndrey V. Elsukov *error = EHOSTUNREACH; 428fcf59617SAndrey V. Elsukov return (NULL); 429fcf59617SAndrey V. Elsukov } 430fcf59617SAndrey V. Elsukov /* 431fcf59617SAndrey V. Elsukov * Craft SA index to search for proper SA. Note that 432fcf59617SAndrey V. Elsukov * we only fillin unspecified SA peers for transport 433fcf59617SAndrey V. Elsukov * mode; for tunnel mode they must already be filled in. 434fcf59617SAndrey V. Elsukov */ 435fcf59617SAndrey V. Elsukov if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) { 436fcf59617SAndrey V. Elsukov saidx = &tmpsaidx; 437fcf59617SAndrey V. Elsukov *saidx = isr->saidx; 438fcf59617SAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 439fcf59617SAndrey V. Elsukov if (saidx->src.sin6.sin6_len == 0) { 440fcf59617SAndrey V. Elsukov sin6 = (struct sockaddr_in6 *)&saidx->src; 441fcf59617SAndrey V. Elsukov sin6->sin6_len = sizeof(*sin6); 442fcf59617SAndrey V. Elsukov sin6->sin6_family = AF_INET6; 443fcf59617SAndrey V. Elsukov sin6->sin6_port = IPSEC_PORT_ANY; 444fcf59617SAndrey V. Elsukov sin6->sin6_addr = ip6->ip6_src; 445fcf59617SAndrey V. Elsukov if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) { 446fcf59617SAndrey V. Elsukov /* fix scope id for comparing SPD */ 447fcf59617SAndrey V. Elsukov sin6->sin6_addr.s6_addr16[1] = 0; 448fcf59617SAndrey V. Elsukov sin6->sin6_scope_id = 449fcf59617SAndrey V. Elsukov ntohs(ip6->ip6_src.s6_addr16[1]); 450fcf59617SAndrey V. Elsukov } 451fcf59617SAndrey V. Elsukov } 452fcf59617SAndrey V. Elsukov if (saidx->dst.sin6.sin6_len == 0) { 453fcf59617SAndrey V. Elsukov sin6 = (struct sockaddr_in6 *)&saidx->dst; 454fcf59617SAndrey V. Elsukov sin6->sin6_len = sizeof(*sin6); 455fcf59617SAndrey V. Elsukov sin6->sin6_family = AF_INET6; 456fcf59617SAndrey V. Elsukov sin6->sin6_port = IPSEC_PORT_ANY; 457fcf59617SAndrey V. Elsukov sin6->sin6_addr = ip6->ip6_dst; 458fcf59617SAndrey V. Elsukov if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) { 459fcf59617SAndrey V. Elsukov /* fix scope id for comparing SPD */ 460fcf59617SAndrey V. Elsukov sin6->sin6_addr.s6_addr16[1] = 0; 461fcf59617SAndrey V. Elsukov sin6->sin6_scope_id = 462fcf59617SAndrey V. Elsukov ntohs(ip6->ip6_dst.s6_addr16[1]); 463fcf59617SAndrey V. Elsukov } 464fcf59617SAndrey V. Elsukov } 465fcf59617SAndrey V. Elsukov } else 466fcf59617SAndrey V. Elsukov saidx = &sp->req[*pidx]->saidx; 467fcf59617SAndrey V. Elsukov /* 468fcf59617SAndrey V. Elsukov * Lookup SA and validate it. 469fcf59617SAndrey V. Elsukov */ 470fcf59617SAndrey V. Elsukov sav = key_allocsa_policy(sp, saidx, error); 471fcf59617SAndrey V. Elsukov if (sav == NULL) { 472fcf59617SAndrey V. Elsukov IPSEC6STAT_INC(ips_out_nosa); 473fcf59617SAndrey V. Elsukov if (*error != 0) 474fcf59617SAndrey V. Elsukov return (NULL); 475fcf59617SAndrey V. Elsukov if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) { 476fcf59617SAndrey V. Elsukov /* 477fcf59617SAndrey V. Elsukov * We have no SA and policy that doesn't require 478fcf59617SAndrey V. Elsukov * this IPsec transform, thus we can continue w/o 479fcf59617SAndrey V. Elsukov * IPsec processing, i.e. return EJUSTRETURN. 480fcf59617SAndrey V. Elsukov * But first check if there is some bundled transform. 481fcf59617SAndrey V. Elsukov */ 482fcf59617SAndrey V. Elsukov if (sp->tcount > ++(*pidx)) 483fcf59617SAndrey V. Elsukov goto next; 484fcf59617SAndrey V. Elsukov *error = EJUSTRETURN; 485fcf59617SAndrey V. Elsukov } 486fcf59617SAndrey V. Elsukov return (NULL); 487fcf59617SAndrey V. Elsukov } 488fcf59617SAndrey V. Elsukov IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform")); 489fcf59617SAndrey V. Elsukov return (sav); 490fcf59617SAndrey V. Elsukov } 491fcf59617SAndrey V. Elsukov 492fcf59617SAndrey V. Elsukov /* 493fcf59617SAndrey V. Elsukov * IPsec output logic for IPv6. 494fcf59617SAndrey V. Elsukov */ 495fcf59617SAndrey V. Elsukov static int 496*1a01e0e7SAndrey V. Elsukov ipsec6_perform_request(struct mbuf *m, struct secpolicy *sp, 497*1a01e0e7SAndrey V. Elsukov struct inpcb *inp, u_int idx) 498fcf59617SAndrey V. Elsukov { 499fcf59617SAndrey V. Elsukov struct ipsec_ctx_data ctx; 500fcf59617SAndrey V. Elsukov union sockaddr_union *dst; 501fcf59617SAndrey V. Elsukov struct secasvar *sav; 502fcf59617SAndrey V. Elsukov struct ip6_hdr *ip6; 503fcf59617SAndrey V. Elsukov int error, i, off; 504fcf59617SAndrey V. Elsukov 505fcf59617SAndrey V. Elsukov IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx)); 506fcf59617SAndrey V. Elsukov 507fcf59617SAndrey V. Elsukov sav = ipsec6_allocsa(m, sp, &idx, &error); 508fcf59617SAndrey V. Elsukov if (sav == NULL) { 509fcf59617SAndrey V. Elsukov if (error == EJUSTRETURN) { /* No IPsec required */ 510fcf59617SAndrey V. Elsukov key_freesp(&sp); 511fcf59617SAndrey V. Elsukov return (error); 512fcf59617SAndrey V. Elsukov } 513fcf59617SAndrey V. Elsukov goto bad; 514fcf59617SAndrey V. Elsukov } 515fcf59617SAndrey V. Elsukov 516fcf59617SAndrey V. Elsukov /* Fix IP length in case if it is not set yet. */ 517fcf59617SAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 518fcf59617SAndrey V. Elsukov ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 519fcf59617SAndrey V. Elsukov 520*1a01e0e7SAndrey V. Elsukov IPSEC_INIT_CTX(&ctx, &m, inp, sav, AF_INET6, IPSEC_ENC_BEFORE); 521fcf59617SAndrey V. Elsukov if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0) 522fcf59617SAndrey V. Elsukov goto bad; 523fcf59617SAndrey V. Elsukov 524fcf59617SAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); /* pfil can change mbuf */ 525fcf59617SAndrey V. Elsukov dst = &sav->sah->saidx.dst; 526fcf59617SAndrey V. Elsukov 527fcf59617SAndrey V. Elsukov /* Do the appropriate encapsulation, if necessary */ 528fcf59617SAndrey V. Elsukov if (sp->req[idx]->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */ 529fcf59617SAndrey V. Elsukov dst->sa.sa_family != AF_INET6 || /* PF mismatch */ 530fcf59617SAndrey V. Elsukov ((dst->sa.sa_family == AF_INET6) && 531fcf59617SAndrey V. Elsukov (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) && 532fcf59617SAndrey V. Elsukov (!in6_sa_equal_addrwithscope(&dst->sin6, &ip6->ip6_dst)))) { 533fcf59617SAndrey V. Elsukov if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) { 534fcf59617SAndrey V. Elsukov /* No jumbogram support. */ 535fcf59617SAndrey V. Elsukov error = ENXIO; /*XXX*/ 536fcf59617SAndrey V. Elsukov goto bad; 537fcf59617SAndrey V. Elsukov } 538fcf59617SAndrey V. Elsukov error = ipsec_encap(&m, &sav->sah->saidx); 539fcf59617SAndrey V. Elsukov if (error != 0) { 5407f1f6591SAndrey V. Elsukov DPRINTF(("%s: encapsulation for SPI 0x%08x failed " 5417f1f6591SAndrey V. Elsukov "with error %d\n", __func__, ntohl(sav->spi), 5427f1f6591SAndrey V. Elsukov error)); 543fcf59617SAndrey V. Elsukov /* XXXAE: IPSEC_OSTAT_INC(tunnel); */ 544fcf59617SAndrey V. Elsukov goto bad; 545fcf59617SAndrey V. Elsukov } 546*1a01e0e7SAndrey V. Elsukov inp = NULL; 547fcf59617SAndrey V. Elsukov } 548fcf59617SAndrey V. Elsukov 549*1a01e0e7SAndrey V. Elsukov IPSEC_INIT_CTX(&ctx, &m, inp, sav, dst->sa.sa_family, IPSEC_ENC_AFTER); 550fcf59617SAndrey V. Elsukov if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0) 551fcf59617SAndrey V. Elsukov goto bad; 552fcf59617SAndrey V. Elsukov 553fcf59617SAndrey V. Elsukov switch(dst->sa.sa_family) { 554fcf59617SAndrey V. Elsukov #ifdef INET 555fcf59617SAndrey V. Elsukov case AF_INET: 556fcf59617SAndrey V. Elsukov { 557fcf59617SAndrey V. Elsukov struct ip *ip; 558fcf59617SAndrey V. Elsukov ip = mtod(m, struct ip *); 559fcf59617SAndrey V. Elsukov i = ip->ip_hl << 2; 560fcf59617SAndrey V. Elsukov off = offsetof(struct ip, ip_p); 561fcf59617SAndrey V. Elsukov } 562fcf59617SAndrey V. Elsukov break; 563fcf59617SAndrey V. Elsukov #endif /* AF_INET */ 564fcf59617SAndrey V. Elsukov case AF_INET6: 565fcf59617SAndrey V. Elsukov i = sizeof(struct ip6_hdr); 566fcf59617SAndrey V. Elsukov off = offsetof(struct ip6_hdr, ip6_nxt); 567fcf59617SAndrey V. Elsukov break; 568fcf59617SAndrey V. Elsukov default: 569fcf59617SAndrey V. Elsukov DPRINTF(("%s: unsupported protocol family %u\n", 570fcf59617SAndrey V. Elsukov __func__, dst->sa.sa_family)); 571fcf59617SAndrey V. Elsukov error = EPFNOSUPPORT; 572fcf59617SAndrey V. Elsukov IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf); 573fcf59617SAndrey V. Elsukov goto bad; 574fcf59617SAndrey V. Elsukov } 575fcf59617SAndrey V. Elsukov error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off); 576fcf59617SAndrey V. Elsukov return (error); 577fcf59617SAndrey V. Elsukov bad: 578fcf59617SAndrey V. Elsukov IPSEC6STAT_INC(ips_out_inval); 579fcf59617SAndrey V. Elsukov if (m != NULL) 580fcf59617SAndrey V. Elsukov m_freem(m); 581fcf59617SAndrey V. Elsukov if (sav != NULL) 582fcf59617SAndrey V. Elsukov key_freesav(&sav); 583fcf59617SAndrey V. Elsukov key_freesp(&sp); 584fcf59617SAndrey V. Elsukov return (error); 585fcf59617SAndrey V. Elsukov } 586fcf59617SAndrey V. Elsukov 587fcf59617SAndrey V. Elsukov int 588fcf59617SAndrey V. Elsukov ipsec6_process_packet(struct mbuf *m, struct secpolicy *sp, 589fcf59617SAndrey V. Elsukov struct inpcb *inp) 590fcf59617SAndrey V. Elsukov { 591fcf59617SAndrey V. Elsukov 592*1a01e0e7SAndrey V. Elsukov return (ipsec6_perform_request(m, sp, inp, 0)); 593fcf59617SAndrey V. Elsukov } 594fcf59617SAndrey V. Elsukov 595fcf59617SAndrey V. Elsukov static int 596fcf59617SAndrey V. Elsukov ipsec6_common_output(struct mbuf *m, struct inpcb *inp, int forwarding) 597fcf59617SAndrey V. Elsukov { 598fcf59617SAndrey V. Elsukov struct secpolicy *sp; 599fcf59617SAndrey V. Elsukov int error; 600fcf59617SAndrey V. Elsukov 601fcf59617SAndrey V. Elsukov /* Lookup for the corresponding outbound security policy */ 602fcf59617SAndrey V. Elsukov sp = ipsec6_checkpolicy(m, inp, &error); 603fcf59617SAndrey V. Elsukov if (sp == NULL) { 604fcf59617SAndrey V. Elsukov if (error == -EINVAL) { 605fcf59617SAndrey V. Elsukov /* Discarded by policy. */ 606fcf59617SAndrey V. Elsukov m_freem(m); 607fcf59617SAndrey V. Elsukov return (EACCES); 608fcf59617SAndrey V. Elsukov } 609fcf59617SAndrey V. Elsukov return (0); /* No IPsec required. */ 610fcf59617SAndrey V. Elsukov } 611fcf59617SAndrey V. Elsukov 612fcf59617SAndrey V. Elsukov if (!forwarding) { 613fcf59617SAndrey V. Elsukov /* 614fcf59617SAndrey V. Elsukov * Do delayed checksums now because we send before 615fcf59617SAndrey V. Elsukov * this is done in the normal processing path. 616fcf59617SAndrey V. Elsukov */ 617fcf59617SAndrey V. Elsukov if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) { 618fcf59617SAndrey V. Elsukov in6_delayed_cksum(m, m->m_pkthdr.len - 619fcf59617SAndrey V. Elsukov sizeof(struct ip6_hdr), sizeof(struct ip6_hdr)); 620fcf59617SAndrey V. Elsukov m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6; 621fcf59617SAndrey V. Elsukov } 622fcf59617SAndrey V. Elsukov #ifdef SCTP 623fcf59617SAndrey V. Elsukov if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) { 624fcf59617SAndrey V. Elsukov sctp_delayed_cksum(m, sizeof(struct ip6_hdr)); 625fcf59617SAndrey V. Elsukov m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6; 626fcf59617SAndrey V. Elsukov } 627fcf59617SAndrey V. Elsukov #endif 628fcf59617SAndrey V. Elsukov } 629fcf59617SAndrey V. Elsukov /* NB: callee frees mbuf and releases reference to SP */ 630fcf59617SAndrey V. Elsukov error = ipsec6_process_packet(m, sp, inp); 631fcf59617SAndrey V. Elsukov if (error == EJUSTRETURN) { 632fcf59617SAndrey V. Elsukov /* 633fcf59617SAndrey V. Elsukov * We had a SP with a level of 'use' and no SA. We 634fcf59617SAndrey V. Elsukov * will just continue to process the packet without 635fcf59617SAndrey V. Elsukov * IPsec processing and return without error. 636fcf59617SAndrey V. Elsukov */ 637fcf59617SAndrey V. Elsukov return (0); 638fcf59617SAndrey V. Elsukov } 639fcf59617SAndrey V. Elsukov if (error == 0) 640fcf59617SAndrey V. Elsukov return (EINPROGRESS); /* consumed by IPsec */ 641fcf59617SAndrey V. Elsukov return (error); 642fcf59617SAndrey V. Elsukov } 643fcf59617SAndrey V. Elsukov 644fcf59617SAndrey V. Elsukov /* 645fcf59617SAndrey V. Elsukov * IPSEC_OUTPUT() method implementation for IPv6. 646fcf59617SAndrey V. Elsukov * 0 - no IPsec handling needed 647fcf59617SAndrey V. Elsukov * other values - mbuf consumed by IPsec. 648fcf59617SAndrey V. Elsukov */ 649fcf59617SAndrey V. Elsukov int 650fcf59617SAndrey V. Elsukov ipsec6_output(struct mbuf *m, struct inpcb *inp) 651fcf59617SAndrey V. Elsukov { 652fcf59617SAndrey V. Elsukov 653fcf59617SAndrey V. Elsukov /* 654fcf59617SAndrey V. Elsukov * If the packet is resubmitted to ip_output (e.g. after 655fcf59617SAndrey V. Elsukov * AH, ESP, etc. processing), there will be a tag to bypass 656fcf59617SAndrey V. Elsukov * the lookup and related policy checking. 657fcf59617SAndrey V. Elsukov */ 658fcf59617SAndrey V. Elsukov if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL) 659fcf59617SAndrey V. Elsukov return (0); 660fcf59617SAndrey V. Elsukov 661fcf59617SAndrey V. Elsukov return (ipsec6_common_output(m, inp, 0)); 662fcf59617SAndrey V. Elsukov } 663fcf59617SAndrey V. Elsukov 664fcf59617SAndrey V. Elsukov /* 665fcf59617SAndrey V. Elsukov * IPSEC_FORWARD() method implementation for IPv6. 666fcf59617SAndrey V. Elsukov * 0 - no IPsec handling needed 667fcf59617SAndrey V. Elsukov * other values - mbuf consumed by IPsec. 668fcf59617SAndrey V. Elsukov */ 669fcf59617SAndrey V. Elsukov int 670fcf59617SAndrey V. Elsukov ipsec6_forward(struct mbuf *m) 671fcf59617SAndrey V. Elsukov { 672fcf59617SAndrey V. Elsukov 673fcf59617SAndrey V. Elsukov /* 674fcf59617SAndrey V. Elsukov * Check if this packet has an active inbound SP and needs to be 675fcf59617SAndrey V. Elsukov * dropped instead of forwarded. 676fcf59617SAndrey V. Elsukov */ 677fcf59617SAndrey V. Elsukov if (ipsec6_in_reject(m, NULL) != 0) { 678fcf59617SAndrey V. Elsukov m_freem(m); 679fcf59617SAndrey V. Elsukov return (EACCES); 680fcf59617SAndrey V. Elsukov } 681fcf59617SAndrey V. Elsukov return (ipsec6_common_output(m, NULL, 1)); 682fcf59617SAndrey V. Elsukov } 683fcf59617SAndrey V. Elsukov #endif /* INET6 */ 684fcf59617SAndrey V. Elsukov 685fcf59617SAndrey V. Elsukov int 686fcf59617SAndrey V. Elsukov ipsec_process_done(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav, 687fcf59617SAndrey V. Elsukov u_int idx) 688fcf59617SAndrey V. Elsukov { 689fcf59617SAndrey V. Elsukov struct xform_history *xh; 690fcf59617SAndrey V. Elsukov struct secasindex *saidx; 691fcf59617SAndrey V. Elsukov struct m_tag *mtag; 692fcf59617SAndrey V. Elsukov int error; 69388768458SSam Leffler 69488768458SSam Leffler saidx = &sav->sah->saidx; 69588768458SSam Leffler switch (saidx->dst.sa.sa_family) { 69688768458SSam Leffler #ifdef INET 69788768458SSam Leffler case AF_INET: 69888768458SSam Leffler /* Fix the header length, for AH processing. */ 69988768458SSam Leffler mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len); 70088768458SSam Leffler break; 70188768458SSam Leffler #endif /* INET */ 70288768458SSam Leffler #ifdef INET6 70388768458SSam Leffler case AF_INET6: 70488768458SSam Leffler /* Fix the header length, for AH processing. */ 70588768458SSam Leffler if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) { 70688768458SSam Leffler error = ENXIO; 70788768458SSam Leffler goto bad; 70888768458SSam Leffler } 70988768458SSam Leffler if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) { 71088768458SSam Leffler /* No jumbogram support. */ 71188768458SSam Leffler error = ENXIO; /*?*/ 71288768458SSam Leffler goto bad; 71388768458SSam Leffler } 71488768458SSam Leffler mtod(m, struct ip6_hdr *)->ip6_plen = 71588768458SSam Leffler htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 71688768458SSam Leffler break; 71788768458SSam Leffler #endif /* INET6 */ 71888768458SSam Leffler default: 7199ffa9677SSam Leffler DPRINTF(("%s: unknown protocol family %u\n", __func__, 72088768458SSam Leffler saidx->dst.sa.sa_family)); 72188768458SSam Leffler error = ENXIO; 72288768458SSam Leffler goto bad; 72388768458SSam Leffler } 72488768458SSam Leffler 72588768458SSam Leffler /* 726fcf59617SAndrey V. Elsukov * Add a record of what we've done to the packet. 72788768458SSam Leffler */ 728fcf59617SAndrey V. Elsukov mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, sizeof(*xh), M_NOWAIT); 72988768458SSam Leffler if (mtag == NULL) { 7309ffa9677SSam Leffler DPRINTF(("%s: could not get packet tag\n", __func__)); 73188768458SSam Leffler error = ENOMEM; 73288768458SSam Leffler goto bad; 73388768458SSam Leffler } 73488768458SSam Leffler 735fcf59617SAndrey V. Elsukov xh = (struct xform_history *)(mtag + 1); 736fcf59617SAndrey V. Elsukov xh->dst = saidx->dst; 737fcf59617SAndrey V. Elsukov xh->proto = saidx->proto; 738fcf59617SAndrey V. Elsukov xh->mode = saidx->mode; 739fcf59617SAndrey V. Elsukov xh->spi = sav->spi; 74088768458SSam Leffler m_tag_prepend(m, mtag); 74188768458SSam Leffler 74259959de5SErmal Luçi key_sa_recordxfer(sav, m); /* record data transfer */ 74359959de5SErmal Luçi 74488768458SSam Leffler /* 74588768458SSam Leffler * If there's another (bundled) SA to apply, do so. 74688768458SSam Leffler * Note that this puts a burden on the kernel stack size. 74788768458SSam Leffler * If this is a problem we'll need to introduce a queue 74888768458SSam Leffler * to set the packet on so we can unwind the stack before 74988768458SSam Leffler * doing further processing. 75088768458SSam Leffler */ 751fcf59617SAndrey V. Elsukov if (++idx < sp->tcount) { 752db178eb8SBjoern A. Zeeb switch (saidx->dst.sa.sa_family) { 753db178eb8SBjoern A. Zeeb #ifdef INET 754db178eb8SBjoern A. Zeeb case AF_INET: 755fcf59617SAndrey V. Elsukov key_freesav(&sav); 756f9d8f665SAndrey V. Elsukov IPSECSTAT_INC(ips_out_bundlesa); 757*1a01e0e7SAndrey V. Elsukov return (ipsec4_perform_request(m, sp, NULL, idx)); 758db178eb8SBjoern A. Zeeb /* NOTREACHED */ 759db178eb8SBjoern A. Zeeb #endif 760db178eb8SBjoern A. Zeeb #ifdef INET6 761db178eb8SBjoern A. Zeeb case AF_INET6: 762fcf59617SAndrey V. Elsukov key_freesav(&sav); 763f9d8f665SAndrey V. Elsukov IPSEC6STAT_INC(ips_out_bundlesa); 764*1a01e0e7SAndrey V. Elsukov return (ipsec6_perform_request(m, sp, NULL, idx)); 765db178eb8SBjoern A. Zeeb /* NOTREACHED */ 766db178eb8SBjoern A. Zeeb #endif /* INET6 */ 767db178eb8SBjoern A. Zeeb default: 768db178eb8SBjoern A. Zeeb DPRINTF(("%s: unknown protocol family %u\n", __func__, 769db178eb8SBjoern A. Zeeb saidx->dst.sa.sa_family)); 770fcf59617SAndrey V. Elsukov error = EPFNOSUPPORT; 771db178eb8SBjoern A. Zeeb goto bad; 772db178eb8SBjoern A. Zeeb } 77388768458SSam Leffler } 77488768458SSam Leffler 775fcf59617SAndrey V. Elsukov key_freesp(&sp), sp = NULL; /* Release reference to SP */ 776fcf59617SAndrey V. Elsukov #ifdef INET 777fcf59617SAndrey V. Elsukov /* 778fcf59617SAndrey V. Elsukov * Do UDP encapsulation if SA requires it. 779fcf59617SAndrey V. Elsukov */ 780fcf59617SAndrey V. Elsukov if (sav->natt != NULL) { 781fcf59617SAndrey V. Elsukov error = udp_ipsec_output(m, sav); 782fcf59617SAndrey V. Elsukov if (error != 0) 783fcf59617SAndrey V. Elsukov goto bad; 784fcf59617SAndrey V. Elsukov } 785fcf59617SAndrey V. Elsukov #endif /* INET */ 78688768458SSam Leffler /* 78788768458SSam Leffler * We're done with IPsec processing, transmit the packet using the 7886508929bSAndrey V. Elsukov * appropriate network protocol (IP or IPv6). 78988768458SSam Leffler */ 79088768458SSam Leffler switch (saidx->dst.sa.sa_family) { 79188768458SSam Leffler #ifdef INET 79288768458SSam Leffler case AF_INET: 793fcf59617SAndrey V. Elsukov key_freesav(&sav); 79488768458SSam Leffler return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL); 79588768458SSam Leffler #endif /* INET */ 79688768458SSam Leffler #ifdef INET6 79788768458SSam Leffler case AF_INET6: 798fcf59617SAndrey V. Elsukov key_freesav(&sav); 79988768458SSam Leffler return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL); 80088768458SSam Leffler #endif /* INET6 */ 80188768458SSam Leffler } 80288768458SSam Leffler panic("ipsec_process_done"); 80388768458SSam Leffler bad: 80488768458SSam Leffler m_freem(m); 805fcf59617SAndrey V. Elsukov key_freesav(&sav); 806fcf59617SAndrey V. Elsukov if (sp != NULL) 807fcf59617SAndrey V. Elsukov key_freesp(&sp); 80888768458SSam Leffler return (error); 80988768458SSam Leffler } 81088768458SSam Leffler 811fcf59617SAndrey V. Elsukov /* 812fcf59617SAndrey V. Elsukov * ipsec_prepend() is optimized version of M_PREPEND(). 813fcf59617SAndrey V. Elsukov * ipsec_encap() is called by IPsec output routine for tunnel mode SA. 814fcf59617SAndrey V. Elsukov * It is expected that after IP encapsulation some IPsec transform will 815fcf59617SAndrey V. Elsukov * be performed. Each IPsec transform inserts its variable length header 816fcf59617SAndrey V. Elsukov * just after outer IP header using m_makespace(). If given mbuf has not 817fcf59617SAndrey V. Elsukov * enough free space at the beginning, we allocate new mbuf and reserve 818fcf59617SAndrey V. Elsukov * some space at the beginning and at the end. 819fcf59617SAndrey V. Elsukov * This helps avoid allocating of new mbuf and data copying in m_makespace(), 820fcf59617SAndrey V. Elsukov * we place outer header in the middle of mbuf's data with reserved leading 821fcf59617SAndrey V. Elsukov * and trailing space: 822fcf59617SAndrey V. Elsukov * [ LEADINGSPACE ][ Outer IP header ][ TRAILINGSPACE ] 823fcf59617SAndrey V. Elsukov * LEADINGSPACE will be used to add ethernet header, TRAILINGSPACE will 824fcf59617SAndrey V. Elsukov * be used to inject AH/ESP/IPCOMP header. 825fcf59617SAndrey V. Elsukov */ 826fcf59617SAndrey V. Elsukov #define IPSEC_TRAILINGSPACE (sizeof(struct udphdr) +/* NAT-T */ \ 827fcf59617SAndrey V. Elsukov max(sizeof(struct newesp) + EALG_MAX_BLOCK_LEN, /* ESP + IV */ \ 828fcf59617SAndrey V. Elsukov sizeof(struct newah) + HASH_MAX_LEN /* AH + ICV */)) 829fcf59617SAndrey V. Elsukov static struct mbuf * 830fcf59617SAndrey V. Elsukov ipsec_prepend(struct mbuf *m, int len, int how) 83188768458SSam Leffler { 832fcf59617SAndrey V. Elsukov struct mbuf *n; 83388768458SSam Leffler 834fcf59617SAndrey V. Elsukov M_ASSERTPKTHDR(m); 835fcf59617SAndrey V. Elsukov IPSEC_ASSERT(len < MHLEN, ("wrong length")); 836fcf59617SAndrey V. Elsukov if (M_LEADINGSPACE(m) >= len) { 837fcf59617SAndrey V. Elsukov /* No need to allocate new mbuf. */ 838fcf59617SAndrey V. Elsukov m->m_data -= len; 839fcf59617SAndrey V. Elsukov m->m_len += len; 840fcf59617SAndrey V. Elsukov m->m_pkthdr.len += len; 841fcf59617SAndrey V. Elsukov return (m); 84288768458SSam Leffler } 843fcf59617SAndrey V. Elsukov n = m_gethdr(how, m->m_type); 844fcf59617SAndrey V. Elsukov if (n == NULL) { 845fcf59617SAndrey V. Elsukov m_freem(m); 846fcf59617SAndrey V. Elsukov return (NULL); 84788768458SSam Leffler } 848fcf59617SAndrey V. Elsukov m_move_pkthdr(n, m); 849fcf59617SAndrey V. Elsukov n->m_next = m; 850fcf59617SAndrey V. Elsukov if (len + IPSEC_TRAILINGSPACE < M_SIZE(n)) 851fcf59617SAndrey V. Elsukov m_align(n, len + IPSEC_TRAILINGSPACE); 852fcf59617SAndrey V. Elsukov n->m_len = len; 853fcf59617SAndrey V. Elsukov n->m_pkthdr.len += len; 854fcf59617SAndrey V. Elsukov return (n); 85588768458SSam Leffler } 85688768458SSam Leffler 85761f37615SAndrey V. Elsukov static int 85861f37615SAndrey V. Elsukov ipsec_encap(struct mbuf **mp, struct secasindex *saidx) 85961f37615SAndrey V. Elsukov { 86061f37615SAndrey V. Elsukov #ifdef INET6 86161f37615SAndrey V. Elsukov struct ip6_hdr *ip6; 86261f37615SAndrey V. Elsukov #endif 86361f37615SAndrey V. Elsukov struct ip *ip; 86461f37615SAndrey V. Elsukov int setdf; 86561f37615SAndrey V. Elsukov uint8_t itos, proto; 86661f37615SAndrey V. Elsukov 86761f37615SAndrey V. Elsukov ip = mtod(*mp, struct ip *); 86861f37615SAndrey V. Elsukov switch (ip->ip_v) { 86961f37615SAndrey V. Elsukov #ifdef INET 87061f37615SAndrey V. Elsukov case IPVERSION: 87161f37615SAndrey V. Elsukov proto = IPPROTO_IPIP; 87261f37615SAndrey V. Elsukov /* 87361f37615SAndrey V. Elsukov * Collect IP_DF state from the inner header 87461f37615SAndrey V. Elsukov * and honor system-wide control of how to handle it. 87561f37615SAndrey V. Elsukov */ 87661f37615SAndrey V. Elsukov switch (V_ip4_ipsec_dfbit) { 87761f37615SAndrey V. Elsukov case 0: /* clear in outer header */ 87861f37615SAndrey V. Elsukov case 1: /* set in outer header */ 87961f37615SAndrey V. Elsukov setdf = V_ip4_ipsec_dfbit; 88061f37615SAndrey V. Elsukov break; 88161f37615SAndrey V. Elsukov default:/* propagate to outer header */ 8826f814d0eSAndrey V. Elsukov setdf = (ip->ip_off & htons(IP_DF)) != 0; 88361f37615SAndrey V. Elsukov } 88461f37615SAndrey V. Elsukov itos = ip->ip_tos; 88561f37615SAndrey V. Elsukov break; 88661f37615SAndrey V. Elsukov #endif 88761f37615SAndrey V. Elsukov #ifdef INET6 88861f37615SAndrey V. Elsukov case (IPV6_VERSION >> 4): 88961f37615SAndrey V. Elsukov proto = IPPROTO_IPV6; 89061f37615SAndrey V. Elsukov ip6 = mtod(*mp, struct ip6_hdr *); 89161f37615SAndrey V. Elsukov itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 89261f37615SAndrey V. Elsukov setdf = V_ip4_ipsec_dfbit ? 1: 0; 89361f37615SAndrey V. Elsukov /* scoped address handling */ 89461f37615SAndrey V. Elsukov in6_clearscope(&ip6->ip6_src); 89561f37615SAndrey V. Elsukov in6_clearscope(&ip6->ip6_dst); 89661f37615SAndrey V. Elsukov break; 89761f37615SAndrey V. Elsukov #endif 89861f37615SAndrey V. Elsukov default: 89961f37615SAndrey V. Elsukov return (EAFNOSUPPORT); 90061f37615SAndrey V. Elsukov } 90161f37615SAndrey V. Elsukov switch (saidx->dst.sa.sa_family) { 90261f37615SAndrey V. Elsukov #ifdef INET 90361f37615SAndrey V. Elsukov case AF_INET: 90461f37615SAndrey V. Elsukov if (saidx->src.sa.sa_family != AF_INET || 90561f37615SAndrey V. Elsukov saidx->src.sin.sin_addr.s_addr == INADDR_ANY || 90661f37615SAndrey V. Elsukov saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) 90761f37615SAndrey V. Elsukov return (EINVAL); 908fcf59617SAndrey V. Elsukov *mp = ipsec_prepend(*mp, sizeof(struct ip), M_NOWAIT); 90961f37615SAndrey V. Elsukov if (*mp == NULL) 91061f37615SAndrey V. Elsukov return (ENOBUFS); 91161f37615SAndrey V. Elsukov ip = mtod(*mp, struct ip *); 91261f37615SAndrey V. Elsukov ip->ip_v = IPVERSION; 91361f37615SAndrey V. Elsukov ip->ip_hl = sizeof(struct ip) >> 2; 91461f37615SAndrey V. Elsukov ip->ip_p = proto; 91561f37615SAndrey V. Elsukov ip->ip_len = htons((*mp)->m_pkthdr.len); 91661f37615SAndrey V. Elsukov ip->ip_ttl = V_ip_defttl; 91761f37615SAndrey V. Elsukov ip->ip_sum = 0; 91861f37615SAndrey V. Elsukov ip->ip_off = setdf ? htons(IP_DF): 0; 91961f37615SAndrey V. Elsukov ip->ip_src = saidx->src.sin.sin_addr; 92061f37615SAndrey V. Elsukov ip->ip_dst = saidx->dst.sin.sin_addr; 92161f37615SAndrey V. Elsukov ip_ecn_ingress(V_ip4_ipsec_ecn, &ip->ip_tos, &itos); 92261f37615SAndrey V. Elsukov ip_fillid(ip); 92361f37615SAndrey V. Elsukov break; 92461f37615SAndrey V. Elsukov #endif /* INET */ 92561f37615SAndrey V. Elsukov #ifdef INET6 92661f37615SAndrey V. Elsukov case AF_INET6: 92761f37615SAndrey V. Elsukov if (saidx->src.sa.sa_family != AF_INET6 || 92861f37615SAndrey V. Elsukov IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr) || 92961f37615SAndrey V. Elsukov IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr)) 93061f37615SAndrey V. Elsukov return (EINVAL); 931fcf59617SAndrey V. Elsukov *mp = ipsec_prepend(*mp, sizeof(struct ip6_hdr), M_NOWAIT); 93261f37615SAndrey V. Elsukov if (*mp == NULL) 93361f37615SAndrey V. Elsukov return (ENOBUFS); 93461f37615SAndrey V. Elsukov ip6 = mtod(*mp, struct ip6_hdr *); 93561f37615SAndrey V. Elsukov ip6->ip6_flow = 0; 93661f37615SAndrey V. Elsukov ip6->ip6_vfc = IPV6_VERSION; 93761f37615SAndrey V. Elsukov ip6->ip6_hlim = V_ip6_defhlim; 93861f37615SAndrey V. Elsukov ip6->ip6_nxt = proto; 93961f37615SAndrey V. Elsukov ip6->ip6_dst = saidx->dst.sin6.sin6_addr; 9401ae800e7SAndrey V. Elsukov /* For link-local address embed scope zone id */ 9411ae800e7SAndrey V. Elsukov if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 9421ae800e7SAndrey V. Elsukov ip6->ip6_dst.s6_addr16[1] = 9431ae800e7SAndrey V. Elsukov htons(saidx->dst.sin6.sin6_scope_id & 0xffff); 94461f37615SAndrey V. Elsukov ip6->ip6_src = saidx->src.sin6.sin6_addr; 9451ae800e7SAndrey V. Elsukov if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 9461ae800e7SAndrey V. Elsukov ip6->ip6_src.s6_addr16[1] = 9471ae800e7SAndrey V. Elsukov htons(saidx->src.sin6.sin6_scope_id & 0xffff); 94861f37615SAndrey V. Elsukov ip6->ip6_plen = htons((*mp)->m_pkthdr.len - sizeof(*ip6)); 94961f37615SAndrey V. Elsukov ip_ecn_ingress(V_ip6_ipsec_ecn, &proto, &itos); 95061f37615SAndrey V. Elsukov ip6->ip6_flow |= htonl((uint32_t)proto << 20); 95161f37615SAndrey V. Elsukov break; 95261f37615SAndrey V. Elsukov #endif /* INET6 */ 95361f37615SAndrey V. Elsukov default: 95461f37615SAndrey V. Elsukov return (EAFNOSUPPORT); 95561f37615SAndrey V. Elsukov } 956fcf59617SAndrey V. Elsukov (*mp)->m_flags &= ~(M_BCAST | M_MCAST); 95761f37615SAndrey V. Elsukov return (0); 95861f37615SAndrey V. Elsukov } 95961f37615SAndrey V. Elsukov 960