2 protocol_auth.c -- handle the meta-protocol, authentication
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2022 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "connection.h"
26 #include "control_common.h"
46 #include "ed25519/sha512.h"
49 /* If nonzero, use null ciphers and skip all key exchanges. */
50 bool bypass_security = false;
52 int invitation_lifetime;
53 ecdsa_t *invitation_key = NULL;
55 static bool send_proxyrequest(connection_t *c) {
61 sockaddr2str(&c->address, &host, &port);
62 send_request(c, "CONNECT %s:%s HTTP/1.1\r\n\r", host, port);
69 if(c->address.sa.sa_family != AF_INET) {
70 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot connect to an IPv6 host through a SOCKS 4 proxy!");
74 const size_t s4reqlen = 9 + (proxyuser ? strlen(proxyuser) : 0);
75 uint8_t *s4req = alloca(s4reqlen);
78 memcpy(s4req + 2, &c->address.in.sin_port, 2);
79 memcpy(s4req + 4, &c->address.in.sin_addr, 4);
82 memcpy(s4req + 8, proxyuser, strlen(proxyuser));
85 s4req[s4reqlen - 1] = 0;
87 return send_meta(c, s4req, s4reqlen);
91 size_t len = 3 + 6 + (c->address.sa.sa_family == AF_INET ? 4 : 16);
95 len += 3 + strlen(proxyuser) + strlen(proxypass);
98 uint8_t *s5req = alloca(len);
107 s5req[i++] = strlen(proxyuser);
108 memcpy(s5req + i, proxyuser, strlen(proxyuser));
109 i += strlen(proxyuser);
110 s5req[i++] = strlen(proxypass);
111 memcpy(s5req + i, proxypass, strlen(proxypass));
112 i += strlen(proxypass);
122 if(c->address.sa.sa_family == AF_INET) {
124 memcpy(s5req + i, &c->address.in.sin_addr, 4);
126 memcpy(s5req + i, &c->address.in.sin_port, 2);
129 } else if(c->address.sa.sa_family == AF_INET6) {
131 memcpy(s5req + i, &c->address.in6.sin6_addr, 16);
133 memcpy(s5req + i, &c->address.in6.sin6_port, 2);
137 logger(DEBUG_ALWAYS, LOG_ERR, "Address family %x not supported for SOCKS 5 proxies!", c->address.sa.sa_family);
145 return send_meta(c, s5req, len);
149 logger(DEBUG_ALWAYS, LOG_ERR, "Proxy type not implemented yet");
156 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type");
161 bool send_id(connection_t *c) {
162 gettimeofday(&c->start, NULL);
167 if(c->outgoing && !read_ecdsa_public_key(&c->ecdsa, &c->config_tree, c->name)) {
170 minor = myself->connection->protocol_minor;
174 if(proxytype && c->outgoing)
175 if(!send_proxyrequest(c)) {
179 return send_request(c, "%d %s %d.%d", ID, myself->connection->name, myself->connection->protocol_major, minor);
182 static bool finalize_invitation(connection_t *c, const char *data, uint16_t len) {
185 if(strchr(data, '\n')) {
186 logger(DEBUG_ALWAYS, LOG_ERR, "Received invalid key from invited node %s (%s)!\n", c->name, c->hostname);
190 // Create a new host config file
191 char filename[PATH_MAX];
192 snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
194 if(!access(filename, F_OK)) {
195 logger(DEBUG_ALWAYS, LOG_ERR, "Host config file for %s (%s) already exists!\n", c->name, c->hostname);
199 FILE *f = fopen(filename, "w");
202 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to create %s: %s\n", filename, strerror(errno));
206 fprintf(f, "Ed25519PublicKey = %s\n", data);
209 logger(DEBUG_CONNECTIONS, LOG_INFO, "Key successfully received from %s (%s)", c->name, c->hostname);
211 // Call invitation-accepted script
213 char *address, *port;
215 environment_init(&env);
216 environment_add(&env, "NODE=%s", c->name);
217 sockaddr2str(&c->address, &address, &port);
218 environment_add(&env, "REMOTEADDRESS=%s", address);
219 environment_add(&env, "NAME=%s", myself->name);
224 execute_script("invitation-accepted", &env);
226 environment_exit(&env);
228 sptps_send_record(&c->sptps, 2, data, 0);
232 static bool receive_invitation_sptps(void *handle, uint8_t type, const void *data, uint16_t len) {
233 connection_t *c = handle;
239 if(type == 1 && c->status.invitation_used) {
240 return finalize_invitation(c, data, len);
243 if(type != 0 || len != 18 || c->status.invitation_used) {
247 // Recover the filename from the cookie and the key
248 char *fingerprint = ecdsa_get_base64_public_key(invitation_key);
249 const size_t hashbuflen = 18 + strlen(fingerprint);
250 char *hashbuf = alloca(hashbuflen);
252 memcpy(hashbuf, data, 18);
253 memcpy(hashbuf + 18, fingerprint, hashbuflen - 18);
254 sha512(hashbuf, hashbuflen, cookie);
255 b64encode_tinc_urlsafe(cookie, cookie, 18);
258 char filename[PATH_MAX], usedname[PATH_MAX];
259 snprintf(filename, sizeof(filename), "%s" SLASH "invitations" SLASH "%s", confbase, cookie);
260 snprintf(usedname, sizeof(usedname), "%s" SLASH "invitations" SLASH "%s.used", confbase, cookie);
262 // Atomically rename the invitation file
263 if(rename(filename, usedname)) {
264 if(errno == ENOENT) {
265 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s tried to use non-existing invitation %s\n", c->hostname, cookie);
267 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to rename invitation %s\n", cookie);
273 // Check the timestamp of the invitation
276 if(stat(usedname, &st)) {
277 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat %s", usedname);
281 if(st.st_mtime + invitation_lifetime < now.tv_sec) {
282 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s tried to use expired invitation %s", c->hostname, cookie);
286 // Open the renamed file
287 FILE *f = fopen(usedname, "r");
290 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to open invitation %s\n", cookie);
294 // Read the new node's Name from the file
297 if(!fgets(buf, sizeof(buf), f)) {
298 logger(DEBUG_ALWAYS, LOG_ERR, "Could not read invitation file %s\n", cookie);
302 size_t buflen = strlen(buf);
304 // Strip whitespace at the end
305 while(buflen && strchr(" \t\r\n", buf[buflen - 1])) {
309 // Split the first line into variable and value
310 len = strcspn(buf, " \t=");
311 char *name = buf + len;
312 name += strspn(name, " \t");
316 name += strspn(name, " \t");
321 // Check that it is a valid Name
322 if(!*buf || !*name || strcasecmp(buf, "Name") || !check_id(name) || !strcmp(name, myself->name)) {
323 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid invitation file %s\n", cookie);
329 c->name = xstrdup(name);
331 // Send the node the contents of the invitation file
335 while((result = fread(buf, 1, sizeof(buf), f))) {
336 sptps_send_record(&c->sptps, 0, buf, result);
339 sptps_send_record(&c->sptps, 1, buf, 0);
343 c->status.invitation_used = true;
345 logger(DEBUG_CONNECTIONS, LOG_INFO, "Invitation %s successfully sent to %s (%s)", cookie, c->name, c->hostname);
349 bool id_h(connection_t *c, const char *request) {
350 char name[MAX_STRING_SIZE];
352 if(sscanf(request, "%*d " MAX_STRING " %2d.%3d", name, &c->protocol_major, &c->protocol_minor) < 2) {
353 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ID", c->name,
358 /* Check if this is a control connection */
360 if(name[0] == '^' && !strcmp(name + 1, controlcookie)) {
361 c->status.control = true;
362 c->allow_request = CONTROL;
363 c->last_ping_time = now.tv_sec + 3600;
366 c->name = xstrdup("<control>");
372 return send_request(c, "%d %d %d", ACK, TINC_CTL_VERSION_CURRENT, getpid());
376 if(!invitation_key) {
377 logger(DEBUG_ALWAYS, LOG_ERR, "Got invitation from %s but we don't have an invitation key", c->hostname);
381 c->ecdsa = ecdsa_set_base64_public_key(name + 1);
384 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad invitation from %s", c->hostname);
388 c->status.invitation = true;
389 char *mykey = ecdsa_get_base64_public_key(invitation_key);
399 if(!send_request(c, "%d %s", ACK, mykey)) {
405 c->protocol_minor = 2;
407 return sptps_start(&c->sptps, c, false, false, invitation_key, c->ecdsa, "tinc invitation", 15, send_meta_sptps, receive_invitation_sptps);
410 /* Check if identity is a valid name */
412 if(!check_id(name) || !strcmp(name, myself->name)) {
413 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ID", c->name,
414 c->hostname, "invalid name");
418 /* If this is an outgoing connection, make sure we are connected to the right host */
421 if(strcmp(c->name, name)) {
422 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s is %s instead of %s", c->hostname, name,
428 c->name = xstrdup(name);
431 /* Check if version matches */
433 if(c->protocol_major != myself->connection->protocol_major) {
434 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) uses incompatible version %d.%d",
435 c->name, c->hostname, c->protocol_major, c->protocol_minor);
439 if(bypass_security) {
440 if(!c->config_tree) {
441 c->config_tree = create_configuration();
444 c->allow_request = ACK;
454 c->protocol_minor = 0;
457 if(!c->config_tree) {
458 c->config_tree = create_configuration();
460 if(!read_host_config(c->config_tree, c->name, false)) {
461 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s had unknown identity (%s)", c->hostname, c->name);
466 read_ecdsa_public_key(&c->ecdsa, &c->config_tree, c->name);
469 /* Ignore failures if no key known yet */
472 if(c->protocol_minor && !ecdsa_active(c->ecdsa)) {
473 c->protocol_minor = 1;
476 /* Forbid version rollback for nodes whose Ed25519 key we know */
478 if(ecdsa_active(c->ecdsa) && c->protocol_minor < 1) {
479 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) tries to roll back protocol version to %d.%d",
480 c->name, c->hostname, c->protocol_major, c->protocol_minor);
484 c->allow_request = METAKEY;
490 if(c->protocol_minor >= 2) {
491 c->allow_request = ACK;
493 const size_t labellen = 25 + strlen(myself->name) + strlen(c->name);
494 char *label = alloca(labellen);
497 snprintf(label, labellen, "tinc TCP key expansion %s %s", myself->name, c->name);
499 snprintf(label, labellen, "tinc TCP key expansion %s %s", c->name, myself->name);
502 return sptps_start(&c->sptps, c, c->outgoing, false, myself->connection->ecdsa, c->ecdsa, label, labellen, send_meta_sptps, receive_meta_sptps);
504 return send_metakey(c);
508 #ifndef DISABLE_LEGACY
509 bool send_metakey(connection_t *c) {
510 if(!myself->connection->rsa) {
511 logger(DEBUG_CONNECTIONS, LOG_ERR, "Peer %s (%s) uses legacy protocol which we don't support", c->name, c->hostname);
515 if(!read_rsa_public_key(&c->rsa, c->config_tree, c->name)) {
519 /* We need to use a stream mode for the meta protocol. Use AES for this,
520 but try to match the key size with the one from the cipher selected
524 size_t keylen = cipher_keylength(myself->incipher);
525 const char *cipher_name;
528 cipher_name = "aes-128-cfb";
529 } else if(keylen <= 24) {
530 cipher_name = "aes-192-cfb";
532 cipher_name = "aes-256-cfb";
535 if(!cipher_open_by_name(&c->outcipher, cipher_name)) {
539 c->outbudget = cipher_budget(&c->outcipher);
541 if(!digest_open_by_name(&c->outdigest, "sha256", DIGEST_ALGO_SIZE)) {
542 cipher_close(&c->outcipher);
546 const size_t len = rsa_size(c->rsa);
547 char *key = alloca(len);
548 char *enckey = alloca(len);
549 char *hexkey = alloca(2 * len + 1);
551 /* Create a random key */
555 /* The message we send must be smaller than the modulus of the RSA key.
556 By definition, for a key of k bits, the following formula holds:
558 2^(k-1) <= modulus < 2^(k)
560 Where ^ means "to the power of", not "xor".
561 This means that to be sure, we must choose our message < 2^(k-1).
562 This can be done by setting the most significant bit to zero.
567 if(!cipher_set_key_from_rsa(&c->outcipher, key, len, true)) {
571 if(debug_level >= DEBUG_SCARY_THINGS) {
572 bin2hex(key, hexkey, len);
573 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Generated random meta key (unencrypted): %s", hexkey);
576 /* Encrypt the random data
578 We do not use one of the PKCS padding schemes here.
579 This is allowed, because we encrypt a totally random string
580 with a length equal to that of the modulus of the RSA key.
583 if(!rsa_public_encrypt(c->rsa, key, len, enckey)) {
584 logger(DEBUG_ALWAYS, LOG_ERR, "Error during encryption of meta key for %s (%s)", c->name, c->hostname);
588 /* Convert the encrypted random data to a hexadecimal formatted string */
590 bin2hex(enckey, hexkey, len);
592 /* Send the meta key */
594 bool result = send_request(c, "%d %d %d %d %d %s", METAKEY,
595 cipher_get_nid(&c->outcipher),
596 digest_get_nid(&c->outdigest), c->outmaclength,
597 c->outcompression, hexkey);
599 c->status.encryptout = true;
603 bool metakey_h(connection_t *c, const char *request) {
604 if(!myself->connection->rsa) {
608 char hexkey[MAX_STRING_SIZE];
609 int cipher, digest, maclength, compression;
610 const size_t len = rsa_size(myself->connection->rsa);
611 char *enckey = alloca(len);
612 char *key = alloca(len);
614 if(sscanf(request, "%*d %d %d %d %d " MAX_STRING, &cipher, &digest, &maclength, &compression, hexkey) != 5) {
615 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "METAKEY", c->name, c->hostname);
619 /* Convert the challenge from hexadecimal back to binary */
621 size_t inlen = hex2bin(hexkey, enckey, len);
623 /* Check if the length of the meta key is all right */
626 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong keylength");
630 /* Decrypt the meta key */
632 if(!rsa_private_decrypt(myself->connection->rsa, enckey, len, key)) {
633 logger(DEBUG_ALWAYS, LOG_ERR, "Error during decryption of meta key for %s (%s)", c->name, c->hostname);
637 if(debug_level >= DEBUG_SCARY_THINGS) {
638 bin2hex(key, hexkey, len);
639 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Received random meta key (unencrypted): %s", hexkey);
642 /* Check and lookup cipher and digest algorithms */
645 if(!cipher_open_by_nid(&c->incipher, cipher) || !cipher_set_key_from_rsa(&c->incipher, key, len, false)) {
646 logger(DEBUG_ALWAYS, LOG_ERR, "Error during initialisation of cipher from %s (%s)", c->name, c->hostname);
650 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "null cipher");
654 c->inbudget = cipher_budget(&c->incipher);
657 if(!digest_open_by_nid(&c->indigest, digest, DIGEST_ALGO_SIZE)) {
658 logger(DEBUG_ALWAYS, LOG_ERR, "Error during initialisation of digest from %s (%s)", c->name, c->hostname);
662 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "null digest");
666 c->status.decryptin = true;
668 c->allow_request = CHALLENGE;
670 return send_challenge(c);
673 bool send_challenge(connection_t *c) {
674 const size_t len = rsa_size(c->rsa);
675 char *buffer = alloca(len * 2 + 1);
677 c->hischallenge = xrealloc(c->hischallenge, len);
679 /* Copy random data to the buffer */
681 randomize(c->hischallenge, len);
685 bin2hex(c->hischallenge, buffer, len);
687 /* Send the challenge */
689 return send_request(c, "%d %s", CHALLENGE, buffer);
692 bool challenge_h(connection_t *c, const char *request) {
693 if(!myself->connection->rsa) {
697 char buffer[MAX_STRING_SIZE];
698 const size_t len = rsa_size(myself->connection->rsa);
700 if(sscanf(request, "%*d " MAX_STRING, buffer) != 1) {
701 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHALLENGE", c->name, c->hostname);
705 /* Check if the length of the challenge is all right */
707 if(strlen(buffer) != (size_t)len * 2) {
708 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge length");
712 c->mychallenge = xrealloc(c->mychallenge, len);
714 /* Convert the challenge from hexadecimal back to binary */
716 hex2bin(buffer, c->mychallenge, len);
718 /* The rest is done by send_chal_reply() */
720 c->allow_request = CHAL_REPLY;
723 return send_chal_reply(c);
729 bool send_chal_reply(connection_t *c) {
730 const size_t len = rsa_size(myself->connection->rsa);
731 size_t digestlen = digest_length(&c->indigest);
732 char *digest = alloca(digestlen * 2 + 1);
734 /* Calculate the hash from the challenge we received */
736 if(!digest_create(&c->indigest, c->mychallenge, len, digest)) {
740 free(c->mychallenge);
741 c->mychallenge = NULL;
743 /* Convert the hash to a hexadecimal formatted string */
745 bin2hex(digest, digest, digestlen);
749 return send_request(c, "%d %s", CHAL_REPLY, digest);
752 bool chal_reply_h(connection_t *c, const char *request) {
753 char hishash[MAX_STRING_SIZE];
755 if(sscanf(request, "%*d " MAX_STRING, hishash) != 1) {
756 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHAL_REPLY", c->name,
761 /* Convert the hash to binary format */
763 size_t inlen = hex2bin(hishash, hishash, sizeof(hishash));
765 /* Check if the length of the hash is all right */
767 if(inlen != digest_length(&c->outdigest)) {
768 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply length");
773 /* Verify the hash */
775 if(!digest_verify(&c->outdigest, c->hischallenge, rsa_size(c->rsa), hishash)) {
776 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply");
780 /* Identity has now been positively verified.
781 Send an acknowledgement with the rest of the information needed.
784 free(c->hischallenge);
785 c->hischallenge = NULL;
786 c->allow_request = ACK;
795 static bool send_upgrade(connection_t *c) {
796 /* Special case when protocol_minor is 1: the other end is Ed25519 capable,
797 * but doesn't know our key yet. So send it now. */
799 char *pubkey = ecdsa_get_base64_public_key(myself->connection->ecdsa);
805 bool result = send_request(c, "%d %s", ACK, pubkey);
810 bool send_metakey(connection_t *c) {
815 bool metakey_h(connection_t *c, const char *request) {
821 bool send_challenge(connection_t *c) {
826 bool challenge_h(connection_t *c, const char *request) {
832 bool send_chal_reply(connection_t *c) {
837 bool chal_reply_h(connection_t *c, const char *request) {
843 static bool send_upgrade(connection_t *c) {
849 bool send_ack(connection_t *c) {
850 if(c->protocol_minor == 1) {
851 return send_upgrade(c);
854 /* ACK message contains rest of the information the other end needs
855 to create node_t and edge_t structures. */
860 /* Estimate weight */
862 gettimeofday(&now, NULL);
863 c->estimated_weight = (int)((now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000);
865 /* Check some options */
867 if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &choice) && choice) || myself->options & OPTION_INDIRECT) {
868 c->options |= OPTION_INDIRECT;
871 if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &choice) && choice) || myself->options & OPTION_TCPONLY) {
872 c->options |= OPTION_TCPONLY | OPTION_INDIRECT;
875 if(myself->options & OPTION_PMTU_DISCOVERY && !(c->options & OPTION_TCPONLY)) {
876 c->options |= OPTION_PMTU_DISCOVERY;
879 choice = myself->options & OPTION_CLAMP_MSS;
880 get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice);
883 c->options |= OPTION_CLAMP_MSS;
886 if(!get_config_int(lookup_config(c->config_tree, "Weight"), &c->estimated_weight)) {
887 get_config_int(lookup_config(&config_tree, "Weight"), &c->estimated_weight);
890 return send_request(c, "%d %s %d %x", ACK, myport, c->estimated_weight, (c->options & 0xffffff) | (experimental ? (PROT_MINOR << 24) : 0));
893 static void send_everything(connection_t *c) {
894 /* Send all known subnets and edges */
896 if(disablebuggypeers) {
899 char pad[MAXBUFSIZE - MAXSIZE];
902 memset(&zeropkt, 0, sizeof(zeropkt));
903 zeropkt.pkt.len = MAXBUFSIZE;
904 send_tcppacket(c, &zeropkt.pkt);
908 for splay_each(subnet_t, s, &myself->subnet_tree) {
909 send_add_subnet(c, s);
915 for splay_each(node_t, n, &node_tree) {
916 for splay_each(subnet_t, s, &n->subnet_tree) {
917 send_add_subnet(c, s);
920 for splay_each(edge_t, e, &n->edge_tree) {
926 static bool upgrade_h(connection_t *c, const char *request) {
927 char pubkey[MAX_STRING_SIZE];
929 if(sscanf(request, "%*d " MAX_STRING, pubkey) != 1) {
930 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name, c->hostname);
934 if(ecdsa_active(c->ecdsa) || read_ecdsa_public_key(&c->ecdsa, &c->config_tree, c->name)) {
935 char *knownkey = ecdsa_get_base64_public_key(c->ecdsa);
936 bool different = strcmp(knownkey, pubkey);
940 logger(DEBUG_ALWAYS, LOG_ERR, "Already have an Ed25519 public key from %s (%s) which is different from the one presented now!", c->name, c->hostname);
944 logger(DEBUG_ALWAYS, LOG_INFO, "Already have Ed25519 public key from %s (%s), ignoring.", c->name, c->hostname);
945 c->allow_request = TERMREQ;
946 return send_termreq(c);
949 c->ecdsa = ecdsa_set_base64_public_key(pubkey);
952 logger(DEBUG_ALWAYS, LOG_INFO, "Got bad Ed25519 public key from %s (%s), not upgrading.", c->name, c->hostname);
956 logger(DEBUG_ALWAYS, LOG_INFO, "Got Ed25519 public key from %s (%s), upgrading!", c->name, c->hostname);
957 append_config_file(c->name, "Ed25519PublicKey", pubkey);
958 c->allow_request = TERMREQ;
961 c->outgoing->timeout = 0;
964 return send_termreq(c);
967 bool ack_h(connection_t *c, const char *request) {
968 if(c->protocol_minor == 1) {
969 return upgrade_h(c, request);
972 char hisport[MAX_STRING_SIZE];
978 if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &weight, &options) != 3) {
979 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name,
984 /* Check if we already have a node_t for him */
986 n = lookup_node(c->name);
990 n->name = xstrdup(c->name);
994 /* Oh dear, we already have a connection to this node. */
995 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Established a second connection with %s (%s), closing old connection", n->connection->name, n->connection->hostname);
997 if(n->connection->outgoing) {
999 logger(DEBUG_ALWAYS, LOG_WARNING, "Two outgoing connections to the same node!");
1001 c->outgoing = n->connection->outgoing;
1004 n->connection->outgoing = NULL;
1007 terminate_connection(n->connection, false);
1008 /* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */
1016 if(!(c->options & options & OPTION_PMTU_DISCOVERY)) {
1017 c->options &= ~OPTION_PMTU_DISCOVERY;
1018 options &= ~OPTION_PMTU_DISCOVERY;
1021 c->options |= options;
1023 if(get_config_int(lookup_config(c->config_tree, "PMTU"), &mtu) && mtu < n->mtu) {
1027 if(get_config_int(lookup_config(&config_tree, "PMTU"), &mtu) && mtu < n->mtu) {
1031 if(get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice)) {
1033 c->options |= OPTION_CLAMP_MSS;
1035 c->options &= ~OPTION_CLAMP_MSS;
1039 /* Activate this connection */
1041 c->allow_request = ALL;
1043 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection with %s (%s) activated", c->name,
1046 /* Send him everything we know */
1050 /* Create an edge_t for this connection */
1052 c->edge = new_edge();
1053 c->edge->from = myself;
1055 sockaddrcpy(&c->edge->address, &c->address);
1056 sockaddr_setport(&c->edge->address, hisport);
1057 sockaddr_t local_sa;
1058 socklen_t local_salen = sizeof(local_sa);
1060 if(getsockname(c->socket, &local_sa.sa, &local_salen) < 0) {
1061 logger(DEBUG_ALWAYS, LOG_WARNING, "Could not get local socket address for connection with %s", c->name);
1063 sockaddr_setport(&local_sa, myport);
1064 c->edge->local_address = local_sa;
1067 c->edge->weight = (weight + c->estimated_weight) / 2;
1068 c->edge->connection = c;
1069 c->edge->options = c->options;
1073 /* Notify everyone of the new edge */
1076 send_add_edge(c, c->edge);
1078 send_add_edge(everyone, c->edge);
1081 /* Run MST and SSSP algorithms */