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