00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _HIP_TYPES_H_
00026 #define _HIP_TYPES_H_
00027
00028
00029 #if defined (__MACOSX__)
00030 #include <mac/mac_types.h>
00031 #elif defined (__WIN32__)
00032 #include <win32/types.h>
00033 #else
00034 #include <asm/types.h>
00035 #endif
00036
00037 #ifdef __MACOSX__
00038 #include <sys/types.h>
00039 #include <sys/time.h>
00040 #endif
00041 #ifdef __WIN32__
00042 #include <winsock2.h>
00043 #else
00044 #include <sys/socket.h>
00045 #include <netinet/in.h>
00046 #endif
00047 #include <sys/types.h>
00048 #include <openssl/bn.h>
00049 #include <openssl/hmac.h>
00050 #include <openssl/rsa.h>
00051 #include <time.h>
00052
00053 #include <hip/hip_proto.h>
00054
00055
00056
00057
00058 #define HIP_PUB_SUFFIX "_host_identities.pub.xml"
00059 #ifndef __WIN32__
00060 #define HIP_CONF_FILENAME "../etc/hip/hip.conf"
00061 #define HIP_MYID_FILENAME "../etc/hip/my_host_identities.xml"
00062 #define HIP_KNOWNID_FILENAME "../etc/hip/known_host_identities.xml"
00063 #define HIP_REG_FILENAME "../etc/hip/registered_host_identities.xml"
00064 #define HIP_PUB_PREFIX "../etc/hip/"
00065
00066
00067
00068 #define HIP_DEFAULT_CONF_FILENAME "/usr/local/etc/hip/hip.conf"
00069 #define HIP_DEFAULT_MYID_FILENAME "/usr/local/etc/hip/my_host_identities.xml"
00070 #define HIP_DEFAULT_KNOWNID_FILENAME "/usr/local/etc/hip/known_host_identities.xml"
00071 #define HIP_DEFAULT_REG_FILENAME "/usr/local/etc/hip/registered_host_identities.xml"
00072 #define HIP_LOCK_FILENAME "/var/run/hip.pid"
00073 #define HIP_LOG_FILENAME "/var/log/hipd.log"
00074 #else
00075 #define HIP_CONF_FILENAME "hip.conf"
00076 #define HIP_MYID_FILENAME "my_host_identities.xml"
00077 #define HIP_KNOWNID_FILENAME "known_host_identities.xml"
00078 #define HIP_REG_FILENAME "registered_host_identities.xml"
00079 #define HIP_PUB_PREFIX ""
00080
00081
00082
00083 #define HIP_DEFAULT_CONF_FILENAME HIP_CONF_FILENAME
00084 #define HIP_DEFAULT_MYID_FILENAME HIP_MYID_FILENAME
00085 #define HIP_DEFAULT_KNOWNID_FILENAME HIP_KNOWNID_FILENAME
00086 #define HIP_DEFAULT_REG_FILENAME HIP_REG_FILENAME
00087 #define HIP_LOCK_FILENAME "hip.pid"
00088 #define HIP_LOG_FILENAME "hipd.log"
00089 #endif
00090
00091
00092
00093
00094 #define MAX_HITS 32
00095 #define MAX_CONNECTIONS 32
00096 #define MAX_OPAQUE_SIZE 255
00097 #define MAX_HI_NAMESIZE 255
00098 #define MAX_HI_BITS 2048
00099
00100 #define MAX_LOCATORS 8
00101 #define MAX_REGISTRATIONS 1024
00102
00103
00104
00105
00106 #define DSA_PRIV 20
00107 #define HIP_KEY_SIZE 24
00108 #define HIP_DSA_SIG_SIZE 41
00109 #define MAX_SIG_SIZE 512
00110 #define NUMKEYS 8
00111 #define KEYMAT_SIZE (4 * NUMKEYS * HIP_KEY_SIZE)
00112
00113
00114
00115
00116
00117
00118
00119
00120 #define HIP_RES_SHIM6_BITS 0x01
00121
00122
00123 #define H_PROTO_UDP 17
00124 #define HIP_ESP_UDP_PORT 54500
00125
00126
00127 #define SADB_RESPONSE_BUFFER 20
00128 #define SADB_MSG_SIZE_ADD 300
00129 #define SADB_MSG_SIZE_DELETE 150
00130
00131
00132
00133
00134 #define TRUE 1
00135 #define FALSE 0
00136
00137
00138
00139 #define SPI_RESERVED 255
00140 #define HIP_ALIGN 4
00141 #define R1_CACHE_SIZE 8
00142 #define ACCEPTABLE_R1_COUNT_RANGE 2
00143 #ifndef HIP_UPDATE_BIND_CHECKS
00144 #define HIP_UPDATE_BIND_CHECKS 5
00145 #endif
00146
00147
00148
00149
00150 #define DBG(x) x
00151 #define TDIFF(a, b) ((a).tv_sec - (b).tv_sec)
00152
00153
00154
00155 #define SA2IP(x) (((struct sockaddr*)x)->sa_family==AF_INET) ? \
00156 (void*)&((struct sockaddr_in*)x)->sin_addr : \
00157 (void*)&((struct sockaddr_in6*)x)->sin6_addr
00158
00159 #define SALEN(x) (((struct sockaddr*)x)->sa_family==AF_INET) ? \
00160 sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6)
00161
00162 #define SAIPLEN(x) (((struct sockaddr*)x)->sa_family==AF_INET) ? 4 : 16
00163
00164 #define SA(x) ((struct sockaddr*)x)
00165
00166
00167 #define yesno(x) x ? "yes" : "no"
00168
00169
00170 #define HIT_SIZE 16
00171
00172 #define HIT_PREFIX_SHA1_32BITS 0x20010010
00173
00174 #define HIT2LSI(a) ( 0x01000000L | \
00175 ((a[HIT_SIZE-3]<<16)+(a[HIT_SIZE-2]<<8)+(a[HIT_SIZE-1])))
00176
00177
00178 #define YLIFE(x) ((float)x-(float)64)/(float)8
00179
00180
00181
00182
00183 typedef __u8 hip_hit [HIT_SIZE];
00184
00185
00186
00187 #define HIP_UDP_PORT 50500
00188 #define HIP_KEEPALIVE_TIMEOUT 20
00189
00190
00191
00192
00193 typedef struct _udphdr {
00194 __u16 src_port;
00195 __u16 dst_port;
00196 __u16 len;
00197 __u16 checksum;
00198 } udphdr;
00199
00200
00201
00202
00203
00204 typedef struct _hiphdr {
00205 __u8 nxt_hdr;
00206 __u8 hdr_len;
00207 __u8 packet_type;
00208
00209 #if defined(__MACOSX__) && defined(__BIG_ENDIAN__)
00210 __u8 version:4,res:4;
00211 #else
00212 __u8 res:4,version:4;
00213 #endif
00214 __u16 checksum;
00215 __u16 control;
00216 hip_hit hit_sndr;
00217 hip_hit hit_rcvr;
00218
00219 } hiphdr;
00220
00221
00222
00223
00224 typedef struct _hipcookie {
00225 __u8 k;
00226 __u8 lifetime;
00227 __u16 opaque;
00228 __u64 i __attribute__ ((packed));
00229 } hipcookie;
00230
00231 struct key_entry {
00232 int type;
00233 int length;
00234 __u8 key[HIP_KEY_SIZE];
00235 };
00236
00237 struct rekey_info {
00238 __u32 update_id;
00239 __u32 new_spi;
00240 __u16 keymat_index;
00241 __u8 acked;
00242 __u8 dh_group_id;
00243 DH *dh;
00244 struct timeval rk_time;
00245 };
00246
00247
00248
00249
00250 struct hip_packet_entry {
00251 __u8 *packet;
00252 int len;
00253 struct timeval xmit_time;
00254 __u32 retransmits;
00255 struct sockaddr_storage dst;
00256 };
00257
00258
00259
00260
00261
00262 typedef struct _hip_assoc {
00263
00264 struct _hi_node *hi;
00265 struct _hi_node *peer_hi;
00266
00267 int state;
00268 struct timeval state_time;
00269 struct timeval use_time;
00270 __u64 used_bytes_in;
00271 __u64 used_bytes_out;
00272 __u32 spi_in;
00273 __u32 spi_out;
00274 hipcookie cookie_r;
00275 __u64 cookie_j;
00276 struct hip_packet_entry rexmt_cache;
00277 struct opaque_entry *opaque;
00278 struct rekey_info *rekey;
00279 struct rekey_info *peer_rekey;
00280
00281 __u16 hip_transform;
00282 __u16 esp_transform;
00283 __u16 available_transforms;
00284 __u8 dh_group_id;
00285 DH *dh;
00286 DH *peer_dh;
00287 __u8 *dh_secret;
00288 __u16 keymat_index;
00289 __u8 keymat[KEYMAT_SIZE] __attribute__ ((packed));
00290 struct key_entry keys[NUMKEYS] __attribute__ ((packed));
00291 __u8 preserve_outbound_policy;
00292 int use_udp;
00293 int next_use_udp;
00294 __u16 peer_dst_port;
00295 struct timeval use_time_ka;
00296 __u16 peer_esp_dst_port;
00297 #ifdef __MACOSX__
00298 __u16 ipfw_rule;
00299 #endif
00300 } hip_assoc;
00301 #define HIPA_SRC(h) ((struct sockaddr*)&h->hi->addrs.addr)
00302 #define HIPA_DST(h) ((struct sockaddr*)&h->peer_hi->addrs.addr)
00303
00304
00305
00306
00307
00308 typedef struct _hip_reg {
00309 hip_hit peer_hit;
00310 #ifdef __WIN32__
00311 HANDLE peer_addr_mutex;
00312 #else
00313 pthread_mutex_t peer_addr_mutex;
00314 #endif
00315 struct sockaddr_storage peer_addr;
00316 double lifetime;
00317 int update;
00318 hip_assoc *hip_a;
00319 } hip_reg;
00320
00321
00322
00323
00324 typedef struct _returned {
00325 int position;
00326 int update;
00327 } returned;
00328
00329
00330
00331
00332 typedef struct _from {
00333 int add_from;
00334 hip_hit hit_from;
00335 struct sockaddr_storage ip_rvs;
00336 struct sockaddr_storage ip_from;
00337 } from;
00338
00339 typedef struct _via {
00340 int add_via_rvs;
00341 struct sockaddr_storage ip_from;
00342 } via;
00343
00344
00345
00346
00347 typedef struct _sockaddr_list
00348 {
00349 struct _sockaddr_list *next;
00350 struct sockaddr_storage addr;
00351 int if_index;
00352 int lifetime;
00353 int status;
00354 int preferred;
00355 __u32 nonce;
00356 struct timeval creation_time;
00357 } sockaddr_list;
00358
00359
00360
00361
00362 typedef struct _r1_cache_entry
00363 {
00364
00365 __u8 *packet;
00366 int len;
00367
00368 hipcookie *current_puzzle;
00369 hipcookie *previous_puzzle;
00370
00371 struct _dh_cache_entry *dh_entry;
00372
00373 struct timeval creation_time;
00374 } r1_cache_entry;
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395 typedef struct _hi_node {
00396 struct _hi_node *next;
00397 hip_hit hit;
00398 struct sockaddr_storage lsi;
00399 struct sockaddr_storage rvs;
00400
00401
00402
00403
00404
00405
00406
00407 #ifdef __WIN32__
00408 HANDLE addrs_mutex;
00409 #else
00410 pthread_mutex_t addrs_mutex;
00411 #endif
00412 struct _sockaddr_list addrs;
00413
00414 int size;
00415 DSA *dsa;
00416 RSA *rsa;
00417 struct _r1_cache_entry r1_cache[R1_CACHE_SIZE];
00418 __u64 r1_gen_count;
00419 __u32 update_id;
00420
00421 char algorithm_id;
00422 char anonymous;
00423 char allow_incoming;
00424 char skip_addrcheck;
00425 char name[MAX_HI_NAMESIZE];
00426 int name_len;
00427 } hi_node;
00428
00429
00430
00431 typedef struct _dh_cache_entry
00432 {
00433 struct _dh_cache_entry *next;
00434 __u8 group_id;
00435 DH *dh;
00436 __u8 is_current;
00437
00438 int ref_count;
00439 struct timeval creation_time;
00440 } dh_cache_entry;
00441
00442
00443
00444 struct opaque_entry
00445 {
00446 __u16 opaque_len;
00447 __u8 opaque_data[MAX_OPAQUE_SIZE];
00448 __u8 opaque_nosig;
00449 };
00450
00451 typedef struct _pseudo_header6
00452 {
00453 unsigned char src_addr[16];
00454 unsigned char dst_addr[16];
00455 __u32 packet_length;
00456 char zero[3];
00457 __u8 next_hdr;
00458 } pseudo_header6;
00459
00460 typedef struct _pseudo_header
00461 {
00462 unsigned char src_addr[4];
00463 unsigned char dst_addr[4];
00464 __u8 zero;
00465 __u8 protocol;
00466 __u16 packet_length;
00467 } pseudo_header;
00468
00469
00470
00471
00472
00473 typedef struct _tlv_head
00474 {
00475 __u16 type;
00476 __u16 length;
00477 } tlv_head;
00478
00479 typedef struct _tlv_esp_info
00480 {
00481 __u16 type;
00482 __u16 length;
00483 __u16 reserved;
00484 __u16 keymat_index;
00485 __u32 old_spi;
00486 __u32 new_spi;
00487 } tlv_esp_info;
00488
00489 typedef struct _tlv_r1_counter
00490 {
00491 __u16 type;
00492 __u16 length;
00493 __u32 reserved;
00494 __u64 r1_gen_counter;
00495 } tlv_r1_counter;
00496
00497 typedef struct _tlv_puzzle
00498 {
00499 __u16 type;
00500 __u16 length;
00501 hipcookie cookie;
00502 } tlv_puzzle;
00503
00504 typedef struct _tlv_solution
00505 {
00506 __u16 type;
00507 __u16 length;
00508 hipcookie cookie;
00509 __u64 j;
00510 } tlv_solution;
00511
00512 typedef struct _tlv_diffie_hellman
00513 {
00514 __u16 type;
00515 __u16 length;
00516 __u8 group_id __attribute__ ((packed));
00517 __u16 pub_len __attribute__ ((packed));
00518 __u8 pub[1];
00519 } tlv_diffie_hellman;
00520
00521
00522 typedef struct _tlv_diffie_hellman_pub_value
00523 {
00524 __u8 group_id;
00525 __u16 pub_len;
00526 __u8 pub[1];
00527 } tlv_diffie_hellman_pub_value;
00528
00529 typedef struct _tlv_hip_transform
00530 {
00531 __u16 type;
00532 __u16 length;
00533 __u16 transform_id;
00534 } tlv_hip_transform;
00535
00536 typedef struct _tlv_esp_transform
00537 {
00538 __u16 type;
00539 __u16 length;
00540 __u16 reserved;
00541 __u16 suite_id;
00542 } tlv_esp_transform;
00543
00544 typedef struct _tlv_encrypted
00545 {
00546 __u16 type;
00547 __u16 length;
00548 __u8 reserved[4];
00549 __u8 iv[8];
00550
00551 __u8 enc_data[1];
00552 } tlv_encrypted;
00553
00554 typedef struct _tlv_host_id
00555 {
00556 __u16 type;
00557 __u16 length;
00558 __u16 hi_length;
00559 __u16 di_type_length;
00560 __u8 hi_hdr[4];
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570 } tlv_host_id;
00571
00572 typedef struct _tlv_cert
00573 {
00574 __u16 type;
00575 __u16 length;
00576 __u8 cert_count;
00577 __u8 cert_id;
00578 __u8 cert_type;
00579 __u8 certificate[1];
00580 } tlv_cert;
00581
00582 typedef struct _tlv_reg_info
00583 {
00584 __u16 type;
00585 __u16 length;
00586 __u8 min_lifetime;
00587 __u8 max_lifetime;
00588 __u8 reg_type;
00589 } tlv_reg_info;
00590
00591 typedef struct _tlv_reg_request
00592 {
00593 __u16 type;
00594 __u16 length;
00595 __u8 lifetime;
00596 __u8 reg_type;
00597 } tlv_reg_request;
00598
00599 typedef struct _tlv_reg_response
00600 {
00601 __u16 type;
00602 __u16 length;
00603 __u8 lifetime;
00604 __u8 reg_type;
00605 } tlv_reg_response;
00606
00607 typedef struct _tlv_reg_failed
00608 {
00609 __u16 type;
00610 __u16 length;
00611 __u8 fail_type;
00612 __u8 reg_type;
00613 } tlv_reg_failed;
00614
00615
00616 #ifndef __WIN32__
00617 typedef struct _tlv_reg_required
00618 {
00619
00620 } tlv_reg_required;
00621 #endif
00622
00623
00624 typedef struct _tlv_echo
00625 {
00626 __u16 type;
00627 __u16 length;
00628 __u8 opaque_data[1];
00629
00630 } tlv_echo;
00631
00632 typedef struct _tlv_hmac
00633 {
00634 __u16 type;
00635 __u16 length;
00636 __u8 hmac[20];
00637 } tlv_hmac;
00638
00639 typedef struct _tlv_hip_sig
00640 {
00641 __u16 type;
00642 __u16 length;
00643 __u8 algorithm;
00644 __u8 signature[0];
00645 } tlv_hip_sig;
00646
00647 typedef struct _tlv_seq
00648 {
00649 __u16 type;
00650 __u16 length;
00651 __u32 update_id;
00652 } tlv_seq;
00653
00654 typedef struct _tlv_ack
00655 {
00656 __u16 type;
00657 __u16 length;
00658 __u32 peer_update_id;
00659 } tlv_ack;
00660
00661 typedef struct _tlv_notify
00662 {
00663 __u16 type;
00664 __u16 length;
00665 __u16 reserved;
00666 __u16 notify_type;
00667 __u8 notify_data[0];
00668 } tlv_notify;
00669
00670 typedef struct _locator
00671 {
00672 __u8 traffic_type;
00673 __u8 locator_type;
00674 __u8 locator_length;
00675 __u8 reserved;
00676 __u32 locator_lifetime;
00677 __u8 locator[20];
00678 } locator;
00679 #define LOCATOR_PREFERRED 0x01
00680 #define LOCATOR_TRAFFIC_TYPE_BOTH 0x00
00681 #define LOCATOR_TRAFFIC_TYPE_SIGNALING 0x01
00682 #define LOCATOR_TRAFFIC_TYPE_DATA 0x02
00683 #define LOCATOR_TYPE_IPV6 0x00
00684 #define LOCATOR_TYPE_SPI_IPV6 0x01
00685
00686 typedef struct _tlv_locator
00687 {
00688 __u16 type;
00689 __u16 length;
00690 locator locator1[1];
00691 } tlv_locator;
00692
00693 typedef struct _tlv_from
00694 {
00695 __u16 type;
00696 __u16 length;
00697 unsigned char addr[16];
00698 } tlv_from;
00699
00700 typedef struct _tlv_rvs_hmac
00701 {
00702 __u16 type;
00703 __u16 length;
00704 __u8 hmac[20];
00705
00706 } tlv_rvs_hmac;
00707
00708 typedef struct _tlv_via_rvs
00709 {
00710 __u16 type;
00711 __u16 length;
00712 __u8 address[16];
00713 } tlv_via_rvs;
00714
00715
00716
00717
00718
00719 typedef struct _thread_arg{
00720 hiphdr hip_header;
00721 tlv_reg_response resp;
00722 } thread_arg;
00723
00724
00725
00726
00727
00728 typedef enum {
00729 D_DEFAULT,
00730 D_VERBOSE,
00731 D_QUIET,
00732 } DEBUG_MODES;
00733
00734 typedef enum {
00735 NORM,
00736 NORMT,
00737 WARN,
00738 ERR,
00739 QOUT,
00740 } LOG_LEVELS;
00741
00742
00743
00744
00745
00746 struct hip_opt {
00747 int daemon;
00748 int debug;
00749 int debug_R1;
00750 int no_retransmit;
00751 int permissive;
00752 int opportunistic;
00753 int allow_any;
00754 int enable_udp;
00755 struct sockaddr *trigger;
00756 int use_i3;
00757 int rvs;
00758 int entries;
00759 int stun;
00760 };
00761
00762
00763
00764
00765 struct hip_conf {
00766 __u8 min_lifetime;
00767 __u8 max_lifetime;
00768 __u8 reg_type_rvs;
00769 __u8 lifetime;
00770 __u8 reg_type;
00771 __u32 cookie_difficulty;
00772 __u32 cookie_lifetime;
00773 __u32 packet_timeout;
00774 __u32 max_retries;
00775 __u32 sa_lifetime;
00776 __u32 loc_lifetime;
00777 char *preferred_hi;
00778 __u8 send_hi_name;
00779 __u8 dh_group;
00780 __u32 dh_lifetime;
00781 __u32 r1_lifetime;
00782 __u32 failure_timeout;
00783 __u32 msl;
00784 __u32 ual;
00785 __u16 esp_transforms[SUITE_ID_MAX];
00786 __u16 hip_transforms[SUITE_ID_MAX];
00787 char *log_filename;
00788 struct sockaddr_storage dht_server;
00789 struct sockaddr_storage dns_server;
00790 __u8 disable_dns_lookups;
00791 __u8 disable_notify;
00792 #ifdef __UMH__
00793 __u8 disable_dns_thread;
00794 __u8 enable_bcast;
00795 #endif
00796 struct sockaddr_storage preferred;
00797 char *preferred_iface;
00798 __u8 save_known_identities;
00799 };
00800
00801
00802 #endif
00803
00804
00805