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"
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);
141 if(!c->ecdsa && errno != ENOENT)
142 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing Ed25519 public key file `%s' failed.", fname);
149 #ifndef DISABLE_LEGACY
150 bool read_rsa_public_key(connection_t *c) {
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));
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) {
229 char fname[PATH_MAX];
232 ecdsa_free(invitation_key);
233 invitation_key = NULL;
236 snprintf(fname, sizeof 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);
247 return invitation_key;
250 #ifndef DISABLE_LEGACY
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;
309 static timeout_t keyexpire_timeout;
311 static void keyexpire_handler(void *data) {
313 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
316 void regenerate_key(void) {
317 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
319 for splay_each(node_t, n, node_tree)
320 n->status.validkey_in = false;
324 Read Subnets from all host config files
326 void load_all_subnets(void) {
329 char dname[PATH_MAX];
331 snprintf(dname, sizeof dname, "%s" SLASH "hosts", confbase);
332 dir = opendir(dname);
334 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
338 while((ent = readdir(dir))) {
339 if(!check_id(ent->d_name))
342 node_t *n = lookup_node(ent->d_name);
343 #ifdef _DIRENT_HAVE_D_TYPE
344 //if(ent->d_type != DT_REG)
348 splay_tree_t *config_tree;
349 init_configuration(&config_tree);
350 read_config_options(config_tree, ent->d_name);
351 read_host_config(config_tree, ent->d_name);
355 n->name = xstrdup(ent->d_name);
359 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
362 if(!get_config_subnet(cfg, &s))
365 if((s2 = lookup_subnet(n, s))) {
372 exit_configuration(&config_tree);
378 void load_all_nodes(void) {
381 char dname[PATH_MAX];
383 snprintf(dname, sizeof dname, "%s" SLASH "hosts", confbase);
384 dir = opendir(dname);
386 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
390 while((ent = readdir(dir))) {
391 if(!check_id(ent->d_name))
394 node_t *n = lookup_node(ent->d_name);
399 n->name = xstrdup(ent->d_name);
407 char *get_name(void) {
411 get_config_string(lookup_config(config_tree, "Name"), &name);
416 returned_name = replace_name(name);
418 return returned_name;
421 bool setup_myself_reloadable(void) {
430 free(scriptinterpreter);
431 scriptinterpreter = NULL;
432 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
435 free(scriptextension);
436 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
437 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, "UDPDiscovery"), &udp_discovery);
511 get_config_int(lookup_config(config_tree, "UDPDiscoveryKeepaliveInterval"), &udp_discovery_keepalive_interval);
512 get_config_int(lookup_config(config_tree, "UDPDiscoveryInterval"), &udp_discovery_interval);
513 get_config_int(lookup_config(config_tree, "UDPDiscoveryTimeout"), &udp_discovery_timeout);
515 get_config_int(lookup_config(config_tree, "MTUInfoInterval"), &mtu_info_interval);
516 get_config_int(lookup_config(config_tree, "UDPInfoInterval"), &udp_info_interval);
518 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
519 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
521 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
522 if(!strcasecmp(rmode, "router"))
523 routing_mode = RMODE_ROUTER;
524 else if(!strcasecmp(rmode, "switch"))
525 routing_mode = RMODE_SWITCH;
526 else if(!strcasecmp(rmode, "hub"))
527 routing_mode = RMODE_HUB;
529 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
535 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
536 if(!strcasecmp(fmode, "off"))
537 forwarding_mode = FMODE_OFF;
538 else if(!strcasecmp(fmode, "internal"))
539 forwarding_mode = FMODE_INTERNAL;
540 else if(!strcasecmp(fmode, "kernel"))
541 forwarding_mode = FMODE_KERNEL;
543 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
550 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
552 myself->options |= OPTION_PMTU_DISCOVERY;
555 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
557 myself->options |= OPTION_CLAMP_MSS;
559 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
560 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
561 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
562 if(!strcasecmp(bmode, "no"))
563 broadcast_mode = BMODE_NONE;
564 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
565 broadcast_mode = BMODE_MST;
566 else if(!strcasecmp(bmode, "direct"))
567 broadcast_mode = BMODE_DIRECT;
569 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
575 const char* const DEFAULT_BROADCAST_SUBNETS[] = { "ff:ff:ff:ff:ff:ff", "255.255.255.255", "224.0.0.0/4", "ff00::/8" };
576 for (size_t i = 0; i < sizeof(DEFAULT_BROADCAST_SUBNETS) / sizeof(*DEFAULT_BROADCAST_SUBNETS); i++) {
577 subnet_t *s = new_subnet();
578 if (!str2net(s, DEFAULT_BROADCAST_SUBNETS[i]))
582 for (config_t* cfg = lookup_config(config_tree, "BroadcastSubnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
584 if (!get_config_subnet(cfg, &s))
589 #if !defined(SOL_IP) || !defined(IP_TOS)
590 if(priorityinheritance)
591 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
594 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
597 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
598 if(maxtimeout <= 0) {
599 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
605 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
606 if(!strcasecmp(afname, "IPv4"))
607 addressfamily = AF_INET;
608 else if(!strcasecmp(afname, "IPv6"))
609 addressfamily = AF_INET6;
610 else if(!strcasecmp(afname, "any"))
611 addressfamily = AF_UNSPEC;
613 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
619 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
621 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
624 config_t *cfg = lookup_config(config_tree, "AutoConnect");
626 if(!get_config_bool(cfg, &autoconnect)) {
627 // Some backwards compatibility with when this option was an int
629 get_config_int(cfg, &val);
634 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
636 read_invitation_key();
642 Add listening sockets.
644 static bool add_listen_address(char *address, bool bindto) {
648 char *space = strchr(address, ' ');
654 if(!strcmp(address, "*"))
658 struct addrinfo *ai, hint = {0};
659 hint.ai_family = addressfamily;
660 hint.ai_socktype = SOCK_STREAM;
661 hint.ai_protocol = IPPROTO_TCP;
662 hint.ai_flags = AI_PASSIVE;
664 #if HAVE_DECL_RES_INIT
667 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
671 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
675 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
676 // Ignore duplicate addresses
679 for(int i = 0; i < listen_sockets; i++)
680 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
688 if(listen_sockets >= MAXSOCKETS) {
689 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
693 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
698 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
705 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
706 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
708 if(debug_level >= DEBUG_CONNECTIONS) {
709 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
710 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
714 listen_socket[listen_sockets].bindto = bindto;
715 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
723 void device_enable(void) {
727 /* Run tinc-up script to further initialize the tap interface */
729 char *envp[5] = {NULL};
730 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
731 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
732 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
733 xasprintf(&envp[3], "NAME=%s", myname);
735 execute_script("tinc-up", envp);
737 for(int i = 0; i < 4; i++)
741 void device_disable(void) {
742 char *envp[5] = {NULL};
743 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
744 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
745 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
746 xasprintf(&envp[3], "NAME=%s", myname);
748 execute_script("tinc-down", envp);
750 for(int i = 0; i < 4; i++)
758 Configure node_t myself and set up the local sockets (listen only)
760 static bool setup_myself(void) {
761 char *name, *hostname, *cipher, *digest, *type;
762 char *address = NULL;
763 bool port_specified = false;
765 if(!(name = get_name())) {
766 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
770 myname = xstrdup(name);
772 myself->connection = new_connection();
774 myself->connection->name = xstrdup(name);
775 read_host_config(config_tree, name);
777 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
778 myport = xstrdup("655");
780 port_specified = true;
782 myself->connection->options = 0;
783 myself->connection->protocol_major = PROT_MAJOR;
784 myself->connection->protocol_minor = PROT_MINOR;
786 myself->options |= PROT_MINOR << 24;
788 #ifdef DISABLE_LEGACY
789 experimental = read_ecdsa_private_key();
791 logger(DEBUG_ALWAYS, LOG_ERR, "No private key available, cannot start tinc!");
795 if(!get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental)) {
796 experimental = read_ecdsa_private_key();
798 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for SPTPS disabled.");
800 if(experimental && !read_ecdsa_private_key())
804 if(!read_rsa_private_key()) {
806 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for legacy protocol disabled.");
808 logger(DEBUG_ALWAYS, LOG_ERR, "No private keys available, cannot start tinc!");
814 /* Ensure myport is numeric */
817 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
819 if(!ai || !ai->ai_addr)
822 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
823 sockaddr2str(&sa, NULL, &myport);
826 /* Read in all the subnets specified in the host configuration file */
828 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
831 if(!get_config_subnet(cfg, &subnet))
834 subnet_add(myself, subnet);
837 /* Check some options */
839 if(!setup_myself_reloadable())
842 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
843 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
844 strictsubnets |= tunnelserver;
846 if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
847 if(max_connection_burst <= 0) {
848 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
853 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
855 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
860 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
862 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
868 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
869 if(replaywin_int < 0) {
870 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
873 replaywin = (unsigned)replaywin_int;
874 sptps_replaywin = replaywin;
877 #ifndef DISABLE_LEGACY
878 /* Generate packet encryption key */
880 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
881 cipher = xstrdup("blowfish");
883 if(!strcasecmp(cipher, "none")) {
884 myself->incipher = NULL;
885 } else if(!(myself->incipher = cipher_open_by_name(cipher))) {
886 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
892 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
894 /* Check if we want to use message authentication codes... */
897 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
900 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
904 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
905 digest = xstrdup("sha1");
907 if(!strcasecmp(digest, "none")) {
908 myself->indigest = NULL;
909 } else if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
910 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
919 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
920 if(myself->incompression < 0 || myself->incompression > 11) {
921 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
925 myself->incompression = 0;
927 myself->connection->outcompression = 0;
931 myself->nexthop = myself;
932 myself->via = myself;
933 myself->status.reachable = true;
934 myself->last_state_change = now.tv_sec;
935 myself->status.sptps = experimental;
949 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
950 if(!strcasecmp(type, "dummy"))
951 devops = dummy_devops;
952 else if(!strcasecmp(type, "raw_socket"))
953 devops = raw_socket_devops;
954 else if(!strcasecmp(type, "multicast"))
955 devops = multicast_devops;
957 else if(!strcasecmp(type, "uml"))
961 else if(!strcasecmp(type, "vde"))
966 get_config_bool(lookup_config(config_tree, "DeviceStandby"), &device_standby);
972 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
976 if(!do_detach && getenv("LISTEN_FDS")) {
980 listen_sockets = atoi(getenv("LISTEN_FDS"));
982 unsetenv("LISTEN_FDS");
985 if(listen_sockets > MAXSOCKETS) {
986 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
990 for(int i = 0; i < listen_sockets; i++) {
992 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
993 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(sockerrno));
998 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
1001 int udp_fd = setup_vpn_in_socket(&sa);
1005 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
1006 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
1008 if(debug_level >= DEBUG_CONNECTIONS) {
1009 hostname = sockaddr2hostname(&sa);
1010 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
1014 memcpy(&listen_socket[i].sa, &sa, salen);
1020 for(config_t *cfg = lookup_config(config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1022 get_config_string(cfg, &address);
1023 if(!add_listen_address(address, true))
1027 for(config_t *cfg = lookup_config(config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1029 get_config_string(cfg, &address);
1030 if(!add_listen_address(address, false))
1035 if(!add_listen_address(address, NULL))
1039 if(!listen_sockets) {
1040 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
1044 /* If no Port option was specified, set myport to the port used by the first listening socket. */
1046 if(!port_specified || atoi(myport) == 0) {
1048 socklen_t salen = sizeof sa;
1049 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
1051 sockaddr2str(&sa, NULL, &myport);
1053 myport = xstrdup("655");
1057 xasprintf(&myself->hostname, "MYSELF port %s", myport);
1058 myself->connection->hostname = xstrdup(myself->hostname);
1062 last_config_check = now.tv_sec;
1070 bool setup_network(void) {
1077 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
1078 if(pinginterval < 1) {
1079 pinginterval = 86400;
1084 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
1086 if(pingtimeout < 1 || pingtimeout > pinginterval)
1087 pingtimeout = pinginterval;
1089 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
1090 maxoutbufsize = 10 * MTU;
1098 if (!device_standby)
1101 /* Run subnet-up scripts for our own subnets */
1103 subnet_update(myself, NULL, true);
1109 close all open network connections
1111 void close_network_connections(void) {
1112 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1114 connection_t *c = node->data;
1115 /* Keep control connections open until the end, so they know when we really terminated */
1116 if(c->status.control)
1119 terminate_connection(c, false);
1123 list_delete_list(outgoing_list);
1125 if(myself && myself->connection) {
1126 subnet_update(myself, NULL, false);
1127 terminate_connection(myself->connection, false);
1128 free_connection(myself->connection);
1131 for(int i = 0; i < listen_sockets; i++) {
1132 io_del(&listen_socket[i].tcp);
1133 io_del(&listen_socket[i].udp);
1134 close(listen_socket[i].tcp.fd);
1135 close(listen_socket[i].udp.fd);
1144 if (!device_standby)
1157 free(scriptextension);
1158 free(scriptinterpreter);