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