ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/connection.C
(Generate patch)

Comparing gvpe/src/connection.C (file contents):
Revision 1.14 by pcg, Fri Aug 8 09:29:22 2003 UTC vs.
Revision 1.66 by pcg, Thu Dec 6 00:35:29 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines