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