2 * Copyright (c) 2015 National Instruments
5 * Joe Hershberger <joe.hershberger@ni.com>
7 * SPDX-License-Identifier: GPL-2.0
10 #include <asm/eth-raw-os.h>
14 #include <netinet/in.h>
15 #include <netinet/ip.h>
16 #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>
29 static int _raw_packet_start(const char *ifname, unsigned char *ethmac,
30 struct eth_sandbox_raw_priv *priv)
32 struct sockaddr_ll *device;
33 struct packet_mreq mr;
37 /* Prepare device struct */
38 priv->device = malloc(sizeof(struct sockaddr_ll));
39 if (priv->device == NULL)
41 device = priv->device;
42 memset(device, 0, sizeof(struct sockaddr_ll));
43 device->sll_ifindex = if_nametoindex(ifname);
44 device->sll_family = AF_PACKET;
45 memcpy(device->sll_addr, ethmac, 6);
46 device->sll_halen = htons(6);
49 priv->sd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
51 printf("Failed to open socket: %d %s\n", errno,
55 /* Bind to the specified interface */
56 ret = setsockopt(priv->sd, SOL_SOCKET, SO_BINDTODEVICE, ifname,
59 printf("Failed to bind to '%s': %d %s\n", ifname, errno,
64 /* Make the socket non-blocking */
65 flags = fcntl(priv->sd, F_GETFL, 0);
66 fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
68 /* Enable promiscuous mode to receive responses meant for us */
69 mr.mr_ifindex = device->sll_ifindex;
70 mr.mr_type = PACKET_MR_PROMISC;
71 ret = setsockopt(priv->sd, SOL_PACKET, PACKET_ADD_MEMBERSHIP,
76 printf("Failed to set promiscuous mode: %d %s\n"
77 "Falling back to the old \"flags\" way...\n",
78 errno, strerror(errno));
79 if (strlen(ifname) >= IFNAMSIZ) {
80 printf("Interface name %s is too long.\n", ifname);
83 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
84 if (ioctl(priv->sd, SIOCGIFFLAGS, &ifr) < 0) {
85 printf("Failed to read flags: %d %s\n", errno,
89 ifr.ifr_flags |= IFF_PROMISC;
90 if (ioctl(priv->sd, SIOCSIFFLAGS, &ifr) < 0) {
91 printf("Failed to write flags: %d %s\n", errno,
99 static int _local_inet_start(struct eth_sandbox_raw_priv *priv)
101 struct sockaddr_in *device;
106 /* Prepare device struct */
107 priv->device = malloc(sizeof(struct sockaddr_in));
108 if (priv->device == NULL)
110 device = priv->device;
111 memset(device, 0, sizeof(struct sockaddr_in));
112 device->sin_family = AF_INET;
113 device->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
117 * Since we specify UDP here, any incoming ICMP packets will
118 * not be received, so things like ping will not work on this
119 * localhost interface.
121 priv->sd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
123 printf("Failed to open socket: %d %s\n", errno,
128 /* Make the socket non-blocking */
129 flags = fcntl(priv->sd, F_GETFL, 0);
130 fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
132 /* Include the UDP/IP headers on send and receive */
133 ret = setsockopt(priv->sd, IPPROTO_IP, IP_HDRINCL, &one,
136 printf("Failed to set header include option: %d %s\n", errno,
140 priv->local_bind_sd = -1;
141 priv->local_bind_udp_port = 0;
145 int sandbox_eth_raw_os_start(const char *ifname, unsigned char *ethmac,
146 struct eth_sandbox_raw_priv *priv)
149 return _local_inet_start(priv);
151 return _raw_packet_start(ifname, ethmac, priv);
154 int sandbox_eth_raw_os_send(void *packet, int length,
155 struct eth_sandbox_raw_priv *priv)
158 struct udphdr *udph = packet + sizeof(struct iphdr);
160 if (!priv->sd || !priv->device)
164 * This block of code came about when testing tftp on the localhost
165 * interface. When using the RAW AF_INET API, the network stack is still
166 * in play responding to incoming traffic based on open "ports". Since
167 * it is raw (at the IP layer, no Ethernet) the network stack tells the
168 * TFTP server that the port it responded to is closed. This causes the
169 * TFTP transfer to be aborted. This block of code inspects the outgoing
170 * packet as formulated by the u-boot network stack to determine the
171 * source port (that the TFTP server will send packets back to) and
172 * opens a typical UDP socket on that port, thus preventing the network
173 * stack from sending that ICMP message claiming that the port has no
176 if (priv->local && (priv->local_bind_sd == -1 ||
177 priv->local_bind_udp_port != udph->source)) {
178 struct iphdr *iph = packet;
179 struct sockaddr_in addr;
181 if (priv->local_bind_sd != -1)
182 close(priv->local_bind_sd);
184 /* A normal UDP socket is required to bind */
185 priv->local_bind_sd = socket(AF_INET, SOCK_DGRAM, 0);
186 if (priv->local_bind_sd < 0) {
187 printf("Failed to open bind sd: %d %s\n", errno,
191 priv->local_bind_udp_port = udph->source;
194 * Bind the UDP port that we intend to use as our source port
195 * so that the kernel will not send an ICMP port unreachable
196 * message to the server
198 addr.sin_family = AF_INET;
199 addr.sin_port = udph->source;
200 addr.sin_addr.s_addr = iph->saddr;
201 retval = bind(priv->local_bind_sd, (struct sockaddr *)&addr,
204 printf("Failed to bind: %d %s\n", errno,
208 retval = sendto(priv->sd, packet, length, 0,
209 (struct sockaddr *)priv->device,
210 sizeof(struct sockaddr_ll));
212 printf("Failed to send packet: %d %s\n", errno,
219 int sandbox_eth_raw_os_recv(void *packet, int *length,
220 const struct eth_sandbox_raw_priv *priv)
225 if (!priv->sd || !priv->device)
227 saddr_size = sizeof(struct sockaddr);
228 retval = recvfrom(priv->sd, packet, 1536, 0,
229 (struct sockaddr *)priv->device,
230 (socklen_t *)&saddr_size);
236 /* The socket is non-blocking, so expect EAGAIN when there is no data */
242 void sandbox_eth_raw_os_stop(struct eth_sandbox_raw_priv *priv)
249 if (priv->local_bind_sd != -1)
250 close(priv->local_bind_sd);
251 priv->local_bind_sd = -1;
252 priv->local_bind_udp_port = 0;