1 | /* |
1 | /* |
2 | vpn.C -- handle the protocol, encryption, handshaking etc. |
2 | vpn.C -- handle the protocol, encryption, handshaking etc. |
3 | Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> |
3 | Copyright (C) 2003-2008,2010,2011,2013 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 |
… | |
… | |
38 | #include <cstdlib> |
38 | #include <cstdlib> |
39 | |
39 | |
40 | #include <sys/types.h> |
40 | #include <sys/types.h> |
41 | #include <sys/socket.h> |
41 | #include <sys/socket.h> |
42 | #include <sys/wait.h> |
42 | #include <sys/wait.h> |
|
|
43 | #include <sys/stat.h> |
43 | #include <errno.h> |
44 | #include <errno.h> |
44 | #include <time.h> |
45 | #include <time.h> |
45 | #include <unistd.h> |
46 | #include <unistd.h> |
46 | #include <fcntl.h> |
47 | #include <fcntl.h> |
47 | #include <sys/socket.h> |
48 | #include <sys/socket.h> |
… | |
… | |
52 | |
53 | |
53 | #include "connection.h" |
54 | #include "connection.h" |
54 | #include "util.h" |
55 | #include "util.h" |
55 | #include "vpn.h" |
56 | #include "vpn.h" |
56 | |
57 | |
|
|
58 | using namespace std; |
|
|
59 | |
57 | vpn network; // THE vpn (bad design...) |
60 | vpn network; // THE vpn (bad design...) |
|
|
61 | |
|
|
62 | ///////////////////////////////////////////////////////////////////////////// |
|
|
63 | |
|
|
64 | // hopefully temporary workaround for rare buffer full conditions |
|
|
65 | // if it happens, usually instantly retrying or retrying ~5ms later |
|
|
66 | // is good enough with current network technologies/kernels |
|
|
67 | |
|
|
68 | static ssize_t |
|
|
69 | xsendto (int fd, const void *buf, size_t len, int flags, |
|
|
70 | const struct sockaddr *sa, socklen_t salen) |
|
|
71 | { |
|
|
72 | ssize_t res; |
|
|
73 | |
|
|
74 | for (int retry = 0; retry <= 13; ++retry) // ~100ms |
|
|
75 | { |
|
|
76 | res = sendto (fd, buf, len, flags, sa, salen); |
|
|
77 | |
|
|
78 | if (res >= 0 || errno != ENOBUFS) |
|
|
79 | break; |
|
|
80 | |
|
|
81 | struct timespec ts = { 0, 1000 * retry }; |
|
|
82 | nanosleep (&ts, 0); |
|
|
83 | } |
|
|
84 | |
|
|
85 | return res; |
|
|
86 | } |
58 | |
87 | |
59 | ///////////////////////////////////////////////////////////////////////////// |
88 | ///////////////////////////////////////////////////////////////////////////// |
60 | |
89 | |
61 | static void inline |
90 | static void inline |
62 | set_tos (int fd, int &tos_prev, int tos) |
91 | set_tos (int fd, int &tos_prev, int tos) |
… | |
… | |
75 | { |
104 | { |
76 | // the tunnel device mtu should be the physical mtu - overhead |
105 | // the tunnel device mtu should be the physical mtu - overhead |
77 | // the tricky part is rounding to the cipher key blocksize |
106 | // the tricky part is rounding to the cipher key blocksize |
78 | int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD; |
107 | int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD; |
79 | mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion |
108 | mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion |
80 | mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round |
109 | mtu -= mtu % BLOCK_SIZE (CIPHER); // round |
81 | mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
110 | mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
82 | |
111 | |
83 | char *env; |
112 | char *env; |
84 | asprintf (&env, "CONFBASE=%s", confbase); putenv (env); |
113 | asprintf (&env, "CONFBASE=%s", confbase); putenv (env); |
85 | asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env); |
114 | asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env); |
… | |
… | |
110 | inline const char * |
139 | inline const char * |
111 | vpn::script_if_up () |
140 | vpn::script_if_up () |
112 | { |
141 | { |
113 | script_init_env (); |
142 | script_init_env (); |
114 | |
143 | |
115 | char *filename; |
144 | return conf.config_filename (::conf.script_if_up, "if-up"); |
116 | asprintf (&filename, |
|
|
117 | "%s/%s", |
|
|
118 | confbase, |
|
|
119 | ::conf.script_if_up ? ::conf.script_if_up : "if-up"); |
|
|
120 | |
|
|
121 | return filename; |
|
|
122 | } |
145 | } |
123 | |
146 | |
124 | int |
147 | int |
125 | vpn::setup_socket (u8 prot, int family, int type, int proto) |
148 | vpn::setup_socket (u8 prot, int family, int type, int proto) |
126 | { |
149 | { |
… | |
… | |
135 | fcntl (fd, F_SETFL, O_NONBLOCK); |
158 | fcntl (fd, F_SETFL, O_NONBLOCK); |
136 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
159 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
137 | |
160 | |
138 | #ifdef SO_MARK |
161 | #ifdef SO_MARK |
139 | if (::conf.nfmark) |
162 | if (::conf.nfmark) |
140 | setsockopt (ipv4_fd, SOL_SOCKET, SO_MARK, &::conf.nfmark, sizeof ::conf.nfmark); |
163 | if (setsockopt (fd, SOL_SOCKET, SO_MARK, &::conf.nfmark, sizeof ::conf.nfmark)) |
|
|
164 | slog (L_WARN, _("unable to set nfmark on %s socket: %s"), strprotocol (prot), strerror (errno)); |
141 | #endif |
165 | #endif |
142 | |
166 | |
143 | return fd; |
167 | return fd; |
144 | } |
168 | } |
145 | |
169 | |
… | |
… | |
391 | |
415 | |
392 | return 0; |
416 | return 0; |
393 | } |
417 | } |
394 | |
418 | |
395 | bool |
419 | bool |
|
|
420 | vpn::drop_privileges () |
|
|
421 | { |
|
|
422 | if (::conf.change_root) |
|
|
423 | { |
|
|
424 | if (!strcmp (::conf.change_root, "/")) |
|
|
425 | { |
|
|
426 | char dir [L_tmpnam]; |
|
|
427 | if (!tmpnam (dir)) |
|
|
428 | { |
|
|
429 | slog (L_CRIT, _("unable to create anonymous root path.")); |
|
|
430 | return false; |
|
|
431 | } |
|
|
432 | |
|
|
433 | if (mkdir (dir, 0700)) |
|
|
434 | { |
|
|
435 | slog (L_CRIT, _("unable to create anonymous root directory.")); |
|
|
436 | return false; |
|
|
437 | } |
|
|
438 | |
|
|
439 | if (chdir (dir)) |
|
|
440 | { |
|
|
441 | slog (L_CRIT, _("unable to change to anonymous root directory.")); |
|
|
442 | return false; |
|
|
443 | } |
|
|
444 | |
|
|
445 | if (rmdir (dir)) |
|
|
446 | slog (L_ERR, _("unable to remove anonymous root directory, continuing.")); |
|
|
447 | } |
|
|
448 | else |
|
|
449 | { |
|
|
450 | if (chdir (::conf.change_root)) |
|
|
451 | { |
|
|
452 | slog (L_CRIT, _("%s: unable to change to specified root directory."), ::conf.change_root); |
|
|
453 | return false; |
|
|
454 | } |
|
|
455 | } |
|
|
456 | |
|
|
457 | if (chroot (".")) |
|
|
458 | { |
|
|
459 | slog (L_CRIT, _("unable to set new root directory.")); |
|
|
460 | return false; |
|
|
461 | } |
|
|
462 | |
|
|
463 | if (chdir ("/")) |
|
|
464 | { |
|
|
465 | slog (L_CRIT, _("unable to set cwd to new root directory.")); |
|
|
466 | return false; |
|
|
467 | } |
|
|
468 | } |
|
|
469 | |
|
|
470 | if (::conf.change_gid) |
|
|
471 | if (setgid (::conf.change_gid)) |
|
|
472 | { |
|
|
473 | slog (L_CRIT, _("unable to change group id to %d."), ::conf.change_gid); |
|
|
474 | return false; |
|
|
475 | } |
|
|
476 | |
|
|
477 | if (::conf.change_uid) |
|
|
478 | if (setuid (::conf.change_uid)) |
|
|
479 | { |
|
|
480 | slog (L_CRIT, _("unable to change user id to %d."), ::conf.change_uid); |
|
|
481 | return false; |
|
|
482 | } |
|
|
483 | |
|
|
484 | return true; |
|
|
485 | } |
|
|
486 | |
|
|
487 | bool |
396 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
488 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
397 | { |
489 | { |
398 | set_tos (ipv4_fd, ipv4_tos, tos); |
490 | set_tos (ipv4_fd, ipv4_tos, tos); |
399 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
491 | xsendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
400 | |
492 | |
401 | return true; |
493 | return true; |
402 | } |
494 | } |
403 | |
495 | |
404 | static u16 |
496 | static u16 |
… | |
… | |
435 | hdr->code = 255; |
527 | hdr->code = 255; |
436 | hdr->checksum = 0; |
528 | hdr->checksum = 0; |
437 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
529 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
438 | |
530 | |
439 | set_tos (icmpv4_fd, icmpv4_tos, tos); |
531 | set_tos (icmpv4_fd, icmpv4_tos, tos); |
440 | sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
532 | xsendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
441 | |
533 | |
442 | return true; |
534 | return true; |
443 | } |
535 | } |
444 | #endif |
536 | #endif |
445 | |
537 | |
446 | bool |
538 | bool |
447 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
539 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
448 | { |
540 | { |
449 | set_tos (udpv4_fd, udpv4_tos, tos); |
541 | set_tos (udpv4_fd, udpv4_tos, tos); |
450 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
542 | xsendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
451 | |
543 | |
452 | return true; |
544 | return true; |
453 | } |
545 | } |
454 | |
546 | |
455 | void |
547 | void |
… | |
… | |
481 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst (), pkt->len); |
573 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst (), pkt->len); |
482 | |
574 | |
483 | if (src == 0 || src > conns.size () |
575 | if (src == 0 || src > conns.size () |
484 | || dst > conns.size () |
576 | || dst > conns.size () |
485 | || pkt->typ () >= vpn_packet::PT_MAX) |
577 | || pkt->typ () >= vpn_packet::PT_MAX) |
486 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)."), |
|
|
487 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
|
|
488 | else if (dst > conns.size ()) |
|
|
489 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)."), |
578 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)."), |
490 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
579 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
491 | else |
580 | else |
492 | { |
581 | { |
493 | connection *c = conns[src - 1]; |
582 | connection *c = conns[src - 1]; |
… | |
… | |
515 | vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
604 | vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
516 | { |
605 | { |
517 | switch (si.prot) |
606 | switch (si.prot) |
518 | { |
607 | { |
519 | case PROT_IPv4: |
608 | case PROT_IPv4: |
520 | return send_ipv4_packet (pkt, si, tos); |
609 | return send_ipv4_packet (pkt, si, tos); |
521 | |
610 | |
522 | case PROT_UDPv4: |
611 | case PROT_UDPv4: |
523 | return send_udpv4_packet (pkt, si, tos); |
612 | return send_udpv4_packet (pkt, si, tos); |
524 | |
613 | |
525 | #if ENABLE_TCP |
614 | #if ENABLE_TCP |
526 | case PROT_TCPv4: |
615 | case PROT_TCPv4: |
527 | return send_tcpv4_packet (pkt, si, tos); |
616 | return send_tcpv4_packet (pkt, si, tos); |
528 | #endif |
617 | #endif |
529 | #if ENABLE_ICMP |
618 | #if ENABLE_ICMP |
530 | case PROT_ICMPv4: |
619 | case PROT_ICMPv4: |
531 | return send_icmpv4_packet (pkt, si, tos); |
620 | return send_icmpv4_packet (pkt, si, tos); |
532 | #endif |
621 | #endif |
533 | #if ENABLE_DNS |
622 | #if ENABLE_DNS |
534 | case PROT_DNSv4: |
623 | case PROT_DNSv4: |
535 | return send_dnsv4_packet (pkt, si, tos); |
624 | return send_dnsv4_packet (pkt, si, tos); |
536 | #endif |
625 | #endif |
537 | default: |
626 | default: |
538 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol."), (const char *)si); |
627 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol."), (const char *)si); |
539 | } |
628 | } |
540 | |
629 | |
… | |
… | |
558 | if (len > 0) |
647 | if (len > 0) |
559 | { |
648 | { |
560 | pkt->len = len; |
649 | pkt->len = len; |
561 | |
650 | |
562 | // raw sockets deliver the ipv4 header, but don't expect it on sends |
651 | // raw sockets deliver the ipv4 header, but don't expect it on sends |
563 | pkt->skip_hdr (IP_OVERHEAD); |
652 | pkt->skip_hdr (pkt->ipv4_hdr_len ()); |
564 | |
653 | |
565 | recv_vpn_packet (pkt, si); |
654 | recv_vpn_packet (pkt, si); |
566 | } |
655 | } |
567 | else |
656 | else |
568 | { |
657 | { |
… | |
… | |
605 | if (hdr->type == ::conf.icmp_type |
694 | if (hdr->type == ::conf.icmp_type |
606 | && hdr->code == 255) |
695 | && hdr->code == 255) |
607 | { |
696 | { |
608 | // raw sockets deliver the ipv4, but don't expect it on sends |
697 | // raw sockets deliver the ipv4, but don't expect it on sends |
609 | // this is slow, but... |
698 | // this is slow, but... |
610 | pkt->skip_hdr (ICMP_OVERHEAD); |
699 | pkt->skip_hdr (pkt->ipv4_hdr_len () + (ICMP_OVERHEAD - IP_OVERHEAD)); |
611 | |
700 | |
612 | recv_vpn_packet (pkt, si); |
701 | recv_vpn_packet (pkt, si); |
613 | } |
702 | } |
614 | } |
703 | } |
615 | else |
704 | else |
… | |
… | |
757 | |
846 | |
758 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
847 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
759 | (*c)->establish_connection (); |
848 | (*c)->establish_connection (); |
760 | } |
849 | } |
761 | |
850 | |
|
|
851 | bool |
762 | bool vpn::can_direct (conf_node *src, conf_node *dst) const |
852 | vpn::can_direct (conf_node *src, conf_node *dst) const |
763 | { |
853 | { |
764 | return src != dst |
854 | return src != dst |
765 | && src->may_direct (dst) |
855 | && src->may_direct (dst) |
766 | && dst->may_direct (src) |
856 | && dst->may_direct (src) |
767 | && (((src->protocols & dst->protocols) && src->connectmode == conf_node::C_ALWAYS) |
857 | && (((src->protocols & dst->protocols) && src->connectmode == conf_node::C_ALWAYS) |
768 | || (src->protocols & dst->connectable_protocols ())); |
858 | || (src->protocols & dst->connectable_protocols ())); |
769 | } |
859 | } |
770 | |
860 | |
771 | // only works for indirect and routed connections: find a router |
861 | // only works for indirect and routed connections: find a router |
772 | // from THISNODE to dst |
862 | // from THISNODE to dst |
|
|
863 | connection * |
773 | connection *vpn::find_router_for (const connection *dst) |
864 | vpn::find_router_for (const connection *dst) |
774 | { |
865 | { |
775 | connection *router = 0; |
866 | connection *router = 0; |
776 | |
867 | |
777 | // first try to find a router with a direct connection, route there |
868 | // first try to find a router with a direct connection, route there |
778 | // regardless of any other considerations. |
869 | // regardless of any other considerations. |
… | |
… | |
817 | } |
908 | } |
818 | |
909 | |
819 | return router; |
910 | return router; |
820 | } |
911 | } |
821 | |
912 | |
|
|
913 | void |
822 | void vpn::connection_established (connection *c) |
914 | vpn::connection_established (connection *c) |
823 | { |
915 | { |
824 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
916 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
825 | { |
917 | { |
826 | connection *o = *i; |
918 | connection *o = *i; |
827 | |
919 | |
828 | if (!o->is_direct |
|
|
829 | && o->si.valid () |
920 | if (o->si.valid () |
830 | && c->si != o->si |
921 | && c->si != o->si |
831 | && c == find_router_for (o)) |
922 | && c == find_router_for (o) |
|
|
923 | && !can_direct (THISNODE, o->conf)) |
832 | { |
924 | { |
833 | slog (L_DEBUG, _("%s: can now route packets via %s, re-keying connection."), |
925 | slog (L_DEBUG, _("%s: can now route packets via %s, re-keying connection."), |
834 | o->conf->nodename, c->conf->nodename); |
926 | o->conf->nodename, c->conf->nodename); |
835 | o->rekey (); |
927 | o->rekey (); |
836 | } |
928 | } |
837 | } |
929 | } |
838 | } |
930 | } |
839 | |
931 | |
|
|
932 | void |
840 | void vpn::send_connect_request (connection *c) |
933 | vpn::send_connect_request (connection *c) |
841 | { |
934 | { |
842 | connection *r = find_router_for (c); |
935 | connection *r = find_router_for (c); |
843 | |
936 | |
844 | if (r) |
937 | if (r) |
845 | { |
938 | { |