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.33 2000/09/17 21:42:05 guus 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*/ ...)
69 char buffer[MAXBUFSIZE+1];
73 /* Use vsnprintf instead of vasprintf: faster, no memory fragmentation, cleanup is automatic,
74 and there is a limit on the input buffer anyway */
76 va_start(args, request);
77 len = vsnprintf(buffer, MAXBUFSIZE+1, format, args);
80 if(len < 0 || len > MAXBUFSIZE)
82 syslog(LOG_ERR, _("Output buffer overflow while sending %s to %s (%s)"), request_name[request], cl->name, cl->hostname);
86 if(debug_lvl >= DEBUG_META)
87 syslog(LOG_DEBUG, _("Sending %s to %s (%s): %s"), request_name[request],
88 cl->name, cl->hostname, buffer);
89 else if(debug_lvl >= DEBUG_PROTOCOL)
90 syslog(LOG_DEBUG, _("Sending %s to %s (%s)"), request_name[request], cl->name, cl->hostname);
93 if(cl->status.encryptin)
95 /* FIXME: Do encryption */
98 if((write(cl->meta_socket, buffer, len)) < 0)
100 syslog(LOG_ERR, _("Sending meta data failed: %m"));
106 /* Connection protocol:
115 ---------------------------------------
116 Any negotations about the meta protocol
117 encryption go here(u).
118 ---------------------------------------
121 ---------------------------------------
127 (E) Encrypted with symmetric cipher.
129 Part of the challenge is directly used to set the symmetric cipher key and the initial vector.
130 Since a man-in-the-middle cannot decrypt the RSA challenges, this means that he cannot get or
131 forge the key for the symmetric cipher.
134 int send_id(conn_list_t *cl)
137 return send_request(cl, "%d %s %d %s", ID,
138 myself->name, myself->protocol_version, opt2str(myself->options));
141 int id_h(conn_list_t *cl)
146 if(sscanf(cl->buffer, "%*d %as %d %as", &cl->name, &cl->protocol_version, &options) != 3)
148 syslog(LOG_ERR, _("Got bad ID from %s"), cl->hostname);
152 /* Check if version matches */
154 if(cl->protocol_version != myself->protocol_version)
156 syslog(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"),
157 cl->name, cl->hostname, cl->protocol_version);
161 /* Check if option string is valid */
163 if((cl->options = str2opt(options)) == -1)
165 syslog(LOG_ERR, _("Peer %s uses invalid option string"), cl->hostname);
169 /* Check if identity is a valid name */
171 if(!check_id(cl->name))
173 syslog(LOG_ERR, _("Peer %s uses invalid identity name"), cl->hostname);
177 /* Load information about peer */
181 syslog(LOG_ERR, _("Peer %s had unknown identity (%s)"), cl->hostname, cl->name);
186 /* First check if the host we connected to is already in our
187 connection list. If so, we are probably making a loop, which
191 if(cl->status.outgoing)
193 if((old = lookup_id(cl->name)))
195 if(debug_lvl > DEBUG_CONNECTIONS)
196 syslog(LOG_NOTICE, _("Uplink %s (%s) is already in our connection list"), cl->name, cl->hostname);
197 cl->status.outgoing = 0;
198 old->status.outgoing = 1;
199 terminate_connection(cl);
204 /* Send a challenge to verify the identity */
206 cl->allow_request = CHAL_REPLY;
208 return send_challenge(cl);
211 int send_challenge(conn_list_t *cl)
218 free(cl->chal_answer);
220 /* Allocate buffers for the challenge and the hash */
222 cl->chal_answer = xmalloc(SHA_DIGEST_LENGTH);
223 keylength = BN_num_bytes(cl->rsakey->length);
224 buffer = xmalloc(keylength*2);
226 /* Copy random data and the public key to the buffer */
228 RAND_bytes(buffer, keylength);
229 BN_bn2bin(cl->rsakey->length, buffer+keylength);
231 /* If we don't have a blowfish key set yet, use the random data from the challenge to do so. */
233 if(!cl->status.encryptin)
235 set_metakey(cl, buffer, keylength);
238 /* Calculate the hash from that */
240 SHA1(buffer, keylength*2, cl->chal_answer);
242 /* Convert the random data to a hexadecimal formatted string */
244 bin2hex(buffer,buffer,keylength);
245 buffer[keylength*2] = '\0';
247 /* Send the challenge */
249 cl->allow_request = CHAL_REPLY;
250 x = send_request(cl, "%d %s", CHALLENGE, buffer);
252 cl->status.encryptout = 1;
257 int challenge_h(conn_list_t *cl)
263 if(sscanf(cl->buffer, "%*d %as", &cl->name, &challenge) != 1)
265 syslog(LOG_ERR, _("Got bad CHALLENGE from %s (%s)"), cl->name, cl->hostname);
269 /* Rest is done by send_chal_reply() */
271 x = send_chal_reply(cl, challenge);
277 int send_chal_reply(conn_list_t *cl, char *challenge)
285 keylength = BN_num_bytes(myself->rsakey->length);
287 /* Check if the length of the challenge is all right */
289 if(strlen(challenge) != keylength*2)
291 syslog(LOG_ERR, _("Intruder: wrong challenge length from %s (%s)"), cl->name, cl->hostname);
295 /* Allocate buffers for the challenge and the hash */
297 buffer = xmalloc(keylength*2);
298 hash = xmalloc(SHA_DIGEST_LENGTH*2+1);
300 /* Copy the incoming random data and our public key to the buffer */
302 hex2bin(challenge, buffer, keylength);
303 BN_bn2bin(myself->rsakey->length, buffer+keylength);
305 /* Calculate the hash from that */
307 SHA1(buffer, keylength*2, hash);
309 /* If we don't have a blowfish key set yet, use the random data from the challenge to do so. */
311 if(!cl->status.encrypted)
313 set_metakey(cl, buffer, keylength);
314 cl->status.encrypted = 1;
319 /* Convert the hash to a hexadecimal formatted string */
321 bin2hex(hash,hash,SHA_DIGEST_LENGTH);
322 hash[SHA_DIGEST_LENGTH*2] = '\0';
326 if(cl->status.outgoing)
327 cl->allow_request = ID;
329 cl->allow_request = ACK;
331 x = send_request(cl, "%d %s", CHAL_REPLY, hash);
337 int chal_reply_h(conn_list_t *cl)
341 if(sscanf(cl->buffer, "%*d %as", &cl->name, &hash) != 2)
343 syslog(LOG_ERR, _("Got bad CHAL_REPLY from %s (%s)"), cl->name, cl->hostname);
347 /* Check if the length of the hash is all right */
349 if(strlen(hash) != SHA_DIGEST_LENGTH*2)
351 syslog(LOG_ERR, _("Intruder: wrong challenge reply length from %s (%s)"), cl->name, cl->hostname);
355 /* Convert the hash to binary format */
357 hex2bin(hash, hash, SHA_DIGEST_LENGTH);
359 /* Verify the incoming hash with the calculated hash */
361 if(!memcmp(hash, cl->chal_answer, SHA_DIGEST_LENGTH))
363 syslog(LOG_ERR, _("Intruder: wrong challenge reply from %s (%s)"), cl->name, cl->hostname);
367 /* Identity has now been positively verified.
368 If we are accepting this new connection, then send our identity,
369 if we are making this connecting, acknowledge.
373 free(cl->chal_answer);
376 if(cl->status.outgoing)
378 cl->allow_request = ACK;
383 cl->allow_request = CHALLENGE;
388 int send_ack(conn_list_t *cl)
391 return send_request(cl, "%d", ACK);
394 int ack_h(conn_list_t *cl)
399 /* Okay, before we active the connection, we check if there is another entry
400 in the connection list with the same vpn_ip. If so, it presumably is an
401 old connection that has timed out but we don't know it yet.
404 while((old = lookup_id(cl->name)))
406 if(debug_lvl > DEBUG_CONNECTIONS)
407 syslog(LOG_NOTICE, _("Removing old entry for %s at %s in favour of new connection from %s"),
408 cl->name, old->hostname, cl->hostname);
409 old->status.active = 0;
410 terminate_connection(old);
413 /* Activate this connection */
415 cl->allow_request = ALL;
416 cl->status.active = 1;
418 if(debug_lvl > DEBUG_CONNECTIONS)
419 syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), cl->name, cl->hostname);
421 /* Exchange information about other tinc daemons */
423 notify_others(cl, NULL, send_add_host);
429 if(cl->status.outgoing)
435 /* Address and subnet information exchange */
437 int send_add_subnet(conn_list_t *cl, conn_list_t *other, subnet_t *subnet)
440 return send_request(cl, "%d %s %s", ADD_SUBNET,
441 other->name, net2str(subnet));
444 int add_subnet_h(conn_list_t *cl)
449 subnet_t *subnet, *old;
451 if(sscanf(cl->buffer, "%*d %as %as", &name, &subnetstr) != 3)
453 syslog(LOG_ERR, _("Got bad ADD_SUBNET from %s (%s)"), cl->name, cl->hostname);
457 /* Check if owner name is a valid */
461 syslog(LOG_ERR, _("Got bad ADD_SUBNET from %s (%s): invalid identity name"), cl->name, cl->hostname);
465 /* Check if subnet string is valid */
467 if((subnet = str2net(subnetstr)) == -1)
469 syslog(LOG_ERR, _("Got bad ADD_SUBNET from %s (%s): invalid subnet string"), cl->name, cl->hostname);
473 /* Check if somebody tries to add a subnet of ourself */
475 if(!strcmp(name, myself->name))
477 syslog(LOG_ERR, _("Warning: got ADD_SUBNET from %s (%s) for ourself, restarting"),
478 cl->name, cl->hostname);
483 /* Check if the owner of the new subnet is in the connection list */
485 if(!(owner = lookup_id(name))
487 syslog(LOG_NOTICE, _("Got ADD_SUBNET for %s from %s (%s) which is not in our connection list"),
488 name, cl->name, cl->hostname);
494 int send_del_subnet(conn_list_t *cl, conn_list_t *other, subnet_t *subnet)
497 return send_request(cl, "%d %s %s", DEL_SUBNET, other->name, net2str(subnet));
500 int del_subnet_h(conn_list_t *cl)
505 subnet_t *subnet, *old;
507 if(sscanf(cl->buffer, "%*d %as %as", &name, &subnetstr) != 3)
509 syslog(LOG_ERR, _("Got bad DEL_SUBNET from %s (%s)"), cl->name, cl->hostname);
513 /* Check if owner name is a valid */
517 syslog(LOG_ERR, _("Got bad DEL_SUBNET from %s (%s): invalid identity name"), cl->name, cl->hostname);
521 /* Check if subnet string is valid */
523 if((subnet = str2net(subnetstr)) == -1)
525 syslog(LOG_ERR, _("Got bad DEL_SUBNET from %s (%s): invalid subnet string"), cl->name, cl->hostname);
529 /* Check if somebody tries to delete a subnet of ourself */
531 if(!strcmp(name, myself->name))
533 syslog(LOG_ERR, _("Warning: got DEL_SUBNET from %s (%s) for ourself, restarting"),
534 cl->name, cl->hostname);
539 /* Check if the owner of the new subnet is in the connection list */
541 if(!(owner = lookup_id(name))
543 syslog(LOG_NOTICE, _("Got DEL_SUBNET for %s from %s (%s) which is not in our connection list"),
544 name, cl->name, cl->hostname);
548 /* New and closed connections notification */
550 int send_add_host(conn_list_t *cl, conn_list_t *other)
553 return send_request(cl, "%d %s %lx:%d %s", ADD_HOST, other->name, other->real_ip, other->port, opt2str(other->options));
556 int add_host_h(conn_list_t *cl)
559 conn_list_t *old, *new;
561 new = new_conn_list();
563 if(sscanf(cl->buffer, "%*d %as %lx:%d %as", &new->name, &new->real_ip, &new->port, &options) != 4)
565 syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s)"), cl->name, cl->hostname);
569 /* Check if option string is valid */
571 if((new->options = str2opt(options)) == -1)
573 syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s): invalid option string"), cl->name, cl->hostname);
577 /* Check if identity is a valid name */
579 if(!check_id(new->name))
581 syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s): invalid identity name"), cl->name, cl->hostname);
585 /* Check if somebody tries to add ourself */
587 if(!strcmp(new->name, myself->name))
589 syslog(LOG_ERR, _("Warning: got ADD_HOST from %s (%s) for ourself, restarting"), cl->name, cl->hostname);
594 /* Fill in more of the new conn_list structure */
596 new->hostname = hostlookup(htonl(new->real_ip));
598 /* Check if the new host already exists in the connnection list */
600 if((old = lookup_id(new->name)))
602 if((new->real_ip == old->real_ip) && (new->port == old->port))
604 if(debug_lvl > DEBUG_CONNECTIONS)
605 syslog(LOG_NOTICE, _("Got duplicate ADD_HOST for %s (%s) from %s (%s)"),
606 old->name, old->hostname, new->name, new->hostname);
611 if(debug_lvl > DEBUG_CONNECTIONS)
612 syslog(LOG_NOTICE, _("Removing old entry for %s (%s)"),
613 old->name, old->hostname);
614 old->status.active = 0;
615 terminate_connection(old);
619 /* Fill in rest of conn_list structure */
622 new->status.active = 1;
624 /* Hook it up into the conn_list */
626 conn_list_add(conn_list, new);
628 /* Tell the rest about the new host */
630 notify_others(new, cl, send_add_host);
636 int send_del_host(conn_list_t *cl, conn_list_t *other)
639 return send_request(cl, "%d %s %lx:%d", DEL_HOST,
640 other->name, other->real_ip, other->port);
643 int del_host_h(conn_list_t *cl)
651 if(sscanf(cl->buffer, "%*d %as %lx:%d", &id, &address, &port) != 3)
653 syslog(LOG_ERR, _("Got bad DEL_HOST from %s (%s)"),
654 cl->name, cl->hostname);
658 /* Check if somebody tries to delete ourself */
660 if(!strcmp(id, myself->name))
662 syslog(LOG_ERR, _("Warning: got DEL_HOST from %s (%s) for ourself, restarting"),
663 cl->name, cl->hostname);
668 /* Check if the new host already exists in the connnection list */
670 if((old = lookup_id(id)))
672 if((address == old->real_ip) && (port == old->port))
674 notify_others(old, cl, send_del_host);
676 old->status.termreq = 1;
677 old->status.active = 0;
679 terminate_connection(old);
685 if(debug_lvl > DEBUG_CONNECTIONS)
687 syslog(LOG_NOTICE, _("Got DEL_HOST for %s from %s (%s) which is not in our connection list"),
688 id, cl->name, cl->hostname);
694 /* Status and error notification routines */
696 int send_status(conn_list_t *cl, int statusno, char *statusstring)
700 statusstring = status_text[statusno];
702 return send_request(cl, "%d %d %s", STATUS, statusno, statusstring);
705 int status_h(conn_list_t *cl)
710 if(sscanf(cl->buffer, "%*d %d %as", &statusno, &statusstring) != 2)
712 syslog(LOG_ERR, _("Got bad STATUS from %s (%s)"),
713 cl->name, cl->hostname);
717 if(debug_lvl > DEBUG_STATUS)
719 syslog(LOG_NOTICE, _("Status message from %s (%s): %s: %s"),
720 cl->name, cl->hostname, status_text[statusno], statusstring);
728 int send_error(conn_list_t *cl, int errno, char *errstring)
732 errstring = strerror(errno);
733 return send_request(cl, "%d %d %s", ERROR, errno, errstring);
736 int error_h(conn_list_t *cl)
741 if(sscanf(cl->buffer, "%*d %d %as", &errno, &errorstring) != 2)
743 syslog(LOG_ERR, _("Got bad error from %s (%s)"),
744 cl->name, cl->hostname);
748 if(debug_lvl > DEBUG_error)
750 syslog(LOG_NOTICE, _("Error message from %s (%s): %s: %s"),
751 cl->name, cl->hostname, strerror(errno), errorstring);
755 cl->status.termreq = 1;
756 terminate_connection(cl);
761 int send_termreq(conn_list_t *cl)
764 return send_request(cl, "%d", TERMREQ);
767 int termreq_h(conn_list_t *cl)
770 cl->status.termreq = 1;
771 terminate_connection(cl);
776 /* Keepalive routines - FIXME: needs a closer look */
778 int send_ping(conn_list_t *cl)
780 cl->status.pinged = 1;
782 return send_request(cl, "%d", PING);
785 int ping_h(conn_list_t *cl)
788 return send_pong(cl);
791 int send_pong(conn_list_t *cl)
794 return send_request(cl, "%d", PONG);
797 int pong_h(conn_list_t *cl)
800 cl->status.got_pong = 1;
807 int send_key_changed(conn_list_t *from, conn_list_t *cl)
811 for(p = conn_list; p != NULL; p = p->next)
813 if(p!=cl && p->status.meta && p->status.active)
814 send_request(p, "%d %s", KEY_CHANGED,
821 int key_changed_h(conn_list_t *cl)
826 if(sscanf(cl->buffer, "%*d %as", &from_id) != 1)
828 syslog(LOG_ERR, _("Got bad KEY_CHANGED from %s (%s)"),
829 cl->name, cl->hostname);
833 if(!(from = lookup_id(from_id)))
835 syslog(LOG_ERR, _("Got KEY_CHANGED from %s (%s) origin %s which does not exist in our connection list"),
836 cl->name, cl->hostname, from_id);
843 from->status.validkey = 0;
844 from->status.waitingforkey = 0;
846 send_key_changed(from, cl);
851 int send_req_key(conn_list_t *from, conn_list_t *to)
854 return send_request(to->nexthop, "%d %s %s", REQ_KEY,
855 from->name, to->name);
858 int req_key_h(conn_list_t *cl)
860 char *from_id, *to_id;
861 conn_list_t *from, *to;
863 if(sscanf(cl->buffer, "%*d %as %as", &from_id, &to_id) != 2)
865 syslog(LOG_ERR, _("Got bad REQ_KEY from %s (%s)"),
866 cl->name, cl->hostname);
870 if(!(from = lookup_id(from_id)))
872 syslog(LOG_ERR, _("Got REQ_KEY from %s (%s) origin %s which does not exist in our connection list"),
873 cl->name, cl->hostname, from_id);
874 free(from_id); free(to_id);
878 /* Check if this key request is for us */
880 if(!strcmp(to_id, myself->name))
882 send_ans_key(myself, from, myself->datakey->key);
886 if(!(to = lookup_id(to_id)))
888 syslog(LOG_ERR, _("Got REQ_KEY from %s (%s) destination %s which does not exist in our connection list"),
889 cl->name, cl->hostname, to_id);
890 free(from_id); free(to_id);
893 send_req_key(from, to);
896 free(from_id); free(to_id);
901 int send_ans_key(conn_list_t *from, conn_list_t *to, char *datakey)
904 return send_request(to->nexthop, "%d %s %s %s", ANS_KEY,
905 from->name, to->name, datakey);
908 int ans_key_h(conn_list_t *cl)
910 char *from_id, *to_id, *datakey;
912 conn_list_t *from, *to;
914 if(sscanf(cl->buffer, "%*d %as %as %as", &from_id, &to_id, &datakey) != 3)
916 syslog(LOG_ERR, _("Got bad ANS_KEY from %s (%s)"),
917 cl->name, cl->hostname);
921 if(!(from = lookup_id(from_id)))
923 syslog(LOG_ERR, _("Got ANS_KEY from %s (%s) origin %s which does not exist in our connection list"),
924 cl->name, cl->hostname, from_id);
925 free(from_id); free(to_id); free(datakey);
929 /* Check if this key request is for us */
931 if(!strcmp(to_id, myself->name))
933 /* It is for us, convert it to binary and set the key with it. */
935 keylength = strlen(datakey);
937 if((keylength%2) || (keylength <= 0))
939 syslog(LOG_ERR, _("Got bad ANS_KEY from %s (%s) origin %s: invalid key"),
940 cl->name, cl->hostname, from->name);
941 free(from_id); free(to_id); free(datakey);
945 hex2bin(datakey, datakey, keylength);
946 BF_set_key(cl->datakey, keylength, datakey);
950 if(!(to = lookup_id(to_id)))
952 syslog(LOG_ERR, _("Got ANS_KEY from %s (%s) destination %s which does not exist in our connection list"),
953 cl->name, cl->hostname, to_id);
954 free(from_id); free(to_id); free(datakey);
957 send_ans_key(from, to, datakey);
960 free(from_id); free(to_id); free(datakey);
968 Notify all my direct connections of a new host
969 that was added to the vpn, with the exception
970 of the source of the announcement.
973 int notify_others(conn_list_t *new, conn_list_t *source,
974 int (*function)(conn_list_t*, conn_list_t*))
978 for(p = conn_list; p != NULL; p = p->next)
979 if(p != new && p != source && p->status.meta && p->status.active)
986 Notify one connection of everything
990 int notify_one(conn_list_t *new)
994 for(p = conn_list; p != NULL; p = p->next)
995 if(p != new && p->status.active)
996 send_add_host(new, p);
1001 /* "Complete overhaul". */
1003 int (*request_handlers[])(conn_list_t*) = {
1004 id_h, challenge_h, chal_reply_h, ack_h,
1005 status_h, error_h, termreq_h,
1007 add_host_h, del_host_h,
1008 add_subnet_h, del_subnet_h,
1009 key_changed_h, req_key_h, ans_key_h,
1012 char (*request_name[]) = {
1013 "ID", "CHALLENGE", "CHAL_REPLY", "ACK",
1014 "STATUS", "ERROR", "TERMREQ",
1016 "ADD_HOST", "DEL_HOST",
1017 "ADD_SUBNET", "DEL_SUBNET",
1018 "KEY_CHANGED", "REQ_KEY", "ANS_KEY",