… | |
… | |
85 | rsachallenge chg; |
85 | rsachallenge chg; |
86 | }; |
86 | }; |
87 | |
87 | |
88 | struct rsa_cache : list<rsa_entry> |
88 | struct rsa_cache : list<rsa_entry> |
89 | { |
89 | { |
90 | void cleaner_cb (tstamp &ts); time_watcher cleaner; |
90 | void cleaner_cb (time_watcher &w); time_watcher cleaner; |
91 | |
91 | |
92 | bool find (const rsaid &id, rsachallenge &chg) |
92 | bool find (const rsaid &id, rsachallenge &chg) |
93 | { |
93 | { |
94 | for (iterator i = begin (); i != end (); ++i) |
94 | for (iterator i = begin (); i != end (); ++i) |
95 | { |
95 | { |
… | |
… | |
129 | : cleaner (this, &rsa_cache::cleaner_cb) |
129 | : cleaner (this, &rsa_cache::cleaner_cb) |
130 | { } |
130 | { } |
131 | |
131 | |
132 | } rsa_cache; |
132 | } rsa_cache; |
133 | |
133 | |
134 | void rsa_cache::cleaner_cb (tstamp &ts) |
134 | void rsa_cache::cleaner_cb (time_watcher &w) |
135 | { |
135 | { |
136 | if (empty ()) |
136 | if (empty ()) |
137 | ts = TSTAMP_CANCEL; |
137 | w.at = TSTAMP_CANCEL; |
138 | else |
138 | else |
139 | { |
139 | { |
140 | ts = NOW + RSA_TTL; |
140 | w.at = NOW + RSA_TTL; |
141 | |
141 | |
142 | for (iterator i = begin (); i != end (); ) |
142 | for (iterator i = begin (); i != end (); ) |
143 | if (i->expire <= NOW) |
143 | if (i->expire <= NOW) |
144 | i = erase (i); |
144 | i = erase (i); |
145 | else |
145 | else |
… | |
… | |
197 | // only do action once every x seconds per host whole allowing bursts. |
197 | // only do action once every x seconds per host whole allowing bursts. |
198 | // this implementation ("splay list" ;) is inefficient, |
198 | // this implementation ("splay list" ;) is inefficient, |
199 | // but low on resources. |
199 | // but low on resources. |
200 | struct net_rate_limiter : list<net_rateinfo> |
200 | struct net_rate_limiter : list<net_rateinfo> |
201 | { |
201 | { |
202 | static const double ALPHA = 1. - 1. / 90.; // allow bursts |
202 | static const double ALPHA = 1. - 1. / 180.; // allow bursts |
203 | static const double CUTOFF = 20.; // one event every CUTOFF seconds |
203 | static const double CUTOFF = 10.; // one event every CUTOFF seconds |
204 | static const double EXPIRE = CUTOFF * 30.; // expire entries after this time |
204 | static const double EXPIRE = CUTOFF * 30.; // expire entries after this time |
|
|
205 | static const double MAXDIF = CUTOFF * (1. / (1. - ALPHA)); // maximum diff /count value |
205 | |
206 | |
206 | bool can (const sockinfo &si) { return can((u32)si.host); } |
207 | bool can (const sockinfo &si) { return can((u32)si.host); } |
207 | bool can (u32 host); |
208 | bool can (u32 host); |
208 | }; |
209 | }; |
209 | |
210 | |
… | |
… | |
225 | { |
226 | { |
226 | net_rateinfo ri; |
227 | net_rateinfo ri; |
227 | |
228 | |
228 | ri.host = host; |
229 | ri.host = host; |
229 | ri.pcnt = 1.; |
230 | ri.pcnt = 1.; |
230 | ri.diff = CUTOFF * (1. / (1. - ALPHA)); |
231 | ri.diff = MAXDIF; |
231 | ri.last = NOW; |
232 | ri.last = NOW; |
232 | |
233 | |
233 | push_front (ri); |
234 | push_front (ri); |
234 | |
235 | |
235 | return true; |
236 | return true; |
… | |
… | |
242 | ri.pcnt = ri.pcnt * ALPHA; |
243 | ri.pcnt = ri.pcnt * ALPHA; |
243 | ri.diff = ri.diff * ALPHA + (NOW - ri.last); |
244 | ri.diff = ri.diff * ALPHA + (NOW - ri.last); |
244 | |
245 | |
245 | ri.last = NOW; |
246 | ri.last = NOW; |
246 | |
247 | |
|
|
248 | double dif = ri.diff / ri.pcnt; |
|
|
249 | |
247 | bool send = ri.diff / ri.pcnt > CUTOFF; |
250 | bool send = dif > CUTOFF; |
248 | |
251 | |
|
|
252 | if (dif > MAXDIF) |
|
|
253 | { |
|
|
254 | ri.pcnt = 1.; |
|
|
255 | ri.diff = MAXDIF; |
|
|
256 | } |
249 | if (send) |
257 | else if (send) |
250 | ri.pcnt++; |
258 | ri.pcnt++; |
251 | |
259 | |
252 | push_front (ri); |
260 | push_front (ri); |
253 | |
261 | |
254 | return send; |
262 | return send; |
… | |
… | |
285 | hmac_gen (ctx); |
293 | hmac_gen (ctx); |
286 | |
294 | |
287 | return !memcmp (hmac, hmac_digest, HMACLENGTH); |
295 | return !memcmp (hmac, hmac_digest, HMACLENGTH); |
288 | } |
296 | } |
289 | |
297 | |
290 | void vpn_packet::set_hdr (ptype type, unsigned int dst) |
298 | void vpn_packet::set_hdr (ptype type_, unsigned int dst) |
291 | { |
299 | { |
292 | this->type = type; |
300 | type = type_; |
293 | |
301 | |
294 | int src = THISNODE->id; |
302 | int src = THISNODE->id; |
295 | |
303 | |
296 | src1 = src; |
304 | src1 = src; |
297 | srcdst = ((src >> 8) << 4) | (dst >> 8); |
305 | srcdst = ((src >> 8) << 4) | (dst >> 8); |
… | |
… | |
546 | }; |
554 | }; |
547 | |
555 | |
548 | ///////////////////////////////////////////////////////////////////////////// |
556 | ///////////////////////////////////////////////////////////////////////////// |
549 | |
557 | |
550 | void |
558 | void |
|
|
559 | connection::connection_established () |
|
|
560 | { |
|
|
561 | if (ictx && octx) |
|
|
562 | { |
|
|
563 | connectmode = conf->connectmode; |
|
|
564 | |
|
|
565 | rekey.start (NOW + ::conf.rekey); |
|
|
566 | keepalive.start (NOW + ::conf.keepalive); |
|
|
567 | |
|
|
568 | // send queued packets |
|
|
569 | if (ictx && octx) |
|
|
570 | while (tap_packet *p = queue.get ()) |
|
|
571 | { |
|
|
572 | send_data_packet (p); |
|
|
573 | delete p; |
|
|
574 | } |
|
|
575 | } |
|
|
576 | else |
|
|
577 | { |
|
|
578 | retry_cnt = 0; |
|
|
579 | establish_connection.start (NOW + 5); |
|
|
580 | keepalive.reset (); |
|
|
581 | rekey.reset (); |
|
|
582 | } |
|
|
583 | } |
|
|
584 | |
|
|
585 | void |
551 | connection::reset_dstaddr () |
586 | connection::reset_si () |
552 | { |
587 | { |
553 | si.set (conf); |
|
|
554 | } |
|
|
555 | |
|
|
556 | void |
|
|
557 | connection::send_ping (const sockinfo &si, u8 pong) |
|
|
558 | { |
|
|
559 | ping_packet *pkt = new ping_packet; |
|
|
560 | |
|
|
561 | pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING); |
|
|
562 | send_vpn_packet (pkt, si, IPTOS_LOWDELAY); |
|
|
563 | |
|
|
564 | delete pkt; |
|
|
565 | } |
|
|
566 | |
|
|
567 | void |
|
|
568 | connection::send_reset (const sockinfo &si) |
|
|
569 | { |
|
|
570 | if (reset_rate_limiter.can (si) && connectmode != conf_node::C_DISABLED) |
|
|
571 | { |
|
|
572 | config_packet *pkt = new config_packet; |
|
|
573 | |
|
|
574 | pkt->setup (vpn_packet::PT_RESET, conf->id); |
|
|
575 | send_vpn_packet (pkt, si, IPTOS_MINCOST); |
|
|
576 | |
|
|
577 | delete pkt; |
|
|
578 | } |
|
|
579 | } |
|
|
580 | |
|
|
581 | void |
|
|
582 | connection::send_auth_request (const sockinfo &si, bool initiate) |
|
|
583 | { |
|
|
584 | auth_req_packet *pkt = new auth_req_packet (conf->id, initiate, THISNODE->protocols); |
|
|
585 | |
|
|
586 | protocol = best_protocol (THISNODE->protocols & conf->protocols); |
588 | protocol = best_protocol (THISNODE->protocols & conf->protocols); |
587 | |
589 | |
588 | // mask out protocols we cannot establish |
590 | // mask out protocols we cannot establish |
589 | if (!conf->udp_port) protocol &= ~PROT_UDPv4; |
591 | if (!conf->udp_port) protocol &= ~PROT_UDPv4; |
590 | if (!conf->tcp_port) protocol &= ~PROT_TCPv4; |
592 | if (!conf->tcp_port) protocol &= ~PROT_TCPv4; |
591 | |
593 | |
592 | if (protocol) |
594 | si.set (conf, protocol); |
|
|
595 | } |
|
|
596 | |
|
|
597 | // ensure sockinfo is valid, forward if necessary |
|
|
598 | const sockinfo & |
|
|
599 | connection::forward_si (const sockinfo &si) const |
|
|
600 | { |
|
|
601 | if (!si.valid ()) |
|
|
602 | { |
|
|
603 | connection *r = vpn->find_router (); |
|
|
604 | |
|
|
605 | if (r) |
|
|
606 | { |
|
|
607 | slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s"), |
|
|
608 | conf->nodename, r->conf->nodename); |
|
|
609 | return r->si; |
|
|
610 | } |
|
|
611 | else |
|
|
612 | slog (L_DEBUG, _("%s: node unreachable, no common protocol"), |
|
|
613 | conf->nodename); |
593 | { |
614 | } |
594 | rsachallenge chg; |
|
|
595 | |
615 | |
596 | rsa_cache.gen (pkt->id, chg); |
616 | return si; |
|
|
617 | } |
597 | |
618 | |
598 | if (0 > RSA_public_encrypt (sizeof chg, |
619 | void |
599 | (unsigned char *)&chg, (unsigned char *)&pkt->encr, |
620 | connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
600 | conf->rsa_key, RSA_PKCS1_OAEP_PADDING)) |
621 | { |
601 | fatal ("RSA_public_encrypt error"); |
622 | if (!vpn->send_vpn_packet (pkt, si, tos)) |
|
|
623 | reset_connection (); |
|
|
624 | } |
602 | |
625 | |
603 | slog (L_TRACE, ">>%d PT_AUTH_REQ [%s]", conf->id, (const char *)si); |
626 | void |
|
|
627 | connection::send_ping (const sockinfo &si, u8 pong) |
|
|
628 | { |
|
|
629 | ping_packet *pkt = new ping_packet; |
604 | |
630 | |
605 | send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly |
631 | pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING); |
|
|
632 | send_vpn_packet (pkt, si, IPTOS_LOWDELAY); |
|
|
633 | |
|
|
634 | delete pkt; |
|
|
635 | } |
|
|
636 | |
|
|
637 | void |
|
|
638 | connection::send_reset (const sockinfo &si) |
|
|
639 | { |
|
|
640 | if (reset_rate_limiter.can (si) && connectmode != conf_node::C_DISABLED) |
|
|
641 | { |
|
|
642 | config_packet *pkt = new config_packet; |
|
|
643 | |
|
|
644 | pkt->setup (vpn_packet::PT_RESET, conf->id); |
|
|
645 | send_vpn_packet (pkt, si, IPTOS_MINCOST); |
606 | |
646 | |
607 | delete pkt; |
647 | delete pkt; |
608 | } |
648 | } |
609 | else |
649 | } |
610 | ; // silently fail |
650 | |
|
|
651 | void |
|
|
652 | connection::send_auth_request (const sockinfo &si, bool initiate) |
|
|
653 | { |
|
|
654 | auth_req_packet *pkt = new auth_req_packet (conf->id, initiate, THISNODE->protocols); |
|
|
655 | |
|
|
656 | rsachallenge chg; |
|
|
657 | |
|
|
658 | rsa_cache.gen (pkt->id, chg); |
|
|
659 | |
|
|
660 | if (0 > RSA_public_encrypt (sizeof chg, |
|
|
661 | (unsigned char *)&chg, (unsigned char *)&pkt->encr, |
|
|
662 | conf->rsa_key, RSA_PKCS1_OAEP_PADDING)) |
|
|
663 | fatal ("RSA_public_encrypt error"); |
|
|
664 | |
|
|
665 | slog (L_TRACE, ">>%d PT_AUTH_REQ [%s]", conf->id, (const char *)si); |
|
|
666 | |
|
|
667 | send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly |
|
|
668 | |
|
|
669 | delete pkt; |
611 | } |
670 | } |
612 | |
671 | |
613 | void |
672 | void |
614 | connection::send_auth_response (const sockinfo &si, const rsaid &id, const rsachallenge &chg) |
673 | connection::send_auth_response (const sockinfo &si, const rsaid &id, const rsachallenge &chg) |
615 | { |
674 | { |
… | |
… | |
641 | |
700 | |
642 | delete r; |
701 | delete r; |
643 | } |
702 | } |
644 | |
703 | |
645 | void |
704 | void |
646 | connection::establish_connection_cb (tstamp &ts) |
705 | connection::establish_connection_cb (time_watcher &w) |
647 | { |
706 | { |
648 | if (ictx || conf == THISNODE |
707 | if (ictx || conf == THISNODE |
649 | || connectmode == conf_node::C_NEVER |
708 | || connectmode == conf_node::C_NEVER |
650 | || connectmode == conf_node::C_DISABLED) |
709 | || connectmode == conf_node::C_DISABLED) |
651 | ts = TSTAMP_CANCEL; |
710 | w.at = TSTAMP_CANCEL; |
652 | else if (ts <= NOW) |
711 | else if (w.at <= NOW) |
653 | { |
712 | { |
654 | double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; |
713 | double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; |
655 | |
714 | |
656 | if (retry_int < 3600 * 8) |
715 | if (retry_int < 3600 * 8) |
657 | retry_cnt++; |
716 | retry_cnt++; |
658 | |
717 | |
659 | ts = NOW + retry_int; |
718 | w.at = NOW + retry_int; |
660 | |
719 | |
661 | if (conf->hostname) |
720 | reset_si (); |
|
|
721 | |
|
|
722 | if (si.prot && !si.host) |
|
|
723 | vpn->send_connect_request (conf->id); |
|
|
724 | else |
662 | { |
725 | { |
663 | reset_dstaddr (); |
726 | const sockinfo &dsi = forward_si (si); |
|
|
727 | |
664 | if (si.host && auth_rate_limiter.can (si)) |
728 | if (dsi.valid () && auth_rate_limiter.can (dsi)) |
665 | { |
729 | { |
666 | if (retry_cnt < 4) |
730 | if (retry_cnt < 4) |
667 | send_auth_request (si, true); |
731 | send_auth_request (dsi, true); |
668 | else |
732 | else |
669 | send_ping (si, 0); |
733 | send_ping (dsi, 0); |
670 | } |
734 | } |
671 | } |
735 | } |
672 | else |
|
|
673 | vpn->connect_request (conf->id); |
|
|
674 | } |
736 | } |
675 | } |
737 | } |
676 | |
738 | |
677 | void |
739 | void |
678 | connection::reset_connection () |
740 | connection::reset_connection () |
… | |
… | |
707 | |
769 | |
708 | reset_connection (); |
770 | reset_connection (); |
709 | } |
771 | } |
710 | |
772 | |
711 | void |
773 | void |
712 | connection::rekey_cb (tstamp &ts) |
774 | connection::rekey_cb (time_watcher &w) |
713 | { |
775 | { |
714 | ts = TSTAMP_CANCEL; |
776 | w.at = TSTAMP_CANCEL; |
715 | |
777 | |
716 | reset_connection (); |
778 | reset_connection (); |
717 | establish_connection (); |
779 | establish_connection (); |
718 | } |
780 | } |
719 | |
781 | |
… | |
… | |
747 | if (!broadcast)//DDDD |
809 | if (!broadcast)//DDDD |
748 | queue.put (new tap_packet (*pkt)); |
810 | queue.put (new tap_packet (*pkt)); |
749 | |
811 | |
750 | establish_connection (); |
812 | establish_connection (); |
751 | } |
813 | } |
|
|
814 | } |
|
|
815 | |
|
|
816 | void connection::inject_vpn_packet (vpn_packet *pkt, int tos) |
|
|
817 | { |
|
|
818 | if (ictx && octx) |
|
|
819 | send_vpn_packet (pkt, si, tos); |
|
|
820 | else |
|
|
821 | establish_connection (); |
752 | } |
822 | } |
753 | |
823 | |
754 | void |
824 | void |
755 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
825 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
756 | { |
826 | { |
… | |
… | |
819 | ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING)) |
889 | ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING)) |
820 | slog (L_ERR, _("%s(%s): challenge illegal or corrupted"), |
890 | slog (L_ERR, _("%s(%s): challenge illegal or corrupted"), |
821 | conf->nodename, (const char *)rsi); |
891 | conf->nodename, (const char *)rsi); |
822 | else |
892 | else |
823 | { |
893 | { |
824 | retry_cnt = 0; |
|
|
825 | establish_connection.set (NOW + 8); //? ;) |
|
|
826 | keepalive.reset (); |
|
|
827 | rekey.reset (); |
|
|
828 | |
|
|
829 | delete ictx; |
|
|
830 | ictx = 0; |
|
|
831 | |
|
|
832 | delete octx; |
894 | delete octx; |
833 | |
895 | |
834 | octx = new crypto_ctx (k, 1); |
896 | octx = new crypto_ctx (k, 1); |
835 | oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; |
897 | oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; |
836 | |
898 | |
837 | conf->protocols = p->protocols; |
899 | conf->protocols = p->protocols; |
|
|
900 | |
838 | send_auth_response (rsi, p->id, k); |
901 | send_auth_response (rsi, p->id, k); |
|
|
902 | |
|
|
903 | connection_established (); |
839 | |
904 | |
840 | break; |
905 | break; |
841 | } |
906 | } |
842 | } |
907 | } |
843 | |
908 | |
… | |
… | |
885 | delete ictx; ictx = cctx; |
950 | delete ictx; ictx = cctx; |
886 | |
951 | |
887 | iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid |
952 | iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid |
888 | |
953 | |
889 | si = rsi; |
954 | si = rsi; |
|
|
955 | protocol = rsi.prot; |
890 | |
956 | |
891 | rekey.set (NOW + ::conf.rekey); |
957 | connection_established (); |
892 | keepalive.set (NOW + ::conf.keepalive); |
|
|
893 | |
958 | |
894 | // send queued packets |
|
|
895 | while (tap_packet *p = queue.get ()) |
|
|
896 | { |
|
|
897 | send_data_packet (p); |
|
|
898 | delete p; |
|
|
899 | } |
|
|
900 | |
|
|
901 | connectmode = conf->connectmode; |
|
|
902 | |
|
|
903 | slog (L_INFO, _("%s(%s): %s connection established, protocol version %d.%d"), |
959 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
904 | conf->nodename, (const char *)rsi, |
960 | conf->nodename, (const char *)rsi, |
905 | strprotocol (protocol), |
|
|
906 | p->prot_major, p->prot_minor); |
961 | p->prot_major, p->prot_minor); |
907 | |
962 | |
908 | if (::conf.script_node_up) |
963 | if (::conf.script_node_up) |
909 | run_script (run_script_cb (this, &connection::script_node_up), false); |
964 | run_script (run_script_cb (this, &connection::script_node_up), false); |
910 | |
965 | |
… | |
… | |
933 | |
988 | |
934 | if (ictx && octx) |
989 | if (ictx && octx) |
935 | { |
990 | { |
936 | vpndata_packet *p = (vpndata_packet *)pkt; |
991 | vpndata_packet *p = (vpndata_packet *)pkt; |
937 | |
992 | |
938 | if (rsi == si) |
993 | if (!p->hmac_chk (ictx)) |
|
|
994 | slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" |
|
|
995 | "could be an attack, or just corruption or an synchronization error"), |
|
|
996 | conf->nodename, (const char *)rsi); |
|
|
997 | else |
939 | { |
998 | { |
940 | if (!p->hmac_chk (ictx)) |
|
|
941 | slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" |
|
|
942 | "could be an attack, or just corruption or an synchronization error"), |
|
|
943 | conf->nodename, (const char *)rsi); |
|
|
944 | else |
999 | u32 seqno; |
|
|
1000 | tap_packet *d = p->unpack (this, seqno); |
|
|
1001 | |
|
|
1002 | if (iseqno.recv_ok (seqno)) |
945 | { |
1003 | { |
946 | u32 seqno; |
1004 | vpn->tap->send (d); |
947 | tap_packet *d = p->unpack (this, seqno); |
|
|
948 | |
1005 | |
949 | if (iseqno.recv_ok (seqno)) |
1006 | if (p->dst () == 0) // re-broadcast |
|
|
1007 | for (vpn::conns_vector::iterator i = vpn->conns.begin (); i != vpn->conns.end (); ++i) |
|
|
1008 | { |
|
|
1009 | connection *c = *i; |
|
|
1010 | |
|
|
1011 | if (c->conf != THISNODE && c->conf != conf) |
|
|
1012 | c->inject_data_packet (d); |
|
|
1013 | } |
|
|
1014 | |
|
|
1015 | if (si != rsi) |
950 | { |
1016 | { |
951 | vpn->tap->send (d); |
1017 | // fast re-sync on conneciton changes, useful especially for tcp/ip |
952 | |
|
|
953 | if (p->dst () == 0) // re-broadcast |
|
|
954 | for (vpn::conns_vector::iterator i = vpn->conns.begin (); i != vpn->conns.end (); ++i) |
|
|
955 | { |
|
|
956 | connection *c = *i; |
|
|
957 | |
|
|
958 | if (c->conf != THISNODE && c->conf != conf) |
|
|
959 | c->inject_data_packet (d); |
|
|
960 | } |
|
|
961 | |
|
|
962 | delete d; |
|
|
963 | |
|
|
964 | break; |
1018 | si = rsi; |
|
|
1019 | |
|
|
1020 | slog (L_INFO, _("%s(%s): socket address changed to %s"), |
|
|
1021 | conf->nodename, (const char *)si, (const char *)rsi); |
965 | } |
1022 | } |
|
|
1023 | |
|
|
1024 | delete d; |
|
|
1025 | |
|
|
1026 | break; |
966 | } |
1027 | } |
967 | } |
1028 | } |
968 | else |
|
|
969 | slog (L_ERR, _("received data packet from unknown source %s"), (const char *)rsi); |
|
|
970 | } |
1029 | } |
971 | |
1030 | |
972 | send_reset (rsi); |
1031 | send_reset (rsi); |
973 | break; |
1032 | break; |
974 | |
1033 | |
… | |
… | |
976 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1035 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
977 | { |
1036 | { |
978 | connect_req_packet *p = (connect_req_packet *) pkt; |
1037 | connect_req_packet *p = (connect_req_packet *) pkt; |
979 | |
1038 | |
980 | assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything |
1039 | assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything |
|
|
1040 | connection *c = vpn->conns[p->id - 1]; |
981 | conf->protocols = p->protocols; |
1041 | conf->protocols = p->protocols; |
982 | connection *c = vpn->conns[p->id - 1]; |
|
|
983 | |
1042 | |
984 | slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]\n", |
1043 | slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]\n", |
985 | conf->id, p->id, c->ictx && c->octx); |
1044 | conf->id, p->id, c->ictx && c->octx); |
986 | |
1045 | |
987 | if (c->ictx && c->octx) |
1046 | if (c->ictx && c->octx) |
… | |
… | |
999 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1058 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1000 | { |
1059 | { |
1001 | connect_info_packet *p = (connect_info_packet *) pkt; |
1060 | connect_info_packet *p = (connect_info_packet *) pkt; |
1002 | |
1061 | |
1003 | assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything |
1062 | assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything |
1004 | conf->protocols = p->protocols; |
1063 | |
1005 | connection *c = vpn->conns[p->id - 1]; |
1064 | connection *c = vpn->conns[p->id - 1]; |
|
|
1065 | |
|
|
1066 | c->conf->protocols = p->protocols; |
|
|
1067 | protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); |
|
|
1068 | p->si.upgrade_protocol (protocol, c->conf); |
1006 | |
1069 | |
1007 | slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", |
1070 | slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", |
1008 | conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); |
1071 | conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); |
1009 | |
1072 | |
|
|
1073 | const sockinfo &dsi = forward_si (p->si); |
|
|
1074 | |
|
|
1075 | if (dsi.valid ()) |
1010 | c->send_auth_request (p->si, true); |
1076 | c->send_auth_request (dsi, true); |
1011 | } |
1077 | } |
1012 | |
1078 | |
1013 | break; |
1079 | break; |
1014 | |
1080 | |
1015 | default: |
1081 | default: |
1016 | send_reset (rsi); |
1082 | send_reset (rsi); |
1017 | break; |
1083 | break; |
1018 | } |
1084 | } |
1019 | } |
1085 | } |
1020 | |
1086 | |
1021 | void connection::keepalive_cb (tstamp &ts) |
1087 | void connection::keepalive_cb (time_watcher &w) |
1022 | { |
1088 | { |
1023 | if (NOW >= last_activity + ::conf.keepalive + 30) |
1089 | if (NOW >= last_activity + ::conf.keepalive + 30) |
1024 | { |
1090 | { |
1025 | reset_connection (); |
1091 | reset_connection (); |
1026 | establish_connection (); |
1092 | establish_connection (); |
1027 | } |
1093 | } |
1028 | else if (NOW < last_activity + ::conf.keepalive) |
1094 | else if (NOW < last_activity + ::conf.keepalive) |
1029 | ts = last_activity + ::conf.keepalive; |
1095 | w.at = last_activity + ::conf.keepalive; |
1030 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1096 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1031 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1097 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1032 | { |
1098 | { |
1033 | send_ping (si); |
1099 | send_ping (si); |
1034 | ts = NOW + 5; |
1100 | w.at = NOW + 5; |
1035 | } |
1101 | } |
1036 | else |
1102 | else |
1037 | reset_connection (); |
1103 | reset_connection (); |
1038 | } |
1104 | } |
1039 | |
1105 | |
1040 | void connection::connect_request (int id) |
1106 | void connection::send_connect_request (int id) |
1041 | { |
1107 | { |
1042 | connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); |
1108 | connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); |
1043 | |
1109 | |
1044 | slog (L_TRACE, ">>%d PT_CONNECT_REQ(%d)", conf->id, id); |
1110 | slog (L_TRACE, ">>%d PT_CONNECT_REQ(%d)", conf->id, id); |
1045 | p->hmac_set (octx); |
1111 | p->hmac_set (octx); |
… | |
… | |
1075 | putenv ("STATE=down"); |
1141 | putenv ("STATE=down"); |
1076 | |
1142 | |
1077 | return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; |
1143 | return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; |
1078 | } |
1144 | } |
1079 | |
1145 | |
1080 | // send a vpn packet out to other hosts |
|
|
1081 | void |
|
|
1082 | connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
|
|
1083 | { |
|
|
1084 | switch (protocol) |
|
|
1085 | { |
|
|
1086 | case PROT_IPv4: |
|
|
1087 | vpn->send_ipv4_packet (pkt, si, tos); |
|
|
1088 | break; |
|
|
1089 | |
|
|
1090 | case PROT_UDPv4: |
|
|
1091 | vpn->send_udpv4_packet (pkt, si, tos); |
|
|
1092 | break; |
|
|
1093 | |
|
|
1094 | case PROT_TCPv4: |
|
|
1095 | vpn->send_tcpv4_packet (pkt, si, tos); |
|
|
1096 | break; |
|
|
1097 | } |
|
|
1098 | } |
|
|
1099 | |
|
|
1100 | connection::connection(struct vpn *vpn_) |
1146 | connection::connection(struct vpn *vpn_) |
1101 | : vpn(vpn_) |
1147 | : vpn(vpn_) |
1102 | , rekey (this, &connection::rekey_cb) |
1148 | , rekey (this, &connection::rekey_cb) |
1103 | , keepalive (this, &connection::keepalive_cb) |
1149 | , keepalive (this, &connection::keepalive_cb) |
1104 | , establish_connection (this, &connection::establish_connection_cb) |
1150 | , establish_connection (this, &connection::establish_connection_cb) |