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