1 /* $NetBSD: arp.c,v 1.18 1997/07/07 15:52:49 drochner Exp $ */
4 * Copyright (c) 1992 Regents of the University of California.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
39 * @(#) Header: arp.c,v 1.5 93/07/15 05:52:26 leres Exp (LBL)
40 * $DragonFly: src/lib/libstand/arp.c,v 1.4 2005/12/11 02:27:26 swildner Exp $
43 #include <sys/param.h>
44 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <netinet/if_ether.h>
49 #include <netinet/in_systm.h>
57 #define ARP_NUM 8 /* need at most 3 arp entries */
62 } arp_list[ARP_NUM] = {
63 /* XXX - net order `INADDR_BROADCAST' must be a constant */
69 static ssize_t arpsend(struct iodesc *, void *, size_t);
70 static ssize_t arprecv(struct iodesc *, void *, size_t, time_t);
72 /* Broadcast an ARP packet, asking who has addr on interface d */
74 arpwhohas(struct iodesc *d, struct in_addr addr)
80 struct ether_header eh;
83 u_char pad[18]; /* 60 - sizeof(...) */
87 struct ether_header eh;
90 u_char pad[24]; /* extra space */
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)
99 /* Don't overflow cache */
100 if (arp_num > ARP_NUM - 1) {
101 arp_num = 1; /* recycle */
102 printf("arpwhohas: overflowed arp_list!\n");
107 printf("arpwhohas: send request for %s\n", inet_ntoa(addr));
110 bzero((char*)&wbuf.data, sizeof(wbuf.data));
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));
122 /* Store ip address in cache (incomplete entry). */
126 arpsend, &wbuf.data, sizeof(wbuf.data),
127 arprecv, &rbuf.data, sizeof(rbuf.data));
129 panic("arp: no response for %s\n",
133 /* Store ethernet address in cache */
137 printf("arp: response from %s\n",
138 ether_sprintf(rbuf.eh.ether_shost));
139 printf("arp: cacheing %s --> %s\n",
140 inet_ntoa(addr), ether_sprintf(ah->arp_sha));
143 MACPY(ah->arp_sha, al->ea);
150 arpsend(struct iodesc *d, void *pkt, size_t len)
155 printf("arpsend: called\n");
158 return (sendether(d, pkt, len, bcea, ETHERTYPE_ARP));
162 * Returns 0 if this is the packet we're waiting for
163 * else -1 (and errno == 0)
166 arprecv(struct iodesc *d, void *pkt, size_t len, time_t tleft)
169 struct ether_arp *ah;
170 u_int16_t etype; /* host order */
177 n = readether(d, pkt, len, tleft, &etype);
179 if (n == -1 || n < sizeof(struct ether_arp)) {
182 printf("bad len=%d\n", n);
187 if (etype != ETHERTYPE_ARP) {
190 printf("not arp type=%d\n", etype);
195 /* Ethernet address now checked in readether() */
197 ah = (struct ether_arp *)pkt;
198 if (ah->arp_hrd != htons(ARPHRD_ETHER) ||
199 ah->arp_pro != htons(ETHERTYPE_IP) ||
200 ah->arp_hln != sizeof(ah->arp_sha) ||
201 ah->arp_pln != sizeof(ah->arp_spa) )
205 printf("bad hrd/pro/hln/pln\n");
210 if (ah->arp_op == htons(ARPOP_REQUEST)) {
213 printf("is request\n");
219 if (ah->arp_op != htons(ARPOP_REPLY)) {
222 printf("not ARP reply\n");
227 /* Is the reply from the source we want? */
228 if (bcmp(&arp_list[arp_num].addr,
229 ah->arp_spa, sizeof(ah->arp_spa)))
233 printf("unwanted address\n");
237 /* We don't care who the reply was sent to. */
239 /* We have our answer. */
248 * Convert an ARP request into a reply and send it.
249 * Notes: Re-uses buffer. Pad to length = 46.
255 arp_reply(struct iodesc *d, void *pkt)
257 struct ether_arp *arp = pkt;
259 if (arp->arp_hrd != htons(ARPHRD_ETHER) ||
260 arp->arp_pro != htons(ETHERTYPE_IP) ||
261 arp->arp_hln != sizeof(arp->arp_sha) ||
262 arp->arp_pln != sizeof(arp->arp_spa) )
266 printf("arp_reply: bad hrd/pro/hln/pln\n");
271 if (arp->arp_op != htons(ARPOP_REQUEST)) {
274 printf("arp_reply: not request!\n");
279 /* If we are not the target, ignore the request. */
280 if (bcmp(arp->arp_tpa, &d->myip, sizeof(arp->arp_tpa)))
285 printf("arp_reply: to %s\n", ether_sprintf(arp->arp_sha));
289 arp->arp_op = htons(ARPOP_REPLY);
290 /* source becomes target */
291 bcopy(arp->arp_sha, arp->arp_tha, sizeof(arp->arp_tha));
292 bcopy(arp->arp_spa, arp->arp_tpa, sizeof(arp->arp_tpa));
293 /* here becomes source */
294 bcopy(d->myea, arp->arp_sha, sizeof(arp->arp_sha));
295 bcopy(&d->myip, arp->arp_spa, sizeof(arp->arp_spa));
298 * No need to get fancy here. If the send fails, the
299 * requestor will just ask again.
301 (void) sendether(d, pkt, sizeof(*arp) + 18,
302 arp->arp_tha, ETHERTYPE_ARP);