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