… | |
… | |
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); |
… | |
… | |
185 | |
240 | |
186 | return 0; |
241 | return 0; |
187 | } |
242 | } |
188 | |
243 | |
189 | // send a vpn packet out to other hosts |
244 | // send a vpn packet out to other hosts |
190 | void |
245 | bool |
191 | vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
246 | vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
192 | { |
247 | { |
193 | switch (si.prot) |
248 | switch (si.prot) |
194 | { |
249 | { |
195 | case PROT_IPv4: |
250 | case PROT_IPv4: |
196 | send_ipv4_packet (pkt, si, tos); |
251 | return send_ipv4_packet (pkt, si, tos); |
197 | break; |
|
|
198 | |
252 | |
199 | case PROT_UDPv4: |
253 | case PROT_UDPv4: |
200 | send_udpv4_packet (pkt, si, tos); |
254 | return send_udpv4_packet (pkt, si, tos); |
201 | break; |
|
|
202 | |
255 | |
203 | #if ENABLE_TCP |
256 | #if ENABLE_TCP |
204 | case PROT_TCPv4: |
257 | case PROT_TCPv4: |
205 | send_tcpv4_packet (pkt, si, tos); |
258 | return send_tcpv4_packet (pkt, si, tos); |
206 | break; |
259 | #endif |
|
|
260 | |
|
|
261 | #if ENABLE_ICMP |
|
|
262 | case PROT_ICMPv4: |
|
|
263 | return send_icmpv4_packet (pkt, si, tos); |
207 | #endif |
264 | #endif |
208 | |
265 | |
209 | default: |
266 | default: |
210 | 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); |
211 | abort (); |
268 | return false; |
212 | } |
269 | } |
213 | } |
270 | } |
214 | |
271 | |
215 | void |
272 | bool |
216 | 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) |
217 | { |
274 | { |
|
|
275 | #if defined(SOL_IP) && defined(IP_TOS) |
218 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
276 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
277 | #endif |
219 | 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 ()); |
220 | } |
|
|
221 | |
279 | |
222 | void |
280 | return true; |
|
|
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 |
|
|
327 | |
|
|
328 | bool |
223 | 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) |
224 | { |
330 | { |
|
|
331 | #if defined(SOL_IP) && defined(IP_TOS) |
225 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
332 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
333 | #endif |
226 | 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 ()); |
|
|
335 | |
|
|
336 | return true; |
227 | } |
337 | } |
228 | |
338 | |
229 | void |
339 | void |
230 | vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
340 | vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
231 | { |
341 | { |
… | |
… | |
238 | if (src == 0 || src > conns.size () |
348 | if (src == 0 || src > conns.size () |
239 | || dst > conns.size () |
349 | || dst > conns.size () |
240 | || pkt->typ () >= vpn_packet::PT_MAX) |
350 | || pkt->typ () >= vpn_packet::PT_MAX) |
241 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), |
351 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), |
242 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
352 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
|
|
353 | else if (dst > conns.size ()) |
|
|
354 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), |
|
|
355 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
243 | else |
356 | else |
244 | { |
357 | { |
245 | connection *c = conns[src - 1]; |
358 | connection *c = conns[src - 1]; |
246 | |
359 | |
247 | if (dst == 0 && !THISNODE->routerprio) |
360 | if (dst == 0) |
248 | slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), |
361 | slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), |
249 | c->conf->nodename, (const char *)rsi); |
362 | c->conf->nodename, (const char *)rsi); |
250 | else if (dst != 0 && dst != THISNODE->id) |
363 | else if (dst != 0 && dst != THISNODE->id) |
251 | // FORWARDING NEEDED ;) |
364 | { |
|
|
365 | if (THISNODE->routerprio) |
|
|
366 | // the tos setting gets lost here. who cares. |
|
|
367 | conns[dst - 1]->inject_vpn_packet (pkt); |
|
|
368 | else |
252 | slog (L_WARN, |
369 | slog (L_WARN, |
253 | _("received frame for node %d ('%s') from %s, but this is node %d ('%s')"), |
370 | _("%s(%s): forwarding request (=> %s), but we are no router"), |
|
|
371 | c->conf->nodename, (const char *)rsi, |
254 | dst, conns[dst - 1]->conf->nodename, |
372 | conns[dst - 1]->conf->nodename); |
255 | (const char *)rsi, |
373 | } |
256 | THISNODE->id, THISNODE->nodename); |
|
|
257 | else |
374 | else |
258 | c->recv_vpn_packet (pkt, rsi); |
375 | c->recv_vpn_packet (pkt, rsi); |
259 | } |
376 | } |
260 | } |
377 | } |
261 | |
378 | |
262 | void |
379 | void |
263 | vpn::udpv4_ev (io_watcher &w, short revents) |
380 | vpn::ipv4_ev (io_watcher &w, short revents) |
264 | { |
381 | { |
265 | if (revents & (POLLIN | POLLERR)) |
382 | if (revents & (POLLIN | POLLERR)) |
266 | { |
383 | { |
267 | vpn_packet *pkt = new vpn_packet; |
384 | vpn_packet *pkt = new vpn_packet; |
268 | struct sockaddr_in sa; |
385 | struct sockaddr_in sa; |
269 | socklen_t sa_len = sizeof (sa); |
386 | socklen_t sa_len = sizeof (sa); |
270 | int len; |
387 | int len; |
271 | |
388 | |
272 | 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); |
273 | |
390 | |
274 | sockinfo si(sa, PROT_UDPv4); |
391 | sockinfo si(sa, PROT_IPv4); |
275 | |
392 | |
276 | if (len > 0) |
393 | if (len > 0) |
277 | { |
394 | { |
278 | pkt->len = len; |
395 | pkt->len = len; |
279 | |
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 | |
280 | recv_vpn_packet (pkt, si); |
401 | recv_vpn_packet (pkt, si); |
281 | } |
402 | } |
282 | else |
403 | else |
283 | { |
404 | { |
284 | // probably ECONNRESET or somesuch |
405 | // probably ECONNRESET or somesuch |
285 | 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)); |
286 | } |
407 | } |
287 | |
408 | |
288 | delete pkt; |
409 | delete pkt; |
289 | } |
410 | } |
290 | else if (revents & POLLHUP) |
411 | else if (revents & POLLHUP) |
291 | { |
412 | { |
292 | // this cannot ;) happen on udp sockets |
413 | // this cannot ;) happen on udp sockets |
293 | slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating.")); |
414 | slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); |
294 | exit (1); |
415 | exit (1); |
295 | } |
416 | } |
296 | else |
417 | else |
297 | { |
418 | { |
298 | slog (L_ERR, |
419 | slog (L_ERR, |
… | |
… | |
300 | revents); |
421 | revents); |
301 | exit (1); |
422 | exit (1); |
302 | } |
423 | } |
303 | } |
424 | } |
304 | |
425 | |
|
|
426 | #if ENABLE_ICMP |
305 | void |
427 | void |
306 | vpn::ipv4_ev (io_watcher &w, short revents) |
428 | vpn::icmpv4_ev (io_watcher &w, short revents) |
307 | { |
429 | { |
308 | if (revents & (POLLIN | POLLERR)) |
430 | if (revents & (POLLIN | POLLERR)) |
309 | { |
431 | { |
310 | vpn_packet *pkt = new vpn_packet; |
432 | vpn_packet *pkt = new vpn_packet; |
311 | struct sockaddr_in sa; |
433 | struct sockaddr_in sa; |
312 | socklen_t sa_len = sizeof (sa); |
434 | socklen_t sa_len = sizeof (sa); |
313 | int len; |
435 | int len; |
314 | |
436 | |
315 | 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); |
316 | |
438 | |
317 | sockinfo si(sa, PROT_IPv4); |
439 | sockinfo si(sa, PROT_ICMPv4); |
318 | |
440 | |
319 | if (len > 0) |
441 | if (len > 0) |
320 | { |
442 | { |
321 | pkt->len = len; |
443 | pkt->len = len; |
322 | |
444 | |
|
|
445 | icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]); |
|
|
446 | |
|
|
447 | if (hdr->type == ::conf.icmp_type |
|
|
448 | && hdr->code == 255) |
|
|
449 | { |
323 | // 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 |
324 | // this is slow, but... |
451 | // this is slow, but... |
325 | pkt->skip_hdr (IP_OVERHEAD); |
452 | pkt->skip_hdr (ICMP_OVERHEAD); |
326 | |
453 | |
327 | recv_vpn_packet (pkt, si); |
454 | recv_vpn_packet (pkt, si); |
|
|
455 | } |
328 | } |
456 | } |
329 | else |
457 | else |
330 | { |
458 | { |
331 | // probably ECONNRESET or somesuch |
459 | // probably ECONNRESET or somesuch |
332 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
460 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
… | |
… | |
335 | delete pkt; |
463 | delete pkt; |
336 | } |
464 | } |
337 | else if (revents & POLLHUP) |
465 | else if (revents & POLLHUP) |
338 | { |
466 | { |
339 | // this cannot ;) happen on udp sockets |
467 | // this cannot ;) happen on udp sockets |
340 | slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); |
468 | slog (L_ERR, _("FATAL: POLLHUP on icmpv4 fd, terminating.")); |
341 | exit (1); |
469 | exit (1); |
342 | } |
470 | } |
343 | else |
471 | else |
344 | { |
472 | { |
345 | slog (L_ERR, |
473 | slog (L_ERR, |
346 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
474 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
347 | revents); |
475 | revents); |
348 | exit (1); |
476 | exit (1); |
349 | } |
477 | } |
350 | } |
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 | } |
351 | |
523 | |
352 | void |
524 | void |
353 | vpn::tap_ev (io_watcher &w, short revents) |
525 | vpn::tap_ev (io_watcher &w, short revents) |
354 | { |
526 | { |
355 | if (revents & POLLIN) |
527 | if (revents & POLLIN) |
… | |
… | |
362 | int dst = mac2id (pkt->dst); |
534 | int dst = mac2id (pkt->dst); |
363 | int src = mac2id (pkt->src); |
535 | int src = mac2id (pkt->src); |
364 | |
536 | |
365 | if (src != THISNODE->id) |
537 | if (src != THISNODE->id) |
366 | { |
538 | { |
367 | 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.")); |
368 | exit (1); |
540 | exit (1); |
369 | } |
541 | } |
370 | |
542 | |
371 | if (dst == THISNODE->id) |
543 | if (dst == THISNODE->id) |
372 | { |
544 | { |
373 | 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.")); |
374 | exit (1); |
546 | exit (1); |
375 | } |
547 | } |
376 | |
548 | |
377 | if (dst > conns.size ()) |
549 | if (dst > conns.size ()) |
378 | 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); |
… | |
… | |
384 | if (dst != THISNODE->id) |
556 | if (dst != THISNODE->id) |
385 | conns[dst - 1]->inject_data_packet (pkt); |
557 | conns[dst - 1]->inject_data_packet (pkt); |
386 | } |
558 | } |
387 | else |
559 | else |
388 | { |
560 | { |
389 | // broadcast, first check router, then self, then english |
561 | // broadcast, this is ugly, but due to the security policy |
390 | connection *router = find_router (); |
562 | // we have to connect to all hosts... |
391 | |
|
|
392 | if (router) |
|
|
393 | router->inject_data_packet (pkt, true); |
|
|
394 | else |
|
|
395 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
563 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
396 | if ((*c)->conf != THISNODE) |
564 | if ((*c)->conf != THISNODE) |
397 | (*c)->inject_data_packet (pkt); |
565 | (*c)->inject_data_packet (pkt); |
398 | } |
566 | } |
399 | } |
567 | } |
400 | |
568 | |
401 | delete pkt; |
569 | delete pkt; |
402 | } |
570 | } |
… | |
… | |
469 | } |
637 | } |
470 | } |
638 | } |
471 | |
639 | |
472 | connection *vpn::find_router () |
640 | connection *vpn::find_router () |
473 | { |
641 | { |
474 | u32 prio = 0; |
642 | u32 prio = 1; |
475 | connection *router = 0; |
643 | connection *router = 0; |
476 | |
644 | |
477 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
645 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
478 | { |
646 | { |
479 | connection *c = *i; |
647 | connection *c = *i; |
480 | |
648 | |
481 | if (c->conf->routerprio > prio |
649 | if (c->conf->routerprio > prio |
482 | && c->connectmode == conf_node::C_ALWAYS |
650 | && c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use |
483 | && c->conf != THISNODE |
|
|
484 | && c->ictx && c->octx) |
651 | && c->ictx && c->octx |
|
|
652 | && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode |
485 | { |
653 | { |
486 | prio = c->conf->routerprio; |
654 | prio = c->conf->routerprio; |
487 | router = c; |
655 | router = c; |
488 | } |
656 | } |
489 | } |
657 | } |
490 | |
658 | |
491 | return router; |
659 | return router; |
492 | } |
660 | } |
493 | |
661 | |
494 | void vpn::connect_request (int id) |
662 | void vpn::send_connect_request (int id) |
495 | { |
663 | { |
496 | connection *c = find_router (); |
664 | connection *c = find_router (); |
497 | |
665 | |
498 | if (c) |
666 | if (c) |
499 | c->connect_request (id); |
667 | c->send_connect_request (id); |
500 | //else // does not work, because all others must connect to the same router |
668 | else |
501 | // // no router found, aggressively connect to all routers |
669 | // no router found, aggressively connect to all routers |
502 | // for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
670 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
503 | // if ((*i)->conf->routerprio) |
671 | if ((*i)->conf->routerprio && (*i)->conf != THISNODE) |
504 | // (*i)->establish_connection (); |
672 | (*i)->establish_connection (); |
505 | } |
673 | } |
506 | |
674 | |
507 | void |
675 | void |
508 | connection::dump_status () |
676 | connection::dump_status () |
509 | { |
677 | { |
… | |
… | |
526 | |
694 | |
527 | slog (L_NOTICE, _("END status dump")); |
695 | slog (L_NOTICE, _("END status dump")); |
528 | } |
696 | } |
529 | |
697 | |
530 | vpn::vpn (void) |
698 | vpn::vpn (void) |
531 | : event(this, &vpn::event_cb) |
699 | : event (this, &vpn::event_cb) |
532 | , udpv4_ev_watcher(this, &vpn::udpv4_ev) |
700 | , udpv4_ev_watcher (this, &vpn::udpv4_ev) |
533 | , ipv4_ev_watcher (this, &vpn::ipv4_ev) |
701 | , ipv4_ev_watcher (this, &vpn::ipv4_ev) |
534 | , tap_ev_watcher (this, &vpn::tap_ev) |
|
|
535 | #if ENABLE_TCP |
702 | #if ENABLE_TCP |
536 | , tcpv4_ev_watcher(this, &vpn::tcpv4_ev) |
703 | , tcpv4_ev_watcher (this, &vpn::tcpv4_ev) |
537 | #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) |
538 | { |
709 | { |
539 | } |
710 | } |
540 | |
711 | |
541 | vpn::~vpn () |
712 | vpn::~vpn () |
542 | { |
713 | { |