2 protocol.c -- handle the meta-protocol
3 Copyright (C) 1999-2001 Ivo Timmermans <itimmermans@bigfoot.com>,
4 2000,2001 Guus Sliepen <guus@sliepen.warande.net>
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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: protocol.c,v 1.28.4.121 2001/11/16 17:39:59 zarq Exp $
25 #include <sys/types.h>
30 #include <sys/socket.h>
41 #include <netinet/in.h>
43 #include <openssl/sha.h>
44 #include <openssl/rand.h>
45 #include <openssl/evp.h>
47 #ifndef HAVE_RAND_PSEUDO_BYTES
48 #define RAND_pseudo_bytes RAND_bytes
55 #include "connection.h"
64 int check_id(char *id)
68 for (i = 0; i < strlen(id); i++)
69 if(!isalnum(id[i]) && id[i] != '_')
75 /* Generic request routines - takes care of logging and error
78 int send_request(connection_t *c, const char *format, ...)
81 char buffer[MAXBUFSIZE];
85 /* Use vsnprintf instead of vasprintf: faster, no memory
86 fragmentation, cleanup is automatic, and there is a limit on the
87 input buffer anyway */
89 va_start(args, format);
90 len = vsnprintf(buffer, MAXBUFSIZE, format, args);
91 request = va_arg(args, int);
94 if(len < 0 || len > MAXBUFSIZE-1)
96 syslog(LOG_ERR, _("Output buffer overflow while sending %s to %s (%s)"), request_name[request], c->name, c->hostname);
100 if(debug_lvl >= DEBUG_PROTOCOL)
102 if(debug_lvl >= DEBUG_META)
103 syslog(LOG_DEBUG, _("Sending %s to %s (%s): %s"), request_name[request], c->name, c->hostname, buffer);
105 syslog(LOG_DEBUG, _("Sending %s to %s (%s)"), request_name[request], c->name, c->hostname);
108 buffer[len++] = '\n';
110 return send_meta(c, buffer, len);
113 int receive_request(connection_t *c)
117 if(sscanf(c->buffer, "%d", &request) == 1)
119 if((request < 0) || (request >= LAST) || (request_handlers[request] == NULL))
121 if(debug_lvl >= DEBUG_META)
122 syslog(LOG_DEBUG, _("Unknown request from %s (%s): %s"),
123 c->name, c->hostname, c->buffer);
125 syslog(LOG_ERR, _("Unknown request from %s (%s)"),
126 c->name, c->hostname);
132 if(debug_lvl >= DEBUG_PROTOCOL)
134 if(debug_lvl >= DEBUG_META)
135 syslog(LOG_DEBUG, _("Got %s from %s (%s): %s"),
136 request_name[request], c->name, c->hostname, c->buffer);
138 syslog(LOG_DEBUG, _("Got %s from %s (%s)"),
139 request_name[request], c->name, c->hostname);
143 if((c->allow_request != ALL) && (c->allow_request != request))
145 syslog(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name, c->hostname);
149 if(request_handlers[request](c))
150 /* Something went wrong. Probably scriptkiddies. Terminate. */
152 syslog(LOG_ERR, _("Error while processing %s from %s (%s)"),
153 request_name[request], c->name, c->hostname);
159 syslog(LOG_ERR, _("Bogus data received from %s (%s)"),
160 c->name, c->hostname);
167 /* The authentication protocol is described in detail in doc/SECURITY2,
168 the rest will be described in doc/PROTOCOL. */
170 int send_id(connection_t *c)
173 return send_request(c, "%d %s %d", ID, myself->connection->name, myself->connection->protocol_version);
176 int id_h(connection_t *c)
178 char name[MAX_STRING_SIZE];
181 if(sscanf(c->buffer, "%*d "MAX_STRING" %d", name, &c->protocol_version) != 2)
183 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ID", c->name, c->hostname);
187 /* Check if identity is a valid name */
191 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ID", c->name, c->hostname, "invalid name");
195 /* If we set c->name in advance, make sure we are connected to the right host */
199 if(strcmp(c->name, name))
201 syslog(LOG_ERR, _("Peer %s is %s instead of %s"), c->hostname, name, c->name);
206 c->name = xstrdup(name);
208 /* Check if version matches */
210 if(c->protocol_version != myself->connection->protocol_version)
212 syslog(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"),
213 c->name, c->hostname, c->protocol_version);
220 init_configuration(&c->config_tree);
221 c->allow_request = ACK;
227 init_configuration(&c->config_tree);
229 if((bla = read_connection_config(c)))
231 syslog(LOG_ERR, _("Peer %s had unknown identity (%s)"), c->hostname, c->name);
236 if(read_rsa_public_key(c))
241 c->allow_request = METAKEY;
243 return send_metakey(c);
246 int send_metakey(connection_t *c)
248 char buffer[MAX_STRING_SIZE];
251 len = RSA_size(c->rsa_key);
253 /* Allocate buffers for the meta key */
256 c->outkey = xmalloc(len);
259 c->outctx = xmalloc(sizeof(*c->outctx));
261 /* Copy random data to the buffer */
263 RAND_bytes(c->outkey, len);
265 /* The message we send must be smaller than the modulus of the RSA key.
266 By definition, for a key of k bits, the following formula holds:
268 2^(k-1) <= modulus < 2^(k)
270 Where ^ means "to the power of", not "xor".
271 This means that to be sure, we must choose our message < 2^(k-1).
272 This can be done by setting the most significant bit to zero.
275 c->outkey[0] &= 0x7F;
277 if(debug_lvl >= DEBUG_SCARY_THINGS)
279 bin2hex(c->outkey, buffer, len);
280 buffer[len*2] = '\0';
281 syslog(LOG_DEBUG, _("Generated random meta key (unencrypted): %s"), buffer);
284 /* Encrypt the random data
286 We do not use one of the PKCS padding schemes here.
287 This is allowed, because we encrypt a totally random string
288 with a length equal to that of the modulus of the RSA key.
291 if(RSA_public_encrypt(len, c->outkey, buffer, c->rsa_key, RSA_NO_PADDING) != len)
293 syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
297 /* Convert the encrypted random data to a hexadecimal formatted string */
299 bin2hex(buffer, buffer, len);
300 buffer[len*2] = '\0';
302 /* Send the meta key */
304 x = send_request(c, "%d %s", METAKEY, buffer);
306 /* Further outgoing requests are encrypted with the key we just generated */
308 EVP_EncryptInit(c->outctx, EVP_bf_cfb(),
309 c->outkey + len - EVP_bf_cfb()->key_len,
310 c->outkey + len - EVP_bf_cfb()->key_len - EVP_bf_cfb()->iv_len);
312 c->status.encryptout = 1;
317 int metakey_h(connection_t *c)
319 char buffer[MAX_STRING_SIZE];
322 if(sscanf(c->buffer, "%*d "MAX_STRING, buffer) != 1)
324 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "METAKEY", c->name, c->hostname);
328 len = RSA_size(myself->connection->rsa_key);
330 /* Check if the length of the meta key is all right */
332 if(strlen(buffer) != len*2)
334 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong keylength");
338 /* Allocate buffers for the meta key */
341 c->inkey = xmalloc(len);
344 c->inctx = xmalloc(sizeof(*c->inctx));
346 /* Convert the challenge from hexadecimal back to binary */
348 hex2bin(buffer,buffer,len);
350 /* Decrypt the meta key */
352 if(RSA_private_decrypt(len, buffer, c->inkey, myself->connection->rsa_key, RSA_NO_PADDING) != len) /* See challenge() */
354 syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
358 if(debug_lvl >= DEBUG_SCARY_THINGS)
360 bin2hex(c->inkey, buffer, len);
361 buffer[len*2] = '\0';
362 syslog(LOG_DEBUG, _("Received random meta key (unencrypted): %s"), buffer);
365 /* All incoming requests will now be encrypted. */
367 EVP_DecryptInit(c->inctx, EVP_bf_cfb(),
368 c->inkey + len - EVP_bf_cfb()->key_len,
369 c->inkey + len - EVP_bf_cfb()->key_len - EVP_bf_cfb()->iv_len);
371 c->status.decryptin = 1;
373 c->allow_request = CHALLENGE;
375 return send_challenge(c);
378 int send_challenge(connection_t *c)
380 char buffer[MAX_STRING_SIZE];
383 /* CHECKME: what is most reasonable value for len? */
385 len = RSA_size(c->rsa_key);
387 /* Allocate buffers for the challenge */
390 c->hischallenge = xmalloc(len);
392 /* Copy random data to the buffer */
394 RAND_bytes(c->hischallenge, len);
399 bin2hex(c->hischallenge, buffer, len);
400 buffer[len*2] = '\0';
403 /* Send the challenge */
405 x = send_request(c, "%d %s", CHALLENGE, buffer);
410 int challenge_h(connection_t *c)
412 char buffer[MAX_STRING_SIZE];
415 if(sscanf(c->buffer, "%*d "MAX_STRING, buffer) != 1)
417 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "CHALLENGE", c->name, c->hostname);
421 len = RSA_size(myself->connection->rsa_key);
423 /* Check if the length of the challenge is all right */
425 if(strlen(buffer) != len*2)
427 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong challenge length");
431 /* Allocate buffers for the challenge */
434 c->mychallenge = xmalloc(len);
436 /* Convert the challenge from hexadecimal back to binary */
438 hex2bin(buffer,c->mychallenge,len);
440 c->allow_request = CHAL_REPLY;
442 /* Rest is done by send_chal_reply() */
444 return send_chal_reply(c);
447 int send_chal_reply(connection_t *c)
449 char hash[SHA_DIGEST_LENGTH*2+1];
451 /* Calculate the hash from the challenge we received */
453 SHA1(c->mychallenge, RSA_size(myself->connection->rsa_key), hash);
455 /* Convert the hash to a hexadecimal formatted string */
457 bin2hex(hash,hash,SHA_DIGEST_LENGTH);
458 hash[SHA_DIGEST_LENGTH*2] = '\0';
463 return send_request(c, "%d %s", CHAL_REPLY, hash);
466 int chal_reply_h(connection_t *c)
468 char hishash[MAX_STRING_SIZE];
469 char myhash[SHA_DIGEST_LENGTH];
471 if(sscanf(c->buffer, "%*d "MAX_STRING, hishash) != 1)
473 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "CHAL_REPLY", c->name, c->hostname);
477 /* Check if the length of the hash is all right */
479 if(strlen(hishash) != SHA_DIGEST_LENGTH*2)
481 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, _("wrong challenge reply length"));
485 /* Convert the hash to binary format */
487 hex2bin(hishash, hishash, SHA_DIGEST_LENGTH);
489 /* Calculate the hash from the challenge we sent */
491 SHA1(c->hischallenge, RSA_size(c->rsa_key), myhash);
493 /* Verify the incoming hash with the calculated hash */
495 if(memcmp(hishash, myhash, SHA_DIGEST_LENGTH))
497 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, _("wrong challenge reply"));
498 if(debug_lvl >= DEBUG_SCARY_THINGS)
500 bin2hex(myhash, hishash, SHA_DIGEST_LENGTH);
501 hishash[SHA_DIGEST_LENGTH*2] = '\0';
502 syslog(LOG_DEBUG, _("Expected challenge reply: %s"), hishash);
507 /* Identity has now been positively verified.
508 Send an acknowledgement with the rest of the information needed.
511 c->allow_request = ACK;
516 int send_ack(connection_t *c)
518 /* ACK message contains rest of the information the other end needs
519 to create node_t and edge_t structures. */
523 /* Estimate weight */
525 gettimeofday(&now, NULL);
526 c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
528 return send_request(c, "%d %hd %d", ACK, myself->port, c->estimated_weight);
531 int ack_h(connection_t *c)
538 avl_node_t *node, *node2;
540 if(sscanf(c->buffer, "%*d %hd %d", &port, &weight) != 2)
542 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ACK", c->name, c->hostname);
546 /* Check if we already have a node_t for him */
548 n = lookup_node(c->name);
553 n->name = xstrdup(c->name);
554 n->address = c->address;
555 n->hostname = xstrdup(c->hostname);
558 /* FIXME: Also check if no other tinc daemon uses the same IP and port for UDP traffic */
566 /* Oh dear, we already have a connection to this node. */
567 syslog(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"), n->name, n->hostname);
568 terminate_connection(n->connection, 0);
571 /* FIXME: check if information in existing node matches that of the other end of this connection */
577 /* Check some options
579 if((cfg = get_config_val(c->config, config_indirectdata)))
581 if(cfg->data.val == stupid_true)
582 c->options |= OPTION_INDIRECT;
585 if((cfg = get_config_val(c->config, config_tcponly)))
587 if(cfg->data.val == stupid_true)
588 c->options |= OPTION_TCPONLY;
591 if((myself->options | c->options) & OPTION_INDIRECT)
598 /* Create an edge_t for this connection */
600 c->edge = new_edge();
602 c->edge->from = myself;
604 c->edge->weight = (weight + c->estimated_weight) / 2;
605 c->edge->connection = c;
609 /* Activate this connection */
611 c->allow_request = ALL;
612 c->status.active = 1;
613 c->node->cipher = EVP_bf_cbc();
614 c->node->keylength = c->node->cipher->key_len + c->node->cipher->iv_len;
616 if(debug_lvl >= DEBUG_CONNECTIONS)
617 syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name, c->hostname);
620 /* Send him our subnets */
622 for(node = myself->subnet_tree->head; node; node = node->next)
624 s = (subnet_t *)node->data;
625 send_add_subnet(c, s);
628 /* And send him all known nodes and their subnets */
630 for(node = node_tree->head; node; node = node->next)
632 n = (node_t *)node->data;
634 if(n == c->node || n == myself)
639 for(node2 = c->node->subnet_tree->head; node2; node2 = node2->next)
641 s = (subnet_t *)node2->data;
642 send_add_subnet(c, s);
646 /* Send all known edges */
648 for(node = edge_tree->head; node; node = node->next)
650 e = (edge_t *)node->data;
658 /* Notify others of this connection */
660 for(node = connection_tree->head; node; node = node->next)
662 other = (connection_t *)node->data;
664 if(other->status.active && other != c)
666 send_add_node(other, c->node);
667 send_add_edge(other, c->edge);
671 /* Run MST and SSSP algorithms */
681 /* Address and subnet information exchange */
683 int send_add_subnet(connection_t *c, subnet_t *subnet)
688 x = send_request(c, "%d %s %s", ADD_SUBNET,
689 subnet->owner->name, netstr = net2str(subnet));
695 int add_subnet_h(connection_t *c)
697 char subnetstr[MAX_STRING_SIZE];
698 char name[MAX_STRING_SIZE];
704 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, name, subnetstr) != 2)
706 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_SUBNET", c->name, c->hostname);
710 /* Check if owner name is a valid */
714 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name, c->hostname, _("invalid name"));
718 /* Check if subnet string is valid */
720 if(!(s = str2net(subnetstr)))
722 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name, c->hostname, _("invalid subnet string"));
726 /* Check if the owner of the new subnet is in the connection list */
728 owner = lookup_node(name);
732 syslog(LOG_ERR, _("Got ADD_SUBNET from %s (%s) for %s which is not in our connection list"),
733 c->name, c->hostname, name);
737 /* Check if we already know this subnet */
739 if(lookup_subnet(owner, s))
745 /* If everything is correct, add the subnet to the list of the owner */
747 subnet_add(owner, s);
751 for(node = connection_tree->head; node; node = node->next)
753 other = (connection_t *)node->data;
754 if(other->status.active && other != c)
755 send_add_subnet(other, s);
761 int send_del_subnet(connection_t *c, subnet_t *s)
766 x = send_request(c, "%d %s %s", DEL_SUBNET, s->owner->name, netstr = net2str(s));
772 int del_subnet_h(connection_t *c)
774 char subnetstr[MAX_STRING_SIZE];
775 char name[MAX_STRING_SIZE];
781 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, name, subnetstr) != 3)
783 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_SUBNET", c->name, c->hostname);
787 /* Check if owner name is a valid */
791 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name, c->hostname, _("invalid name"));
795 /* Check if the owner of the new subnet is in the connection list */
797 if(!(owner = lookup_node(name)))
799 syslog(LOG_ERR, _("Got %s from %s (%s) for %s which is not in our connection list"),
800 "DEL_SUBNET", c->name, c->hostname, name);
804 /* Check if subnet string is valid */
806 if(!(s = str2net(subnetstr)))
808 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name, c->hostname, _("invalid subnet string"));
812 /* If everything is correct, delete the subnet from the list of the owner */
814 find = lookup_subnet(owner, s);
820 syslog(LOG_ERR, _("Got %s from %s (%s) for %s which does not appear in his subnet tree"),
821 "DEL_SUBNET", c->name, c->hostname, name);
825 subnet_del(owner, find);
829 for(node = connection_tree->head; node; node = node->next)
831 other = (connection_t *)node->data;
832 if(other->status.active && other != c)
833 send_del_subnet(other, s);
839 /* New and closed connections notification */
841 int send_add_node(connection_t *c, node_t *n)
844 return send_request(c, "%d %s %lx:%d", ADD_NODE,
845 n->name, n->address, n->port);
848 int add_node_h(connection_t *c)
852 char name[MAX_STRING_SIZE];
857 if(sscanf(c->buffer, "%*d "MAX_STRING" %lx:%hd", name, &address, &port) != 3)
859 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_NODE", c->name, c->hostname);
863 /* Check if identity is a valid name */
867 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_NODE", c->name, c->hostname, _("invalid name"));
871 /* Check if node already exists */
873 n = lookup_node(name);
877 /* Check if it matches */
879 if(n->address != address || n->port != port)
880 syslog(LOG_DEBUG, _("Got %s from %s (%s) for %s which does not match existing entry"), "ADD_NODE", c->name, c->hostname, n->name);
887 n->name = xstrdup(name);
888 n->address = address;
893 /* Tell the rest about the new node */
895 for(node = connection_tree->head; node; node = node->next)
897 other = (connection_t *)node->data;
898 if(other->status.active && other !=c)
899 send_add_node(other, n);
906 int send_del_node(connection_t *c, node_t *n)
909 return send_request(c, "%d %s %lx:%d", DEL_NODE,
910 n->name, n->address, n->port);
913 int del_node_h(connection_t *c)
916 char name[MAX_STRING_SIZE];
922 if(sscanf(c->buffer, "%*d "MAX_STRING" %lx:%hd", name, &address, &port) != 3)
924 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_NODE",
925 c->name, c->hostname);
929 /* Check if identity is a valid name */
933 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_NODE", c->name, c->hostname, _("invalid name"));
937 /* Check if somebody tries to delete ourself */
939 if(!strcmp(name, myself->name))
941 syslog(LOG_ERR, _("Got %s from %s (%s) for ourself!"), "DEL_NODE",
942 c->name, c->hostname);
946 /* Check if the deleted host exists */
948 n = lookup_node(name);
952 syslog(LOG_WARNING, _("Got %s from %s (%s) for %s which does not exist"), "DEL_NODE", c->name, c->hostname, n->name);
956 /* Check if the rest matches */
958 if(address != n->address || port != n->port)
960 syslog(LOG_WARNING, _("Got %s from %s (%s) for %s which does not match existing entry"), "DEL_NODE", c->name, c->hostname, n->name);
963 /* Tell the rest about the deleted node */
965 for(node = connection_tree->head; node; node = node->next)
967 other = (connection_t *)node->data;
968 if(other->status.active && other != c)
969 send_del_node(other, n);
972 /* Delete the node */
984 int send_add_edge(connection_t *c, edge_t *e)
987 return send_request(c, "%d %s %s %lx %d", ADD_EDGE,
988 e->from->name, e->to->name, e->options, e->weight);
991 int add_edge_h(connection_t *c)
996 char from_name[MAX_STRING_SIZE];
997 char to_name[MAX_STRING_SIZE];
1002 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" %lx %d", from_name, to_name, &options, &weight) != 4)
1004 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name, c->hostname);
1008 /* Check if names are valid */
1010 if(check_id(from_name))
1012 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
1016 if(check_id(to_name))
1018 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
1024 from = lookup_node(from_name);
1028 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("unknown node"));
1032 to = lookup_node(to_name);
1036 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("unknown node"));
1040 /* Check if edge already exists */
1042 e = lookup_edge(from, to);
1046 if(e->weight != weight || e->options != options)
1048 syslog(LOG_ERR, _("Got %s from %s (%s) which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
1059 e->options = options;
1064 /* Tell the rest about the new edge */
1066 for(node = connection_tree->head; node; node = node->next)
1068 other = (connection_t *)node->data;
1069 if(other->status.active && other != c)
1070 send_add_edge(other, e);
1073 /* Run MST before or after we tell the rest? */
1081 int send_del_edge(connection_t *c, edge_t *e)
1084 return send_request(c, "%d %s %s %lx %d", DEL_EDGE,
1085 e->from->name, e->to->name, e->options, e->weight);
1088 int del_edge_h(connection_t *c)
1091 char from_name[MAX_STRING_SIZE];
1092 char to_name[MAX_STRING_SIZE];
1096 connection_t *other;
1099 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" %lx %d", from_name, to_name, &options, &weight) != 4)
1101 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE",
1102 c->name, c->hostname);
1106 /* Check if names are valid */
1108 if(check_id(from_name))
1110 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
1114 if(check_id(to_name))
1116 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
1122 from = lookup_node(from_name);
1126 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("unknown node"));
1130 to = lookup_node(to_name);
1134 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("unknown node"));
1138 /* Check if edge exists */
1140 e = lookup_edge(from, to);
1144 if(e->weight != weight || e->options != options)
1146 syslog(LOG_ERR, _("Got %s from %s (%s) which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
1152 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("unknown edge"));
1156 /* Tell the rest about the deleted edge */
1158 for(node = connection_tree->head; node; node = node->next)
1160 other = (connection_t *)node->data;
1161 if(other->status.active && other != c)
1162 send_del_edge(other, e);
1165 /* Delete the edge */
1169 /* Run MST before or after we tell the rest? */
1178 /* Status and error notification routines */
1180 int send_status(connection_t *c, int statusno, char *statusstring)
1184 statusstring = status_text[statusno];
1186 return send_request(c, "%d %d %s", STATUS, statusno, statusstring);
1189 int status_h(connection_t *c)
1192 char statusstring[MAX_STRING_SIZE];
1194 if(sscanf(c->buffer, "%*d %d "MAX_STRING, &statusno, statusstring) != 2)
1196 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "STATUS",
1197 c->name, c->hostname);
1201 if(debug_lvl >= DEBUG_STATUS)
1203 syslog(LOG_NOTICE, _("Status message from %s (%s): %s: %s"),
1204 c->name, c->hostname, status_text[statusno], statusstring);
1211 int send_error(connection_t *c, int err, char *errstring)
1215 errstring = strerror(err);
1216 return send_request(c, "%d %d %s", ERROR, err, errstring);
1219 int error_h(connection_t *c)
1222 char errorstring[MAX_STRING_SIZE];
1224 if(sscanf(c->buffer, "%*d %d "MAX_STRING, &err, errorstring) != 2)
1226 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ERROR",
1227 c->name, c->hostname);
1231 if(debug_lvl >= DEBUG_ERROR)
1233 syslog(LOG_NOTICE, _("Error message from %s (%s): %s: %s"),
1234 c->name, c->hostname, strerror(err), errorstring);
1237 terminate_connection(c, c->status.active);
1242 int send_termreq(connection_t *c)
1245 return send_request(c, "%d", TERMREQ);
1248 int termreq_h(connection_t *c)
1251 terminate_connection(c, c->status.active);
1256 int send_ping(connection_t *c)
1258 char salt[SALTLEN*2+1];
1260 c->status.pinged = 1;
1261 c->last_ping_time = time(NULL);
1262 RAND_pseudo_bytes(salt, SALTLEN);
1263 bin2hex(salt, salt, SALTLEN);
1264 salt[SALTLEN*2] = '\0';
1266 return send_request(c, "%d %s", PING, salt);
1269 int ping_h(connection_t *c)
1272 return send_pong(c);
1275 int send_pong(connection_t *c)
1277 char salt[SALTLEN*2+1];
1279 RAND_pseudo_bytes(salt, SALTLEN);
1280 bin2hex(salt, salt, SALTLEN);
1281 salt[SALTLEN*2] = '\0';
1283 return send_request(c, "%d %s", PONG, salt);
1286 int pong_h(connection_t *c)
1289 c->status.pinged = 0;
1296 int send_key_changed(connection_t *c, node_t *n)
1298 connection_t *other;
1301 /* Only send this message if some other daemon requested our key previously.
1302 This reduces unnecessary key_changed broadcasts.
1305 if(n == myself && !mykeyused)
1308 for(node = connection_tree->head; node; node = node->next)
1310 other = (connection_t *)node->data;
1311 if(other->status.active && other != c)
1312 send_request(other, "%d %s", KEY_CHANGED, n->name);
1318 int key_changed_h(connection_t *c)
1320 char name[MAX_STRING_SIZE];
1323 if(sscanf(c->buffer, "%*d "MAX_STRING, name) != 1)
1325 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
1326 c->name, c->hostname);
1330 n = lookup_node(name);
1334 syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"), "KEY_CHANGED",
1335 c->name, c->hostname, name);
1339 n->status.validkey = 0;
1340 n->status.waitingforkey = 0;
1342 send_key_changed(c, n);
1347 int send_req_key(connection_t *c, node_t *from, node_t *to)
1350 return send_request(c, "%d %s %s", REQ_KEY,
1351 from->name, to->name);
1354 int req_key_h(connection_t *c)
1356 char from_name[MAX_STRING_SIZE];
1357 char to_name[MAX_STRING_SIZE];
1359 char key[MAX_STRING_SIZE];
1361 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, from_name, to_name) != 2)
1363 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY",
1364 c->name, c->hostname);
1368 from = lookup_node(from_name);
1372 syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "REQ_KEY",
1373 c->name, c->hostname, from_name);
1377 to = lookup_node(to_name);
1381 syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "REQ_KEY",
1382 c->name, c->hostname, to_name);
1386 /* Check if this key request is for us */
1388 if(to == myself) /* Yes, send our own key back */
1390 bin2hex(myself->key, key, myself->keylength);
1391 key[myself->keylength * 2] = '\0';
1392 send_ans_key(c, myself, from, key);
1397 if(to->status.validkey) /* Proxy keys */
1399 bin2hex(to->key, key, to->keylength);
1400 key[to->keylength * 2] = '\0';
1401 send_ans_key(c, to, from, key);
1404 send_req_key(to->nexthop->connection, from, to);
1411 int send_ans_key(connection_t *c, node_t *from, node_t *to, char *key)
1414 return send_request(c, "%d %s %s %s", ANS_KEY,
1415 from->name, to->name, key);
1418 int ans_key_h(connection_t *c)
1420 char from_name[MAX_STRING_SIZE];
1421 char to_name[MAX_STRING_SIZE];
1422 char key[MAX_STRING_SIZE];
1426 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING, from_name, to_name, key) != 3)
1428 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY",
1429 c->name, c->hostname);
1433 from = lookup_node(from_name);
1437 syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "ANS_KEY",
1438 c->name, c->hostname, from_name);
1442 to = lookup_node(to_name);
1446 syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "ANS_KEY",
1447 c->name, c->hostname, to_name);
1451 /* Check correctness of packet key */
1453 keylength = strlen(key);
1455 if(keylength != from->keylength * 2)
1457 syslog(LOG_ERR, _("Got bad %s from %s (%s) origin %s: %s"), "ANS_KEY",
1458 c->name, c->hostname, from->name, _("invalid key length"));
1462 /* Forward it if necessary */
1466 send_ans_key(to->nexthop->connection, from, to, key);
1469 /* Update our copy of the origin's packet key */
1474 from->key = xstrdup(key);
1476 hex2bin(from->key, from->key, keylength);
1477 from->key[keylength] = '\0';
1479 from->status.validkey = 1;
1480 from->status.waitingforkey = 0;
1487 int send_tcppacket(connection_t *c, vpn_packet_t *packet)
1493 x = send_request(c, "%d %hd", PACKET, packet->len);
1498 return send_meta(c, packet->data, packet->len);
1501 int tcppacket_h(connection_t *c)
1505 if(sscanf(c->buffer, "%*d %hd", &len) != 1)
1507 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "PACKET", c->name, c->hostname);
1511 /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */
1518 /* Jumptable for the request handlers */
1520 int (*request_handlers[])(connection_t*) = {
1521 id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
1522 status_h, error_h, termreq_h,
1524 add_node_h, del_node_h,
1525 add_subnet_h, del_subnet_h,
1526 add_edge_h, del_edge_h,
1527 key_changed_h, req_key_h, ans_key_h,
1533 char (*request_name[]) = {
1534 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
1535 "STATUS", "ERROR", "TERMREQ",
1537 "ADD_NODE", "DEL_NODE",
1538 "ADD_SUBNET", "DEL_SUBNET",
1539 "ADD_EDGE", "DEL_EDGE",
1540 "KEY_CHANGED", "REQ_KEY", "ANS_KEY",
1544 /* Status strings */
1546 char (*status_text[]) = {
1552 char (*error_text[]) = {