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.31 2000/09/14 21:51:21 zarq Exp $
25 #include <sys/types.h>
30 #include <sys/socket.h>
37 #include <netinet/in.h>
39 #include <openssl/sha.h>
49 /* Generic outgoing request routine - takes care of logging and error detection as well */
51 int send_request(conn_list_t *cl, const char *format, int request, /*args*/ ...)
58 if(debug_lvl >= DEBUG_PROTOCOL)
59 syslog(LOG_DEBUG, _("Sending %s to %s (%s)"), request_name[request], cl->name, cl->hostname);
61 va_start(args, request);
62 len = vasprintf(&buffer, format, args);
65 if(len < 0 || !buffer)
67 syslog(LOG_ERR, _("Error during vasprintf(): %m"));
71 if(debug_lvl >= DEBUG_META)
72 syslog(LOG_DEBUG, _("Sending meta data to %s (%s): %s"),
73 cl->name, cl->hostname, buffer);
75 if(cl->status.encryptin)
77 /* FIXME: Do encryption */
80 if((write(cl->meta_socket, buffer, len)) < 0)
82 syslog(LOG_ERR, _("Sending meta data failed: %m"));
88 /* Connection protocol:
105 Part of the challenge is directly used to set the blowfish key and the initial vector.
106 (Twee vliegen in één klap!)
109 int send_id(conn_list_t *cl)
112 return send_request(cl, "%d %s %d %s", ID,
113 myself->name, myself->protocol_version, opt2str(myself->options));
116 int id_h(conn_list_t *cl)
121 if(sscanf(cl->buffer, "%*d %as %d %as", &cl->name, &cl->protocol_version, &options) != 3)
123 syslog(LOG_ERR, _("Got bad ID from %s"), cl->hostname);
127 /* Check if version matches */
129 if(cl->protocol_version != myself->protocol_version)
131 syslog(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"),
132 cl->name, cl->hostname, cl->protocol_version);
136 /* Check if option string is valid */
138 if((cl->options = str2opt(options)) == -1)
140 syslog(LOG_ERR, _("Peer %s uses invalid option string"), cl->hostname);
144 /* Check if identity is a valid name */
146 if(!check_id(cl->name))
148 syslog(LOG_ERR, _("Peer %s uses invalid identity name"), cl->hostname);
152 /* Load information about peer */
156 syslog(LOG_ERR, _("Peer %s had unknown identity (%s)"), cl->hostname, cl->name);
161 /* First check if the host we connected to is already in our
162 connection list. If so, we are probably making a loop, which
166 if(cl->status.outgoing)
168 if((old = lookup_id(cl->name)))
170 if(debug_lvl > DEBUG_CONNECTIONS)
171 syslog(LOG_NOTICE, _("Uplink %s (%s) is already in our connection list"), cl->name, cl->hostname);
172 cl->status.outgoing = 0;
173 old->status.outgoing = 1;
174 terminate_connection(cl);
179 /* Send a challenge to verify the identity */
181 cl->allow_request = CHAL_REPLY;
183 return send_challenge(cl);
186 int send_challenge(conn_list_t *cl)
193 free(cl->chal_answer);
195 /* Allocate buffers for the challenge and the hash */
197 cl->chal_answer = xmalloc(SHA_DIGEST_LENGTH);
198 keylength = BN_num_bytes(cl->rsakey->length);
199 buffer = xmalloc(keylength*2);
201 /* Copy random data and the public key to the buffer */
203 RAND_bytes(buffer, keylength);
204 BN_bn2bin(cl->rsakey->length, buffer+keylength);
206 /* If we don't have a blowfish key set yet, use the random data from the challenge to do so. */
208 if(!cl->status.encryptin)
210 set_metakey(cl, buffer, keylength);
213 /* Calculate the hash from that */
215 SHA1(buffer, keylength*2, cl->chal_answer);
217 /* Convert the random data to a hexadecimal formatted string */
219 bin2hex(buffer,buffer,keylength);
220 buffer[keylength*2] = '\0';
222 /* Send the challenge */
224 cl->allow_request = CHAL_REPLY;
225 x = send_request(cl, "%d %s", CHALLENGE, buffer);
227 cl->status.encryptout = 1;
232 int challenge_h(conn_list_t *cl)
238 if(sscanf(cl->buffer, "%*d %as", &cl->name, &challenge) != 1)
240 syslog(LOG_ERR, _("Got bad CHALLENGE from %s (%s)"), cl->name, cl->hostname);
244 /* Rest is done by send_chal_reply() */
246 x = send_chal_reply(cl, challenge);
252 int send_chal_reply(conn_list_t *cl, char *challenge)
260 keylength = BN_num_bytes(myself->rsakey->length);
262 /* Check if the length of the challenge is all right */
264 if(strlen(challenge) != keylength*2)
266 syslog(LOG_ERR, _("Intruder: wrong challenge length from %s (%s)"), cl->name, cl->hostname);
270 /* Allocate buffers for the challenge and the hash */
272 buffer = xmalloc(keylength*2);
273 hash = xmalloc(SHA_DIGEST_LENGTH*2+1);
275 /* Copy the incoming random data and our public key to the buffer */
277 hex2bin(challenge, buffer, keylength);
278 BN_bn2bin(myself->rsakey->length, buffer+keylength);
280 /* Calculate the hash from that */
282 SHA1(buffer, keylength*2, hash);
284 /* If we don't have a blowfish key set yet, use the random data from the challenge to do so. */
286 if(!cl->status.encrypted)
288 set_metakey(cl, buffer, keylength);
289 cl->status.encrypted = 1;
294 /* Convert the hash to a hexadecimal formatted string */
296 bin2hex(hash,hash,SHA_DIGEST_LENGTH);
297 hash[SHA_DIGEST_LENGTH*2] = '\0';
301 if(cl->status.outgoing)
302 cl->allow_request = ID;
304 cl->allow_request = ACK;
306 x = send_request(cl, "%d %s", CHAL_REPLY, hash);
312 int chal_reply_h(conn_list_t *cl)
316 if(sscanf(cl->buffer, "%*d %as", &cl->name, &hash) != 2)
318 syslog(LOG_ERR, _("Got bad CHAL_REPLY from %s (%s)"), cl->name, cl->hostname);
322 /* Check if the length of the hash is all right */
324 if(strlen(hash) != SHA_DIGEST_LENGTH*2)
326 syslog(LOG_ERR, _("Intruder: wrong challenge reply length from %s (%s)"), cl->name, cl->hostname);
330 /* Convert the hash to binary format */
332 hex2bin(hash, hash, SHA_DIGEST_LENGTH);
334 /* Verify the incoming hash with the calculated hash */
336 if(!memcmp(hash, cl->chal_answer, SHA_DIGEST_LENGTH))
338 syslog(LOG_ERR, _("Intruder: wrong challenge reply from %s (%s)"), cl->name, cl->hostname);
342 /* Identity has now been positively verified.
343 If we are accepting this new connection, then send our identity,
344 if we are making this connecting, acknowledge.
348 free(cl->chal_answer);
351 if(cl->status.outgoing)
353 cl->allow_request = ACK;
358 cl->allow_request = CHALLENGE;
363 int send_ack(conn_list_t *cl)
366 return send_request(cl, "%d", ACK);
369 int ack_h(conn_list_t *cl)
374 /* Okay, before we active the connection, we check if there is another entry
375 in the connection list with the same vpn_ip. If so, it presumably is an
376 old connection that has timed out but we don't know it yet.
379 while((old = lookup_id(cl->name)))
381 if(debug_lvl > DEBUG_CONNECTIONS)
382 syslog(LOG_NOTICE, _("Removing old entry for %s at %s in favour of new connection from %s"),
383 cl->name, old->hostname, cl->hostname);
384 old->status.active = 0;
385 terminate_connection(old);
388 /* Activate this connection */
390 cl->allow_request = ALL;
391 cl->status.active = 1;
393 if(debug_lvl > DEBUG_CONNECTIONS)
394 syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), cl->name, cl->hostname);
396 /* Exchange information about other tinc daemons */
398 notify_others(cl, NULL, send_add_host);
404 if(cl->status.outgoing)
410 /* Address and subnet information exchange */
412 int send_add_subnet(conn_list_t *cl, conn_list_t *other, subnet_t *subnet)
415 /* return send_request(cl, "%d %s %d %s", ADD_SUBNET,
416 other->name, subnet->type, net2str(subnet)); */
417 return send_request(cl, "%d %s %s", ADD_SUBNET,
418 other->name, net2str(subnet));
421 int add_subnet_h(conn_list_t *cl)
425 int send_del_subnet(conn_list_t *cl, conn_list_t *other, subnet_t *subnet)
428 return send_request(cl, "%d %s %s", DEL_SUBNET, other->name, net2str(subnet));
431 int del_subnet_h(conn_list_t *cl)
435 /* New and closed connections notification */
437 int send_add_host(conn_list_t *cl, conn_list_t *other)
440 return send_request(cl, "%d %s %lx:%d %s", ADD_HOST, other->name, other->real_ip, other->port, opt2str(other->options));
443 int add_host_h(conn_list_t *cl)
446 conn_list_t *old, *new;
448 new = new_conn_list();
450 if(sscanf(cl->buffer, "%*d %as %lx:%d %as", &new->name, &new->real_ip, &new->port, &options) != 4)
452 syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s)"), cl->name, cl->hostname);
456 /* Check if option string is valid */
458 if((new->options = str2opt(options)) == -1)
460 syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s): invalid option string"), cl->name, cl->hostname);
464 /* Check if identity is a valid name */
466 if(!check_id(new->name))
468 syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s): invalid identity name"), cl->name, cl->hostname);
472 /* Check if somebody tries to add ourself */
474 if(!strcmp(new->name, myself->name))
476 syslog(LOG_ERR, _("Warning: got ADD_HOST from %s (%s) for ourself, restarting"), cl->name, cl->hostname);
481 /* Fill in more of the new conn_list structure */
483 new->hostname = hostlookup(htonl(new->real_ip));
485 /* Check if the new host already exists in the connnection list */
487 if((old = lookup_id(new->name)))
489 if((new->real_ip == old->real_ip) && (new->port == old->port))
491 if(debug_lvl > DEBUG_CONNECTIONS)
492 syslog(LOG_NOTICE, _("Got duplicate ADD_HOST for %s (%s) from %s (%s)"),
493 old->name, old->hostname, new->name, new->hostname);
498 if(debug_lvl > DEBUG_CONNECTIONS)
499 syslog(LOG_NOTICE, _("Removing old entry for %s (%s)"),
500 old->name, old->hostname);
501 old->status.active = 0;
502 terminate_connection(old);
506 /* Fill in rest of conn_list structure */
509 new->status.active = 1;
511 /* Hook it up into the conn_list */
513 conn_list_add(conn_list, new);
515 /* Tell the rest about the new host */
517 notify_others(new, cl, send_add_host);
523 int send_del_host(conn_list_t *cl, conn_list_t *other)
526 return send_request(cl, "%d %s %lx:%d", DEL_HOST,
527 other->name, other->real_ip, other->port);
530 int del_host_h(conn_list_t *cl)
538 if(sscanf(cl->buffer, "%*d %as %lx:%d", &id, &address, &port) != 3)
540 syslog(LOG_ERR, _("Got bad DEL_HOST from %s (%s)"),
541 cl->name, cl->hostname);
545 /* Check if somebody tries to delete ourself */
547 if(!strcmp(id, myself->name))
549 syslog(LOG_ERR, _("Warning: got DEL_HOST from %s (%s) for ourself, restarting"),
550 cl->name, cl->hostname);
555 /* Check if the new host already exists in the connnection list */
557 if((old = lookup_id(id)))
559 if((address == old->real_ip) && (port == old->port))
561 notify_others(old, cl, send_del_host);
563 old->status.termreq = 1;
564 old->status.active = 0;
566 terminate_connection(old);
572 if(debug_lvl > DEBUG_CONNECTIONS)
574 syslog(LOG_NOTICE, _("Got DEL_HOST for %s from %s (%s) which is not in our connection list"),
575 id, cl->name, cl->hostname);
581 /* Status and error notification routines */
583 int send_status(conn_list_t *cl, int statusno, char *statusstring)
587 statusstring = status_text[statusno];
589 return send_request(cl, "%d %d %s", STATUS, statusno, statusstring);
592 int status_h(conn_list_t *cl)
597 if(sscanf(cl->buffer, "%*d %d %as", &statusno, &statusstring) != 2)
599 syslog(LOG_ERR, _("Got bad STATUS from %s (%s)"), cl->id, cl->hostname);
603 if(debug_lvl > DEBUG_STATUS)
605 syslog(LOG_NOTICE, _("Status message from %s (%s): %s: %s"),
606 cl->name, cl->hostname, status_text[statusno], statusstring);
614 int send_error(conn_list_t *cl, int errno, char *errstring)
618 errstring = error_text[errno];
619 return send_request(cl, "%d %d %s", ERROR, errno, errstring);
622 int error_h(conn_list_t *cl)
627 if(sscanf(cl->buffer, "%*d %d %as", &errno, &errorstring) != 2)
629 syslog(LOG_ERR, _("Got bad error from %s (%s)"),
630 cl->name, cl->hostname);
634 if(debug_lvl > DEBUG_error)
636 syslog(LOG_NOTICE, _("Error message from %s (%s): %s: %s"),
637 cl->name, cl->hostname, error_text[errno], errorstring);
641 cl->status.termreq = 1;
642 terminate_connection(cl);
647 int send_termreq(conn_list_t *cl)
650 return send_request(cl, "%d", TERMREQ);
653 int termreq_h(conn_list_t *cl)
656 cl->status.termreq = 1;
657 terminate_connection(cl);
662 /* Keepalive routines - FIXME: needs a closer look */
664 int send_ping(conn_list_t *cl)
666 cl->status.pinged = 1;
668 return send_request(cl, "%d", PING);
671 int ping_h(conn_list_t *cl)
674 return send_pong(cl);
677 int send_pong(conn_list_t *cl)
680 return send_request(cl, "%d", PONG);
683 int pong_h(conn_list_t *cl)
686 cl->status.got_pong = 1;
693 int send_key_changed(conn_list_t *from, conn_list_t *cl)
697 for(p = conn_list; p != NULL; p = p->next)
699 if(p!=cl && p->status.meta && p->status.active)
700 send_request(p, "%d %s", KEY_CHANGED,
707 int key_changed_h(conn_list_t *cl)
712 if(sscanf(cl->buffer, "%*d %as", &from_id) != 1)
714 syslog(LOG_ERR, _("Got bad KEY_CHANGED from %s (%s)"),
715 cl->name, cl->hostname);
719 if(!(from = lookup_id(from_id)))
721 syslog(LOG_ERR, _("Got KEY_CHANGED from %s (%s) origin %s which does not exist in our connection list"), cl->id, cl->hostname, from_id);
728 from->status.validkey = 0;
729 from->status.waitingforkey = 0;
731 send_key_changed(from, cl);
736 int send_req_key(conn_list_t *from, conn_list_t *to)
739 return send_request(to->nexthop, "%d %s %s", REQ_KEY, from->id, to->id);
742 int req_key_h(conn_list_t *cl)
744 char *from_id, *to_id;
745 conn_list_t *from, *to;
747 if(sscanf(cl->buffer, "%*d %as %as", &from_id, &to_id) != 2)
749 syslog(LOG_ERR, _("Got bad REQ_KEY from %s (%s)"), cl->id, cl->hostname);
753 if(!(from = lookup_id(from_id)))
755 syslog(LOG_ERR, _("Got REQ_KEY from %s (%s) origin %s which does not exist in our connection list"), cl->id, cl->hostname, from_id);
756 free(from_id); free(to_id);
760 /* Check if this key request is for us */
762 if(!strcmp(id, myself->strcmp))
764 send_ans_key(myself, from, myself->datakey);
768 if(!(to = lookup_id(to_id)))
770 syslog(LOG_ERR, _("Got REQ_KEY from %s (%s) destination %s which does not exist in our connection list"), cl->id, cl->hostname, to_id);
771 free(from_id); free(to_id);
774 send_req_key(from, to);
777 free(from_id); free(to_id);
782 int send_ans_key(conn_list_t *from, conn_list_t *to, char *datakey)
785 return send_request(to->nexthop, "%d %s %s %s", ANS_KEY, from->id, to->id, datakey);
788 int ans_key_h(conn_list_t *cl)
790 char *from_id, *to_id, *datakey;
792 conn_list_t *from, *to;
794 if(sscanf(cl->buffer, "%*d %as %as %as", &from_id, &to_id, &datakey) != 3)
796 syslog(LOG_ERR, _("Got bad ANS_KEY from %s (%s)"), cl->id, cl->hostname);
800 if(!(from = lookup_id(from_id)))
802 syslog(LOG_ERR, _("Got ANS_KEY from %s (%s) origin %s which does not exist in our connection list"), cl->id, cl->hostname, from_id);
803 free(from_id); free(to_id); free(datakey);
807 /* Check if this key request is for us */
809 if(!strcmp(id, myself->strcmp))
811 /* It is for us, convert it to binary and set the key with it. */
813 keylength = strlen(datakey);
815 if((keylength%2) || (keylength <= 0))
817 syslog(LOG_ERR, _("Got bad ANS_KEY from %s (%s) origin %s: invalid key"), cl->id, cl->hostname, from->id);
818 free(from_id); free(to_id); free(datakey);
822 hex2bin(datakey, datakey, keylength);
823 BF_set_key(cl->datakey, keylength, datakey);
827 if(!(to = lookup_id(to_id)))
829 syslog(LOG_ERR, _("Got ANS_KEY from %s (%s) destination %s which does not exist in our connection list"), cl->id, cl->hostname, to_id);
830 free(from_id); free(to_id); free(datakey);
833 send_ans_key(from, to, datakey);
836 free(from_id); free(to_id); free(datakey);
844 Notify all my direct connections of a new host
845 that was added to the vpn, with the exception
846 of the source of the announcement.
849 int notify_others(conn_list_t *new, conn_list_t *source,
850 int (*function)(conn_list_t*, conn_list_t*))
854 for(p = conn_list; p != NULL; p = p->next)
855 if(p != new && p != source && p->status.meta && p->status.active)
862 Notify one connection of everything
866 int notify_one(conn_list_t *new)
870 for(p = conn_list; p != NULL; p = p->next)
871 if(p != new && p->status.active)
872 send_add_host(new, p);
877 /* "Complete overhaul". */
879 int (*request_handlers[])(conn_list_t*) = {
880 id_h, challenge_h, chal_reply_h, ack_h,
881 status_h, error_h, termreq_h,
883 add_host_h, del_host_h,
884 add_subnet_h, del_subnet_h,
885 key_changed_h, req_key_h, ans_key_h,
888 char (*request_name[]) = {
889 "ID", "CHALLENGE", "CHAL_REPLY", "ACK",
890 "STATUS", "ERROR", "TERMREQ",
892 "ADD_HOST", "DEL_HOST",
893 "ADD_SUBNET", "DEL_SUBNET",
894 "KEY_CHANGED", "REQ_KEY", "ANS_KEY",