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