2002-01-11 08:33:22 +03:00
|
|
|
/* $NetBSD: rarpd.c,v 1.44 2002/01/11 05:33:22 itojun Exp $ */
|
1995-11-17 12:49:19 +03:00
|
|
|
|
1993-12-16 08:31:05 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1990 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that: (1) source code distributions
|
|
|
|
* retain the above copyright notice and this paragraph in its entirety, (2)
|
|
|
|
* distributions including binary code include the above copyright notice and
|
|
|
|
* this paragraph in its entirety in the documentation or other materials
|
|
|
|
* provided with the distribution, and (3) all advertising materials mentioning
|
|
|
|
* features or use of this software display the following acknowledgement:
|
|
|
|
* ``This product includes software developed by the University of California,
|
|
|
|
* Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
|
|
|
|
* the University nor the names of its contributors may be used to endorse
|
|
|
|
* or promote products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*/
|
1997-10-17 16:40:03 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-12-16 08:31:05 +03:00
|
|
|
#ifndef lint
|
1997-10-17 16:40:03 +04:00
|
|
|
__COPYRIGHT(
|
|
|
|
"@(#) Copyright (c) 1990 The Regents of the University of California.\n\
|
|
|
|
All rights reserved.\n");
|
|
|
|
#endif /* not lint */
|
1993-12-16 08:31:05 +03:00
|
|
|
|
|
|
|
#ifndef lint
|
2002-01-11 08:33:22 +03:00
|
|
|
__RCSID("$NetBSD: rarpd.c,v 1.44 2002/01/11 05:33:22 itojun Exp $");
|
1993-12-16 08:31:05 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rarpd - Reverse ARP Daemon
|
|
|
|
*
|
1998-07-29 11:26:29 +04:00
|
|
|
* Usage: rarpd -a [-d|-f] [-l]
|
|
|
|
* rarpd [-d|-f] [-l] interface
|
1993-12-16 08:31:05 +03:00
|
|
|
*/
|
|
|
|
|
1999-09-26 14:40:32 +04:00
|
|
|
#include <sys/param.h>
|
1997-10-18 15:18:39 +04:00
|
|
|
#include <sys/file.h>
|
1993-12-16 08:31:05 +03:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
1997-10-18 15:18:39 +04:00
|
|
|
|
|
|
|
#include <net/bpf.h>
|
1993-12-16 08:31:05 +03:00
|
|
|
#include <net/if.h>
|
1994-05-26 00:01:15 +04:00
|
|
|
#include <net/if_dl.h>
|
1997-03-15 21:37:27 +03:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
#include <net/if_ether.h>
|
|
|
|
#endif
|
1994-05-26 00:01:15 +04:00
|
|
|
#include <net/if_types.h>
|
1993-12-16 08:31:05 +03:00
|
|
|
#include <netinet/in.h>
|
1997-03-15 21:37:27 +03:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
#include <netinet/if_inarp.h>
|
|
|
|
#else
|
1993-12-16 08:31:05 +03:00
|
|
|
#include <netinet/if_ether.h>
|
1997-03-15 21:37:27 +03:00
|
|
|
#endif
|
1997-10-18 15:18:39 +04:00
|
|
|
|
1993-12-16 08:31:05 +03:00
|
|
|
#include <arpa/inet.h>
|
1997-10-18 15:18:39 +04:00
|
|
|
|
1999-01-12 01:39:59 +03:00
|
|
|
#include <errno.h>
|
1993-12-16 08:31:05 +03:00
|
|
|
#include <dirent.h>
|
1997-10-18 15:18:39 +04:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
1999-06-06 06:52:16 +04:00
|
|
|
#include <util.h>
|
2000-04-13 13:12:16 +04:00
|
|
|
#ifdef HAVE_IFADDRS_H
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
#endif
|
1998-04-23 06:48:33 +04:00
|
|
|
|
1993-12-16 08:31:05 +03:00
|
|
|
#define FATAL 1 /* fatal error occurred */
|
|
|
|
#define NONFATAL 0 /* non fatal error occurred */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The structure for each interface.
|
|
|
|
*/
|
|
|
|
struct if_info {
|
|
|
|
int ii_fd; /* BPF file descriptor */
|
|
|
|
u_char ii_eaddr[6]; /* Ethernet address of this interface */
|
1998-10-06 04:23:55 +04:00
|
|
|
u_int32_t ii_ipaddr; /* IP address of this interface */
|
|
|
|
u_int32_t ii_netmask; /* subnet or net mask */
|
|
|
|
char *ii_name; /* interface name */
|
|
|
|
struct if_info *ii_alias;
|
1993-12-16 08:31:05 +03:00
|
|
|
struct if_info *ii_next;
|
|
|
|
};
|
|
|
|
/*
|
|
|
|
* The list of all interfaces that are being listened to. rarp_loop()
|
|
|
|
* "selects" on the descriptors in this list.
|
|
|
|
*/
|
|
|
|
struct if_info *iflist;
|
|
|
|
|
1997-10-17 16:40:03 +04:00
|
|
|
u_int32_t choose_ipaddr __P((u_int32_t **, u_int32_t, u_int32_t));
|
2000-10-12 00:23:46 +04:00
|
|
|
void debug __P((const char *,...))
|
|
|
|
__attribute__((__format__(__printf__, 1, 2)));
|
1997-10-17 16:40:03 +04:00
|
|
|
void init_all __P((void));
|
1998-10-06 04:23:55 +04:00
|
|
|
void init_one __P((char *, u_int32_t));
|
|
|
|
u_int32_t ipaddrtonetmask __P((u_int32_t));
|
1997-10-17 16:40:03 +04:00
|
|
|
void lookup_eaddr __P((char *, u_char *));
|
1998-10-06 04:23:55 +04:00
|
|
|
void lookup_ipaddr __P((char *, u_int32_t *, u_int32_t *));
|
1997-10-17 16:40:03 +04:00
|
|
|
int main __P((int, char **));
|
|
|
|
void rarp_loop __P((void));
|
|
|
|
int rarp_open __P((char *));
|
|
|
|
void rarp_process __P((struct if_info *, u_char *));
|
1998-10-06 04:23:55 +04:00
|
|
|
void rarp_reply __P((struct if_info *, struct ether_header *, u_int32_t,
|
1998-04-15 19:06:06 +04:00
|
|
|
struct hostent *));
|
2000-10-12 00:23:46 +04:00
|
|
|
void rarperr __P((int, const char *,...))
|
|
|
|
__attribute__((__format__(__printf__, 2, 3)));
|
1997-11-24 21:43:12 +03:00
|
|
|
|
|
|
|
#if defined(__NetBSD__)
|
|
|
|
#include "mkarp.h"
|
|
|
|
#else
|
1998-10-06 04:23:55 +04:00
|
|
|
void update_arptab __P((u_char *, u_int32_t));
|
1997-11-24 21:43:12 +03:00
|
|
|
#endif
|
|
|
|
|
1997-10-17 16:40:03 +04:00
|
|
|
void usage __P((void));
|
|
|
|
|
|
|
|
static int bpf_open __P((void));
|
|
|
|
static int rarp_check __P((u_char *, int));
|
1993-12-16 08:31:05 +03:00
|
|
|
|
1995-09-02 01:55:44 +04:00
|
|
|
#ifdef REQUIRE_TFTPBOOT
|
1998-10-06 04:23:55 +04:00
|
|
|
int rarp_bootable __P((u_int32_t));
|
1995-09-02 01:55:44 +04:00
|
|
|
#endif
|
|
|
|
|
1993-12-16 08:31:05 +03:00
|
|
|
int aflag = 0; /* listen on "all" interfaces */
|
|
|
|
int dflag = 0; /* print debugging messages */
|
|
|
|
int fflag = 0; /* don't fork */
|
1998-04-15 19:06:06 +04:00
|
|
|
int lflag = 0; /* log all replies */
|
1993-12-16 08:31:05 +03:00
|
|
|
|
1996-03-21 20:56:15 +03:00
|
|
|
int
|
1993-12-16 08:31:05 +03:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
1998-09-29 13:21:35 +04:00
|
|
|
int op;
|
1997-10-17 16:40:03 +04:00
|
|
|
char *ifname, *hostname;
|
1993-12-16 08:31:05 +03:00
|
|
|
|
|
|
|
/* All error reporting is done through syslogs. */
|
2001-01-11 04:43:25 +03:00
|
|
|
openlog("rarpd", LOG_PID, LOG_DAEMON);
|
1993-12-16 08:31:05 +03:00
|
|
|
|
|
|
|
opterr = 0;
|
1998-04-15 19:06:06 +04:00
|
|
|
while ((op = getopt(argc, argv, "adfl")) != -1) {
|
1993-12-16 08:31:05 +03:00
|
|
|
switch (op) {
|
|
|
|
case 'a':
|
|
|
|
++aflag;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
++dflag;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
++fflag;
|
|
|
|
break;
|
|
|
|
|
1998-04-15 19:06:06 +04:00
|
|
|
case 'l':
|
|
|
|
++lflag;
|
|
|
|
break;
|
|
|
|
|
1993-12-16 08:31:05 +03:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ifname = argv[optind++];
|
|
|
|
hostname = ifname ? argv[optind] : 0;
|
|
|
|
if ((aflag && ifname) || (!aflag && ifname == 0))
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if ((!fflag) && (!dflag)) {
|
1998-09-29 13:21:35 +04:00
|
|
|
if (daemon(0, 0))
|
|
|
|
rarperr(FATAL, "daemon");
|
1999-06-06 06:52:16 +04:00
|
|
|
pidfile(NULL);
|
1993-12-16 08:31:05 +03:00
|
|
|
}
|
2002-01-11 08:33:22 +03:00
|
|
|
|
|
|
|
if (aflag)
|
|
|
|
init_all();
|
|
|
|
else
|
|
|
|
init_one(ifname, INADDR_ANY);
|
|
|
|
|
1993-12-16 08:31:05 +03:00
|
|
|
rarp_loop();
|
1997-10-17 16:40:03 +04:00
|
|
|
/* NOTREACHED */
|
|
|
|
return (0);
|
1993-12-16 08:31:05 +03:00
|
|
|
}
|
1997-10-17 16:40:03 +04:00
|
|
|
|
1993-12-16 08:31:05 +03:00
|
|
|
/*
|
|
|
|
* Add 'ifname' to the interface list. Lookup its IP address and network
|
|
|
|
* mask and Ethernet address, and open a BPF file for it.
|
|
|
|
*/
|
|
|
|
void
|
1998-10-06 04:23:55 +04:00
|
|
|
init_one(ifname, ipaddr)
|
1993-12-16 08:31:05 +03:00
|
|
|
char *ifname;
|
1998-10-06 04:23:55 +04:00
|
|
|
u_int32_t ipaddr;
|
1993-12-16 08:31:05 +03:00
|
|
|
{
|
1998-10-06 04:23:55 +04:00
|
|
|
struct if_info *h;
|
1993-12-16 08:31:05 +03:00
|
|
|
struct if_info *p;
|
1998-03-23 11:44:07 +03:00
|
|
|
int fd;
|
|
|
|
|
1998-10-06 04:23:55 +04:00
|
|
|
for (h = iflist; h != NULL; h = h->ii_next) {
|
|
|
|
if (!strcmp(h->ii_name, ifname))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (h == NULL) {
|
|
|
|
fd = rarp_open(ifname);
|
|
|
|
if (fd < 0)
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
fd = h->ii_fd;
|
|
|
|
}
|
1993-12-16 08:31:05 +03:00
|
|
|
|
1994-05-26 00:01:15 +04:00
|
|
|
p = (struct if_info *)malloc(sizeof(*p));
|
1993-12-16 08:31:05 +03:00
|
|
|
if (p == 0) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "malloc: %s", strerror(errno));
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
1998-10-06 04:23:55 +04:00
|
|
|
p->ii_name = strdup(ifname);
|
|
|
|
if (p->ii_name == 0) {
|
|
|
|
rarperr(FATAL, "malloc: %s", strerror(errno));
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
if (h != NULL) {
|
|
|
|
p->ii_alias = h->ii_alias;
|
|
|
|
h->ii_alias = p;
|
|
|
|
} else {
|
|
|
|
p->ii_next = iflist;
|
|
|
|
iflist = p;
|
|
|
|
}
|
1993-12-16 08:31:05 +03:00
|
|
|
|
1998-03-23 11:44:07 +03:00
|
|
|
p->ii_fd = fd;
|
1998-10-06 04:23:55 +04:00
|
|
|
p->ii_ipaddr = ipaddr;
|
1993-12-16 08:31:05 +03:00
|
|
|
lookup_eaddr(ifname, p->ii_eaddr);
|
|
|
|
lookup_ipaddr(ifname, &p->ii_ipaddr, &p->ii_netmask);
|
|
|
|
}
|
1997-10-17 16:40:03 +04:00
|
|
|
|
1993-12-16 08:31:05 +03:00
|
|
|
/*
|
|
|
|
* Initialize all "candidate" interfaces that are in the system
|
|
|
|
* configuration list. A "candidate" is up, not loopback and not
|
|
|
|
* point to point.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
init_all()
|
|
|
|
{
|
2000-04-13 13:12:16 +04:00
|
|
|
#ifdef HAVE_IFADDRS_H
|
|
|
|
struct ifaddrs *ifap, *ifa, *p;
|
|
|
|
|
|
|
|
if (getifaddrs(&ifap) != 0) {
|
|
|
|
rarperr(FATAL, "getifaddrs: %s", strerror(errno));
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
p = NULL;
|
|
|
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
|
|
|
#define SIN(s) ((struct sockaddr_in *) (s))
|
|
|
|
if (ifa->ifa_addr->sa_family != AF_INET)
|
|
|
|
continue;
|
|
|
|
if (p && !strcmp(p->ifa_name, ifa->ifa_name) &&
|
|
|
|
SIN(p->ifa_addr)->sin_addr.s_addr == SIN(ifa->ifa_addr)->sin_addr.s_addr)
|
|
|
|
continue;
|
|
|
|
p = ifa;
|
|
|
|
if ((ifa->ifa_flags &
|
|
|
|
(IFF_UP | IFF_LOOPBACK | IFF_POINTOPOINT)) != IFF_UP)
|
|
|
|
continue;
|
|
|
|
init_one(ifa->ifa_name, SIN(ifa->ifa_addr)->sin_addr.s_addr);
|
|
|
|
#undef SIN
|
|
|
|
}
|
|
|
|
freeifaddrs(ifap);
|
|
|
|
#else
|
1998-07-13 10:31:31 +04:00
|
|
|
char inbuf[8192*2];
|
1993-12-16 08:31:05 +03:00
|
|
|
struct ifconf ifc;
|
2000-02-07 04:15:52 +03:00
|
|
|
struct ifreq ifreq, ifrd, *ifr, *ifrp;
|
1994-05-26 00:01:15 +04:00
|
|
|
int fd;
|
|
|
|
int i, len;
|
1993-12-16 08:31:05 +03:00
|
|
|
|
|
|
|
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "socket: %s", strerror(errno));
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
1994-05-26 00:01:15 +04:00
|
|
|
|
|
|
|
ifc.ifc_len = sizeof(inbuf);
|
|
|
|
ifc.ifc_buf = inbuf;
|
|
|
|
if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 ||
|
1993-12-16 08:31:05 +03:00
|
|
|
ifc.ifc_len < sizeof(struct ifreq)) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "init_all: SIOCGIFCONF: %s", strerror(errno));
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2000-02-07 04:15:52 +03:00
|
|
|
ifr = &ifrd;
|
|
|
|
ifrp = ifc.ifc_req;
|
1996-01-31 23:25:31 +03:00
|
|
|
ifreq.ifr_name[0] = '\0';
|
1994-05-26 00:01:15 +04:00
|
|
|
for (i = 0; i < ifc.ifc_len;
|
2000-02-07 04:15:52 +03:00
|
|
|
i += len, ifrp = (struct ifreq *)((caddr_t)ifrp + len)) {
|
1998-10-06 04:23:55 +04:00
|
|
|
#define SIN(s) ((struct sockaddr_in *) (s))
|
2000-02-07 04:15:52 +03:00
|
|
|
memcpy(&ifrd, ifrp, sizeof (ifrd));
|
1994-05-26 00:01:15 +04:00
|
|
|
len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
|
1998-10-06 04:23:55 +04:00
|
|
|
if (ifr->ifr_addr.sa_family != AF_INET)
|
|
|
|
continue;
|
|
|
|
if (!strncmp(ifreq.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name))
|
|
|
|
&& SIN(&ifreq.ifr_addr)->sin_addr.s_addr == SIN(&ifr->ifr_addr)->sin_addr.s_addr)
|
1996-01-31 23:25:31 +03:00
|
|
|
continue;
|
|
|
|
ifreq = *ifr;
|
1994-05-26 00:01:15 +04:00
|
|
|
if (ioctl(fd, SIOCGIFFLAGS, (caddr_t)ifr) < 0) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "init_all: SIOCGIFFLAGS: %s",
|
1994-05-26 00:01:15 +04:00
|
|
|
strerror(errno));
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
1994-05-26 00:01:15 +04:00
|
|
|
if ((ifr->ifr_flags &
|
|
|
|
(IFF_UP | IFF_LOOPBACK | IFF_POINTOPOINT)) != IFF_UP)
|
1993-12-16 08:31:05 +03:00
|
|
|
continue;
|
1998-10-06 04:23:55 +04:00
|
|
|
init_one(ifr->ifr_name, SIN(&ifreq.ifr_addr)->sin_addr.s_addr);
|
|
|
|
#undef SIN
|
1993-12-16 08:31:05 +03:00
|
|
|
}
|
1998-07-13 10:31:31 +04:00
|
|
|
(void)close(fd);
|
2000-04-13 13:12:16 +04:00
|
|
|
#endif
|
1993-12-16 08:31:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
1998-07-29 11:26:29 +04:00
|
|
|
(void) fprintf(stderr, "usage: rarpd -a [-d|-f] [-l]\n");
|
|
|
|
(void) fprintf(stderr, " rarpd [-d|-f] [-l] interface\n");
|
1993-12-16 08:31:05 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
bpf_open()
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int n = 0;
|
|
|
|
char device[sizeof "/dev/bpf000"];
|
|
|
|
|
|
|
|
/* Go through all the minors and find one that isn't in use. */
|
|
|
|
do {
|
1998-07-13 10:31:31 +04:00
|
|
|
(void)sprintf(device, "/dev/bpf%d", n++);
|
1993-12-16 08:31:05 +03:00
|
|
|
fd = open(device, O_RDWR);
|
|
|
|
} while (fd < 0 && errno == EBUSY);
|
|
|
|
|
|
|
|
if (fd < 0) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "%s: %s", device, strerror(errno));
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Open a BPF file and attach it to the interface named 'device'.
|
|
|
|
* Set immediate mode, and set a filter that accepts only RARP requests.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
rarp_open(device)
|
|
|
|
char *device;
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct ifreq ifr;
|
|
|
|
u_int dlt;
|
|
|
|
int immediate;
|
|
|
|
|
|
|
|
static struct bpf_insn insns[] = {
|
|
|
|
BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 12),
|
|
|
|
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ETHERTYPE_REVARP, 0, 3),
|
|
|
|
BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 20),
|
1994-05-15 01:57:56 +04:00
|
|
|
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ARPOP_REVREQUEST, 0, 1),
|
1997-03-15 21:37:27 +03:00
|
|
|
BPF_STMT(BPF_RET | BPF_K,
|
|
|
|
sizeof(struct arphdr) +
|
|
|
|
2 * ETHER_ADDR_LEN + 2 * sizeof(struct in_addr) +
|
1993-12-16 08:31:05 +03:00
|
|
|
sizeof(struct ether_header)),
|
|
|
|
BPF_STMT(BPF_RET | BPF_K, 0),
|
|
|
|
};
|
|
|
|
static struct bpf_program filter = {
|
|
|
|
sizeof insns / sizeof(insns[0]),
|
|
|
|
insns
|
|
|
|
};
|
|
|
|
|
|
|
|
fd = bpf_open();
|
|
|
|
|
|
|
|
/* Set immediate mode so packets are processed as they arrive. */
|
|
|
|
immediate = 1;
|
|
|
|
if (ioctl(fd, BIOCIMMEDIATE, &immediate) < 0) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "BIOCIMMEDIATE: %s", strerror(errno));
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
1998-07-13 10:31:31 +04:00
|
|
|
(void)strncpy(ifr.ifr_name, device, sizeof ifr.ifr_name - 1);
|
|
|
|
ifr.ifr_name[sizeof ifr.ifr_name - 1] = '\0';
|
1993-12-16 08:31:05 +03:00
|
|
|
if (ioctl(fd, BIOCSETIF, (caddr_t) & ifr) < 0) {
|
1998-03-23 11:44:07 +03:00
|
|
|
if (aflag) { /* for -a skip non-ethernet interfaces */
|
|
|
|
close(fd);
|
|
|
|
return(-1);
|
|
|
|
}
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "BIOCSETIF: %s", strerror(errno));
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
/* Check that the data link layer is an Ethernet; this code won't work
|
|
|
|
* with anything else. */
|
|
|
|
if (ioctl(fd, BIOCGDLT, (caddr_t) & dlt) < 0) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "BIOCGDLT: %s", strerror(errno));
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
if (dlt != DLT_EN10MB) {
|
1998-03-23 11:44:07 +03:00
|
|
|
if (aflag) { /* for -a skip non-ethernet interfaces */
|
|
|
|
close(fd);
|
|
|
|
return(-1);
|
|
|
|
}
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "%s is not an ethernet", device);
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
/* Set filter program. */
|
|
|
|
if (ioctl(fd, BIOCSETF, (caddr_t) & filter) < 0) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "BIOCSETF: %s", strerror(errno));
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Perform various sanity checks on the RARP request packet. Return
|
|
|
|
* false on failure and log the reason.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
rarp_check(p, len)
|
|
|
|
u_char *p;
|
|
|
|
int len;
|
|
|
|
{
|
|
|
|
struct ether_header *ep = (struct ether_header *) p;
|
1997-03-15 21:37:27 +03:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
struct arphdr *ap = (struct arphdr *) (p + sizeof(*ep));
|
|
|
|
#else
|
1993-12-16 08:31:05 +03:00
|
|
|
struct ether_arp *ap = (struct ether_arp *) (p + sizeof(*ep));
|
1997-03-15 21:37:27 +03:00
|
|
|
#endif
|
1993-12-16 08:31:05 +03:00
|
|
|
|
|
|
|
if (len < sizeof(*ep) + sizeof(*ap)) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(NONFATAL, "truncated request");
|
1993-12-16 08:31:05 +03:00
|
|
|
return 0;
|
|
|
|
}
|
1997-03-15 21:37:27 +03:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
/* now that we know the fixed part of the ARP hdr is there: */
|
|
|
|
if (len < sizeof(*ap) + 2 * ap->ar_hln + 2 * ap->ar_pln) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(NONFATAL, "truncated request");
|
1997-03-15 21:37:27 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
1993-12-16 08:31:05 +03:00
|
|
|
/* XXX This test might be better off broken out... */
|
1996-02-02 00:57:00 +03:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
/* BPF (incorrectly) returns this in host order. */
|
|
|
|
if (ep->ether_type != ETHERTYPE_REVARP ||
|
|
|
|
#else
|
1993-12-16 08:31:05 +03:00
|
|
|
if (ntohs (ep->ether_type) != ETHERTYPE_REVARP ||
|
1996-02-02 00:57:00 +03:00
|
|
|
#endif
|
1997-03-15 21:37:27 +03:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
ntohs (ap->ar_hrd) != ARPHRD_ETHER ||
|
|
|
|
ntohs (ap->ar_op) != ARPOP_REVREQUEST ||
|
|
|
|
ntohs (ap->ar_pro) != ETHERTYPE_IP ||
|
|
|
|
ap->ar_hln != 6 || ap->ar_pln != 4) {
|
|
|
|
#else
|
1993-12-16 08:31:05 +03:00
|
|
|
ntohs (ap->arp_hrd) != ARPHRD_ETHER ||
|
1994-05-15 01:57:56 +04:00
|
|
|
ntohs (ap->arp_op) != ARPOP_REVREQUEST ||
|
1993-12-16 08:31:05 +03:00
|
|
|
ntohs (ap->arp_pro) != ETHERTYPE_IP ||
|
|
|
|
ap->arp_hln != 6 || ap->arp_pln != 4) {
|
1997-03-15 21:37:27 +03:00
|
|
|
#endif
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(NONFATAL, "request fails sanity check");
|
1993-12-16 08:31:05 +03:00
|
|
|
return 0;
|
|
|
|
}
|
1997-03-15 21:37:27 +03:00
|
|
|
#ifdef __NetBSD__
|
1997-10-18 15:18:39 +04:00
|
|
|
if (memcmp((char *) &ep->ether_shost, ar_sha(ap), 6) != 0) {
|
1997-03-15 21:37:27 +03:00
|
|
|
#else
|
1997-10-18 15:18:39 +04:00
|
|
|
if (memcmp((char *) &ep->ether_shost, ap->arp_sha, 6) != 0) {
|
1997-03-15 21:37:27 +03:00
|
|
|
#endif
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(NONFATAL, "ether/arp sender address mismatch");
|
1993-12-16 08:31:05 +03:00
|
|
|
return 0;
|
|
|
|
}
|
1997-03-15 21:37:27 +03:00
|
|
|
#ifdef __NetBSD__
|
1997-10-18 15:18:39 +04:00
|
|
|
if (memcmp(ar_sha(ap), ar_tha(ap), 6) != 0) {
|
1997-03-15 21:37:27 +03:00
|
|
|
#else
|
1997-10-18 15:18:39 +04:00
|
|
|
if (memcmp((char *) &ap->arp_sha, (char *) &ap->arp_tha, 6) != 0) {
|
1997-03-15 21:37:27 +03:00
|
|
|
#endif
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(NONFATAL, "ether/arp target address mismatch");
|
1993-12-16 08:31:05 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop indefinitely listening for RARP requests on the
|
|
|
|
* interfaces in 'iflist'.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
rarp_loop()
|
|
|
|
{
|
|
|
|
u_char *buf, *bp, *ep;
|
|
|
|
int cc, fd;
|
|
|
|
fd_set fds, listeners;
|
|
|
|
int bufsize, maxfd = 0;
|
|
|
|
struct if_info *ii;
|
|
|
|
|
|
|
|
if (iflist == 0) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "no interfaces");
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
if (ioctl(iflist->ii_fd, BIOCGBLEN, (caddr_t) & bufsize) < 0) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "BIOCGBLEN: %s", strerror(errno));
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
buf = (u_char *) malloc((unsigned) bufsize);
|
|
|
|
if (buf == 0) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "malloc: %s", strerror(errno));
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Find the highest numbered file descriptor for select().
|
|
|
|
* Initialize the set of descriptors to listen to.
|
|
|
|
*/
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
for (ii = iflist; ii; ii = ii->ii_next) {
|
|
|
|
FD_SET(ii->ii_fd, &fds);
|
|
|
|
if (ii->ii_fd > maxfd)
|
|
|
|
maxfd = ii->ii_fd;
|
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
listeners = fds;
|
|
|
|
if (select(maxfd + 1, &listeners, (struct fd_set *) 0,
|
|
|
|
(struct fd_set *) 0, (struct timeval *) 0) < 0) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "select: %s", strerror(errno));
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
for (ii = iflist; ii; ii = ii->ii_next) {
|
|
|
|
fd = ii->ii_fd;
|
|
|
|
if (!FD_ISSET(fd, &listeners))
|
|
|
|
continue;
|
1998-04-15 19:06:06 +04:00
|
|
|
again:
|
1993-12-16 08:31:05 +03:00
|
|
|
cc = read(fd, (char *) buf, bufsize);
|
|
|
|
/* Don't choke when we get ptraced */
|
|
|
|
if (cc < 0 && errno == EINTR)
|
|
|
|
goto again;
|
|
|
|
/* Due to a SunOS bug, after 2^31 bytes, the file
|
|
|
|
* offset overflows and read fails with EINVAL. The
|
|
|
|
* lseek() to 0 will fix things. */
|
|
|
|
if (cc < 0) {
|
|
|
|
if (errno == EINVAL &&
|
1994-03-30 06:28:43 +04:00
|
|
|
(lseek(fd, 0, SEEK_CUR) + bufsize) < 0) {
|
1998-07-13 10:31:31 +04:00
|
|
|
(void)lseek(fd, 0, 0);
|
1993-12-16 08:31:05 +03:00
|
|
|
goto again;
|
|
|
|
}
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "read: %s", strerror(errno));
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
/* Loop through the packet(s) */
|
|
|
|
#define bhp ((struct bpf_hdr *)bp)
|
|
|
|
bp = buf;
|
|
|
|
ep = bp + cc;
|
|
|
|
while (bp < ep) {
|
|
|
|
register int caplen, hdrlen;
|
|
|
|
|
|
|
|
caplen = bhp->bh_caplen;
|
|
|
|
hdrlen = bhp->bh_hdrlen;
|
2000-02-11 14:25:23 +03:00
|
|
|
debug("received packet on %s", ii->ii_name);
|
|
|
|
|
1993-12-16 08:31:05 +03:00
|
|
|
if (rarp_check(bp + hdrlen, caplen))
|
|
|
|
rarp_process(ii, bp + hdrlen);
|
|
|
|
bp += BPF_WORDALIGN(hdrlen + caplen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1995-09-02 01:55:44 +04:00
|
|
|
|
|
|
|
#ifdef REQUIRE_TFTPBOOT
|
|
|
|
|
1993-12-16 08:31:05 +03:00
|
|
|
#ifndef TFTP_DIR
|
|
|
|
#define TFTP_DIR "/tftpboot"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* True if this server can boot the host whose IP address is 'addr'.
|
|
|
|
* This check is made by looking in the tftp directory for the
|
|
|
|
* configuration file.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
rarp_bootable(addr)
|
1998-10-06 04:23:55 +04:00
|
|
|
u_int32_t addr;
|
1993-12-16 08:31:05 +03:00
|
|
|
{
|
|
|
|
register struct dirent *dent;
|
|
|
|
register DIR *d;
|
|
|
|
char ipname[9];
|
|
|
|
static DIR *dd = 0;
|
|
|
|
|
1998-07-13 10:31:31 +04:00
|
|
|
(void)sprintf(ipname, "%08X", addr);
|
1993-12-16 08:31:05 +03:00
|
|
|
/* If directory is already open, rewind it. Otherwise, open it. */
|
|
|
|
if (d = dd)
|
|
|
|
rewinddir(d);
|
|
|
|
else {
|
|
|
|
if (chdir(TFTP_DIR) == -1) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "chdir: %s", strerror(errno));
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
d = opendir(".");
|
|
|
|
if (d == 0) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "opendir: %s", strerror(errno));
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
dd = d;
|
|
|
|
}
|
|
|
|
while (dent = readdir(d))
|
|
|
|
if (strncmp(dent->d_name, ipname, 8) == 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
1995-09-02 01:55:44 +04:00
|
|
|
#endif /* REQUIRE_TFTPBOOT */
|
|
|
|
|
1993-12-16 08:31:05 +03:00
|
|
|
/*
|
|
|
|
* Given a list of IP addresses, 'alist', return the first address that
|
|
|
|
* is on network 'net'; 'netmask' is a mask indicating the network portion
|
|
|
|
* of the address.
|
|
|
|
*/
|
1997-03-23 03:48:15 +03:00
|
|
|
u_int32_t
|
1993-12-16 08:31:05 +03:00
|
|
|
choose_ipaddr(alist, net, netmask)
|
1997-03-23 03:48:15 +03:00
|
|
|
u_int32_t **alist;
|
|
|
|
u_int32_t net;
|
|
|
|
u_int32_t netmask;
|
1993-12-16 08:31:05 +03:00
|
|
|
{
|
1998-07-13 10:31:31 +04:00
|
|
|
|
1993-12-16 08:31:05 +03:00
|
|
|
for (; *alist; ++alist) {
|
|
|
|
if ((**alist & netmask) == net)
|
|
|
|
return **alist;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Answer the RARP request in 'pkt', on the interface 'ii'. 'pkt' has
|
|
|
|
* already been checked for validity. The reply is overlaid on the request.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
rarp_process(ii, pkt)
|
|
|
|
struct if_info *ii;
|
|
|
|
u_char *pkt;
|
|
|
|
{
|
|
|
|
struct ether_header *ep;
|
|
|
|
struct hostent *hp;
|
1998-10-06 19:50:30 +04:00
|
|
|
u_int32_t target_ipaddr = 0;
|
1997-06-21 18:30:03 +04:00
|
|
|
char ename[MAXHOSTNAMELEN + 1];
|
1994-01-12 22:01:00 +03:00
|
|
|
struct in_addr in;
|
1993-12-16 08:31:05 +03:00
|
|
|
|
|
|
|
ep = (struct ether_header *) pkt;
|
|
|
|
|
1997-06-21 18:30:03 +04:00
|
|
|
if (ether_ntohost(ename, (struct ether_addr *)&ep->ether_shost) != 0) {
|
|
|
|
debug("no IP address for %s",
|
|
|
|
ether_ntoa((struct ether_addr *)&ep->ether_shost));
|
1993-12-16 08:31:05 +03:00
|
|
|
return;
|
1997-06-21 18:30:03 +04:00
|
|
|
}
|
|
|
|
ename[sizeof(ename)-1] = '\0';
|
|
|
|
|
|
|
|
if ((hp = gethostbyname(ename)) == 0) {
|
|
|
|
debug("gethostbyname(%s) failed: %s", ename,
|
|
|
|
hstrerror(h_errno));
|
|
|
|
return;
|
|
|
|
}
|
1993-12-16 08:31:05 +03:00
|
|
|
|
|
|
|
/* Choose correct address from list. */
|
|
|
|
if (hp->h_addrtype != AF_INET) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "cannot handle non IP addresses");
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
1999-02-13 22:58:29 +03:00
|
|
|
for (;; ii = ii->ii_alias) {
|
1998-10-06 04:23:55 +04:00
|
|
|
target_ipaddr = choose_ipaddr((u_int32_t **) hp->h_addr_list,
|
|
|
|
ii->ii_ipaddr & ii->ii_netmask, ii->ii_netmask);
|
|
|
|
if (target_ipaddr != 0)
|
|
|
|
break;
|
1999-02-13 22:58:29 +03:00
|
|
|
if (ii->ii_alias == NULL)
|
|
|
|
break;
|
1998-10-06 04:23:55 +04:00
|
|
|
}
|
1993-12-16 08:31:05 +03:00
|
|
|
|
|
|
|
if (target_ipaddr == 0) {
|
1994-01-12 22:01:00 +03:00
|
|
|
in.s_addr = ii->ii_ipaddr & ii->ii_netmask;
|
1998-09-29 13:21:35 +04:00
|
|
|
rarperr(NONFATAL, "cannot find %s on net %s",
|
1994-01-12 22:01:00 +03:00
|
|
|
ename, inet_ntoa(in));
|
1993-12-16 08:31:05 +03:00
|
|
|
return;
|
|
|
|
}
|
1995-09-02 01:55:44 +04:00
|
|
|
#ifdef REQUIRE_TFTPBOOT
|
1993-12-16 08:31:05 +03:00
|
|
|
if (rarp_bootable(htonl(target_ipaddr)))
|
1995-09-02 01:55:44 +04:00
|
|
|
#endif
|
1998-04-15 19:06:06 +04:00
|
|
|
rarp_reply(ii, ep, target_ipaddr, hp);
|
1997-06-21 18:30:03 +04:00
|
|
|
#ifdef REQUIRE_TFTPBOOT
|
|
|
|
else
|
|
|
|
debug("%08X not bootable", htonl(target_ipaddr));
|
|
|
|
#endif
|
1993-12-16 08:31:05 +03:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Lookup the ethernet address of the interface attached to the BPF
|
|
|
|
* file descriptor 'fd'; return it in 'eaddr'.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
lookup_eaddr(ifname, eaddr)
|
|
|
|
char *ifname;
|
|
|
|
u_char *eaddr;
|
|
|
|
{
|
2000-04-13 13:12:16 +04:00
|
|
|
#ifdef HAVE_IFADDRS_H
|
|
|
|
struct ifaddrs *ifap, *ifa;
|
|
|
|
struct sockaddr_dl *sdl;
|
|
|
|
|
|
|
|
if (getifaddrs(&ifap) != 0) {
|
|
|
|
rarperr(FATAL, "getifaddrs: %s", strerror(errno));
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
|
|
|
sdl = (struct sockaddr_dl *)ifa->ifa_addr;
|
|
|
|
if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER ||
|
|
|
|
sdl->sdl_alen != 6)
|
|
|
|
continue;
|
|
|
|
if (!strcmp(ifa->ifa_name, ifname)) {
|
|
|
|
memmove((caddr_t)eaddr, (caddr_t)LLADDR(sdl), 6);
|
|
|
|
debug("%s: %x:%x:%x:%x:%x:%x",
|
|
|
|
ifa->ifa_name, eaddr[0], eaddr[1],
|
|
|
|
eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
|
|
|
|
freeifaddrs(ifap);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rarperr(FATAL, "lookup_eaddr: Never saw interface `%s'!", ifname);
|
|
|
|
freeifaddrs(ifap);
|
|
|
|
#else
|
1998-07-13 10:31:31 +04:00
|
|
|
char inbuf[8192*2];
|
1993-12-16 08:31:05 +03:00
|
|
|
struct ifconf ifc;
|
|
|
|
struct ifreq *ifr;
|
1994-05-26 00:01:15 +04:00
|
|
|
struct sockaddr_dl *sdl;
|
1993-12-16 08:31:05 +03:00
|
|
|
int fd;
|
|
|
|
int i, len;
|
|
|
|
|
|
|
|
/* We cannot use SIOCGIFADDR on the BPF descriptor.
|
|
|
|
We must instead get all the interfaces with SIOCGIFCONF
|
|
|
|
and find the right one. */
|
|
|
|
|
|
|
|
/* Use datagram socket to get Ethernet address. */
|
|
|
|
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "socket: %s", strerror(errno));
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
1994-05-26 00:01:15 +04:00
|
|
|
ifc.ifc_len = sizeof(inbuf);
|
1993-12-16 08:31:05 +03:00
|
|
|
ifc.ifc_buf = inbuf;
|
1994-05-26 00:01:15 +04:00
|
|
|
if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 ||
|
|
|
|
ifc.ifc_len < sizeof(struct ifreq)) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "lookup_eaddr: SIOGIFCONF: %s", strerror(errno));
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
ifr = ifc.ifc_req;
|
1994-05-26 00:01:15 +04:00
|
|
|
for (i = 0; i < ifc.ifc_len;
|
|
|
|
i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) {
|
|
|
|
len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
|
|
|
|
sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
|
|
|
|
if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER ||
|
|
|
|
sdl->sdl_alen != 6)
|
|
|
|
continue;
|
|
|
|
if (!strncmp(ifr->ifr_name, ifname, sizeof(ifr->ifr_name))) {
|
1997-10-18 15:18:39 +04:00
|
|
|
memmove((caddr_t)eaddr, (caddr_t)LLADDR(sdl), 6);
|
1997-06-21 18:30:03 +04:00
|
|
|
debug("%s: %x:%x:%x:%x:%x:%x",
|
|
|
|
ifr->ifr_name, eaddr[0], eaddr[1],
|
|
|
|
eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
|
1993-12-16 08:31:05 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "lookup_eaddr: Never saw interface `%s'!", ifname);
|
2000-04-13 13:12:16 +04:00
|
|
|
#endif
|
1993-12-16 08:31:05 +03:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Lookup the IP address and network mask of the interface named 'ifname'.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
lookup_ipaddr(ifname, addrp, netmaskp)
|
|
|
|
char *ifname;
|
1998-10-06 04:23:55 +04:00
|
|
|
u_int32_t *addrp;
|
|
|
|
u_int32_t *netmaskp;
|
1993-12-16 08:31:05 +03:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* Use datagram socket to get IP address. */
|
|
|
|
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "socket: %s", strerror(errno));
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
1998-10-06 04:23:55 +04:00
|
|
|
if (*addrp == INADDR_ANY) {
|
|
|
|
struct ifreq ifr;
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
(void)strncpy(ifr.ifr_name, ifname, sizeof ifr.ifr_name);
|
|
|
|
if (ioctl(fd, SIOCGIFADDR, (char *) &ifr) < 0) {
|
|
|
|
rarperr(FATAL, "SIOCGIFADDR: %s", strerror(errno));
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
*addrp = ((struct sockaddr_in *) & ifr.ifr_addr)->sin_addr.s_addr;
|
|
|
|
if (ioctl(fd, SIOCGIFNETMASK, (char *) &ifr) < 0) {
|
|
|
|
perror("SIOCGIFNETMASK");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
*netmaskp = ((struct sockaddr_in *) & ifr.ifr_addr)->sin_addr.s_addr;
|
|
|
|
} else {
|
|
|
|
struct ifaliasreq ifra;
|
|
|
|
memset(&ifra, 0, sizeof(ifra));
|
|
|
|
(void)strncpy(ifra.ifra_name, ifname, sizeof ifra.ifra_name);
|
|
|
|
((struct sockaddr_in *) & ifra.ifra_addr)->sin_family = AF_INET;
|
|
|
|
((struct sockaddr_in *) & ifra.ifra_addr)->sin_addr.s_addr = *addrp;
|
|
|
|
if (ioctl(fd, SIOCGIFALIAS, (char *) &ifra) < 0) {
|
|
|
|
rarperr(FATAL, "SIOCGIFALIAS: %s", strerror(errno));
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
*addrp = ((struct sockaddr_in *) & ifra.ifra_addr)->sin_addr.s_addr;
|
|
|
|
*netmaskp = ((struct sockaddr_in *) & ifra.ifra_mask)->sin_addr.s_addr;
|
1993-12-16 08:31:05 +03:00
|
|
|
}
|
|
|
|
/* If SIOCGIFNETMASK didn't work, figure out a mask from the IP
|
|
|
|
* address class. */
|
|
|
|
if (*netmaskp == 0)
|
|
|
|
*netmaskp = ipaddrtonetmask(*addrp);
|
|
|
|
|
1998-07-13 10:31:31 +04:00
|
|
|
(void)close(fd);
|
1993-12-16 08:31:05 +03:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Poke the kernel arp tables with the ethernet/ip address combinataion
|
|
|
|
* given. When processing a reply, we must do this so that the booting
|
|
|
|
* host (i.e. the guy running rarpd), won't try to ARP for the hardware
|
|
|
|
* address of the guy being booted (he cannot answer the ARP).
|
|
|
|
*/
|
1997-11-24 21:43:12 +03:00
|
|
|
#ifndef __NetBSD__
|
1993-12-16 08:31:05 +03:00
|
|
|
void
|
|
|
|
update_arptab(ep, ipaddr)
|
|
|
|
u_char *ep;
|
1998-10-06 04:23:55 +04:00
|
|
|
u_int32_t ipaddr;
|
1993-12-16 08:31:05 +03:00
|
|
|
{
|
|
|
|
struct arpreq request;
|
|
|
|
struct sockaddr_in *sin;
|
|
|
|
|
|
|
|
request.arp_flags = 0;
|
|
|
|
sin = (struct sockaddr_in *) & request.arp_pa;
|
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
sin->sin_addr.s_addr = ipaddr;
|
|
|
|
request.arp_ha.sa_family = AF_UNSPEC;
|
|
|
|
/* This is needed #if defined(COMPAT_43) && BYTE_ORDER != BIG_ENDIAN,
|
|
|
|
because AF_UNSPEC is zero and the kernel assumes that a zero
|
|
|
|
sa_family means that the real sa_family value is in sa_len. */
|
|
|
|
request.arp_ha.sa_len = 16; /* XXX */
|
1997-10-18 15:18:39 +04:00
|
|
|
memmove((char *) request.arp_ha.sa_data, (char *)ep, 6);
|
1993-12-16 08:31:05 +03:00
|
|
|
|
1994-05-26 00:01:15 +04:00
|
|
|
#if 0
|
1993-12-16 08:31:05 +03:00
|
|
|
s = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (ioctl(s, SIOCSARP, (caddr_t) & request) < 0) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(NONFATAL, "SIOCSARP: %s", strerror(errno));
|
1993-12-16 08:31:05 +03:00
|
|
|
}
|
1998-07-13 10:31:31 +04:00
|
|
|
(void)close(s);
|
1994-05-26 00:01:15 +04:00
|
|
|
#endif
|
1993-12-16 08:31:05 +03:00
|
|
|
}
|
1997-11-24 21:43:12 +03:00
|
|
|
#endif
|
|
|
|
|
1993-12-16 08:31:05 +03:00
|
|
|
/*
|
|
|
|
* Build a reverse ARP packet and sent it out on the interface.
|
1994-05-15 01:57:56 +04:00
|
|
|
* 'ep' points to a valid ARPOP_REVREQUEST. The ARPOP_REVREPLY is built
|
1993-12-16 08:31:05 +03:00
|
|
|
* on top of the request, then written to the network.
|
|
|
|
*
|
|
|
|
* RFC 903 defines the ether_arp fields as follows. The following comments
|
|
|
|
* are taken (more or less) straight from this document.
|
|
|
|
*
|
1994-05-15 01:57:56 +04:00
|
|
|
* ARPOP_REVREQUEST
|
1993-12-16 08:31:05 +03:00
|
|
|
*
|
|
|
|
* arp_sha is the hardware address of the sender of the packet.
|
|
|
|
* arp_spa is undefined.
|
|
|
|
* arp_tha is the 'target' hardware address.
|
|
|
|
* In the case where the sender wishes to determine his own
|
|
|
|
* protocol address, this, like arp_sha, will be the hardware
|
|
|
|
* address of the sender.
|
|
|
|
* arp_tpa is undefined.
|
|
|
|
*
|
1994-05-15 01:57:56 +04:00
|
|
|
* ARPOP_REVREPLY
|
1993-12-16 08:31:05 +03:00
|
|
|
*
|
|
|
|
* arp_sha is the hardware address of the responder (the sender of the
|
|
|
|
* reply packet).
|
|
|
|
* arp_spa is the protocol address of the responder (see the note below).
|
|
|
|
* arp_tha is the hardware address of the target, and should be the same as
|
|
|
|
* that which was given in the request.
|
|
|
|
* arp_tpa is the protocol address of the target, that is, the desired address.
|
|
|
|
*
|
|
|
|
* Note that the requirement that arp_spa be filled in with the responder's
|
|
|
|
* protocol is purely for convenience. For instance, if a system were to use
|
|
|
|
* both ARP and RARP, then the inclusion of the valid protocol-hardware
|
|
|
|
* address pair (arp_spa, arp_sha) may eliminate the need for a subsequent
|
|
|
|
* ARP request.
|
|
|
|
*/
|
|
|
|
void
|
1998-04-15 19:06:06 +04:00
|
|
|
rarp_reply(ii, ep, ipaddr, hp)
|
1993-12-16 08:31:05 +03:00
|
|
|
struct if_info *ii;
|
|
|
|
struct ether_header *ep;
|
1998-10-06 04:23:55 +04:00
|
|
|
u_int32_t ipaddr;
|
1998-04-15 19:06:06 +04:00
|
|
|
struct hostent *hp;
|
1993-12-16 08:31:05 +03:00
|
|
|
{
|
|
|
|
int n;
|
1997-03-15 21:37:27 +03:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
struct arphdr *ap = (struct arphdr *) (ep + 1);
|
|
|
|
#else
|
1993-12-16 08:31:05 +03:00
|
|
|
struct ether_arp *ap = (struct ether_arp *) (ep + 1);
|
1997-03-15 21:37:27 +03:00
|
|
|
#endif
|
|
|
|
|
1993-12-16 08:31:05 +03:00
|
|
|
int len;
|
|
|
|
|
1997-03-15 21:37:27 +03:00
|
|
|
#ifdef __NetBSD__
|
1997-11-24 21:43:12 +03:00
|
|
|
(void)mkarp(ar_sha(ap), ipaddr);
|
1997-03-15 21:37:27 +03:00
|
|
|
#else
|
1993-12-16 08:31:05 +03:00
|
|
|
update_arptab((u_char *) & ap->arp_sha, ipaddr);
|
1997-03-15 21:37:27 +03:00
|
|
|
#endif
|
1993-12-16 08:31:05 +03:00
|
|
|
|
|
|
|
/* Build the rarp reply by modifying the rarp request in place. */
|
1996-02-02 00:57:00 +03:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
/* BPF (incorrectly) wants this in host order. */
|
|
|
|
ep->ether_type = ETHERTYPE_REVARP;
|
|
|
|
#else
|
1994-01-24 04:46:29 +03:00
|
|
|
ep->ether_type = htons(ETHERTYPE_REVARP);
|
1996-02-02 00:57:00 +03:00
|
|
|
#endif
|
1997-03-15 21:37:27 +03:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
ap->ar_hrd = htons(ARPHRD_ETHER);
|
|
|
|
ap->ar_pro = htons(ETHERTYPE_IP);
|
|
|
|
ap->ar_op = htons(ARPOP_REVREPLY);
|
|
|
|
|
1997-10-18 15:18:39 +04:00
|
|
|
memmove((char *) &ep->ether_dhost, ar_sha(ap), 6);
|
|
|
|
memmove((char *) &ep->ether_shost, (char *) ii->ii_eaddr, 6);
|
|
|
|
memmove(ar_sha(ap), (char *) ii->ii_eaddr, 6);
|
1997-03-15 21:37:27 +03:00
|
|
|
|
1997-10-18 15:18:39 +04:00
|
|
|
memmove(ar_tpa(ap), (char *) &ipaddr, 4);
|
1997-03-15 21:37:27 +03:00
|
|
|
/* Target hardware is unchanged. */
|
1997-10-18 15:18:39 +04:00
|
|
|
memmove(ar_spa(ap), (char *) &ii->ii_ipaddr, 4);
|
1997-03-15 21:37:27 +03:00
|
|
|
|
|
|
|
len = sizeof(*ep) + sizeof(*ap) +
|
|
|
|
2 * ap->ar_pln + 2 * ap->ar_hln;
|
|
|
|
#else
|
1994-03-01 09:46:42 +03:00
|
|
|
ap->ea_hdr.ar_hrd = htons(ARPHRD_ETHER);
|
|
|
|
ap->ea_hdr.ar_pro = htons(ETHERTYPE_IP);
|
1994-05-15 01:57:56 +04:00
|
|
|
ap->arp_op = htons(ARPOP_REVREPLY);
|
1993-12-16 08:31:05 +03:00
|
|
|
|
1997-10-18 15:18:39 +04:00
|
|
|
memmove((char *) &ep->ether_dhost, (char *) &ap->arp_sha, 6);
|
|
|
|
memmove((char *) &ep->ether_shost, (char *) ii->ii_eaddr, 6);
|
|
|
|
memmove((char *) &ap->arp_sha, (char *) ii->ii_eaddr, 6);
|
1993-12-16 08:31:05 +03:00
|
|
|
|
1997-10-18 15:18:39 +04:00
|
|
|
memmove((char *) ap->arp_tpa, (char *) &ipaddr, 4);
|
1993-12-16 08:31:05 +03:00
|
|
|
/* Target hardware is unchanged. */
|
1997-10-18 15:18:39 +04:00
|
|
|
memmove((char *) ap->arp_spa, (char *) &ii->ii_ipaddr, 4);
|
1993-12-16 08:31:05 +03:00
|
|
|
|
|
|
|
len = sizeof(*ep) + sizeof(*ap);
|
1997-03-15 21:37:27 +03:00
|
|
|
#endif
|
|
|
|
|
2000-11-20 17:59:30 +03:00
|
|
|
debug("%s asked; %s replied",
|
|
|
|
ether_ntoa((struct ether_addr *)ar_tha(ap)), hp->h_name);
|
1998-04-15 19:06:06 +04:00
|
|
|
if (lflag)
|
2000-11-20 17:59:30 +03:00
|
|
|
syslog(LOG_INFO, "%s asked; %s replied",
|
|
|
|
ether_ntoa((struct ether_addr *)ar_tha(ap)), hp->h_name);
|
1993-12-16 08:31:05 +03:00
|
|
|
n = write(ii->ii_fd, (char *) ep, len);
|
|
|
|
if (n != len) {
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(NONFATAL, "write: only %d of %d bytes written", n, len);
|
1993-12-16 08:31:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Get the netmask of an IP address. This routine is used if
|
|
|
|
* SIOCGIFNETMASK doesn't work.
|
|
|
|
*/
|
1998-10-06 04:23:55 +04:00
|
|
|
u_int32_t
|
1993-12-16 08:31:05 +03:00
|
|
|
ipaddrtonetmask(addr)
|
1998-10-06 04:23:55 +04:00
|
|
|
u_int32_t addr;
|
1993-12-16 08:31:05 +03:00
|
|
|
{
|
1998-07-13 10:31:31 +04:00
|
|
|
|
1993-12-16 08:31:05 +03:00
|
|
|
if (IN_CLASSA(addr))
|
|
|
|
return IN_CLASSA_NET;
|
|
|
|
if (IN_CLASSB(addr))
|
|
|
|
return IN_CLASSB_NET;
|
|
|
|
if (IN_CLASSC(addr))
|
|
|
|
return IN_CLASSC_NET;
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(FATAL, "unknown IP address class: %08X", addr);
|
1993-12-16 08:31:05 +03:00
|
|
|
/* NOTREACHED */
|
1997-10-17 16:40:03 +04:00
|
|
|
return(-1);
|
1993-12-16 08:31:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#if __STDC__
|
|
|
|
#include <stdarg.h>
|
|
|
|
#else
|
|
|
|
#include <varargs.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
#if __STDC__
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(int fatal, const char *fmt,...)
|
1993-12-16 08:31:05 +03:00
|
|
|
#else
|
1997-10-17 16:40:03 +04:00
|
|
|
rarperr(fmt, va_alist)
|
1993-12-16 08:31:05 +03:00
|
|
|
int fatal;
|
|
|
|
char *fmt;
|
|
|
|
va_dcl
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
va_list ap;
|
1998-07-13 10:31:31 +04:00
|
|
|
|
1993-12-16 08:31:05 +03:00
|
|
|
#if __STDC__
|
|
|
|
va_start(ap, fmt);
|
|
|
|
#else
|
|
|
|
va_start(ap);
|
|
|
|
#endif
|
|
|
|
if (dflag) {
|
|
|
|
if (fatal)
|
1998-07-13 10:31:31 +04:00
|
|
|
(void)fprintf(stderr, "rarpd: error: ");
|
1993-12-16 08:31:05 +03:00
|
|
|
else
|
1998-07-13 10:31:31 +04:00
|
|
|
(void)fprintf(stderr, "rarpd: warning: ");
|
|
|
|
(void)vfprintf(stderr, fmt, ap);
|
2001-09-24 17:22:25 +04:00
|
|
|
va_end(ap);
|
|
|
|
#if __STDC__
|
|
|
|
va_start(ap, fmt);
|
|
|
|
#else
|
|
|
|
va_start(ap);
|
|
|
|
#endif
|
1998-07-13 10:31:31 +04:00
|
|
|
(void)fprintf(stderr, "\n");
|
1993-12-16 08:31:05 +03:00
|
|
|
}
|
|
|
|
vsyslog(LOG_ERR, fmt, ap);
|
|
|
|
va_end(ap);
|
1999-06-06 06:52:16 +04:00
|
|
|
if (fatal)
|
1993-12-16 08:31:05 +03:00
|
|
|
exit(1);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
#if __STDC__
|
|
|
|
debug(const char *fmt,...)
|
|
|
|
#else
|
|
|
|
debug(fmt, va_alist)
|
|
|
|
char *fmt;
|
|
|
|
va_dcl
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
#if __STDC__
|
1997-06-21 18:30:03 +04:00
|
|
|
va_start(ap, fmt);
|
1993-12-16 08:31:05 +03:00
|
|
|
#else
|
1997-06-21 18:30:03 +04:00
|
|
|
va_start(ap);
|
1993-12-16 08:31:05 +03:00
|
|
|
#endif
|
1997-06-21 18:30:03 +04:00
|
|
|
if (dflag) {
|
1998-07-13 10:31:31 +04:00
|
|
|
(void)fprintf(stderr, "rarpd: ");
|
|
|
|
(void)vfprintf(stderr, fmt, ap);
|
2001-09-24 17:22:25 +04:00
|
|
|
va_end(ap);
|
|
|
|
#if __STDC__
|
|
|
|
va_start(ap, fmt);
|
|
|
|
#else
|
|
|
|
va_start(ap);
|
|
|
|
#endif
|
1998-07-13 10:31:31 +04:00
|
|
|
(void)fprintf(stderr, "\n");
|
1993-12-16 08:31:05 +03:00
|
|
|
}
|
1997-06-21 18:30:03 +04:00
|
|
|
vsyslog(LOG_WARNING, fmt, ap);
|
|
|
|
va_end(ap);
|
1993-12-16 08:31:05 +03:00
|
|
|
}
|