xref: /netbsd-src/sys/lib/libsa/arp.c (revision 10497fd285fb21c25f76e74beeb53b30864bc3f3)
1*10497fd2Schristos /*	$NetBSD: arp.c,v 1.35 2019/03/31 20:08:45 christos Exp $	*/
26668f51cScgd 
3e03842d0Sbrezak /*
4e03842d0Sbrezak  * Copyright (c) 1992 Regents of the University of California.
5e03842d0Sbrezak  * All rights reserved.
6e03842d0Sbrezak  *
7e03842d0Sbrezak  * This software was developed by the Computer Systems Engineering group
8e03842d0Sbrezak  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9e03842d0Sbrezak  * contributed to Berkeley.
10e03842d0Sbrezak  *
11e03842d0Sbrezak  * Redistribution and use in source and binary forms, with or without
12e03842d0Sbrezak  * modification, are permitted provided that the following conditions
13e03842d0Sbrezak  * are met:
14e03842d0Sbrezak  * 1. Redistributions of source code must retain the above copyright
15e03842d0Sbrezak  *    notice, this list of conditions and the following disclaimer.
16e03842d0Sbrezak  * 2. Redistributions in binary form must reproduce the above copyright
17e03842d0Sbrezak  *    notice, this list of conditions and the following disclaimer in the
18e03842d0Sbrezak  *    documentation and/or other materials provided with the distribution.
19e03842d0Sbrezak  * 3. All advertising materials mentioning features or use of this software
20e03842d0Sbrezak  *    must display the following acknowledgement:
21e03842d0Sbrezak  *	This product includes software developed by the University of
22e03842d0Sbrezak  *	California, Lawrence Berkeley Laboratory and its contributors.
23e03842d0Sbrezak  * 4. Neither the name of the University nor the names of its contributors
24e03842d0Sbrezak  *    may be used to endorse or promote products derived from this software
25e03842d0Sbrezak  *    without specific prior written permission.
26e03842d0Sbrezak  *
27e03842d0Sbrezak  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28e03842d0Sbrezak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29e03842d0Sbrezak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30e03842d0Sbrezak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31e03842d0Sbrezak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32e03842d0Sbrezak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33e03842d0Sbrezak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34e03842d0Sbrezak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35e03842d0Sbrezak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36e03842d0Sbrezak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37e03842d0Sbrezak  * SUCH DAMAGE.
38e03842d0Sbrezak  *
396668f51cScgd  * @(#) Header: arp.c,v 1.5 93/07/15 05:52:26 leres Exp  (LBL)
40e03842d0Sbrezak  */
41e03842d0Sbrezak 
42e03842d0Sbrezak #include <sys/types.h>
43e03842d0Sbrezak #include <sys/socket.h>
44e03842d0Sbrezak #include <net/if.h>
4564cd693aSdrochner #include <net/if_ether.h>
46e03842d0Sbrezak #include <netinet/in.h>
47e03842d0Sbrezak 
48e03842d0Sbrezak #include <netinet/in_systm.h>
49e03842d0Sbrezak 
5036ff5d93Sthorpej #ifdef _STANDALONE
5136ff5d93Sthorpej #include <lib/libkern/libkern.h>
5236ff5d93Sthorpej #else
53*10497fd2Schristos #include <arpa/inet.h>
5436ff5d93Sthorpej #include <string.h>
5536ff5d93Sthorpej #endif
5636ff5d93Sthorpej 
57e03842d0Sbrezak #include "stand.h"
58e03842d0Sbrezak #include "net.h"
59e03842d0Sbrezak 
6064cd693aSdrochner /*
6164cd693aSdrochner  * Ethernet Address Resolution Protocol.
6264cd693aSdrochner  *
6364cd693aSdrochner  * See RFC 826 for protocol description.  Structure below is adapted
6464cd693aSdrochner  * to resolving internet addresses.  Field names used correspond to
6564cd693aSdrochner  * RFC 826.
6664cd693aSdrochner  */
6764cd693aSdrochner struct ether_arp {
6864cd693aSdrochner 	struct	 arphdr ea_hdr;			/* fixed-size header */
6964cd693aSdrochner 	u_int8_t arp_sha[ETHER_ADDR_LEN];	/* sender hardware address */
7064cd693aSdrochner 	u_int8_t arp_spa[4];			/* sender protocol address */
7164cd693aSdrochner 	u_int8_t arp_tha[ETHER_ADDR_LEN];	/* target hardware address */
7264cd693aSdrochner 	u_int8_t arp_tpa[4];			/* target protocol address */
7364cd693aSdrochner };
7464cd693aSdrochner #define	arp_hrd	ea_hdr.ar_hrd
7564cd693aSdrochner #define	arp_pro	ea_hdr.ar_pro
7664cd693aSdrochner #define	arp_hln	ea_hdr.ar_hln
7764cd693aSdrochner #define	arp_pln	ea_hdr.ar_pln
7864cd693aSdrochner #define	arp_op	ea_hdr.ar_op
7964cd693aSdrochner 
80e03842d0Sbrezak /* Cache stuff */
81e03842d0Sbrezak #define ARP_NUM 8			/* need at most 3 arp entries */
82e03842d0Sbrezak 
8363a836a4Sgwr struct arp_list {
84d9124da4Spk 	struct in_addr	addr;
85836038fcSjakllsch 	u_char		ea[ETHER_ADDR_LEN];
86e03842d0Sbrezak } arp_list[ARP_NUM] = {
87de4e6515Spk 	/* XXX - net order `INADDR_BROADCAST' must be a constant */
88de4e6515Spk 	{ {0xffffffff}, BA }
89e03842d0Sbrezak };
9063a836a4Sgwr int arp_num = 1;
91e03842d0Sbrezak 
92e03842d0Sbrezak /* Local forwards */
933f38114dStsutsui static	ssize_t arpsend(struct iodesc *, void *, size_t);
943f38114dStsutsui static	ssize_t arprecv(struct iodesc *, void *, size_t, saseconds_t);
95e03842d0Sbrezak 
96e03842d0Sbrezak /* Broadcast an ARP packet, asking who has addr on interface d */
97e03842d0Sbrezak u_char *
arpwhohas(struct iodesc * d,struct in_addr addr)981c038e68Sisaki arpwhohas(struct iodesc *d, struct in_addr addr)
99e03842d0Sbrezak {
1001279e67bSaugustss 	int i;
101*10497fd2Schristos 	ssize_t ns;
1021279e67bSaugustss 	struct ether_arp *ah;
1031279e67bSaugustss 	struct arp_list *al;
104e03842d0Sbrezak 	struct {
10563a836a4Sgwr 		struct ether_header eh;
1060e4efb30Sgwr 		struct {
1070e4efb30Sgwr 			struct ether_arp arp;
10863a836a4Sgwr 			u_char pad[18]; 	/* 60 - sizeof(...) */
1090e4efb30Sgwr 		} data;
110e03842d0Sbrezak 	} wbuf;
111e03842d0Sbrezak 	struct {
11263a836a4Sgwr 		struct ether_header eh;
1130e4efb30Sgwr 		struct {
1140e4efb30Sgwr 			struct ether_arp arp;
1150e4efb30Sgwr 			u_char pad[24]; 	/* extra space */
1160e4efb30Sgwr 		} data;
117e03842d0Sbrezak 	} rbuf;
118e03842d0Sbrezak 
119e03842d0Sbrezak 	/* Try for cached answer first */
120e03842d0Sbrezak 	for (i = 0, al = arp_list; i < arp_num; ++i, ++al)
121d9124da4Spk 		if (addr.s_addr == al->addr.s_addr)
1221c038e68Sisaki 			return al->ea;
123e03842d0Sbrezak 
124e03842d0Sbrezak 	/* Don't overflow cache */
12563a836a4Sgwr 	if (arp_num > ARP_NUM - 1) {
12663a836a4Sgwr 		arp_num = 1;	/* recycle */
127*10497fd2Schristos 		printf("%s: overflowed arp_list!\n", __func__);
12863a836a4Sgwr 	}
129e03842d0Sbrezak 
130e03842d0Sbrezak #ifdef ARP_DEBUG
131e03842d0Sbrezak  	if (debug)
132*10497fd2Schristos  		printf("%s: send request for %s\n", __func__, inet_ntoa(addr));
133e03842d0Sbrezak #endif
134258efc53Smycroft 
135e1b834bdSchristos 	(void)memset(&wbuf.data, 0, sizeof(wbuf.data));
1360e4efb30Sgwr 	ah = &wbuf.data.arp;
137e03842d0Sbrezak 	ah->arp_hrd = htons(ARPHRD_ETHER);
138e03842d0Sbrezak 	ah->arp_pro = htons(ETHERTYPE_IP);
139e03842d0Sbrezak 	ah->arp_hln = sizeof(ah->arp_sha); /* hardware address length */
140e03842d0Sbrezak 	ah->arp_pln = sizeof(ah->arp_spa); /* protocol address length */
141e03842d0Sbrezak 	ah->arp_op = htons(ARPOP_REQUEST);
142e03842d0Sbrezak 	MACPY(d->myea, ah->arp_sha);
143e1b834bdSchristos 	(void)memcpy(ah->arp_spa, &d->myip, sizeof(ah->arp_spa));
14463a836a4Sgwr 	/* Leave zeros in arp_tha */
145e1b834bdSchristos 	(void)memcpy(ah->arp_tpa, &addr, sizeof(ah->arp_tpa));
146e03842d0Sbrezak 
14763a836a4Sgwr 	/* Store ip address in cache (incomplete entry). */
148e03842d0Sbrezak 	al->addr = addr;
149e03842d0Sbrezak 
150*10497fd2Schristos 	ns = sendrecv(d,
1510e4efb30Sgwr 	    arpsend, &wbuf.data, sizeof(wbuf.data),
1520e4efb30Sgwr 	    arprecv, &rbuf.data, sizeof(rbuf.data));
153*10497fd2Schristos 	if (ns == -1) {
154*10497fd2Schristos 		panic("%s: no response for %s", __func__, inet_ntoa(addr));
15563a836a4Sgwr 	}
156e03842d0Sbrezak 
157e03842d0Sbrezak 	/* Store ethernet address in cache */
1586f2a9404Sgwr 	ah = &rbuf.data.arp;
1596f2a9404Sgwr #ifdef ARP_DEBUG
16063a836a4Sgwr  	if (debug) {
161*10497fd2Schristos 		printf("%s: response from %s\n", __func__,
16263a836a4Sgwr 		    ether_sprintf(rbuf.eh.ether_shost));
163*10497fd2Schristos 		printf("%s: cacheing %s --> %s\n", __func__,
16460d20197Schristos 		    inet_ntoa(addr), ether_sprintf(ah->arp_sha));
16563a836a4Sgwr 	}
1666f2a9404Sgwr #endif
1676f2a9404Sgwr 	MACPY(ah->arp_sha, al->ea);
168e03842d0Sbrezak 	++arp_num;
169258efc53Smycroft 
1701c038e68Sisaki 	return al->ea;
171e03842d0Sbrezak }
172e03842d0Sbrezak 
17384c517c1Spk static ssize_t
arpsend(struct iodesc * d,void * pkt,size_t len)1741c038e68Sisaki arpsend(struct iodesc *d, void *pkt, size_t len)
175e03842d0Sbrezak {
176258efc53Smycroft 
177e03842d0Sbrezak #ifdef ARP_DEBUG
178e03842d0Sbrezak  	if (debug)
179*10497fd2Schristos 		printf("%s: called\n", __func__);
180e03842d0Sbrezak #endif
181258efc53Smycroft 
1821c038e68Sisaki 	return sendether(d, pkt, len, bcea, ETHERTYPE_ARP);
183e03842d0Sbrezak }
184e03842d0Sbrezak 
1856f2a9404Sgwr /*
1866f2a9404Sgwr  * Returns 0 if this is the packet we're waiting for
1876f2a9404Sgwr  * else -1 (and errno == 0)
1886f2a9404Sgwr  */
18984c517c1Spk static ssize_t
arprecv(struct iodesc * d,void * pkt,size_t len,saseconds_t tleft)19069cf32a7Stsutsui arprecv(struct iodesc *d, void *pkt, size_t len, saseconds_t tleft)
191e03842d0Sbrezak {
1921279e67bSaugustss 	ssize_t n;
1931279e67bSaugustss 	struct ether_arp *ah;
194eb50acd9Sthorpej 	u_int16_t etype;	/* host order */
195e03842d0Sbrezak 
196e03842d0Sbrezak #ifdef ARP_DEBUG
197e03842d0Sbrezak 	if (debug)
198*10497fd2Schristos 		printf("%s: ", __func__);
199e03842d0Sbrezak #endif
20084c517c1Spk 	n = readether(d, pkt, len, tleft, &etype);
2016f2a9404Sgwr 	errno = 0;	/* XXX */
20260ae17c7Sfvdl 	if (n == -1 || (size_t)n < sizeof(struct ether_arp)) {
2030e4efb30Sgwr #ifdef ARP_DEBUG
2040e4efb30Sgwr 		if (debug)
205*10497fd2Schristos 			printf("bad len=%zd\n", n);
2060e4efb30Sgwr #endif
2071c038e68Sisaki 		return -1;
2080e4efb30Sgwr 	}
209e03842d0Sbrezak 
210eb50acd9Sthorpej 	if (etype != ETHERTYPE_ARP) {
2110e4efb30Sgwr #ifdef ARP_DEBUG
2120e4efb30Sgwr 		if (debug)
213e44c1d1fSchristos 			printf("not arp type=%d\n", etype);
214e03842d0Sbrezak #endif
2151c038e68Sisaki 		return -1;
216e03842d0Sbrezak 	}
217e03842d0Sbrezak 
218eb50acd9Sthorpej 	/* Ethernet address now checked in readether() */
219eb50acd9Sthorpej 
220258efc53Smycroft 	ah = (struct ether_arp *)pkt;
221eb50acd9Sthorpej 	if (ah->arp_hrd != htons(ARPHRD_ETHER) ||
222eb50acd9Sthorpej 	    ah->arp_pro != htons(ETHERTYPE_IP) ||
223e03842d0Sbrezak 	    ah->arp_hln != sizeof(ah->arp_sha) ||
224eb50acd9Sthorpej 	    ah->arp_pln != sizeof(ah->arp_spa) )
225eb50acd9Sthorpej 	{
226e03842d0Sbrezak #ifdef ARP_DEBUG
227e03842d0Sbrezak 		if (debug)
228e44c1d1fSchristos 			printf("bad hrd/pro/hln/pln\n");
229e03842d0Sbrezak #endif
2301c038e68Sisaki 		return -1;
231e03842d0Sbrezak 	}
232eb50acd9Sthorpej 
233eb50acd9Sthorpej 	if (ah->arp_op == htons(ARPOP_REQUEST)) {
2346f2a9404Sgwr #ifdef ARP_DEBUG
2356f2a9404Sgwr 		if (debug)
236e44c1d1fSchristos 			printf("is request\n");
2376f2a9404Sgwr #endif
238eb50acd9Sthorpej 		arp_reply(d, ah);
2391c038e68Sisaki 		return -1;
240eb50acd9Sthorpej 	}
241eb50acd9Sthorpej 
242eb50acd9Sthorpej 	if (ah->arp_op != htons(ARPOP_REPLY)) {
243eb50acd9Sthorpej #ifdef ARP_DEBUG
244eb50acd9Sthorpej 		if (debug)
245e44c1d1fSchristos 			printf("not ARP reply\n");
246eb50acd9Sthorpej #endif
2471c038e68Sisaki 		return -1;
248eb50acd9Sthorpej 	}
249eb50acd9Sthorpej 
2506f2a9404Sgwr 	/* Is the reply from the source we want? */
251f4655308Sjunyoung 	if (memcmp(&arp_list[arp_num].addr,
2526f2a9404Sgwr 			 ah->arp_spa, sizeof(ah->arp_spa)))
2536f2a9404Sgwr 	{
254e03842d0Sbrezak #ifdef ARP_DEBUG
255e03842d0Sbrezak 		if (debug)
256e44c1d1fSchristos 			printf("unwanted address\n");
257e03842d0Sbrezak #endif
2581c038e68Sisaki 		return -1;
259e03842d0Sbrezak 	}
2606f2a9404Sgwr 	/* We don't care who the reply was sent to. */
261e03842d0Sbrezak 
2626f2a9404Sgwr 	/* We have our answer. */
2630e4efb30Sgwr #ifdef ARP_DEBUG
2640e4efb30Sgwr  	if (debug)
265e44c1d1fSchristos 		printf("got it\n");
2660e4efb30Sgwr #endif
2671c038e68Sisaki 	return n;
268e03842d0Sbrezak }
269eb50acd9Sthorpej 
270eb50acd9Sthorpej /*
271eb50acd9Sthorpej  * Convert an ARP request into a reply and send it.
27200814349Sgwr  * Notes:  Re-uses buffer.  Pad to length = 46.
273eb50acd9Sthorpej  */
274eb50acd9Sthorpej void
arp_reply(struct iodesc * d,void * pkt)2751c038e68Sisaki arp_reply(struct iodesc *d, void *pkt)
276eb50acd9Sthorpej {
277eb50acd9Sthorpej 	struct ether_arp *arp = pkt;
278eb50acd9Sthorpej 
279eb50acd9Sthorpej 	if (arp->arp_hrd != htons(ARPHRD_ETHER) ||
280eb50acd9Sthorpej 	    arp->arp_pro != htons(ETHERTYPE_IP) ||
281eb50acd9Sthorpej 	    arp->arp_hln != sizeof(arp->arp_sha) ||
282eb50acd9Sthorpej 	    arp->arp_pln != sizeof(arp->arp_spa) )
283eb50acd9Sthorpej 	{
284eb50acd9Sthorpej #ifdef ARP_DEBUG
285eb50acd9Sthorpej 		if (debug)
286*10497fd2Schristos 			printf("%s: bad hrd/pro/hln/pln\n", __func__);
287eb50acd9Sthorpej #endif
288eb50acd9Sthorpej 		return;
289eb50acd9Sthorpej 	}
290eb50acd9Sthorpej 
291eb50acd9Sthorpej 	if (arp->arp_op != htons(ARPOP_REQUEST)) {
292eb50acd9Sthorpej #ifdef ARP_DEBUG
293eb50acd9Sthorpej 		if (debug)
294*10497fd2Schristos 			printf("%s: not request!\n", __func__);
295eb50acd9Sthorpej #endif
296eb50acd9Sthorpej 		return;
297eb50acd9Sthorpej 	}
298eb50acd9Sthorpej 
299eb50acd9Sthorpej 	/* If we are not the target, ignore the request. */
300f4655308Sjunyoung 	if (memcmp(arp->arp_tpa, &d->myip, sizeof(arp->arp_tpa)))
301eb50acd9Sthorpej 		return;
302eb50acd9Sthorpej 
303eb50acd9Sthorpej #ifdef ARP_DEBUG
304eb50acd9Sthorpej 	if (debug) {
305*10497fd2Schristos 		printf("%s: to %s\n", __func__, ether_sprintf(arp->arp_sha));
306eb50acd9Sthorpej 	}
307eb50acd9Sthorpej #endif
308eb50acd9Sthorpej 
309eb50acd9Sthorpej 	arp->arp_op = htons(ARPOP_REPLY);
310eb50acd9Sthorpej 	/* source becomes target */
311e1b834bdSchristos 	(void)memcpy(arp->arp_tha, arp->arp_sha, sizeof(arp->arp_tha));
312e1b834bdSchristos 	(void)memcpy(arp->arp_tpa, arp->arp_spa, sizeof(arp->arp_tpa));
313eb50acd9Sthorpej 	/* here becomes source */
314e1b834bdSchristos 	(void)memcpy(arp->arp_sha, d->myea, sizeof(arp->arp_sha));
315e1b834bdSchristos 	(void)memcpy(arp->arp_spa, &d->myip, sizeof(arp->arp_spa));
316eb50acd9Sthorpej 
317eb50acd9Sthorpej 	/*
318eb50acd9Sthorpej 	 * No need to get fancy here.  If the send fails, the
319eb50acd9Sthorpej 	 * requestor will just ask again.
320eb50acd9Sthorpej 	 */
32100814349Sgwr 	(void) sendether(d, pkt, sizeof(*arp) + 18,
32200814349Sgwr 	                 arp->arp_tha, ETHERTYPE_ARP);
323eb50acd9Sthorpej }
324