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