1 |
/* |
2 |
connection.C -- manage a single connection |
3 |
Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> |
4 |
|
5 |
This file is part of GVPE. |
6 |
|
7 |
GVPE is free software; you can redistribute it and/or modify it |
8 |
under the terms of the GNU General Public License as published by the |
9 |
Free Software Foundation; either version 3 of the License, or (at your |
10 |
option) any later version. |
11 |
|
12 |
This program is distributed in the hope that it will be useful, but |
13 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
15 |
Public License for more details. |
16 |
|
17 |
You should have received a copy of the GNU General Public License along |
18 |
with this program; if not, see <http://www.gnu.org/licenses/>. |
19 |
|
20 |
Additional permission under GNU GPL version 3 section 7 |
21 |
|
22 |
If you modify this Program, or any covered work, by linking or |
23 |
combining it with the OpenSSL project's OpenSSL library (or a modified |
24 |
version of that library), containing parts covered by the terms of the |
25 |
OpenSSL or SSLeay licenses, the licensors of this Program grant you |
26 |
additional permission to convey the resulting work. Corresponding |
27 |
Source for a non-source form of such a combination shall include the |
28 |
source code for the parts of OpenSSL used as well as that of the |
29 |
covered work. |
30 |
*/ |
31 |
|
32 |
#include "config.h" |
33 |
|
34 |
#include <list> |
35 |
|
36 |
#include <openssl/rand.h> |
37 |
#include <openssl/evp.h> |
38 |
#include <openssl/rsa.h> |
39 |
#include <openssl/err.h> |
40 |
|
41 |
#include "conf.h" |
42 |
#include "slog.h" |
43 |
#include "device.h" |
44 |
#include "vpn.h" |
45 |
#include "connection.h" |
46 |
|
47 |
#include "netcompat.h" |
48 |
|
49 |
#if !HAVE_RAND_PSEUDO_BYTES |
50 |
# define RAND_pseudo_bytes RAND_bytes |
51 |
#endif |
52 |
|
53 |
#define MAGIC "vped\xbd\xc6\xdb\x82" // 8 bytes of magic |
54 |
|
55 |
#define ULTRA_FAST 1 |
56 |
#define HLOG 15 |
57 |
#include "lzf/lzf.h" |
58 |
#include "lzf/lzf_c.c" |
59 |
#include "lzf/lzf_d.c" |
60 |
|
61 |
struct crypto_ctx |
62 |
{ |
63 |
EVP_CIPHER_CTX cctx; |
64 |
HMAC_CTX hctx; |
65 |
|
66 |
crypto_ctx (const rsachallenge &challenge, int enc); |
67 |
~crypto_ctx (); |
68 |
}; |
69 |
|
70 |
crypto_ctx::crypto_ctx (const rsachallenge &challenge, int enc) |
71 |
{ |
72 |
EVP_CIPHER_CTX_init (&cctx); |
73 |
require (EVP_CipherInit_ex (&cctx, CIPHER, 0, &challenge[CHG_CIPHER_KEY], 0, enc)); |
74 |
HMAC_CTX_init (&hctx); |
75 |
HMAC_Init_ex (&hctx, &challenge[CHG_HMAC_KEY], HMAC_KEYLEN, DIGEST, 0); |
76 |
} |
77 |
|
78 |
crypto_ctx::~crypto_ctx () |
79 |
{ |
80 |
require (EVP_CIPHER_CTX_cleanup (&cctx)); |
81 |
HMAC_CTX_cleanup (&hctx); |
82 |
} |
83 |
|
84 |
static void |
85 |
rsa_hash (const rsaid &id, const rsachallenge &chg, rsaresponse &h) |
86 |
{ |
87 |
EVP_MD_CTX ctx; |
88 |
|
89 |
EVP_MD_CTX_init (&ctx); |
90 |
require (EVP_DigestInit (&ctx, RSA_HASH)); |
91 |
require (EVP_DigestUpdate(&ctx, &chg, sizeof chg)); |
92 |
require (EVP_DigestUpdate(&ctx, &id, sizeof id)); |
93 |
require (EVP_DigestFinal (&ctx, (unsigned char *)&h, 0)); |
94 |
EVP_MD_CTX_cleanup (&ctx); |
95 |
} |
96 |
|
97 |
struct rsa_entry |
98 |
{ |
99 |
tstamp expire; |
100 |
rsaid id; |
101 |
rsachallenge chg; |
102 |
}; |
103 |
|
104 |
struct rsa_cache : list<rsa_entry> |
105 |
{ |
106 |
inline void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner; |
107 |
|
108 |
bool find (const rsaid &id, rsachallenge &chg) |
109 |
{ |
110 |
for (iterator i = begin (); i != end (); ++i) |
111 |
{ |
112 |
if (!memcmp (&id, &i->id, sizeof id) && i->expire > ev_now ()) |
113 |
{ |
114 |
memcpy (&chg, &i->chg, sizeof chg); |
115 |
|
116 |
erase (i); |
117 |
return true; |
118 |
} |
119 |
} |
120 |
|
121 |
if (!cleaner.is_active ()) |
122 |
cleaner.again (); |
123 |
|
124 |
return false; |
125 |
} |
126 |
|
127 |
void gen (rsaid &id, rsachallenge &chg) |
128 |
{ |
129 |
rsa_entry e; |
130 |
|
131 |
RAND_bytes ((unsigned char *)&id, sizeof id); |
132 |
RAND_bytes ((unsigned char *)&chg, sizeof chg); |
133 |
|
134 |
e.expire = ev_now () + RSA_TTL; |
135 |
e.id = id; |
136 |
memcpy (&e.chg, &chg, sizeof chg); |
137 |
|
138 |
push_back (e); |
139 |
|
140 |
if (!cleaner.is_active ()) |
141 |
cleaner.again (); |
142 |
} |
143 |
|
144 |
rsa_cache () |
145 |
{ |
146 |
cleaner.set<rsa_cache, &rsa_cache::cleaner_cb> (this); |
147 |
cleaner.set (RSA_TTL, RSA_TTL); |
148 |
} |
149 |
|
150 |
} rsa_cache; |
151 |
|
152 |
void rsa_cache::cleaner_cb (ev::timer &w, int revents) |
153 |
{ |
154 |
if (empty ()) |
155 |
w.stop (); |
156 |
else |
157 |
{ |
158 |
for (iterator i = begin (); i != end (); ) |
159 |
if (i->expire <= ev_now ()) |
160 |
i = erase (i); |
161 |
else |
162 |
++i; |
163 |
} |
164 |
} |
165 |
|
166 |
////////////////////////////////////////////////////////////////////////////// |
167 |
|
168 |
pkt_queue::pkt_queue (double max_ttl, int max_queue) |
169 |
: max_ttl (max_ttl), max_queue (max_queue) |
170 |
{ |
171 |
queue = new pkt [max_queue]; |
172 |
|
173 |
i = 0; |
174 |
j = 0; |
175 |
|
176 |
expire.set<pkt_queue, &pkt_queue::expire_cb> (this); |
177 |
} |
178 |
|
179 |
pkt_queue::~pkt_queue () |
180 |
{ |
181 |
while (net_packet *p = get ()) |
182 |
delete p; |
183 |
|
184 |
delete [] queue; |
185 |
} |
186 |
|
187 |
void pkt_queue::expire_cb (ev::timer &w, int revents) |
188 |
{ |
189 |
ev_tstamp expire = ev_now () - max_ttl; |
190 |
|
191 |
for (;;) |
192 |
{ |
193 |
if (empty ()) |
194 |
break; |
195 |
|
196 |
double diff = queue[j].tstamp - expire; |
197 |
|
198 |
if (diff >= 0.) |
199 |
{ |
200 |
w.start (diff > 0.5 ? diff : 0.5); |
201 |
break; |
202 |
} |
203 |
|
204 |
delete get (); |
205 |
} |
206 |
} |
207 |
|
208 |
void pkt_queue::put (net_packet *p) |
209 |
{ |
210 |
ev_tstamp now = ev_now (); |
211 |
|
212 |
// start expiry timer |
213 |
if (empty ()) |
214 |
expire.start (max_ttl); |
215 |
|
216 |
int ni = i + 1 == max_queue ? 0 : i + 1; |
217 |
|
218 |
if (ni == j) |
219 |
delete get (); |
220 |
|
221 |
queue[i].pkt = p; |
222 |
queue[i].tstamp = now; |
223 |
|
224 |
i = ni; |
225 |
} |
226 |
|
227 |
net_packet *pkt_queue::get () |
228 |
{ |
229 |
if (empty ()) |
230 |
return 0; |
231 |
|
232 |
net_packet *p = queue[j].pkt; |
233 |
queue[j].pkt = 0; |
234 |
|
235 |
j = j + 1 == max_queue ? 0 : j + 1; |
236 |
|
237 |
return p; |
238 |
} |
239 |
|
240 |
struct net_rateinfo |
241 |
{ |
242 |
u32 host; |
243 |
double pcnt, diff; |
244 |
tstamp last; |
245 |
}; |
246 |
|
247 |
// only do action once every x seconds per host whole allowing bursts. |
248 |
// this implementation ("splay list" ;) is inefficient, |
249 |
// but low on resources. |
250 |
struct net_rate_limiter : list<net_rateinfo> |
251 |
{ |
252 |
# define NRL_ALPHA (1. - 1. / 600.) // allow bursts |
253 |
# define NRL_CUTOFF 10. // one event every CUTOFF seconds |
254 |
# define NRL_EXPIRE (NRL_CUTOFF * 30.) // expire entries after this time |
255 |
# define NRL_MAXDIF (NRL_CUTOFF * (1. / (1. - NRL_ALPHA))) // maximum diff /count value |
256 |
|
257 |
bool can (const sockinfo &si) { return can((u32)si.host); } |
258 |
bool can (u32 host); |
259 |
}; |
260 |
|
261 |
net_rate_limiter auth_rate_limiter, reset_rate_limiter; |
262 |
|
263 |
bool net_rate_limiter::can (u32 host) |
264 |
{ |
265 |
iterator i; |
266 |
|
267 |
for (i = begin (); i != end (); ) |
268 |
if (i->host == host) |
269 |
break; |
270 |
else if (i->last < ev_now () - NRL_EXPIRE) |
271 |
i = erase (i); |
272 |
else |
273 |
i++; |
274 |
|
275 |
if (i == end ()) |
276 |
{ |
277 |
net_rateinfo ri; |
278 |
|
279 |
ri.host = host; |
280 |
ri.pcnt = 1.; |
281 |
ri.diff = NRL_MAXDIF; |
282 |
ri.last = ev_now (); |
283 |
|
284 |
push_front (ri); |
285 |
|
286 |
return true; |
287 |
} |
288 |
else |
289 |
{ |
290 |
net_rateinfo ri (*i); |
291 |
erase (i); |
292 |
|
293 |
ri.pcnt = ri.pcnt * NRL_ALPHA; |
294 |
ri.diff = ri.diff * NRL_ALPHA + (ev_now () - ri.last); |
295 |
|
296 |
ri.last = ev_now (); |
297 |
|
298 |
double dif = ri.diff / ri.pcnt; |
299 |
|
300 |
bool send = dif > NRL_CUTOFF; |
301 |
|
302 |
if (dif > NRL_MAXDIF) |
303 |
{ |
304 |
ri.pcnt = 1.; |
305 |
ri.diff = NRL_MAXDIF; |
306 |
} |
307 |
else if (send) |
308 |
ri.pcnt++; |
309 |
|
310 |
push_front (ri); |
311 |
|
312 |
return send; |
313 |
} |
314 |
} |
315 |
|
316 |
///////////////////////////////////////////////////////////////////////////// |
317 |
|
318 |
unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE]; |
319 |
|
320 |
void hmac_packet::hmac_gen (crypto_ctx *ctx) |
321 |
{ |
322 |
unsigned int xlen; |
323 |
|
324 |
HMAC_CTX *hctx = &ctx->hctx; |
325 |
|
326 |
HMAC_Init_ex (hctx, 0, 0, 0, 0); |
327 |
HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), |
328 |
len - sizeof (hmac_packet)); |
329 |
HMAC_Final (hctx, (unsigned char *) &hmac_digest, &xlen); |
330 |
} |
331 |
|
332 |
void |
333 |
hmac_packet::hmac_set (crypto_ctx *ctx) |
334 |
{ |
335 |
hmac_gen (ctx); |
336 |
|
337 |
memcpy (hmac, hmac_digest, HMACLENGTH); |
338 |
} |
339 |
|
340 |
bool |
341 |
hmac_packet::hmac_chk (crypto_ctx *ctx) |
342 |
{ |
343 |
hmac_gen (ctx); |
344 |
|
345 |
return !memcmp (hmac, hmac_digest, HMACLENGTH); |
346 |
} |
347 |
|
348 |
void vpn_packet::set_hdr (ptype type_, unsigned int dst) |
349 |
{ |
350 |
type = type_; |
351 |
|
352 |
int src = THISNODE->id; |
353 |
|
354 |
src1 = src; |
355 |
srcdst = ((src >> 8) << 4) | (dst >> 8); |
356 |
dst1 = dst; |
357 |
} |
358 |
|
359 |
#define MAXVPNDATA (MAX_MTU - 6 - 6) |
360 |
#define DATAHDR (sizeof (u32) + RAND_SIZE) |
361 |
|
362 |
struct vpndata_packet : vpn_packet |
363 |
{ |
364 |
u8 data[MAXVPNDATA + DATAHDR]; // seqno |
365 |
|
366 |
void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno); |
367 |
tap_packet *unpack (connection *conn, u32 &seqno); |
368 |
private: |
369 |
|
370 |
const u32 data_hdr_size () const |
371 |
{ |
372 |
return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR; |
373 |
} |
374 |
}; |
375 |
|
376 |
void |
377 |
vpndata_packet::setup (connection *conn, int dst, u8 *d, u32 l, u32 seqno) |
378 |
{ |
379 |
EVP_CIPHER_CTX *cctx = &conn->octx->cctx; |
380 |
int outl = 0, outl2; |
381 |
ptype type = PT_DATA_UNCOMPRESSED; |
382 |
|
383 |
#if ENABLE_COMPRESSION |
384 |
u8 cdata[MAX_MTU]; |
385 |
|
386 |
if (conn->features & ENABLE_COMPRESSION) |
387 |
{ |
388 |
u32 cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); |
389 |
|
390 |
if (cl) |
391 |
{ |
392 |
type = PT_DATA_COMPRESSED; |
393 |
d = cdata; |
394 |
l = cl + 2; |
395 |
|
396 |
d[0] = cl >> 8; |
397 |
d[1] = cl; |
398 |
} |
399 |
} |
400 |
#endif |
401 |
|
402 |
require (EVP_EncryptInit_ex (cctx, 0, 0, 0, 0)); |
403 |
|
404 |
struct { |
405 |
#if RAND_SIZE |
406 |
u8 rnd[RAND_SIZE]; |
407 |
#endif |
408 |
u32 seqno; |
409 |
} datahdr; |
410 |
|
411 |
datahdr.seqno = ntohl (seqno); |
412 |
#if RAND_SIZE |
413 |
RAND_pseudo_bytes ((unsigned char *) datahdr.rnd, RAND_SIZE); |
414 |
#endif |
415 |
|
416 |
require (EVP_EncryptUpdate (cctx, |
417 |
(unsigned char *) data + outl, &outl2, |
418 |
(unsigned char *) &datahdr, DATAHDR)); |
419 |
outl += outl2; |
420 |
|
421 |
require (EVP_EncryptUpdate (cctx, |
422 |
(unsigned char *) data + outl, &outl2, |
423 |
(unsigned char *) d, l)); |
424 |
outl += outl2; |
425 |
|
426 |
require (EVP_EncryptFinal_ex (cctx, (unsigned char *) data + outl, &outl2)); |
427 |
outl += outl2; |
428 |
|
429 |
len = outl + data_hdr_size (); |
430 |
|
431 |
set_hdr (type, dst); |
432 |
|
433 |
hmac_set (conn->octx); |
434 |
} |
435 |
|
436 |
tap_packet * |
437 |
vpndata_packet::unpack (connection *conn, u32 &seqno) |
438 |
{ |
439 |
EVP_CIPHER_CTX *cctx = &conn->ictx->cctx; |
440 |
int outl = 0, outl2; |
441 |
tap_packet *p = new tap_packet; |
442 |
u8 *d; |
443 |
u32 l = len - data_hdr_size (); |
444 |
|
445 |
require (EVP_DecryptInit_ex (cctx, 0, 0, 0, 0)); |
446 |
|
447 |
#if ENABLE_COMPRESSION |
448 |
u8 cdata[MAX_MTU]; |
449 |
|
450 |
if (type == PT_DATA_COMPRESSED) |
451 |
d = cdata; |
452 |
else |
453 |
#endif |
454 |
d = &(*p)[6 + 6 - DATAHDR]; |
455 |
|
456 |
/* this overwrites part of the src mac, but we fix that later */ |
457 |
require (EVP_DecryptUpdate (cctx, |
458 |
d, &outl2, |
459 |
(unsigned char *)&data, len - data_hdr_size ())); |
460 |
outl += outl2; |
461 |
|
462 |
require (EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2)); |
463 |
outl += outl2; |
464 |
|
465 |
seqno = ntohl (*(u32 *)(d + RAND_SIZE)); |
466 |
|
467 |
id2mac (dst () ? dst() : THISNODE->id, p->dst); |
468 |
id2mac (src (), p->src); |
469 |
|
470 |
#if ENABLE_COMPRESSION |
471 |
if (type == PT_DATA_COMPRESSED) |
472 |
{ |
473 |
u32 cl = (d[DATAHDR] << 8) | d[DATAHDR + 1]; |
474 |
|
475 |
p->len = lzf_decompress (d + DATAHDR + 2, cl < MAX_MTU ? cl : 0, |
476 |
&(*p)[6 + 6], MAX_MTU) |
477 |
+ 6 + 6; |
478 |
} |
479 |
else |
480 |
p->len = outl + (6 + 6 - DATAHDR); |
481 |
#endif |
482 |
|
483 |
return p; |
484 |
} |
485 |
|
486 |
struct ping_packet : vpn_packet |
487 |
{ |
488 |
void setup (int dst, ptype type) |
489 |
{ |
490 |
set_hdr (type, dst); |
491 |
len = sizeof (*this) - sizeof (net_packet); |
492 |
} |
493 |
}; |
494 |
|
495 |
struct config_packet : vpn_packet |
496 |
{ |
497 |
// actually, hmaclen cannot be checked because the hmac |
498 |
// field comes before this data, so peers with other |
499 |
// hmacs simply will not work. |
500 |
u8 prot_major, prot_minor, randsize, hmaclen; |
501 |
u8 flags, challengelen, features, pad3; |
502 |
u32 cipher_nid, digest_nid, hmac_nid; |
503 |
|
504 |
void setup (ptype type, int dst); |
505 |
bool chk_config () const; |
506 |
|
507 |
static u8 get_features () |
508 |
{ |
509 |
u8 f = 0; |
510 |
#if ENABLE_COMPRESSION |
511 |
f |= FEATURE_COMPRESSION; |
512 |
#endif |
513 |
#if ENABLE_ROHC |
514 |
f |= FEATURE_ROHC; |
515 |
#endif |
516 |
#if ENABLE_BRIDGING |
517 |
f |= FEATURE_BRIDGING; |
518 |
#endif |
519 |
return f; |
520 |
} |
521 |
}; |
522 |
|
523 |
void config_packet::setup (ptype type, int dst) |
524 |
{ |
525 |
prot_major = PROTOCOL_MAJOR; |
526 |
prot_minor = PROTOCOL_MINOR; |
527 |
randsize = RAND_SIZE; |
528 |
hmaclen = HMACLENGTH; |
529 |
flags = 0; |
530 |
challengelen = sizeof (rsachallenge); |
531 |
features = get_features (); |
532 |
|
533 |
cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); |
534 |
digest_nid = htonl (EVP_MD_type (RSA_HASH)); |
535 |
hmac_nid = htonl (EVP_MD_type (DIGEST)); |
536 |
|
537 |
len = sizeof (*this) - sizeof (net_packet); |
538 |
set_hdr (type, dst); |
539 |
} |
540 |
|
541 |
bool config_packet::chk_config () const |
542 |
{ |
543 |
if (prot_major != PROTOCOL_MAJOR) |
544 |
slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); |
545 |
else if (randsize != RAND_SIZE) |
546 |
slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); |
547 |
else if (hmaclen != HMACLENGTH) |
548 |
slog (L_WARN, _("hmac length mismatch (remote %d <=> local %d)"), hmaclen, HMACLENGTH); |
549 |
else if (challengelen != sizeof (rsachallenge)) |
550 |
slog (L_WARN, _("challenge length mismatch (remote %d <=> local %d)"), challengelen, sizeof (rsachallenge)); |
551 |
else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER))) |
552 |
slog (L_WARN, _("cipher mismatch (remote %x <=> local %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER)); |
553 |
else if (digest_nid != htonl (EVP_MD_type (RSA_HASH))) |
554 |
slog (L_WARN, _("digest mismatch (remote %x <=> local %x)"), ntohl (digest_nid), EVP_MD_type (RSA_HASH)); |
555 |
else if (hmac_nid != htonl (EVP_MD_type (DIGEST))) |
556 |
slog (L_WARN, _("hmac mismatch (remote %x <=> local %x)"), ntohl (hmac_nid), EVP_MD_type (DIGEST)); |
557 |
else |
558 |
return true; |
559 |
|
560 |
return false; |
561 |
} |
562 |
|
563 |
struct auth_req_packet : config_packet |
564 |
{ |
565 |
char magic[8]; |
566 |
u8 initiate; // false if this is just an automatic reply |
567 |
u8 protocols; // supported protocols (will be patched on forward) |
568 |
u8 pad2, pad3; |
569 |
rsaid id; |
570 |
rsaencrdata encr; |
571 |
|
572 |
auth_req_packet (int dst, bool initiate_, u8 protocols_) |
573 |
{ |
574 |
config_packet::setup (PT_AUTH_REQ, dst); |
575 |
strncpy (magic, MAGIC, 8); |
576 |
initiate = !!initiate_; |
577 |
protocols = protocols_; |
578 |
|
579 |
len = sizeof (*this) - sizeof (net_packet); |
580 |
} |
581 |
}; |
582 |
|
583 |
struct auth_res_packet : config_packet |
584 |
{ |
585 |
rsaid id; |
586 |
u8 pad1, pad2, pad3; |
587 |
u8 response_len; // encrypted length |
588 |
rsaresponse response; |
589 |
|
590 |
auth_res_packet (int dst) |
591 |
{ |
592 |
config_packet::setup (PT_AUTH_RES, dst); |
593 |
|
594 |
len = sizeof (*this) - sizeof (net_packet); |
595 |
} |
596 |
}; |
597 |
|
598 |
struct connect_req_packet : vpn_packet |
599 |
{ |
600 |
u8 id, protocols; |
601 |
u8 pad1, pad2; |
602 |
|
603 |
connect_req_packet (int dst, int id_, u8 protocols_) |
604 |
: id(id_) |
605 |
, protocols(protocols_) |
606 |
{ |
607 |
set_hdr (PT_CONNECT_REQ, dst); |
608 |
len = sizeof (*this) - sizeof (net_packet); |
609 |
} |
610 |
}; |
611 |
|
612 |
struct connect_info_packet : vpn_packet |
613 |
{ |
614 |
u8 id, protocols; |
615 |
u8 pad1, pad2; |
616 |
sockinfo si; |
617 |
|
618 |
connect_info_packet (int dst, int id_, const sockinfo &si_, u8 protocols_) |
619 |
: id(id_) |
620 |
, protocols(protocols_) |
621 |
, si(si_) |
622 |
{ |
623 |
set_hdr (PT_CONNECT_INFO, dst); |
624 |
|
625 |
len = sizeof (*this) - sizeof (net_packet); |
626 |
} |
627 |
}; |
628 |
|
629 |
///////////////////////////////////////////////////////////////////////////// |
630 |
|
631 |
void |
632 |
connection::connection_established () |
633 |
{ |
634 |
slog (L_TRACE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx); |
635 |
|
636 |
if (ictx && octx) |
637 |
{ |
638 |
// make sure rekeying timeouts are slightly asymmetric |
639 |
ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); |
640 |
rekey.start (rekey_interval, rekey_interval); |
641 |
keepalive.start (::conf.keepalive); |
642 |
|
643 |
// send queued packets |
644 |
if (ictx && octx) |
645 |
{ |
646 |
while (tap_packet *p = (tap_packet *)data_queue.get ()) |
647 |
{ |
648 |
if (p->len) send_data_packet (p); |
649 |
delete p; |
650 |
} |
651 |
|
652 |
while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) |
653 |
{ |
654 |
if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); |
655 |
delete p; |
656 |
} |
657 |
} |
658 |
} |
659 |
else |
660 |
{ |
661 |
retry_cnt = 0; |
662 |
establish_connection.start (5); |
663 |
keepalive.stop (); |
664 |
rekey.stop (); |
665 |
} |
666 |
} |
667 |
|
668 |
void |
669 |
connection::reset_si () |
670 |
{ |
671 |
protocol = best_protocol (THISNODE->protocols & conf->protocols); |
672 |
|
673 |
// mask out protocols we cannot establish |
674 |
if (!conf->udp_port) protocol &= ~PROT_UDPv4; |
675 |
if (!conf->tcp_port) protocol &= ~PROT_TCPv4; |
676 |
if (!conf->dns_port) protocol &= ~PROT_DNSv4; |
677 |
|
678 |
if (protocol |
679 |
&& (!conf->can_direct (THISNODE) |
680 |
|| !THISNODE->can_direct (conf))) |
681 |
{ |
682 |
slog (L_DEBUG, _("%s: direct connection denied"), conf->nodename); |
683 |
protocol = 0; |
684 |
} |
685 |
|
686 |
si.set (conf, protocol); |
687 |
} |
688 |
|
689 |
// ensure sockinfo is valid, forward if necessary |
690 |
const sockinfo & |
691 |
connection::forward_si (const sockinfo &si) const |
692 |
{ |
693 |
if (!si.valid ()) |
694 |
{ |
695 |
connection *r = vpn->find_router (); |
696 |
|
697 |
if (r) |
698 |
{ |
699 |
slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s (%s)"), |
700 |
conf->nodename, r->conf->nodename, (const char *)r->si); |
701 |
return r->si; |
702 |
} |
703 |
else |
704 |
slog (L_DEBUG, _("%s: node unreachable, no common protocol"), |
705 |
conf->nodename); |
706 |
} |
707 |
|
708 |
return si; |
709 |
} |
710 |
|
711 |
void |
712 |
connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
713 |
{ |
714 |
if (!vpn->send_vpn_packet (pkt, si, tos)) |
715 |
reset_connection (); |
716 |
} |
717 |
|
718 |
void |
719 |
connection::send_ping (const sockinfo &si, u8 pong) |
720 |
{ |
721 |
ping_packet *pkt = new ping_packet; |
722 |
|
723 |
pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING); |
724 |
send_vpn_packet (pkt, si, IPTOS_LOWDELAY); |
725 |
|
726 |
delete pkt; |
727 |
} |
728 |
|
729 |
void |
730 |
connection::send_reset (const sockinfo &si) |
731 |
{ |
732 |
if (reset_rate_limiter.can (si) && connectmode != conf_node::C_DISABLED) |
733 |
{ |
734 |
config_packet *pkt = new config_packet; |
735 |
|
736 |
pkt->setup (vpn_packet::PT_RESET, conf->id); |
737 |
send_vpn_packet (pkt, si, IPTOS_MINCOST); |
738 |
|
739 |
delete pkt; |
740 |
} |
741 |
} |
742 |
|
743 |
void |
744 |
connection::send_auth_request (const sockinfo &si, bool initiate) |
745 |
{ |
746 |
auth_req_packet *pkt = new auth_req_packet (conf->id, initiate, THISNODE->protocols); |
747 |
|
748 |
rsachallenge chg; |
749 |
rsa_cache.gen (pkt->id, chg); |
750 |
rsa_encrypt (conf->rsa_key, chg, pkt->encr); |
751 |
|
752 |
slog (L_TRACE, ">>%d PT_AUTH_REQ [%s]", conf->id, (const char *)si); |
753 |
|
754 |
send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly |
755 |
|
756 |
delete pkt; |
757 |
} |
758 |
|
759 |
void |
760 |
connection::send_auth_response (const sockinfo &si, const rsaid &id, const rsachallenge &chg) |
761 |
{ |
762 |
auth_res_packet *pkt = new auth_res_packet (conf->id); |
763 |
|
764 |
pkt->id = id; |
765 |
|
766 |
rsa_hash (id, chg, pkt->response); |
767 |
|
768 |
pkt->hmac_set (octx); |
769 |
|
770 |
slog (L_TRACE, ">>%d PT_AUTH_RES [%s]", conf->id, (const char *)si); |
771 |
|
772 |
send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly |
773 |
|
774 |
delete pkt; |
775 |
} |
776 |
|
777 |
void |
778 |
connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) |
779 |
{ |
780 |
slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)", |
781 |
conf->id, rid, (const char *)rsi); |
782 |
|
783 |
connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); |
784 |
|
785 |
r->hmac_set (octx); |
786 |
send_vpn_packet (r, si); |
787 |
|
788 |
delete r; |
789 |
} |
790 |
|
791 |
inline void |
792 |
connection::establish_connection_cb (ev::timer &w, int revents) |
793 |
{ |
794 |
if (!ictx |
795 |
&& conf != THISNODE |
796 |
&& connectmode != conf_node::C_NEVER |
797 |
&& connectmode != conf_node::C_DISABLED |
798 |
&& !w.is_active ()) |
799 |
{ |
800 |
// a bit hacky, if ondemand, and packets are no longer queued, then reset the connection |
801 |
// and stop trying. should probably be handled by a per-connection expire handler. |
802 |
if (connectmode == conf_node::C_ONDEMAND && vpn_queue.empty () && data_queue.empty ()) |
803 |
{ |
804 |
reset_connection (); |
805 |
return; |
806 |
} |
807 |
|
808 |
last_establish_attempt = ev_now (); |
809 |
|
810 |
ev::tstamp retry_int = ev::tstamp (retry_cnt & 3 |
811 |
? (retry_cnt & 3) + 1 |
812 |
: 1 << (retry_cnt >> 2)); |
813 |
|
814 |
reset_si (); |
815 |
|
816 |
bool slow = si.prot & PROT_SLOW; |
817 |
|
818 |
if (si.prot && !si.host) |
819 |
{ |
820 |
slog (L_TRACE, _("%s: connection request (indirect)"), conf->nodename); |
821 |
/*TODO*/ /* start the timer so we don't recurse endlessly */ |
822 |
w.start (1); |
823 |
vpn->send_connect_request (conf->id); |
824 |
} |
825 |
else |
826 |
{ |
827 |
slog (L_TRACE, _("%s: connection request (direct)"), conf->nodename, !!ictx, !!octx); |
828 |
|
829 |
const sockinfo &dsi = forward_si (si); |
830 |
|
831 |
slow = slow || (dsi.prot & PROT_SLOW); |
832 |
|
833 |
if (dsi.valid () && auth_rate_limiter.can (dsi)) |
834 |
{ |
835 |
if (retry_cnt < 4) |
836 |
send_auth_request (dsi, true); |
837 |
else |
838 |
send_ping (dsi, 0); |
839 |
} |
840 |
} |
841 |
|
842 |
retry_int *= slow ? 8. : 0.9; |
843 |
|
844 |
if (retry_int < conf->max_retry) |
845 |
retry_cnt++; |
846 |
else |
847 |
retry_int = conf->max_retry; |
848 |
|
849 |
w.start (retry_int); |
850 |
} |
851 |
} |
852 |
|
853 |
void |
854 |
connection::reset_connection () |
855 |
{ |
856 |
if (ictx && octx) |
857 |
{ |
858 |
slog (L_INFO, _("%s(%s): connection lost"), |
859 |
conf->nodename, (const char *)si); |
860 |
|
861 |
if (::conf.script_node_down) |
862 |
{ |
863 |
run_script_cb cb; |
864 |
cb.set<connection, &connection::script_node_down> (this); |
865 |
if (!run_script (cb, false)) |
866 |
slog (L_WARN, _("node-down command execution failed, continuing.")); |
867 |
} |
868 |
} |
869 |
|
870 |
delete ictx; ictx = 0; |
871 |
delete octx; octx = 0; |
872 |
#if ENABLE_DNS |
873 |
dnsv4_reset_connection (); |
874 |
#endif |
875 |
|
876 |
si.host = 0; |
877 |
|
878 |
last_activity = 0; |
879 |
retry_cnt = 0; |
880 |
|
881 |
rekey.stop (); |
882 |
keepalive.stop (); |
883 |
establish_connection.stop (); |
884 |
} |
885 |
|
886 |
void |
887 |
connection::shutdown () |
888 |
{ |
889 |
if (ictx && octx) |
890 |
send_reset (si); |
891 |
|
892 |
reset_connection (); |
893 |
} |
894 |
|
895 |
inline void |
896 |
connection::rekey_cb (ev::timer &w, int revents) |
897 |
{ |
898 |
reset_connection (); |
899 |
establish_connection (); |
900 |
} |
901 |
|
902 |
void |
903 |
connection::send_data_packet (tap_packet *pkt) |
904 |
{ |
905 |
vpndata_packet *p = new vpndata_packet; |
906 |
int tos = 0; |
907 |
|
908 |
// I am not hilarious about peeking into packets, but so be it. |
909 |
if (conf->inherit_tos && pkt->is_ipv4 ()) |
910 |
tos = (*pkt)[15] & IPTOS_TOS_MASK; |
911 |
|
912 |
p->setup (this, conf->id, &((*pkt)[6 + 6]), pkt->len - 6 - 6, ++oseqno); // skip 2 macs |
913 |
send_vpn_packet (p, si, tos); |
914 |
|
915 |
delete p; |
916 |
|
917 |
if (oseqno > MAX_SEQNO) |
918 |
rekey (); |
919 |
} |
920 |
|
921 |
void |
922 |
connection::post_inject_queue () |
923 |
{ |
924 |
// force a connection every now and when when packets are sent (max 1/s) |
925 |
if (ev_now () - last_establish_attempt >= 0.95) // arbitrary |
926 |
establish_connection.stop (); |
927 |
|
928 |
establish_connection (); |
929 |
} |
930 |
|
931 |
void |
932 |
connection::inject_data_packet (tap_packet *pkt) |
933 |
{ |
934 |
if (ictx && octx) |
935 |
send_data_packet (pkt); |
936 |
else |
937 |
{ |
938 |
data_queue.put (new tap_packet (*pkt)); |
939 |
post_inject_queue (); |
940 |
} |
941 |
} |
942 |
|
943 |
void connection::inject_vpn_packet (vpn_packet *pkt, int tos) |
944 |
{ |
945 |
if (ictx && octx) |
946 |
send_vpn_packet (pkt, si, tos); |
947 |
else |
948 |
{ |
949 |
vpn_queue.put ((vpn_packet *)new data_packet (*(data_packet *)pkt)); |
950 |
post_inject_queue (); |
951 |
} |
952 |
} |
953 |
|
954 |
void |
955 |
connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
956 |
{ |
957 |
last_activity = ev_now (); |
958 |
|
959 |
slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
960 |
conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
961 |
|
962 |
if (connectmode == conf_node::C_DISABLED) |
963 |
return; |
964 |
|
965 |
switch (pkt->typ ()) |
966 |
{ |
967 |
case vpn_packet::PT_PING: |
968 |
// we send pings instead of auth packets after some retries, |
969 |
// so reset the retry counter and establish a connection |
970 |
// when we receive a ping. |
971 |
if (!ictx) |
972 |
{ |
973 |
if (auth_rate_limiter.can (rsi)) |
974 |
send_auth_request (rsi, true); |
975 |
} |
976 |
else |
977 |
send_ping (rsi, 1); // pong |
978 |
|
979 |
break; |
980 |
|
981 |
case vpn_packet::PT_PONG: |
982 |
break; |
983 |
|
984 |
case vpn_packet::PT_RESET: |
985 |
{ |
986 |
reset_connection (); |
987 |
|
988 |
config_packet *p = (config_packet *) pkt; |
989 |
|
990 |
if (!p->chk_config ()) |
991 |
{ |
992 |
slog (L_WARN, _("%s(%s): protocol mismatch, disabling node"), |
993 |
conf->nodename, (const char *)rsi); |
994 |
connectmode = conf_node::C_DISABLED; |
995 |
} |
996 |
else if (connectmode == conf_node::C_ALWAYS) |
997 |
establish_connection (); |
998 |
} |
999 |
break; |
1000 |
|
1001 |
case vpn_packet::PT_AUTH_REQ: |
1002 |
if (auth_rate_limiter.can (rsi)) |
1003 |
{ |
1004 |
auth_req_packet *p = (auth_req_packet *) pkt; |
1005 |
|
1006 |
slog (L_TRACE, "<<%d PT_AUTH_REQ(%d)", conf->id, p->initiate); |
1007 |
|
1008 |
if (p->chk_config () && !strncmp (p->magic, MAGIC, 8)) |
1009 |
{ |
1010 |
if (p->prot_minor != PROTOCOL_MINOR) |
1011 |
slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
1012 |
conf->nodename, (const char *)rsi, |
1013 |
PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
1014 |
|
1015 |
if (p->initiate) |
1016 |
send_auth_request (rsi, false); |
1017 |
|
1018 |
rsachallenge k; |
1019 |
|
1020 |
if (!rsa_decrypt (::conf.rsa_key, p->encr, k)) |
1021 |
{ |
1022 |
slog (L_ERR, _("%s(%s): challenge illegal or corrupted (%s). mismatched key or config file?"), |
1023 |
conf->nodename, (const char *)rsi, ERR_error_string (ERR_get_error (), 0)); |
1024 |
break; |
1025 |
} |
1026 |
else |
1027 |
{ |
1028 |
delete octx; |
1029 |
|
1030 |
octx = new crypto_ctx (k, 1); |
1031 |
oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; |
1032 |
|
1033 |
conf->protocols = p->protocols; |
1034 |
features = p->features & config_packet::get_features (); |
1035 |
|
1036 |
send_auth_response (rsi, p->id, k); |
1037 |
|
1038 |
connection_established (); |
1039 |
|
1040 |
break; |
1041 |
} |
1042 |
} |
1043 |
else |
1044 |
slog (L_WARN, _("%s(%s): protocol mismatch"), |
1045 |
conf->nodename, (const char *)rsi); |
1046 |
|
1047 |
send_reset (rsi); |
1048 |
} |
1049 |
|
1050 |
break; |
1051 |
|
1052 |
case vpn_packet::PT_AUTH_RES: |
1053 |
{ |
1054 |
auth_res_packet *p = (auth_res_packet *) pkt; |
1055 |
|
1056 |
slog (L_TRACE, "<<%d PT_AUTH_RES", conf->id); |
1057 |
|
1058 |
if (p->chk_config ()) |
1059 |
{ |
1060 |
if (p->prot_minor != PROTOCOL_MINOR) |
1061 |
slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
1062 |
conf->nodename, (const char *)rsi, |
1063 |
PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
1064 |
|
1065 |
rsachallenge chg; |
1066 |
|
1067 |
if (!rsa_cache.find (p->id, chg)) |
1068 |
{ |
1069 |
slog (L_ERR, _("%s(%s): unrequested auth response ignored"), |
1070 |
conf->nodename, (const char *)rsi); |
1071 |
break; |
1072 |
} |
1073 |
else |
1074 |
{ |
1075 |
crypto_ctx *cctx = new crypto_ctx (chg, 0); |
1076 |
|
1077 |
if (!p->hmac_chk (cctx)) |
1078 |
{ |
1079 |
slog (L_ERR, _("%s(%s): hmac authentication error on auth response, received invalid packet\n" |
1080 |
"could be an attack, or just corruption or a synchronization error"), |
1081 |
conf->nodename, (const char *)rsi); |
1082 |
break; |
1083 |
} |
1084 |
else |
1085 |
{ |
1086 |
rsaresponse h; |
1087 |
|
1088 |
rsa_hash (p->id, chg, h); |
1089 |
|
1090 |
if (!memcmp ((u8 *)&h, (u8 *)p->response, sizeof h)) |
1091 |
{ |
1092 |
prot_minor = p->prot_minor; |
1093 |
|
1094 |
delete ictx; ictx = cctx; |
1095 |
|
1096 |
iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid |
1097 |
|
1098 |
si = rsi; |
1099 |
protocol = rsi.prot; |
1100 |
|
1101 |
connection_established (); |
1102 |
|
1103 |
slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
1104 |
conf->nodename, (const char *)rsi, |
1105 |
p->prot_major, p->prot_minor); |
1106 |
|
1107 |
if (::conf.script_node_up) |
1108 |
{ |
1109 |
run_script_cb cb; |
1110 |
cb.set<connection, &connection::script_node_up> (this); |
1111 |
if (!run_script (cb, false)) |
1112 |
slog (L_WARN, _("node-up command execution failed, continuing.")); |
1113 |
} |
1114 |
|
1115 |
break; |
1116 |
} |
1117 |
else |
1118 |
slog (L_ERR, _("%s(%s): sent and received challenge do not match"), |
1119 |
conf->nodename, (const char *)rsi); |
1120 |
} |
1121 |
|
1122 |
delete cctx; |
1123 |
} |
1124 |
} |
1125 |
} |
1126 |
|
1127 |
send_reset (rsi); |
1128 |
break; |
1129 |
|
1130 |
case vpn_packet::PT_DATA_COMPRESSED: |
1131 |
#if !ENABLE_COMPRESSION |
1132 |
send_reset (rsi); |
1133 |
break; |
1134 |
#endif |
1135 |
|
1136 |
case vpn_packet::PT_DATA_UNCOMPRESSED: |
1137 |
|
1138 |
if (ictx && octx) |
1139 |
{ |
1140 |
vpndata_packet *p = (vpndata_packet *)pkt; |
1141 |
|
1142 |
if (!p->hmac_chk (ictx)) |
1143 |
slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" |
1144 |
"could be an attack, or just corruption or a synchronization error"), |
1145 |
conf->nodename, (const char *)rsi); |
1146 |
else |
1147 |
{ |
1148 |
u32 seqno; |
1149 |
tap_packet *d = p->unpack (this, seqno); |
1150 |
|
1151 |
if (iseqno.recv_ok (seqno)) |
1152 |
{ |
1153 |
vpn->tap->send (d); |
1154 |
|
1155 |
if (si != rsi) |
1156 |
{ |
1157 |
// fast re-sync on source address changes, useful especially for tcp/ip |
1158 |
si = rsi; |
1159 |
|
1160 |
slog (L_INFO, _("%s(%s): socket address changed to %s"), |
1161 |
conf->nodename, (const char *)si, (const char *)rsi); |
1162 |
} |
1163 |
} |
1164 |
|
1165 |
delete d; |
1166 |
break; |
1167 |
} |
1168 |
} |
1169 |
|
1170 |
send_reset (rsi); |
1171 |
break; |
1172 |
|
1173 |
case vpn_packet::PT_CONNECT_REQ: |
1174 |
if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1175 |
{ |
1176 |
connect_req_packet *p = (connect_req_packet *) pkt; |
1177 |
|
1178 |
if (p->id > 0 && p->id <= vpn->conns.size ()) |
1179 |
{ |
1180 |
connection *c = vpn->conns[p->id - 1]; |
1181 |
conf->protocols = p->protocols; |
1182 |
|
1183 |
slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]", |
1184 |
conf->id, p->id, c->ictx && c->octx); |
1185 |
|
1186 |
if (c->ictx && c->octx) |
1187 |
{ |
1188 |
// send connect_info packets to both sides, in case one is |
1189 |
// behind a nat firewall (or both ;) |
1190 |
c->send_connect_info (conf->id, si, conf->protocols); |
1191 |
send_connect_info (c->conf->id, c->si, c->conf->protocols); |
1192 |
} |
1193 |
else |
1194 |
c->establish_connection (); |
1195 |
} |
1196 |
else |
1197 |
slog (L_WARN, |
1198 |
_("received authenticated connection request from unknown node #%d, config file mismatch?"), |
1199 |
p->id); |
1200 |
} |
1201 |
|
1202 |
break; |
1203 |
|
1204 |
case vpn_packet::PT_CONNECT_INFO: |
1205 |
if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1206 |
{ |
1207 |
connect_info_packet *p = (connect_info_packet *)pkt; |
1208 |
|
1209 |
if (p->id > 0 && p->id <= vpn->conns.size ()) |
1210 |
{ |
1211 |
connection *c = vpn->conns[p->id - 1]; |
1212 |
|
1213 |
c->conf->protocols = p->protocols; |
1214 |
protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); |
1215 |
p->si.upgrade_protocol (protocol, c->conf); |
1216 |
|
1217 |
slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", |
1218 |
conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); |
1219 |
|
1220 |
const sockinfo &dsi = forward_si (p->si); |
1221 |
|
1222 |
if (dsi.valid ()) |
1223 |
c->send_auth_request (dsi, true); |
1224 |
} |
1225 |
else |
1226 |
slog (L_WARN, |
1227 |
_("received authenticated connection request from unknown node #%d, config file mismatch?"), |
1228 |
p->id); |
1229 |
} |
1230 |
|
1231 |
break; |
1232 |
|
1233 |
default: |
1234 |
send_reset (rsi); |
1235 |
break; |
1236 |
} |
1237 |
} |
1238 |
|
1239 |
inline void |
1240 |
connection::keepalive_cb (ev::timer &w, int revents) |
1241 |
{ |
1242 |
if (ev_now () >= last_activity + ::conf.keepalive + 30) |
1243 |
{ |
1244 |
reset_connection (); |
1245 |
establish_connection (); |
1246 |
} |
1247 |
else if (ev_now () < last_activity + ::conf.keepalive) |
1248 |
w.start (last_activity + ::conf.keepalive - ev::now ()); |
1249 |
else if (conf->connectmode != conf_node::C_ONDEMAND |
1250 |
|| THISNODE->connectmode != conf_node::C_ONDEMAND) |
1251 |
{ |
1252 |
send_ping (si); |
1253 |
w.start (5); |
1254 |
} |
1255 |
else if (ev_now () < last_activity + ::conf.keepalive + 10) |
1256 |
// hold ondemand connections implicitly a few seconds longer |
1257 |
// should delete octx, though, or something like that ;) |
1258 |
w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); |
1259 |
else |
1260 |
reset_connection (); |
1261 |
} |
1262 |
|
1263 |
void connection::send_connect_request (int id) |
1264 |
{ |
1265 |
connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); |
1266 |
|
1267 |
slog (L_TRACE, ">>%d PT_CONNECT_REQ(%d)", conf->id, id); |
1268 |
p->hmac_set (octx); |
1269 |
send_vpn_packet (p, si); |
1270 |
|
1271 |
delete p; |
1272 |
} |
1273 |
|
1274 |
void connection::script_init_env (const char *ext) |
1275 |
{ |
1276 |
char *env; |
1277 |
asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); |
1278 |
asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); |
1279 |
asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, |
1280 |
0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, |
1281 |
conf->id & 0xff); putenv (env); |
1282 |
} |
1283 |
|
1284 |
void connection::script_init_connect_env () |
1285 |
{ |
1286 |
vpn->script_init_env (); |
1287 |
|
1288 |
char *env; |
1289 |
asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
1290 |
asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1291 |
asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1292 |
asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1293 |
} |
1294 |
|
1295 |
inline const char * |
1296 |
connection::script_node_up () |
1297 |
{ |
1298 |
script_init_connect_env (); |
1299 |
|
1300 |
putenv ((char *)"STATE=up"); |
1301 |
|
1302 |
char *filename; |
1303 |
asprintf (&filename, |
1304 |
"%s/%s", |
1305 |
confbase, |
1306 |
::conf.script_node_up ? ::conf.script_node_up : "node-up"); |
1307 |
|
1308 |
return filename; |
1309 |
} |
1310 |
|
1311 |
inline const char * |
1312 |
connection::script_node_down () |
1313 |
{ |
1314 |
script_init_connect_env (); |
1315 |
|
1316 |
putenv ((char *)"STATE=down"); |
1317 |
|
1318 |
char *filename; |
1319 |
asprintf (&filename, |
1320 |
"%s/%s", |
1321 |
confbase, |
1322 |
::conf.script_node_down ? ::conf.script_node_down : "node-down"); |
1323 |
|
1324 |
return filename; |
1325 |
} |
1326 |
|
1327 |
connection::connection (struct vpn *vpn, conf_node *conf) |
1328 |
: vpn(vpn), conf(conf), |
1329 |
#if ENABLE_DNS |
1330 |
dns (0), |
1331 |
#endif |
1332 |
data_queue(conf->max_ttl, conf->max_queue), |
1333 |
vpn_queue(conf->max_ttl, conf->max_queue) |
1334 |
{ |
1335 |
rekey .set<connection, &connection::rekey_cb > (this); |
1336 |
keepalive .set<connection, &connection::keepalive_cb > (this); |
1337 |
establish_connection.set<connection, &connection::establish_connection_cb> (this); |
1338 |
|
1339 |
last_establish_attempt = 0.; |
1340 |
octx = ictx = 0; |
1341 |
|
1342 |
if (!conf->protocols) // make sure some protocol is enabled |
1343 |
conf->protocols = PROT_UDPv4; |
1344 |
|
1345 |
connectmode = conf->connectmode; |
1346 |
|
1347 |
// queue a dummy packet to force an initial connection attempt |
1348 |
if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) |
1349 |
vpn_queue.put (new net_packet); |
1350 |
|
1351 |
reset_connection (); |
1352 |
} |
1353 |
|
1354 |
connection::~connection () |
1355 |
{ |
1356 |
shutdown (); |
1357 |
} |
1358 |
|
1359 |
void connection_init () |
1360 |
{ |
1361 |
auth_rate_limiter.clear (); |
1362 |
reset_rate_limiter.clear (); |
1363 |
} |
1364 |
|