Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 1 | /****************************************************************************** |
Jeremy Kerr | f2101cf | 2016-03-03 14:49:52 +0800 | [diff] [blame] | 2 | * Copyright 2016 Foxconn |
| 3 | * Copyright 2016 IBM Corporation |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | ******************************************************************************/ |
Jeremy Kerr | 982b7bc | 2016-02-25 10:54:39 +0800 | [diff] [blame] | 17 | |
Jeremy Kerr | 52e2478 | 2016-03-03 14:51:35 +0800 | [diff] [blame] | 18 | #include <err.h> |
| 19 | #include <errno.h> |
Jeremy Kerr | 982b7bc | 2016-02-25 10:54:39 +0800 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
Jeremy Kerr | 982b7bc | 2016-02-25 10:54:39 +0800 | [diff] [blame] | 23 | #include <time.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include <sys/ioctl.h> |
| 27 | #include <sys/socket.h> |
| 28 | |
Jeremy Kerr | 760d6ac | 2016-02-25 14:45:33 +0800 | [diff] [blame] | 29 | #include <arpa/inet.h> |
Jeremy Kerr | 52e2478 | 2016-03-03 14:51:35 +0800 | [diff] [blame] | 30 | #include <netinet/in.h> |
Jeremy Kerr | 982b7bc | 2016-02-25 10:54:39 +0800 | [diff] [blame] | 31 | |
Jeremy Kerr | 982b7bc | 2016-02-25 10:54:39 +0800 | [diff] [blame] | 32 | #include <linux/if_arp.h> |
Jeremy Kerr | 52e2478 | 2016-03-03 14:51:35 +0800 | [diff] [blame] | 33 | #include <linux/if_ether.h> |
| 34 | #include <linux/if_packet.h> |
Jeremy Kerr | 982b7bc | 2016-02-25 10:54:39 +0800 | [diff] [blame] | 35 | |
Jeremy Kerr | 71f385b | 2016-03-03 15:18:39 +0800 | [diff] [blame] | 36 | struct eth_addr { |
| 37 | uint8_t eth_addr[ETH_ALEN]; |
| 38 | } __attribute__((packed)); |
| 39 | |
Jeremy Kerr | 760d6ac | 2016-02-25 14:45:33 +0800 | [diff] [blame] | 40 | struct arp_packet { |
Jeremy Kerr | f2101cf | 2016-03-03 14:49:52 +0800 | [diff] [blame] | 41 | struct ethhdr eh; |
| 42 | struct arphdr arp; |
Jeremy Kerr | 71f385b | 2016-03-03 15:18:39 +0800 | [diff] [blame] | 43 | struct eth_addr src_mac; |
Jeremy Kerr | 760d6ac | 2016-02-25 14:45:33 +0800 | [diff] [blame] | 44 | struct in_addr src_ip; |
Jeremy Kerr | 71f385b | 2016-03-03 15:18:39 +0800 | [diff] [blame] | 45 | struct eth_addr dest_mac; |
Jeremy Kerr | 760d6ac | 2016-02-25 14:45:33 +0800 | [diff] [blame] | 46 | struct in_addr dest_ip; |
| 47 | } __attribute__((packed)); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 48 | |
Jeremy Kerr | e09d30d | 2016-03-04 11:42:03 +0800 | [diff] [blame^] | 49 | struct inarp_ctx { |
| 50 | int socket; |
| 51 | const char *ifname; |
| 52 | int ifindex; |
| 53 | struct eth_addr local_mac; |
| 54 | }; |
| 55 | |
Jeremy Kerr | 7b3bcf7 | 2016-02-25 14:29:55 +0800 | [diff] [blame] | 56 | static int send_arp_packet(int fd, |
Jeremy Kerr | f2101cf | 2016-03-03 14:49:52 +0800 | [diff] [blame] | 57 | int ifindex, |
Jeremy Kerr | 71f385b | 2016-03-03 15:18:39 +0800 | [diff] [blame] | 58 | const struct eth_addr *src_mac, |
Jeremy Kerr | 88384bc | 2016-03-03 14:52:50 +0800 | [diff] [blame] | 59 | const struct in_addr *src_ip, |
Jeremy Kerr | 71f385b | 2016-03-03 15:18:39 +0800 | [diff] [blame] | 60 | const struct eth_addr *dest_mac, |
Jeremy Kerr | 88384bc | 2016-03-03 14:52:50 +0800 | [diff] [blame] | 61 | const struct in_addr *dest_ip) |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 62 | { |
Jeremy Kerr | 89dd514 | 2016-02-25 19:40:02 +0800 | [diff] [blame] | 63 | struct sockaddr_ll addr; |
Jeremy Kerr | 5bb869d | 2016-02-25 18:47:08 +0800 | [diff] [blame] | 64 | struct arp_packet arp; |
Jeremy Kerr | d786533 | 2016-02-25 14:52:50 +0800 | [diff] [blame] | 65 | int rc; |
Jeremy Kerr | c10bedb | 2016-02-25 14:26:38 +0800 | [diff] [blame] | 66 | |
Jeremy Kerr | 5bb869d | 2016-02-25 18:47:08 +0800 | [diff] [blame] | 67 | memset(&arp, 0, sizeof(arp)); |
| 68 | |
Jeremy Kerr | c10bedb | 2016-02-25 14:26:38 +0800 | [diff] [blame] | 69 | /* Prepare our link-layer address: raw packet interface, |
| 70 | * using the ifindex interface, receiving ARP packets |
| 71 | */ |
Jeremy Kerr | 89dd514 | 2016-02-25 19:40:02 +0800 | [diff] [blame] | 72 | addr.sll_family = PF_PACKET; |
| 73 | addr.sll_protocol = htons(ETH_P_ARP); |
| 74 | addr.sll_ifindex = ifindex; |
| 75 | addr.sll_hatype = ARPHRD_ETHER; |
| 76 | addr.sll_pkttype = PACKET_OTHERHOST; |
| 77 | addr.sll_halen = ETH_ALEN; |
| 78 | memcpy(addr.sll_addr, dest_mac, ETH_ALEN); |
Jeremy Kerr | c10bedb | 2016-02-25 14:26:38 +0800 | [diff] [blame] | 79 | |
| 80 | /* set the frame header */ |
Jeremy Kerr | 103a958 | 2016-02-25 19:38:52 +0800 | [diff] [blame] | 81 | memcpy(arp.eh.h_dest, dest_mac, ETH_ALEN); |
| 82 | memcpy(arp.eh.h_source, src_mac, ETH_ALEN); |
Jeremy Kerr | 5bb869d | 2016-02-25 18:47:08 +0800 | [diff] [blame] | 83 | arp.eh.h_proto = htons(ETH_P_ARP); |
Jeremy Kerr | c10bedb | 2016-02-25 14:26:38 +0800 | [diff] [blame] | 84 | |
| 85 | /* Fill InARP request data for ethernet + ipv4 */ |
Jeremy Kerr | 5bb869d | 2016-02-25 18:47:08 +0800 | [diff] [blame] | 86 | arp.arp.ar_hrd = htons(ARPHRD_ETHER); |
| 87 | arp.arp.ar_pro = htons(ETH_P_ARP); |
| 88 | arp.arp.ar_hln = ETH_ALEN; |
| 89 | arp.arp.ar_pln = 4; |
| 90 | arp.arp.ar_op = htons(ARPOP_InREPLY); |
Jeremy Kerr | c10bedb | 2016-02-25 14:26:38 +0800 | [diff] [blame] | 91 | |
| 92 | /* fill arp ethernet mac & ipv4 info */ |
Jeremy Kerr | 5bb869d | 2016-02-25 18:47:08 +0800 | [diff] [blame] | 93 | memcpy(&arp.src_mac, src_mac, sizeof(arp.src_mac)); |
| 94 | memcpy(&arp.src_ip, src_ip, sizeof(arp.src_ip)); |
| 95 | memcpy(&arp.dest_mac, dest_mac, sizeof(arp.dest_mac)); |
| 96 | memcpy(&arp.dest_ip, dest_ip, sizeof(arp.dest_ip)); |
Jeremy Kerr | c10bedb | 2016-02-25 14:26:38 +0800 | [diff] [blame] | 97 | |
| 98 | /* send the packet */ |
Jeremy Kerr | 5bb869d | 2016-02-25 18:47:08 +0800 | [diff] [blame] | 99 | rc = sendto(fd, &arp, sizeof(arp), 0, |
Jeremy Kerr | 89dd514 | 2016-02-25 19:40:02 +0800 | [diff] [blame] | 100 | (struct sockaddr *)&addr, sizeof(addr)); |
Jeremy Kerr | d786533 | 2016-02-25 14:52:50 +0800 | [diff] [blame] | 101 | if (rc < 0) |
| 102 | warn("failure sending ARP response"); |
| 103 | |
| 104 | return rc; |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 105 | } |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 106 | |
Jeremy Kerr | 71f385b | 2016-03-03 15:18:39 +0800 | [diff] [blame] | 107 | static const char *eth_mac_to_str(const struct eth_addr *mac_addr) |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 108 | { |
Jeremy Kerr | df04613 | 2016-03-03 15:03:22 +0800 | [diff] [blame] | 109 | static char mac_str[ETH_ALEN * (sizeof("00:") - 1)]; |
Jeremy Kerr | 71f385b | 2016-03-03 15:18:39 +0800 | [diff] [blame] | 110 | const uint8_t *addr = mac_addr->eth_addr; |
Jeremy Kerr | df04613 | 2016-03-03 15:03:22 +0800 | [diff] [blame] | 111 | |
| 112 | snprintf(mac_str, sizeof(mac_str), |
| 113 | "%02x:%02x:%02x:%02x:%02x:%02x", |
Jeremy Kerr | 71f385b | 2016-03-03 15:18:39 +0800 | [diff] [blame] | 114 | addr[0], addr[1], addr[2], |
| 115 | addr[3], addr[4], addr[5]); |
Jeremy Kerr | df04613 | 2016-03-03 15:03:22 +0800 | [diff] [blame] | 116 | |
| 117 | return mac_str; |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 118 | } |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 119 | |
Jeremy Kerr | f3b373f | 2016-02-25 19:24:59 +0800 | [diff] [blame] | 120 | static int do_ifreq(int fd, unsigned long type, |
| 121 | const char *ifname, struct ifreq *ifreq) |
| 122 | { |
| 123 | memset(ifreq, 0, sizeof(*ifreq)); |
Jeremy Kerr | 7275d5c | 2016-03-03 15:30:10 +0800 | [diff] [blame] | 124 | strncpy(ifreq->ifr_name, ifname, sizeof(ifreq->ifr_name)); |
Jeremy Kerr | f3b373f | 2016-02-25 19:24:59 +0800 | [diff] [blame] | 125 | |
| 126 | return ioctl(fd, type, ifreq); |
| 127 | } |
| 128 | |
Jeremy Kerr | e54e483 | 2016-02-25 17:23:10 +0800 | [diff] [blame] | 129 | static int get_local_ipaddr(int fd, const char *ifname, struct in_addr *addr) |
| 130 | { |
| 131 | struct sockaddr_in *sa; |
| 132 | struct ifreq ifreq; |
| 133 | int rc; |
| 134 | |
Jeremy Kerr | f3b373f | 2016-02-25 19:24:59 +0800 | [diff] [blame] | 135 | rc = do_ifreq(fd, SIOCGIFADDR, ifname, &ifreq); |
Jeremy Kerr | e54e483 | 2016-02-25 17:23:10 +0800 | [diff] [blame] | 136 | if (rc) { |
Jeremy Kerr | 1b127c5 | 2016-02-25 19:29:02 +0800 | [diff] [blame] | 137 | warn("Error querying local IP address for %s", ifname); |
Jeremy Kerr | e54e483 | 2016-02-25 17:23:10 +0800 | [diff] [blame] | 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | if (ifreq.ifr_addr.sa_family != AF_INET) { |
| 142 | warnx("Unknown address family %d in address response", |
Jeremy Kerr | f2101cf | 2016-03-03 14:49:52 +0800 | [diff] [blame] | 143 | ifreq.ifr_addr.sa_family); |
Jeremy Kerr | e54e483 | 2016-02-25 17:23:10 +0800 | [diff] [blame] | 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | sa = (struct sockaddr_in *)&ifreq.ifr_addr; |
| 148 | memcpy(addr, &sa->sin_addr, sizeof(*addr)); |
| 149 | return 0; |
| 150 | } |
| 151 | |
Jeremy Kerr | 71f385b | 2016-03-03 15:18:39 +0800 | [diff] [blame] | 152 | static int get_local_hwaddr(int fd, const char *ifname, struct eth_addr *addr) |
Jeremy Kerr | 86489a1 | 2016-02-25 19:14:09 +0800 | [diff] [blame] | 153 | { |
| 154 | struct ifreq ifreq; |
| 155 | int rc; |
| 156 | |
Jeremy Kerr | f3b373f | 2016-02-25 19:24:59 +0800 | [diff] [blame] | 157 | rc = do_ifreq(fd, SIOCGIFHWADDR, ifname, &ifreq); |
Jeremy Kerr | 86489a1 | 2016-02-25 19:14:09 +0800 | [diff] [blame] | 158 | if (rc) { |
Jeremy Kerr | 1b127c5 | 2016-02-25 19:29:02 +0800 | [diff] [blame] | 159 | warn("Error querying local MAC address for %s", ifname); |
Jeremy Kerr | 86489a1 | 2016-02-25 19:14:09 +0800 | [diff] [blame] | 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | memcpy(addr, ifreq.ifr_hwaddr.sa_data, ETH_ALEN); |
| 164 | return 0; |
| 165 | } |
| 166 | |
Jeremy Kerr | d834378 | 2016-02-25 19:17:19 +0800 | [diff] [blame] | 167 | static int get_ifindex(int fd, const char *ifname, int *ifindex) |
| 168 | { |
| 169 | struct ifreq ifreq; |
| 170 | int rc; |
| 171 | |
Jeremy Kerr | f3b373f | 2016-02-25 19:24:59 +0800 | [diff] [blame] | 172 | rc = do_ifreq(fd, SIOCGIFINDEX, ifname, &ifreq); |
Jeremy Kerr | d834378 | 2016-02-25 19:17:19 +0800 | [diff] [blame] | 173 | if (rc < 0) { |
| 174 | warn("Error querying interface %s", ifname); |
| 175 | return -1; |
| 176 | } |
| 177 | |
| 178 | *ifindex = ifreq.ifr_ifindex; |
| 179 | return 0; |
| 180 | } |
| 181 | |
Jeremy Kerr | 115522d | 2016-02-25 10:24:14 +0800 | [diff] [blame] | 182 | static void usage(const char *progname) |
| 183 | { |
| 184 | fprintf(stderr, "Usage: %s <interface>\n", progname); |
| 185 | } |
| 186 | |
| 187 | int main(int argc, char **argv) |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 188 | { |
Jeremy Kerr | 3f8a28e | 2016-02-25 18:56:01 +0800 | [diff] [blame] | 189 | struct arp_packet inarp_req; |
Jeremy Kerr | e09d30d | 2016-03-04 11:42:03 +0800 | [diff] [blame^] | 190 | struct in_addr local_ip; |
| 191 | struct inarp_ctx inarp; |
Jeremy Kerr | 3f8a28e | 2016-02-25 18:56:01 +0800 | [diff] [blame] | 192 | ssize_t len; |
Jeremy Kerr | e09d30d | 2016-03-04 11:42:03 +0800 | [diff] [blame^] | 193 | int ret; |
Jeremy Kerr | 115522d | 2016-02-25 10:24:14 +0800 | [diff] [blame] | 194 | |
| 195 | if (argc < 2) { |
| 196 | usage(argv[0]); |
| 197 | return EXIT_FAILURE; |
| 198 | } |
| 199 | |
Jeremy Kerr | e09d30d | 2016-03-04 11:42:03 +0800 | [diff] [blame^] | 200 | memset(&inarp, 0, sizeof(inarp)); |
| 201 | inarp.ifname = argv[1]; |
Jeremy Kerr | 115522d | 2016-02-25 10:24:14 +0800 | [diff] [blame] | 202 | |
Jeremy Kerr | e09d30d | 2016-03-04 11:42:03 +0800 | [diff] [blame^] | 203 | if (strlen(inarp.ifname) > IFNAMSIZ) |
| 204 | errx(EXIT_FAILURE, "Interface name '%s' is invalid", |
| 205 | inarp.ifname); |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 206 | |
Jeremy Kerr | e09d30d | 2016-03-04 11:42:03 +0800 | [diff] [blame^] | 207 | inarp.socket = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP)); |
| 208 | if (inarp.socket < 0) |
Jeremy Kerr | d786533 | 2016-02-25 14:52:50 +0800 | [diff] [blame] | 209 | err(EXIT_FAILURE, "Error opening ARP socket"); |
Jeremy Kerr | c10bedb | 2016-02-25 14:26:38 +0800 | [diff] [blame] | 210 | |
Jeremy Kerr | e09d30d | 2016-03-04 11:42:03 +0800 | [diff] [blame^] | 211 | ret = get_ifindex(inarp.socket, inarp.ifname, &inarp.ifindex); |
Jeremy Kerr | 86489a1 | 2016-02-25 19:14:09 +0800 | [diff] [blame] | 212 | if (ret) |
| 213 | exit(EXIT_FAILURE); |
Jeremy Kerr | d786533 | 2016-02-25 14:52:50 +0800 | [diff] [blame] | 214 | |
Jeremy Kerr | e09d30d | 2016-03-04 11:42:03 +0800 | [diff] [blame^] | 215 | ret = get_local_hwaddr(inarp.socket, inarp.ifname, &inarp.local_mac); |
Jeremy Kerr | d834378 | 2016-02-25 19:17:19 +0800 | [diff] [blame] | 216 | if (ret) |
| 217 | exit(EXIT_FAILURE); |
| 218 | |
Jeremy Kerr | e09d30d | 2016-03-04 11:42:03 +0800 | [diff] [blame^] | 219 | printf("%s MAC address: %s\n", inarp.ifname, |
| 220 | eth_mac_to_str(&inarp.local_mac)); |
Jeremy Kerr | c10bedb | 2016-02-25 14:26:38 +0800 | [diff] [blame] | 221 | |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 222 | while (1) { |
Jeremy Kerr | e09d30d | 2016-03-04 11:42:03 +0800 | [diff] [blame^] | 223 | len = recvfrom(inarp.socket, &inarp_req, sizeof(inarp_req), 0, |
Jeremy Kerr | 3f8a28e | 2016-02-25 18:56:01 +0800 | [diff] [blame] | 224 | NULL, NULL); |
| 225 | if (len <= 0) { |
Jeremy Kerr | 77cafea | 2016-02-25 17:15:28 +0800 | [diff] [blame] | 226 | if (errno == EINTR) |
| 227 | continue; |
| 228 | err(EXIT_FAILURE, "Error recieving ARP packet"); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 229 | } |
Jeremy Kerr | 77cafea | 2016-02-25 17:15:28 +0800 | [diff] [blame] | 230 | |
Jeremy Kerr | 3f8a28e | 2016-02-25 18:56:01 +0800 | [diff] [blame] | 231 | /* Is this packet large enough for an inarp? */ |
| 232 | if ((size_t)len < sizeof(inarp_req)) |
| 233 | continue; |
| 234 | |
| 235 | /* ... is it an inarp request? */ |
| 236 | if (ntohs(inarp_req.arp.ar_op) != ARPOP_InREQUEST) |
Jeremy Kerr | 77cafea | 2016-02-25 17:15:28 +0800 | [diff] [blame] | 237 | continue; |
| 238 | |
| 239 | /* ... for us? */ |
Jeremy Kerr | e09d30d | 2016-03-04 11:42:03 +0800 | [diff] [blame^] | 240 | if (memcmp(&inarp.local_mac, inarp_req.eh.h_dest, ETH_ALEN)) |
Jeremy Kerr | 77cafea | 2016-02-25 17:15:28 +0800 | [diff] [blame] | 241 | continue; |
| 242 | |
Jeremy Kerr | 71f385b | 2016-03-03 15:18:39 +0800 | [diff] [blame] | 243 | printf("src mac: %s\n", eth_mac_to_str(&inarp_req.src_mac)); |
Jeremy Kerr | 492de5d | 2016-02-25 19:35:44 +0800 | [diff] [blame] | 244 | printf("src ip: %s\n", inet_ntoa(inarp_req.src_ip)); |
Jeremy Kerr | 77cafea | 2016-02-25 17:15:28 +0800 | [diff] [blame] | 245 | |
Jeremy Kerr | e09d30d | 2016-03-04 11:42:03 +0800 | [diff] [blame^] | 246 | ret = get_local_ipaddr(inarp.socket, inarp.ifname, &local_ip); |
Jeremy Kerr | e54e483 | 2016-02-25 17:23:10 +0800 | [diff] [blame] | 247 | /* if we don't have a local IP address to send, just drop the |
| 248 | * request */ |
| 249 | if (ret) |
| 250 | continue; |
| 251 | |
Jeremy Kerr | 492de5d | 2016-02-25 19:35:44 +0800 | [diff] [blame] | 252 | printf("local ip: %s\n", inet_ntoa(local_ip)); |
| 253 | |
Jeremy Kerr | e09d30d | 2016-03-04 11:42:03 +0800 | [diff] [blame^] | 254 | send_arp_packet(inarp.socket, inarp.ifindex, |
Jeremy Kerr | 71f385b | 2016-03-03 15:18:39 +0800 | [diff] [blame] | 255 | &inarp_req.dest_mac, |
Jeremy Kerr | f2101cf | 2016-03-03 14:49:52 +0800 | [diff] [blame] | 256 | &local_ip, |
Jeremy Kerr | 71f385b | 2016-03-03 15:18:39 +0800 | [diff] [blame] | 257 | &inarp_req.src_mac, |
Jeremy Kerr | f2101cf | 2016-03-03 14:49:52 +0800 | [diff] [blame] | 258 | &inarp_req.src_ip); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 259 | } |
Jeremy Kerr | e09d30d | 2016-03-04 11:42:03 +0800 | [diff] [blame^] | 260 | close(inarp.socket); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 261 | return 0; |
| 262 | } |