if_arp.c revision 1.320 1 /* $NetBSD: if_arp.c,v 1.320 2026/05/29 02:45:14 ozaki-r Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2000, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Public Access Networks Corporation ("Panix"). It was developed under
9 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)if_ether.c 8.2 (Berkeley) 9/26/94
62 */
63
64 /*
65 * Ethernet address resolution protocol.
66 * TODO:
67 * add "inuse/lock" bit (or ref. count) along with valid bit
68 */
69
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.320 2026/05/29 02:45:14 ozaki-r 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 in_arpinput(m);
738 continue;
739 default:
740 ARP_STATINC(ARP_STAT_RCVBADPROTO);
741 goto free;
742 }
743
744 badlen:
745 ARP_STATINC(ARP_STAT_RCVBADLEN);
746 free:
747 m_freem(m);
748 }
749 SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
750 return; /* XXX gcc */
751 }
752
753 /*
754 * ARP for Internet protocols on 10 Mb/s Ethernet. Algorithm is that given in
755 * RFC 826. In addition, a sanity check is performed on the sender protocol
756 * address, to catch impersonators.
757 *
758 * We no longer handle negotiations for use of trailer protocol: formerly, ARP
759 * replied for protocol type ETHERTYPE_TRAIL sent along with IP replies if we
760 * wanted trailers sent to us, and also sent them in response to IP replies.
761 * This allowed either end to announce the desire to receive trailer packets.
762 *
763 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, but
764 * formerly didn't normally send requests.
765 */
766 static void
767 in_arpinput(struct mbuf *m)
768 {
769 struct arphdr *ah;
770 struct ifnet *ifp, *rcvif = NULL;
771 struct llentry *la = NULL;
772 struct in_ifaddr *ia = NULL;
773 #if NBRIDGE > 0
774 struct in_ifaddr *bridge_ia = NULL;
775 #endif
776 #if NCARP > 0
777 uint32_t count = 0, index = 0;
778 #endif
779 struct sockaddr sa;
780 struct in_addr isaddr, itaddr, myaddr;
781 int op, rt_cmd, new_state = 0;
782 void *tha;
783 net_stat_ref_t arps;
784 struct psref psref, psref_ia;
785 int s;
786 char ipbuf[INET_ADDRSTRLEN];
787 bool find_source, do_dad;
788
789 if (__predict_false(m_makewritable(&m, 0, m->m_pkthdr.len, M_DONTWAIT)))
790 goto out;
791 ah = mtod(m, struct arphdr *);
792 op = ntohs(ah->ar_op);
793
794 if (ah->ar_pln != sizeof(struct in_addr))
795 goto out;
796
797 /* RFC5227 2.4 says any of the host's own interface addresses
798 * are not conflicting ARP packets. */
799 ifp = if_get_bylla(ar_sha(ah), ah->ar_hln, &psref);
800 if (ifp) {
801 /* it's from me, ignore it. */
802 if_put(ifp, &psref);
803 ARP_STATINC(ARP_STAT_RCVLOCALSHA);
804 goto out;
805 }
806
807 rcvif = ifp = m_get_rcvif_psref(m, &psref);
808 if (__predict_false(rcvif == NULL))
809 goto out;
810 if (rcvif->if_flags & IFF_NOARP)
811 goto out;
812
813 memcpy(&isaddr, ar_spa(ah), sizeof(isaddr));
814 memcpy(&itaddr, ar_tpa(ah), sizeof(itaddr));
815
816 if (m->m_flags & (M_BCAST|M_MCAST))
817 ARP_STATINC(ARP_STAT_RCVMCAST);
818
819 /*
820 * Search for a matching interface address
821 * or any address on the interface to use
822 * as a dummy address in the rest of this function.
823 *
824 * First try and find the source address for early
825 * duplicate address detection.
826 */
827 if (in_nullhost(isaddr)) {
828 if (in_nullhost(itaddr)) /* very bogus ARP */
829 goto out;
830 find_source = false;
831 myaddr = itaddr;
832 } else {
833 find_source = true;
834 myaddr = isaddr;
835 }
836 s = pserialize_read_enter();
837 again:
838 IN_ADDRHASH_READER_FOREACH(ia, myaddr.s_addr) {
839 if (!in_hosteq(ia->ia_addr.sin_addr, myaddr))
840 continue;
841 #if NCARP > 0
842 if (ia->ia_ifp->if_type == IFT_CARP &&
843 ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
844 (IFF_UP|IFF_RUNNING))) {
845 index++;
846 /* XXX: ar_hln? */
847 if (ia->ia_ifp == rcvif && (ah->ar_hln >= 6) &&
848 carp_iamatch(ia, ar_sha(ah),
849 &count, index)) {
850 break;
851 }
852 } else
853 #endif
854 if (ia->ia_ifp == rcvif)
855 break;
856 #if NBRIDGE > 0
857 /*
858 * If the interface we received the packet on
859 * is part of a bridge, check to see if we need
860 * to "bridge" the packet to ourselves at this
861 * layer. Note we still prefer a perfect match,
862 * but allow this weaker match if necessary.
863 */
864 if (rcvif->if_bridge != NULL &&
865 rcvif->if_bridge == ia->ia_ifp->if_bridge)
866 bridge_ia = ia;
867 #endif
868 }
869
870 #if NBRIDGE > 0
871 if (ia == NULL && bridge_ia != NULL) {
872 ia = bridge_ia;
873 m_put_rcvif_psref(rcvif, &psref);
874 rcvif = NULL;
875 /* FIXME */
876 ifp = bridge_ia->ia_ifp;
877 }
878 #endif
879
880 /* If we failed to find the source address then find
881 * the target address. */
882 if (ia == NULL && find_source && !in_nullhost(itaddr)) {
883 find_source = false;
884 myaddr = itaddr;
885 goto again;
886 }
887
888 if (ia != NULL)
889 ia4_acquire(ia, &psref_ia);
890 pserialize_read_exit(s);
891
892 if (ah->ar_hln != ifp->if_addrlen) {
893 ARP_STATINC(ARP_STAT_RCVBADLEN);
894 log(LOG_WARNING,
895 "arp from %s: addr len: new %d, i/f %d (ignored)\n",
896 IN_PRINT(ipbuf, &isaddr), ah->ar_hln, ifp->if_addrlen);
897 goto out;
898 }
899
900 /* Only do DaD if we have a matching address. */
901 do_dad = (ia != NULL);
902
903 if (ia == NULL) {
904 ia = in_get_ia_on_iface_psref(isaddr, rcvif, &psref_ia);
905 if (ia == NULL) {
906 ia = in_get_ia_from_ifp_psref(ifp, &psref_ia);
907 if (ia == NULL) {
908 ARP_STATINC(ARP_STAT_RCVNOINT);
909 goto out;
910 }
911 }
912 }
913
914 myaddr = ia->ia_addr.sin_addr;
915
916 /* XXX checks for bridge case? */
917 if (!memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
918 ARP_STATINC(ARP_STAT_RCVBCASTSHA);
919 log(LOG_ERR,
920 "%s: arp: link address is broadcast for IP address %s!\n",
921 ifp->if_xname, IN_PRINT(ipbuf, &isaddr));
922 goto out;
923 }
924
925 /*
926 * If the source IP address is zero, this is an RFC 5227 ARP probe
927 */
928 if (in_nullhost(isaddr))
929 ARP_STATINC(ARP_STAT_RCVZEROSPA);
930 else if (in_hosteq(isaddr, myaddr)) {
931 ARP_STATINC(ARP_STAT_RCVLOCALSPA);
932 /* This is the original behavior prior to supporting IPv4 DAD */
933 if (!ip_dad_enabled()) {
934 char llabuf[LLA_ADDRSTRLEN];
935 log(LOG_ERR,
936 "duplicate IP address %s sent from link address %s\n",
937 IN_PRINT(ipbuf, &isaddr),
938 lla_snprintf(llabuf, sizeof(llabuf), ar_sha(ah),
939 ah->ar_hln));
940 itaddr = myaddr;
941 goto reply;
942 }
943 }
944
945 if (in_nullhost(itaddr))
946 ARP_STATINC(ARP_STAT_RCVZEROTPA);
947
948 /*
949 * DAD check, RFC 5227.
950 * ARP sender hardware address must match the interface
951 * address of the interface sending the packet.
952 * Collision on sender address is always a duplicate.
953 * Collision on target address is only a duplicate
954 * IF the sender address is the null host (ie a DAD probe)
955 * AND the message was broadcast
956 * AND our address is either tentative or duplicated
957 * If it was unicast then it's a valid Unicast Poll from RFC 1122.
958 */
959 if (ip_dad_enabled() && do_dad &&
960 (in_hosteq(isaddr, myaddr) ||
961 (in_nullhost(isaddr) && in_hosteq(itaddr, myaddr) &&
962 m->m_flags & M_BCAST &&
963 ia->ia4_flags & (IN_IFF_TENTATIVE | IN_IFF_DUPLICATED))))
964 {
965 struct m_tag *mtag;
966
967 mtag = m_tag_find(m, PACKET_TAG_ETHERNET_SRC);
968 if (mtag == NULL || (ah->ar_hln == ETHER_ADDR_LEN &&
969 memcmp(mtag + 1, ar_sha(ah), ah->ar_hln) == 0)) {
970 struct sockaddr_dl sdl, *sdlp;
971
972 sdlp = sockaddr_dl_init(&sdl, sizeof(sdl),
973 ifp->if_index, ifp->if_type,
974 NULL, 0, ar_sha(ah), ah->ar_hln);
975 arp_dad_duplicated((struct ifaddr *)ia, sdlp);
976 goto out;
977 }
978 }
979
980 /*
981 * If the target IP address is zero, ignore the packet.
982 * This prevents the code below from trying to answer
983 * when we are using IP address zero (booting).
984 */
985 if (in_nullhost(itaddr))
986 goto out;
987
988 if (in_nullhost(isaddr))
989 goto reply;
990
991 if (in_hosteq(itaddr, myaddr))
992 la = arpcreate(ifp, &isaddr, NULL, 1);
993 else
994 la = arplookup(ifp, &isaddr, NULL, 1);
995 if (la == NULL)
996 goto reply;
997
998 if ((la->la_flags & LLE_VALID) &&
999 memcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen))
1000 {
1001 char llabuf[LLA_ADDRSTRLEN], *llastr;
1002
1003 llastr = lla_snprintf(llabuf, sizeof(llabuf),
1004 ar_sha(ah), ah->ar_hln);
1005
1006 if (la->la_flags & LLE_STATIC) {
1007 ARP_STATINC(ARP_STAT_RCVOVERPERM);
1008 if (!log_permanent_modify)
1009 goto out;
1010 log(LOG_INFO,
1011 "%s tried to overwrite permanent arp info"
1012 " for %s\n", llastr, IN_PRINT(ipbuf, &isaddr));
1013 goto out;
1014 } else if (la->lle_tbl->llt_ifp != ifp) {
1015 /* XXX should not happen? */
1016 ARP_STATINC(ARP_STAT_RCVOVERINT);
1017 if (!log_wrong_iface)
1018 goto out;
1019 log(LOG_INFO,
1020 "%s on %s tried to overwrite "
1021 "arp info for %s on %s\n",
1022 llastr,
1023 ifp->if_xname, IN_PRINT(ipbuf, &isaddr),
1024 la->lle_tbl->llt_ifp->if_xname);
1025 goto out;
1026 } else {
1027 ARP_STATINC(ARP_STAT_RCVOVER);
1028 if (log_movements)
1029 log(LOG_INFO, "arp info overwritten "
1030 "for %s by %s\n",
1031 IN_PRINT(ipbuf, &isaddr), llastr);
1032 }
1033 rt_cmd = RTM_CHANGE;
1034 new_state = ND_LLINFO_STALE;
1035 } else {
1036 if (op == ARPOP_REPLY && in_hosteq(itaddr, myaddr)) {
1037 /* This was a solicited ARP reply. */
1038 la->ln_byhint = 0;
1039 new_state = ND_LLINFO_REACHABLE;
1040 } else if (op == ARPOP_REQUEST &&
1041 (la->ln_state == ND_LLINFO_NOSTATE ||
1042 la->ln_state == ND_LLINFO_INCOMPLETE)) {
1043 /*
1044 * If an ARP request comes but there is no entry
1045 * and a new one has been created or an entry exists
1046 * but incomplete, make it stale to allow to send
1047 * packets to the requester without an ARP resolution.
1048 */
1049 la->ln_byhint = 0;
1050 new_state = ND_LLINFO_STALE;
1051 }
1052 rt_cmd = la->la_flags & LLE_VALID ? 0 : RTM_ADD;
1053 }
1054
1055 KASSERT(ifp->if_sadl->sdl_alen == ifp->if_addrlen);
1056
1057 KASSERT(sizeof(la->ll_addr) >= ifp->if_addrlen);
1058 memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen);
1059 la->la_flags |= LLE_VALID;
1060 la->la_flags &= ~LLE_UNRESOLVED;
1061 la->ln_asked = 0;
1062 if (new_state != 0) {
1063 la->ln_state = new_state;
1064
1065 if (new_state != ND_LLINFO_REACHABLE ||
1066 !(la->la_flags & LLE_STATIC))
1067 {
1068 int timer = ND_TIMER_GC;
1069
1070 if (new_state == ND_LLINFO_REACHABLE)
1071 timer = ND_TIMER_REACHABLE;
1072 nd_set_timer(la, timer);
1073 }
1074 }
1075
1076 if (rt_cmd != 0) {
1077 struct sockaddr_in sin;
1078
1079 sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0);
1080 rt_clonedmsg(rt_cmd, NULL, sintosa(&sin), ar_sha(ah), ifp);
1081 }
1082
1083 if (la->la_hold != NULL) {
1084 int n = la->la_numheld;
1085 struct mbuf *m_hold, *m_hold_next;
1086 struct sockaddr_in sin;
1087
1088 sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0);
1089
1090 m_hold = la->la_hold;
1091 la->la_hold = NULL;
1092 la->la_numheld = 0;
1093 /*
1094 * We have to unlock here because if_output would call
1095 * arpresolve
1096 */
1097 LLE_WUNLOCK(la);
1098 ARP_STATADD(ARP_STAT_DFRSENT, n);
1099 ARP_STATADD(ARP_STAT_DFRTOTAL, n);
1100 for (; m_hold != NULL; m_hold = m_hold_next) {
1101 m_hold_next = m_hold->m_nextpkt;
1102 m_hold->m_nextpkt = NULL;
1103 if_output_lock(ifp, ifp, m_hold, sintosa(&sin), NULL);
1104 }
1105 } else
1106 LLE_WUNLOCK(la);
1107 la = NULL;
1108
1109 reply:
1110 if (la != NULL) {
1111 LLE_WUNLOCK(la);
1112 la = NULL;
1113 }
1114 if (op != ARPOP_REQUEST) {
1115 if (op == ARPOP_REPLY)
1116 ARP_STATINC(ARP_STAT_RCVREPLY);
1117 goto out;
1118 }
1119 ARP_STATINC(ARP_STAT_RCVREQUEST);
1120 if (in_hosteq(itaddr, myaddr)) {
1121 /* If our address is unusable, don't reply */
1122 if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED))
1123 goto out;
1124 /* I am the target */
1125 tha = ar_tha(ah);
1126 if (tha)
1127 memcpy(tha, ar_sha(ah), ah->ar_hln);
1128 memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
1129 } else {
1130 /* Proxy ARP */
1131 struct llentry *lle = NULL;
1132 struct sockaddr_in sin;
1133
1134 #if NCARP > 0
1135 if (ifp->if_type == IFT_CARP) {
1136 struct ifnet *_rcvif = m_get_rcvif(m, &s);
1137 int iftype = 0;
1138 if (__predict_true(_rcvif != NULL))
1139 iftype = _rcvif->if_type;
1140 m_put_rcvif(_rcvif, &s);
1141 if (iftype != IFT_CARP)
1142 goto out;
1143 }
1144 #endif
1145
1146 tha = ar_tha(ah);
1147
1148 sockaddr_in_init(&sin, &itaddr, 0);
1149
1150 IF_AFDATA_RLOCK(ifp);
1151 lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin);
1152 IF_AFDATA_RUNLOCK(ifp);
1153
1154 if ((lle != NULL) && (lle->la_flags & LLE_PUB)) {
1155 if (tha)
1156 memcpy(tha, ar_sha(ah), ah->ar_hln);
1157 memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln);
1158 LLE_RUNLOCK(lle);
1159 } else {
1160 if (lle != NULL)
1161 LLE_RUNLOCK(lle);
1162 goto out;
1163 }
1164 }
1165 ia4_release(ia, &psref_ia);
1166
1167 /*
1168 * XXX XXX: Here we're recycling the mbuf. But the mbuf could have
1169 * other mbufs in its chain, and just overwriting m->m_pkthdr.len
1170 * would be wrong in this case (the length becomes smaller than the
1171 * real chain size).
1172 *
1173 * This can theoretically cause bugs in the lower layers (drivers,
1174 * and L2encap), in some corner cases.
1175 */
1176 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
1177 memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
1178 ah->ar_op = htons(ARPOP_REPLY);
1179 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1180 switch (ifp->if_type) {
1181 case IFT_IEEE1394:
1182 /* ieee1394 arp reply is broadcast */
1183 m->m_flags &= ~M_MCAST;
1184 m->m_flags |= M_BCAST;
1185 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + ah->ar_hln;
1186 break;
1187 default:
1188 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
1189 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
1190 break;
1191 }
1192 m->m_pkthdr.len = m->m_len;
1193 m->m_pkthdr.csum_flags = 0; /* Clear any in-bound checksum flags. */
1194 sa.sa_family = AF_ARP;
1195 sa.sa_len = 2;
1196 arps = ARP_STAT_GETREF();
1197 _NET_STATINC_REF(arps, ARP_STAT_SNDTOTAL);
1198 _NET_STATINC_REF(arps, ARP_STAT_SNDREPLY);
1199 ARP_STAT_PUTREF();
1200 if_output_lock(ifp, ifp, m, &sa, NULL);
1201 if (rcvif != NULL)
1202 m_put_rcvif_psref(rcvif, &psref);
1203 return;
1204
1205 out:
1206 if (la != NULL)
1207 LLE_WUNLOCK(la);
1208 if (ia != NULL)
1209 ia4_release(ia, &psref_ia);
1210 if (rcvif != NULL)
1211 m_put_rcvif_psref(rcvif, &psref);
1212 m_freem(m);
1213 }
1214
1215 /*
1216 * Lookup or a new address in arptab.
1217 */
1218 struct llentry *
1219 arplookup(struct ifnet *ifp, const struct in_addr *addr,
1220 const struct sockaddr *sa, int wlock)
1221 {
1222 struct sockaddr_in sin;
1223 struct llentry *la;
1224 int flags = wlock ? LLE_EXCLUSIVE : 0;
1225
1226 if (sa == NULL) {
1227 KASSERT(addr != NULL);
1228 sockaddr_in_init(&sin, addr, 0);
1229 sa = sintocsa(&sin);
1230 }
1231
1232 IF_AFDATA_RLOCK(ifp);
1233 la = lla_lookup(LLTABLE(ifp), flags, sa);
1234 IF_AFDATA_RUNLOCK(ifp);
1235
1236 return la;
1237 }
1238
1239 static struct llentry *
1240 arpcreate(struct ifnet *ifp, const struct in_addr *addr,
1241 const struct sockaddr *sa, int wlock)
1242 {
1243 struct sockaddr_in sin;
1244 struct llentry *la;
1245 int flags = wlock ? LLE_EXCLUSIVE : 0;
1246
1247 if (sa == NULL) {
1248 KASSERT(addr != NULL);
1249 sockaddr_in_init(&sin, addr, 0);
1250 sa = sintocsa(&sin);
1251 }
1252
1253 la = arplookup(ifp, addr, sa, wlock);
1254
1255 if (la == NULL) {
1256 struct rtentry *rt;
1257
1258 rt = rtalloc1(sa, 0);
1259 IF_AFDATA_WLOCK(ifp);
1260 la = lla_create(LLTABLE(ifp), flags, sa, rt);
1261 IF_AFDATA_WUNLOCK(ifp);
1262 if (rt != NULL)
1263 rt_unref(rt);
1264
1265 if (la != NULL)
1266 la->ln_state = ND_LLINFO_NOSTATE;
1267 }
1268
1269 return la;
1270 }
1271
1272 int
1273 arpioctl(u_long cmd, void *data)
1274 {
1275
1276 return EOPNOTSUPP;
1277 }
1278
1279 void
1280 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1281 {
1282 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1283
1284 ifa->ifa_rtrequest = arp_rtrequest;
1285 ifa->ifa_flags |= RTF_CONNECTED;
1286
1287 /* ARP will handle DAD for this address. */
1288 if (in_nullhost(IA_SIN(ifa)->sin_addr)) {
1289 if (ia->ia_dad_stop != NULL) /* safety */
1290 ia->ia_dad_stop(ifa);
1291 ia->ia_dad_start = NULL;
1292 ia->ia_dad_stop = NULL;
1293 ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1294 } else {
1295 ia->ia_dad_start = arp_dad_start;
1296 ia->ia_dad_stop = arp_dad_stop;
1297 if (ia->ia4_flags & IN_IFF_TRYTENTATIVE && ip_dad_enabled())
1298 ia->ia4_flags |= IN_IFF_TENTATIVE;
1299 else
1300 arpannounce1(ifa);
1301 }
1302 }
1303
1304 static bool
1305 arp_nud_enabled(__unused struct ifnet *ifp)
1306 {
1307
1308 return arp_perform_nud != 0;
1309 }
1310
1311 static unsigned int
1312 arp_llinfo_reachable(__unused struct ifnet *ifp)
1313 {
1314
1315 return arp_reachable;
1316 }
1317
1318 static unsigned int
1319 arp_llinfo_retrans(__unused struct ifnet *ifp)
1320 {
1321
1322 return arp_retrans;
1323 }
1324
1325 /*
1326 * Gets source address of the first packet in hold queue
1327 * and stores it in @src.
1328 * Returns pointer to @src (if hold queue is not empty) or NULL.
1329 */
1330 static union l3addr *
1331 arp_llinfo_holdsrc(struct llentry *ln, union l3addr *src)
1332 {
1333 struct ip *ip;
1334
1335 if (ln == NULL || ln->ln_hold == NULL)
1336 return NULL;
1337
1338 /*
1339 * assuming every packet in ln_hold has the same IP header
1340 */
1341 ip = mtod(ln->ln_hold, struct ip *);
1342 /* XXX pullup? */
1343 if (sizeof(*ip) < ln->ln_hold->m_len)
1344 src->addr4 = ip->ip_src;
1345 else
1346 src = NULL;
1347
1348 return src;
1349 }
1350
1351 static void
1352 arp_llinfo_output(struct ifnet *ifp, __unused const union l3addr *daddr,
1353 const union l3addr *taddr, const uint8_t *tlladdr,
1354 const union l3addr *hsrc)
1355 {
1356 struct in_addr tip = taddr->addr4, sip = zeroin_addr;
1357 const uint8_t *slladdr = CLLADDR(ifp->if_sadl);
1358
1359 if (hsrc != NULL) {
1360 struct in_ifaddr *ia;
1361 struct psref psref;
1362
1363 ia = in_get_ia_on_iface_psref(hsrc->addr4, ifp, &psref);
1364 if (ia != NULL) {
1365 sip = hsrc->addr4;
1366 ia4_release(ia, &psref);
1367 }
1368 }
1369
1370 if (sip.s_addr == INADDR_ANY) {
1371 struct sockaddr_in dst;
1372 struct rtentry *rt;
1373
1374 sockaddr_in_init(&dst, &tip, 0);
1375 rt = rtalloc1(sintosa(&dst), 0);
1376 if (rt != NULL) {
1377 if (rt->rt_ifp == ifp &&
1378 rt->rt_ifa != NULL &&
1379 rt->rt_ifa->ifa_addr->sa_family == AF_INET)
1380 sip = satosin(rt->rt_ifa->ifa_addr)->sin_addr;
1381 rt_unref(rt);
1382 }
1383 if (sip.s_addr == INADDR_ANY) {
1384 char ipbuf[INET_ADDRSTRLEN];
1385
1386 log(LOG_DEBUG, "%s: source can't be "
1387 "determined: dst=%s\n", __func__,
1388 IN_PRINT(ipbuf, &tip));
1389 return;
1390 }
1391 }
1392
1393 arprequest(ifp, &sip, &tip, slladdr, tlladdr);
1394 }
1395
1396
1397 static void
1398 arp_llinfo_missed(struct ifnet *ifp, const union l3addr *taddr,
1399 __unused int16_t type, struct mbuf *m)
1400 {
1401 struct in_addr mdaddr = zeroin_addr;
1402 struct sockaddr_in dsin, tsin;
1403 struct sockaddr *sa;
1404
1405 if (m != NULL) {
1406 struct ip *ip = mtod(m, struct ip *);
1407
1408 if (sizeof(*ip) < m->m_len)
1409 mdaddr = ip->ip_src;
1410
1411 /* ip_input() will send ICMP_UNREACH_HOST, not us. */
1412 m_freem(m);
1413 }
1414
1415 if (mdaddr.s_addr != INADDR_ANY) {
1416 sockaddr_in_init(&dsin, &mdaddr, 0);
1417 sa = sintosa(&dsin);
1418 } else
1419 sa = NULL;
1420
1421 sockaddr_in_init(&tsin, &taddr->addr4, 0);
1422 rt_clonedmsg(RTM_MISS, sa, sintosa(&tsin), NULL, ifp);
1423 }
1424
1425 static void
1426 arp_free(struct llentry *ln, int gc)
1427 {
1428 struct ifnet *ifp;
1429
1430 KASSERT(ln != NULL);
1431 LLE_WLOCK_ASSERT(ln);
1432
1433 ifp = ln->lle_tbl->llt_ifp;
1434
1435 if (ln->la_flags & LLE_VALID || gc) {
1436 struct sockaddr_in sin;
1437 const char *lladdr;
1438
1439 sockaddr_in_init(&sin, &ln->r_l3addr.addr4, 0);
1440 lladdr = ln->la_flags & LLE_VALID ?
1441 (const char *)&ln->ll_addr : NULL;
1442 rt_clonedmsg(RTM_DELETE, NULL, sintosa(&sin), lladdr, ifp);
1443 }
1444
1445 /*
1446 * Save to unlock. We still hold an extra reference and will not
1447 * free(9) in llentry_free() if someone else holds one as well.
1448 */
1449 LLE_WUNLOCK(ln);
1450 IF_AFDATA_LOCK(ifp);
1451 LLE_WLOCK(ln);
1452
1453 lltable_free_entry(LLTABLE(ifp), ln);
1454
1455 IF_AFDATA_UNLOCK(ifp);
1456 }
1457
1458 /*
1459 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1460 *
1461 * XXX cost-effective methods?
1462 */
1463 void
1464 arp_nud_hint(struct rtentry *rt)
1465 {
1466 struct llentry *ln;
1467 struct ifnet *ifp;
1468
1469 if (rt == NULL)
1470 return;
1471
1472 ifp = rt->rt_ifp;
1473 ln = arplookup(ifp, NULL, rt_getkey(rt), 1);
1474 nd_nud_hint(ln);
1475 }
1476
1477 TAILQ_HEAD(dadq_head, dadq);
1478 struct dadq {
1479 TAILQ_ENTRY(dadq) dad_list;
1480 struct ifaddr *dad_ifa;
1481 int dad_count; /* max ARP to send */
1482 int dad_arp_tcount; /* # of trials to send ARP */
1483 int dad_arp_ocount; /* ARP sent so far */
1484 int dad_arp_announce; /* max ARP announcements */
1485 int dad_arp_acount; /* # of announcements */
1486 struct callout dad_timer_ch;
1487 };
1488
1489 static struct dadq_head dadq;
1490 static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */
1491 static kmutex_t arp_dad_lock;
1492
1493 static void
1494 arp_dad_init(void)
1495 {
1496
1497 TAILQ_INIT(&dadq);
1498 mutex_init(&arp_dad_lock, MUTEX_DEFAULT, IPL_NONE);
1499 }
1500
1501 static struct dadq *
1502 arp_dad_find(struct ifaddr *ifa)
1503 {
1504 struct dadq *dp;
1505
1506 KASSERT(mutex_owned(&arp_dad_lock));
1507
1508 TAILQ_FOREACH(dp, &dadq, dad_list) {
1509 if (dp->dad_ifa == ifa)
1510 return dp;
1511 }
1512 return NULL;
1513 }
1514
1515 static void
1516 arp_dad_starttimer(struct dadq *dp, int ticks)
1517 {
1518
1519 callout_reset(&dp->dad_timer_ch, ticks,
1520 (void (*)(void *))arp_dad_timer, dp);
1521 }
1522
1523 static void
1524 arp_dad_stoptimer(struct dadq *dp)
1525 {
1526
1527 KASSERT(mutex_owned(&arp_dad_lock));
1528
1529 TAILQ_REMOVE(&dadq, dp, dad_list);
1530 /* Tell the timer that dp is being destroyed. */
1531 dp->dad_ifa = NULL;
1532 callout_halt(&dp->dad_timer_ch, &arp_dad_lock);
1533 }
1534
1535 static void
1536 arp_dad_destroytimer(struct dadq *dp)
1537 {
1538
1539 callout_destroy(&dp->dad_timer_ch);
1540 KASSERT(dp->dad_ifa == NULL);
1541 kmem_intr_free(dp, sizeof(*dp));
1542 }
1543
1544 static void
1545 arp_dad_output(struct dadq *dp, struct ifaddr *ifa)
1546 {
1547 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1548 struct ifnet *ifp = ifa->ifa_ifp;
1549 struct in_addr sip;
1550
1551 dp->dad_arp_tcount++;
1552 if ((ifp->if_flags & IFF_UP) == 0)
1553 return;
1554 if ((ifp->if_flags & IFF_RUNNING) == 0)
1555 return;
1556
1557 dp->dad_arp_tcount = 0;
1558 dp->dad_arp_ocount++;
1559
1560 memset(&sip, 0, sizeof(sip));
1561 arprequest(ifa->ifa_ifp, &sip, &ia->ia_addr.sin_addr,
1562 CLLADDR(ifa->ifa_ifp->if_sadl), NULL);
1563 }
1564
1565 /*
1566 * Start Duplicate Address Detection (DAD) for specified interface address.
1567 */
1568 static void
1569 arp_dad_start(struct ifaddr *ifa)
1570 {
1571 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1572 struct dadq *dp;
1573 char ipbuf[INET_ADDRSTRLEN];
1574
1575 /*
1576 * If we don't need DAD, don't do it.
1577 * - DAD is disabled
1578 */
1579 if (!(ia->ia4_flags & IN_IFF_TENTATIVE)) {
1580 log(LOG_DEBUG,
1581 "%s: called with non-tentative address %s(%s)\n", __func__,
1582 IN_PRINT(ipbuf, &ia->ia_addr.sin_addr),
1583 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1584 return;
1585 }
1586 if (!ip_dad_enabled()) {
1587 ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1588 rt_addrmsg(RTM_NEWADDR, ifa);
1589 arpannounce1(ifa);
1590 return;
1591 }
1592 KASSERT(ifa->ifa_ifp != NULL);
1593 if (!(ifa->ifa_ifp->if_flags & IFF_UP))
1594 return;
1595
1596 dp = kmem_intr_alloc(sizeof(*dp), KM_NOSLEEP);
1597
1598 mutex_enter(&arp_dad_lock);
1599 if (arp_dad_find(ifa) != NULL) {
1600 mutex_exit(&arp_dad_lock);
1601 /* DAD already in progress */
1602 if (dp != NULL)
1603 kmem_intr_free(dp, sizeof(*dp));
1604 return;
1605 }
1606
1607 if (dp == NULL) {
1608 mutex_exit(&arp_dad_lock);
1609 log(LOG_ERR, "%s: memory allocation failed for %s(%s)\n",
1610 __func__, IN_PRINT(ipbuf, &ia->ia_addr.sin_addr),
1611 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1612 return;
1613 }
1614
1615 /*
1616 * Send ARP packet for DAD, ip_dad_count times.
1617 * Note that we must delay the first transmission.
1618 */
1619 callout_init(&dp->dad_timer_ch, CALLOUT_MPSAFE);
1620 dp->dad_ifa = ifa;
1621 ifaref(ifa); /* just for safety */
1622 dp->dad_count = ip_dad_count;
1623 dp->dad_arp_announce = 0; /* Will be set when starting to announce */
1624 dp->dad_arp_acount = dp->dad_arp_ocount = dp->dad_arp_tcount = 0;
1625 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1626
1627 ARPLOG(LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1628 ARPLOGADDR(&ia->ia_addr.sin_addr));
1629
1630 arp_dad_starttimer(dp, cprng_fast32() % (PROBE_WAIT * hz));
1631
1632 mutex_exit(&arp_dad_lock);
1633 }
1634
1635 /*
1636 * terminate DAD unconditionally. used for address removals.
1637 */
1638 static void
1639 arp_dad_stop(struct ifaddr *ifa)
1640 {
1641 struct dadq *dp;
1642
1643 mutex_enter(&arp_dad_lock);
1644 dp = arp_dad_find(ifa);
1645 if (dp == NULL) {
1646 mutex_exit(&arp_dad_lock);
1647 /* DAD wasn't started yet */
1648 return;
1649 }
1650
1651 arp_dad_stoptimer(dp);
1652
1653 mutex_exit(&arp_dad_lock);
1654
1655 arp_dad_destroytimer(dp);
1656 ifafree(ifa);
1657 }
1658
1659 static void
1660 arp_dad_timer(struct dadq *dp)
1661 {
1662 struct ifaddr *ifa;
1663 struct in_ifaddr *ia;
1664 char ipbuf[INET_ADDRSTRLEN];
1665 bool need_free = false;
1666
1667 KERNEL_LOCK_UNLESS_NET_MPSAFE();
1668 mutex_enter(&arp_dad_lock);
1669
1670 ifa = dp->dad_ifa;
1671 if (ifa == NULL) {
1672 /* dp is being destroyed by someone. Do nothing. */
1673 goto done;
1674 }
1675
1676 ia = (struct in_ifaddr *)ifa;
1677 if (ia->ia4_flags & IN_IFF_DUPLICATED) {
1678 log(LOG_ERR, "%s: called with duplicate address %s(%s)\n",
1679 __func__, IN_PRINT(ipbuf, &ia->ia_addr.sin_addr),
1680 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1681 goto done;
1682 }
1683 if ((ia->ia4_flags & IN_IFF_TENTATIVE) == 0 && dp->dad_arp_acount == 0)
1684 {
1685 log(LOG_ERR, "%s: called with non-tentative address %s(%s)\n",
1686 __func__, IN_PRINT(ipbuf, &ia->ia_addr.sin_addr),
1687 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1688 goto done;
1689 }
1690
1691 /* timeouted with IFF_{RUNNING,UP} check */
1692 if (dp->dad_arp_tcount > dad_maxtry) {
1693 ARPLOG(LOG_INFO, "%s: could not run DAD, driver problem?\n",
1694 if_name(ifa->ifa_ifp));
1695
1696 arp_dad_stoptimer(dp);
1697 need_free = true;
1698 goto done;
1699 }
1700
1701 /* Need more checks? */
1702 if (dp->dad_arp_ocount < dp->dad_count) {
1703 int adelay;
1704
1705 /*
1706 * We have more ARP to go. Send ARP packet for DAD.
1707 */
1708 arp_dad_output(dp, ifa);
1709 if (dp->dad_arp_ocount < dp->dad_count)
1710 adelay = (PROBE_MIN * hz) +
1711 (cprng_fast32() %
1712 ((PROBE_MAX * hz) - (PROBE_MIN * hz)));
1713 else
1714 adelay = ANNOUNCE_WAIT * hz;
1715 arp_dad_starttimer(dp, adelay);
1716 goto done;
1717 } else if (dp->dad_arp_acount == 0) {
1718 /*
1719 * We are done with DAD.
1720 * No duplicate address found.
1721 */
1722 ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1723 rt_addrmsg(RTM_NEWADDR, ifa);
1724 ARPLOG(LOG_DEBUG,
1725 "%s: DAD complete for %s - no duplicates found\n",
1726 if_name(ifa->ifa_ifp), ARPLOGADDR(&ia->ia_addr.sin_addr));
1727 dp->dad_arp_announce = ANNOUNCE_NUM;
1728 goto announce;
1729 } else if (dp->dad_arp_acount < dp->dad_arp_announce) {
1730 announce:
1731 /*
1732 * Announce the address.
1733 */
1734 arpannounce1(ifa);
1735 dp->dad_arp_acount++;
1736 if (dp->dad_arp_acount < dp->dad_arp_announce) {
1737 arp_dad_starttimer(dp, ANNOUNCE_INTERVAL * hz);
1738 goto done;
1739 }
1740 ARPLOG(LOG_DEBUG,
1741 "%s: ARP announcement complete for %s\n",
1742 if_name(ifa->ifa_ifp), ARPLOGADDR(&ia->ia_addr.sin_addr));
1743 }
1744
1745 arp_dad_stoptimer(dp);
1746 need_free = true;
1747 done:
1748 mutex_exit(&arp_dad_lock);
1749
1750 if (need_free) {
1751 arp_dad_destroytimer(dp);
1752 KASSERT(ifa != NULL);
1753 ifafree(ifa);
1754 }
1755
1756 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
1757 }
1758
1759 static void
1760 arp_dad_duplicated(struct ifaddr *ifa, const struct sockaddr_dl *from)
1761 {
1762 struct in_ifaddr *ia = ifatoia(ifa);
1763 struct ifnet *ifp = ifa->ifa_ifp;
1764 char ipbuf[INET_ADDRSTRLEN], llabuf[LLA_ADDRSTRLEN];
1765 const char *iastr, *llastr;
1766
1767 iastr = IN_PRINT(ipbuf, &ia->ia_addr.sin_addr);
1768 if (__predict_false(from == NULL))
1769 llastr = NULL;
1770 else
1771 llastr = lla_snprintf(llabuf, sizeof(llabuf),
1772 CLLADDR(from), from->sdl_alen);
1773
1774 if (ia->ia4_flags & (IN_IFF_TENTATIVE|IN_IFF_DUPLICATED)) {
1775 log(LOG_ERR,
1776 "%s: DAD duplicate address %s from %s\n",
1777 if_name(ifp), iastr, llastr);
1778 } else if (ia->ia_dad_defended == 0 ||
1779 ia->ia_dad_defended < time_uptime - DEFEND_INTERVAL) {
1780 ia->ia_dad_defended = time_uptime;
1781 arpannounce1(ifa);
1782 log(LOG_ERR,
1783 "%s: DAD defended address %s from %s\n",
1784 if_name(ifp), iastr, llastr);
1785 return;
1786 } else {
1787 /* If DAD is disabled, just report the duplicate. */
1788 if (!ip_dad_enabled()) {
1789 log(LOG_ERR,
1790 "%s: DAD ignoring duplicate address %s from %s\n",
1791 if_name(ifp), iastr, llastr);
1792 return;
1793 }
1794 log(LOG_ERR,
1795 "%s: DAD defence failed for %s from %s\n",
1796 if_name(ifp), iastr, llastr);
1797 }
1798
1799 arp_dad_stop(ifa);
1800
1801 ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1802 if ((ia->ia4_flags & IN_IFF_DUPLICATED) == 0) {
1803 ia->ia4_flags |= IN_IFF_DUPLICATED;
1804 /* Inform the routing socket of the duplicate address */
1805 rt_addrmsg_src(RTM_NEWADDR, ifa, (const struct sockaddr *)from);
1806 }
1807 }
1808
1809 /*
1810 * Called from 10 Mb/s Ethernet interrupt handlers
1811 * when ether packet type ETHERTYPE_REVARP
1812 * is received. Common length and type checks are done here,
1813 * then the protocol-specific routine is called.
1814 */
1815 void
1816 revarpinput(struct mbuf *m)
1817 {
1818 struct arphdr *ar;
1819 int arplen;
1820
1821 arplen = sizeof(struct arphdr);
1822 if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL)
1823 return;
1824 ar = mtod(m, struct arphdr *);
1825
1826 if (ntohs(ar->ar_hrd) == ARPHRD_IEEE1394) {
1827 goto out;
1828 }
1829
1830 arplen = sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln);
1831 if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL)
1832 return;
1833 ar = mtod(m, struct arphdr *);
1834
1835 switch (ntohs(ar->ar_pro)) {
1836 case ETHERTYPE_IP:
1837 case ETHERTYPE_IPTRAILERS:
1838 in_revarpinput(m);
1839 return;
1840
1841 default:
1842 break;
1843 }
1844
1845 out:
1846 m_freem(m);
1847 }
1848
1849 /*
1850 * RARP for Internet protocols on 10 Mb/s Ethernet.
1851 * Algorithm is that given in RFC 903.
1852 * We are only using for bootstrap purposes to get an ip address for one of
1853 * our interfaces. Thus we support no user-interface.
1854 *
1855 * Since the contents of the RARP reply are specific to the interface that
1856 * sent the request, this code must ensure that they are properly associated.
1857 *
1858 * Note: also supports ARP via RARP packets, per the RFC.
1859 */
1860 void
1861 in_revarpinput(struct mbuf *m)
1862 {
1863 struct arphdr *ah;
1864 void *tha;
1865 int op;
1866 struct ifnet *rcvif;
1867 int s;
1868
1869 ah = mtod(m, struct arphdr *);
1870 op = ntohs(ah->ar_op);
1871
1872 rcvif = m_get_rcvif(m, &s);
1873 if (__predict_false(rcvif == NULL))
1874 goto out;
1875 if (rcvif->if_flags & IFF_NOARP)
1876 goto out;
1877
1878 switch (rcvif->if_type) {
1879 case IFT_IEEE1394:
1880 /* ARP without target hardware address is not supported */
1881 goto out;
1882 default:
1883 break;
1884 }
1885
1886 switch (op) {
1887 case ARPOP_REQUEST:
1888 case ARPOP_REPLY: /* per RFC */
1889 m_put_rcvif(rcvif, &s);
1890 in_arpinput(m);
1891 return;
1892 case ARPOP_REVREPLY:
1893 break;
1894 case ARPOP_REVREQUEST: /* handled by rarpd(8) */
1895 default:
1896 goto out;
1897 }
1898 if (!revarp_in_progress)
1899 goto out;
1900 if (rcvif != myip_ifp) /* !same interface */
1901 goto out;
1902 if (myip_initialized)
1903 goto wake;
1904 tha = ar_tha(ah);
1905 if (tha == NULL)
1906 goto out;
1907 if (ah->ar_pln != sizeof(struct in_addr))
1908 goto out;
1909 if (ah->ar_hln != rcvif->if_sadl->sdl_alen)
1910 goto out;
1911 if (memcmp(tha, CLLADDR(rcvif->if_sadl), rcvif->if_sadl->sdl_alen))
1912 goto out;
1913 memcpy(&srv_ip, ar_spa(ah), sizeof(srv_ip));
1914 memcpy(&myip, ar_tpa(ah), sizeof(myip));
1915 myip_initialized = 1;
1916 wake: /* Do wakeup every time in case it was missed. */
1917 wakeup((void *)&myip);
1918
1919 out:
1920 m_put_rcvif(rcvif, &s);
1921 m_freem(m);
1922 }
1923
1924 /*
1925 * Send a RARP request for the ip address of the specified interface.
1926 * The request should be RFC 903-compliant.
1927 */
1928 static void
1929 revarprequest(struct ifnet *ifp)
1930 {
1931 struct sockaddr sa;
1932 struct mbuf *m;
1933 struct arphdr *ah;
1934 void *tha;
1935
1936 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
1937 return;
1938 MCLAIM(m, &arpdomain.dom_mowner);
1939 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
1940 2*ifp->if_addrlen;
1941 m->m_pkthdr.len = m->m_len;
1942 m_align(m, m->m_len);
1943 ah = mtod(m, struct arphdr *);
1944 memset(ah, 0, m->m_len);
1945 ah->ar_pro = htons(ETHERTYPE_IP);
1946 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
1947 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
1948 ah->ar_op = htons(ARPOP_REVREQUEST);
1949
1950 memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
1951 tha = ar_tha(ah);
1952 if (tha == NULL) {
1953 m_free(m);
1954 return;
1955 }
1956 memcpy(tha, CLLADDR(ifp->if_sadl), ah->ar_hln);
1957
1958 sa.sa_family = AF_ARP;
1959 sa.sa_len = 2;
1960 m->m_flags |= M_BCAST;
1961
1962 if_output_lock(ifp, ifp, m, &sa, NULL);
1963 }
1964
1965 /*
1966 * RARP for the ip address of the specified interface, but also
1967 * save the ip address of the server that sent the answer.
1968 * Timeout if no response is received.
1969 */
1970 int
1971 revarpwhoarewe(struct ifnet *ifp, struct in_addr *serv_in,
1972 struct in_addr *clnt_in)
1973 {
1974 int result, count = 20;
1975
1976 myip_initialized = 0;
1977 myip_ifp = ifp;
1978
1979 revarp_in_progress = 1;
1980 while (count--) {
1981 revarprequest(ifp);
1982 result = tsleep((void *)&myip, PSOCK, "revarp", hz/2);
1983 if (result != EWOULDBLOCK)
1984 break;
1985 }
1986 revarp_in_progress = 0;
1987
1988 if (!myip_initialized)
1989 return ENETUNREACH;
1990
1991 memcpy(serv_in, &srv_ip, sizeof(*serv_in));
1992 memcpy(clnt_in, &myip, sizeof(*clnt_in));
1993 return 0;
1994 }
1995
1996 void
1997 arp_stat_add(int type, uint64_t count)
1998 {
1999 ARP_STATADD(type, count);
2000 }
2001
2002 static int
2003 sysctl_net_inet_arp_stats(SYSCTLFN_ARGS)
2004 {
2005
2006 return NETSTAT_SYSCTL(arpstat_percpu, ARP_NSTATS);
2007 }
2008
2009 static void
2010 sysctl_net_inet_arp_setup(struct sysctllog **clog)
2011 {
2012 const struct sysctlnode *node;
2013
2014 sysctl_createv(clog, 0, NULL, NULL,
2015 CTLFLAG_PERMANENT,
2016 CTLTYPE_NODE, "inet", NULL,
2017 NULL, 0, NULL, 0,
2018 CTL_NET, PF_INET, CTL_EOL);
2019 sysctl_createv(clog, 0, NULL, &node,
2020 CTLFLAG_PERMANENT,
2021 CTLTYPE_NODE, "arp",
2022 SYSCTL_DESCR("Address Resolution Protocol"),
2023 NULL, 0, NULL, 0,
2024 CTL_NET, PF_INET, CTL_CREATE, CTL_EOL);
2025
2026 sysctl_createv(clog, 0, NULL, NULL,
2027 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2028 CTLTYPE_INT, "nd_delay",
2029 SYSCTL_DESCR("First probe delay time"),
2030 NULL, 0, &arp_nd_domain.nd_delay, 0,
2031 CTL_NET, PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2032 sysctl_createv(clog, 0, NULL, NULL,
2033 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2034 CTLTYPE_INT, "nd_bmaxtries",
2035 SYSCTL_DESCR("Number of broadcast discovery attempts"),
2036 NULL, 0, &arp_nd_domain.nd_mmaxtries, 0,
2037 CTL_NET, PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2038 sysctl_createv(clog, 0, NULL, NULL,
2039 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2040 CTLTYPE_INT, "nd_umaxtries",
2041 SYSCTL_DESCR("Number of unicast discovery attempts"),
2042 NULL, 0, &arp_nd_domain.nd_umaxtries, 0,
2043 CTL_NET, PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2044 sysctl_createv(clog, 0, NULL, NULL,
2045 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2046 CTLTYPE_INT, "nd_reachable",
2047 SYSCTL_DESCR("Reachable time"),
2048 NULL, 0, &arp_reachable, 0,
2049 CTL_NET, PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2050 sysctl_createv(clog, 0, NULL, NULL,
2051 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2052 CTLTYPE_INT, "nd_retrans",
2053 SYSCTL_DESCR("Retransmission time"),
2054 NULL, 0, &arp_retrans, 0,
2055 CTL_NET, PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2056 sysctl_createv(clog, 0, NULL, NULL,
2057 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2058 CTLTYPE_INT, "nd_nud",
2059 SYSCTL_DESCR("Perform neighbour unreachability detection"),
2060 NULL, 0, &arp_perform_nud, 0,
2061 CTL_NET, PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2062 sysctl_createv(clog, 0, NULL, NULL,
2063 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2064 CTLTYPE_INT, "nd_maxnudhint",
2065 SYSCTL_DESCR("Maximum neighbor unreachable hint count"),
2066 NULL, 0, &arp_nd_domain.nd_maxnudhint, 0,
2067 CTL_NET, PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2068 sysctl_createv(clog, 0, NULL, NULL,
2069 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2070 CTLTYPE_INT, "maxqueuelen",
2071 SYSCTL_DESCR("max packet queue len for a unresolved ARP"),
2072 NULL, 1, &arp_nd_domain.nd_maxqueuelen, 0,
2073 CTL_NET, PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2074
2075 sysctl_createv(clog, 0, NULL, NULL,
2076 CTLFLAG_PERMANENT,
2077 CTLTYPE_STRUCT, "stats",
2078 SYSCTL_DESCR("ARP statistics"),
2079 sysctl_net_inet_arp_stats, 0, NULL, 0,
2080 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2081
2082 sysctl_createv(clog, 0, NULL, NULL,
2083 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2084 CTLTYPE_INT, "log_movements",
2085 SYSCTL_DESCR("log ARP replies from MACs different than"
2086 " the one in the cache"),
2087 NULL, 0, &log_movements, 0,
2088 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2089
2090 sysctl_createv(clog, 0, NULL, NULL,
2091 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2092 CTLTYPE_INT, "log_permanent_modify",
2093 SYSCTL_DESCR("log ARP replies from MACs different than"
2094 " the one in the permanent arp entry"),
2095 NULL, 0, &log_permanent_modify, 0,
2096 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2097
2098 sysctl_createv(clog, 0, NULL, NULL,
2099 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2100 CTLTYPE_INT, "log_wrong_iface",
2101 SYSCTL_DESCR("log ARP packets arriving on the wrong"
2102 " interface"),
2103 NULL, 0, &log_wrong_iface, 0,
2104 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2105
2106 sysctl_createv(clog, 0, NULL, NULL,
2107 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2108 CTLTYPE_INT, "debug",
2109 SYSCTL_DESCR("Enable ARP DAD debug output"),
2110 NULL, 0, &arp_debug, 0,
2111 CTL_NET, PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2112 }
2113
2114 #endif /* INET */
2115