if_llatbl.h revision 191154
154272Ssos/*
255522Ssos * Copyright (c) 2004 Luigi Rizzo, Alessandro Cerri. All rights reserved.
354272Ssos * Copyright (c) 2004-2008 Qing Li. All rights reserved.
454272Ssos * Copyright (c) 2008 Kip Macy. All rights reserved.
554272Ssos *
654272Ssos * Redistribution and use in source and binary forms, with or without
754272Ssos * modification, are permitted provided that the following conditions
854272Ssos * are met:
954272Ssos * 1. Redistributions of source code must retain the above copyright
1054272Ssos *    notice, this list of conditions and the following disclaimer.
1154272Ssos * 2. Redistributions in binary form must reproduce the above copyright
1254272Ssos *    notice, this list of conditions and the following disclaimer in the
1354272Ssos *    documentation and/or other materials provided with the distribution.
1454272Ssos *
1554272Ssos * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1654272Ssos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1754272Ssos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1854272Ssos * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
1954272Ssos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2054272Ssos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2154272Ssos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2254272Ssos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2354272Ssos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2454272Ssos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2554272Ssos * SUCH DAMAGE.
2654272Ssos */
2754272Ssos#include <sys/cdefs.h>
2854272Ssos__FBSDID("$FreeBSD: head/sys/net/if_llatbl.h 191154 2009-04-16 22:04:07Z kmacy $");
2954272Ssos
3054272Ssos#ifndef	_NET_IF_LLATBL_H_
3155522Ssos#define	_NET_IF_LLATBL_H_
3255522Ssos
3355522Ssos#include <sys/_rwlock.h>
3454272Ssos#include <netinet/in.h>
3554272Ssos
3654272Ssosstruct ifnet;
3754272Ssosstruct sysctl_req;
3854272Ssosstruct rt_msghdr;
3954272Ssosstruct rt_addrinfo;
4054272Ssos
4154272Ssosstruct llentry;
4254272SsosLIST_HEAD(llentries, llentry);
4354272Ssos
4454272Ssos/*
4554272Ssos * Code referencing llentry must at least hold
4654272Ssos * a shared lock
4754272Ssos */
4854272Ssosstruct llentry {
4954272Ssos	LIST_ENTRY(llentry)	 lle_next;
5054272Ssos	struct rwlock		 lle_lock;
5154272Ssos	struct lltable		 *lle_tbl;
5254272Ssos	struct llentries	 *lle_head;
5354272Ssos	struct mbuf		 *la_hold;
5454272Ssos	time_t			 la_expire;
5554272Ssos	uint16_t		 la_flags;
5654272Ssos	uint16_t		 la_asked;
5754272Ssos	uint16_t		 la_preempt;
5854272Ssos	uint16_t		 ln_byhint;
5954272Ssos	int16_t			 ln_state;	/* IPv6 has ND6_LLINFO_NOSTATE == -2 */
6054272Ssos	uint16_t		 ln_router;
6154272Ssos	time_t			 ln_ntick;
6254272Ssos	int			 lle_refcnt;
6354272Ssos
6454272Ssos	union {
6554272Ssos		uint64_t	mac_aligned;
6654272Ssos		uint16_t	mac16[3];
6754272Ssos	} ll_addr;
6854272Ssos
6954272Ssos	/* XXX af-private? */
7054272Ssos	union {
7154272Ssos		struct callout	ln_timer_ch;
7254272Ssos		struct callout  la_timer;
7354272Ssos	} lle_timer;
7454272Ssos	/* NB: struct sockaddr must immediately follow */
7554272Ssos};
7654272Ssos
7754272Ssos#define	LLE_WLOCK(lle)		rw_wlock(&(lle)->lle_lock)
7854272Ssos#define	LLE_RLOCK(lle)		rw_rlock(&(lle)->lle_lock)
7954272Ssos#define	LLE_WUNLOCK(lle)	rw_wunlock(&(lle)->lle_lock)
8054272Ssos#define	LLE_RUNLOCK(lle)	rw_runlock(&(lle)->lle_lock)
8154272Ssos#define	LLE_DOWNGRADE(lle)	rw_downgrade(&(lle)->lle_lock)
8254272Ssos#define	LLE_TRY_UPGRADE(lle)	rw_try_upgrade(&(lle)->lle_lock)
8354272Ssos#define	LLE_LOCK_INIT(lle)	rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK)
8454272Ssos#define	LLE_LOCK_DESTROY(lle)	rw_destroy(&(lle)->lle_lock)
8560422Sken#define	LLE_WLOCK_ASSERT(lle)	rw_assert(&(lle)->lle_lock, RA_WLOCKED)
8660422Sken
8754272Ssos#define LLE_IS_VALID(lle)	(((lle) != NULL) && ((lle) != (void *)-1))
8854272Ssos
8954272Ssos#define	LLE_ADDREF(lle) do {					\
9054272Ssos	LLE_WLOCK_ASSERT(lle);					\
9154272Ssos	KASSERT((lle)->lle_refcnt >= 0,				\
9254272Ssos		("negative refcnt %d", (lle)->lle_refcnt));	\
9354272Ssos	(lle)->lle_refcnt++;					\
9454272Ssos} while (0)
9554272Ssos
9654272Ssos#define	LLE_REMREF(lle)	do {					\
9754272Ssos	LLE_WLOCK_ASSERT(lle);					\
9854272Ssos	KASSERT((lle)->lle_refcnt > 1,				\
9954272Ssos		("bogus refcnt %d", (lle)->lle_refcnt));	\
10054272Ssos	(lle)->lle_refcnt--;					\
10154272Ssos} while (0)
10254272Ssos
10354272Ssos#define	LLE_FREE_LOCKED(lle) do {				\
10454272Ssos	if ((lle)->lle_refcnt <= 1)				\
10555522Ssos		(lle)->lle_tbl->llt_free((lle)->lle_tbl, (lle));\
10655522Ssos	else {							\
10755522Ssos		(lle)->lle_refcnt--;				\
10855522Ssos		LLE_WUNLOCK(lle);				\
10955522Ssos	}							\
11083266Speter	/* guard against invalid refs */			\
111	lle = 0;						\
112} while (0)
113
114#define	LLE_FREE(lle) do {					\
115	LLE_WLOCK(lle);						\
116	if ((lle)->lle_refcnt <= 1)				\
117		(lle)->lle_tbl->llt_free((lle)->lle_tbl, (lle));\
118	else {							\
119		(lle)->lle_refcnt--;				\
120		LLE_WUNLOCK(lle);				\
121	}							\
122	/* guard against invalid refs */			\
123	lle = NULL;						\
124} while (0)
125
126
127#define	ln_timer_ch	lle_timer.ln_timer_ch
128#define	la_timer	lle_timer.la_timer
129
130/* XXX bad name */
131#define	L3_ADDR(lle)	((struct sockaddr *)(&lle[1]))
132#define	L3_ADDR_LEN(lle)	(((struct sockaddr *)(&lle[1]))->sa_len)
133
134#ifndef LLTBL_HASHTBL_SIZE
135#define	LLTBL_HASHTBL_SIZE	32	/* default 32 ? */
136#endif
137
138#ifndef LLTBL_HASHMASK
139#define	LLTBL_HASHMASK	(LLTBL_HASHTBL_SIZE - 1)
140#endif
141
142struct lltable {
143	SLIST_ENTRY(lltable)	llt_link;
144	struct llentries	lle_head[LLTBL_HASHTBL_SIZE];
145	int			llt_af;
146	struct ifnet		*llt_ifp;
147
148	struct llentry *	(*llt_new)(const struct sockaddr *, u_int);
149	void			(*llt_free)(struct lltable *, struct llentry *);
150	struct llentry *	(*llt_lookup)(struct lltable *, u_int flags,
151				    const struct sockaddr *l3addr);
152	int			(*llt_rtcheck)(struct ifnet *,
153				    const struct sockaddr *);
154	int			(*llt_dump)(struct lltable *,
155				     struct sysctl_req *);
156};
157MALLOC_DECLARE(M_LLTABLE);
158
159/*
160 * flags to be passed to arplookup.
161 */
162#define	LLE_DELETED	0x0001	/* entry must be deleted */
163#define	LLE_STATIC	0x0002	/* entry is static */
164#define	LLE_IFADDR	0x0004	/* entry is interface addr */
165#define	LLE_VALID	0x0008	/* ll_addr is valid */
166#define	LLE_PROXY	0x0010	/* proxy entry ??? */
167#define	LLE_PUB		0x0020	/* publish entry ??? */
168#define	LLE_DELETE	0x4000	/* delete on a lookup - match LLE_IFADDR */
169#define	LLE_CREATE	0x8000	/* create on a lookup miss */
170#define	LLE_EXCLUSIVE	0x2000	/* return lle xlocked  */
171
172#define LLATBL_HASH(key, mask) \
173	(((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask)
174
175struct lltable *lltable_init(struct ifnet *, int);
176void		lltable_free(struct lltable *);
177void		lltable_drain(int);
178int		lltable_sysctl_dumparp(int, struct sysctl_req *);
179
180void		llentry_free(struct llentry *);
181int		llentry_update(struct llentry **, struct lltable *,
182                       struct sockaddr *, struct ifnet *);
183
184/*
185 * Generic link layer address lookup function.
186 */
187static __inline struct llentry *
188lla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
189{
190	return llt->llt_lookup(llt, flags, l3addr);
191}
192
193int		lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *);
194#endif  /* _NET_IF_LLATBL_H_ */
195