if_arp.c revision 1.163 1 /* $NetBSD: if_arp.c,v 1.163 2015/05/02 14:41:32 roy Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000, 2008 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)if_ether.c 8.2 (Berkeley) 9/26/94
62 */
63
64 /*
65 * Ethernet address resolution protocol.
66 * TODO:
67 * add "inuse/lock" bit (or ref. count) along with valid bit
68 */
69
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.163 2015/05/02 14:41:32 roy Exp $");
72
73 #include "opt_ddb.h"
74 #include "opt_inet.h"
75
76 #ifdef INET
77
78 #include "bridge.h"
79
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/callout.h>
83 #include <sys/malloc.h>
84 #include <sys/mbuf.h>
85 #include <sys/socket.h>
86 #include <sys/time.h>
87 #include <sys/timetc.h>
88 #include <sys/kernel.h>
89 #include <sys/errno.h>
90 #include <sys/ioctl.h>
91 #include <sys/syslog.h>
92 #include <sys/proc.h>
93 #include <sys/protosw.h>
94 #include <sys/domain.h>
95 #include <sys/sysctl.h>
96 #include <sys/socketvar.h>
97 #include <sys/percpu.h>
98 #include <sys/cprng.h>
99
100 #include <net/ethertypes.h>
101 #include <net/if.h>
102 #include <net/if_dl.h>
103 #include <net/if_token.h>
104 #include <net/if_types.h>
105 #include <net/if_ether.h>
106 #include <net/net_osdep.h>
107 #include <net/route.h>
108 #include <net/net_stats.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 "arcnet.h"
117 #if NARCNET > 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 #include "carp.h"
126 #if NCARP > 0
127 #include <netinet/ip_carp.h>
128 #endif
129
130 #define SIN(s) ((struct sockaddr_in *)s)
131 #define SRP(s) ((struct sockaddr_inarp *)s)
132
133 /*
134 * ARP trailer negotiation. Trailer protocol is not IP specific,
135 * but ARP request/response use IP addresses.
136 */
137 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
138
139 /* timer values */
140 int arpt_prune = (5*60*1); /* walk list every 5 minutes */
141 int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
142 int arpt_down = 20; /* once declared down, don't send for 20 secs */
143 int arpt_refresh = (5*60); /* time left before refreshing */
144 #define rt_expire rt_rmx.rmx_expire
145 #define rt_pksent rt_rmx.rmx_pksent
146
147 int ip_dad_count = PROBE_NUM;
148 #ifdef ARP_DEBUG
149 static int arp_debug = 1;
150 #else
151 static int arp_debug = 0;
152 #endif
153 #define arplog(x) do { if (arp_debug) log x; } while (/*CONSTCOND*/ 0)
154
155 static struct sockaddr *arp_setgate(struct rtentry *, struct sockaddr *,
156 const struct sockaddr *);
157 static void arptfree(struct llinfo_arp *);
158 static void arptimer(void *);
159 static struct llinfo_arp *arplookup1(struct mbuf *, const struct in_addr *,
160 int, int, struct rtentry *);
161 static struct llinfo_arp *arplookup(struct mbuf *, const struct in_addr *,
162 int, int);
163 static void in_arpinput(struct mbuf *);
164 static void arp_drainstub(void);
165
166 static void arp_dad_timer(struct ifaddr *);
167 static void arp_dad_duplicated(struct ifaddr *);
168
169 LIST_HEAD(llinfo_arpq, llinfo_arp) llinfo_arp;
170 struct ifqueue arpintrq = {
171 .ifq_head = NULL,
172 .ifq_tail = NULL,
173 .ifq_len = 0,
174 .ifq_maxlen = 50,
175 .ifq_drops = 0,
176 };
177 int arp_inuse, arp_allocated;
178 int arp_maxtries = 5;
179 int useloopback = 1; /* use loopback interface for local traffic */
180 int arpinit_done = 0;
181
182 static percpu_t *arpstat_percpu;
183
184 #define ARP_STAT_GETREF() _NET_STAT_GETREF(arpstat_percpu)
185 #define ARP_STAT_PUTREF() _NET_STAT_PUTREF(arpstat_percpu)
186
187 #define ARP_STATINC(x) _NET_STATINC(arpstat_percpu, x)
188 #define ARP_STATADD(x, v) _NET_STATADD(arpstat_percpu, x, v)
189
190 struct callout arptimer_ch;
191
192 /* revarp state */
193 struct in_addr myip, srv_ip;
194 int myip_initialized = 0;
195 int revarp_in_progress = 0;
196 struct ifnet *myip_ifp = NULL;
197
198 #ifdef DDB
199 static void db_print_sa(const struct sockaddr *);
200 static void db_print_ifa(struct ifaddr *);
201 static void db_print_llinfo(void *);
202 static int db_show_rtentry(struct rtentry *, void *);
203 #endif
204
205 static int arp_drainwanted;
206
207 static int log_movements = 1;
208 static int log_permanent_modify = 1;
209 static int log_wrong_iface = 1;
210 static int log_unknown_network = 1;
211
212 /*
213 * this should be elsewhere.
214 */
215
216 static char *
217 lla_snprintf(u_int8_t *, int);
218
219 static char *
220 lla_snprintf(u_int8_t *adrp, int len)
221 {
222 #define NUMBUFS 3
223 static char buf[NUMBUFS][16*3];
224 static int bnum = 0;
225
226 int i;
227 char *p;
228
229 p = buf[bnum];
230
231 *p++ = hexdigits[(*adrp)>>4];
232 *p++ = hexdigits[(*adrp++)&0xf];
233
234 for (i=1; i<len && i<16; i++) {
235 *p++ = ':';
236 *p++ = hexdigits[(*adrp)>>4];
237 *p++ = hexdigits[(*adrp++)&0xf];
238 }
239
240 *p = 0;
241 p = buf[bnum];
242 bnum = (bnum + 1) % NUMBUFS;
243 return p;
244 }
245
246 DOMAIN_DEFINE(arpdomain); /* forward declare and add to link set */
247
248 static void
249 arp_fasttimo(void)
250 {
251 if (arp_drainwanted) {
252 arp_drain();
253 arp_drainwanted = 0;
254 }
255 }
256
257 const struct protosw arpsw[] = {
258 { .pr_type = 0,
259 .pr_domain = &arpdomain,
260 .pr_protocol = 0,
261 .pr_flags = 0,
262 .pr_input = 0,
263 .pr_output = 0,
264 .pr_ctlinput = 0,
265 .pr_ctloutput = 0,
266 .pr_usrreqs = 0,
267 .pr_init = arp_init,
268 .pr_fasttimo = arp_fasttimo,
269 .pr_slowtimo = 0,
270 .pr_drain = arp_drainstub,
271 }
272 };
273
274 struct domain arpdomain = {
275 .dom_family = PF_ARP,
276 .dom_name = "arp",
277 .dom_protosw = arpsw,
278 .dom_protoswNPROTOSW = &arpsw[__arraycount(arpsw)],
279 };
280
281 /*
282 * ARP table locking.
283 *
284 * to prevent lossage vs. the arp_drain routine (which may be called at
285 * any time, including in a device driver context), we do two things:
286 *
287 * 1) manipulation of la->la_hold is done at splnet() (for all of
288 * about two instructions).
289 *
290 * 2) manipulation of the arp table's linked list is done under the
291 * protection of the ARP_LOCK; if arp_drain() or arptimer is called
292 * while the arp table is locked, we punt and try again later.
293 */
294
295 static int arp_locked;
296 static inline int arp_lock_try(int);
297 static inline void arp_unlock(void);
298
299 static inline int
300 arp_lock_try(int recurse)
301 {
302 int s;
303
304 /*
305 * Use splvm() -- we're blocking things that would cause
306 * mbuf allocation.
307 */
308 s = splvm();
309 if (!recurse && arp_locked) {
310 splx(s);
311 return 0;
312 }
313 arp_locked++;
314 splx(s);
315 return 1;
316 }
317
318 static inline void
319 arp_unlock(void)
320 {
321 int s;
322
323 s = splvm();
324 arp_locked--;
325 splx(s);
326 }
327
328 #ifdef DIAGNOSTIC
329 #define ARP_LOCK(recurse) \
330 do { \
331 if (arp_lock_try(recurse) == 0) { \
332 printf("%s:%d: arp already locked\n", __FILE__, __LINE__); \
333 panic("arp_lock"); \
334 } \
335 } while (/*CONSTCOND*/ 0)
336 #define ARP_LOCK_CHECK() \
337 do { \
338 if (arp_locked == 0) { \
339 printf("%s:%d: arp lock not held\n", __FILE__, __LINE__); \
340 panic("arp lock check"); \
341 } \
342 } while (/*CONSTCOND*/ 0)
343 #else
344 #define ARP_LOCK(x) (void) arp_lock_try(x)
345 #define ARP_LOCK_CHECK() /* nothing */
346 #endif
347
348 #define ARP_UNLOCK() arp_unlock()
349
350 static void sysctl_net_inet_arp_setup(struct sysctllog **);
351
352 void
353 arp_init(void)
354 {
355
356 sysctl_net_inet_arp_setup(NULL);
357 arpstat_percpu = percpu_alloc(sizeof(uint64_t) * ARP_NSTATS);
358 }
359
360 static void
361 arp_drainstub(void)
362 {
363 arp_drainwanted = 1;
364 }
365
366 /*
367 * ARP protocol drain routine. Called when memory is in short supply.
368 * Called at splvm(); don't acquire softnet_lock as can be called from
369 * hardware interrupt handlers.
370 */
371 void
372 arp_drain(void)
373 {
374 struct llinfo_arp *la, *nla;
375 int count = 0;
376 struct mbuf *mold;
377
378 KERNEL_LOCK(1, NULL);
379
380 if (arp_lock_try(0) == 0) {
381 KERNEL_UNLOCK_ONE(NULL);
382 return;
383 }
384
385 for (la = LIST_FIRST(&llinfo_arp); la != NULL; la = nla) {
386 nla = LIST_NEXT(la, la_list);
387
388 mold = la->la_hold;
389 la->la_hold = 0;
390
391 if (mold) {
392 m_freem(mold);
393 count++;
394 }
395 }
396 ARP_UNLOCK();
397 ARP_STATADD(ARP_STAT_DFRDROPPED, count);
398 KERNEL_UNLOCK_ONE(NULL);
399 }
400
401
402 /*
403 * Timeout routine. Age arp_tab entries periodically.
404 */
405 /* ARGSUSED */
406 static void
407 arptimer(void *arg)
408 {
409 struct llinfo_arp *la, *nla;
410
411 mutex_enter(softnet_lock);
412 KERNEL_LOCK(1, NULL);
413
414 if (arp_lock_try(0) == 0) {
415 /* get it later.. */
416 KERNEL_UNLOCK_ONE(NULL);
417 mutex_exit(softnet_lock);
418 return;
419 }
420
421 callout_reset(&arptimer_ch, arpt_prune * hz, arptimer, NULL);
422 for (la = LIST_FIRST(&llinfo_arp); la != NULL; la = nla) {
423 struct rtentry *rt = la->la_rt;
424
425 nla = LIST_NEXT(la, la_list);
426 if (rt->rt_expire == 0)
427 continue;
428 if ((rt->rt_expire - time_second) < arpt_refresh &&
429 rt->rt_pksent > (time_second - arpt_keep)) {
430 /*
431 * If the entry has been used during since last
432 * refresh, try to renew it before deleting.
433 */
434 arprequest(rt->rt_ifp,
435 &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
436 &satocsin(rt_getkey(rt))->sin_addr,
437 CLLADDR(rt->rt_ifp->if_sadl));
438 } else if (rt->rt_expire <= time_second)
439 arptfree(la); /* timer has expired; clear */
440 }
441
442 ARP_UNLOCK();
443
444 KERNEL_UNLOCK_ONE(NULL);
445 mutex_exit(softnet_lock);
446 }
447
448 /*
449 * We set the gateway for RTF_CLONING routes to a "prototype"
450 * link-layer sockaddr whose interface type (if_type) and interface
451 * index (if_index) fields are prepared.
452 */
453 static struct sockaddr *
454 arp_setgate(struct rtentry *rt, struct sockaddr *gate,
455 const struct sockaddr *netmask)
456 {
457 const struct ifnet *ifp = rt->rt_ifp;
458 uint8_t namelen = strlen(ifp->if_xname);
459 uint8_t addrlen = ifp->if_addrlen;
460
461 /*
462 * XXX: If this is a manually added route to interface
463 * such as older version of routed or gated might provide,
464 * restore cloning bit.
465 */
466 if ((rt->rt_flags & RTF_HOST) == 0 && netmask != NULL &&
467 satocsin(netmask)->sin_addr.s_addr != 0xffffffff)
468 rt->rt_flags |= RTF_CLONING;
469 if (rt->rt_flags & RTF_CLONING ||
470 ((rt->rt_flags & (RTF_LLINFO | RTF_LOCAL)) && !rt->rt_llinfo))
471 {
472 union {
473 struct sockaddr sa;
474 struct sockaddr_storage ss;
475 struct sockaddr_dl sdl;
476 } u;
477 /*
478 * Case 1: This route should come from a route to iface.
479 */
480 sockaddr_dl_init(&u.sdl, sizeof(u.ss),
481 ifp->if_index, ifp->if_type, NULL, namelen, NULL, addrlen);
482 rt_setgate(rt, &u.sa);
483 gate = rt->rt_gateway;
484 }
485 return gate;
486 }
487
488 /*
489 * Parallel to llc_rtrequest.
490 */
491 void
492 arp_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info)
493 {
494 struct sockaddr *gate = rt->rt_gateway;
495 struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
496 size_t allocsize;
497 struct mbuf *mold;
498 int s;
499 struct in_ifaddr *ia;
500 struct ifaddr *ifa;
501 struct ifnet *ifp = rt->rt_ifp;
502
503 if (!arpinit_done) {
504 arpinit_done = 1;
505 /*
506 * We generate expiration times from time_second
507 * so avoid accidentally creating permanent routes.
508 */
509 if (time_second == 0) {
510 struct timespec ts;
511 ts.tv_sec = 1;
512 ts.tv_nsec = 0;
513 tc_setclock(&ts);
514 }
515 callout_init(&arptimer_ch, CALLOUT_MPSAFE);
516 callout_reset(&arptimer_ch, hz, arptimer, NULL);
517 }
518
519 if (req == RTM_LLINFO_UPD) {
520 struct in_addr *in;
521
522 if ((ifa = info->rti_ifa) == NULL)
523 return;
524
525 in = &ifatoia(ifa)->ia_addr.sin_addr;
526
527 if (ifatoia(ifa)->ia4_flags &
528 (IN_IFF_NOTREADY | IN_IFF_DETACHED))
529 {
530 arplog((LOG_DEBUG, "arp_request: %s not ready\n",
531 in_fmtaddr(*in)));
532 return;
533 }
534
535 arprequest(ifa->ifa_ifp, in, in,
536 CLLADDR(ifa->ifa_ifp->if_sadl));
537 return;
538 }
539
540 if ((rt->rt_flags & RTF_GATEWAY) != 0) {
541 if (req != RTM_ADD)
542 return;
543
544 /*
545 * linklayers with particular link MTU limitation.
546 */
547 switch(ifp->if_type) {
548 #if NFDDI > 0
549 case IFT_FDDI:
550 if (ifp->if_mtu > FDDIIPMTU)
551 rt->rt_rmx.rmx_mtu = FDDIIPMTU;
552 break;
553 #endif
554 #if NARC > 0
555 case IFT_ARCNET:
556 {
557 int arcipifmtu;
558
559 if (ifp->if_flags & IFF_LINK0)
560 arcipifmtu = arc_ipmtu;
561 else
562 arcipifmtu = ARCMTU;
563 if (ifp->if_mtu > arcipifmtu)
564 rt->rt_rmx.rmx_mtu = arcipifmtu;
565 break;
566 }
567 #endif
568 }
569 return;
570 }
571
572 ARP_LOCK(1); /* we may already be locked here. */
573
574 switch (req) {
575 case RTM_SETGATE:
576 gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]);
577 break;
578 case RTM_ADD:
579 gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]);
580 if (rt->rt_flags & RTF_CLONING ||
581 ((rt->rt_flags & (RTF_LLINFO | RTF_LOCAL)) && !la))
582 {
583 /*
584 * Give this route an expiration time, even though
585 * it's a "permanent" route, so that routes cloned
586 * from it do not need their expiration time set.
587 */
588 rt->rt_expire = time_second;
589 /*
590 * linklayers with particular link MTU limitation.
591 */
592 switch (ifp->if_type) {
593 #if NFDDI > 0
594 case IFT_FDDI:
595 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
596 (rt->rt_rmx.rmx_mtu > FDDIIPMTU ||
597 (rt->rt_rmx.rmx_mtu == 0 &&
598 ifp->if_mtu > FDDIIPMTU)))
599 rt->rt_rmx.rmx_mtu = FDDIIPMTU;
600 break;
601 #endif
602 #if NARC > 0
603 case IFT_ARCNET:
604 {
605 int arcipifmtu;
606 if (ifp->if_flags & IFF_LINK0)
607 arcipifmtu = arc_ipmtu;
608 else
609 arcipifmtu = ARCMTU;
610
611 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
612 (rt->rt_rmx.rmx_mtu > arcipifmtu ||
613 (rt->rt_rmx.rmx_mtu == 0 &&
614 ifp->if_mtu > arcipifmtu)))
615 rt->rt_rmx.rmx_mtu = arcipifmtu;
616 break;
617 }
618 #endif
619 }
620 if (rt->rt_flags & RTF_CLONING)
621 break;
622 }
623 /* Announce a new entry if requested. */
624 if (rt->rt_flags & RTF_ANNOUNCE) {
625 INADDR_TO_IA(satocsin(rt_getkey(rt))->sin_addr, ia);
626 while (ia && ia->ia_ifp != ifp)
627 NEXT_IA_WITH_SAME_ADDR(ia);
628 if (ia == NULL ||
629 ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED))
630 ;
631 else
632 arprequest(ifp,
633 &satocsin(rt_getkey(rt))->sin_addr,
634 &satocsin(rt_getkey(rt))->sin_addr,
635 CLLADDR(satocsdl(gate)));
636 }
637 /*FALLTHROUGH*/
638 case RTM_RESOLVE:
639 if (gate->sa_family != AF_LINK ||
640 gate->sa_len < sockaddr_dl_measure(0, ifp->if_addrlen)) {
641 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
642 break;
643 }
644
645 satosdl(gate)->sdl_type = ifp->if_type;
646 satosdl(gate)->sdl_index = ifp->if_index;
647 if (la != NULL)
648 break; /* This happens on a route change */
649
650 /* If the route is for a broadcast address mark it as such.
651 * This way we can avoid an expensive call to in_broadcast()
652 * in ip_output() most of the time (because the route passed
653 * to ip_output() is almost always a host route). */
654 if (rt->rt_flags & RTF_HOST &&
655 !(rt->rt_flags & RTF_BROADCAST) &&
656 in_broadcast(satocsin(rt_getkey(rt))->sin_addr, rt->rt_ifp))
657 rt->rt_flags |= RTF_BROADCAST;
658 /* There is little point in resolving the broadcast address */
659 if (rt->rt_flags & RTF_BROADCAST)
660 break;
661
662 /*
663 * Case 2: This route may come from cloning, or a manual route
664 * add with a LL address.
665 */
666 switch (ifp->if_type) {
667 #if NTOKEN > 0
668 case IFT_ISO88025:
669 allocsize = sizeof(*la) + sizeof(struct token_rif);
670 break;
671 #endif /* NTOKEN > 0 */
672 default:
673 allocsize = sizeof(*la);
674 }
675 R_Malloc(la, struct llinfo_arp *, allocsize);
676 rt->rt_llinfo = (void *)la;
677 if (la == NULL) {
678 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
679 break;
680 }
681 arp_inuse++, arp_allocated++;
682 memset(la, 0, allocsize);
683 la->la_rt = rt;
684 rt->rt_flags |= RTF_LLINFO;
685 LIST_INSERT_HEAD(&llinfo_arp, la, la_list);
686
687 INADDR_TO_IA(satocsin(rt_getkey(rt))->sin_addr, ia);
688 while (ia && ia->ia_ifp != ifp)
689 NEXT_IA_WITH_SAME_ADDR(ia);
690 if (ia) {
691 /*
692 * This test used to be
693 * if (lo0ifp->if_flags & IFF_UP)
694 * It allowed local traffic to be forced through
695 * the hardware by configuring the loopback down.
696 * However, it causes problems during network
697 * configuration for boards that can't receive
698 * packets they send. It is now necessary to clear
699 * "useloopback" and remove the route to force
700 * traffic out to the hardware.
701 *
702 * In 4.4BSD, the above "if" statement checked
703 * rt->rt_ifa against rt_getkey(rt). It was changed
704 * to the current form so that we can provide a
705 * better support for multiple IPv4 addresses on a
706 * interface.
707 */
708 rt->rt_expire = 0;
709 if (sockaddr_dl_init(satosdl(gate), gate->sa_len,
710 ifp->if_index, ifp->if_type, NULL, 0,
711 CLLADDR(ifp->if_sadl), ifp->if_addrlen) == NULL) {
712 panic("%s(%s): sockaddr_dl_init cannot fail",
713 __func__, ifp->if_xname);
714 }
715 if (useloopback)
716 ifp = rt->rt_ifp = lo0ifp;
717 /*
718 * make sure to set rt->rt_ifa to the interface
719 * address we are using, otherwise we will have trouble
720 * with source address selection.
721 */
722 ifa = &ia->ia_ifa;
723 if (ifa != rt->rt_ifa)
724 rt_replace_ifa(rt, ifa);
725 }
726 break;
727
728 case RTM_DELETE:
729 if (la == NULL)
730 break;
731 arp_inuse--;
732 LIST_REMOVE(la, la_list);
733 rt->rt_llinfo = NULL;
734 rt->rt_flags &= ~RTF_LLINFO;
735
736 s = splnet();
737 mold = la->la_hold;
738 la->la_hold = 0;
739 splx(s);
740
741 if (mold)
742 m_freem(mold);
743
744 Free((void *)la);
745 }
746 ARP_UNLOCK();
747 }
748
749 /*
750 * Broadcast an ARP request. Caller specifies:
751 * - arp header source ip address
752 * - arp header target ip address
753 * - arp header source ethernet address
754 */
755 void
756 arprequest(struct ifnet *ifp,
757 const struct in_addr *sip, const struct in_addr *tip,
758 const u_int8_t *enaddr)
759 {
760 struct mbuf *m;
761 struct arphdr *ah;
762 struct sockaddr sa;
763 uint64_t *arps;
764
765 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
766 return;
767 MCLAIM(m, &arpdomain.dom_mowner);
768 switch (ifp->if_type) {
769 case IFT_IEEE1394:
770 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
771 ifp->if_addrlen;
772 break;
773 default:
774 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
775 2 * ifp->if_addrlen;
776 break;
777 }
778 m->m_pkthdr.len = m->m_len;
779 MH_ALIGN(m, m->m_len);
780 ah = mtod(m, struct arphdr *);
781 memset(ah, 0, m->m_len);
782 switch (ifp->if_type) {
783 case IFT_IEEE1394: /* RFC2734 */
784 /* fill it now for ar_tpa computation */
785 ah->ar_hrd = htons(ARPHRD_IEEE1394);
786 break;
787 default:
788 /* ifp->if_output will fill ar_hrd */
789 break;
790 }
791 ah->ar_pro = htons(ETHERTYPE_IP);
792 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
793 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
794 ah->ar_op = htons(ARPOP_REQUEST);
795 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
796 memcpy(ar_spa(ah), sip, ah->ar_pln);
797 memcpy(ar_tpa(ah), tip, ah->ar_pln);
798 sa.sa_family = AF_ARP;
799 sa.sa_len = 2;
800 m->m_flags |= M_BCAST;
801 arps = ARP_STAT_GETREF();
802 arps[ARP_STAT_SNDTOTAL]++;
803 arps[ARP_STAT_SENDREQUEST]++;
804 ARP_STAT_PUTREF();
805 (*ifp->if_output)(ifp, m, &sa, NULL);
806 }
807
808 /*
809 * Resolve an IP address into an ethernet address. If success,
810 * desten is filled in. If there is no entry in arptab,
811 * set one up and broadcast a request for the IP address.
812 * Hold onto this mbuf and resend it once the address
813 * is finally resolved. A return value of 1 indicates
814 * that desten has been filled in and the packet should be sent
815 * normally; a 0 return indicates that the packet has been
816 * taken over here, either now or for later transmission.
817 */
818 int
819 arpresolve(struct ifnet *ifp, struct rtentry *rt, struct mbuf *m,
820 const struct sockaddr *dst, u_char *desten)
821 {
822 struct llinfo_arp *la;
823 const struct sockaddr_dl *sdl;
824 struct mbuf *mold;
825 int s;
826
827 if ((la = arplookup1(m, &satocsin(dst)->sin_addr, 1, 0, rt)) != NULL)
828 rt = la->la_rt;
829
830 if (la == NULL || rt == NULL) {
831 ARP_STATINC(ARP_STAT_ALLOCFAIL);
832 log(LOG_DEBUG,
833 "arpresolve: can't allocate llinfo on %s for %s\n",
834 ifp->if_xname, in_fmtaddr(satocsin(dst)->sin_addr));
835 m_freem(m);
836 return 0;
837 }
838 sdl = satocsdl(rt->rt_gateway);
839 /*
840 * Check the address family and length is valid, the address
841 * is resolved; otherwise, try to resolve.
842 */
843 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
844 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
845 memcpy(desten, CLLADDR(sdl),
846 min(sdl->sdl_alen, ifp->if_addrlen));
847 rt->rt_pksent = time_second; /* Time for last pkt sent */
848 return 1;
849 }
850 /*
851 * There is an arptab entry, but no ethernet address
852 * response yet. Replace the held mbuf with this
853 * latest one.
854 */
855
856 ARP_STATINC(ARP_STAT_DFRTOTAL);
857 s = splnet();
858 mold = la->la_hold;
859 la->la_hold = m;
860 splx(s);
861
862 if (mold) {
863 ARP_STATINC(ARP_STAT_DFRDROPPED);
864 m_freem(mold);
865 }
866
867 /*
868 * Re-send the ARP request when appropriate.
869 */
870 #ifdef DIAGNOSTIC
871 if (rt->rt_expire == 0) {
872 /* This should never happen. (Should it? -gwr) */
873 printf("arpresolve: unresolved and rt_expire == 0\n");
874 /* Set expiration time to now (expired). */
875 rt->rt_expire = time_second;
876 }
877 #endif
878 if (rt->rt_expire) {
879 rt->rt_flags &= ~RTF_REJECT;
880 if (la->la_asked == 0 || rt->rt_expire != time_second) {
881 rt->rt_expire = time_second;
882 if (la->la_asked++ < arp_maxtries) {
883 arprequest(ifp,
884 &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
885 &satocsin(dst)->sin_addr,
886 #if NCARP > 0
887 (rt->rt_ifp->if_type == IFT_CARP) ?
888 CLLADDR(rt->rt_ifp->if_sadl):
889 #endif
890 CLLADDR(ifp->if_sadl));
891 } else {
892 rt->rt_flags |= RTF_REJECT;
893 rt->rt_expire += arpt_down;
894 la->la_asked = 0;
895 }
896 }
897 }
898 return 0;
899 }
900
901 /*
902 * Common length and type checks are done here,
903 * then the protocol-specific routine is called.
904 */
905 void
906 arpintr(void)
907 {
908 struct mbuf *m;
909 struct arphdr *ar;
910 int s;
911 int arplen;
912
913 mutex_enter(softnet_lock);
914 KERNEL_LOCK(1, NULL);
915 while (arpintrq.ifq_head) {
916 s = splnet();
917 IF_DEQUEUE(&arpintrq, m);
918 splx(s);
919 if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
920 panic("arpintr");
921
922 MCLAIM(m, &arpdomain.dom_mowner);
923 ARP_STATINC(ARP_STAT_RCVTOTAL);
924
925 /*
926 * First, make sure we have at least struct arphdr.
927 */
928 if (m->m_len < sizeof(struct arphdr) ||
929 (ar = mtod(m, struct arphdr *)) == NULL)
930 goto badlen;
931
932 switch (m->m_pkthdr.rcvif->if_type) {
933 case IFT_IEEE1394:
934 arplen = sizeof(struct arphdr) +
935 ar->ar_hln + 2 * ar->ar_pln;
936 break;
937 default:
938 arplen = sizeof(struct arphdr) +
939 2 * ar->ar_hln + 2 * ar->ar_pln;
940 break;
941 }
942
943 if (/* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */
944 m->m_len >= arplen)
945 switch (ntohs(ar->ar_pro)) {
946 case ETHERTYPE_IP:
947 case ETHERTYPE_IPTRAILERS:
948 in_arpinput(m);
949 continue;
950 default:
951 ARP_STATINC(ARP_STAT_RCVBADPROTO);
952 }
953 else {
954 badlen:
955 ARP_STATINC(ARP_STAT_RCVBADLEN);
956 }
957 m_freem(m);
958 }
959 KERNEL_UNLOCK_ONE(NULL);
960 mutex_exit(softnet_lock);
961 }
962
963 /*
964 * ARP for Internet protocols on 10 Mb/s Ethernet.
965 * Algorithm is that given in RFC 826.
966 * In addition, a sanity check is performed on the sender
967 * protocol address, to catch impersonators.
968 * We no longer handle negotiations for use of trailer protocol:
969 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
970 * along with IP replies if we wanted trailers sent to us,
971 * and also sent them in response to IP replies.
972 * This allowed either end to announce the desire to receive
973 * trailer packets.
974 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
975 * but formerly didn't normally send requests.
976 */
977 static void
978 in_arpinput(struct mbuf *m)
979 {
980 struct arphdr *ah;
981 struct ifnet *ifp = m->m_pkthdr.rcvif;
982 struct llinfo_arp *la = NULL;
983 struct rtentry *rt;
984 struct in_ifaddr *ia;
985 #if NBRIDGE > 0
986 struct in_ifaddr *bridge_ia = NULL;
987 #endif
988 #if NCARP > 0
989 u_int32_t count = 0, index = 0;
990 #endif
991 struct sockaddr_dl *sdl;
992 struct sockaddr sa;
993 struct in_addr isaddr, itaddr, myaddr;
994 int op;
995 struct mbuf *mold;
996 void *tha;
997 int s;
998 uint64_t *arps;
999
1000 if (__predict_false(m_makewritable(&m, 0, m->m_pkthdr.len, M_DONTWAIT)))
1001 goto out;
1002 ah = mtod(m, struct arphdr *);
1003 op = ntohs(ah->ar_op);
1004
1005 /*
1006 * Fix up ah->ar_hrd if necessary, before using ar_tha() or
1007 * ar_tpa().
1008 */
1009 switch (ifp->if_type) {
1010 case IFT_IEEE1394:
1011 if (ntohs(ah->ar_hrd) == ARPHRD_IEEE1394)
1012 ;
1013 else {
1014 /* XXX this is to make sure we compute ar_tha right */
1015 /* XXX check ar_hrd more strictly? */
1016 ah->ar_hrd = htons(ARPHRD_IEEE1394);
1017 }
1018 break;
1019 default:
1020 /* XXX check ar_hrd? */
1021 break;
1022 }
1023
1024 memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
1025 memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
1026
1027 if (m->m_flags & (M_BCAST|M_MCAST))
1028 ARP_STATINC(ARP_STAT_RCVMCAST);
1029
1030
1031 /*
1032 * Search for a matching interface address
1033 * or any address on the interface to use
1034 * as a dummy address in the rest of this function
1035 */
1036
1037 INADDR_TO_IA(itaddr, ia);
1038 while (ia != NULL) {
1039 #if NCARP > 0
1040 if (ia->ia_ifp->if_type == IFT_CARP &&
1041 ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
1042 (IFF_UP|IFF_RUNNING))) {
1043 index++;
1044 if (ia->ia_ifp == m->m_pkthdr.rcvif &&
1045 carp_iamatch(ia, ar_sha(ah),
1046 &count, index)) {
1047 break;
1048 }
1049 } else
1050 #endif
1051 if (ia->ia_ifp == m->m_pkthdr.rcvif)
1052 break;
1053 #if NBRIDGE > 0
1054 /*
1055 * If the interface we received the packet on
1056 * is part of a bridge, check to see if we need
1057 * to "bridge" the packet to ourselves at this
1058 * layer. Note we still prefer a perfect match,
1059 * but allow this weaker match if necessary.
1060 */
1061 if (m->m_pkthdr.rcvif->if_bridge != NULL &&
1062 m->m_pkthdr.rcvif->if_bridge == ia->ia_ifp->if_bridge)
1063 bridge_ia = ia;
1064 #endif /* NBRIDGE > 0 */
1065
1066 NEXT_IA_WITH_SAME_ADDR(ia);
1067 }
1068
1069 #if NBRIDGE > 0
1070 if (ia == NULL && bridge_ia != NULL) {
1071 ia = bridge_ia;
1072 ifp = bridge_ia->ia_ifp;
1073 }
1074 #endif
1075
1076 if (ia == NULL) {
1077 INADDR_TO_IA(isaddr, ia);
1078 while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif)
1079 NEXT_IA_WITH_SAME_ADDR(ia);
1080
1081 if (ia == NULL) {
1082 IFP_TO_IA(ifp, ia);
1083 if (ia == NULL) {
1084 ARP_STATINC(ARP_STAT_RCVNOINT);
1085 goto out;
1086 }
1087 }
1088 }
1089
1090 myaddr = ia->ia_addr.sin_addr;
1091
1092 /* XXX checks for bridge case? */
1093 if (!memcmp(ar_sha(ah), CLLADDR(ifp->if_sadl), ifp->if_addrlen)) {
1094 ARP_STATINC(ARP_STAT_RCVLOCALSHA);
1095 goto out; /* it's from me, ignore it. */
1096 }
1097
1098 /* XXX checks for bridge case? */
1099 if (!memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
1100 ARP_STATINC(ARP_STAT_RCVBCASTSHA);
1101 log(LOG_ERR,
1102 "%s: arp: link address is broadcast for IP address %s!\n",
1103 ifp->if_xname, in_fmtaddr(isaddr));
1104 goto out;
1105 }
1106
1107 /*
1108 * If the source IP address is zero, this is an RFC 5227 ARP probe
1109 */
1110 if (in_nullhost(isaddr))
1111 ARP_STATINC(ARP_STAT_RCVZEROSPA);
1112 else if (in_hosteq(isaddr, myaddr))
1113 ARP_STATINC(ARP_STAT_RCVLOCALSPA);
1114
1115 if (in_nullhost(itaddr))
1116 ARP_STATINC(ARP_STAT_RCVZEROTPA);
1117
1118 /* DAD check, RFC 5227 2.1.1, Probe Details */
1119 if (in_hosteq(isaddr, myaddr) ||
1120 (in_nullhost(isaddr) && in_hosteq(itaddr, myaddr)))
1121 {
1122 /* If our address is tentative, mark it as duplicated */
1123 if (ia->ia4_flags & IN_IFF_TENTATIVE)
1124 arp_dad_duplicated((struct ifaddr *)ia);
1125 /* If our address is unuseable, don't reply */
1126 if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED))
1127 goto out;
1128 }
1129
1130 /*
1131 * If the target IP address is zero, ignore the packet.
1132 * This prevents the code below from tring to answer
1133 * when we are using IP address zero (booting).
1134 */
1135 if (in_nullhost(itaddr))
1136 goto out;
1137
1138 if (in_nullhost(isaddr))
1139 goto reply;
1140
1141 if (in_hosteq(isaddr, myaddr)) {
1142 log(LOG_ERR,
1143 "duplicate IP address %s sent from link address %s\n",
1144 in_fmtaddr(isaddr), lla_snprintf(ar_sha(ah), ah->ar_hln));
1145 itaddr = myaddr;
1146 goto reply;
1147 }
1148 la = arplookup(m, &isaddr, in_hosteq(itaddr, myaddr), 0);
1149 if (la != NULL && (rt = la->la_rt) && (sdl = satosdl(rt->rt_gateway))) {
1150 if (sdl->sdl_alen &&
1151 memcmp(ar_sha(ah), CLLADDR(sdl), sdl->sdl_alen)) {
1152 if (rt->rt_flags & RTF_STATIC) {
1153 ARP_STATINC(ARP_STAT_RCVOVERPERM);
1154 if (!log_permanent_modify)
1155 goto out;
1156 log(LOG_INFO,
1157 "%s tried to overwrite permanent arp info"
1158 " for %s\n",
1159 lla_snprintf(ar_sha(ah), ah->ar_hln),
1160 in_fmtaddr(isaddr));
1161 goto out;
1162 } else if (rt->rt_ifp != ifp) {
1163 ARP_STATINC(ARP_STAT_RCVOVERINT);
1164 if (!log_wrong_iface)
1165 goto out;
1166 log(LOG_INFO,
1167 "%s on %s tried to overwrite "
1168 "arp info for %s on %s\n",
1169 lla_snprintf(ar_sha(ah), ah->ar_hln),
1170 ifp->if_xname, in_fmtaddr(isaddr),
1171 rt->rt_ifp->if_xname);
1172 goto out;
1173 } else {
1174 ARP_STATINC(ARP_STAT_RCVOVER);
1175 if (log_movements)
1176 log(LOG_INFO, "arp info overwritten "
1177 "for %s by %s\n",
1178 in_fmtaddr(isaddr),
1179 lla_snprintf(ar_sha(ah),
1180 ah->ar_hln));
1181 }
1182 }
1183 /*
1184 * sanity check for the address length.
1185 * XXX this does not work for protocols with variable address
1186 * length. -is
1187 */
1188 if (sdl->sdl_alen &&
1189 sdl->sdl_alen != ah->ar_hln) {
1190 ARP_STATINC(ARP_STAT_RCVLENCHG);
1191 log(LOG_WARNING,
1192 "arp from %s: new addr len %d, was %d\n",
1193 in_fmtaddr(isaddr), ah->ar_hln, sdl->sdl_alen);
1194 }
1195 if (ifp->if_addrlen != ah->ar_hln) {
1196 ARP_STATINC(ARP_STAT_RCVBADLEN);
1197 log(LOG_WARNING,
1198 "arp from %s: addr len: new %d, i/f %d (ignored)\n",
1199 in_fmtaddr(isaddr), ah->ar_hln,
1200 ifp->if_addrlen);
1201 goto reply;
1202 }
1203 #if NTOKEN > 0
1204 /*
1205 * XXX uses m_data and assumes the complete answer including
1206 * XXX token-ring headers is in the same buf
1207 */
1208 if (ifp->if_type == IFT_ISO88025) {
1209 struct token_header *trh;
1210
1211 trh = (struct token_header *)M_TRHSTART(m);
1212 if (trh->token_shost[0] & TOKEN_RI_PRESENT) {
1213 struct token_rif *rif;
1214 size_t riflen;
1215
1216 rif = TOKEN_RIF(trh);
1217 riflen = (ntohs(rif->tr_rcf) &
1218 TOKEN_RCF_LEN_MASK) >> 8;
1219
1220 if (riflen > 2 &&
1221 riflen < sizeof(struct token_rif) &&
1222 (riflen & 1) == 0) {
1223 rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION);
1224 rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK);
1225 memcpy(TOKEN_RIF(la), rif, riflen);
1226 }
1227 }
1228 }
1229 #endif /* NTOKEN > 0 */
1230 (void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, ar_sha(ah),
1231 ah->ar_hln);
1232 if (rt->rt_expire)
1233 rt->rt_expire = time_second + arpt_keep;
1234 rt->rt_flags &= ~RTF_REJECT;
1235 la->la_asked = 0;
1236
1237 s = splnet();
1238 mold = la->la_hold;
1239 la->la_hold = 0;
1240 splx(s);
1241
1242 if (mold) {
1243 ARP_STATINC(ARP_STAT_DFRSENT);
1244 (*ifp->if_output)(ifp, mold, rt_getkey(rt), rt);
1245 }
1246 }
1247 reply:
1248 if (op != ARPOP_REQUEST) {
1249 if (op == ARPOP_REPLY)
1250 ARP_STATINC(ARP_STAT_RCVREPLY);
1251 out:
1252 m_freem(m);
1253 return;
1254 }
1255 ARP_STATINC(ARP_STAT_RCVREQUEST);
1256 if (in_hosteq(itaddr, myaddr)) {
1257 /* If our address is unuseable, don't reply */
1258 if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED))
1259 goto out;
1260 /* I am the target */
1261 tha = ar_tha(ah);
1262 if (tha)
1263 memcpy(tha, ar_sha(ah), ah->ar_hln);
1264 memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
1265 } else {
1266 la = arplookup(m, &itaddr, 0, SIN_PROXY);
1267 if (la == NULL)
1268 goto out;
1269 rt = la->la_rt;
1270 if (rt->rt_ifp->if_type == IFT_CARP &&
1271 m->m_pkthdr.rcvif->if_type != IFT_CARP)
1272 goto out;
1273 tha = ar_tha(ah);
1274 if (tha)
1275 memcpy(tha, ar_sha(ah), ah->ar_hln);
1276 sdl = satosdl(rt->rt_gateway);
1277 memcpy(ar_sha(ah), CLLADDR(sdl), ah->ar_hln);
1278 }
1279
1280 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
1281 memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
1282 ah->ar_op = htons(ARPOP_REPLY);
1283 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1284 switch (ifp->if_type) {
1285 case IFT_IEEE1394:
1286 /*
1287 * ieee1394 arp reply is broadcast
1288 */
1289 m->m_flags &= ~M_MCAST;
1290 m->m_flags |= M_BCAST;
1291 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + ah->ar_hln;
1292 break;
1293
1294 default:
1295 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
1296 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
1297 break;
1298 }
1299 m->m_pkthdr.len = m->m_len;
1300 sa.sa_family = AF_ARP;
1301 sa.sa_len = 2;
1302 arps = ARP_STAT_GETREF();
1303 arps[ARP_STAT_SNDTOTAL]++;
1304 arps[ARP_STAT_SNDREPLY]++;
1305 ARP_STAT_PUTREF();
1306 (*ifp->if_output)(ifp, m, &sa, NULL);
1307 return;
1308 }
1309
1310 /*
1311 * Free an arp entry.
1312 */
1313 static void arptfree(struct llinfo_arp *la)
1314 {
1315 struct rtentry *rt = la->la_rt;
1316 struct sockaddr_dl *sdl;
1317
1318 ARP_LOCK_CHECK();
1319
1320 if (rt == NULL)
1321 panic("arptfree");
1322 if (rt->rt_refcnt > 0 && (sdl = satosdl(rt->rt_gateway)) &&
1323 sdl->sdl_family == AF_LINK) {
1324 sdl->sdl_alen = 0;
1325 la->la_asked = 0;
1326 rt->rt_flags &= ~RTF_REJECT;
1327 return;
1328 }
1329 rtrequest(RTM_DELETE, rt_getkey(rt), NULL, rt_mask(rt), 0, NULL);
1330 }
1331
1332 static struct llinfo_arp *
1333 arplookup(struct mbuf *m, const struct in_addr *addr, int create, int proxy)
1334 {
1335 return arplookup1(m, addr, create, proxy, NULL);
1336 }
1337
1338 /*
1339 * Lookup or enter a new address in arptab.
1340 */
1341 static struct llinfo_arp *
1342 arplookup1(struct mbuf *m, const struct in_addr *addr, int create, int proxy,
1343 struct rtentry *rt0)
1344 {
1345 struct arphdr *ah;
1346 struct ifnet *ifp = m->m_pkthdr.rcvif;
1347 struct rtentry *rt;
1348 struct sockaddr_inarp sin;
1349 const char *why = NULL;
1350
1351 ah = mtod(m, struct arphdr *);
1352 if (rt0 == NULL) {
1353 memset(&sin, 0, sizeof(sin));
1354 sin.sin_len = sizeof(sin);
1355 sin.sin_family = AF_INET;
1356 sin.sin_addr = *addr;
1357 sin.sin_other = proxy ? SIN_PROXY : 0;
1358 rt = rtalloc1(sintosa(&sin), create);
1359 if (rt == NULL)
1360 return NULL;
1361 rt->rt_refcnt--;
1362 } else
1363 rt = rt0;
1364
1365 #define IS_LLINFO(__rt) \
1366 (((__rt)->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) == RTF_LLINFO && \
1367 (__rt)->rt_gateway->sa_family == AF_LINK)
1368
1369
1370 if (IS_LLINFO(rt))
1371 return (struct llinfo_arp *)rt->rt_llinfo;
1372
1373 if (create) {
1374 if (rt->rt_flags & RTF_GATEWAY) {
1375 if (log_unknown_network)
1376 why = "host is not on local network";
1377 } else if ((rt->rt_flags & RTF_LLINFO) == 0) {
1378 ARP_STATINC(ARP_STAT_ALLOCFAIL);
1379 why = "could not allocate llinfo";
1380 } else
1381 why = "gateway route is not ours";
1382 if (why) {
1383 log(LOG_DEBUG, "arplookup: unable to enter address"
1384 " for %s@%s on %s (%s)\n", in_fmtaddr(*addr),
1385 lla_snprintf(ar_sha(ah), ah->ar_hln),
1386 (ifp) ? ifp->if_xname : "null", why);
1387 }
1388 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_CLONED) != 0) {
1389 rtrequest(RTM_DELETE, rt_getkey(rt),
1390 rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
1391 }
1392 }
1393 return NULL;
1394 }
1395
1396 int
1397 arpioctl(u_long cmd, void *data)
1398 {
1399
1400 return EOPNOTSUPP;
1401 }
1402
1403 void
1404 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1405 {
1406 struct in_addr *ip;
1407 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1408
1409 /*
1410 * Warn the user if another station has this IP address,
1411 * but only if the interface IP address is not zero.
1412 */
1413 ip = &IA_SIN(ifa)->sin_addr;
1414 if (!in_nullhost(*ip) &&
1415 (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED)) == 0)
1416 arprequest(ifp, ip, ip, CLLADDR(ifp->if_sadl));
1417
1418 ifa->ifa_rtrequest = arp_rtrequest;
1419 ifa->ifa_flags |= RTF_CLONING;
1420 }
1421
1422 TAILQ_HEAD(dadq_head, dadq);
1423 struct dadq {
1424 TAILQ_ENTRY(dadq) dad_list;
1425 struct ifaddr *dad_ifa;
1426 int dad_count; /* max ARP to send */
1427 int dad_arp_tcount; /* # of trials to send ARP */
1428 int dad_arp_ocount; /* ARP sent so far */
1429 int dad_arp_announce; /* max ARP announcements */
1430 int dad_arp_acount; /* # of announcements */
1431 struct callout dad_timer_ch;
1432 };
1433 MALLOC_JUSTDEFINE(M_IPARP, "ARP DAD", "ARP DAD Structure");
1434
1435 static struct dadq_head dadq;
1436 static int dad_init = 0;
1437 static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */
1438
1439 static struct dadq *
1440 arp_dad_find(struct ifaddr *ifa)
1441 {
1442 struct dadq *dp;
1443
1444 TAILQ_FOREACH(dp, &dadq, dad_list) {
1445 if (dp->dad_ifa == ifa)
1446 return dp;
1447 }
1448 return NULL;
1449 }
1450
1451 static void
1452 arp_dad_starttimer(struct dadq *dp, int ticks)
1453 {
1454
1455 callout_reset(&dp->dad_timer_ch, ticks,
1456 (void (*)(void *))arp_dad_timer, (void *)dp->dad_ifa);
1457 }
1458
1459 static void
1460 arp_dad_stoptimer(struct dadq *dp)
1461 {
1462
1463 callout_stop(&dp->dad_timer_ch);
1464 }
1465
1466 static void
1467 arp_dad_output(struct dadq *dp, struct ifaddr *ifa)
1468 {
1469 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1470 struct ifnet *ifp = ifa->ifa_ifp;
1471 struct in_addr sip;
1472
1473 dp->dad_arp_tcount++;
1474 if ((ifp->if_flags & IFF_UP) == 0)
1475 return;
1476 if ((ifp->if_flags & IFF_RUNNING) == 0)
1477 return;
1478
1479 dp->dad_arp_tcount = 0;
1480 dp->dad_arp_ocount++;
1481
1482 memset(&sip, 0, sizeof(sip));
1483 arprequest(ifa->ifa_ifp, &sip, &ia->ia_addr.sin_addr,
1484 CLLADDR(ifa->ifa_ifp->if_sadl));
1485 }
1486
1487 /*
1488 * Start Duplicate Address Detection (DAD) for specified interface address.
1489 */
1490 void
1491 arp_dad_start(struct ifaddr *ifa)
1492 {
1493 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1494 struct dadq *dp;
1495
1496 if (!dad_init) {
1497 TAILQ_INIT(&dadq);
1498 dad_init++;
1499 }
1500
1501 /*
1502 * If we don't need DAD, don't do it.
1503 * - DAD is disabled (ip_dad_count == 0)
1504 */
1505 if (!(ia->ia4_flags & IN_IFF_TENTATIVE)) {
1506 log(LOG_DEBUG,
1507 "arp_dad_start: called with non-tentative address "
1508 "%s(%s)\n",
1509 in_fmtaddr(ia->ia_addr.sin_addr),
1510 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1511 return;
1512 }
1513 if (!ip_dad_count) {
1514 struct in_addr *ip = &IA_SIN(ifa)->sin_addr;
1515
1516 ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1517 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1518 arprequest(ifa->ifa_ifp, ip, ip,
1519 CLLADDR(ifa->ifa_ifp->if_sadl));
1520 return;
1521 }
1522 if (ifa->ifa_ifp == NULL)
1523 panic("arp_dad_start: ifa->ifa_ifp == NULL");
1524 if (!(ifa->ifa_ifp->if_flags & IFF_UP))
1525 return;
1526 if (arp_dad_find(ifa) != NULL) {
1527 /* DAD already in progress */
1528 return;
1529 }
1530
1531 dp = malloc(sizeof(*dp), M_IPARP, M_NOWAIT);
1532 if (dp == NULL) {
1533 log(LOG_ERR, "arp_dad_start: memory allocation failed for "
1534 "%s(%s)\n",
1535 in_fmtaddr(ia->ia_addr.sin_addr),
1536 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1537 return;
1538 }
1539 memset(dp, 0, sizeof(*dp));
1540 callout_init(&dp->dad_timer_ch, CALLOUT_MPSAFE);
1541 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1542
1543 arplog((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1544 in_fmtaddr(ia->ia_addr.sin_addr)));
1545
1546 /*
1547 * Send ARP packet for DAD, ip_dad_count times.
1548 * Note that we must delay the first transmission.
1549 */
1550 dp->dad_ifa = ifa;
1551 ifaref(ifa); /* just for safety */
1552 dp->dad_count = ip_dad_count;
1553 dp->dad_arp_announce = 0; /* Will be set when starting to announce */
1554 dp->dad_arp_acount = dp->dad_arp_ocount = dp->dad_arp_tcount = 0;
1555
1556 arp_dad_starttimer(dp, cprng_fast32() % (PROBE_WAIT * hz));
1557 }
1558
1559 /*
1560 * terminate DAD unconditionally. used for address removals.
1561 */
1562 void
1563 arp_dad_stop(struct ifaddr *ifa)
1564 {
1565 struct dadq *dp;
1566
1567 if (!dad_init)
1568 return;
1569 dp = arp_dad_find(ifa);
1570 if (dp == NULL) {
1571 /* DAD wasn't started yet */
1572 return;
1573 }
1574
1575 arp_dad_stoptimer(dp);
1576
1577 TAILQ_REMOVE(&dadq, dp, dad_list);
1578 free(dp, M_IPARP);
1579 dp = NULL;
1580 ifafree(ifa);
1581 }
1582
1583 static void
1584 arp_dad_timer(struct ifaddr *ifa)
1585 {
1586 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1587 struct dadq *dp;
1588 struct in_addr *ip;
1589
1590 mutex_enter(softnet_lock);
1591 KERNEL_LOCK(1, NULL);
1592
1593 /* Sanity check */
1594 if (ia == NULL) {
1595 log(LOG_ERR, "arp_dad_timer: called with null parameter\n");
1596 goto done;
1597 }
1598 dp = arp_dad_find(ifa);
1599 if (dp == NULL) {
1600 log(LOG_ERR, "arp_dad_timer: DAD structure not found\n");
1601 goto done;
1602 }
1603 if (ia->ia4_flags & IN_IFF_DUPLICATED) {
1604 log(LOG_ERR, "nd4_dad_timer: called with duplicate address "
1605 "%s(%s)\n",
1606 in_fmtaddr(ia->ia_addr.sin_addr),
1607 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1608 goto done;
1609 }
1610 if ((ia->ia4_flags & IN_IFF_TENTATIVE) == 0 && dp->dad_arp_acount == 0){
1611 log(LOG_ERR, "arp_dad_timer: called with non-tentative address "
1612 "%s(%s)\n",
1613 in_fmtaddr(ia->ia_addr.sin_addr),
1614 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1615 goto done;
1616 }
1617
1618 /* timeouted with IFF_{RUNNING,UP} check */
1619 if (dp->dad_arp_tcount > dad_maxtry) {
1620 arplog((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1621 if_name(ifa->ifa_ifp)));
1622
1623 TAILQ_REMOVE(&dadq, dp, dad_list);
1624 free(dp, M_IPARP);
1625 dp = NULL;
1626 ifafree(ifa);
1627 goto done;
1628 }
1629
1630 /* Need more checks? */
1631 if (dp->dad_arp_ocount < dp->dad_count) {
1632 int delay;
1633
1634 /*
1635 * We have more ARP to go. Send ARP packet for DAD.
1636 */
1637 arp_dad_output(dp, ifa);
1638 if (dp->dad_arp_ocount < dp->dad_count)
1639 delay = (PROBE_MIN * hz) +
1640 (cprng_fast32() %
1641 ((PROBE_MAX * hz) - (PROBE_MIN * hz)));
1642 else
1643 delay = ANNOUNCE_WAIT * hz;
1644 arp_dad_starttimer(dp, delay);
1645 goto done;
1646 } else if (dp->dad_arp_acount == 0) {
1647 /*
1648 * We are done with DAD.
1649 * No duplicate address found.
1650 */
1651 ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1652 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1653 arplog((LOG_DEBUG,
1654 "%s: DAD complete for %s - no duplicates found\n",
1655 if_name(ifa->ifa_ifp),
1656 in_fmtaddr(ia->ia_addr.sin_addr)));
1657 dp->dad_arp_announce = ANNOUNCE_NUM;
1658 goto announce;
1659 } else if (dp->dad_arp_acount < dp->dad_arp_announce) {
1660 announce:
1661 /*
1662 * Announce the address.
1663 */
1664 ip = &IA_SIN(ifa)->sin_addr;
1665 arprequest(ifa->ifa_ifp, ip, ip,
1666 CLLADDR(ifa->ifa_ifp->if_sadl));
1667 dp->dad_arp_acount++;
1668 if (dp->dad_arp_acount < dp->dad_arp_announce) {
1669 arp_dad_starttimer(dp, ANNOUNCE_INTERVAL * hz);
1670 goto done;
1671 }
1672 arplog((LOG_DEBUG,
1673 "%s: ARP announcement complete for %s\n",
1674 if_name(ifa->ifa_ifp),
1675 in_fmtaddr(ia->ia_addr.sin_addr)));
1676 }
1677
1678 TAILQ_REMOVE(&dadq, dp, dad_list);
1679 free(dp, M_IPARP);
1680 dp = NULL;
1681 ifafree(ifa);
1682
1683 done:
1684 KERNEL_UNLOCK_ONE(NULL);
1685 mutex_exit(softnet_lock);
1686 }
1687
1688 static void
1689 arp_dad_duplicated(struct ifaddr *ifa)
1690 {
1691 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1692 struct ifnet *ifp;
1693 struct dadq *dp;
1694
1695 dp = arp_dad_find(ifa);
1696 if (dp == NULL) {
1697 log(LOG_ERR, "arp_dad_duplicated: DAD structure not found\n");
1698 return;
1699 }
1700
1701 ifp = ifa->ifa_ifp;
1702 log(LOG_ERR, "%s: DAD detected duplicate IPv4 address %s: "
1703 "ARP out=%d\n",
1704 if_name(ifp), in_fmtaddr(ia->ia_addr.sin_addr),
1705 dp->dad_arp_ocount);
1706
1707 ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1708 ia->ia4_flags |= IN_IFF_DUPLICATED;
1709
1710 /* We are done with DAD, with duplicated address found. (failure) */
1711 arp_dad_stoptimer(dp);
1712
1713 /* Inform the routing socket that DAD has completed */
1714 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1715
1716 TAILQ_REMOVE(&dadq, dp, dad_list);
1717 free(dp, M_IPARP);
1718 dp = NULL;
1719 ifafree(ifa);
1720 }
1721
1722 /*
1723 * Called from 10 Mb/s Ethernet interrupt handlers
1724 * when ether packet type ETHERTYPE_REVARP
1725 * is received. Common length and type checks are done here,
1726 * then the protocol-specific routine is called.
1727 */
1728 void
1729 revarpinput(struct mbuf *m)
1730 {
1731 struct arphdr *ar;
1732
1733 if (m->m_len < sizeof(struct arphdr))
1734 goto out;
1735 ar = mtod(m, struct arphdr *);
1736 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
1737 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
1738 goto out;
1739 #endif
1740 if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
1741 goto out;
1742 switch (ntohs(ar->ar_pro)) {
1743 case ETHERTYPE_IP:
1744 case ETHERTYPE_IPTRAILERS:
1745 in_revarpinput(m);
1746 return;
1747
1748 default:
1749 break;
1750 }
1751 out:
1752 m_freem(m);
1753 }
1754
1755 /*
1756 * RARP for Internet protocols on 10 Mb/s Ethernet.
1757 * Algorithm is that given in RFC 903.
1758 * We are only using for bootstrap purposes to get an ip address for one of
1759 * our interfaces. Thus we support no user-interface.
1760 *
1761 * Since the contents of the RARP reply are specific to the interface that
1762 * sent the request, this code must ensure that they are properly associated.
1763 *
1764 * Note: also supports ARP via RARP packets, per the RFC.
1765 */
1766 void
1767 in_revarpinput(struct mbuf *m)
1768 {
1769 struct ifnet *ifp;
1770 struct arphdr *ah;
1771 void *tha;
1772 int op;
1773
1774 ah = mtod(m, struct arphdr *);
1775 op = ntohs(ah->ar_op);
1776
1777 switch (m->m_pkthdr.rcvif->if_type) {
1778 case IFT_IEEE1394:
1779 /* ARP without target hardware address is not supported */
1780 goto out;
1781 default:
1782 break;
1783 }
1784
1785 switch (op) {
1786 case ARPOP_REQUEST:
1787 case ARPOP_REPLY: /* per RFC */
1788 in_arpinput(m);
1789 return;
1790 case ARPOP_REVREPLY:
1791 break;
1792 case ARPOP_REVREQUEST: /* handled by rarpd(8) */
1793 default:
1794 goto out;
1795 }
1796 if (!revarp_in_progress)
1797 goto out;
1798 ifp = m->m_pkthdr.rcvif;
1799 if (ifp != myip_ifp) /* !same interface */
1800 goto out;
1801 if (myip_initialized)
1802 goto wake;
1803 tha = ar_tha(ah);
1804 if (tha == NULL)
1805 goto out;
1806 if (memcmp(tha, CLLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen))
1807 goto out;
1808 memcpy(&srv_ip, ar_spa(ah), sizeof(srv_ip));
1809 memcpy(&myip, ar_tpa(ah), sizeof(myip));
1810 myip_initialized = 1;
1811 wake: /* Do wakeup every time in case it was missed. */
1812 wakeup((void *)&myip);
1813
1814 out:
1815 m_freem(m);
1816 }
1817
1818 /*
1819 * Send a RARP request for the ip address of the specified interface.
1820 * The request should be RFC 903-compliant.
1821 */
1822 void
1823 revarprequest(struct ifnet *ifp)
1824 {
1825 struct sockaddr sa;
1826 struct mbuf *m;
1827 struct arphdr *ah;
1828 void *tha;
1829
1830 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
1831 return;
1832 MCLAIM(m, &arpdomain.dom_mowner);
1833 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
1834 2*ifp->if_addrlen;
1835 m->m_pkthdr.len = m->m_len;
1836 MH_ALIGN(m, m->m_len);
1837 ah = mtod(m, struct arphdr *);
1838 memset(ah, 0, m->m_len);
1839 ah->ar_pro = htons(ETHERTYPE_IP);
1840 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
1841 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
1842 ah->ar_op = htons(ARPOP_REVREQUEST);
1843
1844 memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
1845 tha = ar_tha(ah);
1846 if (tha == NULL) {
1847 m_free(m);
1848 return;
1849 }
1850 memcpy(tha, CLLADDR(ifp->if_sadl), ah->ar_hln);
1851
1852 sa.sa_family = AF_ARP;
1853 sa.sa_len = 2;
1854 m->m_flags |= M_BCAST;
1855
1856 KERNEL_LOCK(1, NULL);
1857 (*ifp->if_output)(ifp, m, &sa, NULL);
1858 KERNEL_UNLOCK_ONE(NULL);
1859 }
1860
1861 /*
1862 * RARP for the ip address of the specified interface, but also
1863 * save the ip address of the server that sent the answer.
1864 * Timeout if no response is received.
1865 */
1866 int
1867 revarpwhoarewe(struct ifnet *ifp, struct in_addr *serv_in,
1868 struct in_addr *clnt_in)
1869 {
1870 int result, count = 20;
1871
1872 myip_initialized = 0;
1873 myip_ifp = ifp;
1874
1875 revarp_in_progress = 1;
1876 while (count--) {
1877 revarprequest(ifp);
1878 result = tsleep((void *)&myip, PSOCK, "revarp", hz/2);
1879 if (result != EWOULDBLOCK)
1880 break;
1881 }
1882 revarp_in_progress = 0;
1883
1884 if (!myip_initialized)
1885 return ENETUNREACH;
1886
1887 memcpy(serv_in, &srv_ip, sizeof(*serv_in));
1888 memcpy(clnt_in, &myip, sizeof(*clnt_in));
1889 return 0;
1890 }
1891
1892
1893
1895 #ifdef DDB
1896
1897 #include <machine/db_machdep.h>
1898 #include <ddb/db_interface.h>
1899 #include <ddb/db_output.h>
1900
1901 static void
1902 db_print_sa(const struct sockaddr *sa)
1903 {
1904 int len;
1905 const u_char *p;
1906
1907 if (sa == NULL) {
1908 db_printf("[NULL]");
1909 return;
1910 }
1911
1912 p = (const u_char *)sa;
1913 len = sa->sa_len;
1914 db_printf("[");
1915 while (len > 0) {
1916 db_printf("%d", *p);
1917 p++; len--;
1918 if (len) db_printf(",");
1919 }
1920 db_printf("]\n");
1921 }
1922
1923 static void
1924 db_print_ifa(struct ifaddr *ifa)
1925 {
1926 if (ifa == NULL)
1927 return;
1928 db_printf(" ifa_addr=");
1929 db_print_sa(ifa->ifa_addr);
1930 db_printf(" ifa_dsta=");
1931 db_print_sa(ifa->ifa_dstaddr);
1932 db_printf(" ifa_mask=");
1933 db_print_sa(ifa->ifa_netmask);
1934 db_printf(" flags=0x%x,refcnt=%d,metric=%d\n",
1935 ifa->ifa_flags,
1936 ifa->ifa_refcnt,
1937 ifa->ifa_metric);
1938 }
1939
1940 static void
1941 db_print_llinfo(void *li)
1942 {
1943 struct llinfo_arp *la;
1944
1945 if (li == NULL)
1946 return;
1947 la = (struct llinfo_arp *)li;
1948 db_printf(" la_rt=%p la_hold=%p, la_asked=0x%lx\n",
1949 la->la_rt, la->la_hold, la->la_asked);
1950 }
1951
1952 /*
1953 * Function to pass to rt_walktree().
1954 * Return non-zero error to abort walk.
1955 */
1956 static int
1957 db_show_rtentry(struct rtentry *rt, void *w)
1958 {
1959 db_printf("rtentry=%p", rt);
1960
1961 db_printf(" flags=0x%x refcnt=%d use=%"PRId64" expire=%"PRId64"\n",
1962 rt->rt_flags, rt->rt_refcnt,
1963 rt->rt_use, (uint64_t)rt->rt_expire);
1964
1965 db_printf(" key="); db_print_sa(rt_getkey(rt));
1966 db_printf(" mask="); db_print_sa(rt_mask(rt));
1967 db_printf(" gw="); db_print_sa(rt->rt_gateway);
1968
1969 db_printf(" ifp=%p ", rt->rt_ifp);
1970 if (rt->rt_ifp)
1971 db_printf("(%s)", rt->rt_ifp->if_xname);
1972 else
1973 db_printf("(NULL)");
1974
1975 db_printf(" ifa=%p\n", rt->rt_ifa);
1976 db_print_ifa(rt->rt_ifa);
1977
1978 db_printf(" gwroute=%p llinfo=%p\n",
1979 rt->rt_gwroute, rt->rt_llinfo);
1980 db_print_llinfo(rt->rt_llinfo);
1981
1982 return 0;
1983 }
1984
1985 /*
1986 * Function to print all the route trees.
1987 * Use this from ddb: "show arptab"
1988 */
1989 void
1990 db_show_arptab(db_expr_t addr, bool have_addr,
1991 db_expr_t count, const char *modif)
1992 {
1993 rt_walktree(AF_INET, db_show_rtentry, NULL);
1994 }
1995 #endif
1996
1997 static int
1998 sysctl_net_inet_arp_stats(SYSCTLFN_ARGS)
1999 {
2000
2001 return NETSTAT_SYSCTL(arpstat_percpu, ARP_NSTATS);
2002 }
2003
2004 static void
2005 sysctl_net_inet_arp_setup(struct sysctllog **clog)
2006 {
2007 const struct sysctlnode *node;
2008
2009 sysctl_createv(clog, 0, NULL, NULL,
2010 CTLFLAG_PERMANENT,
2011 CTLTYPE_NODE, "inet", NULL,
2012 NULL, 0, NULL, 0,
2013 CTL_NET, PF_INET, CTL_EOL);
2014 sysctl_createv(clog, 0, NULL, &node,
2015 CTLFLAG_PERMANENT,
2016 CTLTYPE_NODE, "arp",
2017 SYSCTL_DESCR("Address Resolution Protocol"),
2018 NULL, 0, NULL, 0,
2019 CTL_NET, PF_INET, CTL_CREATE, CTL_EOL);
2020
2021 sysctl_createv(clog, 0, NULL, NULL,
2022 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2023 CTLTYPE_INT, "prune",
2024 SYSCTL_DESCR("ARP cache pruning interval in seconds"),
2025 NULL, 0, &arpt_prune, 0,
2026 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2027
2028 sysctl_createv(clog, 0, NULL, NULL,
2029 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2030 CTLTYPE_INT, "keep",
2031 SYSCTL_DESCR("Valid ARP entry lifetime in seconds"),
2032 NULL, 0, &arpt_keep, 0,
2033 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2034
2035 sysctl_createv(clog, 0, NULL, NULL,
2036 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2037 CTLTYPE_INT, "down",
2038 SYSCTL_DESCR("Failed ARP entry lifetime in seconds"),
2039 NULL, 0, &arpt_down, 0,
2040 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2041
2042 sysctl_createv(clog, 0, NULL, NULL,
2043 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2044 CTLTYPE_INT, "refresh",
2045 SYSCTL_DESCR("ARP entry refresh interval"),
2046 NULL, 0, &arpt_refresh, 0,
2047 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2048
2049 sysctl_createv(clog, 0, NULL, NULL,
2050 CTLFLAG_PERMANENT,
2051 CTLTYPE_STRUCT, "stats",
2052 SYSCTL_DESCR("ARP statistics"),
2053 sysctl_net_inet_arp_stats, 0, NULL, 0,
2054 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2055
2056 sysctl_createv(clog, 0, NULL, NULL,
2057 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2058 CTLTYPE_INT, "log_movements",
2059 SYSCTL_DESCR("log ARP replies from MACs different than"
2060 " the one in the cache"),
2061 NULL, 0, &log_movements, 0,
2062 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2063
2064 sysctl_createv(clog, 0, NULL, NULL,
2065 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2066 CTLTYPE_INT, "log_permanent_modify",
2067 SYSCTL_DESCR("log ARP replies from MACs different than"
2068 " the one in the permanent arp entry"),
2069 NULL, 0, &log_permanent_modify, 0,
2070 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2071
2072 sysctl_createv(clog, 0, NULL, NULL,
2073 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2074 CTLTYPE_INT, "log_wrong_iface",
2075 SYSCTL_DESCR("log ARP packets arriving on the wrong"
2076 " interface"),
2077 NULL, 0, &log_wrong_iface, 0,
2078 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2079
2080 sysctl_createv(clog, 0, NULL, NULL,
2081 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2082 CTLTYPE_INT, "log_unknown_network",
2083 SYSCTL_DESCR("log ARP packets from non-local network"),
2084 NULL, 0, &log_unknown_network, 0,
2085 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2086
2087 sysctl_createv(clog, 0, NULL, NULL,
2088 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2089 CTLTYPE_INT, "debug",
2090 SYSCTL_DESCR("Enable ARP DAD debug output"),
2091 NULL, 0, &arp_debug, 0,
2092 CTL_NET, PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2093 }
2094
2095 #endif /* INET */
2096