3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2015 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"
52 static io_t device_io;
54 bool device_standby = false;
60 proxytype_t proxytype;
62 bool disablebuggypeers;
64 char *scriptinterpreter;
65 char *scriptextension;
67 bool node_read_ecdsa_public_key(node_t *n) {
68 if(ecdsa_active(n->ecdsa))
71 splay_tree_t *config_tree;
76 init_configuration(&config_tree);
77 if(!read_host_config(config_tree, n->name))
80 /* First, check for simple Ed25519PublicKey statement */
82 if(get_config_string(lookup_config(config_tree, "Ed25519PublicKey"), &p)) {
83 n->ecdsa = ecdsa_set_base64_public_key(p);
88 /* Else, check for Ed25519PublicKeyFile statement and read it */
90 if(!get_config_string(lookup_config(config_tree, "Ed25519PublicKeyFile"), &pubname))
91 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
93 fp = fopen(pubname, "r");
98 n->ecdsa = ecdsa_read_pem_public_key(fp);
102 exit_configuration(&config_tree);
107 bool read_ecdsa_public_key(connection_t *c) {
108 if(ecdsa_active(c->ecdsa))
115 if(!c->config_tree) {
116 init_configuration(&c->config_tree);
117 if(!read_host_config(c->config_tree, c->name))
121 /* First, check for simple Ed25519PublicKey statement */
123 if(get_config_string(lookup_config(c->config_tree, "Ed25519PublicKey"), &p)) {
124 c->ecdsa = ecdsa_set_base64_public_key(p);
129 /* Else, check for Ed25519PublicKeyFile statement and read it */
131 if(!get_config_string(lookup_config(c->config_tree, "Ed25519PublicKeyFile"), &fname))
132 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
134 fp = fopen(fname, "r");
137 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading Ed25519 public key file `%s': %s",
138 fname, strerror(errno));
143 c->ecdsa = ecdsa_read_pem_public_key(fp);
145 if(!c->ecdsa && errno != ENOENT)
146 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing Ed25519 public key file `%s' failed.", fname);
153 #ifndef DISABLE_LEGACY
154 bool read_rsa_public_key(connection_t *c) {
159 /* First, check for simple PublicKey statement */
161 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
162 c->rsa = rsa_set_hex_public_key(n, "FFFF");
167 /* Else, check for PublicKeyFile statement and read it */
169 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
170 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
172 fp = fopen(fname, "r");
175 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
180 c->rsa = rsa_read_pem_public_key(fp);
184 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
190 static bool read_ecdsa_private_key(void) {
194 /* Check for PrivateKeyFile statement and read it */
196 if(!get_config_string(lookup_config(config_tree, "Ed25519PrivateKeyFile"), &fname))
197 xasprintf(&fname, "%s" SLASH "ed25519_key.priv", confbase);
199 fp = fopen(fname, "r");
202 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading Ed25519 private key file `%s': %s", fname, strerror(errno));
204 logger(DEBUG_ALWAYS, LOG_INFO, "Create an Ed25519 keypair with `tinc -n %s generate-ed25519-keys'.", netname ?: ".");
209 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
212 if(fstat(fileno(fp), &s)) {
213 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat Ed25519 private key file `%s': %s'", fname, strerror(errno));
218 if(s.st_mode & ~0100700)
219 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for Ed25519 private key file `%s'!", fname);
222 myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
225 if(!myself->connection->ecdsa)
226 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
228 return myself->connection->ecdsa;
231 static bool read_invitation_key(void) {
233 char fname[PATH_MAX];
236 ecdsa_free(invitation_key);
237 invitation_key = NULL;
240 snprintf(fname, sizeof fname, "%s" SLASH "invitations" SLASH "ed25519_key.priv", confbase);
242 fp = fopen(fname, "r");
245 invitation_key = ecdsa_read_pem_private_key(fp);
248 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
251 return invitation_key;
254 #ifndef DISABLE_LEGACY
255 static bool read_rsa_private_key(void) {
260 /* First, check for simple PrivateKey statement */
262 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
263 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
264 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
268 myself->connection->rsa = rsa_set_hex_private_key(n, "FFFF", d);
271 return myself->connection->rsa;
274 /* Else, check for PrivateKeyFile statement and read it */
276 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
277 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
279 fp = fopen(fname, "r");
282 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
283 fname, strerror(errno));
285 logger(DEBUG_ALWAYS, LOG_INFO, "Create an RSA keypair with `tinc -n %s generate-rsa-keys'.", netname ?: ".");
290 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
293 if(fstat(fileno(fp), &s)) {
294 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
299 if(s.st_mode & ~0100700)
300 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
303 myself->connection->rsa = rsa_read_pem_private_key(fp);
306 if(!myself->connection->rsa)
307 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
309 return myself->connection->rsa;
313 static timeout_t keyexpire_timeout;
315 static void keyexpire_handler(void *data) {
317 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
320 void regenerate_key(void) {
321 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
323 for splay_each(node_t, n, node_tree)
324 n->status.validkey_in = false;
328 Read Subnets from all host config files
330 void load_all_subnets(void) {
333 char dname[PATH_MAX];
335 snprintf(dname, sizeof dname, "%s" SLASH "hosts", confbase);
336 dir = opendir(dname);
338 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
342 while((ent = readdir(dir))) {
343 if(!check_id(ent->d_name))
346 node_t *n = lookup_node(ent->d_name);
347 #ifdef _DIRENT_HAVE_D_TYPE
348 //if(ent->d_type != DT_REG)
352 splay_tree_t *config_tree;
353 init_configuration(&config_tree);
354 read_config_options(config_tree, ent->d_name);
355 read_host_config(config_tree, ent->d_name);
359 n->name = xstrdup(ent->d_name);
363 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
366 if(!get_config_subnet(cfg, &s))
369 if((s2 = lookup_subnet(n, s))) {
377 exit_configuration(&config_tree);
383 void load_all_nodes(void) {
386 char dname[PATH_MAX];
388 snprintf(dname, sizeof dname, "%s" SLASH "hosts", confbase);
389 dir = opendir(dname);
391 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
395 while((ent = readdir(dir))) {
396 if(!check_id(ent->d_name))
399 node_t *n = lookup_node(ent->d_name);
404 n->name = xstrdup(ent->d_name);
412 char *get_name(void) {
416 get_config_string(lookup_config(config_tree, "Name"), &name);
421 returned_name = replace_name(name);
423 return returned_name;
426 bool setup_myself_reloadable(void) {
435 free(scriptinterpreter);
436 scriptinterpreter = NULL;
437 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
440 free(scriptextension);
441 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
442 scriptextension = xstrdup("");
444 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
446 if((space = strchr(proxy, ' ')))
449 if(!strcasecmp(proxy, "none")) {
450 proxytype = PROXY_NONE;
451 } else if(!strcasecmp(proxy, "socks4")) {
452 proxytype = PROXY_SOCKS4;
453 } else if(!strcasecmp(proxy, "socks4a")) {
454 proxytype = PROXY_SOCKS4A;
455 } else if(!strcasecmp(proxy, "socks5")) {
456 proxytype = PROXY_SOCKS5;
457 } else if(!strcasecmp(proxy, "http")) {
458 proxytype = PROXY_HTTP;
459 } else if(!strcasecmp(proxy, "exec")) {
460 proxytype = PROXY_EXEC;
462 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
472 if(!space || !*space) {
473 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
476 proxyhost = xstrdup(space);
484 if(space && (space = strchr(space, ' ')))
485 *space++ = 0, proxyport = space;
486 if(space && (space = strchr(space, ' ')))
487 *space++ = 0, proxyuser = space;
488 if(space && (space = strchr(space, ' ')))
489 *space++ = 0, proxypass = space;
490 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
491 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
494 proxyhost = xstrdup(proxyhost);
495 proxyport = xstrdup(proxyport);
496 if(proxyuser && *proxyuser)
497 proxyuser = xstrdup(proxyuser);
498 if(proxypass && *proxypass)
499 proxypass = xstrdup(proxypass);
506 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
507 myself->options |= OPTION_INDIRECT;
509 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
510 myself->options |= OPTION_TCPONLY;
512 if(myself->options & OPTION_TCPONLY)
513 myself->options |= OPTION_INDIRECT;
515 get_config_bool(lookup_config(config_tree, "UDPDiscovery"), &udp_discovery);
516 get_config_int(lookup_config(config_tree, "UDPDiscoveryKeepaliveInterval"), &udp_discovery_keepalive_interval);
517 get_config_int(lookup_config(config_tree, "UDPDiscoveryInterval"), &udp_discovery_interval);
518 get_config_int(lookup_config(config_tree, "UDPDiscoveryTimeout"), &udp_discovery_timeout);
520 get_config_int(lookup_config(config_tree, "MTUInfoInterval"), &mtu_info_interval);
521 get_config_int(lookup_config(config_tree, "UDPInfoInterval"), &udp_info_interval);
523 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
524 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
526 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
527 if(!strcasecmp(rmode, "router"))
528 routing_mode = RMODE_ROUTER;
529 else if(!strcasecmp(rmode, "switch"))
530 routing_mode = RMODE_SWITCH;
531 else if(!strcasecmp(rmode, "hub"))
532 routing_mode = RMODE_HUB;
534 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
540 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
541 if(!strcasecmp(fmode, "off"))
542 forwarding_mode = FMODE_OFF;
543 else if(!strcasecmp(fmode, "internal"))
544 forwarding_mode = FMODE_INTERNAL;
545 else if(!strcasecmp(fmode, "kernel"))
546 forwarding_mode = FMODE_KERNEL;
548 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
555 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
557 myself->options |= OPTION_PMTU_DISCOVERY;
560 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
562 myself->options |= OPTION_CLAMP_MSS;
564 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
565 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
566 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
567 if(!strcasecmp(bmode, "no"))
568 broadcast_mode = BMODE_NONE;
569 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
570 broadcast_mode = BMODE_MST;
571 else if(!strcasecmp(bmode, "direct"))
572 broadcast_mode = BMODE_DIRECT;
574 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
580 const char* const DEFAULT_BROADCAST_SUBNETS[] = { "ff:ff:ff:ff:ff:ff", "255.255.255.255", "224.0.0.0/4", "ff00::/8" };
581 for (size_t i = 0; i < sizeof(DEFAULT_BROADCAST_SUBNETS) / sizeof(*DEFAULT_BROADCAST_SUBNETS); i++) {
582 subnet_t *s = new_subnet();
583 if (!str2net(s, DEFAULT_BROADCAST_SUBNETS[i]))
587 for (config_t* cfg = lookup_config(config_tree, "BroadcastSubnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
589 if (!get_config_subnet(cfg, &s))
594 #if !defined(SOL_IP) || !defined(IP_TOS)
595 if(priorityinheritance)
596 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
599 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
602 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
603 if(maxtimeout <= 0) {
604 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
610 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
611 if(!strcasecmp(afname, "IPv4"))
612 addressfamily = AF_INET;
613 else if(!strcasecmp(afname, "IPv6"))
614 addressfamily = AF_INET6;
615 else if(!strcasecmp(afname, "any"))
616 addressfamily = AF_UNSPEC;
618 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
624 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
626 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
629 config_t *cfg = lookup_config(config_tree, "AutoConnect");
631 if(!get_config_bool(cfg, &autoconnect)) {
632 // Some backwards compatibility with when this option was an int
634 get_config_int(cfg, &val);
639 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
641 read_invitation_key();
647 Add listening sockets.
649 static bool add_listen_address(char *address, bool bindto) {
653 char *space = strchr(address, ' ');
659 if(!strcmp(address, "*"))
663 struct addrinfo *ai, hint = {0};
664 hint.ai_family = addressfamily;
665 hint.ai_socktype = SOCK_STREAM;
666 hint.ai_protocol = IPPROTO_TCP;
667 hint.ai_flags = AI_PASSIVE;
669 #if HAVE_DECL_RES_INIT
672 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
676 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
680 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
681 // Ignore duplicate addresses
684 for(int i = 0; i < listen_sockets; i++)
685 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
693 if(listen_sockets >= MAXSOCKETS) {
694 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
698 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
703 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
710 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
711 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
713 if(debug_level >= DEBUG_CONNECTIONS) {
714 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
715 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
719 listen_socket[listen_sockets].bindto = bindto;
720 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
728 void device_enable(void) {
732 /* Run tinc-up script to further initialize the tap interface */
734 char *envp[5] = {NULL};
735 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
736 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
737 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
738 xasprintf(&envp[3], "NAME=%s", myname);
740 execute_script("tinc-up", envp);
742 for(int i = 0; i < 4; i++)
746 void device_disable(void) {
747 char *envp[5] = {NULL};
748 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
749 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
750 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
751 xasprintf(&envp[3], "NAME=%s", myname);
753 execute_script("tinc-down", envp);
755 for(int i = 0; i < 4; i++)
763 Configure node_t myself and set up the local sockets (listen only)
765 static bool setup_myself(void) {
766 char *name, *hostname, *cipher, *digest, *type;
767 char *address = NULL;
768 bool port_specified = false;
770 if(!(name = get_name())) {
771 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
775 myname = xstrdup(name);
777 myself->connection = new_connection();
779 myself->connection->name = xstrdup(name);
780 read_host_config(config_tree, name);
782 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
783 myport = xstrdup("655");
785 port_specified = true;
787 myself->connection->options = 0;
788 myself->connection->protocol_major = PROT_MAJOR;
789 myself->connection->protocol_minor = PROT_MINOR;
791 myself->options |= PROT_MINOR << 24;
793 #ifdef DISABLE_LEGACY
794 experimental = read_ecdsa_private_key();
796 logger(DEBUG_ALWAYS, LOG_ERR, "No private key available, cannot start tinc!");
800 if(!get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental)) {
801 experimental = read_ecdsa_private_key();
803 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for SPTPS disabled.");
805 if(experimental && !read_ecdsa_private_key())
809 if(!read_rsa_private_key()) {
811 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for legacy protocol disabled.");
813 logger(DEBUG_ALWAYS, LOG_ERR, "No private keys available, cannot start tinc!");
819 /* Ensure myport is numeric */
822 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
824 if(!ai || !ai->ai_addr)
827 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
828 sockaddr2str(&sa, NULL, &myport);
831 /* Read in all the subnets specified in the host configuration file */
833 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
836 if(!get_config_subnet(cfg, &subnet))
839 subnet_add(myself, subnet);
842 /* Check some options */
844 if(!setup_myself_reloadable())
847 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
848 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
849 strictsubnets |= tunnelserver;
851 if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
852 if(max_connection_burst <= 0) {
853 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
858 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
860 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
865 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
867 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
873 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
874 if(replaywin_int < 0) {
875 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
878 replaywin = (unsigned)replaywin_int;
879 sptps_replaywin = replaywin;
882 #ifndef DISABLE_LEGACY
883 /* Generate packet encryption key */
885 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
886 cipher = xstrdup("blowfish");
888 if(!strcasecmp(cipher, "none")) {
889 myself->incipher = NULL;
890 } else if(!(myself->incipher = cipher_open_by_name(cipher))) {
891 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
897 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
899 /* Check if we want to use message authentication codes... */
902 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
905 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
909 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
910 digest = xstrdup("sha1");
912 if(!strcasecmp(digest, "none")) {
913 myself->indigest = NULL;
914 } else if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
915 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
924 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
925 if(myself->incompression < 0 || myself->incompression > 11) {
926 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
930 myself->incompression = 0;
932 myself->connection->outcompression = 0;
936 myself->nexthop = myself;
937 myself->via = myself;
938 myself->status.reachable = true;
939 myself->last_state_change = now.tv_sec;
940 myself->status.sptps = experimental;
954 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
955 if(!strcasecmp(type, "dummy"))
956 devops = dummy_devops;
957 else if(!strcasecmp(type, "raw_socket"))
958 devops = raw_socket_devops;
959 else if(!strcasecmp(type, "multicast"))
960 devops = multicast_devops;
962 else if(!strcasecmp(type, "uml"))
966 else if(!strcasecmp(type, "vde"))
972 get_config_bool(lookup_config(config_tree, "DeviceStandby"), &device_standby);
978 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
982 if(!do_detach && getenv("LISTEN_FDS")) {
986 listen_sockets = atoi(getenv("LISTEN_FDS"));
988 unsetenv("LISTEN_FDS");
991 if(listen_sockets > MAXSOCKETS) {
992 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
996 for(int i = 0; i < listen_sockets; i++) {
998 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
999 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(sockerrno));
1004 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
1007 int udp_fd = setup_vpn_in_socket(&sa);
1011 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
1012 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
1014 if(debug_level >= DEBUG_CONNECTIONS) {
1015 hostname = sockaddr2hostname(&sa);
1016 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
1020 memcpy(&listen_socket[i].sa, &sa, salen);
1026 for(config_t *cfg = lookup_config(config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1028 get_config_string(cfg, &address);
1029 if(!add_listen_address(address, true))
1033 for(config_t *cfg = lookup_config(config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1035 get_config_string(cfg, &address);
1036 if(!add_listen_address(address, false))
1041 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;
1055 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
1057 sockaddr2str(&sa, NULL, &myport);
1059 myport = xstrdup("655");
1063 xasprintf(&myself->hostname, "MYSELF port %s", myport);
1064 myself->connection->hostname = xstrdup(myself->hostname);
1067 get_config_string(lookup_config(config_tree, "UPnP"), &upnp);
1068 bool upnp_tcp = false;
1069 bool upnp_udp = false;
1071 if (!strcasecmp(upnp, "yes"))
1072 upnp_tcp = upnp_udp = true;
1073 else if (!strcasecmp(upnp, "udponly"))
1077 if (upnp_tcp || upnp_udp) {
1078 #ifdef HAVE_MINIUPNPC
1079 upnp_init(upnp_tcp, upnp_udp);
1081 logger(DEBUG_ALWAYS, LOG_WARNING, "UPnP was requested, but tinc isn't built with miniupnpc support!");
1087 last_config_check = now.tv_sec;
1095 bool setup_network(void) {
1102 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
1103 if(pinginterval < 1) {
1104 pinginterval = 86400;
1109 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
1111 if(pingtimeout < 1 || pingtimeout > pinginterval)
1112 pingtimeout = pinginterval;
1114 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
1115 maxoutbufsize = 10 * MTU;
1123 if (!device_standby)
1126 /* Run subnet-up scripts for our own subnets */
1128 subnet_update(myself, NULL, true);
1134 close all open network connections
1136 void close_network_connections(void) {
1137 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1139 connection_t *c = node->data;
1140 /* Keep control connections open until the end, so they know when we really terminated */
1141 if(c->status.control)
1144 terminate_connection(c, false);
1148 list_delete_list(outgoing_list);
1150 if(myself && myself->connection) {
1151 subnet_update(myself, NULL, false);
1152 terminate_connection(myself->connection, false);
1153 free_connection(myself->connection);
1156 for(int i = 0; i < listen_sockets; i++) {
1157 io_del(&listen_socket[i].tcp);
1158 io_del(&listen_socket[i].udp);
1159 close(listen_socket[i].tcp.fd);
1160 close(listen_socket[i].udp.fd);
1169 if (!device_standby)
1182 free(scriptextension);
1183 free(scriptinterpreter);