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