1 | /* |
1 | /* |
2 | vpn.C -- handle the protocol, encryption, handshaking etc. |
2 | vpn.C -- handle the protocol, encryption, handshaking etc. |
|
|
3 | Copyright (C) 2003-2004 Marc Lehmann <pcg@goof.com> |
3 | |
4 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
18 | |
19 | |
19 | #include "config.h" |
20 | #include "config.h" |
20 | |
21 | |
21 | #include <list> |
22 | #include <list> |
22 | |
23 | |
|
|
24 | #include <cstdio> |
|
|
25 | #include <cstring> |
23 | #include <cstdlib> |
26 | #include <cstdlib> |
24 | #include <cstring> |
|
|
25 | #include <cstdio> |
|
|
26 | |
27 | |
27 | #include <sys/types.h> |
28 | #include <sys/types.h> |
28 | #include <sys/socket.h> |
29 | #include <sys/socket.h> |
29 | #include <sys/poll.h> |
|
|
30 | #include <sys/wait.h> |
30 | #include <sys/wait.h> |
31 | #include <errno.h> |
31 | #include <errno.h> |
32 | #include <time.h> |
32 | #include <time.h> |
33 | #include <unistd.h> |
33 | #include <unistd.h> |
34 | #include <arpa/inet.h> |
34 | #include <fcntl.h> |
35 | #include <netinet/in.h> |
35 | #include <sys/socket.h> |
36 | #include <netinet/icmp.h> |
36 | |
|
|
37 | #include "netcompat.h" |
37 | |
38 | |
38 | #include "pidfile.h" |
39 | #include "pidfile.h" |
39 | |
40 | |
40 | #include "connection.h" |
41 | #include "connection.h" |
41 | #include "util.h" |
42 | #include "util.h" |
42 | #include "vpn.h" |
43 | #include "vpn.h" |
|
|
44 | |
|
|
45 | vpn network; // THE vpn (bad design...) |
43 | |
46 | |
44 | ///////////////////////////////////////////////////////////////////////////// |
47 | ///////////////////////////////////////////////////////////////////////////// |
45 | |
48 | |
46 | const char *vpn::script_if_up () |
49 | const char *vpn::script_if_up () |
47 | { |
50 | { |
… | |
… | |
51 | mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion |
54 | mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion |
52 | mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round |
55 | mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round |
53 | mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
56 | mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
54 | |
57 | |
55 | char *env; |
58 | char *env; |
56 | asprintf (&env, "CONFBASE=%s", confbase); |
59 | asprintf (&env, "CONFBASE=%s", confbase); putenv (env); |
57 | putenv (env); |
|
|
58 | asprintf (&env, "NODENAME=%s", THISNODE->nodename); |
60 | asprintf (&env, "NODENAME=%s", THISNODE->nodename); putenv (env); |
59 | putenv (env); |
|
|
60 | asprintf (&env, "NODEID=%d", THISNODE->id); |
61 | asprintf (&env, "NODEID=%d", THISNODE->id); putenv (env); |
61 | putenv (env); |
|
|
62 | asprintf (&env, "IFNAME=%s", tap->interface ()); |
62 | asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env); |
63 | putenv (env); |
63 | asprintf (&env, "IFTYPE=%s", IFTYPE); putenv (env); |
|
|
64 | asprintf (&env, "IFSUBTYPE=%s", IFSUBTYPE); putenv (env); |
64 | asprintf (&env, "MTU=%d", mtu); |
65 | asprintf (&env, "MTU=%d", mtu); putenv (env); |
65 | putenv (env); |
|
|
66 | asprintf (&env, "MAC=%02x:%02x:%02x:%02x:%02x:%02x", |
66 | asprintf (&env, "MAC=%02x:%02x:%02x:%02x:%02x:%02x", |
67 | 0xfe, 0xfd, 0x80, 0x00, THISNODE->id >> 8, |
67 | 0xfe, 0xfd, 0x80, 0x00, THISNODE->id >> 8, |
68 | THISNODE->id & 0xff); |
68 | THISNODE->id & 0xff); |
69 | putenv (env); |
69 | putenv (env); |
70 | |
70 | |
… | |
… | |
81 | ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); |
81 | ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); |
82 | |
82 | |
83 | if (ipv4_fd < 0) |
83 | if (ipv4_fd < 0) |
84 | return -1; |
84 | return -1; |
85 | |
85 | |
86 | #ifdef IP_MTU_DISCOVER |
86 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
|
|
87 | |
|
|
88 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
87 | // this I really consider a linux bug. I am neither connected |
89 | // this I really consider a linux bug. I am neither connected |
88 | // nor do I fragment myself. Linux still sets DF and doesn't |
90 | // nor do I fragment myself. Linux still sets DF and doesn't |
89 | // fragment for me sometimes. |
91 | // fragment for me sometimes. |
90 | { |
92 | { |
91 | int oval = IP_PMTUDISC_DONT; |
93 | int oval = IP_PMTUDISC_DONT; |
… | |
… | |
96 | sockinfo si (THISNODE, PROT_IPv4); |
98 | sockinfo si (THISNODE, PROT_IPv4); |
97 | |
99 | |
98 | if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
100 | if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
99 | { |
101 | { |
100 | slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); |
102 | slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); |
101 | exit (1); |
103 | exit (EXIT_FAILURE); |
102 | } |
104 | } |
103 | |
105 | |
104 | ipv4_ev_watcher.start (ipv4_fd, POLLIN); |
106 | ipv4_ev_watcher.start (ipv4_fd, EVENT_READ); |
105 | } |
107 | } |
106 | |
108 | |
107 | udpv4_fd = -1; |
109 | udpv4_fd = -1; |
108 | |
110 | |
109 | if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) |
111 | if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) |
110 | { |
112 | { |
111 | udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
113 | udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
112 | |
114 | |
113 | if (udpv4_fd < 0) |
115 | if (udpv4_fd < 0) |
114 | return -1; |
116 | return -1; |
|
|
117 | |
|
|
118 | fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); |
115 | |
119 | |
116 | // standard daemon practise... |
120 | // standard daemon practise... |
117 | { |
121 | { |
118 | int oval = 1; |
122 | int oval = 1; |
119 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
123 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
120 | } |
124 | } |
121 | |
125 | |
122 | #ifdef IP_MTU_DISCOVER |
126 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
123 | // this I really consider a linux bug. I am neither connected |
127 | // this I really consider a linux bug. I am neither connected |
124 | // nor do I fragment myself. Linux still sets DF and doesn't |
128 | // nor do I fragment myself. Linux still sets DF and doesn't |
125 | // fragment for me sometimes. |
129 | // fragment for me sometimes. |
126 | { |
130 | { |
127 | int oval = IP_PMTUDISC_DONT; |
131 | int oval = IP_PMTUDISC_DONT; |
… | |
… | |
132 | sockinfo si (THISNODE, PROT_UDPv4); |
136 | sockinfo si (THISNODE, PROT_UDPv4); |
133 | |
137 | |
134 | if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) |
138 | if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) |
135 | { |
139 | { |
136 | slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); |
140 | slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); |
137 | exit (1); |
141 | exit (EXIT_FAILURE); |
138 | } |
142 | } |
139 | |
143 | |
140 | udpv4_ev_watcher.start (udpv4_fd, POLLIN); |
144 | udpv4_ev_watcher.start (udpv4_fd, EVENT_READ); |
141 | } |
145 | } |
142 | |
146 | |
143 | icmpv4_fd = -1; |
147 | icmpv4_fd = -1; |
144 | |
148 | |
145 | #if ENABLE_ICMP |
149 | #if ENABLE_ICMP |
… | |
… | |
147 | { |
151 | { |
148 | icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); |
152 | icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); |
149 | |
153 | |
150 | if (icmpv4_fd < 0) |
154 | if (icmpv4_fd < 0) |
151 | return -1; |
155 | return -1; |
|
|
156 | |
|
|
157 | fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); |
152 | |
158 | |
153 | #ifdef ICMP_FILTER |
159 | #ifdef ICMP_FILTER |
154 | { |
160 | { |
155 | icmp_filter oval; |
161 | icmp_filter oval; |
156 | oval.data = 0xffffffff; |
162 | oval.data = 0xffffffff; |
… | |
… | |
159 | |
165 | |
160 | setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
166 | setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
161 | } |
167 | } |
162 | #endif |
168 | #endif |
163 | |
169 | |
164 | #ifdef IP_MTU_DISCOVER |
170 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
165 | // this I really consider a linux bug. I am neither connected |
171 | // this I really consider a linux bug. I am neither connected |
166 | // nor do I fragment myself. Linux still sets DF and doesn't |
172 | // nor do I fragment myself. Linux still sets DF and doesn't |
167 | // fragment for me sometimes. |
173 | // fragment for me sometimes. |
168 | { |
174 | { |
169 | int oval = IP_PMTUDISC_DONT; |
175 | int oval = IP_PMTUDISC_DONT; |
… | |
… | |
174 | sockinfo si (THISNODE, PROT_ICMPv4); |
180 | sockinfo si (THISNODE, PROT_ICMPv4); |
175 | |
181 | |
176 | if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ())) |
182 | if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ())) |
177 | { |
183 | { |
178 | slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); |
184 | slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); |
179 | exit (1); |
185 | exit (EXIT_FAILURE); |
180 | } |
186 | } |
181 | |
187 | |
182 | icmpv4_ev_watcher.start (icmpv4_fd, POLLIN); |
188 | icmpv4_ev_watcher.start (icmpv4_fd, EVENT_READ); |
183 | } |
189 | } |
184 | #endif |
190 | #endif |
185 | |
191 | |
186 | tcpv4_fd = -1; |
192 | tcpv4_fd = -1; |
187 | |
193 | |
… | |
… | |
190 | { |
196 | { |
191 | tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
197 | tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
192 | |
198 | |
193 | if (tcpv4_fd < 0) |
199 | if (tcpv4_fd < 0) |
194 | return -1; |
200 | return -1; |
|
|
201 | |
|
|
202 | fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); |
195 | |
203 | |
196 | // standard daemon practise... |
204 | // standard daemon practise... |
197 | { |
205 | { |
198 | int oval = 1; |
206 | int oval = 1; |
199 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
207 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
… | |
… | |
202 | sockinfo si (THISNODE, PROT_TCPv4); |
210 | sockinfo si (THISNODE, PROT_TCPv4); |
203 | |
211 | |
204 | if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ())) |
212 | if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ())) |
205 | { |
213 | { |
206 | slog (L_ERR, _("can't bind tcpv4 on %s: %s"), (const char *)si, strerror (errno)); |
214 | slog (L_ERR, _("can't bind tcpv4 on %s: %s"), (const char *)si, strerror (errno)); |
207 | exit (1); |
215 | exit (EXIT_FAILURE); |
208 | } |
216 | } |
209 | |
217 | |
210 | if (listen (tcpv4_fd, 5)) |
218 | if (listen (tcpv4_fd, 5)) |
211 | { |
219 | { |
212 | slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno)); |
220 | slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno)); |
213 | exit (1); |
221 | exit (EXIT_FAILURE); |
214 | } |
222 | } |
215 | |
223 | |
216 | tcpv4_ev_watcher.start (tcpv4_fd, POLLIN); |
224 | tcpv4_ev_watcher.start (tcpv4_fd, EVENT_READ); |
217 | } |
225 | } |
218 | #endif |
226 | #endif |
219 | |
227 | |
220 | tap = new tap_device (); |
228 | tap = new tap_device (); |
221 | if (!tap) //D this, of course, never catches |
229 | if (!tap) //D this, of course, never catches |
222 | { |
230 | { |
223 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
231 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
224 | exit (1); |
232 | exit (EXIT_FAILURE); |
225 | } |
233 | } |
226 | |
234 | |
227 | run_script (run_script_cb (this, &vpn::script_if_up), true); |
235 | run_script (run_script_cb (this, &vpn::script_if_up), true); |
228 | |
236 | |
229 | tap_ev_watcher.start (tap->fd, POLLIN); |
237 | tap_ev_watcher.start (tap->fd, EVENT_READ); |
230 | |
238 | |
231 | reconnect_all (); |
239 | reconnect_all (); |
232 | |
240 | |
233 | return 0; |
241 | return 0; |
234 | } |
242 | } |
… | |
… | |
262 | } |
270 | } |
263 | |
271 | |
264 | bool |
272 | bool |
265 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
273 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
266 | { |
274 | { |
|
|
275 | #if defined(SOL_IP) && defined(IP_TOS) |
267 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
276 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
277 | #endif |
268 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
278 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
269 | |
279 | |
270 | return true; |
280 | return true; |
271 | } |
281 | } |
272 | |
282 | |
… | |
… | |
291 | sum += (sum >> 16); // carry |
301 | sum += (sum >> 16); // carry |
292 | |
302 | |
293 | return ~sum; |
303 | return ~sum; |
294 | } |
304 | } |
295 | |
305 | |
|
|
306 | #if ENABLE_ICMP |
296 | bool |
307 | bool |
297 | vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
308 | vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
298 | { |
309 | { |
|
|
310 | #if defined(SOL_IP) && defined(IP_TOS) |
299 | setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
311 | setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
312 | #endif |
300 | |
313 | |
301 | pkt->unshift_hdr (4); |
314 | pkt->unshift_hdr (4); |
302 | |
315 | |
303 | icmphdr *hdr = (icmphdr *)&((*pkt)[0]); |
316 | icmp_header *hdr = (icmp_header *)&((*pkt)[0]); |
304 | hdr->type = ::conf.icmp_type; |
317 | hdr->type = ::conf.icmp_type; |
305 | hdr->code = 255; |
318 | hdr->code = 255; |
306 | hdr->checksum = 0; |
319 | hdr->checksum = 0; |
307 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
320 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
308 | |
321 | |
309 | sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
322 | sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
310 | |
323 | |
311 | return true; |
324 | return true; |
312 | } |
325 | } |
|
|
326 | #endif |
313 | |
327 | |
314 | bool |
328 | bool |
315 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
329 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
316 | { |
330 | { |
|
|
331 | #if defined(SOL_IP) && defined(IP_TOS) |
317 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
332 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
333 | #endif |
318 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
334 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
319 | |
335 | |
320 | return true; |
336 | return true; |
|
|
337 | } |
|
|
338 | |
|
|
339 | void |
|
|
340 | vpn::inject_data_packet (tap_packet *pkt, int dst) |
|
|
341 | { |
|
|
342 | if (dst) |
|
|
343 | { |
|
|
344 | // unicast |
|
|
345 | if (dst != THISNODE->id) |
|
|
346 | conns[dst - 1]->inject_data_packet (pkt); |
|
|
347 | } |
|
|
348 | else |
|
|
349 | { |
|
|
350 | // broadcast, this is ugly, but due to the security policy |
|
|
351 | // we have to connect to all hosts... |
|
|
352 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
|
|
353 | if ((*c)->conf != THISNODE) |
|
|
354 | (*c)->inject_data_packet (pkt, true); |
|
|
355 | } |
321 | } |
356 | } |
322 | |
357 | |
323 | void |
358 | void |
324 | vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
359 | vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
325 | { |
360 | { |
… | |
… | |
339 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
374 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
340 | else |
375 | else |
341 | { |
376 | { |
342 | connection *c = conns[src - 1]; |
377 | connection *c = conns[src - 1]; |
343 | |
378 | |
344 | if (dst == 0 && !THISNODE->routerprio) |
379 | if (dst == 0) |
345 | slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), |
380 | slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), |
346 | c->conf->nodename, (const char *)rsi); |
381 | c->conf->nodename, (const char *)rsi); |
347 | else if (dst != 0 && dst != THISNODE->id) |
382 | else if (dst != THISNODE->id) |
348 | { |
383 | { |
349 | if (THISNODE->routerprio) |
384 | if (THISNODE->routerprio) |
350 | // the tos setting gets lost here. who cares. |
385 | // the tos setting gets lost here. who cares. |
351 | conns[dst - 1]->inject_vpn_packet (pkt); |
386 | conns[dst - 1]->inject_vpn_packet (pkt); |
352 | else |
387 | else |
… | |
… | |
361 | } |
396 | } |
362 | |
397 | |
363 | void |
398 | void |
364 | vpn::ipv4_ev (io_watcher &w, short revents) |
399 | vpn::ipv4_ev (io_watcher &w, short revents) |
365 | { |
400 | { |
366 | if (revents & (POLLIN | POLLERR)) |
401 | if (revents & EVENT_READ) |
367 | { |
402 | { |
368 | vpn_packet *pkt = new vpn_packet; |
403 | vpn_packet *pkt = new vpn_packet; |
369 | struct sockaddr_in sa; |
404 | struct sockaddr_in sa; |
370 | socklen_t sa_len = sizeof (sa); |
405 | socklen_t sa_len = sizeof (sa); |
371 | int len; |
406 | int len; |
… | |
… | |
390 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
425 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
391 | } |
426 | } |
392 | |
427 | |
393 | delete pkt; |
428 | delete pkt; |
394 | } |
429 | } |
395 | else if (revents & POLLHUP) |
|
|
396 | { |
|
|
397 | // this cannot ;) happen on udp sockets |
|
|
398 | slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); |
|
|
399 | exit (1); |
|
|
400 | } |
|
|
401 | else |
430 | else |
402 | { |
431 | { |
403 | slog (L_ERR, |
432 | slog (L_ERR, |
404 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
433 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
405 | revents); |
434 | revents); |
406 | exit (1); |
435 | exit (EXIT_FAILURE); |
407 | } |
436 | } |
408 | } |
437 | } |
409 | |
438 | |
|
|
439 | #if ENABLE_ICMP |
410 | void |
440 | void |
411 | vpn::icmpv4_ev (io_watcher &w, short revents) |
441 | vpn::icmpv4_ev (io_watcher &w, short revents) |
412 | { |
442 | { |
413 | if (revents & (POLLIN | POLLERR)) |
443 | if (revents & EVENT_READ) |
414 | { |
444 | { |
415 | vpn_packet *pkt = new vpn_packet; |
445 | vpn_packet *pkt = new vpn_packet; |
416 | struct sockaddr_in sa; |
446 | struct sockaddr_in sa; |
417 | socklen_t sa_len = sizeof (sa); |
447 | socklen_t sa_len = sizeof (sa); |
418 | int len; |
448 | int len; |
… | |
… | |
423 | |
453 | |
424 | if (len > 0) |
454 | if (len > 0) |
425 | { |
455 | { |
426 | pkt->len = len; |
456 | pkt->len = len; |
427 | |
457 | |
428 | icmphdr *hdr = (icmphdr *)&((*pkt)[IP_OVERHEAD]); |
458 | icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]); |
429 | |
459 | |
430 | if (hdr->type == ::conf.icmp_type |
460 | if (hdr->type == ::conf.icmp_type |
431 | && hdr->code == 255) |
461 | && hdr->code == 255) |
432 | { |
462 | { |
433 | // raw sockets deliver the ipv4, but don't expect it on sends |
463 | // raw sockets deliver the ipv4, but don't expect it on sends |
… | |
… | |
443 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
473 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
444 | } |
474 | } |
445 | |
475 | |
446 | delete pkt; |
476 | delete pkt; |
447 | } |
477 | } |
448 | else if (revents & POLLHUP) |
|
|
449 | { |
|
|
450 | // this cannot ;) happen on udp sockets |
|
|
451 | slog (L_ERR, _("FATAL: POLLHUP on icmpv4 fd, terminating.")); |
|
|
452 | exit (1); |
|
|
453 | } |
|
|
454 | else |
478 | else |
455 | { |
479 | { |
456 | slog (L_ERR, |
480 | slog (L_ERR, |
457 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
481 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
458 | revents); |
482 | revents); |
459 | exit (1); |
483 | exit (EXIT_FAILURE); |
460 | } |
484 | } |
461 | } |
485 | } |
|
|
486 | #endif |
462 | |
487 | |
463 | void |
488 | void |
464 | vpn::udpv4_ev (io_watcher &w, short revents) |
489 | vpn::udpv4_ev (io_watcher &w, short revents) |
465 | { |
490 | { |
466 | if (revents & (POLLIN | POLLERR)) |
491 | if (revents & EVENT_READ) |
467 | { |
492 | { |
468 | vpn_packet *pkt = new vpn_packet; |
493 | vpn_packet *pkt = new vpn_packet; |
469 | struct sockaddr_in sa; |
494 | struct sockaddr_in sa; |
470 | socklen_t sa_len = sizeof (sa); |
495 | socklen_t sa_len = sizeof (sa); |
471 | int len; |
496 | int len; |
… | |
… | |
486 | slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); |
511 | slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); |
487 | } |
512 | } |
488 | |
513 | |
489 | delete pkt; |
514 | delete pkt; |
490 | } |
515 | } |
491 | else if (revents & POLLHUP) |
|
|
492 | { |
|
|
493 | // this cannot ;) happen on udp sockets |
|
|
494 | slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating.")); |
|
|
495 | exit (1); |
|
|
496 | } |
|
|
497 | else |
516 | else |
498 | { |
517 | { |
499 | slog (L_ERR, |
518 | slog (L_ERR, |
500 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
519 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
501 | revents); |
520 | revents); |
502 | exit (1); |
521 | exit (EXIT_FAILURE); |
503 | } |
522 | } |
504 | } |
523 | } |
505 | |
524 | |
506 | void |
525 | void |
507 | vpn::tap_ev (io_watcher &w, short revents) |
526 | vpn::tap_ev (io_watcher &w, short revents) |
508 | { |
527 | { |
509 | if (revents & POLLIN) |
528 | if (revents & EVENT_READ) |
510 | { |
529 | { |
511 | /* process data */ |
530 | /* process data */ |
512 | tap_packet *pkt; |
531 | tap_packet *pkt; |
513 | |
532 | |
514 | pkt = tap->recv (); |
533 | pkt = tap->recv (); |
515 | |
534 | |
|
|
535 | if (!pkt) |
|
|
536 | return; |
|
|
537 | |
|
|
538 | if (pkt->len > 14) |
|
|
539 | { |
516 | int dst = mac2id (pkt->dst); |
540 | int dst = mac2id (pkt->dst); |
517 | int src = mac2id (pkt->src); |
541 | int src = mac2id (pkt->src); |
518 | |
542 | |
519 | if (src != THISNODE->id) |
543 | if (src != THISNODE->id) |
520 | { |
|
|
521 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating.")); |
|
|
522 | exit (1); |
|
|
523 | } |
|
|
524 | |
|
|
525 | if (dst == THISNODE->id) |
|
|
526 | { |
|
|
527 | slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating.")); |
|
|
528 | exit (1); |
|
|
529 | } |
|
|
530 | |
|
|
531 | if (dst > conns.size ()) |
|
|
532 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
|
|
533 | else |
|
|
534 | { |
|
|
535 | if (dst) |
|
|
536 | { |
544 | { |
537 | // unicast |
545 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting.")); |
538 | if (dst != THISNODE->id) |
546 | exit (EXIT_FAILURE); |
539 | conns[dst - 1]->inject_data_packet (pkt); |
|
|
540 | } |
547 | } |
|
|
548 | |
|
|
549 | if (dst == THISNODE->id) |
|
|
550 | { |
|
|
551 | slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting.")); |
|
|
552 | exit (EXIT_FAILURE); |
|
|
553 | } |
|
|
554 | |
|
|
555 | if (dst > conns.size ()) |
|
|
556 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
541 | else |
557 | else |
542 | { |
|
|
543 | // broadcast, first check router, then self, then english |
|
|
544 | connection *router = find_router (); |
|
|
545 | |
|
|
546 | if (router) |
|
|
547 | router->inject_data_packet (pkt, true); |
558 | inject_data_packet (pkt, dst); |
548 | else |
|
|
549 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
|
|
550 | if ((*c)->conf != THISNODE) |
|
|
551 | (*c)->inject_data_packet (pkt); |
|
|
552 | } |
|
|
553 | } |
559 | } |
554 | |
560 | |
555 | delete pkt; |
561 | delete pkt; |
556 | } |
|
|
557 | else if (revents & (POLLHUP | POLLERR)) |
|
|
558 | { |
|
|
559 | slog (L_ERR, _("FATAL: POLLHUP or POLLERR on network device fd, terminating.")); |
|
|
560 | exit (1); |
|
|
561 | } |
562 | } |
562 | else |
563 | else |
563 | abort (); |
564 | abort (); |
564 | } |
565 | } |
565 | |
566 | |
… | |
… | |
571 | if (events & EVENT_SHUTDOWN) |
572 | if (events & EVENT_SHUTDOWN) |
572 | { |
573 | { |
573 | slog (L_INFO, _("preparing shutdown...")); |
574 | slog (L_INFO, _("preparing shutdown...")); |
574 | |
575 | |
575 | shutdown_all (); |
576 | shutdown_all (); |
576 | |
|
|
577 | remove_pid (pidfilename); |
577 | remove_pid (pidfilename); |
578 | |
|
|
579 | slog (L_INFO, _("terminating")); |
578 | slog (L_INFO, _("terminating")); |
580 | |
579 | exit (EXIT_SUCCESS); |
581 | exit (0); |
|
|
582 | } |
580 | } |
583 | |
581 | |
584 | if (events & EVENT_RECONNECT) |
582 | if (events & EVENT_RECONNECT) |
585 | { |
583 | { |
586 | slog (L_INFO, _("forced reconnect")); |
584 | slog (L_INFO, _("forced reconnect")); |
… | |
… | |
588 | reconnect_all (); |
586 | reconnect_all (); |
589 | } |
587 | } |
590 | |
588 | |
591 | events = 0; |
589 | events = 0; |
592 | } |
590 | } |
593 | |
|
|
594 | w.at = TSTAMP_CANCEL; |
|
|
595 | } |
591 | } |
596 | |
592 | |
597 | void |
593 | void |
598 | vpn::shutdown_all () |
594 | vpn::shutdown_all () |
599 | { |
595 | { |
… | |
… | |
623 | } |
619 | } |
624 | } |
620 | } |
625 | |
621 | |
626 | connection *vpn::find_router () |
622 | connection *vpn::find_router () |
627 | { |
623 | { |
628 | u32 prio = 0; |
624 | u32 prio = 1; |
629 | connection *router = 0; |
625 | connection *router = 0; |
630 | |
626 | |
631 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
627 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
632 | { |
628 | { |
633 | connection *c = *i; |
629 | connection *c = *i; |
634 | |
630 | |
635 | if (c->conf->routerprio > prio |
631 | if (c->conf->routerprio > prio |
636 | && c->connectmode == conf_node::C_ALWAYS |
632 | && c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use |
637 | && c->conf != THISNODE |
|
|
638 | && c->ictx && c->octx) |
633 | && c->ictx && c->octx |
|
|
634 | && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode |
639 | { |
635 | { |
640 | prio = c->conf->routerprio; |
636 | prio = c->conf->routerprio; |
641 | router = c; |
637 | router = c; |
642 | } |
638 | } |
643 | } |
639 | } |
… | |
… | |
652 | if (c) |
648 | if (c) |
653 | c->send_connect_request (id); |
649 | c->send_connect_request (id); |
654 | else |
650 | else |
655 | // no router found, aggressively connect to all routers |
651 | // no router found, aggressively connect to all routers |
656 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
652 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
657 | if ((*i)->conf->routerprio) |
653 | if ((*i)->conf->routerprio && (*i)->conf != THISNODE) |
658 | (*i)->establish_connection (); |
654 | (*i)->establish_connection (); |
659 | } |
655 | } |
660 | |
656 | |
661 | void |
657 | void |
662 | connection::dump_status () |
658 | connection::dump_status () |