5638e64dd7a8b3f18255cd04883939c7184eb4e6
[tinc] / src / net.c
1 /*
2     net.c -- most of the network code
3     Copyright (C) 1998-2005 Ivo Timmermans,
4                   2000-2006 Guus Sliepen <guus@tinc-vpn.org>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id$
21 */
22
23 #include "system.h"
24
25 #include <openssl/rand.h>
26
27 #include "utils.h"
28 #include "avl_tree.h"
29 #include "conf.h"
30 #include "connection.h"
31 #include "device.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 "subnet.h"
40 #include "xalloc.h"
41
42 /* Purge edges and subnets of unreachable nodes. Use carefully. */
43
44 static void purge(void)
45 {
46         avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
47         node_t *n;
48         edge_t *e;
49         subnet_t *s;
50
51         cp();
52
53         ifdebug(PROTOCOL) logger(LOG_DEBUG, _("Purging unreachable nodes"));
54
55         /* Remove all edges and subnets owned by unreachable nodes. */
56
57         for(nnode = node_tree->head; nnode; nnode = nnext) {
58                 nnext = nnode->next;
59                 n = nnode->data;
60
61                 if(!n->status.reachable) {
62                         ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
63                                            n->hostname);
64
65                         for(snode = n->subnet_tree->head; snode; snode = snext) {
66                                 snext = snode->next;
67                                 s = snode->data;
68                                 if(!tunnelserver)
69                                         send_del_subnet(broadcast, s);
70                                 subnet_del(n, s);
71                         }
72
73                         for(enode = n->edge_tree->head; enode; enode = enext) {
74                                 enext = enode->next;
75                                 e = enode->data;
76                                 if(!tunnelserver)
77                                         send_del_edge(broadcast, e);
78                                 edge_del(e);
79                         }
80                 }
81         }
82
83         /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
84
85         for(nnode = node_tree->head; nnode; nnode = nnext) {
86                 nnext = nnode->next;
87                 n = nnode->data;
88
89                 if(!n->status.reachable) {
90                         for(enode = edge_weight_tree->head; enode; enode = enext) {
91                                 enext = enode->next;
92                                 e = enode->data;
93
94                                 if(e->to == n)
95                                         break;
96                         }
97
98                         if(!enode)
99                                 node_del(n);
100                 }
101         }
102 }
103
104 /*
105   put all file descriptors into events
106   While we're at it, purge stuf that needs to be removed.
107 */
108 static int build_fdset(void)
109 {
110         avl_node_t *node, *next;
111         connection_t *c;
112         int i, max = 0;
113
114         cp();
115
116         for(node = connection_tree->head; node; node = next) {
117                 next = node->next;
118                 c = node->data;
119
120                 if(c->status.remove) {
121                         connection_del(c);
122                         if(!connection_tree->head)
123                                 purge();
124                 }
125         }
126
127         return 0;
128 }
129
130 /*
131   Terminate a connection:
132   - Close the socket
133   - Remove associated edge and tell other connections about it if report = true
134   - Check if we need to retry making an outgoing connection
135   - Deactivate the host
136 */
137 void terminate_connection(connection_t *c, bool report)
138 {
139         cp();
140
141         if(c->status.remove)
142                 return;
143
144         ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
145                            c->name, c->hostname);
146
147         c->status.remove = true;
148         c->status.active = false;
149
150         if(c->node)
151                 c->node->connection = NULL;
152
153         if(c->socket)
154                 closesocket(c->socket);
155
156         event_del(&c->ev);
157
158         if(c->edge) {
159                 if(report && !tunnelserver)
160                         send_del_edge(broadcast, c->edge);
161
162                 edge_del(c->edge);
163
164                 /* Run MST and SSSP algorithms */
165
166                 graph();
167
168                 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
169
170                 if(report && !c->node->status.reachable) {
171                         edge_t *e;
172                         e = lookup_edge(c->node, myself);
173                         if(e) {
174                                 if(!tunnelserver)
175                                         send_del_edge(broadcast, e);
176                                 edge_del(e);
177                         }
178                 }
179         }
180
181         /* Check if this was our outgoing connection */
182
183         if(c->outgoing) {
184                 retry_outgoing(c->outgoing);
185                 c->outgoing = NULL;
186         }
187
188         free(c->outbuf);
189         c->outbuf = NULL;
190         c->outbuflen = 0;
191         c->outbufsize = 0;
192         c->outbufstart = 0;
193 }
194
195 /*
196   Check if the other end is active.
197   If we have sent packets, but didn't receive any,
198   then possibly the other end is dead. We send a
199   PING request over the meta connection. If the other
200   end does not reply in time, we consider them dead
201   and close the connection.
202 */
203 static void timeout_handler(int fd, short events, void *event)
204 {
205         avl_node_t *node, *next;
206         connection_t *c;
207         time_t now = time(NULL);
208
209         cp();
210
211         for(node = connection_tree->head; node; node = next) {
212                 next = node->next;
213                 c = node->data;
214
215                 if(c->last_ping_time + pingtimeout < now) {
216                         if(c->status.active) {
217                                 if(c->status.pinged) {
218                                         ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING in %ld seconds"),
219                                                            c->name, c->hostname, now - c->last_ping_time);
220                                         c->status.timeout = true;
221                                         terminate_connection(c, true);
222                                 } else if(c->last_ping_time + pinginterval < now) {
223                                         send_ping(c);
224                                 }
225                         } else {
226                                 if(c->status.remove) {
227                                         logger(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
228                                                    c->name, c->hostname, c->status.value);
229                                         connection_del(c);
230                                         continue;
231                                 }
232                                 ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
233                                                    c->name, c->hostname);
234                                 if(c->status.connecting) {
235                                         c->status.connecting = false;
236                                         closesocket(c->socket);
237                                         do_outgoing_connection(c);
238                                 } else {
239                                         terminate_connection(c, false);
240                                 }
241                         }
242                 }
243         }
244
245         event_add(event, &(struct timeval){pingtimeout, 0});
246 }
247
248 void handle_meta_connection_data(int fd, short events, void *data)
249 {
250         connection_t *c = data;
251         int result;
252         socklen_t len = sizeof(result);
253
254         if (c->status.remove)
255                 return;
256
257         if(c->status.connecting) {
258                 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
259
260                 if(!result)
261                         finish_connecting(c);
262                 else {
263                         ifdebug(CONNECTIONS) logger(LOG_DEBUG,
264                                            _("Error while connecting to %s (%s): %s"),
265                                            c->name, c->hostname, strerror(result));
266                         c->status.connecting = false;
267                         closesocket(c->socket);
268                         do_outgoing_connection(c);
269                         return;
270                 }
271         }
272
273         if (!receive_meta(c)) {
274                 terminate_connection(c, c->status.active);
275                 return;
276         }
277 }
278
279 static void sigterm_handler(int signal, short events, void *data) {
280         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
281         event_loopexit(NULL);
282 }
283
284 static void sigint_handler(int signal, short events, void *data) {
285         static int saved_debug_level = -1;
286
287         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
288
289         if(saved_debug_level != -1) {
290                 logger(LOG_NOTICE, _("Reverting to old debug level (%d)"),
291                         saved_debug_level);
292                 debug_level = saved_debug_level;
293                 saved_debug_level = -1;
294         } else {
295                 logger(LOG_NOTICE,
296                         _("Temporarily setting debug level to 5.  Kill me with SIGINT again to go back to level %d."),
297                         debug_level);
298                 saved_debug_level = debug_level;
299                 debug_level = 5;
300         }
301 }
302
303 static void sigusr1_handler(int signal, short events, void *data) {
304         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
305         dump_connections();
306 }
307
308 static void sigusr2_handler(int signal, short events, void *data) {
309         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
310         dump_device_stats();
311         dump_nodes();
312         dump_edges();
313         dump_subnets();
314 }
315
316 static void sigwinch_handler(int signal, short events, void *data) {
317         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
318         purge();
319 }
320
321 static void sighup_handler(int signal, short events, void *data) {
322         connection_t *c;
323         avl_node_t *node;
324         char *fname;
325         struct stat s;
326         static time_t last_config_check = 0;
327         
328         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
329
330         /* Reread our own configuration file */
331
332         exit_configuration(&config_tree);
333         init_configuration(&config_tree);
334
335         if(!read_server_config()) {
336                 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
337                 event_loopexit(NULL);
338                 return;
339         }
340
341         /* Close connections to hosts that have a changed or deleted host config file */
342         
343         for(node = connection_tree->head; node; node = node->next) {
344                 c = node->data;
345                 
346                 if(c->outgoing) {
347                         free(c->outgoing->name);
348                         if(c->outgoing->ai)
349                                 freeaddrinfo(c->outgoing->ai);
350                         free(c->outgoing);
351                         c->outgoing = NULL;
352                 }
353                 
354                 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
355                 if(stat(fname, &s) || s.st_mtime > last_config_check)
356                         terminate_connection(c, c->status.active);
357                 free(fname);
358         }
359
360         last_config_check = time(NULL);
361
362         /* Try to make outgoing connections */
363         
364         try_outgoing_connections();
365 }
366
367 static void sigalrm_handler(int signal, short events, void *data) {
368         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
369
370         connection_t *c;
371         avl_node_t *node;
372
373         for(node = connection_tree->head; node; node = node->next) {
374                 c = node->data;
375                 
376                 if(c->outgoing && !c->node) {
377                         if(timeout_initialized(&c->outgoing->ev))
378                                 event_del(&c->outgoing->ev);
379                         if(c->status.connecting)
380                                 close(c->socket);
381                         c->outgoing->timeout = 0;
382                         do_outgoing_connection(c);
383                 }
384         }
385 }
386
387 /*
388   this is where it all happens...
389 */
390 int main_loop(void)
391 {
392         struct timeval tv;
393         int r;
394         struct event timeout_event;
395         struct event sighup_event;
396         struct event sigint_event;
397         struct event sigterm_event;
398         struct event sigquit_event;
399         struct event sigusr1_event;
400         struct event sigusr2_event;
401         struct event sigwinch_event;
402         struct event sigalrm_event;
403
404         cp();
405
406         timeout_set(&timeout_event, timeout_handler, &timeout_event);
407         event_add(&timeout_event, &(struct timeval){pingtimeout, 0});
408         signal_set(&sighup_event, SIGHUP, sighup_handler, NULL);
409         signal_add(&sighup_event, NULL);
410         signal_set(&sigint_event, SIGINT, sigint_handler, NULL);
411         signal_add(&sigint_event, NULL);
412         signal_set(&sigterm_event, SIGTERM, sigterm_handler, NULL);
413         signal_add(&sigterm_event, NULL);
414         signal_set(&sigquit_event, SIGQUIT, sigterm_handler, NULL);
415         signal_add(&sigquit_event, NULL);
416         signal_set(&sigusr1_event, SIGUSR1, sigusr1_handler, NULL);
417         signal_add(&sigusr1_event, NULL);
418         signal_set(&sigusr2_event, SIGUSR2, sigusr2_handler, NULL);
419         signal_add(&sigusr2_event, NULL);
420         signal_set(&sigwinch_event, SIGWINCH, sigwinch_handler, NULL);
421         signal_add(&sigwinch_event, NULL);
422         signal_set(&sigalrm_event, SIGALRM, sigalrm_handler, NULL);
423         signal_add(&sigalrm_event, NULL);
424
425         if(event_loop(0) < 0) {
426                 logger(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
427                 return 1;
428         }
429
430         signal_del(&sighup_event);
431         signal_del(&sigint_event);
432         signal_del(&sigterm_event);
433         signal_del(&sigquit_event);
434         signal_del(&sigusr1_event);
435         signal_del(&sigusr2_event);
436         signal_del(&sigwinch_event);
437         signal_del(&sigalrm_event);
438         event_del(&timeout_event);
439
440         return 0;
441 }