--- gvpe/src/vpn.C 2003/04/08 03:25:35 1.10 +++ gvpe/src/vpn.C 2003/10/16 02:28:36 1.18 @@ -26,14 +26,14 @@ #include #include -#include #include #include #include #include -#include -#include -#include +#include +#include + +#include "netcompat.h" #include "pidfile.h" @@ -83,7 +83,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 +115,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 +154,8 @@ if (icmpv4_fd < 0) return -1; + fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); + #ifdef ICMP_FILTER { icmp_filter oval; @@ -161,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. @@ -193,6 +199,8 @@ if (tcpv4_fd < 0) return -1; + fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); + // standard daemon practise... { int oval = 1; @@ -264,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; @@ -293,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; @@ -310,17 +323,39 @@ 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 +vpn::inject_data_packet (tap_packet *pkt, int dst) +{ + if (dst) + { + // unicast + if (dst != THISNODE->id) + conns[dst - 1]->inject_data_packet (pkt); + } + else + { + // 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, true); + } +} + +void vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) { unsigned int src = pkt->src (); @@ -341,10 +376,10 @@ { 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) + else if (dst != THISNODE->id) { if (THISNODE->routerprio) // the tos setting gets lost here. who cares. @@ -407,6 +442,7 @@ } } +#if ENABLE_ICMP void vpn::icmpv4_ev (io_watcher &w, short revents) { @@ -425,7 +461,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 +495,7 @@ exit (1); } } +#endif void vpn::udpv4_ev (io_watcher &w, short revents) @@ -513,43 +550,30 @@ 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 (dst > conns.size ()) - slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); - else - { - if (dst) + if (src != THISNODE->id) { - // unicast - if (dst != THISNODE->id) - conns[dst - 1]->inject_data_packet (pkt); + slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting.")); + exit (1); } - 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); + if (dst == THISNODE->id) + { + 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 + inject_data_packet (pkt, dst); } delete pkt; @@ -625,7 +649,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 +657,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 +678,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 (); }