… | |
… | |
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> |
29 | #include <sys/poll.h> |
30 | #include <sys/wait.h> |
30 | #include <sys/wait.h> |
31 | #include <netinet/in.h> |
|
|
32 | #include <arpa/inet.h> |
|
|
33 | #include <errno.h> |
31 | #include <errno.h> |
34 | #include <time.h> |
32 | #include <time.h> |
35 | #include <unistd.h> |
33 | #include <unistd.h> |
|
|
34 | #include <fcntl.h> |
|
|
35 | #include <sys/socket.h> |
|
|
36 | #include <netinet/in.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 |
|
|
43 | # include <netinet/ip.h> |
|
|
44 | #endif |
|
|
45 | #ifdef HAVE_NETINET_TCP_H |
|
|
46 | # include <netinet/tcp.h> |
|
|
47 | #endif |
|
|
48 | #if ENABLE_ICMP |
|
|
49 | # include <netinet/ip_icmp.h> |
|
|
50 | #endif |
36 | |
51 | |
37 | #include "pidfile.h" |
52 | #include "pidfile.h" |
38 | |
53 | |
39 | #include "connection.h" |
54 | #include "connection.h" |
40 | #include "util.h" |
55 | #include "util.h" |
41 | #include "vpn.h" |
56 | #include "vpn.h" |
|
|
57 | |
|
|
58 | #if !defined(SOL_IP) && defined(IPPROTO_IP) |
|
|
59 | # define SOL_IP IPPROTO_IP |
|
|
60 | #endif |
42 | |
61 | |
43 | ///////////////////////////////////////////////////////////////////////////// |
62 | ///////////////////////////////////////////////////////////////////////////// |
44 | |
63 | |
45 | const char *vpn::script_if_up () |
64 | const char *vpn::script_if_up () |
46 | { |
65 | { |
… | |
… | |
80 | ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); |
99 | ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); |
81 | |
100 | |
82 | if (ipv4_fd < 0) |
101 | if (ipv4_fd < 0) |
83 | return -1; |
102 | return -1; |
84 | |
103 | |
85 | sockinfo si (THISNODE, PROT_IPv4); |
104 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
86 | |
105 | |
87 | if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
106 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
88 | { |
|
|
89 | slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); |
|
|
90 | exit (1); |
|
|
91 | } |
|
|
92 | |
|
|
93 | #ifdef IP_MTU_DISCOVER |
|
|
94 | // this I really consider a linux bug. I am neither connected |
107 | // this I really consider a linux bug. I am neither connected |
95 | // 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 |
96 | // fragment for me sometimes. |
109 | // fragment for me sometimes. |
97 | { |
110 | { |
98 | int oval = IP_PMTUDISC_DONT; |
111 | int oval = IP_PMTUDISC_DONT; |
99 | setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
112 | setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
100 | } |
113 | } |
101 | #endif |
114 | #endif |
102 | |
115 | |
|
|
116 | sockinfo si (THISNODE, PROT_IPv4); |
|
|
117 | |
|
|
118 | if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
119 | { |
|
|
120 | slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); |
|
|
121 | exit (1); |
|
|
122 | } |
|
|
123 | |
103 | ipv4_ev_watcher.start (ipv4_fd, POLLIN); |
124 | ipv4_ev_watcher.start (ipv4_fd, POLLIN); |
104 | } |
125 | } |
105 | |
126 | |
106 | udpv4_fd = -1; |
127 | udpv4_fd = -1; |
107 | |
128 | |
… | |
… | |
109 | { |
130 | { |
110 | udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
131 | udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
111 | |
132 | |
112 | if (udpv4_fd < 0) |
133 | if (udpv4_fd < 0) |
113 | return -1; |
134 | return -1; |
|
|
135 | |
|
|
136 | fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); |
114 | |
137 | |
115 | // standard daemon practise... |
138 | // standard daemon practise... |
116 | { |
139 | { |
117 | int oval = 1; |
140 | int oval = 1; |
118 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
141 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
119 | } |
142 | } |
120 | |
143 | |
121 | sockinfo si (THISNODE, PROT_UDPv4); |
144 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
122 | |
|
|
123 | if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
124 | { |
|
|
125 | slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); |
|
|
126 | exit (1); |
|
|
127 | } |
|
|
128 | |
|
|
129 | #ifdef IP_MTU_DISCOVER |
|
|
130 | // this I really consider a linux bug. I am neither connected |
145 | // this I really consider a linux bug. I am neither connected |
131 | // 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 |
132 | // fragment for me sometimes. |
147 | // fragment for me sometimes. |
133 | { |
148 | { |
134 | int oval = IP_PMTUDISC_DONT; |
149 | int oval = IP_PMTUDISC_DONT; |
135 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
150 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
136 | } |
151 | } |
137 | #endif |
152 | #endif |
138 | |
153 | |
|
|
154 | sockinfo si (THISNODE, PROT_UDPv4); |
|
|
155 | |
|
|
156 | if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
157 | { |
|
|
158 | slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); |
|
|
159 | exit (1); |
|
|
160 | } |
|
|
161 | |
139 | udpv4_ev_watcher.start (udpv4_fd, POLLIN); |
162 | udpv4_ev_watcher.start (udpv4_fd, POLLIN); |
140 | } |
163 | } |
|
|
164 | |
|
|
165 | icmpv4_fd = -1; |
|
|
166 | |
|
|
167 | #if ENABLE_ICMP |
|
|
168 | if (THISNODE->protocols & PROT_ICMPv4) |
|
|
169 | { |
|
|
170 | icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); |
|
|
171 | |
|
|
172 | if (icmpv4_fd < 0) |
|
|
173 | return -1; |
|
|
174 | |
|
|
175 | fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
176 | |
|
|
177 | #ifdef ICMP_FILTER |
|
|
178 | { |
|
|
179 | icmp_filter oval; |
|
|
180 | oval.data = 0xffffffff; |
|
|
181 | if (::conf.icmp_type < 32) |
|
|
182 | oval.data &= ~(1 << ::conf.icmp_type); |
|
|
183 | |
|
|
184 | setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
|
|
185 | } |
|
|
186 | #endif |
|
|
187 | |
|
|
188 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
|
|
189 | // this I really consider a linux bug. I am neither connected |
|
|
190 | // nor do I fragment myself. Linux still sets DF and doesn't |
|
|
191 | // fragment for me sometimes. |
|
|
192 | { |
|
|
193 | int oval = IP_PMTUDISC_DONT; |
|
|
194 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
|
|
195 | } |
|
|
196 | #endif |
|
|
197 | |
|
|
198 | sockinfo si (THISNODE, PROT_ICMPv4); |
|
|
199 | |
|
|
200 | if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
201 | { |
|
|
202 | slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); |
|
|
203 | exit (1); |
|
|
204 | } |
|
|
205 | |
|
|
206 | icmpv4_ev_watcher.start (icmpv4_fd, POLLIN); |
|
|
207 | } |
|
|
208 | #endif |
141 | |
209 | |
142 | tcpv4_fd = -1; |
210 | tcpv4_fd = -1; |
143 | |
211 | |
144 | #if ENABLE_TCP |
212 | #if ENABLE_TCP |
145 | if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port) |
213 | if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port) |
146 | { |
214 | { |
147 | tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
215 | tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
148 | |
216 | |
149 | if (tcpv4_fd < 0) |
217 | if (tcpv4_fd < 0) |
150 | return -1; |
218 | return -1; |
|
|
219 | |
|
|
220 | fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); |
151 | |
221 | |
152 | // standard daemon practise... |
222 | // standard daemon practise... |
153 | { |
223 | { |
154 | int oval = 1; |
224 | int oval = 1; |
155 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
225 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
… | |
… | |
204 | #if ENABLE_TCP |
274 | #if ENABLE_TCP |
205 | case PROT_TCPv4: |
275 | case PROT_TCPv4: |
206 | return send_tcpv4_packet (pkt, si, tos); |
276 | return send_tcpv4_packet (pkt, si, tos); |
207 | #endif |
277 | #endif |
208 | |
278 | |
|
|
279 | #if ENABLE_ICMP |
|
|
280 | case PROT_ICMPv4: |
|
|
281 | return send_icmpv4_packet (pkt, si, tos); |
|
|
282 | #endif |
|
|
283 | |
209 | default: |
284 | default: |
210 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); |
285 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); |
211 | return false; |
286 | return false; |
212 | } |
287 | } |
213 | } |
288 | } |
214 | |
289 | |
215 | bool |
290 | bool |
216 | 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) |
217 | { |
292 | { |
|
|
293 | #if defined(SOL_IP) && defined(IP_TOS) |
218 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
294 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
295 | #endif |
219 | 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 ()); |
220 | |
297 | |
221 | return true; |
298 | return true; |
222 | } |
299 | } |
|
|
300 | |
|
|
301 | static u16 |
|
|
302 | ipv4_checksum (u16 *data, unsigned int len) |
|
|
303 | { |
|
|
304 | // use 32 bit accumulator and fold back carry bits at the end |
|
|
305 | u32 sum = 0; |
|
|
306 | |
|
|
307 | while (len > 1) |
|
|
308 | { |
|
|
309 | sum += *data++; |
|
|
310 | len -= 2; |
|
|
311 | } |
|
|
312 | |
|
|
313 | // odd byte left? |
|
|
314 | if (len) |
|
|
315 | sum += *(u8 *)data; |
|
|
316 | |
|
|
317 | // add back carry bits |
|
|
318 | sum = (sum >> 16) + (sum & 0xffff); // lo += hi |
|
|
319 | sum += (sum >> 16); // carry |
|
|
320 | |
|
|
321 | return ~sum; |
|
|
322 | } |
|
|
323 | |
|
|
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 | |
|
|
342 | bool |
|
|
343 | vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
|
|
344 | { |
|
|
345 | #if defined(SOL_IP) && defined(IP_TOS) |
|
|
346 | setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
347 | #endif |
|
|
348 | |
|
|
349 | pkt->unshift_hdr (4); |
|
|
350 | |
|
|
351 | icmp_header *hdr = (icmp_header *)&((*pkt)[0]); |
|
|
352 | hdr->type = ::conf.icmp_type; |
|
|
353 | hdr->code = 255; |
|
|
354 | hdr->checksum = 0; |
|
|
355 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
|
|
356 | |
|
|
357 | sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
|
|
358 | |
|
|
359 | return true; |
|
|
360 | } |
|
|
361 | #endif |
223 | |
362 | |
224 | bool |
363 | bool |
225 | 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) |
226 | { |
365 | { |
|
|
366 | #if defined(SOL_IP) && defined(IP_TOS) |
227 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
367 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
368 | #endif |
228 | 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 ()); |
229 | |
370 | |
230 | return true; |
371 | return true; |
231 | } |
372 | } |
232 | |
373 | |
… | |
… | |
249 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
390 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
250 | else |
391 | else |
251 | { |
392 | { |
252 | connection *c = conns[src - 1]; |
393 | connection *c = conns[src - 1]; |
253 | |
394 | |
254 | if (dst == 0 && !THISNODE->routerprio) |
395 | if (dst == 0) |
255 | slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), |
396 | slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), |
256 | c->conf->nodename, (const char *)rsi); |
397 | c->conf->nodename, (const char *)rsi); |
257 | else if (dst != 0 && dst != THISNODE->id) |
398 | else if (dst != 0 && dst != THISNODE->id) |
258 | { |
399 | { |
259 | if (THISNODE->routerprio) |
400 | if (THISNODE->routerprio) |
260 | // the tos setting gets lost here. who cares. |
401 | // the tos setting gets lost here. who cares. |
… | |
… | |
269 | c->recv_vpn_packet (pkt, rsi); |
410 | c->recv_vpn_packet (pkt, rsi); |
270 | } |
411 | } |
271 | } |
412 | } |
272 | |
413 | |
273 | void |
414 | void |
274 | vpn::udpv4_ev (io_watcher &w, short revents) |
415 | vpn::ipv4_ev (io_watcher &w, short revents) |
275 | { |
416 | { |
276 | if (revents & (POLLIN | POLLERR)) |
417 | if (revents & (POLLIN | POLLERR)) |
277 | { |
418 | { |
278 | vpn_packet *pkt = new vpn_packet; |
419 | vpn_packet *pkt = new vpn_packet; |
279 | struct sockaddr_in sa; |
420 | struct sockaddr_in sa; |
280 | socklen_t sa_len = sizeof (sa); |
421 | socklen_t sa_len = sizeof (sa); |
281 | int len; |
422 | int len; |
282 | |
423 | |
283 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
424 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
284 | |
425 | |
285 | sockinfo si(sa, PROT_UDPv4); |
426 | sockinfo si(sa, PROT_IPv4); |
286 | |
427 | |
287 | if (len > 0) |
428 | if (len > 0) |
288 | { |
429 | { |
289 | pkt->len = len; |
430 | pkt->len = len; |
290 | |
431 | |
|
|
432 | // raw sockets deliver the ipv4, but don't expect it on sends |
|
|
433 | // this is slow, but... |
|
|
434 | pkt->skip_hdr (IP_OVERHEAD); |
|
|
435 | |
291 | recv_vpn_packet (pkt, si); |
436 | recv_vpn_packet (pkt, si); |
292 | } |
437 | } |
293 | else |
438 | else |
294 | { |
439 | { |
295 | // probably ECONNRESET or somesuch |
440 | // probably ECONNRESET or somesuch |
296 | slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); |
441 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
297 | } |
442 | } |
298 | |
443 | |
299 | delete pkt; |
444 | delete pkt; |
300 | } |
445 | } |
301 | else if (revents & POLLHUP) |
446 | else if (revents & POLLHUP) |
302 | { |
447 | { |
303 | // this cannot ;) happen on udp sockets |
448 | // this cannot ;) happen on udp sockets |
304 | slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating.")); |
449 | slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); |
305 | exit (1); |
450 | exit (1); |
306 | } |
451 | } |
307 | else |
452 | else |
308 | { |
453 | { |
309 | slog (L_ERR, |
454 | slog (L_ERR, |
… | |
… | |
311 | revents); |
456 | revents); |
312 | exit (1); |
457 | exit (1); |
313 | } |
458 | } |
314 | } |
459 | } |
315 | |
460 | |
|
|
461 | #if ENABLE_ICMP |
316 | void |
462 | void |
317 | vpn::ipv4_ev (io_watcher &w, short revents) |
463 | vpn::icmpv4_ev (io_watcher &w, short revents) |
318 | { |
464 | { |
319 | if (revents & (POLLIN | POLLERR)) |
465 | if (revents & (POLLIN | POLLERR)) |
320 | { |
466 | { |
321 | vpn_packet *pkt = new vpn_packet; |
467 | vpn_packet *pkt = new vpn_packet; |
322 | struct sockaddr_in sa; |
468 | struct sockaddr_in sa; |
323 | socklen_t sa_len = sizeof (sa); |
469 | socklen_t sa_len = sizeof (sa); |
324 | int len; |
470 | int len; |
325 | |
471 | |
326 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
472 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
327 | |
473 | |
328 | sockinfo si(sa, PROT_IPv4); |
474 | sockinfo si(sa, PROT_ICMPv4); |
329 | |
475 | |
330 | if (len > 0) |
476 | if (len > 0) |
331 | { |
477 | { |
332 | pkt->len = len; |
478 | pkt->len = len; |
333 | |
479 | |
|
|
480 | icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]); |
|
|
481 | |
|
|
482 | if (hdr->type == ::conf.icmp_type |
|
|
483 | && hdr->code == 255) |
|
|
484 | { |
334 | // 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 |
335 | // this is slow, but... |
486 | // this is slow, but... |
336 | pkt->skip_hdr (IP_OVERHEAD); |
487 | pkt->skip_hdr (ICMP_OVERHEAD); |
337 | |
488 | |
338 | recv_vpn_packet (pkt, si); |
489 | recv_vpn_packet (pkt, si); |
|
|
490 | } |
339 | } |
491 | } |
340 | else |
492 | else |
341 | { |
493 | { |
342 | // probably ECONNRESET or somesuch |
494 | // probably ECONNRESET or somesuch |
343 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
495 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
… | |
… | |
346 | delete pkt; |
498 | delete pkt; |
347 | } |
499 | } |
348 | else if (revents & POLLHUP) |
500 | else if (revents & POLLHUP) |
349 | { |
501 | { |
350 | // this cannot ;) happen on udp sockets |
502 | // this cannot ;) happen on udp sockets |
351 | slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); |
503 | slog (L_ERR, _("FATAL: POLLHUP on icmpv4 fd, terminating.")); |
352 | exit (1); |
504 | exit (1); |
353 | } |
505 | } |
354 | else |
506 | else |
355 | { |
507 | { |
356 | slog (L_ERR, |
508 | slog (L_ERR, |
357 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
509 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
358 | revents); |
510 | revents); |
359 | exit (1); |
511 | exit (1); |
360 | } |
512 | } |
361 | } |
513 | } |
|
|
514 | #endif |
|
|
515 | |
|
|
516 | void |
|
|
517 | vpn::udpv4_ev (io_watcher &w, short revents) |
|
|
518 | { |
|
|
519 | if (revents & (POLLIN | POLLERR)) |
|
|
520 | { |
|
|
521 | vpn_packet *pkt = new vpn_packet; |
|
|
522 | struct sockaddr_in sa; |
|
|
523 | socklen_t sa_len = sizeof (sa); |
|
|
524 | int len; |
|
|
525 | |
|
|
526 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
|
|
527 | |
|
|
528 | sockinfo si(sa, PROT_UDPv4); |
|
|
529 | |
|
|
530 | if (len > 0) |
|
|
531 | { |
|
|
532 | pkt->len = len; |
|
|
533 | |
|
|
534 | recv_vpn_packet (pkt, si); |
|
|
535 | } |
|
|
536 | else |
|
|
537 | { |
|
|
538 | // probably ECONNRESET or somesuch |
|
|
539 | slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); |
|
|
540 | } |
|
|
541 | |
|
|
542 | delete pkt; |
|
|
543 | } |
|
|
544 | else if (revents & POLLHUP) |
|
|
545 | { |
|
|
546 | // this cannot ;) happen on udp sockets |
|
|
547 | slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating.")); |
|
|
548 | exit (1); |
|
|
549 | } |
|
|
550 | else |
|
|
551 | { |
|
|
552 | slog (L_ERR, |
|
|
553 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
|
|
554 | revents); |
|
|
555 | exit (1); |
|
|
556 | } |
|
|
557 | } |
362 | |
558 | |
363 | void |
559 | void |
364 | vpn::tap_ev (io_watcher &w, short revents) |
560 | vpn::tap_ev (io_watcher &w, short revents) |
365 | { |
561 | { |
366 | if (revents & POLLIN) |
562 | if (revents & POLLIN) |
… | |
… | |
373 | int dst = mac2id (pkt->dst); |
569 | int dst = mac2id (pkt->dst); |
374 | int src = mac2id (pkt->src); |
570 | int src = mac2id (pkt->src); |
375 | |
571 | |
376 | if (src != THISNODE->id) |
572 | if (src != THISNODE->id) |
377 | { |
573 | { |
378 | 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.")); |
379 | exit (1); |
575 | exit (1); |
380 | } |
576 | } |
381 | |
577 | |
382 | if (dst == THISNODE->id) |
578 | if (dst == THISNODE->id) |
383 | { |
579 | { |
384 | 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.")); |
385 | exit (1); |
581 | exit (1); |
386 | } |
582 | } |
387 | |
583 | |
388 | if (dst > conns.size ()) |
584 | if (dst > conns.size ()) |
389 | 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); |
… | |
… | |
395 | if (dst != THISNODE->id) |
591 | if (dst != THISNODE->id) |
396 | conns[dst - 1]->inject_data_packet (pkt); |
592 | conns[dst - 1]->inject_data_packet (pkt); |
397 | } |
593 | } |
398 | else |
594 | else |
399 | { |
595 | { |
400 | // broadcast, first check router, then self, then english |
596 | // broadcast, this is ugly, but due to the security policy |
401 | connection *router = find_router (); |
597 | // we have to connect to all hosts... |
402 | |
|
|
403 | if (router) |
|
|
404 | router->inject_data_packet (pkt, true); |
|
|
405 | else |
|
|
406 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
598 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
407 | if ((*c)->conf != THISNODE) |
599 | if ((*c)->conf != THISNODE) |
408 | (*c)->inject_data_packet (pkt); |
600 | (*c)->inject_data_packet (pkt); |
409 | } |
601 | } |
410 | } |
602 | } |
411 | |
603 | |
412 | delete pkt; |
604 | delete pkt; |
413 | } |
605 | } |
… | |
… | |
480 | } |
672 | } |
481 | } |
673 | } |
482 | |
674 | |
483 | connection *vpn::find_router () |
675 | connection *vpn::find_router () |
484 | { |
676 | { |
485 | u32 prio = 0; |
677 | u32 prio = 1; |
486 | connection *router = 0; |
678 | connection *router = 0; |
487 | |
679 | |
488 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
680 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
489 | { |
681 | { |
490 | connection *c = *i; |
682 | connection *c = *i; |
491 | |
683 | |
492 | if (c->conf->routerprio > prio |
684 | if (c->conf->routerprio > prio |
493 | && c->connectmode == conf_node::C_ALWAYS |
685 | && c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use |
494 | && c->conf != THISNODE |
|
|
495 | && c->ictx && c->octx) |
686 | && c->ictx && c->octx |
|
|
687 | && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode |
496 | { |
688 | { |
497 | prio = c->conf->routerprio; |
689 | prio = c->conf->routerprio; |
498 | router = c; |
690 | router = c; |
499 | } |
691 | } |
500 | } |
692 | } |
… | |
… | |
509 | if (c) |
701 | if (c) |
510 | c->send_connect_request (id); |
702 | c->send_connect_request (id); |
511 | else |
703 | else |
512 | // no router found, aggressively connect to all routers |
704 | // no router found, aggressively connect to all routers |
513 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
705 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
514 | if ((*i)->conf->routerprio) |
706 | if ((*i)->conf->routerprio && (*i)->conf != THISNODE) |
515 | (*i)->establish_connection (); |
707 | (*i)->establish_connection (); |
516 | } |
708 | } |
517 | |
709 | |
518 | void |
710 | void |
519 | connection::dump_status () |
711 | connection::dump_status () |
… | |
… | |
537 | |
729 | |
538 | slog (L_NOTICE, _("END status dump")); |
730 | slog (L_NOTICE, _("END status dump")); |
539 | } |
731 | } |
540 | |
732 | |
541 | vpn::vpn (void) |
733 | vpn::vpn (void) |
542 | : event(this, &vpn::event_cb) |
734 | : event (this, &vpn::event_cb) |
543 | , udpv4_ev_watcher(this, &vpn::udpv4_ev) |
735 | , udpv4_ev_watcher (this, &vpn::udpv4_ev) |
544 | , ipv4_ev_watcher (this, &vpn::ipv4_ev) |
736 | , ipv4_ev_watcher (this, &vpn::ipv4_ev) |
545 | , tap_ev_watcher (this, &vpn::tap_ev) |
|
|
546 | #if ENABLE_TCP |
737 | #if ENABLE_TCP |
547 | , tcpv4_ev_watcher(this, &vpn::tcpv4_ev) |
738 | , tcpv4_ev_watcher (this, &vpn::tcpv4_ev) |
548 | #endif |
739 | #endif |
|
|
740 | #if ENABLE_ICMP |
|
|
741 | , icmpv4_ev_watcher(this, &vpn::icmpv4_ev) |
|
|
742 | #endif |
|
|
743 | , tap_ev_watcher (this, &vpn::tap_ev) |
549 | { |
744 | { |
550 | } |
745 | } |
551 | |
746 | |
552 | vpn::~vpn () |
747 | vpn::~vpn () |
553 | { |
748 | { |