--- gvpe/src/vpn.C 2003/10/14 15:48:15 1.15 +++ gvpe/src/vpn.C 2004/07/25 18:11:54 1.26 @@ -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 @@ -20,13 +21,12 @@ #include -#include -#include #include +#include +#include #include #include -#include #include #include #include @@ -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); @@ -101,10 +100,10 @@ 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); + exit (EXIT_FAILURE); } - ipv4_ev_watcher.start (ipv4_fd, POLLIN); + ipv4_ev_watcher.start (ipv4_fd, EVENT_READ); } udpv4_fd = -1; @@ -139,10 +138,10 @@ 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); + exit (EXIT_FAILURE); } - udpv4_ev_watcher.start (udpv4_fd, POLLIN); + udpv4_ev_watcher.start (udpv4_fd, EVENT_READ); } icmpv4_fd = -1; @@ -183,10 +182,10 @@ 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); + exit (EXIT_FAILURE); } - icmpv4_ev_watcher.start (icmpv4_fd, POLLIN); + icmpv4_ev_watcher.start (icmpv4_fd, EVENT_READ); } #endif @@ -213,16 +212,42 @@ if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ())) { slog (L_ERR, _("can't bind tcpv4 on %s: %s"), (const char *)si, strerror (errno)); - exit (1); + exit (EXIT_FAILURE); } if (listen (tcpv4_fd, 5)) { slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno)); - exit (1); + exit (EXIT_FAILURE); + } + + tcpv4_ev_watcher.start (tcpv4_fd, EVENT_READ); + } +#endif + +#if ENABLE_DNS + if (THISNODE->protocols & PROT_DNSv4) + { + dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); + + if (dnsv4_fd < 0) + return -1; + + // standard daemon practise... + { + int oval = 1; + setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); + } + + sockinfo si (THISNODE, PROT_DNSv4); + + if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ())) + { + slog (L_ERR, _("can't bind dnsv4 on %s: %s"), (const char *)si, strerror (errno)); + exit (EXIT_FAILURE); } - tcpv4_ev_watcher.start (tcpv4_fd, POLLIN); + dnsv4_ev_watcher.start (dnsv4_fd, EVENT_READ); } #endif @@ -230,12 +255,12 @@ if (!tap) //D this, of course, never catches { slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); - exit (1); + exit (EXIT_FAILURE); } 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 (); @@ -264,6 +289,11 @@ return send_icmpv4_packet (pkt, si, tos); #endif +#if ENABLE_DNS + case PROT_DNSv4: + return send_dnsv4_packet (pkt, si, tos); +#endif + default: slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); return false; @@ -338,6 +368,25 @@ } 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 (); @@ -361,7 +410,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. @@ -380,7 +429,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; @@ -409,18 +458,12 @@ 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, _("FATAL: unknown revents %08x in socket, terminating\n"), revents); - exit (1); + exit (EXIT_FAILURE); } } @@ -428,7 +471,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; @@ -463,18 +506,12 @@ 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, _("FATAL: unknown revents %08x in socket, terminating\n"), revents); - exit (1); + exit (EXIT_FAILURE); } } #endif @@ -482,7 +519,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; @@ -507,73 +544,53 @@ 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, _("FATAL: unknown revents %08x in socket, terminating\n"), revents); - exit (1); + exit (EXIT_FAILURE); } } 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, exiting.")); - exit (1); - } + if (!pkt) + return; - if (dst == THISNODE->id) + if (pkt->len > 14) { - slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting.")); - 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 (EXIT_FAILURE); } - 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 (EXIT_FAILURE); } + + 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 (); } @@ -588,12 +605,9 @@ slog (L_INFO, _("preparing shutdown...")); shutdown_all (); - - remove_pid (pidfilename); - + remove_pid (conf.pidfilename); slog (L_INFO, _("terminating")); - - exit (0); + exit (EXIT_SUCCESS); } if (events & EVENT_RECONNECT) @@ -605,8 +619,6 @@ events = 0; } - - w.at = TSTAMP_CANCEL; } void @@ -706,6 +718,9 @@ #if ENABLE_ICMP , icmpv4_ev_watcher(this, &vpn::icmpv4_ev) #endif +#if ENABLE_DNS +, dnsv4_ev_watcher (this, &vpn::dnsv4_ev) +#endif , tap_ev_watcher (this, &vpn::tap_ev) { }