1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015-2018 National Instruments
9 #include <asm/eth-raw-os.h>
13 #include <netinet/in.h>
14 #include <netinet/ip.h>
15 #include <netinet/udp.h>
20 #include <sys/types.h>
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
25 #include <arpa/inet.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_packet.h>
31 struct sandbox_eth_raw_if_nameindex *sandbox_eth_raw_if_nameindex(void)
33 return (struct sandbox_eth_raw_if_nameindex *)if_nameindex();
36 void sandbox_eth_raw_if_freenameindex(struct sandbox_eth_raw_if_nameindex *ptr)
38 if_freenameindex((struct if_nameindex *)ptr);
41 int sandbox_eth_raw_os_is_local(const char *ifname)
43 int fd = socket(AF_INET, SOCK_DGRAM, 0);
49 memset(&ifr, 0, sizeof(ifr));
50 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
51 ret = ioctl(fd, SIOCGIFFLAGS, &ifr);
56 ret = !!(ifr.ifr_flags & IFF_LOOPBACK);
62 int sandbox_eth_raw_os_idx_to_name(struct eth_sandbox_raw_priv *priv)
64 if (!if_indextoname(priv->host_ifindex, priv->host_ifname))
69 static int _raw_packet_start(struct eth_sandbox_raw_priv *priv,
70 unsigned char *ethmac)
72 struct sockaddr_ll *device;
73 struct packet_mreq mr;
77 /* Prepare device struct */
78 priv->local_bind_sd = -1;
79 priv->device = malloc(sizeof(struct sockaddr_ll));
80 if (priv->device == NULL)
82 device = priv->device;
83 memset(device, 0, sizeof(struct sockaddr_ll));
84 device->sll_ifindex = if_nametoindex(priv->host_ifname);
85 priv->host_ifindex = device->sll_ifindex;
86 device->sll_family = AF_PACKET;
87 memcpy(device->sll_addr, ethmac, 6);
88 device->sll_halen = htons(6);
91 priv->sd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
93 printf("Failed to open socket: %d %s\n", errno,
97 /* Bind to the specified interface */
98 ret = setsockopt(priv->sd, SOL_SOCKET, SO_BINDTODEVICE,
99 priv->host_ifname, strlen(priv->host_ifname) + 1);
101 printf("Failed to bind to '%s': %d %s\n", priv->host_ifname,
102 errno, strerror(errno));
106 /* Make the socket non-blocking */
107 flags = fcntl(priv->sd, F_GETFL, 0);
108 ret = fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
110 printf("Failed to make socket non-blocking: %d %s\n", errno,
115 /* Enable promiscuous mode to receive responses meant for us */
116 mr.mr_ifindex = device->sll_ifindex;
117 mr.mr_type = PACKET_MR_PROMISC;
118 ret = setsockopt(priv->sd, SOL_PACKET, PACKET_ADD_MEMBERSHIP,
123 printf("Failed to set promiscuous mode: %d %s\n"
124 "Falling back to the old \"flags\" way...\n",
125 errno, strerror(errno));
126 if (strlen(priv->host_ifname) >= IFNAMSIZ) {
127 printf("Interface name %s is too long.\n",
131 strncpy(ifr.ifr_name, priv->host_ifname, IFNAMSIZ);
132 if (ioctl(priv->sd, SIOCGIFFLAGS, &ifr) < 0) {
133 printf("Failed to read flags: %d %s\n", errno,
137 ifr.ifr_flags |= IFF_PROMISC;
138 if (ioctl(priv->sd, SIOCSIFFLAGS, &ifr) < 0) {
139 printf("Failed to write flags: %d %s\n", errno,
147 static int _local_inet_start(struct eth_sandbox_raw_priv *priv)
149 struct sockaddr_in *device;
154 /* Prepare device struct */
155 priv->local_bind_sd = -1;
156 priv->local_bind_udp_port = 0;
157 priv->device = malloc(sizeof(struct sockaddr_in));
158 if (priv->device == NULL)
160 device = priv->device;
161 memset(device, 0, sizeof(struct sockaddr_in));
162 device->sin_family = AF_INET;
163 device->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
167 * Since we specify UDP here, any incoming ICMP packets will
168 * not be received, so things like ping will not work on this
169 * localhost interface.
171 priv->sd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
173 printf("Failed to open socket: %d %s\n", errno,
178 /* Make the socket non-blocking */
179 flags = fcntl(priv->sd, F_GETFL, 0);
180 ret = fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
182 printf("Failed to make socket non-blocking: %d %s\n", errno,
187 /* Include the UDP/IP headers on send and receive */
188 ret = setsockopt(priv->sd, IPPROTO_IP, IP_HDRINCL, &one,
191 printf("Failed to set header include option: %d %s\n", errno,
198 int sandbox_eth_raw_os_start(struct eth_sandbox_raw_priv *priv,
199 unsigned char *ethmac)
202 return _local_inet_start(priv);
204 return _raw_packet_start(priv, ethmac);
207 int sandbox_eth_raw_os_send(void *packet, int length,
208 struct eth_sandbox_raw_priv *priv)
211 struct udphdr *udph = packet + sizeof(struct iphdr);
213 if (priv->sd < 0 || !priv->device)
217 * This block of code came about when testing tftp on the localhost
218 * interface. When using the RAW AF_INET API, the network stack is still
219 * in play responding to incoming traffic based on open "ports". Since
220 * it is raw (at the IP layer, no Ethernet) the network stack tells the
221 * TFTP server that the port it responded to is closed. This causes the
222 * TFTP transfer to be aborted. This block of code inspects the outgoing
223 * packet as formulated by the u-boot network stack to determine the
224 * source port (that the TFTP server will send packets back to) and
225 * opens a typical UDP socket on that port, thus preventing the network
226 * stack from sending that ICMP message claiming that the port has no
229 if (priv->local && (priv->local_bind_sd == -1 ||
230 priv->local_bind_udp_port != udph->source)) {
231 struct iphdr *iph = packet;
232 struct sockaddr_in addr;
234 if (priv->local_bind_sd != -1)
235 os_close(priv->local_bind_sd);
237 /* A normal UDP socket is required to bind */
238 priv->local_bind_sd = socket(AF_INET, SOCK_DGRAM, 0);
239 if (priv->local_bind_sd < 0) {
240 printf("Failed to open bind sd: %d %s\n", errno,
244 priv->local_bind_udp_port = udph->source;
247 * Bind the UDP port that we intend to use as our source port
248 * so that the kernel will not send an ICMP port unreachable
249 * message to the server
251 addr.sin_family = AF_INET;
252 addr.sin_port = udph->source;
253 addr.sin_addr.s_addr = iph->saddr;
254 retval = bind(priv->local_bind_sd, (struct sockaddr *)&addr,
257 printf("Failed to bind: %d %s\n", errno,
261 retval = sendto(priv->sd, packet, length, 0,
262 (struct sockaddr *)priv->device,
263 sizeof(struct sockaddr_ll));
265 printf("Failed to send packet: %d %s\n", errno,
272 int sandbox_eth_raw_os_recv(void *packet, int *length,
273 const struct eth_sandbox_raw_priv *priv)
278 if (priv->sd < 0 || !priv->device)
280 saddr_size = sizeof(struct sockaddr);
281 retval = recvfrom(priv->sd, packet, 1536, 0,
282 (struct sockaddr *)priv->device,
283 (socklen_t *)&saddr_size);
289 /* The socket is non-blocking, so expect EAGAIN when there is no data */
295 void sandbox_eth_raw_os_stop(struct eth_sandbox_raw_priv *priv)
302 if (priv->local_bind_sd != -1)
303 os_close(priv->local_bind_sd);
304 priv->local_bind_sd = -1;
305 priv->local_bind_udp_port = 0;