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