188768458SSam Leffler /* $FreeBSD$ */ 288768458SSam Leffler /* $KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $ */ 388768458SSam Leffler 4c398230bSWarner Losh /*- 551369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 651369649SPedro F. Giffuni * 788768458SSam Leffler * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 888768458SSam Leffler * All rights reserved. 988768458SSam Leffler * 1088768458SSam Leffler * Redistribution and use in source and binary forms, with or without 1188768458SSam Leffler * modification, are permitted provided that the following conditions 1288768458SSam Leffler * are met: 1388768458SSam Leffler * 1. Redistributions of source code must retain the above copyright 1488768458SSam Leffler * notice, this list of conditions and the following disclaimer. 1588768458SSam Leffler * 2. Redistributions in binary form must reproduce the above copyright 1688768458SSam Leffler * notice, this list of conditions and the following disclaimer in the 1788768458SSam Leffler * documentation and/or other materials provided with the distribution. 1888768458SSam Leffler * 3. Neither the name of the project nor the names of its contributors 1988768458SSam Leffler * may be used to endorse or promote products derived from this software 2088768458SSam Leffler * without specific prior written permission. 2188768458SSam Leffler * 2288768458SSam Leffler * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2388768458SSam Leffler * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2488768458SSam Leffler * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2588768458SSam Leffler * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2688768458SSam Leffler * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2788768458SSam Leffler * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2888768458SSam Leffler * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2988768458SSam Leffler * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3088768458SSam Leffler * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3188768458SSam Leffler * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3288768458SSam Leffler * SUCH DAMAGE. 3388768458SSam Leffler */ 3488768458SSam Leffler 3588768458SSam Leffler /* 3688768458SSam Leffler * IPsec controller part. 3788768458SSam Leffler */ 3888768458SSam Leffler 3988768458SSam Leffler #include "opt_inet.h" 4088768458SSam Leffler #include "opt_inet6.h" 4188768458SSam Leffler #include "opt_ipsec.h" 4288768458SSam Leffler 4388768458SSam Leffler #include <sys/param.h> 4488768458SSam Leffler #include <sys/systm.h> 4588768458SSam Leffler #include <sys/malloc.h> 4688768458SSam Leffler #include <sys/mbuf.h> 4788768458SSam Leffler #include <sys/domain.h> 4846ee43b2SRobert Watson #include <sys/priv.h> 4988768458SSam Leffler #include <sys/protosw.h> 5088768458SSam Leffler #include <sys/socket.h> 5188768458SSam Leffler #include <sys/socketvar.h> 5288768458SSam Leffler #include <sys/errno.h> 53ef91a976SAndrey V. Elsukov #include <sys/hhook.h> 5488768458SSam Leffler #include <sys/time.h> 5588768458SSam Leffler #include <sys/kernel.h> 5688768458SSam Leffler #include <sys/syslog.h> 5788768458SSam Leffler #include <sys/sysctl.h> 5888768458SSam Leffler #include <sys/proc.h> 5988768458SSam Leffler 6088768458SSam Leffler #include <net/if.h> 61ef91a976SAndrey V. Elsukov #include <net/if_enc.h> 6276039bc8SGleb Smirnoff #include <net/if_var.h> 63eddfbb76SRobert Watson #include <net/vnet.h> 6488768458SSam Leffler 6588768458SSam Leffler #include <netinet/in.h> 6688768458SSam Leffler #include <netinet/in_systm.h> 6788768458SSam Leffler #include <netinet/ip.h> 6888768458SSam Leffler #include <netinet/ip_var.h> 6988768458SSam Leffler #include <netinet/in_var.h> 7088768458SSam Leffler #include <netinet/udp.h> 7188768458SSam Leffler #include <netinet/udp_var.h> 7288768458SSam Leffler #include <netinet/tcp.h> 7388768458SSam Leffler #include <netinet/udp.h> 7488768458SSam Leffler 7588768458SSam Leffler #include <netinet/ip6.h> 7688768458SSam Leffler #ifdef INET6 7788768458SSam Leffler #include <netinet6/ip6_var.h> 7888768458SSam Leffler #endif 7988768458SSam Leffler #include <netinet/in_pcb.h> 8088768458SSam Leffler #ifdef INET6 8188768458SSam Leffler #include <netinet/icmp6.h> 8288768458SSam Leffler #endif 8388768458SSam Leffler 842cb64cb2SGeorge V. Neville-Neil #include <sys/types.h> 8588768458SSam Leffler #include <netipsec/ipsec.h> 8688768458SSam Leffler #ifdef INET6 8788768458SSam Leffler #include <netipsec/ipsec6.h> 8888768458SSam Leffler #endif 8988768458SSam Leffler #include <netipsec/ah_var.h> 9088768458SSam Leffler #include <netipsec/esp_var.h> 9188768458SSam Leffler #include <netipsec/ipcomp.h> /*XXX*/ 9288768458SSam Leffler #include <netipsec/ipcomp_var.h> 93fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 9488768458SSam Leffler 9588768458SSam Leffler #include <netipsec/key.h> 9688768458SSam Leffler #include <netipsec/keydb.h> 9788768458SSam Leffler #include <netipsec/key_debug.h> 9888768458SSam Leffler 9988768458SSam Leffler #include <netipsec/xform.h> 10088768458SSam Leffler 10188768458SSam Leffler #include <machine/in_cksum.h> 10288768458SSam Leffler 1037aee3dd1SSam Leffler #include <opencrypto/cryptodev.h> 1047aee3dd1SSam Leffler 105de47c390SBjoern A. Zeeb /* NB: name changed so netstat doesn't use it. */ 106db8c0879SAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct ipsecstat, ipsec4stat); 107db8c0879SAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(ipsec4stat); 108db8c0879SAndrey V. Elsukov 109db8c0879SAndrey V. Elsukov #ifdef VIMAGE 110db8c0879SAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(ipsec4stat); 111db8c0879SAndrey V. Elsukov #endif /* VIMAGE */ 112db8c0879SAndrey V. Elsukov 113eddfbb76SRobert Watson /* DF bit on encap. 0: clear 1: set 2: copy */ 114eddfbb76SRobert Watson VNET_DEFINE(int, ip4_ipsec_dfbit) = 0; 115eddfbb76SRobert Watson VNET_DEFINE(int, ip4_esp_trans_deflev) = IPSEC_LEVEL_USE; 116eddfbb76SRobert Watson VNET_DEFINE(int, ip4_esp_net_deflev) = IPSEC_LEVEL_USE; 117eddfbb76SRobert Watson VNET_DEFINE(int, ip4_ah_trans_deflev) = IPSEC_LEVEL_USE; 118eddfbb76SRobert Watson VNET_DEFINE(int, ip4_ah_net_deflev) = IPSEC_LEVEL_USE; 119eddfbb76SRobert Watson /* ECN ignore(-1)/forbidden(0)/allowed(1) */ 120eddfbb76SRobert Watson VNET_DEFINE(int, ip4_ipsec_ecn) = 0; 121eddfbb76SRobert Watson 1225f901c92SAndrew Turner VNET_DEFINE_STATIC(int, ip4_filtertunnel) = 0; 123fcf59617SAndrey V. Elsukov #define V_ip4_filtertunnel VNET(ip4_filtertunnel) 1245f901c92SAndrew Turner VNET_DEFINE_STATIC(int, check_policy_history) = 0; 125fcf59617SAndrey V. Elsukov #define V_check_policy_history VNET(check_policy_history) 1265f901c92SAndrew Turner VNET_DEFINE_STATIC(struct secpolicy *, def_policy) = NULL; 12793201211SAndrey V. Elsukov #define V_def_policy VNET(def_policy) 128fcf59617SAndrey V. Elsukov static int 129fcf59617SAndrey V. Elsukov sysctl_def_policy(SYSCTL_HANDLER_ARGS) 130fcf59617SAndrey V. Elsukov { 131fcf59617SAndrey V. Elsukov int error, value; 132fcf59617SAndrey V. Elsukov 133fcf59617SAndrey V. Elsukov value = V_def_policy->policy; 134fcf59617SAndrey V. Elsukov error = sysctl_handle_int(oidp, &value, 0, req); 135fcf59617SAndrey V. Elsukov if (error == 0) { 136fcf59617SAndrey V. Elsukov if (value != IPSEC_POLICY_DISCARD && 137fcf59617SAndrey V. Elsukov value != IPSEC_POLICY_NONE) 138fcf59617SAndrey V. Elsukov return (EINVAL); 139fcf59617SAndrey V. Elsukov V_def_policy->policy = value; 140fcf59617SAndrey V. Elsukov } 141fcf59617SAndrey V. Elsukov return (error); 142fcf59617SAndrey V. Elsukov } 143fcf59617SAndrey V. Elsukov 14488768458SSam Leffler /* 14588768458SSam Leffler * Crypto support requirements: 14688768458SSam Leffler * 14788768458SSam Leffler * 1 require hardware support 14888768458SSam Leffler * -1 require software support 14988768458SSam Leffler * 0 take anything 15088768458SSam Leffler */ 151eddfbb76SRobert Watson VNET_DEFINE(int, crypto_support) = CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE; 15239bbca6fSFabien Thomas 15339bbca6fSFabien Thomas /* 15439bbca6fSFabien Thomas * Use asynchronous mode to parallelize crypto jobs: 15539bbca6fSFabien Thomas * 15639bbca6fSFabien Thomas * 0 - disabled 15739bbca6fSFabien Thomas * 1 - enabled 15839bbca6fSFabien Thomas */ 15939bbca6fSFabien Thomas VNET_DEFINE(int, async_crypto) = 0; 16039bbca6fSFabien Thomas 161fcf59617SAndrey V. Elsukov /* 162fcf59617SAndrey V. Elsukov * TCP/UDP checksum handling policy for transport mode NAT-T (RFC3948) 163fcf59617SAndrey V. Elsukov * 164fcf59617SAndrey V. Elsukov * 0 - auto: incrementally recompute, when checksum delta is known; 165fcf59617SAndrey V. Elsukov * if checksum delta isn't known, reset checksum to zero for UDP, 166fcf59617SAndrey V. Elsukov * and mark csum_flags as valid for TCP. 167fcf59617SAndrey V. Elsukov * 1 - fully recompute TCP/UDP checksum. 168fcf59617SAndrey V. Elsukov */ 169fcf59617SAndrey V. Elsukov VNET_DEFINE(int, natt_cksum_policy) = 0; 17088768458SSam Leffler 17113a6cf24SBjoern A. Zeeb FEATURE(ipsec, "Internet Protocol Security (IPsec)"); 17213a6cf24SBjoern A. Zeeb FEATURE(ipsec_natt, "UDP Encapsulation of IPsec ESP Packets ('NAT-T')"); 17313a6cf24SBjoern A. Zeeb 17488768458SSam Leffler SYSCTL_DECL(_net_inet_ipsec); 17588768458SSam Leffler 17688768458SSam Leffler /* net.inet.ipsec */ 177fcf59617SAndrey V. Elsukov SYSCTL_PROC(_net_inet_ipsec, IPSECCTL_DEF_POLICY, def_policy, 178*7029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 179*7029da5cSPawel Biernacki 0, 0, sysctl_def_policy, "I", 180be6b1304STom Rhodes "IPsec default policy."); 1816df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev, 1826df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_esp_trans_deflev), 0, 1838b615593SMarko Zec "Default ESP transport mode level"); 1846df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev, 1856df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_esp_net_deflev), 0, 1868b615593SMarko Zec "Default ESP tunnel mode level."); 1876df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev, 1886df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ah_trans_deflev), 0, 1898b615593SMarko Zec "AH transfer mode default level."); 1906df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev, 1916df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ah_net_deflev), 0, 1928b615593SMarko Zec "AH tunnel mode default level."); 1936df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_CLEARTOS, ah_cleartos, 1946df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ah_cleartos), 0, 195fcf59617SAndrey V. Elsukov "If set, clear type-of-service field when doing AH computation."); 1966df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DFBIT, dfbit, 1976df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ipsec_dfbit), 0, 1988b615593SMarko Zec "Do not fragment bit on encap."); 1996df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ECN, ecn, 2006df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ipsec_ecn), 0, 201be6b1304STom Rhodes "Explicit Congestion Notification handling."); 2026df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ipsec, OID_AUTO, crypto_support, 2036df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(crypto_support), 0, 204be6b1304STom Rhodes "Crypto driver selection."); 20539bbca6fSFabien Thomas SYSCTL_INT(_net_inet_ipsec, OID_AUTO, async_crypto, 20639bbca6fSFabien Thomas CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(async_crypto), 0, 20739bbca6fSFabien Thomas "Use asynchronous mode to parallelize crypto jobs."); 208fcf59617SAndrey V. Elsukov SYSCTL_INT(_net_inet_ipsec, OID_AUTO, check_policy_history, 209fcf59617SAndrey V. Elsukov CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(check_policy_history), 0, 210fcf59617SAndrey V. Elsukov "Use strict check of inbound packets to security policy compliance."); 211fcf59617SAndrey V. Elsukov SYSCTL_INT(_net_inet_ipsec, OID_AUTO, natt_cksum_policy, 212fcf59617SAndrey V. Elsukov CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(natt_cksum_policy), 0, 213fcf59617SAndrey V. Elsukov "Method to fix TCP/UDP checksum for transport mode IPsec after NAT."); 214fcf59617SAndrey V. Elsukov SYSCTL_INT(_net_inet_ipsec, OID_AUTO, filtertunnel, 215fcf59617SAndrey V. Elsukov CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_filtertunnel), 0, 216fcf59617SAndrey V. Elsukov "If set, filter packets from an IPsec tunnel."); 217db8c0879SAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_ipsec, OID_AUTO, ipsecstats, struct ipsecstat, 218db8c0879SAndrey V. Elsukov ipsec4stat, "IPsec IPv4 statistics."); 21988768458SSam Leffler 2200f702183SJohn Baldwin struct timeval ipsec_warn_interval = { .tv_sec = 1, .tv_usec = 0 }; 2210f702183SJohn Baldwin SYSCTL_TIMEVAL_SEC(_net_inet_ipsec, OID_AUTO, crypto_warn_interval, CTLFLAG_RW, 2220f702183SJohn Baldwin &ipsec_warn_interval, 2230f702183SJohn Baldwin "Delay in seconds between warnings of deprecated IPsec crypto algorithms."); 2240f702183SJohn Baldwin 2256131838bSPawel Jakub Dawidek #ifdef REGRESSION 226dfa9422bSPawel Jakub Dawidek /* 227dfa9422bSPawel Jakub Dawidek * When set to 1, IPsec will send packets with the same sequence number. 228dfa9422bSPawel Jakub Dawidek * This allows to verify if the other side has proper replay attacks detection. 229dfa9422bSPawel Jakub Dawidek */ 230eddfbb76SRobert Watson VNET_DEFINE(int, ipsec_replay) = 0; 2316df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_replay, 2326df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_replay), 0, 233eddfbb76SRobert Watson "Emulate replay attack"); 234dfa9422bSPawel Jakub Dawidek /* 235dfa9422bSPawel Jakub Dawidek * When set 1, IPsec will send packets with corrupted HMAC. 236dfa9422bSPawel Jakub Dawidek * This allows to verify if the other side properly detects modified packets. 237dfa9422bSPawel Jakub Dawidek */ 238eddfbb76SRobert Watson VNET_DEFINE(int, ipsec_integrity) = 0; 2396df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_integrity, 2406df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_integrity), 0, 241eddfbb76SRobert Watson "Emulate man-in-the-middle attack"); 2426131838bSPawel Jakub Dawidek #endif 243dfa9422bSPawel Jakub Dawidek 24488768458SSam Leffler #ifdef INET6 245db8c0879SAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct ipsecstat, ipsec6stat); 246db8c0879SAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(ipsec6stat); 247db8c0879SAndrey V. Elsukov 248db8c0879SAndrey V. Elsukov #ifdef VIMAGE 249db8c0879SAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(ipsec6stat); 250db8c0879SAndrey V. Elsukov #endif /* VIMAGE */ 251db8c0879SAndrey V. Elsukov 252eddfbb76SRobert Watson VNET_DEFINE(int, ip6_esp_trans_deflev) = IPSEC_LEVEL_USE; 253eddfbb76SRobert Watson VNET_DEFINE(int, ip6_esp_net_deflev) = IPSEC_LEVEL_USE; 254eddfbb76SRobert Watson VNET_DEFINE(int, ip6_ah_trans_deflev) = IPSEC_LEVEL_USE; 255eddfbb76SRobert Watson VNET_DEFINE(int, ip6_ah_net_deflev) = IPSEC_LEVEL_USE; 256eddfbb76SRobert Watson VNET_DEFINE(int, ip6_ipsec_ecn) = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */ 25788768458SSam Leffler 2585f901c92SAndrew Turner VNET_DEFINE_STATIC(int, ip6_filtertunnel) = 0; 259fcf59617SAndrey V. Elsukov #define V_ip6_filtertunnel VNET(ip6_filtertunnel) 260fcf59617SAndrey V. Elsukov 26188768458SSam Leffler SYSCTL_DECL(_net_inet6_ipsec6); 26288768458SSam Leffler 26388768458SSam Leffler /* net.inet6.ipsec6 */ 264fcf59617SAndrey V. Elsukov SYSCTL_PROC(_net_inet6_ipsec6, IPSECCTL_DEF_POLICY, def_policy, 265*7029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 266*7029da5cSPawel Biernacki 0, 0, sysctl_def_policy, "I", 2678b615593SMarko Zec "IPsec default policy."); 2686df8a710SGleb Smirnoff SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev, 2696df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_esp_trans_deflev), 0, 2708b615593SMarko Zec "Default ESP transport mode level."); 2716df8a710SGleb Smirnoff SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev, 2726df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_esp_net_deflev), 0, 2738b615593SMarko Zec "Default ESP tunnel mode level."); 2746df8a710SGleb Smirnoff SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev, 2756df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ah_trans_deflev), 0, 2768b615593SMarko Zec "AH transfer mode default level."); 2776df8a710SGleb Smirnoff SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev, 2786df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ah_net_deflev), 0, 2798b615593SMarko Zec "AH tunnel mode default level."); 2806df8a710SGleb Smirnoff SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ECN, ecn, 2816df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ipsec_ecn), 0, 2823377c961STom Rhodes "Explicit Congestion Notification handling."); 283fcf59617SAndrey V. Elsukov SYSCTL_INT(_net_inet6_ipsec6, OID_AUTO, filtertunnel, 284fcf59617SAndrey V. Elsukov CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_filtertunnel), 0, 285fcf59617SAndrey V. Elsukov "If set, filter packets from an IPsec tunnel."); 286db8c0879SAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet6_ipsec6, IPSECCTL_STATS, ipsecstats, 287db8c0879SAndrey V. Elsukov struct ipsecstat, ipsec6stat, "IPsec IPv6 statistics."); 28888768458SSam Leffler #endif /* INET6 */ 28988768458SSam Leffler 290fcf59617SAndrey V. Elsukov static int ipsec_in_reject(struct secpolicy *, struct inpcb *, 291fcf59617SAndrey V. Elsukov const struct mbuf *); 292fcf59617SAndrey V. Elsukov 293fcf59617SAndrey V. Elsukov #ifdef INET 294fcf59617SAndrey V. Elsukov static void ipsec4_get_ulp(const struct mbuf *, struct secpolicyindex *, int); 295fcf59617SAndrey V. Elsukov static void ipsec4_setspidx_ipaddr(const struct mbuf *, 296fcf59617SAndrey V. Elsukov struct secpolicyindex *); 297fcf59617SAndrey V. Elsukov #endif 29888768458SSam Leffler #ifdef INET6 299efb10c3cSAndrey V. Elsukov static void ipsec6_get_ulp(const struct mbuf *m, struct secpolicyindex *, int); 300fcf59617SAndrey V. Elsukov static void ipsec6_setspidx_ipaddr(const struct mbuf *, 301fcf59617SAndrey V. Elsukov struct secpolicyindex *); 30288768458SSam Leffler #endif 3036464079fSSam Leffler 30488768458SSam Leffler /* 30588768458SSam Leffler * Return a held reference to the default SP. 30688768458SSam Leffler */ 30788768458SSam Leffler static struct secpolicy * 308fcf59617SAndrey V. Elsukov key_allocsp_default(void) 309fcf59617SAndrey V. Elsukov { 310fcf59617SAndrey V. Elsukov 311fcf59617SAndrey V. Elsukov key_addref(V_def_policy); 312fcf59617SAndrey V. Elsukov return (V_def_policy); 313fcf59617SAndrey V. Elsukov } 314fcf59617SAndrey V. Elsukov 315fcf59617SAndrey V. Elsukov static void 316fcf59617SAndrey V. Elsukov ipsec_invalidate_cache(struct inpcb *inp, u_int dir) 31788768458SSam Leffler { 31888768458SSam Leffler struct secpolicy *sp; 31988768458SSam Leffler 320fcf59617SAndrey V. Elsukov INP_WLOCK_ASSERT(inp); 321fcf59617SAndrey V. Elsukov if (dir == IPSEC_DIR_OUTBOUND) { 322fcf59617SAndrey V. Elsukov if (inp->inp_sp->flags & INP_INBOUND_POLICY) 323fcf59617SAndrey V. Elsukov return; 324fcf59617SAndrey V. Elsukov sp = inp->inp_sp->sp_in; 325fcf59617SAndrey V. Elsukov inp->inp_sp->sp_in = NULL; 326fcf59617SAndrey V. Elsukov } else { 327fcf59617SAndrey V. Elsukov if (inp->inp_sp->flags & INP_OUTBOUND_POLICY) 328fcf59617SAndrey V. Elsukov return; 329fcf59617SAndrey V. Elsukov sp = inp->inp_sp->sp_out; 330fcf59617SAndrey V. Elsukov inp->inp_sp->sp_out = NULL; 33188768458SSam Leffler } 332fcf59617SAndrey V. Elsukov if (sp != NULL) 333fcf59617SAndrey V. Elsukov key_freesp(&sp); /* release extra reference */ 33488768458SSam Leffler } 33588768458SSam Leffler 336fcf59617SAndrey V. Elsukov static void 337fcf59617SAndrey V. Elsukov ipsec_cachepolicy(struct inpcb *inp, struct secpolicy *sp, u_int dir) 338fcf59617SAndrey V. Elsukov { 339fcf59617SAndrey V. Elsukov uint32_t genid; 340fcf59617SAndrey V. Elsukov int downgrade; 341fcf59617SAndrey V. Elsukov 342fcf59617SAndrey V. Elsukov INP_LOCK_ASSERT(inp); 343fcf59617SAndrey V. Elsukov 344fcf59617SAndrey V. Elsukov if (dir == IPSEC_DIR_OUTBOUND) { 345fcf59617SAndrey V. Elsukov /* Do we have configured PCB policy? */ 346fcf59617SAndrey V. Elsukov if (inp->inp_sp->flags & INP_OUTBOUND_POLICY) 347fcf59617SAndrey V. Elsukov return; 348fcf59617SAndrey V. Elsukov /* Another thread has already set cached policy */ 349fcf59617SAndrey V. Elsukov if (inp->inp_sp->sp_out != NULL) 350fcf59617SAndrey V. Elsukov return; 35188768458SSam Leffler /* 352fcf59617SAndrey V. Elsukov * Do not cache OUTBOUND policy if PCB isn't connected, 353fcf59617SAndrey V. Elsukov * i.e. foreign address is INADDR_ANY/UNSPECIFIED. 35488768458SSam Leffler */ 355fcf59617SAndrey V. Elsukov #ifdef INET 356fcf59617SAndrey V. Elsukov if ((inp->inp_vflag & INP_IPV4) != 0 && 357fcf59617SAndrey V. Elsukov inp->inp_faddr.s_addr == INADDR_ANY) 358fcf59617SAndrey V. Elsukov return; 359fcf59617SAndrey V. Elsukov #endif 360fcf59617SAndrey V. Elsukov #ifdef INET6 361fcf59617SAndrey V. Elsukov if ((inp->inp_vflag & INP_IPV6) != 0 && 362fcf59617SAndrey V. Elsukov IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 363fcf59617SAndrey V. Elsukov return; 364fcf59617SAndrey V. Elsukov #endif 365fcf59617SAndrey V. Elsukov } else { 366fcf59617SAndrey V. Elsukov /* Do we have configured PCB policy? */ 367fcf59617SAndrey V. Elsukov if (inp->inp_sp->flags & INP_INBOUND_POLICY) 368fcf59617SAndrey V. Elsukov return; 369fcf59617SAndrey V. Elsukov /* Another thread has already set cached policy */ 370fcf59617SAndrey V. Elsukov if (inp->inp_sp->sp_in != NULL) 371fcf59617SAndrey V. Elsukov return; 37288768458SSam Leffler /* 373fcf59617SAndrey V. Elsukov * Do not cache INBOUND policy for listen socket, 374fcf59617SAndrey V. Elsukov * that is bound to INADDR_ANY/UNSPECIFIED address. 37588768458SSam Leffler */ 376fcf59617SAndrey V. Elsukov #ifdef INET 377fcf59617SAndrey V. Elsukov if ((inp->inp_vflag & INP_IPV4) != 0 && 378fcf59617SAndrey V. Elsukov inp->inp_faddr.s_addr == INADDR_ANY) 379fcf59617SAndrey V. Elsukov return; 380fcf59617SAndrey V. Elsukov #endif 381fcf59617SAndrey V. Elsukov #ifdef INET6 382fcf59617SAndrey V. Elsukov if ((inp->inp_vflag & INP_IPV6) != 0 && 383fcf59617SAndrey V. Elsukov IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 384fcf59617SAndrey V. Elsukov return; 385fcf59617SAndrey V. Elsukov #endif 386c1fc5e96SErmal Luçi } 387fcf59617SAndrey V. Elsukov downgrade = 0; 388fcf59617SAndrey V. Elsukov if (!INP_WLOCKED(inp)) { 389fcf59617SAndrey V. Elsukov if ((downgrade = INP_TRY_UPGRADE(inp)) == 0) 390fcf59617SAndrey V. Elsukov return; 39188768458SSam Leffler } 392fcf59617SAndrey V. Elsukov if (dir == IPSEC_DIR_OUTBOUND) 393fcf59617SAndrey V. Elsukov inp->inp_sp->sp_out = sp; 39488768458SSam Leffler else 395fcf59617SAndrey V. Elsukov inp->inp_sp->sp_in = sp; 396fcf59617SAndrey V. Elsukov /* 397fcf59617SAndrey V. Elsukov * SP is already referenced by the lookup code. 398fcf59617SAndrey V. Elsukov * We take extra reference here to avoid race in the 399fcf59617SAndrey V. Elsukov * ipsec_getpcbpolicy() function - SP will not be freed in the 400fcf59617SAndrey V. Elsukov * time between we take SP pointer from the cache and key_addref() 401fcf59617SAndrey V. Elsukov * call. 402fcf59617SAndrey V. Elsukov */ 403fcf59617SAndrey V. Elsukov key_addref(sp); 404fcf59617SAndrey V. Elsukov genid = key_getspgen(); 405fcf59617SAndrey V. Elsukov if (genid != inp->inp_sp->genid) { 406fcf59617SAndrey V. Elsukov ipsec_invalidate_cache(inp, dir); 407fcf59617SAndrey V. Elsukov inp->inp_sp->genid = genid; 40888768458SSam Leffler } 409fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_STAMP, 410fcf59617SAndrey V. Elsukov printf("%s: PCB(%p): cached %s SP(%p)\n", 411fcf59617SAndrey V. Elsukov __func__, inp, dir == IPSEC_DIR_OUTBOUND ? "OUTBOUND": 412fcf59617SAndrey V. Elsukov "INBOUND", sp)); 413fcf59617SAndrey V. Elsukov if (downgrade != 0) 414fcf59617SAndrey V. Elsukov INP_DOWNGRADE(inp); 415fcf59617SAndrey V. Elsukov } 416fcf59617SAndrey V. Elsukov 417fcf59617SAndrey V. Elsukov static struct secpolicy * 418fcf59617SAndrey V. Elsukov ipsec_checkpolicy(struct secpolicy *sp, struct inpcb *inp, int *error) 419fcf59617SAndrey V. Elsukov { 420fcf59617SAndrey V. Elsukov 421fcf59617SAndrey V. Elsukov /* Save found OUTBOUND policy into PCB SP cache. */ 422fcf59617SAndrey V. Elsukov if (inp != NULL && inp->inp_sp != NULL && inp->inp_sp->sp_out == NULL) 423fcf59617SAndrey V. Elsukov ipsec_cachepolicy(inp, sp, IPSEC_DIR_OUTBOUND); 424fcf59617SAndrey V. Elsukov 42588768458SSam Leffler switch (sp->policy) { 42688768458SSam Leffler default: 4279ffa9677SSam Leffler printf("%s: invalid policy %u\n", __func__, sp->policy); 428de47c390SBjoern A. Zeeb /* FALLTHROUGH */ 42988768458SSam Leffler case IPSEC_POLICY_DISCARD: 430de47c390SBjoern A. Zeeb *error = -EINVAL; /* Packet is discarded by caller. */ 431fcf59617SAndrey V. Elsukov /* FALLTHROUGH */ 43288768458SSam Leffler case IPSEC_POLICY_BYPASS: 43388768458SSam Leffler case IPSEC_POLICY_NONE: 434fcf59617SAndrey V. Elsukov key_freesp(&sp); 435de47c390SBjoern A. Zeeb sp = NULL; /* NB: force NULL result. */ 43688768458SSam Leffler break; 43788768458SSam Leffler case IPSEC_POLICY_IPSEC: 438fcf59617SAndrey V. Elsukov /* XXXAE: handle LARVAL SP */ 43988768458SSam Leffler break; 44088768458SSam Leffler } 441fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DUMP, 442fcf59617SAndrey V. Elsukov printf("%s: get SP(%p), error %d\n", __func__, sp, *error)); 443de47c390SBjoern A. Zeeb return (sp); 44488768458SSam Leffler } 44588768458SSam Leffler 446fcf59617SAndrey V. Elsukov static struct secpolicy * 447fcf59617SAndrey V. Elsukov ipsec_getpcbpolicy(struct inpcb *inp, u_int dir) 44888768458SSam Leffler { 449fcf59617SAndrey V. Elsukov struct secpolicy *sp; 450fcf59617SAndrey V. Elsukov int flags, downgrade; 45188768458SSam Leffler 452fcf59617SAndrey V. Elsukov if (inp == NULL || inp->inp_sp == NULL) 453fcf59617SAndrey V. Elsukov return (NULL); 45488768458SSam Leffler 455fcf59617SAndrey V. Elsukov INP_LOCK_ASSERT(inp); 456fcf59617SAndrey V. Elsukov 457fcf59617SAndrey V. Elsukov flags = inp->inp_sp->flags; 458fcf59617SAndrey V. Elsukov if (dir == IPSEC_DIR_OUTBOUND) { 459fcf59617SAndrey V. Elsukov sp = inp->inp_sp->sp_out; 460fcf59617SAndrey V. Elsukov flags &= INP_OUTBOUND_POLICY; 46188768458SSam Leffler } else { 462fcf59617SAndrey V. Elsukov sp = inp->inp_sp->sp_in; 463fcf59617SAndrey V. Elsukov flags &= INP_INBOUND_POLICY; 46488768458SSam Leffler } 46588768458SSam Leffler /* 466fcf59617SAndrey V. Elsukov * Check flags. If we have PCB SP, just return it. 467fcf59617SAndrey V. Elsukov * Otherwise we need to check that cached SP entry isn't stale. 46888768458SSam Leffler */ 469fcf59617SAndrey V. Elsukov if (flags == 0) { 470fcf59617SAndrey V. Elsukov if (sp == NULL) 471fcf59617SAndrey V. Elsukov return (NULL); 472fcf59617SAndrey V. Elsukov if (inp->inp_sp->genid != key_getspgen()) { 473fcf59617SAndrey V. Elsukov /* Invalidate the cache. */ 474fcf59617SAndrey V. Elsukov downgrade = 0; 475fcf59617SAndrey V. Elsukov if (!INP_WLOCKED(inp)) { 476fcf59617SAndrey V. Elsukov if ((downgrade = INP_TRY_UPGRADE(inp)) == 0) 477fcf59617SAndrey V. Elsukov return (NULL); 478fcf59617SAndrey V. Elsukov } 479fcf59617SAndrey V. Elsukov ipsec_invalidate_cache(inp, IPSEC_DIR_OUTBOUND); 480fcf59617SAndrey V. Elsukov ipsec_invalidate_cache(inp, IPSEC_DIR_INBOUND); 481fcf59617SAndrey V. Elsukov if (downgrade != 0) 482fcf59617SAndrey V. Elsukov INP_DOWNGRADE(inp); 483fcf59617SAndrey V. Elsukov return (NULL); 484fcf59617SAndrey V. Elsukov } 485fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_STAMP, 486fcf59617SAndrey V. Elsukov printf("%s: PCB(%p): cache hit SP(%p)\n", 487fcf59617SAndrey V. Elsukov __func__, inp, sp)); 488fcf59617SAndrey V. Elsukov /* Return referenced cached policy */ 489fcf59617SAndrey V. Elsukov } 490fcf59617SAndrey V. Elsukov key_addref(sp); 491fcf59617SAndrey V. Elsukov return (sp); 49288768458SSam Leffler } 49388768458SSam Leffler 494fcf59617SAndrey V. Elsukov #ifdef INET 49588768458SSam Leffler static void 496efb10c3cSAndrey V. Elsukov ipsec4_get_ulp(const struct mbuf *m, struct secpolicyindex *spidx, 497efb10c3cSAndrey V. Elsukov int needport) 49888768458SSam Leffler { 499fcf59617SAndrey V. Elsukov uint8_t nxt; 50088768458SSam Leffler int off; 50188768458SSam Leffler 502de47c390SBjoern A. Zeeb /* Sanity check. */ 503fcf59617SAndrey V. Elsukov IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip), 504fcf59617SAndrey V. Elsukov ("packet too short")); 50588768458SSam Leffler 50687a25418SErmal Luçi if (m->m_len >= sizeof (struct ip)) { 507efb10c3cSAndrey V. Elsukov const struct ip *ip = mtod(m, const struct ip *); 5088f134647SGleb Smirnoff if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) 50988768458SSam Leffler goto done; 51088768458SSam Leffler off = ip->ip_hl << 2; 51188768458SSam Leffler nxt = ip->ip_p; 51288768458SSam Leffler } else { 51388768458SSam Leffler struct ip ih; 51488768458SSam Leffler 51588768458SSam Leffler m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih); 5168f134647SGleb Smirnoff if (ih.ip_off & htons(IP_MF | IP_OFFMASK)) 51788768458SSam Leffler goto done; 51888768458SSam Leffler off = ih.ip_hl << 2; 51988768458SSam Leffler nxt = ih.ip_p; 52088768458SSam Leffler } 52188768458SSam Leffler 52288768458SSam Leffler while (off < m->m_pkthdr.len) { 52388768458SSam Leffler struct ip6_ext ip6e; 52488768458SSam Leffler struct tcphdr th; 52588768458SSam Leffler struct udphdr uh; 52688768458SSam Leffler 52788768458SSam Leffler switch (nxt) { 52888768458SSam Leffler case IPPROTO_TCP: 52988768458SSam Leffler spidx->ul_proto = nxt; 53088768458SSam Leffler if (!needport) 53188768458SSam Leffler goto done_proto; 53288768458SSam Leffler if (off + sizeof(struct tcphdr) > m->m_pkthdr.len) 53388768458SSam Leffler goto done; 53488768458SSam Leffler m_copydata(m, off, sizeof (th), (caddr_t) &th); 53588768458SSam Leffler spidx->src.sin.sin_port = th.th_sport; 53688768458SSam Leffler spidx->dst.sin.sin_port = th.th_dport; 53788768458SSam Leffler return; 53888768458SSam Leffler case IPPROTO_UDP: 53988768458SSam Leffler spidx->ul_proto = nxt; 54088768458SSam Leffler if (!needport) 54188768458SSam Leffler goto done_proto; 54288768458SSam Leffler if (off + sizeof(struct udphdr) > m->m_pkthdr.len) 54388768458SSam Leffler goto done; 54488768458SSam Leffler m_copydata(m, off, sizeof (uh), (caddr_t) &uh); 54588768458SSam Leffler spidx->src.sin.sin_port = uh.uh_sport; 54688768458SSam Leffler spidx->dst.sin.sin_port = uh.uh_dport; 54788768458SSam Leffler return; 54888768458SSam Leffler case IPPROTO_AH: 549afa3570dSSam Leffler if (off + sizeof(ip6e) > m->m_pkthdr.len) 55088768458SSam Leffler goto done; 551de47c390SBjoern A. Zeeb /* XXX Sigh, this works but is totally bogus. */ 55288768458SSam Leffler m_copydata(m, off, sizeof(ip6e), (caddr_t) &ip6e); 55388768458SSam Leffler off += (ip6e.ip6e_len + 2) << 2; 55488768458SSam Leffler nxt = ip6e.ip6e_nxt; 55588768458SSam Leffler break; 55688768458SSam Leffler case IPPROTO_ICMP: 55788768458SSam Leffler default: 558de47c390SBjoern A. Zeeb /* XXX Intermediate headers??? */ 55988768458SSam Leffler spidx->ul_proto = nxt; 56088768458SSam Leffler goto done_proto; 56188768458SSam Leffler } 56288768458SSam Leffler } 56388768458SSam Leffler done: 56488768458SSam Leffler spidx->ul_proto = IPSEC_ULPROTO_ANY; 56588768458SSam Leffler done_proto: 56688768458SSam Leffler spidx->src.sin.sin_port = IPSEC_PORT_ANY; 56788768458SSam Leffler spidx->dst.sin.sin_port = IPSEC_PORT_ANY; 568fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DUMP, 569fcf59617SAndrey V. Elsukov printf("%s: ", __func__); kdebug_secpolicyindex(spidx, NULL)); 57088768458SSam Leffler } 57188768458SSam Leffler 572fcf59617SAndrey V. Elsukov static void 573efb10c3cSAndrey V. Elsukov ipsec4_setspidx_ipaddr(const struct mbuf *m, struct secpolicyindex *spidx) 57488768458SSam Leffler { 57588768458SSam Leffler 576fcf59617SAndrey V. Elsukov ipsec4_setsockaddrs(m, &spidx->src, &spidx->dst); 57788768458SSam Leffler spidx->prefs = sizeof(struct in_addr) << 3; 57888768458SSam Leffler spidx->prefd = sizeof(struct in_addr) << 3; 57988768458SSam Leffler } 58088768458SSam Leffler 581fcf59617SAndrey V. Elsukov static struct secpolicy * 58222bbefb2SAndrey V. Elsukov ipsec4_getpolicy(const struct mbuf *m, struct inpcb *inp, u_int dir, 58322bbefb2SAndrey V. Elsukov int needport) 584fcf59617SAndrey V. Elsukov { 585fcf59617SAndrey V. Elsukov struct secpolicyindex spidx; 586fcf59617SAndrey V. Elsukov struct secpolicy *sp; 587fcf59617SAndrey V. Elsukov 588fcf59617SAndrey V. Elsukov sp = ipsec_getpcbpolicy(inp, dir); 589fcf59617SAndrey V. Elsukov if (sp == NULL && key_havesp(dir)) { 590fcf59617SAndrey V. Elsukov /* Make an index to look for a policy. */ 591fcf59617SAndrey V. Elsukov ipsec4_setspidx_ipaddr(m, &spidx); 59222bbefb2SAndrey V. Elsukov ipsec4_get_ulp(m, &spidx, needport); 593fcf59617SAndrey V. Elsukov spidx.dir = dir; 594fcf59617SAndrey V. Elsukov sp = key_allocsp(&spidx, dir); 595fcf59617SAndrey V. Elsukov } 596fcf59617SAndrey V. Elsukov if (sp == NULL) /* No SP found, use system default. */ 597fcf59617SAndrey V. Elsukov sp = key_allocsp_default(); 598fcf59617SAndrey V. Elsukov return (sp); 599fcf59617SAndrey V. Elsukov } 600fcf59617SAndrey V. Elsukov 601fcf59617SAndrey V. Elsukov /* 602fcf59617SAndrey V. Elsukov * Check security policy for *OUTBOUND* IPv4 packet. 603fcf59617SAndrey V. Elsukov */ 604fcf59617SAndrey V. Elsukov struct secpolicy * 60522bbefb2SAndrey V. Elsukov ipsec4_checkpolicy(const struct mbuf *m, struct inpcb *inp, int *error, 60622bbefb2SAndrey V. Elsukov int needport) 607fcf59617SAndrey V. Elsukov { 608fcf59617SAndrey V. Elsukov struct secpolicy *sp; 609fcf59617SAndrey V. Elsukov 610fcf59617SAndrey V. Elsukov *error = 0; 61122bbefb2SAndrey V. Elsukov sp = ipsec4_getpolicy(m, inp, IPSEC_DIR_OUTBOUND, needport); 612fcf59617SAndrey V. Elsukov if (sp != NULL) 613fcf59617SAndrey V. Elsukov sp = ipsec_checkpolicy(sp, inp, error); 614fcf59617SAndrey V. Elsukov if (sp == NULL) { 615fcf59617SAndrey V. Elsukov switch (*error) { 616fcf59617SAndrey V. Elsukov case 0: /* No IPsec required: BYPASS or NONE */ 617fcf59617SAndrey V. Elsukov break; 618fcf59617SAndrey V. Elsukov case -EINVAL: 619fcf59617SAndrey V. Elsukov IPSECSTAT_INC(ips_out_polvio); 620fcf59617SAndrey V. Elsukov break; 621fcf59617SAndrey V. Elsukov default: 622fcf59617SAndrey V. Elsukov IPSECSTAT_INC(ips_out_inval); 623fcf59617SAndrey V. Elsukov } 624fcf59617SAndrey V. Elsukov } 625fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_STAMP, 626fcf59617SAndrey V. Elsukov printf("%s: using SP(%p), error %d\n", __func__, sp, *error)); 627fcf59617SAndrey V. Elsukov if (sp != NULL) 628fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp)); 629fcf59617SAndrey V. Elsukov return (sp); 630fcf59617SAndrey V. Elsukov } 631fcf59617SAndrey V. Elsukov 632fcf59617SAndrey V. Elsukov /* 633fcf59617SAndrey V. Elsukov * Check IPv4 packet against *INBOUND* security policy. 634fcf59617SAndrey V. Elsukov * This function is called from tcp_input(), udp_input(), 635fcf59617SAndrey V. Elsukov * rip_input() and sctp_input(). 636fcf59617SAndrey V. Elsukov */ 637fcf59617SAndrey V. Elsukov int 638fcf59617SAndrey V. Elsukov ipsec4_in_reject(const struct mbuf *m, struct inpcb *inp) 639fcf59617SAndrey V. Elsukov { 640fcf59617SAndrey V. Elsukov struct secpolicy *sp; 641fcf59617SAndrey V. Elsukov int result; 642fcf59617SAndrey V. Elsukov 64322bbefb2SAndrey V. Elsukov sp = ipsec4_getpolicy(m, inp, IPSEC_DIR_INBOUND, 0); 644fcf59617SAndrey V. Elsukov result = ipsec_in_reject(sp, inp, m); 645fcf59617SAndrey V. Elsukov key_freesp(&sp); 646fcf59617SAndrey V. Elsukov if (result != 0) 647fcf59617SAndrey V. Elsukov IPSECSTAT_INC(ips_in_polvio); 648fcf59617SAndrey V. Elsukov return (result); 649fcf59617SAndrey V. Elsukov } 650fcf59617SAndrey V. Elsukov 651fcf59617SAndrey V. Elsukov /* 652fcf59617SAndrey V. Elsukov * IPSEC_CAP() method implementation for IPv4. 653fcf59617SAndrey V. Elsukov */ 654fcf59617SAndrey V. Elsukov int 655fcf59617SAndrey V. Elsukov ipsec4_capability(struct mbuf *m, u_int cap) 656fcf59617SAndrey V. Elsukov { 657fcf59617SAndrey V. Elsukov 658fcf59617SAndrey V. Elsukov switch (cap) { 659fcf59617SAndrey V. Elsukov case IPSEC_CAP_BYPASS_FILTER: 660fcf59617SAndrey V. Elsukov /* 661fcf59617SAndrey V. Elsukov * Bypass packet filtering for packets previously handled 662fcf59617SAndrey V. Elsukov * by IPsec. 663fcf59617SAndrey V. Elsukov */ 664fcf59617SAndrey V. Elsukov if (!V_ip4_filtertunnel && 665fcf59617SAndrey V. Elsukov m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL) 666fcf59617SAndrey V. Elsukov return (1); 667fcf59617SAndrey V. Elsukov return (0); 668fcf59617SAndrey V. Elsukov case IPSEC_CAP_OPERABLE: 669fcf59617SAndrey V. Elsukov /* Do we have active security policies? */ 670fcf59617SAndrey V. Elsukov if (key_havesp(IPSEC_DIR_INBOUND) != 0 || 671fcf59617SAndrey V. Elsukov key_havesp(IPSEC_DIR_OUTBOUND) != 0) 672fcf59617SAndrey V. Elsukov return (1); 673fcf59617SAndrey V. Elsukov return (0); 674fcf59617SAndrey V. Elsukov }; 675fcf59617SAndrey V. Elsukov return (EOPNOTSUPP); 676fcf59617SAndrey V. Elsukov } 677fcf59617SAndrey V. Elsukov 678fcf59617SAndrey V. Elsukov #endif /* INET */ 679fcf59617SAndrey V. Elsukov 68088768458SSam Leffler #ifdef INET6 68188768458SSam Leffler static void 682efb10c3cSAndrey V. Elsukov ipsec6_get_ulp(const struct mbuf *m, struct secpolicyindex *spidx, 683efb10c3cSAndrey V. Elsukov int needport) 68488768458SSam Leffler { 68588768458SSam Leffler struct tcphdr th; 68688768458SSam Leffler struct udphdr uh; 6870e3c2be4SBjoern A. Zeeb struct icmp6_hdr ih; 688fcf59617SAndrey V. Elsukov int off, nxt; 68988768458SSam Leffler 690fcf59617SAndrey V. Elsukov IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip6_hdr), 691fcf59617SAndrey V. Elsukov ("packet too short")); 69288768458SSam Leffler 693de47c390SBjoern A. Zeeb /* Set default. */ 69488768458SSam Leffler spidx->ul_proto = IPSEC_ULPROTO_ANY; 695fcf59617SAndrey V. Elsukov spidx->src.sin6.sin6_port = IPSEC_PORT_ANY; 696fcf59617SAndrey V. Elsukov spidx->dst.sin6.sin6_port = IPSEC_PORT_ANY; 69788768458SSam Leffler 69888768458SSam Leffler nxt = -1; 69988768458SSam Leffler off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 70088768458SSam Leffler if (off < 0 || m->m_pkthdr.len < off) 70188768458SSam Leffler return; 70288768458SSam Leffler 70388768458SSam Leffler switch (nxt) { 70488768458SSam Leffler case IPPROTO_TCP: 70588768458SSam Leffler spidx->ul_proto = nxt; 70688768458SSam Leffler if (!needport) 70788768458SSam Leffler break; 70888768458SSam Leffler if (off + sizeof(struct tcphdr) > m->m_pkthdr.len) 70988768458SSam Leffler break; 71088768458SSam Leffler m_copydata(m, off, sizeof(th), (caddr_t)&th); 711fcf59617SAndrey V. Elsukov spidx->src.sin6.sin6_port = th.th_sport; 712fcf59617SAndrey V. Elsukov spidx->dst.sin6.sin6_port = th.th_dport; 71388768458SSam Leffler break; 71488768458SSam Leffler case IPPROTO_UDP: 71588768458SSam Leffler spidx->ul_proto = nxt; 71688768458SSam Leffler if (!needport) 71788768458SSam Leffler break; 71888768458SSam Leffler if (off + sizeof(struct udphdr) > m->m_pkthdr.len) 71988768458SSam Leffler break; 72088768458SSam Leffler m_copydata(m, off, sizeof(uh), (caddr_t)&uh); 721fcf59617SAndrey V. Elsukov spidx->src.sin6.sin6_port = uh.uh_sport; 722fcf59617SAndrey V. Elsukov spidx->dst.sin6.sin6_port = uh.uh_dport; 72388768458SSam Leffler break; 72488768458SSam Leffler case IPPROTO_ICMPV6: 7250e3c2be4SBjoern A. Zeeb spidx->ul_proto = nxt; 7260e3c2be4SBjoern A. Zeeb if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len) 7270e3c2be4SBjoern A. Zeeb break; 7280e3c2be4SBjoern A. Zeeb m_copydata(m, off, sizeof(ih), (caddr_t)&ih); 729fcf59617SAndrey V. Elsukov spidx->src.sin6.sin6_port = htons((uint16_t)ih.icmp6_type); 730fcf59617SAndrey V. Elsukov spidx->dst.sin6.sin6_port = htons((uint16_t)ih.icmp6_code); 7310e3c2be4SBjoern A. Zeeb break; 73288768458SSam Leffler default: 733de47c390SBjoern A. Zeeb /* XXX Intermediate headers??? */ 73488768458SSam Leffler spidx->ul_proto = nxt; 73588768458SSam Leffler break; 73688768458SSam Leffler } 737fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DUMP, 738fcf59617SAndrey V. Elsukov printf("%s: ", __func__); kdebug_secpolicyindex(spidx, NULL)); 73988768458SSam Leffler } 74088768458SSam Leffler 741fcf59617SAndrey V. Elsukov static void 742efb10c3cSAndrey V. Elsukov ipsec6_setspidx_ipaddr(const struct mbuf *m, struct secpolicyindex *spidx) 74388768458SSam Leffler { 74488768458SSam Leffler 745fcf59617SAndrey V. Elsukov ipsec6_setsockaddrs(m, &spidx->src, &spidx->dst); 74688768458SSam Leffler spidx->prefs = sizeof(struct in6_addr) << 3; 74788768458SSam Leffler spidx->prefd = sizeof(struct in6_addr) << 3; 74888768458SSam Leffler } 749fcf59617SAndrey V. Elsukov 750fcf59617SAndrey V. Elsukov static struct secpolicy * 75122bbefb2SAndrey V. Elsukov ipsec6_getpolicy(const struct mbuf *m, struct inpcb *inp, u_int dir, 75222bbefb2SAndrey V. Elsukov int needport) 753fcf59617SAndrey V. Elsukov { 754fcf59617SAndrey V. Elsukov struct secpolicyindex spidx; 755fcf59617SAndrey V. Elsukov struct secpolicy *sp; 756fcf59617SAndrey V. Elsukov 757fcf59617SAndrey V. Elsukov sp = ipsec_getpcbpolicy(inp, dir); 758fcf59617SAndrey V. Elsukov if (sp == NULL && key_havesp(dir)) { 759fcf59617SAndrey V. Elsukov /* Make an index to look for a policy. */ 760fcf59617SAndrey V. Elsukov ipsec6_setspidx_ipaddr(m, &spidx); 76122bbefb2SAndrey V. Elsukov ipsec6_get_ulp(m, &spidx, needport); 762fcf59617SAndrey V. Elsukov spidx.dir = dir; 763fcf59617SAndrey V. Elsukov sp = key_allocsp(&spidx, dir); 764fcf59617SAndrey V. Elsukov } 765fcf59617SAndrey V. Elsukov if (sp == NULL) /* No SP found, use system default. */ 766fcf59617SAndrey V. Elsukov sp = key_allocsp_default(); 767fcf59617SAndrey V. Elsukov return (sp); 768fcf59617SAndrey V. Elsukov } 769fcf59617SAndrey V. Elsukov 770fcf59617SAndrey V. Elsukov /* 771fcf59617SAndrey V. Elsukov * Check security policy for *OUTBOUND* IPv6 packet. 772fcf59617SAndrey V. Elsukov */ 773fcf59617SAndrey V. Elsukov struct secpolicy * 77422bbefb2SAndrey V. Elsukov ipsec6_checkpolicy(const struct mbuf *m, struct inpcb *inp, int *error, 77522bbefb2SAndrey V. Elsukov int needport) 776fcf59617SAndrey V. Elsukov { 777fcf59617SAndrey V. Elsukov struct secpolicy *sp; 778fcf59617SAndrey V. Elsukov 779fcf59617SAndrey V. Elsukov *error = 0; 78022bbefb2SAndrey V. Elsukov sp = ipsec6_getpolicy(m, inp, IPSEC_DIR_OUTBOUND, needport); 781fcf59617SAndrey V. Elsukov if (sp != NULL) 782fcf59617SAndrey V. Elsukov sp = ipsec_checkpolicy(sp, inp, error); 783fcf59617SAndrey V. Elsukov if (sp == NULL) { 784fcf59617SAndrey V. Elsukov switch (*error) { 785fcf59617SAndrey V. Elsukov case 0: /* No IPsec required: BYPASS or NONE */ 786fcf59617SAndrey V. Elsukov break; 787fcf59617SAndrey V. Elsukov case -EINVAL: 788fcf59617SAndrey V. Elsukov IPSEC6STAT_INC(ips_out_polvio); 789fcf59617SAndrey V. Elsukov break; 790fcf59617SAndrey V. Elsukov default: 791fcf59617SAndrey V. Elsukov IPSEC6STAT_INC(ips_out_inval); 792fcf59617SAndrey V. Elsukov } 793fcf59617SAndrey V. Elsukov } 794fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_STAMP, 795fcf59617SAndrey V. Elsukov printf("%s: using SP(%p), error %d\n", __func__, sp, *error)); 796fcf59617SAndrey V. Elsukov if (sp != NULL) 797fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp)); 798fcf59617SAndrey V. Elsukov return (sp); 799fcf59617SAndrey V. Elsukov } 800fcf59617SAndrey V. Elsukov 801fcf59617SAndrey V. Elsukov /* 802fcf59617SAndrey V. Elsukov * Check IPv6 packet against inbound security policy. 803fcf59617SAndrey V. Elsukov * This function is called from tcp6_input(), udp6_input(), 804fcf59617SAndrey V. Elsukov * rip6_input() and sctp_input(). 805fcf59617SAndrey V. Elsukov */ 806fcf59617SAndrey V. Elsukov int 807fcf59617SAndrey V. Elsukov ipsec6_in_reject(const struct mbuf *m, struct inpcb *inp) 808fcf59617SAndrey V. Elsukov { 809fcf59617SAndrey V. Elsukov struct secpolicy *sp; 810fcf59617SAndrey V. Elsukov int result; 811fcf59617SAndrey V. Elsukov 81222bbefb2SAndrey V. Elsukov sp = ipsec6_getpolicy(m, inp, IPSEC_DIR_INBOUND, 0); 813fcf59617SAndrey V. Elsukov result = ipsec_in_reject(sp, inp, m); 814fcf59617SAndrey V. Elsukov key_freesp(&sp); 815fcf59617SAndrey V. Elsukov if (result) 816fcf59617SAndrey V. Elsukov IPSEC6STAT_INC(ips_in_polvio); 817fcf59617SAndrey V. Elsukov return (result); 818fcf59617SAndrey V. Elsukov } 819fcf59617SAndrey V. Elsukov 820fcf59617SAndrey V. Elsukov /* 821fcf59617SAndrey V. Elsukov * IPSEC_CAP() method implementation for IPv6. 822fcf59617SAndrey V. Elsukov */ 823fcf59617SAndrey V. Elsukov int 824fcf59617SAndrey V. Elsukov ipsec6_capability(struct mbuf *m, u_int cap) 825fcf59617SAndrey V. Elsukov { 826fcf59617SAndrey V. Elsukov 827fcf59617SAndrey V. Elsukov switch (cap) { 828fcf59617SAndrey V. Elsukov case IPSEC_CAP_BYPASS_FILTER: 829fcf59617SAndrey V. Elsukov /* 830fcf59617SAndrey V. Elsukov * Bypass packet filtering for packets previously handled 831fcf59617SAndrey V. Elsukov * by IPsec. 832fcf59617SAndrey V. Elsukov */ 833fcf59617SAndrey V. Elsukov if (!V_ip6_filtertunnel && 834fcf59617SAndrey V. Elsukov m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL) 835fcf59617SAndrey V. Elsukov return (1); 836fcf59617SAndrey V. Elsukov return (0); 837fcf59617SAndrey V. Elsukov case IPSEC_CAP_OPERABLE: 838fcf59617SAndrey V. Elsukov /* Do we have active security policies? */ 839fcf59617SAndrey V. Elsukov if (key_havesp(IPSEC_DIR_INBOUND) != 0 || 840fcf59617SAndrey V. Elsukov key_havesp(IPSEC_DIR_OUTBOUND) != 0) 841fcf59617SAndrey V. Elsukov return (1); 842fcf59617SAndrey V. Elsukov return (0); 843fcf59617SAndrey V. Elsukov }; 844fcf59617SAndrey V. Elsukov return (EOPNOTSUPP); 845fcf59617SAndrey V. Elsukov } 846fcf59617SAndrey V. Elsukov #endif /* INET6 */ 84788768458SSam Leffler 848ef91a976SAndrey V. Elsukov int 849ef91a976SAndrey V. Elsukov ipsec_run_hhooks(struct ipsec_ctx_data *ctx, int type) 850ef91a976SAndrey V. Elsukov { 851ef91a976SAndrey V. Elsukov int idx; 852ef91a976SAndrey V. Elsukov 853ef91a976SAndrey V. Elsukov switch (ctx->af) { 854ef91a976SAndrey V. Elsukov #ifdef INET 855ef91a976SAndrey V. Elsukov case AF_INET: 856ef91a976SAndrey V. Elsukov idx = HHOOK_IPSEC_INET; 857ef91a976SAndrey V. Elsukov break; 858ef91a976SAndrey V. Elsukov #endif 859ef91a976SAndrey V. Elsukov #ifdef INET6 860ef91a976SAndrey V. Elsukov case AF_INET6: 861ef91a976SAndrey V. Elsukov idx = HHOOK_IPSEC_INET6; 862ef91a976SAndrey V. Elsukov break; 863ef91a976SAndrey V. Elsukov #endif 864ef91a976SAndrey V. Elsukov default: 865ef91a976SAndrey V. Elsukov return (EPFNOSUPPORT); 866ef91a976SAndrey V. Elsukov } 867ef91a976SAndrey V. Elsukov if (type == HHOOK_TYPE_IPSEC_IN) 868ef91a976SAndrey V. Elsukov HHOOKS_RUN_IF(V_ipsec_hhh_in[idx], ctx, NULL); 869ef91a976SAndrey V. Elsukov else 870ef91a976SAndrey V. Elsukov HHOOKS_RUN_IF(V_ipsec_hhh_out[idx], ctx, NULL); 871ef91a976SAndrey V. Elsukov if (*ctx->mp == NULL) 872ef91a976SAndrey V. Elsukov return (EACCES); 873ef91a976SAndrey V. Elsukov return (0); 874ef91a976SAndrey V. Elsukov } 875ef91a976SAndrey V. Elsukov 87688768458SSam Leffler /* 877de47c390SBjoern A. Zeeb * Return current level. 87888768458SSam Leffler * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned. 87988768458SSam Leffler */ 88088768458SSam Leffler u_int 881fcf59617SAndrey V. Elsukov ipsec_get_reqlevel(struct secpolicy *sp, u_int idx) 88288768458SSam Leffler { 883fcf59617SAndrey V. Elsukov struct ipsecrequest *isr; 88488768458SSam Leffler u_int esp_trans_deflev, esp_net_deflev; 88588768458SSam Leffler u_int ah_trans_deflev, ah_net_deflev; 886fcf59617SAndrey V. Elsukov u_int level = 0; 88788768458SSam Leffler 888fcf59617SAndrey V. Elsukov IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx)); 889de47c390SBjoern A. Zeeb /* XXX Note that we have ipseclog() expanded here - code sync issue. */ 89088768458SSam Leffler #define IPSEC_CHECK_DEFAULT(lev) \ 891fcf59617SAndrey V. Elsukov (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE && \ 892fcf59617SAndrey V. Elsukov (lev) != IPSEC_LEVEL_UNIQUE) \ 893fcf59617SAndrey V. Elsukov ? (V_ipsec_debug ? \ 894fcf59617SAndrey V. Elsukov log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\ 895fcf59617SAndrey V. Elsukov (lev), IPSEC_LEVEL_REQUIRE) : 0), \ 896fcf59617SAndrey V. Elsukov (lev) = IPSEC_LEVEL_REQUIRE, (lev) : (lev)) 897fcf59617SAndrey V. Elsukov 898fcf59617SAndrey V. Elsukov /* 899fcf59617SAndrey V. Elsukov * IPsec VTI uses unique security policy with fake spidx filled 900fcf59617SAndrey V. Elsukov * with zeroes. Just return IPSEC_LEVEL_REQUIRE instead of doing 901fcf59617SAndrey V. Elsukov * full level lookup for such policies. 902fcf59617SAndrey V. Elsukov */ 903fcf59617SAndrey V. Elsukov if (sp->state == IPSEC_SPSTATE_IFNET) { 904fcf59617SAndrey V. Elsukov IPSEC_ASSERT(sp->req[idx]->level == IPSEC_LEVEL_UNIQUE, 905fcf59617SAndrey V. Elsukov ("Wrong IPsec request level %d", sp->req[idx]->level)); 906fcf59617SAndrey V. Elsukov return (IPSEC_LEVEL_REQUIRE); 907fcf59617SAndrey V. Elsukov } 90888768458SSam Leffler 909de47c390SBjoern A. Zeeb /* Set default level. */ 910fcf59617SAndrey V. Elsukov switch (sp->spidx.src.sa.sa_family) { 91188768458SSam Leffler #ifdef INET 91288768458SSam Leffler case AF_INET: 913603724d3SBjoern A. Zeeb esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_trans_deflev); 914603724d3SBjoern A. Zeeb esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_net_deflev); 915603724d3SBjoern A. Zeeb ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_trans_deflev); 916603724d3SBjoern A. Zeeb ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_net_deflev); 91788768458SSam Leffler break; 91888768458SSam Leffler #endif 91988768458SSam Leffler #ifdef INET6 92088768458SSam Leffler case AF_INET6: 921603724d3SBjoern A. Zeeb esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_trans_deflev); 922603724d3SBjoern A. Zeeb esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_net_deflev); 923603724d3SBjoern A. Zeeb ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_trans_deflev); 924603724d3SBjoern A. Zeeb ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_net_deflev); 92588768458SSam Leffler break; 92688768458SSam Leffler #endif /* INET6 */ 92788768458SSam Leffler default: 9289ffa9677SSam Leffler panic("%s: unknown af %u", 929fcf59617SAndrey V. Elsukov __func__, sp->spidx.src.sa.sa_family); 93088768458SSam Leffler } 93188768458SSam Leffler 93288768458SSam Leffler #undef IPSEC_CHECK_DEFAULT 93388768458SSam Leffler 934fcf59617SAndrey V. Elsukov isr = sp->req[idx]; 935de47c390SBjoern A. Zeeb /* Set level. */ 93688768458SSam Leffler switch (isr->level) { 93788768458SSam Leffler case IPSEC_LEVEL_DEFAULT: 93888768458SSam Leffler switch (isr->saidx.proto) { 93988768458SSam Leffler case IPPROTO_ESP: 94088768458SSam Leffler if (isr->saidx.mode == IPSEC_MODE_TUNNEL) 94188768458SSam Leffler level = esp_net_deflev; 94288768458SSam Leffler else 94388768458SSam Leffler level = esp_trans_deflev; 94488768458SSam Leffler break; 94588768458SSam Leffler case IPPROTO_AH: 94688768458SSam Leffler if (isr->saidx.mode == IPSEC_MODE_TUNNEL) 94788768458SSam Leffler level = ah_net_deflev; 94888768458SSam Leffler else 94988768458SSam Leffler level = ah_trans_deflev; 9508381996eSSam Leffler break; 95188768458SSam Leffler case IPPROTO_IPCOMP: 95288768458SSam Leffler /* 953de47c390SBjoern A. Zeeb * We don't really care, as IPcomp document says that 954de47c390SBjoern A. Zeeb * we shouldn't compress small packets. 95588768458SSam Leffler */ 95688768458SSam Leffler level = IPSEC_LEVEL_USE; 95788768458SSam Leffler break; 95888768458SSam Leffler default: 9599ffa9677SSam Leffler panic("%s: Illegal protocol defined %u\n", __func__, 96088768458SSam Leffler isr->saidx.proto); 96188768458SSam Leffler } 96288768458SSam Leffler break; 96388768458SSam Leffler 96488768458SSam Leffler case IPSEC_LEVEL_USE: 96588768458SSam Leffler case IPSEC_LEVEL_REQUIRE: 96688768458SSam Leffler level = isr->level; 96788768458SSam Leffler break; 96888768458SSam Leffler case IPSEC_LEVEL_UNIQUE: 96988768458SSam Leffler level = IPSEC_LEVEL_REQUIRE; 97088768458SSam Leffler break; 97188768458SSam Leffler 97288768458SSam Leffler default: 9739ffa9677SSam Leffler panic("%s: Illegal IPsec level %u\n", __func__, isr->level); 97488768458SSam Leffler } 97588768458SSam Leffler 976de47c390SBjoern A. Zeeb return (level); 97788768458SSam Leffler } 97888768458SSam Leffler 979fcf59617SAndrey V. Elsukov static int 980fcf59617SAndrey V. Elsukov ipsec_check_history(const struct mbuf *m, struct secpolicy *sp, u_int idx) 981fcf59617SAndrey V. Elsukov { 982fcf59617SAndrey V. Elsukov struct xform_history *xh; 983fcf59617SAndrey V. Elsukov struct m_tag *mtag; 984fcf59617SAndrey V. Elsukov 985fcf59617SAndrey V. Elsukov mtag = NULL; 986fcf59617SAndrey V. Elsukov while ((mtag = m_tag_find(__DECONST(struct mbuf *, m), 987fcf59617SAndrey V. Elsukov PACKET_TAG_IPSEC_IN_DONE, mtag)) != NULL) { 988fcf59617SAndrey V. Elsukov xh = (struct xform_history *)(mtag + 1); 989fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DATA, 990fcf59617SAndrey V. Elsukov char buf[IPSEC_ADDRSTRLEN]; 991fcf59617SAndrey V. Elsukov printf("%s: mode %s proto %u dst %s\n", __func__, 992fcf59617SAndrey V. Elsukov kdebug_secasindex_mode(xh->mode), xh->proto, 993fcf59617SAndrey V. Elsukov ipsec_address(&xh->dst, buf, sizeof(buf)))); 994fcf59617SAndrey V. Elsukov if (xh->proto != sp->req[idx]->saidx.proto) 995fcf59617SAndrey V. Elsukov continue; 996fcf59617SAndrey V. Elsukov /* If SA had IPSEC_MODE_ANY, consider this as match. */ 997fcf59617SAndrey V. Elsukov if (xh->mode != sp->req[idx]->saidx.mode && 998fcf59617SAndrey V. Elsukov xh->mode != IPSEC_MODE_ANY) 999fcf59617SAndrey V. Elsukov continue; 1000fcf59617SAndrey V. Elsukov /* 1001fcf59617SAndrey V. Elsukov * For transport mode IPsec request doesn't contain 1002fcf59617SAndrey V. Elsukov * addresses. We need to use address from spidx. 1003fcf59617SAndrey V. Elsukov */ 1004fcf59617SAndrey V. Elsukov if (sp->req[idx]->saidx.mode == IPSEC_MODE_TRANSPORT) { 1005fcf59617SAndrey V. Elsukov if (key_sockaddrcmp_withmask(&xh->dst.sa, 1006fcf59617SAndrey V. Elsukov &sp->spidx.dst.sa, sp->spidx.prefd) != 0) 1007fcf59617SAndrey V. Elsukov continue; 1008fcf59617SAndrey V. Elsukov } else { 1009fcf59617SAndrey V. Elsukov if (key_sockaddrcmp(&xh->dst.sa, 1010fcf59617SAndrey V. Elsukov &sp->req[idx]->saidx.dst.sa, 0) != 0) 1011fcf59617SAndrey V. Elsukov continue; 1012fcf59617SAndrey V. Elsukov } 1013fcf59617SAndrey V. Elsukov return (0); /* matched */ 1014fcf59617SAndrey V. Elsukov } 1015fcf59617SAndrey V. Elsukov return (1); 1016fcf59617SAndrey V. Elsukov } 1017fcf59617SAndrey V. Elsukov 101888768458SSam Leffler /* 101988768458SSam Leffler * Check security policy requirements against the actual 102088768458SSam Leffler * packet contents. Return one if the packet should be 102188768458SSam Leffler * reject as "invalid"; otherwiser return zero to have the 102288768458SSam Leffler * packet treated as "valid". 102388768458SSam Leffler * 102488768458SSam Leffler * OUT: 102588768458SSam Leffler * 0: valid 102688768458SSam Leffler * 1: invalid 102788768458SSam Leffler */ 1028a9b9f6b6SAndrey V. Elsukov static int 1029fcf59617SAndrey V. Elsukov ipsec_in_reject(struct secpolicy *sp, struct inpcb *inp, const struct mbuf *m) 103088768458SSam Leffler { 1031fcf59617SAndrey V. Elsukov int i; 103288768458SSam Leffler 1033fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_STAMP, 1034fcf59617SAndrey V. Elsukov printf("%s: PCB(%p): using SP(%p)\n", __func__, inp, sp)); 1035fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp)); 1036fcf59617SAndrey V. Elsukov 1037fcf59617SAndrey V. Elsukov if (inp != NULL && inp->inp_sp != NULL && inp->inp_sp->sp_in == NULL) 1038fcf59617SAndrey V. Elsukov ipsec_cachepolicy(inp, sp, IPSEC_DIR_INBOUND); 103988768458SSam Leffler 1040de47c390SBjoern A. Zeeb /* Check policy. */ 104188768458SSam Leffler switch (sp->policy) { 104288768458SSam Leffler case IPSEC_POLICY_DISCARD: 1043de47c390SBjoern A. Zeeb return (1); 104488768458SSam Leffler case IPSEC_POLICY_BYPASS: 104588768458SSam Leffler case IPSEC_POLICY_NONE: 1046de47c390SBjoern A. Zeeb return (0); 104788768458SSam Leffler } 104888768458SSam Leffler 10499ffa9677SSam Leffler IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC, 10509ffa9677SSam Leffler ("invalid policy %u", sp->policy)); 105188768458SSam Leffler 1052fcf59617SAndrey V. Elsukov /* 1053fcf59617SAndrey V. Elsukov * ipsec[46]_common_input_cb after each transform adds 1054fcf59617SAndrey V. Elsukov * PACKET_TAG_IPSEC_IN_DONE mbuf tag. It contains SPI, proto, mode 1055fcf59617SAndrey V. Elsukov * and destination address from saidx. We can compare info from 1056fcf59617SAndrey V. Elsukov * these tags with requirements in SP. 1057fcf59617SAndrey V. Elsukov */ 1058fcf59617SAndrey V. Elsukov for (i = 0; i < sp->tcount; i++) { 1059fcf59617SAndrey V. Elsukov /* 1060fcf59617SAndrey V. Elsukov * Do not check IPcomp, since IPcomp document 1061fcf59617SAndrey V. Elsukov * says that we shouldn't compress small packets. 1062fcf59617SAndrey V. Elsukov * IPComp policy should always be treated as being 1063fcf59617SAndrey V. Elsukov * in "use" level. 1064fcf59617SAndrey V. Elsukov */ 1065fcf59617SAndrey V. Elsukov if (sp->req[i]->saidx.proto == IPPROTO_IPCOMP || 1066fcf59617SAndrey V. Elsukov ipsec_get_reqlevel(sp, i) != IPSEC_LEVEL_REQUIRE) 106788768458SSam Leffler continue; 1068fcf59617SAndrey V. Elsukov if (V_check_policy_history != 0 && 1069fcf59617SAndrey V. Elsukov ipsec_check_history(m, sp, i) != 0) 1070fcf59617SAndrey V. Elsukov return (1); 1071fcf59617SAndrey V. Elsukov else switch (sp->req[i]->saidx.proto) { 107288768458SSam Leffler case IPPROTO_ESP: 107388768458SSam Leffler if ((m->m_flags & M_DECRYPTED) == 0) { 1074fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DUMP, 10759ffa9677SSam Leffler printf("%s: ESP m_flags:%x\n", __func__, 107688768458SSam Leffler m->m_flags)); 1077de47c390SBjoern A. Zeeb return (1); 107888768458SSam Leffler } 107988768458SSam Leffler break; 108088768458SSam Leffler case IPPROTO_AH: 108188768458SSam Leffler if ((m->m_flags & M_AUTHIPHDR) == 0) { 1082fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DUMP, 10839ffa9677SSam Leffler printf("%s: AH m_flags:%x\n", __func__, 108488768458SSam Leffler m->m_flags)); 1085de47c390SBjoern A. Zeeb return (1); 108688768458SSam Leffler } 108788768458SSam Leffler break; 108888768458SSam Leffler } 108988768458SSam Leffler } 1090de47c390SBjoern A. Zeeb return (0); /* Valid. */ 109188768458SSam Leffler } 109288768458SSam Leffler 1093a91150daSAndrey V. Elsukov /* 1094de47c390SBjoern A. Zeeb * Compute the byte size to be occupied by IPsec header. 1095de47c390SBjoern A. Zeeb * In case it is tunnelled, it includes the size of outer IP header. 109688768458SSam Leffler */ 109788768458SSam Leffler static size_t 109897aa4a51SBjoern A. Zeeb ipsec_hdrsiz_internal(struct secpolicy *sp) 109988768458SSam Leffler { 11001f8bd75eSBjoern A. Zeeb size_t size; 1101fcf59617SAndrey V. Elsukov int i; 110288768458SSam Leffler 1103fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_STAMP, printf("%s: using SP(%p)\n", __func__, sp)); 1104fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp)); 110588768458SSam Leffler 110688768458SSam Leffler switch (sp->policy) { 110788768458SSam Leffler case IPSEC_POLICY_DISCARD: 110888768458SSam Leffler case IPSEC_POLICY_BYPASS: 110988768458SSam Leffler case IPSEC_POLICY_NONE: 1110de47c390SBjoern A. Zeeb return (0); 111188768458SSam Leffler } 111288768458SSam Leffler 11139ffa9677SSam Leffler IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC, 11149ffa9677SSam Leffler ("invalid policy %u", sp->policy)); 111588768458SSam Leffler 1116fcf59617SAndrey V. Elsukov /* 1117fcf59617SAndrey V. Elsukov * XXX: for each transform we need to lookup suitable SA 1118fcf59617SAndrey V. Elsukov * and use info from SA to calculate headers size. 1119fcf59617SAndrey V. Elsukov * XXX: for NAT-T we need to cosider UDP header size. 1120fcf59617SAndrey V. Elsukov */ 11211f8bd75eSBjoern A. Zeeb size = 0; 1122fcf59617SAndrey V. Elsukov for (i = 0; i < sp->tcount; i++) { 1123fcf59617SAndrey V. Elsukov switch (sp->req[i]->saidx.proto) { 112488768458SSam Leffler case IPPROTO_ESP: 1125fcf59617SAndrey V. Elsukov size += esp_hdrsiz(NULL); 112688768458SSam Leffler break; 112788768458SSam Leffler case IPPROTO_AH: 1128fcf59617SAndrey V. Elsukov size += ah_hdrsiz(NULL); 112988768458SSam Leffler break; 113088768458SSam Leffler case IPPROTO_IPCOMP: 1131fcf59617SAndrey V. Elsukov size += sizeof(struct ipcomp); 113288768458SSam Leffler break; 113388768458SSam Leffler } 113488768458SSam Leffler 1135fcf59617SAndrey V. Elsukov if (sp->req[i]->saidx.mode == IPSEC_MODE_TUNNEL) { 1136fcf59617SAndrey V. Elsukov switch (sp->req[i]->saidx.dst.sa.sa_family) { 1137fcf59617SAndrey V. Elsukov #ifdef INET 113888768458SSam Leffler case AF_INET: 1139fcf59617SAndrey V. Elsukov size += sizeof(struct ip); 114088768458SSam Leffler break; 1141fcf59617SAndrey V. Elsukov #endif 114288768458SSam Leffler #ifdef INET6 114388768458SSam Leffler case AF_INET6: 1144fcf59617SAndrey V. Elsukov size += sizeof(struct ip6_hdr); 114588768458SSam Leffler break; 114688768458SSam Leffler #endif 114788768458SSam Leffler default: 11489ffa9677SSam Leffler ipseclog((LOG_ERR, "%s: unknown AF %d in " 11499ffa9677SSam Leffler "IPsec tunnel SA\n", __func__, 1150fcf59617SAndrey V. Elsukov sp->req[i]->saidx.dst.sa.sa_family)); 115188768458SSam Leffler break; 115288768458SSam Leffler } 115388768458SSam Leffler } 115488768458SSam Leffler } 11551f8bd75eSBjoern A. Zeeb return (size); 115688768458SSam Leffler } 115788768458SSam Leffler 115897aa4a51SBjoern A. Zeeb /* 1159fcf59617SAndrey V. Elsukov * Compute ESP/AH header size for protocols with PCB, including 1160fcf59617SAndrey V. Elsukov * outer IP header. Currently only tcp_output() uses it. 116197aa4a51SBjoern A. Zeeb */ 116288768458SSam Leffler size_t 1163fcf59617SAndrey V. Elsukov ipsec_hdrsiz_inpcb(struct inpcb *inp) 116488768458SSam Leffler { 1165fcf59617SAndrey V. Elsukov struct secpolicyindex spidx; 116688768458SSam Leffler struct secpolicy *sp; 1167fcf59617SAndrey V. Elsukov size_t sz; 116888768458SSam Leffler 1169fcf59617SAndrey V. Elsukov sp = ipsec_getpcbpolicy(inp, IPSEC_DIR_OUTBOUND); 1170fcf59617SAndrey V. Elsukov if (sp == NULL && key_havesp(IPSEC_DIR_OUTBOUND)) { 1171fcf59617SAndrey V. Elsukov ipsec_setspidx_inpcb(inp, &spidx, IPSEC_DIR_OUTBOUND); 1172fcf59617SAndrey V. Elsukov sp = key_allocsp(&spidx, IPSEC_DIR_OUTBOUND); 117388768458SSam Leffler } 1174fcf59617SAndrey V. Elsukov if (sp == NULL) 1175fcf59617SAndrey V. Elsukov sp = key_allocsp_default(); 1176fcf59617SAndrey V. Elsukov sz = ipsec_hdrsiz_internal(sp); 1177fcf59617SAndrey V. Elsukov key_freesp(&sp); 1178fcf59617SAndrey V. Elsukov return (sz); 117988768458SSam Leffler } 118088768458SSam Leffler 118188768458SSam Leffler /* 118288768458SSam Leffler * Check the variable replay window. 118388768458SSam Leffler * ipsec_chkreplay() performs replay check before ICV verification. 118488768458SSam Leffler * ipsec_updatereplay() updates replay bitmap. This must be called after 118588768458SSam Leffler * ICV verification (it also performs replay check, which is usually done 118688768458SSam Leffler * beforehand). 118788768458SSam Leffler * 0 (zero) is returned if packet disallowed, 1 if packet permitted. 118888768458SSam Leffler * 1189bf435626SFabien Thomas * Based on RFC 6479. Blocks are 32 bits unsigned integers 119088768458SSam Leffler */ 1191bf435626SFabien Thomas 1192bf435626SFabien Thomas #define IPSEC_BITMAP_INDEX_MASK(w) (w - 1) 1193bf435626SFabien Thomas #define IPSEC_REDUNDANT_BIT_SHIFTS 5 1194bf435626SFabien Thomas #define IPSEC_REDUNDANT_BITS (1 << IPSEC_REDUNDANT_BIT_SHIFTS) 1195bf435626SFabien Thomas #define IPSEC_BITMAP_LOC_MASK (IPSEC_REDUNDANT_BITS - 1) 1196bf435626SFabien Thomas 119788768458SSam Leffler int 1198fcf59617SAndrey V. Elsukov ipsec_chkreplay(uint32_t seq, struct secasvar *sav) 119988768458SSam Leffler { 120088768458SSam Leffler const struct secreplay *replay; 1201fcf59617SAndrey V. Elsukov uint32_t wsizeb; /* Constant: window size. */ 1202fcf59617SAndrey V. Elsukov int index, bit_location; 120388768458SSam Leffler 12049ffa9677SSam Leffler IPSEC_ASSERT(sav != NULL, ("Null SA")); 12059ffa9677SSam Leffler IPSEC_ASSERT(sav->replay != NULL, ("Null replay state")); 120688768458SSam Leffler 120788768458SSam Leffler replay = sav->replay; 120888768458SSam Leffler 1209bf435626SFabien Thomas /* No need to check replay if disabled. */ 121088768458SSam Leffler if (replay->wsize == 0) 1211fcf59617SAndrey V. Elsukov return (1); 121288768458SSam Leffler 1213de47c390SBjoern A. Zeeb /* Constant. */ 121488768458SSam Leffler wsizeb = replay->wsize << 3; 121588768458SSam Leffler 1216de47c390SBjoern A. Zeeb /* Sequence number of 0 is invalid. */ 121788768458SSam Leffler if (seq == 0) 1218fcf59617SAndrey V. Elsukov return (0); 121988768458SSam Leffler 1220de47c390SBjoern A. Zeeb /* First time is always okay. */ 122188768458SSam Leffler if (replay->count == 0) 1222fcf59617SAndrey V. Elsukov return (1); 122388768458SSam Leffler 1224de47c390SBjoern A. Zeeb /* Larger sequences are okay. */ 1225bf435626SFabien Thomas if (seq > replay->lastseq) 1226fcf59617SAndrey V. Elsukov return (1); 122788768458SSam Leffler 1228de47c390SBjoern A. Zeeb /* Over range to check, i.e. too old or wrapped. */ 1229bf435626SFabien Thomas if (replay->lastseq - seq >= wsizeb) 1230fcf59617SAndrey V. Elsukov return (0); 123188768458SSam Leffler 1232bf435626SFabien Thomas /* The sequence is inside the sliding window 1233bf435626SFabien Thomas * now check the bit in the bitmap 1234bf435626SFabien Thomas * bit location only depends on the sequence number 1235bf435626SFabien Thomas */ 1236bf435626SFabien Thomas bit_location = seq & IPSEC_BITMAP_LOC_MASK; 1237bf435626SFabien Thomas index = (seq >> IPSEC_REDUNDANT_BIT_SHIFTS) 1238bf435626SFabien Thomas & IPSEC_BITMAP_INDEX_MASK(replay->bitmap_size); 123988768458SSam Leffler 1240de47c390SBjoern A. Zeeb /* This packet already seen? */ 1241bf435626SFabien Thomas if ((replay->bitmap)[index] & (1 << bit_location)) 1242fcf59617SAndrey V. Elsukov return (0); 1243fcf59617SAndrey V. Elsukov return (1); 124488768458SSam Leffler } 124588768458SSam Leffler 124688768458SSam Leffler /* 1247de47c390SBjoern A. Zeeb * Check replay counter whether to update or not. 124888768458SSam Leffler * OUT: 0: OK 124988768458SSam Leffler * 1: NG 125088768458SSam Leffler */ 125188768458SSam Leffler int 1252fcf59617SAndrey V. Elsukov ipsec_updatereplay(uint32_t seq, struct secasvar *sav) 125388768458SSam Leffler { 1254962ac6c7SAndrey V. Elsukov char buf[128]; 125588768458SSam Leffler struct secreplay *replay; 1256fcf59617SAndrey V. Elsukov uint32_t wsizeb; /* Constant: window size. */ 1257fcf59617SAndrey V. Elsukov int diff, index, bit_location; 125888768458SSam Leffler 12599ffa9677SSam Leffler IPSEC_ASSERT(sav != NULL, ("Null SA")); 12609ffa9677SSam Leffler IPSEC_ASSERT(sav->replay != NULL, ("Null replay state")); 126188768458SSam Leffler 126288768458SSam Leffler replay = sav->replay; 126388768458SSam Leffler 126488768458SSam Leffler if (replay->wsize == 0) 1265de47c390SBjoern A. Zeeb goto ok; /* No need to check replay. */ 126688768458SSam Leffler 1267de47c390SBjoern A. Zeeb /* Constant. */ 126888768458SSam Leffler wsizeb = replay->wsize << 3; 126988768458SSam Leffler 1270de47c390SBjoern A. Zeeb /* Sequence number of 0 is invalid. */ 127188768458SSam Leffler if (seq == 0) 1272fcf59617SAndrey V. Elsukov return (1); 127388768458SSam Leffler 1274bf435626SFabien Thomas /* The packet is too old, no need to update */ 1275bf435626SFabien Thomas if (wsizeb + seq < replay->lastseq) 127688768458SSam Leffler goto ok; 127788768458SSam Leffler 1278bf435626SFabien Thomas /* Now update the bit */ 1279bf435626SFabien Thomas index = (seq >> IPSEC_REDUNDANT_BIT_SHIFTS); 1280bf435626SFabien Thomas 1281bf435626SFabien Thomas /* First check if the sequence number is in the range */ 128288768458SSam Leffler if (seq > replay->lastseq) { 1283bf435626SFabien Thomas int id; 1284bf435626SFabien Thomas int index_cur = replay->lastseq >> IPSEC_REDUNDANT_BIT_SHIFTS; 128588768458SSam Leffler 1286bf435626SFabien Thomas diff = index - index_cur; 1287bf435626SFabien Thomas if (diff > replay->bitmap_size) { 1288bf435626SFabien Thomas /* something unusual in this case */ 1289bf435626SFabien Thomas diff = replay->bitmap_size; 129088768458SSam Leffler } 1291bf435626SFabien Thomas 1292bf435626SFabien Thomas for (id = 0; id < diff; ++id) { 1293bf435626SFabien Thomas replay->bitmap[(id + index_cur + 1) 1294bf435626SFabien Thomas & IPSEC_BITMAP_INDEX_MASK(replay->bitmap_size)] = 0; 1295bf435626SFabien Thomas } 1296bf435626SFabien Thomas 129788768458SSam Leffler replay->lastseq = seq; 129888768458SSam Leffler } 129988768458SSam Leffler 1300bf435626SFabien Thomas index &= IPSEC_BITMAP_INDEX_MASK(replay->bitmap_size); 1301bf435626SFabien Thomas bit_location = seq & IPSEC_BITMAP_LOC_MASK; 1302bf435626SFabien Thomas 1303bf435626SFabien Thomas /* this packet has already been received */ 1304bf435626SFabien Thomas if (replay->bitmap[index] & (1 << bit_location)) 1305fcf59617SAndrey V. Elsukov return (1); 1306bf435626SFabien Thomas 1307bf435626SFabien Thomas replay->bitmap[index] |= (1 << bit_location); 1308bf435626SFabien Thomas 130988768458SSam Leffler ok: 131088768458SSam Leffler if (replay->count == ~0) { 131188768458SSam Leffler 1312de47c390SBjoern A. Zeeb /* Set overflow flag. */ 131388768458SSam Leffler replay->overflow++; 131488768458SSam Leffler 1315de47c390SBjoern A. Zeeb /* Don't increment, no more packets accepted. */ 1316fcf59617SAndrey V. Elsukov if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) { 1317fcf59617SAndrey V. Elsukov if (sav->sah->saidx.proto == IPPROTO_AH) 1318fcf59617SAndrey V. Elsukov AHSTAT_INC(ahs_wrap); 1319fcf59617SAndrey V. Elsukov else if (sav->sah->saidx.proto == IPPROTO_ESP) 1320fcf59617SAndrey V. Elsukov ESPSTAT_INC(esps_wrap); 1321fcf59617SAndrey V. Elsukov return (1); 1322fcf59617SAndrey V. Elsukov } 132388768458SSam Leffler 13249ffa9677SSam Leffler ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n", 1325962ac6c7SAndrey V. Elsukov __func__, replay->overflow, 1326fcf59617SAndrey V. Elsukov ipsec_sa2str(sav, buf, sizeof(buf)))); 1327fcf59617SAndrey V. Elsukov } 1328d5f39c34SFabien Thomas 1329d5f39c34SFabien Thomas replay->count++; 1330fcf59617SAndrey V. Elsukov return (0); 133188768458SSam Leffler } 133288768458SSam Leffler 1333fcf59617SAndrey V. Elsukov int 13342e08e39fSConrad Meyer ipsec_updateid(struct secasvar *sav, crypto_session_t *new, 13352e08e39fSConrad Meyer crypto_session_t *old) 1336fcf59617SAndrey V. Elsukov { 13372e08e39fSConrad Meyer crypto_session_t tmp; 133888768458SSam Leffler 1339fcf59617SAndrey V. Elsukov /* 1340fcf59617SAndrey V. Elsukov * tdb_cryptoid is initialized by xform_init(). 1341fcf59617SAndrey V. Elsukov * Then it can be changed only when some crypto error occurred or 1342fcf59617SAndrey V. Elsukov * when SA is deleted. We stored used cryptoid in the xform_data 1343fcf59617SAndrey V. Elsukov * structure. In case when crypto error occurred and crypto 1344fcf59617SAndrey V. Elsukov * subsystem has reinited the session, it returns new cryptoid 1345fcf59617SAndrey V. Elsukov * and EAGAIN error code. 1346fcf59617SAndrey V. Elsukov * 1347fcf59617SAndrey V. Elsukov * This function will be called when we got EAGAIN from crypto 1348fcf59617SAndrey V. Elsukov * subsystem. 1349fcf59617SAndrey V. Elsukov * *new is cryptoid that was returned by crypto subsystem in 1350fcf59617SAndrey V. Elsukov * the crp_sid. 1351fcf59617SAndrey V. Elsukov * *old is the original cryptoid that we stored in xform_data. 1352fcf59617SAndrey V. Elsukov * 1353fcf59617SAndrey V. Elsukov * For first failed request *old == sav->tdb_cryptoid, then 1354fcf59617SAndrey V. Elsukov * we update sav->tdb_cryptoid and redo crypto_dispatch(). 1355fcf59617SAndrey V. Elsukov * For next failed request *old != sav->tdb_cryptoid, then 1356fcf59617SAndrey V. Elsukov * we store cryptoid from first request into the *new variable 1357fcf59617SAndrey V. Elsukov * and crp_sid from this second session will be returned via 1358fcf59617SAndrey V. Elsukov * *old pointer, so caller can release second session. 1359fcf59617SAndrey V. Elsukov * 1360fcf59617SAndrey V. Elsukov * XXXAE: check this more carefully. 1361fcf59617SAndrey V. Elsukov */ 1362fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_STAMP, 13631b0909d5SConrad Meyer printf("%s: SA(%p) moves cryptoid %p -> %p\n", 13641b0909d5SConrad Meyer __func__, sav, *old, *new)); 1365fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DATA, kdebug_secasv(sav)); 1366fcf59617SAndrey V. Elsukov SECASVAR_LOCK(sav); 1367fcf59617SAndrey V. Elsukov if (sav->tdb_cryptoid != *old) { 1368fcf59617SAndrey V. Elsukov /* cryptoid was already updated */ 1369fcf59617SAndrey V. Elsukov tmp = *new; 1370fcf59617SAndrey V. Elsukov *new = sav->tdb_cryptoid; 1371fcf59617SAndrey V. Elsukov *old = tmp; 1372bf435626SFabien Thomas SECASVAR_UNLOCK(sav); 1373fcf59617SAndrey V. Elsukov return (1); 1374fcf59617SAndrey V. Elsukov } 1375fcf59617SAndrey V. Elsukov sav->tdb_cryptoid = *new; 1376fcf59617SAndrey V. Elsukov SECASVAR_UNLOCK(sav); 1377fcf59617SAndrey V. Elsukov return (0); 137888768458SSam Leffler } 137988768458SSam Leffler 1380fcf59617SAndrey V. Elsukov int 1381fcf59617SAndrey V. Elsukov ipsec_initialized(void) 138288768458SSam Leffler { 1383de47c390SBjoern A. Zeeb 1384fcf59617SAndrey V. Elsukov return (V_def_policy != NULL); 138588768458SSam Leffler } 138688768458SSam Leffler 13878381996eSSam Leffler static void 138893201211SAndrey V. Elsukov def_policy_init(const void *unused __unused) 13891ed81b73SMarko Zec { 13901ed81b73SMarko Zec 1391fcf59617SAndrey V. Elsukov V_def_policy = key_newsp(); 1392fcf59617SAndrey V. Elsukov if (V_def_policy != NULL) { 1393fcf59617SAndrey V. Elsukov V_def_policy->policy = IPSEC_POLICY_NONE; 1394fcf59617SAndrey V. Elsukov /* Force INPCB SP cache invalidation */ 1395fcf59617SAndrey V. Elsukov key_bumpspgen(); 1396fcf59617SAndrey V. Elsukov } else 1397fcf59617SAndrey V. Elsukov printf("%s: failed to initialize default policy\n", __func__); 13988381996eSSam Leffler } 1399fcf59617SAndrey V. Elsukov 1400fcf59617SAndrey V. Elsukov 1401fcf59617SAndrey V. Elsukov static void 1402fcf59617SAndrey V. Elsukov def_policy_uninit(const void *unused __unused) 1403fcf59617SAndrey V. Elsukov { 1404fcf59617SAndrey V. Elsukov 1405fcf59617SAndrey V. Elsukov if (V_def_policy != NULL) { 1406fcf59617SAndrey V. Elsukov key_freesp(&V_def_policy); 1407fcf59617SAndrey V. Elsukov key_bumpspgen(); 1408fcf59617SAndrey V. Elsukov } 1409fcf59617SAndrey V. Elsukov } 1410fcf59617SAndrey V. Elsukov 141189856f7eSBjoern A. Zeeb VNET_SYSINIT(def_policy_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, 141293201211SAndrey V. Elsukov def_policy_init, NULL); 1413fcf59617SAndrey V. Elsukov VNET_SYSUNINIT(def_policy_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, 1414fcf59617SAndrey V. Elsukov def_policy_uninit, NULL); 1415