blob: b87bcd85c4788c533e3e644b8625cd613fd4f947 [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 Kerr760d6ac2016-02-25 14:45:33 +080051 struct arp_packet *eth_arp,
Jeremy Kerr5514f7b2016-02-25 12:41:44 +080052 __be16 ar_op,
53 unsigned char *src_mac,
Jeremy Kerr760d6ac2016-02-25 14:45:33 +080054 struct in_addr *src_ip,
55 unsigned char *dest_mac,
56 struct in_addr *dest_ip)
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080057{
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080058 struct ethhdr *eh = &eth_arp->eh;
59 struct arphdr *arp = &eth_arp->arp;
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080060 struct sockaddr_ll socket_address;
Jeremy Kerrd7865332016-02-25 14:52:50 +080061 int rc;
Jeremy Kerrc10bedb2016-02-25 14:26:38 +080062
63 /* Prepare our link-layer address: raw packet interface,
64 * using the ifindex interface, receiving ARP packets
65 */
Jeremy Kerr5514f7b2016-02-25 12:41:44 +080066 socket_address.sll_family = PF_PACKET;
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080067 socket_address.sll_protocol = htons(ETH_P_ARP);
Jeremy Kerr5514f7b2016-02-25 12:41:44 +080068 socket_address.sll_ifindex = ifindex;
Jeremy Kerr5514f7b2016-02-25 12:41:44 +080069 socket_address.sll_hatype = ARPHRD_ETHER;
Jeremy Kerr5514f7b2016-02-25 12:41:44 +080070 socket_address.sll_pkttype = PACKET_OTHERHOST;
Jeremy Kerr5514f7b2016-02-25 12:41:44 +080071 socket_address.sll_halen = ETH_ALEN;
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080072 memcpy(socket_address.sll_addr, dest_mac, ETH_ALEN);
Jeremy Kerrc10bedb2016-02-25 14:26:38 +080073
74 /* set the frame header */
Jeremy Kerr5514f7b2016-02-25 12:41:44 +080075 memcpy((void *)eh->h_dest, (void *)dest_mac, ETH_ALEN);
76 memcpy((void *)eh->h_source, (void *)src_mac, ETH_ALEN);
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080077 eh->h_proto = htons(ETH_P_ARP);
Jeremy Kerrc10bedb2016-02-25 14:26:38 +080078
79 /* Fill InARP request data for ethernet + ipv4 */
Jeremy Kerr42dc98c2016-02-25 10:23:14 +080080 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 Kerr5514f7b2016-02-25 12:41:44 +080084 arp->ar_op = htons(ar_op);
Jeremy Kerrc10bedb2016-02-25 14:26:38 +080085
86 /* fill arp ethernet mac & ipv4 info */
Jeremy Kerr760d6ac2016-02-25 14:45:33 +080087 memcpy(&eth_arp->src_mac, src_mac, sizeof(eth_arp->src_mac));
88 memcpy(&eth_arp->src_ip, src_ip, sizeof(eth_arp->src_ip));
89 memcpy(&eth_arp->dest_mac, dest_mac, sizeof(eth_arp->dest_mac));
90 memcpy(&eth_arp->dest_ip, dest_ip, sizeof(eth_arp->dest_ip));
Jeremy Kerrc10bedb2016-02-25 14:26:38 +080091
92 /* send the packet */
Jeremy Kerrd7865332016-02-25 14:52:50 +080093 rc = sendto(fd, eth_arp, ETH_ARP_FRAME_LEN, 0,
Jeremy Kerr5514f7b2016-02-25 12:41:44 +080094 (struct sockaddr *)&socket_address,
95 sizeof(socket_address));
Jeremy Kerrd7865332016-02-25 14:52:50 +080096 if (rc < 0)
97 warn("failure sending ARP response");
98
99 return rc;
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800100}
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800101
Jeremy Kerr7b3bcf72016-02-25 14:29:55 +0800102static void show_mac_addr(const char *name, unsigned char *mac_addr)
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800103{
104 int i;
105 printf("%s MAC address: ", name);
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800106 for (i = 0; i < 6; i++) {
107 printf("%.2X%c", (unsigned char)mac_addr[i],
108 (i == 5) ? '\n' : ':');
109 }
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800110 return;
111}
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800112
Jeremy Kerr115522d2016-02-25 10:24:14 +0800113static void usage(const char *progname)
114{
115 fprintf(stderr, "Usage: %s <interface>\n", progname);
116}
117
118int main(int argc, char **argv)
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800119{
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800120 /*buffer for ethernet frame */
Jeremy Kerrc10bedb2016-02-25 14:26:38 +0800121 static unsigned char buffer[ETH_FRAME_LEN];
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800122 int send_result = 0;
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800123 static struct ifreq ifreq_buffer;
Jeremy Kerr115522d2016-02-25 10:24:14 +0800124 const char *ifname;
Jeremy Kerr760d6ac2016-02-25 14:45:33 +0800125 int fd, ret;
Jeremy Kerr115522d2016-02-25 10:24:14 +0800126
127 if (argc < 2) {
128 usage(argv[0]);
129 return EXIT_FAILURE;
130 }
131
132 ifname = argv[1];
133
Jeremy Kerrd7865332016-02-25 14:52:50 +0800134 if (strlen(ifname) > IFNAMSIZ)
135 errx(EXIT_FAILURE, "Interface name '%s' is invalid", ifname);
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800136
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800137 static unsigned char src_mac[6];
Jeremy Kerr760d6ac2016-02-25 14:45:33 +0800138 static struct in_addr src_ip;
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800139 int ifindex;
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800140
Jeremy Kerrd7865332016-02-25 14:52:50 +0800141 fd = socket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
142 if (fd < 0)
143 err(EXIT_FAILURE, "Error opening ARP socket");
Jeremy Kerrc10bedb2016-02-25 14:26:38 +0800144
145 /* Query local mac address */
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800146 memset(&ifreq_buffer, 0x00, sizeof(ifreq_buffer));
Jeremy Kerr115522d2016-02-25 10:24:14 +0800147 strcpy(ifreq_buffer.ifr_name, ifname);
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800148 ret = ioctl(fd, SIOCGIFHWADDR, &ifreq_buffer);
Jeremy Kerrd7865332016-02-25 14:52:50 +0800149 if (ret < 0)
150 err(EXIT_FAILURE, "Error querying local MAC address");
151
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800152 memcpy(src_mac, ifreq_buffer.ifr_hwaddr.sa_data, ETH_ALEN);
Jeremy Kerr115522d2016-02-25 10:24:14 +0800153 show_mac_addr(ifname, src_mac);
Jeremy Kerrc10bedb2016-02-25 14:26:38 +0800154
155 /* find the ifindex of the interface we're using */
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800156 memset(&ifreq_buffer, 0x00, sizeof(ifreq_buffer));
Jeremy Kerr115522d2016-02-25 10:24:14 +0800157 strcpy(ifreq_buffer.ifr_name, ifname);
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800158 ret = ioctl(fd, SIOCGIFINDEX, &ifreq_buffer);
Jeremy Kerrd7865332016-02-25 14:52:50 +0800159 if (ret < 0)
160 err(EXIT_FAILURE, "Error querying interface %s", ifname);
161
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800162 ifindex = ifreq_buffer.ifr_ifindex;
163
Jeremy Kerrc10bedb2016-02-25 14:26:38 +0800164 /* length of the received frame */
165 int length = 0;
Jeremy Kerr760d6ac2016-02-25 14:45:33 +0800166 static struct arp_packet *inarp_req = (void *)buffer;
167 static struct arp_packet inarp_resp;
Jeremy Kerrc10bedb2016-02-25 14:26:38 +0800168
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800169 while (1) {
Jeremy Kerrc10bedb2016-02-25 14:26:38 +0800170 /* get local ip address */
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800171 memset(&ifreq_buffer, 0x00, sizeof(ifreq_buffer));
Jeremy Kerr115522d2016-02-25 10:24:14 +0800172 strcpy(ifreq_buffer.ifr_name, ifname);
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800173 ret = ioctl(fd, SIOCGIFADDR, &ifreq_buffer);
174 if (ret == -1) {
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800175 sleep(1);
176 continue;
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800177 }
178
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800179 if (AF_INET == ifreq_buffer.ifr_addr.sa_family) {
Jeremy Kerr760d6ac2016-02-25 14:45:33 +0800180 memcpy(&src_ip, &ifreq_buffer.ifr_addr.sa_data[2],
181 sizeof(src_ip));
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800182 } else {
Jeremy Kerrd7865332016-02-25 14:52:50 +0800183 warnx("Unknown address family %d in request!",
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800184 ifreq_buffer.ifr_addr.sa_family);
185 sleep(1);
186 continue;
Jeremy Kerr42dc98c2016-02-25 10:23:14 +0800187 }
188
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800189 memset((void *)&inarp_resp, 0, sizeof inarp_resp);
Jeremy Kerr5514f7b2016-02-25 12:41:44 +0800190
Jeremy Kerr77cafea2016-02-25 17:15:28 +0800191 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 Kerr42dc98c2016-02-25 10:23:14 +0800196 }
Jeremy Kerr77cafea2016-02-25 17:15:28 +0800197
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 Kerr42dc98c2016-02-25 10:23:14 +0800233 }
234 close(fd);
235 return 0;
236}