ControlHandler.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 NULL;
  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 NULL;
  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 NULL;
  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 NULL;
  83. }
  84. if (msg->length > Control_KeyPing_MAX_SIZE) {
  85. Log_debug(ch->log, "DROP long keyPing");
  86. return NULL;
  87. }
  88. if (ping->magic != Control_KeyPing_MAGIC) {
  89. Log_debug(ch->log, "DROP keyPing (bad magic)");
  90. return NULL;
  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 NULL;
  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(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 NULL;
  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 NULL;
  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(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 NULL;
  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 NULL;
  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. return NULL;
  179. }
  180. ctrl->header.type_be = Control_GETSNODE_REPLY_be;
  181. snq->kbps_be = 0xffffffff;
  182. snq->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
  183. snq->magic = Control_GETSNODE_REPLY_MAGIC;
  184. uint64_t fixedLabel = 0;
  185. if (ch->activeSnode.path) {
  186. fixedLabel = NumberCompress_getLabelFor(ch->activeSnode.path, label);
  187. uint64_t fixedLabel_be = Endian_hostToBigEndian64(fixedLabel);
  188. Bits_memcpy(snq->pathToSnode_be, &fixedLabel_be, 8);
  189. Bits_memcpy(snq->snodeKey, ch->activeSnode.key, 32);
  190. snq->snodeVersion_be = Endian_hostToBigEndian32(ch->activeSnode.protocolVersion);
  191. } else {
  192. Log_debug(ch->log, "getSupernode query and we do not have a known snode");
  193. snq->snodeVersion_be = 0;
  194. Bits_memset(snq->pathToSnode_be, 0, 8);
  195. Bits_memset(snq->snodeKey, 0, 32);
  196. }
  197. ctrl->header.checksum_be = 0;
  198. ctrl->header.checksum_be = Checksum_engine(msg->bytes, msg->length);
  199. Er_assert(Message_eshift(msg, RouteHeader_SIZE));
  200. struct RouteHeader* routeHeader = (struct RouteHeader*) msg->bytes;
  201. Bits_memset(routeHeader, 0, RouteHeader_SIZE);
  202. SwitchHeader_setVersion(&routeHeader->sh, SwitchHeader_CURRENT_VERSION);
  203. routeHeader->sh.label_be = Endian_hostToBigEndian64(label);
  204. routeHeader->flags |= RouteHeader_flags_CTRLMSG;
  205. Log_debug(ch->log, "Replied to [%" PRIx64 "] with snode [%" PRIx64 "] aka [%" PRIx64 "]",
  206. label, fixedLabel, ch->activeSnode.path);
  207. return Iface_next(&ch->pub.coreIf, msg);
  208. }
  209. /**
  210. * Handle an incoming control message from a switch.
  211. *
  212. * @param context the ducttape context.
  213. * @param message the control message, this should be alligned on the beginning of the content,
  214. * that is to say, after the end of the switch header.
  215. * @param switchHeader the header.
  216. * @param switchIf the interface which leads to the switch.
  217. * @param isFormV8 true if the control message is in the form specified by protocol version 8+
  218. */
  219. static Iface_DEFUN incomingFromCore(struct Message* msg, struct Iface* coreIf)
  220. {
  221. struct ControlHandler_pvt* ch = Identity_check((struct ControlHandler_pvt*) coreIf);
  222. struct RouteHeader routeHdr;
  223. Er_assert(Message_epop(msg, &routeHdr, RouteHeader_SIZE));
  224. uint8_t labelStr[20];
  225. uint64_t label = Endian_bigEndianToHost64(routeHdr.sh.label_be);
  226. AddrTools_printPath(labelStr, label);
  227. // happens in benchmark
  228. // Log_debug(ch->log, "ctrl packet from [%s]", labelStr);
  229. if (msg->length < 4 + Control_Header_SIZE) {
  230. Log_info(ch->log, "DROP runt ctrl packet from [%s]", labelStr);
  231. return NULL;
  232. }
  233. Assert_true(routeHdr.flags & RouteHeader_flags_CTRLMSG);
  234. if (Checksum_engine(msg->bytes, msg->length)) {
  235. Log_info(ch->log, "DROP ctrl packet from [%s] with invalid checksum", labelStr);
  236. return NULL;
  237. }
  238. struct Control* ctrl = (struct Control*) msg->bytes;
  239. if (ctrl->header.type_be == Control_ERROR_be) {
  240. return handleError(msg, ch, label, labelStr, &routeHdr);
  241. } else if (ctrl->header.type_be == Control_KEYPING_be
  242. || ctrl->header.type_be == Control_PING_be)
  243. {
  244. return handlePing(msg, ch, label, labelStr, ctrl->header.type_be);
  245. } else if (ctrl->header.type_be == Control_KEYPONG_be
  246. || ctrl->header.type_be == Control_PONG_be)
  247. {
  248. Log_debug(ch->log, "got switch pong from [%s]", labelStr);
  249. Er_assert(Message_epush(msg, &routeHdr, RouteHeader_SIZE));
  250. return Iface_next(&ch->pub.switchPingerIf, msg);
  251. } else if (ctrl->header.type_be == Control_GETSNODE_QUERY_be) {
  252. return handleGetSnodeQuery(msg, ch, label, labelStr);
  253. } else if (ctrl->header.type_be == Control_GETSNODE_REPLY_be
  254. || ctrl->header.type_be == Control_RPATH_REPLY_be)
  255. {
  256. Log_debug(ch->log, "got %s REPLY from [%s]",
  257. (ctrl->header.type_be == Control_GETSNODE_REPLY_be) ? "GETSNODE" : "RPATH",
  258. labelStr);
  259. Er_assert(Message_epush(msg, &routeHdr, RouteHeader_SIZE));
  260. Er_assert(Message_epush32be(msg, 0xffffffff));
  261. Er_assert(Message_epush32be(msg, PFChan_Core_CTRL_MSG));
  262. return Iface_next(&ch->eventIf, msg);
  263. } else if (ctrl->header.type_be == Control_RPATH_QUERY_be) {
  264. return handleRPathQuery(msg, ch, label, labelStr);
  265. }
  266. Log_info(ch->log, "DROP control packet of unknown type from [%s], type [%d]",
  267. labelStr, Endian_bigEndianToHost16(ctrl->header.type_be));
  268. return NULL;
  269. }
  270. // Forward from switch pinger directly to core.
  271. static Iface_DEFUN incomingFromSwitchPinger(struct Message* msg, struct Iface* switchPingerIf)
  272. {
  273. struct ControlHandler_pvt* ch =
  274. Identity_containerOf(switchPingerIf, struct ControlHandler_pvt, pub.switchPingerIf);
  275. return Iface_next(&ch->pub.coreIf, msg);
  276. }
  277. static Iface_DEFUN changeSnode(struct Message* msg, struct Iface* eventIf)
  278. {
  279. struct ControlHandler_pvt* ch =
  280. Identity_containerOf(eventIf, struct ControlHandler_pvt, eventIf);
  281. enum PFChan_Pathfinder ev = Er_assert(Message_epop32be(msg));
  282. Assert_true(ev == PFChan_Pathfinder_SNODE);
  283. Er_assert(Message_epop32be(msg));
  284. struct PFChan_Node node;
  285. Er_assert(Message_epop(msg, &node, PFChan_Node_SIZE));
  286. Assert_true(!msg->length);
  287. uint64_t path = Endian_bigEndianToHost64(node.path_be);
  288. uint32_t protocolVersion = Endian_bigEndianToHost32(node.version_be);
  289. const char* log = NULL;
  290. if (!ch->activeSnode.path) {
  291. if (node.path_be) {
  292. log = "Found snode";
  293. } else {
  294. // didn't know the snode before, still don't
  295. return NULL;
  296. }
  297. } else if (!node.path_be) {
  298. // We had one, now we don't
  299. log = "Removing snode";
  300. } else {
  301. if (Bits_memcmp(ch->activeSnode.key, node.publicKey, 32)) {
  302. log = "Changing snode";
  303. } else if (path != ch->activeSnode.path) {
  304. log = "Changing snode path";
  305. } else if (ch->activeSnode.protocolVersion != protocolVersion) {
  306. log = "Changing snode protocolVersion";
  307. } else {
  308. // Nothing has changed
  309. return NULL;
  310. }
  311. }
  312. struct Address old = {
  313. .protocolVersion = ch->activeSnode.protocolVersion,
  314. .path = ch->activeSnode.path,
  315. };
  316. Bits_memcpy(old.key, ch->activeSnode.key, 32);
  317. Bits_memcpy(ch->activeSnode.key, node.publicKey, 32);
  318. ch->activeSnode.path = path;
  319. ch->activeSnode.protocolVersion = protocolVersion;
  320. struct Address addr = { .protocolVersion = protocolVersion, .path = path, };
  321. Bits_memcpy(addr.key, node.publicKey, 32);
  322. Log_debug(ch->log, "%s [%s] -> [%s]", log,
  323. Address_toStringKey(&old, msg->alloc)->bytes,
  324. Address_toStringKey(&addr, msg->alloc)->bytes);
  325. return NULL;
  326. }
  327. struct ControlHandler* ControlHandler_new(struct Allocator* allocator,
  328. struct Log* logger,
  329. struct EventEmitter* ee,
  330. uint8_t myPublicKey[32])
  331. {
  332. struct Allocator* alloc = Allocator_child(allocator);
  333. struct ControlHandler_pvt* ch = Allocator_calloc(alloc, sizeof(struct ControlHandler_pvt), 1);
  334. ch->alloc = alloc;
  335. ch->log = logger;
  336. Bits_memcpy(ch->myPublicKey, myPublicKey, 32);
  337. ch->pub.coreIf.send = incomingFromCore;
  338. ch->pub.switchPingerIf.send = incomingFromSwitchPinger;
  339. ch->eventIf.send = changeSnode;
  340. EventEmitter_regCore(ee, &ch->eventIf, PFChan_Pathfinder_SNODE);
  341. Identity_set(ch);
  342. return &ch->pub;
  343. }