SwitchCore.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. #include "memory/Allocator.h"
  16. #include "util/log/Log.h"
  17. #include "switch/SwitchCore.h"
  18. #include "switch/NumberCompress.h"
  19. #include "switch/Penalty.h"
  20. #include "util/Bits.h"
  21. #include "util/Checksum.h"
  22. #include "util/Endian.h"
  23. #include "wire/Control.h"
  24. #include "wire/Error.h"
  25. #include "wire/Headers.h"
  26. #include "wire/SwitchHeader.h"
  27. #include "wire/Message.h"
  28. #include <inttypes.h>
  29. #include <stdbool.h>
  30. struct SwitchInterface
  31. {
  32. struct Iface iface;
  33. struct Allocator* alloc;
  34. struct SwitchCore_pvt* core;
  35. struct Penalty* penalty;
  36. struct Allocator_OnFreeJob* onFree;
  37. int state;
  38. Identity
  39. };
  40. struct SwitchCore_pvt
  41. {
  42. struct SwitchCore pub;
  43. struct SwitchInterface interfaces[NumberCompress_INTERFACES];
  44. bool routerAdded;
  45. struct Log* logger;
  46. struct EventBase* eventBase;
  47. struct Allocator* allocator;
  48. Identity
  49. };
  50. struct ErrorPacket8 {
  51. struct SwitchHeader switchHeader;
  52. uint32_t handle;
  53. struct Control ctrl;
  54. };
  55. Assert_compileTime(sizeof(struct ErrorPacket8) == SwitchHeader_SIZE + 4 + sizeof(struct Control));
  56. static inline Iface_DEFUN sendError(struct SwitchInterface* iface,
  57. struct Message* cause,
  58. uint32_t code,
  59. struct Log* logger)
  60. {
  61. if (cause->length < SwitchHeader_SIZE + 4) {
  62. Log_debug(logger, "runt");
  63. return NULL;
  64. }
  65. struct SwitchHeader* header = (struct SwitchHeader*) cause->bytes;
  66. if (SwitchHeader_getSuppressErrors(header)) {
  67. // don't send errors if they're asking us to suppress them!
  68. return NULL;
  69. }
  70. // limit of 256 bytes
  71. cause->length =
  72. (cause->length < Control_Error_MAX_SIZE) ? cause->length : Control_Error_MAX_SIZE;
  73. // Shift back so we can add another header.
  74. Message_shift(cause,
  75. SwitchHeader_SIZE + 4 + Control_Header_SIZE + Control_Error_HEADER_SIZE,
  76. NULL);
  77. struct ErrorPacket8* err = (struct ErrorPacket8*) cause->bytes;
  78. err->switchHeader.label_be = Bits_bitReverse64(header->label_be);
  79. SwitchHeader_setSuppressErrors(header, true);
  80. SwitchHeader_setVersion(header, SwitchHeader_CURRENT_VERSION);
  81. SwitchHeader_setPenalty(header, 0);
  82. SwitchHeader_setCongestion(header, 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((uint8_t*) &err->ctrl, cause->length - 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(struct Message* 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->length < SwitchHeader_SIZE) {
  99. Log_debug(core->logger, "DROP runt");
  100. return NULL;
  101. }
  102. struct SwitchHeader* header = (struct SwitchHeader*) message->bytes;
  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. Log_info(core->logger, "no such iface");
  162. DEBUG_SRC_DST(core->logger, "DROP packet because there is no interface "
  163. "where the bits specify.");
  164. return sendError(sourceIf, message, Error_MALFORMED_ADDRESS, core->logger);
  165. }
  166. if (core->interfaces[destIndex].state == SwitchCore_setInterfaceState_ifaceState_DOWN &&
  167. 1 != sourceIndex)
  168. {
  169. DEBUG_SRC_DST(core->logger, "DROP packet because interface is down");
  170. return sendError(sourceIf, message, Error_UNDELIVERABLE, core->logger);
  171. }
  172. /*if (sourceIndex == destIndex && sourceIndex != 1) {
  173. DEBUG_SRC_DST(core->logger, "DROP Packet with redundant route.");
  174. return sendError(sourceIf, message, Error_LOOP_ROUTE, core->logger);
  175. }*/
  176. uint64_t sourceLabel = Bits_bitReverse64(NumberCompress_getCompressed(sourceIndex, bits));
  177. uint64_t targetLabel = (label >> bits) | sourceLabel;
  178. int cloneLength = (message->length < Control_Error_MAX_SIZE) ?
  179. message->length : Control_Error_MAX_SIZE;
  180. uint8_t messageClone[Control_Error_MAX_SIZE];
  181. Bits_memcpy(messageClone, message->bytes, cloneLength);
  182. // Update the header
  183. header->label_be = Endian_hostToBigEndian64(targetLabel);
  184. uint32_t labelShift = SwitchHeader_getLabelShift(header) + bits;
  185. if (labelShift > 63) {
  186. // TODO(cjd): hmm should we return an error packet?
  187. Log_debug(core->logger, "Label rolled over");
  188. return NULL;
  189. }
  190. SwitchHeader_setLabelShift(header, labelShift);
  191. if (sourceIndex != 1 && destIndex != 1) {
  192. // no penalty for our own packets
  193. Penalty_apply(sourceIf->penalty, header, message->length);
  194. }
  195. return Iface_next(&core->interfaces[destIndex].iface, message);
  196. }
  197. static int removeInterface(struct Allocator_OnFreeJob* job)
  198. {
  199. struct SwitchInterface* si = Identity_check((struct SwitchInterface*) job->userData);
  200. Bits_memset(si, 0, sizeof(struct SwitchInterface));
  201. return 0;
  202. }
  203. void SwitchCore_setInterfaceState(struct Iface* userIf, int ifaceState)
  204. {
  205. struct SwitchInterface* sif = Identity_check((struct SwitchInterface*) userIf->connectedIf);
  206. Assert_true(ifaceState == (ifaceState & 1));
  207. sif->state = ifaceState;
  208. }
  209. void SwitchCore_swapInterfaces(struct Iface* userIf1, struct Iface* userIf2)
  210. {
  211. struct SwitchInterface* si1 = Identity_check((struct SwitchInterface*) userIf1->connectedIf);
  212. struct SwitchInterface* si2 = Identity_check((struct SwitchInterface*) userIf2->connectedIf);
  213. Iface_unplumb(userIf1, &si1->iface);
  214. Iface_unplumb(userIf2, &si2->iface);
  215. Assert_true(Allocator_cancelOnFree(si1->onFree) > -1);
  216. Assert_true(Allocator_cancelOnFree(si2->onFree) > -1);
  217. struct SwitchInterface si3;
  218. Bits_memcpyConst(&si3, si1, sizeof(struct SwitchInterface));
  219. Bits_memcpyConst(si1, si2, sizeof(struct SwitchInterface));
  220. Bits_memcpyConst(si2, &si3, sizeof(struct SwitchInterface));
  221. si1->onFree = Allocator_onFree(si1->alloc, removeInterface, si1);
  222. si2->onFree = Allocator_onFree(si2->alloc, removeInterface, si2);
  223. Iface_plumb(userIf2, &si1->iface);
  224. Iface_plumb(userIf1, &si2->iface);
  225. }
  226. int SwitchCore_addInterface(struct SwitchCore* switchCore,
  227. struct Iface* iface,
  228. struct Allocator* alloc,
  229. uint64_t* labelOut)
  230. {
  231. struct SwitchCore_pvt* core = Identity_check((struct SwitchCore_pvt*)switchCore);
  232. int ifIndex = 0;
  233. // If there's a vacent spot where another iface was before it was removed, use that.
  234. for (;;ifIndex++) {
  235. if (!core->interfaces[ifIndex].iface.send) { break; }
  236. if (ifIndex == NumberCompress_INTERFACES) { return SwitchCore_addInterface_OUT_OF_SPACE; }
  237. }
  238. struct SwitchInterface* newIf = &core->interfaces[ifIndex];
  239. Identity_set(newIf);
  240. newIf->iface.send = receiveMessage;
  241. newIf->core = core;
  242. newIf->alloc = alloc;
  243. newIf->penalty = Penalty_new(alloc, core->eventBase, core->logger);
  244. newIf->onFree = Allocator_onFree(alloc, removeInterface, newIf);
  245. newIf->state = SwitchCore_setInterfaceState_ifaceState_UP;
  246. Iface_plumb(iface, &newIf->iface);
  247. uint32_t bits = NumberCompress_bitsUsedForNumber(ifIndex);
  248. *labelOut = NumberCompress_getCompressed(ifIndex, bits) | (1 << bits);
  249. return 0;
  250. }
  251. struct SwitchCore* SwitchCore_new(struct Log* logger,
  252. struct Allocator* allocator,
  253. struct EventBase* base)
  254. {
  255. struct SwitchCore_pvt* core = Allocator_calloc(allocator, sizeof(struct SwitchCore_pvt), 1);
  256. Identity_set(core);
  257. core->allocator = allocator;
  258. core->logger = logger;
  259. core->eventBase = base;
  260. struct SwitchInterface* routerIf = &core->interfaces[1];
  261. Identity_set(routerIf);
  262. routerIf->iface.send = receiveMessage;
  263. routerIf->core = core;
  264. routerIf->alloc = allocator;
  265. routerIf->state = SwitchCore_setInterfaceState_ifaceState_UP;
  266. core->pub.routerIf = &routerIf->iface;
  267. return &core->pub;
  268. }