rarpd.c revision 1.1 1 1.1 deraadt /*
2 1.1 deraadt * Copyright (c) 1990 The Regents of the University of California.
3 1.1 deraadt * All rights reserved.
4 1.1 deraadt *
5 1.1 deraadt * Redistribution and use in source and binary forms, with or without
6 1.1 deraadt * modification, are permitted provided that: (1) source code distributions
7 1.1 deraadt * retain the above copyright notice and this paragraph in its entirety, (2)
8 1.1 deraadt * distributions including binary code include the above copyright notice and
9 1.1 deraadt * this paragraph in its entirety in the documentation or other materials
10 1.1 deraadt * provided with the distribution, and (3) all advertising materials mentioning
11 1.1 deraadt * features or use of this software display the following acknowledgement:
12 1.1 deraadt * ``This product includes software developed by the University of California,
13 1.1 deraadt * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 1.1 deraadt * the University nor the names of its contributors may be used to endorse
15 1.1 deraadt * or promote products derived from this software without specific prior
16 1.1 deraadt * written permission.
17 1.1 deraadt * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 1.1 deraadt * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 1.1 deraadt * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 1.1 deraadt */
21 1.1 deraadt #ifndef lint
22 1.1 deraadt char copyright[] =
23 1.1 deraadt "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
24 1.1 deraadt All rights reserved.\n";
25 1.1 deraadt #endif /* not lint */
26 1.1 deraadt
27 1.1 deraadt #ifndef lint
28 1.1 deraadt static char rcsid[] =
29 1.1 deraadt "@(#) $Id: rarpd.c,v 1.1 1993/12/16 05:31:08 deraadt Exp $";
30 1.1 deraadt #endif
31 1.1 deraadt
32 1.1 deraadt
33 1.1 deraadt /*
34 1.1 deraadt * rarpd - Reverse ARP Daemon
35 1.1 deraadt *
36 1.1 deraadt * Usage: rarpd -a [ -d -f ]
37 1.1 deraadt * rarpd [ -d -f ] interface
38 1.1 deraadt */
39 1.1 deraadt
40 1.1 deraadt #include <stdio.h>
41 1.1 deraadt #include <stdlib.h>
42 1.1 deraadt #include <syslog.h>
43 1.1 deraadt #include <string.h>
44 1.1 deraadt #include <strings.h>
45 1.1 deraadt #include <sys/types.h>
46 1.1 deraadt #include <unistd.h>
47 1.1 deraadt #include <sys/time.h>
48 1.1 deraadt #include <net/bpf.h>
49 1.1 deraadt #include <sys/socket.h>
50 1.1 deraadt #include <sys/ioctl.h>
51 1.1 deraadt #include <net/if.h>
52 1.1 deraadt #include <netinet/in.h>
53 1.1 deraadt #include <netinet/if_ether.h>
54 1.1 deraadt #include <sys/errno.h>
55 1.1 deraadt #include <sys/file.h>
56 1.1 deraadt #include <netdb.h>
57 1.1 deraadt #include <arpa/inet.h>
58 1.1 deraadt #include <dirent.h>
59 1.1 deraadt
60 1.1 deraadt #define FATAL 1 /* fatal error occurred */
61 1.1 deraadt #define NONFATAL 0 /* non fatal error occurred */
62 1.1 deraadt
63 1.1 deraadt /*
64 1.1 deraadt * The structure for each interface.
65 1.1 deraadt */
66 1.1 deraadt struct if_info {
67 1.1 deraadt int ii_fd; /* BPF file descriptor */
68 1.1 deraadt u_char ii_eaddr[6]; /* Ethernet address of this interface */
69 1.1 deraadt u_long ii_ipaddr; /* IP address of this interface */
70 1.1 deraadt u_long ii_netmask; /* subnet or net mask */
71 1.1 deraadt struct if_info *ii_next;
72 1.1 deraadt };
73 1.1 deraadt /*
74 1.1 deraadt * The list of all interfaces that are being listened to. rarp_loop()
75 1.1 deraadt * "selects" on the descriptors in this list.
76 1.1 deraadt */
77 1.1 deraadt struct if_info *iflist;
78 1.1 deraadt
79 1.1 deraadt int rarp_open __P((char *));
80 1.1 deraadt int rarp_bootable __P((u_long));
81 1.1 deraadt char *intoa __P((u_long));
82 1.1 deraadt void init_one __P((char *));
83 1.1 deraadt void init_all __P((void));
84 1.1 deraadt void rarp_loop __P((void));
85 1.1 deraadt void lookup_eaddr __P((char *, u_char *));
86 1.1 deraadt void lookup_ipaddr __P((char *, u_long *, u_long *));
87 1.1 deraadt void usage __P((void));
88 1.1 deraadt void rarp_process __P((struct if_info *, u_char *));
89 1.1 deraadt void rarp_reply __P((struct if_info *, struct ether_header *, u_long));
90 1.1 deraadt void update_arptab __P((u_char *, u_long));
91 1.1 deraadt void err __P((int, const char *,...));
92 1.1 deraadt void debug __P((const char *,...));
93 1.1 deraadt u_long ipaddrtonetmask __P((u_long));
94 1.1 deraadt
95 1.1 deraadt int aflag = 0; /* listen on "all" interfaces */
96 1.1 deraadt int dflag = 0; /* print debugging messages */
97 1.1 deraadt int fflag = 0; /* don't fork */
98 1.1 deraadt
99 1.1 deraadt void
100 1.1 deraadt main(argc, argv)
101 1.1 deraadt int argc;
102 1.1 deraadt char **argv;
103 1.1 deraadt {
104 1.1 deraadt int op, pid, devnull, f;
105 1.1 deraadt char *ifname, *hostname, *name;
106 1.1 deraadt
107 1.1 deraadt extern char *optarg;
108 1.1 deraadt extern int optind, opterr;
109 1.1 deraadt
110 1.1 deraadt if (name = strrchr(argv[0], '/'))
111 1.1 deraadt ++name;
112 1.1 deraadt else
113 1.1 deraadt name = argv[0];
114 1.1 deraadt if (*name == '-')
115 1.1 deraadt ++name;
116 1.1 deraadt
117 1.1 deraadt /* All error reporting is done through syslogs. */
118 1.1 deraadt openlog(name, LOG_PID | LOG_CONS, LOG_DAEMON);
119 1.1 deraadt
120 1.1 deraadt opterr = 0;
121 1.1 deraadt while ((op = getopt(argc, argv, "adf")) != EOF) {
122 1.1 deraadt switch (op) {
123 1.1 deraadt case 'a':
124 1.1 deraadt ++aflag;
125 1.1 deraadt break;
126 1.1 deraadt
127 1.1 deraadt case 'd':
128 1.1 deraadt ++dflag;
129 1.1 deraadt break;
130 1.1 deraadt
131 1.1 deraadt case 'f':
132 1.1 deraadt ++fflag;
133 1.1 deraadt break;
134 1.1 deraadt
135 1.1 deraadt default:
136 1.1 deraadt usage();
137 1.1 deraadt /* NOTREACHED */
138 1.1 deraadt }
139 1.1 deraadt }
140 1.1 deraadt ifname = argv[optind++];
141 1.1 deraadt hostname = ifname ? argv[optind] : 0;
142 1.1 deraadt if ((aflag && ifname) || (!aflag && ifname == 0))
143 1.1 deraadt usage();
144 1.1 deraadt
145 1.1 deraadt if (aflag)
146 1.1 deraadt init_all();
147 1.1 deraadt else
148 1.1 deraadt init_one(ifname);
149 1.1 deraadt
150 1.1 deraadt if ((!fflag) && (!dflag)) {
151 1.1 deraadt pid = fork();
152 1.1 deraadt if (pid > 0)
153 1.1 deraadt /* Parent exits, leaving child in background. */
154 1.1 deraadt exit(0);
155 1.1 deraadt else
156 1.1 deraadt if (pid == -1) {
157 1.1 deraadt err(FATAL, "cannot fork");
158 1.1 deraadt /* NOTREACHED */
159 1.1 deraadt }
160 1.1 deraadt /* Fade into the background */
161 1.1 deraadt f = open("/dev/tty", O_RDWR);
162 1.1 deraadt if (f >= 0) {
163 1.1 deraadt if (ioctl(f, TIOCNOTTY, 0) < 0) {
164 1.1 deraadt err(FATAL, "TIOCNOTTY: %s", strerror(errno));
165 1.1 deraadt /* NOTREACHED */
166 1.1 deraadt }
167 1.1 deraadt (void) close(f);
168 1.1 deraadt }
169 1.1 deraadt (void) chdir("/");
170 1.1 deraadt (void) setpgrp(0, getpid());
171 1.1 deraadt devnull = open("/dev/null", O_RDWR);
172 1.1 deraadt if (devnull >= 0) {
173 1.1 deraadt (void) dup2(devnull, 0);
174 1.1 deraadt (void) dup2(devnull, 1);
175 1.1 deraadt (void) dup2(devnull, 2);
176 1.1 deraadt if (devnull > 2)
177 1.1 deraadt (void) close(devnull);
178 1.1 deraadt }
179 1.1 deraadt }
180 1.1 deraadt rarp_loop();
181 1.1 deraadt }
182 1.1 deraadt /*
183 1.1 deraadt * Add 'ifname' to the interface list. Lookup its IP address and network
184 1.1 deraadt * mask and Ethernet address, and open a BPF file for it.
185 1.1 deraadt */
186 1.1 deraadt void
187 1.1 deraadt init_one(ifname)
188 1.1 deraadt char *ifname;
189 1.1 deraadt {
190 1.1 deraadt struct if_info *p;
191 1.1 deraadt
192 1.1 deraadt
193 1.1 deraadt p = (struct if_info *) malloc(sizeof(*p));
194 1.1 deraadt if (p == 0) {
195 1.1 deraadt err(FATAL, "malloc: %s", strerror(errno));
196 1.1 deraadt /* NOTREACHED */
197 1.1 deraadt }
198 1.1 deraadt p->ii_next = iflist;
199 1.1 deraadt iflist = p;
200 1.1 deraadt
201 1.1 deraadt p->ii_fd = rarp_open(ifname);
202 1.1 deraadt lookup_eaddr(ifname, p->ii_eaddr);
203 1.1 deraadt lookup_ipaddr(ifname, &p->ii_ipaddr, &p->ii_netmask);
204 1.1 deraadt }
205 1.1 deraadt /*
206 1.1 deraadt * Initialize all "candidate" interfaces that are in the system
207 1.1 deraadt * configuration list. A "candidate" is up, not loopback and not
208 1.1 deraadt * point to point.
209 1.1 deraadt */
210 1.1 deraadt void
211 1.1 deraadt init_all()
212 1.1 deraadt {
213 1.1 deraadt int fd;
214 1.1 deraadt int i, len;
215 1.1 deraadt struct ifreq ibuf[8], *ifrp;
216 1.1 deraadt struct ifconf ifc;
217 1.1 deraadt
218 1.1 deraadt if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
219 1.1 deraadt err(FATAL, "socket: %s", strerror(errno));
220 1.1 deraadt /* NOTREACHED */
221 1.1 deraadt }
222 1.1 deraadt ifc.ifc_len = sizeof ibuf;
223 1.1 deraadt ifc.ifc_buf = (caddr_t) ibuf;
224 1.1 deraadt if (ioctl(fd, SIOCGIFCONF, (char *) &ifc) < 0 ||
225 1.1 deraadt ifc.ifc_len < sizeof(struct ifreq)) {
226 1.1 deraadt err(FATAL, "SIOCGIFCONF: %s", strerror(errno));
227 1.1 deraadt /* NOTREACHED */
228 1.1 deraadt }
229 1.1 deraadt ifrp = ibuf;
230 1.1 deraadt ifrp = ifc.ifc_req;
231 1.1 deraadt for(i=0; i<ifc.ifc_len; i+=len, ifrp=(struct ifreq *)((caddr_t)ifrp+len)) {
232 1.1 deraadt len = sizeof ifrp->ifr_name + ifrp->ifr_addr.sa_len;
233 1.1 deraadt
234 1.1 deraadt if (ioctl(fd, SIOCGIFFLAGS, (char *) ifrp) < 0) {
235 1.1 deraadt err(FATAL, "SIOCGIFFLAGS: %s", strerror(errno));
236 1.1 deraadt /* NOTREACHED */
237 1.1 deraadt }
238 1.1 deraadt if ((ifrp->ifr_flags & IFF_UP) == 0 ||
239 1.1 deraadt ifrp->ifr_flags & IFF_LOOPBACK ||
240 1.1 deraadt ifrp->ifr_flags & IFF_POINTOPOINT)
241 1.1 deraadt continue;
242 1.1 deraadt init_one(ifrp->ifr_name);
243 1.1 deraadt }
244 1.1 deraadt (void) close(fd);
245 1.1 deraadt }
246 1.1 deraadt
247 1.1 deraadt void
248 1.1 deraadt usage()
249 1.1 deraadt {
250 1.1 deraadt (void) fprintf(stderr, "usage: rarpd -a [ -d -f ]\n");
251 1.1 deraadt (void) fprintf(stderr, " rarpd [ -d -f ] interface\n");
252 1.1 deraadt exit(1);
253 1.1 deraadt }
254 1.1 deraadt
255 1.1 deraadt static int
256 1.1 deraadt bpf_open()
257 1.1 deraadt {
258 1.1 deraadt int fd;
259 1.1 deraadt int n = 0;
260 1.1 deraadt char device[sizeof "/dev/bpf000"];
261 1.1 deraadt
262 1.1 deraadt /* Go through all the minors and find one that isn't in use. */
263 1.1 deraadt do {
264 1.1 deraadt (void) sprintf(device, "/dev/bpf%d", n++);
265 1.1 deraadt fd = open(device, O_RDWR);
266 1.1 deraadt } while (fd < 0 && errno == EBUSY);
267 1.1 deraadt
268 1.1 deraadt if (fd < 0) {
269 1.1 deraadt err(FATAL, "%s: %s", device, strerror(errno));
270 1.1 deraadt /* NOTREACHED */
271 1.1 deraadt }
272 1.1 deraadt return fd;
273 1.1 deraadt }
274 1.1 deraadt /*
275 1.1 deraadt * Open a BPF file and attach it to the interface named 'device'.
276 1.1 deraadt * Set immediate mode, and set a filter that accepts only RARP requests.
277 1.1 deraadt */
278 1.1 deraadt int
279 1.1 deraadt rarp_open(device)
280 1.1 deraadt char *device;
281 1.1 deraadt {
282 1.1 deraadt int fd;
283 1.1 deraadt struct ifreq ifr;
284 1.1 deraadt u_int dlt;
285 1.1 deraadt int immediate;
286 1.1 deraadt
287 1.1 deraadt static struct bpf_insn insns[] = {
288 1.1 deraadt BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 12),
289 1.1 deraadt BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ETHERTYPE_REVARP, 0, 3),
290 1.1 deraadt BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 20),
291 1.1 deraadt BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, REVARP_REQUEST, 0, 1),
292 1.1 deraadt BPF_STMT(BPF_RET | BPF_K, sizeof(struct ether_arp) +
293 1.1 deraadt sizeof(struct ether_header)),
294 1.1 deraadt BPF_STMT(BPF_RET | BPF_K, 0),
295 1.1 deraadt };
296 1.1 deraadt static struct bpf_program filter = {
297 1.1 deraadt sizeof insns / sizeof(insns[0]),
298 1.1 deraadt insns
299 1.1 deraadt };
300 1.1 deraadt
301 1.1 deraadt fd = bpf_open();
302 1.1 deraadt
303 1.1 deraadt /* Set immediate mode so packets are processed as they arrive. */
304 1.1 deraadt immediate = 1;
305 1.1 deraadt if (ioctl(fd, BIOCIMMEDIATE, &immediate) < 0) {
306 1.1 deraadt err(FATAL, "BIOCIMMEDIATE: %s", strerror(errno));
307 1.1 deraadt /* NOTREACHED */
308 1.1 deraadt }
309 1.1 deraadt (void) strncpy(ifr.ifr_name, device, sizeof ifr.ifr_name);
310 1.1 deraadt if (ioctl(fd, BIOCSETIF, (caddr_t) & ifr) < 0) {
311 1.1 deraadt err(FATAL, "BIOCSETIF: %s", strerror(errno));
312 1.1 deraadt /* NOTREACHED */
313 1.1 deraadt }
314 1.1 deraadt /* Check that the data link layer is an Ethernet; this code won't work
315 1.1 deraadt * with anything else. */
316 1.1 deraadt if (ioctl(fd, BIOCGDLT, (caddr_t) & dlt) < 0) {
317 1.1 deraadt err(FATAL, "BIOCGDLT: %s", strerror(errno));
318 1.1 deraadt /* NOTREACHED */
319 1.1 deraadt }
320 1.1 deraadt if (dlt != DLT_EN10MB) {
321 1.1 deraadt err(FATAL, "%s is not an ethernet", device);
322 1.1 deraadt /* NOTREACHED */
323 1.1 deraadt }
324 1.1 deraadt /* Set filter program. */
325 1.1 deraadt if (ioctl(fd, BIOCSETF, (caddr_t) & filter) < 0) {
326 1.1 deraadt err(FATAL, "BIOCSETF: %s", strerror(errno));
327 1.1 deraadt /* NOTREACHED */
328 1.1 deraadt }
329 1.1 deraadt return fd;
330 1.1 deraadt }
331 1.1 deraadt /*
332 1.1 deraadt * Perform various sanity checks on the RARP request packet. Return
333 1.1 deraadt * false on failure and log the reason.
334 1.1 deraadt */
335 1.1 deraadt static int
336 1.1 deraadt rarp_check(p, len)
337 1.1 deraadt u_char *p;
338 1.1 deraadt int len;
339 1.1 deraadt {
340 1.1 deraadt struct ether_header *ep = (struct ether_header *) p;
341 1.1 deraadt struct ether_arp *ap = (struct ether_arp *) (p + sizeof(*ep));
342 1.1 deraadt
343 1.1 deraadt (void) debug("got a packet");
344 1.1 deraadt
345 1.1 deraadt if (len < sizeof(*ep) + sizeof(*ap)) {
346 1.1 deraadt err(NONFATAL, "truncated request");
347 1.1 deraadt return 0;
348 1.1 deraadt }
349 1.1 deraadt /* XXX This test might be better off broken out... */
350 1.1 deraadt if (ntohs (ep->ether_type) != ETHERTYPE_REVARP ||
351 1.1 deraadt ntohs (ap->arp_hrd) != ARPHRD_ETHER ||
352 1.1 deraadt ntohs (ap->arp_op) != REVARP_REQUEST ||
353 1.1 deraadt ntohs (ap->arp_pro) != ETHERTYPE_IP ||
354 1.1 deraadt ap->arp_hln != 6 || ap->arp_pln != 4) {
355 1.1 deraadt err(NONFATAL, "request fails sanity check");
356 1.1 deraadt return 0;
357 1.1 deraadt }
358 1.1 deraadt if (bcmp((char *) &ep->ether_shost, (char *) &ap->arp_sha, 6) != 0) {
359 1.1 deraadt err(NONFATAL, "ether/arp sender address mismatch");
360 1.1 deraadt return 0;
361 1.1 deraadt }
362 1.1 deraadt if (bcmp((char *) &ap->arp_sha, (char *) &ap->arp_tha, 6) != 0) {
363 1.1 deraadt err(NONFATAL, "ether/arp target address mismatch");
364 1.1 deraadt return 0;
365 1.1 deraadt }
366 1.1 deraadt return 1;
367 1.1 deraadt }
368 1.1 deraadt
369 1.1 deraadt /*
370 1.1 deraadt * Loop indefinitely listening for RARP requests on the
371 1.1 deraadt * interfaces in 'iflist'.
372 1.1 deraadt */
373 1.1 deraadt void
374 1.1 deraadt rarp_loop()
375 1.1 deraadt {
376 1.1 deraadt u_char *buf, *bp, *ep;
377 1.1 deraadt int cc, fd;
378 1.1 deraadt fd_set fds, listeners;
379 1.1 deraadt int bufsize, maxfd = 0;
380 1.1 deraadt struct if_info *ii;
381 1.1 deraadt
382 1.1 deraadt if (iflist == 0) {
383 1.1 deraadt err(FATAL, "no interfaces");
384 1.1 deraadt /* NOTREACHED */
385 1.1 deraadt }
386 1.1 deraadt if (ioctl(iflist->ii_fd, BIOCGBLEN, (caddr_t) & bufsize) < 0) {
387 1.1 deraadt err(FATAL, "BIOCGBLEN: %s", strerror(errno));
388 1.1 deraadt /* NOTREACHED */
389 1.1 deraadt }
390 1.1 deraadt buf = (u_char *) malloc((unsigned) bufsize);
391 1.1 deraadt if (buf == 0) {
392 1.1 deraadt err(FATAL, "malloc: %s", strerror(errno));
393 1.1 deraadt /* NOTREACHED */
394 1.1 deraadt }
395 1.1 deraadt /*
396 1.1 deraadt * Find the highest numbered file descriptor for select().
397 1.1 deraadt * Initialize the set of descriptors to listen to.
398 1.1 deraadt */
399 1.1 deraadt FD_ZERO(&fds);
400 1.1 deraadt for (ii = iflist; ii; ii = ii->ii_next) {
401 1.1 deraadt FD_SET(ii->ii_fd, &fds);
402 1.1 deraadt if (ii->ii_fd > maxfd)
403 1.1 deraadt maxfd = ii->ii_fd;
404 1.1 deraadt }
405 1.1 deraadt while (1) {
406 1.1 deraadt listeners = fds;
407 1.1 deraadt if (select(maxfd + 1, &listeners, (struct fd_set *) 0,
408 1.1 deraadt (struct fd_set *) 0, (struct timeval *) 0) < 0) {
409 1.1 deraadt err(FATAL, "select: %s", strerror(errno));
410 1.1 deraadt /* NOTREACHED */
411 1.1 deraadt }
412 1.1 deraadt for (ii = iflist; ii; ii = ii->ii_next) {
413 1.1 deraadt fd = ii->ii_fd;
414 1.1 deraadt if (!FD_ISSET(fd, &listeners))
415 1.1 deraadt continue;
416 1.1 deraadt again:
417 1.1 deraadt cc = read(fd, (char *) buf, bufsize);
418 1.1 deraadt /* Don't choke when we get ptraced */
419 1.1 deraadt if (cc < 0 && errno == EINTR)
420 1.1 deraadt goto again;
421 1.1 deraadt /* Due to a SunOS bug, after 2^31 bytes, the file
422 1.1 deraadt * offset overflows and read fails with EINVAL. The
423 1.1 deraadt * lseek() to 0 will fix things. */
424 1.1 deraadt if (cc < 0) {
425 1.1 deraadt if (errno == EINVAL &&
426 1.1 deraadt (long) (lseek(fd, 0L, SEEK_CUR) + bufsize) < 0) {
427 1.1 deraadt (void) lseek(fd, 0, 0);
428 1.1 deraadt goto again;
429 1.1 deraadt }
430 1.1 deraadt err(FATAL, "read: %s", strerror(errno));
431 1.1 deraadt /* NOTREACHED */
432 1.1 deraadt }
433 1.1 deraadt /* Loop through the packet(s) */
434 1.1 deraadt #define bhp ((struct bpf_hdr *)bp)
435 1.1 deraadt bp = buf;
436 1.1 deraadt ep = bp + cc;
437 1.1 deraadt while (bp < ep) {
438 1.1 deraadt register int caplen, hdrlen;
439 1.1 deraadt
440 1.1 deraadt caplen = bhp->bh_caplen;
441 1.1 deraadt hdrlen = bhp->bh_hdrlen;
442 1.1 deraadt if (rarp_check(bp + hdrlen, caplen))
443 1.1 deraadt rarp_process(ii, bp + hdrlen);
444 1.1 deraadt bp += BPF_WORDALIGN(hdrlen + caplen);
445 1.1 deraadt }
446 1.1 deraadt }
447 1.1 deraadt }
448 1.1 deraadt }
449 1.1 deraadt #ifndef TFTP_DIR
450 1.1 deraadt #define TFTP_DIR "/tftpboot"
451 1.1 deraadt #endif
452 1.1 deraadt
453 1.1 deraadt /*
454 1.1 deraadt * True if this server can boot the host whose IP address is 'addr'.
455 1.1 deraadt * This check is made by looking in the tftp directory for the
456 1.1 deraadt * configuration file.
457 1.1 deraadt */
458 1.1 deraadt int
459 1.1 deraadt rarp_bootable(addr)
460 1.1 deraadt u_long addr;
461 1.1 deraadt {
462 1.1 deraadt register struct dirent *dent;
463 1.1 deraadt register DIR *d;
464 1.1 deraadt char ipname[9];
465 1.1 deraadt static DIR *dd = 0;
466 1.1 deraadt
467 1.1 deraadt (void) sprintf(ipname, "%08X", addr);
468 1.1 deraadt /* If directory is already open, rewind it. Otherwise, open it. */
469 1.1 deraadt if (d = dd)
470 1.1 deraadt rewinddir(d);
471 1.1 deraadt else {
472 1.1 deraadt if (chdir(TFTP_DIR) == -1) {
473 1.1 deraadt err(FATAL, "chdir: %s", strerror(errno));
474 1.1 deraadt /* NOTREACHED */
475 1.1 deraadt }
476 1.1 deraadt d = opendir(".");
477 1.1 deraadt if (d == 0) {
478 1.1 deraadt err(FATAL, "opendir: %s", strerror(errno));
479 1.1 deraadt /* NOTREACHED */
480 1.1 deraadt }
481 1.1 deraadt dd = d;
482 1.1 deraadt }
483 1.1 deraadt while (dent = readdir(d))
484 1.1 deraadt if (strncmp(dent->d_name, ipname, 8) == 0)
485 1.1 deraadt return 1;
486 1.1 deraadt return 0;
487 1.1 deraadt }
488 1.1 deraadt /*
489 1.1 deraadt * Given a list of IP addresses, 'alist', return the first address that
490 1.1 deraadt * is on network 'net'; 'netmask' is a mask indicating the network portion
491 1.1 deraadt * of the address.
492 1.1 deraadt */
493 1.1 deraadt u_long
494 1.1 deraadt choose_ipaddr(alist, net, netmask)
495 1.1 deraadt u_long **alist;
496 1.1 deraadt u_long net;
497 1.1 deraadt u_long netmask;
498 1.1 deraadt {
499 1.1 deraadt for (; *alist; ++alist) {
500 1.1 deraadt if ((**alist & netmask) == net)
501 1.1 deraadt return **alist;
502 1.1 deraadt }
503 1.1 deraadt return 0;
504 1.1 deraadt }
505 1.1 deraadt /*
506 1.1 deraadt * Answer the RARP request in 'pkt', on the interface 'ii'. 'pkt' has
507 1.1 deraadt * already been checked for validity. The reply is overlaid on the request.
508 1.1 deraadt */
509 1.1 deraadt void
510 1.1 deraadt rarp_process(ii, pkt)
511 1.1 deraadt struct if_info *ii;
512 1.1 deraadt u_char *pkt;
513 1.1 deraadt {
514 1.1 deraadt struct ether_header *ep;
515 1.1 deraadt struct hostent *hp;
516 1.1 deraadt u_long target_ipaddr;
517 1.1 deraadt char ename[256];
518 1.1 deraadt
519 1.1 deraadt ep = (struct ether_header *) pkt;
520 1.1 deraadt
521 1.1 deraadt if (ether_ntohost(ename, &ep->ether_shost) != 0 ||
522 1.1 deraadt (hp = gethostbyname(ename)) == 0)
523 1.1 deraadt return;
524 1.1 deraadt
525 1.1 deraadt /* Choose correct address from list. */
526 1.1 deraadt if (hp->h_addrtype != AF_INET) {
527 1.1 deraadt err(FATAL, "cannot handle non IP addresses");
528 1.1 deraadt /* NOTREACHED */
529 1.1 deraadt }
530 1.1 deraadt target_ipaddr = choose_ipaddr((u_long **) hp->h_addr_list,
531 1.1 deraadt ii->ii_ipaddr & ii->ii_netmask,
532 1.1 deraadt ii->ii_netmask);
533 1.1 deraadt
534 1.1 deraadt if (target_ipaddr == 0) {
535 1.1 deraadt err(NONFATAL, "cannot find %s on net %s\n",
536 1.1 deraadt ename, intoa(ii->ii_ipaddr & ii->ii_netmask));
537 1.1 deraadt return;
538 1.1 deraadt }
539 1.1 deraadt if (rarp_bootable(htonl(target_ipaddr)))
540 1.1 deraadt rarp_reply(ii, ep, target_ipaddr);
541 1.1 deraadt }
542 1.1 deraadt /*
543 1.1 deraadt * Lookup the ethernet address of the interface attached to the BPF
544 1.1 deraadt * file descriptor 'fd'; return it in 'eaddr'.
545 1.1 deraadt */
546 1.1 deraadt void
547 1.1 deraadt lookup_eaddr(ifname, eaddr)
548 1.1 deraadt char *ifname;
549 1.1 deraadt u_char *eaddr;
550 1.1 deraadt {
551 1.1 deraadt char inbuf[8192];
552 1.1 deraadt struct ifconf ifc;
553 1.1 deraadt struct ifreq *ifr;
554 1.1 deraadt struct in_addr in;
555 1.1 deraadt int fd;
556 1.1 deraadt int i, len;
557 1.1 deraadt
558 1.1 deraadt /* We cannot use SIOCGIFADDR on the BPF descriptor.
559 1.1 deraadt We must instead get all the interfaces with SIOCGIFCONF
560 1.1 deraadt and find the right one. */
561 1.1 deraadt
562 1.1 deraadt /* Use datagram socket to get Ethernet address. */
563 1.1 deraadt if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
564 1.1 deraadt err(FATAL, "socket: %s", strerror(errno));
565 1.1 deraadt /* NOTREACHED */
566 1.1 deraadt }
567 1.1 deraadt
568 1.1 deraadt ifc.ifc_len = sizeof inbuf;
569 1.1 deraadt ifc.ifc_buf = inbuf;
570 1.1 deraadt if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
571 1.1 deraadt err(FATAL, "lookup_eaddr: SIOGIFCONF: %s", strerror (errno));
572 1.1 deraadt /* NOTREACHED */
573 1.1 deraadt }
574 1.1 deraadt ifr = ifc.ifc_req;
575 1.1 deraadt for(i=0; i<ifc.ifc_len; i+=len, ifr=(struct ifreq *)((caddr_t)ifr+len)) {
576 1.1 deraadt len = sizeof ifr->ifr_name + ifr->ifr_addr.sa_len;
577 1.1 deraadt if (!strncmp (ifr->ifr_name, ifname, sizeof (ifr->ifr_name))) {
578 1.1 deraadt bcopy((char *) &ifr->ifr_addr.sa_data[0],
579 1.1 deraadt (char *) eaddr, 6);
580 1.1 deraadt return;
581 1.1 deraadt }
582 1.1 deraadt }
583 1.1 deraadt
584 1.1 deraadt err(FATAL, "lookup_eaddr: Never saw interface `%s'!", ifname);
585 1.1 deraadt }
586 1.1 deraadt /*
587 1.1 deraadt * Lookup the IP address and network mask of the interface named 'ifname'.
588 1.1 deraadt */
589 1.1 deraadt void
590 1.1 deraadt lookup_ipaddr(ifname, addrp, netmaskp)
591 1.1 deraadt char *ifname;
592 1.1 deraadt u_long *addrp;
593 1.1 deraadt u_long *netmaskp;
594 1.1 deraadt {
595 1.1 deraadt int fd;
596 1.1 deraadt struct ifreq ifr;
597 1.1 deraadt
598 1.1 deraadt /* Use datagram socket to get IP address. */
599 1.1 deraadt if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
600 1.1 deraadt err(FATAL, "socket: %s", strerror(errno));
601 1.1 deraadt /* NOTREACHED */
602 1.1 deraadt }
603 1.1 deraadt (void) strncpy(ifr.ifr_name, ifname, sizeof ifr.ifr_name);
604 1.1 deraadt if (ioctl(fd, SIOCGIFADDR, (char *) &ifr) < 0) {
605 1.1 deraadt err(FATAL, "SIOCGIFADDR: %s", strerror(errno));
606 1.1 deraadt /* NOTREACHED */
607 1.1 deraadt }
608 1.1 deraadt *addrp = ((struct sockaddr_in *) & ifr.ifr_addr)->sin_addr.s_addr;
609 1.1 deraadt if (ioctl(fd, SIOCGIFNETMASK, (char *) &ifr) < 0) {
610 1.1 deraadt perror("SIOCGIFNETMASK");
611 1.1 deraadt exit(1);
612 1.1 deraadt }
613 1.1 deraadt *netmaskp = ((struct sockaddr_in *) & ifr.ifr_addr)->sin_addr.s_addr;
614 1.1 deraadt /* If SIOCGIFNETMASK didn't work, figure out a mask from the IP
615 1.1 deraadt * address class. */
616 1.1 deraadt if (*netmaskp == 0)
617 1.1 deraadt *netmaskp = ipaddrtonetmask(*addrp);
618 1.1 deraadt
619 1.1 deraadt (void) close(fd);
620 1.1 deraadt }
621 1.1 deraadt /*
622 1.1 deraadt * Poke the kernel arp tables with the ethernet/ip address combinataion
623 1.1 deraadt * given. When processing a reply, we must do this so that the booting
624 1.1 deraadt * host (i.e. the guy running rarpd), won't try to ARP for the hardware
625 1.1 deraadt * address of the guy being booted (he cannot answer the ARP).
626 1.1 deraadt */
627 1.1 deraadt void
628 1.1 deraadt update_arptab(ep, ipaddr)
629 1.1 deraadt u_char *ep;
630 1.1 deraadt u_long ipaddr;
631 1.1 deraadt {
632 1.1 deraadt int s;
633 1.1 deraadt struct arpreq request;
634 1.1 deraadt struct sockaddr_in *sin;
635 1.1 deraadt
636 1.1 deraadt request.arp_flags = 0;
637 1.1 deraadt sin = (struct sockaddr_in *) & request.arp_pa;
638 1.1 deraadt sin->sin_family = AF_INET;
639 1.1 deraadt sin->sin_addr.s_addr = ipaddr;
640 1.1 deraadt request.arp_ha.sa_family = AF_UNSPEC;
641 1.1 deraadt /* This is needed #if defined(COMPAT_43) && BYTE_ORDER != BIG_ENDIAN,
642 1.1 deraadt because AF_UNSPEC is zero and the kernel assumes that a zero
643 1.1 deraadt sa_family means that the real sa_family value is in sa_len. */
644 1.1 deraadt request.arp_ha.sa_len = 16; /* XXX */
645 1.1 deraadt bcopy((char *) ep, (char *) request.arp_ha.sa_data, 6);
646 1.1 deraadt
647 1.1 deraadt s = socket(AF_INET, SOCK_DGRAM, 0);
648 1.1 deraadt if (ioctl(s, SIOCSARP, (caddr_t) & request) < 0) {
649 1.1 deraadt err(NONFATAL, "SIOCSARP: %s", strerror(errno));
650 1.1 deraadt }
651 1.1 deraadt (void) close(s);
652 1.1 deraadt }
653 1.1 deraadt /*
654 1.1 deraadt * Build a reverse ARP packet and sent it out on the interface.
655 1.1 deraadt * 'ep' points to a valid REVARP_REQUEST. The REVARP_REPLY is built
656 1.1 deraadt * on top of the request, then written to the network.
657 1.1 deraadt *
658 1.1 deraadt * RFC 903 defines the ether_arp fields as follows. The following comments
659 1.1 deraadt * are taken (more or less) straight from this document.
660 1.1 deraadt *
661 1.1 deraadt * REVARP_REQUEST
662 1.1 deraadt *
663 1.1 deraadt * arp_sha is the hardware address of the sender of the packet.
664 1.1 deraadt * arp_spa is undefined.
665 1.1 deraadt * arp_tha is the 'target' hardware address.
666 1.1 deraadt * In the case where the sender wishes to determine his own
667 1.1 deraadt * protocol address, this, like arp_sha, will be the hardware
668 1.1 deraadt * address of the sender.
669 1.1 deraadt * arp_tpa is undefined.
670 1.1 deraadt *
671 1.1 deraadt * REVARP_REPLY
672 1.1 deraadt *
673 1.1 deraadt * arp_sha is the hardware address of the responder (the sender of the
674 1.1 deraadt * reply packet).
675 1.1 deraadt * arp_spa is the protocol address of the responder (see the note below).
676 1.1 deraadt * arp_tha is the hardware address of the target, and should be the same as
677 1.1 deraadt * that which was given in the request.
678 1.1 deraadt * arp_tpa is the protocol address of the target, that is, the desired address.
679 1.1 deraadt *
680 1.1 deraadt * Note that the requirement that arp_spa be filled in with the responder's
681 1.1 deraadt * protocol is purely for convenience. For instance, if a system were to use
682 1.1 deraadt * both ARP and RARP, then the inclusion of the valid protocol-hardware
683 1.1 deraadt * address pair (arp_spa, arp_sha) may eliminate the need for a subsequent
684 1.1 deraadt * ARP request.
685 1.1 deraadt */
686 1.1 deraadt void
687 1.1 deraadt rarp_reply(ii, ep, ipaddr)
688 1.1 deraadt struct if_info *ii;
689 1.1 deraadt struct ether_header *ep;
690 1.1 deraadt u_long ipaddr;
691 1.1 deraadt {
692 1.1 deraadt int n;
693 1.1 deraadt struct ether_arp *ap = (struct ether_arp *) (ep + 1);
694 1.1 deraadt int len;
695 1.1 deraadt
696 1.1 deraadt update_arptab((u_char *) & ap->arp_sha, ipaddr);
697 1.1 deraadt
698 1.1 deraadt /* Build the rarp reply by modifying the rarp request in place. */
699 1.1 deraadt ep->ether_type = ETHERTYPE_REVARP;
700 1.1 deraadt ap->ea_hdr.ar_hrd = ARPHRD_ETHER;
701 1.1 deraadt ap->ea_hdr.ar_pro = htons (ETHERTYPE_IP);
702 1.1 deraadt ap->arp_op = htons (REVARP_REPLY);
703 1.1 deraadt
704 1.1 deraadt bcopy((char *) &ap->arp_sha, (char *) &ep->ether_dhost, 6);
705 1.1 deraadt bcopy((char *) ii->ii_eaddr, (char *) &ep->ether_shost, 6);
706 1.1 deraadt bcopy((char *) ii->ii_eaddr, (char *) &ap->arp_sha, 6);
707 1.1 deraadt
708 1.1 deraadt bcopy((char *) &ipaddr, (char *) ap->arp_tpa, 4);
709 1.1 deraadt /* Target hardware is unchanged. */
710 1.1 deraadt bcopy((char *) &ii->ii_ipaddr, (char *) ap->arp_spa, 4);
711 1.1 deraadt
712 1.1 deraadt len = sizeof(*ep) + sizeof(*ap);
713 1.1 deraadt n = write(ii->ii_fd, (char *) ep, len);
714 1.1 deraadt if (n != len) {
715 1.1 deraadt err(NONFATAL, "write: only %d of %d bytes written", n, len);
716 1.1 deraadt }
717 1.1 deraadt }
718 1.1 deraadt /*
719 1.1 deraadt * Get the netmask of an IP address. This routine is used if
720 1.1 deraadt * SIOCGIFNETMASK doesn't work.
721 1.1 deraadt */
722 1.1 deraadt u_long
723 1.1 deraadt ipaddrtonetmask(addr)
724 1.1 deraadt u_long addr;
725 1.1 deraadt {
726 1.1 deraadt if (IN_CLASSA(addr))
727 1.1 deraadt return IN_CLASSA_NET;
728 1.1 deraadt if (IN_CLASSB(addr))
729 1.1 deraadt return IN_CLASSB_NET;
730 1.1 deraadt if (IN_CLASSC(addr))
731 1.1 deraadt return IN_CLASSC_NET;
732 1.1 deraadt err(FATAL, "unknown IP address class: %08X", addr);
733 1.1 deraadt /* NOTREACHED */
734 1.1 deraadt }
735 1.1 deraadt /*
736 1.1 deraadt * A faster replacement for inet_ntoa().
737 1.1 deraadt */
738 1.1 deraadt char *
739 1.1 deraadt intoa(addr)
740 1.1 deraadt u_long addr;
741 1.1 deraadt {
742 1.1 deraadt register char *cp;
743 1.1 deraadt register u_int byte;
744 1.1 deraadt register int n;
745 1.1 deraadt static char buf[sizeof(".xxx.xxx.xxx.xxx")];
746 1.1 deraadt
747 1.1 deraadt cp = &buf[sizeof buf];
748 1.1 deraadt *--cp = '\0';
749 1.1 deraadt
750 1.1 deraadt n = 4;
751 1.1 deraadt do {
752 1.1 deraadt byte = addr & 0xff;
753 1.1 deraadt *--cp = byte % 10 + '0';
754 1.1 deraadt byte /= 10;
755 1.1 deraadt if (byte > 0) {
756 1.1 deraadt *--cp = byte % 10 + '0';
757 1.1 deraadt byte /= 10;
758 1.1 deraadt if (byte > 0)
759 1.1 deraadt *--cp = byte + '0';
760 1.1 deraadt }
761 1.1 deraadt *--cp = '.';
762 1.1 deraadt addr >>= 8;
763 1.1 deraadt } while (--n > 0);
764 1.1 deraadt
765 1.1 deraadt return cp + 1;
766 1.1 deraadt }
767 1.1 deraadt
768 1.1 deraadt
769 1.1 deraadt #if __STDC__
770 1.1 deraadt #include <stdarg.h>
771 1.1 deraadt #else
772 1.1 deraadt #include <varargs.h>
773 1.1 deraadt #endif
774 1.1 deraadt
775 1.1 deraadt void
776 1.1 deraadt #if __STDC__
777 1.1 deraadt err(int fatal, const char *fmt,...)
778 1.1 deraadt #else
779 1.1 deraadt err(fmt, va_alist)
780 1.1 deraadt int fatal;
781 1.1 deraadt char *fmt;
782 1.1 deraadt va_dcl
783 1.1 deraadt #endif
784 1.1 deraadt {
785 1.1 deraadt va_list ap;
786 1.1 deraadt #if __STDC__
787 1.1 deraadt va_start(ap, fmt);
788 1.1 deraadt #else
789 1.1 deraadt va_start(ap);
790 1.1 deraadt #endif
791 1.1 deraadt if (dflag) {
792 1.1 deraadt if (fatal)
793 1.1 deraadt (void) fprintf(stderr, "rarpd: error: ");
794 1.1 deraadt else
795 1.1 deraadt (void) fprintf(stderr, "rarpd: warning: ");
796 1.1 deraadt (void) vfprintf(stderr, fmt, ap);
797 1.1 deraadt (void) fprintf(stderr, "\n");
798 1.1 deraadt }
799 1.1 deraadt vsyslog(LOG_ERR, fmt, ap);
800 1.1 deraadt va_end(ap);
801 1.1 deraadt if (fatal)
802 1.1 deraadt exit(1);
803 1.1 deraadt /* NOTREACHED */
804 1.1 deraadt }
805 1.1 deraadt
806 1.1 deraadt void
807 1.1 deraadt #if __STDC__
808 1.1 deraadt debug(const char *fmt,...)
809 1.1 deraadt #else
810 1.1 deraadt debug(fmt, va_alist)
811 1.1 deraadt char *fmt;
812 1.1 deraadt va_dcl
813 1.1 deraadt #endif
814 1.1 deraadt {
815 1.1 deraadt va_list ap;
816 1.1 deraadt
817 1.1 deraadt if (dflag) {
818 1.1 deraadt #if __STDC__
819 1.1 deraadt va_start(ap, fmt);
820 1.1 deraadt #else
821 1.1 deraadt va_start(ap);
822 1.1 deraadt #endif
823 1.1 deraadt (void) fprintf(stderr, "rarpd: ");
824 1.1 deraadt (void) vfprintf(stderr, fmt, ap);
825 1.1 deraadt va_end(ap);
826 1.1 deraadt (void) fprintf(stderr, "\n");
827 1.1 deraadt }
828 1.1 deraadt }
829