1 | /* |
1 | /* |
2 | connection.C -- manage a single connection |
2 | connection.C -- manage a single connection |
3 | Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> |
3 | Copyright (C) 2003-2008,2010,2011 Marc Lehmann <gvpe@schmorp.de> |
4 | |
4 | |
5 | This file is part of GVPE. |
5 | This file is part of GVPE. |
6 | |
6 | |
7 | GVPE is free software; you can redistribute it and/or modify it |
7 | GVPE is free software; you can redistribute it and/or modify it |
8 | under the terms of the GNU General Public License as published by the |
8 | under the terms of the GNU General Public License as published by the |
… | |
… | |
50 | |
50 | |
51 | #if !HAVE_RAND_PSEUDO_BYTES |
51 | #if !HAVE_RAND_PSEUDO_BYTES |
52 | # define RAND_pseudo_bytes RAND_bytes |
52 | # define RAND_pseudo_bytes RAND_bytes |
53 | #endif |
53 | #endif |
54 | |
54 | |
55 | #define MAGIC "vped\xbd\xc6\xdb\x82" // 8 bytes of magic |
55 | #define MAGIC_OLD "vped\xbd\xc6\xdb\x82" // 8 bytes of magic (still used in the protocol) |
|
|
56 | #define MAGIC "gvpe\xbd\xc6\xdb\x82" // 8 bytes of magic (understood but not generated) |
56 | |
57 | |
57 | #define ULTRA_FAST 1 |
58 | #define ULTRA_FAST 1 |
58 | #define HLOG 15 |
59 | #define HLOG 15 |
59 | #include "lzf/lzf.h" |
60 | #include "lzf/lzf.h" |
60 | #include "lzf/lzf_c.c" |
61 | #include "lzf/lzf_c.c" |
… | |
… | |
63 | ////////////////////////////////////////////////////////////////////////////// |
64 | ////////////////////////////////////////////////////////////////////////////// |
64 | |
65 | |
65 | static std::queue< std::pair<run_script_cb *, const char *> > rs_queue; |
66 | static std::queue< std::pair<run_script_cb *, const char *> > rs_queue; |
66 | static ev::child rs_child_ev; |
67 | static ev::child rs_child_ev; |
67 | |
68 | |
|
|
69 | namespace |
|
|
70 | { |
68 | void // c++ requires external linkage here, apparently :( |
71 | void // c++ requires external linkage here, apparently :( |
69 | rs_child_cb (ev::child &w, int revents) |
72 | rs_child_cb (ev::child &w, int revents) |
70 | { |
73 | { |
71 | w.stop (); |
74 | w.stop (); |
72 | |
75 | |
73 | if (rs_queue.empty ()) |
76 | if (rs_queue.empty ()) |
74 | return; |
77 | return; |
75 | |
78 | |
76 | pid_t pid = run_script (*rs_queue.front ().first, false); |
79 | pid_t pid = run_script (*rs_queue.front ().first, false); |
77 | if (pid) |
80 | if (pid) |
78 | { |
81 | { |
79 | w.set (pid); |
82 | w.set (pid); |
80 | w.start (); |
83 | w.start (); |
81 | } |
84 | } |
82 | else |
85 | else |
83 | slog (L_WARN, rs_queue.front ().second); |
86 | slog (L_WARN, rs_queue.front ().second); |
84 | |
87 | |
85 | delete rs_queue.front ().first; |
88 | delete rs_queue.front ().first; |
86 | rs_queue.pop (); |
89 | rs_queue.pop (); |
87 | } |
90 | } |
|
|
91 | }; |
88 | |
92 | |
89 | // despite the fancy name, this is quite a hack |
93 | // despite the fancy name, this is quite a hack |
90 | static void |
94 | static void |
91 | run_script_queued (run_script_cb *cb, const char *warnmsg) |
95 | run_script_queued (run_script_cb *cb, const char *warnmsg) |
92 | { |
96 | { |
… | |
… | |
190 | cleaner.set (RSA_TTL, RSA_TTL); |
194 | cleaner.set (RSA_TTL, RSA_TTL); |
191 | } |
195 | } |
192 | |
196 | |
193 | } rsa_cache; |
197 | } rsa_cache; |
194 | |
198 | |
|
|
199 | void |
195 | void rsa_cache::cleaner_cb (ev::timer &w, int revents) |
200 | rsa_cache::cleaner_cb (ev::timer &w, int revents) |
196 | { |
201 | { |
197 | if (empty ()) |
202 | if (empty ()) |
198 | w.stop (); |
203 | w.stop (); |
199 | else |
204 | else |
200 | { |
205 | { |
… | |
… | |
225 | delete p; |
230 | delete p; |
226 | |
231 | |
227 | delete [] queue; |
232 | delete [] queue; |
228 | } |
233 | } |
229 | |
234 | |
|
|
235 | void |
230 | void pkt_queue::expire_cb (ev::timer &w, int revents) |
236 | pkt_queue::expire_cb (ev::timer &w, int revents) |
231 | { |
237 | { |
232 | ev_tstamp expire = ev_now () - max_ttl; |
238 | ev_tstamp expire = ev_now () - max_ttl; |
233 | |
239 | |
234 | for (;;) |
240 | for (;;) |
235 | { |
241 | { |
… | |
… | |
246 | |
252 | |
247 | delete get (); |
253 | delete get (); |
248 | } |
254 | } |
249 | } |
255 | } |
250 | |
256 | |
|
|
257 | void |
251 | void pkt_queue::put (net_packet *p) |
258 | pkt_queue::put (net_packet *p) |
252 | { |
259 | { |
253 | ev_tstamp now = ev_now (); |
260 | ev_tstamp now = ev_now (); |
254 | |
261 | |
255 | // start expiry timer |
262 | // start expiry timer |
256 | if (empty ()) |
263 | if (empty ()) |
… | |
… | |
265 | queue[i].tstamp = now; |
272 | queue[i].tstamp = now; |
266 | |
273 | |
267 | i = ni; |
274 | i = ni; |
268 | } |
275 | } |
269 | |
276 | |
270 | net_packet *pkt_queue::get () |
277 | net_packet * |
|
|
278 | pkt_queue::get () |
271 | { |
279 | { |
272 | if (empty ()) |
280 | if (empty ()) |
273 | return 0; |
281 | return 0; |
274 | |
282 | |
275 | net_packet *p = queue[j].pkt; |
283 | net_packet *p = queue[j].pkt; |
… | |
… | |
299 | |
307 | |
300 | bool can (const sockinfo &si) { return can((u32)si.host); } |
308 | bool can (const sockinfo &si) { return can((u32)si.host); } |
301 | bool can (u32 host); |
309 | bool can (u32 host); |
302 | }; |
310 | }; |
303 | |
311 | |
304 | net_rate_limiter auth_rate_limiter, reset_rate_limiter; |
312 | static net_rate_limiter auth_rate_limiter, reset_rate_limiter; |
305 | |
313 | |
|
|
314 | bool |
306 | bool net_rate_limiter::can (u32 host) |
315 | net_rate_limiter::can (u32 host) |
307 | { |
316 | { |
308 | iterator i; |
317 | iterator i; |
309 | |
318 | |
310 | for (i = begin (); i != end (); ) |
319 | for (i = begin (); i != end (); ) |
311 | if (i->host == host) |
320 | if (i->host == host) |
… | |
… | |
358 | |
367 | |
359 | ///////////////////////////////////////////////////////////////////////////// |
368 | ///////////////////////////////////////////////////////////////////////////// |
360 | |
369 | |
361 | unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE]; |
370 | unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE]; |
362 | |
371 | |
|
|
372 | void |
363 | void hmac_packet::hmac_gen (crypto_ctx *ctx) |
373 | hmac_packet::hmac_gen (crypto_ctx *ctx) |
364 | { |
374 | { |
365 | unsigned int xlen; |
375 | unsigned int xlen; |
366 | |
376 | |
367 | HMAC_CTX *hctx = &ctx->hctx; |
377 | HMAC_CTX *hctx = &ctx->hctx; |
368 | |
378 | |
… | |
… | |
386 | hmac_gen (ctx); |
396 | hmac_gen (ctx); |
387 | |
397 | |
388 | return !memcmp (hmac, hmac_digest, HMACLENGTH); |
398 | return !memcmp (hmac, hmac_digest, HMACLENGTH); |
389 | } |
399 | } |
390 | |
400 | |
|
|
401 | void |
391 | void vpn_packet::set_hdr (ptype type_, unsigned int dst) |
402 | vpn_packet::set_hdr (ptype type_, unsigned int dst) |
392 | { |
403 | { |
393 | type = type_; |
404 | type = type_; |
394 | |
405 | |
395 | int src = THISNODE->id; |
406 | int src = THISNODE->id; |
396 | |
407 | |
… | |
… | |
401 | |
412 | |
402 | #define MAXVPNDATA (MAX_MTU - 6 - 6) |
413 | #define MAXVPNDATA (MAX_MTU - 6 - 6) |
403 | #define DATAHDR (sizeof (u32) + RAND_SIZE) |
414 | #define DATAHDR (sizeof (u32) + RAND_SIZE) |
404 | |
415 | |
405 | struct vpndata_packet : vpn_packet |
416 | struct vpndata_packet : vpn_packet |
|
|
417 | { |
|
|
418 | u8 data[MAXVPNDATA + DATAHDR]; // seqno |
|
|
419 | |
|
|
420 | void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno); |
|
|
421 | tap_packet *unpack (connection *conn, u32 &seqno); |
|
|
422 | |
|
|
423 | private: |
|
|
424 | const u32 data_hdr_size () const |
406 | { |
425 | { |
407 | u8 data[MAXVPNDATA + DATAHDR]; // seqno |
|
|
408 | |
|
|
409 | void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno); |
|
|
410 | tap_packet *unpack (connection *conn, u32 &seqno); |
|
|
411 | private: |
|
|
412 | |
|
|
413 | const u32 data_hdr_size () const |
|
|
414 | { |
|
|
415 | return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR; |
426 | return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR; |
416 | } |
|
|
417 | }; |
427 | } |
|
|
428 | }; |
418 | |
429 | |
419 | void |
430 | void |
420 | vpndata_packet::setup (connection *conn, int dst, u8 *d, u32 l, u32 seqno) |
431 | vpndata_packet::setup (connection *conn, int dst, u8 *d, u32 l, u32 seqno) |
421 | { |
432 | { |
422 | EVP_CIPHER_CTX *cctx = &conn->octx->cctx; |
433 | EVP_CIPHER_CTX *cctx = &conn->octx->cctx; |
… | |
… | |
424 | ptype type = PT_DATA_UNCOMPRESSED; |
435 | ptype type = PT_DATA_UNCOMPRESSED; |
425 | |
436 | |
426 | #if ENABLE_COMPRESSION |
437 | #if ENABLE_COMPRESSION |
427 | u8 cdata[MAX_MTU]; |
438 | u8 cdata[MAX_MTU]; |
428 | |
439 | |
429 | if (conn->features & ENABLE_COMPRESSION) |
440 | if (conn->features & FEATURE_COMPRESSION) |
430 | { |
441 | { |
431 | u32 cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); |
442 | u32 cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); |
432 | |
443 | |
433 | if (cl) |
444 | if (cl) |
434 | { |
445 | { |
… | |
… | |
561 | #endif |
572 | #endif |
562 | return f; |
573 | return f; |
563 | } |
574 | } |
564 | }; |
575 | }; |
565 | |
576 | |
|
|
577 | void |
566 | void config_packet::setup (ptype type, int dst) |
578 | config_packet::setup (ptype type, int dst) |
567 | { |
579 | { |
568 | prot_major = PROTOCOL_MAJOR; |
580 | prot_major = PROTOCOL_MAJOR; |
569 | prot_minor = PROTOCOL_MINOR; |
581 | prot_minor = PROTOCOL_MINOR; |
570 | randsize = RAND_SIZE; |
582 | randsize = RAND_SIZE; |
571 | hmaclen = HMACLENGTH; |
583 | hmaclen = HMACLENGTH; |
… | |
… | |
579 | |
591 | |
580 | len = sizeof (*this) - sizeof (net_packet); |
592 | len = sizeof (*this) - sizeof (net_packet); |
581 | set_hdr (type, dst); |
593 | set_hdr (type, dst); |
582 | } |
594 | } |
583 | |
595 | |
|
|
596 | bool |
584 | bool config_packet::chk_config () const |
597 | config_packet::chk_config () const |
585 | { |
598 | { |
586 | if (prot_major != PROTOCOL_MAJOR) |
599 | if (prot_major != PROTOCOL_MAJOR) |
587 | slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); |
600 | slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); |
588 | else if (randsize != RAND_SIZE) |
601 | else if (randsize != RAND_SIZE) |
589 | slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); |
602 | slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); |
… | |
… | |
613 | rsaencrdata encr; |
626 | rsaencrdata encr; |
614 | |
627 | |
615 | auth_req_packet (int dst, bool initiate_, u8 protocols_) |
628 | auth_req_packet (int dst, bool initiate_, u8 protocols_) |
616 | { |
629 | { |
617 | config_packet::setup (PT_AUTH_REQ, dst); |
630 | config_packet::setup (PT_AUTH_REQ, dst); |
618 | strncpy (magic, MAGIC, 8); |
631 | strncpy (magic, MAGIC_OLD, 8); |
619 | initiate = !!initiate_; |
632 | initiate = !!initiate_; |
620 | protocols = protocols_; |
633 | protocols = protocols_; |
621 | |
634 | |
622 | len = sizeof (*this) - sizeof (net_packet); |
635 | len = sizeof (*this) - sizeof (net_packet); |
623 | } |
636 | } |
… | |
… | |
672 | ///////////////////////////////////////////////////////////////////////////// |
685 | ///////////////////////////////////////////////////////////////////////////// |
673 | |
686 | |
674 | void |
687 | void |
675 | connection::connection_established () |
688 | connection::connection_established () |
676 | { |
689 | { |
677 | slog (L_TRACE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx); |
690 | slog (L_NOISE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx); |
678 | |
691 | |
679 | if (ictx && octx) |
692 | if (ictx && octx) |
680 | { |
693 | { |
681 | // make sure rekeying timeouts are slightly asymmetric |
694 | // make sure rekeying timeouts are slightly asymmetric |
682 | ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); |
695 | ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); |
… | |
… | |
696 | { |
709 | { |
697 | if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); |
710 | if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); |
698 | delete p; |
711 | delete p; |
699 | } |
712 | } |
700 | } |
713 | } |
|
|
714 | |
|
|
715 | vpn->connection_established (this); |
701 | } |
716 | } |
702 | else |
717 | else |
703 | { |
718 | { |
704 | retry_cnt = 0; |
719 | retry_cnt = 0; |
705 | establish_connection.start (5); |
720 | establish_connection.start (5); |
… | |
… | |
709 | } |
724 | } |
710 | |
725 | |
711 | void |
726 | void |
712 | connection::reset_si () |
727 | connection::reset_si () |
713 | { |
728 | { |
|
|
729 | if (vpn->can_direct (THISNODE, conf)) |
714 | protocol = best_protocol (THISNODE->protocols & conf->protocols); |
730 | protocol = best_protocol (THISNODE->protocols & conf->connectable_protocols ()); |
715 | |
731 | else |
716 | // mask out endpoints we can't connect to |
|
|
717 | if (!conf->udp_port) protocol &= ~PROT_UDPv4; |
|
|
718 | if (!conf->tcp_port) protocol &= ~PROT_TCPv4; |
|
|
719 | if (!conf->dns_port) protocol &= ~PROT_DNSv4; |
|
|
720 | |
|
|
721 | if (protocol |
|
|
722 | && (!conf->can_direct (THISNODE) |
|
|
723 | || !THISNODE->can_direct (conf))) |
|
|
724 | { |
732 | { |
725 | slog (L_DEBUG, _("%s: direct connection denied"), conf->nodename); |
733 | slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename); |
726 | protocol = 0; |
734 | protocol = 0; |
727 | } |
735 | } |
728 | |
736 | |
729 | si.set (conf, protocol); |
737 | si.set (conf, protocol); |
|
|
738 | |
|
|
739 | is_direct = si.valid (); |
730 | } |
740 | } |
731 | |
741 | |
732 | // ensure sockinfo is valid, forward if necessary |
742 | // ensure sockinfo is valid, forward if necessary |
733 | const sockinfo & |
743 | const sockinfo & |
734 | connection::forward_si (const sockinfo &si) const |
744 | connection::forward_si (const sockinfo &si) const |
735 | { |
745 | { |
736 | if (!si.valid ()) |
746 | if (!si.valid ()) |
737 | { |
747 | { |
738 | connection *r = vpn->find_router (); |
748 | connection *r = vpn->find_router_for (this); |
739 | |
749 | |
740 | if (r) |
750 | if (r) |
741 | { |
751 | { |
742 | slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s (%s)"), |
752 | slog (L_DEBUG, _("%s: no common protocol, trying to route through %s."), |
743 | conf->nodename, r->conf->nodename, (const char *)r->si); |
753 | conf->nodename, r->conf->nodename); |
744 | return r->si; |
754 | return r->si; |
745 | } |
755 | } |
746 | else |
756 | else |
747 | slog (L_DEBUG, _("%s: node unreachable, no common protocol, no router"), |
757 | slog (L_DEBUG, _("%s: node unreachable, no common protocol or no router available."), |
748 | conf->nodename); |
758 | conf->nodename); |
749 | } |
759 | } |
750 | |
760 | |
751 | return si; |
761 | return si; |
752 | } |
762 | } |
… | |
… | |
762 | connection::send_ping (const sockinfo &si, u8 pong) |
772 | connection::send_ping (const sockinfo &si, u8 pong) |
763 | { |
773 | { |
764 | ping_packet *pkt = new ping_packet; |
774 | ping_packet *pkt = new ping_packet; |
765 | |
775 | |
766 | pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING); |
776 | pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING); |
|
|
777 | |
|
|
778 | slog (L_TRACE, "%s << %s [%s]", conf->nodename, pong ? "PT_PONG" : "PT_PING", (const char *)si); |
|
|
779 | |
767 | send_vpn_packet (pkt, si, IPTOS_LOWDELAY); |
780 | send_vpn_packet (pkt, si, IPTOS_LOWDELAY); |
768 | |
781 | |
769 | delete pkt; |
782 | delete pkt; |
770 | } |
783 | } |
771 | |
784 | |
… | |
… | |
790 | |
803 | |
791 | rsachallenge chg; |
804 | rsachallenge chg; |
792 | rsa_cache.gen (pkt->id, chg); |
805 | rsa_cache.gen (pkt->id, chg); |
793 | rsa_encrypt (conf->rsa_key, chg, pkt->encr); |
806 | rsa_encrypt (conf->rsa_key, chg, pkt->encr); |
794 | |
807 | |
795 | slog (L_TRACE, ">>%d PT_AUTH_REQ [%s]", conf->id, (const char *)si); |
808 | slog (L_TRACE, "%s << PT_AUTH_REQ [%s]", conf->nodename, (const char *)si); |
796 | |
809 | |
797 | send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly |
810 | send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly |
798 | |
811 | |
799 | delete pkt; |
812 | delete pkt; |
800 | } |
813 | } |
… | |
… | |
808 | |
821 | |
809 | rsa_hash (id, chg, pkt->response); |
822 | rsa_hash (id, chg, pkt->response); |
810 | |
823 | |
811 | pkt->hmac_set (octx); |
824 | pkt->hmac_set (octx); |
812 | |
825 | |
813 | slog (L_TRACE, ">>%d PT_AUTH_RES [%s]", conf->id, (const char *)si); |
826 | slog (L_TRACE, "%s << PT_AUTH_RES [%s]", conf->nodename, (const char *)si); |
814 | |
827 | |
815 | send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly |
828 | send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly |
816 | |
829 | |
817 | delete pkt; |
830 | delete pkt; |
818 | } |
831 | } |
819 | |
832 | |
820 | void |
833 | void |
821 | connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) |
834 | connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) |
822 | { |
835 | { |
823 | slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)", |
836 | slog (L_TRACE, "%s << PT_CONNECT_INFO(%s,%s,p%02x)", conf->nodename, |
824 | conf->id, rid, (const char *)rsi); |
837 | vpn->conns[rid - 1]->conf->nodename, (const char *)rsi, |
|
|
838 | conf->protocols); |
825 | |
839 | |
826 | connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); |
840 | connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); |
827 | |
841 | |
828 | r->hmac_set (octx); |
842 | r->hmac_set (octx); |
829 | send_vpn_packet (r, si); |
843 | send_vpn_packet (r, si); |
… | |
… | |
832 | } |
846 | } |
833 | |
847 | |
834 | inline void |
848 | inline void |
835 | connection::establish_connection_cb (ev::timer &w, int revents) |
849 | connection::establish_connection_cb (ev::timer &w, int revents) |
836 | { |
850 | { |
837 | if (!ictx |
851 | if (!(ictx && octx) |
838 | && conf != THISNODE |
852 | && conf != THISNODE |
839 | && connectmode != conf_node::C_NEVER |
853 | && connectmode != conf_node::C_NEVER |
840 | && connectmode != conf_node::C_DISABLED |
854 | && connectmode != conf_node::C_DISABLED |
841 | && !w.is_active ()) |
855 | && !w.is_active ()) |
842 | { |
856 | { |
… | |
… | |
856 | |
870 | |
857 | reset_si (); |
871 | reset_si (); |
858 | |
872 | |
859 | bool slow = si.prot & PROT_SLOW; |
873 | bool slow = si.prot & PROT_SLOW; |
860 | |
874 | |
861 | if (si.prot && !si.host) |
875 | if (si.prot && !si.host && vpn->can_direct (THISNODE, conf)) |
862 | { |
876 | { |
863 | slog (L_TRACE, _("%s: connection request (indirect)"), conf->nodename); |
|
|
864 | /*TODO*/ /* start the timer so we don't recurse endlessly */ |
877 | /*TODO*/ /* start the timer so we don't recurse endlessly */ |
865 | w.start (1); |
878 | w.start (1); |
866 | vpn->send_connect_request (conf->id); |
879 | vpn->send_connect_request (this); |
867 | } |
880 | } |
868 | else |
881 | else |
869 | { |
882 | { |
870 | slog (L_TRACE, _("%s: connection request (direct)"), conf->nodename, !!ictx, !!octx); |
883 | if (si.valid ()) |
|
|
884 | slog (L_DEBUG, _("%s: sending direct connection request to %s."), |
|
|
885 | conf->nodename, (const char *)si); |
871 | |
886 | |
872 | const sockinfo &dsi = forward_si (si); |
887 | const sockinfo &dsi = forward_si (si); |
873 | |
888 | |
874 | slow = slow || (dsi.prot & PROT_SLOW); |
889 | slow = slow || (dsi.prot & PROT_SLOW); |
875 | |
890 | |
… | |
… | |
915 | dnsv4_reset_connection (); |
930 | dnsv4_reset_connection (); |
916 | #endif |
931 | #endif |
917 | |
932 | |
918 | si.host = 0; |
933 | si.host = 0; |
919 | |
934 | |
920 | last_activity = 0; |
935 | last_activity = 0.; |
|
|
936 | //last_si_change = 0.; |
921 | retry_cnt = 0; |
937 | retry_cnt = 0; |
922 | |
938 | |
923 | rekey.stop (); |
939 | rekey.stop (); |
924 | keepalive.stop (); |
940 | keepalive.stop (); |
925 | establish_connection.stop (); |
941 | establish_connection.stop (); |
… | |
… | |
932 | send_reset (si); |
948 | send_reset (si); |
933 | |
949 | |
934 | reset_connection (); |
950 | reset_connection (); |
935 | } |
951 | } |
936 | |
952 | |
|
|
953 | // poor-man's rekeying |
937 | inline void |
954 | inline void |
938 | connection::rekey_cb (ev::timer &w, int revents) |
955 | connection::rekey_cb (ev::timer &w, int revents) |
939 | { |
956 | { |
940 | reset_connection (); |
957 | reset_connection (); |
941 | establish_connection (); |
958 | establish_connection (); |
… | |
… | |
980 | data_queue.put (new tap_packet (*pkt)); |
997 | data_queue.put (new tap_packet (*pkt)); |
981 | post_inject_queue (); |
998 | post_inject_queue (); |
982 | } |
999 | } |
983 | } |
1000 | } |
984 | |
1001 | |
|
|
1002 | void |
985 | void connection::inject_vpn_packet (vpn_packet *pkt, int tos) |
1003 | connection::inject_vpn_packet (vpn_packet *pkt, int tos) |
986 | { |
1004 | { |
987 | if (ictx && octx) |
1005 | if (ictx && octx) |
988 | send_vpn_packet (pkt, si, tos); |
1006 | send_vpn_packet (pkt, si, tos); |
989 | else |
1007 | else |
990 | { |
1008 | { |
… | |
… | |
996 | void |
1014 | void |
997 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
1015 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
998 | { |
1016 | { |
999 | last_activity = ev_now (); |
1017 | last_activity = ev_now (); |
1000 | |
1018 | |
1001 | slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
1019 | slog (L_NOISE, "%s >> received packet type %d from %d to %d.", |
1002 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
1020 | conf->nodename, pkt->typ (), pkt->src (), pkt->dst ()); |
1003 | |
1021 | |
1004 | if (connectmode == conf_node::C_DISABLED) |
1022 | if (connectmode == conf_node::C_DISABLED) |
1005 | return; |
1023 | return; |
1006 | |
1024 | |
1007 | switch (pkt->typ ()) |
1025 | switch (pkt->typ ()) |
1008 | { |
1026 | { |
1009 | case vpn_packet::PT_PING: |
1027 | case vpn_packet::PT_PING: |
|
|
1028 | slog (L_TRACE, "%s >> PT_PING", conf->nodename); |
|
|
1029 | |
1010 | // we send pings instead of auth packets after some retries, |
1030 | // we send pings instead of auth packets after some retries, |
1011 | // so reset the retry counter and establish a connection |
1031 | // so reset the retry counter and establish a connection |
1012 | // when we receive a ping. |
1032 | // when we receive a ping. |
1013 | if (!ictx) |
1033 | if (!ictx) |
1014 | { |
1034 | { |
1015 | if (auth_rate_limiter.can (rsi)) |
1035 | if (auth_rate_limiter.can (rsi)) |
1016 | send_auth_request (rsi, true); |
1036 | send_auth_request (rsi, true); |
1017 | } |
1037 | } |
1018 | else |
1038 | else |
|
|
1039 | // we would love to change the socket address here, but ping's aren't |
|
|
1040 | // authenticated, so we best ignore it. |
1019 | send_ping (rsi, 1); // pong |
1041 | send_ping (rsi, 1); // pong |
1020 | |
1042 | |
1021 | break; |
1043 | break; |
1022 | |
1044 | |
1023 | case vpn_packet::PT_PONG: |
1045 | case vpn_packet::PT_PONG: |
|
|
1046 | slog (L_TRACE, "%s >> PT_PONG", conf->nodename); |
|
|
1047 | |
|
|
1048 | // a PONG might mean that the other side doesn't really know |
|
|
1049 | // about our desire for communication. |
|
|
1050 | establish_connection (); |
1024 | break; |
1051 | break; |
1025 | |
1052 | |
1026 | case vpn_packet::PT_RESET: |
1053 | case vpn_packet::PT_RESET: |
1027 | { |
1054 | { |
1028 | reset_connection (); |
1055 | reset_connection (); |
1029 | |
1056 | |
1030 | config_packet *p = (config_packet *) pkt; |
1057 | config_packet *p = (config_packet *) pkt; |
1031 | |
1058 | |
1032 | if (!p->chk_config ()) |
1059 | if (!p->chk_config ()) |
1033 | { |
1060 | { |
1034 | slog (L_WARN, _("%s(%s): protocol mismatch, disabling node"), |
1061 | slog (L_WARN, _("%s(%s): protocol mismatch, disabling node."), |
1035 | conf->nodename, (const char *)rsi); |
1062 | conf->nodename, (const char *)rsi); |
1036 | connectmode = conf_node::C_DISABLED; |
1063 | connectmode = conf_node::C_DISABLED; |
1037 | } |
1064 | } |
1038 | else if (connectmode == conf_node::C_ALWAYS) |
1065 | else if (connectmode == conf_node::C_ALWAYS) |
1039 | establish_connection (); |
1066 | establish_connection (); |
… | |
… | |
1041 | break; |
1068 | break; |
1042 | |
1069 | |
1043 | case vpn_packet::PT_AUTH_REQ: |
1070 | case vpn_packet::PT_AUTH_REQ: |
1044 | if (auth_rate_limiter.can (rsi)) |
1071 | if (auth_rate_limiter.can (rsi)) |
1045 | { |
1072 | { |
1046 | auth_req_packet *p = (auth_req_packet *) pkt; |
1073 | auth_req_packet *p = (auth_req_packet *)pkt; |
1047 | |
1074 | |
1048 | slog (L_TRACE, "<<%d PT_AUTH_REQ(%d)", conf->id, p->initiate); |
1075 | slog (L_TRACE, "%s >> PT_AUTH_REQ(%s,p%02x,f%02x)", |
|
|
1076 | conf->nodename, p->initiate ? "initiate" : "reply", |
|
|
1077 | p->protocols, p->features); |
1049 | |
1078 | |
1050 | if (p->chk_config () && !strncmp (p->magic, MAGIC, 8)) |
1079 | if (p->chk_config () |
|
|
1080 | && (!memcmp (p->magic, MAGIC_OLD, 8) || !memcmp (p->magic, MAGIC, 8))) |
1051 | { |
1081 | { |
1052 | if (p->prot_minor != PROTOCOL_MINOR) |
1082 | if (p->prot_minor != PROTOCOL_MINOR) |
1053 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
1083 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
1054 | conf->nodename, (const char *)rsi, |
1084 | conf->nodename, (const char *)rsi, |
1055 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
1085 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
… | |
… | |
1081 | |
1111 | |
1082 | break; |
1112 | break; |
1083 | } |
1113 | } |
1084 | } |
1114 | } |
1085 | else |
1115 | else |
1086 | slog (L_WARN, _("%s(%s): protocol mismatch"), |
1116 | slog (L_WARN, _("%s(%s): protocol mismatch."), |
1087 | conf->nodename, (const char *)rsi); |
1117 | conf->nodename, (const char *)rsi); |
1088 | |
1118 | |
1089 | send_reset (rsi); |
1119 | send_reset (rsi); |
1090 | } |
1120 | } |
1091 | |
1121 | |
1092 | break; |
1122 | break; |
1093 | |
1123 | |
1094 | case vpn_packet::PT_AUTH_RES: |
1124 | case vpn_packet::PT_AUTH_RES: |
1095 | { |
1125 | { |
1096 | auth_res_packet *p = (auth_res_packet *) pkt; |
1126 | auth_res_packet *p = (auth_res_packet *)pkt; |
1097 | |
1127 | |
1098 | slog (L_TRACE, "<<%d PT_AUTH_RES", conf->id); |
1128 | slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename); |
1099 | |
1129 | |
1100 | if (p->chk_config ()) |
1130 | if (p->chk_config ()) |
1101 | { |
1131 | { |
1102 | if (p->prot_minor != PROTOCOL_MINOR) |
1132 | if (p->prot_minor != PROTOCOL_MINOR) |
1103 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
1133 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
… | |
… | |
1106 | |
1136 | |
1107 | rsachallenge chg; |
1137 | rsachallenge chg; |
1108 | |
1138 | |
1109 | if (!rsa_cache.find (p->id, chg)) |
1139 | if (!rsa_cache.find (p->id, chg)) |
1110 | { |
1140 | { |
1111 | slog (L_ERR, _("%s(%s): unrequested auth response ignored"), |
1141 | slog (L_ERR, _("%s(%s): unrequested auth response, ignoring."), |
1112 | conf->nodename, (const char *)rsi); |
1142 | conf->nodename, (const char *)rsi); |
1113 | break; |
1143 | break; |
1114 | } |
1144 | } |
1115 | else |
1145 | else |
1116 | { |
1146 | { |
1117 | crypto_ctx *cctx = new crypto_ctx (chg, 0); |
1147 | crypto_ctx *cctx = new crypto_ctx (chg, 0); |
1118 | |
1148 | |
1119 | if (!p->hmac_chk (cctx)) |
1149 | if (!p->hmac_chk (cctx)) |
1120 | { |
1150 | { |
1121 | slog (L_ERR, _("%s(%s): hmac authentication error on auth response, received invalid packet\n" |
1151 | slog (L_ERR, _("%s(%s): hmac authentication error on auth response, received invalid packet\n" |
1122 | "could be an attack, or just corruption or a synchronization error"), |
1152 | "could be an attack, or just corruption or a synchronization error."), |
1123 | conf->nodename, (const char *)rsi); |
1153 | conf->nodename, (const char *)rsi); |
1124 | break; |
1154 | break; |
1125 | } |
1155 | } |
1126 | else |
1156 | else |
1127 | { |
1157 | { |
… | |
… | |
1138 | iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid |
1168 | iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid |
1139 | |
1169 | |
1140 | si = rsi; |
1170 | si = rsi; |
1141 | protocol = rsi.prot; |
1171 | protocol = rsi.prot; |
1142 | |
1172 | |
|
|
1173 | slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."), |
|
|
1174 | conf->nodename, (const char *)rsi, |
|
|
1175 | is_direct ? "direct" : "forwarded", |
|
|
1176 | p->prot_major, p->prot_minor); |
|
|
1177 | |
1143 | connection_established (); |
1178 | connection_established (); |
1144 | |
|
|
1145 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
|
|
1146 | conf->nodename, (const char *)rsi, |
|
|
1147 | p->prot_major, p->prot_minor); |
|
|
1148 | |
1179 | |
1149 | if (::conf.script_node_up) |
1180 | if (::conf.script_node_up) |
1150 | { |
1181 | { |
1151 | run_script_cb *cb = new run_script_cb; |
1182 | run_script_cb *cb = new run_script_cb; |
1152 | cb->set<connection, &connection::script_node_up> (this); |
1183 | cb->set<connection, &connection::script_node_up> (this); |
… | |
… | |
1154 | } |
1185 | } |
1155 | |
1186 | |
1156 | break; |
1187 | break; |
1157 | } |
1188 | } |
1158 | else |
1189 | else |
1159 | slog (L_ERR, _("%s(%s): sent and received challenge do not match"), |
1190 | slog (L_ERR, _("%s(%s): sent and received challenge do not match."), |
1160 | conf->nodename, (const char *)rsi); |
1191 | conf->nodename, (const char *)rsi); |
1161 | } |
1192 | } |
1162 | |
1193 | |
1163 | delete cctx; |
1194 | delete cctx; |
1164 | } |
1195 | } |
… | |
… | |
1180 | { |
1211 | { |
1181 | vpndata_packet *p = (vpndata_packet *)pkt; |
1212 | vpndata_packet *p = (vpndata_packet *)pkt; |
1182 | |
1213 | |
1183 | if (!p->hmac_chk (ictx)) |
1214 | if (!p->hmac_chk (ictx)) |
1184 | slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" |
1215 | slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" |
1185 | "could be an attack, or just corruption or a synchronization error"), |
1216 | "could be an attack, or just corruption or a synchronization error."), |
1186 | conf->nodename, (const char *)rsi); |
1217 | conf->nodename, (const char *)rsi); |
1187 | else |
1218 | else |
1188 | { |
1219 | { |
1189 | u32 seqno; |
1220 | u32 seqno; |
1190 | tap_packet *d = p->unpack (this, seqno); |
1221 | tap_packet *d = p->unpack (this, seqno); |
|
|
1222 | int seqclass = iseqno.seqno_classify (seqno); |
1191 | |
1223 | |
1192 | if (iseqno.recv_ok (seqno)) |
1224 | if (seqclass == 0) // ok |
1193 | { |
1225 | { |
1194 | vpn->tap->send (d); |
1226 | vpn->tap->send (d); |
1195 | |
1227 | |
1196 | if (si != rsi) |
1228 | if (si != rsi) |
1197 | { |
1229 | { |
1198 | // fast re-sync on source address changes, useful especially for tcp/ip |
1230 | // fast re-sync on source address changes, useful especially for tcp/ip |
|
|
1231 | //if (last_si_change < ev_now () + 5.) |
1199 | si = rsi; |
1232 | // { |
1200 | |
|
|
1201 | slog (L_INFO, _("%s(%s): socket address changed to %s"), |
1233 | slog (L_INFO, _("%s(%s): changing socket address to %s."), |
1202 | conf->nodename, (const char *)si, (const char *)rsi); |
1234 | conf->nodename, (const char *)si, (const char *)rsi); |
|
|
1235 | |
|
|
1236 | si = rsi; |
|
|
1237 | |
|
|
1238 | if (::conf.script_node_change) |
|
|
1239 | { |
|
|
1240 | run_script_cb *cb = new run_script_cb; |
|
|
1241 | cb->set<connection, &connection::script_node_change> (this); |
|
|
1242 | run_script_queued (cb, _("node-change command execution failed, continuing.")); |
|
|
1243 | } |
|
|
1244 | |
|
|
1245 | // } |
|
|
1246 | //else |
|
|
1247 | // slog (L_INFO, _("%s(%s): accepted packet from %s, not (yet) redirecting traffic."), |
|
|
1248 | // conf->nodename, (const char *)si, (const char *)rsi); |
1203 | } |
1249 | } |
|
|
1250 | } |
|
|
1251 | else if (seqclass == 1) // far history |
|
|
1252 | slog (L_ERR, _("received very old packet (received %08lx, expected %08lx). " |
|
|
1253 | "possible replay attack, or just packet duplication/delay, ignoring."), seqno, iseqno.seq + 1); |
|
|
1254 | else if (seqclass == 2) // in-window duplicate, happens often on wireless |
|
|
1255 | slog (L_DEBUG, _("received recent duplicated packet (received %08lx, expected %08lx). " |
|
|
1256 | "possible replay attack, or just packet duplication, ignoring."), seqno, iseqno.seq + 1); |
|
|
1257 | else if (seqclass == 3) // reset |
|
|
1258 | { |
|
|
1259 | slog (L_ERR, _("received out-of-sync (far future) packet (received %08lx, expected %08lx). " |
|
|
1260 | "probably just massive packet loss, sending reset."), seqno, iseqno.seq + 1); |
|
|
1261 | send_reset (rsi); |
1204 | } |
1262 | } |
1205 | |
1263 | |
1206 | delete d; |
1264 | delete d; |
1207 | break; |
1265 | break; |
1208 | } |
1266 | } |
… | |
… | |
1212 | break; |
1270 | break; |
1213 | |
1271 | |
1214 | case vpn_packet::PT_CONNECT_REQ: |
1272 | case vpn_packet::PT_CONNECT_REQ: |
1215 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1273 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1216 | { |
1274 | { |
1217 | connect_req_packet *p = (connect_req_packet *) pkt; |
1275 | connect_req_packet *p = (connect_req_packet *)pkt; |
1218 | |
1276 | |
1219 | if (p->id > 0 && p->id <= vpn->conns.size ()) |
1277 | if (p->id > 0 && p->id <= vpn->conns.size ()) |
1220 | { |
1278 | { |
1221 | connection *c = vpn->conns[p->id - 1]; |
1279 | connection *c = vpn->conns[p->id - 1]; |
1222 | conf->protocols = p->protocols; |
1280 | conf->protocols = p->protocols; |
1223 | |
1281 | |
1224 | slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]", |
1282 | slog (L_TRACE, "%s >> PT_CONNECT_REQ(%s,p%02x) [%d]", |
|
|
1283 | conf->nodename, vpn->conns[p->id - 1]->conf->nodename, |
|
|
1284 | p->protocols, |
1225 | conf->id, p->id, c->ictx && c->octx); |
1285 | c->ictx && c->octx); |
1226 | |
1286 | |
1227 | if (c->ictx && c->octx) |
1287 | if (c->ictx && c->octx) |
1228 | { |
1288 | { |
1229 | // send connect_info packets to both sides, in case one is |
1289 | // send connect_info packets to both sides, in case one is |
1230 | // behind a nat firewall (or both ;) |
1290 | // behind a nat firewall (or both ;) |
… | |
… | |
1253 | |
1313 | |
1254 | c->conf->protocols = p->protocols; |
1314 | c->conf->protocols = p->protocols; |
1255 | protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); |
1315 | protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); |
1256 | p->si.upgrade_protocol (protocol, c->conf); |
1316 | p->si.upgrade_protocol (protocol, c->conf); |
1257 | |
1317 | |
1258 | slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", |
1318 | slog (L_TRACE, "%s >> PT_CONNECT_INFO(%s,%s,p%02x) [%d]", |
1259 | conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); |
1319 | conf->nodename, |
|
|
1320 | vpn->conns[p->id - 1]->conf->nodename, |
|
|
1321 | (const char *)p->si, |
|
|
1322 | p->protocols, |
|
|
1323 | !c->ictx && !c->octx); |
1260 | |
1324 | |
1261 | const sockinfo &dsi = forward_si (p->si); |
1325 | const sockinfo &dsi = forward_si (p->si); |
1262 | |
1326 | |
1263 | if (dsi.valid ()) |
1327 | if (dsi.valid ()) |
1264 | c->send_auth_request (dsi, true); |
1328 | c->send_auth_request (dsi, true); |
… | |
… | |
1278 | } |
1342 | } |
1279 | |
1343 | |
1280 | inline void |
1344 | inline void |
1281 | connection::keepalive_cb (ev::timer &w, int revents) |
1345 | connection::keepalive_cb (ev::timer &w, int revents) |
1282 | { |
1346 | { |
1283 | if (ev_now () >= last_activity + ::conf.keepalive + 30) |
1347 | if (ev_now () >= last_activity + ::conf.keepalive + 15) |
1284 | { |
1348 | { |
1285 | reset_connection (); |
1349 | reset_connection (); |
1286 | establish_connection (); |
1350 | establish_connection (); |
1287 | } |
1351 | } |
1288 | else if (ev_now () < last_activity + ::conf.keepalive) |
1352 | else if (ev_now () < last_activity + ::conf.keepalive) |
1289 | w.start (last_activity + ::conf.keepalive - ev::now ()); |
1353 | w.start (last_activity + ::conf.keepalive - ev::now ()); |
1290 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1354 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1291 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1355 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1292 | { |
1356 | { |
1293 | send_ping (si); |
1357 | send_ping (si); |
1294 | w.start (5); |
1358 | w.start (3); |
1295 | } |
1359 | } |
1296 | else if (ev_now () < last_activity + ::conf.keepalive + 10) |
1360 | else if (ev_now () < last_activity + ::conf.keepalive + 10) |
1297 | // hold ondemand connections implicitly a few seconds longer |
1361 | // hold ondemand connections implicitly a few seconds longer |
1298 | // should delete octx, though, or something like that ;) |
1362 | // should delete octx, though, or something like that ;) |
1299 | w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); |
1363 | w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); |
1300 | else |
1364 | else |
1301 | reset_connection (); |
1365 | reset_connection (); |
1302 | } |
1366 | } |
1303 | |
1367 | |
|
|
1368 | void |
1304 | void connection::send_connect_request (int id) |
1369 | connection::send_connect_request (int id) |
1305 | { |
1370 | { |
1306 | connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); |
1371 | connect_req_packet *p = new connect_req_packet (conf->id, id, THISNODE->protocols); |
1307 | |
1372 | |
1308 | slog (L_TRACE, ">>%d PT_CONNECT_REQ(%d)", conf->id, id); |
1373 | slog (L_TRACE, "%s << PT_CONNECT_REQ(%s,p%02x)", |
|
|
1374 | conf->nodename, vpn->conns[id - 1]->conf->nodename, |
|
|
1375 | THISNODE->protocols); |
1309 | p->hmac_set (octx); |
1376 | p->hmac_set (octx); |
1310 | send_vpn_packet (p, si); |
1377 | send_vpn_packet (p, si); |
1311 | |
1378 | |
1312 | delete p; |
1379 | delete p; |
1313 | } |
1380 | } |
1314 | |
1381 | |
|
|
1382 | void |
1315 | void connection::script_init_env (const char *ext) |
1383 | connection::script_init_env (const char *ext) |
1316 | { |
1384 | { |
1317 | char *env; |
1385 | char *env; |
1318 | asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); |
1386 | asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); |
1319 | asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); |
1387 | asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); |
1320 | asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, |
1388 | asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, |
1321 | 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, |
1389 | 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, |
1322 | conf->id & 0xff); putenv (env); |
1390 | conf->id & 0xff); putenv (env); |
1323 | } |
1391 | } |
1324 | |
1392 | |
|
|
1393 | void |
1325 | void connection::script_init_connect_env () |
1394 | connection::script_init_connect_env () |
1326 | { |
1395 | { |
1327 | vpn->script_init_env (); |
1396 | vpn->script_init_env (); |
1328 | |
1397 | |
1329 | char *env; |
1398 | char *env; |
1330 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
1399 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
|
|
1400 | asprintf (&env, "DESTSI=%s", (const char *)si); putenv (env); |
1331 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1401 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1332 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1402 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1333 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1403 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1334 | } |
1404 | } |
1335 | |
1405 | |
1336 | inline const char * |
1406 | inline const char * |
1337 | connection::script_node_up () |
1407 | connection::script_node_up () |
1338 | { |
1408 | { |
… | |
… | |
1348 | |
1418 | |
1349 | return filename; |
1419 | return filename; |
1350 | } |
1420 | } |
1351 | |
1421 | |
1352 | inline const char * |
1422 | inline const char * |
|
|
1423 | connection::script_node_change () |
|
|
1424 | { |
|
|
1425 | script_init_connect_env (); |
|
|
1426 | |
|
|
1427 | putenv ((char *)"STATE=change"); |
|
|
1428 | |
|
|
1429 | char *filename; |
|
|
1430 | asprintf (&filename, |
|
|
1431 | "%s/%s", |
|
|
1432 | confbase, |
|
|
1433 | ::conf.script_node_change ? ::conf.script_node_change : "node-change"); |
|
|
1434 | |
|
|
1435 | return filename; |
|
|
1436 | } |
|
|
1437 | |
|
|
1438 | inline const char * |
1353 | connection::script_node_down () |
1439 | connection::script_node_down () |
1354 | { |
1440 | { |
1355 | script_init_connect_env (); |
1441 | script_init_connect_env (); |
1356 | |
1442 | |
1357 | putenv ((char *)"STATE=down"); |
1443 | putenv ((char *)"STATE=down"); |
… | |
… | |
1368 | connection::connection (struct vpn *vpn, conf_node *conf) |
1454 | connection::connection (struct vpn *vpn, conf_node *conf) |
1369 | : vpn(vpn), conf(conf), |
1455 | : vpn(vpn), conf(conf), |
1370 | #if ENABLE_DNS |
1456 | #if ENABLE_DNS |
1371 | dns (0), |
1457 | dns (0), |
1372 | #endif |
1458 | #endif |
1373 | data_queue(conf->max_ttl, conf->max_queue), |
1459 | data_queue(conf->max_ttl, conf->max_queue + 1), |
1374 | vpn_queue(conf->max_ttl, conf->max_queue) |
1460 | vpn_queue(conf->max_ttl, conf->max_queue + 1) |
1375 | { |
1461 | { |
1376 | rekey .set<connection, &connection::rekey_cb > (this); |
1462 | rekey .set<connection, &connection::rekey_cb > (this); |
1377 | keepalive .set<connection, &connection::keepalive_cb > (this); |
1463 | keepalive .set<connection, &connection::keepalive_cb > (this); |
1378 | establish_connection.set<connection, &connection::establish_connection_cb> (this); |
1464 | establish_connection.set<connection, &connection::establish_connection_cb> (this); |
1379 | |
1465 | |
1380 | last_establish_attempt = 0.; |
1466 | last_establish_attempt = 0.; |
1381 | octx = ictx = 0; |
1467 | octx = ictx = 0; |
1382 | |
1468 | |
1383 | if (!conf->protocols) // make sure some protocol is enabled |
|
|
1384 | conf->protocols = PROT_UDPv4; |
|
|
1385 | |
|
|
1386 | connectmode = conf->connectmode; |
1469 | connectmode = conf->connectmode; |
1387 | |
1470 | |
1388 | // queue a dummy packet to force an initial connection attempt |
1471 | // queue a dummy packet to force an initial connection attempt |
1389 | if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) |
1472 | if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) |
1390 | vpn_queue.put (new net_packet); |
1473 | vpn_queue.put (new net_packet); |
… | |
… | |
1395 | connection::~connection () |
1478 | connection::~connection () |
1396 | { |
1479 | { |
1397 | shutdown (); |
1480 | shutdown (); |
1398 | } |
1481 | } |
1399 | |
1482 | |
|
|
1483 | void |
1400 | void connection_init () |
1484 | connection_init () |
1401 | { |
1485 | { |
1402 | auth_rate_limiter.clear (); |
1486 | auth_rate_limiter.clear (); |
1403 | reset_rate_limiter.clear (); |
1487 | reset_rate_limiter.clear (); |
1404 | } |
1488 | } |
1405 | |
1489 | |