1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2004 Luigi Rizzo, Alessandro Cerri. All rights reserved.
5 * Copyright (c) 2004-2008 Qing Li. All rights reserved.
6 * Copyright (c) 2008 Kip Macy. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#ifndef	_NET_IF_LLATBL_H_
33#define	_NET_IF_LLATBL_H_
34
35#include <sys/_eventhandler.h>
36#include <sys/_rwlock.h>
37#include <netinet/in.h>
38#include <sys/epoch.h>
39#include <sys/ck.h>
40
41struct ifnet;
42struct sysctl_req;
43struct rt_msghdr;
44struct rt_addrinfo;
45struct llentry;
46CK_LIST_HEAD(llentries, llentry);
47
48#define	LLE_MAX_LINKHDR		24	/* Full IB header */
49/*
50 * Code referencing llentry must at least hold
51 * a shared lock
52 */
53struct llentry {
54	CK_LIST_ENTRY(llentry)	 lle_next;
55	union {
56		struct in_addr	addr4;
57		struct in6_addr	addr6;
58	} r_l3addr;
59	char			r_linkdata[LLE_MAX_LINKHDR]; /* L2 data */
60	uint8_t			r_hdrlen;	/* length for LL header */
61	uint8_t			spare0[3];
62	uint16_t		r_flags;	/* LLE runtime flags */
63	uint16_t		r_skip_req;	/* feedback from fast path */
64
65	struct lltable		 *lle_tbl;
66	struct llentries	 *lle_head;
67	void			(*lle_free)(struct llentry *);
68	struct mbuf		 *la_hold;
69	int			 la_numheld;  /* # of packets currently held */
70	time_t			 la_expire;
71	uint16_t		 la_flags;
72	uint16_t		 la_asked;
73	uint16_t		 la_preempt;
74	int16_t			 ln_state;	/* IPv6 has ND6_LLINFO_NOSTATE == -2 */
75	uint16_t		 ln_router;
76	time_t			 ln_ntick;
77	time_t			lle_remtime;	/* Real time remaining */
78	time_t			lle_hittime;	/* Time when r_skip_req was unset */
79	int			 lle_refcnt;
80	char			*ll_addr;	/* link-layer address */
81
82	CK_LIST_ENTRY(llentry)	lle_chain;	/* chain of deleted items */
83	struct callout		lle_timer;
84	struct rwlock		 lle_lock;
85	struct mtx		req_mtx;
86	struct epoch_context lle_epoch_ctx;
87};
88
89#define	LLE_WLOCK(lle)		rw_wlock(&(lle)->lle_lock)
90#define	LLE_RLOCK(lle)		rw_rlock(&(lle)->lle_lock)
91#define	LLE_WUNLOCK(lle)	rw_wunlock(&(lle)->lle_lock)
92#define	LLE_RUNLOCK(lle)	rw_runlock(&(lle)->lle_lock)
93#define	LLE_DOWNGRADE(lle)	rw_downgrade(&(lle)->lle_lock)
94#define	LLE_TRY_UPGRADE(lle)	rw_try_upgrade(&(lle)->lle_lock)
95#define	LLE_LOCK_INIT(lle)	rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK)
96#define	LLE_LOCK_DESTROY(lle)	rw_destroy(&(lle)->lle_lock)
97#define	LLE_WLOCK_ASSERT(lle)	rw_assert(&(lle)->lle_lock, RA_WLOCKED)
98
99#define	LLE_REQ_INIT(lle)	mtx_init(&(lle)->req_mtx, "lle req", \
100	NULL, MTX_DEF)
101#define	LLE_REQ_DESTROY(lle)	mtx_destroy(&(lle)->req_mtx)
102#define	LLE_REQ_LOCK(lle)	mtx_lock(&(lle)->req_mtx)
103#define	LLE_REQ_UNLOCK(lle)	mtx_unlock(&(lle)->req_mtx)
104
105#define LLE_IS_VALID(lle)	(((lle) != NULL) && ((lle) != (void *)-1))
106
107#define	LLE_ADDREF(lle) do {					\
108	LLE_WLOCK_ASSERT(lle);					\
109	KASSERT((lle)->lle_refcnt >= 0,				\
110	    ("negative refcnt %d on lle %p",			\
111	    (lle)->lle_refcnt, (lle)));				\
112	(lle)->lle_refcnt++;					\
113} while (0)
114
115#define	LLE_REMREF(lle)	do {					\
116	LLE_WLOCK_ASSERT(lle);					\
117	KASSERT((lle)->lle_refcnt > 0,				\
118	    ("bogus refcnt %d on lle %p",			\
119	    (lle)->lle_refcnt, (lle)));				\
120	(lle)->lle_refcnt--;					\
121} while (0)
122
123#define	LLE_FREE_LOCKED(lle) do {				\
124	if ((lle)->lle_refcnt == 1)				\
125		(lle)->lle_free(lle);				\
126	else {							\
127		LLE_REMREF(lle);				\
128		LLE_WUNLOCK(lle);				\
129	}							\
130	/* guard against invalid refs */			\
131	(lle) = NULL;						\
132} while (0)
133
134#define	LLE_FREE(lle) do {					\
135	LLE_WLOCK(lle);						\
136	LLE_FREE_LOCKED(lle);					\
137} while (0)
138
139typedef	struct llentry *(llt_lookup_t)(struct lltable *, u_int flags,
140    const struct sockaddr *l3addr);
141typedef	struct llentry *(llt_alloc_t)(struct lltable *, u_int flags,
142    const struct sockaddr *l3addr);
143typedef	void (llt_delete_t)(struct lltable *, struct llentry *);
144typedef void (llt_prefix_free_t)(struct lltable *,
145    const struct sockaddr *addr, const struct sockaddr *mask, u_int flags);
146typedef int (llt_dump_entry_t)(struct lltable *, struct llentry *,
147    struct sysctl_req *);
148typedef uint32_t (llt_hash_t)(const struct llentry *, uint32_t);
149typedef int (llt_match_prefix_t)(const struct sockaddr *,
150    const struct sockaddr *, u_int, struct llentry *);
151typedef void (llt_free_entry_t)(struct lltable *, struct llentry *);
152typedef void (llt_fill_sa_entry_t)(const struct llentry *, struct sockaddr *);
153typedef void (llt_free_tbl_t)(struct lltable *);
154typedef int (llt_link_entry_t)(struct lltable *, struct llentry *);
155typedef int (llt_unlink_entry_t)(struct llentry *);
156typedef void (llt_mark_used_t)(struct llentry *);
157
158typedef int (llt_foreach_cb_t)(struct lltable *, struct llentry *, void *);
159typedef int (llt_foreach_entry_t)(struct lltable *, llt_foreach_cb_t *, void *);
160
161struct lltable {
162	SLIST_ENTRY(lltable)	llt_link;
163	int			llt_af;
164	int			llt_hsize;
165	int			llt_entries;
166	int			llt_maxentries;
167	struct llentries	*lle_head;
168	struct ifnet		*llt_ifp;
169
170	llt_lookup_t		*llt_lookup;
171	llt_alloc_t		*llt_alloc_entry;
172	llt_delete_t		*llt_delete_entry;
173	llt_prefix_free_t	*llt_prefix_free;
174	llt_dump_entry_t	*llt_dump_entry;
175	llt_hash_t		*llt_hash;
176	llt_match_prefix_t	*llt_match_prefix;
177	llt_free_entry_t	*llt_free_entry;
178	llt_foreach_entry_t	*llt_foreach_entry;
179	llt_link_entry_t	*llt_link_entry;
180	llt_unlink_entry_t	*llt_unlink_entry;
181	llt_fill_sa_entry_t	*llt_fill_sa_entry;
182	llt_free_tbl_t		*llt_free_tbl;
183	llt_mark_used_t		*llt_mark_used;
184};
185
186MALLOC_DECLARE(M_LLTABLE);
187
188/*
189 * LLentry flags
190 */
191#define	LLE_DELETED	0x0001	/* entry must be deleted */
192#define	LLE_STATIC	0x0002	/* entry is static */
193#define	LLE_IFADDR	0x0004	/* entry is interface addr */
194#define	LLE_VALID	0x0008	/* ll_addr is valid */
195#define	LLE_REDIRECT	0x0010	/* installed by redirect; has host rtentry */
196#define	LLE_PUB		0x0020	/* publish entry ??? */
197#define	LLE_LINKED	0x0040	/* linked to lookup structure */
198/* LLE request flags */
199#define	LLE_EXCLUSIVE	0x2000	/* return lle xlocked  */
200#define	LLE_UNLOCKED	0x4000	/* return lle unlocked */
201#define	LLE_ADDRONLY	0x4000	/* return lladdr instead of full header */
202#define	LLE_CREATE	0x8000	/* hint to avoid lle lookup */
203
204/* LLE flags used by fastpath code */
205#define	RLLE_VALID	0x0001		/* entry is valid */
206#define	RLLE_IFADDR	LLE_IFADDR	/* entry is ifaddr */
207
208#define LLATBL_HASH(key, mask) \
209	(((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask)
210
211struct lltable *lltable_allocate_htbl(uint32_t hsize);
212void		lltable_free(struct lltable *);
213void		lltable_link(struct lltable *llt);
214void		lltable_prefix_free(int, struct sockaddr *,
215		    struct sockaddr *, u_int);
216int		lltable_sysctl_dumparp(int, struct sysctl_req *);
217
218size_t		llentry_free(struct llentry *);
219
220/* helper functions */
221size_t lltable_drop_entry_queue(struct llentry *);
222void lltable_set_entry_addr(struct ifnet *ifp, struct llentry *lle,
223    const char *linkhdr, size_t linkhdrsize, int lladdr_off);
224int lltable_try_set_entry_addr(struct ifnet *ifp, struct llentry *lle,
225    const char *linkhdr, size_t linkhdrsize, int lladdr_off);
226
227int lltable_calc_llheader(struct ifnet *ifp, int family, char *lladdr,
228    char *buf, size_t *bufsize, int *lladdr_off);
229void lltable_update_ifaddr(struct lltable *llt);
230struct llentry *lltable_alloc_entry(struct lltable *llt, u_int flags,
231    const struct sockaddr *l4addr);
232void lltable_free_entry(struct lltable *llt, struct llentry *lle);
233int lltable_delete_addr(struct lltable *llt, u_int flags,
234    const struct sockaddr *l3addr);
235int lltable_link_entry(struct lltable *llt, struct llentry *lle);
236int lltable_unlink_entry(struct lltable *llt, struct llentry *lle);
237void lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa);
238struct ifnet *lltable_get_ifp(const struct lltable *llt);
239int lltable_get_af(const struct lltable *llt);
240
241int lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f,
242    void *farg);
243/*
244 * Generic link layer address lookup function.
245 */
246static __inline struct llentry *
247lla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
248{
249
250	return (llt->llt_lookup(llt, flags, l3addr));
251}
252
253/*
254 * Notify the LLE code that the entry was used by datapath.
255 */
256static __inline void
257llentry_mark_used(struct llentry *lle)
258{
259
260	if (lle->r_skip_req == 0)
261		return;
262	if ((lle->r_flags & RLLE_VALID) != 0)
263		lle->lle_tbl->llt_mark_used(lle);
264}
265
266int		lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *);
267
268enum {
269	LLENTRY_RESOLVED,
270	LLENTRY_TIMEDOUT,
271	LLENTRY_DELETED,
272	LLENTRY_EXPIRED,
273};
274typedef void (*lle_event_fn)(void *, struct llentry *, int);
275EVENTHANDLER_DECLARE(lle_event, lle_event_fn);
276#endif  /* _NET_IF_LLATBL_H_ */
277