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.
28 #include "connection.h"
52 static io_t device_io;
54 bool device_standby = false;
56 char *proxyhost = NULL;
57 char *proxyport = NULL;
58 char *proxyuser = NULL;
59 char *proxypass = NULL;
61 proxytype_t proxytype;
63 bool disablebuggypeers;
65 char *scriptinterpreter;
66 char *scriptextension;
68 bool node_read_ecdsa_public_key(node_t *n) {
69 if(ecdsa_active(n->ecdsa)) {
73 splay_tree_t *config_tree;
78 init_configuration(&config_tree);
80 if(!read_host_config(config_tree, n->name, true)) {
84 /* First, check for simple Ed25519PublicKey statement */
86 if(get_config_string(lookup_config(config_tree, "Ed25519PublicKey"), &p)) {
87 n->ecdsa = ecdsa_set_base64_public_key(p);
92 /* Else, check for Ed25519PublicKeyFile statement and read it */
94 if(!get_config_string(lookup_config(config_tree, "Ed25519PublicKeyFile"), &pubname)) {
95 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
98 fp = fopen(pubname, "r");
104 n->ecdsa = ecdsa_read_pem_public_key(fp);
108 exit_configuration(&config_tree);
113 static bool read_invitation_key(void) {
115 char fname[PATH_MAX];
118 ecdsa_free(invitation_key);
119 invitation_key = NULL;
122 snprintf(fname, sizeof(fname), "%s" SLASH "invitations" SLASH "ed25519_key.priv", confbase);
124 fp = fopen(fname, "r");
127 invitation_key = ecdsa_read_pem_private_key(fp);
130 if(!invitation_key) {
131 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
135 return invitation_key;
138 #ifndef DISABLE_LEGACY
139 static timeout_t keyexpire_timeout;
141 static void keyexpire_handler(void *data) {
143 timeout_set(data, &(struct timeval) {
144 keylifetime, rand() % 100000
149 void regenerate_key(void) {
150 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
153 for splay_each(node_t, n, node_tree) {
154 n->status.validkey_in = false;
158 void load_all_nodes(void) {
161 char dname[PATH_MAX];
163 snprintf(dname, sizeof(dname), "%s" SLASH "hosts", confbase);
164 dir = opendir(dname);
167 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
171 while((ent = readdir(dir))) {
172 if(!check_id(ent->d_name)) {
176 node_t *n = lookup_node(ent->d_name);
178 splay_tree_t *config_tree;
179 init_configuration(&config_tree);
180 read_config_options(config_tree, ent->d_name);
181 read_host_config(config_tree, ent->d_name, true);
185 n->name = xstrdup(ent->d_name);
190 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
193 if(!get_config_subnet(cfg, &s)) {
197 if((s2 = lookup_subnet(n, s))) {
206 if(lookup_config(config_tree, "Address")) {
207 n->status.has_address = true;
210 exit_configuration(&config_tree);
216 char *get_name(void) {
220 get_config_string(lookup_config(config_tree, "Name"), &name);
226 returned_name = replace_name(name);
228 return returned_name;
231 bool setup_myself_reloadable(void) {
232 free(scriptinterpreter);
233 scriptinterpreter = NULL;
235 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
237 free(scriptextension);
239 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension)) {
240 scriptextension = xstrdup("");
245 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
250 if((space = strchr(proxy, ' '))) {
254 if(!strcasecmp(proxy, "none")) {
255 proxytype = PROXY_NONE;
256 } else if(!strcasecmp(proxy, "socks4")) {
257 proxytype = PROXY_SOCKS4;
258 } else if(!strcasecmp(proxy, "socks4a")) {
259 proxytype = PROXY_SOCKS4A;
260 } else if(!strcasecmp(proxy, "socks5")) {
261 proxytype = PROXY_SOCKS5;
262 } else if(!strcasecmp(proxy, "http")) {
263 proxytype = PROXY_HTTP;
264 } else if(!strcasecmp(proxy, "exec")) {
265 proxytype = PROXY_EXEC;
267 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
290 if(!space || !*space) {
291 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
296 proxyhost = xstrdup(space);
305 if(space && (space = strchr(space, ' '))) {
306 *space++ = 0, proxyport = space;
309 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
310 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
317 if(space && (space = strchr(space, ' '))) {
318 *space++ = 0, proxyuser = space;
321 if(space && (space = strchr(space, ' '))) {
322 *space++ = 0, proxypass = space;
325 proxyhost = xstrdup(proxyhost);
326 proxyport = xstrdup(proxyport);
328 if(proxyuser && *proxyuser) {
329 proxyuser = xstrdup(proxyuser);
332 if(proxypass && *proxypass) {
333 proxypass = xstrdup(proxypass);
344 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice) {
345 myself->options |= OPTION_INDIRECT;
348 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice) {
349 myself->options |= OPTION_TCPONLY;
352 if(myself->options & OPTION_TCPONLY) {
353 myself->options |= OPTION_INDIRECT;
356 get_config_bool(lookup_config(config_tree, "UDPDiscovery"), &udp_discovery);
357 get_config_int(lookup_config(config_tree, "UDPDiscoveryKeepaliveInterval"), &udp_discovery_keepalive_interval);
358 get_config_int(lookup_config(config_tree, "UDPDiscoveryInterval"), &udp_discovery_interval);
359 get_config_int(lookup_config(config_tree, "UDPDiscoveryTimeout"), &udp_discovery_timeout);
361 get_config_int(lookup_config(config_tree, "MTUInfoInterval"), &mtu_info_interval);
362 get_config_int(lookup_config(config_tree, "UDPInfoInterval"), &udp_info_interval);
364 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
365 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
369 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
370 if(!strcasecmp(rmode, "router")) {
371 routing_mode = RMODE_ROUTER;
372 } else if(!strcasecmp(rmode, "switch")) {
373 routing_mode = RMODE_SWITCH;
374 } else if(!strcasecmp(rmode, "hub")) {
375 routing_mode = RMODE_HUB;
377 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
387 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
388 if(!strcasecmp(fmode, "off")) {
389 forwarding_mode = FMODE_OFF;
390 } else if(!strcasecmp(fmode, "internal")) {
391 forwarding_mode = FMODE_INTERNAL;
392 } else if(!strcasecmp(fmode, "kernel")) {
393 forwarding_mode = FMODE_KERNEL;
395 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
403 choice = !(myself->options & OPTION_TCPONLY);
404 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
407 myself->options |= OPTION_PMTU_DISCOVERY;
411 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
414 myself->options |= OPTION_CLAMP_MSS;
417 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
418 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
422 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
423 if(!strcasecmp(bmode, "no")) {
424 broadcast_mode = BMODE_NONE;
425 } else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst")) {
426 broadcast_mode = BMODE_MST;
427 } else if(!strcasecmp(bmode, "direct")) {
428 broadcast_mode = BMODE_DIRECT;
430 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
438 /* Delete all broadcast subnets before re-adding them */
440 for splay_each(subnet_t, s, subnet_tree) {
442 splay_delete_node(subnet_tree, node);
446 const char *const DEFAULT_BROADCAST_SUBNETS[] = { "ff:ff:ff:ff:ff:ff", "255.255.255.255", "224.0.0.0/4", "ff00::/8" };
448 for(size_t i = 0; i < sizeof(DEFAULT_BROADCAST_SUBNETS) / sizeof(*DEFAULT_BROADCAST_SUBNETS); i++) {
449 subnet_t *s = new_subnet();
451 if(!str2net(s, DEFAULT_BROADCAST_SUBNETS[i])) {
458 for(config_t *cfg = lookup_config(config_tree, "BroadcastSubnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
461 if(!get_config_subnet(cfg, &s)) {
470 if(priorityinheritance) {
471 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform for IPv4 connections", "PriorityInheritance");
476 #if !defined(IPV6_TCLASS)
478 if(priorityinheritance) {
479 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform for IPv6 connections", "PriorityInheritance");
484 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire)) {
488 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
489 if(maxtimeout <= 0) {
490 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
499 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
500 if(!strcasecmp(afname, "IPv4")) {
501 addressfamily = AF_INET;
502 } else if(!strcasecmp(afname, "IPv6")) {
503 addressfamily = AF_INET6;
504 } else if(!strcasecmp(afname, "any")) {
505 addressfamily = AF_UNSPEC;
507 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
515 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
517 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime)) {
521 if(!get_config_bool(lookup_config(config_tree, "AutoConnect"), &autoconnect)) {
525 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
527 if(!get_config_int(lookup_config(config_tree, "InvitationExpire"), &invitation_lifetime)) {
528 invitation_lifetime = 604800; // 1 week
531 read_invitation_key();
537 Add listening sockets.
539 static bool add_listen_address(char *address, bool bindto) {
543 char *space = strchr(address, ' ');
550 if(!strcmp(address, "*")) {
555 struct addrinfo *ai, hint = {0};
557 hint.ai_family = addressfamily;
559 hint.ai_socktype = SOCK_STREAM;
561 hint.ai_protocol = IPPROTO_TCP;
563 hint.ai_flags = AI_PASSIVE;
565 #if HAVE_DECL_RES_INIT
569 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
574 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
578 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
579 // Ignore duplicate addresses
582 for(int i = 0; i < listen_sockets; i++)
583 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
592 if(listen_sockets >= MAXSOCKETS) {
593 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
598 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
604 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
611 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
612 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
614 if(debug_level >= DEBUG_CONNECTIONS) {
615 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
616 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
620 listen_socket[listen_sockets].bindto = bindto;
621 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
629 void device_enable(void) {
634 /* Run tinc-up script to further initialize the tap interface */
637 environment_init(&env);
638 execute_script("tinc-up", &env);
639 environment_exit(&env);
642 void device_disable(void) {
644 environment_init(&env);
645 execute_script("tinc-down", &env);
646 environment_exit(&env);
654 Configure node_t myself and set up the local sockets (listen only)
656 static bool setup_myself(void) {
657 char *name, *hostname, *type;
658 char *address = NULL;
659 bool port_specified = false;
661 if(!(name = get_name())) {
662 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
666 myname = xstrdup(name);
668 myself->connection = new_connection();
670 myself->connection->name = xstrdup(name);
671 read_host_config(config_tree, name, true);
673 if(!get_config_string(lookup_config(config_tree, "Port"), &myport)) {
674 myport = xstrdup("655");
676 port_specified = true;
679 myself->connection->options = 0;
680 myself->connection->protocol_major = PROT_MAJOR;
681 myself->connection->protocol_minor = PROT_MINOR;
683 myself->options |= PROT_MINOR << 24;
685 #ifdef DISABLE_LEGACY
686 myself->connection->ecdsa = read_ecdsa_private_key(config_tree, NULL);
687 experimental = myself->connection->ecdsa != NULL;
690 logger(DEBUG_ALWAYS, LOG_ERR, "No private key available, cannot start tinc!");
696 if(!get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental)) {
697 myself->connection->ecdsa = read_ecdsa_private_key(config_tree, NULL);
698 experimental = myself->connection->ecdsa != NULL;
701 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for SPTPS disabled.");
705 myself->connection->ecdsa = read_ecdsa_private_key(config_tree, NULL);
707 if(!myself->connection->ecdsa) {
713 myself->connection->rsa = read_rsa_private_key(config_tree, NULL);
715 if(!myself->connection->rsa) {
717 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for legacy protocol disabled.");
719 logger(DEBUG_ALWAYS, LOG_ERR, "No private keys available, cannot start tinc!");
726 /* Ensure myport is numeric */
729 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
732 if(!ai || !ai->ai_addr) {
737 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
739 sockaddr2str(&sa, NULL, &myport);
742 /* Read in all the subnets specified in the host configuration file */
744 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
747 if(!get_config_subnet(cfg, &subnet)) {
751 subnet_add(myself, subnet);
754 /* Check some options */
756 if(!setup_myself_reloadable()) {
760 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
761 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
762 strictsubnets |= tunnelserver;
764 if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
765 if(max_connection_burst <= 0) {
766 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
771 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
773 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
777 udp_rcvbuf_warnings = true;
780 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
782 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
786 udp_sndbuf_warnings = true;
789 get_config_int(lookup_config(config_tree, "FWMark"), &fwmark);
793 logger(DEBUG_ALWAYS, LOG_ERR, "FWMark not supported on this platform!");
801 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
802 if(replaywin_int < 0) {
803 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
807 replaywin = (unsigned)replaywin_int;
808 sptps_replaywin = replaywin;
811 #ifndef DISABLE_LEGACY
812 /* Generate packet encryption key */
816 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher)) {
817 cipher = xstrdup("aes-256-cbc");
820 if(!strcasecmp(cipher, "none")) {
821 myself->incipher = NULL;
822 } else if(!(myself->incipher = cipher_open_by_name(cipher))) {
823 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
830 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval) {
831 keylifetime, rand() % 100000
834 /* Check if we want to use message authentication codes... */
837 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
840 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
846 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest)) {
847 digest = xstrdup("sha256");
850 if(!strcasecmp(digest, "none")) {
851 myself->indigest = NULL;
852 } else if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
853 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
863 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
864 switch(myself->incompression) {
869 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
870 logger(DEBUG_ALWAYS, LOG_ERR, "LZ4 compression is unavailable on this node.");
879 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
880 logger(DEBUG_ALWAYS, LOG_ERR, "LZO compression is unavailable on this node.");
896 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
897 logger(DEBUG_ALWAYS, LOG_ERR, "ZLIB compression is unavailable on this node.");
905 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
906 logger(DEBUG_ALWAYS, LOG_ERR, "Compression level %i is unrecognized by this node.", myself->incompression);
910 myself->incompression = 0;
913 myself->connection->outcompression = 0;
917 myself->nexthop = myself;
918 myself->via = myself;
919 myself->status.reachable = true;
920 myself->last_state_change = now.tv_sec;
921 myself->status.sptps = experimental;
932 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
933 if(!strcasecmp(type, "dummy")) {
934 devops = dummy_devops;
935 } else if(!strcasecmp(type, "raw_socket")) {
936 devops = raw_socket_devops;
937 } else if(!strcasecmp(type, "multicast")) {
938 devops = multicast_devops;
942 else if(!strcasecmp(type, "fd")) {
948 else if(!strcasecmp(type, "uml")) {
954 else if(!strcasecmp(type, "vde")) {
962 get_config_bool(lookup_config(config_tree, "DeviceStandby"), &device_standby);
964 if(!devops.setup()) {
969 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
974 if(!do_detach && getenv("LISTEN_FDS")) {
978 listen_sockets = atoi(getenv("LISTEN_FDS"));
980 unsetenv("LISTEN_FDS");
983 if(listen_sockets > MAXSOCKETS) {
984 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
988 for(int i = 0; i < listen_sockets; i++) {
991 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
992 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(sockerrno));
997 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
1000 int udp_fd = setup_vpn_in_socket(&sa);
1006 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
1007 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
1009 if(debug_level >= DEBUG_CONNECTIONS) {
1010 hostname = sockaddr2hostname(&sa);
1011 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
1015 memcpy(&listen_socket[i].sa, &sa, salen);
1021 for(config_t *cfg = lookup_config(config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1023 get_config_string(cfg, &address);
1025 if(!add_listen_address(address, true)) {
1030 for(config_t *cfg = lookup_config(config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1032 get_config_string(cfg, &address);
1034 if(!add_listen_address(address, false)) {
1040 if(!add_listen_address(address, NULL)) {
1045 if(!listen_sockets) {
1046 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
1050 /* If no Port option was specified, set myport to the port used by the first listening socket. */
1052 if(!port_specified || atoi(myport) == 0) {
1054 socklen_t salen = sizeof(sa);
1056 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
1058 sockaddr2str(&sa, NULL, &myport);
1061 myport = xstrdup("655");
1066 xasprintf(&myself->hostname, "MYSELF port %s", myport);
1067 myself->connection->hostname = xstrdup(myself->hostname);
1070 get_config_string(lookup_config(config_tree, "UPnP"), &upnp);
1071 bool upnp_tcp = false;
1072 bool upnp_udp = false;
1075 if(!strcasecmp(upnp, "yes")) {
1076 upnp_tcp = upnp_udp = true;
1077 } else if(!strcasecmp(upnp, "udponly")) {
1084 if(upnp_tcp || upnp_udp) {
1085 #ifdef HAVE_MINIUPNPC
1086 upnp_init(upnp_tcp, upnp_udp);
1088 logger(DEBUG_ALWAYS, LOG_WARNING, "UPnP was requested, but tinc isn't built with miniupnpc support!");
1094 last_config_check = now.tv_sec;
1102 bool setup_network(void) {
1109 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
1110 if(pinginterval < 1) {
1111 pinginterval = 86400;
1117 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout)) {
1121 if(pingtimeout < 1 || pingtimeout > pinginterval) {
1122 pingtimeout = pinginterval;
1125 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize)) {
1126 maxoutbufsize = 10 * MTU;
1129 if(!setup_myself()) {
1133 if(!init_control()) {
1137 if(!device_standby) {
1141 /* Run subnet-up scripts for our own subnets */
1143 subnet_update(myself, NULL, true);
1149 close all open network connections
1151 void close_network_connections(void) {
1152 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1154 connection_t *c = node->data;
1156 /* Keep control connections open until the end, so they know when we really terminated */
1157 if(c->status.control) {
1162 terminate_connection(c, false);
1166 list_delete_list(outgoing_list);
1169 if(myself && myself->connection) {
1170 subnet_update(myself, NULL, false);
1171 free_connection(myself->connection);
1174 for(int i = 0; i < listen_sockets; i++) {
1175 io_del(&listen_socket[i].tcp);
1176 io_del(&listen_socket[i].udp);
1177 close(listen_socket[i].tcp.fd);
1178 close(listen_socket[i].udp.fd);
1187 if(!device_standby) {
1193 if(device_fd >= 0) {
1203 free(scriptextension);
1204 free(scriptinterpreter);