summaryrefslogtreecommitdiff
path: root/doc/dev/notes/relay-sketch.md
blob: 352c5b45b70c8a7eeed1ceede503f1db1e55b90b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# First steps and initial design for Arti relay work

(April 2024)

First, I'll sketch what we should do
in order to get arti relay work started.

Then, I'll try to sketch out some initial answers
to some of the design issues that arise during the startup plan.


## Planning the work

### What do we do first?

I think the first phase of our relay development
should focus on getting a minimally functional relay
that can participate in the Tor protocols.

Once that's done, we can add more features,
improve performance,
hunt bugs,
and check other things off our list of deliverables.

With that in mind, here's what we need to build
to get a relay implementation
that can function on a testing network.

- DESIGN: We need a sketch of our [high-level code layout](#high-level),
  including what the major pieces are,
  which modules own [which objects](#proto-objects),
  and how it all generally looks.
  This doesn't need to be final.

- We need to support the relay variants of
  the channel establishment handshake.
  See <https://spec.torproject.org/tor-spec/negotiating-channels.html>.

  - This will require us to extend our `TlsProvider` API
    in `tor-rtcompat`
    so that it allows key material exporters.

    - DESIGN: How does the API for this work?
      Do we continue to allow the nativetls crate,
      which does not expose key material exporters?

- Extend ChanMgr to handle relay-style channels.
  - Ability to launch connections
  - Ability to manage incoming connections, including those with no
    identities
  - Soon after:
    - Ability to discard long-unused connections according to relay rules
    - Ability to de-duplicate connections according to relay rules
    - Ability to select _best_ connection among several with same ID?

- Code to listen for incoming OR connections.
  - DESIGN: [Where does this live](#high-level)?

- Support for incoming CREATE2 cells on channels.
  - DESIGN: Is this the same Channel type or a new type?

- A new `RelayCirc` type, crated by CREATE2 cells.
  - DESIGN: [How much code](#proto-objects) can this share internally
    with ClientCirc?
    The reactor logic is very similar,
    but the API is quite different.
  - Needs to accept cells, encrypt/decrypt, and re-transmit.
    - We'll want to refactor all of our "cells moving around" logic
      during the relay time, but we can do so more slowly.
  - Need to handle more types of command cells than currently
    handled.

- Support for EXTENDED2 cells on RelayCircuits

- Exit logic for circuits.
  - Including exit policy support.
  - DESIGN: best practices for DNS and DNS caching.

- Support for generating and publishing relay descriptors.
  - DESIGN: Can/should this share any logic
    with publishing HS descriptors?

- Key management for relays.

Once all of the above is done,
we should be able run a mixed test network.
(We will still need C relays to be the directory caches,
and C directory authorities.)
Performance will be poor.


### What can we work on when we're blocked on the above?

If for some reason we get blocked doing one of the steps above,
we can spend our time on some other stuff
that won't be useful until later.

- Directory authority low-level operation
- Directory cache logic
- Designs for KIST+EWMA etc.
- Ensuring that we don't block important threads
  on expensive public-key crypto.

## Designing an Arti relay

NOTE:
None of the design here is complete or final!
The goal is to get us enough direction
so that we can start working.


### Design question: How do our high level crates work?

<a name="high-level"></a>

I propose the following:

- `arti` remains the top-level entry point.

- There is a new experimental `relay` feature.

- `arti` runs as a relay or a proxy; not both.
  (Running this way has created _tons_ of problems
  in C tor.)

- There is a new `arti-relay` crate underneath `arti`
  but above `arti-client`.
  It is responsible for launching and orchestrating
  our other modules, in the same way `arti-client` is.

  - It defines a `TorRelay` type.

- The `arti` crate still handles all the incoming TCP connections.
  It passes incoming OR connections to the TorRelay crate.

### Design: How do we handle channels, circuits, and streams?

<a name="proto-objects"></a>

Relay channels still use the `Channel` type.
There is some API to use when constructing a channel,
to tell the channel to negotiate using the relay protocol,
and to accept incoming circuits.

Relay channels are owned by the same `ChanMgr` object
as clients use.  We add new features to `ChanMgr` as needed.

Relay circuits use a new `RelayCirc` type,
since their API is radically different from `ClientCirc`.
Ideally, `RelayCirc` and `ClientCirc`
share much of their backend and reactor code.
(If this is feasible.)
But we have to do this in such a way that
we do not risk exposing one variety's features
as if they were the other's.

The `RelayCirc` type should not be owned by `CircMgr`,
since that module is extremely specialized
for launching multihop client circuits for particular needs.
Instead, `RelayCircs` are kept alive by the channels
that they're on.
There's probably a `RelayCircMgr` type somewhere
to handle enumerating or killing circuits
if we need to do so.

> NOTE: Conceivably there should be a single reactor for every
> RelayCirc on a given channel, or something like that.
> If so, the shared reactor logic above is probably not what we need.

Relay streams can continue to use `DataStream`,
in the same way that onion service streams do today.
We'll need an `exitproxy` implementation
sort of like our current `hsproxy` code.