if_llatbl.h revision 186121
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 186121 2008-12-15 06:53:09Z kmacy $");
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
44/*
45 * Code referencing llentry must at least hold
46 * a shared lock
47 */
48struct llentry {
49	LIST_ENTRY(llentry)	 lle_next;
50	struct rwlock		 lle_lock;
51	struct lltable		 *lle_tbl;
52	struct llentries	 *lle_head;
53	struct mbuf		 *la_hold;
54	time_t			 la_expire;
55	uint16_t		 la_flags;
56	uint16_t		 la_asked;
57	uint16_t		 la_preempt;
58	uint16_t		 ln_byhint;
59	int16_t			 ln_state;	/* IPv6 has ND6_LLINFO_NOSTATE == -2 */
60	uint16_t		 ln_router;
61	time_t			 ln_ntick;
62	int			 lle_refcnt;
63
64	union {
65		uint64_t	mac_aligned;
66		uint16_t	mac16[3];
67	} ll_addr;
68
69	/* XXX af-private? */
70	union {
71		struct callout	ln_timer_ch;
72		struct callout  la_timer;
73	} lle_timer;
74	/* NB: struct sockaddr must immediately follow */
75};
76
77#define	LLE_WLOCK(lle)		rw_wlock(&(lle)->lle_lock)
78#define	LLE_RLOCK(lle)		rw_rlock(&(lle)->lle_lock)
79#define	LLE_WUNLOCK(lle)	rw_wunlock(&(lle)->lle_lock)
80#define	LLE_RUNLOCK(lle)	rw_runlock(&(lle)->lle_lock)
81#define	LLE_DOWNGRADE(lle)	rw_downgrade(&(lle)->lle_lock)
82#define	LLE_TRY_UPGRADE(lle)	rw_try_upgrade(&(lle)->lle_lock)
83#define	LLE_LOCK_INIT(lle)	rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK)
84#define	LLE_WLOCK_ASSERT(lle)	rw_assert(&(lle)->lle_lock, RA_WLOCKED)
85
86#define LLE_IS_VALID(lle)	(((lle) != NULL) && ((lle) != (void *)-1))
87
88#define	LLE_ADDREF(lle) do {					\
89	LLE_WLOCK_ASSERT(lle);					\
90	KASSERT((lle)->lle_refcnt >= 0,				\
91		("negative refcnt %d", (lle)->lle_refcnt));	\
92	(lle)->lle_refcnt++;					\
93} while (0)
94
95#define	LLE_REMREF(lle)	do {					\
96	LLE_WLOCK_ASSERT(lle);					\
97	KASSERT((lle)->lle_refcnt > 1,				\
98		("bogus refcnt %d", (lle)->lle_refcnt));	\
99	(lle)->lle_refcnt--;					\
100} while (0)
101
102#define	LLE_FREE_LOCKED(lle) do {				\
103	if ((lle)->lle_refcnt <= 1)				\
104		(lle)->lle_tbl->llt_free((lle)->lle_tbl, (lle));\
105	else {							\
106		(lle)->lle_refcnt--;				\
107		LLE_WUNLOCK(lle);				\
108	}							\
109	/* guard against invalid refs */			\
110	lle = 0;						\
111} while (0)
112
113#define	LLE_FREE(lle) do {					\
114	LLE_WLOCK(lle);						\
115	if ((lle)->lle_refcnt <= 1)				\
116		(lle)->lle_tbl->llt_free((lle)->lle_tbl, (lle));\
117	else {							\
118		(lle)->lle_refcnt--;				\
119		LLE_WUNLOCK(lle);				\
120	}							\
121	/* guard against invalid refs */			\
122	lle = 0;						\
123} while (0)
124
125
126#define	ln_timer_ch	lle_timer.ln_timer_ch
127#define	la_timer	lle_timer.la_timer
128
129/* XXX bad name */
130#define	L3_ADDR(lle)	((struct sockaddr *)(&lle[1]))
131#define	L3_ADDR_LEN(lle)	(((struct sockaddr *)(&lle[1]))->sa_len)
132
133#ifndef LLTBL_HASHTBL_SIZE
134#define	LLTBL_HASHTBL_SIZE	32	/* default 32 ? */
135#endif
136
137#ifndef LLTBL_HASHMASK
138#define	LLTBL_HASHMASK	(LLTBL_HASHTBL_SIZE - 1)
139#endif
140
141struct lltable {
142	SLIST_ENTRY(lltable)	llt_link;
143	struct llentries	lle_head[LLTBL_HASHTBL_SIZE];
144	int			llt_af;
145	struct ifnet		*llt_ifp;
146
147	struct llentry *	(*llt_new)(const struct sockaddr *, u_int);
148	void			(*llt_free)(struct lltable *, struct llentry *);
149	struct llentry *	(*llt_lookup)(struct lltable *, u_int flags,
150				    const struct sockaddr *l3addr);
151	int			(*llt_rtcheck)(struct ifnet *,
152				    const struct sockaddr *);
153	int			(*llt_dump)(struct lltable *,
154				     struct sysctl_req *);
155};
156MALLOC_DECLARE(M_LLTABLE);
157
158/*
159 * flags to be passed to arplookup.
160 */
161#define	LLE_DELETED	0x0001	/* entry must be deleted */
162#define	LLE_STATIC	0x0002	/* entry is static */
163#define	LLE_IFADDR	0x0004	/* entry is interface addr */
164#define	LLE_VALID	0x0008	/* ll_addr is valid */
165#define	LLE_PROXY	0x0010	/* proxy entry ??? */
166#define	LLE_PUB		0x0020	/* publish entry ??? */
167#define	LLE_DELETE	0x4000	/* delete on a lookup - match LLE_IFADDR */
168#define	LLE_CREATE	0x8000	/* create on a lookup miss */
169#define	LLE_EXCLUSIVE	0x2000	/* return lle xlocked  */
170
171#define LLATBL_HASH(key, mask) \
172	(((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask)
173
174struct lltable *lltable_init(struct ifnet *, int);
175void		lltable_free(struct lltable *);
176void		lltable_drain(int);
177int		lltable_sysctl_dumparp(int, struct sysctl_req *);
178
179void		llentry_free(struct llentry *);
180
181/*
182 * Generic link layer address lookup function.
183 */
184static __inline struct llentry *
185lla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
186{
187	return llt->llt_lookup(llt, flags, l3addr);
188}
189
190int		lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *);
191#endif  /* _NET_IF_LLATBL_H_ */
192