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