3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2013 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"
46 static io_t device_io;
53 proxytype_t proxytype;
55 bool disablebuggypeers;
57 char *scriptinterpreter;
58 char *scriptextension;
60 bool node_read_ecdsa_public_key(node_t *n) {
61 if(ecdsa_active(n->ecdsa))
64 splay_tree_t *config_tree;
69 init_configuration(&config_tree);
70 if(!read_host_config(config_tree, n->name))
73 /* First, check for simple ECDSAPublicKey statement */
75 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
76 n->ecdsa = ecdsa_set_base64_public_key(p);
81 /* Else, check for ECDSAPublicKeyFile statement and read it */
83 if(!get_config_string(lookup_config(config_tree, "ECDSAPublicKeyFile"), &pubname))
84 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
86 fp = fopen(pubname, "r");
89 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s", pubname, strerror(errno));
93 n->ecdsa = ecdsa_read_pem_public_key(fp);
97 exit_configuration(&config_tree);
102 bool read_ecdsa_public_key(connection_t *c) {
103 if(ecdsa_active(c->ecdsa))
110 if(!c->config_tree) {
111 init_configuration(&c->config_tree);
112 if(!read_host_config(c->config_tree, c->name))
116 /* First, check for simple ECDSAPublicKey statement */
118 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
119 c->ecdsa = ecdsa_set_base64_public_key(p);
124 /* Else, check for ECDSAPublicKeyFile statement and read it */
126 if(!get_config_string(lookup_config(c->config_tree, "ECDSAPublicKeyFile"), &fname))
127 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
129 fp = fopen(fname, "r");
132 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s",
133 fname, strerror(errno));
138 c->ecdsa = ecdsa_read_pem_public_key(fp);
142 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing ECDSA public key file `%s' failed.", fname);
147 bool read_rsa_public_key(connection_t *c) {
148 if(ecdsa_active(c->ecdsa))
155 /* First, check for simple PublicKey statement */
157 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
158 c->rsa = rsa_set_hex_public_key(n, "FFFF");
163 /* Else, check for PublicKeyFile statement and read it */
165 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
166 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
168 fp = fopen(fname, "r");
171 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
176 c->rsa = rsa_read_pem_public_key(fp);
180 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
185 static bool read_ecdsa_private_key(void) {
189 /* Check for PrivateKeyFile statement and read it */
191 if(!get_config_string(lookup_config(config_tree, "ECDSAPrivateKeyFile"), &fname))
192 xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", confbase);
194 fp = fopen(fname, "r");
197 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file `%s': %s", fname, strerror(errno));
199 logger(DEBUG_ALWAYS, LOG_INFO, "Create an ECDSA keypair with `tinc -n %s generate-ecdsa-keys'.", netname ?: ".");
204 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
207 if(fstat(fileno(fp), &s)) {
208 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat ECDSA private key file `%s': %s'", fname, strerror(errno));
213 if(s.st_mode & ~0100700)
214 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for ECDSA private key file `%s'!", fname);
217 myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
220 if(!myself->connection->ecdsa)
221 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
223 return myself->connection->ecdsa;
226 static bool read_rsa_private_key(void) {
231 /* First, check for simple PrivateKey statement */
233 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
234 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
235 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
239 myself->connection->rsa = rsa_set_hex_private_key(n, "FFFF", d);
242 return myself->connection->rsa;
245 /* Else, check for PrivateKeyFile statement and read it */
247 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
248 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
250 fp = fopen(fname, "r");
253 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
254 fname, strerror(errno));
259 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
262 if(fstat(fileno(fp), &s)) {
263 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
268 if(s.st_mode & ~0100700)
269 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
272 myself->connection->rsa = rsa_read_pem_private_key(fp);
275 if(!myself->connection->rsa)
276 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
278 return myself->connection->rsa;
281 static timeout_t keyexpire_timeout;
283 static void keyexpire_handler(void *data) {
285 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
288 void regenerate_key(void) {
289 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
294 Read Subnets from all host config files
296 void load_all_subnets(void) {
301 xasprintf(&dname, "%s" SLASH "hosts", confbase);
302 dir = opendir(dname);
304 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
309 while((ent = readdir(dir))) {
310 if(!check_id(ent->d_name))
313 node_t *n = lookup_node(ent->d_name);
314 #ifdef _DIRENT_HAVE_D_TYPE
315 //if(ent->d_type != DT_REG)
319 splay_tree_t *config_tree;
320 init_configuration(&config_tree);
321 read_config_options(config_tree, ent->d_name);
322 read_host_config(config_tree, ent->d_name);
326 n->name = xstrdup(ent->d_name);
330 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
333 if(!get_config_subnet(cfg, &s))
336 if((s2 = lookup_subnet(n, s))) {
343 exit_configuration(&config_tree);
349 void load_all_nodes(void) {
354 xasprintf(&dname, "%s" SLASH "hosts", confbase);
355 dir = opendir(dname);
357 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
362 while((ent = readdir(dir))) {
363 if(!check_id(ent->d_name))
366 node_t *n = lookup_node(ent->d_name);
371 n->name = xstrdup(ent->d_name);
379 char *get_name(void) {
382 get_config_string(lookup_config(config_tree, "Name"), &name);
388 char *envname = getenv(name + 1);
390 if(strcmp(name + 1, "HOST")) {
391 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
395 if(gethostname(envname, 32)) {
396 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", strerror(errno));
402 name = xstrdup(envname);
403 for(char *c = name; *c; c++)
408 if(!check_id(name)) {
409 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
417 bool setup_myself_reloadable(void) {
426 free(scriptinterpreter);
427 scriptinterpreter = NULL;
428 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
431 free(scriptextension);
432 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
434 scriptextension = xstrdup(".bat");
436 scriptextension = xstrdup("");
439 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
441 if((space = strchr(proxy, ' ')))
444 if(!strcasecmp(proxy, "none")) {
445 proxytype = PROXY_NONE;
446 } else if(!strcasecmp(proxy, "socks4")) {
447 proxytype = PROXY_SOCKS4;
448 } else if(!strcasecmp(proxy, "socks4a")) {
449 proxytype = PROXY_SOCKS4A;
450 } else if(!strcasecmp(proxy, "socks5")) {
451 proxytype = PROXY_SOCKS5;
452 } else if(!strcasecmp(proxy, "http")) {
453 proxytype = PROXY_HTTP;
454 } else if(!strcasecmp(proxy, "exec")) {
455 proxytype = PROXY_EXEC;
457 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
467 if(!space || !*space) {
468 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
471 proxyhost = xstrdup(space);
479 if(space && (space = strchr(space, ' ')))
480 *space++ = 0, proxyport = space;
481 if(space && (space = strchr(space, ' ')))
482 *space++ = 0, proxyuser = space;
483 if(space && (space = strchr(space, ' ')))
484 *space++ = 0, proxypass = space;
485 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
486 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
489 proxyhost = xstrdup(proxyhost);
490 proxyport = xstrdup(proxyport);
491 if(proxyuser && *proxyuser)
492 proxyuser = xstrdup(proxyuser);
493 if(proxypass && *proxypass)
494 proxypass = xstrdup(proxypass);
501 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
502 myself->options |= OPTION_INDIRECT;
504 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
505 myself->options |= OPTION_TCPONLY;
507 if(myself->options & OPTION_TCPONLY)
508 myself->options |= OPTION_INDIRECT;
510 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
511 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
513 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
514 if(!strcasecmp(rmode, "router"))
515 routing_mode = RMODE_ROUTER;
516 else if(!strcasecmp(rmode, "switch"))
517 routing_mode = RMODE_SWITCH;
518 else if(!strcasecmp(rmode, "hub"))
519 routing_mode = RMODE_HUB;
521 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
527 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
528 if(!strcasecmp(fmode, "off"))
529 forwarding_mode = FMODE_OFF;
530 else if(!strcasecmp(fmode, "internal"))
531 forwarding_mode = FMODE_INTERNAL;
532 else if(!strcasecmp(fmode, "kernel"))
533 forwarding_mode = FMODE_KERNEL;
535 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
542 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
544 myself->options |= OPTION_PMTU_DISCOVERY;
547 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
549 myself->options |= OPTION_CLAMP_MSS;
551 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
552 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
553 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
554 if(!strcasecmp(bmode, "no"))
555 broadcast_mode = BMODE_NONE;
556 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
557 broadcast_mode = BMODE_MST;
558 else if(!strcasecmp(bmode, "direct"))
559 broadcast_mode = BMODE_DIRECT;
561 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
567 #if !defined(SOL_IP) || !defined(IP_TOS)
568 if(priorityinheritance)
569 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "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 get_config_int(lookup_config(config_tree, "AutoConnect"), &autoconnect);
604 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
610 Configure node_t myself and set up the local sockets (listen only)
612 static bool setup_myself(void) {
613 char *name, *hostname, *cipher, *digest, *type;
614 char *address = NULL;
616 if(!(name = get_name())) {
617 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
622 myself->connection = new_connection();
624 myself->connection->name = xstrdup(name);
625 read_host_config(config_tree, name);
627 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
628 myport = xstrdup("655");
630 xasprintf(&myself->hostname, "MYSELF port %s", myport);
631 myself->connection->hostname = xstrdup(myself->hostname);
633 myself->connection->options = 0;
634 myself->connection->protocol_major = PROT_MAJOR;
635 myself->connection->protocol_minor = PROT_MINOR;
637 myself->options |= PROT_MINOR << 24;
639 get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental);
641 if(experimental && !read_ecdsa_private_key())
644 if(!read_rsa_private_key())
648 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
650 if(!ai || !ai->ai_addr)
653 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
654 sockaddr2str(&sa, NULL, &myport);
657 /* Read in all the subnets specified in the host configuration file */
659 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
662 if(!get_config_subnet(cfg, &subnet))
665 subnet_add(myself, subnet);
668 /* Check some options */
670 if(!setup_myself_reloadable())
673 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
674 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
675 strictsubnets |= tunnelserver;
679 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
680 if(udp_rcvbuf <= 0) {
681 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
686 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
687 if(udp_sndbuf <= 0) {
688 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
694 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
695 if(replaywin_int < 0) {
696 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
699 replaywin = (unsigned)replaywin_int;
700 sptps_replaywin = replaywin;
703 /* Generate packet encryption key */
705 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
706 cipher = xstrdup("blowfish");
708 if(!(myself->incipher = cipher_open_by_name(cipher))) {
709 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
716 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
718 /* Check if we want to use message authentication codes... */
721 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
724 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
728 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
729 digest = xstrdup("sha1");
731 if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
732 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
740 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
741 if(myself->incompression < 0 || myself->incompression > 11) {
742 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
746 myself->incompression = 0;
748 myself->connection->outcompression = 0;
752 myself->nexthop = myself;
753 myself->via = myself;
754 myself->status.reachable = true;
755 myself->last_state_change = now.tv_sec;
756 myself->status.sptps = experimental;
770 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
771 if(!strcasecmp(type, "dummy"))
772 devops = dummy_devops;
773 else if(!strcasecmp(type, "raw_socket"))
774 devops = raw_socket_devops;
775 else if(!strcasecmp(type, "multicast"))
776 devops = multicast_devops;
778 else if(!strcasecmp(type, "uml"))
782 else if(!strcasecmp(type, "vde"))
791 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
793 /* Run tinc-up script to further initialize the tap interface */
795 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
796 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
797 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
798 xasprintf(&envp[3], "NAME=%s", myself->name);
801 execute_script("tinc-up", envp);
803 for(int i = 0; i < 4; i++)
806 /* Run subnet-up scripts for our own subnets */
808 subnet_update(myself, NULL, true);
813 int unix_fd = socket(AF_UNIX, SOCK_STREAM, 0);
815 logger(DEBUG_ALWAYS, LOG_ERR, "Could not create UNIX socket: %s", sockstrerror(errno));
819 struct sockaddr_un sa;
820 sa.sun_family = AF_UNIX;
821 strncpy(sa.sun_path, unixsocketname, sizeof sa.sun_path);
823 if(connect(unix_fd, (struct sockaddr *)&sa, sizeof sa) >= 0) {
824 logger(DEBUG_ALWAYS, LOG_ERR, "UNIX socket %s is still in use!", unixsocketname);
828 unlink(unixsocketname);
830 if(bind(unix_fd, (struct sockaddr *)&sa, sizeof sa) < 0) {
831 logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind UNIX socket to %s: %s", unixsocketname, sockstrerror(errno));
835 if(listen(unix_fd, 3) < 0) {
836 logger(DEBUG_ALWAYS, LOG_ERR, "Could not listen on UNIX socket %s: %s", unixsocketname, sockstrerror(errno));
840 io_add(&unix_socket, handle_new_unix_connection, &unix_socket, unix_fd, IO_READ);
843 if(!do_detach && getenv("LISTEN_FDS")) {
847 listen_sockets = atoi(getenv("LISTEN_FDS"));
849 unsetenv("LISTEN_FDS");
852 if(listen_sockets > MAXSOCKETS) {
853 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
857 for(int i = 0; i < listen_sockets; i++) {
859 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
860 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
865 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
868 int udp_fd = setup_vpn_in_socket(&sa);
872 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
873 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
875 if(debug_level >= DEBUG_CONNECTIONS) {
876 hostname = sockaddr2hostname(&sa);
877 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
881 memcpy(&listen_socket[i].sa, &sa, salen);
885 config_t *cfg = lookup_config(config_tree, "BindToAddress");
888 get_config_string(cfg, &address);
890 cfg = lookup_config_next(config_tree, cfg);
895 char *space = strchr(address, ' ');
901 if(!strcmp(address, "*"))
905 struct addrinfo *ai, hint = {0};
906 hint.ai_family = addressfamily;
907 hint.ai_socktype = SOCK_STREAM;
908 hint.ai_protocol = IPPROTO_TCP;
909 hint.ai_flags = AI_PASSIVE;
911 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
915 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
920 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
921 if(listen_sockets >= MAXSOCKETS) {
922 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
926 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
931 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
938 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
939 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
941 if(debug_level >= DEBUG_CONNECTIONS) {
942 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
943 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
947 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
956 logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");
958 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
962 last_config_check = now.tv_sec;
970 bool setup_network(void) {
977 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
978 if(pinginterval < 1) {
979 pinginterval = 86400;
984 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
986 if(pingtimeout < 1 || pingtimeout > pinginterval)
987 pingtimeout = pinginterval;
989 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
990 maxoutbufsize = 10 * MTU;
999 close all open network connections
1001 void close_network_connections(void) {
1002 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1004 connection_t *c = node->data;
1005 /* Keep control connections open until the end, so they know when we really terminated */
1006 if(c->status.control)
1009 terminate_connection(c, false);
1012 list_delete_list(outgoing_list);
1014 if(myself && myself->connection) {
1015 subnet_update(myself, NULL, false);
1016 terminate_connection(myself->connection, false);
1017 free_connection(myself->connection);
1020 for(int i = 0; i < listen_sockets; i++) {
1021 io_del(&listen_socket[i].tcp);
1022 io_del(&listen_socket[i].udp);
1023 close(listen_socket[i].tcp.fd);
1024 close(listen_socket[i].udp.fd);
1028 io_del(&unix_socket);
1029 close(unix_socket.fd);
1033 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
1034 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
1035 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
1036 xasprintf(&envp[3], "NAME=%s", myself->name);
1045 execute_script("tinc-down", envp);
1047 if(myport) free(myport);
1049 for(int i = 0; i < 4; i++)