1 | /* $NetBSD: if_llatbl.h,v 1.17 2019/07/18 06:47:10 ozaki-r Exp $ */ |
2 | /* |
3 | * Copyright (c) 2004 Luigi Rizzo, Alessandro Cerri. All rights reserved. |
4 | * Copyright (c) 2004-2008 Qing Li. All rights reserved. |
5 | * Copyright (c) 2008 Kip Macy. All rights reserved. |
6 | * Copyright (c) 2015 The NetBSD Foundation, Inc. |
7 | * All rights reserved. |
8 | * |
9 | * Redistribution and use in source and binary forms, with or without |
10 | * modification, are permitted provided that the following conditions |
11 | * are met: |
12 | * 1. Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. |
14 | * 2. Redistributions in binary form must reproduce the above copyright |
15 | * notice, this list of conditions and the following disclaimer in the |
16 | * documentation and/or other materials provided with the distribution. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
28 | * SUCH DAMAGE. |
29 | */ |
30 | #include <sys/cdefs.h> |
31 | |
32 | #ifndef _NET_IF_LLATBL_H_ |
33 | #define _NET_IF_LLATBL_H_ |
34 | |
35 | #if defined(_KERNEL_OPT) |
36 | #include "opt_gateway.h" |
37 | #endif |
38 | |
39 | #include <sys/rwlock.h> |
40 | #include <sys/syslog.h> |
41 | |
42 | #include <netinet/in.h> |
43 | |
44 | struct ifnet; |
45 | struct sysctl_req; |
46 | struct rt_msghdr; |
47 | struct rt_addrinfo; |
48 | struct rt_walkarg; |
49 | |
50 | struct llentry; |
51 | LIST_HEAD(llentries, llentry); |
52 | |
53 | extern krwlock_t lltable_rwlock; |
54 | #define LLTABLE_RLOCK() rw_enter(&lltable_rwlock, RW_READER) |
55 | #define LLTABLE_RUNLOCK() rw_exit(&lltable_rwlock) |
56 | #define LLTABLE_WLOCK() rw_enter(&lltable_rwlock, RW_WRITER) |
57 | #define LLTABLE_WUNLOCK() rw_exit(&lltable_rwlock) |
58 | #define LLTABLE_LOCK_ASSERT() KASSERT(rw_lock_held(&lltable_rwlock)) |
59 | |
60 | /* |
61 | * Code referencing llentry must at least hold |
62 | * a shared lock |
63 | */ |
64 | struct llentry { |
65 | LIST_ENTRY(llentry) lle_next; |
66 | union { |
67 | struct in_addr addr4; |
68 | struct in6_addr addr6; |
69 | } r_l3addr; |
70 | union { |
71 | uint64_t mac_aligned; |
72 | uint16_t mac16[3]; |
73 | uint8_t mac8[20]; /* IB needs 20 bytes. */ |
74 | } ll_addr; |
75 | uint32_t spare0; |
76 | uint64_t spare1; |
77 | |
78 | struct lltable *lle_tbl; |
79 | struct llentries *lle_head; |
80 | void (*lle_free)(struct llentry *); |
81 | void (*lle_ll_free)(struct llentry *); |
82 | struct mbuf *la_hold; |
83 | int la_numheld; /* # of packets currently held */ |
84 | time_t la_expire; |
85 | uint16_t la_flags; |
86 | uint16_t la_asked; |
87 | uint16_t la_preempt; |
88 | uint16_t ln_byhint; |
89 | int16_t ln_state; /* IPv6 has ND6_LLINFO_NOSTATE == -2 */ |
90 | uint16_t ln_router; |
91 | time_t ln_ntick; |
92 | int lle_refcnt; |
93 | |
94 | LIST_ENTRY(llentry) lle_chain; /* chain of deleted items */ |
95 | struct callout lle_timer; |
96 | krwlock_t lle_lock; |
97 | |
98 | #ifdef __NetBSD__ |
99 | #define la_timer lle_timer |
100 | #define ln_timer_ch lle_timer |
101 | #define ln_expire la_expire |
102 | #define ln_asked la_asked |
103 | #define ln_hold la_hold |
104 | void *la_opaque; /* For tokenring */ |
105 | #endif |
106 | }; |
107 | |
108 | |
109 | #if 0 |
110 | #define LLE_LOCK_TRACE(t, lle) log(LOG_DEBUG, \ |
111 | "%s:%d: LOCK(" #t "): lle=%p\n", \ |
112 | __func__, __LINE__, (lle)) |
113 | #else |
114 | #define LLE_LOCK_TRACE(t, lle) do {} while (0) |
115 | #endif |
116 | |
117 | #define LLE_WLOCK(lle) do { \ |
118 | LLE_LOCK_TRACE(WL, (lle)); \ |
119 | rw_enter(&(lle)->lle_lock, RW_WRITER); \ |
120 | } while (0) |
121 | #define LLE_RLOCK(lle) do { \ |
122 | LLE_LOCK_TRACE(RL, (lle)); \ |
123 | rw_enter(&(lle)->lle_lock, RW_READER); \ |
124 | } while (0) |
125 | #define LLE_WUNLOCK(lle) do { \ |
126 | LLE_LOCK_TRACE(WU, (lle)); \ |
127 | rw_exit(&(lle)->lle_lock); \ |
128 | } while (0) |
129 | #define LLE_RUNLOCK(lle) do { \ |
130 | LLE_LOCK_TRACE(RU, (lle)); \ |
131 | rw_exit(&(lle)->lle_lock); \ |
132 | } while (0) |
133 | #define LLE_DOWNGRADE(lle) rw_downgrade(&(lle)->lle_lock) |
134 | #define LLE_TRY_UPGRADE(lle) rw_tryupgrade(&(lle)->lle_lock) |
135 | #ifdef __FreeBSD__ |
136 | #define LLE_LOCK_INIT(lle) rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK) |
137 | #else /* XXX */ |
138 | #define LLE_LOCK_INIT(lle) rw_init(&(lle)->lle_lock) |
139 | #endif |
140 | #define LLE_LOCK_DESTROY(lle) rw_destroy(&(lle)->lle_lock) |
141 | #define LLE_WLOCK_ASSERT(lle) KASSERT(rw_write_held(&(lle)->lle_lock)) |
142 | |
143 | #define LLE_IS_VALID(lle) (((lle) != NULL) && ((lle) != (void *)-1)) |
144 | |
145 | #if 0 |
146 | #define LLE_REF_TRACE(t, lle, n) \ |
147 | log(LOG_DEBUG, "%s:%d: %p REF(" #t "): refcnt=%d\n", \ |
148 | __func__, __LINE__, (lle), (n)) |
149 | #else |
150 | #define LLE_REF_TRACE(t, lle, n) do {} while (0) |
151 | #endif |
152 | |
153 | #define LLE_ADDREF(lle) do { \ |
154 | LLE_WLOCK_ASSERT(lle); \ |
155 | LLE_REF_TRACE(ADD, (lle), (lle)->lle_refcnt); \ |
156 | KASSERTMSG((lle)->lle_refcnt >= 0, \ |
157 | "negative refcnt %d on lle %p", \ |
158 | (lle)->lle_refcnt, (lle)); \ |
159 | (lle)->lle_refcnt++; \ |
160 | } while (0) |
161 | |
162 | #define LLE_REMREF(lle) do { \ |
163 | LLE_WLOCK_ASSERT(lle); \ |
164 | LLE_REF_TRACE(REM, (lle), (lle)->lle_refcnt); \ |
165 | KASSERTMSG((lle)->lle_refcnt > 0, \ |
166 | "bogus refcnt %d on lle %p", \ |
167 | (lle)->lle_refcnt, (lle)); \ |
168 | (lle)->lle_refcnt--; \ |
169 | if ((lle)->lle_refcnt == 0) \ |
170 | LLE_REF_TRACE(ZERO, (lle), (lle)->lle_refcnt); \ |
171 | } while (0) |
172 | |
173 | #define LLE_FREE_LOCKED(lle) do { \ |
174 | if ((lle)->lle_refcnt == 1) { \ |
175 | if ((lle)->lle_ll_free != NULL) \ |
176 | (lle)->lle_ll_free(lle); \ |
177 | (lle)->lle_free(lle); \ |
178 | } else { \ |
179 | LLE_REMREF(lle); \ |
180 | LLE_WUNLOCK(lle); \ |
181 | } \ |
182 | /* guard against invalid refs */ \ |
183 | (lle) = NULL; \ |
184 | } while (0) |
185 | |
186 | #define LLE_FREE(lle) do { \ |
187 | LLE_WLOCK(lle); \ |
188 | LLE_FREE_LOCKED(lle); \ |
189 | } while (0) |
190 | |
191 | |
192 | typedef struct llentry *(llt_lookup_t)(struct lltable *, u_int flags, |
193 | const struct sockaddr *l3addr); |
194 | typedef struct llentry *(llt_create_t)(struct lltable *, u_int flags, |
195 | const struct sockaddr *l3addr, const struct rtentry *); |
196 | typedef int (llt_delete_t)(struct lltable *, u_int flags, |
197 | const struct sockaddr *l3addr); |
198 | typedef void (llt_prefix_free_t)(struct lltable *, |
199 | const struct sockaddr *prefix, const struct sockaddr *mask, u_int flags); |
200 | typedef int (llt_dump_entry_t)(struct lltable *, struct llentry *, |
201 | struct rt_walkarg *); |
202 | typedef uint32_t (llt_hash_t)(const struct llentry *, uint32_t); |
203 | typedef int (llt_match_prefix_t)(const struct sockaddr *, |
204 | const struct sockaddr *, u_int, struct llentry *); |
205 | typedef void (llt_free_entry_t)(struct lltable *, struct llentry *); |
206 | typedef void (llt_fill_sa_entry_t)(const struct llentry *, struct sockaddr *); |
207 | typedef void (llt_free_tbl_t)(struct lltable *); |
208 | typedef void (llt_link_entry_t)(struct lltable *, struct llentry *); |
209 | typedef void (llt_unlink_entry_t)(struct llentry *); |
210 | |
211 | typedef int (llt_foreach_cb_t)(struct lltable *, struct llentry *, void *); |
212 | typedef int (llt_foreach_entry_t)(struct lltable *, llt_foreach_cb_t *, void *); |
213 | |
214 | struct lltable { |
215 | SLIST_ENTRY(lltable) llt_link; |
216 | int llt_af; |
217 | int llt_hsize; |
218 | struct llentries *lle_head; |
219 | unsigned int llt_lle_count; |
220 | struct ifnet *llt_ifp; |
221 | |
222 | llt_lookup_t *llt_lookup; |
223 | llt_create_t *llt_create; |
224 | llt_delete_t *llt_delete; |
225 | llt_prefix_free_t *llt_prefix_free; |
226 | llt_dump_entry_t *llt_dump_entry; |
227 | llt_hash_t *llt_hash; |
228 | llt_match_prefix_t *llt_match_prefix; |
229 | llt_free_entry_t *llt_free_entry; |
230 | llt_foreach_entry_t *llt_foreach_entry; |
231 | llt_link_entry_t *llt_link_entry; |
232 | llt_unlink_entry_t *llt_unlink_entry; |
233 | llt_fill_sa_entry_t *llt_fill_sa_entry; |
234 | llt_free_tbl_t *llt_free_tbl; |
235 | }; |
236 | |
237 | MALLOC_DECLARE(M_LLTABLE); |
238 | |
239 | /* |
240 | * LLentry flags |
241 | */ |
242 | #define LLE_DELETED 0x0001 /* entry must be deleted */ |
243 | #define LLE_STATIC 0x0002 /* entry is static */ |
244 | #define LLE_IFADDR 0x0004 /* entry is interface addr */ |
245 | #define LLE_VALID 0x0008 /* ll_addr is valid */ |
246 | #define LLE_PUB 0x0020 /* publish entry ??? */ |
247 | #define LLE_LINKED 0x0040 /* linked to lookup structure */ |
248 | /* LLE request flags */ |
249 | #define LLE_EXCLUSIVE 0x2000 /* return lle xlocked */ |
250 | |
251 | #define LLATBL_HASH(key, mask) \ |
252 | (((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask) |
253 | |
254 | void lltableinit(void); |
255 | |
256 | struct lltable *lltable_allocate_htbl(uint32_t hsize); |
257 | void lltable_free(struct lltable *); |
258 | void lltable_link(struct lltable *llt); |
259 | void lltable_prefix_free(const int, const struct sockaddr *, |
260 | const struct sockaddr *, const u_int); |
261 | void lltable_drain(int); |
262 | void lltable_purge_entries(struct lltable *); |
263 | int lltable_sysctl_dump(int, struct rt_walkarg *); |
264 | int lltable_dump_entry(struct lltable *, struct llentry *, |
265 | struct rt_walkarg *, struct sockaddr *); |
266 | |
267 | size_t llentry_free(struct llentry *); |
268 | struct llentry *llentry_alloc(struct ifnet *, struct lltable *, |
269 | struct sockaddr_storage *); |
270 | |
271 | struct llentry *llentry_pool_get(int); |
272 | void llentry_pool_put(struct llentry *); |
273 | |
274 | /* helper functions */ |
275 | size_t lltable_drop_entry_queue(struct llentry *); |
276 | |
277 | struct llentry *lltable_create_lle(struct lltable *llt, u_int flags, |
278 | const void *paddr); |
279 | void lltable_link_entry(struct lltable *llt, struct llentry *lle); |
280 | void lltable_unlink_entry(struct lltable *llt, struct llentry *lle); |
281 | void lltable_free_entry(struct lltable *llt, struct llentry *lle); |
282 | void lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa); |
283 | struct ifnet *lltable_get_ifp(const struct lltable *llt); |
284 | int lltable_get_af(const struct lltable *llt); |
285 | |
286 | static __inline unsigned int |
287 | lltable_get_entry_count(struct lltable *llt) |
288 | { |
289 | return llt->llt_lle_count; |
290 | } |
291 | |
292 | int lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f, |
293 | void *farg); |
294 | /* |
295 | * Generic link layer address lookup function. |
296 | */ |
297 | static __inline struct llentry * |
298 | lla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) |
299 | { |
300 | |
301 | return (llt->llt_lookup(llt, flags, l3addr)); |
302 | } |
303 | |
304 | static __inline struct llentry * |
305 | lla_create(struct lltable *llt, u_int flags, const struct sockaddr *l3addr, |
306 | const struct rtentry *rt) |
307 | { |
308 | |
309 | return (llt->llt_create(llt, flags, l3addr, rt)); |
310 | } |
311 | |
312 | static __inline int |
313 | lla_delete(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) |
314 | { |
315 | |
316 | return (llt->llt_delete(llt, flags, l3addr)); |
317 | } |
318 | |
319 | |
320 | int lla_rt_output(const u_char, const int, const time_t, |
321 | struct rt_addrinfo *info, int); |
322 | |
323 | #endif /* _NET_IF_LLATBL_H_ */ |
324 | |