eth-raw-os.c 6.8 KB

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