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"
29 #include "compression.h"
54 static io_t device_io;
56 bool device_standby = false;
58 char *proxyhost = NULL;
59 char *proxyport = NULL;
60 char *proxyuser = NULL;
61 char *proxypass = NULL;
63 proxytype_t proxytype;
65 bool disablebuggypeers;
67 char *scriptinterpreter;
68 char *scriptextension;
70 bool node_read_ecdsa_public_key(node_t *n) {
71 if(ecdsa_active(n->ecdsa)) {
80 init_configuration(&config);
82 if(!read_host_config(&config, n->name, true)) {
86 /* First, check for simple Ed25519PublicKey statement */
88 if(get_config_string(lookup_config(&config, "Ed25519PublicKey"), &p)) {
89 n->ecdsa = ecdsa_set_base64_public_key(p);
94 /* Else, check for Ed25519PublicKeyFile statement and read it */
96 if(!get_config_string(lookup_config(&config, "Ed25519PublicKeyFile"), &pubname)) {
97 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
100 fp = fopen(pubname, "r");
106 n->ecdsa = ecdsa_read_pem_public_key(fp);
110 splay_empty_tree(&config);
115 static bool read_invitation_key(void) {
117 char fname[PATH_MAX];
120 ecdsa_free(invitation_key);
121 invitation_key = NULL;
124 snprintf(fname, sizeof(fname), "%s" SLASH "invitations" SLASH "ed25519_key.priv", confbase);
126 fp = fopen(fname, "r");
129 invitation_key = ecdsa_read_pem_private_key(fp);
132 if(!invitation_key) {
133 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
137 return invitation_key;
140 #ifndef DISABLE_LEGACY
141 static timeout_t keyexpire_timeout;
143 static void keyexpire_handler(void *data) {
145 timeout_set(data, &(struct timeval) {
146 keylifetime, jitter()
151 void regenerate_key(void) {
152 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
155 for splay_each(node_t, n, &node_tree) {
156 n->status.validkey_in = false;
160 void load_all_nodes(void) {
163 char dname[PATH_MAX];
165 snprintf(dname, sizeof(dname), "%s" SLASH "hosts", confbase);
166 dir = opendir(dname);
169 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
173 while((ent = readdir(dir))) {
174 if(!check_id(ent->d_name)) {
178 node_t *n = lookup_node(ent->d_name);
181 init_configuration(&config);
182 read_config_options(&config, ent->d_name);
183 read_host_config(&config, ent->d_name, true);
187 n->name = xstrdup(ent->d_name);
192 for(config_t *cfg = lookup_config(&config, "Subnet"); cfg; cfg = lookup_config_next(&config, cfg)) {
195 if(!get_config_subnet(cfg, &s)) {
199 if((s2 = lookup_subnet(n, s))) {
208 if(lookup_config(&config, "Address")) {
209 n->status.has_address = true;
212 splay_empty_tree(&config);
218 char *get_name(void) {
222 get_config_string(lookup_config(&config_tree, "Name"), &name);
228 returned_name = replace_name(name);
230 return returned_name;
233 bool setup_myself_reloadable(void) {
234 free(scriptinterpreter);
235 scriptinterpreter = NULL;
237 get_config_string(lookup_config(&config_tree, "ScriptsInterpreter"), &scriptinterpreter);
239 free(scriptextension);
241 if(!get_config_string(lookup_config(&config_tree, "ScriptsExtension"), &scriptextension)) {
242 scriptextension = xstrdup("");
247 get_config_string(lookup_config(&config_tree, "Proxy"), &proxy);
252 if((space = strchr(proxy, ' '))) {
256 if(!strcasecmp(proxy, "none")) {
257 proxytype = PROXY_NONE;
258 } else if(!strcasecmp(proxy, "socks4")) {
259 proxytype = PROXY_SOCKS4;
260 } else if(!strcasecmp(proxy, "socks4a")) {
261 proxytype = PROXY_SOCKS4A;
262 } else if(!strcasecmp(proxy, "socks5")) {
263 proxytype = PROXY_SOCKS5;
264 } else if(!strcasecmp(proxy, "http")) {
265 proxytype = PROXY_HTTP;
266 } else if(!strcasecmp(proxy, "exec")) {
267 proxytype = PROXY_EXEC;
269 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
292 if(!space || !*space) {
293 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
298 proxyhost = xstrdup(space);
307 if(space && (space = strchr(space, ' '))) {
308 *space++ = 0, proxyport = space;
311 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
312 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
319 if(space && (space = strchr(space, ' '))) {
320 *space++ = 0, proxyuser = space;
323 if(space && (space = strchr(space, ' '))) {
324 *space++ = 0, proxypass = space;
327 proxyhost = xstrdup(proxyhost);
328 proxyport = xstrdup(proxyport);
330 if(proxyuser && *proxyuser) {
331 proxyuser = xstrdup(proxyuser);
334 if(proxypass && *proxypass) {
335 proxypass = xstrdup(proxypass);
346 if(get_config_bool(lookup_config(&config_tree, "IndirectData"), &choice) && choice) {
347 myself->options |= OPTION_INDIRECT;
350 if(get_config_bool(lookup_config(&config_tree, "TCPOnly"), &choice) && choice) {
351 myself->options |= OPTION_TCPONLY;
354 if(myself->options & OPTION_TCPONLY) {
355 myself->options |= OPTION_INDIRECT;
358 get_config_bool(lookup_config(&config_tree, "UDPDiscovery"), &udp_discovery);
359 get_config_int(lookup_config(&config_tree, "UDPDiscoveryKeepaliveInterval"), &udp_discovery_keepalive_interval);
360 get_config_int(lookup_config(&config_tree, "UDPDiscoveryInterval"), &udp_discovery_interval);
361 get_config_int(lookup_config(&config_tree, "UDPDiscoveryTimeout"), &udp_discovery_timeout);
363 get_config_int(lookup_config(&config_tree, "MTUInfoInterval"), &mtu_info_interval);
364 get_config_int(lookup_config(&config_tree, "UDPInfoInterval"), &udp_info_interval);
366 get_config_bool(lookup_config(&config_tree, "DirectOnly"), &directonly);
367 get_config_bool(lookup_config(&config_tree, "LocalDiscovery"), &localdiscovery);
371 if(get_config_string(lookup_config(&config_tree, "Mode"), &rmode)) {
372 if(!strcasecmp(rmode, "router")) {
373 routing_mode = RMODE_ROUTER;
374 } else if(!strcasecmp(rmode, "switch")) {
375 routing_mode = RMODE_SWITCH;
376 } else if(!strcasecmp(rmode, "hub")) {
377 routing_mode = RMODE_HUB;
379 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
389 if(get_config_string(lookup_config(&config_tree, "Forwarding"), &fmode)) {
390 if(!strcasecmp(fmode, "off")) {
391 forwarding_mode = FMODE_OFF;
392 } else if(!strcasecmp(fmode, "internal")) {
393 forwarding_mode = FMODE_INTERNAL;
394 } else if(!strcasecmp(fmode, "kernel")) {
395 forwarding_mode = FMODE_KERNEL;
397 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
405 choice = !(myself->options & OPTION_TCPONLY);
406 get_config_bool(lookup_config(&config_tree, "PMTUDiscovery"), &choice);
409 myself->options |= OPTION_PMTU_DISCOVERY;
413 get_config_bool(lookup_config(&config_tree, "ClampMSS"), &choice);
416 myself->options |= OPTION_CLAMP_MSS;
419 get_config_bool(lookup_config(&config_tree, "PriorityInheritance"), &priorityinheritance);
420 get_config_bool(lookup_config(&config_tree, "DecrementTTL"), &decrement_ttl);
424 if(get_config_string(lookup_config(&config_tree, "Broadcast"), &bmode)) {
425 if(!strcasecmp(bmode, "no")) {
426 broadcast_mode = BMODE_NONE;
427 } else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst")) {
428 broadcast_mode = BMODE_MST;
429 } else if(!strcasecmp(bmode, "direct")) {
430 broadcast_mode = BMODE_DIRECT;
432 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
440 /* Delete all broadcast subnets before re-adding them */
442 for splay_each(subnet_t, s, &subnet_tree) {
444 splay_delete_node(&subnet_tree, node);
448 const char *const DEFAULT_BROADCAST_SUBNETS[] = { "ff:ff:ff:ff:ff:ff", "255.255.255.255", "224.0.0.0/4", "ff00::/8" };
450 for(size_t i = 0; i < sizeof(DEFAULT_BROADCAST_SUBNETS) / sizeof(*DEFAULT_BROADCAST_SUBNETS); i++) {
451 subnet_t *s = new_subnet();
453 if(!str2net(s, DEFAULT_BROADCAST_SUBNETS[i])) {
460 for(config_t *cfg = lookup_config(&config_tree, "BroadcastSubnet"); cfg; cfg = lookup_config_next(&config_tree, cfg)) {
463 if(!get_config_subnet(cfg, &s)) {
472 if(priorityinheritance) {
473 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform for IPv4 connections", "PriorityInheritance");
478 #if !defined(IPV6_TCLASS)
480 if(priorityinheritance) {
481 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform for IPv6 connections", "PriorityInheritance");
486 if(!get_config_int(lookup_config(&config_tree, "MACExpire"), &macexpire)) {
490 if(get_config_int(lookup_config(&config_tree, "MaxTimeout"), &maxtimeout)) {
491 if(maxtimeout <= 0) {
492 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
501 if(get_config_string(lookup_config(&config_tree, "AddressFamily"), &afname)) {
502 if(!strcasecmp(afname, "IPv4")) {
503 addressfamily = AF_INET;
504 } else if(!strcasecmp(afname, "IPv6")) {
505 addressfamily = AF_INET6;
506 } else if(!strcasecmp(afname, "any")) {
507 addressfamily = AF_UNSPEC;
509 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
517 get_config_bool(lookup_config(&config_tree, "Hostnames"), &hostnames);
519 if(!get_config_int(lookup_config(&config_tree, "KeyExpire"), &keylifetime)) {
523 if(!get_config_bool(lookup_config(&config_tree, "AutoConnect"), &autoconnect)) {
527 get_config_bool(lookup_config(&config_tree, "DisableBuggyPeers"), &disablebuggypeers);
529 if(!get_config_int(lookup_config(&config_tree, "InvitationExpire"), &invitation_lifetime)) {
530 invitation_lifetime = 604800; // 1 week
533 read_invitation_key();
538 // Get the port that `from_fd` is listening on, and assign it to
539 // `sa` if `sa` has a dynamically allocated (zero) port.
540 static bool assign_static_port(sockaddr_t *sa, int from_fd) {
541 // We cannot get a port from a bad FD. Bail out.
546 int port = get_bound_port(from_fd);
552 // If the port is non-zero, don't reassign it as it's already static.
553 switch(sa->sa.sa_family) {
555 if(!sa->in.sin_port) {
556 sa->in.sin_port = htons(port);
562 if(!sa->in6.sin6_port) {
563 sa->in6.sin6_port = htons(port);
569 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown address family 0x%x", sa->sa.sa_family);
574 typedef int (*bind_fn_t)(const sockaddr_t *);
576 static int bind_reusing_port(const sockaddr_t *sa, int from_fd, bind_fn_t setup) {
578 memcpy(&reuse_sa, sa, SALEN(sa->sa));
582 // Check if the address we've been passed here is using port 0.
583 // If it is, try to get an actual port from an already bound socket, and reuse it here.
584 if(assign_static_port(&reuse_sa, from_fd)) {
585 fd = setup(&reuse_sa);
588 // If we're binding to a hardcoded non-zero port, or no socket is listening yet,
589 // or binding failed, try the original address.
598 Add listening sockets.
600 static bool add_listen_address(char *address, bool bindto) {
601 char *port = myport.tcp;
604 char *space = strchr(address, ' ');
611 if(!strcmp(address, "*")) {
616 struct addrinfo *ai, hint = {0};
618 hint.ai_family = addressfamily;
620 hint.ai_socktype = SOCK_STREAM;
622 hint.ai_protocol = IPPROTO_TCP;
624 hint.ai_flags = AI_PASSIVE;
626 #if HAVE_DECL_RES_INIT
630 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
635 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
639 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
640 // Ignore duplicate addresses
643 for(int i = 0; i < listen_sockets; i++)
644 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
653 if(listen_sockets >= MAXSOCKETS) {
654 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
659 const sockaddr_t *sa = (sockaddr_t *) aip->ai_addr;
660 int from_fd = listen_socket[0].tcp.fd;
662 // If we're binding to a dynamically allocated (zero) port, try to get the actual
663 // port of the first TCP socket, and use it for this one. If that succeeds, our
664 // tincd instance will use the same port for all addresses it listens on.
665 int tcp_fd = bind_reusing_port(sa, from_fd, setup_listen_socket);
671 // If we just successfully bound the first socket, use it for the UDP procedure below.
672 // Otherwise, keep using the socket we've obtained from listen_socket[0].
677 int udp_fd = bind_reusing_port(sa, from_fd, setup_vpn_in_socket);
684 listen_socket_t *sock = &listen_socket[listen_sockets];
685 io_add(&sock->tcp, handle_new_meta_connection, sock, tcp_fd, IO_READ);
686 io_add(&sock->udp, handle_incoming_vpn_data, sock, udp_fd, IO_READ);
688 if(debug_level >= DEBUG_CONNECTIONS) {
689 int tcp_port = get_bound_port(tcp_fd);
690 char *hostname = NULL;
691 sockaddr2str(sa, &hostname, NULL);
692 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s port %d", hostname, tcp_port);
696 sock->bindto = bindto;
697 memcpy(&sock->sa, aip->ai_addr, aip->ai_addrlen);
705 void device_enable(void) {
710 /* Run tinc-up script to further initialize the tap interface */
713 environment_init(&env);
714 execute_script("tinc-up", &env);
715 environment_exit(&env);
718 void device_disable(void) {
720 environment_init(&env);
721 execute_script("tinc-down", &env);
722 environment_exit(&env);
730 Configure node_t myself and set up the local sockets (listen only)
732 static bool setup_myself(void) {
734 char *address = NULL;
735 bool port_specified = false;
737 if(!(name = get_name())) {
738 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
742 myname = xstrdup(name);
744 myself->connection = new_connection();
746 myself->connection->name = xstrdup(name);
747 read_host_config(&config_tree, name, true);
749 if(!get_config_string(lookup_config(&config_tree, "Port"), &myport.tcp)) {
750 myport.tcp = xstrdup("655");
752 port_specified = true;
755 myport.udp = xstrdup(myport.tcp);
757 myself->connection->options = 0;
758 myself->connection->protocol_major = PROT_MAJOR;
759 myself->connection->protocol_minor = PROT_MINOR;
761 myself->options |= PROT_MINOR << 24;
763 #ifdef DISABLE_LEGACY
764 myself->connection->ecdsa = read_ecdsa_private_key(&config_tree, NULL);
765 experimental = myself->connection->ecdsa != NULL;
768 logger(DEBUG_ALWAYS, LOG_ERR, "No private key available, cannot start tinc!");
774 if(!get_config_bool(lookup_config(&config_tree, "ExperimentalProtocol"), &experimental)) {
775 myself->connection->ecdsa = read_ecdsa_private_key(&config_tree, NULL);
776 experimental = myself->connection->ecdsa != NULL;
779 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for SPTPS disabled.");
783 myself->connection->ecdsa = read_ecdsa_private_key(&config_tree, NULL);
785 if(!myself->connection->ecdsa) {
791 rsa_t *rsa = read_rsa_private_key(&config_tree, NULL);
794 myself->connection->legacy = new_legacy_ctx(rsa);
797 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for legacy protocol disabled.");
799 logger(DEBUG_ALWAYS, LOG_ERR, "No private keys available, cannot start tinc!");
806 /* Ensure myport is numeric */
807 if(!is_decimal(myport.tcp)) {
808 uint16_t port = service_to_port(myport.tcp);
815 myport.tcp = int_to_str(port);
818 myport.udp = xstrdup(myport.tcp);
821 /* Read in all the subnets specified in the host configuration file */
823 for(config_t *cfg = lookup_config(&config_tree, "Subnet"); cfg; cfg = lookup_config_next(&config_tree, cfg)) {
826 if(!get_config_subnet(cfg, &subnet)) {
830 subnet_add(myself, subnet);
833 /* Check some options */
835 if(!setup_myself_reloadable()) {
839 get_config_bool(lookup_config(&config_tree, "StrictSubnets"), &strictsubnets);
840 get_config_bool(lookup_config(&config_tree, "TunnelServer"), &tunnelserver);
841 strictsubnets |= tunnelserver;
843 if(get_config_int(lookup_config(&config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
844 if(max_connection_burst <= 0) {
845 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
850 if(get_config_int(lookup_config(&config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
852 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
856 udp_rcvbuf_warnings = true;
859 if(get_config_int(lookup_config(&config_tree, "UDPSndBuf"), &udp_sndbuf)) {
861 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
865 udp_sndbuf_warnings = true;
868 get_config_int(lookup_config(&config_tree, "FWMark"), &fwmark);
872 logger(DEBUG_ALWAYS, LOG_ERR, "FWMark not supported on this platform!");
880 if(get_config_int(lookup_config(&config_tree, "ReplayWindow"), &replaywin_int)) {
881 if(replaywin_int < 0) {
882 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
886 replaywin = (unsigned)replaywin_int;
887 sptps_replaywin = replaywin;
890 #ifndef DISABLE_LEGACY
891 /* Generate packet encryption key */
895 if(!get_config_string(lookup_config(&config_tree, "Cipher"), &cipher)) {
896 cipher = xstrdup("aes-256-cbc");
899 if(!strcasecmp(cipher, "none")) {
900 myself->incipher = NULL;
902 myself->incipher = cipher_alloc();
904 if(!cipher_open_by_name(myself->incipher, cipher)) {
905 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
906 cipher_free(&myself->incipher);
914 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval) {
915 keylifetime, jitter()
918 /* Check if we want to use message authentication codes... */
921 get_config_int(lookup_config(&config_tree, "MACLength"), &maclength);
924 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
930 if(!get_config_string(lookup_config(&config_tree, "Digest"), &digest)) {
931 digest = xstrdup("sha256");
934 if(!strcasecmp(digest, "none")) {
935 myself->indigest = NULL;
937 myself->indigest = digest_alloc();
939 if(!digest_open_by_name(myself->indigest, digest, maclength)) {
940 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
941 digest_free(&myself->indigest);
951 int incompression = 0;
953 if(get_config_int(lookup_config(&config_tree, "Compression"), &incompression)) {
954 myself->incompression = incompression;
956 switch(myself->incompression) {
961 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
962 logger(DEBUG_ALWAYS, LOG_ERR, "LZ4 compression is unavailable on this node.");
966 case COMPRESS_LZO_HI:
967 case COMPRESS_LZO_LO:
971 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
972 logger(DEBUG_ALWAYS, LOG_ERR, "LZO compression is unavailable on this node.");
976 case COMPRESS_ZLIB_9:
977 case COMPRESS_ZLIB_8:
978 case COMPRESS_ZLIB_7:
979 case COMPRESS_ZLIB_6:
980 case COMPRESS_ZLIB_5:
981 case COMPRESS_ZLIB_4:
982 case COMPRESS_ZLIB_3:
983 case COMPRESS_ZLIB_2:
984 case COMPRESS_ZLIB_1:
988 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
989 logger(DEBUG_ALWAYS, LOG_ERR, "ZLIB compression is unavailable on this node.");
997 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
998 logger(DEBUG_ALWAYS, LOG_ERR, "Compression level %i is unrecognized by this node.", myself->incompression);
1002 myself->incompression = COMPRESS_NONE;
1007 myself->nexthop = myself;
1008 myself->via = myself;
1009 myself->status.reachable = true;
1010 myself->last_state_change = now.tv_sec;
1011 myself->status.sptps = experimental;
1022 if(get_config_string(lookup_config(&config_tree, "DeviceType"), &type)) {
1023 if(!strcasecmp(type, "dummy")) {
1024 devops = dummy_devops;
1025 } else if(!strcasecmp(type, "raw_socket")) {
1026 devops = raw_socket_devops;
1027 } else if(!strcasecmp(type, "multicast")) {
1028 devops = multicast_devops;
1031 #ifdef HAVE_SYS_UN_H
1032 else if(!strcasecmp(type, "fd")) {
1038 else if(!strcasecmp(type, "uml")) {
1039 devops = uml_devops;
1044 else if(!strcasecmp(type, "vde")) {
1045 devops = vde_devops;
1052 get_config_bool(lookup_config(&config_tree, "DeviceStandby"), &device_standby);
1054 if(!devops.setup()) {
1058 if(device_fd >= 0) {
1059 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
1064 if(!do_detach && getenv("LISTEN_FDS")) {
1068 listen_sockets = atoi(getenv("LISTEN_FDS"));
1069 #ifdef HAVE_UNSETENV
1070 unsetenv("LISTEN_FDS");
1073 if(listen_sockets > MAXSOCKETS) {
1074 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
1078 for(int i = 0; i < listen_sockets; i++) {
1079 const int tcp_fd = i + 3;
1082 if(getsockname(tcp_fd, &sa.sa, &salen) < 0) {
1083 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", tcp_fd, sockstrerror(sockerrno));
1088 fcntl(tcp_fd, F_SETFD, FD_CLOEXEC);
1091 int udp_fd = setup_vpn_in_socket(&sa);
1097 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], tcp_fd, IO_READ);
1098 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
1100 if(debug_level >= DEBUG_CONNECTIONS) {
1101 char *hostname = sockaddr2hostname(&sa);
1102 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
1106 memcpy(&listen_socket[i].sa, &sa, salen);
1112 for(config_t *cfg = lookup_config(&config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(&config_tree, cfg)) {
1114 get_config_string(cfg, &address);
1116 if(!add_listen_address(address, true)) {
1121 for(config_t *cfg = lookup_config(&config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(&config_tree, cfg)) {
1123 get_config_string(cfg, &address);
1125 if(!add_listen_address(address, false)) {
1131 if(!add_listen_address(address, NULL)) {
1136 if(!listen_sockets) {
1137 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
1141 /* If no Port option was specified, set myport to the port used by the first listening socket. */
1143 if(!port_specified || atoi(myport.tcp) == 0) {
1144 listen_socket_t *sock = &listen_socket[0];
1146 uint16_t tcp = get_bound_port(sock->tcp.fd);
1148 myport.tcp = int_to_str(tcp);
1150 uint16_t udp = get_bound_port(sock->udp.fd);
1152 myport.udp = int_to_str(udp);
1155 xasprintf(&myself->hostname, "MYSELF port %s", myport.tcp);
1156 myself->connection->hostname = xstrdup(myself->hostname);
1159 get_config_string(lookup_config(&config_tree, "UPnP"), &upnp);
1160 bool upnp_tcp = false;
1161 bool upnp_udp = false;
1164 if(!strcasecmp(upnp, "yes")) {
1165 upnp_tcp = upnp_udp = true;
1166 } else if(!strcasecmp(upnp, "udponly")) {
1173 if(upnp_tcp || upnp_udp) {
1174 #ifdef HAVE_MINIUPNPC
1175 upnp_init(upnp_tcp, upnp_udp);
1177 logger(DEBUG_ALWAYS, LOG_WARNING, "UPnP was requested, but tinc isn't built with miniupnpc support!");
1183 last_config_check = now.tv_sec;
1191 bool setup_network(void) {
1195 if(get_config_int(lookup_config(&config_tree, "PingInterval"), &pinginterval)) {
1196 if(pinginterval < 1) {
1197 pinginterval = 86400;
1203 if(!get_config_int(lookup_config(&config_tree, "PingTimeout"), &pingtimeout)) {
1207 if(pingtimeout < 1 || pingtimeout > pinginterval) {
1208 pingtimeout = pinginterval;
1211 if(!get_config_int(lookup_config(&config_tree, "MaxOutputBufferSize"), &maxoutbufsize)) {
1212 maxoutbufsize = 10 * MTU;
1215 if(!setup_myself()) {
1219 if(!init_control()) {
1223 if(!device_standby) {
1227 /* Run subnet-up scripts for our own subnets */
1229 subnet_update(myself, NULL, true);
1235 close all open network connections
1237 void close_network_connections(void) {
1238 for(list_node_t *node = connection_list.head, *next; node; node = next) {
1240 connection_t *c = node->data;
1242 /* Keep control connections open until the end, so they know when we really terminated */
1243 if(c->status.control) {
1248 terminate_connection(c, false);
1251 list_empty_list(&outgoing_list);
1253 if(myself && myself->connection) {
1254 subnet_update(myself, NULL, false);
1255 free_connection(myself->connection);
1258 for(int i = 0; i < listen_sockets; i++) {
1259 io_del(&listen_socket[i].tcp);
1260 io_del(&listen_socket[i].udp);
1261 closesocket(listen_socket[i].tcp.fd);
1262 closesocket(listen_socket[i].udp.fd);
1271 if(!device_standby) {
1278 if(device_fd >= 0) {
1288 free(scriptextension);
1289 free(scriptinterpreter);