SwitchCore.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 <https://www.gnu.org/licenses/>.
  14. */
  15. #include "memory/Allocator.h"
  16. #include "util/log/Log.h"
  17. #include "switch/SwitchCore.h"
  18. // TODO(cjd): Get rid of NumberCompress so we can set encodingScheme at runtime.
  19. #define NumberCompress_OLD_CODE
  20. #include "switch/NumberCompress.h"
  21. #include "util/Bits.h"
  22. #include "util/Checksum.h"
  23. #include "util/Endian.h"
  24. #include "wire/Control.h"
  25. #include "wire/Error.h"
  26. #include "wire/Headers.h"
  27. #include "wire/SwitchHeader.h"
  28. #include "wire/Message.h"
  29. #include <inttypes.h>
  30. #include <stdbool.h>
  31. struct SwitchInterface
  32. {
  33. struct Iface iface;
  34. struct Allocator* alloc;
  35. struct SwitchCore_pvt* core;
  36. struct Allocator_OnFreeJob* onFree;
  37. Identity
  38. };
  39. struct SwitchCore_pvt
  40. {
  41. struct SwitchCore pub;
  42. struct SwitchInterface interfaces[NumberCompress_INTERFACES];
  43. bool routerAdded;
  44. struct Log* logger;
  45. EventBase_t* eventBase;
  46. struct Allocator* allocator;
  47. Identity
  48. };
  49. struct ErrorPacket8 {
  50. struct SwitchHeader switchHeader;
  51. uint32_t handle;
  52. struct Control ctrl;
  53. };
  54. Assert_compileTime(sizeof(struct ErrorPacket8) == SwitchHeader_SIZE + 4 + sizeof(struct Control));
  55. static inline Iface_DEFUN sendError(struct SwitchInterface* iface,
  56. Message_t* cause,
  57. uint32_t code,
  58. struct Log* logger)
  59. {
  60. if (Message_getLength(cause) < SwitchHeader_SIZE + 4) {
  61. Log_debug(logger, "runt");
  62. return Error(cause, "RUNT");
  63. }
  64. struct SwitchHeader* causeHeader = (struct SwitchHeader*) Message_bytes(cause);
  65. if (SwitchHeader_getSuppressErrors(causeHeader)) {
  66. // don't send errors if they're asking us to suppress them!
  67. return NULL;
  68. }
  69. // limit of 256 bytes
  70. if (Message_getLength(cause) > Control_Error_MAX_SIZE) {
  71. Err(Message_truncate(cause, Control_Error_MAX_SIZE));
  72. }
  73. // Shift back so we can add another header.
  74. Err(Message_epush(cause,
  75. NULL,
  76. SwitchHeader_SIZE + 4 + Control_Header_SIZE + Control_Error_HEADER_SIZE));
  77. struct ErrorPacket8* err = (struct ErrorPacket8*) Message_bytes(cause);
  78. err->switchHeader.label_be = Bits_bitReverse64(causeHeader->label_be);
  79. SwitchHeader_setSuppressErrors(&err->switchHeader, true);
  80. SwitchHeader_setVersion(&err->switchHeader, SwitchHeader_CURRENT_VERSION);
  81. SwitchHeader_setTrafficClass(&err->switchHeader, 0xffff);
  82. SwitchHeader_setCongestion(&err->switchHeader, 0);
  83. err->handle = 0xffffffff;
  84. err->ctrl.header.type_be = Control_ERROR_be;
  85. err->ctrl.content.error.errorType_be = Endian_hostToBigEndian32(code);
  86. err->ctrl.header.checksum_be = 0;
  87. err->ctrl.header.checksum_be =
  88. Checksum_engine_be((uint8_t*) &err->ctrl, Message_getLength(cause) - SwitchHeader_SIZE - 4);
  89. return Iface_next(&iface->iface, cause);
  90. }
  91. #define DEBUG_SRC_DST(logger, message) \
  92. Log_debug(logger, message " ([%u] to [%u])", sourceIndex, destIndex)
  93. /** This never returns an error, it sends an error packet instead. */
  94. static Iface_DEFUN receiveMessage(Message_t* message, struct Iface* iface)
  95. {
  96. struct SwitchInterface* sourceIf = Identity_check((struct SwitchInterface*) iface);
  97. struct SwitchCore_pvt* core = Identity_check(sourceIf->core);
  98. if (Message_getLength(message) < SwitchHeader_SIZE) {
  99. Log_debug(core->logger, "DROP runt");
  100. return Error(message, "RUNT");
  101. }
  102. struct SwitchHeader* header = (struct SwitchHeader*) Message_bytes(message);
  103. const uint64_t label = Endian_bigEndianToHost64(header->label_be);
  104. uint32_t bits = NumberCompress_bitsUsedForLabel(label);
  105. const uint32_t sourceIndex = sourceIf - core->interfaces;
  106. const uint32_t destIndex = NumberCompress_getDecompressed(label, bits);
  107. const uint32_t sourceBits = NumberCompress_bitsUsedForNumber(sourceIndex);
  108. Assert_true(destIndex < NumberCompress_INTERFACES);
  109. Assert_true(sourceIndex < NumberCompress_INTERFACES);
  110. if (1 == destIndex && 1 != (label & 0xf)) {
  111. // routing interface: must always be compressed as 0001
  112. DEBUG_SRC_DST(core->logger,
  113. "DROP packet for this router because the destination "
  114. "discriminator was wrong");
  115. return sendError(sourceIf, message, Error_MALFORMED_ADDRESS, core->logger);
  116. }
  117. if (sourceBits > bits) {
  118. if (destIndex == 1) {
  119. // If the destination index is this router, don't drop the packet since there no
  120. // way for a node to know the size of the representation of its source label.
  121. // - label ends in 0001; if there are enough zeroes at the end after removing the 1,
  122. // we can still fit in the source discriminator
  123. // - the return path probably doesn't start with 3 zeroes, but it will still be working,
  124. // as the source discriminator is large enough to make space for 3 zeroes between
  125. // reverse return path and forward path (see below)
  126. if (0 != ((label ^ 1) & (UINT64_MAX >> (64 - sourceBits - 4)))) {
  127. // This is a bug.
  128. // https://github.com/cjdelisle/cjdns/issues/93
  129. // The problem is that there is no way to splice a route and know for certain
  130. // that you've not spliced one which will end up in this if statement.
  131. // Unfortunately there seems no clean way around this issue at the moment.
  132. // If this router and switch communicated using labels with "64 + four less
  133. // than the number of bits in largest discriminator" bits wide, it could handle
  134. // this situation, this solution is obviously non-trivial.
  135. DEBUG_SRC_DST(core->logger,
  136. "DROP packet for this router because there is no way to "
  137. "represent the return path.");
  138. return sendError(sourceIf, message, Error_RETURN_PATH_INVALID, core->logger);
  139. }
  140. bits = sourceBits;
  141. } else if (1 == sourceIndex) {
  142. // - we need at least 3 zeroes between reverse return path and forward path:
  143. // right now the label only contains the forward path
  144. // - sourceBits == 4, bits < 4 -> bits + 64 - sourceBits < 64
  145. // - the reverse source discriminator "1000" and the target discriminator "0001"
  146. // can overlap as "10001" (or "100001" or ...)
  147. if (0 != label >> (bits + 64 - sourceBits)) {
  148. // not enough zeroes
  149. DEBUG_SRC_DST(core->logger, "DROP packet because source address is "
  150. "larger than destination address.");
  151. return sendError(sourceIf, message, Error_MALFORMED_ADDRESS, core->logger);
  152. }
  153. } else {
  154. //Log_info(core->logger, "source exceeds dest");
  155. DEBUG_SRC_DST(core->logger, "DROP packet because source address is "
  156. "larger than destination address.");
  157. return sendError(sourceIf, message, Error_MALFORMED_ADDRESS, core->logger);
  158. }
  159. }
  160. if (core->interfaces[destIndex].alloc == NULL) {
  161. if (sourceIndex == 1) {
  162. DEBUG_SRC_DST(core->logger, "DROP packet we tried to send, no such peer");
  163. }
  164. // This is important, but it's someone else's important problem
  165. // DEBUG_SRC_DST(core->logger, "DROP packet because there is no interface "
  166. // "where the bits specify.");
  167. return sendError(sourceIf, message, Error_MALFORMED_ADDRESS, core->logger);
  168. }
  169. /*if (sourceIndex == destIndex && sourceIndex != 1) {
  170. DEBUG_SRC_DST(core->logger, "DROP Packet with redundant route.");
  171. return sendError(sourceIf, message, Error_LOOP_ROUTE, core->logger);
  172. }*/
  173. uint64_t sourceLabel = Bits_bitReverse64(NumberCompress_getCompressed(sourceIndex, bits));
  174. uint64_t targetLabel = (label >> bits) | sourceLabel;
  175. int cloneLength = (Message_getLength(message) < Control_Error_MAX_SIZE) ?
  176. Message_getLength(message) : Control_Error_MAX_SIZE;
  177. uint8_t messageClone[Control_Error_MAX_SIZE];
  178. Bits_memcpy(messageClone, Message_bytes(message), cloneLength);
  179. // Update the header
  180. header->label_be = Endian_hostToBigEndian64(targetLabel);
  181. uint32_t labelShift = SwitchHeader_getLabelShift(header) + bits;
  182. if (labelShift > 63) {
  183. // TODO(cjd): hmm should we return an error packet?
  184. Log_debug(core->logger, "Label rolled over");
  185. return Error(message, "UNDELIVERABLE");
  186. }
  187. SwitchHeader_setLabelShift(header, labelShift);
  188. SwitchHeader_setTrafficClass(header, 0xffff);
  189. return Iface_next(&core->interfaces[destIndex].iface, message);
  190. }
  191. static void removeInterface(struct Allocator_OnFreeJob* job)
  192. {
  193. struct SwitchInterface* si = Identity_check((struct SwitchInterface*) job->userData);
  194. Bits_memset(si, 0, sizeof(struct SwitchInterface));
  195. }
  196. void SwitchCore_swapInterfaces(struct Iface* userIf1, struct Iface* userIf2)
  197. {
  198. struct SwitchInterface* si1 = Identity_check((struct SwitchInterface*) userIf1->connectedIf);
  199. struct SwitchInterface* si2 = Identity_check((struct SwitchInterface*) userIf2->connectedIf);
  200. Iface_unplumb(userIf1, &si1->iface);
  201. Iface_unplumb(userIf2, &si2->iface);
  202. Assert_true(Allocator_cancelOnFree(si1->onFree) > -1);
  203. Assert_true(Allocator_cancelOnFree(si2->onFree) > -1);
  204. struct SwitchInterface si3;
  205. Bits_memcpy(&si3, si1, sizeof(struct SwitchInterface));
  206. Bits_memcpy(si1, si2, sizeof(struct SwitchInterface));
  207. Bits_memcpy(si2, &si3, sizeof(struct SwitchInterface));
  208. si1->onFree = Allocator_onFree(si1->alloc, removeInterface, si1);
  209. si2->onFree = Allocator_onFree(si2->alloc, removeInterface, si2);
  210. Iface_plumb(userIf2, &si1->iface);
  211. Iface_plumb(userIf1, &si2->iface);
  212. }
  213. int SwitchCore_addInterface(struct SwitchCore* switchCore,
  214. struct Iface* iface,
  215. struct Allocator* alloc,
  216. uint64_t* labelOut)
  217. {
  218. struct SwitchCore_pvt* core = Identity_check((struct SwitchCore_pvt*)switchCore);
  219. int ifIndex = 0;
  220. // If there's a vacent spot where another iface was before it was removed, use that.
  221. for (;;ifIndex++) {
  222. if (ifIndex == NumberCompress_INTERFACES) { return SwitchCore_addInterface_OUT_OF_SPACE; }
  223. if (!core->interfaces[ifIndex].iface.send) { break; }
  224. }
  225. struct SwitchInterface* newIf = &core->interfaces[ifIndex];
  226. Identity_set(newIf);
  227. newIf->iface.send = receiveMessage;
  228. newIf->core = core;
  229. newIf->alloc = alloc;
  230. newIf->onFree = Allocator_onFree(alloc, removeInterface, newIf);
  231. Iface_plumb(iface, &newIf->iface);
  232. uint32_t bits = NumberCompress_bitsUsedForNumber(ifIndex);
  233. *labelOut = NumberCompress_getCompressed(ifIndex, bits) | (1 << bits);
  234. return 0;
  235. }
  236. struct SwitchCore* SwitchCore_new(struct Log* logger,
  237. struct Allocator* allocator,
  238. EventBase_t* base)
  239. {
  240. struct SwitchCore_pvt* core = Allocator_calloc(allocator, sizeof(struct SwitchCore_pvt), 1);
  241. Identity_set(core);
  242. core->allocator = allocator;
  243. core->logger = logger;
  244. core->eventBase = base;
  245. struct SwitchInterface* routerIf = &core->interfaces[1];
  246. Identity_set(routerIf);
  247. routerIf->iface.send = receiveMessage;
  248. routerIf->core = core;
  249. routerIf->alloc = allocator;
  250. core->pub.routerIf = &routerIf->iface;
  251. return &core->pub;
  252. }