if_llatbl.h revision 286457
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 286457 2015-08-08 17:48:54Z 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 lltable *, 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	/* XXX af-private? */
80	union {
81		struct callout	ln_timer_ch;
82		struct callout  la_timer;
83	} lle_timer;
84	/* NB: struct sockaddr must immediately follow */
85};
86
87#define	LLE_WLOCK(lle)		rw_wlock(&(lle)->lle_lock)
88#define	LLE_RLOCK(lle)		rw_rlock(&(lle)->lle_lock)
89#define	LLE_WUNLOCK(lle)	rw_wunlock(&(lle)->lle_lock)
90#define	LLE_RUNLOCK(lle)	rw_runlock(&(lle)->lle_lock)
91#define	LLE_DOWNGRADE(lle)	rw_downgrade(&(lle)->lle_lock)
92#define	LLE_TRY_UPGRADE(lle)	rw_try_upgrade(&(lle)->lle_lock)
93#define	LLE_LOCK_INIT(lle)	rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK)
94#define	LLE_LOCK_DESTROY(lle)	rw_destroy(&(lle)->lle_lock)
95#define	LLE_WLOCK_ASSERT(lle)	rw_assert(&(lle)->lle_lock, RA_WLOCKED)
96
97#define LLE_IS_VALID(lle)	(((lle) != NULL) && ((lle) != (void *)-1))
98
99#define	LLE_ADDREF(lle) do {					\
100	LLE_WLOCK_ASSERT(lle);					\
101	KASSERT((lle)->lle_refcnt >= 0,				\
102	    ("negative refcnt %d on lle %p",			\
103	    (lle)->lle_refcnt, (lle)));				\
104	(lle)->lle_refcnt++;					\
105} while (0)
106
107#define	LLE_REMREF(lle)	do {					\
108	LLE_WLOCK_ASSERT(lle);					\
109	KASSERT((lle)->lle_refcnt > 0,				\
110	    ("bogus refcnt %d on lle %p",			\
111	    (lle)->lle_refcnt, (lle)));				\
112	(lle)->lle_refcnt--;					\
113} while (0)
114
115#define	LLE_FREE_LOCKED(lle) do {				\
116	if ((lle)->lle_refcnt == 1)				\
117		(lle)->lle_free((lle)->lle_tbl, (lle));		\
118	else {							\
119		LLE_REMREF(lle);				\
120		LLE_WUNLOCK(lle);				\
121	}							\
122	/* guard against invalid refs */			\
123	(lle) = NULL;						\
124} while (0)
125
126#define	LLE_FREE(lle) do {					\
127	LLE_WLOCK(lle);						\
128	LLE_FREE_LOCKED(lle);					\
129} while (0)
130
131
132#define	ln_timer_ch	lle_timer.ln_timer_ch
133#define	la_timer	lle_timer.la_timer
134
135/* XXX bad name */
136#define	L3_ADDR(lle)	((struct sockaddr *)(&lle[1]))
137#define	L3_ADDR_LEN(lle)	(((struct sockaddr *)(&lle[1]))->sa_len)
138
139#ifndef LLTBL_HASHTBL_SIZE
140#define	LLTBL_HASHTBL_SIZE	32	/* default 32 ? */
141#endif
142
143#ifndef LLTBL_HASHMASK
144#define	LLTBL_HASHMASK	(LLTBL_HASHTBL_SIZE - 1)
145#endif
146
147typedef	struct llentry *(llt_lookup_t)(struct lltable *, u_int flags,
148    const struct sockaddr *l3addr);
149typedef	struct llentry *(llt_create_t)(struct lltable *, u_int flags,
150    const struct sockaddr *l3addr);
151typedef	int (llt_delete_t)(struct lltable *, u_int flags,
152    const struct sockaddr *l3addr);
153typedef void (llt_prefix_free_t)(struct lltable *,
154    const struct sockaddr *prefix, const struct sockaddr *mask, u_int flags);
155typedef int (llt_dump_t)(struct lltable *, struct sysctl_req *);
156
157struct lltable {
158	SLIST_ENTRY(lltable)	llt_link;
159	struct llentries	lle_head[LLTBL_HASHTBL_SIZE];
160	int			llt_af;
161	struct ifnet		*llt_ifp;
162
163	llt_lookup_t		*llt_lookup;
164	llt_create_t		*llt_create;
165	llt_delete_t		*llt_delete;
166	llt_prefix_free_t	*llt_prefix_free;
167	llt_dump_t		*llt_dump;
168};
169
170MALLOC_DECLARE(M_LLTABLE);
171
172/*
173 * LLentry flags
174 */
175#define	LLE_DELETED	0x0001	/* entry must be deleted */
176#define	LLE_STATIC	0x0002	/* entry is static */
177#define	LLE_IFADDR	0x0004	/* entry is interface addr */
178#define	LLE_VALID	0x0008	/* ll_addr is valid */
179#define	LLE_PUB		0x0020	/* publish entry ??? */
180#define	LLE_LINKED	0x0040	/* linked to lookup structure */
181/* LLE request flags */
182#define	LLE_EXCLUSIVE	0x2000	/* return lle xlocked  */
183
184#define LLATBL_HASH(key, mask) \
185	(((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask)
186
187struct lltable *lltable_init(struct ifnet *, int);
188void		lltable_free(struct lltable *);
189void		lltable_prefix_free(int, struct sockaddr *,
190		    struct sockaddr *, u_int);
191#if 0
192void		lltable_drain(int);
193#endif
194int		lltable_sysctl_dumparp(int, struct sysctl_req *);
195
196size_t		llentry_free(struct llentry *);
197struct llentry  *llentry_alloc(struct ifnet *, struct lltable *,
198		    struct sockaddr_storage *);
199
200/*
201 * Generic link layer address lookup function.
202 */
203static __inline struct llentry *
204lla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
205{
206
207	return (llt->llt_lookup(llt, flags, l3addr));
208}
209
210static __inline struct llentry *
211lla_create(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
212{
213
214	return (llt->llt_create(llt, flags, l3addr));
215}
216
217static __inline int
218lla_delete(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
219{
220
221	return (llt->llt_delete(llt, flags, l3addr));
222}
223
224
225int		lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *);
226
227#include <sys/eventhandler.h>
228enum {
229	LLENTRY_RESOLVED,
230	LLENTRY_TIMEDOUT,
231	LLENTRY_DELETED,
232	LLENTRY_EXPIRED,
233};
234typedef void (*lle_event_fn)(void *, struct llentry *, int);
235EVENTHANDLER_DECLARE(lle_event, lle_event_fn);
236#endif  /* _NET_IF_LLATBL_H_ */
237