arp.c revision 1.3 1 /* $NetBSD: arp.c,v 1.3 1994/10/26 05:44:36 cgd 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 <netinet/in.h>
46
47 #include <netinet/if_ether.h>
48 #include <netinet/in_systm.h>
49
50 #include <string.h>
51
52 #include "stand.h"
53 #include "net.h"
54
55 /* Cache stuff */
56 #define ARP_NUM 8 /* need at most 3 arp entries */
57
58 static struct arp_list {
59 n_long addr;
60 u_char ea[6];
61 } arp_list[ARP_NUM] = {
62 { INADDR_BROADCAST, BA }
63 };
64 static int arp_num = 1;
65
66 /* Local forwards */
67 static int arpsend __P((struct iodesc *, void *, int));
68 static int arprecv __P((struct iodesc *, void *, int));
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 n_long addr;
75 {
76 register int i;
77 register struct ether_arp *ah;
78 register struct arp_list *al;
79 struct {
80 u_char header[ETHER_SIZE];
81 struct ether_arp warp;
82 } wbuf;
83 union {
84 u_char buffer[RECV_SIZE];
85 struct {
86 u_char header[ETHER_SIZE];
87 struct ether_arp rarp;
88 } ru;
89 } rbuf;
90
91 #ifdef ARP_DEBUG
92 if (debug)
93 printf("arpwhohas: called for %s\n", intoa(addr));
94 #endif
95 /* Try for cached answer first */
96 for (i = 0, al = arp_list; i < arp_num; ++i, ++al)
97 if (addr == al->addr)
98 return (al->ea);
99
100 /* Don't overflow cache */
101 if (arp_num > ARP_NUM - 1)
102 panic("arpwhohas: overflowed arp_list!");
103
104 #ifdef ARP_DEBUG
105 if (debug)
106 printf("arpwhohas: not cached\n");
107 #endif
108 ah = &wbuf.warp;
109 bzero(ah, sizeof(*ah));
110
111 ah->arp_hrd = htons(ARPHRD_ETHER);
112 ah->arp_pro = htons(ETHERTYPE_IP);
113 ah->arp_hln = sizeof(ah->arp_sha); /* hardware address length */
114 ah->arp_pln = sizeof(ah->arp_spa); /* protocol address length */
115 ah->arp_op = htons(ARPOP_REQUEST);
116 MACPY(d->myea, ah->arp_sha);
117 bcopy(&d->myip, ah->arp_spa, sizeof(ah->arp_spa));
118 bcopy(&addr, ah->arp_tpa, sizeof(ah->arp_tpa));
119
120 /* Store ip address in cache */
121 al->addr = addr;
122
123 (void)sendrecv(d, arpsend, ah, sizeof(*ah), arprecv,
124 ((u_char *)&rbuf.ru.rarp) - ETHER_SIZE,
125 ETHER_SIZE + sizeof(rbuf.ru.rarp));
126
127 /* Store ethernet address in cache */
128 MACPY(rbuf.ru.rarp.arp_sha, al->ea);
129 ++arp_num;
130 #ifdef ARP_DEBUG
131 if (debug)
132 printf("arpwhohas: cacheing %s --> %s\n", intoa(addr), ether_sprintf(al->ea));
133 #endif
134
135 return (al->ea);
136 }
137
138 static int
139 arpsend(d, pkt, len)
140 register struct iodesc *d;
141 register void *pkt;
142 register int len;
143 {
144 #ifdef ARP_DEBUG
145 if (debug)
146 printf("arpsend: called\n");
147 #endif
148 return (sendether(d, pkt, len, bcea, ETHERTYPE_ARP));
149 }
150
151 /* Returns 0 if this is the packet we're waiting for else -1 (and errno == 0) */
152 static int
153 arprecv(d, pkt, len)
154 register struct iodesc *d;
155 register void *pkt;
156 register int len;
157 {
158 register struct ether_header *eh;
159 register struct ether_arp *ah;
160
161 #ifdef ARP_DEBUG
162 if (debug)
163 printf("arprecv: called\n");
164 #endif
165 if (len < sizeof(*eh) + sizeof(*ah)) {
166 errno = 0;
167 return (-1);
168 }
169
170 eh = (struct ether_header *)pkt;
171
172 /* Must be to us */
173 if (bcmp(d->myea, eh->ether_dhost, 6) != 0 &&
174 bcmp(bcea, eh->ether_dhost, 6) != 0) {
175 #ifdef ARP_DEBUG
176 if (debug)
177 printf("arprecv: not ours %s\n", ether_sprintf(eh->ether_dhost));
178 #endif
179 errno = 0;
180 return (-1);
181 }
182
183 if (ntohs(eh->ether_type) != ETHERTYPE_ARP) {
184 #ifdef ARP_DEBUG
185 if (debug)
186 printf("arprecv: not arp %d\n", ntohs(eh->ether_type));
187 #endif
188 errno = 0;
189 return (-1);
190 }
191
192 ah = (struct ether_arp *)(eh + 1);
193 HTONS(ah->arp_hrd);
194 HTONS(ah->arp_pro);
195 HTONS(ah->arp_op);
196 if (ah->arp_hrd != ARPHRD_ETHER || ah->arp_pro != ETHERTYPE_IP ||
197 ah->arp_hln != sizeof(ah->arp_sha) ||
198 ah->arp_pln != sizeof(ah->arp_spa) || ah->arp_op != ARPOP_REPLY) {
199 #ifdef ARP_DEBUG
200 if (debug)
201 printf("arprecv: not valid reply\n");
202 #endif
203 errno = 0;
204 return (-1);
205 }
206 if (bcmp(&arp_list[arp_num].addr, ah->arp_spa, sizeof(long)) != 0 ||
207 bcmp(&d->myip, ah->arp_tpa, sizeof(d->myip)) != 0) {
208 #ifdef ARP_DEBUG
209 if (debug)
210 printf("arprecv: already cached??\n");
211 #endif
212 errno = 0;
213 return (-1);
214 }
215
216 return (0);
217 }
218