… | |
… | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU General Public License for more details. |
15 | GNU General Public License for more details. |
16 | |
16 | |
17 | You should have received a copy of the GNU General Public License |
17 | You should have received a copy of the GNU General Public License |
18 | along with gvpe; if not, write to the Free Software |
18 | along with gvpe; if not, write to the Free Software |
19 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | */ |
20 | */ |
21 | |
21 | |
22 | #include "config.h" |
22 | #include "config.h" |
23 | |
23 | |
24 | #include <list> |
24 | #include <list> |
… | |
… | |
58 | mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round |
58 | mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round |
59 | mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
59 | mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
60 | |
60 | |
61 | char *env; |
61 | char *env; |
62 | asprintf (&env, "CONFBASE=%s", confbase); putenv (env); |
62 | asprintf (&env, "CONFBASE=%s", confbase); putenv (env); |
63 | asprintf (&env, "NODENAME=%s", THISNODE->nodename); putenv (env); |
|
|
64 | asprintf (&env, "NODEID=%d", THISNODE->id); putenv (env); |
|
|
65 | asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env); |
63 | asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env); |
66 | asprintf (&env, "IFTYPE=%s", IFTYPE); putenv (env); |
64 | asprintf (&env, "IFTYPE=%s", IFTYPE); putenv (env); |
67 | asprintf (&env, "IFSUBTYPE=%s", IFSUBTYPE); putenv (env); |
65 | asprintf (&env, "IFSUBTYPE=%s", IFSUBTYPE); putenv (env); |
68 | asprintf (&env, "MTU=%d", mtu); putenv (env); |
66 | asprintf (&env, "MTU=%d", mtu); putenv (env); |
69 | asprintf (&env, "MAC=%02x:%02x:%02x:%02x:%02x:%02x", |
67 | asprintf (&env, "NODES=%d", conns.size ()); putenv (env); |
70 | 0xfe, 0xfd, 0x80, 0x00, THISNODE->id >> 8, |
68 | asprintf (&env, "NODEID=%d", THISNODE->id); putenv (env); |
71 | THISNODE->id & 0xff); putenv (env); |
|
|
72 | |
69 | |
73 | // TODO: info for other nodes, maybe? |
70 | conns [THISNODE->id - 1]->script_init_env (""); |
|
|
71 | |
|
|
72 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
|
|
73 | { |
|
|
74 | char ext[16]; |
|
|
75 | snprintf (ext, 16, "_%d", (*c)->conf->id); |
|
|
76 | (*c)->script_init_env (ext); |
|
|
77 | } |
74 | } |
78 | } |
75 | |
79 | |
76 | const char *vpn::script_if_init () |
80 | const char *vpn::script_if_init () |
77 | { |
81 | { |
78 | script_init_env (); |
82 | script_init_env (); |
… | |
… | |
104 | |
108 | |
105 | if (ipv4_fd < 0) |
109 | if (ipv4_fd < 0) |
106 | return -1; |
110 | return -1; |
107 | |
111 | |
108 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
112 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
|
|
113 | fcntl (ipv4_fd, F_SETFD, FD_CLOEXEC); |
109 | |
114 | |
110 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
115 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
111 | // this I really consider a linux bug. I am neither connected |
116 | // this I really consider a linux bug. I am neither connected |
112 | // nor do I fragment myself. Linux still sets DF and doesn't |
117 | // nor do I fragment myself. Linux still sets DF and doesn't |
113 | // fragment for me sometimes. |
118 | // fragment for me sometimes. |
… | |
… | |
123 | { |
128 | { |
124 | slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); |
129 | slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); |
125 | exit (EXIT_FAILURE); |
130 | exit (EXIT_FAILURE); |
126 | } |
131 | } |
127 | |
132 | |
128 | ipv4_ev_watcher.start (ipv4_fd, EVENT_READ); |
133 | ipv4_ev_watcher.start (ipv4_fd, EV_READ); |
129 | } |
134 | } |
130 | |
135 | |
131 | udpv4_fd = -1; |
136 | udpv4_fd = -1; |
132 | |
137 | |
133 | if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) |
138 | if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) |
… | |
… | |
136 | |
141 | |
137 | if (udpv4_fd < 0) |
142 | if (udpv4_fd < 0) |
138 | return -1; |
143 | return -1; |
139 | |
144 | |
140 | fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); |
145 | fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
146 | fcntl (udpv4_fd, F_SETFD, FD_CLOEXEC); |
141 | |
147 | |
142 | // standard daemon practise... |
148 | // standard daemon practise... |
143 | { |
149 | { |
144 | int oval = 1; |
150 | int oval = 1; |
145 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
151 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
… | |
… | |
161 | { |
167 | { |
162 | slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); |
168 | slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); |
163 | exit (EXIT_FAILURE); |
169 | exit (EXIT_FAILURE); |
164 | } |
170 | } |
165 | |
171 | |
166 | udpv4_ev_watcher.start (udpv4_fd, EVENT_READ); |
172 | udpv4_ev_watcher.start (udpv4_fd, EV_READ); |
167 | } |
173 | } |
168 | |
174 | |
169 | icmpv4_fd = -1; |
175 | icmpv4_fd = -1; |
170 | |
176 | |
171 | #if ENABLE_ICMP |
177 | #if ENABLE_ICMP |
… | |
… | |
175 | |
181 | |
176 | if (icmpv4_fd < 0) |
182 | if (icmpv4_fd < 0) |
177 | return -1; |
183 | return -1; |
178 | |
184 | |
179 | fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); |
185 | fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
186 | fcntl (icmpv4_fd, F_SETFD, FD_CLOEXEC); |
180 | |
187 | |
181 | #ifdef ICMP_FILTER |
188 | #ifdef ICMP_FILTER |
182 | { |
189 | { |
183 | icmp_filter oval; |
190 | icmp_filter oval; |
184 | oval.data = 0xffffffff; |
191 | oval.data = 0xffffffff; |
… | |
… | |
193 | // this I really consider a linux bug. I am neither connected |
200 | // this I really consider a linux bug. I am neither connected |
194 | // nor do I fragment myself. Linux still sets DF and doesn't |
201 | // nor do I fragment myself. Linux still sets DF and doesn't |
195 | // fragment for me sometimes. |
202 | // fragment for me sometimes. |
196 | { |
203 | { |
197 | int oval = IP_PMTUDISC_DONT; |
204 | int oval = IP_PMTUDISC_DONT; |
198 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
205 | setsockopt (icmpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
199 | } |
206 | } |
200 | #endif |
207 | #endif |
201 | |
208 | |
202 | sockinfo si (THISNODE, PROT_ICMPv4); |
209 | sockinfo si (THISNODE, PROT_ICMPv4); |
203 | |
210 | |
… | |
… | |
205 | { |
212 | { |
206 | slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); |
213 | slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); |
207 | exit (EXIT_FAILURE); |
214 | exit (EXIT_FAILURE); |
208 | } |
215 | } |
209 | |
216 | |
210 | icmpv4_ev_watcher.start (icmpv4_fd, EVENT_READ); |
217 | icmpv4_ev_watcher.start (icmpv4_fd, EV_READ); |
211 | } |
218 | } |
212 | #endif |
219 | #endif |
213 | |
220 | |
214 | tcpv4_fd = -1; |
221 | tcpv4_fd = -1; |
215 | |
222 | |
… | |
… | |
220 | |
227 | |
221 | if (tcpv4_fd < 0) |
228 | if (tcpv4_fd < 0) |
222 | return -1; |
229 | return -1; |
223 | |
230 | |
224 | fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); |
231 | fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
232 | fcntl (tcpv4_fd, F_SETFD, FD_CLOEXEC); |
225 | |
233 | |
226 | // standard daemon practise... |
234 | // standard daemon practise... |
227 | { |
235 | { |
228 | int oval = 1; |
236 | int oval = 1; |
229 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
237 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
… | |
… | |
241 | { |
249 | { |
242 | slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno)); |
250 | slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno)); |
243 | exit (EXIT_FAILURE); |
251 | exit (EXIT_FAILURE); |
244 | } |
252 | } |
245 | |
253 | |
246 | tcpv4_ev_watcher.start (tcpv4_fd, EVENT_READ); |
254 | tcpv4_ev_watcher.start (tcpv4_fd, EV_READ); |
247 | } |
255 | } |
248 | #endif |
256 | #endif |
249 | |
257 | |
250 | #if ENABLE_DNS |
258 | #if ENABLE_DNS |
251 | if (THISNODE->protocols & PROT_DNSv4) |
259 | if (THISNODE->protocols & PROT_DNSv4) |
… | |
… | |
255 | dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
263 | dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
256 | |
264 | |
257 | if (dnsv4_fd < 0) |
265 | if (dnsv4_fd < 0) |
258 | return -1; |
266 | return -1; |
259 | |
267 | |
|
|
268 | fcntl (dnsv4_fd, F_SETFL, O_NONBLOCK); |
|
|
269 | fcntl (dnsv4_fd, F_SETFD, FD_CLOEXEC); |
|
|
270 | |
260 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
271 | # if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
261 | // this I really consider a linux bug. I am neither connected |
272 | // this I really consider a linux bug. I am neither connected |
262 | // nor do I fragment myself. Linux still sets DF and doesn't |
273 | // nor do I fragment myself. Linux still sets DF and doesn't |
263 | // fragment for me sometimes. |
274 | // fragment for me sometimes. |
264 | { |
275 | { |
265 | int oval = IP_PMTUDISC_DONT; |
276 | int oval = IP_PMTUDISC_DONT; |
266 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
277 | setsockopt (dnsv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
267 | } |
278 | } |
268 | #endif |
279 | # endif |
269 | |
280 | |
270 | // standard daemon practise... |
281 | // standard daemon practise... |
271 | { |
282 | { |
272 | int oval = 1; |
283 | int oval = 1; |
273 | setsockopt (dnsv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
284 | setsockopt (dnsv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
… | |
… | |
281 | { |
292 | { |
282 | slog (L_ERR, _("can't bind dnsv4 on %s: %s"), (const char *)si, strerror (errno)); |
293 | slog (L_ERR, _("can't bind dnsv4 on %s: %s"), (const char *)si, strerror (errno)); |
283 | exit (EXIT_FAILURE); |
294 | exit (EXIT_FAILURE); |
284 | } |
295 | } |
285 | |
296 | |
286 | dnsv4_ev_watcher.start (dnsv4_fd, EVENT_READ); |
297 | dnsv4_ev_watcher.start (dnsv4_fd, EV_READ); |
287 | } |
298 | } |
288 | #endif |
299 | #endif |
|
|
300 | |
|
|
301 | ///////////////////////////////////////////////////////////////////////////// |
|
|
302 | |
|
|
303 | reconnect_all (); |
|
|
304 | |
|
|
305 | ///////////////////////////////////////////////////////////////////////////// |
289 | |
306 | |
290 | tap = new tap_device (); |
307 | tap = new tap_device (); |
291 | if (!tap) //D this, of course, never catches |
308 | if (!tap) //D this, of course, never catches |
292 | { |
309 | { |
293 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
310 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
294 | exit (EXIT_FAILURE); |
311 | exit (EXIT_FAILURE); |
295 | } |
312 | } |
296 | |
313 | |
|
|
314 | fcntl (tap->fd, F_SETFD, FD_CLOEXEC); |
|
|
315 | |
297 | if (tap->if_up () && |
316 | if (tap->if_up () && |
298 | !run_script (run_script_cb (this, &vpn::script_if_init), true)) |
317 | !run_script (run_script_cb (this, &vpn::script_if_init), true)) |
299 | { |
318 | { |
300 | slog (L_ERR, _("interface initialization command '%s' failed, exiting."), |
319 | slog (L_ERR, _("interface initialization command '%s' failed, exiting."), |
301 | tap->if_up ()); |
320 | tap->if_up ()); |
… | |
… | |
306 | { |
325 | { |
307 | slog (L_ERR, _("if-up command execution failed, exiting.")); |
326 | slog (L_ERR, _("if-up command execution failed, exiting.")); |
308 | exit (EXIT_FAILURE); |
327 | exit (EXIT_FAILURE); |
309 | } |
328 | } |
310 | |
329 | |
311 | tap_ev_watcher.start (tap->fd, EVENT_READ); |
330 | tap_ev_watcher.start (tap->fd, EV_READ); |
312 | |
|
|
313 | reconnect_all (); |
|
|
314 | |
331 | |
315 | return 0; |
332 | return 0; |
316 | } |
333 | } |
317 | |
334 | |
318 | bool |
335 | bool |
… | |
… | |
469 | |
486 | |
470 | return false; |
487 | return false; |
471 | } |
488 | } |
472 | |
489 | |
473 | void |
490 | void |
474 | vpn::ipv4_ev (io_watcher &w, short revents) |
491 | vpn::ipv4_ev (ev::io &w, int revents) |
475 | { |
492 | { |
476 | if (revents & EVENT_READ) |
493 | if (revents & EV_READ) |
477 | { |
494 | { |
478 | vpn_packet *pkt = new vpn_packet; |
495 | vpn_packet *pkt = new vpn_packet; |
479 | struct sockaddr_in sa; |
496 | struct sockaddr_in sa; |
480 | socklen_t sa_len = sizeof (sa); |
497 | socklen_t sa_len = sizeof (sa); |
481 | int len; |
498 | int len; |
… | |
… | |
510 | } |
527 | } |
511 | } |
528 | } |
512 | |
529 | |
513 | #if ENABLE_ICMP |
530 | #if ENABLE_ICMP |
514 | void |
531 | void |
515 | vpn::icmpv4_ev (io_watcher &w, short revents) |
532 | vpn::icmpv4_ev (ev::io &w, int revents) |
516 | { |
533 | { |
517 | if (revents & EVENT_READ) |
534 | if (revents & EV_READ) |
518 | { |
535 | { |
519 | vpn_packet *pkt = new vpn_packet; |
536 | vpn_packet *pkt = new vpn_packet; |
520 | struct sockaddr_in sa; |
537 | struct sockaddr_in sa; |
521 | socklen_t sa_len = sizeof (sa); |
538 | socklen_t sa_len = sizeof (sa); |
522 | int len; |
539 | int len; |
… | |
… | |
558 | } |
575 | } |
559 | } |
576 | } |
560 | #endif |
577 | #endif |
561 | |
578 | |
562 | void |
579 | void |
563 | vpn::udpv4_ev (io_watcher &w, short revents) |
580 | vpn::udpv4_ev (ev::io &w, int revents) |
564 | { |
581 | { |
565 | if (revents & EVENT_READ) |
582 | if (revents & EV_READ) |
566 | { |
583 | { |
567 | vpn_packet *pkt = new vpn_packet; |
584 | vpn_packet *pkt = new vpn_packet; |
568 | struct sockaddr_in sa; |
585 | struct sockaddr_in sa; |
569 | socklen_t sa_len = sizeof (sa); |
586 | socklen_t sa_len = sizeof (sa); |
570 | int len; |
587 | int len; |
… | |
… | |
595 | exit (EXIT_FAILURE); |
612 | exit (EXIT_FAILURE); |
596 | } |
613 | } |
597 | } |
614 | } |
598 | |
615 | |
599 | void |
616 | void |
600 | vpn::tap_ev (io_watcher &w, short revents) |
617 | vpn::tap_ev (ev::io &w, int revents) |
601 | { |
618 | { |
602 | if (revents & EVENT_READ) |
619 | if (revents & EV_READ) |
603 | { |
620 | { |
604 | /* process data */ |
621 | /* process data */ |
605 | tap_packet *pkt; |
622 | tap_packet *pkt; |
606 | |
623 | |
607 | pkt = tap->recv (); |
624 | pkt = tap->recv (); |
… | |
… | |
637 | else |
654 | else |
638 | abort (); |
655 | abort (); |
639 | } |
656 | } |
640 | |
657 | |
641 | void |
658 | void |
642 | vpn::event_cb (time_watcher &w) |
659 | vpn::event_cb (ev::timer &w, int) |
643 | { |
660 | { |
644 | if (events) |
661 | if (events) |
645 | { |
662 | { |
646 | if (events & EVENT_SHUTDOWN) |
663 | if (events & EVENT_SHUTDOWN) |
647 | { |
664 | { |
… | |
… | |
738 | } |
755 | } |
739 | |
756 | |
740 | void |
757 | void |
741 | vpn::dump_status () |
758 | vpn::dump_status () |
742 | { |
759 | { |
743 | slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)NOW); |
760 | slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)ev_now ()); |
744 | |
761 | |
745 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
762 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
746 | (*c)->dump_status (); |
763 | (*c)->dump_status (); |
747 | |
764 | |
748 | slog (L_NOTICE, _("END status dump")); |
765 | slog (L_NOTICE, _("END status dump")); |