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 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); |
… | |
… | |
760 | { |
773 | { |
761 | ping_packet *pkt = new ping_packet; |
774 | ping_packet *pkt = new ping_packet; |
762 | |
775 | |
763 | 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); |
764 | |
777 | |
765 | slog (L_TRACE, ">>%d %s [%s]", conf->id, pong ? "PT_PONG" : "PT_PING", (const char *)si); |
778 | slog (L_TRACE, "%s << %s [%s]", conf->nodename, pong ? "PT_PONG" : "PT_PING", (const char *)si); |
766 | |
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 | } |
… | |
… | |
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)", conf->nodename, |
824 | conf->id, rid, (const char *)rsi); |
837 | vpn->conns[rid - 1]->conf->nodename, (const char *)rsi); |
825 | |
838 | |
826 | connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); |
839 | connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); |
827 | |
840 | |
828 | r->hmac_set (octx); |
841 | r->hmac_set (octx); |
829 | send_vpn_packet (r, si); |
842 | send_vpn_packet (r, si); |
… | |
… | |
832 | } |
845 | } |
833 | |
846 | |
834 | inline void |
847 | inline void |
835 | connection::establish_connection_cb (ev::timer &w, int revents) |
848 | connection::establish_connection_cb (ev::timer &w, int revents) |
836 | { |
849 | { |
837 | if (!ictx |
850 | if (!(ictx && octx) |
838 | && conf != THISNODE |
851 | && conf != THISNODE |
839 | && connectmode != conf_node::C_NEVER |
852 | && connectmode != conf_node::C_NEVER |
840 | && connectmode != conf_node::C_DISABLED |
853 | && connectmode != conf_node::C_DISABLED |
841 | && !w.is_active ()) |
854 | && !w.is_active ()) |
842 | { |
855 | { |
… | |
… | |
983 | data_queue.put (new tap_packet (*pkt)); |
996 | data_queue.put (new tap_packet (*pkt)); |
984 | post_inject_queue (); |
997 | post_inject_queue (); |
985 | } |
998 | } |
986 | } |
999 | } |
987 | |
1000 | |
|
|
1001 | void |
988 | void connection::inject_vpn_packet (vpn_packet *pkt, int tos) |
1002 | connection::inject_vpn_packet (vpn_packet *pkt, int tos) |
989 | { |
1003 | { |
990 | if (ictx && octx) |
1004 | if (ictx && octx) |
991 | send_vpn_packet (pkt, si, tos); |
1005 | send_vpn_packet (pkt, si, tos); |
992 | else |
1006 | else |
993 | { |
1007 | { |
… | |
… | |
999 | void |
1013 | void |
1000 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
1014 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
1001 | { |
1015 | { |
1002 | last_activity = ev_now (); |
1016 | last_activity = ev_now (); |
1003 | |
1017 | |
1004 | slog (L_NOISE, "<<%d received packet type %d from %d to %d.", |
1018 | slog (L_NOISE, "%s >> received packet type %d from %d to %d.", |
1005 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
1019 | conf->nodename, pkt->typ (), pkt->src (), pkt->dst ()); |
1006 | |
1020 | |
1007 | if (connectmode == conf_node::C_DISABLED) |
1021 | if (connectmode == conf_node::C_DISABLED) |
1008 | return; |
1022 | return; |
1009 | |
1023 | |
1010 | switch (pkt->typ ()) |
1024 | switch (pkt->typ ()) |
1011 | { |
1025 | { |
1012 | case vpn_packet::PT_PING: |
1026 | case vpn_packet::PT_PING: |
|
|
1027 | slog (L_TRACE, "%s >> PT_PING", conf->nodename); |
|
|
1028 | |
1013 | // we send pings instead of auth packets after some retries, |
1029 | // we send pings instead of auth packets after some retries, |
1014 | // so reset the retry counter and establish a connection |
1030 | // so reset the retry counter and establish a connection |
1015 | // when we receive a ping. |
1031 | // when we receive a ping. |
1016 | if (!ictx) |
1032 | if (!ictx) |
1017 | { |
1033 | { |
1018 | if (auth_rate_limiter.can (rsi)) |
1034 | if (auth_rate_limiter.can (rsi)) |
1019 | send_auth_request (rsi, true); |
1035 | send_auth_request (rsi, true); |
1020 | } |
1036 | } |
1021 | else |
1037 | else |
1022 | // we would love to change thre socket address here, but ping's aren't |
1038 | // we would love to change the socket address here, but ping's aren't |
1023 | // authenticated, so we best ignore it. |
1039 | // authenticated, so we best ignore it. |
1024 | send_ping (rsi, 1); // pong |
1040 | send_ping (rsi, 1); // pong |
1025 | |
1041 | |
1026 | break; |
1042 | break; |
1027 | |
1043 | |
1028 | case vpn_packet::PT_PONG: |
1044 | case vpn_packet::PT_PONG: |
|
|
1045 | slog (L_TRACE, "%s >> PT_PONG", conf->nodename); |
|
|
1046 | |
|
|
1047 | // a PONG might mean that the other side doesn't really know |
|
|
1048 | // about our desire for communication. |
|
|
1049 | establish_connection (); |
1029 | break; |
1050 | break; |
1030 | |
1051 | |
1031 | case vpn_packet::PT_RESET: |
1052 | case vpn_packet::PT_RESET: |
1032 | { |
1053 | { |
1033 | reset_connection (); |
1054 | reset_connection (); |
… | |
… | |
1048 | case vpn_packet::PT_AUTH_REQ: |
1069 | case vpn_packet::PT_AUTH_REQ: |
1049 | if (auth_rate_limiter.can (rsi)) |
1070 | if (auth_rate_limiter.can (rsi)) |
1050 | { |
1071 | { |
1051 | auth_req_packet *p = (auth_req_packet *) pkt; |
1072 | auth_req_packet *p = (auth_req_packet *) pkt; |
1052 | |
1073 | |
1053 | slog (L_TRACE, "<<%d PT_AUTH_REQ(%d)", conf->id, p->initiate); |
1074 | slog (L_TRACE, "%s >> PT_AUTH_REQ(%s)", conf->nodename, p->initiate ? "initiate" : "reply"); |
1054 | |
1075 | |
1055 | if (p->chk_config () && !strncmp (p->magic, MAGIC, 8)) |
1076 | if (p->chk_config () |
|
|
1077 | && (!strncmp (p->magic, MAGIC_OLD, 8) || !strncmp (p->magic, MAGIC, 8))) |
1056 | { |
1078 | { |
1057 | if (p->prot_minor != PROTOCOL_MINOR) |
1079 | if (p->prot_minor != PROTOCOL_MINOR) |
1058 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
1080 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
1059 | conf->nodename, (const char *)rsi, |
1081 | conf->nodename, (const char *)rsi, |
1060 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
1082 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
… | |
… | |
1096 | |
1118 | |
1097 | break; |
1119 | break; |
1098 | |
1120 | |
1099 | case vpn_packet::PT_AUTH_RES: |
1121 | case vpn_packet::PT_AUTH_RES: |
1100 | { |
1122 | { |
1101 | auth_res_packet *p = (auth_res_packet *) pkt; |
1123 | auth_res_packet *p = (auth_res_packet *)pkt; |
1102 | |
1124 | |
1103 | slog (L_TRACE, "<<%d PT_AUTH_RES", conf->id); |
1125 | slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename); |
1104 | |
1126 | |
1105 | if (p->chk_config ()) |
1127 | if (p->chk_config ()) |
1106 | { |
1128 | { |
1107 | if (p->prot_minor != PROTOCOL_MINOR) |
1129 | if (p->prot_minor != PROTOCOL_MINOR) |
1108 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
1130 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
… | |
… | |
1145 | si = rsi; |
1167 | si = rsi; |
1146 | protocol = rsi.prot; |
1168 | protocol = rsi.prot; |
1147 | |
1169 | |
1148 | slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."), |
1170 | slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."), |
1149 | conf->nodename, (const char *)rsi, |
1171 | conf->nodename, (const char *)rsi, |
1150 | is_direct ? "direct" : "routed", |
1172 | is_direct ? "direct" : "forwarded", |
1151 | p->prot_major, p->prot_minor); |
1173 | p->prot_major, p->prot_minor); |
1152 | |
1174 | |
1153 | connection_established (); |
1175 | connection_established (); |
1154 | |
1176 | |
1155 | if (::conf.script_node_up) |
1177 | if (::conf.script_node_up) |
… | |
… | |
1207 | // { |
1229 | // { |
1208 | slog (L_INFO, _("%s(%s): changing socket address to %s."), |
1230 | slog (L_INFO, _("%s(%s): changing socket address to %s."), |
1209 | conf->nodename, (const char *)si, (const char *)rsi); |
1231 | conf->nodename, (const char *)si, (const char *)rsi); |
1210 | |
1232 | |
1211 | si = rsi; |
1233 | si = rsi; |
|
|
1234 | |
|
|
1235 | if (::conf.script_node_change) |
|
|
1236 | { |
|
|
1237 | run_script_cb *cb = new run_script_cb; |
|
|
1238 | cb->set<connection, &connection::script_node_change> (this); |
|
|
1239 | run_script_queued (cb, _("node-change command execution failed, continuing.")); |
|
|
1240 | } |
|
|
1241 | |
1212 | // } |
1242 | // } |
1213 | //else |
1243 | //else |
1214 | // slog (L_INFO, _("%s(%s): accepted packet from %s, not (yet) redirecting traffic."), |
1244 | // slog (L_INFO, _("%s(%s): accepted packet from %s, not (yet) redirecting traffic."), |
1215 | // conf->nodename, (const char *)si, (const char *)rsi); |
1245 | // conf->nodename, (const char *)si, (const char *)rsi); |
1216 | } |
1246 | } |
… | |
… | |
1237 | break; |
1267 | break; |
1238 | |
1268 | |
1239 | case vpn_packet::PT_CONNECT_REQ: |
1269 | case vpn_packet::PT_CONNECT_REQ: |
1240 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1270 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1241 | { |
1271 | { |
1242 | connect_req_packet *p = (connect_req_packet *) pkt; |
1272 | connect_req_packet *p = (connect_req_packet *)pkt; |
1243 | |
1273 | |
1244 | if (p->id > 0 && p->id <= vpn->conns.size ()) |
1274 | if (p->id > 0 && p->id <= vpn->conns.size ()) |
1245 | { |
1275 | { |
1246 | connection *c = vpn->conns[p->id - 1]; |
1276 | connection *c = vpn->conns[p->id - 1]; |
1247 | conf->protocols = p->protocols; |
1277 | conf->protocols = p->protocols; |
1248 | |
1278 | |
1249 | slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]", |
1279 | slog (L_TRACE, "%s >> PT_CONNECT_REQ(%s) [%d]", |
1250 | conf->id, p->id, c->ictx && c->octx); |
1280 | conf->nodename, vpn->conns[p->id - 1]->conf->nodename, c->ictx && c->octx); |
1251 | |
1281 | |
1252 | if (c->ictx && c->octx) |
1282 | if (c->ictx && c->octx) |
1253 | { |
1283 | { |
1254 | // send connect_info packets to both sides, in case one is |
1284 | // send connect_info packets to both sides, in case one is |
1255 | // behind a nat firewall (or both ;) |
1285 | // behind a nat firewall (or both ;) |
… | |
… | |
1278 | |
1308 | |
1279 | c->conf->protocols = p->protocols; |
1309 | c->conf->protocols = p->protocols; |
1280 | protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); |
1310 | protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); |
1281 | p->si.upgrade_protocol (protocol, c->conf); |
1311 | p->si.upgrade_protocol (protocol, c->conf); |
1282 | |
1312 | |
1283 | slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", |
1313 | slog (L_TRACE, "%s >> PT_CONNECT_INFO(%s,%s) [%d]", |
|
|
1314 | conf->nodename, vpn->conns[p->id - 1]->conf->nodename, |
1284 | conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); |
1315 | (const char *)p->si, !c->ictx && !c->octx); |
1285 | |
1316 | |
1286 | const sockinfo &dsi = forward_si (p->si); |
1317 | const sockinfo &dsi = forward_si (p->si); |
1287 | |
1318 | |
1288 | if (dsi.valid ()) |
1319 | if (dsi.valid ()) |
1289 | c->send_auth_request (dsi, true); |
1320 | c->send_auth_request (dsi, true); |
… | |
… | |
1303 | } |
1334 | } |
1304 | |
1335 | |
1305 | inline void |
1336 | inline void |
1306 | connection::keepalive_cb (ev::timer &w, int revents) |
1337 | connection::keepalive_cb (ev::timer &w, int revents) |
1307 | { |
1338 | { |
1308 | if (ev_now () >= last_activity + ::conf.keepalive + 30) |
1339 | if (ev_now () >= last_activity + ::conf.keepalive + 15) |
1309 | { |
1340 | { |
1310 | reset_connection (); |
1341 | reset_connection (); |
1311 | establish_connection (); |
1342 | establish_connection (); |
1312 | } |
1343 | } |
1313 | else if (ev_now () < last_activity + ::conf.keepalive) |
1344 | else if (ev_now () < last_activity + ::conf.keepalive) |
1314 | w.start (last_activity + ::conf.keepalive - ev::now ()); |
1345 | w.start (last_activity + ::conf.keepalive - ev::now ()); |
1315 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1346 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1316 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1347 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1317 | { |
1348 | { |
1318 | send_ping (si); |
1349 | send_ping (si); |
1319 | w.start (5); |
1350 | w.start (3); |
1320 | } |
1351 | } |
1321 | else if (ev_now () < last_activity + ::conf.keepalive + 10) |
1352 | else if (ev_now () < last_activity + ::conf.keepalive + 10) |
1322 | // hold ondemand connections implicitly a few seconds longer |
1353 | // hold ondemand connections implicitly a few seconds longer |
1323 | // should delete octx, though, or something like that ;) |
1354 | // should delete octx, though, or something like that ;) |
1324 | w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); |
1355 | w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); |
1325 | else |
1356 | else |
1326 | reset_connection (); |
1357 | reset_connection (); |
1327 | } |
1358 | } |
1328 | |
1359 | |
|
|
1360 | void |
1329 | void connection::send_connect_request (int id) |
1361 | connection::send_connect_request (int id) |
1330 | { |
1362 | { |
1331 | connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); |
1363 | connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); |
1332 | |
1364 | |
1333 | slog (L_TRACE, ">>%d PT_CONNECT_REQ(%d)", conf->id, id); |
1365 | slog (L_TRACE, "%s << PT_CONNECT_REQ(%s)", |
|
|
1366 | conf->nodename, vpn->conns[id - 1]->conf->nodename); |
1334 | p->hmac_set (octx); |
1367 | p->hmac_set (octx); |
1335 | send_vpn_packet (p, si); |
1368 | send_vpn_packet (p, si); |
1336 | |
1369 | |
1337 | delete p; |
1370 | delete p; |
1338 | } |
1371 | } |
1339 | |
1372 | |
|
|
1373 | void |
1340 | void connection::script_init_env (const char *ext) |
1374 | connection::script_init_env (const char *ext) |
1341 | { |
1375 | { |
1342 | char *env; |
1376 | char *env; |
1343 | asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); |
1377 | asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); |
1344 | asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); |
1378 | asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); |
1345 | asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, |
1379 | asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, |
1346 | 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, |
1380 | 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, |
1347 | conf->id & 0xff); putenv (env); |
1381 | conf->id & 0xff); putenv (env); |
1348 | } |
1382 | } |
1349 | |
1383 | |
|
|
1384 | void |
1350 | void connection::script_init_connect_env () |
1385 | connection::script_init_connect_env () |
1351 | { |
1386 | { |
1352 | vpn->script_init_env (); |
1387 | vpn->script_init_env (); |
1353 | |
1388 | |
1354 | char *env; |
1389 | char *env; |
1355 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
1390 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
|
|
1391 | asprintf (&env, "DESTSI=%s", (const char *)si); putenv (env); |
1356 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1392 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1357 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1393 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1358 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1394 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1359 | } |
1395 | } |
1360 | |
1396 | |
1361 | inline const char * |
1397 | inline const char * |
1362 | connection::script_node_up () |
1398 | connection::script_node_up () |
1363 | { |
1399 | { |
… | |
… | |
1368 | char *filename; |
1404 | char *filename; |
1369 | asprintf (&filename, |
1405 | asprintf (&filename, |
1370 | "%s/%s", |
1406 | "%s/%s", |
1371 | confbase, |
1407 | confbase, |
1372 | ::conf.script_node_up ? ::conf.script_node_up : "node-up"); |
1408 | ::conf.script_node_up ? ::conf.script_node_up : "node-up"); |
|
|
1409 | |
|
|
1410 | return filename; |
|
|
1411 | } |
|
|
1412 | |
|
|
1413 | inline const char * |
|
|
1414 | connection::script_node_change () |
|
|
1415 | { |
|
|
1416 | script_init_connect_env (); |
|
|
1417 | |
|
|
1418 | putenv ((char *)"STATE=change"); |
|
|
1419 | |
|
|
1420 | char *filename; |
|
|
1421 | asprintf (&filename, |
|
|
1422 | "%s/%s", |
|
|
1423 | confbase, |
|
|
1424 | ::conf.script_node_change ? ::conf.script_node_change : "node-change"); |
1373 | |
1425 | |
1374 | return filename; |
1426 | return filename; |
1375 | } |
1427 | } |
1376 | |
1428 | |
1377 | inline const char * |
1429 | inline const char * |
… | |
… | |
1420 | connection::~connection () |
1472 | connection::~connection () |
1421 | { |
1473 | { |
1422 | shutdown (); |
1474 | shutdown (); |
1423 | } |
1475 | } |
1424 | |
1476 | |
|
|
1477 | void |
1425 | void connection_init () |
1478 | connection_init () |
1426 | { |
1479 | { |
1427 | auth_rate_limiter.clear (); |
1480 | auth_rate_limiter.clear (); |
1428 | reset_rate_limiter.clear (); |
1481 | reset_rate_limiter.clear (); |
1429 | } |
1482 | } |
1430 | |
1483 | |