A middle relay is the most boring job in the Tor network, and that's a compliment. It receives encrypted cells from one relay and passes them to another. It never sees a client IP address and never sees a destination. Running one is about as uneventful as hosting goes.

Exit relays are where things get interesting. They're the point where Tor traffic turns back into ordinary internet traffic, so every website on the far end sees the exit's IP address, not yours. Which means every abuse complaint, every automated block, every confused sysadmin also lands on the exit's operator.

What the middle does

In a three-hop circuit the middle relay sits between your guard and the exit. It knows the guard's address and the exit's address. It doesn't know which client is behind the guard, and it can't read the cells because they're still wrapped in the exit's encryption layer. The point of having it at all is to stop the guard and the exit from knowing about each other, which is explained in onion routing, layer by layer.

There's no "Middle" flag in the consensus. Any relay that is Running and Valid can be a middle. Guards and exits get used as middles too, though less often, because the bandwidth weights try to save scarce guard and exit capacity for those positions. Since Tor 0.4.7 the authorities can also assign a MiddleOnly flag to relays they're suspicious of but haven't proven malicious: such a relay can still carry traffic but won't be used as a guard, exit, or in sensitive onion service positions.

What the exit does

When your client wants to open a TCP connection, it sends a RELAY_BEGIN cell to the exit containing a hostname (or IP) and a port. The exit checks its own exit policy, resolves the name using its system resolver, connects, and sends back RELAY_CONNECTED. After that it shuttles bytes between the TCP connection and the circuit.

Because the exit performs DNS resolution, exit operators are told to run their own local caching resolver (unbound is the usual choice) rather than relying on a big public resolver. Otherwise a single DNS provider ends up seeing the hostnames for a large share of Tor exit traffic, which is bad for everyone. The Tor Project has been banging this drum for years, and the advice in their exit relay setup guide is good.

Exit policies

An exit policy is an ordered list of accept and reject rules in the relay's torrc. The first rule that matches a destination wins. Rules look like this:

ExitRelay 1
ExitPolicy reject *:25
ExitPolicy accept *:80
ExitPolicy accept *:443
ExitPolicy reject *:*

That relay will connect to the web on ports 80 and 443 and refuse everything else. Rejecting port 25 is near-universal, because open SMTP through an anonymity network is a spam cannon. Tor rejects outbound SMTP in its default exit policy anyway.

A few details that catch people out:

  • Since 0.3.5, a relay is not an exit unless you set ExitRelay 1 or write an explicit ExitPolicy. Older guides that assume the opposite are out of date.
  • ExitPolicyRejectPrivate is on by default and blocks private address ranges and the relay's own addresses, so clients can't use your exit to poke at your LAN.
  • There's a ReducedExitPolicy 1 shortcut that accepts a curated list of common ports (web, mail retrieval, SSH, IRC, XMPP and so on) and rejects most file-sharing ports. It's a reasonable middle ground for a first exit.
  • IPv6 exit needs IPv6Exit 1; IPv6 rules use addresses in square brackets, or accept6/reject6.

How clients learn the policy

Clients don't download the full exit policy. The microdescriptor for each relay carries a compact summary, a single line listing accepted (or rejected) ports:

p accept 22,53,80,443,993,995

That's a made-up example for a small exit; a ReducedExitPolicy relay advertises a much longer list of ranges. The summary ignores address-specific rules, so occasionally a client picks an exit that turns out to reject the specific destination. The exit replies with an END cell giving the reason, and the client retries elsewhere. You don't notice it in practice.

The Exit flag itself is narrower than "allows some exiting". The authorities give it only to relays that allow exits to at least two of ports 80, 443 and 6667 and to a reasonable chunk of address space. A relay that only allows, say, port 22 still works as an exit for SSH, but it's not counted as an Exit for bandwidth weighting purposes.

BadExit

When the Tor Project's bad-relays team finds an exit tampering with traffic (stripping TLS, injecting content, rewriting cryptocurrency addresses), the directory authorities mark it BadExit and clients stop using it as an exit. If the evidence is strong they reject it from the network entirely. Scanners for this kind of behaviour run regularly, and there's more on the history of these incidents in Sybil attacks and malicious relays.

The practical side of running an exit

I won't pretend this is easy. Running an exit means abuse emails, mostly automated and sometimes several a day on a busy exit. It means your IP ends up on blocklists, which is part of why Tor users see so many CAPTCHAs and blocks. It can mean a visit from police in some countries, though in most places the legal position of exit operators as conduits is reasonably well understood. Don't run an exit from your home connection. Talk to your hosting provider first, set a reverse DNS name like tor-exit.example.org, and put up the Tor Project's exit notice page on port 80 so anyone investigating can see what the IP is.

Many operators prefer to join or donate to an established exit organisation instead, which spreads the legal and administrative load. If you'd rather just contribute bandwidth without the email, run a middle relay. The steps are in Running a Tor Relay, and the network needs both kinds.

#exit-relays #middle-relays #exit-policy #torrc