-
Notifications
You must be signed in to change notification settings - Fork 293
add autonat v2 spec #638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add autonat v2 spec #638
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,148 +1,7 @@ | ||
| # NAT Discovery <!-- omit in toc --> | ||
| > How we detect if we're behind a NAT. | ||
|
|
||
| | Lifecycle Stage | Maturity | Status | Latest Revision | | ||
| |-----------------|----------------|--------|-----------------| | ||
| | 3A | Recommendation | Active | r1, 2023-02-16 | | ||
|
|
||
|
|
||
| Authors: [@marten-seemann] | ||
|
|
||
| Interest Group: [@mxinden], [@vyzo], [@raulk], [@stebalien], [@willscott] | ||
|
|
||
| [@marten-seemann]: https://github.com/marten-seemann | ||
| [@mxinden]: https://github.com/mxinden/ | ||
| [@vyzo]: https://github.com/vyzo | ||
| [@raulk]: https://github.com/raulk | ||
| [@stebalien]: https://github.com/stebalien | ||
| [@willscott]: https://github.com/willscott | ||
|
|
||
| See the [lifecycle document][lifecycle-spec] for context about the maturity level | ||
| and spec status. | ||
|
|
||
| [lifecycle-spec]: https://github.com/libp2p/specs/blob/master/00-framework-01-spec-lifecycle.md | ||
|
|
||
| ## Table of Contents <!-- omit in toc --> | ||
|
|
||
| - [Overview](#overview) | ||
| - [AutoNAT Protocol](#autonat-protocol) | ||
| - [RPC messages](#rpc-messages) | ||
| - [Security Considerations](#security-considerations) | ||
|
|
||
| ## Overview | ||
|
|
||
| A priori, a node cannot know if it is behind a NAT / firewall or if it is | ||
| publicly reachable. Knowing its NAT status is essential for the node to be | ||
| well-behaved in the network: A node that's behind a NAT doesn't need to | ||
| advertise its (undiable) addresses to the rest of the network, preventing | ||
| superfluous dials from other peers. Furthermore, it might actively seek to | ||
| improve its connectivity by finding a relay server, which would allow other | ||
| peers to establish a relayed connection. | ||
|
|
||
| To determine if it is located behind a NAT, nodes use the `autonat` protocol. | ||
| Using this protocol, the node requests other peers to dial its presumed public | ||
| addresses. If a couple of these dial attempts succeed, the node can be reasonably | ||
| certain that it is not located behind a NAT. Likewise, if a couple of these dial | ||
| attempts fail, this is a strong indicator that a NAT is blocking incoming | ||
| connections. | ||
|
|
||
| ## AutoNAT Protocol | ||
|
|
||
| The AutoNAT Protocol uses the Protocol ID `/libp2p/autonat/1.0.0`. The node | ||
| wishing to determine its NAT status opens a stream using this protocol ID, and | ||
| then sends a `Dial` message. The `Dial` message contains a list of multiaddresses. | ||
| Upon receiving this message, the peer starts to dial these addresses. It MAY | ||
| add the observed address of the connection on which the request was received to | ||
| the list of addresses. It MAY dial the addresses in parallel. | ||
| The peer MAY also use a different IP and peer ID than | ||
| it uses for its regular libp2p connection to perform these dial backs. | ||
|
|
||
| In order to prevent attacks like the one described in [RFC 3489, Section | ||
| 12.1.1](https://www.rfc-editor.org/rfc/rfc3489#section-12.1.1) (see excerpt | ||
| below), implementations MUST NOT dial any multiaddress unless it is based on the | ||
| IP address the requesting node is observed as. This restriction as well implies that | ||
| implementations MUST NOT accept dial requests via relayed connections as one can | ||
| not validate the IP address of the requesting node. | ||
|
|
||
| > RFC 3489 12.1.1 Attack I: DDOS Against a Target | ||
| > | ||
| > In this case, the attacker provides a large number of clients with the same | ||
| > faked MAPPED-ADDRESS that points to the intended target. This will trick all | ||
| > the STUN clients into thinking that their addresses are equal to that of the | ||
| > target. The clients then hand out that address in order to receive traffic on | ||
| > it (for example, in SIP or H.323 messages). However, all of that traffic | ||
| > becomes focused at the intended target. The attack can provide substantial | ||
| > amplification, especially when used with clients that are using STUN to enable | ||
| > multimedia applications. | ||
|
|
||
| If all dials fail, the receiver sends a `DialResponse` message with the | ||
| `ResponseStatus` `E_DIAL_ERROR`. If at least one of the dials complete | ||
| successfully, it sends a `DialResponse` with the `ResponseStatus` `OK`. It | ||
| SHOULD include the address it successfully dialed in its response. | ||
|
|
||
| The initiator uses the responses obtained from multiple peers to determine its | ||
| NAT status. If more than 3 peers report a successfully dialed address, the node | ||
| SHOULD assume that it is not located behind a NAT and publicly accessible. On | ||
| the other hand, if more than 3 peers report unsuccessful dials, the node SHOULD | ||
| assume that it is not publicly accessible. | ||
| Nodes are encouraged to periodically re-check their status, especially after | ||
| changing their set of addresses they're listening on. | ||
|
|
||
| ## RPC messages | ||
|
|
||
| Messages are exchanged by: | ||
|
|
||
| 1. Opening a new stream. | ||
| 2. Sending the RPC request message. | ||
| 3. Listening for the RPC response message. | ||
|
|
||
| All RPC messages sent over a stream are prefixed with the message length in | ||
| bytes, encoded as an unsigned variable length integer as defined by the | ||
| [multiformats unsigned-varint spec][uvarint-spec]. | ||
|
|
||
| ```proto | ||
| syntax = "proto2"; | ||
|
|
||
| message Message { | ||
| enum MessageType { | ||
| DIAL = 0; | ||
| DIAL_RESPONSE = 1; | ||
| } | ||
|
|
||
| enum ResponseStatus { | ||
| OK = 0; | ||
| E_DIAL_ERROR = 100; | ||
| E_DIAL_REFUSED = 101; | ||
| E_BAD_REQUEST = 200; | ||
| E_INTERNAL_ERROR = 300; | ||
| } | ||
|
|
||
| message PeerInfo { | ||
| optional bytes id = 1; | ||
| repeated bytes addrs = 2; | ||
| } | ||
|
|
||
| message Dial { | ||
| optional PeerInfo peer = 1; | ||
| } | ||
|
|
||
| message DialResponse { | ||
| optional ResponseStatus status = 1; | ||
| optional string statusText = 2; | ||
| optional bytes addr = 3; | ||
| } | ||
|
|
||
| optional MessageType type = 1; | ||
| optional Dial dial = 2; | ||
| optional DialResponse dialResponse = 3; | ||
| } | ||
| ``` | ||
|
|
||
| ## Security Considerations | ||
|
|
||
| Note that in the current iteration of this protocol, a node doesn't check if | ||
| a peer's report of a successful dial is accurate. This might be solved in a | ||
| future iteration of this protocol, see | ||
| https://github.com/libp2p/go-libp2p-autonat/issues/10 for a detailed discussion. | ||
|
|
||
| [uvarint-spec]: https://github.com/multiformats/unsigned-varint | ||
| Specifications: | ||
| - [autonat v1](autonat-v1.md) | ||
| - [autonat v2](autonat-v2.md) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| | Lifecycle Stage | Maturity | Status | Latest Revision | | ||
| |-----------------|----------------|--------|-----------------| | ||
| | 3A | Recommendation | Active | r1, 2023-02-16 | | ||
|
|
||
|
|
||
| Authors: [@marten-seemann] | ||
|
|
||
| Interest Group: [@mxinden], [@vyzo], [@raulk], [@stebalien], [@willscott] | ||
|
|
||
| [@marten-seemann]: https://github.com/marten-seemann | ||
| [@mxinden]: https://github.com/mxinden/ | ||
| [@vyzo]: https://github.com/vyzo | ||
| [@raulk]: https://github.com/raulk | ||
| [@stebalien]: https://github.com/stebalien | ||
| [@willscott]: https://github.com/willscott | ||
|
|
||
| See the [lifecycle document][lifecycle-spec] for context about the maturity level | ||
| and spec status. | ||
|
|
||
| [lifecycle-spec]: https://github.com/libp2p/specs/blob/master/00-framework-01-spec-lifecycle.md | ||
|
|
||
| ## Table of Contents <!-- omit in toc --> | ||
|
|
||
| - [Overview](#overview) | ||
| - [AutoNAT Protocol](#autonat-protocol) | ||
| - [RPC messages](#rpc-messages) | ||
| - [Security Considerations](#security-considerations) | ||
|
|
||
| ## Overview | ||
|
|
||
| A priori, a node cannot know if it is behind a NAT / firewall or if it is | ||
| publicly reachable. Knowing its NAT status is essential for the node to be | ||
| well-behaved in the network: A node that's behind a NAT doesn't need to | ||
| advertise its (undiable) addresses to the rest of the network, preventing | ||
| superfluous dials from other peers. Furthermore, it might actively seek to | ||
| improve its connectivity by finding a relay server, which would allow other | ||
| peers to establish a relayed connection. | ||
|
|
||
| To determine if it is located behind a NAT, nodes use the `autonat` protocol. | ||
| Using this protocol, the node requests other peers to dial its presumed public | ||
| addresses. If a couple of these dial attempts succeed, the node can be reasonably | ||
| certain that it is not located behind a NAT. Likewise, if a couple of these dial | ||
| attempts fail, this is a strong indicator that a NAT is blocking incoming | ||
| connections. | ||
|
|
||
| ## AutoNAT Protocol | ||
|
|
||
| The AutoNAT Protocol uses the Protocol ID `/libp2p/autonat/1.0.0`. The node | ||
| wishing to determine its NAT status opens a stream using this protocol ID, and | ||
| then sends a `Dial` message. The `Dial` message contains a list of multiaddresses. | ||
| Upon receiving this message, the peer starts to dial these addresses. It MAY | ||
| add the observed address of the connection on which the request was received to | ||
| the list of addresses. It MAY dial the addresses in parallel. | ||
| The peer MAY also use a different IP and peer ID than | ||
| it uses for its regular libp2p connection to perform these dial backs. | ||
|
|
||
| In order to prevent attacks like the one described in [RFC 3489, Section | ||
| 12.1.1](https://www.rfc-editor.org/rfc/rfc3489#section-12.1.1) (see excerpt | ||
| below), implementations MUST NOT dial any multiaddress unless it is based on the | ||
| IP address the requesting node is observed as. This restriction as well implies that | ||
| implementations MUST NOT accept dial requests via relayed connections as one can | ||
| not validate the IP address of the requesting node. | ||
|
|
||
| > RFC 3489 12.1.1 Attack I: DDOS Against a Target | ||
| > | ||
| > In this case, the attacker provides a large number of clients with the same | ||
| > faked MAPPED-ADDRESS that points to the intended target. This will trick all | ||
| > the STUN clients into thinking that their addresses are equal to that of the | ||
| > target. The clients then hand out that address in order to receive traffic on | ||
| > it (for example, in SIP or H.323 messages). However, all of that traffic | ||
| > becomes focused at the intended target. The attack can provide substantial | ||
| > amplification, especially when used with clients that are using STUN to enable | ||
| > multimedia applications. | ||
|
|
||
| If all dials fail, the receiver sends a `DialResponse` message with the | ||
| `ResponseStatus` `E_DIAL_ERROR`. If at least one of the dials complete | ||
| successfully, it sends a `DialResponse` with the `ResponseStatus` `OK`. It | ||
| SHOULD include the address it successfully dialed in its response. | ||
|
|
||
| The initiator uses the responses obtained from multiple peers to determine its | ||
| NAT status. If more than 3 peers report a successfully dialed address, the node | ||
| SHOULD assume that it is not located behind a NAT and publicly accessible. On | ||
| the other hand, if more than 3 peers report unsuccessful dials, the node SHOULD | ||
| assume that it is not publicly accessible. | ||
| Nodes are encouraged to periodically re-check their status, especially after | ||
| changing their set of addresses they're listening on. | ||
|
|
||
| ## RPC messages | ||
|
|
||
| Messages are exchanged by: | ||
|
|
||
| 1. Opening a new stream. | ||
| 2. Sending the RPC request message. | ||
| 3. Listening for the RPC response message. | ||
|
|
||
| All RPC messages sent over a stream are prefixed with the message length in | ||
| bytes, encoded as an unsigned variable length integer as defined by the | ||
| [multiformats unsigned-varint spec][uvarint-spec]. | ||
|
|
||
| ```proto | ||
| syntax = "proto2"; | ||
|
|
||
| message Message { | ||
| enum MessageType { | ||
| DIAL = 0; | ||
| DIAL_RESPONSE = 1; | ||
| } | ||
|
|
||
| enum ResponseStatus { | ||
| OK = 0; | ||
| E_DIAL_ERROR = 100; | ||
| E_DIAL_REFUSED = 101; | ||
| E_BAD_REQUEST = 200; | ||
| E_INTERNAL_ERROR = 300; | ||
| } | ||
|
|
||
| message PeerInfo { | ||
| optional bytes id = 1; | ||
| repeated bytes addrs = 2; | ||
| } | ||
|
|
||
| message Dial { | ||
| optional PeerInfo peer = 1; | ||
| } | ||
|
|
||
| message DialResponse { | ||
| optional ResponseStatus status = 1; | ||
| optional string statusText = 2; | ||
| optional bytes addr = 3; | ||
| } | ||
|
|
||
| optional MessageType type = 1; | ||
| optional Dial dial = 2; | ||
| optional DialResponse dialResponse = 3; | ||
| } | ||
| ``` | ||
|
|
||
| ## Security Considerations | ||
|
|
||
| Note that in the current iteration of this protocol, a node doesn't check if | ||
| a peer's report of a successful dial is accurate. This might be solved in a | ||
| future iteration of this protocol, see | ||
| https://github.com/libp2p/go-libp2p-autonat/issues/10 for a detailed discussion. | ||
|
|
||
| [uvarint-spec]: https://github.com/multiformats/unsigned-varint |
16 changes: 16 additions & 0 deletions
16
autonat/autonat-v2-amplification-attack-prevention.plantuml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| @startuml | ||
| participant Cli | ||
| participant Srv | ||
|
|
||
| skinparam sequenceMessageAlign center | ||
| skinparam defaultFontName monospaced | ||
|
|
||
|
|
||
| == Amplification Attack Prevention == | ||
|
|
||
| Cli -> Srv: [conn1: stream: dial] DialRequest:{nonce: 0xabcd, addrs: (addr1, addr2, addr3)} | ||
| Srv -> Cli: [conn1: stream: dial] DialDataRequest:{addrIdx: 1, numBytes: 120k} | ||
| Cli -> Srv: [conn1: stream: dial] DialDataResponse:{data: 4k bytes},DialDataResponse:{data: 4k bytes},... | ||
| Srv -> Cli: [conn2: stream: dial-back]addr2 DialBack:{nonce: 0xabcd} | ||
| Srv -> Cli: [conn1: stream: dial] DialResponse:{status: OK, addrIdx: 1, dialStatus: DIAL_STATUS_OK} | ||
| @enduml | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very small nit, but can we 0-index these addrs too?
addr0, addr1, addr2