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