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