2 protocol_auth.c -- handle the meta-protocol, authentication
3 Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
23 #include "connection.h"
29 #include "meshlink_internal.h"
40 #include "ed25519/sha512.h"
44 extern bool node_write_devclass(meshlink_handle_t *mesh, node_t *n);
46 bool send_id(meshlink_handle_t *mesh, connection_t *c) {
47 return send_request(mesh, c, NULL, "%d %s %d.%d %s", ID, mesh->self->name, PROT_MAJOR, PROT_MINOR, mesh->appname);
50 bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
54 char name[MAX_STRING_SIZE];
56 if(sscanf(request, "%*d " MAX_STRING " %d.%d", name, &c->protocol_major, &c->protocol_minor) < 2) {
57 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ID", c->name);
61 /* Check if identity is a valid name */
64 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ID", c->name, "invalid name");
68 /* If this is an outgoing connection, make sure we are connected to the right host */
71 if(strcmp(c->name, name)) {
72 logger(mesh, MESHLINK_ERROR, "Peer is %s instead of %s", name, c->name);
80 c->name = xstrdup(name);
83 /* Check if version matches */
85 if(c->protocol_major != PROT_MAJOR) {
86 logger(mesh, MESHLINK_ERROR, "Peer %s uses incompatible version %d.%d",
87 c->name, c->protocol_major, c->protocol_minor);
91 /* Check if we know this node */
93 node_t *n = lookup_node(mesh, c->name);
96 logger(mesh, MESHLINK_ERROR, "Peer %s has unknown identity", c->name);
100 if(!node_read_public_key(mesh, n)) {
101 logger(mesh, MESHLINK_ERROR, "No key known for peer %s", c->name);
103 if(n->status.reachable && !n->status.waitingforkey) {
104 logger(mesh, MESHLINK_INFO, "Requesting key from peer %s", c->name);
105 send_req_key(mesh, n);
111 /* Forbid version rollback for nodes whose ECDSA key we know */
113 if(ecdsa_active(c->ecdsa) && c->protocol_minor < 2) {
114 logger(mesh, MESHLINK_ERROR, "Peer %s tries to roll back protocol version to %d.%d",
115 c->name, c->protocol_major, c->protocol_minor);
119 c->allow_request = ACK;
120 c->last_ping_time = mesh->loop.now.tv_sec;
121 char label[sizeof(meshlink_tcp_label) + strlen(mesh->self->name) + strlen(c->name) + 2];
124 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, mesh->self->name, c->name);
126 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, c->name, mesh->self->name);
129 if(mesh->log_level <= MESHLINK_DEBUG) {
130 char buf1[1024], buf2[1024];
131 bin2hex((uint8_t *)mesh->private_key + 64, buf1, 32);
132 bin2hex((uint8_t *)n->ecdsa + 64, buf2, 32);
133 logger(mesh, MESHLINK_DEBUG, "Connection to %s mykey %s hiskey %s", c->name, buf1, buf2);
136 return sptps_start(&c->sptps, c, c->outgoing, false, mesh->private_key, n->ecdsa, label, sizeof(label) - 1, send_meta_sptps, receive_meta_sptps);
139 bool send_ack(meshlink_handle_t *mesh, connection_t *c) {
140 node_t *n = lookup_node(mesh, c->name);
142 if(n && n->status.blacklisted) {
143 logger(mesh, MESHLINK_WARNING, "Peer %s is blacklisted", c->name);
144 return send_error(mesh, c, BLACKLISTED, "blacklisted");
147 c->last_ping_time = mesh->loop.now.tv_sec;
148 return send_request(mesh, c, NULL, "%d %s %d %x", ACK, mesh->myport, mesh->devclass, OPTION_PMTU_DISCOVERY | (PROT_MINOR << 24));
151 static void send_everything(meshlink_handle_t *mesh, connection_t *c) {
152 /* Send all known subnets and edges */
154 for splay_each(node_t, n, mesh->nodes) {
155 for inner_splay_each(edge_t, e, n->edge_tree) {
156 send_add_edge(mesh, c, e, 0);
161 bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
165 char hisport[MAX_STRING_SIZE];
170 if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &devclass, &options) != 3) {
171 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ACK", c->name);
175 if(devclass < 0 || devclass >= DEV_CLASS_COUNT) {
176 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ACK", c->name, "devclass invalid");
180 /* Check if we already have a node_t for him */
182 n = lookup_node(mesh, c->name);
186 n->name = xstrdup(c->name);
190 /* Oh dear, we already have a connection to this node. */
191 logger(mesh, MESHLINK_INFO, "Established a second connection with %s, closing old connection", n->connection->name);
193 if(n->connection->outgoing) {
195 logger(mesh, MESHLINK_WARNING, "Two outgoing connections to the same node!");
197 c->outgoing = n->connection->outgoing;
200 n->connection->outgoing = NULL;
203 /* Remove the edge before terminating the connection, to prevent a graph update. */
204 edge_del(mesh, n->connection->edge);
205 n->connection->edge = NULL;
207 terminate_connection(mesh, n->connection, false);
211 n->devclass = devclass;
212 n->status.dirty = true;
214 n->last_successfull_connection = mesh->loop.now.tv_sec;
220 /* Activate this connection */
222 c->allow_request = ALL;
223 c->last_key_renewal = mesh->loop.now.tv_sec;
224 c->status.active = true;
226 logger(mesh, MESHLINK_INFO, "Connection with %s activated", c->name);
228 if(mesh->meta_status_cb) {
229 mesh->meta_status_cb(mesh, (meshlink_node_t *)n, true);
232 /* Terminate any connections to this node that are not activated yet */
234 for list_each(connection_t, other, mesh->connections) {
235 if(!other->status.active && !strcmp(other->name, c->name)) {
236 if(other->outgoing) {
238 logger(mesh, MESHLINK_WARNING, "Two outgoing connections to the same node!");
240 c->outgoing = other->outgoing;
243 other->outgoing = NULL;
246 logger(mesh, MESHLINK_DEBUG, "Terminating pending second connection with %s", n->name);
247 terminate_connection(mesh, other, false);
251 /* Send him everything we know */
253 send_everything(mesh, c);
255 /* Create an edge_t for this connection */
257 assert(devclass >= 0 && devclass < DEV_CLASS_COUNT);
259 c->edge = new_edge();
260 c->edge->from = mesh->self;
262 sockaddrcpy_setport(&c->edge->address, &c->address, atoi(hisport));
263 c->edge->weight = mesh->dev_class_traits[devclass].edge_weight;
264 c->edge->connection = c;
266 node_add_recent_address(mesh, n, &c->address);
267 edge_add(mesh, c->edge);
269 /* Notify everyone of the new edge */
271 send_add_edge(mesh, mesh->everyone, c->edge, 0);
273 /* Run MST and SSSP algorithms */
277 /* Request a session key to jump start UDP traffic */
279 if(c->status.initiator) {
280 send_req_key(mesh, n);