1997-03-15 21:09:08 +03:00
|
|
|
/* $NetBSD: rarp.c,v 1.14 1997/03/15 18:12:17 is 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>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
1997-03-15 21:09:08 +03:00
|
|
|
#include "if_ether.h"
|
1994-05-08 20:11:14 +04:00
|
|
|
#include "stand.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "netif.h"
|
|
|
|
|
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;
|
|
|
|
register struct ether_arp *ap;
|
|
|
|
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)
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("rarp: d=%x\n", (u_int)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)
|
|
|
|
register struct iodesc *d;
|
|
|
|
register void *pkt;
|
1995-02-20 14:04:00 +03:00
|
|
|
register 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)
|
1994-05-08 20:11:14 +04:00
|
|
|
register struct iodesc *d;
|
|
|
|
register void *pkt;
|
1995-02-20 14:04:00 +03:00
|
|
|
register size_t len;
|
|
|
|
time_t tleft;
|
1994-05-08 20:11:14 +04:00
|
|
|
{
|
1995-09-15 03:45:21 +04:00
|
|
|
register ssize_t n;
|
1994-05-08 20:11:14 +04:00
|
|
|
register 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
|
|
|
}
|