1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015-2018 National Instruments
4 * Copyright (c) 2015-2018 Joe Hershberger <joe.hershberger@ni.com>
7 #include <asm/eth-raw-os.h>
11 #include <netinet/in.h>
12 #include <netinet/ip.h>
13 #include <netinet/udp.h>
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
22 #include <arpa/inet.h>
23 #include <linux/if_ether.h>
24 #include <linux/if_packet.h>
26 struct sandbox_eth_raw_if_nameindex *sandbox_eth_raw_if_nameindex(void)
28 return (struct sandbox_eth_raw_if_nameindex *)if_nameindex();
31 void sandbox_eth_raw_if_freenameindex(struct sandbox_eth_raw_if_nameindex *ptr)
33 if_freenameindex((struct if_nameindex *)ptr);
36 int sandbox_eth_raw_os_is_local(const char *ifname)
38 int fd = socket(AF_INET, SOCK_DGRAM, 0);
44 memset(&ifr, 0, sizeof(ifr));
45 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
46 ret = ioctl(fd, SIOCGIFFLAGS, &ifr);
51 ret = !!(ifr.ifr_flags & IFF_LOOPBACK);
57 int sandbox_eth_raw_os_idx_to_name(struct eth_sandbox_raw_priv *priv)
59 if (!if_indextoname(priv->host_ifindex, priv->host_ifname))
64 static int _raw_packet_start(struct eth_sandbox_raw_priv *priv,
65 unsigned char *ethmac)
67 struct sockaddr_ll *device;
68 struct packet_mreq mr;
72 /* Prepare device struct */
73 priv->local_bind_sd = -1;
74 priv->device = malloc(sizeof(struct sockaddr_ll));
75 if (priv->device == NULL)
77 device = priv->device;
78 memset(device, 0, sizeof(struct sockaddr_ll));
79 device->sll_ifindex = if_nametoindex(priv->host_ifname);
80 priv->host_ifindex = device->sll_ifindex;
81 device->sll_family = AF_PACKET;
82 memcpy(device->sll_addr, ethmac, 6);
83 device->sll_halen = htons(6);
86 priv->sd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
88 printf("Failed to open socket: %d %s\n", errno,
92 /* Bind to the specified interface */
93 ret = setsockopt(priv->sd, SOL_SOCKET, SO_BINDTODEVICE,
94 priv->host_ifname, strlen(priv->host_ifname) + 1);
96 printf("Failed to bind to '%s': %d %s\n", priv->host_ifname,
97 errno, strerror(errno));
101 /* Make the socket non-blocking */
102 flags = fcntl(priv->sd, F_GETFL, 0);
103 fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
105 /* Enable promiscuous mode to receive responses meant for us */
106 mr.mr_ifindex = device->sll_ifindex;
107 mr.mr_type = PACKET_MR_PROMISC;
108 ret = setsockopt(priv->sd, SOL_PACKET, PACKET_ADD_MEMBERSHIP,
113 printf("Failed to set promiscuous mode: %d %s\n"
114 "Falling back to the old \"flags\" way...\n",
115 errno, strerror(errno));
116 if (strlen(priv->host_ifname) >= IFNAMSIZ) {
117 printf("Interface name %s is too long.\n",
121 strncpy(ifr.ifr_name, priv->host_ifname, IFNAMSIZ);
122 if (ioctl(priv->sd, SIOCGIFFLAGS, &ifr) < 0) {
123 printf("Failed to read flags: %d %s\n", errno,
127 ifr.ifr_flags |= IFF_PROMISC;
128 if (ioctl(priv->sd, SIOCSIFFLAGS, &ifr) < 0) {
129 printf("Failed to write flags: %d %s\n", errno,
137 static int _local_inet_start(struct eth_sandbox_raw_priv *priv)
139 struct sockaddr_in *device;
144 /* Prepare device struct */
145 priv->local_bind_sd = -1;
146 priv->local_bind_udp_port = 0;
147 priv->device = malloc(sizeof(struct sockaddr_in));
148 if (priv->device == NULL)
150 device = priv->device;
151 memset(device, 0, sizeof(struct sockaddr_in));
152 device->sin_family = AF_INET;
153 device->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
157 * Since we specify UDP here, any incoming ICMP packets will
158 * not be received, so things like ping will not work on this
159 * localhost interface.
161 priv->sd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
163 printf("Failed to open socket: %d %s\n", errno,
168 /* Make the socket non-blocking */
169 flags = fcntl(priv->sd, F_GETFL, 0);
170 fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
172 /* Include the UDP/IP headers on send and receive */
173 ret = setsockopt(priv->sd, IPPROTO_IP, IP_HDRINCL, &one,
176 printf("Failed to set header include option: %d %s\n", errno,
183 int sandbox_eth_raw_os_start(struct eth_sandbox_raw_priv *priv,
184 unsigned char *ethmac)
187 return _local_inet_start(priv);
189 return _raw_packet_start(priv, ethmac);
192 int sandbox_eth_raw_os_send(void *packet, int length,
193 struct eth_sandbox_raw_priv *priv)
196 struct udphdr *udph = packet + sizeof(struct iphdr);
198 if (priv->sd < 0 || !priv->device)
202 * This block of code came about when testing tftp on the localhost
203 * interface. When using the RAW AF_INET API, the network stack is still
204 * in play responding to incoming traffic based on open "ports". Since
205 * it is raw (at the IP layer, no Ethernet) the network stack tells the
206 * TFTP server that the port it responded to is closed. This causes the
207 * TFTP transfer to be aborted. This block of code inspects the outgoing
208 * packet as formulated by the u-boot network stack to determine the
209 * source port (that the TFTP server will send packets back to) and
210 * opens a typical UDP socket on that port, thus preventing the network
211 * stack from sending that ICMP message claiming that the port has no
214 if (priv->local && (priv->local_bind_sd == -1 ||
215 priv->local_bind_udp_port != udph->source)) {
216 struct iphdr *iph = packet;
217 struct sockaddr_in addr;
219 if (priv->local_bind_sd != -1)
220 close(priv->local_bind_sd);
222 /* A normal UDP socket is required to bind */
223 priv->local_bind_sd = socket(AF_INET, SOCK_DGRAM, 0);
224 if (priv->local_bind_sd < 0) {
225 printf("Failed to open bind sd: %d %s\n", errno,
229 priv->local_bind_udp_port = udph->source;
232 * Bind the UDP port that we intend to use as our source port
233 * so that the kernel will not send an ICMP port unreachable
234 * message to the server
236 addr.sin_family = AF_INET;
237 addr.sin_port = udph->source;
238 addr.sin_addr.s_addr = iph->saddr;
239 retval = bind(priv->local_bind_sd, (struct sockaddr *)&addr,
242 printf("Failed to bind: %d %s\n", errno,
246 retval = sendto(priv->sd, packet, length, 0,
247 (struct sockaddr *)priv->device,
248 sizeof(struct sockaddr_ll));
250 printf("Failed to send packet: %d %s\n", errno,
257 int sandbox_eth_raw_os_recv(void *packet, int *length,
258 const struct eth_sandbox_raw_priv *priv)
263 if (priv->sd < 0 || !priv->device)
265 saddr_size = sizeof(struct sockaddr);
266 retval = recvfrom(priv->sd, packet, 1536, 0,
267 (struct sockaddr *)priv->device,
268 (socklen_t *)&saddr_size);
274 /* The socket is non-blocking, so expect EAGAIN when there is no data */
280 void sandbox_eth_raw_os_stop(struct eth_sandbox_raw_priv *priv)
287 if (priv->local_bind_sd != -1)
288 close(priv->local_bind_sd);
289 priv->local_bind_sd = -1;
290 priv->local_bind_udp_port = 0;