New peer-to-peer (DHT-based) discovery protocol

Background

I’m proposing a peer-to-peer / DHT-based discovery method. With this I hope to accomplish reduced reliance on any central party, preventing tracking and control by any central party, and a higher level of scalability for the future.

Goals & constraints

In addition to the general goals in the background above, there are some specific technical constraints we need to fulfil in order for the mechanism to be maximally useful.

  1. We want to prevent forgery of discovery information; that is, I should not be able to publish discovery data for a given device without effectively being that device.
  2. We want to prevent denial-of-service for any given device; that is, I should not be able to prevent lookups for a given device, nor block lookups by a given device.
  3. We want to prevent tracking; I should not be able to see which other devices a device communicates with just by way of being part of the discovery network.

Equally, there are a few constraints or goals that could have been useful that I have rejected, at least for the time being:

  1. Knowing a device ID enables tracking of that device by way of doing lookups for it. This is the case today, and forms the conceptual basis of what discovery does. It would be possible to achieve a higher level of privacy by encrypting discovery records specifically for known recipients only, but this would imply an n-fold increase in load on the discovery network and may also conflict with constraint #1 above (forgery prevention) or otherwise publish more information for tracking purposes (not only “I exist” but “I exist, and peer with this other device”).
  2. Participating in the discovery network necessarily enables tracking a subset of other devices who are part of it.
    • Primarily, since a participating device can get announcements from other devices it can see their external IP address and device ID. Note that this is independent of whether the announcements themselves are encrypted etc – by seeing the IP address, we can always connect back with a portscan and in most cases get an open port and see the device ID. This is how Syncthing works.
    • Additionally, participating devices will serve lookup requests for other devices they have been selected as storage nodes for, and partially serve “find node” requests for lookups of devices they are not storage nodes for. We use temporary lookup keys that are not directly convertible to a device ID, but once you know a lookup key exists you can perform the corresponding lookup yourself and deduce the device ID from the result. You may then also be able to portscan the querying device and get a device ID, which tell you device A is looking for device B.

I’m attempting to mitigate #2 above by having lookup keys rotate every 24 hours, thus switching out the subset of devices directly involved in the discovery of any other device. This prevents passive, long-term tracking of devices, while leaving #1 as a remaining tracking avenue.

See “other considered designs” and “anticipated objections” below for further thoughts on the topic.

Design & specification

I’m basing the implementation on libp2p’s implementation of the Kademlia network, as used in for example IPFS. Syncthing uses separate ports, protocols, etc and does not share any peers with IPFS or any other preexisting network.

The basic principle is that each device produces a discovery record, which contains its address information and public TLS certificate, signs it using the TLS private key, and publishes it under a key derived from the device ID and time of day.

  • The address information is the purpose of discovery.
  • The certificate is required to verify the signature.
  • The signature proves that the record was generated by the publishing device.

The lookup key is created by hashing a seed, the device ID, and the time of day added to the device ID in seconds and truncated to the current day. This creates a random key that changes every 24 hours, at an offset into the day that is fixed based on the device ID.

Announcements are published to the set of DHT nodes selected by this lookup key using the algorithm in Kademlia. Each such node verifies that the record is properly signed and that it’s published under the lookup key that matches the device ID in question.

Lookups are done by calculating the lookup key for the target device ID and then doing the lookup in the Kademlia network.

The message format is protobuf, schema here.

There is a separate binary stdht the runs just a DHT node, primarily for bootstrap purposes.

Kademlia uses Ed25519 keys to identify nodes. These need to be stable for the network to be stable, but are an additional tracking avenue. I set Syncthing nodes to rotate the key every seven days as a compromise. Bootstrap nodes are of course stable.

I’ve started a set of bootstrap nodes on the same infra that runs global discovery and everything else, and Syncthing knows to find these as TXT records on dht.syncthing.net. We can add and remove these as required in the future, including maybe allowing community contributions.

For context, I plan for the current global discovery servers to be phased out over time once this is out there. Initially, in that new Syncthing instances will cache which devices were successfully looked-up over DHT and then stop querying global discovery for them. In the long run, by disabling it by default in clients entirely. Maybe, at that point, the load remaining from old clients is low enough that it’s no longer a concern and they can live forever. Maybe it isn’t, and there will be a flag day at some point where I say “upgrade, get off global discovery, run your own, whatever, the current servers are shutting down”. We’ll see.

Alternative designs

Encrypt the discovery information

We can encrypt the discovery information using a key derived from the device ID. That makes it opaque to other nodes that don’t already know the device ID. However:

  • The nodes receiving the announcement can anyway in many cases find the device ID by reverse port scan and then decrypt the record.
  • Non-evil nodes that don’t know the device ID now cannot verify that the record actually belongs to the lookup key, so this enables DoS via announcement forgery.

Encrypt the discovery information, using an ephemeral key pair

We can do the above, but encrypt and sign the announcement using an ephemeral Ed25519 key. The storage nodes can then validate the announcement versus that key and prevent forgery. However, we need to distribute that key to other devices that need to use it for lookups. We could do that by publishing it under a device ID-derived key, but again that cannot be validated by other nodes since there’s nothing to bind a given key to a device ID.

This partially solves the DoS problem by moving the forgery vulnerability it from the discovery record to the ephemeral key, which could be cached by nodes who already know the device in question.

The price is that now need to do key management, key rotation, caching, are partially open to DoS against lookups, and increase the conceptual complexity by a lot.

Encrypt the discovery information, for the recipient only

This has many of the same problems as the above, although DoS is harder since you need to target a specific pair of devices rather than just one of them. Additionally, each device now needs to publish n records for n peers rather than just one, significantly increasing the load on the network as a whole.

Anticipated objections

“The tracking prevention above is insufficient, I’m better off trusting a single central third party”

I think this is a valid trade-off, but the trusted central third party is tired of the cost and operational expense of running global discovery for a continually growing network of devices, many of them presumably in commercial use.

There will be options for the peer-to-peer-adverse, including running their own global discovery server, using DNS, using a private DHT.

“I don’t want the resource consumption / battery drain / network usage of other peoples discovery”

Currently, I’ve intentionally not added a config to be just a passive participant and not become a server node when possible; with the exception that Android and iOS devices are always in client mode, as I don’t think they’re particularly useful as storage nodes anyway. My reasoning is that it’s essential to get the node count up initially, and that reaping the benefits of the community should imply participating by default.

For not using peer-to-peer at all, see above.

Future improvements

Tickets will be filed etc, but some things I’ve skipped in the initial implementation that we’ll surely need to add down the line:

  • Better way to run a closed/private DHT. Currently you can just set the “bootstrap nodes” config to something that points to a node of your own instead of default, but there’s nothing to prevent a random misconfigured client from joining the default DHT, leaking the other nodes, and thus joining the networks. We probably need to use separate, configurable, protocol names, etc.
  • Probably lots and lots of configurables for various aspects of the DHT. Currently I want to get something off the ground with reasonable defaults, and optimise later.