… | |
… | |
202 | // only do action once every x seconds per host whole allowing bursts. |
202 | // only do action once every x seconds per host whole allowing bursts. |
203 | // this implementation ("splay list" ;) is inefficient, |
203 | // this implementation ("splay list" ;) is inefficient, |
204 | // but low on resources. |
204 | // but low on resources. |
205 | struct net_rate_limiter : list<net_rateinfo> |
205 | struct net_rate_limiter : list<net_rateinfo> |
206 | { |
206 | { |
207 | static const double ALPHA = 1. - 1. / 600.; // allow bursts |
207 | # define NRL_ALPHA (1. - 1. / 600.) // allow bursts |
208 | static const double CUTOFF = 10.; // one event every CUTOFF seconds |
208 | # define NRL_CUTOFF 10. // one event every CUTOFF seconds |
209 | static const double EXPIRE = CUTOFF * 30.; // expire entries after this time |
209 | # define NRL_EXPIRE (NRL_CUTOFF * 30.) // expire entries after this time |
210 | static const double MAXDIF = CUTOFF * (1. / (1. - ALPHA)); // maximum diff /count value |
210 | # define NRL_MAXDIF (NRL_CUTOFF * (1. / (1. - NRL_ALPHA))) // maximum diff /count value |
211 | |
211 | |
212 | bool can (const sockinfo &si) { return can((u32)si.host); } |
212 | bool can (const sockinfo &si) { return can((u32)si.host); } |
213 | bool can (u32 host); |
213 | bool can (u32 host); |
214 | }; |
214 | }; |
215 | |
215 | |
… | |
… | |
220 | iterator i; |
220 | iterator i; |
221 | |
221 | |
222 | for (i = begin (); i != end (); ) |
222 | for (i = begin (); i != end (); ) |
223 | if (i->host == host) |
223 | if (i->host == host) |
224 | break; |
224 | break; |
225 | else if (i->last < NOW - EXPIRE) |
225 | else if (i->last < NOW - NRL_EXPIRE) |
226 | i = erase (i); |
226 | i = erase (i); |
227 | else |
227 | else |
228 | i++; |
228 | i++; |
229 | |
229 | |
230 | if (i == end ()) |
230 | if (i == end ()) |
231 | { |
231 | { |
232 | net_rateinfo ri; |
232 | net_rateinfo ri; |
233 | |
233 | |
234 | ri.host = host; |
234 | ri.host = host; |
235 | ri.pcnt = 1.; |
235 | ri.pcnt = 1.; |
236 | ri.diff = MAXDIF; |
236 | ri.diff = NRL_MAXDIF; |
237 | ri.last = NOW; |
237 | ri.last = NOW; |
238 | |
238 | |
239 | push_front (ri); |
239 | push_front (ri); |
240 | |
240 | |
241 | return true; |
241 | return true; |
… | |
… | |
243 | else |
243 | else |
244 | { |
244 | { |
245 | net_rateinfo ri (*i); |
245 | net_rateinfo ri (*i); |
246 | erase (i); |
246 | erase (i); |
247 | |
247 | |
248 | ri.pcnt = ri.pcnt * ALPHA; |
248 | ri.pcnt = ri.pcnt * NRL_ALPHA; |
249 | ri.diff = ri.diff * ALPHA + (NOW - ri.last); |
249 | ri.diff = ri.diff * NRL_ALPHA + (NOW - ri.last); |
250 | |
250 | |
251 | ri.last = NOW; |
251 | ri.last = NOW; |
252 | |
252 | |
253 | double dif = ri.diff / ri.pcnt; |
253 | double dif = ri.diff / ri.pcnt; |
254 | |
254 | |
255 | bool send = dif > CUTOFF; |
255 | bool send = dif > NRL_CUTOFF; |
256 | |
256 | |
257 | if (dif > MAXDIF) |
257 | if (dif > NRL_MAXDIF) |
258 | { |
258 | { |
259 | ri.pcnt = 1.; |
259 | ri.pcnt = 1.; |
260 | ri.diff = MAXDIF; |
260 | ri.diff = NRL_MAXDIF; |
261 | } |
261 | } |
262 | else if (send) |
262 | else if (send) |
263 | ri.pcnt++; |
263 | ri.pcnt++; |
264 | |
264 | |
265 | push_front (ri); |
265 | push_front (ri); |
… | |
… | |
456 | u8 flags, challengelen, features, pad3; |
456 | u8 flags, challengelen, features, pad3; |
457 | u32 cipher_nid, digest_nid, hmac_nid; |
457 | u32 cipher_nid, digest_nid, hmac_nid; |
458 | |
458 | |
459 | void setup (ptype type, int dst); |
459 | void setup (ptype type, int dst); |
460 | bool chk_config () const; |
460 | bool chk_config () const; |
|
|
461 | |
|
|
462 | static u8 get_features () |
|
|
463 | { |
|
|
464 | u8 f = 0; |
|
|
465 | #if ENABLE_COMPRESSION |
|
|
466 | f |= FEATURE_COMPRESSION; |
|
|
467 | #endif |
|
|
468 | #if ENABLE_ROHC |
|
|
469 | f |= FEATURE_ROHC; |
|
|
470 | #endif |
|
|
471 | return f; |
|
|
472 | } |
461 | }; |
473 | }; |
462 | |
|
|
463 | #define FEATURES ((ENABLE_COMPRESSION ? FEATURE_COMPRESSION : 0) \ |
|
|
464 | | (ENABLE_ROHC ? FEATURE_ROHC : 0)) |
|
|
465 | |
474 | |
466 | void config_packet::setup (ptype type, int dst) |
475 | void config_packet::setup (ptype type, int dst) |
467 | { |
476 | { |
468 | prot_major = PROTOCOL_MAJOR; |
477 | prot_major = PROTOCOL_MAJOR; |
469 | prot_minor = PROTOCOL_MINOR; |
478 | prot_minor = PROTOCOL_MINOR; |
470 | randsize = RAND_SIZE; |
479 | randsize = RAND_SIZE; |
471 | hmaclen = HMACLENGTH; |
480 | hmaclen = HMACLENGTH; |
472 | flags = ENABLE_COMPRESSION ? 0x81 : 0x80; |
481 | flags = ENABLE_COMPRESSION ? 0x81 : 0x80; |
473 | challengelen = sizeof (rsachallenge); |
482 | challengelen = sizeof (rsachallenge); |
474 | features = FEATURES; |
483 | features = get_features (); |
475 | |
484 | |
476 | cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); |
485 | cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); |
477 | digest_nid = htonl (EVP_MD_type (RSA_HASH)); |
486 | digest_nid = htonl (EVP_MD_type (RSA_HASH)); |
478 | hmac_nid = htonl (EVP_MD_type (DIGEST)); |
487 | hmac_nid = htonl (EVP_MD_type (DIGEST)); |
479 | |
488 | |
… | |
… | |
580 | { |
589 | { |
581 | if (ictx && octx) |
590 | if (ictx && octx) |
582 | { |
591 | { |
583 | connectmode = conf->connectmode; |
592 | connectmode = conf->connectmode; |
584 | |
593 | |
|
|
594 | // make sure rekeying timeouts are slightly asymmetric |
585 | rekey.start (NOW + ::conf.rekey); |
595 | rekey.start (NOW + ::conf.rekey |
|
|
596 | + (conf->id > THISNODE->id ? 10 : 0)); |
586 | keepalive.start (NOW + ::conf.keepalive); |
597 | keepalive.start (NOW + ::conf.keepalive); |
587 | |
598 | |
588 | // send queued packets |
599 | // send queued packets |
589 | if (ictx && octx) |
600 | if (ictx && octx) |
590 | { |
601 | { |
… | |
… | |
733 | && connectmode != conf_node::C_DISABLED |
744 | && connectmode != conf_node::C_DISABLED |
734 | && NOW > w.at) |
745 | && NOW > w.at) |
735 | { |
746 | { |
736 | double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; |
747 | double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; |
737 | |
748 | |
738 | if (retry_int < 3600 * 8) |
749 | if (retry_int < conf->max_retry) |
739 | retry_cnt++; |
750 | retry_cnt++; |
|
|
751 | else |
|
|
752 | retry_int = conf->max_retry; |
740 | |
753 | |
741 | w.start (NOW + retry_int); |
754 | w.start (NOW + retry_int); |
742 | |
755 | |
743 | reset_si (); |
756 | reset_si (); |
744 | |
757 | |
… | |
… | |
772 | } |
785 | } |
773 | |
786 | |
774 | delete ictx; ictx = 0; |
787 | delete ictx; ictx = 0; |
775 | delete octx; octx = 0; |
788 | delete octx; octx = 0; |
776 | |
789 | |
777 | si.host= 0; |
790 | si.host = 0; |
778 | |
791 | |
779 | last_activity = 0; |
792 | last_activity = 0; |
780 | retry_cnt = 0; |
793 | retry_cnt = 0; |
781 | |
794 | |
782 | rekey.stop (); |
795 | rekey.stop (); |
… | |
… | |
923 | |
936 | |
924 | // compatibility code, remove when no longer required |
937 | // compatibility code, remove when no longer required |
925 | if (p->flags & 1) p->features |= FEATURE_COMPRESSION; |
938 | if (p->flags & 1) p->features |= FEATURE_COMPRESSION; |
926 | |
939 | |
927 | conf->protocols = p->protocols; |
940 | conf->protocols = p->protocols; |
928 | features = p->features & FEATURES; |
941 | features = p->features & config_packet::get_features (); |
929 | |
942 | |
930 | send_auth_response (rsi, p->id, k); |
943 | send_auth_response (rsi, p->id, k); |
931 | |
944 | |
932 | connection_established (); |
945 | connection_established (); |
933 | |
946 | |
… | |
… | |
1047 | si = rsi; |
1060 | si = rsi; |
1048 | |
1061 | |
1049 | slog (L_INFO, _("%s(%s): socket address changed to %s"), |
1062 | slog (L_INFO, _("%s(%s): socket address changed to %s"), |
1050 | conf->nodename, (const char *)si, (const char *)rsi); |
1063 | conf->nodename, (const char *)si, (const char *)rsi); |
1051 | } |
1064 | } |
1052 | |
|
|
1053 | delete d; |
|
|
1054 | |
|
|
1055 | break; |
|
|
1056 | } |
1065 | } |
|
|
1066 | |
|
|
1067 | delete d; |
|
|
1068 | break; |
1057 | } |
1069 | } |
1058 | } |
1070 | } |
1059 | |
1071 | |
1060 | send_reset (rsi); |
1072 | send_reset (rsi); |
1061 | break; |
1073 | break; |
… | |
… | |
1086 | break; |
1098 | break; |
1087 | |
1099 | |
1088 | case vpn_packet::PT_CONNECT_INFO: |
1100 | case vpn_packet::PT_CONNECT_INFO: |
1089 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1101 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1090 | { |
1102 | { |
1091 | connect_info_packet *p = (connect_info_packet *) pkt; |
1103 | connect_info_packet *p = (connect_info_packet *)pkt; |
1092 | |
1104 | |
1093 | assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything |
1105 | if (p->id > 0 && p->id <= vpn->conns.size ()) // hmac-auth does not mean we accept anything |
1094 | |
1106 | { |
1095 | connection *c = vpn->conns[p->id - 1]; |
1107 | connection *c = vpn->conns[p->id - 1]; |
1096 | |
1108 | |
1097 | c->conf->protocols = p->protocols; |
1109 | c->conf->protocols = p->protocols; |
1098 | protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); |
1110 | protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); |
1099 | p->si.upgrade_protocol (protocol, c->conf); |
1111 | p->si.upgrade_protocol (protocol, c->conf); |
1100 | |
1112 | |
1101 | slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", |
1113 | slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", |
1102 | conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); |
1114 | conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); |
1103 | |
1115 | |
1104 | const sockinfo &dsi = forward_si (p->si); |
1116 | const sockinfo &dsi = forward_si (p->si); |
1105 | |
1117 | |
1106 | if (dsi.valid ()) |
1118 | if (dsi.valid ()) |
1107 | c->send_auth_request (dsi, true); |
1119 | c->send_auth_request (dsi, true); |
|
|
1120 | } |
1108 | } |
1121 | } |
1109 | |
1122 | |
1110 | break; |
1123 | break; |
1111 | |
1124 | |
1112 | default: |
1125 | default: |