blob: 81b15645084964aff56fa4a05b6a12ce87963abd [file] [log] [blame]
Jeremy Kerr42dc98c2016-02-25 10:23:14 +08001/******************************************************************************
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 Kerr982b7bc2016-02-25 10:54:39 +080016
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 Kerrd7865332016-02-25 14:52:50 +080023#include <err.h>
Jeremy Kerr982b7bc2016-02-25 10:54:39 +080024
25#include <sys/ioctl.h>
26#include <sys/socket.h>
27
28#include <netinet/in.h>
Jeremy Kerr760d6ac2016-02-25 14:45:33 +080029#include <arpa/inet.h>
Jeremy Kerr982b7bc2016-02-25 10:54:39 +080030
31#include <linux/if_packet.h>
32#include <linux/if_ether.h>
33#include <linux/if_arp.h>
34
Jeremy Kerr66a28052016-02-25 14:31:35 +080035#define ETH_ARP_FRAME_LEN ( \
36 sizeof(struct ethhdr) + \
37 sizeof(struct arphdr) + \
Jeremy Kerr760d6ac2016-02-25 14:45:33 +080038 ((ETH_ALEN + sizeof(struct in_addr)) * 2))
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080039
Jeremy Kerr760d6ac2016-02-25 14:45:33 +080040struct arp_packet {
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080041 struct ethhdr eh;
42 struct arphdr arp;
Jeremy Kerr760d6ac2016-02-25 14:45:33 +080043 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 Kerr42dc98c2016-02-25 10:23:14 +080048
Jeremy Kerr7b3bcf72016-02-25 14:29:55 +080049static int send_arp_packet(int fd,
Jeremy Kerr5514f7b2016-02-25 12:41:44 +080050 int ifindex,
Jeremy Kerr5514f7b2016-02-25 12:41:44 +080051 unsigned char *src_mac,
Jeremy Kerr760d6ac2016-02-25 14:45:33 +080052 struct in_addr *src_ip,
53 unsigned char *dest_mac,
54 struct in_addr *dest_ip)
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080055{
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080056 struct sockaddr_ll socket_address;
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 Kerr5514f7b2016-02-25 12:41:44 +080065 socket_address.sll_family = PF_PACKET;
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080066 socket_address.sll_protocol = htons(ETH_P_ARP);
Jeremy Kerr5514f7b2016-02-25 12:41:44 +080067 socket_address.sll_ifindex = ifindex;
Jeremy Kerr5514f7b2016-02-25 12:41:44 +080068 socket_address.sll_hatype = ARPHRD_ETHER;
Jeremy Kerr5514f7b2016-02-25 12:41:44 +080069 socket_address.sll_pkttype = PACKET_OTHERHOST;
Jeremy Kerr5514f7b2016-02-25 12:41:44 +080070 socket_address.sll_halen = ETH_ALEN;
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080071 memcpy(socket_address.sll_addr, dest_mac, ETH_ALEN);
Jeremy Kerrc10bedb2016-02-25 14:26:38 +080072
73 /* set the frame header */
Jeremy Kerr5bb869d2016-02-25 18:47:08 +080074 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 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 Kerr5514f7b2016-02-25 12:41:44 +080093 (struct sockaddr *)&socket_address,
94 sizeof(socket_address));
Jeremy Kerrd7865332016-02-25 14:52:50 +080095 if (rc < 0)
96 warn("failure sending ARP response");
97
98 return rc;
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080099}
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800100
Jeremy Kerr7b3bcf72016-02-25 14:29:55 +0800101static void show_mac_addr(const char *name, unsigned char *mac_addr)
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800102{
103 int i;
104 printf("%s MAC address: ", name);
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800105 for (i = 0; i < 6; i++) {
106 printf("%.2X%c", (unsigned char)mac_addr[i],
107 (i == 5) ? '\n' : ':');
108 }
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800109 return;
110}
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800111
Jeremy Kerre54e4832016-02-25 17:23:10 +0800112static 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 Kerr115522d2016-02-25 10:24:14 +0800138static void usage(const char *progname)
139{
140 fprintf(stderr, "Usage: %s <interface>\n", progname);
141}
142
143int main(int argc, char **argv)
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800144{
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800145 /*buffer for ethernet frame */
Jeremy Kerrc10bedb2016-02-25 14:26:38 +0800146 static unsigned char buffer[ETH_FRAME_LEN];
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800147 static struct ifreq ifreq_buffer;
Jeremy Kerr115522d2016-02-25 10:24:14 +0800148 const char *ifname;
Jeremy Kerr760d6ac2016-02-25 14:45:33 +0800149 int fd, ret;
Jeremy Kerr115522d2016-02-25 10:24:14 +0800150
151 if (argc < 2) {
152 usage(argv[0]);
153 return EXIT_FAILURE;
154 }
155
156 ifname = argv[1];
157
Jeremy Kerrd7865332016-02-25 14:52:50 +0800158 if (strlen(ifname) > IFNAMSIZ)
159 errx(EXIT_FAILURE, "Interface name '%s' is invalid", ifname);
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800160
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800161 static unsigned char src_mac[6];
Jeremy Kerre54e4832016-02-25 17:23:10 +0800162 static struct in_addr local_ip;
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800163 int ifindex;
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800164
Jeremy Kerrc304ce72016-02-25 18:37:14 +0800165 fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
Jeremy Kerrd7865332016-02-25 14:52:50 +0800166 if (fd < 0)
167 err(EXIT_FAILURE, "Error opening ARP socket");
Jeremy Kerrc10bedb2016-02-25 14:26:38 +0800168
169 /* Query local mac address */
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800170 memset(&ifreq_buffer, 0x00, sizeof(ifreq_buffer));
Jeremy Kerr115522d2016-02-25 10:24:14 +0800171 strcpy(ifreq_buffer.ifr_name, ifname);
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800172 ret = ioctl(fd, SIOCGIFHWADDR, &ifreq_buffer);
Jeremy Kerrd7865332016-02-25 14:52:50 +0800173 if (ret < 0)
174 err(EXIT_FAILURE, "Error querying local MAC address");
175
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800176 memcpy(src_mac, ifreq_buffer.ifr_hwaddr.sa_data, ETH_ALEN);
Jeremy Kerr115522d2016-02-25 10:24:14 +0800177 show_mac_addr(ifname, src_mac);
Jeremy Kerrc10bedb2016-02-25 14:26:38 +0800178
179 /* find the ifindex of the interface we're using */
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800180 memset(&ifreq_buffer, 0x00, sizeof(ifreq_buffer));
Jeremy Kerr115522d2016-02-25 10:24:14 +0800181 strcpy(ifreq_buffer.ifr_name, ifname);
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800182 ret = ioctl(fd, SIOCGIFINDEX, &ifreq_buffer);
Jeremy Kerrd7865332016-02-25 14:52:50 +0800183 if (ret < 0)
184 err(EXIT_FAILURE, "Error querying interface %s", ifname);
185
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800186 ifindex = ifreq_buffer.ifr_ifindex;
187
Jeremy Kerrc10bedb2016-02-25 14:26:38 +0800188 /* length of the received frame */
189 int length = 0;
Jeremy Kerr760d6ac2016-02-25 14:45:33 +0800190 static struct arp_packet *inarp_req = (void *)buffer;
Jeremy Kerrc10bedb2016-02-25 14:26:38 +0800191
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800192 while (1) {
Jeremy Kerr77cafea2016-02-25 17:15:28 +0800193 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 Kerr42dc98c2016-02-25 10:23:14 +0800198 }
Jeremy Kerr77cafea2016-02-25 17:15:28 +0800199
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 Kerre54e4832016-02-25 17:23:10 +0800218 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 Kerr5bb869d2016-02-25 18:47:08 +0800224 send_arp_packet(fd, ifindex,
Jeremy Kerr77cafea2016-02-25 17:15:28 +0800225 inarp_req->dest_mac,
Jeremy Kerre54e4832016-02-25 17:23:10 +0800226 &local_ip,
Jeremy Kerr77cafea2016-02-25 17:15:28 +0800227 inarp_req->src_mac,
228 &inarp_req->src_ip);
Jeremy Kerr46d6d762016-02-25 18:38:42 +0800229
Jeremy Kerr77cafea2016-02-25 17:15:28 +0800230 memset(buffer, 0, sizeof(buffer));
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800231 }
232 close(fd);
233 return 0;
234}