if_llatbl.h revision 287798
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 287798 2015-09-14 19:17:25Z vangyzen $");
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;
57286624Smelifaro	union {
58286624Smelifaro		struct in_addr	addr4;
59286624Smelifaro		struct in6_addr	addr6;
60286624Smelifaro	} r_l3addr;
61286624Smelifaro	union {
62286624Smelifaro		uint64_t	mac_aligned;
63286624Smelifaro		uint16_t	mac16[3];
64286624Smelifaro		uint8_t		mac8[20];	/* IB needs 20 bytes. */
65286624Smelifaro	} ll_addr;
66286624Smelifaro	uint32_t		spare0;
67286624Smelifaro	uint64_t		spare1;
68286624Smelifaro
69186121Skmacy	struct lltable		 *lle_tbl;
70186121Skmacy	struct llentries	 *lle_head;
71286577Smelifaro	void			(*lle_free)(struct llentry *);
72186121Skmacy	struct mbuf		 *la_hold;
73238945Sglebius	int			 la_numheld;  /* # of packets currently held */
74186121Skmacy	time_t			 la_expire;
75238945Sglebius	uint16_t		 la_flags;
76186121Skmacy	uint16_t		 la_asked;
77186121Skmacy	uint16_t		 la_preempt;
78186121Skmacy	uint16_t		 ln_byhint;
79186121Skmacy	int16_t			 ln_state;	/* IPv6 has ND6_LLINFO_NOSTATE == -2 */
80238945Sglebius	uint16_t		 ln_router;
81186121Skmacy	time_t			 ln_ntick;
82186121Skmacy	int			 lle_refcnt;
83238945Sglebius
84286577Smelifaro	LIST_ENTRY(llentry)	lle_chain;	/* chain of deleted items */
85286629Smelifaro	struct callout		lle_timer;
86286624Smelifaro	struct rwlock		 lle_lock;
87186121Skmacy};
88186121Skmacy
89186121Skmacy#define	LLE_WLOCK(lle)		rw_wlock(&(lle)->lle_lock)
90186121Skmacy#define	LLE_RLOCK(lle)		rw_rlock(&(lle)->lle_lock)
91186121Skmacy#define	LLE_WUNLOCK(lle)	rw_wunlock(&(lle)->lle_lock)
92186121Skmacy#define	LLE_RUNLOCK(lle)	rw_runlock(&(lle)->lle_lock)
93186121Skmacy#define	LLE_DOWNGRADE(lle)	rw_downgrade(&(lle)->lle_lock)
94186121Skmacy#define	LLE_TRY_UPGRADE(lle)	rw_try_upgrade(&(lle)->lle_lock)
95186121Skmacy#define	LLE_LOCK_INIT(lle)	rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK)
96186149Skmacy#define	LLE_LOCK_DESTROY(lle)	rw_destroy(&(lle)->lle_lock)
97186121Skmacy#define	LLE_WLOCK_ASSERT(lle)	rw_assert(&(lle)->lle_lock, RA_WLOCKED)
98186121Skmacy
99186121Skmacy#define LLE_IS_VALID(lle)	(((lle) != NULL) && ((lle) != (void *)-1))
100186121Skmacy
101186121Skmacy#define	LLE_ADDREF(lle) do {					\
102186121Skmacy	LLE_WLOCK_ASSERT(lle);					\
103186121Skmacy	KASSERT((lle)->lle_refcnt >= 0,				\
104238990Sglebius	    ("negative refcnt %d on lle %p",			\
105238990Sglebius	    (lle)->lle_refcnt, (lle)));				\
106186121Skmacy	(lle)->lle_refcnt++;					\
107186121Skmacy} while (0)
108238945Sglebius
109186121Skmacy#define	LLE_REMREF(lle)	do {					\
110186121Skmacy	LLE_WLOCK_ASSERT(lle);					\
111238990Sglebius	KASSERT((lle)->lle_refcnt > 0,				\
112238990Sglebius	    ("bogus refcnt %d on lle %p",			\
113238990Sglebius	    (lle)->lle_refcnt, (lle)));				\
114186121Skmacy	(lle)->lle_refcnt--;					\
115186121Skmacy} while (0)
116186121Skmacy
117186121Skmacy#define	LLE_FREE_LOCKED(lle) do {				\
118238990Sglebius	if ((lle)->lle_refcnt == 1)				\
119286577Smelifaro		(lle)->lle_free(lle);				\
120186121Skmacy	else {							\
121238990Sglebius		LLE_REMREF(lle);				\
122186121Skmacy		LLE_WUNLOCK(lle);				\
123186121Skmacy	}							\
124186121Skmacy	/* guard against invalid refs */			\
125238990Sglebius	(lle) = NULL;						\
126186121Skmacy} while (0)
127186121Skmacy
128186121Skmacy#define	LLE_FREE(lle) do {					\
129186121Skmacy	LLE_WLOCK(lle);						\
130216859Sbz	LLE_FREE_LOCKED(lle);					\
131186121Skmacy} while (0)
132186121Skmacy
133186121Skmacy
134286457Smelifarotypedef	struct llentry *(llt_lookup_t)(struct lltable *, u_int flags,
135286457Smelifaro    const struct sockaddr *l3addr);
136286955Smelifarotypedef	struct llentry *(llt_alloc_t)(struct lltable *, u_int flags,
137286457Smelifaro    const struct sockaddr *l3addr);
138287789Smelifarotypedef	void (llt_delete_t)(struct lltable *, struct llentry *);
139286457Smelifarotypedef void (llt_prefix_free_t)(struct lltable *,
140287789Smelifaro    const struct sockaddr *addr, const struct sockaddr *mask, u_int flags);
141286577Smelifarotypedef int (llt_dump_entry_t)(struct lltable *, struct llentry *,
142286577Smelifaro    struct sysctl_req *);
143286577Smelifarotypedef uint32_t (llt_hash_t)(const struct llentry *, uint32_t);
144286577Smelifarotypedef int (llt_match_prefix_t)(const struct sockaddr *,
145286577Smelifaro    const struct sockaddr *, u_int, struct llentry *);
146286577Smelifarotypedef void (llt_free_entry_t)(struct lltable *, struct llentry *);
147286577Smelifarotypedef void (llt_fill_sa_entry_t)(const struct llentry *, struct sockaddr *);
148286616Smelifarotypedef void (llt_free_tbl_t)(struct lltable *);
149286577Smelifarotypedef void (llt_link_entry_t)(struct lltable *, struct llentry *);
150286577Smelifarotypedef void (llt_unlink_entry_t)(struct llentry *);
151286457Smelifaro
152286577Smelifarotypedef int (llt_foreach_cb_t)(struct lltable *, struct llentry *, void *);
153286577Smelifarotypedef int (llt_foreach_entry_t)(struct lltable *, llt_foreach_cb_t *, void *);
154286577Smelifaro
155186121Skmacystruct lltable {
156186121Skmacy	SLIST_ENTRY(lltable)	llt_link;
157186121Skmacy	int			llt_af;
158286616Smelifaro	int			llt_hsize;
159286616Smelifaro	struct llentries	*lle_head;
160186121Skmacy	struct ifnet		*llt_ifp;
161186121Skmacy
162286457Smelifaro	llt_lookup_t		*llt_lookup;
163286955Smelifaro	llt_alloc_t		*llt_alloc_entry;
164287789Smelifaro	llt_delete_t		*llt_delete_entry;
165286457Smelifaro	llt_prefix_free_t	*llt_prefix_free;
166286577Smelifaro	llt_dump_entry_t	*llt_dump_entry;
167286577Smelifaro	llt_hash_t		*llt_hash;
168286577Smelifaro	llt_match_prefix_t	*llt_match_prefix;
169286577Smelifaro	llt_free_entry_t	*llt_free_entry;
170286577Smelifaro	llt_foreach_entry_t	*llt_foreach_entry;
171286577Smelifaro	llt_link_entry_t	*llt_link_entry;
172286577Smelifaro	llt_unlink_entry_t	*llt_unlink_entry;
173286577Smelifaro	llt_fill_sa_entry_t	*llt_fill_sa_entry;
174286616Smelifaro	llt_free_tbl_t		*llt_free_tbl;
175186121Skmacy};
176286457Smelifaro
177186121SkmacyMALLOC_DECLARE(M_LLTABLE);
178186121Skmacy
179186121Skmacy/*
180286457Smelifaro * LLentry flags
181186121Skmacy */
182186121Skmacy#define	LLE_DELETED	0x0001	/* entry must be deleted */
183186121Skmacy#define	LLE_STATIC	0x0002	/* entry is static */
184186121Skmacy#define	LLE_IFADDR	0x0004	/* entry is interface addr */
185186121Skmacy#define	LLE_VALID	0x0008	/* ll_addr is valid */
186287798Svangyzen#define	LLE_REDIRECT	0x0010	/* installed by redirect; has host rtentry */
187186121Skmacy#define	LLE_PUB		0x0020	/* publish entry ??? */
188238990Sglebius#define	LLE_LINKED	0x0040	/* linked to lookup structure */
189286457Smelifaro/* LLE request flags */
190238945Sglebius#define	LLE_EXCLUSIVE	0x2000	/* return lle xlocked  */
191186121Skmacy
192186121Skmacy#define LLATBL_HASH(key, mask) \
193186121Skmacy	(((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask)
194186121Skmacy
195286616Smelifarostruct lltable *lltable_allocate_htbl(uint32_t hsize);
196186121Skmacyvoid		lltable_free(struct lltable *);
197286616Smelifarovoid		lltable_link(struct lltable *llt);
198238945Sglebiusvoid		lltable_prefix_free(int, struct sockaddr *,
199238945Sglebius		    struct sockaddr *, u_int);
200213929Sbz#if 0
201186121Skmacyvoid		lltable_drain(int);
202213929Sbz#endif
203186121Skmacyint		lltable_sysctl_dumparp(int, struct sysctl_req *);
204186121Skmacy
205215207Sgnnsize_t		llentry_free(struct llentry *);
206238989Sglebiusstruct llentry  *llentry_alloc(struct ifnet *, struct lltable *,
207238989Sglebius		    struct sockaddr_storage *);
208186121Skmacy
209286577Smelifaro/* helper functions */
210286577Smelifarosize_t lltable_drop_entry_queue(struct llentry *);
211286577Smelifaro
212286955Smelifarostruct llentry *lltable_alloc_entry(struct lltable *llt, u_int flags,
213286955Smelifaro    const struct sockaddr *l4addr);
214286955Smelifarovoid lltable_free_entry(struct lltable *llt, struct llentry *lle);
215287789Smelifaroint lltable_delete_addr(struct lltable *llt, u_int flags,
216287789Smelifaro    const struct sockaddr *l3addr);
217286577Smelifarovoid lltable_link_entry(struct lltable *llt, struct llentry *lle);
218286577Smelifarovoid lltable_unlink_entry(struct lltable *llt, struct llentry *lle);
219286577Smelifarovoid lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa);
220286577Smelifarostruct ifnet *lltable_get_ifp(const struct lltable *llt);
221286577Smelifaroint lltable_get_af(const struct lltable *llt);
222286577Smelifaro
223286577Smelifaroint lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f,
224286577Smelifaro    void *farg);
225186121Skmacy/*
226186121Skmacy * Generic link layer address lookup function.
227186121Skmacy */
228186121Skmacystatic __inline struct llentry *
229186121Skmacylla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
230186121Skmacy{
231286457Smelifaro
232286457Smelifaro	return (llt->llt_lookup(llt, flags, l3addr));
233186121Skmacy}
234186121Skmacy
235186121Skmacyint		lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *);
236245924Snp
237245924Snp#include <sys/eventhandler.h>
238245924Snpenum {
239245924Snp	LLENTRY_RESOLVED,
240245924Snp	LLENTRY_TIMEDOUT,
241245924Snp	LLENTRY_DELETED,
242245924Snp	LLENTRY_EXPIRED,
243245924Snp};
244245924Snptypedef void (*lle_event_fn)(void *, struct llentry *, int);
245245924SnpEVENTHANDLER_DECLARE(lle_event, lle_event_fn);
246186121Skmacy#endif  /* _NET_IF_LLATBL_H_ */
247