1 | /* |
1 | /* |
2 | vpn.C -- handle the protocol, encryption, handshaking etc. |
2 | vpn.C -- handle the protocol, encryption, handshaking etc. |
3 | Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> |
3 | Copyright (C) 2003-2008,2010,2011,2013 Marc Lehmann <gvpe@schmorp.de> |
4 | |
4 | |
5 | This file is part of GVPE. |
5 | This file is part of GVPE. |
6 | |
6 | |
7 | GVPE is free software; you can redistribute it and/or modify it |
7 | GVPE is free software; you can redistribute it and/or modify it |
8 | under the terms of the GNU General Public License as published by the |
8 | under the terms of the GNU General Public License as published by the |
… | |
… | |
38 | #include <cstdlib> |
38 | #include <cstdlib> |
39 | |
39 | |
40 | #include <sys/types.h> |
40 | #include <sys/types.h> |
41 | #include <sys/socket.h> |
41 | #include <sys/socket.h> |
42 | #include <sys/wait.h> |
42 | #include <sys/wait.h> |
|
|
43 | #include <sys/stat.h> |
43 | #include <errno.h> |
44 | #include <errno.h> |
44 | #include <time.h> |
45 | #include <time.h> |
45 | #include <unistd.h> |
46 | #include <unistd.h> |
46 | #include <fcntl.h> |
47 | #include <fcntl.h> |
47 | #include <sys/socket.h> |
48 | #include <sys/socket.h> |
… | |
… | |
52 | |
53 | |
53 | #include "connection.h" |
54 | #include "connection.h" |
54 | #include "util.h" |
55 | #include "util.h" |
55 | #include "vpn.h" |
56 | #include "vpn.h" |
56 | |
57 | |
|
|
58 | using namespace std; |
|
|
59 | |
57 | vpn network; // THE vpn (bad design...) |
60 | vpn network; // THE vpn (bad design...) |
58 | |
61 | |
59 | ///////////////////////////////////////////////////////////////////////////// |
62 | ///////////////////////////////////////////////////////////////////////////// |
60 | |
63 | |
61 | static void inline |
64 | static void inline |
… | |
… | |
75 | { |
78 | { |
76 | // the tunnel device mtu should be the physical mtu - overhead |
79 | // the tunnel device mtu should be the physical mtu - overhead |
77 | // the tricky part is rounding to the cipher key blocksize |
80 | // the tricky part is rounding to the cipher key blocksize |
78 | int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD; |
81 | int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD; |
79 | mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion |
82 | mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion |
80 | mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round |
83 | mtu -= mtu % BLOCK_SIZE (CIPHER); // round |
81 | mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
84 | mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
82 | |
85 | |
83 | char *env; |
86 | char *env; |
84 | asprintf (&env, "CONFBASE=%s", confbase); putenv (env); |
87 | asprintf (&env, "CONFBASE=%s", confbase); putenv (env); |
85 | asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env); |
88 | asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env); |
… | |
… | |
120 | |
123 | |
121 | return filename; |
124 | return filename; |
122 | } |
125 | } |
123 | |
126 | |
124 | int |
127 | int |
|
|
128 | vpn::setup_socket (u8 prot, int family, int type, int proto) |
|
|
129 | { |
|
|
130 | int fd = socket (family, type, proto); |
|
|
131 | |
|
|
132 | if (fd < 0) |
|
|
133 | { |
|
|
134 | slog (L_ERR, _("unable to create %s socket: %s."), strprotocol (prot), strerror (errno)); |
|
|
135 | return fd; |
|
|
136 | } |
|
|
137 | |
|
|
138 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
139 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
140 | |
|
|
141 | #ifdef SO_MARK |
|
|
142 | if (::conf.nfmark) |
|
|
143 | if (setsockopt (fd, SOL_SOCKET, SO_MARK, &::conf.nfmark, sizeof ::conf.nfmark)) |
|
|
144 | slog (L_WARN, _("unable to set nfmark on %s socket: %s"), strprotocol (prot), strerror (errno)); |
|
|
145 | #endif |
|
|
146 | |
|
|
147 | return fd; |
|
|
148 | } |
|
|
149 | |
|
|
150 | int |
125 | vpn::setup () |
151 | vpn::setup () |
126 | { |
152 | { |
127 | int success = 0; |
153 | int success = 0; |
128 | |
154 | |
129 | ipv4_tos = -1; |
155 | ipv4_tos = -1; |
130 | ipv4_fd = -1; |
156 | ipv4_fd = -1; |
131 | |
157 | |
132 | if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto) |
158 | if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto) |
133 | { |
159 | { |
134 | ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); |
160 | ipv4_fd = setup_socket (PROT_IPv4, PF_INET, SOCK_RAW, ::conf.ip_proto); |
135 | |
161 | |
136 | if (ipv4_fd < 0) |
162 | if (ipv4_fd < 0) |
137 | return -1; |
163 | return -1; |
138 | |
|
|
139 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
|
|
140 | fcntl (ipv4_fd, F_SETFD, FD_CLOEXEC); |
|
|
141 | |
164 | |
142 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
165 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
143 | // this I really consider a linux bug. I am neither connected |
166 | // this I really consider a linux bug. I am neither connected |
144 | // nor do I fragment myself. Linux still sets DF and doesn't |
167 | // nor do I fragment myself. Linux still sets DF and doesn't |
145 | // fragment for me sometimes. |
168 | // fragment for me sometimes. |
… | |
… | |
152 | sockinfo si (THISNODE, PROT_IPv4); |
175 | sockinfo si (THISNODE, PROT_IPv4); |
153 | |
176 | |
154 | if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
177 | if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
155 | { |
178 | { |
156 | slog (L_ERR, _("can't bind ipv4 socket on %s: %s, exiting."), (const char *)si, strerror (errno)); |
179 | slog (L_ERR, _("can't bind ipv4 socket on %s: %s, exiting."), (const char *)si, strerror (errno)); |
157 | exit (EXIT_FAILURE); |
180 | return -1; |
158 | } |
181 | } |
159 | |
182 | |
160 | ipv4_ev_watcher.start (ipv4_fd, EV_READ); |
183 | ipv4_ev_watcher.start (ipv4_fd, EV_READ); |
161 | ++success; |
184 | ++success; |
162 | } |
185 | } |
… | |
… | |
166 | udpv4_tos = -1; |
189 | udpv4_tos = -1; |
167 | udpv4_fd = -1; |
190 | udpv4_fd = -1; |
168 | |
191 | |
169 | if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) |
192 | if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) |
170 | { |
193 | { |
171 | udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
194 | udpv4_fd = setup_socket (PROT_UDPv4, PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
172 | |
195 | |
173 | if (udpv4_fd < 0) |
196 | if (udpv4_fd < 0) |
174 | return -1; |
197 | return -1; |
175 | |
|
|
176 | fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
177 | fcntl (udpv4_fd, F_SETFD, FD_CLOEXEC); |
|
|
178 | |
198 | |
179 | // standard daemon practise... |
199 | // standard daemon practise... |
180 | { |
200 | { |
181 | int oval = 1; |
201 | int oval = 1; |
182 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
202 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
… | |
… | |
195 | sockinfo si (THISNODE, PROT_UDPv4); |
215 | sockinfo si (THISNODE, PROT_UDPv4); |
196 | |
216 | |
197 | if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) |
217 | if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) |
198 | { |
218 | { |
199 | slog (L_ERR, _("can't bind udpv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); |
219 | slog (L_ERR, _("can't bind udpv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); |
200 | exit (EXIT_FAILURE); |
220 | return -1; |
201 | } |
221 | } |
202 | |
222 | |
203 | udpv4_ev_watcher.start (udpv4_fd, EV_READ); |
223 | udpv4_ev_watcher.start (udpv4_fd, EV_READ); |
204 | ++success; |
224 | ++success; |
205 | } |
225 | } |
… | |
… | |
210 | icmpv4_fd = -1; |
230 | icmpv4_fd = -1; |
211 | |
231 | |
212 | #if ENABLE_ICMP |
232 | #if ENABLE_ICMP |
213 | if (THISNODE->protocols & PROT_ICMPv4) |
233 | if (THISNODE->protocols & PROT_ICMPv4) |
214 | { |
234 | { |
215 | icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); |
235 | icmpv4_fd = setup_socket (PROT_ICMPv4, PF_INET, SOCK_RAW, IPPROTO_ICMP); |
216 | |
236 | |
217 | if (icmpv4_fd < 0) |
237 | if (icmpv4_fd < 0) |
218 | return -1; |
238 | return -1; |
219 | |
|
|
220 | fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
221 | fcntl (icmpv4_fd, F_SETFD, FD_CLOEXEC); |
|
|
222 | |
239 | |
223 | #ifdef ICMP_FILTER |
240 | #ifdef ICMP_FILTER |
224 | { |
241 | { |
225 | icmp_filter oval; |
242 | icmp_filter oval; |
226 | oval.data = 0xffffffff; |
243 | oval.data = 0xffffffff; |
… | |
… | |
244 | sockinfo si (THISNODE, PROT_ICMPv4); |
261 | sockinfo si (THISNODE, PROT_ICMPv4); |
245 | |
262 | |
246 | if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ())) |
263 | if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ())) |
247 | { |
264 | { |
248 | slog (L_ERR, _("can't bind icmpv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); |
265 | slog (L_ERR, _("can't bind icmpv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); |
249 | exit (EXIT_FAILURE); |
266 | return -1; |
250 | } |
267 | } |
251 | |
268 | |
252 | icmpv4_ev_watcher.start (icmpv4_fd, EV_READ); |
269 | icmpv4_ev_watcher.start (icmpv4_fd, EV_READ); |
253 | ++success; |
270 | ++success; |
254 | } |
271 | } |
… | |
… | |
257 | tcpv4_fd = -1; |
274 | tcpv4_fd = -1; |
258 | |
275 | |
259 | #if ENABLE_TCP |
276 | #if ENABLE_TCP |
260 | if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port) |
277 | if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port) |
261 | { |
278 | { |
262 | tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
279 | tcpv4_fd = setup_socket (PROT_TCPv4, PF_INET, SOCK_STREAM, IPPROTO_TCP); |
263 | |
280 | |
264 | if (tcpv4_fd < 0) |
281 | if (tcpv4_fd < 0) |
265 | return -1; |
282 | return -1; |
266 | |
283 | |
267 | fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
268 | fcntl (tcpv4_fd, F_SETFD, FD_CLOEXEC); |
|
|
269 | |
|
|
270 | // standard daemon practise... |
284 | // standard daemon practise... |
271 | { |
285 | { |
272 | int oval = 1; |
286 | int oval = 1; |
273 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
287 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
274 | } |
288 | } |
… | |
… | |
276 | sockinfo si (THISNODE, PROT_TCPv4); |
290 | sockinfo si (THISNODE, PROT_TCPv4); |
277 | |
291 | |
278 | if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ())) |
292 | if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ())) |
279 | { |
293 | { |
280 | slog (L_ERR, _("can't bind tcpv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); |
294 | slog (L_ERR, _("can't bind tcpv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); |
281 | exit (EXIT_FAILURE); |
295 | return -1; |
282 | } |
296 | } |
283 | |
297 | |
284 | if (listen (tcpv4_fd, 5)) |
298 | if (listen (tcpv4_fd, 5)) |
285 | { |
299 | { |
286 | slog (L_ERR, _("can't listen tcpv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); |
300 | slog (L_ERR, _("can't listen tcpv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); |
287 | exit (EXIT_FAILURE); |
301 | return -1; |
288 | } |
302 | } |
289 | |
303 | |
290 | tcpv4_ev_watcher.start (tcpv4_fd, EV_READ); |
304 | tcpv4_ev_watcher.start (tcpv4_fd, EV_READ); |
291 | ++success; |
305 | ++success; |
292 | } |
306 | } |
… | |
… | |
300 | #if ENABLE_DNS |
314 | #if ENABLE_DNS |
301 | if (THISNODE->protocols & PROT_DNSv4) |
315 | if (THISNODE->protocols & PROT_DNSv4) |
302 | { |
316 | { |
303 | dns_forwarder.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); |
317 | dns_forwarder.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); |
304 | |
318 | |
305 | dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
319 | dnsv4_fd = setup_socket (PROT_DNSv4, PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
306 | |
320 | |
307 | if (dnsv4_fd < 0) |
321 | if (dnsv4_fd < 0) |
308 | return -1; |
322 | return -1; |
309 | |
|
|
310 | fcntl (dnsv4_fd, F_SETFL, O_NONBLOCK); |
|
|
311 | fcntl (dnsv4_fd, F_SETFD, FD_CLOEXEC); |
|
|
312 | |
323 | |
313 | # if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
324 | # if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
314 | // this I really consider a linux bug. I am neither connected |
325 | // this I really consider a linux bug. I am neither connected |
315 | // nor do I fragment myself. Linux still sets DF and doesn't |
326 | // nor do I fragment myself. Linux still sets DF and doesn't |
316 | // fragment for me sometimes. |
327 | // fragment for me sometimes. |
… | |
… | |
331 | PROT_DNSv4); |
342 | PROT_DNSv4); |
332 | |
343 | |
333 | if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ())) |
344 | if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ())) |
334 | { |
345 | { |
335 | slog (L_ERR, _("can't bind dnsv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); |
346 | slog (L_ERR, _("can't bind dnsv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); |
336 | exit (EXIT_FAILURE); |
347 | return -1; |
337 | } |
348 | } |
338 | |
349 | |
339 | dnsv4_ev_watcher.start (dnsv4_fd, EV_READ); |
350 | dnsv4_ev_watcher.start (dnsv4_fd, EV_READ); |
340 | ++success; |
351 | ++success; |
341 | } |
352 | } |
… | |
… | |
343 | |
354 | |
344 | ///////////////////////////////////////////////////////////////////////////// |
355 | ///////////////////////////////////////////////////////////////////////////// |
345 | |
356 | |
346 | if (!success) |
357 | if (!success) |
347 | { |
358 | { |
348 | slog (L_ERR, _("no protocols enabled, exiting.")); |
359 | slog (L_ERR, _("no protocols enabled.")); |
349 | exit (EXIT_FAILURE); |
360 | return -1; |
350 | } |
361 | } |
351 | |
362 | |
352 | reconnect_all (); |
363 | reconnect_all (); |
353 | |
364 | |
354 | ///////////////////////////////////////////////////////////////////////////// |
365 | ///////////////////////////////////////////////////////////////////////////// |
355 | |
366 | |
356 | tap = new tap_device (); |
367 | tap = new tap_device (); |
357 | if (!tap) //D this, of course, never catches |
368 | if (!tap) //D this, of course, never catches |
358 | { |
369 | { |
359 | slog (L_ERR, _("cannot create network interface '%s', exiting."), conf.ifname); |
370 | slog (L_ERR, _("cannot create network interface '%s'."), conf.ifname); |
360 | exit (EXIT_FAILURE); |
371 | return -1; |
361 | } |
372 | } |
362 | |
373 | |
363 | fcntl (tap->fd, F_SETFD, FD_CLOEXEC); |
374 | fcntl (tap->fd, F_SETFD, FD_CLOEXEC); |
364 | |
375 | |
365 | run_script_cb cb; |
376 | run_script_cb cb; |
366 | cb.set<vpn, &vpn::script_if_init> (this); |
377 | cb.set<vpn, &vpn::script_if_init> (this); |
367 | |
378 | |
368 | if (tap->if_up () && |
379 | if (tap->if_up () && |
369 | !run_script (cb, true)) |
380 | !run_script (cb, true)) |
370 | { |
381 | { |
371 | slog (L_ERR, _("interface initialization command '%s' failed, exiting."), |
382 | slog (L_ERR, _("interface initialization command '%s' failed."), |
372 | tap->if_up ()); |
383 | tap->if_up ()); |
373 | exit (EXIT_FAILURE); |
384 | return -1; |
374 | } |
385 | } |
375 | |
386 | |
376 | cb.set<vpn, &vpn::script_if_up> (this); |
387 | cb.set<vpn, &vpn::script_if_up> (this); |
377 | if (!run_script (cb, true)) |
388 | if (!run_script (cb, true)) |
378 | { |
389 | { |
379 | slog (L_ERR, _("if-up command execution failed, exiting.")); |
390 | slog (L_ERR, _("if-up command execution failed.")); |
380 | exit (EXIT_FAILURE); |
391 | return -1; |
381 | } |
392 | } |
382 | |
393 | |
383 | tap_ev_watcher.start (tap->fd, EV_READ); |
394 | tap_ev_watcher.start (tap->fd, EV_READ); |
384 | |
395 | |
385 | return 0; |
396 | return 0; |
|
|
397 | } |
|
|
398 | |
|
|
399 | bool |
|
|
400 | vpn::drop_privileges () |
|
|
401 | { |
|
|
402 | if (::conf.change_root) |
|
|
403 | { |
|
|
404 | if (!strcmp (::conf.change_root, "/")) |
|
|
405 | { |
|
|
406 | char dir [L_tmpnam]; |
|
|
407 | if (!tmpnam (dir)) |
|
|
408 | { |
|
|
409 | slog (L_CRIT, _("unable to create anonymous root path.")); |
|
|
410 | return false; |
|
|
411 | } |
|
|
412 | |
|
|
413 | if (mkdir (dir, 0700)) |
|
|
414 | { |
|
|
415 | slog (L_CRIT, _("unable to crate anonymous root directory.")); |
|
|
416 | return false; |
|
|
417 | } |
|
|
418 | |
|
|
419 | if (chdir (dir)) |
|
|
420 | { |
|
|
421 | slog (L_CRIT, _("unable to change to anonymous root directory.")); |
|
|
422 | return false; |
|
|
423 | } |
|
|
424 | |
|
|
425 | if (rmdir (dir)) |
|
|
426 | slog (L_ERR, _("unable to remove anonymous root directory, continuing.")); |
|
|
427 | } |
|
|
428 | else |
|
|
429 | { |
|
|
430 | if (chdir (::conf.change_root)) |
|
|
431 | { |
|
|
432 | slog (L_CRIT, _("%s: unable to change to specified root directory."), ::conf.change_root); |
|
|
433 | return false; |
|
|
434 | } |
|
|
435 | } |
|
|
436 | |
|
|
437 | if (chroot (".")) |
|
|
438 | { |
|
|
439 | slog (L_CRIT, _("unable to set new root directory.")); |
|
|
440 | return false; |
|
|
441 | } |
|
|
442 | |
|
|
443 | if (chdir ("/")) |
|
|
444 | { |
|
|
445 | slog (L_CRIT, _("unable to set cwd to new root directory.")); |
|
|
446 | return false; |
|
|
447 | } |
|
|
448 | } |
|
|
449 | |
|
|
450 | if (::conf.change_gid) |
|
|
451 | if (setgid (::conf.change_gid)) |
|
|
452 | { |
|
|
453 | slog (L_CRIT, _("unable to change group id to %d."), ::conf.change_gid); |
|
|
454 | return false; |
|
|
455 | } |
|
|
456 | |
|
|
457 | if (::conf.change_uid) |
|
|
458 | if (setuid (::conf.change_uid)) |
|
|
459 | { |
|
|
460 | slog (L_CRIT, _("unable to change user id to %d."), ::conf.change_uid); |
|
|
461 | return false; |
|
|
462 | } |
|
|
463 | |
|
|
464 | return true; |
386 | } |
465 | } |
387 | |
466 | |
388 | bool |
467 | bool |
389 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
468 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
390 | { |
469 | { |
… | |
… | |
474 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst (), pkt->len); |
553 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst (), pkt->len); |
475 | |
554 | |
476 | if (src == 0 || src > conns.size () |
555 | if (src == 0 || src > conns.size () |
477 | || dst > conns.size () |
556 | || dst > conns.size () |
478 | || pkt->typ () >= vpn_packet::PT_MAX) |
557 | || pkt->typ () >= vpn_packet::PT_MAX) |
479 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)."), |
|
|
480 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
|
|
481 | else if (dst > conns.size ()) |
|
|
482 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)."), |
558 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)."), |
483 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
559 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
484 | else |
560 | else |
485 | { |
561 | { |
486 | connection *c = conns[src - 1]; |
562 | connection *c = conns[src - 1]; |
… | |
… | |
508 | vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
584 | vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
509 | { |
585 | { |
510 | switch (si.prot) |
586 | switch (si.prot) |
511 | { |
587 | { |
512 | case PROT_IPv4: |
588 | case PROT_IPv4: |
513 | return send_ipv4_packet (pkt, si, tos); |
589 | return send_ipv4_packet (pkt, si, tos); |
514 | |
590 | |
515 | case PROT_UDPv4: |
591 | case PROT_UDPv4: |
516 | return send_udpv4_packet (pkt, si, tos); |
592 | return send_udpv4_packet (pkt, si, tos); |
517 | |
593 | |
518 | #if ENABLE_TCP |
594 | #if ENABLE_TCP |
519 | case PROT_TCPv4: |
595 | case PROT_TCPv4: |
520 | return send_tcpv4_packet (pkt, si, tos); |
596 | return send_tcpv4_packet (pkt, si, tos); |
521 | #endif |
597 | #endif |
522 | #if ENABLE_ICMP |
598 | #if ENABLE_ICMP |
523 | case PROT_ICMPv4: |
599 | case PROT_ICMPv4: |
524 | return send_icmpv4_packet (pkt, si, tos); |
600 | return send_icmpv4_packet (pkt, si, tos); |
525 | #endif |
601 | #endif |
526 | #if ENABLE_DNS |
602 | #if ENABLE_DNS |
527 | case PROT_DNSv4: |
603 | case PROT_DNSv4: |
528 | return send_dnsv4_packet (pkt, si, tos); |
604 | return send_dnsv4_packet (pkt, si, tos); |
529 | #endif |
605 | #endif |
530 | default: |
606 | default: |
531 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol."), (const char *)si); |
607 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol."), (const char *)si); |
532 | } |
608 | } |
533 | |
609 | |
… | |
… | |
551 | if (len > 0) |
627 | if (len > 0) |
552 | { |
628 | { |
553 | pkt->len = len; |
629 | pkt->len = len; |
554 | |
630 | |
555 | // raw sockets deliver the ipv4 header, but don't expect it on sends |
631 | // raw sockets deliver the ipv4 header, but don't expect it on sends |
556 | pkt->skip_hdr (IP_OVERHEAD); |
632 | pkt->skip_hdr (pkt->ipv4_hdr_len ()); |
557 | |
633 | |
558 | recv_vpn_packet (pkt, si); |
634 | recv_vpn_packet (pkt, si); |
559 | } |
635 | } |
560 | else |
636 | else |
561 | { |
637 | { |
… | |
… | |
598 | if (hdr->type == ::conf.icmp_type |
674 | if (hdr->type == ::conf.icmp_type |
599 | && hdr->code == 255) |
675 | && hdr->code == 255) |
600 | { |
676 | { |
601 | // raw sockets deliver the ipv4, but don't expect it on sends |
677 | // raw sockets deliver the ipv4, but don't expect it on sends |
602 | // this is slow, but... |
678 | // this is slow, but... |
603 | pkt->skip_hdr (ICMP_OVERHEAD); |
679 | pkt->skip_hdr (pkt->ipv4_hdr_len () + (ICMP_OVERHEAD - IP_OVERHEAD)); |
604 | |
680 | |
605 | recv_vpn_packet (pkt, si); |
681 | recv_vpn_packet (pkt, si); |
606 | } |
682 | } |
607 | } |
683 | } |
608 | else |
684 | else |
… | |
… | |
750 | |
826 | |
751 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
827 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
752 | (*c)->establish_connection (); |
828 | (*c)->establish_connection (); |
753 | } |
829 | } |
754 | |
830 | |
|
|
831 | bool |
755 | bool vpn::can_direct (conf_node *src, conf_node *dst) const |
832 | vpn::can_direct (conf_node *src, conf_node *dst) const |
756 | { |
833 | { |
757 | return src != dst |
834 | return src != dst |
758 | && src->may_direct (dst) |
835 | && src->may_direct (dst) |
759 | && dst->may_direct (src) |
836 | && dst->may_direct (src) |
760 | && (((src->protocols & dst->protocols) && src->connectmode == conf_node::C_ALWAYS) |
837 | && (((src->protocols & dst->protocols) && src->connectmode == conf_node::C_ALWAYS) |
761 | || (src->protocols & dst->connectable_protocols ())); |
838 | || (src->protocols & dst->connectable_protocols ())); |
762 | } |
839 | } |
763 | |
840 | |
764 | // only works for indirect and routed connections: find a router |
841 | // only works for indirect and routed connections: find a router |
765 | // from THISNODE to dst |
842 | // from THISNODE to dst |
|
|
843 | connection * |
766 | connection *vpn::find_router_for (const connection *dst) |
844 | vpn::find_router_for (const connection *dst) |
767 | { |
845 | { |
768 | connection *router = 0; |
846 | connection *router = 0; |
769 | |
847 | |
770 | // first try to find a router with a direct connection, route there |
848 | // first try to find a router with a direct connection, route there |
771 | // regardless of any other considerations. |
849 | // regardless of any other considerations. |
… | |
… | |
810 | } |
888 | } |
811 | |
889 | |
812 | return router; |
890 | return router; |
813 | } |
891 | } |
814 | |
892 | |
|
|
893 | void |
815 | void vpn::connection_established (connection *c) |
894 | vpn::connection_established (connection *c) |
816 | { |
895 | { |
817 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
896 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
818 | { |
897 | { |
819 | connection *o = *i; |
898 | connection *o = *i; |
820 | |
899 | |
… | |
… | |
828 | o->rekey (); |
907 | o->rekey (); |
829 | } |
908 | } |
830 | } |
909 | } |
831 | } |
910 | } |
832 | |
911 | |
|
|
912 | void |
833 | void vpn::send_connect_request (connection *c) |
913 | vpn::send_connect_request (connection *c) |
834 | { |
914 | { |
835 | connection *r = find_router_for (c); |
915 | connection *r = find_router_for (c); |
836 | |
916 | |
837 | if (r) |
917 | if (r) |
838 | { |
918 | { |
839 | slog (L_TRACE, _("%s: no way to connect, sending mediated connection request via %s."), |
919 | slog (L_TRACE, _("%s: no address known, sending mediated connection request via %s."), |
840 | c->conf->nodename, r->conf->nodename); |
920 | c->conf->nodename, r->conf->nodename); |
841 | r->send_connect_request (c->conf->id); |
921 | r->send_connect_request (c->conf->id); |
842 | } |
922 | } |
843 | else |
923 | else |
844 | slog (L_DEBUG, _("%s: no way to connect and no router found: unable to connect."), |
924 | slog (L_DEBUG, _("%s: no way to connect and no router found: unable to connect at this time."), |
845 | c->conf->nodename); |
925 | c->conf->nodename); |
846 | } |
926 | } |
847 | |
927 | |
848 | void |
928 | void |
849 | connection::dump_status () |
929 | connection::dump_status () |