-
Notifications
You must be signed in to change notification settings - Fork 7
/
garp.c
252 lines (200 loc) · 6.33 KB
/
garp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* ============================================================================
*
* Filename: garp.c
*
* Description: Simple implementation of a gratuitous ARP packet.
*
* Created: 02/03/2017 09:30:35 PM
*
* Author: Gustavo Pantuza
* Organization: Computer Science Community
*
* ============================================================================
*/
/* C standard library */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
/* System libraries */
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
/* Networking header files */
#include <netdb.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/ethernet.h>
/* Gratuitous ARP */
#include "garp.h"
#include "garp_config.h"
/**
* Prints the Usage message with garp example
*/
void
usage ()
{
fprintf(stdout,
"Gratuitous ARP %d.%d\n\n"
"Usage: garp iface addr\n"
"Example: garp eth0 192.168.1.42\n",
GARP_VERSION_MAJOR, GARP_VERSION_MINOR
);
}
/**
* Copies the interface name from arguments into buffer
*/
void
set_iface (char iface[25], char *argv_iface)
{
strncpy(iface, argv_iface, strlen(argv_iface));
}
/**
* Assigns the source IP address from arguments
*/
void
set_ip (struct in_addr* source_addr, char* argv_addr)
{
source_addr->s_addr = inet_addr(argv_addr);
}
/**
* Gets the MAC address and the index of the given interface using ioctl.
*/
void
get_mac_address (struct ifreq* ethernet, char* iface,
unsigned char source_eth_addr[ETHERNET_ADDR_LEN])
{
int file_descriptor = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
strncpy(ethernet->ifr_name, iface, IF_NAMESIZE);
/* Copies the MAC address into ethernet ifreq struct object */
if(ioctl(file_descriptor, SIOCGIFHWADDR, ethernet) == -1) {
fprintf(stderr, "Error: Cannot get ethernet address: ");
fprintf(stderr, "%s", strerror(errno));
exit(1);
}
sprintf(
(char *) source_eth_addr, "%s",
(char *) ethernet->ifr_hwaddr.sa_data
);
close(file_descriptor);
}
/**
* Gets the interface index using ioctl
*/
void
get_iface_index (struct ifreq* ethernet, char* iface)
{
int file_descriptor = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
struct ifreq tmp_ethernet;
strncpy(tmp_ethernet.ifr_name, iface, IF_NAMESIZE);
/* Copies the interface index into ethernet ifreq struct object */
if(ioctl(file_descriptor, SIOCGIFINDEX, &tmp_ethernet) == -1){
fprintf(stderr, "Error: Cannot get ethernet index: ");
fprintf(stderr, "%s", strerror(errno));
exit(1);
}
ethernet->ifr_ifindex = tmp_ethernet.ifr_ifindex;
close(file_descriptor);
}
/**
* Function used for printing the raw hexdecimal packet data
*/
void
print_raw_packet (struct gratuitous_arp *packet, unsigned int packet_len)
{
char buffer[packet_len];
memcpy(buffer, packet, packet_len);
for(int i = 0; i < packet_len; i++) {
/* Breaks line for each 32 bits size */
if(!(i % 4)) {
fprintf(stdout, "\n");
}
fprintf(stdout, "%.2X ", buffer[i] & 0xff);
}
fprintf(stdout, "\n");
}
/**
* Sends the packet to a socket using sendto.
*/
void
send_gratuitous_arp (int socket_fd, struct gratuitous_arp* arp,
struct sockaddr_ll* addr)
{
int sent = sendto(socket_fd, arp, sizeof(*arp), 0,
(struct sockaddr *) addr, sizeof(*addr));
if(sent < 0) {
fprintf(stderr, "Error on sending packet: ");
fprintf(stderr, "%s", strerror(errno));
exit(EXIT_FAILURE);
}
}
/**
* Main execution of the Gratuitous ARP program
*/
int
main (int argc, char* argv[])
{
if(argc != 3) {
fprintf(stderr, "Error: Wrong number of arguments %d\n\n", argc);
usage();
return EXIT_FAILURE;
}
fprintf(stdout, "Sending gratuitous ARP..\n");
/* Interface name used to send packet */
char iface[25];
/* Structs to store source and target addresses */
struct in_addr source_ip_address;
/* The Gratuitous ARP packet data */
struct gratuitous_arp packet;
/* The link layer packet */
struct sockaddr_ll link_address;
/* The ethernet interface */
struct ifreq ethernet;
/* File descriptor of the socket local socket */
int socket_fd;
/* Sets interface name from program arguments */
set_iface(iface, argv[1]);
/* Sets the IP address that we will fake */
set_ip(&source_ip_address, argv[2]);
/* Tries to create a socket */
socket_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
if(socket_fd < 0) {
fprintf(stderr, "Error on creating socket");
return EXIT_FAILURE;
}
/* Fill data for Ethernet header */
get_mac_address(ðernet, iface, packet.source_ethernet_address);
get_iface_index(ðernet, iface);
memset(packet.target_ethernet_address, 0xFF, ETHERNET_ADDR_LEN);
packet.ethernet_type = htons(ETHERNET_TYPE);
/* Link layer address data */
link_address.sll_family = AF_PACKET;
link_address.sll_protocol = htons(ETH_P_ARP);
link_address.sll_ifindex = ethernet.ifr_ifindex;
/* Copy data into ARP packet struct */
packet.hardware_type = htons(ETHERNET_ARP_TYPE);
packet.protocol_type = htons(IP_TYPE);
packet.hardware_address_length = ETHERNET_ADDR_LEN;
packet.protocol_address_length = IP_ADDR_LEN;
packet.arp_options = htons(REQUEST_OPERATION);
/* Gets the local interface hardware (Ethernet) and protocol address */
memcpy(packet.source_hardware_address,
packet.source_ethernet_address, ETHERNET_ADDR_LEN);
memcpy(packet.source_protocol_address, &source_ip_address, IP_ADDR_LEN);
/* Copies the target hardware (Ethernet) and protocol address */
memcpy(packet.target_hardware_address,
packet.target_ethernet_address, ETHERNET_ADDR_LEN);
memcpy(packet.target_protocol_address, &source_ip_address, IP_ADDR_LEN);
/* Just adding extra padding data to complete packet size */
memset(packet.padding, 0, ARP_PADDING_SIZE);
/* Sends the packet out */
send_gratuitous_arp(socket_fd, &packet, &link_address);
/* Prints hexdecimal packet data */
print_raw_packet(&packet, sizeof(packet));
/* Closes the socket */
close(socket_fd);
return EXIT_SUCCESS;
}