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