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