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.30 by pcg, Fri Mar 4 20:26:50 2005 UTC vs.
Revision 1.39 by pcg, Sat Nov 10 05:14:22 2007 UTC

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
51const char *vpn::script_if_up () 51void
52vpn::script_init_env ()
52{ 53{
53 // the tunnel device mtu should be the physical mtu - overhead 54 // the tunnel device mtu should be the physical mtu - overhead
54 // the tricky part is rounding to the cipher key blocksize 55 // the tricky part is rounding to the cipher key blocksize
55 int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD; 56 int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD;
56 mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion 57 mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion
57 mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round 58 mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round
58 mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again 59 mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again
59 60
60 char *env; 61 char *env;
61 asprintf (&env, "CONFBASE=%s", confbase); putenv (env); 62 asprintf (&env, "CONFBASE=%s", confbase); putenv (env);
62 asprintf (&env, "NODENAME=%s", THISNODE->nodename); putenv (env);
63 asprintf (&env, "NODEID=%d", THISNODE->id); putenv (env);
64 asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env); 63 asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env);
65 asprintf (&env, "IFTYPE=%s", IFTYPE); putenv (env); 64 asprintf (&env, "IFTYPE=%s", IFTYPE); putenv (env);
66 asprintf (&env, "IFSUBTYPE=%s", IFSUBTYPE); putenv (env); 65 asprintf (&env, "IFSUBTYPE=%s", IFSUBTYPE); putenv (env);
67 asprintf (&env, "MTU=%d", mtu); putenv (env); 66 asprintf (&env, "MTU=%d", mtu); putenv (env);
68 asprintf (&env, "MAC=%02x:%02x:%02x:%02x:%02x:%02x", 67 asprintf (&env, "NODES=%d", conns.size ()); putenv (env);
69 0xfe, 0xfd, 0x80, 0x00, THISNODE->id >> 8, 68 asprintf (&env, "NODEID=%d", THISNODE->id); putenv (env);
70 THISNODE->id & 0xff);
71 putenv (env);
72 69
70 conns [THISNODE->id - 1]->script_init_env ("");
71
72 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
73 {
74 char ext[16];
75 snprintf (ext, 16, "_%d", (*c)->conf->id);
76 (*c)->script_init_env (ext);
77 }
78}
79
80const char *vpn::script_if_init ()
81{
82 script_init_env ();
83
84 return tap->if_up ();
85}
86
87const char *vpn::script_if_up ()
88{
89 script_init_env ();
90
91 char *filename;
92 asprintf (&filename,
93 "%s/%s",
94 confbase,
73 return ::conf.script_if_up ? ::conf.script_if_up : "if-up"; 95 ::conf.script_if_up ? ::conf.script_if_up : "if-up");
96
97 return filename;
74} 98}
75 99
76int 100int
77vpn::setup () 101vpn::setup ()
78{ 102{
84 108
85 if (ipv4_fd < 0) 109 if (ipv4_fd < 0)
86 return -1; 110 return -1;
87 111
88 fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); 112 fcntl (ipv4_fd, F_SETFL, O_NONBLOCK);
113 fcntl (ipv4_fd, F_SETFD, FD_CLOEXEC);
89 114
90#if defined(SOL_IP) && defined(IP_MTU_DISCOVER) 115#if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
91 // this I really consider a linux bug. I am neither connected 116 // this I really consider a linux bug. I am neither connected
92 // nor do I fragment myself. Linux still sets DF and doesn't 117 // nor do I fragment myself. Linux still sets DF and doesn't
93 // fragment for me sometimes. 118 // fragment for me sometimes.
103 { 128 {
104 slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); 129 slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno));
105 exit (EXIT_FAILURE); 130 exit (EXIT_FAILURE);
106 } 131 }
107 132
108 ipv4_ev_watcher.start (ipv4_fd, EVENT_READ); 133 ipv4_ev_watcher.start (ipv4_fd, EV_READ);
109 } 134 }
110 135
111 udpv4_fd = -1; 136 udpv4_fd = -1;
112 137
113 if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) 138 if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port)
116 141
117 if (udpv4_fd < 0) 142 if (udpv4_fd < 0)
118 return -1; 143 return -1;
119 144
120 fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); 145 fcntl (udpv4_fd, F_SETFL, O_NONBLOCK);
146 fcntl (udpv4_fd, F_SETFD, FD_CLOEXEC);
121 147
122 // standard daemon practise... 148 // standard daemon practise...
123 { 149 {
124 int oval = 1; 150 int oval = 1;
125 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 151 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
141 { 167 {
142 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); 168 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno));
143 exit (EXIT_FAILURE); 169 exit (EXIT_FAILURE);
144 } 170 }
145 171
146 udpv4_ev_watcher.start (udpv4_fd, EVENT_READ); 172 udpv4_ev_watcher.start (udpv4_fd, EV_READ);
147 } 173 }
148 174
149 icmpv4_fd = -1; 175 icmpv4_fd = -1;
150 176
151#if ENABLE_ICMP 177#if ENABLE_ICMP
155 181
156 if (icmpv4_fd < 0) 182 if (icmpv4_fd < 0)
157 return -1; 183 return -1;
158 184
159 fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); 185 fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK);
186 fcntl (icmpv4_fd, F_SETFD, FD_CLOEXEC);
160 187
161#ifdef ICMP_FILTER 188#ifdef ICMP_FILTER
162 { 189 {
163 icmp_filter oval; 190 icmp_filter oval;
164 oval.data = 0xffffffff; 191 oval.data = 0xffffffff;
173 // this I really consider a linux bug. I am neither connected 200 // this I really consider a linux bug. I am neither connected
174 // nor do I fragment myself. Linux still sets DF and doesn't 201 // nor do I fragment myself. Linux still sets DF and doesn't
175 // fragment for me sometimes. 202 // fragment for me sometimes.
176 { 203 {
177 int oval = IP_PMTUDISC_DONT; 204 int oval = IP_PMTUDISC_DONT;
178 setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); 205 setsockopt (icmpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval);
179 } 206 }
180#endif 207#endif
181 208
182 sockinfo si (THISNODE, PROT_ICMPv4); 209 sockinfo si (THISNODE, PROT_ICMPv4);
183 210
185 { 212 {
186 slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); 213 slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno));
187 exit (EXIT_FAILURE); 214 exit (EXIT_FAILURE);
188 } 215 }
189 216
190 icmpv4_ev_watcher.start (icmpv4_fd, EVENT_READ); 217 icmpv4_ev_watcher.start (icmpv4_fd, EV_READ);
191 } 218 }
192#endif 219#endif
193 220
194 tcpv4_fd = -1; 221 tcpv4_fd = -1;
195 222
200 227
201 if (tcpv4_fd < 0) 228 if (tcpv4_fd < 0)
202 return -1; 229 return -1;
203 230
204 fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); 231 fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK);
232 fcntl (tcpv4_fd, F_SETFD, FD_CLOEXEC);
205 233
206 // standard daemon practise... 234 // standard daemon practise...
207 { 235 {
208 int oval = 1; 236 int oval = 1;
209 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 237 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
221 { 249 {
222 slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno)); 250 slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno));
223 exit (EXIT_FAILURE); 251 exit (EXIT_FAILURE);
224 } 252 }
225 253
226 tcpv4_ev_watcher.start (tcpv4_fd, EVENT_READ); 254 tcpv4_ev_watcher.start (tcpv4_fd, EV_READ);
227 } 255 }
228#endif 256#endif
229 257
230#if ENABLE_DNS 258#if ENABLE_DNS
231 if (THISNODE->protocols & PROT_DNSv4) 259 if (THISNODE->protocols & PROT_DNSv4)
234 262
235 dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); 263 dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
236 264
237 if (dnsv4_fd < 0) 265 if (dnsv4_fd < 0)
238 return -1; 266 return -1;
267
268 fcntl (dnsv4_fd, F_SETFL, O_NONBLOCK);
269 fcntl (dnsv4_fd, F_SETFD, FD_CLOEXEC);
270
271# if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
272 // this I really consider a linux bug. I am neither connected
273 // nor do I fragment myself. Linux still sets DF and doesn't
274 // fragment for me sometimes.
275 {
276 int oval = IP_PMTUDISC_DONT;
277 setsockopt (dnsv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval);
278 }
279# endif
239 280
240 // standard daemon practise... 281 // standard daemon practise...
241 { 282 {
242 int oval = 1; 283 int oval = 1;
243 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 284 setsockopt (dnsv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
244 } 285 }
245 286
246 sockinfo si (THISNODE, PROT_DNSv4); 287 sockinfo si (THISNODE->dns_hostname,
288 THISNODE->dns_hostname ? THISNODE->dns_port : 0,
289 PROT_DNSv4);
247 290
248 if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ())) 291 if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ()))
249 { 292 {
250 slog (L_ERR, _("can't bind dnsv4 on %s: %s"), (const char *)si, strerror (errno)); 293 slog (L_ERR, _("can't bind dnsv4 on %s: %s"), (const char *)si, strerror (errno));
251 exit (EXIT_FAILURE); 294 exit (EXIT_FAILURE);
252 } 295 }
253 296
254 dnsv4_ev_watcher.start (dnsv4_fd, EVENT_READ); 297 dnsv4_ev_watcher.start (dnsv4_fd, EV_READ);
255 } 298 }
256#endif 299#endif
300
301 /////////////////////////////////////////////////////////////////////////////
302
303 reconnect_all ();
304
305 /////////////////////////////////////////////////////////////////////////////
257 306
258 tap = new tap_device (); 307 tap = new tap_device ();
259 if (!tap) //D this, of course, never catches 308 if (!tap) //D this, of course, never catches
260 { 309 {
261 slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); 310 slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname);
262 exit (EXIT_FAILURE); 311 exit (EXIT_FAILURE);
263 } 312 }
264 313
314 fcntl (tap->fd, F_SETFD, FD_CLOEXEC);
315
316 if (tap->if_up () &&
317 !run_script (run_script_cb (this, &vpn::script_if_init), true))
318 {
319 slog (L_ERR, _("interface initialization command '%s' failed, exiting."),
320 tap->if_up ());
321 exit (EXIT_FAILURE);
322 }
323
265 run_script (run_script_cb (this, &vpn::script_if_up), true); 324 if (!run_script (run_script_cb (this, &vpn::script_if_up), true))
325 {
326 slog (L_ERR, _("if-up command execution failed, exiting."));
327 exit (EXIT_FAILURE);
328 }
266 329
267 tap_ev_watcher.start (tap->fd, EVENT_READ); 330 tap_ev_watcher.start (tap->fd, EV_READ);
268
269 reconnect_all ();
270 331
271 return 0; 332 return 0;
272} 333}
273 334
274bool 335bool
395 else 456 else
396 c->recv_vpn_packet (pkt, rsi); 457 c->recv_vpn_packet (pkt, rsi);
397 } 458 }
398} 459}
399 460
461bool
462vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos)
463{
464 switch (si.prot)
465 {
466 case PROT_IPv4:
467 return send_ipv4_packet (pkt, si, tos);
468 case PROT_UDPv4:
469 return send_udpv4_packet (pkt, si, tos);
470#if ENABLE_TCP
471 case PROT_TCPv4:
472 return send_tcpv4_packet (pkt, si, tos);
473#endif
474#if ENABLE_ICMP
475 case PROT_ICMPv4:
476 return send_icmpv4_packet (pkt, si, tos);
477#endif
478#if ENABLE_DNS
479 case PROT_DNSv4:
480 return send_dnsv4_packet (pkt, si, tos);
481#endif
482
483 default:
484 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si);
485 }
486
487 return false;
488}
489
400void 490void
401vpn::ipv4_ev (io_watcher &w, short revents) 491vpn::ipv4_ev (ev::io &w, int revents)
402{ 492{
403 if (revents & EVENT_READ) 493 if (revents & EV_READ)
404 { 494 {
405 vpn_packet *pkt = new vpn_packet; 495 vpn_packet *pkt = new vpn_packet;
406 struct sockaddr_in sa; 496 struct sockaddr_in sa;
407 socklen_t sa_len = sizeof (sa); 497 socklen_t sa_len = sizeof (sa);
408 int len; 498 int len;
437 } 527 }
438} 528}
439 529
440#if ENABLE_ICMP 530#if ENABLE_ICMP
441void 531void
442vpn::icmpv4_ev (io_watcher &w, short revents) 532vpn::icmpv4_ev (ev::io &w, int revents)
443{ 533{
444 if (revents & EVENT_READ) 534 if (revents & EV_READ)
445 { 535 {
446 vpn_packet *pkt = new vpn_packet; 536 vpn_packet *pkt = new vpn_packet;
447 struct sockaddr_in sa; 537 struct sockaddr_in sa;
448 socklen_t sa_len = sizeof (sa); 538 socklen_t sa_len = sizeof (sa);
449 int len; 539 int len;
485 } 575 }
486} 576}
487#endif 577#endif
488 578
489void 579void
490vpn::udpv4_ev (io_watcher &w, short revents) 580vpn::udpv4_ev (ev::io &w, int revents)
491{ 581{
492 if (revents & EVENT_READ) 582 if (revents & EV_READ)
493 { 583 {
494 vpn_packet *pkt = new vpn_packet; 584 vpn_packet *pkt = new vpn_packet;
495 struct sockaddr_in sa; 585 struct sockaddr_in sa;
496 socklen_t sa_len = sizeof (sa); 586 socklen_t sa_len = sizeof (sa);
497 int len; 587 int len;
522 exit (EXIT_FAILURE); 612 exit (EXIT_FAILURE);
523 } 613 }
524} 614}
525 615
526void 616void
527vpn::tap_ev (io_watcher &w, short revents) 617vpn::tap_ev (ev::io &w, int revents)
528{ 618{
529 if (revents & EVENT_READ) 619 if (revents & EV_READ)
530 { 620 {
531 /* process data */ 621 /* process data */
532 tap_packet *pkt; 622 tap_packet *pkt;
533 623
534 pkt = tap->recv (); 624 pkt = tap->recv ();
564 else 654 else
565 abort (); 655 abort ();
566} 656}
567 657
568void 658void
569vpn::event_cb (time_watcher &w) 659vpn::event_cb (ev::timer &w, int)
570{ 660{
571 if (events) 661 if (events)
572 { 662 {
573 if (events & EVENT_SHUTDOWN) 663 if (events & EVENT_SHUTDOWN)
574 { 664 {
665} 755}
666 756
667void 757void
668vpn::dump_status () 758vpn::dump_status ()
669{ 759{
670 slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)NOW); 760 slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)ev::ev_now ());
671 761
672 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) 762 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
673 (*c)->dump_status (); 763 (*c)->dump_status ();
674 764
675 slog (L_NOTICE, _("END status dump")); 765 slog (L_NOTICE, _("END status dump"));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines