… | |
… | |
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 <fcntl.h> |
34 | #include <fcntl.h> |
35 | #include <arpa/inet.h> |
35 | #include <sys/socket.h> |
36 | #include <netinet/in.h> |
36 | |
37 | #include <netinet/ip_icmp.h> |
37 | #include "netcompat.h" |
38 | |
38 | |
39 | #include "pidfile.h" |
39 | #include "pidfile.h" |
40 | |
40 | |
41 | #include "connection.h" |
41 | #include "connection.h" |
42 | #include "util.h" |
42 | #include "util.h" |
… | |
… | |
84 | if (ipv4_fd < 0) |
84 | if (ipv4_fd < 0) |
85 | return -1; |
85 | return -1; |
86 | |
86 | |
87 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
87 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
88 | |
88 | |
89 | #ifdef IP_MTU_DISCOVER |
89 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
90 | // this I really consider a linux bug. I am neither connected |
90 | // this I really consider a linux bug. I am neither connected |
91 | // 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 |
92 | // fragment for me sometimes. |
92 | // fragment for me sometimes. |
93 | { |
93 | { |
94 | int oval = IP_PMTUDISC_DONT; |
94 | int oval = IP_PMTUDISC_DONT; |
… | |
… | |
122 | { |
122 | { |
123 | int oval = 1; |
123 | int oval = 1; |
124 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
124 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
125 | } |
125 | } |
126 | |
126 | |
127 | #ifdef IP_MTU_DISCOVER |
127 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
128 | // this I really consider a linux bug. I am neither connected |
128 | // this I really consider a linux bug. I am neither connected |
129 | // 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 |
130 | // fragment for me sometimes. |
130 | // fragment for me sometimes. |
131 | { |
131 | { |
132 | int oval = IP_PMTUDISC_DONT; |
132 | int oval = IP_PMTUDISC_DONT; |
… | |
… | |
166 | |
166 | |
167 | setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
167 | setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
168 | } |
168 | } |
169 | #endif |
169 | #endif |
170 | |
170 | |
171 | #ifdef IP_MTU_DISCOVER |
171 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
172 | // this I really consider a linux bug. I am neither connected |
172 | // this I really consider a linux bug. I am neither connected |
173 | // 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 |
174 | // fragment for me sometimes. |
174 | // fragment for me sometimes. |
175 | { |
175 | { |
176 | int oval = IP_PMTUDISC_DONT; |
176 | int oval = IP_PMTUDISC_DONT; |
… | |
… | |
271 | } |
271 | } |
272 | |
272 | |
273 | bool |
273 | bool |
274 | 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) |
275 | { |
275 | { |
|
|
276 | #if defined(SOL_IP) && defined(IP_TOS) |
276 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
277 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
278 | #endif |
277 | 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 ()); |
278 | |
280 | |
279 | return true; |
281 | return true; |
280 | } |
282 | } |
281 | |
283 | |
… | |
… | |
304 | |
306 | |
305 | #if ENABLE_ICMP |
307 | #if ENABLE_ICMP |
306 | bool |
308 | bool |
307 | 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) |
308 | { |
310 | { |
|
|
311 | #if defined(SOL_IP) && defined(IP_TOS) |
309 | setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
312 | setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
313 | #endif |
310 | |
314 | |
311 | pkt->unshift_hdr (4); |
315 | pkt->unshift_hdr (4); |
312 | |
316 | |
313 | icmphdr *hdr = (icmphdr *)&((*pkt)[0]); |
317 | icmp_header *hdr = (icmp_header *)&((*pkt)[0]); |
314 | hdr->type = ::conf.icmp_type; |
318 | hdr->type = ::conf.icmp_type; |
315 | hdr->code = 255; |
319 | hdr->code = 255; |
316 | hdr->checksum = 0; |
320 | hdr->checksum = 0; |
317 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
321 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
318 | |
322 | |
… | |
… | |
323 | #endif |
327 | #endif |
324 | |
328 | |
325 | bool |
329 | bool |
326 | 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) |
327 | { |
331 | { |
|
|
332 | #if defined(SOL_IP) && defined(IP_TOS) |
328 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
333 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
334 | #endif |
329 | 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 ()); |
330 | |
336 | |
331 | return true; |
337 | return true; |
332 | } |
338 | } |
333 | |
339 | |
… | |
… | |
435 | |
441 | |
436 | if (len > 0) |
442 | if (len > 0) |
437 | { |
443 | { |
438 | pkt->len = len; |
444 | pkt->len = len; |
439 | |
445 | |
440 | icmphdr *hdr = (icmphdr *)&((*pkt)[IP_OVERHEAD]); |
446 | icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]); |
441 | |
447 | |
442 | if (hdr->type == ::conf.icmp_type |
448 | if (hdr->type == ::conf.icmp_type |
443 | && hdr->code == 255) |
449 | && hdr->code == 255) |
444 | { |
450 | { |
445 | // 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 |
… | |
… | |
529 | int dst = mac2id (pkt->dst); |
535 | int dst = mac2id (pkt->dst); |
530 | int src = mac2id (pkt->src); |
536 | int src = mac2id (pkt->src); |
531 | |
537 | |
532 | if (src != THISNODE->id) |
538 | if (src != THISNODE->id) |
533 | { |
539 | { |
534 | 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.")); |
535 | exit (1); |
541 | exit (1); |
536 | } |
542 | } |
537 | |
543 | |
538 | if (dst == THISNODE->id) |
544 | if (dst == THISNODE->id) |
539 | { |
545 | { |
540 | 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.")); |
541 | exit (1); |
547 | exit (1); |
542 | } |
548 | } |
543 | |
549 | |
544 | if (dst > conns.size ()) |
550 | if (dst > conns.size ()) |
545 | 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); |