4061d7af7bf0e28191373256fbd12e83d76f9dff
[tinc] / 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.162 2002/03/11 11:23:04 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 int do_prune = 0;
72 int do_purge = 0;
73 int sighup = 0;
74 int sigalrm = 0;
75
76 time_t now = 0;
77
78 /*
79   put all file descriptors in an fd_set array
80 */
81 void build_fdset(fd_set *fs)
82 {
83   avl_node_t *node;
84   connection_t *c;
85   int i;
86 cp
87   FD_ZERO(fs);
88
89   for(node = connection_tree->head; node; node = node->next)
90     {
91       c = (connection_t *)node->data;
92       FD_SET(c->socket, fs);
93     }
94
95   for(i = 0; i < listen_sockets; i++)
96     {
97       FD_SET(tcp_socket[i], fs);
98       FD_SET(udp_socket[i], fs);
99     }
100
101   FD_SET(device_fd, fs);
102 cp
103 }
104
105 /* Purge edges and subnets of unreachable nodes. Use carefully. */
106
107 void purge(void)
108 {
109   avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext, *cnode;
110   node_t *n;
111   edge_t *e;
112   subnet_t *s;
113   connection_t *c;
114 cp
115   if(debug_lvl >= DEBUG_PROTOCOL)
116     syslog(LOG_DEBUG, _("Purging unreachable nodes"));
117
118   for(nnode = node_tree->head; nnode; nnode = nnext)
119   {
120     nnext = nnode->next;
121     n = (node_t *)nnode->data;
122
123     if(!n->status.reachable)
124     {
125       if(debug_lvl >= DEBUG_SCARY_THINGS)
126         syslog(LOG_DEBUG, _("Purging node %s (%s)"), n->name, n->hostname);
127
128       for(snode = n->subnet_tree->head; snode; snode = snext)
129       {
130         snext = snode->next;
131         s = (subnet_t *)snode->data;
132
133         for(cnode = connection_tree->head; cnode; cnode = cnode->next)
134         {
135           c = (connection_t *)cnode->data;
136           if(c->status.active)
137             send_del_subnet(c, s);
138         }
139
140         subnet_del(n, s);
141       }
142
143       for(enode = n->edge_tree->head; enode; enode = enext)
144       {
145         enext = enode->next;
146         e = (edge_t *)enode->data;
147
148         for(cnode = connection_tree->head; cnode; cnode = cnode->next)
149         {
150           c = (connection_t *)cnode->data;
151           if(c->status.active)
152             send_del_edge(c, e);
153         }
154
155         edge_del(e);
156       }
157
158       node_del(n);
159     }
160   }
161 cp
162 }
163
164 /*
165   Terminate a connection:
166   - Close the socket
167   - Remove associated edge and tell other connections about it if report = 1
168   - Check if we need to retry making an outgoing connection
169   - Deactivate the host
170 */
171 void terminate_connection(connection_t *c, int report)
172 {
173   avl_node_t *node;
174   connection_t *other;
175 cp
176   if(c->status.remove)
177     return;
178
179   if(debug_lvl >= DEBUG_CONNECTIONS)
180     syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
181            c->name, c->hostname);
182
183   c->status.remove = 1;
184
185   if(c->socket)
186     close(c->socket);
187
188   if(c->edge)
189     {
190       if(report)
191         {
192           for(node = connection_tree->head; node; node = node->next)
193             {
194               other = (connection_t *)node->data;
195               if(other->status.active && other != c)
196                 send_del_edge(other, c->edge);
197             }
198         }
199
200       edge_del(c->edge);
201     }
202
203   /* Run MST and SSSP algorithms */
204
205   graph();
206
207   /* Check if this was our outgoing connection */
208
209   if(c->outgoing)
210     {
211       retry_outgoing(c->outgoing);
212       c->outgoing = NULL;
213     }
214
215   /* Deactivate */
216
217   c->status.active = 0;
218   if(c->node)
219     c->node->connection = NULL;
220   do_prune = 1;
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, *next;
235   connection_t *c;
236 cp
237   for(node = connection_tree->head; node; node = next)
238     {
239       next = node->next;
240       c = (connection_t *)node->data;
241       if(c->last_ping_time + pingtimeout < now)
242         {
243           if(c->status.active)
244             {
245               if(c->status.pinged)
246                 {
247                   if(debug_lvl >= DEBUG_PROTOCOL)
248                     syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
249                            c->name, c->hostname);
250                   c->status.timeout = 1;
251                   terminate_connection(c, 1);
252                 }
253               else
254                 {
255                   send_ping(c);
256                 }
257             }
258           else
259             {
260               if(debug_lvl >= DEBUG_CONNECTIONS)
261                 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
262                        c->name, c->hostname);
263               terminate_connection(c, 0);
264             }
265         }
266     }
267 cp
268 }
269
270 /*
271   check all connections to see if anything
272   happened on their sockets
273 */
274 void check_network_activity(fd_set *f)
275 {
276   connection_t *c;
277   avl_node_t *node;
278   int result, i;
279   int len = sizeof(result);
280   vpn_packet_t packet;
281 cp
282   if(FD_ISSET(device_fd, f))
283     {
284       if(!read_packet(&packet))
285         route_outgoing(&packet);
286     }
287
288   for(i = 0; i < listen_sockets; i++)
289     {
290       if(FD_ISSET(udp_socket[i], f))
291         handle_incoming_vpn_data(udp_socket[i]);
292       if(FD_ISSET(tcp_socket[i], f))
293         handle_new_meta_connection(tcp_socket[i]);
294     }
295
296   for(node = connection_tree->head; node; node = node->next)
297     {
298       c = (connection_t *)node->data;
299
300       if(c->status.remove)
301         return;
302
303       if(FD_ISSET(c->socket, f))
304         {
305           if(c->status.connecting)
306             {
307               c->status.connecting = 0;
308               getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
309               if(!result)
310                 finish_connecting(c);
311               else
312                 {
313                   if(debug_lvl >= DEBUG_CONNECTIONS)
314                     syslog(LOG_DEBUG, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(result));
315                   close(c->socket);
316                   do_outgoing_connection(c);
317                   continue;
318                 }
319             }
320           if(receive_meta(c) < 0)
321             {
322               terminate_connection(c, c->status.active);
323               return;
324             }
325         }
326     }
327 cp
328 }
329
330 void prune_connections(void)
331 {
332   connection_t *c;
333   avl_node_t *node, *next;
334 cp
335   for(node = connection_tree->head; node; node = next)
336     {
337       next = node->next;
338       c = (connection_t *)node->data;
339
340       if(c->status.remove)
341         connection_del(c);
342     }
343
344   if(!connection_tree->head)
345     purge();
346 cp
347 }
348
349 /*
350   this is where it all happens...
351 */
352 void main_loop(void)
353 {
354   fd_set fset;
355   struct timeval tv;
356   int r;
357   time_t last_ping_check;
358   int t;
359   event_t *event;
360 cp
361   last_ping_check = now;
362
363   srand(now);
364
365   for(;;)
366     {
367       now = time(NULL);
368
369       tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
370       tv.tv_usec = 0;
371
372       build_fdset(&fset);
373
374       if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
375         {
376           if(errno != EINTR) /* because of a signal */
377             {
378               syslog(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
379               return;
380             }
381         }
382
383       if(r > 0)
384         check_network_activity(&fset);
385
386       if(do_prune)
387         {
388           prune_connections();
389           do_prune = 0;
390         }
391
392      if(do_purge)
393         {
394           purge();
395           do_purge = 0;
396         }
397
398       /* Let's check if everybody is still alive */
399
400       if(last_ping_check + pingtimeout < now)
401         {
402           check_dead_connections();
403           last_ping_check = now;
404
405           if(routing_mode== RMODE_SWITCH)
406             age_mac();
407
408           /* Should we regenerate our key? */
409
410           if(keyexpires < now)
411             {
412               if(debug_lvl >= DEBUG_STATUS)
413                 syslog(LOG_INFO, _("Regenerating symmetric key"));
414
415               RAND_pseudo_bytes(myself->key, myself->keylength);
416               send_key_changed(myself->connection, myself);
417               keyexpires = now + keylifetime;
418             }
419         }
420
421
422       while((event = get_expired_event()))
423         {
424           event->handler(event->data);
425           free(event);
426         }
427
428       if(sigalrm)
429         {
430           syslog(LOG_INFO, _("Flushing event queue"));
431
432           while(event_tree->head)
433             {
434               event = (event_t *)event_tree->head->data;
435               event->handler(event->data);
436               event_del(event);
437             }
438           sigalrm = 0;
439         }
440
441       if(sighup)
442         {
443           sighup = 0;
444           close_network_connections();
445           exit_configuration(&config_tree);
446
447           syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds..."));
448           sleep(5);
449
450           init_configuration(&config_tree);
451
452           if(read_server_config())
453             {
454               syslog(LOG_ERR, _("Unable to reread configuration file, exitting."));
455               exit(1);
456             }
457
458           if(setup_network_connections())
459             return;
460
461           continue;
462         }
463     }
464 cp
465 }