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