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