Put #ifndef checks for HAVE_RAND_PSEUDO_BYTES in the correct places.
[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.170 2002/04/18 20:09:05 zarq 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         connection_del(c);
159       else
160         FD_SET(c->socket, fs);
161     }
162
163   if(!connection_tree->head)
164     purge();
165
166   for(i = 0; i < listen_sockets; i++)
167     {
168       FD_SET(listen_socket[i].tcp, fs);
169       FD_SET(listen_socket[i].udp, fs);
170     }
171
172   FD_SET(device_fd, fs);
173 cp
174 }
175
176 /*
177   Terminate a connection:
178   - Close the socket
179   - Remove associated edge and tell other connections about it if report = 1
180   - Check if we need to retry making an outgoing connection
181   - Deactivate the host
182 */
183 void terminate_connection(connection_t *c, int report)
184 {
185   avl_node_t *node;
186   connection_t *other;
187 cp
188   if(c->status.remove)
189     return;
190
191   if(debug_lvl >= DEBUG_CONNECTIONS)
192     syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
193            c->name, c->hostname);
194
195   c->status.remove = 1;
196   c->status.active = 0;
197
198   if(c->node)
199     c->node->connection = NULL;
200
201   if(c->socket)
202     close(c->socket);
203
204   if(c->edge)
205     {
206       if(report)
207         {
208           for(node = connection_tree->head; node; node = node->next)
209             {
210               other = (connection_t *)node->data;
211               if(other->status.active && other != c)
212                 send_del_edge(other, c->edge);
213             }
214         }
215
216       edge_del(c->edge);
217
218       /* Run MST and SSSP algorithms */
219
220       graph();
221     }
222
223   /* Check if this was our outgoing connection */
224
225   if(c->outgoing)
226     {
227       retry_outgoing(c->outgoing);
228       c->outgoing = NULL;
229     }
230 cp
231 }
232
233 /*
234   Check if the other end is active.
235   If we have sent packets, but didn't receive any,
236   then possibly the other end is dead. We send a
237   PING request over the meta connection. If the other
238   end does not reply in time, we consider them dead
239   and close the connection.
240 */
241 void check_dead_connections(void)
242 {
243   avl_node_t *node, *next;
244   connection_t *c;
245 cp
246   for(node = connection_tree->head; node; node = next)
247     {
248       next = node->next;
249       c = (connection_t *)node->data;
250       if(c->last_ping_time + pingtimeout < now)
251         {
252           if(c->status.active)
253             {
254               if(c->status.pinged)
255                 {
256                   if(debug_lvl >= DEBUG_PROTOCOL)
257                     syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
258                            c->name, c->hostname);
259                   c->status.timeout = 1;
260                   terminate_connection(c, 1);
261                 }
262               else
263                 {
264                   send_ping(c);
265                 }
266             }
267           else
268             {
269               if(debug_lvl >= DEBUG_CONNECTIONS)
270                 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
271                        c->name, c->hostname);
272               terminate_connection(c, 0);
273             }
274         }
275     }
276 cp
277 }
278
279 /*
280   check all connections to see if anything
281   happened on their sockets
282 */
283 void check_network_activity(fd_set *f)
284 {
285   connection_t *c;
286   avl_node_t *node;
287   int result, i;
288   int len = sizeof(result);
289   vpn_packet_t packet;
290 cp
291   if(FD_ISSET(device_fd, f))
292     {
293       if(!read_packet(&packet))
294         route_outgoing(&packet);
295     }
296
297   for(node = connection_tree->head; node; node = node->next)
298     {
299       c = (connection_t *)node->data;
300
301       if(c->status.remove)
302         continue;
303
304       if(FD_ISSET(c->socket, f))
305         {
306           if(c->status.connecting)
307             {
308               c->status.connecting = 0;
309               getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
310               if(!result)
311                 finish_connecting(c);
312               else
313                 {
314                   if(debug_lvl >= DEBUG_CONNECTIONS)
315                     syslog(LOG_DEBUG, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(result));
316                   close(c->socket);
317                   do_outgoing_connection(c);
318                   continue;
319                 }
320             }
321           if(receive_meta(c) < 0)
322             {
323               terminate_connection(c, c->status.active);
324               continue;
325             }
326         }
327     }
328
329   for(i = 0; i < listen_sockets; i++)
330     {
331       if(FD_ISSET(listen_socket[i].udp, f))
332         handle_incoming_vpn_data(listen_socket[i].udp);
333       if(FD_ISSET(listen_socket[i].tcp, f))
334         handle_new_meta_connection(listen_socket[i].tcp);
335     }
336 cp
337 }
338
339 /*
340   this is where it all happens...
341 */
342 void main_loop(void)
343 {
344   fd_set fset;
345   struct timeval tv;
346   int r;
347   time_t last_ping_check;
348   event_t *event;
349 cp
350   last_ping_check = now;
351
352   srand(now);
353
354   for(;;)
355     {
356       now = time(NULL);
357
358       tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
359       tv.tv_usec = 0;
360
361       build_fdset(&fset);
362
363       if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
364         {
365           if(errno != EINTR && errno != EAGAIN)
366             {
367               syslog(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
368               cp_trace();
369               dump_connections();
370               return;
371             }
372
373           continue;
374         }
375
376       check_network_activity(&fset);
377
378       if(do_purge)
379         {
380           purge();
381           do_purge = 0;
382         }
383
384       /* Let's check if everybody is still alive */
385
386       if(last_ping_check + pingtimeout < now)
387         {
388           check_dead_connections();
389           last_ping_check = now;
390
391           if(routing_mode== RMODE_SWITCH)
392             age_mac();
393
394           age_past_requests();
395
396           /* Should we regenerate our key? */
397
398           if(keyexpires < now)
399             {
400               if(debug_lvl >= DEBUG_STATUS)
401                 syslog(LOG_INFO, _("Regenerating symmetric key"));
402
403               RAND_pseudo_bytes(myself->key, myself->keylength);
404               send_key_changed(myself->connection, myself);
405               keyexpires = now + keylifetime;
406             }
407         }
408
409
410       while((event = get_expired_event()))
411         {
412           event->handler(event->data);
413           free(event);
414         }
415
416       if(sigalrm)
417         {
418           syslog(LOG_INFO, _("Flushing event queue"));
419
420           while(event_tree->head)
421             {
422               event = (event_t *)event_tree->head->data;
423               event->handler(event->data);
424               event_del(event);
425             }
426           sigalrm = 0;
427         }
428
429       if(sighup)
430         {
431           sighup = 0;
432           close_network_connections();
433           exit_configuration(&config_tree);
434
435           syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds..."));
436           sleep(5);
437
438           init_configuration(&config_tree);
439
440           if(read_server_config())
441             {
442               syslog(LOG_ERR, _("Unable to reread configuration file, exitting."));
443               exit(1);
444             }
445
446           if(setup_network_connections())
447             return;
448
449           continue;
450         }
451     }
452 cp
453 }