3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2021 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;
55 char *proxyhost = NULL;
56 char *proxyport = NULL;
57 char *proxyuser = NULL;
58 char *proxypass = NULL;
60 proxytype_t proxytype;
62 bool disablebuggypeers;
64 char *scriptinterpreter;
65 char *scriptextension;
67 bool node_read_ecdsa_public_key(node_t *n) {
68 if(ecdsa_active(n->ecdsa)) {
72 splay_tree_t *config_tree;
77 init_configuration(&config_tree);
79 if(!read_host_config(config_tree, n->name, true)) {
83 /* First, check for simple Ed25519PublicKey statement */
85 if(get_config_string(lookup_config(config_tree, "Ed25519PublicKey"), &p)) {
86 n->ecdsa = ecdsa_set_base64_public_key(p);
91 /* Else, check for Ed25519PublicKeyFile statement and read it */
93 if(!get_config_string(lookup_config(config_tree, "Ed25519PublicKeyFile"), &pubname)) {
94 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
97 fp = fopen(pubname, "r");
103 n->ecdsa = ecdsa_read_pem_public_key(fp);
107 exit_configuration(&config_tree);
112 bool read_ecdsa_public_key(connection_t *c) {
113 if(ecdsa_active(c->ecdsa)) {
121 if(!c->config_tree) {
122 init_configuration(&c->config_tree);
124 if(!read_host_config(c->config_tree, c->name, true)) {
129 /* First, check for simple Ed25519PublicKey statement */
131 if(get_config_string(lookup_config(c->config_tree, "Ed25519PublicKey"), &p)) {
132 c->ecdsa = ecdsa_set_base64_public_key(p);
137 /* Else, check for Ed25519PublicKeyFile statement and read it */
139 if(!get_config_string(lookup_config(c->config_tree, "Ed25519PublicKeyFile"), &fname)) {
140 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
143 fp = fopen(fname, "r");
146 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading Ed25519 public key file `%s': %s",
147 fname, strerror(errno));
152 c->ecdsa = ecdsa_read_pem_public_key(fp);
154 if(!c->ecdsa && errno != ENOENT) {
155 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing Ed25519 public key file `%s' failed.", fname);
163 #ifndef DISABLE_LEGACY
164 bool read_rsa_public_key(connection_t *c) {
169 /* First, check for simple PublicKey statement */
171 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
172 c->rsa = rsa_set_hex_public_key(n, "FFFF");
177 /* Else, check for PublicKeyFile statement and read it */
179 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname)) {
180 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
183 fp = fopen(fname, "r");
186 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
191 c->rsa = rsa_read_pem_public_key(fp);
195 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
203 static bool read_ecdsa_private_key(void) {
207 /* Check for PrivateKeyFile statement and read it */
209 if(!get_config_string(lookup_config(config_tree, "Ed25519PrivateKeyFile"), &fname)) {
210 xasprintf(&fname, "%s" SLASH "ed25519_key.priv", confbase);
213 fp = fopen(fname, "r");
216 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading Ed25519 private key file `%s': %s", fname, strerror(errno));
218 if(errno == ENOENT) {
219 logger(DEBUG_ALWAYS, LOG_INFO, "Create an Ed25519 key pair with `tinc -n %s generate-ed25519-keys'.", netname ? netname : ".");
229 if(fstat(fileno(fp), &s)) {
230 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat Ed25519 private key file `%s': %s'", fname, strerror(errno));
235 if(s.st_mode & ~0100700u) {
236 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for Ed25519 private key file `%s'!", fname);
241 myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
244 if(!myself->connection->ecdsa) {
245 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
249 return myself->connection->ecdsa;
252 static bool read_invitation_key(void) {
254 char fname[PATH_MAX];
257 ecdsa_free(invitation_key);
258 invitation_key = NULL;
261 snprintf(fname, sizeof(fname), "%s" SLASH "invitations" SLASH "ed25519_key.priv", confbase);
263 fp = fopen(fname, "r");
266 invitation_key = ecdsa_read_pem_private_key(fp);
269 if(!invitation_key) {
270 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
274 return invitation_key;
277 #ifndef DISABLE_LEGACY
278 static bool read_rsa_private_key(void) {
283 /* First, check for simple PrivateKey statement */
285 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
286 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
287 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
292 myself->connection->rsa = rsa_set_hex_private_key(n, "FFFF", d);
295 return myself->connection->rsa;
298 /* Else, check for PrivateKeyFile statement and read it */
300 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname)) {
301 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
304 fp = fopen(fname, "r");
307 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
308 fname, strerror(errno));
310 if(errno == ENOENT) {
311 logger(DEBUG_ALWAYS, LOG_INFO, "Create an RSA key pair with `tinc -n %s generate-rsa-keys'.", netname ? netname : ".");
321 if(fstat(fileno(fp), &s)) {
322 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
327 if(s.st_mode & ~0100700u) {
328 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
333 myself->connection->rsa = rsa_read_pem_private_key(fp);
336 if(!myself->connection->rsa) {
337 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
341 return myself->connection->rsa;
345 #ifndef DISABLE_LEGACY
346 static timeout_t keyexpire_timeout;
348 static void keyexpire_handler(void *data) {
350 timeout_set(data, &(struct timeval) {
351 keylifetime, rand() % 100000
356 void regenerate_key(void) {
357 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
360 for splay_each(node_t, n, node_tree) {
361 n->status.validkey_in = false;
365 void load_all_nodes(void) {
368 char dname[PATH_MAX];
370 snprintf(dname, sizeof(dname), "%s" SLASH "hosts", confbase);
371 dir = opendir(dname);
374 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
378 while((ent = readdir(dir))) {
379 if(!check_id(ent->d_name)) {
383 node_t *n = lookup_node(ent->d_name);
385 splay_tree_t *config_tree;
386 init_configuration(&config_tree);
387 read_config_options(config_tree, ent->d_name);
388 read_host_config(config_tree, ent->d_name, true);
392 n->name = xstrdup(ent->d_name);
397 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
400 if(!get_config_subnet(cfg, &s)) {
404 if((s2 = lookup_subnet(n, s))) {
413 if(lookup_config(config_tree, "Address")) {
414 n->status.has_address = true;
417 exit_configuration(&config_tree);
423 char *get_name(void) {
427 get_config_string(lookup_config(config_tree, "Name"), &name);
433 returned_name = replace_name(name);
435 return returned_name;
438 bool setup_myself_reloadable(void) {
439 free(scriptinterpreter);
440 scriptinterpreter = NULL;
442 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
444 free(scriptextension);
446 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension)) {
447 scriptextension = xstrdup("");
452 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
457 if((space = strchr(proxy, ' '))) {
461 if(!strcasecmp(proxy, "none")) {
462 proxytype = PROXY_NONE;
463 } else if(!strcasecmp(proxy, "socks4")) {
464 proxytype = PROXY_SOCKS4;
465 } else if(!strcasecmp(proxy, "socks4a")) {
466 proxytype = PROXY_SOCKS4A;
467 } else if(!strcasecmp(proxy, "socks5")) {
468 proxytype = PROXY_SOCKS5;
469 } else if(!strcasecmp(proxy, "http")) {
470 proxytype = PROXY_HTTP;
471 } else if(!strcasecmp(proxy, "exec")) {
472 proxytype = PROXY_EXEC;
474 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
497 if(!space || !*space) {
498 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
503 proxyhost = xstrdup(space);
512 if(space && (space = strchr(space, ' '))) {
513 *space++ = 0, proxyport = space;
516 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
517 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
524 if(space && (space = strchr(space, ' '))) {
525 *space++ = 0, proxyuser = space;
528 if(space && (space = strchr(space, ' '))) {
529 *space++ = 0, proxypass = space;
532 proxyhost = xstrdup(proxyhost);
533 proxyport = xstrdup(proxyport);
535 if(proxyuser && *proxyuser) {
536 proxyuser = xstrdup(proxyuser);
539 if(proxypass && *proxypass) {
540 proxypass = xstrdup(proxypass);
551 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice) {
552 myself->options |= OPTION_INDIRECT;
555 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice) {
556 myself->options |= OPTION_TCPONLY;
559 if(myself->options & OPTION_TCPONLY) {
560 myself->options |= OPTION_INDIRECT;
563 get_config_bool(lookup_config(config_tree, "UDPDiscovery"), &udp_discovery);
564 get_config_int(lookup_config(config_tree, "UDPDiscoveryKeepaliveInterval"), &udp_discovery_keepalive_interval);
565 get_config_int(lookup_config(config_tree, "UDPDiscoveryInterval"), &udp_discovery_interval);
566 get_config_int(lookup_config(config_tree, "UDPDiscoveryTimeout"), &udp_discovery_timeout);
568 get_config_int(lookup_config(config_tree, "MTUInfoInterval"), &mtu_info_interval);
569 get_config_int(lookup_config(config_tree, "UDPInfoInterval"), &udp_info_interval);
571 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
572 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
576 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
577 if(!strcasecmp(rmode, "router")) {
578 routing_mode = RMODE_ROUTER;
579 } else if(!strcasecmp(rmode, "switch")) {
580 routing_mode = RMODE_SWITCH;
581 } else if(!strcasecmp(rmode, "hub")) {
582 routing_mode = RMODE_HUB;
584 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
594 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
595 if(!strcasecmp(fmode, "off")) {
596 forwarding_mode = FMODE_OFF;
597 } else if(!strcasecmp(fmode, "internal")) {
598 forwarding_mode = FMODE_INTERNAL;
599 } else if(!strcasecmp(fmode, "kernel")) {
600 forwarding_mode = FMODE_KERNEL;
602 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
610 choice = !(myself->options & OPTION_TCPONLY);
611 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
614 myself->options |= OPTION_PMTU_DISCOVERY;
618 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
621 myself->options |= OPTION_CLAMP_MSS;
624 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
625 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
629 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
630 if(!strcasecmp(bmode, "no")) {
631 broadcast_mode = BMODE_NONE;
632 } else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst")) {
633 broadcast_mode = BMODE_MST;
634 } else if(!strcasecmp(bmode, "direct")) {
635 broadcast_mode = BMODE_DIRECT;
637 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
645 /* Delete all broadcast subnets before re-adding them */
647 for splay_each(subnet_t, s, subnet_tree) {
649 splay_delete_node(subnet_tree, node);
653 const char *const DEFAULT_BROADCAST_SUBNETS[] = { "ff:ff:ff:ff:ff:ff", "255.255.255.255", "224.0.0.0/4", "ff00::/8" };
655 for(size_t i = 0; i < sizeof(DEFAULT_BROADCAST_SUBNETS) / sizeof(*DEFAULT_BROADCAST_SUBNETS); i++) {
656 subnet_t *s = new_subnet();
658 if(!str2net(s, DEFAULT_BROADCAST_SUBNETS[i])) {
665 for(config_t *cfg = lookup_config(config_tree, "BroadcastSubnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
668 if(!get_config_subnet(cfg, &s)) {
677 if(priorityinheritance) {
678 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform for IPv4 connections", "PriorityInheritance");
683 #if !defined(IPV6_TCLASS)
685 if(priorityinheritance) {
686 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform for IPv6 connections", "PriorityInheritance");
691 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire)) {
695 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
696 if(maxtimeout <= 0) {
697 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
706 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
707 if(!strcasecmp(afname, "IPv4")) {
708 addressfamily = AF_INET;
709 } else if(!strcasecmp(afname, "IPv6")) {
710 addressfamily = AF_INET6;
711 } else if(!strcasecmp(afname, "any")) {
712 addressfamily = AF_UNSPEC;
714 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
722 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
724 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime)) {
728 if(!get_config_bool(lookup_config(config_tree, "AutoConnect"), &autoconnect)) {
732 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
734 if(!get_config_int(lookup_config(config_tree, "InvitationExpire"), &invitation_lifetime)) {
735 invitation_lifetime = 604800; // 1 week
738 read_invitation_key();
744 Add listening sockets.
746 static bool add_listen_address(char *address, bool bindto) {
750 char *space = strchr(address, ' ');
757 if(!strcmp(address, "*")) {
762 struct addrinfo *ai, hint = {0};
764 hint.ai_family = addressfamily;
766 hint.ai_socktype = SOCK_STREAM;
768 hint.ai_protocol = IPPROTO_TCP;
770 hint.ai_flags = AI_PASSIVE;
772 #if HAVE_DECL_RES_INIT
776 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
781 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
785 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
786 // Ignore duplicate addresses
789 for(int i = 0; i < listen_sockets; i++)
790 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
799 if(listen_sockets >= MAXSOCKETS) {
800 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
805 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
811 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
818 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
819 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
821 if(debug_level >= DEBUG_CONNECTIONS) {
822 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
823 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
827 listen_socket[listen_sockets].bindto = bindto;
828 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
836 void device_enable(void) {
841 /* Run tinc-up script to further initialize the tap interface */
844 environment_init(&env);
845 execute_script("tinc-up", &env);
846 environment_exit(&env);
849 void device_disable(void) {
851 environment_init(&env);
852 execute_script("tinc-down", &env);
853 environment_exit(&env);
861 Configure node_t myself and set up the local sockets (listen only)
863 static bool setup_myself(void) {
864 char *name, *hostname, *type;
865 char *address = NULL;
866 bool port_specified = false;
868 if(!(name = get_name())) {
869 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
873 myname = xstrdup(name);
875 myself->connection = new_connection();
877 myself->connection->name = xstrdup(name);
878 read_host_config(config_tree, name, true);
880 if(!get_config_string(lookup_config(config_tree, "Port"), &myport)) {
881 myport = xstrdup("655");
883 port_specified = true;
886 myself->connection->options = 0;
887 myself->connection->protocol_major = PROT_MAJOR;
888 myself->connection->protocol_minor = PROT_MINOR;
890 myself->options |= PROT_MINOR << 24;
892 #ifdef DISABLE_LEGACY
893 experimental = read_ecdsa_private_key();
896 logger(DEBUG_ALWAYS, LOG_ERR, "No private key available, cannot start tinc!");
902 if(!get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental)) {
903 experimental = read_ecdsa_private_key();
906 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for SPTPS disabled.");
909 if(experimental && !read_ecdsa_private_key()) {
914 if(!read_rsa_private_key()) {
916 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for legacy protocol disabled.");
918 logger(DEBUG_ALWAYS, LOG_ERR, "No private keys available, cannot start tinc!");
925 /* Ensure myport is numeric */
928 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
931 if(!ai || !ai->ai_addr) {
936 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
938 sockaddr2str(&sa, NULL, &myport);
941 /* Read in all the subnets specified in the host configuration file */
943 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
946 if(!get_config_subnet(cfg, &subnet)) {
950 subnet_add(myself, subnet);
953 /* Check some options */
955 if(!setup_myself_reloadable()) {
959 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
960 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
961 strictsubnets |= tunnelserver;
963 if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
964 if(max_connection_burst <= 0) {
965 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
970 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
972 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
976 udp_rcvbuf_warnings = true;
979 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
981 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
985 udp_sndbuf_warnings = true;
988 get_config_int(lookup_config(config_tree, "FWMark"), &fwmark);
992 logger(DEBUG_ALWAYS, LOG_ERR, "FWMark not supported on this platform!");
1000 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
1001 if(replaywin_int < 0) {
1002 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
1006 replaywin = (unsigned)replaywin_int;
1007 sptps_replaywin = replaywin;
1010 #ifndef DISABLE_LEGACY
1011 /* Generate packet encryption key */
1015 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher)) {
1016 cipher = xstrdup("aes-256-cbc");
1019 if(!strcasecmp(cipher, "none")) {
1020 myself->incipher = NULL;
1021 } else if(!(myself->incipher = cipher_open_by_name(cipher))) {
1022 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
1029 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval) {
1030 keylifetime, rand() % 100000
1033 /* Check if we want to use message authentication codes... */
1036 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
1039 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
1045 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest)) {
1046 digest = xstrdup("sha256");
1049 if(!strcasecmp(digest, "none")) {
1050 myself->indigest = NULL;
1051 } else if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
1052 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
1062 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
1063 switch(myself->incompression) {
1068 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
1069 logger(DEBUG_ALWAYS, LOG_ERR, "LZ4 compression is unavailable on this node.");
1078 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
1079 logger(DEBUG_ALWAYS, LOG_ERR, "LZO compression is unavailable on this node.");
1095 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
1096 logger(DEBUG_ALWAYS, LOG_ERR, "ZLIB compression is unavailable on this node.");
1104 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
1105 logger(DEBUG_ALWAYS, LOG_ERR, "Compression level %i is unrecognized by this node.", myself->incompression);
1109 myself->incompression = 0;
1112 myself->connection->outcompression = 0;
1116 myself->nexthop = myself;
1117 myself->via = myself;
1118 myself->status.reachable = true;
1119 myself->last_state_change = now.tv_sec;
1120 myself->status.sptps = experimental;
1131 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
1132 if(!strcasecmp(type, "dummy")) {
1133 devops = dummy_devops;
1134 } else if(!strcasecmp(type, "raw_socket")) {
1135 devops = raw_socket_devops;
1136 } else if(!strcasecmp(type, "multicast")) {
1137 devops = multicast_devops;
1140 #ifdef HAVE_SYS_UN_H
1141 else if(!strcasecmp(type, "fd")) {
1147 else if(!strcasecmp(type, "uml")) {
1148 devops = uml_devops;
1153 else if(!strcasecmp(type, "vde")) {
1154 devops = vde_devops;
1161 get_config_bool(lookup_config(config_tree, "DeviceStandby"), &device_standby);
1163 if(!devops.setup()) {
1167 if(device_fd >= 0) {
1168 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
1173 if(!do_detach && getenv("LISTEN_FDS")) {
1177 listen_sockets = atoi(getenv("LISTEN_FDS"));
1178 #ifdef HAVE_UNSETENV
1179 unsetenv("LISTEN_FDS");
1182 if(listen_sockets > MAXSOCKETS) {
1183 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
1187 for(int i = 0; i < listen_sockets; i++) {
1190 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
1191 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(sockerrno));
1196 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
1199 int udp_fd = setup_vpn_in_socket(&sa);
1205 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
1206 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
1208 if(debug_level >= DEBUG_CONNECTIONS) {
1209 hostname = sockaddr2hostname(&sa);
1210 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
1214 memcpy(&listen_socket[i].sa, &sa, salen);
1220 for(config_t *cfg = lookup_config(config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1222 get_config_string(cfg, &address);
1224 if(!add_listen_address(address, true)) {
1229 for(config_t *cfg = lookup_config(config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1231 get_config_string(cfg, &address);
1233 if(!add_listen_address(address, false)) {
1239 if(!add_listen_address(address, NULL)) {
1244 if(!listen_sockets) {
1245 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
1249 /* If no Port option was specified, set myport to the port used by the first listening socket. */
1251 if(!port_specified || atoi(myport) == 0) {
1253 socklen_t salen = sizeof(sa);
1255 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
1257 sockaddr2str(&sa, NULL, &myport);
1260 myport = xstrdup("655");
1265 xasprintf(&myself->hostname, "MYSELF port %s", myport);
1266 myself->connection->hostname = xstrdup(myself->hostname);
1269 get_config_string(lookup_config(config_tree, "UPnP"), &upnp);
1270 bool upnp_tcp = false;
1271 bool upnp_udp = false;
1274 if(!strcasecmp(upnp, "yes")) {
1275 upnp_tcp = upnp_udp = true;
1276 } else if(!strcasecmp(upnp, "udponly")) {
1283 if(upnp_tcp || upnp_udp) {
1284 #ifdef HAVE_MINIUPNPC
1285 upnp_init(upnp_tcp, upnp_udp);
1287 logger(DEBUG_ALWAYS, LOG_WARNING, "UPnP was requested, but tinc isn't built with miniupnpc support!");
1293 last_config_check = now.tv_sec;
1301 bool setup_network(void) {
1308 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
1309 if(pinginterval < 1) {
1310 pinginterval = 86400;
1316 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout)) {
1320 if(pingtimeout < 1 || pingtimeout > pinginterval) {
1321 pingtimeout = pinginterval;
1324 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize)) {
1325 maxoutbufsize = 10 * MTU;
1328 if(!setup_myself()) {
1332 if(!init_control()) {
1336 if(!device_standby) {
1340 /* Run subnet-up scripts for our own subnets */
1342 subnet_update(myself, NULL, true);
1348 close all open network connections
1350 void close_network_connections(void) {
1351 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1353 connection_t *c = node->data;
1355 /* Keep control connections open until the end, so they know when we really terminated */
1356 if(c->status.control) {
1361 terminate_connection(c, false);
1365 list_delete_list(outgoing_list);
1368 if(myself && myself->connection) {
1369 subnet_update(myself, NULL, false);
1370 free_connection(myself->connection);
1373 for(int i = 0; i < listen_sockets; i++) {
1374 io_del(&listen_socket[i].tcp);
1375 io_del(&listen_socket[i].udp);
1376 close(listen_socket[i].tcp.fd);
1377 close(listen_socket[i].udp.fd);
1386 if(!device_standby) {
1392 if(device_fd >= 0) {
1402 free(scriptextension);
1403 free(scriptinterpreter);