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.37 by pcg, Tue Apr 19 03:47:32 2005 UTC vs.
Revision 1.43 by pcg, Tue Dec 4 15:01:12 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
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 15 GNU General Public License for more details.
16 16
17 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License
18 along with gvpe; if not, write to the Free Software 18 along with gvpe; if not, write to the Free Software
19 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20*/ 20*/
21 21
22#include "config.h" 22#include "config.h"
23 23
24#include <list> 24#include <list>
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
311 exit (EXIT_FAILURE); 329 exit (EXIT_FAILURE);
312 } 330 }
313 331
314 fcntl (tap->fd, F_SETFD, FD_CLOEXEC); 332 fcntl (tap->fd, F_SETFD, FD_CLOEXEC);
315 333
334 run_script_cb cb;
335
336 callback_set (cb, this, vpn, script_if_init);
316 if (tap->if_up () && 337 if (tap->if_up () &&
317 !run_script (run_script_cb (this, &vpn::script_if_init), true)) 338 !run_script (cb, true))
318 { 339 {
319 slog (L_ERR, _("interface initialization command '%s' failed, exiting."), 340 slog (L_ERR, _("interface initialization command '%s' failed, exiting."),
320 tap->if_up ()); 341 tap->if_up ());
321 exit (EXIT_FAILURE); 342 exit (EXIT_FAILURE);
322 } 343 }
323 344
324 if (!run_script (run_script_cb (this, &vpn::script_if_up), true)) 345 callback_set (cb, this, vpn, script_if_up);
346 if (!run_script (cb, true))
325 { 347 {
326 slog (L_ERR, _("if-up command execution failed, exiting.")); 348 slog (L_ERR, _("if-up command execution failed, exiting."));
327 exit (EXIT_FAILURE); 349 exit (EXIT_FAILURE);
328 } 350 }
329 351
330 tap_ev_watcher.start (tap->fd, EVENT_READ); 352 tap_ev_watcher.start (tap->fd, EV_READ);
331 353
332 return 0; 354 return 0;
333} 355}
334 356
335bool 357bool
336vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 358vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
337{ 359{
338#if defined(SOL_IP) && defined(IP_TOS) 360 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 ()); 361 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
342 362
343 return true; 363 return true;
344} 364}
345 365
368 388
369#if ENABLE_ICMP 389#if ENABLE_ICMP
370bool 390bool
371vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 391vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
372{ 392{
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); 393 pkt->unshift_hdr (4);
378 394
379 icmp_header *hdr = (icmp_header *)&((*pkt)[0]); 395 icmp_header *hdr = (icmp_header *)&((*pkt)[0]);
380 hdr->type = ::conf.icmp_type; 396 hdr->type = ::conf.icmp_type;
381 hdr->code = 255; 397 hdr->code = 255;
382 hdr->checksum = 0; 398 hdr->checksum = 0;
383 hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); 399 hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len);
384 400
401 set_tos (icmpv4_fd, icmpv4_tos, tos);
385 sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 402 sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
386 403
387 return true; 404 return true;
388} 405}
389#endif 406#endif
390 407
391bool 408bool
392vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 409vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
393{ 410{
394#if defined(SOL_IP) && defined(IP_TOS) 411 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 ()); 412 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
398 413
399 return true; 414 return true;
400} 415}
401 416
463{ 478{
464 switch (si.prot) 479 switch (si.prot)
465 { 480 {
466 case PROT_IPv4: 481 case PROT_IPv4:
467 return send_ipv4_packet (pkt, si, tos); 482 return send_ipv4_packet (pkt, si, tos);
483
468 case PROT_UDPv4: 484 case PROT_UDPv4:
469 return send_udpv4_packet (pkt, si, tos); 485 return send_udpv4_packet (pkt, si, tos);
486
470#if ENABLE_TCP 487#if ENABLE_TCP
471 case PROT_TCPv4: 488 case PROT_TCPv4:
472 return send_tcpv4_packet (pkt, si, tos); 489 return send_tcpv4_packet (pkt, si, tos);
473#endif 490#endif
474#if ENABLE_ICMP 491#if ENABLE_ICMP
477#endif 494#endif
478#if ENABLE_DNS 495#if ENABLE_DNS
479 case PROT_DNSv4: 496 case PROT_DNSv4:
480 return send_dnsv4_packet (pkt, si, tos); 497 return send_dnsv4_packet (pkt, si, tos);
481#endif 498#endif
482
483 default: 499 default:
484 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); 500 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si);
485 } 501 }
486 502
487 return false; 503 return false;
488} 504}
489 505
490void 506void
491vpn::ipv4_ev (io_watcher &w, short revents) 507vpn::ipv4_ev (ev::io &w, int revents)
492{ 508{
493 if (revents & EVENT_READ) 509 if (revents & EV_READ)
494 { 510 {
495 vpn_packet *pkt = new vpn_packet; 511 vpn_packet *pkt = new vpn_packet;
496 struct sockaddr_in sa; 512 struct sockaddr_in sa;
497 socklen_t sa_len = sizeof (sa); 513 socklen_t sa_len = sizeof (sa);
498 int len; 514 int len;
527 } 543 }
528} 544}
529 545
530#if ENABLE_ICMP 546#if ENABLE_ICMP
531void 547void
532vpn::icmpv4_ev (io_watcher &w, short revents) 548vpn::icmpv4_ev (ev::io &w, int revents)
533{ 549{
534 if (revents & EVENT_READ) 550 if (revents & EV_READ)
535 { 551 {
536 vpn_packet *pkt = new vpn_packet; 552 vpn_packet *pkt = new vpn_packet;
537 struct sockaddr_in sa; 553 struct sockaddr_in sa;
538 socklen_t sa_len = sizeof (sa); 554 socklen_t sa_len = sizeof (sa);
539 int len; 555 int len;
575 } 591 }
576} 592}
577#endif 593#endif
578 594
579void 595void
580vpn::udpv4_ev (io_watcher &w, short revents) 596vpn::udpv4_ev (ev::io &w, int revents)
581{ 597{
582 if (revents & EVENT_READ) 598 if (revents & EV_READ)
583 { 599 {
584 vpn_packet *pkt = new vpn_packet; 600 vpn_packet *pkt = new vpn_packet;
585 struct sockaddr_in sa; 601 struct sockaddr_in sa;
586 socklen_t sa_len = sizeof (sa); 602 socklen_t sa_len = sizeof (sa);
587 int len; 603 int len;
612 exit (EXIT_FAILURE); 628 exit (EXIT_FAILURE);
613 } 629 }
614} 630}
615 631
616void 632void
617vpn::tap_ev (io_watcher &w, short revents) 633vpn::tap_ev (ev::io &w, int revents)
618{ 634{
619 if (revents & EVENT_READ) 635 if (revents & EV_READ)
620 { 636 {
621 /* process data */ 637 /* process data */
622 tap_packet *pkt; 638 tap_packet *pkt;
623 639
624 pkt = tap->recv (); 640 pkt = tap->recv ();
654 else 670 else
655 abort (); 671 abort ();
656} 672}
657 673
658void 674void
659vpn::event_cb (time_watcher &w) 675vpn::event_cb (ev::timer &w, int)
660{ 676{
661 if (events) 677 if (events)
662 { 678 {
663 if (events & EVENT_SHUTDOWN) 679 if (events & EVENT_SHUTDOWN)
664 { 680 {
755} 771}
756 772
757void 773void
758vpn::dump_status () 774vpn::dump_status ()
759{ 775{
760 slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)NOW); 776 slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)ev_now ());
761 777
762 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) 778 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
763 (*c)->dump_status (); 779 (*c)->dump_status ();
764 780
765 slog (L_NOTICE, _("END status dump")); 781 slog (L_NOTICE, _("END status dump"));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines