1fe267a55SPedro F. Giffuni /*- 2fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3fe267a55SPedro F. Giffuni * 482f39c91SKip Macy * Copyright (c) 2004 Luigi Rizzo, Alessandro Cerri. All rights reserved. 582f39c91SKip Macy * Copyright (c) 2004-2008 Qing Li. All rights reserved. 682f39c91SKip Macy * Copyright (c) 2008 Kip Macy. All rights reserved. 782f39c91SKip Macy * 882f39c91SKip Macy * Redistribution and use in source and binary forms, with or without 982f39c91SKip Macy * modification, are permitted provided that the following conditions 1082f39c91SKip Macy * are met: 1182f39c91SKip Macy * 1. Redistributions of source code must retain the above copyright 1282f39c91SKip Macy * notice, this list of conditions and the following disclaimer. 1382f39c91SKip Macy * 2. Redistributions in binary form must reproduce the above copyright 1482f39c91SKip Macy * notice, this list of conditions and the following disclaimer in the 1582f39c91SKip Macy * documentation and/or other materials provided with the distribution. 1682f39c91SKip Macy * 1782f39c91SKip Macy * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1882f39c91SKip Macy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1982f39c91SKip Macy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2082f39c91SKip Macy * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 2182f39c91SKip Macy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2282f39c91SKip Macy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2382f39c91SKip Macy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2482f39c91SKip Macy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2582f39c91SKip Macy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2682f39c91SKip Macy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2782f39c91SKip Macy * SUCH DAMAGE. 2882f39c91SKip Macy */ 2982f39c91SKip Macy #include <sys/cdefs.h> 3082f39c91SKip Macy __FBSDID("$FreeBSD$"); 3182f39c91SKip Macy 3282f39c91SKip Macy #ifndef _NET_IF_LLATBL_H_ 3382f39c91SKip Macy #define _NET_IF_LLATBL_H_ 3482f39c91SKip Macy 3582f39c91SKip Macy #include <sys/_rwlock.h> 3682f39c91SKip Macy #include <netinet/in.h> 37*4f6c66ccSMatt Macy #include <sys/epoch.h> 3882f39c91SKip Macy 3982f39c91SKip Macy struct ifnet; 4082f39c91SKip Macy struct sysctl_req; 4182f39c91SKip Macy struct rt_msghdr; 4282f39c91SKip Macy struct rt_addrinfo; 4382f39c91SKip Macy struct llentry; 4482f39c91SKip Macy LIST_HEAD(llentries, llentry); 4582f39c91SKip Macy 464fb3a820SAlexander V. Chernikov #define LLE_MAX_LINKHDR 24 /* Full IB header */ 4782f39c91SKip Macy /* 4882f39c91SKip Macy * Code referencing llentry must at least hold 4982f39c91SKip Macy * a shared lock 5082f39c91SKip Macy */ 5182f39c91SKip Macy struct llentry { 5282f39c91SKip Macy LIST_ENTRY(llentry) lle_next; 53314294deSAlexander V. Chernikov union { 54314294deSAlexander V. Chernikov struct in_addr addr4; 55314294deSAlexander V. Chernikov struct in6_addr addr6; 56314294deSAlexander V. Chernikov } r_l3addr; 574fb3a820SAlexander V. Chernikov char r_linkdata[LLE_MAX_LINKHDR]; /* L2 data */ 584fb3a820SAlexander V. Chernikov uint8_t r_hdrlen; /* length for LL header */ 594fb3a820SAlexander V. Chernikov uint8_t spare0[3]; 60f8aee88fSAlexander V. Chernikov uint16_t r_flags; /* LLE runtime flags */ 61f8aee88fSAlexander V. Chernikov uint16_t r_skip_req; /* feedback from fast path */ 62314294deSAlexander V. Chernikov 6382f39c91SKip Macy struct lltable *lle_tbl; 6482f39c91SKip Macy struct llentries *lle_head; 6511cdad98SAlexander V. Chernikov void (*lle_free)(struct llentry *); 6682f39c91SKip Macy struct mbuf *la_hold; 67e162ea60SGeorge V. Neville-Neil int la_numheld; /* # of packets currently held */ 6882f39c91SKip Macy time_t la_expire; 6982f39c91SKip Macy uint16_t la_flags; 7082f39c91SKip Macy uint16_t la_asked; 7182f39c91SKip Macy uint16_t la_preempt; 7282f39c91SKip Macy int16_t ln_state; /* IPv6 has ND6_LLINFO_NOSTATE == -2 */ 7382f39c91SKip Macy uint16_t ln_router; 7482f39c91SKip Macy time_t ln_ntick; 7512cb7521SAlexander V. Chernikov time_t lle_remtime; /* Real time remaining */ 7612cb7521SAlexander V. Chernikov time_t lle_hittime; /* Time when r_skip_req was unset */ 7782f39c91SKip Macy int lle_refcnt; 784fb3a820SAlexander V. Chernikov char *ll_addr; /* link-layer address */ 7982f39c91SKip Macy 8011cdad98SAlexander V. Chernikov LIST_ENTRY(llentry) lle_chain; /* chain of deleted items */ 810447c136SAlexander V. Chernikov struct callout lle_timer; 82314294deSAlexander V. Chernikov struct rwlock lle_lock; 83f8aee88fSAlexander V. Chernikov struct mtx req_mtx; 84*4f6c66ccSMatt Macy struct epoch_context lle_epoch_ctx; 8582f39c91SKip Macy }; 8682f39c91SKip Macy 8782f39c91SKip Macy #define LLE_WLOCK(lle) rw_wlock(&(lle)->lle_lock) 8882f39c91SKip Macy #define LLE_RLOCK(lle) rw_rlock(&(lle)->lle_lock) 8982f39c91SKip Macy #define LLE_WUNLOCK(lle) rw_wunlock(&(lle)->lle_lock) 9082f39c91SKip Macy #define LLE_RUNLOCK(lle) rw_runlock(&(lle)->lle_lock) 9182f39c91SKip Macy #define LLE_DOWNGRADE(lle) rw_downgrade(&(lle)->lle_lock) 9282f39c91SKip Macy #define LLE_TRY_UPGRADE(lle) rw_try_upgrade(&(lle)->lle_lock) 9382f39c91SKip Macy #define LLE_LOCK_INIT(lle) rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK) 948a61a4eeSKip Macy #define LLE_LOCK_DESTROY(lle) rw_destroy(&(lle)->lle_lock) 9582f39c91SKip Macy #define LLE_WLOCK_ASSERT(lle) rw_assert(&(lle)->lle_lock, RA_WLOCKED) 9682f39c91SKip Macy 97f8aee88fSAlexander V. Chernikov #define LLE_REQ_INIT(lle) mtx_init(&(lle)->req_mtx, "lle req", \ 98f8aee88fSAlexander V. Chernikov NULL, MTX_DEF) 99f8aee88fSAlexander V. Chernikov #define LLE_REQ_DESTROY(lle) mtx_destroy(&(lle)->req_mtx) 100f8aee88fSAlexander V. Chernikov #define LLE_REQ_LOCK(lle) mtx_lock(&(lle)->req_mtx) 101f8aee88fSAlexander V. Chernikov #define LLE_REQ_UNLOCK(lle) mtx_unlock(&(lle)->req_mtx) 102f8aee88fSAlexander V. Chernikov 10382f39c91SKip Macy #define LLE_IS_VALID(lle) (((lle) != NULL) && ((lle) != (void *)-1)) 10482f39c91SKip Macy 10582f39c91SKip Macy #define LLE_ADDREF(lle) do { \ 10682f39c91SKip Macy LLE_WLOCK_ASSERT(lle); \ 10782f39c91SKip Macy KASSERT((lle)->lle_refcnt >= 0, \ 108ea537929SGleb Smirnoff ("negative refcnt %d on lle %p", \ 109ea537929SGleb Smirnoff (lle)->lle_refcnt, (lle))); \ 11082f39c91SKip Macy (lle)->lle_refcnt++; \ 11182f39c91SKip Macy } while (0) 112ea50c13eSGleb Smirnoff 11382f39c91SKip Macy #define LLE_REMREF(lle) do { \ 11482f39c91SKip Macy LLE_WLOCK_ASSERT(lle); \ 115ea537929SGleb Smirnoff KASSERT((lle)->lle_refcnt > 0, \ 116ea537929SGleb Smirnoff ("bogus refcnt %d on lle %p", \ 117ea537929SGleb Smirnoff (lle)->lle_refcnt, (lle))); \ 11882f39c91SKip Macy (lle)->lle_refcnt--; \ 11982f39c91SKip Macy } while (0) 12082f39c91SKip Macy 12182f39c91SKip Macy #define LLE_FREE_LOCKED(lle) do { \ 122ea537929SGleb Smirnoff if ((lle)->lle_refcnt == 1) \ 12311cdad98SAlexander V. Chernikov (lle)->lle_free(lle); \ 12482f39c91SKip Macy else { \ 125ea537929SGleb Smirnoff LLE_REMREF(lle); \ 12682f39c91SKip Macy LLE_WUNLOCK(lle); \ 12782f39c91SKip Macy } \ 12882f39c91SKip Macy /* guard against invalid refs */ \ 129ea537929SGleb Smirnoff (lle) = NULL; \ 13082f39c91SKip Macy } while (0) 13182f39c91SKip Macy 13282f39c91SKip Macy #define LLE_FREE(lle) do { \ 13382f39c91SKip Macy LLE_WLOCK(lle); \ 134962be6dfSBjoern A. Zeeb LLE_FREE_LOCKED(lle); \ 13582f39c91SKip Macy } while (0) 13682f39c91SKip Macy 137b4b1367aSAlexander V. Chernikov typedef struct llentry *(llt_lookup_t)(struct lltable *, u_int flags, 138b4b1367aSAlexander V. Chernikov const struct sockaddr *l3addr); 1395a255516SAlexander V. Chernikov typedef struct llentry *(llt_alloc_t)(struct lltable *, u_int flags, 140b4b1367aSAlexander V. Chernikov const struct sockaddr *l3addr); 1413e7a2321SAlexander V. Chernikov typedef void (llt_delete_t)(struct lltable *, struct llentry *); 142b4b1367aSAlexander V. Chernikov typedef void (llt_prefix_free_t)(struct lltable *, 1433e7a2321SAlexander V. Chernikov const struct sockaddr *addr, const struct sockaddr *mask, u_int flags); 14411cdad98SAlexander V. Chernikov typedef int (llt_dump_entry_t)(struct lltable *, struct llentry *, 14511cdad98SAlexander V. Chernikov struct sysctl_req *); 14611cdad98SAlexander V. Chernikov typedef uint32_t (llt_hash_t)(const struct llentry *, uint32_t); 14711cdad98SAlexander V. Chernikov typedef int (llt_match_prefix_t)(const struct sockaddr *, 14811cdad98SAlexander V. Chernikov const struct sockaddr *, u_int, struct llentry *); 14911cdad98SAlexander V. Chernikov typedef void (llt_free_entry_t)(struct lltable *, struct llentry *); 15011cdad98SAlexander V. Chernikov typedef void (llt_fill_sa_entry_t)(const struct llentry *, struct sockaddr *); 15141cb42a6SAlexander V. Chernikov typedef void (llt_free_tbl_t)(struct lltable *); 15211cdad98SAlexander V. Chernikov typedef void (llt_link_entry_t)(struct lltable *, struct llentry *); 15311cdad98SAlexander V. Chernikov typedef void (llt_unlink_entry_t)(struct llentry *); 1541435dcd9SAlexander V. Chernikov typedef void (llt_mark_used_t)(struct llentry *); 15511cdad98SAlexander V. Chernikov 15611cdad98SAlexander V. Chernikov typedef int (llt_foreach_cb_t)(struct lltable *, struct llentry *, void *); 15711cdad98SAlexander V. Chernikov typedef int (llt_foreach_entry_t)(struct lltable *, llt_foreach_cb_t *, void *); 158b4b1367aSAlexander V. Chernikov 15982f39c91SKip Macy struct lltable { 16082f39c91SKip Macy SLIST_ENTRY(lltable) llt_link; 16182f39c91SKip Macy int llt_af; 16241cb42a6SAlexander V. Chernikov int llt_hsize; 16341cb42a6SAlexander V. Chernikov struct llentries *lle_head; 16482f39c91SKip Macy struct ifnet *llt_ifp; 16582f39c91SKip Macy 166b4b1367aSAlexander V. Chernikov llt_lookup_t *llt_lookup; 1675a255516SAlexander V. Chernikov llt_alloc_t *llt_alloc_entry; 1683e7a2321SAlexander V. Chernikov llt_delete_t *llt_delete_entry; 169b4b1367aSAlexander V. Chernikov llt_prefix_free_t *llt_prefix_free; 17011cdad98SAlexander V. Chernikov llt_dump_entry_t *llt_dump_entry; 17111cdad98SAlexander V. Chernikov llt_hash_t *llt_hash; 17211cdad98SAlexander V. Chernikov llt_match_prefix_t *llt_match_prefix; 17311cdad98SAlexander V. Chernikov llt_free_entry_t *llt_free_entry; 17411cdad98SAlexander V. Chernikov llt_foreach_entry_t *llt_foreach_entry; 17511cdad98SAlexander V. Chernikov llt_link_entry_t *llt_link_entry; 17611cdad98SAlexander V. Chernikov llt_unlink_entry_t *llt_unlink_entry; 17711cdad98SAlexander V. Chernikov llt_fill_sa_entry_t *llt_fill_sa_entry; 17841cb42a6SAlexander V. Chernikov llt_free_tbl_t *llt_free_tbl; 1791435dcd9SAlexander V. Chernikov llt_mark_used_t *llt_mark_used; 18082f39c91SKip Macy }; 181b4b1367aSAlexander V. Chernikov 18282f39c91SKip Macy MALLOC_DECLARE(M_LLTABLE); 18382f39c91SKip Macy 18482f39c91SKip Macy /* 185e362cf0eSAlexander V. Chernikov * LLentry flags 18682f39c91SKip Macy */ 18782f39c91SKip Macy #define LLE_DELETED 0x0001 /* entry must be deleted */ 18882f39c91SKip Macy #define LLE_STATIC 0x0002 /* entry is static */ 18982f39c91SKip Macy #define LLE_IFADDR 0x0004 /* entry is interface addr */ 19082f39c91SKip Macy #define LLE_VALID 0x0008 /* ll_addr is valid */ 19117a03656SEric van Gyzen #define LLE_REDIRECT 0x0010 /* installed by redirect; has host rtentry */ 19282f39c91SKip Macy #define LLE_PUB 0x0020 /* publish entry ??? */ 193ea537929SGleb Smirnoff #define LLE_LINKED 0x0040 /* linked to lookup structure */ 194b4b1367aSAlexander V. Chernikov /* LLE request flags */ 195ea50c13eSGleb Smirnoff #define LLE_EXCLUSIVE 0x2000 /* return lle xlocked */ 196f8aee88fSAlexander V. Chernikov #define LLE_UNLOCKED 0x4000 /* return lle unlocked */ 1974fb3a820SAlexander V. Chernikov #define LLE_ADDRONLY 0x4000 /* return lladdr instead of full header */ 1984fb3a820SAlexander V. Chernikov #define LLE_CREATE 0x8000 /* hint to avoid lle lookup */ 199f8aee88fSAlexander V. Chernikov 200f8aee88fSAlexander V. Chernikov /* LLE flags used by fastpath code */ 201f8aee88fSAlexander V. Chernikov #define RLLE_VALID 0x0001 /* entry is valid */ 202f8aee88fSAlexander V. Chernikov #define RLLE_IFADDR LLE_IFADDR /* entry is ifaddr */ 20382f39c91SKip Macy 20482f39c91SKip Macy #define LLATBL_HASH(key, mask) \ 20582f39c91SKip Macy (((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask) 20682f39c91SKip Macy 20741cb42a6SAlexander V. Chernikov struct lltable *lltable_allocate_htbl(uint32_t hsize); 20882f39c91SKip Macy void lltable_free(struct lltable *); 20941cb42a6SAlexander V. Chernikov void lltable_link(struct lltable *llt); 210c9d763bfSQing Li void lltable_prefix_free(int, struct sockaddr *, 2115b84dc78SQing Li struct sockaddr *, u_int); 212fc2bfb32SBjoern A. Zeeb #if 0 21382f39c91SKip Macy void lltable_drain(int); 214fc2bfb32SBjoern A. Zeeb #endif 21582f39c91SKip Macy int lltable_sysctl_dumparp(int, struct sysctl_req *); 21682f39c91SKip Macy 217e162ea60SGeorge V. Neville-Neil size_t llentry_free(struct llentry *); 218b1d86af7SGleb Smirnoff struct llentry *llentry_alloc(struct ifnet *, struct lltable *, 219b1d86af7SGleb Smirnoff struct sockaddr_storage *); 22082f39c91SKip Macy 22111cdad98SAlexander V. Chernikov /* helper functions */ 22211cdad98SAlexander V. Chernikov size_t lltable_drop_entry_queue(struct llentry *); 223ddd208f7SAlexander V. Chernikov void lltable_set_entry_addr(struct ifnet *ifp, struct llentry *lle, 2244fb3a820SAlexander V. Chernikov const char *linkhdr, size_t linkhdrsize, int lladdr_off); 22512cb7521SAlexander V. Chernikov int lltable_try_set_entry_addr(struct ifnet *ifp, struct llentry *lle, 2264fb3a820SAlexander V. Chernikov const char *linkhdr, size_t linkhdrsize, int lladdr_off); 22711cdad98SAlexander V. Chernikov 2284fb3a820SAlexander V. Chernikov int lltable_calc_llheader(struct ifnet *ifp, int family, char *lladdr, 2294fb3a820SAlexander V. Chernikov char *buf, size_t *bufsize, int *lladdr_off); 2304fb3a820SAlexander V. Chernikov void lltable_update_ifaddr(struct lltable *llt); 2315a255516SAlexander V. Chernikov struct llentry *lltable_alloc_entry(struct lltable *llt, u_int flags, 2325a255516SAlexander V. Chernikov const struct sockaddr *l4addr); 2335a255516SAlexander V. Chernikov void lltable_free_entry(struct lltable *llt, struct llentry *lle); 2343e7a2321SAlexander V. Chernikov int lltable_delete_addr(struct lltable *llt, u_int flags, 2353e7a2321SAlexander V. Chernikov const struct sockaddr *l3addr); 23611cdad98SAlexander V. Chernikov void lltable_link_entry(struct lltable *llt, struct llentry *lle); 23711cdad98SAlexander V. Chernikov void lltable_unlink_entry(struct lltable *llt, struct llentry *lle); 23811cdad98SAlexander V. Chernikov void lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa); 23911cdad98SAlexander V. Chernikov struct ifnet *lltable_get_ifp(const struct lltable *llt); 24011cdad98SAlexander V. Chernikov int lltable_get_af(const struct lltable *llt); 24111cdad98SAlexander V. Chernikov 24211cdad98SAlexander V. Chernikov int lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f, 24311cdad98SAlexander V. Chernikov void *farg); 24482f39c91SKip Macy /* 24582f39c91SKip Macy * Generic link layer address lookup function. 24682f39c91SKip Macy */ 24782f39c91SKip Macy static __inline struct llentry * 24882f39c91SKip Macy lla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 24982f39c91SKip Macy { 250b4b1367aSAlexander V. Chernikov 251e362cf0eSAlexander V. Chernikov return (llt->llt_lookup(llt, flags, l3addr)); 25282f39c91SKip Macy } 25382f39c91SKip Macy 2541435dcd9SAlexander V. Chernikov /* 2551435dcd9SAlexander V. Chernikov * Notify the LLE code that the entry was used by datapath. 2561435dcd9SAlexander V. Chernikov */ 2571435dcd9SAlexander V. Chernikov static __inline void 2581435dcd9SAlexander V. Chernikov llentry_mark_used(struct llentry *lle) 2591435dcd9SAlexander V. Chernikov { 2601435dcd9SAlexander V. Chernikov 2611435dcd9SAlexander V. Chernikov if (lle->r_skip_req == 0) 2621435dcd9SAlexander V. Chernikov return; 2631435dcd9SAlexander V. Chernikov if ((lle->r_flags & RLLE_VALID) != 0) 2641435dcd9SAlexander V. Chernikov lle->lle_tbl->llt_mark_used(lle); 2651435dcd9SAlexander V. Chernikov } 2661435dcd9SAlexander V. Chernikov 26782f39c91SKip Macy int lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *); 2684364ec08SNavdeep Parhar 2694364ec08SNavdeep Parhar #include <sys/eventhandler.h> 2704364ec08SNavdeep Parhar enum { 2714364ec08SNavdeep Parhar LLENTRY_RESOLVED, 2724364ec08SNavdeep Parhar LLENTRY_TIMEDOUT, 2734364ec08SNavdeep Parhar LLENTRY_DELETED, 2744364ec08SNavdeep Parhar LLENTRY_EXPIRED, 2754364ec08SNavdeep Parhar }; 2764364ec08SNavdeep Parhar typedef void (*lle_event_fn)(void *, struct llentry *, int); 2774364ec08SNavdeep Parhar EVENTHANDLER_DECLARE(lle_event, lle_event_fn); 27882f39c91SKip Macy #endif /* _NET_IF_LLATBL_H_ */ 279