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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines