1995-12-08 07:12:53 +03:00
|
|
|
/* $NetBSD: arp.c,v 1.13 1995/12/08 04:12:53 gwr Exp $ */
|
1994-10-26 08:44:32 +03:00
|
|
|
|
1994-05-08 20:11:14 +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-05-08 20:11:14 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <netinet/if_ether.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "stand.h"
|
|
|
|
#include "net.h"
|
|
|
|
|
|
|
|
/* Cache stuff */
|
|
|
|
#define ARP_NUM 8 /* need at most 3 arp entries */
|
|
|
|
|
1995-12-08 07:12:53 +03:00
|
|
|
struct arp_list {
|
1995-09-19 01:19:18 +04:00
|
|
|
struct in_addr addr;
|
|
|
|
u_char ea[6];
|
1994-05-08 20:11:14 +04:00
|
|
|
} arp_list[ARP_NUM] = {
|
1995-09-28 02:14:57 +03:00
|
|
|
/* XXX - net order `INADDR_BROADCAST' must be a constant */
|
|
|
|
{ {0xffffffff}, BA }
|
1994-05-08 20:11:14 +04:00
|
|
|
};
|
1995-12-08 07:12:53 +03:00
|
|
|
int arp_num = 1;
|
1994-05-08 20:11:14 +04:00
|
|
|
|
|
|
|
/* Local forwards */
|
1995-09-15 03:45:21 +04:00
|
|
|
static ssize_t arpsend __P((struct iodesc *, void *, size_t));
|
|
|
|
static ssize_t arprecv __P((struct iodesc *, void *, size_t, time_t));
|
1994-05-08 20:11:14 +04:00
|
|
|
|
|
|
|
/* Broadcast an ARP packet, asking who has addr on interface d */
|
|
|
|
u_char *
|
|
|
|
arpwhohas(d, addr)
|
|
|
|
register struct iodesc *d;
|
1995-09-19 01:19:18 +04:00
|
|
|
struct in_addr addr;
|
1994-05-08 20:11:14 +04:00
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
register struct ether_arp *ah;
|
|
|
|
register struct arp_list *al;
|
|
|
|
struct {
|
1995-12-08 07:12:53 +03:00
|
|
|
struct ether_header eh;
|
1995-06-27 19:18:57 +04:00
|
|
|
struct {
|
|
|
|
struct ether_arp arp;
|
1995-12-08 07:12:53 +03:00
|
|
|
u_char pad[18]; /* 60 - sizeof(...) */
|
1995-06-27 19:18:57 +04:00
|
|
|
} data;
|
1994-05-08 20:11:14 +04:00
|
|
|
} wbuf;
|
1995-02-20 14:04:00 +03:00
|
|
|
struct {
|
1995-12-08 07:12:53 +03:00
|
|
|
struct ether_header eh;
|
1995-06-27 19:18:57 +04:00
|
|
|
struct {
|
|
|
|
struct ether_arp arp;
|
|
|
|
u_char pad[24]; /* extra space */
|
|
|
|
} data;
|
1994-05-08 20:11:14 +04:00
|
|
|
} rbuf;
|
|
|
|
|
|
|
|
/* Try for cached answer first */
|
|
|
|
for (i = 0, al = arp_list; i < arp_num; ++i, ++al)
|
1995-09-19 01:19:18 +04:00
|
|
|
if (addr.s_addr == al->addr.s_addr)
|
1994-05-08 20:11:14 +04:00
|
|
|
return (al->ea);
|
|
|
|
|
|
|
|
/* Don't overflow cache */
|
1995-12-08 07:12:53 +03:00
|
|
|
if (arp_num > ARP_NUM - 1) {
|
|
|
|
arp_num = 1; /* recycle */
|
|
|
|
printf("arpwhohas: overflowed arp_list!\n");
|
|
|
|
}
|
1994-05-08 20:11:14 +04:00
|
|
|
|
|
|
|
#ifdef ARP_DEBUG
|
|
|
|
if (debug)
|
1995-12-08 07:12:53 +03:00
|
|
|
printf("arpwhohas: send request for %s\n", inet_ntoa(addr));
|
1994-05-08 20:11:14 +04:00
|
|
|
#endif
|
1995-02-20 14:04:00 +03:00
|
|
|
|
1995-06-27 19:18:57 +04:00
|
|
|
bzero((char*)&wbuf.data, sizeof(wbuf.data));
|
|
|
|
ah = &wbuf.data.arp;
|
1994-05-08 20:11:14 +04:00
|
|
|
ah->arp_hrd = htons(ARPHRD_ETHER);
|
|
|
|
ah->arp_pro = htons(ETHERTYPE_IP);
|
|
|
|
ah->arp_hln = sizeof(ah->arp_sha); /* hardware address length */
|
|
|
|
ah->arp_pln = sizeof(ah->arp_spa); /* protocol address length */
|
|
|
|
ah->arp_op = htons(ARPOP_REQUEST);
|
|
|
|
MACPY(d->myea, ah->arp_sha);
|
|
|
|
bcopy(&d->myip, ah->arp_spa, sizeof(ah->arp_spa));
|
1995-12-08 07:12:53 +03:00
|
|
|
/* Leave zeros in arp_tha */
|
1994-05-08 20:11:14 +04:00
|
|
|
bcopy(&addr, ah->arp_tpa, sizeof(ah->arp_tpa));
|
|
|
|
|
1995-12-08 07:12:53 +03:00
|
|
|
/* Store ip address in cache (incomplete entry). */
|
1994-05-08 20:11:14 +04:00
|
|
|
al->addr = addr;
|
|
|
|
|
1995-12-08 07:12:53 +03:00
|
|
|
i = sendrecv(d,
|
1995-06-27 19:18:57 +04:00
|
|
|
arpsend, &wbuf.data, sizeof(wbuf.data),
|
|
|
|
arprecv, &rbuf.data, sizeof(rbuf.data));
|
1995-12-08 07:12:53 +03:00
|
|
|
if (i == -1) {
|
|
|
|
panic("arp: no response for %s\n",
|
|
|
|
inet_ntoa(addr));
|
|
|
|
}
|
1994-05-08 20:11:14 +04:00
|
|
|
|
|
|
|
/* Store ethernet address in cache */
|
1995-09-23 07:36:06 +04:00
|
|
|
ah = &rbuf.data.arp;
|
|
|
|
#ifdef ARP_DEBUG
|
1995-12-08 07:12:53 +03:00
|
|
|
if (debug) {
|
|
|
|
printf("arp: response from %s\n",
|
|
|
|
ether_sprintf(rbuf.eh.ether_shost));
|
1995-09-23 07:36:06 +04:00
|
|
|
printf("arp: cacheing %s --> %s\n",
|
1995-12-08 07:12:53 +03:00
|
|
|
inet_ntoa(addr),
|
1995-09-23 07:36:06 +04:00
|
|
|
ether_sprintf(ah->arp_sha));
|
1995-12-08 07:12:53 +03:00
|
|
|
}
|
1995-09-23 07:36:06 +04:00
|
|
|
#endif
|
|
|
|
MACPY(ah->arp_sha, al->ea);
|
1994-05-08 20:11:14 +04:00
|
|
|
++arp_num;
|
1995-02-20 14:04:00 +03:00
|
|
|
|
1994-05-08 20:11:14 +04:00
|
|
|
return (al->ea);
|
|
|
|
}
|
|
|
|
|
1995-09-15 03:45:21 +04:00
|
|
|
static ssize_t
|
1994-05-08 20:11:14 +04:00
|
|
|
arpsend(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 ARP_DEBUG
|
|
|
|
if (debug)
|
1995-02-20 14:04:00 +03:00
|
|
|
printf("arpsend: 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_ARP));
|
|
|
|
}
|
|
|
|
|
1995-09-23 07:36:06 +04:00
|
|
|
/*
|
|
|
|
* Returns 0 if this is the packet we're waiting for
|
|
|
|
* else -1 (and errno == 0)
|
|
|
|
*/
|
1995-09-15 03:45:21 +04:00
|
|
|
static ssize_t
|
1995-02-20 14:04:00 +03:00
|
|
|
arprecv(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 *ah;
|
1995-09-12 01:11:36 +04:00
|
|
|
u_int16_t etype; /* host order */
|
1994-05-08 20:11:14 +04:00
|
|
|
|
|
|
|
#ifdef ARP_DEBUG
|
|
|
|
if (debug)
|
1995-06-27 19:18:57 +04:00
|
|
|
printf("arprecv: ");
|
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:18:57 +04:00
|
|
|
#ifdef ARP_DEBUG
|
|
|
|
if (debug)
|
1995-09-15 03:45:21 +04:00
|
|
|
printf("bad len=%d\n", n);
|
1995-06-27 19:18:57 +04:00
|
|
|
#endif
|
1995-09-23 07:36:06 +04:00
|
|
|
return (-1);
|
1995-06-27 19:18:57 +04:00
|
|
|
}
|
1994-05-08 20:11:14 +04:00
|
|
|
|
1995-09-12 01:11:36 +04:00
|
|
|
if (etype != ETHERTYPE_ARP) {
|
1994-05-08 20:11:14 +04:00
|
|
|
#ifdef ARP_DEBUG
|
|
|
|
if (debug)
|
1995-09-12 01:11:36 +04:00
|
|
|
printf("not arp type=%d\n", etype);
|
1994-05-08 20:11:14 +04:00
|
|
|
#endif
|
1995-09-23 07:36:06 +04:00
|
|
|
return (-1);
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
1995-09-12 01:11:36 +04:00
|
|
|
|
|
|
|
/* Ethernet address now checked in readether() */
|
|
|
|
|
|
|
|
ah = (struct ether_arp *)pkt;
|
|
|
|
if (ah->arp_hrd != htons(ARPHRD_ETHER) ||
|
|
|
|
ah->arp_pro != htons(ETHERTYPE_IP) ||
|
|
|
|
ah->arp_hln != sizeof(ah->arp_sha) ||
|
|
|
|
ah->arp_pln != sizeof(ah->arp_spa) )
|
|
|
|
{
|
1994-05-08 20:11:14 +04:00
|
|
|
#ifdef ARP_DEBUG
|
|
|
|
if (debug)
|
1995-12-08 07:12:53 +03:00
|
|
|
printf("bad hrd/pro/hln/pln\n");
|
1994-05-08 20:11:14 +04:00
|
|
|
#endif
|
1995-09-23 07:36:06 +04:00
|
|
|
return (-1);
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
|
|
|
|
1995-09-12 01:11:36 +04:00
|
|
|
if (ah->arp_op == htons(ARPOP_REQUEST)) {
|
1995-09-23 07:36:06 +04:00
|
|
|
#ifdef ARP_DEBUG
|
|
|
|
if (debug)
|
1995-12-08 07:12:53 +03:00
|
|
|
printf("is request\n");
|
1995-09-23 07:36:06 +04:00
|
|
|
#endif
|
1995-09-12 01:11:36 +04:00
|
|
|
arp_reply(d, ah);
|
1995-09-23 07:36:06 +04:00
|
|
|
return (-1);
|
1995-09-12 01:11:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ah->arp_op != htons(ARPOP_REPLY)) {
|
1994-05-08 20:11:14 +04:00
|
|
|
#ifdef ARP_DEBUG
|
|
|
|
if (debug)
|
1995-09-12 01:11:36 +04:00
|
|
|
printf("not ARP reply\n");
|
1994-05-08 20:11:14 +04:00
|
|
|
#endif
|
1995-09-23 07:36:06 +04:00
|
|
|
return (-1);
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
1995-09-12 01:11:36 +04:00
|
|
|
|
1995-09-23 07:36:06 +04:00
|
|
|
/* Is the reply from the source we want? */
|
|
|
|
if (bcmp(&arp_list[arp_num].addr,
|
|
|
|
ah->arp_spa, sizeof(ah->arp_spa)))
|
|
|
|
{
|
1994-05-08 20:11:14 +04:00
|
|
|
#ifdef ARP_DEBUG
|
|
|
|
if (debug)
|
1995-09-23 07:36:06 +04:00
|
|
|
printf("unwanted address\n");
|
1994-05-08 20:11:14 +04:00
|
|
|
#endif
|
1995-09-23 07:36:06 +04:00
|
|
|
return (-1);
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
1995-09-23 07:36:06 +04:00
|
|
|
/* We don't care who the reply was sent to. */
|
1994-05-08 20:11:14 +04:00
|
|
|
|
1995-09-23 07:36:06 +04:00
|
|
|
/* We have our answer. */
|
1995-06-27 19:18:57 +04:00
|
|
|
#ifdef ARP_DEBUG
|
|
|
|
if (debug)
|
1995-09-23 07:36:06 +04:00
|
|
|
printf("got it\n");
|
1995-06-27 19:18:57 +04:00
|
|
|
#endif
|
1995-09-15 03:45:21 +04:00
|
|
|
return (n);
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
1995-09-12 01:11:36 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert an ARP request into a reply and send it.
|
1995-09-13 19:06:12 +04:00
|
|
|
* Notes: Re-uses buffer. Pad to length = 46.
|
1995-09-12 01:11:36 +04:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
arp_reply(d, pkt)
|
|
|
|
register struct iodesc *d;
|
|
|
|
register void *pkt; /* the request */
|
|
|
|
{
|
|
|
|
struct ether_arp *arp = pkt;
|
|
|
|
|
|
|
|
if (arp->arp_hrd != htons(ARPHRD_ETHER) ||
|
|
|
|
arp->arp_pro != htons(ETHERTYPE_IP) ||
|
|
|
|
arp->arp_hln != sizeof(arp->arp_sha) ||
|
|
|
|
arp->arp_pln != sizeof(arp->arp_spa) )
|
|
|
|
{
|
|
|
|
#ifdef ARP_DEBUG
|
|
|
|
if (debug)
|
1995-12-08 07:12:53 +03:00
|
|
|
printf("arp_reply: bad hrd/pro/hln/pln\n");
|
1995-09-12 01:11:36 +04:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arp->arp_op != htons(ARPOP_REQUEST)) {
|
|
|
|
#ifdef ARP_DEBUG
|
|
|
|
if (debug)
|
|
|
|
printf("arp_reply: not request!\n");
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we are not the target, ignore the request. */
|
|
|
|
if (bcmp(arp->arp_tpa, &d->myip, sizeof(arp->arp_tpa)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
#ifdef ARP_DEBUG
|
|
|
|
if (debug) {
|
|
|
|
printf("arp_reply: to %s\n",
|
|
|
|
ether_sprintf(arp->arp_sha));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
arp->arp_op = htons(ARPOP_REPLY);
|
|
|
|
/* source becomes target */
|
|
|
|
bcopy(arp->arp_sha, arp->arp_tha, sizeof(arp->arp_tha));
|
|
|
|
bcopy(arp->arp_spa, arp->arp_tpa, sizeof(arp->arp_tpa));
|
|
|
|
/* here becomes source */
|
|
|
|
bcopy(d->myea, arp->arp_sha, sizeof(arp->arp_sha));
|
|
|
|
bcopy(&d->myip, arp->arp_spa, sizeof(arp->arp_spa));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No need to get fancy here. If the send fails, the
|
|
|
|
* requestor will just ask again.
|
|
|
|
*/
|
1995-09-13 19:06:12 +04:00
|
|
|
(void) sendether(d, pkt, sizeof(*arp) + 18,
|
|
|
|
arp->arp_tha, ETHERTYPE_ARP);
|
1995-09-12 01:11:36 +04:00
|
|
|
}
|