if_llatbl.h revision 1.1 1 /* $NetBSD: if_llatbl.h,v 1.1 2015/08/31 07:56:58 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 #include <sys/rwlock.h>
36 #include <netinet/in.h>
37
38 struct ifnet;
39 struct sysctl_req;
40 struct rt_msghdr;
41 struct rt_addrinfo;
42
43 struct llentry;
44 LIST_HEAD(llentries, llentry);
45
46 extern krwlock_t lltable_rwlock;
47 #define LLTABLE_RLOCK() rw_enter(&lltable_rwlock, RW_READER)
48 #define LLTABLE_RUNLOCK() rw_exit(&lltable_rwlock)
49 #define LLTABLE_WLOCK() rw_enter(&lltable_rwlock, RW_WRITER)
50 #define LLTABLE_WUNLOCK() rw_exit(&lltable_rwlock)
51 #define LLTABLE_LOCK_ASSERT() KASSERT(rw_lock_held(&lltable_rwlock))
52
53 /*
54 * Code referencing llentry must at least hold
55 * a shared lock
56 */
57 struct llentry {
58 LIST_ENTRY(llentry) lle_next;
59 union {
60 struct in_addr addr4;
61 struct in6_addr addr6;
62 } r_l3addr;
63 union {
64 uint64_t mac_aligned;
65 uint16_t mac16[3];
66 uint8_t mac8[20]; /* IB needs 20 bytes. */
67 } ll_addr;
68 uint32_t spare0;
69 uint64_t spare1;
70
71 struct lltable *lle_tbl;
72 struct llentries *lle_head;
73 void (*lle_free)(struct llentry *);
74 struct mbuf *la_hold;
75 int la_numheld; /* # of packets currently held */
76 time_t la_expire;
77 uint16_t la_flags;
78 uint16_t la_asked;
79 uint16_t la_preempt;
80 uint16_t ln_byhint;
81 int16_t ln_state; /* IPv6 has ND6_LLINFO_NOSTATE == -2 */
82 uint16_t ln_router;
83 time_t ln_ntick;
84 int lle_refcnt;
85
86 LIST_ENTRY(llentry) lle_chain; /* chain of deleted items */
87 struct callout lle_timer;
88 krwlock_t lle_lock;
89
90 #ifdef __NetBSD__
91 #define la_timer lle_timer
92 struct rtentry *la_rt;
93 void *la_opaque; /* For tokenring */
94 #endif
95 };
96
97 #define LLE_WLOCK(lle) rw_enter(&(lle)->lle_lock, RW_WRITER)
98 #define LLE_RLOCK(lle) rw_enter(&(lle)->lle_lock, RW_READER)
99 #define LLE_WUNLOCK(lle) rw_exit(&(lle)->lle_lock)
100 #define LLE_RUNLOCK(lle) rw_exit(&(lle)->lle_lock)
101 #define LLE_DOWNGRADE(lle) rw_downgrade(&(lle)->lle_lock)
102 #define LLE_TRY_UPGRADE(lle) rw_tryupgrade(&(lle)->lle_lock)
103 #ifdef __FreeBSD__
104 #define LLE_LOCK_INIT(lle) rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK)
105 #else /* XXX */
106 #define LLE_LOCK_INIT(lle) rw_init(&(lle)->lle_lock)
107 #endif
108 #define LLE_LOCK_DESTROY(lle) rw_destroy(&(lle)->lle_lock)
109 #define LLE_WLOCK_ASSERT(lle) KASSERT(rw_lock_held(&(lle)->lle_lock))
110
111 #define LLE_IS_VALID(lle) (((lle) != NULL) && ((lle) != (void *)-1))
112
113 #define LLE_ADDREF(lle) do { \
114 LLE_WLOCK_ASSERT(lle); \
115 KASSERTMSG((lle)->lle_refcnt >= 0, \
116 "negative refcnt %d on lle %p", \
117 (lle)->lle_refcnt, (lle)); \
118 (lle)->lle_refcnt++; \
119 } while (0)
120
121 #define LLE_REMREF(lle) do { \
122 LLE_WLOCK_ASSERT(lle); \
123 KASSERTMSG((lle)->lle_refcnt > 0, \
124 "bogus refcnt %d on lle %p", \
125 (lle)->lle_refcnt, (lle)); \
126 (lle)->lle_refcnt--; \
127 } while (0)
128
129 #define LLE_FREE_LOCKED(lle) do { \
130 if ((lle)->lle_refcnt == 1) \
131 (lle)->lle_free(lle); \
132 else { \
133 LLE_REMREF(lle); \
134 LLE_WUNLOCK(lle); \
135 } \
136 /* guard against invalid refs */ \
137 (lle) = NULL; \
138 } while (0)
139
140 #define LLE_FREE(lle) do { \
141 LLE_WLOCK(lle); \
142 LLE_FREE_LOCKED(lle); \
143 } while (0)
144
145
146 typedef struct llentry *(llt_lookup_t)(struct lltable *, u_int flags,
147 const struct sockaddr *l3addr);
148 typedef struct llentry *(llt_create_t)(struct lltable *, u_int flags,
149 const struct sockaddr *l3addr);
150 typedef int (llt_delete_t)(struct lltable *, u_int flags,
151 const struct sockaddr *l3addr);
152 typedef void (llt_prefix_free_t)(struct lltable *,
153 const struct sockaddr *prefix, const struct sockaddr *mask, u_int flags);
154 typedef int (llt_dump_entry_t)(struct lltable *, struct llentry *,
155 struct sysctl_req *);
156 typedef uint32_t (llt_hash_t)(const struct llentry *, uint32_t);
157 typedef int (llt_match_prefix_t)(const struct sockaddr *,
158 const struct sockaddr *, u_int, struct llentry *);
159 typedef void (llt_free_entry_t)(struct lltable *, struct llentry *);
160 typedef void (llt_fill_sa_entry_t)(const struct llentry *, struct sockaddr *);
161 typedef void (llt_free_tbl_t)(struct lltable *);
162 typedef void (llt_link_entry_t)(struct lltable *, struct llentry *);
163 typedef void (llt_unlink_entry_t)(struct llentry *);
164
165 typedef int (llt_foreach_cb_t)(struct lltable *, struct llentry *, void *);
166 typedef int (llt_foreach_entry_t)(struct lltable *, llt_foreach_cb_t *, void *);
167
168 struct lltable {
169 SLIST_ENTRY(lltable) llt_link;
170 int llt_af;
171 int llt_hsize;
172 struct llentries *lle_head;
173 struct ifnet *llt_ifp;
174
175 llt_lookup_t *llt_lookup;
176 llt_create_t *llt_create;
177 llt_delete_t *llt_delete;
178 llt_prefix_free_t *llt_prefix_free;
179 llt_dump_entry_t *llt_dump_entry;
180 llt_hash_t *llt_hash;
181 llt_match_prefix_t *llt_match_prefix;
182 llt_free_entry_t *llt_free_entry;
183 llt_foreach_entry_t *llt_foreach_entry;
184 llt_link_entry_t *llt_link_entry;
185 llt_unlink_entry_t *llt_unlink_entry;
186 llt_fill_sa_entry_t *llt_fill_sa_entry;
187 llt_free_tbl_t *llt_free_tbl;
188 };
189
190 MALLOC_DECLARE(M_LLTABLE);
191
192 /*
193 * LLentry flags
194 */
195 #define LLE_DELETED 0x0001 /* entry must be deleted */
196 #define LLE_STATIC 0x0002 /* entry is static */
197 #define LLE_IFADDR 0x0004 /* entry is interface addr */
198 #define LLE_VALID 0x0008 /* ll_addr is valid */
199 #define LLE_PUB 0x0020 /* publish entry ??? */
200 #define LLE_LINKED 0x0040 /* linked to lookup structure */
201 /* LLE request flags */
202 #define LLE_EXCLUSIVE 0x2000 /* return lle xlocked */
203
204 #define LLATBL_HASH(key, mask) \
205 (((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask)
206
207 void lltableinit(void);
208
209 struct lltable *lltable_allocate_htbl(uint32_t hsize);
210 void lltable_free(struct lltable *);
211 void lltable_link(struct lltable *llt);
212 void lltable_prefix_free(int, struct sockaddr *,
213 struct sockaddr *, u_int);
214 void lltable_drain(int);
215 int lltable_sysctl_dumparp(int, struct sysctl_req *);
216
217 size_t llentry_free(struct llentry *);
218 struct llentry *llentry_alloc(struct ifnet *, struct lltable *,
219 struct sockaddr_storage *);
220
221 /* helper functions */
222 size_t lltable_drop_entry_queue(struct llentry *);
223
224 struct llentry *lltable_create_lle(struct lltable *llt, u_int flags,
225 const void *paddr);
226 void lltable_link_entry(struct lltable *llt, struct llentry *lle);
227 void lltable_unlink_entry(struct lltable *llt, struct llentry *lle);
228 void lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa);
229 struct ifnet *lltable_get_ifp(const struct lltable *llt);
230 int lltable_get_af(const struct lltable *llt);
231
232 int lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f,
233 void *farg);
234 /*
235 * Generic link layer address lookup function.
236 */
237 static __inline struct llentry *
238 lla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
239 {
240
241 return (llt->llt_lookup(llt, flags, l3addr));
242 }
243
244 static __inline struct llentry *
245 lla_create(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
246 {
247
248 return (llt->llt_create(llt, flags, l3addr));
249 }
250
251 static __inline int
252 lla_delete(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
253 {
254
255 return (llt->llt_delete(llt, flags, l3addr));
256 }
257
258
259 int lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *);
260
261 #endif /* _NET_IF_LLATBL_H_ */
262