… | |
… | |
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" |
42 | |
57 | |
43 | #if ENABLE_TCP |
58 | #if !defined(SOL_IP) && defined(IPPROTO_IP) |
44 | # include <map> |
59 | # define SOL_IP IPPROTO_IP |
45 | # include <unistd.h> |
|
|
46 | # include <fcntl.h> |
|
|
47 | # include <sys/poll.h> |
|
|
48 | #endif |
60 | #endif |
49 | |
61 | |
50 | ///////////////////////////////////////////////////////////////////////////// |
62 | ///////////////////////////////////////////////////////////////////////////// |
51 | |
63 | |
52 | const char *vpn::script_if_up () |
64 | const char *vpn::script_if_up () |
… | |
… | |
78 | } |
90 | } |
79 | |
91 | |
80 | int |
92 | int |
81 | vpn::setup () |
93 | vpn::setup () |
82 | { |
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 | |
83 | udpv4_fd = -1; |
127 | udpv4_fd = -1; |
84 | |
128 | |
85 | if (THISNODE->protocols & PROT_UDPv4) |
129 | if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) |
86 | { |
130 | { |
87 | udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
131 | udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
88 | |
132 | |
89 | if (udpv4_fd < 0) |
133 | if (udpv4_fd < 0) |
90 | return -1; |
134 | return -1; |
|
|
135 | |
|
|
136 | fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); |
91 | |
137 | |
92 | // standard daemon practise... |
138 | // standard daemon practise... |
93 | { |
139 | { |
94 | int oval = 1; |
140 | int oval = 1; |
95 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
141 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
96 | } |
142 | } |
97 | |
143 | |
98 | sockinfo si (THISNODE, PROT_UDPv4); |
144 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
99 | |
|
|
100 | if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
101 | { |
|
|
102 | slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); |
|
|
103 | exit (1); |
|
|
104 | } |
|
|
105 | |
|
|
106 | #ifdef IP_MTU_DISCOVER |
|
|
107 | // this I really consider a linux bug. I am neither connected |
145 | // this I really consider a linux bug. I am neither connected |
108 | // 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 |
109 | // fragment for me sometimes. |
147 | // fragment for me sometimes. |
110 | { |
148 | { |
111 | int oval = IP_PMTUDISC_DONT; |
149 | int oval = IP_PMTUDISC_DONT; |
112 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
150 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
113 | } |
151 | } |
114 | #endif |
152 | #endif |
115 | |
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 | |
116 | udpv4_ev_watcher.start (udpv4_fd, POLLIN); |
162 | udpv4_ev_watcher.start (udpv4_fd, POLLIN); |
117 | } |
163 | } |
118 | |
164 | |
119 | ipv4_fd = -1; |
165 | icmpv4_fd = -1; |
|
|
166 | |
|
|
167 | #if ENABLE_ICMP |
120 | if (THISNODE->protocols & PROT_IPv4) |
168 | if (THISNODE->protocols & PROT_ICMPv4) |
121 | { |
169 | { |
122 | ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); |
170 | icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); |
123 | |
171 | |
124 | if (ipv4_fd < 0) |
172 | if (icmpv4_fd < 0) |
125 | return -1; |
173 | return -1; |
126 | |
174 | |
127 | sockinfo si (THISNODE, PROT_IPv4); |
175 | fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); |
128 | |
176 | |
129 | if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
177 | #ifdef ICMP_FILTER |
130 | { |
178 | { |
131 | slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); |
179 | icmp_filter oval; |
132 | 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); |
133 | } |
185 | } |
|
|
186 | #endif |
134 | |
187 | |
135 | #ifdef IP_MTU_DISCOVER |
188 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
136 | // this I really consider a linux bug. I am neither connected |
189 | // this I really consider a linux bug. I am neither connected |
137 | // 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 |
138 | // fragment for me sometimes. |
191 | // fragment for me sometimes. |
139 | { |
192 | { |
140 | int oval = IP_PMTUDISC_DONT; |
193 | int oval = IP_PMTUDISC_DONT; |
141 | setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
194 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
142 | } |
195 | } |
143 | #endif |
196 | #endif |
144 | |
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 | |
145 | ipv4_ev_watcher.start (ipv4_fd, POLLIN); |
206 | icmpv4_ev_watcher.start (icmpv4_fd, POLLIN); |
146 | } |
207 | } |
|
|
208 | #endif |
|
|
209 | |
|
|
210 | tcpv4_fd = -1; |
147 | |
211 | |
148 | #if ENABLE_TCP |
212 | #if ENABLE_TCP |
149 | if (THISNODE->protocols & PROT_TCPv4) |
213 | if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port) |
150 | { |
214 | { |
151 | tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
215 | tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
152 | |
216 | |
153 | if (tcpv4_fd < 0) |
217 | if (tcpv4_fd < 0) |
154 | return -1; |
218 | return -1; |
|
|
219 | |
|
|
220 | fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); |
155 | |
221 | |
156 | // standard daemon practise... |
222 | // standard daemon practise... |
157 | { |
223 | { |
158 | int oval = 1; |
224 | int oval = 1; |
159 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
225 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
… | |
… | |
191 | reconnect_all (); |
257 | reconnect_all (); |
192 | |
258 | |
193 | return 0; |
259 | return 0; |
194 | } |
260 | } |
195 | |
261 | |
196 | void |
262 | // send a vpn packet out to other hosts |
|
|
263 | bool |
|
|
264 | vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
|
|
265 | { |
|
|
266 | switch (si.prot) |
|
|
267 | { |
|
|
268 | case PROT_IPv4: |
|
|
269 | return send_ipv4_packet (pkt, si, tos); |
|
|
270 | |
|
|
271 | case PROT_UDPv4: |
|
|
272 | return send_udpv4_packet (pkt, si, tos); |
|
|
273 | |
|
|
274 | #if ENABLE_TCP |
|
|
275 | case PROT_TCPv4: |
|
|
276 | return send_tcpv4_packet (pkt, si, tos); |
|
|
277 | #endif |
|
|
278 | |
|
|
279 | #if ENABLE_ICMP |
|
|
280 | case PROT_ICMPv4: |
|
|
281 | return send_icmpv4_packet (pkt, si, tos); |
|
|
282 | #endif |
|
|
283 | |
|
|
284 | default: |
|
|
285 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); |
|
|
286 | return false; |
|
|
287 | } |
|
|
288 | } |
|
|
289 | |
|
|
290 | bool |
197 | 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) |
198 | { |
292 | { |
|
|
293 | #if defined(SOL_IP) && defined(IP_TOS) |
199 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
294 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
295 | #endif |
200 | 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 ()); |
201 | } |
|
|
202 | |
297 | |
203 | 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 |
204 | 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) |
205 | { |
365 | { |
|
|
366 | #if defined(SOL_IP) && defined(IP_TOS) |
206 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
367 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
368 | #endif |
207 | 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; |
208 | } |
372 | } |
209 | |
373 | |
210 | void |
374 | void |
211 | vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
375 | vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
212 | { |
376 | { |
… | |
… | |
219 | if (src == 0 || src > conns.size () |
383 | if (src == 0 || src > conns.size () |
220 | || dst > conns.size () |
384 | || dst > conns.size () |
221 | || pkt->typ () >= vpn_packet::PT_MAX) |
385 | || pkt->typ () >= vpn_packet::PT_MAX) |
222 | 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)"), |
223 | (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 ()); |
224 | else |
391 | else |
225 | { |
392 | { |
226 | connection *c = conns[src - 1]; |
393 | connection *c = conns[src - 1]; |
227 | |
394 | |
228 | if (dst == 0 && !THISNODE->routerprio) |
395 | if (dst == 0) |
229 | slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), |
396 | slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), |
230 | c->conf->nodename, (const char *)rsi); |
397 | c->conf->nodename, (const char *)rsi); |
231 | else if (dst != 0 && dst != THISNODE->id) |
398 | else if (dst != 0 && dst != THISNODE->id) |
232 | // 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 |
233 | slog (L_WARN, |
404 | slog (L_WARN, |
234 | _("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, |
235 | dst, conns[dst - 1]->conf->nodename, |
407 | conns[dst - 1]->conf->nodename); |
236 | (const char *)rsi, |
408 | } |
237 | THISNODE->id, THISNODE->nodename); |
|
|
238 | else |
409 | else |
239 | c->recv_vpn_packet (pkt, rsi); |
410 | c->recv_vpn_packet (pkt, rsi); |
240 | } |
411 | } |
241 | } |
412 | } |
242 | |
413 | |
243 | void |
414 | void |
244 | vpn::udpv4_ev (io_watcher &w, short revents) |
415 | vpn::ipv4_ev (io_watcher &w, short revents) |
245 | { |
416 | { |
246 | if (revents & (POLLIN | POLLERR)) |
417 | if (revents & (POLLIN | POLLERR)) |
247 | { |
418 | { |
248 | vpn_packet *pkt = new vpn_packet; |
419 | vpn_packet *pkt = new vpn_packet; |
249 | struct sockaddr_in sa; |
420 | struct sockaddr_in sa; |
250 | socklen_t sa_len = sizeof (sa); |
421 | socklen_t sa_len = sizeof (sa); |
251 | int len; |
422 | int len; |
252 | |
423 | |
253 | len = recvfrom (w.p->fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
424 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
254 | |
425 | |
255 | sockinfo si(sa); |
426 | sockinfo si(sa, PROT_IPv4); |
256 | |
427 | |
257 | if (len > 0) |
428 | if (len > 0) |
258 | { |
429 | { |
259 | pkt->len = len; |
430 | pkt->len = len; |
|
|
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); |
260 | |
435 | |
261 | recv_vpn_packet (pkt, si); |
436 | recv_vpn_packet (pkt, si); |
262 | } |
437 | } |
263 | else |
438 | else |
264 | { |
439 | { |
… | |
… | |
269 | delete pkt; |
444 | delete pkt; |
270 | } |
445 | } |
271 | else if (revents & POLLHUP) |
446 | else if (revents & POLLHUP) |
272 | { |
447 | { |
273 | // this cannot ;) happen on udp sockets |
448 | // this cannot ;) happen on udp sockets |
274 | slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating.")); |
449 | slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); |
275 | exit (1); |
450 | exit (1); |
276 | } |
451 | } |
277 | else |
452 | else |
278 | { |
453 | { |
279 | slog (L_ERR, |
454 | slog (L_ERR, |
… | |
… | |
281 | revents); |
456 | revents); |
282 | exit (1); |
457 | exit (1); |
283 | } |
458 | } |
284 | } |
459 | } |
285 | |
460 | |
|
|
461 | #if ENABLE_ICMP |
286 | void |
462 | void |
287 | vpn::ipv4_ev (io_watcher &w, short revents) |
463 | vpn::icmpv4_ev (io_watcher &w, short revents) |
288 | { |
464 | { |
289 | if (revents & (POLLIN | POLLERR)) |
465 | if (revents & (POLLIN | POLLERR)) |
290 | { |
466 | { |
291 | vpn_packet *pkt = new vpn_packet; |
467 | vpn_packet *pkt = new vpn_packet; |
292 | struct sockaddr_in sa; |
468 | struct sockaddr_in sa; |
293 | socklen_t sa_len = sizeof (sa); |
469 | socklen_t sa_len = sizeof (sa); |
294 | int len; |
470 | int len; |
295 | |
471 | |
296 | len = recvfrom (w.p->fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
472 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
297 | |
473 | |
298 | sockinfo si(sa, PROT_IPv4); |
474 | sockinfo si(sa, PROT_ICMPv4); |
299 | |
475 | |
300 | if (len > 0) |
476 | if (len > 0) |
301 | { |
477 | { |
302 | pkt->len = len; |
478 | pkt->len = len; |
303 | |
479 | |
|
|
480 | icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]); |
|
|
481 | |
|
|
482 | if (hdr->type == ::conf.icmp_type |
|
|
483 | && hdr->code == 255) |
|
|
484 | { |
304 | // 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 |
305 | // this is slow, but... |
486 | // this is slow, but... |
306 | pkt->skip_hdr (IP_OVERHEAD); |
487 | pkt->skip_hdr (ICMP_OVERHEAD); |
307 | |
488 | |
308 | recv_vpn_packet (pkt, si); |
489 | recv_vpn_packet (pkt, si); |
|
|
490 | } |
309 | } |
491 | } |
310 | else |
492 | else |
311 | { |
493 | { |
312 | // probably ECONNRESET or somesuch |
494 | // probably ECONNRESET or somesuch |
313 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
495 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
… | |
… | |
316 | delete pkt; |
498 | delete pkt; |
317 | } |
499 | } |
318 | else if (revents & POLLHUP) |
500 | else if (revents & POLLHUP) |
319 | { |
501 | { |
320 | // this cannot ;) happen on udp sockets |
502 | // this cannot ;) happen on udp sockets |
321 | slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); |
503 | slog (L_ERR, _("FATAL: POLLHUP on icmpv4 fd, terminating.")); |
322 | exit (1); |
504 | exit (1); |
323 | } |
505 | } |
324 | else |
506 | else |
325 | { |
507 | { |
326 | slog (L_ERR, |
508 | slog (L_ERR, |
327 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
509 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
328 | revents); |
510 | revents); |
329 | exit (1); |
511 | exit (1); |
330 | } |
512 | } |
331 | } |
513 | } |
|
|
514 | #endif |
332 | |
515 | |
333 | #if ENABLE_TCP |
516 | void |
334 | |
|
|
335 | struct tcp_connection; |
|
|
336 | |
|
|
337 | struct lt_sockinfo |
|
|
338 | { |
|
|
339 | bool operator()(const sockinfo *a, const sockinfo *b) const |
|
|
340 | { |
|
|
341 | return *a < *b; |
|
|
342 | } |
|
|
343 | }; |
|
|
344 | |
|
|
345 | struct tcp_si_map : public map<const sockinfo *, tcp_connection *, lt_sockinfo> { |
|
|
346 | void cleaner_cb (time_watcher &w); time_watcher cleaner; |
|
|
347 | |
|
|
348 | tcp_si_map () |
|
|
349 | : cleaner(this, &tcp_si_map::cleaner_cb) |
|
|
350 | { |
|
|
351 | cleaner.start (0); |
|
|
352 | } |
|
|
353 | } tcp_si; |
|
|
354 | |
|
|
355 | struct tcp_connection : io_watcher { |
|
|
356 | tstamp last_activity; |
|
|
357 | const sockinfo si; |
|
|
358 | vpn &v; |
|
|
359 | bool ok; |
|
|
360 | |
|
|
361 | vpn_packet *r_pkt; |
|
|
362 | u32 r_len, r_ofs; |
|
|
363 | |
|
|
364 | void tcpv4_ev (io_watcher &w, short revents); |
517 | vpn::udpv4_ev (io_watcher &w, short revents) |
365 | |
|
|
366 | operator tcp_si_map::value_type() |
|
|
367 | { |
|
|
368 | return tcp_si_map::value_type (&si, this); |
|
|
369 | } |
|
|
370 | |
|
|
371 | tcp_connection (int fd, const sockinfo &si_, vpn &v_) |
|
|
372 | : v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev) |
|
|
373 | { |
|
|
374 | last_activity = NOW; |
|
|
375 | ok = false; |
|
|
376 | r_pkt = 0; |
|
|
377 | start (fd, POLLOUT); |
|
|
378 | } |
|
|
379 | |
|
|
380 | ~tcp_connection () { if (p) close (p->fd); } |
|
|
381 | }; |
|
|
382 | |
|
|
383 | void tcp_si_map::cleaner_cb (time_watcher &w) |
|
|
384 | { |
518 | { |
385 | w.at = NOW + 600; |
|
|
386 | tstamp to = NOW - ::conf.keepalive - 30; |
|
|
387 | |
|
|
388 | for (iterator i = begin (); i != end(); ) |
|
|
389 | if (i->second->last_activity >= to) |
|
|
390 | ++i; |
|
|
391 | else |
|
|
392 | { |
|
|
393 | erase (i); |
|
|
394 | i = begin (); |
|
|
395 | } |
|
|
396 | } |
|
|
397 | |
|
|
398 | void |
|
|
399 | vpn::send_tcpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
|
|
400 | { |
|
|
401 | tcp_si_map::iterator info = tcp_si.find (&si); |
|
|
402 | |
|
|
403 | if (info == tcp_si.end ()) |
|
|
404 | { |
|
|
405 | // woaw, the first lost packet ;) |
|
|
406 | int fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
|
|
407 | |
|
|
408 | if (fd >= 0) |
|
|
409 | { |
|
|
410 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
411 | |
|
|
412 | if (connect (fd, si.sav4 (), si.salenv4 ()) >= 0 |
|
|
413 | || errno == EINPROGRESS) |
|
|
414 | { |
|
|
415 | tcp_connection *i = new tcp_connection (fd, si, *this); |
|
|
416 | |
|
|
417 | tcp_si.insert (*i); |
|
|
418 | } |
|
|
419 | else |
|
|
420 | close (fd); |
|
|
421 | } |
|
|
422 | } |
|
|
423 | else |
|
|
424 | { |
|
|
425 | tcp_connection *i = info->second; |
|
|
426 | |
|
|
427 | i->last_activity = NOW; |
|
|
428 | |
|
|
429 | if (i->ok) |
|
|
430 | { |
|
|
431 | setsockopt (i->p->fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
432 | |
|
|
433 | // we use none of the advantages of tcp |
|
|
434 | write (i->p->fd, (void *)pkt, pkt->len + sizeof (u32)) != pkt->len + sizeof (u32); |
|
|
435 | } |
|
|
436 | } |
|
|
437 | |
|
|
438 | #if 0 |
|
|
439 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
440 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
|
|
441 | #endif |
|
|
442 | } |
|
|
443 | |
|
|
444 | void |
|
|
445 | tcp_connection::tcpv4_ev (io_watcher &w, short revents) |
|
|
446 | { |
|
|
447 | last_activity = NOW; |
|
|
448 | |
|
|
449 | if (!ok) // just established? |
|
|
450 | { |
|
|
451 | ok = true; |
|
|
452 | set (POLLIN); |
|
|
453 | } |
|
|
454 | |
|
|
455 | if (revents & (POLLIN | POLLERR)) |
519 | if (revents & (POLLIN | POLLERR)) |
456 | { |
520 | { |
457 | u32 len; |
|
|
458 | |
|
|
459 | if (sizeof (len) == read (p->fd, &len, sizeof (len))) |
|
|
460 | { |
|
|
461 | vpn_packet *pkt = new vpn_packet; |
521 | vpn_packet *pkt = new vpn_packet; |
462 | |
|
|
463 | if (len == read (p->fd, &((*pkt)[0]), len)) |
|
|
464 | { |
|
|
465 | pkt->len = len; |
|
|
466 | |
|
|
467 | v.recv_vpn_packet (pkt, si); |
|
|
468 | return; |
|
|
469 | } |
|
|
470 | |
|
|
471 | delete pkt; |
|
|
472 | } |
|
|
473 | |
|
|
474 | tcp_si.erase (&si); |
|
|
475 | |
|
|
476 | set (0);//D |
|
|
477 | } |
|
|
478 | } |
|
|
479 | |
|
|
480 | void |
|
|
481 | vpn::tcpv4_ev (io_watcher &w, short revents) |
|
|
482 | { |
|
|
483 | if (revents & (POLLIN | POLLERR)) |
|
|
484 | { |
|
|
485 | struct sockaddr_in sa; |
522 | struct sockaddr_in sa; |
486 | socklen_t sa_len = sizeof (sa); |
523 | socklen_t sa_len = sizeof (sa); |
487 | int len; |
524 | int len; |
488 | |
525 | |
489 | int fd = accept (w.p->fd, (sockaddr *)&sa, &sa_len); |
526 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
490 | |
527 | |
491 | if (fd >= 0) |
|
|
492 | { |
|
|
493 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
494 | |
|
|
495 | sockinfo si(sa, PROT_TCPv4); |
528 | sockinfo si(sa, PROT_UDPv4); |
496 | tcp_connection *i = new tcp_connection (fd, si, *this); |
|
|
497 | |
529 | |
498 | slog (L_DEBUG, _("accepted tcp connection from %s\n"), (const char *)si);//D |
530 | if (len > 0) |
499 | |
|
|
500 | tcp_si.insert (*i); |
|
|
501 | } |
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) |
502 | } |
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 | } |
503 | } |
557 | } |
504 | |
|
|
505 | #endif |
|
|
506 | |
558 | |
507 | void |
559 | void |
508 | vpn::tap_ev (io_watcher &w, short revents) |
560 | vpn::tap_ev (io_watcher &w, short revents) |
509 | { |
561 | { |
510 | if (revents & POLLIN) |
562 | if (revents & POLLIN) |
… | |
… | |
517 | int dst = mac2id (pkt->dst); |
569 | int dst = mac2id (pkt->dst); |
518 | int src = mac2id (pkt->src); |
570 | int src = mac2id (pkt->src); |
519 | |
571 | |
520 | if (src != THISNODE->id) |
572 | if (src != THISNODE->id) |
521 | { |
573 | { |
522 | 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.")); |
523 | exit (1); |
575 | exit (1); |
524 | } |
576 | } |
525 | |
577 | |
526 | if (dst == THISNODE->id) |
578 | if (dst == THISNODE->id) |
527 | { |
579 | { |
528 | 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.")); |
529 | exit (1); |
581 | exit (1); |
530 | } |
582 | } |
531 | |
583 | |
532 | if (dst > conns.size ()) |
584 | if (dst > conns.size ()) |
533 | 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); |
… | |
… | |
539 | if (dst != THISNODE->id) |
591 | if (dst != THISNODE->id) |
540 | conns[dst - 1]->inject_data_packet (pkt); |
592 | conns[dst - 1]->inject_data_packet (pkt); |
541 | } |
593 | } |
542 | else |
594 | else |
543 | { |
595 | { |
544 | // broadcast, first check router, then self, then english |
596 | // broadcast, this is ugly, but due to the security policy |
545 | connection *router = find_router (); |
597 | // we have to connect to all hosts... |
546 | |
|
|
547 | if (router) |
|
|
548 | router->inject_data_packet (pkt, true); |
|
|
549 | else |
|
|
550 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
598 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
551 | if ((*c)->conf != THISNODE) |
599 | if ((*c)->conf != THISNODE) |
552 | (*c)->inject_data_packet (pkt); |
600 | (*c)->inject_data_packet (pkt); |
553 | } |
601 | } |
554 | } |
602 | } |
555 | |
603 | |
556 | delete pkt; |
604 | delete pkt; |
557 | } |
605 | } |
… | |
… | |
624 | } |
672 | } |
625 | } |
673 | } |
626 | |
674 | |
627 | connection *vpn::find_router () |
675 | connection *vpn::find_router () |
628 | { |
676 | { |
629 | u32 prio = 0; |
677 | u32 prio = 1; |
630 | connection *router = 0; |
678 | connection *router = 0; |
631 | |
679 | |
632 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
680 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
633 | { |
681 | { |
634 | connection *c = *i; |
682 | connection *c = *i; |
635 | |
683 | |
636 | if (c->conf->routerprio > prio |
684 | if (c->conf->routerprio > prio |
637 | && c->connectmode == conf_node::C_ALWAYS |
685 | && c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use |
638 | && c->conf != THISNODE |
|
|
639 | && c->ictx && c->octx) |
686 | && c->ictx && c->octx |
|
|
687 | && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode |
640 | { |
688 | { |
641 | prio = c->conf->routerprio; |
689 | prio = c->conf->routerprio; |
642 | router = c; |
690 | router = c; |
643 | } |
691 | } |
644 | } |
692 | } |
645 | |
693 | |
646 | return router; |
694 | return router; |
647 | } |
695 | } |
648 | |
696 | |
649 | void vpn::connect_request (int id) |
697 | void vpn::send_connect_request (int id) |
650 | { |
698 | { |
651 | connection *c = find_router (); |
699 | connection *c = find_router (); |
652 | |
700 | |
653 | if (c) |
701 | if (c) |
654 | c->connect_request (id); |
702 | c->send_connect_request (id); |
655 | //else // does not work, because all others must connect to the same router |
703 | else |
656 | // // no router found, aggressively connect to all routers |
704 | // no router found, aggressively connect to all routers |
657 | // for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
705 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
658 | // if ((*i)->conf->routerprio) |
706 | if ((*i)->conf->routerprio && (*i)->conf != THISNODE) |
659 | // (*i)->establish_connection (); |
707 | (*i)->establish_connection (); |
660 | } |
708 | } |
661 | |
709 | |
662 | void |
710 | void |
663 | connection::dump_status () |
711 | connection::dump_status () |
664 | { |
712 | { |
… | |
… | |
681 | |
729 | |
682 | slog (L_NOTICE, _("END status dump")); |
730 | slog (L_NOTICE, _("END status dump")); |
683 | } |
731 | } |
684 | |
732 | |
685 | vpn::vpn (void) |
733 | vpn::vpn (void) |
686 | : event(this, &vpn::event_cb) |
734 | : event (this, &vpn::event_cb) |
687 | , udpv4_ev_watcher(this, &vpn::udpv4_ev) |
735 | , udpv4_ev_watcher (this, &vpn::udpv4_ev) |
688 | , ipv4_ev_watcher (this, &vpn::ipv4_ev) |
736 | , ipv4_ev_watcher (this, &vpn::ipv4_ev) |
689 | , tap_ev_watcher (this, &vpn::tap_ev) |
|
|
690 | #if ENABLE_TCP |
737 | #if ENABLE_TCP |
691 | , tcpv4_ev_watcher(this, &vpn::tcpv4_ev) |
738 | , tcpv4_ev_watcher (this, &vpn::tcpv4_ev) |
692 | #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) |
693 | { |
744 | { |
694 | } |
745 | } |
695 | |
746 | |
696 | vpn::~vpn () |
747 | vpn::~vpn () |
697 | { |
748 | { |