Cleanups:
[oweals/tinc.git] / 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.179 2002/09/09 19:39: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_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 "graph.h"
69 #include "process.h"
70 #include "route.h"
71 #include "device.h"
72 #include "event.h"
73
74 #include "system.h"
75
76 #ifndef HAVE_RAND_PSEUDO_BYTES
77 #define RAND_pseudo_bytes RAND_bytes
78 #endif
79
80 int do_purge = 0;
81 int sighup = 0;
82 int sigalrm = 0;
83
84 time_t now = 0;
85
86 /* Purge edges and subnets of unreachable nodes. Use carefully. */
87
88 void purge(void)
89 {
90   avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
91   node_t *n;
92   edge_t *e;
93   subnet_t *s;
94   cp();
95   if(debug_lvl >= DEBUG_PROTOCOL)
96     syslog(LOG_DEBUG, _("Purging unreachable nodes"));
97
98   for(nnode = node_tree->head; nnode; nnode = nnext)
99   {
100     nnext = nnode->next;
101     n = (node_t *)nnode->data;
102
103     if(!n->status.reachable)
104     {
105       if(debug_lvl >= DEBUG_SCARY_THINGS)
106         syslog(LOG_DEBUG, _("Purging node %s (%s)"), n->name, n->hostname);
107
108       for(snode = n->subnet_tree->head; snode; snode = snext)
109       {
110         snext = snode->next;
111         s = (subnet_t *)snode->data;
112         send_del_subnet(broadcast, s);
113         subnet_del(n, s);
114       }
115
116       for(enode = n->edge_tree->head; enode; enode = enext)
117       {
118         enext = enode->next;
119         e = (edge_t *)enode->data;
120         send_del_edge(broadcast, e);
121         edge_del(e);
122       }
123
124       node_del(n);
125     }
126   }
127   cp();
128 }
129
130 /*
131   put all file descriptors in an fd_set array
132   While we're at it, purge stuff that needs to be removed.
133 */
134 void build_fdset(fd_set *fs)
135 {
136   avl_node_t *node, *next;
137   connection_t *c;
138   int i;
139   cp();
140   FD_ZERO(fs);
141
142   for(node = connection_tree->head; node; node = next)
143     {
144       next = node->next;
145       c = (connection_t *)node->data;
146
147       if(c->status.remove)
148         {
149           connection_del(c);
150           if(!connection_tree->head)
151             purge();
152         }
153       else
154         FD_SET(c->socket, fs);
155     }
156
157   for(i = 0; i < listen_sockets; i++)
158     {
159       FD_SET(listen_socket[i].tcp, fs);
160       FD_SET(listen_socket[i].udp, fs);
161     }
162
163   FD_SET(device_fd, fs);
164   cp();
165 }
166
167 /*
168   Terminate a connection:
169   - Close the socket
170   - Remove associated edge and tell other connections about it if report = 1
171   - Check if we need to retry making an outgoing connection
172   - Deactivate the host
173 */
174 void terminate_connection(connection_t *c, int report)
175 {
176   cp();
177   if(c->status.remove)
178     return;
179
180   if(debug_lvl >= DEBUG_CONNECTIONS)
181     syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
182            c->name, c->hostname);
183
184   c->status.remove = 1;
185   c->status.active = 0;
186
187   if(c->node)
188     c->node->connection = NULL;
189
190   if(c->socket)
191     close(c->socket);
192
193   if(c->edge)
194     {
195       if(report)
196         send_del_edge(broadcast, c->edge);
197
198       edge_del(c->edge);
199
200       /* Run MST and SSSP algorithms */
201
202       graph();
203     }
204
205   /* Check if this was our outgoing connection */
206
207   if(c->outgoing)
208     {
209       retry_outgoing(c->outgoing);
210       c->outgoing = NULL;
211     }
212   cp();
213 }
214
215 /*
216   Check if the other end is active.
217   If we have sent packets, but didn't receive any,
218   then possibly the other end is dead. We send a
219   PING request over the meta connection. If the other
220   end does not reply in time, we consider them dead
221   and close the connection.
222 */
223 void check_dead_connections(void)
224 {
225   avl_node_t *node, *next;
226   connection_t *c;
227   cp();
228   for(node = connection_tree->head; node; node = next)
229     {
230       next = node->next;
231       c = (connection_t *)node->data;
232       if(c->last_ping_time + pingtimeout < now)
233         {
234           if(c->status.active)
235             {
236               if(c->status.pinged)
237                 {
238                   if(debug_lvl >= DEBUG_PROTOCOL)
239                     syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
240                            c->name, c->hostname);
241                   c->status.timeout = 1;
242                   terminate_connection(c, 1);
243                 }
244               else
245                 {
246                   send_ping(c);
247                 }
248             }
249           else
250             {
251               if(c->status.remove)
252                 {
253                   syslog(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."), c->name, c->hostname, c->status);
254                   connection_del(c);
255                   continue;
256                 }
257               if(debug_lvl >= DEBUG_CONNECTIONS)
258                 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
259                        c->name, c->hostname);
260               terminate_connection(c, 0);
261             }
262         }
263     }
264   cp();
265 }
266
267 /*
268   check all connections to see if anything
269   happened on their sockets
270 */
271 void check_network_activity(fd_set *f)
272 {
273   connection_t *c;
274   avl_node_t *node;
275   int result, i;
276   int len = sizeof(result);
277   vpn_packet_t packet;
278   cp();
279   if(FD_ISSET(device_fd, f))
280     {
281       if(!read_packet(&packet))
282         route_outgoing(&packet);
283     }
284
285   for(node = connection_tree->head; node; node = node->next)
286     {
287       c = (connection_t *)node->data;
288
289       if(c->status.remove)
290         continue;
291
292       if(FD_ISSET(c->socket, f))
293         {
294           if(c->status.connecting)
295             {
296               c->status.connecting = 0;
297               getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
298               if(!result)
299                 finish_connecting(c);
300               else
301                 {
302                   if(debug_lvl >= DEBUG_CONNECTIONS)
303                     syslog(LOG_DEBUG, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(result));
304                   close(c->socket);
305                   do_outgoing_connection(c);
306                   continue;
307                 }
308             }
309           if(receive_meta(c) < 0)
310             {
311               terminate_connection(c, c->status.active);
312               continue;
313             }
314         }
315     }
316
317   for(i = 0; i < listen_sockets; i++)
318     {
319       if(FD_ISSET(listen_socket[i].udp, f))
320         handle_incoming_vpn_data(listen_socket[i].udp);
321       if(FD_ISSET(listen_socket[i].tcp, f))
322         handle_new_meta_connection(listen_socket[i].tcp);
323     }
324   cp();
325 }
326
327 /*
328   this is where it all happens...
329 */
330 void main_loop(void)
331 {
332   fd_set fset;
333   struct timeval tv;
334   int r;
335   time_t last_ping_check;
336   event_t *event;
337   cp();
338   last_ping_check = now;
339
340   srand(now);
341
342   for(;;)
343     {
344       now = time(NULL);
345
346       tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
347       tv.tv_usec = 0;
348
349       build_fdset(&fset);
350
351       r = select(FD_SETSIZE, &fset, NULL, NULL, &tv);
352
353       if(r < 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(broadcast, 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 }