UpperDistributor.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "crypto/AddressCalc.h"
  16. #include "dht/Address.h"
  17. #include "interface/Iface.h"
  18. #include "memory/Allocator.h"
  19. #include "util/Identity.h"
  20. #include "net/UpperDistributor.h"
  21. #include "net/SessionManager.h"
  22. #include "net/EventEmitter.h"
  23. #include "util/Checksum.h"
  24. #include "wire/DataHeader.h"
  25. #include "wire/RouteHeader.h"
  26. #include "wire/Headers.h"
  27. #include "wire/Error.h"
  28. struct UpperDistributor_Handler_pvt
  29. {
  30. struct UpperDistributor_Handler pub;
  31. struct Allocator* alloc;
  32. };
  33. #define Map_KEY_TYPE int
  34. #define Map_VALUE_TYPE struct UpperDistributor_Handler_pvt*
  35. #define Map_NAME OfHandlers
  36. #include "util/Map.h"
  37. struct UpperDistributor_pvt
  38. {
  39. struct UpperDistributor pub;
  40. struct Iface eventIf;
  41. struct Log* log;
  42. struct Address* myAddress;
  43. struct Map_OfHandlers* handlers;
  44. struct Allocator* alloc;
  45. int noSendToHandler;
  46. Identity
  47. };
  48. #define MAGIC_PORT 1
  49. static Iface_DEFUN incomingFromSessionManagerIf(struct Message*, struct Iface*);
  50. static Iface_DEFUN fromHandler(struct Message* msg, struct UpperDistributor_pvt* ud)
  51. {
  52. Er_assert(Message_epop(msg, NULL, RouteHeader_SIZE));
  53. struct DataHeader dh;
  54. Er_assert(Message_epop(msg, &dh, DataHeader_SIZE));
  55. enum ContentType type = DataHeader_getContentType(&dh);
  56. if (type != ContentType_IP6_UDP) {
  57. Log_debug(ud->log, "DROP Message from handler with invalid type [%d]", type);
  58. return Error(msg, "INVALID");
  59. }
  60. if (Message_getLength(msg) < Headers_UDPHeader_SIZE + RouteHeader_SIZE + DataHeader_SIZE) {
  61. Log_debug(ud->log, "DROP runt");
  62. return Error(msg, "RUNT");
  63. }
  64. uint8_t srcAndDest[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
  65. AddressCalc_makeValidAddress(&srcAndDest[16]);
  66. Bits_memcpy(srcAndDest, ud->myAddress->ip6.bytes, 16);
  67. struct Headers_UDPHeader* udp = (struct Headers_UDPHeader*) msg->msgbytes;
  68. if (Checksum_udpIp6_be(srcAndDest, msg->msgbytes, Message_getLength(msg))) {
  69. Log_debug(ud->log, "DROP Bad checksum");
  70. return Error(msg, "INVALID");
  71. }
  72. if (udp->destPort_be != Endian_bigEndianToHost16(MAGIC_PORT)) {
  73. Log_debug(ud->log, "DROP Message to unknown port [%d]",
  74. Endian_bigEndianToHost16(udp->destPort_be));
  75. return Error(msg, "INVALID");
  76. }
  77. int udpPort = Endian_bigEndianToHost16(udp->srcPort_be);
  78. int index = Map_OfHandlers_indexForKey(&udpPort, ud->handlers);
  79. if (index < 0) {
  80. Log_debug(ud->log, "DROP Message from unregistered port [%d]", udpPort);
  81. return Error(msg, "INVALID");
  82. }
  83. Er_assert(Message_epop(msg, NULL, Headers_UDPHeader_SIZE));
  84. Assert_true(Message_getLength(msg) >= RouteHeader_SIZE);
  85. struct RouteHeader* hdr = (struct RouteHeader*) msg->msgbytes;
  86. if (!Bits_memcmp(hdr->ip6, ud->myAddress->ip6.bytes, 16)) {
  87. ud->noSendToHandler = 1;
  88. Log_debug(ud->log, "Message to self");
  89. return incomingFromSessionManagerIf(msg, &ud->pub.sessionManagerIf);
  90. }
  91. struct DataHeader* dataHeader = (struct DataHeader*) &hdr[1];
  92. if (DataHeader_getContentType(dataHeader) == ContentType_CJDHT) {
  93. if (Bits_isZero(hdr->publicKey, 32)) {
  94. Log_debug(ud->log, "DROP message with no pubkey");
  95. return Error(msg, "INVALID");
  96. } else if (hdr->sh.label_be == 0) {
  97. Log_debug(ud->log, "DROP message with no label");
  98. return Error(msg, "INVALID");
  99. } else if (hdr->version_be == 0) {
  100. Log_debug(ud->log, "DROP message with no version");
  101. return Error(msg, "INVALID");
  102. }
  103. }
  104. return Iface_next(&ud->pub.sessionManagerIf, msg);
  105. }
  106. static void sendToHandlers(struct Message* msg,
  107. enum ContentType type,
  108. struct UpperDistributor_pvt* ud)
  109. {
  110. if (ud->noSendToHandler) {
  111. ud->noSendToHandler--;
  112. return;
  113. }
  114. for (int i = 0; i < (int)ud->handlers->count; i++) {
  115. if (ud->handlers->values[i]->pub.type != type) { continue; }
  116. struct Allocator* alloc = Allocator_child(Message_getAlloc(msg));
  117. struct Message* cmsg = Message_clone(msg, alloc);
  118. {
  119. struct Headers_UDPHeader udpH;
  120. udpH.srcPort_be = Endian_hostToBigEndian16(MAGIC_PORT);
  121. udpH.destPort_be = Endian_hostToBigEndian16(ud->handlers->values[i]->pub.udpPort);
  122. udpH.length_be = Endian_hostToBigEndian16(Message_getLength(cmsg) + Headers_UDPHeader_SIZE);
  123. udpH.checksum_be = 0;
  124. Er_assert(Message_epush(cmsg, &udpH, Headers_UDPHeader_SIZE));
  125. uint8_t srcAndDest[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
  126. AddressCalc_makeValidAddress(srcAndDest);
  127. Bits_memcpy(&srcAndDest[16], ud->myAddress->ip6.bytes, 16);
  128. uint16_t checksum_be = Checksum_udpIp6_be(srcAndDest, cmsg->msgbytes, Message_getLength(cmsg));
  129. ((struct Headers_UDPHeader*)cmsg->msgbytes)->checksum_be = checksum_be;
  130. }
  131. {
  132. struct DataHeader dh = { .unused = 0 };
  133. DataHeader_setVersion(&dh, DataHeader_CURRENT_VERSION);
  134. DataHeader_setContentType(&dh, ContentType_IP6_UDP);
  135. Er_assert(Message_epush(cmsg, &dh, DataHeader_SIZE));
  136. }
  137. {
  138. struct RouteHeader rh = {
  139. .version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL),
  140. .ip6 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}
  141. };
  142. AddressCalc_makeValidAddress(rh.ip6);
  143. Er_assert(Message_epush(cmsg, &rh, RouteHeader_SIZE));
  144. }
  145. Iface_send(&ud->pub.tunAdapterIf, cmsg);
  146. Allocator_free(alloc);
  147. }
  148. }
  149. static Iface_DEFUN toSessionManagerIf(struct Message* msg, struct UpperDistributor_pvt* ud)
  150. {
  151. Assert_true(Message_getLength(msg) >= RouteHeader_SIZE + DataHeader_SIZE);
  152. struct RouteHeader* hdr = (struct RouteHeader*) msg->msgbytes;
  153. struct DataHeader* dh = (struct DataHeader*) &hdr[1];
  154. enum ContentType type = DataHeader_getContentType(dh);
  155. sendToHandlers(msg, type, ud);
  156. return Iface_next(&ud->pub.sessionManagerIf, msg);
  157. }
  158. static Iface_DEFUN incomingFromEventIf(struct Message* msg, struct Iface* eventIf)
  159. {
  160. struct UpperDistributor_pvt* ud =
  161. Identity_containerOf(eventIf, struct UpperDistributor_pvt, eventIf);
  162. uint32_t messageType = Er_assert(Message_epop32be(msg));
  163. Assert_true(messageType == PFChan_Pathfinder_SENDMSG ||
  164. messageType == PFChan_Pathfinder_CTRL_SENDMSG);
  165. Er_assert(Message_epop32be(msg));
  166. return toSessionManagerIf(msg, ud);
  167. }
  168. static Iface_DEFUN incomingFromTunAdapterIf(struct Message* msg, struct Iface* tunAdapterIf)
  169. {
  170. struct UpperDistributor_pvt* ud =
  171. Identity_containerOf(tunAdapterIf, struct UpperDistributor_pvt, pub.tunAdapterIf);
  172. struct RouteHeader* rh = (struct RouteHeader*) msg->msgbytes;
  173. Assert_true(Message_getLength(msg) >= RouteHeader_SIZE);
  174. uint8_t expected_ip6[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
  175. AddressCalc_makeValidAddress(expected_ip6);
  176. if (!Bits_memcmp(rh->ip6, expected_ip6, 16)) {
  177. return fromHandler(msg, ud);
  178. }
  179. return toSessionManagerIf(msg, ud);
  180. }
  181. static Iface_DEFUN incomingFromIpTunnelIf(struct Message* msg, struct Iface* ipTunnelIf)
  182. {
  183. struct UpperDistributor_pvt* ud =
  184. Identity_containerOf(ipTunnelIf, struct UpperDistributor_pvt, pub.ipTunnelIf);
  185. return toSessionManagerIf(msg, ud);
  186. }
  187. static Iface_DEFUN incomingFromControlHandlerIf(struct Message* msg, struct Iface* iface)
  188. {
  189. struct UpperDistributor_pvt* ud =
  190. Identity_containerOf(iface, struct UpperDistributor_pvt, pub.controlHandlerIf);
  191. Assert_true(Message_getLength(msg) >= RouteHeader_SIZE);
  192. struct RouteHeader* hdr = (struct RouteHeader*) msg->msgbytes;
  193. Assert_true(hdr->flags & RouteHeader_flags_CTRLMSG);
  194. Assert_true(!(hdr->flags & RouteHeader_flags_INCOMING));
  195. sendToHandlers(msg, ContentType_CTRL, ud);
  196. return Iface_next(&ud->pub.sessionManagerIf, msg);
  197. }
  198. static Iface_DEFUN incomingFromSessionManagerIf(struct Message* msg, struct Iface* sessionManagerIf)
  199. {
  200. struct UpperDistributor_pvt* ud =
  201. Identity_containerOf(sessionManagerIf, struct UpperDistributor_pvt, pub.sessionManagerIf);
  202. Assert_true(Message_getLength(msg) >= RouteHeader_SIZE);
  203. struct RouteHeader* hdr = (struct RouteHeader*) msg->msgbytes;
  204. if (hdr->flags & RouteHeader_flags_CTRLMSG) {
  205. sendToHandlers(msg, ContentType_CTRL, ud);
  206. return Iface_next(&ud->pub.controlHandlerIf, msg);
  207. }
  208. Assert_true(Message_getLength(msg) >= RouteHeader_SIZE + DataHeader_SIZE);
  209. struct DataHeader* dh = (struct DataHeader*) &hdr[1];
  210. enum ContentType type = DataHeader_getContentType(dh);
  211. sendToHandlers(msg, type, ud);
  212. if (type <= ContentType_IP6_MAX) {
  213. return Iface_next(&ud->pub.tunAdapterIf, msg);
  214. }
  215. if (type == ContentType_CJDHT) {
  216. Er_assert(Message_epush32be(msg, 0xffffffff));
  217. Er_assert(Message_epush32be(msg, PFChan_Core_MSG));
  218. return Iface_next(&ud->eventIf, msg);
  219. }
  220. if (type == ContentType_IPTUN) {
  221. return Iface_next(&ud->pub.ipTunnelIf, msg);
  222. }
  223. Log_debug(ud->log, "DROP message with unknown type [%d]", type);
  224. return Error(msg, "INVALID");
  225. }
  226. int UpperDistributor_unregisterHandler(struct UpperDistributor* upper, int udpPort)
  227. {
  228. struct UpperDistributor_pvt* ud = Identity_check((struct UpperDistributor_pvt*) upper);
  229. int index = Map_OfHandlers_indexForKey(&udpPort, ud->handlers);
  230. if (index < 0) {
  231. return UpperDistributor_unregisterHandler_NONEXISTANT;
  232. }
  233. struct UpperDistributor_Handler_pvt* udhp = ud->handlers->values[index];
  234. Map_OfHandlers_remove(index, ud->handlers);
  235. Allocator_free(udhp->alloc);
  236. return 0;
  237. }
  238. int UpperDistributor_listHandlers(struct UpperDistributor* ud,
  239. struct UpperDistributor_Handler** outputList,
  240. struct Allocator* alloc)
  241. {
  242. struct UpperDistributor_pvt* udp = Identity_check((struct UpperDistributor_pvt*) ud);
  243. if (!udp->handlers->count) {
  244. *outputList = NULL;
  245. return 0;
  246. }
  247. struct UpperDistributor_Handler* out = *outputList =
  248. Allocator_calloc(alloc, sizeof(struct UpperDistributor_Handler), udp->handlers->count);
  249. for (int i = 0; i < (int)udp->handlers->count; i++) {
  250. Bits_memcpy(&out[i], udp->handlers->values[i], sizeof(struct UpperDistributor_Handler));
  251. }
  252. return udp->handlers->count;
  253. }
  254. int UpperDistributor_registerHandler(struct UpperDistributor* upper,
  255. enum ContentType ct,
  256. int udpPort)
  257. {
  258. struct UpperDistributor_pvt* ud = Identity_check((struct UpperDistributor_pvt*) upper);
  259. int index = Map_OfHandlers_indexForKey(&udpPort, ud->handlers);
  260. if (index >= 0) {
  261. return UpperDistributor_registerHandler_PORT_REGISTERED;
  262. }
  263. struct Allocator* alloc = Allocator_child(ud->alloc);
  264. struct UpperDistributor_Handler_pvt* udhp =
  265. Allocator_calloc(alloc, sizeof(struct UpperDistributor_Handler_pvt), 1);
  266. udhp->alloc = alloc;
  267. udhp->pub.udpPort = udpPort;
  268. udhp->pub.type = ct;
  269. Assert_true(Map_OfHandlers_put(&udpPort, &udhp, ud->handlers) >= 0);
  270. return 0;
  271. }
  272. struct UpperDistributor* UpperDistributor_new(struct Allocator* allocator,
  273. struct Log* log,
  274. struct EventEmitter* ee,
  275. struct Address* myAddress)
  276. {
  277. struct Allocator* alloc = Allocator_child(allocator);
  278. struct UpperDistributor_pvt* out =
  279. Allocator_calloc(alloc, sizeof(struct UpperDistributor_pvt), 1);
  280. Identity_set(out);
  281. out->handlers = Map_OfHandlers_new(alloc);
  282. out->eventIf.send = incomingFromEventIf;
  283. out->pub.tunAdapterIf.send = incomingFromTunAdapterIf;
  284. out->pub.ipTunnelIf.send = incomingFromIpTunnelIf;
  285. out->pub.sessionManagerIf.send = incomingFromSessionManagerIf;
  286. out->pub.controlHandlerIf.send = incomingFromControlHandlerIf;
  287. out->log = log;
  288. out->alloc = alloc;
  289. out->myAddress = myAddress;
  290. EventEmitter_regCore(ee, &out->eventIf, PFChan_Pathfinder_SENDMSG);
  291. EventEmitter_regCore(ee, &out->eventIf, PFChan_Pathfinder_CTRL_SENDMSG);
  292. return &out->pub;
  293. }