ipsec.h revision 179067
1/* $FreeBSD: head/sys/netipsec/ipsec.h 179067 2008-05-17 04:00:11Z gnn $ */ 2/* $KAME: ipsec.h,v 1.53 2001/11/20 08:32:38 itojun Exp $ */ 3 4/*- 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33/* 34 * IPsec controller part. 35 */ 36 37#ifndef _NETIPSEC_IPSEC_H_ 38#define _NETIPSEC_IPSEC_H_ 39 40#if defined(_KERNEL) && !defined(_LKM) && !defined(KLD_MODULE) 41#include "opt_inet.h" 42#include "opt_ipsec.h" 43#endif 44 45#include <net/pfkeyv2.h> 46#include <netipsec/keydb.h> 47 48#ifdef _KERNEL 49 50#define IPSEC_SPLASSERT_SOFTNET(_m) /* XXX-BZ remove me */ 51#define IPSEC_ASSERT(_c,_m) KASSERT(_c, _m) 52 53#define IPSEC_IS_PRIVILEGED_SO(_so) \ 54 ((_so)->so_cred != NULL && \ 55 priv_check_cred((_so)->so_cred, PRIV_NETINET_IPSEC, 0) \ 56 == 0) 57 58/* 59 * Security Policy Index 60 * Ensure that both address families in the "src" and "dst" are same. 61 * When the value of the ul_proto is ICMPv6, the port field in "src" 62 * specifies ICMPv6 type, and the port field in "dst" specifies ICMPv6 code. 63 */ 64struct secpolicyindex { 65 u_int8_t dir; /* direction of packet flow, see blow */ 66 union sockaddr_union src; /* IP src address for SP */ 67 union sockaddr_union dst; /* IP dst address for SP */ 68 u_int8_t prefs; /* prefix length in bits for src */ 69 u_int8_t prefd; /* prefix length in bits for dst */ 70 u_int16_t ul_proto; /* upper layer Protocol */ 71#ifdef notyet 72 uid_t uids; 73 uid_t uidd; 74 gid_t gids; 75 gid_t gidd; 76#endif 77}; 78 79/* Security Policy Data Base */ 80struct secpolicy { 81 LIST_ENTRY(secpolicy) chain; 82 struct mtx lock; 83 84 u_int refcnt; /* reference count */ 85 struct secpolicyindex spidx; /* selector */ 86 u_int32_t id; /* It's unique number on the system. */ 87 u_int state; /* 0: dead, others: alive */ 88#define IPSEC_SPSTATE_DEAD 0 89#define IPSEC_SPSTATE_ALIVE 1 90 u_int16_t policy; /* policy_type per pfkeyv2.h */ 91 u_int16_t scangen; /* scan generation # */ 92 struct ipsecrequest *req; 93 /* pointer to the ipsec request tree, */ 94 /* if policy == IPSEC else this value == NULL.*/ 95 96 /* 97 * lifetime handler. 98 * the policy can be used without limitiation if both lifetime and 99 * validtime are zero. 100 * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime. 101 * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime. 102 */ 103 time_t created; /* time created the policy */ 104 time_t lastused; /* updated every when kernel sends a packet */ 105 long lifetime; /* duration of the lifetime of this policy */ 106 long validtime; /* duration this policy is valid without use */ 107}; 108 109#define SECPOLICY_LOCK_INIT(_sp) \ 110 mtx_init(&(_sp)->lock, "ipsec policy", NULL, MTX_DEF) 111#define SECPOLICY_LOCK(_sp) mtx_lock(&(_sp)->lock) 112#define SECPOLICY_UNLOCK(_sp) mtx_unlock(&(_sp)->lock) 113#define SECPOLICY_LOCK_DESTROY(_sp) mtx_destroy(&(_sp)->lock) 114#define SECPOLICY_LOCK_ASSERT(_sp) mtx_assert(&(_sp)->lock, MA_OWNED) 115 116/* Request for IPsec */ 117struct ipsecrequest { 118 struct ipsecrequest *next; 119 /* pointer to next structure */ 120 /* If NULL, it means the end of chain. */ 121 struct secasindex saidx;/* hint for search proper SA */ 122 /* if __ss_len == 0 then no address specified.*/ 123 u_int level; /* IPsec level defined below. */ 124 125 struct secasvar *sav; /* place holder of SA for use */ 126 struct secpolicy *sp; /* back pointer to SP */ 127 struct mtx lock; /* to interlock updates */ 128}; 129 130/* 131 * Need recursion for when crypto callbacks happen directly, 132 * as in the case of software crypto. Need to look at how 133 * hard it is to remove this... 134 */ 135#define IPSECREQUEST_LOCK_INIT(_isr) \ 136 mtx_init(&(_isr)->lock, "ipsec request", NULL, MTX_DEF | MTX_RECURSE) 137#define IPSECREQUEST_LOCK(_isr) mtx_lock(&(_isr)->lock) 138#define IPSECREQUEST_UNLOCK(_isr) mtx_unlock(&(_isr)->lock) 139#define IPSECREQUEST_LOCK_DESTROY(_isr) mtx_destroy(&(_isr)->lock) 140#define IPSECREQUEST_LOCK_ASSERT(_isr) mtx_assert(&(_isr)->lock, MA_OWNED) 141 142/* security policy in PCB */ 143struct inpcbpolicy { 144 struct secpolicy *sp_in; 145 struct secpolicy *sp_out; 146 int priv; /* privileged socket ? */ 147}; 148 149/* SP acquiring list table. */ 150struct secspacq { 151 LIST_ENTRY(secspacq) chain; 152 153 struct secpolicyindex spidx; 154 155 time_t created; /* for lifetime */ 156 int count; /* for lifetime */ 157 /* XXX: here is mbuf place holder to be sent ? */ 158}; 159#endif /* _KERNEL */ 160 161/* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */ 162#define IPSEC_PORT_ANY 0 163#define IPSEC_ULPROTO_ANY 255 164#define IPSEC_PROTO_ANY 255 165 166/* mode of security protocol */ 167/* NOTE: DON'T use IPSEC_MODE_ANY at SPD. It's only use in SAD */ 168#define IPSEC_MODE_ANY 0 /* i.e. wildcard. */ 169#define IPSEC_MODE_TRANSPORT 1 170#define IPSEC_MODE_TUNNEL 2 171#define IPSEC_MODE_TCPMD5 3 /* TCP MD5 mode */ 172 173/* 174 * Direction of security policy. 175 * NOTE: Since INVALID is used just as flag. 176 * The other are used for loop counter too. 177 */ 178#define IPSEC_DIR_ANY 0 179#define IPSEC_DIR_INBOUND 1 180#define IPSEC_DIR_OUTBOUND 2 181#define IPSEC_DIR_MAX 3 182#define IPSEC_DIR_INVALID 4 183 184/* Policy level */ 185/* 186 * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB, 187 * DISCARD, IPSEC and NONE are allowed for setkey() in SPD. 188 * DISCARD and NONE are allowed for system default. 189 */ 190#define IPSEC_POLICY_DISCARD 0 /* discarding packet */ 191#define IPSEC_POLICY_NONE 1 /* through IPsec engine */ 192#define IPSEC_POLICY_IPSEC 2 /* do IPsec */ 193#define IPSEC_POLICY_ENTRUST 3 /* consulting SPD if present. */ 194#define IPSEC_POLICY_BYPASS 4 /* only for privileged socket. */ 195 196/* Security protocol level */ 197#define IPSEC_LEVEL_DEFAULT 0 /* reference to system default */ 198#define IPSEC_LEVEL_USE 1 /* use SA if present. */ 199#define IPSEC_LEVEL_REQUIRE 2 /* require SA. */ 200#define IPSEC_LEVEL_UNIQUE 3 /* unique SA. */ 201 202#define IPSEC_MANUAL_REQID_MAX 0x3fff 203 /* 204 * if security policy level == unique, this id 205 * indicate to a relative SA for use, else is 206 * zero. 207 * 1 - 0x3fff are reserved for manual keying. 208 * 0 are reserved for above reason. Others is 209 * for kernel use. 210 * Note that this id doesn't identify SA 211 * by only itself. 212 */ 213#define IPSEC_REPLAYWSIZE 32 214 215/* statistics for ipsec processing */ 216struct ipsecstat { 217 u_quad_t in_success; /* succeeded inbound process */ 218 u_quad_t in_polvio; 219 /* security policy violation for inbound process */ 220 u_quad_t in_nosa; /* inbound SA is unavailable */ 221 u_quad_t in_inval; /* inbound processing failed due to EINVAL */ 222 u_quad_t in_nomem; /* inbound processing failed due to ENOBUFS */ 223 u_quad_t in_badspi; /* failed getting a SPI */ 224 u_quad_t in_ahreplay; /* AH replay check failed */ 225 u_quad_t in_espreplay; /* ESP replay check failed */ 226 u_quad_t in_ahauthsucc; /* AH authentication success */ 227 u_quad_t in_ahauthfail; /* AH authentication failure */ 228 u_quad_t in_espauthsucc; /* ESP authentication success */ 229 u_quad_t in_espauthfail; /* ESP authentication failure */ 230 u_quad_t in_esphist[256]; 231 u_quad_t in_ahhist[256]; 232 u_quad_t in_comphist[256]; 233 u_quad_t out_success; /* succeeded outbound process */ 234 u_quad_t out_polvio; 235 /* security policy violation for outbound process */ 236 u_quad_t out_nosa; /* outbound SA is unavailable */ 237 u_quad_t out_inval; /* outbound process failed due to EINVAL */ 238 u_quad_t out_nomem; /* inbound processing failed due to ENOBUFS */ 239 u_quad_t out_noroute; /* there is no route */ 240 u_quad_t out_esphist[256]; 241 u_quad_t out_ahhist[256]; 242 u_quad_t out_comphist[256]; 243 244 u_quad_t spdcachelookup; 245 u_quad_t spdcachemiss; 246 247 u_int32_t ips_in_polvio; /* input: sec policy violation */ 248 u_int32_t ips_out_polvio; /* output: sec policy violation */ 249 u_int32_t ips_out_nosa; /* output: SA unavailable */ 250 u_int32_t ips_out_nomem; /* output: no memory available */ 251 u_int32_t ips_out_noroute; /* output: no route available */ 252 u_int32_t ips_out_inval; /* output: generic error */ 253 u_int32_t ips_out_bundlesa; /* output: bundled SA processed */ 254 u_int32_t ips_mbcoalesced; /* mbufs coalesced during clone */ 255 u_int32_t ips_clcoalesced; /* clusters coalesced during clone */ 256 u_int32_t ips_clcopied; /* clusters copied during clone */ 257 u_int32_t ips_mbinserted; /* mbufs inserted during makespace */ 258 /* 259 * Temporary statistics for performance analysis. 260 */ 261 /* See where ESP/AH/IPCOMP header land in mbuf on input */ 262 u_int32_t ips_input_front; 263 u_int32_t ips_input_middle; 264 u_int32_t ips_input_end; 265}; 266 267/* 268 * Definitions for IPsec & Key sysctl operations. 269 */ 270/* 271 * Names for IPsec & Key sysctl objects 272 */ 273#define IPSECCTL_STATS 1 /* stats */ 274#define IPSECCTL_DEF_POLICY 2 275#define IPSECCTL_DEF_ESP_TRANSLEV 3 /* int; ESP transport mode */ 276#define IPSECCTL_DEF_ESP_NETLEV 4 /* int; ESP tunnel mode */ 277#define IPSECCTL_DEF_AH_TRANSLEV 5 /* int; AH transport mode */ 278#define IPSECCTL_DEF_AH_NETLEV 6 /* int; AH tunnel mode */ 279#if 0 /* obsolete, do not reuse */ 280#define IPSECCTL_INBOUND_CALL_IKE 7 281#endif 282#define IPSECCTL_AH_CLEARTOS 8 283#define IPSECCTL_AH_OFFSETMASK 9 284#define IPSECCTL_DFBIT 10 285#define IPSECCTL_ECN 11 286#define IPSECCTL_DEBUG 12 287#define IPSECCTL_ESP_RANDPAD 13 288#define IPSECCTL_MAXID 14 289 290#define IPSECCTL_NAMES { \ 291 { 0, 0 }, \ 292 { 0, 0 }, \ 293 { "def_policy", CTLTYPE_INT }, \ 294 { "esp_trans_deflev", CTLTYPE_INT }, \ 295 { "esp_net_deflev", CTLTYPE_INT }, \ 296 { "ah_trans_deflev", CTLTYPE_INT }, \ 297 { "ah_net_deflev", CTLTYPE_INT }, \ 298 { 0, 0 }, \ 299 { "ah_cleartos", CTLTYPE_INT }, \ 300 { "ah_offsetmask", CTLTYPE_INT }, \ 301 { "dfbit", CTLTYPE_INT }, \ 302 { "ecn", CTLTYPE_INT }, \ 303 { "debug", CTLTYPE_INT }, \ 304 { "esp_randpad", CTLTYPE_INT }, \ 305} 306 307#define IPSEC6CTL_NAMES { \ 308 { 0, 0 }, \ 309 { 0, 0 }, \ 310 { "def_policy", CTLTYPE_INT }, \ 311 { "esp_trans_deflev", CTLTYPE_INT }, \ 312 { "esp_net_deflev", CTLTYPE_INT }, \ 313 { "ah_trans_deflev", CTLTYPE_INT }, \ 314 { "ah_net_deflev", CTLTYPE_INT }, \ 315 { 0, 0 }, \ 316 { 0, 0 }, \ 317 { 0, 0 }, \ 318 { 0, 0 }, \ 319 { "ecn", CTLTYPE_INT }, \ 320 { "debug", CTLTYPE_INT }, \ 321 { "esp_randpad", CTLTYPE_INT }, \ 322} 323 324#ifdef _KERNEL 325struct ipsec_output_state { 326 struct mbuf *m; 327 struct route *ro; 328 struct sockaddr *dst; 329}; 330 331struct ipsec_history { 332 int ih_proto; 333 u_int32_t ih_spi; 334}; 335 336extern int ipsec_debug; 337#ifdef REGRESSION 338extern int ipsec_replay; 339extern int ipsec_integrity; 340#endif 341 342extern struct ipsecstat ipsec4stat; 343extern struct secpolicy ip4_def_policy; 344extern int ip4_esp_trans_deflev; 345extern int ip4_esp_net_deflev; 346extern int ip4_ah_trans_deflev; 347extern int ip4_ah_net_deflev; 348extern int ip4_ah_cleartos; 349extern int ip4_ah_offsetmask; 350extern int ip4_ipsec_dfbit; 351extern int ip4_ipsec_ecn; 352extern int ip4_esp_randpad; 353extern int crypto_support; 354 355#define ipseclog(x) do { if (ipsec_debug) log x; } while (0) 356/* for openbsd compatibility */ 357#define DPRINTF(x) do { if (ipsec_debug) printf x; } while (0) 358 359extern struct ipsecrequest *ipsec_newisr(void); 360extern void ipsec_delisr(struct ipsecrequest *); 361 362struct tdb_ident; 363extern struct secpolicy *ipsec_getpolicy __P((struct tdb_ident*, u_int)); 364struct inpcb; 365extern struct secpolicy *ipsec4_checkpolicy __P((struct mbuf *, u_int, u_int, 366 int *, struct inpcb *)); 367extern struct secpolicy *ipsec_getpolicybysock(struct mbuf *, u_int, 368 struct inpcb *, int *); 369extern struct secpolicy * ipsec_getpolicybyaddr(struct mbuf *, u_int, 370 int, int *); 371 372struct inpcb; 373extern int ipsec_init_policy __P((struct socket *so, struct inpcbpolicy **)); 374extern int ipsec_copy_policy 375 __P((struct inpcbpolicy *, struct inpcbpolicy *)); 376extern u_int ipsec_get_reqlevel __P((struct ipsecrequest *)); 377extern int ipsec_in_reject __P((struct secpolicy *, struct mbuf *)); 378 379extern int ipsec4_set_policy __P((struct inpcb *inp, int optname, 380 caddr_t request, size_t len, struct ucred *cred)); 381extern int ipsec4_get_policy __P((struct inpcb *inpcb, caddr_t request, 382 size_t len, struct mbuf **mp)); 383extern int ipsec4_delete_pcbpolicy __P((struct inpcb *)); 384extern int ipsec4_in_reject __P((struct mbuf *, struct inpcb *)); 385 386struct secas; 387struct tcpcb; 388extern int ipsec_chkreplay __P((u_int32_t, struct secasvar *)); 389extern int ipsec_updatereplay __P((u_int32_t, struct secasvar *)); 390 391extern size_t ipsec4_hdrsiz __P((struct mbuf *, u_int, struct inpcb *)); 392extern size_t ipsec_hdrsiz_tcp __P((struct tcpcb *)); 393 394union sockaddr_union; 395extern char * ipsec_address(union sockaddr_union* sa); 396extern const char *ipsec_logsastr __P((struct secasvar *)); 397 398extern void ipsec_dumpmbuf __P((struct mbuf *)); 399 400struct m_tag; 401extern void ah4_input(struct mbuf *m, int off); 402extern void ah4_ctlinput(int cmd, struct sockaddr *sa, void *); 403extern void esp4_input(struct mbuf *m, int off); 404extern void esp4_ctlinput(int cmd, struct sockaddr *sa, void *); 405extern void ipcomp4_input(struct mbuf *m, int off); 406extern int ipsec4_common_input(struct mbuf *m, ...); 407extern int ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav, 408 int skip, int protoff, struct m_tag *mt); 409extern int ipsec4_process_packet __P((struct mbuf *, struct ipsecrequest *, 410 int, int)); 411extern int ipsec_process_done __P((struct mbuf *, struct ipsecrequest *)); 412 413extern struct mbuf *ipsec_copypkt __P((struct mbuf *)); 414 415extern void m_checkalignment(const char* where, struct mbuf *m0, 416 int off, int len); 417extern struct mbuf *m_makespace(struct mbuf *m0, int skip, int hlen, int *off); 418extern caddr_t m_pad(struct mbuf *m, int n); 419extern int m_striphdr(struct mbuf *m, int skip, int hlen); 420 421#ifdef DEV_ENC 422#define ENC_BEFORE 0x0001 423#define ENC_AFTER 0x0002 424#define ENC_IN 0x0100 425#define ENC_OUT 0x0200 426extern int ipsec_filter(struct mbuf **, int, int); 427extern void ipsec_bpf(struct mbuf *, struct secasvar *, int, int); 428#endif 429#endif /* _KERNEL */ 430 431#ifndef _KERNEL 432extern caddr_t ipsec_set_policy __P((char *, int)); 433extern int ipsec_get_policylen __P((caddr_t)); 434extern char *ipsec_dump_policy __P((caddr_t, char *)); 435 436extern const char *ipsec_strerror __P((void)); 437#endif /* !_KERNEL */ 438 439#endif /* _NETIPSEC_IPSEC_H_ */ 440