blob: 2ef7d41eed24fe36f69921c144650dadcaaf4355 [file] [log] [blame]
Jeremy Kerr42dc98c2016-02-25 10:23:14 +08001/******************************************************************************
Jeremy Kerrf2101cf2016-03-03 14:49:52 +08002 * 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 Kerr982b7bc2016-02-25 10:54:39 +080017
Jeremy Kerr52e24782016-03-03 14:51:35 +080018#include <err.h>
19#include <errno.h>
Jeremy Kerr982b7bc2016-02-25 10:54:39 +080020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
Jeremy Kerr982b7bc2016-02-25 10:54:39 +080023#include <time.h>
24#include <unistd.h>
25
26#include <sys/ioctl.h>
27#include <sys/socket.h>
28
Jeremy Kerr760d6ac2016-02-25 14:45:33 +080029#include <arpa/inet.h>
Jeremy Kerr52e24782016-03-03 14:51:35 +080030#include <netinet/in.h>
Jeremy Kerr982b7bc2016-02-25 10:54:39 +080031
Jeremy Kerr982b7bc2016-02-25 10:54:39 +080032#include <linux/if_arp.h>
Jeremy Kerr52e24782016-03-03 14:51:35 +080033#include <linux/if_ether.h>
34#include <linux/if_packet.h>
Jeremy Kerr982b7bc2016-02-25 10:54:39 +080035
Jeremy Kerr71f385b2016-03-03 15:18:39 +080036struct eth_addr {
37 uint8_t eth_addr[ETH_ALEN];
38} __attribute__((packed));
39
Jeremy Kerr760d6ac2016-02-25 14:45:33 +080040struct arp_packet {
Jeremy Kerrf2101cf2016-03-03 14:49:52 +080041 struct ethhdr eh;
42 struct arphdr arp;
Jeremy Kerr71f385b2016-03-03 15:18:39 +080043 struct eth_addr src_mac;
Jeremy Kerr760d6ac2016-02-25 14:45:33 +080044 struct in_addr src_ip;
Jeremy Kerr71f385b2016-03-03 15:18:39 +080045 struct eth_addr dest_mac;
Jeremy Kerr760d6ac2016-02-25 14:45:33 +080046 struct in_addr dest_ip;
47} __attribute__((packed));
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080048
Jeremy Kerr7b3bcf72016-02-25 14:29:55 +080049static int send_arp_packet(int fd,
Jeremy Kerrf2101cf2016-03-03 14:49:52 +080050 int ifindex,
Jeremy Kerr71f385b2016-03-03 15:18:39 +080051 const struct eth_addr *src_mac,
Jeremy Kerr88384bc2016-03-03 14:52:50 +080052 const struct in_addr *src_ip,
Jeremy Kerr71f385b2016-03-03 15:18:39 +080053 const struct eth_addr *dest_mac,
Jeremy Kerr88384bc2016-03-03 14:52:50 +080054 const struct in_addr *dest_ip)
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080055{
Jeremy Kerr89dd5142016-02-25 19:40:02 +080056 struct sockaddr_ll addr;
Jeremy Kerr5bb869d2016-02-25 18:47:08 +080057 struct arp_packet arp;
Jeremy Kerrd7865332016-02-25 14:52:50 +080058 int rc;
Jeremy Kerrc10bedb2016-02-25 14:26:38 +080059
Jeremy Kerr5bb869d2016-02-25 18:47:08 +080060 memset(&arp, 0, sizeof(arp));
61
Jeremy Kerrc10bedb2016-02-25 14:26:38 +080062 /* Prepare our link-layer address: raw packet interface,
63 * using the ifindex interface, receiving ARP packets
64 */
Jeremy Kerr89dd5142016-02-25 19:40:02 +080065 addr.sll_family = PF_PACKET;
66 addr.sll_protocol = htons(ETH_P_ARP);
67 addr.sll_ifindex = ifindex;
68 addr.sll_hatype = ARPHRD_ETHER;
69 addr.sll_pkttype = PACKET_OTHERHOST;
70 addr.sll_halen = ETH_ALEN;
71 memcpy(addr.sll_addr, dest_mac, ETH_ALEN);
Jeremy Kerrc10bedb2016-02-25 14:26:38 +080072
73 /* set the frame header */
Jeremy Kerr103a9582016-02-25 19:38:52 +080074 memcpy(arp.eh.h_dest, dest_mac, ETH_ALEN);
75 memcpy(arp.eh.h_source, src_mac, ETH_ALEN);
Jeremy Kerr5bb869d2016-02-25 18:47:08 +080076 arp.eh.h_proto = htons(ETH_P_ARP);
Jeremy Kerrc10bedb2016-02-25 14:26:38 +080077
78 /* Fill InARP request data for ethernet + ipv4 */
Jeremy Kerr5bb869d2016-02-25 18:47:08 +080079 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 Kerrc10bedb2016-02-25 14:26:38 +080084
85 /* fill arp ethernet mac & ipv4 info */
Jeremy Kerr5bb869d2016-02-25 18:47:08 +080086 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 Kerrc10bedb2016-02-25 14:26:38 +080090
91 /* send the packet */
Jeremy Kerr5bb869d2016-02-25 18:47:08 +080092 rc = sendto(fd, &arp, sizeof(arp), 0,
Jeremy Kerr89dd5142016-02-25 19:40:02 +080093 (struct sockaddr *)&addr, sizeof(addr));
Jeremy Kerrd7865332016-02-25 14:52:50 +080094 if (rc < 0)
95 warn("failure sending ARP response");
96
97 return rc;
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080098}
Jeremy Kerr5514f7b2016-02-25 12:41:44 +080099
Jeremy Kerr71f385b2016-03-03 15:18:39 +0800100static const char *eth_mac_to_str(const struct eth_addr *mac_addr)
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800101{
Jeremy Kerrdf046132016-03-03 15:03:22 +0800102 static char mac_str[ETH_ALEN * (sizeof("00:") - 1)];
Jeremy Kerr71f385b2016-03-03 15:18:39 +0800103 const uint8_t *addr = mac_addr->eth_addr;
Jeremy Kerrdf046132016-03-03 15:03:22 +0800104
105 snprintf(mac_str, sizeof(mac_str),
106 "%02x:%02x:%02x:%02x:%02x:%02x",
Jeremy Kerr71f385b2016-03-03 15:18:39 +0800107 addr[0], addr[1], addr[2],
108 addr[3], addr[4], addr[5]);
Jeremy Kerrdf046132016-03-03 15:03:22 +0800109
110 return mac_str;
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800111}
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800112
Jeremy Kerrf3b373f2016-02-25 19:24:59 +0800113static int do_ifreq(int fd, unsigned long type,
114 const char *ifname, struct ifreq *ifreq)
115{
116 memset(ifreq, 0, sizeof(*ifreq));
Jeremy Kerr7275d5c2016-03-03 15:30:10 +0800117 strncpy(ifreq->ifr_name, ifname, sizeof(ifreq->ifr_name));
Jeremy Kerrf3b373f2016-02-25 19:24:59 +0800118
119 return ioctl(fd, type, ifreq);
120}
121
Jeremy Kerre54e4832016-02-25 17:23:10 +0800122static int get_local_ipaddr(int fd, const char *ifname, struct in_addr *addr)
123{
124 struct sockaddr_in *sa;
125 struct ifreq ifreq;
126 int rc;
127
Jeremy Kerrf3b373f2016-02-25 19:24:59 +0800128 rc = do_ifreq(fd, SIOCGIFADDR, ifname, &ifreq);
Jeremy Kerre54e4832016-02-25 17:23:10 +0800129 if (rc) {
Jeremy Kerr1b127c52016-02-25 19:29:02 +0800130 warn("Error querying local IP address for %s", ifname);
Jeremy Kerre54e4832016-02-25 17:23:10 +0800131 return -1;
132 }
133
134 if (ifreq.ifr_addr.sa_family != AF_INET) {
135 warnx("Unknown address family %d in address response",
Jeremy Kerrf2101cf2016-03-03 14:49:52 +0800136 ifreq.ifr_addr.sa_family);
Jeremy Kerre54e4832016-02-25 17:23:10 +0800137 return -1;
138 }
139
140 sa = (struct sockaddr_in *)&ifreq.ifr_addr;
141 memcpy(addr, &sa->sin_addr, sizeof(*addr));
142 return 0;
143}
144
Jeremy Kerr71f385b2016-03-03 15:18:39 +0800145static int get_local_hwaddr(int fd, const char *ifname, struct eth_addr *addr)
Jeremy Kerr86489a12016-02-25 19:14:09 +0800146{
147 struct ifreq ifreq;
148 int rc;
149
Jeremy Kerrf3b373f2016-02-25 19:24:59 +0800150 rc = do_ifreq(fd, SIOCGIFHWADDR, ifname, &ifreq);
Jeremy Kerr86489a12016-02-25 19:14:09 +0800151 if (rc) {
Jeremy Kerr1b127c52016-02-25 19:29:02 +0800152 warn("Error querying local MAC address for %s", ifname);
Jeremy Kerr86489a12016-02-25 19:14:09 +0800153 return -1;
154 }
155
156 memcpy(addr, ifreq.ifr_hwaddr.sa_data, ETH_ALEN);
157 return 0;
158}
159
Jeremy Kerrd8343782016-02-25 19:17:19 +0800160static int get_ifindex(int fd, const char *ifname, int *ifindex)
161{
162 struct ifreq ifreq;
163 int rc;
164
Jeremy Kerrf3b373f2016-02-25 19:24:59 +0800165 rc = do_ifreq(fd, SIOCGIFINDEX, ifname, &ifreq);
Jeremy Kerrd8343782016-02-25 19:17:19 +0800166 if (rc < 0) {
167 warn("Error querying interface %s", ifname);
168 return -1;
169 }
170
171 *ifindex = ifreq.ifr_ifindex;
172 return 0;
173}
174
Jeremy Kerr115522d2016-02-25 10:24:14 +0800175static void usage(const char *progname)
176{
177 fprintf(stderr, "Usage: %s <interface>\n", progname);
178}
179
180int main(int argc, char **argv)
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800181{
Jeremy Kerr71f385b2016-03-03 15:18:39 +0800182 static struct eth_addr local_mac;
Jeremy Kerr14209b02016-02-25 19:34:37 +0800183 static struct in_addr local_ip;
Jeremy Kerr3f8a28e2016-02-25 18:56:01 +0800184 struct arp_packet inarp_req;
Jeremy Kerr14209b02016-02-25 19:34:37 +0800185 int fd, ret, ifindex;
Jeremy Kerr115522d2016-02-25 10:24:14 +0800186 const char *ifname;
Jeremy Kerr3f8a28e2016-02-25 18:56:01 +0800187 ssize_t len;
Jeremy Kerr115522d2016-02-25 10:24:14 +0800188
189 if (argc < 2) {
190 usage(argv[0]);
191 return EXIT_FAILURE;
192 }
193
194 ifname = argv[1];
195
Jeremy Kerrd7865332016-02-25 14:52:50 +0800196 if (strlen(ifname) > IFNAMSIZ)
197 errx(EXIT_FAILURE, "Interface name '%s' is invalid", ifname);
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800198
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800199
Jeremy Kerrc304ce72016-02-25 18:37:14 +0800200 fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
Jeremy Kerrd7865332016-02-25 14:52:50 +0800201 if (fd < 0)
202 err(EXIT_FAILURE, "Error opening ARP socket");
Jeremy Kerrc10bedb2016-02-25 14:26:38 +0800203
Jeremy Kerr1b127c52016-02-25 19:29:02 +0800204 ret = get_ifindex(fd, ifname, &ifindex);
Jeremy Kerr86489a12016-02-25 19:14:09 +0800205 if (ret)
206 exit(EXIT_FAILURE);
Jeremy Kerrd7865332016-02-25 14:52:50 +0800207
Jeremy Kerr71f385b2016-03-03 15:18:39 +0800208 ret = get_local_hwaddr(fd, ifname, &local_mac);
Jeremy Kerrd8343782016-02-25 19:17:19 +0800209 if (ret)
210 exit(EXIT_FAILURE);
211
Jeremy Kerr71f385b2016-03-03 15:18:39 +0800212 printf("%s MAC address: %s\n", ifname, eth_mac_to_str(&local_mac));
Jeremy Kerrc10bedb2016-02-25 14:26:38 +0800213
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800214 while (1) {
Jeremy Kerr3f8a28e2016-02-25 18:56:01 +0800215 len = recvfrom(fd, &inarp_req, sizeof(inarp_req), 0,
216 NULL, NULL);
217 if (len <= 0) {
Jeremy Kerr77cafea2016-02-25 17:15:28 +0800218 if (errno == EINTR)
219 continue;
220 err(EXIT_FAILURE, "Error recieving ARP packet");
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800221 }
Jeremy Kerr77cafea2016-02-25 17:15:28 +0800222
Jeremy Kerr3f8a28e2016-02-25 18:56:01 +0800223 /* Is this packet large enough for an inarp? */
224 if ((size_t)len < sizeof(inarp_req))
225 continue;
226
227 /* ... is it an inarp request? */
228 if (ntohs(inarp_req.arp.ar_op) != ARPOP_InREQUEST)
Jeremy Kerr77cafea2016-02-25 17:15:28 +0800229 continue;
230
231 /* ... for us? */
Jeremy Kerr71f385b2016-03-03 15:18:39 +0800232 if (memcmp(&local_mac, inarp_req.eh.h_dest, ETH_ALEN))
Jeremy Kerr77cafea2016-02-25 17:15:28 +0800233 continue;
234
Jeremy Kerr71f385b2016-03-03 15:18:39 +0800235 printf("src mac: %s\n", eth_mac_to_str(&inarp_req.src_mac));
Jeremy Kerr492de5d2016-02-25 19:35:44 +0800236 printf("src ip: %s\n", inet_ntoa(inarp_req.src_ip));
Jeremy Kerr77cafea2016-02-25 17:15:28 +0800237
Jeremy Kerre54e4832016-02-25 17:23:10 +0800238 ret = get_local_ipaddr(fd, ifname, &local_ip);
239 /* if we don't have a local IP address to send, just drop the
240 * request */
241 if (ret)
242 continue;
243
Jeremy Kerr492de5d2016-02-25 19:35:44 +0800244 printf("local ip: %s\n", inet_ntoa(local_ip));
245
Jeremy Kerr5bb869d2016-02-25 18:47:08 +0800246 send_arp_packet(fd, ifindex,
Jeremy Kerr71f385b2016-03-03 15:18:39 +0800247 &inarp_req.dest_mac,
Jeremy Kerrf2101cf2016-03-03 14:49:52 +0800248 &local_ip,
Jeremy Kerr71f385b2016-03-03 15:18:39 +0800249 &inarp_req.src_mac,
Jeremy Kerrf2101cf2016-03-03 14:49:52 +0800250 &inarp_req.src_ip);
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800251 }
252 close(fd);
253 return 0;
254}