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