3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2012 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.
25 #include "splay_tree.h"
28 #include "connection.h"
46 static struct event device_ev;
53 proxytype_t proxytype;
55 bool node_read_ecdsa_public_key(node_t *n) {
56 if(ecdsa_active(&n->ecdsa))
59 splay_tree_t *config_tree;
65 xasprintf(&fname, "%s/hosts/%s", confbase, n->name);
67 init_configuration(&config_tree);
68 if(!read_config_file(config_tree, fname))
71 /* First, check for simple ECDSAPublicKey statement */
73 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
74 result = ecdsa_set_base64_public_key(&n->ecdsa, p);
79 /* Else, check for ECDSAPublicKeyFile statement and read it */
83 if(!get_config_string(lookup_config(config_tree, "ECDSAPublicKeyFile"), &fname))
84 xasprintf(&fname, "%s/hosts/%s", confbase, n->name);
86 fp = fopen(fname, "r");
89 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s", fname, strerror(errno));
93 result = ecdsa_read_pem_public_key(&n->ecdsa, fp);
97 exit_configuration(&config_tree);
102 bool read_ecdsa_public_key(connection_t *c) {
108 /* First, check for simple ECDSAPublicKey statement */
110 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
111 result = ecdsa_set_base64_public_key(&c->ecdsa, p);
116 /* Else, check for ECDSAPublicKeyFile statement and read it */
118 if(!get_config_string(lookup_config(c->config_tree, "ECDSAPublicKeyFile"), &fname))
119 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
121 fp = fopen(fname, "r");
124 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s",
125 fname, strerror(errno));
130 result = ecdsa_read_pem_public_key(&c->ecdsa, fp);
134 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing ECDSA public key file `%s' failed.", fname);
139 bool read_rsa_public_key(connection_t *c) {
145 /* First, check for simple PublicKey statement */
147 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
148 result = rsa_set_hex_public_key(&c->rsa, n, "FFFF");
153 /* Else, check for PublicKeyFile statement and read it */
155 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
156 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
158 fp = fopen(fname, "r");
161 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
166 result = rsa_read_pem_public_key(&c->rsa, fp);
170 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
175 static bool read_ecdsa_private_key(void) {
180 /* Check for PrivateKeyFile statement and read it */
182 if(!get_config_string(lookup_config(config_tree, "ECDSAPrivateKeyFile"), &fname))
183 xasprintf(&fname, "%s/ecdsa_key.priv", confbase);
185 fp = fopen(fname, "r");
188 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file `%s': %s", fname, strerror(errno));
193 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
196 if(fstat(fileno(fp), &s)) {
197 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat ECDSA private key file `%s': %s'", fname, strerror(errno));
202 if(s.st_mode & ~0100700)
203 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for ECDSA private key file `%s'!", fname);
206 result = ecdsa_read_pem_private_key(&myself->connection->ecdsa, fp);
210 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
215 static bool read_rsa_private_key(void) {
221 /* First, check for simple PrivateKey statement */
223 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
224 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
225 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
229 result = rsa_set_hex_private_key(&myself->connection->rsa, n, "FFFF", d);
235 /* Else, check for PrivateKeyFile statement and read it */
237 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
238 xasprintf(&fname, "%s/rsa_key.priv", confbase);
240 fp = fopen(fname, "r");
243 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
244 fname, strerror(errno));
249 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
252 if(fstat(fileno(fp), &s)) {
253 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
258 if(s.st_mode & ~0100700)
259 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
262 result = rsa_read_pem_private_key(&myself->connection->rsa, fp);
266 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
271 static struct event keyexpire_event;
273 static void keyexpire_handler(int fd, short events, void *data) {
277 void regenerate_key(void) {
278 if(timeout_initialized(&keyexpire_event)) {
279 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
280 event_del(&keyexpire_event);
283 timeout_set(&keyexpire_event, keyexpire_handler, NULL);
286 event_add(&keyexpire_event, &(struct timeval){keylifetime, 0});
290 Read Subnets from all host config files
292 void load_all_subnets(void) {
297 splay_tree_t *config_tree;
302 xasprintf(&dname, "%s/hosts", confbase);
303 dir = opendir(dname);
305 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
310 while((ent = readdir(dir))) {
311 if(!check_id(ent->d_name))
314 n = lookup_node(ent->d_name);
315 #ifdef _DIRENT_HAVE_D_TYPE
316 //if(ent->d_type != DT_REG)
320 xasprintf(&fname, "%s/hosts/%s", confbase, ent->d_name);
321 init_configuration(&config_tree);
322 read_config_options(config_tree, ent->d_name);
323 read_config_file(config_tree, fname);
328 n->name = xstrdup(ent->d_name);
332 for(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 char *get_name(void) {
352 get_config_string(lookup_config(config_tree, "Name"), &name);
358 char *envname = getenv(name + 1);
360 if(strcmp(name + 1, "HOST")) {
361 fprintf(stderr, "Invalid Name: environment variable %s does not exist\n", name + 1);
364 envname = alloca(32);
365 if(gethostname(envname, 32)) {
366 fprintf(stderr, "Could not get hostname: %s\n", strerror(errno));
372 name = xstrdup(envname);
373 for(char *c = name; *c; c++)
378 if(!check_id(name)) {
379 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
388 Configure node_t myself and set up the local sockets (listen only)
390 static bool setup_myself(void) {
393 char *name, *hostname, *mode, *afname, *cipher, *digest, *type;
395 char *address = NULL;
399 struct addrinfo *ai, *aip, hint = {0};
405 myself->connection = new_connection();
407 myself->hostname = xstrdup("MYSELF");
408 myself->connection->hostname = xstrdup("MYSELF");
410 myself->connection->options = 0;
411 myself->connection->protocol_major = PROT_MAJOR;
412 myself->connection->protocol_minor = PROT_MINOR;
414 if(!(name = get_name())) {
415 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
420 myself->connection->name = xstrdup(name);
421 xasprintf(&fname, "%s/hosts/%s", confbase, name);
422 read_config_options(config_tree, name);
423 read_config_file(config_tree, fname);
426 get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental);
428 if(experimental && !read_ecdsa_private_key())
431 if(!read_rsa_private_key())
434 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
435 myport = xstrdup("655");
438 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
440 if(!ai || !ai->ai_addr)
443 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
444 sockaddr2str(&sa, NULL, &myport);
447 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
449 if((space = strchr(proxy, ' ')))
452 if(!strcasecmp(proxy, "none")) {
453 proxytype = PROXY_NONE;
454 } else if(!strcasecmp(proxy, "socks4")) {
455 proxytype = PROXY_SOCKS4;
456 } else if(!strcasecmp(proxy, "socks4a")) {
457 proxytype = PROXY_SOCKS4A;
458 } else if(!strcasecmp(proxy, "socks5")) {
459 proxytype = PROXY_SOCKS5;
460 } else if(!strcasecmp(proxy, "http")) {
461 proxytype = PROXY_HTTP;
462 } else if(!strcasecmp(proxy, "exec")) {
463 proxytype = PROXY_EXEC;
465 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
475 if(!space || !*space) {
476 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
479 proxyhost = xstrdup(space);
487 if(space && (space = strchr(space, ' ')))
488 *space++ = 0, proxyport = space;
489 if(space && (space = strchr(space, ' ')))
490 *space++ = 0, proxyuser = space;
491 if(space && (space = strchr(space, ' ')))
492 *space++ = 0, proxypass = space;
493 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
494 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
497 proxyhost = xstrdup(proxyhost);
498 proxyport = xstrdup(proxyport);
499 if(proxyuser && *proxyuser)
500 proxyuser = xstrdup(proxyuser);
501 if(proxypass && *proxypass)
502 proxypass = xstrdup(proxypass);
509 /* Read in all the subnets specified in the host configuration file */
511 cfg = lookup_config(config_tree, "Subnet");
514 if(!get_config_subnet(cfg, &subnet))
517 subnet_add(myself, subnet);
519 cfg = lookup_config_next(config_tree, cfg);
522 /* Check some options */
524 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
525 myself->options |= OPTION_INDIRECT;
527 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
528 myself->options |= OPTION_TCPONLY;
530 if(myself->options & OPTION_TCPONLY)
531 myself->options |= OPTION_INDIRECT;
533 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
534 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
535 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
536 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
537 strictsubnets |= tunnelserver;
539 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
540 if(!strcasecmp(mode, "router"))
541 routing_mode = RMODE_ROUTER;
542 else if(!strcasecmp(mode, "switch"))
543 routing_mode = RMODE_SWITCH;
544 else if(!strcasecmp(mode, "hub"))
545 routing_mode = RMODE_HUB;
547 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
553 if(get_config_string(lookup_config(config_tree, "Forwarding"), &mode)) {
554 if(!strcasecmp(mode, "off"))
555 forwarding_mode = FMODE_OFF;
556 else if(!strcasecmp(mode, "internal"))
557 forwarding_mode = FMODE_INTERNAL;
558 else if(!strcasecmp(mode, "kernel"))
559 forwarding_mode = FMODE_KERNEL;
561 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
568 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
570 myself->options |= OPTION_PMTU_DISCOVERY;
573 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
575 myself->options |= OPTION_CLAMP_MSS;
577 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
578 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
579 if(get_config_string(lookup_config(config_tree, "Broadcast"), &mode)) {
580 if(!strcasecmp(mode, "no"))
581 broadcast_mode = BMODE_NONE;
582 else if(!strcasecmp(mode, "yes") || !strcasecmp(mode, "mst"))
583 broadcast_mode = BMODE_MST;
584 else if(!strcasecmp(mode, "direct"))
585 broadcast_mode = BMODE_DIRECT;
587 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
593 #if !defined(SOL_IP) || !defined(IP_TOS)
594 if(priorityinheritance)
595 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
598 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
601 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
602 if(maxtimeout <= 0) {
603 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
609 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
610 if(udp_rcvbuf <= 0) {
611 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
616 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
617 if(udp_sndbuf <= 0) {
618 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
623 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
624 if(replaywin_int < 0) {
625 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
628 replaywin = (unsigned)replaywin_int;
631 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
632 if(!strcasecmp(afname, "IPv4"))
633 addressfamily = AF_INET;
634 else if(!strcasecmp(afname, "IPv6"))
635 addressfamily = AF_INET6;
636 else if(!strcasecmp(afname, "any"))
637 addressfamily = AF_UNSPEC;
639 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
645 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
647 /* Generate packet encryption key */
649 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
650 cipher = xstrdup("blowfish");
652 if(!cipher_open_by_name(&myself->incipher, cipher)) {
653 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
657 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
662 /* Check if we want to use message authentication codes... */
665 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
668 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
672 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
673 digest = xstrdup("sha1");
675 if(!digest_open_by_name(&myself->indigest, digest, maclength)) {
676 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
682 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
683 if(myself->incompression < 0 || myself->incompression > 11) {
684 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
688 myself->incompression = 0;
690 myself->connection->outcompression = 0;
694 myself->nexthop = myself;
695 myself->via = myself;
696 myself->status.reachable = true;
708 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
709 if(!strcasecmp(type, "dummy"))
710 devops = dummy_devops;
711 else if(!strcasecmp(type, "raw_socket"))
712 devops = raw_socket_devops;
713 else if(!strcasecmp(type, "multicast"))
714 devops = multicast_devops;
716 else if(!strcasecmp(type, "uml"))
720 else if(!strcasecmp(type, "vde"))
729 event_set(&device_ev, device_fd, EV_READ|EV_PERSIST, handle_device_data, NULL);
731 if (event_add(&device_ev, NULL) < 0) {
732 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
738 /* Run tinc-up script to further initialize the tap interface */
739 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
740 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
741 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
742 xasprintf(&envp[3], "NAME=%s", myself->name);
745 execute_script("tinc-up", envp);
747 for(i = 0; i < 4; i++)
750 /* Run subnet-up scripts for our own subnets */
752 subnet_update(myself, NULL, true);
756 if(!do_detach && getenv("LISTEN_FDS")) {
760 listen_sockets = atoi(getenv("LISTEN_FDS"));
762 unsetenv("LISTEN_FDS");
765 if(listen_sockets > MAXSOCKETS) {
766 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
770 for(i = 0; i < listen_sockets; i++) {
772 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
773 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
777 listen_socket[i].tcp = i + 3;
780 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
783 listen_socket[i].udp = setup_vpn_in_socket(&sa);
784 if(listen_socket[i].udp < 0)
787 event_set(&listen_socket[i].ev_tcp, listen_socket[i].tcp, EV_READ|EV_PERSIST, handle_new_meta_connection, NULL);
788 if(event_add(&listen_socket[i].ev_tcp, NULL) < 0) {
789 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
793 event_set(&listen_socket[i].ev_udp, listen_socket[i].udp, EV_READ|EV_PERSIST, handle_incoming_vpn_data, (void *)(intptr_t)listen_sockets);
794 if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
795 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
799 if(debug_level >= DEBUG_CONNECTIONS) {
800 hostname = sockaddr2hostname(&sa);
801 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
805 memcpy(&listen_socket[i].sa, &sa, salen);
809 cfg = lookup_config(config_tree, "BindToAddress");
812 get_config_string(cfg, &address);
814 cfg = lookup_config_next(config_tree, cfg);
819 char *space = strchr(address, ' ');
825 if(!strcmp(address, "*"))
829 hint.ai_family = addressfamily;
830 hint.ai_socktype = SOCK_STREAM;
831 hint.ai_protocol = IPPROTO_TCP;
832 hint.ai_flags = AI_PASSIVE;
834 err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
838 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
843 for(aip = ai; aip; aip = aip->ai_next) {
844 if(listen_sockets >= MAXSOCKETS) {
845 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
849 listen_socket[listen_sockets].tcp =
850 setup_listen_socket((sockaddr_t *) aip->ai_addr);
852 if(listen_socket[listen_sockets].tcp < 0)
855 listen_socket[listen_sockets].udp =
856 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
858 if(listen_socket[listen_sockets].udp < 0) {
859 close(listen_socket[listen_sockets].tcp);
863 event_set(&listen_socket[listen_sockets].ev_tcp,
864 listen_socket[listen_sockets].tcp,
866 handle_new_meta_connection, NULL);
867 if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
868 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
872 event_set(&listen_socket[listen_sockets].ev_udp,
873 listen_socket[listen_sockets].udp,
875 handle_incoming_vpn_data, (void *)(intptr_t)listen_sockets);
876 if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
877 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
881 if(debug_level >= DEBUG_CONNECTIONS) {
882 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
883 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
887 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
896 logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");
898 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
908 bool setup_network(void) {
915 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
916 if(pinginterval < 1) {
917 pinginterval = 86400;
922 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
924 if(pingtimeout < 1 || pingtimeout > pinginterval)
925 pingtimeout = pinginterval;
927 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
928 maxoutbufsize = 10 * MTU;
937 close all open network connections
939 void close_network_connections(void) {
940 splay_node_t *node, *next;
945 for(node = connection_tree->head; node; node = next) {
949 terminate_connection(c, false);
952 list_delete_list(outgoing_list);
954 if(myself && myself->connection) {
955 subnet_update(myself, NULL, false);
956 terminate_connection(myself->connection, false);
957 free_connection(myself->connection);
960 for(i = 0; i < listen_sockets; i++) {
961 event_del(&listen_socket[i].ev_tcp);
962 event_del(&listen_socket[i].ev_udp);
963 close(listen_socket[i].tcp);
964 close(listen_socket[i].udp);
967 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
968 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
969 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
970 xasprintf(&envp[3], "NAME=%s", myself->name);
979 execute_script("tinc-down", envp);
981 if(myport) free(myport);
983 for(i = 0; i < 4; i++)