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