3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2015 Guus Sliepen <guus@tinc-vpn.org>
5 2006 Scott Lamb <slamb@slamb.org>
6 2010 Brandon Black <blblack@gmail.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "connection.h"
48 static io_t device_io;
50 bool device_standby = false;
56 proxytype_t proxytype;
58 bool disablebuggypeers;
60 char *scriptinterpreter;
61 char *scriptextension;
63 bool node_read_ecdsa_public_key(node_t *n) {
64 if(ecdsa_active(n->ecdsa))
67 splay_tree_t *config_tree;
72 init_configuration(&config_tree);
73 if(!read_host_config(config_tree, n->name))
76 /* First, check for simple Ed25519PublicKey statement */
78 if(get_config_string(lookup_config(config_tree, "Ed25519PublicKey"), &p)) {
79 n->ecdsa = ecdsa_set_base64_public_key(p);
84 /* Else, check for Ed25519PublicKeyFile statement and read it */
86 if(!get_config_string(lookup_config(config_tree, "Ed25519PublicKeyFile"), &pubname))
87 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
89 fp = fopen(pubname, "r");
94 n->ecdsa = ecdsa_read_pem_public_key(fp);
98 exit_configuration(&config_tree);
103 bool read_ecdsa_public_key(connection_t *c) {
104 if(ecdsa_active(c->ecdsa))
111 if(!c->config_tree) {
112 init_configuration(&c->config_tree);
113 if(!read_host_config(c->config_tree, c->name))
117 /* First, check for simple Ed25519PublicKey statement */
119 if(get_config_string(lookup_config(c->config_tree, "Ed25519PublicKey"), &p)) {
120 c->ecdsa = ecdsa_set_base64_public_key(p);
125 /* Else, check for Ed25519PublicKeyFile statement and read it */
127 if(!get_config_string(lookup_config(c->config_tree, "Ed25519PublicKeyFile"), &fname))
128 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
130 fp = fopen(fname, "r");
133 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading Ed25519 public key file `%s': %s",
134 fname, strerror(errno));
139 c->ecdsa = ecdsa_read_pem_public_key(fp);
141 if(!c->ecdsa && errno != ENOENT)
142 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing Ed25519 public key file `%s' failed.", fname);
149 #ifndef DISABLE_LEGACY
150 bool read_rsa_public_key(connection_t *c) {
155 /* First, check for simple PublicKey statement */
157 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
158 c->rsa = rsa_set_hex_public_key(n, "FFFF");
163 /* Else, check for PublicKeyFile statement and read it */
165 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
166 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
168 fp = fopen(fname, "r");
171 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
176 c->rsa = rsa_read_pem_public_key(fp);
180 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
186 static bool read_ecdsa_private_key(void) {
190 /* Check for PrivateKeyFile statement and read it */
192 if(!get_config_string(lookup_config(config_tree, "Ed25519PrivateKeyFile"), &fname))
193 xasprintf(&fname, "%s" SLASH "ed25519_key.priv", confbase);
195 fp = fopen(fname, "r");
198 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading Ed25519 private key file `%s': %s", fname, strerror(errno));
200 logger(DEBUG_ALWAYS, LOG_INFO, "Create an Ed25519 keypair with `tinc -n %s generate-ed25519-keys'.", netname ?: ".");
205 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
208 if(fstat(fileno(fp), &s)) {
209 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat Ed25519 private key file `%s': %s'", fname, strerror(errno));
214 if(s.st_mode & ~0100700)
215 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for Ed25519 private key file `%s'!", fname);
218 myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
221 if(!myself->connection->ecdsa)
222 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
224 return myself->connection->ecdsa;
227 static bool read_invitation_key(void) {
229 char fname[PATH_MAX];
232 ecdsa_free(invitation_key);
233 invitation_key = NULL;
236 snprintf(fname, sizeof fname, "%s" SLASH "invitations" SLASH "ed25519_key.priv", confbase);
238 fp = fopen(fname, "r");
241 invitation_key = ecdsa_read_pem_private_key(fp);
244 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
247 return invitation_key;
250 #ifndef DISABLE_LEGACY
251 static bool read_rsa_private_key(void) {
256 /* First, check for simple PrivateKey statement */
258 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
259 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
260 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
264 myself->connection->rsa = rsa_set_hex_private_key(n, "FFFF", d);
267 return myself->connection->rsa;
270 /* Else, check for PrivateKeyFile statement and read it */
272 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
273 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
275 fp = fopen(fname, "r");
278 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
279 fname, strerror(errno));
281 logger(DEBUG_ALWAYS, LOG_INFO, "Create an RSA keypair with `tinc -n %s generate-rsa-keys'.", netname ?: ".");
286 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
289 if(fstat(fileno(fp), &s)) {
290 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
295 if(s.st_mode & ~0100700)
296 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
299 myself->connection->rsa = rsa_read_pem_private_key(fp);
302 if(!myself->connection->rsa)
303 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
305 return myself->connection->rsa;
309 static timeout_t keyexpire_timeout;
311 static void keyexpire_handler(void *data) {
313 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
316 void regenerate_key(void) {
317 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
319 for splay_each(node_t, n, node_tree)
320 n->status.validkey_in = false;
324 Read Subnets from all host config files
326 void load_all_subnets(void) {
329 char dname[PATH_MAX];
331 snprintf(dname, sizeof dname, "%s" SLASH "hosts", confbase);
332 dir = opendir(dname);
334 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
338 while((ent = readdir(dir))) {
339 if(!check_id(ent->d_name))
342 node_t *n = lookup_node(ent->d_name);
343 #ifdef _DIRENT_HAVE_D_TYPE
344 //if(ent->d_type != DT_REG)
348 splay_tree_t *config_tree;
349 init_configuration(&config_tree);
350 read_config_options(config_tree, ent->d_name);
351 read_host_config(config_tree, ent->d_name);
355 n->name = xstrdup(ent->d_name);
359 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
362 if(!get_config_subnet(cfg, &s))
365 if((s2 = lookup_subnet(n, s))) {
373 exit_configuration(&config_tree);
379 void load_all_nodes(void) {
382 char dname[PATH_MAX];
384 snprintf(dname, sizeof dname, "%s" SLASH "hosts", confbase);
385 dir = opendir(dname);
387 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
391 while((ent = readdir(dir))) {
392 if(!check_id(ent->d_name))
395 node_t *n = lookup_node(ent->d_name);
400 n->name = xstrdup(ent->d_name);
408 char *get_name(void) {
412 get_config_string(lookup_config(config_tree, "Name"), &name);
417 returned_name = replace_name(name);
419 return returned_name;
422 bool setup_myself_reloadable(void) {
431 free(scriptinterpreter);
432 scriptinterpreter = NULL;
433 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
436 free(scriptextension);
437 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
438 scriptextension = xstrdup("");
440 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
442 if((space = strchr(proxy, ' ')))
445 if(!strcasecmp(proxy, "none")) {
446 proxytype = PROXY_NONE;
447 } else if(!strcasecmp(proxy, "socks4")) {
448 proxytype = PROXY_SOCKS4;
449 } else if(!strcasecmp(proxy, "socks4a")) {
450 proxytype = PROXY_SOCKS4A;
451 } else if(!strcasecmp(proxy, "socks5")) {
452 proxytype = PROXY_SOCKS5;
453 } else if(!strcasecmp(proxy, "http")) {
454 proxytype = PROXY_HTTP;
455 } else if(!strcasecmp(proxy, "exec")) {
456 proxytype = PROXY_EXEC;
458 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
468 if(!space || !*space) {
469 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
472 proxyhost = xstrdup(space);
480 if(space && (space = strchr(space, ' ')))
481 *space++ = 0, proxyport = space;
482 if(space && (space = strchr(space, ' ')))
483 *space++ = 0, proxyuser = space;
484 if(space && (space = strchr(space, ' ')))
485 *space++ = 0, proxypass = space;
486 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
487 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
490 proxyhost = xstrdup(proxyhost);
491 proxyport = xstrdup(proxyport);
492 if(proxyuser && *proxyuser)
493 proxyuser = xstrdup(proxyuser);
494 if(proxypass && *proxypass)
495 proxypass = xstrdup(proxypass);
502 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
503 myself->options |= OPTION_INDIRECT;
505 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
506 myself->options |= OPTION_TCPONLY;
508 if(myself->options & OPTION_TCPONLY)
509 myself->options |= OPTION_INDIRECT;
511 get_config_bool(lookup_config(config_tree, "UDPDiscovery"), &udp_discovery);
512 get_config_int(lookup_config(config_tree, "UDPDiscoveryKeepaliveInterval"), &udp_discovery_keepalive_interval);
513 get_config_int(lookup_config(config_tree, "UDPDiscoveryInterval"), &udp_discovery_interval);
514 get_config_int(lookup_config(config_tree, "UDPDiscoveryTimeout"), &udp_discovery_timeout);
516 get_config_int(lookup_config(config_tree, "MTUInfoInterval"), &mtu_info_interval);
517 get_config_int(lookup_config(config_tree, "UDPInfoInterval"), &udp_info_interval);
519 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
520 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
522 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
523 if(!strcasecmp(rmode, "router"))
524 routing_mode = RMODE_ROUTER;
525 else if(!strcasecmp(rmode, "switch"))
526 routing_mode = RMODE_SWITCH;
527 else if(!strcasecmp(rmode, "hub"))
528 routing_mode = RMODE_HUB;
530 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
536 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
537 if(!strcasecmp(fmode, "off"))
538 forwarding_mode = FMODE_OFF;
539 else if(!strcasecmp(fmode, "internal"))
540 forwarding_mode = FMODE_INTERNAL;
541 else if(!strcasecmp(fmode, "kernel"))
542 forwarding_mode = FMODE_KERNEL;
544 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
551 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
553 myself->options |= OPTION_PMTU_DISCOVERY;
556 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
558 myself->options |= OPTION_CLAMP_MSS;
560 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
561 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
562 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
563 if(!strcasecmp(bmode, "no"))
564 broadcast_mode = BMODE_NONE;
565 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
566 broadcast_mode = BMODE_MST;
567 else if(!strcasecmp(bmode, "direct"))
568 broadcast_mode = BMODE_DIRECT;
570 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
576 const char* const DEFAULT_BROADCAST_SUBNETS[] = { "ff:ff:ff:ff:ff:ff", "255.255.255.255", "224.0.0.0/4", "ff00::/8" };
577 for (size_t i = 0; i < sizeof(DEFAULT_BROADCAST_SUBNETS) / sizeof(*DEFAULT_BROADCAST_SUBNETS); i++) {
578 subnet_t *s = new_subnet();
579 if (!str2net(s, DEFAULT_BROADCAST_SUBNETS[i]))
583 for (config_t* cfg = lookup_config(config_tree, "BroadcastSubnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
585 if (!get_config_subnet(cfg, &s))
590 #if !defined(SOL_IP) || !defined(IP_TOS)
591 if(priorityinheritance)
592 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
595 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
598 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
599 if(maxtimeout <= 0) {
600 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
606 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
607 if(!strcasecmp(afname, "IPv4"))
608 addressfamily = AF_INET;
609 else if(!strcasecmp(afname, "IPv6"))
610 addressfamily = AF_INET6;
611 else if(!strcasecmp(afname, "any"))
612 addressfamily = AF_UNSPEC;
614 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
620 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
622 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
625 config_t *cfg = lookup_config(config_tree, "AutoConnect");
627 if(!get_config_bool(cfg, &autoconnect)) {
628 // Some backwards compatibility with when this option was an int
630 get_config_int(cfg, &val);
635 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
637 read_invitation_key();
643 Add listening sockets.
645 static bool add_listen_address(char *address, bool bindto) {
649 char *space = strchr(address, ' ');
655 if(!strcmp(address, "*"))
659 struct addrinfo *ai, hint = {0};
660 hint.ai_family = addressfamily;
661 hint.ai_socktype = SOCK_STREAM;
662 hint.ai_protocol = IPPROTO_TCP;
663 hint.ai_flags = AI_PASSIVE;
665 #if HAVE_DECL_RES_INIT
668 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
672 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
676 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
677 // Ignore duplicate addresses
680 for(int i = 0; i < listen_sockets; i++)
681 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
689 if(listen_sockets >= MAXSOCKETS) {
690 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
694 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
699 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
706 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
707 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
709 if(debug_level >= DEBUG_CONNECTIONS) {
710 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
711 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
715 listen_socket[listen_sockets].bindto = bindto;
716 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
724 void device_enable(void) {
728 /* Run tinc-up script to further initialize the tap interface */
730 char *envp[5] = {NULL};
731 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
732 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
733 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
734 xasprintf(&envp[3], "NAME=%s", myname);
736 execute_script("tinc-up", envp);
738 for(int i = 0; i < 4; i++)
742 void device_disable(void) {
743 char *envp[5] = {NULL};
744 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
745 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
746 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
747 xasprintf(&envp[3], "NAME=%s", myname);
749 execute_script("tinc-down", envp);
751 for(int i = 0; i < 4; i++)
759 Configure node_t myself and set up the local sockets (listen only)
761 static bool setup_myself(void) {
762 char *name, *hostname, *cipher, *digest, *type;
763 char *address = NULL;
764 bool port_specified = false;
766 if(!(name = get_name())) {
767 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
771 myname = xstrdup(name);
773 myself->connection = new_connection();
775 myself->connection->name = xstrdup(name);
776 read_host_config(config_tree, name);
778 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
779 myport = xstrdup("655");
781 port_specified = true;
783 myself->connection->options = 0;
784 myself->connection->protocol_major = PROT_MAJOR;
785 myself->connection->protocol_minor = PROT_MINOR;
787 myself->options |= PROT_MINOR << 24;
789 #ifdef DISABLE_LEGACY
790 experimental = read_ecdsa_private_key();
792 logger(DEBUG_ALWAYS, LOG_ERR, "No private key available, cannot start tinc!");
796 if(!get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental)) {
797 experimental = read_ecdsa_private_key();
799 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for SPTPS disabled.");
801 if(experimental && !read_ecdsa_private_key())
805 if(!read_rsa_private_key()) {
807 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for legacy protocol disabled.");
809 logger(DEBUG_ALWAYS, LOG_ERR, "No private keys available, cannot start tinc!");
815 /* Ensure myport is numeric */
818 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
820 if(!ai || !ai->ai_addr)
823 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
824 sockaddr2str(&sa, NULL, &myport);
827 /* Read in all the subnets specified in the host configuration file */
829 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
832 if(!get_config_subnet(cfg, &subnet))
835 subnet_add(myself, subnet);
838 /* Check some options */
840 if(!setup_myself_reloadable())
843 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
844 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
845 strictsubnets |= tunnelserver;
847 if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
848 if(max_connection_burst <= 0) {
849 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
854 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
856 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
861 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
863 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
869 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
870 if(replaywin_int < 0) {
871 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
874 replaywin = (unsigned)replaywin_int;
875 sptps_replaywin = replaywin;
878 #ifndef DISABLE_LEGACY
879 /* Generate packet encryption key */
881 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
882 cipher = xstrdup("blowfish");
884 if(!strcasecmp(cipher, "none")) {
885 myself->incipher = NULL;
886 } else if(!(myself->incipher = cipher_open_by_name(cipher))) {
887 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
893 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
895 /* Check if we want to use message authentication codes... */
898 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
901 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
905 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
906 digest = xstrdup("sha1");
908 if(!strcasecmp(digest, "none")) {
909 myself->indigest = NULL;
910 } else if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
911 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
920 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
921 if(myself->incompression < 0 || myself->incompression > 11) {
922 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
926 myself->incompression = 0;
928 myself->connection->outcompression = 0;
932 myself->nexthop = myself;
933 myself->via = myself;
934 myself->status.reachable = true;
935 myself->last_state_change = now.tv_sec;
936 myself->status.sptps = experimental;
950 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
951 if(!strcasecmp(type, "dummy"))
952 devops = dummy_devops;
953 else if(!strcasecmp(type, "raw_socket"))
954 devops = raw_socket_devops;
955 else if(!strcasecmp(type, "multicast"))
956 devops = multicast_devops;
958 else if(!strcasecmp(type, "uml"))
962 else if(!strcasecmp(type, "vde"))
968 get_config_bool(lookup_config(config_tree, "DeviceStandby"), &device_standby);
974 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
978 if(!do_detach && getenv("LISTEN_FDS")) {
982 listen_sockets = atoi(getenv("LISTEN_FDS"));
984 unsetenv("LISTEN_FDS");
987 if(listen_sockets > MAXSOCKETS) {
988 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
992 for(int i = 0; i < listen_sockets; i++) {
994 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
995 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(sockerrno));
1000 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
1003 int udp_fd = setup_vpn_in_socket(&sa);
1007 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
1008 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
1010 if(debug_level >= DEBUG_CONNECTIONS) {
1011 hostname = sockaddr2hostname(&sa);
1012 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
1016 memcpy(&listen_socket[i].sa, &sa, salen);
1022 for(config_t *cfg = lookup_config(config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1024 get_config_string(cfg, &address);
1025 if(!add_listen_address(address, true))
1029 for(config_t *cfg = lookup_config(config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1031 get_config_string(cfg, &address);
1032 if(!add_listen_address(address, false))
1037 if(!add_listen_address(address, NULL))
1041 if(!listen_sockets) {
1042 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
1046 /* If no Port option was specified, set myport to the port used by the first listening socket. */
1048 if(!port_specified || atoi(myport) == 0) {
1050 socklen_t salen = sizeof sa;
1051 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
1053 sockaddr2str(&sa, NULL, &myport);
1055 myport = xstrdup("655");
1059 xasprintf(&myself->hostname, "MYSELF port %s", myport);
1060 myself->connection->hostname = xstrdup(myself->hostname);
1064 last_config_check = now.tv_sec;
1072 bool setup_network(void) {
1079 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
1080 if(pinginterval < 1) {
1081 pinginterval = 86400;
1086 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
1088 if(pingtimeout < 1 || pingtimeout > pinginterval)
1089 pingtimeout = pinginterval;
1091 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
1092 maxoutbufsize = 10 * MTU;
1100 if (!device_standby)
1103 /* Run subnet-up scripts for our own subnets */
1105 subnet_update(myself, NULL, true);
1111 close all open network connections
1113 void close_network_connections(void) {
1114 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1116 connection_t *c = node->data;
1117 /* Keep control connections open until the end, so they know when we really terminated */
1118 if(c->status.control)
1121 terminate_connection(c, false);
1125 list_delete_list(outgoing_list);
1127 if(myself && myself->connection) {
1128 subnet_update(myself, NULL, false);
1129 terminate_connection(myself->connection, false);
1130 free_connection(myself->connection);
1133 for(int i = 0; i < listen_sockets; i++) {
1134 io_del(&listen_socket[i].tcp);
1135 io_del(&listen_socket[i].udp);
1136 close(listen_socket[i].tcp.fd);
1137 close(listen_socket[i].udp.fd);
1146 if (!device_standby)
1159 free(scriptextension);
1160 free(scriptinterpreter);