Merge branch 'master' into 1.1
[tinc] / 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 "utils.h"
25 #include "splay_tree.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 "net.h"
33 #include "netutl.h"
34 #include "process.h"
35 #include "protocol.h"
36 #include "subnet.h"
37 #include "xalloc.h"
38
39 /* Purge edges and subnets of unreachable nodes. Use carefully. */
40
41 void purge(void) {
42         splay_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
43         node_t *n;
44         edge_t *e;
45         subnet_t *s;
46
47         ifdebug(PROTOCOL) logger(LOG_DEBUG, "Purging unreachable nodes");
48
49         /* Remove all edges and subnets owned by unreachable nodes. */
50
51         for(nnode = node_tree->head; nnode; nnode = nnext) {
52                 nnext = nnode->next;
53                 n = nnode->data;
54
55                 if(!n->status.reachable) {
56                         ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Purging node %s (%s)", n->name,
57                                            n->hostname);
58
59                         for(snode = n->subnet_tree->head; snode; snode = snext) {
60                                 snext = snode->next;
61                                 s = snode->data;
62                                 send_del_subnet(broadcast, s);
63                                 if(!strictsubnets)
64                                         subnet_del(n, s);
65                         }
66
67                         for(enode = n->edge_tree->head; enode; enode = enext) {
68                                 enext = enode->next;
69                                 e = enode->data;
70                                 if(!tunnelserver)
71                                         send_del_edge(broadcast, e);
72                                 edge_del(e);
73                         }
74                 }
75         }
76
77         /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
78
79         for(nnode = node_tree->head; nnode; nnode = nnext) {
80                 nnext = nnode->next;
81                 n = nnode->data;
82
83                 if(!n->status.reachable) {
84                         for(enode = edge_weight_tree->head; enode; enode = enext) {
85                                 enext = enode->next;
86                                 e = enode->data;
87
88                                 if(e->to == n)
89                                         break;
90                         }
91
92                         if(!enode && (!strictsubnets || !n->subnet_tree->head))
93                                 /* in strictsubnets mode do not delete nodes with subnets */
94                                 node_del(n);
95                 }
96         }
97 }
98
99 /*
100   Terminate a connection:
101   - Close the socket
102   - Remove associated edge and tell other connections about it if report = true
103   - Check if we need to retry making an outgoing connection
104   - Deactivate the host
105 */
106 void terminate_connection(connection_t *c, bool report) {
107         ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Closing connection with %s (%s)",
108                            c->name, c->hostname);
109
110         c->status.active = false;
111
112         if(c->node)
113                 c->node->connection = NULL;
114
115         if(c->socket)
116                 closesocket(c->socket);
117
118         if(c->edge) {
119                 if(report && !tunnelserver)
120                         send_del_edge(broadcast, c->edge);
121
122                 edge_del(c->edge);
123
124                 /* Run MST and SSSP algorithms */
125
126                 graph();
127
128                 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
129
130                 if(report && !c->node->status.reachable) {
131                         edge_t *e;
132                         e = lookup_edge(c->node, myself);
133                         if(e) {
134                                 if(!tunnelserver)
135                                         send_del_edge(broadcast, e);
136                                 edge_del(e);
137                         }
138                 }
139         }
140
141         /* Check if this was our outgoing connection */
142
143         if(c->outgoing)
144                 retry_outgoing(c->outgoing);
145
146         connection_del(c);
147 }
148
149 /*
150   Check if the other end is active.
151   If we have sent packets, but didn't receive any,
152   then possibly the other end is dead. We send a
153   PING request over the meta connection. If the other
154   end does not reply in time, we consider them dead
155   and close the connection.
156 */
157 static void timeout_handler(int fd, short events, void *event) {
158         splay_node_t *node, *next;
159         connection_t *c;
160         time_t now = time(NULL);
161
162         for(node = connection_tree->head; node; node = next) {
163                 next = node->next;
164                 c = node->data;
165
166                 if(c->last_ping_time + pingtimeout < now) {
167                         if(c->status.active) {
168                                 if(c->status.pinged) {
169                                         ifdebug(CONNECTIONS) logger(LOG_INFO, "%s (%s) didn't respond to PING in %ld seconds",
170                                                            c->name, c->hostname, now - c->last_ping_time);
171                                         terminate_connection(c, true);
172                                         continue;
173                                 } else if(c->last_ping_time + pinginterval < now) {
174                                         send_ping(c);
175                                 }
176                         } else {
177                                 if(c->status.connecting) {
178                                         ifdebug(CONNECTIONS)
179                                                 logger(LOG_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname);
180                                         c->status.connecting = false;
181                                         closesocket(c->socket);
182                                         do_outgoing_connection(c);
183                                 } else {
184                                         ifdebug(CONNECTIONS) logger(LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
185                                         terminate_connection(c, false);
186                                         continue;
187                                 }
188                         }
189                 }
190         }
191
192         event_add(event, &(struct timeval){pingtimeout, 0});
193 }
194
195 void handle_meta_connection_data(int fd, short events, void *data) {
196         connection_t *c = data;
197         int result;
198         socklen_t len = sizeof result;
199
200         if(c->status.connecting) {
201                 c->status.connecting = false;
202
203                 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
204
205                 if(!result)
206                         finish_connecting(c);
207                 else {
208                         ifdebug(CONNECTIONS) logger(LOG_DEBUG,
209                                            "Error while connecting to %s (%s): %s",
210                                            c->name, c->hostname, sockstrerror(result));
211                         closesocket(c->socket);
212                         do_outgoing_connection(c);
213                         return;
214                 }
215         }
216
217         if (!receive_meta(c)) {
218                 terminate_connection(c, c->status.active);
219                 return;
220         }
221 }
222
223 static void sigterm_handler(int signal, short events, void *data) {
224         logger(LOG_NOTICE, "Got %s signal", strsignal(signal));
225         event_loopexit(NULL);
226 }
227
228 static void sighup_handler(int signal, short events, void *data) {
229         logger(LOG_NOTICE, "Got %s signal", strsignal(signal));
230         reload_configuration();
231 }
232
233 int reload_configuration(void) {
234         connection_t *c;
235         splay_node_t *node, *next;
236         char *fname;
237         struct stat s;
238         static time_t last_config_check = 0;
239
240         /* Reread our own configuration file */
241
242         exit_configuration(&config_tree);
243         init_configuration(&config_tree);
244
245         if(!read_server_config()) {
246                 logger(LOG_ERR, "Unable to reread configuration file, exitting.");
247                 event_loopexit(NULL);
248                 return EINVAL;
249         }
250
251         /* Close connections to hosts that have a changed or deleted host config file */
252         
253         for(node = connection_tree->head; node; node = next) {
254                 c = node->data;
255                 next = node->next;
256                 
257                 if(c->outgoing) {
258                         free(c->outgoing->name);
259                         if(c->outgoing->ai)
260                                 freeaddrinfo(c->outgoing->ai);
261                         free(c->outgoing);
262                         c->outgoing = NULL;
263                 }
264                 
265                 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
266                 if(stat(fname, &s) || s.st_mtime > last_config_check)
267                         terminate_connection(c, c->status.active);
268                 free(fname);
269         }
270
271         last_config_check = time(NULL);
272
273         /* If StrictSubnet is set, expire deleted Subnets and read new ones in */
274
275         if(strictsubnets) {
276                 subnet_t *subnet;
277
278                 for(node = subnet_tree->head; node; node = node->next) {
279                         subnet = node->data;
280                         subnet->expires = 1;
281                 }
282
283                 load_all_subnets();
284
285                 for(node = subnet_tree->head; node; node = next) {
286                         next = node->next;
287                         subnet = node->data;
288                         if(subnet->expires == 1) {
289                                 send_del_subnet(broadcast, subnet);
290                                 if(subnet->owner->status.reachable)
291                                         subnet_update(subnet->owner, subnet, false);
292                                 subnet_del(subnet->owner, subnet);
293                         } else if(subnet->expires == -1) {
294                                 subnet->expires = 0;
295                         } else {
296                                 send_add_subnet(broadcast, subnet);
297                                 if(subnet->owner->status.reachable)
298                                         subnet_update(subnet->owner, subnet, true);
299                         }
300                 }
301         }
302
303         /* Try to make outgoing connections */
304         
305         try_outgoing_connections();
306
307         return 0;
308 }
309
310 void retry(void) {
311         connection_t *c;
312         splay_node_t *node;
313
314         for(node = connection_tree->head; node; node = node->next) {
315                 c = node->data;
316                 
317                 if(c->outgoing && !c->node) {
318                         if(timeout_initialized(&c->outgoing->ev))
319                                 event_del(&c->outgoing->ev);
320                         if(c->status.connecting)
321                                 close(c->socket);
322                         c->outgoing->timeout = 0;
323                         do_outgoing_connection(c);
324                 }
325         }
326 }
327
328 /*
329   this is where it all happens...
330 */
331 int main_loop(void) {
332         struct event timeout_event;
333         struct event sighup_event;
334         struct event sigterm_event;
335         struct event sigquit_event;
336
337         timeout_set(&timeout_event, timeout_handler, &timeout_event);
338         event_add(&timeout_event, &(struct timeval){pingtimeout, 0});
339
340 #ifdef SIGHUP
341         signal_set(&sighup_event, SIGHUP, sighup_handler, NULL);
342         signal_add(&sighup_event, NULL);
343 #endif
344 #ifdef SIGTERM
345         signal_set(&sigterm_event, SIGTERM, sigterm_handler, NULL);
346         signal_add(&sigterm_event, NULL);
347 #endif
348 #ifdef SIGQUIT
349         signal_set(&sigquit_event, SIGQUIT, sigterm_handler, NULL);
350         signal_add(&sigquit_event, NULL);
351 #endif
352
353         if(event_loop(0) < 0) {
354                 logger(LOG_ERR, "Error while waiting for input: %s", strerror(errno));
355                 return 1;
356         }
357
358         signal_del(&sighup_event);
359         signal_del(&sigterm_event);
360         signal_del(&sigquit_event);
361         event_del(&timeout_event);
362
363         return 0;
364 }