1 |
pcg |
1.1 |
/* |
2 |
|
|
vpn.C -- handle the protocol, encryption, handshaking etc. |
3 |
pcg |
1.22 |
Copyright (C) 2003-2004 Marc Lehmann <pcg@goof.com> |
4 |
pcg |
1.1 |
|
5 |
|
|
This program is free software; you can redistribute it and/or modify |
6 |
|
|
it under the terms of the GNU General Public License as published by |
7 |
|
|
the Free Software Foundation; either version 2 of the License, or |
8 |
|
|
(at your option) any later version. |
9 |
|
|
|
10 |
|
|
This program is distributed in the hope that it will be useful, |
11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
GNU General Public License for more details. |
14 |
|
|
|
15 |
|
|
You should have received a copy of the GNU General Public License |
16 |
|
|
along with this program; if not, write to the Free Software |
17 |
|
|
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 |
|
|
*/ |
19 |
|
|
|
20 |
|
|
#include "config.h" |
21 |
|
|
|
22 |
|
|
#include <list> |
23 |
|
|
|
24 |
pcg |
1.23 |
#include <cstdio> |
25 |
|
|
#include <cstring> |
26 |
pcg |
1.1 |
#include <cstdlib> |
27 |
|
|
|
28 |
|
|
#include <sys/types.h> |
29 |
|
|
#include <sys/socket.h> |
30 |
|
|
#include <sys/wait.h> |
31 |
|
|
#include <errno.h> |
32 |
|
|
#include <time.h> |
33 |
|
|
#include <unistd.h> |
34 |
pcg |
1.11 |
#include <fcntl.h> |
35 |
pcg |
1.14 |
#include <sys/socket.h> |
36 |
pcg |
1.15 |
|
37 |
|
|
#include "netcompat.h" |
38 |
pcg |
1.1 |
|
39 |
|
|
#include "pidfile.h" |
40 |
|
|
|
41 |
|
|
#include "connection.h" |
42 |
|
|
#include "util.h" |
43 |
|
|
#include "vpn.h" |
44 |
|
|
|
45 |
pcg |
1.20 |
vpn network; // THE vpn (bad design...) |
46 |
|
|
|
47 |
pcg |
1.1 |
///////////////////////////////////////////////////////////////////////////// |
48 |
|
|
|
49 |
pcg |
1.2 |
const char *vpn::script_if_up () |
50 |
pcg |
1.1 |
{ |
51 |
|
|
// the tunnel device mtu should be the physical mtu - overhead |
52 |
|
|
// the tricky part is rounding to the cipher key blocksize |
53 |
|
|
int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD; |
54 |
|
|
mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion |
55 |
|
|
mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round |
56 |
|
|
mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
57 |
|
|
|
58 |
|
|
char *env; |
59 |
pcg |
1.21 |
asprintf (&env, "CONFBASE=%s", confbase); putenv (env); |
60 |
|
|
asprintf (&env, "NODENAME=%s", THISNODE->nodename); putenv (env); |
61 |
|
|
asprintf (&env, "NODEID=%d", THISNODE->id); putenv (env); |
62 |
|
|
asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env); |
63 |
|
|
asprintf (&env, "IFTYPE=%s", IFTYPE); putenv (env); |
64 |
|
|
asprintf (&env, "IFSUBTYPE=%s", IFSUBTYPE); putenv (env); |
65 |
|
|
asprintf (&env, "MTU=%d", mtu); putenv (env); |
66 |
pcg |
1.1 |
asprintf (&env, "MAC=%02x:%02x:%02x:%02x:%02x:%02x", |
67 |
|
|
0xfe, 0xfd, 0x80, 0x00, THISNODE->id >> 8, |
68 |
|
|
THISNODE->id & 0xff); |
69 |
|
|
putenv (env); |
70 |
|
|
|
71 |
|
|
return ::conf.script_if_up ? ::conf.script_if_up : "if-up"; |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
int |
75 |
|
|
vpn::setup () |
76 |
|
|
{ |
77 |
pcg |
1.8 |
ipv4_fd = -1; |
78 |
pcg |
1.1 |
|
79 |
pcg |
1.8 |
if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto) |
80 |
pcg |
1.1 |
{ |
81 |
pcg |
1.8 |
ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); |
82 |
pcg |
1.1 |
|
83 |
pcg |
1.8 |
if (ipv4_fd < 0) |
84 |
pcg |
1.1 |
return -1; |
85 |
|
|
|
86 |
pcg |
1.11 |
fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
87 |
|
|
|
88 |
pcg |
1.14 |
#if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
89 |
pcg |
1.1 |
// this I really consider a linux bug. I am neither connected |
90 |
|
|
// nor do I fragment myself. Linux still sets DF and doesn't |
91 |
|
|
// fragment for me sometimes. |
92 |
|
|
{ |
93 |
|
|
int oval = IP_PMTUDISC_DONT; |
94 |
pcg |
1.8 |
setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
95 |
pcg |
1.1 |
} |
96 |
|
|
#endif |
97 |
|
|
|
98 |
pcg |
1.9 |
sockinfo si (THISNODE, PROT_IPv4); |
99 |
|
|
|
100 |
|
|
if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
101 |
|
|
{ |
102 |
|
|
slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); |
103 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
104 |
pcg |
1.9 |
} |
105 |
|
|
|
106 |
pcg |
1.22 |
ipv4_ev_watcher.start (ipv4_fd, EVENT_READ); |
107 |
pcg |
1.1 |
} |
108 |
|
|
|
109 |
pcg |
1.8 |
udpv4_fd = -1; |
110 |
|
|
|
111 |
|
|
if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) |
112 |
pcg |
1.1 |
{ |
113 |
pcg |
1.8 |
udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
114 |
pcg |
1.1 |
|
115 |
pcg |
1.8 |
if (udpv4_fd < 0) |
116 |
pcg |
1.1 |
return -1; |
117 |
|
|
|
118 |
pcg |
1.11 |
fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); |
119 |
|
|
|
120 |
pcg |
1.8 |
// standard daemon practise... |
121 |
|
|
{ |
122 |
|
|
int oval = 1; |
123 |
|
|
setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
124 |
|
|
} |
125 |
|
|
|
126 |
pcg |
1.14 |
#if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
127 |
pcg |
1.9 |
// this I really consider a linux bug. I am neither connected |
128 |
|
|
// nor do I fragment myself. Linux still sets DF and doesn't |
129 |
|
|
// fragment for me sometimes. |
130 |
|
|
{ |
131 |
|
|
int oval = IP_PMTUDISC_DONT; |
132 |
|
|
setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
133 |
|
|
} |
134 |
|
|
#endif |
135 |
|
|
|
136 |
pcg |
1.8 |
sockinfo si (THISNODE, PROT_UDPv4); |
137 |
pcg |
1.2 |
|
138 |
pcg |
1.8 |
if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) |
139 |
pcg |
1.1 |
{ |
140 |
pcg |
1.8 |
slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); |
141 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
142 |
pcg |
1.1 |
} |
143 |
|
|
|
144 |
pcg |
1.22 |
udpv4_ev_watcher.start (udpv4_fd, EVENT_READ); |
145 |
pcg |
1.9 |
} |
146 |
|
|
|
147 |
|
|
icmpv4_fd = -1; |
148 |
|
|
|
149 |
|
|
#if ENABLE_ICMP |
150 |
|
|
if (THISNODE->protocols & PROT_ICMPv4) |
151 |
|
|
{ |
152 |
|
|
icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); |
153 |
|
|
|
154 |
|
|
if (icmpv4_fd < 0) |
155 |
|
|
return -1; |
156 |
|
|
|
157 |
pcg |
1.11 |
fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); |
158 |
|
|
|
159 |
pcg |
1.9 |
#ifdef ICMP_FILTER |
160 |
|
|
{ |
161 |
|
|
icmp_filter oval; |
162 |
|
|
oval.data = 0xffffffff; |
163 |
|
|
if (::conf.icmp_type < 32) |
164 |
|
|
oval.data &= ~(1 << ::conf.icmp_type); |
165 |
|
|
|
166 |
|
|
setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
167 |
|
|
} |
168 |
|
|
#endif |
169 |
|
|
|
170 |
pcg |
1.14 |
#if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
171 |
pcg |
1.1 |
// this I really consider a linux bug. I am neither connected |
172 |
|
|
// nor do I fragment myself. Linux still sets DF and doesn't |
173 |
|
|
// fragment for me sometimes. |
174 |
|
|
{ |
175 |
|
|
int oval = IP_PMTUDISC_DONT; |
176 |
pcg |
1.8 |
setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
177 |
pcg |
1.1 |
} |
178 |
|
|
#endif |
179 |
|
|
|
180 |
pcg |
1.9 |
sockinfo si (THISNODE, PROT_ICMPv4); |
181 |
|
|
|
182 |
|
|
if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ())) |
183 |
|
|
{ |
184 |
|
|
slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); |
185 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
186 |
pcg |
1.9 |
} |
187 |
|
|
|
188 |
pcg |
1.22 |
icmpv4_ev_watcher.start (icmpv4_fd, EVENT_READ); |
189 |
pcg |
1.1 |
} |
190 |
pcg |
1.9 |
#endif |
191 |
pcg |
1.1 |
|
192 |
pcg |
1.8 |
tcpv4_fd = -1; |
193 |
|
|
|
194 |
pcg |
1.2 |
#if ENABLE_TCP |
195 |
pcg |
1.8 |
if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port) |
196 |
pcg |
1.2 |
{ |
197 |
|
|
tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
198 |
|
|
|
199 |
|
|
if (tcpv4_fd < 0) |
200 |
|
|
return -1; |
201 |
|
|
|
202 |
pcg |
1.11 |
fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); |
203 |
|
|
|
204 |
pcg |
1.3 |
// standard daemon practise... |
205 |
|
|
{ |
206 |
|
|
int oval = 1; |
207 |
|
|
setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
208 |
|
|
} |
209 |
|
|
|
210 |
pcg |
1.2 |
sockinfo si (THISNODE, PROT_TCPv4); |
211 |
|
|
|
212 |
|
|
if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ())) |
213 |
|
|
{ |
214 |
|
|
slog (L_ERR, _("can't bind tcpv4 on %s: %s"), (const char *)si, strerror (errno)); |
215 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
216 |
pcg |
1.2 |
} |
217 |
|
|
|
218 |
|
|
if (listen (tcpv4_fd, 5)) |
219 |
|
|
{ |
220 |
|
|
slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno)); |
221 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
222 |
pcg |
1.2 |
} |
223 |
|
|
|
224 |
pcg |
1.22 |
tcpv4_ev_watcher.start (tcpv4_fd, EVENT_READ); |
225 |
pcg |
1.2 |
} |
226 |
|
|
#endif |
227 |
|
|
|
228 |
pcg |
1.24 |
#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 ())) |
245 |
|
|
{ |
246 |
|
|
slog (L_ERR, _("can't bind dnsv4 on %s: %s"), (const char *)si, strerror (errno)); |
247 |
|
|
exit (EXIT_FAILURE); |
248 |
|
|
} |
249 |
|
|
|
250 |
|
|
dnsv4_ev_watcher.start (dnsv4_fd, EVENT_READ); |
251 |
|
|
} |
252 |
|
|
#endif |
253 |
|
|
|
254 |
pcg |
1.1 |
tap = new tap_device (); |
255 |
|
|
if (!tap) //D this, of course, never catches |
256 |
|
|
{ |
257 |
|
|
slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
258 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
259 |
pcg |
1.1 |
} |
260 |
|
|
|
261 |
|
|
run_script (run_script_cb (this, &vpn::script_if_up), true); |
262 |
|
|
|
263 |
pcg |
1.22 |
tap_ev_watcher.start (tap->fd, EVENT_READ); |
264 |
pcg |
1.1 |
|
265 |
|
|
reconnect_all (); |
266 |
|
|
|
267 |
|
|
return 0; |
268 |
|
|
} |
269 |
|
|
|
270 |
pcg |
1.5 |
// send a vpn packet out to other hosts |
271 |
pcg |
1.7 |
bool |
272 |
pcg |
1.5 |
vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
273 |
|
|
{ |
274 |
|
|
switch (si.prot) |
275 |
|
|
{ |
276 |
|
|
case PROT_IPv4: |
277 |
pcg |
1.7 |
return send_ipv4_packet (pkt, si, tos); |
278 |
pcg |
1.5 |
|
279 |
|
|
case PROT_UDPv4: |
280 |
pcg |
1.7 |
return send_udpv4_packet (pkt, si, tos); |
281 |
pcg |
1.5 |
|
282 |
|
|
#if ENABLE_TCP |
283 |
|
|
case PROT_TCPv4: |
284 |
pcg |
1.7 |
return send_tcpv4_packet (pkt, si, tos); |
285 |
pcg |
1.5 |
#endif |
286 |
|
|
|
287 |
pcg |
1.9 |
#if ENABLE_ICMP |
288 |
|
|
case PROT_ICMPv4: |
289 |
|
|
return send_icmpv4_packet (pkt, si, tos); |
290 |
|
|
#endif |
291 |
|
|
|
292 |
pcg |
1.24 |
#if ENABLE_DNS |
293 |
|
|
case PROT_DNSv4: |
294 |
|
|
return send_dnsv4_packet (pkt, si, tos); |
295 |
|
|
#endif |
296 |
|
|
|
297 |
pcg |
1.5 |
default: |
298 |
|
|
slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); |
299 |
pcg |
1.7 |
return false; |
300 |
pcg |
1.5 |
} |
301 |
|
|
} |
302 |
|
|
|
303 |
pcg |
1.7 |
bool |
304 |
pcg |
1.1 |
vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
305 |
|
|
{ |
306 |
pcg |
1.14 |
#if defined(SOL_IP) && defined(IP_TOS) |
307 |
pcg |
1.1 |
setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
308 |
pcg |
1.14 |
#endif |
309 |
pcg |
1.1 |
sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
310 |
pcg |
1.7 |
|
311 |
|
|
return true; |
312 |
pcg |
1.1 |
} |
313 |
|
|
|
314 |
pcg |
1.9 |
static u16 |
315 |
|
|
ipv4_checksum (u16 *data, unsigned int len) |
316 |
|
|
{ |
317 |
|
|
// use 32 bit accumulator and fold back carry bits at the end |
318 |
|
|
u32 sum = 0; |
319 |
|
|
|
320 |
|
|
while (len > 1) |
321 |
|
|
{ |
322 |
|
|
sum += *data++; |
323 |
|
|
len -= 2; |
324 |
|
|
} |
325 |
|
|
|
326 |
|
|
// odd byte left? |
327 |
|
|
if (len) |
328 |
|
|
sum += *(u8 *)data; |
329 |
|
|
|
330 |
|
|
// add back carry bits |
331 |
|
|
sum = (sum >> 16) + (sum & 0xffff); // lo += hi |
332 |
|
|
sum += (sum >> 16); // carry |
333 |
|
|
|
334 |
|
|
return ~sum; |
335 |
|
|
} |
336 |
|
|
|
337 |
pcg |
1.13 |
#if ENABLE_ICMP |
338 |
pcg |
1.9 |
bool |
339 |
|
|
vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
340 |
|
|
{ |
341 |
pcg |
1.14 |
#if defined(SOL_IP) && defined(IP_TOS) |
342 |
pcg |
1.9 |
setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
343 |
pcg |
1.14 |
#endif |
344 |
pcg |
1.9 |
|
345 |
|
|
pkt->unshift_hdr (4); |
346 |
|
|
|
347 |
pcg |
1.14 |
icmp_header *hdr = (icmp_header *)&((*pkt)[0]); |
348 |
pcg |
1.9 |
hdr->type = ::conf.icmp_type; |
349 |
|
|
hdr->code = 255; |
350 |
|
|
hdr->checksum = 0; |
351 |
|
|
hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
352 |
|
|
|
353 |
|
|
sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
354 |
|
|
|
355 |
|
|
return true; |
356 |
|
|
} |
357 |
pcg |
1.13 |
#endif |
358 |
pcg |
1.9 |
|
359 |
pcg |
1.7 |
bool |
360 |
pcg |
1.1 |
vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
361 |
|
|
{ |
362 |
pcg |
1.14 |
#if defined(SOL_IP) && defined(IP_TOS) |
363 |
pcg |
1.1 |
setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
364 |
pcg |
1.14 |
#endif |
365 |
pcg |
1.1 |
sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
366 |
pcg |
1.7 |
|
367 |
|
|
return true; |
368 |
pcg |
1.1 |
} |
369 |
|
|
|
370 |
|
|
void |
371 |
pcg |
1.18 |
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 |
|
|
} |
387 |
|
|
} |
388 |
|
|
|
389 |
|
|
void |
390 |
pcg |
1.1 |
vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
391 |
|
|
{ |
392 |
|
|
unsigned int src = pkt->src (); |
393 |
|
|
unsigned int dst = pkt->dst (); |
394 |
|
|
|
395 |
|
|
slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d"), |
396 |
|
|
(const char *)rsi, pkt->typ (), pkt->src (), pkt->dst (), pkt->len); |
397 |
|
|
|
398 |
|
|
if (src == 0 || src > conns.size () |
399 |
|
|
|| dst > conns.size () |
400 |
|
|
|| pkt->typ () >= vpn_packet::PT_MAX) |
401 |
|
|
slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), |
402 |
|
|
(const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
403 |
pcg |
1.6 |
else if (dst > conns.size ()) |
404 |
|
|
slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), |
405 |
|
|
(const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
406 |
pcg |
1.1 |
else |
407 |
|
|
{ |
408 |
|
|
connection *c = conns[src - 1]; |
409 |
|
|
|
410 |
pcg |
1.13 |
if (dst == 0) |
411 |
|
|
slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), |
412 |
pcg |
1.1 |
c->conf->nodename, (const char *)rsi); |
413 |
pcg |
1.18 |
else if (dst != THISNODE->id) |
414 |
pcg |
1.6 |
{ |
415 |
|
|
if (THISNODE->routerprio) |
416 |
|
|
// the tos setting gets lost here. who cares. |
417 |
|
|
conns[dst - 1]->inject_vpn_packet (pkt); |
418 |
|
|
else |
419 |
|
|
slog (L_WARN, |
420 |
|
|
_("%s(%s): forwarding request (=> %s), but we are no router"), |
421 |
|
|
c->conf->nodename, (const char *)rsi, |
422 |
|
|
conns[dst - 1]->conf->nodename); |
423 |
|
|
} |
424 |
pcg |
1.1 |
else |
425 |
|
|
c->recv_vpn_packet (pkt, rsi); |
426 |
|
|
} |
427 |
|
|
} |
428 |
|
|
|
429 |
|
|
void |
430 |
pcg |
1.9 |
vpn::ipv4_ev (io_watcher &w, short revents) |
431 |
pcg |
1.1 |
{ |
432 |
pcg |
1.22 |
if (revents & EVENT_READ) |
433 |
pcg |
1.1 |
{ |
434 |
|
|
vpn_packet *pkt = new vpn_packet; |
435 |
|
|
struct sockaddr_in sa; |
436 |
|
|
socklen_t sa_len = sizeof (sa); |
437 |
|
|
int len; |
438 |
|
|
|
439 |
pcg |
1.5 |
len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
440 |
pcg |
1.1 |
|
441 |
pcg |
1.9 |
sockinfo si(sa, PROT_IPv4); |
442 |
pcg |
1.1 |
|
443 |
|
|
if (len > 0) |
444 |
|
|
{ |
445 |
|
|
pkt->len = len; |
446 |
|
|
|
447 |
pcg |
1.9 |
// raw sockets deliver the ipv4, but don't expect it on sends |
448 |
|
|
// this is slow, but... |
449 |
|
|
pkt->skip_hdr (IP_OVERHEAD); |
450 |
|
|
|
451 |
pcg |
1.1 |
recv_vpn_packet (pkt, si); |
452 |
|
|
} |
453 |
|
|
else |
454 |
|
|
{ |
455 |
|
|
// probably ECONNRESET or somesuch |
456 |
pcg |
1.9 |
slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
457 |
pcg |
1.1 |
} |
458 |
|
|
|
459 |
|
|
delete pkt; |
460 |
|
|
} |
461 |
|
|
else |
462 |
|
|
{ |
463 |
|
|
slog (L_ERR, |
464 |
|
|
_("FATAL: unknown revents %08x in socket, terminating\n"), |
465 |
|
|
revents); |
466 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
467 |
pcg |
1.1 |
} |
468 |
|
|
} |
469 |
|
|
|
470 |
pcg |
1.13 |
#if ENABLE_ICMP |
471 |
pcg |
1.1 |
void |
472 |
pcg |
1.9 |
vpn::icmpv4_ev (io_watcher &w, short revents) |
473 |
pcg |
1.1 |
{ |
474 |
pcg |
1.22 |
if (revents & EVENT_READ) |
475 |
pcg |
1.1 |
{ |
476 |
|
|
vpn_packet *pkt = new vpn_packet; |
477 |
|
|
struct sockaddr_in sa; |
478 |
|
|
socklen_t sa_len = sizeof (sa); |
479 |
|
|
int len; |
480 |
|
|
|
481 |
pcg |
1.5 |
len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
482 |
pcg |
1.1 |
|
483 |
pcg |
1.9 |
sockinfo si(sa, PROT_ICMPv4); |
484 |
pcg |
1.1 |
|
485 |
|
|
if (len > 0) |
486 |
|
|
{ |
487 |
|
|
pkt->len = len; |
488 |
|
|
|
489 |
pcg |
1.14 |
icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]); |
490 |
pcg |
1.9 |
|
491 |
|
|
if (hdr->type == ::conf.icmp_type |
492 |
|
|
&& hdr->code == 255) |
493 |
|
|
{ |
494 |
|
|
// raw sockets deliver the ipv4, but don't expect it on sends |
495 |
|
|
// this is slow, but... |
496 |
|
|
pkt->skip_hdr (ICMP_OVERHEAD); |
497 |
|
|
|
498 |
|
|
recv_vpn_packet (pkt, si); |
499 |
|
|
} |
500 |
|
|
} |
501 |
|
|
else |
502 |
|
|
{ |
503 |
|
|
// probably ECONNRESET or somesuch |
504 |
|
|
slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
505 |
|
|
} |
506 |
|
|
|
507 |
|
|
delete pkt; |
508 |
|
|
} |
509 |
|
|
else |
510 |
|
|
{ |
511 |
|
|
slog (L_ERR, |
512 |
|
|
_("FATAL: unknown revents %08x in socket, terminating\n"), |
513 |
|
|
revents); |
514 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
515 |
pcg |
1.9 |
} |
516 |
|
|
} |
517 |
pcg |
1.13 |
#endif |
518 |
pcg |
1.9 |
|
519 |
|
|
void |
520 |
|
|
vpn::udpv4_ev (io_watcher &w, short revents) |
521 |
|
|
{ |
522 |
pcg |
1.22 |
if (revents & EVENT_READ) |
523 |
pcg |
1.9 |
{ |
524 |
|
|
vpn_packet *pkt = new vpn_packet; |
525 |
|
|
struct sockaddr_in sa; |
526 |
|
|
socklen_t sa_len = sizeof (sa); |
527 |
|
|
int len; |
528 |
|
|
|
529 |
|
|
len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
530 |
|
|
|
531 |
|
|
sockinfo si(sa, PROT_UDPv4); |
532 |
|
|
|
533 |
|
|
if (len > 0) |
534 |
|
|
{ |
535 |
|
|
pkt->len = len; |
536 |
pcg |
1.1 |
|
537 |
|
|
recv_vpn_packet (pkt, si); |
538 |
|
|
} |
539 |
|
|
else |
540 |
|
|
{ |
541 |
|
|
// probably ECONNRESET or somesuch |
542 |
pcg |
1.9 |
slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); |
543 |
pcg |
1.1 |
} |
544 |
|
|
|
545 |
|
|
delete pkt; |
546 |
|
|
} |
547 |
|
|
else |
548 |
|
|
{ |
549 |
|
|
slog (L_ERR, |
550 |
|
|
_("FATAL: unknown revents %08x in socket, terminating\n"), |
551 |
|
|
revents); |
552 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
553 |
pcg |
1.1 |
} |
554 |
|
|
} |
555 |
pcg |
1.2 |
|
556 |
|
|
void |
557 |
pcg |
1.3 |
vpn::tap_ev (io_watcher &w, short revents) |
558 |
pcg |
1.1 |
{ |
559 |
pcg |
1.22 |
if (revents & EVENT_READ) |
560 |
pcg |
1.1 |
{ |
561 |
|
|
/* process data */ |
562 |
|
|
tap_packet *pkt; |
563 |
|
|
|
564 |
|
|
pkt = tap->recv (); |
565 |
|
|
|
566 |
pcg |
1.17 |
if (!pkt) |
567 |
|
|
return; |
568 |
pcg |
1.1 |
|
569 |
pcg |
1.17 |
if (pkt->len > 14) |
570 |
pcg |
1.1 |
{ |
571 |
pcg |
1.17 |
int dst = mac2id (pkt->dst); |
572 |
|
|
int src = mac2id (pkt->src); |
573 |
pcg |
1.1 |
|
574 |
pcg |
1.17 |
if (src != THISNODE->id) |
575 |
|
|
{ |
576 |
|
|
slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting.")); |
577 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
578 |
pcg |
1.17 |
} |
579 |
pcg |
1.1 |
|
580 |
pcg |
1.17 |
if (dst == THISNODE->id) |
581 |
pcg |
1.1 |
{ |
582 |
pcg |
1.17 |
slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting.")); |
583 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
584 |
pcg |
1.1 |
} |
585 |
pcg |
1.17 |
|
586 |
|
|
if (dst > conns.size ()) |
587 |
|
|
slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
588 |
pcg |
1.1 |
else |
589 |
pcg |
1.18 |
inject_data_packet (pkt, dst); |
590 |
pcg |
1.1 |
} |
591 |
|
|
|
592 |
|
|
delete pkt; |
593 |
|
|
} |
594 |
|
|
else |
595 |
|
|
abort (); |
596 |
|
|
} |
597 |
|
|
|
598 |
|
|
void |
599 |
pcg |
1.3 |
vpn::event_cb (time_watcher &w) |
600 |
pcg |
1.1 |
{ |
601 |
|
|
if (events) |
602 |
|
|
{ |
603 |
|
|
if (events & EVENT_SHUTDOWN) |
604 |
|
|
{ |
605 |
|
|
slog (L_INFO, _("preparing shutdown...")); |
606 |
|
|
|
607 |
|
|
shutdown_all (); |
608 |
pcg |
1.24 |
remove_pid (conf.pidfilename); |
609 |
pcg |
1.1 |
slog (L_INFO, _("terminating")); |
610 |
pcg |
1.23 |
exit (EXIT_SUCCESS); |
611 |
pcg |
1.1 |
} |
612 |
|
|
|
613 |
|
|
if (events & EVENT_RECONNECT) |
614 |
|
|
{ |
615 |
|
|
slog (L_INFO, _("forced reconnect")); |
616 |
|
|
|
617 |
|
|
reconnect_all (); |
618 |
|
|
} |
619 |
|
|
|
620 |
|
|
events = 0; |
621 |
|
|
} |
622 |
|
|
} |
623 |
|
|
|
624 |
|
|
void |
625 |
|
|
vpn::shutdown_all () |
626 |
|
|
{ |
627 |
|
|
for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
628 |
|
|
(*c)->shutdown (); |
629 |
|
|
} |
630 |
|
|
|
631 |
|
|
void |
632 |
|
|
vpn::reconnect_all () |
633 |
|
|
{ |
634 |
|
|
for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
635 |
|
|
delete *c; |
636 |
|
|
|
637 |
|
|
conns.clear (); |
638 |
|
|
|
639 |
|
|
connection_init (); |
640 |
|
|
|
641 |
|
|
for (configuration::node_vector::iterator i = conf.nodes.begin (); |
642 |
|
|
i != conf.nodes.end (); ++i) |
643 |
|
|
{ |
644 |
|
|
connection *conn = new connection (this); |
645 |
|
|
|
646 |
|
|
conn->conf = *i; |
647 |
|
|
conns.push_back (conn); |
648 |
|
|
|
649 |
|
|
conn->establish_connection (); |
650 |
|
|
} |
651 |
|
|
} |
652 |
|
|
|
653 |
|
|
connection *vpn::find_router () |
654 |
|
|
{ |
655 |
pcg |
1.26 |
u32 prio = 1; |
656 |
pcg |
1.1 |
connection *router = 0; |
657 |
|
|
|
658 |
|
|
for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
659 |
|
|
{ |
660 |
|
|
connection *c = *i; |
661 |
|
|
|
662 |
pcg |
1.13 |
if (c->conf->routerprio > prio |
663 |
pcg |
1.11 |
&& c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use |
664 |
|
|
&& c->ictx && c->octx |
665 |
pcg |
1.13 |
&& c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode |
666 |
pcg |
1.1 |
{ |
667 |
|
|
prio = c->conf->routerprio; |
668 |
|
|
router = c; |
669 |
|
|
} |
670 |
|
|
} |
671 |
|
|
|
672 |
|
|
return router; |
673 |
|
|
} |
674 |
|
|
|
675 |
pcg |
1.7 |
void vpn::send_connect_request (int id) |
676 |
pcg |
1.1 |
{ |
677 |
|
|
connection *c = find_router (); |
678 |
|
|
|
679 |
|
|
if (c) |
680 |
pcg |
1.7 |
c->send_connect_request (id); |
681 |
|
|
else |
682 |
|
|
// no router found, aggressively connect to all routers |
683 |
|
|
for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
684 |
pcg |
1.13 |
if ((*i)->conf->routerprio && (*i)->conf != THISNODE) |
685 |
pcg |
1.7 |
(*i)->establish_connection (); |
686 |
pcg |
1.1 |
} |
687 |
|
|
|
688 |
|
|
void |
689 |
|
|
connection::dump_status () |
690 |
|
|
{ |
691 |
|
|
slog (L_NOTICE, _("node %s (id %d)"), conf->nodename, conf->id); |
692 |
|
|
slog (L_NOTICE, _(" connectmode %d (%d) / sockaddr %s / minor %d"), |
693 |
|
|
connectmode, conf->connectmode, (const char *)si, (int)prot_minor); |
694 |
|
|
slog (L_NOTICE, _(" ictx/octx %08lx/%08lx / oseqno %d / retry_cnt %d"), |
695 |
|
|
(long)ictx, (long)octx, (int)oseqno, (int)retry_cnt); |
696 |
|
|
slog (L_NOTICE, _(" establish_conn %ld / rekey %ld / keepalive %ld"), |
697 |
|
|
(long)(establish_connection.at), (long)(rekey.at), (long)(keepalive.at)); |
698 |
|
|
} |
699 |
|
|
|
700 |
|
|
void |
701 |
|
|
vpn::dump_status () |
702 |
|
|
{ |
703 |
|
|
slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)NOW); |
704 |
|
|
|
705 |
|
|
for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
706 |
|
|
(*c)->dump_status (); |
707 |
|
|
|
708 |
|
|
slog (L_NOTICE, _("END status dump")); |
709 |
|
|
} |
710 |
|
|
|
711 |
|
|
vpn::vpn (void) |
712 |
pcg |
1.9 |
: event (this, &vpn::event_cb) |
713 |
|
|
, udpv4_ev_watcher (this, &vpn::udpv4_ev) |
714 |
|
|
, ipv4_ev_watcher (this, &vpn::ipv4_ev) |
715 |
pcg |
1.2 |
#if ENABLE_TCP |
716 |
pcg |
1.9 |
, tcpv4_ev_watcher (this, &vpn::tcpv4_ev) |
717 |
|
|
#endif |
718 |
|
|
#if ENABLE_ICMP |
719 |
|
|
, icmpv4_ev_watcher(this, &vpn::icmpv4_ev) |
720 |
pcg |
1.24 |
#endif |
721 |
|
|
#if ENABLE_DNS |
722 |
|
|
, dnsv4_ev_watcher (this, &vpn::dnsv4_ev) |
723 |
pcg |
1.2 |
#endif |
724 |
pcg |
1.9 |
, tap_ev_watcher (this, &vpn::tap_ev) |
725 |
pcg |
1.1 |
{ |
726 |
|
|
} |
727 |
|
|
|
728 |
|
|
vpn::~vpn () |
729 |
|
|
{ |
730 |
|
|
} |
731 |
|
|
|