2 protocol_key.c -- handle the meta-protocol, key exchange
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2010 Guus Sliepen <guus@tinc-vpn.org>
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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "splay_tree.h"
25 #include "connection.h"
35 static bool mykeyused = false;
37 void send_key_changed() {
41 send_request(broadcast, "%d %x %s", KEY_CHANGED, rand(), myself->name);
43 /* Immediately send new keys to directly connected nodes to keep UDP mappings alive */
45 for(node = connection_tree->head; node; node = node->next) {
47 if(c->status.active && c->node && c->node->status.reachable)
48 send_ans_key(c->node);
52 bool key_changed_h(connection_t *c, char *request) {
53 char name[MAX_STRING_SIZE];
56 if(sscanf(request, "%*d %*x " MAX_STRING, name) != 1) {
57 logger(LOG_ERR, "Got bad %s from %s (%s)", "KEY_CHANGED",
58 c->name, c->hostname);
62 if(seen_request(request))
65 n = lookup_node(name);
68 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
69 "KEY_CHANGED", c->name, c->hostname, name);
73 n->status.validkey = false;
79 forward_request(c, request);
84 bool send_req_key(node_t *to) {
85 return send_request(to->nexthop->connection, "%d %s %s", REQ_KEY, myself->name, to->name);
88 bool req_key_h(connection_t *c, char *request) {
89 char from_name[MAX_STRING_SIZE];
90 char to_name[MAX_STRING_SIZE];
93 if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 2) {
94 logger(LOG_ERR, "Got bad %s from %s (%s)", "REQ_KEY", c->name,
99 if(!check_id(from_name) || !check_id(to_name)) {
100 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_KEY", c->name, c->hostname, "invalid name");
104 from = lookup_node(from_name);
107 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
108 "REQ_KEY", c->name, c->hostname, from_name);
112 to = lookup_node(to_name);
115 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
116 "REQ_KEY", c->name, c->hostname, to_name);
120 /* Check if this key request is for us */
122 if(to == myself) { /* Yes, send our own key back */
129 if(!to->status.reachable) {
130 logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
131 "REQ_KEY", c->name, c->hostname, to_name);
135 send_request(to->nexthop->connection, "%s", request);
141 bool send_ans_key(node_t *to) {
142 size_t keylen = cipher_keylength(&myself->incipher);
143 char key[keylen * 2 + 1];
145 cipher_open_by_nid(&to->incipher, cipher_get_nid(&myself->incipher));
146 digest_open_by_nid(&to->indigest, digest_get_nid(&myself->indigest), digest_length(&myself->indigest));
147 to->incompression = myself->incompression;
149 randomize(key, keylen);
150 cipher_set_key(&to->incipher, key, true);
151 digest_set_key(&to->indigest, key, keylen);
153 bin2hex(key, key, keylen);
154 key[keylen * 2] = '\0';
156 // Reset sequence number and late packet window
158 to->received_seqno = 0;
159 if(replaywin) memset(to->late, 0, replaywin);
161 return send_request(to->nexthop->connection, "%d %s %s %s %d %d %zu %d", ANS_KEY,
162 myself->name, to->name, key,
163 cipher_get_nid(&to->incipher),
164 digest_get_nid(&to->indigest),
165 digest_length(&to->indigest),
169 bool ans_key_h(connection_t *c, char *request) {
170 char from_name[MAX_STRING_SIZE];
171 char to_name[MAX_STRING_SIZE];
172 char key[MAX_STRING_SIZE];
173 char address[MAX_STRING_SIZE] = "";
174 char port[MAX_STRING_SIZE] = "";
175 int cipher, digest, maclength, compression, keylen;
178 if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
179 from_name, to_name, key, &cipher, &digest, &maclength,
180 &compression, address, port) < 7) {
181 logger(LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
186 if(!check_id(from_name) || !check_id(to_name)) {
187 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
191 from = lookup_node(from_name);
194 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
195 "ANS_KEY", c->name, c->hostname, from_name);
199 to = lookup_node(to_name);
202 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
203 "ANS_KEY", c->name, c->hostname, to_name);
207 /* Forward it if necessary */
213 if(!to->status.reachable) {
214 logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
215 "ANS_KEY", c->name, c->hostname, to_name);
219 if(!*address && from->address.sa.sa_family != AF_UNSPEC) {
220 char *address, *port;
221 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
222 sockaddr2str(&from->address, &address, &port);
223 send_request(to->nexthop->connection, "%s %s %s", request, address, port);
229 return send_request(to->nexthop->connection, "%s", request);
232 /* Check and lookup cipher and digest algorithms */
234 if(!cipher_open_by_nid(&from->outcipher, cipher)) {
235 logger(LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name, from->hostname);
239 keylen = strlen(key) / 2;
241 if(keylen != cipher_keylength(&from->outcipher)) {
242 logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname);
246 if(!digest_open_by_nid(&from->outdigest, digest, maclength)) {
247 logger(LOG_ERR, "Node %s (%s) uses unknown digest!", from->name, from->hostname);
251 if(maclength != digest_length(&from->outdigest)) {
252 logger(LOG_ERR, "Node %s (%s) uses bogus MAC length!", from->name, from->hostname);
256 if(compression < 0 || compression > 11) {
257 logger(LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
261 from->outcompression = compression;
263 /* Update our copy of the origin's packet key */
265 hex2bin(key, key, keylen);
266 cipher_set_key(&from->outcipher, key, false);
267 digest_set_key(&from->outdigest, key, keylen);
269 from->status.validkey = true;
270 from->sent_seqno = 0;
272 if(*address && *port) {
273 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
274 sockaddr_t sa = str2sockaddr(address, port);
275 update_node_udp(from, &sa);
278 if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuprobes)
279 send_mtu_probe(from);