ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vpn.C
Revision: 1.27
Committed: Tue Mar 1 06:27:20 2005 UTC (19 years, 3 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.26: +1 -37 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 vpn.C -- handle the protocol, encryption, handshaking etc.
3 Copyright (C) 2003-2004 Marc Lehmann <pcg@goof.com>
4
5 This program is free software; you can redistribute it and/or modify
6 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
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include "config.h"
21
22 #include <list>
23
24 #include <cstdio>
25 #include <cstring>
26 #include <cstdlib>
27
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/wait.h>
31 #include <errno.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <sys/socket.h>
36
37 #include "netcompat.h"
38
39 #include "pidfile.h"
40
41 #include "connection.h"
42 #include "util.h"
43 #include "vpn.h"
44
45 vpn network; // THE vpn (bad design...)
46
47 /////////////////////////////////////////////////////////////////////////////
48
49 const char *vpn::script_if_up ()
50 {
51 // the tunnel device mtu should be the physical mtu - overhead
52 // the tricky part is rounding to the cipher key blocksize
53 int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD;
54 mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion
55 mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round
56 mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again
57
58 char *env;
59 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, "IFTYPE=%s", IFTYPE); putenv (env);
64 asprintf (&env, "IFSUBTYPE=%s", IFSUBTYPE); putenv (env);
65 asprintf (&env, "MTU=%d", mtu); putenv (env);
66 asprintf (&env, "MAC=%02x:%02x:%02x:%02x:%02x:%02x",
67 0xfe, 0xfd, 0x80, 0x00, THISNODE->id >> 8,
68 THISNODE->id & 0xff);
69 putenv (env);
70
71 return ::conf.script_if_up ? ::conf.script_if_up : "if-up";
72 }
73
74 int
75 vpn::setup ()
76 {
77 ipv4_fd = -1;
78
79 if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto)
80 {
81 ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto);
82
83 if (ipv4_fd < 0)
84 return -1;
85
86 fcntl (ipv4_fd, F_SETFL, O_NONBLOCK);
87
88 #if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
89 // this I really consider a linux bug. I am neither connected
90 // nor do I fragment myself. Linux still sets DF and doesn't
91 // fragment for me sometimes.
92 {
93 int oval = IP_PMTUDISC_DONT;
94 setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval);
95 }
96 #endif
97
98 sockinfo si (THISNODE, PROT_IPv4);
99
100 if (bind (ipv4_fd, si.sav4 (), si.salenv4 ()))
101 {
102 slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno));
103 exit (EXIT_FAILURE);
104 }
105
106 ipv4_ev_watcher.start (ipv4_fd, EVENT_READ);
107 }
108
109 udpv4_fd = -1;
110
111 if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port)
112 {
113 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
114
115 if (udpv4_fd < 0)
116 return -1;
117
118 fcntl (udpv4_fd, F_SETFL, O_NONBLOCK);
119
120 // standard daemon practise...
121 {
122 int oval = 1;
123 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
124 }
125
126 #if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
127 // this I really consider a linux bug. I am neither connected
128 // nor do I fragment myself. Linux still sets DF and doesn't
129 // fragment for me sometimes.
130 {
131 int oval = IP_PMTUDISC_DONT;
132 setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval);
133 }
134 #endif
135
136 sockinfo si (THISNODE, PROT_UDPv4);
137
138 if (bind (udpv4_fd, si.sav4 (), si.salenv4 ()))
139 {
140 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno));
141 exit (EXIT_FAILURE);
142 }
143
144 udpv4_ev_watcher.start (udpv4_fd, EVENT_READ);
145 }
146
147 icmpv4_fd = -1;
148
149 #if ENABLE_ICMP
150 if (THISNODE->protocols & PROT_ICMPv4)
151 {
152 icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP);
153
154 if (icmpv4_fd < 0)
155 return -1;
156
157 fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK);
158
159 #ifdef ICMP_FILTER
160 {
161 icmp_filter oval;
162 oval.data = 0xffffffff;
163 if (::conf.icmp_type < 32)
164 oval.data &= ~(1 << ::conf.icmp_type);
165
166 setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval);
167 }
168 #endif
169
170 #if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
171 // this I really consider a linux bug. I am neither connected
172 // nor do I fragment myself. Linux still sets DF and doesn't
173 // fragment for me sometimes.
174 {
175 int oval = IP_PMTUDISC_DONT;
176 setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval);
177 }
178 #endif
179
180 sockinfo si (THISNODE, PROT_ICMPv4);
181
182 if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ()))
183 {
184 slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno));
185 exit (EXIT_FAILURE);
186 }
187
188 icmpv4_ev_watcher.start (icmpv4_fd, EVENT_READ);
189 }
190 #endif
191
192 tcpv4_fd = -1;
193
194 #if ENABLE_TCP
195 if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port)
196 {
197 tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
198
199 if (tcpv4_fd < 0)
200 return -1;
201
202 fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK);
203
204 // standard daemon practise...
205 {
206 int oval = 1;
207 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
208 }
209
210 sockinfo si (THISNODE, PROT_TCPv4);
211
212 if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ()))
213 {
214 slog (L_ERR, _("can't bind tcpv4 on %s: %s"), (const char *)si, strerror (errno));
215 exit (EXIT_FAILURE);
216 }
217
218 if (listen (tcpv4_fd, 5))
219 {
220 slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno));
221 exit (EXIT_FAILURE);
222 }
223
224 tcpv4_ev_watcher.start (tcpv4_fd, EVENT_READ);
225 }
226 #endif
227
228 #if ENABLE_DNS
229 if (THISNODE->protocols & PROT_DNSv4)
230 {
231 dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
232
233 if (dnsv4_fd < 0)
234 return -1;
235
236 // standard daemon practise...
237 {
238 int oval = 1;
239 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
240 }
241
242 sockinfo si (THISNODE, PROT_DNSv4);
243
244 if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ()))
245 {
246 slog (L_ERR, _("can't bind dnsv4 on %s: %s"), (const char *)si, strerror (errno));
247 exit (EXIT_FAILURE);
248 }
249
250 dnsv4_ev_watcher.start (dnsv4_fd, EVENT_READ);
251 }
252 #endif
253
254 tap = new tap_device ();
255 if (!tap) //D this, of course, never catches
256 {
257 slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname);
258 exit (EXIT_FAILURE);
259 }
260
261 run_script (run_script_cb (this, &vpn::script_if_up), true);
262
263 tap_ev_watcher.start (tap->fd, EVENT_READ);
264
265 reconnect_all ();
266
267 return 0;
268 }
269
270 bool
271 vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
272 {
273 #if defined(SOL_IP) && defined(IP_TOS)
274 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
275 #endif
276 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
277
278 return true;
279 }
280
281 static u16
282 ipv4_checksum (u16 *data, unsigned int len)
283 {
284 // use 32 bit accumulator and fold back carry bits at the end
285 u32 sum = 0;
286
287 while (len > 1)
288 {
289 sum += *data++;
290 len -= 2;
291 }
292
293 // odd byte left?
294 if (len)
295 sum += *(u8 *)data;
296
297 // add back carry bits
298 sum = (sum >> 16) + (sum & 0xffff); // lo += hi
299 sum += (sum >> 16); // carry
300
301 return ~sum;
302 }
303
304 #if ENABLE_ICMP
305 bool
306 vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
307 {
308 #if defined(SOL_IP) && defined(IP_TOS)
309 setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
310 #endif
311
312 pkt->unshift_hdr (4);
313
314 icmp_header *hdr = (icmp_header *)&((*pkt)[0]);
315 hdr->type = ::conf.icmp_type;
316 hdr->code = 255;
317 hdr->checksum = 0;
318 hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len);
319
320 sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
321
322 return true;
323 }
324 #endif
325
326 bool
327 vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
328 {
329 #if defined(SOL_IP) && defined(IP_TOS)
330 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
331 #endif
332 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
333
334 return true;
335 }
336
337 void
338 vpn::inject_data_packet (tap_packet *pkt, int dst)
339 {
340 if (dst)
341 {
342 // unicast
343 if (dst != THISNODE->id)
344 conns[dst - 1]->inject_data_packet (pkt);
345 }
346 else
347 {
348 // broadcast, this is ugly, but due to the security policy
349 // we have to connect to all hosts...
350 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
351 if ((*c)->conf != THISNODE)
352 (*c)->inject_data_packet (pkt, true);
353 }
354 }
355
356 void
357 vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
358 {
359 unsigned int src = pkt->src ();
360 unsigned int dst = pkt->dst ();
361
362 slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d"),
363 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst (), pkt->len);
364
365 if (src == 0 || src > conns.size ()
366 || dst > conns.size ()
367 || pkt->typ () >= vpn_packet::PT_MAX)
368 slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"),
369 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ());
370 else if (dst > conns.size ())
371 slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"),
372 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ());
373 else
374 {
375 connection *c = conns[src - 1];
376
377 if (dst == 0)
378 slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"),
379 c->conf->nodename, (const char *)rsi);
380 else if (dst != THISNODE->id)
381 {
382 if (THISNODE->routerprio)
383 // the tos setting gets lost here. who cares.
384 conns[dst - 1]->inject_vpn_packet (pkt);
385 else
386 slog (L_WARN,
387 _("%s(%s): forwarding request (=> %s), but we are no router"),
388 c->conf->nodename, (const char *)rsi,
389 conns[dst - 1]->conf->nodename);
390 }
391 else
392 c->recv_vpn_packet (pkt, rsi);
393 }
394 }
395
396 void
397 vpn::ipv4_ev (io_watcher &w, short revents)
398 {
399 if (revents & EVENT_READ)
400 {
401 vpn_packet *pkt = new vpn_packet;
402 struct sockaddr_in sa;
403 socklen_t sa_len = sizeof (sa);
404 int len;
405
406 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
407
408 sockinfo si(sa, PROT_IPv4);
409
410 if (len > 0)
411 {
412 pkt->len = len;
413
414 // raw sockets deliver the ipv4, but don't expect it on sends
415 // this is slow, but...
416 pkt->skip_hdr (IP_OVERHEAD);
417
418 recv_vpn_packet (pkt, si);
419 }
420 else
421 {
422 // probably ECONNRESET or somesuch
423 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno));
424 }
425
426 delete pkt;
427 }
428 else
429 {
430 slog (L_ERR,
431 _("FATAL: unknown revents %08x in socket, terminating\n"),
432 revents);
433 exit (EXIT_FAILURE);
434 }
435 }
436
437 #if ENABLE_ICMP
438 void
439 vpn::icmpv4_ev (io_watcher &w, short revents)
440 {
441 if (revents & EVENT_READ)
442 {
443 vpn_packet *pkt = new vpn_packet;
444 struct sockaddr_in sa;
445 socklen_t sa_len = sizeof (sa);
446 int len;
447
448 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
449
450 sockinfo si(sa, PROT_ICMPv4);
451
452 if (len > 0)
453 {
454 pkt->len = len;
455
456 icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]);
457
458 if (hdr->type == ::conf.icmp_type
459 && hdr->code == 255)
460 {
461 // raw sockets deliver the ipv4, but don't expect it on sends
462 // this is slow, but...
463 pkt->skip_hdr (ICMP_OVERHEAD);
464
465 recv_vpn_packet (pkt, si);
466 }
467 }
468 else
469 {
470 // probably ECONNRESET or somesuch
471 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno));
472 }
473
474 delete pkt;
475 }
476 else
477 {
478 slog (L_ERR,
479 _("FATAL: unknown revents %08x in socket, terminating\n"),
480 revents);
481 exit (EXIT_FAILURE);
482 }
483 }
484 #endif
485
486 void
487 vpn::udpv4_ev (io_watcher &w, short revents)
488 {
489 if (revents & EVENT_READ)
490 {
491 vpn_packet *pkt = new vpn_packet;
492 struct sockaddr_in sa;
493 socklen_t sa_len = sizeof (sa);
494 int len;
495
496 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
497
498 sockinfo si(sa, PROT_UDPv4);
499
500 if (len > 0)
501 {
502 pkt->len = len;
503
504 recv_vpn_packet (pkt, si);
505 }
506 else
507 {
508 // probably ECONNRESET or somesuch
509 slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno));
510 }
511
512 delete pkt;
513 }
514 else
515 {
516 slog (L_ERR,
517 _("FATAL: unknown revents %08x in socket, terminating\n"),
518 revents);
519 exit (EXIT_FAILURE);
520 }
521 }
522
523 void
524 vpn::tap_ev (io_watcher &w, short revents)
525 {
526 if (revents & EVENT_READ)
527 {
528 /* process data */
529 tap_packet *pkt;
530
531 pkt = tap->recv ();
532
533 if (!pkt)
534 return;
535
536 if (pkt->len > 14)
537 {
538 int dst = mac2id (pkt->dst);
539 int src = mac2id (pkt->src);
540
541 if (src != THISNODE->id)
542 {
543 slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting."));
544 exit (EXIT_FAILURE);
545 }
546
547 if (dst == THISNODE->id)
548 {
549 slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting."));
550 exit (EXIT_FAILURE);
551 }
552
553 if (dst > conns.size ())
554 slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst);
555 else
556 inject_data_packet (pkt, dst);
557 }
558
559 delete pkt;
560 }
561 else
562 abort ();
563 }
564
565 void
566 vpn::event_cb (time_watcher &w)
567 {
568 if (events)
569 {
570 if (events & EVENT_SHUTDOWN)
571 {
572 slog (L_INFO, _("preparing shutdown..."));
573
574 shutdown_all ();
575 remove_pid (conf.pidfilename);
576 slog (L_INFO, _("terminating"));
577 exit (EXIT_SUCCESS);
578 }
579
580 if (events & EVENT_RECONNECT)
581 {
582 slog (L_INFO, _("forced reconnect"));
583
584 reconnect_all ();
585 }
586
587 events = 0;
588 }
589 }
590
591 void
592 vpn::shutdown_all ()
593 {
594 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
595 (*c)->shutdown ();
596 }
597
598 void
599 vpn::reconnect_all ()
600 {
601 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
602 delete *c;
603
604 conns.clear ();
605
606 connection_init ();
607
608 for (configuration::node_vector::iterator i = conf.nodes.begin ();
609 i != conf.nodes.end (); ++i)
610 {
611 connection *conn = new connection (this, *i);
612 conns.push_back (conn);
613 conn->establish_connection ();
614 }
615 }
616
617 connection *vpn::find_router ()
618 {
619 u32 prio = 1;
620 connection *router = 0;
621
622 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
623 {
624 connection *c = *i;
625
626 if (c->conf->routerprio > prio
627 && c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use
628 && c->ictx && c->octx
629 && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode
630 {
631 prio = c->conf->routerprio;
632 router = c;
633 }
634 }
635
636 return router;
637 }
638
639 void vpn::send_connect_request (int id)
640 {
641 connection *c = find_router ();
642
643 if (c)
644 c->send_connect_request (id);
645 else
646 // no router found, aggressively connect to all routers
647 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
648 if ((*i)->conf->routerprio && (*i)->conf != THISNODE)
649 (*i)->establish_connection ();
650 }
651
652 void
653 connection::dump_status ()
654 {
655 slog (L_NOTICE, _("node %s (id %d)"), conf->nodename, conf->id);
656 slog (L_NOTICE, _(" connectmode %d (%d) / sockaddr %s / minor %d"),
657 connectmode, conf->connectmode, (const char *)si, (int)prot_minor);
658 slog (L_NOTICE, _(" ictx/octx %08lx/%08lx / oseqno %d / retry_cnt %d"),
659 (long)ictx, (long)octx, (int)oseqno, (int)retry_cnt);
660 slog (L_NOTICE, _(" establish_conn %ld / rekey %ld / keepalive %ld"),
661 (long)(establish_connection.at), (long)(rekey.at), (long)(keepalive.at));
662 }
663
664 void
665 vpn::dump_status ()
666 {
667 slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)NOW);
668
669 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
670 (*c)->dump_status ();
671
672 slog (L_NOTICE, _("END status dump"));
673 }
674
675 vpn::vpn (void)
676 : event (this, &vpn::event_cb)
677 , udpv4_ev_watcher (this, &vpn::udpv4_ev)
678 , ipv4_ev_watcher (this, &vpn::ipv4_ev)
679 #if ENABLE_TCP
680 , tcpv4_ev_watcher (this, &vpn::tcpv4_ev)
681 #endif
682 #if ENABLE_ICMP
683 , icmpv4_ev_watcher(this, &vpn::icmpv4_ev)
684 #endif
685 #if ENABLE_DNS
686 , dnsv4_ev_watcher (this, &vpn::dnsv4_ev)
687 #endif
688 , tap_ev_watcher (this, &vpn::tap_ev)
689 {
690 }
691
692 vpn::~vpn ()
693 {
694 }
695