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.114 2001/10/30 16:34:32 guus 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
56 #include "connection.h"
65 int check_id(char *id)
69 for (i = 0; i < strlen(id); i++)
70 if(!isalnum(id[i]) && id[i] != '_')
76 /* Generic request routines - takes care of logging and error
79 int send_request(connection_t *c, const char *format, ...)
82 char buffer[MAXBUFSIZE];
86 /* Use vsnprintf instead of vasprintf: faster, no memory
87 fragmentation, cleanup is automatic, and there is a limit on the
88 input buffer anyway */
90 va_start(args, format);
91 len = vsnprintf(buffer, MAXBUFSIZE, format, args);
92 request = va_arg(args, int);
95 if(len < 0 || len > MAXBUFSIZE-1)
97 syslog(LOG_ERR, _("Output buffer overflow while sending %s to %s (%s)"), request_name[request], c->name, c->hostname);
101 if(debug_lvl >= DEBUG_PROTOCOL)
103 if(debug_lvl >= DEBUG_META)
104 syslog(LOG_DEBUG, _("Sending %s to %s (%s): %s"), request_name[request], c->name, c->hostname, buffer);
106 syslog(LOG_DEBUG, _("Sending %s to %s (%s)"), request_name[request], c->name, c->hostname);
109 buffer[len++] = '\n';
111 return send_meta(c, buffer, len);
114 int receive_request(connection_t *c)
118 if(sscanf(c->buffer, "%d", &request) == 1)
120 if((request < 0) || (request >= LAST) || (request_handlers[request] == NULL))
122 if(debug_lvl >= DEBUG_META)
123 syslog(LOG_DEBUG, _("Unknown request from %s (%s): %s"),
124 c->name, c->hostname, c->buffer);
126 syslog(LOG_ERR, _("Unknown request from %s (%s)"),
127 c->name, c->hostname);
133 if(debug_lvl >= DEBUG_PROTOCOL)
135 if(debug_lvl >= DEBUG_META)
136 syslog(LOG_DEBUG, _("Got %s from %s (%s): %s"),
137 request_name[request], c->name, c->hostname, c->buffer);
139 syslog(LOG_DEBUG, _("Got %s from %s (%s)"),
140 request_name[request], c->name, c->hostname);
144 if((c->allow_request != ALL) && (c->allow_request != request))
146 syslog(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name, c->hostname);
150 if(request_handlers[request](c))
151 /* Something went wrong. Probably scriptkiddies. Terminate. */
153 syslog(LOG_ERR, _("Error while processing %s from %s (%s)"),
154 request_name[request], c->name, c->hostname);
160 syslog(LOG_ERR, _("Bogus data received from %s (%s)"),
161 c->name, c->hostname);
168 /* The authentication protocol is described in detail in doc/SECURITY2,
169 the rest will be described in doc/PROTOCOL. */
171 int send_id(connection_t *c)
174 return send_request(c, "%d %s %d", ID, myself->connection->name, myself->connection->protocol_version);
177 int id_h(connection_t *c)
179 char name[MAX_STRING_SIZE];
182 if(sscanf(c->buffer, "%*d "MAX_STRING" %d", name, &c->protocol_version) != 2)
184 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ID", c->name, c->hostname);
188 /* Check if identity is a valid name */
192 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ID", c->name, c->hostname, "invalid name");
196 /* If we set c->name in advance, make sure we are connected to the right host */
200 if(strcmp(c->name, name))
202 syslog(LOG_ERR, _("Peer %s is %s instead of %s"), c->hostname, name, c->name);
207 c->name = xstrdup(name);
209 /* Check if version matches */
211 if(c->protocol_version != myself->connection->protocol_version)
213 syslog(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"),
214 c->name, c->hostname, c->protocol_version);
221 init_configuration(&c->config_tree);
222 c->allow_request = ACK;
228 init_configuration(&c->config_tree);
230 if((bla = read_connection_config(c)))
232 syslog(LOG_ERR, _("Peer %s had unknown identity (%s)"), c->hostname, c->name);
236 if(read_rsa_public_key(c))
242 c->allow_request = METAKEY;
244 return send_metakey(c);
247 int send_metakey(connection_t *c)
252 len = RSA_size(c->rsa_key);
254 /* Allocate buffers for the meta key */
256 buffer = xmalloc(len*2+1);
259 c->outkey = xmalloc(len);
262 c->outctx = xmalloc(sizeof(*c->outctx));
264 /* Copy random data to the buffer */
266 RAND_bytes(c->outkey, len);
268 /* The message we send must be smaller than the modulus of the RSA key.
269 By definition, for a key of k bits, the following formula holds:
271 2^(k-1) <= modulus < 2^(k)
273 Where ^ means "to the power of", not "xor".
274 This means that to be sure, we must choose our message < 2^(k-1).
275 This can be done by setting the most significant bit to zero.
278 c->outkey[0] &= 0x7F;
280 if(debug_lvl >= DEBUG_SCARY_THINGS)
282 bin2hex(c->outkey, buffer, len);
283 buffer[len*2] = '\0';
284 syslog(LOG_DEBUG, _("Generated random meta key (unencrypted): %s"), buffer);
287 /* Encrypt the random data
289 We do not use one of the PKCS padding schemes here.
290 This is allowed, because we encrypt a totally random string
291 with a length equal to that of the modulus of the RSA key.
294 if(RSA_public_encrypt(len, c->outkey, buffer, c->rsa_key, RSA_NO_PADDING) != len)
296 syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
301 /* Convert the encrypted random data to a hexadecimal formatted string */
303 bin2hex(buffer, buffer, len);
304 buffer[len*2] = '\0';
306 /* Send the meta key */
308 x = send_request(c, "%d %s", METAKEY, buffer);
311 /* Further outgoing requests are encrypted with the key we just generated */
313 EVP_EncryptInit(c->outctx, EVP_bf_cfb(),
314 c->outkey + len - EVP_bf_cfb()->key_len,
315 c->outkey + len - EVP_bf_cfb()->key_len - EVP_bf_cfb()->iv_len);
317 c->status.encryptout = 1;
322 int metakey_h(connection_t *c)
324 char buffer[MAX_STRING_SIZE];
327 if(sscanf(c->buffer, "%*d "MAX_STRING, buffer) != 1)
329 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "METAKEY", c->name, c->hostname);
333 len = RSA_size(myself->connection->rsa_key);
335 /* Check if the length of the meta key is all right */
337 if(strlen(buffer) != len*2)
339 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong keylength");
343 /* Allocate buffers for the meta key */
346 c->inkey = xmalloc(len);
349 c->inctx = xmalloc(sizeof(*c->inctx));
351 /* Convert the challenge from hexadecimal back to binary */
353 hex2bin(buffer,buffer,len);
355 /* Decrypt the meta key */
357 if(RSA_private_decrypt(len, buffer, c->inkey, myself->connection->rsa_key, RSA_NO_PADDING) != len) /* See challenge() */
359 syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
363 if(debug_lvl >= DEBUG_SCARY_THINGS)
365 bin2hex(c->inkey, buffer, len);
366 buffer[len*2] = '\0';
367 syslog(LOG_DEBUG, _("Received random meta key (unencrypted): %s"), buffer);
370 /* All incoming requests will now be encrypted. */
372 EVP_DecryptInit(c->inctx, EVP_bf_cfb(),
373 c->inkey + len - EVP_bf_cfb()->key_len,
374 c->inkey + len - EVP_bf_cfb()->key_len - EVP_bf_cfb()->iv_len);
376 c->status.decryptin = 1;
378 c->allow_request = CHALLENGE;
380 return send_challenge(c);
383 int send_challenge(connection_t *c)
388 /* CHECKME: what is most reasonable value for len? */
390 len = RSA_size(c->rsa_key);
392 /* Allocate buffers for the challenge */
394 buffer = xmalloc(len*2+1);
397 free(c->hischallenge);
399 c->hischallenge = xmalloc(len);
401 /* Copy random data to the buffer */
403 RAND_bytes(c->hischallenge, len);
408 bin2hex(c->hischallenge, buffer, len);
409 buffer[len*2] = '\0';
412 /* Send the challenge */
414 x = send_request(c, "%d %s", CHALLENGE, buffer);
420 int challenge_h(connection_t *c)
422 char buffer[MAX_STRING_SIZE];
425 if(sscanf(c->buffer, "%*d "MAX_STRING, buffer) != 1)
427 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "CHALLENGE", c->name, c->hostname);
431 len = RSA_size(myself->connection->rsa_key);
433 /* Check if the length of the challenge is all right */
435 if(strlen(buffer) != len*2)
437 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong challenge length");
441 /* Allocate buffers for the challenge */
444 c->mychallenge = xmalloc(len);
446 /* Convert the challenge from hexadecimal back to binary */
448 hex2bin(buffer,c->mychallenge,len);
450 c->allow_request = CHAL_REPLY;
452 /* Rest is done by send_chal_reply() */
454 return send_chal_reply(c);
457 int send_chal_reply(connection_t *c)
459 char hash[SHA_DIGEST_LENGTH*2+1];
461 /* Calculate the hash from the challenge we received */
463 SHA1(c->mychallenge, RSA_size(myself->connection->rsa_key), hash);
465 /* Convert the hash to a hexadecimal formatted string */
467 bin2hex(hash,hash,SHA_DIGEST_LENGTH);
468 hash[SHA_DIGEST_LENGTH*2] = '\0';
473 return send_request(c, "%d %s", CHAL_REPLY, hash);
476 int chal_reply_h(connection_t *c)
478 char hishash[MAX_STRING_SIZE];
479 char myhash[SHA_DIGEST_LENGTH];
481 if(sscanf(c->buffer, "%*d "MAX_STRING, hishash) != 1)
483 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "CHAL_REPLY", c->name, c->hostname);
487 /* Check if the length of the hash is all right */
489 if(strlen(hishash) != SHA_DIGEST_LENGTH*2)
491 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, _("wrong challenge reply length"));
495 /* Convert the hash to binary format */
497 hex2bin(hishash, hishash, SHA_DIGEST_LENGTH);
499 /* Calculate the hash from the challenge we sent */
501 SHA1(c->hischallenge, RSA_size(c->rsa_key), myhash);
503 /* Verify the incoming hash with the calculated hash */
505 if(memcmp(hishash, myhash, SHA_DIGEST_LENGTH))
507 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, _("wrong challenge reply"));
508 if(debug_lvl >= DEBUG_SCARY_THINGS)
510 bin2hex(myhash, hishash, SHA_DIGEST_LENGTH);
511 hishash[SHA_DIGEST_LENGTH*2] = '\0';
512 syslog(LOG_DEBUG, _("Expected challenge reply: %s"), hishash);
517 /* Identity has now been positively verified.
518 Send an acknowledgement with the rest of the information needed.
521 c->allow_request = ACK;
526 int send_ack(connection_t *c)
528 /* ACK message contains rest of the information the other end needs
529 to create node_t and edge_t structures. */
533 /* Estimate weight */
535 gettimeofday(&now, NULL);
536 c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
538 return send_request(c, "%d %hd %d", ACK, myself->port, c->estimated_weight);
541 int ack_h(connection_t *c)
549 avl_node_t *node, *node2;
551 if(sscanf(c->buffer, "%*d %hd %d", &port, &weight) != 2)
553 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ACK", c->name, c->hostname);
557 /* Check if we already have a node_t for him */
559 n = lookup_node(c->name);
564 n->name = xstrdup(c->name);
565 n->address = c->address;
566 n->hostname = xstrdup(c->hostname);
569 /* FIXME: Also check if no other tinc daemon uses the same IP and port for UDP traffic */
577 /* Oh dear, we already have a connection to this node. */
578 syslog(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"), n->name, n->hostname);
579 terminate_connection(n->connection, 0);
582 /* FIXME: check if information in existing node matches that of the other end of this connection */
588 /* Check some options
590 if((cfg = get_config_val(c->config, config_indirectdata)))
592 if(cfg->data.val == stupid_true)
593 c->options |= OPTION_INDIRECT;
596 if((cfg = get_config_val(c->config, config_tcponly)))
598 if(cfg->data.val == stupid_true)
599 c->options |= OPTION_TCPONLY;
602 if((myself->options | c->options) & OPTION_INDIRECT)
609 /* Create an edge_t for this connection */
611 c->edge = new_edge();
613 c->edge->from = myself;
615 c->edge->weight = (weight + c->estimated_weight) / 2;
616 c->edge->connection = c;
620 /* Activate this connection */
622 c->allow_request = ALL;
623 c->status.active = 1;
624 c->node->cipher = EVP_bf_cbc();
625 c->node->keylength = c->node->cipher->key_len + c->node->cipher->iv_len;
627 if(debug_lvl >= DEBUG_CONNECTIONS)
628 syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name, c->hostname);
631 /* Send him our subnets */
633 for(node = myself->subnet_tree->head; node; node = node->next)
635 s = (subnet_t *)node->data;
636 send_add_subnet(c, s);
639 /* And send him all known nodes and their subnets */
641 for(node = node_tree->head; node; node = node->next)
643 n = (node_t *)node->data;
645 if(n == c->node || n == myself)
650 for(node2 = c->node->subnet_tree->head; node2; node2 = node2->next)
652 s = (subnet_t *)node2->data;
653 send_add_subnet(c, s);
657 /* Send all known edges */
659 for(node = edge_tree->head; node; node = node->next)
661 e = (edge_t *)node->data;
669 /* Notify others of this connection */
671 for(node = connection_tree->head; node; node = node->next)
673 other = (connection_t *)node->data;
678 send_add_node(other, c->node);
679 send_add_edge(other, c->edge);
682 /* Run MST and SSSP algorithms */
692 /* Address and subnet information exchange */
694 int send_add_subnet(connection_t *c, subnet_t *subnet)
699 x = send_request(c, "%d %s %s", ADD_SUBNET,
700 subnet->owner->name, netstr = net2str(subnet));
706 int add_subnet_h(connection_t *c)
708 char subnetstr[MAX_STRING_SIZE];
709 char name[MAX_STRING_SIZE];
715 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, name, subnetstr) != 2)
717 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_SUBNET", c->name, c->hostname);
721 /* Check if owner name is a valid */
725 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name, c->hostname, _("invalid name"));
729 /* Check if subnet string is valid */
731 if(!(s = str2net(subnetstr)))
733 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name, c->hostname, _("invalid subnet string"));
737 /* Check if the owner of the new subnet is in the connection list */
739 if(!(owner = lookup_node(name)))
741 syslog(LOG_ERR, _("Got ADD_SUBNET from %s (%s) for %s which is not in our connection list"),
742 name, c->name, c->hostname);
746 /* If everything is correct, add the subnet to the list of the owner */
748 subnet_add(owner, s);
752 for(node = connection_tree->head; node; node = node->next)
754 other = (connection_t *)node->data;
755 if(other->status.active && other != c)
756 send_add_subnet(other, s);
762 int send_del_subnet(connection_t *c, subnet_t *s)
767 x = send_request(c, "%d %s %s", DEL_SUBNET, s->owner->name, netstr = net2str(s));
773 int del_subnet_h(connection_t *c)
775 char subnetstr[MAX_STRING_SIZE];
776 char name[MAX_STRING_SIZE];
782 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, name, subnetstr) != 3)
784 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_SUBNET", c->name, c->hostname);
788 /* Check if owner name is a valid */
792 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name, c->hostname, _("invalid name"));
796 /* Check if subnet string is valid */
798 if(!(s = str2net(subnetstr)))
800 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name, c->hostname, _("invalid subnet string"));
804 /* Check if the owner of the new subnet is in the connection list */
806 if(!(owner = lookup_node(name)))
808 syslog(LOG_ERR, _("Got %s from %s (%s) for %s which is not in our connection list"),
809 "DEL_SUBNET", c->name, c->hostname, name);
813 /* If everything is correct, delete the subnet from the list of the owner */
815 find = lookup_subnet(owner, s);
819 syslog(LOG_ERR, _("Got %s from %s (%s) for %s which does not appear in his subnet tree"),
820 "DEL_SUBNET", c->name, c->hostname, name);
824 subnet_del(owner, s);
828 for(node = connection_tree->head; node; node = node->next)
830 other = (connection_t *)node->data;
831 if(other->status.active && other != c)
832 send_del_subnet(other, s);
838 /* New and closed connections notification */
840 int send_add_node(connection_t *c, node_t *n)
843 return send_request(c, "%d %s %lx:%d", ADD_NODE,
844 n->name, n->address, n->port);
847 int add_node_h(connection_t *c)
851 char name[MAX_STRING_SIZE];
856 if(sscanf(c->buffer, "%*d "MAX_STRING" %lx:%hd", name, &address, &port) != 3)
858 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_NODE", c->name, c->hostname);
862 /* Check if identity is a valid name */
866 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_NODE", c->name, c->hostname, _("invalid name"));
870 /* Check if node already exists */
872 n = lookup_node(name);
876 /* Check if it matches */
878 if(n->address != address || n->port != port)
879 syslog(LOG_DEBUG, _("Got %s from %s (%s) for %s which does not match existing entry"), "ADD_NODE", c->name, c->hostname, n->name);
886 n->name = xstrdup(name);
887 n->address = address;
892 /* Tell the rest about the new node */
894 for(node = connection_tree->head; node; node = node->next)
896 other = (connection_t *)node->data;
897 if(other->status.active && other !=c)
898 send_add_node(other, n);
905 int send_del_node(connection_t *c, node_t *n)
908 return send_request(c, "%d %s %lx:%d", DEL_NODE,
909 n->name, n->address, n->port);
912 int del_node_h(connection_t *c)
916 char name[MAX_STRING_SIZE];
920 avl_node_t *node, *next;
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 all edges associated with the node */
974 for(node = n->edge_tree->head; node; node = next)
977 e = (edge_t *)node->data;
981 /* Delete the node */
993 int send_add_edge(connection_t *c, edge_t *e)
996 return send_request(c, "%d %s %s %lx %d", ADD_NODE,
997 e->from->name, e->to->name, e->options, e->weight);
1000 int add_edge_h(connection_t *c)
1002 connection_t *other;
1005 char from_name[MAX_STRING_SIZE];
1006 char to_name[MAX_STRING_SIZE];
1011 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" %lx %d", from_name, to_name, &options, &weight) != 4)
1013 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name, c->hostname);
1017 /* Check if names are valid */
1019 if(check_id(from_name))
1021 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
1025 if(check_id(to_name))
1027 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
1033 from = lookup_node(from_name);
1037 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("unknown node"));
1041 to = lookup_node(to_name);
1045 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("unknown node"));
1049 /* Check if edge already exists */
1051 e = lookup_edge(from, to);
1055 if(e->weight != weight || e->options != options)
1057 syslog(LOG_ERR, _("Got %s from %s (%s) which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
1068 e->options = options;
1073 /* Tell the rest about the new edge */
1075 for(node = connection_tree->head; node; node = node->next)
1077 other = (connection_t *)node->data;
1078 if(other->status.active && other != c)
1079 send_add_edge(other, e);
1082 /* Run MST before or after we tell the rest? */
1090 int send_del_edge(connection_t *c, edge_t *e)
1093 return send_request(c, "%d %s %s %lx %d", DEL_EDGE,
1094 e->from->name, e->to->name, e->options, e->weight);
1097 int del_edge_h(connection_t *c)
1100 char from_name[MAX_STRING_SIZE];
1101 char to_name[MAX_STRING_SIZE];
1105 connection_t *other;
1108 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" %lx %d", from_name, to_name, &options, &weight) != 4)
1110 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE",
1111 c->name, c->hostname);
1115 /* Check if names are valid */
1117 if(check_id(from_name))
1119 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
1123 if(check_id(to_name))
1125 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
1131 from = lookup_node(from_name);
1135 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("unknown node"));
1139 to = lookup_node(to_name);
1143 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("unknown node"));
1147 /* Check if edge exists */
1149 e = lookup_edge(from, to);
1153 if(e->weight != weight || e->options != options)
1155 syslog(LOG_ERR, _("Got %s from %s (%s) which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
1161 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("unknown edge"));
1165 /* Tell the rest about the deleted edge */
1167 for(node = connection_tree->head; node; node = node->next)
1169 other = (connection_t *)node->data;
1170 if(other->status.active && other != c)
1171 send_del_edge(other, e);
1174 /* Delete the edge */
1178 /* Run MST before or after we tell the rest? */
1187 /* Status and error notification routines */
1189 int send_status(connection_t *c, int statusno, char *statusstring)
1193 statusstring = status_text[statusno];
1195 return send_request(c, "%d %d %s", STATUS, statusno, statusstring);
1198 int status_h(connection_t *c)
1201 char statusstring[MAX_STRING_SIZE];
1203 if(sscanf(c->buffer, "%*d %d "MAX_STRING, &statusno, statusstring) != 2)
1205 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "STATUS",
1206 c->name, c->hostname);
1210 if(debug_lvl >= DEBUG_STATUS)
1212 syslog(LOG_NOTICE, _("Status message from %s (%s): %s: %s"),
1213 c->name, c->hostname, status_text[statusno], statusstring);
1220 int send_error(connection_t *c, int err, char *errstring)
1224 errstring = strerror(err);
1225 return send_request(c, "%d %d %s", ERROR, err, errstring);
1228 int error_h(connection_t *c)
1231 char errorstring[MAX_STRING_SIZE];
1233 if(sscanf(c->buffer, "%*d %d "MAX_STRING, &err, errorstring) != 2)
1235 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ERROR",
1236 c->name, c->hostname);
1240 if(debug_lvl >= DEBUG_ERROR)
1242 syslog(LOG_NOTICE, _("Error message from %s (%s): %s: %s"),
1243 c->name, c->hostname, strerror(err), errorstring);
1246 terminate_connection(c, c->status.active);
1251 int send_termreq(connection_t *c)
1254 return send_request(c, "%d", TERMREQ);
1257 int termreq_h(connection_t *c)
1260 terminate_connection(c, c->status.active);
1265 int send_ping(connection_t *c)
1267 char salt[SALTLEN*2+1];
1269 c->status.pinged = 1;
1270 c->last_ping_time = time(NULL);
1271 RAND_pseudo_bytes(salt, SALTLEN);
1272 bin2hex(salt, salt, SALTLEN);
1273 salt[SALTLEN*2] = '\0';
1275 return send_request(c, "%d %s", PING, salt);
1278 int ping_h(connection_t *c)
1281 return send_pong(c);
1284 int send_pong(connection_t *c)
1286 char salt[SALTLEN*2+1];
1288 RAND_pseudo_bytes(salt, SALTLEN);
1289 bin2hex(salt, salt, SALTLEN);
1290 salt[SALTLEN*2] = '\0';
1292 return send_request(c, "%d %s", PONG, salt);
1295 int pong_h(connection_t *c)
1298 c->status.pinged = 0;
1305 int send_key_changed(connection_t *c, node_t *n)
1307 connection_t *other;
1310 /* Only send this message if some other daemon requested our key previously.
1311 This reduces unnecessary key_changed broadcasts.
1314 if(n == myself && !mykeyused)
1317 for(node = connection_tree->head; node; node = node->next)
1319 other = (connection_t *)node->data;
1320 if(other != c && other->status.active)
1321 send_request(other, "%d %s", KEY_CHANGED, n->name);
1327 int key_changed_h(connection_t *c)
1329 char name[MAX_STRING_SIZE];
1332 if(sscanf(c->buffer, "%*d "MAX_STRING, name) != 1)
1334 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
1335 c->name, c->hostname);
1339 n = lookup_node(name);
1343 syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"), "KEY_CHANGED",
1344 c->name, c->hostname, name);
1348 n->status.validkey = 0;
1349 n->status.waitingforkey = 0;
1351 send_key_changed(c, n);
1356 int send_req_key(connection_t *c, node_t *from, node_t *to)
1359 return send_request(c, "%d %s %s", REQ_KEY,
1360 from->name, to->name);
1363 int req_key_h(connection_t *c)
1365 char from_name[MAX_STRING_SIZE];
1366 char to_name[MAX_STRING_SIZE];
1368 char key[MAX_STRING_SIZE];
1370 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, from_name, to_name) != 2)
1372 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY",
1373 c->name, c->hostname);
1377 from = lookup_node(from_name);
1381 syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "REQ_KEY",
1382 c->name, c->hostname, from_name);
1386 to = lookup_node(to_name);
1390 syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "REQ_KEY",
1391 c->name, c->hostname, to_name);
1395 /* Check if this key request is for us */
1397 if(to == myself) /* Yes, send our own key back */
1399 bin2hex(myself->key, key, myself->keylength);
1400 key[myself->keylength * 2] = '\0';
1401 send_ans_key(c, myself, from, key);
1406 if(to->status.validkey) /* Proxy keys */
1408 bin2hex(to->key, key, to->keylength);
1409 key[to->keylength * 2] = '\0';
1410 send_ans_key(c, to, from, key);
1413 send_req_key(to->nexthop->connection, from, to);
1420 int send_ans_key(connection_t *c, node_t *from, node_t *to, char *key)
1423 return send_request(c, "%d %s %s %s", ANS_KEY,
1424 from->name, to->name, key);
1427 int ans_key_h(connection_t *c)
1429 char from_name[MAX_STRING_SIZE];
1430 char to_name[MAX_STRING_SIZE];
1431 char key[MAX_STRING_SIZE];
1435 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING, from_name, to_name, key) != 3)
1437 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY",
1438 c->name, c->hostname);
1442 from = lookup_node(from_name);
1446 syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "ANS_KEY",
1447 c->name, c->hostname, from_name);
1451 to = lookup_node(to_name);
1455 syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "ANS_KEY",
1456 c->name, c->hostname, to_name);
1460 /* Check correctness of packet key */
1462 keylength = strlen(key);
1464 if(keylength != from->keylength * 2)
1466 syslog(LOG_ERR, _("Got bad %s from %s (%s) origin %s: %s"), "ANS_KEY",
1467 c->name, c->hostname, from->name, _("invalid key length"));
1471 /* Forward it if necessary */
1475 send_ans_key(to->nexthop->connection, from, to, key);
1478 /* Update our copy of the origin's packet key */
1483 from->key = xstrdup(key);
1485 hex2bin(from->key, from->key, keylength);
1486 from->key[keylength] = '\0';
1488 from->status.validkey = 1;
1489 from->status.waitingforkey = 0;
1496 int send_tcppacket(connection_t *c, vpn_packet_t *packet)
1502 x = send_request(c, "%d %hd", PACKET, packet->len);
1507 return send_meta(c, packet->data, packet->len);
1510 int tcppacket_h(connection_t *c)
1514 if(sscanf(c->buffer, "%*d %hd", &len) != 1)
1516 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "PACKET", c->name, c->hostname);
1520 /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */
1527 /* Jumptable for the request handlers */
1529 int (*request_handlers[])(connection_t*) = {
1530 id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
1531 status_h, error_h, termreq_h,
1533 add_node_h, del_node_h,
1534 add_subnet_h, del_subnet_h,
1535 add_edge_h, del_edge_h,
1536 key_changed_h, req_key_h, ans_key_h,
1542 char (*request_name[]) = {
1543 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
1544 "STATUS", "ERROR", "TERMREQ",
1546 "ADD_NODE", "DEL_NODE",
1547 "ADD_SUBNET", "DEL_SUBNET",
1548 "ADD_EDGE", "DEL_EDGE",
1549 "KEY_CHANGED", "REQ_KEY", "ANS_KEY",
1553 /* Status strings */
1555 char (*status_text[]) = {
1561 char (*error_text[]) = {