blob: 39ea3b3f0128fe4af877a4dc55e5bd2d2e2c2192 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Joe Hershberger586cbd12015-03-22 17:09:21 -05002/*
Joe Hershberger1b312cb2018-07-02 14:47:51 -05003 * Copyright (c) 2015-2018 National Instruments
4 * Copyright (c) 2015-2018 Joe Hershberger <joe.hershberger@ni.com>
Joe Hershberger586cbd12015-03-22 17:09:21 -05005 */
6
Heinrich Schuchardtd6d7adb2022-01-21 18:01:23 +01007#define _GNU_SOURCE
8
Joe Hershberger586cbd12015-03-22 17:09:21 -05009#include <asm/eth-raw-os.h>
10#include <errno.h>
11#include <fcntl.h>
12#include <net/if.h>
13#include <netinet/in.h>
Joe Hershbergera8921922015-03-22 17:09:23 -050014#include <netinet/ip.h>
15#include <netinet/udp.h>
Simon Glass60097632018-11-15 18:44:06 -070016#include <stdbool.h>
Joe Hershberger586cbd12015-03-22 17:09:21 -050017#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <sys/types.h>
21#include <sys/ioctl.h>
22#include <sys/socket.h>
23#include <unistd.h>
24
Joe Hershbergera8921922015-03-22 17:09:23 -050025#include <arpa/inet.h>
Joe Hershberger586cbd12015-03-22 17:09:21 -050026#include <linux/if_ether.h>
27#include <linux/if_packet.h>
28
Simon Glass60097632018-11-15 18:44:06 -070029#include <os.h>
30
Joe Hershberger89590c82018-07-02 14:47:54 -050031struct sandbox_eth_raw_if_nameindex *sandbox_eth_raw_if_nameindex(void)
32{
33 return (struct sandbox_eth_raw_if_nameindex *)if_nameindex();
34}
35
36void sandbox_eth_raw_if_freenameindex(struct sandbox_eth_raw_if_nameindex *ptr)
37{
38 if_freenameindex((struct if_nameindex *)ptr);
39}
40
Joe Hershberger1b312cb2018-07-02 14:47:51 -050041int sandbox_eth_raw_os_is_local(const char *ifname)
42{
43 int fd = socket(AF_INET, SOCK_DGRAM, 0);
44 struct ifreq ifr;
45 int ret = 0;
46
47 if (fd < 0)
48 return -errno;
49 memset(&ifr, 0, sizeof(ifr));
50 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
51 ret = ioctl(fd, SIOCGIFFLAGS, &ifr);
52 if (ret < 0) {
53 ret = -errno;
54 goto out;
55 }
56 ret = !!(ifr.ifr_flags & IFF_LOOPBACK);
57out:
Heinrich Schuchardt69db2ee2020-10-27 20:29:21 +010058 os_close(fd);
Joe Hershberger1b312cb2018-07-02 14:47:51 -050059 return ret;
60}
61
Joe Hershberger144e14b2018-07-02 14:47:52 -050062int sandbox_eth_raw_os_idx_to_name(struct eth_sandbox_raw_priv *priv)
63{
64 if (!if_indextoname(priv->host_ifindex, priv->host_ifname))
65 return -errno;
66 return 0;
67}
68
Joe Hershbergere5691402018-07-02 14:47:50 -050069static int _raw_packet_start(struct eth_sandbox_raw_priv *priv,
70 unsigned char *ethmac)
Joe Hershberger586cbd12015-03-22 17:09:21 -050071{
72 struct sockaddr_ll *device;
73 struct packet_mreq mr;
74 int ret;
75 int flags;
76
77 /* Prepare device struct */
Joe Hershbergerf9d41682018-07-02 14:47:47 -050078 priv->local_bind_sd = -1;
Simon Glassfa252262020-02-03 07:36:02 -070079 priv->device = malloc(sizeof(struct sockaddr_ll));
Joe Hershberger586cbd12015-03-22 17:09:21 -050080 if (priv->device == NULL)
81 return -ENOMEM;
82 device = priv->device;
83 memset(device, 0, sizeof(struct sockaddr_ll));
Joe Hershbergere5691402018-07-02 14:47:50 -050084 device->sll_ifindex = if_nametoindex(priv->host_ifname);
85 priv->host_ifindex = device->sll_ifindex;
Joe Hershberger586cbd12015-03-22 17:09:21 -050086 device->sll_family = AF_PACKET;
87 memcpy(device->sll_addr, ethmac, 6);
88 device->sll_halen = htons(6);
89
90 /* Open socket */
91 priv->sd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
92 if (priv->sd < 0) {
93 printf("Failed to open socket: %d %s\n", errno,
94 strerror(errno));
95 return -errno;
96 }
97 /* Bind to the specified interface */
Joe Hershbergere5691402018-07-02 14:47:50 -050098 ret = setsockopt(priv->sd, SOL_SOCKET, SO_BINDTODEVICE,
99 priv->host_ifname, strlen(priv->host_ifname) + 1);
Joe Hershberger586cbd12015-03-22 17:09:21 -0500100 if (ret < 0) {
Joe Hershbergere5691402018-07-02 14:47:50 -0500101 printf("Failed to bind to '%s': %d %s\n", priv->host_ifname,
102 errno, strerror(errno));
Joe Hershberger586cbd12015-03-22 17:09:21 -0500103 return -errno;
104 }
105
106 /* Make the socket non-blocking */
107 flags = fcntl(priv->sd, F_GETFL, 0);
Heinrich Schuchardt3efaa0e2024-01-07 09:27:12 +0100108 ret = fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
109 if (ret == -1) {
110 printf("Failed to make socket non-blocking: %d %s\n", errno,
111 strerror(errno));
112 return -errno;
113 }
Joe Hershberger586cbd12015-03-22 17:09:21 -0500114
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,
119 &mr, sizeof(mr));
120 if (ret < 0) {
121 struct ifreq ifr;
122
123 printf("Failed to set promiscuous mode: %d %s\n"
124 "Falling back to the old \"flags\" way...\n",
125 errno, strerror(errno));
Joe Hershbergere5691402018-07-02 14:47:50 -0500126 if (strlen(priv->host_ifname) >= IFNAMSIZ) {
127 printf("Interface name %s is too long.\n",
128 priv->host_ifname);
Tom Rinia5d3a4e2015-12-07 22:26:34 -0500129 return -EINVAL;
130 }
Joe Hershbergere5691402018-07-02 14:47:50 -0500131 strncpy(ifr.ifr_name, priv->host_ifname, IFNAMSIZ);
Joe Hershberger586cbd12015-03-22 17:09:21 -0500132 if (ioctl(priv->sd, SIOCGIFFLAGS, &ifr) < 0) {
133 printf("Failed to read flags: %d %s\n", errno,
134 strerror(errno));
135 return -errno;
136 }
137 ifr.ifr_flags |= IFF_PROMISC;
138 if (ioctl(priv->sd, SIOCSIFFLAGS, &ifr) < 0) {
139 printf("Failed to write flags: %d %s\n", errno,
140 strerror(errno));
141 return -errno;
142 }
143 }
Joe Hershbergera8921922015-03-22 17:09:23 -0500144 return 0;
145}
146
147static int _local_inet_start(struct eth_sandbox_raw_priv *priv)
148{
149 struct sockaddr_in *device;
150 int ret;
151 int flags;
152 int one = 1;
153
154 /* Prepare device struct */
Joe Hershbergerf9d41682018-07-02 14:47:47 -0500155 priv->local_bind_sd = -1;
156 priv->local_bind_udp_port = 0;
Simon Glassfa252262020-02-03 07:36:02 -0700157 priv->device = malloc(sizeof(struct sockaddr_in));
Joe Hershbergera8921922015-03-22 17:09:23 -0500158 if (priv->device == NULL)
159 return -ENOMEM;
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);
164
165 /**
166 * Open socket
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.
170 */
171 priv->sd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
172 if (priv->sd < 0) {
173 printf("Failed to open socket: %d %s\n", errno,
174 strerror(errno));
175 return -errno;
176 }
177
178 /* Make the socket non-blocking */
179 flags = fcntl(priv->sd, F_GETFL, 0);
Heinrich Schuchardt3efaa0e2024-01-07 09:27:12 +0100180 ret = fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
181 if (ret == -1) {
182 printf("Failed to make socket non-blocking: %d %s\n", errno,
183 strerror(errno));
184 return -errno;
185 }
Joe Hershbergera8921922015-03-22 17:09:23 -0500186
187 /* Include the UDP/IP headers on send and receive */
188 ret = setsockopt(priv->sd, IPPROTO_IP, IP_HDRINCL, &one,
189 sizeof(one));
190 if (ret < 0) {
191 printf("Failed to set header include option: %d %s\n", errno,
192 strerror(errno));
193 return -errno;
194 }
Joe Hershberger586cbd12015-03-22 17:09:21 -0500195 return 0;
196}
197
Joe Hershbergere5691402018-07-02 14:47:50 -0500198int sandbox_eth_raw_os_start(struct eth_sandbox_raw_priv *priv,
199 unsigned char *ethmac)
Joe Hershbergera8921922015-03-22 17:09:23 -0500200{
201 if (priv->local)
202 return _local_inet_start(priv);
203 else
Joe Hershbergere5691402018-07-02 14:47:50 -0500204 return _raw_packet_start(priv, ethmac);
Joe Hershbergera8921922015-03-22 17:09:23 -0500205}
206
Joe Hershberger586cbd12015-03-22 17:09:21 -0500207int sandbox_eth_raw_os_send(void *packet, int length,
Joe Hershbergera8921922015-03-22 17:09:23 -0500208 struct eth_sandbox_raw_priv *priv)
Joe Hershberger586cbd12015-03-22 17:09:21 -0500209{
210 int retval;
Joe Hershbergera8921922015-03-22 17:09:23 -0500211 struct udphdr *udph = packet + sizeof(struct iphdr);
Joe Hershberger586cbd12015-03-22 17:09:21 -0500212
Joe Hershberger0edb5252018-07-02 14:47:44 -0500213 if (priv->sd < 0 || !priv->device)
Joe Hershberger586cbd12015-03-22 17:09:21 -0500214 return -EINVAL;
215
Joe Hershbergera8921922015-03-22 17:09:23 -0500216 /*
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
227 * bound socket.
228 */
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;
233
234 if (priv->local_bind_sd != -1)
Heinrich Schuchardt69db2ee2020-10-27 20:29:21 +0100235 os_close(priv->local_bind_sd);
Joe Hershbergera8921922015-03-22 17:09:23 -0500236
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,
241 strerror(errno));
242 return -errno;
243 }
244 priv->local_bind_udp_port = udph->source;
245
246 /**
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
250 */
251 addr.sin_family = AF_INET;
252 addr.sin_port = udph->source;
253 addr.sin_addr.s_addr = iph->saddr;
Tom Rini8e2334e2015-11-28 08:04:40 -0500254 retval = bind(priv->local_bind_sd, (struct sockaddr *)&addr,
255 sizeof(addr));
Joe Hershbergera8921922015-03-22 17:09:23 -0500256 if (retval < 0)
257 printf("Failed to bind: %d %s\n", errno,
258 strerror(errno));
259 }
260
Joe Hershberger586cbd12015-03-22 17:09:21 -0500261 retval = sendto(priv->sd, packet, length, 0,
262 (struct sockaddr *)priv->device,
263 sizeof(struct sockaddr_ll));
264 if (retval < 0) {
265 printf("Failed to send packet: %d %s\n", errno,
266 strerror(errno));
267 return -errno;
268 }
Maxim Uvarov2e84c232023-12-26 21:46:14 +0600269 return 0;
Joe Hershberger586cbd12015-03-22 17:09:21 -0500270}
271
272int sandbox_eth_raw_os_recv(void *packet, int *length,
273 const struct eth_sandbox_raw_priv *priv)
274{
275 int retval;
276 int saddr_size;
277
Joe Hershberger0edb5252018-07-02 14:47:44 -0500278 if (priv->sd < 0 || !priv->device)
Joe Hershberger586cbd12015-03-22 17:09:21 -0500279 return -EINVAL;
280 saddr_size = sizeof(struct sockaddr);
281 retval = recvfrom(priv->sd, packet, 1536, 0,
282 (struct sockaddr *)priv->device,
283 (socklen_t *)&saddr_size);
284 *length = 0;
285 if (retval >= 0) {
286 *length = retval;
287 return 0;
288 }
289 /* The socket is non-blocking, so expect EAGAIN when there is no data */
290 if (errno == EAGAIN)
291 return 0;
292 return -errno;
293}
294
295void sandbox_eth_raw_os_stop(struct eth_sandbox_raw_priv *priv)
296{
Simon Glassfa252262020-02-03 07:36:02 -0700297 free(priv->device);
Joe Hershberger586cbd12015-03-22 17:09:21 -0500298 priv->device = NULL;
Heinrich Schuchardt69db2ee2020-10-27 20:29:21 +0100299 os_close(priv->sd);
Joe Hershberger586cbd12015-03-22 17:09:21 -0500300 priv->sd = -1;
Joe Hershbergera8921922015-03-22 17:09:23 -0500301 if (priv->local) {
302 if (priv->local_bind_sd != -1)
Heinrich Schuchardt69db2ee2020-10-27 20:29:21 +0100303 os_close(priv->local_bind_sd);
Joe Hershbergera8921922015-03-22 17:09:23 -0500304 priv->local_bind_sd = -1;
305 priv->local_bind_udp_port = 0;
306 }
Joe Hershberger586cbd12015-03-22 17:09:21 -0500307}