… | |
… | |
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 <sys/socket.h> |
|
|
36 | #include <netinet/in.h> |
35 | #include <arpa/inet.h> |
37 | #include <arpa/inet.h> |
|
|
38 | #include <net/if.h> |
|
|
39 | #ifdef HAVE_NETINET_IN_SYSTM_H |
|
|
40 | # include <netinet/in_systm.h> |
|
|
41 | #endif |
|
|
42 | #ifdef HAVE_NETINET_IP_H |
36 | #include <netinet/in.h> |
43 | # include <netinet/ip.h> |
|
|
44 | #endif |
|
|
45 | #ifdef HAVE_NETINET_TCP_H |
|
|
46 | # include <netinet/tcp.h> |
|
|
47 | #endif |
|
|
48 | #if ENABLE_ICMP |
37 | #include <netinet/ip_icmp.h> |
49 | # include <netinet/ip_icmp.h> |
|
|
50 | #endif |
38 | |
51 | |
39 | #include "pidfile.h" |
52 | #include "pidfile.h" |
40 | |
53 | |
41 | #include "connection.h" |
54 | #include "connection.h" |
42 | #include "util.h" |
55 | #include "util.h" |
43 | #include "vpn.h" |
56 | #include "vpn.h" |
|
|
57 | |
|
|
58 | #if !defined(SOL_IP) && defined(IPPROTO_IP) |
|
|
59 | # define SOL_IP IPPROTO_IP |
|
|
60 | #endif |
44 | |
61 | |
45 | ///////////////////////////////////////////////////////////////////////////// |
62 | ///////////////////////////////////////////////////////////////////////////// |
46 | |
63 | |
47 | const char *vpn::script_if_up () |
64 | const char *vpn::script_if_up () |
48 | { |
65 | { |
… | |
… | |
84 | if (ipv4_fd < 0) |
101 | if (ipv4_fd < 0) |
85 | return -1; |
102 | return -1; |
86 | |
103 | |
87 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
104 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
88 | |
105 | |
89 | #ifdef IP_MTU_DISCOVER |
106 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
90 | // this I really consider a linux bug. I am neither connected |
107 | // this I really consider a linux bug. I am neither connected |
91 | // nor do I fragment myself. Linux still sets DF and doesn't |
108 | // nor do I fragment myself. Linux still sets DF and doesn't |
92 | // fragment for me sometimes. |
109 | // fragment for me sometimes. |
93 | { |
110 | { |
94 | int oval = IP_PMTUDISC_DONT; |
111 | int oval = IP_PMTUDISC_DONT; |
… | |
… | |
122 | { |
139 | { |
123 | int oval = 1; |
140 | int oval = 1; |
124 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
141 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
125 | } |
142 | } |
126 | |
143 | |
127 | #ifdef IP_MTU_DISCOVER |
144 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
128 | // this I really consider a linux bug. I am neither connected |
145 | // this I really consider a linux bug. I am neither connected |
129 | // nor do I fragment myself. Linux still sets DF and doesn't |
146 | // nor do I fragment myself. Linux still sets DF and doesn't |
130 | // fragment for me sometimes. |
147 | // fragment for me sometimes. |
131 | { |
148 | { |
132 | int oval = IP_PMTUDISC_DONT; |
149 | int oval = IP_PMTUDISC_DONT; |
… | |
… | |
166 | |
183 | |
167 | setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
184 | setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
168 | } |
185 | } |
169 | #endif |
186 | #endif |
170 | |
187 | |
171 | #ifdef IP_MTU_DISCOVER |
188 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
172 | // this I really consider a linux bug. I am neither connected |
189 | // this I really consider a linux bug. I am neither connected |
173 | // nor do I fragment myself. Linux still sets DF and doesn't |
190 | // nor do I fragment myself. Linux still sets DF and doesn't |
174 | // fragment for me sometimes. |
191 | // fragment for me sometimes. |
175 | { |
192 | { |
176 | int oval = IP_PMTUDISC_DONT; |
193 | int oval = IP_PMTUDISC_DONT; |
… | |
… | |
271 | } |
288 | } |
272 | |
289 | |
273 | bool |
290 | bool |
274 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
291 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
275 | { |
292 | { |
|
|
293 | #if defined(SOL_IP) && defined(IP_TOS) |
276 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
294 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
295 | #endif |
277 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
296 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
278 | |
297 | |
279 | return true; |
298 | return true; |
280 | } |
299 | } |
281 | |
300 | |
… | |
… | |
301 | |
320 | |
302 | return ~sum; |
321 | return ~sum; |
303 | } |
322 | } |
304 | |
323 | |
305 | #if ENABLE_ICMP |
324 | #if ENABLE_ICMP |
|
|
325 | struct icmp_header { |
|
|
326 | u8 type; |
|
|
327 | u8 code; |
|
|
328 | u16 checksum; |
|
|
329 | union { |
|
|
330 | struct { |
|
|
331 | u16 id; |
|
|
332 | u16 sequence; |
|
|
333 | } echo; |
|
|
334 | u32 gateway; |
|
|
335 | struct { |
|
|
336 | u16 unused; |
|
|
337 | u16 mtu; |
|
|
338 | } frag; |
|
|
339 | } un; |
|
|
340 | }; |
|
|
341 | |
306 | bool |
342 | bool |
307 | vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
343 | vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
308 | { |
344 | { |
|
|
345 | #if defined(SOL_IP) && defined(IP_TOS) |
309 | setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
346 | setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
347 | #endif |
310 | |
348 | |
311 | pkt->unshift_hdr (4); |
349 | pkt->unshift_hdr (4); |
312 | |
350 | |
313 | icmphdr *hdr = (icmphdr *)&((*pkt)[0]); |
351 | icmp_header *hdr = (icmp_header *)&((*pkt)[0]); |
314 | hdr->type = ::conf.icmp_type; |
352 | hdr->type = ::conf.icmp_type; |
315 | hdr->code = 255; |
353 | hdr->code = 255; |
316 | hdr->checksum = 0; |
354 | hdr->checksum = 0; |
317 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
355 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
318 | |
356 | |
… | |
… | |
323 | #endif |
361 | #endif |
324 | |
362 | |
325 | bool |
363 | bool |
326 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
364 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
327 | { |
365 | { |
|
|
366 | #if defined(SOL_IP) && defined(IP_TOS) |
328 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
367 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
368 | #endif |
329 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
369 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
330 | |
370 | |
331 | return true; |
371 | return true; |
332 | } |
372 | } |
333 | |
373 | |
… | |
… | |
435 | |
475 | |
436 | if (len > 0) |
476 | if (len > 0) |
437 | { |
477 | { |
438 | pkt->len = len; |
478 | pkt->len = len; |
439 | |
479 | |
440 | icmphdr *hdr = (icmphdr *)&((*pkt)[IP_OVERHEAD]); |
480 | icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]); |
441 | |
481 | |
442 | if (hdr->type == ::conf.icmp_type |
482 | if (hdr->type == ::conf.icmp_type |
443 | && hdr->code == 255) |
483 | && hdr->code == 255) |
444 | { |
484 | { |
445 | // raw sockets deliver the ipv4, but don't expect it on sends |
485 | // raw sockets deliver the ipv4, but don't expect it on sends |
… | |
… | |
529 | int dst = mac2id (pkt->dst); |
569 | int dst = mac2id (pkt->dst); |
530 | int src = mac2id (pkt->src); |
570 | int src = mac2id (pkt->src); |
531 | |
571 | |
532 | if (src != THISNODE->id) |
572 | if (src != THISNODE->id) |
533 | { |
573 | { |
534 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating.")); |
574 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting.")); |
535 | exit (1); |
575 | exit (1); |
536 | } |
576 | } |
537 | |
577 | |
538 | if (dst == THISNODE->id) |
578 | if (dst == THISNODE->id) |
539 | { |
579 | { |
540 | slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating.")); |
580 | slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting.")); |
541 | exit (1); |
581 | exit (1); |
542 | } |
582 | } |
543 | |
583 | |
544 | if (dst > conns.size ()) |
584 | if (dst > conns.size ()) |
545 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
585 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |