… | |
… | |
35 | #include "slog.h" |
35 | #include "slog.h" |
36 | #include "device.h" |
36 | #include "device.h" |
37 | #include "vpn.h" |
37 | #include "vpn.h" |
38 | #include "connection.h" |
38 | #include "connection.h" |
39 | |
39 | |
|
|
40 | #include "netcompat.h" |
|
|
41 | |
40 | #if !HAVE_RAND_PSEUDO_BYTES |
42 | #if !HAVE_RAND_PSEUDO_BYTES |
41 | # define RAND_pseudo_bytes RAND_bytes |
43 | # define RAND_pseudo_bytes RAND_bytes |
42 | #endif |
44 | #endif |
43 | |
45 | |
44 | #define MAGIC "vped\xbd\xc6\xdb\x82" // 8 bytes of magic |
46 | #define MAGIC "vped\xbd\xc6\xdb\x82" // 8 bytes of magic |
… | |
… | |
85 | rsachallenge chg; |
87 | rsachallenge chg; |
86 | }; |
88 | }; |
87 | |
89 | |
88 | struct rsa_cache : list<rsa_entry> |
90 | struct rsa_cache : list<rsa_entry> |
89 | { |
91 | { |
90 | void cleaner_cb (tstamp &ts); time_watcher cleaner; |
92 | void cleaner_cb (time_watcher &w); time_watcher cleaner; |
91 | |
93 | |
92 | bool find (const rsaid &id, rsachallenge &chg) |
94 | bool find (const rsaid &id, rsachallenge &chg) |
93 | { |
95 | { |
94 | for (iterator i = begin (); i != end (); ++i) |
96 | for (iterator i = begin (); i != end (); ++i) |
95 | { |
97 | { |
… | |
… | |
129 | : cleaner (this, &rsa_cache::cleaner_cb) |
131 | : cleaner (this, &rsa_cache::cleaner_cb) |
130 | { } |
132 | { } |
131 | |
133 | |
132 | } rsa_cache; |
134 | } rsa_cache; |
133 | |
135 | |
134 | void rsa_cache::cleaner_cb (tstamp &ts) |
136 | void rsa_cache::cleaner_cb (time_watcher &w) |
135 | { |
137 | { |
136 | if (empty ()) |
138 | if (empty ()) |
137 | ts = TSTAMP_CANCEL; |
139 | w.at = TSTAMP_CANCEL; |
138 | else |
140 | else |
139 | { |
141 | { |
140 | ts = NOW + RSA_TTL; |
142 | w.at = NOW + RSA_TTL; |
141 | |
143 | |
142 | for (iterator i = begin (); i != end (); ) |
144 | for (iterator i = begin (); i != end (); ) |
143 | if (i->expire <= NOW) |
145 | if (i->expire <= NOW) |
144 | i = erase (i); |
146 | i = erase (i); |
145 | else |
147 | else |
… | |
… | |
147 | } |
149 | } |
148 | } |
150 | } |
149 | |
151 | |
150 | ////////////////////////////////////////////////////////////////////////////// |
152 | ////////////////////////////////////////////////////////////////////////////// |
151 | |
153 | |
152 | void pkt_queue::put (tap_packet *p) |
154 | void pkt_queue::put (net_packet *p) |
153 | { |
155 | { |
154 | if (queue[i]) |
156 | if (queue[i]) |
155 | { |
157 | { |
156 | delete queue[i]; |
158 | delete queue[i]; |
157 | j = (j + 1) % QUEUEDEPTH; |
159 | j = (j + 1) % QUEUEDEPTH; |
… | |
… | |
160 | queue[i] = p; |
162 | queue[i] = p; |
161 | |
163 | |
162 | i = (i + 1) % QUEUEDEPTH; |
164 | i = (i + 1) % QUEUEDEPTH; |
163 | } |
165 | } |
164 | |
166 | |
165 | tap_packet *pkt_queue::get () |
167 | net_packet *pkt_queue::get () |
166 | { |
168 | { |
167 | tap_packet *p = queue[j]; |
169 | net_packet *p = queue[j]; |
168 | |
170 | |
169 | if (p) |
171 | if (p) |
170 | { |
172 | { |
171 | queue[j] = 0; |
173 | queue[j] = 0; |
172 | j = (j + 1) % QUEUEDEPTH; |
174 | j = (j + 1) % QUEUEDEPTH; |
… | |
… | |
197 | // only do action once every x seconds per host whole allowing bursts. |
199 | // only do action once every x seconds per host whole allowing bursts. |
198 | // this implementation ("splay list" ;) is inefficient, |
200 | // this implementation ("splay list" ;) is inefficient, |
199 | // but low on resources. |
201 | // but low on resources. |
200 | struct net_rate_limiter : list<net_rateinfo> |
202 | struct net_rate_limiter : list<net_rateinfo> |
201 | { |
203 | { |
202 | static const double ALPHA = 1. - 1. / 90.; // allow bursts |
204 | static const double ALPHA = 1. - 1. / 600.; // allow bursts |
203 | static const double CUTOFF = 20.; // one event every CUTOFF seconds |
205 | static const double CUTOFF = 10.; // one event every CUTOFF seconds |
204 | static const double EXPIRE = CUTOFF * 30.; // expire entries after this time |
206 | static const double EXPIRE = CUTOFF * 30.; // expire entries after this time |
|
|
207 | static const double MAXDIF = CUTOFF * (1. / (1. - ALPHA)); // maximum diff /count value |
205 | |
208 | |
206 | bool can (const sockinfo &si) { return can((u32)si.host); } |
209 | bool can (const sockinfo &si) { return can((u32)si.host); } |
207 | bool can (u32 host); |
210 | bool can (u32 host); |
208 | }; |
211 | }; |
209 | |
212 | |
210 | net_rate_limiter auth_rate_limiter, reset_rate_limiter; |
213 | net_rate_limiter auth_rate_limiter, reset_rate_limiter; |
211 | |
214 | |
… | |
… | |
225 | { |
228 | { |
226 | net_rateinfo ri; |
229 | net_rateinfo ri; |
227 | |
230 | |
228 | ri.host = host; |
231 | ri.host = host; |
229 | ri.pcnt = 1.; |
232 | ri.pcnt = 1.; |
230 | ri.diff = CUTOFF * (1. / (1. - ALPHA)); |
233 | ri.diff = MAXDIF; |
231 | ri.last = NOW; |
234 | ri.last = NOW; |
232 | |
235 | |
233 | push_front (ri); |
236 | push_front (ri); |
234 | |
237 | |
235 | return true; |
238 | return true; |
… | |
… | |
242 | ri.pcnt = ri.pcnt * ALPHA; |
245 | ri.pcnt = ri.pcnt * ALPHA; |
243 | ri.diff = ri.diff * ALPHA + (NOW - ri.last); |
246 | ri.diff = ri.diff * ALPHA + (NOW - ri.last); |
244 | |
247 | |
245 | ri.last = NOW; |
248 | ri.last = NOW; |
246 | |
249 | |
|
|
250 | double dif = ri.diff / ri.pcnt; |
|
|
251 | |
247 | bool send = ri.diff / ri.pcnt > CUTOFF; |
252 | bool send = dif > CUTOFF; |
248 | |
253 | |
|
|
254 | if (dif > MAXDIF) |
|
|
255 | { |
|
|
256 | ri.pcnt = 1.; |
|
|
257 | ri.diff = MAXDIF; |
|
|
258 | } |
249 | if (send) |
259 | else if (send) |
250 | ri.pcnt++; |
260 | ri.pcnt++; |
251 | |
261 | |
252 | push_front (ri); |
262 | push_front (ri); |
253 | |
263 | |
254 | return send; |
264 | return send; |
… | |
… | |
285 | hmac_gen (ctx); |
295 | hmac_gen (ctx); |
286 | |
296 | |
287 | return !memcmp (hmac, hmac_digest, HMACLENGTH); |
297 | return !memcmp (hmac, hmac_digest, HMACLENGTH); |
288 | } |
298 | } |
289 | |
299 | |
290 | void vpn_packet::set_hdr (ptype type, unsigned int dst) |
300 | void vpn_packet::set_hdr (ptype type_, unsigned int dst) |
291 | { |
301 | { |
292 | this->type = type; |
302 | type = type_; |
293 | |
303 | |
294 | int src = THISNODE->id; |
304 | int src = THISNODE->id; |
295 | |
305 | |
296 | src1 = src; |
306 | src1 = src; |
297 | srcdst = ((src >> 8) << 4) | (dst >> 8); |
307 | srcdst = ((src >> 8) << 4) | (dst >> 8); |
… | |
… | |
467 | set_hdr (type, dst); |
477 | set_hdr (type, dst); |
468 | } |
478 | } |
469 | |
479 | |
470 | bool config_packet::chk_config () const |
480 | bool config_packet::chk_config () const |
471 | { |
481 | { |
472 | return prot_major == PROTOCOL_MAJOR |
482 | if (prot_major != PROTOCOL_MAJOR) |
473 | && randsize == RAND_SIZE |
483 | slog (L_WARN, _("major version mismatch (%d <=> %d)"), prot_major, PROTOCOL_MAJOR); |
474 | && hmaclen == HMACLENGTH |
484 | else if (randsize != RAND_SIZE) |
475 | && flags == curflags () |
485 | slog (L_WARN, _("rand size mismatch (%d <=> %d)"), randsize, RAND_SIZE); |
|
|
486 | else if (hmaclen != HMACLENGTH) |
|
|
487 | slog (L_WARN, _("hmac length mismatch (%d <=> %d)"), hmaclen, HMACLENGTH); |
|
|
488 | else if (flags != curflags ()) |
|
|
489 | slog (L_WARN, _("flag mismatch (%x <=> %x)"), flags, curflags ()); |
476 | && challengelen == sizeof (rsachallenge) |
490 | else if (challengelen != sizeof (rsachallenge)) |
|
|
491 | slog (L_WARN, _("challenge length mismatch (%d <=> %d)"), challengelen, sizeof (rsachallenge)); |
477 | && cipher_nid == htonl (EVP_CIPHER_nid (CIPHER)) |
492 | else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER))) |
|
|
493 | slog (L_WARN, _("cipher mismatch (%x <=> %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER)); |
478 | && digest_nid == htonl (EVP_MD_type (RSA_HASH)) |
494 | else if (digest_nid != htonl (EVP_MD_type (RSA_HASH))) |
|
|
495 | slog (L_WARN, _("digest mismatch (%x <=> %x)"), ntohl (digest_nid), EVP_MD_type (RSA_HASH)); |
479 | && hmac_nid == htonl (EVP_MD_type (DIGEST)); |
496 | else if (hmac_nid != htonl (EVP_MD_type (DIGEST))) |
|
|
497 | slog (L_WARN, _("hmac mismatch (%x <=> %x)"), ntohl (hmac_nid), EVP_MD_type (DIGEST)); |
|
|
498 | else |
|
|
499 | return true; |
|
|
500 | |
|
|
501 | return false; |
480 | } |
502 | } |
481 | |
503 | |
482 | struct auth_req_packet : config_packet |
504 | struct auth_req_packet : config_packet |
483 | { |
505 | { |
484 | char magic[8]; |
506 | char magic[8]; |
… | |
… | |
546 | }; |
568 | }; |
547 | |
569 | |
548 | ///////////////////////////////////////////////////////////////////////////// |
570 | ///////////////////////////////////////////////////////////////////////////// |
549 | |
571 | |
550 | void |
572 | void |
|
|
573 | connection::connection_established () |
|
|
574 | { |
|
|
575 | if (ictx && octx) |
|
|
576 | { |
|
|
577 | connectmode = conf->connectmode; |
|
|
578 | |
|
|
579 | rekey.start (NOW + ::conf.rekey); |
|
|
580 | keepalive.start (NOW + ::conf.keepalive); |
|
|
581 | |
|
|
582 | // send queued packets |
|
|
583 | if (ictx && octx) |
|
|
584 | { |
|
|
585 | while (tap_packet *p = (tap_packet *)data_queue.get ()) |
|
|
586 | { |
|
|
587 | send_data_packet (p); |
|
|
588 | delete p; |
|
|
589 | } |
|
|
590 | |
|
|
591 | while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) |
|
|
592 | { |
|
|
593 | send_vpn_packet (p, si, IPTOS_RELIABILITY); |
|
|
594 | delete p; |
|
|
595 | } |
|
|
596 | } |
|
|
597 | } |
|
|
598 | else |
|
|
599 | { |
|
|
600 | retry_cnt = 0; |
|
|
601 | establish_connection.start (NOW + 5); |
|
|
602 | keepalive.reset (); |
|
|
603 | rekey.reset (); |
|
|
604 | } |
|
|
605 | } |
|
|
606 | |
|
|
607 | void |
551 | connection::reset_dstaddr () |
608 | connection::reset_si () |
552 | { |
609 | { |
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); |
610 | protocol = best_protocol (THISNODE->protocols & conf->protocols); |
587 | |
611 | |
588 | // mask out protocols we cannot establish |
612 | // mask out protocols we cannot establish |
589 | if (!conf->udp_port) protocol &= ~PROT_UDPv4; |
613 | if (!conf->udp_port) protocol &= ~PROT_UDPv4; |
590 | if (!conf->tcp_port) protocol &= ~PROT_TCPv4; |
614 | if (!conf->tcp_port) protocol &= ~PROT_TCPv4; |
591 | |
615 | |
592 | if (protocol) |
616 | si.set (conf, protocol); |
|
|
617 | } |
|
|
618 | |
|
|
619 | // ensure sockinfo is valid, forward if necessary |
|
|
620 | const sockinfo & |
|
|
621 | connection::forward_si (const sockinfo &si) const |
|
|
622 | { |
|
|
623 | if (!si.valid ()) |
|
|
624 | { |
|
|
625 | connection *r = vpn->find_router (); |
|
|
626 | |
|
|
627 | if (r) |
|
|
628 | { |
|
|
629 | slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s"), |
|
|
630 | conf->nodename, r->conf->nodename); |
|
|
631 | return r->si; |
|
|
632 | } |
|
|
633 | else |
|
|
634 | slog (L_DEBUG, _("%s: node unreachable, no common protocol"), |
|
|
635 | conf->nodename); |
593 | { |
636 | } |
594 | rsachallenge chg; |
|
|
595 | |
637 | |
596 | rsa_cache.gen (pkt->id, chg); |
638 | return si; |
|
|
639 | } |
597 | |
640 | |
598 | if (0 > RSA_public_encrypt (sizeof chg, |
641 | void |
599 | (unsigned char *)&chg, (unsigned char *)&pkt->encr, |
642 | connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
600 | conf->rsa_key, RSA_PKCS1_OAEP_PADDING)) |
643 | { |
601 | fatal ("RSA_public_encrypt error"); |
644 | if (!vpn->send_vpn_packet (pkt, si, tos)) |
|
|
645 | reset_connection (); |
|
|
646 | } |
602 | |
647 | |
603 | slog (L_TRACE, ">>%d PT_AUTH_REQ [%s]", conf->id, (const char *)si); |
648 | void |
|
|
649 | connection::send_ping (const sockinfo &si, u8 pong) |
|
|
650 | { |
|
|
651 | ping_packet *pkt = new ping_packet; |
604 | |
652 | |
605 | send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly |
653 | pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING); |
|
|
654 | send_vpn_packet (pkt, si, IPTOS_LOWDELAY); |
|
|
655 | |
|
|
656 | delete pkt; |
|
|
657 | } |
|
|
658 | |
|
|
659 | void |
|
|
660 | connection::send_reset (const sockinfo &si) |
|
|
661 | { |
|
|
662 | if (reset_rate_limiter.can (si) && connectmode != conf_node::C_DISABLED) |
|
|
663 | { |
|
|
664 | config_packet *pkt = new config_packet; |
|
|
665 | |
|
|
666 | pkt->setup (vpn_packet::PT_RESET, conf->id); |
|
|
667 | send_vpn_packet (pkt, si, IPTOS_MINCOST); |
606 | |
668 | |
607 | delete pkt; |
669 | delete pkt; |
608 | } |
670 | } |
609 | else |
671 | } |
610 | ; // silently fail |
672 | |
|
|
673 | void |
|
|
674 | connection::send_auth_request (const sockinfo &si, bool initiate) |
|
|
675 | { |
|
|
676 | auth_req_packet *pkt = new auth_req_packet (conf->id, initiate, THISNODE->protocols); |
|
|
677 | |
|
|
678 | rsachallenge chg; |
|
|
679 | |
|
|
680 | rsa_cache.gen (pkt->id, chg); |
|
|
681 | |
|
|
682 | if (0 > RSA_public_encrypt (sizeof chg, |
|
|
683 | (unsigned char *)&chg, (unsigned char *)&pkt->encr, |
|
|
684 | conf->rsa_key, RSA_PKCS1_OAEP_PADDING)) |
|
|
685 | fatal ("RSA_public_encrypt error"); |
|
|
686 | |
|
|
687 | slog (L_TRACE, ">>%d PT_AUTH_REQ [%s]", conf->id, (const char *)si); |
|
|
688 | |
|
|
689 | send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly |
|
|
690 | |
|
|
691 | delete pkt; |
611 | } |
692 | } |
612 | |
693 | |
613 | void |
694 | void |
614 | connection::send_auth_response (const sockinfo &si, const rsaid &id, const rsachallenge &chg) |
695 | connection::send_auth_response (const sockinfo &si, const rsaid &id, const rsachallenge &chg) |
615 | { |
696 | { |
… | |
… | |
641 | |
722 | |
642 | delete r; |
723 | delete r; |
643 | } |
724 | } |
644 | |
725 | |
645 | void |
726 | void |
646 | connection::establish_connection_cb (tstamp &ts) |
727 | connection::establish_connection_cb (time_watcher &w) |
647 | { |
728 | { |
648 | if (ictx || conf == THISNODE |
729 | if (ictx || conf == THISNODE |
649 | || connectmode == conf_node::C_NEVER |
730 | || connectmode == conf_node::C_NEVER |
650 | || connectmode == conf_node::C_DISABLED) |
731 | || connectmode == conf_node::C_DISABLED) |
651 | ts = TSTAMP_CANCEL; |
732 | w.at = TSTAMP_CANCEL; |
652 | else if (ts <= NOW) |
733 | else if (w.at <= NOW) |
653 | { |
734 | { |
654 | double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; |
735 | double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; |
655 | |
736 | |
656 | if (retry_int < 3600 * 8) |
737 | if (retry_int < 3600 * 8) |
657 | retry_cnt++; |
738 | retry_cnt++; |
658 | |
739 | |
659 | ts = NOW + retry_int; |
740 | w.at = NOW + retry_int; |
660 | |
741 | |
661 | if (conf->hostname) |
742 | reset_si (); |
|
|
743 | |
|
|
744 | if (si.prot && !si.host) |
|
|
745 | vpn->send_connect_request (conf->id); |
|
|
746 | else |
662 | { |
747 | { |
663 | reset_dstaddr (); |
748 | const sockinfo &dsi = forward_si (si); |
|
|
749 | |
664 | if (si.host && auth_rate_limiter.can (si)) |
750 | if (dsi.valid () && auth_rate_limiter.can (dsi)) |
665 | { |
751 | { |
666 | if (retry_cnt < 4) |
752 | if (retry_cnt < 4) |
667 | send_auth_request (si, true); |
753 | send_auth_request (dsi, true); |
668 | else |
754 | else |
669 | send_ping (si, 0); |
755 | send_ping (dsi, 0); |
670 | } |
756 | } |
671 | } |
757 | } |
672 | else |
|
|
673 | vpn->connect_request (conf->id); |
|
|
674 | } |
758 | } |
675 | } |
759 | } |
676 | |
760 | |
677 | void |
761 | void |
678 | connection::reset_connection () |
762 | connection::reset_connection () |
… | |
… | |
707 | |
791 | |
708 | reset_connection (); |
792 | reset_connection (); |
709 | } |
793 | } |
710 | |
794 | |
711 | void |
795 | void |
712 | connection::rekey_cb (tstamp &ts) |
796 | connection::rekey_cb (time_watcher &w) |
713 | { |
797 | { |
714 | ts = TSTAMP_CANCEL; |
798 | w.at = TSTAMP_CANCEL; |
715 | |
799 | |
716 | reset_connection (); |
800 | reset_connection (); |
717 | establish_connection (); |
801 | establish_connection (); |
718 | } |
802 | } |
719 | |
803 | |
… | |
… | |
721 | connection::send_data_packet (tap_packet *pkt, bool broadcast) |
805 | connection::send_data_packet (tap_packet *pkt, bool broadcast) |
722 | { |
806 | { |
723 | vpndata_packet *p = new vpndata_packet; |
807 | vpndata_packet *p = new vpndata_packet; |
724 | int tos = 0; |
808 | int tos = 0; |
725 | |
809 | |
|
|
810 | // I am not hilarious about peeking into packets, but so be it. |
726 | if (conf->inherit_tos |
811 | if (conf->inherit_tos |
727 | && (*pkt)[12] == 0x08 && (*pkt)[13] == 0x00 // IP |
812 | && (*pkt)[12] == 0x08 && (*pkt)[13] == 0x00 // IP |
728 | && ((*pkt)[14] & 0xf0) == 0x40) // IPv4 |
813 | && ((*pkt)[14] & 0xf0) == 0x40) // IPv4 |
729 | tos = (*pkt)[15] & IPTOS_TOS_MASK; |
814 | tos = (*pkt)[15] & IPTOS_TOS_MASK; |
730 | |
815 | |
… | |
… | |
743 | if (ictx && octx) |
828 | if (ictx && octx) |
744 | send_data_packet (pkt, broadcast); |
829 | send_data_packet (pkt, broadcast); |
745 | else |
830 | else |
746 | { |
831 | { |
747 | if (!broadcast)//DDDD |
832 | if (!broadcast)//DDDD |
748 | queue.put (new tap_packet (*pkt)); |
833 | data_queue.put (new tap_packet (*pkt)); |
|
|
834 | |
|
|
835 | establish_connection (); |
|
|
836 | } |
|
|
837 | } |
|
|
838 | |
|
|
839 | void connection::inject_vpn_packet (vpn_packet *pkt, int tos) |
|
|
840 | { |
|
|
841 | if (ictx && octx) |
|
|
842 | send_vpn_packet (pkt, si, tos); |
|
|
843 | else |
|
|
844 | { |
|
|
845 | vpn_queue.put (new vpn_packet (*pkt)); |
749 | |
846 | |
750 | establish_connection (); |
847 | establish_connection (); |
751 | } |
848 | } |
752 | } |
849 | } |
753 | |
850 | |
… | |
… | |
759 | slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
856 | slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
760 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
857 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
761 | |
858 | |
762 | switch (pkt->typ ()) |
859 | switch (pkt->typ ()) |
763 | { |
860 | { |
764 | case vpn_packet::PT_PING: |
861 | case vpn_packet::PT_PING: |
765 | // we send pings instead of auth packets after some retries, |
862 | // we send pings instead of auth packets after some retries, |
766 | // so reset the retry counter and establish a connection |
863 | // so reset the retry counter and establish a connection |
767 | // when we receive a ping. |
864 | // when we receive a ping. |
768 | if (!ictx) |
865 | if (!ictx) |
|
|
866 | { |
|
|
867 | if (auth_rate_limiter.can (rsi)) |
|
|
868 | send_auth_request (rsi, true); |
|
|
869 | } |
|
|
870 | else |
|
|
871 | send_ping (rsi, 1); // pong |
|
|
872 | |
|
|
873 | break; |
|
|
874 | |
|
|
875 | case vpn_packet::PT_PONG: |
|
|
876 | break; |
|
|
877 | |
|
|
878 | case vpn_packet::PT_RESET: |
769 | { |
879 | { |
770 | if (auth_rate_limiter.can (rsi)) |
880 | reset_connection (); |
771 | send_auth_request (rsi, true); |
881 | |
|
|
882 | config_packet *p = (config_packet *) pkt; |
|
|
883 | |
|
|
884 | if (!p->chk_config ()) |
|
|
885 | { |
|
|
886 | slog (L_WARN, _("%s(%s): protocol mismatch, disabling node"), |
|
|
887 | conf->nodename, (const char *)rsi); |
|
|
888 | connectmode = conf_node::C_DISABLED; |
|
|
889 | } |
|
|
890 | else if (connectmode == conf_node::C_ALWAYS) |
|
|
891 | establish_connection (); |
772 | } |
892 | } |
773 | else |
|
|
774 | send_ping (rsi, 1); // pong |
|
|
775 | |
|
|
776 | break; |
893 | break; |
777 | |
894 | |
778 | case vpn_packet::PT_PONG: |
|
|
779 | break; |
|
|
780 | |
|
|
781 | case vpn_packet::PT_RESET: |
895 | case vpn_packet::PT_AUTH_REQ: |
782 | { |
896 | if (auth_rate_limiter.can (rsi)) |
783 | reset_connection (); |
|
|
784 | |
|
|
785 | config_packet *p = (config_packet *) pkt; |
|
|
786 | |
|
|
787 | if (!p->chk_config ()) |
|
|
788 | { |
897 | { |
|
|
898 | auth_req_packet *p = (auth_req_packet *) pkt; |
|
|
899 | |
|
|
900 | slog (L_TRACE, "<<%d PT_AUTH_REQ(%d)", conf->id, p->initiate); |
|
|
901 | |
|
|
902 | if (p->chk_config () && !strncmp (p->magic, MAGIC, 8)) |
|
|
903 | { |
|
|
904 | if (p->prot_minor != PROTOCOL_MINOR) |
|
|
905 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
|
|
906 | conf->nodename, (const char *)rsi, |
|
|
907 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
|
|
908 | |
|
|
909 | if (p->initiate) |
|
|
910 | send_auth_request (rsi, false); |
|
|
911 | |
|
|
912 | rsachallenge k; |
|
|
913 | |
|
|
914 | if (0 > RSA_private_decrypt (sizeof (p->encr), |
|
|
915 | (unsigned char *)&p->encr, (unsigned char *)&k, |
|
|
916 | ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING)) |
|
|
917 | slog (L_ERR, _("%s(%s): challenge illegal or corrupted (%s). mismatched key or config file?"), |
|
|
918 | conf->nodename, (const char *)rsi, ERR_error_string (ERR_get_error (), 0)); |
|
|
919 | else |
|
|
920 | { |
|
|
921 | delete octx; |
|
|
922 | |
|
|
923 | octx = new crypto_ctx (k, 1); |
|
|
924 | oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; |
|
|
925 | |
|
|
926 | conf->protocols = p->protocols; |
|
|
927 | |
|
|
928 | send_auth_response (rsi, p->id, k); |
|
|
929 | |
|
|
930 | connection_established (); |
|
|
931 | |
|
|
932 | break; |
|
|
933 | } |
|
|
934 | } |
|
|
935 | else |
789 | slog (L_WARN, _("%s(%s): protocol mismatch, disabling node"), |
936 | slog (L_WARN, _("%s(%s): protocol mismatch"), |
790 | conf->nodename, (const char *)rsi); |
937 | conf->nodename, (const char *)rsi); |
791 | connectmode = conf_node::C_DISABLED; |
938 | |
|
|
939 | send_reset (rsi); |
792 | } |
940 | } |
793 | else if (connectmode == conf_node::C_ALWAYS) |
941 | |
794 | establish_connection (); |
|
|
795 | } |
|
|
796 | break; |
942 | break; |
797 | |
943 | |
798 | case vpn_packet::PT_AUTH_REQ: |
944 | case vpn_packet::PT_AUTH_RES: |
799 | if (auth_rate_limiter.can (rsi)) |
|
|
800 | { |
945 | { |
801 | auth_req_packet *p = (auth_req_packet *) pkt; |
946 | auth_res_packet *p = (auth_res_packet *) pkt; |
802 | |
947 | |
803 | slog (L_TRACE, "<<%d PT_AUTH_REQ(%d)", conf->id, p->initiate); |
948 | slog (L_TRACE, "<<%d PT_AUTH_RES", conf->id); |
804 | |
949 | |
805 | if (p->chk_config () && !strncmp (p->magic, MAGIC, 8)) |
950 | if (p->chk_config ()) |
806 | { |
951 | { |
807 | if (p->prot_minor != PROTOCOL_MINOR) |
952 | if (p->prot_minor != PROTOCOL_MINOR) |
808 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
953 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
809 | conf->nodename, (const char *)rsi, |
954 | conf->nodename, (const char *)rsi, |
810 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
955 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
811 | |
956 | |
812 | if (p->initiate) |
|
|
813 | send_auth_request (rsi, false); |
|
|
814 | |
|
|
815 | rsachallenge k; |
957 | rsachallenge chg; |
816 | |
958 | |
817 | if (0 > RSA_private_decrypt (sizeof (p->encr), |
959 | if (!rsa_cache.find (p->id, chg)) |
818 | (unsigned char *)&p->encr, (unsigned char *)&k, |
960 | { |
819 | ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING)) |
961 | slog (L_ERR, _("%s(%s): unrequested auth response ignored"), |
820 | slog (L_ERR, _("%s(%s): challenge illegal or corrupted"), |
|
|
821 | conf->nodename, (const char *)rsi); |
962 | conf->nodename, (const char *)rsi); |
|
|
963 | break; |
|
|
964 | } |
822 | else |
965 | else |
823 | { |
966 | { |
824 | retry_cnt = 0; |
967 | crypto_ctx *cctx = new crypto_ctx (chg, 0); |
825 | establish_connection.set (NOW + 8); //? ;) |
968 | |
826 | keepalive.reset (); |
969 | if (!p->hmac_chk (cctx)) |
|
|
970 | { |
|
|
971 | slog (L_ERR, _("%s(%s): hmac authentication error on auth response, received invalid packet\n" |
|
|
972 | "could be an attack, or just corruption or an synchronization error"), |
|
|
973 | conf->nodename, (const char *)rsi); |
|
|
974 | break; |
|
|
975 | } |
827 | rekey.reset (); |
976 | else |
|
|
977 | { |
|
|
978 | rsaresponse h; |
828 | |
979 | |
|
|
980 | rsa_hash (p->id, chg, h); |
|
|
981 | |
|
|
982 | if (!memcmp ((u8 *)&h, (u8 *)p->response, sizeof h)) |
|
|
983 | { |
|
|
984 | prot_minor = p->prot_minor; |
|
|
985 | |
|
|
986 | delete ictx; ictx = cctx; |
|
|
987 | |
|
|
988 | iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid |
|
|
989 | |
|
|
990 | si = rsi; |
|
|
991 | protocol = rsi.prot; |
|
|
992 | |
|
|
993 | connection_established (); |
|
|
994 | |
|
|
995 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
|
|
996 | conf->nodename, (const char *)rsi, |
|
|
997 | p->prot_major, p->prot_minor); |
|
|
998 | |
|
|
999 | if (::conf.script_node_up) |
|
|
1000 | run_script (run_script_cb (this, &connection::script_node_up), false); |
|
|
1001 | |
|
|
1002 | break; |
|
|
1003 | } |
|
|
1004 | else |
|
|
1005 | slog (L_ERR, _("%s(%s): sent and received challenge do not match"), |
|
|
1006 | conf->nodename, (const char *)rsi); |
|
|
1007 | } |
|
|
1008 | |
829 | delete ictx; |
1009 | delete cctx; |
830 | ictx = 0; |
|
|
831 | |
|
|
832 | delete octx; |
|
|
833 | |
|
|
834 | octx = new crypto_ctx (k, 1); |
|
|
835 | oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; |
|
|
836 | |
|
|
837 | conf->protocols = p->protocols; |
|
|
838 | send_auth_response (rsi, p->id, k); |
|
|
839 | |
|
|
840 | break; |
|
|
841 | } |
1010 | } |
842 | } |
1011 | } |
843 | |
|
|
844 | send_reset (rsi); |
|
|
845 | } |
1012 | } |
846 | |
1013 | |
|
|
1014 | send_reset (rsi); |
847 | break; |
1015 | break; |
848 | |
1016 | |
849 | case vpn_packet::PT_AUTH_RES: |
1017 | case vpn_packet::PT_DATA_COMPRESSED: |
850 | { |
1018 | #if !ENABLE_COMPRESSION |
851 | auth_res_packet *p = (auth_res_packet *) pkt; |
1019 | send_reset (rsi); |
|
|
1020 | break; |
|
|
1021 | #endif |
852 | |
1022 | |
853 | slog (L_TRACE, "<<%d PT_AUTH_RES", conf->id); |
1023 | case vpn_packet::PT_DATA_UNCOMPRESSED: |
854 | |
1024 | |
855 | if (p->chk_config ()) |
1025 | if (ictx && octx) |
856 | { |
1026 | { |
857 | if (p->prot_minor != PROTOCOL_MINOR) |
1027 | vpndata_packet *p = (vpndata_packet *)pkt; |
858 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
|
|
859 | conf->nodename, (const char *)rsi, |
|
|
860 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
|
|
861 | |
1028 | |
862 | rsachallenge chg; |
1029 | if (!p->hmac_chk (ictx)) |
863 | |
1030 | slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" |
864 | if (!rsa_cache.find (p->id, chg)) |
1031 | "could be an attack, or just corruption or an synchronization error"), |
865 | slog (L_ERR, _("%s(%s): unrequested auth response"), |
|
|
866 | conf->nodename, (const char *)rsi); |
1032 | conf->nodename, (const char *)rsi); |
867 | else |
1033 | else |
868 | { |
1034 | { |
869 | crypto_ctx *cctx = new crypto_ctx (chg, 0); |
|
|
870 | |
|
|
871 | if (!p->hmac_chk (cctx)) |
|
|
872 | slog (L_ERR, _("%s(%s): hmac authentication error on auth response, received invalid packet\n" |
|
|
873 | "could be an attack, or just corruption or an synchronization error"), |
|
|
874 | conf->nodename, (const char *)rsi); |
|
|
875 | else |
1035 | u32 seqno; |
|
|
1036 | tap_packet *d = p->unpack (this, seqno); |
|
|
1037 | |
|
|
1038 | if (iseqno.recv_ok (seqno)) |
876 | { |
1039 | { |
877 | rsaresponse h; |
1040 | vpn->tap->send (d); |
878 | |
1041 | |
879 | rsa_hash (p->id, chg, h); |
1042 | if (p->dst () == 0) // re-broadcast |
|
|
1043 | for (vpn::conns_vector::iterator i = vpn->conns.begin (); i != vpn->conns.end (); ++i) |
|
|
1044 | { |
|
|
1045 | connection *c = *i; |
880 | |
1046 | |
881 | if (!memcmp ((u8 *)&h, (u8 *)p->response, sizeof h)) |
1047 | if (c->conf != THISNODE && c->conf != conf) |
|
|
1048 | c->inject_data_packet (d); |
|
|
1049 | } |
|
|
1050 | |
|
|
1051 | if (si != rsi) |
882 | { |
1052 | { |
883 | prot_minor = p->prot_minor; |
1053 | // fast re-sync on connection changes, useful especially for tcp/ip |
884 | |
|
|
885 | delete ictx; ictx = cctx; |
|
|
886 | |
|
|
887 | iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid |
|
|
888 | |
|
|
889 | si = rsi; |
1054 | si = rsi; |
890 | |
1055 | |
891 | rekey.set (NOW + ::conf.rekey); |
1056 | slog (L_INFO, _("%s(%s): socket address changed to %s"), |
892 | keepalive.set (NOW + ::conf.keepalive); |
|
|
893 | |
|
|
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"), |
|
|
904 | conf->nodename, (const char *)rsi, |
1057 | conf->nodename, (const char *)si, (const char *)rsi); |
905 | strprotocol (protocol), |
|
|
906 | p->prot_major, p->prot_minor); |
|
|
907 | |
|
|
908 | if (::conf.script_node_up) |
|
|
909 | run_script (run_script_cb (this, &connection::script_node_up), false); |
|
|
910 | |
|
|
911 | break; |
|
|
912 | } |
1058 | } |
|
|
1059 | |
913 | else |
1060 | delete d; |
914 | slog (L_ERR, _("%s(%s): sent and received challenge do not match"), |
1061 | |
915 | conf->nodename, (const char *)rsi); |
1062 | break; |
916 | } |
1063 | } |
917 | |
|
|
918 | delete cctx; |
|
|
919 | } |
1064 | } |
920 | } |
1065 | } |
921 | } |
|
|
922 | |
1066 | |
923 | send_reset (rsi); |
1067 | send_reset (rsi); |
924 | break; |
1068 | break; |
925 | |
1069 | |
926 | case vpn_packet::PT_DATA_COMPRESSED: |
|
|
927 | #if !ENABLE_COMPRESSION |
|
|
928 | send_reset (rsi); |
|
|
929 | break; |
|
|
930 | #endif |
|
|
931 | |
|
|
932 | case vpn_packet::PT_DATA_UNCOMPRESSED: |
|
|
933 | |
|
|
934 | if (ictx && octx) |
|
|
935 | { |
|
|
936 | vpndata_packet *p = (vpndata_packet *)pkt; |
|
|
937 | |
|
|
938 | if (rsi == si) |
|
|
939 | { |
|
|
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 |
|
|
945 | { |
|
|
946 | u32 seqno; |
|
|
947 | tap_packet *d = p->unpack (this, seqno); |
|
|
948 | |
|
|
949 | if (iseqno.recv_ok (seqno)) |
|
|
950 | { |
|
|
951 | vpn->tap->send (d); |
|
|
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; |
|
|
965 | } |
|
|
966 | } |
|
|
967 | } |
|
|
968 | else |
|
|
969 | slog (L_ERR, _("received data packet from unknown source %s"), (const char *)rsi); |
|
|
970 | } |
|
|
971 | |
|
|
972 | send_reset (rsi); |
|
|
973 | break; |
|
|
974 | |
|
|
975 | case vpn_packet::PT_CONNECT_REQ: |
1070 | case vpn_packet::PT_CONNECT_REQ: |
976 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1071 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
977 | { |
1072 | { |
978 | connect_req_packet *p = (connect_req_packet *) pkt; |
1073 | connect_req_packet *p = (connect_req_packet *) pkt; |
979 | |
1074 | |
980 | assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything |
1075 | assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything |
981 | conf->protocols = p->protocols; |
|
|
982 | connection *c = vpn->conns[p->id - 1]; |
1076 | connection *c = vpn->conns[p->id - 1]; |
|
|
1077 | conf->protocols = p->protocols; |
983 | |
1078 | |
984 | slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]\n", |
1079 | slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]\n", |
985 | conf->id, p->id, c->ictx && c->octx); |
1080 | conf->id, p->id, c->ictx && c->octx); |
986 | |
1081 | |
987 | if (c->ictx && c->octx) |
1082 | if (c->ictx && c->octx) |
988 | { |
1083 | { |
989 | // send connect_info packets to both sides, in case one is |
1084 | // send connect_info packets to both sides, in case one is |
990 | // behind a nat firewall (or both ;) |
1085 | // behind a nat firewall (or both ;) |
991 | c->send_connect_info (conf->id, si, conf->protocols); |
1086 | c->send_connect_info (conf->id, si, conf->protocols); |
992 | send_connect_info (c->conf->id, c->si, c->conf->protocols); |
1087 | send_connect_info (c->conf->id, c->si, c->conf->protocols); |
993 | } |
1088 | } |
|
|
1089 | else |
|
|
1090 | c->establish_connection (); |
994 | } |
1091 | } |
995 | |
1092 | |
996 | break; |
1093 | break; |
997 | |
1094 | |
998 | case vpn_packet::PT_CONNECT_INFO: |
1095 | case vpn_packet::PT_CONNECT_INFO: |
999 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1096 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1000 | { |
1097 | { |
1001 | connect_info_packet *p = (connect_info_packet *) pkt; |
1098 | connect_info_packet *p = (connect_info_packet *) pkt; |
1002 | |
1099 | |
1003 | assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything |
1100 | assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything |
1004 | conf->protocols = p->protocols; |
1101 | |
1005 | connection *c = vpn->conns[p->id - 1]; |
1102 | connection *c = vpn->conns[p->id - 1]; |
1006 | |
1103 | |
|
|
1104 | c->conf->protocols = p->protocols; |
|
|
1105 | protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); |
|
|
1106 | p->si.upgrade_protocol (protocol, c->conf); |
|
|
1107 | |
1007 | slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", |
1108 | slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", |
1008 | conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); |
1109 | conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); |
1009 | |
1110 | |
|
|
1111 | const sockinfo &dsi = forward_si (p->si); |
|
|
1112 | |
|
|
1113 | if (dsi.valid ()) |
1010 | c->send_auth_request (p->si, true); |
1114 | c->send_auth_request (dsi, true); |
1011 | } |
1115 | } |
1012 | |
1116 | |
1013 | break; |
1117 | break; |
1014 | |
1118 | |
1015 | default: |
1119 | default: |
1016 | send_reset (rsi); |
1120 | send_reset (rsi); |
1017 | break; |
1121 | break; |
1018 | |
|
|
1019 | } |
1122 | } |
1020 | } |
1123 | } |
1021 | |
1124 | |
1022 | void connection::keepalive_cb (tstamp &ts) |
1125 | void connection::keepalive_cb (time_watcher &w) |
1023 | { |
1126 | { |
1024 | if (NOW >= last_activity + ::conf.keepalive + 30) |
1127 | if (NOW >= last_activity + ::conf.keepalive + 30) |
1025 | { |
1128 | { |
1026 | reset_connection (); |
1129 | reset_connection (); |
1027 | establish_connection (); |
1130 | establish_connection (); |
1028 | } |
1131 | } |
1029 | else if (NOW < last_activity + ::conf.keepalive) |
1132 | else if (NOW < last_activity + ::conf.keepalive) |
1030 | ts = last_activity + ::conf.keepalive; |
1133 | w.at = last_activity + ::conf.keepalive; |
1031 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1134 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1032 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1135 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1033 | { |
1136 | { |
1034 | send_ping (si); |
1137 | send_ping (si); |
1035 | ts = NOW + 5; |
1138 | w.at = NOW + 5; |
1036 | } |
1139 | } |
|
|
1140 | else if (NOW < last_activity + ::conf.keepalive + 10) |
|
|
1141 | // hold ondemand connections implicitly a few seconds longer |
|
|
1142 | // should delete octx, though, or something like that ;) |
|
|
1143 | w.at = last_activity + ::conf.keepalive + 10; |
1037 | else |
1144 | else |
1038 | reset_connection (); |
1145 | reset_connection (); |
1039 | } |
1146 | } |
1040 | |
1147 | |
1041 | void connection::connect_request (int id) |
1148 | void connection::send_connect_request (int id) |
1042 | { |
1149 | { |
1043 | connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); |
1150 | connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); |
1044 | |
1151 | |
1045 | slog (L_TRACE, ">>%d PT_CONNECT_REQ(%d)", conf->id, id); |
1152 | slog (L_TRACE, ">>%d PT_CONNECT_REQ(%d)", conf->id, id); |
1046 | p->hmac_set (octx); |
1153 | p->hmac_set (octx); |
… | |
… | |
1049 | delete p; |
1156 | delete p; |
1050 | } |
1157 | } |
1051 | |
1158 | |
1052 | void connection::script_node () |
1159 | void connection::script_node () |
1053 | { |
1160 | { |
1054 | vpn->script_if_up (0); |
1161 | vpn->script_if_up (); |
1055 | |
1162 | |
1056 | char *env; |
1163 | char *env; |
1057 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
1164 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
1058 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1165 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1059 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1166 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1060 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1167 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1061 | } |
1168 | } |
1062 | |
1169 | |
1063 | const char *connection::script_node_up (int) |
1170 | const char *connection::script_node_up () |
1064 | { |
1171 | { |
1065 | script_node (); |
1172 | script_node (); |
1066 | |
1173 | |
1067 | putenv ("STATE=up"); |
1174 | putenv ("STATE=up"); |
1068 | |
1175 | |
1069 | return ::conf.script_node_up ? ::conf.script_node_up : "node-up"; |
1176 | return ::conf.script_node_up ? ::conf.script_node_up : "node-up"; |
1070 | } |
1177 | } |
1071 | |
1178 | |
1072 | const char *connection::script_node_down (int) |
1179 | const char *connection::script_node_down () |
1073 | { |
1180 | { |
1074 | script_node (); |
1181 | script_node (); |
1075 | |
1182 | |
1076 | putenv ("STATE=down"); |
1183 | putenv ("STATE=down"); |
1077 | |
1184 | |
1078 | return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; |
1185 | return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; |
1079 | } |
|
|
1080 | |
|
|
1081 | // send a vpn packet out to other hosts |
|
|
1082 | void |
|
|
1083 | connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
|
|
1084 | { |
|
|
1085 | if (protocol & PROT_IPv4) |
|
|
1086 | vpn->send_ipv4_packet (pkt, si, tos); |
|
|
1087 | else |
|
|
1088 | vpn->send_udpv4_packet (pkt, si, tos); |
|
|
1089 | } |
1186 | } |
1090 | |
1187 | |
1091 | connection::connection(struct vpn *vpn_) |
1188 | connection::connection(struct vpn *vpn_) |
1092 | : vpn(vpn_) |
1189 | : vpn(vpn_) |
1093 | , rekey (this, &connection::rekey_cb) |
1190 | , rekey (this, &connection::rekey_cb) |