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