ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vpn.C
(Generate patch)

Comparing gvpe/src/vpn.C (file contents):
Revision 1.10 by pcg, Tue Apr 8 03:25:35 2003 UTC vs.
Revision 1.27 by pcg, Tue Mar 1 06:27:20 2005 UTC

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
45vpn network; // THE vpn (bad design...)
43 46
44///////////////////////////////////////////////////////////////////////////// 47/////////////////////////////////////////////////////////////////////////////
45 48
46const char *vpn::script_if_up () 49const 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}
235
236// send a vpn packet out to other hosts
237bool
238vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos)
239{
240 switch (si.prot)
241 {
242 case PROT_IPv4:
243 return send_ipv4_packet (pkt, si, tos);
244
245 case PROT_UDPv4:
246 return send_udpv4_packet (pkt, si, tos);
247
248#if ENABLE_TCP
249 case PROT_TCPv4:
250 return send_tcpv4_packet (pkt, si, tos);
251#endif
252
253#if ENABLE_ICMP
254 case PROT_ICMPv4:
255 return send_icmpv4_packet (pkt, si, tos);
256#endif
257
258 default:
259 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si);
260 return false;
261 }
262} 268}
263 269
264bool 270bool
265vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 271vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
266{ 272{
273#if defined(SOL_IP) && defined(IP_TOS)
267 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 274 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
275#endif
268 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 276 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
269 277
270 return true; 278 return true;
271} 279}
272 280
291 sum += (sum >> 16); // carry 299 sum += (sum >> 16); // carry
292 300
293 return ~sum; 301 return ~sum;
294} 302}
295 303
304#if ENABLE_ICMP
296bool 305bool
297vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 306vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
298{ 307{
308#if defined(SOL_IP) && defined(IP_TOS)
299 setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 309 setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
310#endif
300 311
301 pkt->unshift_hdr (4); 312 pkt->unshift_hdr (4);
302 313
303 icmphdr *hdr = (icmphdr *)&((*pkt)[0]); 314 icmp_header *hdr = (icmp_header *)&((*pkt)[0]);
304 hdr->type = ::conf.icmp_type; 315 hdr->type = ::conf.icmp_type;
305 hdr->code = 255; 316 hdr->code = 255;
306 hdr->checksum = 0; 317 hdr->checksum = 0;
307 hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); 318 hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len);
308 319
309 sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 320 sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
310 321
311 return true; 322 return true;
312} 323}
324#endif
313 325
314bool 326bool
315vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 327vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
316{ 328{
329#if defined(SOL_IP) && defined(IP_TOS)
317 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 330 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
331#endif
318 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 332 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
319 333
320 return true; 334 return true;
335}
336
337void
338vpn::inject_data_packet (tap_packet *pkt, int dst)
339{
340 if (dst)
341 {
342 // unicast
343 if (dst != THISNODE->id)
344 conns[dst - 1]->inject_data_packet (pkt);
345 }
346 else
347 {
348 // broadcast, this is ugly, but due to the security policy
349 // we have to connect to all hosts...
350 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
351 if ((*c)->conf != THISNODE)
352 (*c)->inject_data_packet (pkt, true);
353 }
321} 354}
322 355
323void 356void
324vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) 357vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
325{ 358{
339 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); 372 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ());
340 else 373 else
341 { 374 {
342 connection *c = conns[src - 1]; 375 connection *c = conns[src - 1];
343 376
344 if (dst == 0 && !THISNODE->routerprio) 377 if (dst == 0)
345 slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), 378 slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"),
346 c->conf->nodename, (const char *)rsi); 379 c->conf->nodename, (const char *)rsi);
347 else if (dst != 0 && dst != THISNODE->id) 380 else if (dst != THISNODE->id)
348 { 381 {
349 if (THISNODE->routerprio) 382 if (THISNODE->routerprio)
350 // the tos setting gets lost here. who cares. 383 // the tos setting gets lost here. who cares.
351 conns[dst - 1]->inject_vpn_packet (pkt); 384 conns[dst - 1]->inject_vpn_packet (pkt);
352 else 385 else
361} 394}
362 395
363void 396void
364vpn::ipv4_ev (io_watcher &w, short revents) 397vpn::ipv4_ev (io_watcher &w, short revents)
365{ 398{
366 if (revents & (POLLIN | POLLERR)) 399 if (revents & EVENT_READ)
367 { 400 {
368 vpn_packet *pkt = new vpn_packet; 401 vpn_packet *pkt = new vpn_packet;
369 struct sockaddr_in sa; 402 struct sockaddr_in sa;
370 socklen_t sa_len = sizeof (sa); 403 socklen_t sa_len = sizeof (sa);
371 int len; 404 int len;
390 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); 423 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno));
391 } 424 }
392 425
393 delete pkt; 426 delete pkt;
394 } 427 }
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 428 else
402 { 429 {
403 slog (L_ERR, 430 slog (L_ERR,
404 _("FATAL: unknown revents %08x in socket, terminating\n"), 431 _("FATAL: unknown revents %08x in socket, terminating\n"),
405 revents); 432 revents);
406 exit (1); 433 exit (EXIT_FAILURE);
407 } 434 }
408} 435}
409 436
437#if ENABLE_ICMP
410void 438void
411vpn::icmpv4_ev (io_watcher &w, short revents) 439vpn::icmpv4_ev (io_watcher &w, short revents)
412{ 440{
413 if (revents & (POLLIN | POLLERR)) 441 if (revents & EVENT_READ)
414 { 442 {
415 vpn_packet *pkt = new vpn_packet; 443 vpn_packet *pkt = new vpn_packet;
416 struct sockaddr_in sa; 444 struct sockaddr_in sa;
417 socklen_t sa_len = sizeof (sa); 445 socklen_t sa_len = sizeof (sa);
418 int len; 446 int len;
423 451
424 if (len > 0) 452 if (len > 0)
425 { 453 {
426 pkt->len = len; 454 pkt->len = len;
427 455
428 icmphdr *hdr = (icmphdr *)&((*pkt)[IP_OVERHEAD]); 456 icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]);
429 457
430 if (hdr->type == ::conf.icmp_type 458 if (hdr->type == ::conf.icmp_type
431 && hdr->code == 255) 459 && hdr->code == 255)
432 { 460 {
433 // raw sockets deliver the ipv4, but don't expect it on sends 461 // raw sockets deliver the ipv4, but don't expect it on sends
443 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); 471 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno));
444 } 472 }
445 473
446 delete pkt; 474 delete pkt;
447 } 475 }
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 476 else
455 { 477 {
456 slog (L_ERR, 478 slog (L_ERR,
457 _("FATAL: unknown revents %08x in socket, terminating\n"), 479 _("FATAL: unknown revents %08x in socket, terminating\n"),
458 revents); 480 revents);
459 exit (1); 481 exit (EXIT_FAILURE);
460 } 482 }
461} 483}
484#endif
462 485
463void 486void
464vpn::udpv4_ev (io_watcher &w, short revents) 487vpn::udpv4_ev (io_watcher &w, short revents)
465{ 488{
466 if (revents & (POLLIN | POLLERR)) 489 if (revents & EVENT_READ)
467 { 490 {
468 vpn_packet *pkt = new vpn_packet; 491 vpn_packet *pkt = new vpn_packet;
469 struct sockaddr_in sa; 492 struct sockaddr_in sa;
470 socklen_t sa_len = sizeof (sa); 493 socklen_t sa_len = sizeof (sa);
471 int len; 494 int len;
486 slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); 509 slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno));
487 } 510 }
488 511
489 delete pkt; 512 delete pkt;
490 } 513 }
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 514 else
498 { 515 {
499 slog (L_ERR, 516 slog (L_ERR,
500 _("FATAL: unknown revents %08x in socket, terminating\n"), 517 _("FATAL: unknown revents %08x in socket, terminating\n"),
501 revents); 518 revents);
502 exit (1); 519 exit (EXIT_FAILURE);
503 } 520 }
504} 521}
505 522
506void 523void
507vpn::tap_ev (io_watcher &w, short revents) 524vpn::tap_ev (io_watcher &w, short revents)
508{ 525{
509 if (revents & POLLIN) 526 if (revents & EVENT_READ)
510 { 527 {
511 /* process data */ 528 /* process data */
512 tap_packet *pkt; 529 tap_packet *pkt;
513 530
514 pkt = tap->recv (); 531 pkt = tap->recv ();
515 532
533 if (!pkt)
534 return;
535
536 if (pkt->len > 14)
537 {
516 int dst = mac2id (pkt->dst); 538 int dst = mac2id (pkt->dst);
517 int src = mac2id (pkt->src); 539 int src = mac2id (pkt->src);
518 540
519 if (src != THISNODE->id) 541 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 { 542 {
537 // unicast 543 slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting."));
538 if (dst != THISNODE->id) 544 exit (EXIT_FAILURE);
539 conns[dst - 1]->inject_data_packet (pkt);
540 } 545 }
546
547 if (dst == THISNODE->id)
548 {
549 slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting."));
550 exit (EXIT_FAILURE);
551 }
552
553 if (dst > conns.size ())
554 slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst);
541 else 555 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); 556 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 } 557 }
554 558
555 delete pkt; 559 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 } 560 }
562 else 561 else
563 abort (); 562 abort ();
564} 563}
565 564
571 if (events & EVENT_SHUTDOWN) 570 if (events & EVENT_SHUTDOWN)
572 { 571 {
573 slog (L_INFO, _("preparing shutdown...")); 572 slog (L_INFO, _("preparing shutdown..."));
574 573
575 shutdown_all (); 574 shutdown_all ();
576
577 remove_pid (pidfilename); 575 remove_pid (conf.pidfilename);
578
579 slog (L_INFO, _("terminating")); 576 slog (L_INFO, _("terminating"));
580 577 exit (EXIT_SUCCESS);
581 exit (0);
582 } 578 }
583 579
584 if (events & EVENT_RECONNECT) 580 if (events & EVENT_RECONNECT)
585 { 581 {
586 slog (L_INFO, _("forced reconnect")); 582 slog (L_INFO, _("forced reconnect"));
588 reconnect_all (); 584 reconnect_all ();
589 } 585 }
590 586
591 events = 0; 587 events = 0;
592 } 588 }
593
594 w.at = TSTAMP_CANCEL;
595} 589}
596 590
597void 591void
598vpn::shutdown_all () 592vpn::shutdown_all ()
599{ 593{
612 connection_init (); 606 connection_init ();
613 607
614 for (configuration::node_vector::iterator i = conf.nodes.begin (); 608 for (configuration::node_vector::iterator i = conf.nodes.begin ();
615 i != conf.nodes.end (); ++i) 609 i != conf.nodes.end (); ++i)
616 { 610 {
617 connection *conn = new connection (this); 611 connection *conn = new connection (this, *i);
618
619 conn->conf = *i;
620 conns.push_back (conn); 612 conns.push_back (conn);
621
622 conn->establish_connection (); 613 conn->establish_connection ();
623 } 614 }
624} 615}
625 616
626connection *vpn::find_router () 617connection *vpn::find_router ()
627{ 618{
628 u32 prio = 0; 619 u32 prio = 1;
629 connection *router = 0; 620 connection *router = 0;
630 621
631 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) 622 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
632 { 623 {
633 connection *c = *i; 624 connection *c = *i;
634 625
635 if (c->conf->routerprio > prio 626 if (c->conf->routerprio > prio
636 && c->connectmode == conf_node::C_ALWAYS 627 && 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) 628 && c->ictx && c->octx
629 && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode
639 { 630 {
640 prio = c->conf->routerprio; 631 prio = c->conf->routerprio;
641 router = c; 632 router = c;
642 } 633 }
643 } 634 }
652 if (c) 643 if (c)
653 c->send_connect_request (id); 644 c->send_connect_request (id);
654 else 645 else
655 // no router found, aggressively connect to all routers 646 // no router found, aggressively connect to all routers
656 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) 647 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
657 if ((*i)->conf->routerprio) 648 if ((*i)->conf->routerprio && (*i)->conf != THISNODE)
658 (*i)->establish_connection (); 649 (*i)->establish_connection ();
659} 650}
660 651
661void 652void
662connection::dump_status () 653connection::dump_status ()
689, tcpv4_ev_watcher (this, &vpn::tcpv4_ev) 680, tcpv4_ev_watcher (this, &vpn::tcpv4_ev)
690#endif 681#endif
691#if ENABLE_ICMP 682#if ENABLE_ICMP
692, icmpv4_ev_watcher(this, &vpn::icmpv4_ev) 683, icmpv4_ev_watcher(this, &vpn::icmpv4_ev)
693#endif 684#endif
685#if ENABLE_DNS
686, dnsv4_ev_watcher (this, &vpn::dnsv4_ev)
687#endif
694, tap_ev_watcher (this, &vpn::tap_ev) 688, tap_ev_watcher (this, &vpn::tap_ev)
695{ 689{
696} 690}
697 691
698vpn::~vpn () 692vpn::~vpn ()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines