1 | /* |
1 | /* |
2 | vpn.C -- handle the protocol, encryption, handshaking etc. |
2 | vpn.C -- handle the protocol, encryption, handshaking etc. |
|
|
3 | Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
3 | |
4 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
24 | #include <cstring> |
25 | #include <cstring> |
25 | #include <cstdio> |
26 | #include <cstdio> |
26 | |
27 | |
27 | #include <sys/types.h> |
28 | #include <sys/types.h> |
28 | #include <sys/socket.h> |
29 | #include <sys/socket.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 | |
|
|
37 | #include "netcompat.h" |
36 | |
38 | |
37 | #include "pidfile.h" |
39 | #include "pidfile.h" |
38 | |
40 | |
39 | #include "connection.h" |
41 | #include "connection.h" |
40 | #include "util.h" |
42 | #include "util.h" |
41 | #include "vpn.h" |
43 | #include "vpn.h" |
|
|
44 | |
|
|
45 | vpn network; // THE vpn (bad design...) |
42 | |
46 | |
43 | ///////////////////////////////////////////////////////////////////////////// |
47 | ///////////////////////////////////////////////////////////////////////////// |
44 | |
48 | |
45 | const char *vpn::script_if_up () |
49 | const char *vpn::script_if_up () |
46 | { |
50 | { |
… | |
… | |
80 | ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); |
84 | ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); |
81 | |
85 | |
82 | if (ipv4_fd < 0) |
86 | if (ipv4_fd < 0) |
83 | return -1; |
87 | return -1; |
84 | |
88 | |
85 | sockinfo si (THISNODE, PROT_IPv4); |
89 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
86 | |
90 | |
87 | if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
91 | #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 |
92 | // this I really consider a linux bug. I am neither connected |
95 | // nor do I fragment myself. Linux still sets DF and doesn't |
93 | // nor do I fragment myself. Linux still sets DF and doesn't |
96 | // fragment for me sometimes. |
94 | // fragment for me sometimes. |
97 | { |
95 | { |
98 | int oval = IP_PMTUDISC_DONT; |
96 | int oval = IP_PMTUDISC_DONT; |
99 | setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
97 | setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
100 | } |
98 | } |
101 | #endif |
99 | #endif |
102 | |
100 | |
|
|
101 | sockinfo si (THISNODE, PROT_IPv4); |
|
|
102 | |
|
|
103 | if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
104 | { |
|
|
105 | slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); |
|
|
106 | exit (1); |
|
|
107 | } |
|
|
108 | |
103 | ipv4_ev_watcher.start (ipv4_fd, POLLIN); |
109 | ipv4_ev_watcher.start (ipv4_fd, POLLIN); |
104 | } |
110 | } |
105 | |
111 | |
106 | udpv4_fd = -1; |
112 | udpv4_fd = -1; |
107 | |
113 | |
… | |
… | |
109 | { |
115 | { |
110 | udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
116 | udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
111 | |
117 | |
112 | if (udpv4_fd < 0) |
118 | if (udpv4_fd < 0) |
113 | return -1; |
119 | return -1; |
|
|
120 | |
|
|
121 | fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); |
114 | |
122 | |
115 | // standard daemon practise... |
123 | // standard daemon practise... |
116 | { |
124 | { |
117 | int oval = 1; |
125 | int oval = 1; |
118 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
126 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
119 | } |
127 | } |
120 | |
128 | |
121 | sockinfo si (THISNODE, PROT_UDPv4); |
129 | #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 |
130 | // this I really consider a linux bug. I am neither connected |
131 | // nor do I fragment myself. Linux still sets DF and doesn't |
131 | // nor do I fragment myself. Linux still sets DF and doesn't |
132 | // fragment for me sometimes. |
132 | // fragment for me sometimes. |
133 | { |
133 | { |
134 | int oval = IP_PMTUDISC_DONT; |
134 | int oval = IP_PMTUDISC_DONT; |
135 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
135 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
136 | } |
136 | } |
137 | #endif |
137 | #endif |
138 | |
138 | |
|
|
139 | sockinfo si (THISNODE, PROT_UDPv4); |
|
|
140 | |
|
|
141 | if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
142 | { |
|
|
143 | slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); |
|
|
144 | exit (1); |
|
|
145 | } |
|
|
146 | |
139 | udpv4_ev_watcher.start (udpv4_fd, POLLIN); |
147 | udpv4_ev_watcher.start (udpv4_fd, POLLIN); |
140 | } |
148 | } |
|
|
149 | |
|
|
150 | icmpv4_fd = -1; |
|
|
151 | |
|
|
152 | #if ENABLE_ICMP |
|
|
153 | if (THISNODE->protocols & PROT_ICMPv4) |
|
|
154 | { |
|
|
155 | icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); |
|
|
156 | |
|
|
157 | if (icmpv4_fd < 0) |
|
|
158 | return -1; |
|
|
159 | |
|
|
160 | fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
161 | |
|
|
162 | #ifdef ICMP_FILTER |
|
|
163 | { |
|
|
164 | icmp_filter oval; |
|
|
165 | oval.data = 0xffffffff; |
|
|
166 | if (::conf.icmp_type < 32) |
|
|
167 | oval.data &= ~(1 << ::conf.icmp_type); |
|
|
168 | |
|
|
169 | setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
|
|
170 | } |
|
|
171 | #endif |
|
|
172 | |
|
|
173 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
|
|
174 | // this I really consider a linux bug. I am neither connected |
|
|
175 | // nor do I fragment myself. Linux still sets DF and doesn't |
|
|
176 | // fragment for me sometimes. |
|
|
177 | { |
|
|
178 | int oval = IP_PMTUDISC_DONT; |
|
|
179 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
|
|
180 | } |
|
|
181 | #endif |
|
|
182 | |
|
|
183 | sockinfo si (THISNODE, PROT_ICMPv4); |
|
|
184 | |
|
|
185 | if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
186 | { |
|
|
187 | slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); |
|
|
188 | exit (1); |
|
|
189 | } |
|
|
190 | |
|
|
191 | icmpv4_ev_watcher.start (icmpv4_fd, POLLIN); |
|
|
192 | } |
|
|
193 | #endif |
141 | |
194 | |
142 | tcpv4_fd = -1; |
195 | tcpv4_fd = -1; |
143 | |
196 | |
144 | #if ENABLE_TCP |
197 | #if ENABLE_TCP |
145 | if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port) |
198 | if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port) |
146 | { |
199 | { |
147 | tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
200 | tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
148 | |
201 | |
149 | if (tcpv4_fd < 0) |
202 | if (tcpv4_fd < 0) |
150 | return -1; |
203 | return -1; |
|
|
204 | |
|
|
205 | fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); |
151 | |
206 | |
152 | // standard daemon practise... |
207 | // standard daemon practise... |
153 | { |
208 | { |
154 | int oval = 1; |
209 | int oval = 1; |
155 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
210 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
… | |
… | |
204 | #if ENABLE_TCP |
259 | #if ENABLE_TCP |
205 | case PROT_TCPv4: |
260 | case PROT_TCPv4: |
206 | return send_tcpv4_packet (pkt, si, tos); |
261 | return send_tcpv4_packet (pkt, si, tos); |
207 | #endif |
262 | #endif |
208 | |
263 | |
|
|
264 | #if ENABLE_ICMP |
|
|
265 | case PROT_ICMPv4: |
|
|
266 | return send_icmpv4_packet (pkt, si, tos); |
|
|
267 | #endif |
|
|
268 | |
209 | default: |
269 | default: |
210 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); |
270 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); |
211 | return false; |
271 | return false; |
212 | } |
272 | } |
213 | } |
273 | } |
214 | |
274 | |
215 | bool |
275 | bool |
216 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
276 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
217 | { |
277 | { |
|
|
278 | #if defined(SOL_IP) && defined(IP_TOS) |
218 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
279 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
280 | #endif |
219 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
281 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
220 | |
282 | |
221 | return true; |
283 | return true; |
222 | } |
284 | } |
|
|
285 | |
|
|
286 | static u16 |
|
|
287 | ipv4_checksum (u16 *data, unsigned int len) |
|
|
288 | { |
|
|
289 | // use 32 bit accumulator and fold back carry bits at the end |
|
|
290 | u32 sum = 0; |
|
|
291 | |
|
|
292 | while (len > 1) |
|
|
293 | { |
|
|
294 | sum += *data++; |
|
|
295 | len -= 2; |
|
|
296 | } |
|
|
297 | |
|
|
298 | // odd byte left? |
|
|
299 | if (len) |
|
|
300 | sum += *(u8 *)data; |
|
|
301 | |
|
|
302 | // add back carry bits |
|
|
303 | sum = (sum >> 16) + (sum & 0xffff); // lo += hi |
|
|
304 | sum += (sum >> 16); // carry |
|
|
305 | |
|
|
306 | return ~sum; |
|
|
307 | } |
|
|
308 | |
|
|
309 | #if ENABLE_ICMP |
|
|
310 | bool |
|
|
311 | vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
|
|
312 | { |
|
|
313 | #if defined(SOL_IP) && defined(IP_TOS) |
|
|
314 | setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
315 | #endif |
|
|
316 | |
|
|
317 | pkt->unshift_hdr (4); |
|
|
318 | |
|
|
319 | icmp_header *hdr = (icmp_header *)&((*pkt)[0]); |
|
|
320 | hdr->type = ::conf.icmp_type; |
|
|
321 | hdr->code = 255; |
|
|
322 | hdr->checksum = 0; |
|
|
323 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
|
|
324 | |
|
|
325 | sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
|
|
326 | |
|
|
327 | return true; |
|
|
328 | } |
|
|
329 | #endif |
223 | |
330 | |
224 | bool |
331 | bool |
225 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
332 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
226 | { |
333 | { |
|
|
334 | #if defined(SOL_IP) && defined(IP_TOS) |
227 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
335 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
336 | #endif |
228 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
337 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
229 | |
338 | |
230 | return true; |
339 | return true; |
|
|
340 | } |
|
|
341 | |
|
|
342 | void |
|
|
343 | vpn::inject_data_packet (tap_packet *pkt, int dst) |
|
|
344 | { |
|
|
345 | if (dst) |
|
|
346 | { |
|
|
347 | // unicast |
|
|
348 | if (dst != THISNODE->id) |
|
|
349 | conns[dst - 1]->inject_data_packet (pkt); |
|
|
350 | } |
|
|
351 | else |
|
|
352 | { |
|
|
353 | // broadcast, this is ugly, but due to the security policy |
|
|
354 | // we have to connect to all hosts... |
|
|
355 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
|
|
356 | if ((*c)->conf != THISNODE) |
|
|
357 | (*c)->inject_data_packet (pkt, true); |
|
|
358 | } |
231 | } |
359 | } |
232 | |
360 | |
233 | void |
361 | void |
234 | vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
362 | vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
235 | { |
363 | { |
… | |
… | |
249 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
377 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
250 | else |
378 | else |
251 | { |
379 | { |
252 | connection *c = conns[src - 1]; |
380 | connection *c = conns[src - 1]; |
253 | |
381 | |
254 | if (dst == 0 && !THISNODE->routerprio) |
382 | if (dst == 0) |
255 | slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), |
383 | slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), |
256 | c->conf->nodename, (const char *)rsi); |
384 | c->conf->nodename, (const char *)rsi); |
257 | else if (dst != 0 && dst != THISNODE->id) |
385 | else if (dst != THISNODE->id) |
258 | { |
386 | { |
259 | if (THISNODE->routerprio) |
387 | if (THISNODE->routerprio) |
260 | // the tos setting gets lost here. who cares. |
388 | // the tos setting gets lost here. who cares. |
261 | conns[dst - 1]->inject_vpn_packet (pkt); |
389 | conns[dst - 1]->inject_vpn_packet (pkt); |
262 | else |
390 | else |
… | |
… | |
269 | c->recv_vpn_packet (pkt, rsi); |
397 | c->recv_vpn_packet (pkt, rsi); |
270 | } |
398 | } |
271 | } |
399 | } |
272 | |
400 | |
273 | void |
401 | void |
274 | vpn::udpv4_ev (io_watcher &w, short revents) |
402 | vpn::ipv4_ev (io_watcher &w, short revents) |
275 | { |
403 | { |
276 | if (revents & (POLLIN | POLLERR)) |
404 | if (revents & (POLLIN | POLLERR)) |
277 | { |
405 | { |
278 | vpn_packet *pkt = new vpn_packet; |
406 | vpn_packet *pkt = new vpn_packet; |
279 | struct sockaddr_in sa; |
407 | struct sockaddr_in sa; |
280 | socklen_t sa_len = sizeof (sa); |
408 | socklen_t sa_len = sizeof (sa); |
281 | int len; |
409 | int len; |
282 | |
410 | |
283 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
411 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
284 | |
412 | |
285 | sockinfo si(sa, PROT_UDPv4); |
413 | sockinfo si(sa, PROT_IPv4); |
286 | |
414 | |
287 | if (len > 0) |
415 | if (len > 0) |
288 | { |
416 | { |
289 | pkt->len = len; |
417 | pkt->len = len; |
290 | |
418 | |
|
|
419 | // raw sockets deliver the ipv4, but don't expect it on sends |
|
|
420 | // this is slow, but... |
|
|
421 | pkt->skip_hdr (IP_OVERHEAD); |
|
|
422 | |
291 | recv_vpn_packet (pkt, si); |
423 | recv_vpn_packet (pkt, si); |
292 | } |
424 | } |
293 | else |
425 | else |
294 | { |
426 | { |
295 | // probably ECONNRESET or somesuch |
427 | // probably ECONNRESET or somesuch |
296 | slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); |
428 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
297 | } |
429 | } |
298 | |
430 | |
299 | delete pkt; |
431 | delete pkt; |
300 | } |
432 | } |
301 | else if (revents & POLLHUP) |
433 | else if (revents & POLLHUP) |
302 | { |
434 | { |
303 | // this cannot ;) happen on udp sockets |
435 | // this cannot ;) happen on udp sockets |
304 | slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating.")); |
436 | slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); |
305 | exit (1); |
437 | exit (1); |
306 | } |
438 | } |
307 | else |
439 | else |
308 | { |
440 | { |
309 | slog (L_ERR, |
441 | slog (L_ERR, |
… | |
… | |
311 | revents); |
443 | revents); |
312 | exit (1); |
444 | exit (1); |
313 | } |
445 | } |
314 | } |
446 | } |
315 | |
447 | |
|
|
448 | #if ENABLE_ICMP |
316 | void |
449 | void |
317 | vpn::ipv4_ev (io_watcher &w, short revents) |
450 | vpn::icmpv4_ev (io_watcher &w, short revents) |
318 | { |
451 | { |
319 | if (revents & (POLLIN | POLLERR)) |
452 | if (revents & (POLLIN | POLLERR)) |
320 | { |
453 | { |
321 | vpn_packet *pkt = new vpn_packet; |
454 | vpn_packet *pkt = new vpn_packet; |
322 | struct sockaddr_in sa; |
455 | struct sockaddr_in sa; |
323 | socklen_t sa_len = sizeof (sa); |
456 | socklen_t sa_len = sizeof (sa); |
324 | int len; |
457 | int len; |
325 | |
458 | |
326 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
459 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
327 | |
460 | |
328 | sockinfo si(sa, PROT_IPv4); |
461 | sockinfo si(sa, PROT_ICMPv4); |
329 | |
462 | |
330 | if (len > 0) |
463 | if (len > 0) |
331 | { |
464 | { |
332 | pkt->len = len; |
465 | pkt->len = len; |
333 | |
466 | |
|
|
467 | icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]); |
|
|
468 | |
|
|
469 | if (hdr->type == ::conf.icmp_type |
|
|
470 | && hdr->code == 255) |
|
|
471 | { |
334 | // raw sockets deliver the ipv4, but don't expect it on sends |
472 | // raw sockets deliver the ipv4, but don't expect it on sends |
335 | // this is slow, but... |
473 | // this is slow, but... |
336 | pkt->skip_hdr (IP_OVERHEAD); |
474 | pkt->skip_hdr (ICMP_OVERHEAD); |
337 | |
475 | |
338 | recv_vpn_packet (pkt, si); |
476 | recv_vpn_packet (pkt, si); |
|
|
477 | } |
339 | } |
478 | } |
340 | else |
479 | else |
341 | { |
480 | { |
342 | // probably ECONNRESET or somesuch |
481 | // probably ECONNRESET or somesuch |
343 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
482 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
… | |
… | |
346 | delete pkt; |
485 | delete pkt; |
347 | } |
486 | } |
348 | else if (revents & POLLHUP) |
487 | else if (revents & POLLHUP) |
349 | { |
488 | { |
350 | // this cannot ;) happen on udp sockets |
489 | // this cannot ;) happen on udp sockets |
351 | slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); |
490 | slog (L_ERR, _("FATAL: POLLHUP on icmpv4 fd, terminating.")); |
352 | exit (1); |
491 | exit (1); |
353 | } |
492 | } |
354 | else |
493 | else |
355 | { |
494 | { |
356 | slog (L_ERR, |
495 | slog (L_ERR, |
357 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
496 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
358 | revents); |
497 | revents); |
359 | exit (1); |
498 | exit (1); |
360 | } |
499 | } |
361 | } |
500 | } |
|
|
501 | #endif |
|
|
502 | |
|
|
503 | void |
|
|
504 | vpn::udpv4_ev (io_watcher &w, short revents) |
|
|
505 | { |
|
|
506 | if (revents & (POLLIN | POLLERR)) |
|
|
507 | { |
|
|
508 | vpn_packet *pkt = new vpn_packet; |
|
|
509 | struct sockaddr_in sa; |
|
|
510 | socklen_t sa_len = sizeof (sa); |
|
|
511 | int len; |
|
|
512 | |
|
|
513 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
|
|
514 | |
|
|
515 | sockinfo si(sa, PROT_UDPv4); |
|
|
516 | |
|
|
517 | if (len > 0) |
|
|
518 | { |
|
|
519 | pkt->len = len; |
|
|
520 | |
|
|
521 | recv_vpn_packet (pkt, si); |
|
|
522 | } |
|
|
523 | else |
|
|
524 | { |
|
|
525 | // probably ECONNRESET or somesuch |
|
|
526 | slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); |
|
|
527 | } |
|
|
528 | |
|
|
529 | delete pkt; |
|
|
530 | } |
|
|
531 | else if (revents & POLLHUP) |
|
|
532 | { |
|
|
533 | // this cannot ;) happen on udp sockets |
|
|
534 | slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating.")); |
|
|
535 | exit (1); |
|
|
536 | } |
|
|
537 | else |
|
|
538 | { |
|
|
539 | slog (L_ERR, |
|
|
540 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
|
|
541 | revents); |
|
|
542 | exit (1); |
|
|
543 | } |
|
|
544 | } |
362 | |
545 | |
363 | void |
546 | void |
364 | vpn::tap_ev (io_watcher &w, short revents) |
547 | vpn::tap_ev (io_watcher &w, short revents) |
365 | { |
548 | { |
366 | if (revents & POLLIN) |
549 | if (revents & POLLIN) |
… | |
… | |
368 | /* process data */ |
551 | /* process data */ |
369 | tap_packet *pkt; |
552 | tap_packet *pkt; |
370 | |
553 | |
371 | pkt = tap->recv (); |
554 | pkt = tap->recv (); |
372 | |
555 | |
|
|
556 | if (!pkt) |
|
|
557 | return; |
|
|
558 | |
|
|
559 | if (pkt->len > 14) |
|
|
560 | { |
373 | int dst = mac2id (pkt->dst); |
561 | int dst = mac2id (pkt->dst); |
374 | int src = mac2id (pkt->src); |
562 | int src = mac2id (pkt->src); |
375 | |
563 | |
376 | if (src != THISNODE->id) |
564 | if (src != THISNODE->id) |
377 | { |
|
|
378 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating.")); |
|
|
379 | exit (1); |
|
|
380 | } |
|
|
381 | |
|
|
382 | if (dst == THISNODE->id) |
|
|
383 | { |
|
|
384 | slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating.")); |
|
|
385 | exit (1); |
|
|
386 | } |
|
|
387 | |
|
|
388 | if (dst > conns.size ()) |
|
|
389 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
|
|
390 | else |
|
|
391 | { |
|
|
392 | if (dst) |
|
|
393 | { |
565 | { |
394 | // unicast |
566 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting.")); |
395 | if (dst != THISNODE->id) |
567 | exit (1); |
396 | conns[dst - 1]->inject_data_packet (pkt); |
|
|
397 | } |
568 | } |
|
|
569 | |
|
|
570 | if (dst == THISNODE->id) |
|
|
571 | { |
|
|
572 | slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting.")); |
|
|
573 | exit (1); |
|
|
574 | } |
|
|
575 | |
|
|
576 | if (dst > conns.size ()) |
|
|
577 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
398 | else |
578 | else |
399 | { |
|
|
400 | // broadcast, first check router, then self, then english |
|
|
401 | connection *router = find_router (); |
|
|
402 | |
|
|
403 | if (router) |
|
|
404 | router->inject_data_packet (pkt, true); |
579 | inject_data_packet (pkt, dst); |
405 | else |
|
|
406 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
|
|
407 | if ((*c)->conf != THISNODE) |
|
|
408 | (*c)->inject_data_packet (pkt); |
|
|
409 | } |
|
|
410 | } |
580 | } |
411 | |
581 | |
412 | delete pkt; |
582 | delete pkt; |
413 | } |
583 | } |
414 | else if (revents & (POLLHUP | POLLERR)) |
584 | else if (revents & (POLLHUP | POLLERR)) |
… | |
… | |
480 | } |
650 | } |
481 | } |
651 | } |
482 | |
652 | |
483 | connection *vpn::find_router () |
653 | connection *vpn::find_router () |
484 | { |
654 | { |
485 | u32 prio = 0; |
655 | u32 prio = 1; |
486 | connection *router = 0; |
656 | connection *router = 0; |
487 | |
657 | |
488 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
658 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
489 | { |
659 | { |
490 | connection *c = *i; |
660 | connection *c = *i; |
491 | |
661 | |
492 | if (c->conf->routerprio > prio |
662 | if (c->conf->routerprio > prio |
493 | && c->connectmode == conf_node::C_ALWAYS |
663 | && 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) |
664 | && c->ictx && c->octx |
|
|
665 | && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode |
496 | { |
666 | { |
497 | prio = c->conf->routerprio; |
667 | prio = c->conf->routerprio; |
498 | router = c; |
668 | router = c; |
499 | } |
669 | } |
500 | } |
670 | } |
… | |
… | |
509 | if (c) |
679 | if (c) |
510 | c->send_connect_request (id); |
680 | c->send_connect_request (id); |
511 | else |
681 | else |
512 | // no router found, aggressively connect to all routers |
682 | // no router found, aggressively connect to all routers |
513 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
683 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
514 | if ((*i)->conf->routerprio) |
684 | if ((*i)->conf->routerprio && (*i)->conf != THISNODE) |
515 | (*i)->establish_connection (); |
685 | (*i)->establish_connection (); |
516 | } |
686 | } |
517 | |
687 | |
518 | void |
688 | void |
519 | connection::dump_status () |
689 | connection::dump_status () |
… | |
… | |
537 | |
707 | |
538 | slog (L_NOTICE, _("END status dump")); |
708 | slog (L_NOTICE, _("END status dump")); |
539 | } |
709 | } |
540 | |
710 | |
541 | vpn::vpn (void) |
711 | vpn::vpn (void) |
542 | : event(this, &vpn::event_cb) |
712 | : event (this, &vpn::event_cb) |
543 | , udpv4_ev_watcher(this, &vpn::udpv4_ev) |
713 | , udpv4_ev_watcher (this, &vpn::udpv4_ev) |
544 | , ipv4_ev_watcher (this, &vpn::ipv4_ev) |
714 | , ipv4_ev_watcher (this, &vpn::ipv4_ev) |
545 | , tap_ev_watcher (this, &vpn::tap_ev) |
|
|
546 | #if ENABLE_TCP |
715 | #if ENABLE_TCP |
547 | , tcpv4_ev_watcher(this, &vpn::tcpv4_ev) |
716 | , tcpv4_ev_watcher (this, &vpn::tcpv4_ev) |
548 | #endif |
717 | #endif |
|
|
718 | #if ENABLE_ICMP |
|
|
719 | , icmpv4_ev_watcher(this, &vpn::icmpv4_ev) |
|
|
720 | #endif |
|
|
721 | , tap_ev_watcher (this, &vpn::tap_ev) |
549 | { |
722 | { |
550 | } |
723 | } |
551 | |
724 | |
552 | vpn::~vpn () |
725 | vpn::~vpn () |
553 | { |
726 | { |