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