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