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