3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2014 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"
48 static io_t device_io;
50 bool device_standby = false;
56 proxytype_t proxytype;
58 bool disablebuggypeers;
60 char *scriptinterpreter;
61 char *scriptextension;
63 bool node_read_ecdsa_public_key(node_t *n) {
64 if(ecdsa_active(n->ecdsa))
67 splay_tree_t *config_tree;
72 init_configuration(&config_tree);
73 if(!read_host_config(config_tree, n->name))
76 /* First, check for simple Ed25519PublicKey statement */
78 if(get_config_string(lookup_config(config_tree, "Ed25519PublicKey"), &p)) {
79 n->ecdsa = ecdsa_set_base64_public_key(p);
84 /* Else, check for Ed25519PublicKeyFile statement and read it */
86 if(!get_config_string(lookup_config(config_tree, "Ed25519PublicKeyFile"), &pubname))
87 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
89 fp = fopen(pubname, "r");
94 n->ecdsa = ecdsa_read_pem_public_key(fp);
98 exit_configuration(&config_tree);
103 bool read_ecdsa_public_key(connection_t *c) {
104 if(ecdsa_active(c->ecdsa))
111 if(!c->config_tree) {
112 init_configuration(&c->config_tree);
113 if(!read_host_config(c->config_tree, c->name))
117 /* First, check for simple Ed25519PublicKey statement */
119 if(get_config_string(lookup_config(c->config_tree, "Ed25519PublicKey"), &p)) {
120 c->ecdsa = ecdsa_set_base64_public_key(p);
125 /* Else, check for Ed25519PublicKeyFile statement and read it */
127 if(!get_config_string(lookup_config(c->config_tree, "Ed25519PublicKeyFile"), &fname))
128 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
130 fp = fopen(fname, "r");
133 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading Ed25519 public key file `%s': %s",
134 fname, strerror(errno));
139 c->ecdsa = ecdsa_read_pem_public_key(fp);
143 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing Ed25519 public key file `%s' failed.", fname);
148 bool read_rsa_public_key(connection_t *c) {
149 if(ecdsa_active(c->ecdsa))
156 /* First, check for simple PublicKey statement */
158 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
159 c->rsa = rsa_set_hex_public_key(n, "FFFF");
164 /* Else, check for PublicKeyFile statement and read it */
166 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
167 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
169 fp = fopen(fname, "r");
172 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
177 c->rsa = rsa_read_pem_public_key(fp);
181 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
186 static bool read_ecdsa_private_key(void) {
190 /* Check for PrivateKeyFile statement and read it */
192 if(!get_config_string(lookup_config(config_tree, "Ed25519PrivateKeyFile"), &fname))
193 xasprintf(&fname, "%s" SLASH "ed25519_key.priv", confbase);
195 fp = fopen(fname, "r");
198 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading Ed25519 private key file `%s': %s", fname, strerror(errno));
200 logger(DEBUG_ALWAYS, LOG_INFO, "Create an Ed25519 keypair with `tinc -n %s generate-ed25519-keys'.", netname ?: ".");
205 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
208 if(fstat(fileno(fp), &s)) {
209 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat Ed25519 private key file `%s': %s'", fname, strerror(errno));
214 if(s.st_mode & ~0100700)
215 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for Ed25519 private key file `%s'!", fname);
218 myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
221 if(!myself->connection->ecdsa)
222 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
224 return myself->connection->ecdsa;
227 static bool read_invitation_key(void) {
232 ecdsa_free(invitation_key);
233 invitation_key = NULL;
236 xasprintf(&fname, "%s" SLASH "invitations" SLASH "ed25519_key.priv", confbase);
238 fp = fopen(fname, "r");
241 invitation_key = ecdsa_read_pem_private_key(fp);
244 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
248 return invitation_key;
251 static bool read_rsa_private_key(void) {
256 /* First, check for simple PrivateKey statement */
258 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
259 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
260 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
264 myself->connection->rsa = rsa_set_hex_private_key(n, "FFFF", d);
267 return myself->connection->rsa;
270 /* Else, check for PrivateKeyFile statement and read it */
272 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
273 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
275 fp = fopen(fname, "r");
278 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
279 fname, strerror(errno));
281 logger(DEBUG_ALWAYS, LOG_INFO, "Create an RSA keypair with `tinc -n %s generate-rsa-keys'.", netname ?: ".");
286 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
289 if(fstat(fileno(fp), &s)) {
290 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
295 if(s.st_mode & ~0100700)
296 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
299 myself->connection->rsa = rsa_read_pem_private_key(fp);
302 if(!myself->connection->rsa)
303 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
305 return myself->connection->rsa;
308 static timeout_t keyexpire_timeout;
310 static void keyexpire_handler(void *data) {
312 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
315 void regenerate_key(void) {
316 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
321 Read Subnets from all host config files
323 void load_all_subnets(void) {
328 xasprintf(&dname, "%s" SLASH "hosts", confbase);
329 dir = opendir(dname);
331 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
336 while((ent = readdir(dir))) {
337 if(!check_id(ent->d_name))
340 node_t *n = lookup_node(ent->d_name);
341 #ifdef _DIRENT_HAVE_D_TYPE
342 //if(ent->d_type != DT_REG)
346 splay_tree_t *config_tree;
347 init_configuration(&config_tree);
348 read_config_options(config_tree, ent->d_name);
349 read_host_config(config_tree, ent->d_name);
353 n->name = xstrdup(ent->d_name);
357 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
360 if(!get_config_subnet(cfg, &s))
363 if((s2 = lookup_subnet(n, s))) {
370 exit_configuration(&config_tree);
376 void load_all_nodes(void) {
381 xasprintf(&dname, "%s" SLASH "hosts", confbase);
382 dir = opendir(dname);
384 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
389 while((ent = readdir(dir))) {
390 if(!check_id(ent->d_name))
393 node_t *n = lookup_node(ent->d_name);
398 n->name = xstrdup(ent->d_name);
406 char *get_name(void) {
410 get_config_string(lookup_config(config_tree, "Name"), &name);
415 returned_name = replace_name(name);
417 return returned_name;
420 bool setup_myself_reloadable(void) {
429 free(scriptinterpreter);
430 scriptinterpreter = NULL;
431 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
434 free(scriptextension);
435 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
436 scriptextension = xstrdup("");
438 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
440 if((space = strchr(proxy, ' ')))
443 if(!strcasecmp(proxy, "none")) {
444 proxytype = PROXY_NONE;
445 } else if(!strcasecmp(proxy, "socks4")) {
446 proxytype = PROXY_SOCKS4;
447 } else if(!strcasecmp(proxy, "socks4a")) {
448 proxytype = PROXY_SOCKS4A;
449 } else if(!strcasecmp(proxy, "socks5")) {
450 proxytype = PROXY_SOCKS5;
451 } else if(!strcasecmp(proxy, "http")) {
452 proxytype = PROXY_HTTP;
453 } else if(!strcasecmp(proxy, "exec")) {
454 proxytype = PROXY_EXEC;
456 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
466 if(!space || !*space) {
467 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
470 proxyhost = xstrdup(space);
478 if(space && (space = strchr(space, ' ')))
479 *space++ = 0, proxyport = space;
480 if(space && (space = strchr(space, ' ')))
481 *space++ = 0, proxyuser = space;
482 if(space && (space = strchr(space, ' ')))
483 *space++ = 0, proxypass = space;
484 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
485 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
488 proxyhost = xstrdup(proxyhost);
489 proxyport = xstrdup(proxyport);
490 if(proxyuser && *proxyuser)
491 proxyuser = xstrdup(proxyuser);
492 if(proxypass && *proxypass)
493 proxypass = xstrdup(proxypass);
500 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
501 myself->options |= OPTION_INDIRECT;
503 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
504 myself->options |= OPTION_TCPONLY;
506 if(myself->options & OPTION_TCPONLY)
507 myself->options |= OPTION_INDIRECT;
509 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
510 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
512 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
513 if(!strcasecmp(rmode, "router"))
514 routing_mode = RMODE_ROUTER;
515 else if(!strcasecmp(rmode, "switch"))
516 routing_mode = RMODE_SWITCH;
517 else if(!strcasecmp(rmode, "hub"))
518 routing_mode = RMODE_HUB;
520 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
526 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
527 if(!strcasecmp(fmode, "off"))
528 forwarding_mode = FMODE_OFF;
529 else if(!strcasecmp(fmode, "internal"))
530 forwarding_mode = FMODE_INTERNAL;
531 else if(!strcasecmp(fmode, "kernel"))
532 forwarding_mode = FMODE_KERNEL;
534 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
541 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
543 myself->options |= OPTION_PMTU_DISCOVERY;
546 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
548 myself->options |= OPTION_CLAMP_MSS;
550 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
551 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
552 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
553 if(!strcasecmp(bmode, "no"))
554 broadcast_mode = BMODE_NONE;
555 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
556 broadcast_mode = BMODE_MST;
557 else if(!strcasecmp(bmode, "direct"))
558 broadcast_mode = BMODE_DIRECT;
560 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
566 const char* const DEFAULT_BROADCAST_SUBNETS[] = { "ff:ff:ff:ff:ff:ff", "255.255.255.255", "224.0.0.0/4", "ff00::/8" };
567 for (size_t i = 0; i < sizeof(DEFAULT_BROADCAST_SUBNETS) / sizeof(*DEFAULT_BROADCAST_SUBNETS); i++) {
568 subnet_t *s = new_subnet();
569 if (!str2net(s, DEFAULT_BROADCAST_SUBNETS[i]))
573 for (config_t* cfg = lookup_config(config_tree, "BroadcastSubnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
575 if (!get_config_subnet(cfg, &s))
580 #if !defined(SOL_IP) || !defined(IP_TOS)
581 if(priorityinheritance)
582 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
585 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
588 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
589 if(maxtimeout <= 0) {
590 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
596 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
597 if(!strcasecmp(afname, "IPv4"))
598 addressfamily = AF_INET;
599 else if(!strcasecmp(afname, "IPv6"))
600 addressfamily = AF_INET6;
601 else if(!strcasecmp(afname, "any"))
602 addressfamily = AF_UNSPEC;
604 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
610 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
612 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
615 config_t *cfg = lookup_config(config_tree, "AutoConnect");
617 if(!get_config_bool(cfg, &autoconnect)) {
618 // Some backwards compatibility with when this option was an int
620 get_config_int(cfg, &val);
625 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
627 read_invitation_key();
633 Add listening sockets.
635 static bool add_listen_address(char *address, bool bindto) {
639 char *space = strchr(address, ' ');
645 if(!strcmp(address, "*"))
649 struct addrinfo *ai, hint = {0};
650 hint.ai_family = addressfamily;
651 hint.ai_socktype = SOCK_STREAM;
652 hint.ai_protocol = IPPROTO_TCP;
653 hint.ai_flags = AI_PASSIVE;
655 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
659 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
663 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
664 // Ignore duplicate addresses
667 for(int i = 0; i < listen_sockets; i++)
668 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
676 if(listen_sockets >= MAXSOCKETS) {
677 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
681 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
686 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
693 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
694 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
696 if(debug_level >= DEBUG_CONNECTIONS) {
697 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
698 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
702 listen_socket[listen_sockets].bindto = bindto;
703 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
711 void device_enable(void) {
715 /* Run tinc-up script to further initialize the tap interface */
717 char *envp[5] = {NULL};
718 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
719 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
720 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
721 xasprintf(&envp[3], "NAME=%s", myname);
723 execute_script("tinc-up", envp);
725 for(int i = 0; i < 4; i++)
729 void device_disable(void) {
730 char *envp[5] = {NULL};
731 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
732 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
733 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
734 xasprintf(&envp[3], "NAME=%s", myname);
736 execute_script("tinc-down", envp);
738 for(int i = 0; i < 4; i++)
746 Configure node_t myself and set up the local sockets (listen only)
748 static bool setup_myself(void) {
749 char *name, *hostname, *cipher, *digest, *type;
750 char *address = NULL;
751 bool port_specified = false;
753 if(!(name = get_name())) {
754 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
758 myname = xstrdup(name);
760 myself->connection = new_connection();
762 myself->connection->name = xstrdup(name);
763 read_host_config(config_tree, name);
765 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
766 myport = xstrdup("655");
768 port_specified = true;
770 myself->connection->options = 0;
771 myself->connection->protocol_major = PROT_MAJOR;
772 myself->connection->protocol_minor = PROT_MINOR;
774 myself->options |= PROT_MINOR << 24;
776 if(!get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental)) {
777 experimental = read_ecdsa_private_key();
779 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for SPTPS disabled.");
781 if(experimental && !read_ecdsa_private_key())
785 if(!read_rsa_private_key()) {
787 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for legacy protocol disabled.");
789 logger(DEBUG_ALWAYS, LOG_ERR, "No private keys available, cannot start tinc!");
794 /* Ensure myport is numeric */
797 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
799 if(!ai || !ai->ai_addr)
802 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
803 sockaddr2str(&sa, NULL, &myport);
806 /* Read in all the subnets specified in the host configuration file */
808 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
811 if(!get_config_subnet(cfg, &subnet))
814 subnet_add(myself, subnet);
817 /* Check some options */
819 if(!setup_myself_reloadable())
822 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
823 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
824 strictsubnets |= tunnelserver;
826 if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
827 if(max_connection_burst <= 0) {
828 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
833 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
834 if(udp_rcvbuf <= 0) {
835 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
840 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
841 if(udp_sndbuf <= 0) {
842 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
848 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
849 if(replaywin_int < 0) {
850 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
853 replaywin = (unsigned)replaywin_int;
854 sptps_replaywin = replaywin;
857 /* Generate packet encryption key */
859 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
860 cipher = xstrdup("blowfish");
862 if(!strcasecmp(cipher, "none")) {
863 myself->incipher = NULL;
864 } else if(!(myself->incipher = cipher_open_by_name(cipher))) {
865 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
871 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
873 /* Check if we want to use message authentication codes... */
876 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
879 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
883 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
884 digest = xstrdup("sha1");
886 if(!strcasecmp(digest, "none")) {
887 myself->indigest = NULL;
888 } else if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
889 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
897 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
898 if(myself->incompression < 0 || myself->incompression > 11) {
899 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
903 myself->incompression = 0;
905 myself->connection->outcompression = 0;
909 myself->nexthop = myself;
910 myself->via = myself;
911 myself->status.reachable = true;
912 myself->last_state_change = now.tv_sec;
913 myself->status.sptps = experimental;
927 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
928 if(!strcasecmp(type, "dummy"))
929 devops = dummy_devops;
930 else if(!strcasecmp(type, "raw_socket"))
931 devops = raw_socket_devops;
932 else if(!strcasecmp(type, "multicast"))
933 devops = multicast_devops;
935 else if(!strcasecmp(type, "uml"))
939 else if(!strcasecmp(type, "vde"))
944 get_config_bool(lookup_config(config_tree, "DeviceStandby"), &device_standby);
950 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
954 if(!do_detach && getenv("LISTEN_FDS")) {
958 listen_sockets = atoi(getenv("LISTEN_FDS"));
960 unsetenv("LISTEN_FDS");
963 if(listen_sockets > MAXSOCKETS) {
964 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
968 for(int i = 0; i < listen_sockets; i++) {
970 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
971 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(sockerrno));
976 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
979 int udp_fd = setup_vpn_in_socket(&sa);
983 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
984 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
986 if(debug_level >= DEBUG_CONNECTIONS) {
987 hostname = sockaddr2hostname(&sa);
988 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
992 memcpy(&listen_socket[i].sa, &sa, salen);
998 for(config_t *cfg = lookup_config(config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1000 get_config_string(cfg, &address);
1001 if(!add_listen_address(address, true))
1005 for(config_t *cfg = lookup_config(config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1007 get_config_string(cfg, &address);
1008 if(!add_listen_address(address, false))
1013 if(!add_listen_address(address, NULL))
1017 if(!listen_sockets) {
1018 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
1022 /* If no Port option was specified, set myport to the port used by the first listening socket. */
1024 if(!port_specified || atoi(myport) == 0) {
1026 socklen_t salen = sizeof sa;
1027 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
1029 sockaddr2str(&sa, NULL, &myport);
1031 myport = xstrdup("655");
1035 xasprintf(&myself->hostname, "MYSELF port %s", myport);
1036 myself->connection->hostname = xstrdup(myself->hostname);
1040 last_config_check = now.tv_sec;
1048 bool setup_network(void) {
1055 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
1056 if(pinginterval < 1) {
1057 pinginterval = 86400;
1062 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
1064 if(pingtimeout < 1 || pingtimeout > pinginterval)
1065 pingtimeout = pinginterval;
1067 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
1068 maxoutbufsize = 10 * MTU;
1076 if (!device_standby)
1079 /* Run subnet-up scripts for our own subnets */
1081 subnet_update(myself, NULL, true);
1087 close all open network connections
1089 void close_network_connections(void) {
1090 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1092 connection_t *c = node->data;
1093 /* Keep control connections open until the end, so they know when we really terminated */
1094 if(c->status.control)
1097 terminate_connection(c, false);
1101 list_delete_list(outgoing_list);
1103 if(myself && myself->connection) {
1104 subnet_update(myself, NULL, false);
1105 terminate_connection(myself->connection, false);
1106 free_connection(myself->connection);
1109 for(int i = 0; i < listen_sockets; i++) {
1110 io_del(&listen_socket[i].tcp);
1111 io_del(&listen_socket[i].udp);
1112 close(listen_socket[i].tcp.fd);
1113 close(listen_socket[i].udp.fd);
1122 if (!device_standby)
1135 free(scriptextension);
1136 free(scriptinterpreter);