… | |
… | |
29 | #include <sys/poll.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" |
… | |
… | |
81 | ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); |
82 | ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); |
82 | |
83 | |
83 | if (ipv4_fd < 0) |
84 | if (ipv4_fd < 0) |
84 | return -1; |
85 | return -1; |
85 | |
86 | |
86 | #ifdef IP_MTU_DISCOVER |
87 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
|
|
88 | |
|
|
89 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
87 | // this I really consider a linux bug. I am neither connected |
90 | // this I really consider a linux bug. I am neither connected |
88 | // nor do I fragment myself. Linux still sets DF and doesn't |
91 | // nor do I fragment myself. Linux still sets DF and doesn't |
89 | // fragment for me sometimes. |
92 | // fragment for me sometimes. |
90 | { |
93 | { |
91 | int oval = IP_PMTUDISC_DONT; |
94 | int oval = IP_PMTUDISC_DONT; |
… | |
… | |
111 | udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
114 | udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
112 | |
115 | |
113 | if (udpv4_fd < 0) |
116 | if (udpv4_fd < 0) |
114 | return -1; |
117 | return -1; |
115 | |
118 | |
|
|
119 | fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
120 | |
116 | // standard daemon practise... |
121 | // standard daemon practise... |
117 | { |
122 | { |
118 | int oval = 1; |
123 | int oval = 1; |
119 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
124 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
120 | } |
125 | } |
121 | |
126 | |
122 | #ifdef IP_MTU_DISCOVER |
127 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
123 | // this I really consider a linux bug. I am neither connected |
128 | // this I really consider a linux bug. I am neither connected |
124 | // nor do I fragment myself. Linux still sets DF and doesn't |
129 | // nor do I fragment myself. Linux still sets DF and doesn't |
125 | // fragment for me sometimes. |
130 | // fragment for me sometimes. |
126 | { |
131 | { |
127 | int oval = IP_PMTUDISC_DONT; |
132 | int oval = IP_PMTUDISC_DONT; |
… | |
… | |
148 | icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); |
153 | icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); |
149 | |
154 | |
150 | if (icmpv4_fd < 0) |
155 | if (icmpv4_fd < 0) |
151 | return -1; |
156 | return -1; |
152 | |
157 | |
|
|
158 | fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
159 | |
153 | #ifdef ICMP_FILTER |
160 | #ifdef ICMP_FILTER |
154 | { |
161 | { |
155 | icmp_filter oval; |
162 | icmp_filter oval; |
156 | oval.data = 0xffffffff; |
163 | oval.data = 0xffffffff; |
157 | if (::conf.icmp_type < 32) |
164 | if (::conf.icmp_type < 32) |
… | |
… | |
159 | |
166 | |
160 | setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
167 | setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
161 | } |
168 | } |
162 | #endif |
169 | #endif |
163 | |
170 | |
164 | #ifdef IP_MTU_DISCOVER |
171 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
165 | // this I really consider a linux bug. I am neither connected |
172 | // this I really consider a linux bug. I am neither connected |
166 | // nor do I fragment myself. Linux still sets DF and doesn't |
173 | // nor do I fragment myself. Linux still sets DF and doesn't |
167 | // fragment for me sometimes. |
174 | // fragment for me sometimes. |
168 | { |
175 | { |
169 | int oval = IP_PMTUDISC_DONT; |
176 | int oval = IP_PMTUDISC_DONT; |
… | |
… | |
191 | tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
198 | tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
192 | |
199 | |
193 | if (tcpv4_fd < 0) |
200 | if (tcpv4_fd < 0) |
194 | return -1; |
201 | return -1; |
195 | |
202 | |
|
|
203 | fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
204 | |
196 | // standard daemon practise... |
205 | // standard daemon practise... |
197 | { |
206 | { |
198 | int oval = 1; |
207 | int oval = 1; |
199 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
208 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
200 | } |
209 | } |
… | |
… | |
262 | } |
271 | } |
263 | |
272 | |
264 | bool |
273 | bool |
265 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
274 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
266 | { |
275 | { |
|
|
276 | #if defined(SOL_IP) && defined(IP_TOS) |
267 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
277 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
278 | #endif |
268 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
279 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
269 | |
280 | |
270 | return true; |
281 | return true; |
271 | } |
282 | } |
272 | |
283 | |
… | |
… | |
291 | sum += (sum >> 16); // carry |
302 | sum += (sum >> 16); // carry |
292 | |
303 | |
293 | return ~sum; |
304 | return ~sum; |
294 | } |
305 | } |
295 | |
306 | |
|
|
307 | #if ENABLE_ICMP |
296 | bool |
308 | bool |
297 | vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
309 | vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
298 | { |
310 | { |
|
|
311 | #if defined(SOL_IP) && defined(IP_TOS) |
299 | setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
312 | setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
313 | #endif |
300 | |
314 | |
301 | pkt->unshift_hdr (4); |
315 | pkt->unshift_hdr (4); |
302 | |
316 | |
303 | icmphdr *hdr = (icmphdr *)&((*pkt)[0]); |
317 | icmp_header *hdr = (icmp_header *)&((*pkt)[0]); |
304 | hdr->type = ::conf.icmp_type; |
318 | hdr->type = ::conf.icmp_type; |
305 | hdr->code = 255; |
319 | hdr->code = 255; |
306 | hdr->checksum = 0; |
320 | hdr->checksum = 0; |
307 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
321 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
308 | |
322 | |
309 | sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
323 | sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
310 | |
324 | |
311 | return true; |
325 | return true; |
312 | } |
326 | } |
|
|
327 | #endif |
313 | |
328 | |
314 | bool |
329 | bool |
315 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
330 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
316 | { |
331 | { |
|
|
332 | #if defined(SOL_IP) && defined(IP_TOS) |
317 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
333 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
334 | #endif |
318 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
335 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
319 | |
336 | |
320 | return true; |
337 | return true; |
321 | } |
338 | } |
322 | |
339 | |
… | |
… | |
339 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
356 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
340 | else |
357 | else |
341 | { |
358 | { |
342 | connection *c = conns[src - 1]; |
359 | connection *c = conns[src - 1]; |
343 | |
360 | |
344 | if (dst == 0 && !THISNODE->routerprio) |
361 | if (dst == 0) |
345 | slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), |
362 | slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), |
346 | c->conf->nodename, (const char *)rsi); |
363 | c->conf->nodename, (const char *)rsi); |
347 | else if (dst != 0 && dst != THISNODE->id) |
364 | else if (dst != 0 && dst != THISNODE->id) |
348 | { |
365 | { |
349 | if (THISNODE->routerprio) |
366 | if (THISNODE->routerprio) |
350 | // the tos setting gets lost here. who cares. |
367 | // the tos setting gets lost here. who cares. |
… | |
… | |
405 | revents); |
422 | revents); |
406 | exit (1); |
423 | exit (1); |
407 | } |
424 | } |
408 | } |
425 | } |
409 | |
426 | |
|
|
427 | #if ENABLE_ICMP |
410 | void |
428 | void |
411 | vpn::icmpv4_ev (io_watcher &w, short revents) |
429 | vpn::icmpv4_ev (io_watcher &w, short revents) |
412 | { |
430 | { |
413 | if (revents & (POLLIN | POLLERR)) |
431 | if (revents & (POLLIN | POLLERR)) |
414 | { |
432 | { |
… | |
… | |
423 | |
441 | |
424 | if (len > 0) |
442 | if (len > 0) |
425 | { |
443 | { |
426 | pkt->len = len; |
444 | pkt->len = len; |
427 | |
445 | |
428 | icmphdr *hdr = (icmphdr *)&((*pkt)[IP_OVERHEAD]); |
446 | icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]); |
429 | |
447 | |
430 | if (hdr->type == ::conf.icmp_type |
448 | if (hdr->type == ::conf.icmp_type |
431 | && hdr->code == 255) |
449 | && hdr->code == 255) |
432 | { |
450 | { |
433 | // raw sockets deliver the ipv4, but don't expect it on sends |
451 | // raw sockets deliver the ipv4, but don't expect it on sends |
… | |
… | |
457 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
475 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
458 | revents); |
476 | revents); |
459 | exit (1); |
477 | exit (1); |
460 | } |
478 | } |
461 | } |
479 | } |
|
|
480 | #endif |
462 | |
481 | |
463 | void |
482 | void |
464 | vpn::udpv4_ev (io_watcher &w, short revents) |
483 | vpn::udpv4_ev (io_watcher &w, short revents) |
465 | { |
484 | { |
466 | if (revents & (POLLIN | POLLERR)) |
485 | if (revents & (POLLIN | POLLERR)) |
… | |
… | |
516 | int dst = mac2id (pkt->dst); |
535 | int dst = mac2id (pkt->dst); |
517 | int src = mac2id (pkt->src); |
536 | int src = mac2id (pkt->src); |
518 | |
537 | |
519 | if (src != THISNODE->id) |
538 | if (src != THISNODE->id) |
520 | { |
539 | { |
521 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating.")); |
540 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting.")); |
522 | exit (1); |
541 | exit (1); |
523 | } |
542 | } |
524 | |
543 | |
525 | if (dst == THISNODE->id) |
544 | if (dst == THISNODE->id) |
526 | { |
545 | { |
527 | slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating.")); |
546 | slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting.")); |
528 | exit (1); |
547 | exit (1); |
529 | } |
548 | } |
530 | |
549 | |
531 | if (dst > conns.size ()) |
550 | if (dst > conns.size ()) |
532 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
551 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
… | |
… | |
538 | if (dst != THISNODE->id) |
557 | if (dst != THISNODE->id) |
539 | conns[dst - 1]->inject_data_packet (pkt); |
558 | conns[dst - 1]->inject_data_packet (pkt); |
540 | } |
559 | } |
541 | else |
560 | else |
542 | { |
561 | { |
543 | // broadcast, first check router, then self, then english |
562 | // broadcast, this is ugly, but due to the security policy |
544 | connection *router = find_router (); |
563 | // we have to connect to all hosts... |
545 | |
|
|
546 | if (router) |
|
|
547 | router->inject_data_packet (pkt, true); |
|
|
548 | else |
|
|
549 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
564 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
550 | if ((*c)->conf != THISNODE) |
565 | if ((*c)->conf != THISNODE) |
551 | (*c)->inject_data_packet (pkt); |
566 | (*c)->inject_data_packet (pkt); |
552 | } |
567 | } |
553 | } |
568 | } |
554 | |
569 | |
555 | delete pkt; |
570 | delete pkt; |
556 | } |
571 | } |
… | |
… | |
623 | } |
638 | } |
624 | } |
639 | } |
625 | |
640 | |
626 | connection *vpn::find_router () |
641 | connection *vpn::find_router () |
627 | { |
642 | { |
628 | u32 prio = 0; |
643 | u32 prio = 1; |
629 | connection *router = 0; |
644 | connection *router = 0; |
630 | |
645 | |
631 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
646 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
632 | { |
647 | { |
633 | connection *c = *i; |
648 | connection *c = *i; |
634 | |
649 | |
635 | if (c->conf->routerprio > prio |
650 | if (c->conf->routerprio > prio |
636 | && c->connectmode == conf_node::C_ALWAYS |
651 | && 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) |
652 | && c->ictx && c->octx |
|
|
653 | && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode |
639 | { |
654 | { |
640 | prio = c->conf->routerprio; |
655 | prio = c->conf->routerprio; |
641 | router = c; |
656 | router = c; |
642 | } |
657 | } |
643 | } |
658 | } |
… | |
… | |
652 | if (c) |
667 | if (c) |
653 | c->send_connect_request (id); |
668 | c->send_connect_request (id); |
654 | else |
669 | else |
655 | // no router found, aggressively connect to all routers |
670 | // no router found, aggressively connect to all routers |
656 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
671 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
657 | if ((*i)->conf->routerprio) |
672 | if ((*i)->conf->routerprio && (*i)->conf != THISNODE) |
658 | (*i)->establish_connection (); |
673 | (*i)->establish_connection (); |
659 | } |
674 | } |
660 | |
675 | |
661 | void |
676 | void |
662 | connection::dump_status () |
677 | connection::dump_status () |