if_arp.c revision 1.86 1 /* $NetBSD: if_arp.c,v 1.86 2002/06/25 02:53:27 enami Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Public Access Networks Corporation ("Panix"). It was developed under
9 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
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 NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1982, 1986, 1988, 1993
42 * The Regents of the University of California. All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * @(#)if_ether.c 8.2 (Berkeley) 9/26/94
73 */
74
75 /*
76 * Ethernet address resolution protocol.
77 * TODO:
78 * add "inuse/lock" bit (or ref. count) along with valid bit
79 */
80
81 #include <sys/cdefs.h>
82 __KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.86 2002/06/25 02:53:27 enami Exp $");
83
84 #include "opt_ddb.h"
85 #include "opt_inet.h"
86
87 #ifdef INET
88
89 #include "bridge.h"
90
91 #include <sys/param.h>
92 #include <sys/systm.h>
93 #include <sys/callout.h>
94 #include <sys/malloc.h>
95 #include <sys/mbuf.h>
96 #include <sys/socket.h>
97 #include <sys/time.h>
98 #include <sys/kernel.h>
99 #include <sys/errno.h>
100 #include <sys/ioctl.h>
101 #include <sys/syslog.h>
102 #include <sys/proc.h>
103 #include <sys/protosw.h>
104 #include <sys/domain.h>
105
106 #include <net/ethertypes.h>
107 #include <net/if.h>
108 #include <net/if_dl.h>
109 #include <net/if_token.h>
110 #include <net/if_types.h>
111 #include <net/route.h>
112
113 #include <netinet/in.h>
114 #include <netinet/in_systm.h>
115 #include <netinet/in_var.h>
116 #include <netinet/ip.h>
117 #include <netinet/if_inarp.h>
118
119 #include "loop.h"
120 #include "arc.h"
121 #if NARC > 0
122 #include <net/if_arc.h>
123 #endif
124 #include "fddi.h"
125 #if NFDDI > 0
126 #include <net/if_fddi.h>
127 #endif
128 #include "token.h"
129 #include "token.h"
130
131 #define SIN(s) ((struct sockaddr_in *)s)
132 #define SDL(s) ((struct sockaddr_dl *)s)
133 #define SRP(s) ((struct sockaddr_inarp *)s)
134
135 /*
136 * ARP trailer negotiation. Trailer protocol is not IP specific,
137 * but ARP request/response use IP addresses.
138 */
139 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
140
141 /* timer values */
142 int arpt_prune = (5*60*1); /* walk list every 5 minutes */
143 int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
144 int arpt_down = 20; /* once declared down, don't send for 20 secs */
145 #define rt_expire rt_rmx.rmx_expire
146
147 static void arprequest __P((struct ifnet *,
148 struct in_addr *, struct in_addr *, u_int8_t *));
149 static void arptfree __P((struct llinfo_arp *));
150 static void arptimer __P((void *));
151 static struct llinfo_arp *arplookup __P((struct mbuf *, struct in_addr *,
152 int, int));
153 static void in_arpinput __P((struct mbuf *));
154
155 #if NLOOP > 0
156 extern struct ifnet loif[NLOOP];
157 #endif
158 LIST_HEAD(, llinfo_arp) llinfo_arp;
159 struct ifqueue arpintrq = {0, 0, 0, 50};
160 int arp_inuse, arp_allocated, arp_intimer;
161 int arp_maxtries = 5;
162 int useloopback = 1; /* use loopback interface for local traffic */
163 int arpinit_done = 0;
164
165 struct arpstat arpstat;
166 struct callout arptimer_ch;
167
168
169 /* revarp state */
170 static struct in_addr myip, srv_ip;
171 static int myip_initialized = 0;
172 static int revarp_in_progress = 0;
173 static struct ifnet *myip_ifp = NULL;
174
175 #ifdef DDB
176 static void db_print_sa __P((struct sockaddr *));
177 static void db_print_ifa __P((struct ifaddr *));
178 static void db_print_llinfo __P((caddr_t));
179 static int db_show_radix_node __P((struct radix_node *, void *));
180 #endif
181
182 /*
183 * this should be elsewhere.
184 */
185
186 static char *
187 lla_snprintf __P((u_int8_t *, int));
188
189 static char *
190 lla_snprintf(adrp, len)
191 u_int8_t *adrp;
192 int len;
193 {
194 #define NUMBUFS 3
195 static char buf[NUMBUFS][16*3];
196 static int bnum = 0;
197 static const char hexdigits[] = {
198 '0','1','2','3','4','5','6','7',
199 '8','9','a','b','c','d','e','f'
200 };
201
202 int i;
203 char *p;
204
205 p = buf[bnum];
206
207 *p++ = hexdigits[(*adrp)>>4];
208 *p++ = hexdigits[(*adrp++)&0xf];
209
210 for (i=1; i<len && i<16; i++) {
211 *p++ = ':';
212 *p++ = hexdigits[(*adrp)>>4];
213 *p++ = hexdigits[(*adrp++)&0xf];
214 }
215
216 *p = 0;
217 p = buf[bnum];
218 bnum = (bnum + 1) % NUMBUFS;
219 return p;
220 }
221
222 struct protosw arpsw[] = {
223 { 0, 0, 0, 0,
224 0, 0, 0, 0,
225 0,
226 0, 0, 0, arp_drain,
227 }
228 };
229
230
231 struct domain arpdomain =
232 { PF_ARP, "arp", 0, 0, 0,
233 arpsw, &arpsw[sizeof(arpsw)/sizeof(arpsw[0])]
234 };
235
236 /*
237 * ARP table locking.
238 *
239 * to prevent lossage vs. the arp_drain routine (which may be called at
240 * any time, including in a device driver context), we do two things:
241 *
242 * 1) manipulation of la->la_hold is done at splnet() (for all of
243 * about two instructions).
244 *
245 * 2) manipulation of the arp table's linked list is done under the
246 * protection of the ARP_LOCK; if arp_drain() or arptimer is called
247 * while the arp table is locked, we punt and try again later.
248 */
249
250 static int arp_locked;
251 static __inline int arp_lock_try __P((int));
252 static __inline void arp_unlock __P((void));
253
254 static __inline int
255 arp_lock_try(int recurse)
256 {
257 int s;
258
259 /*
260 * Use splvm() -- we're blocking things that would cause
261 * mbuf allocation.
262 */
263 s = splvm();
264 if (!recurse && arp_locked) {
265 splx(s);
266 return (0);
267 }
268 arp_locked++;
269 splx(s);
270 return (1);
271 }
272
273 static __inline void
274 arp_unlock()
275 {
276 int s;
277
278 s = splvm();
279 arp_locked--;
280 splx(s);
281 }
282
283 #ifdef DIAGNOSTIC
284 #define ARP_LOCK(recurse) \
285 do { \
286 if (arp_lock_try(recurse) == 0) { \
287 printf("%s:%d: arp already locked\n", __FILE__, __LINE__); \
288 panic("arp_lock"); \
289 } \
290 } while (0)
291 #define ARP_LOCK_CHECK() \
292 do { \
293 if (arp_locked == 0) { \
294 printf("%s:%d: arp lock not held\n", __FILE__, __LINE__); \
295 panic("arp lock check"); \
296 } \
297 } while (0)
298 #else
299 #define ARP_LOCK(x) (void) arp_lock_try(x)
300 #define ARP_LOCK_CHECK() /* nothing */
301 #endif
302
303 #define ARP_UNLOCK() arp_unlock()
304
305 /*
306 * ARP protocol drain routine. Called when memory is in short supply.
307 * Called at splvm();
308 */
309
310 void
311 arp_drain()
312 {
313 struct llinfo_arp *la, *nla;
314 int count = 0;
315 struct mbuf *mold;
316
317 if (arp_lock_try(0) == 0) {
318 printf("arp_drain: locked; punting\n");
319 return;
320 }
321
322 for (la = LIST_FIRST(&llinfo_arp); la != 0; la = nla) {
323 nla = LIST_NEXT(la, la_list);
324
325 mold = la->la_hold;
326 la->la_hold = 0;
327
328 if (mold) {
329 m_freem(mold);
330 count++;
331 }
332 }
333 ARP_UNLOCK();
334 arpstat.as_dfrdropped += count;
335 }
336
337
338 /*
339 * Timeout routine. Age arp_tab entries periodically.
340 */
341 /* ARGSUSED */
342 static void
343 arptimer(arg)
344 void *arg;
345 {
346 int s;
347 struct llinfo_arp *la, *nla;
348
349 s = splsoftnet();
350
351 if (arp_lock_try(0) == 0) {
352 /* get it later.. */
353 splx(s);
354 return;
355 }
356
357 callout_reset(&arptimer_ch, arpt_prune * hz, arptimer, NULL);
358 for (la = LIST_FIRST(&llinfo_arp); la != 0; la = nla) {
359 struct rtentry *rt = la->la_rt;
360
361 nla = LIST_NEXT(la, la_list);
362 if (rt->rt_expire && rt->rt_expire <= time.tv_sec)
363 arptfree(la); /* timer has expired; clear */
364 }
365
366 ARP_UNLOCK();
367
368 splx(s);
369 }
370
371 /*
372 * Parallel to llc_rtrequest.
373 */
374 void
375 arp_rtrequest(req, rt, info)
376 int req;
377 struct rtentry *rt;
378 struct rt_addrinfo *info;
379 {
380 struct sockaddr *gate = rt->rt_gateway;
381 struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
382 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
383 size_t allocsize;
384 struct mbuf *mold;
385 int s;
386 struct in_ifaddr *ia;
387 struct ifaddr *ifa;
388
389 if (!arpinit_done) {
390 arpinit_done = 1;
391 /*
392 * We generate expiration times from time.tv_sec
393 * so avoid accidently creating permanent routes.
394 */
395 if (time.tv_sec == 0) {
396 time.tv_sec++;
397 }
398 callout_init(&arptimer_ch);
399 callout_reset(&arptimer_ch, hz, arptimer, NULL);
400 }
401
402 if ((rt->rt_flags & RTF_GATEWAY) != 0) {
403 if (req != RTM_ADD)
404 return;
405
406 /*
407 * linklayers with particular link MTU limitation.
408 */
409 switch(rt->rt_ifp->if_type) {
410 #if NFDDI > 0
411 case IFT_FDDI:
412 if (rt->rt_ifp->if_mtu > FDDIIPMTU)
413 rt->rt_rmx.rmx_mtu = FDDIIPMTU;
414 break;
415 #endif
416 #if NARC > 0
417 case IFT_ARCNET:
418 {
419 int arcipifmtu;
420
421 if (rt->rt_ifp->if_flags & IFF_LINK0)
422 arcipifmtu = arc_ipmtu;
423 else
424 arcipifmtu = ARCMTU;
425 if (rt->rt_ifp->if_mtu > arcipifmtu)
426 rt->rt_rmx.rmx_mtu = arcipifmtu;
427 break;
428 }
429 #endif
430 }
431 return;
432 }
433
434 ARP_LOCK(1); /* we may already be locked here. */
435
436 switch (req) {
437
438 case RTM_ADD:
439 /*
440 * XXX: If this is a manually added route to interface
441 * such as older version of routed or gated might provide,
442 * restore cloning bit.
443 */
444 if ((rt->rt_flags & RTF_HOST) == 0 &&
445 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
446 rt->rt_flags |= RTF_CLONING;
447 if (rt->rt_flags & RTF_CLONING) {
448 /*
449 * Case 1: This route should come from a route to iface.
450 */
451 rt_setgate(rt, rt_key(rt),
452 (struct sockaddr *)&null_sdl);
453 gate = rt->rt_gateway;
454 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
455 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
456 /*
457 * Give this route an expiration time, even though
458 * it's a "permanent" route, so that routes cloned
459 * from it do not need their expiration time set.
460 */
461 rt->rt_expire = time.tv_sec;
462 /*
463 * linklayers with particular link MTU limitation.
464 */
465 switch (rt->rt_ifp->if_type) {
466 #if NFDDI > 0
467 case IFT_FDDI:
468 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
469 (rt->rt_rmx.rmx_mtu > FDDIIPMTU ||
470 (rt->rt_rmx.rmx_mtu == 0 &&
471 rt->rt_ifp->if_mtu > FDDIIPMTU)))
472 rt->rt_rmx.rmx_mtu = FDDIIPMTU;
473 break;
474 #endif
475 #if NARC > 0
476 case IFT_ARCNET:
477 {
478 int arcipifmtu;
479 if (rt->rt_ifp->if_flags & IFF_LINK0)
480 arcipifmtu = arc_ipmtu;
481 else
482 arcipifmtu = ARCMTU;
483
484 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
485 (rt->rt_rmx.rmx_mtu > arcipifmtu ||
486 (rt->rt_rmx.rmx_mtu == 0 &&
487 rt->rt_ifp->if_mtu > arcipifmtu)))
488 rt->rt_rmx.rmx_mtu = arcipifmtu;
489 break;
490 }
491 #endif
492 }
493 break;
494 }
495 /* Announce a new entry if requested. */
496 if (rt->rt_flags & RTF_ANNOUNCE)
497 arprequest(rt->rt_ifp,
498 &SIN(rt_key(rt))->sin_addr,
499 &SIN(rt_key(rt))->sin_addr,
500 (u_char *)LLADDR(SDL(gate)));
501 /*FALLTHROUGH*/
502 case RTM_RESOLVE:
503 if (gate->sa_family != AF_LINK ||
504 gate->sa_len < sizeof(null_sdl)) {
505 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
506 break;
507 }
508 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
509 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
510 if (la != 0)
511 break; /* This happens on a route change */
512 /*
513 * Case 2: This route may come from cloning, or a manual route
514 * add with a LL address.
515 */
516 switch (SDL(gate)->sdl_type) {
517 #if NTOKEN > 0
518 case IFT_ISO88025:
519 allocsize = sizeof(*la) + sizeof(struct token_rif);
520 break;
521 #endif /* NTOKEN > 0 */
522 default:
523 allocsize = sizeof(*la);
524 }
525 R_Malloc(la, struct llinfo_arp *, allocsize);
526 rt->rt_llinfo = (caddr_t)la;
527 if (la == 0) {
528 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
529 break;
530 }
531 arp_inuse++, arp_allocated++;
532 Bzero(la, allocsize);
533 la->la_rt = rt;
534 rt->rt_flags |= RTF_LLINFO;
535 LIST_INSERT_HEAD(&llinfo_arp, la, la_list);
536
537 INADDR_TO_IA(SIN(rt_key(rt))->sin_addr, ia);
538 while (ia && ia->ia_ifp != rt->rt_ifp)
539 NEXT_IA_WITH_SAME_ADDR(ia);
540 if (ia) {
541 /*
542 * This test used to be
543 * if (loif.if_flags & IFF_UP)
544 * It allowed local traffic to be forced through
545 * the hardware by configuring the loopback down.
546 * However, it causes problems during network
547 * configuration for boards that can't receive
548 * packets they send. It is now necessary to clear
549 * "useloopback" and remove the route to force
550 * traffic out to the hardware.
551 *
552 * In 4.4BSD, the above "if" statement checked
553 * rt->rt_ifa against rt_key(rt). It was changed
554 * to the current form so that we can provide a
555 * better support for multiple IPv4 addresses on a
556 * interface.
557 */
558 rt->rt_expire = 0;
559 Bcopy(LLADDR(rt->rt_ifp->if_sadl),
560 LLADDR(SDL(gate)),
561 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
562 #if NLOOP > 0
563 if (useloopback)
564 rt->rt_ifp = &loif[0];
565 #endif
566 /*
567 * make sure to set rt->rt_ifa to the interface
568 * address we are using, otherwise we will have trouble
569 * with source address selection.
570 */
571 ifa = &ia->ia_ifa;
572 if (ifa != rt->rt_ifa) {
573 IFAFREE(rt->rt_ifa);
574 IFAREF(ifa);
575 rt->rt_ifa = ifa;
576 }
577 }
578 break;
579
580 case RTM_DELETE:
581 if (la == 0)
582 break;
583 arp_inuse--;
584 LIST_REMOVE(la, la_list);
585 rt->rt_llinfo = 0;
586 rt->rt_flags &= ~RTF_LLINFO;
587
588 s = splnet();
589 mold = la->la_hold;
590 la->la_hold = 0;
591 splx(s);
592
593 if (mold)
594 m_freem(mold);
595
596 Free((caddr_t)la);
597 }
598 ARP_UNLOCK();
599 }
600
601 /*
602 * Broadcast an ARP request. Caller specifies:
603 * - arp header source ip address
604 * - arp header target ip address
605 * - arp header source ethernet address
606 */
607 static void
608 arprequest(ifp, sip, tip, enaddr)
609 struct ifnet *ifp;
610 struct in_addr *sip, *tip;
611 u_int8_t *enaddr;
612 {
613 struct mbuf *m;
614 struct arphdr *ah;
615 struct sockaddr sa;
616
617 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
618 return;
619 switch (ifp->if_type) {
620 case IFT_IEEE1394:
621 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
622 ifp->if_addrlen;
623 break;
624 default:
625 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
626 2 * ifp->if_addrlen;
627 break;
628 }
629 m->m_pkthdr.len = m->m_len;
630 MH_ALIGN(m, m->m_len);
631 ah = mtod(m, struct arphdr *);
632 bzero((caddr_t)ah, m->m_len);
633 switch (ifp->if_type) {
634 case IFT_ETHER: /* RFC826 */
635 case IFT_FDDI: /* RFC1390 */
636 case IFT_HIPPI: /* RFC1374 */
637 ah->ar_hrd = htons(ARPHRD_ETHER);
638 break;
639 case IFT_IEEE1394: /* RFC2734 */
640 ah->ar_hrd = htons(ARPHRD_IEEE1394);
641 break;
642 default:
643 /* why we don't set ar_hrd in other cases? */
644 break;
645 }
646 ah->ar_pro = htons(ETHERTYPE_IP);
647 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
648 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
649 ah->ar_op = htons(ARPOP_REQUEST);
650 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
651 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
652 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
653 sa.sa_family = AF_ARP;
654 sa.sa_len = 2;
655 m->m_flags |= M_BCAST;
656 arpstat.as_sndtotal++;
657 arpstat.as_sndrequest++;
658 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
659 }
660
661 /*
662 * Resolve an IP address into an ethernet address. If success,
663 * desten is filled in. If there is no entry in arptab,
664 * set one up and broadcast a request for the IP address.
665 * Hold onto this mbuf and resend it once the address
666 * is finally resolved. A return value of 1 indicates
667 * that desten has been filled in and the packet should be sent
668 * normally; a 0 return indicates that the packet has been
669 * taken over here, either now or for later transmission.
670 */
671 int
672 arpresolve(ifp, rt, m, dst, desten)
673 struct ifnet *ifp;
674 struct rtentry *rt;
675 struct mbuf *m;
676 struct sockaddr *dst;
677 u_char *desten;
678 {
679 struct llinfo_arp *la;
680 struct sockaddr_dl *sdl;
681 struct mbuf *mold;
682 int s;
683
684 if (rt)
685 la = (struct llinfo_arp *)rt->rt_llinfo;
686 else {
687 if ((la = arplookup(m, &SIN(dst)->sin_addr, 1, 0)) != NULL)
688 rt = la->la_rt;
689 }
690 if (la == 0 || rt == 0) {
691 arpstat.as_allocfail++;
692 log(LOG_DEBUG,
693 "arpresolve: can't allocate llinfo on %s for %s\n",
694 ifp->if_xname, in_fmtaddr(SIN(dst)->sin_addr));
695 m_freem(m);
696 return (0);
697 }
698 sdl = SDL(rt->rt_gateway);
699 /*
700 * Check the address family and length is valid, the address
701 * is resolved; otherwise, try to resolve.
702 */
703 if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) &&
704 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
705 bcopy(LLADDR(sdl), desten,
706 min(sdl->sdl_alen, ifp->if_addrlen));
707 return 1;
708 }
709 /*
710 * There is an arptab entry, but no ethernet address
711 * response yet. Replace the held mbuf with this
712 * latest one.
713 */
714
715 arpstat.as_dfrtotal++;
716 s = splnet();
717 mold = la->la_hold;
718 la->la_hold = m;
719 splx(s);
720
721 if (mold) {
722 arpstat.as_dfrdropped++;
723 m_freem(mold);
724 }
725
726 /*
727 * Re-send the ARP request when appropriate.
728 */
729 #ifdef DIAGNOSTIC
730 if (rt->rt_expire == 0) {
731 /* This should never happen. (Should it? -gwr) */
732 printf("arpresolve: unresolved and rt_expire == 0\n");
733 /* Set expiration time to now (expired). */
734 rt->rt_expire = time.tv_sec;
735 }
736 #endif
737 if (rt->rt_expire) {
738 rt->rt_flags &= ~RTF_REJECT;
739 if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) {
740 rt->rt_expire = time.tv_sec;
741 if (la->la_asked++ < arp_maxtries)
742 arprequest(ifp,
743 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
744 &SIN(dst)->sin_addr,
745 LLADDR(ifp->if_sadl));
746 else {
747 rt->rt_flags |= RTF_REJECT;
748 rt->rt_expire += arpt_down;
749 la->la_asked = 0;
750 }
751 }
752 }
753 return (0);
754 }
755
756 /*
757 * Common length and type checks are done here,
758 * then the protocol-specific routine is called.
759 */
760 void
761 arpintr()
762 {
763 struct mbuf *m;
764 struct arphdr *ar;
765 int s;
766 int arplen;
767
768 while (arpintrq.ifq_head) {
769 s = splnet();
770 IF_DEQUEUE(&arpintrq, m);
771 splx(s);
772 if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
773 panic("arpintr");
774
775 arpstat.as_rcvtotal++;
776
777 /*
778 * First, make sure we have at least struct arphdr.
779 */
780 if (m->m_len < sizeof(struct arphdr) ||
781 (ar = mtod(m, struct arphdr *)) == NULL)
782 goto badlen;
783
784 switch (m->m_pkthdr.rcvif->if_type) {
785 case IFT_IEEE1394:
786 arplen = sizeof(struct arphdr) +
787 ar->ar_hln + 2 * ar->ar_pln;
788 break;
789 default:
790 arplen = sizeof(struct arphdr) +
791 2 * ar->ar_hln + 2 * ar->ar_pln;
792 break;
793 }
794
795 if (/* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */
796 m->m_len >= arplen)
797 switch (ntohs(ar->ar_pro)) {
798 case ETHERTYPE_IP:
799 case ETHERTYPE_IPTRAILERS:
800 in_arpinput(m);
801 continue;
802 default:
803 arpstat.as_rcvbadproto++;
804 }
805 else {
806 badlen:
807 arpstat.as_rcvbadlen++;
808 }
809 m_freem(m);
810 }
811 }
812
813 /*
814 * ARP for Internet protocols on 10 Mb/s Ethernet.
815 * Algorithm is that given in RFC 826.
816 * In addition, a sanity check is performed on the sender
817 * protocol address, to catch impersonators.
818 * We no longer handle negotiations for use of trailer protocol:
819 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
820 * along with IP replies if we wanted trailers sent to us,
821 * and also sent them in response to IP replies.
822 * This allowed either end to announce the desire to receive
823 * trailer packets.
824 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
825 * but formerly didn't normally send requests.
826 */
827 static void
828 in_arpinput(m)
829 struct mbuf *m;
830 {
831 struct arphdr *ah;
832 struct ifnet *ifp = m->m_pkthdr.rcvif;
833 struct llinfo_arp *la = 0;
834 struct rtentry *rt;
835 struct in_ifaddr *ia;
836 #if NBRIDGE > 0
837 struct in_ifaddr *bridge_ia = NULL;
838 #endif
839 struct sockaddr_dl *sdl;
840 struct sockaddr sa;
841 struct in_addr isaddr, itaddr, myaddr;
842 int op;
843 struct mbuf *mold;
844 int s;
845
846 ah = mtod(m, struct arphdr *);
847 op = ntohs(ah->ar_op);
848 bcopy((caddr_t)ar_spa(ah), (caddr_t)&isaddr, sizeof (isaddr));
849 bcopy((caddr_t)ar_tpa(ah), (caddr_t)&itaddr, sizeof (itaddr));
850
851 switch (m->m_pkthdr.rcvif->if_type) {
852 case IFT_IEEE1394:
853 if (ntohs(ah->ar_hrd) == ARPHRD_IEEE1394)
854 ;
855 else {
856 /* XXX this is to make sure we compute ar_tha right */
857 /* XXX check ar_hrd more strictly? */
858 ah->ar_hrd = htons(ARPHRD_IEEE1394);
859 }
860 break;
861 default:
862 /* XXX check ar_hrd? */
863 break;
864 }
865
866 if (m->m_flags & (M_BCAST|M_MCAST))
867 arpstat.as_rcvmcast++;
868
869 /*
870 * If the target IP address is zero, ignore the packet.
871 * This prevents the code below from tring to answer
872 * when we are using IP address zero (booting).
873 */
874 if (in_nullhost(itaddr)) {
875 arpstat.as_rcvzerotpa++;
876 goto out;
877 }
878
879 /*
880 * If the source IP address is zero, this is most likely a
881 * confused host trying to use IP address zero. (Windoze?)
882 * XXX: Should we bother trying to reply to these?
883 */
884 if (in_nullhost(isaddr)) {
885 arpstat.as_rcvzerospa++;
886 goto out;
887 }
888
889 /*
890 * Search for a matching interface address
891 * or any address on the interface to use
892 * as a dummy address in the rest of this function
893 */
894 INADDR_TO_IA(itaddr, ia);
895 while (ia != NULL) {
896 if (ia->ia_ifp == m->m_pkthdr.rcvif)
897 break;
898
899 #if NBRIDGE > 0
900 /*
901 * If the interface we received the packet on
902 * is part of a bridge, check to see if we need
903 * to "bridge" the packet to ourselves at this
904 * layer. Note we still prefer a perfect match,
905 * but allow this weaker match if necessary.
906 */
907 if (m->m_pkthdr.rcvif->if_bridge != NULL &&
908 m->m_pkthdr.rcvif->if_bridge == ia->ia_ifp->if_bridge)
909 bridge_ia = ia;
910 #endif /* NBRIDGE > 0 */
911
912 NEXT_IA_WITH_SAME_ADDR(ia);
913 }
914
915 #if NBRIDGE > 0
916 if (ia == NULL && bridge_ia != NULL) {
917 ia = bridge_ia;
918 ifp = bridge_ia->ia_ifp;
919 }
920 #endif
921
922 if (ia == NULL) {
923 INADDR_TO_IA(isaddr, ia);
924 while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif)
925 NEXT_IA_WITH_SAME_ADDR(ia);
926
927 if (ia == NULL) {
928 IFP_TO_IA(ifp, ia);
929 if (ia == NULL) {
930 arpstat.as_rcvnoint++;
931 goto out;
932 }
933 }
934 }
935
936 myaddr = ia->ia_addr.sin_addr;
937
938 /* XXX checks for bridge case? */
939 if (!bcmp((caddr_t)ar_sha(ah), LLADDR(ifp->if_sadl),
940 ifp->if_addrlen)) {
941 arpstat.as_rcvlocalsha++;
942 goto out; /* it's from me, ignore it. */
943 }
944
945 /* XXX checks for bridge case? */
946 if (!bcmp((caddr_t)ar_sha(ah), (caddr_t)ifp->if_broadcastaddr,
947 ifp->if_addrlen)) {
948 arpstat.as_rcvbcastsha++;
949 log(LOG_ERR,
950 "%s: arp: link address is broadcast for IP address %s!\n",
951 ifp->if_xname, in_fmtaddr(isaddr));
952 goto out;
953 }
954
955 if (in_hosteq(isaddr, myaddr)) {
956 arpstat.as_rcvlocalspa++;
957 log(LOG_ERR,
958 "duplicate IP address %s sent from link address %s\n",
959 in_fmtaddr(isaddr), lla_snprintf(ar_sha(ah), ah->ar_hln));
960 itaddr = myaddr;
961 goto reply;
962 }
963 la = arplookup(m, &isaddr, in_hosteq(itaddr, myaddr), 0);
964 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
965 if (sdl->sdl_alen &&
966 bcmp((caddr_t)ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
967 if (rt->rt_flags & RTF_STATIC) {
968 arpstat.as_rcvoverperm++;
969 log(LOG_INFO,
970 "%s tried to overwrite permanent arp info"
971 " for %s\n",
972 lla_snprintf(ar_sha(ah), ah->ar_hln),
973 in_fmtaddr(isaddr));
974 goto out;
975 } else if (rt->rt_ifp != ifp) {
976 arpstat.as_rcvoverint++;
977 log(LOG_INFO,
978 "%s on %s tried to overwrite "
979 "arp info for %s on %s\n",
980 lla_snprintf(ar_sha(ah), ah->ar_hln),
981 ifp->if_xname, in_fmtaddr(isaddr),
982 rt->rt_ifp->if_xname);
983 goto out;
984 } else {
985 arpstat.as_rcvover++;
986 log(LOG_INFO,
987 "arp info overwritten for %s by %s\n",
988 in_fmtaddr(isaddr),
989 lla_snprintf(ar_sha(ah), ah->ar_hln));
990 }
991 }
992 /*
993 * sanity check for the address length.
994 * XXX this does not work for protocols with variable address
995 * length. -is
996 */
997 if (sdl->sdl_alen &&
998 sdl->sdl_alen != ah->ar_hln) {
999 arpstat.as_rcvlenchg++;
1000 log(LOG_WARNING,
1001 "arp from %s: new addr len %d, was %d",
1002 in_fmtaddr(isaddr), ah->ar_hln, sdl->sdl_alen);
1003 }
1004 if (ifp->if_addrlen != ah->ar_hln) {
1005 arpstat.as_rcvbadlen++;
1006 log(LOG_WARNING,
1007 "arp from %s: addr len: new %d, i/f %d (ignored)",
1008 in_fmtaddr(isaddr), ah->ar_hln,
1009 ifp->if_addrlen);
1010 goto reply;
1011 }
1012 #if NTOKEN > 0
1013 /*
1014 * XXX uses m_data and assumes the complete answer including
1015 * XXX token-ring headers is in the same buf
1016 */
1017 if (ifp->if_type == IFT_ISO88025) {
1018 struct token_header *trh;
1019
1020 trh = (struct token_header *)M_TRHSTART(m);
1021 if (trh->token_shost[0] & TOKEN_RI_PRESENT) {
1022 struct token_rif *rif;
1023 size_t riflen;
1024
1025 rif = TOKEN_RIF(trh);
1026 riflen = (ntohs(rif->tr_rcf) &
1027 TOKEN_RCF_LEN_MASK) >> 8;
1028
1029 if (riflen > 2 &&
1030 riflen < sizeof(struct token_rif) &&
1031 (riflen & 1) == 0) {
1032 rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION);
1033 rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK);
1034 bcopy(rif, TOKEN_RIF(la), riflen);
1035 }
1036 }
1037 }
1038 #endif /* NTOKEN > 0 */
1039 bcopy((caddr_t)ar_sha(ah), LLADDR(sdl),
1040 sdl->sdl_alen = ah->ar_hln);
1041 if (rt->rt_expire)
1042 rt->rt_expire = time.tv_sec + arpt_keep;
1043 rt->rt_flags &= ~RTF_REJECT;
1044 la->la_asked = 0;
1045
1046 s = splnet();
1047 mold = la->la_hold;
1048 la->la_hold = 0;
1049 splx(s);
1050
1051 if (mold) {
1052 arpstat.as_dfrsent++;
1053 (*ifp->if_output)(ifp, mold, rt_key(rt), rt);
1054 }
1055 }
1056 reply:
1057 if (op != ARPOP_REQUEST) {
1058 if (op == ARPOP_REPLY)
1059 arpstat.as_rcvreply++;
1060 out:
1061 m_freem(m);
1062 return;
1063 }
1064 arpstat.as_rcvrequest++;
1065 if (in_hosteq(itaddr, myaddr)) {
1066 /* I am the target */
1067 if (ar_tha(ah))
1068 bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah),
1069 ah->ar_hln);
1070 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
1071 } else {
1072 la = arplookup(m, &itaddr, 0, SIN_PROXY);
1073 if (la == 0)
1074 goto out;
1075 rt = la->la_rt;
1076 if (ar_tha(ah))
1077 bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah),
1078 ah->ar_hln);
1079 sdl = SDL(rt->rt_gateway);
1080 bcopy(LLADDR(sdl), (caddr_t)ar_sha(ah), ah->ar_hln);
1081 }
1082
1083 bcopy((caddr_t)ar_spa(ah), (caddr_t)ar_tpa(ah), ah->ar_pln);
1084 bcopy((caddr_t)&itaddr, (caddr_t)ar_spa(ah), ah->ar_pln);
1085 ah->ar_op = htons(ARPOP_REPLY);
1086 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1087 switch (ifp->if_type) {
1088 case IFT_IEEE1394:
1089 /*
1090 * ieee1394 arp reply is broadcast
1091 */
1092 m->m_flags &= ~M_MCAST;
1093 m->m_flags |= M_BCAST;
1094 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + ah->ar_hln;
1095 break;
1096
1097 default:
1098 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
1099 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
1100 break;
1101 }
1102 m->m_pkthdr.len = m->m_len;
1103 sa.sa_family = AF_ARP;
1104 sa.sa_len = 2;
1105 arpstat.as_sndtotal++;
1106 arpstat.as_sndreply++;
1107 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
1108 return;
1109 }
1110
1111 /*
1112 * Free an arp entry.
1113 */
1114 static void
1115 arptfree(la)
1116 struct llinfo_arp *la;
1117 {
1118 struct rtentry *rt = la->la_rt;
1119 struct sockaddr_dl *sdl;
1120
1121 ARP_LOCK_CHECK();
1122
1123 if (rt == 0)
1124 panic("arptfree");
1125 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
1126 sdl->sdl_family == AF_LINK) {
1127 sdl->sdl_alen = 0;
1128 la->la_asked = 0;
1129 rt->rt_flags &= ~RTF_REJECT;
1130 return;
1131 }
1132 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
1133 0, (struct rtentry **)0);
1134 }
1135
1136 /*
1137 * Lookup or enter a new address in arptab.
1138 */
1139 static struct llinfo_arp *
1140 arplookup(m, addr, create, proxy)
1141 struct mbuf *m;
1142 struct in_addr *addr;
1143 int create, proxy;
1144 {
1145 struct arphdr *ah;
1146 struct ifnet *ifp = m->m_pkthdr.rcvif;
1147 struct rtentry *rt;
1148 static struct sockaddr_inarp sin;
1149 const char *why = 0;
1150
1151 ah = mtod(m, struct arphdr *);
1152 sin.sin_len = sizeof(sin);
1153 sin.sin_family = AF_INET;
1154 sin.sin_addr = *addr;
1155 sin.sin_other = proxy ? SIN_PROXY : 0;
1156 rt = rtalloc1(sintosa(&sin), create);
1157 if (rt == 0)
1158 return (0);
1159 rt->rt_refcnt--;
1160
1161 if (rt->rt_flags & RTF_GATEWAY)
1162 why = "host is not on local network";
1163 else if ((rt->rt_flags & RTF_LLINFO) == 0) {
1164 arpstat.as_allocfail++;
1165 why = "could not allocate llinfo";
1166 } else if (rt->rt_gateway->sa_family != AF_LINK)
1167 why = "gateway route is not ours";
1168 else
1169 return ((struct llinfo_arp *)rt->rt_llinfo);
1170
1171 if (create)
1172 log(LOG_DEBUG, "arplookup: unable to enter address"
1173 " for %s@%s on %s (%s)\n",
1174 in_fmtaddr(*addr), lla_snprintf(ar_sha(ah), ah->ar_hln),
1175 ifp->if_xname, why);
1176 return (0);
1177 }
1178
1179 int
1180 arpioctl(cmd, data)
1181 u_long cmd;
1182 caddr_t data;
1183 {
1184
1185 return (EOPNOTSUPP);
1186 }
1187
1188 void
1189 arp_ifinit(ifp, ifa)
1190 struct ifnet *ifp;
1191 struct ifaddr *ifa;
1192 {
1193 struct in_addr *ip;
1194
1195 /*
1196 * Warn the user if another station has this IP address,
1197 * but only if the interface IP address is not zero.
1198 */
1199 ip = &IA_SIN(ifa)->sin_addr;
1200 if (!in_nullhost(*ip))
1201 arprequest(ifp, ip, ip, LLADDR(ifp->if_sadl));
1202
1203 ifa->ifa_rtrequest = arp_rtrequest;
1204 ifa->ifa_flags |= RTF_CLONING;
1205 }
1206
1207 /*
1208 * Called from 10 Mb/s Ethernet interrupt handlers
1209 * when ether packet type ETHERTYPE_REVARP
1210 * is received. Common length and type checks are done here,
1211 * then the protocol-specific routine is called.
1212 */
1213 void
1214 revarpinput(m)
1215 struct mbuf *m;
1216 {
1217 struct arphdr *ar;
1218
1219 if (m->m_len < sizeof(struct arphdr))
1220 goto out;
1221 ar = mtod(m, struct arphdr *);
1222 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
1223 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
1224 goto out;
1225 #endif
1226 if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
1227 goto out;
1228 switch (ntohs(ar->ar_pro)) {
1229 case ETHERTYPE_IP:
1230 case ETHERTYPE_IPTRAILERS:
1231 in_revarpinput(m);
1232 return;
1233
1234 default:
1235 break;
1236 }
1237 out:
1238 m_freem(m);
1239 }
1240
1241 /*
1242 * RARP for Internet protocols on 10 Mb/s Ethernet.
1243 * Algorithm is that given in RFC 903.
1244 * We are only using for bootstrap purposes to get an ip address for one of
1245 * our interfaces. Thus we support no user-interface.
1246 *
1247 * Since the contents of the RARP reply are specific to the interface that
1248 * sent the request, this code must ensure that they are properly associated.
1249 *
1250 * Note: also supports ARP via RARP packets, per the RFC.
1251 */
1252 void
1253 in_revarpinput(m)
1254 struct mbuf *m;
1255 {
1256 struct ifnet *ifp;
1257 struct arphdr *ah;
1258 int op;
1259
1260 ah = mtod(m, struct arphdr *);
1261 op = ntohs(ah->ar_op);
1262
1263 switch (m->m_pkthdr.rcvif->if_type) {
1264 case IFT_IEEE1394:
1265 /* ARP without target hardware address is not supported */
1266 goto out;
1267 default:
1268 break;
1269 }
1270
1271 switch (op) {
1272 case ARPOP_REQUEST:
1273 case ARPOP_REPLY: /* per RFC */
1274 in_arpinput(m);
1275 return;
1276 case ARPOP_REVREPLY:
1277 break;
1278 case ARPOP_REVREQUEST: /* handled by rarpd(8) */
1279 default:
1280 goto out;
1281 }
1282 if (!revarp_in_progress)
1283 goto out;
1284 ifp = m->m_pkthdr.rcvif;
1285 if (ifp != myip_ifp) /* !same interface */
1286 goto out;
1287 if (myip_initialized)
1288 goto wake;
1289 if (bcmp(ar_tha(ah), LLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen))
1290 goto out;
1291 bcopy((caddr_t)ar_spa(ah), (caddr_t)&srv_ip, sizeof(srv_ip));
1292 bcopy((caddr_t)ar_tpa(ah), (caddr_t)&myip, sizeof(myip));
1293 myip_initialized = 1;
1294 wake: /* Do wakeup every time in case it was missed. */
1295 wakeup((caddr_t)&myip);
1296
1297 out:
1298 m_freem(m);
1299 }
1300
1301 /*
1302 * Send a RARP request for the ip address of the specified interface.
1303 * The request should be RFC 903-compliant.
1304 */
1305 void
1306 revarprequest(ifp)
1307 struct ifnet *ifp;
1308 {
1309 struct sockaddr sa;
1310 struct mbuf *m;
1311 struct arphdr *ah;
1312
1313 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
1314 return;
1315 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
1316 2*ifp->if_addrlen;
1317 m->m_pkthdr.len = m->m_len;
1318 MH_ALIGN(m, m->m_len);
1319 ah = mtod(m, struct arphdr *);
1320 bzero((caddr_t)ah, m->m_len);
1321 ah->ar_pro = htons(ETHERTYPE_IP);
1322 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
1323 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
1324 ah->ar_op = htons(ARPOP_REVREQUEST);
1325
1326 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
1327 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_tha(ah), ah->ar_hln);
1328
1329 sa.sa_family = AF_ARP;
1330 sa.sa_len = 2;
1331 m->m_flags |= M_BCAST;
1332 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
1333
1334 }
1335
1336 /*
1337 * RARP for the ip address of the specified interface, but also
1338 * save the ip address of the server that sent the answer.
1339 * Timeout if no response is received.
1340 */
1341 int
1342 revarpwhoarewe(ifp, serv_in, clnt_in)
1343 struct ifnet *ifp;
1344 struct in_addr *serv_in;
1345 struct in_addr *clnt_in;
1346 {
1347 int result, count = 20;
1348
1349 myip_initialized = 0;
1350 myip_ifp = ifp;
1351
1352 revarp_in_progress = 1;
1353 while (count--) {
1354 revarprequest(ifp);
1355 result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
1356 if (result != EWOULDBLOCK)
1357 break;
1358 }
1359 revarp_in_progress = 0;
1360
1361 if (!myip_initialized)
1362 return ENETUNREACH;
1363
1364 bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
1365 bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
1366 return 0;
1367 }
1368
1369
1370
1372 #ifdef DDB
1373
1374 #include <machine/db_machdep.h>
1375 #include <ddb/db_interface.h>
1376 #include <ddb/db_output.h>
1377 static void
1378 db_print_sa(sa)
1379 struct sockaddr *sa;
1380 {
1381 int len;
1382 u_char *p;
1383
1384 if (sa == 0) {
1385 db_printf("[NULL]");
1386 return;
1387 }
1388
1389 p = (u_char*)sa;
1390 len = sa->sa_len;
1391 db_printf("[");
1392 while (len > 0) {
1393 db_printf("%d", *p);
1394 p++; len--;
1395 if (len) db_printf(",");
1396 }
1397 db_printf("]\n");
1398 }
1399 static void
1400 db_print_ifa(ifa)
1401 struct ifaddr *ifa;
1402 {
1403 if (ifa == 0)
1404 return;
1405 db_printf(" ifa_addr=");
1406 db_print_sa(ifa->ifa_addr);
1407 db_printf(" ifa_dsta=");
1408 db_print_sa(ifa->ifa_dstaddr);
1409 db_printf(" ifa_mask=");
1410 db_print_sa(ifa->ifa_netmask);
1411 db_printf(" flags=0x%x,refcnt=%d,metric=%d\n",
1412 ifa->ifa_flags,
1413 ifa->ifa_refcnt,
1414 ifa->ifa_metric);
1415 }
1416 static void
1417 db_print_llinfo(li)
1418 caddr_t li;
1419 {
1420 struct llinfo_arp *la;
1421
1422 if (li == 0)
1423 return;
1424 la = (struct llinfo_arp *)li;
1425 db_printf(" la_rt=%p la_hold=%p, la_asked=0x%lx\n",
1426 la->la_rt, la->la_hold, la->la_asked);
1427 }
1428 /*
1429 * Function to pass to rn_walktree().
1430 * Return non-zero error to abort walk.
1431 */
1432 static int
1433 db_show_radix_node(rn, w)
1434 struct radix_node *rn;
1435 void *w;
1436 {
1437 struct rtentry *rt = (struct rtentry *)rn;
1438
1439 db_printf("rtentry=%p", rt);
1440
1441 db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n",
1442 rt->rt_flags, rt->rt_refcnt,
1443 rt->rt_use, rt->rt_expire);
1444
1445 db_printf(" key="); db_print_sa(rt_key(rt));
1446 db_printf(" mask="); db_print_sa(rt_mask(rt));
1447 db_printf(" gw="); db_print_sa(rt->rt_gateway);
1448
1449 db_printf(" ifp=%p ", rt->rt_ifp);
1450 if (rt->rt_ifp)
1451 db_printf("(%s)", rt->rt_ifp->if_xname);
1452 else
1453 db_printf("(NULL)");
1454
1455 db_printf(" ifa=%p\n", rt->rt_ifa);
1456 db_print_ifa(rt->rt_ifa);
1457
1458 db_printf(" genmask="); db_print_sa(rt->rt_genmask);
1459
1460 db_printf(" gwroute=%p llinfo=%p\n",
1461 rt->rt_gwroute, rt->rt_llinfo);
1462 db_print_llinfo(rt->rt_llinfo);
1463
1464 return (0);
1465 }
1466 /*
1467 * Function to print all the route trees.
1468 * Use this from ddb: "show arptab"
1469 */
1470 void
1471 db_show_arptab(addr, have_addr, count, modif)
1472 db_expr_t addr;
1473 int have_addr;
1474 db_expr_t count;
1475 char * modif;
1476 {
1477 struct radix_node_head *rnh;
1478 rnh = rt_tables[AF_INET];
1479 db_printf("Route tree for AF_INET\n");
1480 if (rnh == NULL) {
1481 db_printf(" (not initialized)\n");
1482 return;
1483 }
1484 rn_walktree(rnh, db_show_radix_node, NULL);
1485 return;
1486 }
1487 #endif
1488 #endif /* INET */
1489