arpd.c
5.82 KB
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
/*
* arpd.c --
*
* A program to reply to ARP and RARP requests. This program
* only translates between Internet and Ethernet addresses. See
* RFC826 for details of the ARP protocol, and RFC903 for details
* of the RARP protocol.
*
* Copyright 1989 Regents of the University of California
* Permission to use, copy, modify, and distribute this
* software and its documentation for any purpose and without
* fee is hereby granted, provided that the above copyright
* notice appear in all copies. The University of California
* makes no representations about the suitability of this
* software for any purpose. It is provided "as is" without
* express or implied warranty.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if_arp.h>
#include <netinet/in.h>
#include <sys/file.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <net/if.h>
#include <netinet/if_ether.h>
#include <bstring.h>
int arpverbose = 0;
extern u_long proxyaddr;
/*
* The ARP/RARP packet size is defined below. Because of structure
* alignment differences between machines, it isn't safe to use
* sizeof with the structure.
*/
#define ARP_PACKET_SIZE 42
static int FindArpReply(unsigned long dstipaddr, u_char* etherdst);
extern unsigned long FindGateway(unsigned long);
struct ether_addr {
unsigned char ether_addr_octet[6];
};
extern struct ether_addr *ether_aton(char *s);
/*
*----------------------------------------------------------------------
*
* ArpRequest --
*
* Handle an ARP request.
*
* Results:
* None.
*
* Side effects:
* A response is transmitted for the ARP request.
*
*----------------------------------------------------------------------
*/
int
ArpRequest(void *pin, int pinlen, void *pout, int *poutlen)
{
struct {
struct ether_header hdr;
struct ether_arp arp;
} packet;
int bytesRead, etherType, protocolType, hardwareType, arpOp;
unsigned long senderAddr, targetAddr;
u_char eaddr[6];
/*
* Read and validate the ARP packet.
*/
bytesRead = (sizeof(packet) > pinlen) ? pinlen : sizeof(packet);
bcopy(pin, &packet, bytesRead);
if (bytesRead < 0) {
printf("arpd couldn't read ARP packet: %d\n", errno);
return 0;
}
if (bytesRead < ARP_PACKET_SIZE) {
if (arpverbose)
printf("arpd got short ARP packet: only %d bytes\n", bytesRead);
return 0;
}
etherType = ntohs(packet.hdr.ether_type);
if (etherType != ETHERTYPE_ARP) {
if (arpverbose)
printf("arpd got ARP packet with ether_type 0x%x\n", etherType);
return 0;
}
arpOp = ntohs(packet.arp.ea_hdr.ar_op);
if (arpOp != ARPOP_REQUEST) {
if (arpverbose) {
printf("arpd got ARP packet with unknown op %d\n", arpOp);
}
return 0;
}
hardwareType = ntohs(packet.arp.ea_hdr.ar_hrd);
if (hardwareType != ARPHRD_ETHER) {
if (arpverbose) {
printf("arpd got ARP packet with unknown hardware type 0x%x\n",
hardwareType);
}
return 0;
}
protocolType = ntohs(packet.arp.ea_hdr.ar_pro);
if (protocolType != ETHERTYPE_IP) {
if (arpverbose) {
printf("arpd got ARP packet with unknown protocol type 0x%x\n",
protocolType);
}
return 0;
}
bcopy((void *) packet.arp.arp_spa, (void *) &senderAddr,
sizeof(senderAddr));
bcopy((void *) packet.arp.arp_tpa, (void *) &targetAddr,
sizeof(targetAddr));
if(arpverbose) {
printf("ARP request: address 0x%x\n", targetAddr);
}
#ifdef SIM_PROXY
if(targetAddr == senderAddr)
return(0);
#endif
if(!FindArpReply(targetAddr, eaddr))
return 0;
if(arpverbose) {
char *c;
c = eaddr;
printf("ARP request: address 0x%x, hardware %x:%x:%x:%x:%x:%x\n", targetAddr,
c[0],c[1],c[2],c[3],c[4],c[5]);
}
/*
* Reverse sender and target fields, and respond with the appropriate
* Ethernet address. No need to fill in the source in the packet
* header: the kernel automatically overwrites it.
*/
bcopy((void *) &targetAddr, (void *) packet.arp.arp_spa,
sizeof(targetAddr));
bcopy((void *) &senderAddr, (void *) packet.arp.arp_tpa,
sizeof(senderAddr));
bcopy((void *) &packet.arp.arp_sha, (void *) &packet.arp.arp_tha,
6);
bcopy((void *) &eaddr, (void *) &packet.arp.arp_sha,
6);
packet.arp.ea_hdr.ar_op = htons(ARPOP_REPLY);
bcopy((void *) &packet.hdr.ether_shost, (void *) &packet.hdr.ether_dhost,
6);
bcopy((void *) &packet, pout, ARP_PACKET_SIZE);
(*poutlen) = ARP_PACKET_SIZE;
return 1;
}
static int
FindArpReply(unsigned long dstipaddr, u_char *etherdst)
{
FILE *pfd;
unsigned int gateway;
int found;
gateway = FindGateway(dstipaddr);
if (gateway == (unsigned int ) -1) {
return 0;
}
#ifdef sparc
pfd = popen("/etc/arp -a", "r");
#endif
#ifdef sgi
pfd = popen("/usr/etc/arp -a", "r");
#endif
if (pfd == (FILE *) NULL) {
return 0;
}
found = 0;
while(1) {
unsigned int ip1,ip2,ip3,ip4,e1,e2,e3,e4,e5,e6;
struct in_addr *ia = (struct in_addr *) &gateway;
int n;
char name[256], eaddrstring[32];
n = fscanf(pfd, "%s (%d.%d.%d.%d) at %s\n",
name, &ip1,&ip2,&ip3,&ip4, eaddrstring);
if (n < 0)
break;
if (n != 6)
continue;
#ifdef sparc
if ( (ip1 == ia->S_un.S_un_b.s_b1) &&
(ip2 == ia->S_un.S_un_b.s_b2) &&
(ip3 == ia->S_un.S_un_b.s_b3) &&
(ip4 == ia->S_un.S_un_b.s_b4) ) {
u_char *e = ether_aton(eaddrstring);
if (e != NULL) {
*etherdst = *e;
found = 1;
}
break;
}
#endif /* sparc */
#ifdef sgi
if ( ((( ip1*256+ip2)*256+ip3)*256+ip4) == ia->s_addr){
struct ether_addr *e = ether_aton(eaddrstring);
if (e != NULL) {
bcopy((void *)e, etherdst, 6);
found = 1;
printf("arp -a found: %s\n",eaddrstring);
}
break;
}
#endif /* sgi */
}
pclose(pfd);
return found;
}