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, 1787029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1797029da5cSPawel 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 2206131838bSPawel Jakub Dawidek #ifdef REGRESSION 221dfa9422bSPawel Jakub Dawidek /* 222dfa9422bSPawel Jakub Dawidek * When set to 1, IPsec will send packets with the same sequence number. 223dfa9422bSPawel Jakub Dawidek * This allows to verify if the other side has proper replay attacks detection. 224dfa9422bSPawel Jakub Dawidek */ 225eddfbb76SRobert Watson VNET_DEFINE(int, ipsec_replay) = 0; 2266df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_replay, 2276df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_replay), 0, 228eddfbb76SRobert Watson "Emulate replay attack"); 229dfa9422bSPawel Jakub Dawidek /* 230dfa9422bSPawel Jakub Dawidek * When set 1, IPsec will send packets with corrupted HMAC. 231dfa9422bSPawel Jakub Dawidek * This allows to verify if the other side properly detects modified packets. 232dfa9422bSPawel Jakub Dawidek */ 233eddfbb76SRobert Watson VNET_DEFINE(int, ipsec_integrity) = 0; 2346df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_integrity, 2356df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_integrity), 0, 236eddfbb76SRobert Watson "Emulate man-in-the-middle attack"); 2376131838bSPawel Jakub Dawidek #endif 238dfa9422bSPawel Jakub Dawidek 23988768458SSam Leffler #ifdef INET6 240db8c0879SAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct ipsecstat, ipsec6stat); 241db8c0879SAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(ipsec6stat); 242db8c0879SAndrey V. Elsukov 243db8c0879SAndrey V. Elsukov #ifdef VIMAGE 244db8c0879SAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(ipsec6stat); 245db8c0879SAndrey V. Elsukov #endif /* VIMAGE */ 246db8c0879SAndrey V. Elsukov 247eddfbb76SRobert Watson VNET_DEFINE(int, ip6_esp_trans_deflev) = IPSEC_LEVEL_USE; 248eddfbb76SRobert Watson VNET_DEFINE(int, ip6_esp_net_deflev) = IPSEC_LEVEL_USE; 249eddfbb76SRobert Watson VNET_DEFINE(int, ip6_ah_trans_deflev) = IPSEC_LEVEL_USE; 250eddfbb76SRobert Watson VNET_DEFINE(int, ip6_ah_net_deflev) = IPSEC_LEVEL_USE; 251eddfbb76SRobert Watson VNET_DEFINE(int, ip6_ipsec_ecn) = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */ 25288768458SSam Leffler 2535f901c92SAndrew Turner VNET_DEFINE_STATIC(int, ip6_filtertunnel) = 0; 254fcf59617SAndrey V. Elsukov #define V_ip6_filtertunnel VNET(ip6_filtertunnel) 255fcf59617SAndrey V. Elsukov 25688768458SSam Leffler SYSCTL_DECL(_net_inet6_ipsec6); 25788768458SSam Leffler 25888768458SSam Leffler /* net.inet6.ipsec6 */ 259fcf59617SAndrey V. Elsukov SYSCTL_PROC(_net_inet6_ipsec6, IPSECCTL_DEF_POLICY, def_policy, 2607029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 2617029da5cSPawel Biernacki 0, 0, sysctl_def_policy, "I", 2628b615593SMarko Zec "IPsec default policy."); 2636df8a710SGleb Smirnoff SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev, 2646df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_esp_trans_deflev), 0, 2658b615593SMarko Zec "Default ESP transport mode level."); 2666df8a710SGleb Smirnoff SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev, 2676df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_esp_net_deflev), 0, 2688b615593SMarko Zec "Default ESP tunnel mode level."); 2696df8a710SGleb Smirnoff SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev, 2706df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ah_trans_deflev), 0, 2718b615593SMarko Zec "AH transfer mode default level."); 2726df8a710SGleb Smirnoff SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev, 2736df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ah_net_deflev), 0, 2748b615593SMarko Zec "AH tunnel mode default level."); 2756df8a710SGleb Smirnoff SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ECN, ecn, 2766df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ipsec_ecn), 0, 2773377c961STom Rhodes "Explicit Congestion Notification handling."); 278fcf59617SAndrey V. Elsukov SYSCTL_INT(_net_inet6_ipsec6, OID_AUTO, filtertunnel, 279fcf59617SAndrey V. Elsukov CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_filtertunnel), 0, 280fcf59617SAndrey V. Elsukov "If set, filter packets from an IPsec tunnel."); 281db8c0879SAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet6_ipsec6, IPSECCTL_STATS, ipsecstats, 282db8c0879SAndrey V. Elsukov struct ipsecstat, ipsec6stat, "IPsec IPv6 statistics."); 28388768458SSam Leffler #endif /* INET6 */ 28488768458SSam Leffler 285fcf59617SAndrey V. Elsukov static int ipsec_in_reject(struct secpolicy *, struct inpcb *, 286fcf59617SAndrey V. Elsukov const struct mbuf *); 287fcf59617SAndrey V. Elsukov 288fcf59617SAndrey V. Elsukov #ifdef INET 289fcf59617SAndrey V. Elsukov static void ipsec4_get_ulp(const struct mbuf *, struct secpolicyindex *, int); 290fcf59617SAndrey V. Elsukov static void ipsec4_setspidx_ipaddr(const struct mbuf *, 291fcf59617SAndrey V. Elsukov struct secpolicyindex *); 292fcf59617SAndrey V. Elsukov #endif 29388768458SSam Leffler #ifdef INET6 294efb10c3cSAndrey V. Elsukov static void ipsec6_get_ulp(const struct mbuf *m, struct secpolicyindex *, int); 295fcf59617SAndrey V. Elsukov static void ipsec6_setspidx_ipaddr(const struct mbuf *, 296fcf59617SAndrey V. Elsukov struct secpolicyindex *); 29788768458SSam Leffler #endif 2986464079fSSam Leffler 29988768458SSam Leffler /* 30088768458SSam Leffler * Return a held reference to the default SP. 30188768458SSam Leffler */ 30288768458SSam Leffler static struct secpolicy * 303fcf59617SAndrey V. Elsukov key_allocsp_default(void) 304fcf59617SAndrey V. Elsukov { 305fcf59617SAndrey V. Elsukov 306fcf59617SAndrey V. Elsukov key_addref(V_def_policy); 307fcf59617SAndrey V. Elsukov return (V_def_policy); 308fcf59617SAndrey V. Elsukov } 309fcf59617SAndrey V. Elsukov 310fcf59617SAndrey V. Elsukov static void 311fcf59617SAndrey V. Elsukov ipsec_invalidate_cache(struct inpcb *inp, u_int dir) 31288768458SSam Leffler { 31388768458SSam Leffler struct secpolicy *sp; 31488768458SSam Leffler 315fcf59617SAndrey V. Elsukov INP_WLOCK_ASSERT(inp); 316fcf59617SAndrey V. Elsukov if (dir == IPSEC_DIR_OUTBOUND) { 317fcf59617SAndrey V. Elsukov if (inp->inp_sp->flags & INP_INBOUND_POLICY) 318fcf59617SAndrey V. Elsukov return; 319fcf59617SAndrey V. Elsukov sp = inp->inp_sp->sp_in; 320fcf59617SAndrey V. Elsukov inp->inp_sp->sp_in = NULL; 321fcf59617SAndrey V. Elsukov } else { 322fcf59617SAndrey V. Elsukov if (inp->inp_sp->flags & INP_OUTBOUND_POLICY) 323fcf59617SAndrey V. Elsukov return; 324fcf59617SAndrey V. Elsukov sp = inp->inp_sp->sp_out; 325fcf59617SAndrey V. Elsukov inp->inp_sp->sp_out = NULL; 32688768458SSam Leffler } 327fcf59617SAndrey V. Elsukov if (sp != NULL) 328fcf59617SAndrey V. Elsukov key_freesp(&sp); /* release extra reference */ 32988768458SSam Leffler } 33088768458SSam Leffler 331fcf59617SAndrey V. Elsukov static void 332fcf59617SAndrey V. Elsukov ipsec_cachepolicy(struct inpcb *inp, struct secpolicy *sp, u_int dir) 333fcf59617SAndrey V. Elsukov { 334fcf59617SAndrey V. Elsukov uint32_t genid; 335fcf59617SAndrey V. Elsukov int downgrade; 336fcf59617SAndrey V. Elsukov 337fcf59617SAndrey V. Elsukov INP_LOCK_ASSERT(inp); 338fcf59617SAndrey V. Elsukov 339fcf59617SAndrey V. Elsukov if (dir == IPSEC_DIR_OUTBOUND) { 340fcf59617SAndrey V. Elsukov /* Do we have configured PCB policy? */ 341fcf59617SAndrey V. Elsukov if (inp->inp_sp->flags & INP_OUTBOUND_POLICY) 342fcf59617SAndrey V. Elsukov return; 343fcf59617SAndrey V. Elsukov /* Another thread has already set cached policy */ 344fcf59617SAndrey V. Elsukov if (inp->inp_sp->sp_out != NULL) 345fcf59617SAndrey V. Elsukov return; 34688768458SSam Leffler /* 347fcf59617SAndrey V. Elsukov * Do not cache OUTBOUND policy if PCB isn't connected, 348fcf59617SAndrey V. Elsukov * i.e. foreign address is INADDR_ANY/UNSPECIFIED. 34988768458SSam Leffler */ 350fcf59617SAndrey V. Elsukov #ifdef INET 351fcf59617SAndrey V. Elsukov if ((inp->inp_vflag & INP_IPV4) != 0 && 352fcf59617SAndrey V. Elsukov inp->inp_faddr.s_addr == INADDR_ANY) 353fcf59617SAndrey V. Elsukov return; 354fcf59617SAndrey V. Elsukov #endif 355fcf59617SAndrey V. Elsukov #ifdef INET6 356fcf59617SAndrey V. Elsukov if ((inp->inp_vflag & INP_IPV6) != 0 && 357fcf59617SAndrey V. Elsukov IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 358fcf59617SAndrey V. Elsukov return; 359fcf59617SAndrey V. Elsukov #endif 360fcf59617SAndrey V. Elsukov } else { 361fcf59617SAndrey V. Elsukov /* Do we have configured PCB policy? */ 362fcf59617SAndrey V. Elsukov if (inp->inp_sp->flags & INP_INBOUND_POLICY) 363fcf59617SAndrey V. Elsukov return; 364fcf59617SAndrey V. Elsukov /* Another thread has already set cached policy */ 365fcf59617SAndrey V. Elsukov if (inp->inp_sp->sp_in != NULL) 366fcf59617SAndrey V. Elsukov return; 36788768458SSam Leffler /* 368fcf59617SAndrey V. Elsukov * Do not cache INBOUND policy for listen socket, 369fcf59617SAndrey V. Elsukov * that is bound to INADDR_ANY/UNSPECIFIED address. 37088768458SSam Leffler */ 371fcf59617SAndrey V. Elsukov #ifdef INET 372fcf59617SAndrey V. Elsukov if ((inp->inp_vflag & INP_IPV4) != 0 && 373fcf59617SAndrey V. Elsukov inp->inp_faddr.s_addr == INADDR_ANY) 374fcf59617SAndrey V. Elsukov return; 375fcf59617SAndrey V. Elsukov #endif 376fcf59617SAndrey V. Elsukov #ifdef INET6 377fcf59617SAndrey V. Elsukov if ((inp->inp_vflag & INP_IPV6) != 0 && 378fcf59617SAndrey V. Elsukov IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 379fcf59617SAndrey V. Elsukov return; 380fcf59617SAndrey V. Elsukov #endif 381c1fc5e96SErmal Luçi } 382fcf59617SAndrey V. Elsukov downgrade = 0; 383fcf59617SAndrey V. Elsukov if (!INP_WLOCKED(inp)) { 384fcf59617SAndrey V. Elsukov if ((downgrade = INP_TRY_UPGRADE(inp)) == 0) 385fcf59617SAndrey V. Elsukov return; 38688768458SSam Leffler } 387fcf59617SAndrey V. Elsukov if (dir == IPSEC_DIR_OUTBOUND) 388fcf59617SAndrey V. Elsukov inp->inp_sp->sp_out = sp; 38988768458SSam Leffler else 390fcf59617SAndrey V. Elsukov inp->inp_sp->sp_in = sp; 391fcf59617SAndrey V. Elsukov /* 392fcf59617SAndrey V. Elsukov * SP is already referenced by the lookup code. 393fcf59617SAndrey V. Elsukov * We take extra reference here to avoid race in the 394fcf59617SAndrey V. Elsukov * ipsec_getpcbpolicy() function - SP will not be freed in the 395fcf59617SAndrey V. Elsukov * time between we take SP pointer from the cache and key_addref() 396fcf59617SAndrey V. Elsukov * call. 397fcf59617SAndrey V. Elsukov */ 398fcf59617SAndrey V. Elsukov key_addref(sp); 399fcf59617SAndrey V. Elsukov genid = key_getspgen(); 400fcf59617SAndrey V. Elsukov if (genid != inp->inp_sp->genid) { 401fcf59617SAndrey V. Elsukov ipsec_invalidate_cache(inp, dir); 402fcf59617SAndrey V. Elsukov inp->inp_sp->genid = genid; 40388768458SSam Leffler } 404fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_STAMP, 405fcf59617SAndrey V. Elsukov printf("%s: PCB(%p): cached %s SP(%p)\n", 406fcf59617SAndrey V. Elsukov __func__, inp, dir == IPSEC_DIR_OUTBOUND ? "OUTBOUND": 407fcf59617SAndrey V. Elsukov "INBOUND", sp)); 408fcf59617SAndrey V. Elsukov if (downgrade != 0) 409fcf59617SAndrey V. Elsukov INP_DOWNGRADE(inp); 410fcf59617SAndrey V. Elsukov } 411fcf59617SAndrey V. Elsukov 412fcf59617SAndrey V. Elsukov static struct secpolicy * 413fcf59617SAndrey V. Elsukov ipsec_checkpolicy(struct secpolicy *sp, struct inpcb *inp, int *error) 414fcf59617SAndrey V. Elsukov { 415fcf59617SAndrey V. Elsukov 416fcf59617SAndrey V. Elsukov /* Save found OUTBOUND policy into PCB SP cache. */ 417fcf59617SAndrey V. Elsukov if (inp != NULL && inp->inp_sp != NULL && inp->inp_sp->sp_out == NULL) 418fcf59617SAndrey V. Elsukov ipsec_cachepolicy(inp, sp, IPSEC_DIR_OUTBOUND); 419fcf59617SAndrey V. Elsukov 42088768458SSam Leffler switch (sp->policy) { 42188768458SSam Leffler default: 4229ffa9677SSam Leffler printf("%s: invalid policy %u\n", __func__, sp->policy); 423de47c390SBjoern A. Zeeb /* FALLTHROUGH */ 42488768458SSam Leffler case IPSEC_POLICY_DISCARD: 425de47c390SBjoern A. Zeeb *error = -EINVAL; /* Packet is discarded by caller. */ 426fcf59617SAndrey V. Elsukov /* FALLTHROUGH */ 42788768458SSam Leffler case IPSEC_POLICY_BYPASS: 42888768458SSam Leffler case IPSEC_POLICY_NONE: 429fcf59617SAndrey V. Elsukov key_freesp(&sp); 430de47c390SBjoern A. Zeeb sp = NULL; /* NB: force NULL result. */ 43188768458SSam Leffler break; 43288768458SSam Leffler case IPSEC_POLICY_IPSEC: 433fcf59617SAndrey V. Elsukov /* XXXAE: handle LARVAL SP */ 43488768458SSam Leffler break; 43588768458SSam Leffler } 436fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DUMP, 437fcf59617SAndrey V. Elsukov printf("%s: get SP(%p), error %d\n", __func__, sp, *error)); 438de47c390SBjoern A. Zeeb return (sp); 43988768458SSam Leffler } 44088768458SSam Leffler 441fcf59617SAndrey V. Elsukov static struct secpolicy * 442fcf59617SAndrey V. Elsukov ipsec_getpcbpolicy(struct inpcb *inp, u_int dir) 44388768458SSam Leffler { 444fcf59617SAndrey V. Elsukov struct secpolicy *sp; 445fcf59617SAndrey V. Elsukov int flags, downgrade; 44688768458SSam Leffler 447fcf59617SAndrey V. Elsukov if (inp == NULL || inp->inp_sp == NULL) 448fcf59617SAndrey V. Elsukov return (NULL); 44988768458SSam Leffler 450fcf59617SAndrey V. Elsukov INP_LOCK_ASSERT(inp); 451fcf59617SAndrey V. Elsukov 452fcf59617SAndrey V. Elsukov flags = inp->inp_sp->flags; 453fcf59617SAndrey V. Elsukov if (dir == IPSEC_DIR_OUTBOUND) { 454fcf59617SAndrey V. Elsukov sp = inp->inp_sp->sp_out; 455fcf59617SAndrey V. Elsukov flags &= INP_OUTBOUND_POLICY; 45688768458SSam Leffler } else { 457fcf59617SAndrey V. Elsukov sp = inp->inp_sp->sp_in; 458fcf59617SAndrey V. Elsukov flags &= INP_INBOUND_POLICY; 45988768458SSam Leffler } 46088768458SSam Leffler /* 461fcf59617SAndrey V. Elsukov * Check flags. If we have PCB SP, just return it. 462fcf59617SAndrey V. Elsukov * Otherwise we need to check that cached SP entry isn't stale. 46388768458SSam Leffler */ 464fcf59617SAndrey V. Elsukov if (flags == 0) { 465fcf59617SAndrey V. Elsukov if (sp == NULL) 466fcf59617SAndrey V. Elsukov return (NULL); 467fcf59617SAndrey V. Elsukov if (inp->inp_sp->genid != key_getspgen()) { 468fcf59617SAndrey V. Elsukov /* Invalidate the cache. */ 469fcf59617SAndrey V. Elsukov downgrade = 0; 470fcf59617SAndrey V. Elsukov if (!INP_WLOCKED(inp)) { 471fcf59617SAndrey V. Elsukov if ((downgrade = INP_TRY_UPGRADE(inp)) == 0) 472fcf59617SAndrey V. Elsukov return (NULL); 473fcf59617SAndrey V. Elsukov } 474fcf59617SAndrey V. Elsukov ipsec_invalidate_cache(inp, IPSEC_DIR_OUTBOUND); 475fcf59617SAndrey V. Elsukov ipsec_invalidate_cache(inp, IPSEC_DIR_INBOUND); 476fcf59617SAndrey V. Elsukov if (downgrade != 0) 477fcf59617SAndrey V. Elsukov INP_DOWNGRADE(inp); 478fcf59617SAndrey V. Elsukov return (NULL); 479fcf59617SAndrey V. Elsukov } 480fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_STAMP, 481fcf59617SAndrey V. Elsukov printf("%s: PCB(%p): cache hit SP(%p)\n", 482fcf59617SAndrey V. Elsukov __func__, inp, sp)); 483fcf59617SAndrey V. Elsukov /* Return referenced cached policy */ 484fcf59617SAndrey V. Elsukov } 485fcf59617SAndrey V. Elsukov key_addref(sp); 486fcf59617SAndrey V. Elsukov return (sp); 48788768458SSam Leffler } 48888768458SSam Leffler 489fcf59617SAndrey V. Elsukov #ifdef INET 49088768458SSam Leffler static void 491efb10c3cSAndrey V. Elsukov ipsec4_get_ulp(const struct mbuf *m, struct secpolicyindex *spidx, 492efb10c3cSAndrey V. Elsukov int needport) 49388768458SSam Leffler { 494fcf59617SAndrey V. Elsukov uint8_t nxt; 49588768458SSam Leffler int off; 49688768458SSam Leffler 497de47c390SBjoern A. Zeeb /* Sanity check. */ 498fcf59617SAndrey V. Elsukov IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip), 499fcf59617SAndrey V. Elsukov ("packet too short")); 50088768458SSam Leffler 50187a25418SErmal Luçi if (m->m_len >= sizeof (struct ip)) { 502efb10c3cSAndrey V. Elsukov const struct ip *ip = mtod(m, const struct ip *); 5038f134647SGleb Smirnoff if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) 50488768458SSam Leffler goto done; 50588768458SSam Leffler off = ip->ip_hl << 2; 50688768458SSam Leffler nxt = ip->ip_p; 50788768458SSam Leffler } else { 50888768458SSam Leffler struct ip ih; 50988768458SSam Leffler 51088768458SSam Leffler m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih); 5118f134647SGleb Smirnoff if (ih.ip_off & htons(IP_MF | IP_OFFMASK)) 51288768458SSam Leffler goto done; 51388768458SSam Leffler off = ih.ip_hl << 2; 51488768458SSam Leffler nxt = ih.ip_p; 51588768458SSam Leffler } 51688768458SSam Leffler 51788768458SSam Leffler while (off < m->m_pkthdr.len) { 51888768458SSam Leffler struct ip6_ext ip6e; 51988768458SSam Leffler struct tcphdr th; 52088768458SSam Leffler struct udphdr uh; 52188768458SSam Leffler 52288768458SSam Leffler switch (nxt) { 52388768458SSam Leffler case IPPROTO_TCP: 52488768458SSam Leffler spidx->ul_proto = nxt; 52588768458SSam Leffler if (!needport) 52688768458SSam Leffler goto done_proto; 52788768458SSam Leffler if (off + sizeof(struct tcphdr) > m->m_pkthdr.len) 52888768458SSam Leffler goto done; 52988768458SSam Leffler m_copydata(m, off, sizeof (th), (caddr_t) &th); 53088768458SSam Leffler spidx->src.sin.sin_port = th.th_sport; 53188768458SSam Leffler spidx->dst.sin.sin_port = th.th_dport; 53288768458SSam Leffler return; 53388768458SSam Leffler case IPPROTO_UDP: 53488768458SSam Leffler spidx->ul_proto = nxt; 53588768458SSam Leffler if (!needport) 53688768458SSam Leffler goto done_proto; 53788768458SSam Leffler if (off + sizeof(struct udphdr) > m->m_pkthdr.len) 53888768458SSam Leffler goto done; 53988768458SSam Leffler m_copydata(m, off, sizeof (uh), (caddr_t) &uh); 54088768458SSam Leffler spidx->src.sin.sin_port = uh.uh_sport; 54188768458SSam Leffler spidx->dst.sin.sin_port = uh.uh_dport; 54288768458SSam Leffler return; 54388768458SSam Leffler case IPPROTO_AH: 544afa3570dSSam Leffler if (off + sizeof(ip6e) > m->m_pkthdr.len) 54588768458SSam Leffler goto done; 546de47c390SBjoern A. Zeeb /* XXX Sigh, this works but is totally bogus. */ 54788768458SSam Leffler m_copydata(m, off, sizeof(ip6e), (caddr_t) &ip6e); 54888768458SSam Leffler off += (ip6e.ip6e_len + 2) << 2; 54988768458SSam Leffler nxt = ip6e.ip6e_nxt; 55088768458SSam Leffler break; 55188768458SSam Leffler case IPPROTO_ICMP: 55288768458SSam Leffler default: 553de47c390SBjoern A. Zeeb /* XXX Intermediate headers??? */ 55488768458SSam Leffler spidx->ul_proto = nxt; 55588768458SSam Leffler goto done_proto; 55688768458SSam Leffler } 55788768458SSam Leffler } 55888768458SSam Leffler done: 55988768458SSam Leffler spidx->ul_proto = IPSEC_ULPROTO_ANY; 56088768458SSam Leffler done_proto: 56188768458SSam Leffler spidx->src.sin.sin_port = IPSEC_PORT_ANY; 56288768458SSam Leffler spidx->dst.sin.sin_port = IPSEC_PORT_ANY; 563fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DUMP, 564fcf59617SAndrey V. Elsukov printf("%s: ", __func__); kdebug_secpolicyindex(spidx, NULL)); 56588768458SSam Leffler } 56688768458SSam Leffler 567fcf59617SAndrey V. Elsukov static void 568efb10c3cSAndrey V. Elsukov ipsec4_setspidx_ipaddr(const struct mbuf *m, struct secpolicyindex *spidx) 56988768458SSam Leffler { 57088768458SSam Leffler 571fcf59617SAndrey V. Elsukov ipsec4_setsockaddrs(m, &spidx->src, &spidx->dst); 57288768458SSam Leffler spidx->prefs = sizeof(struct in_addr) << 3; 57388768458SSam Leffler spidx->prefd = sizeof(struct in_addr) << 3; 57488768458SSam Leffler } 57588768458SSam Leffler 576fcf59617SAndrey V. Elsukov static struct secpolicy * 57722bbefb2SAndrey V. Elsukov ipsec4_getpolicy(const struct mbuf *m, struct inpcb *inp, u_int dir, 57822bbefb2SAndrey V. Elsukov int needport) 579fcf59617SAndrey V. Elsukov { 580fcf59617SAndrey V. Elsukov struct secpolicyindex spidx; 581fcf59617SAndrey V. Elsukov struct secpolicy *sp; 582fcf59617SAndrey V. Elsukov 583fcf59617SAndrey V. Elsukov sp = ipsec_getpcbpolicy(inp, dir); 584fcf59617SAndrey V. Elsukov if (sp == NULL && key_havesp(dir)) { 585fcf59617SAndrey V. Elsukov /* Make an index to look for a policy. */ 586fcf59617SAndrey V. Elsukov ipsec4_setspidx_ipaddr(m, &spidx); 58722bbefb2SAndrey V. Elsukov ipsec4_get_ulp(m, &spidx, needport); 588fcf59617SAndrey V. Elsukov spidx.dir = dir; 589fcf59617SAndrey V. Elsukov sp = key_allocsp(&spidx, dir); 590fcf59617SAndrey V. Elsukov } 591fcf59617SAndrey V. Elsukov if (sp == NULL) /* No SP found, use system default. */ 592fcf59617SAndrey V. Elsukov sp = key_allocsp_default(); 593fcf59617SAndrey V. Elsukov return (sp); 594fcf59617SAndrey V. Elsukov } 595fcf59617SAndrey V. Elsukov 596fcf59617SAndrey V. Elsukov /* 597fcf59617SAndrey V. Elsukov * Check security policy for *OUTBOUND* IPv4 packet. 598fcf59617SAndrey V. Elsukov */ 599fcf59617SAndrey V. Elsukov struct secpolicy * 60022bbefb2SAndrey V. Elsukov ipsec4_checkpolicy(const struct mbuf *m, struct inpcb *inp, int *error, 60122bbefb2SAndrey V. Elsukov int needport) 602fcf59617SAndrey V. Elsukov { 603fcf59617SAndrey V. Elsukov struct secpolicy *sp; 604fcf59617SAndrey V. Elsukov 605fcf59617SAndrey V. Elsukov *error = 0; 60622bbefb2SAndrey V. Elsukov sp = ipsec4_getpolicy(m, inp, IPSEC_DIR_OUTBOUND, needport); 607fcf59617SAndrey V. Elsukov if (sp != NULL) 608fcf59617SAndrey V. Elsukov sp = ipsec_checkpolicy(sp, inp, error); 609fcf59617SAndrey V. Elsukov if (sp == NULL) { 610fcf59617SAndrey V. Elsukov switch (*error) { 611fcf59617SAndrey V. Elsukov case 0: /* No IPsec required: BYPASS or NONE */ 612fcf59617SAndrey V. Elsukov break; 613fcf59617SAndrey V. Elsukov case -EINVAL: 614fcf59617SAndrey V. Elsukov IPSECSTAT_INC(ips_out_polvio); 615fcf59617SAndrey V. Elsukov break; 616fcf59617SAndrey V. Elsukov default: 617fcf59617SAndrey V. Elsukov IPSECSTAT_INC(ips_out_inval); 618fcf59617SAndrey V. Elsukov } 619fcf59617SAndrey V. Elsukov } 620fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_STAMP, 621fcf59617SAndrey V. Elsukov printf("%s: using SP(%p), error %d\n", __func__, sp, *error)); 622fcf59617SAndrey V. Elsukov if (sp != NULL) 623fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp)); 624fcf59617SAndrey V. Elsukov return (sp); 625fcf59617SAndrey V. Elsukov } 626fcf59617SAndrey V. Elsukov 627fcf59617SAndrey V. Elsukov /* 628fcf59617SAndrey V. Elsukov * Check IPv4 packet against *INBOUND* security policy. 629fcf59617SAndrey V. Elsukov * This function is called from tcp_input(), udp_input(), 630fcf59617SAndrey V. Elsukov * rip_input() and sctp_input(). 631fcf59617SAndrey V. Elsukov */ 632fcf59617SAndrey V. Elsukov int 633fcf59617SAndrey V. Elsukov ipsec4_in_reject(const struct mbuf *m, struct inpcb *inp) 634fcf59617SAndrey V. Elsukov { 635fcf59617SAndrey V. Elsukov struct secpolicy *sp; 636fcf59617SAndrey V. Elsukov int result; 637fcf59617SAndrey V. Elsukov 63822bbefb2SAndrey V. Elsukov sp = ipsec4_getpolicy(m, inp, IPSEC_DIR_INBOUND, 0); 639fcf59617SAndrey V. Elsukov result = ipsec_in_reject(sp, inp, m); 640fcf59617SAndrey V. Elsukov key_freesp(&sp); 641fcf59617SAndrey V. Elsukov if (result != 0) 642fcf59617SAndrey V. Elsukov IPSECSTAT_INC(ips_in_polvio); 643fcf59617SAndrey V. Elsukov return (result); 644fcf59617SAndrey V. Elsukov } 645fcf59617SAndrey V. Elsukov 646fcf59617SAndrey V. Elsukov /* 647fcf59617SAndrey V. Elsukov * IPSEC_CAP() method implementation for IPv4. 648fcf59617SAndrey V. Elsukov */ 649fcf59617SAndrey V. Elsukov int 650fcf59617SAndrey V. Elsukov ipsec4_capability(struct mbuf *m, u_int cap) 651fcf59617SAndrey V. Elsukov { 652fcf59617SAndrey V. Elsukov 653fcf59617SAndrey V. Elsukov switch (cap) { 654fcf59617SAndrey V. Elsukov case IPSEC_CAP_BYPASS_FILTER: 655fcf59617SAndrey V. Elsukov /* 656fcf59617SAndrey V. Elsukov * Bypass packet filtering for packets previously handled 657fcf59617SAndrey V. Elsukov * by IPsec. 658fcf59617SAndrey V. Elsukov */ 659fcf59617SAndrey V. Elsukov if (!V_ip4_filtertunnel && 660fcf59617SAndrey V. Elsukov m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL) 661fcf59617SAndrey V. Elsukov return (1); 662fcf59617SAndrey V. Elsukov return (0); 663fcf59617SAndrey V. Elsukov case IPSEC_CAP_OPERABLE: 664fcf59617SAndrey V. Elsukov /* Do we have active security policies? */ 665fcf59617SAndrey V. Elsukov if (key_havesp(IPSEC_DIR_INBOUND) != 0 || 666fcf59617SAndrey V. Elsukov key_havesp(IPSEC_DIR_OUTBOUND) != 0) 667fcf59617SAndrey V. Elsukov return (1); 668fcf59617SAndrey V. Elsukov return (0); 669fcf59617SAndrey V. Elsukov }; 670fcf59617SAndrey V. Elsukov return (EOPNOTSUPP); 671fcf59617SAndrey V. Elsukov } 672fcf59617SAndrey V. Elsukov 673fcf59617SAndrey V. Elsukov #endif /* INET */ 674fcf59617SAndrey V. Elsukov 67588768458SSam Leffler #ifdef INET6 67688768458SSam Leffler static void 677efb10c3cSAndrey V. Elsukov ipsec6_get_ulp(const struct mbuf *m, struct secpolicyindex *spidx, 678efb10c3cSAndrey V. Elsukov int needport) 67988768458SSam Leffler { 68088768458SSam Leffler struct tcphdr th; 68188768458SSam Leffler struct udphdr uh; 6820e3c2be4SBjoern A. Zeeb struct icmp6_hdr ih; 683fcf59617SAndrey V. Elsukov int off, nxt; 68488768458SSam Leffler 685fcf59617SAndrey V. Elsukov IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip6_hdr), 686fcf59617SAndrey V. Elsukov ("packet too short")); 68788768458SSam Leffler 688de47c390SBjoern A. Zeeb /* Set default. */ 68988768458SSam Leffler spidx->ul_proto = IPSEC_ULPROTO_ANY; 690fcf59617SAndrey V. Elsukov spidx->src.sin6.sin6_port = IPSEC_PORT_ANY; 691fcf59617SAndrey V. Elsukov spidx->dst.sin6.sin6_port = IPSEC_PORT_ANY; 69288768458SSam Leffler 69388768458SSam Leffler nxt = -1; 69488768458SSam Leffler off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 69588768458SSam Leffler if (off < 0 || m->m_pkthdr.len < off) 69688768458SSam Leffler return; 69788768458SSam Leffler 69888768458SSam Leffler switch (nxt) { 69988768458SSam Leffler case IPPROTO_TCP: 70088768458SSam Leffler spidx->ul_proto = nxt; 70188768458SSam Leffler if (!needport) 70288768458SSam Leffler break; 70388768458SSam Leffler if (off + sizeof(struct tcphdr) > m->m_pkthdr.len) 70488768458SSam Leffler break; 70588768458SSam Leffler m_copydata(m, off, sizeof(th), (caddr_t)&th); 706fcf59617SAndrey V. Elsukov spidx->src.sin6.sin6_port = th.th_sport; 707fcf59617SAndrey V. Elsukov spidx->dst.sin6.sin6_port = th.th_dport; 70888768458SSam Leffler break; 70988768458SSam Leffler case IPPROTO_UDP: 71088768458SSam Leffler spidx->ul_proto = nxt; 71188768458SSam Leffler if (!needport) 71288768458SSam Leffler break; 71388768458SSam Leffler if (off + sizeof(struct udphdr) > m->m_pkthdr.len) 71488768458SSam Leffler break; 71588768458SSam Leffler m_copydata(m, off, sizeof(uh), (caddr_t)&uh); 716fcf59617SAndrey V. Elsukov spidx->src.sin6.sin6_port = uh.uh_sport; 717fcf59617SAndrey V. Elsukov spidx->dst.sin6.sin6_port = uh.uh_dport; 71888768458SSam Leffler break; 71988768458SSam Leffler case IPPROTO_ICMPV6: 7200e3c2be4SBjoern A. Zeeb spidx->ul_proto = nxt; 7210e3c2be4SBjoern A. Zeeb if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len) 7220e3c2be4SBjoern A. Zeeb break; 7230e3c2be4SBjoern A. Zeeb m_copydata(m, off, sizeof(ih), (caddr_t)&ih); 724fcf59617SAndrey V. Elsukov spidx->src.sin6.sin6_port = htons((uint16_t)ih.icmp6_type); 725fcf59617SAndrey V. Elsukov spidx->dst.sin6.sin6_port = htons((uint16_t)ih.icmp6_code); 7260e3c2be4SBjoern A. Zeeb break; 72788768458SSam Leffler default: 728de47c390SBjoern A. Zeeb /* XXX Intermediate headers??? */ 72988768458SSam Leffler spidx->ul_proto = nxt; 73088768458SSam Leffler break; 73188768458SSam Leffler } 732fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DUMP, 733fcf59617SAndrey V. Elsukov printf("%s: ", __func__); kdebug_secpolicyindex(spidx, NULL)); 73488768458SSam Leffler } 73588768458SSam Leffler 736fcf59617SAndrey V. Elsukov static void 737efb10c3cSAndrey V. Elsukov ipsec6_setspidx_ipaddr(const struct mbuf *m, struct secpolicyindex *spidx) 73888768458SSam Leffler { 73988768458SSam Leffler 740fcf59617SAndrey V. Elsukov ipsec6_setsockaddrs(m, &spidx->src, &spidx->dst); 74188768458SSam Leffler spidx->prefs = sizeof(struct in6_addr) << 3; 74288768458SSam Leffler spidx->prefd = sizeof(struct in6_addr) << 3; 74388768458SSam Leffler } 744fcf59617SAndrey V. Elsukov 745fcf59617SAndrey V. Elsukov static struct secpolicy * 74622bbefb2SAndrey V. Elsukov ipsec6_getpolicy(const struct mbuf *m, struct inpcb *inp, u_int dir, 74722bbefb2SAndrey V. Elsukov int needport) 748fcf59617SAndrey V. Elsukov { 749fcf59617SAndrey V. Elsukov struct secpolicyindex spidx; 750fcf59617SAndrey V. Elsukov struct secpolicy *sp; 751fcf59617SAndrey V. Elsukov 752fcf59617SAndrey V. Elsukov sp = ipsec_getpcbpolicy(inp, dir); 753fcf59617SAndrey V. Elsukov if (sp == NULL && key_havesp(dir)) { 754fcf59617SAndrey V. Elsukov /* Make an index to look for a policy. */ 755fcf59617SAndrey V. Elsukov ipsec6_setspidx_ipaddr(m, &spidx); 75622bbefb2SAndrey V. Elsukov ipsec6_get_ulp(m, &spidx, needport); 757fcf59617SAndrey V. Elsukov spidx.dir = dir; 758fcf59617SAndrey V. Elsukov sp = key_allocsp(&spidx, dir); 759fcf59617SAndrey V. Elsukov } 760fcf59617SAndrey V. Elsukov if (sp == NULL) /* No SP found, use system default. */ 761fcf59617SAndrey V. Elsukov sp = key_allocsp_default(); 762fcf59617SAndrey V. Elsukov return (sp); 763fcf59617SAndrey V. Elsukov } 764fcf59617SAndrey V. Elsukov 765fcf59617SAndrey V. Elsukov /* 766fcf59617SAndrey V. Elsukov * Check security policy for *OUTBOUND* IPv6 packet. 767fcf59617SAndrey V. Elsukov */ 768fcf59617SAndrey V. Elsukov struct secpolicy * 76922bbefb2SAndrey V. Elsukov ipsec6_checkpolicy(const struct mbuf *m, struct inpcb *inp, int *error, 77022bbefb2SAndrey V. Elsukov int needport) 771fcf59617SAndrey V. Elsukov { 772fcf59617SAndrey V. Elsukov struct secpolicy *sp; 773fcf59617SAndrey V. Elsukov 774fcf59617SAndrey V. Elsukov *error = 0; 77522bbefb2SAndrey V. Elsukov sp = ipsec6_getpolicy(m, inp, IPSEC_DIR_OUTBOUND, needport); 776fcf59617SAndrey V. Elsukov if (sp != NULL) 777fcf59617SAndrey V. Elsukov sp = ipsec_checkpolicy(sp, inp, error); 778fcf59617SAndrey V. Elsukov if (sp == NULL) { 779fcf59617SAndrey V. Elsukov switch (*error) { 780fcf59617SAndrey V. Elsukov case 0: /* No IPsec required: BYPASS or NONE */ 781fcf59617SAndrey V. Elsukov break; 782fcf59617SAndrey V. Elsukov case -EINVAL: 783fcf59617SAndrey V. Elsukov IPSEC6STAT_INC(ips_out_polvio); 784fcf59617SAndrey V. Elsukov break; 785fcf59617SAndrey V. Elsukov default: 786fcf59617SAndrey V. Elsukov IPSEC6STAT_INC(ips_out_inval); 787fcf59617SAndrey V. Elsukov } 788fcf59617SAndrey V. Elsukov } 789fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_STAMP, 790fcf59617SAndrey V. Elsukov printf("%s: using SP(%p), error %d\n", __func__, sp, *error)); 791fcf59617SAndrey V. Elsukov if (sp != NULL) 792fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp)); 793fcf59617SAndrey V. Elsukov return (sp); 794fcf59617SAndrey V. Elsukov } 795fcf59617SAndrey V. Elsukov 796fcf59617SAndrey V. Elsukov /* 797fcf59617SAndrey V. Elsukov * Check IPv6 packet against inbound security policy. 798fcf59617SAndrey V. Elsukov * This function is called from tcp6_input(), udp6_input(), 799fcf59617SAndrey V. Elsukov * rip6_input() and sctp_input(). 800fcf59617SAndrey V. Elsukov */ 801fcf59617SAndrey V. Elsukov int 802fcf59617SAndrey V. Elsukov ipsec6_in_reject(const struct mbuf *m, struct inpcb *inp) 803fcf59617SAndrey V. Elsukov { 804fcf59617SAndrey V. Elsukov struct secpolicy *sp; 805fcf59617SAndrey V. Elsukov int result; 806fcf59617SAndrey V. Elsukov 80722bbefb2SAndrey V. Elsukov sp = ipsec6_getpolicy(m, inp, IPSEC_DIR_INBOUND, 0); 808fcf59617SAndrey V. Elsukov result = ipsec_in_reject(sp, inp, m); 809fcf59617SAndrey V. Elsukov key_freesp(&sp); 810fcf59617SAndrey V. Elsukov if (result) 811fcf59617SAndrey V. Elsukov IPSEC6STAT_INC(ips_in_polvio); 812fcf59617SAndrey V. Elsukov return (result); 813fcf59617SAndrey V. Elsukov } 814fcf59617SAndrey V. Elsukov 815fcf59617SAndrey V. Elsukov /* 816fcf59617SAndrey V. Elsukov * IPSEC_CAP() method implementation for IPv6. 817fcf59617SAndrey V. Elsukov */ 818fcf59617SAndrey V. Elsukov int 819fcf59617SAndrey V. Elsukov ipsec6_capability(struct mbuf *m, u_int cap) 820fcf59617SAndrey V. Elsukov { 821fcf59617SAndrey V. Elsukov 822fcf59617SAndrey V. Elsukov switch (cap) { 823fcf59617SAndrey V. Elsukov case IPSEC_CAP_BYPASS_FILTER: 824fcf59617SAndrey V. Elsukov /* 825fcf59617SAndrey V. Elsukov * Bypass packet filtering for packets previously handled 826fcf59617SAndrey V. Elsukov * by IPsec. 827fcf59617SAndrey V. Elsukov */ 828fcf59617SAndrey V. Elsukov if (!V_ip6_filtertunnel && 829fcf59617SAndrey V. Elsukov m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL) 830fcf59617SAndrey V. Elsukov return (1); 831fcf59617SAndrey V. Elsukov return (0); 832fcf59617SAndrey V. Elsukov case IPSEC_CAP_OPERABLE: 833fcf59617SAndrey V. Elsukov /* Do we have active security policies? */ 834fcf59617SAndrey V. Elsukov if (key_havesp(IPSEC_DIR_INBOUND) != 0 || 835fcf59617SAndrey V. Elsukov key_havesp(IPSEC_DIR_OUTBOUND) != 0) 836fcf59617SAndrey V. Elsukov return (1); 837fcf59617SAndrey V. Elsukov return (0); 838fcf59617SAndrey V. Elsukov }; 839fcf59617SAndrey V. Elsukov return (EOPNOTSUPP); 840fcf59617SAndrey V. Elsukov } 841fcf59617SAndrey V. Elsukov #endif /* INET6 */ 84288768458SSam Leffler 843ef91a976SAndrey V. Elsukov int 844ef91a976SAndrey V. Elsukov ipsec_run_hhooks(struct ipsec_ctx_data *ctx, int type) 845ef91a976SAndrey V. Elsukov { 846ef91a976SAndrey V. Elsukov int idx; 847ef91a976SAndrey V. Elsukov 848ef91a976SAndrey V. Elsukov switch (ctx->af) { 849ef91a976SAndrey V. Elsukov #ifdef INET 850ef91a976SAndrey V. Elsukov case AF_INET: 851ef91a976SAndrey V. Elsukov idx = HHOOK_IPSEC_INET; 852ef91a976SAndrey V. Elsukov break; 853ef91a976SAndrey V. Elsukov #endif 854ef91a976SAndrey V. Elsukov #ifdef INET6 855ef91a976SAndrey V. Elsukov case AF_INET6: 856ef91a976SAndrey V. Elsukov idx = HHOOK_IPSEC_INET6; 857ef91a976SAndrey V. Elsukov break; 858ef91a976SAndrey V. Elsukov #endif 859ef91a976SAndrey V. Elsukov default: 860ef91a976SAndrey V. Elsukov return (EPFNOSUPPORT); 861ef91a976SAndrey V. Elsukov } 862ef91a976SAndrey V. Elsukov if (type == HHOOK_TYPE_IPSEC_IN) 863ef91a976SAndrey V. Elsukov HHOOKS_RUN_IF(V_ipsec_hhh_in[idx], ctx, NULL); 864ef91a976SAndrey V. Elsukov else 865ef91a976SAndrey V. Elsukov HHOOKS_RUN_IF(V_ipsec_hhh_out[idx], ctx, NULL); 866ef91a976SAndrey V. Elsukov if (*ctx->mp == NULL) 867ef91a976SAndrey V. Elsukov return (EACCES); 868ef91a976SAndrey V. Elsukov return (0); 869ef91a976SAndrey V. Elsukov } 870ef91a976SAndrey V. Elsukov 87188768458SSam Leffler /* 872de47c390SBjoern A. Zeeb * Return current level. 87388768458SSam Leffler * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned. 87488768458SSam Leffler */ 87588768458SSam Leffler u_int 876fcf59617SAndrey V. Elsukov ipsec_get_reqlevel(struct secpolicy *sp, u_int idx) 87788768458SSam Leffler { 878fcf59617SAndrey V. Elsukov struct ipsecrequest *isr; 87988768458SSam Leffler u_int esp_trans_deflev, esp_net_deflev; 88088768458SSam Leffler u_int ah_trans_deflev, ah_net_deflev; 881fcf59617SAndrey V. Elsukov u_int level = 0; 88288768458SSam Leffler 883fcf59617SAndrey V. Elsukov IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx)); 884de47c390SBjoern A. Zeeb /* XXX Note that we have ipseclog() expanded here - code sync issue. */ 88588768458SSam Leffler #define IPSEC_CHECK_DEFAULT(lev) \ 886fcf59617SAndrey V. Elsukov (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE && \ 887fcf59617SAndrey V. Elsukov (lev) != IPSEC_LEVEL_UNIQUE) \ 888fcf59617SAndrey V. Elsukov ? (V_ipsec_debug ? \ 889fcf59617SAndrey V. Elsukov log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\ 890fcf59617SAndrey V. Elsukov (lev), IPSEC_LEVEL_REQUIRE) : 0), \ 891fcf59617SAndrey V. Elsukov (lev) = IPSEC_LEVEL_REQUIRE, (lev) : (lev)) 892fcf59617SAndrey V. Elsukov 893fcf59617SAndrey V. Elsukov /* 894fcf59617SAndrey V. Elsukov * IPsec VTI uses unique security policy with fake spidx filled 895fcf59617SAndrey V. Elsukov * with zeroes. Just return IPSEC_LEVEL_REQUIRE instead of doing 896fcf59617SAndrey V. Elsukov * full level lookup for such policies. 897fcf59617SAndrey V. Elsukov */ 898fcf59617SAndrey V. Elsukov if (sp->state == IPSEC_SPSTATE_IFNET) { 899fcf59617SAndrey V. Elsukov IPSEC_ASSERT(sp->req[idx]->level == IPSEC_LEVEL_UNIQUE, 900fcf59617SAndrey V. Elsukov ("Wrong IPsec request level %d", sp->req[idx]->level)); 901fcf59617SAndrey V. Elsukov return (IPSEC_LEVEL_REQUIRE); 902fcf59617SAndrey V. Elsukov } 90388768458SSam Leffler 904de47c390SBjoern A. Zeeb /* Set default level. */ 905fcf59617SAndrey V. Elsukov switch (sp->spidx.src.sa.sa_family) { 90688768458SSam Leffler #ifdef INET 90788768458SSam Leffler case AF_INET: 908603724d3SBjoern A. Zeeb esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_trans_deflev); 909603724d3SBjoern A. Zeeb esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_net_deflev); 910603724d3SBjoern A. Zeeb ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_trans_deflev); 911603724d3SBjoern A. Zeeb ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_net_deflev); 91288768458SSam Leffler break; 91388768458SSam Leffler #endif 91488768458SSam Leffler #ifdef INET6 91588768458SSam Leffler case AF_INET6: 916603724d3SBjoern A. Zeeb esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_trans_deflev); 917603724d3SBjoern A. Zeeb esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_net_deflev); 918603724d3SBjoern A. Zeeb ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_trans_deflev); 919603724d3SBjoern A. Zeeb ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_net_deflev); 92088768458SSam Leffler break; 92188768458SSam Leffler #endif /* INET6 */ 92288768458SSam Leffler default: 9239ffa9677SSam Leffler panic("%s: unknown af %u", 924fcf59617SAndrey V. Elsukov __func__, sp->spidx.src.sa.sa_family); 92588768458SSam Leffler } 92688768458SSam Leffler 92788768458SSam Leffler #undef IPSEC_CHECK_DEFAULT 92888768458SSam Leffler 929fcf59617SAndrey V. Elsukov isr = sp->req[idx]; 930de47c390SBjoern A. Zeeb /* Set level. */ 93188768458SSam Leffler switch (isr->level) { 93288768458SSam Leffler case IPSEC_LEVEL_DEFAULT: 93388768458SSam Leffler switch (isr->saidx.proto) { 93488768458SSam Leffler case IPPROTO_ESP: 93588768458SSam Leffler if (isr->saidx.mode == IPSEC_MODE_TUNNEL) 93688768458SSam Leffler level = esp_net_deflev; 93788768458SSam Leffler else 93888768458SSam Leffler level = esp_trans_deflev; 93988768458SSam Leffler break; 94088768458SSam Leffler case IPPROTO_AH: 94188768458SSam Leffler if (isr->saidx.mode == IPSEC_MODE_TUNNEL) 94288768458SSam Leffler level = ah_net_deflev; 94388768458SSam Leffler else 94488768458SSam Leffler level = ah_trans_deflev; 9458381996eSSam Leffler break; 94688768458SSam Leffler case IPPROTO_IPCOMP: 94788768458SSam Leffler /* 948de47c390SBjoern A. Zeeb * We don't really care, as IPcomp document says that 949de47c390SBjoern A. Zeeb * we shouldn't compress small packets. 95088768458SSam Leffler */ 95188768458SSam Leffler level = IPSEC_LEVEL_USE; 95288768458SSam Leffler break; 95388768458SSam Leffler default: 9549ffa9677SSam Leffler panic("%s: Illegal protocol defined %u\n", __func__, 95588768458SSam Leffler isr->saidx.proto); 95688768458SSam Leffler } 95788768458SSam Leffler break; 95888768458SSam Leffler 95988768458SSam Leffler case IPSEC_LEVEL_USE: 96088768458SSam Leffler case IPSEC_LEVEL_REQUIRE: 96188768458SSam Leffler level = isr->level; 96288768458SSam Leffler break; 96388768458SSam Leffler case IPSEC_LEVEL_UNIQUE: 96488768458SSam Leffler level = IPSEC_LEVEL_REQUIRE; 96588768458SSam Leffler break; 96688768458SSam Leffler 96788768458SSam Leffler default: 9689ffa9677SSam Leffler panic("%s: Illegal IPsec level %u\n", __func__, isr->level); 96988768458SSam Leffler } 97088768458SSam Leffler 971de47c390SBjoern A. Zeeb return (level); 97288768458SSam Leffler } 97388768458SSam Leffler 974fcf59617SAndrey V. Elsukov static int 975fcf59617SAndrey V. Elsukov ipsec_check_history(const struct mbuf *m, struct secpolicy *sp, u_int idx) 976fcf59617SAndrey V. Elsukov { 977fcf59617SAndrey V. Elsukov struct xform_history *xh; 978fcf59617SAndrey V. Elsukov struct m_tag *mtag; 979fcf59617SAndrey V. Elsukov 980fcf59617SAndrey V. Elsukov mtag = NULL; 981fcf59617SAndrey V. Elsukov while ((mtag = m_tag_find(__DECONST(struct mbuf *, m), 982fcf59617SAndrey V. Elsukov PACKET_TAG_IPSEC_IN_DONE, mtag)) != NULL) { 983fcf59617SAndrey V. Elsukov xh = (struct xform_history *)(mtag + 1); 984fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DATA, 985fcf59617SAndrey V. Elsukov char buf[IPSEC_ADDRSTRLEN]; 986fcf59617SAndrey V. Elsukov printf("%s: mode %s proto %u dst %s\n", __func__, 987fcf59617SAndrey V. Elsukov kdebug_secasindex_mode(xh->mode), xh->proto, 988fcf59617SAndrey V. Elsukov ipsec_address(&xh->dst, buf, sizeof(buf)))); 989fcf59617SAndrey V. Elsukov if (xh->proto != sp->req[idx]->saidx.proto) 990fcf59617SAndrey V. Elsukov continue; 991fcf59617SAndrey V. Elsukov /* If SA had IPSEC_MODE_ANY, consider this as match. */ 992fcf59617SAndrey V. Elsukov if (xh->mode != sp->req[idx]->saidx.mode && 993fcf59617SAndrey V. Elsukov xh->mode != IPSEC_MODE_ANY) 994fcf59617SAndrey V. Elsukov continue; 995fcf59617SAndrey V. Elsukov /* 996fcf59617SAndrey V. Elsukov * For transport mode IPsec request doesn't contain 997fcf59617SAndrey V. Elsukov * addresses. We need to use address from spidx. 998fcf59617SAndrey V. Elsukov */ 999fcf59617SAndrey V. Elsukov if (sp->req[idx]->saidx.mode == IPSEC_MODE_TRANSPORT) { 1000fcf59617SAndrey V. Elsukov if (key_sockaddrcmp_withmask(&xh->dst.sa, 1001fcf59617SAndrey V. Elsukov &sp->spidx.dst.sa, sp->spidx.prefd) != 0) 1002fcf59617SAndrey V. Elsukov continue; 1003fcf59617SAndrey V. Elsukov } else { 1004fcf59617SAndrey V. Elsukov if (key_sockaddrcmp(&xh->dst.sa, 1005fcf59617SAndrey V. Elsukov &sp->req[idx]->saidx.dst.sa, 0) != 0) 1006fcf59617SAndrey V. Elsukov continue; 1007fcf59617SAndrey V. Elsukov } 1008fcf59617SAndrey V. Elsukov return (0); /* matched */ 1009fcf59617SAndrey V. Elsukov } 1010fcf59617SAndrey V. Elsukov return (1); 1011fcf59617SAndrey V. Elsukov } 1012fcf59617SAndrey V. Elsukov 101388768458SSam Leffler /* 101488768458SSam Leffler * Check security policy requirements against the actual 101588768458SSam Leffler * packet contents. Return one if the packet should be 101688768458SSam Leffler * reject as "invalid"; otherwiser return zero to have the 101788768458SSam Leffler * packet treated as "valid". 101888768458SSam Leffler * 101988768458SSam Leffler * OUT: 102088768458SSam Leffler * 0: valid 102188768458SSam Leffler * 1: invalid 102288768458SSam Leffler */ 1023a9b9f6b6SAndrey V. Elsukov static int 1024fcf59617SAndrey V. Elsukov ipsec_in_reject(struct secpolicy *sp, struct inpcb *inp, const struct mbuf *m) 102588768458SSam Leffler { 1026fcf59617SAndrey V. Elsukov int i; 102788768458SSam Leffler 1028fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_STAMP, 1029fcf59617SAndrey V. Elsukov printf("%s: PCB(%p): using SP(%p)\n", __func__, inp, sp)); 1030fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp)); 1031fcf59617SAndrey V. Elsukov 1032fcf59617SAndrey V. Elsukov if (inp != NULL && inp->inp_sp != NULL && inp->inp_sp->sp_in == NULL) 1033fcf59617SAndrey V. Elsukov ipsec_cachepolicy(inp, sp, IPSEC_DIR_INBOUND); 103488768458SSam Leffler 1035de47c390SBjoern A. Zeeb /* Check policy. */ 103688768458SSam Leffler switch (sp->policy) { 103788768458SSam Leffler case IPSEC_POLICY_DISCARD: 1038de47c390SBjoern A. Zeeb return (1); 103988768458SSam Leffler case IPSEC_POLICY_BYPASS: 104088768458SSam Leffler case IPSEC_POLICY_NONE: 1041de47c390SBjoern A. Zeeb return (0); 104288768458SSam Leffler } 104388768458SSam Leffler 10449ffa9677SSam Leffler IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC, 10459ffa9677SSam Leffler ("invalid policy %u", sp->policy)); 104688768458SSam Leffler 1047fcf59617SAndrey V. Elsukov /* 1048fcf59617SAndrey V. Elsukov * ipsec[46]_common_input_cb after each transform adds 1049fcf59617SAndrey V. Elsukov * PACKET_TAG_IPSEC_IN_DONE mbuf tag. It contains SPI, proto, mode 1050fcf59617SAndrey V. Elsukov * and destination address from saidx. We can compare info from 1051fcf59617SAndrey V. Elsukov * these tags with requirements in SP. 1052fcf59617SAndrey V. Elsukov */ 1053fcf59617SAndrey V. Elsukov for (i = 0; i < sp->tcount; i++) { 1054fcf59617SAndrey V. Elsukov /* 1055fcf59617SAndrey V. Elsukov * Do not check IPcomp, since IPcomp document 1056fcf59617SAndrey V. Elsukov * says that we shouldn't compress small packets. 1057fcf59617SAndrey V. Elsukov * IPComp policy should always be treated as being 1058fcf59617SAndrey V. Elsukov * in "use" level. 1059fcf59617SAndrey V. Elsukov */ 1060fcf59617SAndrey V. Elsukov if (sp->req[i]->saidx.proto == IPPROTO_IPCOMP || 1061fcf59617SAndrey V. Elsukov ipsec_get_reqlevel(sp, i) != IPSEC_LEVEL_REQUIRE) 106288768458SSam Leffler continue; 1063fcf59617SAndrey V. Elsukov if (V_check_policy_history != 0 && 1064fcf59617SAndrey V. Elsukov ipsec_check_history(m, sp, i) != 0) 1065fcf59617SAndrey V. Elsukov return (1); 1066fcf59617SAndrey V. Elsukov else switch (sp->req[i]->saidx.proto) { 106788768458SSam Leffler case IPPROTO_ESP: 106888768458SSam Leffler if ((m->m_flags & M_DECRYPTED) == 0) { 1069fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DUMP, 10709ffa9677SSam Leffler printf("%s: ESP m_flags:%x\n", __func__, 107188768458SSam Leffler m->m_flags)); 1072de47c390SBjoern A. Zeeb return (1); 107388768458SSam Leffler } 107488768458SSam Leffler break; 107588768458SSam Leffler case IPPROTO_AH: 107688768458SSam Leffler if ((m->m_flags & M_AUTHIPHDR) == 0) { 1077fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DUMP, 10789ffa9677SSam Leffler printf("%s: AH m_flags:%x\n", __func__, 107988768458SSam Leffler m->m_flags)); 1080de47c390SBjoern A. Zeeb return (1); 108188768458SSam Leffler } 108288768458SSam Leffler break; 108388768458SSam Leffler } 108488768458SSam Leffler } 1085de47c390SBjoern A. Zeeb return (0); /* Valid. */ 108688768458SSam Leffler } 108788768458SSam Leffler 1088a91150daSAndrey V. Elsukov /* 1089de47c390SBjoern A. Zeeb * Compute the byte size to be occupied by IPsec header. 1090de47c390SBjoern A. Zeeb * In case it is tunnelled, it includes the size of outer IP header. 109188768458SSam Leffler */ 109288768458SSam Leffler static size_t 109397aa4a51SBjoern A. Zeeb ipsec_hdrsiz_internal(struct secpolicy *sp) 109488768458SSam Leffler { 10951f8bd75eSBjoern A. Zeeb size_t size; 1096fcf59617SAndrey V. Elsukov int i; 109788768458SSam Leffler 1098fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_STAMP, printf("%s: using SP(%p)\n", __func__, sp)); 1099fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp)); 110088768458SSam Leffler 110188768458SSam Leffler switch (sp->policy) { 110288768458SSam Leffler case IPSEC_POLICY_DISCARD: 110388768458SSam Leffler case IPSEC_POLICY_BYPASS: 110488768458SSam Leffler case IPSEC_POLICY_NONE: 1105de47c390SBjoern A. Zeeb return (0); 110688768458SSam Leffler } 110788768458SSam Leffler 11089ffa9677SSam Leffler IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC, 11099ffa9677SSam Leffler ("invalid policy %u", sp->policy)); 111088768458SSam Leffler 1111fcf59617SAndrey V. Elsukov /* 1112fcf59617SAndrey V. Elsukov * XXX: for each transform we need to lookup suitable SA 1113fcf59617SAndrey V. Elsukov * and use info from SA to calculate headers size. 1114fcf59617SAndrey V. Elsukov * XXX: for NAT-T we need to cosider UDP header size. 1115fcf59617SAndrey V. Elsukov */ 11161f8bd75eSBjoern A. Zeeb size = 0; 1117fcf59617SAndrey V. Elsukov for (i = 0; i < sp->tcount; i++) { 1118fcf59617SAndrey V. Elsukov switch (sp->req[i]->saidx.proto) { 111988768458SSam Leffler case IPPROTO_ESP: 1120fcf59617SAndrey V. Elsukov size += esp_hdrsiz(NULL); 112188768458SSam Leffler break; 112288768458SSam Leffler case IPPROTO_AH: 1123fcf59617SAndrey V. Elsukov size += ah_hdrsiz(NULL); 112488768458SSam Leffler break; 112588768458SSam Leffler case IPPROTO_IPCOMP: 1126fcf59617SAndrey V. Elsukov size += sizeof(struct ipcomp); 112788768458SSam Leffler break; 112888768458SSam Leffler } 112988768458SSam Leffler 1130fcf59617SAndrey V. Elsukov if (sp->req[i]->saidx.mode == IPSEC_MODE_TUNNEL) { 1131fcf59617SAndrey V. Elsukov switch (sp->req[i]->saidx.dst.sa.sa_family) { 1132fcf59617SAndrey V. Elsukov #ifdef INET 113388768458SSam Leffler case AF_INET: 1134fcf59617SAndrey V. Elsukov size += sizeof(struct ip); 113588768458SSam Leffler break; 1136fcf59617SAndrey V. Elsukov #endif 113788768458SSam Leffler #ifdef INET6 113888768458SSam Leffler case AF_INET6: 1139fcf59617SAndrey V. Elsukov size += sizeof(struct ip6_hdr); 114088768458SSam Leffler break; 114188768458SSam Leffler #endif 114288768458SSam Leffler default: 11439ffa9677SSam Leffler ipseclog((LOG_ERR, "%s: unknown AF %d in " 11449ffa9677SSam Leffler "IPsec tunnel SA\n", __func__, 1145fcf59617SAndrey V. Elsukov sp->req[i]->saidx.dst.sa.sa_family)); 114688768458SSam Leffler break; 114788768458SSam Leffler } 114888768458SSam Leffler } 114988768458SSam Leffler } 11501f8bd75eSBjoern A. Zeeb return (size); 115188768458SSam Leffler } 115288768458SSam Leffler 115397aa4a51SBjoern A. Zeeb /* 1154fcf59617SAndrey V. Elsukov * Compute ESP/AH header size for protocols with PCB, including 1155fcf59617SAndrey V. Elsukov * outer IP header. Currently only tcp_output() uses it. 115697aa4a51SBjoern A. Zeeb */ 115788768458SSam Leffler size_t 1158fcf59617SAndrey V. Elsukov ipsec_hdrsiz_inpcb(struct inpcb *inp) 115988768458SSam Leffler { 1160fcf59617SAndrey V. Elsukov struct secpolicyindex spidx; 116188768458SSam Leffler struct secpolicy *sp; 1162fcf59617SAndrey V. Elsukov size_t sz; 116388768458SSam Leffler 1164fcf59617SAndrey V. Elsukov sp = ipsec_getpcbpolicy(inp, IPSEC_DIR_OUTBOUND); 1165fcf59617SAndrey V. Elsukov if (sp == NULL && key_havesp(IPSEC_DIR_OUTBOUND)) { 1166fcf59617SAndrey V. Elsukov ipsec_setspidx_inpcb(inp, &spidx, IPSEC_DIR_OUTBOUND); 1167fcf59617SAndrey V. Elsukov sp = key_allocsp(&spidx, IPSEC_DIR_OUTBOUND); 116888768458SSam Leffler } 1169fcf59617SAndrey V. Elsukov if (sp == NULL) 1170fcf59617SAndrey V. Elsukov sp = key_allocsp_default(); 1171fcf59617SAndrey V. Elsukov sz = ipsec_hdrsiz_internal(sp); 1172fcf59617SAndrey V. Elsukov key_freesp(&sp); 1173fcf59617SAndrey V. Elsukov return (sz); 117488768458SSam Leffler } 117588768458SSam Leffler 1176*8b7f3994SMarcin Wojtas 1177*8b7f3994SMarcin Wojtas #define IPSEC_BITMAP_INDEX_MASK(w) (w - 1) 1178*8b7f3994SMarcin Wojtas #define IPSEC_REDUNDANT_BIT_SHIFTS 5 1179*8b7f3994SMarcin Wojtas #define IPSEC_REDUNDANT_BITS (1 << IPSEC_REDUNDANT_BIT_SHIFTS) 1180*8b7f3994SMarcin Wojtas #define IPSEC_BITMAP_LOC_MASK (IPSEC_REDUNDANT_BITS - 1) 1181*8b7f3994SMarcin Wojtas 1182*8b7f3994SMarcin Wojtas /* 1183*8b7f3994SMarcin Wojtas * Functions below are responsible for checking and updating bitmap. 1184*8b7f3994SMarcin Wojtas * These are used to separate ipsec_chkreplay() and ipsec_updatereplay() 1185*8b7f3994SMarcin Wojtas * from window implementation 1186*8b7f3994SMarcin Wojtas * 1187*8b7f3994SMarcin Wojtas * Based on RFC 6479. Blocks are 32 bits unsigned integers 1188*8b7f3994SMarcin Wojtas */ 1189*8b7f3994SMarcin Wojtas 1190*8b7f3994SMarcin Wojtas static inline int 1191*8b7f3994SMarcin Wojtas check_window(const struct secreplay *replay, uint64_t seq) 1192*8b7f3994SMarcin Wojtas { 1193*8b7f3994SMarcin Wojtas int index, bit_location; 1194*8b7f3994SMarcin Wojtas 1195*8b7f3994SMarcin Wojtas bit_location = seq & IPSEC_BITMAP_LOC_MASK; 1196*8b7f3994SMarcin Wojtas index = (seq >> IPSEC_REDUNDANT_BIT_SHIFTS) 1197*8b7f3994SMarcin Wojtas & IPSEC_BITMAP_INDEX_MASK(replay->bitmap_size); 1198*8b7f3994SMarcin Wojtas 1199*8b7f3994SMarcin Wojtas /* This packet already seen? */ 1200*8b7f3994SMarcin Wojtas return ((replay->bitmap)[index] & (1 << bit_location)); 1201*8b7f3994SMarcin Wojtas } 1202*8b7f3994SMarcin Wojtas 1203*8b7f3994SMarcin Wojtas static inline void 1204*8b7f3994SMarcin Wojtas advance_window(const struct secreplay *replay, uint64_t seq) 1205*8b7f3994SMarcin Wojtas { 1206*8b7f3994SMarcin Wojtas int i; 1207*8b7f3994SMarcin Wojtas uint64_t index, index_cur, diff; 1208*8b7f3994SMarcin Wojtas 1209*8b7f3994SMarcin Wojtas index_cur = replay->last >> IPSEC_REDUNDANT_BIT_SHIFTS; 1210*8b7f3994SMarcin Wojtas index = seq >> IPSEC_REDUNDANT_BIT_SHIFTS; 1211*8b7f3994SMarcin Wojtas diff = index - index_cur; 1212*8b7f3994SMarcin Wojtas 1213*8b7f3994SMarcin Wojtas if (diff > replay->bitmap_size) { 1214*8b7f3994SMarcin Wojtas /* something unusual in this case */ 1215*8b7f3994SMarcin Wojtas diff = replay->bitmap_size; 1216*8b7f3994SMarcin Wojtas } 1217*8b7f3994SMarcin Wojtas 1218*8b7f3994SMarcin Wojtas for (i = 0; i < diff; i++) { 1219*8b7f3994SMarcin Wojtas replay->bitmap[(i + index_cur + 1) 1220*8b7f3994SMarcin Wojtas & IPSEC_BITMAP_INDEX_MASK(replay->bitmap_size)] = 0; 1221*8b7f3994SMarcin Wojtas } 1222*8b7f3994SMarcin Wojtas } 1223*8b7f3994SMarcin Wojtas 1224*8b7f3994SMarcin Wojtas static inline void 1225*8b7f3994SMarcin Wojtas set_window(const struct secreplay *replay, uint64_t seq) 1226*8b7f3994SMarcin Wojtas { 1227*8b7f3994SMarcin Wojtas int index, bit_location; 1228*8b7f3994SMarcin Wojtas 1229*8b7f3994SMarcin Wojtas bit_location = seq & IPSEC_BITMAP_LOC_MASK; 1230*8b7f3994SMarcin Wojtas index = (seq >> IPSEC_REDUNDANT_BIT_SHIFTS) 1231*8b7f3994SMarcin Wojtas & IPSEC_BITMAP_INDEX_MASK(replay->bitmap_size); 1232*8b7f3994SMarcin Wojtas 1233*8b7f3994SMarcin Wojtas replay->bitmap[index] |= (1 << bit_location); 1234*8b7f3994SMarcin Wojtas } 1235*8b7f3994SMarcin Wojtas 123688768458SSam Leffler /* 123788768458SSam Leffler * Check the variable replay window. 123888768458SSam Leffler * ipsec_chkreplay() performs replay check before ICV verification. 123988768458SSam Leffler * ipsec_updatereplay() updates replay bitmap. This must be called after 124088768458SSam Leffler * ICV verification (it also performs replay check, which is usually done 124188768458SSam Leffler * beforehand). 124288768458SSam Leffler * 0 (zero) is returned if packet disallowed, 1 if packet permitted. 124388768458SSam Leffler * 1244*8b7f3994SMarcin Wojtas * Based on RFC 4303 124588768458SSam Leffler */ 1246bf435626SFabien Thomas 124788768458SSam Leffler int 1248*8b7f3994SMarcin Wojtas ipsec_chkreplay(uint32_t seq, uint32_t *seqhigh, struct secasvar *sav) 124988768458SSam Leffler { 1250*8b7f3994SMarcin Wojtas char buf[128]; 1251*8b7f3994SMarcin Wojtas struct secreplay *replay; 1252*8b7f3994SMarcin Wojtas uint32_t window; 1253*8b7f3994SMarcin Wojtas uint32_t tl, th, bl; 1254*8b7f3994SMarcin Wojtas uint32_t seqh; 125588768458SSam Leffler 12569ffa9677SSam Leffler IPSEC_ASSERT(sav != NULL, ("Null SA")); 12579ffa9677SSam Leffler IPSEC_ASSERT(sav->replay != NULL, ("Null replay state")); 125888768458SSam Leffler 125988768458SSam Leffler replay = sav->replay; 126088768458SSam Leffler 1261bf435626SFabien Thomas /* No need to check replay if disabled. */ 126288768458SSam Leffler if (replay->wsize == 0) 1263fcf59617SAndrey V. Elsukov return (1); 126488768458SSam Leffler 1265*8b7f3994SMarcin Wojtas /* Zero sequence number is not allowed. */ 1266*8b7f3994SMarcin Wojtas if (seq == 0 && replay->last == 0) 1267fcf59617SAndrey V. Elsukov return (0); 126888768458SSam Leffler 1269*8b7f3994SMarcin Wojtas window = replay->wsize << 3; /* Size of window */ 1270*8b7f3994SMarcin Wojtas tl = (uint32_t)replay->last; /* Top of window, lower part */ 1271*8b7f3994SMarcin Wojtas th = (uint32_t)(replay->last >> 32); /* Top of window, high part */ 1272*8b7f3994SMarcin Wojtas bl = tl - window + 1; /* Bottom of window, lower part */ 127388768458SSam Leffler 1274*8b7f3994SMarcin Wojtas /* 1275*8b7f3994SMarcin Wojtas * We keep the high part intact when: 1276*8b7f3994SMarcin Wojtas * 1) the seq is within [bl, 0xffffffff] and the whole window is 1277*8b7f3994SMarcin Wojtas * within one subspace; 1278*8b7f3994SMarcin Wojtas * 2) the seq is within [0, bl) and window spans two subspaces. 1279bf435626SFabien Thomas */ 1280*8b7f3994SMarcin Wojtas if ((tl >= window - 1 && seq >= bl) || 1281*8b7f3994SMarcin Wojtas (tl < window - 1 && seq < bl)) { 1282*8b7f3994SMarcin Wojtas *seqhigh = th; 1283*8b7f3994SMarcin Wojtas if (seq <= tl) { 1284*8b7f3994SMarcin Wojtas /* Sequence number inside window - check against replay */ 1285*8b7f3994SMarcin Wojtas if (check_window(replay, seq)) 1286fcf59617SAndrey V. Elsukov return (0); 1287*8b7f3994SMarcin Wojtas } 1288*8b7f3994SMarcin Wojtas 1289*8b7f3994SMarcin Wojtas /* Sequence number above top of window or not found in bitmap */ 1290*8b7f3994SMarcin Wojtas return (1); 1291*8b7f3994SMarcin Wojtas } 1292*8b7f3994SMarcin Wojtas 1293*8b7f3994SMarcin Wojtas /* 1294*8b7f3994SMarcin Wojtas * If ESN is not enabled and packet with highest sequence number 1295*8b7f3994SMarcin Wojtas * was received we should report overflow 1296*8b7f3994SMarcin Wojtas */ 1297*8b7f3994SMarcin Wojtas if (tl == 0xffffffff && !(sav->flags & SADB_X_SAFLAGS_ESN)) { 1298*8b7f3994SMarcin Wojtas /* Set overflow flag. */ 1299*8b7f3994SMarcin Wojtas replay->overflow++; 1300*8b7f3994SMarcin Wojtas 1301*8b7f3994SMarcin Wojtas if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) { 1302*8b7f3994SMarcin Wojtas if (sav->sah->saidx.proto == IPPROTO_ESP) 1303*8b7f3994SMarcin Wojtas ESPSTAT_INC(esps_wrap); 1304*8b7f3994SMarcin Wojtas else if (sav->sah->saidx.proto == IPPROTO_AH) 1305*8b7f3994SMarcin Wojtas AHSTAT_INC(ahs_wrap); 1306*8b7f3994SMarcin Wojtas return (0); 1307*8b7f3994SMarcin Wojtas } 1308*8b7f3994SMarcin Wojtas 1309*8b7f3994SMarcin Wojtas ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n", 1310*8b7f3994SMarcin Wojtas __func__, replay->overflow, 1311*8b7f3994SMarcin Wojtas ipsec_sa2str(sav, buf, sizeof(buf)))); 1312*8b7f3994SMarcin Wojtas } 1313*8b7f3994SMarcin Wojtas 1314*8b7f3994SMarcin Wojtas /* 1315*8b7f3994SMarcin Wojtas * Seq is within [bl, 0xffffffff] and bl is within 1316*8b7f3994SMarcin Wojtas * [0xffffffff-window, 0xffffffff]. This means we got a seq 1317*8b7f3994SMarcin Wojtas * which is within our replay window, but in the previous 1318*8b7f3994SMarcin Wojtas * subspace. 1319*8b7f3994SMarcin Wojtas */ 1320*8b7f3994SMarcin Wojtas if (tl < window - 1 && seq >= bl) { 1321*8b7f3994SMarcin Wojtas if (th == 0) 1322*8b7f3994SMarcin Wojtas return (0); 1323*8b7f3994SMarcin Wojtas *seqhigh = th - 1; 1324*8b7f3994SMarcin Wojtas seqh = th - 1; 1325*8b7f3994SMarcin Wojtas if (check_window(replay, seq)) 1326*8b7f3994SMarcin Wojtas return (0); 1327*8b7f3994SMarcin Wojtas return (1); 1328*8b7f3994SMarcin Wojtas } 1329*8b7f3994SMarcin Wojtas 1330*8b7f3994SMarcin Wojtas /* 1331*8b7f3994SMarcin Wojtas * Seq is within [0, bl) but the whole window is within one subspace. 1332*8b7f3994SMarcin Wojtas * This means that seq has wrapped and is in next subspace 1333*8b7f3994SMarcin Wojtas */ 1334*8b7f3994SMarcin Wojtas *seqhigh = th + 1; 1335*8b7f3994SMarcin Wojtas seqh = th + 1; 1336*8b7f3994SMarcin Wojtas 1337*8b7f3994SMarcin Wojtas /* Don't let high part wrap. */ 1338*8b7f3994SMarcin Wojtas if (seqh == 0) { 1339*8b7f3994SMarcin Wojtas /* Set overflow flag. */ 1340*8b7f3994SMarcin Wojtas replay->overflow++; 1341*8b7f3994SMarcin Wojtas 1342*8b7f3994SMarcin Wojtas if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) { 1343*8b7f3994SMarcin Wojtas if (sav->sah->saidx.proto == IPPROTO_ESP) 1344*8b7f3994SMarcin Wojtas ESPSTAT_INC(esps_wrap); 1345*8b7f3994SMarcin Wojtas else if (sav->sah->saidx.proto == IPPROTO_AH) 1346*8b7f3994SMarcin Wojtas AHSTAT_INC(ahs_wrap); 1347*8b7f3994SMarcin Wojtas return (0); 1348*8b7f3994SMarcin Wojtas } 1349*8b7f3994SMarcin Wojtas 1350*8b7f3994SMarcin Wojtas ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n", 1351*8b7f3994SMarcin Wojtas __func__, replay->overflow, 1352*8b7f3994SMarcin Wojtas ipsec_sa2str(sav, buf, sizeof(buf)))); 1353*8b7f3994SMarcin Wojtas } 1354*8b7f3994SMarcin Wojtas 1355fcf59617SAndrey V. Elsukov return (1); 135688768458SSam Leffler } 135788768458SSam Leffler 135888768458SSam Leffler /* 1359de47c390SBjoern A. Zeeb * Check replay counter whether to update or not. 136088768458SSam Leffler * OUT: 0: OK 136188768458SSam Leffler * 1: NG 136288768458SSam Leffler */ 136388768458SSam Leffler int 1364fcf59617SAndrey V. Elsukov ipsec_updatereplay(uint32_t seq, struct secasvar *sav) 136588768458SSam Leffler { 136688768458SSam Leffler struct secreplay *replay; 1367*8b7f3994SMarcin Wojtas uint32_t window; 1368*8b7f3994SMarcin Wojtas uint32_t tl, th, bl; 1369*8b7f3994SMarcin Wojtas uint32_t seqh; 137088768458SSam Leffler 13719ffa9677SSam Leffler IPSEC_ASSERT(sav != NULL, ("Null SA")); 13729ffa9677SSam Leffler IPSEC_ASSERT(sav->replay != NULL, ("Null replay state")); 137388768458SSam Leffler 137488768458SSam Leffler replay = sav->replay; 137588768458SSam Leffler 1376*8b7f3994SMarcin Wojtas /* No need to check replay if disabled. */ 137788768458SSam Leffler if (replay->wsize == 0) 1378*8b7f3994SMarcin Wojtas return (0); 137988768458SSam Leffler 1380*8b7f3994SMarcin Wojtas /* Zero sequence number is not allowed. */ 1381*8b7f3994SMarcin Wojtas if (seq == 0 && replay->last == 0) 1382fcf59617SAndrey V. Elsukov return (1); 138388768458SSam Leffler 1384*8b7f3994SMarcin Wojtas window = replay->wsize << 3; /* Size of window */ 1385*8b7f3994SMarcin Wojtas tl = (uint32_t)replay->last; /* Top of window, lower part */ 1386*8b7f3994SMarcin Wojtas th = (uint32_t)(replay->last >> 32); /* Top of window, high part */ 1387*8b7f3994SMarcin Wojtas bl = tl - window + 1; /* Bottom of window, lower part */ 138888768458SSam Leffler 1389*8b7f3994SMarcin Wojtas /* 1390*8b7f3994SMarcin Wojtas * We keep the high part intact when: 1391*8b7f3994SMarcin Wojtas * 1) the seq is within [bl, 0xffffffff] and the whole window is 1392*8b7f3994SMarcin Wojtas * within one subspace; 1393*8b7f3994SMarcin Wojtas * 2) the seq is within [0, bl) and window spans two subspaces. 1394*8b7f3994SMarcin Wojtas */ 1395*8b7f3994SMarcin Wojtas if ((tl >= window - 1 && seq >= bl) || 1396*8b7f3994SMarcin Wojtas (tl < window - 1 && seq < bl)) { 1397*8b7f3994SMarcin Wojtas seqh = th; 1398*8b7f3994SMarcin Wojtas if (seq <= tl) { 1399*8b7f3994SMarcin Wojtas /* Sequence number inside window - check against replay */ 1400*8b7f3994SMarcin Wojtas if (check_window(replay, seq)) 1401fcf59617SAndrey V. Elsukov return (1); 1402*8b7f3994SMarcin Wojtas set_window(replay, seq); 1403*8b7f3994SMarcin Wojtas } else { 1404*8b7f3994SMarcin Wojtas advance_window(replay, ((uint64_t)seqh << 32) | seq); 1405*8b7f3994SMarcin Wojtas set_window(replay, seq); 1406*8b7f3994SMarcin Wojtas replay->last = ((uint64_t)seqh << 32) | seq; 1407fcf59617SAndrey V. Elsukov } 140888768458SSam Leffler 1409*8b7f3994SMarcin Wojtas /* Sequence number above top of window or not found in bitmap */ 1410d5f39c34SFabien Thomas replay->count++; 1411fcf59617SAndrey V. Elsukov return (0); 141288768458SSam Leffler } 141388768458SSam Leffler 1414*8b7f3994SMarcin Wojtas if (!(sav->flags & SADB_X_SAFLAGS_ESN)) 1415*8b7f3994SMarcin Wojtas return (1); 1416*8b7f3994SMarcin Wojtas 1417*8b7f3994SMarcin Wojtas /* 1418*8b7f3994SMarcin Wojtas * Seq is within [bl, 0xffffffff] and bl is within 1419*8b7f3994SMarcin Wojtas * [0xffffffff-window, 0xffffffff]. This means we got a seq 1420*8b7f3994SMarcin Wojtas * which is within our replay window, but in the previous 1421*8b7f3994SMarcin Wojtas * subspace. 1422*8b7f3994SMarcin Wojtas */ 1423*8b7f3994SMarcin Wojtas if (tl < window - 1 && seq >= bl) { 1424*8b7f3994SMarcin Wojtas if (th == 0) 1425*8b7f3994SMarcin Wojtas return (1); 1426*8b7f3994SMarcin Wojtas if (check_window(replay, seq)) 1427*8b7f3994SMarcin Wojtas return (1); 1428*8b7f3994SMarcin Wojtas 1429*8b7f3994SMarcin Wojtas set_window(replay, seq); 1430*8b7f3994SMarcin Wojtas replay->count++; 1431*8b7f3994SMarcin Wojtas return (0); 1432*8b7f3994SMarcin Wojtas } 1433*8b7f3994SMarcin Wojtas 1434*8b7f3994SMarcin Wojtas /* 1435*8b7f3994SMarcin Wojtas * Seq is within [0, bl) but the whole window is within one subspace. 1436*8b7f3994SMarcin Wojtas * This means that seq has wrapped and is in next subspace 1437*8b7f3994SMarcin Wojtas */ 1438*8b7f3994SMarcin Wojtas seqh = th + 1; 1439*8b7f3994SMarcin Wojtas 1440*8b7f3994SMarcin Wojtas /* Don't let high part wrap. */ 1441*8b7f3994SMarcin Wojtas if (seqh == 0) 1442*8b7f3994SMarcin Wojtas return (1); 1443*8b7f3994SMarcin Wojtas 1444*8b7f3994SMarcin Wojtas advance_window(replay, ((uint64_t)seqh << 32) | seq); 1445*8b7f3994SMarcin Wojtas set_window(replay, seq); 1446*8b7f3994SMarcin Wojtas replay->last = ((uint64_t)seqh << 32) | seq; 1447*8b7f3994SMarcin Wojtas replay->count++; 1448*8b7f3994SMarcin Wojtas return (0); 1449*8b7f3994SMarcin Wojtas } 1450fcf59617SAndrey V. Elsukov int 14512e08e39fSConrad Meyer ipsec_updateid(struct secasvar *sav, crypto_session_t *new, 14522e08e39fSConrad Meyer crypto_session_t *old) 1453fcf59617SAndrey V. Elsukov { 14542e08e39fSConrad Meyer crypto_session_t tmp; 145588768458SSam Leffler 1456fcf59617SAndrey V. Elsukov /* 1457fcf59617SAndrey V. Elsukov * tdb_cryptoid is initialized by xform_init(). 1458fcf59617SAndrey V. Elsukov * Then it can be changed only when some crypto error occurred or 1459fcf59617SAndrey V. Elsukov * when SA is deleted. We stored used cryptoid in the xform_data 1460fcf59617SAndrey V. Elsukov * structure. In case when crypto error occurred and crypto 1461fcf59617SAndrey V. Elsukov * subsystem has reinited the session, it returns new cryptoid 1462fcf59617SAndrey V. Elsukov * and EAGAIN error code. 1463fcf59617SAndrey V. Elsukov * 1464fcf59617SAndrey V. Elsukov * This function will be called when we got EAGAIN from crypto 1465fcf59617SAndrey V. Elsukov * subsystem. 1466fcf59617SAndrey V. Elsukov * *new is cryptoid that was returned by crypto subsystem in 1467fcf59617SAndrey V. Elsukov * the crp_sid. 1468fcf59617SAndrey V. Elsukov * *old is the original cryptoid that we stored in xform_data. 1469fcf59617SAndrey V. Elsukov * 1470fcf59617SAndrey V. Elsukov * For first failed request *old == sav->tdb_cryptoid, then 1471fcf59617SAndrey V. Elsukov * we update sav->tdb_cryptoid and redo crypto_dispatch(). 1472fcf59617SAndrey V. Elsukov * For next failed request *old != sav->tdb_cryptoid, then 1473fcf59617SAndrey V. Elsukov * we store cryptoid from first request into the *new variable 1474fcf59617SAndrey V. Elsukov * and crp_sid from this second session will be returned via 1475fcf59617SAndrey V. Elsukov * *old pointer, so caller can release second session. 1476fcf59617SAndrey V. Elsukov * 1477fcf59617SAndrey V. Elsukov * XXXAE: check this more carefully. 1478fcf59617SAndrey V. Elsukov */ 1479fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_STAMP, 14801b0909d5SConrad Meyer printf("%s: SA(%p) moves cryptoid %p -> %p\n", 14811b0909d5SConrad Meyer __func__, sav, *old, *new)); 1482fcf59617SAndrey V. Elsukov KEYDBG(IPSEC_DATA, kdebug_secasv(sav)); 1483fcf59617SAndrey V. Elsukov SECASVAR_LOCK(sav); 1484fcf59617SAndrey V. Elsukov if (sav->tdb_cryptoid != *old) { 1485fcf59617SAndrey V. Elsukov /* cryptoid was already updated */ 1486fcf59617SAndrey V. Elsukov tmp = *new; 1487fcf59617SAndrey V. Elsukov *new = sav->tdb_cryptoid; 1488fcf59617SAndrey V. Elsukov *old = tmp; 1489bf435626SFabien Thomas SECASVAR_UNLOCK(sav); 1490fcf59617SAndrey V. Elsukov return (1); 1491fcf59617SAndrey V. Elsukov } 1492fcf59617SAndrey V. Elsukov sav->tdb_cryptoid = *new; 1493fcf59617SAndrey V. Elsukov SECASVAR_UNLOCK(sav); 1494fcf59617SAndrey V. Elsukov return (0); 149588768458SSam Leffler } 149688768458SSam Leffler 1497fcf59617SAndrey V. Elsukov int 1498fcf59617SAndrey V. Elsukov ipsec_initialized(void) 149988768458SSam Leffler { 1500de47c390SBjoern A. Zeeb 1501fcf59617SAndrey V. Elsukov return (V_def_policy != NULL); 150288768458SSam Leffler } 150388768458SSam Leffler 15048381996eSSam Leffler static void 150593201211SAndrey V. Elsukov def_policy_init(const void *unused __unused) 15061ed81b73SMarko Zec { 15071ed81b73SMarko Zec 1508fcf59617SAndrey V. Elsukov V_def_policy = key_newsp(); 1509fcf59617SAndrey V. Elsukov if (V_def_policy != NULL) { 1510fcf59617SAndrey V. Elsukov V_def_policy->policy = IPSEC_POLICY_NONE; 1511fcf59617SAndrey V. Elsukov /* Force INPCB SP cache invalidation */ 1512fcf59617SAndrey V. Elsukov key_bumpspgen(); 1513fcf59617SAndrey V. Elsukov } else 1514fcf59617SAndrey V. Elsukov printf("%s: failed to initialize default policy\n", __func__); 15158381996eSSam Leffler } 1516fcf59617SAndrey V. Elsukov 1517fcf59617SAndrey V. Elsukov static void 1518fcf59617SAndrey V. Elsukov def_policy_uninit(const void *unused __unused) 1519fcf59617SAndrey V. Elsukov { 1520fcf59617SAndrey V. Elsukov 1521fcf59617SAndrey V. Elsukov if (V_def_policy != NULL) { 1522fcf59617SAndrey V. Elsukov key_freesp(&V_def_policy); 1523fcf59617SAndrey V. Elsukov key_bumpspgen(); 1524fcf59617SAndrey V. Elsukov } 1525fcf59617SAndrey V. Elsukov } 1526fcf59617SAndrey V. Elsukov 152789856f7eSBjoern A. Zeeb VNET_SYSINIT(def_policy_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, 152893201211SAndrey V. Elsukov def_policy_init, NULL); 1529fcf59617SAndrey V. Elsukov VNET_SYSUNINIT(def_policy_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, 1530fcf59617SAndrey V. Elsukov def_policy_uninit, NULL); 1531