ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vpn.C
Revision: 1.3
Committed: Wed Apr 2 21:02:25 2003 UTC (21 years, 1 month ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.2: +123 -90 lines
Log Message:
§§

File Contents

# Content
1 /*
2 vpn.C -- handle the protocol, encryption, handshaking etc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #include "config.h"
20
21 #include <list>
22
23 #include <cstdlib>
24 #include <cstring>
25 #include <cstdio>
26
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <sys/poll.h>
30 #include <sys/wait.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <errno.h>
34 #include <time.h>
35 #include <unistd.h>
36
37 #include "pidfile.h"
38
39 #include "connection.h"
40 #include "util.h"
41 #include "vpn.h"
42
43 #if ENABLE_TCP
44 # include <map>
45 # include <unistd.h>
46 # include <fcntl.h>
47 # include <sys/poll.h>
48 #endif
49
50 /////////////////////////////////////////////////////////////////////////////
51
52 const char *vpn::script_if_up ()
53 {
54 // the tunnel device mtu should be the physical mtu - overhead
55 // the tricky part is rounding to the cipher key blocksize
56 int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD;
57 mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion
58 mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round
59 mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again
60
61 char *env;
62 asprintf (&env, "CONFBASE=%s", confbase);
63 putenv (env);
64 asprintf (&env, "NODENAME=%s", THISNODE->nodename);
65 putenv (env);
66 asprintf (&env, "NODEID=%d", THISNODE->id);
67 putenv (env);
68 asprintf (&env, "IFNAME=%s", tap->interface ());
69 putenv (env);
70 asprintf (&env, "MTU=%d", mtu);
71 putenv (env);
72 asprintf (&env, "MAC=%02x:%02x:%02x:%02x:%02x:%02x",
73 0xfe, 0xfd, 0x80, 0x00, THISNODE->id >> 8,
74 THISNODE->id & 0xff);
75 putenv (env);
76
77 return ::conf.script_if_up ? ::conf.script_if_up : "if-up";
78 }
79
80 int
81 vpn::setup ()
82 {
83 udpv4_fd = -1;
84
85 if (THISNODE->protocols & PROT_UDPv4)
86 {
87 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
88
89 if (udpv4_fd < 0)
90 return -1;
91
92 // standard daemon practise...
93 {
94 int oval = 1;
95 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
96 }
97
98 sockinfo si (THISNODE, PROT_UDPv4);
99
100 if (bind (udpv4_fd, si.sav4 (), si.salenv4 ()))
101 {
102 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno));
103 exit (1);
104 }
105
106 #ifdef IP_MTU_DISCOVER
107 // this I really consider a linux bug. I am neither connected
108 // nor do I fragment myself. Linux still sets DF and doesn't
109 // fragment for me sometimes.
110 {
111 int oval = IP_PMTUDISC_DONT;
112 setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval);
113 }
114 #endif
115
116 udpv4_ev_watcher.start (udpv4_fd, POLLIN);
117 }
118
119 ipv4_fd = -1;
120 if (THISNODE->protocols & PROT_IPv4)
121 {
122 ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto);
123
124 if (ipv4_fd < 0)
125 return -1;
126
127 sockinfo si (THISNODE, PROT_IPv4);
128
129 if (bind (ipv4_fd, si.sav4 (), si.salenv4 ()))
130 {
131 slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno));
132 exit (1);
133 }
134
135 #ifdef IP_MTU_DISCOVER
136 // this I really consider a linux bug. I am neither connected
137 // nor do I fragment myself. Linux still sets DF and doesn't
138 // fragment for me sometimes.
139 {
140 int oval = IP_PMTUDISC_DONT;
141 setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval);
142 }
143 #endif
144
145 ipv4_ev_watcher.start (ipv4_fd, POLLIN);
146 }
147
148 #if ENABLE_TCP
149 if (THISNODE->protocols & PROT_TCPv4)
150 {
151 tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
152
153 if (tcpv4_fd < 0)
154 return -1;
155
156 // standard daemon practise...
157 {
158 int oval = 1;
159 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
160 }
161
162 sockinfo si (THISNODE, PROT_TCPv4);
163
164 if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ()))
165 {
166 slog (L_ERR, _("can't bind tcpv4 on %s: %s"), (const char *)si, strerror (errno));
167 exit (1);
168 }
169
170 if (listen (tcpv4_fd, 5))
171 {
172 slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno));
173 exit (1);
174 }
175
176 tcpv4_ev_watcher.start (tcpv4_fd, POLLIN);
177 }
178 #endif
179
180 tap = new tap_device ();
181 if (!tap) //D this, of course, never catches
182 {
183 slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname);
184 exit (1);
185 }
186
187 run_script (run_script_cb (this, &vpn::script_if_up), true);
188
189 tap_ev_watcher.start (tap->fd, POLLIN);
190
191 reconnect_all ();
192
193 return 0;
194 }
195
196 void
197 vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
198 {
199 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
200 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
201 }
202
203 void
204 vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
205 {
206 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
207 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
208 }
209
210 void
211 vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
212 {
213 unsigned int src = pkt->src ();
214 unsigned int dst = pkt->dst ();
215
216 slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d"),
217 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst (), pkt->len);
218
219 if (src == 0 || src > conns.size ()
220 || dst > conns.size ()
221 || pkt->typ () >= vpn_packet::PT_MAX)
222 slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"),
223 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ());
224 else
225 {
226 connection *c = conns[src - 1];
227
228 if (dst == 0 && !THISNODE->routerprio)
229 slog (L_WARN, _("%s(%s): received broadcast, but we are no router"),
230 c->conf->nodename, (const char *)rsi);
231 else if (dst != 0 && dst != THISNODE->id)
232 // FORWARDING NEEDED ;)
233 slog (L_WARN,
234 _("received frame for node %d ('%s') from %s, but this is node %d ('%s')"),
235 dst, conns[dst - 1]->conf->nodename,
236 (const char *)rsi,
237 THISNODE->id, THISNODE->nodename);
238 else
239 c->recv_vpn_packet (pkt, rsi);
240 }
241 }
242
243 void
244 vpn::udpv4_ev (io_watcher &w, short revents)
245 {
246 if (revents & (POLLIN | POLLERR))
247 {
248 vpn_packet *pkt = new vpn_packet;
249 struct sockaddr_in sa;
250 socklen_t sa_len = sizeof (sa);
251 int len;
252
253 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
254
255 sockinfo si(sa);
256
257 if (len > 0)
258 {
259 pkt->len = len;
260
261 recv_vpn_packet (pkt, si);
262 }
263 else
264 {
265 // probably ECONNRESET or somesuch
266 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno));
267 }
268
269 delete pkt;
270 }
271 else if (revents & POLLHUP)
272 {
273 // this cannot ;) happen on udp sockets
274 slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating."));
275 exit (1);
276 }
277 else
278 {
279 slog (L_ERR,
280 _("FATAL: unknown revents %08x in socket, terminating\n"),
281 revents);
282 exit (1);
283 }
284 }
285
286 void
287 vpn::ipv4_ev (io_watcher &w, short revents)
288 {
289 if (revents & (POLLIN | POLLERR))
290 {
291 vpn_packet *pkt = new vpn_packet;
292 struct sockaddr_in sa;
293 socklen_t sa_len = sizeof (sa);
294 int len;
295
296 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
297
298 sockinfo si(sa, PROT_IPv4);
299
300 if (len > 0)
301 {
302 pkt->len = len;
303
304 // raw sockets deliver the ipv4, but don't expect it on sends
305 // this is slow, but...
306 pkt->skip_hdr (IP_OVERHEAD);
307
308 recv_vpn_packet (pkt, si);
309 }
310 else
311 {
312 // probably ECONNRESET or somesuch
313 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno));
314 }
315
316 delete pkt;
317 }
318 else if (revents & POLLHUP)
319 {
320 // this cannot ;) happen on udp sockets
321 slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating."));
322 exit (1);
323 }
324 else
325 {
326 slog (L_ERR,
327 _("FATAL: unknown revents %08x in socket, terminating\n"),
328 revents);
329 exit (1);
330 }
331 }
332
333 #if ENABLE_TCP
334
335 struct tcp_connection;
336
337 struct lt_sockinfo
338 {
339 bool operator()(const sockinfo *a, const sockinfo *b) const
340 {
341 return *a < *b;
342 }
343 };
344
345 struct tcp_si_map : public map<const sockinfo *, tcp_connection *, lt_sockinfo> {
346 void cleaner_cb (time_watcher &w); time_watcher cleaner;
347
348 tcp_si_map ()
349 : cleaner(this, &tcp_si_map::cleaner_cb)
350 {
351 cleaner.start (0);
352 }
353 } tcp_si;
354
355 struct tcp_connection : io_watcher {
356 tstamp last_activity;
357 const sockinfo si;
358 vpn &v;
359 bool ok;
360
361 void tcpv4_ev (io_watcher &w, short revents);
362
363 operator tcp_si_map::value_type()
364 {
365 return tcp_si_map::value_type (&si, this);
366 }
367
368 tcp_connection (int fd_, const sockinfo &si_, vpn &v_)
369 : v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev), ok(false)
370 {
371 last_activity = NOW;
372 start (fd_, POLLOUT);
373 }
374
375 ~tcp_connection () { close (fd); }
376 };
377
378 void tcp_si_map::cleaner_cb (time_watcher &w)
379 {
380 w.at = NOW + 600;
381 tstamp to = NOW - ::conf.keepalive - 30;
382
383 for (iterator i = begin (); i != end(); )
384 if (i->second->last_activity >= to)
385 ++i;
386 else
387 {
388 erase (i);
389 i = begin ();
390 }
391 }
392
393 void
394 vpn::send_tcpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
395 {
396 tcp_si_map::iterator info = tcp_si.find (&si);
397
398 if (info == tcp_si.end ())
399 {
400 // woaw, the first lost packet ;)
401 int fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
402
403 if (fd >= 0)
404 {
405 fcntl (fd, F_SETFL, O_NONBLOCK);
406
407 if (connect (fd, si.sav4 (), si.salenv4 ()) >= 0
408 || errno == EINPROGRESS)
409 {
410 tcp_connection *i = new tcp_connection (fd, si, *this);
411
412 tcp_si.insert (*i);
413 }
414 else
415 close (fd);
416 }
417 }
418 else
419 {
420 tcp_connection *i = info->second;
421
422 i->last_activity = NOW;
423
424 if (i->ok)
425 {
426 setsockopt (i->fd, SOL_IP, IP_TOS, &tos, sizeof tos);
427
428 // we use none of the advantages of tcp
429 write (i->fd, (void *)pkt, pkt->len + sizeof (u32)) != pkt->len + sizeof (u32);
430 }
431 }
432
433 #if 0
434 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
435 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
436 #endif
437 }
438
439 void
440 tcp_connection::tcpv4_ev (io_watcher &w, short revents)
441 {
442 last_activity = NOW;
443
444 if (!ok) // just established?
445 {
446 ok = true;
447 fcntl (fd, F_SETFL, 0);
448 stop ();
449 start (fd, POLLIN);
450 }
451
452 if (revents & (POLLIN | POLLERR))
453 {
454 u32 len;
455
456 if (sizeof (len) == read (fd, &len, sizeof (len)))
457 {
458 vpn_packet *pkt = new vpn_packet;
459
460 if (len == read (fd, &((*pkt)[0]), len))
461 {
462 pkt->len = len;
463
464 v.recv_vpn_packet (pkt, si);
465 return;
466 }
467
468 delete pkt;
469 }
470
471 tcp_si.erase (&si);
472 stop ();
473 }
474 }
475
476 void
477 vpn::tcpv4_ev (io_watcher &w, short revents)
478 {
479 if (revents & (POLLIN | POLLERR))
480 {
481 struct sockaddr_in sa;
482 socklen_t sa_len = sizeof (sa);
483 int len;
484
485 int fd = accept (w.fd, (sockaddr *)&sa, &sa_len);
486
487 if (fd >= 0)
488 {
489 fcntl (fd, F_SETFL, O_NONBLOCK);
490
491 sockinfo si(sa, PROT_TCPv4);
492 tcp_connection *i = new tcp_connection (fd, si, *this);
493
494 slog (L_ERR, "accepted %d\n", fd);//D
495
496 tcp_si.insert (*i);
497 }
498 }
499 }
500
501 #endif
502
503 void
504 vpn::tap_ev (io_watcher &w, short revents)
505 {
506 if (revents & POLLIN)
507 {
508 /* process data */
509 tap_packet *pkt;
510
511 pkt = tap->recv ();
512
513 int dst = mac2id (pkt->dst);
514 int src = mac2id (pkt->src);
515
516 if (src != THISNODE->id)
517 {
518 slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating."));
519 exit (1);
520 }
521
522 if (dst == THISNODE->id)
523 {
524 slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating."));
525 exit (1);
526 }
527
528 if (dst > conns.size ())
529 slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst);
530 else
531 {
532 if (dst)
533 {
534 // unicast
535 if (dst != THISNODE->id)
536 conns[dst - 1]->inject_data_packet (pkt);
537 }
538 else
539 {
540 // broadcast, first check router, then self, then english
541 connection *router = find_router ();
542
543 if (router)
544 router->inject_data_packet (pkt, true);
545 else
546 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
547 if ((*c)->conf != THISNODE)
548 (*c)->inject_data_packet (pkt);
549 }
550 }
551
552 delete pkt;
553 }
554 else if (revents & (POLLHUP | POLLERR))
555 {
556 slog (L_ERR, _("FATAL: POLLHUP or POLLERR on network device fd, terminating."));
557 exit (1);
558 }
559 else
560 abort ();
561 }
562
563 void
564 vpn::event_cb (time_watcher &w)
565 {
566 if (events)
567 {
568 if (events & EVENT_SHUTDOWN)
569 {
570 slog (L_INFO, _("preparing shutdown..."));
571
572 shutdown_all ();
573
574 remove_pid (pidfilename);
575
576 slog (L_INFO, _("terminating"));
577
578 exit (0);
579 }
580
581 if (events & EVENT_RECONNECT)
582 {
583 slog (L_INFO, _("forced reconnect"));
584
585 reconnect_all ();
586 }
587
588 events = 0;
589 }
590
591 w.at = TSTAMP_CANCEL;
592 }
593
594 void
595 vpn::shutdown_all ()
596 {
597 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
598 (*c)->shutdown ();
599 }
600
601 void
602 vpn::reconnect_all ()
603 {
604 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
605 delete *c;
606
607 conns.clear ();
608
609 connection_init ();
610
611 for (configuration::node_vector::iterator i = conf.nodes.begin ();
612 i != conf.nodes.end (); ++i)
613 {
614 connection *conn = new connection (this);
615
616 conn->conf = *i;
617 conns.push_back (conn);
618
619 conn->establish_connection ();
620 }
621 }
622
623 connection *vpn::find_router ()
624 {
625 u32 prio = 0;
626 connection *router = 0;
627
628 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
629 {
630 connection *c = *i;
631
632 if (c->conf->routerprio > prio
633 && c->connectmode == conf_node::C_ALWAYS
634 && c->conf != THISNODE
635 && c->ictx && c->octx)
636 {
637 prio = c->conf->routerprio;
638 router = c;
639 }
640 }
641
642 return router;
643 }
644
645 void vpn::connect_request (int id)
646 {
647 connection *c = find_router ();
648
649 if (c)
650 c->connect_request (id);
651 //else // does not work, because all others must connect to the same router
652 // // no router found, aggressively connect to all routers
653 // for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
654 // if ((*i)->conf->routerprio)
655 // (*i)->establish_connection ();
656 }
657
658 void
659 connection::dump_status ()
660 {
661 slog (L_NOTICE, _("node %s (id %d)"), conf->nodename, conf->id);
662 slog (L_NOTICE, _(" connectmode %d (%d) / sockaddr %s / minor %d"),
663 connectmode, conf->connectmode, (const char *)si, (int)prot_minor);
664 slog (L_NOTICE, _(" ictx/octx %08lx/%08lx / oseqno %d / retry_cnt %d"),
665 (long)ictx, (long)octx, (int)oseqno, (int)retry_cnt);
666 slog (L_NOTICE, _(" establish_conn %ld / rekey %ld / keepalive %ld"),
667 (long)(establish_connection.at), (long)(rekey.at), (long)(keepalive.at));
668 }
669
670 void
671 vpn::dump_status ()
672 {
673 slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)NOW);
674
675 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
676 (*c)->dump_status ();
677
678 slog (L_NOTICE, _("END status dump"));
679 }
680
681 vpn::vpn (void)
682 : event(this, &vpn::event_cb)
683 , udpv4_ev_watcher(this, &vpn::udpv4_ev)
684 , ipv4_ev_watcher (this, &vpn::ipv4_ev)
685 , tap_ev_watcher (this, &vpn::tap_ev)
686 #if ENABLE_TCP
687 , tcpv4_ev_watcher(this, &vpn::tcpv4_ev)
688 #endif
689 {
690 }
691
692 vpn::~vpn ()
693 {
694 }
695