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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #ifndef _NET_IF_LLATBL_H_
31 #define _NET_IF_LLATBL_H_
33 #include <sys/_rwlock.h>
34 #include <netinet/in.h>
42 LIST_HEAD(llentries, llentry);
45 * Code referencing llentry must at least hold
49 LIST_ENTRY(llentry) lle_next;
50 struct rwlock lle_lock;
51 struct lltable *lle_tbl;
52 struct llentries *lle_head;
59 int16_t ln_state; /* IPv6 has ND6_LLINFO_NOSTATE == -2 */
71 struct callout ln_timer_ch;
72 struct callout la_timer;
74 /* NB: struct sockaddr must immediately follow */
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_LOCK_DESTROY(lle) rw_destroy(&(lle)->lle_lock)
85 #define LLE_WLOCK_ASSERT(lle) rw_assert(&(lle)->lle_lock, RA_WLOCKED)
87 #define LLE_IS_VALID(lle) (((lle) != NULL) && ((lle) != (void *)-1))
89 #define LLE_ADDREF(lle) do { \
90 LLE_WLOCK_ASSERT(lle); \
91 KASSERT((lle)->lle_refcnt >= 0, \
92 ("negative refcnt %d", (lle)->lle_refcnt)); \
93 (lle)->lle_refcnt++; \
96 #define LLE_REMREF(lle) do { \
97 LLE_WLOCK_ASSERT(lle); \
98 KASSERT((lle)->lle_refcnt > 1, \
99 ("bogus refcnt %d", (lle)->lle_refcnt)); \
100 (lle)->lle_refcnt--; \
103 #define LLE_FREE_LOCKED(lle) do { \
104 if ((lle)->lle_refcnt <= 1) \
105 (lle)->lle_tbl->llt_free((lle)->lle_tbl, (lle));\
107 (lle)->lle_refcnt--; \
110 /* guard against invalid refs */ \
114 #define LLE_FREE(lle) do { \
116 if ((lle)->lle_refcnt <= 1) \
117 (lle)->lle_tbl->llt_free((lle)->lle_tbl, (lle));\
119 (lle)->lle_refcnt--; \
122 /* guard against invalid refs */ \
127 #define ln_timer_ch lle_timer.ln_timer_ch
128 #define la_timer lle_timer.la_timer
131 #define L3_ADDR(lle) ((struct sockaddr *)(&lle[1]))
132 #define L3_ADDR_LEN(lle) (((struct sockaddr *)(&lle[1]))->sa_len)
134 #ifndef LLTBL_HASHTBL_SIZE
135 #define LLTBL_HASHTBL_SIZE 32 /* default 32 ? */
138 #ifndef LLTBL_HASHMASK
139 #define LLTBL_HASHMASK (LLTBL_HASHTBL_SIZE - 1)
143 SLIST_ENTRY(lltable) llt_link;
144 struct llentries lle_head[LLTBL_HASHTBL_SIZE];
146 struct ifnet *llt_ifp;
148 struct llentry * (*llt_new)(const struct sockaddr *, u_int);
149 void (*llt_free)(struct lltable *, struct llentry *);
150 void (*llt_prefix_free)(struct lltable *,
151 const struct sockaddr *prefix,
152 const struct sockaddr *mask);
153 struct llentry * (*llt_lookup)(struct lltable *, u_int flags,
154 const struct sockaddr *l3addr);
155 int (*llt_rtcheck)(struct ifnet *,
156 const struct sockaddr *);
157 int (*llt_dump)(struct lltable *,
158 struct sysctl_req *);
160 MALLOC_DECLARE(M_LLTABLE);
163 * flags to be passed to arplookup.
165 #define LLE_DELETED 0x0001 /* entry must be deleted */
166 #define LLE_STATIC 0x0002 /* entry is static */
167 #define LLE_IFADDR 0x0004 /* entry is interface addr */
168 #define LLE_VALID 0x0008 /* ll_addr is valid */
169 #define LLE_PROXY 0x0010 /* proxy entry ??? */
170 #define LLE_PUB 0x0020 /* publish entry ??? */
171 #define LLE_DELETE 0x4000 /* delete on a lookup - match LLE_IFADDR */
172 #define LLE_CREATE 0x8000 /* create on a lookup miss */
173 #define LLE_EXCLUSIVE 0x2000 /* return lle xlocked */
175 #define LLATBL_HASH(key, mask) \
176 (((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask)
178 struct lltable *lltable_init(struct ifnet *, int);
179 void lltable_free(struct lltable *);
180 void lltable_prefix_free(int, struct sockaddr *,
182 void lltable_drain(int);
183 int lltable_sysctl_dumparp(int, struct sysctl_req *);
185 void llentry_free(struct llentry *);
186 int llentry_update(struct llentry **, struct lltable *,
187 struct sockaddr *, struct ifnet *);
190 * Generic link layer address lookup function.
192 static __inline struct llentry *
193 lla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
195 return llt->llt_lookup(llt, flags, l3addr);
198 int lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *);
199 #endif /* _NET_IF_LLATBL_H_ */