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.38 by pcg, Tue Apr 26 00:55:56 2005 UTC vs.
Revision 1.42 by pcg, Sun Dec 2 00:54:52 2007 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-2005 Marc Lehmann <gvpe@schmorp.de> 3 Copyright (C) 2003-2007 Marc Lehmann <gvpe@schmorp.de>
4 4
5 This file is part of GVPE. 5 This file is part of GVPE.
6 6
7 GVPE is free software; you can redistribute it and/or modify 7 GVPE is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by 8 it under the terms of the GNU General Public License as published by
46 46
47vpn network; // THE vpn (bad design...) 47vpn network; // THE vpn (bad design...)
48 48
49///////////////////////////////////////////////////////////////////////////// 49/////////////////////////////////////////////////////////////////////////////
50 50
51static void inline
52set_tos (int fd, int &tos_prev, int tos)
53{
54#if defined(SOL_IP) && defined(IP_TOS)
55 if (tos_prev == tos)
56 return;
57
58 tos_prev = tos;
59 setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos);
60#endif
61}
62
51void 63void
52vpn::script_init_env () 64vpn::script_init_env ()
53{ 65{
54 // the tunnel device mtu should be the physical mtu - overhead 66 // the tunnel device mtu should be the physical mtu - overhead
55 // the tricky part is rounding to the cipher key blocksize 67 // the tricky part is rounding to the cipher key blocksize
98} 110}
99 111
100int 112int
101vpn::setup () 113vpn::setup ()
102{ 114{
115 ipv4_tos = -1;
103 ipv4_fd = -1; 116 ipv4_fd = -1;
104 117
105 if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto) 118 if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto)
106 { 119 {
107 ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); 120 ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto);
108 121
128 { 141 {
129 slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); 142 slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno));
130 exit (EXIT_FAILURE); 143 exit (EXIT_FAILURE);
131 } 144 }
132 145
133 ipv4_ev_watcher.start (ipv4_fd, EVENT_READ); 146 ipv4_ev_watcher.start (ipv4_fd, EV_READ);
134 } 147 }
135 148
149 udpv4_tos = -1;
136 udpv4_fd = -1; 150 udpv4_fd = -1;
137 151
138 if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) 152 if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port)
139 { 153 {
140 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); 154 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
141 155
167 { 181 {
168 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); 182 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno));
169 exit (EXIT_FAILURE); 183 exit (EXIT_FAILURE);
170 } 184 }
171 185
172 udpv4_ev_watcher.start (udpv4_fd, EVENT_READ); 186 udpv4_ev_watcher.start (udpv4_fd, EV_READ);
173 } 187 }
174 188
189 icmpv4_tos = -1;
175 icmpv4_fd = -1; 190 icmpv4_fd = -1;
176 191
177#if ENABLE_ICMP 192#if ENABLE_ICMP
178 if (THISNODE->protocols & PROT_ICMPv4) 193 if (THISNODE->protocols & PROT_ICMPv4)
179 { 194 {
180 icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); 195 icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP);
212 { 227 {
213 slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); 228 slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno));
214 exit (EXIT_FAILURE); 229 exit (EXIT_FAILURE);
215 } 230 }
216 231
217 icmpv4_ev_watcher.start (icmpv4_fd, EVENT_READ); 232 icmpv4_ev_watcher.start (icmpv4_fd, EV_READ);
218 } 233 }
219#endif 234#endif
220 235
221 tcpv4_fd = -1; 236 tcpv4_fd = -1;
222 237
249 { 264 {
250 slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno)); 265 slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno));
251 exit (EXIT_FAILURE); 266 exit (EXIT_FAILURE);
252 } 267 }
253 268
254 tcpv4_ev_watcher.start (tcpv4_fd, EVENT_READ); 269 tcpv4_ev_watcher.start (tcpv4_fd, EV_READ);
255 } 270 }
256#endif 271#endif
272
273 dnsv4_tos = -1;
274 dnsv4_fd = -1;
257 275
258#if ENABLE_DNS 276#if ENABLE_DNS
259 if (THISNODE->protocols & PROT_DNSv4) 277 if (THISNODE->protocols & PROT_DNSv4)
260 { 278 {
261 dns_forwarder.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); 279 dns_forwarder.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4);
292 { 310 {
293 slog (L_ERR, _("can't bind dnsv4 on %s: %s"), (const char *)si, strerror (errno)); 311 slog (L_ERR, _("can't bind dnsv4 on %s: %s"), (const char *)si, strerror (errno));
294 exit (EXIT_FAILURE); 312 exit (EXIT_FAILURE);
295 } 313 }
296 314
297 dnsv4_ev_watcher.start (dnsv4_fd, EVENT_READ); 315 dnsv4_ev_watcher.start (dnsv4_fd, EV_READ);
298 } 316 }
299#endif 317#endif
300 318
301 ///////////////////////////////////////////////////////////////////////////// 319 /////////////////////////////////////////////////////////////////////////////
302 320
325 { 343 {
326 slog (L_ERR, _("if-up command execution failed, exiting.")); 344 slog (L_ERR, _("if-up command execution failed, exiting."));
327 exit (EXIT_FAILURE); 345 exit (EXIT_FAILURE);
328 } 346 }
329 347
330 tap_ev_watcher.start (tap->fd, EVENT_READ); 348 tap_ev_watcher.start (tap->fd, EV_READ);
331 349
332 return 0; 350 return 0;
333} 351}
334 352
335bool 353bool
336vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 354vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
337{ 355{
338#if defined(SOL_IP) && defined(IP_TOS) 356 set_tos (ipv4_fd, ipv4_tos, tos);
339 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
340#endif
341 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 357 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
342 358
343 return true; 359 return true;
344} 360}
345 361
368 384
369#if ENABLE_ICMP 385#if ENABLE_ICMP
370bool 386bool
371vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 387vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
372{ 388{
373#if defined(SOL_IP) && defined(IP_TOS)
374 setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
375#endif
376
377 pkt->unshift_hdr (4); 389 pkt->unshift_hdr (4);
378 390
379 icmp_header *hdr = (icmp_header *)&((*pkt)[0]); 391 icmp_header *hdr = (icmp_header *)&((*pkt)[0]);
380 hdr->type = ::conf.icmp_type; 392 hdr->type = ::conf.icmp_type;
381 hdr->code = 255; 393 hdr->code = 255;
382 hdr->checksum = 0; 394 hdr->checksum = 0;
383 hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); 395 hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len);
384 396
397 set_tos (icmpv4_fd, icmpv4_tos, tos);
385 sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 398 sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
386 399
387 return true; 400 return true;
388} 401}
389#endif 402#endif
390 403
391bool 404bool
392vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 405vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
393{ 406{
394#if defined(SOL_IP) && defined(IP_TOS) 407 set_tos (udpv4_fd, udpv4_tos, tos);
395 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
396#endif
397 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 408 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
398 409
399 return true; 410 return true;
400} 411}
401 412
463{ 474{
464 switch (si.prot) 475 switch (si.prot)
465 { 476 {
466 case PROT_IPv4: 477 case PROT_IPv4:
467 return send_ipv4_packet (pkt, si, tos); 478 return send_ipv4_packet (pkt, si, tos);
479
468 case PROT_UDPv4: 480 case PROT_UDPv4:
469 return send_udpv4_packet (pkt, si, tos); 481 return send_udpv4_packet (pkt, si, tos);
482
470#if ENABLE_TCP 483#if ENABLE_TCP
471 case PROT_TCPv4: 484 case PROT_TCPv4:
472 return send_tcpv4_packet (pkt, si, tos); 485 return send_tcpv4_packet (pkt, si, tos);
473#endif 486#endif
474#if ENABLE_ICMP 487#if ENABLE_ICMP
477#endif 490#endif
478#if ENABLE_DNS 491#if ENABLE_DNS
479 case PROT_DNSv4: 492 case PROT_DNSv4:
480 return send_dnsv4_packet (pkt, si, tos); 493 return send_dnsv4_packet (pkt, si, tos);
481#endif 494#endif
482
483 default: 495 default:
484 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); 496 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si);
485 } 497 }
486 498
487 return false; 499 return false;
488} 500}
489 501
490void 502void
491vpn::ipv4_ev (io_watcher &w, short revents) 503vpn::ipv4_ev (ev::io &w, int revents)
492{ 504{
493 if (revents & EVENT_READ) 505 if (revents & EV_READ)
494 { 506 {
495 vpn_packet *pkt = new vpn_packet; 507 vpn_packet *pkt = new vpn_packet;
496 struct sockaddr_in sa; 508 struct sockaddr_in sa;
497 socklen_t sa_len = sizeof (sa); 509 socklen_t sa_len = sizeof (sa);
498 int len; 510 int len;
527 } 539 }
528} 540}
529 541
530#if ENABLE_ICMP 542#if ENABLE_ICMP
531void 543void
532vpn::icmpv4_ev (io_watcher &w, short revents) 544vpn::icmpv4_ev (ev::io &w, int revents)
533{ 545{
534 if (revents & EVENT_READ) 546 if (revents & EV_READ)
535 { 547 {
536 vpn_packet *pkt = new vpn_packet; 548 vpn_packet *pkt = new vpn_packet;
537 struct sockaddr_in sa; 549 struct sockaddr_in sa;
538 socklen_t sa_len = sizeof (sa); 550 socklen_t sa_len = sizeof (sa);
539 int len; 551 int len;
575 } 587 }
576} 588}
577#endif 589#endif
578 590
579void 591void
580vpn::udpv4_ev (io_watcher &w, short revents) 592vpn::udpv4_ev (ev::io &w, int revents)
581{ 593{
582 if (revents & EVENT_READ) 594 if (revents & EV_READ)
583 { 595 {
584 vpn_packet *pkt = new vpn_packet; 596 vpn_packet *pkt = new vpn_packet;
585 struct sockaddr_in sa; 597 struct sockaddr_in sa;
586 socklen_t sa_len = sizeof (sa); 598 socklen_t sa_len = sizeof (sa);
587 int len; 599 int len;
612 exit (EXIT_FAILURE); 624 exit (EXIT_FAILURE);
613 } 625 }
614} 626}
615 627
616void 628void
617vpn::tap_ev (io_watcher &w, short revents) 629vpn::tap_ev (ev::io &w, int revents)
618{ 630{
619 if (revents & EVENT_READ) 631 if (revents & EV_READ)
620 { 632 {
621 /* process data */ 633 /* process data */
622 tap_packet *pkt; 634 tap_packet *pkt;
623 635
624 pkt = tap->recv (); 636 pkt = tap->recv ();
654 else 666 else
655 abort (); 667 abort ();
656} 668}
657 669
658void 670void
659vpn::event_cb (time_watcher &w) 671vpn::event_cb (ev::timer &w, int)
660{ 672{
661 if (events) 673 if (events)
662 { 674 {
663 if (events & EVENT_SHUTDOWN) 675 if (events & EVENT_SHUTDOWN)
664 { 676 {
755} 767}
756 768
757void 769void
758vpn::dump_status () 770vpn::dump_status ()
759{ 771{
760 slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)NOW); 772 slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)ev_now ());
761 773
762 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) 774 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
763 (*c)->dump_status (); 775 (*c)->dump_status ();
764 776
765 slog (L_NOTICE, _("END status dump")); 777 slog (L_NOTICE, _("END status dump"));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines