scproto.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #define SC_VERSION 29
  87. #define SC_OLDVERSION_NOSSL 27
  88. #define SC_OLDVERSION_BROKENCERT 26
  89. #define SC_KEEPALIVE_INTERVAL 10000
  90. /**
  91. * SCProto packet header.
  92. * Follows up to SC_MAX_PAYLOAD bytes of payload.
  93. */
  94. struct sc_header {
  95. /**
  96. * Message type.
  97. */
  98. uint8_t type;
  99. } __attribute__((packed));
  100. #define SC_MAX_PAYLOAD 2000
  101. #define SC_MAX_ENC (sizeof(struct sc_header) + SC_MAX_PAYLOAD)
  102. typedef uint16_t peerid_t;
  103. #define SCID_KEEPALIVE 0
  104. #define SCID_CLIENTHELLO 1
  105. #define SCID_SERVERHELLO 2
  106. #define SCID_NEWCLIENT 3
  107. #define SCID_ENDCLIENT 4
  108. #define SCID_OUTMSG 5
  109. #define SCID_INMSG 6
  110. #define SCID_RESETPEER 7
  111. #define SCID_ACCEPTPEER 8
  112. /**
  113. * "clienthello" client packet payload.
  114. * Packet type is SCID_CLIENTHELLO.
  115. */
  116. struct sc_client_hello {
  117. /**
  118. * Protocol version the client is using.
  119. */
  120. uint16_t version;
  121. } __attribute__((packed));
  122. /**
  123. * "serverhello" server packet payload.
  124. * Packet type is SCID_SERVERHELLO.
  125. */
  126. struct sc_server_hello {
  127. /**
  128. * Flags. Not used yet.
  129. */
  130. uint16_t flags;
  131. /**
  132. * Peer ID of the client.
  133. */
  134. peerid_t id;
  135. /**
  136. * IPv4 address of the client as seen by the server
  137. * (network byte order). Zero if not applicable.
  138. */
  139. uint32_t clientAddr;
  140. } __attribute__((packed));
  141. /**
  142. * "newclient" server packet payload.
  143. * Packet type is SCID_NEWCLIENT.
  144. * If the server is using TLS, follows up to SCID_NEWCLIENT_MAX_CERT_LEN
  145. * bytes of the new client's certificate (encoded in DER).
  146. */
  147. struct sc_server_newclient {
  148. /**
  149. * ID of the new peer.
  150. */
  151. peerid_t id;
  152. /**
  153. * Flags. Possible flags:
  154. * - SCID_NEWCLIENT_FLAG_RELAY_SERVER
  155. * You can relay frames to other peers through this peer.
  156. * - SCID_NEWCLIENT_FLAG_RELAY_CLIENT
  157. * You must allow this peer to relay frames to other peers through you.
  158. * - SCID_NEWCLIENT_FLAG_SSL
  159. * SSL must be used to talk to this peer through messages.
  160. */
  161. uint16_t flags;
  162. } __attribute__((packed));
  163. #define SCID_NEWCLIENT_FLAG_RELAY_SERVER 1
  164. #define SCID_NEWCLIENT_FLAG_RELAY_CLIENT 2
  165. #define SCID_NEWCLIENT_FLAG_SSL 4
  166. #define SCID_NEWCLIENT_MAX_CERT_LEN (SC_MAX_PAYLOAD - sizeof(struct sc_server_newclient))
  167. /**
  168. * "endclient" server packet payload.
  169. * Packet type is SCID_ENDCLIENT.
  170. */
  171. struct sc_server_endclient {
  172. /**
  173. * ID of the removed peer.
  174. */
  175. peerid_t id;
  176. } __attribute__((packed));
  177. /**
  178. * "outmsg" client packet header.
  179. * Packet type is SCID_OUTMSG.
  180. * Follows up to SC_MAX_MSGLEN bytes of message payload.
  181. */
  182. struct sc_client_outmsg {
  183. /**
  184. * ID of the destionation peer.
  185. */
  186. peerid_t clientid;
  187. } __attribute__((packed));
  188. /**
  189. * "inmsg" server packet payload.
  190. * Packet type is SCID_INMSG.
  191. * Follows up to SC_MAX_MSGLEN bytes of message payload.
  192. */
  193. struct sc_server_inmsg {
  194. /**
  195. * ID of the source peer.
  196. */
  197. peerid_t clientid;
  198. } __attribute__((packed));
  199. #define _SC_MAX_OUTMSGLEN (SC_MAX_PAYLOAD - sizeof(struct sc_client_outmsg))
  200. #define _SC_MAX_INMSGLEN (SC_MAX_PAYLOAD - sizeof(struct sc_server_inmsg))
  201. #define SC_MAX_MSGLEN (_SC_MAX_OUTMSGLEN < _SC_MAX_INMSGLEN ? _SC_MAX_OUTMSGLEN : _SC_MAX_INMSGLEN)
  202. /**
  203. * "resetpeer" client packet header.
  204. * Packet type is SCID_RESETPEER.
  205. */
  206. struct sc_client_resetpeer {
  207. /**
  208. * ID of the peer to reset.
  209. */
  210. peerid_t clientid;
  211. } __attribute__((packed));
  212. /**
  213. * "acceptpeer" client packet payload.
  214. * Packet type is SCID_ACCEPTPEER.
  215. */
  216. struct sc_client_acceptpeer {
  217. /**
  218. * ID of the peer to accept.
  219. */
  220. peerid_t clientid;
  221. } __attribute__((packed));
  222. #endif