NFE - Change default RX ring size from 128 -> 256, Adjust moderation timer.
[dragonfly.git] / usr.sbin / rarpd / rarpd.c
blobb52f410b541b1decfe0bf321f9ec72da8833f811
1 /*
2 * Copyright (c) 1990, 1991, 1992, 1993, 1996
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution
12 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
13 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 * @(#) Copyright (c) 1990, 1991, 1992, 1993, 1996 The Regents of the University of California. All rights reserved.
17 * $FreeBSD: src/usr.sbin/rarpd/rarpd.c,v 1.41 2004/08/07 04:28:54 imp Exp $
18 * $DragonFly: src/usr.sbin/rarpd/rarpd.c,v 1.4 2004/12/18 22:48:05 swildner Exp $
22 * rarpd - Reverse ARP Daemon
24 * Usage: rarpd -a [-dfsv] [-t directory] [hostname]
25 * rarpd [-dfsv] [-t directory] interface [hostname]
27 * 'hostname' is optional solely for backwards compatibility with Sun's rarpd.
28 * Currently, the argument is ignored.
30 #include <sys/param.h>
31 #include <sys/file.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <sys/time.h>
36 #include <net/bpf.h>
37 #include <net/ethernet.h>
38 #include <net/if.h>
39 #include <net/if_types.h>
40 #include <net/if_dl.h>
41 #include <net/route.h>
43 #include <netinet/in.h>
44 #include <netinet/if_ether.h>
46 #include <arpa/inet.h>
48 #include <dirent.h>
49 #include <errno.h>
50 #include <ifaddrs.h>
51 #include <netdb.h>
52 #include <stdarg.h>
53 #include <stdio.h>
54 #include <string.h>
55 #include <syslog.h>
56 #include <stdlib.h>
57 #include <unistd.h>
59 /* Cast a struct sockaddr to a struct sockaddr_in */
60 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
62 #ifndef TFTP_DIR
63 #define TFTP_DIR "/tftpboot"
64 #endif
66 #define ARPSECS (20 * 60) /* as per code in netinet/if_ether.c */
67 #define REVARP_REQUEST ARPOP_REVREQUEST
68 #define REVARP_REPLY ARPOP_REVREPLY
71 * The structure for each interface.
73 struct if_info {
74 struct if_info *ii_next;
75 int ii_fd; /* BPF file descriptor */
76 in_addr_t ii_ipaddr; /* IP address */
77 in_addr_t ii_netmask; /* subnet or net mask */
78 u_char ii_eaddr[ETHER_ADDR_LEN]; /* ethernet address */
79 char ii_ifname[IF_NAMESIZE];
83 * The list of all interfaces that are being listened to. rarp_loop()
84 * "selects" on the descriptors in this list.
86 struct if_info *iflist;
88 int verbose; /* verbose messages */
89 const char *tftp_dir = TFTP_DIR; /* tftp directory */
91 int dflag; /* messages to stdout/stderr, not syslog(3) */
92 int sflag; /* ignore /tftpboot */
94 static u_char zero[6];
96 static int bpf_open(void);
97 static in_addr_t choose_ipaddr(in_addr_t **, in_addr_t, in_addr_t);
98 static char *eatoa(u_char *);
99 static int expand_syslog_m(const char *fmt, char **newfmt);
100 static void init(char *);
101 static void init_one(struct ifaddrs *, char *, int);
102 static char *intoa(in_addr_t);
103 static in_addr_t ipaddrtonetmask(in_addr_t);
104 static void logmsg(int, const char *, ...) __printflike(2, 3);
105 static int rarp_bootable(in_addr_t);
106 static int rarp_check(u_char *, u_int);
107 static void rarp_loop(void);
108 static int rarp_open(char *);
109 static void rarp_process(struct if_info *, u_char *, u_int);
110 static void rarp_reply(struct if_info *, struct ether_header *,
111 in_addr_t, u_int);
112 static void update_arptab(u_char *, in_addr_t);
113 static void usage(void);
116 main(int argc, char *argv[])
118 int op;
119 char *ifname, *hostname, *name;
121 int aflag = 0; /* listen on "all" interfaces */
122 int fflag = 0; /* don't fork */
124 if ((name = strrchr(argv[0], '/')) != NULL)
125 ++name;
126 else
127 name = argv[0];
128 if (*name == '-')
129 ++name;
132 * All error reporting is done through syslog, unless -d is specified
134 openlog(name, LOG_PID | LOG_CONS, LOG_DAEMON);
136 opterr = 0;
137 while ((op = getopt(argc, argv, "adfst:v")) != -1)
138 switch (op) {
139 case 'a':
140 ++aflag;
141 break;
143 case 'd':
144 ++dflag;
145 break;
147 case 'f':
148 ++fflag;
149 break;
151 case 's':
152 ++sflag;
153 break;
155 case 't':
156 tftp_dir = optarg;
157 break;
159 case 'v':
160 ++verbose;
161 break;
163 default:
164 usage();
165 /* NOTREACHED */
167 argc -= optind;
168 argv += optind;
170 ifname = (aflag == 0) ? argv[0] : NULL;
171 hostname = ifname ? argv[1] : argv[0];
173 if ((aflag && ifname) || (!aflag && ifname == NULL))
174 usage();
176 init(ifname);
178 if (!fflag) {
179 if (daemon(0,0)) {
180 logmsg(LOG_ERR, "cannot fork");
181 exit(1);
184 rarp_loop();
185 return(0);
189 * Add to the interface list.
191 static void
192 init_one(struct ifaddrs *ifa, char *target, int pass1)
194 struct if_info *ii, *ii2;
195 struct sockaddr_dl *ll;
196 int family;
198 family = ifa->ifa_addr->sa_family;
199 switch (family) {
200 case AF_INET:
201 if (pass1)
202 /* Consider only AF_LINK during pass1. */
203 return;
204 /* FALLTHROUGH */
205 case AF_LINK:
206 if (!(ifa->ifa_flags & IFF_UP) ||
207 (ifa->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)))
208 return;
209 break;
210 default:
211 return;
214 /* Don't bother going any further if not the target interface */
215 if (target != NULL && strcmp(ifa->ifa_name, target) != 0)
216 return;
218 /* Look for interface in list */
219 for (ii = iflist; ii != NULL; ii = ii->ii_next)
220 if (strcmp(ifa->ifa_name, ii->ii_ifname) == 0)
221 break;
223 if (pass1 && ii != NULL)
224 /* We've already seen that interface once. */
225 return;
227 /* Allocate a new one if not found */
228 if (ii == NULL) {
229 ii = (struct if_info *)malloc(sizeof(*ii));
230 if (ii == NULL) {
231 logmsg(LOG_ERR, "malloc: %m");
232 exit(1);
234 bzero(ii, sizeof(*ii));
235 ii->ii_fd = -1;
236 strlcpy(ii->ii_ifname, ifa->ifa_name, sizeof(ii->ii_ifname));
237 ii->ii_next = iflist;
238 iflist = ii;
239 } else if (!pass1 && ii->ii_ipaddr != 0) {
241 * Second AF_INET definition for that interface: clone
242 * the existing one, and work on that cloned one.
243 * This must be another IP address for this interface,
244 * so avoid killing the previous configuration.
246 ii2 = (struct if_info *)malloc(sizeof(*ii2));
247 if (ii2 == NULL) {
248 logmsg(LOG_ERR, "malloc: %m");
249 exit(1);
251 memcpy(ii2, ii, sizeof(*ii2));
252 ii2->ii_fd = -1;
253 ii2->ii_next = iflist;
254 iflist = ii2;
256 ii = ii2;
259 switch (family) {
260 case AF_INET:
261 ii->ii_ipaddr = SATOSIN(ifa->ifa_addr)->sin_addr.s_addr;
262 ii->ii_netmask = SATOSIN(ifa->ifa_netmask)->sin_addr.s_addr;
263 if (ii->ii_netmask == 0)
264 ii->ii_netmask = ipaddrtonetmask(ii->ii_ipaddr);
265 if (ii->ii_fd < 0)
266 ii->ii_fd = rarp_open(ii->ii_ifname);
267 break;
269 case AF_LINK:
270 ll = (struct sockaddr_dl *)ifa->ifa_addr;
271 if (ll->sdl_type == IFT_ETHER)
272 bcopy(LLADDR(ll), ii->ii_eaddr, 6);
273 break;
277 * Initialize all "candidate" interfaces that are in the system
278 * configuration list. A "candidate" is up, not loopback and not
279 * point to point.
281 static void
282 init(char *target)
284 struct if_info *ii, *nii, *lii;
285 struct ifaddrs *ifhead, *ifa;
286 int error;
288 error = getifaddrs(&ifhead);
289 if (error) {
290 logmsg(LOG_ERR, "getifaddrs: %m");
291 exit(1);
294 * We make two passes over the list we have got. In the first
295 * one, we only collect AF_LINK interfaces, and initialize our
296 * list of interfaces from them. In the second pass, we
297 * collect the actual IP addresses from the AF_INET
298 * interfaces, and allow for the same interface name to appear
299 * multiple times (in case of more than one IP address).
301 for (ifa = ifhead; ifa != NULL; ifa = ifa->ifa_next)
302 init_one(ifa, target, 1);
303 for (ifa = ifhead; ifa != NULL; ifa = ifa->ifa_next)
304 init_one(ifa, target, 0);
305 freeifaddrs(ifhead);
307 /* Throw away incomplete interfaces */
308 lii = NULL;
309 for (ii = iflist; ii != NULL; ii = nii) {
310 nii = ii->ii_next;
311 if (ii->ii_ipaddr == 0 ||
312 bcmp(ii->ii_eaddr, zero, 6) == 0) {
313 if (lii == NULL)
314 iflist = nii;
315 else
316 lii->ii_next = nii;
317 if (ii->ii_fd >= 0)
318 close(ii->ii_fd);
319 free(ii);
320 continue;
322 lii = ii;
325 /* Verbose stuff */
326 if (verbose)
327 for (ii = iflist; ii != NULL; ii = ii->ii_next)
328 logmsg(LOG_DEBUG, "%s %s 0x%08x %s",
329 ii->ii_ifname, intoa(ntohl(ii->ii_ipaddr)),
330 (in_addr_t)ntohl(ii->ii_netmask), eatoa(ii->ii_eaddr));
333 static void
334 usage(void)
336 fprintf(stderr, "%s\n%s\n",
337 "usage: rarpd -a [-dfsv] [-t directory]",
338 " rarpd [-dfsv] [-t directory] interface");
339 exit(1);
342 static int
343 bpf_open(void)
345 int fd;
346 int n = 0;
347 char device[sizeof "/dev/bpf000"];
350 * Go through all the minors and find one that isn't in use.
352 do {
353 sprintf(device, "/dev/bpf%d", n++);
354 fd = open(device, O_RDWR);
355 } while ((fd == -1) && (errno == EBUSY));
357 if (fd == -1) {
358 logmsg(LOG_ERR, "%s: %m", device);
359 exit(1);
361 return fd;
365 * Open a BPF file and attach it to the interface named 'device'.
366 * Set immediate mode, and set a filter that accepts only RARP requests.
368 static int
369 rarp_open(char *device)
371 int fd;
372 struct ifreq ifr;
373 u_int dlt;
374 int immediate;
376 static struct bpf_insn insns[] = {
377 BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 12),
378 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, ETHERTYPE_REVARP, 0, 3),
379 BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 20),
380 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, REVARP_REQUEST, 0, 1),
381 BPF_STMT(BPF_RET|BPF_K, sizeof(struct ether_arp) +
382 sizeof(struct ether_header)),
383 BPF_STMT(BPF_RET|BPF_K, 0),
385 static struct bpf_program filter = {
386 sizeof insns / sizeof(insns[0]),
387 insns
390 fd = bpf_open();
392 * Set immediate mode so packets are processed as they arrive.
394 immediate = 1;
395 if (ioctl(fd, BIOCIMMEDIATE, &immediate) == -1) {
396 logmsg(LOG_ERR, "BIOCIMMEDIATE: %m");
397 exit(1);
399 strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
400 if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) == -1) {
401 logmsg(LOG_ERR, "BIOCSETIF: %m");
402 exit(1);
405 * Check that the data link layer is an Ethernet; this code won't
406 * work with anything else.
408 if (ioctl(fd, BIOCGDLT, (caddr_t)&dlt) == -1) {
409 logmsg(LOG_ERR, "BIOCGDLT: %m");
410 exit(1);
412 if (dlt != DLT_EN10MB) {
413 logmsg(LOG_ERR, "%s is not an ethernet", device);
414 exit(1);
417 * Set filter program.
419 if (ioctl(fd, BIOCSETF, (caddr_t)&filter) == -1) {
420 logmsg(LOG_ERR, "BIOCSETF: %m");
421 exit(1);
423 return fd;
427 * Perform various sanity checks on the RARP request packet. Return
428 * false on failure and log the reason.
430 static int
431 rarp_check(u_char *p, u_int len)
433 struct ether_header *ep = (struct ether_header *)p;
434 struct ether_arp *ap = (struct ether_arp *)(p + sizeof(*ep));
436 if (len < sizeof(*ep) + sizeof(*ap)) {
437 logmsg(LOG_ERR, "truncated request, got %u, expected %lu",
438 len, (u_long)(sizeof(*ep) + sizeof(*ap)));
439 return 0;
442 * XXX This test might be better off broken out...
444 if (ntohs(ep->ether_type) != ETHERTYPE_REVARP ||
445 ntohs(ap->arp_hrd) != ARPHRD_ETHER ||
446 ntohs(ap->arp_op) != REVARP_REQUEST ||
447 ntohs(ap->arp_pro) != ETHERTYPE_IP ||
448 ap->arp_hln != 6 || ap->arp_pln != 4) {
449 logmsg(LOG_DEBUG, "request fails sanity check");
450 return 0;
452 if (bcmp((char *)&ep->ether_shost, (char *)&ap->arp_sha, 6) != 0) {
453 logmsg(LOG_DEBUG, "ether/arp sender address mismatch");
454 return 0;
456 if (bcmp((char *)&ap->arp_sha, (char *)&ap->arp_tha, 6) != 0) {
457 logmsg(LOG_DEBUG, "ether/arp target address mismatch");
458 return 0;
460 return 1;
464 * Loop indefinitely listening for RARP requests on the
465 * interfaces in 'iflist'.
467 static void
468 rarp_loop(void)
470 u_char *buf, *bp, *ep;
471 int cc, fd;
472 fd_set fds, listeners;
473 int bufsize, maxfd = 0;
474 struct if_info *ii;
476 if (iflist == NULL) {
477 logmsg(LOG_ERR, "no interfaces");
478 exit(1);
480 if (ioctl(iflist->ii_fd, BIOCGBLEN, (caddr_t)&bufsize) == -1) {
481 logmsg(LOG_ERR, "BIOCGBLEN: %m");
482 exit(1);
484 buf = malloc(bufsize);
485 if (buf == NULL) {
486 logmsg(LOG_ERR, "malloc: %m");
487 exit(1);
490 while (1) {
492 * Find the highest numbered file descriptor for select().
493 * Initialize the set of descriptors to listen to.
495 FD_ZERO(&fds);
496 for (ii = iflist; ii != NULL; ii = ii->ii_next) {
497 FD_SET(ii->ii_fd, &fds);
498 if (ii->ii_fd > maxfd)
499 maxfd = ii->ii_fd;
501 listeners = fds;
502 if (select(maxfd + 1, &listeners, NULL, NULL, NULL) == -1) {
503 /* Don't choke when we get ptraced */
504 if (errno == EINTR)
505 continue;
506 logmsg(LOG_ERR, "select: %m");
507 exit(1);
509 for (ii = iflist; ii != NULL; ii = ii->ii_next) {
510 fd = ii->ii_fd;
511 if (!FD_ISSET(fd, &listeners))
512 continue;
513 again:
514 cc = read(fd, (char *)buf, bufsize);
515 /* Don't choke when we get ptraced */
516 if ((cc == -1) && (errno == EINTR))
517 goto again;
519 /* Loop through the packet(s) */
520 #define bhp ((struct bpf_hdr *)bp)
521 bp = buf;
522 ep = bp + cc;
523 while (bp < ep) {
524 u_int caplen, hdrlen;
526 caplen = bhp->bh_caplen;
527 hdrlen = bhp->bh_hdrlen;
528 if (rarp_check(bp + hdrlen, caplen))
529 rarp_process(ii, bp + hdrlen, caplen);
530 bp += BPF_WORDALIGN(hdrlen + caplen);
534 #undef bhp
538 * True if this server can boot the host whose IP address is 'addr'.
539 * This check is made by looking in the tftp directory for the
540 * configuration file.
542 static int
543 rarp_bootable(in_addr_t addr)
545 struct dirent *dent;
546 DIR *d;
547 char ipname[9];
548 static DIR *dd = NULL;
550 sprintf(ipname, "%08X", (in_addr_t)ntohl(addr));
553 * If directory is already open, rewind it. Otherwise, open it.
555 if ((d = dd) != NULL)
556 rewinddir(d);
557 else {
558 if (chdir(tftp_dir) == -1) {
559 logmsg(LOG_ERR, "chdir: %s: %m", tftp_dir);
560 exit(1);
562 d = opendir(".");
563 if (d == NULL) {
564 logmsg(LOG_ERR, "opendir: %m");
565 exit(1);
567 dd = d;
569 while ((dent = readdir(d)) != NULL)
570 if (strncmp(dent->d_name, ipname, 8) == 0)
571 return 1;
572 return 0;
576 * Given a list of IP addresses, 'alist', return the first address that
577 * is on network 'net'; 'netmask' is a mask indicating the network portion
578 * of the address.
580 static in_addr_t
581 choose_ipaddr(in_addr_t **alist, in_addr_t net, in_addr_t netmask)
583 for (; *alist; ++alist)
584 if ((**alist & netmask) == net)
585 return **alist;
586 return 0;
590 * Answer the RARP request in 'pkt', on the interface 'ii'. 'pkt' has
591 * already been checked for validity. The reply is overlaid on the request.
593 static void
594 rarp_process(struct if_info *ii, u_char *pkt, u_int len)
596 struct ether_header *ep;
597 struct hostent *hp;
598 in_addr_t target_ipaddr;
599 char ename[256];
601 ep = (struct ether_header *)pkt;
602 /* should this be arp_tha? */
603 if (ether_ntohost(ename, (struct ether_addr *)&ep->ether_shost) != 0) {
604 logmsg(LOG_ERR, "cannot map %s to name",
605 eatoa(ep->ether_shost));
606 return;
609 if ((hp = gethostbyname(ename)) == NULL) {
610 logmsg(LOG_ERR, "cannot map %s to IP address", ename);
611 return;
615 * Choose correct address from list.
617 if (hp->h_addrtype != AF_INET) {
618 logmsg(LOG_ERR, "cannot handle non IP addresses for %s",
619 ename);
620 return;
622 target_ipaddr = choose_ipaddr((in_addr_t **)hp->h_addr_list,
623 ii->ii_ipaddr & ii->ii_netmask,
624 ii->ii_netmask);
625 if (target_ipaddr == 0) {
626 logmsg(LOG_ERR, "cannot find %s on net %s",
627 ename, intoa(ntohl(ii->ii_ipaddr & ii->ii_netmask)));
628 return;
630 if (sflag || rarp_bootable(target_ipaddr))
631 rarp_reply(ii, ep, target_ipaddr, len);
632 else if (verbose > 1)
633 logmsg(LOG_INFO, "%s %s at %s DENIED (not bootable)",
634 ii->ii_ifname,
635 eatoa(ep->ether_shost),
636 intoa(ntohl(target_ipaddr)));
640 * Poke the kernel arp tables with the ethernet/ip address combinataion
641 * given. When processing a reply, we must do this so that the booting
642 * host (i.e. the guy running rarpd), won't try to ARP for the hardware
643 * address of the guy being booted (he cannot answer the ARP).
645 struct sockaddr_inarp sin_inarp = {
646 sizeof(struct sockaddr_inarp), AF_INET, 0,
647 {0},
648 {0},
649 0, 0
651 struct sockaddr_dl sin_dl = {
652 sizeof(struct sockaddr_dl), AF_LINK, 0, IFT_ETHER, 0, 6,
653 0, ""
655 struct {
656 struct rt_msghdr rthdr;
657 char rtspace[512];
658 } rtmsg;
660 static void
661 update_arptab(u_char *ep, in_addr_t ipaddr)
663 int cc;
664 struct sockaddr_inarp *ar, *ar2;
665 struct sockaddr_dl *ll, *ll2;
666 struct rt_msghdr *rt;
667 int xtype, xindex;
668 static pid_t pid;
669 int r;
670 static int seq;
672 r = socket(PF_ROUTE, SOCK_RAW, 0);
673 if (r == -1) {
674 logmsg(LOG_ERR, "raw route socket: %m");
675 exit(1);
677 pid = getpid();
679 ar = &sin_inarp;
680 ar->sin_addr.s_addr = ipaddr;
681 ll = &sin_dl;
682 bcopy(ep, LLADDR(ll), 6);
684 /* Get the type and interface index */
685 rt = &rtmsg.rthdr;
686 bzero(rt, sizeof(rtmsg));
687 rt->rtm_version = RTM_VERSION;
688 rt->rtm_addrs = RTA_DST;
689 rt->rtm_type = RTM_GET;
690 rt->rtm_seq = ++seq;
691 ar2 = (struct sockaddr_inarp *)rtmsg.rtspace;
692 bcopy(ar, ar2, sizeof(*ar));
693 rt->rtm_msglen = sizeof(*rt) + sizeof(*ar);
694 errno = 0;
695 if ((write(r, rt, rt->rtm_msglen) == -1) && (errno != ESRCH)) {
696 logmsg(LOG_ERR, "rtmsg get write: %m");
697 close(r);
698 return;
700 do {
701 cc = read(r, rt, sizeof(rtmsg));
702 } while (cc > 0 && (rt->rtm_seq != seq || rt->rtm_pid != pid));
703 if (cc == -1) {
704 logmsg(LOG_ERR, "rtmsg get read: %m");
705 close(r);
706 return;
708 ll2 = (struct sockaddr_dl *)((u_char *)ar2 + ar2->sin_len);
709 if (ll2->sdl_family != AF_LINK) {
711 * XXX I think this means the ip address is not on a
712 * directly connected network (the family is AF_INET in
713 * this case).
715 logmsg(LOG_ERR, "bogus link family (%d) wrong net for %08X?\n",
716 ll2->sdl_family, ipaddr);
717 close(r);
718 return;
720 xtype = ll2->sdl_type;
721 xindex = ll2->sdl_index;
723 /* Set the new arp entry */
724 bzero(rt, sizeof(rtmsg));
725 rt->rtm_version = RTM_VERSION;
726 rt->rtm_addrs = RTA_DST | RTA_GATEWAY;
727 rt->rtm_inits = RTV_EXPIRE;
728 rt->rtm_rmx.rmx_expire = time(0) + ARPSECS;
729 rt->rtm_flags = RTF_HOST | RTF_STATIC;
730 rt->rtm_type = RTM_ADD;
731 rt->rtm_seq = ++seq;
733 bcopy(ar, ar2, sizeof(*ar));
735 ll2 = (struct sockaddr_dl *)((u_char *)ar2 + sizeof(*ar2));
736 bcopy(ll, ll2, sizeof(*ll));
737 ll2->sdl_type = xtype;
738 ll2->sdl_index = xindex;
740 rt->rtm_msglen = sizeof(*rt) + sizeof(*ar2) + sizeof(*ll2);
741 errno = 0;
742 if ((write(r, rt, rt->rtm_msglen) == -1) && (errno != EEXIST)) {
743 logmsg(LOG_ERR, "rtmsg add write: %m");
744 close(r);
745 return;
747 do {
748 cc = read(r, rt, sizeof(rtmsg));
749 } while (cc > 0 && (rt->rtm_seq != seq || rt->rtm_pid != pid));
750 close(r);
751 if (cc == -1) {
752 logmsg(LOG_ERR, "rtmsg add read: %m");
753 return;
758 * Build a reverse ARP packet and sent it out on the interface.
759 * 'ep' points to a valid REVARP_REQUEST. The REVARP_REPLY is built
760 * on top of the request, then written to the network.
762 * RFC 903 defines the ether_arp fields as follows. The following comments
763 * are taken (more or less) straight from this document.
765 * REVARP_REQUEST
767 * arp_sha is the hardware address of the sender of the packet.
768 * arp_spa is undefined.
769 * arp_tha is the 'target' hardware address.
770 * In the case where the sender wishes to determine his own
771 * protocol address, this, like arp_sha, will be the hardware
772 * address of the sender.
773 * arp_tpa is undefined.
775 * REVARP_REPLY
777 * arp_sha is the hardware address of the responder (the sender of the
778 * reply packet).
779 * arp_spa is the protocol address of the responder (see the note below).
780 * arp_tha is the hardware address of the target, and should be the same as
781 * that which was given in the request.
782 * arp_tpa is the protocol address of the target, that is, the desired address.
784 * Note that the requirement that arp_spa be filled in with the responder's
785 * protocol is purely for convenience. For instance, if a system were to use
786 * both ARP and RARP, then the inclusion of the valid protocol-hardware
787 * address pair (arp_spa, arp_sha) may eliminate the need for a subsequent
788 * ARP request.
790 static void
791 rarp_reply(struct if_info *ii, struct ether_header *ep, in_addr_t ipaddr,
792 u_int len)
794 u_int n;
795 struct ether_arp *ap = (struct ether_arp *)(ep + 1);
797 update_arptab((u_char *)&ap->arp_sha, ipaddr);
800 * Build the rarp reply by modifying the rarp request in place.
802 ap->arp_op = htons(REVARP_REPLY);
804 #ifdef BROKEN_BPF
805 ep->ether_type = ETHERTYPE_REVARP;
806 #endif
807 bcopy((char *)&ap->arp_sha, (char *)&ep->ether_dhost, 6);
808 bcopy((char *)ii->ii_eaddr, (char *)&ep->ether_shost, 6);
809 bcopy((char *)ii->ii_eaddr, (char *)&ap->arp_sha, 6);
811 bcopy((char *)&ipaddr, (char *)ap->arp_tpa, 4);
812 /* Target hardware is unchanged. */
813 bcopy((char *)&ii->ii_ipaddr, (char *)ap->arp_spa, 4);
815 /* Zero possible garbage after packet. */
816 bzero((char *)ep + (sizeof(*ep) + sizeof(*ap)),
817 len - (sizeof(*ep) + sizeof(*ap)));
818 n = write(ii->ii_fd, (char *)ep, len);
819 if (n != len)
820 logmsg(LOG_ERR, "write: only %d of %d bytes written", n, len);
821 if (verbose)
822 logmsg(LOG_INFO, "%s %s at %s REPLIED", ii->ii_ifname,
823 eatoa(ap->arp_tha),
824 intoa(ntohl(ipaddr)));
828 * Get the netmask of an IP address. This routine is used if
829 * SIOCGIFNETMASK doesn't work.
831 static in_addr_t
832 ipaddrtonetmask(in_addr_t addr)
834 addr = ntohl(addr);
835 if (IN_CLASSA(addr))
836 return htonl(IN_CLASSA_NET);
837 if (IN_CLASSB(addr))
838 return htonl(IN_CLASSB_NET);
839 if (IN_CLASSC(addr))
840 return htonl(IN_CLASSC_NET);
841 logmsg(LOG_DEBUG, "unknown IP address class: %08X", addr);
842 return htonl(0xffffffff);
846 * A faster replacement for inet_ntoa().
848 static char *
849 intoa(in_addr_t addr)
851 char *cp;
852 u_int byte;
853 int n;
854 static char buf[sizeof(".xxx.xxx.xxx.xxx")];
856 cp = &buf[sizeof buf];
857 *--cp = '\0';
859 n = 4;
860 do {
861 byte = addr & 0xff;
862 *--cp = byte % 10 + '0';
863 byte /= 10;
864 if (byte > 0) {
865 *--cp = byte % 10 + '0';
866 byte /= 10;
867 if (byte > 0)
868 *--cp = byte + '0';
870 *--cp = '.';
871 addr >>= 8;
872 } while (--n > 0);
874 return cp + 1;
877 static char *
878 eatoa(u_char *ea)
880 static char buf[sizeof("xx:xx:xx:xx:xx:xx")];
882 sprintf(buf, "%x:%x:%x:%x:%x:%x",
883 ea[0], ea[1], ea[2], ea[3], ea[4], ea[5]);
884 return (buf);
887 static void
888 logmsg(int pri, const char *fmt, ...)
890 va_list v;
891 FILE *fp;
892 char *newfmt;
894 va_start(v, fmt);
895 if (dflag) {
896 if (pri == LOG_ERR)
897 fp = stderr;
898 else
899 fp = stdout;
900 if (expand_syslog_m(fmt, &newfmt) == -1) {
901 vfprintf(fp, fmt, v);
902 } else {
903 vfprintf(fp, newfmt, v);
904 free(newfmt);
906 fputs("\n", fp);
907 fflush(fp);
908 } else {
909 vsyslog(pri, fmt, v);
911 va_end(v);
914 static int
915 expand_syslog_m(const char *fmt, char **newfmt)
917 const char *str, *m;
918 char *p, *np;
920 p = strdup("");
921 str = fmt;
922 while ((m = strstr(str, "%m")) != NULL) {
923 asprintf(&np, "%s%.*s%s", p, (int)(m - str),
924 str, strerror(errno));
925 free(p);
926 if (np == NULL) {
927 errno = ENOMEM;
928 return (-1);
930 p = np;
931 str = m + 2;
934 if (*str != '\0') {
935 asprintf(&np, "%s%s", p, str);
936 free(p);
937 if (np == NULL) {
938 errno = ENOMEM;
939 return (-1);
941 p = np;
944 *newfmt = p;
945 return (0);