Only purge once when there are no more connections.
[oweals/tinc.git] / src / net.c
1 /*
2     net.c -- most of the network code
3     Copyright (C) 1998-2002 Ivo Timmermans <itimmermans@bigfoot.com>,
4                   2000-2002 Guus Sliepen <guus@sliepen.warande.net>
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: net.c,v 1.35.4.171 2002/05/01 09:15:58 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <netdb.h>
28 #include <netinet/in.h>
29 #ifdef HAVE_LINUX
30  #include <netinet/ip.h>
31  #include <netinet/tcp.h>
32 #endif
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <signal.h>
37 #include <sys/time.h>
38 #include <sys/types.h>
39 #include <syslog.h>
40 #include <unistd.h>
41 #include <sys/ioctl.h>
42 /* SunOS really wants sys/socket.h BEFORE net/if.h,
43    and FreeBSD wants these lines below the rest. */
44 #include <arpa/inet.h>
45 #include <sys/socket.h>
46 #include <net/if.h>
47
48 #include <openssl/rand.h>
49
50 #include <utils.h>
51 #include <xalloc.h>
52 #include <avl_tree.h>
53 #include <list.h>
54
55 #include "conf.h"
56 #include "connection.h"
57 #include "meta.h"
58 #include "net.h"
59 #include "netutl.h"
60 #include "process.h"
61 #include "protocol.h"
62 #include "subnet.h"
63 #include "graph.h"
64 #include "process.h"
65 #include "route.h"
66 #include "device.h"
67 #include "event.h"
68
69 #include "system.h"
70
71 #ifndef HAVE_RAND_PSEUDO_BYTES
72 #define RAND_pseudo_bytes RAND_bytes
73 #endif
74
75 int do_purge = 0;
76 int sighup = 0;
77 int sigalrm = 0;
78
79 time_t now = 0;
80
81 /* Purge edges and subnets of unreachable nodes. Use carefully. */
82
83 void purge(void)
84 {
85   avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext, *cnode;
86   node_t *n;
87   edge_t *e;
88   subnet_t *s;
89   connection_t *c;
90 cp
91   if(debug_lvl >= DEBUG_PROTOCOL)
92     syslog(LOG_DEBUG, _("Purging unreachable nodes"));
93
94   for(nnode = node_tree->head; nnode; nnode = nnext)
95   {
96     nnext = nnode->next;
97     n = (node_t *)nnode->data;
98
99     if(!n->status.reachable)
100     {
101       if(debug_lvl >= DEBUG_SCARY_THINGS)
102         syslog(LOG_DEBUG, _("Purging node %s (%s)"), n->name, n->hostname);
103
104       for(snode = n->subnet_tree->head; snode; snode = snext)
105       {
106         snext = snode->next;
107         s = (subnet_t *)snode->data;
108
109         for(cnode = connection_tree->head; cnode; cnode = cnode->next)
110         {
111           c = (connection_t *)cnode->data;
112           if(c->status.active)
113             send_del_subnet(c, s);
114         }
115
116         subnet_del(n, s);
117       }
118
119       for(enode = n->edge_tree->head; enode; enode = enext)
120       {
121         enext = enode->next;
122         e = (edge_t *)enode->data;
123
124         for(cnode = connection_tree->head; cnode; cnode = cnode->next)
125         {
126           c = (connection_t *)cnode->data;
127           if(c->status.active)
128             send_del_edge(c, e);
129         }
130
131         edge_del(e);
132       }
133
134       node_del(n);
135     }
136   }
137 cp
138 }
139
140 /*
141   put all file descriptors in an fd_set array
142   While we're at it, purge stuff that needs to be removed.
143 */
144 void build_fdset(fd_set *fs)
145 {
146   avl_node_t *node, *next;
147   connection_t *c;
148   int i;
149 cp
150   FD_ZERO(fs);
151
152   for(node = connection_tree->head; node; node = next)
153     {
154       next = node->next;
155       c = (connection_t *)node->data;
156
157       if(c->status.remove)
158         {
159           connection_del(c);
160           if(!connection_tree->head)
161             purge();
162         }
163       else
164         FD_SET(c->socket, fs);
165     }
166
167   for(i = 0; i < listen_sockets; i++)
168     {
169       FD_SET(listen_socket[i].tcp, fs);
170       FD_SET(listen_socket[i].udp, fs);
171     }
172
173   FD_SET(device_fd, fs);
174 cp
175 }
176
177 /*
178   Terminate a connection:
179   - Close the socket
180   - Remove associated edge and tell other connections about it if report = 1
181   - Check if we need to retry making an outgoing connection
182   - Deactivate the host
183 */
184 void terminate_connection(connection_t *c, int report)
185 {
186   avl_node_t *node;
187   connection_t *other;
188 cp
189   if(c->status.remove)
190     return;
191
192   if(debug_lvl >= DEBUG_CONNECTIONS)
193     syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
194            c->name, c->hostname);
195
196   c->status.remove = 1;
197   c->status.active = 0;
198
199   if(c->node)
200     c->node->connection = NULL;
201
202   if(c->socket)
203     close(c->socket);
204
205   if(c->edge)
206     {
207       if(report)
208         {
209           for(node = connection_tree->head; node; node = node->next)
210             {
211               other = (connection_t *)node->data;
212               if(other->status.active && other != c)
213                 send_del_edge(other, c->edge);
214             }
215         }
216
217       edge_del(c->edge);
218
219       /* Run MST and SSSP algorithms */
220
221       graph();
222     }
223
224   /* Check if this was our outgoing connection */
225
226   if(c->outgoing)
227     {
228       retry_outgoing(c->outgoing);
229       c->outgoing = NULL;
230     }
231 cp
232 }
233
234 /*
235   Check if the other end is active.
236   If we have sent packets, but didn't receive any,
237   then possibly the other end is dead. We send a
238   PING request over the meta connection. If the other
239   end does not reply in time, we consider them dead
240   and close the connection.
241 */
242 void check_dead_connections(void)
243 {
244   avl_node_t *node, *next;
245   connection_t *c;
246 cp
247   for(node = connection_tree->head; node; node = next)
248     {
249       next = node->next;
250       c = (connection_t *)node->data;
251       if(c->last_ping_time + pingtimeout < now)
252         {
253           if(c->status.active)
254             {
255               if(c->status.pinged)
256                 {
257                   if(debug_lvl >= DEBUG_PROTOCOL)
258                     syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
259                            c->name, c->hostname);
260                   c->status.timeout = 1;
261                   terminate_connection(c, 1);
262                 }
263               else
264                 {
265                   send_ping(c);
266                 }
267             }
268           else
269             {
270               if(debug_lvl >= DEBUG_CONNECTIONS)
271                 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
272                        c->name, c->hostname);
273               terminate_connection(c, 0);
274             }
275         }
276     }
277 cp
278 }
279
280 /*
281   check all connections to see if anything
282   happened on their sockets
283 */
284 void check_network_activity(fd_set *f)
285 {
286   connection_t *c;
287   avl_node_t *node;
288   int result, i;
289   int len = sizeof(result);
290   vpn_packet_t packet;
291 cp
292   if(FD_ISSET(device_fd, f))
293     {
294       if(!read_packet(&packet))
295         route_outgoing(&packet);
296     }
297
298   for(node = connection_tree->head; node; node = node->next)
299     {
300       c = (connection_t *)node->data;
301
302       if(c->status.remove)
303         continue;
304
305       if(FD_ISSET(c->socket, f))
306         {
307           if(c->status.connecting)
308             {
309               c->status.connecting = 0;
310               getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
311               if(!result)
312                 finish_connecting(c);
313               else
314                 {
315                   if(debug_lvl >= DEBUG_CONNECTIONS)
316                     syslog(LOG_DEBUG, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(result));
317                   close(c->socket);
318                   do_outgoing_connection(c);
319                   continue;
320                 }
321             }
322           if(receive_meta(c) < 0)
323             {
324               terminate_connection(c, c->status.active);
325               continue;
326             }
327         }
328     }
329
330   for(i = 0; i < listen_sockets; i++)
331     {
332       if(FD_ISSET(listen_socket[i].udp, f))
333         handle_incoming_vpn_data(listen_socket[i].udp);
334       if(FD_ISSET(listen_socket[i].tcp, f))
335         handle_new_meta_connection(listen_socket[i].tcp);
336     }
337 cp
338 }
339
340 /*
341   this is where it all happens...
342 */
343 void main_loop(void)
344 {
345   fd_set fset;
346   struct timeval tv;
347   int r;
348   time_t last_ping_check;
349   event_t *event;
350 cp
351   last_ping_check = now;
352
353   srand(now);
354
355   for(;;)
356     {
357       now = time(NULL);
358
359       tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
360       tv.tv_usec = 0;
361
362       build_fdset(&fset);
363
364       if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
365         {
366           if(errno != EINTR && errno != EAGAIN)
367             {
368               syslog(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
369               cp_trace();
370               dump_connections();
371               return;
372             }
373
374           continue;
375         }
376
377       check_network_activity(&fset);
378
379       if(do_purge)
380         {
381           purge();
382           do_purge = 0;
383         }
384
385       /* Let's check if everybody is still alive */
386
387       if(last_ping_check + pingtimeout < now)
388         {
389           check_dead_connections();
390           last_ping_check = now;
391
392           if(routing_mode== RMODE_SWITCH)
393             age_mac();
394
395           age_past_requests();
396
397           /* Should we regenerate our key? */
398
399           if(keyexpires < now)
400             {
401               if(debug_lvl >= DEBUG_STATUS)
402                 syslog(LOG_INFO, _("Regenerating symmetric key"));
403
404               RAND_pseudo_bytes(myself->key, myself->keylength);
405               send_key_changed(myself->connection, myself);
406               keyexpires = now + keylifetime;
407             }
408         }
409
410
411       while((event = get_expired_event()))
412         {
413           event->handler(event->data);
414           free(event);
415         }
416
417       if(sigalrm)
418         {
419           syslog(LOG_INFO, _("Flushing event queue"));
420
421           while(event_tree->head)
422             {
423               event = (event_t *)event_tree->head->data;
424               event->handler(event->data);
425               event_del(event);
426             }
427           sigalrm = 0;
428         }
429
430       if(sighup)
431         {
432           sighup = 0;
433           close_network_connections();
434           exit_configuration(&config_tree);
435
436           syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds..."));
437           sleep(5);
438
439           init_configuration(&config_tree);
440
441           if(read_server_config())
442             {
443               syslog(LOG_ERR, _("Unable to reread configuration file, exitting."));
444               exit(1);
445             }
446
447           if(setup_network_connections())
448             return;
449
450           continue;
451         }
452     }
453 cp
454 }