1/*-
2 * SPDX-License-Identifier: BSD-1-Clause
3 *
4 * Copyright (c) 1990, 1991, 1992, 1993, 1996
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution
13 *
14 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 */
18
19#if 0
20#ifndef lint
21static const char copyright[] =
22"@(#) Copyright (c) 1990, 1991, 1992, 1993, 1996\n\
23The Regents of the University of California.  All rights reserved.\n";
24#endif /* not lint */
25#endif
26#include <sys/cdefs.h>
27__FBSDID("$FreeBSD$");
28
29/*
30 * rarpd - Reverse ARP Daemon
31 *
32 * Usage:	rarpd -a [-dfsv] [-t directory] [-P pidfile] [hostname]
33 *		rarpd [-dfsv] [-t directory] [-P pidfile] interface [hostname]
34 *
35 * 'hostname' is optional solely for backwards compatibility with Sun's rarpd.
36 * Currently, the argument is ignored.
37 */
38#include <sys/param.h>
39#include <sys/file.h>
40#include <sys/ioctl.h>
41#include <sys/socket.h>
42#include <sys/time.h>
43
44#include <net/bpf.h>
45#include <net/ethernet.h>
46#include <net/if.h>
47#include <net/if_types.h>
48#include <net/if_dl.h>
49#include <net/route.h>
50
51#include <netinet/in.h>
52#include <netinet/if_ether.h>
53
54#include <arpa/inet.h>
55
56#include <dirent.h>
57#include <errno.h>
58#include <ifaddrs.h>
59#include <netdb.h>
60#include <stdarg.h>
61#include <stdio.h>
62#include <string.h>
63#include <syslog.h>
64#include <stdlib.h>
65#include <unistd.h>
66#include <libutil.h>
67
68/* Cast a struct sockaddr to a struct sockaddr_in */
69#define SATOSIN(sa) ((struct sockaddr_in *)(sa))
70
71#ifndef TFTP_DIR
72#define TFTP_DIR "/tftpboot"
73#endif
74
75#define ARPSECS (20 * 60)		/* as per code in netinet/if_ether.c */
76#define REVARP_REQUEST ARPOP_REVREQUEST
77#define REVARP_REPLY ARPOP_REVREPLY
78
79/*
80 * The structure for each interface.
81 */
82struct if_info {
83	struct if_info	*ii_next;
84	int		ii_fd;			/* BPF file descriptor */
85	in_addr_t	ii_ipaddr;		/* IP address */
86	in_addr_t	ii_netmask;		/* subnet or net mask */
87	u_char		ii_eaddr[ETHER_ADDR_LEN];	/* ethernet address */
88	char		ii_ifname[IF_NAMESIZE];
89};
90
91/*
92 * The list of all interfaces that are being listened to.  rarp_loop()
93 * "selects" on the descriptors in this list.
94 */
95static struct if_info *iflist;
96
97static int verbose;		/* verbose messages */
98static const char *tftp_dir = TFTP_DIR;	/* tftp directory */
99
100static int dflag;		/* messages to stdout/stderr, not syslog(3) */
101static int sflag;		/* ignore /tftpboot */
102
103static	u_char zero[6];
104
105static char pidfile_buf[PATH_MAX];
106static char *pidfile;
107#define	RARPD_PIDFILE	"/var/run/rarpd.%s.pid"
108static struct pidfh *pidfile_fh;
109
110static int	bpf_open(void);
111static in_addr_t	choose_ipaddr(in_addr_t **, in_addr_t, in_addr_t);
112static char	*eatoa(u_char *);
113static int	expand_syslog_m(const char *fmt, char **newfmt);
114static void	init(char *);
115static void	init_one(struct ifaddrs *, char *, int);
116static char	*intoa(in_addr_t);
117static in_addr_t	ipaddrtonetmask(in_addr_t);
118static void	logmsg(int, const char *, ...) __printflike(2, 3);
119static int	rarp_bootable(in_addr_t);
120static int	rarp_check(u_char *, u_int);
121static void	rarp_loop(void);
122static int	rarp_open(char *);
123static void	rarp_process(struct if_info *, u_char *, u_int);
124static void	rarp_reply(struct if_info *, struct ether_header *,
125		in_addr_t, u_int);
126static void	update_arptab(u_char *, in_addr_t);
127static void	usage(void);
128
129int
130main(int argc, char *argv[])
131{
132	int op;
133	char *ifname, *name;
134
135	int aflag = 0;		/* listen on "all" interfaces  */
136	int fflag = 0;		/* don't fork */
137
138	if ((name = strrchr(argv[0], '/')) != NULL)
139		++name;
140	else
141		name = argv[0];
142	if (*name == '-')
143		++name;
144
145	/*
146	 * All error reporting is done through syslog, unless -d is specified
147	 */
148	openlog(name, LOG_PID | LOG_CONS, LOG_DAEMON);
149
150	opterr = 0;
151	while ((op = getopt(argc, argv, "adfsP:t:v")) != -1)
152		switch (op) {
153		case 'a':
154			++aflag;
155			break;
156
157		case 'd':
158			++dflag;
159			break;
160
161		case 'f':
162			++fflag;
163			break;
164
165		case 's':
166			++sflag;
167			break;
168
169		case 'P':
170			strncpy(pidfile_buf, optarg, sizeof(pidfile_buf) - 1);
171			pidfile_buf[sizeof(pidfile_buf) - 1] = '\0';
172			pidfile = pidfile_buf;
173			break;
174
175		case 't':
176			tftp_dir = optarg;
177			break;
178
179		case 'v':
180			++verbose;
181			break;
182
183		default:
184			usage();
185			/* NOTREACHED */
186		}
187	argc -= optind;
188	argv += optind;
189
190	ifname = (aflag == 0) ? argv[0] : NULL;
191
192	if ((aflag && ifname) || (!aflag && ifname == NULL))
193		usage();
194
195	init(ifname);
196
197	if (!fflag) {
198		if (pidfile == NULL && ifname != NULL && aflag == 0) {
199			snprintf(pidfile_buf, sizeof(pidfile_buf) - 1,
200			    RARPD_PIDFILE, ifname);
201			pidfile_buf[sizeof(pidfile_buf) - 1] = '\0';
202			pidfile = pidfile_buf;
203		}
204		/* If pidfile == NULL, /var/run/<progname>.pid will be used. */
205		pidfile_fh = pidfile_open(pidfile, 0600, NULL);
206		if (pidfile_fh == NULL)
207			logmsg(LOG_ERR, "Cannot open or create pidfile: %s",
208			    (pidfile == NULL) ? "/var/run/rarpd.pid" : pidfile);
209		if (daemon(0,0)) {
210			logmsg(LOG_ERR, "cannot fork");
211			pidfile_remove(pidfile_fh);
212			exit(1);
213		}
214		pidfile_write(pidfile_fh);
215	}
216	rarp_loop();
217	return(0);
218}
219
220/*
221 * Add to the interface list.
222 */
223static void
224init_one(struct ifaddrs *ifa, char *target, int pass1)
225{
226	struct if_info *ii, *ii2;
227	struct sockaddr_dl *ll;
228	int family;
229
230	family = ifa->ifa_addr->sa_family;
231	switch (family) {
232	case AF_INET:
233		if (pass1)
234			/* Consider only AF_LINK during pass1. */
235			return;
236		/* FALLTHROUGH */
237	case AF_LINK:
238		if (!(ifa->ifa_flags & IFF_UP) ||
239		    (ifa->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)))
240			return;
241		break;
242	default:
243		return;
244	}
245
246	/* Don't bother going any further if not the target interface */
247	if (target != NULL && strcmp(ifa->ifa_name, target) != 0)
248		return;
249
250	/* Look for interface in list */
251	for (ii = iflist; ii != NULL; ii = ii->ii_next)
252		if (strcmp(ifa->ifa_name, ii->ii_ifname) == 0)
253			break;
254
255	if (pass1 && ii != NULL)
256		/* We've already seen that interface once. */
257		return;
258
259	/* Allocate a new one if not found */
260	if (ii == NULL) {
261		ii = (struct if_info *)malloc(sizeof(*ii));
262		if (ii == NULL) {
263			logmsg(LOG_ERR, "malloc: %m");
264			pidfile_remove(pidfile_fh);
265			exit(1);
266		}
267		bzero(ii, sizeof(*ii));
268		ii->ii_fd = -1;
269		strlcpy(ii->ii_ifname, ifa->ifa_name, sizeof(ii->ii_ifname));
270		ii->ii_next = iflist;
271		iflist = ii;
272	} else if (!pass1 && ii->ii_ipaddr != 0) {
273		/*
274		 * Second AF_INET definition for that interface: clone
275		 * the existing one, and work on that cloned one.
276		 * This must be another IP address for this interface,
277		 * so avoid killing the previous configuration.
278		 */
279		ii2 = (struct if_info *)malloc(sizeof(*ii2));
280		if (ii2 == NULL) {
281			logmsg(LOG_ERR, "malloc: %m");
282			pidfile_remove(pidfile_fh);
283			exit(1);
284		}
285		memcpy(ii2, ii, sizeof(*ii2));
286		ii2->ii_fd = -1;
287		ii2->ii_next = iflist;
288		iflist = ii2;
289
290		ii = ii2;
291	}
292
293	switch (family) {
294	case AF_INET:
295		ii->ii_ipaddr = SATOSIN(ifa->ifa_addr)->sin_addr.s_addr;
296		ii->ii_netmask = SATOSIN(ifa->ifa_netmask)->sin_addr.s_addr;
297		if (ii->ii_netmask == 0)
298			ii->ii_netmask = ipaddrtonetmask(ii->ii_ipaddr);
299		if (ii->ii_fd < 0)
300			ii->ii_fd = rarp_open(ii->ii_ifname);
301		break;
302
303	case AF_LINK:
304		ll = (struct sockaddr_dl *)ifa->ifa_addr;
305		switch (ll->sdl_type) {
306		case IFT_ETHER:
307		case IFT_L2VLAN:
308			bcopy(LLADDR(ll), ii->ii_eaddr, 6);
309		}
310		break;
311	}
312}
313
314/*
315 * Initialize all "candidate" interfaces that are in the system
316 * configuration list.  A "candidate" is up, not loopback and not
317 * point to point.
318 */
319static void
320init(char *target)
321{
322	struct if_info *ii, *nii, *lii;
323	struct ifaddrs *ifhead, *ifa;
324	int error;
325
326	error = getifaddrs(&ifhead);
327	if (error) {
328		logmsg(LOG_ERR, "getifaddrs: %m");
329		pidfile_remove(pidfile_fh);
330		exit(1);
331	}
332	/*
333	 * We make two passes over the list we have got.  In the first
334	 * one, we only collect AF_LINK interfaces, and initialize our
335	 * list of interfaces from them.  In the second pass, we
336	 * collect the actual IP addresses from the AF_INET
337	 * interfaces, and allow for the same interface name to appear
338	 * multiple times (in case of more than one IP address).
339	 */
340	for (ifa = ifhead; ifa != NULL; ifa = ifa->ifa_next)
341		init_one(ifa, target, 1);
342	for (ifa = ifhead; ifa != NULL; ifa = ifa->ifa_next)
343		init_one(ifa, target, 0);
344	freeifaddrs(ifhead);
345
346	/* Throw away incomplete interfaces */
347	lii = NULL;
348	for (ii = iflist; ii != NULL; ii = nii) {
349		nii = ii->ii_next;
350		if (ii->ii_ipaddr == 0 ||
351		    bcmp(ii->ii_eaddr, zero, 6) == 0) {
352			if (lii == NULL)
353				iflist = nii;
354			else
355				lii->ii_next = nii;
356			if (ii->ii_fd >= 0)
357				close(ii->ii_fd);
358			free(ii);
359			continue;
360		}
361		lii = ii;
362	}
363
364	/* Verbose stuff */
365	if (verbose)
366		for (ii = iflist; ii != NULL; ii = ii->ii_next)
367			logmsg(LOG_DEBUG, "%s %s 0x%08x %s",
368			    ii->ii_ifname, intoa(ntohl(ii->ii_ipaddr)),
369			    (in_addr_t)ntohl(ii->ii_netmask), eatoa(ii->ii_eaddr));
370}
371
372static void
373usage(void)
374{
375
376	(void)fprintf(stderr, "%s\n%s\n",
377	    "usage: rarpd -a [-dfsv] [-t directory] [-P pidfile]",
378	    "       rarpd [-dfsv] [-t directory] [-P pidfile] interface");
379	exit(1);
380}
381
382static int
383bpf_open(void)
384{
385	int fd;
386	int n = 0;
387	char device[sizeof "/dev/bpf000"];
388
389	/*
390	 * Go through all the minors and find one that isn't in use.
391	 */
392	do {
393		(void)sprintf(device, "/dev/bpf%d", n++);
394		fd = open(device, O_RDWR);
395	} while ((fd == -1) && (errno == EBUSY));
396
397	if (fd == -1) {
398		logmsg(LOG_ERR, "%s: %m", device);
399		pidfile_remove(pidfile_fh);
400		exit(1);
401	}
402	return fd;
403}
404
405/*
406 * Open a BPF file and attach it to the interface named 'device'.
407 * Set immediate mode, and set a filter that accepts only RARP requests.
408 */
409static int
410rarp_open(char *device)
411{
412	int fd;
413	struct ifreq ifr;
414	u_int dlt;
415	int immediate;
416
417	static struct bpf_insn insns[] = {
418		BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 12),
419		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, ETHERTYPE_REVARP, 0, 3),
420		BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 20),
421		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, REVARP_REQUEST, 0, 1),
422		BPF_STMT(BPF_RET|BPF_K, sizeof(struct ether_arp) +
423			 sizeof(struct ether_header)),
424		BPF_STMT(BPF_RET|BPF_K, 0),
425	};
426	static struct bpf_program filter = {
427		sizeof insns / sizeof(insns[0]),
428		insns
429	};
430
431	fd = bpf_open();
432	/*
433	 * Set immediate mode so packets are processed as they arrive.
434	 */
435	immediate = 1;
436	if (ioctl(fd, BIOCIMMEDIATE, &immediate) == -1) {
437		logmsg(LOG_ERR, "BIOCIMMEDIATE: %m");
438		goto rarp_open_err;
439	}
440	strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
441	if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) == -1) {
442		logmsg(LOG_ERR, "BIOCSETIF: %m");
443		goto rarp_open_err;
444	}
445	/*
446	 * Check that the data link layer is an Ethernet; this code won't
447	 * work with anything else.
448	 */
449	if (ioctl(fd, BIOCGDLT, (caddr_t)&dlt) == -1) {
450		logmsg(LOG_ERR, "BIOCGDLT: %m");
451		goto rarp_open_err;
452	}
453	if (dlt != DLT_EN10MB) {
454		logmsg(LOG_ERR, "%s is not an ethernet", device);
455		goto rarp_open_err;
456	}
457	/*
458	 * Set filter program.
459	 */
460	if (ioctl(fd, BIOCSETF, (caddr_t)&filter) == -1) {
461		logmsg(LOG_ERR, "BIOCSETF: %m");
462		goto rarp_open_err;
463	}
464	return fd;
465
466rarp_open_err:
467	pidfile_remove(pidfile_fh);
468	exit(1);
469}
470
471/*
472 * Perform various sanity checks on the RARP request packet.  Return
473 * false on failure and log the reason.
474 */
475static int
476rarp_check(u_char *p, u_int len)
477{
478	struct ether_header *ep = (struct ether_header *)p;
479	struct ether_arp *ap = (struct ether_arp *)(p + sizeof(*ep));
480
481	if (len < sizeof(*ep) + sizeof(*ap)) {
482		logmsg(LOG_ERR, "truncated request, got %u, expected %lu",
483				len, (u_long)(sizeof(*ep) + sizeof(*ap)));
484		return 0;
485	}
486	/*
487	 * XXX This test might be better off broken out...
488	 */
489	if (ntohs(ep->ether_type) != ETHERTYPE_REVARP ||
490	    ntohs(ap->arp_hrd) != ARPHRD_ETHER ||
491	    ntohs(ap->arp_op) != REVARP_REQUEST ||
492	    ntohs(ap->arp_pro) != ETHERTYPE_IP ||
493	    ap->arp_hln != 6 || ap->arp_pln != 4) {
494		logmsg(LOG_DEBUG, "request fails sanity check");
495		return 0;
496	}
497	if (bcmp((char *)&ep->ether_shost, (char *)&ap->arp_sha, 6) != 0) {
498		logmsg(LOG_DEBUG, "ether/arp sender address mismatch");
499		return 0;
500	}
501	if (bcmp((char *)&ap->arp_sha, (char *)&ap->arp_tha, 6) != 0) {
502		logmsg(LOG_DEBUG, "ether/arp target address mismatch");
503		return 0;
504	}
505	return 1;
506}
507
508/*
509 * Loop indefinitely listening for RARP requests on the
510 * interfaces in 'iflist'.
511 */
512static void
513rarp_loop(void)
514{
515	u_char *buf, *bp, *ep;
516	int cc, fd;
517	fd_set fds, listeners;
518	int bufsize, maxfd = 0;
519	struct if_info *ii;
520
521	if (iflist == NULL) {
522		logmsg(LOG_ERR, "no interfaces");
523		goto rarpd_loop_err;
524	}
525	if (ioctl(iflist->ii_fd, BIOCGBLEN, (caddr_t)&bufsize) == -1) {
526		logmsg(LOG_ERR, "BIOCGBLEN: %m");
527		goto rarpd_loop_err;
528	}
529	buf = malloc(bufsize);
530	if (buf == NULL) {
531		logmsg(LOG_ERR, "malloc: %m");
532		goto rarpd_loop_err;
533	}
534
535	while (1) {
536		/*
537		 * Find the highest numbered file descriptor for select().
538		 * Initialize the set of descriptors to listen to.
539		 */
540		FD_ZERO(&fds);
541		for (ii = iflist; ii != NULL; ii = ii->ii_next) {
542			FD_SET(ii->ii_fd, &fds);
543			if (ii->ii_fd > maxfd)
544				maxfd = ii->ii_fd;
545		}
546		listeners = fds;
547		if (select(maxfd + 1, &listeners, NULL, NULL, NULL) == -1) {
548			/* Don't choke when we get ptraced */
549			if (errno == EINTR)
550				continue;
551			logmsg(LOG_ERR, "select: %m");
552			goto rarpd_loop_err;
553		}
554		for (ii = iflist; ii != NULL; ii = ii->ii_next) {
555			fd = ii->ii_fd;
556			if (!FD_ISSET(fd, &listeners))
557				continue;
558		again:
559			cc = read(fd, (char *)buf, bufsize);
560			/* Don't choke when we get ptraced */
561			if ((cc == -1) && (errno == EINTR))
562				goto again;
563
564			/* Loop through the packet(s) */
565#define bhp ((struct bpf_hdr *)bp)
566			bp = buf;
567			ep = bp + cc;
568			while (bp < ep) {
569				u_int caplen, hdrlen;
570
571				caplen = bhp->bh_caplen;
572				hdrlen = bhp->bh_hdrlen;
573				if (rarp_check(bp + hdrlen, caplen))
574					rarp_process(ii, bp + hdrlen, caplen);
575				bp += BPF_WORDALIGN(hdrlen + caplen);
576			}
577		}
578	}
579#undef bhp
580	return;
581
582rarpd_loop_err:
583	pidfile_remove(pidfile_fh);
584	exit(1);
585}
586
587/*
588 * True if this server can boot the host whose IP address is 'addr'.
589 * This check is made by looking in the tftp directory for the
590 * configuration file.
591 */
592static int
593rarp_bootable(in_addr_t addr)
594{
595	struct dirent *dent;
596	DIR *d;
597	char ipname[9];
598	static DIR *dd = NULL;
599
600	(void)sprintf(ipname, "%08X", (in_addr_t)ntohl(addr));
601
602	/*
603	 * If directory is already open, rewind it.  Otherwise, open it.
604	 */
605	if ((d = dd) != NULL)
606		rewinddir(d);
607	else {
608		if (chdir(tftp_dir) == -1) {
609			logmsg(LOG_ERR, "chdir: %s: %m", tftp_dir);
610			goto rarp_bootable_err;
611		}
612		d = opendir(".");
613		if (d == NULL) {
614			logmsg(LOG_ERR, "opendir: %m");
615			goto rarp_bootable_err;
616		}
617		dd = d;
618	}
619	while ((dent = readdir(d)) != NULL)
620		if (strncmp(dent->d_name, ipname, 8) == 0)
621			return 1;
622	return 0;
623
624rarp_bootable_err:
625	pidfile_remove(pidfile_fh);
626	exit(1);
627}
628
629/*
630 * Given a list of IP addresses, 'alist', return the first address that
631 * is on network 'net'; 'netmask' is a mask indicating the network portion
632 * of the address.
633 */
634static in_addr_t
635choose_ipaddr(in_addr_t **alist, in_addr_t net, in_addr_t netmask)
636{
637
638	for (; *alist; ++alist)
639		if ((**alist & netmask) == net)
640			return **alist;
641	return 0;
642}
643
644/*
645 * Answer the RARP request in 'pkt', on the interface 'ii'.  'pkt' has
646 * already been checked for validity.  The reply is overlaid on the request.
647 */
648static void
649rarp_process(struct if_info *ii, u_char *pkt, u_int len)
650{
651	struct ether_header *ep;
652	struct hostent *hp;
653	in_addr_t target_ipaddr;
654	char ename[256];
655
656	ep = (struct ether_header *)pkt;
657	/* should this be arp_tha? */
658	if (ether_ntohost(ename, (struct ether_addr *)&ep->ether_shost) != 0) {
659		logmsg(LOG_ERR, "cannot map %s to name",
660			eatoa(ep->ether_shost));
661		return;
662	}
663
664	if ((hp = gethostbyname(ename)) == NULL) {
665		logmsg(LOG_ERR, "cannot map %s to IP address", ename);
666		return;
667	}
668
669	/*
670	 * Choose correct address from list.
671	 */
672	if (hp->h_addrtype != AF_INET) {
673		logmsg(LOG_ERR, "cannot handle non IP addresses for %s",
674								ename);
675		return;
676	}
677	target_ipaddr = choose_ipaddr((in_addr_t **)hp->h_addr_list,
678				      ii->ii_ipaddr & ii->ii_netmask,
679				      ii->ii_netmask);
680	if (target_ipaddr == 0) {
681		logmsg(LOG_ERR, "cannot find %s on net %s",
682		       ename, intoa(ntohl(ii->ii_ipaddr & ii->ii_netmask)));
683		return;
684	}
685	if (sflag || rarp_bootable(target_ipaddr))
686		rarp_reply(ii, ep, target_ipaddr, len);
687	else if (verbose > 1)
688		logmsg(LOG_INFO, "%s %s at %s DENIED (not bootable)",
689		    ii->ii_ifname,
690		    eatoa(ep->ether_shost),
691		    intoa(ntohl(target_ipaddr)));
692}
693
694/*
695 * Poke the kernel arp tables with the ethernet/ip address combinataion
696 * given.  When processing a reply, we must do this so that the booting
697 * host (i.e. the guy running rarpd), won't try to ARP for the hardware
698 * address of the guy being booted (he cannot answer the ARP).
699 */
700static struct sockaddr_in sin_inarp = {
701	sizeof(struct sockaddr_in), AF_INET, 0,
702	{0},
703	{0},
704};
705
706static struct sockaddr_dl sin_dl = {
707	sizeof(struct sockaddr_dl), AF_LINK, 0, IFT_ETHER, 0, 6,
708	0, ""
709};
710
711static struct {
712	struct rt_msghdr rthdr;
713	char rtspace[512];
714} rtmsg;
715
716static void
717update_arptab(u_char *ep, in_addr_t ipaddr)
718{
719	struct timespec tp;
720	int cc;
721	struct sockaddr_in *ar, *ar2;
722	struct sockaddr_dl *ll, *ll2;
723	struct rt_msghdr *rt;
724	int xtype, xindex;
725	static pid_t pid;
726	int r;
727	static int seq;
728
729	r = socket(PF_ROUTE, SOCK_RAW, 0);
730	if (r == -1) {
731		logmsg(LOG_ERR, "raw route socket: %m");
732		pidfile_remove(pidfile_fh);
733		exit(1);
734	}
735	pid = getpid();
736
737	ar = &sin_inarp;
738	ar->sin_addr.s_addr = ipaddr;
739	ll = &sin_dl;
740	bcopy(ep, LLADDR(ll), 6);
741
742	/* Get the type and interface index */
743	rt = &rtmsg.rthdr;
744	bzero(&rtmsg, sizeof(rtmsg));
745	rt->rtm_version = RTM_VERSION;
746	rt->rtm_addrs = RTA_DST;
747	rt->rtm_type = RTM_GET;
748	rt->rtm_seq = ++seq;
749	ar2 = (struct sockaddr_in *)rtmsg.rtspace;
750	bcopy(ar, ar2, sizeof(*ar));
751	rt->rtm_msglen = sizeof(*rt) + sizeof(*ar);
752	errno = 0;
753	if ((write(r, rt, rt->rtm_msglen) == -1) && (errno != ESRCH)) {
754		logmsg(LOG_ERR, "rtmsg get write: %m");
755		close(r);
756		return;
757	}
758	do {
759		cc = read(r, rt, sizeof(rtmsg));
760	} while (cc > 0 && (rt->rtm_type != RTM_GET || rt->rtm_seq != seq ||
761	    rt->rtm_pid != pid));
762	if (cc == -1) {
763		logmsg(LOG_ERR, "rtmsg get read: %m");
764		close(r);
765		return;
766	}
767	ll2 = (struct sockaddr_dl *)((u_char *)ar2 + ar2->sin_len);
768	if (ll2->sdl_family != AF_LINK) {
769		/*
770		 * XXX I think this means the ip address is not on a
771		 * directly connected network (the family is AF_INET in
772		 * this case).
773		 */
774		logmsg(LOG_ERR, "bogus link family (%d) wrong net for %08X?\n",
775		    ll2->sdl_family, ipaddr);
776		close(r);
777		return;
778	}
779	xtype = ll2->sdl_type;
780	xindex = ll2->sdl_index;
781
782	/* Set the new arp entry */
783	bzero(rt, sizeof(rtmsg));
784	rt->rtm_version = RTM_VERSION;
785	rt->rtm_addrs = RTA_DST | RTA_GATEWAY;
786	rt->rtm_inits = RTV_EXPIRE;
787	clock_gettime(CLOCK_MONOTONIC, &tp);
788	rt->rtm_rmx.rmx_expire = tp.tv_sec + ARPSECS;
789	rt->rtm_flags = RTF_HOST | RTF_STATIC;
790	rt->rtm_type = RTM_ADD;
791	rt->rtm_seq = ++seq;
792
793	bcopy(ar, ar2, sizeof(*ar));
794
795	ll2 = (struct sockaddr_dl *)((u_char *)ar2 + sizeof(*ar2));
796	bcopy(ll, ll2, sizeof(*ll));
797	ll2->sdl_type = xtype;
798	ll2->sdl_index = xindex;
799
800	rt->rtm_msglen = sizeof(*rt) + sizeof(*ar2) + sizeof(*ll2);
801	errno = 0;
802	if ((write(r, rt, rt->rtm_msglen) == -1) && (errno != EEXIST)) {
803		logmsg(LOG_ERR, "rtmsg add write: %m");
804		close(r);
805		return;
806	}
807	do {
808		cc = read(r, rt, sizeof(rtmsg));
809	} while (cc > 0 && (rt->rtm_type != RTM_ADD || rt->rtm_seq != seq ||
810	    rt->rtm_pid != pid));
811	close(r);
812	if (cc == -1) {
813		logmsg(LOG_ERR, "rtmsg add read: %m");
814		return;
815	}
816}
817
818/*
819 * Build a reverse ARP packet and sent it out on the interface.
820 * 'ep' points to a valid REVARP_REQUEST.  The REVARP_REPLY is built
821 * on top of the request, then written to the network.
822 *
823 * RFC 903 defines the ether_arp fields as follows.  The following comments
824 * are taken (more or less) straight from this document.
825 *
826 * REVARP_REQUEST
827 *
828 * arp_sha is the hardware address of the sender of the packet.
829 * arp_spa is undefined.
830 * arp_tha is the 'target' hardware address.
831 *   In the case where the sender wishes to determine his own
832 *   protocol address, this, like arp_sha, will be the hardware
833 *   address of the sender.
834 * arp_tpa is undefined.
835 *
836 * REVARP_REPLY
837 *
838 * arp_sha is the hardware address of the responder (the sender of the
839 *   reply packet).
840 * arp_spa is the protocol address of the responder (see the note below).
841 * arp_tha is the hardware address of the target, and should be the same as
842 *   that which was given in the request.
843 * arp_tpa is the protocol address of the target, that is, the desired address.
844 *
845 * Note that the requirement that arp_spa be filled in with the responder's
846 * protocol is purely for convenience.  For instance, if a system were to use
847 * both ARP and RARP, then the inclusion of the valid protocol-hardware
848 * address pair (arp_spa, arp_sha) may eliminate the need for a subsequent
849 * ARP request.
850 */
851static void
852rarp_reply(struct if_info *ii, struct ether_header *ep, in_addr_t ipaddr,
853		u_int len)
854{
855	u_int n;
856	struct ether_arp *ap = (struct ether_arp *)(ep + 1);
857
858	update_arptab((u_char *)&ap->arp_sha, ipaddr);
859
860	/*
861	 * Build the rarp reply by modifying the rarp request in place.
862	 */
863	ap->arp_op = htons(REVARP_REPLY);
864
865#ifdef BROKEN_BPF
866	ep->ether_type = ETHERTYPE_REVARP;
867#endif
868	bcopy((char *)&ap->arp_sha, (char *)&ep->ether_dhost, 6);
869	bcopy((char *)ii->ii_eaddr, (char *)&ep->ether_shost, 6);
870	bcopy((char *)ii->ii_eaddr, (char *)&ap->arp_sha, 6);
871
872	bcopy((char *)&ipaddr, (char *)ap->arp_tpa, 4);
873	/* Target hardware is unchanged. */
874	bcopy((char *)&ii->ii_ipaddr, (char *)ap->arp_spa, 4);
875
876	/* Zero possible garbage after packet. */
877	bzero((char *)ep + (sizeof(*ep) + sizeof(*ap)),
878			len - (sizeof(*ep) + sizeof(*ap)));
879	n = write(ii->ii_fd, (char *)ep, len);
880	if (n != len)
881		logmsg(LOG_ERR, "write: only %d of %d bytes written", n, len);
882	if (verbose)
883		logmsg(LOG_INFO, "%s %s at %s REPLIED", ii->ii_ifname,
884		    eatoa(ap->arp_tha),
885		    intoa(ntohl(ipaddr)));
886}
887
888/*
889 * Get the netmask of an IP address.  This routine is used if
890 * SIOCGIFNETMASK doesn't work.
891 */
892static in_addr_t
893ipaddrtonetmask(in_addr_t addr)
894{
895
896	addr = ntohl(addr);
897	if (IN_CLASSA(addr))
898		return htonl(IN_CLASSA_NET);
899	if (IN_CLASSB(addr))
900		return htonl(IN_CLASSB_NET);
901	if (IN_CLASSC(addr))
902		return htonl(IN_CLASSC_NET);
903	logmsg(LOG_DEBUG, "unknown IP address class: %08X", addr);
904	return htonl(0xffffffff);
905}
906
907/*
908 * A faster replacement for inet_ntoa().
909 */
910static char *
911intoa(in_addr_t addr)
912{
913	char *cp;
914	u_int byte;
915	int n;
916	static char buf[sizeof(".xxx.xxx.xxx.xxx")];
917
918	cp = &buf[sizeof buf];
919	*--cp = '\0';
920
921	n = 4;
922	do {
923		byte = addr & 0xff;
924		*--cp = byte % 10 + '0';
925		byte /= 10;
926		if (byte > 0) {
927			*--cp = byte % 10 + '0';
928			byte /= 10;
929			if (byte > 0)
930				*--cp = byte + '0';
931		}
932		*--cp = '.';
933		addr >>= 8;
934	} while (--n > 0);
935
936	return cp + 1;
937}
938
939static char *
940eatoa(u_char *ea)
941{
942	static char buf[sizeof("xx:xx:xx:xx:xx:xx")];
943
944	(void)sprintf(buf, "%x:%x:%x:%x:%x:%x",
945	    ea[0], ea[1], ea[2], ea[3], ea[4], ea[5]);
946	return (buf);
947}
948
949static void
950logmsg(int pri, const char *fmt, ...)
951{
952	va_list v;
953	FILE *fp;
954	char *newfmt;
955
956	va_start(v, fmt);
957	if (dflag) {
958		if (pri == LOG_ERR)
959			fp = stderr;
960		else
961			fp = stdout;
962		if (expand_syslog_m(fmt, &newfmt) == -1) {
963			vfprintf(fp, fmt, v);
964		} else {
965			vfprintf(fp, newfmt, v);
966			free(newfmt);
967		}
968		fputs("\n", fp);
969		fflush(fp);
970	} else {
971		vsyslog(pri, fmt, v);
972	}
973	va_end(v);
974}
975
976static int
977expand_syslog_m(const char *fmt, char **newfmt) {
978	const char *str, *m;
979	char *p, *np;
980
981	p = strdup("");
982	str = fmt;
983	while ((m = strstr(str, "%m")) != NULL) {
984		asprintf(&np, "%s%.*s%s", p, (int)(m - str),
985		    str, strerror(errno));
986		free(p);
987		if (np == NULL) {
988			errno = ENOMEM;
989			return (-1);
990		}
991		p = np;
992		str = m + 2;
993	}
994
995	if (*str != '\0') {
996		asprintf(&np, "%s%s", p, str);
997		free(p);
998		if (np == NULL) {
999			errno = ENOMEM;
1000			return (-1);
1001		}
1002		p = np;
1003	}
1004
1005	*newfmt = p;
1006	return (0);
1007}
1008