… | |
… | |
24 | #include <cstring> |
24 | #include <cstring> |
25 | #include <cstdio> |
25 | #include <cstdio> |
26 | |
26 | |
27 | #include <sys/types.h> |
27 | #include <sys/types.h> |
28 | #include <sys/socket.h> |
28 | #include <sys/socket.h> |
29 | #include <sys/poll.h> |
|
|
30 | #include <sys/wait.h> |
29 | #include <sys/wait.h> |
31 | #include <errno.h> |
30 | #include <errno.h> |
32 | #include <time.h> |
31 | #include <time.h> |
33 | #include <unistd.h> |
32 | #include <unistd.h> |
34 | #include <arpa/inet.h> |
33 | #include <fcntl.h> |
35 | #include <netinet/in.h> |
34 | #include <sys/socket.h> |
36 | #include <netinet/ip_icmp.h> |
35 | |
|
|
36 | #include "netcompat.h" |
37 | |
37 | |
38 | #include "pidfile.h" |
38 | #include "pidfile.h" |
39 | |
39 | |
40 | #include "connection.h" |
40 | #include "connection.h" |
41 | #include "util.h" |
41 | #include "util.h" |
… | |
… | |
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; |
321 | } |
337 | } |
322 | |
338 | |
… | |
… | |
339 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
355 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
340 | else |
356 | else |
341 | { |
357 | { |
342 | connection *c = conns[src - 1]; |
358 | connection *c = conns[src - 1]; |
343 | |
359 | |
344 | if (dst == 0 && !THISNODE->routerprio) |
360 | if (dst == 0) |
345 | slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), |
361 | slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), |
346 | c->conf->nodename, (const char *)rsi); |
362 | c->conf->nodename, (const char *)rsi); |
347 | else if (dst != 0 && dst != THISNODE->id) |
363 | else if (dst != 0 && dst != THISNODE->id) |
348 | { |
364 | { |
349 | if (THISNODE->routerprio) |
365 | if (THISNODE->routerprio) |
350 | // the tos setting gets lost here. who cares. |
366 | // the tos setting gets lost here. who cares. |
… | |
… | |
405 | revents); |
421 | revents); |
406 | exit (1); |
422 | exit (1); |
407 | } |
423 | } |
408 | } |
424 | } |
409 | |
425 | |
|
|
426 | #if ENABLE_ICMP |
410 | void |
427 | void |
411 | vpn::icmpv4_ev (io_watcher &w, short revents) |
428 | vpn::icmpv4_ev (io_watcher &w, short revents) |
412 | { |
429 | { |
413 | if (revents & (POLLIN | POLLERR)) |
430 | if (revents & (POLLIN | POLLERR)) |
414 | { |
431 | { |
… | |
… | |
423 | |
440 | |
424 | if (len > 0) |
441 | if (len > 0) |
425 | { |
442 | { |
426 | pkt->len = len; |
443 | pkt->len = len; |
427 | |
444 | |
428 | icmphdr *hdr = (icmphdr *)&((*pkt)[IP_OVERHEAD]); |
445 | icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]); |
429 | |
446 | |
430 | if (hdr->type == ::conf.icmp_type |
447 | if (hdr->type == ::conf.icmp_type |
431 | && hdr->code == 255) |
448 | && hdr->code == 255) |
432 | { |
449 | { |
433 | // raw sockets deliver the ipv4, but don't expect it on sends |
450 | // raw sockets deliver the ipv4, but don't expect it on sends |
… | |
… | |
457 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
474 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
458 | revents); |
475 | revents); |
459 | exit (1); |
476 | exit (1); |
460 | } |
477 | } |
461 | } |
478 | } |
|
|
479 | #endif |
462 | |
480 | |
463 | void |
481 | void |
464 | vpn::udpv4_ev (io_watcher &w, short revents) |
482 | vpn::udpv4_ev (io_watcher &w, short revents) |
465 | { |
483 | { |
466 | if (revents & (POLLIN | POLLERR)) |
484 | if (revents & (POLLIN | POLLERR)) |
… | |
… | |
511 | /* process data */ |
529 | /* process data */ |
512 | tap_packet *pkt; |
530 | tap_packet *pkt; |
513 | |
531 | |
514 | pkt = tap->recv (); |
532 | pkt = tap->recv (); |
515 | |
533 | |
|
|
534 | if (!pkt) |
|
|
535 | return; |
|
|
536 | |
|
|
537 | if (pkt->len > 14) |
|
|
538 | { |
516 | int dst = mac2id (pkt->dst); |
539 | int dst = mac2id (pkt->dst); |
517 | int src = mac2id (pkt->src); |
540 | int src = mac2id (pkt->src); |
518 | |
541 | |
519 | if (src != THISNODE->id) |
542 | 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 | { |
543 | { |
537 | // unicast |
544 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting.")); |
538 | if (dst != THISNODE->id) |
545 | exit (1); |
539 | conns[dst - 1]->inject_data_packet (pkt); |
|
|
540 | } |
546 | } |
|
|
547 | |
|
|
548 | if (dst == THISNODE->id) |
|
|
549 | { |
|
|
550 | slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting.")); |
|
|
551 | exit (1); |
|
|
552 | } |
|
|
553 | |
|
|
554 | if (dst > conns.size ()) |
|
|
555 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
541 | else |
556 | else |
542 | { |
557 | { |
543 | // broadcast, first check router, then self, then english |
|
|
544 | connection *router = find_router (); |
|
|
545 | |
|
|
546 | if (router) |
558 | if (dst) |
|
|
559 | { |
|
|
560 | // unicast |
|
|
561 | if (dst != THISNODE->id) |
547 | router->inject_data_packet (pkt, true); |
562 | conns[dst - 1]->inject_data_packet (pkt); |
|
|
563 | } |
548 | else |
564 | else |
|
|
565 | { |
|
|
566 | // broadcast, this is ugly, but due to the security policy |
|
|
567 | // we have to connect to all hosts... |
549 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
568 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
550 | if ((*c)->conf != THISNODE) |
569 | if ((*c)->conf != THISNODE) |
551 | (*c)->inject_data_packet (pkt); |
570 | (*c)->inject_data_packet (pkt); |
|
|
571 | } |
552 | } |
572 | } |
553 | } |
573 | } |
554 | |
574 | |
555 | delete pkt; |
575 | delete pkt; |
556 | } |
576 | } |
… | |
… | |
623 | } |
643 | } |
624 | } |
644 | } |
625 | |
645 | |
626 | connection *vpn::find_router () |
646 | connection *vpn::find_router () |
627 | { |
647 | { |
628 | u32 prio = 0; |
648 | u32 prio = 1; |
629 | connection *router = 0; |
649 | connection *router = 0; |
630 | |
650 | |
631 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
651 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
632 | { |
652 | { |
633 | connection *c = *i; |
653 | connection *c = *i; |
634 | |
654 | |
635 | if (c->conf->routerprio > prio |
655 | if (c->conf->routerprio > prio |
636 | && c->connectmode == conf_node::C_ALWAYS |
656 | && 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) |
657 | && c->ictx && c->octx |
|
|
658 | && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode |
639 | { |
659 | { |
640 | prio = c->conf->routerprio; |
660 | prio = c->conf->routerprio; |
641 | router = c; |
661 | router = c; |
642 | } |
662 | } |
643 | } |
663 | } |
… | |
… | |
652 | if (c) |
672 | if (c) |
653 | c->send_connect_request (id); |
673 | c->send_connect_request (id); |
654 | else |
674 | else |
655 | // no router found, aggressively connect to all routers |
675 | // no router found, aggressively connect to all routers |
656 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
676 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
657 | if ((*i)->conf->routerprio) |
677 | if ((*i)->conf->routerprio && (*i)->conf != THISNODE) |
658 | (*i)->establish_connection (); |
678 | (*i)->establish_connection (); |
659 | } |
679 | } |
660 | |
680 | |
661 | void |
681 | void |
662 | connection::dump_status () |
682 | connection::dump_status () |