--- gvpe/src/vpn.C 2003/04/06 04:17:36 1.6 +++ gvpe/src/vpn.C 2003/10/14 15:48:15 1.15 @@ -28,11 +28,13 @@ #include #include #include -#include -#include #include #include #include +#include +#include + +#include "netcompat.h" #include "pidfile.h" @@ -73,30 +75,56 @@ int vpn::setup () { + ipv4_fd = -1; + + if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto) + { + ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); + + if (ipv4_fd < 0) + return -1; + + fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); + +#if defined(SOL_IP) && defined(IP_MTU_DISCOVER) + // this I really consider a linux bug. I am neither connected + // nor do I fragment myself. Linux still sets DF and doesn't + // fragment for me sometimes. + { + int oval = IP_PMTUDISC_DONT; + setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); + } +#endif + + sockinfo si (THISNODE, PROT_IPv4); + + if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) + { + slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); + exit (1); + } + + ipv4_ev_watcher.start (ipv4_fd, POLLIN); + } + udpv4_fd = -1; - if (THISNODE->protocols & PROT_UDPv4) + if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) { udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); if (udpv4_fd < 0) return -1; + fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); + // standard daemon practise... { int oval = 1; setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); } - sockinfo si (THISNODE, PROT_UDPv4); - - if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) - { - slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); - exit (1); - } - -#ifdef IP_MTU_DISCOVER +#if defined(SOL_IP) && defined(IP_MTU_DISCOVER) // this I really consider a linux bug. I am neither connected // nor do I fragment myself. Linux still sets DF and doesn't // fragment for me sometimes. @@ -106,46 +134,74 @@ } #endif + sockinfo si (THISNODE, PROT_UDPv4); + + if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) + { + slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); + exit (1); + } + udpv4_ev_watcher.start (udpv4_fd, POLLIN); } - ipv4_fd = -1; - if (THISNODE->protocols & PROT_IPv4) + icmpv4_fd = -1; + +#if ENABLE_ICMP + if (THISNODE->protocols & PROT_ICMPv4) { - ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); + icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); - if (ipv4_fd < 0) + if (icmpv4_fd < 0) return -1; - sockinfo si (THISNODE, PROT_IPv4); + fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); - if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) - { - slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); - exit (1); - } +#ifdef ICMP_FILTER + { + icmp_filter oval; + oval.data = 0xffffffff; + if (::conf.icmp_type < 32) + oval.data &= ~(1 << ::conf.icmp_type); -#ifdef IP_MTU_DISCOVER + setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); + } +#endif + +#if defined(SOL_IP) && defined(IP_MTU_DISCOVER) // this I really consider a linux bug. I am neither connected // nor do I fragment myself. Linux still sets DF and doesn't // fragment for me sometimes. { int oval = IP_PMTUDISC_DONT; - setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); + setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); } #endif - ipv4_ev_watcher.start (ipv4_fd, POLLIN); + sockinfo si (THISNODE, PROT_ICMPv4); + + if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ())) + { + slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); + exit (1); + } + + icmpv4_ev_watcher.start (icmpv4_fd, POLLIN); } +#endif + + tcpv4_fd = -1; #if ENABLE_TCP - if (THISNODE->protocols & PROT_TCPv4) + if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port) { tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); if (tcpv4_fd < 0) return -1; + fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); + // standard daemon practise... { int oval = 1; @@ -187,43 +243,98 @@ } // send a vpn packet out to other hosts -void +bool vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) { switch (si.prot) { case PROT_IPv4: - send_ipv4_packet (pkt, si, tos); - break; + return send_ipv4_packet (pkt, si, tos); case PROT_UDPv4: - send_udpv4_packet (pkt, si, tos); - break; + return send_udpv4_packet (pkt, si, tos); #if ENABLE_TCP case PROT_TCPv4: - send_tcpv4_packet (pkt, si, tos); - break; + return send_tcpv4_packet (pkt, si, tos); +#endif + +#if ENABLE_ICMP + case PROT_ICMPv4: + return send_icmpv4_packet (pkt, si, tos); #endif default: slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); - abort (); + return false; } } -void +bool vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) { +#if defined(SOL_IP) && defined(IP_TOS) setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); +#endif sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); + + return true; } -void +static u16 +ipv4_checksum (u16 *data, unsigned int len) +{ + // use 32 bit accumulator and fold back carry bits at the end + u32 sum = 0; + + while (len > 1) + { + sum += *data++; + len -= 2; + } + + // odd byte left? + if (len) + sum += *(u8 *)data; + + // add back carry bits + sum = (sum >> 16) + (sum & 0xffff); // lo += hi + sum += (sum >> 16); // carry + + return ~sum; +} + +#if ENABLE_ICMP +bool +vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) +{ +#if defined(SOL_IP) && defined(IP_TOS) + setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); +#endif + + pkt->unshift_hdr (4); + + icmp_header *hdr = (icmp_header *)&((*pkt)[0]); + hdr->type = ::conf.icmp_type; + hdr->code = 255; + hdr->checksum = 0; + hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); + + sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); + + return true; +} +#endif + +bool vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) { +#if defined(SOL_IP) && defined(IP_TOS) setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); +#endif sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); + + return true; } void @@ -247,8 +358,8 @@ { connection *c = conns[src - 1]; - if (dst == 0 && !THISNODE->routerprio) - slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), + if (dst == 0) + slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), c->conf->nodename, (const char *)rsi); else if (dst != 0 && dst != THISNODE->id) { @@ -267,7 +378,7 @@ } void -vpn::udpv4_ev (io_watcher &w, short revents) +vpn::ipv4_ev (io_watcher &w, short revents) { if (revents & (POLLIN | POLLERR)) { @@ -278,18 +389,22 @@ len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); - sockinfo si(sa, PROT_UDPv4); + sockinfo si(sa, PROT_IPv4); if (len > 0) { pkt->len = len; + // raw sockets deliver the ipv4, but don't expect it on sends + // this is slow, but... + pkt->skip_hdr (IP_OVERHEAD); + recv_vpn_packet (pkt, si); } else { // probably ECONNRESET or somesuch - slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); + slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); } delete pkt; @@ -297,7 +412,7 @@ else if (revents & POLLHUP) { // this cannot ;) happen on udp sockets - slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating.")); + slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); exit (1); } else @@ -309,8 +424,9 @@ } } +#if ENABLE_ICMP void -vpn::ipv4_ev (io_watcher &w, short revents) +vpn::icmpv4_ev (io_watcher &w, short revents) { if (revents & (POLLIN | POLLERR)) { @@ -321,17 +437,23 @@ len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); - sockinfo si(sa, PROT_IPv4); + sockinfo si(sa, PROT_ICMPv4); if (len > 0) { pkt->len = len; - // raw sockets deliver the ipv4, but don't expect it on sends - // this is slow, but... - pkt->skip_hdr (IP_OVERHEAD); + icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]); - recv_vpn_packet (pkt, si); + if (hdr->type == ::conf.icmp_type + && hdr->code == 255) + { + // raw sockets deliver the ipv4, but don't expect it on sends + // this is slow, but... + pkt->skip_hdr (ICMP_OVERHEAD); + + recv_vpn_packet (pkt, si); + } } else { @@ -344,7 +466,51 @@ else if (revents & POLLHUP) { // this cannot ;) happen on udp sockets - slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); + slog (L_ERR, _("FATAL: POLLHUP on icmpv4 fd, terminating.")); + exit (1); + } + else + { + slog (L_ERR, + _("FATAL: unknown revents %08x in socket, terminating\n"), + revents); + exit (1); + } +} +#endif + +void +vpn::udpv4_ev (io_watcher &w, short revents) +{ + if (revents & (POLLIN | POLLERR)) + { + vpn_packet *pkt = new vpn_packet; + struct sockaddr_in sa; + socklen_t sa_len = sizeof (sa); + int len; + + len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); + + sockinfo si(sa, PROT_UDPv4); + + if (len > 0) + { + pkt->len = len; + + recv_vpn_packet (pkt, si); + } + else + { + // probably ECONNRESET or somesuch + slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); + } + + delete pkt; + } + else if (revents & POLLHUP) + { + // this cannot ;) happen on udp sockets + slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating.")); exit (1); } else @@ -371,13 +537,13 @@ if (src != THISNODE->id) { - slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating.")); + slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting.")); exit (1); } if (dst == THISNODE->id) { - slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating.")); + slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting.")); exit (1); } @@ -393,15 +559,11 @@ } else { - // broadcast, first check router, then self, then english - connection *router = find_router (); - - if (router) - router->inject_data_packet (pkt, true); - else - for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) - if ((*c)->conf != THISNODE) - (*c)->inject_data_packet (pkt); + // broadcast, this is ugly, but due to the security policy + // we have to connect to all hosts... + for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) + if ((*c)->conf != THISNODE) + (*c)->inject_data_packet (pkt); } } @@ -478,7 +640,7 @@ connection *vpn::find_router () { - u32 prio = 0; + u32 prio = 1; connection *router = 0; for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) @@ -486,9 +648,9 @@ connection *c = *i; if (c->conf->routerprio > prio - && c->connectmode == conf_node::C_ALWAYS - && c->conf != THISNODE - && c->ictx && c->octx) + && c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use + && c->ictx && c->octx + && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode { prio = c->conf->routerprio; router = c; @@ -498,17 +660,17 @@ return router; } -void vpn::connect_request (int id) +void vpn::send_connect_request (int id) { connection *c = find_router (); if (c) - c->connect_request (id); - //else // does not work, because all others must connect to the same router - // // no router found, aggressively connect to all routers - // for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) - // if ((*i)->conf->routerprio) - // (*i)->establish_connection (); + c->send_connect_request (id); + else + // no router found, aggressively connect to all routers + for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) + if ((*i)->conf->routerprio && (*i)->conf != THISNODE) + (*i)->establish_connection (); } void @@ -535,13 +697,16 @@ } vpn::vpn (void) -: event(this, &vpn::event_cb) -, udpv4_ev_watcher(this, &vpn::udpv4_ev) -, ipv4_ev_watcher (this, &vpn::ipv4_ev) -, tap_ev_watcher (this, &vpn::tap_ev) +: event (this, &vpn::event_cb) +, udpv4_ev_watcher (this, &vpn::udpv4_ev) +, ipv4_ev_watcher (this, &vpn::ipv4_ev) #if ENABLE_TCP -, tcpv4_ev_watcher(this, &vpn::tcpv4_ev) +, tcpv4_ev_watcher (this, &vpn::tcpv4_ev) +#endif +#if ENABLE_ICMP +, icmpv4_ev_watcher(this, &vpn::icmpv4_ev) #endif +, tap_ev_watcher (this, &vpn::tap_ev) { }