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