--- gvpe/src/vpn.C 2003/04/04 05:26:45 1.4 +++ gvpe/src/vpn.C 2003/04/08 02:00:54 1.9 @@ -28,11 +28,12 @@ #include #include #include -#include -#include #include #include #include +#include +#include +#include #include "pidfile.h" @@ -40,13 +41,6 @@ #include "util.h" #include "vpn.h" -#if ENABLE_TCP -# include -# include -# include -# include -#endif - ///////////////////////////////////////////////////////////////////////////// const char *vpn::script_if_up () @@ -80,9 +74,39 @@ 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; + +#ifdef 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); @@ -95,14 +119,6 @@ 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 // this I really consider a linux bug. I am neither connected // nor do I fragment myself. Linux still sets DF and doesn't @@ -113,24 +129,37 @@ } #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); +#ifdef ICMP_FILTER + { + icmp_filter oval; + oval.data = 0xffffffff; + if (::conf.icmp_type < 32) + oval.data &= ~(1 << ::conf.icmp_type); - 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); - } + setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); + } +#endif #ifdef IP_MTU_DISCOVER // this I really consider a linux bug. I am neither connected @@ -138,15 +167,26 @@ // 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); @@ -193,18 +233,91 @@ return 0; } -void +// send a vpn packet out to other hosts +bool +vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) +{ + switch (si.prot) + { + case PROT_IPv4: + return send_ipv4_packet (pkt, si, tos); + + case PROT_UDPv4: + return send_udpv4_packet (pkt, si, tos); + +#if ENABLE_TCP + case PROT_TCPv4: + 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); + return false; + } +} + +bool vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) { setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 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; +} + +bool +vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) +{ + setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); + + pkt->unshift_hdr (4); + + icmphdr *hdr = (icmphdr *)&((*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; +} + +bool vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) { setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); + + return true; } void @@ -221,6 +334,9 @@ || pkt->typ () >= vpn_packet::PT_MAX) slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); + else if (dst > conns.size ()) + slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), + (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); else { connection *c = conns[src - 1]; @@ -229,19 +345,23 @@ slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), c->conf->nodename, (const char *)rsi); else if (dst != 0 && dst != THISNODE->id) - // FORWARDING NEEDED ;) - slog (L_WARN, - _("received frame for node %d ('%s') from %s, but this is node %d ('%s')"), - dst, conns[dst - 1]->conf->nodename, - (const char *)rsi, - THISNODE->id, THISNODE->nodename); + { + if (THISNODE->routerprio) + // the tos setting gets lost here. who cares. + conns[dst - 1]->inject_vpn_packet (pkt); + else + slog (L_WARN, + _("%s(%s): forwarding request (=> %s), but we are no router"), + c->conf->nodename, (const char *)rsi, + conns[dst - 1]->conf->nodename); + } else c->recv_vpn_packet (pkt, rsi); } } void -vpn::udpv4_ev (io_watcher &w, short revents) +vpn::ipv4_ev (io_watcher &w, short revents) { if (revents & (POLLIN | POLLERR)) { @@ -250,14 +370,18 @@ socklen_t sa_len = sizeof (sa); int len; - len = recvfrom (w.p->fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); + len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); - sockinfo si(sa); + 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 @@ -271,7 +395,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 @@ -284,7 +408,7 @@ } void -vpn::ipv4_ev (io_watcher &w, short revents) +vpn::icmpv4_ev (io_watcher &w, short revents) { if (revents & (POLLIN | POLLERR)) { @@ -293,19 +417,25 @@ socklen_t sa_len = sizeof (sa); int len; - len = recvfrom (w.p->fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); + 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); + icmphdr *hdr = (icmphdr *)&((*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 { @@ -318,7 +448,7 @@ 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 @@ -330,180 +460,49 @@ } } -#if ENABLE_TCP - -struct tcp_connection; - -struct lt_sockinfo +void +vpn::udpv4_ev (io_watcher &w, short revents) { - bool operator()(const sockinfo *a, const sockinfo *b) const - { - return *a < *b; - } -}; - -struct tcp_si_map : public map { - void cleaner_cb (time_watcher &w); time_watcher cleaner; - - tcp_si_map () - : cleaner(this, &tcp_si_map::cleaner_cb) - { - cleaner.start (0); - } -} tcp_si; - -struct tcp_connection : io_watcher { - tstamp last_activity; - const sockinfo si; - vpn &v; - bool ok; - - vpn_packet *r_pkt; - u32 r_len, r_ofs; - - void tcpv4_ev (io_watcher &w, short revents); - - operator tcp_si_map::value_type() - { - return tcp_si_map::value_type (&si, this); - } - - tcp_connection (int fd, const sockinfo &si_, vpn &v_) - : v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev) + if (revents & (POLLIN | POLLERR)) { - last_activity = NOW; - ok = false; - r_pkt = 0; - start (fd, POLLOUT); - } - - ~tcp_connection () { if (p) close (p->fd); } -}; - -void tcp_si_map::cleaner_cb (time_watcher &w) -{ - w.at = NOW + 600; - tstamp to = NOW - ::conf.keepalive - 30; - - for (iterator i = begin (); i != end(); ) - if (i->second->last_activity >= to) - ++i; - else - { - erase (i); - i = begin (); - } -} + vpn_packet *pkt = new vpn_packet; + struct sockaddr_in sa; + socklen_t sa_len = sizeof (sa); + int len; -void -vpn::send_tcpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) -{ - tcp_si_map::iterator info = tcp_si.find (&si); + len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); - if (info == tcp_si.end ()) - { - // woaw, the first lost packet ;) - int fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); + sockinfo si(sa, PROT_UDPv4); - if (fd >= 0) + if (len > 0) { - fcntl (fd, F_SETFL, O_NONBLOCK); - - if (connect (fd, si.sav4 (), si.salenv4 ()) >= 0 - || errno == EINPROGRESS) - { - tcp_connection *i = new tcp_connection (fd, si, *this); + pkt->len = len; - tcp_si.insert (*i); - } - else - close (fd); + recv_vpn_packet (pkt, si); } - } - else - { - tcp_connection *i = info->second; - - i->last_activity = NOW; - - if (i->ok) + else { - setsockopt (i->p->fd, SOL_IP, IP_TOS, &tos, sizeof tos); - - // we use none of the advantages of tcp - write (i->p->fd, (void *)pkt, pkt->len + sizeof (u32)) != pkt->len + sizeof (u32); + // probably ECONNRESET or somesuch + slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); } - } - -#if 0 - setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); - sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); -#endif -} - -void -tcp_connection::tcpv4_ev (io_watcher &w, short revents) -{ - last_activity = NOW; - if (!ok) // just established? - { - ok = true; - set (POLLIN); + delete pkt; } - - if (revents & (POLLIN | POLLERR)) + else if (revents & POLLHUP) { - u32 len; - - if (sizeof (len) == read (p->fd, &len, sizeof (len))) - { - vpn_packet *pkt = new vpn_packet; - - if (len == read (p->fd, &((*pkt)[0]), len)) - { - pkt->len = len; - - v.recv_vpn_packet (pkt, si); - return; - } - - delete pkt; - } - - tcp_si.erase (&si); - - set (0);//D + // this cannot ;) happen on udp sockets + slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating.")); + exit (1); } -} - -void -vpn::tcpv4_ev (io_watcher &w, short revents) -{ - if (revents & (POLLIN | POLLERR)) + else { - struct sockaddr_in sa; - socklen_t sa_len = sizeof (sa); - int len; - - int fd = accept (w.p->fd, (sockaddr *)&sa, &sa_len); - - if (fd >= 0) - { - fcntl (fd, F_SETFL, O_NONBLOCK); - - sockinfo si(sa, PROT_TCPv4); - tcp_connection *i = new tcp_connection (fd, si, *this); - - slog (L_DEBUG, _("accepted tcp connection from %s\n"), (const char *)si);//D - - tcp_si.insert (*i); - } + slog (L_ERR, + _("FATAL: unknown revents %08x in socket, terminating\n"), + revents); + exit (1); } } -#endif - void vpn::tap_ev (io_watcher &w, short revents) { @@ -646,17 +645,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)->establish_connection (); } void @@ -683,13 +682,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) { }