1aaea26efSSam Leffler /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3fe267a55SPedro F. Giffuni * 4aaea26efSSam Leffler * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting 5fcf59617SAndrey V. Elsukov * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org> 6aaea26efSSam Leffler * All rights reserved. 7aaea26efSSam Leffler * 8aaea26efSSam Leffler * Redistribution and use in source and binary forms, with or without 9aaea26efSSam Leffler * modification, are permitted provided that the following conditions 10aaea26efSSam Leffler * are met: 11aaea26efSSam Leffler * 1. Redistributions of source code must retain the above copyright 12aaea26efSSam Leffler * notice, this list of conditions and the following disclaimer. 13aaea26efSSam Leffler * 2. Redistributions in binary form must reproduce the above copyright 14aaea26efSSam Leffler * notice, this list of conditions and the following disclaimer in the 15aaea26efSSam Leffler * documentation and/or other materials provided with the distribution. 16aaea26efSSam Leffler * 17aaea26efSSam Leffler * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18aaea26efSSam Leffler * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19aaea26efSSam Leffler * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20aaea26efSSam Leffler * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21aaea26efSSam Leffler * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22aaea26efSSam Leffler * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23aaea26efSSam Leffler * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24aaea26efSSam Leffler * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25aaea26efSSam Leffler * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26aaea26efSSam Leffler * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27aaea26efSSam Leffler * SUCH DAMAGE. 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> 54e68b3792SGleb Smirnoff #include <netinet/in_pcb.h> 5588768458SSam Leffler #include <netinet/in_systm.h> 5688768458SSam Leffler #include <netinet/ip.h> 5788768458SSam Leffler #include <netinet/ip_var.h> 5888768458SSam Leffler #include <netinet/in_var.h> 5988768458SSam Leffler #include <netinet/ip_ecn.h> 6088768458SSam Leffler #ifdef INET6 6188768458SSam Leffler #include <netinet6/ip6_ecn.h> 6288768458SSam Leffler #endif 636b66194bSKornel Duleba #include <netinet/ip_icmp.h> 646b66194bSKornel Duleba #include <netinet/tcp_var.h> 6588768458SSam Leffler 6688768458SSam Leffler #include <netinet/ip6.h> 6788768458SSam Leffler #ifdef INET6 6888768458SSam Leffler #include <netinet6/ip6_var.h> 6961f37615SAndrey V. Elsukov #include <netinet6/scope6_var.h> 7088768458SSam Leffler #endif 7188768458SSam Leffler #include <netinet/in_pcb.h> 7288768458SSam Leffler #ifdef INET6 7388768458SSam Leffler #include <netinet/icmp6.h> 7488768458SSam Leffler #endif 7595033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT) 76fcf59617SAndrey V. Elsukov #include <netinet/sctp_crc32.h> 77fcf59617SAndrey V. Elsukov #endif 7888768458SSam Leffler 79fcf59617SAndrey V. Elsukov #include <netinet/udp.h> 80fcf59617SAndrey V. Elsukov #include <netipsec/ah.h> 81fcf59617SAndrey V. Elsukov #include <netipsec/esp.h> 8288768458SSam Leffler #include <netipsec/ipsec.h> 8388768458SSam Leffler #ifdef INET6 8488768458SSam Leffler #include <netipsec/ipsec6.h> 8588768458SSam Leffler #endif 86809fef29SGleb Smirnoff #include <netipsec/ipsec_support.h> 87ef2a572bSKonstantin Belousov #include <netipsec/ipsec_offload.h> 8888768458SSam Leffler #include <netipsec/ah_var.h> 8988768458SSam Leffler #include <netipsec/esp_var.h> 9088768458SSam Leffler #include <netipsec/ipcomp_var.h> 9188768458SSam Leffler 9288768458SSam Leffler #include <netipsec/xform.h> 9388768458SSam Leffler 9488768458SSam Leffler #include <netipsec/key.h> 9588768458SSam Leffler #include <netipsec/keydb.h> 9688768458SSam Leffler #include <netipsec/key_debug.h> 9788768458SSam Leffler 9888768458SSam Leffler #include <machine/in_cksum.h> 9988768458SSam Leffler 100fcf59617SAndrey V. Elsukov #define IPSEC_OSTAT_INC(proto, name) do { \ 101fcf59617SAndrey V. Elsukov if ((proto) == IPPROTO_ESP) \ 102fcf59617SAndrey V. Elsukov ESPSTAT_INC(esps_##name); \ 103fcf59617SAndrey V. Elsukov else if ((proto) == IPPROTO_AH)\ 104fcf59617SAndrey V. Elsukov AHSTAT_INC(ahs_##name); \ 105fcf59617SAndrey V. Elsukov else \ 106fcf59617SAndrey V. Elsukov IPCOMPSTAT_INC(ipcomps_##name); \ 107fcf59617SAndrey V. Elsukov } while (0) 108fcf59617SAndrey V. Elsukov 109fcf59617SAndrey V. Elsukov static int ipsec_encap(struct mbuf **mp, struct secasindex *saidx); 1109dfc8606SBartlomiej Grzesik static size_t ipsec_get_pmtu(struct secasvar *sav); 111fcf59617SAndrey V. Elsukov 112fcf59617SAndrey V. Elsukov #ifdef INET 113fcf59617SAndrey V. Elsukov static struct secasvar * 114de1da299SKonstantin Belousov ipsec4_allocsa(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp, 115de1da299SKonstantin Belousov u_int *pidx, int *error) 116fcf59617SAndrey V. Elsukov { 117fcf59617SAndrey V. Elsukov struct secasindex *saidx, tmpsaidx; 118fcf59617SAndrey V. Elsukov struct ipsecrequest *isr; 119fcf59617SAndrey V. Elsukov struct sockaddr_in *sin; 120fcf59617SAndrey V. Elsukov struct secasvar *sav; 121fcf59617SAndrey V. Elsukov struct ip *ip; 122fcf59617SAndrey V. Elsukov 123fcf59617SAndrey V. Elsukov /* 124fcf59617SAndrey V. Elsukov * Check system global policy controls. 125fcf59617SAndrey V. Elsukov */ 126fcf59617SAndrey V. Elsukov next: 127fcf59617SAndrey V. Elsukov isr = sp->req[*pidx]; 128fcf59617SAndrey V. Elsukov if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) || 129fcf59617SAndrey V. Elsukov (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) || 130fcf59617SAndrey V. Elsukov (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) { 131fcf59617SAndrey V. Elsukov DPRINTF(("%s: IPsec outbound packet dropped due" 132fcf59617SAndrey V. Elsukov " to policy (check your sysctls)\n", __func__)); 133fcf59617SAndrey V. Elsukov IPSEC_OSTAT_INC(isr->saidx.proto, pdrops); 134fcf59617SAndrey V. Elsukov *error = EHOSTUNREACH; 135fcf59617SAndrey V. Elsukov return (NULL); 136fcf59617SAndrey V. Elsukov } 137fcf59617SAndrey V. Elsukov /* 138fcf59617SAndrey V. Elsukov * Craft SA index to search for proper SA. Note that 139fcf59617SAndrey V. Elsukov * we only initialize unspecified SA peers for transport 140fcf59617SAndrey V. Elsukov * mode; for tunnel mode they must already be filled in. 141fcf59617SAndrey V. Elsukov */ 142fcf59617SAndrey V. Elsukov if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) { 143fcf59617SAndrey V. Elsukov saidx = &tmpsaidx; 144fcf59617SAndrey V. Elsukov *saidx = isr->saidx; 145fcf59617SAndrey V. Elsukov ip = mtod(m, struct ip *); 146fcf59617SAndrey V. Elsukov if (saidx->src.sa.sa_len == 0) { 147fcf59617SAndrey V. Elsukov sin = &saidx->src.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_src; 152fcf59617SAndrey V. Elsukov } 153fcf59617SAndrey V. Elsukov if (saidx->dst.sa.sa_len == 0) { 154fcf59617SAndrey V. Elsukov sin = &saidx->dst.sin; 155fcf59617SAndrey V. Elsukov sin->sin_len = sizeof(*sin); 156fcf59617SAndrey V. Elsukov sin->sin_family = AF_INET; 157fcf59617SAndrey V. Elsukov sin->sin_port = IPSEC_PORT_ANY; 158fcf59617SAndrey V. Elsukov sin->sin_addr = ip->ip_dst; 159fcf59617SAndrey V. Elsukov } 160fcf59617SAndrey V. Elsukov } else 161fcf59617SAndrey V. Elsukov saidx = &sp->req[*pidx]->saidx; 162fcf59617SAndrey V. Elsukov /* 163fcf59617SAndrey V. Elsukov * Lookup SA and validate it. 164fcf59617SAndrey V. Elsukov */ 165fcf59617SAndrey V. Elsukov sav = key_allocsa_policy(sp, saidx, error); 166fcf59617SAndrey V. Elsukov if (sav == NULL) { 167fcf59617SAndrey V. Elsukov IPSECSTAT_INC(ips_out_nosa); 168fcf59617SAndrey V. Elsukov if (*error != 0) 169fcf59617SAndrey V. Elsukov return (NULL); 170fcf59617SAndrey V. Elsukov if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) { 171fcf59617SAndrey V. Elsukov /* 172fcf59617SAndrey V. Elsukov * We have no SA and policy that doesn't require 173fcf59617SAndrey V. Elsukov * this IPsec transform, thus we can continue w/o 174fcf59617SAndrey V. Elsukov * IPsec processing, i.e. return EJUSTRETURN. 175fcf59617SAndrey V. Elsukov * But first check if there is some bundled transform. 176fcf59617SAndrey V. Elsukov */ 177fcf59617SAndrey V. Elsukov if (sp->tcount > ++(*pidx)) 178fcf59617SAndrey V. Elsukov goto next; 179fcf59617SAndrey V. Elsukov *error = EJUSTRETURN; 180fcf59617SAndrey V. Elsukov } 181fcf59617SAndrey V. Elsukov return (NULL); 182fcf59617SAndrey V. Elsukov } 183fcf59617SAndrey V. Elsukov IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform")); 184fcf59617SAndrey V. Elsukov return (sav); 185fcf59617SAndrey V. Elsukov } 186fcf59617SAndrey V. Elsukov 187fcf59617SAndrey V. Elsukov /* 188fcf59617SAndrey V. Elsukov * IPsec output logic for IPv4. 189fcf59617SAndrey V. Elsukov */ 190fcf59617SAndrey V. Elsukov static int 191de1da299SKonstantin Belousov ipsec4_perform_request(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp, 19200524fd4SKonstantin Belousov struct inpcb *inp, u_int idx, u_long mtu) 193fcf59617SAndrey V. Elsukov { 194fcf59617SAndrey V. Elsukov struct ipsec_ctx_data ctx; 195fcf59617SAndrey V. Elsukov union sockaddr_union *dst; 196fcf59617SAndrey V. Elsukov struct secasvar *sav; 197fcf59617SAndrey V. Elsukov struct ip *ip; 198*240b7bfeSKonstantin Belousov int error, hwassist, i, off; 199*240b7bfeSKonstantin Belousov bool accel; 200fcf59617SAndrey V. Elsukov 201fcf59617SAndrey V. Elsukov IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx)); 202fcf59617SAndrey V. Elsukov 203fcf59617SAndrey V. Elsukov /* 204fcf59617SAndrey V. Elsukov * We hold the reference to SP. Content of SP couldn't be changed. 205fcf59617SAndrey V. Elsukov * Craft secasindex and do lookup for suitable SA. 206fcf59617SAndrey V. Elsukov * Then do encapsulation if needed and call xform's output. 207fcf59617SAndrey V. Elsukov * We need to store SP in the xform callback parameters. 208fcf59617SAndrey V. Elsukov * In xform callback we will extract SP and it can be used to 209fcf59617SAndrey V. Elsukov * determine next transform. At the end of transform we can 210fcf59617SAndrey V. Elsukov * release reference to SP. 211fcf59617SAndrey V. Elsukov */ 212de1da299SKonstantin Belousov sav = ipsec4_allocsa(ifp, m, sp, &idx, &error); 213fcf59617SAndrey V. Elsukov if (sav == NULL) { 214fcf59617SAndrey V. Elsukov if (error == EJUSTRETURN) { /* No IPsec required */ 215ef2a572bSKonstantin Belousov (void)ipsec_accel_output(ifp, m, inp, sp, NULL, 216*240b7bfeSKonstantin Belousov AF_INET, mtu, &hwassist); 217fcf59617SAndrey V. Elsukov key_freesp(&sp); 218fcf59617SAndrey V. Elsukov return (error); 219fcf59617SAndrey V. Elsukov } 220fcf59617SAndrey V. Elsukov goto bad; 221fcf59617SAndrey V. Elsukov } 222fcf59617SAndrey V. Elsukov /* 223fcf59617SAndrey V. Elsukov * XXXAE: most likely ip_sum at this point is wrong. 224fcf59617SAndrey V. Elsukov */ 2251a01e0e7SAndrey V. Elsukov IPSEC_INIT_CTX(&ctx, &m, inp, sav, AF_INET, IPSEC_ENC_BEFORE); 226fcf59617SAndrey V. Elsukov if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0) 227fcf59617SAndrey V. Elsukov goto bad; 228fcf59617SAndrey V. Elsukov 229*240b7bfeSKonstantin Belousov hwassist = 0; 230*240b7bfeSKonstantin Belousov accel = ipsec_accel_output(ifp, m, inp, sp, sav, AF_INET, mtu, 231*240b7bfeSKonstantin Belousov &hwassist); 232*240b7bfeSKonstantin Belousov 233*240b7bfeSKonstantin Belousov /* 234*240b7bfeSKonstantin Belousov * Do delayed checksums now because we send before 235*240b7bfeSKonstantin Belousov * this is done in the normal processing path. 236*240b7bfeSKonstantin Belousov */ 237*240b7bfeSKonstantin Belousov if ((m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~hwassist) != 0) { 238*240b7bfeSKonstantin Belousov in_delayed_cksum(m); 239*240b7bfeSKonstantin Belousov m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 240*240b7bfeSKonstantin Belousov } 241*240b7bfeSKonstantin Belousov #if defined(SCTP) || defined(SCTP_SUPPORT) 242*240b7bfeSKonstantin Belousov if ((m->m_pkthdr.csum_flags & CSUM_SCTP & ~hwassist) != 0) { 243*240b7bfeSKonstantin Belousov struct ip *ip; 244*240b7bfeSKonstantin Belousov 245*240b7bfeSKonstantin Belousov ip = mtod(m, struct ip *); 246*240b7bfeSKonstantin Belousov sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2)); 247*240b7bfeSKonstantin Belousov m->m_pkthdr.csum_flags &= ~CSUM_SCTP; 248*240b7bfeSKonstantin Belousov } 249*240b7bfeSKonstantin Belousov #endif 250*240b7bfeSKonstantin Belousov if (accel) 251ef2a572bSKonstantin Belousov return (EJUSTRETURN); 252ef2a572bSKonstantin Belousov 253fcf59617SAndrey V. Elsukov ip = mtod(m, struct ip *); 254fcf59617SAndrey V. Elsukov dst = &sav->sah->saidx.dst; 255fcf59617SAndrey V. Elsukov /* Do the appropriate encapsulation, if necessary */ 256fcf59617SAndrey V. Elsukov if (sp->req[idx]->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */ 257fcf59617SAndrey V. Elsukov dst->sa.sa_family != AF_INET || /* PF mismatch */ 258fcf59617SAndrey V. Elsukov (dst->sa.sa_family == AF_INET && /* Proxy */ 259fcf59617SAndrey V. Elsukov dst->sin.sin_addr.s_addr != INADDR_ANY && 260fcf59617SAndrey V. Elsukov dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) { 261fcf59617SAndrey V. Elsukov /* Fix IPv4 header checksum and length */ 262fcf59617SAndrey V. Elsukov ip->ip_len = htons(m->m_pkthdr.len); 263fcf59617SAndrey V. Elsukov ip->ip_sum = 0; 264fcf59617SAndrey V. Elsukov ip->ip_sum = in_cksum(m, ip->ip_hl << 2); 265fcf59617SAndrey V. Elsukov error = ipsec_encap(&m, &sav->sah->saidx); 266fcf59617SAndrey V. Elsukov if (error != 0) { 2677f1f6591SAndrey V. Elsukov DPRINTF(("%s: encapsulation for SPI 0x%08x failed " 2687f1f6591SAndrey V. Elsukov "with error %d\n", __func__, ntohl(sav->spi), 2697f1f6591SAndrey V. Elsukov error)); 270fcf59617SAndrey V. Elsukov /* XXXAE: IPSEC_OSTAT_INC(tunnel); */ 271fcf59617SAndrey V. Elsukov goto bad; 272fcf59617SAndrey V. Elsukov } 2731a01e0e7SAndrey V. Elsukov inp = NULL; 274fcf59617SAndrey V. Elsukov } 275fcf59617SAndrey V. Elsukov 2761a01e0e7SAndrey V. Elsukov IPSEC_INIT_CTX(&ctx, &m, inp, sav, dst->sa.sa_family, IPSEC_ENC_AFTER); 277fcf59617SAndrey V. Elsukov if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0) 278fcf59617SAndrey V. Elsukov goto bad; 279fcf59617SAndrey V. Elsukov 280fcf59617SAndrey V. Elsukov /* 281fcf59617SAndrey V. Elsukov * Dispatch to the appropriate IPsec transform logic. The 282fcf59617SAndrey V. Elsukov * packet will be returned for transmission after crypto 283fcf59617SAndrey V. Elsukov * processing, etc. are completed. 284fcf59617SAndrey V. Elsukov * 285fcf59617SAndrey V. Elsukov * NB: m & sav are ``passed to caller'' who's responsible for 286fcf59617SAndrey V. Elsukov * reclaiming their resources. 287fcf59617SAndrey V. Elsukov */ 288fcf59617SAndrey V. Elsukov switch(dst->sa.sa_family) { 289fcf59617SAndrey V. Elsukov case AF_INET: 290fcf59617SAndrey V. Elsukov ip = mtod(m, struct ip *); 291fcf59617SAndrey V. Elsukov i = ip->ip_hl << 2; 292fcf59617SAndrey V. Elsukov off = offsetof(struct ip, ip_p); 293fcf59617SAndrey V. Elsukov break; 294fcf59617SAndrey V. Elsukov #ifdef INET6 295fcf59617SAndrey V. Elsukov case AF_INET6: 296fcf59617SAndrey V. Elsukov i = sizeof(struct ip6_hdr); 297fcf59617SAndrey V. Elsukov off = offsetof(struct ip6_hdr, ip6_nxt); 298fcf59617SAndrey V. Elsukov break; 299fcf59617SAndrey V. Elsukov #endif /* INET6 */ 300fcf59617SAndrey V. Elsukov default: 301fcf59617SAndrey V. Elsukov DPRINTF(("%s: unsupported protocol family %u\n", 302fcf59617SAndrey V. Elsukov __func__, dst->sa.sa_family)); 303fcf59617SAndrey V. Elsukov error = EPFNOSUPPORT; 304fcf59617SAndrey V. Elsukov IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf); 305fcf59617SAndrey V. Elsukov goto bad; 306fcf59617SAndrey V. Elsukov } 307fcf59617SAndrey V. Elsukov error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off); 308fcf59617SAndrey V. Elsukov return (error); 309fcf59617SAndrey V. Elsukov bad: 310fcf59617SAndrey V. Elsukov IPSECSTAT_INC(ips_out_inval); 311fcf59617SAndrey V. Elsukov if (m != NULL) 312fcf59617SAndrey V. Elsukov m_freem(m); 313fcf59617SAndrey V. Elsukov if (sav != NULL) 314fcf59617SAndrey V. Elsukov key_freesav(&sav); 315fcf59617SAndrey V. Elsukov key_freesp(&sp); 316fcf59617SAndrey V. Elsukov return (error); 317fcf59617SAndrey V. Elsukov } 3187b495c44SVANHULLEBUS Yvan 31988768458SSam Leffler int 320de1da299SKonstantin Belousov ipsec4_process_packet(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp, 32100524fd4SKonstantin Belousov struct inpcb *inp, u_long mtu) 32288768458SSam Leffler { 323fcf59617SAndrey V. Elsukov 32400524fd4SKonstantin Belousov return (ipsec4_perform_request(ifp, m, sp, inp, 0, mtu)); 325fcf59617SAndrey V. Elsukov } 326fcf59617SAndrey V. Elsukov 3276b66194bSKornel Duleba int 328de1da299SKonstantin Belousov ipsec4_check_pmtu(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp, 329de1da299SKonstantin Belousov int forwarding) 3306b66194bSKornel Duleba { 3316b66194bSKornel Duleba struct secasvar *sav; 3326b66194bSKornel Duleba struct ip *ip; 3336b66194bSKornel Duleba size_t hlen, pmtu; 3346b66194bSKornel Duleba uint32_t idx; 3356b66194bSKornel Duleba int error; 3366b66194bSKornel Duleba 3376b66194bSKornel Duleba /* Don't check PMTU if the frame won't have DF bit set. */ 3386b66194bSKornel Duleba if (!V_ip4_ipsec_dfbit) 3396b66194bSKornel Duleba return (0); 3406b66194bSKornel Duleba if (V_ip4_ipsec_dfbit == 1) 3416b66194bSKornel Duleba goto setdf; 3426b66194bSKornel Duleba 3436b66194bSKornel Duleba /* V_ip4_ipsec_dfbit > 1 - we will copy it from inner header. */ 3446b66194bSKornel Duleba ip = mtod(m, struct ip *); 3456b66194bSKornel Duleba if (!(ip->ip_off & htons(IP_DF))) 3466b66194bSKornel Duleba return (0); 3476b66194bSKornel Duleba 3486b66194bSKornel Duleba setdf: 3496b66194bSKornel Duleba idx = sp->tcount - 1; 350de1da299SKonstantin Belousov sav = ipsec4_allocsa(ifp, m, sp, &idx, &error); 3516b66194bSKornel Duleba if (sav == NULL) { 3526b66194bSKornel Duleba key_freesp(&sp); 353a16771deSKornel Duleba /* 354a16771deSKornel Duleba * No matching SA was found and SADB_ACQUIRE message was generated. 355a16771deSKornel Duleba * Since we have matched a SP to this packet drop it silently. 356a16771deSKornel Duleba */ 357a16771deSKornel Duleba if (error == 0) 358a16771deSKornel Duleba error = EINPROGRESS; 3596b66194bSKornel Duleba if (error != EJUSTRETURN) 3606b66194bSKornel Duleba m_freem(m); 3616b66194bSKornel Duleba 3626b66194bSKornel Duleba return (error); 3636b66194bSKornel Duleba } 3646b66194bSKornel Duleba 3659dfc8606SBartlomiej Grzesik pmtu = ipsec_get_pmtu(sav); 366b4220bf3SBartlomiej Grzesik if (pmtu == 0) { 3679dfc8606SBartlomiej Grzesik key_freesav(&sav); 3686b66194bSKornel Duleba return (0); 369b4220bf3SBartlomiej Grzesik } 370b4220bf3SBartlomiej Grzesik 3716b66194bSKornel Duleba hlen = ipsec_hdrsiz_internal(sp); 3729dfc8606SBartlomiej Grzesik key_freesav(&sav); 3739dfc8606SBartlomiej Grzesik 3746b66194bSKornel Duleba if (m_length(m, NULL) + hlen > pmtu) { 3756b66194bSKornel Duleba /* 3766b66194bSKornel Duleba * If we're forwarding generate ICMP message here, 3778deba29cSWojciech Macek * so that it contains pmtu subtracted by header size. 3786b66194bSKornel Duleba * Set error to EINPROGRESS, in order for the frame 3796b66194bSKornel Duleba * to be dropped silently. 3806b66194bSKornel Duleba */ 3816b66194bSKornel Duleba if (forwarding) { 3826b66194bSKornel Duleba if (pmtu > hlen) 3836b66194bSKornel Duleba icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 3846b66194bSKornel Duleba 0, pmtu - hlen); 3856b66194bSKornel Duleba else 3866b66194bSKornel Duleba m_freem(m); 3876b66194bSKornel Duleba 3886b66194bSKornel Duleba key_freesp(&sp); 3896b66194bSKornel Duleba return (EINPROGRESS); /* Pretend that we consumed it. */ 3906b66194bSKornel Duleba } else { 3916b66194bSKornel Duleba m_freem(m); 3926b66194bSKornel Duleba key_freesp(&sp); 3936b66194bSKornel Duleba return (EMSGSIZE); 3946b66194bSKornel Duleba } 3956b66194bSKornel Duleba } 3966b66194bSKornel Duleba 3976b66194bSKornel Duleba return (0); 3986b66194bSKornel Duleba } 3996b66194bSKornel Duleba 400fcf59617SAndrey V. Elsukov static int 401de1da299SKonstantin Belousov ipsec4_common_output(struct ifnet *ifp, struct mbuf *m, struct inpcb *inp, 40200524fd4SKonstantin Belousov int forwarding, u_long mtu) 403fcf59617SAndrey V. Elsukov { 404fcf59617SAndrey V. Elsukov struct secpolicy *sp; 40588768458SSam Leffler int error; 40688768458SSam Leffler 407fcf59617SAndrey V. Elsukov /* Lookup for the corresponding outbound security policy */ 40822bbefb2SAndrey V. Elsukov sp = ipsec4_checkpolicy(m, inp, &error, !forwarding); 409fcf59617SAndrey V. Elsukov if (sp == NULL) { 410fcf59617SAndrey V. Elsukov if (error == -EINVAL) { 411fcf59617SAndrey V. Elsukov /* Discarded by policy. */ 412fcf59617SAndrey V. Elsukov m_freem(m); 413fcf59617SAndrey V. Elsukov return (EACCES); 414fcf59617SAndrey V. Elsukov } 415fcf59617SAndrey V. Elsukov return (0); /* No IPsec required. */ 416fcf59617SAndrey V. Elsukov } 417fcf59617SAndrey V. Elsukov 418fcf59617SAndrey V. Elsukov /* 419fcf59617SAndrey V. Elsukov * Usually we have to have tunnel mode IPsec security policy 420fcf59617SAndrey V. Elsukov * when we are forwarding a packet. Otherwise we could not handle 421fcf59617SAndrey V. Elsukov * encrypted replies, because they are not destined for us. But 422fcf59617SAndrey V. Elsukov * some users are doing source address translation for forwarded 423fcf59617SAndrey V. Elsukov * packets, and thus, even if they are forwarded, the replies will 424fcf59617SAndrey V. Elsukov * return back to us. 425fcf59617SAndrey V. Elsukov */ 426fcf59617SAndrey V. Elsukov 427fcf59617SAndrey V. Elsukov /* NB: callee frees mbuf and releases reference to SP */ 428de1da299SKonstantin Belousov error = ipsec4_check_pmtu(ifp, m, sp, forwarding); 4296b66194bSKornel Duleba if (error != 0) { 4306b66194bSKornel Duleba if (error == EJUSTRETURN) 4316b66194bSKornel Duleba return (0); 4326b66194bSKornel Duleba 4336b66194bSKornel Duleba return (error); 4346b66194bSKornel Duleba } 4356b66194bSKornel Duleba 43600524fd4SKonstantin Belousov error = ipsec4_process_packet(ifp, m, sp, inp, mtu); 437fcf59617SAndrey V. Elsukov if (error == EJUSTRETURN) { 438fcf59617SAndrey V. Elsukov /* 439fcf59617SAndrey V. Elsukov * We had a SP with a level of 'use' and no SA. We 440fcf59617SAndrey V. Elsukov * will just continue to process the packet without 441fcf59617SAndrey V. Elsukov * IPsec processing and return without error. 442fcf59617SAndrey V. Elsukov */ 443fcf59617SAndrey V. Elsukov return (0); 444fcf59617SAndrey V. Elsukov } 445fcf59617SAndrey V. Elsukov if (error == 0) 446fcf59617SAndrey V. Elsukov return (EINPROGRESS); /* consumed by IPsec */ 447fcf59617SAndrey V. Elsukov return (error); 448fcf59617SAndrey V. Elsukov } 449fcf59617SAndrey V. Elsukov 450fcf59617SAndrey V. Elsukov /* 451fcf59617SAndrey V. Elsukov * IPSEC_OUTPUT() method implementation for IPv4. 452fcf59617SAndrey V. Elsukov * 0 - no IPsec handling needed 453fcf59617SAndrey V. Elsukov * other values - mbuf consumed by IPsec. 454fcf59617SAndrey V. Elsukov */ 455fcf59617SAndrey V. Elsukov int 45600524fd4SKonstantin Belousov ipsec4_output(struct ifnet *ifp, struct mbuf *m, struct inpcb *inp, u_long mtu) 457fcf59617SAndrey V. Elsukov { 458fcf59617SAndrey V. Elsukov 459fcf59617SAndrey V. Elsukov /* 460fcf59617SAndrey V. Elsukov * If the packet is resubmitted to ip_output (e.g. after 461fcf59617SAndrey V. Elsukov * AH, ESP, etc. processing), there will be a tag to bypass 462fcf59617SAndrey V. Elsukov * the lookup and related policy checking. 463fcf59617SAndrey V. Elsukov */ 464fcf59617SAndrey V. Elsukov if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL) 465fcf59617SAndrey V. Elsukov return (0); 466fcf59617SAndrey V. Elsukov 46700524fd4SKonstantin Belousov return (ipsec4_common_output(ifp, m, inp, 0, mtu)); 468fcf59617SAndrey V. Elsukov } 469fcf59617SAndrey V. Elsukov 470fcf59617SAndrey V. Elsukov /* 471fcf59617SAndrey V. Elsukov * IPSEC_FORWARD() method implementation for IPv4. 472fcf59617SAndrey V. Elsukov * 0 - no IPsec handling needed 473fcf59617SAndrey V. Elsukov * other values - mbuf consumed by IPsec. 474fcf59617SAndrey V. Elsukov */ 475fcf59617SAndrey V. Elsukov int 476fcf59617SAndrey V. Elsukov ipsec4_forward(struct mbuf *m) 477fcf59617SAndrey V. Elsukov { 478fcf59617SAndrey V. Elsukov 479fcf59617SAndrey V. Elsukov /* 480fcf59617SAndrey V. Elsukov * Check if this packet has an active inbound SP and needs to be 481fcf59617SAndrey V. Elsukov * dropped instead of forwarded. 482fcf59617SAndrey V. Elsukov */ 483fcf59617SAndrey V. Elsukov if (ipsec4_in_reject(m, NULL) != 0) { 484fcf59617SAndrey V. Elsukov m_freem(m); 485fcf59617SAndrey V. Elsukov return (EACCES); 486fcf59617SAndrey V. Elsukov } 48700524fd4SKonstantin Belousov return (ipsec4_common_output(NULL /* XXXKIB */, m, NULL, 1, 0)); 488fcf59617SAndrey V. Elsukov } 489fcf59617SAndrey V. Elsukov #endif 490fcf59617SAndrey V. Elsukov 491fcf59617SAndrey V. Elsukov #ifdef INET6 492fcf59617SAndrey V. Elsukov static int 493fcf59617SAndrey V. Elsukov in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa, 494fcf59617SAndrey V. Elsukov const struct in6_addr *ia) 495fcf59617SAndrey V. Elsukov { 496fcf59617SAndrey V. Elsukov struct in6_addr ia2; 497fcf59617SAndrey V. Elsukov 498fcf59617SAndrey V. Elsukov if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr)) { 499fcf59617SAndrey V. Elsukov memcpy(&ia2, &sa->sin6_addr, sizeof(ia2)); 500fcf59617SAndrey V. Elsukov ia2.s6_addr16[1] = htons(sa->sin6_scope_id); 501fcf59617SAndrey V. Elsukov return (IN6_ARE_ADDR_EQUAL(ia, &ia2)); 502fcf59617SAndrey V. Elsukov } 503fcf59617SAndrey V. Elsukov return (IN6_ARE_ADDR_EQUAL(&sa->sin6_addr, ia)); 504fcf59617SAndrey V. Elsukov } 505fcf59617SAndrey V. Elsukov 506fcf59617SAndrey V. Elsukov static struct secasvar * 507de1da299SKonstantin Belousov ipsec6_allocsa(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp, 508de1da299SKonstantin Belousov u_int *pidx, int *error) 509fcf59617SAndrey V. Elsukov { 510fcf59617SAndrey V. Elsukov struct secasindex *saidx, tmpsaidx; 511fcf59617SAndrey V. Elsukov struct ipsecrequest *isr; 512fcf59617SAndrey V. Elsukov struct sockaddr_in6 *sin6; 513fcf59617SAndrey V. Elsukov struct secasvar *sav; 514fcf59617SAndrey V. Elsukov struct ip6_hdr *ip6; 515fcf59617SAndrey V. Elsukov 516fcf59617SAndrey V. Elsukov /* 517fcf59617SAndrey V. Elsukov * Check system global policy controls. 518fcf59617SAndrey V. Elsukov */ 519fcf59617SAndrey V. Elsukov next: 520fcf59617SAndrey V. Elsukov isr = sp->req[*pidx]; 521fcf59617SAndrey V. Elsukov if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) || 522fcf59617SAndrey V. Elsukov (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) || 523fcf59617SAndrey V. Elsukov (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) { 524fcf59617SAndrey V. Elsukov DPRINTF(("%s: IPsec outbound packet dropped due" 525fcf59617SAndrey V. Elsukov " to policy (check your sysctls)\n", __func__)); 526fcf59617SAndrey V. Elsukov IPSEC_OSTAT_INC(isr->saidx.proto, pdrops); 527fcf59617SAndrey V. Elsukov *error = EHOSTUNREACH; 528fcf59617SAndrey V. Elsukov return (NULL); 529fcf59617SAndrey V. Elsukov } 530fcf59617SAndrey V. Elsukov /* 531fcf59617SAndrey V. Elsukov * Craft SA index to search for proper SA. Note that 532fcf59617SAndrey V. Elsukov * we only fillin unspecified SA peers for transport 533fcf59617SAndrey V. Elsukov * mode; for tunnel mode they must already be filled in. 534fcf59617SAndrey V. Elsukov */ 535fcf59617SAndrey V. Elsukov if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) { 536fcf59617SAndrey V. Elsukov saidx = &tmpsaidx; 537fcf59617SAndrey V. Elsukov *saidx = isr->saidx; 538fcf59617SAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 539fcf59617SAndrey V. Elsukov if (saidx->src.sin6.sin6_len == 0) { 540fcf59617SAndrey V. Elsukov sin6 = (struct sockaddr_in6 *)&saidx->src; 541fcf59617SAndrey V. Elsukov sin6->sin6_len = sizeof(*sin6); 542fcf59617SAndrey V. Elsukov sin6->sin6_family = AF_INET6; 543fcf59617SAndrey V. Elsukov sin6->sin6_port = IPSEC_PORT_ANY; 544fcf59617SAndrey V. Elsukov sin6->sin6_addr = ip6->ip6_src; 545fcf59617SAndrey V. Elsukov if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) { 546fcf59617SAndrey V. Elsukov /* fix scope id for comparing SPD */ 547fcf59617SAndrey V. Elsukov sin6->sin6_addr.s6_addr16[1] = 0; 548fcf59617SAndrey V. Elsukov sin6->sin6_scope_id = 549fcf59617SAndrey V. Elsukov ntohs(ip6->ip6_src.s6_addr16[1]); 550fcf59617SAndrey V. Elsukov } 551fcf59617SAndrey V. Elsukov } 552fcf59617SAndrey V. Elsukov if (saidx->dst.sin6.sin6_len == 0) { 553fcf59617SAndrey V. Elsukov sin6 = (struct sockaddr_in6 *)&saidx->dst; 554fcf59617SAndrey V. Elsukov sin6->sin6_len = sizeof(*sin6); 555fcf59617SAndrey V. Elsukov sin6->sin6_family = AF_INET6; 556fcf59617SAndrey V. Elsukov sin6->sin6_port = IPSEC_PORT_ANY; 557fcf59617SAndrey V. Elsukov sin6->sin6_addr = ip6->ip6_dst; 558fcf59617SAndrey V. Elsukov if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) { 559fcf59617SAndrey V. Elsukov /* fix scope id for comparing SPD */ 560fcf59617SAndrey V. Elsukov sin6->sin6_addr.s6_addr16[1] = 0; 561fcf59617SAndrey V. Elsukov sin6->sin6_scope_id = 562fcf59617SAndrey V. Elsukov ntohs(ip6->ip6_dst.s6_addr16[1]); 563fcf59617SAndrey V. Elsukov } 564fcf59617SAndrey V. Elsukov } 565fcf59617SAndrey V. Elsukov } else 566fcf59617SAndrey V. Elsukov saidx = &sp->req[*pidx]->saidx; 567fcf59617SAndrey V. Elsukov /* 568fcf59617SAndrey V. Elsukov * Lookup SA and validate it. 569fcf59617SAndrey V. Elsukov */ 570fcf59617SAndrey V. Elsukov sav = key_allocsa_policy(sp, saidx, error); 571fcf59617SAndrey V. Elsukov if (sav == NULL) { 572fcf59617SAndrey V. Elsukov IPSEC6STAT_INC(ips_out_nosa); 573fcf59617SAndrey V. Elsukov if (*error != 0) 574fcf59617SAndrey V. Elsukov return (NULL); 575fcf59617SAndrey V. Elsukov if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) { 576fcf59617SAndrey V. Elsukov /* 577fcf59617SAndrey V. Elsukov * We have no SA and policy that doesn't require 578fcf59617SAndrey V. Elsukov * this IPsec transform, thus we can continue w/o 579fcf59617SAndrey V. Elsukov * IPsec processing, i.e. return EJUSTRETURN. 580fcf59617SAndrey V. Elsukov * But first check if there is some bundled transform. 581fcf59617SAndrey V. Elsukov */ 582fcf59617SAndrey V. Elsukov if (sp->tcount > ++(*pidx)) 583fcf59617SAndrey V. Elsukov goto next; 584fcf59617SAndrey V. Elsukov *error = EJUSTRETURN; 585fcf59617SAndrey V. Elsukov } 586fcf59617SAndrey V. Elsukov return (NULL); 587fcf59617SAndrey V. Elsukov } 588fcf59617SAndrey V. Elsukov IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform")); 589fcf59617SAndrey V. Elsukov return (sav); 590fcf59617SAndrey V. Elsukov } 591fcf59617SAndrey V. Elsukov 592fcf59617SAndrey V. Elsukov /* 593fcf59617SAndrey V. Elsukov * IPsec output logic for IPv6. 594fcf59617SAndrey V. Elsukov */ 595fcf59617SAndrey V. Elsukov static int 596de1da299SKonstantin Belousov ipsec6_perform_request(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp, 59700524fd4SKonstantin Belousov struct inpcb *inp, u_int idx, u_long mtu) 598fcf59617SAndrey V. Elsukov { 599fcf59617SAndrey V. Elsukov struct ipsec_ctx_data ctx; 600fcf59617SAndrey V. Elsukov union sockaddr_union *dst; 601fcf59617SAndrey V. Elsukov struct secasvar *sav; 602fcf59617SAndrey V. Elsukov struct ip6_hdr *ip6; 603*240b7bfeSKonstantin Belousov int error, hwassist, i, off; 604*240b7bfeSKonstantin Belousov bool accel; 605fcf59617SAndrey V. Elsukov 606fcf59617SAndrey V. Elsukov IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx)); 607fcf59617SAndrey V. Elsukov 608de1da299SKonstantin Belousov sav = ipsec6_allocsa(ifp, m, sp, &idx, &error); 609fcf59617SAndrey V. Elsukov if (sav == NULL) { 610fcf59617SAndrey V. Elsukov if (error == EJUSTRETURN) { /* No IPsec required */ 611ef2a572bSKonstantin Belousov (void)ipsec_accel_output(ifp, m, inp, sp, NULL, 612*240b7bfeSKonstantin Belousov AF_INET6, mtu, &hwassist); 613fcf59617SAndrey V. Elsukov key_freesp(&sp); 614fcf59617SAndrey V. Elsukov return (error); 615fcf59617SAndrey V. Elsukov } 616fcf59617SAndrey V. Elsukov goto bad; 617fcf59617SAndrey V. Elsukov } 618fcf59617SAndrey V. Elsukov 619fcf59617SAndrey V. Elsukov /* Fix IP length in case if it is not set yet. */ 620fcf59617SAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 621fcf59617SAndrey V. Elsukov ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 622fcf59617SAndrey V. Elsukov 6231a01e0e7SAndrey V. Elsukov IPSEC_INIT_CTX(&ctx, &m, inp, sav, AF_INET6, IPSEC_ENC_BEFORE); 624fcf59617SAndrey V. Elsukov if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0) 625fcf59617SAndrey V. Elsukov goto bad; 626fcf59617SAndrey V. Elsukov 627*240b7bfeSKonstantin Belousov hwassist = 0; 628*240b7bfeSKonstantin Belousov accel = ipsec_accel_output(ifp, m, inp, sp, sav, AF_INET6, mtu, 629*240b7bfeSKonstantin Belousov &hwassist); 630*240b7bfeSKonstantin Belousov 631*240b7bfeSKonstantin Belousov /* 632*240b7bfeSKonstantin Belousov * Do delayed checksums now because we send before 633*240b7bfeSKonstantin Belousov * this is done in the normal processing path. 634*240b7bfeSKonstantin Belousov */ 635*240b7bfeSKonstantin Belousov if ((m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6 & ~hwassist) != 0) { 636*240b7bfeSKonstantin Belousov in6_delayed_cksum(m, m->m_pkthdr.len - 637*240b7bfeSKonstantin Belousov sizeof(struct ip6_hdr), sizeof(struct ip6_hdr)); 638*240b7bfeSKonstantin Belousov m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6; 639*240b7bfeSKonstantin Belousov } 640*240b7bfeSKonstantin Belousov #if defined(SCTP) || defined(SCTP_SUPPORT) 641*240b7bfeSKonstantin Belousov if ((m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6 & ~hwassist) != 0) { 642*240b7bfeSKonstantin Belousov sctp_delayed_cksum(m, sizeof(struct ip6_hdr)); 643*240b7bfeSKonstantin Belousov m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6; 644*240b7bfeSKonstantin Belousov } 645*240b7bfeSKonstantin Belousov #endif 646*240b7bfeSKonstantin Belousov if (accel) 647ef2a572bSKonstantin Belousov return (EJUSTRETURN); 648ef2a572bSKonstantin Belousov 649fcf59617SAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); /* pfil can change mbuf */ 650fcf59617SAndrey V. Elsukov dst = &sav->sah->saidx.dst; 651fcf59617SAndrey V. Elsukov 652fcf59617SAndrey V. Elsukov /* Do the appropriate encapsulation, if necessary */ 653fcf59617SAndrey V. Elsukov if (sp->req[idx]->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */ 654fcf59617SAndrey V. Elsukov dst->sa.sa_family != AF_INET6 || /* PF mismatch */ 655fcf59617SAndrey V. Elsukov ((dst->sa.sa_family == AF_INET6) && 656fcf59617SAndrey V. Elsukov (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) && 657fcf59617SAndrey V. Elsukov (!in6_sa_equal_addrwithscope(&dst->sin6, &ip6->ip6_dst)))) { 658fcf59617SAndrey V. Elsukov if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) { 659fcf59617SAndrey V. Elsukov /* No jumbogram support. */ 660fcf59617SAndrey V. Elsukov error = ENXIO; /*XXX*/ 661fcf59617SAndrey V. Elsukov goto bad; 662fcf59617SAndrey V. Elsukov } 663fcf59617SAndrey V. Elsukov error = ipsec_encap(&m, &sav->sah->saidx); 664fcf59617SAndrey V. Elsukov if (error != 0) { 6657f1f6591SAndrey V. Elsukov DPRINTF(("%s: encapsulation for SPI 0x%08x failed " 6667f1f6591SAndrey V. Elsukov "with error %d\n", __func__, ntohl(sav->spi), 6677f1f6591SAndrey V. Elsukov error)); 668fcf59617SAndrey V. Elsukov /* XXXAE: IPSEC_OSTAT_INC(tunnel); */ 669fcf59617SAndrey V. Elsukov goto bad; 670fcf59617SAndrey V. Elsukov } 6711a01e0e7SAndrey V. Elsukov inp = NULL; 672fcf59617SAndrey V. Elsukov } 673fcf59617SAndrey V. Elsukov 6741a01e0e7SAndrey V. Elsukov IPSEC_INIT_CTX(&ctx, &m, inp, sav, dst->sa.sa_family, IPSEC_ENC_AFTER); 675fcf59617SAndrey V. Elsukov if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0) 676fcf59617SAndrey V. Elsukov goto bad; 677fcf59617SAndrey V. Elsukov 678fcf59617SAndrey V. Elsukov switch(dst->sa.sa_family) { 679fcf59617SAndrey V. Elsukov #ifdef INET 680fcf59617SAndrey V. Elsukov case AF_INET: 681fcf59617SAndrey V. Elsukov { 682fcf59617SAndrey V. Elsukov struct ip *ip; 683fcf59617SAndrey V. Elsukov ip = mtod(m, struct ip *); 684fcf59617SAndrey V. Elsukov i = ip->ip_hl << 2; 685fcf59617SAndrey V. Elsukov off = offsetof(struct ip, ip_p); 686fcf59617SAndrey V. Elsukov } 687fcf59617SAndrey V. Elsukov break; 688fcf59617SAndrey V. Elsukov #endif /* AF_INET */ 689fcf59617SAndrey V. Elsukov case AF_INET6: 690fcf59617SAndrey V. Elsukov i = sizeof(struct ip6_hdr); 691fcf59617SAndrey V. Elsukov off = offsetof(struct ip6_hdr, ip6_nxt); 692fcf59617SAndrey V. Elsukov break; 693fcf59617SAndrey V. Elsukov default: 694fcf59617SAndrey V. Elsukov DPRINTF(("%s: unsupported protocol family %u\n", 695fcf59617SAndrey V. Elsukov __func__, dst->sa.sa_family)); 696fcf59617SAndrey V. Elsukov error = EPFNOSUPPORT; 697fcf59617SAndrey V. Elsukov IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf); 698fcf59617SAndrey V. Elsukov goto bad; 699fcf59617SAndrey V. Elsukov } 700fcf59617SAndrey V. Elsukov error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off); 701fcf59617SAndrey V. Elsukov return (error); 702fcf59617SAndrey V. Elsukov bad: 703fcf59617SAndrey V. Elsukov IPSEC6STAT_INC(ips_out_inval); 704fcf59617SAndrey V. Elsukov if (m != NULL) 705fcf59617SAndrey V. Elsukov m_freem(m); 706fcf59617SAndrey V. Elsukov if (sav != NULL) 707fcf59617SAndrey V. Elsukov key_freesav(&sav); 708fcf59617SAndrey V. Elsukov key_freesp(&sp); 709fcf59617SAndrey V. Elsukov return (error); 710fcf59617SAndrey V. Elsukov } 711fcf59617SAndrey V. Elsukov 712fcf59617SAndrey V. Elsukov int 713de1da299SKonstantin Belousov ipsec6_process_packet(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp, 71400524fd4SKonstantin Belousov struct inpcb *inp, u_long mtu) 715fcf59617SAndrey V. Elsukov { 716fcf59617SAndrey V. Elsukov 71700524fd4SKonstantin Belousov return (ipsec6_perform_request(ifp, m, sp, inp, 0, mtu)); 718fcf59617SAndrey V. Elsukov } 719fcf59617SAndrey V. Elsukov 7209dfc8606SBartlomiej Grzesik /* 7219dfc8606SBartlomiej Grzesik * IPv6 implementation is based on IPv4 implementation. 7229dfc8606SBartlomiej Grzesik */ 7239dfc8606SBartlomiej Grzesik int 724de1da299SKonstantin Belousov ipsec6_check_pmtu(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp, 725de1da299SKonstantin Belousov int forwarding) 7269dfc8606SBartlomiej Grzesik { 7279dfc8606SBartlomiej Grzesik struct secasvar *sav; 7289dfc8606SBartlomiej Grzesik size_t hlen, pmtu; 7299dfc8606SBartlomiej Grzesik uint32_t idx; 7309dfc8606SBartlomiej Grzesik int error; 7319dfc8606SBartlomiej Grzesik 7329dfc8606SBartlomiej Grzesik /* 7339dfc8606SBartlomiej Grzesik * According to RFC8200 L3 fragmentation is supposed to be done only on 7349dfc8606SBartlomiej Grzesik * locally generated packets. During L3 forwarding packets that are too 7359dfc8606SBartlomiej Grzesik * big are always supposed to be dropped, with an ICMPv6 packet being 7369dfc8606SBartlomiej Grzesik * sent back. 7379dfc8606SBartlomiej Grzesik */ 7389dfc8606SBartlomiej Grzesik if (!forwarding) 7399dfc8606SBartlomiej Grzesik return (0); 7409dfc8606SBartlomiej Grzesik 7419dfc8606SBartlomiej Grzesik idx = sp->tcount - 1; 742de1da299SKonstantin Belousov sav = ipsec6_allocsa(ifp, m, sp, &idx, &error); 7439dfc8606SBartlomiej Grzesik if (sav == NULL) { 7449dfc8606SBartlomiej Grzesik key_freesp(&sp); 7459dfc8606SBartlomiej Grzesik /* 7469dfc8606SBartlomiej Grzesik * No matching SA was found and SADB_ACQUIRE message was generated. 7479dfc8606SBartlomiej Grzesik * Since we have matched a SP to this packet drop it silently. 7489dfc8606SBartlomiej Grzesik */ 7499dfc8606SBartlomiej Grzesik if (error == 0) 7509dfc8606SBartlomiej Grzesik error = EINPROGRESS; 7519dfc8606SBartlomiej Grzesik if (error != EJUSTRETURN) 7529dfc8606SBartlomiej Grzesik m_freem(m); 7539dfc8606SBartlomiej Grzesik 7549dfc8606SBartlomiej Grzesik return (error); 7559dfc8606SBartlomiej Grzesik } 7569dfc8606SBartlomiej Grzesik 7579dfc8606SBartlomiej Grzesik pmtu = ipsec_get_pmtu(sav); 7589dfc8606SBartlomiej Grzesik if (pmtu == 0) { 7599dfc8606SBartlomiej Grzesik key_freesav(&sav); 7609dfc8606SBartlomiej Grzesik return (0); 7619dfc8606SBartlomiej Grzesik } 7629dfc8606SBartlomiej Grzesik 7639dfc8606SBartlomiej Grzesik hlen = ipsec_hdrsiz_internal(sp); 7649dfc8606SBartlomiej Grzesik key_freesav(&sav); 7659dfc8606SBartlomiej Grzesik 7669dfc8606SBartlomiej Grzesik if (m_length(m, NULL) + hlen > pmtu) { 7679dfc8606SBartlomiej Grzesik /* 7689dfc8606SBartlomiej Grzesik * If we're forwarding generate ICMPv6 message here, 769c27214f0SWojciech Macek * so that it contains pmtu subtracted by header size. 7709dfc8606SBartlomiej Grzesik * Set error to EINPROGRESS, in order for the frame 7719dfc8606SBartlomiej Grzesik * to be dropped silently. 7729dfc8606SBartlomiej Grzesik */ 7739dfc8606SBartlomiej Grzesik if (forwarding) { 7749dfc8606SBartlomiej Grzesik if (pmtu > hlen) 7759dfc8606SBartlomiej Grzesik icmp6_error(m, ICMP6_PACKET_TOO_BIG, 0, pmtu - hlen); 7769dfc8606SBartlomiej Grzesik else 7779dfc8606SBartlomiej Grzesik m_freem(m); 7789dfc8606SBartlomiej Grzesik 7799dfc8606SBartlomiej Grzesik key_freesp(&sp); 7809dfc8606SBartlomiej Grzesik return (EINPROGRESS); /* Pretend that we consumed it. */ 7819dfc8606SBartlomiej Grzesik } 7829dfc8606SBartlomiej Grzesik } 7839dfc8606SBartlomiej Grzesik 7849dfc8606SBartlomiej Grzesik return (0); 7859dfc8606SBartlomiej Grzesik } 7869dfc8606SBartlomiej Grzesik 787fcf59617SAndrey V. Elsukov static int 788de1da299SKonstantin Belousov ipsec6_common_output(struct ifnet *ifp, struct mbuf *m, struct inpcb *inp, 78900524fd4SKonstantin Belousov int forwarding, u_long mtu) 790fcf59617SAndrey V. Elsukov { 791fcf59617SAndrey V. Elsukov struct secpolicy *sp; 792fcf59617SAndrey V. Elsukov int error; 793fcf59617SAndrey V. Elsukov 794fcf59617SAndrey V. Elsukov /* Lookup for the corresponding outbound security policy */ 79522bbefb2SAndrey V. Elsukov sp = ipsec6_checkpolicy(m, inp, &error, !forwarding); 796fcf59617SAndrey V. Elsukov if (sp == NULL) { 797fcf59617SAndrey V. Elsukov if (error == -EINVAL) { 798fcf59617SAndrey V. Elsukov /* Discarded by policy. */ 799fcf59617SAndrey V. Elsukov m_freem(m); 800fcf59617SAndrey V. Elsukov return (EACCES); 801fcf59617SAndrey V. Elsukov } 802fcf59617SAndrey V. Elsukov return (0); /* No IPsec required. */ 803fcf59617SAndrey V. Elsukov } 804fcf59617SAndrey V. Elsukov 805de1da299SKonstantin Belousov error = ipsec6_check_pmtu(ifp, m, sp, forwarding); 8069dfc8606SBartlomiej Grzesik if (error != 0) { 8079dfc8606SBartlomiej Grzesik if (error == EJUSTRETURN) 8089dfc8606SBartlomiej Grzesik return (0); 8099dfc8606SBartlomiej Grzesik 8109dfc8606SBartlomiej Grzesik return (error); 8119dfc8606SBartlomiej Grzesik } 8129dfc8606SBartlomiej Grzesik 813fcf59617SAndrey V. Elsukov /* NB: callee frees mbuf and releases reference to SP */ 81400524fd4SKonstantin Belousov error = ipsec6_process_packet(ifp, m, sp, inp, mtu); 815fcf59617SAndrey V. Elsukov if (error == EJUSTRETURN) { 816fcf59617SAndrey V. Elsukov /* 817fcf59617SAndrey V. Elsukov * We had a SP with a level of 'use' and no SA. We 818fcf59617SAndrey V. Elsukov * will just continue to process the packet without 819fcf59617SAndrey V. Elsukov * IPsec processing and return without error. 820fcf59617SAndrey V. Elsukov */ 821fcf59617SAndrey V. Elsukov return (0); 822fcf59617SAndrey V. Elsukov } 823fcf59617SAndrey V. Elsukov if (error == 0) 824fcf59617SAndrey V. Elsukov return (EINPROGRESS); /* consumed by IPsec */ 825fcf59617SAndrey V. Elsukov return (error); 826fcf59617SAndrey V. Elsukov } 827fcf59617SAndrey V. Elsukov 828fcf59617SAndrey V. Elsukov /* 829fcf59617SAndrey V. Elsukov * IPSEC_OUTPUT() method implementation for IPv6. 830fcf59617SAndrey V. Elsukov * 0 - no IPsec handling needed 831fcf59617SAndrey V. Elsukov * other values - mbuf consumed by IPsec. 832fcf59617SAndrey V. Elsukov */ 833fcf59617SAndrey V. Elsukov int 83400524fd4SKonstantin Belousov ipsec6_output(struct ifnet *ifp, struct mbuf *m, struct inpcb *inp, u_long mtu) 835fcf59617SAndrey V. Elsukov { 836fcf59617SAndrey V. Elsukov 837fcf59617SAndrey V. Elsukov /* 838fcf59617SAndrey V. Elsukov * If the packet is resubmitted to ip_output (e.g. after 839fcf59617SAndrey V. Elsukov * AH, ESP, etc. processing), there will be a tag to bypass 840fcf59617SAndrey V. Elsukov * the lookup and related policy checking. 841fcf59617SAndrey V. Elsukov */ 842fcf59617SAndrey V. Elsukov if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL) 843fcf59617SAndrey V. Elsukov return (0); 844fcf59617SAndrey V. Elsukov 84500524fd4SKonstantin Belousov return (ipsec6_common_output(ifp, m, inp, 0, mtu)); 846fcf59617SAndrey V. Elsukov } 847fcf59617SAndrey V. Elsukov 848fcf59617SAndrey V. Elsukov /* 849fcf59617SAndrey V. Elsukov * IPSEC_FORWARD() method implementation for IPv6. 850fcf59617SAndrey V. Elsukov * 0 - no IPsec handling needed 851fcf59617SAndrey V. Elsukov * other values - mbuf consumed by IPsec. 852fcf59617SAndrey V. Elsukov */ 853fcf59617SAndrey V. Elsukov int 854fcf59617SAndrey V. Elsukov ipsec6_forward(struct mbuf *m) 855fcf59617SAndrey V. Elsukov { 856fcf59617SAndrey V. Elsukov 857fcf59617SAndrey V. Elsukov /* 858fcf59617SAndrey V. Elsukov * Check if this packet has an active inbound SP and needs to be 859fcf59617SAndrey V. Elsukov * dropped instead of forwarded. 860fcf59617SAndrey V. Elsukov */ 861fcf59617SAndrey V. Elsukov if (ipsec6_in_reject(m, NULL) != 0) { 862fcf59617SAndrey V. Elsukov m_freem(m); 863fcf59617SAndrey V. Elsukov return (EACCES); 864fcf59617SAndrey V. Elsukov } 86500524fd4SKonstantin Belousov return (ipsec6_common_output(NULL /* XXXKIB */, m, NULL, 1, 0)); 866fcf59617SAndrey V. Elsukov } 867fcf59617SAndrey V. Elsukov #endif /* INET6 */ 868fcf59617SAndrey V. Elsukov 869fcf59617SAndrey V. Elsukov int 870fcf59617SAndrey V. Elsukov ipsec_process_done(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav, 871fcf59617SAndrey V. Elsukov u_int idx) 872fcf59617SAndrey V. Elsukov { 873f82eb2a6SJohn Baldwin struct epoch_tracker et; 874fcf59617SAndrey V. Elsukov struct xform_history *xh; 875fcf59617SAndrey V. Elsukov struct secasindex *saidx; 876fcf59617SAndrey V. Elsukov struct m_tag *mtag; 877fcf59617SAndrey V. Elsukov int error; 87888768458SSam Leffler 879ef2a572bSKonstantin Belousov if (sav->state >= SADB_SASTATE_DEAD) { 880ef2a572bSKonstantin Belousov error = ESRCH; 881ef2a572bSKonstantin Belousov goto bad; 882ef2a572bSKonstantin Belousov } 88388768458SSam Leffler saidx = &sav->sah->saidx; 88488768458SSam Leffler switch (saidx->dst.sa.sa_family) { 88588768458SSam Leffler #ifdef INET 88688768458SSam Leffler case AF_INET: 88788768458SSam Leffler /* Fix the header length, for AH processing. */ 88888768458SSam Leffler mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len); 88988768458SSam Leffler break; 89088768458SSam Leffler #endif /* INET */ 89188768458SSam Leffler #ifdef INET6 89288768458SSam Leffler case AF_INET6: 89388768458SSam Leffler /* Fix the header length, for AH processing. */ 89488768458SSam Leffler if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) { 89588768458SSam Leffler error = ENXIO; 89688768458SSam Leffler goto bad; 89788768458SSam Leffler } 89888768458SSam Leffler if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) { 89988768458SSam Leffler /* No jumbogram support. */ 90088768458SSam Leffler error = ENXIO; /*?*/ 90188768458SSam Leffler goto bad; 90288768458SSam Leffler } 90388768458SSam Leffler mtod(m, struct ip6_hdr *)->ip6_plen = 90488768458SSam Leffler htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 90588768458SSam Leffler break; 90688768458SSam Leffler #endif /* INET6 */ 90788768458SSam Leffler default: 9089ffa9677SSam Leffler DPRINTF(("%s: unknown protocol family %u\n", __func__, 90988768458SSam Leffler saidx->dst.sa.sa_family)); 91088768458SSam Leffler error = ENXIO; 91188768458SSam Leffler goto bad; 91288768458SSam Leffler } 91388768458SSam Leffler 91488768458SSam Leffler /* 915fcf59617SAndrey V. Elsukov * Add a record of what we've done to the packet. 91688768458SSam Leffler */ 917fcf59617SAndrey V. Elsukov mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, sizeof(*xh), M_NOWAIT); 91888768458SSam Leffler if (mtag == NULL) { 9199ffa9677SSam Leffler DPRINTF(("%s: could not get packet tag\n", __func__)); 92088768458SSam Leffler error = ENOMEM; 92188768458SSam Leffler goto bad; 92288768458SSam Leffler } 92388768458SSam Leffler 924fcf59617SAndrey V. Elsukov xh = (struct xform_history *)(mtag + 1); 925fcf59617SAndrey V. Elsukov xh->dst = saidx->dst; 926fcf59617SAndrey V. Elsukov xh->proto = saidx->proto; 927fcf59617SAndrey V. Elsukov xh->mode = saidx->mode; 928fcf59617SAndrey V. Elsukov xh->spi = sav->spi; 92988768458SSam Leffler m_tag_prepend(m, mtag); 93088768458SSam Leffler 93159959de5SErmal Luçi key_sa_recordxfer(sav, m); /* record data transfer */ 93259959de5SErmal Luçi 93388768458SSam Leffler /* 93488768458SSam Leffler * If there's another (bundled) SA to apply, do so. 93588768458SSam Leffler * Note that this puts a burden on the kernel stack size. 93688768458SSam Leffler * If this is a problem we'll need to introduce a queue 93788768458SSam Leffler * to set the packet on so we can unwind the stack before 93888768458SSam Leffler * doing further processing. 93988768458SSam Leffler */ 940fcf59617SAndrey V. Elsukov if (++idx < sp->tcount) { 941db178eb8SBjoern A. Zeeb switch (saidx->dst.sa.sa_family) { 942db178eb8SBjoern A. Zeeb #ifdef INET 943db178eb8SBjoern A. Zeeb case AF_INET: 944fcf59617SAndrey V. Elsukov key_freesav(&sav); 945f9d8f665SAndrey V. Elsukov IPSECSTAT_INC(ips_out_bundlesa); 946de1da299SKonstantin Belousov return (ipsec4_perform_request(NULL, m, sp, NULL, 94700524fd4SKonstantin Belousov idx, 0)); 948db178eb8SBjoern A. Zeeb /* NOTREACHED */ 949db178eb8SBjoern A. Zeeb #endif 950db178eb8SBjoern A. Zeeb #ifdef INET6 951db178eb8SBjoern A. Zeeb case AF_INET6: 952fcf59617SAndrey V. Elsukov key_freesav(&sav); 953f9d8f665SAndrey V. Elsukov IPSEC6STAT_INC(ips_out_bundlesa); 954de1da299SKonstantin Belousov return (ipsec6_perform_request(NULL, m, sp, NULL, 95500524fd4SKonstantin Belousov idx, 0)); 956db178eb8SBjoern A. Zeeb /* NOTREACHED */ 957db178eb8SBjoern A. Zeeb #endif /* INET6 */ 958db178eb8SBjoern A. Zeeb default: 959db178eb8SBjoern A. Zeeb DPRINTF(("%s: unknown protocol family %u\n", __func__, 960db178eb8SBjoern A. Zeeb saidx->dst.sa.sa_family)); 961fcf59617SAndrey V. Elsukov error = EPFNOSUPPORT; 962db178eb8SBjoern A. Zeeb goto bad; 963db178eb8SBjoern A. Zeeb } 96488768458SSam Leffler } 96588768458SSam Leffler 966fcf59617SAndrey V. Elsukov key_freesp(&sp), sp = NULL; /* Release reference to SP */ 96780044c78SXavier Beaudouin #if defined(INET) || defined(INET6) 968fcf59617SAndrey V. Elsukov /* 969fcf59617SAndrey V. Elsukov * Do UDP encapsulation if SA requires it. 970fcf59617SAndrey V. Elsukov */ 971fcf59617SAndrey V. Elsukov if (sav->natt != NULL) { 972fcf59617SAndrey V. Elsukov error = udp_ipsec_output(m, sav); 973fcf59617SAndrey V. Elsukov if (error != 0) 974fcf59617SAndrey V. Elsukov goto bad; 975fcf59617SAndrey V. Elsukov } 97680044c78SXavier Beaudouin #endif /* INET || INET6 */ 97788768458SSam Leffler /* 97888768458SSam Leffler * We're done with IPsec processing, transmit the packet using the 9796508929bSAndrey V. Elsukov * appropriate network protocol (IP or IPv6). 98088768458SSam Leffler */ 981f82eb2a6SJohn Baldwin NET_EPOCH_ENTER(et); 98288768458SSam Leffler switch (saidx->dst.sa.sa_family) { 98388768458SSam Leffler #ifdef INET 98488768458SSam Leffler case AF_INET: 985fcf59617SAndrey V. Elsukov key_freesav(&sav); 986f82eb2a6SJohn Baldwin error = ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL); 987f82eb2a6SJohn Baldwin break; 98888768458SSam Leffler #endif /* INET */ 98988768458SSam Leffler #ifdef INET6 99088768458SSam Leffler case AF_INET6: 991fcf59617SAndrey V. Elsukov key_freesav(&sav); 992f82eb2a6SJohn Baldwin error = ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL); 993f82eb2a6SJohn Baldwin break; 99488768458SSam Leffler #endif /* INET6 */ 995f82eb2a6SJohn Baldwin default: 99688768458SSam Leffler panic("ipsec_process_done"); 997f82eb2a6SJohn Baldwin } 998f82eb2a6SJohn Baldwin NET_EPOCH_EXIT(et); 999f82eb2a6SJohn Baldwin return (error); 100088768458SSam Leffler bad: 100188768458SSam Leffler m_freem(m); 1002fcf59617SAndrey V. Elsukov key_freesav(&sav); 1003fcf59617SAndrey V. Elsukov if (sp != NULL) 1004fcf59617SAndrey V. Elsukov key_freesp(&sp); 100588768458SSam Leffler return (error); 100688768458SSam Leffler } 100788768458SSam Leffler 1008fcf59617SAndrey V. Elsukov /* 1009fcf59617SAndrey V. Elsukov * ipsec_prepend() is optimized version of M_PREPEND(). 1010fcf59617SAndrey V. Elsukov * ipsec_encap() is called by IPsec output routine for tunnel mode SA. 1011fcf59617SAndrey V. Elsukov * It is expected that after IP encapsulation some IPsec transform will 1012fcf59617SAndrey V. Elsukov * be performed. Each IPsec transform inserts its variable length header 1013fcf59617SAndrey V. Elsukov * just after outer IP header using m_makespace(). If given mbuf has not 1014fcf59617SAndrey V. Elsukov * enough free space at the beginning, we allocate new mbuf and reserve 1015fcf59617SAndrey V. Elsukov * some space at the beginning and at the end. 1016fcf59617SAndrey V. Elsukov * This helps avoid allocating of new mbuf and data copying in m_makespace(), 1017fcf59617SAndrey V. Elsukov * we place outer header in the middle of mbuf's data with reserved leading 1018fcf59617SAndrey V. Elsukov * and trailing space: 1019fcf59617SAndrey V. Elsukov * [ LEADINGSPACE ][ Outer IP header ][ TRAILINGSPACE ] 1020fcf59617SAndrey V. Elsukov * LEADINGSPACE will be used to add ethernet header, TRAILINGSPACE will 1021fcf59617SAndrey V. Elsukov * be used to inject AH/ESP/IPCOMP header. 1022fcf59617SAndrey V. Elsukov */ 1023fcf59617SAndrey V. Elsukov #define IPSEC_TRAILINGSPACE (sizeof(struct udphdr) +/* NAT-T */ \ 1024fcf59617SAndrey V. Elsukov max(sizeof(struct newesp) + EALG_MAX_BLOCK_LEN, /* ESP + IV */ \ 1025fcf59617SAndrey V. Elsukov sizeof(struct newah) + HASH_MAX_LEN /* AH + ICV */)) 1026fcf59617SAndrey V. Elsukov static struct mbuf * 1027fcf59617SAndrey V. Elsukov ipsec_prepend(struct mbuf *m, int len, int how) 102888768458SSam Leffler { 1029fcf59617SAndrey V. Elsukov struct mbuf *n; 103088768458SSam Leffler 1031fcf59617SAndrey V. Elsukov M_ASSERTPKTHDR(m); 1032fcf59617SAndrey V. Elsukov IPSEC_ASSERT(len < MHLEN, ("wrong length")); 1033fcf59617SAndrey V. Elsukov if (M_LEADINGSPACE(m) >= len) { 1034fcf59617SAndrey V. Elsukov /* No need to allocate new mbuf. */ 1035fcf59617SAndrey V. Elsukov m->m_data -= len; 1036fcf59617SAndrey V. Elsukov m->m_len += len; 1037fcf59617SAndrey V. Elsukov m->m_pkthdr.len += len; 1038fcf59617SAndrey V. Elsukov return (m); 103988768458SSam Leffler } 1040fcf59617SAndrey V. Elsukov n = m_gethdr(how, m->m_type); 1041fcf59617SAndrey V. Elsukov if (n == NULL) { 1042fcf59617SAndrey V. Elsukov m_freem(m); 1043fcf59617SAndrey V. Elsukov return (NULL); 104488768458SSam Leffler } 1045fcf59617SAndrey V. Elsukov m_move_pkthdr(n, m); 1046fcf59617SAndrey V. Elsukov n->m_next = m; 1047fcf59617SAndrey V. Elsukov if (len + IPSEC_TRAILINGSPACE < M_SIZE(n)) 1048fcf59617SAndrey V. Elsukov m_align(n, len + IPSEC_TRAILINGSPACE); 1049fcf59617SAndrey V. Elsukov n->m_len = len; 1050fcf59617SAndrey V. Elsukov n->m_pkthdr.len += len; 1051fcf59617SAndrey V. Elsukov return (n); 105288768458SSam Leffler } 105388768458SSam Leffler 10549dfc8606SBartlomiej Grzesik static size_t 10559dfc8606SBartlomiej Grzesik ipsec_get_pmtu(struct secasvar *sav) 10569dfc8606SBartlomiej Grzesik { 10579dfc8606SBartlomiej Grzesik union sockaddr_union *dst; 10589dfc8606SBartlomiej Grzesik struct in_conninfo inc; 10599dfc8606SBartlomiej Grzesik size_t pmtu; 10609dfc8606SBartlomiej Grzesik 10619dfc8606SBartlomiej Grzesik dst = &sav->sah->saidx.dst; 10629dfc8606SBartlomiej Grzesik memset(&inc, 0, sizeof(inc)); 10639dfc8606SBartlomiej Grzesik 10649dfc8606SBartlomiej Grzesik switch (dst->sa.sa_family) { 10659dfc8606SBartlomiej Grzesik #ifdef INET 10669dfc8606SBartlomiej Grzesik case AF_INET: 10679dfc8606SBartlomiej Grzesik inc.inc_faddr = satosin(&dst->sa)->sin_addr; 10689dfc8606SBartlomiej Grzesik break; 10699dfc8606SBartlomiej Grzesik #endif 10709dfc8606SBartlomiej Grzesik #ifdef INET6 10719dfc8606SBartlomiej Grzesik case AF_INET6: 10729dfc8606SBartlomiej Grzesik inc.inc6_faddr = satosin6(&dst->sa)->sin6_addr; 10739dfc8606SBartlomiej Grzesik inc.inc_flags |= INC_ISIPV6; 10749dfc8606SBartlomiej Grzesik break; 10759dfc8606SBartlomiej Grzesik #endif 10769dfc8606SBartlomiej Grzesik default: 10779dfc8606SBartlomiej Grzesik return (0); 10789dfc8606SBartlomiej Grzesik } 10799dfc8606SBartlomiej Grzesik 10809dfc8606SBartlomiej Grzesik pmtu = tcp_hc_getmtu(&inc); 10819dfc8606SBartlomiej Grzesik if (pmtu != 0) 10829dfc8606SBartlomiej Grzesik return (pmtu); 10839dfc8606SBartlomiej Grzesik 10849dfc8606SBartlomiej Grzesik /* No entry in hostcache. Assume that PMTU is equal to link's MTU */ 10859dfc8606SBartlomiej Grzesik switch (dst->sa.sa_family) { 10869dfc8606SBartlomiej Grzesik #ifdef INET 10879dfc8606SBartlomiej Grzesik case AF_INET: 10889dfc8606SBartlomiej Grzesik pmtu = tcp_maxmtu(&inc, NULL); 10899dfc8606SBartlomiej Grzesik break; 10909dfc8606SBartlomiej Grzesik #endif 10919dfc8606SBartlomiej Grzesik #ifdef INET6 10929dfc8606SBartlomiej Grzesik case AF_INET6: 10939dfc8606SBartlomiej Grzesik pmtu = tcp_maxmtu6(&inc, NULL); 10949dfc8606SBartlomiej Grzesik break; 10959dfc8606SBartlomiej Grzesik #endif 10969dfc8606SBartlomiej Grzesik default: 10979dfc8606SBartlomiej Grzesik return (0); 10989dfc8606SBartlomiej Grzesik } 10999dfc8606SBartlomiej Grzesik if (pmtu == 0) 11009dfc8606SBartlomiej Grzesik return (0); 11019dfc8606SBartlomiej Grzesik 11029dfc8606SBartlomiej Grzesik tcp_hc_updatemtu(&inc, pmtu); 11039dfc8606SBartlomiej Grzesik 11049dfc8606SBartlomiej Grzesik return (pmtu); 11059dfc8606SBartlomiej Grzesik } 11069dfc8606SBartlomiej Grzesik 110761f37615SAndrey V. Elsukov static int 110861f37615SAndrey V. Elsukov ipsec_encap(struct mbuf **mp, struct secasindex *saidx) 110961f37615SAndrey V. Elsukov { 111061f37615SAndrey V. Elsukov #ifdef INET6 111161f37615SAndrey V. Elsukov struct ip6_hdr *ip6; 111261f37615SAndrey V. Elsukov #endif 111361f37615SAndrey V. Elsukov struct ip *ip; 111440461785SJohn Baldwin #ifdef INET 111561f37615SAndrey V. Elsukov int setdf; 111640461785SJohn Baldwin #endif 111761f37615SAndrey V. Elsukov uint8_t itos, proto; 111861f37615SAndrey V. Elsukov 111961f37615SAndrey V. Elsukov ip = mtod(*mp, struct ip *); 112061f37615SAndrey V. Elsukov switch (ip->ip_v) { 112161f37615SAndrey V. Elsukov #ifdef INET 112261f37615SAndrey V. Elsukov case IPVERSION: 112361f37615SAndrey V. Elsukov proto = IPPROTO_IPIP; 112461f37615SAndrey V. Elsukov /* 112561f37615SAndrey V. Elsukov * Collect IP_DF state from the inner header 112661f37615SAndrey V. Elsukov * and honor system-wide control of how to handle it. 112761f37615SAndrey V. Elsukov */ 112861f37615SAndrey V. Elsukov switch (V_ip4_ipsec_dfbit) { 112961f37615SAndrey V. Elsukov case 0: /* clear in outer header */ 113061f37615SAndrey V. Elsukov case 1: /* set in outer header */ 113161f37615SAndrey V. Elsukov setdf = V_ip4_ipsec_dfbit; 113261f37615SAndrey V. Elsukov break; 113361f37615SAndrey V. Elsukov default:/* propagate to outer header */ 11346f814d0eSAndrey V. Elsukov setdf = (ip->ip_off & htons(IP_DF)) != 0; 113561f37615SAndrey V. Elsukov } 113661f37615SAndrey V. Elsukov itos = ip->ip_tos; 113761f37615SAndrey V. Elsukov break; 113861f37615SAndrey V. Elsukov #endif 113961f37615SAndrey V. Elsukov #ifdef INET6 114061f37615SAndrey V. Elsukov case (IPV6_VERSION >> 4): 114161f37615SAndrey V. Elsukov proto = IPPROTO_IPV6; 114261f37615SAndrey V. Elsukov ip6 = mtod(*mp, struct ip6_hdr *); 114361f37615SAndrey V. Elsukov itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 114461f37615SAndrey V. Elsukov /* scoped address handling */ 114561f37615SAndrey V. Elsukov in6_clearscope(&ip6->ip6_src); 114661f37615SAndrey V. Elsukov in6_clearscope(&ip6->ip6_dst); 114761f37615SAndrey V. Elsukov break; 114861f37615SAndrey V. Elsukov #endif 114961f37615SAndrey V. Elsukov default: 115061f37615SAndrey V. Elsukov return (EAFNOSUPPORT); 115161f37615SAndrey V. Elsukov } 115261f37615SAndrey V. Elsukov switch (saidx->dst.sa.sa_family) { 115361f37615SAndrey V. Elsukov #ifdef INET 115461f37615SAndrey V. Elsukov case AF_INET: 115561f37615SAndrey V. Elsukov if (saidx->src.sa.sa_family != AF_INET || 115661f37615SAndrey V. Elsukov saidx->src.sin.sin_addr.s_addr == INADDR_ANY || 115761f37615SAndrey V. Elsukov saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) 115861f37615SAndrey V. Elsukov return (EINVAL); 1159fcf59617SAndrey V. Elsukov *mp = ipsec_prepend(*mp, sizeof(struct ip), M_NOWAIT); 116061f37615SAndrey V. Elsukov if (*mp == NULL) 116161f37615SAndrey V. Elsukov return (ENOBUFS); 116261f37615SAndrey V. Elsukov ip = mtod(*mp, struct ip *); 116361f37615SAndrey V. Elsukov ip->ip_v = IPVERSION; 116461f37615SAndrey V. Elsukov ip->ip_hl = sizeof(struct ip) >> 2; 116561f37615SAndrey V. Elsukov ip->ip_p = proto; 116661f37615SAndrey V. Elsukov ip->ip_len = htons((*mp)->m_pkthdr.len); 116761f37615SAndrey V. Elsukov ip->ip_ttl = V_ip_defttl; 116861f37615SAndrey V. Elsukov ip->ip_sum = 0; 116961f37615SAndrey V. Elsukov ip->ip_off = setdf ? htons(IP_DF): 0; 117061f37615SAndrey V. Elsukov ip->ip_src = saidx->src.sin.sin_addr; 117161f37615SAndrey V. Elsukov ip->ip_dst = saidx->dst.sin.sin_addr; 117261f37615SAndrey V. Elsukov ip_ecn_ingress(V_ip4_ipsec_ecn, &ip->ip_tos, &itos); 117361f37615SAndrey V. Elsukov ip_fillid(ip); 117461f37615SAndrey V. Elsukov break; 117561f37615SAndrey V. Elsukov #endif /* INET */ 117661f37615SAndrey V. Elsukov #ifdef INET6 117761f37615SAndrey V. Elsukov case AF_INET6: 117861f37615SAndrey V. Elsukov if (saidx->src.sa.sa_family != AF_INET6 || 117961f37615SAndrey V. Elsukov IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr) || 118061f37615SAndrey V. Elsukov IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr)) 118161f37615SAndrey V. Elsukov return (EINVAL); 1182fcf59617SAndrey V. Elsukov *mp = ipsec_prepend(*mp, sizeof(struct ip6_hdr), M_NOWAIT); 118361f37615SAndrey V. Elsukov if (*mp == NULL) 118461f37615SAndrey V. Elsukov return (ENOBUFS); 118561f37615SAndrey V. Elsukov ip6 = mtod(*mp, struct ip6_hdr *); 118661f37615SAndrey V. Elsukov ip6->ip6_flow = 0; 118761f37615SAndrey V. Elsukov ip6->ip6_vfc = IPV6_VERSION; 118861f37615SAndrey V. Elsukov ip6->ip6_hlim = V_ip6_defhlim; 118961f37615SAndrey V. Elsukov ip6->ip6_nxt = proto; 119061f37615SAndrey V. Elsukov ip6->ip6_dst = saidx->dst.sin6.sin6_addr; 11911ae800e7SAndrey V. Elsukov /* For link-local address embed scope zone id */ 11921ae800e7SAndrey V. Elsukov if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 11931ae800e7SAndrey V. Elsukov ip6->ip6_dst.s6_addr16[1] = 11941ae800e7SAndrey V. Elsukov htons(saidx->dst.sin6.sin6_scope_id & 0xffff); 119561f37615SAndrey V. Elsukov ip6->ip6_src = saidx->src.sin6.sin6_addr; 11961ae800e7SAndrey V. Elsukov if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 11971ae800e7SAndrey V. Elsukov ip6->ip6_src.s6_addr16[1] = 11981ae800e7SAndrey V. Elsukov htons(saidx->src.sin6.sin6_scope_id & 0xffff); 119961f37615SAndrey V. Elsukov ip6->ip6_plen = htons((*mp)->m_pkthdr.len - sizeof(*ip6)); 120061f37615SAndrey V. Elsukov ip_ecn_ingress(V_ip6_ipsec_ecn, &proto, &itos); 120161f37615SAndrey V. Elsukov ip6->ip6_flow |= htonl((uint32_t)proto << 20); 120261f37615SAndrey V. Elsukov break; 120361f37615SAndrey V. Elsukov #endif /* INET6 */ 120461f37615SAndrey V. Elsukov default: 120561f37615SAndrey V. Elsukov return (EAFNOSUPPORT); 120661f37615SAndrey V. Elsukov } 1207fcf59617SAndrey V. Elsukov (*mp)->m_flags &= ~(M_BCAST | M_MCAST); 120861f37615SAndrey V. Elsukov return (0); 120961f37615SAndrey V. Elsukov } 1210