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