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