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