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