xref: /illumos-gate/usr/src/boot/libsa/arp.c (revision 22028508)
1199767f8SToomas Soome /*	$NetBSD: arp.c,v 1.18 1997/07/07 15:52:49 drochner Exp $	*/
2199767f8SToomas Soome 
3199767f8SToomas Soome /*
4199767f8SToomas Soome  * Copyright (c) 1992 Regents of the University of California.
5199767f8SToomas Soome  * All rights reserved.
6199767f8SToomas Soome  *
7199767f8SToomas Soome  * This software was developed by the Computer Systems Engineering group
8199767f8SToomas Soome  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9199767f8SToomas Soome  * contributed to Berkeley.
10199767f8SToomas Soome  *
11199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
12199767f8SToomas Soome  * modification, are permitted provided that the following conditions
13199767f8SToomas Soome  * are met:
14199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
15199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
16199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
17199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
18199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
191974da4bSToomas Soome  * 3. Neither the name of the University nor the names of its contributors
20199767f8SToomas Soome  *    may be used to endorse or promote products derived from this software
21199767f8SToomas Soome  *    without specific prior written permission.
22199767f8SToomas Soome  *
23199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33199767f8SToomas Soome  * SUCH DAMAGE.
34199767f8SToomas Soome  *
35199767f8SToomas Soome  * @(#) Header: arp.c,v 1.5 93/07/15 05:52:26 leres Exp  (LBL)
36199767f8SToomas Soome  */
37199767f8SToomas Soome 
38199767f8SToomas Soome #include <sys/cdefs.h>
39199767f8SToomas Soome 
40199767f8SToomas Soome #include <sys/types.h>
41199767f8SToomas Soome #include <sys/socket.h>
42199767f8SToomas Soome #include <net/if.h>
43199767f8SToomas Soome #include <netinet/in.h>
44199767f8SToomas Soome #include <netinet/if_ether.h>
45199767f8SToomas Soome 
46199767f8SToomas Soome #include <netinet/in_systm.h>
47199767f8SToomas Soome 
48199767f8SToomas Soome #include <string.h>
49199767f8SToomas Soome 
50199767f8SToomas Soome #include "stand.h"
51199767f8SToomas Soome #include "net.h"
52199767f8SToomas Soome 
53199767f8SToomas Soome /* Cache stuff */
54a40552edSToomas Soome #define	ARP_NUM 8			/* need at most 3 arp entries */
55199767f8SToomas Soome 
56199767f8SToomas Soome struct arp_list {
57199767f8SToomas Soome 	struct in_addr	addr;
58a40552edSToomas Soome 	uchar_t		ea[6];
59199767f8SToomas Soome } arp_list[ARP_NUM] = {
60199767f8SToomas Soome 	/* XXX - net order `INADDR_BROADCAST' must be a constant */
61199767f8SToomas Soome 	{ {0xffffffff}, BA }
62199767f8SToomas Soome };
63199767f8SToomas Soome int arp_num = 1;
64199767f8SToomas Soome 
65199767f8SToomas Soome /* Local forwards */
66199767f8SToomas Soome static	ssize_t arpsend(struct iodesc *, void *, size_t);
67890c8671SToomas Soome static	ssize_t arprecv(struct iodesc *, void **, void **, time_t, void *);
68199767f8SToomas Soome 
69199767f8SToomas Soome /* Broadcast an ARP packet, asking who has addr on interface d */
70a40552edSToomas Soome uchar_t *
arpwhohas(struct iodesc * d,struct in_addr addr)71859472daSToomas Soome arpwhohas(struct iodesc *d, struct in_addr addr)
72199767f8SToomas Soome {
73199767f8SToomas Soome 	int i;
74199767f8SToomas Soome 	struct ether_arp *ah;
75199767f8SToomas Soome 	struct arp_list *al;
76859472daSToomas Soome 	void *pkt;
77199767f8SToomas Soome 	struct {
78199767f8SToomas Soome 		struct ether_header eh;
79199767f8SToomas Soome 		struct {
80199767f8SToomas Soome 			struct ether_arp arp;
81a40552edSToomas Soome 			uchar_t pad[18];		/* 60 - sizeof (...) */
82199767f8SToomas Soome 		} data;
83199767f8SToomas Soome 	} wbuf;
84199767f8SToomas Soome 
85199767f8SToomas Soome 	/* Try for cached answer first */
86199767f8SToomas Soome 	for (i = 0, al = arp_list; i < arp_num; ++i, ++al)
87199767f8SToomas Soome 		if (addr.s_addr == al->addr.s_addr)
88199767f8SToomas Soome 			return (al->ea);
89199767f8SToomas Soome 
90199767f8SToomas Soome 	/* Don't overflow cache */
91199767f8SToomas Soome 	if (arp_num > ARP_NUM - 1) {
92199767f8SToomas Soome 		arp_num = 1;	/* recycle */
93199767f8SToomas Soome 		printf("arpwhohas: overflowed arp_list!\n");
94199767f8SToomas Soome 	}
95199767f8SToomas Soome 
96199767f8SToomas Soome #ifdef ARP_DEBUG
97a40552edSToomas Soome 	if (debug)
98a40552edSToomas Soome 		printf("arpwhohas: send request for %s\n", inet_ntoa(addr));
99199767f8SToomas Soome #endif
100199767f8SToomas Soome 
101a40552edSToomas Soome 	bzero((char *)&wbuf.data, sizeof (wbuf.data));
102199767f8SToomas Soome 	ah = &wbuf.data.arp;
103199767f8SToomas Soome 	ah->arp_hrd = htons(ARPHRD_ETHER);
104199767f8SToomas Soome 	ah->arp_pro = htons(ETHERTYPE_IP);
105a40552edSToomas Soome 	ah->arp_hln = sizeof (ah->arp_sha); /* hardware address length */
106a40552edSToomas Soome 	ah->arp_pln = sizeof (ah->arp_spa); /* protocol address length */
107199767f8SToomas Soome 	ah->arp_op = htons(ARPOP_REQUEST);
108199767f8SToomas Soome 	MACPY(d->myea, ah->arp_sha);
109a40552edSToomas Soome 	bcopy(&d->myip, ah->arp_spa, sizeof (ah->arp_spa));
110199767f8SToomas Soome 	/* Leave zeros in arp_tha */
111a40552edSToomas Soome 	bcopy(&addr, ah->arp_tpa, sizeof (ah->arp_tpa));
112199767f8SToomas Soome 
113199767f8SToomas Soome 	/* Store ip address in cache (incomplete entry). */
114199767f8SToomas Soome 	al->addr = addr;
115199767f8SToomas Soome 
116859472daSToomas Soome 	pkt = NULL;
117859472daSToomas Soome 	ah = NULL;
118199767f8SToomas Soome 	i = sendrecv(d,
119a40552edSToomas Soome 	    arpsend, &wbuf.data, sizeof (wbuf.data),
120890c8671SToomas Soome 	    arprecv, &pkt, (void **)&ah, NULL);
12196cf0467SToomas Soome 	if (i == -1)
12296cf0467SToomas Soome 		panic("arp: no response for %s", inet_ntoa(addr));
123199767f8SToomas Soome 
124199767f8SToomas Soome 	/* Store ethernet address in cache */
125199767f8SToomas Soome #ifdef ARP_DEBUG
126a40552edSToomas Soome 	if (debug) {
127859472daSToomas Soome 		struct ether_header *eh;
128859472daSToomas Soome 
129859472daSToomas Soome 		eh = (struct ether_header *)((uintptr_t)pkt + ETHER_ALIGN);
130199767f8SToomas Soome 		printf("arp: response from %s\n",
131859472daSToomas Soome 		    ether_sprintf(eh->ether_shost));
132199767f8SToomas Soome 		printf("arp: cacheing %s --> %s\n",
133199767f8SToomas Soome 		    inet_ntoa(addr), ether_sprintf(ah->arp_sha));
134199767f8SToomas Soome 	}
135199767f8SToomas Soome #endif
136199767f8SToomas Soome 	MACPY(ah->arp_sha, al->ea);
137199767f8SToomas Soome 	++arp_num;
138859472daSToomas Soome 	free(pkt);
139199767f8SToomas Soome 	return (al->ea);
140199767f8SToomas Soome }
141199767f8SToomas Soome 
142199767f8SToomas Soome static ssize_t
arpsend(struct iodesc * d,void * pkt,size_t len)143859472daSToomas Soome arpsend(struct iodesc *d, void *pkt, size_t len)
144199767f8SToomas Soome {
145199767f8SToomas Soome 
146199767f8SToomas Soome #ifdef ARP_DEBUG
147a40552edSToomas Soome 	if (debug)
148199767f8SToomas Soome 		printf("arpsend: called\n");
149199767f8SToomas Soome #endif
150199767f8SToomas Soome 
151199767f8SToomas Soome 	return (sendether(d, pkt, len, bcea, ETHERTYPE_ARP));
152199767f8SToomas Soome }
153199767f8SToomas Soome 
154199767f8SToomas Soome /*
155199767f8SToomas Soome  * Returns 0 if this is the packet we're waiting for
156199767f8SToomas Soome  * else -1 (and errno == 0)
157199767f8SToomas Soome  */
158199767f8SToomas Soome static ssize_t
arprecv(struct iodesc * d,void ** pkt,void ** payload,time_t tleft,void * extra __unused)1598eef2ab6SToomas Soome arprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft,
1608eef2ab6SToomas Soome     void *extra __unused)
161199767f8SToomas Soome {
162199767f8SToomas Soome 	ssize_t n;
163199767f8SToomas Soome 	struct ether_arp *ah;
164a40552edSToomas Soome 	uint16_t etype;	/* host order */
165859472daSToomas Soome 	void *ptr;
166199767f8SToomas Soome 
167199767f8SToomas Soome #ifdef ARP_DEBUG
168a40552edSToomas Soome 	if (debug)
169199767f8SToomas Soome 		printf("arprecv: ");
170199767f8SToomas Soome #endif
171199767f8SToomas Soome 
172859472daSToomas Soome 	ptr = NULL;
173859472daSToomas Soome 	n = readether(d, &ptr, (void **)&ah, tleft, &etype);
174199767f8SToomas Soome 	errno = 0;	/* XXX */
175a40552edSToomas Soome 	if (n == -1 || n < sizeof (struct ether_arp)) {
176199767f8SToomas Soome #ifdef ARP_DEBUG
177199767f8SToomas Soome 		if (debug)
178199767f8SToomas Soome 			printf("bad len=%d\n", n);
179199767f8SToomas Soome #endif
180859472daSToomas Soome 		free(ptr);
181199767f8SToomas Soome 		return (-1);
182199767f8SToomas Soome 	}
183199767f8SToomas Soome 
184199767f8SToomas Soome 	if (etype != ETHERTYPE_ARP) {
185199767f8SToomas Soome #ifdef ARP_DEBUG
186199767f8SToomas Soome 		if (debug)
187199767f8SToomas Soome 			printf("not arp type=%d\n", etype);
188199767f8SToomas Soome #endif
189859472daSToomas Soome 		free(ptr);
190199767f8SToomas Soome 		return (-1);
191199767f8SToomas Soome 	}
192199767f8SToomas Soome 
193199767f8SToomas Soome 	/* Ethernet address now checked in readether() */
194199767f8SToomas Soome 	if (ah->arp_hrd != htons(ARPHRD_ETHER) ||
195199767f8SToomas Soome 	    ah->arp_pro != htons(ETHERTYPE_IP) ||
196a40552edSToomas Soome 	    ah->arp_hln != sizeof (ah->arp_sha) ||
197a40552edSToomas Soome 	    ah->arp_pln != sizeof (ah->arp_spa)) {
198199767f8SToomas Soome #ifdef ARP_DEBUG
199199767f8SToomas Soome 		if (debug)
200199767f8SToomas Soome 			printf("bad hrd/pro/hln/pln\n");
201199767f8SToomas Soome #endif
202859472daSToomas Soome 		free(ptr);
203199767f8SToomas Soome 		return (-1);
204199767f8SToomas Soome 	}
205199767f8SToomas Soome 
206199767f8SToomas Soome 	if (ah->arp_op == htons(ARPOP_REQUEST)) {
207199767f8SToomas Soome #ifdef ARP_DEBUG
208199767f8SToomas Soome 		if (debug)
209199767f8SToomas Soome 			printf("is request\n");
210199767f8SToomas Soome #endif
211199767f8SToomas Soome 		arp_reply(d, ah);
212859472daSToomas Soome 		free(ptr);
213199767f8SToomas Soome 		return (-1);
214199767f8SToomas Soome 	}
215199767f8SToomas Soome 
216199767f8SToomas Soome 	if (ah->arp_op != htons(ARPOP_REPLY)) {
217199767f8SToomas Soome #ifdef ARP_DEBUG
218199767f8SToomas Soome 		if (debug)
219199767f8SToomas Soome 			printf("not ARP reply\n");
220199767f8SToomas Soome #endif
221859472daSToomas Soome 		free(ptr);
222199767f8SToomas Soome 		return (-1);
223199767f8SToomas Soome 	}
224199767f8SToomas Soome 
225199767f8SToomas Soome 	/* Is the reply from the source we want? */
226a40552edSToomas Soome 	if (bcmp(&arp_list[arp_num].addr, ah->arp_spa, sizeof (ah->arp_spa))) {
227199767f8SToomas Soome #ifdef ARP_DEBUG
228199767f8SToomas Soome 		if (debug)
229199767f8SToomas Soome 			printf("unwanted address\n");
230199767f8SToomas Soome #endif
231859472daSToomas Soome 		free(ptr);
232199767f8SToomas Soome 		return (-1);
233199767f8SToomas Soome 	}
234199767f8SToomas Soome 	/* We don't care who the reply was sent to. */
235199767f8SToomas Soome 
236199767f8SToomas Soome 	/* We have our answer. */
237199767f8SToomas Soome #ifdef ARP_DEBUG
238a40552edSToomas Soome 	if (debug)
239199767f8SToomas Soome 		printf("got it\n");
240199767f8SToomas Soome #endif
241859472daSToomas Soome 	*pkt = ptr;
242859472daSToomas Soome 	*payload = ah;
243199767f8SToomas Soome 	return (n);
244199767f8SToomas Soome }
245199767f8SToomas Soome 
246199767f8SToomas Soome /*
247199767f8SToomas Soome  * Convert an ARP request into a reply and send it.
248199767f8SToomas Soome  * Notes:  Re-uses buffer.  Pad to length = 46.
249199767f8SToomas Soome  */
250199767f8SToomas Soome void
arp_reply(struct iodesc * d,void * pkt)251859472daSToomas Soome arp_reply(struct iodesc *d, void *pkt)
252199767f8SToomas Soome {
253199767f8SToomas Soome 	struct ether_arp *arp = pkt;
254199767f8SToomas Soome 
255199767f8SToomas Soome 	if (arp->arp_hrd != htons(ARPHRD_ETHER) ||
256199767f8SToomas Soome 	    arp->arp_pro != htons(ETHERTYPE_IP) ||
257a40552edSToomas Soome 	    arp->arp_hln != sizeof (arp->arp_sha) ||
258a40552edSToomas Soome 	    arp->arp_pln != sizeof (arp->arp_spa)) {
259199767f8SToomas Soome #ifdef ARP_DEBUG
260199767f8SToomas Soome 		if (debug)
261199767f8SToomas Soome 			printf("arp_reply: bad hrd/pro/hln/pln\n");
262199767f8SToomas Soome #endif
263199767f8SToomas Soome 		return;
264199767f8SToomas Soome 	}
265199767f8SToomas Soome 
266199767f8SToomas Soome 	if (arp->arp_op != htons(ARPOP_REQUEST)) {
267199767f8SToomas Soome #ifdef ARP_DEBUG
268199767f8SToomas Soome 		if (debug)
269199767f8SToomas Soome 			printf("arp_reply: not request!\n");
270199767f8SToomas Soome #endif
271199767f8SToomas Soome 		return;
272199767f8SToomas Soome 	}
273199767f8SToomas Soome 
274199767f8SToomas Soome 	/* If we are not the target, ignore the request. */
275a40552edSToomas Soome 	if (bcmp(arp->arp_tpa, &d->myip, sizeof (arp->arp_tpa)))
276199767f8SToomas Soome 		return;
277199767f8SToomas Soome 
278199767f8SToomas Soome #ifdef ARP_DEBUG
279199767f8SToomas Soome 	if (debug) {
280199767f8SToomas Soome 		printf("arp_reply: to %s\n", ether_sprintf(arp->arp_sha));
281199767f8SToomas Soome 	}
282199767f8SToomas Soome #endif
283199767f8SToomas Soome 
284199767f8SToomas Soome 	arp->arp_op = htons(ARPOP_REPLY);
285199767f8SToomas Soome 	/* source becomes target */
286a40552edSToomas Soome 	bcopy(arp->arp_sha, arp->arp_tha, sizeof (arp->arp_tha));
287a40552edSToomas Soome 	bcopy(arp->arp_spa, arp->arp_tpa, sizeof (arp->arp_tpa));
288199767f8SToomas Soome 	/* here becomes source */
289a40552edSToomas Soome 	bcopy(d->myea,  arp->arp_sha, sizeof (arp->arp_sha));
290a40552edSToomas Soome 	bcopy(&d->myip, arp->arp_spa, sizeof (arp->arp_spa));
291199767f8SToomas Soome 
292199767f8SToomas Soome 	/*
293199767f8SToomas Soome 	 * No need to get fancy here.  If the send fails, the
294199767f8SToomas Soome 	 * requestor will just ask again.
295199767f8SToomas Soome 	 */
296a40552edSToomas Soome 	(void) sendether(d, pkt, sizeof (*arp) + 18,
297a40552edSToomas Soome 	    arp->arp_tha, ETHERTYPE_ARP);
298199767f8SToomas Soome }
299