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"
45 #include "compression.h"
47 #include "ed25519/sha512.h"
50 /* If nonzero, use null ciphers and skip all key exchanges. */
51 bool bypass_security = false;
53 int invitation_lifetime;
54 ecdsa_t *invitation_key = NULL;
56 static bool send_proxyrequest(connection_t *c) {
62 sockaddr2str(&c->address, &host, &port);
63 send_request(c, "CONNECT %s:%s HTTP/1.1\r\n\r", host, port);
70 if(c->address.sa.sa_family != AF_INET) {
71 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot connect to an IPv6 host through a SOCKS 4 proxy!");
75 const size_t s4reqlen = 9 + (proxyuser ? strlen(proxyuser) : 0);
76 uint8_t *s4req = alloca(s4reqlen);
79 memcpy(s4req + 2, &c->address.in.sin_port, 2);
80 memcpy(s4req + 4, &c->address.in.sin_addr, 4);
83 memcpy(s4req + 8, proxyuser, strlen(proxyuser));
86 s4req[s4reqlen - 1] = 0;
88 return send_meta(c, s4req, s4reqlen);
92 size_t len = 3 + 6 + (c->address.sa.sa_family == AF_INET ? 4 : 16);
96 len += 3 + strlen(proxyuser) + strlen(proxypass);
99 uint8_t *s5req = alloca(len);
108 s5req[i++] = strlen(proxyuser);
109 memcpy(s5req + i, proxyuser, strlen(proxyuser));
110 i += strlen(proxyuser);
111 s5req[i++] = strlen(proxypass);
112 memcpy(s5req + i, proxypass, strlen(proxypass));
113 i += strlen(proxypass);
123 if(c->address.sa.sa_family == AF_INET) {
125 memcpy(s5req + i, &c->address.in.sin_addr, 4);
127 memcpy(s5req + i, &c->address.in.sin_port, 2);
130 } else if(c->address.sa.sa_family == AF_INET6) {
132 memcpy(s5req + i, &c->address.in6.sin6_addr, 16);
134 memcpy(s5req + i, &c->address.in6.sin6_port, 2);
138 logger(DEBUG_ALWAYS, LOG_ERR, "Address family %x not supported for SOCKS 5 proxies!", c->address.sa.sa_family);
146 return send_meta(c, s5req, len);
150 logger(DEBUG_ALWAYS, LOG_ERR, "Proxy type not implemented yet");
157 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type");
162 bool send_id(connection_t *c) {
163 gettimeofday(&c->start, NULL);
168 if(c->outgoing && !read_ecdsa_public_key(&c->ecdsa, &c->config_tree, c->name)) {
171 minor = myself->connection->protocol_minor;
175 if(proxytype && c->outgoing)
176 if(!send_proxyrequest(c)) {
180 return send_request(c, "%d %s %d.%d", ID, myself->connection->name, myself->connection->protocol_major, minor);
183 static bool finalize_invitation(connection_t *c, const char *data, uint16_t len) {
186 if(strchr(data, '\n')) {
187 logger(DEBUG_ALWAYS, LOG_ERR, "Received invalid key from invited node %s (%s)!\n", c->name, c->hostname);
191 // Create a new host config file
192 char filename[PATH_MAX];
193 snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
195 if(!access(filename, F_OK)) {
196 logger(DEBUG_ALWAYS, LOG_ERR, "Host config file for %s (%s) already exists!\n", c->name, c->hostname);
200 FILE *f = fopen(filename, "w");
203 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to create %s: %s\n", filename, strerror(errno));
207 fprintf(f, "Ed25519PublicKey = %s\n", data);
210 logger(DEBUG_CONNECTIONS, LOG_INFO, "Key successfully received from %s (%s)", c->name, c->hostname);
212 // Call invitation-accepted script
214 char *address, *port;
216 environment_init(&env);
217 environment_add(&env, "NODE=%s", c->name);
218 sockaddr2str(&c->address, &address, &port);
219 environment_add(&env, "REMOTEADDRESS=%s", address);
220 environment_add(&env, "NAME=%s", myself->name);
225 execute_script("invitation-accepted", &env);
227 environment_exit(&env);
229 sptps_send_record(&c->sptps, 2, data, 0);
233 static bool receive_invitation_sptps(void *handle, uint8_t type, const void *data, uint16_t len) {
234 connection_t *c = handle;
240 if(type == 1 && c->status.invitation_used) {
241 return finalize_invitation(c, data, len);
244 if(type != 0 || len != 18 || c->status.invitation_used) {
248 // Recover the filename from the cookie and the key
249 char *fingerprint = ecdsa_get_base64_public_key(invitation_key);
250 const size_t hashbuflen = 18 + strlen(fingerprint);
251 char *hashbuf = alloca(hashbuflen);
253 memcpy(hashbuf, data, 18);
254 memcpy(hashbuf + 18, fingerprint, hashbuflen - 18);
255 sha512(hashbuf, hashbuflen, cookie);
256 b64encode_tinc_urlsafe(cookie, cookie, 18);
259 char filename[PATH_MAX], usedname[PATH_MAX];
260 snprintf(filename, sizeof(filename), "%s" SLASH "invitations" SLASH "%s", confbase, cookie);
261 snprintf(usedname, sizeof(usedname), "%s" SLASH "invitations" SLASH "%s.used", confbase, cookie);
263 // Atomically rename the invitation file
264 if(rename(filename, usedname)) {
265 if(errno == ENOENT) {
266 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s tried to use non-existing invitation %s\n", c->hostname, cookie);
268 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to rename invitation %s\n", cookie);
274 // Check the timestamp of the invitation
277 if(stat(usedname, &st)) {
278 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat %s", usedname);
282 if(st.st_mtime + invitation_lifetime < now.tv_sec) {
283 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s tried to use expired invitation %s", c->hostname, cookie);
287 // Open the renamed file
288 FILE *f = fopen(usedname, "r");
291 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to open invitation %s\n", cookie);
295 // Read the new node's Name from the file
298 if(!fgets(buf, sizeof(buf), f)) {
299 logger(DEBUG_ALWAYS, LOG_ERR, "Could not read invitation file %s\n", cookie);
304 size_t buflen = strlen(buf);
306 // Strip whitespace at the end
307 while(buflen && strchr(" \t\r\n", buf[buflen - 1])) {
311 // Split the first line into variable and value
312 len = strcspn(buf, " \t=");
313 char *name = buf + len;
314 name += strspn(name, " \t");
318 name += strspn(name, " \t");
323 // Check that it is a valid Name
324 if(!*buf || !*name || strcasecmp(buf, "Name") || !check_id(name) || !strcmp(name, myself->name)) {
325 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid invitation file %s\n", cookie);
331 c->name = xstrdup(name);
333 // Send the node the contents of the invitation file
337 while((result = fread(buf, 1, sizeof(buf), f))) {
338 sptps_send_record(&c->sptps, 0, buf, result);
342 logger(DEBUG_ALWAYS, LOG_ERR, "Could not read invitation file %s\n", cookie);
347 sptps_send_record(&c->sptps, 1, buf, 0);
351 c->status.invitation_used = true;
353 logger(DEBUG_CONNECTIONS, LOG_INFO, "Invitation %s successfully sent to %s (%s)", cookie, c->name, c->hostname);
357 bool id_h(connection_t *c, const char *request) {
358 char name[MAX_STRING_SIZE];
360 if(sscanf(request, "%*d " MAX_STRING " %2d.%3d", name, &c->protocol_major, &c->protocol_minor) < 2) {
361 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ID", c->name,
366 /* Check if this is a control connection */
368 if(name[0] == '^' && !strcmp(name + 1, controlcookie)) {
369 c->status.control = true;
370 c->allow_request = CONTROL;
371 c->last_ping_time = now.tv_sec + 3600;
374 c->name = xstrdup("<control>");
380 return send_request(c, "%d %d %d", ACK, TINC_CTL_VERSION_CURRENT, getpid());
384 if(!invitation_key) {
385 logger(DEBUG_ALWAYS, LOG_ERR, "Got invitation from %s but we don't have an invitation key", c->hostname);
389 c->ecdsa = ecdsa_set_base64_public_key(name + 1);
392 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad invitation from %s", c->hostname);
396 c->status.invitation = true;
397 char *mykey = ecdsa_get_base64_public_key(invitation_key);
407 if(!send_request(c, "%d %s", ACK, mykey)) {
413 c->protocol_minor = 2;
415 return sptps_start(&c->sptps, c, false, false, invitation_key, c->ecdsa, "tinc invitation", 15, send_meta_sptps, receive_invitation_sptps);
418 /* Check if identity is a valid name */
420 if(!check_id(name) || !strcmp(name, myself->name)) {
421 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ID", c->name,
422 c->hostname, "invalid name");
426 /* If this is an outgoing connection, make sure we are connected to the right host */
429 if(strcmp(c->name, name)) {
430 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s is %s instead of %s", c->hostname, name,
436 c->name = xstrdup(name);
439 /* Check if version matches */
441 if(c->protocol_major != myself->connection->protocol_major) {
442 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) uses incompatible version %d.%d",
443 c->name, c->hostname, c->protocol_major, c->protocol_minor);
447 if(bypass_security) {
448 if(!c->config_tree) {
449 c->config_tree = create_configuration();
452 c->allow_request = ACK;
462 c->protocol_minor = 0;
465 if(!c->config_tree) {
466 c->config_tree = create_configuration();
468 if(!read_host_config(c->config_tree, c->name, false)) {
469 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s had unknown identity (%s)", c->hostname, c->name);
474 read_ecdsa_public_key(&c->ecdsa, &c->config_tree, c->name);
477 /* Ignore failures if no key known yet */
480 if(c->protocol_minor && !ecdsa_active(c->ecdsa)) {
481 c->protocol_minor = 1;
484 /* Forbid version rollback for nodes whose Ed25519 key we know */
486 if(ecdsa_active(c->ecdsa) && c->protocol_minor < 1) {
487 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) tries to roll back protocol version to %d.%d",
488 c->name, c->hostname, c->protocol_major, c->protocol_minor);
492 c->allow_request = METAKEY;
498 if(c->protocol_minor >= 2) {
499 c->allow_request = ACK;
501 const size_t labellen = 25 + strlen(myself->name) + strlen(c->name);
502 char *label = alloca(labellen);
505 snprintf(label, labellen, "tinc TCP key expansion %s %s", myself->name, c->name);
507 snprintf(label, labellen, "tinc TCP key expansion %s %s", c->name, myself->name);
510 return sptps_start(&c->sptps, c, c->outgoing, false, myself->connection->ecdsa, c->ecdsa, label, labellen, send_meta_sptps, receive_meta_sptps);
512 return send_metakey(c);
516 #ifndef DISABLE_LEGACY
517 static const char *get_cipher_name(cipher_t *cipher) {
518 size_t keylen = cipher_keylength(cipher);
521 return "aes-128-cfb";
522 } else if(keylen <= 24) {
523 return "aes-192-cfb";
525 return "aes-256-cfb";
529 bool send_metakey(connection_t *c) {
530 if(!myself->connection->legacy) {
531 logger(DEBUG_CONNECTIONS, LOG_ERR, "Peer %s (%s) uses legacy protocol which we don't support", c->name, c->hostname);
535 rsa_t *rsa = read_rsa_public_key(c->config_tree, c->name);
541 legacy_ctx_t *ctx = new_legacy_ctx(rsa);
543 /* We need to use a stream mode for the meta protocol. Use AES for this,
544 but try to match the key size with the one from the cipher selected
548 const char *cipher_name = get_cipher_name(myself->incipher);
550 if(!init_crypto_by_name(&ctx->out, cipher_name, "sha256")) {
551 logger(DEBUG_ALWAYS, LOG_ERR, "Error during initialisation of cipher or digest to %s (%s)", c->name, c->hostname);
552 free_legacy_ctx(ctx);
556 const size_t len = rsa_size(ctx->rsa);
557 const size_t hexkeylen = HEX_SIZE(len);
558 char *key = alloca(len);
559 char *enckey = alloca(len);
560 char *hexkey = alloca(hexkeylen);
562 /* Create a random key */
566 /* The message we send must be smaller than the modulus of the RSA key.
567 By definition, for a key of k bits, the following formula holds:
569 2^(k-1) <= modulus < 2^(k)
571 Where ^ means "to the power of", not "xor".
572 This means that to be sure, we must choose our message < 2^(k-1).
573 This can be done by setting the most significant bit to zero.
578 if(!cipher_set_key_from_rsa(&ctx->out.cipher, key, len, true)) {
579 free_legacy_ctx(ctx);
584 if(debug_level >= DEBUG_SCARY_THINGS) {
585 bin2hex(key, hexkey, len);
586 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Generated random meta key (unencrypted): %s", hexkey);
587 memzero(hexkey, hexkeylen);
590 /* Encrypt the random data
592 We do not use one of the PKCS padding schemes here.
593 This is allowed, because we encrypt a totally random string
594 with a length equal to that of the modulus of the RSA key.
597 bool encrypted = rsa_public_encrypt(ctx->rsa, key, len, enckey);
601 free_legacy_ctx(ctx);
602 logger(DEBUG_ALWAYS, LOG_ERR, "Error during encryption of meta key for %s (%s)", c->name, c->hostname);
606 free_legacy_ctx(c->legacy);
609 /* Convert the encrypted random data to a hexadecimal formatted string */
611 bin2hex(enckey, hexkey, len);
613 /* Send the meta key */
615 bool result = send_request(c, "%d %d %d %d %d %s", METAKEY,
616 cipher_get_nid(&c->legacy->out.cipher),
617 digest_get_nid(&c->legacy->out.digest), c->outmaclength,
618 COMPRESS_NONE, hexkey);
620 c->status.encryptout = true;
624 bool metakey_h(connection_t *c, const char *request) {
625 if(!myself->connection->legacy || !c->legacy) {
629 char hexkey[MAX_STRING_SIZE];
631 const size_t len = rsa_size(myself->connection->legacy->rsa);
632 char *enckey = alloca(len);
633 char *key = alloca(len);
635 if(sscanf(request, "%*d %d %d %*d %*d " MAX_STRING, &cipher, &digest, hexkey) != 3) {
636 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "METAKEY", c->name, c->hostname);
640 if(!cipher || !digest) {
641 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): cipher %d, digest %d", c->name, c->hostname, cipher, digest);
645 /* Convert the challenge from hexadecimal back to binary */
647 size_t inlen = hex2bin(hexkey, enckey, len);
649 /* Check if the length of the meta key is all right */
652 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong keylength");
656 /* Decrypt the meta key */
658 if(!rsa_private_decrypt(myself->connection->legacy->rsa, enckey, len, key)) {
659 logger(DEBUG_ALWAYS, LOG_ERR, "Error during decryption of meta key for %s (%s)", c->name, c->hostname);
663 if(debug_level >= DEBUG_SCARY_THINGS) {
664 bin2hex(key, hexkey, len);
665 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Received random meta key (unencrypted): %s", hexkey);
666 // Hopefully the user knew what he was doing leaking session keys into logs. We'll do the right thing here anyway.
667 memzero(hexkey, HEX_SIZE(len));
670 /* Check and lookup cipher and digest algorithms */
672 if(!init_crypto_by_nid(&c->legacy->in, cipher, digest)) {
674 logger(DEBUG_ALWAYS, LOG_ERR, "Error during initialisation of cipher or digest from %s (%s)", c->name, c->hostname);
678 bool key_set = cipher_set_key_from_rsa(&c->legacy->in.cipher, key, len, false);
682 logger(DEBUG_ALWAYS, LOG_ERR, "Error setting RSA key for %s (%s)", c->name, c->hostname);
686 c->status.decryptin = true;
688 c->allow_request = CHALLENGE;
690 return send_challenge(c);
693 bool send_challenge(connection_t *c) {
694 const size_t len = rsa_size(c->legacy->rsa);
695 char *buffer = alloca(len * 2 + 1);
697 c->hischallenge = xrealloc(c->hischallenge, len);
699 /* Copy random data to the buffer */
701 randomize(c->hischallenge, len);
705 bin2hex(c->hischallenge, buffer, len);
707 /* Send the challenge */
709 return send_request(c, "%d %s", CHALLENGE, buffer);
712 bool challenge_h(connection_t *c, const char *request) {
713 if(!myself->connection->legacy) {
717 char buffer[MAX_STRING_SIZE];
718 const size_t len = rsa_size(myself->connection->legacy->rsa);
720 if(sscanf(request, "%*d " MAX_STRING, buffer) != 1) {
721 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHALLENGE", c->name, c->hostname);
725 /* Check if the length of the challenge is all right */
727 if(strlen(buffer) != (size_t)len * 2) {
728 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge length");
732 c->mychallenge = xrealloc(c->mychallenge, len);
734 /* Convert the challenge from hexadecimal back to binary */
736 hex2bin(buffer, c->mychallenge, len);
738 /* The rest is done by send_chal_reply() */
740 c->allow_request = CHAL_REPLY;
743 return send_chal_reply(c);
749 bool send_chal_reply(connection_t *c) {
750 const size_t len = rsa_size(myself->connection->legacy->rsa);
751 size_t digestlen = digest_length(&c->legacy->in.digest);
752 char *digest = alloca(digestlen * 2 + 1);
754 /* Calculate the hash from the challenge we received */
756 if(!digest_create(&c->legacy->in.digest, c->mychallenge, len, digest)) {
760 free(c->mychallenge);
761 c->mychallenge = NULL;
763 /* Convert the hash to a hexadecimal formatted string */
765 bin2hex(digest, digest, digestlen);
769 return send_request(c, "%d %s", CHAL_REPLY, digest);
772 bool chal_reply_h(connection_t *c, const char *request) {
773 char hishash[MAX_STRING_SIZE];
775 if(sscanf(request, "%*d " MAX_STRING, hishash) != 1) {
776 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHAL_REPLY", c->name,
781 /* Convert the hash to binary format */
783 size_t inlen = hex2bin(hishash, hishash, sizeof(hishash));
785 /* Check if the length of the hash is all right */
787 if(inlen != digest_length(&c->legacy->out.digest)) {
788 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply length");
793 /* Verify the hash */
795 if(!digest_verify(&c->legacy->out.digest, c->hischallenge, rsa_size(c->legacy->rsa), hishash)) {
796 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply");
800 /* Identity has now been positively verified.
801 Send an acknowledgement with the rest of the information needed.
804 free(c->hischallenge);
805 c->hischallenge = NULL;
806 c->allow_request = ACK;
815 static bool send_upgrade(connection_t *c) {
816 /* Special case when protocol_minor is 1: the other end is Ed25519 capable,
817 * but doesn't know our key yet. So send it now. */
819 char *pubkey = ecdsa_get_base64_public_key(myself->connection->ecdsa);
825 bool result = send_request(c, "%d %s", ACK, pubkey);
830 bool send_metakey(connection_t *c) {
835 bool metakey_h(connection_t *c, const char *request) {
841 bool send_challenge(connection_t *c) {
846 bool challenge_h(connection_t *c, const char *request) {
852 bool send_chal_reply(connection_t *c) {
857 bool chal_reply_h(connection_t *c, const char *request) {
863 static bool send_upgrade(connection_t *c) {
869 bool send_ack(connection_t *c) {
870 if(c->protocol_minor == 1) {
871 return send_upgrade(c);
874 /* ACK message contains rest of the information the other end needs
875 to create node_t and edge_t structures. */
880 /* Estimate weight */
882 gettimeofday(&now, NULL);
883 c->estimated_weight = (int)((now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000);
885 /* Check some options */
887 if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &choice) && choice) || myself->options & OPTION_INDIRECT) {
888 c->options |= OPTION_INDIRECT;
891 if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &choice) && choice) || myself->options & OPTION_TCPONLY) {
892 c->options |= OPTION_TCPONLY | OPTION_INDIRECT;
895 if(myself->options & OPTION_PMTU_DISCOVERY && !(c->options & OPTION_TCPONLY)) {
896 c->options |= OPTION_PMTU_DISCOVERY;
899 choice = myself->options & OPTION_CLAMP_MSS;
900 get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice);
903 c->options |= OPTION_CLAMP_MSS;
906 if(!get_config_int(lookup_config(c->config_tree, "Weight"), &c->estimated_weight)) {
907 get_config_int(lookup_config(&config_tree, "Weight"), &c->estimated_weight);
910 return send_request(c, "%d %s %d %x", ACK, myport.udp, c->estimated_weight, (c->options & 0xffffff) | (experimental ? (PROT_MINOR << 24) : 0));
913 static void send_everything(connection_t *c) {
914 /* Send all known subnets and edges */
916 if(disablebuggypeers) {
919 char pad[MAXBUFSIZE - MAXSIZE];
922 memset(&zeropkt, 0, sizeof(zeropkt));
923 zeropkt.pkt.len = MAXBUFSIZE;
924 send_tcppacket(c, &zeropkt.pkt);
928 for splay_each(subnet_t, s, &myself->subnet_tree) {
929 send_add_subnet(c, s);
935 for splay_each(node_t, n, &node_tree) {
936 for splay_each(subnet_t, s, &n->subnet_tree) {
937 send_add_subnet(c, s);
940 for splay_each(edge_t, e, &n->edge_tree) {
946 static bool upgrade_h(connection_t *c, const char *request) {
947 char pubkey[MAX_STRING_SIZE];
949 if(sscanf(request, "%*d " MAX_STRING, pubkey) != 1) {
950 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name, c->hostname);
954 if(ecdsa_active(c->ecdsa) || read_ecdsa_public_key(&c->ecdsa, &c->config_tree, c->name)) {
955 char *knownkey = ecdsa_get_base64_public_key(c->ecdsa);
956 bool different = strcmp(knownkey, pubkey);
960 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);
964 logger(DEBUG_ALWAYS, LOG_INFO, "Already have Ed25519 public key from %s (%s), ignoring.", c->name, c->hostname);
965 c->allow_request = TERMREQ;
966 return send_termreq(c);
969 c->ecdsa = ecdsa_set_base64_public_key(pubkey);
972 logger(DEBUG_ALWAYS, LOG_INFO, "Got bad Ed25519 public key from %s (%s), not upgrading.", c->name, c->hostname);
976 logger(DEBUG_ALWAYS, LOG_INFO, "Got Ed25519 public key from %s (%s), upgrading!", c->name, c->hostname);
977 append_config_file(c->name, "Ed25519PublicKey", pubkey);
978 c->allow_request = TERMREQ;
981 c->outgoing->timeout = 0;
984 return send_termreq(c);
987 bool ack_h(connection_t *c, const char *request) {
988 if(c->protocol_minor == 1) {
989 return upgrade_h(c, request);
992 char hisport[MAX_STRING_SIZE];
998 if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &weight, &options) != 3) {
999 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name,
1004 /* Check if we already have a node_t for him */
1006 n = lookup_node(c->name);
1010 n->name = xstrdup(c->name);
1014 /* Oh dear, we already have a connection to this node. */
1015 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Established a second connection with %s (%s), closing old connection", n->connection->name, n->connection->hostname);
1017 if(n->connection->outgoing) {
1019 logger(DEBUG_ALWAYS, LOG_WARNING, "Two outgoing connections to the same node!");
1021 c->outgoing = n->connection->outgoing;
1024 n->connection->outgoing = NULL;
1027 terminate_connection(n->connection, false);
1028 /* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */
1036 if(!(c->options & options & OPTION_PMTU_DISCOVERY)) {
1037 c->options &= ~OPTION_PMTU_DISCOVERY;
1038 options &= ~OPTION_PMTU_DISCOVERY;
1041 c->options |= options;
1043 if(get_config_int(lookup_config(c->config_tree, "PMTU"), &mtu) && mtu < n->mtu) {
1047 if(get_config_int(lookup_config(&config_tree, "PMTU"), &mtu) && mtu < n->mtu) {
1051 if(get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice)) {
1053 c->options |= OPTION_CLAMP_MSS;
1055 c->options &= ~OPTION_CLAMP_MSS;
1059 /* Activate this connection */
1061 c->allow_request = ALL;
1063 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection with %s (%s) activated", c->name,
1066 /* Send him everything we know */
1070 /* Create an edge_t for this connection */
1072 c->edge = new_edge();
1073 c->edge->from = myself;
1075 sockaddrcpy(&c->edge->address, &c->address);
1076 sockaddr_setport(&c->edge->address, hisport);
1077 sockaddr_t local_sa;
1078 socklen_t local_salen = sizeof(local_sa);
1080 if(getsockname(c->socket, &local_sa.sa, &local_salen) < 0) {
1081 logger(DEBUG_ALWAYS, LOG_WARNING, "Could not get local socket address for connection with %s", c->name);
1083 sockaddr_setport(&local_sa, myport.udp);
1084 c->edge->local_address = local_sa;
1087 c->edge->weight = (weight + c->estimated_weight) / 2;
1088 c->edge->connection = c;
1089 c->edge->options = c->options;
1093 /* Notify everyone of the new edge */
1096 send_add_edge(c, c->edge);
1098 send_add_edge(everyone, c->edge);
1101 /* Run MST and SSSP algorithms */