UpperDistributor.c 12 KB

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