eth-raw-os.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. if (strlen(ifname) >= IFNAMSIZ) {
  72. printf("Interface name %s is too long.\n", ifname);
  73. return -EINVAL;
  74. }
  75. strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  76. if (ioctl(priv->sd, SIOCGIFFLAGS, &ifr) < 0) {
  77. printf("Failed to read flags: %d %s\n", errno,
  78. strerror(errno));
  79. return -errno;
  80. }
  81. ifr.ifr_flags |= IFF_PROMISC;
  82. if (ioctl(priv->sd, SIOCSIFFLAGS, &ifr) < 0) {
  83. printf("Failed to write flags: %d %s\n", errno,
  84. strerror(errno));
  85. return -errno;
  86. }
  87. }
  88. return 0;
  89. }
  90. static int _local_inet_start(struct eth_sandbox_raw_priv *priv)
  91. {
  92. struct sockaddr_in *device;
  93. int ret;
  94. int flags;
  95. int one = 1;
  96. /* Prepare device struct */
  97. priv->device = malloc(sizeof(struct sockaddr_in));
  98. if (priv->device == NULL)
  99. return -ENOMEM;
  100. device = priv->device;
  101. memset(device, 0, sizeof(struct sockaddr_in));
  102. device->sin_family = AF_INET;
  103. device->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  104. /**
  105. * Open socket
  106. * Since we specify UDP here, any incoming ICMP packets will
  107. * not be received, so things like ping will not work on this
  108. * localhost interface.
  109. */
  110. priv->sd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
  111. if (priv->sd < 0) {
  112. printf("Failed to open socket: %d %s\n", errno,
  113. strerror(errno));
  114. return -errno;
  115. }
  116. /* Make the socket non-blocking */
  117. flags = fcntl(priv->sd, F_GETFL, 0);
  118. fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
  119. /* Include the UDP/IP headers on send and receive */
  120. ret = setsockopt(priv->sd, IPPROTO_IP, IP_HDRINCL, &one,
  121. sizeof(one));
  122. if (ret < 0) {
  123. printf("Failed to set header include option: %d %s\n", errno,
  124. strerror(errno));
  125. return -errno;
  126. }
  127. priv->local_bind_sd = -1;
  128. priv->local_bind_udp_port = 0;
  129. return 0;
  130. }
  131. int sandbox_eth_raw_os_start(const char *ifname, unsigned char *ethmac,
  132. struct eth_sandbox_raw_priv *priv)
  133. {
  134. if (priv->local)
  135. return _local_inet_start(priv);
  136. else
  137. return _raw_packet_start(ifname, ethmac, priv);
  138. }
  139. int sandbox_eth_raw_os_send(void *packet, int length,
  140. struct eth_sandbox_raw_priv *priv)
  141. {
  142. int retval;
  143. struct udphdr *udph = packet + sizeof(struct iphdr);
  144. if (!priv->sd || !priv->device)
  145. return -EINVAL;
  146. /*
  147. * This block of code came about when testing tftp on the localhost
  148. * interface. When using the RAW AF_INET API, the network stack is still
  149. * in play responding to incoming traffic based on open "ports". Since
  150. * it is raw (at the IP layer, no Ethernet) the network stack tells the
  151. * TFTP server that the port it responded to is closed. This causes the
  152. * TFTP transfer to be aborted. This block of code inspects the outgoing
  153. * packet as formulated by the u-boot network stack to determine the
  154. * source port (that the TFTP server will send packets back to) and
  155. * opens a typical UDP socket on that port, thus preventing the network
  156. * stack from sending that ICMP message claiming that the port has no
  157. * bound socket.
  158. */
  159. if (priv->local && (priv->local_bind_sd == -1 ||
  160. priv->local_bind_udp_port != udph->source)) {
  161. struct iphdr *iph = packet;
  162. struct sockaddr_in addr;
  163. if (priv->local_bind_sd != -1)
  164. close(priv->local_bind_sd);
  165. /* A normal UDP socket is required to bind */
  166. priv->local_bind_sd = socket(AF_INET, SOCK_DGRAM, 0);
  167. if (priv->local_bind_sd < 0) {
  168. printf("Failed to open bind sd: %d %s\n", errno,
  169. strerror(errno));
  170. return -errno;
  171. }
  172. priv->local_bind_udp_port = udph->source;
  173. /**
  174. * Bind the UDP port that we intend to use as our source port
  175. * so that the kernel will not send an ICMP port unreachable
  176. * message to the server
  177. */
  178. addr.sin_family = AF_INET;
  179. addr.sin_port = udph->source;
  180. addr.sin_addr.s_addr = iph->saddr;
  181. retval = bind(priv->local_bind_sd, (struct sockaddr *)&addr,
  182. sizeof(addr));
  183. if (retval < 0)
  184. printf("Failed to bind: %d %s\n", errno,
  185. strerror(errno));
  186. }
  187. retval = sendto(priv->sd, packet, length, 0,
  188. (struct sockaddr *)priv->device,
  189. sizeof(struct sockaddr_ll));
  190. if (retval < 0) {
  191. printf("Failed to send packet: %d %s\n", errno,
  192. strerror(errno));
  193. return -errno;
  194. }
  195. return retval;
  196. }
  197. int sandbox_eth_raw_os_recv(void *packet, int *length,
  198. const struct eth_sandbox_raw_priv *priv)
  199. {
  200. int retval;
  201. int saddr_size;
  202. if (!priv->sd || !priv->device)
  203. return -EINVAL;
  204. saddr_size = sizeof(struct sockaddr);
  205. retval = recvfrom(priv->sd, packet, 1536, 0,
  206. (struct sockaddr *)priv->device,
  207. (socklen_t *)&saddr_size);
  208. *length = 0;
  209. if (retval >= 0) {
  210. *length = retval;
  211. return 0;
  212. }
  213. /* The socket is non-blocking, so expect EAGAIN when there is no data */
  214. if (errno == EAGAIN)
  215. return 0;
  216. return -errno;
  217. }
  218. void sandbox_eth_raw_os_stop(struct eth_sandbox_raw_priv *priv)
  219. {
  220. free(priv->device);
  221. priv->device = NULL;
  222. close(priv->sd);
  223. priv->sd = -1;
  224. if (priv->local) {
  225. if (priv->local_bind_sd != -1)
  226. close(priv->local_bind_sd);
  227. priv->local_bind_sd = -1;
  228. priv->local_bind_udp_port = 0;
  229. }
  230. }