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