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, true)) {
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, true)) {
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 ? netname : ".");
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 ? netname : ".");
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 #ifndef DISABLE_LEGACY
345 static timeout_t keyexpire_timeout;
347 static void keyexpire_handler(void *data) {
349 timeout_set(data, &(struct timeval) {
350 keylifetime, rand() % 100000
355 void regenerate_key(void) {
356 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
359 for splay_each(node_t, n, node_tree) {
360 n->status.validkey_in = false;
364 void load_all_nodes(void) {
367 char dname[PATH_MAX];
369 snprintf(dname, sizeof(dname), "%s" SLASH "hosts", confbase);
370 dir = opendir(dname);
373 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
377 while((ent = readdir(dir))) {
378 if(!check_id(ent->d_name)) {
382 node_t *n = lookup_node(ent->d_name);
384 splay_tree_t *config_tree;
385 init_configuration(&config_tree);
386 read_config_options(config_tree, ent->d_name);
387 read_host_config(config_tree, ent->d_name, true);
391 n->name = xstrdup(ent->d_name);
396 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
399 if(!get_config_subnet(cfg, &s)) {
403 if((s2 = lookup_subnet(n, s))) {
412 if(lookup_config(config_tree, "Address")) {
413 n->status.has_address = true;
416 exit_configuration(&config_tree);
422 char *get_name(void) {
426 get_config_string(lookup_config(config_tree, "Name"), &name);
432 returned_name = replace_name(name);
434 return returned_name;
437 bool setup_myself_reloadable(void) {
446 free(scriptinterpreter);
447 scriptinterpreter = NULL;
448 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
451 free(scriptextension);
453 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension)) {
454 scriptextension = xstrdup("");
457 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
460 if((space = strchr(proxy, ' '))) {
464 if(!strcasecmp(proxy, "none")) {
465 proxytype = PROXY_NONE;
466 } else if(!strcasecmp(proxy, "socks4")) {
467 proxytype = PROXY_SOCKS4;
468 } else if(!strcasecmp(proxy, "socks4a")) {
469 proxytype = PROXY_SOCKS4A;
470 } else if(!strcasecmp(proxy, "socks5")) {
471 proxytype = PROXY_SOCKS5;
472 } else if(!strcasecmp(proxy, "http")) {
473 proxytype = PROXY_HTTP;
474 } else if(!strcasecmp(proxy, "exec")) {
475 proxytype = PROXY_EXEC;
477 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
487 if(!space || !*space) {
488 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
492 proxyhost = xstrdup(space);
501 if(space && (space = strchr(space, ' '))) {
502 *space++ = 0, proxyport = space;
505 if(space && (space = strchr(space, ' '))) {
506 *space++ = 0, proxyuser = space;
509 if(space && (space = strchr(space, ' '))) {
510 *space++ = 0, proxypass = space;
513 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
514 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
518 proxyhost = xstrdup(proxyhost);
519 proxyport = xstrdup(proxyport);
521 if(proxyuser && *proxyuser) {
522 proxyuser = xstrdup(proxyuser);
525 if(proxypass && *proxypass) {
526 proxypass = xstrdup(proxypass);
535 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice) {
536 myself->options |= OPTION_INDIRECT;
539 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice) {
540 myself->options |= OPTION_TCPONLY;
543 if(myself->options & OPTION_TCPONLY) {
544 myself->options |= OPTION_INDIRECT;
547 get_config_bool(lookup_config(config_tree, "UDPDiscovery"), &udp_discovery);
548 get_config_int(lookup_config(config_tree, "UDPDiscoveryKeepaliveInterval"), &udp_discovery_keepalive_interval);
549 get_config_int(lookup_config(config_tree, "UDPDiscoveryInterval"), &udp_discovery_interval);
550 get_config_int(lookup_config(config_tree, "UDPDiscoveryTimeout"), &udp_discovery_timeout);
552 get_config_int(lookup_config(config_tree, "MTUInfoInterval"), &mtu_info_interval);
553 get_config_int(lookup_config(config_tree, "UDPInfoInterval"), &udp_info_interval);
555 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
556 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
558 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
559 if(!strcasecmp(rmode, "router")) {
560 routing_mode = RMODE_ROUTER;
561 } else if(!strcasecmp(rmode, "switch")) {
562 routing_mode = RMODE_SWITCH;
563 } else if(!strcasecmp(rmode, "hub")) {
564 routing_mode = RMODE_HUB;
566 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
573 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
574 if(!strcasecmp(fmode, "off")) {
575 forwarding_mode = FMODE_OFF;
576 } else if(!strcasecmp(fmode, "internal")) {
577 forwarding_mode = FMODE_INTERNAL;
578 } else if(!strcasecmp(fmode, "kernel")) {
579 forwarding_mode = FMODE_KERNEL;
581 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
588 choice = !(myself->options & OPTION_TCPONLY);
589 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
592 myself->options |= OPTION_PMTU_DISCOVERY;
596 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
599 myself->options |= OPTION_CLAMP_MSS;
602 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
603 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
605 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
606 if(!strcasecmp(bmode, "no")) {
607 broadcast_mode = BMODE_NONE;
608 } else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst")) {
609 broadcast_mode = BMODE_MST;
610 } else if(!strcasecmp(bmode, "direct")) {
611 broadcast_mode = BMODE_DIRECT;
613 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
620 const char *const DEFAULT_BROADCAST_SUBNETS[] = { "ff:ff:ff:ff:ff:ff", "255.255.255.255", "224.0.0.0/4", "ff00::/8" };
622 for(size_t i = 0; i < sizeof(DEFAULT_BROADCAST_SUBNETS) / sizeof(*DEFAULT_BROADCAST_SUBNETS); i++) {
623 subnet_t *s = new_subnet();
625 if(!str2net(s, DEFAULT_BROADCAST_SUBNETS[i])) {
632 for(config_t *cfg = lookup_config(config_tree, "BroadcastSubnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
635 if(!get_config_subnet(cfg, &s)) {
644 if(priorityinheritance) {
645 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform for IPv4 connections", "PriorityInheritance");
650 #if !defined(IPV6_TCLASS)
652 if(priorityinheritance) {
653 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform for IPv6 connections", "PriorityInheritance");
658 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire)) {
662 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
663 if(maxtimeout <= 0) {
664 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
671 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
672 if(!strcasecmp(afname, "IPv4")) {
673 addressfamily = AF_INET;
674 } else if(!strcasecmp(afname, "IPv6")) {
675 addressfamily = AF_INET6;
676 } else if(!strcasecmp(afname, "any")) {
677 addressfamily = AF_UNSPEC;
679 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
686 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
688 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime)) {
692 if(!get_config_bool(lookup_config(config_tree, "AutoConnect"), &autoconnect)) {
696 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
698 if(!get_config_int(lookup_config(config_tree, "InvitationExpire"), &invitation_lifetime)) {
699 invitation_lifetime = 604800; // 1 week
702 read_invitation_key();
708 Add listening sockets.
710 static bool add_listen_address(char *address, bool bindto) {
714 char *space = strchr(address, ' ');
721 if(!strcmp(address, "*")) {
726 struct addrinfo *ai, hint = {0};
728 hint.ai_family = addressfamily;
730 hint.ai_socktype = SOCK_STREAM;
732 hint.ai_protocol = IPPROTO_TCP;
734 hint.ai_flags = AI_PASSIVE;
736 #if HAVE_DECL_RES_INIT
740 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
745 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
749 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
750 // Ignore duplicate addresses
753 for(int i = 0; i < listen_sockets; i++)
754 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
763 if(listen_sockets >= MAXSOCKETS) {
764 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
768 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
774 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
781 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
782 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
784 if(debug_level >= DEBUG_CONNECTIONS) {
785 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
786 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
790 listen_socket[listen_sockets].bindto = bindto;
791 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
799 void device_enable(void) {
804 /* Run tinc-up script to further initialize the tap interface */
807 environment_init(&env);
808 execute_script("tinc-up", &env);
809 environment_exit(&env);
812 void device_disable(void) {
814 environment_init(&env);
815 execute_script("tinc-down", &env);
816 environment_exit(&env);
824 Configure node_t myself and set up the local sockets (listen only)
826 static bool setup_myself(void) {
827 char *name, *hostname, *type;
828 char *address = NULL;
829 bool port_specified = false;
831 if(!(name = get_name())) {
832 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
836 myname = xstrdup(name);
838 myself->connection = new_connection();
840 myself->connection->name = xstrdup(name);
841 read_host_config(config_tree, name, true);
843 if(!get_config_string(lookup_config(config_tree, "Port"), &myport)) {
844 myport = xstrdup("655");
846 port_specified = true;
849 myself->connection->options = 0;
850 myself->connection->protocol_major = PROT_MAJOR;
851 myself->connection->protocol_minor = PROT_MINOR;
853 myself->options |= PROT_MINOR << 24;
855 #ifdef DISABLE_LEGACY
856 experimental = read_ecdsa_private_key();
859 logger(DEBUG_ALWAYS, LOG_ERR, "No private key available, cannot start tinc!");
865 if(!get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental)) {
866 experimental = read_ecdsa_private_key();
869 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for SPTPS disabled.");
872 if(experimental && !read_ecdsa_private_key()) {
877 if(!read_rsa_private_key()) {
879 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for legacy protocol disabled.");
881 logger(DEBUG_ALWAYS, LOG_ERR, "No private keys available, cannot start tinc!");
888 /* Ensure myport is numeric */
891 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
894 if(!ai || !ai->ai_addr) {
899 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
901 sockaddr2str(&sa, NULL, &myport);
904 /* Read in all the subnets specified in the host configuration file */
906 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
909 if(!get_config_subnet(cfg, &subnet)) {
913 subnet_add(myself, subnet);
916 /* Check some options */
918 if(!setup_myself_reloadable()) {
922 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
923 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
924 strictsubnets |= tunnelserver;
926 if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
927 if(max_connection_burst <= 0) {
928 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
933 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
935 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
940 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
942 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
947 get_config_int(lookup_config(config_tree, "FWMark"), &fwmark);
951 logger(DEBUG_ALWAYS, LOG_ERR, "FWMark not supported on this platform!");
959 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
960 if(replaywin_int < 0) {
961 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
965 replaywin = (unsigned)replaywin_int;
966 sptps_replaywin = replaywin;
969 #ifndef DISABLE_LEGACY
970 /* Generate packet encryption key */
974 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher)) {
975 cipher = xstrdup("aes-256-cbc");
978 if(!strcasecmp(cipher, "none")) {
979 myself->incipher = NULL;
980 } else if(!(myself->incipher = cipher_open_by_name(cipher))) {
981 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
988 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval) {
989 keylifetime, rand() % 100000
992 /* Check if we want to use message authentication codes... */
995 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
998 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
1004 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest)) {
1005 digest = xstrdup("sha256");
1008 if(!strcasecmp(digest, "none")) {
1009 myself->indigest = NULL;
1010 } else if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
1011 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
1021 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
1022 if(myself->incompression < 0 || myself->incompression > 11) {
1023 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
1027 myself->incompression = 0;
1030 myself->connection->outcompression = 0;
1034 myself->nexthop = myself;
1035 myself->via = myself;
1036 myself->status.reachable = true;
1037 myself->last_state_change = now.tv_sec;
1038 myself->status.sptps = experimental;
1049 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
1050 if(!strcasecmp(type, "dummy")) {
1051 devops = dummy_devops;
1052 } else if(!strcasecmp(type, "raw_socket")) {
1053 devops = raw_socket_devops;
1054 } else if(!strcasecmp(type, "multicast")) {
1055 devops = multicast_devops;
1056 } else if(!strcasecmp(type, "fd")) {
1061 else if(!strcasecmp(type, "uml")) {
1062 devops = uml_devops;
1067 else if(!strcasecmp(type, "vde")) {
1068 devops = vde_devops;
1075 get_config_bool(lookup_config(config_tree, "DeviceStandby"), &device_standby);
1077 if(!devops.setup()) {
1081 if(device_fd >= 0) {
1082 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
1087 if(!do_detach && getenv("LISTEN_FDS")) {
1091 listen_sockets = atoi(getenv("LISTEN_FDS"));
1092 #ifdef HAVE_UNSETENV
1093 unsetenv("LISTEN_FDS");
1096 if(listen_sockets > MAXSOCKETS) {
1097 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
1101 for(int i = 0; i < listen_sockets; i++) {
1104 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
1105 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(sockerrno));
1110 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
1113 int udp_fd = setup_vpn_in_socket(&sa);
1119 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
1120 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
1122 if(debug_level >= DEBUG_CONNECTIONS) {
1123 hostname = sockaddr2hostname(&sa);
1124 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
1128 memcpy(&listen_socket[i].sa, &sa, salen);
1134 for(config_t *cfg = lookup_config(config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1136 get_config_string(cfg, &address);
1138 if(!add_listen_address(address, true)) {
1143 for(config_t *cfg = lookup_config(config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1145 get_config_string(cfg, &address);
1147 if(!add_listen_address(address, false)) {
1153 if(!add_listen_address(address, NULL)) {
1158 if(!listen_sockets) {
1159 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
1163 /* If no Port option was specified, set myport to the port used by the first listening socket. */
1165 if(!port_specified || atoi(myport) == 0) {
1167 socklen_t salen = sizeof(sa);
1169 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
1171 sockaddr2str(&sa, NULL, &myport);
1174 myport = xstrdup("655");
1179 xasprintf(&myself->hostname, "MYSELF port %s", myport);
1180 myself->connection->hostname = xstrdup(myself->hostname);
1183 get_config_string(lookup_config(config_tree, "UPnP"), &upnp);
1184 bool upnp_tcp = false;
1185 bool upnp_udp = false;
1188 if(!strcasecmp(upnp, "yes")) {
1189 upnp_tcp = upnp_udp = true;
1190 } else if(!strcasecmp(upnp, "udponly")) {
1197 if(upnp_tcp || upnp_udp) {
1198 #ifdef HAVE_MINIUPNPC
1199 upnp_init(upnp_tcp, upnp_udp);
1201 logger(DEBUG_ALWAYS, LOG_WARNING, "UPnP was requested, but tinc isn't built with miniupnpc support!");
1207 last_config_check = now.tv_sec;
1215 bool setup_network(void) {
1222 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
1223 if(pinginterval < 1) {
1224 pinginterval = 86400;
1230 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout)) {
1234 if(pingtimeout < 1 || pingtimeout > pinginterval) {
1235 pingtimeout = pinginterval;
1238 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize)) {
1239 maxoutbufsize = 10 * MTU;
1242 if(!setup_myself()) {
1246 if(!init_control()) {
1250 if(!device_standby) {
1254 /* Run subnet-up scripts for our own subnets */
1256 subnet_update(myself, NULL, true);
1262 close all open network connections
1264 void close_network_connections(void) {
1265 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1267 connection_t *c = node->data;
1269 /* Keep control connections open until the end, so they know when we really terminated */
1270 if(c->status.control) {
1275 terminate_connection(c, false);
1279 list_delete_list(outgoing_list);
1282 if(myself && myself->connection) {
1283 subnet_update(myself, NULL, false);
1284 connection_del(myself->connection);
1287 for(int i = 0; i < listen_sockets; i++) {
1288 io_del(&listen_socket[i].tcp);
1289 io_del(&listen_socket[i].udp);
1290 close(listen_socket[i].tcp.fd);
1291 close(listen_socket[i].udp.fd);
1300 if(!device_standby) {
1306 if(device_fd >= 0) {
1316 free(scriptextension);
1317 free(scriptinterpreter);