Reset the *correct* seqnos.
[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.177 2002/09/04 16:26:44 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(debug_lvl >= DEBUG_CONNECTIONS)
252                 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
253                        c->name, c->hostname);
254               terminate_connection(c, 0);
255             }
256         }
257     }
258 cp
259 }
260
261 /*
262   check all connections to see if anything
263   happened on their sockets
264 */
265 void check_network_activity(fd_set *f)
266 {
267   connection_t *c;
268   avl_node_t *node;
269   int result, i;
270   int len = sizeof(result);
271   vpn_packet_t packet;
272 cp
273   if(FD_ISSET(device_fd, f))
274     {
275       if(!read_packet(&packet))
276         route_outgoing(&packet);
277     }
278
279   for(node = connection_tree->head; node; node = node->next)
280     {
281       c = (connection_t *)node->data;
282
283       if(c->status.remove)
284         continue;
285
286       if(FD_ISSET(c->socket, f))
287         {
288           if(c->status.connecting)
289             {
290               c->status.connecting = 0;
291               getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
292               if(!result)
293                 finish_connecting(c);
294               else
295                 {
296                   if(debug_lvl >= DEBUG_CONNECTIONS)
297                     syslog(LOG_DEBUG, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(result));
298                   close(c->socket);
299                   do_outgoing_connection(c);
300                   continue;
301                 }
302             }
303           if(receive_meta(c) < 0)
304             {
305               terminate_connection(c, c->status.active);
306               continue;
307             }
308         }
309     }
310
311   for(i = 0; i < listen_sockets; i++)
312     {
313       if(FD_ISSET(listen_socket[i].udp, f))
314         handle_incoming_vpn_data(listen_socket[i].udp);
315       if(FD_ISSET(listen_socket[i].tcp, f))
316         handle_new_meta_connection(listen_socket[i].tcp);
317     }
318 cp
319 }
320
321 /*
322   this is where it all happens...
323 */
324 void main_loop(void)
325 {
326   fd_set fset;
327   struct timeval tv;
328   int r;
329   time_t last_ping_check;
330   event_t *event;
331 cp
332   last_ping_check = now;
333
334   srand(now);
335
336   for(;;)
337     {
338       now = time(NULL);
339
340       tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
341       tv.tv_usec = 0;
342
343       build_fdset(&fset);
344
345       if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
346         {
347           if(errno != EINTR && errno != EAGAIN)
348             {
349               syslog(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
350               cp_trace();
351               dump_connections();
352               return;
353             }
354
355           continue;
356         }
357
358       check_network_activity(&fset);
359
360       if(do_purge)
361         {
362           purge();
363           do_purge = 0;
364         }
365
366       /* Let's check if everybody is still alive */
367
368       if(last_ping_check + pingtimeout < now)
369         {
370           check_dead_connections();
371           last_ping_check = now;
372
373           if(routing_mode== RMODE_SWITCH)
374             age_mac();
375
376           age_past_requests();
377
378           /* Should we regenerate our key? */
379
380           if(keyexpires < now)
381             {
382               if(debug_lvl >= DEBUG_STATUS)
383                 syslog(LOG_INFO, _("Regenerating symmetric key"));
384
385               RAND_pseudo_bytes(myself->key, myself->keylength);
386               send_key_changed(broadcast, myself);
387               keyexpires = now + keylifetime;
388             }
389         }
390
391
392       while((event = get_expired_event()))
393         {
394           event->handler(event->data);
395           free(event);
396         }
397
398       if(sigalrm)
399         {
400           syslog(LOG_INFO, _("Flushing event queue"));
401
402           while(event_tree->head)
403             {
404               event = (event_t *)event_tree->head->data;
405               event->handler(event->data);
406               event_del(event);
407             }
408           sigalrm = 0;
409         }
410
411       if(sighup)
412         {
413           sighup = 0;
414           close_network_connections();
415           exit_configuration(&config_tree);
416
417           syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds..."));
418           sleep(5);
419
420           init_configuration(&config_tree);
421
422           if(read_server_config())
423             {
424               syslog(LOG_ERR, _("Unable to reread configuration file, exitting."));
425               exit(1);
426             }
427
428           if(setup_network_connections())
429             return;
430
431           continue;
432         }
433     }
434 cp
435 }