Home | History | Annotate | Line # | Download | only in libsa
arp.c revision 1.17
      1  1.17  drochner /*	$NetBSD: arp.c,v 1.17 1997/06/26 19:11:30 drochner Exp $	*/
      2   1.3       cgd 
      3   1.1    brezak /*
      4   1.1    brezak  * Copyright (c) 1992 Regents of the University of California.
      5   1.1    brezak  * All rights reserved.
      6   1.1    brezak  *
      7   1.1    brezak  * This software was developed by the Computer Systems Engineering group
      8   1.1    brezak  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9   1.1    brezak  * contributed to Berkeley.
     10   1.1    brezak  *
     11   1.1    brezak  * Redistribution and use in source and binary forms, with or without
     12   1.1    brezak  * modification, are permitted provided that the following conditions
     13   1.1    brezak  * are met:
     14   1.1    brezak  * 1. Redistributions of source code must retain the above copyright
     15   1.1    brezak  *    notice, this list of conditions and the following disclaimer.
     16   1.1    brezak  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1    brezak  *    notice, this list of conditions and the following disclaimer in the
     18   1.1    brezak  *    documentation and/or other materials provided with the distribution.
     19   1.1    brezak  * 3. All advertising materials mentioning features or use of this software
     20   1.1    brezak  *    must display the following acknowledgement:
     21   1.1    brezak  *	This product includes software developed by the University of
     22   1.1    brezak  *	California, Lawrence Berkeley Laboratory and its contributors.
     23   1.1    brezak  * 4. Neither the name of the University nor the names of its contributors
     24   1.1    brezak  *    may be used to endorse or promote products derived from this software
     25   1.1    brezak  *    without specific prior written permission.
     26   1.1    brezak  *
     27   1.1    brezak  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28   1.1    brezak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29   1.1    brezak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30   1.1    brezak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31   1.1    brezak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32   1.1    brezak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33   1.1    brezak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34   1.1    brezak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35   1.1    brezak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36   1.1    brezak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37   1.1    brezak  * SUCH DAMAGE.
     38   1.1    brezak  *
     39   1.3       cgd  * @(#) Header: arp.c,v 1.5 93/07/15 05:52:26 leres Exp  (LBL)
     40   1.1    brezak  */
     41   1.1    brezak 
     42   1.1    brezak #include <sys/types.h>
     43   1.1    brezak #include <sys/socket.h>
     44   1.1    brezak #include <net/if.h>
     45   1.1    brezak #include <netinet/in.h>
     46   1.1    brezak 
     47   1.1    brezak #include <netinet/in_systm.h>
     48   1.1    brezak 
     49  1.17  drochner #ifdef _STANDALONE
     50  1.17  drochner #include <lib/libkern/libkern.h>
     51  1.17  drochner #else
     52   1.1    brezak #include <string.h>
     53  1.17  drochner #endif
     54   1.1    brezak 
     55  1.16        is #include "if_ether.h"
     56   1.1    brezak #include "stand.h"
     57   1.1    brezak #include "net.h"
     58   1.5       gwr 
     59   1.1    brezak /* Cache stuff */
     60   1.1    brezak #define ARP_NUM 8			/* need at most 3 arp entries */
     61   1.1    brezak 
     62  1.13       gwr struct arp_list {
     63  1.10        pk 	struct in_addr	addr;
     64  1.10        pk 	u_char		ea[6];
     65   1.1    brezak } arp_list[ARP_NUM] = {
     66  1.12        pk 	/* XXX - net order `INADDR_BROADCAST' must be a constant */
     67  1.12        pk 	{ {0xffffffff}, BA }
     68   1.1    brezak };
     69  1.13       gwr int arp_num = 1;
     70   1.1    brezak 
     71   1.1    brezak /* Local forwards */
     72   1.9        pk static	ssize_t arpsend __P((struct iodesc *, void *, size_t));
     73   1.9        pk static	ssize_t arprecv __P((struct iodesc *, void *, size_t, time_t));
     74   1.1    brezak 
     75   1.1    brezak /* Broadcast an ARP packet, asking who has addr on interface d */
     76   1.1    brezak u_char *
     77   1.1    brezak arpwhohas(d, addr)
     78   1.1    brezak 	register struct iodesc *d;
     79  1.10        pk 	struct in_addr addr;
     80   1.1    brezak {
     81   1.1    brezak 	register int i;
     82   1.1    brezak 	register struct ether_arp *ah;
     83   1.1    brezak 	register struct arp_list *al;
     84   1.1    brezak 	struct {
     85  1.13       gwr 		struct ether_header eh;
     86   1.5       gwr 		struct {
     87   1.5       gwr 			struct ether_arp arp;
     88  1.13       gwr 			u_char pad[18]; 	/* 60 - sizeof(...) */
     89   1.5       gwr 		} data;
     90   1.1    brezak 	} wbuf;
     91   1.4   mycroft 	struct {
     92  1.13       gwr 		struct ether_header eh;
     93   1.5       gwr 		struct {
     94   1.5       gwr 			struct ether_arp arp;
     95   1.5       gwr 			u_char pad[24]; 	/* extra space */
     96   1.5       gwr 		} data;
     97   1.1    brezak 	} rbuf;
     98   1.1    brezak 
     99   1.1    brezak 	/* Try for cached answer first */
    100   1.1    brezak 	for (i = 0, al = arp_list; i < arp_num; ++i, ++al)
    101  1.10        pk 		if (addr.s_addr == al->addr.s_addr)
    102   1.1    brezak 			return (al->ea);
    103   1.1    brezak 
    104   1.1    brezak 	/* Don't overflow cache */
    105  1.13       gwr 	if (arp_num > ARP_NUM - 1) {
    106  1.13       gwr 		arp_num = 1;	/* recycle */
    107  1.15  christos 		printf("arpwhohas: overflowed arp_list!\n");
    108  1.13       gwr 	}
    109   1.1    brezak 
    110   1.1    brezak #ifdef ARP_DEBUG
    111   1.1    brezak  	if (debug)
    112  1.15  christos  	    printf("arpwhohas: send request for %s\n", inet_ntoa(addr));
    113   1.1    brezak #endif
    114   1.4   mycroft 
    115   1.5       gwr 	bzero((char*)&wbuf.data, sizeof(wbuf.data));
    116   1.5       gwr 	ah = &wbuf.data.arp;
    117   1.1    brezak 	ah->arp_hrd = htons(ARPHRD_ETHER);
    118   1.1    brezak 	ah->arp_pro = htons(ETHERTYPE_IP);
    119   1.1    brezak 	ah->arp_hln = sizeof(ah->arp_sha); /* hardware address length */
    120   1.1    brezak 	ah->arp_pln = sizeof(ah->arp_spa); /* protocol address length */
    121   1.1    brezak 	ah->arp_op = htons(ARPOP_REQUEST);
    122   1.1    brezak 	MACPY(d->myea, ah->arp_sha);
    123   1.1    brezak 	bcopy(&d->myip, ah->arp_spa, sizeof(ah->arp_spa));
    124  1.13       gwr 	/* Leave zeros in arp_tha */
    125   1.1    brezak 	bcopy(&addr, ah->arp_tpa, sizeof(ah->arp_tpa));
    126   1.1    brezak 
    127  1.13       gwr 	/* Store ip address in cache (incomplete entry). */
    128   1.1    brezak 	al->addr = addr;
    129   1.1    brezak 
    130  1.13       gwr 	i = sendrecv(d,
    131   1.5       gwr 	    arpsend, &wbuf.data, sizeof(wbuf.data),
    132   1.5       gwr 	    arprecv, &rbuf.data, sizeof(rbuf.data));
    133  1.13       gwr 	if (i == -1) {
    134  1.13       gwr 		panic("arp: no response for %s\n",
    135  1.13       gwr 			  inet_ntoa(addr));
    136  1.13       gwr 	}
    137   1.1    brezak 
    138   1.1    brezak 	/* Store ethernet address in cache */
    139  1.11       gwr 	ah = &rbuf.data.arp;
    140  1.11       gwr #ifdef ARP_DEBUG
    141  1.13       gwr  	if (debug) {
    142  1.15  christos 		printf("arp: response from %s\n",
    143  1.14  christos 		    ether_sprintf(rbuf.eh.ether_shost));
    144  1.15  christos 		printf("arp: cacheing %s --> %s\n",
    145  1.14  christos 		    inet_ntoa(addr), ether_sprintf(ah->arp_sha));
    146  1.13       gwr 	}
    147  1.11       gwr #endif
    148  1.11       gwr 	MACPY(ah->arp_sha, al->ea);
    149   1.1    brezak 	++arp_num;
    150   1.4   mycroft 
    151   1.1    brezak 	return (al->ea);
    152   1.1    brezak }
    153   1.1    brezak 
    154   1.9        pk static ssize_t
    155   1.1    brezak arpsend(d, pkt, len)
    156   1.1    brezak 	register struct iodesc *d;
    157   1.1    brezak 	register void *pkt;
    158   1.4   mycroft 	register size_t len;
    159   1.1    brezak {
    160   1.4   mycroft 
    161   1.1    brezak #ifdef ARP_DEBUG
    162   1.1    brezak  	if (debug)
    163  1.15  christos 		printf("arpsend: called\n");
    164   1.1    brezak #endif
    165   1.4   mycroft 
    166   1.1    brezak 	return (sendether(d, pkt, len, bcea, ETHERTYPE_ARP));
    167   1.1    brezak }
    168   1.1    brezak 
    169  1.11       gwr /*
    170  1.11       gwr  * Returns 0 if this is the packet we're waiting for
    171  1.11       gwr  * else -1 (and errno == 0)
    172  1.11       gwr  */
    173   1.9        pk static ssize_t
    174   1.4   mycroft arprecv(d, pkt, len, tleft)
    175   1.1    brezak 	register struct iodesc *d;
    176   1.1    brezak 	register void *pkt;
    177   1.4   mycroft 	register size_t len;
    178   1.4   mycroft 	time_t tleft;
    179   1.1    brezak {
    180   1.9        pk 	register ssize_t n;
    181   1.1    brezak 	register struct ether_arp *ah;
    182   1.7   thorpej 	u_int16_t etype;	/* host order */
    183   1.1    brezak 
    184   1.1    brezak #ifdef ARP_DEBUG
    185   1.1    brezak  	if (debug)
    186  1.15  christos 		printf("arprecv: ");
    187   1.1    brezak #endif
    188   1.1    brezak 
    189   1.9        pk 	n = readether(d, pkt, len, tleft, &etype);
    190  1.11       gwr 	errno = 0;	/* XXX */
    191   1.9        pk 	if (n == -1 || n < sizeof(struct ether_arp)) {
    192   1.5       gwr #ifdef ARP_DEBUG
    193   1.5       gwr 		if (debug)
    194  1.15  christos 			printf("bad len=%d\n", n);
    195   1.5       gwr #endif
    196  1.11       gwr 		return (-1);
    197   1.5       gwr 	}
    198   1.1    brezak 
    199   1.7   thorpej 	if (etype != ETHERTYPE_ARP) {
    200   1.1    brezak #ifdef ARP_DEBUG
    201   1.1    brezak 		if (debug)
    202  1.15  christos 			printf("not arp type=%d\n", etype);
    203   1.1    brezak #endif
    204  1.11       gwr 		return (-1);
    205   1.1    brezak 	}
    206   1.7   thorpej 
    207   1.7   thorpej 	/* Ethernet address now checked in readether() */
    208   1.7   thorpej 
    209   1.7   thorpej 	ah = (struct ether_arp *)pkt;
    210   1.7   thorpej 	if (ah->arp_hrd != htons(ARPHRD_ETHER) ||
    211   1.7   thorpej 	    ah->arp_pro != htons(ETHERTYPE_IP) ||
    212   1.7   thorpej 	    ah->arp_hln != sizeof(ah->arp_sha) ||
    213   1.7   thorpej 	    ah->arp_pln != sizeof(ah->arp_spa) )
    214   1.7   thorpej 	{
    215   1.1    brezak #ifdef ARP_DEBUG
    216   1.1    brezak 		if (debug)
    217  1.15  christos 			printf("bad hrd/pro/hln/pln\n");
    218   1.1    brezak #endif
    219  1.11       gwr 		return (-1);
    220   1.1    brezak 	}
    221   1.1    brezak 
    222   1.7   thorpej 	if (ah->arp_op == htons(ARPOP_REQUEST)) {
    223  1.11       gwr #ifdef ARP_DEBUG
    224  1.11       gwr 		if (debug)
    225  1.15  christos 			printf("is request\n");
    226  1.11       gwr #endif
    227   1.7   thorpej 		arp_reply(d, ah);
    228  1.11       gwr 		return (-1);
    229   1.7   thorpej 	}
    230   1.7   thorpej 
    231   1.7   thorpej 	if (ah->arp_op != htons(ARPOP_REPLY)) {
    232   1.1    brezak #ifdef ARP_DEBUG
    233   1.1    brezak 		if (debug)
    234  1.15  christos 			printf("not ARP reply\n");
    235   1.1    brezak #endif
    236  1.11       gwr 		return (-1);
    237   1.1    brezak 	}
    238   1.7   thorpej 
    239  1.11       gwr 	/* Is the reply from the source we want? */
    240  1.11       gwr 	if (bcmp(&arp_list[arp_num].addr,
    241  1.11       gwr 			 ah->arp_spa, sizeof(ah->arp_spa)))
    242  1.11       gwr 	{
    243   1.1    brezak #ifdef ARP_DEBUG
    244   1.1    brezak 		if (debug)
    245  1.15  christos 			printf("unwanted address\n");
    246   1.1    brezak #endif
    247  1.11       gwr 		return (-1);
    248   1.1    brezak 	}
    249  1.11       gwr 	/* We don't care who the reply was sent to. */
    250   1.1    brezak 
    251  1.11       gwr 	/* We have our answer. */
    252   1.5       gwr #ifdef ARP_DEBUG
    253   1.5       gwr  	if (debug)
    254  1.15  christos 		printf("got it\n");
    255   1.5       gwr #endif
    256   1.9        pk 	return (n);
    257   1.7   thorpej }
    258   1.7   thorpej 
    259   1.7   thorpej /*
    260   1.7   thorpej  * Convert an ARP request into a reply and send it.
    261   1.8       gwr  * Notes:  Re-uses buffer.  Pad to length = 46.
    262   1.7   thorpej  */
    263   1.7   thorpej void
    264   1.7   thorpej arp_reply(d, pkt)
    265   1.7   thorpej 	register struct iodesc *d;
    266   1.7   thorpej 	register void *pkt;		/* the request */
    267   1.7   thorpej {
    268   1.7   thorpej 	struct ether_arp *arp = pkt;
    269   1.7   thorpej 
    270   1.7   thorpej 	if (arp->arp_hrd != htons(ARPHRD_ETHER) ||
    271   1.7   thorpej 	    arp->arp_pro != htons(ETHERTYPE_IP) ||
    272   1.7   thorpej 	    arp->arp_hln != sizeof(arp->arp_sha) ||
    273   1.7   thorpej 	    arp->arp_pln != sizeof(arp->arp_spa) )
    274   1.7   thorpej 	{
    275   1.7   thorpej #ifdef ARP_DEBUG
    276   1.7   thorpej 		if (debug)
    277  1.15  christos 			printf("arp_reply: bad hrd/pro/hln/pln\n");
    278   1.7   thorpej #endif
    279   1.7   thorpej 		return;
    280   1.7   thorpej 	}
    281   1.7   thorpej 
    282   1.7   thorpej 	if (arp->arp_op != htons(ARPOP_REQUEST)) {
    283   1.7   thorpej #ifdef ARP_DEBUG
    284   1.7   thorpej 		if (debug)
    285  1.15  christos 			printf("arp_reply: not request!\n");
    286   1.7   thorpej #endif
    287   1.7   thorpej 		return;
    288   1.7   thorpej 	}
    289   1.7   thorpej 
    290   1.7   thorpej 	/* If we are not the target, ignore the request. */
    291   1.7   thorpej 	if (bcmp(arp->arp_tpa, &d->myip, sizeof(arp->arp_tpa)))
    292   1.7   thorpej 		return;
    293   1.7   thorpej 
    294   1.7   thorpej #ifdef ARP_DEBUG
    295   1.7   thorpej 	if (debug) {
    296  1.15  christos 		printf("arp_reply: to %s\n", ether_sprintf(arp->arp_sha));
    297   1.7   thorpej 	}
    298   1.7   thorpej #endif
    299   1.7   thorpej 
    300   1.7   thorpej 	arp->arp_op = htons(ARPOP_REPLY);
    301   1.7   thorpej 	/* source becomes target */
    302   1.7   thorpej 	bcopy(arp->arp_sha, arp->arp_tha, sizeof(arp->arp_tha));
    303   1.7   thorpej 	bcopy(arp->arp_spa, arp->arp_tpa, sizeof(arp->arp_tpa));
    304   1.7   thorpej 	/* here becomes source */
    305   1.7   thorpej 	bcopy(d->myea,  arp->arp_sha, sizeof(arp->arp_sha));
    306   1.7   thorpej 	bcopy(&d->myip, arp->arp_spa, sizeof(arp->arp_spa));
    307   1.7   thorpej 
    308   1.7   thorpej 	/*
    309   1.7   thorpej 	 * No need to get fancy here.  If the send fails, the
    310   1.7   thorpej 	 * requestor will just ask again.
    311   1.7   thorpej 	 */
    312   1.8       gwr 	(void) sendether(d, pkt, sizeof(*arp) + 18,
    313   1.8       gwr 	                 arp->arp_tha, ETHERTYPE_ARP);
    314   1.1    brezak }
    315