if_llatbl.h revision 292978
1186121Skmacy/* 2186121Skmacy * Copyright (c) 2004 Luigi Rizzo, Alessandro Cerri. All rights reserved. 3186121Skmacy * Copyright (c) 2004-2008 Qing Li. All rights reserved. 4186121Skmacy * Copyright (c) 2008 Kip Macy. All rights reserved. 5186121Skmacy * 6186121Skmacy * Redistribution and use in source and binary forms, with or without 7186121Skmacy * modification, are permitted provided that the following conditions 8186121Skmacy * are met: 9186121Skmacy * 1. Redistributions of source code must retain the above copyright 10186121Skmacy * notice, this list of conditions and the following disclaimer. 11186121Skmacy * 2. Redistributions in binary form must reproduce the above copyright 12186121Skmacy * notice, this list of conditions and the following disclaimer in the 13186121Skmacy * documentation and/or other materials provided with the distribution. 14186121Skmacy * 15186121Skmacy * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16186121Skmacy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17186121Skmacy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18186121Skmacy * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 19186121Skmacy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20186121Skmacy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21186121Skmacy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22186121Skmacy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23186121Skmacy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24186121Skmacy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25186121Skmacy * SUCH DAMAGE. 26186121Skmacy */ 27186121Skmacy#include <sys/cdefs.h> 28186121Skmacy__FBSDID("$FreeBSD: head/sys/net/if_llatbl.h 292978 2015-12-31 05:03:27Z melifaro $"); 29186121Skmacy 30186121Skmacy#ifndef _NET_IF_LLATBL_H_ 31186121Skmacy#define _NET_IF_LLATBL_H_ 32186121Skmacy 33186121Skmacy#include <sys/_rwlock.h> 34186121Skmacy#include <netinet/in.h> 35186121Skmacy 36186121Skmacystruct ifnet; 37186121Skmacystruct sysctl_req; 38186121Skmacystruct rt_msghdr; 39186121Skmacystruct rt_addrinfo; 40186121Skmacy 41186121Skmacystruct llentry; 42186121SkmacyLIST_HEAD(llentries, llentry); 43186121Skmacy 44250300Sandreextern struct rwlock lltable_rwlock; 45196535Srwatson#define LLTABLE_RLOCK() rw_rlock(&lltable_rwlock) 46196535Srwatson#define LLTABLE_RUNLOCK() rw_runlock(&lltable_rwlock) 47196535Srwatson#define LLTABLE_WLOCK() rw_wlock(&lltable_rwlock) 48196535Srwatson#define LLTABLE_WUNLOCK() rw_wunlock(&lltable_rwlock) 49196535Srwatson#define LLTABLE_LOCK_ASSERT() rw_assert(&lltable_rwlock, RA_LOCKED) 50196535Srwatson 51292978Smelifaro#define LLE_MAX_LINKHDR 24 /* Full IB header */ 52186121Skmacy/* 53186121Skmacy * Code referencing llentry must at least hold 54186121Skmacy * a shared lock 55186121Skmacy */ 56186121Skmacystruct llentry { 57186121Skmacy LIST_ENTRY(llentry) lle_next; 58286624Smelifaro union { 59286624Smelifaro struct in_addr addr4; 60286624Smelifaro struct in6_addr addr6; 61286624Smelifaro } r_l3addr; 62292978Smelifaro char r_linkdata[LLE_MAX_LINKHDR]; /* L2 data */ 63292978Smelifaro uint8_t r_hdrlen; /* length for LL header */ 64292978Smelifaro uint8_t spare0[3]; 65291853Smelifaro uint16_t r_flags; /* LLE runtime flags */ 66291853Smelifaro uint16_t r_skip_req; /* feedback from fast path */ 67286624Smelifaro 68186121Skmacy struct lltable *lle_tbl; 69186121Skmacy struct llentries *lle_head; 70286577Smelifaro void (*lle_free)(struct llentry *); 71186121Skmacy struct mbuf *la_hold; 72238945Sglebius int la_numheld; /* # of packets currently held */ 73186121Skmacy time_t la_expire; 74238945Sglebius uint16_t la_flags; 75186121Skmacy uint16_t la_asked; 76186121Skmacy uint16_t la_preempt; 77186121Skmacy int16_t ln_state; /* IPv6 has ND6_LLINFO_NOSTATE == -2 */ 78238945Sglebius uint16_t ln_router; 79186121Skmacy time_t ln_ntick; 80292155Smelifaro time_t lle_remtime; /* Real time remaining */ 81292155Smelifaro time_t lle_hittime; /* Time when r_skip_req was unset */ 82186121Skmacy int lle_refcnt; 83292978Smelifaro char *ll_addr; /* link-layer address */ 84238945Sglebius 85286577Smelifaro LIST_ENTRY(llentry) lle_chain; /* chain of deleted items */ 86286629Smelifaro struct callout lle_timer; 87286624Smelifaro struct rwlock lle_lock; 88291853Smelifaro struct mtx req_mtx; 89186121Skmacy}; 90186121Skmacy 91186121Skmacy#define LLE_WLOCK(lle) rw_wlock(&(lle)->lle_lock) 92186121Skmacy#define LLE_RLOCK(lle) rw_rlock(&(lle)->lle_lock) 93186121Skmacy#define LLE_WUNLOCK(lle) rw_wunlock(&(lle)->lle_lock) 94186121Skmacy#define LLE_RUNLOCK(lle) rw_runlock(&(lle)->lle_lock) 95186121Skmacy#define LLE_DOWNGRADE(lle) rw_downgrade(&(lle)->lle_lock) 96186121Skmacy#define LLE_TRY_UPGRADE(lle) rw_try_upgrade(&(lle)->lle_lock) 97186121Skmacy#define LLE_LOCK_INIT(lle) rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK) 98186149Skmacy#define LLE_LOCK_DESTROY(lle) rw_destroy(&(lle)->lle_lock) 99186121Skmacy#define LLE_WLOCK_ASSERT(lle) rw_assert(&(lle)->lle_lock, RA_WLOCKED) 100186121Skmacy 101291853Smelifaro#define LLE_REQ_INIT(lle) mtx_init(&(lle)->req_mtx, "lle req", \ 102291853Smelifaro NULL, MTX_DEF) 103291853Smelifaro#define LLE_REQ_DESTROY(lle) mtx_destroy(&(lle)->req_mtx) 104291853Smelifaro#define LLE_REQ_LOCK(lle) mtx_lock(&(lle)->req_mtx) 105291853Smelifaro#define LLE_REQ_UNLOCK(lle) mtx_unlock(&(lle)->req_mtx) 106291853Smelifaro 107186121Skmacy#define LLE_IS_VALID(lle) (((lle) != NULL) && ((lle) != (void *)-1)) 108186121Skmacy 109186121Skmacy#define LLE_ADDREF(lle) do { \ 110186121Skmacy LLE_WLOCK_ASSERT(lle); \ 111186121Skmacy KASSERT((lle)->lle_refcnt >= 0, \ 112238990Sglebius ("negative refcnt %d on lle %p", \ 113238990Sglebius (lle)->lle_refcnt, (lle))); \ 114186121Skmacy (lle)->lle_refcnt++; \ 115186121Skmacy} while (0) 116238945Sglebius 117186121Skmacy#define LLE_REMREF(lle) do { \ 118186121Skmacy LLE_WLOCK_ASSERT(lle); \ 119238990Sglebius KASSERT((lle)->lle_refcnt > 0, \ 120238990Sglebius ("bogus refcnt %d on lle %p", \ 121238990Sglebius (lle)->lle_refcnt, (lle))); \ 122186121Skmacy (lle)->lle_refcnt--; \ 123186121Skmacy} while (0) 124186121Skmacy 125186121Skmacy#define LLE_FREE_LOCKED(lle) do { \ 126238990Sglebius if ((lle)->lle_refcnt == 1) \ 127286577Smelifaro (lle)->lle_free(lle); \ 128186121Skmacy else { \ 129238990Sglebius LLE_REMREF(lle); \ 130186121Skmacy LLE_WUNLOCK(lle); \ 131186121Skmacy } \ 132186121Skmacy /* guard against invalid refs */ \ 133238990Sglebius (lle) = NULL; \ 134186121Skmacy} while (0) 135186121Skmacy 136186121Skmacy#define LLE_FREE(lle) do { \ 137186121Skmacy LLE_WLOCK(lle); \ 138216859Sbz LLE_FREE_LOCKED(lle); \ 139186121Skmacy} while (0) 140186121Skmacy 141186121Skmacy 142286457Smelifarotypedef struct llentry *(llt_lookup_t)(struct lltable *, u_int flags, 143286457Smelifaro const struct sockaddr *l3addr); 144286955Smelifarotypedef struct llentry *(llt_alloc_t)(struct lltable *, u_int flags, 145286457Smelifaro const struct sockaddr *l3addr); 146287789Smelifarotypedef void (llt_delete_t)(struct lltable *, struct llentry *); 147286457Smelifarotypedef void (llt_prefix_free_t)(struct lltable *, 148287789Smelifaro const struct sockaddr *addr, const struct sockaddr *mask, u_int flags); 149286577Smelifarotypedef int (llt_dump_entry_t)(struct lltable *, struct llentry *, 150286577Smelifaro struct sysctl_req *); 151286577Smelifarotypedef uint32_t (llt_hash_t)(const struct llentry *, uint32_t); 152286577Smelifarotypedef int (llt_match_prefix_t)(const struct sockaddr *, 153286577Smelifaro const struct sockaddr *, u_int, struct llentry *); 154286577Smelifarotypedef void (llt_free_entry_t)(struct lltable *, struct llentry *); 155286577Smelifarotypedef void (llt_fill_sa_entry_t)(const struct llentry *, struct sockaddr *); 156286616Smelifarotypedef void (llt_free_tbl_t)(struct lltable *); 157286577Smelifarotypedef void (llt_link_entry_t)(struct lltable *, struct llentry *); 158286577Smelifarotypedef void (llt_unlink_entry_t)(struct llentry *); 159286457Smelifaro 160286577Smelifarotypedef int (llt_foreach_cb_t)(struct lltable *, struct llentry *, void *); 161286577Smelifarotypedef int (llt_foreach_entry_t)(struct lltable *, llt_foreach_cb_t *, void *); 162286577Smelifaro 163186121Skmacystruct lltable { 164186121Skmacy SLIST_ENTRY(lltable) llt_link; 165186121Skmacy int llt_af; 166286616Smelifaro int llt_hsize; 167286616Smelifaro struct llentries *lle_head; 168186121Skmacy struct ifnet *llt_ifp; 169186121Skmacy 170286457Smelifaro llt_lookup_t *llt_lookup; 171286955Smelifaro llt_alloc_t *llt_alloc_entry; 172287789Smelifaro llt_delete_t *llt_delete_entry; 173286457Smelifaro llt_prefix_free_t *llt_prefix_free; 174286577Smelifaro llt_dump_entry_t *llt_dump_entry; 175286577Smelifaro llt_hash_t *llt_hash; 176286577Smelifaro llt_match_prefix_t *llt_match_prefix; 177286577Smelifaro llt_free_entry_t *llt_free_entry; 178286577Smelifaro llt_foreach_entry_t *llt_foreach_entry; 179286577Smelifaro llt_link_entry_t *llt_link_entry; 180286577Smelifaro llt_unlink_entry_t *llt_unlink_entry; 181286577Smelifaro llt_fill_sa_entry_t *llt_fill_sa_entry; 182286616Smelifaro llt_free_tbl_t *llt_free_tbl; 183186121Skmacy}; 184286457Smelifaro 185186121SkmacyMALLOC_DECLARE(M_LLTABLE); 186186121Skmacy 187186121Skmacy/* 188286457Smelifaro * LLentry flags 189186121Skmacy */ 190186121Skmacy#define LLE_DELETED 0x0001 /* entry must be deleted */ 191186121Skmacy#define LLE_STATIC 0x0002 /* entry is static */ 192186121Skmacy#define LLE_IFADDR 0x0004 /* entry is interface addr */ 193186121Skmacy#define LLE_VALID 0x0008 /* ll_addr is valid */ 194287798Svangyzen#define LLE_REDIRECT 0x0010 /* installed by redirect; has host rtentry */ 195186121Skmacy#define LLE_PUB 0x0020 /* publish entry ??? */ 196238990Sglebius#define LLE_LINKED 0x0040 /* linked to lookup structure */ 197286457Smelifaro/* LLE request flags */ 198238945Sglebius#define LLE_EXCLUSIVE 0x2000 /* return lle xlocked */ 199291853Smelifaro#define LLE_UNLOCKED 0x4000 /* return lle unlocked */ 200292978Smelifaro#define LLE_ADDRONLY 0x4000 /* return lladdr instead of full header */ 201292978Smelifaro#define LLE_CREATE 0x8000 /* hint to avoid lle lookup */ 202186121Skmacy 203291853Smelifaro/* LLE flags used by fastpath code */ 204291853Smelifaro#define RLLE_VALID 0x0001 /* entry is valid */ 205291853Smelifaro#define RLLE_IFADDR LLE_IFADDR /* entry is ifaddr */ 206291853Smelifaro 207186121Skmacy#define LLATBL_HASH(key, mask) \ 208186121Skmacy (((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask) 209186121Skmacy 210286616Smelifarostruct lltable *lltable_allocate_htbl(uint32_t hsize); 211186121Skmacyvoid lltable_free(struct lltable *); 212286616Smelifarovoid lltable_link(struct lltable *llt); 213238945Sglebiusvoid lltable_prefix_free(int, struct sockaddr *, 214238945Sglebius struct sockaddr *, u_int); 215213929Sbz#if 0 216186121Skmacyvoid lltable_drain(int); 217213929Sbz#endif 218186121Skmacyint lltable_sysctl_dumparp(int, struct sysctl_req *); 219186121Skmacy 220215207Sgnnsize_t llentry_free(struct llentry *); 221238989Sglebiusstruct llentry *llentry_alloc(struct ifnet *, struct lltable *, 222238989Sglebius struct sockaddr_storage *); 223186121Skmacy 224286577Smelifaro/* helper functions */ 225286577Smelifarosize_t lltable_drop_entry_queue(struct llentry *); 226290486Smelifarovoid lltable_set_entry_addr(struct ifnet *ifp, struct llentry *lle, 227292978Smelifaro const char *linkhdr, size_t linkhdrsize, int lladdr_off); 228292155Smelifaroint lltable_try_set_entry_addr(struct ifnet *ifp, struct llentry *lle, 229292978Smelifaro const char *linkhdr, size_t linkhdrsize, int lladdr_off); 230286577Smelifaro 231292978Smelifaroint lltable_calc_llheader(struct ifnet *ifp, int family, char *lladdr, 232292978Smelifaro char *buf, size_t *bufsize, int *lladdr_off); 233292978Smelifarovoid lltable_update_ifaddr(struct lltable *llt); 234286955Smelifarostruct llentry *lltable_alloc_entry(struct lltable *llt, u_int flags, 235286955Smelifaro const struct sockaddr *l4addr); 236286955Smelifarovoid lltable_free_entry(struct lltable *llt, struct llentry *lle); 237287789Smelifaroint lltable_delete_addr(struct lltable *llt, u_int flags, 238287789Smelifaro const struct sockaddr *l3addr); 239286577Smelifarovoid lltable_link_entry(struct lltable *llt, struct llentry *lle); 240286577Smelifarovoid lltable_unlink_entry(struct lltable *llt, struct llentry *lle); 241286577Smelifarovoid lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa); 242286577Smelifarostruct ifnet *lltable_get_ifp(const struct lltable *llt); 243286577Smelifaroint lltable_get_af(const struct lltable *llt); 244286577Smelifaro 245286577Smelifaroint lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f, 246286577Smelifaro void *farg); 247186121Skmacy/* 248186121Skmacy * Generic link layer address lookup function. 249186121Skmacy */ 250186121Skmacystatic __inline struct llentry * 251186121Skmacylla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 252186121Skmacy{ 253286457Smelifaro 254286457Smelifaro return (llt->llt_lookup(llt, flags, l3addr)); 255186121Skmacy} 256186121Skmacy 257186121Skmacyint lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *); 258245924Snp 259245924Snp#include <sys/eventhandler.h> 260245924Snpenum { 261245924Snp LLENTRY_RESOLVED, 262245924Snp LLENTRY_TIMEDOUT, 263245924Snp LLENTRY_DELETED, 264245924Snp LLENTRY_EXPIRED, 265245924Snp}; 266245924Snptypedef void (*lle_event_fn)(void *, struct llentry *, int); 267245924SnpEVENTHANDLER_DECLARE(lle_event, lle_event_fn); 268186121Skmacy#endif /* _NET_IF_LLATBL_H_ */ 269