| 1 | /* |
| 2 | * Copyright (c) 2016 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
| 5 | * |
| 6 | * This file contains Original Code and/or Modifications of Original Code |
| 7 | * as defined in and that are subject to the Apple Public Source License |
| 8 | * Version 2.0 (the 'License'). You may not use this file except in |
| 9 | * compliance with the License. The rights granted to you under the License |
| 10 | * may not be used to create, or enable the creation or redistribution of, |
| 11 | * unlawful or unlicensed copies of an Apple operating system, or to |
| 12 | * circumvent, violate, or enable the circumvention or violation of, any |
| 13 | * terms of an Apple operating system software license agreement. |
| 14 | * |
| 15 | * Please obtain a copy of the License at |
| 16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. |
| 17 | * |
| 18 | * The Original Code and all software distributed under the License are |
| 19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
| 20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
| 21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
| 23 | * Please see the License for the specific language governing rights and |
| 24 | * limitations under the License. |
| 25 | * |
| 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
| 27 | */ |
| 28 | /* |
| 29 | * Copyright (c) 2004 Luigi Rizzo, Alessandro Cerri. All rights reserved. |
| 30 | * Copyright (c) 2004-2008 Qing Li. All rights reserved. |
| 31 | * Copyright (c) 2008 Kip Macy. All rights reserved. |
| 32 | * |
| 33 | * Redistribution and use in source and binary forms, with or without |
| 34 | * modification, are permitted provided that the following conditions |
| 35 | * are met: |
| 36 | * 1. Redistributions of source code must retain the above copyright |
| 37 | * notice, this list of conditions and the following disclaimer. |
| 38 | * 2. Redistributions in binary form must reproduce the above copyright |
| 39 | * notice, this list of conditions and the following disclaimer in the |
| 40 | * documentation and/or other materials provided with the distribution. |
| 41 | * |
| 42 | * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 43 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 44 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 45 | * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
| 46 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 47 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 48 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 49 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 50 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 51 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 52 | * SUCH DAMAGE. |
| 53 | */ |
| 54 | #include <sys/cdefs.h> |
| 55 | |
| 56 | #ifndef _NET_IF_LLATBL_H_ |
| 57 | #define _NET_IF_LLATBL_H_ |
| 58 | |
| 59 | #include <netinet/in.h> |
| 60 | #include <net/if_dl.h> |
| 61 | #include <kern/locks.h> |
| 62 | #include <kern/thread_call.h> |
| 63 | #include <sys/eventhandler.h> |
| 64 | |
| 65 | struct ifnet; |
| 66 | struct sysctl_req; |
| 67 | struct rt_msghdr; |
| 68 | struct rt_addrinfo; |
| 69 | |
| 70 | struct llentry; |
| 71 | LIST_HEAD(llentries, llentry); |
| 72 | |
| 73 | extern lck_rw_t *lltable_rwlock; |
| 74 | #define LLTABLE_RLOCK() lck_rw_lock_shared(lltable_rwlock) |
| 75 | #define LLTABLE_RUNLOCK() lck_rw_done(lltable_rwlock) |
| 76 | #define LLTABLE_WLOCK() lck_rw_lock_exclusive(lltable_rwlock) |
| 77 | #define LLTABLE_WUNLOCK() lck_rw_done(lltable_rwlock) |
| 78 | #define LLTABLE_LOCK_ASSERT() LCK_RW_ASSERT(lltable_rwlock, LCK_RW_ASSERT_EXCLUSIVE) |
| 79 | |
| 80 | #define LLE_MAX_LINKHDR 24 /* Full IB header */ |
| 81 | /* |
| 82 | * Code referencing llentry must at least hold |
| 83 | * a shared lock |
| 84 | */ |
| 85 | struct llentry { |
| 86 | LIST_ENTRY(llentry) lle_next; |
| 87 | union { |
| 88 | struct in_addr addr4; |
| 89 | struct in6_addr addr6; |
| 90 | } r_l3addr; |
| 91 | #if 0 |
| 92 | char r_linkdata[LLE_MAX_LINKHDR]; /* L2 data */ |
| 93 | uint8_t r_hdrlen; /* length for LL header */ |
| 94 | uint8_t spare0[3]; |
| 95 | #endif |
| 96 | uint16_t r_flags; /* LLE runtime flags */ |
| 97 | uint16_t r_skip_req; /* feedback from fast path */ |
| 98 | |
| 99 | struct lltable *lle_tbl; |
| 100 | struct llentries *lle_head; |
| 101 | void (*lle_free)(struct llentry *); |
| 102 | struct mbuf *la_hold; |
| 103 | int la_numheld; /* # of packets currently held */ |
| 104 | u_int64_t la_expire; |
| 105 | uint16_t la_flags; |
| 106 | uint16_t la_asked; |
| 107 | uint16_t la_preempt; |
| 108 | int16_t ln_state; /* IPv6 has ND6_LLINFO_NOSTATE == -2 */ |
| 109 | uint16_t ln_router; |
| 110 | time_t ln_ntick; |
| 111 | time_t lle_remtime; /* Real time remaining */ |
| 112 | time_t lle_hittime; /* Time when r_skip_req was unset */ |
| 113 | int lle_refcnt; |
| 114 | union { |
| 115 | uint64_t mac_aligned; |
| 116 | uint16_t mac16[3]; |
| 117 | } ll_addr; |
| 118 | LIST_ENTRY(llentry) lle_chain; /* chain of deleted items */ |
| 119 | thread_call_t lle_timer; |
| 120 | u_int64_t ln_lastused; /* last used timestamp */ |
| 121 | struct if_llreach *ln_llreach; /* link-layer reachability record */ |
| 122 | decl_lck_rw_data(, lle_lock); |
| 123 | decl_lck_mtx_data(, req_mtx); |
| 124 | }; |
| 125 | |
| 126 | extern lck_grp_t *lle_lock_grp; |
| 127 | extern lck_attr_t *lle_lock_attr; |
| 128 | |
| 129 | #define LLE_WLOCK(lle) lck_rw_lock_exclusive(&(lle)->lle_lock) |
| 130 | #define LLE_RLOCK(lle) lck_rw_lock_shared(&(lle)->lle_lock) |
| 131 | #define LLE_WUNLOCK(lle) lck_rw_done(&(lle)->lle_lock) |
| 132 | #define LLE_RUNLOCK(lle) lck_rw_done(&(lle)->lle_lock) |
| 133 | #define LLE_DOWNGRADE(lle) lck_rw_lock_exclusive_to_shared(&(lle)->lle_lock) |
| 134 | #define LLE_TRY_UPGRADE(lle) lck_rw_lock_shared_to_exclusive(&(lle)->lle_lock) |
| 135 | #define LLE_LOCK_INIT(lle) lck_rw_init(&(lle)->lle_lock, lle_lock_grp, lle_lock_attr) |
| 136 | #define LLE_LOCK_DESTROY(lle) lck_rw_destroy(&(lle)->lle_lock, lle_lock_grp) |
| 137 | #define LLE_WLOCK_ASSERT(lle) LCK_RW_ASSERT(&(lle)->lle_lock, LCK_RW_ASSERT_EXCLUSIVE) |
| 138 | |
| 139 | #define LLE_REQ_INIT(lle) lck_mtx_init(&(lle)->req_mtx, lle_lock_grp, lle_lock_attr) |
| 140 | #define LLE_REQ_DESTROY(lle) lck_mtx_destroy(&(lle)->req_mtx, lle_lock_grp) |
| 141 | #define LLE_REQ_LOCK(lle) lck_mtx_lock(&(lle)->req_mtx) |
| 142 | #define LLE_REQ_UNLOCK(lle) lck_mtx_unlock(&(lle)->req_mtx) |
| 143 | |
| 144 | #define LLE_IS_VALID(lle) (((lle) != NULL) && ((lle) != (void *)-1)) |
| 145 | |
| 146 | #define LLE_ADDREF(lle) do { \ |
| 147 | LLE_WLOCK_ASSERT(lle); \ |
| 148 | VERIFY((lle)->lle_refcnt >= 0); \ |
| 149 | (lle)->lle_refcnt++; \ |
| 150 | } while (0) |
| 151 | |
| 152 | #define LLE_REMREF(lle) do { \ |
| 153 | LLE_WLOCK_ASSERT(lle); \ |
| 154 | VERIFY((lle)->lle_refcnt > 0); \ |
| 155 | (lle)->lle_refcnt--; \ |
| 156 | } while (0) |
| 157 | |
| 158 | #define LLE_FREE_LOCKED(lle) do { \ |
| 159 | if ((lle)->lle_refcnt == 1) \ |
| 160 | (lle)->lle_free(lle); \ |
| 161 | else { \ |
| 162 | LLE_REMREF(lle); \ |
| 163 | LLE_WUNLOCK(lle); \ |
| 164 | } \ |
| 165 | /* guard against invalid refs */ \ |
| 166 | (lle) = NULL; \ |
| 167 | } while (0) |
| 168 | |
| 169 | #define LLE_FREE(lle) do { \ |
| 170 | LLE_WLOCK(lle); \ |
| 171 | LLE_FREE_LOCKED(lle); \ |
| 172 | } while (0) |
| 173 | |
| 174 | typedef struct llentry *(llt_lookup_t)(struct lltable *, u_int flags, |
| 175 | const struct sockaddr *l3addr); |
| 176 | typedef struct llentry *(llt_alloc_t)(struct lltable *, u_int flags, |
| 177 | const struct sockaddr *l3addr); |
| 178 | typedef void (llt_delete_t)(struct lltable *, struct llentry *); |
| 179 | typedef void (llt_prefix_free_t)(struct lltable *, |
| 180 | const struct sockaddr *addr, const struct sockaddr *mask, u_int flags); |
| 181 | typedef int (llt_dump_entry_t)(struct lltable *, struct llentry *, |
| 182 | struct sysctl_req *); |
| 183 | typedef uint32_t (llt_hash_t)(const struct llentry *, uint32_t); |
| 184 | typedef int (llt_match_prefix_t)(const struct sockaddr *, |
| 185 | const struct sockaddr *, u_int, struct llentry *); |
| 186 | typedef void (llt_free_entry_t)(struct lltable *, struct llentry *); |
| 187 | typedef void (llt_fill_sa_entry_t)(const struct llentry *, struct sockaddr *); |
| 188 | typedef void (llt_free_tbl_t)(struct lltable *); |
| 189 | typedef void (llt_link_entry_t)(struct lltable *, struct llentry *); |
| 190 | typedef void (llt_unlink_entry_t)(struct llentry *); |
| 191 | |
| 192 | typedef int (llt_foreach_cb_t)(struct lltable *, struct llentry *, void *); |
| 193 | typedef int (llt_foreach_entry_t)(struct lltable *, llt_foreach_cb_t *, void *); |
| 194 | |
| 195 | struct lltable { |
| 196 | SLIST_ENTRY(lltable) llt_link; |
| 197 | int llt_af; |
| 198 | int llt_hsize; |
| 199 | struct llentries *lle_head; |
| 200 | struct ifnet *llt_ifp; |
| 201 | |
| 202 | llt_lookup_t *llt_lookup; |
| 203 | llt_alloc_t *llt_alloc_entry; |
| 204 | llt_delete_t *llt_delete_entry; |
| 205 | llt_prefix_free_t *llt_prefix_free; |
| 206 | llt_dump_entry_t *llt_dump_entry; |
| 207 | llt_hash_t *llt_hash; |
| 208 | llt_match_prefix_t *llt_match_prefix; |
| 209 | llt_free_entry_t *llt_free_entry; |
| 210 | llt_foreach_entry_t *llt_foreach_entry; |
| 211 | llt_link_entry_t *llt_link_entry; |
| 212 | llt_unlink_entry_t *llt_unlink_entry; |
| 213 | llt_fill_sa_entry_t *llt_fill_sa_entry; |
| 214 | llt_free_tbl_t *llt_free_tbl; |
| 215 | }; |
| 216 | |
| 217 | #ifdef MALLOC_DECLARE |
| 218 | MALLOC_DECLARE(M_LLTABLE); |
| 219 | #endif |
| 220 | |
| 221 | /* |
| 222 | * LLentry flags |
| 223 | */ |
| 224 | #define LLE_DELETED 0x0001 /* entry must be deleted */ |
| 225 | #define LLE_STATIC 0x0002 /* entry is static */ |
| 226 | #define LLE_IFADDR 0x0004 /* entry is interface addr */ |
| 227 | #define LLE_VALID 0x0008 /* ll_addr is valid */ |
| 228 | #define LLE_REDIRECT 0x0010 /* installed by redirect; has host rtentry */ |
| 229 | #define LLE_PUB 0x0020 /* publish entry ??? */ |
| 230 | #define LLE_LINKED 0x0040 /* linked to lookup structure */ |
| 231 | /* LLE request flags */ |
| 232 | #define LLE_EXCLUSIVE 0x2000 /* return lle xlocked */ |
| 233 | #define LLE_UNLOCKED 0x4000 /* return lle unlocked */ |
| 234 | #define LLE_ADDRONLY 0x4000 /* return lladdr instead of full header */ |
| 235 | #define LLE_CREATE 0x8000 /* hint to avoid lle lookup */ |
| 236 | |
| 237 | /* LLE flags used by fastpath code */ |
| 238 | #define RLLE_VALID 0x0001 /* entry is valid */ |
| 239 | #define RLLE_IFADDR LLE_IFADDR /* entry is ifaddr */ |
| 240 | |
| 241 | #define LLATBL_HASH(key, mask) \ |
| 242 | (((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask) |
| 243 | |
| 244 | void lltable_glbl_init(void); |
| 245 | struct lltable *lltable_allocate_htbl(uint32_t hsize); |
| 246 | void lltable_free(struct lltable *); |
| 247 | void lltable_link(struct lltable *llt); |
| 248 | void lltable_prefix_free(int, struct sockaddr *, |
| 249 | struct sockaddr *, u_int); |
| 250 | #if 0 |
| 251 | void lltable_drain(int); |
| 252 | #endif |
| 253 | int lltable_sysctl_dumparp(int, struct sysctl_req *); |
| 254 | |
| 255 | size_t llentry_free(struct llentry *); |
| 256 | struct llentry *llentry_alloc(struct ifnet *, struct lltable *, |
| 257 | struct sockaddr_storage *); |
| 258 | |
| 259 | /* helper functions */ |
| 260 | size_t lltable_drop_entry_queue(struct llentry *); |
| 261 | void lltable_set_entry_addr(struct ifnet *ifp, struct llentry *lle, |
| 262 | const char *ll_addr); |
| 263 | int lltable_try_set_entry_addr(struct ifnet *ifp, struct llentry *lle, |
| 264 | const char *linkhdr, size_t linkhdrsize, int lladdr_off); |
| 265 | |
| 266 | int (struct ifnet *ifp, int family, char *lladdr, |
| 267 | char *buf, size_t *bufsize, int *lladdr_off); |
| 268 | void lltable_update_ifaddr(struct lltable *llt); |
| 269 | struct llentry *lltable_alloc_entry(struct lltable *llt, u_int flags, |
| 270 | const struct sockaddr *l4addr); |
| 271 | void lltable_free_entry(struct lltable *llt, struct llentry *lle); |
| 272 | int lltable_delete_addr(struct lltable *llt, u_int flags, |
| 273 | const struct sockaddr *l3addr); |
| 274 | void lltable_link_entry(struct lltable *llt, struct llentry *lle); |
| 275 | void lltable_unlink_entry(struct lltable *llt, struct llentry *lle); |
| 276 | void lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa); |
| 277 | struct ifnet *lltable_get_ifp(const struct lltable *llt); |
| 278 | int lltable_get_af(const struct lltable *llt); |
| 279 | |
| 280 | int lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f, |
| 281 | void *farg); |
| 282 | /* |
| 283 | * Generic link layer address lookup function. |
| 284 | */ |
| 285 | static __inline struct llentry * |
| 286 | lla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) |
| 287 | { |
| 288 | return (llt->llt_lookup(llt, flags, l3addr)); |
| 289 | } |
| 290 | |
| 291 | int lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *); |
| 292 | |
| 293 | enum { |
| 294 | LLENTRY_RESOLVED, |
| 295 | LLENTRY_TIMEDOUT, |
| 296 | LLENTRY_DELETED, |
| 297 | LLENTRY_EXPIRED, |
| 298 | }; |
| 299 | |
| 300 | typedef void (*lle_event_fn)(struct eventhandler_entry_arg, struct llentry *, int); |
| 301 | EVENTHANDLER_DECLARE(lle_event, lle_event_fn); |
| 302 | #endif /* _NET_IF_LLATBL_H_ */ |
| 303 | |