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.40 by pcg, Sat Dec 1 23:35:31 2007 UTC vs.
Revision 1.44 by pcg, Tue Dec 4 17:17:20 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
131 } 144 }
132 145
133 ipv4_ev_watcher.start (ipv4_fd, EV_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
170 } 184 }
171 185
172 udpv4_ev_watcher.start (udpv4_fd, EV_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);
252 } 267 }
253 268
254 tcpv4_ev_watcher.start (tcpv4_fd, EV_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);
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 cb.set<vpn, &vpn::script_if_init> (this);
336
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 cb.set<vpn, &vpn::script_if_up> (this);
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
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;
764 780
765 slog (L_NOTICE, _("END status dump")); 781 slog (L_NOTICE, _("END status dump"));
766} 782}
767 783
768vpn::vpn (void) 784vpn::vpn (void)
785{
769: event (this, &vpn::event_cb) 786 event .set<vpn, &vpn::event_cb > (this);
770, udpv4_ev_watcher (this, &vpn::udpv4_ev) 787 udpv4_ev_watcher .set<vpn, &vpn::udpv4_ev > (this);
771, ipv4_ev_watcher (this, &vpn::ipv4_ev) 788 ipv4_ev_watcher .set<vpn, &vpn::ipv4_ev > (this);
772#if ENABLE_TCP 789#if ENABLE_TCP
773, tcpv4_ev_watcher (this, &vpn::tcpv4_ev) 790 tcpv4_ev_watcher .set<vpn, &vpn::tcpv4_ev > (this);
774#endif 791#endif
775#if ENABLE_ICMP 792#if ENABLE_ICMP
776, icmpv4_ev_watcher(this, &vpn::icmpv4_ev) 793 icmpv4_ev_watcher.set<vpn, &vpn::icmpv4_ev> (this);
777#endif 794#endif
778#if ENABLE_DNS 795#if ENABLE_DNS
779, dnsv4_ev_watcher (this, &vpn::dnsv4_ev) 796 dnsv4_ev_watcher .set<vpn, &vpn::dnsv4_ev > (this);
780#endif 797#endif
781, tap_ev_watcher (this, &vpn::tap_ev) 798 tap_ev_watcher .set<vpn, &vpn::tap_ev > (this);
782{
783} 799}
784 800
785vpn::~vpn () 801vpn::~vpn ()
786{ 802{
787} 803}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines