Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 National Instruments |
| 4 | * |
| 5 | * (C) Copyright 2015 |
| 6 | * Joe Hershberger <joe.hershberger@ni.com> |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 7 | */ |
| 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 Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 14 | #include <netinet/ip.h> |
| 15 | #include <netinet/udp.h> |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 16 | #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 Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 24 | #include <arpa/inet.h> |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 25 | #include <linux/if_ether.h> |
| 26 | #include <linux/if_packet.h> |
| 27 | |
Joe Hershberger | e569140 | 2018-07-02 14:47:50 -0500 | [diff] [blame^] | 28 | static int _raw_packet_start(struct eth_sandbox_raw_priv *priv, |
| 29 | unsigned char *ethmac) |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 30 | { |
| 31 | struct sockaddr_ll *device; |
| 32 | struct packet_mreq mr; |
| 33 | int ret; |
| 34 | int flags; |
| 35 | |
| 36 | /* Prepare device struct */ |
Joe Hershberger | f9d4168 | 2018-07-02 14:47:47 -0500 | [diff] [blame] | 37 | priv->local_bind_sd = -1; |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 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)); |
Joe Hershberger | e569140 | 2018-07-02 14:47:50 -0500 | [diff] [blame^] | 43 | device->sll_ifindex = if_nametoindex(priv->host_ifname); |
| 44 | priv->host_ifindex = device->sll_ifindex; |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 45 | 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 Hershberger | e569140 | 2018-07-02 14:47:50 -0500 | [diff] [blame^] | 57 | ret = setsockopt(priv->sd, SOL_SOCKET, SO_BINDTODEVICE, |
| 58 | priv->host_ifname, strlen(priv->host_ifname) + 1); |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 59 | if (ret < 0) { |
Joe Hershberger | e569140 | 2018-07-02 14:47:50 -0500 | [diff] [blame^] | 60 | printf("Failed to bind to '%s': %d %s\n", priv->host_ifname, |
| 61 | errno, strerror(errno)); |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 62 | 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 Hershberger | e569140 | 2018-07-02 14:47:50 -0500 | [diff] [blame^] | 80 | if (strlen(priv->host_ifname) >= IFNAMSIZ) { |
| 81 | printf("Interface name %s is too long.\n", |
| 82 | priv->host_ifname); |
Tom Rini | a5d3a4e | 2015-12-07 22:26:34 -0500 | [diff] [blame] | 83 | return -EINVAL; |
| 84 | } |
Joe Hershberger | e569140 | 2018-07-02 14:47:50 -0500 | [diff] [blame^] | 85 | strncpy(ifr.ifr_name, priv->host_ifname, IFNAMSIZ); |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 86 | 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 Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static 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 Hershberger | f9d4168 | 2018-07-02 14:47:47 -0500 | [diff] [blame] | 109 | priv->local_bind_sd = -1; |
| 110 | priv->local_bind_udp_port = 0; |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 111 | 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 Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | |
Joe Hershberger | e569140 | 2018-07-02 14:47:50 -0500 | [diff] [blame^] | 147 | int sandbox_eth_raw_os_start(struct eth_sandbox_raw_priv *priv, |
| 148 | unsigned char *ethmac) |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 149 | { |
| 150 | if (priv->local) |
| 151 | return _local_inet_start(priv); |
| 152 | else |
Joe Hershberger | e569140 | 2018-07-02 14:47:50 -0500 | [diff] [blame^] | 153 | return _raw_packet_start(priv, ethmac); |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 154 | } |
| 155 | |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 156 | int sandbox_eth_raw_os_send(void *packet, int length, |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 157 | struct eth_sandbox_raw_priv *priv) |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 158 | { |
| 159 | int retval; |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 160 | struct udphdr *udph = packet + sizeof(struct iphdr); |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 161 | |
Joe Hershberger | 0edb525 | 2018-07-02 14:47:44 -0500 | [diff] [blame] | 162 | if (priv->sd < 0 || !priv->device) |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 163 | return -EINVAL; |
| 164 | |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 165 | /* |
| 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 Rini | 8e2334e | 2015-11-28 08:04:40 -0500 | [diff] [blame] | 203 | retval = bind(priv->local_bind_sd, (struct sockaddr *)&addr, |
| 204 | sizeof(addr)); |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 205 | if (retval < 0) |
| 206 | printf("Failed to bind: %d %s\n", errno, |
| 207 | strerror(errno)); |
| 208 | } |
| 209 | |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 210 | 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 | |
| 221 | int 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 Hershberger | 0edb525 | 2018-07-02 14:47:44 -0500 | [diff] [blame] | 227 | if (priv->sd < 0 || !priv->device) |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 228 | 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 | |
| 244 | void 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 Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 250 | 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 Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 256 | } |