xref: /netbsd-src/sys/net/if_llatbl.h (revision a6f3f22f245acb8ee3bbf6871d7dce989204fa97)
1 /*	$NetBSD: if_llatbl.h,v 1.4 2015/10/09 01:50:09 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 #ifdef GATEWAY
41 #include <sys/mutex.h>
42 #endif
43 
44 #include <netinet/in.h>
45 
46 struct ifnet;
47 struct sysctl_req;
48 struct rt_msghdr;
49 struct rt_addrinfo;
50 
51 struct llentry;
52 LIST_HEAD(llentries, llentry);
53 
54 extern krwlock_t lltable_rwlock;
55 #define	LLTABLE_RLOCK()		rw_enter(&lltable_rwlock, RW_READER)
56 #define	LLTABLE_RUNLOCK()	rw_exit(&lltable_rwlock)
57 #define	LLTABLE_WLOCK()		rw_enter(&lltable_rwlock, RW_WRITER)
58 #define	LLTABLE_WUNLOCK()	rw_exit(&lltable_rwlock)
59 #define	LLTABLE_LOCK_ASSERT()	KASSERT(rw_lock_held(&lltable_rwlock))
60 
61 /*
62  * Code referencing llentry must at least hold
63  * a shared lock
64  */
65 struct llentry {
66 	LIST_ENTRY(llentry)	 lle_next;
67 	union {
68 		struct in_addr	addr4;
69 		struct in6_addr	addr6;
70 	} r_l3addr;
71 	union {
72 		uint64_t	mac_aligned;
73 		uint16_t	mac16[3];
74 		uint8_t		mac8[20];	/* IB needs 20 bytes. */
75 	} ll_addr;
76 	uint32_t		spare0;
77 	uint64_t		spare1;
78 
79 	struct lltable		 *lle_tbl;
80 	struct llentries	 *lle_head;
81 	void			(*lle_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 #ifdef GATEWAY
97 	kmutex_t		lle_lock;
98 #else
99 	krwlock_t		lle_lock;
100 #endif
101 
102 #ifdef __NetBSD__
103 #define	la_timer	lle_timer
104 	struct rtentry		*la_rt;
105 	void			*la_opaque;	/* For tokenring */
106 #endif
107 };
108 
109 
110 #if 0
111 #define LLE_LOCK_TRACE(n)	aprint_normal("%s: " #n " line %d\n", __func__, __LINE__)
112 #else
113 #define LLE_LOCK_TRACE(n)
114 #endif
115 
116 #ifdef GATEWAY
117 #define	LLE_WLOCK(lle)		do { \
118 					LLE_LOCK_TRACE(WL); \
119 					mutex_enter(&(lle)->lle_lock); \
120 				} while (0)
121 #define	LLE_RLOCK(lle)		do { \
122 					LLE_LOCK_TRACE(RL); \
123 					mutex_enter(&(lle)->lle_lock); \
124 				} while (0)
125 #define	LLE_WUNLOCK(lle)	do { \
126 					LLE_LOCK_TRACE(WU); \
127 					mutex_exit(&(lle)->lle_lock); \
128 				} while (0)
129 #define	LLE_RUNLOCK(lle)	do { \
130 					LLE_LOCK_TRACE(RU); \
131 					mutex_exit(&(lle)->lle_lock); \
132 				} while (0)
133 #define	LLE_DOWNGRADE(lle)	do {} while (0)
134 #define	LLE_TRY_UPGRADE(lle)	(1)
135 #define	LLE_LOCK_INIT(lle)	mutex_init(&(lle)->lle_lock, MUTEX_DEFAULT, \
136 				    IPL_NET)
137 #define	LLE_LOCK_DESTROY(lle)	mutex_destroy(&(lle)->lle_lock)
138 #define	LLE_WLOCK_ASSERT(lle)	KASSERT(mutex_owned(&(lle)->lle_lock))
139 
140 #else /* GATEWAY */
141 #define	LLE_WLOCK(lle)		do { \
142 					LLE_LOCK_TRACE(WL); \
143 					rw_enter(&(lle)->lle_lock, RW_WRITER); \
144 				} while (0)
145 #define	LLE_RLOCK(lle)		do { \
146 					LLE_LOCK_TRACE(RL); \
147 					rw_enter(&(lle)->lle_lock, RW_READER); \
148 				} while (0)
149 #define	LLE_WUNLOCK(lle)	do { \
150 					LLE_LOCK_TRACE(WU); \
151 					rw_exit(&(lle)->lle_lock); \
152 				} while (0)
153 #define	LLE_RUNLOCK(lle)	do { \
154 					LLE_LOCK_TRACE(RU); \
155 					rw_exit(&(lle)->lle_lock); \
156 				} while (0)
157 #define	LLE_DOWNGRADE(lle)	rw_downgrade(&(lle)->lle_lock)
158 #define	LLE_TRY_UPGRADE(lle)	rw_tryupgrade(&(lle)->lle_lock)
159 #ifdef __FreeBSD__
160 #define	LLE_LOCK_INIT(lle)	rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK)
161 #else /* XXX */
162 #define	LLE_LOCK_INIT(lle)	rw_init(&(lle)->lle_lock)
163 #endif
164 #define	LLE_LOCK_DESTROY(lle)	rw_destroy(&(lle)->lle_lock)
165 #define	LLE_WLOCK_ASSERT(lle)	KASSERT(rw_write_held(&(lle)->lle_lock))
166 #endif /* GATEWAY */
167 
168 #define LLE_IS_VALID(lle)	(((lle) != NULL) && ((lle) != (void *)-1))
169 
170 #define	LLE_ADDREF(lle) do {					\
171 	LLE_WLOCK_ASSERT(lle);					\
172 	KASSERTMSG((lle)->lle_refcnt >= 0,				\
173 	    "negative refcnt %d on lle %p",			\
174 	    (lle)->lle_refcnt, (lle));				\
175 	(lle)->lle_refcnt++;					\
176 } while (0)
177 
178 #define	LLE_REMREF(lle)	do {					\
179 	LLE_WLOCK_ASSERT(lle);					\
180 	KASSERTMSG((lle)->lle_refcnt > 0,				\
181 	    "bogus refcnt %d on lle %p",			\
182 	    (lle)->lle_refcnt, (lle));				\
183 	(lle)->lle_refcnt--;					\
184 } while (0)
185 
186 #define	LLE_FREE_LOCKED(lle) do {				\
187 	if ((lle)->lle_refcnt == 1)				\
188 		(lle)->lle_free(lle);				\
189 	else {							\
190 		LLE_REMREF(lle);				\
191 		LLE_WUNLOCK(lle);				\
192 	}							\
193 	/* guard against invalid refs */			\
194 	(lle) = NULL;						\
195 } while (0)
196 
197 #define	LLE_FREE(lle) do {					\
198 	LLE_WLOCK(lle);						\
199 	LLE_FREE_LOCKED(lle);					\
200 } while (0)
201 
202 
203 typedef	struct llentry *(llt_lookup_t)(struct lltable *, u_int flags,
204     const struct sockaddr *l3addr);
205 typedef	struct llentry *(llt_create_t)(struct lltable *, u_int flags,
206     const struct sockaddr *l3addr);
207 typedef	int (llt_delete_t)(struct lltable *, u_int flags,
208     const struct sockaddr *l3addr);
209 typedef void (llt_prefix_free_t)(struct lltable *,
210     const struct sockaddr *prefix, const struct sockaddr *mask, u_int flags);
211 typedef int (llt_dump_entry_t)(struct lltable *, struct llentry *,
212     struct sysctl_req *);
213 typedef uint32_t (llt_hash_t)(const struct llentry *, uint32_t);
214 typedef int (llt_match_prefix_t)(const struct sockaddr *,
215     const struct sockaddr *, u_int, struct llentry *);
216 typedef void (llt_free_entry_t)(struct lltable *, struct llentry *);
217 typedef void (llt_fill_sa_entry_t)(const struct llentry *, struct sockaddr *);
218 typedef void (llt_free_tbl_t)(struct lltable *);
219 typedef void (llt_link_entry_t)(struct lltable *, struct llentry *);
220 typedef void (llt_unlink_entry_t)(struct llentry *);
221 
222 typedef int (llt_foreach_cb_t)(struct lltable *, struct llentry *, void *);
223 typedef int (llt_foreach_entry_t)(struct lltable *, llt_foreach_cb_t *, void *);
224 
225 struct lltable {
226 	SLIST_ENTRY(lltable)	llt_link;
227 	int			llt_af;
228 	int			llt_hsize;
229 	struct llentries	*lle_head;
230 	struct ifnet		*llt_ifp;
231 
232 	llt_lookup_t		*llt_lookup;
233 	llt_create_t		*llt_create;
234 	llt_delete_t		*llt_delete;
235 	llt_prefix_free_t	*llt_prefix_free;
236 	llt_dump_entry_t	*llt_dump_entry;
237 	llt_hash_t		*llt_hash;
238 	llt_match_prefix_t	*llt_match_prefix;
239 	llt_free_entry_t	*llt_free_entry;
240 	llt_foreach_entry_t	*llt_foreach_entry;
241 	llt_link_entry_t	*llt_link_entry;
242 	llt_unlink_entry_t	*llt_unlink_entry;
243 	llt_fill_sa_entry_t	*llt_fill_sa_entry;
244 	llt_free_tbl_t		*llt_free_tbl;
245 };
246 
247 MALLOC_DECLARE(M_LLTABLE);
248 
249 /*
250  * LLentry flags
251  */
252 #define	LLE_DELETED	0x0001	/* entry must be deleted */
253 #define	LLE_STATIC	0x0002	/* entry is static */
254 #define	LLE_IFADDR	0x0004	/* entry is interface addr */
255 #define	LLE_VALID	0x0008	/* ll_addr is valid */
256 #define	LLE_PUB		0x0020	/* publish entry ??? */
257 #define	LLE_LINKED	0x0040	/* linked to lookup structure */
258 /* LLE request flags */
259 #define	LLE_EXCLUSIVE	0x2000	/* return lle xlocked  */
260 
261 #define LLATBL_HASH(key, mask) \
262 	(((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask)
263 
264 void lltableinit(void);
265 
266 struct lltable *lltable_allocate_htbl(uint32_t hsize);
267 void		lltable_free(struct lltable *);
268 void		lltable_link(struct lltable *llt);
269 void		lltable_prefix_free(int, struct sockaddr *,
270 		    struct sockaddr *, u_int);
271 void		lltable_drain(int);
272 int		lltable_sysctl_dumparp(int, struct sysctl_req *);
273 
274 size_t		llentry_free(struct llentry *);
275 struct llentry  *llentry_alloc(struct ifnet *, struct lltable *,
276 		    struct sockaddr_storage *);
277 
278 /* helper functions */
279 size_t lltable_drop_entry_queue(struct llentry *);
280 
281 struct llentry *lltable_create_lle(struct lltable *llt, u_int flags,
282     const void *paddr);
283 void lltable_link_entry(struct lltable *llt, struct llentry *lle);
284 void lltable_unlink_entry(struct lltable *llt, struct llentry *lle);
285 void lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa);
286 struct ifnet *lltable_get_ifp(const struct lltable *llt);
287 int lltable_get_af(const struct lltable *llt);
288 
289 int lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f,
290     void *farg);
291 /*
292  * Generic link layer address lookup function.
293  */
294 static __inline struct llentry *
295 lla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
296 {
297 
298 	return (llt->llt_lookup(llt, flags, l3addr));
299 }
300 
301 static __inline struct llentry *
302 lla_create(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
303 {
304 
305 	return (llt->llt_create(llt, flags, l3addr));
306 }
307 
308 static __inline int
309 lla_delete(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
310 {
311 
312 	return (llt->llt_delete(llt, flags, l3addr));
313 }
314 
315 
316 int lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *);
317 
318 #endif  /* _NET_IF_LLATBL_H_ */
319