ip_ipsp.h revision 1.48
1/* $OpenBSD: ip_ipsp.h,v 1.48 1999/12/06 07:14:36 angelos Exp $ */ 2 3/* 4 * The authors of this code are John Ioannidis (ji@tla.org), 5 * Angelos D. Keromytis (kermit@csd.uch.gr), 6 * Niels Provos (provos@physnet.uni-hamburg.de) and 7 * Niklas Hallqvist (niklas@appli.se). 8 * 9 * This code was written by John Ioannidis for BSD/OS in Athens, Greece, 10 * in November 1995. 11 * 12 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996, 13 * by Angelos D. Keromytis. 14 * 15 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis 16 * and Niels Provos. 17 * 18 * Additional features in 1999 by Angelos D. Keromytis and Niklas Hallqvist. 19 * 20 * Copyright (c) 1995, 1996, 1997, 1998, 1999 by John Ioannidis, 21 * Angelos D. Keromytis and Niels Provos. 22 * Copyright (c) 1999 Niklas Hallqvist. 23 * 24 * Permission to use, copy, and modify this software without fee 25 * is hereby granted, provided that this entire notice is included in 26 * all copies of any software which is or includes a copy or 27 * modification of this software. 28 * You may use this code under the GNU public license if you so wish. Please 29 * contribute changes back to the authors under this freer than GPL license 30 * so that we may further the use of strong encryption without limitations to 31 * all. 32 * 33 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 34 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 35 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 36 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 37 * PURPOSE. 38 */ 39 40#ifndef _NETINET_IPSP_H_ 41#define _NETINET_IPSP_H_ 42 43/* 44 * IPSP global definitions. 45 */ 46 47#include <sys/types.h> 48#include <sys/queue.h> 49#include <netinet/in.h> 50#include <sys/md5k.h> 51#include <netinet/ip_sha1.h> 52#include <netinet/ip_rmd160.h> 53#include <netinet/ip_blf.h> 54#include <netinet/ip_cast.h> 55#include <netinet/ip_skipjack.h> 56 57union sockaddr_union 58{ 59 struct sockaddr sa; 60 struct sockaddr_in sin; 61 struct sockaddr_in6 sin6; 62}; 63 64/* HMAC key sizes */ 65#define MD5HMAC96_KEYSIZE 16 66#define SHA1HMAC96_KEYSIZE 20 67#define RIPEMD160HMAC96_KEYSIZE 20 68 69/* IV lengths */ 70#define ESP_DES_IVS 8 71#define ESP_3DES_IVS 8 72#define ESP_BLF_IVS 8 73#define ESP_CAST_IVS 8 74#define ESP_SKIPJACK_IVS 8 75#define ESP_MAX_IVS 8 /* Keep updated */ 76 77/* Block sizes -- it is assumed that they're powers of 2 */ 78#define ESP_DES_BLKS 8 79#define ESP_3DES_BLKS 8 80#define ESP_BLF_BLKS 8 81#define ESP_CAST_BLKS 8 82#define ESP_SKIPJACK_BLKS 8 83#define ESP_MAX_BLKS 8 /* Keep updated */ 84 85#define HMAC_BLOCK_LEN 64 86 87#define AH_HMAC_HASHLEN 12 /* 96 bits of authenticator */ 88#define AH_HMAC_RPLENGTH 4 /* 32 bits of replay counter */ 89#define AH_HMAC_INITIAL_RPL 1 /* Replay counter initial value */ 90 91/* HMAC definitions */ 92#define HMAC_IPAD_VAL 0x36 93#define HMAC_OPAD_VAL 0x5C 94#define HMAC_BLOCK_LEN 64 95 96/* Authenticator lengths */ 97#define AH_MD5_ALEN 16 98#define AH_SHA1_ALEN 20 99#define AH_RMD160_ALEN 20 100#define AH_ALEN_MAX 20 /* Keep updated */ 101 102/* Reserved SPI numbers */ 103#define SPI_LOCAL_USE 0 104#define SPI_RESERVED_MIN 1 105#define SPI_RESERVED_MAX 255 106 107struct sockaddr_encap 108{ 109 u_int8_t sen_len; /* length */ 110 u_int8_t sen_family; /* PF_KEY */ 111 u_int16_t sen_type; /* see SENT_* */ 112 union 113 { 114 u_int8_t Data[16]; /* other stuff mapped here */ 115 116 struct /* SENT_IP4 */ 117 { 118 struct in_addr Src; 119 struct in_addr Dst; 120 u_int16_t Sport; 121 u_int16_t Dport; 122 u_int8_t Proto; 123 u_int8_t Filler[3]; 124 } Sip4; 125 126 struct /* SENT_IP6 */ 127 { 128 struct in6_addr Src; 129 struct in6_addr Dst; 130 u_int16_t Sport; 131 u_int16_t Dport; 132 u_int8_t Proto; 133 u_int8_t Filler[3]; 134 } Sip6; 135 136 struct /* SENT_IPSP */ 137 { 138 struct in_addr Dst; 139 u_int32_t Spi; 140 u_int8_t Sproto; 141 u_int8_t Filler[7]; 142 } Sipsp; 143 144 struct /* SENT_IPSP6 */ 145 { 146 struct in6_addr Dst; 147 u_int32_t Spi; 148 u_int8_t Sproto; 149 u_int8_t Filler[7]; 150 } Sipsp6; 151 } Sen; 152}; 153 154#define sen_data Sen.Data 155#define sen_ip_src Sen.Sip4.Src 156#define sen_ip_dst Sen.Sip4.Dst 157#define sen_proto Sen.Sip4.Proto 158#define sen_sport Sen.Sip4.Sport 159#define sen_dport Sen.Sip4.Dport 160#define sen_ip6_src Sen.Sip6.Src 161#define sen_ip6_dst Sen.Sip6.Dst 162#define sen_ip6_proto Sen.Sip6.Proto 163#define sen_ip6_sport Sen.Sip6.Sport 164#define sen_ip6_dport Sen.Sip6.Dport 165#define sen_ipsp_dst Sen.Sipsp.Dst 166#define sen_ipsp_spi Sen.Sipsp.Spi 167#define sen_ipsp_sproto Sen.Sipsp.Sproto 168#define sen_ipsp6_dst Sen.Sipsp6.Dst 169#define sen_ipsp6_spi Sen.Sipsp6.Spi 170#define sen_ipsp6_sproto Sen.Sipsp6.Sproto 171 172/* 173 * The "type" is really part of the address as far as the routing 174 * system is concerned. By using only one bit in the type field 175 * for each type, we sort-of make sure that different types of 176 * encapsulation addresses won't be matched against the wrong type. 177 * 178 */ 179 180#define SENT_IP4 0x0001 /* data is two struct in_addr */ 181#define SENT_IPSP 0x0002 /* data as in IP4/6 plus SPI */ 182#define SENT_IP6 0x0004 183#define SENT_IPSP6 0x0008 184 185/* 186 * SENT_HDRLEN is the length of the "header" 187 * SENT_*_LEN are the lengths of various forms of sen_data 188 * SENT_*_OFF are the offsets in the sen_data array of various fields 189 */ 190 191#define SENT_HDRLEN (2 * sizeof(u_int8_t) + sizeof(u_int16_t)) 192 193#define SENT_IP4_SRCOFF (0) 194#define SENT_IP4_DSTOFF (sizeof (struct in_addr)) 195 196#define SENT_IP6_SRCOFF (0) 197#define SENT_IP6_DSTOFF (sizeof (struct in6_addr)) 198 199#define SENT_IP4_LEN 20 200#define SENT_IPSP_LEN 20 201#define SENT_IP6_LEN 44 202#define SENT_IPSP6_LEN 32 203 204#define NOTIFY_SOFT_EXPIRE 0 /* Soft expiration of SA */ 205#define NOTIFY_HARD_EXPIRE 1 /* Hard expiration of SA */ 206#define NOTIFY_REQUEST_SA 2 /* Establish an SA */ 207 208#define NOTIFY_SATYPE_CONF 1 /* SA should do encryption */ 209#define NOTIFY_SATYPE_AUTH 2 /* SA should do authentication */ 210#define NOTIFY_SATYPE_TUNNEL 4 /* SA should use tunneling */ 211 212/* 213 * For encapsulation routes are possible not only for the destination 214 * address but also for the protocol, source and destination ports 215 * if available 216 */ 217 218struct route_enc { 219 struct rtentry *re_rt; 220 struct sockaddr_encap re_dst; 221}; 222 223struct flow 224{ 225 struct flow *flow_next; /* Next in flow chain */ 226 struct flow *flow_prev; /* Previous in flow chain */ 227 struct tdb *flow_sa; /* Pointer to the SA */ 228 union sockaddr_union flow_src; /* Source address */ 229 union sockaddr_union flow_srcmask; /* Source netmask */ 230 union sockaddr_union flow_dst; /* Destination address */ 231 union sockaddr_union flow_dstmask; /* Destination netmask */ 232 u_int8_t flow_proto; /* Transport protocol, if applicable */ 233 u_int8_t foo[3]; /* Alignment */ 234}; 235 236struct tdb /* tunnel descriptor block */ 237{ 238 struct tdb *tdb_hnext; /* Next in hash chain */ 239 struct tdb *tdb_onext; /* Next in output */ 240 struct tdb *tdb_inext; /* Previous in output */ 241 242 struct xformsw *tdb_xform; /* Transformation to use */ 243 struct enc_xform *tdb_encalgxform; /* Encryption algorithm xform */ 244 struct auth_hash *tdb_authalgxform; /* Authentication algorithm xform */ 245 246#define TDBF_UNIQUE 0x00001 /* This should not be used by others */ 247#define TDBF_TIMER 0x00002 /* Absolute expiration timer in use */ 248#define TDBF_BYTES 0x00004 /* Check the byte counters */ 249#define TDBF_ALLOCATIONS 0x00008 /* Check the flows counters */ 250#define TDBF_INVALID 0x00010 /* This SPI is not valid yet/anymore */ 251#define TDBF_FIRSTUSE 0x00020 /* Expire after first use */ 252#define TDBF_HALFIV 0x00040 /* Use half-length IV (ESP old only) */ 253#define TDBF_SOFT_TIMER 0x00080 /* Soft expiration */ 254#define TDBF_SOFT_BYTES 0x00100 /* Soft expiration */ 255#define TDBF_SOFT_ALLOCATIONS 0x00200 /* Soft expiration */ 256#define TDBF_SOFT_FIRSTUSE 0x00400 /* Soft expiration */ 257#define TDBF_PFS 0x00800 /* Ask for PFS from Key Mgmt. */ 258#define TDBF_TUNNELING 0x01000 /* Force IP-IP encapsulation */ 259 u_int32_t tdb_flags; /* Flags related to this TDB */ 260 261 TAILQ_ENTRY(tdb) tdb_expnext; /* Expiration cluster list link */ 262 TAILQ_ENTRY(tdb) tdb_explink; /* Expiration ordered list link */ 263 264 u_int32_t tdb_exp_allocations; /* Expire after so many flows */ 265 u_int32_t tdb_soft_allocations; /* Expiration warning */ 266 u_int32_t tdb_cur_allocations; /* Total number of allocations */ 267 268 u_int64_t tdb_exp_bytes; /* Expire after so many bytes passed */ 269 u_int64_t tdb_soft_bytes; /* Expiration warning */ 270 u_int64_t tdb_cur_bytes; /* Current count of bytes */ 271 272 u_int64_t tdb_exp_timeout; /* When does the SPI expire */ 273 u_int64_t tdb_soft_timeout; /* Send a soft-expire warning */ 274 u_int64_t tdb_established; /* When was the SPI established */ 275 u_int64_t tdb_timeout; /* Next absolute expiration time. */ 276 277 u_int64_t tdb_first_use; /* When was it first used */ 278 u_int64_t tdb_soft_first_use; /* Soft warning */ 279 u_int64_t tdb_exp_first_use; /* Expire if tdb_first_use + 280 * tdb_exp_first_use <= curtime */ 281 282 u_int32_t tdb_spi; /* SPI */ 283 u_int16_t tdb_amxkeylen; /* AH-old only */ 284 u_int16_t tdb_ivlen; /* IV length */ 285 u_int8_t tdb_sproto; /* IPsec protocol */ 286 u_int8_t tdb_wnd; /* Replay window */ 287 u_int8_t tdb_satype; /* SA type (RFC2367, PF_KEY) */ 288 u_int8_t tdb_FILLER; /* Padding */ 289 290 union sockaddr_union tdb_dst; /* Destination address for this SA */ 291 union sockaddr_union tdb_src; /* Source address for this SA */ 292 union sockaddr_union tdb_proxy; 293 294 u_int8_t *tdb_key; /* Key material (schedules) */ 295 u_int8_t *tdb_ictx; /* Authentication contexts */ 296 u_int8_t *tdb_octx; 297 u_int8_t *tdb_srcid; /* Source ID for this SA */ 298 u_int8_t *tdb_dstid; /* Destination ID for this SA */ 299 u_int8_t *tdb_amxkey; /* AH-old only */ 300 301 union 302 { 303 u_int8_t Iv[ESP_3DES_IVS]; /* That's enough space */ 304 u_int32_t Ivl; /* Make sure this is 4 bytes */ 305 u_int64_t Ivq; /* Make sure this is 8 bytes! */ 306 }IV; 307#define tdb_iv IV.Iv 308#define tdb_ivl IV.Ivl 309#define tdb_ivq IV.Ivq 310 311 u_int32_t tdb_rpl; /* Replay counter */ 312 u_int32_t tdb_bitmap; /* Used for replay sliding window */ 313 u_int32_t tdb_initial; /* Initial replay value */ 314 315 u_int32_t tdb_epoch; /* Used by the kernfs interface */ 316 u_int16_t tdb_srcid_len; 317 u_int16_t tdb_dstid_len; 318 u_int16_t tdb_srcid_type; 319 u_int16_t tdb_dstid_type; 320 321 caddr_t tdb_interface; 322 struct flow *tdb_flow; /* Which flows use this SA */ 323 324 struct tdb *tdb_bind_out; /* Outgoing SA to use */ 325 TAILQ_HEAD(tdb_bind_head, tdb) tdb_bind_in; 326 TAILQ_ENTRY(tdb) tdb_bind_in_next; /* Refering Incoming SAs */ 327 TAILQ_HEAD(tdb_inp_head, inpcb) tdb_inp; 328}; 329 330union authctx_old { 331 MD5_CTX md5ctx; 332 SHA1_CTX sha1ctx; 333}; 334 335union authctx { 336 MD5_CTX md5ctx; 337 SHA1_CTX sha1ctx; 338 RMD160_CTX rmd160ctx; 339}; 340 341struct tdb_ident { 342 u_int32_t spi; 343 union sockaddr_union dst; 344 u_int8_t proto; 345}; 346 347struct auth_hash { 348 int type; 349 char *name; 350 u_int16_t keysize; 351 u_int16_t hashsize; 352 u_int16_t ctxsize; 353 void (*Init)(void *); 354 void (*Update)(void *, u_int8_t *, u_int16_t); 355 void (*Final)(u_int8_t *, void *); 356}; 357 358struct enc_xform { 359 int type; 360 char *name; 361 u_int16_t blocksize, ivsize; 362 u_int16_t minkey, maxkey; 363 u_int32_t ivmask; /* Or all possible modes, zero iv = 1 */ 364 void (*encrypt)(struct tdb *, u_int8_t *); 365 void (*decrypt)(struct tdb *, u_int8_t *); 366 void (*setkey)(u_int8_t **, u_int8_t *, int len); 367 void (*zerokey)(u_int8_t **); 368}; 369 370struct ipsecinit 371{ 372 u_int8_t *ii_enckey; 373 u_int8_t *ii_authkey; 374 u_int16_t ii_enckeylen; 375 u_int16_t ii_authkeylen; 376 u_int8_t ii_encalg; 377 u_int8_t ii_authalg; 378}; 379 380struct xformsw 381{ 382 u_short xf_type; /* Unique ID of xform */ 383 u_short xf_flags; /* flags (see below) */ 384 char *xf_name; /* human-readable name */ 385 int (*xf_attach)(void); /* called at config time */ 386 int (*xf_init)(struct tdb *, struct xformsw *, struct ipsecinit *); 387 int (*xf_zeroize)(struct tdb *); /* termination */ 388 struct mbuf *(*xf_input)(struct mbuf *, struct tdb *, int, int); /* input */ 389 int (*xf_output)(struct mbuf *, struct tdb *, struct mbuf **, int, int); /* output */ 390}; 391 392/* xform IDs */ 393#define XF_IP4 1 /* IP inside IP */ 394#define XF_OLD_AH 2 /* RFCs 1828 & 1852 */ 395#define XF_OLD_ESP 3 /* RFCs 1829 & 1851 */ 396#define XF_NEW_AH 4 /* AH HMAC 96bits */ 397#define XF_NEW_ESP 5 /* ESP + auth 96bits + replay counter */ 398#define XF_TCPSIGNATURE 6 /* TCP MD5 Signature option, RFC 2358 */ 399 400/* xform attributes */ 401#define XFT_AUTH 0x0001 402#define XFT_CONF 0x0100 403 404#define IPSEC_ZEROES_SIZE 64 405#define IPSEC_KERNFS_BUFSIZE 4096 406 407#if BYTE_ORDER == LITTLE_ENDIAN 408static __inline u_int64_t 409htonq(u_int64_t q) 410{ 411 register u_int32_t u, l; 412 u = q >> 32; 413 l = (u_int32_t) q; 414 415 return htonl(u) | ((u_int64_t)htonl(l) << 32); 416} 417 418#define ntohq(_x) htonq(_x) 419 420#elif BYTE_ORDER == BIG_ENDIAN 421 422#define htonq(_x) (_x) 423#define ntohq(_x) htonq(_x) 424 425#else 426#error "Please fix <machine/endian.h>" 427#endif 428 429#ifdef _KERNEL 430 431/* 432 * Protects all tdb lists. 433 * Must at least be splsoftnet (note: do not use splsoftclock as it is 434 * special on some architectures, assuming it is always an spl lowering 435 * operation). 436 */ 437#define spltdb splsoftnet 438 439extern int encdebug; 440extern int ipsec_in_use; 441extern u_int8_t hmac_ipad_buffer[64]; 442extern u_int8_t hmac_opad_buffer[64]; 443 444extern TAILQ_HEAD(expclusterlist_head, tdb) expclusterlist; 445extern TAILQ_HEAD(explist_head, tdb) explist; 446extern struct xformsw xformsw[], *xformswNXFORMSW; 447 448/* Check if a given tdb has encryption, authentication and/or tunneling */ 449#define TDB_ATTRIB(x) (((x)->tdb_encalgxform ? NOTIFY_SATYPE_CONF : 0)| \ 450 ((x)->tdb_authalgxform ? NOTIFY_SATYPE_AUTH : 0)) 451 452/* Traverse spi chain and get attributes */ 453 454#define SPI_CHAIN_ATTRIB(have, TDB_DIR, TDBP) do {\ 455 int s = spltdb(); \ 456 struct tdb *tmptdb = (TDBP); \ 457 \ 458 (have) = 0; \ 459 while (tmptdb && tmptdb->tdb_xform) { \ 460 if (tmptdb == NULL || tmptdb->tdb_flags & TDBF_INVALID) \ 461 break; \ 462 (have) |= TDB_ATTRIB(tmptdb); \ 463 tmptdb = tmptdb->TDB_DIR; \ 464 } \ 465 splx(s); \ 466} while (0) 467 468/* Misc. */ 469extern char *inet_ntoa4(struct in_addr); 470 471#if INET6 472extern char *inet6_ntoa4(struct in6_addr); 473#endif /* INET6 */ 474 475extern char *ipsp_address(union sockaddr_union); 476 477/* TDB management routines */ 478extern void tdb_add_inp(struct tdb *tdb, struct inpcb *inp); 479extern u_int32_t reserve_spi(u_int32_t, u_int32_t, union sockaddr_union *, 480 union sockaddr_union *, u_int8_t, int *); 481extern struct tdb *gettdb(u_int32_t, union sockaddr_union *, u_int8_t); 482extern void puttdb(struct tdb *); 483extern void tdb_delete(struct tdb *, int, int); 484extern int tdb_init(struct tdb *, u_int16_t, struct ipsecinit *); 485extern void tdb_expiration(struct tdb *, int); 486/* Flag values for the last argument of tdb_expiration(). */ 487#define TDBEXP_EARLY 1 /* The tdb is likely to end up early. */ 488#define TDBEXP_TIMEOUT 2 /* Maintain expiration timeout. */ 489extern int tdb_walk(int (*)(struct tdb *, void *), void *); 490extern void handle_expirations(void *); 491 492/* Flow management routines */ 493extern struct flow *get_flow(void); 494extern void put_flow(struct flow *, struct tdb *); 495extern void delete_flow(struct flow *, struct tdb *); 496extern struct flow *find_flow(union sockaddr_union *, union sockaddr_union *, 497 union sockaddr_union *, union sockaddr_union *, 498 u_int8_t, struct tdb *); 499extern struct flow *find_global_flow(union sockaddr_union *, 500 union sockaddr_union *, 501 union sockaddr_union *, 502 union sockaddr_union *, u_int8_t); 503 504/* XF_IP4 */ 505extern int ipe4_attach(void); 506extern int ipe4_init(struct tdb *, struct xformsw *, struct ipsecinit *); 507extern int ipe4_zeroize(struct tdb *); 508extern int ipe4_output(struct mbuf *, struct tdb *, struct mbuf **, int, int); 509extern void ipe4_input __P((struct mbuf *, ...)); 510extern void ip4_input __P((struct mbuf *, ...)); 511 512/* XF_ETHERIP */ 513extern int etherip_output(struct mbuf *, struct tdb *, struct mbuf **, 514 int, int); 515extern void etherip_input __P((struct mbuf *, ...)); 516 517/* XF_OLD_AH */ 518extern int ah_old_attach(void); 519extern int ah_old_init(struct tdb *, struct xformsw *, struct ipsecinit *); 520extern int ah_old_zeroize(struct tdb *); 521extern int ah_old_output(struct mbuf *, struct tdb *, struct mbuf **, 522 int, int); 523extern struct mbuf *ah_old_input(struct mbuf *, struct tdb *, int, int); 524 525/* XF_NEW_AH */ 526extern int ah_new_attach(void); 527extern int ah_new_init(struct tdb *, struct xformsw *, struct ipsecinit *); 528extern int ah_new_zeroize(struct tdb *); 529extern int ah_new_output(struct mbuf *, struct tdb *, struct mbuf **, 530 int, int); 531extern struct mbuf *ah_new_input(struct mbuf *, struct tdb *, int, int); 532 533/* XF_OLD_ESP */ 534extern int esp_old_attach(void); 535extern int esp_old_init(struct tdb *, struct xformsw *, struct ipsecinit *); 536extern int esp_old_zeroize(struct tdb *); 537extern int esp_old_output(struct mbuf *, struct tdb *, struct mbuf **, 538 int, int); 539extern struct mbuf *esp_old_input(struct mbuf *, struct tdb *, int, int); 540 541/* XF_NEW_ESP */ 542extern int esp_new_attach(void); 543extern int esp_new_init(struct tdb *, struct xformsw *, struct ipsecinit *); 544extern int esp_new_zeroize(struct tdb *); 545extern int esp_new_output(struct mbuf *, struct tdb *, struct mbuf **, 546 int, int); 547extern struct mbuf *esp_new_input(struct mbuf *, struct tdb *, int, int); 548 549/* XF_TCPSIGNATURE */ 550extern int tcp_signature_tdb_attach __P((void)); 551extern int tcp_signature_tdb_init __P((struct tdb *, struct xformsw *, 552 struct ipsecinit *)); 553extern int tcp_signature_tdb_zeroize __P((struct tdb *)); 554extern struct mbuf *tcp_signature_tdb_input __P((struct mbuf *, struct tdb *)); 555extern int tcp_signature_tdb_output __P((struct mbuf *, struct tdb *, 556 struct mbuf **)); 557 558/* Padding */ 559extern caddr_t m_pad(struct mbuf *, int, int); 560 561/* Replay window */ 562extern int checkreplaywindow32(u_int32_t, u_int32_t, u_int32_t *, u_int32_t, 563 u_int32_t *); 564 565extern unsigned char ipseczeroes[]; 566#endif /* _KERNEL */ 567#endif /* _NETINET_IPSP_H_ */ 568