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.14 by pcg, Tue Oct 14 03:22:09 2003 UTC vs.
Revision 1.22 by pcg, Sat Jan 17 01:18:36 2004 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-2004 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 <fcntl.h> 34#include <fcntl.h>
35#include <sys/socket.h> 35#include <sys/socket.h>
36#include <netinet/in.h> 36
37#include <arpa/inet.h> 37#include "netcompat.h"
38#include <net/if.h>
39#ifdef HAVE_NETINET_IN_SYSTM_H
40# include <netinet/in_systm.h>
41#endif
42#ifdef HAVE_NETINET_IP_H
43# include <netinet/ip.h>
44#endif
45#ifdef HAVE_NETINET_TCP_H
46# include <netinet/tcp.h>
47#endif
48#if ENABLE_ICMP
49# include <netinet/ip_icmp.h>
50#endif
51 38
52#include "pidfile.h" 39#include "pidfile.h"
53 40
54#include "connection.h" 41#include "connection.h"
55#include "util.h" 42#include "util.h"
56#include "vpn.h" 43#include "vpn.h"
57 44
58#if !defined(SOL_IP) && defined(IPPROTO_IP) 45vpn network; // THE vpn (bad design...)
59# define SOL_IP IPPROTO_IP
60#endif
61 46
62///////////////////////////////////////////////////////////////////////////// 47/////////////////////////////////////////////////////////////////////////////
63 48
64const char *vpn::script_if_up () 49const char *vpn::script_if_up ()
65{ 50{
69 mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion 54 mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion
70 mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round 55 mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round
71 mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again 56 mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again
72 57
73 char *env; 58 char *env;
74 asprintf (&env, "CONFBASE=%s", confbase); 59 asprintf (&env, "CONFBASE=%s", confbase); putenv (env);
75 putenv (env);
76 asprintf (&env, "NODENAME=%s", THISNODE->nodename); 60 asprintf (&env, "NODENAME=%s", THISNODE->nodename); putenv (env);
77 putenv (env);
78 asprintf (&env, "NODEID=%d", THISNODE->id); 61 asprintf (&env, "NODEID=%d", THISNODE->id); putenv (env);
79 putenv (env);
80 asprintf (&env, "IFNAME=%s", tap->interface ()); 62 asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env);
81 putenv (env); 63 asprintf (&env, "IFTYPE=%s", IFTYPE); putenv (env);
64 asprintf (&env, "IFSUBTYPE=%s", IFSUBTYPE); putenv (env);
82 asprintf (&env, "MTU=%d", mtu); 65 asprintf (&env, "MTU=%d", mtu); putenv (env);
83 putenv (env);
84 asprintf (&env, "MAC=%02x:%02x:%02x:%02x:%02x:%02x", 66 asprintf (&env, "MAC=%02x:%02x:%02x:%02x:%02x:%02x",
85 0xfe, 0xfd, 0x80, 0x00, THISNODE->id >> 8, 67 0xfe, 0xfd, 0x80, 0x00, THISNODE->id >> 8,
86 THISNODE->id & 0xff); 68 THISNODE->id & 0xff);
87 putenv (env); 69 putenv (env);
88 70
119 { 101 {
120 slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); 102 slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno));
121 exit (1); 103 exit (1);
122 } 104 }
123 105
124 ipv4_ev_watcher.start (ipv4_fd, POLLIN); 106 ipv4_ev_watcher.start (ipv4_fd, EVENT_READ);
125 } 107 }
126 108
127 udpv4_fd = -1; 109 udpv4_fd = -1;
128 110
129 if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) 111 if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port)
157 { 139 {
158 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); 140 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno));
159 exit (1); 141 exit (1);
160 } 142 }
161 143
162 udpv4_ev_watcher.start (udpv4_fd, POLLIN); 144 udpv4_ev_watcher.start (udpv4_fd, EVENT_READ);
163 } 145 }
164 146
165 icmpv4_fd = -1; 147 icmpv4_fd = -1;
166 148
167#if ENABLE_ICMP 149#if ENABLE_ICMP
201 { 183 {
202 slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); 184 slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno));
203 exit (1); 185 exit (1);
204 } 186 }
205 187
206 icmpv4_ev_watcher.start (icmpv4_fd, POLLIN); 188 icmpv4_ev_watcher.start (icmpv4_fd, EVENT_READ);
207 } 189 }
208#endif 190#endif
209 191
210 tcpv4_fd = -1; 192 tcpv4_fd = -1;
211 193
237 { 219 {
238 slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno)); 220 slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno));
239 exit (1); 221 exit (1);
240 } 222 }
241 223
242 tcpv4_ev_watcher.start (tcpv4_fd, POLLIN); 224 tcpv4_ev_watcher.start (tcpv4_fd, EVENT_READ);
243 } 225 }
244#endif 226#endif
245 227
246 tap = new tap_device (); 228 tap = new tap_device ();
247 if (!tap) //D this, of course, never catches 229 if (!tap) //D this, of course, never catches
250 exit (1); 232 exit (1);
251 } 233 }
252 234
253 run_script (run_script_cb (this, &vpn::script_if_up), true); 235 run_script (run_script_cb (this, &vpn::script_if_up), true);
254 236
255 tap_ev_watcher.start (tap->fd, POLLIN); 237 tap_ev_watcher.start (tap->fd, EVENT_READ);
256 238
257 reconnect_all (); 239 reconnect_all ();
258 240
259 return 0; 241 return 0;
260} 242}
320 302
321 return ~sum; 303 return ~sum;
322} 304}
323 305
324#if ENABLE_ICMP 306#if ENABLE_ICMP
325struct icmp_header {
326 u8 type;
327 u8 code;
328 u16 checksum;
329 union {
330 struct {
331 u16 id;
332 u16 sequence;
333 } echo;
334 u32 gateway;
335 struct {
336 u16 unused;
337 u16 mtu;
338 } frag;
339 } un;
340};
341
342bool 307bool
343vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 308vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
344{ 309{
345#if defined(SOL_IP) && defined(IP_TOS) 310#if defined(SOL_IP) && defined(IP_TOS)
346 setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 311 setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
367 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 332 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
368#endif 333#endif
369 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 ());
370 335
371 return true; 336 return true;
337}
338
339void
340vpn::inject_data_packet (tap_packet *pkt, int dst)
341{
342 if (dst)
343 {
344 // unicast
345 if (dst != THISNODE->id)
346 conns[dst - 1]->inject_data_packet (pkt);
347 }
348 else
349 {
350 // broadcast, this is ugly, but due to the security policy
351 // we have to connect to all hosts...
352 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
353 if ((*c)->conf != THISNODE)
354 (*c)->inject_data_packet (pkt, true);
355 }
372} 356}
373 357
374void 358void
375vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) 359vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
376{ 360{
393 connection *c = conns[src - 1]; 377 connection *c = conns[src - 1];
394 378
395 if (dst == 0) 379 if (dst == 0)
396 slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), 380 slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"),
397 c->conf->nodename, (const char *)rsi); 381 c->conf->nodename, (const char *)rsi);
398 else if (dst != 0 && dst != THISNODE->id) 382 else if (dst != THISNODE->id)
399 { 383 {
400 if (THISNODE->routerprio) 384 if (THISNODE->routerprio)
401 // the tos setting gets lost here. who cares. 385 // the tos setting gets lost here. who cares.
402 conns[dst - 1]->inject_vpn_packet (pkt); 386 conns[dst - 1]->inject_vpn_packet (pkt);
403 else 387 else
412} 396}
413 397
414void 398void
415vpn::ipv4_ev (io_watcher &w, short revents) 399vpn::ipv4_ev (io_watcher &w, short revents)
416{ 400{
417 if (revents & (POLLIN | POLLERR)) 401 if (revents & EVENT_READ)
418 { 402 {
419 vpn_packet *pkt = new vpn_packet; 403 vpn_packet *pkt = new vpn_packet;
420 struct sockaddr_in sa; 404 struct sockaddr_in sa;
421 socklen_t sa_len = sizeof (sa); 405 socklen_t sa_len = sizeof (sa);
422 int len; 406 int len;
441 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); 425 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno));
442 } 426 }
443 427
444 delete pkt; 428 delete pkt;
445 } 429 }
446 else if (revents & POLLHUP)
447 {
448 // this cannot ;) happen on udp sockets
449 slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating."));
450 exit (1);
451 }
452 else 430 else
453 { 431 {
454 slog (L_ERR, 432 slog (L_ERR,
455 _("FATAL: unknown revents %08x in socket, terminating\n"), 433 _("FATAL: unknown revents %08x in socket, terminating\n"),
456 revents); 434 revents);
460 438
461#if ENABLE_ICMP 439#if ENABLE_ICMP
462void 440void
463vpn::icmpv4_ev (io_watcher &w, short revents) 441vpn::icmpv4_ev (io_watcher &w, short revents)
464{ 442{
465 if (revents & (POLLIN | POLLERR)) 443 if (revents & EVENT_READ)
466 { 444 {
467 vpn_packet *pkt = new vpn_packet; 445 vpn_packet *pkt = new vpn_packet;
468 struct sockaddr_in sa; 446 struct sockaddr_in sa;
469 socklen_t sa_len = sizeof (sa); 447 socklen_t sa_len = sizeof (sa);
470 int len; 448 int len;
495 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); 473 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno));
496 } 474 }
497 475
498 delete pkt; 476 delete pkt;
499 } 477 }
500 else if (revents & POLLHUP)
501 {
502 // this cannot ;) happen on udp sockets
503 slog (L_ERR, _("FATAL: POLLHUP on icmpv4 fd, terminating."));
504 exit (1);
505 }
506 else 478 else
507 { 479 {
508 slog (L_ERR, 480 slog (L_ERR,
509 _("FATAL: unknown revents %08x in socket, terminating\n"), 481 _("FATAL: unknown revents %08x in socket, terminating\n"),
510 revents); 482 revents);
514#endif 486#endif
515 487
516void 488void
517vpn::udpv4_ev (io_watcher &w, short revents) 489vpn::udpv4_ev (io_watcher &w, short revents)
518{ 490{
519 if (revents & (POLLIN | POLLERR)) 491 if (revents & EVENT_READ)
520 { 492 {
521 vpn_packet *pkt = new vpn_packet; 493 vpn_packet *pkt = new vpn_packet;
522 struct sockaddr_in sa; 494 struct sockaddr_in sa;
523 socklen_t sa_len = sizeof (sa); 495 socklen_t sa_len = sizeof (sa);
524 int len; 496 int len;
539 slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); 511 slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno));
540 } 512 }
541 513
542 delete pkt; 514 delete pkt;
543 } 515 }
544 else if (revents & POLLHUP)
545 {
546 // this cannot ;) happen on udp sockets
547 slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating."));
548 exit (1);
549 }
550 else 516 else
551 { 517 {
552 slog (L_ERR, 518 slog (L_ERR,
553 _("FATAL: unknown revents %08x in socket, terminating\n"), 519 _("FATAL: unknown revents %08x in socket, terminating\n"),
554 revents); 520 revents);
557} 523}
558 524
559void 525void
560vpn::tap_ev (io_watcher &w, short revents) 526vpn::tap_ev (io_watcher &w, short revents)
561{ 527{
562 if (revents & POLLIN) 528 if (revents & EVENT_READ)
563 { 529 {
564 /* process data */ 530 /* process data */
565 tap_packet *pkt; 531 tap_packet *pkt;
566 532
567 pkt = tap->recv (); 533 pkt = tap->recv ();
568 534
535 if (!pkt)
536 return;
537
538 if (pkt->len > 14)
539 {
569 int dst = mac2id (pkt->dst); 540 int dst = mac2id (pkt->dst);
570 int src = mac2id (pkt->src); 541 int src = mac2id (pkt->src);
571 542
572 if (src != THISNODE->id) 543 if (src != THISNODE->id)
573 {
574 slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting."));
575 exit (1);
576 }
577
578 if (dst == THISNODE->id)
579 {
580 slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting."));
581 exit (1);
582 }
583
584 if (dst > conns.size ())
585 slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst);
586 else
587 {
588 if (dst)
589 { 544 {
590 // unicast 545 slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting."));
591 if (dst != THISNODE->id) 546 exit (1);
592 conns[dst - 1]->inject_data_packet (pkt);
593 } 547 }
548
549 if (dst == THISNODE->id)
550 {
551 slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting."));
552 exit (1);
553 }
554
555 if (dst > conns.size ())
556 slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst);
594 else 557 else
595 {
596 // broadcast, this is ugly, but due to the security policy
597 // we have to connect to all hosts...
598 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
599 if ((*c)->conf != THISNODE)
600 (*c)->inject_data_packet (pkt); 558 inject_data_packet (pkt, dst);
601 }
602 } 559 }
603 560
604 delete pkt; 561 delete pkt;
605 }
606 else if (revents & (POLLHUP | POLLERR))
607 {
608 slog (L_ERR, _("FATAL: POLLHUP or POLLERR on network device fd, terminating."));
609 exit (1);
610 } 562 }
611 else 563 else
612 abort (); 564 abort ();
613} 565}
614 566
620 if (events & EVENT_SHUTDOWN) 572 if (events & EVENT_SHUTDOWN)
621 { 573 {
622 slog (L_INFO, _("preparing shutdown...")); 574 slog (L_INFO, _("preparing shutdown..."));
623 575
624 shutdown_all (); 576 shutdown_all ();
625
626 remove_pid (pidfilename); 577 remove_pid (pidfilename);
627
628 slog (L_INFO, _("terminating")); 578 slog (L_INFO, _("terminating"));
629
630 exit (0); 579 exit (0);
631 } 580 }
632 581
633 if (events & EVENT_RECONNECT) 582 if (events & EVENT_RECONNECT)
634 { 583 {
637 reconnect_all (); 586 reconnect_all ();
638 } 587 }
639 588
640 events = 0; 589 events = 0;
641 } 590 }
642
643 w.at = TSTAMP_CANCEL;
644} 591}
645 592
646void 593void
647vpn::shutdown_all () 594vpn::shutdown_all ()
648{ 595{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines