| 1 | # Levin Protocol |
| 2 | This is a document explaining the current design of the Levin protocol, as |
| 3 | used by Monero. The protocol is largely inherited from cryptonote, but has |
| 4 | undergone some changes. |
| 5 | |
| 6 | This document also may differ from the `struct bucket_head2` in Monero's |
| 7 | code slightly - the spec here is slightly more strict to allow for |
| 8 | extensibility. |
| 9 | |
| 10 | One of the goals of this document is to clearly indicate what is being sent |
| 11 | "on the wire" to identify metadata that could de-anonymize users over I2P/Tor. |
| 12 | These issues will be addressed as they are found. See `ANONYMITY_NETWORKS.md` in |
| 13 | the `docs` folder for any outstanding issues. |
| 14 | |
| 15 | > This document does not currently list all data being sent by the monero |
| 16 | > protocol, that portion is a work-in-progress. Please take the time to do it |
| 17 | > if interested in learning about Monero p2p traffic! |
| 18 | |
| 19 | |
| 20 | ## Header |
| 21 | This header is sent for every Monero p2p message. |
| 22 | |
| 23 | ``` |
| 24 | 0 1 2 3 |
| 25 | 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 |
| 26 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 27 | | 0x01 | 0x21 | 0x01 | 0x01 | |
| 28 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 29 | | 0x01 | 0x01 | 0x01 | 0x01 | |
| 30 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 31 | | Length | |
| 32 | | | |
| 33 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 34 | | E. Response | Command |
| 35 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 36 | | Return Code |
| 37 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 38 | |Q|S|B|E| Reserved |
| 39 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 40 | | 0x01 | 0x00 | 0x00 | |
| 41 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 42 | | 0x00 | |
| 43 | +-+-+-+-+-+-+-+-+ |
| 44 | ``` |
| 45 | |
| 46 | ### Signature |
| 47 | The first 8 bytes are the "signature" which helps identify the protocol (in |
| 48 | case someone connected to the wrong port, etc). The comments indicate that byte |
| 49 | sequence is from "benders nightmare". |
| 50 | |
| 51 | This also can be used by deep packet inspection (DPI) engines to identify |
| 52 | Monero when the link is not encrypted. SSL has been proposed as a means to |
| 53 | mitigate this issue, but BIP-151 or the Noise protocol should also be considered. |
| 54 | |
| 55 | ### Length |
| 56 | The length is an unsigned 64-bit little endian integer. The length does _not_ |
| 57 | include the header. |
| 58 | |
| 59 | The implementation currently rejects received messages that exceed 100 MB |
| 60 | (base 10) by default. |
| 61 | |
| 62 | ### Expect Response |
| 63 | A zero-byte if no response is expected from the peer, and a non-zero byte if a |
| 64 | response is expected from the peer. Peers must respond to requests with this |
| 65 | flag in the same order that they were received, however, other messages can be |
| 66 | sent between responses. |
| 67 | |
| 68 | There are some commands in the |
| 69 | [cryptonote protocol](#cryptonote-protocol-commands) where a response is |
| 70 | expected from the peer, but this flag is not set. Those responses are returned |
| 71 | as notify messages and can be sent in any order by the peer. |
| 72 | |
| 73 | ### Command |
| 74 | An unsigned 32-bit little endian integer representing the Monero specific |
| 75 | command being invoked. |
| 76 | |
| 77 | ### Return Code |
| 78 | A signed 32-bit little endian integer representing the response from the peer |
| 79 | from the last command that was invoked. This is `0` for request messages. |
| 80 | |
| 81 | ### Flags |
| 82 | * `Q` - Bit is set if the message is a request. |
| 83 | * `S` - Bit is set if the message is a response. |
| 84 | * `B` - Bit is set if this is a the beginning of a [fragmented message](#fragmented-messages). |
| 85 | * `E` - Bit is set if this is the end of a [fragmented message](#fragmented-messages). |
| 86 | |
| 87 | ### Version |
| 88 | A fixed value of `1` as an unsigned 32-bit little endian integer. |
| 89 | |
| 90 | |
| 91 | ## Message Flow |
| 92 | The protocol can be subdivided into: (1) notifications, (2) requests, |
| 93 | (3) responses, (4) fragmented messages, and (5) dummy messages. Response |
| 94 | messages must be sent in the same order that a peer issued a request message. |
| 95 | A peer does not have to send a response immediately following a request - any |
| 96 | other message type can be sent instead. |
| 97 | |
| 98 | ### Notifications |
| 99 | Notifications are one-way messages that can be sent at any time without |
| 100 | an expectation of a response from the peer. The `Q` bit must be set, the `S`, |
| 101 | `B` and `E` bits must be unset, and the `Expect Response` field must be zeroed. |
| 102 | |
| 103 | Some notifications must be in response to other notifications. This is not |
| 104 | part of the levin messaging layer, and is described in the |
| 105 | [commands](#commands) section. |
| 106 | |
| 107 | ### Requests |
| 108 | Requests are the basis of the admin protocol for Monero. The `Q` bit must be |
| 109 | set, the `S`, `B` and `E` bits must be unset, and the `Expect Response` field |
| 110 | must be non-zero. The peer is expected to send a response message with the same |
| 111 | `command` number. |
| 112 | |
| 113 | ### Responses |
| 114 | Response message can only be sent after a peer first issues a request message. |
| 115 | Responses must have the `S` bit set, the `Q`, `B` and `E` bits unset, and have |
| 116 | a zeroed `Expect Response` field. The `Command` field must be the same value |
| 117 | that was sent in the request message. The `Return Code` is specific to the |
| 118 | `Command` being issued (see [commands])(#commands)). |
| 119 | |
| 120 | ### Fragmented |
| 121 | Fragmented messages were introduced for the "white noise" feature for i2p/tor. |
| 122 | A transaction can be sent in fragments to conceal when "real" data is being |
| 123 | sent instead of dummy messages. Only one fragmented message can be sent at a |
| 124 | time, and bits `B` and `E` are never set at the same time |
| 125 | (see [dummy messages](#dummy)). The re-constructed message must contain a |
| 126 | levin header for a different (non-fragment) message type. |
| 127 | |
| 128 | The `Q` and `S` bits are never set and the `Expect Response` field must always |
| 129 | be zero. The first fragment has the `B` bit set, neither `B` nor `E` is set for |
| 130 | "middle" fragments, and `E` is set for the last fragment. |
| 131 | |
| 132 | ### Dummy |
| 133 | Dummy messages have the `B` and `E` bits set, the `Q` and `S` bits unset, and |
| 134 | the `Expect Response` field zeroed. When a message of this type is received, the |
| 135 | contents can be safely ignored. |
| 136 | |
| 137 | |
| 138 | ## Commands |
| 139 | ### P2P (Admin) Commands |
| 140 | |
| 141 | #### (`1001` Request) Handshake |
| 142 | #### (`1001` Response) Handshake |
| 143 | #### (`1002` Request) Timed Sync |
| 144 | #### (`1002` Response) Timed Sync |
| 145 | #### (`1003` Request) Ping |
| 146 | #### (`1003` Response) Ping |
| 147 | #### (`1004` Request) Stat Info |
| 148 | #### (`1004` Response) Stat Info |
| 149 | #### (`1005` Request) Network State |
| 150 | #### (`1005` Response) Network State |
| 151 | #### (`1006` Request) Peer ID |
| 152 | #### (`1006` Response) Peer ID |
| 153 | #### (`1007` Request) Support Flags |
| 154 | #### (`1007` Response) Support Flags |
| 155 | |
| 156 | ### Cryptonote Protocol Commands |
| 157 | |
| 158 | #### (`2001` Notification) New Block |
| 159 | #### (`2002` Notification) New Transactions |
| 160 | #### (`2003` Notification) Request Get Objects |
| 161 | #### (`2004` Notification) Response Get Objects |
| 162 | #### (`2006` Notification) Request Chain |
| 163 | #### (`2007` Notification) Response Chain Entry |
| 164 | #### (`2008` Notification) New Fluffy Block |
| 165 | #### (`2009` Notification) Request Fluffy Missing TX |