if_arp.c revision 1.16 1 1.16 cgd /* $NetBSD: if_arp.c,v 1.16 1994/06/29 06:37:54 cgd Exp $ */
2 1.16 cgd
3 1.1 cgd /*
4 1.12 mycroft * Copyright (c) 1982, 1986, 1988, 1993
5 1.12 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd *
35 1.16 cgd * @(#)if_ether.c 8.1 (Berkeley) 6/10/93
36 1.1 cgd */
37 1.1 cgd
38 1.1 cgd /*
39 1.1 cgd * Ethernet address resolution protocol.
40 1.1 cgd * TODO:
41 1.1 cgd * add "inuse/lock" bit (or ref. count) along with valid bit
42 1.1 cgd */
43 1.1 cgd
44 1.15 chopps #ifdef INET
45 1.15 chopps
46 1.5 mycroft #include <sys/param.h>
47 1.5 mycroft #include <sys/systm.h>
48 1.5 mycroft #include <sys/malloc.h>
49 1.5 mycroft #include <sys/mbuf.h>
50 1.5 mycroft #include <sys/socket.h>
51 1.5 mycroft #include <sys/time.h>
52 1.5 mycroft #include <sys/kernel.h>
53 1.5 mycroft #include <sys/errno.h>
54 1.5 mycroft #include <sys/ioctl.h>
55 1.5 mycroft #include <sys/syslog.h>
56 1.1 cgd
57 1.5 mycroft #include <net/if.h>
58 1.12 mycroft #include <net/if_dl.h>
59 1.12 mycroft #include <net/route.h>
60 1.5 mycroft
61 1.5 mycroft #include <netinet/in.h>
62 1.5 mycroft #include <netinet/in_systm.h>
63 1.5 mycroft #include <netinet/in_var.h>
64 1.5 mycroft #include <netinet/ip.h>
65 1.5 mycroft #include <netinet/if_ether.h>
66 1.1 cgd
67 1.12 mycroft #define SIN(s) ((struct sockaddr_in *)s)
68 1.12 mycroft #define SDL(s) ((struct sockaddr_dl *)s)
69 1.12 mycroft #define SRP(s) ((struct sockaddr_inarp *)s)
70 1.1 cgd
71 1.1 cgd /*
72 1.1 cgd * ARP trailer negotiation. Trailer protocol is not IP specific,
73 1.1 cgd * but ARP request/response use IP addresses.
74 1.1 cgd */
75 1.12 mycroft #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
76 1.1 cgd
77 1.1 cgd /* timer values */
78 1.12 mycroft int arpt_prune = (5*60*1); /* walk list every 5 minutes */
79 1.12 mycroft int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
80 1.12 mycroft int arpt_down = 20; /* once declared down, don't send for 20 secs */
81 1.12 mycroft #define rt_expire rt_rmx.rmx_expire
82 1.12 mycroft
83 1.12 mycroft static void arprequest __P((struct arpcom *, u_long *, u_long *, u_char *));
84 1.12 mycroft static void arptfree __P((struct llinfo_arp *));
85 1.12 mycroft static void arptimer __P((void *));
86 1.12 mycroft static struct llinfo_arp *arplookup __P((u_long, int, int));
87 1.12 mycroft static void in_arpinput __P((struct mbuf *));
88 1.12 mycroft
89 1.12 mycroft extern struct ifnet loif;
90 1.12 mycroft extern struct timeval time;
91 1.12 mycroft struct llinfo_arp llinfo_arp = {&llinfo_arp, &llinfo_arp};
92 1.12 mycroft struct ifqueue arpintrq = {0, 0, 0, 50};
93 1.12 mycroft int arp_inuse, arp_allocated, arp_intimer;
94 1.12 mycroft int arp_maxtries = 5;
95 1.12 mycroft int useloopback = 1; /* use loopback interface for local traffic */
96 1.12 mycroft int arpinit_done = 0;
97 1.9 glass
98 1.12 mycroft /* revarp state */
99 1.13 gwr static struct in_addr myip, srv_ip;
100 1.13 gwr static int myip_initialized = 0;
101 1.13 gwr static int revarp_in_progress = 0;
102 1.13 gwr static struct ifnet *myip_ifp = NULL;
103 1.1 cgd
104 1.1 cgd /*
105 1.12 mycroft * Timeout routine. Age arp_tab entries periodically.
106 1.1 cgd */
107 1.3 andrew /* ARGSUSED */
108 1.12 mycroft static void
109 1.11 cgd arptimer(arg)
110 1.11 cgd void *arg;
111 1.1 cgd {
112 1.12 mycroft int s = splnet();
113 1.12 mycroft register struct llinfo_arp *la = llinfo_arp.la_next;
114 1.1 cgd
115 1.12 mycroft timeout(arptimer, NULL, arpt_prune * hz);
116 1.12 mycroft while (la != &llinfo_arp) {
117 1.12 mycroft register struct rtentry *rt = la->la_rt;
118 1.12 mycroft la = la->la_next;
119 1.12 mycroft if (rt->rt_expire && rt->rt_expire <= time.tv_sec)
120 1.12 mycroft arptfree(la->la_prev); /* timer has expired; clear */
121 1.12 mycroft }
122 1.12 mycroft splx(s);
123 1.12 mycroft }
124 1.12 mycroft
125 1.12 mycroft /*
126 1.12 mycroft * Parallel to llc_rtrequest.
127 1.12 mycroft */
128 1.12 mycroft void
129 1.12 mycroft arp_rtrequest(req, rt, sa)
130 1.12 mycroft int req;
131 1.12 mycroft register struct rtentry *rt;
132 1.12 mycroft struct sockaddr *sa;
133 1.12 mycroft {
134 1.12 mycroft register struct sockaddr *gate = rt->rt_gateway;
135 1.12 mycroft register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
136 1.12 mycroft static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
137 1.12 mycroft
138 1.12 mycroft if (!arpinit_done) {
139 1.12 mycroft arpinit_done = 1;
140 1.13 gwr /*
141 1.13 gwr * We generate expiration times from time.tv_sec
142 1.13 gwr * so avoid accidently creating permanent routes.
143 1.13 gwr */
144 1.13 gwr if (time.tv_sec == 0) {
145 1.13 gwr time.tv_sec++;
146 1.13 gwr }
147 1.12 mycroft timeout(arptimer, (caddr_t)0, hz);
148 1.12 mycroft }
149 1.12 mycroft if (rt->rt_flags & RTF_GATEWAY)
150 1.12 mycroft return;
151 1.12 mycroft switch (req) {
152 1.12 mycroft
153 1.12 mycroft case RTM_ADD:
154 1.12 mycroft /*
155 1.12 mycroft * XXX: If this is a manually added route to interface
156 1.12 mycroft * such as older version of routed or gated might provide,
157 1.12 mycroft * restore cloning bit.
158 1.12 mycroft */
159 1.12 mycroft if ((rt->rt_flags & RTF_HOST) == 0 &&
160 1.12 mycroft SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
161 1.12 mycroft rt->rt_flags |= RTF_CLONING;
162 1.12 mycroft if (rt->rt_flags & RTF_CLONING) {
163 1.12 mycroft /*
164 1.12 mycroft * Case 1: This route should come from a route to iface.
165 1.12 mycroft */
166 1.12 mycroft rt_setgate(rt, rt_key(rt),
167 1.12 mycroft (struct sockaddr *)&null_sdl);
168 1.12 mycroft gate = rt->rt_gateway;
169 1.12 mycroft SDL(gate)->sdl_type = rt->rt_ifp->if_type;
170 1.12 mycroft SDL(gate)->sdl_index = rt->rt_ifp->if_index;
171 1.13 gwr /*
172 1.14 gwr * Give this route an expiration time, even though
173 1.14 gwr * it's a "permanent" route, so that routes cloned
174 1.14 gwr * from it do not need their expiration time set.
175 1.13 gwr */
176 1.14 gwr rt->rt_expire = time.tv_sec;
177 1.12 mycroft break;
178 1.12 mycroft }
179 1.12 mycroft /* Announce a new entry if requested. */
180 1.12 mycroft if (rt->rt_flags & RTF_ANNOUNCE)
181 1.12 mycroft arprequest((struct arpcom *)rt->rt_ifp,
182 1.12 mycroft &SIN(rt_key(rt))->sin_addr.s_addr,
183 1.12 mycroft &SIN(rt_key(rt))->sin_addr.s_addr,
184 1.12 mycroft (u_char *)LLADDR(SDL(gate)));
185 1.12 mycroft /*FALLTHROUGH*/
186 1.12 mycroft case RTM_RESOLVE:
187 1.12 mycroft if (gate->sa_family != AF_LINK ||
188 1.12 mycroft gate->sa_len < sizeof(null_sdl)) {
189 1.12 mycroft log(LOG_DEBUG, "arp_rtrequest: bad gateway value");
190 1.12 mycroft break;
191 1.12 mycroft }
192 1.12 mycroft SDL(gate)->sdl_type = rt->rt_ifp->if_type;
193 1.12 mycroft SDL(gate)->sdl_index = rt->rt_ifp->if_index;
194 1.12 mycroft if (la != 0)
195 1.12 mycroft break; /* This happens on a route change */
196 1.12 mycroft /*
197 1.12 mycroft * Case 2: This route may come from cloning, or a manual route
198 1.12 mycroft * add with a LL address.
199 1.12 mycroft */
200 1.12 mycroft R_Malloc(la, struct llinfo_arp *, sizeof(*la));
201 1.12 mycroft rt->rt_llinfo = (caddr_t)la;
202 1.12 mycroft if (la == 0) {
203 1.12 mycroft log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
204 1.12 mycroft break;
205 1.12 mycroft }
206 1.12 mycroft arp_inuse++, arp_allocated++;
207 1.12 mycroft Bzero(la, sizeof(*la));
208 1.12 mycroft la->la_rt = rt;
209 1.12 mycroft rt->rt_flags |= RTF_LLINFO;
210 1.12 mycroft insque(la, &llinfo_arp);
211 1.12 mycroft if (SIN(rt_key(rt))->sin_addr.s_addr ==
212 1.12 mycroft (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
213 1.12 mycroft /*
214 1.12 mycroft * This test used to be
215 1.12 mycroft * if (loif.if_flags & IFF_UP)
216 1.12 mycroft * It allowed local traffic to be forced
217 1.12 mycroft * through the hardware by configuring the loopback down.
218 1.12 mycroft * However, it causes problems during network configuration
219 1.12 mycroft * for boards that can't receive packets they send.
220 1.12 mycroft * It is now necessary to clear "useloopback" and remove
221 1.12 mycroft * the route to force traffic out to the hardware.
222 1.12 mycroft */
223 1.12 mycroft rt->rt_expire = 0;
224 1.12 mycroft Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
225 1.12 mycroft LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
226 1.12 mycroft if (useloopback)
227 1.12 mycroft rt->rt_ifp = &loif;
228 1.12 mycroft
229 1.12 mycroft }
230 1.12 mycroft break;
231 1.12 mycroft
232 1.12 mycroft case RTM_DELETE:
233 1.12 mycroft if (la == 0)
234 1.12 mycroft break;
235 1.12 mycroft arp_inuse--;
236 1.12 mycroft remque(la);
237 1.12 mycroft rt->rt_llinfo = 0;
238 1.12 mycroft rt->rt_flags &= ~RTF_LLINFO;
239 1.12 mycroft if (la->la_hold)
240 1.12 mycroft m_freem(la->la_hold);
241 1.12 mycroft Free((caddr_t)la);
242 1.1 cgd }
243 1.1 cgd }
244 1.1 cgd
245 1.1 cgd /*
246 1.1 cgd * Broadcast an ARP packet, asking who has addr on interface ac.
247 1.1 cgd */
248 1.12 mycroft void
249 1.1 cgd arpwhohas(ac, addr)
250 1.1 cgd register struct arpcom *ac;
251 1.12 mycroft register struct in_addr *addr;
252 1.12 mycroft {
253 1.12 mycroft arprequest(ac, &ac->ac_ipaddr.s_addr, &addr->s_addr, ac->ac_enaddr);
254 1.12 mycroft }
255 1.12 mycroft
256 1.12 mycroft /*
257 1.12 mycroft * Broadcast an ARP request. Caller specifies:
258 1.12 mycroft * - arp header source ip address
259 1.12 mycroft * - arp header target ip address
260 1.12 mycroft * - arp header source ethernet address
261 1.12 mycroft */
262 1.12 mycroft static void
263 1.12 mycroft arprequest(ac, sip, tip, enaddr)
264 1.12 mycroft register struct arpcom *ac;
265 1.12 mycroft register u_long *sip, *tip;
266 1.12 mycroft register u_char *enaddr;
267 1.1 cgd {
268 1.1 cgd register struct mbuf *m;
269 1.1 cgd register struct ether_header *eh;
270 1.1 cgd register struct ether_arp *ea;
271 1.1 cgd struct sockaddr sa;
272 1.1 cgd
273 1.1 cgd if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
274 1.1 cgd return;
275 1.1 cgd m->m_len = sizeof(*ea);
276 1.1 cgd m->m_pkthdr.len = sizeof(*ea);
277 1.1 cgd MH_ALIGN(m, sizeof(*ea));
278 1.1 cgd ea = mtod(m, struct ether_arp *);
279 1.1 cgd eh = (struct ether_header *)sa.sa_data;
280 1.1 cgd bzero((caddr_t)ea, sizeof (*ea));
281 1.1 cgd bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
282 1.1 cgd sizeof(eh->ether_dhost));
283 1.7 deraadt eh->ether_type = htons(ETHERTYPE_ARP);
284 1.1 cgd ea->arp_hrd = htons(ARPHRD_ETHER);
285 1.1 cgd ea->arp_pro = htons(ETHERTYPE_IP);
286 1.1 cgd ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */
287 1.1 cgd ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */
288 1.1 cgd ea->arp_op = htons(ARPOP_REQUEST);
289 1.12 mycroft bcopy((caddr_t)enaddr, (caddr_t)ea->arp_sha, sizeof(ea->arp_sha));
290 1.12 mycroft bcopy((caddr_t)sip, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa));
291 1.12 mycroft bcopy((caddr_t)tip, (caddr_t)ea->arp_tpa, sizeof(ea->arp_tpa));
292 1.1 cgd sa.sa_family = AF_UNSPEC;
293 1.1 cgd sa.sa_len = sizeof(sa);
294 1.1 cgd (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
295 1.1 cgd }
296 1.1 cgd
297 1.1 cgd /*
298 1.6 mycroft * Resolve an IP address into an ethernet address. If success,
299 1.1 cgd * desten is filled in. If there is no entry in arptab,
300 1.1 cgd * set one up and broadcast a request for the IP address.
301 1.1 cgd * Hold onto this mbuf and resend it once the address
302 1.1 cgd * is finally resolved. A return value of 1 indicates
303 1.1 cgd * that desten has been filled in and the packet should be sent
304 1.1 cgd * normally; a 0 return indicates that the packet has been
305 1.1 cgd * taken over here, either now or for later transmission.
306 1.1 cgd */
307 1.12 mycroft int
308 1.12 mycroft arpresolve(ac, rt, m, dst, desten)
309 1.1 cgd register struct arpcom *ac;
310 1.12 mycroft register struct rtentry *rt;
311 1.1 cgd struct mbuf *m;
312 1.12 mycroft register struct sockaddr *dst;
313 1.1 cgd register u_char *desten;
314 1.1 cgd {
315 1.12 mycroft register struct llinfo_arp *la;
316 1.12 mycroft struct sockaddr_dl *sdl;
317 1.1 cgd
318 1.1 cgd if (m->m_flags & M_BCAST) { /* broadcast */
319 1.1 cgd bcopy((caddr_t)etherbroadcastaddr, (caddr_t)desten,
320 1.1 cgd sizeof(etherbroadcastaddr));
321 1.1 cgd return (1);
322 1.1 cgd }
323 1.4 hpeyerl if (m->m_flags & M_MCAST) { /* multicast */
324 1.12 mycroft ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
325 1.4 hpeyerl return(1);
326 1.4 hpeyerl }
327 1.12 mycroft if (rt)
328 1.12 mycroft la = (struct llinfo_arp *)rt->rt_llinfo;
329 1.12 mycroft else {
330 1.12 mycroft if (la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0))
331 1.12 mycroft rt = la->la_rt;
332 1.12 mycroft }
333 1.12 mycroft if (la == 0 || rt == 0) {
334 1.12 mycroft log(LOG_DEBUG, "arpresolve: can't allocate llinfo");
335 1.12 mycroft m_freem(m);
336 1.12 mycroft return (0);
337 1.1 cgd }
338 1.12 mycroft sdl = SDL(rt->rt_gateway);
339 1.12 mycroft /*
340 1.12 mycroft * Check the address family and length is valid, the address
341 1.12 mycroft * is resolved; otherwise, try to resolve.
342 1.12 mycroft */
343 1.12 mycroft if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) &&
344 1.12 mycroft sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
345 1.12 mycroft bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
346 1.12 mycroft return 1;
347 1.1 cgd }
348 1.1 cgd /*
349 1.1 cgd * There is an arptab entry, but no ethernet address
350 1.1 cgd * response yet. Replace the held mbuf with this
351 1.1 cgd * latest one.
352 1.1 cgd */
353 1.12 mycroft if (la->la_hold)
354 1.12 mycroft m_freem(la->la_hold);
355 1.14 gwr la->la_hold = m;
356 1.13 gwr /*
357 1.13 gwr * Re-send the ARP request when appropriate.
358 1.13 gwr */
359 1.13 gwr #ifdef DIAGNOSTIC
360 1.13 gwr if (rt->rt_expire == 0) {
361 1.14 gwr /* This should never happen. (Should it? -gwr) */
362 1.13 gwr printf("arpresolve: unresolved and rt_expire == 0\n");
363 1.13 gwr /* Set expiration time to now (expired). */
364 1.13 gwr rt->rt_expire = time.tv_sec;
365 1.13 gwr }
366 1.13 gwr #endif
367 1.14 gwr if (rt->rt_expire) {
368 1.14 gwr rt->rt_flags &= ~RTF_REJECT;
369 1.14 gwr if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) {
370 1.14 gwr rt->rt_expire = time.tv_sec;
371 1.14 gwr if (la->la_asked++ < arp_maxtries)
372 1.14 gwr arpwhohas(ac, &(SIN(dst)->sin_addr));
373 1.14 gwr else {
374 1.14 gwr rt->rt_flags |= RTF_REJECT;
375 1.14 gwr rt->rt_expire += arpt_down;
376 1.14 gwr la->la_asked = 0;
377 1.14 gwr }
378 1.14 gwr }
379 1.12 mycroft }
380 1.1 cgd return (0);
381 1.10 mycroft }
382 1.10 mycroft
383 1.1 cgd /*
384 1.12 mycroft * Common length and type checks are done here,
385 1.1 cgd * then the protocol-specific routine is called.
386 1.1 cgd */
387 1.12 mycroft void
388 1.12 mycroft arpintr()
389 1.1 cgd {
390 1.12 mycroft register struct mbuf *m;
391 1.1 cgd register struct arphdr *ar;
392 1.12 mycroft int s;
393 1.1 cgd
394 1.12 mycroft while (arpintrq.ifq_head) {
395 1.12 mycroft s = splimp();
396 1.12 mycroft IF_DEQUEUE(&arpintrq, m);
397 1.12 mycroft splx(s);
398 1.12 mycroft if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
399 1.12 mycroft panic("arpintr");
400 1.12 mycroft if (m->m_len >= sizeof(struct arphdr) &&
401 1.12 mycroft (ar = mtod(m, struct arphdr *)) &&
402 1.12 mycroft ntohs(ar->ar_hrd) == ARPHRD_ETHER &&
403 1.12 mycroft m->m_len >=
404 1.12 mycroft sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
405 1.12 mycroft switch (ntohs(ar->ar_pro)) {
406 1.12 mycroft
407 1.12 mycroft case ETHERTYPE_IP:
408 1.12 mycroft case ETHERTYPE_IPTRAILERS:
409 1.12 mycroft in_arpinput(m);
410 1.12 mycroft continue;
411 1.12 mycroft }
412 1.12 mycroft m_freem(m);
413 1.1 cgd }
414 1.1 cgd }
415 1.1 cgd
416 1.1 cgd /*
417 1.1 cgd * ARP for Internet protocols on 10 Mb/s Ethernet.
418 1.1 cgd * Algorithm is that given in RFC 826.
419 1.1 cgd * In addition, a sanity check is performed on the sender
420 1.1 cgd * protocol address, to catch impersonators.
421 1.12 mycroft * We no longer handle negotiations for use of trailer protocol:
422 1.12 mycroft * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
423 1.12 mycroft * along with IP replies if we wanted trailers sent to us,
424 1.12 mycroft * and also sent them in response to IP replies.
425 1.12 mycroft * This allowed either end to announce the desire to receive
426 1.1 cgd * trailer packets.
427 1.12 mycroft * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
428 1.12 mycroft * but formerly didn't normally send requests.
429 1.1 cgd */
430 1.12 mycroft static void
431 1.12 mycroft in_arpinput(m)
432 1.1 cgd struct mbuf *m;
433 1.1 cgd {
434 1.1 cgd register struct ether_arp *ea;
435 1.12 mycroft register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
436 1.1 cgd struct ether_header *eh;
437 1.12 mycroft register struct llinfo_arp *la = 0;
438 1.12 mycroft register struct rtentry *rt;
439 1.12 mycroft struct in_ifaddr *ia, *maybe_ia = 0;
440 1.12 mycroft struct sockaddr_dl *sdl;
441 1.1 cgd struct sockaddr sa;
442 1.1 cgd struct in_addr isaddr, itaddr, myaddr;
443 1.12 mycroft int op;
444 1.1 cgd
445 1.1 cgd ea = mtod(m, struct ether_arp *);
446 1.1 cgd op = ntohs(ea->arp_op);
447 1.1 cgd bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof (isaddr));
448 1.1 cgd bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof (itaddr));
449 1.1 cgd for (ia = in_ifaddr; ia; ia = ia->ia_next)
450 1.1 cgd if (ia->ia_ifp == &ac->ac_if) {
451 1.1 cgd maybe_ia = ia;
452 1.1 cgd if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
453 1.1 cgd (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
454 1.1 cgd break;
455 1.1 cgd }
456 1.1 cgd if (maybe_ia == 0)
457 1.1 cgd goto out;
458 1.1 cgd myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
459 1.1 cgd if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
460 1.1 cgd sizeof (ea->arp_sha)))
461 1.1 cgd goto out; /* it's from me, ignore it. */
462 1.1 cgd if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
463 1.1 cgd sizeof (ea->arp_sha))) {
464 1.1 cgd log(LOG_ERR,
465 1.1 cgd "arp: ether address is broadcast for IP address %x!\n",
466 1.1 cgd ntohl(isaddr.s_addr));
467 1.1 cgd goto out;
468 1.1 cgd }
469 1.1 cgd if (isaddr.s_addr == myaddr.s_addr) {
470 1.1 cgd log(LOG_ERR,
471 1.1 cgd "duplicate IP address %x!! sent from ethernet address: %s\n",
472 1.1 cgd ntohl(isaddr.s_addr), ether_sprintf(ea->arp_sha));
473 1.1 cgd itaddr = myaddr;
474 1.12 mycroft goto reply;
475 1.1 cgd }
476 1.12 mycroft la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
477 1.12 mycroft if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
478 1.12 mycroft if (sdl->sdl_alen &&
479 1.12 mycroft bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen))
480 1.12 mycroft log(LOG_INFO, "arp info overwritten for %x by %s\n",
481 1.12 mycroft isaddr.s_addr, ether_sprintf(ea->arp_sha));
482 1.12 mycroft bcopy((caddr_t)ea->arp_sha, LLADDR(sdl),
483 1.12 mycroft sdl->sdl_alen = sizeof(ea->arp_sha));
484 1.12 mycroft if (rt->rt_expire)
485 1.12 mycroft rt->rt_expire = time.tv_sec + arpt_keep;
486 1.12 mycroft rt->rt_flags &= ~RTF_REJECT;
487 1.12 mycroft la->la_asked = 0;
488 1.12 mycroft if (la->la_hold) {
489 1.12 mycroft (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
490 1.12 mycroft rt_key(rt), rt);
491 1.12 mycroft la->la_hold = 0;
492 1.1 cgd }
493 1.1 cgd }
494 1.1 cgd reply:
495 1.12 mycroft if (op != ARPOP_REQUEST) {
496 1.12 mycroft out:
497 1.12 mycroft m_freem(m);
498 1.12 mycroft return;
499 1.1 cgd }
500 1.1 cgd if (itaddr.s_addr == myaddr.s_addr) {
501 1.1 cgd /* I am the target */
502 1.1 cgd bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha,
503 1.1 cgd sizeof(ea->arp_sha));
504 1.1 cgd bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
505 1.1 cgd sizeof(ea->arp_sha));
506 1.1 cgd } else {
507 1.12 mycroft la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
508 1.12 mycroft if (la == 0)
509 1.1 cgd goto out;
510 1.12 mycroft rt = la->la_rt;
511 1.1 cgd bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha,
512 1.1 cgd sizeof(ea->arp_sha));
513 1.12 mycroft sdl = SDL(rt->rt_gateway);
514 1.12 mycroft bcopy(LLADDR(sdl), (caddr_t)ea->arp_sha, sizeof(ea->arp_sha));
515 1.1 cgd }
516 1.1 cgd
517 1.12 mycroft bcopy((caddr_t)ea->arp_spa, (caddr_t)ea->arp_tpa, sizeof(ea->arp_spa));
518 1.12 mycroft bcopy((caddr_t)&itaddr, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa));
519 1.6 mycroft ea->arp_op = htons(ARPOP_REPLY);
520 1.12 mycroft ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
521 1.1 cgd eh = (struct ether_header *)sa.sa_data;
522 1.1 cgd bcopy((caddr_t)ea->arp_tha, (caddr_t)eh->ether_dhost,
523 1.1 cgd sizeof(eh->ether_dhost));
524 1.7 deraadt eh->ether_type = htons(ETHERTYPE_ARP);
525 1.1 cgd sa.sa_family = AF_UNSPEC;
526 1.1 cgd sa.sa_len = sizeof(sa);
527 1.1 cgd (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
528 1.1 cgd return;
529 1.1 cgd }
530 1.1 cgd
531 1.1 cgd /*
532 1.12 mycroft * Free an arp entry.
533 1.1 cgd */
534 1.12 mycroft static void
535 1.12 mycroft arptfree(la)
536 1.12 mycroft register struct llinfo_arp *la;
537 1.12 mycroft {
538 1.12 mycroft register struct rtentry *rt = la->la_rt;
539 1.12 mycroft register struct sockaddr_dl *sdl;
540 1.12 mycroft
541 1.12 mycroft if (rt == 0)
542 1.12 mycroft panic("arptfree");
543 1.12 mycroft if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
544 1.12 mycroft sdl->sdl_family == AF_LINK) {
545 1.12 mycroft sdl->sdl_alen = 0;
546 1.12 mycroft la->la_asked = 0;
547 1.12 mycroft rt->rt_flags &= ~RTF_REJECT;
548 1.12 mycroft return;
549 1.12 mycroft }
550 1.12 mycroft rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
551 1.12 mycroft 0, (struct rtentry **)0);
552 1.1 cgd }
553 1.1 cgd
554 1.1 cgd /*
555 1.12 mycroft * Lookup or enter a new address in arptab.
556 1.12 mycroft */
557 1.12 mycroft static struct llinfo_arp *
558 1.12 mycroft arplookup(addr, create, proxy)
559 1.12 mycroft u_long addr;
560 1.12 mycroft int create, proxy;
561 1.12 mycroft {
562 1.12 mycroft register struct rtentry *rt;
563 1.12 mycroft static struct sockaddr_inarp sin;
564 1.1 cgd
565 1.12 mycroft sin.sin_len = sizeof(sin);
566 1.12 mycroft sin.sin_family = AF_INET;
567 1.12 mycroft sin.sin_addr.s_addr = addr;
568 1.12 mycroft sin.sin_other = proxy ? SIN_PROXY : 0;
569 1.12 mycroft rt = rtalloc1((struct sockaddr *)&sin, create);
570 1.12 mycroft if (rt == 0)
571 1.12 mycroft return (0);
572 1.12 mycroft rt->rt_refcnt--;
573 1.12 mycroft if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
574 1.12 mycroft rt->rt_gateway->sa_family != AF_LINK) {
575 1.12 mycroft if (create)
576 1.12 mycroft log(LOG_DEBUG, "arptnew failed on %x\n", ntohl(addr));
577 1.12 mycroft return (0);
578 1.1 cgd }
579 1.12 mycroft return ((struct llinfo_arp *)rt->rt_llinfo);
580 1.1 cgd }
581 1.1 cgd
582 1.12 mycroft int
583 1.1 cgd arpioctl(cmd, data)
584 1.1 cgd int cmd;
585 1.1 cgd caddr_t data;
586 1.1 cgd {
587 1.12 mycroft return (EOPNOTSUPP);
588 1.9 glass }
589 1.9 glass
590 1.9 glass /*
591 1.9 glass * Called from 10 Mb/s Ethernet interrupt handlers
592 1.9 glass * when ether packet type ETHERTYPE_REVARP
593 1.9 glass * is received. Common length and type checks are done here,
594 1.9 glass * then the protocol-specific routine is called.
595 1.9 glass */
596 1.12 mycroft void
597 1.12 mycroft revarpinput(m)
598 1.9 glass struct mbuf *m;
599 1.9 glass {
600 1.9 glass struct arphdr *ar;
601 1.9 glass int op, s;
602 1.9 glass
603 1.9 glass if (m->m_len < sizeof(struct arphdr))
604 1.9 glass goto out;
605 1.9 glass ar = mtod(m, struct arphdr *);
606 1.9 glass if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
607 1.9 glass goto out;
608 1.12 mycroft if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
609 1.9 glass goto out;
610 1.9 glass switch (ntohs(ar->ar_pro)) {
611 1.9 glass
612 1.9 glass case ETHERTYPE_IP:
613 1.9 glass case ETHERTYPE_IPTRAILERS:
614 1.12 mycroft in_revarpinput(m);
615 1.9 glass return;
616 1.9 glass
617 1.9 glass default:
618 1.9 glass break;
619 1.9 glass }
620 1.9 glass out:
621 1.9 glass m_freem(m);
622 1.9 glass }
623 1.9 glass
624 1.9 glass /*
625 1.9 glass * RARP for Internet protocols on 10 Mb/s Ethernet.
626 1.9 glass * Algorithm is that given in RFC 903.
627 1.9 glass * We are only using for bootstrap purposes to get an ip address for one of
628 1.9 glass * our interfaces. Thus we support no user-interface.
629 1.9 glass *
630 1.9 glass * Since the contents of the RARP reply are specific to the interface that
631 1.9 glass * sent the request, this code must ensure that they are properly associated.
632 1.9 glass *
633 1.9 glass * Note: also supports ARP via RARP packets, per the RFC.
634 1.9 glass */
635 1.12 mycroft in_revarpinput(m)
636 1.9 glass struct mbuf *m;
637 1.9 glass {
638 1.12 mycroft struct ifnet *ifp;
639 1.9 glass struct ether_arp *ar;
640 1.9 glass int op, s;
641 1.9 glass
642 1.9 glass ar = mtod(m, struct ether_arp *);
643 1.9 glass op = ntohs(ar->arp_op);
644 1.9 glass switch (op) {
645 1.9 glass case ARPOP_REQUEST:
646 1.9 glass case ARPOP_REPLY: /* per RFC */
647 1.12 mycroft in_arpinput(m);
648 1.9 glass return;
649 1.12 mycroft case ARPOP_REVREPLY:
650 1.9 glass break;
651 1.12 mycroft case ARPOP_REVREQUEST: /* handled by rarpd(8) */
652 1.9 glass default:
653 1.9 glass goto out;
654 1.9 glass }
655 1.9 glass if (!revarp_in_progress)
656 1.9 glass goto out;
657 1.12 mycroft ifp = m->m_pkthdr.rcvif;
658 1.12 mycroft if (ifp != myip_ifp) /* !same interface */
659 1.9 glass goto out;
660 1.13 gwr if (myip_initialized)
661 1.13 gwr goto wake;
662 1.12 mycroft if (bcmp(ar->arp_tha, ((struct arpcom *)ifp)->ac_enaddr,
663 1.12 mycroft sizeof(ar->arp_tha)))
664 1.9 glass goto out;
665 1.13 gwr bcopy((caddr_t)ar->arp_spa, (caddr_t)&srv_ip, sizeof(srv_ip));
666 1.12 mycroft bcopy((caddr_t)ar->arp_tpa, (caddr_t)&myip, sizeof(myip));
667 1.9 glass myip_initialized = 1;
668 1.13 gwr wake: /* Do wakeup every time in case it was missed. */
669 1.12 mycroft wakeup((caddr_t)&myip);
670 1.12 mycroft
671 1.12 mycroft out:
672 1.9 glass m_freem(m);
673 1.9 glass }
674 1.9 glass
675 1.9 glass /*
676 1.9 glass * Send a RARP request for the ip address of the specified interface.
677 1.9 glass * The request should be RFC 903-compliant.
678 1.9 glass */
679 1.12 mycroft void
680 1.12 mycroft revarprequest(ifp)
681 1.9 glass struct ifnet *ifp;
682 1.9 glass {
683 1.9 glass struct sockaddr sa;
684 1.9 glass struct mbuf *m;
685 1.9 glass struct ether_header *eh;
686 1.9 glass struct ether_arp *ea;
687 1.12 mycroft struct arpcom *ac = (struct arpcom *)ifp;
688 1.9 glass
689 1.9 glass if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
690 1.9 glass return;
691 1.9 glass m->m_len = sizeof(*ea);
692 1.9 glass m->m_pkthdr.len = sizeof(*ea);
693 1.9 glass MH_ALIGN(m, sizeof(*ea));
694 1.9 glass ea = mtod(m, struct ether_arp *);
695 1.12 mycroft eh = (struct ether_header *)sa.sa_data;
696 1.12 mycroft bzero((caddr_t)ea, sizeof(*ea));
697 1.9 glass bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
698 1.12 mycroft sizeof(eh->ether_dhost));
699 1.9 glass eh->ether_type = htons(ETHERTYPE_REVARP);
700 1.9 glass ea->arp_hrd = htons(ARPHRD_ETHER);
701 1.9 glass ea->arp_pro = htons(ETHERTYPE_IP);
702 1.9 glass ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */
703 1.9 glass ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */
704 1.12 mycroft ea->arp_op = htons(ARPOP_REVREQUEST);
705 1.9 glass bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
706 1.9 glass sizeof(ea->arp_sha));
707 1.9 glass bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_tha,
708 1.9 glass sizeof(ea->arp_tha));
709 1.9 glass sa.sa_family = AF_UNSPEC;
710 1.9 glass sa.sa_len = sizeof(sa);
711 1.9 glass ifp->if_output(ifp, m, &sa, (struct rtentry *)0);
712 1.9 glass }
713 1.9 glass
714 1.9 glass /*
715 1.13 gwr * RARP for the ip address of the specified interface, but also
716 1.13 gwr * save the ip address of the server that sent the answer.
717 1.13 gwr * Timeout if no response is received.
718 1.9 glass */
719 1.12 mycroft int
720 1.13 gwr revarpwhoarewe(ifp, serv_in, clnt_in)
721 1.9 glass struct ifnet *ifp;
722 1.13 gwr struct in_addr *serv_in;
723 1.13 gwr struct in_addr *clnt_in;
724 1.9 glass {
725 1.9 glass int result, count = 20;
726 1.9 glass
727 1.9 glass if (myip_initialized)
728 1.9 glass return EIO;
729 1.9 glass
730 1.9 glass myip_ifp = ifp;
731 1.9 glass revarp_in_progress = 1;
732 1.9 glass while (count--) {
733 1.12 mycroft revarprequest(ifp);
734 1.12 mycroft result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
735 1.12 mycroft if (result != EWOULDBLOCK)
736 1.12 mycroft break;
737 1.9 glass }
738 1.9 glass revarp_in_progress = 0;
739 1.9 glass if (!myip_initialized)
740 1.9 glass return ENETUNREACH;
741 1.9 glass
742 1.13 gwr bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
743 1.13 gwr bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
744 1.9 glass return 0;
745 1.1 cgd }
746 1.13 gwr
747 1.13 gwr /* For compatibility: only saves interface address. */
748 1.13 gwr int
749 1.13 gwr revarpwhoami(in, ifp)
750 1.13 gwr struct in_addr *in;
751 1.13 gwr struct ifnet *ifp;
752 1.13 gwr {
753 1.13 gwr struct in_addr server;
754 1.13 gwr return (revarpwhoarewe(ifp, &server, in));
755 1.13 gwr }
756 1.13 gwr
757 1.13 gwr
759 1.13 gwr #ifdef DDB
760 1.13 gwr static void
761 1.13 gwr db_print_sa(sa)
762 1.13 gwr struct sockaddr *sa;
763 1.13 gwr {
764 1.13 gwr int len;
765 1.13 gwr u_char *p;
766 1.13 gwr
767 1.13 gwr if (sa == 0) {
768 1.13 gwr db_printf("[NULL]");
769 1.13 gwr return;
770 1.13 gwr }
771 1.13 gwr
772 1.13 gwr p = (u_char*)sa;
773 1.13 gwr len = sa->sa_len;
774 1.13 gwr db_printf("[");
775 1.13 gwr while (len > 0) {
776 1.13 gwr db_printf("%d", *p);
777 1.13 gwr p++; len--;
778 1.13 gwr if (len) db_printf(",");
779 1.13 gwr }
780 1.13 gwr db_printf("]\n");
781 1.13 gwr }
782 1.13 gwr static void
783 1.13 gwr db_print_ifa(ifa)
784 1.13 gwr struct ifaddr *ifa;
785 1.13 gwr {
786 1.13 gwr if (ifa == 0)
787 1.13 gwr return;
788 1.13 gwr db_printf(" ifa_addr=");
789 1.13 gwr db_print_sa(ifa->ifa_addr);
790 1.13 gwr db_printf(" ifa_dsta=");
791 1.13 gwr db_print_sa(ifa->ifa_dstaddr);
792 1.13 gwr db_printf(" ifa_mask=");
793 1.13 gwr db_print_sa(ifa->ifa_netmask);
794 1.13 gwr db_printf(" flags=0x%x,refcnt=%d,metric=%d\n",
795 1.13 gwr ifa->ifa_flags,
796 1.13 gwr ifa->ifa_refcnt,
797 1.13 gwr ifa->ifa_metric);
798 1.13 gwr }
799 1.13 gwr static void
800 1.13 gwr db_print_llinfo(li)
801 1.13 gwr caddr_t li;
802 1.13 gwr {
803 1.13 gwr struct llinfo_arp *la;
804 1.13 gwr
805 1.13 gwr if (li == 0)
806 1.13 gwr return;
807 1.13 gwr la = (struct llinfo_arp *)li;
808 1.13 gwr db_printf(" la_rt=0x%x la_hold=0x%x, la_asked=0x%x\n",
809 1.13 gwr la->la_rt, la->la_hold, la->la_asked);
810 1.13 gwr }
811 1.13 gwr /*
812 1.13 gwr * Function to pass to rn_walktree().
813 1.13 gwr * Return non-zero error to abort walk.
814 1.13 gwr */
815 1.13 gwr static int
816 1.13 gwr db_show_radix_node(rn, w)
817 1.13 gwr struct radix_node *rn;
818 1.13 gwr void *w;
819 1.13 gwr {
820 1.13 gwr struct rtentry *rt = (struct rtentry *)rn;
821 1.13 gwr
822 1.13 gwr db_printf("rtentry=0x%x", rt);
823 1.13 gwr
824 1.13 gwr db_printf(" flags=0x%x refcnt=%d use=%d expire=%d\n",
825 1.13 gwr rt->rt_flags, rt->rt_refcnt,
826 1.13 gwr rt->rt_use, rt->rt_expire);
827 1.13 gwr
828 1.13 gwr db_printf(" key="); db_print_sa(rt_key(rt));
829 1.13 gwr db_printf(" mask="); db_print_sa(rt_mask(rt));
830 1.13 gwr db_printf(" gw="); db_print_sa(rt->rt_gateway);
831 1.13 gwr
832 1.13 gwr db_printf(" ifp=0x%x ", rt->rt_ifp);
833 1.13 gwr if (rt->rt_ifp)
834 1.13 gwr db_printf("(%s%d)",
835 1.13 gwr rt->rt_ifp->if_name,
836 1.13 gwr rt->rt_ifp->if_unit);
837 1.13 gwr else
838 1.13 gwr db_printf("(NULL)");
839 1.13 gwr
840 1.13 gwr db_printf(" ifa=0x%x\n", rt->rt_ifa);
841 1.13 gwr db_print_ifa(rt->rt_ifa);
842 1.13 gwr
843 1.13 gwr db_printf(" genmask="); db_print_sa(rt->rt_genmask);
844 1.13 gwr
845 1.13 gwr db_printf(" gwroute=0x%x llinfo=0x%x\n",
846 1.13 gwr rt->rt_gwroute, rt->rt_llinfo);
847 1.13 gwr db_print_llinfo(rt->rt_llinfo);
848 1.13 gwr
849 1.13 gwr return (0);
850 1.13 gwr }
851 1.13 gwr /*
852 1.13 gwr * Function to print all the route trees.
853 1.13 gwr * Use this from ddb: "call db_show_arptab"
854 1.13 gwr */
855 1.13 gwr db_show_arptab()
856 1.13 gwr {
857 1.13 gwr struct radix_node_head *rnh;
858 1.13 gwr rnh = rt_tables[AF_INET];
859 1.13 gwr db_printf("Route tree for AF_INET\n");
860 1.13 gwr if (rnh == NULL) {
861 1.13 gwr db_printf(" (not initialized)\n");
862 1.13 gwr return (0);
863 1.13 gwr }
864 1.13 gwr rn_walktree(rnh, db_show_radix_node, NULL);
865 1.13 gwr return (0);
866 1.13 gwr }
867 1.15 chopps #endif
868 #endif /* INET */
869