ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vpn.C
Revision: 1.26
Committed: Sun Jul 25 18:11:54 2004 UTC (19 years, 10 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1_7
Changes since 1.25: +1 -1 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 // send a vpn packet out to other hosts
271 bool
272 vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos)
273 {
274 switch (si.prot)
275 {
276 case PROT_IPv4:
277 return send_ipv4_packet (pkt, si, tos);
278
279 case PROT_UDPv4:
280 return send_udpv4_packet (pkt, si, tos);
281
282 #if ENABLE_TCP
283 case PROT_TCPv4:
284 return send_tcpv4_packet (pkt, si, tos);
285 #endif
286
287 #if ENABLE_ICMP
288 case PROT_ICMPv4:
289 return send_icmpv4_packet (pkt, si, tos);
290 #endif
291
292 #if ENABLE_DNS
293 case PROT_DNSv4:
294 return send_dnsv4_packet (pkt, si, tos);
295 #endif
296
297 default:
298 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si);
299 return false;
300 }
301 }
302
303 bool
304 vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
305 {
306 #if defined(SOL_IP) && defined(IP_TOS)
307 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
308 #endif
309 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
310
311 return true;
312 }
313
314 static u16
315 ipv4_checksum (u16 *data, unsigned int len)
316 {
317 // use 32 bit accumulator and fold back carry bits at the end
318 u32 sum = 0;
319
320 while (len > 1)
321 {
322 sum += *data++;
323 len -= 2;
324 }
325
326 // odd byte left?
327 if (len)
328 sum += *(u8 *)data;
329
330 // add back carry bits
331 sum = (sum >> 16) + (sum & 0xffff); // lo += hi
332 sum += (sum >> 16); // carry
333
334 return ~sum;
335 }
336
337 #if ENABLE_ICMP
338 bool
339 vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
340 {
341 #if defined(SOL_IP) && defined(IP_TOS)
342 setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
343 #endif
344
345 pkt->unshift_hdr (4);
346
347 icmp_header *hdr = (icmp_header *)&((*pkt)[0]);
348 hdr->type = ::conf.icmp_type;
349 hdr->code = 255;
350 hdr->checksum = 0;
351 hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len);
352
353 sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
354
355 return true;
356 }
357 #endif
358
359 bool
360 vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
361 {
362 #if defined(SOL_IP) && defined(IP_TOS)
363 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
364 #endif
365 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
366
367 return true;
368 }
369
370 void
371 vpn::inject_data_packet (tap_packet *pkt, int dst)
372 {
373 if (dst)
374 {
375 // unicast
376 if (dst != THISNODE->id)
377 conns[dst - 1]->inject_data_packet (pkt);
378 }
379 else
380 {
381 // broadcast, this is ugly, but due to the security policy
382 // we have to connect to all hosts...
383 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
384 if ((*c)->conf != THISNODE)
385 (*c)->inject_data_packet (pkt, true);
386 }
387 }
388
389 void
390 vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
391 {
392 unsigned int src = pkt->src ();
393 unsigned int dst = pkt->dst ();
394
395 slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d"),
396 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst (), pkt->len);
397
398 if (src == 0 || src > conns.size ()
399 || dst > conns.size ()
400 || pkt->typ () >= vpn_packet::PT_MAX)
401 slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"),
402 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ());
403 else if (dst > conns.size ())
404 slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"),
405 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ());
406 else
407 {
408 connection *c = conns[src - 1];
409
410 if (dst == 0)
411 slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"),
412 c->conf->nodename, (const char *)rsi);
413 else if (dst != THISNODE->id)
414 {
415 if (THISNODE->routerprio)
416 // the tos setting gets lost here. who cares.
417 conns[dst - 1]->inject_vpn_packet (pkt);
418 else
419 slog (L_WARN,
420 _("%s(%s): forwarding request (=> %s), but we are no router"),
421 c->conf->nodename, (const char *)rsi,
422 conns[dst - 1]->conf->nodename);
423 }
424 else
425 c->recv_vpn_packet (pkt, rsi);
426 }
427 }
428
429 void
430 vpn::ipv4_ev (io_watcher &w, short revents)
431 {
432 if (revents & EVENT_READ)
433 {
434 vpn_packet *pkt = new vpn_packet;
435 struct sockaddr_in sa;
436 socklen_t sa_len = sizeof (sa);
437 int len;
438
439 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
440
441 sockinfo si(sa, PROT_IPv4);
442
443 if (len > 0)
444 {
445 pkt->len = len;
446
447 // raw sockets deliver the ipv4, but don't expect it on sends
448 // this is slow, but...
449 pkt->skip_hdr (IP_OVERHEAD);
450
451 recv_vpn_packet (pkt, si);
452 }
453 else
454 {
455 // probably ECONNRESET or somesuch
456 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno));
457 }
458
459 delete pkt;
460 }
461 else
462 {
463 slog (L_ERR,
464 _("FATAL: unknown revents %08x in socket, terminating\n"),
465 revents);
466 exit (EXIT_FAILURE);
467 }
468 }
469
470 #if ENABLE_ICMP
471 void
472 vpn::icmpv4_ev (io_watcher &w, short revents)
473 {
474 if (revents & EVENT_READ)
475 {
476 vpn_packet *pkt = new vpn_packet;
477 struct sockaddr_in sa;
478 socklen_t sa_len = sizeof (sa);
479 int len;
480
481 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
482
483 sockinfo si(sa, PROT_ICMPv4);
484
485 if (len > 0)
486 {
487 pkt->len = len;
488
489 icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]);
490
491 if (hdr->type == ::conf.icmp_type
492 && hdr->code == 255)
493 {
494 // raw sockets deliver the ipv4, but don't expect it on sends
495 // this is slow, but...
496 pkt->skip_hdr (ICMP_OVERHEAD);
497
498 recv_vpn_packet (pkt, si);
499 }
500 }
501 else
502 {
503 // probably ECONNRESET or somesuch
504 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno));
505 }
506
507 delete pkt;
508 }
509 else
510 {
511 slog (L_ERR,
512 _("FATAL: unknown revents %08x in socket, terminating\n"),
513 revents);
514 exit (EXIT_FAILURE);
515 }
516 }
517 #endif
518
519 void
520 vpn::udpv4_ev (io_watcher &w, short revents)
521 {
522 if (revents & EVENT_READ)
523 {
524 vpn_packet *pkt = new vpn_packet;
525 struct sockaddr_in sa;
526 socklen_t sa_len = sizeof (sa);
527 int len;
528
529 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
530
531 sockinfo si(sa, PROT_UDPv4);
532
533 if (len > 0)
534 {
535 pkt->len = len;
536
537 recv_vpn_packet (pkt, si);
538 }
539 else
540 {
541 // probably ECONNRESET or somesuch
542 slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno));
543 }
544
545 delete pkt;
546 }
547 else
548 {
549 slog (L_ERR,
550 _("FATAL: unknown revents %08x in socket, terminating\n"),
551 revents);
552 exit (EXIT_FAILURE);
553 }
554 }
555
556 void
557 vpn::tap_ev (io_watcher &w, short revents)
558 {
559 if (revents & EVENT_READ)
560 {
561 /* process data */
562 tap_packet *pkt;
563
564 pkt = tap->recv ();
565
566 if (!pkt)
567 return;
568
569 if (pkt->len > 14)
570 {
571 int dst = mac2id (pkt->dst);
572 int src = mac2id (pkt->src);
573
574 if (src != THISNODE->id)
575 {
576 slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting."));
577 exit (EXIT_FAILURE);
578 }
579
580 if (dst == THISNODE->id)
581 {
582 slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting."));
583 exit (EXIT_FAILURE);
584 }
585
586 if (dst > conns.size ())
587 slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst);
588 else
589 inject_data_packet (pkt, dst);
590 }
591
592 delete pkt;
593 }
594 else
595 abort ();
596 }
597
598 void
599 vpn::event_cb (time_watcher &w)
600 {
601 if (events)
602 {
603 if (events & EVENT_SHUTDOWN)
604 {
605 slog (L_INFO, _("preparing shutdown..."));
606
607 shutdown_all ();
608 remove_pid (conf.pidfilename);
609 slog (L_INFO, _("terminating"));
610 exit (EXIT_SUCCESS);
611 }
612
613 if (events & EVENT_RECONNECT)
614 {
615 slog (L_INFO, _("forced reconnect"));
616
617 reconnect_all ();
618 }
619
620 events = 0;
621 }
622 }
623
624 void
625 vpn::shutdown_all ()
626 {
627 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
628 (*c)->shutdown ();
629 }
630
631 void
632 vpn::reconnect_all ()
633 {
634 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
635 delete *c;
636
637 conns.clear ();
638
639 connection_init ();
640
641 for (configuration::node_vector::iterator i = conf.nodes.begin ();
642 i != conf.nodes.end (); ++i)
643 {
644 connection *conn = new connection (this);
645
646 conn->conf = *i;
647 conns.push_back (conn);
648
649 conn->establish_connection ();
650 }
651 }
652
653 connection *vpn::find_router ()
654 {
655 u32 prio = 1;
656 connection *router = 0;
657
658 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
659 {
660 connection *c = *i;
661
662 if (c->conf->routerprio > prio
663 && c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use
664 && c->ictx && c->octx
665 && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode
666 {
667 prio = c->conf->routerprio;
668 router = c;
669 }
670 }
671
672 return router;
673 }
674
675 void vpn::send_connect_request (int id)
676 {
677 connection *c = find_router ();
678
679 if (c)
680 c->send_connect_request (id);
681 else
682 // no router found, aggressively connect to all routers
683 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
684 if ((*i)->conf->routerprio && (*i)->conf != THISNODE)
685 (*i)->establish_connection ();
686 }
687
688 void
689 connection::dump_status ()
690 {
691 slog (L_NOTICE, _("node %s (id %d)"), conf->nodename, conf->id);
692 slog (L_NOTICE, _(" connectmode %d (%d) / sockaddr %s / minor %d"),
693 connectmode, conf->connectmode, (const char *)si, (int)prot_minor);
694 slog (L_NOTICE, _(" ictx/octx %08lx/%08lx / oseqno %d / retry_cnt %d"),
695 (long)ictx, (long)octx, (int)oseqno, (int)retry_cnt);
696 slog (L_NOTICE, _(" establish_conn %ld / rekey %ld / keepalive %ld"),
697 (long)(establish_connection.at), (long)(rekey.at), (long)(keepalive.at));
698 }
699
700 void
701 vpn::dump_status ()
702 {
703 slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)NOW);
704
705 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
706 (*c)->dump_status ();
707
708 slog (L_NOTICE, _("END status dump"));
709 }
710
711 vpn::vpn (void)
712 : event (this, &vpn::event_cb)
713 , udpv4_ev_watcher (this, &vpn::udpv4_ev)
714 , ipv4_ev_watcher (this, &vpn::ipv4_ev)
715 #if ENABLE_TCP
716 , tcpv4_ev_watcher (this, &vpn::tcpv4_ev)
717 #endif
718 #if ENABLE_ICMP
719 , icmpv4_ev_watcher(this, &vpn::icmpv4_ev)
720 #endif
721 #if ENABLE_DNS
722 , dnsv4_ev_watcher (this, &vpn::dnsv4_ev)
723 #endif
724 , tap_ev_watcher (this, &vpn::tap_ev)
725 {
726 }
727
728 vpn::~vpn ()
729 {
730 }
731