ipsec.c revision 283902
1189462Semax/* $FreeBSD: stable/10/sys/netipsec/ipsec.c 283902 2015-06-02 03:43:36Z ae $ */ 2121054Semax/* $KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $ */ 3121054Semax 4121054Semax/*- 5121054Semax * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6121054Semax * All rights reserved. 7121054Semax * 8121054Semax * Redistribution and use in source and binary forms, with or without 9121054Semax * modification, are permitted provided that the following conditions 10121054Semax * are met: 11121054Semax * 1. Redistributions of source code must retain the above copyright 12121054Semax * notice, this list of conditions and the following disclaimer. 13121054Semax * 2. Redistributions in binary form must reproduce the above copyright 14121054Semax * notice, this list of conditions and the following disclaimer in the 15121054Semax * documentation and/or other materials provided with the distribution. 16121054Semax * 3. Neither the name of the project nor the names of its contributors 17121054Semax * may be used to endorse or promote products derived from this software 18121054Semax * without specific prior written permission. 19121054Semax * 20121054Semax * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21121054Semax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22121054Semax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23121054Semax * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24121054Semax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25121054Semax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26121054Semax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27121054Semax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28191388Semax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29121054Semax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30121054Semax * SUCH DAMAGE. 31121054Semax */ 32121054Semax 33121054Semax/* 34121054Semax * IPsec controller part. 35121054Semax */ 36131631Sru 37121054Semax#include "opt_inet.h" 38121054Semax#include "opt_inet6.h" 39121054Semax#include "opt_ipsec.h" 40121054Semax 41131631Sru#include <sys/param.h> 42121054Semax#include <sys/systm.h> 43181698Semax#include <sys/malloc.h> 44191388Semax#include <sys/mbuf.h> 45191388Semax#include <sys/domain.h> 46191388Semax#include <sys/priv.h> 47191388Semax#include <sys/protosw.h> 48191388Semax#include <sys/socket.h> 49191388Semax#include <sys/socketvar.h> 50191388Semax#include <sys/errno.h> 51191388Semax#include <sys/time.h> 52191388Semax#include <sys/kernel.h> 53191388Semax#include <sys/syslog.h> 54191388Semax#include <sys/sysctl.h> 55191388Semax#include <sys/proc.h> 56191388Semax 57191388Semax#include <net/if.h> 58191388Semax#include <net/vnet.h> 59191388Semax 60191388Semax#include <netinet/in.h> 61181698Semax#include <netinet/in_systm.h> 62181698Semax#include <netinet/ip.h> 63181698Semax#include <netinet/ip_var.h> 64131631Sru#include <netinet/in_var.h> 65121054Semax#include <netinet/udp.h> 66121054Semax#include <netinet/udp_var.h> 67121054Semax#include <netinet/tcp.h> 68121054Semax#include <netinet/udp.h> 69121054Semax 70121054Semax#include <netinet/ip6.h> 71121054Semax#ifdef INET6 72121054Semax#include <netinet6/ip6_var.h> 73121054Semax#endif 74121054Semax#include <netinet/in_pcb.h> 75121054Semax#ifdef INET6 76121054Semax#include <netinet/icmp6.h> 77121054Semax#endif 78121054Semax 79121054Semax#include <sys/types.h> 80121054Semax#include <netipsec/ipsec.h> 81121054Semax#ifdef INET6 82121054Semax#include <netipsec/ipsec6.h> 83121054Semax#endif 84121054Semax#include <netipsec/ah_var.h> 85121054Semax#include <netipsec/esp_var.h> 86121054Semax#include <netipsec/ipcomp.h> /*XXX*/ 87121054Semax#include <netipsec/ipcomp_var.h> 88121054Semax 89121054Semax#include <netipsec/key.h> 90121054Semax#include <netipsec/keydb.h> 91121054Semax#include <netipsec/key_debug.h> 92121054Semax 93181698Semax#include <netipsec/xform.h> 94189462Semax 95189462Semax#include <machine/in_cksum.h> 96189462Semax 97189462Semax#include <opencrypto/cryptodev.h> 98189462Semax 99189462Semax#ifdef IPSEC_DEBUG 100189462SemaxVNET_DEFINE(int, ipsec_debug) = 1; 101189462Semax#else 102189462SemaxVNET_DEFINE(int, ipsec_debug) = 0; 103189462Semax#endif 104191388Semax 105191388Semax/* NB: name changed so netstat doesn't use it. */ 106191388SemaxVNET_PCPUSTAT_DEFINE(struct ipsecstat, ipsec4stat); 107191388SemaxVNET_PCPUSTAT_SYSINIT(ipsec4stat); 108191388Semax 109191388Semax#ifdef VIMAGE 110191388SemaxVNET_PCPUSTAT_SYSUNINIT(ipsec4stat); 111191388Semax#endif /* VIMAGE */ 112191388Semax 113191388SemaxVNET_DEFINE(int, ip4_ah_offsetmask) = 0; /* maybe IP_DF? */ 114191388Semax/* DF bit on encap. 0: clear 1: set 2: copy */ 115191388SemaxVNET_DEFINE(int, ip4_ipsec_dfbit) = 0; 116191388SemaxVNET_DEFINE(int, ip4_esp_trans_deflev) = IPSEC_LEVEL_USE; 117191388SemaxVNET_DEFINE(int, ip4_esp_net_deflev) = IPSEC_LEVEL_USE; 118191388SemaxVNET_DEFINE(int, ip4_ah_trans_deflev) = IPSEC_LEVEL_USE; 119191388SemaxVNET_DEFINE(int, ip4_ah_net_deflev) = IPSEC_LEVEL_USE; 120191388SemaxVNET_DEFINE(struct secpolicy, ip4_def_policy); 121191388Semax/* ECN ignore(-1)/forbidden(0)/allowed(1) */ 122191388SemaxVNET_DEFINE(int, ip4_ipsec_ecn) = 0; 123191388SemaxVNET_DEFINE(int, ip4_esp_randpad) = -1; 124191388Semax 125191388Semax/* 126191388Semax * Crypto support requirements: 127191388Semax * 128191388Semax * 1 require hardware support 129191388Semax * -1 require software support 130181698Semax * 0 take anything 131181698Semax */ 132181698SemaxVNET_DEFINE(int, crypto_support) = CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE; 133181698Semax 134181698SemaxFEATURE(ipsec, "Internet Protocol Security (IPsec)"); 135121054Semax#ifdef IPSEC_NAT_T 136121054SemaxFEATURE(ipsec_natt, "UDP Encapsulation of IPsec ESP Packets ('NAT-T')"); 137121054Semax#endif 138121054Semax 139121054SemaxSYSCTL_DECL(_net_inet_ipsec); 140121054Semax 141121054Semax/* net.inet.ipsec */ 142121054SemaxSYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DEF_POLICY, def_policy, 143121054Semax CTLFLAG_RW, &VNET_NAME(ip4_def_policy).policy, 0, 144121054Semax "IPsec default policy."); 145121054SemaxSYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev, 146121054Semax CTLFLAG_RW, &VNET_NAME(ip4_esp_trans_deflev), 0, 147121054Semax "Default ESP transport mode level"); 148121054SemaxSYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev, 149121054Semax CTLFLAG_RW, &VNET_NAME(ip4_esp_net_deflev), 0, 150121054Semax "Default ESP tunnel mode level."); 151121054SemaxSYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev, 152140170Sru CTLFLAG_RW, &VNET_NAME(ip4_ah_trans_deflev), 0, 153121054Semax "AH transfer mode default level."); 154121054SemaxSYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev, 155121054Semax CTLFLAG_RW, &VNET_NAME(ip4_ah_net_deflev), 0, 156121054Semax "AH tunnel mode default level."); 157121054SemaxSYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_AH_CLEARTOS, ah_cleartos, 158121054Semax CTLFLAG_RW, &VNET_NAME(ah_cleartos), 0, 159121054Semax "If set clear type-of-service field when doing AH computation."); 160121054SemaxSYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_AH_OFFSETMASK, ah_offsetmask, 161121054Semax CTLFLAG_RW, &VNET_NAME(ip4_ah_offsetmask), 0, 162131631Sru "If not set clear offset field mask when doing AH computation."); 163121054SemaxSYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DFBIT, dfbit, 164121054Semax CTLFLAG_RW, &VNET_NAME(ip4_ipsec_dfbit), 0, 165121054Semax "Do not fragment bit on encap."); 166121054SemaxSYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_ECN, ecn, 167121054Semax CTLFLAG_RW, &VNET_NAME(ip4_ipsec_ecn), 0, 168121054Semax "Explicit Congestion Notification handling."); 169121054SemaxSYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DEBUG, debug, 170121054Semax CTLFLAG_RW, &VNET_NAME(ipsec_debug), 0, 171121054Semax "Enable IPsec debugging output when set."); 172121054SemaxSYSCTL_VNET_INT(_net_inet_ipsec, OID_AUTO, crypto_support, 173121054Semax CTLFLAG_RW, &VNET_NAME(crypto_support), 0, 174121054Semax "Crypto driver selection."); 175121054SemaxSYSCTL_VNET_PCPUSTAT(_net_inet_ipsec, OID_AUTO, ipsecstats, struct ipsecstat, 176121054Semax ipsec4stat, "IPsec IPv4 statistics."); 177121054Semax 178121054Semax#ifdef REGRESSION 179121054Semax/* 180121054Semax * When set to 1, IPsec will send packets with the same sequence number. 181121054Semax * This allows to verify if the other side has proper replay attacks detection. 182121054Semax */ 183121054SemaxVNET_DEFINE(int, ipsec_replay) = 0; 184121054SemaxSYSCTL_VNET_INT(_net_inet_ipsec, OID_AUTO, test_replay, 185121054Semax CTLFLAG_RW, &VNET_NAME(ipsec_replay), 0, 186121054Semax "Emulate replay attack"); 187121054Semax/* 188121054Semax * When set 1, IPsec will send packets with corrupted HMAC. 189121054Semax * This allows to verify if the other side properly detects modified packets. 190121054Semax */ 191121054SemaxVNET_DEFINE(int, ipsec_integrity) = 0; 192121054SemaxSYSCTL_VNET_INT(_net_inet_ipsec, OID_AUTO, test_integrity, 193121054Semax CTLFLAG_RW, &VNET_NAME(ipsec_integrity), 0, 194121054Semax "Emulate man-in-the-middle attack"); 195121054Semax#endif 196121054Semax 197121054Semax#ifdef INET6 198121054SemaxVNET_PCPUSTAT_DEFINE(struct ipsecstat, ipsec6stat); 199121054SemaxVNET_PCPUSTAT_SYSINIT(ipsec6stat); 200121054Semax 201121054Semax#ifdef VIMAGE 202121054SemaxVNET_PCPUSTAT_SYSUNINIT(ipsec6stat); 203121054Semax#endif /* VIMAGE */ 204121054Semax 205121054SemaxVNET_DEFINE(int, ip6_esp_trans_deflev) = IPSEC_LEVEL_USE; 206121054SemaxVNET_DEFINE(int, ip6_esp_net_deflev) = IPSEC_LEVEL_USE; 207140170SruVNET_DEFINE(int, ip6_ah_trans_deflev) = IPSEC_LEVEL_USE; 208121054SemaxVNET_DEFINE(int, ip6_ah_net_deflev) = IPSEC_LEVEL_USE; 209121054SemaxVNET_DEFINE(int, ip6_ipsec_ecn) = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */ 210121054Semax 211121054SemaxSYSCTL_DECL(_net_inet6_ipsec6); 212121054Semax 213121054Semax/* net.inet6.ipsec6 */ 214121054SemaxSYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_DEF_POLICY, def_policy, CTLFLAG_RW, 215121054Semax &VNET_NAME(ip4_def_policy).policy, 0, 216121054Semax "IPsec default policy."); 217121054SemaxSYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_TRANSLEV, 218121054Semax esp_trans_deflev, CTLFLAG_RW, &VNET_NAME(ip6_esp_trans_deflev), 0, 219121054Semax "Default ESP transport mode level."); 220121054SemaxSYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_NETLEV, 221121054Semax esp_net_deflev, CTLFLAG_RW, &VNET_NAME(ip6_esp_net_deflev), 0, 222121054Semax "Default ESP tunnel mode level."); 223121054SemaxSYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_TRANSLEV, 224121054Semax ah_trans_deflev, CTLFLAG_RW, &VNET_NAME(ip6_ah_trans_deflev), 0, 225121054Semax "AH transfer mode default level."); 226121054SemaxSYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_NETLEV, 227121054Semax ah_net_deflev, CTLFLAG_RW, &VNET_NAME(ip6_ah_net_deflev), 0, 228121054Semax "AH tunnel mode default level."); 229121054SemaxSYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_ECN, 230121054Semax ecn, CTLFLAG_RW, &VNET_NAME(ip6_ipsec_ecn), 0, 231121054Semax "Explicit Congestion Notification handling."); 232121054SemaxSYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_DEBUG, debug, CTLFLAG_RW, 233121054Semax &VNET_NAME(ipsec_debug), 0, 234121054Semax "Enable IPsec debugging output when set."); 235121054SemaxSYSCTL_VNET_PCPUSTAT(_net_inet6_ipsec6, IPSECCTL_STATS, ipsecstats, 236121054Semax struct ipsecstat, ipsec6stat, "IPsec IPv6 statistics."); 237121054Semax#endif /* INET6 */ 238121054Semax 239121054Semaxstatic int ipsec_setspidx_inpcb(struct mbuf *, struct inpcb *); 240121054Semaxstatic int ipsec_setspidx(struct mbuf *, struct secpolicyindex *, int); 241121054Semaxstatic void ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *, int); 242121054Semaxstatic int ipsec4_setspidx_ipaddr(struct mbuf *, struct secpolicyindex *); 243121054Semax#ifdef INET6 244121054Semaxstatic void ipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *, int); 245121054Semaxstatic int ipsec6_setspidx_ipaddr(struct mbuf *, struct secpolicyindex *); 246121054Semax#endif 247121054Semaxstatic void ipsec_delpcbpolicy(struct inpcbpolicy *); 248121054Semaxstatic struct secpolicy *ipsec_deepcopy_policy(struct secpolicy *src); 249121054Semaxstatic void vshiftl(unsigned char *, int, int); 250121054Semax 251181698SemaxMALLOC_DEFINE(M_IPSEC_INPCB, "inpcbpolicy", "inpcb-resident ipsec policy"); 252181698Semax 253189462Semax/* 254189462Semax * Return a held reference to the default SP. 255189462Semax */ 256189462Semaxstatic struct secpolicy * 257189462Semaxkey_allocsp_default(const char* where, int tag) 258189462Semax{ 259189462Semax struct secpolicy *sp; 260189462Semax 261189462Semax KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 262189462Semax printf("DP key_allocsp_default from %s:%u\n", where, tag)); 263189462Semax 264189462Semax sp = &V_ip4_def_policy; 265189462Semax if (sp->policy != IPSEC_POLICY_DISCARD && 266189462Semax sp->policy != IPSEC_POLICY_NONE) { 267189462Semax ipseclog((LOG_INFO, "fixed system default policy: %d->%d\n", 268189462Semax sp->policy, IPSEC_POLICY_NONE)); 269189462Semax sp->policy = IPSEC_POLICY_NONE; 270189462Semax } 271189462Semax key_addref(sp); 272189462Semax 273189462Semax KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 274189462Semax printf("DP key_allocsp_default returns SP:%p (%u)\n", 275198492Semax sp, sp->refcnt)); 276189462Semax return (sp); 277189462Semax} 278189462Semax#define KEY_ALLOCSP_DEFAULT() \ 279189462Semax key_allocsp_default(__FILE__, __LINE__) 280189462Semax 281189462Semax/* 282189462Semax * For OUTBOUND packet having a socket. Searching SPD for packet, 283189462Semax * and return a pointer to SP. 284189462Semax * OUT: NULL: no apropreate SP found, the following value is set to error. 285189462Semax * 0 : bypass 286189462Semax * EACCES : discard packet. 287189462Semax * ENOENT : ipsec_acquire() in progress, maybe. 288189462Semax * others : error occured. 289189462Semax * others: a pointer to SP 290189462Semax * 291189462Semax * NOTE: IPv6 mapped adddress concern is implemented here. 292189462Semax */ 293189462Semaxstruct secpolicy * 294189462Semaxipsec_getpolicy(struct tdb_ident *tdbi, u_int dir) 295189462Semax{ 296189462Semax struct secpolicy *sp; 297189462Semax 298189462Semax IPSEC_ASSERT(tdbi != NULL, ("null tdbi")); 299189462Semax IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND, 300189462Semax ("invalid direction %u", dir)); 301189462Semax 302189462Semax sp = KEY_ALLOCSP2(tdbi->spi, &tdbi->dst, tdbi->proto, dir); 303189462Semax if (sp == NULL) /*XXX????*/ 304189462Semax sp = KEY_ALLOCSP_DEFAULT(); 305189462Semax IPSEC_ASSERT(sp != NULL, ("null SP")); 306189462Semax return (sp); 307189462Semax} 308189462Semax 309189462Semax/* 310189462Semax * For OUTBOUND packet having a socket. Searching SPD for packet, 311189462Semax * and return a pointer to SP. 312189462Semax * OUT: NULL: no apropreate SP found, the following value is set to error. 313189462Semax * 0 : bypass 314189462Semax * EACCES : discard packet. 315189462Semax * ENOENT : ipsec_acquire() in progress, maybe. 316189462Semax * others : error occured. 317189462Semax * others: a pointer to SP 318189462Semax * 319189462Semax * NOTE: IPv6 mapped adddress concern is implemented here. 320189462Semax */ 321189462Semaxstatic struct secpolicy * 322189462Semaxipsec_getpolicybysock(struct mbuf *m, u_int dir, struct inpcb *inp, int *error) 323189462Semax{ 324189462Semax struct inpcbpolicy *pcbsp; 325189462Semax struct secpolicy *currsp = NULL; /* Policy on socket. */ 326189462Semax struct secpolicy *sp; 327189462Semax 328189462Semax IPSEC_ASSERT(m != NULL, ("null mbuf")); 329189462Semax IPSEC_ASSERT(inp != NULL, ("null inpcb")); 330189462Semax IPSEC_ASSERT(error != NULL, ("null error")); 331189462Semax IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND, 332189462Semax ("invalid direction %u", dir)); 333189462Semax 334189462Semax /* Set spidx in pcb. */ 335189462Semax *error = ipsec_setspidx_inpcb(m, inp); 336189462Semax if (*error) 337189462Semax return (NULL); 338189462Semax 339189462Semax pcbsp = inp->inp_sp; 340189462Semax IPSEC_ASSERT(pcbsp != NULL, ("null pcbsp")); 341189462Semax switch (dir) { 342189462Semax case IPSEC_DIR_INBOUND: 343189462Semax currsp = pcbsp->sp_in; 344189462Semax break; 345189462Semax case IPSEC_DIR_OUTBOUND: 346189462Semax currsp = pcbsp->sp_out; 347189462Semax break; 348189462Semax } 349189462Semax IPSEC_ASSERT(currsp != NULL, ("null currsp")); 350189462Semax 351189462Semax if (pcbsp->priv) { /* When privilieged socket. */ 352189462Semax switch (currsp->policy) { 353189462Semax case IPSEC_POLICY_BYPASS: 354189462Semax case IPSEC_POLICY_IPSEC: 355189462Semax key_addref(currsp); 356189462Semax sp = currsp; 357191388Semax break; 358191388Semax 359191388Semax case IPSEC_POLICY_ENTRUST: 360191388Semax /* Look for a policy in SPD. */ 361191388Semax sp = KEY_ALLOCSP(&currsp->spidx, dir); 362191388Semax if (sp == NULL) /* No SP found. */ 363191388Semax sp = KEY_ALLOCSP_DEFAULT(); 364191388Semax break; 365191388Semax 366191388Semax default: 367191388Semax ipseclog((LOG_ERR, "%s: Invalid policy for PCB %d\n", 368191388Semax __func__, currsp->policy)); 369191388Semax *error = EINVAL; 370191388Semax return (NULL); 371191388Semax } 372191388Semax } else { /* Unpriv, SPD has policy. */ 373191388Semax sp = KEY_ALLOCSP(&currsp->spidx, dir); 374191388Semax if (sp == NULL) { /* No SP found. */ 375191388Semax switch (currsp->policy) { 376191388Semax case IPSEC_POLICY_BYPASS: 377191388Semax ipseclog((LOG_ERR, "%s: Illegal policy for " 378191388Semax "non-priviliged defined %d\n", 379191388Semax __func__, currsp->policy)); 380191388Semax *error = EINVAL; 381191388Semax return (NULL); 382191388Semax 383191388Semax case IPSEC_POLICY_ENTRUST: 384191388Semax sp = KEY_ALLOCSP_DEFAULT(); 385191388Semax break; 386210823Sjoel 387191388Semax case IPSEC_POLICY_IPSEC: 388191388Semax key_addref(currsp); 389191388Semax sp = currsp; 390191388Semax break; 391191388Semax 392191388Semax default: 393191388Semax ipseclog((LOG_ERR, "%s: Invalid policy for " 394191388Semax "PCB %d\n", __func__, currsp->policy)); 395191388Semax *error = EINVAL; 396191388Semax return (NULL); 397191388Semax } 398191388Semax } 399191388Semax } 400191388Semax IPSEC_ASSERT(sp != NULL, 401191388Semax ("null SP (priv %u policy %u", pcbsp->priv, currsp->policy)); 402191388Semax KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 403191388Semax printf("DP %s (priv %u policy %u) allocate SP:%p (refcnt %u)\n", 404191388Semax __func__, pcbsp->priv, currsp->policy, sp, sp->refcnt)); 405191388Semax return (sp); 406191388Semax} 407191388Semax 408191388Semax/* 409191388Semax * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet, 410191388Semax * and return a pointer to SP. 411191388Semax * OUT: positive: a pointer to the entry for security policy leaf matched. 412191388Semax * NULL: no apropreate SP found, the following value is set to error. 413191388Semax * 0 : bypass 414191388Semax * EACCES : discard packet. 415191388Semax * ENOENT : ipsec_acquire() in progress, maybe. 416191388Semax * others : error occured. 417191388Semax */ 418191388Semaxstruct secpolicy * 419191388Semaxipsec_getpolicybyaddr(struct mbuf *m, u_int dir, int flag, int *error) 420191388Semax{ 421191388Semax struct secpolicyindex spidx; 422191388Semax struct secpolicy *sp; 423191388Semax 424191388Semax IPSEC_ASSERT(m != NULL, ("null mbuf")); 425210823Sjoel IPSEC_ASSERT(error != NULL, ("null error")); 426191388Semax IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND, 427191388Semax ("invalid direction %u", dir)); 428191388Semax 429191388Semax sp = NULL; 430191388Semax if (key_havesp(dir)) { 431191388Semax /* Make an index to look for a policy. */ 432191388Semax *error = ipsec_setspidx(m, &spidx, 433191388Semax (flag & IP_FORWARDING) ? 0 : 1); 434191388Semax if (*error != 0) { 435191388Semax DPRINTF(("%s: setpidx failed, dir %u flag %u\n", 436191388Semax __func__, dir, flag)); 437191388Semax return (NULL); 438191388Semax } 439191388Semax spidx.dir = dir; 440191388Semax 441191388Semax sp = KEY_ALLOCSP(&spidx, dir); 442191388Semax } 443191388Semax if (sp == NULL) /* No SP found, use system default. */ 444191388Semax sp = KEY_ALLOCSP_DEFAULT(); 445191388Semax IPSEC_ASSERT(sp != NULL, ("null SP")); 446191388Semax return (sp); 447191388Semax} 448191388Semax 449191388Semaxstruct secpolicy * 450191388Semaxipsec4_checkpolicy(struct mbuf *m, u_int dir, u_int flag, int *error, 451191388Semax struct inpcb *inp) 452191388Semax{ 453191388Semax struct secpolicy *sp; 454191388Semax 455191388Semax *error = 0; 456191388Semax if (inp == NULL) 457191388Semax sp = ipsec_getpolicybyaddr(m, dir, flag, error); 458191388Semax else 459191388Semax sp = ipsec_getpolicybysock(m, dir, inp, error); 460191388Semax if (sp == NULL) { 461191388Semax IPSEC_ASSERT(*error != 0, ("getpolicy failed w/o error")); 462191388Semax IPSECSTAT_INC(ips_out_inval); 463191388Semax return (NULL); 464191388Semax } 465191388Semax IPSEC_ASSERT(*error == 0, ("sp w/ error set to %u", *error)); 466191388Semax switch (sp->policy) { 467191388Semax case IPSEC_POLICY_ENTRUST: 468191388Semax default: 469191388Semax printf("%s: invalid policy %u\n", __func__, sp->policy); 470191388Semax /* FALLTHROUGH */ 471191388Semax case IPSEC_POLICY_DISCARD: 472191388Semax IPSECSTAT_INC(ips_out_polvio); 473191388Semax *error = -EINVAL; /* Packet is discarded by caller. */ 474191388Semax break; 475191388Semax case IPSEC_POLICY_BYPASS: 476191388Semax case IPSEC_POLICY_NONE: 477191388Semax KEY_FREESP(&sp); 478191388Semax sp = NULL; /* NB: force NULL result. */ 479191388Semax break; 480191388Semax case IPSEC_POLICY_IPSEC: 481191388Semax if (sp->req == NULL) /* Acquire a SA. */ 482191388Semax *error = key_spdacquire(sp); 483191388Semax break; 484191388Semax } 485191388Semax if (*error != 0) { 486191388Semax KEY_FREESP(&sp); 487191388Semax sp = NULL; 488191388Semax } 489191388Semax return (sp); 490191388Semax} 491191388Semax 492191388Semaxstatic int 493191388Semaxipsec_setspidx_inpcb(struct mbuf *m, struct inpcb *inp) 494191388Semax{ 495191388Semax int error; 496191388Semax 497235286Sgjb IPSEC_ASSERT(inp != NULL, ("null inp")); 498235286Sgjb IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp")); 499191388Semax IPSEC_ASSERT(inp->inp_sp->sp_out != NULL && inp->inp_sp->sp_in != NULL, 500191388Semax ("null sp_in || sp_out")); 501191388Semax 502191388Semax error = ipsec_setspidx(m, &inp->inp_sp->sp_in->spidx, 1); 503191388Semax if (error == 0) { 504191388Semax inp->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND; 505191388Semax inp->inp_sp->sp_out->spidx = inp->inp_sp->sp_in->spidx; 506191388Semax inp->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND; 507191388Semax } else { 508191388Semax bzero(&inp->inp_sp->sp_in->spidx, 509191388Semax sizeof (inp->inp_sp->sp_in->spidx)); 510191388Semax bzero(&inp->inp_sp->sp_out->spidx, 511191388Semax sizeof (inp->inp_sp->sp_in->spidx)); 512191388Semax } 513191388Semax return (error); 514191388Semax} 515233648Seadler 516191388Semax/* 517191388Semax * Configure security policy index (src/dst/proto/sport/dport) 518191388Semax * by looking at the content of mbuf. 519191388Semax * The caller is responsible for error recovery (like clearing up spidx). 520191388Semax */ 521191388Semaxstatic int 522191388Semaxipsec_setspidx(struct mbuf *m, struct secpolicyindex *spidx, int needport) 523191388Semax{ 524191388Semax struct ip *ip = NULL; 525191388Semax struct ip ipbuf; 526191388Semax u_int v; 527191388Semax struct mbuf *n; 528191388Semax int len; 529191388Semax int error; 530233648Seadler 531191388Semax IPSEC_ASSERT(m != NULL, ("null mbuf")); 532191388Semax 533191388Semax /* 534191388Semax * Validate m->m_pkthdr.len. We see incorrect length if we 535191388Semax * mistakenly call this function with inconsistent mbuf chain 536191388Semax * (like 4.4BSD tcp/udp processing). XXX Should we panic here? 537191388Semax */ 538191388Semax len = 0; 539191388Semax for (n = m; n; n = n->m_next) 540191388Semax len += n->m_len; 541191388Semax if (m->m_pkthdr.len != len) { 542191388Semax KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 543191388Semax printf("%s: pkthdr len(%d) mismatch (%d), ignored.\n", 544191388Semax __func__, len, m->m_pkthdr.len)); 545191388Semax return (EINVAL); 546191388Semax } 547191388Semax 548191388Semax if (m->m_pkthdr.len < sizeof(struct ip)) { 549211397Sjoel KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 550191388Semax printf("%s: pkthdr len(%d) too small (v4), ignored.\n", 551191388Semax __func__, m->m_pkthdr.len)); 552191388Semax return (EINVAL); 553191388Semax } 554191388Semax 555191388Semax if (m->m_len >= sizeof(*ip)) 556191388Semax ip = mtod(m, struct ip *); 557191388Semax else { 558191388Semax m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf); 559191388Semax ip = &ipbuf; 560191388Semax } 561191388Semax v = ip->ip_v; 562191388Semax switch (v) { 563191388Semax case 4: 564191388Semax error = ipsec4_setspidx_ipaddr(m, spidx); 565191388Semax if (error) 566191388Semax return (error); 567191388Semax ipsec4_get_ulp(m, spidx, needport); 568191388Semax return (0); 569191388Semax#ifdef INET6 570191388Semax case 6: 571191388Semax if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) { 572191388Semax KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 573191388Semax printf("%s: pkthdr len(%d) too small (v6), " 574191388Semax "ignored\n", __func__, m->m_pkthdr.len)); 575191388Semax return (EINVAL); 576191388Semax } 577191388Semax error = ipsec6_setspidx_ipaddr(m, spidx); 578191388Semax if (error) 579191388Semax return (error); 580191388Semax ipsec6_get_ulp(m, spidx, needport); 581191388Semax return (0); 582191388Semax#endif 583191388Semax default: 584191388Semax KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 585191388Semax printf("%s: " "unknown IP version %u, ignored.\n", 586191388Semax __func__, v)); 587191388Semax return (EINVAL); 588191388Semax } 589191388Semax} 590191388Semax 591191388Semaxstatic void 592181698Semaxipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport) 593181698Semax{ 594181698Semax u_int8_t nxt; 595181698Semax int off; 596181698Semax 597181698Semax /* Sanity check. */ 598181698Semax IPSEC_ASSERT(m != NULL, ("null mbuf")); 599181698Semax IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip),("packet too short")); 600181698Semax 601181698Semax if (m->m_len >= sizeof (struct ip)) { 602181698Semax struct ip *ip = mtod(m, struct ip *); 603181698Semax if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) 604181698Semax goto done; 605181698Semax off = ip->ip_hl << 2; 606181698Semax nxt = ip->ip_p; 607181698Semax } else { 608181698Semax struct ip ih; 609181698Semax 610181698Semax m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih); 611181698Semax if (ih.ip_off & htons(IP_MF | IP_OFFMASK)) 612121054Semax goto done; 613131631Sru off = ih.ip_hl << 2; 614121054Semax nxt = ih.ip_p; 615121054Semax } 616121054Semax 617121054Semax while (off < m->m_pkthdr.len) { 618121054Semax struct ip6_ext ip6e; 619121054Semax struct tcphdr th; 620121054Semax struct udphdr uh; 621121054Semax 622121054Semax switch (nxt) { 623121054Semax case IPPROTO_TCP: 624121054Semax spidx->ul_proto = nxt; 625121054Semax if (!needport) 626121054Semax goto done_proto; 627121054Semax if (off + sizeof(struct tcphdr) > m->m_pkthdr.len) 628121054Semax goto done; 629121054Semax m_copydata(m, off, sizeof (th), (caddr_t) &th); 630121054Semax spidx->src.sin.sin_port = th.th_sport; 631121054Semax spidx->dst.sin.sin_port = th.th_dport; 632121054Semax return; 633121054Semax case IPPROTO_UDP: 634121054Semax spidx->ul_proto = nxt; 635121054Semax if (!needport) 636121054Semax goto done_proto; 637121054Semax if (off + sizeof(struct udphdr) > m->m_pkthdr.len) 638121054Semax goto done; 639121054Semax m_copydata(m, off, sizeof (uh), (caddr_t) &uh); 640121054Semax spidx->src.sin.sin_port = uh.uh_sport; 641121054Semax spidx->dst.sin.sin_port = uh.uh_dport; 642121054Semax return; 643121054Semax case IPPROTO_AH: 644121054Semax if (off + sizeof(ip6e) > m->m_pkthdr.len) 645121054Semax goto done; 646121054Semax /* XXX Sigh, this works but is totally bogus. */ 647131631Sru m_copydata(m, off, sizeof(ip6e), (caddr_t) &ip6e); 648121054Semax off += (ip6e.ip6e_len + 2) << 2; 649121054Semax nxt = ip6e.ip6e_nxt; 650121054Semax break; 651121054Semax case IPPROTO_ICMP: 652121054Semax default: 653121054Semax /* XXX Intermediate headers??? */ 654121054Semax spidx->ul_proto = nxt; 655121054Semax goto done_proto; 656121054Semax } 657121054Semax } 658121054Semaxdone: 659131631Sru spidx->ul_proto = IPSEC_ULPROTO_ANY; 660121054Semaxdone_proto: 661121054Semax spidx->src.sin.sin_port = IPSEC_PORT_ANY; 662121054Semax spidx->dst.sin.sin_port = IPSEC_PORT_ANY; 663121054Semax} 664121054Semax 665121054Semax/* Assumes that m is sane. */ 666121054Semaxstatic int 667121054Semaxipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx) 668121054Semax{ 669121054Semax static const struct sockaddr_in template = { 670121054Semax sizeof (struct sockaddr_in), 671121054Semax AF_INET, 672131631Sru 0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 } 673131631Sru }; 674131631Sru 675121054Semax spidx->src.sin = template; 676121054Semax spidx->dst.sin = template; 677121054Semax 678140568Sru if (m->m_len < sizeof (struct ip)) { 679121054Semax m_copydata(m, offsetof(struct ip, ip_src), 680121054Semax sizeof (struct in_addr), 681121054Semax (caddr_t) &spidx->src.sin.sin_addr); 682189462Semax m_copydata(m, offsetof(struct ip, ip_dst), 683189462Semax sizeof (struct in_addr), 684121054Semax (caddr_t) &spidx->dst.sin.sin_addr); 685121054Semax } else { 686121054Semax struct ip *ip = mtod(m, struct ip *); 687121054Semax spidx->src.sin.sin_addr = ip->ip_src; 688121054Semax spidx->dst.sin.sin_addr = ip->ip_dst; 689121054Semax } 690121054Semax 691121054Semax spidx->prefs = sizeof(struct in_addr) << 3; 692121054Semax spidx->prefd = sizeof(struct in_addr) << 3; 693121054Semax 694121054Semax return (0); 695121054Semax} 696121054Semax 697121054Semax#ifdef INET6 698121054Semaxstatic void 699121054Semaxipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport) 700121054Semax{ 701121054Semax int off, nxt; 702121054Semax struct tcphdr th; 703121054Semax struct udphdr uh; 704121054Semax struct icmp6_hdr ih; 705121054Semax 706121054Semax /* Sanity check. */ 707121054Semax if (m == NULL) 708189462Semax panic("%s: NULL pointer was passed.\n", __func__); 709189462Semax 710189462Semax KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 711189462Semax printf("%s:\n", __func__); kdebug_mbuf(m)); 712189462Semax 713189462Semax /* Set default. */ 714189462Semax spidx->ul_proto = IPSEC_ULPROTO_ANY; 715189462Semax ((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY; 716189462Semax ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY; 717189462Semax 718189462Semax nxt = -1; 719189462Semax off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 720189462Semax if (off < 0 || m->m_pkthdr.len < off) 721189462Semax return; 722140505Sru 723140505Sru switch (nxt) { 724121054Semax case IPPROTO_TCP: 725191388Semax spidx->ul_proto = nxt; 726121054Semax if (!needport) 727121054Semax break; 728 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len) 729 break; 730 m_copydata(m, off, sizeof(th), (caddr_t)&th); 731 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport; 732 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport; 733 break; 734 case IPPROTO_UDP: 735 spidx->ul_proto = nxt; 736 if (!needport) 737 break; 738 if (off + sizeof(struct udphdr) > m->m_pkthdr.len) 739 break; 740 m_copydata(m, off, sizeof(uh), (caddr_t)&uh); 741 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport; 742 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport; 743 break; 744 case IPPROTO_ICMPV6: 745 spidx->ul_proto = nxt; 746 if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len) 747 break; 748 m_copydata(m, off, sizeof(ih), (caddr_t)&ih); 749 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = 750 htons((uint16_t)ih.icmp6_type); 751 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = 752 htons((uint16_t)ih.icmp6_code); 753 break; 754 default: 755 /* XXX Intermediate headers??? */ 756 spidx->ul_proto = nxt; 757 break; 758 } 759} 760 761/* Assumes that m is sane. */ 762static int 763ipsec6_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx) 764{ 765 struct ip6_hdr *ip6 = NULL; 766 struct ip6_hdr ip6buf; 767 struct sockaddr_in6 *sin6; 768 769 if (m->m_len >= sizeof(*ip6)) 770 ip6 = mtod(m, struct ip6_hdr *); 771 else { 772 m_copydata(m, 0, sizeof(ip6buf), (caddr_t)&ip6buf); 773 ip6 = &ip6buf; 774 } 775 776 sin6 = (struct sockaddr_in6 *)&spidx->src; 777 bzero(sin6, sizeof(*sin6)); 778 sin6->sin6_family = AF_INET6; 779 sin6->sin6_len = sizeof(struct sockaddr_in6); 780 bcopy(&ip6->ip6_src, &sin6->sin6_addr, sizeof(ip6->ip6_src)); 781 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) { 782 sin6->sin6_addr.s6_addr16[1] = 0; 783 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]); 784 } 785 spidx->prefs = sizeof(struct in6_addr) << 3; 786 787 sin6 = (struct sockaddr_in6 *)&spidx->dst; 788 bzero(sin6, sizeof(*sin6)); 789 sin6->sin6_family = AF_INET6; 790 sin6->sin6_len = sizeof(struct sockaddr_in6); 791 bcopy(&ip6->ip6_dst, &sin6->sin6_addr, sizeof(ip6->ip6_dst)); 792 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) { 793 sin6->sin6_addr.s6_addr16[1] = 0; 794 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]); 795 } 796 spidx->prefd = sizeof(struct in6_addr) << 3; 797 798 return (0); 799} 800#endif 801 802static void 803ipsec_delpcbpolicy(struct inpcbpolicy *p) 804{ 805 806 free(p, M_IPSEC_INPCB); 807} 808 809/* Initialize policy in PCB. */ 810int 811ipsec_init_policy(struct socket *so, struct inpcbpolicy **pcb_sp) 812{ 813 struct inpcbpolicy *new; 814 815 /* Sanity check. */ 816 if (so == NULL || pcb_sp == NULL) 817 panic("%s: NULL pointer was passed.\n", __func__); 818 819 new = (struct inpcbpolicy *) malloc(sizeof(struct inpcbpolicy), 820 M_IPSEC_INPCB, M_NOWAIT|M_ZERO); 821 if (new == NULL) { 822 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__)); 823 return (ENOBUFS); 824 } 825 826 new->priv = IPSEC_IS_PRIVILEGED_SO(so); 827 828 if ((new->sp_in = KEY_NEWSP()) == NULL) { 829 ipsec_delpcbpolicy(new); 830 return (ENOBUFS); 831 } 832 new->sp_in->state = IPSEC_SPSTATE_ALIVE; 833 new->sp_in->policy = IPSEC_POLICY_ENTRUST; 834 835 if ((new->sp_out = KEY_NEWSP()) == NULL) { 836 KEY_FREESP(&new->sp_in); 837 ipsec_delpcbpolicy(new); 838 return (ENOBUFS); 839 } 840 new->sp_out->state = IPSEC_SPSTATE_ALIVE; 841 new->sp_out->policy = IPSEC_POLICY_ENTRUST; 842 843 *pcb_sp = new; 844 845 return (0); 846} 847 848/* Copy old IPsec policy into new. */ 849int 850ipsec_copy_policy(struct inpcbpolicy *old, struct inpcbpolicy *new) 851{ 852 struct secpolicy *sp; 853 854 sp = ipsec_deepcopy_policy(old->sp_in); 855 if (sp) { 856 KEY_FREESP(&new->sp_in); 857 new->sp_in = sp; 858 } else 859 return (ENOBUFS); 860 861 sp = ipsec_deepcopy_policy(old->sp_out); 862 if (sp) { 863 KEY_FREESP(&new->sp_out); 864 new->sp_out = sp; 865 } else 866 return (ENOBUFS); 867 868 new->priv = old->priv; 869 870 return (0); 871} 872 873struct ipsecrequest * 874ipsec_newisr(void) 875{ 876 struct ipsecrequest *p; 877 878 p = malloc(sizeof(struct ipsecrequest), M_IPSEC_SR, M_NOWAIT|M_ZERO); 879 if (p != NULL) 880 IPSECREQUEST_LOCK_INIT(p); 881 return (p); 882} 883 884void 885ipsec_delisr(struct ipsecrequest *p) 886{ 887 888 IPSECREQUEST_LOCK_DESTROY(p); 889 free(p, M_IPSEC_SR); 890} 891 892/* Deep-copy a policy in PCB. */ 893static struct secpolicy * 894ipsec_deepcopy_policy(struct secpolicy *src) 895{ 896 struct ipsecrequest *newchain = NULL; 897 struct ipsecrequest *p; 898 struct ipsecrequest **q; 899 struct ipsecrequest *r; 900 struct secpolicy *dst; 901 902 if (src == NULL) 903 return (NULL); 904 dst = KEY_NEWSP(); 905 if (dst == NULL) 906 return (NULL); 907 908 /* 909 * Deep-copy IPsec request chain. This is required since struct 910 * ipsecrequest is not reference counted. 911 */ 912 q = &newchain; 913 for (p = src->req; p; p = p->next) { 914 *q = ipsec_newisr(); 915 if (*q == NULL) 916 goto fail; 917 (*q)->saidx.proto = p->saidx.proto; 918 (*q)->saidx.mode = p->saidx.mode; 919 (*q)->level = p->level; 920 (*q)->saidx.reqid = p->saidx.reqid; 921 922 bcopy(&p->saidx.src, &(*q)->saidx.src, sizeof((*q)->saidx.src)); 923 bcopy(&p->saidx.dst, &(*q)->saidx.dst, sizeof((*q)->saidx.dst)); 924 925 (*q)->sp = dst; 926 927 q = &((*q)->next); 928 } 929 930 dst->req = newchain; 931 dst->state = src->state; 932 dst->policy = src->policy; 933 /* Do not touch the refcnt fields. */ 934 935 return (dst); 936 937fail: 938 for (p = newchain; p; p = r) { 939 r = p->next; 940 ipsec_delisr(p); 941 p = NULL; 942 } 943 return (NULL); 944} 945 946/* Set policy and IPsec request if present. */ 947static int 948ipsec_set_policy_internal(struct secpolicy **pcb_sp, int optname, 949 caddr_t request, size_t len, struct ucred *cred) 950{ 951 struct sadb_x_policy *xpl; 952 struct secpolicy *newsp = NULL; 953 int error; 954 955 /* Sanity check. */ 956 if (pcb_sp == NULL || *pcb_sp == NULL || request == NULL) 957 return (EINVAL); 958 if (len < sizeof(*xpl)) 959 return (EINVAL); 960 xpl = (struct sadb_x_policy *)request; 961 962 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 963 printf("%s: passed policy\n", __func__); 964 kdebug_sadb_x_policy((struct sadb_ext *)xpl)); 965 966 /* Check policy type. */ 967 /* ipsec_set_policy_internal() accepts IPSEC, ENTRUST and BYPASS. */ 968 if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD 969 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE) 970 return (EINVAL); 971 972 /* Check privileged socket. */ 973 if (cred != NULL && xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) { 974 error = priv_check_cred(cred, PRIV_NETINET_IPSEC, 0); 975 if (error) 976 return (EACCES); 977 } 978 979 /* Allocating new SP entry. */ 980 if ((newsp = key_msg2sp(xpl, len, &error)) == NULL) 981 return (error); 982 983 newsp->state = IPSEC_SPSTATE_ALIVE; 984 985 /* Clear old SP and set new SP. */ 986 KEY_FREESP(pcb_sp); 987 *pcb_sp = newsp; 988 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 989 printf("%s: new policy\n", __func__); 990 kdebug_secpolicy(newsp)); 991 992 return (0); 993} 994 995int 996ipsec_set_policy(struct inpcb *inp, int optname, caddr_t request, 997 size_t len, struct ucred *cred) 998{ 999 struct sadb_x_policy *xpl; 1000 struct secpolicy **pcb_sp; 1001 1002 /* Sanity check. */ 1003 if (inp == NULL || request == NULL) 1004 return (EINVAL); 1005 if (len < sizeof(*xpl)) 1006 return (EINVAL); 1007 xpl = (struct sadb_x_policy *)request; 1008 1009 /* Select direction. */ 1010 switch (xpl->sadb_x_policy_dir) { 1011 case IPSEC_DIR_INBOUND: 1012 pcb_sp = &inp->inp_sp->sp_in; 1013 break; 1014 case IPSEC_DIR_OUTBOUND: 1015 pcb_sp = &inp->inp_sp->sp_out; 1016 break; 1017 default: 1018 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__, 1019 xpl->sadb_x_policy_dir)); 1020 return (EINVAL); 1021 } 1022 1023 return (ipsec_set_policy_internal(pcb_sp, optname, request, len, cred)); 1024} 1025 1026int 1027ipsec_get_policy(struct inpcb *inp, caddr_t request, size_t len, 1028 struct mbuf **mp) 1029{ 1030 struct sadb_x_policy *xpl; 1031 struct secpolicy *pcb_sp; 1032 1033 /* Sanity check. */ 1034 if (inp == NULL || request == NULL || mp == NULL) 1035 return (EINVAL); 1036 IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp")); 1037 if (len < sizeof(*xpl)) 1038 return (EINVAL); 1039 xpl = (struct sadb_x_policy *)request; 1040 1041 /* Select direction. */ 1042 switch (xpl->sadb_x_policy_dir) { 1043 case IPSEC_DIR_INBOUND: 1044 pcb_sp = inp->inp_sp->sp_in; 1045 break; 1046 case IPSEC_DIR_OUTBOUND: 1047 pcb_sp = inp->inp_sp->sp_out; 1048 break; 1049 default: 1050 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__, 1051 xpl->sadb_x_policy_dir)); 1052 return (EINVAL); 1053 } 1054 1055 /* Sanity check. Should be an IPSEC_ASSERT. */ 1056 if (pcb_sp == NULL) 1057 return (EINVAL); 1058 1059 *mp = key_sp2msg(pcb_sp); 1060 if (!*mp) { 1061 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__)); 1062 return (ENOBUFS); 1063 } 1064 1065 (*mp)->m_type = MT_DATA; 1066 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 1067 printf("%s:\n", __func__); kdebug_mbuf(*mp)); 1068 1069 return (0); 1070} 1071 1072/* Delete policy in PCB. */ 1073int 1074ipsec_delete_pcbpolicy(struct inpcb *inp) 1075{ 1076 IPSEC_ASSERT(inp != NULL, ("null inp")); 1077 1078 if (inp->inp_sp == NULL) 1079 return (0); 1080 1081 if (inp->inp_sp->sp_in != NULL) 1082 KEY_FREESP(&inp->inp_sp->sp_in); 1083 1084 if (inp->inp_sp->sp_out != NULL) 1085 KEY_FREESP(&inp->inp_sp->sp_out); 1086 1087 ipsec_delpcbpolicy(inp->inp_sp); 1088 inp->inp_sp = NULL; 1089 1090 return (0); 1091} 1092 1093/* 1094 * Return current level. 1095 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned. 1096 */ 1097u_int 1098ipsec_get_reqlevel(struct ipsecrequest *isr) 1099{ 1100 u_int level = 0; 1101 u_int esp_trans_deflev, esp_net_deflev; 1102 u_int ah_trans_deflev, ah_net_deflev; 1103 1104 IPSEC_ASSERT(isr != NULL && isr->sp != NULL, ("null argument")); 1105 IPSEC_ASSERT(isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family, 1106 ("af family mismatch, src %u, dst %u", 1107 isr->sp->spidx.src.sa.sa_family, 1108 isr->sp->spidx.dst.sa.sa_family)); 1109 1110/* XXX Note that we have ipseclog() expanded here - code sync issue. */ 1111#define IPSEC_CHECK_DEFAULT(lev) \ 1112 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \ 1113 && (lev) != IPSEC_LEVEL_UNIQUE) \ 1114 ? (V_ipsec_debug \ 1115 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\ 1116 (lev), IPSEC_LEVEL_REQUIRE) \ 1117 : 0), \ 1118 (lev) = IPSEC_LEVEL_REQUIRE, \ 1119 (lev) \ 1120 : (lev)) 1121 1122 /* Set default level. */ 1123 switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) { 1124#ifdef INET 1125 case AF_INET: 1126 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_trans_deflev); 1127 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_net_deflev); 1128 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_trans_deflev); 1129 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_net_deflev); 1130 break; 1131#endif 1132#ifdef INET6 1133 case AF_INET6: 1134 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_trans_deflev); 1135 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_net_deflev); 1136 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_trans_deflev); 1137 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_net_deflev); 1138 break; 1139#endif /* INET6 */ 1140 default: 1141 panic("%s: unknown af %u", 1142 __func__, isr->sp->spidx.src.sa.sa_family); 1143 } 1144 1145#undef IPSEC_CHECK_DEFAULT 1146 1147 /* Set level. */ 1148 switch (isr->level) { 1149 case IPSEC_LEVEL_DEFAULT: 1150 switch (isr->saidx.proto) { 1151 case IPPROTO_ESP: 1152 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) 1153 level = esp_net_deflev; 1154 else 1155 level = esp_trans_deflev; 1156 break; 1157 case IPPROTO_AH: 1158 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) 1159 level = ah_net_deflev; 1160 else 1161 level = ah_trans_deflev; 1162 break; 1163 case IPPROTO_IPCOMP: 1164 /* 1165 * We don't really care, as IPcomp document says that 1166 * we shouldn't compress small packets. 1167 */ 1168 level = IPSEC_LEVEL_USE; 1169 break; 1170 default: 1171 panic("%s: Illegal protocol defined %u\n", __func__, 1172 isr->saidx.proto); 1173 } 1174 break; 1175 1176 case IPSEC_LEVEL_USE: 1177 case IPSEC_LEVEL_REQUIRE: 1178 level = isr->level; 1179 break; 1180 case IPSEC_LEVEL_UNIQUE: 1181 level = IPSEC_LEVEL_REQUIRE; 1182 break; 1183 1184 default: 1185 panic("%s: Illegal IPsec level %u\n", __func__, isr->level); 1186 } 1187 1188 return (level); 1189} 1190 1191/* 1192 * Check security policy requirements against the actual 1193 * packet contents. Return one if the packet should be 1194 * reject as "invalid"; otherwiser return zero to have the 1195 * packet treated as "valid". 1196 * 1197 * OUT: 1198 * 0: valid 1199 * 1: invalid 1200 */ 1201int 1202ipsec_in_reject(struct secpolicy *sp, struct mbuf *m) 1203{ 1204 struct ipsecrequest *isr; 1205 int need_auth; 1206 1207 KEYDEBUG(KEYDEBUG_IPSEC_DATA, 1208 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp)); 1209 1210 /* Check policy. */ 1211 switch (sp->policy) { 1212 case IPSEC_POLICY_DISCARD: 1213 return (1); 1214 case IPSEC_POLICY_BYPASS: 1215 case IPSEC_POLICY_NONE: 1216 return (0); 1217 } 1218 1219 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC, 1220 ("invalid policy %u", sp->policy)); 1221 1222 /* XXX Should compare policy against IPsec header history. */ 1223 1224 need_auth = 0; 1225 for (isr = sp->req; isr != NULL; isr = isr->next) { 1226 if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE) 1227 continue; 1228 switch (isr->saidx.proto) { 1229 case IPPROTO_ESP: 1230 if ((m->m_flags & M_DECRYPTED) == 0) { 1231 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 1232 printf("%s: ESP m_flags:%x\n", __func__, 1233 m->m_flags)); 1234 return (1); 1235 } 1236 1237 if (!need_auth && 1238 isr->sav != NULL && 1239 isr->sav->tdb_authalgxform != NULL && 1240 (m->m_flags & M_AUTHIPDGM) == 0) { 1241 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 1242 printf("%s: ESP/AH m_flags:%x\n", __func__, 1243 m->m_flags)); 1244 return (1); 1245 } 1246 break; 1247 case IPPROTO_AH: 1248 need_auth = 1; 1249 if ((m->m_flags & M_AUTHIPHDR) == 0) { 1250 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 1251 printf("%s: AH m_flags:%x\n", __func__, 1252 m->m_flags)); 1253 return (1); 1254 } 1255 break; 1256 case IPPROTO_IPCOMP: 1257 /* 1258 * We don't really care, as IPcomp document 1259 * says that we shouldn't compress small 1260 * packets. IPComp policy should always be 1261 * treated as being in "use" level. 1262 */ 1263 break; 1264 } 1265 } 1266 return (0); /* Valid. */ 1267} 1268 1269static int 1270ipsec46_in_reject(struct mbuf *m, struct inpcb *inp) 1271{ 1272 struct secpolicy *sp; 1273 int error; 1274 int result; 1275 1276 IPSEC_ASSERT(m != NULL, ("null mbuf")); 1277 1278 /* 1279 * Get SP for this packet. 1280 * When we are called from ip_forward(), we call 1281 * ipsec_getpolicybyaddr() with IP_FORWARDING flag. 1282 */ 1283 if (inp == NULL) 1284 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error); 1285 else 1286 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error); 1287 1288 if (sp != NULL) { 1289 result = ipsec_in_reject(sp, m); 1290 KEY_FREESP(&sp); 1291 } else { 1292 result = 0; /* XXX Should be panic? 1293 * -> No, there may be error. */ 1294 } 1295 return (result); 1296} 1297 1298/* 1299 * Check AH/ESP integrity. 1300 * This function is called from tcp_input(), udp_input(), 1301 * and {ah,esp}4_input for tunnel mode. 1302 */ 1303int 1304ipsec4_in_reject(struct mbuf *m, struct inpcb *inp) 1305{ 1306 int result; 1307 1308 result = ipsec46_in_reject(m, inp); 1309 if (result) 1310 IPSECSTAT_INC(ips_in_polvio); 1311 1312 return (result); 1313} 1314 1315#ifdef INET6 1316/* 1317 * Check AH/ESP integrity. 1318 * This function is called from tcp6_input(), udp6_input(), 1319 * and {ah,esp}6_input for tunnel mode. 1320 */ 1321int 1322ipsec6_in_reject(struct mbuf *m, struct inpcb *inp) 1323{ 1324 int result; 1325 1326 result = ipsec46_in_reject(m, inp); 1327 if (result) 1328 IPSEC6STAT_INC(ips_in_polvio); 1329 1330 return (result); 1331} 1332#endif 1333 1334/* 1335 * Compute the byte size to be occupied by IPsec header. 1336 * In case it is tunnelled, it includes the size of outer IP header. 1337 * NOTE: SP passed is freed in this function. 1338 */ 1339static size_t 1340ipsec_hdrsiz_internal(struct secpolicy *sp) 1341{ 1342 struct ipsecrequest *isr; 1343 size_t size; 1344 1345 KEYDEBUG(KEYDEBUG_IPSEC_DATA, 1346 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp)); 1347 1348 switch (sp->policy) { 1349 case IPSEC_POLICY_DISCARD: 1350 case IPSEC_POLICY_BYPASS: 1351 case IPSEC_POLICY_NONE: 1352 return (0); 1353 } 1354 1355 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC, 1356 ("invalid policy %u", sp->policy)); 1357 1358 size = 0; 1359 for (isr = sp->req; isr != NULL; isr = isr->next) { 1360 size_t clen = 0; 1361 1362 switch (isr->saidx.proto) { 1363 case IPPROTO_ESP: 1364 clen = esp_hdrsiz(isr->sav); 1365 break; 1366 case IPPROTO_AH: 1367 clen = ah_hdrsiz(isr->sav); 1368 break; 1369 case IPPROTO_IPCOMP: 1370 clen = sizeof(struct ipcomp); 1371 break; 1372 } 1373 1374 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) { 1375 switch (isr->saidx.dst.sa.sa_family) { 1376 case AF_INET: 1377 clen += sizeof(struct ip); 1378 break; 1379#ifdef INET6 1380 case AF_INET6: 1381 clen += sizeof(struct ip6_hdr); 1382 break; 1383#endif 1384 default: 1385 ipseclog((LOG_ERR, "%s: unknown AF %d in " 1386 "IPsec tunnel SA\n", __func__, 1387 ((struct sockaddr *)&isr->saidx.dst)->sa_family)); 1388 break; 1389 } 1390 } 1391 size += clen; 1392 } 1393 1394 return (size); 1395} 1396 1397/* 1398 * This function is called from ipsec_hdrsiz_tcp(), ip_ipsec_mtu(), 1399 * disabled ip6_ipsec_mtu() and ip6_forward(). 1400 */ 1401size_t 1402ipsec_hdrsiz(struct mbuf *m, u_int dir, struct inpcb *inp) 1403{ 1404 struct secpolicy *sp; 1405 int error; 1406 size_t size; 1407 1408 IPSEC_ASSERT(m != NULL, ("null mbuf")); 1409 1410 /* Get SP for this packet. 1411 * When we are called from ip_forward(), we call 1412 * ipsec_getpolicybyaddr() with IP_FORWARDING flag. 1413 */ 1414 if (inp == NULL) 1415 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error); 1416 else 1417 sp = ipsec_getpolicybysock(m, dir, inp, &error); 1418 1419 if (sp != NULL) { 1420 size = ipsec_hdrsiz_internal(sp); 1421 KEYDEBUG(KEYDEBUG_IPSEC_DATA, 1422 printf("%s: size:%lu.\n", __func__, 1423 (unsigned long)size)); 1424 1425 KEY_FREESP(&sp); 1426 } else { 1427 size = 0; /* XXX Should be panic? 1428 * -> No, we are called w/o knowing if 1429 * IPsec processing is needed. */ 1430 } 1431 return (size); 1432} 1433 1434/* 1435 * Check the variable replay window. 1436 * ipsec_chkreplay() performs replay check before ICV verification. 1437 * ipsec_updatereplay() updates replay bitmap. This must be called after 1438 * ICV verification (it also performs replay check, which is usually done 1439 * beforehand). 1440 * 0 (zero) is returned if packet disallowed, 1 if packet permitted. 1441 * 1442 * Based on RFC 2401. 1443 */ 1444int 1445ipsec_chkreplay(u_int32_t seq, struct secasvar *sav) 1446{ 1447 const struct secreplay *replay; 1448 u_int32_t diff; 1449 int fr; 1450 u_int32_t wsizeb; /* Constant: bits of window size. */ 1451 int frlast; /* Constant: last frame. */ 1452 1453 IPSEC_ASSERT(sav != NULL, ("Null SA")); 1454 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state")); 1455 1456 replay = sav->replay; 1457 1458 if (replay->wsize == 0) 1459 return (1); /* No need to check replay. */ 1460 1461 /* Constant. */ 1462 frlast = replay->wsize - 1; 1463 wsizeb = replay->wsize << 3; 1464 1465 /* Sequence number of 0 is invalid. */ 1466 if (seq == 0) 1467 return (0); 1468 1469 /* First time is always okay. */ 1470 if (replay->count == 0) 1471 return (1); 1472 1473 if (seq > replay->lastseq) { 1474 /* Larger sequences are okay. */ 1475 return (1); 1476 } else { 1477 /* seq is equal or less than lastseq. */ 1478 diff = replay->lastseq - seq; 1479 1480 /* Over range to check, i.e. too old or wrapped. */ 1481 if (diff >= wsizeb) 1482 return (0); 1483 1484 fr = frlast - diff / 8; 1485 1486 /* This packet already seen? */ 1487 if ((replay->bitmap)[fr] & (1 << (diff % 8))) 1488 return (0); 1489 1490 /* Out of order but good. */ 1491 return (1); 1492 } 1493} 1494 1495/* 1496 * Check replay counter whether to update or not. 1497 * OUT: 0: OK 1498 * 1: NG 1499 */ 1500int 1501ipsec_updatereplay(u_int32_t seq, struct secasvar *sav) 1502{ 1503 struct secreplay *replay; 1504 u_int32_t diff; 1505 int fr; 1506 u_int32_t wsizeb; /* Constant: bits of window size. */ 1507 int frlast; /* Constant: last frame. */ 1508 1509 IPSEC_ASSERT(sav != NULL, ("Null SA")); 1510 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state")); 1511 1512 replay = sav->replay; 1513 1514 if (replay->wsize == 0) 1515 goto ok; /* No need to check replay. */ 1516 1517 /* Constant. */ 1518 frlast = replay->wsize - 1; 1519 wsizeb = replay->wsize << 3; 1520 1521 /* Sequence number of 0 is invalid. */ 1522 if (seq == 0) 1523 return (1); 1524 1525 /* First time. */ 1526 if (replay->count == 0) { 1527 replay->lastseq = seq; 1528 bzero(replay->bitmap, replay->wsize); 1529 (replay->bitmap)[frlast] = 1; 1530 goto ok; 1531 } 1532 1533 if (seq > replay->lastseq) { 1534 /* seq is larger than lastseq. */ 1535 diff = seq - replay->lastseq; 1536 1537 /* New larger sequence number. */ 1538 if (diff < wsizeb) { 1539 /* In window. */ 1540 /* Set bit for this packet. */ 1541 vshiftl(replay->bitmap, diff, replay->wsize); 1542 (replay->bitmap)[frlast] |= 1; 1543 } else { 1544 /* This packet has a "way larger". */ 1545 bzero(replay->bitmap, replay->wsize); 1546 (replay->bitmap)[frlast] = 1; 1547 } 1548 replay->lastseq = seq; 1549 1550 /* Larger is good. */ 1551 } else { 1552 /* seq is equal or less than lastseq. */ 1553 diff = replay->lastseq - seq; 1554 1555 /* Over range to check, i.e. too old or wrapped. */ 1556 if (diff >= wsizeb) 1557 return (1); 1558 1559 fr = frlast - diff / 8; 1560 1561 /* This packet already seen? */ 1562 if ((replay->bitmap)[fr] & (1 << (diff % 8))) 1563 return (1); 1564 1565 /* Mark as seen. */ 1566 (replay->bitmap)[fr] |= (1 << (diff % 8)); 1567 1568 /* Out of order but good. */ 1569 } 1570 1571ok: 1572 if (replay->count == ~0) { 1573 1574 /* Set overflow flag. */ 1575 replay->overflow++; 1576 1577 /* Don't increment, no more packets accepted. */ 1578 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) 1579 return (1); 1580 1581 ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n", 1582 __func__, replay->overflow, ipsec_logsastr(sav))); 1583 } 1584 1585 replay->count++; 1586 1587 return (0); 1588} 1589 1590/* 1591 * Shift variable length buffer to left. 1592 * IN: bitmap: pointer to the buffer 1593 * nbit: the number of to shift. 1594 * wsize: buffer size (bytes). 1595 */ 1596static void 1597vshiftl(unsigned char *bitmap, int nbit, int wsize) 1598{ 1599 int s, j, i; 1600 unsigned char over; 1601 1602 for (j = 0; j < nbit; j += 8) { 1603 s = (nbit - j < 8) ? (nbit - j): 8; 1604 bitmap[0] <<= s; 1605 for (i = 1; i < wsize; i++) { 1606 over = (bitmap[i] >> (8 - s)); 1607 bitmap[i] <<= s; 1608 bitmap[i-1] |= over; 1609 } 1610 } 1611} 1612 1613#ifdef INET 1614/* Return a printable string for the IPv4 address. */ 1615static char * 1616inet_ntoa4(struct in_addr ina) 1617{ 1618 static char buf[4][4 * sizeof "123" + 4]; 1619 unsigned char *ucp = (unsigned char *) &ina; 1620 static int i = 3; 1621 1622 /* XXX-BZ Returns static buffer. */ 1623 i = (i + 1) % 4; 1624 sprintf(buf[i], "%d.%d.%d.%d", ucp[0] & 0xff, ucp[1] & 0xff, 1625 ucp[2] & 0xff, ucp[3] & 0xff); 1626 return (buf[i]); 1627} 1628#endif 1629 1630/* Return a printable string for the address. */ 1631char * 1632ipsec_address(union sockaddr_union* sa) 1633{ 1634#ifdef INET6 1635 char ip6buf[INET6_ADDRSTRLEN]; 1636#endif 1637 1638 switch (sa->sa.sa_family) { 1639#ifdef INET 1640 case AF_INET: 1641 return (inet_ntoa4(sa->sin.sin_addr)); 1642#endif /* INET */ 1643#ifdef INET6 1644 case AF_INET6: 1645 return (ip6_sprintf(ip6buf, &sa->sin6.sin6_addr)); 1646#endif /* INET6 */ 1647 default: 1648 return ("(unknown address family)"); 1649 } 1650} 1651 1652const char * 1653ipsec_logsastr(struct secasvar *sav) 1654{ 1655 static char buf[256]; 1656 char *p; 1657 struct secasindex *saidx = &sav->sah->saidx; 1658 1659 IPSEC_ASSERT(saidx->src.sa.sa_family == saidx->dst.sa.sa_family, 1660 ("address family mismatch")); 1661 1662 p = buf; 1663 snprintf(buf, sizeof(buf), "SA(SPI=%u ", (u_int32_t)ntohl(sav->spi)); 1664 while (p && *p) 1665 p++; 1666 /* NB: only use ipsec_address on one address at a time. */ 1667 snprintf(p, sizeof (buf) - (p - buf), "src=%s ", 1668 ipsec_address(&saidx->src)); 1669 while (p && *p) 1670 p++; 1671 snprintf(p, sizeof (buf) - (p - buf), "dst=%s)", 1672 ipsec_address(&saidx->dst)); 1673 1674 return (buf); 1675} 1676 1677void 1678ipsec_dumpmbuf(struct mbuf *m) 1679{ 1680 int totlen; 1681 int i; 1682 u_char *p; 1683 1684 totlen = 0; 1685 printf("---\n"); 1686 while (m) { 1687 p = mtod(m, u_char *); 1688 for (i = 0; i < m->m_len; i++) { 1689 printf("%02x ", p[i]); 1690 totlen++; 1691 if (totlen % 16 == 0) 1692 printf("\n"); 1693 } 1694 m = m->m_next; 1695 } 1696 if (totlen % 16 != 0) 1697 printf("\n"); 1698 printf("---\n"); 1699} 1700 1701static void 1702ipsec_init(const void *unused __unused) 1703{ 1704 1705 SECPOLICY_LOCK_INIT(&V_ip4_def_policy); 1706 V_ip4_def_policy.refcnt = 1; /* NB: disallow free. */ 1707} 1708VNET_SYSINIT(ipsec_init, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, ipsec_init, 1709 NULL); 1710 1711 1712/* XXX This stuff doesn't belong here... */ 1713 1714static struct xformsw* xforms = NULL; 1715 1716/* 1717 * Register a transform; typically at system startup. 1718 */ 1719void 1720xform_register(struct xformsw* xsp) 1721{ 1722 1723 xsp->xf_next = xforms; 1724 xforms = xsp; 1725} 1726 1727/* 1728 * Initialize transform support in an sav. 1729 */ 1730int 1731xform_init(struct secasvar *sav, int xftype) 1732{ 1733 struct xformsw *xsp; 1734 1735 if (sav->tdb_xform != NULL) /* Previously initialized. */ 1736 return (0); 1737 for (xsp = xforms; xsp; xsp = xsp->xf_next) 1738 if (xsp->xf_type == xftype) 1739 return ((*xsp->xf_init)(sav, xsp)); 1740 return (EINVAL); 1741} 1742