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 #ifndef DISABLE_LEGACY
149 bool read_rsa_public_key(connection_t *c) {
150 if(ecdsa_active(c->ecdsa))
157 /* First, check for simple PublicKey statement */
159 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
160 c->rsa = rsa_set_hex_public_key(n, "FFFF");
165 /* Else, check for PublicKeyFile statement and read it */
167 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
168 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
170 fp = fopen(fname, "r");
173 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
178 c->rsa = rsa_read_pem_public_key(fp);
182 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
188 static bool read_ecdsa_private_key(void) {
192 /* Check for PrivateKeyFile statement and read it */
194 if(!get_config_string(lookup_config(config_tree, "Ed25519PrivateKeyFile"), &fname))
195 xasprintf(&fname, "%s" SLASH "ed25519_key.priv", confbase);
197 fp = fopen(fname, "r");
200 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading Ed25519 private key file `%s': %s", fname, strerror(errno));
202 logger(DEBUG_ALWAYS, LOG_INFO, "Create an Ed25519 keypair with `tinc -n %s generate-ed25519-keys'.", netname ?: ".");
207 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
210 if(fstat(fileno(fp), &s)) {
211 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat Ed25519 private key file `%s': %s'", fname, strerror(errno));
216 if(s.st_mode & ~0100700)
217 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for Ed25519 private key file `%s'!", fname);
220 myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
223 if(!myself->connection->ecdsa)
224 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
226 return myself->connection->ecdsa;
229 static bool read_invitation_key(void) {
234 ecdsa_free(invitation_key);
235 invitation_key = NULL;
238 xasprintf(&fname, "%s" SLASH "invitations" SLASH "ed25519_key.priv", confbase);
240 fp = fopen(fname, "r");
243 invitation_key = ecdsa_read_pem_private_key(fp);
246 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");
325 Read Subnets from all host config files
327 void load_all_subnets(void) {
332 xasprintf(&dname, "%s" SLASH "hosts", confbase);
333 dir = opendir(dname);
335 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
340 while((ent = readdir(dir))) {
341 if(!check_id(ent->d_name))
344 node_t *n = lookup_node(ent->d_name);
345 #ifdef _DIRENT_HAVE_D_TYPE
346 //if(ent->d_type != DT_REG)
350 splay_tree_t *config_tree;
351 init_configuration(&config_tree);
352 read_config_options(config_tree, ent->d_name);
353 read_host_config(config_tree, ent->d_name);
357 n->name = xstrdup(ent->d_name);
361 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
364 if(!get_config_subnet(cfg, &s))
367 if((s2 = lookup_subnet(n, s))) {
374 exit_configuration(&config_tree);
380 void load_all_nodes(void) {
385 xasprintf(&dname, "%s" SLASH "hosts", confbase);
386 dir = opendir(dname);
388 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
393 while((ent = readdir(dir))) {
394 if(!check_id(ent->d_name))
397 node_t *n = lookup_node(ent->d_name);
402 n->name = xstrdup(ent->d_name);
410 char *get_name(void) {
414 get_config_string(lookup_config(config_tree, "Name"), &name);
419 returned_name = replace_name(name);
421 return returned_name;
424 bool setup_myself_reloadable(void) {
433 free(scriptinterpreter);
434 scriptinterpreter = NULL;
435 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
438 free(scriptextension);
439 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
440 scriptextension = xstrdup("");
442 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
444 if((space = strchr(proxy, ' ')))
447 if(!strcasecmp(proxy, "none")) {
448 proxytype = PROXY_NONE;
449 } else if(!strcasecmp(proxy, "socks4")) {
450 proxytype = PROXY_SOCKS4;
451 } else if(!strcasecmp(proxy, "socks4a")) {
452 proxytype = PROXY_SOCKS4A;
453 } else if(!strcasecmp(proxy, "socks5")) {
454 proxytype = PROXY_SOCKS5;
455 } else if(!strcasecmp(proxy, "http")) {
456 proxytype = PROXY_HTTP;
457 } else if(!strcasecmp(proxy, "exec")) {
458 proxytype = PROXY_EXEC;
460 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
470 if(!space || !*space) {
471 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
474 proxyhost = xstrdup(space);
482 if(space && (space = strchr(space, ' ')))
483 *space++ = 0, proxyport = space;
484 if(space && (space = strchr(space, ' ')))
485 *space++ = 0, proxyuser = space;
486 if(space && (space = strchr(space, ' ')))
487 *space++ = 0, proxypass = space;
488 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
489 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
492 proxyhost = xstrdup(proxyhost);
493 proxyport = xstrdup(proxyport);
494 if(proxyuser && *proxyuser)
495 proxyuser = xstrdup(proxyuser);
496 if(proxypass && *proxypass)
497 proxypass = xstrdup(proxypass);
504 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
505 myself->options |= OPTION_INDIRECT;
507 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
508 myself->options |= OPTION_TCPONLY;
510 if(myself->options & OPTION_TCPONLY)
511 myself->options |= OPTION_INDIRECT;
513 get_config_bool(lookup_config(config_tree, "UDPDiscovery"), &udp_discovery);
514 get_config_int(lookup_config(config_tree, "UDPDiscoveryInterval"), &udp_discovery_interval);
515 get_config_int(lookup_config(config_tree, "UDPDiscoveryTimeout"), &udp_discovery_timeout);
517 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
518 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
520 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
521 if(!strcasecmp(rmode, "router"))
522 routing_mode = RMODE_ROUTER;
523 else if(!strcasecmp(rmode, "switch"))
524 routing_mode = RMODE_SWITCH;
525 else if(!strcasecmp(rmode, "hub"))
526 routing_mode = RMODE_HUB;
528 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
534 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
535 if(!strcasecmp(fmode, "off"))
536 forwarding_mode = FMODE_OFF;
537 else if(!strcasecmp(fmode, "internal"))
538 forwarding_mode = FMODE_INTERNAL;
539 else if(!strcasecmp(fmode, "kernel"))
540 forwarding_mode = FMODE_KERNEL;
542 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
549 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
551 myself->options |= OPTION_PMTU_DISCOVERY;
554 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
556 myself->options |= OPTION_CLAMP_MSS;
558 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
559 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
560 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
561 if(!strcasecmp(bmode, "no"))
562 broadcast_mode = BMODE_NONE;
563 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
564 broadcast_mode = BMODE_MST;
565 else if(!strcasecmp(bmode, "direct"))
566 broadcast_mode = BMODE_DIRECT;
568 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
574 const char* const DEFAULT_BROADCAST_SUBNETS[] = { "ff:ff:ff:ff:ff:ff", "255.255.255.255", "224.0.0.0/4", "ff00::/8" };
575 for (size_t i = 0; i < sizeof(DEFAULT_BROADCAST_SUBNETS) / sizeof(*DEFAULT_BROADCAST_SUBNETS); i++) {
576 subnet_t *s = new_subnet();
577 if (!str2net(s, DEFAULT_BROADCAST_SUBNETS[i]))
581 for (config_t* cfg = lookup_config(config_tree, "BroadcastSubnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
583 if (!get_config_subnet(cfg, &s))
588 #if !defined(SOL_IP) || !defined(IP_TOS)
589 if(priorityinheritance)
590 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
593 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
596 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
597 if(maxtimeout <= 0) {
598 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
604 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
605 if(!strcasecmp(afname, "IPv4"))
606 addressfamily = AF_INET;
607 else if(!strcasecmp(afname, "IPv6"))
608 addressfamily = AF_INET6;
609 else if(!strcasecmp(afname, "any"))
610 addressfamily = AF_UNSPEC;
612 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
618 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
620 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
623 config_t *cfg = lookup_config(config_tree, "AutoConnect");
625 if(!get_config_bool(cfg, &autoconnect)) {
626 // Some backwards compatibility with when this option was an int
628 get_config_int(cfg, &val);
633 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
635 read_invitation_key();
641 Add listening sockets.
643 static bool add_listen_address(char *address, bool bindto) {
647 char *space = strchr(address, ' ');
653 if(!strcmp(address, "*"))
657 struct addrinfo *ai, hint = {0};
658 hint.ai_family = addressfamily;
659 hint.ai_socktype = SOCK_STREAM;
660 hint.ai_protocol = IPPROTO_TCP;
661 hint.ai_flags = AI_PASSIVE;
663 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
667 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
671 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
672 // Ignore duplicate addresses
675 for(int i = 0; i < listen_sockets; i++)
676 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
684 if(listen_sockets >= MAXSOCKETS) {
685 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
689 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
694 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
701 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
702 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
704 if(debug_level >= DEBUG_CONNECTIONS) {
705 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
706 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
710 listen_socket[listen_sockets].bindto = bindto;
711 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
719 void device_enable(void) {
723 /* Run tinc-up script to further initialize the tap interface */
725 char *envp[5] = {NULL};
726 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
727 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
728 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
729 xasprintf(&envp[3], "NAME=%s", myname);
731 execute_script("tinc-up", envp);
733 for(int i = 0; i < 4; i++)
737 void device_disable(void) {
738 char *envp[5] = {NULL};
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", myname);
744 execute_script("tinc-down", envp);
746 for(int i = 0; i < 4; i++)
754 Configure node_t myself and set up the local sockets (listen only)
756 static bool setup_myself(void) {
757 char *name, *hostname, *cipher, *digest, *type;
758 char *address = NULL;
759 bool port_specified = false;
761 if(!(name = get_name())) {
762 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
766 myname = xstrdup(name);
768 myself->connection = new_connection();
770 myself->connection->name = xstrdup(name);
771 read_host_config(config_tree, name);
773 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
774 myport = xstrdup("655");
776 port_specified = true;
778 myself->connection->options = 0;
779 myself->connection->protocol_major = PROT_MAJOR;
780 myself->connection->protocol_minor = PROT_MINOR;
782 myself->options |= PROT_MINOR << 24;
784 #ifdef DISABLE_LEGACY
785 experimental = read_ecdsa_private_key();
787 logger(DEBUG_ALWAYS, LOG_ERR, "No private key available, cannot start tinc!");
791 if(!get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental)) {
792 experimental = read_ecdsa_private_key();
794 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for SPTPS disabled.");
796 if(experimental && !read_ecdsa_private_key())
800 if(!read_rsa_private_key()) {
802 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for legacy protocol disabled.");
804 logger(DEBUG_ALWAYS, LOG_ERR, "No private keys available, cannot start tinc!");
810 /* Ensure myport is numeric */
813 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
815 if(!ai || !ai->ai_addr)
818 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
819 sockaddr2str(&sa, NULL, &myport);
822 /* Read in all the subnets specified in the host configuration file */
824 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
827 if(!get_config_subnet(cfg, &subnet))
830 subnet_add(myself, subnet);
833 /* Check some options */
835 if(!setup_myself_reloadable())
838 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
839 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
840 strictsubnets |= tunnelserver;
842 if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
843 if(max_connection_burst <= 0) {
844 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
849 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
850 if(udp_rcvbuf <= 0) {
851 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
856 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
857 if(udp_sndbuf <= 0) {
858 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
864 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
865 if(replaywin_int < 0) {
866 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
869 replaywin = (unsigned)replaywin_int;
870 sptps_replaywin = replaywin;
873 #ifndef DISABLE_LEGACY
874 /* Generate packet encryption key */
876 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
877 cipher = xstrdup("blowfish");
879 if(!strcasecmp(cipher, "none")) {
880 myself->incipher = NULL;
881 } else if(!(myself->incipher = cipher_open_by_name(cipher))) {
882 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
888 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
890 /* Check if we want to use message authentication codes... */
893 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
896 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
900 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
901 digest = xstrdup("sha1");
903 if(!strcasecmp(digest, "none")) {
904 myself->indigest = NULL;
905 } else if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
906 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
915 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
916 if(myself->incompression < 0 || myself->incompression > 11) {
917 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
921 myself->incompression = 0;
923 myself->connection->outcompression = 0;
927 myself->nexthop = myself;
928 myself->via = myself;
929 myself->status.reachable = true;
930 myself->last_state_change = now.tv_sec;
931 myself->status.sptps = experimental;
945 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
946 if(!strcasecmp(type, "dummy"))
947 devops = dummy_devops;
948 else if(!strcasecmp(type, "raw_socket"))
949 devops = raw_socket_devops;
950 else if(!strcasecmp(type, "multicast"))
951 devops = multicast_devops;
953 else if(!strcasecmp(type, "uml"))
957 else if(!strcasecmp(type, "vde"))
962 get_config_bool(lookup_config(config_tree, "DeviceStandby"), &device_standby);
968 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
972 if(!do_detach && getenv("LISTEN_FDS")) {
976 listen_sockets = atoi(getenv("LISTEN_FDS"));
978 unsetenv("LISTEN_FDS");
981 if(listen_sockets > MAXSOCKETS) {
982 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
986 for(int i = 0; i < listen_sockets; i++) {
988 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
989 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(sockerrno));
994 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
997 int udp_fd = setup_vpn_in_socket(&sa);
1001 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
1002 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
1004 if(debug_level >= DEBUG_CONNECTIONS) {
1005 hostname = sockaddr2hostname(&sa);
1006 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
1010 memcpy(&listen_socket[i].sa, &sa, salen);
1016 for(config_t *cfg = lookup_config(config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1018 get_config_string(cfg, &address);
1019 if(!add_listen_address(address, true))
1023 for(config_t *cfg = lookup_config(config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1025 get_config_string(cfg, &address);
1026 if(!add_listen_address(address, false))
1031 if(!add_listen_address(address, NULL))
1035 if(!listen_sockets) {
1036 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
1040 /* If no Port option was specified, set myport to the port used by the first listening socket. */
1042 if(!port_specified || atoi(myport) == 0) {
1044 socklen_t salen = sizeof sa;
1045 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
1047 sockaddr2str(&sa, NULL, &myport);
1049 myport = xstrdup("655");
1053 xasprintf(&myself->hostname, "MYSELF port %s", myport);
1054 myself->connection->hostname = xstrdup(myself->hostname);
1058 last_config_check = now.tv_sec;
1066 bool setup_network(void) {
1073 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
1074 if(pinginterval < 1) {
1075 pinginterval = 86400;
1080 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
1082 if(pingtimeout < 1 || pingtimeout > pinginterval)
1083 pingtimeout = pinginterval;
1085 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
1086 maxoutbufsize = 10 * MTU;
1094 if (!device_standby)
1097 /* Run subnet-up scripts for our own subnets */
1099 subnet_update(myself, NULL, true);
1105 close all open network connections
1107 void close_network_connections(void) {
1108 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1110 connection_t *c = node->data;
1111 /* Keep control connections open until the end, so they know when we really terminated */
1112 if(c->status.control)
1115 terminate_connection(c, false);
1119 list_delete_list(outgoing_list);
1121 if(myself && myself->connection) {
1122 subnet_update(myself, NULL, false);
1123 terminate_connection(myself->connection, false);
1124 free_connection(myself->connection);
1127 for(int i = 0; i < listen_sockets; i++) {
1128 io_del(&listen_socket[i].tcp);
1129 io_del(&listen_socket[i].udp);
1130 close(listen_socket[i].tcp.fd);
1131 close(listen_socket[i].udp.fd);
1140 if (!device_standby)
1153 free(scriptextension);
1154 free(scriptinterpreter);