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.31 by pcg, Sat Mar 5 19:13:16 2005 UTC vs.
Revision 1.40 by pcg, Sat Dec 1 23:35:31 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 (dnsv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 284 setsockopt (dnsv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
251 { 292 {
252 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));
253 exit (EXIT_FAILURE); 294 exit (EXIT_FAILURE);
254 } 295 }
255 296
256 dnsv4_ev_watcher.start (dnsv4_fd, EVENT_READ); 297 dnsv4_ev_watcher.start (dnsv4_fd, EV_READ);
257 } 298 }
258#endif 299#endif
300
301 /////////////////////////////////////////////////////////////////////////////
302
303 reconnect_all ();
304
305 /////////////////////////////////////////////////////////////////////////////
259 306
260 tap = new tap_device (); 307 tap = new tap_device ();
261 if (!tap) //D this, of course, never catches 308 if (!tap) //D this, of course, never catches
262 { 309 {
263 slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); 310 slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname);
264 exit (EXIT_FAILURE); 311 exit (EXIT_FAILURE);
265 } 312 }
266 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
267 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 }
268 329
269 tap_ev_watcher.start (tap->fd, EVENT_READ); 330 tap_ev_watcher.start (tap->fd, EV_READ);
270
271 reconnect_all ();
272 331
273 return 0; 332 return 0;
274} 333}
275 334
276bool 335bool
427 486
428 return false; 487 return false;
429} 488}
430 489
431void 490void
432vpn::ipv4_ev (io_watcher &w, short revents) 491vpn::ipv4_ev (ev::io &w, int revents)
433{ 492{
434 if (revents & EVENT_READ) 493 if (revents & EV_READ)
435 { 494 {
436 vpn_packet *pkt = new vpn_packet; 495 vpn_packet *pkt = new vpn_packet;
437 struct sockaddr_in sa; 496 struct sockaddr_in sa;
438 socklen_t sa_len = sizeof (sa); 497 socklen_t sa_len = sizeof (sa);
439 int len; 498 int len;
468 } 527 }
469} 528}
470 529
471#if ENABLE_ICMP 530#if ENABLE_ICMP
472void 531void
473vpn::icmpv4_ev (io_watcher &w, short revents) 532vpn::icmpv4_ev (ev::io &w, int revents)
474{ 533{
475 if (revents & EVENT_READ) 534 if (revents & EV_READ)
476 { 535 {
477 vpn_packet *pkt = new vpn_packet; 536 vpn_packet *pkt = new vpn_packet;
478 struct sockaddr_in sa; 537 struct sockaddr_in sa;
479 socklen_t sa_len = sizeof (sa); 538 socklen_t sa_len = sizeof (sa);
480 int len; 539 int len;
516 } 575 }
517} 576}
518#endif 577#endif
519 578
520void 579void
521vpn::udpv4_ev (io_watcher &w, short revents) 580vpn::udpv4_ev (ev::io &w, int revents)
522{ 581{
523 if (revents & EVENT_READ) 582 if (revents & EV_READ)
524 { 583 {
525 vpn_packet *pkt = new vpn_packet; 584 vpn_packet *pkt = new vpn_packet;
526 struct sockaddr_in sa; 585 struct sockaddr_in sa;
527 socklen_t sa_len = sizeof (sa); 586 socklen_t sa_len = sizeof (sa);
528 int len; 587 int len;
553 exit (EXIT_FAILURE); 612 exit (EXIT_FAILURE);
554 } 613 }
555} 614}
556 615
557void 616void
558vpn::tap_ev (io_watcher &w, short revents) 617vpn::tap_ev (ev::io &w, int revents)
559{ 618{
560 if (revents & EVENT_READ) 619 if (revents & EV_READ)
561 { 620 {
562 /* process data */ 621 /* process data */
563 tap_packet *pkt; 622 tap_packet *pkt;
564 623
565 pkt = tap->recv (); 624 pkt = tap->recv ();
595 else 654 else
596 abort (); 655 abort ();
597} 656}
598 657
599void 658void
600vpn::event_cb (time_watcher &w) 659vpn::event_cb (ev::timer &w, int)
601{ 660{
602 if (events) 661 if (events)
603 { 662 {
604 if (events & EVENT_SHUTDOWN) 663 if (events & EVENT_SHUTDOWN)
605 { 664 {
696} 755}
697 756
698void 757void
699vpn::dump_status () 758vpn::dump_status ()
700{ 759{
701 slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)NOW); 760 slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)ev_now ());
702 761
703 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) 762 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
704 (*c)->dump_status (); 763 (*c)->dump_status ();
705 764
706 slog (L_NOTICE, _("END status dump")); 765 slog (L_NOTICE, _("END status dump"));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines