1/*
2 * Copyright (c) 2011-2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29/*
30 * Link-layer Reachability Record
31 *
32 * Each interface maintains a red-black tree which contains records related
33 * to the on-link nodes which we are interested in communicating with.  Each
34 * record gets allocated and inserted into the tree in the following manner:
35 * upon processing an ARP announcement or reply from a known node (i.e. there
36 * exists a ARP route entry for the node), and if a link-layer reachability
37 * record for the node doesn't yet exist; and, upon processing a ND6 RS/RA/
38 * NS/NA/redirect from a node, and if a link-layer reachability record for the
39 * node doesn't yet exist.
40 *
41 * Each newly created record is then referred to by the resolver route entry;
42 * if a record already exists, its reference count gets increased for the new
43 * resolver entry which now refers to it.  A record gets removed from the tree
44 * and freed once its reference counts drops to zero, i.e. when there is no
45 * more resolver entry referring to it.
46 *
47 * A record contains the link-layer protocol (e.g. Ethertype IP/IPv6), the
48 * HW address of the sender, the "last heard from" timestamp (lr_lastrcvd) and
49 * the number of references made to it (lr_reqcnt).  Because the key for each
50 * record in the red-black tree consists of the link-layer protocol, therefore
51 * the namespace for the records is partitioned based on the type of link-layer
52 * protocol, i.e. an Ethertype IP link-layer record is only referred to by one
53 * or more ARP entries; an Ethernet IPv6 link-layer record is only referred to
54 * by one or more ND6 entries.  Therefore, lr_reqcnt represents the number of
55 * resolver entry references to the record for the same protocol family.
56 *
57 * Upon receiving packets from the network, the protocol's input callback
58 * (e.g. ether_inet{6}_input) informs the corresponding resolver (ARP/ND6)
59 * about the (link-layer) origin of the packet.  This results in searching
60 * for a matching record in the red-black tree for the interface where the
61 * packet arrived on.  If there's no match, no further processing takes place.
62 * Otherwise, the lr_lastrcvd timestamp of the record is updated.
63 *
64 * When an IP/IPv6 packet is transmitted to the resolver (i.e. the destination
65 * is on-link), ARP/ND6 records the "last spoken to" timestamp in the route
66 * entry ({la,ln}_lastused).
67 *
68 * The reachability of the on-link node is determined by the following logic,
69 * upon sending a packet thru the resolver:
70 *
71 *   a) If the record is only used by exactly one resolver entry (lr_reqcnt
72 *	is 1), i.e. the target host does not have IP/IPv6 aliases that we know
73 *	of, check if lr_lastrcvd is "recent."  If so, simply send the packet;
74 *	otherwise, re-resolve the target node.
75 *
76 *   b) If the record is shared by multiple resolver entries (lr_reqcnt is
77 *	greater than 1), i.e. the target host has more than one IP/IPv6 aliases
78 *	on the same network interface, we can't rely on lr_lastrcvd alone, as
79 *	one of the IP/IPv6 aliases could have been silently moved to another
80 *	node for which we don't have a link-layer record.  If lr_lastrcvd is
81 *	not "recent", we re-resolve the target node.  Otherwise, we perform
82 *	an additional check against {la,ln}_lastused to see whether it is also
83 *	"recent", relative to lr_lastrcvd.  If so, simply send the packet;
84 *	otherwise, re-resolve the target node.
85 *
86 * The value for "recent" is configurable by adjusting the basetime value for
87 * net.link.ether.inet.arp_llreach_base or net.inet6.icmp6.nd6_llreach_base.
88 * The default basetime value is 30 seconds, and the actual expiration time
89 * is calculated by multiplying the basetime value with some random factor,
90 * which results in a number between 15 to 45 seconds.  Setting the basetime
91 * value to 0 effectively disables this feature for the corresponding resolver.
92 *
93 * Assumptions:
94 *
95 * The above logic is based upon the following assumptions:
96 *
97 *   i) Network traffics are mostly bi-directional, i.e. the act of sending
98 *	packets to an on-link node would most likely cause us to receive
99 *	packets from that node.
100 *
101 *  ii) If the on-link node's IP/IPv6 address silently moves to another
102 *	on-link node for which we are not aware of, non-unicast packets
103 *	from the old node would trigger the record's lr_lastrcvd to be
104 *	kept recent.
105 *
106 * We can mitigate the above by having the resolver check its {la,ln}_lastused
107 * timestamp at all times, i.e. not only when lr_reqcnt is greater than 1; but
108 * we currently optimize for the common cases.
109 */
110
111#include <sys/param.h>
112#include <sys/systm.h>
113#include <sys/kernel.h>
114#include <sys/malloc.h>
115#include <sys/tree.h>
116#include <sys/sysctl.h>
117#include <sys/mcache.h>
118#include <sys/protosw.h>
119
120#include <net/if_dl.h>
121#include <net/if.h>
122#include <net/if_var.h>
123#include <net/if_llreach.h>
124#include <net/dlil.h>
125#include <net/kpi_interface.h>
126#include <net/route.h>
127
128#include <kern/assert.h>
129#include <kern/locks.h>
130#include <kern/zalloc.h>
131
132#if INET6
133#include <netinet6/in6_var.h>
134#include <netinet6/nd6.h>
135#endif /* INET6 */
136
137static unsigned int iflr_size;		/* size of if_llreach */
138static struct zone *iflr_zone;		/* zone for if_llreach */
139
140#define	IFLR_ZONE_MAX		128		/* maximum elements in zone */
141#define	IFLR_ZONE_NAME		"if_llreach"	/* zone name */
142
143static struct if_llreach *iflr_alloc(int);
144static void iflr_free(struct if_llreach *);
145static __inline int iflr_cmp(const struct if_llreach *,
146    const struct if_llreach *);
147static __inline int iflr_reachable(struct if_llreach *, int, u_int64_t);
148static int sysctl_llreach_ifinfo SYSCTL_HANDLER_ARGS;
149
150/* The following is protected by if_llreach_lock */
151RB_GENERATE_PREV(ll_reach_tree, if_llreach, lr_link, iflr_cmp);
152
153SYSCTL_DECL(_net_link_generic_system);
154
155SYSCTL_NODE(_net_link_generic_system, OID_AUTO, llreach_info,
156    CTLFLAG_RD | CTLFLAG_LOCKED, sysctl_llreach_ifinfo,
157    "Per-interface tree of source link-layer reachability records");
158
159/*
160 * Link-layer reachability is based off node constants in RFC4861.
161 */
162#if INET6
163#define	LL_COMPUTE_RTIME(x)	ND_COMPUTE_RTIME(x)
164#else
165#define LL_MIN_RANDOM_FACTOR	512	/* 1024 * 0.5 */
166#define LL_MAX_RANDOM_FACTOR	1536	/* 1024 * 1.5 */
167#define LL_COMPUTE_RTIME(x)						\
168	(((LL_MIN_RANDOM_FACTOR * (x >> 10)) + (random() &		\
169	((LL_MAX_RANDOM_FACTOR - LL_MIN_RANDOM_FACTOR) * (x >> 10)))) / 1000)
170#endif /* !INET6 */
171
172void
173ifnet_llreach_init(void)
174{
175	iflr_size = sizeof (struct if_llreach);
176	iflr_zone = zinit(iflr_size,
177	    IFLR_ZONE_MAX * iflr_size, 0, IFLR_ZONE_NAME);
178	if (iflr_zone == NULL) {
179		panic("%s: failed allocating %s", __func__, IFLR_ZONE_NAME);
180		/* NOTREACHED */
181	}
182	zone_change(iflr_zone, Z_EXPAND, TRUE);
183	zone_change(iflr_zone, Z_CALLERACCT, FALSE);
184}
185
186void
187ifnet_llreach_ifattach(struct ifnet *ifp, boolean_t reuse)
188{
189	lck_rw_lock_exclusive(&ifp->if_llreach_lock);
190	/* Initialize link-layer source tree (if not already) */
191	if (!reuse)
192		RB_INIT(&ifp->if_ll_srcs);
193	lck_rw_done(&ifp->if_llreach_lock);
194}
195
196void
197ifnet_llreach_ifdetach(struct ifnet *ifp)
198{
199#pragma unused(ifp)
200	/*
201	 * Nothing to do for now; the link-layer source tree might
202	 * contain entries at this point, that are still referred
203	 * to by route entries pointing to this ifp.
204	 */
205}
206
207/*
208 * Link-layer source tree comparison function.
209 *
210 * An ordered predicate is necessary; bcmp() is not documented to return
211 * an indication of order, memcmp() is, and is an ISO C99 requirement.
212 */
213static __inline int
214iflr_cmp(const struct if_llreach *a, const struct if_llreach *b)
215{
216	return (memcmp(&a->lr_key, &b->lr_key, sizeof (a->lr_key)));
217}
218
219static __inline int
220iflr_reachable(struct if_llreach *lr, int cmp_delta, u_int64_t tval)
221{
222	u_int64_t now;
223	u_int64_t expire;
224
225	now = net_uptime();		/* current approx. uptime */
226	/*
227	 * No need for lr_lock; atomically read the last rcvd uptime.
228	 */
229	expire = lr->lr_lastrcvd + lr->lr_reachable;
230	/*
231	 * If we haven't heard back from the local host for over
232	 * lr_reachable seconds, consider that the host is no
233	 * longer reachable.
234	 */
235	if (!cmp_delta)
236		return (expire >= now);
237	/*
238	 * If the caller supplied a reference time, consider the
239	 * host is reachable if the record hasn't expired (see above)
240	 * and if the reference time is within the past lr_reachable
241	 * seconds.
242	 */
243	return ((expire >= now) && (now - tval) < lr->lr_reachable);
244}
245
246int
247ifnet_llreach_reachable(struct if_llreach *lr)
248{
249	/*
250	 * Check whether the cache is too old to be trusted.
251	 */
252	return (iflr_reachable(lr, 0, 0));
253}
254
255int
256ifnet_llreach_reachable_delta(struct if_llreach *lr, u_int64_t tval)
257{
258	/*
259	 * Check whether the cache is too old to be trusted.
260	 */
261	return (iflr_reachable(lr, 1, tval));
262}
263
264void
265ifnet_llreach_set_reachable(struct ifnet *ifp, u_int16_t llproto, void *addr,
266    unsigned int alen)
267{
268	struct if_llreach find, *lr;
269
270	VERIFY(alen == IF_LLREACH_MAXLEN);	/* for now */
271
272	find.lr_key.proto = llproto;
273	bcopy(addr, &find.lr_key.addr, IF_LLREACH_MAXLEN);
274
275	lck_rw_lock_shared(&ifp->if_llreach_lock);
276	lr = RB_FIND(ll_reach_tree, &ifp->if_ll_srcs, &find);
277	if (lr == NULL) {
278		lck_rw_done(&ifp->if_llreach_lock);
279		return;
280	}
281	/*
282	 * No need for lr_lock; atomically update the last rcvd uptime.
283	 */
284	lr->lr_lastrcvd = net_uptime();
285	lck_rw_done(&ifp->if_llreach_lock);
286}
287
288struct if_llreach *
289ifnet_llreach_alloc(struct ifnet *ifp, u_int16_t llproto, void *addr,
290    unsigned int alen, u_int64_t llreach_base)
291{
292	struct if_llreach find, *lr;
293	struct timeval now;
294
295	if (llreach_base == 0)
296		return (NULL);
297
298	VERIFY(alen == IF_LLREACH_MAXLEN);	/* for now */
299
300	find.lr_key.proto = llproto;
301	bcopy(addr, &find.lr_key.addr, IF_LLREACH_MAXLEN);
302
303	lck_rw_lock_shared(&ifp->if_llreach_lock);
304	lr = RB_FIND(ll_reach_tree, &ifp->if_ll_srcs, &find);
305	if (lr != NULL) {
306found:
307		IFLR_LOCK(lr);
308		VERIFY(lr->lr_reqcnt >= 1);
309		lr->lr_reqcnt++;
310		VERIFY(lr->lr_reqcnt != 0);
311		IFLR_ADDREF_LOCKED(lr);		/* for caller */
312		lr->lr_lastrcvd = net_uptime();	/* current approx. uptime */
313		IFLR_UNLOCK(lr);
314		lck_rw_done(&ifp->if_llreach_lock);
315		return (lr);
316	}
317
318	if (!lck_rw_lock_shared_to_exclusive(&ifp->if_llreach_lock))
319		lck_rw_lock_exclusive(&ifp->if_llreach_lock);
320
321	lck_rw_assert(&ifp->if_llreach_lock, LCK_RW_ASSERT_EXCLUSIVE);
322
323	/* in case things have changed while becoming writer */
324	lr = RB_FIND(ll_reach_tree, &ifp->if_ll_srcs, &find);
325	if (lr != NULL)
326		goto found;
327
328	lr = iflr_alloc(M_WAITOK);
329	if (lr == NULL) {
330		lck_rw_done(&ifp->if_llreach_lock);
331		return (NULL);
332	}
333	IFLR_LOCK(lr);
334	lr->lr_reqcnt++;
335	VERIFY(lr->lr_reqcnt == 1);
336	IFLR_ADDREF_LOCKED(lr);			/* for RB tree */
337	IFLR_ADDREF_LOCKED(lr);			/* for caller */
338	lr->lr_lastrcvd = net_uptime();		/* current approx. uptime */
339	lr->lr_baseup = lr->lr_lastrcvd;	/* base uptime */
340	microtime(&now);
341	lr->lr_basecal = now.tv_sec;		/* base calendar time */
342	lr->lr_basereachable = llreach_base;
343	lr->lr_reachable = LL_COMPUTE_RTIME(lr->lr_basereachable * 1000);
344	lr->lr_debug |= IFD_ATTACHED;
345	lr->lr_ifp = ifp;
346	lr->lr_key.proto = llproto;
347	bcopy(addr, &lr->lr_key.addr, IF_LLREACH_MAXLEN);
348	lr->lr_rssi = IFNET_RSSI_UNKNOWN;
349	lr->lr_lqm = IFNET_LQM_THRESH_UNKNOWN;
350	lr->lr_npm = IFNET_NPM_THRESH_UNKNOWN;
351	RB_INSERT(ll_reach_tree, &ifp->if_ll_srcs, lr);
352	IFLR_UNLOCK(lr);
353	lck_rw_done(&ifp->if_llreach_lock);
354
355	return (lr);
356}
357
358void
359ifnet_llreach_free(struct if_llreach *lr)
360{
361	struct ifnet *ifp;
362
363	/* no need to lock here; lr_ifp never changes */
364	ifp = lr->lr_ifp;
365
366	lck_rw_lock_exclusive(&ifp->if_llreach_lock);
367	IFLR_LOCK(lr);
368	if (lr->lr_reqcnt == 0) {
369		panic("%s: lr=%p negative reqcnt", __func__, lr);
370		/* NOTREACHED */
371	}
372	--lr->lr_reqcnt;
373	if (lr->lr_reqcnt > 0) {
374		IFLR_UNLOCK(lr);
375		lck_rw_done(&ifp->if_llreach_lock);
376		IFLR_REMREF(lr);		/* for caller */
377		return;
378	}
379	if (!(lr->lr_debug & IFD_ATTACHED)) {
380		panic("%s: Attempt to detach an unattached llreach lr=%p",
381		    __func__, lr);
382		/* NOTREACHED */
383	}
384	lr->lr_debug &= ~IFD_ATTACHED;
385	RB_REMOVE(ll_reach_tree, &ifp->if_ll_srcs, lr);
386	IFLR_UNLOCK(lr);
387	lck_rw_done(&ifp->if_llreach_lock);
388
389	IFLR_REMREF(lr);			/* for RB tree */
390	IFLR_REMREF(lr);			/* for caller */
391}
392
393u_int64_t
394ifnet_llreach_up2calexp(struct if_llreach *lr, u_int64_t uptime)
395{
396	u_int64_t calendar = 0;
397
398	if (uptime != 0) {
399		struct timeval cnow;
400		u_int64_t unow;
401
402		getmicrotime(&cnow);	/* current calendar time */
403		unow = net_uptime();	/* current approx. uptime */
404		/*
405		 * Take into account possible calendar time changes;
406		 * adjust base calendar value if necessary, i.e.
407		 * the calendar skew should equate to the uptime skew.
408		 */
409		lr->lr_basecal += (cnow.tv_sec - lr->lr_basecal) -
410		    (unow - lr->lr_baseup);
411
412		calendar = lr->lr_basecal + lr->lr_reachable +
413		    (uptime - lr->lr_baseup);
414	}
415
416	return (calendar);
417}
418
419u_int64_t
420ifnet_llreach_up2upexp(struct if_llreach *lr, u_int64_t uptime)
421{
422	return (lr->lr_reachable + uptime);
423}
424
425int
426ifnet_llreach_get_defrouter(struct ifnet *ifp, int af,
427    struct ifnet_llreach_info *iflri)
428{
429	struct radix_node_head *rnh;
430	struct sockaddr_storage dst_ss, mask_ss;
431	struct rtentry *rt;
432	int error = ESRCH;
433
434	VERIFY(ifp != NULL && iflri != NULL &&
435	    (af == AF_INET || af == AF_INET6));
436
437	bzero(iflri, sizeof (*iflri));
438
439	if ((rnh = rt_tables[af]) == NULL)
440		return (error);
441
442	bzero(&dst_ss, sizeof (dst_ss));
443	bzero(&mask_ss, sizeof (mask_ss));
444	dst_ss.ss_family = af;
445	dst_ss.ss_len = (af == AF_INET) ? sizeof (struct sockaddr_in) :
446	    sizeof (struct sockaddr_in6);
447
448	lck_mtx_lock(rnh_lock);
449	rt = rt_lookup(TRUE, SA(&dst_ss), SA(&mask_ss), rnh, ifp->if_index);
450	if (rt != NULL) {
451		struct rtentry *gwrt;
452
453		RT_LOCK(rt);
454		if ((rt->rt_flags & RTF_GATEWAY) &&
455		    (gwrt = rt->rt_gwroute) != NULL &&
456		    rt_key(rt)->sa_family == rt_key(gwrt)->sa_family &&
457		    (gwrt->rt_flags & RTF_UP)) {
458			RT_UNLOCK(rt);
459			RT_LOCK(gwrt);
460			if (gwrt->rt_llinfo_get_iflri != NULL) {
461				(*gwrt->rt_llinfo_get_iflri)(gwrt, iflri);
462				error = 0;
463			}
464			RT_UNLOCK(gwrt);
465		} else {
466			RT_UNLOCK(rt);
467		}
468		rtfree_locked(rt);
469	}
470	lck_mtx_unlock(rnh_lock);
471
472	return (error);
473}
474
475static struct if_llreach *
476iflr_alloc(int how)
477{
478	struct if_llreach *lr;
479
480	lr = (how == M_WAITOK) ? zalloc(iflr_zone) : zalloc_noblock(iflr_zone);
481	if (lr != NULL) {
482		bzero(lr, iflr_size);
483		lck_mtx_init(&lr->lr_lock, ifnet_lock_group, ifnet_lock_attr);
484		lr->lr_debug |= IFD_ALLOC;
485	}
486	return (lr);
487}
488
489static void
490iflr_free(struct if_llreach *lr)
491{
492	IFLR_LOCK(lr);
493	if (lr->lr_debug & IFD_ATTACHED) {
494		panic("%s: attached lr=%p is being freed", __func__, lr);
495		/* NOTREACHED */
496	} else if (!(lr->lr_debug & IFD_ALLOC)) {
497		panic("%s: lr %p cannot be freed", __func__, lr);
498		/* NOTREACHED */
499	} else if (lr->lr_refcnt != 0) {
500		panic("%s: non-zero refcount lr=%p", __func__, lr);
501		/* NOTREACHED */
502	} else if (lr->lr_reqcnt != 0) {
503		panic("%s: non-zero reqcnt lr=%p", __func__, lr);
504		/* NOTREACHED */
505	}
506	lr->lr_debug &= ~IFD_ALLOC;
507	IFLR_UNLOCK(lr);
508
509	lck_mtx_destroy(&lr->lr_lock, ifnet_lock_group);
510	zfree(iflr_zone, lr);
511}
512
513void
514iflr_addref(struct if_llreach *lr, int locked)
515{
516	if (!locked)
517		IFLR_LOCK(lr);
518	else
519		IFLR_LOCK_ASSERT_HELD(lr);
520
521	if (++lr->lr_refcnt == 0) {
522		panic("%s: lr=%p wraparound refcnt", __func__, lr);
523		/* NOTREACHED */
524	}
525	if (!locked)
526		IFLR_UNLOCK(lr);
527}
528
529void
530iflr_remref(struct if_llreach *lr)
531{
532	IFLR_LOCK(lr);
533	if (lr->lr_refcnt == 0) {
534		panic("%s: lr=%p negative refcnt", __func__, lr);
535		/* NOTREACHED */
536	}
537	--lr->lr_refcnt;
538	if (lr->lr_refcnt > 0) {
539		IFLR_UNLOCK(lr);
540		return;
541	}
542	IFLR_UNLOCK(lr);
543
544	iflr_free(lr);	/* deallocate it */
545}
546
547void
548ifnet_lr2ri(struct if_llreach *lr, struct rt_reach_info *ri)
549{
550	struct if_llreach_info lri;
551
552	IFLR_LOCK_ASSERT_HELD(lr);
553
554	bzero(ri, sizeof (*ri));
555	ifnet_lr2lri(lr, &lri);
556	ri->ri_refcnt = lri.lri_refcnt;
557	ri->ri_probes = lri.lri_probes;
558	ri->ri_rcv_expire = lri.lri_expire;
559	ri->ri_rssi = lri.lri_rssi;
560	ri->ri_lqm = lri.lri_lqm;
561	ri->ri_npm = lri.lri_npm;
562}
563
564void
565ifnet_lr2iflri(struct if_llreach *lr, struct ifnet_llreach_info *iflri)
566{
567	IFLR_LOCK_ASSERT_HELD(lr);
568
569	bzero(iflri, sizeof (*iflri));
570	/*
571	 * Note here we return request count, not actual memory refcnt.
572	 */
573	iflri->iflri_refcnt = lr->lr_reqcnt;
574	iflri->iflri_probes = lr->lr_probes;
575	iflri->iflri_rcv_expire = ifnet_llreach_up2upexp(lr, lr->lr_lastrcvd);
576	iflri->iflri_curtime = net_uptime();
577	switch (lr->lr_key.proto) {
578	case ETHERTYPE_IP:
579		iflri->iflri_netproto = PF_INET;
580		break;
581	case ETHERTYPE_IPV6:
582		iflri->iflri_netproto = PF_INET6;
583		break;
584	default:
585		/*
586		 * This shouldn't be possible for the time being,
587		 * since link-layer reachability records are only
588		 * kept for ARP and ND6.
589		 */
590		iflri->iflri_netproto = PF_UNSPEC;
591		break;
592	}
593	bcopy(&lr->lr_key.addr, &iflri->iflri_addr, IF_LLREACH_MAXLEN);
594	iflri->iflri_rssi = lr->lr_rssi;
595	iflri->iflri_lqm = lr->lr_lqm;
596	iflri->iflri_npm = lr->lr_npm;
597}
598
599void
600ifnet_lr2lri(struct if_llreach *lr, struct if_llreach_info *lri)
601{
602	IFLR_LOCK_ASSERT_HELD(lr);
603
604	bzero(lri, sizeof (*lri));
605	/*
606	 * Note here we return request count, not actual memory refcnt.
607	 */
608	lri->lri_refcnt	= lr->lr_reqcnt;
609	lri->lri_ifindex = lr->lr_ifp->if_index;
610	lri->lri_probes	= lr->lr_probes;
611	lri->lri_expire = ifnet_llreach_up2calexp(lr, lr->lr_lastrcvd);
612	lri->lri_proto = lr->lr_key.proto;
613	bcopy(&lr->lr_key.addr, &lri->lri_addr, IF_LLREACH_MAXLEN);
614	lri->lri_rssi = lr->lr_rssi;
615	lri->lri_lqm = lr->lr_lqm;
616	lri->lri_npm = lr->lr_npm;
617}
618
619static int
620sysctl_llreach_ifinfo SYSCTL_HANDLER_ARGS
621{
622#pragma unused(oidp)
623	int		*name, retval = 0;
624	unsigned int	namelen;
625	uint32_t	ifindex;
626	struct if_llreach *lr;
627	struct if_llreach_info lri;
628	struct ifnet	*ifp;
629
630	name = (int *)arg1;
631	namelen = (unsigned int)arg2;
632
633	if (req->newptr != USER_ADDR_NULL)
634		return (EPERM);
635
636	if (namelen != 1)
637		return (EINVAL);
638
639	ifindex = name[0];
640	ifnet_head_lock_shared();
641	if (ifindex <= 0 || ifindex > (u_int)if_index) {
642		printf("%s: ifindex %u out of range\n", __func__, ifindex);
643		ifnet_head_done();
644		return (ENOENT);
645	}
646
647	ifp = ifindex2ifnet[ifindex];
648	ifnet_head_done();
649	if (ifp == NULL) {
650		printf("%s: no ifp for ifindex %u\n", __func__, ifindex);
651		return (ENOENT);
652	}
653
654	lck_rw_lock_shared(&ifp->if_llreach_lock);
655	RB_FOREACH(lr, ll_reach_tree, &ifp->if_ll_srcs) {
656		/* Export to if_llreach_info structure */
657		IFLR_LOCK(lr);
658		ifnet_lr2lri(lr, &lri);
659		IFLR_UNLOCK(lr);
660
661		if ((retval = SYSCTL_OUT(req, &lri, sizeof (lri))) != 0)
662			break;
663	}
664	lck_rw_done(&ifp->if_llreach_lock);
665
666	return (retval);
667}
668