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