eth-raw-os.c 6.7 KB

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