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