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$"); |
---|
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 | |
---|
36 | struct ifnet; |
---|
37 | struct sysctl_req; |
---|
38 | struct rt_msghdr; |
---|
39 | struct rt_addrinfo; |
---|
40 | struct llentry; |
---|
41 | LIST_HEAD(llentries, llentry); |
---|
42 | |
---|
43 | #define LLE_MAX_LINKHDR 24 /* Full IB header */ |
---|
44 | /* |
---|
45 | * Code referencing llentry must at least hold |
---|
46 | * a shared lock |
---|
47 | */ |
---|
48 | struct llentry { |
---|
49 | LIST_ENTRY(llentry) lle_next; |
---|
50 | union { |
---|
51 | struct in_addr addr4; |
---|
52 | struct in6_addr addr6; |
---|
53 | } r_l3addr; |
---|
54 | char r_linkdata[LLE_MAX_LINKHDR]; /* L2 data */ |
---|
55 | uint8_t r_hdrlen; /* length for LL header */ |
---|
56 | uint8_t spare0[3]; |
---|
57 | uint16_t r_flags; /* LLE runtime flags */ |
---|
58 | uint16_t r_skip_req; /* feedback from fast path */ |
---|
59 | |
---|
60 | struct lltable *lle_tbl; |
---|
61 | struct llentries *lle_head; |
---|
62 | void (*lle_free)(struct llentry *); |
---|
63 | struct mbuf *la_hold; |
---|
64 | int la_numheld; /* # of packets currently held */ |
---|
65 | time_t la_expire; |
---|
66 | uint16_t la_flags; |
---|
67 | uint16_t la_asked; |
---|
68 | uint16_t la_preempt; |
---|
69 | int16_t ln_state; /* IPv6 has ND6_LLINFO_NOSTATE == -2 */ |
---|
70 | uint16_t ln_router; |
---|
71 | time_t ln_ntick; |
---|
72 | time_t lle_remtime; /* Real time remaining */ |
---|
73 | time_t lle_hittime; /* Time when r_skip_req was unset */ |
---|
74 | int lle_refcnt; |
---|
75 | char *ll_addr; /* link-layer address */ |
---|
76 | |
---|
77 | LIST_ENTRY(llentry) lle_chain; /* chain of deleted items */ |
---|
78 | struct callout lle_timer; |
---|
79 | struct rwlock lle_lock; |
---|
80 | struct mtx req_mtx; |
---|
81 | }; |
---|
82 | |
---|
83 | #define LLE_WLOCK(lle) rw_wlock(&(lle)->lle_lock) |
---|
84 | #define LLE_RLOCK(lle) rw_rlock(&(lle)->lle_lock) |
---|
85 | #define LLE_WUNLOCK(lle) rw_wunlock(&(lle)->lle_lock) |
---|
86 | #define LLE_RUNLOCK(lle) rw_runlock(&(lle)->lle_lock) |
---|
87 | #define LLE_DOWNGRADE(lle) rw_downgrade(&(lle)->lle_lock) |
---|
88 | #define LLE_TRY_UPGRADE(lle) rw_try_upgrade(&(lle)->lle_lock) |
---|
89 | #define LLE_LOCK_INIT(lle) rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK) |
---|
90 | #define LLE_LOCK_DESTROY(lle) rw_destroy(&(lle)->lle_lock) |
---|
91 | #define LLE_WLOCK_ASSERT(lle) rw_assert(&(lle)->lle_lock, RA_WLOCKED) |
---|
92 | |
---|
93 | #define LLE_REQ_INIT(lle) mtx_init(&(lle)->req_mtx, "lle req", \ |
---|
94 | NULL, MTX_DEF) |
---|
95 | #define LLE_REQ_DESTROY(lle) mtx_destroy(&(lle)->req_mtx) |
---|
96 | #define LLE_REQ_LOCK(lle) mtx_lock(&(lle)->req_mtx) |
---|
97 | #define LLE_REQ_UNLOCK(lle) mtx_unlock(&(lle)->req_mtx) |
---|
98 | |
---|
99 | #define LLE_IS_VALID(lle) (((lle) != NULL) && ((lle) != (void *)-1)) |
---|
100 | |
---|
101 | #define LLE_ADDREF(lle) do { \ |
---|
102 | LLE_WLOCK_ASSERT(lle); \ |
---|
103 | KASSERT((lle)->lle_refcnt >= 0, \ |
---|
104 | ("negative refcnt %d on lle %p", \ |
---|
105 | (lle)->lle_refcnt, (lle))); \ |
---|
106 | (lle)->lle_refcnt++; \ |
---|
107 | } while (0) |
---|
108 | |
---|
109 | #define LLE_REMREF(lle) do { \ |
---|
110 | LLE_WLOCK_ASSERT(lle); \ |
---|
111 | KASSERT((lle)->lle_refcnt > 0, \ |
---|
112 | ("bogus refcnt %d on lle %p", \ |
---|
113 | (lle)->lle_refcnt, (lle))); \ |
---|
114 | (lle)->lle_refcnt--; \ |
---|
115 | } while (0) |
---|
116 | |
---|
117 | #define LLE_FREE_LOCKED(lle) do { \ |
---|
118 | if ((lle)->lle_refcnt == 1) \ |
---|
119 | (lle)->lle_free(lle); \ |
---|
120 | else { \ |
---|
121 | LLE_REMREF(lle); \ |
---|
122 | LLE_WUNLOCK(lle); \ |
---|
123 | } \ |
---|
124 | /* guard against invalid refs */ \ |
---|
125 | (lle) = NULL; \ |
---|
126 | } while (0) |
---|
127 | |
---|
128 | #define LLE_FREE(lle) do { \ |
---|
129 | LLE_WLOCK(lle); \ |
---|
130 | LLE_FREE_LOCKED(lle); \ |
---|
131 | } while (0) |
---|
132 | |
---|
133 | typedef struct llentry *(llt_lookup_t)(struct lltable *, u_int flags, |
---|
134 | const struct sockaddr *l3addr); |
---|
135 | typedef struct llentry *(llt_alloc_t)(struct lltable *, u_int flags, |
---|
136 | const struct sockaddr *l3addr); |
---|
137 | typedef void (llt_delete_t)(struct lltable *, struct llentry *); |
---|
138 | typedef void (llt_prefix_free_t)(struct lltable *, |
---|
139 | const struct sockaddr *addr, const struct sockaddr *mask, u_int flags); |
---|
140 | typedef int (llt_dump_entry_t)(struct lltable *, struct llentry *, |
---|
141 | struct sysctl_req *); |
---|
142 | typedef uint32_t (llt_hash_t)(const struct llentry *, uint32_t); |
---|
143 | typedef int (llt_match_prefix_t)(const struct sockaddr *, |
---|
144 | const struct sockaddr *, u_int, struct llentry *); |
---|
145 | typedef void (llt_free_entry_t)(struct lltable *, struct llentry *); |
---|
146 | typedef void (llt_fill_sa_entry_t)(const struct llentry *, struct sockaddr *); |
---|
147 | typedef void (llt_free_tbl_t)(struct lltable *); |
---|
148 | typedef void (llt_link_entry_t)(struct lltable *, struct llentry *); |
---|
149 | typedef void (llt_unlink_entry_t)(struct llentry *); |
---|
150 | |
---|
151 | typedef int (llt_foreach_cb_t)(struct lltable *, struct llentry *, void *); |
---|
152 | typedef int (llt_foreach_entry_t)(struct lltable *, llt_foreach_cb_t *, void *); |
---|
153 | |
---|
154 | struct lltable { |
---|
155 | SLIST_ENTRY(lltable) llt_link; |
---|
156 | int llt_af; |
---|
157 | int llt_hsize; |
---|
158 | struct llentries *lle_head; |
---|
159 | struct ifnet *llt_ifp; |
---|
160 | |
---|
161 | llt_lookup_t *llt_lookup; |
---|
162 | llt_alloc_t *llt_alloc_entry; |
---|
163 | llt_delete_t *llt_delete_entry; |
---|
164 | llt_prefix_free_t *llt_prefix_free; |
---|
165 | llt_dump_entry_t *llt_dump_entry; |
---|
166 | llt_hash_t *llt_hash; |
---|
167 | llt_match_prefix_t *llt_match_prefix; |
---|
168 | llt_free_entry_t *llt_free_entry; |
---|
169 | llt_foreach_entry_t *llt_foreach_entry; |
---|
170 | llt_link_entry_t *llt_link_entry; |
---|
171 | llt_unlink_entry_t *llt_unlink_entry; |
---|
172 | llt_fill_sa_entry_t *llt_fill_sa_entry; |
---|
173 | llt_free_tbl_t *llt_free_tbl; |
---|
174 | }; |
---|
175 | |
---|
176 | MALLOC_DECLARE(M_LLTABLE); |
---|
177 | |
---|
178 | /* |
---|
179 | * LLentry flags |
---|
180 | */ |
---|
181 | #define LLE_DELETED 0x0001 /* entry must be deleted */ |
---|
182 | #define LLE_STATIC 0x0002 /* entry is static */ |
---|
183 | #define LLE_IFADDR 0x0004 /* entry is interface addr */ |
---|
184 | #define LLE_VALID 0x0008 /* ll_addr is valid */ |
---|
185 | #define LLE_REDIRECT 0x0010 /* installed by redirect; has host rtentry */ |
---|
186 | #define LLE_PUB 0x0020 /* publish entry ??? */ |
---|
187 | #define LLE_LINKED 0x0040 /* linked to lookup structure */ |
---|
188 | /* LLE request flags */ |
---|
189 | #define LLE_EXCLUSIVE 0x2000 /* return lle xlocked */ |
---|
190 | #define LLE_UNLOCKED 0x4000 /* return lle unlocked */ |
---|
191 | #define LLE_ADDRONLY 0x4000 /* return lladdr instead of full header */ |
---|
192 | #define LLE_CREATE 0x8000 /* hint to avoid lle lookup */ |
---|
193 | |
---|
194 | /* LLE flags used by fastpath code */ |
---|
195 | #define RLLE_VALID 0x0001 /* entry is valid */ |
---|
196 | #define RLLE_IFADDR LLE_IFADDR /* entry is ifaddr */ |
---|
197 | |
---|
198 | #define LLATBL_HASH(key, mask) \ |
---|
199 | (((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask) |
---|
200 | |
---|
201 | struct lltable *lltable_allocate_htbl(uint32_t hsize); |
---|
202 | void lltable_free(struct lltable *); |
---|
203 | void lltable_link(struct lltable *llt); |
---|
204 | void lltable_prefix_free(int, struct sockaddr *, |
---|
205 | struct sockaddr *, u_int); |
---|
206 | #if 0 |
---|
207 | void lltable_drain(int); |
---|
208 | #endif |
---|
209 | int lltable_sysctl_dumparp(int, struct sysctl_req *); |
---|
210 | |
---|
211 | size_t llentry_free(struct llentry *); |
---|
212 | struct llentry *llentry_alloc(struct ifnet *, struct lltable *, |
---|
213 | struct sockaddr_storage *); |
---|
214 | |
---|
215 | /* helper functions */ |
---|
216 | size_t lltable_drop_entry_queue(struct llentry *); |
---|
217 | void lltable_set_entry_addr(struct ifnet *ifp, struct llentry *lle, |
---|
218 | const char *linkhdr, size_t linkhdrsize, int lladdr_off); |
---|
219 | int lltable_try_set_entry_addr(struct ifnet *ifp, struct llentry *lle, |
---|
220 | const char *linkhdr, size_t linkhdrsize, int lladdr_off); |
---|
221 | |
---|
222 | int lltable_calc_llheader(struct ifnet *ifp, int family, char *lladdr, |
---|
223 | char *buf, size_t *bufsize, int *lladdr_off); |
---|
224 | void lltable_update_ifaddr(struct lltable *llt); |
---|
225 | struct llentry *lltable_alloc_entry(struct lltable *llt, u_int flags, |
---|
226 | const struct sockaddr *l4addr); |
---|
227 | void lltable_free_entry(struct lltable *llt, struct llentry *lle); |
---|
228 | int lltable_delete_addr(struct lltable *llt, u_int flags, |
---|
229 | const struct sockaddr *l3addr); |
---|
230 | void lltable_link_entry(struct lltable *llt, struct llentry *lle); |
---|
231 | void lltable_unlink_entry(struct lltable *llt, struct llentry *lle); |
---|
232 | void lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa); |
---|
233 | struct ifnet *lltable_get_ifp(const struct lltable *llt); |
---|
234 | int lltable_get_af(const struct lltable *llt); |
---|
235 | |
---|
236 | int lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f, |
---|
237 | void *farg); |
---|
238 | /* |
---|
239 | * Generic link layer address lookup function. |
---|
240 | */ |
---|
241 | static __inline struct llentry * |
---|
242 | lla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) |
---|
243 | { |
---|
244 | |
---|
245 | return (llt->llt_lookup(llt, flags, l3addr)); |
---|
246 | } |
---|
247 | |
---|
248 | int lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *); |
---|
249 | |
---|
250 | #include <sys/eventhandler.h> |
---|
251 | enum { |
---|
252 | LLENTRY_RESOLVED, |
---|
253 | LLENTRY_TIMEDOUT, |
---|
254 | LLENTRY_DELETED, |
---|
255 | LLENTRY_EXPIRED, |
---|
256 | }; |
---|
257 | typedef void (*lle_event_fn)(void *, struct llentry *, int); |
---|
258 | EVENTHANDLER_DECLARE(lle_event, lle_event_fn); |
---|
259 | #endif /* _NET_IF_LLATBL_H_ */ |
---|