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