1 /* $FreeBSD: src/sys/netinet6/ipsec.h,v 1.4.2.2 2001/07/03 11:01:54 ume Exp $ */ 2 /* $KAME: ipsec.h,v 1.44 2001/03/23 08:08:47 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 _NETINET6_IPSEC_H_ 38 #define _NETINET6_IPSEC_H_ 39 #include <sys/cdefs.h> 40 #include <sys/appleapiopts.h> 41 42 #include <net/pfkeyv2.h> 43 #include <uuid/uuid.h> 44 #ifdef BSD_KERNEL_PRIVATE 45 #include <netkey/keydb.h> 46 #include <netinet/ip_var.h> 47 48 #include <os/log.h> 49 50 #define IPSEC_STAT_INCREMENT(x) \ 51 OSIncrementAtomic64((SInt64 *)&x) 52 53 struct secpolicyaddrrange { 54 struct sockaddr_storage start; /* Start (low values) of address range */ 55 struct sockaddr_storage end; /* End (high values) of address range */ 56 }; 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 */ 64 struct secpolicyindex { 65 u_int8_t dir; /* direction of packet flow, see below */ 66 struct sockaddr_storage src; /* IP src address for SP */ 67 struct sockaddr_storage 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_int8_t ul_proto; /* upper layer Protocol */ 71 ifnet_t internal_if; /* Interface a matching packet is bound to */ 72 struct secpolicyaddrrange src_range; /* IP src address range for SP */ 73 struct secpolicyaddrrange dst_range; /* IP dst address range for SP */ 74 #ifdef notyet 75 uid_t uids; 76 uid_t uidd; 77 gid_t gids; 78 gid_t gidd; 79 #endif 80 }; 81 82 /* Security Policy Data Base */ 83 struct secpolicy { 84 LIST_ENTRY(secpolicy) chain; 85 86 int refcnt; /* reference count */ 87 struct secpolicyindex spidx; /* selector */ 88 u_int32_t id; /* It's unique number on the system. */ 89 u_int state; /* 0: dead, others: alive */ 90 #define IPSEC_SPSTATE_DEAD 0 91 #define IPSEC_SPSTATE_ALIVE 1 92 93 u_int policy; /* DISCARD, NONE or IPSEC, see keyv2.h */ 94 struct ipsecrequest *req; 95 /* pointer to the ipsec request tree, */ 96 /* if policy == IPSEC else this value == NULL.*/ 97 98 ifnet_t ipsec_if; /* IPsec interface to use */ 99 ifnet_t outgoing_if; /* Outgoing interface for encrypted traffic */ 100 101 char disabled; /* Set to ignore policy */ 102 103 /* 104 * lifetime handler. 105 * the policy can be used without limitiation if both lifetime and 106 * validtime are zero. 107 * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime. 108 * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime. 109 */ 110 long created; /* time created the policy */ 111 long lastused; /* updated every when kernel sends a packet */ 112 long lifetime; /* duration of the lifetime of this policy */ 113 long validtime; /* duration this policy is valid without use */ 114 }; 115 116 /* Request for IPsec */ 117 struct 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 secpolicy *sp; /* back pointer to SP */ 126 }; 127 128 /* security policy in PCB */ 129 struct inpcbpolicy { 130 struct secpolicy *sp_in; 131 struct secpolicy *sp_out; 132 int priv; /* privileged socket ? */ 133 }; 134 135 /* SP acquiring list table. */ 136 struct secspacq { 137 LIST_ENTRY(secspacq) chain; 138 139 struct secpolicyindex spidx; 140 141 long created; /* for lifetime */ 142 int count; /* for lifetime */ 143 /* XXX: here is mbuf place holder to be sent ? */ 144 }; 145 #endif /* BSD_KERNEL_PRIVATE */ 146 147 /* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */ 148 #define IPSEC_PORT_ANY 0 149 #define IPSEC_ULPROTO_ANY 255 150 #define IPSEC_PROTO_ANY 255 151 152 /* mode of security protocol */ 153 /* NOTE: DON'T use IPSEC_MODE_ANY at SPD. It's only use in SAD */ 154 #define IPSEC_MODE_ANY 0 /* i.e. wildcard. */ 155 #define IPSEC_MODE_TRANSPORT 1 156 #define IPSEC_MODE_TUNNEL 2 157 158 /* 159 * Direction of security policy. 160 * NOTE: Since INVALID is used just as flag. 161 * The other are used for loop counter too. 162 */ 163 #define IPSEC_DIR_ANY 0 164 #define IPSEC_DIR_INBOUND 1 165 #define IPSEC_DIR_OUTBOUND 2 166 #define IPSEC_DIR_MAX 3 167 #define IPSEC_DIR_INVALID 4 168 169 /* Policy level */ 170 /* 171 * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB, 172 * DISCARD, IPSEC and NONE are allowed for setkey() in SPD. 173 * DISCARD and NONE are allowed for system default. 174 */ 175 #define IPSEC_POLICY_DISCARD 0 /* discarding packet */ 176 #define IPSEC_POLICY_NONE 1 /* through IPsec engine */ 177 #define IPSEC_POLICY_IPSEC 2 /* do IPsec */ 178 #define IPSEC_POLICY_ENTRUST 3 /* consulting SPD if present. */ 179 #define IPSEC_POLICY_BYPASS 4 /* only for privileged socket. */ 180 #define IPSEC_POLICY_GENERATE 5 /* same as discard - IKE daemon can override with generated policy */ 181 182 /* Security protocol level */ 183 #define IPSEC_LEVEL_DEFAULT 0 /* reference to system default */ 184 #define IPSEC_LEVEL_USE 1 /* use SA if present. */ 185 #define IPSEC_LEVEL_REQUIRE 2 /* require SA. */ 186 #define IPSEC_LEVEL_UNIQUE 3 /* unique SA. */ 187 188 #define IPSEC_MANUAL_REQID_MAX 0x3fff 189 /* 190 * if security policy level == unique, this id 191 * indicate to a relative SA for use, else is 192 * zero. 193 * 1 - 0x3fff are reserved for manual keying. 194 * 0 are reserved for above reason. Others is 195 * for kernel use. 196 * Note that this id doesn't identify SA 197 * by only itself. 198 */ 199 #define IPSEC_REPLAYWSIZE 32 200 201 /* 202 * Maximum key sizes in bytes expected to be passed from userspace. 203 * 204 * These values are based on the NULL algorithms for AH and ESP, 205 * which both specify a keymax of 2048 bits. 206 */ 207 #define IPSEC_KEY_AUTH_MAX_BYTES 256 208 #define IPSEC_KEY_ENCRYPT_MAX_BYTES 256 209 210 /* statistics for ipsec processing */ 211 struct ipsecstat { 212 u_quad_t in_success __attribute__ ((aligned(8))); /* succeeded inbound process */ 213 u_quad_t in_polvio __attribute__ ((aligned(8))); 214 /* security policy violation for inbound process */ 215 u_quad_t in_nosa __attribute__ ((aligned(8))); /* inbound SA is unavailable */ 216 u_quad_t in_inval __attribute__ ((aligned(8))); /* inbound processing failed due to EINVAL */ 217 u_quad_t in_nomem __attribute__ ((aligned(8))); /* inbound processing failed due to ENOBUFS */ 218 u_quad_t in_badspi __attribute__ ((aligned(8))); /* failed getting a SPI */ 219 u_quad_t in_ahreplay __attribute__ ((aligned(8))); /* AH replay check failed */ 220 u_quad_t in_espreplay __attribute__ ((aligned(8))); /* ESP replay check failed */ 221 u_quad_t in_ahauthsucc __attribute__ ((aligned(8))); /* AH authentication success */ 222 u_quad_t in_ahauthfail __attribute__ ((aligned(8))); /* AH authentication failure */ 223 u_quad_t in_espauthsucc __attribute__ ((aligned(8))); /* ESP authentication success */ 224 u_quad_t in_espauthfail __attribute__ ((aligned(8))); /* ESP authentication failure */ 225 u_quad_t in_esphist[256] __attribute__ ((aligned(8))); 226 u_quad_t in_ahhist[256] __attribute__ ((aligned(8))); 227 u_quad_t in_comphist[256] __attribute__ ((aligned(8))); 228 u_quad_t out_success __attribute__ ((aligned(8))); /* succeeded outbound process */ 229 u_quad_t out_polvio __attribute__ ((aligned(8))); 230 /* security policy violation for outbound process */ 231 u_quad_t out_nosa __attribute__ ((aligned(8))); /* outbound SA is unavailable */ 232 u_quad_t out_inval __attribute__ ((aligned(8))); /* outbound process failed due to EINVAL */ 233 u_quad_t out_nomem __attribute__ ((aligned(8))); /* inbound processing failed due to ENOBUFS */ 234 u_quad_t out_noroute __attribute__ ((aligned(8))); /* there is no route */ 235 u_quad_t out_esphist[256] __attribute__ ((aligned(8))); 236 u_quad_t out_ahhist[256] __attribute__ ((aligned(8))); 237 u_quad_t out_comphist[256] __attribute__ ((aligned(8))); 238 }; 239 240 #define IPSEC_MAX_WAKE_PKT_LEN 100 241 struct ipsec_wake_pkt_info { 242 u_int8_t wake_pkt[IPSEC_MAX_WAKE_PKT_LEN]; 243 uuid_string_t wake_uuid; 244 u_int32_t wake_pkt_spi; 245 u_int32_t wake_pkt_seq; 246 u_int16_t wake_pkt_len; 247 }; 248 249 struct ipsec_wake_pkt_event_data { 250 uuid_string_t wake_uuid; 251 }; 252 253 #ifdef BSD_KERNEL_PRIVATE 254 /* 255 * Definitions for IPsec & Key sysctl operations. 256 */ 257 /* 258 * Names for IPsec & Key sysctl objects 259 */ 260 #define IPSECCTL_STATS 1 /* stats */ 261 #define IPSECCTL_DEF_POLICY 2 262 #define IPSECCTL_DEF_ESP_TRANSLEV 3 /* int; ESP transport mode */ 263 #define IPSECCTL_DEF_ESP_NETLEV 4 /* int; ESP tunnel mode */ 264 #define IPSECCTL_DEF_AH_TRANSLEV 5 /* int; AH transport mode */ 265 #define IPSECCTL_DEF_AH_NETLEV 6 /* int; AH tunnel mode */ 266 #if 0 /* obsolete, do not reuse */ 267 #define IPSECCTL_INBOUND_CALL_IKE 7 268 #endif 269 #define IPSECCTL_AH_CLEARTOS 8 270 #define IPSECCTL_AH_OFFSETMASK 9 271 #define IPSECCTL_DFBIT 10 272 #define IPSECCTL_ECN 11 273 #define IPSECCTL_DEBUG 12 274 #define IPSECCTL_ESP_RANDPAD 13 275 #define IPSECCTL_MAXID 14 276 277 #define IPSECCTL_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 { "ah_cleartos", CTLTYPE_INT }, \ 287 { "ah_offsetmask", CTLTYPE_INT }, \ 288 { "dfbit", CTLTYPE_INT }, \ 289 { "ecn", CTLTYPE_INT }, \ 290 { "debug", CTLTYPE_INT }, \ 291 { "esp_randpad", CTLTYPE_INT }, \ 292 } 293 294 #define IPSEC6CTL_NAMES { \ 295 { 0, 0 }, \ 296 { 0, 0 }, \ 297 { "def_policy", CTLTYPE_INT }, \ 298 { "esp_trans_deflev", CTLTYPE_INT }, \ 299 { "esp_net_deflev", CTLTYPE_INT }, \ 300 { "ah_trans_deflev", CTLTYPE_INT }, \ 301 { "ah_net_deflev", CTLTYPE_INT }, \ 302 { 0, 0 }, \ 303 { 0, 0 }, \ 304 { 0, 0 }, \ 305 { 0, 0 }, \ 306 { "ecn", CTLTYPE_INT }, \ 307 { "debug", CTLTYPE_INT }, \ 308 { "esp_randpad", CTLTYPE_INT }, \ 309 } 310 311 #if defined(__ARM__) 312 #define IPSEC_IS_P2ALIGNED(p) IS_P2ALIGNED(p, sizeof (u_int32_t)) 313 #define IPSEC_GET_P2UNALIGNED_OFS(p) (sizeof(u_int32_t) - (((uintptr_t)(p)) & ((uintptr_t)(sizeof(u_int32_t)) - 1))) 314 #else 315 #define IPSEC_IS_P2ALIGNED(p) 1 316 #define IPSEC_GET_P2UNALIGNED_OFS(p) 0 317 #endif 318 319 struct ipsec_output_state { 320 int tunneled; 321 struct mbuf *m; 322 struct route_in6 ro; 323 struct sockaddr *dst; 324 u_int outgoing_if; 325 u_int32_t dscp_mapping; 326 }; 327 328 struct ipsec_history { 329 int ih_proto; 330 u_int32_t ih_spi; 331 }; 332 333 extern int ipsec_debug; 334 335 extern struct ipsecstat ipsecstat; 336 extern struct secpolicy ip4_def_policy; 337 extern int ip4_esp_trans_deflev; 338 extern int ip4_esp_net_deflev; 339 extern int ip4_ah_trans_deflev; 340 extern int ip4_ah_net_deflev; 341 extern int ip4_ah_cleartos; 342 extern int ip4_ah_offsetmask; 343 extern int ip4_ipsec_dfbit; 344 extern int ip4_ipsec_ecn; 345 extern int ip4_esp_randpad; 346 347 extern bool ipsec_save_wake_pkt; 348 349 #define _ipsec_log(level, fmt, ...) do { \ 350 os_log_type_t type; \ 351 switch (level) { \ 352 default: \ 353 type = OS_LOG_TYPE_DEFAULT; \ 354 break; \ 355 case LOG_INFO: \ 356 type = OS_LOG_TYPE_INFO; \ 357 break; \ 358 case LOG_DEBUG: \ 359 type = OS_LOG_TYPE_DEBUG; \ 360 break; \ 361 case LOG_ERR: \ 362 type = OS_LOG_TYPE_ERROR; \ 363 break; \ 364 } \ 365 os_log_with_type(OS_LOG_DEFAULT, type, fmt, ##__VA_ARGS__); \ 366 } while (0) 367 368 #define ipseclog(x) do { if (ipsec_debug != 0) _ipsec_log x; } while (0) 369 370 extern struct secpolicy *ipsec4_getpolicybysock(struct mbuf *, u_int8_t, 371 struct socket *, int *); 372 extern struct secpolicy *ipsec4_getpolicybyaddr(struct mbuf *, u_int8_t, int, 373 int *); 374 extern int ipsec4_getpolicybyinterface(struct mbuf *, u_int8_t, int *, 375 struct ip_out_args *, struct secpolicy **); 376 377 extern u_int ipsec_get_reqlevel(struct ipsecrequest *); 378 379 struct inpcb; 380 extern int ipsec_init_policy(struct socket *so, struct inpcbpolicy **); 381 extern int ipsec_copy_policy(struct inpcbpolicy *, struct inpcbpolicy *); 382 extern u_int ipsec_get_reqlevel(struct ipsecrequest *); 383 384 extern int ipsec4_set_policy(struct inpcb *inp, int optname, 385 caddr_t request, size_t len, int priv); 386 extern int ipsec4_delete_pcbpolicy(struct inpcb *); 387 extern int ipsec4_in_reject_so(struct mbuf *, struct socket *); 388 extern int ipsec4_in_reject(struct mbuf *, struct inpcb *); 389 390 struct secas; 391 struct tcpcb; 392 extern int ipsec_chkreplay(u_int32_t, struct secasvar *, u_int8_t); 393 extern int ipsec_updatereplay(u_int32_t, struct secasvar *, u_int8_t); 394 395 extern size_t ipsec4_hdrsiz(struct mbuf *, u_int8_t, struct inpcb *); 396 extern size_t ipsec_hdrsiz_tcp(struct tcpcb *); 397 extern size_t ipsec_hdrsiz(struct secpolicy *); 398 399 struct ip; 400 extern const char *ipsec4_logpacketstr(struct ip *, u_int32_t); 401 extern const char *ipsec_logsastr(struct secasvar *); 402 403 extern void ipsec_dumpmbuf(struct mbuf *); 404 405 extern int ipsec4_interface_output(struct ipsec_output_state *state, ifnet_t interface); 406 extern int ipsec4_output(struct ipsec_output_state *, struct secpolicy *, int); 407 #if INET 408 extern struct mbuf * ipsec4_splithdr(struct mbuf *); 409 extern int ipsec4_encapsulate(struct mbuf *, struct secasvar *); 410 #endif 411 extern struct mbuf * ipsec6_splithdr(struct mbuf *); 412 extern int ipsec6_encapsulate(struct mbuf *, struct secasvar *); 413 extern int ipsec4_tunnel_validate(struct mbuf *, int, u_int, struct secasvar *, sa_family_t *); 414 extern struct mbuf *ipsec_copypkt(struct mbuf *); 415 extern void ipsec_delaux(struct mbuf *); 416 extern int ipsec_setsocket(struct mbuf *, struct socket *); 417 extern struct socket *ipsec_getsocket(struct mbuf *); 418 extern int ipsec_addhist(struct mbuf *, int, u_int32_t); 419 extern struct ipsec_history *ipsec_gethist(struct mbuf *, int *); 420 extern void ipsec_clearhist(struct mbuf *); 421 extern void ipsec_monitor_sleep_wake(void); 422 extern void ipsec_save_wake_packet(struct mbuf *, u_int32_t, u_int32_t); 423 #endif /* BSD_KERNEL_PRIVATE */ 424 425 #ifndef KERNEL 426 __BEGIN_DECLS 427 extern caddr_t ipsec_set_policy(char *, int); 428 extern int ipsec_get_policylen(caddr_t); 429 extern char *ipsec_dump_policy(caddr_t, char *); 430 431 extern const char *ipsec_strerror(void); 432 __END_DECLS 433 #endif /* KERNEL */ 434 435 #endif /* _NETINET6_IPSEC_H_ */ 436