FutureHeaders.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef RouteHeader_H
  16. #define RouteHeader_H
  17. #include "util/Assert.h"
  18. #include <inttypes.h>
  19. /**
  20. * The RouteHeader is hidden from the switches by the l2 encryption layer but it is seen
  21. * by every router which hands off the packet.
  22. *
  23. * 1 2 3
  24. * 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
  25. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  26. * 0 | ver | MTU | Flow ID |
  27. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  28. * 4 | |
  29. * + +
  30. * 8 | |
  31. * + Destination IP6 +
  32. * 12 | |
  33. * + +
  34. * 16 | |
  35. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  36. *
  37. * Ver is 3 bits representing the protocol version *modulo 8*,
  38. */
  39. struct RouteHeader
  40. {
  41. uint16_t versionAndMtu_be;
  42. /**
  43. * An arbitrary value created by hashing information which represents the dataflow such
  44. * as protocol (TCP/UDP) and port numbers such that multiple packets which are part of the
  45. * same data flow will will contain the same IDs.
  46. * It is not critical that each dataflow has a unique ID.
  47. * Routers faced with multiple near-best-paths to a given destination, each having a distinct
  48. * bottleneck, may split traffic to optimize usage of each path with reduced risk of packets
  49. * arriving out of order.
  50. * While an implementatin could theoretically use sequencial flowIds such as 1, 2, 3, etc.
  51. * it is in the user's best interest to minimize the number of other flows using the same
  52. * flowId as himself so the optimum choice of flowId is through use of a pseud-random hash
  53. * function.
  54. */
  55. uint16_t flowId;
  56. /** The destination cjdns address. */
  57. uint8_t destAddr[16];
  58. };
  59. #define RouteHeader_SIZE 20
  60. Assert_compileTime(sizeof(struct RouteHeader) == RouteHeader_SIZE);
  61. static inline void RouteHeader_setVersion(struct RouteHeader* hdr, uint8_t version)
  62. {
  63. type->versionAndMtu_be = (type->versionAndMtu_be & Endian_hostToBigEndian16(0x1fff)) |
  64. Endian_hostToBigEndian16((version % 8) << 13);
  65. }
  66. static inline uint8_t RouteHeader_getVersion(struct RouteHeader* hdr)
  67. {
  68. return Endian_bigEndianToHost16(type->versionAndMtu_be) >> 13;
  69. }
  70. static inline uint16_t RouteHeader_getMTU(struct RouteHeader* hdr)
  71. {
  72. return Endian_bigEndianToHost16(type->versionAndMtu_be) >> 13;
  73. }
  74. * This is to be initialized to what the sending node believes is the full path MTU
  75. * (or 0xffff if unsure) and clamped to the MTU of each path by the routers which forward
  76. * the packet.
  77. #endif
  78. /* vim: set expandtab ts=4 sw=4: */
  79. /*
  80. * You may redistribute this program and/or modify it under the terms of
  81. * the GNU General Public License as published by the Free Software Foundation,
  82. * either version 3 of the License, or (at your option) any later version.
  83. *
  84. * This program is distributed in the hope that it will be useful,
  85. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  86. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  87. * GNU General Public License for more details.
  88. *
  89. * You should have received a copy of the GNU General Public License
  90. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  91. */
  92. #ifndef DataHeader_H
  93. #define DataHeader_H
  94. #include "util/Assert.h"
  95. #include "util/Endian.h"
  96. /**
  97. * 1 2 3
  98. * 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
  99. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  100. * 0 | ver |R| unused| unused | Content Type |
  101. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  102. *
  103. * The DataHeader is protected from the switches by the l2 encryption layer and from the
  104. * routers by the l3 layer of encryption. It's primary uses are to tell the endpoint enough
  105. * information to route the packet to the correct cjdns subsystem and (maybe) reconstruct
  106. * the IPv6 header.
  107. */
  108. struct DataHeader
  109. {
  110. /**
  111. * If set, the receiving node should respond within 10 milliseconds, if in the next 10
  112. * milliseconds it has something else to send back, this is acceptable, otherwise it should
  113. * synthisize a control packet to respond with.
  114. */
  115. #define DataHeader_RESPOND_TO_ME (1<<7)
  116. /**
  117. * Version is set to the version of the data as per Version.h
  118. * If the number is over 255, it wraps
  119. */
  120. uint8_t versionAndFlags;
  121. uint8_t unused;
  122. uint16_t contentType_be;
  123. };
  124. #define DataHeader_SIZE 4
  125. Assert_compileTime(sizeof(struct DataHeader) == DataHeader_SIZE);
  126. enum DataHeader_ContentType
  127. {
  128. /**
  129. * The lowest 255 message types are reserved for cjdns/IPv6 packets.
  130. * AKA: packets where the IP address is within the FC00::/8 block.
  131. * Any packet sent in this way will have the IPv6 header deconstructed and this
  132. * field will come from the nextHeader field in the IPv6 header.
  133. */
  134. DataHeader_ContentType_IP6_IP = 0,
  135. DataHeader_ContentType_IP6_ICMP = 1,
  136. DataHeader_ContentType_IP6_IGMP = 2,
  137. DataHeader_ContentType_IP6_IPIP = 4,
  138. DataHeader_ContentType_IP6_TCP = 6,
  139. DataHeader_ContentType_IP6_EGP = 8,
  140. DataHeader_ContentType_IP6_PUP = 12,
  141. DataHeader_ContentType_IP6_UDP = 17,
  142. DataHeader_ContentType_IP6_IDP = 22,
  143. DataHeader_ContentType_IP6_TP = 29,
  144. DataHeader_ContentType_IP6_DCCP = 33,
  145. DataHeader_ContentType_IP6_IPV6 = 41,
  146. DataHeader_ContentType_IP6_RSVP = 46,
  147. DataHeader_ContentType_IP6_GRE = 47,
  148. DataHeader_ContentType_IP6_ESP = 50,
  149. DataHeader_ContentType_IP6_AH = 51,
  150. DataHeader_ContentType_IP6_MTP = 92,
  151. DataHeader_ContentType_IP6_BEETPH = 94,
  152. DataHeader_ContentType_IP6_ENCAP = 98,
  153. DataHeader_ContentType_IP6_PIM = 103,
  154. DataHeader_ContentType_IP6_COMP = 108,
  155. DataHeader_ContentType_IP6_SCTP = 132,
  156. DataHeader_ContentType_IP6_UDPLITE = 136,
  157. DataHeader_ContentType_IP6_RAW = 255,
  158. /** Bencoded inter-router CTRL messages. */
  159. DataHeader_ContentType_CTRL = 256,
  160. /**
  161. * Content types in the AVAILABLE range are not defined and can be used
  162. * like port numbers for subsystems of cjdns to communicate with subsystems within
  163. * cjdns on other machines, providing they first agree on which numbers to use via
  164. * CTRL messages.
  165. */
  166. DataHeader_ContentType_AVAILABLE = 257),
  167. DataHeader_ContentType_AVAILABLE_MAX = 0xffff
  168. };
  169. static inline enum DataHeader_ContentType DataHeader_getContentType(struct DataHeader* hdr)
  170. {
  171. return Endian_bigEndianToHost16(hdr->contentType_be);
  172. }
  173. static inline void DataHeader_setContentType(struct DataHeader* hdr,
  174. enum DataHeader_ContentType type)
  175. {
  176. Assert_true(type <= DataHeader_ContentType_AVAILABLE_MAX);
  177. hdr->contentType_be = Endian_hostToBigEndian16(type);
  178. }
  179. static inline void DataHeader_setRespondToMe(struct DataHeader* hdr, bool rtm)
  180. {
  181. type->versionAndFlags = (type->versionAndFlags & ~DataHeader_RESPOND_TO_ME) |
  182. (rtm) ? DataHeader_RESPOND_TO_ME : 0;
  183. }
  184. static inline bool DataHeader_getRespondToMe(struct DataHeader* hdr)
  185. {
  186. return type->versionAndFlags & DataHeader_RESPOND_TO_ME;
  187. }
  188. static inline void DataHeader_setVersion(struct DataHeader* hdr, uint8_t version)
  189. {
  190. type->versionAndFlags = (type->versionAndFlags & 0x1f) | ((version % 8) << 5);
  191. }
  192. static inline uint8_t DataHeader_getVersion(struct DataHeader* hdr)
  193. {
  194. return type->versionAndFlags >> 5;
  195. }
  196. #endif