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.20 by pcg, Thu Oct 16 14:12:00 2003 UTC

1/* 1/*
2 vpn.C -- handle the protocol, encryption, handshaking etc. 2 vpn.C -- handle the protocol, encryption, handshaking etc.
3 Copyright (C) 2003 Marc Lehmann <pcg@goof.com>
3 4
4 This program is free software; you can redistribute it and/or modify 5 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by 6 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or 7 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version. 8 (at your option) any later version.
24#include <cstring> 25#include <cstring>
25#include <cstdio> 26#include <cstdio>
26 27
27#include <sys/types.h> 28#include <sys/types.h>
28#include <sys/socket.h> 29#include <sys/socket.h>
29#include <sys/poll.h>
30#include <sys/wait.h> 30#include <sys/wait.h>
31#include <errno.h> 31#include <errno.h>
32#include <time.h> 32#include <time.h>
33#include <unistd.h> 33#include <unistd.h>
34#include <arpa/inet.h> 34#include <fcntl.h>
35#include <netinet/in.h> 35#include <sys/socket.h>
36#include <netinet/icmp.h> 36
37#include "netcompat.h"
37 38
38#include "pidfile.h" 39#include "pidfile.h"
39 40
40#include "connection.h" 41#include "connection.h"
41#include "util.h" 42#include "util.h"
42#include "vpn.h" 43#include "vpn.h"
44
45vpn network; // THE vpn (bad design...)
43 46
44///////////////////////////////////////////////////////////////////////////// 47/////////////////////////////////////////////////////////////////////////////
45 48
46const char *vpn::script_if_up () 49const char *vpn::script_if_up ()
47{ 50{
81 ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); 84 ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto);
82 85
83 if (ipv4_fd < 0) 86 if (ipv4_fd < 0)
84 return -1; 87 return -1;
85 88
86#ifdef IP_MTU_DISCOVER 89 fcntl (ipv4_fd, F_SETFL, O_NONBLOCK);
90
91#if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
87 // this I really consider a linux bug. I am neither connected 92 // this I really consider a linux bug. I am neither connected
88 // nor do I fragment myself. Linux still sets DF and doesn't 93 // nor do I fragment myself. Linux still sets DF and doesn't
89 // fragment for me sometimes. 94 // fragment for me sometimes.
90 { 95 {
91 int oval = IP_PMTUDISC_DONT; 96 int oval = IP_PMTUDISC_DONT;
111 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); 116 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
112 117
113 if (udpv4_fd < 0) 118 if (udpv4_fd < 0)
114 return -1; 119 return -1;
115 120
121 fcntl (udpv4_fd, F_SETFL, O_NONBLOCK);
122
116 // standard daemon practise... 123 // standard daemon practise...
117 { 124 {
118 int oval = 1; 125 int oval = 1;
119 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 126 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
120 } 127 }
121 128
122#ifdef IP_MTU_DISCOVER 129#if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
123 // this I really consider a linux bug. I am neither connected 130 // this I really consider a linux bug. I am neither connected
124 // nor do I fragment myself. Linux still sets DF and doesn't 131 // nor do I fragment myself. Linux still sets DF and doesn't
125 // fragment for me sometimes. 132 // fragment for me sometimes.
126 { 133 {
127 int oval = IP_PMTUDISC_DONT; 134 int oval = IP_PMTUDISC_DONT;
148 icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); 155 icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP);
149 156
150 if (icmpv4_fd < 0) 157 if (icmpv4_fd < 0)
151 return -1; 158 return -1;
152 159
160 fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK);
161
153#ifdef ICMP_FILTER 162#ifdef ICMP_FILTER
154 { 163 {
155 icmp_filter oval; 164 icmp_filter oval;
156 oval.data = 0xffffffff; 165 oval.data = 0xffffffff;
157 if (::conf.icmp_type < 32) 166 if (::conf.icmp_type < 32)
159 168
160 setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); 169 setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval);
161 } 170 }
162#endif 171#endif
163 172
164#ifdef IP_MTU_DISCOVER 173#if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
165 // this I really consider a linux bug. I am neither connected 174 // this I really consider a linux bug. I am neither connected
166 // nor do I fragment myself. Linux still sets DF and doesn't 175 // nor do I fragment myself. Linux still sets DF and doesn't
167 // fragment for me sometimes. 176 // fragment for me sometimes.
168 { 177 {
169 int oval = IP_PMTUDISC_DONT; 178 int oval = IP_PMTUDISC_DONT;
191 tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); 200 tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
192 201
193 if (tcpv4_fd < 0) 202 if (tcpv4_fd < 0)
194 return -1; 203 return -1;
195 204
205 fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK);
206
196 // standard daemon practise... 207 // standard daemon practise...
197 { 208 {
198 int oval = 1; 209 int oval = 1;
199 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 210 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
200 } 211 }
262} 273}
263 274
264bool 275bool
265vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 276vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
266{ 277{
278#if defined(SOL_IP) && defined(IP_TOS)
267 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 279 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
280#endif
268 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 281 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
269 282
270 return true; 283 return true;
271} 284}
272 285
291 sum += (sum >> 16); // carry 304 sum += (sum >> 16); // carry
292 305
293 return ~sum; 306 return ~sum;
294} 307}
295 308
309#if ENABLE_ICMP
296bool 310bool
297vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 311vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
298{ 312{
313#if defined(SOL_IP) && defined(IP_TOS)
299 setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 314 setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
315#endif
300 316
301 pkt->unshift_hdr (4); 317 pkt->unshift_hdr (4);
302 318
303 icmphdr *hdr = (icmphdr *)&((*pkt)[0]); 319 icmp_header *hdr = (icmp_header *)&((*pkt)[0]);
304 hdr->type = ::conf.icmp_type; 320 hdr->type = ::conf.icmp_type;
305 hdr->code = 255; 321 hdr->code = 255;
306 hdr->checksum = 0; 322 hdr->checksum = 0;
307 hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); 323 hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len);
308 324
309 sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 325 sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
310 326
311 return true; 327 return true;
312} 328}
329#endif
313 330
314bool 331bool
315vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 332vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
316{ 333{
334#if defined(SOL_IP) && defined(IP_TOS)
317 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 335 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
336#endif
318 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 337 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
319 338
320 return true; 339 return true;
340}
341
342void
343vpn::inject_data_packet (tap_packet *pkt, int dst)
344{
345 if (dst)
346 {
347 // unicast
348 if (dst != THISNODE->id)
349 conns[dst - 1]->inject_data_packet (pkt);
350 }
351 else
352 {
353 // broadcast, this is ugly, but due to the security policy
354 // we have to connect to all hosts...
355 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
356 if ((*c)->conf != THISNODE)
357 (*c)->inject_data_packet (pkt, true);
358 }
321} 359}
322 360
323void 361void
324vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) 362vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
325{ 363{
339 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); 377 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ());
340 else 378 else
341 { 379 {
342 connection *c = conns[src - 1]; 380 connection *c = conns[src - 1];
343 381
344 if (dst == 0 && !THISNODE->routerprio) 382 if (dst == 0)
345 slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), 383 slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"),
346 c->conf->nodename, (const char *)rsi); 384 c->conf->nodename, (const char *)rsi);
347 else if (dst != 0 && dst != THISNODE->id) 385 else if (dst != THISNODE->id)
348 { 386 {
349 if (THISNODE->routerprio) 387 if (THISNODE->routerprio)
350 // the tos setting gets lost here. who cares. 388 // the tos setting gets lost here. who cares.
351 conns[dst - 1]->inject_vpn_packet (pkt); 389 conns[dst - 1]->inject_vpn_packet (pkt);
352 else 390 else
405 revents); 443 revents);
406 exit (1); 444 exit (1);
407 } 445 }
408} 446}
409 447
448#if ENABLE_ICMP
410void 449void
411vpn::icmpv4_ev (io_watcher &w, short revents) 450vpn::icmpv4_ev (io_watcher &w, short revents)
412{ 451{
413 if (revents & (POLLIN | POLLERR)) 452 if (revents & (POLLIN | POLLERR))
414 { 453 {
423 462
424 if (len > 0) 463 if (len > 0)
425 { 464 {
426 pkt->len = len; 465 pkt->len = len;
427 466
428 icmphdr *hdr = (icmphdr *)&((*pkt)[IP_OVERHEAD]); 467 icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]);
429 468
430 if (hdr->type == ::conf.icmp_type 469 if (hdr->type == ::conf.icmp_type
431 && hdr->code == 255) 470 && hdr->code == 255)
432 { 471 {
433 // raw sockets deliver the ipv4, but don't expect it on sends 472 // raw sockets deliver the ipv4, but don't expect it on sends
457 _("FATAL: unknown revents %08x in socket, terminating\n"), 496 _("FATAL: unknown revents %08x in socket, terminating\n"),
458 revents); 497 revents);
459 exit (1); 498 exit (1);
460 } 499 }
461} 500}
501#endif
462 502
463void 503void
464vpn::udpv4_ev (io_watcher &w, short revents) 504vpn::udpv4_ev (io_watcher &w, short revents)
465{ 505{
466 if (revents & (POLLIN | POLLERR)) 506 if (revents & (POLLIN | POLLERR))
511 /* process data */ 551 /* process data */
512 tap_packet *pkt; 552 tap_packet *pkt;
513 553
514 pkt = tap->recv (); 554 pkt = tap->recv ();
515 555
556 if (!pkt)
557 return;
558
559 if (pkt->len > 14)
560 {
516 int dst = mac2id (pkt->dst); 561 int dst = mac2id (pkt->dst);
517 int src = mac2id (pkt->src); 562 int src = mac2id (pkt->src);
518 563
519 if (src != THISNODE->id) 564 if (src != THISNODE->id)
520 {
521 slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating."));
522 exit (1);
523 }
524
525 if (dst == THISNODE->id)
526 {
527 slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating."));
528 exit (1);
529 }
530
531 if (dst > conns.size ())
532 slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst);
533 else
534 {
535 if (dst)
536 { 565 {
537 // unicast 566 slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting."));
538 if (dst != THISNODE->id) 567 exit (1);
539 conns[dst - 1]->inject_data_packet (pkt);
540 } 568 }
569
570 if (dst == THISNODE->id)
571 {
572 slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting."));
573 exit (1);
574 }
575
576 if (dst > conns.size ())
577 slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst);
541 else 578 else
542 {
543 // broadcast, first check router, then self, then english
544 connection *router = find_router ();
545
546 if (router)
547 router->inject_data_packet (pkt, true); 579 inject_data_packet (pkt, dst);
548 else
549 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
550 if ((*c)->conf != THISNODE)
551 (*c)->inject_data_packet (pkt);
552 }
553 } 580 }
554 581
555 delete pkt; 582 delete pkt;
556 } 583 }
557 else if (revents & (POLLHUP | POLLERR)) 584 else if (revents & (POLLHUP | POLLERR))
623 } 650 }
624} 651}
625 652
626connection *vpn::find_router () 653connection *vpn::find_router ()
627{ 654{
628 u32 prio = 0; 655 u32 prio = 1;
629 connection *router = 0; 656 connection *router = 0;
630 657
631 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) 658 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
632 { 659 {
633 connection *c = *i; 660 connection *c = *i;
634 661
635 if (c->conf->routerprio > prio 662 if (c->conf->routerprio > prio
636 && c->connectmode == conf_node::C_ALWAYS 663 && 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) 664 && c->ictx && c->octx
665 && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode
639 { 666 {
640 prio = c->conf->routerprio; 667 prio = c->conf->routerprio;
641 router = c; 668 router = c;
642 } 669 }
643 } 670 }
652 if (c) 679 if (c)
653 c->send_connect_request (id); 680 c->send_connect_request (id);
654 else 681 else
655 // no router found, aggressively connect to all routers 682 // no router found, aggressively connect to all routers
656 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) 683 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
657 if ((*i)->conf->routerprio) 684 if ((*i)->conf->routerprio && (*i)->conf != THISNODE)
658 (*i)->establish_connection (); 685 (*i)->establish_connection ();
659} 686}
660 687
661void 688void
662connection::dump_status () 689connection::dump_status ()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines