scproto.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**
  2. * @file scproto.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Definitions for SCProto, the protocol that the clients communicate in
  32. * with the server.
  33. *
  34. * All multi-byte integers in structs are little-endian, unless stated otherwise.
  35. *
  36. * A SCProto packet consists of:
  37. * - a header (struct {@link sc_header}) which contains the type of the
  38. * packet
  39. * - the payload
  40. *
  41. * It goes roughly like that:
  42. *
  43. * When the client connects to the server, it sends a "clienthello" packet
  44. * to the server. The packet contains the protocol version the client is using.
  45. * When the server receives the "clienthello" packet, it checks the version.
  46. * If it doesn't match, it disconnects the client. Otherwise the server sends
  47. * the client a "serverhello" packet to the client. That packet contains
  48. * the ID of the client and possibly its IPv4 address as the server sees it
  49. * (zero if not applicable).
  50. *
  51. * The server than proceeds to synchronize the peers' knowledge of each other.
  52. * It does that by sending a "newclient" messages to a client to inform it of
  53. * another peer, and "endclient" messages to inform it that a peer is gone.
  54. * Each client, upon receiving a "newclient" message, MUST sent a corresponding
  55. * "acceptpeer" message, before sending any messages to the new peer.
  56. * The server forwards messages between synchronized peers to allow them to
  57. * communicate. A peer sends a message to another peer by sending the "outmsg"
  58. * packet to the server, and the server delivers a message to a peer by sending
  59. * it the "inmsg" packet.
  60. *
  61. * The message service is reliable; messages from one client to another are
  62. * expected to arrive unmodified and in the same order. There is, however,
  63. * no flow control. This means that messages can not be used for bulk transfers
  64. * between the clients (and they are not). If the server runs out of buffer for
  65. * messages from one client to another, it will stop forwarding messages, and
  66. * will reset knowledge of the two clients after some delay. Similarly, if one
  67. * of the clients runs out of buffer locally, it will send the "resetpeer"
  68. * packet to make the server reset knowledge.
  69. *
  70. * The messages transport either:
  71. *
  72. * - If the relevant "newclient" packets do not contain the
  73. * SCID_NEWCLIENT_FLAG_SSL flag, then plaintext MsgProto messages.
  74. *
  75. * - If the relevant "newclient" packets do contain the SCID_NEWCLIENT_FLAG_SSL
  76. * flag, then SSL, broken down into packets, PacketProto inside SSL, and finally
  77. * MsgProto inside PacketProto. The master peer (one with higher ID) acts as an
  78. * SSL server, and the other acts as an SSL client. The peers must identify with
  79. * the same certificate they used when connecting to the server, and each peer
  80. * must byte-compare the other's certificate agains the one provided to it by
  81. * by the server in the relevent "newclient" message.
  82. */
  83. #ifndef BADVPN_PROTOCOL_SCPROTO_H
  84. #define BADVPN_PROTOCOL_SCPROTO_H
  85. #include <stdint.h>
  86. #include <misc/packed.h>
  87. #define SC_VERSION 29
  88. #define SC_OLDVERSION_NOSSL 27
  89. #define SC_OLDVERSION_BROKENCERT 26
  90. #define SC_KEEPALIVE_INTERVAL 10000
  91. /**
  92. * SCProto packet header.
  93. * Follows up to SC_MAX_PAYLOAD bytes of payload.
  94. */
  95. B_START_PACKED
  96. struct sc_header {
  97. /**
  98. * Message type.
  99. */
  100. uint8_t type;
  101. } B_PACKED;
  102. B_END_PACKED
  103. #define SC_MAX_PAYLOAD 2000
  104. #define SC_MAX_ENC (sizeof(struct sc_header) + SC_MAX_PAYLOAD)
  105. typedef uint16_t peerid_t;
  106. #define SCID_KEEPALIVE 0
  107. #define SCID_CLIENTHELLO 1
  108. #define SCID_SERVERHELLO 2
  109. #define SCID_NEWCLIENT 3
  110. #define SCID_ENDCLIENT 4
  111. #define SCID_OUTMSG 5
  112. #define SCID_INMSG 6
  113. #define SCID_RESETPEER 7
  114. #define SCID_ACCEPTPEER 8
  115. /**
  116. * "clienthello" client packet payload.
  117. * Packet type is SCID_CLIENTHELLO.
  118. */
  119. B_START_PACKED
  120. struct sc_client_hello {
  121. /**
  122. * Protocol version the client is using.
  123. */
  124. uint16_t version;
  125. } B_PACKED;
  126. B_END_PACKED
  127. /**
  128. * "serverhello" server packet payload.
  129. * Packet type is SCID_SERVERHELLO.
  130. */
  131. B_START_PACKED
  132. struct sc_server_hello {
  133. /**
  134. * Flags. Not used yet.
  135. */
  136. uint16_t flags;
  137. /**
  138. * Peer ID of the client.
  139. */
  140. peerid_t id;
  141. /**
  142. * IPv4 address of the client as seen by the server
  143. * (network byte order). Zero if not applicable.
  144. */
  145. uint32_t clientAddr;
  146. } B_PACKED;
  147. B_END_PACKED
  148. /**
  149. * "newclient" server packet payload.
  150. * Packet type is SCID_NEWCLIENT.
  151. * If the server is using TLS, follows up to SCID_NEWCLIENT_MAX_CERT_LEN
  152. * bytes of the new client's certificate (encoded in DER).
  153. */
  154. B_START_PACKED
  155. struct sc_server_newclient {
  156. /**
  157. * ID of the new peer.
  158. */
  159. peerid_t id;
  160. /**
  161. * Flags. Possible flags:
  162. * - SCID_NEWCLIENT_FLAG_RELAY_SERVER
  163. * You can relay frames to other peers through this peer.
  164. * - SCID_NEWCLIENT_FLAG_RELAY_CLIENT
  165. * You must allow this peer to relay frames to other peers through you.
  166. * - SCID_NEWCLIENT_FLAG_SSL
  167. * SSL must be used to talk to this peer through messages.
  168. */
  169. uint16_t flags;
  170. } B_PACKED;
  171. B_END_PACKED
  172. #define SCID_NEWCLIENT_FLAG_RELAY_SERVER 1
  173. #define SCID_NEWCLIENT_FLAG_RELAY_CLIENT 2
  174. #define SCID_NEWCLIENT_FLAG_SSL 4
  175. #define SCID_NEWCLIENT_MAX_CERT_LEN (SC_MAX_PAYLOAD - sizeof(struct sc_server_newclient))
  176. /**
  177. * "endclient" server packet payload.
  178. * Packet type is SCID_ENDCLIENT.
  179. */
  180. B_START_PACKED
  181. struct sc_server_endclient {
  182. /**
  183. * ID of the removed peer.
  184. */
  185. peerid_t id;
  186. } B_PACKED;
  187. B_END_PACKED
  188. /**
  189. * "outmsg" client packet header.
  190. * Packet type is SCID_OUTMSG.
  191. * Follows up to SC_MAX_MSGLEN bytes of message payload.
  192. */
  193. B_START_PACKED
  194. struct sc_client_outmsg {
  195. /**
  196. * ID of the destionation peer.
  197. */
  198. peerid_t clientid;
  199. } B_PACKED;
  200. B_END_PACKED
  201. /**
  202. * "inmsg" server packet payload.
  203. * Packet type is SCID_INMSG.
  204. * Follows up to SC_MAX_MSGLEN bytes of message payload.
  205. */
  206. B_START_PACKED
  207. struct sc_server_inmsg {
  208. /**
  209. * ID of the source peer.
  210. */
  211. peerid_t clientid;
  212. } B_PACKED;
  213. B_END_PACKED
  214. #define _SC_MAX_OUTMSGLEN (SC_MAX_PAYLOAD - sizeof(struct sc_client_outmsg))
  215. #define _SC_MAX_INMSGLEN (SC_MAX_PAYLOAD - sizeof(struct sc_server_inmsg))
  216. #define SC_MAX_MSGLEN (_SC_MAX_OUTMSGLEN < _SC_MAX_INMSGLEN ? _SC_MAX_OUTMSGLEN : _SC_MAX_INMSGLEN)
  217. /**
  218. * "resetpeer" client packet header.
  219. * Packet type is SCID_RESETPEER.
  220. */
  221. B_START_PACKED
  222. struct sc_client_resetpeer {
  223. /**
  224. * ID of the peer to reset.
  225. */
  226. peerid_t clientid;
  227. } B_PACKED;
  228. B_END_PACKED
  229. /**
  230. * "acceptpeer" client packet payload.
  231. * Packet type is SCID_ACCEPTPEER.
  232. */
  233. B_START_PACKED
  234. struct sc_client_acceptpeer {
  235. /**
  236. * ID of the peer to accept.
  237. */
  238. peerid_t clientid;
  239. } B_PACKED;
  240. B_END_PACKED
  241. #endif