+static bool send_initial_sptps_data(void *handle, uint8_t type, const void *data, size_t len) {
+ node_t *to = handle;
+ to->sptps.send_data = send_sptps_data_myself;
+ char buf[len * 4 / 3 + 5];
+ b64encode(data, buf, len);
+ return send_request(to->nexthop->connection, "%d %s %s %d %s", REQ_KEY, myself->name, to->name, REQ_KEY, buf);
+}
+
+bool send_req_key(node_t *to) {
+ if(to->status.sptps) {
+ if(!node_read_ecdsa_public_key(to)) {
+ logger(DEBUG_PROTOCOL, LOG_DEBUG, "No Ed25519 key known for %s (%s)", to->name, to->hostname);
+ send_request(to->nexthop->connection, "%d %s %s %d", REQ_KEY, myself->name, to->name, REQ_PUBKEY);
+ return true;
+ }
+
+ char label[25 + strlen(myself->name) + strlen(to->name)];
+ snprintf(label, sizeof(label), "tinc UDP key expansion %s %s", myself->name, to->name);
+ sptps_stop(&to->sptps);
+ to->status.validkey = false;
+ to->status.waitingforkey = true;
+ to->last_req_key = now.tv_sec;
+ to->incompression = myself->incompression;
+ return sptps_start(&to->sptps, to, true, true, myself->connection->ecdsa, to->ecdsa, label, sizeof(label), send_initial_sptps_data, receive_sptps_record);
+ }
+
+ return send_request(to->nexthop->connection, "%d %s %s", REQ_KEY, myself->name, to->name);
+}
+
+/* REQ_KEY is overloaded to allow arbitrary requests to be routed between two nodes. */
+
+static bool req_key_ext_h(connection_t *c, const char *request, node_t *from, node_t *to, int reqno) {
+ /* If this is a SPTPS packet, see if sending UDP info helps.
+ Note that we only do this if we're the destination or the static relay;
+ otherwise every hop would initiate its own UDP info message, resulting in elevated chatter. */
+ if((reqno == REQ_KEY || reqno == SPTPS_PACKET) && to->via == myself) {
+ send_udp_info(myself, from);
+ }
+
+ if(reqno == SPTPS_PACKET) {
+ /* This is a SPTPS data packet. */
+
+ char buf[MAX_STRING_SIZE];
+ int len;
+
+ if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s) to %s (%s): %s", "SPTPS_PACKET", from->name, from->hostname, to->name, to->hostname, "invalid SPTPS data");
+ return true;
+ }
+
+ if(to != myself) {
+ /* We don't just forward the request, because we want to use UDP if it's available. */
+ if(forwarding_mode == FMODE_INTERNAL) {
+ send_sptps_data(to, from, 0, buf, len);
+ try_tx(to, true);
+ }
+ } else {
+ /* The packet is for us */
+ if(!sptps_receive_data(&from->sptps, buf, len)) {
+ /* Uh-oh. It might be that the tunnel is stuck in some corrupted state,
+ so let's restart SPTPS in case that helps. But don't do that too often
+ to prevent storms. */
+ if(from->last_req_key < now.tv_sec - 10) {
+ logger(DEBUG_PROTOCOL, LOG_ERR, "Failed to decode TCP packet from %s (%s), restarting SPTPS", from->name, from->hostname);
+ send_req_key(from);
+ }
+
+ return true;
+ }
+
+ send_mtu_info(myself, from, MTU);
+ }
+
+ return true;
+ }
+
+ /* Requests that are not SPTPS data packets are forwarded as-is. */
+
+ if(to != myself) {
+ return send_request(to->nexthop->connection, "%s", request);
+ }
+
+ /* The request is for us */
+
+ switch(reqno) {
+ case REQ_PUBKEY: {
+ if(!node_read_ecdsa_public_key(from)) {
+ /* Request their key *before* we send our key back. Otherwise the first SPTPS packet from them will get dropped. */
+ logger(DEBUG_PROTOCOL, LOG_DEBUG, "Preemptively requesting Ed25519 key for %s (%s)", from->name, from->hostname);
+ send_request(from->nexthop->connection, "%d %s %s %d", REQ_KEY, myself->name, from->name, REQ_PUBKEY);
+ }
+
+ char *pubkey = ecdsa_get_base64_public_key(myself->connection->ecdsa);
+ send_request(from->nexthop->connection, "%d %s %s %d %s", REQ_KEY, myself->name, from->name, ANS_PUBKEY, pubkey);
+ free(pubkey);
+ return true;
+ }
+
+ case ANS_PUBKEY: {
+ if(node_read_ecdsa_public_key(from)) {
+ logger(DEBUG_PROTOCOL, LOG_WARNING, "Got ANS_PUBKEY from %s (%s) even though we already have his pubkey", from->name, from->hostname);
+ return true;
+ }
+
+ char pubkey[MAX_STRING_SIZE];
+
+ if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, pubkey) != 1 || !(from->ecdsa = ecdsa_set_base64_public_key(pubkey))) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_PUBKEY", from->name, from->hostname, "invalid pubkey");
+ return true;
+ }
+
+ logger(DEBUG_PROTOCOL, LOG_INFO, "Learned Ed25519 public key from %s (%s)", from->name, from->hostname);
+ append_config_file(from->name, "Ed25519PublicKey", pubkey);
+ return true;
+ }
+
+ case REQ_KEY: {
+ if(!node_read_ecdsa_public_key(from)) {
+ logger(DEBUG_PROTOCOL, LOG_DEBUG, "No Ed25519 key known for %s (%s)", from->name, from->hostname);
+ send_request(from->nexthop->connection, "%d %s %s %d", REQ_KEY, myself->name, from->name, REQ_PUBKEY);
+ return true;
+ }
+
+ if(from->sptps.label) {
+ logger(DEBUG_ALWAYS, LOG_DEBUG, "Got REQ_KEY from %s while we already started a SPTPS session!", from->name);
+ }
+
+ char buf[MAX_STRING_SIZE];
+ int len;
+
+ if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_SPTPS_START", from->name, from->hostname, "invalid SPTPS data");
+ return true;
+ }
+
+ char label[25 + strlen(from->name) + strlen(myself->name)];
+ snprintf(label, sizeof(label), "tinc UDP key expansion %s %s", from->name, myself->name);
+ sptps_stop(&from->sptps);
+ from->status.validkey = false;
+ from->status.waitingforkey = true;
+ from->last_req_key = now.tv_sec;
+ sptps_start(&from->sptps, from, false, true, myself->connection->ecdsa, from->ecdsa, label, sizeof(label), send_sptps_data_myself, receive_sptps_record);
+ sptps_receive_data(&from->sptps, buf, len);
+ send_mtu_info(myself, from, MTU);
+ return true;
+ }
+
+ default:
+ logger(DEBUG_ALWAYS, LOG_ERR, "Unknown extended REQ_KEY request from %s (%s): %s", from->name, from->hostname, request);
+ return true;
+ }
+}
+
+bool req_key_h(connection_t *c, const char *request) {
+ char from_name[MAX_STRING_SIZE];
+ char to_name[MAX_STRING_SIZE];
+ node_t *from, *to;
+ int reqno = 0;
+
+ if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING " %d", from_name, to_name, &reqno) < 2) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "REQ_KEY", c->name,
+ c->hostname);
+ return false;
+ }
+
+ if(!check_id(from_name) || !check_id(to_name)) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_KEY", c->name, c->hostname, "invalid name");
+ return false;
+ }
+
+ from = lookup_node(from_name);
+
+ if(!from) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
+ "REQ_KEY", c->name, c->hostname, from_name);
+ return true;
+ }
+
+ to = lookup_node(to_name);
+
+ if(!to) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
+ "REQ_KEY", c->name, c->hostname, to_name);
+ return true;
+ }