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 | 66a2805 | 2016-02-25 14:31:35 +0800 | [diff] [blame] | 35 | #define ETH_ARP_FRAME_LEN ( \ |
| 36 | sizeof(struct ethhdr) + \ |
| 37 | sizeof(struct arphdr) + \ |
Jeremy Kerr | 760d6ac | 2016-02-25 14:45:33 +0800 | [diff] [blame] | 38 | ((ETH_ALEN + sizeof(struct in_addr)) * 2)) |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 39 | |
Jeremy Kerr | 760d6ac | 2016-02-25 14:45:33 +0800 | [diff] [blame] | 40 | struct arp_packet { |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 41 | struct ethhdr eh; |
| 42 | struct arphdr arp; |
Jeremy Kerr | 760d6ac | 2016-02-25 14:45:33 +0800 | [diff] [blame] | 43 | uint8_t src_mac[ETH_ALEN]; |
| 44 | struct in_addr src_ip; |
| 45 | uint8_t dest_mac[ETH_ALEN]; |
| 46 | struct in_addr dest_ip; |
| 47 | } __attribute__((packed)); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 48 | |
Jeremy Kerr | 7b3bcf7 | 2016-02-25 14:29:55 +0800 | [diff] [blame] | 49 | static int send_arp_packet(int fd, |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 50 | int ifindex, |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 51 | unsigned char *src_mac, |
Jeremy Kerr | 760d6ac | 2016-02-25 14:45:33 +0800 | [diff] [blame] | 52 | struct in_addr *src_ip, |
| 53 | unsigned char *dest_mac, |
| 54 | struct in_addr *dest_ip) |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 55 | { |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 56 | struct sockaddr_ll socket_address; |
Jeremy Kerr | 5bb869d | 2016-02-25 18:47:08 +0800 | [diff] [blame^] | 57 | struct arp_packet arp; |
Jeremy Kerr | d786533 | 2016-02-25 14:52:50 +0800 | [diff] [blame] | 58 | int rc; |
Jeremy Kerr | c10bedb | 2016-02-25 14:26:38 +0800 | [diff] [blame] | 59 | |
Jeremy Kerr | 5bb869d | 2016-02-25 18:47:08 +0800 | [diff] [blame^] | 60 | memset(&arp, 0, sizeof(arp)); |
| 61 | |
Jeremy Kerr | c10bedb | 2016-02-25 14:26:38 +0800 | [diff] [blame] | 62 | /* Prepare our link-layer address: raw packet interface, |
| 63 | * using the ifindex interface, receiving ARP packets |
| 64 | */ |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 65 | socket_address.sll_family = PF_PACKET; |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 66 | socket_address.sll_protocol = htons(ETH_P_ARP); |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 67 | socket_address.sll_ifindex = ifindex; |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 68 | socket_address.sll_hatype = ARPHRD_ETHER; |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 69 | socket_address.sll_pkttype = PACKET_OTHERHOST; |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 70 | socket_address.sll_halen = ETH_ALEN; |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 71 | memcpy(socket_address.sll_addr, dest_mac, ETH_ALEN); |
Jeremy Kerr | c10bedb | 2016-02-25 14:26:38 +0800 | [diff] [blame] | 72 | |
| 73 | /* set the frame header */ |
Jeremy Kerr | 5bb869d | 2016-02-25 18:47:08 +0800 | [diff] [blame^] | 74 | memcpy(arp.eh.h_dest, (void *)dest_mac, ETH_ALEN); |
| 75 | memcpy(arp.eh.h_source, (void *)src_mac, ETH_ALEN); |
| 76 | arp.eh.h_proto = htons(ETH_P_ARP); |
Jeremy Kerr | c10bedb | 2016-02-25 14:26:38 +0800 | [diff] [blame] | 77 | |
| 78 | /* Fill InARP request data for ethernet + ipv4 */ |
Jeremy Kerr | 5bb869d | 2016-02-25 18:47:08 +0800 | [diff] [blame^] | 79 | arp.arp.ar_hrd = htons(ARPHRD_ETHER); |
| 80 | arp.arp.ar_pro = htons(ETH_P_ARP); |
| 81 | arp.arp.ar_hln = ETH_ALEN; |
| 82 | arp.arp.ar_pln = 4; |
| 83 | arp.arp.ar_op = htons(ARPOP_InREPLY); |
Jeremy Kerr | c10bedb | 2016-02-25 14:26:38 +0800 | [diff] [blame] | 84 | |
| 85 | /* fill arp ethernet mac & ipv4 info */ |
Jeremy Kerr | 5bb869d | 2016-02-25 18:47:08 +0800 | [diff] [blame^] | 86 | memcpy(&arp.src_mac, src_mac, sizeof(arp.src_mac)); |
| 87 | memcpy(&arp.src_ip, src_ip, sizeof(arp.src_ip)); |
| 88 | memcpy(&arp.dest_mac, dest_mac, sizeof(arp.dest_mac)); |
| 89 | memcpy(&arp.dest_ip, dest_ip, sizeof(arp.dest_ip)); |
Jeremy Kerr | c10bedb | 2016-02-25 14:26:38 +0800 | [diff] [blame] | 90 | |
| 91 | /* send the packet */ |
Jeremy Kerr | 5bb869d | 2016-02-25 18:47:08 +0800 | [diff] [blame^] | 92 | rc = sendto(fd, &arp, sizeof(arp), 0, |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 93 | (struct sockaddr *)&socket_address, |
| 94 | sizeof(socket_address)); |
Jeremy Kerr | d786533 | 2016-02-25 14:52:50 +0800 | [diff] [blame] | 95 | if (rc < 0) |
| 96 | warn("failure sending ARP response"); |
| 97 | |
| 98 | return rc; |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 99 | } |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 100 | |
Jeremy Kerr | 7b3bcf7 | 2016-02-25 14:29:55 +0800 | [diff] [blame] | 101 | static void show_mac_addr(const char *name, unsigned char *mac_addr) |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 102 | { |
| 103 | int i; |
| 104 | printf("%s MAC address: ", name); |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 105 | for (i = 0; i < 6; i++) { |
| 106 | printf("%.2X%c", (unsigned char)mac_addr[i], |
| 107 | (i == 5) ? '\n' : ':'); |
| 108 | } |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 109 | return; |
| 110 | } |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 111 | |
Jeremy Kerr | e54e483 | 2016-02-25 17:23:10 +0800 | [diff] [blame] | 112 | static int get_local_ipaddr(int fd, const char *ifname, struct in_addr *addr) |
| 113 | { |
| 114 | struct sockaddr_in *sa; |
| 115 | struct ifreq ifreq; |
| 116 | int rc; |
| 117 | |
| 118 | memset(&ifreq, 0, sizeof(ifreq)); |
| 119 | strcpy(ifreq.ifr_name, ifname); |
| 120 | |
| 121 | rc = ioctl(fd, SIOCGIFADDR, &ifreq); |
| 122 | if (rc) { |
| 123 | warn("Error querying local address for %s", ifname); |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | if (ifreq.ifr_addr.sa_family != AF_INET) { |
| 128 | warnx("Unknown address family %d in address response", |
| 129 | ifreq.ifr_addr.sa_family); |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | sa = (struct sockaddr_in *)&ifreq.ifr_addr; |
| 134 | memcpy(addr, &sa->sin_addr, sizeof(*addr)); |
| 135 | return 0; |
| 136 | } |
| 137 | |
Jeremy Kerr | 115522d | 2016-02-25 10:24:14 +0800 | [diff] [blame] | 138 | static void usage(const char *progname) |
| 139 | { |
| 140 | fprintf(stderr, "Usage: %s <interface>\n", progname); |
| 141 | } |
| 142 | |
| 143 | int main(int argc, char **argv) |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 144 | { |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 145 | /*buffer for ethernet frame */ |
Jeremy Kerr | c10bedb | 2016-02-25 14:26:38 +0800 | [diff] [blame] | 146 | static unsigned char buffer[ETH_FRAME_LEN]; |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 147 | static struct ifreq ifreq_buffer; |
Jeremy Kerr | 115522d | 2016-02-25 10:24:14 +0800 | [diff] [blame] | 148 | const char *ifname; |
Jeremy Kerr | 760d6ac | 2016-02-25 14:45:33 +0800 | [diff] [blame] | 149 | int fd, ret; |
Jeremy Kerr | 115522d | 2016-02-25 10:24:14 +0800 | [diff] [blame] | 150 | |
| 151 | if (argc < 2) { |
| 152 | usage(argv[0]); |
| 153 | return EXIT_FAILURE; |
| 154 | } |
| 155 | |
| 156 | ifname = argv[1]; |
| 157 | |
Jeremy Kerr | d786533 | 2016-02-25 14:52:50 +0800 | [diff] [blame] | 158 | if (strlen(ifname) > IFNAMSIZ) |
| 159 | errx(EXIT_FAILURE, "Interface name '%s' is invalid", ifname); |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 160 | |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 161 | static unsigned char src_mac[6]; |
Jeremy Kerr | e54e483 | 2016-02-25 17:23:10 +0800 | [diff] [blame] | 162 | static struct in_addr local_ip; |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 163 | int ifindex; |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 164 | |
Jeremy Kerr | c304ce7 | 2016-02-25 18:37:14 +0800 | [diff] [blame] | 165 | fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP)); |
Jeremy Kerr | d786533 | 2016-02-25 14:52:50 +0800 | [diff] [blame] | 166 | if (fd < 0) |
| 167 | err(EXIT_FAILURE, "Error opening ARP socket"); |
Jeremy Kerr | c10bedb | 2016-02-25 14:26:38 +0800 | [diff] [blame] | 168 | |
| 169 | /* Query local mac address */ |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 170 | memset(&ifreq_buffer, 0x00, sizeof(ifreq_buffer)); |
Jeremy Kerr | 115522d | 2016-02-25 10:24:14 +0800 | [diff] [blame] | 171 | strcpy(ifreq_buffer.ifr_name, ifname); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 172 | ret = ioctl(fd, SIOCGIFHWADDR, &ifreq_buffer); |
Jeremy Kerr | d786533 | 2016-02-25 14:52:50 +0800 | [diff] [blame] | 173 | if (ret < 0) |
| 174 | err(EXIT_FAILURE, "Error querying local MAC address"); |
| 175 | |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 176 | memcpy(src_mac, ifreq_buffer.ifr_hwaddr.sa_data, ETH_ALEN); |
Jeremy Kerr | 115522d | 2016-02-25 10:24:14 +0800 | [diff] [blame] | 177 | show_mac_addr(ifname, src_mac); |
Jeremy Kerr | c10bedb | 2016-02-25 14:26:38 +0800 | [diff] [blame] | 178 | |
| 179 | /* find the ifindex of the interface we're using */ |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 180 | memset(&ifreq_buffer, 0x00, sizeof(ifreq_buffer)); |
Jeremy Kerr | 115522d | 2016-02-25 10:24:14 +0800 | [diff] [blame] | 181 | strcpy(ifreq_buffer.ifr_name, ifname); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 182 | ret = ioctl(fd, SIOCGIFINDEX, &ifreq_buffer); |
Jeremy Kerr | d786533 | 2016-02-25 14:52:50 +0800 | [diff] [blame] | 183 | if (ret < 0) |
| 184 | err(EXIT_FAILURE, "Error querying interface %s", ifname); |
| 185 | |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 186 | ifindex = ifreq_buffer.ifr_ifindex; |
| 187 | |
Jeremy Kerr | c10bedb | 2016-02-25 14:26:38 +0800 | [diff] [blame] | 188 | /* length of the received frame */ |
| 189 | int length = 0; |
Jeremy Kerr | 760d6ac | 2016-02-25 14:45:33 +0800 | [diff] [blame] | 190 | static struct arp_packet *inarp_req = (void *)buffer; |
Jeremy Kerr | c10bedb | 2016-02-25 14:26:38 +0800 | [diff] [blame] | 191 | |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 192 | while (1) { |
Jeremy Kerr | 77cafea | 2016-02-25 17:15:28 +0800 | [diff] [blame] | 193 | length = recvfrom(fd, buffer, ETH_ARP_FRAME_LEN, 0, NULL, NULL); |
| 194 | if (length <= 0) { |
| 195 | if (errno == EINTR) |
| 196 | continue; |
| 197 | err(EXIT_FAILURE, "Error recieving ARP packet"); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 198 | } |
Jeremy Kerr | 77cafea | 2016-02-25 17:15:28 +0800 | [diff] [blame] | 199 | |
| 200 | /* is this an inarp request? */ |
| 201 | if (ntohs(inarp_req->arp.ar_op) != ARPOP_InREQUEST) |
| 202 | continue; |
| 203 | |
| 204 | /* ... for us? */ |
| 205 | if (memcmp(src_mac, inarp_req->eh.h_dest, ETH_ALEN)) |
| 206 | continue; |
| 207 | |
| 208 | printf("src mac =%02x:%02x:%02x:%02x:%02x:%02x\n", |
| 209 | inarp_req->src_mac[0], |
| 210 | inarp_req->src_mac[1], |
| 211 | inarp_req->src_mac[2], |
| 212 | inarp_req->src_mac[3], |
| 213 | inarp_req->src_mac[4], |
| 214 | inarp_req->src_mac[5]); |
| 215 | |
| 216 | printf("src ip = %s\n", inet_ntoa(inarp_req->src_ip)); |
| 217 | |
Jeremy Kerr | e54e483 | 2016-02-25 17:23:10 +0800 | [diff] [blame] | 218 | ret = get_local_ipaddr(fd, ifname, &local_ip); |
| 219 | /* if we don't have a local IP address to send, just drop the |
| 220 | * request */ |
| 221 | if (ret) |
| 222 | continue; |
| 223 | |
Jeremy Kerr | 5bb869d | 2016-02-25 18:47:08 +0800 | [diff] [blame^] | 224 | send_arp_packet(fd, ifindex, |
Jeremy Kerr | 77cafea | 2016-02-25 17:15:28 +0800 | [diff] [blame] | 225 | inarp_req->dest_mac, |
Jeremy Kerr | e54e483 | 2016-02-25 17:23:10 +0800 | [diff] [blame] | 226 | &local_ip, |
Jeremy Kerr | 77cafea | 2016-02-25 17:15:28 +0800 | [diff] [blame] | 227 | inarp_req->src_mac, |
| 228 | &inarp_req->src_ip); |
Jeremy Kerr | 46d6d76 | 2016-02-25 18:38:42 +0800 | [diff] [blame] | 229 | |
Jeremy Kerr | 77cafea | 2016-02-25 17:15:28 +0800 | [diff] [blame] | 230 | memset(buffer, 0, sizeof(buffer)); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 231 | } |
| 232 | close(fd); |
| 233 | return 0; |
| 234 | } |