blob: 12ddb345d92f843afa661cfffc256525a1a38e1b [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 Hershberger8c7988b2018-07-02 14:47:50 -050047static int _raw_packet_start(struct eth_sandbox_raw_priv *priv,
48 unsigned char *ethmac)
Joe Hershbergera346ca72015-03-22 17:09:21 -050049{
50 struct sockaddr_ll *device;
51 struct packet_mreq mr;
52 int ret;
53 int flags;
54
55 /* Prepare device struct */
Joe Hershberger50ed0ef2018-07-02 14:47:47 -050056 priv->local_bind_sd = -1;
Joe Hershbergera346ca72015-03-22 17:09:21 -050057 priv->device = malloc(sizeof(struct sockaddr_ll));
58 if (priv->device == NULL)
59 return -ENOMEM;
60 device = priv->device;
61 memset(device, 0, sizeof(struct sockaddr_ll));
Joe Hershberger8c7988b2018-07-02 14:47:50 -050062 device->sll_ifindex = if_nametoindex(priv->host_ifname);
63 priv->host_ifindex = device->sll_ifindex;
Joe Hershbergera346ca72015-03-22 17:09:21 -050064 device->sll_family = AF_PACKET;
65 memcpy(device->sll_addr, ethmac, 6);
66 device->sll_halen = htons(6);
67
68 /* Open socket */
69 priv->sd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
70 if (priv->sd < 0) {
71 printf("Failed to open socket: %d %s\n", errno,
72 strerror(errno));
73 return -errno;
74 }
75 /* Bind to the specified interface */
Joe Hershberger8c7988b2018-07-02 14:47:50 -050076 ret = setsockopt(priv->sd, SOL_SOCKET, SO_BINDTODEVICE,
77 priv->host_ifname, strlen(priv->host_ifname) + 1);
Joe Hershbergera346ca72015-03-22 17:09:21 -050078 if (ret < 0) {
Joe Hershberger8c7988b2018-07-02 14:47:50 -050079 printf("Failed to bind to '%s': %d %s\n", priv->host_ifname,
80 errno, strerror(errno));
Joe Hershbergera346ca72015-03-22 17:09:21 -050081 return -errno;
82 }
83
84 /* Make the socket non-blocking */
85 flags = fcntl(priv->sd, F_GETFL, 0);
86 fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
87
88 /* Enable promiscuous mode to receive responses meant for us */
89 mr.mr_ifindex = device->sll_ifindex;
90 mr.mr_type = PACKET_MR_PROMISC;
91 ret = setsockopt(priv->sd, SOL_PACKET, PACKET_ADD_MEMBERSHIP,
92 &mr, sizeof(mr));
93 if (ret < 0) {
94 struct ifreq ifr;
95
96 printf("Failed to set promiscuous mode: %d %s\n"
97 "Falling back to the old \"flags\" way...\n",
98 errno, strerror(errno));
Joe Hershberger8c7988b2018-07-02 14:47:50 -050099 if (strlen(priv->host_ifname) >= IFNAMSIZ) {
100 printf("Interface name %s is too long.\n",
101 priv->host_ifname);
Tom Riniab971e12015-12-07 22:26:34 -0500102 return -EINVAL;
103 }
Joe Hershberger8c7988b2018-07-02 14:47:50 -0500104 strncpy(ifr.ifr_name, priv->host_ifname, IFNAMSIZ);
Joe Hershbergera346ca72015-03-22 17:09:21 -0500105 if (ioctl(priv->sd, SIOCGIFFLAGS, &ifr) < 0) {
106 printf("Failed to read flags: %d %s\n", errno,
107 strerror(errno));
108 return -errno;
109 }
110 ifr.ifr_flags |= IFF_PROMISC;
111 if (ioctl(priv->sd, SIOCSIFFLAGS, &ifr) < 0) {
112 printf("Failed to write flags: %d %s\n", errno,
113 strerror(errno));
114 return -errno;
115 }
116 }
117 return 0;
118}
119
Joe Hershberger22f68522015-03-22 17:09:23 -0500120static int _local_inet_start(struct eth_sandbox_raw_priv *priv)
121{
122 struct sockaddr_in *device;
123 int ret;
124 int flags;
125 int one = 1;
126
127 /* Prepare device struct */
Joe Hershberger50ed0ef2018-07-02 14:47:47 -0500128 priv->local_bind_sd = -1;
129 priv->local_bind_udp_port = 0;
Joe Hershberger22f68522015-03-22 17:09:23 -0500130 priv->device = malloc(sizeof(struct sockaddr_in));
131 if (priv->device == NULL)
132 return -ENOMEM;
133 device = priv->device;
134 memset(device, 0, sizeof(struct sockaddr_in));
135 device->sin_family = AF_INET;
136 device->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
137
138 /**
139 * Open socket
140 * Since we specify UDP here, any incoming ICMP packets will
141 * not be received, so things like ping will not work on this
142 * localhost interface.
143 */
144 priv->sd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
145 if (priv->sd < 0) {
146 printf("Failed to open socket: %d %s\n", errno,
147 strerror(errno));
148 return -errno;
149 }
150
151 /* Make the socket non-blocking */
152 flags = fcntl(priv->sd, F_GETFL, 0);
153 fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
154
155 /* Include the UDP/IP headers on send and receive */
156 ret = setsockopt(priv->sd, IPPROTO_IP, IP_HDRINCL, &one,
157 sizeof(one));
158 if (ret < 0) {
159 printf("Failed to set header include option: %d %s\n", errno,
160 strerror(errno));
161 return -errno;
162 }
Joe Hershberger22f68522015-03-22 17:09:23 -0500163 return 0;
164}
165
Joe Hershberger8c7988b2018-07-02 14:47:50 -0500166int sandbox_eth_raw_os_start(struct eth_sandbox_raw_priv *priv,
167 unsigned char *ethmac)
Joe Hershberger22f68522015-03-22 17:09:23 -0500168{
169 if (priv->local)
170 return _local_inet_start(priv);
171 else
Joe Hershberger8c7988b2018-07-02 14:47:50 -0500172 return _raw_packet_start(priv, ethmac);
Joe Hershberger22f68522015-03-22 17:09:23 -0500173}
174
Joe Hershbergera346ca72015-03-22 17:09:21 -0500175int sandbox_eth_raw_os_send(void *packet, int length,
Joe Hershberger22f68522015-03-22 17:09:23 -0500176 struct eth_sandbox_raw_priv *priv)
Joe Hershbergera346ca72015-03-22 17:09:21 -0500177{
178 int retval;
Joe Hershberger22f68522015-03-22 17:09:23 -0500179 struct udphdr *udph = packet + sizeof(struct iphdr);
Joe Hershbergera346ca72015-03-22 17:09:21 -0500180
Joe Hershberger97367df2018-07-02 14:47:44 -0500181 if (priv->sd < 0 || !priv->device)
Joe Hershbergera346ca72015-03-22 17:09:21 -0500182 return -EINVAL;
183
Joe Hershberger22f68522015-03-22 17:09:23 -0500184 /*
185 * This block of code came about when testing tftp on the localhost
186 * interface. When using the RAW AF_INET API, the network stack is still
187 * in play responding to incoming traffic based on open "ports". Since
188 * it is raw (at the IP layer, no Ethernet) the network stack tells the
189 * TFTP server that the port it responded to is closed. This causes the
190 * TFTP transfer to be aborted. This block of code inspects the outgoing
191 * packet as formulated by the u-boot network stack to determine the
192 * source port (that the TFTP server will send packets back to) and
193 * opens a typical UDP socket on that port, thus preventing the network
194 * stack from sending that ICMP message claiming that the port has no
195 * bound socket.
196 */
197 if (priv->local && (priv->local_bind_sd == -1 ||
198 priv->local_bind_udp_port != udph->source)) {
199 struct iphdr *iph = packet;
200 struct sockaddr_in addr;
201
202 if (priv->local_bind_sd != -1)
203 close(priv->local_bind_sd);
204
205 /* A normal UDP socket is required to bind */
206 priv->local_bind_sd = socket(AF_INET, SOCK_DGRAM, 0);
207 if (priv->local_bind_sd < 0) {
208 printf("Failed to open bind sd: %d %s\n", errno,
209 strerror(errno));
210 return -errno;
211 }
212 priv->local_bind_udp_port = udph->source;
213
214 /**
215 * Bind the UDP port that we intend to use as our source port
216 * so that the kernel will not send an ICMP port unreachable
217 * message to the server
218 */
219 addr.sin_family = AF_INET;
220 addr.sin_port = udph->source;
221 addr.sin_addr.s_addr = iph->saddr;
Tom Rini71229092015-11-28 08:04:40 -0500222 retval = bind(priv->local_bind_sd, (struct sockaddr *)&addr,
223 sizeof(addr));
Joe Hershberger22f68522015-03-22 17:09:23 -0500224 if (retval < 0)
225 printf("Failed to bind: %d %s\n", errno,
226 strerror(errno));
227 }
228
Joe Hershbergera346ca72015-03-22 17:09:21 -0500229 retval = sendto(priv->sd, packet, length, 0,
230 (struct sockaddr *)priv->device,
231 sizeof(struct sockaddr_ll));
232 if (retval < 0) {
233 printf("Failed to send packet: %d %s\n", errno,
234 strerror(errno));
235 return -errno;
236 }
237 return retval;
238}
239
240int sandbox_eth_raw_os_recv(void *packet, int *length,
241 const struct eth_sandbox_raw_priv *priv)
242{
243 int retval;
244 int saddr_size;
245
Joe Hershberger97367df2018-07-02 14:47:44 -0500246 if (priv->sd < 0 || !priv->device)
Joe Hershbergera346ca72015-03-22 17:09:21 -0500247 return -EINVAL;
248 saddr_size = sizeof(struct sockaddr);
249 retval = recvfrom(priv->sd, packet, 1536, 0,
250 (struct sockaddr *)priv->device,
251 (socklen_t *)&saddr_size);
252 *length = 0;
253 if (retval >= 0) {
254 *length = retval;
255 return 0;
256 }
257 /* The socket is non-blocking, so expect EAGAIN when there is no data */
258 if (errno == EAGAIN)
259 return 0;
260 return -errno;
261}
262
263void sandbox_eth_raw_os_stop(struct eth_sandbox_raw_priv *priv)
264{
265 free(priv->device);
266 priv->device = NULL;
267 close(priv->sd);
268 priv->sd = -1;
Joe Hershberger22f68522015-03-22 17:09:23 -0500269 if (priv->local) {
270 if (priv->local_bind_sd != -1)
271 close(priv->local_bind_sd);
272 priv->local_bind_sd = -1;
273 priv->local_bind_udp_port = 0;
274 }
Joe Hershbergera346ca72015-03-22 17:09:21 -0500275}