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))
70 splay_tree_t *config_tree;
75 init_configuration(&config_tree);
76 if(!read_host_config(config_tree, n->name))
79 /* First, check for simple Ed25519PublicKey statement */
81 if(get_config_string(lookup_config(config_tree, "Ed25519PublicKey"), &p)) {
82 n->ecdsa = ecdsa_set_base64_public_key(p);
87 /* Else, check for Ed25519PublicKeyFile statement and read it */
89 if(!get_config_string(lookup_config(config_tree, "Ed25519PublicKeyFile"), &pubname))
90 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
92 fp = fopen(pubname, "r");
97 n->ecdsa = ecdsa_read_pem_public_key(fp);
101 exit_configuration(&config_tree);
106 bool read_ecdsa_public_key(connection_t *c) {
107 if(ecdsa_active(c->ecdsa))
114 if(!c->config_tree) {
115 init_configuration(&c->config_tree);
116 if(!read_host_config(c->config_tree, c->name))
120 /* First, check for simple Ed25519PublicKey statement */
122 if(get_config_string(lookup_config(c->config_tree, "Ed25519PublicKey"), &p)) {
123 c->ecdsa = ecdsa_set_base64_public_key(p);
128 /* Else, check for Ed25519PublicKeyFile statement and read it */
130 if(!get_config_string(lookup_config(c->config_tree, "Ed25519PublicKeyFile"), &fname))
131 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
133 fp = fopen(fname, "r");
136 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading Ed25519 public key file `%s': %s",
137 fname, strerror(errno));
142 c->ecdsa = ecdsa_read_pem_public_key(fp);
144 if(!c->ecdsa && errno != ENOENT)
145 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing Ed25519 public key file `%s' failed.", fname);
152 #ifndef DISABLE_LEGACY
153 bool read_rsa_public_key(connection_t *c) {
158 /* First, check for simple PublicKey statement */
160 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
161 c->rsa = rsa_set_hex_public_key(n, "FFFF");
166 /* Else, check for PublicKeyFile statement and read it */
168 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
169 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
171 fp = fopen(fname, "r");
174 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
179 c->rsa = rsa_read_pem_public_key(fp);
183 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
189 static bool read_ecdsa_private_key(void) {
193 /* Check for PrivateKeyFile statement and read it */
195 if(!get_config_string(lookup_config(config_tree, "Ed25519PrivateKeyFile"), &fname))
196 xasprintf(&fname, "%s" SLASH "ed25519_key.priv", confbase);
198 fp = fopen(fname, "r");
201 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading Ed25519 private key file `%s': %s", fname, strerror(errno));
203 logger(DEBUG_ALWAYS, LOG_INFO, "Create an Ed25519 keypair with `tinc -n %s generate-ed25519-keys'.", netname ?: ".");
208 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
211 if(fstat(fileno(fp), &s)) {
212 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat Ed25519 private key file `%s': %s'", fname, strerror(errno));
217 if(s.st_mode & ~0100700)
218 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for Ed25519 private key file `%s'!", fname);
221 myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
224 if(!myself->connection->ecdsa)
225 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
227 return myself->connection->ecdsa;
230 static bool read_invitation_key(void) {
232 char fname[PATH_MAX];
235 ecdsa_free(invitation_key);
236 invitation_key = NULL;
239 snprintf(fname, sizeof fname, "%s" SLASH "invitations" SLASH "ed25519_key.priv", confbase);
241 fp = fopen(fname, "r");
244 invitation_key = ecdsa_read_pem_private_key(fp);
247 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
250 return invitation_key;
253 #ifndef DISABLE_LEGACY
254 static bool read_rsa_private_key(void) {
259 /* First, check for simple PrivateKey statement */
261 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
262 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
263 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
267 myself->connection->rsa = rsa_set_hex_private_key(n, "FFFF", d);
270 return myself->connection->rsa;
273 /* Else, check for PrivateKeyFile statement and read it */
275 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
276 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
278 fp = fopen(fname, "r");
281 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
282 fname, strerror(errno));
284 logger(DEBUG_ALWAYS, LOG_INFO, "Create an RSA keypair with `tinc -n %s generate-rsa-keys'.", netname ?: ".");
289 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
292 if(fstat(fileno(fp), &s)) {
293 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
298 if(s.st_mode & ~0100700)
299 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
302 myself->connection->rsa = rsa_read_pem_private_key(fp);
305 if(!myself->connection->rsa)
306 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
308 return myself->connection->rsa;
312 static timeout_t keyexpire_timeout;
314 static void keyexpire_handler(void *data) {
316 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
319 void regenerate_key(void) {
320 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
322 for splay_each(node_t, n, node_tree)
323 n->status.validkey_in = false;
326 void load_all_nodes(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);
344 splay_tree_t *config_tree;
345 init_configuration(&config_tree);
346 read_config_options(config_tree, ent->d_name);
347 read_host_config(config_tree, ent->d_name);
351 n->name = xstrdup(ent->d_name);
356 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
359 if(!get_config_subnet(cfg, &s))
362 if((s2 = lookup_subnet(n, s))) {
371 if(lookup_config(config_tree, "Address"))
372 n->status.has_address = true;
374 exit_configuration(&config_tree);
380 char *get_name(void) {
384 get_config_string(lookup_config(config_tree, "Name"), &name);
389 returned_name = replace_name(name);
391 return returned_name;
394 bool setup_myself_reloadable(void) {
403 free(scriptinterpreter);
404 scriptinterpreter = NULL;
405 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
408 free(scriptextension);
409 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
410 scriptextension = xstrdup("");
412 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
414 if((space = strchr(proxy, ' ')))
417 if(!strcasecmp(proxy, "none")) {
418 proxytype = PROXY_NONE;
419 } else if(!strcasecmp(proxy, "socks4")) {
420 proxytype = PROXY_SOCKS4;
421 } else if(!strcasecmp(proxy, "socks4a")) {
422 proxytype = PROXY_SOCKS4A;
423 } else if(!strcasecmp(proxy, "socks5")) {
424 proxytype = PROXY_SOCKS5;
425 } else if(!strcasecmp(proxy, "http")) {
426 proxytype = PROXY_HTTP;
427 } else if(!strcasecmp(proxy, "exec")) {
428 proxytype = PROXY_EXEC;
430 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
440 if(!space || !*space) {
441 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
444 proxyhost = xstrdup(space);
452 if(space && (space = strchr(space, ' ')))
453 *space++ = 0, proxyport = space;
454 if(space && (space = strchr(space, ' ')))
455 *space++ = 0, proxyuser = space;
456 if(space && (space = strchr(space, ' ')))
457 *space++ = 0, proxypass = space;
458 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
459 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
462 proxyhost = xstrdup(proxyhost);
463 proxyport = xstrdup(proxyport);
464 if(proxyuser && *proxyuser)
465 proxyuser = xstrdup(proxyuser);
466 if(proxypass && *proxypass)
467 proxypass = xstrdup(proxypass);
474 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
475 myself->options |= OPTION_INDIRECT;
477 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
478 myself->options |= OPTION_TCPONLY;
480 if(myself->options & OPTION_TCPONLY)
481 myself->options |= OPTION_INDIRECT;
483 get_config_bool(lookup_config(config_tree, "UDPDiscovery"), &udp_discovery);
484 get_config_int(lookup_config(config_tree, "UDPDiscoveryKeepaliveInterval"), &udp_discovery_keepalive_interval);
485 get_config_int(lookup_config(config_tree, "UDPDiscoveryInterval"), &udp_discovery_interval);
486 get_config_int(lookup_config(config_tree, "UDPDiscoveryTimeout"), &udp_discovery_timeout);
488 get_config_int(lookup_config(config_tree, "MTUInfoInterval"), &mtu_info_interval);
489 get_config_int(lookup_config(config_tree, "UDPInfoInterval"), &udp_info_interval);
491 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
492 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
494 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
495 if(!strcasecmp(rmode, "router"))
496 routing_mode = RMODE_ROUTER;
497 else if(!strcasecmp(rmode, "switch"))
498 routing_mode = RMODE_SWITCH;
499 else if(!strcasecmp(rmode, "hub"))
500 routing_mode = RMODE_HUB;
502 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
508 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
509 if(!strcasecmp(fmode, "off"))
510 forwarding_mode = FMODE_OFF;
511 else if(!strcasecmp(fmode, "internal"))
512 forwarding_mode = FMODE_INTERNAL;
513 else if(!strcasecmp(fmode, "kernel"))
514 forwarding_mode = FMODE_KERNEL;
516 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
523 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
525 myself->options |= OPTION_PMTU_DISCOVERY;
528 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
530 myself->options |= OPTION_CLAMP_MSS;
532 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
533 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
534 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
535 if(!strcasecmp(bmode, "no"))
536 broadcast_mode = BMODE_NONE;
537 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
538 broadcast_mode = BMODE_MST;
539 else if(!strcasecmp(bmode, "direct"))
540 broadcast_mode = BMODE_DIRECT;
542 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
548 const char* const DEFAULT_BROADCAST_SUBNETS[] = { "ff:ff:ff:ff:ff:ff", "255.255.255.255", "224.0.0.0/4", "ff00::/8" };
549 for (size_t i = 0; i < sizeof(DEFAULT_BROADCAST_SUBNETS) / sizeof(*DEFAULT_BROADCAST_SUBNETS); i++) {
550 subnet_t *s = new_subnet();
551 if (!str2net(s, DEFAULT_BROADCAST_SUBNETS[i]))
555 for (config_t* cfg = lookup_config(config_tree, "BroadcastSubnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
557 if (!get_config_subnet(cfg, &s))
562 #if !defined(IPPROTO_IP) || !defined(IP_TOS)
563 if(priorityinheritance)
564 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform for IPv4 connections", "PriorityInheritance");
567 #if !defined(IPPROTO_IPV6) || !defined(IPV6_TCLASS)
568 if(priorityinheritance)
569 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform for IPv6 connections", "PriorityInheritance");
572 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
575 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
576 if(maxtimeout <= 0) {
577 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
583 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
584 if(!strcasecmp(afname, "IPv4"))
585 addressfamily = AF_INET;
586 else if(!strcasecmp(afname, "IPv6"))
587 addressfamily = AF_INET6;
588 else if(!strcasecmp(afname, "any"))
589 addressfamily = AF_UNSPEC;
591 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
597 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
599 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
602 config_t *cfg = lookup_config(config_tree, "AutoConnect");
604 if(!get_config_bool(cfg, &autoconnect)) {
605 // Some backwards compatibility with when this option was an int
607 get_config_int(cfg, &val);
612 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
614 read_invitation_key();
620 Add listening sockets.
622 static bool add_listen_address(char *address, bool bindto) {
626 char *space = strchr(address, ' ');
632 if(!strcmp(address, "*"))
636 struct addrinfo *ai, hint = {0};
637 hint.ai_family = addressfamily;
638 hint.ai_socktype = SOCK_STREAM;
639 hint.ai_protocol = IPPROTO_TCP;
640 hint.ai_flags = AI_PASSIVE;
642 #if HAVE_DECL_RES_INIT
645 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
649 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
653 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
654 // Ignore duplicate addresses
657 for(int i = 0; i < listen_sockets; i++)
658 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
666 if(listen_sockets >= MAXSOCKETS) {
667 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
671 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
676 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
683 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
684 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
686 if(debug_level >= DEBUG_CONNECTIONS) {
687 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
688 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
692 listen_socket[listen_sockets].bindto = bindto;
693 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
701 void device_enable(void) {
705 /* Run tinc-up script to further initialize the tap interface */
708 environment_init(&env);
709 execute_script("tinc-up", &env);
710 environment_exit(&env);
713 void device_disable(void) {
715 environment_init(&env);
716 execute_script("tinc-down", &env);
717 environment_exit(&env);
724 Configure node_t myself and set up the local sockets (listen only)
726 static bool setup_myself(void) {
727 char *name, *hostname, *cipher, *digest, *type;
728 char *address = NULL;
729 bool port_specified = false;
731 if(!(name = get_name())) {
732 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
736 myname = xstrdup(name);
738 myself->connection = new_connection();
740 myself->connection->name = xstrdup(name);
741 read_host_config(config_tree, name);
743 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
744 myport = xstrdup("655");
746 port_specified = true;
748 myself->connection->options = 0;
749 myself->connection->protocol_major = PROT_MAJOR;
750 myself->connection->protocol_minor = PROT_MINOR;
752 myself->options |= PROT_MINOR << 24;
754 #ifdef DISABLE_LEGACY
755 experimental = read_ecdsa_private_key();
757 logger(DEBUG_ALWAYS, LOG_ERR, "No private key available, cannot start tinc!");
761 if(!get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental)) {
762 experimental = read_ecdsa_private_key();
764 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for SPTPS disabled.");
766 if(experimental && !read_ecdsa_private_key())
770 if(!read_rsa_private_key()) {
772 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for legacy protocol disabled.");
774 logger(DEBUG_ALWAYS, LOG_ERR, "No private keys available, cannot start tinc!");
780 /* Ensure myport is numeric */
783 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
785 if(!ai || !ai->ai_addr)
788 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
789 sockaddr2str(&sa, NULL, &myport);
792 /* Read in all the subnets specified in the host configuration file */
794 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
797 if(!get_config_subnet(cfg, &subnet))
800 subnet_add(myself, subnet);
803 /* Check some options */
805 if(!setup_myself_reloadable())
808 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
809 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
810 strictsubnets |= tunnelserver;
812 if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
813 if(max_connection_burst <= 0) {
814 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
819 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
821 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
826 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
828 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
834 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
835 if(replaywin_int < 0) {
836 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
839 replaywin = (unsigned)replaywin_int;
840 sptps_replaywin = replaywin;
843 #ifndef DISABLE_LEGACY
844 /* Generate packet encryption key */
846 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
847 cipher = xstrdup("aes-256-cbc");
849 if(!strcasecmp(cipher, "none")) {
850 myself->incipher = NULL;
851 } else if(!(myself->incipher = cipher_open_by_name(cipher))) {
852 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
858 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
860 /* Check if we want to use message authentication codes... */
863 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
866 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
870 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
871 digest = xstrdup("sha256");
873 if(!strcasecmp(digest, "none")) {
874 myself->indigest = NULL;
875 } else if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
876 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
885 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
886 if(myself->incompression < 0 || myself->incompression > 11) {
887 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
891 myself->incompression = 0;
893 myself->connection->outcompression = 0;
897 myself->nexthop = myself;
898 myself->via = myself;
899 myself->status.reachable = true;
900 myself->last_state_change = now.tv_sec;
901 myself->status.sptps = experimental;
912 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
913 if(!strcasecmp(type, "dummy"))
914 devops = dummy_devops;
915 else if(!strcasecmp(type, "raw_socket"))
916 devops = raw_socket_devops;
917 else if(!strcasecmp(type, "multicast"))
918 devops = multicast_devops;
919 else if(!strcasecmp(type, "fd"))
922 else if(!strcasecmp(type, "uml"))
926 else if(!strcasecmp(type, "vde"))
932 get_config_bool(lookup_config(config_tree, "DeviceStandby"), &device_standby);
938 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
942 if(!do_detach && getenv("LISTEN_FDS")) {
946 listen_sockets = atoi(getenv("LISTEN_FDS"));
948 unsetenv("LISTEN_FDS");
951 if(listen_sockets > MAXSOCKETS) {
952 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
956 for(int i = 0; i < listen_sockets; i++) {
958 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
959 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(sockerrno));
964 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
967 int udp_fd = setup_vpn_in_socket(&sa);
971 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
972 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
974 if(debug_level >= DEBUG_CONNECTIONS) {
975 hostname = sockaddr2hostname(&sa);
976 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
980 memcpy(&listen_socket[i].sa, &sa, salen);
986 for(config_t *cfg = lookup_config(config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
988 get_config_string(cfg, &address);
989 if(!add_listen_address(address, true))
993 for(config_t *cfg = lookup_config(config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
995 get_config_string(cfg, &address);
996 if(!add_listen_address(address, false))
1001 if(!add_listen_address(address, NULL))
1005 if(!listen_sockets) {
1006 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
1010 /* If no Port option was specified, set myport to the port used by the first listening socket. */
1012 if(!port_specified || atoi(myport) == 0) {
1014 socklen_t salen = sizeof sa;
1015 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
1017 sockaddr2str(&sa, NULL, &myport);
1019 myport = xstrdup("655");
1023 xasprintf(&myself->hostname, "MYSELF port %s", myport);
1024 myself->connection->hostname = xstrdup(myself->hostname);
1027 get_config_string(lookup_config(config_tree, "UPnP"), &upnp);
1028 bool upnp_tcp = false;
1029 bool upnp_udp = false;
1031 if (!strcasecmp(upnp, "yes"))
1032 upnp_tcp = upnp_udp = true;
1033 else if (!strcasecmp(upnp, "udponly"))
1037 if (upnp_tcp || upnp_udp) {
1038 #ifdef HAVE_MINIUPNPC
1039 upnp_init(upnp_tcp, upnp_udp);
1041 logger(DEBUG_ALWAYS, LOG_WARNING, "UPnP was requested, but tinc isn't built with miniupnpc support!");
1047 last_config_check = now.tv_sec;
1055 bool setup_network(void) {
1062 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
1063 if(pinginterval < 1) {
1064 pinginterval = 86400;
1069 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
1071 if(pingtimeout < 1 || pingtimeout > pinginterval)
1072 pingtimeout = pinginterval;
1074 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
1075 maxoutbufsize = 10 * MTU;
1083 if (!device_standby)
1086 /* Run subnet-up scripts for our own subnets */
1088 subnet_update(myself, NULL, true);
1094 close all open network connections
1096 void close_network_connections(void) {
1097 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1099 connection_t *c = node->data;
1100 /* Keep control connections open until the end, so they know when we really terminated */
1101 if(c->status.control)
1104 terminate_connection(c, false);
1108 list_delete_list(outgoing_list);
1110 if(myself && myself->connection) {
1111 subnet_update(myself, NULL, false);
1112 connection_del(myself->connection);
1115 for(int i = 0; i < listen_sockets; i++) {
1116 io_del(&listen_socket[i].tcp);
1117 io_del(&listen_socket[i].udp);
1118 close(listen_socket[i].tcp.fd);
1119 close(listen_socket[i].udp.fd);
1128 if (!device_standby)
1140 free(scriptextension);
1141 free(scriptinterpreter);