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