--- gvpe/src/vpn.C 2003/04/08 03:25:35 1.10 +++ gvpe/src/vpn.C 2003/10/14 03:22:09 1.14 @@ -31,9 +31,23 @@ #include #include #include -#include +#include +#include #include -#include +#include +#include +#ifdef HAVE_NETINET_IN_SYSTM_H +# include +#endif +#ifdef HAVE_NETINET_IP_H +# include +#endif +#ifdef HAVE_NETINET_TCP_H +# include +#endif +#if ENABLE_ICMP +# include +#endif #include "pidfile.h" @@ -41,6 +55,10 @@ #include "util.h" #include "vpn.h" +#if !defined(SOL_IP) && defined(IPPROTO_IP) +# define SOL_IP IPPROTO_IP +#endif + ///////////////////////////////////////////////////////////////////////////// const char *vpn::script_if_up () @@ -83,7 +101,9 @@ if (ipv4_fd < 0) return -1; -#ifdef IP_MTU_DISCOVER + 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. @@ -113,13 +133,15 @@ 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); } -#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. @@ -150,6 +172,8 @@ if (icmpv4_fd < 0) return -1; + fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); + #ifdef ICMP_FILTER { icmp_filter oval; @@ -161,7 +185,7 @@ } #endif -#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. @@ -193,6 +217,8 @@ if (tcpv4_fd < 0) return -1; + fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); + // standard daemon practise... { int oval = 1; @@ -264,7 +290,9 @@ 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; @@ -293,14 +321,34 @@ return ~sum; } +#if ENABLE_ICMP +struct icmp_header { + u8 type; + u8 code; + u16 checksum; + union { + struct { + u16 id; + u16 sequence; + } echo; + u32 gateway; + struct { + u16 unused; + u16 mtu; + } frag; + } un; +}; + 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); - icmphdr *hdr = (icmphdr *)&((*pkt)[0]); + icmp_header *hdr = (icmp_header *)&((*pkt)[0]); hdr->type = ::conf.icmp_type; hdr->code = 255; hdr->checksum = 0; @@ -310,11 +358,14 @@ 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; @@ -341,8 +392,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) { @@ -407,6 +458,7 @@ } } +#if ENABLE_ICMP void vpn::icmpv4_ev (io_watcher &w, short revents) { @@ -425,7 +477,7 @@ { pkt->len = len; - icmphdr *hdr = (icmphdr *)&((*pkt)[IP_OVERHEAD]); + icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]); if (hdr->type == ::conf.icmp_type && hdr->code == 255) @@ -459,6 +511,7 @@ exit (1); } } +#endif void vpn::udpv4_ev (io_watcher &w, short revents) @@ -518,13 +571,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); } @@ -540,15 +593,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); } } @@ -625,7 +674,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) @@ -633,9 +682,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; @@ -654,7 +703,7 @@ else // no router found, aggressively connect to all routers for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) - if ((*i)->conf->routerprio) + if ((*i)->conf->routerprio && (*i)->conf != THISNODE) (*i)->establish_connection (); }