1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015 National Instruments
6 * Joe Hershberger <joe.hershberger@ni.com>
9 #include <asm/eth-raw-os.h>
13 #include <netinet/in.h>
14 #include <netinet/ip.h>
15 #include <netinet/udp.h>
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #include <sys/socket.h>
24 #include <arpa/inet.h>
25 #include <linux/if_ether.h>
26 #include <linux/if_packet.h>
28 static int _raw_packet_start(const char *ifname, unsigned char *ethmac,
29 struct eth_sandbox_raw_priv *priv)
31 struct sockaddr_ll *device;
32 struct packet_mreq mr;
36 /* Prepare device struct */
37 priv->device = malloc(sizeof(struct sockaddr_ll));
38 if (priv->device == NULL)
40 device = priv->device;
41 memset(device, 0, sizeof(struct sockaddr_ll));
42 device->sll_ifindex = if_nametoindex(ifname);
43 device->sll_family = AF_PACKET;
44 memcpy(device->sll_addr, ethmac, 6);
45 device->sll_halen = htons(6);
48 priv->sd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
50 printf("Failed to open socket: %d %s\n", errno,
54 /* Bind to the specified interface */
55 ret = setsockopt(priv->sd, SOL_SOCKET, SO_BINDTODEVICE, ifname,
58 printf("Failed to bind to '%s': %d %s\n", ifname, errno,
63 /* Make the socket non-blocking */
64 flags = fcntl(priv->sd, F_GETFL, 0);
65 fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
67 /* Enable promiscuous mode to receive responses meant for us */
68 mr.mr_ifindex = device->sll_ifindex;
69 mr.mr_type = PACKET_MR_PROMISC;
70 ret = setsockopt(priv->sd, SOL_PACKET, PACKET_ADD_MEMBERSHIP,
75 printf("Failed to set promiscuous mode: %d %s\n"
76 "Falling back to the old \"flags\" way...\n",
77 errno, strerror(errno));
78 if (strlen(ifname) >= IFNAMSIZ) {
79 printf("Interface name %s is too long.\n", ifname);
82 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
83 if (ioctl(priv->sd, SIOCGIFFLAGS, &ifr) < 0) {
84 printf("Failed to read flags: %d %s\n", errno,
88 ifr.ifr_flags |= IFF_PROMISC;
89 if (ioctl(priv->sd, SIOCSIFFLAGS, &ifr) < 0) {
90 printf("Failed to write flags: %d %s\n", errno,
98 static int _local_inet_start(struct eth_sandbox_raw_priv *priv)
100 struct sockaddr_in *device;
105 /* Prepare device struct */
106 priv->device = malloc(sizeof(struct sockaddr_in));
107 if (priv->device == NULL)
109 device = priv->device;
110 memset(device, 0, sizeof(struct sockaddr_in));
111 device->sin_family = AF_INET;
112 device->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
116 * Since we specify UDP here, any incoming ICMP packets will
117 * not be received, so things like ping will not work on this
118 * localhost interface.
120 priv->sd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
122 printf("Failed to open socket: %d %s\n", errno,
127 /* Make the socket non-blocking */
128 flags = fcntl(priv->sd, F_GETFL, 0);
129 fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
131 /* Include the UDP/IP headers on send and receive */
132 ret = setsockopt(priv->sd, IPPROTO_IP, IP_HDRINCL, &one,
135 printf("Failed to set header include option: %d %s\n", errno,
139 priv->local_bind_sd = -1;
140 priv->local_bind_udp_port = 0;
144 int sandbox_eth_raw_os_start(const char *ifname, unsigned char *ethmac,
145 struct eth_sandbox_raw_priv *priv)
148 return _local_inet_start(priv);
150 return _raw_packet_start(ifname, ethmac, priv);
153 int sandbox_eth_raw_os_send(void *packet, int length,
154 struct eth_sandbox_raw_priv *priv)
157 struct udphdr *udph = packet + sizeof(struct iphdr);
159 if (!priv->sd || !priv->device)
163 * This block of code came about when testing tftp on the localhost
164 * interface. When using the RAW AF_INET API, the network stack is still
165 * in play responding to incoming traffic based on open "ports". Since
166 * it is raw (at the IP layer, no Ethernet) the network stack tells the
167 * TFTP server that the port it responded to is closed. This causes the
168 * TFTP transfer to be aborted. This block of code inspects the outgoing
169 * packet as formulated by the u-boot network stack to determine the
170 * source port (that the TFTP server will send packets back to) and
171 * opens a typical UDP socket on that port, thus preventing the network
172 * stack from sending that ICMP message claiming that the port has no
175 if (priv->local && (priv->local_bind_sd == -1 ||
176 priv->local_bind_udp_port != udph->source)) {
177 struct iphdr *iph = packet;
178 struct sockaddr_in addr;
180 if (priv->local_bind_sd != -1)
181 close(priv->local_bind_sd);
183 /* A normal UDP socket is required to bind */
184 priv->local_bind_sd = socket(AF_INET, SOCK_DGRAM, 0);
185 if (priv->local_bind_sd < 0) {
186 printf("Failed to open bind sd: %d %s\n", errno,
190 priv->local_bind_udp_port = udph->source;
193 * Bind the UDP port that we intend to use as our source port
194 * so that the kernel will not send an ICMP port unreachable
195 * message to the server
197 addr.sin_family = AF_INET;
198 addr.sin_port = udph->source;
199 addr.sin_addr.s_addr = iph->saddr;
200 retval = bind(priv->local_bind_sd, (struct sockaddr *)&addr,
203 printf("Failed to bind: %d %s\n", errno,
207 retval = sendto(priv->sd, packet, length, 0,
208 (struct sockaddr *)priv->device,
209 sizeof(struct sockaddr_ll));
211 printf("Failed to send packet: %d %s\n", errno,
218 int sandbox_eth_raw_os_recv(void *packet, int *length,
219 const struct eth_sandbox_raw_priv *priv)
224 if (!priv->sd || !priv->device)
226 saddr_size = sizeof(struct sockaddr);
227 retval = recvfrom(priv->sd, packet, 1536, 0,
228 (struct sockaddr *)priv->device,
229 (socklen_t *)&saddr_size);
235 /* The socket is non-blocking, so expect EAGAIN when there is no data */
241 void sandbox_eth_raw_os_stop(struct eth_sandbox_raw_priv *priv)
248 if (priv->local_bind_sd != -1)
249 close(priv->local_bind_sd);
250 priv->local_bind_sd = -1;
251 priv->local_bind_udp_port = 0;