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/ip_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); |
|
|
222 | } |
|
|
223 | |
|
|
224 | tcpv4_ev_watcher.start (tcpv4_fd, EVENT_READ); |
|
|
225 | } |
|
|
226 | #endif |
|
|
227 | |
|
|
228 | #if ENABLE_DNS |
|
|
229 | if (THISNODE->protocols & PROT_DNSv4) |
|
|
230 | { |
|
|
231 | dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
|
|
232 | |
|
|
233 | if (dnsv4_fd < 0) |
|
|
234 | return -1; |
|
|
235 | |
|
|
236 | // standard daemon practise... |
|
|
237 | { |
|
|
238 | int oval = 1; |
|
|
239 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
|
|
240 | } |
|
|
241 | |
|
|
242 | sockinfo si (THISNODE, PROT_DNSv4); |
|
|
243 | |
|
|
244 | if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ())) |
214 | } |
245 | { |
|
|
246 | slog (L_ERR, _("can't bind dnsv4 on %s: %s"), (const char *)si, strerror (errno)); |
|
|
247 | exit (EXIT_FAILURE); |
|
|
248 | } |
215 | |
249 | |
216 | tcpv4_ev_watcher.start (tcpv4_fd, POLLIN); |
250 | dnsv4_ev_watcher.start (dnsv4_fd, EVENT_READ); |
217 | } |
251 | } |
218 | #endif |
252 | #endif |
219 | |
253 | |
220 | tap = new tap_device (); |
254 | tap = new tap_device (); |
221 | if (!tap) //D this, of course, never catches |
255 | if (!tap) //D this, of course, never catches |
222 | { |
256 | { |
223 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
257 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
224 | exit (1); |
258 | exit (EXIT_FAILURE); |
225 | } |
259 | } |
226 | |
260 | |
227 | run_script (run_script_cb (this, &vpn::script_if_up), true); |
261 | run_script (run_script_cb (this, &vpn::script_if_up), true); |
228 | |
262 | |
229 | tap_ev_watcher.start (tap->fd, POLLIN); |
263 | tap_ev_watcher.start (tap->fd, EVENT_READ); |
230 | |
264 | |
231 | reconnect_all (); |
265 | reconnect_all (); |
232 | |
266 | |
233 | return 0; |
267 | return 0; |
234 | } |
268 | } |
… | |
… | |
253 | #if ENABLE_ICMP |
287 | #if ENABLE_ICMP |
254 | case PROT_ICMPv4: |
288 | case PROT_ICMPv4: |
255 | return send_icmpv4_packet (pkt, si, tos); |
289 | return send_icmpv4_packet (pkt, si, tos); |
256 | #endif |
290 | #endif |
257 | |
291 | |
|
|
292 | #if ENABLE_DNS |
|
|
293 | case PROT_DNSv4: |
|
|
294 | return send_dnsv4_packet (pkt, si, tos); |
|
|
295 | #endif |
|
|
296 | |
258 | default: |
297 | default: |
259 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); |
298 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); |
260 | return false; |
299 | return false; |
261 | } |
300 | } |
262 | } |
301 | } |
263 | |
302 | |
264 | bool |
303 | bool |
265 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
304 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
266 | { |
305 | { |
|
|
306 | #if defined(SOL_IP) && defined(IP_TOS) |
267 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
307 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
308 | #endif |
268 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
309 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
269 | |
310 | |
270 | return true; |
311 | return true; |
271 | } |
312 | } |
272 | |
313 | |
… | |
… | |
291 | sum += (sum >> 16); // carry |
332 | sum += (sum >> 16); // carry |
292 | |
333 | |
293 | return ~sum; |
334 | return ~sum; |
294 | } |
335 | } |
295 | |
336 | |
|
|
337 | #if ENABLE_ICMP |
296 | bool |
338 | bool |
297 | vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
339 | vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
298 | { |
340 | { |
|
|
341 | #if defined(SOL_IP) && defined(IP_TOS) |
299 | setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
342 | setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
343 | #endif |
300 | |
344 | |
301 | pkt->unshift_hdr (4); |
345 | pkt->unshift_hdr (4); |
302 | |
346 | |
303 | icmphdr *hdr = (icmphdr *)&((*pkt)[0]); |
347 | icmp_header *hdr = (icmp_header *)&((*pkt)[0]); |
304 | hdr->type = ::conf.icmp_type; |
348 | hdr->type = ::conf.icmp_type; |
305 | hdr->code = 255; |
349 | hdr->code = 255; |
306 | hdr->checksum = 0; |
350 | hdr->checksum = 0; |
307 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
351 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
308 | |
352 | |
309 | sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
353 | sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
310 | |
354 | |
311 | return true; |
355 | return true; |
312 | } |
356 | } |
|
|
357 | #endif |
313 | |
358 | |
314 | bool |
359 | bool |
315 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
360 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
316 | { |
361 | { |
|
|
362 | #if defined(SOL_IP) && defined(IP_TOS) |
317 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
363 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
364 | #endif |
318 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
365 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
319 | |
366 | |
320 | return true; |
367 | return true; |
|
|
368 | } |
|
|
369 | |
|
|
370 | void |
|
|
371 | vpn::inject_data_packet (tap_packet *pkt, int dst) |
|
|
372 | { |
|
|
373 | if (dst) |
|
|
374 | { |
|
|
375 | // unicast |
|
|
376 | if (dst != THISNODE->id) |
|
|
377 | conns[dst - 1]->inject_data_packet (pkt); |
|
|
378 | } |
|
|
379 | else |
|
|
380 | { |
|
|
381 | // broadcast, this is ugly, but due to the security policy |
|
|
382 | // we have to connect to all hosts... |
|
|
383 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
|
|
384 | if ((*c)->conf != THISNODE) |
|
|
385 | (*c)->inject_data_packet (pkt, true); |
|
|
386 | } |
321 | } |
387 | } |
322 | |
388 | |
323 | void |
389 | void |
324 | vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
390 | vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
325 | { |
391 | { |
… | |
… | |
339 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
405 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
340 | else |
406 | else |
341 | { |
407 | { |
342 | connection *c = conns[src - 1]; |
408 | connection *c = conns[src - 1]; |
343 | |
409 | |
344 | if (dst == 0 && !THISNODE->routerprio) |
410 | if (dst == 0) |
345 | slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), |
411 | slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), |
346 | c->conf->nodename, (const char *)rsi); |
412 | c->conf->nodename, (const char *)rsi); |
347 | else if (dst != 0 && dst != THISNODE->id) |
413 | else if (dst != THISNODE->id) |
348 | { |
414 | { |
349 | if (THISNODE->routerprio) |
415 | if (THISNODE->routerprio) |
350 | // the tos setting gets lost here. who cares. |
416 | // the tos setting gets lost here. who cares. |
351 | conns[dst - 1]->inject_vpn_packet (pkt); |
417 | conns[dst - 1]->inject_vpn_packet (pkt); |
352 | else |
418 | else |
… | |
… | |
361 | } |
427 | } |
362 | |
428 | |
363 | void |
429 | void |
364 | vpn::ipv4_ev (io_watcher &w, short revents) |
430 | vpn::ipv4_ev (io_watcher &w, short revents) |
365 | { |
431 | { |
366 | if (revents & (POLLIN | POLLERR)) |
432 | if (revents & EVENT_READ) |
367 | { |
433 | { |
368 | vpn_packet *pkt = new vpn_packet; |
434 | vpn_packet *pkt = new vpn_packet; |
369 | struct sockaddr_in sa; |
435 | struct sockaddr_in sa; |
370 | socklen_t sa_len = sizeof (sa); |
436 | socklen_t sa_len = sizeof (sa); |
371 | int len; |
437 | int len; |
… | |
… | |
390 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
456 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
391 | } |
457 | } |
392 | |
458 | |
393 | delete pkt; |
459 | delete pkt; |
394 | } |
460 | } |
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 |
461 | else |
402 | { |
462 | { |
403 | slog (L_ERR, |
463 | slog (L_ERR, |
404 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
464 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
405 | revents); |
465 | revents); |
406 | exit (1); |
466 | exit (EXIT_FAILURE); |
407 | } |
467 | } |
408 | } |
468 | } |
409 | |
469 | |
|
|
470 | #if ENABLE_ICMP |
410 | void |
471 | void |
411 | vpn::icmpv4_ev (io_watcher &w, short revents) |
472 | vpn::icmpv4_ev (io_watcher &w, short revents) |
412 | { |
473 | { |
413 | if (revents & (POLLIN | POLLERR)) |
474 | if (revents & EVENT_READ) |
414 | { |
475 | { |
415 | vpn_packet *pkt = new vpn_packet; |
476 | vpn_packet *pkt = new vpn_packet; |
416 | struct sockaddr_in sa; |
477 | struct sockaddr_in sa; |
417 | socklen_t sa_len = sizeof (sa); |
478 | socklen_t sa_len = sizeof (sa); |
418 | int len; |
479 | int len; |
… | |
… | |
423 | |
484 | |
424 | if (len > 0) |
485 | if (len > 0) |
425 | { |
486 | { |
426 | pkt->len = len; |
487 | pkt->len = len; |
427 | |
488 | |
428 | icmphdr *hdr = (icmphdr *)&((*pkt)[IP_OVERHEAD]); |
489 | icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]); |
429 | |
490 | |
430 | if (hdr->type == ::conf.icmp_type |
491 | if (hdr->type == ::conf.icmp_type |
431 | && hdr->code == 255) |
492 | && hdr->code == 255) |
432 | { |
493 | { |
433 | // raw sockets deliver the ipv4, but don't expect it on sends |
494 | // raw sockets deliver the ipv4, but don't expect it on sends |
… | |
… | |
443 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
504 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
444 | } |
505 | } |
445 | |
506 | |
446 | delete pkt; |
507 | delete pkt; |
447 | } |
508 | } |
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 |
509 | else |
455 | { |
510 | { |
456 | slog (L_ERR, |
511 | slog (L_ERR, |
457 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
512 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
458 | revents); |
513 | revents); |
459 | exit (1); |
514 | exit (EXIT_FAILURE); |
460 | } |
515 | } |
461 | } |
516 | } |
|
|
517 | #endif |
462 | |
518 | |
463 | void |
519 | void |
464 | vpn::udpv4_ev (io_watcher &w, short revents) |
520 | vpn::udpv4_ev (io_watcher &w, short revents) |
465 | { |
521 | { |
466 | if (revents & (POLLIN | POLLERR)) |
522 | if (revents & EVENT_READ) |
467 | { |
523 | { |
468 | vpn_packet *pkt = new vpn_packet; |
524 | vpn_packet *pkt = new vpn_packet; |
469 | struct sockaddr_in sa; |
525 | struct sockaddr_in sa; |
470 | socklen_t sa_len = sizeof (sa); |
526 | socklen_t sa_len = sizeof (sa); |
471 | int len; |
527 | int len; |
… | |
… | |
486 | slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); |
542 | slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); |
487 | } |
543 | } |
488 | |
544 | |
489 | delete pkt; |
545 | delete pkt; |
490 | } |
546 | } |
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 |
547 | else |
498 | { |
548 | { |
499 | slog (L_ERR, |
549 | slog (L_ERR, |
500 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
550 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
501 | revents); |
551 | revents); |
502 | exit (1); |
552 | exit (EXIT_FAILURE); |
503 | } |
553 | } |
504 | } |
554 | } |
505 | |
555 | |
506 | void |
556 | void |
507 | vpn::tap_ev (io_watcher &w, short revents) |
557 | vpn::tap_ev (io_watcher &w, short revents) |
508 | { |
558 | { |
509 | if (revents & POLLIN) |
559 | if (revents & EVENT_READ) |
510 | { |
560 | { |
511 | /* process data */ |
561 | /* process data */ |
512 | tap_packet *pkt; |
562 | tap_packet *pkt; |
513 | |
563 | |
514 | pkt = tap->recv (); |
564 | pkt = tap->recv (); |
515 | |
565 | |
|
|
566 | if (!pkt) |
|
|
567 | return; |
|
|
568 | |
|
|
569 | if (pkt->len > 14) |
|
|
570 | { |
516 | int dst = mac2id (pkt->dst); |
571 | int dst = mac2id (pkt->dst); |
517 | int src = mac2id (pkt->src); |
572 | int src = mac2id (pkt->src); |
518 | |
573 | |
519 | if (src != THISNODE->id) |
574 | 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 | { |
575 | { |
537 | // unicast |
576 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting.")); |
538 | if (dst != THISNODE->id) |
577 | exit (EXIT_FAILURE); |
539 | conns[dst - 1]->inject_data_packet (pkt); |
|
|
540 | } |
578 | } |
|
|
579 | |
|
|
580 | if (dst == THISNODE->id) |
|
|
581 | { |
|
|
582 | slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting.")); |
|
|
583 | exit (EXIT_FAILURE); |
|
|
584 | } |
|
|
585 | |
|
|
586 | if (dst > conns.size ()) |
|
|
587 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
541 | else |
588 | 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); |
589 | 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 | } |
590 | } |
554 | |
591 | |
555 | delete pkt; |
592 | 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 | } |
593 | } |
562 | else |
594 | else |
563 | abort (); |
595 | abort (); |
564 | } |
596 | } |
565 | |
597 | |
… | |
… | |
571 | if (events & EVENT_SHUTDOWN) |
603 | if (events & EVENT_SHUTDOWN) |
572 | { |
604 | { |
573 | slog (L_INFO, _("preparing shutdown...")); |
605 | slog (L_INFO, _("preparing shutdown...")); |
574 | |
606 | |
575 | shutdown_all (); |
607 | shutdown_all (); |
576 | |
|
|
577 | remove_pid (pidfilename); |
608 | remove_pid (conf.pidfilename); |
578 | |
|
|
579 | slog (L_INFO, _("terminating")); |
609 | slog (L_INFO, _("terminating")); |
580 | |
610 | exit (EXIT_SUCCESS); |
581 | exit (0); |
|
|
582 | } |
611 | } |
583 | |
612 | |
584 | if (events & EVENT_RECONNECT) |
613 | if (events & EVENT_RECONNECT) |
585 | { |
614 | { |
586 | slog (L_INFO, _("forced reconnect")); |
615 | slog (L_INFO, _("forced reconnect")); |
… | |
… | |
588 | reconnect_all (); |
617 | reconnect_all (); |
589 | } |
618 | } |
590 | |
619 | |
591 | events = 0; |
620 | events = 0; |
592 | } |
621 | } |
593 | |
|
|
594 | w.at = TSTAMP_CANCEL; |
|
|
595 | } |
622 | } |
596 | |
623 | |
597 | void |
624 | void |
598 | vpn::shutdown_all () |
625 | vpn::shutdown_all () |
599 | { |
626 | { |
… | |
… | |
623 | } |
650 | } |
624 | } |
651 | } |
625 | |
652 | |
626 | connection *vpn::find_router () |
653 | connection *vpn::find_router () |
627 | { |
654 | { |
628 | u32 prio = 0; |
655 | u32 prio = 1; |
629 | connection *router = 0; |
656 | connection *router = 0; |
630 | |
657 | |
631 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
658 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
632 | { |
659 | { |
633 | connection *c = *i; |
660 | connection *c = *i; |
634 | |
661 | |
635 | if (c->conf->routerprio > prio |
662 | if (c->conf->routerprio > prio |
636 | && c->connectmode == conf_node::C_ALWAYS |
663 | && 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) |
664 | && c->ictx && c->octx |
|
|
665 | && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode |
639 | { |
666 | { |
640 | prio = c->conf->routerprio; |
667 | prio = c->conf->routerprio; |
641 | router = c; |
668 | router = c; |
642 | } |
669 | } |
643 | } |
670 | } |
… | |
… | |
652 | if (c) |
679 | if (c) |
653 | c->send_connect_request (id); |
680 | c->send_connect_request (id); |
654 | else |
681 | else |
655 | // no router found, aggressively connect to all routers |
682 | // no router found, aggressively connect to all routers |
656 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
683 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
657 | if ((*i)->conf->routerprio) |
684 | if ((*i)->conf->routerprio && (*i)->conf != THISNODE) |
658 | (*i)->establish_connection (); |
685 | (*i)->establish_connection (); |
659 | } |
686 | } |
660 | |
687 | |
661 | void |
688 | void |
662 | connection::dump_status () |
689 | connection::dump_status () |
… | |
… | |
689 | , tcpv4_ev_watcher (this, &vpn::tcpv4_ev) |
716 | , tcpv4_ev_watcher (this, &vpn::tcpv4_ev) |
690 | #endif |
717 | #endif |
691 | #if ENABLE_ICMP |
718 | #if ENABLE_ICMP |
692 | , icmpv4_ev_watcher(this, &vpn::icmpv4_ev) |
719 | , icmpv4_ev_watcher(this, &vpn::icmpv4_ev) |
693 | #endif |
720 | #endif |
|
|
721 | #if ENABLE_DNS |
|
|
722 | , dnsv4_ev_watcher (this, &vpn::dnsv4_ev) |
|
|
723 | #endif |
694 | , tap_ev_watcher (this, &vpn::tap_ev) |
724 | , tap_ev_watcher (this, &vpn::tap_ev) |
695 | { |
725 | { |
696 | } |
726 | } |
697 | |
727 | |
698 | vpn::~vpn () |
728 | vpn::~vpn () |