]>
git.saurik.com Git - apple/xnu.git/blob - bsd/net/if_llatbl.h
2 * Copyright (c) 2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 2004 Luigi Rizzo, Alessandro Cerri. All rights reserved.
30 * Copyright (c) 2004-2008 Qing Li. All rights reserved.
31 * Copyright (c) 2008 Kip Macy. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
42 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 #include <sys/cdefs.h>
56 #ifndef _NET_IF_LLATBL_H_
57 #define _NET_IF_LLATBL_H_
59 #include <netinet/in.h>
60 #include <net/if_dl.h>
61 #include <kern/locks.h>
62 #include <kern/thread_call.h>
63 #include <sys/eventhandler.h>
71 LIST_HEAD(llentries
, llentry
);
73 extern lck_rw_t
*lltable_rwlock
;
74 #define LLTABLE_RLOCK() lck_rw_lock_shared(lltable_rwlock)
75 #define LLTABLE_RUNLOCK() lck_rw_done(lltable_rwlock)
76 #define LLTABLE_WLOCK() lck_rw_lock_exclusive(lltable_rwlock)
77 #define LLTABLE_WUNLOCK() lck_rw_done(lltable_rwlock)
78 #define LLTABLE_LOCK_ASSERT() LCK_RW_ASSERT(lltable_rwlock, LCK_RW_ASSERT_EXCLUSIVE)
80 #define LLE_MAX_LINKHDR 24 /* Full IB header */
82 * Code referencing llentry must at least hold
86 LIST_ENTRY(llentry
) lle_next
;
89 struct in6_addr addr6
;
92 char r_linkdata
[LLE_MAX_LINKHDR
]; /* L2 data */
93 uint8_t r_hdrlen
; /* length for LL header */
96 uint16_t r_flags
; /* LLE runtime flags */
97 uint16_t r_skip_req
; /* feedback from fast path */
99 struct lltable
*lle_tbl
;
100 struct llentries
*lle_head
;
101 void (*lle_free
)(struct llentry
*);
102 struct mbuf
*la_hold
;
103 int la_numheld
; /* # of packets currently held */
108 int16_t ln_state
; /* IPv6 has ND6_LLINFO_NOSTATE == -2 */
111 time_t lle_remtime
; /* Real time remaining */
112 time_t lle_hittime
; /* Time when r_skip_req was unset */
115 uint64_t mac_aligned
;
118 LIST_ENTRY(llentry
) lle_chain
; /* chain of deleted items */
119 thread_call_t lle_timer
;
120 u_int64_t ln_lastused
; /* last used timestamp */
121 struct if_llreach
*ln_llreach
; /* link-layer reachability record */
122 decl_lck_rw_data(, lle_lock
);
123 decl_lck_mtx_data(, req_mtx
);
126 extern lck_grp_t
*lle_lock_grp
;
127 extern lck_attr_t
*lle_lock_attr
;
129 #define LLE_WLOCK(lle) lck_rw_lock_exclusive(&(lle)->lle_lock)
130 #define LLE_RLOCK(lle) lck_rw_lock_shared(&(lle)->lle_lock)
131 #define LLE_WUNLOCK(lle) lck_rw_done(&(lle)->lle_lock)
132 #define LLE_RUNLOCK(lle) lck_rw_done(&(lle)->lle_lock)
133 #define LLE_DOWNGRADE(lle) lck_rw_lock_exclusive_to_shared(&(lle)->lle_lock)
134 #define LLE_TRY_UPGRADE(lle) lck_rw_lock_shared_to_exclusive(&(lle)->lle_lock)
135 #define LLE_LOCK_INIT(lle) lck_rw_init(&(lle)->lle_lock, lle_lock_grp, lle_lock_attr)
136 #define LLE_LOCK_DESTROY(lle) lck_rw_destroy(&(lle)->lle_lock, lle_lock_grp)
137 #define LLE_WLOCK_ASSERT(lle) LCK_RW_ASSERT(&(lle)->lle_lock, LCK_RW_ASSERT_EXCLUSIVE)
139 #define LLE_REQ_INIT(lle) lck_mtx_init(&(lle)->req_mtx, lle_lock_grp, lle_lock_attr)
140 #define LLE_REQ_DESTROY(lle) lck_mtx_destroy(&(lle)->req_mtx, lle_lock_grp)
141 #define LLE_REQ_LOCK(lle) lck_mtx_lock(&(lle)->req_mtx)
142 #define LLE_REQ_UNLOCK(lle) lck_mtx_unlock(&(lle)->req_mtx)
144 #define LLE_IS_VALID(lle) (((lle) != NULL) && ((lle) != (void *)-1))
146 #define LLE_ADDREF(lle) do { \
147 LLE_WLOCK_ASSERT(lle); \
148 VERIFY((lle)->lle_refcnt >= 0); \
149 (lle)->lle_refcnt++; \
152 #define LLE_REMREF(lle) do { \
153 LLE_WLOCK_ASSERT(lle); \
154 VERIFY((lle)->lle_refcnt > 0); \
155 (lle)->lle_refcnt--; \
158 #define LLE_FREE_LOCKED(lle) do { \
159 if ((lle)->lle_refcnt == 1) \
160 (lle)->lle_free(lle); \
165 /* guard against invalid refs */ \
169 #define LLE_FREE(lle) do { \
171 LLE_FREE_LOCKED(lle); \
174 typedef struct llentry
*(llt_lookup_t
)(struct lltable
*, uint16_t flags
,
175 const struct sockaddr
*l3addr
);
176 typedef struct llentry
*(llt_alloc_t
)(struct lltable
*, uint16_t flags
,
177 const struct sockaddr
*l3addr
);
178 typedef void (llt_delete_t
)(struct lltable
*, struct llentry
*);
179 typedef void (llt_prefix_free_t
)(struct lltable
*,
180 const struct sockaddr
*addr
, const struct sockaddr
*mask
, uint16_t flags
);
181 typedef int (llt_dump_entry_t
)(struct lltable
*, struct llentry
*,
182 struct sysctl_req
*);
183 typedef uint32_t (llt_hash_t
)(const struct llentry
*, uint32_t);
184 typedef int (llt_match_prefix_t
)(const struct sockaddr
*,
185 const struct sockaddr
*, uint16_t, struct llentry
*);
186 typedef void (llt_free_entry_t
)(struct lltable
*, struct llentry
*);
187 typedef void (llt_fill_sa_entry_t
)(const struct llentry
*, struct sockaddr
*);
188 typedef void (llt_free_tbl_t
)(struct lltable
*);
189 typedef void (llt_link_entry_t
)(struct lltable
*, struct llentry
*);
190 typedef void (llt_unlink_entry_t
)(struct llentry
*);
192 typedef int (llt_foreach_cb_t
)(struct lltable
*, struct llentry
*, void *);
193 typedef int (llt_foreach_entry_t
)(struct lltable
*, llt_foreach_cb_t
*, void *);
196 SLIST_ENTRY(lltable
) llt_link
;
199 struct llentries
*lle_head
;
200 struct ifnet
*llt_ifp
;
202 llt_lookup_t
*llt_lookup
;
203 llt_alloc_t
*llt_alloc_entry
;
204 llt_delete_t
*llt_delete_entry
;
205 llt_prefix_free_t
*llt_prefix_free
;
206 llt_dump_entry_t
*llt_dump_entry
;
207 llt_hash_t
*llt_hash
;
208 llt_match_prefix_t
*llt_match_prefix
;
209 llt_free_entry_t
*llt_free_entry
;
210 llt_foreach_entry_t
*llt_foreach_entry
;
211 llt_link_entry_t
*llt_link_entry
;
212 llt_unlink_entry_t
*llt_unlink_entry
;
213 llt_fill_sa_entry_t
*llt_fill_sa_entry
;
214 llt_free_tbl_t
*llt_free_tbl
;
217 #ifdef MALLOC_DECLARE
218 MALLOC_DECLARE(M_LLTABLE
);
224 #define LLE_DELETED 0x0001 /* entry must be deleted */
225 #define LLE_STATIC 0x0002 /* entry is static */
226 #define LLE_IFADDR 0x0004 /* entry is interface addr */
227 #define LLE_VALID 0x0008 /* ll_addr is valid */
228 #define LLE_REDIRECT 0x0010 /* installed by redirect; has host rtentry */
229 #define LLE_PUB 0x0020 /* publish entry ??? */
230 #define LLE_LINKED 0x0040 /* linked to lookup structure */
231 /* LLE request flags */
232 #define LLE_EXCLUSIVE 0x2000 /* return lle xlocked */
233 #define LLE_UNLOCKED 0x4000 /* return lle unlocked */
234 #define LLE_ADDRONLY 0x4000 /* return lladdr instead of full header */
235 #define LLE_CREATE 0x8000 /* hint to avoid lle lookup */
237 /* LLE flags used by fastpath code */
238 #define RLLE_VALID 0x0001 /* entry is valid */
239 #define RLLE_IFADDR LLE_IFADDR /* entry is ifaddr */
241 #define LLATBL_HASH(key, mask) \
242 (((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask)
244 void lltable_glbl_init(void);
245 struct lltable
*lltable_allocate_htbl(uint32_t hsize
);
246 void lltable_free(struct lltable
*);
247 void lltable_link(struct lltable
*llt
);
248 void lltable_prefix_free(int, struct sockaddr
*,
249 struct sockaddr
*, uint16_t);
251 void lltable_drain(int);
253 int lltable_sysctl_dumparp(int, struct sysctl_req
*);
255 size_t llentry_free(struct llentry
*);
256 struct llentry
*llentry_alloc(struct ifnet
*, struct lltable
*,
257 struct sockaddr_storage
*);
259 /* helper functions */
260 size_t lltable_drop_entry_queue(struct llentry
*);
261 void lltable_set_entry_addr(struct ifnet
*ifp
, struct llentry
*lle
,
262 const char *ll_addr
);
263 int lltable_try_set_entry_addr(struct ifnet
*ifp
, struct llentry
*lle
,
264 const char *linkhdr
, size_t linkhdrsize
, int lladdr_off
);
266 int lltable_calc_llheader(struct ifnet
*ifp
, int family
, char *lladdr
,
267 char *buf
, size_t *bufsize
, int *lladdr_off
);
268 void lltable_update_ifaddr(struct lltable
*llt
);
269 struct llentry
*lltable_alloc_entry(struct lltable
*llt
, uint16_t flags
,
270 const struct sockaddr
*l4addr
);
271 void lltable_free_entry(struct lltable
*llt
, struct llentry
*lle
);
272 int lltable_delete_addr(struct lltable
*llt
, u_int flags
,
273 const struct sockaddr
*l3addr
);
274 void lltable_link_entry(struct lltable
*llt
, struct llentry
*lle
);
275 void lltable_unlink_entry(struct lltable
*llt
, struct llentry
*lle
);
276 void lltable_fill_sa_entry(const struct llentry
*lle
, struct sockaddr
*sa
);
277 struct ifnet
*lltable_get_ifp(const struct lltable
*llt
);
278 int lltable_get_af(const struct lltable
*llt
);
280 int lltable_foreach_lle(struct lltable
*llt
, llt_foreach_cb_t
*f
,
283 * Generic link layer address lookup function.
285 static __inline
struct llentry
*
286 lla_lookup(struct lltable
*llt
, uint16_t flags
, const struct sockaddr
*l3addr
)
288 return llt
->llt_lookup(llt
, flags
, l3addr
);
291 int lla_rt_output(struct rt_msghdr
*, struct rt_addrinfo
*);
300 typedef void (*lle_event_fn
)(struct eventhandler_entry_arg
, struct llentry
*, int);
301 EVENTHANDLER_DECLARE(lle_event
, lle_event_fn
);
302 #endif /* _NET_IF_LLATBL_H_ */