1 /* $NetBSD: arp.c,v 1.33 2009/01/17 14:00:36 tsutsui 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)
42 #include <sys/types.h>
43 #include <sys/socket.h>
45 #include <net/if_ether.h>
46 #include <netinet/in.h>
48 #include <netinet/in_systm.h>
51 #include <lib/libkern/libkern.h>
60 * Ethernet Address Resolution Protocol.
62 * See RFC 826 for protocol description. Structure below is adapted
63 * to resolving internet addresses. Field names used correspond to
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 */
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
80 #define ARP_NUM 8 /* need at most 3 arp entries */
85 } arp_list
[ARP_NUM
] = {
86 /* XXX - net order `INADDR_BROADCAST' must be a constant */
92 static ssize_t
arpsend(struct iodesc
*, void *, size_t);
93 static ssize_t
arprecv(struct iodesc
*, void *, size_t, saseconds_t
);
95 /* Broadcast an ARP packet, asking who has addr on interface d */
97 arpwhohas(struct iodesc
*d
, struct in_addr addr
)
100 struct ether_arp
*ah
;
103 struct ether_header eh
;
105 struct ether_arp arp
;
106 u_char pad
[18]; /* 60 - sizeof(...) */
110 struct ether_header eh
;
112 struct ether_arp arp
;
113 u_char pad
[24]; /* extra space */
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
)
122 /* Don't overflow cache */
123 if (arp_num
> ARP_NUM
- 1) {
124 arp_num
= 1; /* recycle */
125 printf("arpwhohas: overflowed arp_list!\n");
130 printf("arpwhohas: send request for %s\n", inet_ntoa(addr
));
133 (void)memset(&wbuf
.data
, 0, sizeof(wbuf
.data
));
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
));
145 /* Store ip address in cache (incomplete entry). */
149 arpsend
, &wbuf
.data
, sizeof(wbuf
.data
),
150 arprecv
, &rbuf
.data
, sizeof(rbuf
.data
));
152 panic("arp: no response for %s",
156 /* Store ethernet address in cache */
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
));
166 MACPY(ah
->arp_sha
, al
->ea
);
173 arpsend(struct iodesc
*d
, void *pkt
, size_t len
)
178 printf("arpsend: called\n");
181 return sendether(d
, pkt
, len
, bcea
, ETHERTYPE_ARP
);
185 * Returns 0 if this is the packet we're waiting for
186 * else -1 (and errno == 0)
189 arprecv(struct iodesc
*d
, void *pkt
, size_t len
, saseconds_t tleft
)
192 struct ether_arp
*ah
;
193 u_int16_t etype
; /* host order */
200 n
= readether(d
, pkt
, len
, tleft
, &etype
);
202 if (n
== -1 || (size_t)n
< sizeof(struct ether_arp
)) {
205 printf("bad len=%ld\n", (signed long) n
);
210 if (etype
!= ETHERTYPE_ARP
) {
213 printf("not arp type=%d\n", etype
);
218 /* Ethernet address now checked in readether() */
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
) )
228 printf("bad hrd/pro/hln/pln\n");
233 if (ah
->arp_op
== htons(ARPOP_REQUEST
)) {
236 printf("is request\n");
242 if (ah
->arp_op
!= htons(ARPOP_REPLY
)) {
245 printf("not ARP reply\n");
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
)))
256 printf("unwanted address\n");
260 /* We don't care who the reply was sent to. */
262 /* We have our answer. */
271 * Convert an ARP request into a reply and send it.
272 * Notes: Re-uses buffer. Pad to length = 46.
275 arp_reply(struct iodesc
*d
, void *pkt
)
277 struct ether_arp
*arp
= pkt
;
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
) )
286 printf("arp_reply: bad hrd/pro/hln/pln\n");
291 if (arp
->arp_op
!= htons(ARPOP_REQUEST
)) {
294 printf("arp_reply: not request!\n");
299 /* If we are not the target, ignore the request. */
300 if (memcmp(arp
->arp_tpa
, &d
->myip
, sizeof(arp
->arp_tpa
)))
305 printf("arp_reply: to %s\n", ether_sprintf(arp
->arp_sha
));
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
));
318 * No need to get fancy here. If the send fails, the
319 * requestor will just ask again.
321 (void) sendether(d
, pkt
, sizeof(*arp
) + 18,
322 arp
->arp_tha
, ETHERTYPE_ARP
);