2 protocol.c -- handle the meta-protocol
3 Copyright (C) 1999,2000 Ivo Timmermans <itimmermans@bigfoot.com>,
4 2000 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.32 2000/09/15 12:58:40 zarq Exp $
25 #include <sys/types.h>
30 #include <sys/socket.h>
37 #include <netinet/in.h>
39 #include <openssl/sha.h>
49 int check_id(char *id)
53 for (i = 0; i < strlen(id); i++)
55 if(!isalpha(id[i]) && id[i] != '_')
64 /* Generic outgoing request routine - takes care of logging and error detection as well */
66 int send_request(conn_list_t *cl, const char *format, int request, /*args*/ ...)
73 if(debug_lvl >= DEBUG_PROTOCOL)
74 syslog(LOG_DEBUG, _("Sending %s to %s (%s)"), request_name[request], cl->name, cl->hostname);
76 va_start(args, request);
77 len = vasprintf(&buffer, format, args);
80 if(len < 0 || !buffer)
82 syslog(LOG_ERR, _("Error during vasprintf(): %m"));
86 if(debug_lvl >= DEBUG_META)
87 syslog(LOG_DEBUG, _("Sending meta data to %s (%s): %s"),
88 cl->name, cl->hostname, buffer);
90 if(cl->status.encryptin)
92 /* FIXME: Do encryption */
95 if((write(cl->meta_socket, buffer, len)) < 0)
97 syslog(LOG_ERR, _("Sending meta data failed: %m"));
103 /* Connection protocol:
120 Part of the challenge is directly used to set the blowfish key and the initial vector.
121 (Twee vliegen in één klap!)
124 int send_id(conn_list_t *cl)
127 return send_request(cl, "%d %s %d %s", ID,
128 myself->name, myself->protocol_version, opt2str(myself->options));
131 int id_h(conn_list_t *cl)
136 if(sscanf(cl->buffer, "%*d %as %d %as", &cl->name, &cl->protocol_version, &options) != 3)
138 syslog(LOG_ERR, _("Got bad ID from %s"), cl->hostname);
142 /* Check if version matches */
144 if(cl->protocol_version != myself->protocol_version)
146 syslog(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"),
147 cl->name, cl->hostname, cl->protocol_version);
151 /* Check if option string is valid */
153 if((cl->options = str2opt(options)) == -1)
155 syslog(LOG_ERR, _("Peer %s uses invalid option string"), cl->hostname);
159 /* Check if identity is a valid name */
161 if(!check_id(cl->name))
163 syslog(LOG_ERR, _("Peer %s uses invalid identity name"), cl->hostname);
167 /* Load information about peer */
171 syslog(LOG_ERR, _("Peer %s had unknown identity (%s)"), cl->hostname, cl->name);
176 /* First check if the host we connected to is already in our
177 connection list. If so, we are probably making a loop, which
181 if(cl->status.outgoing)
183 if((old = lookup_id(cl->name)))
185 if(debug_lvl > DEBUG_CONNECTIONS)
186 syslog(LOG_NOTICE, _("Uplink %s (%s) is already in our connection list"), cl->name, cl->hostname);
187 cl->status.outgoing = 0;
188 old->status.outgoing = 1;
189 terminate_connection(cl);
194 /* Send a challenge to verify the identity */
196 cl->allow_request = CHAL_REPLY;
198 return send_challenge(cl);
201 int send_challenge(conn_list_t *cl)
208 free(cl->chal_answer);
210 /* Allocate buffers for the challenge and the hash */
212 cl->chal_answer = xmalloc(SHA_DIGEST_LENGTH);
213 keylength = BN_num_bytes(cl->rsakey->length);
214 buffer = xmalloc(keylength*2);
216 /* Copy random data and the public key to the buffer */
218 RAND_bytes(buffer, keylength);
219 BN_bn2bin(cl->rsakey->length, buffer+keylength);
221 /* If we don't have a blowfish key set yet, use the random data from the challenge to do so. */
223 if(!cl->status.encryptin)
225 set_metakey(cl, buffer, keylength);
228 /* Calculate the hash from that */
230 SHA1(buffer, keylength*2, cl->chal_answer);
232 /* Convert the random data to a hexadecimal formatted string */
234 bin2hex(buffer,buffer,keylength);
235 buffer[keylength*2] = '\0';
237 /* Send the challenge */
239 cl->allow_request = CHAL_REPLY;
240 x = send_request(cl, "%d %s", CHALLENGE, buffer);
242 cl->status.encryptout = 1;
247 int challenge_h(conn_list_t *cl)
253 if(sscanf(cl->buffer, "%*d %as", &cl->name, &challenge) != 1)
255 syslog(LOG_ERR, _("Got bad CHALLENGE from %s (%s)"), cl->name, cl->hostname);
259 /* Rest is done by send_chal_reply() */
261 x = send_chal_reply(cl, challenge);
267 int send_chal_reply(conn_list_t *cl, char *challenge)
275 keylength = BN_num_bytes(myself->rsakey->length);
277 /* Check if the length of the challenge is all right */
279 if(strlen(challenge) != keylength*2)
281 syslog(LOG_ERR, _("Intruder: wrong challenge length from %s (%s)"), cl->name, cl->hostname);
285 /* Allocate buffers for the challenge and the hash */
287 buffer = xmalloc(keylength*2);
288 hash = xmalloc(SHA_DIGEST_LENGTH*2+1);
290 /* Copy the incoming random data and our public key to the buffer */
292 hex2bin(challenge, buffer, keylength);
293 BN_bn2bin(myself->rsakey->length, buffer+keylength);
295 /* Calculate the hash from that */
297 SHA1(buffer, keylength*2, hash);
299 /* If we don't have a blowfish key set yet, use the random data from the challenge to do so. */
301 if(!cl->status.encrypted)
303 set_metakey(cl, buffer, keylength);
304 cl->status.encrypted = 1;
309 /* Convert the hash to a hexadecimal formatted string */
311 bin2hex(hash,hash,SHA_DIGEST_LENGTH);
312 hash[SHA_DIGEST_LENGTH*2] = '\0';
316 if(cl->status.outgoing)
317 cl->allow_request = ID;
319 cl->allow_request = ACK;
321 x = send_request(cl, "%d %s", CHAL_REPLY, hash);
327 int chal_reply_h(conn_list_t *cl)
331 if(sscanf(cl->buffer, "%*d %as", &cl->name, &hash) != 2)
333 syslog(LOG_ERR, _("Got bad CHAL_REPLY from %s (%s)"), cl->name, cl->hostname);
337 /* Check if the length of the hash is all right */
339 if(strlen(hash) != SHA_DIGEST_LENGTH*2)
341 syslog(LOG_ERR, _("Intruder: wrong challenge reply length from %s (%s)"), cl->name, cl->hostname);
345 /* Convert the hash to binary format */
347 hex2bin(hash, hash, SHA_DIGEST_LENGTH);
349 /* Verify the incoming hash with the calculated hash */
351 if(!memcmp(hash, cl->chal_answer, SHA_DIGEST_LENGTH))
353 syslog(LOG_ERR, _("Intruder: wrong challenge reply from %s (%s)"), cl->name, cl->hostname);
357 /* Identity has now been positively verified.
358 If we are accepting this new connection, then send our identity,
359 if we are making this connecting, acknowledge.
363 free(cl->chal_answer);
366 if(cl->status.outgoing)
368 cl->allow_request = ACK;
373 cl->allow_request = CHALLENGE;
378 int send_ack(conn_list_t *cl)
381 return send_request(cl, "%d", ACK);
384 int ack_h(conn_list_t *cl)
389 /* Okay, before we active the connection, we check if there is another entry
390 in the connection list with the same vpn_ip. If so, it presumably is an
391 old connection that has timed out but we don't know it yet.
394 while((old = lookup_id(cl->name)))
396 if(debug_lvl > DEBUG_CONNECTIONS)
397 syslog(LOG_NOTICE, _("Removing old entry for %s at %s in favour of new connection from %s"),
398 cl->name, old->hostname, cl->hostname);
399 old->status.active = 0;
400 terminate_connection(old);
403 /* Activate this connection */
405 cl->allow_request = ALL;
406 cl->status.active = 1;
408 if(debug_lvl > DEBUG_CONNECTIONS)
409 syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), cl->name, cl->hostname);
411 /* Exchange information about other tinc daemons */
413 notify_others(cl, NULL, send_add_host);
419 if(cl->status.outgoing)
425 /* Address and subnet information exchange */
427 int send_add_subnet(conn_list_t *cl, conn_list_t *other, subnet_t *subnet)
430 /* return send_request(cl, "%d %s %d %s", ADD_SUBNET,
431 other->name, subnet->type, net2str(subnet)); */
432 return send_request(cl, "%d %s %s", ADD_SUBNET,
433 other->name, net2str(subnet));
436 int add_subnet_h(conn_list_t *cl)
440 int send_del_subnet(conn_list_t *cl, conn_list_t *other, subnet_t *subnet)
443 return send_request(cl, "%d %s %s", DEL_SUBNET, other->name, net2str(subnet));
446 int del_subnet_h(conn_list_t *cl)
450 /* New and closed connections notification */
452 int send_add_host(conn_list_t *cl, conn_list_t *other)
455 return send_request(cl, "%d %s %lx:%d %s", ADD_HOST, other->name, other->real_ip, other->port, opt2str(other->options));
458 int add_host_h(conn_list_t *cl)
461 conn_list_t *old, *new;
463 new = new_conn_list();
465 if(sscanf(cl->buffer, "%*d %as %lx:%d %as", &new->name, &new->real_ip, &new->port, &options) != 4)
467 syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s)"), cl->name, cl->hostname);
471 /* Check if option string is valid */
473 if((new->options = str2opt(options)) == -1)
475 syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s): invalid option string"), cl->name, cl->hostname);
479 /* Check if identity is a valid name */
481 if(!check_id(new->name))
483 syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s): invalid identity name"), cl->name, cl->hostname);
487 /* Check if somebody tries to add ourself */
489 if(!strcmp(new->name, myself->name))
491 syslog(LOG_ERR, _("Warning: got ADD_HOST from %s (%s) for ourself, restarting"), cl->name, cl->hostname);
496 /* Fill in more of the new conn_list structure */
498 new->hostname = hostlookup(htonl(new->real_ip));
500 /* Check if the new host already exists in the connnection list */
502 if((old = lookup_id(new->name)))
504 if((new->real_ip == old->real_ip) && (new->port == old->port))
506 if(debug_lvl > DEBUG_CONNECTIONS)
507 syslog(LOG_NOTICE, _("Got duplicate ADD_HOST for %s (%s) from %s (%s)"),
508 old->name, old->hostname, new->name, new->hostname);
513 if(debug_lvl > DEBUG_CONNECTIONS)
514 syslog(LOG_NOTICE, _("Removing old entry for %s (%s)"),
515 old->name, old->hostname);
516 old->status.active = 0;
517 terminate_connection(old);
521 /* Fill in rest of conn_list structure */
524 new->status.active = 1;
526 /* Hook it up into the conn_list */
528 conn_list_add(conn_list, new);
530 /* Tell the rest about the new host */
532 notify_others(new, cl, send_add_host);
538 int send_del_host(conn_list_t *cl, conn_list_t *other)
541 return send_request(cl, "%d %s %lx:%d", DEL_HOST,
542 other->name, other->real_ip, other->port);
545 int del_host_h(conn_list_t *cl)
553 if(sscanf(cl->buffer, "%*d %as %lx:%d", &id, &address, &port) != 3)
555 syslog(LOG_ERR, _("Got bad DEL_HOST from %s (%s)"),
556 cl->name, cl->hostname);
560 /* Check if somebody tries to delete ourself */
562 if(!strcmp(id, myself->name))
564 syslog(LOG_ERR, _("Warning: got DEL_HOST from %s (%s) for ourself, restarting"),
565 cl->name, cl->hostname);
570 /* Check if the new host already exists in the connnection list */
572 if((old = lookup_id(id)))
574 if((address == old->real_ip) && (port == old->port))
576 notify_others(old, cl, send_del_host);
578 old->status.termreq = 1;
579 old->status.active = 0;
581 terminate_connection(old);
587 if(debug_lvl > DEBUG_CONNECTIONS)
589 syslog(LOG_NOTICE, _("Got DEL_HOST for %s from %s (%s) which is not in our connection list"),
590 id, cl->name, cl->hostname);
596 /* Status and error notification routines */
598 int send_status(conn_list_t *cl, int statusno, char *statusstring)
602 statusstring = status_text[statusno];
604 return send_request(cl, "%d %d %s", STATUS, statusno, statusstring);
607 int status_h(conn_list_t *cl)
612 if(sscanf(cl->buffer, "%*d %d %as", &statusno, &statusstring) != 2)
614 syslog(LOG_ERR, _("Got bad STATUS from %s (%s)"),
615 cl->name, cl->hostname);
619 if(debug_lvl > DEBUG_STATUS)
621 syslog(LOG_NOTICE, _("Status message from %s (%s): %s: %s"),
622 cl->name, cl->hostname, status_text[statusno], statusstring);
630 int send_error(conn_list_t *cl, int errno, char *errstring)
634 errstring = strerror(errno);
635 return send_request(cl, "%d %d %s", ERROR, errno, errstring);
638 int error_h(conn_list_t *cl)
643 if(sscanf(cl->buffer, "%*d %d %as", &errno, &errorstring) != 2)
645 syslog(LOG_ERR, _("Got bad error from %s (%s)"),
646 cl->name, cl->hostname);
650 if(debug_lvl > DEBUG_error)
652 syslog(LOG_NOTICE, _("Error message from %s (%s): %s: %s"),
653 cl->name, cl->hostname, strerror(errno), errorstring);
657 cl->status.termreq = 1;
658 terminate_connection(cl);
663 int send_termreq(conn_list_t *cl)
666 return send_request(cl, "%d", TERMREQ);
669 int termreq_h(conn_list_t *cl)
672 cl->status.termreq = 1;
673 terminate_connection(cl);
678 /* Keepalive routines - FIXME: needs a closer look */
680 int send_ping(conn_list_t *cl)
682 cl->status.pinged = 1;
684 return send_request(cl, "%d", PING);
687 int ping_h(conn_list_t *cl)
690 return send_pong(cl);
693 int send_pong(conn_list_t *cl)
696 return send_request(cl, "%d", PONG);
699 int pong_h(conn_list_t *cl)
702 cl->status.got_pong = 1;
709 int send_key_changed(conn_list_t *from, conn_list_t *cl)
713 for(p = conn_list; p != NULL; p = p->next)
715 if(p!=cl && p->status.meta && p->status.active)
716 send_request(p, "%d %s", KEY_CHANGED,
723 int key_changed_h(conn_list_t *cl)
728 if(sscanf(cl->buffer, "%*d %as", &from_id) != 1)
730 syslog(LOG_ERR, _("Got bad KEY_CHANGED from %s (%s)"),
731 cl->name, cl->hostname);
735 if(!(from = lookup_id(from_id)))
737 syslog(LOG_ERR, _("Got KEY_CHANGED from %s (%s) origin %s which does not exist in our connection list"),
738 cl->name, cl->hostname, from_id);
745 from->status.validkey = 0;
746 from->status.waitingforkey = 0;
748 send_key_changed(from, cl);
753 int send_req_key(conn_list_t *from, conn_list_t *to)
756 return send_request(to->nexthop, "%d %s %s", REQ_KEY,
757 from->name, to->name);
760 int req_key_h(conn_list_t *cl)
762 char *from_id, *to_id;
763 conn_list_t *from, *to;
765 if(sscanf(cl->buffer, "%*d %as %as", &from_id, &to_id) != 2)
767 syslog(LOG_ERR, _("Got bad REQ_KEY from %s (%s)"),
768 cl->name, cl->hostname);
772 if(!(from = lookup_id(from_id)))
774 syslog(LOG_ERR, _("Got REQ_KEY from %s (%s) origin %s which does not exist in our connection list"),
775 cl->name, cl->hostname, from_id);
776 free(from_id); free(to_id);
780 /* Check if this key request is for us */
782 if(!strcmp(to_id, myself->name))
784 send_ans_key(myself, from, myself->datakey->key);
788 if(!(to = lookup_id(to_id)))
790 syslog(LOG_ERR, _("Got REQ_KEY from %s (%s) destination %s which does not exist in our connection list"),
791 cl->name, cl->hostname, to_id);
792 free(from_id); free(to_id);
795 send_req_key(from, to);
798 free(from_id); free(to_id);
803 int send_ans_key(conn_list_t *from, conn_list_t *to, char *datakey)
806 return send_request(to->nexthop, "%d %s %s %s", ANS_KEY,
807 from->name, to->name, datakey);
810 int ans_key_h(conn_list_t *cl)
812 char *from_id, *to_id, *datakey;
814 conn_list_t *from, *to;
816 if(sscanf(cl->buffer, "%*d %as %as %as", &from_id, &to_id, &datakey) != 3)
818 syslog(LOG_ERR, _("Got bad ANS_KEY from %s (%s)"),
819 cl->name, cl->hostname);
823 if(!(from = lookup_id(from_id)))
825 syslog(LOG_ERR, _("Got ANS_KEY from %s (%s) origin %s which does not exist in our connection list"),
826 cl->name, cl->hostname, from_id);
827 free(from_id); free(to_id); free(datakey);
831 /* Check if this key request is for us */
833 if(!strcmp(to_id, myself->name))
835 /* It is for us, convert it to binary and set the key with it. */
837 keylength = strlen(datakey);
839 if((keylength%2) || (keylength <= 0))
841 syslog(LOG_ERR, _("Got bad ANS_KEY from %s (%s) origin %s: invalid key"),
842 cl->name, cl->hostname, from->name);
843 free(from_id); free(to_id); free(datakey);
847 hex2bin(datakey, datakey, keylength);
848 BF_set_key(cl->datakey, keylength, datakey);
852 if(!(to = lookup_id(to_id)))
854 syslog(LOG_ERR, _("Got ANS_KEY from %s (%s) destination %s which does not exist in our connection list"),
855 cl->name, cl->hostname, to_id);
856 free(from_id); free(to_id); free(datakey);
859 send_ans_key(from, to, datakey);
862 free(from_id); free(to_id); free(datakey);
870 Notify all my direct connections of a new host
871 that was added to the vpn, with the exception
872 of the source of the announcement.
875 int notify_others(conn_list_t *new, conn_list_t *source,
876 int (*function)(conn_list_t*, conn_list_t*))
880 for(p = conn_list; p != NULL; p = p->next)
881 if(p != new && p != source && p->status.meta && p->status.active)
888 Notify one connection of everything
892 int notify_one(conn_list_t *new)
896 for(p = conn_list; p != NULL; p = p->next)
897 if(p != new && p->status.active)
898 send_add_host(new, p);
903 /* "Complete overhaul". */
905 int (*request_handlers[])(conn_list_t*) = {
906 id_h, challenge_h, chal_reply_h, ack_h,
907 status_h, error_h, termreq_h,
909 add_host_h, del_host_h,
910 add_subnet_h, del_subnet_h,
911 key_changed_h, req_key_h, ans_key_h,
914 char (*request_name[]) = {
915 "ID", "CHALLENGE", "CHAL_REPLY", "ACK",
916 "STATUS", "ERROR", "TERMREQ",
918 "ADD_HOST", "DEL_HOST",
919 "ADD_SUBNET", "DEL_SUBNET",
920 "KEY_CHANGED", "REQ_KEY", "ANS_KEY",