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