2000-03-30 16:19:46 +04:00
|
|
|
/* $NetBSD: rarp.c,v 1.20 2000/03/30 12:19:48 augustss Exp $ */
|
1994-10-26 08:44:32 +03:00
|
|
|
|
1994-08-04 23:39:32 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1992 Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed by the Computer Systems Engineering group
|
|
|
|
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
|
|
|
* contributed to Berkeley.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Lawrence Berkeley Laboratory and its contributors.
|
|
|
|
* 4. 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 BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1994-10-26 08:44:32 +03:00
|
|
|
* @(#) Header: arp.c,v 1.5 93/07/15 05:52:26 leres Exp (LBL)
|
1994-08-04 23:39:32 +04:00
|
|
|
*/
|
1994-05-08 20:11:14 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <net/if.h>
|
1997-07-07 19:52:49 +04:00
|
|
|
#include <net/if_ether.h>
|
1994-05-08 20:11:14 +04:00
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
|
1999-11-14 00:17:56 +03:00
|
|
|
#ifdef _STANDALONE
|
|
|
|
#include <lib/libkern/libkern.h>
|
|
|
|
#else
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
1994-05-08 20:11:14 +04:00
|
|
|
#include "stand.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "netif.h"
|
|
|
|
|
1997-07-07 19:52:49 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ethernet Address Resolution Protocol.
|
|
|
|
*
|
|
|
|
* See RFC 826 for protocol description. Structure below is adapted
|
|
|
|
* to resolving internet addresses. Field names used correspond to
|
|
|
|
* RFC 826.
|
|
|
|
*/
|
|
|
|
struct ether_arp {
|
|
|
|
struct arphdr ea_hdr; /* fixed-size header */
|
|
|
|
u_int8_t arp_sha[ETHER_ADDR_LEN]; /* sender hardware address */
|
|
|
|
u_int8_t arp_spa[4]; /* sender protocol address */
|
|
|
|
u_int8_t arp_tha[ETHER_ADDR_LEN]; /* target hardware address */
|
|
|
|
u_int8_t arp_tpa[4]; /* target protocol address */
|
|
|
|
};
|
|
|
|
#define arp_hrd ea_hdr.ar_hrd
|
|
|
|
#define arp_pro ea_hdr.ar_pro
|
|
|
|
#define arp_hln ea_hdr.ar_hln
|
|
|
|
#define arp_pln ea_hdr.ar_pln
|
|
|
|
#define arp_op ea_hdr.ar_op
|
|
|
|
|
1995-09-15 03:45:21 +04:00
|
|
|
static ssize_t rarpsend __P((struct iodesc *, void *, size_t));
|
|
|
|
static ssize_t rarprecv __P((struct iodesc *, void *, size_t, time_t));
|
1994-05-08 20:11:14 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ethernet (Reverse) Address Resolution Protocol (see RFC 903, and 826).
|
|
|
|
*/
|
1995-09-19 01:19:18 +04:00
|
|
|
int
|
1994-05-08 20:11:14 +04:00
|
|
|
rarp_getipaddress(sock)
|
|
|
|
int sock;
|
|
|
|
{
|
|
|
|
struct iodesc *d;
|
2000-03-30 16:19:46 +04:00
|
|
|
struct ether_arp *ap;
|
1994-05-08 20:11:14 +04:00
|
|
|
struct {
|
1995-06-27 19:27:24 +04:00
|
|
|
u_char header[ETHER_SIZE];
|
|
|
|
struct {
|
|
|
|
struct ether_arp arp;
|
|
|
|
u_char pad[18]; /* 60 - sizeof(arp) */
|
|
|
|
} data;
|
1994-05-08 20:11:14 +04:00
|
|
|
} wbuf;
|
1995-02-20 14:04:00 +03:00
|
|
|
struct {
|
1995-06-27 19:27:24 +04:00
|
|
|
u_char header[ETHER_SIZE];
|
|
|
|
struct {
|
|
|
|
struct ether_arp arp;
|
|
|
|
u_char pad[24]; /* extra space */
|
|
|
|
} data;
|
1994-05-08 20:11:14 +04:00
|
|
|
} rbuf;
|
|
|
|
|
|
|
|
#ifdef RARP_DEBUG
|
|
|
|
if (debug)
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("rarp: socket=%d\n", sock);
|
1994-05-08 20:11:14 +04:00
|
|
|
#endif
|
|
|
|
if (!(d = socktodesc(sock))) {
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("rarp: bad socket. %d\n", sock);
|
1995-09-19 01:19:18 +04:00
|
|
|
return (-1);
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
|
|
|
#ifdef RARP_DEBUG
|
|
|
|
if (debug)
|
1999-11-12 16:12:09 +03:00
|
|
|
printf("rarp: d=%lx\n", (u_long)d);
|
1994-05-08 20:11:14 +04:00
|
|
|
#endif
|
|
|
|
|
1995-06-27 19:27:24 +04:00
|
|
|
bzero((char*)&wbuf.data, sizeof(wbuf.data));
|
|
|
|
ap = &wbuf.data.arp;
|
1994-05-08 20:11:14 +04:00
|
|
|
ap->arp_hrd = htons(ARPHRD_ETHER);
|
|
|
|
ap->arp_pro = htons(ETHERTYPE_IP);
|
|
|
|
ap->arp_hln = sizeof(ap->arp_sha); /* hardware address length */
|
|
|
|
ap->arp_pln = sizeof(ap->arp_spa); /* protocol address length */
|
1995-06-27 19:27:24 +04:00
|
|
|
ap->arp_op = htons(ARPOP_REVREQUEST);
|
1994-05-08 20:11:14 +04:00
|
|
|
bcopy(d->myea, ap->arp_sha, 6);
|
|
|
|
bcopy(d->myea, ap->arp_tha, 6);
|
|
|
|
|
|
|
|
if (sendrecv(d,
|
1995-06-27 19:27:24 +04:00
|
|
|
rarpsend, &wbuf.data, sizeof(wbuf.data),
|
1995-09-23 07:36:06 +04:00
|
|
|
rarprecv, &rbuf.data, sizeof(rbuf.data)) < 0)
|
|
|
|
{
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("No response for RARP request\n");
|
1995-09-19 01:19:18 +04:00
|
|
|
return (-1);
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
|
|
|
|
1995-09-23 07:36:06 +04:00
|
|
|
ap = &rbuf.data.arp;
|
|
|
|
bcopy(ap->arp_tpa, (char *)&myip, sizeof(myip));
|
|
|
|
#if 0
|
|
|
|
/* XXX - Can NOT assume this is our root server! */
|
|
|
|
bcopy(ap->arp_spa, (char *)&rootip, sizeof(rootip));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Compute our "natural" netmask. */
|
|
|
|
if (IN_CLASSA(myip.s_addr))
|
|
|
|
netmask = IN_CLASSA_NET;
|
|
|
|
else if (IN_CLASSB(myip.s_addr))
|
|
|
|
netmask = IN_CLASSB_NET;
|
|
|
|
else
|
|
|
|
netmask = IN_CLASSC_NET;
|
|
|
|
|
|
|
|
d->myip = myip;
|
1995-09-19 01:19:18 +04:00
|
|
|
return (0);
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Broadcast a RARP request (i.e. who knows who I am)
|
|
|
|
*/
|
1995-09-15 03:45:21 +04:00
|
|
|
static ssize_t
|
1994-05-08 20:11:14 +04:00
|
|
|
rarpsend(d, pkt, len)
|
2000-03-30 16:19:46 +04:00
|
|
|
struct iodesc *d;
|
|
|
|
void *pkt;
|
|
|
|
size_t len;
|
1994-05-08 20:11:14 +04:00
|
|
|
{
|
1995-02-20 14:04:00 +03:00
|
|
|
|
1994-05-08 20:11:14 +04:00
|
|
|
#ifdef RARP_DEBUG
|
|
|
|
if (debug)
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("rarpsend: called\n");
|
1994-05-08 20:11:14 +04:00
|
|
|
#endif
|
1995-02-20 14:04:00 +03:00
|
|
|
|
1994-05-08 20:11:14 +04:00
|
|
|
return (sendether(d, pkt, len, bcea, ETHERTYPE_REVARP));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1995-09-23 07:36:06 +04:00
|
|
|
* Returns 0 if this is the packet we're waiting for
|
|
|
|
* else -1 (and errno == 0)
|
1994-05-08 20:11:14 +04:00
|
|
|
*/
|
1995-09-15 03:45:21 +04:00
|
|
|
static ssize_t
|
1995-02-20 14:04:00 +03:00
|
|
|
rarprecv(d, pkt, len, tleft)
|
2000-03-30 16:19:46 +04:00
|
|
|
struct iodesc *d;
|
|
|
|
void *pkt;
|
|
|
|
size_t len;
|
1995-02-20 14:04:00 +03:00
|
|
|
time_t tleft;
|
1994-05-08 20:11:14 +04:00
|
|
|
{
|
2000-03-30 16:19:46 +04:00
|
|
|
ssize_t n;
|
|
|
|
struct ether_arp *ap;
|
1995-09-12 01:11:36 +04:00
|
|
|
u_int16_t etype; /* host order */
|
1994-05-08 20:11:14 +04:00
|
|
|
|
|
|
|
#ifdef RARP_DEBUG
|
|
|
|
if (debug)
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("rarprecv: ");
|
1994-05-08 20:11:14 +04:00
|
|
|
#endif
|
|
|
|
|
1995-09-15 03:45:21 +04:00
|
|
|
n = readether(d, pkt, len, tleft, &etype);
|
1995-09-23 07:36:06 +04:00
|
|
|
errno = 0; /* XXX */
|
1995-09-15 03:45:21 +04:00
|
|
|
if (n == -1 || n < sizeof(struct ether_arp)) {
|
1995-06-27 19:27:24 +04:00
|
|
|
#ifdef RARP_DEBUG
|
|
|
|
if (debug)
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("bad len=%d\n", n);
|
1995-06-27 19:27:24 +04:00
|
|
|
#endif
|
1995-09-23 07:36:06 +04:00
|
|
|
return (-1);
|
1995-06-27 19:27:24 +04:00
|
|
|
}
|
1995-02-20 14:04:00 +03:00
|
|
|
|
1995-09-12 01:11:36 +04:00
|
|
|
if (etype != ETHERTYPE_REVARP) {
|
1995-06-27 19:27:24 +04:00
|
|
|
#ifdef RARP_DEBUG
|
|
|
|
if (debug)
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("bad type=0x%x\n", etype);
|
1995-06-27 19:27:24 +04:00
|
|
|
#endif
|
1995-09-23 07:36:06 +04:00
|
|
|
return (-1);
|
1995-06-27 19:27:24 +04:00
|
|
|
}
|
1994-05-08 20:11:14 +04:00
|
|
|
|
1995-02-20 14:04:00 +03:00
|
|
|
ap = (struct ether_arp *)pkt;
|
1995-09-23 07:36:06 +04:00
|
|
|
if (ap->arp_hrd != htons(ARPHRD_ETHER) ||
|
|
|
|
ap->arp_pro != htons(ETHERTYPE_IP) ||
|
|
|
|
ap->arp_hln != sizeof(ap->arp_sha) ||
|
|
|
|
ap->arp_pln != sizeof(ap->arp_spa) )
|
|
|
|
{
|
1995-06-27 19:27:24 +04:00
|
|
|
#ifdef RARP_DEBUG
|
|
|
|
if (debug)
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("bad hrd/pro/hln/pln\n");
|
1995-06-27 19:27:24 +04:00
|
|
|
#endif
|
1995-09-23 07:36:06 +04:00
|
|
|
return (-1);
|
1995-06-27 19:27:24 +04:00
|
|
|
}
|
|
|
|
|
1995-09-23 07:36:06 +04:00
|
|
|
if (ap->arp_op != htons(ARPOP_REVREPLY)) {
|
1995-06-27 19:27:24 +04:00
|
|
|
#ifdef RARP_DEBUG
|
|
|
|
if (debug)
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("bad op=0x%x\n", ntohs(ap->arp_op));
|
1995-06-27 19:27:24 +04:00
|
|
|
#endif
|
1995-09-23 07:36:06 +04:00
|
|
|
return (-1);
|
1995-06-27 19:27:24 +04:00
|
|
|
}
|
|
|
|
|
1995-09-23 07:36:06 +04:00
|
|
|
/* Is the reply for our Ethernet address? */
|
1995-06-27 19:27:24 +04:00
|
|
|
if (bcmp(ap->arp_tha, d->myea, 6)) {
|
|
|
|
#ifdef RARP_DEBUG
|
|
|
|
if (debug)
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("unwanted address\n");
|
1995-06-27 19:27:24 +04:00
|
|
|
#endif
|
1995-09-23 07:36:06 +04:00
|
|
|
return (-1);
|
1995-06-27 19:27:24 +04:00
|
|
|
}
|
1994-05-08 20:11:14 +04:00
|
|
|
|
1995-09-23 07:36:06 +04:00
|
|
|
/* We have our answer. */
|
1995-06-27 19:27:24 +04:00
|
|
|
#ifdef RARP_DEBUG
|
1995-09-23 07:36:06 +04:00
|
|
|
if (debug)
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("got it\n");
|
1995-06-27 19:27:24 +04:00
|
|
|
#endif
|
1995-09-15 03:45:21 +04:00
|
|
|
return (n);
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|