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: releng/11.0/sys/net/if_llatbl.h 301217 2016-06-02 17:51:29Z gnn $");
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
51292978Smelifaro#define	LLE_MAX_LINKHDR		24	/* Full IB header */
52186121Skmacy/*
53186121Skmacy * Code referencing llentry must at least hold
54186121Skmacy * a shared lock
55186121Skmacy */
56186121Skmacystruct llentry {
57186121Skmacy	LIST_ENTRY(llentry)	 lle_next;
58286624Smelifaro	union {
59286624Smelifaro		struct in_addr	addr4;
60286624Smelifaro		struct in6_addr	addr6;
61286624Smelifaro	} r_l3addr;
62292978Smelifaro	char			r_linkdata[LLE_MAX_LINKHDR]; /* L2 data */
63292978Smelifaro	uint8_t			r_hdrlen;	/* length for LL header */
64292978Smelifaro	uint8_t			spare0[3];
65291853Smelifaro	uint16_t		r_flags;	/* LLE runtime flags */
66291853Smelifaro	uint16_t		r_skip_req;	/* feedback from fast path */
67286624Smelifaro
68186121Skmacy	struct lltable		 *lle_tbl;
69186121Skmacy	struct llentries	 *lle_head;
70286577Smelifaro	void			(*lle_free)(struct llentry *);
71186121Skmacy	struct mbuf		 *la_hold;
72238945Sglebius	int			 la_numheld;  /* # of packets currently held */
73186121Skmacy	time_t			 la_expire;
74238945Sglebius	uint16_t		 la_flags;
75186121Skmacy	uint16_t		 la_asked;
76186121Skmacy	uint16_t		 la_preempt;
77186121Skmacy	int16_t			 ln_state;	/* IPv6 has ND6_LLINFO_NOSTATE == -2 */
78238945Sglebius	uint16_t		 ln_router;
79186121Skmacy	time_t			 ln_ntick;
80292155Smelifaro	time_t			lle_remtime;	/* Real time remaining */
81292155Smelifaro	time_t			lle_hittime;	/* Time when r_skip_req was unset */
82186121Skmacy	int			 lle_refcnt;
83292978Smelifaro	char			*ll_addr;	/* link-layer address */
84238945Sglebius
85286577Smelifaro	LIST_ENTRY(llentry)	lle_chain;	/* chain of deleted items */
86286629Smelifaro	struct callout		lle_timer;
87286624Smelifaro	struct rwlock		 lle_lock;
88291853Smelifaro	struct mtx		req_mtx;
89186121Skmacy};
90186121Skmacy
91186121Skmacy#define	LLE_WLOCK(lle)		rw_wlock(&(lle)->lle_lock)
92186121Skmacy#define	LLE_RLOCK(lle)		rw_rlock(&(lle)->lle_lock)
93186121Skmacy#define	LLE_WUNLOCK(lle)	rw_wunlock(&(lle)->lle_lock)
94186121Skmacy#define	LLE_RUNLOCK(lle)	rw_runlock(&(lle)->lle_lock)
95186121Skmacy#define	LLE_DOWNGRADE(lle)	rw_downgrade(&(lle)->lle_lock)
96186121Skmacy#define	LLE_TRY_UPGRADE(lle)	rw_try_upgrade(&(lle)->lle_lock)
97186121Skmacy#define	LLE_LOCK_INIT(lle)	rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK)
98186149Skmacy#define	LLE_LOCK_DESTROY(lle)	rw_destroy(&(lle)->lle_lock)
99186121Skmacy#define	LLE_WLOCK_ASSERT(lle)	rw_assert(&(lle)->lle_lock, RA_WLOCKED)
100186121Skmacy
101291853Smelifaro#define	LLE_REQ_INIT(lle)	mtx_init(&(lle)->req_mtx, "lle req", \
102291853Smelifaro	NULL, MTX_DEF)
103291853Smelifaro#define	LLE_REQ_DESTROY(lle)	mtx_destroy(&(lle)->req_mtx)
104291853Smelifaro#define	LLE_REQ_LOCK(lle)	mtx_lock(&(lle)->req_mtx)
105291853Smelifaro#define	LLE_REQ_UNLOCK(lle)	mtx_unlock(&(lle)->req_mtx)
106291853Smelifaro
107186121Skmacy#define LLE_IS_VALID(lle)	(((lle) != NULL) && ((lle) != (void *)-1))
108186121Skmacy
109186121Skmacy#define	LLE_ADDREF(lle) do {					\
110186121Skmacy	LLE_WLOCK_ASSERT(lle);					\
111186121Skmacy	KASSERT((lle)->lle_refcnt >= 0,				\
112238990Sglebius	    ("negative refcnt %d on lle %p",			\
113238990Sglebius	    (lle)->lle_refcnt, (lle)));				\
114186121Skmacy	(lle)->lle_refcnt++;					\
115186121Skmacy} while (0)
116238945Sglebius
117186121Skmacy#define	LLE_REMREF(lle)	do {					\
118186121Skmacy	LLE_WLOCK_ASSERT(lle);					\
119238990Sglebius	KASSERT((lle)->lle_refcnt > 0,				\
120238990Sglebius	    ("bogus refcnt %d on lle %p",			\
121238990Sglebius	    (lle)->lle_refcnt, (lle)));				\
122186121Skmacy	(lle)->lle_refcnt--;					\
123186121Skmacy} while (0)
124186121Skmacy
125186121Skmacy#define	LLE_FREE_LOCKED(lle) do {				\
126238990Sglebius	if ((lle)->lle_refcnt == 1)				\
127286577Smelifaro		(lle)->lle_free(lle);				\
128186121Skmacy	else {							\
129238990Sglebius		LLE_REMREF(lle);				\
130186121Skmacy		LLE_WUNLOCK(lle);				\
131186121Skmacy	}							\
132186121Skmacy	/* guard against invalid refs */			\
133238990Sglebius	(lle) = NULL;						\
134186121Skmacy} while (0)
135186121Skmacy
136186121Skmacy#define	LLE_FREE(lle) do {					\
137186121Skmacy	LLE_WLOCK(lle);						\
138216859Sbz	LLE_FREE_LOCKED(lle);					\
139186121Skmacy} while (0)
140186121Skmacy
141286457Smelifarotypedef	struct llentry *(llt_lookup_t)(struct lltable *, u_int flags,
142286457Smelifaro    const struct sockaddr *l3addr);
143286955Smelifarotypedef	struct llentry *(llt_alloc_t)(struct lltable *, u_int flags,
144286457Smelifaro    const struct sockaddr *l3addr);
145287789Smelifarotypedef	void (llt_delete_t)(struct lltable *, struct llentry *);
146286457Smelifarotypedef void (llt_prefix_free_t)(struct lltable *,
147287789Smelifaro    const struct sockaddr *addr, const struct sockaddr *mask, u_int flags);
148286577Smelifarotypedef int (llt_dump_entry_t)(struct lltable *, struct llentry *,
149286577Smelifaro    struct sysctl_req *);
150286577Smelifarotypedef uint32_t (llt_hash_t)(const struct llentry *, uint32_t);
151286577Smelifarotypedef int (llt_match_prefix_t)(const struct sockaddr *,
152286577Smelifaro    const struct sockaddr *, u_int, struct llentry *);
153286577Smelifarotypedef void (llt_free_entry_t)(struct lltable *, struct llentry *);
154286577Smelifarotypedef void (llt_fill_sa_entry_t)(const struct llentry *, struct sockaddr *);
155286616Smelifarotypedef void (llt_free_tbl_t)(struct lltable *);
156286577Smelifarotypedef void (llt_link_entry_t)(struct lltable *, struct llentry *);
157286577Smelifarotypedef void (llt_unlink_entry_t)(struct llentry *);
158286457Smelifaro
159286577Smelifarotypedef int (llt_foreach_cb_t)(struct lltable *, struct llentry *, void *);
160286577Smelifarotypedef int (llt_foreach_entry_t)(struct lltable *, llt_foreach_cb_t *, void *);
161286577Smelifaro
162186121Skmacystruct lltable {
163186121Skmacy	SLIST_ENTRY(lltable)	llt_link;
164186121Skmacy	int			llt_af;
165286616Smelifaro	int			llt_hsize;
166286616Smelifaro	struct llentries	*lle_head;
167186121Skmacy	struct ifnet		*llt_ifp;
168186121Skmacy
169286457Smelifaro	llt_lookup_t		*llt_lookup;
170286955Smelifaro	llt_alloc_t		*llt_alloc_entry;
171287789Smelifaro	llt_delete_t		*llt_delete_entry;
172286457Smelifaro	llt_prefix_free_t	*llt_prefix_free;
173286577Smelifaro	llt_dump_entry_t	*llt_dump_entry;
174286577Smelifaro	llt_hash_t		*llt_hash;
175286577Smelifaro	llt_match_prefix_t	*llt_match_prefix;
176286577Smelifaro	llt_free_entry_t	*llt_free_entry;
177286577Smelifaro	llt_foreach_entry_t	*llt_foreach_entry;
178286577Smelifaro	llt_link_entry_t	*llt_link_entry;
179286577Smelifaro	llt_unlink_entry_t	*llt_unlink_entry;
180286577Smelifaro	llt_fill_sa_entry_t	*llt_fill_sa_entry;
181286616Smelifaro	llt_free_tbl_t		*llt_free_tbl;
182186121Skmacy};
183286457Smelifaro
184186121SkmacyMALLOC_DECLARE(M_LLTABLE);
185186121Skmacy
186186121Skmacy/*
187286457Smelifaro * LLentry flags
188186121Skmacy */
189186121Skmacy#define	LLE_DELETED	0x0001	/* entry must be deleted */
190186121Skmacy#define	LLE_STATIC	0x0002	/* entry is static */
191186121Skmacy#define	LLE_IFADDR	0x0004	/* entry is interface addr */
192186121Skmacy#define	LLE_VALID	0x0008	/* ll_addr is valid */
193287798Svangyzen#define	LLE_REDIRECT	0x0010	/* installed by redirect; has host rtentry */
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  */
198291853Smelifaro#define	LLE_UNLOCKED	0x4000	/* return lle unlocked */
199292978Smelifaro#define	LLE_ADDRONLY	0x4000	/* return lladdr instead of full header */
200292978Smelifaro#define	LLE_CREATE	0x8000	/* hint to avoid lle lookup */
201186121Skmacy
202291853Smelifaro/* LLE flags used by fastpath code */
203291853Smelifaro#define	RLLE_VALID	0x0001		/* entry is valid */
204291853Smelifaro#define	RLLE_IFADDR	LLE_IFADDR	/* entry is ifaddr */
205291853Smelifaro
206186121Skmacy#define LLATBL_HASH(key, mask) \
207186121Skmacy	(((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask)
208186121Skmacy
209286616Smelifarostruct lltable *lltable_allocate_htbl(uint32_t hsize);
210186121Skmacyvoid		lltable_free(struct lltable *);
211286616Smelifarovoid		lltable_link(struct lltable *llt);
212238945Sglebiusvoid		lltable_prefix_free(int, struct sockaddr *,
213238945Sglebius		    struct sockaddr *, u_int);
214213929Sbz#if 0
215186121Skmacyvoid		lltable_drain(int);
216213929Sbz#endif
217186121Skmacyint		lltable_sysctl_dumparp(int, struct sysctl_req *);
218186121Skmacy
219215207Sgnnsize_t		llentry_free(struct llentry *);
220238989Sglebiusstruct llentry  *llentry_alloc(struct ifnet *, struct lltable *,
221238989Sglebius		    struct sockaddr_storage *);
222186121Skmacy
223286577Smelifaro/* helper functions */
224286577Smelifarosize_t lltable_drop_entry_queue(struct llentry *);
225290486Smelifarovoid lltable_set_entry_addr(struct ifnet *ifp, struct llentry *lle,
226292978Smelifaro    const char *linkhdr, size_t linkhdrsize, int lladdr_off);
227292155Smelifaroint lltable_try_set_entry_addr(struct ifnet *ifp, struct llentry *lle,
228292978Smelifaro    const char *linkhdr, size_t linkhdrsize, int lladdr_off);
229286577Smelifaro
230292978Smelifaroint lltable_calc_llheader(struct ifnet *ifp, int family, char *lladdr,
231292978Smelifaro    char *buf, size_t *bufsize, int *lladdr_off);
232292978Smelifarovoid lltable_update_ifaddr(struct lltable *llt);
233286955Smelifarostruct llentry *lltable_alloc_entry(struct lltable *llt, u_int flags,
234286955Smelifaro    const struct sockaddr *l4addr);
235286955Smelifarovoid lltable_free_entry(struct lltable *llt, struct llentry *lle);
236287789Smelifaroint lltable_delete_addr(struct lltable *llt, u_int flags,
237287789Smelifaro    const struct sockaddr *l3addr);
238286577Smelifarovoid lltable_link_entry(struct lltable *llt, struct llentry *lle);
239286577Smelifarovoid lltable_unlink_entry(struct lltable *llt, struct llentry *lle);
240286577Smelifarovoid lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa);
241286577Smelifarostruct ifnet *lltable_get_ifp(const struct lltable *llt);
242286577Smelifaroint lltable_get_af(const struct lltable *llt);
243286577Smelifaro
244286577Smelifaroint lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f,
245286577Smelifaro    void *farg);
246186121Skmacy/*
247186121Skmacy * Generic link layer address lookup function.
248186121Skmacy */
249186121Skmacystatic __inline struct llentry *
250186121Skmacylla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
251186121Skmacy{
252286457Smelifaro
253286457Smelifaro	return (llt->llt_lookup(llt, flags, l3addr));
254186121Skmacy}
255186121Skmacy
256186121Skmacyint		lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *);
257245924Snp
258245924Snp#include <sys/eventhandler.h>
259245924Snpenum {
260245924Snp	LLENTRY_RESOLVED,
261245924Snp	LLENTRY_TIMEDOUT,
262245924Snp	LLENTRY_DELETED,
263245924Snp	LLENTRY_EXPIRED,
264245924Snp};
265245924Snptypedef void (*lle_event_fn)(void *, struct llentry *, int);
266245924SnpEVENTHANDLER_DECLARE(lle_event, lle_event_fn);
267186121Skmacy#endif  /* _NET_IF_LLATBL_H_ */
268