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.19 by pcg, Thu Oct 16 02:41:21 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"
81 ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); 82 ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto);
82 83
83 if (ipv4_fd < 0) 84 if (ipv4_fd < 0)
84 return -1; 85 return -1;
85 86
86#ifdef IP_MTU_DISCOVER 87 fcntl (ipv4_fd, F_SETFL, O_NONBLOCK);
88
89#if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
87 // this I really consider a linux bug. I am neither connected 90 // this I really consider a linux bug. I am neither connected
88 // nor do I fragment myself. Linux still sets DF and doesn't 91 // nor do I fragment myself. Linux still sets DF and doesn't
89 // fragment for me sometimes. 92 // fragment for me sometimes.
90 { 93 {
91 int oval = IP_PMTUDISC_DONT; 94 int oval = IP_PMTUDISC_DONT;
111 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); 114 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
112 115
113 if (udpv4_fd < 0) 116 if (udpv4_fd < 0)
114 return -1; 117 return -1;
115 118
119 fcntl (udpv4_fd, F_SETFL, O_NONBLOCK);
120
116 // standard daemon practise... 121 // standard daemon practise...
117 { 122 {
118 int oval = 1; 123 int oval = 1;
119 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 124 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
120 } 125 }
121 126
122#ifdef IP_MTU_DISCOVER 127#if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
123 // this I really consider a linux bug. I am neither connected 128 // this I really consider a linux bug. I am neither connected
124 // nor do I fragment myself. Linux still sets DF and doesn't 129 // nor do I fragment myself. Linux still sets DF and doesn't
125 // fragment for me sometimes. 130 // fragment for me sometimes.
126 { 131 {
127 int oval = IP_PMTUDISC_DONT; 132 int oval = IP_PMTUDISC_DONT;
148 icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); 153 icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP);
149 154
150 if (icmpv4_fd < 0) 155 if (icmpv4_fd < 0)
151 return -1; 156 return -1;
152 157
158 fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK);
159
153#ifdef ICMP_FILTER 160#ifdef ICMP_FILTER
154 { 161 {
155 icmp_filter oval; 162 icmp_filter oval;
156 oval.data = 0xffffffff; 163 oval.data = 0xffffffff;
157 if (::conf.icmp_type < 32) 164 if (::conf.icmp_type < 32)
159 166
160 setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); 167 setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval);
161 } 168 }
162#endif 169#endif
163 170
164#ifdef IP_MTU_DISCOVER 171#if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
165 // this I really consider a linux bug. I am neither connected 172 // this I really consider a linux bug. I am neither connected
166 // nor do I fragment myself. Linux still sets DF and doesn't 173 // nor do I fragment myself. Linux still sets DF and doesn't
167 // fragment for me sometimes. 174 // fragment for me sometimes.
168 { 175 {
169 int oval = IP_PMTUDISC_DONT; 176 int oval = IP_PMTUDISC_DONT;
191 tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); 198 tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
192 199
193 if (tcpv4_fd < 0) 200 if (tcpv4_fd < 0)
194 return -1; 201 return -1;
195 202
203 fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK);
204
196 // standard daemon practise... 205 // standard daemon practise...
197 { 206 {
198 int oval = 1; 207 int oval = 1;
199 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 208 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
200 } 209 }
262} 271}
263 272
264bool 273bool
265vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 274vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
266{ 275{
276#if defined(SOL_IP) && defined(IP_TOS)
267 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 277 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
278#endif
268 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 279 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
269 280
270 return true; 281 return true;
271} 282}
272 283
291 sum += (sum >> 16); // carry 302 sum += (sum >> 16); // carry
292 303
293 return ~sum; 304 return ~sum;
294} 305}
295 306
307#if ENABLE_ICMP
296bool 308bool
297vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 309vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
298{ 310{
311#if defined(SOL_IP) && defined(IP_TOS)
299 setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 312 setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
313#endif
300 314
301 pkt->unshift_hdr (4); 315 pkt->unshift_hdr (4);
302 316
303 icmphdr *hdr = (icmphdr *)&((*pkt)[0]); 317 icmp_header *hdr = (icmp_header *)&((*pkt)[0]);
304 hdr->type = ::conf.icmp_type; 318 hdr->type = ::conf.icmp_type;
305 hdr->code = 255; 319 hdr->code = 255;
306 hdr->checksum = 0; 320 hdr->checksum = 0;
307 hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); 321 hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len);
308 322
309 sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 323 sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
310 324
311 return true; 325 return true;
312} 326}
327#endif
313 328
314bool 329bool
315vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 330vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
316{ 331{
332#if defined(SOL_IP) && defined(IP_TOS)
317 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 333 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
334#endif
318 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 335 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
319 336
320 return true; 337 return true;
338}
339
340void
341vpn::inject_data_packet (tap_packet *pkt, int dst)
342{
343 if (dst)
344 {
345 // unicast
346 if (dst != THISNODE->id)
347 conns[dst - 1]->inject_data_packet (pkt);
348 }
349 else
350 {
351 // broadcast, this is ugly, but due to the security policy
352 // we have to connect to all hosts...
353 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
354 if ((*c)->conf != THISNODE)
355 (*c)->inject_data_packet (pkt, true);
356 }
321} 357}
322 358
323void 359void
324vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) 360vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
325{ 361{
339 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); 375 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ());
340 else 376 else
341 { 377 {
342 connection *c = conns[src - 1]; 378 connection *c = conns[src - 1];
343 379
344 if (dst == 0 && !THISNODE->routerprio) 380 if (dst == 0)
345 slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), 381 slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"),
346 c->conf->nodename, (const char *)rsi); 382 c->conf->nodename, (const char *)rsi);
347 else if (dst != 0 && dst != THISNODE->id) 383 else if (dst != THISNODE->id)
348 { 384 {
349 if (THISNODE->routerprio) 385 if (THISNODE->routerprio)
350 // the tos setting gets lost here. who cares. 386 // the tos setting gets lost here. who cares.
351 conns[dst - 1]->inject_vpn_packet (pkt); 387 conns[dst - 1]->inject_vpn_packet (pkt);
352 else 388 else
405 revents); 441 revents);
406 exit (1); 442 exit (1);
407 } 443 }
408} 444}
409 445
446#if ENABLE_ICMP
410void 447void
411vpn::icmpv4_ev (io_watcher &w, short revents) 448vpn::icmpv4_ev (io_watcher &w, short revents)
412{ 449{
413 if (revents & (POLLIN | POLLERR)) 450 if (revents & (POLLIN | POLLERR))
414 { 451 {
423 460
424 if (len > 0) 461 if (len > 0)
425 { 462 {
426 pkt->len = len; 463 pkt->len = len;
427 464
428 icmphdr *hdr = (icmphdr *)&((*pkt)[IP_OVERHEAD]); 465 icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]);
429 466
430 if (hdr->type == ::conf.icmp_type 467 if (hdr->type == ::conf.icmp_type
431 && hdr->code == 255) 468 && hdr->code == 255)
432 { 469 {
433 // raw sockets deliver the ipv4, but don't expect it on sends 470 // raw sockets deliver the ipv4, but don't expect it on sends
457 _("FATAL: unknown revents %08x in socket, terminating\n"), 494 _("FATAL: unknown revents %08x in socket, terminating\n"),
458 revents); 495 revents);
459 exit (1); 496 exit (1);
460 } 497 }
461} 498}
499#endif
462 500
463void 501void
464vpn::udpv4_ev (io_watcher &w, short revents) 502vpn::udpv4_ev (io_watcher &w, short revents)
465{ 503{
466 if (revents & (POLLIN | POLLERR)) 504 if (revents & (POLLIN | POLLERR))
511 /* process data */ 549 /* process data */
512 tap_packet *pkt; 550 tap_packet *pkt;
513 551
514 pkt = tap->recv (); 552 pkt = tap->recv ();
515 553
554 if (!pkt)
555 return;
556
557 if (pkt->len > 14)
558 {
516 int dst = mac2id (pkt->dst); 559 int dst = mac2id (pkt->dst);
517 int src = mac2id (pkt->src); 560 int src = mac2id (pkt->src);
518 561
519 if (src != THISNODE->id) 562 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 { 563 {
537 // unicast 564 slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting."));
538 if (dst != THISNODE->id) 565 exit (1);
539 conns[dst - 1]->inject_data_packet (pkt);
540 } 566 }
567
568 if (dst == THISNODE->id)
569 {
570 slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting."));
571 exit (1);
572 }
573
574 if (dst > conns.size ())
575 slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst);
541 else 576 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); 577 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 } 578 }
554 579
555 delete pkt; 580 delete pkt;
556 } 581 }
557 else if (revents & (POLLHUP | POLLERR)) 582 else if (revents & (POLLHUP | POLLERR))
623 } 648 }
624} 649}
625 650
626connection *vpn::find_router () 651connection *vpn::find_router ()
627{ 652{
628 u32 prio = 0; 653 u32 prio = 1;
629 connection *router = 0; 654 connection *router = 0;
630 655
631 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) 656 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
632 { 657 {
633 connection *c = *i; 658 connection *c = *i;
634 659
635 if (c->conf->routerprio > prio 660 if (c->conf->routerprio > prio
636 && c->connectmode == conf_node::C_ALWAYS 661 && 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) 662 && c->ictx && c->octx
663 && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode
639 { 664 {
640 prio = c->conf->routerprio; 665 prio = c->conf->routerprio;
641 router = c; 666 router = c;
642 } 667 }
643 } 668 }
652 if (c) 677 if (c)
653 c->send_connect_request (id); 678 c->send_connect_request (id);
654 else 679 else
655 // no router found, aggressively connect to all routers 680 // no router found, aggressively connect to all routers
656 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) 681 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
657 if ((*i)->conf->routerprio) 682 if ((*i)->conf->routerprio && (*i)->conf != THISNODE)
658 (*i)->establish_connection (); 683 (*i)->establish_connection ();
659} 684}
660 685
661void 686void
662connection::dump_status () 687connection::dump_status ()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines