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