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