if_llatbl.h revision 286577
1/*
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.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 */
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/net/if_llatbl.h 286577 2015-08-10 12:03:59Z melifaro $");
29
30#ifndef	_NET_IF_LLATBL_H_
31#define	_NET_IF_LLATBL_H_
32
33#include <sys/_rwlock.h>
34#include <netinet/in.h>
35
36struct ifnet;
37struct sysctl_req;
38struct rt_msghdr;
39struct rt_addrinfo;
40
41struct llentry;
42LIST_HEAD(llentries, llentry);
43
44extern 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)
50
51/*
52 * Code referencing llentry must at least hold
53 * a shared lock
54 */
55struct llentry {
56	LIST_ENTRY(llentry)	 lle_next;
57	struct rwlock		 lle_lock;
58	struct lltable		 *lle_tbl;
59	struct llentries	 *lle_head;
60	void			(*lle_free)(struct llentry *);
61	struct mbuf		 *la_hold;
62	int			 la_numheld;  /* # of packets currently held */
63	time_t			 la_expire;
64	uint16_t		 la_flags;
65	uint16_t		 la_asked;
66	uint16_t		 la_preempt;
67	uint16_t		 ln_byhint;
68	int16_t			 ln_state;	/* IPv6 has ND6_LLINFO_NOSTATE == -2 */
69	uint16_t		 ln_router;
70	time_t			 ln_ntick;
71	int			 lle_refcnt;
72
73	union {
74		uint64_t	mac_aligned;
75		uint16_t	mac16[3];
76		uint8_t		mac8[20];	/* IB needs 20 bytes. */
77	} ll_addr;
78
79	LIST_ENTRY(llentry)	lle_chain;	/* chain of deleted items */
80	/* XXX af-private? */
81	union {
82		struct callout	ln_timer_ch;
83		struct callout  la_timer;
84	} lle_timer;
85	/* NB: struct sockaddr must immediately follow */
86};
87
88#define	LLE_WLOCK(lle)		rw_wlock(&(lle)->lle_lock)
89#define	LLE_RLOCK(lle)		rw_rlock(&(lle)->lle_lock)
90#define	LLE_WUNLOCK(lle)	rw_wunlock(&(lle)->lle_lock)
91#define	LLE_RUNLOCK(lle)	rw_runlock(&(lle)->lle_lock)
92#define	LLE_DOWNGRADE(lle)	rw_downgrade(&(lle)->lle_lock)
93#define	LLE_TRY_UPGRADE(lle)	rw_try_upgrade(&(lle)->lle_lock)
94#define	LLE_LOCK_INIT(lle)	rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK)
95#define	LLE_LOCK_DESTROY(lle)	rw_destroy(&(lle)->lle_lock)
96#define	LLE_WLOCK_ASSERT(lle)	rw_assert(&(lle)->lle_lock, RA_WLOCKED)
97
98#define LLE_IS_VALID(lle)	(((lle) != NULL) && ((lle) != (void *)-1))
99
100#define	LLE_ADDREF(lle) do {					\
101	LLE_WLOCK_ASSERT(lle);					\
102	KASSERT((lle)->lle_refcnt >= 0,				\
103	    ("negative refcnt %d on lle %p",			\
104	    (lle)->lle_refcnt, (lle)));				\
105	(lle)->lle_refcnt++;					\
106} while (0)
107
108#define	LLE_REMREF(lle)	do {					\
109	LLE_WLOCK_ASSERT(lle);					\
110	KASSERT((lle)->lle_refcnt > 0,				\
111	    ("bogus refcnt %d on lle %p",			\
112	    (lle)->lle_refcnt, (lle)));				\
113	(lle)->lle_refcnt--;					\
114} while (0)
115
116#define	LLE_FREE_LOCKED(lle) do {				\
117	if ((lle)->lle_refcnt == 1)				\
118		(lle)->lle_free(lle);				\
119	else {							\
120		LLE_REMREF(lle);				\
121		LLE_WUNLOCK(lle);				\
122	}							\
123	/* guard against invalid refs */			\
124	(lle) = NULL;						\
125} while (0)
126
127#define	LLE_FREE(lle) do {					\
128	LLE_WLOCK(lle);						\
129	LLE_FREE_LOCKED(lle);					\
130} while (0)
131
132
133#define	ln_timer_ch	lle_timer.ln_timer_ch
134#define	la_timer	lle_timer.la_timer
135
136/* XXX bad name */
137#define	L3_CADDR(lle)	((const struct sockaddr *)(&lle[1]))
138#define	L3_ADDR(lle)	((struct sockaddr *)(&lle[1]))
139#define	L3_ADDR_LEN(lle)	(((struct sockaddr *)(&lle[1]))->sa_len)
140
141#ifndef LLTBL_HASHTBL_SIZE
142#define	LLTBL_HASHTBL_SIZE	32	/* default 32 ? */
143#endif
144
145#ifndef LLTBL_HASHMASK
146#define	LLTBL_HASHMASK	(LLTBL_HASHTBL_SIZE - 1)
147#endif
148
149typedef	struct llentry *(llt_lookup_t)(struct lltable *, u_int flags,
150    const struct sockaddr *l3addr);
151typedef	struct llentry *(llt_create_t)(struct lltable *, u_int flags,
152    const struct sockaddr *l3addr);
153typedef	int (llt_delete_t)(struct lltable *, u_int flags,
154    const struct sockaddr *l3addr);
155typedef void (llt_prefix_free_t)(struct lltable *,
156    const struct sockaddr *prefix, const struct sockaddr *mask, u_int flags);
157typedef int (llt_dump_entry_t)(struct lltable *, struct llentry *,
158    struct sysctl_req *);
159typedef uint32_t (llt_hash_t)(const struct llentry *, uint32_t);
160typedef int (llt_match_prefix_t)(const struct sockaddr *,
161    const struct sockaddr *, u_int, struct llentry *);
162typedef void (llt_free_entry_t)(struct lltable *, struct llentry *);
163typedef void (llt_fill_sa_entry_t)(const struct llentry *, struct sockaddr *);
164typedef void (llt_link_entry_t)(struct lltable *, struct llentry *);
165typedef void (llt_unlink_entry_t)(struct llentry *);
166
167typedef int (llt_foreach_cb_t)(struct lltable *, struct llentry *, void *);
168typedef int (llt_foreach_entry_t)(struct lltable *, llt_foreach_cb_t *, void *);
169
170struct lltable {
171	SLIST_ENTRY(lltable)	llt_link;
172	struct llentries	lle_head[LLTBL_HASHTBL_SIZE];
173	int			llt_af;
174	struct ifnet		*llt_ifp;
175
176	llt_lookup_t		*llt_lookup;
177	llt_create_t		*llt_create;
178	llt_delete_t		*llt_delete;
179	llt_prefix_free_t	*llt_prefix_free;
180	llt_dump_entry_t	*llt_dump_entry;
181	llt_hash_t		*llt_hash;
182	llt_match_prefix_t	*llt_match_prefix;
183	llt_free_entry_t	*llt_free_entry;
184	llt_foreach_entry_t	*llt_foreach_entry;
185	llt_link_entry_t	*llt_link_entry;
186	llt_unlink_entry_t	*llt_unlink_entry;
187	llt_fill_sa_entry_t	*llt_fill_sa_entry;
188};
189
190MALLOC_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
207struct lltable *lltable_init(struct ifnet *, int);
208void		lltable_free(struct lltable *);
209void		lltable_prefix_free(int, struct sockaddr *,
210		    struct sockaddr *, u_int);
211#if 0
212void		lltable_drain(int);
213#endif
214int		lltable_sysctl_dumparp(int, struct sysctl_req *);
215
216size_t		llentry_free(struct llentry *);
217struct llentry  *llentry_alloc(struct ifnet *, struct lltable *,
218		    struct sockaddr_storage *);
219
220/* helper functions */
221size_t lltable_drop_entry_queue(struct llentry *);
222
223struct llentry *lltable_create_lle(struct lltable *llt, u_int flags,
224    const void *paddr);
225void lltable_link_entry(struct lltable *llt, struct llentry *lle);
226void lltable_unlink_entry(struct lltable *llt, struct llentry *lle);
227void lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa);
228struct ifnet *lltable_get_ifp(const struct lltable *llt);
229int lltable_get_af(const struct lltable *llt);
230
231int lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f,
232    void *farg);
233/*
234 * Generic link layer address lookup function.
235 */
236static __inline struct llentry *
237lla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
238{
239
240	return (llt->llt_lookup(llt, flags, l3addr));
241}
242
243static __inline struct llentry *
244lla_create(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
245{
246
247	return (llt->llt_create(llt, flags, l3addr));
248}
249
250static __inline int
251lla_delete(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
252{
253
254	return (llt->llt_delete(llt, flags, l3addr));
255}
256
257
258int		lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *);
259
260#include <sys/eventhandler.h>
261enum {
262	LLENTRY_RESOLVED,
263	LLENTRY_TIMEDOUT,
264	LLENTRY_DELETED,
265	LLENTRY_EXPIRED,
266};
267typedef void (*lle_event_fn)(void *, struct llentry *, int);
268EVENTHANDLER_DECLARE(lle_event, lle_event_fn);
269#endif  /* _NET_IF_LLATBL_H_ */
270