if_llatbl.h revision 286616
1186121Skmacy/*
2186121Skmacy * Copyright (c) 2004 Luigi Rizzo, Alessandro Cerri. All rights reserved.
3186121Skmacy * Copyright (c) 2004-2008 Qing Li. All rights reserved.
4186121Skmacy * Copyright (c) 2008 Kip Macy. All rights reserved.
5186121Skmacy *
6186121Skmacy * Redistribution and use in source and binary forms, with or without
7186121Skmacy * modification, are permitted provided that the following conditions
8186121Skmacy * are met:
9186121Skmacy * 1. Redistributions of source code must retain the above copyright
10186121Skmacy *    notice, this list of conditions and the following disclaimer.
11186121Skmacy * 2. Redistributions in binary form must reproduce the above copyright
12186121Skmacy *    notice, this list of conditions and the following disclaimer in the
13186121Skmacy *    documentation and/or other materials provided with the distribution.
14186121Skmacy *
15186121Skmacy * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16186121Skmacy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17186121Skmacy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18186121Skmacy * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19186121Skmacy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20186121Skmacy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21186121Skmacy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22186121Skmacy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23186121Skmacy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24186121Skmacy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25186121Skmacy * SUCH DAMAGE.
26186121Skmacy */
27186121Skmacy#include <sys/cdefs.h>
28186121Skmacy__FBSDID("$FreeBSD: head/sys/net/if_llatbl.h 286616 2015-08-11 05:51:00Z melifaro $");
29186121Skmacy
30186121Skmacy#ifndef	_NET_IF_LLATBL_H_
31186121Skmacy#define	_NET_IF_LLATBL_H_
32186121Skmacy
33186121Skmacy#include <sys/_rwlock.h>
34186121Skmacy#include <netinet/in.h>
35186121Skmacy
36186121Skmacystruct ifnet;
37186121Skmacystruct sysctl_req;
38186121Skmacystruct rt_msghdr;
39186121Skmacystruct rt_addrinfo;
40186121Skmacy
41186121Skmacystruct llentry;
42186121SkmacyLIST_HEAD(llentries, llentry);
43186121Skmacy
44250300Sandreextern struct rwlock lltable_rwlock;
45196535Srwatson#define	LLTABLE_RLOCK()		rw_rlock(&lltable_rwlock)
46196535Srwatson#define	LLTABLE_RUNLOCK()	rw_runlock(&lltable_rwlock)
47196535Srwatson#define	LLTABLE_WLOCK()		rw_wlock(&lltable_rwlock)
48196535Srwatson#define	LLTABLE_WUNLOCK()	rw_wunlock(&lltable_rwlock)
49196535Srwatson#define	LLTABLE_LOCK_ASSERT()	rw_assert(&lltable_rwlock, RA_LOCKED)
50196535Srwatson
51186121Skmacy/*
52186121Skmacy * Code referencing llentry must at least hold
53186121Skmacy * a shared lock
54186121Skmacy */
55186121Skmacystruct llentry {
56186121Skmacy	LIST_ENTRY(llentry)	 lle_next;
57186121Skmacy	struct rwlock		 lle_lock;
58186121Skmacy	struct lltable		 *lle_tbl;
59186121Skmacy	struct llentries	 *lle_head;
60286577Smelifaro	void			(*lle_free)(struct llentry *);
61186121Skmacy	struct mbuf		 *la_hold;
62238945Sglebius	int			 la_numheld;  /* # of packets currently held */
63186121Skmacy	time_t			 la_expire;
64238945Sglebius	uint16_t		 la_flags;
65186121Skmacy	uint16_t		 la_asked;
66186121Skmacy	uint16_t		 la_preempt;
67186121Skmacy	uint16_t		 ln_byhint;
68186121Skmacy	int16_t			 ln_state;	/* IPv6 has ND6_LLINFO_NOSTATE == -2 */
69238945Sglebius	uint16_t		 ln_router;
70186121Skmacy	time_t			 ln_ntick;
71186121Skmacy	int			 lle_refcnt;
72238945Sglebius
73186121Skmacy	union {
74186121Skmacy		uint64_t	mac_aligned;
75186121Skmacy		uint16_t	mac16[3];
76219819Sjeff		uint8_t		mac8[20];	/* IB needs 20 bytes. */
77186121Skmacy	} ll_addr;
78186121Skmacy
79286577Smelifaro	LIST_ENTRY(llentry)	lle_chain;	/* chain of deleted items */
80186121Skmacy	/* XXX af-private? */
81186121Skmacy	union {
82186121Skmacy		struct callout	ln_timer_ch;
83186121Skmacy		struct callout  la_timer;
84186121Skmacy	} lle_timer;
85186121Skmacy	/* NB: struct sockaddr must immediately follow */
86186121Skmacy};
87186121Skmacy
88186121Skmacy#define	LLE_WLOCK(lle)		rw_wlock(&(lle)->lle_lock)
89186121Skmacy#define	LLE_RLOCK(lle)		rw_rlock(&(lle)->lle_lock)
90186121Skmacy#define	LLE_WUNLOCK(lle)	rw_wunlock(&(lle)->lle_lock)
91186121Skmacy#define	LLE_RUNLOCK(lle)	rw_runlock(&(lle)->lle_lock)
92186121Skmacy#define	LLE_DOWNGRADE(lle)	rw_downgrade(&(lle)->lle_lock)
93186121Skmacy#define	LLE_TRY_UPGRADE(lle)	rw_try_upgrade(&(lle)->lle_lock)
94186121Skmacy#define	LLE_LOCK_INIT(lle)	rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK)
95186149Skmacy#define	LLE_LOCK_DESTROY(lle)	rw_destroy(&(lle)->lle_lock)
96186121Skmacy#define	LLE_WLOCK_ASSERT(lle)	rw_assert(&(lle)->lle_lock, RA_WLOCKED)
97186121Skmacy
98186121Skmacy#define LLE_IS_VALID(lle)	(((lle) != NULL) && ((lle) != (void *)-1))
99186121Skmacy
100186121Skmacy#define	LLE_ADDREF(lle) do {					\
101186121Skmacy	LLE_WLOCK_ASSERT(lle);					\
102186121Skmacy	KASSERT((lle)->lle_refcnt >= 0,				\
103238990Sglebius	    ("negative refcnt %d on lle %p",			\
104238990Sglebius	    (lle)->lle_refcnt, (lle)));				\
105186121Skmacy	(lle)->lle_refcnt++;					\
106186121Skmacy} while (0)
107238945Sglebius
108186121Skmacy#define	LLE_REMREF(lle)	do {					\
109186121Skmacy	LLE_WLOCK_ASSERT(lle);					\
110238990Sglebius	KASSERT((lle)->lle_refcnt > 0,				\
111238990Sglebius	    ("bogus refcnt %d on lle %p",			\
112238990Sglebius	    (lle)->lle_refcnt, (lle)));				\
113186121Skmacy	(lle)->lle_refcnt--;					\
114186121Skmacy} while (0)
115186121Skmacy
116186121Skmacy#define	LLE_FREE_LOCKED(lle) do {				\
117238990Sglebius	if ((lle)->lle_refcnt == 1)				\
118286577Smelifaro		(lle)->lle_free(lle);				\
119186121Skmacy	else {							\
120238990Sglebius		LLE_REMREF(lle);				\
121186121Skmacy		LLE_WUNLOCK(lle);				\
122186121Skmacy	}							\
123186121Skmacy	/* guard against invalid refs */			\
124238990Sglebius	(lle) = NULL;						\
125186121Skmacy} while (0)
126186121Skmacy
127186121Skmacy#define	LLE_FREE(lle) do {					\
128186121Skmacy	LLE_WLOCK(lle);						\
129216859Sbz	LLE_FREE_LOCKED(lle);					\
130186121Skmacy} while (0)
131186121Skmacy
132186121Skmacy
133186121Skmacy#define	ln_timer_ch	lle_timer.ln_timer_ch
134186121Skmacy#define	la_timer	lle_timer.la_timer
135186121Skmacy
136186121Skmacy/* XXX bad name */
137286577Smelifaro#define	L3_CADDR(lle)	((const struct sockaddr *)(&lle[1]))
138186121Skmacy#define	L3_ADDR(lle)	((struct sockaddr *)(&lle[1]))
139186121Skmacy#define	L3_ADDR_LEN(lle)	(((struct sockaddr *)(&lle[1]))->sa_len)
140186121Skmacy
141286457Smelifarotypedef	struct llentry *(llt_lookup_t)(struct lltable *, u_int flags,
142286457Smelifaro    const struct sockaddr *l3addr);
143286457Smelifarotypedef	struct llentry *(llt_create_t)(struct lltable *, u_int flags,
144286457Smelifaro    const struct sockaddr *l3addr);
145286457Smelifarotypedef	int (llt_delete_t)(struct lltable *, u_int flags,
146286457Smelifaro    const struct sockaddr *l3addr);
147286457Smelifarotypedef void (llt_prefix_free_t)(struct lltable *,
148286457Smelifaro    const struct sockaddr *prefix, const struct sockaddr *mask, u_int flags);
149286577Smelifarotypedef int (llt_dump_entry_t)(struct lltable *, struct llentry *,
150286577Smelifaro    struct sysctl_req *);
151286577Smelifarotypedef uint32_t (llt_hash_t)(const struct llentry *, uint32_t);
152286577Smelifarotypedef int (llt_match_prefix_t)(const struct sockaddr *,
153286577Smelifaro    const struct sockaddr *, u_int, struct llentry *);
154286577Smelifarotypedef void (llt_free_entry_t)(struct lltable *, struct llentry *);
155286577Smelifarotypedef void (llt_fill_sa_entry_t)(const struct llentry *, struct sockaddr *);
156286616Smelifarotypedef void (llt_free_tbl_t)(struct lltable *);
157286577Smelifarotypedef void (llt_link_entry_t)(struct lltable *, struct llentry *);
158286577Smelifarotypedef void (llt_unlink_entry_t)(struct llentry *);
159286457Smelifaro
160286577Smelifarotypedef int (llt_foreach_cb_t)(struct lltable *, struct llentry *, void *);
161286577Smelifarotypedef int (llt_foreach_entry_t)(struct lltable *, llt_foreach_cb_t *, void *);
162286577Smelifaro
163186121Skmacystruct lltable {
164186121Skmacy	SLIST_ENTRY(lltable)	llt_link;
165186121Skmacy	int			llt_af;
166286616Smelifaro	int			llt_hsize;
167286616Smelifaro	struct llentries	*lle_head;
168186121Skmacy	struct ifnet		*llt_ifp;
169186121Skmacy
170286457Smelifaro	llt_lookup_t		*llt_lookup;
171286457Smelifaro	llt_create_t		*llt_create;
172286457Smelifaro	llt_delete_t		*llt_delete;
173286457Smelifaro	llt_prefix_free_t	*llt_prefix_free;
174286577Smelifaro	llt_dump_entry_t	*llt_dump_entry;
175286577Smelifaro	llt_hash_t		*llt_hash;
176286577Smelifaro	llt_match_prefix_t	*llt_match_prefix;
177286577Smelifaro	llt_free_entry_t	*llt_free_entry;
178286577Smelifaro	llt_foreach_entry_t	*llt_foreach_entry;
179286577Smelifaro	llt_link_entry_t	*llt_link_entry;
180286577Smelifaro	llt_unlink_entry_t	*llt_unlink_entry;
181286577Smelifaro	llt_fill_sa_entry_t	*llt_fill_sa_entry;
182286616Smelifaro	llt_free_tbl_t		*llt_free_tbl;
183186121Skmacy};
184286457Smelifaro
185186121SkmacyMALLOC_DECLARE(M_LLTABLE);
186186121Skmacy
187186121Skmacy/*
188286457Smelifaro * LLentry flags
189186121Skmacy */
190186121Skmacy#define	LLE_DELETED	0x0001	/* entry must be deleted */
191186121Skmacy#define	LLE_STATIC	0x0002	/* entry is static */
192186121Skmacy#define	LLE_IFADDR	0x0004	/* entry is interface addr */
193186121Skmacy#define	LLE_VALID	0x0008	/* ll_addr is valid */
194186121Skmacy#define	LLE_PUB		0x0020	/* publish entry ??? */
195238990Sglebius#define	LLE_LINKED	0x0040	/* linked to lookup structure */
196286457Smelifaro/* LLE request flags */
197238945Sglebius#define	LLE_EXCLUSIVE	0x2000	/* return lle xlocked  */
198186121Skmacy
199186121Skmacy#define LLATBL_HASH(key, mask) \
200186121Skmacy	(((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask)
201186121Skmacy
202286616Smelifarostruct lltable *lltable_allocate_htbl(uint32_t hsize);
203186121Skmacyvoid		lltable_free(struct lltable *);
204286616Smelifarovoid		lltable_link(struct lltable *llt);
205238945Sglebiusvoid		lltable_prefix_free(int, struct sockaddr *,
206238945Sglebius		    struct sockaddr *, u_int);
207213929Sbz#if 0
208186121Skmacyvoid		lltable_drain(int);
209213929Sbz#endif
210186121Skmacyint		lltable_sysctl_dumparp(int, struct sysctl_req *);
211186121Skmacy
212215207Sgnnsize_t		llentry_free(struct llentry *);
213238989Sglebiusstruct llentry  *llentry_alloc(struct ifnet *, struct lltable *,
214238989Sglebius		    struct sockaddr_storage *);
215186121Skmacy
216286577Smelifaro/* helper functions */
217286577Smelifarosize_t lltable_drop_entry_queue(struct llentry *);
218286577Smelifaro
219286577Smelifarostruct llentry *lltable_create_lle(struct lltable *llt, u_int flags,
220286577Smelifaro    const void *paddr);
221286577Smelifarovoid lltable_link_entry(struct lltable *llt, struct llentry *lle);
222286577Smelifarovoid lltable_unlink_entry(struct lltable *llt, struct llentry *lle);
223286577Smelifarovoid lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa);
224286577Smelifarostruct ifnet *lltable_get_ifp(const struct lltable *llt);
225286577Smelifaroint lltable_get_af(const struct lltable *llt);
226286577Smelifaro
227286577Smelifaroint lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f,
228286577Smelifaro    void *farg);
229186121Skmacy/*
230186121Skmacy * Generic link layer address lookup function.
231186121Skmacy */
232186121Skmacystatic __inline struct llentry *
233186121Skmacylla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
234186121Skmacy{
235286457Smelifaro
236286457Smelifaro	return (llt->llt_lookup(llt, flags, l3addr));
237186121Skmacy}
238186121Skmacy
239286457Smelifarostatic __inline struct llentry *
240286457Smelifarolla_create(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
241286457Smelifaro{
242286457Smelifaro
243286457Smelifaro	return (llt->llt_create(llt, flags, l3addr));
244286457Smelifaro}
245286457Smelifaro
246286457Smelifarostatic __inline int
247286457Smelifarolla_delete(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
248286457Smelifaro{
249286457Smelifaro
250286457Smelifaro	return (llt->llt_delete(llt, flags, l3addr));
251286457Smelifaro}
252286457Smelifaro
253286457Smelifaro
254186121Skmacyint		lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *);
255245924Snp
256245924Snp#include <sys/eventhandler.h>
257245924Snpenum {
258245924Snp	LLENTRY_RESOLVED,
259245924Snp	LLENTRY_TIMEDOUT,
260245924Snp	LLENTRY_DELETED,
261245924Snp	LLENTRY_EXPIRED,
262245924Snp};
263245924Snptypedef void (*lle_event_fn)(void *, struct llentry *, int);
264245924SnpEVENTHANDLER_DECLARE(lle_event, lle_event_fn);
265186121Skmacy#endif  /* _NET_IF_LLATBL_H_ */
266