392aa7acbe79084e32210c4e37485906b00854f2
[tinc] / src / net.c
1 /*
2     net.c -- most of the network code
3     Copyright (C) 1998-2005 Ivo Timmermans,
4                   2000-2012 Guus Sliepen <guus@tinc-vpn.org>
5                   2006      Scott Lamb <slamb@slamb.org>
6                   2011      Loïc Grenié <loic.grenie@gmail.com>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License along
19     with this program; if not, write to the Free Software Foundation, Inc.,
20     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "system.h"
24
25 #include <openssl/rand.h>
26
27 #include "utils.h"
28 #include "avl_tree.h"
29 #include "conf.h"
30 #include "connection.h"
31 #include "device.h"
32 #include "event.h"
33 #include "graph.h"
34 #include "logger.h"
35 #include "meta.h"
36 #include "net.h"
37 #include "netutl.h"
38 #include "process.h"
39 #include "protocol.h"
40 #include "route.h"
41 #include "subnet.h"
42 #include "xalloc.h"
43 #include "resolv.h"
44
45 bool do_purge = false;
46 volatile bool running = false;
47 #ifdef HAVE_PSELECT
48 bool graph_dump = false;
49 #endif
50
51 time_t now = 0;
52 int contradicting_add_edge = 0;
53 int contradicting_del_edge = 0;
54 static int sleeptime = 10;
55
56 /* Purge edges and subnets of unreachable nodes. Use carefully. */
57
58 static void purge(void) {
59         avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
60         node_t *n;
61         edge_t *e;
62         subnet_t *s;
63
64         ifdebug(PROTOCOL) logger(LOG_DEBUG, "Purging unreachable nodes");
65
66         /* Remove all edges and subnets owned by unreachable nodes. */
67
68         for(nnode = node_tree->head; nnode; nnode = nnext) {
69                 nnext = nnode->next;
70                 n = nnode->data;
71
72                 if(!n->status.reachable) {
73                         ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Purging node %s (%s)", n->name,
74                                            n->hostname);
75
76                         for(snode = n->subnet_tree->head; snode; snode = snext) {
77                                 snext = snode->next;
78                                 s = snode->data;
79                                 send_del_subnet(everyone, s);
80                                 if(!strictsubnets)
81                                         subnet_del(n, s);
82                         }
83
84                         for(enode = n->edge_tree->head; enode; enode = enext) {
85                                 enext = enode->next;
86                                 e = enode->data;
87                                 if(!tunnelserver)
88                                         send_del_edge(everyone, e);
89                                 edge_del(e);
90                         }
91                 }
92         }
93
94         /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
95
96         for(nnode = node_tree->head; nnode; nnode = nnext) {
97                 nnext = nnode->next;
98                 n = nnode->data;
99
100                 if(!n->status.reachable) {
101                         for(enode = edge_weight_tree->head; enode; enode = enext) {
102                                 enext = enode->next;
103                                 e = enode->data;
104
105                                 if(e->to == n)
106                                         break;
107                         }
108
109                         if(!enode && (!strictsubnets || !n->subnet_tree->head))
110                                 /* in strictsubnets mode do not delete nodes with subnets */
111                                 node_del(n);
112                 }
113         }
114 }
115
116 /*
117   put all file descriptors in an fd_set array
118   While we're at it, purge stuff that needs to be removed.
119 */
120 static int build_fdset(fd_set *readset, fd_set *writeset) {
121         avl_node_t *node, *next;
122         connection_t *c;
123         int i, max = 0;
124
125         FD_ZERO(readset);
126         FD_ZERO(writeset);
127
128         for(node = connection_tree->head; node; node = next) {
129                 next = node->next;
130                 c = node->data;
131
132                 if(c->status.remove) {
133                         connection_del(c);
134                         if(!connection_tree->head)
135                                 purge();
136                 } else {
137                         FD_SET(c->socket, readset);
138                         if(c->outbuflen > 0 || c->status.connecting)
139                                 FD_SET(c->socket, writeset);
140                         if(c->socket > max)
141                                 max = c->socket;
142                 }
143         }
144
145         for(i = 0; i < listen_sockets; i++) {
146                 FD_SET(listen_socket[i].tcp, readset);
147                 if(listen_socket[i].tcp > max)
148                         max = listen_socket[i].tcp;
149                 FD_SET(listen_socket[i].udp, readset);
150                 if(listen_socket[i].udp > max)
151                         max = listen_socket[i].udp;
152         }
153
154         if(device_fd >= 0)
155                 FD_SET(device_fd, readset);
156         if(device_fd > max)
157                 max = device_fd;
158         
159         return max;
160 }
161
162 /*
163   Terminate a connection:
164   - Close the socket
165   - Remove associated edge and tell other connections about it if report = true
166   - Check if we need to retry making an outgoing connection
167   - Deactivate the host
168 */
169 void terminate_connection(connection_t *c, bool report) {
170         if(c->status.remove)
171                 return;
172
173         ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Closing connection with %s (%s)",
174                            c->name, c->hostname);
175
176         c->status.remove = true;
177         c->status.active = false;
178
179         if(c->node)
180                 c->node->connection = NULL;
181
182         if(c->socket)
183                 closesocket(c->socket);
184
185         if(c->edge) {
186                 if(report && !tunnelserver)
187                         send_del_edge(everyone, c->edge);
188
189                 edge_del(c->edge);
190
191                 /* Run MST and SSSP algorithms */
192
193                 graph();
194
195                 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
196
197                 if(report && !c->node->status.reachable) {
198                         edge_t *e;
199                         e = lookup_edge(c->node, myself);
200                         if(e) {
201                                 if(!tunnelserver)
202                                         send_del_edge(everyone, e);
203                                 edge_del(e);
204                         }
205                 }
206         }
207
208         free_connection_partially(c);
209
210         /* Check if this was our outgoing connection */
211
212         if(c->outgoing) {
213                 c->status.remove = false;
214                 do_outgoing_connection(c);      
215         }
216
217 #ifndef HAVE_MINGW
218         /* Clean up dead proxy processes */
219
220         while(waitpid(-1, NULL, WNOHANG) > 0);
221 #endif
222 }
223
224 /*
225   Check if the other end is active.
226   If we have sent packets, but didn't receive any,
227   then possibly the other end is dead. We send a
228   PING request over the meta connection. If the other
229   end does not reply in time, we consider them dead
230   and close the connection.
231 */
232 static void check_dead_connections(void) {
233         avl_node_t *node, *next;
234         connection_t *c;
235
236         for(node = connection_tree->head; node; node = next) {
237                 next = node->next;
238                 c = node->data;
239
240                 if(c->last_ping_time + pingtimeout <= now) {
241                         if(c->status.active) {
242                                 if(c->status.pinged) {
243                                         ifdebug(CONNECTIONS) logger(LOG_INFO, "%s (%s) didn't respond to PING in %ld seconds",
244                                                            c->name, c->hostname, (long)now - c->last_ping_time);
245                                         c->status.timeout = true;
246                                         terminate_connection(c, true);
247                                 } else if(c->last_ping_time + pinginterval <= now) {
248                                         send_ping(c);
249                                 }
250                         } else {
251                                 if(c->status.remove) {
252                                         logger(LOG_WARNING, "Old connection_t for %s (%s) status %04x still lingering, deleting...",
253                                                    c->name, c->hostname, bitfield_to_int(&c->status, sizeof c->status));
254                                         connection_del(c);
255                                         continue;
256                                 }
257                                 ifdebug(CONNECTIONS) logger(LOG_WARNING, "Timeout from %s (%s) during authentication",
258                                                    c->name, c->hostname);
259                                 if(c->status.connecting) {
260                                         c->status.connecting = false;
261                                         closesocket(c->socket);
262                                         do_outgoing_connection(c);
263                                 } else {
264                                         terminate_connection(c, false);
265                                 }
266                         }
267                 }
268
269                 if(c->outbuflen > 0 && c->last_flushed_time + pingtimeout <= now) {
270                         if(c->status.active) {
271                                 ifdebug(CONNECTIONS) logger(LOG_INFO,
272                                                 "%s (%s) could not flush for %ld seconds (%d bytes remaining)",
273                                                 c->name, c->hostname, (long)now - c->last_flushed_time, c->outbuflen);
274                                 c->status.timeout = true;
275                                 terminate_connection(c, true);
276                         }
277                 }
278         }
279 }
280
281 /*
282   check all connections to see if anything
283   happened on their sockets
284 */
285 static void check_network_activity(fd_set * readset, fd_set * writeset) {
286         connection_t *c;
287         avl_node_t *node;
288         int result, i;
289         socklen_t len = sizeof(result);
290         vpn_packet_t packet;
291         static int errors = 0;
292
293         /* check input from kernel */
294         if(device_fd >= 0 && FD_ISSET(device_fd, readset)) {
295                 if(devops.read(&packet)) {
296                         if(packet.len) {
297                                 errors = 0;
298                                 packet.priority = 0;
299                                 route(myself, &packet);
300                         }
301                 } else {
302                         usleep(errors * 50000);
303                         errors++;
304                         if(errors > 10) {
305                                 logger(LOG_ERR, "Too many errors from %s, exiting!", device);
306                                 running = false;
307                         }
308                 }
309         }
310
311         /* check meta connections */
312         for(node = connection_tree->head; node; node = node->next) {
313                 c = node->data;
314
315                 if(c->status.remove)
316                         continue;
317
318                 if(FD_ISSET(c->socket, writeset)) {
319                         if(c->status.connecting) {
320                                 c->status.connecting = false;
321                                 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, (void *)&result, &len);
322
323                                 if(!result)
324                                         finish_connecting(c);
325                                 else {
326                                         ifdebug(CONNECTIONS) logger(LOG_DEBUG,
327                                                            "Error while connecting to %s (%s): %s",
328                                                            c->name, c->hostname, sockstrerror(result));
329                                         closesocket(c->socket);
330                                         do_outgoing_connection(c);
331                                         continue;
332                                 }
333                         }
334
335                         if(!flush_meta(c)) {
336                                 terminate_connection(c, c->status.active);
337                                 continue;
338                         }
339                 }
340
341                 if(FD_ISSET(c->socket, readset)) {
342                         if(!receive_meta(c)) {
343                                 terminate_connection(c, c->status.active);
344                                 continue;
345                         }
346                 }
347         }
348
349         for(i = 0; i < listen_sockets; i++) {
350                 if(FD_ISSET(listen_socket[i].udp, readset))
351                         handle_incoming_vpn_data(i);
352
353                 if(FD_ISSET(listen_socket[i].tcp, readset))
354                         handle_new_meta_connection(listen_socket[i].tcp);
355         }
356 }
357
358 /*
359   this is where it all happens...
360 */
361 int main_loop(void) {
362         fd_set readset, writeset;
363 #ifdef HAVE_PSELECT
364         struct timespec tv;
365         sigset_t omask, block_mask;
366         time_t next_event;
367 #else
368         struct timeval tv;
369 #endif
370         int r, maxfd;
371         time_t last_ping_check, last_config_check, last_graph_dump;
372         event_t *event;
373
374         last_ping_check = now;
375         last_config_check = now;
376         last_graph_dump = now;
377         
378         srand(now);
379
380 #ifdef HAVE_PSELECT
381         if(lookup_config(config_tree, "GraphDumpFile"))
382                 graph_dump = true;
383         /* Block SIGHUP & SIGALRM */
384         sigemptyset(&block_mask);
385         sigaddset(&block_mask, SIGHUP);
386         sigaddset(&block_mask, SIGALRM);
387         sigprocmask(SIG_BLOCK, &block_mask, &omask);
388 #endif
389
390         running = true;
391
392         while(running) {
393 #ifdef HAVE_PSELECT
394                 next_event = last_ping_check + pingtimeout;
395                 if(graph_dump && next_event > last_graph_dump + 60)
396                         next_event = last_graph_dump + 60;
397
398                 if((event = peek_next_event()) && next_event > event->time)
399                         next_event = event->time;
400
401                 if(next_event <= now)
402                         tv.tv_sec = 0;
403                 else
404                         tv.tv_sec = next_event - now;
405                 tv.tv_nsec = 0;
406 #else
407                 tv.tv_sec = 1;
408                 tv.tv_usec = 0;
409 #endif
410
411                 maxfd = build_fdset(&readset, &writeset);
412
413 #ifdef HAVE_MINGW
414                 LeaveCriticalSection(&mutex);
415 #endif
416 #ifdef HAVE_PSELECT
417                 r = pselect(maxfd + 1, &readset, &writeset, NULL, &tv, &omask);
418 #else
419                 r = select(maxfd + 1, &readset, &writeset, NULL, &tv);
420 #endif
421                 now = time(NULL);
422 #ifdef HAVE_MINGW
423                 EnterCriticalSection(&mutex);
424 #endif
425
426                 if(r < 0) {
427                         if(!sockwouldblock(sockerrno)) {
428                                 logger(LOG_ERR, "Error while waiting for input: %s", sockstrerror(sockerrno));
429                                 dump_connections();
430                                 return 1;
431                         }
432                 }
433
434                 if(r > 0)
435                         check_network_activity(&readset, &writeset);
436
437                 if(do_purge) {
438                         purge();
439                         do_purge = false;
440                 }
441
442                 /* Let's check if everybody is still alive */
443
444                 if(last_ping_check + pingtimeout <= now) {
445                         check_dead_connections();
446                         last_ping_check = now;
447
448                         if(routing_mode == RMODE_SWITCH)
449                                 age_subnets();
450
451                         age_past_requests();
452
453                         /* Should we regenerate our key? */
454
455                         if(keyexpires <= now) {
456                                 avl_node_t *node;
457                                 node_t *n;
458
459                                 ifdebug(STATUS) logger(LOG_INFO, "Expiring symmetric keys");
460
461                                 for(node = node_tree->head; node; node = node->next) {
462                                         n = node->data;
463                                         if(n->inkey) {
464                                                 free(n->inkey);
465                                                 n->inkey = NULL;
466                                         }
467                                 }
468
469                                 send_key_changed();
470                                 keyexpires = now + keylifetime;
471                         }
472
473                         /* Detect ADD_EDGE/DEL_EDGE storms that are caused when
474                          * two tinc daemons with the same name are on the VPN.
475                          * If so, sleep a while. If this happens multiple times
476                          * in a row, sleep longer. */
477
478                         if(contradicting_del_edge > 100 && contradicting_add_edge > 100) {
479                                 logger(LOG_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", sleeptime);
480                                 usleep(sleeptime * 1000000LL);
481                                 sleeptime *= 2;
482                                 if(sleeptime < 0)
483                                         sleeptime = 3600;
484                         } else {
485                                 sleeptime /= 2;
486                                 if(sleeptime < 10)
487                                         sleeptime = 10;
488                         }
489
490                         contradicting_add_edge = 0;
491                         contradicting_del_edge = 0;
492                 }
493
494                 if(sigalrm) {
495                         avl_node_t *node;
496                         logger(LOG_INFO, "Flushing event queue");
497                         expire_events();
498                         res_init();
499                         for(node = connection_tree->head; node; node = node->next) {
500                                 connection_t *c = node->data;
501                                 if(c->status.active)
502                                         send_ping(c);
503                         }
504                         sigalrm = false;
505                 }
506
507                 while((event = get_expired_event())) {
508                         event->handler(event->data);
509                         free_event(event);
510                 }
511
512                 if(sighup) {
513                         connection_t *c;
514                         avl_node_t *node, *next;
515                         char *fname;
516                         struct stat s;
517                         
518                         sighup = false;
519
520                         reopenlogger();
521                         
522                         /* Reread our own configuration file */
523
524                         exit_configuration(&config_tree);
525                         init_configuration(&config_tree);
526
527                         if(!read_server_config()) {
528                                 logger(LOG_ERR, "Unable to reread configuration file, exitting.");
529                                 return 1;
530                         }
531
532                         /* Cancel non-active outgoing connections */
533
534                         for(node = connection_tree->head; node; node = next) {
535                                 next = node->next;
536                                 c = node->data;
537
538                                 c->outgoing = NULL;
539
540                                 if(c->status.connecting) {
541                                         terminate_connection(c, false);
542                                         connection_del(c);
543                                 }
544                         }
545
546                         /* Wipe list of outgoing connections */
547
548                         for(list_node_t *node = outgoing_list->head; node; node = node->next) {
549                                 outgoing_t *outgoing = node->data;
550
551                                 if(outgoing->event)
552                                         event_del(outgoing->event);
553                         }
554
555                         list_delete_list(outgoing_list);
556
557                         /* Close connections to hosts that have a changed or deleted host config file */
558                         
559                         for(node = connection_tree->head; node; node = node->next) {
560                                 c = node->data;
561                                 
562                                 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
563                                 if(stat(fname, &s) || s.st_mtime > last_config_check)
564                                         terminate_connection(c, c->status.active);
565                                 free(fname);
566                         }
567
568                         last_config_check = now;
569
570                         /* If StrictSubnet is set, expire deleted Subnets and read new ones in */
571
572                         if(strictsubnets) {
573                                 subnet_t *subnet;
574
575                                 for(node = subnet_tree->head; node; node = node->next) {
576                                         subnet = node->data;
577                                         subnet->expires = 1;
578                                 }
579
580                                 load_all_subnets();
581
582                                 for(node = subnet_tree->head; node; node = next) {
583                                         next = node->next;
584                                         subnet = node->data;
585                                         if(subnet->expires == 1) {
586                                                 send_del_subnet(everyone, subnet);
587                                                 if(subnet->owner->status.reachable)
588                                                         subnet_update(subnet->owner, subnet, false);
589                                                 subnet_del(subnet->owner, subnet);
590                                         } else if(subnet->expires == -1) {
591                                                 subnet->expires = 0;
592                                         } else {
593                                                 send_add_subnet(everyone, subnet);
594                                                 if(subnet->owner->status.reachable)
595                                                         subnet_update(subnet->owner, subnet, true);
596                                         }
597                                 }
598                         }
599
600                         /* Try to make outgoing connections */
601                         
602                         try_outgoing_connections();
603                 }
604                 
605                 /* Dump graph if wanted every 60 seconds*/
606
607                 if(last_graph_dump + 60 <= now) {
608                         dump_graph();
609                         last_graph_dump = now;
610                 }
611         }
612
613 #ifdef HAVE_PSELECT
614         /* Restore SIGHUP & SIGALARM mask */
615         sigprocmask(SIG_SETMASK, &omask, NULL);
616 #endif
617
618         return 0;
619 }