]> git.meshlink.io Git - meshlink/blob - src/protocol_key.c
Don't send UDP probes to tiny nodes.
[meshlink] / src / protocol_key.c
1 /*
2     protocol_key.c -- handle the meta-protocol, key exchange
3     Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "connection.h"
23 #include "logger.h"
24 #include "meshlink_internal.h"
25 #include "net.h"
26 #include "netutl.h"
27 #include "node.h"
28 #include "prf.h"
29 #include "pmtu.h"
30 #include "protocol.h"
31 #include "sptps.h"
32 #include "utils.h"
33 #include "xalloc.h"
34
35 static const int req_key_timeout = 2;
36
37 bool key_changed_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
38         assert(request);
39         assert(*request);
40
41         char name[MAX_STRING_SIZE];
42         node_t *n;
43
44         if(sscanf(request, "%*d %*x " MAX_STRING, name) != 1) {
45                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "KEY_CHANGED", c->name);
46                 return false;
47         }
48
49         if(seen_request(mesh, request)) {
50                 return true;
51         }
52
53         n = lookup_node(mesh, name);
54
55         if(!n) {
56                 logger(mesh, MESHLINK_ERROR, "Got %s from %s origin %s which does not exist", "KEY_CHANGED", c->name, name);
57                 return true;
58         }
59
60         /* Tell the others */
61
62         forward_request(mesh, c, NULL, request);
63
64         return true;
65 }
66
67 static bool send_initial_sptps_data(void *handle, uint8_t type, const void *data, size_t len) {
68         (void)type;
69
70         assert(data);
71         assert(len);
72
73         node_t *to = handle;
74         meshlink_handle_t *mesh = to->mesh;
75
76         if(!to->nexthop || !to->nexthop->connection) {
77                 logger(mesh, MESHLINK_WARNING, "Cannot send SPTPS data to %s via %s", to->name, to->nexthop ? to->nexthop->name : to->name);
78                 return false;
79         }
80
81         to->sptps.send_data = send_sptps_data;
82         char buf[len * 4 / 3 + 5];
83         b64encode(data, buf, len);
84         return send_request(mesh, to->nexthop->connection, NULL, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_KEY, buf);
85 }
86
87 bool send_canonical_address(meshlink_handle_t *mesh, node_t *to) {
88         if(!mesh->self->canonical_address) {
89                 return true;
90         }
91
92         return send_request(mesh, to->nexthop->connection, NULL, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_CANONICAL, mesh->self->canonical_address);
93 }
94
95 bool send_req_key(meshlink_handle_t *mesh, node_t *to) {
96         if(!node_read_public_key(mesh, to)) {
97                 logger(mesh, MESHLINK_DEBUG, "No ECDSA key known for %s", to->name);
98
99                 if(!to->nexthop || !to->nexthop->connection) {
100                         logger(mesh, MESHLINK_WARNING, "Cannot send REQ_PUBKEY to %s via %s", to->name, to->nexthop ? to->nexthop->name : to->name);
101                         return true;
102                 }
103
104                 char *pubkey = ecdsa_get_base64_public_key(mesh->private_key);
105                 send_request(mesh, to->nexthop->connection, NULL, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_PUBKEY, pubkey);
106                 free(pubkey);
107                 return true;
108         }
109
110         if(to->sptps.label) {
111                 logger(mesh, MESHLINK_DEBUG, "send_req_key(%s) called while sptps->label != NULL!", to->name);
112         }
113
114         /* Send our canonical address to help with UDP hole punching */
115         send_canonical_address(mesh, to);
116
117         char label[sizeof(meshlink_udp_label) + strlen(mesh->self->name) + strlen(to->name) + 2];
118         snprintf(label, sizeof(label), "%s %s %s", meshlink_udp_label, mesh->self->name, to->name);
119         sptps_stop(&to->sptps);
120         to->status.validkey = false;
121         to->status.waitingforkey = true;
122         to->last_req_key = mesh->loop.now.tv_sec;
123         return sptps_start(&to->sptps, to, true, true, mesh->private_key, to->ecdsa, label, sizeof(label) - 1, send_initial_sptps_data, receive_sptps_record);
124 }
125
126 /* REQ_KEY is overloaded to allow arbitrary requests to be routed between two nodes. */
127
128 static bool req_key_ext_h(meshlink_handle_t *mesh, connection_t *c, const char *request, node_t *from, int reqno) {
129         (void)c;
130
131         if(!from->nexthop || !from->nexthop->connection) {
132                 logger(mesh, MESHLINK_WARNING, "Cannot answer REQ_KEY from %s via %s", from->name, from->nexthop ? from->nexthop->name : from->name);
133                 return true;
134         }
135
136         switch(reqno) {
137         case REQ_PUBKEY: {
138                 char *pubkey = ecdsa_get_base64_public_key(mesh->private_key);
139
140                 if(!node_read_public_key(mesh, from)) {
141                         char hiskey[MAX_STRING_SIZE];
142
143                         if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, hiskey) == 1) {
144                                 from->ecdsa = ecdsa_set_base64_public_key(hiskey);
145
146                                 if(!from->ecdsa) {
147                                         logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_PUBKEY", from->name, "invalid pubkey");
148                                         return true;
149                                 }
150
151                                 logger(mesh, MESHLINK_INFO, "Learned ECDSA public key from %s", from->name);
152                                 from->status.dirty = true;
153
154                                 if(!node_write_config(mesh, from, true)) {
155                                         // ignore
156                                 }
157                         }
158                 }
159
160                 send_request(mesh, from->nexthop->connection, NULL, "%d %s %s %d %s", REQ_KEY, mesh->self->name, from->name, ANS_PUBKEY, pubkey);
161                 free(pubkey);
162                 return true;
163         }
164
165         case ANS_PUBKEY: {
166                 if(node_read_public_key(mesh, from)) {
167                         logger(mesh, MESHLINK_WARNING, "Got ANS_PUBKEY from %s even though we already have his pubkey", from->name);
168                         return true;
169                 }
170
171                 char pubkey[MAX_STRING_SIZE];
172
173                 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, pubkey) != 1 || !(from->ecdsa = ecdsa_set_base64_public_key(pubkey))) {
174                         logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ANS_PUBKEY", from->name, "invalid pubkey");
175                         return true;
176                 }
177
178                 logger(mesh, MESHLINK_INFO, "Learned ECDSA public key from %s", from->name);
179                 from->status.dirty = true;
180
181                 if(!node_write_config(mesh, from, true)) {
182                         // ignore
183                 }
184
185                 /* If we are trying to form an outgoing connection to this node, retry immediately */
186                 for list_each(outgoing_t, outgoing, mesh->outgoings) {
187                         if(outgoing->node == from && outgoing->ev.cb) {
188                                 outgoing->timeout = 0;
189                                 timeout_set(&mesh->loop, &outgoing->ev, &(struct timespec) {
190                                         0, 0
191                                 });
192                         }
193                 }
194
195                 /* Also reset any UTCP timers */
196                 utcp_reset_timers(from->utcp);
197
198                 return true;
199         }
200
201         case REQ_KEY: {
202                 if(!node_read_public_key(mesh, from)) {
203                         logger(mesh, MESHLINK_DEBUG, "No ECDSA key known for %s", from->name);
204                         send_request(mesh, from->nexthop->connection, NULL, "%d %s %s %d", REQ_KEY, mesh->self->name, from->name, REQ_PUBKEY);
205                         return true;
206                 }
207
208                 if(from->sptps.label) {
209                         logger(mesh, MESHLINK_DEBUG, "Got REQ_KEY from %s while we already started a SPTPS session!", from->name);
210
211                         if(mesh->loop.now.tv_sec < from->last_req_key + req_key_timeout && strcmp(mesh->self->name, from->name) < 0) {
212                                 logger(mesh, MESHLINK_DEBUG, "Ignoring REQ_KEY from %s.", from->name);
213                                 return true;
214                         }
215                 }
216
217                 char buf[MAX_STRING_SIZE];
218                 int len;
219
220                 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
221                         logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_SPTPS_START", from->name, "invalid SPTPS data");
222                         return true;
223                 }
224
225                 char label[sizeof(meshlink_udp_label) + strlen(from->name) + strlen(mesh->self->name) + 2];
226                 snprintf(label, sizeof(label), "%s %s %s", meshlink_udp_label, from->name, mesh->self->name);
227                 sptps_stop(&from->sptps);
228                 from->status.validkey = false;
229                 from->status.waitingforkey = true;
230                 from->last_req_key = mesh->loop.now.tv_sec;
231
232                 /* Send our canonical address to help with UDP hole punching */
233                 send_canonical_address(mesh, from);
234
235                 if(!sptps_start(&from->sptps, from, false, true, mesh->private_key, from->ecdsa, label, sizeof(label) - 1, send_sptps_data, receive_sptps_record)) {
236                         logger(mesh, MESHLINK_ERROR, "Could not start SPTPS session with %s: %s", from->name, strerror(errno));
237                         return true;
238                 }
239
240                 if(!sptps_receive_data(&from->sptps, buf, len)) {
241                         logger(mesh, MESHLINK_ERROR, "Could not process SPTPS data from %s: %s", from->name, strerror(errno));
242                         return true;
243                 }
244
245                 return true;
246         }
247
248         case REQ_SPTPS: {
249                 if(!from->status.validkey) {
250                         logger(mesh, MESHLINK_ERROR, "Got REQ_SPTPS from %s but we don't have a valid key yet", from->name);
251                         return true;
252                 }
253
254                 char buf[MAX_STRING_SIZE];
255                 int len;
256
257                 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
258                         logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_SPTPS", from->name, "invalid SPTPS data");
259                         return true;
260                 }
261
262                 if(!sptps_receive_data(&from->sptps, buf, len)) {
263                         logger(mesh, MESHLINK_ERROR, "Could not process SPTPS data from %s: %s", from->name, strerror(errno));
264                         return true;
265                 }
266
267                 return true;
268         }
269
270         case REQ_CANONICAL: {
271                 char host[MAX_STRING_SIZE];
272                 char port[MAX_STRING_SIZE];
273
274                 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING " " MAX_STRING, host, port) != 2) {
275                         logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_CANONICAL", from->name, "invalid canonical address");
276                         return true;
277                 }
278
279                 char *canonical_address;
280                 xasprintf(&canonical_address, "%s %s", host, port);
281
282                 if(mesh->log_level <= MESHLINK_DEBUG && (!from->canonical_address || strcmp(from->canonical_address, canonical_address))) {
283                         logger(mesh, MESHLINK_DEBUG, "Updating canonical address of %s to %s", from->name, canonical_address);
284                 }
285
286                 free(from->canonical_address);
287                 from->canonical_address = canonical_address;
288                 return true;
289         }
290
291         default:
292                 logger(mesh, MESHLINK_ERROR, "Unknown extended REQ_KEY request from %s: %s", from->name, request);
293                 return true;
294         }
295 }
296
297 bool req_key_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
298         assert(request);
299         assert(*request);
300
301         char from_name[MAX_STRING_SIZE];
302         char to_name[MAX_STRING_SIZE];
303         node_t *from, *to;
304         int reqno = 0;
305
306         if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING " %d", from_name, to_name, &reqno) < 2) {
307                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "REQ_KEY", c->name);
308                 return false;
309         }
310
311         if(!check_id(from_name) || !check_id(to_name)) {
312                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_KEY", c->name, "invalid name");
313                 return false;
314         }
315
316         from = lookup_node(mesh, from_name);
317
318         if(!from) {
319                 logger(mesh, MESHLINK_ERROR, "Got %s from %s origin %s which does not exist in our connection list",
320                        "REQ_KEY", c->name, from_name);
321                 return true;
322         }
323
324         to = lookup_node(mesh, to_name);
325
326         if(!to) {
327                 logger(mesh, MESHLINK_ERROR, "Got %s from %s destination %s which does not exist in our connection list",
328                        "REQ_KEY", c->name, to_name);
329                 return true;
330         }
331
332         /* Check if this key request is for us */
333
334         if(to == mesh->self) {                      /* Yes */
335                 /* Is this an extended REQ_KEY message? */
336                 if(reqno) {
337                         return req_key_ext_h(mesh, c, request, from, reqno);
338                 }
339
340                 /* This should never happen. Ignore it, unless it came directly from the connected peer, in which case we disconnect. */
341                 return from->connection != c;
342         } else {
343                 if(!to->status.reachable || !to->nexthop || !to->nexthop->connection) {
344                         logger(mesh, MESHLINK_WARNING, "Got %s from %s destination %s which is not reachable",
345                                "REQ_KEY", c->name, to_name);
346                         return true;
347                 }
348
349                 size_t len = strlen(request);
350                 from->in_forward += len + SPTPS_OVERHEAD;
351                 to->out_forward += len + SPTPS_OVERHEAD;
352
353                 send_request(mesh, to->nexthop->connection, NULL, "%s", request);
354         }
355
356         return true;
357 }
358
359 bool ans_key_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
360         assert(request);
361         assert(*request);
362
363         char from_name[MAX_STRING_SIZE];
364         char to_name[MAX_STRING_SIZE];
365         char key[MAX_STRING_SIZE];
366         char address[MAX_STRING_SIZE] = "";
367         char port[MAX_STRING_SIZE] = "";
368         int cipher, digest, maclength, compression;
369         node_t *from, *to;
370
371         if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
372                         from_name, to_name, key, &cipher, &digest, &maclength,
373                         &compression, address, port) < 7) {
374                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ANS_KEY", c->name);
375                 return false;
376         }
377
378         if(!check_id(from_name) || !check_id(to_name)) {
379                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ANS_KEY", c->name, "invalid name");
380                 return false;
381         }
382
383         from = lookup_node(mesh, from_name);
384
385         if(!from) {
386                 logger(mesh, MESHLINK_ERROR, "Got %s from %s origin %s which does not exist in our connection list",
387                        "ANS_KEY", c->name, from_name);
388                 return true;
389         }
390
391         to = lookup_node(mesh, to_name);
392
393         if(!to) {
394                 logger(mesh, MESHLINK_ERROR, "Got %s from %s destination %s which does not exist in our connection list",
395                        "ANS_KEY", c->name, to_name);
396                 return true;
397         }
398
399         /* Forward it if necessary */
400
401         if(to != mesh->self) {
402                 if(!to->status.reachable) {
403                         logger(mesh, MESHLINK_WARNING, "Got %s from %s destination %s which is not reachable",
404                                "ANS_KEY", c->name, to_name);
405                         return true;
406                 }
407
408                 if(from == to) {
409                         logger(mesh, MESHLINK_WARNING, "Got %s from %s from %s to %s",
410                                "ANS_KEY", c->name, from_name, to_name);
411                         return true;
412                 }
413
414                 if(!to->nexthop || !to->nexthop->connection) {
415                         logger(mesh, MESHLINK_WARNING, "Cannot forward ANS_KEY to %s via %s", to->name, to->nexthop ? to->nexthop->name : to->name);
416                         return false;
417                 }
418
419                 /* TODO: find a good way to avoid the use of strlen() */
420                 size_t len = strlen(request);
421                 from->in_forward += len + SPTPS_OVERHEAD;
422                 to->out_forward += len + SPTPS_OVERHEAD;
423
424                 /* Append the known UDP address of the from node, if we have a confirmed one */
425                 if(!*address && from->status.udp_confirmed && from->address.sa.sa_family != AF_UNSPEC) {
426                         char *reflexive_address, *reflexive_port;
427                         logger(mesh, MESHLINK_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
428                         sockaddr2str(&from->address, &reflexive_address, &reflexive_port);
429                         send_request(mesh, to->nexthop->connection, NULL, "%s %s %s", request, reflexive_address, reflexive_port);
430                         free(reflexive_address);
431                         free(reflexive_port);
432                         return true;
433                 }
434
435                 return send_request(mesh, to->nexthop->connection, NULL, "%s", request);
436         }
437
438         /* Is this an ANS_KEY informing us of our own reflexive UDP address? */
439
440         if(from == mesh->self) {
441                 if(*key == '.' && *address && *port) {
442                         logger(mesh, MESHLINK_DEBUG, "Learned our own reflexive UDP address from %s: %s port %s", c->name, address, port);
443
444                         /* Inform all other nodes we want to communicate with and which are reachable via this connection */
445                         for splay_each(node_t, n, mesh->nodes) {
446                                 if(n->nexthop != c->node) {
447                                         continue;
448                                 }
449
450                                 if(n->status.udp_confirmed) {
451                                         continue;
452                                 }
453
454                                 if(!n->status.waitingforkey && !n->status.validkey) {
455                                         continue;
456                                 }
457
458                                 if(!n->nexthop->connection) {
459                                         continue;
460                                 }
461
462                                 logger(mesh, MESHLINK_DEBUG, "Forwarding our own reflexive UDP address to %s", n->name);
463                                 send_request(mesh, c, NULL, "%d %s %s . -1 -1 -1 0 %s %s", ANS_KEY, mesh->self->name, n->name, address, port);
464                         }
465                 } else {
466                         logger(mesh, MESHLINK_WARNING, "Got %s from %s from %s to %s",
467                                "ANS_KEY", c->name, from_name, to_name);
468                 }
469
470                 return true;
471         }
472
473         /* Process SPTPS data if present */
474
475         if(*key != '.') {
476                 /* Don't use key material until every check has passed. */
477                 from->status.validkey = false;
478
479                 /* Compression is not supported. */
480                 if(compression != 0) {
481                         logger(mesh, MESHLINK_ERROR, "Node %s uses bogus compression level!", from->name);
482                         return true;
483                 }
484
485                 char buf[strlen(key)];
486                 int len = b64decode(key, buf, strlen(key));
487
488                 if(!len || !sptps_receive_data(&from->sptps, buf, len)) {
489                         logger(mesh, MESHLINK_ERROR, "Error processing SPTPS data from %s", from->name);
490                 }
491         }
492
493         if(from->status.validkey) {
494                 if(*address && *port) {
495                         logger(mesh, MESHLINK_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
496                         sockaddr_t sa = str2sockaddr(address, port);
497                         update_node_udp(mesh, from, &sa);
498                 }
499
500                 keepalive(mesh, from, true);
501         }
502
503         return true;
504 }