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