ipsec.h revision 319599
1133808Spjd/* $FreeBSD: stable/11/sys/netipsec/ipsec.h 319599 2017-06-05 11:11:07Z ae $ */ 2133808Spjd/* $KAME: ipsec.h,v 1.53 2001/11/20 08:32:38 itojun Exp $ */ 3133808Spjd 4133808Spjd/*- 5133808Spjd * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6133808Spjd * All rights reserved. 7133808Spjd * 8133808Spjd * Redistribution and use in source and binary forms, with or without 9133808Spjd * modification, are permitted provided that the following conditions 10133808Spjd * are met: 11133808Spjd * 1. Redistributions of source code must retain the above copyright 12133808Spjd * notice, this list of conditions and the following disclaimer. 13133808Spjd * 2. Redistributions in binary form must reproduce the above copyright 14133808Spjd * notice, this list of conditions and the following disclaimer in the 15133808Spjd * documentation and/or other materials provided with the distribution. 16133808Spjd * 3. Neither the name of the project nor the names of its contributors 17133808Spjd * may be used to endorse or promote products derived from this software 18133808Spjd * without specific prior written permission. 19133808Spjd * 20133808Spjd * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21133808Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22133808Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23133808Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24133808Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25133808Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26133808Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27133808Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28133808Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29133808Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30133808Spjd * SUCH DAMAGE. 31133808Spjd */ 32133808Spjd 33133808Spjd/* 34133808Spjd * IPsec controller part. 35133808Spjd */ 36133808Spjd 37133808Spjd#ifndef _NETIPSEC_IPSEC_H_ 38133808Spjd#define _NETIPSEC_IPSEC_H_ 39133808Spjd 40133808Spjd#if defined(_KERNEL) && !defined(_LKM) && !defined(KLD_MODULE) 41133808Spjd#include "opt_inet.h" 42133808Spjd#include "opt_ipsec.h" 43133808Spjd#endif 44133808Spjd 45133808Spjd#include <net/pfkeyv2.h> 46133808Spjd#include <netipsec/keydb.h> 47133808Spjd 48133808Spjd#ifdef _KERNEL 49133808Spjd 50133808Spjd#include <sys/_lock.h> 51133808Spjd#include <sys/_mutex.h> 52133808Spjd#include <sys/_rwlock.h> 53133808Spjd 54133808Spjd#define IPSEC_ASSERT(_c,_m) KASSERT(_c, _m) 55133808Spjd 56133808Spjd/* 57133808Spjd * Security Policy Index 58133808Spjd * Ensure that both address families in the "src" and "dst" are same. 59133808Spjd * When the value of the ul_proto is ICMPv6, the port field in "src" 60133808Spjd * specifies ICMPv6 type, and the port field in "dst" specifies ICMPv6 code. 61134124Spjd */ 62134124Spjdstruct secpolicyindex { 63134168Spjd union sockaddr_union src; /* IP src address for SP */ 64134168Spjd union sockaddr_union dst; /* IP dst address for SP */ 65133808Spjd uint8_t ul_proto; /* upper layer Protocol */ 66133808Spjd uint8_t dir; /* direction of packet flow */ 67133808Spjd uint8_t prefs; /* prefix length in bits for src */ 68133808Spjd uint8_t prefd; /* prefix length in bits for dst */ 69133808Spjd}; 70133808Spjd 71133808Spjd/* Request for IPsec */ 72133808Spjdstruct ipsecrequest { 73133808Spjd struct secasindex saidx;/* hint for search proper SA */ 74133808Spjd /* if __ss_len == 0 then no address specified.*/ 75133808Spjd u_int level; /* IPsec level defined below. */ 76133808Spjd}; 77133808Spjd 78133808Spjd/* Security Policy Data Base */ 79133808Spjdstruct secpolicy { 80134124Spjd TAILQ_ENTRY(secpolicy) chain; 81134168Spjd LIST_ENTRY(secpolicy) idhash; 82133808Spjd LIST_ENTRY(secpolicy) drainq; 83133808Spjd 84133808Spjd struct secpolicyindex spidx; /* selector */ 85133808Spjd#define IPSEC_MAXREQ 4 86133808Spjd struct ipsecrequest *req[IPSEC_MAXREQ]; 87133808Spjd u_int tcount; /* IPsec transforms count */ 88133808Spjd volatile u_int refcnt; /* reference count */ 89133808Spjd u_int policy; /* policy_type per pfkeyv2.h */ 90133808Spjd u_int state; 91133808Spjd#define IPSEC_SPSTATE_DEAD 0 92133808Spjd#define IPSEC_SPSTATE_LARVAL 1 93133808Spjd#define IPSEC_SPSTATE_ALIVE 2 94133808Spjd#define IPSEC_SPSTATE_PCB 3 95133808Spjd#define IPSEC_SPSTATE_IFNET 4 96133808Spjd uint32_t priority; /* priority of this policy */ 97133808Spjd uint32_t id; /* It's unique number on the system. */ 98133808Spjd /* 99133808Spjd * lifetime handler. 100133808Spjd * the policy can be used without limitiation if both lifetime and 101133808Spjd * validtime are zero. 102133808Spjd * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime. 103133808Spjd * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime. 104133808Spjd */ 105133808Spjd time_t created; /* time created the policy */ 106133808Spjd time_t lastused; /* updated every when kernel sends a packet */ 107133808Spjd long lifetime; /* duration of the lifetime of this policy */ 108134168Spjd long validtime; /* duration this policy is valid without use */ 109133808Spjd}; 110133808Spjd 111134168Spjd/* 112133808Spjd * PCB security policies. 113133808Spjd * Application can setup private security policies for socket. 114133808Spjd * Such policies can have IPSEC, BYPASS and ENTRUST type. 115133960Spjd * By default, policies are set to NULL. This means that they have ENTRUST type. 116133808Spjd * When application sets BYPASS or IPSEC type policy, the flags field 117133808Spjd * is also updated. When flags is not set, the system could store 118133808Spjd * used security policy into the sp_in/sp_out pointer to speed up further 119133808Spjd * lookups. 120133808Spjd */ 121133808Spjdstruct inpcbpolicy { 122133808Spjd struct secpolicy *sp_in; 123133808Spjd struct secpolicy *sp_out; 124133808Spjd 125133808Spjd uint32_t genid; 126133808Spjd uint16_t flags; 127133808Spjd#define INP_INBOUND_POLICY 0x0001 128133808Spjd#define INP_OUTBOUND_POLICY 0x0002 129133808Spjd uint16_t hdrsz; 130133808Spjd}; 131133808Spjd 132133808Spjd/* SP acquiring list table. */ 133133808Spjdstruct secspacq { 134133808Spjd LIST_ENTRY(secspacq) chain; 135133808Spjd 136133808Spjd struct secpolicyindex spidx; 137133808Spjd 138133808Spjd time_t created; /* for lifetime */ 139133808Spjd int count; /* for lifetime */ 140133808Spjd /* XXX: here is mbuf place holder to be sent ? */ 141133808Spjd}; 142133808Spjd#endif /* _KERNEL */ 143133808Spjd 144133808Spjd/* buffer size for formatted output of ipsec address */ 145133808Spjd#define IPSEC_ADDRSTRLEN (INET6_ADDRSTRLEN + 11) 146133808Spjd 147133808Spjd/* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */ 148133808Spjd#define IPSEC_PORT_ANY 0 149133808Spjd#define IPSEC_ULPROTO_ANY 255 150134168Spjd#define IPSEC_PROTO_ANY 255 151134124Spjd 152133808Spjd/* mode of security protocol */ 153133808Spjd/* NOTE: DON'T use IPSEC_MODE_ANY at SPD. It's only use in SAD */ 154133808Spjd#define IPSEC_MODE_ANY 0 /* i.e. wildcard. */ 155133808Spjd#define IPSEC_MODE_TRANSPORT 1 156133808Spjd#define IPSEC_MODE_TUNNEL 2 157133808Spjd#define IPSEC_MODE_TCPMD5 3 /* TCP MD5 mode */ 158133808Spjd 159133808Spjd/* 160133808Spjd * Direction of security policy. 161133808Spjd * NOTE: Since INVALID is used just as flag. 162133808Spjd * The other are used for loop counter too. 163133808Spjd */ 164133808Spjd#define IPSEC_DIR_ANY 0 165133808Spjd#define IPSEC_DIR_INBOUND 1 166133808Spjd#define IPSEC_DIR_OUTBOUND 2 167133808Spjd#define IPSEC_DIR_MAX 3 168133808Spjd#define IPSEC_DIR_INVALID 4 169133808Spjd 170133808Spjd/* Policy level */ 171133808Spjd/* 172133808Spjd * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB, 173133808Spjd * DISCARD, IPSEC and NONE are allowed for setkey() in SPD. 174133808Spjd * DISCARD and NONE are allowed for system default. 175133808Spjd */ 176133808Spjd#define IPSEC_POLICY_DISCARD 0 /* discarding packet */ 177133808Spjd#define IPSEC_POLICY_NONE 1 /* through IPsec engine */ 178133808Spjd#define IPSEC_POLICY_IPSEC 2 /* do IPsec */ 179133808Spjd#define IPSEC_POLICY_ENTRUST 3 /* consulting SPD if present. */ 180133808Spjd#define IPSEC_POLICY_BYPASS 4 /* only for privileged socket. */ 181133808Spjd 182133808Spjd/* Policy scope */ 183133808Spjd#define IPSEC_POLICYSCOPE_ANY 0x00 /* unspecified */ 184133808Spjd#define IPSEC_POLICYSCOPE_GLOBAL 0x01 /* global scope */ 185133808Spjd#define IPSEC_POLICYSCOPE_IFNET 0x02 /* if_ipsec(4) scope */ 186133808Spjd#define IPSEC_POLICYSCOPE_PCB 0x04 /* PCB scope */ 187133808Spjd 188133808Spjd/* Security protocol level */ 189133808Spjd#define IPSEC_LEVEL_DEFAULT 0 /* reference to system default */ 190133808Spjd#define IPSEC_LEVEL_USE 1 /* use SA if present. */ 191133808Spjd#define IPSEC_LEVEL_REQUIRE 2 /* require SA. */ 192133808Spjd#define IPSEC_LEVEL_UNIQUE 3 /* unique SA. */ 193133808Spjd 194134124Spjd#define IPSEC_MANUAL_REQID_MAX 0x3fff 195134124Spjd /* 196134124Spjd * if security policy level == unique, this id 197134124Spjd * indicate to a relative SA for use, else is 198134124Spjd * zero. 199134124Spjd * 1 - 0x3fff are reserved for manual keying. 200134124Spjd * 0 are reserved for above reason. Others is 201134168Spjd * for kernel use. 202134168Spjd * Note that this id doesn't identify SA 203134168Spjd * by only itself. 204134168Spjd */ 205134168Spjd#define IPSEC_REPLAYWSIZE 32 206134168Spjd 207134168Spjd/* statistics for ipsec processing */ 208134168Spjdstruct ipsecstat { 209134168Spjd uint64_t ips_in_polvio; /* input: sec policy violation */ 210134168Spjd uint64_t ips_in_nomem; /* input: no memory available */ 211134168Spjd uint64_t ips_in_inval; /* input: generic error */ 212133808Spjd 213133808Spjd uint64_t ips_out_polvio; /* output: sec policy violation */ 214133808Spjd uint64_t ips_out_nosa; /* output: SA unavailable */ 215133808Spjd uint64_t ips_out_nomem; /* output: no memory available */ 216133808Spjd uint64_t ips_out_noroute; /* output: no route available */ 217133808Spjd uint64_t ips_out_inval; /* output: generic error */ 218133808Spjd uint64_t ips_out_bundlesa; /* output: bundled SA processed */ 219133808Spjd 220133808Spjd uint64_t ips_mbcoalesced; /* mbufs coalesced during clone */ 221133808Spjd uint64_t ips_clcoalesced; /* clusters coalesced during clone */ 222133808Spjd uint64_t ips_clcopied; /* clusters copied during clone */ 223133808Spjd uint64_t ips_mbinserted; /* mbufs inserted during makespace */ 224133808Spjd /* 225133808Spjd * Temporary statistics for performance analysis. 226133808Spjd */ 227133808Spjd /* See where ESP/AH/IPCOMP header land in mbuf on input */ 228133808Spjd uint64_t ips_input_front; 229133808Spjd uint64_t ips_input_middle; 230133808Spjd uint64_t ips_input_end; 231133808Spjd}; 232133808Spjd 233133808Spjd/* 234133808Spjd * Definitions for IPsec & Key sysctl operations. 235133808Spjd */ 236133808Spjd#define IPSECCTL_STATS 1 /* stats */ 237133808Spjd#define IPSECCTL_DEF_POLICY 2 238133808Spjd#define IPSECCTL_DEF_ESP_TRANSLEV 3 /* int; ESP transport mode */ 239133808Spjd#define IPSECCTL_DEF_ESP_NETLEV 4 /* int; ESP tunnel mode */ 240133808Spjd#define IPSECCTL_DEF_AH_TRANSLEV 5 /* int; AH transport mode */ 241133808Spjd#define IPSECCTL_DEF_AH_NETLEV 6 /* int; AH tunnel mode */ 242133808Spjd#if 0 /* obsolete, do not reuse */ 243133808Spjd#define IPSECCTL_INBOUND_CALL_IKE 7 244133808Spjd#endif 245133808Spjd#define IPSECCTL_AH_CLEARTOS 8 246133808Spjd#define IPSECCTL_AH_OFFSETMASK 9 247133808Spjd#define IPSECCTL_DFBIT 10 248133808Spjd#define IPSECCTL_ECN 11 249133808Spjd#define IPSECCTL_DEBUG 12 250133808Spjd#define IPSECCTL_ESP_RANDPAD 13 251133808Spjd 252133808Spjd#ifdef _KERNEL 253133808Spjd#include <sys/counter.h> 254133808Spjd 255133808Spjdstruct ipsec_ctx_data; 256133808Spjd#define IPSEC_INIT_CTX(_ctx, _mp, _sav, _af, _enc) do { \ 257133808Spjd (_ctx)->mp = (_mp); \ 258133808Spjd (_ctx)->sav = (_sav); \ 259133808Spjd (_ctx)->af = (_af); \ 260133808Spjd (_ctx)->enc = (_enc); \ 261133808Spjd} while(0) 262133808Spjdint ipsec_run_hhooks(struct ipsec_ctx_data *ctx, int direction); 263133808Spjd 264133808SpjdVNET_DECLARE(int, ipsec_debug); 265133808Spjd#define V_ipsec_debug VNET(ipsec_debug) 266133808Spjd 267133808Spjd#ifdef REGRESSION 268133808SpjdVNET_DECLARE(int, ipsec_replay); 269133808SpjdVNET_DECLARE(int, ipsec_integrity); 270133808Spjd 271133808Spjd#define V_ipsec_replay VNET(ipsec_replay) 272133808Spjd#define V_ipsec_integrity VNET(ipsec_integrity) 273133808Spjd#endif 274133808Spjd 275133808SpjdVNET_PCPUSTAT_DECLARE(struct ipsecstat, ipsec4stat); 276133808SpjdVNET_DECLARE(int, ip4_esp_trans_deflev); 277133808SpjdVNET_DECLARE(int, ip4_esp_net_deflev); 278133808SpjdVNET_DECLARE(int, ip4_ah_trans_deflev); 279133808SpjdVNET_DECLARE(int, ip4_ah_net_deflev); 280133808SpjdVNET_DECLARE(int, ip4_ah_offsetmask); 281133808SpjdVNET_DECLARE(int, ip4_ipsec_dfbit); 282133808SpjdVNET_DECLARE(int, ip4_ipsec_ecn); 283133808SpjdVNET_DECLARE(int, ip4_esp_randpad); 284133808SpjdVNET_DECLARE(int, crypto_support); 285133808SpjdVNET_DECLARE(int, natt_cksum_policy); 286133808Spjd 287133808Spjd#define IPSECSTAT_INC(name) \ 288133808Spjd VNET_PCPUSTAT_ADD(struct ipsecstat, ipsec4stat, name, 1) 289133808Spjd#define V_ip4_esp_trans_deflev VNET(ip4_esp_trans_deflev) 290133808Spjd#define V_ip4_esp_net_deflev VNET(ip4_esp_net_deflev) 291133808Spjd#define V_ip4_ah_trans_deflev VNET(ip4_ah_trans_deflev) 292133808Spjd#define V_ip4_ah_net_deflev VNET(ip4_ah_net_deflev) 293133808Spjd#define V_ip4_ah_offsetmask VNET(ip4_ah_offsetmask) 294133808Spjd#define V_ip4_ipsec_dfbit VNET(ip4_ipsec_dfbit) 295133808Spjd#define V_ip4_ipsec_ecn VNET(ip4_ipsec_ecn) 296133808Spjd#define V_ip4_esp_randpad VNET(ip4_esp_randpad) 297133808Spjd#define V_crypto_support VNET(crypto_support) 298133808Spjd#define V_natt_cksum_policy VNET(natt_cksum_policy) 299133808Spjd 300133808Spjd#define ipseclog(x) do { if (V_ipsec_debug) log x; } while (0) 301133808Spjd/* for openbsd compatibility */ 302133808Spjd#ifdef IPSEC_DEBUG 303133808Spjd#define IPSEC_DEBUG_DECLARE(x) x 304133808Spjd#define DPRINTF(x) do { if (V_ipsec_debug) printf x; } while (0) 305133808Spjd#else 306133808Spjd#define IPSEC_DEBUG_DECLARE(x) 307133808Spjd#define DPRINTF(x) 308133808Spjd#endif 309133808Spjd 310133808Spjdstruct inpcb; 311133808Spjdstruct m_tag; 312133808Spjdstruct secasvar; 313133808Spjdstruct sockopt; 314133808Spjdstruct tcphdr; 315133808Spjdunion sockaddr_union; 316133808Spjd 317133808Spjdint ipsec_if_input(struct mbuf *, struct secasvar *, uint32_t); 318133808Spjd 319133808Spjdstruct ipsecrequest *ipsec_newisr(void); 320133808Spjdvoid ipsec_delisr(struct ipsecrequest *); 321133808Spjdstruct secpolicy *ipsec4_checkpolicy(const struct mbuf *, struct inpcb *, 322133808Spjd int *); 323133808Spjd 324133808Spjdu_int ipsec_get_reqlevel(struct secpolicy *, u_int); 325133808Spjd 326133808Spjdvoid udp_ipsec_adjust_cksum(struct mbuf *, struct secasvar *, int, int); 327133808Spjdint udp_ipsec_output(struct mbuf *, struct secasvar *); 328133808Spjdint udp_ipsec_input(struct mbuf *, int, int); 329133808Spjdint udp_ipsec_pcbctl(struct inpcb *, struct sockopt *); 330133808Spjd 331133808Spjdint ipsec_chkreplay(uint32_t, struct secasvar *); 332133808Spjdint ipsec_updatereplay(uint32_t, struct secasvar *); 333133808Spjdint ipsec_updateid(struct secasvar *, uint64_t *, uint64_t *); 334133808Spjdint ipsec_initialized(void); 335133808Spjd 336133808Spjdvoid ipsec_setspidx_inpcb(struct inpcb *, struct secpolicyindex *, u_int); 337133808Spjd 338133808Spjdvoid ipsec4_setsockaddrs(const struct mbuf *, union sockaddr_union *, 339133808Spjd union sockaddr_union *); 340133808Spjdint ipsec4_in_reject(const struct mbuf *, struct inpcb *); 341133808Spjdint ipsec4_input(struct mbuf *, int, int); 342133808Spjdint ipsec4_forward(struct mbuf *); 343133808Spjdint ipsec4_pcbctl(struct inpcb *, struct sockopt *); 344133808Spjdint ipsec4_output(struct mbuf *, struct inpcb *); 345133808Spjdint ipsec4_capability(struct mbuf *, u_int); 346133808Spjdint ipsec4_common_input_cb(struct mbuf *, struct secasvar *, int, int); 347133808Spjdint ipsec4_process_packet(struct mbuf *, struct secpolicy *, struct inpcb *); 348133808Spjdint ipsec_process_done(struct mbuf *, struct secpolicy *, struct secasvar *, 349133808Spjd u_int); 350133808Spjd 351133808Spjdextern void m_checkalignment(const char* where, struct mbuf *m0, 352133808Spjd int off, int len); 353133808Spjdextern struct mbuf *m_makespace(struct mbuf *m0, int skip, int hlen, int *off); 354133808Spjdextern caddr_t m_pad(struct mbuf *m, int n); 355133808Spjdextern int m_striphdr(struct mbuf *m, int skip, int hlen); 356133808Spjd 357133808Spjd#endif /* _KERNEL */ 358133808Spjd 359133808Spjd#ifndef _KERNEL 360133808Spjdextern caddr_t ipsec_set_policy(char *, int); 361133808Spjdextern int ipsec_get_policylen(caddr_t); 362133808Spjdextern char *ipsec_dump_policy(caddr_t, char *); 363133808Spjdextern const char *ipsec_strerror(void); 364133808Spjd 365133808Spjd#endif /* ! KERNEL */ 366 367#endif /* _NETIPSEC_IPSEC_H_ */ 368