182f39c91SKip Macy /* 282f39c91SKip Macy * Copyright (c) 2004 Luigi Rizzo, Alessandro Cerri. All rights reserved. 382f39c91SKip Macy * Copyright (c) 2004-2008 Qing Li. All rights reserved. 482f39c91SKip Macy * Copyright (c) 2008 Kip Macy. All rights reserved. 582f39c91SKip Macy * 682f39c91SKip Macy * Redistribution and use in source and binary forms, with or without 782f39c91SKip Macy * modification, are permitted provided that the following conditions 882f39c91SKip Macy * are met: 982f39c91SKip Macy * 1. Redistributions of source code must retain the above copyright 1082f39c91SKip Macy * notice, this list of conditions and the following disclaimer. 1182f39c91SKip Macy * 2. Redistributions in binary form must reproduce the above copyright 1282f39c91SKip Macy * notice, this list of conditions and the following disclaimer in the 1382f39c91SKip Macy * documentation and/or other materials provided with the distribution. 1482f39c91SKip Macy * 1582f39c91SKip Macy * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1682f39c91SKip Macy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1782f39c91SKip Macy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1882f39c91SKip Macy * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 1982f39c91SKip Macy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2082f39c91SKip Macy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2182f39c91SKip Macy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2282f39c91SKip Macy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2382f39c91SKip Macy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2482f39c91SKip Macy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2582f39c91SKip Macy * SUCH DAMAGE. 2682f39c91SKip Macy */ 2782f39c91SKip Macy #include <sys/cdefs.h> 2882f39c91SKip Macy __FBSDID("$FreeBSD$"); 2982f39c91SKip Macy 3082f39c91SKip Macy #ifndef _NET_IF_LLATBL_H_ 3182f39c91SKip Macy #define _NET_IF_LLATBL_H_ 3282f39c91SKip Macy 3382f39c91SKip Macy #include <sys/_rwlock.h> 3482f39c91SKip Macy #include <netinet/in.h> 3582f39c91SKip Macy 3682f39c91SKip Macy struct ifnet; 3782f39c91SKip Macy struct sysctl_req; 3882f39c91SKip Macy struct rt_msghdr; 3982f39c91SKip Macy struct rt_addrinfo; 4082f39c91SKip Macy 4182f39c91SKip Macy struct llentry; 4282f39c91SKip Macy LIST_HEAD(llentries, llentry); 4382f39c91SKip Macy 44dc56e98fSRobert Watson extern struct rwlock lltable_rwlock; 45dc56e98fSRobert Watson #define LLTABLE_RLOCK() rw_rlock(&lltable_rwlock) 46dc56e98fSRobert Watson #define LLTABLE_RUNLOCK() rw_runlock(&lltable_rwlock) 47dc56e98fSRobert Watson #define LLTABLE_WLOCK() rw_wlock(&lltable_rwlock) 48dc56e98fSRobert Watson #define LLTABLE_WUNLOCK() rw_wunlock(&lltable_rwlock) 49dc56e98fSRobert Watson #define LLTABLE_LOCK_ASSERT() rw_assert(&lltable_rwlock, RA_LOCKED) 50dc56e98fSRobert Watson 5182f39c91SKip Macy /* 5282f39c91SKip Macy * Code referencing llentry must at least hold 5382f39c91SKip Macy * a shared lock 5482f39c91SKip Macy */ 5582f39c91SKip Macy struct llentry { 5682f39c91SKip Macy LIST_ENTRY(llentry) lle_next; 5782f39c91SKip Macy struct rwlock lle_lock; 5882f39c91SKip Macy struct lltable *lle_tbl; 5982f39c91SKip Macy struct llentries *lle_head; 6082f39c91SKip Macy struct mbuf *la_hold; 6182f39c91SKip Macy time_t la_expire; 6282f39c91SKip Macy uint16_t la_flags; 6382f39c91SKip Macy uint16_t la_asked; 6482f39c91SKip Macy uint16_t la_preempt; 6582f39c91SKip Macy uint16_t ln_byhint; 6682f39c91SKip Macy int16_t ln_state; /* IPv6 has ND6_LLINFO_NOSTATE == -2 */ 6782f39c91SKip Macy uint16_t ln_router; 6882f39c91SKip Macy time_t ln_ntick; 6982f39c91SKip Macy int lle_refcnt; 7082f39c91SKip Macy 7182f39c91SKip Macy union { 7282f39c91SKip Macy uint64_t mac_aligned; 7382f39c91SKip Macy uint16_t mac16[3]; 7482f39c91SKip Macy } ll_addr; 7582f39c91SKip Macy 7682f39c91SKip Macy /* XXX af-private? */ 7782f39c91SKip Macy union { 7882f39c91SKip Macy struct callout ln_timer_ch; 7982f39c91SKip Macy struct callout la_timer; 8082f39c91SKip Macy } lle_timer; 8182f39c91SKip Macy /* NB: struct sockaddr must immediately follow */ 8282f39c91SKip Macy }; 8382f39c91SKip Macy 8482f39c91SKip Macy #define LLE_WLOCK(lle) rw_wlock(&(lle)->lle_lock) 8582f39c91SKip Macy #define LLE_RLOCK(lle) rw_rlock(&(lle)->lle_lock) 8682f39c91SKip Macy #define LLE_WUNLOCK(lle) rw_wunlock(&(lle)->lle_lock) 8782f39c91SKip Macy #define LLE_RUNLOCK(lle) rw_runlock(&(lle)->lle_lock) 8882f39c91SKip Macy #define LLE_DOWNGRADE(lle) rw_downgrade(&(lle)->lle_lock) 8982f39c91SKip Macy #define LLE_TRY_UPGRADE(lle) rw_try_upgrade(&(lle)->lle_lock) 9082f39c91SKip Macy #define LLE_LOCK_INIT(lle) rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK) 918a61a4eeSKip Macy #define LLE_LOCK_DESTROY(lle) rw_destroy(&(lle)->lle_lock) 9282f39c91SKip Macy #define LLE_WLOCK_ASSERT(lle) rw_assert(&(lle)->lle_lock, RA_WLOCKED) 9382f39c91SKip Macy 9482f39c91SKip Macy #define LLE_IS_VALID(lle) (((lle) != NULL) && ((lle) != (void *)-1)) 9582f39c91SKip Macy 9682f39c91SKip Macy #define LLE_ADDREF(lle) do { \ 9782f39c91SKip Macy LLE_WLOCK_ASSERT(lle); \ 9882f39c91SKip Macy KASSERT((lle)->lle_refcnt >= 0, \ 9982f39c91SKip Macy ("negative refcnt %d", (lle)->lle_refcnt)); \ 10082f39c91SKip Macy (lle)->lle_refcnt++; \ 10182f39c91SKip Macy } while (0) 10282f39c91SKip Macy 10382f39c91SKip Macy #define LLE_REMREF(lle) do { \ 10482f39c91SKip Macy LLE_WLOCK_ASSERT(lle); \ 10582f39c91SKip Macy KASSERT((lle)->lle_refcnt > 1, \ 10682f39c91SKip Macy ("bogus refcnt %d", (lle)->lle_refcnt)); \ 10782f39c91SKip Macy (lle)->lle_refcnt--; \ 10882f39c91SKip Macy } while (0) 10982f39c91SKip Macy 11082f39c91SKip Macy #define LLE_FREE_LOCKED(lle) do { \ 11182f39c91SKip Macy if ((lle)->lle_refcnt <= 1) \ 11282f39c91SKip Macy (lle)->lle_tbl->llt_free((lle)->lle_tbl, (lle));\ 11382f39c91SKip Macy else { \ 11482f39c91SKip Macy (lle)->lle_refcnt--; \ 11582f39c91SKip Macy LLE_WUNLOCK(lle); \ 11682f39c91SKip Macy } \ 11782f39c91SKip Macy /* guard against invalid refs */ \ 11882f39c91SKip Macy lle = 0; \ 11982f39c91SKip Macy } while (0) 12082f39c91SKip Macy 12182f39c91SKip Macy #define LLE_FREE(lle) do { \ 12282f39c91SKip Macy LLE_WLOCK(lle); \ 12382f39c91SKip Macy if ((lle)->lle_refcnt <= 1) \ 12482f39c91SKip Macy (lle)->lle_tbl->llt_free((lle)->lle_tbl, (lle));\ 12582f39c91SKip Macy else { \ 12682f39c91SKip Macy (lle)->lle_refcnt--; \ 12782f39c91SKip Macy LLE_WUNLOCK(lle); \ 12882f39c91SKip Macy } \ 12982f39c91SKip Macy /* guard against invalid refs */ \ 1308a61a4eeSKip Macy lle = NULL; \ 13182f39c91SKip Macy } while (0) 13282f39c91SKip Macy 13382f39c91SKip Macy 13482f39c91SKip Macy #define ln_timer_ch lle_timer.ln_timer_ch 13582f39c91SKip Macy #define la_timer lle_timer.la_timer 13682f39c91SKip Macy 13782f39c91SKip Macy /* XXX bad name */ 13882f39c91SKip Macy #define L3_ADDR(lle) ((struct sockaddr *)(&lle[1])) 13982f39c91SKip Macy #define L3_ADDR_LEN(lle) (((struct sockaddr *)(&lle[1]))->sa_len) 14082f39c91SKip Macy 14182f39c91SKip Macy #ifndef LLTBL_HASHTBL_SIZE 14282f39c91SKip Macy #define LLTBL_HASHTBL_SIZE 32 /* default 32 ? */ 14382f39c91SKip Macy #endif 14482f39c91SKip Macy 14582f39c91SKip Macy #ifndef LLTBL_HASHMASK 14682f39c91SKip Macy #define LLTBL_HASHMASK (LLTBL_HASHTBL_SIZE - 1) 14782f39c91SKip Macy #endif 14882f39c91SKip Macy 14982f39c91SKip Macy struct lltable { 15082f39c91SKip Macy SLIST_ENTRY(lltable) llt_link; 15182f39c91SKip Macy struct llentries lle_head[LLTBL_HASHTBL_SIZE]; 15282f39c91SKip Macy int llt_af; 15382f39c91SKip Macy struct ifnet *llt_ifp; 15482f39c91SKip Macy 15582f39c91SKip Macy struct llentry * (*llt_new)(const struct sockaddr *, u_int); 15682f39c91SKip Macy void (*llt_free)(struct lltable *, struct llentry *); 157c9d763bfSQing Li void (*llt_prefix_free)(struct lltable *, 158c9d763bfSQing Li const struct sockaddr *prefix, 159c9d763bfSQing Li const struct sockaddr *mask); 16082f39c91SKip Macy struct llentry * (*llt_lookup)(struct lltable *, u_int flags, 16182f39c91SKip Macy const struct sockaddr *l3addr); 162c7ab6602SQing Li int (*llt_rtcheck)(struct ifnet *, u_int flags, 16382f39c91SKip Macy const struct sockaddr *); 16482f39c91SKip Macy int (*llt_dump)(struct lltable *, 16582f39c91SKip Macy struct sysctl_req *); 16682f39c91SKip Macy }; 16782f39c91SKip Macy MALLOC_DECLARE(M_LLTABLE); 16882f39c91SKip Macy 16982f39c91SKip Macy /* 17082f39c91SKip Macy * flags to be passed to arplookup. 17182f39c91SKip Macy */ 17282f39c91SKip Macy #define LLE_DELETED 0x0001 /* entry must be deleted */ 17382f39c91SKip Macy #define LLE_STATIC 0x0002 /* entry is static */ 17482f39c91SKip Macy #define LLE_IFADDR 0x0004 /* entry is interface addr */ 17582f39c91SKip Macy #define LLE_VALID 0x0008 /* ll_addr is valid */ 17682f39c91SKip Macy #define LLE_PROXY 0x0010 /* proxy entry ??? */ 17782f39c91SKip Macy #define LLE_PUB 0x0020 /* publish entry ??? */ 17882f39c91SKip Macy #define LLE_DELETE 0x4000 /* delete on a lookup - match LLE_IFADDR */ 17982f39c91SKip Macy #define LLE_CREATE 0x8000 /* create on a lookup miss */ 18082f39c91SKip Macy #define LLE_EXCLUSIVE 0x2000 /* return lle xlocked */ 18182f39c91SKip Macy 18282f39c91SKip Macy #define LLATBL_HASH(key, mask) \ 18382f39c91SKip Macy (((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask) 18482f39c91SKip Macy 18582f39c91SKip Macy struct lltable *lltable_init(struct ifnet *, int); 18682f39c91SKip Macy void lltable_free(struct lltable *); 187c9d763bfSQing Li void lltable_prefix_free(int, struct sockaddr *, 188c9d763bfSQing Li struct sockaddr *); 189*fc2bfb32SBjoern A. Zeeb #if 0 19082f39c91SKip Macy void lltable_drain(int); 191*fc2bfb32SBjoern A. Zeeb #endif 19282f39c91SKip Macy int lltable_sysctl_dumparp(int, struct sysctl_req *); 19382f39c91SKip Macy 19482f39c91SKip Macy void llentry_free(struct llentry *); 195c8da95acSKip Macy int llentry_update(struct llentry **, struct lltable *, 196d4121a02SKip Macy struct sockaddr_storage *, struct ifnet *); 19782f39c91SKip Macy 19882f39c91SKip Macy /* 19982f39c91SKip Macy * Generic link layer address lookup function. 20082f39c91SKip Macy */ 20182f39c91SKip Macy static __inline struct llentry * 20282f39c91SKip Macy lla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 20382f39c91SKip Macy { 20482f39c91SKip Macy return llt->llt_lookup(llt, flags, l3addr); 20582f39c91SKip Macy } 20682f39c91SKip Macy 20782f39c91SKip Macy int lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *); 20882f39c91SKip Macy #endif /* _NET_IF_LLATBL_H_ */ 209