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.7 by pcg, Sun Apr 6 18:12:18 2003 UTC vs.
Revision 1.11 by pcg, Sun Apr 13 00:35:46 2003 UTC

26 26
27#include <sys/types.h> 27#include <sys/types.h>
28#include <sys/socket.h> 28#include <sys/socket.h>
29#include <sys/poll.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 <arpa/inet.h>
36#include <netinet/in.h>
37#include <netinet/ip_icmp.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"
71} 73}
72 74
73int 75int
74vpn::setup () 76vpn::setup ()
75{ 77{
78 ipv4_fd = -1;
79
80 if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto)
81 {
82 ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto);
83
84 if (ipv4_fd < 0)
85 return -1;
86
87 fcntl (ipv4_fd, F_SETFL, O_NONBLOCK);
88
89#ifdef IP_MTU_DISCOVER
90 // this I really consider a linux bug. I am neither connected
91 // nor do I fragment myself. Linux still sets DF and doesn't
92 // fragment for me sometimes.
93 {
94 int oval = IP_PMTUDISC_DONT;
95 setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval);
96 }
97#endif
98
99 sockinfo si (THISNODE, PROT_IPv4);
100
101 if (bind (ipv4_fd, si.sav4 (), si.salenv4 ()))
102 {
103 slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno));
104 exit (1);
105 }
106
107 ipv4_ev_watcher.start (ipv4_fd, POLLIN);
108 }
109
76 udpv4_fd = -1; 110 udpv4_fd = -1;
77 111
78 if (THISNODE->protocols & PROT_UDPv4) 112 if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port)
79 { 113 {
80 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); 114 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
81 115
82 if (udpv4_fd < 0) 116 if (udpv4_fd < 0)
83 return -1; 117 return -1;
118
119 fcntl (udpv4_fd, F_SETFL, O_NONBLOCK);
84 120
85 // standard daemon practise... 121 // standard daemon practise...
86 { 122 {
87 int oval = 1; 123 int oval = 1;
88 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 124 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
89 } 125 }
90
91 sockinfo si (THISNODE, PROT_UDPv4);
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 126
99#ifdef IP_MTU_DISCOVER 127#ifdef IP_MTU_DISCOVER
100 // this I really consider a linux bug. I am neither connected 128 // this I really consider a linux bug. I am neither connected
101 // nor do I fragment myself. Linux still sets DF and doesn't 129 // nor do I fragment myself. Linux still sets DF and doesn't
102 // fragment for me sometimes. 130 // fragment for me sometimes.
104 int oval = IP_PMTUDISC_DONT; 132 int oval = IP_PMTUDISC_DONT;
105 setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); 133 setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval);
106 } 134 }
107#endif 135#endif
108 136
137 sockinfo si (THISNODE, PROT_UDPv4);
138
139 if (bind (udpv4_fd, si.sav4 (), si.salenv4 ()))
140 {
141 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno));
142 exit (1);
143 }
144
109 udpv4_ev_watcher.start (udpv4_fd, POLLIN); 145 udpv4_ev_watcher.start (udpv4_fd, POLLIN);
110 } 146 }
111 147
112 ipv4_fd = -1; 148 icmpv4_fd = -1;
149
150#if ENABLE_ICMP
113 if (THISNODE->protocols & PROT_IPv4) 151 if (THISNODE->protocols & PROT_ICMPv4)
114 { 152 {
115 ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); 153 icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP);
116 154
117 if (ipv4_fd < 0) 155 if (icmpv4_fd < 0)
118 return -1; 156 return -1;
119 157
120 sockinfo si (THISNODE, PROT_IPv4); 158 fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK);
121 159
122 if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) 160#ifdef ICMP_FILTER
123 { 161 {
124 slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); 162 icmp_filter oval;
125 exit (1); 163 oval.data = 0xffffffff;
164 if (::conf.icmp_type < 32)
165 oval.data &= ~(1 << ::conf.icmp_type);
166
167 setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval);
126 } 168 }
169#endif
127 170
128#ifdef IP_MTU_DISCOVER 171#ifdef IP_MTU_DISCOVER
129 // this I really consider a linux bug. I am neither connected 172 // this I really consider a linux bug. I am neither connected
130 // nor do I fragment myself. Linux still sets DF and doesn't 173 // nor do I fragment myself. Linux still sets DF and doesn't
131 // fragment for me sometimes. 174 // fragment for me sometimes.
132 { 175 {
133 int oval = IP_PMTUDISC_DONT; 176 int oval = IP_PMTUDISC_DONT;
134 setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); 177 setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval);
135 } 178 }
136#endif 179#endif
137 180
181 sockinfo si (THISNODE, PROT_ICMPv4);
182
183 if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ()))
184 {
185 slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno));
186 exit (1);
187 }
188
138 ipv4_ev_watcher.start (ipv4_fd, POLLIN); 189 icmpv4_ev_watcher.start (icmpv4_fd, POLLIN);
139 } 190 }
191#endif
192
193 tcpv4_fd = -1;
140 194
141#if ENABLE_TCP 195#if ENABLE_TCP
142 if (THISNODE->protocols & PROT_TCPv4) 196 if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port)
143 { 197 {
144 tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); 198 tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
145 199
146 if (tcpv4_fd < 0) 200 if (tcpv4_fd < 0)
147 return -1; 201 return -1;
202
203 fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK);
148 204
149 // standard daemon practise... 205 // standard daemon practise...
150 { 206 {
151 int oval = 1; 207 int oval = 1;
152 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 208 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
201#if ENABLE_TCP 257#if ENABLE_TCP
202 case PROT_TCPv4: 258 case PROT_TCPv4:
203 return send_tcpv4_packet (pkt, si, tos); 259 return send_tcpv4_packet (pkt, si, tos);
204#endif 260#endif
205 261
262#if ENABLE_ICMP
263 case PROT_ICMPv4:
264 return send_icmpv4_packet (pkt, si, tos);
265#endif
266
206 default: 267 default:
207 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); 268 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si);
208 return false; 269 return false;
209 } 270 }
210} 271}
212bool 273bool
213vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 274vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
214{ 275{
215 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 276 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
216 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 277 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
278
279 return true;
280}
281
282static u16
283ipv4_checksum (u16 *data, unsigned int len)
284{
285 // use 32 bit accumulator and fold back carry bits at the end
286 u32 sum = 0;
287
288 while (len > 1)
289 {
290 sum += *data++;
291 len -= 2;
292 }
293
294 // odd byte left?
295 if (len)
296 sum += *(u8 *)data;
297
298 // add back carry bits
299 sum = (sum >> 16) + (sum & 0xffff); // lo += hi
300 sum += (sum >> 16); // carry
301
302 return ~sum;
303}
304
305bool
306vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
307{
308 setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
309
310 pkt->unshift_hdr (4);
311
312 icmphdr *hdr = (icmphdr *)&((*pkt)[0]);
313 hdr->type = ::conf.icmp_type;
314 hdr->code = 255;
315 hdr->checksum = 0;
316 hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len);
317
318 sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
217 319
218 return true; 320 return true;
219} 321}
220 322
221bool 323bool
266 c->recv_vpn_packet (pkt, rsi); 368 c->recv_vpn_packet (pkt, rsi);
267 } 369 }
268} 370}
269 371
270void 372void
271vpn::udpv4_ev (io_watcher &w, short revents) 373vpn::ipv4_ev (io_watcher &w, short revents)
272{ 374{
273 if (revents & (POLLIN | POLLERR)) 375 if (revents & (POLLIN | POLLERR))
274 { 376 {
275 vpn_packet *pkt = new vpn_packet; 377 vpn_packet *pkt = new vpn_packet;
276 struct sockaddr_in sa; 378 struct sockaddr_in sa;
277 socklen_t sa_len = sizeof (sa); 379 socklen_t sa_len = sizeof (sa);
278 int len; 380 int len;
279 381
280 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); 382 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
281 383
282 sockinfo si(sa, PROT_UDPv4); 384 sockinfo si(sa, PROT_IPv4);
283 385
284 if (len > 0) 386 if (len > 0)
285 { 387 {
286 pkt->len = len; 388 pkt->len = len;
287 389
390 // raw sockets deliver the ipv4, but don't expect it on sends
391 // this is slow, but...
392 pkt->skip_hdr (IP_OVERHEAD);
393
288 recv_vpn_packet (pkt, si); 394 recv_vpn_packet (pkt, si);
289 } 395 }
290 else 396 else
291 { 397 {
292 // probably ECONNRESET or somesuch 398 // probably ECONNRESET or somesuch
293 slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); 399 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno));
294 } 400 }
295 401
296 delete pkt; 402 delete pkt;
297 } 403 }
298 else if (revents & POLLHUP) 404 else if (revents & POLLHUP)
299 { 405 {
300 // this cannot ;) happen on udp sockets 406 // this cannot ;) happen on udp sockets
301 slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating.")); 407 slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating."));
302 exit (1); 408 exit (1);
303 } 409 }
304 else 410 else
305 { 411 {
306 slog (L_ERR, 412 slog (L_ERR,
309 exit (1); 415 exit (1);
310 } 416 }
311} 417}
312 418
313void 419void
314vpn::ipv4_ev (io_watcher &w, short revents) 420vpn::icmpv4_ev (io_watcher &w, short revents)
315{ 421{
316 if (revents & (POLLIN | POLLERR)) 422 if (revents & (POLLIN | POLLERR))
317 { 423 {
318 vpn_packet *pkt = new vpn_packet; 424 vpn_packet *pkt = new vpn_packet;
319 struct sockaddr_in sa; 425 struct sockaddr_in sa;
320 socklen_t sa_len = sizeof (sa); 426 socklen_t sa_len = sizeof (sa);
321 int len; 427 int len;
322 428
323 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); 429 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
324 430
325 sockinfo si(sa, PROT_IPv4); 431 sockinfo si(sa, PROT_ICMPv4);
326 432
327 if (len > 0) 433 if (len > 0)
328 { 434 {
329 pkt->len = len; 435 pkt->len = len;
330 436
437 icmphdr *hdr = (icmphdr *)&((*pkt)[IP_OVERHEAD]);
438
439 if (hdr->type == ::conf.icmp_type
440 && hdr->code == 255)
441 {
331 // raw sockets deliver the ipv4, but don't expect it on sends 442 // raw sockets deliver the ipv4, but don't expect it on sends
332 // this is slow, but... 443 // this is slow, but...
333 pkt->skip_hdr (IP_OVERHEAD); 444 pkt->skip_hdr (ICMP_OVERHEAD);
334 445
335 recv_vpn_packet (pkt, si); 446 recv_vpn_packet (pkt, si);
447 }
336 } 448 }
337 else 449 else
338 { 450 {
339 // probably ECONNRESET or somesuch 451 // probably ECONNRESET or somesuch
340 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); 452 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno));
343 delete pkt; 455 delete pkt;
344 } 456 }
345 else if (revents & POLLHUP) 457 else if (revents & POLLHUP)
346 { 458 {
347 // this cannot ;) happen on udp sockets 459 // this cannot ;) happen on udp sockets
348 slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); 460 slog (L_ERR, _("FATAL: POLLHUP on icmpv4 fd, terminating."));
461 exit (1);
462 }
463 else
464 {
465 slog (L_ERR,
466 _("FATAL: unknown revents %08x in socket, terminating\n"),
467 revents);
468 exit (1);
469 }
470}
471
472void
473vpn::udpv4_ev (io_watcher &w, short revents)
474{
475 if (revents & (POLLIN | POLLERR))
476 {
477 vpn_packet *pkt = new vpn_packet;
478 struct sockaddr_in sa;
479 socklen_t sa_len = sizeof (sa);
480 int len;
481
482 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
483
484 sockinfo si(sa, PROT_UDPv4);
485
486 if (len > 0)
487 {
488 pkt->len = len;
489
490 recv_vpn_packet (pkt, si);
491 }
492 else
493 {
494 // probably ECONNRESET or somesuch
495 slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno));
496 }
497
498 delete pkt;
499 }
500 else if (revents & POLLHUP)
501 {
502 // this cannot ;) happen on udp sockets
503 slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating."));
349 exit (1); 504 exit (1);
350 } 505 }
351 else 506 else
352 { 507 {
353 slog (L_ERR, 508 slog (L_ERR,
485 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) 640 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
486 { 641 {
487 connection *c = *i; 642 connection *c = *i;
488 643
489 if (c->conf->routerprio > prio 644 if (c->conf->routerprio > prio
490 && c->connectmode == conf_node::C_ALWAYS 645 && c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use
491 && c->conf != THISNODE
492 && c->ictx && c->octx) 646 && c->ictx && c->octx
647 && c->conf != THISNODE // redundant, since ictx==octx==0 always on thisnode
648 && (!THISNODE->routerprio
649 || c->conf->routerprio <= THISNODE->routerprio))
493 { 650 {
494 prio = c->conf->routerprio; 651 prio = c->conf->routerprio;
495 router = c; 652 router = c;
496 } 653 }
497 } 654 }
534 691
535 slog (L_NOTICE, _("END status dump")); 692 slog (L_NOTICE, _("END status dump"));
536} 693}
537 694
538vpn::vpn (void) 695vpn::vpn (void)
539: event(this, &vpn::event_cb) 696: event (this, &vpn::event_cb)
540, udpv4_ev_watcher(this, &vpn::udpv4_ev) 697, udpv4_ev_watcher (this, &vpn::udpv4_ev)
541, ipv4_ev_watcher (this, &vpn::ipv4_ev) 698, ipv4_ev_watcher (this, &vpn::ipv4_ev)
542, tap_ev_watcher (this, &vpn::tap_ev)
543#if ENABLE_TCP 699#if ENABLE_TCP
544, tcpv4_ev_watcher(this, &vpn::tcpv4_ev) 700, tcpv4_ev_watcher (this, &vpn::tcpv4_ev)
545#endif 701#endif
702#if ENABLE_ICMP
703, icmpv4_ev_watcher(this, &vpn::icmpv4_ev)
704#endif
705, tap_ev_watcher (this, &vpn::tap_ev)
546{ 706{
547} 707}
548 708
549vpn::~vpn () 709vpn::~vpn ()
550{ 710{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines