ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vpn.C
(Generate patch)

Comparing gvpe/src/vpn.C (file contents):
Revision 1.9 by pcg, Tue Apr 8 02:00:54 2003 UTC vs.
Revision 1.16 by pcg, Tue Oct 14 19:59:16 2003 UTC

24#include <cstring> 24#include <cstring>
25#include <cstdio> 25#include <cstdio>
26 26
27#include <sys/types.h> 27#include <sys/types.h>
28#include <sys/socket.h> 28#include <sys/socket.h>
29#include <sys/poll.h>
30#include <sys/wait.h> 29#include <sys/wait.h>
31#include <errno.h> 30#include <errno.h>
32#include <time.h> 31#include <time.h>
33#include <unistd.h> 32#include <unistd.h>
34#include <arpa/inet.h> 33#include <fcntl.h>
35#include <netinet/in.h> 34#include <sys/socket.h>
36#include <netinet/icmp.h> 35
36#include "netcompat.h"
37 37
38#include "pidfile.h" 38#include "pidfile.h"
39 39
40#include "connection.h" 40#include "connection.h"
41#include "util.h" 41#include "util.h"
81 ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); 81 ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto);
82 82
83 if (ipv4_fd < 0) 83 if (ipv4_fd < 0)
84 return -1; 84 return -1;
85 85
86#ifdef IP_MTU_DISCOVER 86 fcntl (ipv4_fd, F_SETFL, O_NONBLOCK);
87
88#if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
87 // this I really consider a linux bug. I am neither connected 89 // this I really consider a linux bug. I am neither connected
88 // nor do I fragment myself. Linux still sets DF and doesn't 90 // nor do I fragment myself. Linux still sets DF and doesn't
89 // fragment for me sometimes. 91 // fragment for me sometimes.
90 { 92 {
91 int oval = IP_PMTUDISC_DONT; 93 int oval = IP_PMTUDISC_DONT;
111 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); 113 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
112 114
113 if (udpv4_fd < 0) 115 if (udpv4_fd < 0)
114 return -1; 116 return -1;
115 117
118 fcntl (udpv4_fd, F_SETFL, O_NONBLOCK);
119
116 // standard daemon practise... 120 // standard daemon practise...
117 { 121 {
118 int oval = 1; 122 int oval = 1;
119 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 123 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
120 } 124 }
121 125
122#ifdef IP_MTU_DISCOVER 126#if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
123 // this I really consider a linux bug. I am neither connected 127 // this I really consider a linux bug. I am neither connected
124 // nor do I fragment myself. Linux still sets DF and doesn't 128 // nor do I fragment myself. Linux still sets DF and doesn't
125 // fragment for me sometimes. 129 // fragment for me sometimes.
126 { 130 {
127 int oval = IP_PMTUDISC_DONT; 131 int oval = IP_PMTUDISC_DONT;
148 icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); 152 icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP);
149 153
150 if (icmpv4_fd < 0) 154 if (icmpv4_fd < 0)
151 return -1; 155 return -1;
152 156
157 fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK);
158
153#ifdef ICMP_FILTER 159#ifdef ICMP_FILTER
154 { 160 {
155 icmp_filter oval; 161 icmp_filter oval;
156 oval.data = 0xffffffff; 162 oval.data = 0xffffffff;
157 if (::conf.icmp_type < 32) 163 if (::conf.icmp_type < 32)
159 165
160 setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); 166 setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval);
161 } 167 }
162#endif 168#endif
163 169
164#ifdef IP_MTU_DISCOVER 170#if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
165 // this I really consider a linux bug. I am neither connected 171 // this I really consider a linux bug. I am neither connected
166 // nor do I fragment myself. Linux still sets DF and doesn't 172 // nor do I fragment myself. Linux still sets DF and doesn't
167 // fragment for me sometimes. 173 // fragment for me sometimes.
168 { 174 {
169 int oval = IP_PMTUDISC_DONT; 175 int oval = IP_PMTUDISC_DONT;
191 tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); 197 tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
192 198
193 if (tcpv4_fd < 0) 199 if (tcpv4_fd < 0)
194 return -1; 200 return -1;
195 201
202 fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK);
203
196 // standard daemon practise... 204 // standard daemon practise...
197 { 205 {
198 int oval = 1; 206 int oval = 1;
199 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 207 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
200 } 208 }
262} 270}
263 271
264bool 272bool
265vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 273vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
266{ 274{
275#if defined(SOL_IP) && defined(IP_TOS)
267 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 276 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
277#endif
268 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 278 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
269 279
270 return true; 280 return true;
271} 281}
272 282
291 sum += (sum >> 16); // carry 301 sum += (sum >> 16); // carry
292 302
293 return ~sum; 303 return ~sum;
294} 304}
295 305
306#if ENABLE_ICMP
296bool 307bool
297vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 308vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
298{ 309{
310#if defined(SOL_IP) && defined(IP_TOS)
299 setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 311 setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
312#endif
300 313
301 pkt->unshift_hdr (4); 314 pkt->unshift_hdr (4);
302 315
303 icmphdr *hdr = (icmphdr *)&((*pkt)[0]); 316 icmp_header *hdr = (icmp_header *)&((*pkt)[0]);
304 hdr->type = ::conf.icmp_type; 317 hdr->type = ::conf.icmp_type;
305 hdr->code = 255; 318 hdr->code = 255;
306 hdr->checksum = 0; 319 hdr->checksum = 0;
307 hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); 320 hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len);
308 321
309 sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 322 sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
310 323
311 return true; 324 return true;
312} 325}
326#endif
313 327
314bool 328bool
315vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 329vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
316{ 330{
331#if defined(SOL_IP) && defined(IP_TOS)
317 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 332 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
333#endif
318 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 334 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
319 335
320 return true; 336 return true;
321} 337}
322 338
339 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); 355 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ());
340 else 356 else
341 { 357 {
342 connection *c = conns[src - 1]; 358 connection *c = conns[src - 1];
343 359
344 if (dst == 0 && !THISNODE->routerprio) 360 if (dst == 0)
345 slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), 361 slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"),
346 c->conf->nodename, (const char *)rsi); 362 c->conf->nodename, (const char *)rsi);
347 else if (dst != 0 && dst != THISNODE->id) 363 else if (dst != 0 && dst != THISNODE->id)
348 { 364 {
349 if (THISNODE->routerprio) 365 if (THISNODE->routerprio)
350 // the tos setting gets lost here. who cares. 366 // the tos setting gets lost here. who cares.
405 revents); 421 revents);
406 exit (1); 422 exit (1);
407 } 423 }
408} 424}
409 425
426#if ENABLE_ICMP
410void 427void
411vpn::icmpv4_ev (io_watcher &w, short revents) 428vpn::icmpv4_ev (io_watcher &w, short revents)
412{ 429{
413 if (revents & (POLLIN | POLLERR)) 430 if (revents & (POLLIN | POLLERR))
414 { 431 {
423 440
424 if (len > 0) 441 if (len > 0)
425 { 442 {
426 pkt->len = len; 443 pkt->len = len;
427 444
428 icmphdr *hdr = (icmphdr *)&((*pkt)[IP_OVERHEAD]); 445 icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]);
429 446
430 if (hdr->type == ::conf.icmp_type 447 if (hdr->type == ::conf.icmp_type
431 && hdr->code == 255) 448 && hdr->code == 255)
432 { 449 {
433 // raw sockets deliver the ipv4, but don't expect it on sends 450 // raw sockets deliver the ipv4, but don't expect it on sends
457 _("FATAL: unknown revents %08x in socket, terminating\n"), 474 _("FATAL: unknown revents %08x in socket, terminating\n"),
458 revents); 475 revents);
459 exit (1); 476 exit (1);
460 } 477 }
461} 478}
479#endif
462 480
463void 481void
464vpn::udpv4_ev (io_watcher &w, short revents) 482vpn::udpv4_ev (io_watcher &w, short revents)
465{ 483{
466 if (revents & (POLLIN | POLLERR)) 484 if (revents & (POLLIN | POLLERR))
516 int dst = mac2id (pkt->dst); 534 int dst = mac2id (pkt->dst);
517 int src = mac2id (pkt->src); 535 int src = mac2id (pkt->src);
518 536
519 if (src != THISNODE->id) 537 if (src != THISNODE->id)
520 { 538 {
521 slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating.")); 539 slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting."));
522 exit (1); 540 exit (1);
523 } 541 }
524 542
525 if (dst == THISNODE->id) 543 if (dst == THISNODE->id)
526 { 544 {
527 slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating.")); 545 slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting."));
528 exit (1); 546 exit (1);
529 } 547 }
530 548
531 if (dst > conns.size ()) 549 if (dst > conns.size ())
532 slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); 550 slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst);
538 if (dst != THISNODE->id) 556 if (dst != THISNODE->id)
539 conns[dst - 1]->inject_data_packet (pkt); 557 conns[dst - 1]->inject_data_packet (pkt);
540 } 558 }
541 else 559 else
542 { 560 {
543 // broadcast, first check router, then self, then english 561 // broadcast, this is ugly, but due to the security policy
544 connection *router = find_router (); 562 // we have to connect to all hosts...
545
546 if (router)
547 router->inject_data_packet (pkt, true);
548 else
549 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) 563 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
550 if ((*c)->conf != THISNODE) 564 if ((*c)->conf != THISNODE)
551 (*c)->inject_data_packet (pkt); 565 (*c)->inject_data_packet (pkt);
552 } 566 }
553 } 567 }
554 568
555 delete pkt; 569 delete pkt;
556 } 570 }
623 } 637 }
624} 638}
625 639
626connection *vpn::find_router () 640connection *vpn::find_router ()
627{ 641{
628 u32 prio = 0; 642 u32 prio = 1;
629 connection *router = 0; 643 connection *router = 0;
630 644
631 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) 645 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
632 { 646 {
633 connection *c = *i; 647 connection *c = *i;
634 648
635 if (c->conf->routerprio > prio 649 if (c->conf->routerprio > prio
636 && c->connectmode == conf_node::C_ALWAYS 650 && c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use
637 && c->conf != THISNODE
638 && c->ictx && c->octx) 651 && c->ictx && c->octx
652 && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode
639 { 653 {
640 prio = c->conf->routerprio; 654 prio = c->conf->routerprio;
641 router = c; 655 router = c;
642 } 656 }
643 } 657 }
652 if (c) 666 if (c)
653 c->send_connect_request (id); 667 c->send_connect_request (id);
654 else 668 else
655 // no router found, aggressively connect to all routers 669 // no router found, aggressively connect to all routers
656 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) 670 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
657 if ((*i)->conf->routerprio) 671 if ((*i)->conf->routerprio && (*i)->conf != THISNODE)
658 (*i)->establish_connection (); 672 (*i)->establish_connection ();
659} 673}
660 674
661void 675void
662connection::dump_status () 676connection::dump_status ()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines