ControlHandler.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 "net/ControlHandler.h"
  16. #include "util/Identity.h"
  17. #include "util/AddrTools.h"
  18. #include "util/Checksum.h"
  19. #include "wire/Control.h"
  20. #include "wire/Error.h"
  21. #include "switch/NumberCompress.h"
  22. struct ControlHandler_pvt
  23. {
  24. struct ControlHandler pub;
  25. struct Log* log;
  26. struct Allocator* alloc;
  27. struct Router* router;
  28. uint8_t myPublicKey[32];
  29. struct Iface eventIf;
  30. Identity
  31. };
  32. /**
  33. * Expects [ Ctrl ][ Error ][ cause SwitchHeader ][ cause handle ][ cause etc.... ]
  34. */
  35. #define handleError_MIN_SIZE (Control_Header_SIZE + Control_Error_MIN_SIZE + SwitchHeader_SIZE + 4)
  36. static Iface_DEFUN handleError(struct Message* msg,
  37. struct ControlHandler_pvt* ch,
  38. uint64_t label,
  39. uint8_t* labelStr)
  40. {
  41. if (msg->length < handleError_MIN_SIZE) {
  42. Log_info(ch->log, "DROP runt error packet from [%s]", labelStr);
  43. return NULL;
  44. }
  45. Message_shift(msg, SwitchHeader_SIZE + 4, NULL);
  46. Message_push32(msg, 0xffffffff, NULL);
  47. Message_push32(msg, PFChan_Core_SWITCH_ERR, NULL);
  48. return Iface_next(&ch->eventIf, msg);
  49. }
  50. /**
  51. * Expects [ SwitchHeader ][ Ctrl ][ (key)Ping ][ data etc.... ]
  52. */
  53. #define handlePing_MIN_SIZE (Control_Header_SIZE + Control_Ping_MIN_SIZE)
  54. static Iface_DEFUN handlePing(struct Message* msg,
  55. struct ControlHandler_pvt* ch,
  56. uint64_t label,
  57. uint8_t* labelStr,
  58. uint16_t messageType_be)
  59. {
  60. if (msg->length < handlePing_MIN_SIZE) {
  61. Log_info(ch->log, "DROP runt ping");
  62. return NULL;
  63. }
  64. struct Control* ctrl = (struct Control*) msg->bytes;
  65. Message_shift(msg, -Control_Header_SIZE, NULL);
  66. // Ping and keyPing share version location
  67. struct Control_Ping* ping = (struct Control_Ping*) msg->bytes;
  68. uint32_t herVersion = Endian_bigEndianToHost32(ping->version_be);
  69. if (!Version_isCompatible(Version_CURRENT_PROTOCOL, herVersion)) {
  70. Log_debug(ch->log, "DROP ping from incompatible version [%d]", herVersion);
  71. return NULL;
  72. }
  73. if (messageType_be == Control_KEYPING_be) {
  74. Log_debug(ch->log, "got switch keyPing from [%s]", labelStr);
  75. if (msg->length < Control_KeyPing_HEADER_SIZE) {
  76. // min keyPing size is longer
  77. Log_debug(ch->log, "DROP runt keyPing");
  78. return NULL;
  79. }
  80. if (msg->length > Control_KeyPing_MAX_SIZE) {
  81. Log_debug(ch->log, "DROP long keyPing");
  82. return NULL;
  83. }
  84. if (ping->magic != Control_KeyPing_MAGIC) {
  85. Log_debug(ch->log, "DROP keyPing (bad magic)");
  86. return NULL;
  87. }
  88. struct Control_KeyPing* keyPing = (struct Control_KeyPing*) msg->bytes;
  89. keyPing->magic = Control_KeyPong_MAGIC;
  90. ctrl->header.type_be = Control_KEYPONG_be;
  91. Bits_memcpyConst(keyPing->key, ch->myPublicKey, 32);
  92. } else if (messageType_be == Control_PING_be) {
  93. Log_debug(ch->log, "got switch ping from [%s]", labelStr);
  94. if (ping->magic != Control_Ping_MAGIC) {
  95. Log_debug(ch->log, "DROP ping (bad magic)");
  96. return NULL;
  97. }
  98. ping->magic = Control_Pong_MAGIC;
  99. ctrl->header.type_be = Control_PONG_be;
  100. } else {
  101. Assert_failure("2+2=5");
  102. }
  103. ping->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
  104. Message_shift(msg, Control_Header_SIZE, NULL);
  105. ctrl->header.checksum_be = 0;
  106. ctrl->header.checksum_be = Checksum_engine(msg->bytes, msg->length);
  107. Message_push32(msg, 0xffffffff, NULL);
  108. Message_shift(msg, SwitchHeader_SIZE, NULL);
  109. struct SwitchHeader* switchHeader = (struct SwitchHeader*) msg->bytes;
  110. Bits_memset(switchHeader, 0, SwitchHeader_SIZE);
  111. SwitchHeader_setVersion(switchHeader, SwitchHeader_CURRENT_VERSION);
  112. switchHeader->label_be = Endian_hostToBigEndian64(label);
  113. return Iface_next(&ch->pub.coreIf, msg);
  114. }
  115. /**
  116. * Handle an incoming control message from a switch.
  117. *
  118. * @param context the ducttape context.
  119. * @param message the control message, this should be alligned on the beginning of the content,
  120. * that is to say, after the end of the switch header.
  121. * @param switchHeader the header.
  122. * @param switchIf the interface which leads to the switch.
  123. * @param isFormV8 true if the control message is in the form specified by protocol version 8+
  124. */
  125. static Iface_DEFUN incomingFromCore(struct Message* msg, struct Iface* coreIf)
  126. {
  127. struct ControlHandler_pvt* ch = Identity_check((struct ControlHandler_pvt*) coreIf);
  128. struct SwitchHeader switchHeader;
  129. Message_pop(msg, &switchHeader, SwitchHeader_SIZE, NULL);
  130. uint8_t labelStr[20];
  131. uint64_t label = Endian_bigEndianToHost64(switchHeader.label_be);
  132. AddrTools_printPath(labelStr, label);
  133. Log_debug(ch->log, "ctrl packet from [%s]", labelStr);
  134. if (msg->length < 4 + Control_Header_SIZE) {
  135. Log_info(ch->log, "DROP runt ctrl packet from [%s]", labelStr);
  136. return NULL;
  137. }
  138. Assert_true(Message_pop32(msg, NULL) == 0xffffffff);
  139. if (Checksum_engine(msg->bytes, msg->length)) {
  140. Log_info(ch->log, "DROP ctrl packet from [%s] with invalid checksum", labelStr);
  141. return NULL;
  142. }
  143. struct Control* ctrl = (struct Control*) msg->bytes;
  144. if (ctrl->header.type_be == Control_ERROR_be) {
  145. return handleError(msg, ch, label, labelStr);
  146. } else if (ctrl->header.type_be == Control_KEYPING_be
  147. || ctrl->header.type_be == Control_PING_be)
  148. {
  149. return handlePing(msg, ch, label, labelStr, ctrl->header.type_be);
  150. } else if (ctrl->header.type_be == Control_KEYPONG_be
  151. || ctrl->header.type_be == Control_PONG_be)
  152. {
  153. Log_debug(ch->log, "got switch pong from [%s]", labelStr);
  154. // Shift back over the header
  155. Message_shift(msg, 4 + SwitchHeader_SIZE, NULL);
  156. return Iface_next(&ch->pub.switchPingerIf, msg);
  157. }
  158. Log_info(ch->log, "DROP control packet of unknown type from [%s], type [%d]",
  159. labelStr, Endian_bigEndianToHost16(ctrl->header.type_be));
  160. return NULL;
  161. }
  162. // Forward from switch pinger directly to core.
  163. static Iface_DEFUN incomingFromSwitchPinger(struct Message* msg, struct Iface* switchPingerIf)
  164. {
  165. struct ControlHandler_pvt* ch =
  166. Identity_containerOf(switchPingerIf, struct ControlHandler_pvt, pub.switchPingerIf);
  167. return Iface_next(&ch->pub.coreIf, msg);
  168. }
  169. struct ControlHandler* ControlHandler_new(struct Allocator* allocator,
  170. struct Log* logger,
  171. struct EventEmitter* ee,
  172. uint8_t myPublicKey[32])
  173. {
  174. struct Allocator* alloc = Allocator_child(allocator);
  175. struct ControlHandler_pvt* ch = Allocator_calloc(alloc, sizeof(struct ControlHandler_pvt), 1);
  176. ch->alloc = alloc;
  177. ch->log = logger;
  178. Bits_memcpyConst(ch->myPublicKey, myPublicKey, 32);
  179. ch->pub.coreIf.send = incomingFromCore;
  180. ch->pub.switchPingerIf.send = incomingFromSwitchPinger;
  181. EventEmitter_regCore(ee, &ch->eventIf, 0);
  182. Identity_set(ch);
  183. return &ch->pub;
  184. }