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