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.6 by pcg, Sun Apr 6 04:17:36 2003 UTC vs.
Revision 1.20 by pcg, Thu Oct 16 14:12:00 2003 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 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.
24#include <cstring> 25#include <cstring>
25#include <cstdio> 26#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 <netinet/in.h>
32#include <arpa/inet.h>
33#include <errno.h> 31#include <errno.h>
34#include <time.h> 32#include <time.h>
35#include <unistd.h> 33#include <unistd.h>
34#include <fcntl.h>
35#include <sys/socket.h>
36
37#include "netcompat.h"
36 38
37#include "pidfile.h" 39#include "pidfile.h"
38 40
39#include "connection.h" 41#include "connection.h"
40#include "util.h" 42#include "util.h"
41#include "vpn.h" 43#include "vpn.h"
44
45vpn network; // THE vpn (bad design...)
42 46
43///////////////////////////////////////////////////////////////////////////// 47/////////////////////////////////////////////////////////////////////////////
44 48
45const char *vpn::script_if_up () 49const char *vpn::script_if_up ()
46{ 50{
71} 75}
72 76
73int 77int
74vpn::setup () 78vpn::setup ()
75{ 79{
80 ipv4_fd = -1;
81
82 if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto)
83 {
84 ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto);
85
86 if (ipv4_fd < 0)
87 return -1;
88
89 fcntl (ipv4_fd, F_SETFL, O_NONBLOCK);
90
91#if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
92 // this I really consider a linux bug. I am neither connected
93 // nor do I fragment myself. Linux still sets DF and doesn't
94 // fragment for me sometimes.
95 {
96 int oval = IP_PMTUDISC_DONT;
97 setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval);
98 }
99#endif
100
101 sockinfo si (THISNODE, PROT_IPv4);
102
103 if (bind (ipv4_fd, si.sav4 (), si.salenv4 ()))
104 {
105 slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno));
106 exit (1);
107 }
108
109 ipv4_ev_watcher.start (ipv4_fd, POLLIN);
110 }
111
76 udpv4_fd = -1; 112 udpv4_fd = -1;
77 113
78 if (THISNODE->protocols & PROT_UDPv4) 114 if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port)
79 { 115 {
80 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); 116 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
81 117
82 if (udpv4_fd < 0) 118 if (udpv4_fd < 0)
83 return -1; 119 return -1;
120
121 fcntl (udpv4_fd, F_SETFL, O_NONBLOCK);
84 122
85 // standard daemon practise... 123 // standard daemon practise...
86 { 124 {
87 int oval = 1; 125 int oval = 1;
88 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 126 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
89 } 127 }
90 128
91 sockinfo si (THISNODE, PROT_UDPv4); 129#if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
92
93 if (bind (udpv4_fd, si.sav4 (), si.salenv4 ()))
94 {
95 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno));
96 exit (1);
97 }
98
99#ifdef IP_MTU_DISCOVER
100 // this I really consider a linux bug. I am neither connected 130 // this I really consider a linux bug. I am neither connected
101 // nor do I fragment myself. Linux still sets DF and doesn't 131 // nor do I fragment myself. Linux still sets DF and doesn't
102 // fragment for me sometimes. 132 // fragment for me sometimes.
103 { 133 {
104 int oval = IP_PMTUDISC_DONT; 134 int oval = IP_PMTUDISC_DONT;
105 setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); 135 setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval);
106 } 136 }
107#endif 137#endif
108 138
139 sockinfo si (THISNODE, PROT_UDPv4);
140
141 if (bind (udpv4_fd, si.sav4 (), si.salenv4 ()))
142 {
143 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno));
144 exit (1);
145 }
146
109 udpv4_ev_watcher.start (udpv4_fd, POLLIN); 147 udpv4_ev_watcher.start (udpv4_fd, POLLIN);
110 } 148 }
111 149
112 ipv4_fd = -1; 150 icmpv4_fd = -1;
151
152#if ENABLE_ICMP
113 if (THISNODE->protocols & PROT_IPv4) 153 if (THISNODE->protocols & PROT_ICMPv4)
114 { 154 {
115 ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); 155 icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP);
116 156
117 if (ipv4_fd < 0) 157 if (icmpv4_fd < 0)
118 return -1; 158 return -1;
119 159
120 sockinfo si (THISNODE, PROT_IPv4); 160 fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK);
121 161
122 if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) 162#ifdef ICMP_FILTER
123 { 163 {
124 slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); 164 icmp_filter oval;
125 exit (1); 165 oval.data = 0xffffffff;
166 if (::conf.icmp_type < 32)
167 oval.data &= ~(1 << ::conf.icmp_type);
168
169 setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval);
126 } 170 }
171#endif
127 172
128#ifdef IP_MTU_DISCOVER 173#if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
129 // this I really consider a linux bug. I am neither connected 174 // this I really consider a linux bug. I am neither connected
130 // nor do I fragment myself. Linux still sets DF and doesn't 175 // nor do I fragment myself. Linux still sets DF and doesn't
131 // fragment for me sometimes. 176 // fragment for me sometimes.
132 { 177 {
133 int oval = IP_PMTUDISC_DONT; 178 int oval = IP_PMTUDISC_DONT;
134 setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); 179 setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval);
135 } 180 }
136#endif 181#endif
137 182
183 sockinfo si (THISNODE, PROT_ICMPv4);
184
185 if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ()))
186 {
187 slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno));
188 exit (1);
189 }
190
138 ipv4_ev_watcher.start (ipv4_fd, POLLIN); 191 icmpv4_ev_watcher.start (icmpv4_fd, POLLIN);
139 } 192 }
193#endif
194
195 tcpv4_fd = -1;
140 196
141#if ENABLE_TCP 197#if ENABLE_TCP
142 if (THISNODE->protocols & PROT_TCPv4) 198 if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port)
143 { 199 {
144 tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); 200 tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
145 201
146 if (tcpv4_fd < 0) 202 if (tcpv4_fd < 0)
147 return -1; 203 return -1;
204
205 fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK);
148 206
149 // standard daemon practise... 207 // standard daemon practise...
150 { 208 {
151 int oval = 1; 209 int oval = 1;
152 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 210 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
185 243
186 return 0; 244 return 0;
187} 245}
188 246
189// send a vpn packet out to other hosts 247// send a vpn packet out to other hosts
190void 248bool
191vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) 249vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos)
192{ 250{
193 switch (si.prot) 251 switch (si.prot)
194 { 252 {
195 case PROT_IPv4: 253 case PROT_IPv4:
196 send_ipv4_packet (pkt, si, tos); 254 return send_ipv4_packet (pkt, si, tos);
197 break;
198 255
199 case PROT_UDPv4: 256 case PROT_UDPv4:
200 send_udpv4_packet (pkt, si, tos); 257 return send_udpv4_packet (pkt, si, tos);
201 break;
202 258
203#if ENABLE_TCP 259#if ENABLE_TCP
204 case PROT_TCPv4: 260 case PROT_TCPv4:
205 send_tcpv4_packet (pkt, si, tos); 261 return send_tcpv4_packet (pkt, si, tos);
206 break; 262#endif
263
264#if ENABLE_ICMP
265 case PROT_ICMPv4:
266 return send_icmpv4_packet (pkt, si, tos);
207#endif 267#endif
208 268
209 default: 269 default:
210 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); 270 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si);
211 abort (); 271 return false;
212 } 272 }
213} 273}
214 274
215void 275bool
216vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 276vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
217{ 277{
278#if defined(SOL_IP) && defined(IP_TOS)
218 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 279 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
280#endif
219 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 281 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
220}
221 282
222void 283 return true;
284}
285
286static u16
287ipv4_checksum (u16 *data, unsigned int len)
288{
289 // use 32 bit accumulator and fold back carry bits at the end
290 u32 sum = 0;
291
292 while (len > 1)
293 {
294 sum += *data++;
295 len -= 2;
296 }
297
298 // odd byte left?
299 if (len)
300 sum += *(u8 *)data;
301
302 // add back carry bits
303 sum = (sum >> 16) + (sum & 0xffff); // lo += hi
304 sum += (sum >> 16); // carry
305
306 return ~sum;
307}
308
309#if ENABLE_ICMP
310bool
311vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
312{
313#if defined(SOL_IP) && defined(IP_TOS)
314 setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
315#endif
316
317 pkt->unshift_hdr (4);
318
319 icmp_header *hdr = (icmp_header *)&((*pkt)[0]);
320 hdr->type = ::conf.icmp_type;
321 hdr->code = 255;
322 hdr->checksum = 0;
323 hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len);
324
325 sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
326
327 return true;
328}
329#endif
330
331bool
223vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 332vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
224{ 333{
334#if defined(SOL_IP) && defined(IP_TOS)
225 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 335 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
336#endif
226 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 337 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
338
339 return true;
340}
341
342void
343vpn::inject_data_packet (tap_packet *pkt, int dst)
344{
345 if (dst)
346 {
347 // unicast
348 if (dst != THISNODE->id)
349 conns[dst - 1]->inject_data_packet (pkt);
350 }
351 else
352 {
353 // broadcast, this is ugly, but due to the security policy
354 // we have to connect to all hosts...
355 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
356 if ((*c)->conf != THISNODE)
357 (*c)->inject_data_packet (pkt, true);
358 }
227} 359}
228 360
229void 361void
230vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) 362vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
231{ 363{
245 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); 377 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ());
246 else 378 else
247 { 379 {
248 connection *c = conns[src - 1]; 380 connection *c = conns[src - 1];
249 381
250 if (dst == 0 && !THISNODE->routerprio) 382 if (dst == 0)
251 slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), 383 slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"),
252 c->conf->nodename, (const char *)rsi); 384 c->conf->nodename, (const char *)rsi);
253 else if (dst != 0 && dst != THISNODE->id) 385 else if (dst != THISNODE->id)
254 { 386 {
255 if (THISNODE->routerprio) 387 if (THISNODE->routerprio)
256 // the tos setting gets lost here. who cares. 388 // the tos setting gets lost here. who cares.
257 conns[dst - 1]->inject_vpn_packet (pkt); 389 conns[dst - 1]->inject_vpn_packet (pkt);
258 else 390 else
265 c->recv_vpn_packet (pkt, rsi); 397 c->recv_vpn_packet (pkt, rsi);
266 } 398 }
267} 399}
268 400
269void 401void
270vpn::udpv4_ev (io_watcher &w, short revents) 402vpn::ipv4_ev (io_watcher &w, short revents)
271{ 403{
272 if (revents & (POLLIN | POLLERR)) 404 if (revents & (POLLIN | POLLERR))
273 { 405 {
274 vpn_packet *pkt = new vpn_packet; 406 vpn_packet *pkt = new vpn_packet;
275 struct sockaddr_in sa; 407 struct sockaddr_in sa;
276 socklen_t sa_len = sizeof (sa); 408 socklen_t sa_len = sizeof (sa);
277 int len; 409 int len;
278 410
279 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); 411 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
280 412
281 sockinfo si(sa, PROT_UDPv4); 413 sockinfo si(sa, PROT_IPv4);
282 414
283 if (len > 0) 415 if (len > 0)
284 { 416 {
285 pkt->len = len; 417 pkt->len = len;
286 418
419 // raw sockets deliver the ipv4, but don't expect it on sends
420 // this is slow, but...
421 pkt->skip_hdr (IP_OVERHEAD);
422
287 recv_vpn_packet (pkt, si); 423 recv_vpn_packet (pkt, si);
288 } 424 }
289 else 425 else
290 { 426 {
291 // probably ECONNRESET or somesuch 427 // probably ECONNRESET or somesuch
292 slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); 428 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno));
293 } 429 }
294 430
295 delete pkt; 431 delete pkt;
296 } 432 }
297 else if (revents & POLLHUP) 433 else if (revents & POLLHUP)
298 { 434 {
299 // this cannot ;) happen on udp sockets 435 // this cannot ;) happen on udp sockets
300 slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating.")); 436 slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating."));
301 exit (1); 437 exit (1);
302 } 438 }
303 else 439 else
304 { 440 {
305 slog (L_ERR, 441 slog (L_ERR,
307 revents); 443 revents);
308 exit (1); 444 exit (1);
309 } 445 }
310} 446}
311 447
448#if ENABLE_ICMP
312void 449void
313vpn::ipv4_ev (io_watcher &w, short revents) 450vpn::icmpv4_ev (io_watcher &w, short revents)
314{ 451{
315 if (revents & (POLLIN | POLLERR)) 452 if (revents & (POLLIN | POLLERR))
316 { 453 {
317 vpn_packet *pkt = new vpn_packet; 454 vpn_packet *pkt = new vpn_packet;
318 struct sockaddr_in sa; 455 struct sockaddr_in sa;
319 socklen_t sa_len = sizeof (sa); 456 socklen_t sa_len = sizeof (sa);
320 int len; 457 int len;
321 458
322 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); 459 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
323 460
324 sockinfo si(sa, PROT_IPv4); 461 sockinfo si(sa, PROT_ICMPv4);
325 462
326 if (len > 0) 463 if (len > 0)
327 { 464 {
328 pkt->len = len; 465 pkt->len = len;
329 466
467 icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]);
468
469 if (hdr->type == ::conf.icmp_type
470 && hdr->code == 255)
471 {
330 // raw sockets deliver the ipv4, but don't expect it on sends 472 // raw sockets deliver the ipv4, but don't expect it on sends
331 // this is slow, but... 473 // this is slow, but...
332 pkt->skip_hdr (IP_OVERHEAD); 474 pkt->skip_hdr (ICMP_OVERHEAD);
333 475
334 recv_vpn_packet (pkt, si); 476 recv_vpn_packet (pkt, si);
477 }
335 } 478 }
336 else 479 else
337 { 480 {
338 // probably ECONNRESET or somesuch 481 // probably ECONNRESET or somesuch
339 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); 482 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno));
342 delete pkt; 485 delete pkt;
343 } 486 }
344 else if (revents & POLLHUP) 487 else if (revents & POLLHUP)
345 { 488 {
346 // this cannot ;) happen on udp sockets 489 // this cannot ;) happen on udp sockets
347 slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); 490 slog (L_ERR, _("FATAL: POLLHUP on icmpv4 fd, terminating."));
348 exit (1); 491 exit (1);
349 } 492 }
350 else 493 else
351 { 494 {
352 slog (L_ERR, 495 slog (L_ERR,
353 _("FATAL: unknown revents %08x in socket, terminating\n"), 496 _("FATAL: unknown revents %08x in socket, terminating\n"),
354 revents); 497 revents);
355 exit (1); 498 exit (1);
356 } 499 }
357} 500}
501#endif
502
503void
504vpn::udpv4_ev (io_watcher &w, short revents)
505{
506 if (revents & (POLLIN | POLLERR))
507 {
508 vpn_packet *pkt = new vpn_packet;
509 struct sockaddr_in sa;
510 socklen_t sa_len = sizeof (sa);
511 int len;
512
513 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
514
515 sockinfo si(sa, PROT_UDPv4);
516
517 if (len > 0)
518 {
519 pkt->len = len;
520
521 recv_vpn_packet (pkt, si);
522 }
523 else
524 {
525 // probably ECONNRESET or somesuch
526 slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno));
527 }
528
529 delete pkt;
530 }
531 else if (revents & POLLHUP)
532 {
533 // this cannot ;) happen on udp sockets
534 slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating."));
535 exit (1);
536 }
537 else
538 {
539 slog (L_ERR,
540 _("FATAL: unknown revents %08x in socket, terminating\n"),
541 revents);
542 exit (1);
543 }
544}
358 545
359void 546void
360vpn::tap_ev (io_watcher &w, short revents) 547vpn::tap_ev (io_watcher &w, short revents)
361{ 548{
362 if (revents & POLLIN) 549 if (revents & POLLIN)
364 /* process data */ 551 /* process data */
365 tap_packet *pkt; 552 tap_packet *pkt;
366 553
367 pkt = tap->recv (); 554 pkt = tap->recv ();
368 555
556 if (!pkt)
557 return;
558
559 if (pkt->len > 14)
560 {
369 int dst = mac2id (pkt->dst); 561 int dst = mac2id (pkt->dst);
370 int src = mac2id (pkt->src); 562 int src = mac2id (pkt->src);
371 563
372 if (src != THISNODE->id) 564 if (src != THISNODE->id)
373 {
374 slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating."));
375 exit (1);
376 }
377
378 if (dst == THISNODE->id)
379 {
380 slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating."));
381 exit (1);
382 }
383
384 if (dst > conns.size ())
385 slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst);
386 else
387 {
388 if (dst)
389 { 565 {
390 // unicast 566 slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting."));
391 if (dst != THISNODE->id) 567 exit (1);
392 conns[dst - 1]->inject_data_packet (pkt);
393 } 568 }
569
570 if (dst == THISNODE->id)
571 {
572 slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting."));
573 exit (1);
574 }
575
576 if (dst > conns.size ())
577 slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst);
394 else 578 else
395 {
396 // broadcast, first check router, then self, then english
397 connection *router = find_router ();
398
399 if (router)
400 router->inject_data_packet (pkt, true); 579 inject_data_packet (pkt, dst);
401 else
402 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
403 if ((*c)->conf != THISNODE)
404 (*c)->inject_data_packet (pkt);
405 }
406 } 580 }
407 581
408 delete pkt; 582 delete pkt;
409 } 583 }
410 else if (revents & (POLLHUP | POLLERR)) 584 else if (revents & (POLLHUP | POLLERR))
476 } 650 }
477} 651}
478 652
479connection *vpn::find_router () 653connection *vpn::find_router ()
480{ 654{
481 u32 prio = 0; 655 u32 prio = 1;
482 connection *router = 0; 656 connection *router = 0;
483 657
484 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) 658 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
485 { 659 {
486 connection *c = *i; 660 connection *c = *i;
487 661
488 if (c->conf->routerprio > prio 662 if (c->conf->routerprio > prio
489 && c->connectmode == conf_node::C_ALWAYS 663 && c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use
490 && c->conf != THISNODE
491 && c->ictx && c->octx) 664 && c->ictx && c->octx
665 && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode
492 { 666 {
493 prio = c->conf->routerprio; 667 prio = c->conf->routerprio;
494 router = c; 668 router = c;
495 } 669 }
496 } 670 }
497 671
498 return router; 672 return router;
499} 673}
500 674
501void vpn::connect_request (int id) 675void vpn::send_connect_request (int id)
502{ 676{
503 connection *c = find_router (); 677 connection *c = find_router ();
504 678
505 if (c) 679 if (c)
506 c->connect_request (id); 680 c->send_connect_request (id);
507 //else // does not work, because all others must connect to the same router 681 else
508 // // no router found, aggressively connect to all routers 682 // no router found, aggressively connect to all routers
509 // for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) 683 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
510 // if ((*i)->conf->routerprio) 684 if ((*i)->conf->routerprio && (*i)->conf != THISNODE)
511 // (*i)->establish_connection (); 685 (*i)->establish_connection ();
512} 686}
513 687
514void 688void
515connection::dump_status () 689connection::dump_status ()
516{ 690{
533 707
534 slog (L_NOTICE, _("END status dump")); 708 slog (L_NOTICE, _("END status dump"));
535} 709}
536 710
537vpn::vpn (void) 711vpn::vpn (void)
538: event(this, &vpn::event_cb) 712: event (this, &vpn::event_cb)
539, udpv4_ev_watcher(this, &vpn::udpv4_ev) 713, udpv4_ev_watcher (this, &vpn::udpv4_ev)
540, ipv4_ev_watcher (this, &vpn::ipv4_ev) 714, ipv4_ev_watcher (this, &vpn::ipv4_ev)
541, tap_ev_watcher (this, &vpn::tap_ev)
542#if ENABLE_TCP 715#if ENABLE_TCP
543, tcpv4_ev_watcher(this, &vpn::tcpv4_ev) 716, tcpv4_ev_watcher (this, &vpn::tcpv4_ev)
544#endif 717#endif
718#if ENABLE_ICMP
719, icmpv4_ev_watcher(this, &vpn::icmpv4_ev)
720#endif
721, tap_ev_watcher (this, &vpn::tap_ev)
545{ 722{
546} 723}
547 724
548vpn::~vpn () 725vpn::~vpn ()
549{ 726{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines