X-Git-Url: https://www.tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fnet_setup.c;h=7b4c741f0fe55ce1bd5daefcf39e2d990fa8867e;hb=refs%2Fheads%2F1.1;hp=4e1f2b81fcfda99998be76b0d288df4dc080d24c;hpb=0871c3095151bce6a4031a2662aa51b7193b855c;p=tinc diff --git a/src/net_setup.c b/src/net_setup.c index 4e1f2b81..dd6c58f1 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -28,6 +28,7 @@ #include "connection.h" #include "compression.h" #include "control.h" +#include "crypto.h" #include "device.h" #include "digest.h" #include "ecdsa.h" @@ -44,12 +45,13 @@ #include "utils.h" #include "xalloc.h" #include "keys.h" +#include "sandbox.h" #ifdef HAVE_MINIUPNPC #include "upnp.h" #endif -char *myport; +ports_t myport; static io_t device_io; devops_t devops; bool device_standby = false; @@ -71,20 +73,20 @@ bool node_read_ecdsa_public_key(node_t *n) { return true; } - splay_tree_t *config_tree; FILE *fp; char *pubname = NULL; char *p; - init_configuration(&config_tree); + splay_tree_t config; + init_configuration(&config); - if(!read_host_config(config_tree, n->name, true)) { + if(!read_host_config(&config, n->name, true)) { goto exit; } /* First, check for simple Ed25519PublicKey statement */ - if(get_config_string(lookup_config(config_tree, "Ed25519PublicKey"), &p)) { + if(get_config_string(lookup_config(&config, "Ed25519PublicKey"), &p)) { n->ecdsa = ecdsa_set_base64_public_key(p); free(p); goto exit; @@ -92,7 +94,7 @@ bool node_read_ecdsa_public_key(node_t *n) { /* Else, check for Ed25519PublicKeyFile statement and read it */ - if(!get_config_string(lookup_config(config_tree, "Ed25519PublicKeyFile"), &pubname)) { + if(!get_config_string(lookup_config(&config, "Ed25519PublicKeyFile"), &pubname)) { xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name); } @@ -106,7 +108,7 @@ bool node_read_ecdsa_public_key(node_t *n) { fclose(fp); exit: - exit_configuration(&config_tree); + splay_empty_tree(&config); free(pubname); return n->ecdsa; } @@ -142,7 +144,7 @@ static timeout_t keyexpire_timeout; static void keyexpire_handler(void *data) { regenerate_key(); timeout_set(data, &(struct timeval) { - keylifetime, rand() % 100000 + keylifetime, jitter() }); } #endif @@ -151,7 +153,7 @@ void regenerate_key(void) { logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys"); send_key_changed(); - for splay_each(node_t, n, node_tree) { + for splay_each(node_t, n, &node_tree) { n->status.validkey_in = false; } } @@ -176,19 +178,18 @@ void load_all_nodes(void) { node_t *n = lookup_node(ent->d_name); - splay_tree_t *config_tree; - init_configuration(&config_tree); - read_config_options(config_tree, ent->d_name); - read_host_config(config_tree, ent->d_name, true); + splay_tree_t config; + init_configuration(&config); + read_config_options(&config, ent->d_name); + read_host_config(&config, ent->d_name, true); if(!n) { - n = new_node(); - n->name = xstrdup(ent->d_name); + n = new_node(ent->d_name); node_add(n); } if(strictsubnets) { - for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) { + for(config_t *cfg = lookup_config(&config, "Subnet"); cfg; cfg = lookup_config_next(&config, cfg)) { subnet_t *s, *s2; if(!get_config_subnet(cfg, &s)) { @@ -204,11 +205,11 @@ void load_all_nodes(void) { } } - if(lookup_config(config_tree, "Address")) { + if(lookup_config(&config, "Address")) { n->status.has_address = true; } - exit_configuration(&config_tree); + splay_empty_tree(&config); } closedir(dir); @@ -218,7 +219,7 @@ char *get_name(void) { char *name = NULL; char *returned_name; - get_config_string(lookup_config(config_tree, "Name"), &name); + get_config_string(lookup_config(&config_tree, "Name"), &name); if(!name) { return NULL; @@ -229,21 +230,35 @@ char *get_name(void) { return returned_name; } -bool setup_myself_reloadable(void) { - free(scriptinterpreter); - scriptinterpreter = NULL; +static void read_interpreter(void) { + char *interpreter = NULL; + get_config_string(lookup_config(&config_tree, "ScriptsInterpreter"), &interpreter); - get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter); + if(!interpreter || (sandbox_can(START_PROCESSES, AFTER_SANDBOX) && sandbox_can(USE_NEW_PATHS, AFTER_SANDBOX))) { + free(scriptinterpreter); + scriptinterpreter = interpreter; + return; + } + + if(!string_eq(interpreter, scriptinterpreter)) { + logger(DEBUG_ALWAYS, LOG_NOTICE, "Not changing ScriptsInterpreter because of sandbox."); + } + + free(interpreter); +} + +bool setup_myself_reloadable(void) { + read_interpreter(); free(scriptextension); - if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension)) { + if(!get_config_string(lookup_config(&config_tree, "ScriptsExtension"), &scriptextension)) { scriptextension = xstrdup(""); } char *proxy = NULL; - get_config_string(lookup_config(config_tree, "Proxy"), &proxy); + get_config_string(lookup_config(&config_tree, "Proxy"), &proxy); if(proxy) { char *space; @@ -263,10 +278,15 @@ bool setup_myself_reloadable(void) { } else if(!strcasecmp(proxy, "http")) { proxytype = PROXY_HTTP; } else if(!strcasecmp(proxy, "exec")) { - proxytype = PROXY_EXEC; + if(sandbox_can(START_PROCESSES, AFTER_SANDBOX)) { + proxytype = PROXY_EXEC; + } else { + logger(DEBUG_ALWAYS, LOG_ERR, "Cannot use exec proxies with current sandbox level."); + return false; + } } else { logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy); - free(proxy); + free_string(proxy); return false; } @@ -276,10 +296,10 @@ bool setup_myself_reloadable(void) { free(proxyport); proxyport = NULL; - free(proxyuser); + free_string(proxyuser); proxyuser = NULL; - free(proxypass); + free_string(proxypass); proxypass = NULL; switch(proxytype) { @@ -290,10 +310,14 @@ bool setup_myself_reloadable(void) { case PROXY_EXEC: if(!space || !*space) { logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!"); - free(proxy); + free_string(proxy); return false; } + if(!sandbox_can(USE_NEW_PATHS, AFTER_SANDBOX)) { + logger(DEBUG_ALWAYS, LOG_NOTICE, "Changed exec proxy may fail to work because of sandbox."); + } + proxyhost = xstrdup(space); break; @@ -311,7 +335,7 @@ bool setup_myself_reloadable(void) { logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!"); proxyport = NULL; proxyhost = NULL; - free(proxy); + free_string(proxy); return false; } @@ -337,16 +361,16 @@ bool setup_myself_reloadable(void) { break; } - free(proxy); + free_string(proxy); } bool choice; - if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice) { + if(get_config_bool(lookup_config(&config_tree, "IndirectData"), &choice) && choice) { myself->options |= OPTION_INDIRECT; } - if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice) { + if(get_config_bool(lookup_config(&config_tree, "TCPOnly"), &choice) && choice) { myself->options |= OPTION_TCPONLY; } @@ -354,20 +378,20 @@ bool setup_myself_reloadable(void) { myself->options |= OPTION_INDIRECT; } - get_config_bool(lookup_config(config_tree, "UDPDiscovery"), &udp_discovery); - get_config_int(lookup_config(config_tree, "UDPDiscoveryKeepaliveInterval"), &udp_discovery_keepalive_interval); - get_config_int(lookup_config(config_tree, "UDPDiscoveryInterval"), &udp_discovery_interval); - get_config_int(lookup_config(config_tree, "UDPDiscoveryTimeout"), &udp_discovery_timeout); + get_config_bool(lookup_config(&config_tree, "UDPDiscovery"), &udp_discovery); + get_config_int(lookup_config(&config_tree, "UDPDiscoveryKeepaliveInterval"), &udp_discovery_keepalive_interval); + get_config_int(lookup_config(&config_tree, "UDPDiscoveryInterval"), &udp_discovery_interval); + get_config_int(lookup_config(&config_tree, "UDPDiscoveryTimeout"), &udp_discovery_timeout); - get_config_int(lookup_config(config_tree, "MTUInfoInterval"), &mtu_info_interval); - get_config_int(lookup_config(config_tree, "UDPInfoInterval"), &udp_info_interval); + get_config_int(lookup_config(&config_tree, "MTUInfoInterval"), &mtu_info_interval); + get_config_int(lookup_config(&config_tree, "UDPInfoInterval"), &udp_info_interval); - get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly); - get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery); + get_config_bool(lookup_config(&config_tree, "DirectOnly"), &directonly); + get_config_bool(lookup_config(&config_tree, "LocalDiscovery"), &localdiscovery); char *rmode = NULL; - if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) { + if(get_config_string(lookup_config(&config_tree, "Mode"), &rmode)) { if(!strcasecmp(rmode, "router")) { routing_mode = RMODE_ROUTER; } else if(!strcasecmp(rmode, "switch")) { @@ -385,7 +409,7 @@ bool setup_myself_reloadable(void) { char *fmode = NULL; - if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) { + if(get_config_string(lookup_config(&config_tree, "Forwarding"), &fmode)) { if(!strcasecmp(fmode, "off")) { forwarding_mode = FMODE_OFF; } else if(!strcasecmp(fmode, "internal")) { @@ -402,25 +426,25 @@ bool setup_myself_reloadable(void) { } choice = !(myself->options & OPTION_TCPONLY); - get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice); + get_config_bool(lookup_config(&config_tree, "PMTUDiscovery"), &choice); if(choice) { myself->options |= OPTION_PMTU_DISCOVERY; } choice = true; - get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice); + get_config_bool(lookup_config(&config_tree, "ClampMSS"), &choice); if(choice) { myself->options |= OPTION_CLAMP_MSS; } - get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance); - get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl); + get_config_bool(lookup_config(&config_tree, "PriorityInheritance"), &priorityinheritance); + get_config_bool(lookup_config(&config_tree, "DecrementTTL"), &decrement_ttl); char *bmode = NULL; - if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) { + if(get_config_string(lookup_config(&config_tree, "Broadcast"), &bmode)) { if(!strcasecmp(bmode, "no")) { broadcast_mode = BMODE_NONE; } else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst")) { @@ -438,9 +462,9 @@ bool setup_myself_reloadable(void) { /* Delete all broadcast subnets before re-adding them */ - for splay_each(subnet_t, s, subnet_tree) { + for splay_each(subnet_t, s, &subnet_tree) { if(!s->owner) { - splay_delete_node(subnet_tree, node); + splay_delete_node(&subnet_tree, node); } } @@ -456,7 +480,7 @@ bool setup_myself_reloadable(void) { subnet_add(NULL, s); } - for(config_t *cfg = lookup_config(config_tree, "BroadcastSubnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) { + for(config_t *cfg = lookup_config(&config_tree, "BroadcastSubnet"); cfg; cfg = lookup_config_next(&config_tree, cfg)) { subnet_t *s; if(!get_config_subnet(cfg, &s)) { @@ -482,11 +506,11 @@ bool setup_myself_reloadable(void) { #endif - if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire)) { + if(!get_config_int(lookup_config(&config_tree, "MACExpire"), &macexpire)) { macexpire = 600; } - if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) { + if(get_config_int(lookup_config(&config_tree, "MaxTimeout"), &maxtimeout)) { if(maxtimeout <= 0) { logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!"); return false; @@ -497,7 +521,7 @@ bool setup_myself_reloadable(void) { char *afname = NULL; - if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) { + if(get_config_string(lookup_config(&config_tree, "AddressFamily"), &afname)) { if(!strcasecmp(afname, "IPv4")) { addressfamily = AF_INET; } else if(!strcasecmp(afname, "IPv6")) { @@ -513,19 +537,19 @@ bool setup_myself_reloadable(void) { free(afname); } - get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames); + get_config_bool(lookup_config(&config_tree, "Hostnames"), &hostnames); - if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime)) { + if(!get_config_int(lookup_config(&config_tree, "KeyExpire"), &keylifetime)) { keylifetime = 3600; } - if(!get_config_bool(lookup_config(config_tree, "AutoConnect"), &autoconnect)) { + if(!get_config_bool(lookup_config(&config_tree, "AutoConnect"), &autoconnect)) { autoconnect = true; } - get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers); + get_config_bool(lookup_config(&config_tree, "DisableBuggyPeers"), &disablebuggypeers); - if(!get_config_int(lookup_config(config_tree, "InvitationExpire"), &invitation_lifetime)) { + if(!get_config_int(lookup_config(&config_tree, "InvitationExpire"), &invitation_lifetime)) { invitation_lifetime = 604800; // 1 week } @@ -534,11 +558,70 @@ bool setup_myself_reloadable(void) { return true; } +// Get the port that `from_fd` is listening on, and assign it to +// `sa` if `sa` has a dynamically allocated (zero) port. +static bool assign_static_port(sockaddr_t *sa, int from_fd) { + // We cannot get a port from a bad FD. Bail out. + if(from_fd <= 0) { + return false; + } + + int port = get_bound_port(from_fd); + + if(!port) { + return false; + } + + // If the port is non-zero, don't reassign it as it's already static. + switch(sa->sa.sa_family) { + case AF_INET: + if(!sa->in.sin_port) { + sa->in.sin_port = htons(port); + } + + return true; + + case AF_INET6: + if(!sa->in6.sin6_port) { + sa->in6.sin6_port = htons(port); + } + + return true; + + default: + logger(DEBUG_ALWAYS, LOG_ERR, "Unknown address family 0x%x", sa->sa.sa_family); + return false; + } +} + +typedef int (*bind_fn_t)(const sockaddr_t *); + +static int bind_reusing_port(const sockaddr_t *sa, int from_fd, bind_fn_t setup) { + sockaddr_t reuse_sa; + memcpy(&reuse_sa, sa, SALEN(sa->sa)); + + int fd = -1; + + // Check if the address we've been passed here is using port 0. + // If it is, try to get an actual port from an already bound socket, and reuse it here. + if(assign_static_port(&reuse_sa, from_fd)) { + fd = setup(&reuse_sa); + } + + // If we're binding to a hardcoded non-zero port, or no socket is listening yet, + // or binding failed, try the original address. + if(fd < 0) { + fd = setup(sa); + } + + return fd; +} + /* Add listening sockets. */ static bool add_listen_address(char *address, bool bindto) { - char *port = myport; + char *port = myport.tcp; if(address) { char *space = strchr(address, ' '); @@ -591,35 +674,51 @@ static bool add_listen_address(char *address, bool bindto) { } if(listen_sockets >= MAXSOCKETS) { + listen_sockets = MAXSOCKETS; logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets"); freeaddrinfo(ai); return false; } - int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr); + const sockaddr_t *sa = (sockaddr_t *) aip->ai_addr; + int from_fd = listen_socket[0].tcp.fd; + + // If we're binding to a dynamically allocated (zero) port, try to get the actual + // port of the first TCP socket, and use it for this one. If that succeeds, our + // tincd instance will use the same port for all addresses it listens on. + int tcp_fd = bind_reusing_port(sa, from_fd, setup_listen_socket); if(tcp_fd < 0) { continue; } - int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr); + // If we just successfully bound the first socket, use it for the UDP procedure below. + // Otherwise, keep using the socket we've obtained from listen_socket[0]. + if(!from_fd) { + from_fd = tcp_fd; + } + + int udp_fd = bind_reusing_port(sa, from_fd, setup_vpn_in_socket); if(udp_fd < 0) { - close(tcp_fd); + closesocket(tcp_fd); continue; } - io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ); - io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ); + listen_socket_t *sock = &listen_socket[listen_sockets]; + io_add(&sock->tcp, handle_new_meta_connection, sock, tcp_fd, IO_READ); + io_add(&sock->udp, handle_incoming_vpn_data, sock, udp_fd, IO_READ); if(debug_level >= DEBUG_CONNECTIONS) { - char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr); - logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname); + int tcp_port = get_bound_port(tcp_fd); + char *hostname = NULL; + sockaddr2str(sa, &hostname, NULL); + logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s port %d", hostname, tcp_port); free(hostname); } - listen_socket[listen_sockets].bindto = bindto; - memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen); + sock->bindto = bindto; + memcpy(&sock->sa, aip->ai_addr, aip->ai_addrlen); listen_sockets++; } @@ -655,7 +754,7 @@ void device_disable(void) { Configure node_t myself and set up the local sockets (listen only) */ static bool setup_myself(void) { - char *name, *hostname, *type; + char *name, *type; char *address = NULL; bool port_specified = false; @@ -665,18 +764,19 @@ static bool setup_myself(void) { } myname = xstrdup(name); - myself = new_node(); + myself = new_node(name); myself->connection = new_connection(); - myself->name = name; - myself->connection->name = xstrdup(name); - read_host_config(config_tree, name, true); + myself->connection->name = name; + read_host_config(&config_tree, name, true); - if(!get_config_string(lookup_config(config_tree, "Port"), &myport)) { - myport = xstrdup("655"); + if(!get_config_string(lookup_config(&config_tree, "Port"), &myport.tcp)) { + myport.tcp = xstrdup("655"); } else { port_specified = true; } + myport.udp = xstrdup(myport.tcp); + myself->connection->options = 0; myself->connection->protocol_major = PROT_MAJOR; myself->connection->protocol_minor = PROT_MINOR; @@ -684,7 +784,7 @@ static bool setup_myself(void) { myself->options |= PROT_MINOR << 24; #ifdef DISABLE_LEGACY - myself->connection->ecdsa = read_ecdsa_private_key(config_tree, NULL); + myself->connection->ecdsa = read_ecdsa_private_key(&config_tree, NULL); experimental = myself->connection->ecdsa != NULL; if(!experimental) { @@ -694,8 +794,8 @@ static bool setup_myself(void) { #else - if(!get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental)) { - myself->connection->ecdsa = read_ecdsa_private_key(config_tree, NULL); + if(!get_config_bool(lookup_config(&config_tree, "ExperimentalProtocol"), &experimental)) { + myself->connection->ecdsa = read_ecdsa_private_key(&config_tree, NULL); experimental = myself->connection->ecdsa != NULL; if(!experimental) { @@ -703,7 +803,7 @@ static bool setup_myself(void) { } } else { if(experimental) { - myself->connection->ecdsa = read_ecdsa_private_key(config_tree, NULL); + myself->connection->ecdsa = read_ecdsa_private_key(&config_tree, NULL); if(!myself->connection->ecdsa) { return false; @@ -711,9 +811,11 @@ static bool setup_myself(void) { } } - myself->connection->rsa = read_rsa_private_key(config_tree, NULL); + rsa_t *rsa = read_rsa_private_key(&config_tree, NULL); - if(!myself->connection->rsa) { + if(rsa) { + myself->connection->legacy = new_legacy_ctx(rsa); + } else { if(experimental) { logger(DEBUG_ALWAYS, LOG_WARNING, "Support for legacy protocol disabled."); } else { @@ -725,24 +827,23 @@ static bool setup_myself(void) { #endif /* Ensure myport is numeric */ + if(!is_decimal(myport.tcp)) { + uint16_t port = service_to_port(myport.tcp); - if(!atoi(myport)) { - struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM); - sockaddr_t sa; - - if(!ai || !ai->ai_addr) { + if(!port) { return false; } - free(myport); - memcpy(&sa, ai->ai_addr, ai->ai_addrlen); - freeaddrinfo(ai); - sockaddr2str(&sa, NULL, &myport); + free(myport.tcp); + myport.tcp = int_to_str(port); + + free(myport.udp); + myport.udp = xstrdup(myport.tcp); } /* Read in all the subnets specified in the host configuration file */ - for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) { + for(config_t *cfg = lookup_config(&config_tree, "Subnet"); cfg; cfg = lookup_config_next(&config_tree, cfg)) { subnet_t *subnet; if(!get_config_subnet(cfg, &subnet)) { @@ -758,18 +859,18 @@ static bool setup_myself(void) { return false; } - get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets); - get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver); + get_config_bool(lookup_config(&config_tree, "StrictSubnets"), &strictsubnets); + get_config_bool(lookup_config(&config_tree, "TunnelServer"), &tunnelserver); strictsubnets |= tunnelserver; - if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) { + if(get_config_int(lookup_config(&config_tree, "MaxConnectionBurst"), &max_connection_burst)) { if(max_connection_burst <= 0) { logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!"); return false; } } - if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) { + if(get_config_int(lookup_config(&config_tree, "UDPRcvBuf"), &udp_rcvbuf)) { if(udp_rcvbuf < 0) { logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!"); return false; @@ -778,7 +879,7 @@ static bool setup_myself(void) { udp_rcvbuf_warnings = true; } - if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) { + if(get_config_int(lookup_config(&config_tree, "UDPSndBuf"), &udp_sndbuf)) { if(udp_sndbuf < 0) { logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!"); return false; @@ -787,7 +888,7 @@ static bool setup_myself(void) { udp_sndbuf_warnings = true; } - get_config_int(lookup_config(config_tree, "FWMark"), &fwmark); + get_config_int(lookup_config(&config_tree, "FWMark"), &fwmark); #ifndef SO_MARK if(fwmark) { @@ -799,7 +900,7 @@ static bool setup_myself(void) { int replaywin_int; - if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) { + if(get_config_int(lookup_config(&config_tree, "ReplayWindow"), &replaywin_int)) { if(replaywin_int < 0) { logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!"); return false; @@ -814,28 +915,34 @@ static bool setup_myself(void) { char *cipher; - if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher)) { + if(!get_config_string(lookup_config(&config_tree, "Cipher"), &cipher)) { cipher = xstrdup("aes-256-cbc"); } if(!strcasecmp(cipher, "none")) { myself->incipher = NULL; - } else if(!(myself->incipher = cipher_open_by_name(cipher))) { - logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!"); - free(cipher); - return false; + } else { + myself->incipher = cipher_alloc(); + + if(!cipher_open_by_name(myself->incipher, cipher)) { + logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!"); + cipher_free(myself->incipher); + myself->incipher = NULL; + free(cipher); + return false; + } } free(cipher); timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval) { - keylifetime, rand() % 100000 + keylifetime, jitter() }); /* Check if we want to use message authentication codes... */ int maclength = 4; - get_config_int(lookup_config(config_tree, "MACLength"), &maclength); + get_config_int(lookup_config(&config_tree, "MACLength"), &maclength); if(maclength < 0) { logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!"); @@ -844,23 +951,33 @@ static bool setup_myself(void) { char *digest; - if(!get_config_string(lookup_config(config_tree, "Digest"), &digest)) { + if(!get_config_string(lookup_config(&config_tree, "Digest"), &digest)) { digest = xstrdup("sha256"); } if(!strcasecmp(digest, "none")) { myself->indigest = NULL; - } else if(!(myself->indigest = digest_open_by_name(digest, maclength))) { - logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!"); - free(digest); - return false; + } else { + myself->indigest = digest_alloc(); + + if(!digest_open_by_name(myself->indigest, digest, maclength)) { + logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!"); + digest_free(myself->indigest); + myself->indigest = NULL; + free(digest); + return false; + } } free(digest); #endif /* Compression */ - if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) { + int incompression = 0; + + if(get_config_int(lookup_config(&config_tree, "Compression"), &incompression)) { + myself->incompression = incompression; + switch(myself->incompression) { case COMPRESS_LZ4: #ifdef HAVE_LZ4 @@ -910,8 +1027,6 @@ static bool setup_myself(void) { myself->incompression = COMPRESS_NONE; } - myself->connection->outcompression = COMPRESS_NONE; - /* Done */ myself->nexthop = myself; @@ -929,8 +1044,8 @@ static bool setup_myself(void) { devops = os_devops; - if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) { - if(!strcasecmp(type, "dummy")) { + if(get_config_string(lookup_config(&config_tree, "DeviceType"), &type)) { + if(!strcasecmp(type, DEVICE_DUMMY)) { devops = dummy_devops; } else if(!strcasecmp(type, "raw_socket")) { devops = raw_socket_devops; @@ -959,7 +1074,7 @@ static bool setup_myself(void) { free(type); } - get_config_bool(lookup_config(config_tree, "DeviceStandby"), &device_standby); + get_config_bool(lookup_config(&config_tree, "DeviceStandby"), &device_standby); if(!devops.setup()) { return false; @@ -971,30 +1086,34 @@ static bool setup_myself(void) { /* Open sockets */ - if(!do_detach && getenv("LISTEN_FDS")) { + const char *listen_fds = getenv("LISTEN_FDS"); + + if(!do_detach && listen_fds) { sockaddr_t sa; socklen_t salen; - listen_sockets = atoi(getenv("LISTEN_FDS")); + listen_sockets = atoi(listen_fds); #ifdef HAVE_UNSETENV unsetenv("LISTEN_FDS"); #endif if(listen_sockets > MAXSOCKETS) { + listen_sockets = MAXSOCKETS; logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets"); return false; } for(int i = 0; i < listen_sockets; i++) { + const int tcp_fd = i + 3; salen = sizeof(sa); - if(getsockname(i + 3, &sa.sa, &salen) < 0) { - logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(sockerrno)); + if(getsockname(tcp_fd, &sa.sa, &salen) < 0) { + logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", tcp_fd, sockstrerror(sockerrno)); return false; } #ifdef FD_CLOEXEC - fcntl(i + 3, F_SETFD, FD_CLOEXEC); + fcntl(tcp_fd, F_SETFD, FD_CLOEXEC); #endif int udp_fd = setup_vpn_in_socket(&sa); @@ -1003,11 +1122,11 @@ static bool setup_myself(void) { return false; } - io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ); + io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], tcp_fd, IO_READ); io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ); if(debug_level >= DEBUG_CONNECTIONS) { - hostname = sockaddr2hostname(&sa); + char *hostname = sockaddr2hostname(&sa); logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname); free(hostname); } @@ -1018,7 +1137,7 @@ static bool setup_myself(void) { listen_sockets = 0; int cfgs = 0; - for(config_t *cfg = lookup_config(config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) { + for(config_t *cfg = lookup_config(&config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(&config_tree, cfg)) { cfgs++; get_config_string(cfg, &address); @@ -1027,7 +1146,7 @@ static bool setup_myself(void) { } } - for(config_t *cfg = lookup_config(config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) { + for(config_t *cfg = lookup_config(&config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(&config_tree, cfg)) { cfgs++; get_config_string(cfg, &address); @@ -1049,25 +1168,23 @@ static bool setup_myself(void) { /* If no Port option was specified, set myport to the port used by the first listening socket. */ - if(!port_specified || atoi(myport) == 0) { - sockaddr_t sa; - socklen_t salen = sizeof(sa); + if(!port_specified || atoi(myport.tcp) == 0) { + listen_socket_t *sock = &listen_socket[0]; - if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) { - free(myport); - sockaddr2str(&sa, NULL, &myport); + uint16_t tcp = get_bound_port(sock->tcp.fd); + free(myport.tcp); + myport.tcp = int_to_str(tcp); - if(!myport) { - myport = xstrdup("655"); - } - } + uint16_t udp = get_bound_port(sock->udp.fd); + free(myport.udp); + myport.udp = int_to_str(udp); } - xasprintf(&myself->hostname, "MYSELF port %s", myport); + xasprintf(&myself->hostname, "MYSELF port %s", myport.tcp); myself->connection->hostname = xstrdup(myself->hostname); char *upnp = NULL; - get_config_string(lookup_config(config_tree, "UPnP"), &upnp); + get_config_string(lookup_config(&config_tree, "UPnP"), &upnp); bool upnp_tcp = false; bool upnp_udp = false; @@ -1102,11 +1219,8 @@ static bool setup_myself(void) { bool setup_network(void) { init_connections(); init_subnets(); - init_nodes(); - init_edges(); - init_requests(); - if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) { + if(get_config_int(lookup_config(&config_tree, "PingInterval"), &pinginterval)) { if(pinginterval < 1) { pinginterval = 86400; } @@ -1114,7 +1228,7 @@ bool setup_network(void) { pinginterval = 60; } - if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout)) { + if(!get_config_int(lookup_config(&config_tree, "PingTimeout"), &pingtimeout)) { pingtimeout = 5; } @@ -1122,7 +1236,7 @@ bool setup_network(void) { pingtimeout = pinginterval; } - if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize)) { + if(!get_config_int(lookup_config(&config_tree, "MaxOutputBufferSize"), &maxoutbufsize)) { maxoutbufsize = 10 * MTU; } @@ -1172,8 +1286,8 @@ void close_network_connections(void) { for(int i = 0; i < listen_sockets; i++) { io_del(&listen_socket[i].tcp); io_del(&listen_socket[i].udp); - close(listen_socket[i].tcp.fd); - close(listen_socket[i].udp.fd); + closesocket(listen_socket[i].tcp.fd); + closesocket(listen_socket[i].udp.fd); } exit_requests(); @@ -1186,7 +1300,8 @@ void close_network_connections(void) { device_disable(); } - free(myport); + free(myport.tcp); + free(myport.udp); if(device_fd >= 0) { io_del(&device_io);