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 |
… | |
… | |
64 | ////////////////////////////////////////////////////////////////////////////// |
64 | ////////////////////////////////////////////////////////////////////////////// |
65 | |
65 | |
66 | 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; |
67 | static ev::child rs_child_ev; |
67 | static ev::child rs_child_ev; |
68 | |
68 | |
|
|
69 | namespace |
|
|
70 | { |
69 | void // c++ requires external linkage here, apparently :( |
71 | void // c++ requires external linkage here, apparently :( |
70 | rs_child_cb (ev::child &w, int revents) |
72 | rs_child_cb (ev::child &w, int revents) |
71 | { |
73 | { |
72 | w.stop (); |
74 | w.stop (); |
73 | |
75 | |
74 | if (rs_queue.empty ()) |
76 | if (rs_queue.empty ()) |
75 | return; |
77 | return; |
76 | |
78 | |
77 | pid_t pid = run_script (*rs_queue.front ().first, false); |
79 | pid_t pid = run_script (*rs_queue.front ().first, false); |
78 | if (pid) |
80 | if (pid) |
79 | { |
81 | { |
80 | w.set (pid); |
82 | w.set (pid); |
81 | w.start (); |
83 | w.start (); |
82 | } |
84 | } |
83 | else |
85 | else |
84 | slog (L_WARN, rs_queue.front ().second); |
86 | slog (L_WARN, rs_queue.front ().second); |
85 | |
87 | |
86 | delete rs_queue.front ().first; |
88 | delete rs_queue.front ().first; |
87 | rs_queue.pop (); |
89 | rs_queue.pop (); |
88 | } |
90 | } |
|
|
91 | }; |
89 | |
92 | |
90 | // despite the fancy name, this is quite a hack |
93 | // despite the fancy name, this is quite a hack |
91 | static void |
94 | static void |
92 | run_script_queued (run_script_cb *cb, const char *warnmsg) |
95 | run_script_queued (run_script_cb *cb, const char *warnmsg) |
93 | { |
96 | { |
… | |
… | |
129 | rsa_hash (const rsaid &id, const rsachallenge &chg, rsaresponse &h) |
132 | rsa_hash (const rsaid &id, const rsachallenge &chg, rsaresponse &h) |
130 | { |
133 | { |
131 | EVP_MD_CTX ctx; |
134 | EVP_MD_CTX ctx; |
132 | |
135 | |
133 | EVP_MD_CTX_init (&ctx); |
136 | EVP_MD_CTX_init (&ctx); |
134 | require (EVP_DigestInit (&ctx, RSA_HASH)); |
137 | require (EVP_DigestInit (&ctx, RSA_HASH)); |
135 | require (EVP_DigestUpdate(&ctx, &chg, sizeof chg)); |
138 | require (EVP_DigestUpdate (&ctx, &chg, sizeof chg)); |
136 | require (EVP_DigestUpdate(&ctx, &id, sizeof id)); |
139 | require (EVP_DigestUpdate (&ctx, &id, sizeof id)); |
137 | require (EVP_DigestFinal (&ctx, (unsigned char *)&h, 0)); |
140 | require (EVP_DigestFinal (&ctx, (unsigned char *)&h, 0)); |
138 | EVP_MD_CTX_cleanup (&ctx); |
141 | EVP_MD_CTX_cleanup (&ctx); |
139 | } |
142 | } |
140 | |
143 | |
141 | struct rsa_entry |
144 | struct rsa_entry |
142 | { |
145 | { |
… | |
… | |
191 | cleaner.set (RSA_TTL, RSA_TTL); |
194 | cleaner.set (RSA_TTL, RSA_TTL); |
192 | } |
195 | } |
193 | |
196 | |
194 | } rsa_cache; |
197 | } rsa_cache; |
195 | |
198 | |
|
|
199 | void |
196 | void rsa_cache::cleaner_cb (ev::timer &w, int revents) |
200 | rsa_cache::cleaner_cb (ev::timer &w, int revents) |
197 | { |
201 | { |
198 | if (empty ()) |
202 | if (empty ()) |
199 | w.stop (); |
203 | w.stop (); |
200 | else |
204 | else |
201 | { |
205 | { |
… | |
… | |
226 | delete p; |
230 | delete p; |
227 | |
231 | |
228 | delete [] queue; |
232 | delete [] queue; |
229 | } |
233 | } |
230 | |
234 | |
|
|
235 | void |
231 | void pkt_queue::expire_cb (ev::timer &w, int revents) |
236 | pkt_queue::expire_cb (ev::timer &w, int revents) |
232 | { |
237 | { |
233 | ev_tstamp expire = ev_now () - max_ttl; |
238 | ev_tstamp expire = ev_now () - max_ttl; |
234 | |
239 | |
235 | for (;;) |
240 | for (;;) |
236 | { |
241 | { |
… | |
… | |
247 | |
252 | |
248 | delete get (); |
253 | delete get (); |
249 | } |
254 | } |
250 | } |
255 | } |
251 | |
256 | |
|
|
257 | void |
252 | void pkt_queue::put (net_packet *p) |
258 | pkt_queue::put (net_packet *p) |
253 | { |
259 | { |
254 | ev_tstamp now = ev_now (); |
260 | ev_tstamp now = ev_now (); |
255 | |
261 | |
256 | // start expiry timer |
262 | // start expiry timer |
257 | if (empty ()) |
263 | if (empty ()) |
… | |
… | |
266 | queue[i].tstamp = now; |
272 | queue[i].tstamp = now; |
267 | |
273 | |
268 | i = ni; |
274 | i = ni; |
269 | } |
275 | } |
270 | |
276 | |
271 | net_packet *pkt_queue::get () |
277 | net_packet * |
|
|
278 | pkt_queue::get () |
272 | { |
279 | { |
273 | if (empty ()) |
280 | if (empty ()) |
274 | return 0; |
281 | return 0; |
275 | |
282 | |
276 | net_packet *p = queue[j].pkt; |
283 | net_packet *p = queue[j].pkt; |
… | |
… | |
300 | |
307 | |
301 | bool can (const sockinfo &si) { return can((u32)si.host); } |
308 | bool can (const sockinfo &si) { return can((u32)si.host); } |
302 | bool can (u32 host); |
309 | bool can (u32 host); |
303 | }; |
310 | }; |
304 | |
311 | |
305 | net_rate_limiter auth_rate_limiter, reset_rate_limiter; |
312 | static net_rate_limiter auth_rate_limiter, reset_rate_limiter; |
306 | |
313 | |
|
|
314 | bool |
307 | bool net_rate_limiter::can (u32 host) |
315 | net_rate_limiter::can (u32 host) |
308 | { |
316 | { |
309 | iterator i; |
317 | iterator i; |
310 | |
318 | |
311 | for (i = begin (); i != end (); ) |
319 | for (i = begin (); i != end (); ) |
312 | if (i->host == host) |
320 | if (i->host == host) |
… | |
… | |
359 | |
367 | |
360 | ///////////////////////////////////////////////////////////////////////////// |
368 | ///////////////////////////////////////////////////////////////////////////// |
361 | |
369 | |
362 | unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE]; |
370 | unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE]; |
363 | |
371 | |
|
|
372 | void |
364 | void hmac_packet::hmac_gen (crypto_ctx *ctx) |
373 | hmac_packet::hmac_gen (crypto_ctx *ctx) |
365 | { |
374 | { |
366 | unsigned int xlen; |
375 | unsigned int xlen; |
367 | |
376 | |
368 | HMAC_CTX *hctx = &ctx->hctx; |
377 | HMAC_CTX *hctx = &ctx->hctx; |
369 | |
378 | |
… | |
… | |
387 | hmac_gen (ctx); |
396 | hmac_gen (ctx); |
388 | |
397 | |
389 | return !memcmp (hmac, hmac_digest, HMACLENGTH); |
398 | return !memcmp (hmac, hmac_digest, HMACLENGTH); |
390 | } |
399 | } |
391 | |
400 | |
|
|
401 | void |
392 | void vpn_packet::set_hdr (ptype type_, unsigned int dst) |
402 | vpn_packet::set_hdr (ptype type_, unsigned int dst) |
393 | { |
403 | { |
394 | type = type_; |
404 | type = type_; |
395 | |
405 | |
396 | int src = THISNODE->id; |
406 | int src = THISNODE->id; |
397 | |
407 | |
… | |
… | |
402 | |
412 | |
403 | #define MAXVPNDATA (MAX_MTU - 6 - 6) |
413 | #define MAXVPNDATA (MAX_MTU - 6 - 6) |
404 | #define DATAHDR (sizeof (u32) + RAND_SIZE) |
414 | #define DATAHDR (sizeof (u32) + RAND_SIZE) |
405 | |
415 | |
406 | 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 |
407 | { |
425 | { |
408 | u8 data[MAXVPNDATA + DATAHDR]; // seqno |
|
|
409 | |
|
|
410 | void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno); |
|
|
411 | tap_packet *unpack (connection *conn, u32 &seqno); |
|
|
412 | private: |
|
|
413 | |
|
|
414 | const u32 data_hdr_size () const |
|
|
415 | { |
|
|
416 | return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR; |
426 | return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR; |
417 | } |
|
|
418 | }; |
427 | } |
|
|
428 | }; |
419 | |
429 | |
420 | void |
430 | void |
421 | 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) |
422 | { |
432 | { |
423 | EVP_CIPHER_CTX *cctx = &conn->octx->cctx; |
433 | EVP_CIPHER_CTX *cctx = &conn->octx->cctx; |
… | |
… | |
425 | ptype type = PT_DATA_UNCOMPRESSED; |
435 | ptype type = PT_DATA_UNCOMPRESSED; |
426 | |
436 | |
427 | #if ENABLE_COMPRESSION |
437 | #if ENABLE_COMPRESSION |
428 | u8 cdata[MAX_MTU]; |
438 | u8 cdata[MAX_MTU]; |
429 | |
439 | |
430 | if (conn->features & ENABLE_COMPRESSION) |
440 | if (conn->features & FEATURE_COMPRESSION) |
431 | { |
441 | { |
432 | u32 cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); |
442 | u32 cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); |
433 | |
443 | |
434 | if (cl) |
444 | if (cl) |
435 | { |
445 | { |
… | |
… | |
562 | #endif |
572 | #endif |
563 | return f; |
573 | return f; |
564 | } |
574 | } |
565 | }; |
575 | }; |
566 | |
576 | |
|
|
577 | void |
567 | void config_packet::setup (ptype type, int dst) |
578 | config_packet::setup (ptype type, int dst) |
568 | { |
579 | { |
569 | prot_major = PROTOCOL_MAJOR; |
580 | prot_major = PROTOCOL_MAJOR; |
570 | prot_minor = PROTOCOL_MINOR; |
581 | prot_minor = PROTOCOL_MINOR; |
571 | randsize = RAND_SIZE; |
582 | randsize = RAND_SIZE; |
572 | hmaclen = HMACLENGTH; |
583 | hmaclen = HMACLENGTH; |
… | |
… | |
580 | |
591 | |
581 | len = sizeof (*this) - sizeof (net_packet); |
592 | len = sizeof (*this) - sizeof (net_packet); |
582 | set_hdr (type, dst); |
593 | set_hdr (type, dst); |
583 | } |
594 | } |
584 | |
595 | |
|
|
596 | bool |
585 | bool config_packet::chk_config () const |
597 | config_packet::chk_config () const |
586 | { |
598 | { |
587 | if (prot_major != PROTOCOL_MAJOR) |
599 | if (prot_major != PROTOCOL_MAJOR) |
588 | 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); |
589 | else if (randsize != RAND_SIZE) |
601 | else if (randsize != RAND_SIZE) |
590 | 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); |
… | |
… | |
680 | if (ictx && octx) |
692 | if (ictx && octx) |
681 | { |
693 | { |
682 | // make sure rekeying timeouts are slightly asymmetric |
694 | // make sure rekeying timeouts are slightly asymmetric |
683 | 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); |
684 | rekey.start (rekey_interval, rekey_interval); |
696 | rekey.start (rekey_interval, rekey_interval); |
|
|
697 | |
685 | keepalive.start (::conf.keepalive); |
698 | keepalive.start (::conf.keepalive); |
686 | |
699 | |
687 | // send queued packets |
700 | // send queued packets |
688 | if (ictx && octx) |
701 | if (ictx && octx) |
689 | { |
702 | { |
… | |
… | |
819 | } |
832 | } |
820 | |
833 | |
821 | void |
834 | void |
822 | connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) |
835 | connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) |
823 | { |
836 | { |
824 | slog (L_TRACE, "%s << PT_CONNECT_INFO(%s,%s)", conf->nodename, |
837 | slog (L_TRACE, "%s << PT_CONNECT_INFO(%s,%s,p%02x)", conf->nodename, |
825 | vpn->conns[rid - 1]->conf->nodename, (const char *)rsi); |
838 | vpn->conns[rid - 1]->conf->nodename, (const char *)rsi, |
|
|
839 | conf->protocols); |
826 | |
840 | |
827 | connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); |
841 | connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); |
828 | |
842 | |
829 | r->hmac_set (octx); |
843 | r->hmac_set (octx); |
830 | send_vpn_packet (r, si); |
844 | send_vpn_packet (r, si); |
… | |
… | |
833 | } |
847 | } |
834 | |
848 | |
835 | inline void |
849 | inline void |
836 | connection::establish_connection_cb (ev::timer &w, int revents) |
850 | connection::establish_connection_cb (ev::timer &w, int revents) |
837 | { |
851 | { |
838 | if (!ictx |
852 | if (!(ictx && octx) |
839 | && conf != THISNODE |
853 | && conf != THISNODE |
840 | && connectmode != conf_node::C_NEVER |
854 | && connectmode != conf_node::C_NEVER |
841 | && connectmode != conf_node::C_DISABLED |
855 | && connectmode != conf_node::C_DISABLED |
842 | && !w.is_active ()) |
856 | && !w.is_active ()) |
843 | { |
857 | { |
… | |
… | |
911 | } |
925 | } |
912 | } |
926 | } |
913 | |
927 | |
914 | delete ictx; ictx = 0; |
928 | delete ictx; ictx = 0; |
915 | delete octx; octx = 0; |
929 | delete octx; octx = 0; |
916 | #if ENABLE_DNS |
|
|
917 | dnsv4_reset_connection (); |
|
|
918 | #endif |
|
|
919 | |
930 | |
920 | si.host = 0; |
931 | si.host = 0; |
921 | |
932 | |
922 | last_activity = 0.; |
933 | last_activity = 0.; |
923 | //last_si_change = 0.; |
934 | //last_si_change = 0.; |
… | |
… | |
984 | data_queue.put (new tap_packet (*pkt)); |
995 | data_queue.put (new tap_packet (*pkt)); |
985 | post_inject_queue (); |
996 | post_inject_queue (); |
986 | } |
997 | } |
987 | } |
998 | } |
988 | |
999 | |
|
|
1000 | void |
989 | void connection::inject_vpn_packet (vpn_packet *pkt, int tos) |
1001 | connection::inject_vpn_packet (vpn_packet *pkt, int tos) |
990 | { |
1002 | { |
991 | if (ictx && octx) |
1003 | if (ictx && octx) |
992 | send_vpn_packet (pkt, si, tos); |
1004 | send_vpn_packet (pkt, si, tos); |
993 | else |
1005 | else |
994 | { |
1006 | { |
… | |
… | |
1000 | void |
1012 | void |
1001 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
1013 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
1002 | { |
1014 | { |
1003 | last_activity = ev_now (); |
1015 | last_activity = ev_now (); |
1004 | |
1016 | |
1005 | slog (L_NOISE, "%s >> received packet type %d from %d to %d.", |
1017 | slog (L_NOISE, "%s >> received packet type %d from %d to %d.", |
1006 | conf->nodename, pkt->typ (), pkt->src (), pkt->dst ()); |
1018 | conf->nodename, pkt->typ (), pkt->src (), pkt->dst ()); |
1007 | |
1019 | |
1008 | if (connectmode == conf_node::C_DISABLED) |
1020 | if (connectmode == conf_node::C_DISABLED) |
1009 | return; |
1021 | return; |
1010 | |
1022 | |
… | |
… | |
1020 | { |
1032 | { |
1021 | if (auth_rate_limiter.can (rsi)) |
1033 | if (auth_rate_limiter.can (rsi)) |
1022 | send_auth_request (rsi, true); |
1034 | send_auth_request (rsi, true); |
1023 | } |
1035 | } |
1024 | else |
1036 | else |
1025 | // we would love to change thre socket address here, but ping's aren't |
1037 | // we would love to change the socket address here, but ping's aren't |
1026 | // authenticated, so we best ignore it. |
1038 | // authenticated, so we best ignore it. |
1027 | send_ping (rsi, 1); // pong |
1039 | send_ping (rsi, 1); // pong |
1028 | |
1040 | |
1029 | break; |
1041 | break; |
1030 | |
1042 | |
1031 | case vpn_packet::PT_PONG: |
1043 | case vpn_packet::PT_PONG: |
1032 | slog (L_TRACE, "%s >> PT_PONG", conf->nodename); |
1044 | slog (L_TRACE, "%s >> PT_PONG", conf->nodename); |
|
|
1045 | |
|
|
1046 | // a PONG might mean that the other side doesn't really know |
|
|
1047 | // about our desire for communication. |
|
|
1048 | establish_connection (); |
1033 | break; |
1049 | break; |
1034 | |
1050 | |
1035 | case vpn_packet::PT_RESET: |
1051 | case vpn_packet::PT_RESET: |
1036 | { |
1052 | { |
1037 | reset_connection (); |
1053 | reset_connection (); |
… | |
… | |
1050 | break; |
1066 | break; |
1051 | |
1067 | |
1052 | case vpn_packet::PT_AUTH_REQ: |
1068 | case vpn_packet::PT_AUTH_REQ: |
1053 | if (auth_rate_limiter.can (rsi)) |
1069 | if (auth_rate_limiter.can (rsi)) |
1054 | { |
1070 | { |
1055 | auth_req_packet *p = (auth_req_packet *) pkt; |
1071 | auth_req_packet *p = (auth_req_packet *)pkt; |
1056 | |
1072 | |
1057 | slog (L_TRACE, "%s >> PT_AUTH_REQ(%s)", conf->nodename, p->initiate ? "initiate" : "reply"); |
1073 | slog (L_TRACE, "%s >> PT_AUTH_REQ(%s,p%02x,f%02x)", |
|
|
1074 | conf->nodename, p->initiate ? "initiate" : "reply", |
|
|
1075 | p->protocols, p->features); |
1058 | |
1076 | |
1059 | if (p->chk_config () |
1077 | if (p->chk_config () |
1060 | && (!strncmp (p->magic, MAGIC_OLD, 8) || !strncmp (p->magic, MAGIC, 8))) |
1078 | && (!memcmp (p->magic, MAGIC_OLD, 8) || !memcmp (p->magic, MAGIC, 8))) |
1061 | { |
1079 | { |
1062 | if (p->prot_minor != PROTOCOL_MINOR) |
1080 | if (p->prot_minor != PROTOCOL_MINOR) |
1063 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
1081 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
1064 | conf->nodename, (const char *)rsi, |
1082 | conf->nodename, (const char *)rsi, |
1065 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
1083 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
… | |
… | |
1257 | if (p->id > 0 && p->id <= vpn->conns.size ()) |
1275 | if (p->id > 0 && p->id <= vpn->conns.size ()) |
1258 | { |
1276 | { |
1259 | connection *c = vpn->conns[p->id - 1]; |
1277 | connection *c = vpn->conns[p->id - 1]; |
1260 | conf->protocols = p->protocols; |
1278 | conf->protocols = p->protocols; |
1261 | |
1279 | |
1262 | slog (L_TRACE, "%s >> PT_CONNECT_REQ(%s) [%d]", |
1280 | slog (L_TRACE, "%s >> PT_CONNECT_REQ(%s,p%02x) [%d]", |
1263 | conf->nodename, vpn->conns[p->id - 1]->conf->nodename, c->ictx && c->octx); |
1281 | conf->nodename, vpn->conns[p->id - 1]->conf->nodename, |
|
|
1282 | p->protocols, |
|
|
1283 | c->ictx && c->octx); |
1264 | |
1284 | |
1265 | if (c->ictx && c->octx) |
1285 | if (c->ictx && c->octx) |
1266 | { |
1286 | { |
1267 | // send connect_info packets to both sides, in case one is |
1287 | // send connect_info packets to both sides, in case one is |
1268 | // behind a nat firewall (or both ;) |
1288 | // behind a nat firewall (or both ;) |
… | |
… | |
1291 | |
1311 | |
1292 | c->conf->protocols = p->protocols; |
1312 | c->conf->protocols = p->protocols; |
1293 | protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); |
1313 | protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); |
1294 | p->si.upgrade_protocol (protocol, c->conf); |
1314 | p->si.upgrade_protocol (protocol, c->conf); |
1295 | |
1315 | |
1296 | slog (L_TRACE, "%s >> PT_CONNECT_INFO(%s,%s) [%d]", |
1316 | slog (L_TRACE, "%s >> PT_CONNECT_INFO(%s,%s,protocols=%02x,protocol=%02x,upgradable=%02x) [%d]", |
|
|
1317 | conf->nodename, |
1297 | conf->nodename, vpn->conns[p->id - 1]->conf->nodename, |
1318 | vpn->conns[p->id - 1]->conf->nodename, |
|
|
1319 | (const char *)p->si, |
|
|
1320 | p->protocols, |
|
|
1321 | protocol, |
|
|
1322 | p->si.supported_protocols (c->conf), |
1298 | (const char *)p->si, !c->ictx && !c->octx); |
1323 | !c->ictx && !c->octx); |
1299 | |
1324 | |
1300 | const sockinfo &dsi = forward_si (p->si); |
1325 | const sockinfo &dsi = forward_si (p->si); |
1301 | |
1326 | |
1302 | if (dsi.valid ()) |
1327 | if (dsi.valid ()) |
1303 | c->send_auth_request (dsi, true); |
1328 | c->send_auth_request (dsi, true); |
|
|
1329 | else |
|
|
1330 | slog (L_INFO, "connect info for %s received (%s), but still unable to contact.", |
|
|
1331 | vpn->conns[p->id - 1]->conf->nodename, |
|
|
1332 | (const char *)p->si); |
1304 | } |
1333 | } |
1305 | else |
1334 | else |
1306 | slog (L_WARN, |
1335 | slog (L_WARN, |
1307 | _("received authenticated connection request from unknown node #%d, config file mismatch?"), |
1336 | _("received authenticated connection request from unknown node #%d, config file mismatch?"), |
1308 | p->id); |
1337 | p->id); |
… | |
… | |
1317 | } |
1346 | } |
1318 | |
1347 | |
1319 | inline void |
1348 | inline void |
1320 | connection::keepalive_cb (ev::timer &w, int revents) |
1349 | connection::keepalive_cb (ev::timer &w, int revents) |
1321 | { |
1350 | { |
1322 | if (ev_now () >= last_activity + ::conf.keepalive + 15) |
1351 | ev_tstamp when = last_activity + ::conf.keepalive - ev::now (); |
|
|
1352 | |
|
|
1353 | if (when >= 0) |
|
|
1354 | w.start (when); |
|
|
1355 | else if (when < -15) |
1323 | { |
1356 | { |
1324 | reset_connection (); |
1357 | reset_connection (); |
1325 | establish_connection (); |
1358 | establish_connection (); |
1326 | } |
1359 | } |
1327 | else if (ev_now () < last_activity + ::conf.keepalive) |
|
|
1328 | w.start (last_activity + ::conf.keepalive - ev::now ()); |
|
|
1329 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1360 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1330 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1361 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1331 | { |
1362 | { |
|
|
1363 | w.start (3); |
1332 | send_ping (si); |
1364 | send_ping (si); |
1333 | w.start (3); |
|
|
1334 | } |
1365 | } |
1335 | else if (ev_now () < last_activity + ::conf.keepalive + 10) |
1366 | else if (when >= -10) |
1336 | // hold ondemand connections implicitly a few seconds longer |
1367 | // hold ondemand connections implicitly a few seconds longer |
1337 | // should delete octx, though, or something like that ;) |
1368 | // should delete octx, though, or something like that ;) |
1338 | w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); |
1369 | w.start (when + 10); |
1339 | else |
1370 | else |
1340 | reset_connection (); |
1371 | reset_connection (); |
1341 | } |
1372 | } |
1342 | |
1373 | |
|
|
1374 | void |
1343 | void connection::send_connect_request (int id) |
1375 | connection::send_connect_request (int id) |
1344 | { |
1376 | { |
1345 | connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); |
1377 | connect_req_packet *p = new connect_req_packet (conf->id, id, THISNODE->protocols); |
1346 | |
1378 | |
1347 | slog (L_TRACE, "%s << PT_CONNECT_REQ(%s)", |
1379 | slog (L_TRACE, "%s << PT_CONNECT_REQ(%s,p%02x)", |
1348 | conf->nodename, vpn->conns[id - 1]->conf->nodename); |
1380 | conf->nodename, vpn->conns[id - 1]->conf->nodename, |
|
|
1381 | THISNODE->protocols); |
1349 | p->hmac_set (octx); |
1382 | p->hmac_set (octx); |
1350 | send_vpn_packet (p, si); |
1383 | send_vpn_packet (p, si); |
1351 | |
1384 | |
1352 | delete p; |
1385 | delete p; |
1353 | } |
1386 | } |
1354 | |
1387 | |
|
|
1388 | void |
1355 | void connection::script_init_env (const char *ext) |
1389 | connection::script_init_env (const char *ext) |
1356 | { |
1390 | { |
1357 | char *env; |
1391 | char *env; |
1358 | asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); |
1392 | asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); |
1359 | asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); |
1393 | asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); |
1360 | asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, |
1394 | asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, |
1361 | 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, |
1395 | 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, |
1362 | conf->id & 0xff); putenv (env); |
1396 | conf->id & 0xff); putenv (env); |
1363 | } |
1397 | } |
1364 | |
1398 | |
|
|
1399 | void |
1365 | void connection::script_init_connect_env () |
1400 | connection::script_init_connect_env () |
1366 | { |
1401 | { |
1367 | vpn->script_init_env (); |
1402 | vpn->script_init_env (); |
1368 | |
1403 | |
1369 | char *env; |
1404 | char *env; |
1370 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
1405 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
… | |
… | |
1435 | establish_connection.set<connection, &connection::establish_connection_cb> (this); |
1470 | establish_connection.set<connection, &connection::establish_connection_cb> (this); |
1436 | |
1471 | |
1437 | last_establish_attempt = 0.; |
1472 | last_establish_attempt = 0.; |
1438 | octx = ictx = 0; |
1473 | octx = ictx = 0; |
1439 | |
1474 | |
1440 | if (!conf->protocols) // make sure some protocol is enabled |
|
|
1441 | conf->protocols = PROT_UDPv4; |
|
|
1442 | |
|
|
1443 | connectmode = conf->connectmode; |
1475 | connectmode = conf->connectmode; |
1444 | |
1476 | |
1445 | // queue a dummy packet to force an initial connection attempt |
1477 | // queue a dummy packet to force an initial connection attempt |
1446 | if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) |
1478 | if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) |
1447 | vpn_queue.put (new net_packet); |
1479 | vpn_queue.put (new net_packet); |
… | |
… | |
1452 | connection::~connection () |
1484 | connection::~connection () |
1453 | { |
1485 | { |
1454 | shutdown (); |
1486 | shutdown (); |
1455 | } |
1487 | } |
1456 | |
1488 | |
|
|
1489 | void |
1457 | void connection_init () |
1490 | connection_init () |
1458 | { |
1491 | { |
1459 | auth_rate_limiter.clear (); |
1492 | auth_rate_limiter.clear (); |
1460 | reset_rate_limiter.clear (); |
1493 | reset_rate_limiter.clear (); |
1461 | } |
1494 | } |
1462 | |
1495 | |