blob: c30e1e8256fdabea879f46c9efe74b6c315f1952 [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 Kerr760d6ac2016-02-25 14:45:33 +080036struct arp_packet {
Jeremy Kerrf2101cf2016-03-03 14:49:52 +080037 struct ethhdr eh;
38 struct arphdr arp;
Jeremy Kerr760d6ac2016-02-25 14:45:33 +080039 uint8_t src_mac[ETH_ALEN];
40 struct in_addr src_ip;
41 uint8_t dest_mac[ETH_ALEN];
42 struct in_addr dest_ip;
43} __attribute__((packed));
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080044
Jeremy Kerr7b3bcf72016-02-25 14:29:55 +080045static int send_arp_packet(int fd,
Jeremy Kerrf2101cf2016-03-03 14:49:52 +080046 int ifindex,
Jeremy Kerr88384bc2016-03-03 14:52:50 +080047 const unsigned char *src_mac,
48 const struct in_addr *src_ip,
49 const unsigned char *dest_mac,
50 const struct in_addr *dest_ip)
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080051{
Jeremy Kerr89dd5142016-02-25 19:40:02 +080052 struct sockaddr_ll addr;
Jeremy Kerr5bb869d2016-02-25 18:47:08 +080053 struct arp_packet arp;
Jeremy Kerrd7865332016-02-25 14:52:50 +080054 int rc;
Jeremy Kerrc10bedb2016-02-25 14:26:38 +080055
Jeremy Kerr5bb869d2016-02-25 18:47:08 +080056 memset(&arp, 0, sizeof(arp));
57
Jeremy Kerrc10bedb2016-02-25 14:26:38 +080058 /* Prepare our link-layer address: raw packet interface,
59 * using the ifindex interface, receiving ARP packets
60 */
Jeremy Kerr89dd5142016-02-25 19:40:02 +080061 addr.sll_family = PF_PACKET;
62 addr.sll_protocol = htons(ETH_P_ARP);
63 addr.sll_ifindex = ifindex;
64 addr.sll_hatype = ARPHRD_ETHER;
65 addr.sll_pkttype = PACKET_OTHERHOST;
66 addr.sll_halen = ETH_ALEN;
67 memcpy(addr.sll_addr, dest_mac, ETH_ALEN);
Jeremy Kerrc10bedb2016-02-25 14:26:38 +080068
69 /* set the frame header */
Jeremy Kerr103a9582016-02-25 19:38:52 +080070 memcpy(arp.eh.h_dest, dest_mac, ETH_ALEN);
71 memcpy(arp.eh.h_source, src_mac, ETH_ALEN);
Jeremy Kerr5bb869d2016-02-25 18:47:08 +080072 arp.eh.h_proto = htons(ETH_P_ARP);
Jeremy Kerrc10bedb2016-02-25 14:26:38 +080073
74 /* Fill InARP request data for ethernet + ipv4 */
Jeremy Kerr5bb869d2016-02-25 18:47:08 +080075 arp.arp.ar_hrd = htons(ARPHRD_ETHER);
76 arp.arp.ar_pro = htons(ETH_P_ARP);
77 arp.arp.ar_hln = ETH_ALEN;
78 arp.arp.ar_pln = 4;
79 arp.arp.ar_op = htons(ARPOP_InREPLY);
Jeremy Kerrc10bedb2016-02-25 14:26:38 +080080
81 /* fill arp ethernet mac & ipv4 info */
Jeremy Kerr5bb869d2016-02-25 18:47:08 +080082 memcpy(&arp.src_mac, src_mac, sizeof(arp.src_mac));
83 memcpy(&arp.src_ip, src_ip, sizeof(arp.src_ip));
84 memcpy(&arp.dest_mac, dest_mac, sizeof(arp.dest_mac));
85 memcpy(&arp.dest_ip, dest_ip, sizeof(arp.dest_ip));
Jeremy Kerrc10bedb2016-02-25 14:26:38 +080086
87 /* send the packet */
Jeremy Kerr5bb869d2016-02-25 18:47:08 +080088 rc = sendto(fd, &arp, sizeof(arp), 0,
Jeremy Kerr89dd5142016-02-25 19:40:02 +080089 (struct sockaddr *)&addr, sizeof(addr));
Jeremy Kerrd7865332016-02-25 14:52:50 +080090 if (rc < 0)
91 warn("failure sending ARP response");
92
93 return rc;
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080094}
Jeremy Kerr5514f7b2016-02-25 12:41:44 +080095
Jeremy Kerr88384bc2016-03-03 14:52:50 +080096static void show_mac_addr(const char *name, const unsigned char *mac_addr)
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080097{
98 int i;
99 printf("%s MAC address: ", name);
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800100 for (i = 0; i < 6; i++) {
101 printf("%.2X%c", (unsigned char)mac_addr[i],
Jeremy Kerrf2101cf2016-03-03 14:49:52 +0800102 (i == 5) ? '\n' : ':');
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800103 }
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800104 return;
105}
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800106
Jeremy Kerrf3b373f2016-02-25 19:24:59 +0800107static 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 Kerre54e4832016-02-25 17:23:10 +0800116static 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 Kerrf3b373f2016-02-25 19:24:59 +0800122 rc = do_ifreq(fd, SIOCGIFADDR, ifname, &ifreq);
Jeremy Kerre54e4832016-02-25 17:23:10 +0800123 if (rc) {
Jeremy Kerr1b127c52016-02-25 19:29:02 +0800124 warn("Error querying local IP address for %s", ifname);
Jeremy Kerre54e4832016-02-25 17:23:10 +0800125 return -1;
126 }
127
128 if (ifreq.ifr_addr.sa_family != AF_INET) {
129 warnx("Unknown address family %d in address response",
Jeremy Kerrf2101cf2016-03-03 14:49:52 +0800130 ifreq.ifr_addr.sa_family);
Jeremy Kerre54e4832016-02-25 17:23:10 +0800131 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 Kerr86489a12016-02-25 19:14:09 +0800139static int get_local_hwaddr(int fd, const char *ifname, uint8_t *addr)
140{
141 struct ifreq ifreq;
142 int rc;
143
Jeremy Kerrf3b373f2016-02-25 19:24:59 +0800144 rc = do_ifreq(fd, SIOCGIFHWADDR, ifname, &ifreq);
Jeremy Kerr86489a12016-02-25 19:14:09 +0800145 if (rc) {
Jeremy Kerr1b127c52016-02-25 19:29:02 +0800146 warn("Error querying local MAC address for %s", ifname);
Jeremy Kerr86489a12016-02-25 19:14:09 +0800147 return -1;
148 }
149
150 memcpy(addr, ifreq.ifr_hwaddr.sa_data, ETH_ALEN);
151 return 0;
152}
153
Jeremy Kerrd8343782016-02-25 19:17:19 +0800154static int get_ifindex(int fd, const char *ifname, int *ifindex)
155{
156 struct ifreq ifreq;
157 int rc;
158
Jeremy Kerrf3b373f2016-02-25 19:24:59 +0800159 rc = do_ifreq(fd, SIOCGIFINDEX, ifname, &ifreq);
Jeremy Kerrd8343782016-02-25 19:17:19 +0800160 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 Kerr115522d2016-02-25 10:24:14 +0800169static void usage(const char *progname)
170{
171 fprintf(stderr, "Usage: %s <interface>\n", progname);
172}
173
174int main(int argc, char **argv)
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800175{
Jeremy Kerr14209b02016-02-25 19:34:37 +0800176 static unsigned char local_mac[6];
177 static struct in_addr local_ip;
Jeremy Kerr3f8a28e2016-02-25 18:56:01 +0800178 struct arp_packet inarp_req;
Jeremy Kerr14209b02016-02-25 19:34:37 +0800179 int fd, ret, ifindex;
Jeremy Kerr115522d2016-02-25 10:24:14 +0800180 const char *ifname;
Jeremy Kerr3f8a28e2016-02-25 18:56:01 +0800181 ssize_t len;
Jeremy Kerr115522d2016-02-25 10:24:14 +0800182
183 if (argc < 2) {
184 usage(argv[0]);
185 return EXIT_FAILURE;
186 }
187
188 ifname = argv[1];
189
Jeremy Kerrd7865332016-02-25 14:52:50 +0800190 if (strlen(ifname) > IFNAMSIZ)
191 errx(EXIT_FAILURE, "Interface name '%s' is invalid", ifname);
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800192
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800193
Jeremy Kerrc304ce72016-02-25 18:37:14 +0800194 fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
Jeremy Kerrd7865332016-02-25 14:52:50 +0800195 if (fd < 0)
196 err(EXIT_FAILURE, "Error opening ARP socket");
Jeremy Kerrc10bedb2016-02-25 14:26:38 +0800197
Jeremy Kerr1b127c52016-02-25 19:29:02 +0800198 ret = get_ifindex(fd, ifname, &ifindex);
Jeremy Kerr86489a12016-02-25 19:14:09 +0800199 if (ret)
200 exit(EXIT_FAILURE);
Jeremy Kerrd7865332016-02-25 14:52:50 +0800201
Jeremy Kerr14209b02016-02-25 19:34:37 +0800202 ret = get_local_hwaddr(fd, ifname, local_mac);
Jeremy Kerrd8343782016-02-25 19:17:19 +0800203 if (ret)
204 exit(EXIT_FAILURE);
205
Jeremy Kerr14209b02016-02-25 19:34:37 +0800206 show_mac_addr(ifname, local_mac);
Jeremy Kerrc10bedb2016-02-25 14:26:38 +0800207
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800208 while (1) {
Jeremy Kerr3f8a28e2016-02-25 18:56:01 +0800209 len = recvfrom(fd, &inarp_req, sizeof(inarp_req), 0,
210 NULL, NULL);
211 if (len <= 0) {
Jeremy Kerr77cafea2016-02-25 17:15:28 +0800212 if (errno == EINTR)
213 continue;
214 err(EXIT_FAILURE, "Error recieving ARP packet");
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800215 }
Jeremy Kerr77cafea2016-02-25 17:15:28 +0800216
Jeremy Kerr3f8a28e2016-02-25 18:56:01 +0800217 /* 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 Kerr77cafea2016-02-25 17:15:28 +0800223 continue;
224
225 /* ... for us? */
Jeremy Kerr14209b02016-02-25 19:34:37 +0800226 if (memcmp(local_mac, inarp_req.eh.h_dest, ETH_ALEN))
Jeremy Kerr77cafea2016-02-25 17:15:28 +0800227 continue;
228
Jeremy Kerr492de5d2016-02-25 19:35:44 +0800229 printf("src mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
Jeremy Kerr3f8a28e2016-02-25 18:56:01 +0800230 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 Kerr77cafea2016-02-25 17:15:28 +0800236
Jeremy Kerr492de5d2016-02-25 19:35:44 +0800237 printf("src ip: %s\n", inet_ntoa(inarp_req.src_ip));
Jeremy Kerr77cafea2016-02-25 17:15:28 +0800238
Jeremy Kerre54e4832016-02-25 17:23:10 +0800239 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 Kerr492de5d2016-02-25 19:35:44 +0800245 printf("local ip: %s\n", inet_ntoa(local_ip));
246
Jeremy Kerr5bb869d2016-02-25 18:47:08 +0800247 send_arp_packet(fd, ifindex,
Jeremy Kerrf2101cf2016-03-03 14:49:52 +0800248 inarp_req.dest_mac,
249 &local_ip,
250 inarp_req.src_mac,
251 &inarp_req.src_ip);
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800252 }
253 close(fd);
254 return 0;
255}