xref: /freebsd/sys/net/if_llatbl.h (revision 3e7a2321e36754ec10eb4b83a434197b896c0a45)
182f39c91SKip Macy /*
282f39c91SKip Macy  * Copyright (c) 2004 Luigi Rizzo, Alessandro Cerri. All rights reserved.
382f39c91SKip Macy  * Copyright (c) 2004-2008 Qing Li. All rights reserved.
482f39c91SKip Macy  * Copyright (c) 2008 Kip Macy. All rights reserved.
582f39c91SKip Macy  *
682f39c91SKip Macy  * Redistribution and use in source and binary forms, with or without
782f39c91SKip Macy  * modification, are permitted provided that the following conditions
882f39c91SKip Macy  * are met:
982f39c91SKip Macy  * 1. Redistributions of source code must retain the above copyright
1082f39c91SKip Macy  *    notice, this list of conditions and the following disclaimer.
1182f39c91SKip Macy  * 2. Redistributions in binary form must reproduce the above copyright
1282f39c91SKip Macy  *    notice, this list of conditions and the following disclaimer in the
1382f39c91SKip Macy  *    documentation and/or other materials provided with the distribution.
1482f39c91SKip Macy  *
1582f39c91SKip Macy  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1682f39c91SKip Macy  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1782f39c91SKip Macy  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1882f39c91SKip Macy  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
1982f39c91SKip Macy  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2082f39c91SKip Macy  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2182f39c91SKip Macy  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2282f39c91SKip Macy  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2382f39c91SKip Macy  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2482f39c91SKip Macy  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2582f39c91SKip Macy  * SUCH DAMAGE.
2682f39c91SKip Macy  */
2782f39c91SKip Macy #include <sys/cdefs.h>
2882f39c91SKip Macy __FBSDID("$FreeBSD$");
2982f39c91SKip Macy 
3082f39c91SKip Macy #ifndef	_NET_IF_LLATBL_H_
3182f39c91SKip Macy #define	_NET_IF_LLATBL_H_
3282f39c91SKip Macy 
3382f39c91SKip Macy #include <sys/_rwlock.h>
3482f39c91SKip Macy #include <netinet/in.h>
3582f39c91SKip Macy 
3682f39c91SKip Macy struct ifnet;
3782f39c91SKip Macy struct sysctl_req;
3882f39c91SKip Macy struct rt_msghdr;
3982f39c91SKip Macy struct rt_addrinfo;
4082f39c91SKip Macy 
4182f39c91SKip Macy struct llentry;
4282f39c91SKip Macy LIST_HEAD(llentries, llentry);
4382f39c91SKip Macy 
44f89d4c3aSAndre Oppermann extern struct rwlock lltable_rwlock;
45dc56e98fSRobert Watson #define	LLTABLE_RLOCK()		rw_rlock(&lltable_rwlock)
46dc56e98fSRobert Watson #define	LLTABLE_RUNLOCK()	rw_runlock(&lltable_rwlock)
47dc56e98fSRobert Watson #define	LLTABLE_WLOCK()		rw_wlock(&lltable_rwlock)
48dc56e98fSRobert Watson #define	LLTABLE_WUNLOCK()	rw_wunlock(&lltable_rwlock)
49dc56e98fSRobert Watson #define	LLTABLE_LOCK_ASSERT()	rw_assert(&lltable_rwlock, RA_LOCKED)
50dc56e98fSRobert Watson 
5182f39c91SKip Macy /*
5282f39c91SKip Macy  * Code referencing llentry must at least hold
5382f39c91SKip Macy  * a shared lock
5482f39c91SKip Macy  */
5582f39c91SKip Macy struct llentry {
5682f39c91SKip Macy 	LIST_ENTRY(llentry)	 lle_next;
57314294deSAlexander V. Chernikov 	union {
58314294deSAlexander V. Chernikov 		struct in_addr	addr4;
59314294deSAlexander V. Chernikov 		struct in6_addr	addr6;
60314294deSAlexander V. Chernikov 	} r_l3addr;
61314294deSAlexander V. Chernikov 	union {
62314294deSAlexander V. Chernikov 		uint64_t	mac_aligned;
63314294deSAlexander V. Chernikov 		uint16_t	mac16[3];
64314294deSAlexander V. Chernikov 		uint8_t		mac8[20];	/* IB needs 20 bytes. */
65314294deSAlexander V. Chernikov 	} ll_addr;
66314294deSAlexander V. Chernikov 	uint32_t		spare0;
67314294deSAlexander V. Chernikov 	uint64_t		spare1;
68314294deSAlexander V. Chernikov 
6982f39c91SKip Macy 	struct lltable		 *lle_tbl;
7082f39c91SKip Macy 	struct llentries	 *lle_head;
7111cdad98SAlexander V. Chernikov 	void			(*lle_free)(struct llentry *);
7282f39c91SKip Macy 	struct mbuf		 *la_hold;
73e162ea60SGeorge V. Neville-Neil 	int			 la_numheld;  /* # of packets currently held */
7482f39c91SKip Macy 	time_t			 la_expire;
7582f39c91SKip Macy 	uint16_t		 la_flags;
7682f39c91SKip Macy 	uint16_t		 la_asked;
7782f39c91SKip Macy 	uint16_t		 la_preempt;
7882f39c91SKip Macy 	uint16_t		 ln_byhint;
7982f39c91SKip Macy 	int16_t			 ln_state;	/* IPv6 has ND6_LLINFO_NOSTATE == -2 */
8082f39c91SKip Macy 	uint16_t		 ln_router;
8182f39c91SKip Macy 	time_t			 ln_ntick;
8282f39c91SKip Macy 	int			 lle_refcnt;
8382f39c91SKip Macy 
8411cdad98SAlexander V. Chernikov 	LIST_ENTRY(llentry)	lle_chain;	/* chain of deleted items */
850447c136SAlexander V. Chernikov 	struct callout		lle_timer;
86314294deSAlexander V. Chernikov 	struct rwlock		 lle_lock;
8782f39c91SKip Macy };
8882f39c91SKip Macy 
8982f39c91SKip Macy #define	LLE_WLOCK(lle)		rw_wlock(&(lle)->lle_lock)
9082f39c91SKip Macy #define	LLE_RLOCK(lle)		rw_rlock(&(lle)->lle_lock)
9182f39c91SKip Macy #define	LLE_WUNLOCK(lle)	rw_wunlock(&(lle)->lle_lock)
9282f39c91SKip Macy #define	LLE_RUNLOCK(lle)	rw_runlock(&(lle)->lle_lock)
9382f39c91SKip Macy #define	LLE_DOWNGRADE(lle)	rw_downgrade(&(lle)->lle_lock)
9482f39c91SKip Macy #define	LLE_TRY_UPGRADE(lle)	rw_try_upgrade(&(lle)->lle_lock)
9582f39c91SKip Macy #define	LLE_LOCK_INIT(lle)	rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK)
968a61a4eeSKip Macy #define	LLE_LOCK_DESTROY(lle)	rw_destroy(&(lle)->lle_lock)
9782f39c91SKip Macy #define	LLE_WLOCK_ASSERT(lle)	rw_assert(&(lle)->lle_lock, RA_WLOCKED)
9882f39c91SKip Macy 
9982f39c91SKip Macy #define LLE_IS_VALID(lle)	(((lle) != NULL) && ((lle) != (void *)-1))
10082f39c91SKip Macy 
10182f39c91SKip Macy #define	LLE_ADDREF(lle) do {					\
10282f39c91SKip Macy 	LLE_WLOCK_ASSERT(lle);					\
10382f39c91SKip Macy 	KASSERT((lle)->lle_refcnt >= 0,				\
104ea537929SGleb Smirnoff 	    ("negative refcnt %d on lle %p",			\
105ea537929SGleb Smirnoff 	    (lle)->lle_refcnt, (lle)));				\
10682f39c91SKip Macy 	(lle)->lle_refcnt++;					\
10782f39c91SKip Macy } while (0)
108ea50c13eSGleb Smirnoff 
10982f39c91SKip Macy #define	LLE_REMREF(lle)	do {					\
11082f39c91SKip Macy 	LLE_WLOCK_ASSERT(lle);					\
111ea537929SGleb Smirnoff 	KASSERT((lle)->lle_refcnt > 0,				\
112ea537929SGleb Smirnoff 	    ("bogus refcnt %d on lle %p",			\
113ea537929SGleb Smirnoff 	    (lle)->lle_refcnt, (lle)));				\
11482f39c91SKip Macy 	(lle)->lle_refcnt--;					\
11582f39c91SKip Macy } while (0)
11682f39c91SKip Macy 
11782f39c91SKip Macy #define	LLE_FREE_LOCKED(lle) do {				\
118ea537929SGleb Smirnoff 	if ((lle)->lle_refcnt == 1)				\
11911cdad98SAlexander V. Chernikov 		(lle)->lle_free(lle);				\
12082f39c91SKip Macy 	else {							\
121ea537929SGleb Smirnoff 		LLE_REMREF(lle);				\
12282f39c91SKip Macy 		LLE_WUNLOCK(lle);				\
12382f39c91SKip Macy 	}							\
12482f39c91SKip Macy 	/* guard against invalid refs */			\
125ea537929SGleb Smirnoff 	(lle) = NULL;						\
12682f39c91SKip Macy } while (0)
12782f39c91SKip Macy 
12882f39c91SKip Macy #define	LLE_FREE(lle) do {					\
12982f39c91SKip Macy 	LLE_WLOCK(lle);						\
130962be6dfSBjoern A. Zeeb 	LLE_FREE_LOCKED(lle);					\
13182f39c91SKip Macy } while (0)
13282f39c91SKip Macy 
13382f39c91SKip Macy 
134b4b1367aSAlexander V. Chernikov typedef	struct llentry *(llt_lookup_t)(struct lltable *, u_int flags,
135b4b1367aSAlexander V. Chernikov     const struct sockaddr *l3addr);
1365a255516SAlexander V. Chernikov typedef	struct llentry *(llt_alloc_t)(struct lltable *, u_int flags,
137b4b1367aSAlexander V. Chernikov     const struct sockaddr *l3addr);
138*3e7a2321SAlexander V. Chernikov typedef	void (llt_delete_t)(struct lltable *, struct llentry *);
139b4b1367aSAlexander V. Chernikov typedef void (llt_prefix_free_t)(struct lltable *,
140*3e7a2321SAlexander V. Chernikov     const struct sockaddr *addr, const struct sockaddr *mask, u_int flags);
14111cdad98SAlexander V. Chernikov typedef int (llt_dump_entry_t)(struct lltable *, struct llentry *,
14211cdad98SAlexander V. Chernikov     struct sysctl_req *);
14311cdad98SAlexander V. Chernikov typedef uint32_t (llt_hash_t)(const struct llentry *, uint32_t);
14411cdad98SAlexander V. Chernikov typedef int (llt_match_prefix_t)(const struct sockaddr *,
14511cdad98SAlexander V. Chernikov     const struct sockaddr *, u_int, struct llentry *);
14611cdad98SAlexander V. Chernikov typedef void (llt_free_entry_t)(struct lltable *, struct llentry *);
14711cdad98SAlexander V. Chernikov typedef void (llt_fill_sa_entry_t)(const struct llentry *, struct sockaddr *);
14841cb42a6SAlexander V. Chernikov typedef void (llt_free_tbl_t)(struct lltable *);
14911cdad98SAlexander V. Chernikov typedef void (llt_link_entry_t)(struct lltable *, struct llentry *);
15011cdad98SAlexander V. Chernikov typedef void (llt_unlink_entry_t)(struct llentry *);
15111cdad98SAlexander V. Chernikov 
15211cdad98SAlexander V. Chernikov typedef int (llt_foreach_cb_t)(struct lltable *, struct llentry *, void *);
15311cdad98SAlexander V. Chernikov typedef int (llt_foreach_entry_t)(struct lltable *, llt_foreach_cb_t *, void *);
154b4b1367aSAlexander V. Chernikov 
15582f39c91SKip Macy struct lltable {
15682f39c91SKip Macy 	SLIST_ENTRY(lltable)	llt_link;
15782f39c91SKip Macy 	int			llt_af;
15841cb42a6SAlexander V. Chernikov 	int			llt_hsize;
15941cb42a6SAlexander V. Chernikov 	struct llentries	*lle_head;
16082f39c91SKip Macy 	struct ifnet		*llt_ifp;
16182f39c91SKip Macy 
162b4b1367aSAlexander V. Chernikov 	llt_lookup_t		*llt_lookup;
1635a255516SAlexander V. Chernikov 	llt_alloc_t		*llt_alloc_entry;
164*3e7a2321SAlexander V. Chernikov 	llt_delete_t		*llt_delete_entry;
165b4b1367aSAlexander V. Chernikov 	llt_prefix_free_t	*llt_prefix_free;
16611cdad98SAlexander V. Chernikov 	llt_dump_entry_t	*llt_dump_entry;
16711cdad98SAlexander V. Chernikov 	llt_hash_t		*llt_hash;
16811cdad98SAlexander V. Chernikov 	llt_match_prefix_t	*llt_match_prefix;
16911cdad98SAlexander V. Chernikov 	llt_free_entry_t	*llt_free_entry;
17011cdad98SAlexander V. Chernikov 	llt_foreach_entry_t	*llt_foreach_entry;
17111cdad98SAlexander V. Chernikov 	llt_link_entry_t	*llt_link_entry;
17211cdad98SAlexander V. Chernikov 	llt_unlink_entry_t	*llt_unlink_entry;
17311cdad98SAlexander V. Chernikov 	llt_fill_sa_entry_t	*llt_fill_sa_entry;
17441cb42a6SAlexander V. Chernikov 	llt_free_tbl_t		*llt_free_tbl;
17582f39c91SKip Macy };
176b4b1367aSAlexander V. Chernikov 
17782f39c91SKip Macy MALLOC_DECLARE(M_LLTABLE);
17882f39c91SKip Macy 
17982f39c91SKip Macy /*
180e362cf0eSAlexander V. Chernikov  * LLentry flags
18182f39c91SKip Macy  */
18282f39c91SKip Macy #define	LLE_DELETED	0x0001	/* entry must be deleted */
18382f39c91SKip Macy #define	LLE_STATIC	0x0002	/* entry is static */
18482f39c91SKip Macy #define	LLE_IFADDR	0x0004	/* entry is interface addr */
18582f39c91SKip Macy #define	LLE_VALID	0x0008	/* ll_addr is valid */
18682f39c91SKip Macy #define	LLE_PUB		0x0020	/* publish entry ??? */
187ea537929SGleb Smirnoff #define	LLE_LINKED	0x0040	/* linked to lookup structure */
188b4b1367aSAlexander V. Chernikov /* LLE request flags */
189ea50c13eSGleb Smirnoff #define	LLE_EXCLUSIVE	0x2000	/* return lle xlocked  */
19082f39c91SKip Macy 
19182f39c91SKip Macy #define LLATBL_HASH(key, mask) \
19282f39c91SKip Macy 	(((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask)
19382f39c91SKip Macy 
19441cb42a6SAlexander V. Chernikov struct lltable *lltable_allocate_htbl(uint32_t hsize);
19582f39c91SKip Macy void		lltable_free(struct lltable *);
19641cb42a6SAlexander V. Chernikov void		lltable_link(struct lltable *llt);
197c9d763bfSQing Li void		lltable_prefix_free(int, struct sockaddr *,
1985b84dc78SQing Li 		    struct sockaddr *, u_int);
199fc2bfb32SBjoern A. Zeeb #if 0
20082f39c91SKip Macy void		lltable_drain(int);
201fc2bfb32SBjoern A. Zeeb #endif
20282f39c91SKip Macy int		lltable_sysctl_dumparp(int, struct sysctl_req *);
20382f39c91SKip Macy 
204e162ea60SGeorge V. Neville-Neil size_t		llentry_free(struct llentry *);
205b1d86af7SGleb Smirnoff struct llentry  *llentry_alloc(struct ifnet *, struct lltable *,
206b1d86af7SGleb Smirnoff 		    struct sockaddr_storage *);
20782f39c91SKip Macy 
20811cdad98SAlexander V. Chernikov /* helper functions */
20911cdad98SAlexander V. Chernikov size_t lltable_drop_entry_queue(struct llentry *);
21011cdad98SAlexander V. Chernikov 
2115a255516SAlexander V. Chernikov struct llentry *lltable_alloc_entry(struct lltable *llt, u_int flags,
2125a255516SAlexander V. Chernikov     const struct sockaddr *l4addr);
2135a255516SAlexander V. Chernikov void lltable_free_entry(struct lltable *llt, struct llentry *lle);
214*3e7a2321SAlexander V. Chernikov int lltable_delete_addr(struct lltable *llt, u_int flags,
215*3e7a2321SAlexander V. Chernikov     const struct sockaddr *l3addr);
21611cdad98SAlexander V. Chernikov void lltable_link_entry(struct lltable *llt, struct llentry *lle);
21711cdad98SAlexander V. Chernikov void lltable_unlink_entry(struct lltable *llt, struct llentry *lle);
21811cdad98SAlexander V. Chernikov void lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa);
21911cdad98SAlexander V. Chernikov struct ifnet *lltable_get_ifp(const struct lltable *llt);
22011cdad98SAlexander V. Chernikov int lltable_get_af(const struct lltable *llt);
22111cdad98SAlexander V. Chernikov 
22211cdad98SAlexander V. Chernikov int lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f,
22311cdad98SAlexander V. Chernikov     void *farg);
22482f39c91SKip Macy /*
22582f39c91SKip Macy  * Generic link layer address lookup function.
22682f39c91SKip Macy  */
22782f39c91SKip Macy static __inline struct llentry *
22882f39c91SKip Macy lla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
22982f39c91SKip Macy {
230b4b1367aSAlexander V. Chernikov 
231e362cf0eSAlexander V. Chernikov 	return (llt->llt_lookup(llt, flags, l3addr));
23282f39c91SKip Macy }
23382f39c91SKip Macy 
23482f39c91SKip Macy int		lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *);
2354364ec08SNavdeep Parhar 
2364364ec08SNavdeep Parhar #include <sys/eventhandler.h>
2374364ec08SNavdeep Parhar enum {
2384364ec08SNavdeep Parhar 	LLENTRY_RESOLVED,
2394364ec08SNavdeep Parhar 	LLENTRY_TIMEDOUT,
2404364ec08SNavdeep Parhar 	LLENTRY_DELETED,
2414364ec08SNavdeep Parhar 	LLENTRY_EXPIRED,
2424364ec08SNavdeep Parhar };
2434364ec08SNavdeep Parhar typedef void (*lle_event_fn)(void *, struct llentry *, int);
2444364ec08SNavdeep Parhar EVENTHANDLER_DECLARE(lle_event, lle_event_fn);
24582f39c91SKip Macy #endif  /* _NET_IF_LLATBL_H_ */
246