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