blob: df7acaa0bc8480ec01cad2a0b1d3543bbf3841f9 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Joe Hershbergera346ca72015-03-22 17:09:21 -05002/*
Joe Hershbergerac132702018-07-02 14:47:51 -05003 * Copyright (c) 2015-2018 National Instruments
4 * Copyright (c) 2015-2018 Joe Hershberger <joe.hershberger@ni.com>
Joe Hershbergera346ca72015-03-22 17:09:21 -05005 */
6
7#include <asm/eth-raw-os.h>
8#include <errno.h>
9#include <fcntl.h>
10#include <net/if.h>
11#include <netinet/in.h>
Joe Hershberger22f68522015-03-22 17:09:23 -050012#include <netinet/ip.h>
13#include <netinet/udp.h>
Joe Hershbergera346ca72015-03-22 17:09:21 -050014#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <sys/types.h>
18#include <sys/ioctl.h>
19#include <sys/socket.h>
20#include <unistd.h>
21
Joe Hershberger22f68522015-03-22 17:09:23 -050022#include <arpa/inet.h>
Joe Hershbergera346ca72015-03-22 17:09:21 -050023#include <linux/if_ether.h>
24#include <linux/if_packet.h>
25
Joe Hershbergerac132702018-07-02 14:47:51 -050026int sandbox_eth_raw_os_is_local(const char *ifname)
27{
28 int fd = socket(AF_INET, SOCK_DGRAM, 0);
29 struct ifreq ifr;
30 int ret = 0;
31
32 if (fd < 0)
33 return -errno;
34 memset(&ifr, 0, sizeof(ifr));
35 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
36 ret = ioctl(fd, SIOCGIFFLAGS, &ifr);
37 if (ret < 0) {
38 ret = -errno;
39 goto out;
40 }
41 ret = !!(ifr.ifr_flags & IFF_LOOPBACK);
42out:
43 close(fd);
44 return ret;
45}
46
Joe Hershbergerc9e2caf2018-07-02 14:47:52 -050047int sandbox_eth_raw_os_idx_to_name(struct eth_sandbox_raw_priv *priv)
48{
49 if (!if_indextoname(priv->host_ifindex, priv->host_ifname))
50 return -errno;
51 return 0;
52}
53
Joe Hershberger8c7988b2018-07-02 14:47:50 -050054static int _raw_packet_start(struct eth_sandbox_raw_priv *priv,
55 unsigned char *ethmac)
Joe Hershbergera346ca72015-03-22 17:09:21 -050056{
57 struct sockaddr_ll *device;
58 struct packet_mreq mr;
59 int ret;
60 int flags;
61
62 /* Prepare device struct */
Joe Hershberger50ed0ef2018-07-02 14:47:47 -050063 priv->local_bind_sd = -1;
Joe Hershbergera346ca72015-03-22 17:09:21 -050064 priv->device = malloc(sizeof(struct sockaddr_ll));
65 if (priv->device == NULL)
66 return -ENOMEM;
67 device = priv->device;
68 memset(device, 0, sizeof(struct sockaddr_ll));
Joe Hershberger8c7988b2018-07-02 14:47:50 -050069 device->sll_ifindex = if_nametoindex(priv->host_ifname);
70 priv->host_ifindex = device->sll_ifindex;
Joe Hershbergera346ca72015-03-22 17:09:21 -050071 device->sll_family = AF_PACKET;
72 memcpy(device->sll_addr, ethmac, 6);
73 device->sll_halen = htons(6);
74
75 /* Open socket */
76 priv->sd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
77 if (priv->sd < 0) {
78 printf("Failed to open socket: %d %s\n", errno,
79 strerror(errno));
80 return -errno;
81 }
82 /* Bind to the specified interface */
Joe Hershberger8c7988b2018-07-02 14:47:50 -050083 ret = setsockopt(priv->sd, SOL_SOCKET, SO_BINDTODEVICE,
84 priv->host_ifname, strlen(priv->host_ifname) + 1);
Joe Hershbergera346ca72015-03-22 17:09:21 -050085 if (ret < 0) {
Joe Hershberger8c7988b2018-07-02 14:47:50 -050086 printf("Failed to bind to '%s': %d %s\n", priv->host_ifname,
87 errno, strerror(errno));
Joe Hershbergera346ca72015-03-22 17:09:21 -050088 return -errno;
89 }
90
91 /* Make the socket non-blocking */
92 flags = fcntl(priv->sd, F_GETFL, 0);
93 fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
94
95 /* Enable promiscuous mode to receive responses meant for us */
96 mr.mr_ifindex = device->sll_ifindex;
97 mr.mr_type = PACKET_MR_PROMISC;
98 ret = setsockopt(priv->sd, SOL_PACKET, PACKET_ADD_MEMBERSHIP,
99 &mr, sizeof(mr));
100 if (ret < 0) {
101 struct ifreq ifr;
102
103 printf("Failed to set promiscuous mode: %d %s\n"
104 "Falling back to the old \"flags\" way...\n",
105 errno, strerror(errno));
Joe Hershberger8c7988b2018-07-02 14:47:50 -0500106 if (strlen(priv->host_ifname) >= IFNAMSIZ) {
107 printf("Interface name %s is too long.\n",
108 priv->host_ifname);
Tom Riniab971e12015-12-07 22:26:34 -0500109 return -EINVAL;
110 }
Joe Hershberger8c7988b2018-07-02 14:47:50 -0500111 strncpy(ifr.ifr_name, priv->host_ifname, IFNAMSIZ);
Joe Hershbergera346ca72015-03-22 17:09:21 -0500112 if (ioctl(priv->sd, SIOCGIFFLAGS, &ifr) < 0) {
113 printf("Failed to read flags: %d %s\n", errno,
114 strerror(errno));
115 return -errno;
116 }
117 ifr.ifr_flags |= IFF_PROMISC;
118 if (ioctl(priv->sd, SIOCSIFFLAGS, &ifr) < 0) {
119 printf("Failed to write flags: %d %s\n", errno,
120 strerror(errno));
121 return -errno;
122 }
123 }
124 return 0;
125}
126
Joe Hershberger22f68522015-03-22 17:09:23 -0500127static int _local_inet_start(struct eth_sandbox_raw_priv *priv)
128{
129 struct sockaddr_in *device;
130 int ret;
131 int flags;
132 int one = 1;
133
134 /* Prepare device struct */
Joe Hershberger50ed0ef2018-07-02 14:47:47 -0500135 priv->local_bind_sd = -1;
136 priv->local_bind_udp_port = 0;
Joe Hershberger22f68522015-03-22 17:09:23 -0500137 priv->device = malloc(sizeof(struct sockaddr_in));
138 if (priv->device == NULL)
139 return -ENOMEM;
140 device = priv->device;
141 memset(device, 0, sizeof(struct sockaddr_in));
142 device->sin_family = AF_INET;
143 device->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
144
145 /**
146 * Open socket
147 * Since we specify UDP here, any incoming ICMP packets will
148 * not be received, so things like ping will not work on this
149 * localhost interface.
150 */
151 priv->sd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
152 if (priv->sd < 0) {
153 printf("Failed to open socket: %d %s\n", errno,
154 strerror(errno));
155 return -errno;
156 }
157
158 /* Make the socket non-blocking */
159 flags = fcntl(priv->sd, F_GETFL, 0);
160 fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
161
162 /* Include the UDP/IP headers on send and receive */
163 ret = setsockopt(priv->sd, IPPROTO_IP, IP_HDRINCL, &one,
164 sizeof(one));
165 if (ret < 0) {
166 printf("Failed to set header include option: %d %s\n", errno,
167 strerror(errno));
168 return -errno;
169 }
Joe Hershberger22f68522015-03-22 17:09:23 -0500170 return 0;
171}
172
Joe Hershberger8c7988b2018-07-02 14:47:50 -0500173int sandbox_eth_raw_os_start(struct eth_sandbox_raw_priv *priv,
174 unsigned char *ethmac)
Joe Hershberger22f68522015-03-22 17:09:23 -0500175{
176 if (priv->local)
177 return _local_inet_start(priv);
178 else
Joe Hershberger8c7988b2018-07-02 14:47:50 -0500179 return _raw_packet_start(priv, ethmac);
Joe Hershberger22f68522015-03-22 17:09:23 -0500180}
181
Joe Hershbergera346ca72015-03-22 17:09:21 -0500182int sandbox_eth_raw_os_send(void *packet, int length,
Joe Hershberger22f68522015-03-22 17:09:23 -0500183 struct eth_sandbox_raw_priv *priv)
Joe Hershbergera346ca72015-03-22 17:09:21 -0500184{
185 int retval;
Joe Hershberger22f68522015-03-22 17:09:23 -0500186 struct udphdr *udph = packet + sizeof(struct iphdr);
Joe Hershbergera346ca72015-03-22 17:09:21 -0500187
Joe Hershberger97367df2018-07-02 14:47:44 -0500188 if (priv->sd < 0 || !priv->device)
Joe Hershbergera346ca72015-03-22 17:09:21 -0500189 return -EINVAL;
190
Joe Hershberger22f68522015-03-22 17:09:23 -0500191 /*
192 * This block of code came about when testing tftp on the localhost
193 * interface. When using the RAW AF_INET API, the network stack is still
194 * in play responding to incoming traffic based on open "ports". Since
195 * it is raw (at the IP layer, no Ethernet) the network stack tells the
196 * TFTP server that the port it responded to is closed. This causes the
197 * TFTP transfer to be aborted. This block of code inspects the outgoing
198 * packet as formulated by the u-boot network stack to determine the
199 * source port (that the TFTP server will send packets back to) and
200 * opens a typical UDP socket on that port, thus preventing the network
201 * stack from sending that ICMP message claiming that the port has no
202 * bound socket.
203 */
204 if (priv->local && (priv->local_bind_sd == -1 ||
205 priv->local_bind_udp_port != udph->source)) {
206 struct iphdr *iph = packet;
207 struct sockaddr_in addr;
208
209 if (priv->local_bind_sd != -1)
210 close(priv->local_bind_sd);
211
212 /* A normal UDP socket is required to bind */
213 priv->local_bind_sd = socket(AF_INET, SOCK_DGRAM, 0);
214 if (priv->local_bind_sd < 0) {
215 printf("Failed to open bind sd: %d %s\n", errno,
216 strerror(errno));
217 return -errno;
218 }
219 priv->local_bind_udp_port = udph->source;
220
221 /**
222 * Bind the UDP port that we intend to use as our source port
223 * so that the kernel will not send an ICMP port unreachable
224 * message to the server
225 */
226 addr.sin_family = AF_INET;
227 addr.sin_port = udph->source;
228 addr.sin_addr.s_addr = iph->saddr;
Tom Rini71229092015-11-28 08:04:40 -0500229 retval = bind(priv->local_bind_sd, (struct sockaddr *)&addr,
230 sizeof(addr));
Joe Hershberger22f68522015-03-22 17:09:23 -0500231 if (retval < 0)
232 printf("Failed to bind: %d %s\n", errno,
233 strerror(errno));
234 }
235
Joe Hershbergera346ca72015-03-22 17:09:21 -0500236 retval = sendto(priv->sd, packet, length, 0,
237 (struct sockaddr *)priv->device,
238 sizeof(struct sockaddr_ll));
239 if (retval < 0) {
240 printf("Failed to send packet: %d %s\n", errno,
241 strerror(errno));
242 return -errno;
243 }
244 return retval;
245}
246
247int sandbox_eth_raw_os_recv(void *packet, int *length,
248 const struct eth_sandbox_raw_priv *priv)
249{
250 int retval;
251 int saddr_size;
252
Joe Hershberger97367df2018-07-02 14:47:44 -0500253 if (priv->sd < 0 || !priv->device)
Joe Hershbergera346ca72015-03-22 17:09:21 -0500254 return -EINVAL;
255 saddr_size = sizeof(struct sockaddr);
256 retval = recvfrom(priv->sd, packet, 1536, 0,
257 (struct sockaddr *)priv->device,
258 (socklen_t *)&saddr_size);
259 *length = 0;
260 if (retval >= 0) {
261 *length = retval;
262 return 0;
263 }
264 /* The socket is non-blocking, so expect EAGAIN when there is no data */
265 if (errno == EAGAIN)
266 return 0;
267 return -errno;
268}
269
270void sandbox_eth_raw_os_stop(struct eth_sandbox_raw_priv *priv)
271{
272 free(priv->device);
273 priv->device = NULL;
274 close(priv->sd);
275 priv->sd = -1;
Joe Hershberger22f68522015-03-22 17:09:23 -0500276 if (priv->local) {
277 if (priv->local_bind_sd != -1)
278 close(priv->local_bind_sd);
279 priv->local_bind_sd = -1;
280 priv->local_bind_udp_port = 0;
281 }
Joe Hershbergera346ca72015-03-22 17:09:21 -0500282}