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.27 by pcg, Tue Mar 1 06:27:20 2005 UTC vs.
Revision 1.40 by pcg, Sat Dec 1 23:35:31 2007 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 Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de>
4 4
5 This file is part of GVPE.
6
5 This program is free software; you can redistribute it and/or modify 7 GVPE is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by 8 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or 9 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 10 (at your option) any later version.
9 11
10 This program is distributed in the hope that it will be useful, 12 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details. 15 GNU General Public License for more details.
14 16
15 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software 18 along with gvpe; if not, write to the Free Software
17 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*/ 20*/
19 21
20#include "config.h" 22#include "config.h"
21 23
22#include <list> 24#include <list>
44 46
45vpn network; // THE vpn (bad design...) 47vpn network; // THE vpn (bad design...)
46 48
47///////////////////////////////////////////////////////////////////////////// 49/////////////////////////////////////////////////////////////////////////////
48 50
49const char *vpn::script_if_up () 51void
52vpn::script_init_env ()
50{ 53{
51 // the tunnel device mtu should be the physical mtu - overhead 54 // the tunnel device mtu should be the physical mtu - overhead
52 // the tricky part is rounding to the cipher key blocksize 55 // the tricky part is rounding to the cipher key blocksize
53 int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD; 56 int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD;
54 mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion 57 mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion
55 mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round 58 mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round
56 mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again 59 mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again
57 60
58 char *env; 61 char *env;
59 asprintf (&env, "CONFBASE=%s", confbase); putenv (env); 62 asprintf (&env, "CONFBASE=%s", confbase); putenv (env);
60 asprintf (&env, "NODENAME=%s", THISNODE->nodename); putenv (env);
61 asprintf (&env, "NODEID=%d", THISNODE->id); putenv (env);
62 asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env); 63 asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env);
63 asprintf (&env, "IFTYPE=%s", IFTYPE); putenv (env); 64 asprintf (&env, "IFTYPE=%s", IFTYPE); putenv (env);
64 asprintf (&env, "IFSUBTYPE=%s", IFSUBTYPE); putenv (env); 65 asprintf (&env, "IFSUBTYPE=%s", IFSUBTYPE); putenv (env);
65 asprintf (&env, "MTU=%d", mtu); putenv (env); 66 asprintf (&env, "MTU=%d", mtu); putenv (env);
66 asprintf (&env, "MAC=%02x:%02x:%02x:%02x:%02x:%02x", 67 asprintf (&env, "NODES=%d", conns.size ()); putenv (env);
67 0xfe, 0xfd, 0x80, 0x00, THISNODE->id >> 8, 68 asprintf (&env, "NODEID=%d", THISNODE->id); putenv (env);
68 THISNODE->id & 0xff);
69 putenv (env);
70 69
70 conns [THISNODE->id - 1]->script_init_env ("");
71
72 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
73 {
74 char ext[16];
75 snprintf (ext, 16, "_%d", (*c)->conf->id);
76 (*c)->script_init_env (ext);
77 }
78}
79
80const char *vpn::script_if_init ()
81{
82 script_init_env ();
83
84 return tap->if_up ();
85}
86
87const char *vpn::script_if_up ()
88{
89 script_init_env ();
90
91 char *filename;
92 asprintf (&filename,
93 "%s/%s",
94 confbase,
71 return ::conf.script_if_up ? ::conf.script_if_up : "if-up"; 95 ::conf.script_if_up ? ::conf.script_if_up : "if-up");
96
97 return filename;
72} 98}
73 99
74int 100int
75vpn::setup () 101vpn::setup ()
76{ 102{
82 108
83 if (ipv4_fd < 0) 109 if (ipv4_fd < 0)
84 return -1; 110 return -1;
85 111
86 fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); 112 fcntl (ipv4_fd, F_SETFL, O_NONBLOCK);
113 fcntl (ipv4_fd, F_SETFD, FD_CLOEXEC);
87 114
88#if defined(SOL_IP) && defined(IP_MTU_DISCOVER) 115#if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
89 // this I really consider a linux bug. I am neither connected 116 // this I really consider a linux bug. I am neither connected
90 // nor do I fragment myself. Linux still sets DF and doesn't 117 // nor do I fragment myself. Linux still sets DF and doesn't
91 // fragment for me sometimes. 118 // fragment for me sometimes.
101 { 128 {
102 slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); 129 slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno));
103 exit (EXIT_FAILURE); 130 exit (EXIT_FAILURE);
104 } 131 }
105 132
106 ipv4_ev_watcher.start (ipv4_fd, EVENT_READ); 133 ipv4_ev_watcher.start (ipv4_fd, EV_READ);
107 } 134 }
108 135
109 udpv4_fd = -1; 136 udpv4_fd = -1;
110 137
111 if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) 138 if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port)
114 141
115 if (udpv4_fd < 0) 142 if (udpv4_fd < 0)
116 return -1; 143 return -1;
117 144
118 fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); 145 fcntl (udpv4_fd, F_SETFL, O_NONBLOCK);
146 fcntl (udpv4_fd, F_SETFD, FD_CLOEXEC);
119 147
120 // standard daemon practise... 148 // standard daemon practise...
121 { 149 {
122 int oval = 1; 150 int oval = 1;
123 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 151 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
139 { 167 {
140 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); 168 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno));
141 exit (EXIT_FAILURE); 169 exit (EXIT_FAILURE);
142 } 170 }
143 171
144 udpv4_ev_watcher.start (udpv4_fd, EVENT_READ); 172 udpv4_ev_watcher.start (udpv4_fd, EV_READ);
145 } 173 }
146 174
147 icmpv4_fd = -1; 175 icmpv4_fd = -1;
148 176
149#if ENABLE_ICMP 177#if ENABLE_ICMP
153 181
154 if (icmpv4_fd < 0) 182 if (icmpv4_fd < 0)
155 return -1; 183 return -1;
156 184
157 fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); 185 fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK);
186 fcntl (icmpv4_fd, F_SETFD, FD_CLOEXEC);
158 187
159#ifdef ICMP_FILTER 188#ifdef ICMP_FILTER
160 { 189 {
161 icmp_filter oval; 190 icmp_filter oval;
162 oval.data = 0xffffffff; 191 oval.data = 0xffffffff;
171 // this I really consider a linux bug. I am neither connected 200 // this I really consider a linux bug. I am neither connected
172 // nor do I fragment myself. Linux still sets DF and doesn't 201 // nor do I fragment myself. Linux still sets DF and doesn't
173 // fragment for me sometimes. 202 // fragment for me sometimes.
174 { 203 {
175 int oval = IP_PMTUDISC_DONT; 204 int oval = IP_PMTUDISC_DONT;
176 setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); 205 setsockopt (icmpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval);
177 } 206 }
178#endif 207#endif
179 208
180 sockinfo si (THISNODE, PROT_ICMPv4); 209 sockinfo si (THISNODE, PROT_ICMPv4);
181 210
183 { 212 {
184 slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); 213 slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno));
185 exit (EXIT_FAILURE); 214 exit (EXIT_FAILURE);
186 } 215 }
187 216
188 icmpv4_ev_watcher.start (icmpv4_fd, EVENT_READ); 217 icmpv4_ev_watcher.start (icmpv4_fd, EV_READ);
189 } 218 }
190#endif 219#endif
191 220
192 tcpv4_fd = -1; 221 tcpv4_fd = -1;
193 222
198 227
199 if (tcpv4_fd < 0) 228 if (tcpv4_fd < 0)
200 return -1; 229 return -1;
201 230
202 fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); 231 fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK);
232 fcntl (tcpv4_fd, F_SETFD, FD_CLOEXEC);
203 233
204 // standard daemon practise... 234 // standard daemon practise...
205 { 235 {
206 int oval = 1; 236 int oval = 1;
207 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 237 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
219 { 249 {
220 slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno)); 250 slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno));
221 exit (EXIT_FAILURE); 251 exit (EXIT_FAILURE);
222 } 252 }
223 253
224 tcpv4_ev_watcher.start (tcpv4_fd, EVENT_READ); 254 tcpv4_ev_watcher.start (tcpv4_fd, EV_READ);
225 } 255 }
226#endif 256#endif
227 257
228#if ENABLE_DNS 258#if ENABLE_DNS
229 if (THISNODE->protocols & PROT_DNSv4) 259 if (THISNODE->protocols & PROT_DNSv4)
230 { 260 {
261 dns_forwarder.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4);
262
231 dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); 263 dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
232 264
233 if (dnsv4_fd < 0) 265 if (dnsv4_fd < 0)
234 return -1; 266 return -1;
267
268 fcntl (dnsv4_fd, F_SETFL, O_NONBLOCK);
269 fcntl (dnsv4_fd, F_SETFD, FD_CLOEXEC);
270
271# if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
272 // this I really consider a linux bug. I am neither connected
273 // nor do I fragment myself. Linux still sets DF and doesn't
274 // fragment for me sometimes.
275 {
276 int oval = IP_PMTUDISC_DONT;
277 setsockopt (dnsv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval);
278 }
279# endif
235 280
236 // standard daemon practise... 281 // standard daemon practise...
237 { 282 {
238 int oval = 1; 283 int oval = 1;
239 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 284 setsockopt (dnsv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
240 } 285 }
241 286
242 sockinfo si (THISNODE, PROT_DNSv4); 287 sockinfo si (THISNODE->dns_hostname,
288 THISNODE->dns_hostname ? THISNODE->dns_port : 0,
289 PROT_DNSv4);
243 290
244 if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ())) 291 if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ()))
245 { 292 {
246 slog (L_ERR, _("can't bind dnsv4 on %s: %s"), (const char *)si, strerror (errno)); 293 slog (L_ERR, _("can't bind dnsv4 on %s: %s"), (const char *)si, strerror (errno));
247 exit (EXIT_FAILURE); 294 exit (EXIT_FAILURE);
248 } 295 }
249 296
250 dnsv4_ev_watcher.start (dnsv4_fd, EVENT_READ); 297 dnsv4_ev_watcher.start (dnsv4_fd, EV_READ);
251 } 298 }
252#endif 299#endif
300
301 /////////////////////////////////////////////////////////////////////////////
302
303 reconnect_all ();
304
305 /////////////////////////////////////////////////////////////////////////////
253 306
254 tap = new tap_device (); 307 tap = new tap_device ();
255 if (!tap) //D this, of course, never catches 308 if (!tap) //D this, of course, never catches
256 { 309 {
257 slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); 310 slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname);
258 exit (EXIT_FAILURE); 311 exit (EXIT_FAILURE);
259 } 312 }
260 313
314 fcntl (tap->fd, F_SETFD, FD_CLOEXEC);
315
316 if (tap->if_up () &&
317 !run_script (run_script_cb (this, &vpn::script_if_init), true))
318 {
319 slog (L_ERR, _("interface initialization command '%s' failed, exiting."),
320 tap->if_up ());
321 exit (EXIT_FAILURE);
322 }
323
261 run_script (run_script_cb (this, &vpn::script_if_up), true); 324 if (!run_script (run_script_cb (this, &vpn::script_if_up), true))
325 {
326 slog (L_ERR, _("if-up command execution failed, exiting."));
327 exit (EXIT_FAILURE);
328 }
262 329
263 tap_ev_watcher.start (tap->fd, EVENT_READ); 330 tap_ev_watcher.start (tap->fd, EV_READ);
264
265 reconnect_all ();
266 331
267 return 0; 332 return 0;
268} 333}
269 334
270bool 335bool
391 else 456 else
392 c->recv_vpn_packet (pkt, rsi); 457 c->recv_vpn_packet (pkt, rsi);
393 } 458 }
394} 459}
395 460
461bool
462vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos)
463{
464 switch (si.prot)
465 {
466 case PROT_IPv4:
467 return send_ipv4_packet (pkt, si, tos);
468 case PROT_UDPv4:
469 return send_udpv4_packet (pkt, si, tos);
470#if ENABLE_TCP
471 case PROT_TCPv4:
472 return send_tcpv4_packet (pkt, si, tos);
473#endif
474#if ENABLE_ICMP
475 case PROT_ICMPv4:
476 return send_icmpv4_packet (pkt, si, tos);
477#endif
478#if ENABLE_DNS
479 case PROT_DNSv4:
480 return send_dnsv4_packet (pkt, si, tos);
481#endif
482
483 default:
484 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si);
485 }
486
487 return false;
488}
489
396void 490void
397vpn::ipv4_ev (io_watcher &w, short revents) 491vpn::ipv4_ev (ev::io &w, int revents)
398{ 492{
399 if (revents & EVENT_READ) 493 if (revents & EV_READ)
400 { 494 {
401 vpn_packet *pkt = new vpn_packet; 495 vpn_packet *pkt = new vpn_packet;
402 struct sockaddr_in sa; 496 struct sockaddr_in sa;
403 socklen_t sa_len = sizeof (sa); 497 socklen_t sa_len = sizeof (sa);
404 int len; 498 int len;
409 503
410 if (len > 0) 504 if (len > 0)
411 { 505 {
412 pkt->len = len; 506 pkt->len = len;
413 507
414 // raw sockets deliver the ipv4, but don't expect it on sends 508 // raw sockets deliver the ipv4 header, but don't expect it on sends
415 // this is slow, but...
416 pkt->skip_hdr (IP_OVERHEAD); 509 pkt->skip_hdr (IP_OVERHEAD);
417 510
418 recv_vpn_packet (pkt, si); 511 recv_vpn_packet (pkt, si);
419 } 512 }
420 else 513 else
426 delete pkt; 519 delete pkt;
427 } 520 }
428 else 521 else
429 { 522 {
430 slog (L_ERR, 523 slog (L_ERR,
431 _("FATAL: unknown revents %08x in socket, terminating\n"), 524 _("FATAL: unknown revents %08x in socket, terminating\n"),
432 revents); 525 revents);
433 exit (EXIT_FAILURE); 526 exit (EXIT_FAILURE);
434 } 527 }
435} 528}
436 529
437#if ENABLE_ICMP 530#if ENABLE_ICMP
438void 531void
439vpn::icmpv4_ev (io_watcher &w, short revents) 532vpn::icmpv4_ev (ev::io &w, int revents)
440{ 533{
441 if (revents & EVENT_READ) 534 if (revents & EV_READ)
442 { 535 {
443 vpn_packet *pkt = new vpn_packet; 536 vpn_packet *pkt = new vpn_packet;
444 struct sockaddr_in sa; 537 struct sockaddr_in sa;
445 socklen_t sa_len = sizeof (sa); 538 socklen_t sa_len = sizeof (sa);
446 int len; 539 int len;
482 } 575 }
483} 576}
484#endif 577#endif
485 578
486void 579void
487vpn::udpv4_ev (io_watcher &w, short revents) 580vpn::udpv4_ev (ev::io &w, int revents)
488{ 581{
489 if (revents & EVENT_READ) 582 if (revents & EV_READ)
490 { 583 {
491 vpn_packet *pkt = new vpn_packet; 584 vpn_packet *pkt = new vpn_packet;
492 struct sockaddr_in sa; 585 struct sockaddr_in sa;
493 socklen_t sa_len = sizeof (sa); 586 socklen_t sa_len = sizeof (sa);
494 int len; 587 int len;
519 exit (EXIT_FAILURE); 612 exit (EXIT_FAILURE);
520 } 613 }
521} 614}
522 615
523void 616void
524vpn::tap_ev (io_watcher &w, short revents) 617vpn::tap_ev (ev::io &w, int revents)
525{ 618{
526 if (revents & EVENT_READ) 619 if (revents & EV_READ)
527 { 620 {
528 /* process data */ 621 /* process data */
529 tap_packet *pkt; 622 tap_packet *pkt;
530 623
531 pkt = tap->recv (); 624 pkt = tap->recv ();
538 int dst = mac2id (pkt->dst); 631 int dst = mac2id (pkt->dst);
539 int src = mac2id (pkt->src); 632 int src = mac2id (pkt->src);
540 633
541 if (src != THISNODE->id) 634 if (src != THISNODE->id)
542 { 635 {
543 slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting.")); 636 slog (L_ERR, _("FATAL: tap packet not originating on current node received (if-up script not working properly?), exiting."));
544 exit (EXIT_FAILURE); 637 exit (EXIT_FAILURE);
545 } 638 }
546 639
547 if (dst == THISNODE->id) 640 if (dst == THISNODE->id)
548 { 641 {
561 else 654 else
562 abort (); 655 abort ();
563} 656}
564 657
565void 658void
566vpn::event_cb (time_watcher &w) 659vpn::event_cb (ev::timer &w, int)
567{ 660{
568 if (events) 661 if (events)
569 { 662 {
570 if (events & EVENT_SHUTDOWN) 663 if (events & EVENT_SHUTDOWN)
571 { 664 {
662} 755}
663 756
664void 757void
665vpn::dump_status () 758vpn::dump_status ()
666{ 759{
667 slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)NOW); 760 slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)ev_now ());
668 761
669 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) 762 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
670 (*c)->dump_status (); 763 (*c)->dump_status ();
671 764
672 slog (L_NOTICE, _("END status dump")); 765 slog (L_NOTICE, _("END status dump"));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines