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