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