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