--- gvpe/src/vpn.C 2003/04/13 00:42:04 1.12 +++ gvpe/src/vpn.C 2003/10/15 06:06:41 1.17 @@ -26,15 +26,14 @@ #include #include -#include #include #include #include #include #include -#include -#include -#include +#include + +#include "netcompat.h" #include "pidfile.h" @@ -86,7 +85,7 @@ fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); -#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. @@ -124,7 +123,7 @@ 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. @@ -168,7 +167,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. @@ -273,7 +272,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; @@ -302,14 +303,17 @@ 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); - icmphdr *hdr = (icmphdr *)&((*pkt)[0]); + icmp_header *hdr = (icmp_header *)&((*pkt)[0]); hdr->type = ::conf.icmp_type; hdr->code = 255; hdr->checksum = 0; @@ -319,11 +323,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; @@ -350,8 +357,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) { @@ -416,6 +423,7 @@ } } +#if ENABLE_ICMP void vpn::icmpv4_ev (io_watcher &w, short revents) { @@ -434,7 +442,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) @@ -468,6 +476,7 @@ exit (1); } } +#endif void vpn::udpv4_ev (io_watcher &w, short revents) @@ -522,42 +531,44 @@ pkt = tap->recv (); - int dst = mac2id (pkt->dst); - int src = mac2id (pkt->src); + if (!pkt) + return; - if (src != THISNODE->id) + if (pkt->len > 14) { - slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating.")); - exit (1); - } + int dst = mac2id (pkt->dst); + int src = mac2id (pkt->src); - if (dst == THISNODE->id) - { - slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating.")); - exit (1); - } + if (src != THISNODE->id) + { + slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting.")); + exit (1); + } - if (dst > conns.size ()) - slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); - else - { - if (dst) + if (dst == THISNODE->id) { - // unicast - if (dst != THISNODE->id) - conns[dst - 1]->inject_data_packet (pkt); + slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting.")); + exit (1); } + + if (dst > conns.size ()) + slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); else { - // broadcast, first check router, then self, then english - connection *router = find_router (); - - if (router) - router->inject_data_packet (pkt, true); + if (dst) + { + // unicast + if (dst != THISNODE->id) + conns[dst - 1]->inject_data_packet (pkt); + } 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); + } } } @@ -641,12 +652,10 @@ { connection *c = *i; - if (c->conf->routerprio >= prio + if (c->conf->routerprio > prio && 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 - && (!THISNODE->routerprio - || c->conf->routerprio <= THISNODE->routerprio)) + && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode { prio = c->conf->routerprio; router = c; @@ -665,7 +674,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 (); }