- simplify configure.in
[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.187 2003/07/06 17:15:25 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <netdb.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <signal.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <syslog.h>
36 #include <unistd.h>
37 #include <sys/ioctl.h>
38 /* SunOS really wants sys/socket.h BEFORE net/if.h,
39    and FreeBSD wants these lines below the rest. */
40 #include <arpa/inet.h>
41 #include <sys/socket.h>
42 #include <net/if.h>
43 #ifdef HAVE_NETINET_IN_SYSTM_H
44 #include <netinet/in_systm.h>
45 #endif
46 #include <netinet/in.h>
47 #ifdef HAVE_NETINET_IP_H
48 #include <netinet/ip.h>
49 #endif
50 #ifdef HAVE_NETINET_TCP_H
51 #include <netinet/tcp.h>
52 #endif
53
54 #include <openssl/rand.h>
55
56 #include <utils.h>
57 #include <xalloc.h>
58 #include <avl_tree.h>
59 #include <list.h>
60
61 #include "conf.h"
62 #include "connection.h"
63 #include "meta.h"
64 #include "net.h"
65 #include "netutl.h"
66 #include "process.h"
67 #include "protocol.h"
68 #include "subnet.h"
69 #include "graph.h"
70 #include "process.h"
71 #include "route.h"
72 #include "device.h"
73 #include "event.h"
74
75 #include "system.h"
76
77 int do_purge = 0;
78 int sighup = 0;
79 int sigalrm = 0;
80
81 time_t now = 0;
82
83 /* Purge edges and subnets of unreachable nodes. Use carefully. */
84
85 void purge(void)
86 {
87         avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
88         node_t *n;
89         edge_t *e;
90         subnet_t *s;
91
92         cp();
93
94         if(debug_lvl >= DEBUG_PROTOCOL)
95                 syslog(LOG_DEBUG, _("Purging unreachable nodes"));
96
97         for(nnode = node_tree->head; nnode; nnode = nnext) {
98                 nnext = nnode->next;
99                 n = (node_t *) nnode->data;
100
101                 if(!n->status.reachable) {
102                         if(debug_lvl >= DEBUG_SCARY_THINGS)
103                                 syslog(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
104                                            n->hostname);
105
106                         for(snode = n->subnet_tree->head; snode; snode = snext) {
107                                 snext = snode->next;
108                                 s = (subnet_t *) snode->data;
109                                 send_del_subnet(broadcast, s);
110                                 subnet_del(n, s);
111                         }
112
113                         for(enode = n->edge_tree->head; enode; enode = enext) {
114                                 enext = enode->next;
115                                 e = (edge_t *) enode->data;
116                                 send_del_edge(broadcast, e);
117                                 edge_del(e);
118                         }
119
120                         node_del(n);
121                 }
122         }
123 }
124
125 /*
126   put all file descriptors in an fd_set array
127   While we're at it, purge stuff that needs to be removed.
128 */
129 int build_fdset(fd_set * fs)
130 {
131         avl_node_t *node, *next;
132         connection_t *c;
133         int i, max = 0;
134
135         cp();
136
137         FD_ZERO(fs);
138
139         for(node = connection_tree->head; node; node = next) {
140                 next = node->next;
141                 c = (connection_t *) node->data;
142
143                 if(c->status.remove) {
144                         connection_del(c);
145                         if(!connection_tree->head)
146                                 purge();
147                 } else {
148                         FD_SET(c->socket, fs);
149                         if(c->socket > max)
150                                 max = c->socket;
151                 }
152         }
153
154         for(i = 0; i < listen_sockets; i++) {
155                 FD_SET(listen_socket[i].tcp, fs);
156                 if(listen_socket[i].tcp > max)
157                         max = listen_socket[i].tcp;
158                 FD_SET(listen_socket[i].udp, fs);
159                 if(listen_socket[i].udp > max)
160                         max = listen_socket[i].udp;
161         }
162
163         FD_SET(device_fd, fs);
164         if(device_fd > max)
165                 max = device_fd;
166         
167         return max;
168 }
169
170 /*
171   Terminate a connection:
172   - Close the socket
173   - Remove associated edge and tell other connections about it if report = 1
174   - Check if we need to retry making an outgoing connection
175   - Deactivate the host
176 */
177 void terminate_connection(connection_t *c, int report)
178 {
179         cp();
180
181         if(c->status.remove)
182                 return;
183
184         if(debug_lvl >= DEBUG_CONNECTIONS)
185                 syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
186                            c->name, c->hostname);
187
188         c->status.remove = 1;
189         c->status.active = 0;
190
191         if(c->node)
192                 c->node->connection = NULL;
193
194         if(c->socket)
195                 close(c->socket);
196
197         if(c->edge) {
198                 if(report)
199                         send_del_edge(broadcast, c->edge);
200
201                 edge_del(c->edge);
202
203                 /* Run MST and SSSP algorithms */
204
205                 graph();
206         }
207
208         /* Check if this was our outgoing connection */
209
210         if(c->outgoing) {
211                 retry_outgoing(c->outgoing);
212                 c->outgoing = NULL;
213         }
214 }
215
216 /*
217   Check if the other end is active.
218   If we have sent packets, but didn't receive any,
219   then possibly the other end is dead. We send a
220   PING request over the meta connection. If the other
221   end does not reply in time, we consider them dead
222   and close the connection.
223 */
224 void check_dead_connections(void)
225 {
226         avl_node_t *node, *next;
227         connection_t *c;
228
229         cp();
230
231         for(node = connection_tree->head; node; node = next) {
232                 next = node->next;
233                 c = (connection_t *) node->data;
234
235                 if(c->last_ping_time + pingtimeout < now) {
236                         if(c->status.active) {
237                                 if(c->status.pinged) {
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                                 } else {
244                                         send_ping(c);
245                                 }
246                         } else {
247                                 if(c->status.remove) {
248                                         syslog(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
249                                                    c->name, c->hostname, c->status);
250                                         connection_del(c);
251                                         continue;
252                                 }
253                                 if(debug_lvl >= DEBUG_CONNECTIONS)
254                                         syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
255                                                    c->name, c->hostname);
256                                 terminate_connection(c, 0);
257                         }
258                 }
259         }
260 }
261
262 /*
263   check all connections to see if anything
264   happened on their sockets
265 */
266 void check_network_activity(fd_set * f)
267 {
268         connection_t *c;
269         avl_node_t *node;
270         int result, i;
271         int len = sizeof(result);
272         vpn_packet_t packet;
273
274         cp();
275
276         if(FD_ISSET(device_fd, f)) {
277                 if(!read_packet(&packet))
278                         route_outgoing(&packet);
279         }
280
281         for(node = connection_tree->head; node; node = node->next) {
282                 c = (connection_t *) node->data;
283
284                 if(c->status.remove)
285                         continue;
286
287                 if(FD_ISSET(c->socket, f)) {
288                         if(c->status.connecting) {
289                                 c->status.connecting = 0;
290                                 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
291
292                                 if(!result)
293                                         finish_connecting(c);
294                                 else {
295                                         if(debug_lvl >= DEBUG_CONNECTIONS)
296                                                 syslog(LOG_DEBUG,
297                                                            _("Error while connecting to %s (%s): %s"),
298                                                            c->name, c->hostname, strerror(result));
299                                         close(c->socket);
300                                         do_outgoing_connection(c);
301                                         continue;
302                                 }
303                         }
304
305                         if(receive_meta(c) < 0) {
306                                 terminate_connection(c, c->status.active);
307                                 continue;
308                         }
309                 }
310         }
311
312         for(i = 0; i < listen_sockets; i++) {
313                 if(FD_ISSET(listen_socket[i].udp, f))
314                         handle_incoming_vpn_data(listen_socket[i].udp);
315
316                 if(FD_ISSET(listen_socket[i].tcp, f))
317                         handle_new_meta_connection(listen_socket[i].tcp);
318         }
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, maxfd;
329         time_t last_ping_check, last_config_check;
330         event_t *event;
331
332         cp();
333
334         last_ping_check = now;
335         last_config_check = now;
336         srand(now);
337
338         for(;;) {
339                 now = time(NULL);
340
341                 tv.tv_sec = 1 + (rand() & 7);   /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
342                 tv.tv_usec = 0;
343
344                 maxfd = build_fdset(&fset);
345
346                 r = select(maxfd + 1, &fset, NULL, NULL, &tv);
347
348                 if(r < 0) {
349                         if(errno != EINTR && errno != EAGAIN) {
350                                 syslog(LOG_ERR, _("Error while waiting for input: %s"),
351                                            strerror(errno));
352                                 cp_trace();
353                                 dump_connections();
354                                 return;
355                         }
356
357                         continue;
358                 }
359
360                 check_network_activity(&fset);
361
362                 if(do_purge) {
363                         purge();
364                         do_purge = 0;
365                 }
366
367                 /* Let's check if everybody is still alive */
368
369                 if(last_ping_check + pingtimeout < now) {
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                                 if(debug_lvl >= DEBUG_STATUS)
382                                         syslog(LOG_INFO, _("Regenerating symmetric key"));
383
384                                 RAND_pseudo_bytes(myself->key, myself->keylength);
385                                 EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, myself->key, myself->key + myself->cipher->key_len);
386                                 send_key_changed(broadcast, myself);
387                                 keyexpires = now + keylifetime;
388                         }
389                 }
390
391
392                 while((event = get_expired_event())) {
393                         event->handler(event->data);
394                         free(event);
395                 }
396
397                 if(sigalrm) {
398                         syslog(LOG_INFO, _("Flushing event queue"));
399
400                         while(event_tree->head) {
401                                 event = (event_t *) event_tree->head->data;
402                                 event->handler(event->data);
403                                 event_del(event);
404                         }
405                         sigalrm = 0;
406                 }
407
408                 if(sighup) {
409                         connection_t *c;
410                         avl_node_t *node;
411                         char *fname;
412                         struct stat s;
413                         
414                         sighup = 0;
415                         
416                         /* Reread our own configuration file */
417
418                         exit_configuration(&config_tree);
419                         init_configuration(&config_tree);
420
421                         if(read_server_config()) {
422                                 syslog(LOG_ERR, _("Unable to reread configuration file, exitting."));
423                                 exit(1);
424                         }
425
426                         /* Close connections to hosts that have a changed or deleted host config file */
427                         
428                         for(node = connection_tree->head; node; node = node->next) {
429                                 c = (connection_t *) node->data;
430                                 
431                                 if(c->outgoing) {
432                                         free(c->outgoing->name);
433                                         freeaddrinfo(c->outgoing->ai);
434                                         free(c->outgoing);
435                                         c->outgoing = NULL;
436                                 }
437                                 
438                                 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
439                                 if(stat(fname, &s) || s.st_mtime > last_config_check)
440                                         terminate_connection(c, c->status.active);
441                                 free(fname);
442                         }
443
444                         last_config_check = now;
445
446                         /* Try to make outgoing connections */
447                         
448                         try_outgoing_connections();
449                                                 
450                         continue;
451                 }
452         }
453 }