if_arp.c revision 1.205 1 /* $NetBSD: if_arp.c,v 1.205 2016/04/07 03:22:15 christos 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.205 2016/04/07 03:22:15 christos 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, "%s: bad gateway value\n", __func__);
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, void *desten, size_t destlen)
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 KASSERT(destlen >= ifp->if_addrlen);
712 memcpy(desten, &la->ll_addr, ifp->if_addrlen);
713 LLE_RUNLOCK(la);
714 return 0;
715 }
716
717 /*
718 * Re-send the ARP request when appropriate.
719 */
720 #ifdef DIAGNOSTIC
721 if (rt->rt_expire == 0) {
722 /* This should never happen. (Should it? -gwr) */
723 printf("%s: unresolved and rt_expire == 0\n", __func__);
724 /* Set expiration time to now (expired). */
725 rt->rt_expire = time_uptime;
726 }
727 #endif
728
729 notfound:
730 #ifdef IFF_STATICARP /* FreeBSD */
731 #define _IFF_NOARP (IFF_NOARP | IFF_STATICARP)
732 #else
733 #define _IFF_NOARP IFF_NOARP
734 #endif
735 if (ifp->if_flags & _IFF_NOARP) {
736 if (la != NULL)
737 LLE_RUNLOCK(la);
738 error = ENOTSUP;
739 goto bad;
740 }
741 #undef _IFF_NOARP
742 if (la == NULL) {
743 create_lookup = "create";
744 IF_AFDATA_WLOCK(ifp);
745 la = lla_create(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
746 IF_AFDATA_WUNLOCK(ifp);
747 if (la == NULL)
748 ARP_STATINC(ARP_STAT_ALLOCFAIL);
749 else
750 arp_init_llentry(ifp, la);
751 } else if (LLE_TRY_UPGRADE(la) == 0) {
752 create_lookup = "lookup";
753 LLE_RUNLOCK(la);
754 IF_AFDATA_RLOCK(ifp);
755 la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
756 IF_AFDATA_RUNLOCK(ifp);
757 }
758
759 error = EINVAL;
760 if (la == NULL) {
761 log(LOG_DEBUG,
762 "%s: failed to %s llentry for %s on %s\n",
763 __func__, create_lookup, inet_ntoa(satocsin(dst)->sin_addr),
764 ifp->if_xname);
765 goto bad;
766 }
767
768 if ((la->la_flags & LLE_VALID) &&
769 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime))
770 {
771 KASSERT(destlen >= ifp->if_addrlen);
772 memcpy(desten, &la->ll_addr, ifp->if_addrlen);
773 renew = false;
774 /*
775 * If entry has an expiry time and it is approaching,
776 * see if we need to send an ARP request within this
777 * arpt_down interval.
778 */
779 if (!(la->la_flags & LLE_STATIC) &&
780 time_uptime + la->la_preempt > la->la_expire)
781 {
782 renew = true;
783 la->la_preempt--;
784 }
785
786 LLE_WUNLOCK(la);
787
788 if (renew) {
789 const u_int8_t *enaddr =
790 #if NCARP > 0
791 (ifp->if_type == IFT_CARP) ?
792 CLLADDR(ifp->if_sadl):
793 #endif
794 CLLADDR(ifp->if_sadl);
795 arprequest(ifp,
796 &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
797 &satocsin(dst)->sin_addr, enaddr);
798 }
799
800 return 0;
801 }
802
803 if (la->la_flags & LLE_STATIC) { /* should not happen! */
804 LLE_RUNLOCK(la);
805 log(LOG_DEBUG, "%s: ouch, empty static llinfo for %s\n",
806 __func__, inet_ntoa(satocsin(dst)->sin_addr));
807 error = EINVAL;
808 goto bad;
809 }
810
811 renew = (la->la_asked == 0 || la->la_expire != time_uptime);
812
813 /*
814 * There is an arptab entry, but no ethernet address
815 * response yet. Add the mbuf to the list, dropping
816 * the oldest packet if we have exceeded the system
817 * setting.
818 */
819 LLE_WLOCK_ASSERT(la);
820 if (la->la_numheld >= arp_maxhold) {
821 if (la->la_hold != NULL) {
822 struct mbuf *next = la->la_hold->m_nextpkt;
823 m_freem(la->la_hold);
824 la->la_hold = next;
825 la->la_numheld--;
826 ARP_STATINC(ARP_STAT_DFRDROPPED);
827 }
828 }
829 if (la->la_hold != NULL) {
830 struct mbuf *curr = la->la_hold;
831 while (curr->m_nextpkt != NULL)
832 curr = curr->m_nextpkt;
833 curr->m_nextpkt = m;
834 } else
835 la->la_hold = m;
836 la->la_numheld++;
837 if (!renew)
838 LLE_DOWNGRADE(la);
839
840 /*
841 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
842 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
843 * if we have already sent arp_maxtries ARP requests. Retransmit the
844 * ARP request, but not faster than one request per second.
845 */
846 if (la->la_asked < arp_maxtries)
847 error = EWOULDBLOCK; /* First request. */
848 else
849 error = (rt != NULL && rt->rt_flags & RTF_GATEWAY) ?
850 EHOSTUNREACH : EHOSTDOWN;
851
852 if (renew) {
853 const u_int8_t *enaddr =
854 #if NCARP > 0
855 (rt != NULL && rt->rt_ifp->if_type == IFT_CARP) ?
856 CLLADDR(rt->rt_ifp->if_sadl):
857 #endif
858 CLLADDR(ifp->if_sadl);
859 la->la_expire = time_uptime;
860 arp_settimer(la, arpt_down);
861 la->la_asked++;
862 LLE_WUNLOCK(la);
863
864 if (rt != NULL) {
865 arprequest(ifp, &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
866 &satocsin(dst)->sin_addr, enaddr);
867 } else {
868 struct sockaddr_in sin;
869
870 sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0);
871
872 /* XXX */
873 rt = rtalloc1((struct sockaddr *)&sin, 0);
874 if (rt == NULL)
875 goto bad;
876 arprequest(ifp, &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
877 &satocsin(dst)->sin_addr, enaddr);
878 rtfree(rt);
879 rt = NULL;
880 }
881 return error;
882 }
883
884 LLE_RUNLOCK(la);
885 return error;
886
887 bad:
888 m_freem(m);
889 return error;
890 }
891
892 /*
893 * Common length and type checks are done here,
894 * then the protocol-specific routine is called.
895 */
896 void
897 arpintr(void)
898 {
899 struct mbuf *m;
900 struct arphdr *ar;
901 int s;
902 int arplen;
903
904 mutex_enter(softnet_lock);
905 KERNEL_LOCK(1, NULL);
906 while (arpintrq.ifq_head) {
907 s = splnet();
908 IF_DEQUEUE(&arpintrq, m);
909 splx(s);
910 if (m == NULL || (m->m_flags & M_PKTHDR) == 0)
911 panic("arpintr");
912
913 MCLAIM(m, &arpdomain.dom_mowner);
914 ARP_STATINC(ARP_STAT_RCVTOTAL);
915
916 /*
917 * First, make sure we have at least struct arphdr.
918 */
919 if (m->m_len < sizeof(struct arphdr) ||
920 (ar = mtod(m, struct arphdr *)) == NULL)
921 goto badlen;
922
923 switch (m->m_pkthdr.rcvif->if_type) {
924 case IFT_IEEE1394:
925 arplen = sizeof(struct arphdr) +
926 ar->ar_hln + 2 * ar->ar_pln;
927 break;
928 default:
929 arplen = sizeof(struct arphdr) +
930 2 * ar->ar_hln + 2 * ar->ar_pln;
931 break;
932 }
933
934 if (/* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */
935 m->m_len >= arplen)
936 switch (ntohs(ar->ar_pro)) {
937 case ETHERTYPE_IP:
938 case ETHERTYPE_IPTRAILERS:
939 in_arpinput(m);
940 continue;
941 default:
942 ARP_STATINC(ARP_STAT_RCVBADPROTO);
943 }
944 else {
945 badlen:
946 ARP_STATINC(ARP_STAT_RCVBADLEN);
947 }
948 m_freem(m);
949 }
950 KERNEL_UNLOCK_ONE(NULL);
951 mutex_exit(softnet_lock);
952 }
953
954 /*
955 * ARP for Internet protocols on 10 Mb/s Ethernet.
956 * Algorithm is that given in RFC 826.
957 * In addition, a sanity check is performed on the sender
958 * protocol address, to catch impersonators.
959 * We no longer handle negotiations for use of trailer protocol:
960 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
961 * along with IP replies if we wanted trailers sent to us,
962 * and also sent them in response to IP replies.
963 * This allowed either end to announce the desire to receive
964 * trailer packets.
965 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
966 * but formerly didn't normally send requests.
967 */
968 static void
969 in_arpinput(struct mbuf *m)
970 {
971 struct arphdr *ah;
972 struct ifnet *ifp = m->m_pkthdr.rcvif;
973 struct llentry *la = NULL;
974 struct in_ifaddr *ia;
975 #if NBRIDGE > 0
976 struct in_ifaddr *bridge_ia = NULL;
977 #endif
978 #if NCARP > 0
979 u_int32_t count = 0, index = 0;
980 #endif
981 struct sockaddr sa;
982 struct in_addr isaddr, itaddr, myaddr;
983 int op;
984 void *tha;
985 uint64_t *arps;
986
987 if (__predict_false(m_makewritable(&m, 0, m->m_pkthdr.len, M_DONTWAIT)))
988 goto out;
989 ah = mtod(m, struct arphdr *);
990 op = ntohs(ah->ar_op);
991
992 /*
993 * Fix up ah->ar_hrd if necessary, before using ar_tha() or
994 * ar_tpa().
995 */
996 switch (ifp->if_type) {
997 case IFT_IEEE1394:
998 if (ntohs(ah->ar_hrd) == ARPHRD_IEEE1394)
999 ;
1000 else {
1001 /* XXX this is to make sure we compute ar_tha right */
1002 /* XXX check ar_hrd more strictly? */
1003 ah->ar_hrd = htons(ARPHRD_IEEE1394);
1004 }
1005 break;
1006 default:
1007 /* XXX check ar_hrd? */
1008 break;
1009 }
1010
1011 memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
1012 memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
1013
1014 if (m->m_flags & (M_BCAST|M_MCAST))
1015 ARP_STATINC(ARP_STAT_RCVMCAST);
1016
1017
1018 /*
1019 * Search for a matching interface address
1020 * or any address on the interface to use
1021 * as a dummy address in the rest of this function
1022 */
1023
1024 INADDR_TO_IA(itaddr, ia);
1025 while (ia != NULL) {
1026 #if NCARP > 0
1027 if (ia->ia_ifp->if_type == IFT_CARP &&
1028 ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
1029 (IFF_UP|IFF_RUNNING))) {
1030 index++;
1031 if (ia->ia_ifp == m->m_pkthdr.rcvif &&
1032 carp_iamatch(ia, ar_sha(ah),
1033 &count, index)) {
1034 break;
1035 }
1036 } else
1037 #endif
1038 if (ia->ia_ifp == m->m_pkthdr.rcvif)
1039 break;
1040 #if NBRIDGE > 0
1041 /*
1042 * If the interface we received the packet on
1043 * is part of a bridge, check to see if we need
1044 * to "bridge" the packet to ourselves at this
1045 * layer. Note we still prefer a perfect match,
1046 * but allow this weaker match if necessary.
1047 */
1048 if (m->m_pkthdr.rcvif->if_bridge != NULL &&
1049 m->m_pkthdr.rcvif->if_bridge == ia->ia_ifp->if_bridge)
1050 bridge_ia = ia;
1051 #endif /* NBRIDGE > 0 */
1052
1053 NEXT_IA_WITH_SAME_ADDR(ia);
1054 }
1055
1056 #if NBRIDGE > 0
1057 if (ia == NULL && bridge_ia != NULL) {
1058 ia = bridge_ia;
1059 ifp = bridge_ia->ia_ifp;
1060 }
1061 #endif
1062
1063 if (ia == NULL) {
1064 INADDR_TO_IA(isaddr, ia);
1065 while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif)
1066 NEXT_IA_WITH_SAME_ADDR(ia);
1067
1068 if (ia == NULL) {
1069 IFP_TO_IA(ifp, ia);
1070 if (ia == NULL) {
1071 ARP_STATINC(ARP_STAT_RCVNOINT);
1072 goto out;
1073 }
1074 }
1075 }
1076
1077 myaddr = ia->ia_addr.sin_addr;
1078
1079 /* XXX checks for bridge case? */
1080 if (!memcmp(ar_sha(ah), CLLADDR(ifp->if_sadl), ifp->if_addrlen)) {
1081 ARP_STATINC(ARP_STAT_RCVLOCALSHA);
1082 goto out; /* it's from me, ignore it. */
1083 }
1084
1085 /* XXX checks for bridge case? */
1086 if (!memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
1087 ARP_STATINC(ARP_STAT_RCVBCASTSHA);
1088 log(LOG_ERR,
1089 "%s: arp: link address is broadcast for IP address %s!\n",
1090 ifp->if_xname, in_fmtaddr(isaddr));
1091 goto out;
1092 }
1093
1094 /*
1095 * If the source IP address is zero, this is an RFC 5227 ARP probe
1096 */
1097 if (in_nullhost(isaddr))
1098 ARP_STATINC(ARP_STAT_RCVZEROSPA);
1099 else if (in_hosteq(isaddr, myaddr))
1100 ARP_STATINC(ARP_STAT_RCVLOCALSPA);
1101
1102 if (in_nullhost(itaddr))
1103 ARP_STATINC(ARP_STAT_RCVZEROTPA);
1104
1105 /* DAD check, RFC 5227 2.1.1, Probe Details */
1106 if (in_hosteq(isaddr, myaddr) ||
1107 (in_nullhost(isaddr) && in_hosteq(itaddr, myaddr)))
1108 {
1109 /* If our address is tentative, mark it as duplicated */
1110 if (ia->ia4_flags & IN_IFF_TENTATIVE)
1111 arp_dad_duplicated((struct ifaddr *)ia);
1112 /* If our address is unuseable, don't reply */
1113 if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED))
1114 goto out;
1115 }
1116
1117 /*
1118 * If the target IP address is zero, ignore the packet.
1119 * This prevents the code below from tring to answer
1120 * when we are using IP address zero (booting).
1121 */
1122 if (in_nullhost(itaddr))
1123 goto out;
1124
1125 if (in_nullhost(isaddr))
1126 goto reply;
1127
1128 if (in_hosteq(isaddr, myaddr)) {
1129 log(LOG_ERR,
1130 "duplicate IP address %s sent from link address %s\n",
1131 in_fmtaddr(isaddr), lla_snprintf(ar_sha(ah), ah->ar_hln));
1132 itaddr = myaddr;
1133 goto reply;
1134 }
1135
1136 if (in_hosteq(itaddr, myaddr))
1137 la = arpcreate(ifp, m, &isaddr, NULL, 1);
1138 else
1139 la = arplookup(ifp, m, &isaddr, NULL, 1);
1140 if (la == NULL)
1141 goto reply;
1142
1143 if ((la->la_flags & LLE_VALID) &&
1144 memcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) {
1145 if (la->la_flags & LLE_STATIC) {
1146 ARP_STATINC(ARP_STAT_RCVOVERPERM);
1147 if (!log_permanent_modify)
1148 goto out;
1149 log(LOG_INFO,
1150 "%s tried to overwrite permanent arp info"
1151 " for %s\n",
1152 lla_snprintf(ar_sha(ah), ah->ar_hln),
1153 in_fmtaddr(isaddr));
1154 goto out;
1155 } else if (la->lle_tbl->llt_ifp != ifp) {
1156 /* XXX should not happen? */
1157 ARP_STATINC(ARP_STAT_RCVOVERINT);
1158 if (!log_wrong_iface)
1159 goto out;
1160 log(LOG_INFO,
1161 "%s on %s tried to overwrite "
1162 "arp info for %s on %s\n",
1163 lla_snprintf(ar_sha(ah), ah->ar_hln),
1164 ifp->if_xname, in_fmtaddr(isaddr),
1165 la->lle_tbl->llt_ifp->if_xname);
1166 goto out;
1167 } else {
1168 ARP_STATINC(ARP_STAT_RCVOVER);
1169 if (log_movements)
1170 log(LOG_INFO, "arp info overwritten "
1171 "for %s by %s\n",
1172 in_fmtaddr(isaddr),
1173 lla_snprintf(ar_sha(ah),
1174 ah->ar_hln));
1175 }
1176 }
1177
1178 /* XXX llentry should have addrlen? */
1179 #if 0
1180 /*
1181 * sanity check for the address length.
1182 * XXX this does not work for protocols with variable address
1183 * length. -is
1184 */
1185 if (sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln) {
1186 ARP_STATINC(ARP_STAT_RCVLENCHG);
1187 log(LOG_WARNING,
1188 "arp from %s: new addr len %d, was %d\n",
1189 in_fmtaddr(isaddr), ah->ar_hln, sdl->sdl_alen);
1190 }
1191 #endif
1192
1193 if (ifp->if_addrlen != ah->ar_hln) {
1194 ARP_STATINC(ARP_STAT_RCVBADLEN);
1195 log(LOG_WARNING,
1196 "arp from %s: addr len: new %d, i/f %d (ignored)\n",
1197 in_fmtaddr(isaddr), ah->ar_hln,
1198 ifp->if_addrlen);
1199 goto reply;
1200 }
1201
1202 #if NTOKEN > 0
1203 /*
1204 * XXX uses m_data and assumes the complete answer including
1205 * XXX token-ring headers is in the same buf
1206 */
1207 if (ifp->if_type == IFT_ISO88025) {
1208 struct token_header *trh;
1209
1210 trh = (struct token_header *)M_TRHSTART(m);
1211 if (trh->token_shost[0] & TOKEN_RI_PRESENT) {
1212 struct token_rif *rif;
1213 size_t riflen;
1214
1215 rif = TOKEN_RIF(trh);
1216 riflen = (ntohs(rif->tr_rcf) &
1217 TOKEN_RCF_LEN_MASK) >> 8;
1218
1219 if (riflen > 2 &&
1220 riflen < sizeof(struct token_rif) &&
1221 (riflen & 1) == 0) {
1222 rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION);
1223 rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK);
1224 memcpy(TOKEN_RIF_LLE(la), rif, riflen);
1225 }
1226 }
1227 }
1228 #endif /* NTOKEN > 0 */
1229
1230 KASSERT(sizeof(la->ll_addr) >= ifp->if_addrlen);
1231 (void)memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen);
1232 la->la_flags |= LLE_VALID;
1233 la->la_expire = time_uptime + arpt_keep;
1234 la->la_asked = 0;
1235 KASSERT((la->la_flags & LLE_STATIC) == 0);
1236 arp_settimer(la, arpt_keep);
1237 /* rt->rt_flags &= ~RTF_REJECT; */
1238
1239 if (la->la_hold != NULL) {
1240 int n = la->la_numheld;
1241 struct mbuf *m_hold, *m_hold_next;
1242 struct sockaddr_in sin;
1243
1244 sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0);
1245
1246 m_hold = la->la_hold;
1247 la->la_hold = NULL;
1248 la->la_numheld = 0;
1249 /*
1250 * We have to unlock here because if_output would call
1251 * arpresolve
1252 */
1253 LLE_WUNLOCK(la);
1254 ARP_STATADD(ARP_STAT_DFRSENT, n);
1255 for (; m_hold != NULL; m_hold = m_hold_next) {
1256 m_hold_next = m_hold->m_nextpkt;
1257 m_hold->m_nextpkt = NULL;
1258 (*ifp->if_output)(ifp, m_hold, sintosa(&sin), NULL);
1259 }
1260 } else
1261 LLE_WUNLOCK(la);
1262 la = NULL;
1263
1264 reply:
1265 if (la != NULL) {
1266 LLE_WUNLOCK(la);
1267 la = NULL;
1268 }
1269 if (op != ARPOP_REQUEST) {
1270 if (op == ARPOP_REPLY)
1271 ARP_STATINC(ARP_STAT_RCVREPLY);
1272 goto out;
1273 }
1274 ARP_STATINC(ARP_STAT_RCVREQUEST);
1275 if (in_hosteq(itaddr, myaddr)) {
1276 /* If our address is unuseable, don't reply */
1277 if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED))
1278 goto out;
1279 /* I am the target */
1280 tha = ar_tha(ah);
1281 if (tha)
1282 memcpy(tha, ar_sha(ah), ah->ar_hln);
1283 memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
1284 } else {
1285 /* Proxy ARP */
1286 struct llentry *lle = NULL;
1287 struct sockaddr_in sin;
1288
1289 #if NCARP > 0
1290 if (ifp->if_type == IFT_CARP &&
1291 m->m_pkthdr.rcvif->if_type != IFT_CARP)
1292 goto out;
1293 #endif
1294
1295 tha = ar_tha(ah);
1296
1297 sockaddr_in_init(&sin, &itaddr, 0);
1298
1299 IF_AFDATA_RLOCK(ifp);
1300 lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin);
1301 IF_AFDATA_RUNLOCK(ifp);
1302
1303 if ((lle != NULL) && (lle->la_flags & LLE_PUB)) {
1304 (void)memcpy(tha, ar_sha(ah), ah->ar_hln);
1305 (void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln);
1306 LLE_RUNLOCK(lle);
1307 } else {
1308 if (lle != NULL)
1309 LLE_RUNLOCK(lle);
1310 goto drop;
1311 }
1312 }
1313
1314 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
1315 memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
1316 ah->ar_op = htons(ARPOP_REPLY);
1317 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1318 switch (ifp->if_type) {
1319 case IFT_IEEE1394:
1320 /*
1321 * ieee1394 arp reply is broadcast
1322 */
1323 m->m_flags &= ~M_MCAST;
1324 m->m_flags |= M_BCAST;
1325 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + ah->ar_hln;
1326 break;
1327
1328 default:
1329 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
1330 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
1331 break;
1332 }
1333 m->m_pkthdr.len = m->m_len;
1334 sa.sa_family = AF_ARP;
1335 sa.sa_len = 2;
1336 arps = ARP_STAT_GETREF();
1337 arps[ARP_STAT_SNDTOTAL]++;
1338 arps[ARP_STAT_SNDREPLY]++;
1339 ARP_STAT_PUTREF();
1340 (*ifp->if_output)(ifp, m, &sa, NULL);
1341 return;
1342
1343 out:
1344 if (la != NULL)
1345 LLE_WUNLOCK(la);
1346 drop:
1347 m_freem(m);
1348 }
1349
1350 /*
1351 * Lookup or a new address in arptab.
1352 */
1353 static struct llentry *
1354 arplookup(struct ifnet *ifp, struct mbuf *m, const struct in_addr *addr,
1355 const struct sockaddr *sa, int wlock)
1356 {
1357 struct sockaddr_in sin;
1358 struct llentry *la;
1359 int flags = wlock ? LLE_EXCLUSIVE : 0;
1360
1361
1362 if (sa == NULL) {
1363 KASSERT(addr != NULL);
1364 sockaddr_in_init(&sin, addr, 0);
1365 sa = sintocsa(&sin);
1366 }
1367
1368 IF_AFDATA_RLOCK(ifp);
1369 la = lla_lookup(LLTABLE(ifp), flags, sa);
1370 IF_AFDATA_RUNLOCK(ifp);
1371
1372 return la;
1373 }
1374
1375 static struct llentry *
1376 arpcreate(struct ifnet *ifp, struct mbuf *m, const struct in_addr *addr,
1377 const struct sockaddr *sa, int wlock)
1378 {
1379 struct sockaddr_in sin;
1380 struct llentry *la;
1381 int flags = wlock ? LLE_EXCLUSIVE : 0;
1382
1383 if (sa == NULL) {
1384 KASSERT(addr != NULL);
1385 sockaddr_in_init(&sin, addr, 0);
1386 sa = sintocsa(&sin);
1387 }
1388
1389 la = arplookup(ifp, m, addr, sa, wlock);
1390
1391 if (la == NULL) {
1392 IF_AFDATA_WLOCK(ifp);
1393 la = lla_create(LLTABLE(ifp), flags, sa);
1394 IF_AFDATA_WUNLOCK(ifp);
1395
1396 if (la != NULL)
1397 arp_init_llentry(ifp, la);
1398 }
1399
1400 return la;
1401 }
1402
1403 int
1404 arpioctl(u_long cmd, void *data)
1405 {
1406
1407 return EOPNOTSUPP;
1408 }
1409
1410 void
1411 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1412 {
1413 struct in_addr *ip;
1414 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1415
1416 /*
1417 * Warn the user if another station has this IP address,
1418 * but only if the interface IP address is not zero.
1419 */
1420 ip = &IA_SIN(ifa)->sin_addr;
1421 if (!in_nullhost(*ip) &&
1422 (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED)) == 0) {
1423 struct llentry *lle;
1424
1425 arprequest(ifp, ip, ip, CLLADDR(ifp->if_sadl));
1426
1427 /*
1428 * interface address is considered static entry
1429 * because the output of the arp utility shows
1430 * that L2 entry as permanent
1431 */
1432 IF_AFDATA_WLOCK(ifp);
1433 lle = lla_create(LLTABLE(ifp), (LLE_IFADDR | LLE_STATIC),
1434 (struct sockaddr *)IA_SIN(ifa));
1435 IF_AFDATA_WUNLOCK(ifp);
1436 if (lle == NULL)
1437 log(LOG_INFO, "%s: cannot create arp entry for"
1438 " interface address\n", __func__);
1439 else {
1440 arp_init_llentry(ifp, lle);
1441 LLE_RUNLOCK(lle);
1442 }
1443 }
1444
1445 ifa->ifa_rtrequest = arp_rtrequest;
1446 ifa->ifa_flags |= RTF_CONNECTED;
1447
1448 /* ARP will handle DAD for this address. */
1449 if (ia->ia4_flags & IN_IFF_TRYTENTATIVE) {
1450 ia->ia4_flags |= IN_IFF_TENTATIVE;
1451 ia->ia_dad_start = arp_dad_start;
1452 ia->ia_dad_stop = arp_dad_stop;
1453 }
1454 }
1455
1456 TAILQ_HEAD(dadq_head, dadq);
1457 struct dadq {
1458 TAILQ_ENTRY(dadq) dad_list;
1459 struct ifaddr *dad_ifa;
1460 int dad_count; /* max ARP to send */
1461 int dad_arp_tcount; /* # of trials to send ARP */
1462 int dad_arp_ocount; /* ARP sent so far */
1463 int dad_arp_announce; /* max ARP announcements */
1464 int dad_arp_acount; /* # of announcements */
1465 struct callout dad_timer_ch;
1466 };
1467 MALLOC_JUSTDEFINE(M_IPARP, "ARP DAD", "ARP DAD Structure");
1468
1469 static struct dadq_head dadq;
1470 static int dad_init = 0;
1471 static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */
1472
1473 static struct dadq *
1474 arp_dad_find(struct ifaddr *ifa)
1475 {
1476 struct dadq *dp;
1477
1478 TAILQ_FOREACH(dp, &dadq, dad_list) {
1479 if (dp->dad_ifa == ifa)
1480 return dp;
1481 }
1482 return NULL;
1483 }
1484
1485 static void
1486 arp_dad_starttimer(struct dadq *dp, int ticks)
1487 {
1488
1489 callout_reset(&dp->dad_timer_ch, ticks,
1490 (void (*)(void *))arp_dad_timer, (void *)dp->dad_ifa);
1491 }
1492
1493 static void
1494 arp_dad_stoptimer(struct dadq *dp)
1495 {
1496
1497 callout_stop(&dp->dad_timer_ch);
1498 }
1499
1500 static void
1501 arp_dad_output(struct dadq *dp, struct ifaddr *ifa)
1502 {
1503 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1504 struct ifnet *ifp = ifa->ifa_ifp;
1505 struct in_addr sip;
1506
1507 dp->dad_arp_tcount++;
1508 if ((ifp->if_flags & IFF_UP) == 0)
1509 return;
1510 if ((ifp->if_flags & IFF_RUNNING) == 0)
1511 return;
1512
1513 dp->dad_arp_tcount = 0;
1514 dp->dad_arp_ocount++;
1515
1516 memset(&sip, 0, sizeof(sip));
1517 arprequest(ifa->ifa_ifp, &sip, &ia->ia_addr.sin_addr,
1518 CLLADDR(ifa->ifa_ifp->if_sadl));
1519 }
1520
1521 /*
1522 * Start Duplicate Address Detection (DAD) for specified interface address.
1523 */
1524 static void
1525 arp_dad_start(struct ifaddr *ifa)
1526 {
1527 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1528 struct dadq *dp;
1529
1530 if (!dad_init) {
1531 TAILQ_INIT(&dadq);
1532 dad_init++;
1533 }
1534
1535 /*
1536 * If we don't need DAD, don't do it.
1537 * - DAD is disabled (ip_dad_count == 0)
1538 */
1539 if (!(ia->ia4_flags & IN_IFF_TENTATIVE)) {
1540 log(LOG_DEBUG,
1541 "%s: called with non-tentative address %s(%s)\n", __func__,
1542 in_fmtaddr(ia->ia_addr.sin_addr),
1543 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1544 return;
1545 }
1546 if (!ip_dad_count) {
1547 struct in_addr *ip = &IA_SIN(ifa)->sin_addr;
1548
1549 ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1550 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1551 arprequest(ifa->ifa_ifp, ip, ip,
1552 CLLADDR(ifa->ifa_ifp->if_sadl));
1553 return;
1554 }
1555 if (ifa->ifa_ifp == NULL)
1556 panic("arp_dad_start: ifa->ifa_ifp == NULL");
1557 if (!(ifa->ifa_ifp->if_flags & IFF_UP))
1558 return;
1559 if (arp_dad_find(ifa) != NULL) {
1560 /* DAD already in progress */
1561 return;
1562 }
1563
1564 dp = malloc(sizeof(*dp), M_IPARP, M_NOWAIT);
1565 if (dp == NULL) {
1566 log(LOG_ERR, "%s: memory allocation failed for %s(%s)\n",
1567 __func__, in_fmtaddr(ia->ia_addr.sin_addr),
1568 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1569 return;
1570 }
1571 memset(dp, 0, sizeof(*dp));
1572 callout_init(&dp->dad_timer_ch, CALLOUT_MPSAFE);
1573 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1574
1575 arplog((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1576 in_fmtaddr(ia->ia_addr.sin_addr)));
1577
1578 /*
1579 * Send ARP packet for DAD, ip_dad_count times.
1580 * Note that we must delay the first transmission.
1581 */
1582 dp->dad_ifa = ifa;
1583 ifaref(ifa); /* just for safety */
1584 dp->dad_count = ip_dad_count;
1585 dp->dad_arp_announce = 0; /* Will be set when starting to announce */
1586 dp->dad_arp_acount = dp->dad_arp_ocount = dp->dad_arp_tcount = 0;
1587
1588 arp_dad_starttimer(dp, cprng_fast32() % (PROBE_WAIT * hz));
1589 }
1590
1591 /*
1592 * terminate DAD unconditionally. used for address removals.
1593 */
1594 static void
1595 arp_dad_stop(struct ifaddr *ifa)
1596 {
1597 struct dadq *dp;
1598
1599 if (!dad_init)
1600 return;
1601 dp = arp_dad_find(ifa);
1602 if (dp == NULL) {
1603 /* DAD wasn't started yet */
1604 return;
1605 }
1606
1607 arp_dad_stoptimer(dp);
1608
1609 TAILQ_REMOVE(&dadq, dp, dad_list);
1610 free(dp, M_IPARP);
1611 dp = NULL;
1612 ifafree(ifa);
1613 }
1614
1615 static void
1616 arp_dad_timer(struct ifaddr *ifa)
1617 {
1618 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1619 struct dadq *dp;
1620 struct in_addr *ip;
1621
1622 mutex_enter(softnet_lock);
1623 KERNEL_LOCK(1, NULL);
1624
1625 /* Sanity check */
1626 if (ia == NULL) {
1627 log(LOG_ERR, "%s: called with null parameter\n", __func__);
1628 goto done;
1629 }
1630 dp = arp_dad_find(ifa);
1631 if (dp == NULL) {
1632 log(LOG_ERR, "%s: DAD structure not found\n", __func__);
1633 goto done;
1634 }
1635 if (ia->ia4_flags & IN_IFF_DUPLICATED) {
1636 log(LOG_ERR, "%s: called with duplicate address %s(%s)\n",
1637 __func__, 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 {
1643 log(LOG_ERR, "%s: called with non-tentative address %s(%s)\n",
1644 __func__, 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, "%s: DAD structure not found\n", __func__);
1729 return;
1730 }
1731
1732 ifp = ifa->ifa_ifp;
1733 log(LOG_ERR,
1734 "%s: DAD detected duplicate IPv4 address %s: 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