scproto.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**
  2. * @file scproto.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Definitions for SCProto, the protocol that the clients communicate in
  25. * with the server.
  26. *
  27. * All multi-byte integers in structs are little-endian, unless stated otherwise.
  28. *
  29. * A SCProto packet consists of:
  30. * - a header (struct {@link sc_header}) which contains the type of the
  31. * packet
  32. * - the payload
  33. *
  34. * It goes roughly like that:
  35. *
  36. * When the client connects to the server, it sends a "clienthello" packet
  37. * to the server. The packet contains the protocol version the client is using.
  38. * When the server receives the "clienthello" packet, it checks the version.
  39. * If it doesn't match, it disconnects the client. Otherwise the server sends
  40. * the client a "serverhello" packet to the client. That packet contains
  41. * the ID of the client and possibly its IPv4 address as the server sees it
  42. * (zero if not applicable).
  43. *
  44. * The server than proceeds to synchronize the peers' knowledge of each other.
  45. * It does that by sending a "newclient" messages to a client to inform it of
  46. * another peer, and "endclient" messages to inform it that a peer is gone.
  47. * Each client, upon receiving a "newclient" message, MUST sent a corresponding
  48. * "acceptpeer" message, before sending any messages to the new peer.
  49. * The server forwards messages between synchronized peers to allow them to
  50. * communicate. A peer sends a message to another peer by sending the "outmsg"
  51. * packet to the server, and the server delivers a message to a peer by sending
  52. * it the "inmsg" packet.
  53. *
  54. * The message service is reliable; messages from one client to another are
  55. * expected to arrive unmodified and in the same order. There is, however,
  56. * no flow control. This means that messages can not be used for bulk transfers
  57. * between the clients (and they are not). If the server runs out of buffer for
  58. * messages from one client to another, it will stop forwarding messages, and
  59. * will reset knowledge of the two clients after some delay. Similarly, if one
  60. * of the clients runs out of buffer locally, it will send the "resetpeer"
  61. * packet to make the server reset knowledge.
  62. *
  63. * The messages transport either:
  64. *
  65. * - If the relevant "newclient" packets do not contain the
  66. * SCID_NEWCLIENT_FLAG_SSL flag, then plaintext MsgProto messages.
  67. *
  68. * - If the relevant "newclient" packets do contain the SCID_NEWCLIENT_FLAG_SSL
  69. * flag, then SSL, broken down into packets, PacketProto inside SSL, and finally
  70. * MsgProto inside PacketProto. The master peer (one with higher ID) acts as an
  71. * SSL server, and the other acts as an SSL client. The peers must identify with
  72. * the same certificate they used when connecting to the server, and each peer
  73. * must byte-compare the other's certificate agains the one provided to it by
  74. * by the server in the relevent "newclient" message.
  75. */
  76. #ifndef BADVPN_PROTOCOL_SCPROTO_H
  77. #define BADVPN_PROTOCOL_SCPROTO_H
  78. #include <stdint.h>
  79. #define SC_VERSION 29
  80. #define SC_OLDVERSION_NOSSL 27
  81. #define SC_OLDVERSION_BROKENCERT 26
  82. #define SC_KEEPALIVE_INTERVAL 10000
  83. /**
  84. * SCProto packet header.
  85. * Follows up to SC_MAX_PAYLOAD bytes of payload.
  86. */
  87. struct sc_header {
  88. /**
  89. * Message type.
  90. */
  91. uint8_t type;
  92. } __attribute__((packed));
  93. #define SC_MAX_PAYLOAD 2000
  94. #define SC_MAX_ENC (sizeof(struct sc_header) + SC_MAX_PAYLOAD)
  95. typedef uint16_t peerid_t;
  96. #define SCID_KEEPALIVE 0
  97. #define SCID_CLIENTHELLO 1
  98. #define SCID_SERVERHELLO 2
  99. #define SCID_NEWCLIENT 3
  100. #define SCID_ENDCLIENT 4
  101. #define SCID_OUTMSG 5
  102. #define SCID_INMSG 6
  103. #define SCID_RESETPEER 7
  104. #define SCID_ACCEPTPEER 8
  105. /**
  106. * "clienthello" client packet payload.
  107. * Packet type is SCID_CLIENTHELLO.
  108. */
  109. struct sc_client_hello {
  110. /**
  111. * Protocol version the client is using.
  112. */
  113. uint16_t version;
  114. } __attribute__((packed));
  115. /**
  116. * "serverhello" server packet payload.
  117. * Packet type is SCID_SERVERHELLO.
  118. */
  119. struct sc_server_hello {
  120. /**
  121. * Flags. Not used yet.
  122. */
  123. uint16_t flags;
  124. /**
  125. * Peer ID of the client.
  126. */
  127. peerid_t id;
  128. /**
  129. * IPv4 address of the client as seen by the server
  130. * (network byte order). Zero if not applicable.
  131. */
  132. uint32_t clientAddr;
  133. } __attribute__((packed));
  134. /**
  135. * "newclient" server packet payload.
  136. * Packet type is SCID_NEWCLIENT.
  137. * If the server is using TLS, follows up to SCID_NEWCLIENT_MAX_CERT_LEN
  138. * bytes of the new client's certificate (encoded in DER).
  139. */
  140. struct sc_server_newclient {
  141. /**
  142. * ID of the new peer.
  143. */
  144. peerid_t id;
  145. /**
  146. * Flags. Possible flags:
  147. * - SCID_NEWCLIENT_FLAG_RELAY_SERVER
  148. * You can relay frames to other peers through this peer.
  149. * - SCID_NEWCLIENT_FLAG_RELAY_CLIENT
  150. * You must allow this peer to relay frames to other peers through you.
  151. * - SCID_NEWCLIENT_FLAG_SSL
  152. * SSL must be used to talk to this peer through messages.
  153. */
  154. uint16_t flags;
  155. } __attribute__((packed));
  156. #define SCID_NEWCLIENT_FLAG_RELAY_SERVER 1
  157. #define SCID_NEWCLIENT_FLAG_RELAY_CLIENT 2
  158. #define SCID_NEWCLIENT_FLAG_SSL 4
  159. #define SCID_NEWCLIENT_MAX_CERT_LEN (SC_MAX_PAYLOAD - sizeof(struct sc_server_newclient))
  160. /**
  161. * "endclient" server packet payload.
  162. * Packet type is SCID_ENDCLIENT.
  163. */
  164. struct sc_server_endclient {
  165. /**
  166. * ID of the removed peer.
  167. */
  168. peerid_t id;
  169. } __attribute__((packed));
  170. /**
  171. * "outmsg" client packet header.
  172. * Packet type is SCID_OUTMSG.
  173. * Follows up to SC_MAX_MSGLEN bytes of message payload.
  174. */
  175. struct sc_client_outmsg {
  176. /**
  177. * ID of the destionation peer.
  178. */
  179. peerid_t clientid;
  180. } __attribute__((packed));
  181. /**
  182. * "inmsg" server packet payload.
  183. * Packet type is SCID_INMSG.
  184. * Follows up to SC_MAX_MSGLEN bytes of message payload.
  185. */
  186. struct sc_server_inmsg {
  187. /**
  188. * ID of the source peer.
  189. */
  190. peerid_t clientid;
  191. } __attribute__((packed));
  192. #define _SC_MAX_OUTMSGLEN (SC_MAX_PAYLOAD - sizeof(struct sc_client_outmsg))
  193. #define _SC_MAX_INMSGLEN (SC_MAX_PAYLOAD - sizeof(struct sc_server_inmsg))
  194. #define SC_MAX_MSGLEN (_SC_MAX_OUTMSGLEN < _SC_MAX_INMSGLEN ? _SC_MAX_OUTMSGLEN : _SC_MAX_INMSGLEN)
  195. /**
  196. * "resetpeer" client packet header.
  197. * Packet type is SCID_RESETPEER.
  198. */
  199. struct sc_client_resetpeer {
  200. /**
  201. * ID of the peer to reset.
  202. */
  203. peerid_t clientid;
  204. } __attribute__((packed));
  205. /**
  206. * "acceptpeer" client packet payload.
  207. * Packet type is SCID_ACCEPTPEER.
  208. */
  209. struct sc_client_acceptpeer {
  210. /**
  211. * ID of the peer to accept.
  212. */
  213. peerid_t clientid;
  214. } __attribute__((packed));
  215. #endif