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