1
0

ETHInterface_linux.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 "interface/ETHInterface.h"
  16. #include "exception/Except.h"
  17. #include "memory/Allocator.h"
  18. #include "net/InterfaceController.h"
  19. #include "wire/Headers.h"
  20. #include "wire/Message.h"
  21. #include "wire/Error.h"
  22. #include "wire/Ethernet.h"
  23. #include "util/Assert.h"
  24. #include "util/platform/Socket.h"
  25. #include "util/events/Event.h"
  26. #include "util/Identity.h"
  27. #include "util/AddrTools.h"
  28. #include "util/version/Version.h"
  29. #include "util/events/Timeout.h"
  30. #include <string.h>
  31. #include <sys/socket.h>
  32. #include <linux/if_packet.h>
  33. #include <linux/if_ether.h>
  34. #include <linux/if_arp.h>
  35. #include <sys/types.h>
  36. #include <sys/ioctl.h>
  37. #include <unistd.h>
  38. #include <errno.h>
  39. #include <ifaddrs.h>
  40. #define MAX_PACKET_SIZE 1496
  41. #define MIN_PACKET_SIZE 46
  42. #define PADDING 512
  43. // 2 last 0x00 of .sll_addr are removed from original size (20)
  44. #define SOCKADDR_LL_LEN 18
  45. struct ETHInterface_pvt
  46. {
  47. struct ETHInterface pub;
  48. Socket socket;
  49. /** The unix interface index which is used to identify the eth device. */
  50. int ifindex;
  51. struct Log* logger;
  52. struct sockaddr_ll addrBase;
  53. String* ifName;
  54. Identity
  55. };
  56. static void sendMessageInternal(struct Message* message,
  57. struct sockaddr_ll* addr,
  58. struct ETHInterface_pvt* context)
  59. {
  60. /* Cut down on the noise
  61. uint8_t buff[sizeof(*addr) * 2 + 1] = {0};
  62. Hex_encode(buff, sizeof(buff), (uint8_t*)addr, sizeof(*addr));
  63. Log_debug(context->logger, "Sending ethernet frame to [%s]", buff);
  64. */
  65. if (sendto(context->socket,
  66. message->bytes,
  67. message->length,
  68. 0,
  69. (struct sockaddr*) addr,
  70. sizeof(struct sockaddr_ll)) < 0)
  71. {
  72. switch (errno) {
  73. default:;
  74. Log_info(context->logger, "[%s] Got error sending to socket [%s]",
  75. context->ifName->bytes, strerror(errno));
  76. case EMSGSIZE:
  77. case ENOBUFS:
  78. case EAGAIN:;
  79. // todo: care
  80. }
  81. }
  82. return;
  83. }
  84. static Iface_DEFUN sendMessage(struct Message* msg, struct Iface* iface)
  85. {
  86. struct ETHInterface_pvt* ctx =
  87. Identity_containerOf(iface, struct ETHInterface_pvt, pub.generic.iface);
  88. struct Sockaddr* sa = (struct Sockaddr*) msg->bytes;
  89. Assert_true(msg->length >= Sockaddr_OVERHEAD);
  90. Assert_true(sa->addrLen <= ETHInterface_Sockaddr_SIZE);
  91. struct ETHInterface_Sockaddr sockaddr = { .generic = { .addrLen = 0 } };
  92. Message_pop(msg, &sockaddr, sa->addrLen, NULL);
  93. struct sockaddr_ll addr;
  94. Bits_memcpyConst(&addr, &ctx->addrBase, sizeof(struct sockaddr_ll));
  95. if (sockaddr.generic.flags & Sockaddr_flags_BCAST) {
  96. Bits_memset(addr.sll_addr, 0xff, 6);
  97. } else {
  98. Bits_memcpyConst(addr.sll_addr, sockaddr.mac, 6);
  99. }
  100. struct ETHInterface_Header hdr = {
  101. .version = ETHInterface_CURRENT_VERSION,
  102. .zero = 0,
  103. .length_be = Endian_hostToBigEndian16(msg->length + ETHInterface_Header_SIZE),
  104. .fc00_be = Endian_hostToBigEndian16(0xfc00)
  105. };
  106. Message_push(msg, &hdr, ETHInterface_Header_SIZE, NULL);
  107. sendMessageInternal(msg, &addr, ctx);
  108. return NULL;
  109. }
  110. static void handleEvent2(struct ETHInterface_pvt* context, struct Allocator* messageAlloc)
  111. {
  112. struct Message* msg = Message_new(MAX_PACKET_SIZE, PADDING, messageAlloc);
  113. struct sockaddr_ll addr;
  114. uint32_t addrLen = sizeof(struct sockaddr_ll);
  115. // Knock it out of alignment by 2 bytes so that it will be
  116. // aligned when the idAndPadding is shifted off.
  117. Message_shift(msg, 2, NULL);
  118. int rc = recvfrom(context->socket,
  119. msg->bytes,
  120. msg->length,
  121. 0,
  122. (struct sockaddr*) &addr,
  123. &addrLen);
  124. if (rc < ETHInterface_Header_SIZE) {
  125. Log_debug(context->logger, "Failed to receive eth frame");
  126. return;
  127. }
  128. Assert_true(msg->length >= rc);
  129. msg->length = rc;
  130. //Assert_true(addrLen == SOCKADDR_LL_LEN);
  131. struct ETHInterface_Header hdr;
  132. Message_pop(msg, &hdr, ETHInterface_Header_SIZE, NULL);
  133. // here we could put a switch statement to handle different versions differently.
  134. if (hdr.version != ETHInterface_CURRENT_VERSION) {
  135. Log_debug(context->logger, "DROP unknown version");
  136. return;
  137. }
  138. uint16_t reportedLength = Endian_bigEndianToHost16(hdr.length_be);
  139. reportedLength -= ETHInterface_Header_SIZE;
  140. if (msg->length != reportedLength) {
  141. if (msg->length < reportedLength) {
  142. Log_debug(context->logger, "DROP size field is larger than frame");
  143. return;
  144. }
  145. msg->length = reportedLength;
  146. }
  147. if (hdr.fc00_be != Endian_hostToBigEndian16(0xfc00)) {
  148. Log_debug(context->logger, "DROP bad magic");
  149. return;
  150. }
  151. struct ETHInterface_Sockaddr sockaddr = { .zero = 0 };
  152. Bits_memcpyConst(sockaddr.mac, addr.sll_addr, 6);
  153. sockaddr.generic.addrLen = ETHInterface_Sockaddr_SIZE;
  154. if (addr.sll_pkttype == PACKET_BROADCAST) {
  155. sockaddr.generic.flags |= Sockaddr_flags_BCAST;
  156. }
  157. Message_push(msg, &sockaddr, ETHInterface_Sockaddr_SIZE, NULL);
  158. Assert_true(!((uintptr_t)msg->bytes % 4) && "Alignment fault");
  159. Iface_send(&context->pub.generic.iface, msg);
  160. }
  161. static void handleEvent(void* vcontext)
  162. {
  163. struct ETHInterface_pvt* context = Identity_check((struct ETHInterface_pvt*) vcontext);
  164. struct Allocator* messageAlloc = Allocator_child(context->pub.generic.alloc);
  165. handleEvent2(context, messageAlloc);
  166. Allocator_free(messageAlloc);
  167. }
  168. List* ETHInterface_listDevices(struct Allocator* alloc, struct Except* eh)
  169. {
  170. struct ifaddrs* ifaddr = NULL;
  171. if (getifaddrs(&ifaddr) || ifaddr == NULL) {
  172. Except_throw(eh, "getifaddrs() -> errno:%d [%s]", errno, strerror(errno));
  173. }
  174. List* out = List_new(alloc);
  175. for (struct ifaddrs* ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
  176. if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_PACKET) {
  177. List_addString(out, String_new(ifa->ifa_name, alloc), alloc);
  178. }
  179. }
  180. freeifaddrs(ifaddr);
  181. return out;
  182. }
  183. static int closeSocket(struct Allocator_OnFreeJob* j)
  184. {
  185. struct ETHInterface_pvt* ctx = Identity_check((struct ETHInterface_pvt*) j->userData);
  186. close(ctx->socket);
  187. return 0;
  188. }
  189. struct ETHInterface* ETHInterface_new(struct EventBase* eventBase,
  190. const char* bindDevice,
  191. struct Allocator* alloc,
  192. struct Except* exHandler,
  193. struct Log* logger)
  194. {
  195. struct ETHInterface_pvt* ctx = Allocator_calloc(alloc, sizeof(struct ETHInterface_pvt), 1);
  196. Identity_set(ctx);
  197. ctx->pub.generic.iface.send = sendMessage;
  198. ctx->pub.generic.alloc = alloc;
  199. ctx->logger = logger;
  200. struct ifreq ifr = { .ifr_ifindex = 0 };
  201. ctx->socket = socket(AF_PACKET, SOCK_DGRAM, Ethernet_TYPE_CJDNS);
  202. if (ctx->socket == -1) {
  203. Except_throw(exHandler, "call to socket() failed. [%s]", strerror(errno));
  204. }
  205. Allocator_onFree(alloc, closeSocket, ctx);
  206. CString_strncpy(ifr.ifr_name, bindDevice, IFNAMSIZ - 1);
  207. ctx->ifName = String_new(bindDevice, alloc);
  208. if (ioctl(ctx->socket, SIOCGIFINDEX, &ifr) == -1) {
  209. Except_throw(exHandler, "failed to find interface index [%s]", strerror(errno));
  210. }
  211. ctx->ifindex = ifr.ifr_ifindex;
  212. if (ioctl(ctx->socket, SIOCGIFFLAGS, &ifr) < 0) {
  213. Except_throw(exHandler, "ioctl(SIOCGIFFLAGS) [%s]", strerror(errno));
  214. }
  215. if (!((ifr.ifr_flags & IFF_UP) && (ifr.ifr_flags & IFF_RUNNING))) {
  216. Log_info(logger, "Bringing up interface [%s]", ifr.ifr_name);
  217. ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
  218. if (ioctl(ctx->socket, SIOCSIFFLAGS, &ifr) < 0) {
  219. Except_throw(exHandler, "ioctl(SIOCSIFFLAGS) [%s]", strerror(errno));
  220. }
  221. }
  222. ctx->addrBase = (struct sockaddr_ll) {
  223. .sll_family = AF_PACKET,
  224. .sll_protocol = Ethernet_TYPE_CJDNS,
  225. .sll_ifindex = ctx->ifindex,
  226. .sll_hatype = ARPHRD_ETHER,
  227. .sll_pkttype = PACKET_OTHERHOST,
  228. .sll_halen = ETH_ALEN
  229. };
  230. if (bind(ctx->socket, (struct sockaddr*) &ctx->addrBase, sizeof(struct sockaddr_ll))) {
  231. Except_throw(exHandler, "call to bind() failed [%s]", strerror(errno));
  232. }
  233. Socket_makeNonBlocking(ctx->socket);
  234. Event_socketRead(handleEvent, ctx, ctx->socket, eventBase, alloc, exHandler);
  235. return &ctx->pub;
  236. }