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