3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2017 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"
51 static io_t device_io;
53 bool device_standby = false;
59 proxytype_t proxytype;
61 bool disablebuggypeers;
63 char *scriptinterpreter;
64 char *scriptextension;
66 bool node_read_ecdsa_public_key(node_t *n) {
67 if(ecdsa_active(n->ecdsa)) {
71 splay_tree_t *config_tree;
76 init_configuration(&config_tree);
78 if(!read_host_config(config_tree, n->name)) {
82 /* First, check for simple Ed25519PublicKey statement */
84 if(get_config_string(lookup_config(config_tree, "Ed25519PublicKey"), &p)) {
85 n->ecdsa = ecdsa_set_base64_public_key(p);
90 /* Else, check for Ed25519PublicKeyFile statement and read it */
92 if(!get_config_string(lookup_config(config_tree, "Ed25519PublicKeyFile"), &pubname)) {
93 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
96 fp = fopen(pubname, "r");
102 n->ecdsa = ecdsa_read_pem_public_key(fp);
106 exit_configuration(&config_tree);
111 bool read_ecdsa_public_key(connection_t *c) {
112 if(ecdsa_active(c->ecdsa)) {
120 if(!c->config_tree) {
121 init_configuration(&c->config_tree);
123 if(!read_host_config(c->config_tree, c->name)) {
128 /* First, check for simple Ed25519PublicKey statement */
130 if(get_config_string(lookup_config(c->config_tree, "Ed25519PublicKey"), &p)) {
131 c->ecdsa = ecdsa_set_base64_public_key(p);
136 /* Else, check for Ed25519PublicKeyFile statement and read it */
138 if(!get_config_string(lookup_config(c->config_tree, "Ed25519PublicKeyFile"), &fname)) {
139 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
142 fp = fopen(fname, "r");
145 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading Ed25519 public key file `%s': %s",
146 fname, strerror(errno));
151 c->ecdsa = ecdsa_read_pem_public_key(fp);
153 if(!c->ecdsa && errno != ENOENT) {
154 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing Ed25519 public key file `%s' failed.", fname);
162 #ifndef DISABLE_LEGACY
163 bool read_rsa_public_key(connection_t *c) {
168 /* First, check for simple PublicKey statement */
170 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
171 c->rsa = rsa_set_hex_public_key(n, "FFFF");
176 /* Else, check for PublicKeyFile statement and read it */
178 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname)) {
179 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
182 fp = fopen(fname, "r");
185 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
190 c->rsa = rsa_read_pem_public_key(fp);
194 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
202 static bool read_ecdsa_private_key(void) {
206 /* Check for PrivateKeyFile statement and read it */
208 if(!get_config_string(lookup_config(config_tree, "Ed25519PrivateKeyFile"), &fname)) {
209 xasprintf(&fname, "%s" SLASH "ed25519_key.priv", confbase);
212 fp = fopen(fname, "r");
215 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading Ed25519 private key file `%s': %s", fname, strerror(errno));
217 if(errno == ENOENT) {
218 logger(DEBUG_ALWAYS, LOG_INFO, "Create an Ed25519 keypair with `tinc -n %s generate-ed25519-keys'.", netname ? : ".");
225 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
228 if(fstat(fileno(fp), &s)) {
229 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat Ed25519 private key file `%s': %s'", fname, strerror(errno));
234 if(s.st_mode & ~0100700) {
235 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for Ed25519 private key file `%s'!", fname);
240 myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
243 if(!myself->connection->ecdsa) {
244 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
248 return myself->connection->ecdsa;
251 static bool read_invitation_key(void) {
253 char fname[PATH_MAX];
256 ecdsa_free(invitation_key);
257 invitation_key = NULL;
260 snprintf(fname, sizeof(fname), "%s" SLASH "invitations" SLASH "ed25519_key.priv", confbase);
262 fp = fopen(fname, "r");
265 invitation_key = ecdsa_read_pem_private_key(fp);
268 if(!invitation_key) {
269 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
273 return invitation_key;
276 #ifndef DISABLE_LEGACY
277 static bool read_rsa_private_key(void) {
282 /* First, check for simple PrivateKey statement */
284 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
285 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
286 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
291 myself->connection->rsa = rsa_set_hex_private_key(n, "FFFF", d);
294 return myself->connection->rsa;
297 /* Else, check for PrivateKeyFile statement and read it */
299 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname)) {
300 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
303 fp = fopen(fname, "r");
306 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
307 fname, strerror(errno));
309 if(errno == ENOENT) {
310 logger(DEBUG_ALWAYS, LOG_INFO, "Create an RSA keypair with `tinc -n %s generate-rsa-keys'.", netname ? : ".");
317 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
320 if(fstat(fileno(fp), &s)) {
321 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
326 if(s.st_mode & ~0100700) {
327 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
332 myself->connection->rsa = rsa_read_pem_private_key(fp);
335 if(!myself->connection->rsa) {
336 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
340 return myself->connection->rsa;
344 static timeout_t keyexpire_timeout;
346 static void keyexpire_handler(void *data) {
348 timeout_set(data, &(struct timeval) {
349 keylifetime, rand() % 100000
353 void regenerate_key(void) {
354 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
357 for splay_each(node_t, n, node_tree) {
358 n->status.validkey_in = false;
362 void load_all_nodes(void) {
365 char dname[PATH_MAX];
367 snprintf(dname, sizeof(dname), "%s" SLASH "hosts", confbase);
368 dir = opendir(dname);
371 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
375 while((ent = readdir(dir))) {
376 if(!check_id(ent->d_name)) {
380 node_t *n = lookup_node(ent->d_name);
382 splay_tree_t *config_tree;
383 init_configuration(&config_tree);
384 read_config_options(config_tree, ent->d_name);
385 read_host_config(config_tree, ent->d_name);
389 n->name = xstrdup(ent->d_name);
394 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
397 if(!get_config_subnet(cfg, &s)) {
401 if((s2 = lookup_subnet(n, s))) {
410 if(lookup_config(config_tree, "Address")) {
411 n->status.has_address = true;
414 exit_configuration(&config_tree);
420 char *get_name(void) {
424 get_config_string(lookup_config(config_tree, "Name"), &name);
430 returned_name = replace_name(name);
432 return returned_name;
435 bool setup_myself_reloadable(void) {
444 free(scriptinterpreter);
445 scriptinterpreter = NULL;
446 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
449 free(scriptextension);
451 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension)) {
452 scriptextension = xstrdup("");
455 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
458 if((space = strchr(proxy, ' '))) {
462 if(!strcasecmp(proxy, "none")) {
463 proxytype = PROXY_NONE;
464 } else if(!strcasecmp(proxy, "socks4")) {
465 proxytype = PROXY_SOCKS4;
466 } else if(!strcasecmp(proxy, "socks4a")) {
467 proxytype = PROXY_SOCKS4A;
468 } else if(!strcasecmp(proxy, "socks5")) {
469 proxytype = PROXY_SOCKS5;
470 } else if(!strcasecmp(proxy, "http")) {
471 proxytype = PROXY_HTTP;
472 } else if(!strcasecmp(proxy, "exec")) {
473 proxytype = PROXY_EXEC;
475 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
485 if(!space || !*space) {
486 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
490 proxyhost = xstrdup(space);
499 if(space && (space = strchr(space, ' '))) {
500 *space++ = 0, proxyport = space;
503 if(space && (space = strchr(space, ' '))) {
504 *space++ = 0, proxyuser = space;
507 if(space && (space = strchr(space, ' '))) {
508 *space++ = 0, proxypass = space;
511 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
512 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
516 proxyhost = xstrdup(proxyhost);
517 proxyport = xstrdup(proxyport);
519 if(proxyuser && *proxyuser) {
520 proxyuser = xstrdup(proxyuser);
523 if(proxypass && *proxypass) {
524 proxypass = xstrdup(proxypass);
533 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice) {
534 myself->options |= OPTION_INDIRECT;
537 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice) {
538 myself->options |= OPTION_TCPONLY;
541 if(myself->options & OPTION_TCPONLY) {
542 myself->options |= OPTION_INDIRECT;
545 get_config_bool(lookup_config(config_tree, "UDPDiscovery"), &udp_discovery);
546 get_config_int(lookup_config(config_tree, "UDPDiscoveryKeepaliveInterval"), &udp_discovery_keepalive_interval);
547 get_config_int(lookup_config(config_tree, "UDPDiscoveryInterval"), &udp_discovery_interval);
548 get_config_int(lookup_config(config_tree, "UDPDiscoveryTimeout"), &udp_discovery_timeout);
550 get_config_int(lookup_config(config_tree, "MTUInfoInterval"), &mtu_info_interval);
551 get_config_int(lookup_config(config_tree, "UDPInfoInterval"), &udp_info_interval);
553 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
554 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
556 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
557 if(!strcasecmp(rmode, "router")) {
558 routing_mode = RMODE_ROUTER;
559 } else if(!strcasecmp(rmode, "switch")) {
560 routing_mode = RMODE_SWITCH;
561 } else if(!strcasecmp(rmode, "hub")) {
562 routing_mode = RMODE_HUB;
564 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
571 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
572 if(!strcasecmp(fmode, "off")) {
573 forwarding_mode = FMODE_OFF;
574 } else if(!strcasecmp(fmode, "internal")) {
575 forwarding_mode = FMODE_INTERNAL;
576 } else if(!strcasecmp(fmode, "kernel")) {
577 forwarding_mode = FMODE_KERNEL;
579 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
586 choice = !(myself->options & OPTION_TCPONLY);
587 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
590 myself->options |= OPTION_PMTU_DISCOVERY;
594 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
597 myself->options |= OPTION_CLAMP_MSS;
600 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
601 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
603 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
604 if(!strcasecmp(bmode, "no")) {
605 broadcast_mode = BMODE_NONE;
606 } else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst")) {
607 broadcast_mode = BMODE_MST;
608 } else if(!strcasecmp(bmode, "direct")) {
609 broadcast_mode = BMODE_DIRECT;
611 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
618 const char *const DEFAULT_BROADCAST_SUBNETS[] = { "ff:ff:ff:ff:ff:ff", "255.255.255.255", "224.0.0.0/4", "ff00::/8" };
620 for(size_t i = 0; i < sizeof(DEFAULT_BROADCAST_SUBNETS) / sizeof(*DEFAULT_BROADCAST_SUBNETS); i++) {
621 subnet_t *s = new_subnet();
623 if(!str2net(s, DEFAULT_BROADCAST_SUBNETS[i])) {
630 for(config_t *cfg = lookup_config(config_tree, "BroadcastSubnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
633 if(!get_config_subnet(cfg, &s)) {
640 #if !defined(IPPROTO_IP) || !defined(IP_TOS)
642 if(priorityinheritance) {
643 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform for IPv4 connections", "PriorityInheritance");
648 #if !defined(IPPROTO_IPV6) || !defined(IPV6_TCLASS)
650 if(priorityinheritance) {
651 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform for IPv6 connections", "PriorityInheritance");
656 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire)) {
660 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
661 if(maxtimeout <= 0) {
662 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
669 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
670 if(!strcasecmp(afname, "IPv4")) {
671 addressfamily = AF_INET;
672 } else if(!strcasecmp(afname, "IPv6")) {
673 addressfamily = AF_INET6;
674 } else if(!strcasecmp(afname, "any")) {
675 addressfamily = AF_UNSPEC;
677 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
684 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
686 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime)) {
690 config_t *cfg = lookup_config(config_tree, "AutoConnect");
693 if(!get_config_bool(cfg, &autoconnect)) {
694 // Some backwards compatibility with when this option was an int
696 get_config_int(cfg, &val);
701 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
703 if(!get_config_int(lookup_config(config_tree, "InvitationExpire"), &invitation_lifetime)) {
704 invitation_lifetime = 604800; // 1 week
707 read_invitation_key();
713 Add listening sockets.
715 static bool add_listen_address(char *address, bool bindto) {
719 char *space = strchr(address, ' ');
726 if(!strcmp(address, "*")) {
731 struct addrinfo *ai, hint = {0};
733 hint.ai_family = addressfamily;
735 hint.ai_socktype = SOCK_STREAM;
737 hint.ai_protocol = IPPROTO_TCP;
739 hint.ai_flags = AI_PASSIVE;
741 #if HAVE_DECL_RES_INIT
745 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
750 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
754 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
755 // Ignore duplicate addresses
758 for(int i = 0; i < listen_sockets; i++)
759 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
768 if(listen_sockets >= MAXSOCKETS) {
769 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
773 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
779 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
786 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
787 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
789 if(debug_level >= DEBUG_CONNECTIONS) {
790 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
791 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
795 listen_socket[listen_sockets].bindto = bindto;
796 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
804 void device_enable(void) {
809 /* Run tinc-up script to further initialize the tap interface */
812 environment_init(&env);
813 execute_script("tinc-up", &env);
814 environment_exit(&env);
817 void device_disable(void) {
819 environment_init(&env);
820 execute_script("tinc-down", &env);
821 environment_exit(&env);
829 Configure node_t myself and set up the local sockets (listen only)
831 static bool setup_myself(void) {
832 char *name, *hostname, *cipher, *digest, *type;
833 char *address = NULL;
834 bool port_specified = false;
836 if(!(name = get_name())) {
837 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
841 myname = xstrdup(name);
843 myself->connection = new_connection();
845 myself->connection->name = xstrdup(name);
846 read_host_config(config_tree, name);
848 if(!get_config_string(lookup_config(config_tree, "Port"), &myport)) {
849 myport = xstrdup("655");
851 port_specified = true;
854 myself->connection->options = 0;
855 myself->connection->protocol_major = PROT_MAJOR;
856 myself->connection->protocol_minor = PROT_MINOR;
858 myself->options |= PROT_MINOR << 24;
860 #ifdef DISABLE_LEGACY
861 experimental = read_ecdsa_private_key();
864 logger(DEBUG_ALWAYS, LOG_ERR, "No private key available, cannot start tinc!");
870 if(!get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental)) {
871 experimental = read_ecdsa_private_key();
874 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for SPTPS disabled.");
877 if(experimental && !read_ecdsa_private_key()) {
882 if(!read_rsa_private_key()) {
884 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for legacy protocol disabled.");
886 logger(DEBUG_ALWAYS, LOG_ERR, "No private keys available, cannot start tinc!");
893 /* Ensure myport is numeric */
896 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
899 if(!ai || !ai->ai_addr) {
904 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
905 sockaddr2str(&sa, NULL, &myport);
908 /* Read in all the subnets specified in the host configuration file */
910 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
913 if(!get_config_subnet(cfg, &subnet)) {
917 subnet_add(myself, subnet);
920 /* Check some options */
922 if(!setup_myself_reloadable()) {
926 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
927 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
928 strictsubnets |= tunnelserver;
930 if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
931 if(max_connection_burst <= 0) {
932 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
937 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
939 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
944 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
946 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
953 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
954 if(replaywin_int < 0) {
955 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
959 replaywin = (unsigned)replaywin_int;
960 sptps_replaywin = replaywin;
963 #ifndef DISABLE_LEGACY
964 /* Generate packet encryption key */
966 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher)) {
967 cipher = xstrdup("aes-256-cbc");
970 if(!strcasecmp(cipher, "none")) {
971 myself->incipher = NULL;
972 } else if(!(myself->incipher = cipher_open_by_name(cipher))) {
973 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
979 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval) {
980 keylifetime, rand() % 100000
983 /* Check if we want to use message authentication codes... */
986 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
989 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
993 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest)) {
994 digest = xstrdup("sha256");
997 if(!strcasecmp(digest, "none")) {
998 myself->indigest = NULL;
999 } else if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
1000 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
1009 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
1010 if(myself->incompression < 0 || myself->incompression > 11) {
1011 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
1015 myself->incompression = 0;
1018 myself->connection->outcompression = 0;
1022 myself->nexthop = myself;
1023 myself->via = myself;
1024 myself->status.reachable = true;
1025 myself->last_state_change = now.tv_sec;
1026 myself->status.sptps = experimental;
1037 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
1038 if(!strcasecmp(type, "dummy")) {
1039 devops = dummy_devops;
1040 } else if(!strcasecmp(type, "raw_socket")) {
1041 devops = raw_socket_devops;
1042 } else if(!strcasecmp(type, "multicast")) {
1043 devops = multicast_devops;
1044 } else if(!strcasecmp(type, "fd")) {
1049 else if(!strcasecmp(type, "uml")) {
1050 devops = uml_devops;
1055 else if(!strcasecmp(type, "vde")) {
1056 devops = vde_devops;
1063 get_config_bool(lookup_config(config_tree, "DeviceStandby"), &device_standby);
1065 if(!devops.setup()) {
1069 if(device_fd >= 0) {
1070 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
1075 if(!do_detach && getenv("LISTEN_FDS")) {
1079 listen_sockets = atoi(getenv("LISTEN_FDS"));
1080 #ifdef HAVE_UNSETENV
1081 unsetenv("LISTEN_FDS");
1084 if(listen_sockets > MAXSOCKETS) {
1085 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
1089 for(int i = 0; i < listen_sockets; i++) {
1092 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
1093 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(sockerrno));
1098 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
1101 int udp_fd = setup_vpn_in_socket(&sa);
1107 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
1108 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
1110 if(debug_level >= DEBUG_CONNECTIONS) {
1111 hostname = sockaddr2hostname(&sa);
1112 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
1116 memcpy(&listen_socket[i].sa, &sa, salen);
1122 for(config_t *cfg = lookup_config(config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1124 get_config_string(cfg, &address);
1126 if(!add_listen_address(address, true)) {
1131 for(config_t *cfg = lookup_config(config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1133 get_config_string(cfg, &address);
1135 if(!add_listen_address(address, false)) {
1141 if(!add_listen_address(address, NULL)) {
1146 if(!listen_sockets) {
1147 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
1151 /* If no Port option was specified, set myport to the port used by the first listening socket. */
1153 if(!port_specified || atoi(myport) == 0) {
1155 socklen_t salen = sizeof(sa);
1157 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
1159 sockaddr2str(&sa, NULL, &myport);
1162 myport = xstrdup("655");
1167 xasprintf(&myself->hostname, "MYSELF port %s", myport);
1168 myself->connection->hostname = xstrdup(myself->hostname);
1171 get_config_string(lookup_config(config_tree, "UPnP"), &upnp);
1172 bool upnp_tcp = false;
1173 bool upnp_udp = false;
1176 if(!strcasecmp(upnp, "yes")) {
1177 upnp_tcp = upnp_udp = true;
1178 } else if(!strcasecmp(upnp, "udponly")) {
1185 if(upnp_tcp || upnp_udp) {
1186 #ifdef HAVE_MINIUPNPC
1187 upnp_init(upnp_tcp, upnp_udp);
1189 logger(DEBUG_ALWAYS, LOG_WARNING, "UPnP was requested, but tinc isn't built with miniupnpc support!");
1195 last_config_check = now.tv_sec;
1203 bool setup_network(void) {
1210 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
1211 if(pinginterval < 1) {
1212 pinginterval = 86400;
1218 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout)) {
1222 if(pingtimeout < 1 || pingtimeout > pinginterval) {
1223 pingtimeout = pinginterval;
1226 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize)) {
1227 maxoutbufsize = 10 * MTU;
1230 if(!setup_myself()) {
1234 if(!init_control()) {
1238 if(!device_standby) {
1242 /* Run subnet-up scripts for our own subnets */
1244 subnet_update(myself, NULL, true);
1250 close all open network connections
1252 void close_network_connections(void) {
1253 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1255 connection_t *c = node->data;
1257 /* Keep control connections open until the end, so they know when we really terminated */
1258 if(c->status.control) {
1263 terminate_connection(c, false);
1267 list_delete_list(outgoing_list);
1270 if(myself && myself->connection) {
1271 subnet_update(myself, NULL, false);
1272 connection_del(myself->connection);
1275 for(int i = 0; i < listen_sockets; i++) {
1276 io_del(&listen_socket[i].tcp);
1277 io_del(&listen_socket[i].udp);
1278 close(listen_socket[i].tcp.fd);
1279 close(listen_socket[i].udp.fd);
1288 if(!device_standby) {
1294 if(device_fd >= 0) {
1304 free(scriptextension);
1305 free(scriptinterpreter);