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