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, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 * @(#) Copyright (c) 1990, 1991, 1992, 1993, 1996 The Regents of the University of California. All rights reserved.
22 * $FreeBSD: src/usr.sbin/rarpd/rarpd.c,v 1.23.2.4 2002/12/01 19:19:34 dwmalone Exp $
23 * $DragonFly: src/usr.sbin/rarpd/rarpd.c,v 1.3 2004/02/10 02:59:43 rob Exp $
27 * rarpd - Reverse ARP Daemon
29 * Usage: rarpd -a [ -dfsv ] [-t directory] [ hostname ]
30 * rarpd [ -dfsv ] [-t directory] interface [ hostname ]
32 * 'hostname' is optional solely for backwards compatibility with Sun's rarpd.
33 * Currently, the argument is ignored.
35 #include <sys/param.h>
37 #include <sys/ioctl.h>
38 #include <sys/socket.h>
42 #include <net/ethernet.h>
44 #include <net/if_types.h>
45 #include <net/if_dl.h>
46 #include <net/route.h>
48 #include <netinet/in.h>
49 #include <netinet/if_ether.h>
51 #include <arpa/inet.h>
62 #if defined(SUNOS4) || defined(__DragonFly__) /* XXX */
72 /* Cast a struct sockaddr to a structaddr_in */
73 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
76 #define TFTP_DIR "/tftpboot"
80 #define ARPSECS (20 * 60) /* as per code in netinet/if_ether.c */
81 #define REVARP_REQUEST ARPOP_REVREQUEST
82 #define REVARP_REPLY ARPOP_REVREPLY
85 #ifndef ETHERTYPE_REVARP
86 #define ETHERTYPE_REVARP 0x8035
87 #define REVARP_REQUEST 3
88 #define REVARP_REPLY 4
92 * Map field names in ether_arp struct. What a pain in the neck.
99 #define arp_sha arp_xsha
100 #define arp_spa arp_xspa
101 #define arp_tha arp_xtha
102 #define arp_tpa arp_xtpa
106 * The structure for each interface.
109 struct if_info *ii_next;
110 int ii_fd; /* BPF file descriptor */
111 u_long ii_ipaddr; /* IP address of this interface */
112 u_long ii_netmask; /* subnet or net mask */
113 u_char ii_eaddr[6]; /* Ethernet address of this interface */
114 char ii_ifname[sizeof(((struct ifreq *)0)->ifr_name) + 1];
118 * The list of all interfaces that are being listened to. rarp_loop()
119 * "selects" on the descriptors in this list.
121 struct if_info *iflist;
123 int verbose; /* verbose messages */
124 int s; /* inet datagram socket */
125 const char *tftp_dir = TFTP_DIR; /* tftp directory */
127 int dflag; /* messages to stdout/stderr, not syslog(3) */
128 int sflag; /* ignore /tftpboot */
130 static u_char zero[6];
132 static int bpf_open(void);
133 static u_long choose_ipaddr(u_long **, u_long, u_long);
134 static char *eatoa(u_char *);
135 static int expand_syslog_m(const char *fmt, char **newfmt);
136 static void init(char *);
137 static void init_one(struct ifreq *, char *);
138 static char *intoa(u_long);
139 static u_long ipaddrtonetmask(u_long);
140 static void logmsg(int, const char *, ...) __printflike(2, 3);
141 static int rarp_bootable(u_long);
142 static int rarp_check(u_char *, u_int);
143 static void rarp_loop(void);
144 static int rarp_open(char *);
145 static void rarp_process(struct if_info *, u_char *, u_int);
146 static void rarp_reply(struct if_info *, struct ether_header *,
148 static void update_arptab(u_char *, u_long);
149 static void usage(void);
152 main(int argc, char *argv[])
155 char *ifname, *hostname, *name;
157 int aflag = 0; /* listen on "all" interfaces */
158 int fflag = 0; /* don't fork */
160 if ((name = strrchr(argv[0], '/')) != NULL)
168 * All error reporting is done through syslog, unless -d is specified
170 openlog(name, LOG_PID | LOG_CONS, LOG_DAEMON);
173 while ((op = getopt(argc, argv, "adfst:v")) != -1) {
204 ifname = argv[optind++];
205 hostname = ifname ? argv[optind] : NULL;
206 if ((aflag && ifname) || (!aflag && ifname == NULL))
216 logmsg(LOG_ERR, "cannot fork");
225 * Add to the interface list.
228 init_one(struct ifreq *ifrp, char *target)
231 struct sockaddr_dl *ll;
235 family = ifrp->ifr_addr.sa_family;
242 (void)strncpy(ifr.ifr_name, ifrp->ifr_name,
243 sizeof(ifrp->ifr_name));
244 if (ioctl(s, SIOCGIFFLAGS, (char *)&ifr) == -1) {
246 "SIOCGIFFLAGS: %.*s: %m",
247 (int)sizeof(ifrp->ifr_name), ifrp->ifr_name);
250 if ((ifr.ifr_flags & IFF_UP) == 0 ||
251 (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0)
260 /* Don't bother going any further if not the target interface */
261 if (target != NULL &&
262 strncmp(ifrp->ifr_name, target, sizeof(ifrp->ifr_name)) != 0)
265 /* Look for interface in list */
266 for (ii = iflist; ii != NULL; ii = ii->ii_next)
267 if (strncmp(ifrp->ifr_name, ii->ii_ifname,
268 sizeof(ifrp->ifr_name)) == 0)
271 /* Allocate a new one if not found */
273 ii = (struct if_info *)malloc(sizeof(*ii));
275 logmsg(LOG_ERR, "malloc: %m");
278 bzero(ii, sizeof(*ii));
280 (void)strncpy(ii->ii_ifname, ifrp->ifr_name,
281 sizeof(ifrp->ifr_name));
282 ii->ii_ifname[sizeof(ii->ii_ifname) - 1] = '\0';
283 ii->ii_next = iflist;
290 if (ioctl(s, SIOCGIFADDR, (char *)&ifr) == -1) {
291 logmsg(LOG_ERR, "ipaddr SIOCGIFADDR: %s: %m",
295 ii->ii_ipaddr = SATOSIN(&ifr.ifr_addr)->sin_addr.s_addr;
296 if (ioctl(s, SIOCGIFNETMASK, (char *)&ifr) == -1) {
297 logmsg(LOG_ERR, "SIOCGIFNETMASK: %m");
300 ii->ii_netmask = SATOSIN(&ifr.ifr_addr)->sin_addr.s_addr;
301 if (ii->ii_netmask == 0)
302 ii->ii_netmask = ipaddrtonetmask(ii->ii_ipaddr);
304 ii->ii_fd = rarp_open(ii->ii_ifname);
306 /* Use BPF descriptor to get ethernet address. */
307 if (ioctl(ii->ii_fd, SIOCGIFADDR, (char *)&ifr) == -1) {
308 logmsg(LOG_ERR, "eaddr SIOCGIFADDR: %s: %m",
312 bcopy(&ifr.ifr_addr.sa_data[0], ii->ii_eaddr, 6);
319 ll = (struct sockaddr_dl *)&ifrp->ifr_addr;
320 if (ll->sdl_type == IFT_ETHER)
321 bcopy(LLADDR(ll), ii->ii_eaddr, 6);
327 * Initialize all "candidate" interfaces that are in the system
328 * configuration list. A "candidate" is up, not loopback and not
335 struct ifreq *ifrp, *ifend;
336 struct if_info *ii, *nii, *lii;
338 struct ifreq ibuf[16];
340 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
341 logmsg(LOG_ERR, "socket: %m");
344 ifc.ifc_len = sizeof ibuf;
345 ifc.ifc_buf = (caddr_t)ibuf;
346 if ((ioctl(s, SIOCGIFCONF, (char *)&ifc) == -1) ||
347 ((u_int)ifc.ifc_len < sizeof(struct ifreq))) {
348 logmsg(LOG_ERR, "SIOCGIFCONF: %m");
352 ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
353 while (ifrp < ifend) {
354 init_one(ifrp, target);
357 n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
358 if (n < sizeof(*ifrp))
360 ifrp = (struct ifreq *)((char *)ifrp + n);
366 /* Throw away incomplete interfaces */
368 for (ii = iflist; ii != NULL; ii = nii) {
370 if (ii->ii_ipaddr == 0 ||
371 bcmp(ii->ii_eaddr, zero, 6) == 0) {
386 for (ii = iflist; ii != NULL; ii = ii->ii_next)
387 logmsg(LOG_DEBUG, "%s %s 0x%08lx %s",
388 ii->ii_ifname, intoa(ntohl(ii->ii_ipaddr)),
389 (u_long)ntohl(ii->ii_netmask), eatoa(ii->ii_eaddr));
395 (void)fprintf(stderr, "usage: rarpd [-adfsv] [-t directory] [interface]\n");
404 char device[sizeof "/dev/bpf000"];
407 * Go through all the minors and find one that isn't in use.
410 (void)sprintf(device, "/dev/bpf%d", n++);
411 fd = open(device, O_RDWR);
412 } while ((fd == -1) && (errno == EBUSY));
415 logmsg(LOG_ERR, "%s: %m", device);
422 * Open a BPF file and attach it to the interface named 'device'.
423 * Set immediate mode, and set a filter that accepts only RARP requests.
426 rarp_open(char *device)
433 static struct bpf_insn insns[] = {
434 BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 12),
435 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, ETHERTYPE_REVARP, 0, 3),
436 BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 20),
437 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, REVARP_REQUEST, 0, 1),
438 BPF_STMT(BPF_RET|BPF_K, sizeof(struct ether_arp) +
439 sizeof(struct ether_header)),
440 BPF_STMT(BPF_RET|BPF_K, 0),
442 static struct bpf_program filter = {
443 sizeof insns / sizeof(insns[0]),
449 * Set immediate mode so packets are processed as they arrive.
452 if (ioctl(fd, BIOCIMMEDIATE, &immediate) == -1) {
453 logmsg(LOG_ERR, "BIOCIMMEDIATE: %m");
456 (void)strncpy(ifr.ifr_name, device, sizeof ifr.ifr_name);
457 if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) == -1) {
458 logmsg(LOG_ERR, "BIOCSETIF: %m");
462 * Check that the data link layer is an Ethernet; this code won't
463 * work with anything else.
465 if (ioctl(fd, BIOCGDLT, (caddr_t)&dlt) == -1) {
466 logmsg(LOG_ERR, "BIOCGDLT: %m");
469 if (dlt != DLT_EN10MB) {
470 logmsg(LOG_ERR, "%s is not an ethernet", device);
474 * Set filter program.
476 if (ioctl(fd, BIOCSETF, (caddr_t)&filter) == -1) {
477 logmsg(LOG_ERR, "BIOCSETF: %m");
484 * Perform various sanity checks on the RARP request packet. Return
485 * false on failure and log the reason.
488 rarp_check(u_char *p, u_int len)
490 struct ether_header *ep = (struct ether_header *)p;
491 struct ether_arp *ap = (struct ether_arp *)(p + sizeof(*ep));
493 if (len < sizeof(*ep) + sizeof(*ap)) {
494 logmsg(LOG_ERR, "truncated request, got %u, expected %lu",
495 len, (u_long)(sizeof(*ep) + sizeof(*ap)));
499 * XXX This test might be better off broken out...
501 if (ntohs(ep->ether_type) != ETHERTYPE_REVARP ||
502 ntohs(ap->arp_hrd) != ARPHRD_ETHER ||
503 ntohs(ap->arp_op) != REVARP_REQUEST ||
504 ntohs(ap->arp_pro) != ETHERTYPE_IP ||
505 ap->arp_hln != 6 || ap->arp_pln != 4) {
506 logmsg(LOG_DEBUG, "request fails sanity check");
509 if (bcmp((char *)&ep->ether_shost, (char *)&ap->arp_sha, 6) != 0) {
510 logmsg(LOG_DEBUG, "ether/arp sender address mismatch");
513 if (bcmp((char *)&ap->arp_sha, (char *)&ap->arp_tha, 6) != 0) {
514 logmsg(LOG_DEBUG, "ether/arp target address mismatch");
521 #define FD_SET(n, fdp) ((fdp)->fds_bits[0] |= (1 << (n)))
522 #define FD_ISSET(n, fdp) ((fdp)->fds_bits[0] & (1 << (n)))
523 #define FD_ZERO(fdp) ((fdp)->fds_bits[0] = 0)
527 * Loop indefinitely listening for RARP requests on the
528 * interfaces in 'iflist'.
533 u_char *buf, *bp, *ep;
535 fd_set fds, listeners;
536 int bufsize, maxfd = 0;
539 if (iflist == NULL) {
540 logmsg(LOG_ERR, "no interfaces");
543 if (ioctl(iflist->ii_fd, BIOCGBLEN, (caddr_t)&bufsize) == -1) {
544 logmsg(LOG_ERR, "BIOCGBLEN: %m");
547 buf = malloc(bufsize);
549 logmsg(LOG_ERR, "malloc: %m");
555 * Find the highest numbered file descriptor for select().
556 * Initialize the set of descriptors to listen to.
559 for (ii = iflist; ii != NULL; ii = ii->ii_next) {
560 FD_SET(ii->ii_fd, &fds);
561 if (ii->ii_fd > maxfd)
565 if (select(maxfd + 1, &listeners, NULL, NULL, NULL) == -1) {
566 /* Don't choke when we get ptraced */
569 logmsg(LOG_ERR, "select: %m");
572 for (ii = iflist; ii != NULL; ii = ii->ii_next) {
574 if (!FD_ISSET(fd, &listeners))
577 cc = read(fd, (char *)buf, bufsize);
578 /* Don't choke when we get ptraced */
579 if ((cc == -1) && (errno == EINTR))
581 #if defined(SUNOS3) || defined(SUNOS4)
583 * Due to a SunOS bug, after 2^31 bytes, the
584 * file offset overflows and read fails with
585 * EINVAL. The lseek() to 0 will fix things.
588 if (errno == EINVAL &&
589 (long)(tell(fd) + bufsize) < 0) {
590 (void)lseek(fd, 0, 0);
593 logmsg(LOG_ERR, "read: %m");
598 /* Loop through the packet(s) */
599 #define bhp ((struct bpf_hdr *)bp)
603 u_int caplen, hdrlen;
605 caplen = bhp->bh_caplen;
606 hdrlen = bhp->bh_hdrlen;
607 if (rarp_check(bp + hdrlen, caplen))
608 rarp_process(ii, bp + hdrlen, caplen);
609 bp += BPF_WORDALIGN(hdrlen + caplen);
617 * True if this server can boot the host whose IP address is 'addr'.
618 * This check is made by looking in the tftp directory for the
619 * configuration file.
622 rarp_bootable(u_long addr)
631 static DIR *dd = NULL;
633 (void)sprintf(ipname, "%08lX", (u_long)ntohl(addr));
636 * If directory is already open, rewind it. Otherwise, open it.
638 if ((d = dd) != NULL)
641 if (chdir(tftp_dir) == -1) {
642 logmsg(LOG_ERR, "chdir: %s: %m", tftp_dir);
647 logmsg(LOG_ERR, "opendir: %m");
652 while ((dent = readdir(d)) != NULL)
653 if (strncmp(dent->d_name, ipname, 8) == 0)
659 * Given a list of IP addresses, 'alist', return the first address that
660 * is on network 'net'; 'netmask' is a mask indicating the network portion
664 choose_ipaddr(u_long **alist, u_long net, u_long netmask)
666 for (; *alist; ++alist)
667 if ((**alist & netmask) == net)
673 * Answer the RARP request in 'pkt', on the interface 'ii'. 'pkt' has
674 * already been checked for validity. The reply is overlaid on the request.
677 rarp_process(struct if_info *ii, u_char *pkt, u_int len)
679 struct ether_header *ep;
681 u_long target_ipaddr;
684 ep = (struct ether_header *)pkt;
685 /* should this be arp_tha? */
686 if (ether_ntohost(ename, (struct ether_addr *)&ep->ether_shost) != 0) {
687 logmsg(LOG_ERR, "cannot map %s to name",
688 eatoa(ep->ether_shost));
692 if ((hp = gethostbyname(ename)) == NULL) {
693 logmsg(LOG_ERR, "cannot map %s to IP address", ename);
698 * Choose correct address from list.
700 if (hp->h_addrtype != AF_INET) {
701 logmsg(LOG_ERR, "cannot handle non IP addresses for %s",
705 target_ipaddr = choose_ipaddr((u_long **)hp->h_addr_list,
706 ii->ii_ipaddr & ii->ii_netmask,
708 if (target_ipaddr == 0) {
709 logmsg(LOG_ERR, "cannot find %s on net %s",
710 ename, intoa(ntohl(ii->ii_ipaddr & ii->ii_netmask)));
713 if (sflag || rarp_bootable(target_ipaddr))
714 rarp_reply(ii, ep, target_ipaddr, len);
715 else if (verbose > 1)
716 logmsg(LOG_INFO, "%s %s at %s DENIED (not bootable)",
718 eatoa(ep->ether_shost),
719 intoa(ntohl(target_ipaddr)));
723 * Poke the kernel arp tables with the ethernet/ip address combinataion
724 * given. When processing a reply, we must do this so that the booting
725 * host (i.e. the guy running rarpd), won't try to ARP for the hardware
726 * address of the guy being booted (he cannot answer the ARP).
729 struct sockaddr_inarp sin_inarp = {
730 sizeof(struct sockaddr_inarp), AF_INET, 0,
735 struct sockaddr_dl sin_dl = {
736 sizeof(struct sockaddr_dl), AF_LINK, 0, IFT_ETHER, 0, 6,
740 struct rt_msghdr rthdr;
745 update_arptab(u_char *ep, u_long ipaddr)
748 struct sockaddr_inarp *ar, *ar2;
749 struct sockaddr_dl *ll, *ll2;
750 struct rt_msghdr *rt;
756 r = socket(PF_ROUTE, SOCK_RAW, 0);
758 logmsg(LOG_ERR, "raw route socket: %m");
764 ar->sin_addr.s_addr = ipaddr;
766 bcopy(ep, LLADDR(ll), 6);
768 /* Get the type and interface index */
770 bzero(rt, sizeof(rtmsg));
771 rt->rtm_version = RTM_VERSION;
772 rt->rtm_addrs = RTA_DST;
773 rt->rtm_type = RTM_GET;
775 ar2 = (struct sockaddr_inarp *)rtmsg.rtspace;
776 bcopy(ar, ar2, sizeof(*ar));
777 rt->rtm_msglen = sizeof(*rt) + sizeof(*ar);
779 if ((write(r, rt, rt->rtm_msglen) == -1) && (errno != ESRCH)) {
780 logmsg(LOG_ERR, "rtmsg get write: %m");
785 cc = read(r, rt, sizeof(rtmsg));
786 } while (cc > 0 && (rt->rtm_seq != seq || rt->rtm_pid != pid));
788 logmsg(LOG_ERR, "rtmsg get read: %m");
792 ll2 = (struct sockaddr_dl *)((u_char *)ar2 + ar2->sin_len);
793 if (ll2->sdl_family != AF_LINK) {
795 * XXX I think this means the ip address is not on a
796 * directly connected network (the family is AF_INET in
799 logmsg(LOG_ERR, "bogus link family (%d) wrong net for %08lX?\n",
800 ll2->sdl_family, ipaddr);
804 xtype = ll2->sdl_type;
805 xindex = ll2->sdl_index;
807 /* Set the new arp entry */
808 bzero(rt, sizeof(rtmsg));
809 rt->rtm_version = RTM_VERSION;
810 rt->rtm_addrs = RTA_DST | RTA_GATEWAY;
811 rt->rtm_inits = RTV_EXPIRE;
812 rt->rtm_rmx.rmx_expire = time(0) + ARPSECS;
813 rt->rtm_flags = RTF_HOST | RTF_STATIC;
814 rt->rtm_type = RTM_ADD;
817 bcopy(ar, ar2, sizeof(*ar));
819 ll2 = (struct sockaddr_dl *)((u_char *)ar2 + sizeof(*ar2));
820 bcopy(ll, ll2, sizeof(*ll));
821 ll2->sdl_type = xtype;
822 ll2->sdl_index = xindex;
824 rt->rtm_msglen = sizeof(*rt) + sizeof(*ar2) + sizeof(*ll2);
826 if ((write(r, rt, rt->rtm_msglen) == -1) && (errno != EEXIST)) {
827 logmsg(LOG_ERR, "rtmsg add write: %m");
832 cc = read(r, rt, sizeof(rtmsg));
833 } while (cc > 0 && (rt->rtm_seq != seq || rt->rtm_pid != pid));
836 logmsg(LOG_ERR, "rtmsg add read: %m");
842 update_arptab(u_char *ep, u_long ipaddr)
844 struct arpreq request;
845 struct sockaddr_in *sin;
847 request.arp_flags = 0;
848 sin = (struct sockaddr_in *)&request.arp_pa;
849 sin->sin_family = AF_INET;
850 sin->sin_addr.s_addr = ipaddr;
851 request.arp_ha.sa_family = AF_UNSPEC;
852 bcopy((char *)ep, (char *)request.arp_ha.sa_data, 6);
854 if (ioctl(s, SIOCSARP, (caddr_t)&request) == -1)
855 logmsg(LOG_ERR, "SIOCSARP: %m");
860 * Build a reverse ARP packet and sent it out on the interface.
861 * 'ep' points to a valid REVARP_REQUEST. The REVARP_REPLY is built
862 * on top of the request, then written to the network.
864 * RFC 903 defines the ether_arp fields as follows. The following comments
865 * are taken (more or less) straight from this document.
869 * arp_sha is the hardware address of the sender of the packet.
870 * arp_spa is undefined.
871 * arp_tha is the 'target' hardware address.
872 * In the case where the sender wishes to determine his own
873 * protocol address, this, like arp_sha, will be the hardware
874 * address of the sender.
875 * arp_tpa is undefined.
879 * arp_sha is the hardware address of the responder (the sender of the
881 * arp_spa is the protocol address of the responder (see the note below).
882 * arp_tha is the hardware address of the target, and should be the same as
883 * that which was given in the request.
884 * arp_tpa is the protocol address of the target, that is, the desired address.
886 * Note that the requirement that arp_spa be filled in with the responder's
887 * protocol is purely for convenience. For instance, if a system were to use
888 * both ARP and RARP, then the inclusion of the valid protocol-hardware
889 * address pair (arp_spa, arp_sha) may eliminate the need for a subsequent
893 rarp_reply(struct if_info *ii, struct ether_header *ep, u_long ipaddr,
897 struct ether_arp *ap = (struct ether_arp *)(ep + 1);
899 update_arptab((u_char *)&ap->arp_sha, ipaddr);
902 * Build the rarp reply by modifying the rarp request in place.
904 ap->arp_op = htons(REVARP_REPLY);
907 ep->ether_type = ETHERTYPE_REVARP;
909 bcopy((char *)&ap->arp_sha, (char *)&ep->ether_dhost, 6);
910 bcopy((char *)ii->ii_eaddr, (char *)&ep->ether_shost, 6);
911 bcopy((char *)ii->ii_eaddr, (char *)&ap->arp_sha, 6);
913 bcopy((char *)&ipaddr, (char *)ap->arp_tpa, 4);
914 /* Target hardware is unchanged. */
915 bcopy((char *)&ii->ii_ipaddr, (char *)ap->arp_spa, 4);
917 /* Zero possible garbage after packet. */
918 bzero((char *)ep + (sizeof(*ep) + sizeof(*ap)),
919 len - (sizeof(*ep) + sizeof(*ap)));
920 n = write(ii->ii_fd, (char *)ep, len);
922 logmsg(LOG_ERR, "write: only %d of %d bytes written", n, len);
924 logmsg(LOG_INFO, "%s %s at %s REPLIED", ii->ii_ifname,
926 intoa(ntohl(ipaddr)));
930 * Get the netmask of an IP address. This routine is used if
931 * SIOCGIFNETMASK doesn't work.
934 ipaddrtonetmask(u_long addr)
938 return htonl(IN_CLASSA_NET);
940 return htonl(IN_CLASSB_NET);
942 return htonl(IN_CLASSC_NET);
943 logmsg(LOG_DEBUG, "unknown IP address class: %08lX", addr);
944 return htonl(0xffffffff);
948 * A faster replacement for inet_ntoa().
956 static char buf[sizeof(".xxx.xxx.xxx.xxx")];
958 cp = &buf[sizeof buf];
964 *--cp = byte % 10 + '0';
967 *--cp = byte % 10 + '0';
982 static char buf[sizeof("xx:xx:xx:xx:xx:xx")];
984 (void)sprintf(buf, "%x:%x:%x:%x:%x:%x",
985 ea[0], ea[1], ea[2], ea[3], ea[4], ea[5]);
990 logmsg(int pri, const char *fmt, ...)
1002 if (expand_syslog_m(fmt, &newfmt) == -1) {
1003 vfprintf(fp, fmt, v);
1005 vfprintf(fp, newfmt, v);
1011 vsyslog(pri, fmt, v);
1017 expand_syslog_m(const char *fmt, char **newfmt) {
1018 const char *str, *m;
1023 while ((m = strstr(str, "%m")) != NULL) {
1024 asprintf(&np, "%s%.*s%s", p, (int)(m - str),
1025 str, strerror(errno));
1036 asprintf(&np, "%s%s", p, str);