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 if(!get_config_int(lookup_config(config_tree, "InvitationExpire"), &invitation_lifetime))
615 invitation_lifetime = 604800; // 1 week
617 read_invitation_key();
623 Add listening sockets.
625 static bool add_listen_address(char *address, bool bindto) {
629 char *space = strchr(address, ' ');
635 if(!strcmp(address, "*"))
639 struct addrinfo *ai, hint = {0};
640 hint.ai_family = addressfamily;
641 hint.ai_socktype = SOCK_STREAM;
642 hint.ai_protocol = IPPROTO_TCP;
643 hint.ai_flags = AI_PASSIVE;
645 #if HAVE_DECL_RES_INIT
648 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
652 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
656 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
657 // Ignore duplicate addresses
660 for(int i = 0; i < listen_sockets; i++)
661 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
669 if(listen_sockets >= MAXSOCKETS) {
670 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
674 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
679 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
686 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
687 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
689 if(debug_level >= DEBUG_CONNECTIONS) {
690 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
691 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
695 listen_socket[listen_sockets].bindto = bindto;
696 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
704 void device_enable(void) {
708 /* Run tinc-up script to further initialize the tap interface */
711 environment_init(&env);
712 execute_script("tinc-up", &env);
713 environment_exit(&env);
716 void device_disable(void) {
718 environment_init(&env);
719 execute_script("tinc-down", &env);
720 environment_exit(&env);
727 Configure node_t myself and set up the local sockets (listen only)
729 static bool setup_myself(void) {
730 char *name, *hostname, *cipher, *digest, *type;
731 char *address = NULL;
732 bool port_specified = false;
734 if(!(name = get_name())) {
735 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
739 myname = xstrdup(name);
741 myself->connection = new_connection();
743 myself->connection->name = xstrdup(name);
744 read_host_config(config_tree, name);
746 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
747 myport = xstrdup("655");
749 port_specified = true;
751 myself->connection->options = 0;
752 myself->connection->protocol_major = PROT_MAJOR;
753 myself->connection->protocol_minor = PROT_MINOR;
755 myself->options |= PROT_MINOR << 24;
757 #ifdef DISABLE_LEGACY
758 experimental = read_ecdsa_private_key();
760 logger(DEBUG_ALWAYS, LOG_ERR, "No private key available, cannot start tinc!");
764 if(!get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental)) {
765 experimental = read_ecdsa_private_key();
767 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for SPTPS disabled.");
769 if(experimental && !read_ecdsa_private_key())
773 if(!read_rsa_private_key()) {
775 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for legacy protocol disabled.");
777 logger(DEBUG_ALWAYS, LOG_ERR, "No private keys available, cannot start tinc!");
783 /* Ensure myport is numeric */
786 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
788 if(!ai || !ai->ai_addr)
791 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
792 sockaddr2str(&sa, NULL, &myport);
795 /* Read in all the subnets specified in the host configuration file */
797 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
800 if(!get_config_subnet(cfg, &subnet))
803 subnet_add(myself, subnet);
806 /* Check some options */
808 if(!setup_myself_reloadable())
811 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
812 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
813 strictsubnets |= tunnelserver;
815 if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
816 if(max_connection_burst <= 0) {
817 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
822 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
824 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
829 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
831 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
837 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
838 if(replaywin_int < 0) {
839 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
842 replaywin = (unsigned)replaywin_int;
843 sptps_replaywin = replaywin;
846 #ifndef DISABLE_LEGACY
847 /* Generate packet encryption key */
849 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
850 cipher = xstrdup("aes-256-cbc");
852 if(!strcasecmp(cipher, "none")) {
853 myself->incipher = NULL;
854 } else if(!(myself->incipher = cipher_open_by_name(cipher))) {
855 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
861 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
863 /* Check if we want to use message authentication codes... */
866 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
869 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
873 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
874 digest = xstrdup("sha256");
876 if(!strcasecmp(digest, "none")) {
877 myself->indigest = NULL;
878 } else if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
879 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
888 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
889 if(myself->incompression < 0 || myself->incompression > 11) {
890 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
894 myself->incompression = 0;
896 myself->connection->outcompression = 0;
900 myself->nexthop = myself;
901 myself->via = myself;
902 myself->status.reachable = true;
903 myself->last_state_change = now.tv_sec;
904 myself->status.sptps = experimental;
915 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
916 if(!strcasecmp(type, "dummy"))
917 devops = dummy_devops;
918 else if(!strcasecmp(type, "raw_socket"))
919 devops = raw_socket_devops;
920 else if(!strcasecmp(type, "multicast"))
921 devops = multicast_devops;
922 else if(!strcasecmp(type, "fd"))
925 else if(!strcasecmp(type, "uml"))
929 else if(!strcasecmp(type, "vde"))
935 get_config_bool(lookup_config(config_tree, "DeviceStandby"), &device_standby);
941 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
945 if(!do_detach && getenv("LISTEN_FDS")) {
949 listen_sockets = atoi(getenv("LISTEN_FDS"));
951 unsetenv("LISTEN_FDS");
954 if(listen_sockets > MAXSOCKETS) {
955 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
959 for(int i = 0; i < listen_sockets; i++) {
961 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
962 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(sockerrno));
967 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
970 int udp_fd = setup_vpn_in_socket(&sa);
974 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
975 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
977 if(debug_level >= DEBUG_CONNECTIONS) {
978 hostname = sockaddr2hostname(&sa);
979 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
983 memcpy(&listen_socket[i].sa, &sa, salen);
989 for(config_t *cfg = lookup_config(config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
991 get_config_string(cfg, &address);
992 if(!add_listen_address(address, true))
996 for(config_t *cfg = lookup_config(config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
998 get_config_string(cfg, &address);
999 if(!add_listen_address(address, false))
1004 if(!add_listen_address(address, NULL))
1008 if(!listen_sockets) {
1009 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
1013 /* If no Port option was specified, set myport to the port used by the first listening socket. */
1015 if(!port_specified || atoi(myport) == 0) {
1017 socklen_t salen = sizeof sa;
1018 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
1020 sockaddr2str(&sa, NULL, &myport);
1022 myport = xstrdup("655");
1026 xasprintf(&myself->hostname, "MYSELF port %s", myport);
1027 myself->connection->hostname = xstrdup(myself->hostname);
1030 get_config_string(lookup_config(config_tree, "UPnP"), &upnp);
1031 bool upnp_tcp = false;
1032 bool upnp_udp = false;
1034 if (!strcasecmp(upnp, "yes"))
1035 upnp_tcp = upnp_udp = true;
1036 else if (!strcasecmp(upnp, "udponly"))
1040 if (upnp_tcp || upnp_udp) {
1041 #ifdef HAVE_MINIUPNPC
1042 upnp_init(upnp_tcp, upnp_udp);
1044 logger(DEBUG_ALWAYS, LOG_WARNING, "UPnP was requested, but tinc isn't built with miniupnpc support!");
1050 last_config_check = now.tv_sec;
1058 bool setup_network(void) {
1065 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
1066 if(pinginterval < 1) {
1067 pinginterval = 86400;
1072 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
1074 if(pingtimeout < 1 || pingtimeout > pinginterval)
1075 pingtimeout = pinginterval;
1077 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
1078 maxoutbufsize = 10 * MTU;
1086 if (!device_standby)
1089 /* Run subnet-up scripts for our own subnets */
1091 subnet_update(myself, NULL, true);
1097 close all open network connections
1099 void close_network_connections(void) {
1100 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1102 connection_t *c = node->data;
1103 /* Keep control connections open until the end, so they know when we really terminated */
1104 if(c->status.control)
1107 terminate_connection(c, false);
1111 list_delete_list(outgoing_list);
1113 if(myself && myself->connection) {
1114 subnet_update(myself, NULL, false);
1115 connection_del(myself->connection);
1118 for(int i = 0; i < listen_sockets; i++) {
1119 io_del(&listen_socket[i].tcp);
1120 io_del(&listen_socket[i].udp);
1121 close(listen_socket[i].tcp.fd);
1122 close(listen_socket[i].udp.fd);
1131 if (!device_standby)
1143 free(scriptextension);
1144 free(scriptinterpreter);