xref: /titanic_52/usr/src/boot/lib/libstand/arp.c (revision 7b2a1233f92b2b3cb858f2fdb69378a9ab0a42f1)
14a5d661aSToomas Soome /*	$NetBSD: arp.c,v 1.18 1997/07/07 15:52:49 drochner Exp $	*/
24a5d661aSToomas Soome 
34a5d661aSToomas Soome /*
44a5d661aSToomas Soome  * Copyright (c) 1992 Regents of the University of California.
54a5d661aSToomas Soome  * All rights reserved.
64a5d661aSToomas Soome  *
74a5d661aSToomas Soome  * This software was developed by the Computer Systems Engineering group
84a5d661aSToomas Soome  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
94a5d661aSToomas Soome  * contributed to Berkeley.
104a5d661aSToomas Soome  *
114a5d661aSToomas Soome  * Redistribution and use in source and binary forms, with or without
124a5d661aSToomas Soome  * modification, are permitted provided that the following conditions
134a5d661aSToomas Soome  * are met:
144a5d661aSToomas Soome  * 1. Redistributions of source code must retain the above copyright
154a5d661aSToomas Soome  *    notice, this list of conditions and the following disclaimer.
164a5d661aSToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
174a5d661aSToomas Soome  *    notice, this list of conditions and the following disclaimer in the
184a5d661aSToomas Soome  *    documentation and/or other materials provided with the distribution.
194a5d661aSToomas Soome  * 4. Neither the name of the University nor the names of its contributors
204a5d661aSToomas Soome  *    may be used to endorse or promote products derived from this software
214a5d661aSToomas Soome  *    without specific prior written permission.
224a5d661aSToomas Soome  *
234a5d661aSToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
244a5d661aSToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
254a5d661aSToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
264a5d661aSToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
274a5d661aSToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
284a5d661aSToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
294a5d661aSToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
304a5d661aSToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
314a5d661aSToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
324a5d661aSToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
334a5d661aSToomas Soome  * SUCH DAMAGE.
344a5d661aSToomas Soome  *
354a5d661aSToomas Soome  * @(#) Header: arp.c,v 1.5 93/07/15 05:52:26 leres Exp  (LBL)
364a5d661aSToomas Soome  */
374a5d661aSToomas Soome 
384a5d661aSToomas Soome #include <sys/cdefs.h>
394a5d661aSToomas Soome 
404a5d661aSToomas Soome #include <sys/types.h>
414a5d661aSToomas Soome #include <sys/socket.h>
424a5d661aSToomas Soome #include <net/if.h>
434a5d661aSToomas Soome #include <netinet/in.h>
444a5d661aSToomas Soome #include <netinet/if_ether.h>
454a5d661aSToomas Soome 
464a5d661aSToomas Soome #include <netinet/in_systm.h>
474a5d661aSToomas Soome 
484a5d661aSToomas Soome #include <string.h>
494a5d661aSToomas Soome 
504a5d661aSToomas Soome #include "stand.h"
514a5d661aSToomas Soome #include "net.h"
524a5d661aSToomas Soome 
534a5d661aSToomas Soome /* Cache stuff */
544a5d661aSToomas Soome #define ARP_NUM 8			/* need at most 3 arp entries */
554a5d661aSToomas Soome 
564a5d661aSToomas Soome struct arp_list {
574a5d661aSToomas Soome 	struct in_addr	addr;
584a5d661aSToomas Soome 	u_char		ea[6];
594a5d661aSToomas Soome } arp_list[ARP_NUM] = {
604a5d661aSToomas Soome 	/* XXX - net order `INADDR_BROADCAST' must be a constant */
614a5d661aSToomas Soome 	{ {0xffffffff}, BA }
624a5d661aSToomas Soome };
634a5d661aSToomas Soome int arp_num = 1;
644a5d661aSToomas Soome 
654a5d661aSToomas Soome /* Local forwards */
664a5d661aSToomas Soome static	ssize_t arpsend(struct iodesc *, void *, size_t);
67*7b2a1233SToomas Soome static	ssize_t arprecv(struct iodesc *, void **, void **, time_t);
684a5d661aSToomas Soome 
694a5d661aSToomas Soome /* Broadcast an ARP packet, asking who has addr on interface d */
704a5d661aSToomas Soome u_char *
71*7b2a1233SToomas Soome arpwhohas(struct iodesc *d, struct in_addr addr)
724a5d661aSToomas Soome {
734a5d661aSToomas Soome 	int i;
744a5d661aSToomas Soome 	struct ether_arp *ah;
754a5d661aSToomas Soome 	struct arp_list *al;
76*7b2a1233SToomas Soome 	void *pkt;
774a5d661aSToomas Soome 	struct {
784a5d661aSToomas Soome 		struct ether_header eh;
794a5d661aSToomas Soome 		struct {
804a5d661aSToomas Soome 			struct ether_arp arp;
814a5d661aSToomas Soome 			u_char pad[18]; 	/* 60 - sizeof(...) */
824a5d661aSToomas Soome 		} data;
834a5d661aSToomas Soome 	} wbuf;
844a5d661aSToomas Soome 
854a5d661aSToomas Soome 	/* Try for cached answer first */
864a5d661aSToomas Soome 	for (i = 0, al = arp_list; i < arp_num; ++i, ++al)
874a5d661aSToomas Soome 		if (addr.s_addr == al->addr.s_addr)
884a5d661aSToomas Soome 			return (al->ea);
894a5d661aSToomas Soome 
904a5d661aSToomas Soome 	/* Don't overflow cache */
914a5d661aSToomas Soome 	if (arp_num > ARP_NUM - 1) {
924a5d661aSToomas Soome 		arp_num = 1;	/* recycle */
934a5d661aSToomas Soome 		printf("arpwhohas: overflowed arp_list!\n");
944a5d661aSToomas Soome 	}
954a5d661aSToomas Soome 
964a5d661aSToomas Soome #ifdef ARP_DEBUG
974a5d661aSToomas Soome  	if (debug)
984a5d661aSToomas Soome  	    printf("arpwhohas: send request for %s\n", inet_ntoa(addr));
994a5d661aSToomas Soome #endif
1004a5d661aSToomas Soome 
1014a5d661aSToomas Soome 	bzero((char*)&wbuf.data, sizeof(wbuf.data));
1024a5d661aSToomas Soome 	ah = &wbuf.data.arp;
1034a5d661aSToomas Soome 	ah->arp_hrd = htons(ARPHRD_ETHER);
1044a5d661aSToomas Soome 	ah->arp_pro = htons(ETHERTYPE_IP);
1054a5d661aSToomas Soome 	ah->arp_hln = sizeof(ah->arp_sha); /* hardware address length */
1064a5d661aSToomas Soome 	ah->arp_pln = sizeof(ah->arp_spa); /* protocol address length */
1074a5d661aSToomas Soome 	ah->arp_op = htons(ARPOP_REQUEST);
1084a5d661aSToomas Soome 	MACPY(d->myea, ah->arp_sha);
1094a5d661aSToomas Soome 	bcopy(&d->myip, ah->arp_spa, sizeof(ah->arp_spa));
1104a5d661aSToomas Soome 	/* Leave zeros in arp_tha */
1114a5d661aSToomas Soome 	bcopy(&addr, ah->arp_tpa, sizeof(ah->arp_tpa));
1124a5d661aSToomas Soome 
1134a5d661aSToomas Soome 	/* Store ip address in cache (incomplete entry). */
1144a5d661aSToomas Soome 	al->addr = addr;
1154a5d661aSToomas Soome 
116*7b2a1233SToomas Soome 	pkt = NULL;
117*7b2a1233SToomas Soome 	ah = NULL;
1184a5d661aSToomas Soome 	i = sendrecv(d,
1194a5d661aSToomas Soome 	    arpsend, &wbuf.data, sizeof(wbuf.data),
120*7b2a1233SToomas Soome 	    arprecv, &pkt, (void **)&ah);
1214a5d661aSToomas Soome 	if (i == -1) {
1224a5d661aSToomas Soome 		panic("arp: no response for %s\n",
1234a5d661aSToomas Soome 			  inet_ntoa(addr));
1244a5d661aSToomas Soome 	}
1254a5d661aSToomas Soome 
1264a5d661aSToomas Soome 	/* Store ethernet address in cache */
1274a5d661aSToomas Soome #ifdef ARP_DEBUG
1284a5d661aSToomas Soome  	if (debug) {
129*7b2a1233SToomas Soome 		struct ether_header *eh;
130*7b2a1233SToomas Soome 
131*7b2a1233SToomas Soome 		eh = (struct ether_header *)((uintptr_t)pkt + ETHER_ALIGN);
1324a5d661aSToomas Soome 		printf("arp: response from %s\n",
133*7b2a1233SToomas Soome 		    ether_sprintf(eh->ether_shost));
1344a5d661aSToomas Soome 		printf("arp: cacheing %s --> %s\n",
1354a5d661aSToomas Soome 		    inet_ntoa(addr), ether_sprintf(ah->arp_sha));
1364a5d661aSToomas Soome 	}
1374a5d661aSToomas Soome #endif
1384a5d661aSToomas Soome 	MACPY(ah->arp_sha, al->ea);
1394a5d661aSToomas Soome 	++arp_num;
140*7b2a1233SToomas Soome 	free(pkt);
1414a5d661aSToomas Soome 	return (al->ea);
1424a5d661aSToomas Soome }
1434a5d661aSToomas Soome 
1444a5d661aSToomas Soome static ssize_t
145*7b2a1233SToomas Soome arpsend(struct iodesc *d, void *pkt, size_t len)
1464a5d661aSToomas Soome {
1474a5d661aSToomas Soome 
1484a5d661aSToomas Soome #ifdef ARP_DEBUG
1494a5d661aSToomas Soome  	if (debug)
1504a5d661aSToomas Soome 		printf("arpsend: called\n");
1514a5d661aSToomas Soome #endif
1524a5d661aSToomas Soome 
1534a5d661aSToomas Soome 	return (sendether(d, pkt, len, bcea, ETHERTYPE_ARP));
1544a5d661aSToomas Soome }
1554a5d661aSToomas Soome 
1564a5d661aSToomas Soome /*
1574a5d661aSToomas Soome  * Returns 0 if this is the packet we're waiting for
1584a5d661aSToomas Soome  * else -1 (and errno == 0)
1594a5d661aSToomas Soome  */
1604a5d661aSToomas Soome static ssize_t
161*7b2a1233SToomas Soome arprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft)
1624a5d661aSToomas Soome {
1634a5d661aSToomas Soome 	ssize_t n;
1644a5d661aSToomas Soome 	struct ether_arp *ah;
1654a5d661aSToomas Soome 	u_int16_t etype;	/* host order */
166*7b2a1233SToomas Soome 	void *ptr;
1674a5d661aSToomas Soome 
1684a5d661aSToomas Soome #ifdef ARP_DEBUG
1694a5d661aSToomas Soome  	if (debug)
1704a5d661aSToomas Soome 		printf("arprecv: ");
1714a5d661aSToomas Soome #endif
1724a5d661aSToomas Soome 
173*7b2a1233SToomas Soome 	ptr = NULL;
174*7b2a1233SToomas Soome 	n = readether(d, &ptr, (void **)&ah, tleft, &etype);
1754a5d661aSToomas Soome 	errno = 0;	/* XXX */
1764a5d661aSToomas Soome 	if (n == -1 || n < sizeof(struct ether_arp)) {
1774a5d661aSToomas Soome #ifdef ARP_DEBUG
1784a5d661aSToomas Soome 		if (debug)
1794a5d661aSToomas Soome 			printf("bad len=%d\n", n);
1804a5d661aSToomas Soome #endif
181*7b2a1233SToomas Soome 		free(ptr);
1824a5d661aSToomas Soome 		return (-1);
1834a5d661aSToomas Soome 	}
1844a5d661aSToomas Soome 
1854a5d661aSToomas Soome 	if (etype != ETHERTYPE_ARP) {
1864a5d661aSToomas Soome #ifdef ARP_DEBUG
1874a5d661aSToomas Soome 		if (debug)
1884a5d661aSToomas Soome 			printf("not arp type=%d\n", etype);
1894a5d661aSToomas Soome #endif
190*7b2a1233SToomas Soome 		free(ptr);
1914a5d661aSToomas Soome 		return (-1);
1924a5d661aSToomas Soome 	}
1934a5d661aSToomas Soome 
1944a5d661aSToomas Soome 	/* Ethernet address now checked in readether() */
1954a5d661aSToomas Soome 	if (ah->arp_hrd != htons(ARPHRD_ETHER) ||
1964a5d661aSToomas Soome 	    ah->arp_pro != htons(ETHERTYPE_IP) ||
1974a5d661aSToomas Soome 	    ah->arp_hln != sizeof(ah->arp_sha) ||
1984a5d661aSToomas Soome 	    ah->arp_pln != sizeof(ah->arp_spa) )
1994a5d661aSToomas Soome 	{
2004a5d661aSToomas Soome #ifdef ARP_DEBUG
2014a5d661aSToomas Soome 		if (debug)
2024a5d661aSToomas Soome 			printf("bad hrd/pro/hln/pln\n");
2034a5d661aSToomas Soome #endif
204*7b2a1233SToomas Soome 		free(ptr);
2054a5d661aSToomas Soome 		return (-1);
2064a5d661aSToomas Soome 	}
2074a5d661aSToomas Soome 
2084a5d661aSToomas Soome 	if (ah->arp_op == htons(ARPOP_REQUEST)) {
2094a5d661aSToomas Soome #ifdef ARP_DEBUG
2104a5d661aSToomas Soome 		if (debug)
2114a5d661aSToomas Soome 			printf("is request\n");
2124a5d661aSToomas Soome #endif
2134a5d661aSToomas Soome 		arp_reply(d, ah);
214*7b2a1233SToomas Soome 		free(ptr);
2154a5d661aSToomas Soome 		return (-1);
2164a5d661aSToomas Soome 	}
2174a5d661aSToomas Soome 
2184a5d661aSToomas Soome 	if (ah->arp_op != htons(ARPOP_REPLY)) {
2194a5d661aSToomas Soome #ifdef ARP_DEBUG
2204a5d661aSToomas Soome 		if (debug)
2214a5d661aSToomas Soome 			printf("not ARP reply\n");
2224a5d661aSToomas Soome #endif
223*7b2a1233SToomas Soome 		free(ptr);
2244a5d661aSToomas Soome 		return (-1);
2254a5d661aSToomas Soome 	}
2264a5d661aSToomas Soome 
2274a5d661aSToomas Soome 	/* Is the reply from the source we want? */
2284a5d661aSToomas Soome 	if (bcmp(&arp_list[arp_num].addr,
2294a5d661aSToomas Soome 			 ah->arp_spa, sizeof(ah->arp_spa)))
2304a5d661aSToomas Soome 	{
2314a5d661aSToomas Soome #ifdef ARP_DEBUG
2324a5d661aSToomas Soome 		if (debug)
2334a5d661aSToomas Soome 			printf("unwanted address\n");
2344a5d661aSToomas Soome #endif
235*7b2a1233SToomas Soome 		free(ptr);
2364a5d661aSToomas Soome 		return (-1);
2374a5d661aSToomas Soome 	}
2384a5d661aSToomas Soome 	/* We don't care who the reply was sent to. */
2394a5d661aSToomas Soome 
2404a5d661aSToomas Soome 	/* We have our answer. */
2414a5d661aSToomas Soome #ifdef ARP_DEBUG
2424a5d661aSToomas Soome  	if (debug)
2434a5d661aSToomas Soome 		printf("got it\n");
2444a5d661aSToomas Soome #endif
245*7b2a1233SToomas Soome 	*pkt = ptr;
246*7b2a1233SToomas Soome 	*payload = ah;
2474a5d661aSToomas Soome 	return (n);
2484a5d661aSToomas Soome }
2494a5d661aSToomas Soome 
2504a5d661aSToomas Soome /*
2514a5d661aSToomas Soome  * Convert an ARP request into a reply and send it.
2524a5d661aSToomas Soome  * Notes:  Re-uses buffer.  Pad to length = 46.
2534a5d661aSToomas Soome  */
2544a5d661aSToomas Soome void
255*7b2a1233SToomas Soome arp_reply(struct iodesc *d, void *pkt)
2564a5d661aSToomas Soome {
2574a5d661aSToomas Soome 	struct ether_arp *arp = pkt;
2584a5d661aSToomas Soome 
2594a5d661aSToomas Soome 	if (arp->arp_hrd != htons(ARPHRD_ETHER) ||
2604a5d661aSToomas Soome 	    arp->arp_pro != htons(ETHERTYPE_IP) ||
2614a5d661aSToomas Soome 	    arp->arp_hln != sizeof(arp->arp_sha) ||
2624a5d661aSToomas Soome 	    arp->arp_pln != sizeof(arp->arp_spa) )
2634a5d661aSToomas Soome 	{
2644a5d661aSToomas Soome #ifdef ARP_DEBUG
2654a5d661aSToomas Soome 		if (debug)
2664a5d661aSToomas Soome 			printf("arp_reply: bad hrd/pro/hln/pln\n");
2674a5d661aSToomas Soome #endif
2684a5d661aSToomas Soome 		return;
2694a5d661aSToomas Soome 	}
2704a5d661aSToomas Soome 
2714a5d661aSToomas Soome 	if (arp->arp_op != htons(ARPOP_REQUEST)) {
2724a5d661aSToomas Soome #ifdef ARP_DEBUG
2734a5d661aSToomas Soome 		if (debug)
2744a5d661aSToomas Soome 			printf("arp_reply: not request!\n");
2754a5d661aSToomas Soome #endif
2764a5d661aSToomas Soome 		return;
2774a5d661aSToomas Soome 	}
2784a5d661aSToomas Soome 
2794a5d661aSToomas Soome 	/* If we are not the target, ignore the request. */
2804a5d661aSToomas Soome 	if (bcmp(arp->arp_tpa, &d->myip, sizeof(arp->arp_tpa)))
2814a5d661aSToomas Soome 		return;
2824a5d661aSToomas Soome 
2834a5d661aSToomas Soome #ifdef ARP_DEBUG
2844a5d661aSToomas Soome 	if (debug) {
2854a5d661aSToomas Soome 		printf("arp_reply: to %s\n", ether_sprintf(arp->arp_sha));
2864a5d661aSToomas Soome 	}
2874a5d661aSToomas Soome #endif
2884a5d661aSToomas Soome 
2894a5d661aSToomas Soome 	arp->arp_op = htons(ARPOP_REPLY);
2904a5d661aSToomas Soome 	/* source becomes target */
2914a5d661aSToomas Soome 	bcopy(arp->arp_sha, arp->arp_tha, sizeof(arp->arp_tha));
2924a5d661aSToomas Soome 	bcopy(arp->arp_spa, arp->arp_tpa, sizeof(arp->arp_tpa));
2934a5d661aSToomas Soome 	/* here becomes source */
2944a5d661aSToomas Soome 	bcopy(d->myea,  arp->arp_sha, sizeof(arp->arp_sha));
2954a5d661aSToomas Soome 	bcopy(&d->myip, arp->arp_spa, sizeof(arp->arp_spa));
2964a5d661aSToomas Soome 
2974a5d661aSToomas Soome 	/*
2984a5d661aSToomas Soome 	 * No need to get fancy here.  If the send fails, the
2994a5d661aSToomas Soome 	 * requestor will just ask again.
3004a5d661aSToomas Soome 	 */
3014a5d661aSToomas Soome 	(void) sendether(d, pkt, sizeof(*arp) + 18,
3024a5d661aSToomas Soome 	                 arp->arp_tha, ETHERTYPE_ARP);
3034a5d661aSToomas Soome }
304