Repository navigation
Expand file tree
/
Copy pathrarpd.c
More file actions
368 lines (314 loc) · 9.02 KB
/
Copy pathrarpd.c
File metadata and controls
368 lines (314 loc) · 9.02 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
rarpd.c
Created: Nov 12, 1992 by Philip Homburg
Changed: May 13, 1995 by Kees J. Bot
Rewrite to handle multiple ethernets.
Changed: Jul 18, 1995 by Kees J. Bot
Do RARP requests (formerly inet's job)
Changed: Dec 14, 1996 by Kees J. Bot
Query the netmask
Changed: Dec 11, 2000 by Kees J. Bot
Dressed down to be only a RARP server, giving the floor to DHCP
*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/asynchio.h>
#include <net/hton.h>
#include <net/gen/socket.h>
#include <netdb.h>
#include <net/gen/in.h>
#include <net/gen/inet.h>
#include <net/gen/ether.h>
#include <net/gen/eth_io.h>
#include <net/gen/if_ether.h>
#include <net/gen/ip_io.h>
#include <arpa/nameser.h>
#define MAX_RARP_RETRIES 5
#define RARP_TIMEOUT 5
#undef HTONS
#define HTONS htons
typedef struct rarp46
{
ether_addr_t a46_dstaddr;
ether_addr_t a46_srcaddr;
ether_type_t a46_ethtype;
u16_t a46_hdr, a46_pro;
u8_t a46_hln, a46_pln;
u16_t a46_op;
ether_addr_t a46_sha;
u8_t a46_spa[4];
ether_addr_t a46_tha;
u8_t a46_tpa[4];
char a46_padding[ETH_MIN_PACK_SIZE - (4*6 + 2*4 + 4*2 + 2*1)];
} rarp46_t;
#define ETH_RARP_PROTO 0x8035
#define RARP_ETHERNET 1
#define RARP_REQUEST 3
#define RARP_REPLY 4
static char *program;
static unsigned debug;
#define between(a, c, z) ((unsigned) (c) - (a) <= (unsigned) (z) - (a))
static void report(const char *label)
{
fprintf(stderr, "%s: %s: %s\n", program, label, strerror(errno));
}
static void fatal(const char *label)
{
report(label);
exit(1);
}
static void *allocate(size_t size)
{
void *mem;
if ((mem= malloc(size)) == NULL) fatal("Can't allocate memory");
return mem;
}
static char *ethdev(int n)
{
static char an_ethdev[]= "/dev/ethNNN";
sprintf(an_ethdev + sizeof(an_ethdev)-4, "%d", n);
return an_ethdev;
}
static char *ipdev(int n)
{
static char an_ipdev[]= "/dev/ipNNN";
sprintf(an_ipdev + sizeof(an_ipdev)-4, "%d", n);
return an_ipdev;
}
typedef struct ethernet {
int n; /* Network number. */
int eth_fd; /* Open low level ethernet device. */
ether_addr_t eth_addr; /* Ethernet address of this net. */
char packet[ETH_MAX_PACK_SIZE]; /* Incoming packet. */
ipaddr_t ip_addr; /* IP address of this net. */
ipaddr_t ip_mask; /* Associated netmask. */
} ethernet_t;
static ethernet_t *ethernets;
static void onsig(int sig)
{
switch (sig) {
case SIGUSR1: debug++; break;
case SIGUSR2: debug= 0; break;
}
}
static void rarp_reply(ethernet_t *ep, char *hostname, ipaddr_t ip_addr,
ether_addr_t eth_addr)
{
rarp46_t rarp46;
/* Construct a RARP reply packet and send it. */
rarp46.a46_dstaddr= eth_addr;
rarp46.a46_hdr= HTONS(RARP_ETHERNET);
rarp46.a46_pro= HTONS(ETH_IP_PROTO);
rarp46.a46_hln= 6;
rarp46.a46_pln= 4;
rarp46.a46_op= HTONS(RARP_REPLY);
rarp46.a46_sha= ep->eth_addr;
memcpy(rarp46.a46_spa, &ep->ip_addr, sizeof(ipaddr_t));
rarp46.a46_tha= eth_addr;
memcpy(rarp46.a46_tpa, &ip_addr, sizeof(ipaddr_t));
if (debug >= 1) {
printf("%s: Replying %s (%s) to %s\n",
ethdev(ep->n), inet_ntoa(ip_addr), hostname, ether_ntoa(ð_addr));
}
(void) write(ep->eth_fd, &rarp46, sizeof(rarp46));
}
static int addhostname(char *addname, char *hostname, int n)
{
/* Create an additional hostname for a given hostname by adding "-n" to
* the first part. E.g. given "wombat.cs.vu.nl" and n=2 return
* "wombat-2.cs.vu.nl". This is useful for VU practical work where
* people get a few extra ethernet addresses on a machine and are asked
* to build a TCP/IP stack on it.
*/
char *dot;
if (strlen(hostname) + 4 >= 1024) return 0;
if ((dot= strchr(hostname, '.')) == NULL) dot= strchr(hostname, 0);
sprintf(addname, "%.*s-%d%s", (dot - hostname), hostname, n, dot);
return 1;
}
static void usage(void)
{
fprintf(stderr, "Usage: %s [-d[level]] network-name ...\n", program);
exit(1);
}
static int ifname2n(const char *name)
{
/* Translate an interface name, ip0, ip1, etc, to a number. */
const char *np;
char *end;
unsigned long n;
np= name;
if (*np++ != 'i' || *np++ != 'p') usage();
n= strtoul(np, &end, 10);
if (end == np || *end != 0) usage();
if (n >= 1000) {
fprintf(stderr, "%s: Network number of \"%s\" is a bit large\n",
program, name);
exit(1);
}
return n;
}
int main(int argc, char **argv)
{
int i;
ethernet_t *ep;
nwio_ethopt_t ethopt;
nwio_ethstat_t ethstat;
char hostname[1024];
struct hostent *hostent;
struct sigaction sa;
nwio_ipconf_t ipconf;
asynchio_t asyn;
ssize_t n;
ipaddr_t ip_addr;
rarp46_t rarp46;
int fd;
int n_eths;
program= argv[0];
asyn_init(&asyn);
debug= 0;
i= 1;
while (i < argc && argv[i][0] == '-') {
char *opt= argv[i++]+1;
if (opt[0] == '-' && opt[1] == 0) break; /* -- */
while (*opt != 0) switch (*opt++) {
case 'd':
debug= 1;
if (between('0', *opt, '9')) debug= strtoul(opt, &opt, 10);
break;
default:
usage();
}
}
if ((n_eths= (argc - i)) == 0) usage();
#if __minix_vmd
/* Minix-vmd can handle all nets at once using async I/O. */
ethernets= allocate(n_eths * sizeof(ethernets[0]));
for (i= 0; i < n_eths; i++) {
ethernets[i].n= ifname2n(argv[argc - n_eths + i]);
}
#else
/* Minix forks n-1 times to handle each net in a process each. */
for (i= 0; i < n_eths; i++) {
if (i+1 < n_eths) {
switch (fork()) {
case -1: fatal("fork()");
case 0: break;
default: continue;
}
}
ethernets= allocate(1 * sizeof(ethernets[0]));
ethernets[0].n= ifname2n(argv[argc - n_eths + i]);
}
n_eths= 1;
#endif
sa.sa_handler= onsig;
sigemptyset(&sa.sa_mask);
sa.sa_flags= 0;
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
for (i= 0; i < n_eths; i++) {
ep= ðernets[i];
if ((ep->eth_fd= open(ethdev(ep->n), O_RDWR)) < 0) fatal(ethdev(ep->n));
if (ioctl(ep->eth_fd, NWIOGETHSTAT, ðstat) < 0) {
fprintf(stderr, "%s: %s: Unable to get eth statistics: %s\n",
program, ethdev(ep->n), strerror(errno));
exit(1);
}
ep->eth_addr= ethstat.nwes_addr;
if (debug >= 1) {
printf("%s: Ethernet address is %s\n",
ethdev(ep->n), ether_ntoa(&ep->eth_addr));
}
ethopt.nweo_flags= NWEO_COPY | NWEO_EN_LOC | NWEO_EN_BROAD |
NWEO_TYPESPEC;
ethopt.nweo_type= HTONS(ETH_RARP_PROTO);
if (ioctl(ep->eth_fd, NWIOSETHOPT, ðopt) < 0) {
fprintf(stderr, "%s: %s: Unable to set eth options: %s\n",
program, ethdev(ep->n), strerror(errno));
exit(1);
}
/* What are my address and netmask? */
if ((fd= open(ipdev(ep->n), O_RDWR)) < 0) fatal(ipdev(ep->n));
if (ioctl(fd, NWIOGIPCONF, &ipconf) < 0) fatal(ipdev(ep->n));
ep->ip_addr= ipconf.nwic_ipaddr;
ep->ip_mask= ipconf.nwic_netmask;
close(fd);
if (debug >= 1) {
printf("%s: IP address is %s / ",
ipdev(ep->n), inet_ntoa(ep->ip_addr));
printf("%s\n", inet_ntoa(ep->ip_mask));
}
}
/* Wait for RARP requests, reply, repeat. */
for(;;) {
fflush(NULL);
/* Wait for a RARP request. */
for (i= 0; i < n_eths; i++) {
ep= ðernets[i];
n= asyn_read(&asyn, ep->eth_fd, ep->packet, sizeof(ep->packet));
if (n != -1) break;
if (errno != EINPROGRESS) {
report(ethdev(ep->n));
sleep(10);
}
}
/* RARP request? */
if (i < n_eths
&& n >= sizeof(rarp46)
&& (memcpy(&rarp46, ep->packet, sizeof(rarp46)), 1)
&& rarp46.a46_hdr == HTONS(RARP_ETHERNET)
&& rarp46.a46_pro == HTONS(ETH_IP_PROTO)
&& rarp46.a46_hln == 6
&& rarp46.a46_pln == 4
&& rarp46.a46_op == HTONS(RARP_REQUEST)
) {
if ((ether_ntohost(hostname, &rarp46.a46_tha) == 0
|| (rarp46.a46_tha.ea_addr[0] == 'v'
&& (memcpy(&ip_addr, rarp46.a46_tha.ea_addr+2, 4), 1)
&& (hostent= gethostbyaddr((char*) &ip_addr,
4, AF_INET)) != NULL
&& addhostname(hostname, hostent->h_name,
rarp46.a46_tha.ea_addr[1])))
&& (hostent= gethostbyname(hostname)) != NULL
&& hostent->h_addrtype == AF_INET
) {
/* Host is found in the ethers file and the DNS, or the
* ethernet address denotes a special additional address
* used for implementing a TCP/IP stack in user space.
*/
for (i= 0; hostent->h_addr_list[i] != NULL; i++) {
memcpy(&ip_addr, hostent->h_addr_list[i], sizeof(ipaddr_t));
/* Check if the address is on this network. */
if (((ip_addr ^ ep->ip_addr) & ep->ip_mask) == 0) break;
}
if (hostent->h_addr_list[i] != NULL) {
rarp_reply(ep, hostname, ip_addr, rarp46.a46_tha);
} else {
if (debug >= 2) {
printf("%s: Host '%s' (%s) is on the wrong net\n",
ethdev(ep->n),
hostname, ether_ntoa(&rarp46.a46_tha));
}
}
} else {
if (debug >= 2) {
printf("%s: RARP request from unknown host '%s'\n",
ethdev(ep->n), ether_ntoa(&rarp46.a46_tha));
}
}
}
/* Wait for another request. */
if (asyn_wait(&asyn, 0, NULL) < 0) {
report("asyn_wait()");
sleep(10);
}
}
}