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