… | |
… | |
35 | #include "slog.h" |
35 | #include "slog.h" |
36 | #include "device.h" |
36 | #include "device.h" |
37 | #include "vpn.h" |
37 | #include "vpn.h" |
38 | #include "connection.h" |
38 | #include "connection.h" |
39 | |
39 | |
|
|
40 | #include <sys/socket.h> |
|
|
41 | #ifdef HAVE_NETINET_IN_H |
|
|
42 | # include <netinet/in.h> |
|
|
43 | #endif |
|
|
44 | #include <arpa/inet.h> |
|
|
45 | #include <net/if.h> |
|
|
46 | #ifdef HAVE_NETINET_IN_SYSTM_H |
|
|
47 | # include <netinet/in_systm.h> |
|
|
48 | #endif |
|
|
49 | #ifdef HAVE_NETINET_IP_H |
|
|
50 | # include <netinet/ip.h> |
|
|
51 | #endif |
|
|
52 | |
|
|
53 | #ifndef IPTOS_TOS_MASK |
|
|
54 | # define IPTOS_TOS_MASK (IPTOS_LOWDELAY | IPTOS_THROUGHPUT | IPTOS_RELIABILITY | IPTOS_MINCOST) |
|
|
55 | #endif |
|
|
56 | |
40 | #if !HAVE_RAND_PSEUDO_BYTES |
57 | #if !HAVE_RAND_PSEUDO_BYTES |
41 | # define RAND_pseudo_bytes RAND_bytes |
58 | # define RAND_pseudo_bytes RAND_bytes |
42 | #endif |
59 | #endif |
43 | |
60 | |
44 | #define MAGIC "vped\xbd\xc6\xdb\x82" // 8 bytes of magic |
61 | #define MAGIC "vped\xbd\xc6\xdb\x82" // 8 bytes of magic |
… | |
… | |
197 | // only do action once every x seconds per host whole allowing bursts. |
214 | // only do action once every x seconds per host whole allowing bursts. |
198 | // this implementation ("splay list" ;) is inefficient, |
215 | // this implementation ("splay list" ;) is inefficient, |
199 | // but low on resources. |
216 | // but low on resources. |
200 | struct net_rate_limiter : list<net_rateinfo> |
217 | struct net_rate_limiter : list<net_rateinfo> |
201 | { |
218 | { |
202 | static const double ALPHA = 1. - 1. / 180.; // allow bursts |
219 | static const double ALPHA = 1. - 1. / 600.; // allow bursts |
203 | static const double CUTOFF = 10.; // one event every CUTOFF seconds |
220 | static const double CUTOFF = 10.; // one event every CUTOFF seconds |
204 | static const double EXPIRE = CUTOFF * 30.; // expire entries after this time |
221 | static const double EXPIRE = CUTOFF * 30.; // expire entries after this time |
205 | static const double MAXDIF = CUTOFF * (1. / (1. - ALPHA)); // maximum diff /count value |
222 | static const double MAXDIF = CUTOFF * (1. / (1. - ALPHA)); // maximum diff /count value |
206 | |
223 | |
207 | bool can (const sockinfo &si) { return can((u32)si.host); } |
224 | bool can (const sockinfo &si) { return can((u32)si.host); } |
208 | bool can (u32 host); |
225 | bool can (u32 host); |
209 | }; |
226 | }; |
210 | |
227 | |
211 | net_rate_limiter auth_rate_limiter, reset_rate_limiter; |
228 | net_rate_limiter auth_rate_limiter, reset_rate_limiter; |
212 | |
229 | |
… | |
… | |
475 | set_hdr (type, dst); |
492 | set_hdr (type, dst); |
476 | } |
493 | } |
477 | |
494 | |
478 | bool config_packet::chk_config () const |
495 | bool config_packet::chk_config () const |
479 | { |
496 | { |
480 | return prot_major == PROTOCOL_MAJOR |
497 | if (prot_major != PROTOCOL_MAJOR) |
481 | && randsize == RAND_SIZE |
498 | slog (L_WARN, _("major version mismatch (%d <=> %d)"), prot_major, PROTOCOL_MAJOR); |
482 | && hmaclen == HMACLENGTH |
499 | else if (randsize != RAND_SIZE) |
483 | && flags == curflags () |
500 | slog (L_WARN, _("rand size mismatch (%d <=> %d)"), randsize, RAND_SIZE); |
|
|
501 | else if (hmaclen != HMACLENGTH) |
|
|
502 | slog (L_WARN, _("hmac length mismatch (%d <=> %d)"), hmaclen, HMACLENGTH); |
|
|
503 | else if (flags != curflags ()) |
|
|
504 | slog (L_WARN, _("flag mismatch (%x <=> %x)"), flags, curflags ()); |
484 | && challengelen == sizeof (rsachallenge) |
505 | else if (challengelen != sizeof (rsachallenge)) |
|
|
506 | slog (L_WARN, _("challenge length mismatch (%d <=> %d)"), challengelen, sizeof (rsachallenge)); |
485 | && cipher_nid == htonl (EVP_CIPHER_nid (CIPHER)) |
507 | else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER))) |
|
|
508 | slog (L_WARN, _("cipher mismatch (%x <=> %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER)); |
486 | && digest_nid == htonl (EVP_MD_type (RSA_HASH)) |
509 | else if (digest_nid != htonl (EVP_MD_type (RSA_HASH))) |
|
|
510 | slog (L_WARN, _("digest mismatch (%x <=> %x)"), ntohl (digest_nid), EVP_MD_type (RSA_HASH)); |
487 | && hmac_nid == htonl (EVP_MD_type (DIGEST)); |
511 | else if (hmac_nid != htonl (EVP_MD_type (DIGEST))) |
|
|
512 | slog (L_WARN, _("hmac mismatch (%x <=> %x)"), ntohl (hmac_nid), EVP_MD_type (DIGEST)); |
|
|
513 | else |
|
|
514 | return true; |
|
|
515 | |
|
|
516 | return false; |
488 | } |
517 | } |
489 | |
518 | |
490 | struct auth_req_packet : config_packet |
519 | struct auth_req_packet : config_packet |
491 | { |
520 | { |
492 | char magic[8]; |
521 | char magic[8]; |
… | |
… | |
898 | rsachallenge k; |
927 | rsachallenge k; |
899 | |
928 | |
900 | if (0 > RSA_private_decrypt (sizeof (p->encr), |
929 | if (0 > RSA_private_decrypt (sizeof (p->encr), |
901 | (unsigned char *)&p->encr, (unsigned char *)&k, |
930 | (unsigned char *)&p->encr, (unsigned char *)&k, |
902 | ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING)) |
931 | ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING)) |
903 | slog (L_ERR, _("%s(%s): challenge illegal or corrupted"), |
932 | slog (L_ERR, _("%s(%s): challenge illegal or corrupted (%s). mismatched key or config file?"), |
904 | conf->nodename, (const char *)rsi); |
933 | conf->nodename, (const char *)rsi, ERR_error_string (ERR_get_error (), 0)); |
905 | else |
934 | else |
906 | { |
935 | { |
907 | delete octx; |
936 | delete octx; |
908 | |
937 | |
909 | octx = new crypto_ctx (k, 1); |
938 | octx = new crypto_ctx (k, 1); |
… | |
… | |
916 | connection_established (); |
945 | connection_established (); |
917 | |
946 | |
918 | break; |
947 | break; |
919 | } |
948 | } |
920 | } |
949 | } |
|
|
950 | else |
|
|
951 | slog (L_WARN, _("%s(%s): protocol mismatch"), |
|
|
952 | conf->nodename, (const char *)rsi); |
921 | |
953 | |
922 | send_reset (rsi); |
954 | send_reset (rsi); |
923 | } |
955 | } |
924 | |
956 | |
925 | break; |
957 | break; |
… | |
… | |
938 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
970 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
939 | |
971 | |
940 | rsachallenge chg; |
972 | rsachallenge chg; |
941 | |
973 | |
942 | if (!rsa_cache.find (p->id, chg)) |
974 | if (!rsa_cache.find (p->id, chg)) |
|
|
975 | { |
943 | slog (L_ERR, _("%s(%s): unrequested auth response"), |
976 | slog (L_ERR, _("%s(%s): unrequested auth response ignored"), |
944 | conf->nodename, (const char *)rsi); |
977 | conf->nodename, (const char *)rsi); |
|
|
978 | break; |
|
|
979 | } |
945 | else |
980 | else |
946 | { |
981 | { |
947 | crypto_ctx *cctx = new crypto_ctx (chg, 0); |
982 | crypto_ctx *cctx = new crypto_ctx (chg, 0); |
948 | |
983 | |
949 | if (!p->hmac_chk (cctx)) |
984 | if (!p->hmac_chk (cctx)) |
|
|
985 | { |
950 | slog (L_ERR, _("%s(%s): hmac authentication error on auth response, received invalid packet\n" |
986 | slog (L_ERR, _("%s(%s): hmac authentication error on auth response, received invalid packet\n" |
951 | "could be an attack, or just corruption or an synchronization error"), |
987 | "could be an attack, or just corruption or an synchronization error"), |
952 | conf->nodename, (const char *)rsi); |
988 | conf->nodename, (const char *)rsi); |
|
|
989 | break; |
|
|
990 | } |
953 | else |
991 | else |
954 | { |
992 | { |
955 | rsaresponse h; |
993 | rsaresponse h; |
956 | |
994 | |
957 | rsa_hash (p->id, chg, h); |
995 | rsa_hash (p->id, chg, h); |
… | |
… | |
1025 | c->inject_data_packet (d); |
1063 | c->inject_data_packet (d); |
1026 | } |
1064 | } |
1027 | |
1065 | |
1028 | if (si != rsi) |
1066 | if (si != rsi) |
1029 | { |
1067 | { |
1030 | // fast re-sync on conneciton changes, useful especially for tcp/ip |
1068 | // fast re-sync on connection changes, useful especially for tcp/ip |
1031 | si = rsi; |
1069 | si = rsi; |
1032 | |
1070 | |
1033 | slog (L_INFO, _("%s(%s): socket address changed to %s"), |
1071 | slog (L_INFO, _("%s(%s): socket address changed to %s"), |
1034 | conf->nodename, (const char *)si, (const char *)rsi); |
1072 | conf->nodename, (const char *)si, (const char *)rsi); |
1035 | } |
1073 | } |