ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/connection.C
Revision: 1.12
Committed: Sun Apr 13 16:53:36 2003 UTC (21 years, 1 month ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: VPE_0_9
Changes since 1.11: +27 -10 lines
Log Message:
*** empty log message ***

File Contents

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