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>
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.
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.
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.
20 $Id: net.c,v 1.35.4.187 2003/07/06 17:15:25 guus Exp $
33 #include <sys/types.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>
43 #ifdef HAVE_NETINET_IN_SYSTM_H
44 #include <netinet/in_systm.h>
46 #include <netinet/in.h>
47 #ifdef HAVE_NETINET_IP_H
48 #include <netinet/ip.h>
50 #ifdef HAVE_NETINET_TCP_H
51 #include <netinet/tcp.h>
54 #include <openssl/rand.h>
62 #include "connection.h"
83 /* Purge edges and subnets of unreachable nodes. Use carefully. */
87 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
94 if(debug_lvl >= DEBUG_PROTOCOL)
95 syslog(LOG_DEBUG, _("Purging unreachable nodes"));
97 for(nnode = node_tree->head; nnode; nnode = nnext) {
99 n = (node_t *) nnode->data;
101 if(!n->status.reachable) {
102 if(debug_lvl >= DEBUG_SCARY_THINGS)
103 syslog(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
106 for(snode = n->subnet_tree->head; snode; snode = snext) {
108 s = (subnet_t *) snode->data;
109 send_del_subnet(broadcast, s);
113 for(enode = n->edge_tree->head; enode; enode = enext) {
115 e = (edge_t *) enode->data;
116 send_del_edge(broadcast, e);
126 put all file descriptors in an fd_set array
127 While we're at it, purge stuff that needs to be removed.
129 int build_fdset(fd_set * fs)
131 avl_node_t *node, *next;
139 for(node = connection_tree->head; node; node = next) {
141 c = (connection_t *) node->data;
143 if(c->status.remove) {
145 if(!connection_tree->head)
148 FD_SET(c->socket, fs);
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;
163 FD_SET(device_fd, fs);
171 Terminate a connection:
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
177 void terminate_connection(connection_t *c, int report)
184 if(debug_lvl >= DEBUG_CONNECTIONS)
185 syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
186 c->name, c->hostname);
188 c->status.remove = 1;
189 c->status.active = 0;
192 c->node->connection = NULL;
199 send_del_edge(broadcast, c->edge);
203 /* Run MST and SSSP algorithms */
208 /* Check if this was our outgoing connection */
211 retry_outgoing(c->outgoing);
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.
224 void check_dead_connections(void)
226 avl_node_t *node, *next;
231 for(node = connection_tree->head; node; node = next) {
233 c = (connection_t *) node->data;
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);
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);
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);
263 check all connections to see if anything
264 happened on their sockets
266 void check_network_activity(fd_set * f)
271 int len = sizeof(result);
276 if(FD_ISSET(device_fd, f)) {
277 if(!read_packet(&packet))
278 route_outgoing(&packet);
281 for(node = connection_tree->head; node; node = node->next) {
282 c = (connection_t *) node->data;
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);
293 finish_connecting(c);
295 if(debug_lvl >= DEBUG_CONNECTIONS)
297 _("Error while connecting to %s (%s): %s"),
298 c->name, c->hostname, strerror(result));
300 do_outgoing_connection(c);
305 if(receive_meta(c) < 0) {
306 terminate_connection(c, c->status.active);
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);
316 if(FD_ISSET(listen_socket[i].tcp, f))
317 handle_new_meta_connection(listen_socket[i].tcp);
322 this is where it all happens...
329 time_t last_ping_check, last_config_check;
334 last_ping_check = now;
335 last_config_check = now;
341 tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
344 maxfd = build_fdset(&fset);
346 r = select(maxfd + 1, &fset, NULL, NULL, &tv);
349 if(errno != EINTR && errno != EAGAIN) {
350 syslog(LOG_ERR, _("Error while waiting for input: %s"),
360 check_network_activity(&fset);
367 /* Let's check if everybody is still alive */
369 if(last_ping_check + pingtimeout < now) {
370 check_dead_connections();
371 last_ping_check = now;
373 if(routing_mode == RMODE_SWITCH)
378 /* Should we regenerate our key? */
380 if(keyexpires < now) {
381 if(debug_lvl >= DEBUG_STATUS)
382 syslog(LOG_INFO, _("Regenerating symmetric key"));
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;
392 while((event = get_expired_event())) {
393 event->handler(event->data);
398 syslog(LOG_INFO, _("Flushing event queue"));
400 while(event_tree->head) {
401 event = (event_t *) event_tree->head->data;
402 event->handler(event->data);
416 /* Reread our own configuration file */
418 exit_configuration(&config_tree);
419 init_configuration(&config_tree);
421 if(read_server_config()) {
422 syslog(LOG_ERR, _("Unable to reread configuration file, exitting."));
426 /* Close connections to hosts that have a changed or deleted host config file */
428 for(node = connection_tree->head; node; node = node->next) {
429 c = (connection_t *) node->data;
432 free(c->outgoing->name);
433 freeaddrinfo(c->outgoing->ai);
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);
444 last_config_check = now;
446 /* Try to make outgoing connections */
448 try_outgoing_connections();