1 | /* |
1 | /* |
2 | connection.C -- manage a single connection |
2 | connection.C -- manage a single connection |
3 | Copyright (C) 2003-2004 Marc Lehmann <pcg@goof.com> |
3 | Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de> |
4 | |
4 | |
|
|
5 | This file is part of GVPE. |
|
|
6 | |
5 | This program is free software; you can redistribute it and/or modify |
7 | GVPE is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
8 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
9 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
10 | (at your option) any later version. |
9 | |
11 | |
10 | This program is distributed in the hope that it will be useful, |
12 | This program is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | GNU General Public License for more details. |
15 | GNU General Public License for more details. |
14 | |
16 | |
15 | You should have received a copy of the GNU General Public License |
17 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software |
18 | along with gvpe; if not, write to the Free Software |
17 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
18 | */ |
20 | */ |
19 | |
21 | |
20 | #include "config.h" |
22 | #include "config.h" |
21 | |
|
|
22 | extern "C" { |
|
|
23 | # include "lzf/lzf.h" |
|
|
24 | } |
|
|
25 | |
|
|
26 | #include <cassert> |
|
|
27 | |
23 | |
28 | #include <list> |
24 | #include <list> |
29 | |
25 | |
30 | #include <openssl/rand.h> |
26 | #include <openssl/rand.h> |
31 | #include <openssl/evp.h> |
27 | #include <openssl/evp.h> |
32 | #include <openssl/rsa.h> |
28 | #include <openssl/rsa.h> |
33 | #include <openssl/err.h> |
29 | #include <openssl/err.h> |
34 | |
|
|
35 | #include "gettext.h" |
|
|
36 | |
30 | |
37 | #include "conf.h" |
31 | #include "conf.h" |
38 | #include "slog.h" |
32 | #include "slog.h" |
39 | #include "device.h" |
33 | #include "device.h" |
40 | #include "vpn.h" |
34 | #include "vpn.h" |
… | |
… | |
45 | #if !HAVE_RAND_PSEUDO_BYTES |
39 | #if !HAVE_RAND_PSEUDO_BYTES |
46 | # define RAND_pseudo_bytes RAND_bytes |
40 | # define RAND_pseudo_bytes RAND_bytes |
47 | #endif |
41 | #endif |
48 | |
42 | |
49 | #define MAGIC "vped\xbd\xc6\xdb\x82" // 8 bytes of magic |
43 | #define MAGIC "vped\xbd\xc6\xdb\x82" // 8 bytes of magic |
|
|
44 | |
|
|
45 | #define ULTRA_FAST 1 |
|
|
46 | #define HLOG 15 |
|
|
47 | #include "lzf/lzf.h" |
|
|
48 | #include "lzf/lzf_c.c" |
|
|
49 | #include "lzf/lzf_d.c" |
50 | |
50 | |
51 | struct crypto_ctx |
51 | struct crypto_ctx |
52 | { |
52 | { |
53 | EVP_CIPHER_CTX cctx; |
53 | EVP_CIPHER_CTX cctx; |
54 | HMAC_CTX hctx; |
54 | HMAC_CTX hctx; |
… | |
… | |
90 | rsachallenge chg; |
90 | rsachallenge chg; |
91 | }; |
91 | }; |
92 | |
92 | |
93 | struct rsa_cache : list<rsa_entry> |
93 | struct rsa_cache : list<rsa_entry> |
94 | { |
94 | { |
95 | void cleaner_cb (time_watcher &w); time_watcher cleaner; |
95 | void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner; |
96 | |
96 | |
97 | bool find (const rsaid &id, rsachallenge &chg) |
97 | bool find (const rsaid &id, rsachallenge &chg) |
98 | { |
98 | { |
99 | for (iterator i = begin (); i != end (); ++i) |
99 | for (iterator i = begin (); i != end (); ++i) |
100 | { |
100 | { |
101 | if (!memcmp (&id, &i->id, sizeof id) && i->expire > NOW) |
101 | if (!memcmp (&id, &i->id, sizeof id) && i->expire > ev::ev_now ()) |
102 | { |
102 | { |
103 | memcpy (&chg, &i->chg, sizeof chg); |
103 | memcpy (&chg, &i->chg, sizeof chg); |
104 | |
104 | |
105 | erase (i); |
105 | erase (i); |
106 | return true; |
106 | return true; |
107 | } |
107 | } |
108 | } |
108 | } |
109 | |
109 | |
110 | if (cleaner.at < NOW) |
110 | if (!cleaner.is_active ()) |
111 | cleaner.start (NOW + RSA_TTL); |
111 | cleaner.again (); |
112 | |
112 | |
113 | return false; |
113 | return false; |
114 | } |
114 | } |
115 | |
115 | |
116 | void gen (rsaid &id, rsachallenge &chg) |
116 | void gen (rsaid &id, rsachallenge &chg) |
117 | { |
117 | { |
118 | rsa_entry e; |
118 | rsa_entry e; |
119 | |
119 | |
120 | RAND_bytes ((unsigned char *)&id, sizeof id); |
120 | RAND_bytes ((unsigned char *)&id, sizeof id); |
121 | RAND_bytes ((unsigned char *)&chg, sizeof chg); |
121 | RAND_bytes ((unsigned char *)&chg, sizeof chg); |
122 | |
122 | |
123 | e.expire = NOW + RSA_TTL; |
123 | e.expire = ev::ev_now () + RSA_TTL; |
124 | e.id = id; |
124 | e.id = id; |
125 | memcpy (&e.chg, &chg, sizeof chg); |
125 | memcpy (&e.chg, &chg, sizeof chg); |
126 | |
126 | |
127 | push_back (e); |
127 | push_back (e); |
128 | |
128 | |
129 | if (cleaner.at < NOW) |
129 | if (!cleaner.is_active ()) |
130 | cleaner.start (NOW + RSA_TTL); |
130 | cleaner.again (); |
131 | } |
131 | } |
132 | |
132 | |
133 | rsa_cache () |
133 | rsa_cache () |
134 | : cleaner (this, &rsa_cache::cleaner_cb) |
134 | : cleaner (this, &rsa_cache::cleaner_cb) |
135 | { } |
135 | { |
|
|
136 | cleaner.set (RSA_TTL, RSA_TTL); |
|
|
137 | } |
136 | |
138 | |
137 | } rsa_cache; |
139 | } rsa_cache; |
138 | |
140 | |
139 | void rsa_cache::cleaner_cb (time_watcher &w) |
141 | void rsa_cache::cleaner_cb (ev::timer &w, int revents) |
140 | { |
142 | { |
141 | if (!empty ()) |
143 | if (empty ()) |
|
|
144 | w.stop (); |
|
|
145 | else |
142 | { |
146 | { |
143 | w.start (NOW + RSA_TTL); |
|
|
144 | |
|
|
145 | for (iterator i = begin (); i != end (); ) |
147 | for (iterator i = begin (); i != end (); ) |
146 | if (i->expire <= NOW) |
148 | if (i->expire <= ev::ev_now ()) |
147 | i = erase (i); |
149 | i = erase (i); |
148 | else |
150 | else |
149 | ++i; |
151 | ++i; |
150 | } |
152 | } |
151 | } |
153 | } |
… | |
… | |
200 | // 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. |
201 | // this implementation ("splay list" ;) is inefficient, |
203 | // this implementation ("splay list" ;) is inefficient, |
202 | // but low on resources. |
204 | // but low on resources. |
203 | struct net_rate_limiter : list<net_rateinfo> |
205 | struct net_rate_limiter : list<net_rateinfo> |
204 | { |
206 | { |
205 | static const double ALPHA = 1. - 1. / 600.; // allow bursts |
207 | # define NRL_ALPHA (1. - 1. / 600.) // allow bursts |
206 | static const double CUTOFF = 10.; // one event every CUTOFF seconds |
208 | # define NRL_CUTOFF 10. // one event every CUTOFF seconds |
207 | static const double EXPIRE = CUTOFF * 30.; // expire entries after this time |
209 | # define NRL_EXPIRE (NRL_CUTOFF * 30.) // expire entries after this time |
208 | 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 |
209 | |
211 | |
210 | bool can (const sockinfo &si) { return can((u32)si.host); } |
212 | bool can (const sockinfo &si) { return can((u32)si.host); } |
211 | bool can (u32 host); |
213 | bool can (u32 host); |
212 | }; |
214 | }; |
213 | |
215 | |
… | |
… | |
218 | iterator i; |
220 | iterator i; |
219 | |
221 | |
220 | for (i = begin (); i != end (); ) |
222 | for (i = begin (); i != end (); ) |
221 | if (i->host == host) |
223 | if (i->host == host) |
222 | break; |
224 | break; |
223 | else if (i->last < NOW - EXPIRE) |
225 | else if (i->last < ev::ev_now () - NRL_EXPIRE) |
224 | i = erase (i); |
226 | i = erase (i); |
225 | else |
227 | else |
226 | i++; |
228 | i++; |
227 | |
229 | |
228 | if (i == end ()) |
230 | if (i == end ()) |
229 | { |
231 | { |
230 | net_rateinfo ri; |
232 | net_rateinfo ri; |
231 | |
233 | |
232 | ri.host = host; |
234 | ri.host = host; |
233 | ri.pcnt = 1.; |
235 | ri.pcnt = 1.; |
234 | ri.diff = MAXDIF; |
236 | ri.diff = NRL_MAXDIF; |
235 | ri.last = NOW; |
237 | ri.last = ev::ev_now (); |
236 | |
238 | |
237 | push_front (ri); |
239 | push_front (ri); |
238 | |
240 | |
239 | return true; |
241 | return true; |
240 | } |
242 | } |
241 | else |
243 | else |
242 | { |
244 | { |
243 | net_rateinfo ri (*i); |
245 | net_rateinfo ri (*i); |
244 | erase (i); |
246 | erase (i); |
245 | |
247 | |
246 | ri.pcnt = ri.pcnt * ALPHA; |
248 | ri.pcnt = ri.pcnt * NRL_ALPHA; |
247 | ri.diff = ri.diff * ALPHA + (NOW - ri.last); |
249 | ri.diff = ri.diff * NRL_ALPHA + (ev::ev_now () - ri.last); |
248 | |
250 | |
249 | ri.last = NOW; |
251 | ri.last = ev::ev_now (); |
250 | |
252 | |
251 | double dif = ri.diff / ri.pcnt; |
253 | double dif = ri.diff / ri.pcnt; |
252 | |
254 | |
253 | bool send = dif > CUTOFF; |
255 | bool send = dif > NRL_CUTOFF; |
254 | |
256 | |
255 | if (dif > MAXDIF) |
257 | if (dif > NRL_MAXDIF) |
256 | { |
258 | { |
257 | ri.pcnt = 1.; |
259 | ri.pcnt = 1.; |
258 | ri.diff = MAXDIF; |
260 | ri.diff = NRL_MAXDIF; |
259 | } |
261 | } |
260 | else if (send) |
262 | else if (send) |
261 | ri.pcnt++; |
263 | ri.pcnt++; |
262 | |
264 | |
263 | push_front (ri); |
265 | push_front (ri); |
… | |
… | |
333 | int outl = 0, outl2; |
335 | int outl = 0, outl2; |
334 | ptype type = PT_DATA_UNCOMPRESSED; |
336 | ptype type = PT_DATA_UNCOMPRESSED; |
335 | |
337 | |
336 | #if ENABLE_COMPRESSION |
338 | #if ENABLE_COMPRESSION |
337 | u8 cdata[MAX_MTU]; |
339 | u8 cdata[MAX_MTU]; |
338 | u32 cl; |
|
|
339 | |
340 | |
|
|
341 | if (conn->features & ENABLE_COMPRESSION) |
|
|
342 | { |
340 | cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); |
343 | u32 cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); |
|
|
344 | |
341 | if (cl) |
345 | if (cl) |
342 | { |
346 | { |
343 | type = PT_DATA_COMPRESSED; |
347 | type = PT_DATA_COMPRESSED; |
344 | d = cdata; |
348 | d = cdata; |
345 | l = cl + 2; |
349 | l = cl + 2; |
346 | |
350 | |
347 | d[0] = cl >> 8; |
351 | d[0] = cl >> 8; |
348 | d[1] = cl; |
352 | d[1] = cl; |
|
|
353 | } |
349 | } |
354 | } |
350 | #endif |
355 | #endif |
351 | |
356 | |
352 | require (EVP_EncryptInit_ex (cctx, 0, 0, 0, 0)); |
357 | require (EVP_EncryptInit_ex (cctx, 0, 0, 0, 0)); |
353 | |
358 | |
… | |
… | |
446 | { |
451 | { |
447 | // actually, hmaclen cannot be checked because the hmac |
452 | // actually, hmaclen cannot be checked because the hmac |
448 | // field comes before this data, so peers with other |
453 | // field comes before this data, so peers with other |
449 | // hmacs simply will not work. |
454 | // hmacs simply will not work. |
450 | u8 prot_major, prot_minor, randsize, hmaclen; |
455 | u8 prot_major, prot_minor, randsize, hmaclen; |
451 | u8 flags, challengelen, pad2, pad3; |
456 | u8 flags, challengelen, features, pad3; |
452 | u32 cipher_nid, digest_nid, hmac_nid; |
457 | u32 cipher_nid, digest_nid, hmac_nid; |
453 | |
|
|
454 | const u8 curflags () const |
|
|
455 | { |
|
|
456 | return 0x80 |
|
|
457 | | (ENABLE_COMPRESSION ? 0x01 : 0x00); |
|
|
458 | } |
|
|
459 | |
458 | |
460 | void setup (ptype type, int dst); |
459 | void setup (ptype type, int dst); |
461 | 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 | #if ENABLE_BRIDGING |
|
|
472 | f |= FEATURE_BRIDGING; |
|
|
473 | #endif |
|
|
474 | return f; |
|
|
475 | } |
462 | }; |
476 | }; |
463 | |
477 | |
464 | void config_packet::setup (ptype type, int dst) |
478 | void config_packet::setup (ptype type, int dst) |
465 | { |
479 | { |
466 | prot_major = PROTOCOL_MAJOR; |
480 | prot_major = PROTOCOL_MAJOR; |
467 | prot_minor = PROTOCOL_MINOR; |
481 | prot_minor = PROTOCOL_MINOR; |
468 | randsize = RAND_SIZE; |
482 | randsize = RAND_SIZE; |
469 | hmaclen = HMACLENGTH; |
483 | hmaclen = HMACLENGTH; |
470 | flags = curflags (); |
484 | flags = 0; |
471 | challengelen = sizeof (rsachallenge); |
485 | challengelen = sizeof (rsachallenge); |
|
|
486 | features = get_features (); |
472 | |
487 | |
473 | cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); |
488 | cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); |
474 | digest_nid = htonl (EVP_MD_type (RSA_HASH)); |
489 | digest_nid = htonl (EVP_MD_type (RSA_HASH)); |
475 | hmac_nid = htonl (EVP_MD_type (DIGEST)); |
490 | hmac_nid = htonl (EVP_MD_type (DIGEST)); |
476 | |
491 | |
… | |
… | |
484 | slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); |
499 | slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); |
485 | else if (randsize != RAND_SIZE) |
500 | else if (randsize != RAND_SIZE) |
486 | slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); |
501 | slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); |
487 | else if (hmaclen != HMACLENGTH) |
502 | else if (hmaclen != HMACLENGTH) |
488 | slog (L_WARN, _("hmac length mismatch (remote %d <=> local %d)"), hmaclen, HMACLENGTH); |
503 | slog (L_WARN, _("hmac length mismatch (remote %d <=> local %d)"), hmaclen, HMACLENGTH); |
489 | else if (flags != curflags ()) |
|
|
490 | slog (L_WARN, _("flag mismatch (remote %x <=> local %x)"), flags, curflags ()); |
|
|
491 | else if (challengelen != sizeof (rsachallenge)) |
504 | else if (challengelen != sizeof (rsachallenge)) |
492 | slog (L_WARN, _("challenge length mismatch (remote %d <=> local %d)"), challengelen, sizeof (rsachallenge)); |
505 | slog (L_WARN, _("challenge length mismatch (remote %d <=> local %d)"), challengelen, sizeof (rsachallenge)); |
493 | else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER))) |
506 | else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER))) |
494 | slog (L_WARN, _("cipher mismatch (remote %x <=> local %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER)); |
507 | slog (L_WARN, _("cipher mismatch (remote %x <=> local %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER)); |
495 | else if (digest_nid != htonl (EVP_MD_type (RSA_HASH))) |
508 | else if (digest_nid != htonl (EVP_MD_type (RSA_HASH))) |
… | |
… | |
575 | { |
588 | { |
576 | if (ictx && octx) |
589 | if (ictx && octx) |
577 | { |
590 | { |
578 | connectmode = conf->connectmode; |
591 | connectmode = conf->connectmode; |
579 | |
592 | |
580 | rekey.start (NOW + ::conf.rekey); |
593 | // make sure rekeying timeouts are slightly asymmetric |
|
|
594 | ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); |
|
|
595 | rekey.start (rekey_interval, rekey_interval); |
581 | keepalive.start (NOW + ::conf.keepalive); |
596 | keepalive.start (::conf.keepalive); |
582 | |
597 | |
583 | // send queued packets |
598 | // send queued packets |
584 | if (ictx && octx) |
599 | if (ictx && octx) |
585 | { |
600 | { |
586 | while (tap_packet *p = (tap_packet *)data_queue.get ()) |
601 | while (tap_packet *p = (tap_packet *)data_queue.get ()) |
… | |
… | |
597 | } |
612 | } |
598 | } |
613 | } |
599 | else |
614 | else |
600 | { |
615 | { |
601 | retry_cnt = 0; |
616 | retry_cnt = 0; |
602 | establish_connection.start (NOW + 5); |
617 | establish_connection.start (5); |
603 | keepalive.stop (); |
618 | keepalive.stop (); |
604 | rekey.stop (); |
619 | rekey.stop (); |
605 | } |
620 | } |
606 | } |
621 | } |
607 | |
622 | |
… | |
… | |
611 | protocol = best_protocol (THISNODE->protocols & conf->protocols); |
626 | protocol = best_protocol (THISNODE->protocols & conf->protocols); |
612 | |
627 | |
613 | // mask out protocols we cannot establish |
628 | // mask out protocols we cannot establish |
614 | if (!conf->udp_port) protocol &= ~PROT_UDPv4; |
629 | if (!conf->udp_port) protocol &= ~PROT_UDPv4; |
615 | if (!conf->tcp_port) protocol &= ~PROT_TCPv4; |
630 | if (!conf->tcp_port) protocol &= ~PROT_TCPv4; |
|
|
631 | if (!conf->dns_port) protocol &= ~PROT_DNSv4; |
|
|
632 | |
|
|
633 | if (protocol |
|
|
634 | && (!conf->can_direct (THISNODE) |
|
|
635 | || !THISNODE->can_direct (conf))) |
|
|
636 | { |
|
|
637 | slog (L_DEBUG, _("%s: direct connection denied"), conf->nodename); |
|
|
638 | protocol = 0; |
|
|
639 | } |
616 | |
640 | |
617 | si.set (conf, protocol); |
641 | si.set (conf, protocol); |
618 | } |
642 | } |
619 | |
643 | |
620 | // ensure sockinfo is valid, forward if necessary |
644 | // ensure sockinfo is valid, forward if necessary |
… | |
… | |
625 | { |
649 | { |
626 | connection *r = vpn->find_router (); |
650 | connection *r = vpn->find_router (); |
627 | |
651 | |
628 | if (r) |
652 | if (r) |
629 | { |
653 | { |
630 | slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s"), |
654 | slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s (%s)"), |
631 | conf->nodename, r->conf->nodename); |
655 | conf->nodename, r->conf->nodename, (const char *)r->si); |
632 | return r->si; |
656 | return r->si; |
633 | } |
657 | } |
634 | else |
658 | else |
635 | slog (L_DEBUG, _("%s: node unreachable, no common protocol"), |
659 | slog (L_DEBUG, _("%s: node unreachable, no common protocol"), |
636 | conf->nodename); |
660 | conf->nodename); |
… | |
… | |
706 | } |
730 | } |
707 | |
731 | |
708 | void |
732 | void |
709 | connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) |
733 | connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) |
710 | { |
734 | { |
711 | slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)\n", |
735 | slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)", |
712 | conf->id, rid, (const char *)rsi); |
736 | conf->id, rid, (const char *)rsi); |
713 | |
737 | |
714 | connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); |
738 | connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); |
715 | |
739 | |
716 | r->hmac_set (octx); |
740 | r->hmac_set (octx); |
… | |
… | |
718 | |
742 | |
719 | delete r; |
743 | delete r; |
720 | } |
744 | } |
721 | |
745 | |
722 | void |
746 | void |
723 | connection::establish_connection_cb (time_watcher &w) |
747 | connection::establish_connection_cb (ev::timer &w, int revents) |
724 | { |
748 | { |
725 | if (!ictx |
749 | if (!ictx |
726 | && conf != THISNODE |
750 | && conf != THISNODE |
727 | && connectmode != conf_node::C_NEVER |
751 | && connectmode != conf_node::C_NEVER |
728 | && connectmode != conf_node::C_DISABLED |
752 | && connectmode != conf_node::C_DISABLED |
729 | && NOW > w.at) |
753 | && !w.is_active ()) |
730 | { |
754 | { |
731 | double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; |
755 | ev::tstamp retry_int = ev::tstamp (retry_cnt & 3 |
732 | |
756 | ? (retry_cnt & 3) + 1 |
733 | if (retry_int < 3600 * 8) |
757 | : 1 << (retry_cnt >> 2)); |
734 | retry_cnt++; |
|
|
735 | |
|
|
736 | w.start (NOW + retry_int); |
|
|
737 | |
758 | |
738 | reset_si (); |
759 | reset_si (); |
739 | |
760 | |
|
|
761 | bool slow = si.prot & PROT_SLOW; |
|
|
762 | |
740 | if (si.prot && !si.host) |
763 | if (si.prot && !si.host) |
|
|
764 | { |
|
|
765 | /*TODO*/ /* start the timer so we don't recurse endlessly */ |
|
|
766 | w.start (1); |
741 | vpn->send_connect_request (conf->id); |
767 | vpn->send_connect_request (conf->id); |
|
|
768 | } |
742 | else |
769 | else |
743 | { |
770 | { |
744 | const sockinfo &dsi = forward_si (si); |
771 | const sockinfo &dsi = forward_si (si); |
|
|
772 | |
|
|
773 | slow = slow || (dsi.prot & PROT_SLOW); |
745 | |
774 | |
746 | if (dsi.valid () && auth_rate_limiter.can (dsi)) |
775 | if (dsi.valid () && auth_rate_limiter.can (dsi)) |
747 | { |
776 | { |
748 | if (retry_cnt < 4) |
777 | if (retry_cnt < 4) |
749 | send_auth_request (dsi, true); |
778 | send_auth_request (dsi, true); |
750 | else |
779 | else |
751 | send_ping (dsi, 0); |
780 | send_ping (dsi, 0); |
752 | } |
781 | } |
753 | } |
782 | } |
|
|
783 | |
|
|
784 | retry_int *= slow ? 8. : 0.7; |
|
|
785 | |
|
|
786 | if (retry_int < conf->max_retry) |
|
|
787 | retry_cnt++; |
|
|
788 | else |
|
|
789 | retry_int = conf->max_retry; |
|
|
790 | |
|
|
791 | w.start (retry_int); |
754 | } |
792 | } |
755 | } |
793 | } |
756 | |
794 | |
757 | void |
795 | void |
758 | connection::reset_connection () |
796 | connection::reset_connection () |
… | |
… | |
761 | { |
799 | { |
762 | slog (L_INFO, _("%s(%s): connection lost"), |
800 | slog (L_INFO, _("%s(%s): connection lost"), |
763 | conf->nodename, (const char *)si); |
801 | conf->nodename, (const char *)si); |
764 | |
802 | |
765 | if (::conf.script_node_down) |
803 | if (::conf.script_node_down) |
766 | run_script (run_script_cb (this, &connection::script_node_down), false); |
804 | if (!run_script (run_script_cb (this, &connection::script_node_down), false)) |
|
|
805 | slog (L_WARN, _("node-down command execution failed, continuing.")); |
767 | } |
806 | } |
768 | |
807 | |
769 | delete ictx; ictx = 0; |
808 | delete ictx; ictx = 0; |
770 | delete octx; octx = 0; |
809 | delete octx; octx = 0; |
|
|
810 | #if ENABLE_DNS |
|
|
811 | dnsv4_reset_connection (); |
|
|
812 | #endif |
771 | |
813 | |
772 | si.host= 0; |
814 | si.host = 0; |
773 | |
815 | |
774 | last_activity = 0; |
816 | last_activity = 0; |
775 | retry_cnt = 0; |
817 | retry_cnt = 0; |
776 | |
818 | |
777 | rekey.stop (); |
819 | rekey.stop (); |
… | |
… | |
787 | |
829 | |
788 | reset_connection (); |
830 | reset_connection (); |
789 | } |
831 | } |
790 | |
832 | |
791 | void |
833 | void |
792 | connection::rekey_cb (time_watcher &w) |
834 | connection::rekey_cb (ev::timer &w, int revents) |
793 | { |
835 | { |
794 | reset_connection (); |
836 | reset_connection (); |
795 | establish_connection (); |
837 | establish_connection (); |
796 | } |
838 | } |
797 | |
839 | |
… | |
… | |
819 | { |
861 | { |
820 | if (ictx && octx) |
862 | if (ictx && octx) |
821 | send_data_packet (pkt); |
863 | send_data_packet (pkt); |
822 | else |
864 | else |
823 | { |
865 | { |
824 | if (!broadcast)//DDDD |
866 | if (!broadcast) |
825 | data_queue.put (new tap_packet (*pkt)); |
867 | data_queue.put (new tap_packet (*pkt)); |
826 | |
868 | |
827 | establish_connection (); |
869 | establish_connection (); |
828 | } |
870 | } |
829 | } |
871 | } |
… | |
… | |
841 | } |
883 | } |
842 | |
884 | |
843 | void |
885 | void |
844 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
886 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
845 | { |
887 | { |
846 | last_activity = NOW; |
888 | last_activity = ev::ev_now (); |
847 | |
889 | |
848 | slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
890 | slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
849 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
891 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
850 | |
892 | |
851 | switch (pkt->typ ()) |
893 | switch (pkt->typ ()) |
… | |
… | |
915 | |
957 | |
916 | octx = new crypto_ctx (k, 1); |
958 | octx = new crypto_ctx (k, 1); |
917 | oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; |
959 | oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; |
918 | |
960 | |
919 | conf->protocols = p->protocols; |
961 | conf->protocols = p->protocols; |
|
|
962 | features = p->features & config_packet::get_features (); |
920 | |
963 | |
921 | send_auth_response (rsi, p->id, k); |
964 | send_auth_response (rsi, p->id, k); |
922 | |
965 | |
923 | connection_established (); |
966 | connection_established (); |
924 | |
967 | |
… | |
… | |
988 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
1031 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
989 | conf->nodename, (const char *)rsi, |
1032 | conf->nodename, (const char *)rsi, |
990 | p->prot_major, p->prot_minor); |
1033 | p->prot_major, p->prot_minor); |
991 | |
1034 | |
992 | if (::conf.script_node_up) |
1035 | if (::conf.script_node_up) |
993 | run_script (run_script_cb (this, &connection::script_node_up), false); |
1036 | if (!run_script (run_script_cb (this, &connection::script_node_up), false)) |
|
|
1037 | slog (L_WARN, _("node-up command execution failed, continuing.")); |
994 | |
1038 | |
995 | break; |
1039 | break; |
996 | } |
1040 | } |
997 | else |
1041 | else |
998 | slog (L_ERR, _("%s(%s): sent and received challenge do not match"), |
1042 | slog (L_ERR, _("%s(%s): sent and received challenge do not match"), |
… | |
… | |
1032 | { |
1076 | { |
1033 | vpn->tap->send (d); |
1077 | vpn->tap->send (d); |
1034 | |
1078 | |
1035 | if (si != rsi) |
1079 | if (si != rsi) |
1036 | { |
1080 | { |
1037 | // fast re-sync on connection changes, useful especially for tcp/ip |
1081 | // fast re-sync on source address changes, useful especially for tcp/ip |
1038 | si = rsi; |
1082 | si = rsi; |
1039 | |
1083 | |
1040 | slog (L_INFO, _("%s(%s): socket address changed to %s"), |
1084 | slog (L_INFO, _("%s(%s): socket address changed to %s"), |
1041 | conf->nodename, (const char *)si, (const char *)rsi); |
1085 | conf->nodename, (const char *)si, (const char *)rsi); |
1042 | } |
1086 | } |
1043 | |
|
|
1044 | delete d; |
|
|
1045 | |
|
|
1046 | break; |
|
|
1047 | } |
1087 | } |
|
|
1088 | |
|
|
1089 | delete d; |
|
|
1090 | break; |
1048 | } |
1091 | } |
1049 | } |
1092 | } |
1050 | |
1093 | |
1051 | send_reset (rsi); |
1094 | send_reset (rsi); |
1052 | break; |
1095 | break; |
… | |
… | |
1054 | case vpn_packet::PT_CONNECT_REQ: |
1097 | case vpn_packet::PT_CONNECT_REQ: |
1055 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1098 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1056 | { |
1099 | { |
1057 | connect_req_packet *p = (connect_req_packet *) pkt; |
1100 | connect_req_packet *p = (connect_req_packet *) pkt; |
1058 | |
1101 | |
1059 | assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything |
1102 | if (p->id > 0 && p->id <= vpn->conns.size ()) |
1060 | connection *c = vpn->conns[p->id - 1]; |
|
|
1061 | conf->protocols = p->protocols; |
|
|
1062 | |
|
|
1063 | slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]\n", |
|
|
1064 | conf->id, p->id, c->ictx && c->octx); |
|
|
1065 | |
|
|
1066 | if (c->ictx && c->octx) |
|
|
1067 | { |
1103 | { |
|
|
1104 | connection *c = vpn->conns[p->id - 1]; |
|
|
1105 | conf->protocols = p->protocols; |
|
|
1106 | |
|
|
1107 | slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]", |
|
|
1108 | conf->id, p->id, c->ictx && c->octx); |
|
|
1109 | |
|
|
1110 | if (c->ictx && c->octx) |
|
|
1111 | { |
1068 | // send connect_info packets to both sides, in case one is |
1112 | // send connect_info packets to both sides, in case one is |
1069 | // behind a nat firewall (or both ;) |
1113 | // behind a nat firewall (or both ;) |
1070 | c->send_connect_info (conf->id, si, conf->protocols); |
1114 | c->send_connect_info (conf->id, si, conf->protocols); |
1071 | send_connect_info (c->conf->id, c->si, c->conf->protocols); |
1115 | send_connect_info (c->conf->id, c->si, c->conf->protocols); |
|
|
1116 | } |
|
|
1117 | else |
|
|
1118 | c->establish_connection (); |
1072 | } |
1119 | } |
1073 | else |
1120 | else |
1074 | c->establish_connection (); |
1121 | slog (L_WARN, |
|
|
1122 | _("received authenticated connection request from unknown node #%d, config file mismatch?"), |
|
|
1123 | p->id); |
1075 | } |
1124 | } |
1076 | |
1125 | |
1077 | break; |
1126 | break; |
1078 | |
1127 | |
1079 | case vpn_packet::PT_CONNECT_INFO: |
1128 | case vpn_packet::PT_CONNECT_INFO: |
1080 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1129 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1081 | { |
1130 | { |
1082 | connect_info_packet *p = (connect_info_packet *) pkt; |
1131 | connect_info_packet *p = (connect_info_packet *)pkt; |
1083 | |
1132 | |
1084 | assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything |
1133 | if (p->id > 0 && p->id <= vpn->conns.size ()) |
1085 | |
1134 | { |
1086 | connection *c = vpn->conns[p->id - 1]; |
1135 | connection *c = vpn->conns[p->id - 1]; |
1087 | |
1136 | |
1088 | c->conf->protocols = p->protocols; |
1137 | c->conf->protocols = p->protocols; |
1089 | protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); |
1138 | protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); |
1090 | p->si.upgrade_protocol (protocol, c->conf); |
1139 | p->si.upgrade_protocol (protocol, c->conf); |
1091 | |
1140 | |
1092 | slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", |
1141 | slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", |
1093 | conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); |
1142 | conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); |
1094 | |
1143 | |
1095 | const sockinfo &dsi = forward_si (p->si); |
1144 | const sockinfo &dsi = forward_si (p->si); |
1096 | |
1145 | |
1097 | if (dsi.valid ()) |
1146 | if (dsi.valid ()) |
1098 | c->send_auth_request (dsi, true); |
1147 | c->send_auth_request (dsi, true); |
|
|
1148 | } |
|
|
1149 | else |
|
|
1150 | slog (L_WARN, |
|
|
1151 | _("received authenticated connection request from unknown node #%d, config file mismatch?"), |
|
|
1152 | p->id); |
1099 | } |
1153 | } |
1100 | |
1154 | |
1101 | break; |
1155 | break; |
1102 | |
1156 | |
1103 | default: |
1157 | default: |
1104 | send_reset (rsi); |
1158 | send_reset (rsi); |
1105 | break; |
1159 | break; |
1106 | } |
1160 | } |
1107 | } |
1161 | } |
1108 | |
1162 | |
1109 | void connection::keepalive_cb (time_watcher &w) |
1163 | void connection::keepalive_cb (ev::timer &w, int revents) |
1110 | { |
1164 | { |
1111 | if (NOW >= last_activity + ::conf.keepalive + 30) |
1165 | if (ev::ev_now () >= last_activity + ::conf.keepalive + 30) |
1112 | { |
1166 | { |
1113 | reset_connection (); |
1167 | reset_connection (); |
1114 | establish_connection (); |
1168 | establish_connection (); |
1115 | } |
1169 | } |
1116 | else if (NOW < last_activity + ::conf.keepalive) |
1170 | else if (ev::ev_now () < last_activity + ::conf.keepalive) |
1117 | w.start (last_activity + ::conf.keepalive); |
1171 | w.start (last_activity + ::conf.keepalive - ev::now ()); |
1118 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1172 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1119 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1173 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1120 | { |
1174 | { |
1121 | send_ping (si); |
1175 | send_ping (si); |
1122 | w.start (NOW + 5); |
1176 | w.start (5); |
1123 | } |
1177 | } |
1124 | else if (NOW < last_activity + ::conf.keepalive + 10) |
1178 | else if (ev::ev_now () < last_activity + ::conf.keepalive + 10) |
1125 | // hold ondemand connections implicitly a few seconds longer |
1179 | // hold ondemand connections implicitly a few seconds longer |
1126 | // should delete octx, though, or something like that ;) |
1180 | // should delete octx, though, or something like that ;) |
1127 | w.start (last_activity + ::conf.keepalive + 10); |
1181 | w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); |
1128 | else |
1182 | else |
1129 | reset_connection (); |
1183 | reset_connection (); |
1130 | } |
1184 | } |
1131 | |
1185 | |
1132 | void connection::send_connect_request (int id) |
1186 | void connection::send_connect_request (int id) |
… | |
… | |
1138 | send_vpn_packet (p, si); |
1192 | send_vpn_packet (p, si); |
1139 | |
1193 | |
1140 | delete p; |
1194 | delete p; |
1141 | } |
1195 | } |
1142 | |
1196 | |
|
|
1197 | void connection::script_init_env (const char *ext) |
|
|
1198 | { |
|
|
1199 | char *env; |
|
|
1200 | asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); |
|
|
1201 | asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); |
|
|
1202 | asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, |
|
|
1203 | 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, |
|
|
1204 | conf->id & 0xff); putenv (env); |
|
|
1205 | } |
|
|
1206 | |
1143 | void connection::script_node () |
1207 | void connection::script_init_connect_env () |
1144 | { |
1208 | { |
1145 | vpn->script_if_up (); |
1209 | vpn->script_init_env (); |
1146 | |
1210 | |
1147 | char *env; |
1211 | char *env; |
1148 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
1212 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
1149 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1213 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1150 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1214 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1151 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1215 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1152 | } |
1216 | } |
1153 | |
1217 | |
1154 | const char *connection::script_node_up () |
1218 | const char *connection::script_node_up () |
1155 | { |
1219 | { |
1156 | script_node (); |
1220 | script_init_connect_env (); |
1157 | |
1221 | |
1158 | putenv ("STATE=up"); |
1222 | putenv ((char *)"STATE=up"); |
1159 | |
1223 | |
|
|
1224 | char *filename; |
|
|
1225 | asprintf (&filename, |
|
|
1226 | "%s/%s", |
|
|
1227 | confbase, |
1160 | return ::conf.script_node_up ? ::conf.script_node_up : "node-up"; |
1228 | ::conf.script_node_up ? ::conf.script_node_up : "node-up"); |
|
|
1229 | |
|
|
1230 | return filename; |
1161 | } |
1231 | } |
1162 | |
1232 | |
1163 | const char *connection::script_node_down () |
1233 | const char *connection::script_node_down () |
1164 | { |
1234 | { |
1165 | script_node (); |
1235 | script_init_connect_env (); |
1166 | |
1236 | |
1167 | putenv ("STATE=down"); |
1237 | putenv ((char *)"STATE=down"); |
1168 | |
1238 | |
1169 | return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; |
1239 | char *filename; |
1170 | } |
1240 | asprintf (&filename, |
|
|
1241 | "%s/%s", |
|
|
1242 | confbase, |
|
|
1243 | ::conf.script_node_down ? ::conf.script_node_down : "node-down"); |
1171 | |
1244 | |
|
|
1245 | return filename; |
|
|
1246 | } |
|
|
1247 | |
1172 | connection::connection(struct vpn *vpn_) |
1248 | connection::connection (struct vpn *vpn, conf_node *conf) |
1173 | : vpn(vpn_) |
1249 | : vpn(vpn), conf(conf) |
1174 | , rekey (this, &connection::rekey_cb) |
1250 | , rekey (this, &connection::rekey_cb) |
1175 | , keepalive (this, &connection::keepalive_cb) |
1251 | , keepalive (this, &connection::keepalive_cb) |
1176 | , establish_connection (this, &connection::establish_connection_cb) |
1252 | , establish_connection (this, &connection::establish_connection_cb) |
|
|
1253 | #if ENABLE_DNS |
|
|
1254 | , dns (0) |
|
|
1255 | #endif |
1177 | { |
1256 | { |
1178 | octx = ictx = 0; |
1257 | octx = ictx = 0; |
1179 | retry_cnt = 0; |
1258 | retry_cnt = 0; |
1180 | |
1259 | |
|
|
1260 | if (!conf->protocols) // make sure some protocol is enabled |
|
|
1261 | conf->protocols = PROT_UDPv4; |
|
|
1262 | |
1181 | connectmode = conf_node::C_ALWAYS; // initial setting |
1263 | connectmode = conf_node::C_ALWAYS; // initial setting |
1182 | reset_connection (); |
1264 | reset_connection (); |
1183 | } |
1265 | } |
1184 | |
1266 | |
1185 | connection::~connection () |
1267 | connection::~connection () |