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