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