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