2 * Copyright (c) 2004 Luigi Rizzo, Alessandro Cerri. All rights reserved.
3 * Copyright (c) 2004-2008 Qing Li. All rights reserved.
4 * Copyright (c) 2008 Kip Macy. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #ifndef _NET_IF_LLATBL_H_
31 #define _NET_IF_LLATBL_H_
33 #include <sys/_rwlock.h>
34 #include <netinet/in.h>
42 LIST_HEAD(llentries, llentry);
44 extern struct rwlock lltable_rwlock;
45 #define LLTABLE_RLOCK() rw_rlock(&lltable_rwlock)
46 #define LLTABLE_RUNLOCK() rw_runlock(&lltable_rwlock)
47 #define LLTABLE_WLOCK() rw_wlock(&lltable_rwlock)
48 #define LLTABLE_WUNLOCK() rw_wunlock(&lltable_rwlock)
49 #define LLTABLE_LOCK_ASSERT() rw_assert(&lltable_rwlock, RA_LOCKED)
52 * Code referencing llentry must at least hold
56 LIST_ENTRY(llentry) lle_next;
59 struct in6_addr addr6;
64 uint8_t mac8[20]; /* IB needs 20 bytes. */
69 struct lltable *lle_tbl;
70 struct llentries *lle_head;
71 void (*lle_free)(struct llentry *);
73 int la_numheld; /* # of packets currently held */
79 int16_t ln_state; /* IPv6 has ND6_LLINFO_NOSTATE == -2 */
84 LIST_ENTRY(llentry) lle_chain; /* chain of deleted items */
85 struct callout lle_timer;
86 struct rwlock lle_lock;
89 #define LLE_WLOCK(lle) rw_wlock(&(lle)->lle_lock)
90 #define LLE_RLOCK(lle) rw_rlock(&(lle)->lle_lock)
91 #define LLE_WUNLOCK(lle) rw_wunlock(&(lle)->lle_lock)
92 #define LLE_RUNLOCK(lle) rw_runlock(&(lle)->lle_lock)
93 #define LLE_DOWNGRADE(lle) rw_downgrade(&(lle)->lle_lock)
94 #define LLE_TRY_UPGRADE(lle) rw_try_upgrade(&(lle)->lle_lock)
95 #define LLE_LOCK_INIT(lle) rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK)
96 #define LLE_LOCK_DESTROY(lle) rw_destroy(&(lle)->lle_lock)
97 #define LLE_WLOCK_ASSERT(lle) rw_assert(&(lle)->lle_lock, RA_WLOCKED)
99 #define LLE_IS_VALID(lle) (((lle) != NULL) && ((lle) != (void *)-1))
101 #define LLE_ADDREF(lle) do { \
102 LLE_WLOCK_ASSERT(lle); \
103 KASSERT((lle)->lle_refcnt >= 0, \
104 ("negative refcnt %d on lle %p", \
105 (lle)->lle_refcnt, (lle))); \
106 (lle)->lle_refcnt++; \
109 #define LLE_REMREF(lle) do { \
110 LLE_WLOCK_ASSERT(lle); \
111 KASSERT((lle)->lle_refcnt > 0, \
112 ("bogus refcnt %d on lle %p", \
113 (lle)->lle_refcnt, (lle))); \
114 (lle)->lle_refcnt--; \
117 #define LLE_FREE_LOCKED(lle) do { \
118 if ((lle)->lle_refcnt == 1) \
119 (lle)->lle_free(lle); \
124 /* guard against invalid refs */ \
128 #define LLE_FREE(lle) do { \
130 LLE_FREE_LOCKED(lle); \
134 typedef struct llentry *(llt_lookup_t)(struct lltable *, u_int flags,
135 const struct sockaddr *l3addr);
136 typedef struct llentry *(llt_create_t)(struct lltable *, u_int flags,
137 const struct sockaddr *l3addr);
138 typedef int (llt_delete_t)(struct lltable *, u_int flags,
139 const struct sockaddr *l3addr);
140 typedef void (llt_prefix_free_t)(struct lltable *,
141 const struct sockaddr *prefix, const struct sockaddr *mask, u_int flags);
142 typedef int (llt_dump_entry_t)(struct lltable *, struct llentry *,
143 struct sysctl_req *);
144 typedef uint32_t (llt_hash_t)(const struct llentry *, uint32_t);
145 typedef int (llt_match_prefix_t)(const struct sockaddr *,
146 const struct sockaddr *, u_int, struct llentry *);
147 typedef void (llt_free_entry_t)(struct lltable *, struct llentry *);
148 typedef void (llt_fill_sa_entry_t)(const struct llentry *, struct sockaddr *);
149 typedef void (llt_free_tbl_t)(struct lltable *);
150 typedef void (llt_link_entry_t)(struct lltable *, struct llentry *);
151 typedef void (llt_unlink_entry_t)(struct llentry *);
153 typedef int (llt_foreach_cb_t)(struct lltable *, struct llentry *, void *);
154 typedef int (llt_foreach_entry_t)(struct lltable *, llt_foreach_cb_t *, void *);
157 SLIST_ENTRY(lltable) llt_link;
160 struct llentries *lle_head;
161 struct ifnet *llt_ifp;
163 llt_lookup_t *llt_lookup;
164 llt_create_t *llt_create;
165 llt_delete_t *llt_delete;
166 llt_prefix_free_t *llt_prefix_free;
167 llt_dump_entry_t *llt_dump_entry;
168 llt_hash_t *llt_hash;
169 llt_match_prefix_t *llt_match_prefix;
170 llt_free_entry_t *llt_free_entry;
171 llt_foreach_entry_t *llt_foreach_entry;
172 llt_link_entry_t *llt_link_entry;
173 llt_unlink_entry_t *llt_unlink_entry;
174 llt_fill_sa_entry_t *llt_fill_sa_entry;
175 llt_free_tbl_t *llt_free_tbl;
178 MALLOC_DECLARE(M_LLTABLE);
183 #define LLE_DELETED 0x0001 /* entry must be deleted */
184 #define LLE_STATIC 0x0002 /* entry is static */
185 #define LLE_IFADDR 0x0004 /* entry is interface addr */
186 #define LLE_VALID 0x0008 /* ll_addr is valid */
187 #define LLE_PUB 0x0020 /* publish entry ??? */
188 #define LLE_LINKED 0x0040 /* linked to lookup structure */
189 /* LLE request flags */
190 #define LLE_EXCLUSIVE 0x2000 /* return lle xlocked */
192 #define LLATBL_HASH(key, mask) \
193 (((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask)
195 struct lltable *lltable_allocate_htbl(uint32_t hsize);
196 void lltable_free(struct lltable *);
197 void lltable_link(struct lltable *llt);
198 void lltable_prefix_free(int, struct sockaddr *,
199 struct sockaddr *, u_int);
201 void lltable_drain(int);
203 int lltable_sysctl_dumparp(int, struct sysctl_req *);
205 size_t llentry_free(struct llentry *);
206 struct llentry *llentry_alloc(struct ifnet *, struct lltable *,
207 struct sockaddr_storage *);
209 /* helper functions */
210 size_t lltable_drop_entry_queue(struct llentry *);
212 struct llentry *lltable_create_lle(struct lltable *llt, u_int flags,
214 void lltable_link_entry(struct lltable *llt, struct llentry *lle);
215 void lltable_unlink_entry(struct lltable *llt, struct llentry *lle);
216 void lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa);
217 struct ifnet *lltable_get_ifp(const struct lltable *llt);
218 int lltable_get_af(const struct lltable *llt);
220 int lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f,
223 * Generic link layer address lookup function.
225 static __inline struct llentry *
226 lla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
229 return (llt->llt_lookup(llt, flags, l3addr));
232 static __inline struct llentry *
233 lla_create(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
236 return (llt->llt_create(llt, flags, l3addr));
240 lla_delete(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
243 return (llt->llt_delete(llt, flags, l3addr));
247 int lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *);
249 #include <sys/eventhandler.h>
256 typedef void (*lle_event_fn)(void *, struct llentry *, int);
257 EVENTHANDLER_DECLARE(lle_event, lle_event_fn);
258 #endif /* _NET_IF_LLATBL_H_ */