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.189 2003/07/06 23:16:28 guus Exp $
33 #include <sys/types.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>
42 #ifdef HAVE_NETINET_IN_SYSTM_H
43 #include <netinet/in_systm.h>
45 #include <netinet/in.h>
46 #ifdef HAVE_NETINET_IP_H
47 #include <netinet/ip.h>
49 #ifdef HAVE_NETINET_TCP_H
50 #include <netinet/tcp.h>
53 #include <openssl/rand.h>
61 #include "connection.h"
83 /* Purge edges and subnets of unreachable nodes. Use carefully. */
85 static void purge(void)
87 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
94 logger(DEBUG_PROTOCOL, LOG_DEBUG, _("Purging unreachable nodes"));
96 for(nnode = node_tree->head; nnode; nnode = nnext) {
98 n = (node_t *) nnode->data;
100 if(!n->status.reachable) {
101 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, _("Purging node %s (%s)"), n->name,
104 for(snode = n->subnet_tree->head; snode; snode = snext) {
106 s = (subnet_t *) snode->data;
107 send_del_subnet(broadcast, s);
111 for(enode = n->edge_tree->head; enode; enode = enext) {
113 e = (edge_t *) enode->data;
114 send_del_edge(broadcast, e);
124 put all file descriptors in an fd_set array
125 While we're at it, purge stuff that needs to be removed.
127 static int build_fdset(fd_set * fs)
129 avl_node_t *node, *next;
137 for(node = connection_tree->head; node; node = next) {
139 c = (connection_t *) node->data;
141 if(c->status.remove) {
143 if(!connection_tree->head)
146 FD_SET(c->socket, fs);
152 for(i = 0; i < listen_sockets; i++) {
153 FD_SET(listen_socket[i].tcp, fs);
154 if(listen_socket[i].tcp > max)
155 max = listen_socket[i].tcp;
156 FD_SET(listen_socket[i].udp, fs);
157 if(listen_socket[i].udp > max)
158 max = listen_socket[i].udp;
161 FD_SET(device_fd, fs);
169 Terminate a connection:
171 - Remove associated edge and tell other connections about it if report = 1
172 - Check if we need to retry making an outgoing connection
173 - Deactivate the host
175 void terminate_connection(connection_t *c, int report)
182 logger(DEBUG_CONNECTIONS, LOG_NOTICE, _("Closing connection with %s (%s)"),
183 c->name, c->hostname);
185 c->status.remove = 1;
186 c->status.active = 0;
189 c->node->connection = NULL;
196 send_del_edge(broadcast, c->edge);
200 /* Run MST and SSSP algorithms */
205 /* Check if this was our outgoing connection */
208 retry_outgoing(c->outgoing);
214 Check if the other end is active.
215 If we have sent packets, but didn't receive any,
216 then possibly the other end is dead. We send a
217 PING request over the meta connection. If the other
218 end does not reply in time, we consider them dead
219 and close the connection.
221 static void check_dead_connections(void)
223 avl_node_t *node, *next;
228 for(node = connection_tree->head; node; node = next) {
230 c = (connection_t *) node->data;
232 if(c->last_ping_time + pingtimeout < now) {
233 if(c->status.active) {
234 if(c->status.pinged) {
235 logger(DEBUG_CONNECTIONS, LOG_INFO, _("%s (%s) didn't respond to PING"),
236 c->name, c->hostname);
237 c->status.timeout = 1;
238 terminate_connection(c, 1);
243 if(c->status.remove) {
244 logger(DEBUG_ALWAYS, LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
245 c->name, c->hostname, c->status);
249 logger(DEBUG_CONNECTIONS, LOG_WARNING, _("Timeout from %s (%s) during authentication"),
250 c->name, c->hostname);
251 terminate_connection(c, 0);
258 check all connections to see if anything
259 happened on their sockets
261 static void check_network_activity(fd_set * f)
266 int len = sizeof(result);
271 if(FD_ISSET(device_fd, f)) {
272 if(!read_packet(&packet))
273 route_outgoing(&packet);
276 for(node = connection_tree->head; node; node = node->next) {
277 c = (connection_t *) node->data;
282 if(FD_ISSET(c->socket, f)) {
283 if(c->status.connecting) {
284 c->status.connecting = 0;
285 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
288 finish_connecting(c);
290 logger(DEBUG_CONNECTIONS, LOG_DEBUG,
291 _("Error while connecting to %s (%s): %s"),
292 c->name, c->hostname, strerror(result));
294 do_outgoing_connection(c);
299 if(receive_meta(c) < 0) {
300 terminate_connection(c, c->status.active);
306 for(i = 0; i < listen_sockets; i++) {
307 if(FD_ISSET(listen_socket[i].udp, f))
308 handle_incoming_vpn_data(listen_socket[i].udp);
310 if(FD_ISSET(listen_socket[i].tcp, f))
311 handle_new_meta_connection(listen_socket[i].tcp);
316 this is where it all happens...
323 time_t last_ping_check, last_config_check;
328 last_ping_check = now;
329 last_config_check = now;
335 tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
338 maxfd = build_fdset(&fset);
340 r = select(maxfd + 1, &fset, NULL, NULL, &tv);
343 if(errno != EINTR && errno != EAGAIN) {
344 logger(DEBUG_ALWAYS, LOG_ERR, _("Error while waiting for input: %s"),
354 check_network_activity(&fset);
361 /* Let's check if everybody is still alive */
363 if(last_ping_check + pingtimeout < now) {
364 check_dead_connections();
365 last_ping_check = now;
367 if(routing_mode == RMODE_SWITCH)
372 /* Should we regenerate our key? */
374 if(keyexpires < now) {
375 logger(DEBUG_STATUS, LOG_INFO, _("Regenerating symmetric key"));
377 RAND_pseudo_bytes(myself->key, myself->keylength);
378 EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, myself->key, myself->key + myself->cipher->key_len);
379 send_key_changed(broadcast, myself);
380 keyexpires = now + keylifetime;
385 while((event = get_expired_event())) {
386 event->handler(event->data);
391 logger(DEBUG_ALWAYS, LOG_INFO, _("Flushing event queue"));
393 while(event_tree->head) {
394 event = (event_t *) event_tree->head->data;
395 event->handler(event->data);
409 /* Reread our own configuration file */
411 exit_configuration(&config_tree);
412 init_configuration(&config_tree);
414 if(read_server_config()) {
415 logger(DEBUG_ALWAYS, LOG_ERR, _("Unable to reread configuration file, exitting."));
419 /* Close connections to hosts that have a changed or deleted host config file */
421 for(node = connection_tree->head; node; node = node->next) {
422 c = (connection_t *) node->data;
425 free(c->outgoing->name);
426 freeaddrinfo(c->outgoing->ai);
431 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
432 if(stat(fname, &s) || s.st_mtime > last_config_check)
433 terminate_connection(c, c->status.active);
437 last_config_check = now;
439 /* Try to make outgoing connections */
441 try_outgoing_connections();