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.60 by root, Fri Sep 16 18:01:27 2011 UTC vs.
Revision 1.69 by root, Sat Sep 5 17:40:22 2015 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-2008,2010,2011 Marc Lehmann <gvpe@schmorp.de> 3 Copyright (C) 2003-2008,2010,2011,2013 Marc Lehmann <gvpe@schmorp.de>
4 4
5 This file is part of GVPE. 5 This file is part of GVPE.
6 6
7 GVPE is free software; you can redistribute it and/or modify it 7 GVPE is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the 8 under the terms of the GNU General Public License as published by the
38#include <cstdlib> 38#include <cstdlib>
39 39
40#include <sys/types.h> 40#include <sys/types.h>
41#include <sys/socket.h> 41#include <sys/socket.h>
42#include <sys/wait.h> 42#include <sys/wait.h>
43#include <sys/stat.h>
43#include <errno.h> 44#include <errno.h>
44#include <time.h> 45#include <time.h>
45#include <unistd.h> 46#include <unistd.h>
46#include <fcntl.h> 47#include <fcntl.h>
47#include <sys/socket.h> 48#include <sys/socket.h>
52 53
53#include "connection.h" 54#include "connection.h"
54#include "util.h" 55#include "util.h"
55#include "vpn.h" 56#include "vpn.h"
56 57
58using namespace std;
59
57vpn network; // THE vpn (bad design...) 60vpn network; // THE vpn (bad design...)
61
62/////////////////////////////////////////////////////////////////////////////
63
64// hopefully temporary workaround for rare buffer full conditions
65// if it happens, usually instantly retrying or retrying ~5ms later
66// is good enough with current network technologies/kernels
67
68static ssize_t
69xsendto (int fd, const void *buf, size_t len, int flags,
70 const struct sockaddr *sa, socklen_t salen)
71{
72 ssize_t res;
73
74 for (int retry = 0; retry <= 13; ++retry) // ~100ms
75 {
76 res = sendto (fd, buf, len, flags, sa, salen);
77
78 if (res >= 0 || errno != ENOBUFS)
79 break;
80
81 struct timespec ts = { 0, 1000 * retry };
82 nanosleep (&ts, 0);
83 }
84
85 return res;
86}
58 87
59///////////////////////////////////////////////////////////////////////////// 88/////////////////////////////////////////////////////////////////////////////
60 89
61static void inline 90static void inline
62set_tos (int fd, int &tos_prev, int tos) 91set_tos (int fd, int &tos_prev, int tos)
75{ 104{
76 // the tunnel device mtu should be the physical mtu - overhead 105 // the tunnel device mtu should be the physical mtu - overhead
77 // the tricky part is rounding to the cipher key blocksize 106 // the tricky part is rounding to the cipher key blocksize
78 int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD; 107 int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD;
79 mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion 108 mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion
80 mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round 109 mtu -= mtu % BLOCK_SIZE (CIPHER); // round
81 mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again 110 mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again
82 111
83 char *env; 112 char *env;
84 asprintf (&env, "CONFBASE=%s", confbase); putenv (env); 113 asprintf (&env, "CONFBASE=%s", confbase); putenv (env);
85 asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env); 114 asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env);
110inline const char * 139inline const char *
111vpn::script_if_up () 140vpn::script_if_up ()
112{ 141{
113 script_init_env (); 142 script_init_env ();
114 143
115 char *filename; 144 return conf.config_filename (::conf.script_if_up, "if-up");
116 asprintf (&filename,
117 "%s/%s",
118 confbase,
119 ::conf.script_if_up ? ::conf.script_if_up : "if-up");
120
121 return filename;
122} 145}
123 146
124int 147int
125vpn::setup_socket (u8 prot, int family, int type, int proto) 148vpn::setup_socket (u8 prot, int family, int type, int proto)
126{ 149{
392 415
393 return 0; 416 return 0;
394} 417}
395 418
396bool 419bool
420vpn::drop_privileges ()
421{
422 if (::conf.change_root)
423 {
424 if (!strcmp (::conf.change_root, "/"))
425 {
426 char dir [L_tmpnam];
427 if (!tmpnam (dir))
428 {
429 slog (L_CRIT, _("unable to create anonymous root path."));
430 return false;
431 }
432
433 if (mkdir (dir, 0700))
434 {
435 slog (L_CRIT, _("unable to create anonymous root directory."));
436 return false;
437 }
438
439 if (chdir (dir))
440 {
441 slog (L_CRIT, _("unable to change to anonymous root directory."));
442 return false;
443 }
444
445 if (rmdir (dir))
446 slog (L_ERR, _("unable to remove anonymous root directory, continuing."));
447 }
448 else
449 {
450 if (chdir (::conf.change_root))
451 {
452 slog (L_CRIT, _("%s: unable to change to specified root directory."), ::conf.change_root);
453 return false;
454 }
455 }
456
457 if (chroot ("."))
458 {
459 slog (L_CRIT, _("unable to set new root directory."));
460 return false;
461 }
462
463 if (chdir ("/"))
464 {
465 slog (L_CRIT, _("unable to set cwd to new root directory."));
466 return false;
467 }
468 }
469
470 if (::conf.change_gid)
471 if (setgid (::conf.change_gid))
472 {
473 slog (L_CRIT, _("unable to change group id to %d."), ::conf.change_gid);
474 return false;
475 }
476
477 if (::conf.change_uid)
478 if (setuid (::conf.change_uid))
479 {
480 slog (L_CRIT, _("unable to change user id to %d."), ::conf.change_uid);
481 return false;
482 }
483
484 return true;
485}
486
487bool
397vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 488vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
398{ 489{
399 set_tos (ipv4_fd, ipv4_tos, tos); 490 set_tos (ipv4_fd, ipv4_tos, tos);
400 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 491 xsendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
401 492
402 return true; 493 return true;
403} 494}
404 495
405static u16 496static u16
436 hdr->code = 255; 527 hdr->code = 255;
437 hdr->checksum = 0; 528 hdr->checksum = 0;
438 hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); 529 hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len);
439 530
440 set_tos (icmpv4_fd, icmpv4_tos, tos); 531 set_tos (icmpv4_fd, icmpv4_tos, tos);
441 sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 532 xsendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
442 533
443 return true; 534 return true;
444} 535}
445#endif 536#endif
446 537
447bool 538bool
448vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 539vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
449{ 540{
450 set_tos (udpv4_fd, udpv4_tos, tos); 541 set_tos (udpv4_fd, udpv4_tos, tos);
451 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 542 xsendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
452 543
453 return true; 544 return true;
454} 545}
455 546
456void 547void
482 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst (), pkt->len); 573 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst (), pkt->len);
483 574
484 if (src == 0 || src > conns.size () 575 if (src == 0 || src > conns.size ()
485 || dst > conns.size () 576 || dst > conns.size ()
486 || pkt->typ () >= vpn_packet::PT_MAX) 577 || pkt->typ () >= vpn_packet::PT_MAX)
487 slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)."),
488 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ());
489 else if (dst > conns.size ())
490 slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)."), 578 slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)."),
491 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); 579 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ());
492 else 580 else
493 { 581 {
494 connection *c = conns[src - 1]; 582 connection *c = conns[src - 1];
516vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) 604vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos)
517{ 605{
518 switch (si.prot) 606 switch (si.prot)
519 { 607 {
520 case PROT_IPv4: 608 case PROT_IPv4:
521 return send_ipv4_packet (pkt, si, tos); 609 return send_ipv4_packet (pkt, si, tos);
522 610
523 case PROT_UDPv4: 611 case PROT_UDPv4:
524 return send_udpv4_packet (pkt, si, tos); 612 return send_udpv4_packet (pkt, si, tos);
525 613
526#if ENABLE_TCP 614#if ENABLE_TCP
527 case PROT_TCPv4: 615 case PROT_TCPv4:
528 return send_tcpv4_packet (pkt, si, tos); 616 return send_tcpv4_packet (pkt, si, tos);
529#endif 617#endif
530#if ENABLE_ICMP 618#if ENABLE_ICMP
531 case PROT_ICMPv4: 619 case PROT_ICMPv4:
532 return send_icmpv4_packet (pkt, si, tos); 620 return send_icmpv4_packet (pkt, si, tos);
533#endif 621#endif
534#if ENABLE_DNS 622#if ENABLE_DNS
535 case PROT_DNSv4: 623 case PROT_DNSv4:
536 return send_dnsv4_packet (pkt, si, tos); 624 return send_dnsv4_packet (pkt, si, tos);
537#endif 625#endif
538 default: 626 default:
539 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol."), (const char *)si); 627 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol."), (const char *)si);
540 } 628 }
541 629
827{ 915{
828 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) 916 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
829 { 917 {
830 connection *o = *i; 918 connection *o = *i;
831 919
832 if (!o->is_direct
833 && o->si.valid () 920 if (o->si.valid ()
834 && c->si != o->si 921 && c->si != o->si
835 && c == find_router_for (o)) 922 && c == find_router_for (o)
923 && !can_direct (THISNODE, o->conf))
836 { 924 {
837 slog (L_DEBUG, _("%s: can now route packets via %s, re-keying connection."), 925 slog (L_DEBUG, _("%s: can now route packets via %s, re-keying connection."),
838 o->conf->nodename, c->conf->nodename); 926 o->conf->nodename, c->conf->nodename);
839 o->rekey (); 927 o->rekey ();
840 } 928 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines