ControlHandler.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 "dht/Address.h"
  16. #include "net/ControlHandler.h"
  17. #include "util/Identity.h"
  18. #include "util/AddrTools.h"
  19. #include "util/Checksum.h"
  20. #include "wire/Control.h"
  21. #include "wire/Error.h"
  22. #define NumberCompress_OLD_CODE
  23. #include "switch/NumberCompress.h"
  24. struct ControlHandler_pvt
  25. {
  26. struct ControlHandler pub;
  27. struct Log* log;
  28. struct Allocator* alloc;
  29. uint8_t myPublicKey[32];
  30. struct Iface eventIf;
  31. struct Address activeSnode;
  32. Identity
  33. };
  34. /**
  35. * Expects [ Ctrl ][ Error ][ cause SwitchHeader ][ cause handle ][ cause etc.... ]
  36. */
  37. #define handleError_MIN_SIZE (Control_Header_SIZE + Control_Error_MIN_SIZE + SwitchHeader_SIZE + 4)
  38. static Iface_DEFUN handleError(struct Message* msg,
  39. struct ControlHandler_pvt* ch,
  40. uint64_t label,
  41. uint8_t* labelStr,
  42. struct RouteHeader* rh)
  43. {
  44. if (msg->length < handleError_MIN_SIZE) {
  45. Log_info(ch->log, "DROP runt error packet from [%s]", labelStr);
  46. return Error(RUNT);
  47. }
  48. msg->length = handleError_MIN_SIZE;
  49. Er_assert(Message_epush(msg, &rh->sh, SwitchHeader_SIZE));
  50. Er_assert(Message_epush32be(msg, 0xffffffff));
  51. Er_assert(Message_epush32be(msg, PFChan_Core_SWITCH_ERR));
  52. return Iface_next(&ch->eventIf, msg);
  53. }
  54. /**
  55. * Expects [ SwitchHeader ][ Ctrl ][ (key)Ping ][ data etc.... ]
  56. */
  57. #define handlePing_MIN_SIZE (Control_Header_SIZE + Control_Ping_MIN_SIZE)
  58. static Iface_DEFUN handlePing(struct Message* msg,
  59. struct ControlHandler_pvt* ch,
  60. uint64_t label,
  61. uint8_t* labelStr,
  62. uint16_t messageType_be)
  63. {
  64. if (msg->length < handlePing_MIN_SIZE) {
  65. Log_info(ch->log, "DROP runt ping");
  66. return Error(RUNT);
  67. }
  68. struct Control* ctrl = (struct Control*) msg->bytes;
  69. Er_assert(Message_eshift(msg, -Control_Header_SIZE));
  70. // Ping and keyPing share version location
  71. struct Control_Ping* ping = (struct Control_Ping*) msg->bytes;
  72. uint32_t herVersion = Endian_bigEndianToHost32(ping->version_be);
  73. if (!Version_isCompatible(Version_CURRENT_PROTOCOL, herVersion)) {
  74. Log_debug(ch->log, "DROP ping from incompatible version [%d]", herVersion);
  75. return Error(INVALID);
  76. }
  77. if (messageType_be == Control_KEYPING_be) {
  78. Log_debug(ch->log, "got switch keyPing from [%s]", labelStr);
  79. if (msg->length < Control_KeyPing_HEADER_SIZE) {
  80. // min keyPing size is longer
  81. Log_debug(ch->log, "DROP runt keyPing");
  82. return Error(RUNT);
  83. }
  84. if (msg->length > Control_KeyPing_MAX_SIZE) {
  85. Log_debug(ch->log, "DROP long keyPing");
  86. return Error(INVALID);
  87. }
  88. if (ping->magic != Control_KeyPing_MAGIC) {
  89. Log_debug(ch->log, "DROP keyPing (bad magic)");
  90. return Error(INVALID);
  91. }
  92. struct Control_KeyPing* keyPing = (struct Control_KeyPing*) msg->bytes;
  93. keyPing->magic = Control_KeyPong_MAGIC;
  94. ctrl->header.type_be = Control_KEYPONG_be;
  95. Bits_memcpy(keyPing->key, ch->myPublicKey, 32);
  96. } else if (messageType_be == Control_PING_be) {
  97. // Happens in benchmark.
  98. //Log_debug(ch->log, "got switch ping from [%s]", labelStr);
  99. if (ping->magic != Control_Ping_MAGIC) {
  100. Log_debug(ch->log, "DROP ping (bad magic)");
  101. return Error(INVALID);
  102. }
  103. ping->magic = Control_Pong_MAGIC;
  104. ctrl->header.type_be = Control_PONG_be;
  105. } else {
  106. Assert_failure("2+2=5");
  107. }
  108. ping->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
  109. Er_assert(Message_eshift(msg, Control_Header_SIZE));
  110. ctrl->header.checksum_be = 0;
  111. ctrl->header.checksum_be = Checksum_engine_be(msg->bytes, msg->length);
  112. Er_assert(Message_eshift(msg, RouteHeader_SIZE));
  113. struct RouteHeader* routeHeader = (struct RouteHeader*) msg->bytes;
  114. Bits_memset(routeHeader, 0, RouteHeader_SIZE);
  115. SwitchHeader_setVersion(&routeHeader->sh, SwitchHeader_CURRENT_VERSION);
  116. routeHeader->sh.label_be = Endian_hostToBigEndian64(label);
  117. routeHeader->flags |= RouteHeader_flags_CTRLMSG;
  118. return Iface_next(&ch->pub.coreIf, msg);
  119. }
  120. /**
  121. * Expects [ SwitchHeader ][ Ctrl ][ RPath ][ data etc.... ]
  122. */
  123. #define handleRPathQuery_MIN_SIZE (Control_Header_SIZE + Control_RPath_HEADER_SIZE)
  124. static Iface_DEFUN handleRPathQuery(struct Message* msg,
  125. struct ControlHandler_pvt* ch,
  126. uint64_t label,
  127. uint8_t* labelStr)
  128. {
  129. Log_debug(ch->log, "Incoming RPATH query");
  130. if (msg->length < handleRPathQuery_MIN_SIZE) {
  131. Log_info(ch->log, "DROP runt RPATH query");
  132. return Error(RUNT);
  133. }
  134. struct Control* ctrl = (struct Control*) msg->bytes;
  135. struct Control_RPath* rpa = &ctrl->content.rpath;
  136. if (rpa->magic != Control_RPATH_QUERY_MAGIC) {
  137. Log_debug(ch->log, "DROP RPATH query (bad magic)");
  138. return Error(INVALID);
  139. }
  140. ctrl->header.type_be = Control_RPATH_REPLY_be;
  141. rpa->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
  142. rpa->magic = Control_RPATH_REPLY_MAGIC;
  143. uint64_t label_be = Endian_hostToBigEndian64(label);
  144. Bits_memcpy(rpa->rpath_be, &label_be, 8);
  145. ctrl->header.checksum_be = 0;
  146. ctrl->header.checksum_be = Checksum_engine_be(msg->bytes, msg->length);
  147. Er_assert(Message_eshift(msg, RouteHeader_SIZE));
  148. struct RouteHeader* routeHeader = (struct RouteHeader*) msg->bytes;
  149. Bits_memset(routeHeader, 0, RouteHeader_SIZE);
  150. SwitchHeader_setVersion(&routeHeader->sh, SwitchHeader_CURRENT_VERSION);
  151. routeHeader->sh.label_be = label_be;
  152. routeHeader->flags |= RouteHeader_flags_CTRLMSG;
  153. return Iface_next(&ch->pub.coreIf, msg);
  154. }
  155. /**
  156. * Expects [ SwitchHeader ][ Ctrl ][ SupernodeQuery ][ data etc.... ]
  157. */
  158. #define handleGetSnodeQuery_MIN_SIZE (Control_Header_SIZE + Control_GetSnode_HEADER_SIZE)
  159. static Iface_DEFUN handleGetSnodeQuery(struct Message* msg,
  160. struct ControlHandler_pvt* ch,
  161. uint64_t label,
  162. uint8_t* labelStr)
  163. {
  164. Log_debug(ch->log, "incoming getSupernode query");
  165. if (msg->length < handleGetSnodeQuery_MIN_SIZE) {
  166. Log_info(ch->log, "DROP runt getSupernode query");
  167. return Error(RUNT);
  168. }
  169. struct Control* ctrl = (struct Control*) msg->bytes;
  170. struct Control_GetSnode* snq = &ctrl->content.getSnode;
  171. if (snq->magic != Control_GETSNODE_QUERY_MAGIC) {
  172. Log_debug(ch->log, "DROP getSupernode query (bad magic)");
  173. return Error(INVALID);
  174. }
  175. uint32_t herVersion = Endian_bigEndianToHost32(snq->version_be);
  176. if (!Version_isCompatible(Version_CURRENT_PROTOCOL, herVersion)) {
  177. Log_debug(ch->log, "DROP getSupernode query from incompatible version [%d]", herVersion);
  178. // Nothing wrong with the query but we're just not going to answer it
  179. return Error(NONE);
  180. }
  181. ctrl->header.type_be = Control_GETSNODE_REPLY_be;
  182. snq->kbps_be = 0xffffffff;
  183. snq->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
  184. snq->magic = Control_GETSNODE_REPLY_MAGIC;
  185. uint64_t fixedLabel = 0;
  186. if (ch->activeSnode.path) {
  187. fixedLabel = NumberCompress_getLabelFor(ch->activeSnode.path, label);
  188. uint64_t fixedLabel_be = Endian_hostToBigEndian64(fixedLabel);
  189. Bits_memcpy(snq->pathToSnode_be, &fixedLabel_be, 8);
  190. Bits_memcpy(snq->snodeKey, ch->activeSnode.key, 32);
  191. snq->snodeVersion_be = Endian_hostToBigEndian32(ch->activeSnode.protocolVersion);
  192. } else {
  193. Log_debug(ch->log, "getSupernode query and we do not have a known snode");
  194. snq->snodeVersion_be = 0;
  195. Bits_memset(snq->pathToSnode_be, 0, 8);
  196. Bits_memset(snq->snodeKey, 0, 32);
  197. }
  198. ctrl->header.checksum_be = 0;
  199. ctrl->header.checksum_be = Checksum_engine_be(msg->bytes, msg->length);
  200. Er_assert(Message_eshift(msg, RouteHeader_SIZE));
  201. struct RouteHeader* routeHeader = (struct RouteHeader*) msg->bytes;
  202. Bits_memset(routeHeader, 0, RouteHeader_SIZE);
  203. SwitchHeader_setVersion(&routeHeader->sh, SwitchHeader_CURRENT_VERSION);
  204. routeHeader->sh.label_be = Endian_hostToBigEndian64(label);
  205. routeHeader->flags |= RouteHeader_flags_CTRLMSG;
  206. Log_debug(ch->log, "Replied to [%" PRIx64 "] with snode [%" PRIx64 "] aka [%" PRIx64 "]",
  207. label, fixedLabel, ch->activeSnode.path);
  208. return Iface_next(&ch->pub.coreIf, msg);
  209. }
  210. /**
  211. * Handle an incoming control message from a switch.
  212. *
  213. * @param context the ducttape context.
  214. * @param message the control message, this should be alligned on the beginning of the content,
  215. * that is to say, after the end of the switch header.
  216. * @param switchHeader the header.
  217. * @param switchIf the interface which leads to the switch.
  218. * @param isFormV8 true if the control message is in the form specified by protocol version 8+
  219. */
  220. static Iface_DEFUN incomingFromCore(struct Message* msg, struct Iface* coreIf)
  221. {
  222. struct ControlHandler_pvt* ch = Identity_check((struct ControlHandler_pvt*) coreIf);
  223. struct RouteHeader routeHdr;
  224. Er_assert(Message_epop(msg, &routeHdr, RouteHeader_SIZE));
  225. uint8_t labelStr[20];
  226. uint64_t label = Endian_bigEndianToHost64(routeHdr.sh.label_be);
  227. AddrTools_printPath(labelStr, label);
  228. // happens in benchmark
  229. // Log_debug(ch->log, "ctrl packet from [%s]", labelStr);
  230. if (msg->length < 4 + Control_Header_SIZE) {
  231. Log_info(ch->log, "DROP runt ctrl packet from [%s]", labelStr);
  232. return Error(RUNT);
  233. }
  234. Assert_true(routeHdr.flags & RouteHeader_flags_CTRLMSG);
  235. if (Checksum_engine_be(msg->bytes, msg->length)) {
  236. Log_info(ch->log, "DROP ctrl packet from [%s] with invalid checksum", labelStr);
  237. return Error(INVALID);
  238. }
  239. struct Control* ctrl = (struct Control*) msg->bytes;
  240. if (ctrl->header.type_be == Control_ERROR_be) {
  241. return handleError(msg, ch, label, labelStr, &routeHdr);
  242. } else if (ctrl->header.type_be == Control_KEYPING_be
  243. || ctrl->header.type_be == Control_PING_be)
  244. {
  245. return handlePing(msg, ch, label, labelStr, ctrl->header.type_be);
  246. } else if (ctrl->header.type_be == Control_KEYPONG_be
  247. || ctrl->header.type_be == Control_PONG_be)
  248. {
  249. Log_debug(ch->log, "got switch pong from [%s]", labelStr);
  250. Er_assert(Message_epush(msg, &routeHdr, RouteHeader_SIZE));
  251. return Iface_next(&ch->pub.switchPingerIf, msg);
  252. } else if (ctrl->header.type_be == Control_GETSNODE_QUERY_be) {
  253. return handleGetSnodeQuery(msg, ch, label, labelStr);
  254. } else if (ctrl->header.type_be == Control_GETSNODE_REPLY_be
  255. || ctrl->header.type_be == Control_RPATH_REPLY_be)
  256. {
  257. Log_debug(ch->log, "got %s REPLY from [%s]",
  258. (ctrl->header.type_be == Control_GETSNODE_REPLY_be) ? "GETSNODE" : "RPATH",
  259. labelStr);
  260. Er_assert(Message_epush(msg, &routeHdr, RouteHeader_SIZE));
  261. Er_assert(Message_epush32be(msg, 0xffffffff));
  262. Er_assert(Message_epush32be(msg, PFChan_Core_CTRL_MSG));
  263. return Iface_next(&ch->eventIf, msg);
  264. } else if (ctrl->header.type_be == Control_RPATH_QUERY_be) {
  265. return handleRPathQuery(msg, ch, label, labelStr);
  266. }
  267. Log_info(ch->log, "DROP control packet of unknown type from [%s], type [%d]",
  268. labelStr, Endian_bigEndianToHost16(ctrl->header.type_be));
  269. return Error(INVALID);
  270. }
  271. // Forward from switch pinger directly to core.
  272. static Iface_DEFUN incomingFromSwitchPinger(struct Message* msg, struct Iface* switchPingerIf)
  273. {
  274. struct ControlHandler_pvt* ch =
  275. Identity_containerOf(switchPingerIf, struct ControlHandler_pvt, pub.switchPingerIf);
  276. return Iface_next(&ch->pub.coreIf, msg);
  277. }
  278. static Iface_DEFUN changeSnode(struct Message* msg, struct Iface* eventIf)
  279. {
  280. struct ControlHandler_pvt* ch =
  281. Identity_containerOf(eventIf, struct ControlHandler_pvt, eventIf);
  282. enum PFChan_Pathfinder ev = Er_assert(Message_epop32be(msg));
  283. Assert_true(ev == PFChan_Pathfinder_SNODE);
  284. Er_assert(Message_epop32be(msg));
  285. struct PFChan_Node node;
  286. Er_assert(Message_epop(msg, &node, PFChan_Node_SIZE));
  287. Assert_true(!msg->length);
  288. uint64_t path = Endian_bigEndianToHost64(node.path_be);
  289. uint32_t protocolVersion = Endian_bigEndianToHost32(node.version_be);
  290. const char* log = NULL;
  291. if (!ch->activeSnode.path) {
  292. if (node.path_be) {
  293. log = "Found snode";
  294. } else {
  295. // didn't know the snode before, still don't
  296. return Error(NONE);
  297. }
  298. } else if (!node.path_be) {
  299. // We had one, now we don't
  300. log = "Removing snode";
  301. } else {
  302. if (Bits_memcmp(ch->activeSnode.key, node.publicKey, 32)) {
  303. log = "Changing snode";
  304. } else if (path != ch->activeSnode.path) {
  305. log = "Changing snode path";
  306. } else if (ch->activeSnode.protocolVersion != protocolVersion) {
  307. log = "Changing snode protocolVersion";
  308. } else {
  309. // Nothing has changed
  310. return Error(NONE);
  311. }
  312. }
  313. struct Address old = {
  314. .protocolVersion = ch->activeSnode.protocolVersion,
  315. .path = ch->activeSnode.path,
  316. };
  317. Bits_memcpy(old.key, ch->activeSnode.key, 32);
  318. Bits_memcpy(ch->activeSnode.key, node.publicKey, 32);
  319. ch->activeSnode.path = path;
  320. ch->activeSnode.protocolVersion = protocolVersion;
  321. struct Address addr = { .protocolVersion = protocolVersion, .path = path, };
  322. Bits_memcpy(addr.key, node.publicKey, 32);
  323. Log_debug(ch->log, "%s [%s] -> [%s]", log,
  324. Address_toStringKey(&old, msg->alloc)->bytes,
  325. Address_toStringKey(&addr, msg->alloc)->bytes);
  326. return Error(NONE);
  327. }
  328. struct ControlHandler* ControlHandler_new(struct Allocator* allocator,
  329. struct Log* logger,
  330. struct EventEmitter* ee,
  331. uint8_t myPublicKey[32])
  332. {
  333. struct Allocator* alloc = Allocator_child(allocator);
  334. struct ControlHandler_pvt* ch = Allocator_calloc(alloc, sizeof(struct ControlHandler_pvt), 1);
  335. ch->alloc = alloc;
  336. ch->log = logger;
  337. Bits_memcpy(ch->myPublicKey, myPublicKey, 32);
  338. ch->pub.coreIf.send = incomingFromCore;
  339. ch->pub.switchPingerIf.send = incomingFromSwitchPinger;
  340. ch->eventIf.send = changeSnode;
  341. EventEmitter_regCore(ee, &ch->eventIf, PFChan_Pathfinder_SNODE);
  342. Identity_set(ch);
  343. return &ch->pub;
  344. }