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>
18 #include <sys/types.h>
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
23 #include <arpa/inet.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_packet.h>
29 struct sandbox_eth_raw_if_nameindex *sandbox_eth_raw_if_nameindex(void)
31 return (struct sandbox_eth_raw_if_nameindex *)if_nameindex();
34 void sandbox_eth_raw_if_freenameindex(struct sandbox_eth_raw_if_nameindex *ptr)
36 if_freenameindex((struct if_nameindex *)ptr);
39 int sandbox_eth_raw_os_is_local(const char *ifname)
41 int fd = socket(AF_INET, SOCK_DGRAM, 0);
47 memset(&ifr, 0, sizeof(ifr));
48 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
49 ret = ioctl(fd, SIOCGIFFLAGS, &ifr);
54 ret = !!(ifr.ifr_flags & IFF_LOOPBACK);
60 int sandbox_eth_raw_os_idx_to_name(struct eth_sandbox_raw_priv *priv)
62 if (!if_indextoname(priv->host_ifindex, priv->host_ifname))
67 static int _raw_packet_start(struct eth_sandbox_raw_priv *priv,
68 unsigned char *ethmac)
70 struct sockaddr_ll *device;
71 struct packet_mreq mr;
75 /* Prepare device struct */
76 priv->local_bind_sd = -1;
77 priv->device = os_malloc(sizeof(struct sockaddr_ll));
78 if (priv->device == NULL)
80 device = priv->device;
81 memset(device, 0, sizeof(struct sockaddr_ll));
82 device->sll_ifindex = if_nametoindex(priv->host_ifname);
83 priv->host_ifindex = device->sll_ifindex;
84 device->sll_family = AF_PACKET;
85 memcpy(device->sll_addr, ethmac, 6);
86 device->sll_halen = htons(6);
89 priv->sd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
91 printf("Failed to open socket: %d %s\n", errno,
95 /* Bind to the specified interface */
96 ret = setsockopt(priv->sd, SOL_SOCKET, SO_BINDTODEVICE,
97 priv->host_ifname, strlen(priv->host_ifname) + 1);
99 printf("Failed to bind to '%s': %d %s\n", priv->host_ifname,
100 errno, strerror(errno));
104 /* Make the socket non-blocking */
105 flags = fcntl(priv->sd, F_GETFL, 0);
106 fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
108 /* Enable promiscuous mode to receive responses meant for us */
109 mr.mr_ifindex = device->sll_ifindex;
110 mr.mr_type = PACKET_MR_PROMISC;
111 ret = setsockopt(priv->sd, SOL_PACKET, PACKET_ADD_MEMBERSHIP,
116 printf("Failed to set promiscuous mode: %d %s\n"
117 "Falling back to the old \"flags\" way...\n",
118 errno, strerror(errno));
119 if (strlen(priv->host_ifname) >= IFNAMSIZ) {
120 printf("Interface name %s is too long.\n",
124 strncpy(ifr.ifr_name, priv->host_ifname, IFNAMSIZ);
125 if (ioctl(priv->sd, SIOCGIFFLAGS, &ifr) < 0) {
126 printf("Failed to read flags: %d %s\n", errno,
130 ifr.ifr_flags |= IFF_PROMISC;
131 if (ioctl(priv->sd, SIOCSIFFLAGS, &ifr) < 0) {
132 printf("Failed to write flags: %d %s\n", errno,
140 static int _local_inet_start(struct eth_sandbox_raw_priv *priv)
142 struct sockaddr_in *device;
147 /* Prepare device struct */
148 priv->local_bind_sd = -1;
149 priv->local_bind_udp_port = 0;
150 priv->device = os_malloc(sizeof(struct sockaddr_in));
151 if (priv->device == NULL)
153 device = priv->device;
154 memset(device, 0, sizeof(struct sockaddr_in));
155 device->sin_family = AF_INET;
156 device->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
160 * Since we specify UDP here, any incoming ICMP packets will
161 * not be received, so things like ping will not work on this
162 * localhost interface.
164 priv->sd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
166 printf("Failed to open socket: %d %s\n", errno,
171 /* Make the socket non-blocking */
172 flags = fcntl(priv->sd, F_GETFL, 0);
173 fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
175 /* Include the UDP/IP headers on send and receive */
176 ret = setsockopt(priv->sd, IPPROTO_IP, IP_HDRINCL, &one,
179 printf("Failed to set header include option: %d %s\n", errno,
186 int sandbox_eth_raw_os_start(struct eth_sandbox_raw_priv *priv,
187 unsigned char *ethmac)
190 return _local_inet_start(priv);
192 return _raw_packet_start(priv, ethmac);
195 int sandbox_eth_raw_os_send(void *packet, int length,
196 struct eth_sandbox_raw_priv *priv)
199 struct udphdr *udph = packet + sizeof(struct iphdr);
201 if (priv->sd < 0 || !priv->device)
205 * This block of code came about when testing tftp on the localhost
206 * interface. When using the RAW AF_INET API, the network stack is still
207 * in play responding to incoming traffic based on open "ports". Since
208 * it is raw (at the IP layer, no Ethernet) the network stack tells the
209 * TFTP server that the port it responded to is closed. This causes the
210 * TFTP transfer to be aborted. This block of code inspects the outgoing
211 * packet as formulated by the u-boot network stack to determine the
212 * source port (that the TFTP server will send packets back to) and
213 * opens a typical UDP socket on that port, thus preventing the network
214 * stack from sending that ICMP message claiming that the port has no
217 if (priv->local && (priv->local_bind_sd == -1 ||
218 priv->local_bind_udp_port != udph->source)) {
219 struct iphdr *iph = packet;
220 struct sockaddr_in addr;
222 if (priv->local_bind_sd != -1)
223 close(priv->local_bind_sd);
225 /* A normal UDP socket is required to bind */
226 priv->local_bind_sd = socket(AF_INET, SOCK_DGRAM, 0);
227 if (priv->local_bind_sd < 0) {
228 printf("Failed to open bind sd: %d %s\n", errno,
232 priv->local_bind_udp_port = udph->source;
235 * Bind the UDP port that we intend to use as our source port
236 * so that the kernel will not send an ICMP port unreachable
237 * message to the server
239 addr.sin_family = AF_INET;
240 addr.sin_port = udph->source;
241 addr.sin_addr.s_addr = iph->saddr;
242 retval = bind(priv->local_bind_sd, (struct sockaddr *)&addr,
245 printf("Failed to bind: %d %s\n", errno,
249 retval = sendto(priv->sd, packet, length, 0,
250 (struct sockaddr *)priv->device,
251 sizeof(struct sockaddr_ll));
253 printf("Failed to send packet: %d %s\n", errno,
260 int sandbox_eth_raw_os_recv(void *packet, int *length,
261 const struct eth_sandbox_raw_priv *priv)
266 if (priv->sd < 0 || !priv->device)
268 saddr_size = sizeof(struct sockaddr);
269 retval = recvfrom(priv->sd, packet, 1536, 0,
270 (struct sockaddr *)priv->device,
271 (socklen_t *)&saddr_size);
277 /* The socket is non-blocking, so expect EAGAIN when there is no data */
283 void sandbox_eth_raw_os_stop(struct eth_sandbox_raw_priv *priv)
285 os_free(priv->device);
290 if (priv->local_bind_sd != -1)
291 close(priv->local_bind_sd);
292 priv->local_bind_sd = -1;
293 priv->local_bind_udp_port = 0;