1
0

UDPInterface.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 "rust/cjdns_sys/Rffi.h"
  16. #include "benc/StringList.h"
  17. #include "interface/UDPInterface.h"
  18. #include "wire/Message.h"
  19. #include "util/events/UDPAddrIface.h"
  20. #include "util/GlobalConfig.h"
  21. #include "wire/Error.h"
  22. #include <netinet/in.h>
  23. #include <string.h>
  24. #define ArrayList_TYPE struct Sockaddr
  25. #define ArrayList_NAME Sockaddr
  26. #include "util/ArrayList.h"
  27. struct UDPInterface_pvt
  28. {
  29. struct UDPInterface pub;
  30. struct Log* log;
  31. struct Allocator* allocator;
  32. Iface_t iface;
  33. struct Allocator* bcastAddrAlloc;
  34. struct ArrayList_Sockaddr* bcastAddrs;
  35. struct Allocator* bcastIfaceAlloc;
  36. struct StringList* bcastIfaces;
  37. struct UDPAddrIface* commIf;
  38. struct UDPAddrIface* bcastIf;
  39. struct GlobalConfig* globalConf;
  40. struct Iface commSock;
  41. struct Iface bcastSock;
  42. uint16_t beaconPort_be;
  43. uint16_t commPort_be;
  44. Identity
  45. };
  46. static struct Sockaddr* mkBcastAddr(
  47. uint16_t beaconPort_be,
  48. const Rffi_NetworkInterface* iface,
  49. struct Allocator* alloc)
  50. {
  51. uint32_t addr; memcpy(&addr, iface->address.octets, 4);
  52. uint32_t nmAddr; memcpy(&nmAddr, iface->address.netmask, 4);
  53. uint32_t bcastAddr = ( addr & nmAddr ) | ~nmAddr;
  54. uint8_t bcastBytes[4]; memcpy(bcastBytes, &bcastAddr, 4);
  55. struct Sockaddr* out = Sockaddr_fromBytes(bcastBytes, Sockaddr_AF_INET, alloc);
  56. Sockaddr_setPort(out, Endian_bigEndianToHost16(beaconPort_be));
  57. return out;
  58. }
  59. static int updateBcastAddrs(struct UDPInterface_pvt* ctx)
  60. {
  61. bool all = false;
  62. for (int i = 0; ctx->bcastIfaces && i < ctx->bcastIfaces->length; i++) {
  63. String* iface = StringList_get(ctx->bcastIfaces, i);
  64. if (String_equals(iface, String_CONST("all"))) { all = true; }
  65. }
  66. struct Allocator* tmpAlloc = Allocator_child(ctx->allocator);
  67. const Rffi_NetworkInterface* interfaces;
  68. int count = Rffi_interface_addresses(&interfaces, tmpAlloc);
  69. if (ctx->bcastAddrAlloc) { Allocator_free(ctx->bcastAddrAlloc); }
  70. struct Allocator* alloc = ctx->bcastAddrAlloc = Allocator_child(ctx->allocator);
  71. ctx->bcastAddrs = ArrayList_Sockaddr_new(alloc);
  72. String* tunDev = GlobalConfig_getTunName(ctx->globalConf);
  73. for (int i = 0; i < count; i++) {
  74. if (interfaces[i].is_internal) { continue; }
  75. if (interfaces[i].address.is_ipv6) { continue; }
  76. if (tunDev && !CString_strncmp(interfaces[i].name, tunDev->bytes, tunDev->len)) {
  77. continue;
  78. }
  79. struct Sockaddr* addr = mkBcastAddr(ctx->beaconPort_be, &interfaces[i], alloc);
  80. if (!all) {
  81. String* addrStr = String_new(Sockaddr_print(addr, alloc), alloc);
  82. bool found = false;
  83. for (int j = 0; ctx->bcastIfaces && j < ctx->bcastIfaces->length; j++) {
  84. String* iface = StringList_get(ctx->bcastIfaces, j);
  85. if (String_equals(iface, addrStr)) { found = true; }
  86. if (CString_strncmp(iface->bytes, interfaces[i].name, iface->len)) { found = true; }
  87. }
  88. if (!found) { continue; }
  89. }
  90. ArrayList_Sockaddr_add(ctx->bcastAddrs, addr);
  91. }
  92. Allocator_free(tmpAlloc);
  93. return 0;
  94. }
  95. static Iface_DEFUN sendPacket(struct Message* m, struct Iface* iface)
  96. {
  97. struct UDPInterface_pvt* ctx =
  98. Identity_containerOf(iface, struct UDPInterface_pvt, iface);
  99. Assert_true(Message_getLength(m) > Sockaddr_OVERHEAD);
  100. struct Sockaddr* sa = (struct Sockaddr*) m->msgbytes;
  101. Assert_true(Message_getLength(m) > sa->addrLen);
  102. // Regular traffic
  103. if (!(sa->flags & Sockaddr_flags_BCAST)) { return Iface_next(&ctx->commSock, m); }
  104. if (updateBcastAddrs(ctx)) {
  105. return Error(m, "updateBcastAddrs check logs");
  106. }
  107. // bcast
  108. struct UDPInterface_BroadcastHeader hdr = {
  109. .fffffffc_be = Endian_hostToBigEndian32(0xfffffffc),
  110. .version = UDPInterface_CURRENT_VERSION,
  111. .zero = 0,
  112. .commPort_be = ctx->commPort_be
  113. };
  114. Er_assert(Message_eshift(m, -sa->addrLen));
  115. Er_assert(Message_epush(m, &hdr, UDPInterface_BroadcastHeader_SIZE));
  116. for (int i = 0; i < ctx->bcastAddrs->length; i++) {
  117. struct Allocator* tmpAlloc = Allocator_child(ctx->allocator);
  118. struct Message* mm = Message_clone(m, tmpAlloc);
  119. struct Sockaddr* addr = ArrayList_Sockaddr_get(ctx->bcastAddrs, i);
  120. Er_assert(Message_epush(mm, addr, addr->addrLen));
  121. Iface_send(&ctx->bcastSock, mm);
  122. Allocator_free(tmpAlloc);
  123. }
  124. return NULL;
  125. }
  126. static Iface_DEFUN fromCommSock(struct Message* m, struct Iface* iface)
  127. {
  128. struct UDPInterface_pvt* ctx =
  129. Identity_containerOf(iface, struct UDPInterface_pvt, commSock);
  130. return Iface_next(ctx->pub.generic.iface, m);
  131. }
  132. static Iface_DEFUN fromBcastSock(struct Message* m, struct Iface* iface)
  133. {
  134. struct UDPInterface_pvt* ctx =
  135. Identity_containerOf(iface, struct UDPInterface_pvt, bcastSock);
  136. if (Message_getLength(m) < UDPInterface_BroadcastHeader_SIZE + Sockaddr_OVERHEAD) {
  137. Log_debug(ctx->log, "DROP runt bcast");
  138. return Error(m, "RUNT bcast");
  139. }
  140. struct Sockaddr_storage ss;
  141. Er_assert(Message_epop(m, &ss, Sockaddr_OVERHEAD));
  142. if (Message_getLength(m) < UDPInterface_BroadcastHeader_SIZE + ss.addr.addrLen - Sockaddr_OVERHEAD) {
  143. Log_debug(ctx->log, "DROP runt bcast");
  144. return Error(m, "RUNT bcast");
  145. }
  146. Er_assert(Message_epop(m, &ss.nativeAddr, ss.addr.addrLen - Sockaddr_OVERHEAD));
  147. struct UDPInterface_BroadcastHeader hdr;
  148. Er_assert(Message_epop(m, &hdr, UDPInterface_BroadcastHeader_SIZE));
  149. if (hdr.fffffffc_be != Endian_hostToBigEndian32(0xfffffffc)) {
  150. Log_debug(ctx->log, "DROP bcast bad magic, expected 0xfffffffc got [%08x]",
  151. Endian_bigEndianToHost32(hdr.fffffffc_be));
  152. return Error(m, "INVALID bcast, bad magic");
  153. }
  154. if (hdr.version != UDPInterface_CURRENT_VERSION) {
  155. Log_debug(ctx->log, "DROP bcast bad version [%u]", hdr.version);
  156. return Error(m, "INVALID bcast, bad version");
  157. }
  158. if (hdr.zero) {
  159. Log_debug(ctx->log, "DROP bcast malformed (zero not zero)");
  160. return Error(m, "INVALID bcast, hdr.zero isn't 0");
  161. }
  162. uint16_t commPort = Endian_bigEndianToHost16(hdr.commPort_be);
  163. // Fake that it came from the communication port
  164. Sockaddr_setPort(&ss.addr, commPort);
  165. ss.addr.flags |= Sockaddr_flags_BCAST;
  166. Er_assert(Message_epush(m, &ss.addr, ss.addr.addrLen));
  167. return Iface_next(ctx->pub.generic.iface, m);
  168. }
  169. Er_DEFUN(struct UDPInterface* UDPInterface_new(struct EventBase* eventBase,
  170. struct Sockaddr* bindAddr,
  171. uint16_t beaconPort,
  172. struct Allocator* alloc,
  173. struct Log* logger,
  174. struct GlobalConfig* globalConf))
  175. {
  176. if (beaconPort && Sockaddr_getFamily(bindAddr) != Sockaddr_AF_INET) {
  177. Er_raise(alloc, "UDP broadcast only supported by ipv4.");
  178. }
  179. if (beaconPort && Sockaddr_getPort(bindAddr) == beaconPort) {
  180. Er_raise(alloc, "UDP broadcast port must be different from communication port.");
  181. }
  182. struct UDPAddrIface* uai = Er(UDPAddrIface_new(eventBase, bindAddr, alloc, logger));
  183. uint16_t commPort = Sockaddr_getPort(uai->generic.addr);
  184. struct UDPInterface_pvt* context = Allocator_calloc(alloc, sizeof(struct UDPInterface_pvt), 1);
  185. Identity_set(context);
  186. context->log = logger;
  187. context->allocator = alloc;
  188. context->beaconPort_be = Endian_hostToBigEndian16(beaconPort);
  189. context->commPort_be = Endian_hostToBigEndian16(commPort);
  190. context->pub.generic.addr = uai->generic.addr;
  191. context->pub.generic.alloc = alloc;
  192. context->iface.send = sendPacket;
  193. context->pub.generic.iface = &context->iface;
  194. context->commSock.send = fromCommSock;
  195. context->bcastSock.send = fromBcastSock;
  196. context->commIf = uai;
  197. context->globalConf = globalConf;
  198. Iface_plumb(uai->generic.iface, &context->commSock);
  199. if (beaconPort) {
  200. struct Sockaddr* bcastAddr = Sockaddr_clone(bindAddr, alloc);
  201. Sockaddr_setPort(bcastAddr, beaconPort);
  202. struct UDPAddrIface* bcast =
  203. Er(UDPAddrIface_new(eventBase, bcastAddr, alloc, logger));
  204. UDPAddrIface_setBroadcast(bcast, 1);
  205. Iface_plumb(bcast->generic.iface, &context->bcastSock);
  206. context->bcastIf = bcast;
  207. }
  208. Er_ret(&context->pub);
  209. }
  210. Er_DEFUN(List* UDPInterface_listDevices(struct Allocator* alloc))
  211. {
  212. const Rffi_NetworkInterface* interfaces;
  213. int count = Rffi_interface_addresses(&interfaces, alloc);
  214. List* out = List_new(alloc);
  215. for (int i = 0; i < count; i++) {
  216. if (interfaces[i].is_internal) { continue; }
  217. if (interfaces[i].address.is_ipv6) { continue; }
  218. List_addString(out, String_new(interfaces[i].name, alloc), alloc);
  219. }
  220. Er_ret(out);
  221. }
  222. void UDPInterface_setBroadcastDevices(struct UDPInterface* udpif, List* devices)
  223. {
  224. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  225. if (ctx->bcastIfaceAlloc) { Allocator_free(ctx->bcastIfaceAlloc); }
  226. struct Allocator* alloc = ctx->bcastIfaceAlloc = Allocator_child(ctx->allocator);
  227. struct StringList* bcastIfaces = ctx->bcastIfaces = StringList_new(alloc);
  228. int len = List_size(devices);
  229. for (uint32_t i = 0; i < (unsigned) len; i++) {
  230. String* dev = List_getString(devices, i);
  231. StringList_add(bcastIfaces, String_clone(dev, alloc));
  232. }
  233. }
  234. List* UDPInterface_getBroadcastDevices(struct UDPInterface* udpif, struct Allocator* alloc)
  235. {
  236. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  237. List* out = List_new(alloc);
  238. for (int i = 0; ctx->bcastIfaces && i < ctx->bcastIfaces->length; i++) {
  239. List_addString(out, StringList_get(ctx->bcastIfaces, i), alloc);
  240. }
  241. return out;
  242. }
  243. List* UDPInterface_getBroadcastAddrs(struct UDPInterface* udpif, struct Allocator* alloc)
  244. {
  245. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  246. List* out = List_new(alloc);
  247. if (updateBcastAddrs(ctx)) {
  248. // TODO(cjd): There should be some way to return the fact that there was an error
  249. return out;
  250. }
  251. for (int i = 0; i < ctx->bcastAddrs->length; i++) {
  252. char* addr = Sockaddr_print(ArrayList_Sockaddr_get(ctx->bcastAddrs, i), alloc);
  253. List_addStringC(out, addr, alloc);
  254. }
  255. return out;
  256. }
  257. int UDPInterface_setDSCP(struct UDPInterface* udpif, uint8_t dscp)
  258. {
  259. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  260. int res = UDPAddrIface_setDSCP(ctx->commIf, dscp);
  261. if (res) { return res; }
  262. if (ctx->bcastIf) { return UDPAddrIface_setDSCP(ctx->bcastIf, dscp); }
  263. return 0;
  264. }
  265. int UDPInterface_getFd(struct UDPInterface* udpif)
  266. {
  267. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  268. return UDPAddrIface_getFd(ctx->commIf);
  269. }