blob: 82bf66688632781864addaa2b25cff95efc99aee [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/*
3 * Copyright (c) 2015 National Instruments
4 *
5 * (C) Copyright 2015
6 * Joe Hershberger <joe.hershberger@ni.com>
Joe Hershberger586cbd12015-03-22 17:09:21 -05007 */
8
9#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>
Joe Hershberger586cbd12015-03-22 17:09:21 -050016#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <sys/types.h>
20#include <sys/ioctl.h>
21#include <sys/socket.h>
22#include <unistd.h>
23
Joe Hershbergera8921922015-03-22 17:09:23 -050024#include <arpa/inet.h>
Joe Hershberger586cbd12015-03-22 17:09:21 -050025#include <linux/if_ether.h>
26#include <linux/if_packet.h>
27
Joe Hershbergere5691402018-07-02 14:47:50 -050028static int _raw_packet_start(struct eth_sandbox_raw_priv *priv,
29 unsigned char *ethmac)
Joe Hershberger586cbd12015-03-22 17:09:21 -050030{
31 struct sockaddr_ll *device;
32 struct packet_mreq mr;
33 int ret;
34 int flags;
35
36 /* Prepare device struct */
Joe Hershbergerf9d41682018-07-02 14:47:47 -050037 priv->local_bind_sd = -1;
Joe Hershberger586cbd12015-03-22 17:09:21 -050038 priv->device = malloc(sizeof(struct sockaddr_ll));
39 if (priv->device == NULL)
40 return -ENOMEM;
41 device = priv->device;
42 memset(device, 0, sizeof(struct sockaddr_ll));
Joe Hershbergere5691402018-07-02 14:47:50 -050043 device->sll_ifindex = if_nametoindex(priv->host_ifname);
44 priv->host_ifindex = device->sll_ifindex;
Joe Hershberger586cbd12015-03-22 17:09:21 -050045 device->sll_family = AF_PACKET;
46 memcpy(device->sll_addr, ethmac, 6);
47 device->sll_halen = htons(6);
48
49 /* Open socket */
50 priv->sd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
51 if (priv->sd < 0) {
52 printf("Failed to open socket: %d %s\n", errno,
53 strerror(errno));
54 return -errno;
55 }
56 /* Bind to the specified interface */
Joe Hershbergere5691402018-07-02 14:47:50 -050057 ret = setsockopt(priv->sd, SOL_SOCKET, SO_BINDTODEVICE,
58 priv->host_ifname, strlen(priv->host_ifname) + 1);
Joe Hershberger586cbd12015-03-22 17:09:21 -050059 if (ret < 0) {
Joe Hershbergere5691402018-07-02 14:47:50 -050060 printf("Failed to bind to '%s': %d %s\n", priv->host_ifname,
61 errno, strerror(errno));
Joe Hershberger586cbd12015-03-22 17:09:21 -050062 return -errno;
63 }
64
65 /* Make the socket non-blocking */
66 flags = fcntl(priv->sd, F_GETFL, 0);
67 fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
68
69 /* Enable promiscuous mode to receive responses meant for us */
70 mr.mr_ifindex = device->sll_ifindex;
71 mr.mr_type = PACKET_MR_PROMISC;
72 ret = setsockopt(priv->sd, SOL_PACKET, PACKET_ADD_MEMBERSHIP,
73 &mr, sizeof(mr));
74 if (ret < 0) {
75 struct ifreq ifr;
76
77 printf("Failed to set promiscuous mode: %d %s\n"
78 "Falling back to the old \"flags\" way...\n",
79 errno, strerror(errno));
Joe Hershbergere5691402018-07-02 14:47:50 -050080 if (strlen(priv->host_ifname) >= IFNAMSIZ) {
81 printf("Interface name %s is too long.\n",
82 priv->host_ifname);
Tom Rinia5d3a4e2015-12-07 22:26:34 -050083 return -EINVAL;
84 }
Joe Hershbergere5691402018-07-02 14:47:50 -050085 strncpy(ifr.ifr_name, priv->host_ifname, IFNAMSIZ);
Joe Hershberger586cbd12015-03-22 17:09:21 -050086 if (ioctl(priv->sd, SIOCGIFFLAGS, &ifr) < 0) {
87 printf("Failed to read flags: %d %s\n", errno,
88 strerror(errno));
89 return -errno;
90 }
91 ifr.ifr_flags |= IFF_PROMISC;
92 if (ioctl(priv->sd, SIOCSIFFLAGS, &ifr) < 0) {
93 printf("Failed to write flags: %d %s\n", errno,
94 strerror(errno));
95 return -errno;
96 }
97 }
Joe Hershbergera8921922015-03-22 17:09:23 -050098 return 0;
99}
100
101static int _local_inet_start(struct eth_sandbox_raw_priv *priv)
102{
103 struct sockaddr_in *device;
104 int ret;
105 int flags;
106 int one = 1;
107
108 /* Prepare device struct */
Joe Hershbergerf9d41682018-07-02 14:47:47 -0500109 priv->local_bind_sd = -1;
110 priv->local_bind_udp_port = 0;
Joe Hershbergera8921922015-03-22 17:09:23 -0500111 priv->device = malloc(sizeof(struct sockaddr_in));
112 if (priv->device == NULL)
113 return -ENOMEM;
114 device = priv->device;
115 memset(device, 0, sizeof(struct sockaddr_in));
116 device->sin_family = AF_INET;
117 device->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
118
119 /**
120 * Open socket
121 * Since we specify UDP here, any incoming ICMP packets will
122 * not be received, so things like ping will not work on this
123 * localhost interface.
124 */
125 priv->sd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
126 if (priv->sd < 0) {
127 printf("Failed to open socket: %d %s\n", errno,
128 strerror(errno));
129 return -errno;
130 }
131
132 /* Make the socket non-blocking */
133 flags = fcntl(priv->sd, F_GETFL, 0);
134 fcntl(priv->sd, F_SETFL, flags | O_NONBLOCK);
135
136 /* Include the UDP/IP headers on send and receive */
137 ret = setsockopt(priv->sd, IPPROTO_IP, IP_HDRINCL, &one,
138 sizeof(one));
139 if (ret < 0) {
140 printf("Failed to set header include option: %d %s\n", errno,
141 strerror(errno));
142 return -errno;
143 }
Joe Hershberger586cbd12015-03-22 17:09:21 -0500144 return 0;
145}
146
Joe Hershbergere5691402018-07-02 14:47:50 -0500147int sandbox_eth_raw_os_start(struct eth_sandbox_raw_priv *priv,
148 unsigned char *ethmac)
Joe Hershbergera8921922015-03-22 17:09:23 -0500149{
150 if (priv->local)
151 return _local_inet_start(priv);
152 else
Joe Hershbergere5691402018-07-02 14:47:50 -0500153 return _raw_packet_start(priv, ethmac);
Joe Hershbergera8921922015-03-22 17:09:23 -0500154}
155
Joe Hershberger586cbd12015-03-22 17:09:21 -0500156int sandbox_eth_raw_os_send(void *packet, int length,
Joe Hershbergera8921922015-03-22 17:09:23 -0500157 struct eth_sandbox_raw_priv *priv)
Joe Hershberger586cbd12015-03-22 17:09:21 -0500158{
159 int retval;
Joe Hershbergera8921922015-03-22 17:09:23 -0500160 struct udphdr *udph = packet + sizeof(struct iphdr);
Joe Hershberger586cbd12015-03-22 17:09:21 -0500161
Joe Hershberger0edb5252018-07-02 14:47:44 -0500162 if (priv->sd < 0 || !priv->device)
Joe Hershberger586cbd12015-03-22 17:09:21 -0500163 return -EINVAL;
164
Joe Hershbergera8921922015-03-22 17:09:23 -0500165 /*
166 * This block of code came about when testing tftp on the localhost
167 * interface. When using the RAW AF_INET API, the network stack is still
168 * in play responding to incoming traffic based on open "ports". Since
169 * it is raw (at the IP layer, no Ethernet) the network stack tells the
170 * TFTP server that the port it responded to is closed. This causes the
171 * TFTP transfer to be aborted. This block of code inspects the outgoing
172 * packet as formulated by the u-boot network stack to determine the
173 * source port (that the TFTP server will send packets back to) and
174 * opens a typical UDP socket on that port, thus preventing the network
175 * stack from sending that ICMP message claiming that the port has no
176 * bound socket.
177 */
178 if (priv->local && (priv->local_bind_sd == -1 ||
179 priv->local_bind_udp_port != udph->source)) {
180 struct iphdr *iph = packet;
181 struct sockaddr_in addr;
182
183 if (priv->local_bind_sd != -1)
184 close(priv->local_bind_sd);
185
186 /* A normal UDP socket is required to bind */
187 priv->local_bind_sd = socket(AF_INET, SOCK_DGRAM, 0);
188 if (priv->local_bind_sd < 0) {
189 printf("Failed to open bind sd: %d %s\n", errno,
190 strerror(errno));
191 return -errno;
192 }
193 priv->local_bind_udp_port = udph->source;
194
195 /**
196 * Bind the UDP port that we intend to use as our source port
197 * so that the kernel will not send an ICMP port unreachable
198 * message to the server
199 */
200 addr.sin_family = AF_INET;
201 addr.sin_port = udph->source;
202 addr.sin_addr.s_addr = iph->saddr;
Tom Rini8e2334e2015-11-28 08:04:40 -0500203 retval = bind(priv->local_bind_sd, (struct sockaddr *)&addr,
204 sizeof(addr));
Joe Hershbergera8921922015-03-22 17:09:23 -0500205 if (retval < 0)
206 printf("Failed to bind: %d %s\n", errno,
207 strerror(errno));
208 }
209
Joe Hershberger586cbd12015-03-22 17:09:21 -0500210 retval = sendto(priv->sd, packet, length, 0,
211 (struct sockaddr *)priv->device,
212 sizeof(struct sockaddr_ll));
213 if (retval < 0) {
214 printf("Failed to send packet: %d %s\n", errno,
215 strerror(errno));
216 return -errno;
217 }
218 return retval;
219}
220
221int sandbox_eth_raw_os_recv(void *packet, int *length,
222 const struct eth_sandbox_raw_priv *priv)
223{
224 int retval;
225 int saddr_size;
226
Joe Hershberger0edb5252018-07-02 14:47:44 -0500227 if (priv->sd < 0 || !priv->device)
Joe Hershberger586cbd12015-03-22 17:09:21 -0500228 return -EINVAL;
229 saddr_size = sizeof(struct sockaddr);
230 retval = recvfrom(priv->sd, packet, 1536, 0,
231 (struct sockaddr *)priv->device,
232 (socklen_t *)&saddr_size);
233 *length = 0;
234 if (retval >= 0) {
235 *length = retval;
236 return 0;
237 }
238 /* The socket is non-blocking, so expect EAGAIN when there is no data */
239 if (errno == EAGAIN)
240 return 0;
241 return -errno;
242}
243
244void sandbox_eth_raw_os_stop(struct eth_sandbox_raw_priv *priv)
245{
246 free(priv->device);
247 priv->device = NULL;
248 close(priv->sd);
249 priv->sd = -1;
Joe Hershbergera8921922015-03-22 17:09:23 -0500250 if (priv->local) {
251 if (priv->local_bind_sd != -1)
252 close(priv->local_bind_sd);
253 priv->local_bind_sd = -1;
254 priv->local_bind_udp_port = 0;
255 }
Joe Hershberger586cbd12015-03-22 17:09:21 -0500256}