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.43 by pcg, Fri Mar 4 04:52:38 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
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"
40 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;
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 = ENABLE_COMPRESSION ? 0x81 : 0x80;
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);
503#if 0 // this implementation should handle all flag settings
504 else if (flags != curflags ())
505 slog (L_WARN, _("flag mismatch (remote %x <=> local %x)"), flags, curflags ());
506#endif
471 && challengelen == sizeof (rsachallenge) 507 else if (challengelen != sizeof (rsachallenge))
508 slog (L_WARN, _("challenge length mismatch (remote %d <=> local %d)"), challengelen, sizeof (rsachallenge));
472 && cipher_nid == htonl (EVP_CIPHER_nid (CIPHER)) 509 else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER)))
510 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)) 511 else if (digest_nid != htonl (EVP_MD_type (RSA_HASH)))
512 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)); 513 else if (hmac_nid != htonl (EVP_MD_type (DIGEST)))
514 slog (L_WARN, _("hmac mismatch (remote %x <=> local %x)"), ntohl (hmac_nid), EVP_MD_type (DIGEST));
515 else
516 return true;
517
518 return false;
475} 519}
476 520
477struct auth_req_packet : config_packet 521struct auth_req_packet : config_packet
478{ 522{
479 char magic[8]; 523 char magic[8];
480 u8 initiate; // false if this is just an automatic reply 524 u8 initiate; // false if this is just an automatic reply
481 u8 protocols; // supported protocols (will get patches on forward) 525 u8 protocols; // supported protocols (will be patched on forward)
482 u8 pad2, pad3; 526 u8 pad2, pad3;
483 rsaid id; 527 rsaid id;
484 rsaencrdata encr; 528 rsaencrdata encr;
485 529
486 auth_req_packet (int dst, bool initiate_, u8 protocols_) 530 auth_req_packet (int dst, bool initiate_, u8 protocols_)
541}; 585};
542 586
543///////////////////////////////////////////////////////////////////////////// 587/////////////////////////////////////////////////////////////////////////////
544 588
545void 589void
590connection::connection_established ()
591{
592 if (ictx && octx)
593 {
594 connectmode = conf->connectmode;
595
596 // make sure rekeying timeouts are slightly asymmetric
597 rekey.start (NOW + ::conf.rekey
598 + (conf->id > THISNODE->id ? 10 : 0));
599 keepalive.start (NOW + ::conf.keepalive);
600
601 // send queued packets
602 if (ictx && octx)
603 {
604 while (tap_packet *p = (tap_packet *)data_queue.get ())
605 {
606 send_data_packet (p);
607 delete p;
608 }
609
610 while (vpn_packet *p = (vpn_packet *)vpn_queue.get ())
611 {
612 send_vpn_packet (p, si, IPTOS_RELIABILITY);
613 delete p;
614 }
615 }
616 }
617 else
618 {
619 retry_cnt = 0;
620 establish_connection.start (NOW + 5);
621 keepalive.stop ();
622 rekey.stop ();
623 }
624}
625
626void
546connection::reset_dstaddr () 627connection::reset_si ()
547{ 628{
629 protocol = best_protocol (THISNODE->protocols & conf->protocols);
630
631 // mask out protocols we cannot establish
632 if (!conf->udp_port) protocol &= ~PROT_UDPv4;
633 if (!conf->tcp_port) protocol &= ~PROT_TCPv4;
634 if (!conf->dns_port) protocol &= ~PROT_DNSv4;
635
548 si.set (conf); 636 si.set (conf, protocol);
637}
638
639// ensure sockinfo is valid, forward if necessary
640const sockinfo &
641connection::forward_si (const sockinfo &si) const
642{
643 if (!si.valid ())
644 {
645 connection *r = vpn->find_router ();
646
647 if (r)
648 {
649 slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s"),
650 conf->nodename, r->conf->nodename);
651 return r->si;
652 }
653 else
654 slog (L_DEBUG, _("%s: node unreachable, no common protocol"),
655 conf->nodename);
656 }
657
658 return si;
659}
660
661void
662connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos)
663{
664 bool ok;
665
666 switch (si.prot)
667 {
668 case PROT_IPv4:
669 ok = vpn->send_ipv4_packet (pkt, si, tos); break;
670 case PROT_UDPv4:
671 ok = vpn->send_udpv4_packet (pkt, si, tos); break;
672#if ENABLE_TCP
673 case PROT_TCPv4:
674 ok = vpn->send_tcpv4_packet (pkt, si, tos); break;
675#endif
676#if ENABLE_ICMP
677 case PROT_ICMPv4:
678 ok = vpn->send_icmpv4_packet (pkt, si, tos); break;
679#endif
680#if ENABLE_DNS
681 case PROT_DNSv4:
682 ok = send_dnsv4_packet (pkt, si, tos); break;
683#endif
684
685 default:
686 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si);
687 ok = false;
688 }
689
690 if (!ok)
691 reset_connection ();
549} 692}
550 693
551void 694void
552connection::send_ping (const sockinfo &si, u8 pong) 695connection::send_ping (const sockinfo &si, u8 pong)
553{ 696{
576void 719void
577connection::send_auth_request (const sockinfo &si, bool initiate) 720connection::send_auth_request (const sockinfo &si, bool initiate)
578{ 721{
579 auth_req_packet *pkt = new auth_req_packet (conf->id, initiate, THISNODE->protocols); 722 auth_req_packet *pkt = new auth_req_packet (conf->id, initiate, THISNODE->protocols);
580 723
581 protocol = best_protocol (THISNODE->protocols & conf->protocols);
582
583 rsachallenge chg; 724 rsachallenge chg;
584
585 rsa_cache.gen (pkt->id, chg); 725 rsa_cache.gen (pkt->id, chg);
586 726 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 727
592 slog (L_TRACE, ">>%d PT_AUTH_REQ [%s]", conf->id, (const char *)si); 728 slog (L_TRACE, ">>%d PT_AUTH_REQ [%s]", conf->id, (const char *)si);
593 729
594 send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly 730 send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly
595 731
596 delete pkt; 732 delete pkt;
597} 733}
598 734
599void 735void
627 763
628 delete r; 764 delete r;
629} 765}
630 766
631void 767void
632connection::establish_connection_cb (tstamp &ts) 768connection::establish_connection_cb (time_watcher &w)
633{ 769{
634 if (ictx || conf == THISNODE 770 if (!ictx
771 && conf != THISNODE
635 || connectmode == conf_node::C_NEVER 772 && connectmode != conf_node::C_NEVER
636 || connectmode == conf_node::C_DISABLED) 773 && connectmode != conf_node::C_DISABLED
637 ts = TSTAMP_CANCEL; 774 && NOW > w.at)
638 else if (ts <= NOW)
639 { 775 {
640 double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; 776 double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6;
641 777
642 if (retry_int < 3600 * 8) 778 if (retry_int < conf->max_retry)
643 retry_cnt++; 779 retry_cnt++;
780 else
781 retry_int = conf->max_retry;
644 782
645 ts = NOW + retry_int; 783 w.start (NOW + retry_int);
646 784
647 if (conf->hostname) 785 reset_si ();
786
787 if (si.prot && !si.host)
788 vpn->send_connect_request (conf->id);
789 else
648 { 790 {
649 reset_dstaddr (); 791 const sockinfo &dsi = forward_si (si);
792
650 if (si.host && auth_rate_limiter.can (si)) 793 if (dsi.valid () && auth_rate_limiter.can (dsi))
651 { 794 {
652 if (retry_cnt < 4) 795 if (retry_cnt < 4)
653 send_auth_request (si, true); 796 send_auth_request (dsi, true);
654 else 797 else
655 send_ping (si, 0); 798 send_ping (dsi, 0);
656 } 799 }
657 } 800 }
658 else
659 vpn->connect_request (conf->id);
660 } 801 }
661} 802}
662 803
663void 804void
664connection::reset_connection () 805connection::reset_connection ()
673 } 814 }
674 815
675 delete ictx; ictx = 0; 816 delete ictx; ictx = 0;
676 delete octx; octx = 0; 817 delete octx; octx = 0;
677 818
678 si.host= 0; 819 si.host = 0;
679 820
680 last_activity = 0; 821 last_activity = 0;
681 retry_cnt = 0; 822 retry_cnt = 0;
682 823
683 rekey.reset (); 824 rekey.stop ();
684 keepalive.reset (); 825 keepalive.stop ();
685 establish_connection.reset (); 826 establish_connection.stop ();
686} 827}
687 828
688void 829void
689connection::shutdown () 830connection::shutdown ()
690{ 831{
693 834
694 reset_connection (); 835 reset_connection ();
695} 836}
696 837
697void 838void
698connection::rekey_cb (tstamp &ts) 839connection::rekey_cb (time_watcher &w)
699{ 840{
700 ts = TSTAMP_CANCEL;
701
702 reset_connection (); 841 reset_connection ();
703 establish_connection (); 842 establish_connection ();
704} 843}
705 844
706void 845void
707connection::send_data_packet (tap_packet *pkt, bool broadcast) 846connection::send_data_packet (tap_packet *pkt)
708{ 847{
709 vpndata_packet *p = new vpndata_packet; 848 vpndata_packet *p = new vpndata_packet;
710 int tos = 0; 849 int tos = 0;
711 850
712 if (conf->inherit_tos 851 // I am not hilarious about peeking into packets, but so be it.
713 && (*pkt)[12] == 0x08 && (*pkt)[13] == 0x00 // IP 852 if (conf->inherit_tos && pkt->is_ipv4 ())
714 && ((*pkt)[14] & 0xf0) == 0x40) // IPv4
715 tos = (*pkt)[15] & IPTOS_TOS_MASK; 853 tos = (*pkt)[15] & IPTOS_TOS_MASK;
716 854
717 p->setup (this, broadcast ? 0 : conf->id, &((*pkt)[6 + 6]), pkt->len - 6 - 6, ++oseqno); // skip 2 macs 855 p->setup (this, conf->id, &((*pkt)[6 + 6]), pkt->len - 6 - 6, ++oseqno); // skip 2 macs
718 send_vpn_packet (p, si, tos); 856 send_vpn_packet (p, si, tos);
719 857
720 delete p; 858 delete p;
721 859
722 if (oseqno > MAX_SEQNO) 860 if (oseqno > MAX_SEQNO)
723 rekey (); 861 rekey ();
724} 862}
725 863
726void 864void
727connection::inject_data_packet (tap_packet *pkt, bool broadcast) 865connection::inject_data_packet (tap_packet *pkt, bool broadcast/*TODO DDD*/)
728{ 866{
729 if (ictx && octx) 867 if (ictx && octx)
730 send_data_packet (pkt, broadcast); 868 send_data_packet (pkt);
731 else 869 else
732 { 870 {
733 if (!broadcast)//DDDD 871 if (!broadcast)//DDDD
734 queue.put (new tap_packet (*pkt)); 872 data_queue.put (new tap_packet (*pkt));
873
874 establish_connection ();
875 }
876}
877
878void connection::inject_vpn_packet (vpn_packet *pkt, int tos)
879{
880 if (ictx && octx)
881 send_vpn_packet (pkt, si, tos);
882 else
883 {
884 vpn_queue.put ((vpn_packet *)new data_packet (*(data_packet *)pkt));
735 885
736 establish_connection (); 886 establish_connection ();
737 } 887 }
738} 888}
739 889
745 slog (L_NOISE, "<<%d received packet type %d from %d to %d", 895 slog (L_NOISE, "<<%d received packet type %d from %d to %d",
746 conf->id, pkt->typ (), pkt->src (), pkt->dst ()); 896 conf->id, pkt->typ (), pkt->src (), pkt->dst ());
747 897
748 switch (pkt->typ ()) 898 switch (pkt->typ ())
749 { 899 {
750 case vpn_packet::PT_PING: 900 case vpn_packet::PT_PING:
751 // we send pings instead of auth packets after some retries, 901 // we send pings instead of auth packets after some retries,
752 // so reset the retry counter and establish a connection 902 // so reset the retry counter and establish a connection
753 // when we receive a ping. 903 // when we receive a ping.
754 if (!ictx) 904 if (!ictx)
905 {
906 if (auth_rate_limiter.can (rsi))
907 send_auth_request (rsi, true);
908 }
909 else
910 send_ping (rsi, 1); // pong
911
912 break;
913
914 case vpn_packet::PT_PONG:
915 break;
916
917 case vpn_packet::PT_RESET:
755 { 918 {
756 if (auth_rate_limiter.can (rsi)) 919 reset_connection ();
757 send_auth_request (rsi, true); 920
921 config_packet *p = (config_packet *) pkt;
922
923 if (!p->chk_config ())
924 {
925 slog (L_WARN, _("%s(%s): protocol mismatch, disabling node"),
926 conf->nodename, (const char *)rsi);
927 connectmode = conf_node::C_DISABLED;
928 }
929 else if (connectmode == conf_node::C_ALWAYS)
930 establish_connection ();
758 } 931 }
759 else
760 send_ping (rsi, 1); // pong
761
762 break; 932 break;
763 933
764 case vpn_packet::PT_PONG:
765 break;
766
767 case vpn_packet::PT_RESET: 934 case vpn_packet::PT_AUTH_REQ:
768 { 935 if (auth_rate_limiter.can (rsi))
769 reset_connection ();
770
771 config_packet *p = (config_packet *) pkt;
772
773 if (!p->chk_config ())
774 { 936 {
937 auth_req_packet *p = (auth_req_packet *) pkt;
938
939 slog (L_TRACE, "<<%d PT_AUTH_REQ(%d)", conf->id, p->initiate);
940
941 if (p->chk_config () && !strncmp (p->magic, MAGIC, 8))
942 {
943 if (p->prot_minor != PROTOCOL_MINOR)
944 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."),
945 conf->nodename, (const char *)rsi,
946 PROTOCOL_MINOR, conf->nodename, p->prot_minor);
947
948 if (p->initiate)
949 send_auth_request (rsi, false);
950
951 rsachallenge k;
952
953 if (!rsa_decrypt (::conf.rsa_key, p->encr, k))
954 {
955 slog (L_ERR, _("%s(%s): challenge illegal or corrupted (%s). mismatched key or config file?"),
956 conf->nodename, (const char *)rsi, ERR_error_string (ERR_get_error (), 0));
957 break;
958 }
959 else
960 {
961 delete octx;
962
963 octx = new crypto_ctx (k, 1);
964 oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff;
965
966 // compatibility code, remove when no longer required
967 if (p->flags & 1) p->features |= FEATURE_COMPRESSION;
968
969 conf->protocols = p->protocols;
970 features = p->features & config_packet::get_features ();
971
972 send_auth_response (rsi, p->id, k);
973
974 connection_established ();
975
976 break;
977 }
978 }
979 else
775 slog (L_WARN, _("%s(%s): protocol mismatch, disabling node"), 980 slog (L_WARN, _("%s(%s): protocol mismatch"),
776 conf->nodename, (const char *)rsi); 981 conf->nodename, (const char *)rsi);
777 connectmode = conf_node::C_DISABLED; 982
983 send_reset (rsi);
778 } 984 }
779 else if (connectmode == conf_node::C_ALWAYS) 985
780 establish_connection ();
781 }
782 break; 986 break;
783 987
784 case vpn_packet::PT_AUTH_REQ: 988 case vpn_packet::PT_AUTH_RES:
785 if (auth_rate_limiter.can (rsi))
786 { 989 {
787 auth_req_packet *p = (auth_req_packet *) pkt; 990 auth_res_packet *p = (auth_res_packet *) pkt;
788 991
789 slog (L_TRACE, "<<%d PT_AUTH_REQ(%d)", conf->id, p->initiate); 992 slog (L_TRACE, "<<%d PT_AUTH_RES", conf->id);
790 993
791 if (p->chk_config () && !strncmp (p->magic, MAGIC, 8)) 994 if (p->chk_config ())
792 { 995 {
793 if (p->prot_minor != PROTOCOL_MINOR) 996 if (p->prot_minor != PROTOCOL_MINOR)
794 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), 997 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."),
795 conf->nodename, (const char *)rsi, 998 conf->nodename, (const char *)rsi,
796 PROTOCOL_MINOR, conf->nodename, p->prot_minor); 999 PROTOCOL_MINOR, conf->nodename, p->prot_minor);
797 1000
798 if (p->initiate)
799 send_auth_request (rsi, false);
800
801 rsachallenge k; 1001 rsachallenge chg;
802 1002
803 if (0 > RSA_private_decrypt (sizeof (p->encr), 1003 if (!rsa_cache.find (p->id, chg))
804 (unsigned char *)&p->encr, (unsigned char *)&k, 1004 {
805 ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING)) 1005 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); 1006 conf->nodename, (const char *)rsi);
1007 break;
1008 }
808 else 1009 else
809 { 1010 {
810 retry_cnt = 0; 1011 crypto_ctx *cctx = new crypto_ctx (chg, 0);
811 establish_connection.set (NOW + 8); //? ;) 1012
812 keepalive.reset (); 1013 if (!p->hmac_chk (cctx))
1014 {
1015 slog (L_ERR, _("%s(%s): hmac authentication error on auth response, received invalid packet\n"
1016 "could be an attack, or just corruption or a synchronization error"),
1017 conf->nodename, (const char *)rsi);
1018 break;
1019 }
813 rekey.reset (); 1020 else
1021 {
1022 rsaresponse h;
814 1023
1024 rsa_hash (p->id, chg, h);
1025
1026 if (!memcmp ((u8 *)&h, (u8 *)p->response, sizeof h))
1027 {
1028 prot_minor = p->prot_minor;
1029
1030 delete ictx; ictx = cctx;
1031
1032 iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid
1033
1034 si = rsi;
1035 protocol = rsi.prot;
1036
1037 connection_established ();
1038
1039 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"),
1040 conf->nodename, (const char *)rsi,
1041 p->prot_major, p->prot_minor);
1042
1043 if (::conf.script_node_up)
1044 run_script (run_script_cb (this, &connection::script_node_up), false);
1045
1046 break;
1047 }
1048 else
1049 slog (L_ERR, _("%s(%s): sent and received challenge do not match"),
1050 conf->nodename, (const char *)rsi);
1051 }
1052
815 delete ictx; 1053 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 } 1054 }
828 } 1055 }
829
830 send_reset (rsi);
831 } 1056 }
832 1057
1058 send_reset (rsi);
833 break; 1059 break;
834 1060
835 case vpn_packet::PT_AUTH_RES: 1061 case vpn_packet::PT_DATA_COMPRESSED:
836 { 1062#if !ENABLE_COMPRESSION
837 auth_res_packet *p = (auth_res_packet *) pkt; 1063 send_reset (rsi);
1064 break;
1065#endif
838 1066
839 slog (L_TRACE, "<<%d PT_AUTH_RES", conf->id); 1067 case vpn_packet::PT_DATA_UNCOMPRESSED:
840 1068
841 if (p->chk_config ()) 1069 if (ictx && octx)
842 { 1070 {
843 if (p->prot_minor != PROTOCOL_MINOR) 1071 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 1072
848 rsachallenge chg; 1073 if (!p->hmac_chk (ictx))
849 1074 slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n"
850 if (!rsa_cache.find (p->id, chg)) 1075 "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); 1076 conf->nodename, (const char *)rsi);
853 else 1077 else
854 { 1078 {
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 1079 u32 seqno;
1080 tap_packet *d = p->unpack (this, seqno);
1081
1082 if (iseqno.recv_ok (seqno))
862 { 1083 {
863 rsaresponse h; 1084 vpn->tap->send (d);
864 1085
865 rsa_hash (p->id, chg, h); 1086 if (si != rsi)
866
867 if (!memcmp ((u8 *)&h, (u8 *)p->response, sizeof h))
868 { 1087 {
869 prot_minor = p->prot_minor; 1088 // 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; 1089 si = rsi;
876 1090
877 rekey.set (NOW + ::conf.rekey); 1091 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, 1092 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 } 1093 }
899 else
900 slog (L_ERR, _("%s(%s): sent and received challenge do not match"),
901 conf->nodename, (const char *)rsi);
902 } 1094 }
903 1095
904 delete cctx; 1096 delete d;
1097 break;
905 } 1098 }
906 } 1099 }
907 }
908 1100
909 send_reset (rsi); 1101 send_reset (rsi);
910 break; 1102 break;
911 1103
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: 1104 case vpn_packet::PT_CONNECT_REQ:
962 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) 1105 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx))
963 { 1106 {
964 connect_req_packet *p = (connect_req_packet *) pkt; 1107 connect_req_packet *p = (connect_req_packet *) pkt;
965 1108
966 assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything 1109 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]; 1110 connection *c = vpn->conns[p->id - 1];
1111 conf->protocols = p->protocols;
969 1112
970 slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]\n", 1113 slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]\n",
971 conf->id, p->id, c->ictx && c->octx); 1114 conf->id, p->id, c->ictx && c->octx);
972 1115
973 if (c->ictx && c->octx) 1116 if (c->ictx && c->octx)
974 { 1117 {
975 // send connect_info packets to both sides, in case one is 1118 // send connect_info packets to both sides, in case one is
976 // behind a nat firewall (or both ;) 1119 // behind a nat firewall (or both ;)
977 c->send_connect_info (conf->id, si, conf->protocols); 1120 c->send_connect_info (conf->id, si, conf->protocols);
978 send_connect_info (c->conf->id, c->si, c->conf->protocols); 1121 send_connect_info (c->conf->id, c->si, c->conf->protocols);
979 } 1122 }
1123 else
1124 c->establish_connection ();
980 } 1125 }
981 1126
982 break; 1127 break;
983 1128
984 case vpn_packet::PT_CONNECT_INFO: 1129 case vpn_packet::PT_CONNECT_INFO:
985 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) 1130 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx))
986 { 1131 {
987 connect_info_packet *p = (connect_info_packet *) pkt; 1132 connect_info_packet *p = (connect_info_packet *)pkt;
988 1133
989 assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything 1134 if (p->id > 0 && p->id <= vpn->conns.size ()) // hmac-auth does not mean we accept anything
990 conf->protocols = p->protocols; 1135 {
991 connection *c = vpn->conns[p->id - 1]; 1136 connection *c = vpn->conns[p->id - 1];
992 1137
1138 c->conf->protocols = p->protocols;
1139 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf));
1140 p->si.upgrade_protocol (protocol, c->conf);
1141
993 slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", 1142 slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)",
994 conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); 1143 conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx);
995 1144
1145 const sockinfo &dsi = forward_si (p->si);
1146
1147 if (dsi.valid ())
996 c->send_auth_request (p->si, true); 1148 c->send_auth_request (dsi, true);
1149 }
997 } 1150 }
998 1151
999 break; 1152 break;
1000 1153
1001 default: 1154 default:
1002 send_reset (rsi); 1155 send_reset (rsi);
1003 break; 1156 break;
1004
1005 } 1157 }
1006} 1158}
1007 1159
1008void connection::keepalive_cb (tstamp &ts) 1160void connection::keepalive_cb (time_watcher &w)
1009{ 1161{
1010 if (NOW >= last_activity + ::conf.keepalive + 30) 1162 if (NOW >= last_activity + ::conf.keepalive + 30)
1011 { 1163 {
1012 reset_connection (); 1164 reset_connection ();
1013 establish_connection (); 1165 establish_connection ();
1014 } 1166 }
1015 else if (NOW < last_activity + ::conf.keepalive) 1167 else if (NOW < last_activity + ::conf.keepalive)
1016 ts = last_activity + ::conf.keepalive; 1168 w.start (last_activity + ::conf.keepalive);
1017 else if (conf->connectmode != conf_node::C_ONDEMAND 1169 else if (conf->connectmode != conf_node::C_ONDEMAND
1018 || THISNODE->connectmode != conf_node::C_ONDEMAND) 1170 || THISNODE->connectmode != conf_node::C_ONDEMAND)
1019 { 1171 {
1020 send_ping (si); 1172 send_ping (si);
1021 ts = NOW + 5; 1173 w.start (NOW + 5);
1022 } 1174 }
1175 else if (NOW < last_activity + ::conf.keepalive + 10)
1176 // hold ondemand connections implicitly a few seconds longer
1177 // should delete octx, though, or something like that ;)
1178 w.start (last_activity + ::conf.keepalive + 10);
1023 else 1179 else
1024 reset_connection (); 1180 reset_connection ();
1025} 1181}
1026 1182
1027void connection::connect_request (int id) 1183void connection::send_connect_request (int id)
1028{ 1184{
1029 connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); 1185 connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols);
1030 1186
1031 slog (L_TRACE, ">>%d PT_CONNECT_REQ(%d)", conf->id, id); 1187 slog (L_TRACE, ">>%d PT_CONNECT_REQ(%d)", conf->id, id);
1032 p->hmac_set (octx); 1188 p->hmac_set (octx);
1035 delete p; 1191 delete p;
1036} 1192}
1037 1193
1038void connection::script_node () 1194void connection::script_node ()
1039{ 1195{
1040 vpn->script_if_up (0); 1196 vpn->script_if_up ();
1041 1197
1042 char *env; 1198 char *env;
1043 asprintf (&env, "DESTID=%d", conf->id); putenv (env); 1199 asprintf (&env, "DESTID=%d", conf->id); putenv (env);
1044 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); 1200 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env);
1045 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); 1201 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env);
1046 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); 1202 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env);
1047} 1203}
1048 1204
1049const char *connection::script_node_up (int) 1205const char *connection::script_node_up ()
1050{ 1206{
1051 script_node (); 1207 script_node ();
1052 1208
1053 putenv ("STATE=up"); 1209 putenv ("STATE=up");
1054 1210
1055 return ::conf.script_node_up ? ::conf.script_node_up : "node-up"; 1211 return ::conf.script_node_up ? ::conf.script_node_up : "node-up";
1056} 1212}
1057 1213
1058const char *connection::script_node_down (int) 1214const char *connection::script_node_down ()
1059{ 1215{
1060 script_node (); 1216 script_node ();
1061 1217
1062 putenv ("STATE=down"); 1218 putenv ("STATE=down");
1063 1219
1064 return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; 1220 return ::conf.script_node_up ? ::conf.script_node_down : "node-down";
1065} 1221}
1066 1222
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_) 1223connection::connection (struct vpn *vpn, conf_node *conf)
1078: vpn(vpn_) 1224: vpn(vpn), conf(conf)
1079, rekey (this, &connection::rekey_cb) 1225, rekey (this, &connection::rekey_cb)
1080, keepalive (this, &connection::keepalive_cb) 1226, keepalive (this, &connection::keepalive_cb)
1081, establish_connection (this, &connection::establish_connection_cb) 1227, establish_connection (this, &connection::establish_connection_cb)
1228#if ENABLE_DNS
1229, dns (0)
1230#endif
1082{ 1231{
1083 octx = ictx = 0; 1232 octx = ictx = 0;
1084 retry_cnt = 0; 1233 retry_cnt = 0;
1085 1234
1235 if (!conf->protocols) // make sure some protocol is enabled
1236 conf->protocols = PROT_UDPv4;
1237
1086 connectmode = conf_node::C_ALWAYS; // initial setting 1238 connectmode = conf_node::C_ALWAYS; // initial setting
1087 reset_connection (); 1239 reset_connection ();
1088} 1240}
1089 1241
1090connection::~connection () 1242connection::~connection ()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines