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