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 | { |
… | |
… | |
128 | rsa_hash (const rsaid &id, const rsachallenge &chg, rsaresponse &h) |
132 | rsa_hash (const rsaid &id, const rsachallenge &chg, rsaresponse &h) |
129 | { |
133 | { |
130 | EVP_MD_CTX ctx; |
134 | EVP_MD_CTX ctx; |
131 | |
135 | |
132 | EVP_MD_CTX_init (&ctx); |
136 | EVP_MD_CTX_init (&ctx); |
133 | require (EVP_DigestInit (&ctx, RSA_HASH)); |
137 | require (EVP_DigestInit (&ctx, RSA_HASH)); |
134 | require (EVP_DigestUpdate(&ctx, &chg, sizeof chg)); |
138 | require (EVP_DigestUpdate (&ctx, &chg, sizeof chg)); |
135 | require (EVP_DigestUpdate(&ctx, &id, sizeof id)); |
139 | require (EVP_DigestUpdate (&ctx, &id, sizeof id)); |
136 | require (EVP_DigestFinal (&ctx, (unsigned char *)&h, 0)); |
140 | require (EVP_DigestFinal (&ctx, (unsigned char *)&h, 0)); |
137 | EVP_MD_CTX_cleanup (&ctx); |
141 | EVP_MD_CTX_cleanup (&ctx); |
138 | } |
142 | } |
139 | |
143 | |
140 | struct rsa_entry |
144 | struct rsa_entry |
141 | { |
145 | { |
… | |
… | |
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 | } |
… | |
… | |
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, "%s >> PT_AUTH_REQ [%s]", conf->nodename, (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 | } |
… | |
… | |
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, "%s << PT_CONNECT_INFO(%s,%s)", conf->nodename, |
836 | slog (L_TRACE, "%s << PT_CONNECT_INFO(%s,%s,p%02x)", conf->nodename, |
824 | vpn->conns[rid - 1]->conf->nodename, (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 | { |
… | |
… | |
910 | } |
924 | } |
911 | } |
925 | } |
912 | |
926 | |
913 | delete ictx; ictx = 0; |
927 | delete ictx; ictx = 0; |
914 | delete octx; octx = 0; |
928 | delete octx; octx = 0; |
915 | #if ENABLE_DNS |
|
|
916 | dnsv4_reset_connection (); |
|
|
917 | #endif |
|
|
918 | |
929 | |
919 | si.host = 0; |
930 | si.host = 0; |
920 | |
931 | |
921 | last_activity = 0.; |
932 | last_activity = 0.; |
922 | //last_si_change = 0.; |
933 | //last_si_change = 0.; |
… | |
… | |
983 | data_queue.put (new tap_packet (*pkt)); |
994 | data_queue.put (new tap_packet (*pkt)); |
984 | post_inject_queue (); |
995 | post_inject_queue (); |
985 | } |
996 | } |
986 | } |
997 | } |
987 | |
998 | |
|
|
999 | void |
988 | void connection::inject_vpn_packet (vpn_packet *pkt, int tos) |
1000 | connection::inject_vpn_packet (vpn_packet *pkt, int tos) |
989 | { |
1001 | { |
990 | if (ictx && octx) |
1002 | if (ictx && octx) |
991 | send_vpn_packet (pkt, si, tos); |
1003 | send_vpn_packet (pkt, si, tos); |
992 | else |
1004 | else |
993 | { |
1005 | { |
… | |
… | |
999 | void |
1011 | void |
1000 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
1012 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
1001 | { |
1013 | { |
1002 | last_activity = ev_now (); |
1014 | last_activity = ev_now (); |
1003 | |
1015 | |
1004 | slog (L_NOISE, "%s >> received packet type %d from %d to %d.", |
1016 | slog (L_NOISE, "%s >> received packet type %d from %d to %d.", |
1005 | conf->nodename, pkt->typ (), pkt->src (), pkt->dst ()); |
1017 | conf->nodename, pkt->typ (), pkt->src (), pkt->dst ()); |
1006 | |
1018 | |
1007 | if (connectmode == conf_node::C_DISABLED) |
1019 | if (connectmode == conf_node::C_DISABLED) |
1008 | return; |
1020 | return; |
1009 | |
1021 | |
… | |
… | |
1019 | { |
1031 | { |
1020 | if (auth_rate_limiter.can (rsi)) |
1032 | if (auth_rate_limiter.can (rsi)) |
1021 | send_auth_request (rsi, true); |
1033 | send_auth_request (rsi, true); |
1022 | } |
1034 | } |
1023 | else |
1035 | else |
1024 | // we would love to change thre socket address here, but ping's aren't |
1036 | // we would love to change the socket address here, but ping's aren't |
1025 | // authenticated, so we best ignore it. |
1037 | // authenticated, so we best ignore it. |
1026 | send_ping (rsi, 1); // pong |
1038 | send_ping (rsi, 1); // pong |
1027 | |
1039 | |
1028 | break; |
1040 | break; |
1029 | |
1041 | |
1030 | case vpn_packet::PT_PONG: |
1042 | case vpn_packet::PT_PONG: |
1031 | slog (L_TRACE, "%s >> PT_PONG", conf->nodename); |
1043 | slog (L_TRACE, "%s >> PT_PONG", conf->nodename); |
|
|
1044 | |
|
|
1045 | // a PONG might mean that the other side doesn't really know |
|
|
1046 | // about our desire for communication. |
|
|
1047 | establish_connection (); |
1032 | break; |
1048 | break; |
1033 | |
1049 | |
1034 | case vpn_packet::PT_RESET: |
1050 | case vpn_packet::PT_RESET: |
1035 | { |
1051 | { |
1036 | reset_connection (); |
1052 | reset_connection (); |
… | |
… | |
1049 | break; |
1065 | break; |
1050 | |
1066 | |
1051 | case vpn_packet::PT_AUTH_REQ: |
1067 | case vpn_packet::PT_AUTH_REQ: |
1052 | if (auth_rate_limiter.can (rsi)) |
1068 | if (auth_rate_limiter.can (rsi)) |
1053 | { |
1069 | { |
1054 | auth_req_packet *p = (auth_req_packet *) pkt; |
1070 | auth_req_packet *p = (auth_req_packet *)pkt; |
1055 | |
1071 | |
1056 | slog (L_TRACE, "%s << PT_AUTH_REQ(%s)", conf->nodename, p->initiate ? "initiate" : "reply"); |
1072 | slog (L_TRACE, "%s >> PT_AUTH_REQ(%s,p%02x,f%02x)", |
|
|
1073 | conf->nodename, p->initiate ? "initiate" : "reply", |
|
|
1074 | p->protocols, p->features); |
1057 | |
1075 | |
1058 | if (p->chk_config () && !strncmp (p->magic, MAGIC, 8)) |
1076 | if (p->chk_config () |
|
|
1077 | && (!memcmp (p->magic, MAGIC_OLD, 8) || !memcmp (p->magic, MAGIC, 8))) |
1059 | { |
1078 | { |
1060 | if (p->prot_minor != PROTOCOL_MINOR) |
1079 | if (p->prot_minor != PROTOCOL_MINOR) |
1061 | 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."), |
1062 | conf->nodename, (const char *)rsi, |
1081 | conf->nodename, (const char *)rsi, |
1063 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
1082 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
… | |
… | |
1101 | |
1120 | |
1102 | case vpn_packet::PT_AUTH_RES: |
1121 | case vpn_packet::PT_AUTH_RES: |
1103 | { |
1122 | { |
1104 | auth_res_packet *p = (auth_res_packet *)pkt; |
1123 | auth_res_packet *p = (auth_res_packet *)pkt; |
1105 | |
1124 | |
1106 | slog (L_TRACE, "%s << PT_AUTH_RES", conf->nodename); |
1125 | slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename); |
1107 | |
1126 | |
1108 | if (p->chk_config ()) |
1127 | if (p->chk_config ()) |
1109 | { |
1128 | { |
1110 | if (p->prot_minor != PROTOCOL_MINOR) |
1129 | if (p->prot_minor != PROTOCOL_MINOR) |
1111 | 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."), |
… | |
… | |
1148 | si = rsi; |
1167 | si = rsi; |
1149 | protocol = rsi.prot; |
1168 | protocol = rsi.prot; |
1150 | |
1169 | |
1151 | 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."), |
1152 | conf->nodename, (const char *)rsi, |
1171 | conf->nodename, (const char *)rsi, |
1153 | is_direct ? "direct" : "routed", |
1172 | is_direct ? "direct" : "forwarded", |
1154 | p->prot_major, p->prot_minor); |
1173 | p->prot_major, p->prot_minor); |
1155 | |
1174 | |
1156 | connection_established (); |
1175 | connection_established (); |
1157 | |
1176 | |
1158 | if (::conf.script_node_up) |
1177 | if (::conf.script_node_up) |
… | |
… | |
1210 | // { |
1229 | // { |
1211 | slog (L_INFO, _("%s(%s): changing socket address to %s."), |
1230 | slog (L_INFO, _("%s(%s): changing socket address to %s."), |
1212 | conf->nodename, (const char *)si, (const char *)rsi); |
1231 | conf->nodename, (const char *)si, (const char *)rsi); |
1213 | |
1232 | |
1214 | 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 | |
1215 | // } |
1242 | // } |
1216 | //else |
1243 | //else |
1217 | // 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."), |
1218 | // conf->nodename, (const char *)si, (const char *)rsi); |
1245 | // conf->nodename, (const char *)si, (const char *)rsi); |
1219 | } |
1246 | } |
… | |
… | |
1240 | break; |
1267 | break; |
1241 | |
1268 | |
1242 | case vpn_packet::PT_CONNECT_REQ: |
1269 | case vpn_packet::PT_CONNECT_REQ: |
1243 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1270 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1244 | { |
1271 | { |
1245 | connect_req_packet *p = (connect_req_packet *) pkt; |
1272 | connect_req_packet *p = (connect_req_packet *)pkt; |
1246 | |
1273 | |
1247 | if (p->id > 0 && p->id <= vpn->conns.size ()) |
1274 | if (p->id > 0 && p->id <= vpn->conns.size ()) |
1248 | { |
1275 | { |
1249 | connection *c = vpn->conns[p->id - 1]; |
1276 | connection *c = vpn->conns[p->id - 1]; |
1250 | conf->protocols = p->protocols; |
1277 | conf->protocols = p->protocols; |
1251 | |
1278 | |
1252 | slog (L_TRACE, "%s << PT_CONNECT_REQ(%s) [%d]", |
1279 | slog (L_TRACE, "%s >> PT_CONNECT_REQ(%s,p%02x) [%d]", |
1253 | conf->nodename, vpn->conns[p->id - 1]->conf->nodename, c->ictx && c->octx); |
1280 | conf->nodename, vpn->conns[p->id - 1]->conf->nodename, |
|
|
1281 | p->protocols, |
|
|
1282 | c->ictx && c->octx); |
1254 | |
1283 | |
1255 | if (c->ictx && c->octx) |
1284 | if (c->ictx && c->octx) |
1256 | { |
1285 | { |
1257 | // send connect_info packets to both sides, in case one is |
1286 | // send connect_info packets to both sides, in case one is |
1258 | // behind a nat firewall (or both ;) |
1287 | // behind a nat firewall (or both ;) |
… | |
… | |
1281 | |
1310 | |
1282 | c->conf->protocols = p->protocols; |
1311 | c->conf->protocols = p->protocols; |
1283 | protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); |
1312 | protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); |
1284 | p->si.upgrade_protocol (protocol, c->conf); |
1313 | p->si.upgrade_protocol (protocol, c->conf); |
1285 | |
1314 | |
1286 | slog (L_TRACE, "%s << PT_CONNECT_INFO(%s,%s) [%d]", |
1315 | slog (L_TRACE, "%s >> PT_CONNECT_INFO(%s,%s,p%02x) [%d]", |
|
|
1316 | conf->nodename, |
1287 | conf->nodename, vpn->conns[p->id - 1]->conf->nodename, |
1317 | vpn->conns[p->id - 1]->conf->nodename, |
|
|
1318 | (const char *)p->si, |
|
|
1319 | p->protocols, |
1288 | (const char *)p->si, !c->ictx && !c->octx); |
1320 | !c->ictx && !c->octx); |
1289 | |
1321 | |
1290 | const sockinfo &dsi = forward_si (p->si); |
1322 | const sockinfo &dsi = forward_si (p->si); |
1291 | |
1323 | |
1292 | if (dsi.valid ()) |
1324 | if (dsi.valid ()) |
1293 | c->send_auth_request (dsi, true); |
1325 | c->send_auth_request (dsi, true); |
… | |
… | |
1307 | } |
1339 | } |
1308 | |
1340 | |
1309 | inline void |
1341 | inline void |
1310 | connection::keepalive_cb (ev::timer &w, int revents) |
1342 | connection::keepalive_cb (ev::timer &w, int revents) |
1311 | { |
1343 | { |
1312 | if (ev_now () >= last_activity + ::conf.keepalive + 30) |
1344 | if (ev_now () >= last_activity + ::conf.keepalive + 15) |
1313 | { |
1345 | { |
1314 | reset_connection (); |
1346 | reset_connection (); |
1315 | establish_connection (); |
1347 | establish_connection (); |
1316 | } |
1348 | } |
1317 | else if (ev_now () < last_activity + ::conf.keepalive) |
1349 | else if (ev_now () < last_activity + ::conf.keepalive) |
1318 | w.start (last_activity + ::conf.keepalive - ev::now ()); |
1350 | w.start (last_activity + ::conf.keepalive - ev::now ()); |
1319 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1351 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1320 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1352 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1321 | { |
1353 | { |
1322 | send_ping (si); |
1354 | send_ping (si); |
1323 | w.start (5); |
1355 | w.start (3); |
1324 | } |
1356 | } |
1325 | else if (ev_now () < last_activity + ::conf.keepalive + 10) |
1357 | else if (ev_now () < last_activity + ::conf.keepalive + 10) |
1326 | // hold ondemand connections implicitly a few seconds longer |
1358 | // hold ondemand connections implicitly a few seconds longer |
1327 | // should delete octx, though, or something like that ;) |
1359 | // should delete octx, though, or something like that ;) |
1328 | w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); |
1360 | w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); |
1329 | else |
1361 | else |
1330 | reset_connection (); |
1362 | reset_connection (); |
1331 | } |
1363 | } |
1332 | |
1364 | |
|
|
1365 | void |
1333 | void connection::send_connect_request (int id) |
1366 | connection::send_connect_request (int id) |
1334 | { |
1367 | { |
1335 | connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); |
1368 | connect_req_packet *p = new connect_req_packet (conf->id, id, THISNODE->protocols); |
1336 | |
1369 | |
1337 | slog (L_TRACE, "%s >> PT_CONNECT_REQ(%s)", |
1370 | slog (L_TRACE, "%s << PT_CONNECT_REQ(%s,p%02x)", |
1338 | conf->nodename, vpn->conns[id - 1]->conf->nodename); |
1371 | conf->nodename, vpn->conns[id - 1]->conf->nodename, |
|
|
1372 | THISNODE->protocols); |
1339 | p->hmac_set (octx); |
1373 | p->hmac_set (octx); |
1340 | send_vpn_packet (p, si); |
1374 | send_vpn_packet (p, si); |
1341 | |
1375 | |
1342 | delete p; |
1376 | delete p; |
1343 | } |
1377 | } |
1344 | |
1378 | |
|
|
1379 | void |
1345 | void connection::script_init_env (const char *ext) |
1380 | connection::script_init_env (const char *ext) |
1346 | { |
1381 | { |
1347 | char *env; |
1382 | char *env; |
1348 | asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); |
1383 | asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); |
1349 | asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); |
1384 | asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); |
1350 | asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, |
1385 | asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, |
1351 | 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, |
1386 | 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, |
1352 | conf->id & 0xff); putenv (env); |
1387 | conf->id & 0xff); putenv (env); |
1353 | } |
1388 | } |
1354 | |
1389 | |
|
|
1390 | void |
1355 | void connection::script_init_connect_env () |
1391 | connection::script_init_connect_env () |
1356 | { |
1392 | { |
1357 | vpn->script_init_env (); |
1393 | vpn->script_init_env (); |
1358 | |
1394 | |
1359 | char *env; |
1395 | char *env; |
1360 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
1396 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
|
|
1397 | asprintf (&env, "DESTSI=%s", (const char *)si); putenv (env); |
1361 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1398 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1362 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1399 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1363 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1400 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1364 | } |
1401 | } |
1365 | |
1402 | |
1366 | inline const char * |
1403 | inline const char * |
1367 | connection::script_node_up () |
1404 | connection::script_node_up () |
1368 | { |
1405 | { |
… | |
… | |
1373 | char *filename; |
1410 | char *filename; |
1374 | asprintf (&filename, |
1411 | asprintf (&filename, |
1375 | "%s/%s", |
1412 | "%s/%s", |
1376 | confbase, |
1413 | confbase, |
1377 | ::conf.script_node_up ? ::conf.script_node_up : "node-up"); |
1414 | ::conf.script_node_up ? ::conf.script_node_up : "node-up"); |
|
|
1415 | |
|
|
1416 | return filename; |
|
|
1417 | } |
|
|
1418 | |
|
|
1419 | inline const char * |
|
|
1420 | connection::script_node_change () |
|
|
1421 | { |
|
|
1422 | script_init_connect_env (); |
|
|
1423 | |
|
|
1424 | putenv ((char *)"STATE=change"); |
|
|
1425 | |
|
|
1426 | char *filename; |
|
|
1427 | asprintf (&filename, |
|
|
1428 | "%s/%s", |
|
|
1429 | confbase, |
|
|
1430 | ::conf.script_node_change ? ::conf.script_node_change : "node-change"); |
1378 | |
1431 | |
1379 | return filename; |
1432 | return filename; |
1380 | } |
1433 | } |
1381 | |
1434 | |
1382 | inline const char * |
1435 | inline const char * |
… | |
… | |
1408 | establish_connection.set<connection, &connection::establish_connection_cb> (this); |
1461 | establish_connection.set<connection, &connection::establish_connection_cb> (this); |
1409 | |
1462 | |
1410 | last_establish_attempt = 0.; |
1463 | last_establish_attempt = 0.; |
1411 | octx = ictx = 0; |
1464 | octx = ictx = 0; |
1412 | |
1465 | |
1413 | if (!conf->protocols) // make sure some protocol is enabled |
|
|
1414 | conf->protocols = PROT_UDPv4; |
|
|
1415 | |
|
|
1416 | connectmode = conf->connectmode; |
1466 | connectmode = conf->connectmode; |
1417 | |
1467 | |
1418 | // queue a dummy packet to force an initial connection attempt |
1468 | // queue a dummy packet to force an initial connection attempt |
1419 | if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) |
1469 | if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) |
1420 | vpn_queue.put (new net_packet); |
1470 | vpn_queue.put (new net_packet); |
… | |
… | |
1425 | connection::~connection () |
1475 | connection::~connection () |
1426 | { |
1476 | { |
1427 | shutdown (); |
1477 | shutdown (); |
1428 | } |
1478 | } |
1429 | |
1479 | |
|
|
1480 | void |
1430 | void connection_init () |
1481 | connection_init () |
1431 | { |
1482 | { |
1432 | auth_rate_limiter.clear (); |
1483 | auth_rate_limiter.clear (); |
1433 | reset_rate_limiter.clear (); |
1484 | reset_rate_limiter.clear (); |
1434 | } |
1485 | } |
1435 | |
1486 | |