Use read_host_config() where appropriate.
[tinc] / src / net.c
1 /*
2     net.c -- most of the network code
3     Copyright (C) 1998-2005 Ivo Timmermans,
4                   2000-2013 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 "utils.h"
26 #include "conf.h"
27 #include "connection.h"
28 #include "device.h"
29 #include "graph.h"
30 #include "logger.h"
31 #include "meta.h"
32 #include "names.h"
33 #include "net.h"
34 #include "netutl.h"
35 #include "process.h"
36 #include "protocol.h"
37 #include "subnet.h"
38 #include "xalloc.h"
39
40 int contradicting_add_edge = 0;
41 int contradicting_del_edge = 0;
42 static int sleeptime = 10;
43 time_t last_config_check = 0;
44
45 /* Purge edges and subnets of unreachable nodes. Use carefully. */
46
47 void purge(void) {
48         logger(DEBUG_PROTOCOL, LOG_DEBUG, "Purging unreachable nodes");
49
50         /* Remove all edges and subnets owned by unreachable nodes. */
51
52         for splay_each(node_t, n, node_tree) {
53                 if(!n->status.reachable) {
54                         logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Purging node %s (%s)", n->name, n->hostname);
55
56                         for splay_each(subnet_t, s, n->subnet_tree) {
57                                 send_del_subnet(everyone, s);
58                                 if(!strictsubnets)
59                                         subnet_del(n, s);
60                         }
61
62                         for splay_each(edge_t, e, n->edge_tree) {
63                                 if(!tunnelserver)
64                                         send_del_edge(everyone, e);
65                                 edge_del(e);
66                         }
67                 }
68         }
69
70         /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
71
72         for splay_each(node_t, n, node_tree) {
73                 if(!n->status.reachable) {
74                         for splay_each(edge_t, e, edge_weight_tree)
75                                 if(e->to == n)
76                                         return;
77
78                         if(!autoconnect && (!strictsubnets || !n->subnet_tree->head))
79                                 /* in strictsubnets mode do not delete nodes with subnets */
80                                 node_del(n);
81                 }
82         }
83 }
84
85 /*
86   Terminate a connection:
87   - Mark it as inactive
88   - Remove the edge representing this connection
89   - Kill it with fire
90   - Check if we need to retry making an outgoing connection
91 */
92 void terminate_connection(connection_t *c, bool report) {
93         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Closing connection with %s (%s)", c->name, c->hostname);
94
95         c->status.active = false;
96
97         if(c->node && c->node->connection == c)
98                 c->node->connection = NULL;
99
100         if(c->edge) {
101                 if(report && !tunnelserver)
102                         send_del_edge(everyone, c->edge);
103
104                 edge_del(c->edge);
105                 c->edge = NULL;
106
107                 /* Run MST and SSSP algorithms */
108
109                 graph();
110
111                 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
112
113                 if(report && !c->node->status.reachable) {
114                         edge_t *e;
115                         e = lookup_edge(c->node, myself);
116                         if(e) {
117                                 if(!tunnelserver)
118                                         send_del_edge(everyone, e);
119                                 edge_del(e);
120                         }
121                 }
122         }
123
124         outgoing_t *outgoing = c->outgoing;
125         connection_del(c);
126
127         /* Check if this was our outgoing connection */
128
129         if(outgoing)
130                 do_outgoing_connection(outgoing);
131 }
132
133 /*
134   Check if the other end is active.
135   If we have sent packets, but didn't receive any,
136   then possibly the other end is dead. We send a
137   PING request over the meta connection. If the other
138   end does not reply in time, we consider them dead
139   and close the connection.
140 */
141 static void timeout_handler(void *data) {
142         for list_each(connection_t, c, connection_list) {
143                 if(c->status.control)
144                         continue;
145
146                 if(c->last_ping_time + pingtimeout <= now.tv_sec) {
147                         if(c->status.active) {
148                                 if(c->status.pinged) {
149                                         logger(DEBUG_CONNECTIONS, LOG_INFO, "%s (%s) didn't respond to PING in %ld seconds", c->name, c->hostname, (long)now.tv_sec - c->last_ping_time);
150                                 } else if(c->last_ping_time + pinginterval <= now.tv_sec) {
151                                         send_ping(c);
152                                         continue;
153                                 } else {
154                                         continue;
155                                 }
156                         } else {
157                                 if(c->status.connecting)
158                                         logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname);
159                                 else
160                                         logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
161                         }
162                         terminate_connection(c, c->status.active);
163                 }
164         }
165
166         timeout_set(data, &(struct timeval){pingtimeout, rand() % 100000});
167 }
168
169 static void periodic_handler(void *data) {
170         /* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages.
171            This usually only happens when another node has the same Name as this node.
172            If so, sleep for a short while to prevent a storm of contradicting messages.
173         */
174
175         if(contradicting_del_edge > 100 && contradicting_add_edge > 100) {
176                 logger(DEBUG_ALWAYS, LOG_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", sleeptime);
177                 usleep(sleeptime * 1000000LL);
178                 sleeptime *= 2;
179                 if(sleeptime < 0)
180                         sleeptime = 3600;
181         } else {
182                 sleeptime /= 2;
183                 if(sleeptime < 10)
184                         sleeptime = 10;
185         }
186
187         contradicting_add_edge = 0;
188         contradicting_del_edge = 0;
189
190         /* If AutoConnect is set, check if we need to make or break connections. */
191
192         if(autoconnect && node_tree->count > 1) {
193                 /* Count number of active connections */
194                 int nc = 0;
195                 for list_each(connection_t, c, connection_list) {
196                         if(c->status.active && !c->status.control)
197                                 nc++;
198                 }
199
200                 if(nc < autoconnect) {
201                         /* Not enough active connections, try to add one.
202                            Choose a random node, if we don't have a connection to it,
203                            and we are not already trying to make one, create an
204                            outgoing connection to this node.
205                         */
206                         int r = rand() % node_tree->count;
207                         int i = 0;
208
209                         for splay_each(node_t, n, node_tree) {
210                                 if(i++ != r)
211                                         continue;
212
213                                 if(n->connection)
214                                         break;
215
216                                 bool found = false;
217
218                                 for list_each(outgoing_t, outgoing, outgoing_list) {
219                                         if(!strcmp(outgoing->name, n->name)) {
220                                                 found = true;
221                                                 break;
222                                         }
223                                 }
224
225                                 if(!found) {
226                                         logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name);
227                                         outgoing_t *outgoing = xzalloc(sizeof *outgoing);
228                                         outgoing->name = xstrdup(n->name);
229                                         list_insert_tail(outgoing_list, outgoing);
230                                         setup_outgoing_connection(outgoing);
231                                 }
232                                 break;
233                         }
234                 } else if(nc > autoconnect) {
235                         /* Too many active connections, try to remove one.
236                            Choose a random outgoing connection to a node
237                            that has at least one other connection.
238                         */
239                         int r = rand() % nc;
240                         int i = 0;
241
242                         for list_each(connection_t, c, connection_list) {
243                                 if(!c->status.active || c->status.control)
244                                         continue;
245
246                                 if(i++ != r)
247                                         continue;
248
249                                 if(!c->outgoing || !c->node || c->node->edge_tree->count < 2)
250                                         break;
251
252                                 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autodisconnecting from %s", c->name);
253                                 list_delete(outgoing_list, c->outgoing);
254                                 c->outgoing = NULL;
255                                 terminate_connection(c, c->status.active);
256                                 break;
257                         }
258                 }
259
260                 if(nc >= autoconnect) {
261                         /* If we have enough active connections,
262                            remove any pending outgoing connections.
263                         */
264                         for list_each(outgoing_t, o, outgoing_list) {
265                                 bool found = false;
266                                 for list_each(connection_t, c, connection_list) {
267                                         if(c->outgoing == o) {
268                                                 found = true;
269                                                 break;
270                                         }
271                                 }
272                                 if(!found) {
273                                         logger(DEBUG_CONNECTIONS, LOG_INFO, "Cancelled outgoing connection to %s", o->name);
274                                         list_delete_node(outgoing_list, node);
275                                 }
276                         }
277                 }
278         }
279
280         timeout_set(data, &(struct timeval){5, rand() % 100000});
281 }
282
283 void handle_meta_connection_data(connection_t *c) {
284         if (!receive_meta(c)) {
285                 terminate_connection(c, c->status.active);
286                 return;
287         }
288 }
289
290 #ifndef HAVE_MINGW
291 static void sigterm_handler(void *data) {
292         logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
293         event_exit();
294 }
295
296 static void sighup_handler(void *data) {
297         logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
298         reopenlogger();
299         reload_configuration();
300 }
301
302 static void sigalrm_handler(void *data) {
303         logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
304         retry();
305 }
306 #endif
307
308 int reload_configuration(void) {
309         char *fname = NULL;
310
311         /* Reread our own configuration file */
312
313         exit_configuration(&config_tree);
314         init_configuration(&config_tree);
315
316         if(!read_server_config()) {
317                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reread configuration file, exitting.");
318                 event_exit();
319                 return EINVAL;
320         }
321
322         read_config_options(config_tree, NULL);
323
324         xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, myself->name);
325         read_config_file(config_tree, fname);
326         free(fname);
327
328         /* Parse some options that are allowed to be changed while tinc is running */
329
330         setup_myself_reloadable();
331
332         /* If StrictSubnet is set, expire deleted Subnets and read new ones in */
333
334         if(strictsubnets) {
335                 for splay_each(subnet_t, subnet, subnet_tree)
336                         subnet->expires = 1;
337
338                 load_all_subnets();
339
340                 for splay_each(subnet_t, subnet, subnet_tree) {
341                         if(subnet->expires == 1) {
342                                 send_del_subnet(everyone, subnet);
343                                 if(subnet->owner->status.reachable)
344                                         subnet_update(subnet->owner, subnet, false);
345                                 subnet_del(subnet->owner, subnet);
346                         } else if(subnet->expires == -1) {
347                                 subnet->expires = 0;
348                         } else {
349                                 send_add_subnet(everyone, subnet);
350                                 if(subnet->owner->status.reachable)
351                                         subnet_update(subnet->owner, subnet, true);
352                         }
353                 }
354         } else { /* Only read our own subnets back in */
355                 for splay_each(subnet_t, subnet, myself->subnet_tree)
356                         if(!subnet->expires)
357                                 subnet->expires = 1;
358
359                 config_t *cfg = lookup_config(config_tree, "Subnet");
360
361                 while(cfg) {
362                         subnet_t *subnet, *s2;
363
364                         if(!get_config_subnet(cfg, &subnet))
365                                 continue;
366
367                         if((s2 = lookup_subnet(myself, subnet))) {
368                                 if(s2->expires == 1)
369                                         s2->expires = 0;
370
371                                 free_subnet(subnet);
372                         } else {
373                                 subnet_add(myself, subnet);
374                                 send_add_subnet(everyone, subnet);
375                                 subnet_update(myself, subnet, true);
376                         }
377
378                         cfg = lookup_config_next(config_tree, cfg);
379                 }
380
381                 for splay_each(subnet_t, subnet, myself->subnet_tree) {
382                         if(subnet->expires == 1) {
383                                 send_del_subnet(everyone, subnet);
384                                 subnet_update(myself, subnet, false);
385                                 subnet_del(myself, subnet);
386                         }
387                 }
388         }
389
390         /* Try to make outgoing connections */
391
392         try_outgoing_connections();
393
394         /* Close connections to hosts that have a changed or deleted host config file */
395
396         for list_each(connection_t, c, connection_list) {
397                 if(c->status.control)
398                         continue;
399
400                 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
401                 struct stat s;
402                 if(stat(fname, &s) || s.st_mtime > last_config_check) {
403                         logger(DEBUG_CONNECTIONS, LOG_INFO, "Host config file of %s has been changed", c->name);
404                         terminate_connection(c, c->status.active);
405                 }
406                 free(fname);
407         }
408
409         last_config_check = now.tv_sec;
410
411         return 0;
412 }
413
414 void retry(void) {
415         for list_each(connection_t, c, connection_list) {
416                 if(c->outgoing && !c->node) {
417                         timeout_del(&c->outgoing->ev);
418                         if(c->status.connecting)
419                                 close(c->socket);
420                         c->outgoing->timeout = 0;
421                         terminate_connection(c, c->status.active);
422                 }
423         }
424 }
425
426 /*
427   this is where it all happens...
428 */
429 int main_loop(void) {
430         timeout_t pingtimer = {{0}};
431         timeout_t periodictimer = {{0}};
432
433         timeout_add(&pingtimer, timeout_handler, &pingtimer, &(struct timeval){pingtimeout, rand() % 100000});
434         timeout_add(&periodictimer, periodic_handler, &periodictimer, &(struct timeval){pingtimeout, rand() % 100000});
435
436 #ifndef HAVE_MINGW
437         signal_t sighup = {0};
438         signal_t sigterm = {0};
439         signal_t sigquit = {0};
440         signal_t sigint = {0};
441         signal_t sigalrm = {0};
442
443         signal_add(&sighup, sighup_handler, &sighup, SIGHUP);
444         signal_add(&sigterm, sigterm_handler, &sigterm, SIGTERM);
445         signal_add(&sigquit, sigterm_handler, &sigquit, SIGQUIT);
446         signal_add(&sigint, sigterm_handler, &sigint, SIGINT);
447         signal_add(&sigalrm, sigalrm_handler, &sigalrm, SIGALRM);
448 #endif
449
450         if(!event_loop()) {
451                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while waiting for input: %s", strerror(errno));
452                 return 1;
453         }
454
455 #ifndef HAVE_MINGW
456         signal_del(&sighup);
457         signal_del(&sigalrm);
458         signal_del(&sigquit);
459         signal_del(&sigterm);
460 #endif
461
462         timeout_del(&periodictimer);
463         timeout_del(&pingtimer);
464
465         return 0;
466 }