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