eth-raw-os.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015-2018 National Instruments
  4. * Copyright (c) 2015-2018 Joe Hershberger <joe.hershberger@ni.com>
  5. */
  6. #include <asm/eth-raw-os.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <net/if.h>
  10. #include <netinet/in.h>
  11. #include <netinet/ip.h>
  12. #include <netinet/udp.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <sys/types.h>
  17. #include <sys/ioctl.h>
  18. #include <sys/socket.h>
  19. #include <unistd.h>
  20. #include <arpa/inet.h>
  21. #include <linux/if_ether.h>
  22. #include <linux/if_packet.h>
  23. struct sandbox_eth_raw_if_nameindex *sandbox_eth_raw_if_nameindex(void)
  24. {
  25. return (struct sandbox_eth_raw_if_nameindex *)if_nameindex();
  26. }
  27. void sandbox_eth_raw_if_freenameindex(struct sandbox_eth_raw_if_nameindex *ptr)
  28. {
  29. if_freenameindex((struct if_nameindex *)ptr);
  30. }
  31. int sandbox_eth_raw_os_is_local(const char *ifname)
  32. {
  33. int fd = socket(AF_INET, SOCK_DGRAM, 0);
  34. struct ifreq ifr;
  35. int ret = 0;
  36. if (fd < 0)
  37. return -errno;
  38. memset(&ifr, 0, sizeof(ifr));
  39. strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  40. ret = ioctl(fd, SIOCGIFFLAGS, &ifr);
  41. if (ret < 0) {
  42. ret = -errno;
  43. goto out;
  44. }
  45. ret = !!(ifr.ifr_flags & IFF_LOOPBACK);
  46. out:
  47. close(fd);
  48. return ret;
  49. }
  50. int sandbox_eth_raw_os_idx_to_name(struct eth_sandbox_raw_priv *priv)
  51. {
  52. if (!if_indextoname(priv->host_ifindex, priv->host_ifname))
  53. return -errno;
  54. return 0;
  55. }
  56. static int _raw_packet_start(struct eth_sandbox_raw_priv *priv,
  57. unsigned char *ethmac)
  58. {
  59. struct sockaddr_ll *device;
  60. struct packet_mreq mr;
  61. int ret;
  62. int flags;
  63. /* Prepare device struct */
  64. priv->local_bind_sd = -1;
  65. priv->device = malloc(sizeof(struct sockaddr_ll));
  66. if (priv->device == NULL)
  67. return -ENOMEM;
  68. device = priv->device;
  69. memset(device, 0, sizeof(struct sockaddr_ll));
  70. device->sll_ifindex = if_nametoindex(priv->host_ifname);
  71. priv->host_ifindex = device->sll_ifindex;
  72. device->sll_family = AF_PACKET;
  73. memcpy(device->sll_addr, ethmac, 6);
  74. device->sll_halen = htons(6);
  75. /* Open socket */
  76. priv->sd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  77. if (priv->sd < 0) {
  78. printf("Failed to open socket: %d %s\n", errno,
  79. strerror(errno));
  80. return -errno;
  81. }
  82. /* Bind to the specified interface */
  83. ret = setsockopt(priv->sd, SOL_SOCKET, SO_BINDTODEVICE,
  84. priv->host_ifname, strlen(priv->host_ifname) + 1);
  85. if (ret < 0) {
  86. printf("Failed to bind to '%s': %d %s\n", priv->host_ifname,
  87. errno, strerror(errno));
  88. return -errno;
  89. }
  90. /* Make the socket non-blocking */
  91. flags = fcntl(priv->sd, F_GETFL, 0);
  92. fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
  93. /* Enable promiscuous mode to receive responses meant for us */
  94. mr.mr_ifindex = device->sll_ifindex;
  95. mr.mr_type = PACKET_MR_PROMISC;
  96. ret = setsockopt(priv->sd, SOL_PACKET, PACKET_ADD_MEMBERSHIP,
  97. &mr, sizeof(mr));
  98. if (ret < 0) {
  99. struct ifreq ifr;
  100. printf("Failed to set promiscuous mode: %d %s\n"
  101. "Falling back to the old \"flags\" way...\n",
  102. errno, strerror(errno));
  103. if (strlen(priv->host_ifname) >= IFNAMSIZ) {
  104. printf("Interface name %s is too long.\n",
  105. priv->host_ifname);
  106. return -EINVAL;
  107. }
  108. strncpy(ifr.ifr_name, priv->host_ifname, IFNAMSIZ);
  109. if (ioctl(priv->sd, SIOCGIFFLAGS, &ifr) < 0) {
  110. printf("Failed to read flags: %d %s\n", errno,
  111. strerror(errno));
  112. return -errno;
  113. }
  114. ifr.ifr_flags |= IFF_PROMISC;
  115. if (ioctl(priv->sd, SIOCSIFFLAGS, &ifr) < 0) {
  116. printf("Failed to write flags: %d %s\n", errno,
  117. strerror(errno));
  118. return -errno;
  119. }
  120. }
  121. return 0;
  122. }
  123. static int _local_inet_start(struct eth_sandbox_raw_priv *priv)
  124. {
  125. struct sockaddr_in *device;
  126. int ret;
  127. int flags;
  128. int one = 1;
  129. /* Prepare device struct */
  130. priv->local_bind_sd = -1;
  131. priv->local_bind_udp_port = 0;
  132. priv->device = malloc(sizeof(struct sockaddr_in));
  133. if (priv->device == NULL)
  134. return -ENOMEM;
  135. device = priv->device;
  136. memset(device, 0, sizeof(struct sockaddr_in));
  137. device->sin_family = AF_INET;
  138. device->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  139. /**
  140. * Open socket
  141. * Since we specify UDP here, any incoming ICMP packets will
  142. * not be received, so things like ping will not work on this
  143. * localhost interface.
  144. */
  145. priv->sd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
  146. if (priv->sd < 0) {
  147. printf("Failed to open socket: %d %s\n", errno,
  148. strerror(errno));
  149. return -errno;
  150. }
  151. /* Make the socket non-blocking */
  152. flags = fcntl(priv->sd, F_GETFL, 0);
  153. fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
  154. /* Include the UDP/IP headers on send and receive */
  155. ret = setsockopt(priv->sd, IPPROTO_IP, IP_HDRINCL, &one,
  156. sizeof(one));
  157. if (ret < 0) {
  158. printf("Failed to set header include option: %d %s\n", errno,
  159. strerror(errno));
  160. return -errno;
  161. }
  162. return 0;
  163. }
  164. int sandbox_eth_raw_os_start(struct eth_sandbox_raw_priv *priv,
  165. unsigned char *ethmac)
  166. {
  167. if (priv->local)
  168. return _local_inet_start(priv);
  169. else
  170. return _raw_packet_start(priv, ethmac);
  171. }
  172. int sandbox_eth_raw_os_send(void *packet, int length,
  173. struct eth_sandbox_raw_priv *priv)
  174. {
  175. int retval;
  176. struct udphdr *udph = packet + sizeof(struct iphdr);
  177. if (priv->sd < 0 || !priv->device)
  178. return -EINVAL;
  179. /*
  180. * This block of code came about when testing tftp on the localhost
  181. * interface. When using the RAW AF_INET API, the network stack is still
  182. * in play responding to incoming traffic based on open "ports". Since
  183. * it is raw (at the IP layer, no Ethernet) the network stack tells the
  184. * TFTP server that the port it responded to is closed. This causes the
  185. * TFTP transfer to be aborted. This block of code inspects the outgoing
  186. * packet as formulated by the u-boot network stack to determine the
  187. * source port (that the TFTP server will send packets back to) and
  188. * opens a typical UDP socket on that port, thus preventing the network
  189. * stack from sending that ICMP message claiming that the port has no
  190. * bound socket.
  191. */
  192. if (priv->local && (priv->local_bind_sd == -1 ||
  193. priv->local_bind_udp_port != udph->source)) {
  194. struct iphdr *iph = packet;
  195. struct sockaddr_in addr;
  196. if (priv->local_bind_sd != -1)
  197. close(priv->local_bind_sd);
  198. /* A normal UDP socket is required to bind */
  199. priv->local_bind_sd = socket(AF_INET, SOCK_DGRAM, 0);
  200. if (priv->local_bind_sd < 0) {
  201. printf("Failed to open bind sd: %d %s\n", errno,
  202. strerror(errno));
  203. return -errno;
  204. }
  205. priv->local_bind_udp_port = udph->source;
  206. /**
  207. * Bind the UDP port that we intend to use as our source port
  208. * so that the kernel will not send an ICMP port unreachable
  209. * message to the server
  210. */
  211. addr.sin_family = AF_INET;
  212. addr.sin_port = udph->source;
  213. addr.sin_addr.s_addr = iph->saddr;
  214. retval = bind(priv->local_bind_sd, (struct sockaddr *)&addr,
  215. sizeof(addr));
  216. if (retval < 0)
  217. printf("Failed to bind: %d %s\n", errno,
  218. strerror(errno));
  219. }
  220. retval = sendto(priv->sd, packet, length, 0,
  221. (struct sockaddr *)priv->device,
  222. sizeof(struct sockaddr_ll));
  223. if (retval < 0) {
  224. printf("Failed to send packet: %d %s\n", errno,
  225. strerror(errno));
  226. return -errno;
  227. }
  228. return retval;
  229. }
  230. int sandbox_eth_raw_os_recv(void *packet, int *length,
  231. const struct eth_sandbox_raw_priv *priv)
  232. {
  233. int retval;
  234. int saddr_size;
  235. if (priv->sd < 0 || !priv->device)
  236. return -EINVAL;
  237. saddr_size = sizeof(struct sockaddr);
  238. retval = recvfrom(priv->sd, packet, 1536, 0,
  239. (struct sockaddr *)priv->device,
  240. (socklen_t *)&saddr_size);
  241. *length = 0;
  242. if (retval >= 0) {
  243. *length = retval;
  244. return 0;
  245. }
  246. /* The socket is non-blocking, so expect EAGAIN when there is no data */
  247. if (errno == EAGAIN)
  248. return 0;
  249. return -errno;
  250. }
  251. void sandbox_eth_raw_os_stop(struct eth_sandbox_raw_priv *priv)
  252. {
  253. free(priv->device);
  254. priv->device = NULL;
  255. close(priv->sd);
  256. priv->sd = -1;
  257. if (priv->local) {
  258. if (priv->local_bind_sd != -1)
  259. close(priv->local_bind_sd);
  260. priv->local_bind_sd = -1;
  261. priv->local_bind_udp_port = 0;
  262. }
  263. }