--- gvpe/src/vpn.C 2003/04/13 16:53:36 1.13 +++ gvpe/src/vpn.C 2004/01/17 01:18:36 1.22 @@ -1,5 +1,6 @@ /* vpn.C -- handle the protocol, encryption, handshaking etc. + Copyright (C) 2003-2004 Marc Lehmann This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -26,15 +27,14 @@ #include #include -#include #include #include #include #include #include -#include -#include -#include +#include + +#include "netcompat.h" #include "pidfile.h" @@ -42,6 +42,8 @@ #include "util.h" #include "vpn.h" +vpn network; // THE vpn (bad design...) + ///////////////////////////////////////////////////////////////////////////// const char *vpn::script_if_up () @@ -54,16 +56,13 @@ mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again char *env; - asprintf (&env, "CONFBASE=%s", confbase); - putenv (env); - asprintf (&env, "NODENAME=%s", THISNODE->nodename); - putenv (env); - asprintf (&env, "NODEID=%d", THISNODE->id); - putenv (env); - asprintf (&env, "IFNAME=%s", tap->interface ()); - putenv (env); - asprintf (&env, "MTU=%d", mtu); - putenv (env); + asprintf (&env, "CONFBASE=%s", confbase); putenv (env); + asprintf (&env, "NODENAME=%s", THISNODE->nodename); putenv (env); + asprintf (&env, "NODEID=%d", THISNODE->id); putenv (env); + asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env); + asprintf (&env, "IFTYPE=%s", IFTYPE); putenv (env); + asprintf (&env, "IFSUBTYPE=%s", IFSUBTYPE); putenv (env); + asprintf (&env, "MTU=%d", mtu); putenv (env); asprintf (&env, "MAC=%02x:%02x:%02x:%02x:%02x:%02x", 0xfe, 0xfd, 0x80, 0x00, THISNODE->id >> 8, THISNODE->id & 0xff); @@ -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. @@ -104,7 +103,7 @@ exit (1); } - ipv4_ev_watcher.start (ipv4_fd, POLLIN); + ipv4_ev_watcher.start (ipv4_fd, EVENT_READ); } udpv4_fd = -1; @@ -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. @@ -142,7 +141,7 @@ exit (1); } - udpv4_ev_watcher.start (udpv4_fd, POLLIN); + udpv4_ev_watcher.start (udpv4_fd, EVENT_READ); } icmpv4_fd = -1; @@ -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. @@ -186,7 +185,7 @@ exit (1); } - icmpv4_ev_watcher.start (icmpv4_fd, POLLIN); + icmpv4_ev_watcher.start (icmpv4_fd, EVENT_READ); } #endif @@ -222,7 +221,7 @@ exit (1); } - tcpv4_ev_watcher.start (tcpv4_fd, POLLIN); + tcpv4_ev_watcher.start (tcpv4_fd, EVENT_READ); } #endif @@ -235,7 +234,7 @@ run_script (run_script_cb (this, &vpn::script_if_up), true); - tap_ev_watcher.start (tap->fd, POLLIN); + tap_ev_watcher.start (tap->fd, EVENT_READ); reconnect_all (); @@ -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; @@ -306,11 +307,13 @@ 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; @@ -325,13 +328,34 @@ 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 (); @@ -355,7 +379,7 @@ 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. @@ -374,7 +398,7 @@ void vpn::ipv4_ev (io_watcher &w, short revents) { - if (revents & (POLLIN | POLLERR)) + if (revents & EVENT_READ) { vpn_packet *pkt = new vpn_packet; struct sockaddr_in sa; @@ -403,12 +427,6 @@ delete pkt; } - else if (revents & POLLHUP) - { - // this cannot ;) happen on udp sockets - slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); - exit (1); - } else { slog (L_ERR, @@ -422,7 +440,7 @@ void vpn::icmpv4_ev (io_watcher &w, short revents) { - if (revents & (POLLIN | POLLERR)) + if (revents & EVENT_READ) { vpn_packet *pkt = new vpn_packet; struct sockaddr_in sa; @@ -437,7 +455,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) @@ -457,12 +475,6 @@ delete pkt; } - else if (revents & POLLHUP) - { - // this cannot ;) happen on udp sockets - slog (L_ERR, _("FATAL: POLLHUP on icmpv4 fd, terminating.")); - exit (1); - } else { slog (L_ERR, @@ -476,7 +488,7 @@ void vpn::udpv4_ev (io_watcher &w, short revents) { - if (revents & (POLLIN | POLLERR)) + if (revents & EVENT_READ) { vpn_packet *pkt = new vpn_packet; struct sockaddr_in sa; @@ -501,12 +513,6 @@ 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 { slog (L_ERR, @@ -519,55 +525,41 @@ void vpn::tap_ev (io_watcher &w, short revents) { - if (revents & POLLIN) + if (revents & EVENT_READ) { /* process data */ tap_packet *pkt; pkt = tap->recv (); - int dst = mac2id (pkt->dst); - int src = mac2id (pkt->src); - - if (src != THISNODE->id) - { - slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating.")); - exit (1); - } + if (!pkt) + return; - if (dst == THISNODE->id) + if (pkt->len > 14) { - slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating.")); - exit (1); - } + int dst = mac2id (pkt->dst); + int src = mac2id (pkt->src); - 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 + + if (dst == THISNODE->id) { - // 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); + 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; } - else if (revents & (POLLHUP | POLLERR)) - { - slog (L_ERR, _("FATAL: POLLHUP or POLLERR on network device fd, terminating.")); - exit (1); - } else abort (); } @@ -582,11 +574,8 @@ slog (L_INFO, _("preparing shutdown...")); shutdown_all (); - remove_pid (pidfilename); - slog (L_INFO, _("terminating")); - exit (0); } @@ -599,8 +588,6 @@ events = 0; } - - w.at = TSTAMP_CANCEL; } void