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