Home | History | Annotate | Line # | Download | only in net
      1  1.19      yamt /*	$NetBSD: if_llatbl.h,v 1.19 2022/11/19 08:00:51 yamt Exp $	*/
      2   1.1     ozaki /*
      3   1.1     ozaki  * Copyright (c) 2004 Luigi Rizzo, Alessandro Cerri. All rights reserved.
      4   1.1     ozaki  * Copyright (c) 2004-2008 Qing Li. All rights reserved.
      5   1.1     ozaki  * Copyright (c) 2008 Kip Macy. All rights reserved.
      6   1.1     ozaki  * Copyright (c) 2015 The NetBSD Foundation, Inc.
      7   1.1     ozaki  * All rights reserved.
      8   1.1     ozaki  *
      9   1.1     ozaki  * Redistribution and use in source and binary forms, with or without
     10   1.1     ozaki  * modification, are permitted provided that the following conditions
     11   1.1     ozaki  * are met:
     12   1.1     ozaki  * 1. Redistributions of source code must retain the above copyright
     13   1.1     ozaki  *    notice, this list of conditions and the following disclaimer.
     14   1.1     ozaki  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1     ozaki  *    notice, this list of conditions and the following disclaimer in the
     16   1.1     ozaki  *    documentation and/or other materials provided with the distribution.
     17   1.1     ozaki  *
     18   1.1     ozaki  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     19   1.1     ozaki  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20   1.1     ozaki  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21   1.1     ozaki  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
     22   1.1     ozaki  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23   1.1     ozaki  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24   1.1     ozaki  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25   1.1     ozaki  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26   1.1     ozaki  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27   1.1     ozaki  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28   1.1     ozaki  * SUCH DAMAGE.
     29   1.1     ozaki  */
     30   1.1     ozaki #include <sys/cdefs.h>
     31   1.1     ozaki 
     32   1.1     ozaki #ifndef	_NET_IF_LLATBL_H_
     33   1.1     ozaki #define	_NET_IF_LLATBL_H_
     34   1.1     ozaki 
     35   1.3     ozaki #if defined(_KERNEL_OPT)
     36   1.3     ozaki #include "opt_gateway.h"
     37  1.19      yamt #include "opt_mbuftrace.h"
     38   1.3     ozaki #endif
     39   1.3     ozaki 
     40   1.1     ozaki #include <sys/rwlock.h>
     41  1.16     ozaki #include <sys/syslog.h>
     42   1.3     ozaki 
     43   1.1     ozaki #include <netinet/in.h>
     44   1.1     ozaki 
     45   1.1     ozaki struct ifnet;
     46   1.1     ozaki struct sysctl_req;
     47   1.1     ozaki struct rt_msghdr;
     48   1.1     ozaki struct rt_addrinfo;
     49   1.9     ozaki struct rt_walkarg;
     50   1.1     ozaki 
     51   1.1     ozaki struct llentry;
     52   1.1     ozaki LIST_HEAD(llentries, llentry);
     53   1.1     ozaki 
     54   1.1     ozaki extern krwlock_t lltable_rwlock;
     55   1.1     ozaki #define	LLTABLE_RLOCK()		rw_enter(&lltable_rwlock, RW_READER)
     56   1.1     ozaki #define	LLTABLE_RUNLOCK()	rw_exit(&lltable_rwlock)
     57   1.1     ozaki #define	LLTABLE_WLOCK()		rw_enter(&lltable_rwlock, RW_WRITER)
     58   1.1     ozaki #define	LLTABLE_WUNLOCK()	rw_exit(&lltable_rwlock)
     59   1.1     ozaki #define	LLTABLE_LOCK_ASSERT()	KASSERT(rw_lock_held(&lltable_rwlock))
     60   1.1     ozaki 
     61   1.1     ozaki /*
     62   1.1     ozaki  * Code referencing llentry must at least hold
     63   1.1     ozaki  * a shared lock
     64   1.1     ozaki  */
     65   1.1     ozaki struct llentry {
     66   1.1     ozaki 	LIST_ENTRY(llentry)	 lle_next;
     67  1.18       roy 	union l3addr {
     68   1.1     ozaki 		struct in_addr	addr4;
     69   1.1     ozaki 		struct in6_addr	addr6;
     70   1.1     ozaki 	} r_l3addr;
     71   1.1     ozaki 	union {
     72   1.1     ozaki 		uint64_t	mac_aligned;
     73   1.1     ozaki 		uint16_t	mac16[3];
     74   1.1     ozaki 		uint8_t		mac8[20];	/* IB needs 20 bytes. */
     75   1.1     ozaki 	} ll_addr;
     76   1.1     ozaki 	uint32_t		spare0;
     77   1.1     ozaki 	uint64_t		spare1;
     78   1.1     ozaki 
     79   1.1     ozaki 	struct lltable		 *lle_tbl;
     80   1.1     ozaki 	struct llentries	 *lle_head;
     81   1.1     ozaki 	void			(*lle_free)(struct llentry *);
     82   1.7     ozaki 	void			(*lle_ll_free)(struct llentry *);
     83   1.1     ozaki 	struct mbuf		 *la_hold;
     84   1.1     ozaki 	int			 la_numheld;  /* # of packets currently held */
     85   1.1     ozaki 	time_t			 la_expire;
     86   1.1     ozaki 	uint16_t		 la_flags;
     87   1.1     ozaki 	uint16_t		 la_asked;
     88   1.1     ozaki 	uint16_t		 la_preempt;
     89   1.1     ozaki 	uint16_t		 ln_byhint;
     90  1.18       roy 	int16_t			 ln_state;	/* ND_LLINFO_NOSTATE == -2 */
     91   1.1     ozaki 	uint16_t		 ln_router;
     92   1.1     ozaki 	time_t			 ln_ntick;
     93   1.1     ozaki 	int			 lle_refcnt;
     94   1.1     ozaki 
     95   1.1     ozaki 	LIST_ENTRY(llentry)	lle_chain;	/* chain of deleted items */
     96   1.1     ozaki 	struct callout		lle_timer;
     97   1.1     ozaki 	krwlock_t		lle_lock;
     98   1.1     ozaki 
     99   1.1     ozaki #ifdef __NetBSD__
    100   1.1     ozaki #define	la_timer	lle_timer
    101   1.6     ozaki #define	ln_timer_ch	lle_timer
    102   1.6     ozaki #define	ln_expire	la_expire
    103   1.6     ozaki #define	ln_asked	la_asked
    104   1.6     ozaki #define	ln_hold		la_hold
    105   1.1     ozaki 	void			*la_opaque;	/* For tokenring */
    106   1.1     ozaki #endif
    107   1.1     ozaki };
    108   1.1     ozaki 
    109   1.2     ozaki 
    110   1.2     ozaki #if 0
    111  1.16     ozaki #define LLE_LOCK_TRACE(t, lle)	log(LOG_DEBUG, \
    112   1.5     ozaki 				    "%s:%d: LOCK(" #t "): lle=%p\n", \
    113   1.5     ozaki 				    __func__, __LINE__, (lle))
    114   1.2     ozaki #else
    115   1.5     ozaki #define LLE_LOCK_TRACE(t, lle)	do {} while (0)
    116   1.2     ozaki #endif
    117   1.2     ozaki 
    118   1.2     ozaki #define	LLE_WLOCK(lle)		do { \
    119   1.5     ozaki 					LLE_LOCK_TRACE(WL, (lle)); \
    120   1.2     ozaki 					rw_enter(&(lle)->lle_lock, RW_WRITER); \
    121   1.2     ozaki 				} while (0)
    122   1.2     ozaki #define	LLE_RLOCK(lle)		do { \
    123   1.5     ozaki 					LLE_LOCK_TRACE(RL, (lle)); \
    124   1.2     ozaki 					rw_enter(&(lle)->lle_lock, RW_READER); \
    125   1.2     ozaki 				} while (0)
    126   1.2     ozaki #define	LLE_WUNLOCK(lle)	do { \
    127   1.5     ozaki 					LLE_LOCK_TRACE(WU, (lle)); \
    128   1.2     ozaki 					rw_exit(&(lle)->lle_lock); \
    129   1.2     ozaki 				} while (0)
    130   1.2     ozaki #define	LLE_RUNLOCK(lle)	do { \
    131   1.5     ozaki 					LLE_LOCK_TRACE(RU, (lle)); \
    132   1.2     ozaki 					rw_exit(&(lle)->lle_lock); \
    133   1.2     ozaki 				} while (0)
    134   1.1     ozaki #define	LLE_DOWNGRADE(lle)	rw_downgrade(&(lle)->lle_lock)
    135   1.1     ozaki #define	LLE_TRY_UPGRADE(lle)	rw_tryupgrade(&(lle)->lle_lock)
    136   1.1     ozaki #ifdef __FreeBSD__
    137   1.1     ozaki #define	LLE_LOCK_INIT(lle)	rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK)
    138   1.1     ozaki #else /* XXX */
    139   1.1     ozaki #define	LLE_LOCK_INIT(lle)	rw_init(&(lle)->lle_lock)
    140   1.1     ozaki #endif
    141   1.1     ozaki #define	LLE_LOCK_DESTROY(lle)	rw_destroy(&(lle)->lle_lock)
    142   1.2     ozaki #define	LLE_WLOCK_ASSERT(lle)	KASSERT(rw_write_held(&(lle)->lle_lock))
    143   1.1     ozaki 
    144   1.1     ozaki #define LLE_IS_VALID(lle)	(((lle) != NULL) && ((lle) != (void *)-1))
    145   1.1     ozaki 
    146   1.5     ozaki #if 0
    147  1.17     ozaki #define LLE_REF_TRACE(t, lle, n) \
    148  1.17     ozaki 	log(LOG_DEBUG, "%s:%d: %p REF(" #t "): refcnt=%d\n", \
    149  1.17     ozaki 	    __func__, __LINE__, (lle), (n))
    150   1.5     ozaki #else
    151  1.17     ozaki #define LLE_REF_TRACE(t, lle, n)	do {} while (0)
    152   1.5     ozaki #endif
    153   1.5     ozaki 
    154   1.1     ozaki #define	LLE_ADDREF(lle) do {					\
    155   1.1     ozaki 	LLE_WLOCK_ASSERT(lle);					\
    156  1.17     ozaki 	LLE_REF_TRACE(ADD, (lle), (lle)->lle_refcnt);		\
    157  1.17     ozaki 	KASSERTMSG((lle)->lle_refcnt >= 0,			\
    158   1.1     ozaki 	    "negative refcnt %d on lle %p",			\
    159   1.1     ozaki 	    (lle)->lle_refcnt, (lle));				\
    160   1.1     ozaki 	(lle)->lle_refcnt++;					\
    161   1.1     ozaki } while (0)
    162   1.1     ozaki 
    163   1.1     ozaki #define	LLE_REMREF(lle)	do {					\
    164   1.1     ozaki 	LLE_WLOCK_ASSERT(lle);					\
    165  1.17     ozaki 	LLE_REF_TRACE(REM, (lle), (lle)->lle_refcnt);		\
    166  1.17     ozaki 	KASSERTMSG((lle)->lle_refcnt > 0,			\
    167   1.1     ozaki 	    "bogus refcnt %d on lle %p",			\
    168   1.1     ozaki 	    (lle)->lle_refcnt, (lle));				\
    169   1.1     ozaki 	(lle)->lle_refcnt--;					\
    170   1.5     ozaki 	if ((lle)->lle_refcnt == 0)				\
    171  1.17     ozaki 		LLE_REF_TRACE(ZERO, (lle), (lle)->lle_refcnt);	\
    172   1.1     ozaki } while (0)
    173   1.1     ozaki 
    174   1.1     ozaki #define	LLE_FREE_LOCKED(lle) do {				\
    175   1.7     ozaki 	if ((lle)->lle_refcnt == 1) {				\
    176   1.7     ozaki 		if ((lle)->lle_ll_free != NULL)			\
    177   1.7     ozaki 			(lle)->lle_ll_free(lle);		\
    178   1.1     ozaki 		(lle)->lle_free(lle);				\
    179   1.7     ozaki 	} else {						\
    180   1.1     ozaki 		LLE_REMREF(lle);				\
    181   1.1     ozaki 		LLE_WUNLOCK(lle);				\
    182   1.1     ozaki 	}							\
    183   1.1     ozaki 	/* guard against invalid refs */			\
    184   1.1     ozaki 	(lle) = NULL;						\
    185   1.1     ozaki } while (0)
    186   1.1     ozaki 
    187   1.1     ozaki #define	LLE_FREE(lle) do {					\
    188   1.1     ozaki 	LLE_WLOCK(lle);						\
    189   1.1     ozaki 	LLE_FREE_LOCKED(lle);					\
    190   1.1     ozaki } while (0)
    191   1.1     ozaki 
    192   1.1     ozaki 
    193   1.1     ozaki typedef	struct llentry *(llt_lookup_t)(struct lltable *, u_int flags,
    194   1.1     ozaki     const struct sockaddr *l3addr);
    195   1.1     ozaki typedef	struct llentry *(llt_create_t)(struct lltable *, u_int flags,
    196  1.13     ozaki     const struct sockaddr *l3addr, const struct rtentry *);
    197   1.1     ozaki typedef	int (llt_delete_t)(struct lltable *, u_int flags,
    198   1.1     ozaki     const struct sockaddr *l3addr);
    199   1.1     ozaki typedef void (llt_prefix_free_t)(struct lltable *,
    200   1.1     ozaki     const struct sockaddr *prefix, const struct sockaddr *mask, u_int flags);
    201   1.1     ozaki typedef int (llt_dump_entry_t)(struct lltable *, struct llentry *,
    202   1.9     ozaki     struct rt_walkarg *);
    203   1.1     ozaki typedef uint32_t (llt_hash_t)(const struct llentry *, uint32_t);
    204   1.1     ozaki typedef int (llt_match_prefix_t)(const struct sockaddr *,
    205   1.1     ozaki     const struct sockaddr *, u_int, struct llentry *);
    206   1.1     ozaki typedef void (llt_free_entry_t)(struct lltable *, struct llentry *);
    207   1.1     ozaki typedef void (llt_fill_sa_entry_t)(const struct llentry *, struct sockaddr *);
    208   1.1     ozaki typedef void (llt_free_tbl_t)(struct lltable *);
    209   1.1     ozaki typedef void (llt_link_entry_t)(struct lltable *, struct llentry *);
    210   1.1     ozaki typedef void (llt_unlink_entry_t)(struct llentry *);
    211   1.1     ozaki 
    212   1.1     ozaki typedef int (llt_foreach_cb_t)(struct lltable *, struct llentry *, void *);
    213   1.1     ozaki typedef int (llt_foreach_entry_t)(struct lltable *, llt_foreach_cb_t *, void *);
    214   1.1     ozaki 
    215   1.1     ozaki struct lltable {
    216   1.1     ozaki 	SLIST_ENTRY(lltable)	llt_link;
    217   1.1     ozaki 	int			llt_af;
    218   1.1     ozaki 	int			llt_hsize;
    219   1.1     ozaki 	struct llentries	*lle_head;
    220   1.6     ozaki 	unsigned int		llt_lle_count;
    221   1.1     ozaki 	struct ifnet		*llt_ifp;
    222   1.1     ozaki 
    223   1.1     ozaki 	llt_lookup_t		*llt_lookup;
    224   1.1     ozaki 	llt_create_t		*llt_create;
    225   1.1     ozaki 	llt_delete_t		*llt_delete;
    226   1.1     ozaki 	llt_prefix_free_t	*llt_prefix_free;
    227   1.1     ozaki 	llt_dump_entry_t	*llt_dump_entry;
    228   1.1     ozaki 	llt_hash_t		*llt_hash;
    229   1.1     ozaki 	llt_match_prefix_t	*llt_match_prefix;
    230   1.1     ozaki 	llt_free_entry_t	*llt_free_entry;
    231   1.1     ozaki 	llt_foreach_entry_t	*llt_foreach_entry;
    232   1.1     ozaki 	llt_link_entry_t	*llt_link_entry;
    233   1.1     ozaki 	llt_unlink_entry_t	*llt_unlink_entry;
    234   1.1     ozaki 	llt_fill_sa_entry_t	*llt_fill_sa_entry;
    235   1.1     ozaki 	llt_free_tbl_t		*llt_free_tbl;
    236  1.19      yamt #ifdef MBUFTRACE
    237  1.19      yamt 	struct mowner		*llt_mowner;
    238  1.19      yamt #endif
    239   1.1     ozaki };
    240   1.1     ozaki 
    241   1.1     ozaki MALLOC_DECLARE(M_LLTABLE);
    242   1.1     ozaki 
    243   1.1     ozaki /*
    244   1.1     ozaki  * LLentry flags
    245   1.1     ozaki  */
    246   1.1     ozaki #define	LLE_DELETED	0x0001	/* entry must be deleted */
    247   1.1     ozaki #define	LLE_STATIC	0x0002	/* entry is static */
    248   1.1     ozaki #define	LLE_IFADDR	0x0004	/* entry is interface addr */
    249   1.1     ozaki #define	LLE_VALID	0x0008	/* ll_addr is valid */
    250   1.1     ozaki #define	LLE_PUB		0x0020	/* publish entry ??? */
    251   1.1     ozaki #define	LLE_LINKED	0x0040	/* linked to lookup structure */
    252   1.1     ozaki /* LLE request flags */
    253   1.1     ozaki #define	LLE_EXCLUSIVE	0x2000	/* return lle xlocked  */
    254   1.1     ozaki 
    255   1.1     ozaki #define LLATBL_HASH(key, mask) \
    256   1.1     ozaki 	(((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask)
    257   1.1     ozaki 
    258   1.1     ozaki void lltableinit(void);
    259   1.1     ozaki 
    260   1.1     ozaki struct lltable *lltable_allocate_htbl(uint32_t hsize);
    261   1.1     ozaki void		lltable_free(struct lltable *);
    262   1.1     ozaki void		lltable_link(struct lltable *llt);
    263  1.11     ozaki void		lltable_prefix_free(const int, const struct sockaddr *,
    264  1.11     ozaki 		    const struct sockaddr *, const u_int);
    265   1.1     ozaki void		lltable_drain(int);
    266   1.6     ozaki void		lltable_purge_entries(struct lltable *);
    267  1.12     ozaki int		lltable_sysctl_dump(int, struct rt_walkarg *);
    268   1.9     ozaki int		lltable_dump_entry(struct lltable *, struct llentry *,
    269   1.9     ozaki 		    struct rt_walkarg *, struct sockaddr *);
    270   1.1     ozaki 
    271   1.1     ozaki size_t		llentry_free(struct llentry *);
    272   1.1     ozaki struct llentry  *llentry_alloc(struct ifnet *, struct lltable *,
    273   1.1     ozaki 		    struct sockaddr_storage *);
    274   1.1     ozaki 
    275  1.14     ozaki struct llentry  *llentry_pool_get(int);
    276  1.14     ozaki void		llentry_pool_put(struct llentry *);
    277  1.14     ozaki 
    278   1.1     ozaki /* helper functions */
    279   1.1     ozaki size_t lltable_drop_entry_queue(struct llentry *);
    280   1.1     ozaki 
    281   1.1     ozaki struct llentry *lltable_create_lle(struct lltable *llt, u_int flags,
    282   1.1     ozaki     const void *paddr);
    283   1.1     ozaki void lltable_link_entry(struct lltable *llt, struct llentry *lle);
    284   1.1     ozaki void lltable_unlink_entry(struct lltable *llt, struct llentry *lle);
    285  1.10     ozaki void lltable_free_entry(struct lltable *llt, struct llentry *lle);
    286   1.1     ozaki void lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa);
    287   1.1     ozaki struct ifnet *lltable_get_ifp(const struct lltable *llt);
    288   1.1     ozaki int lltable_get_af(const struct lltable *llt);
    289   1.1     ozaki 
    290  1.15  christos static __inline unsigned int
    291   1.6     ozaki lltable_get_entry_count(struct lltable *llt)
    292   1.6     ozaki {
    293   1.6     ozaki 	return llt->llt_lle_count;
    294   1.6     ozaki }
    295   1.6     ozaki 
    296   1.1     ozaki int lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f,
    297   1.1     ozaki     void *farg);
    298   1.1     ozaki /*
    299   1.1     ozaki  * Generic link layer address lookup function.
    300   1.1     ozaki  */
    301   1.1     ozaki static __inline struct llentry *
    302   1.1     ozaki lla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
    303   1.1     ozaki {
    304   1.1     ozaki 
    305   1.1     ozaki 	return (llt->llt_lookup(llt, flags, l3addr));
    306   1.1     ozaki }
    307   1.1     ozaki 
    308   1.1     ozaki static __inline struct llentry *
    309  1.13     ozaki lla_create(struct lltable *llt, u_int flags, const struct sockaddr *l3addr,
    310  1.13     ozaki     const struct rtentry *rt)
    311   1.1     ozaki {
    312   1.1     ozaki 
    313  1.13     ozaki 	return (llt->llt_create(llt, flags, l3addr, rt));
    314   1.1     ozaki }
    315   1.1     ozaki 
    316   1.1     ozaki static __inline int
    317   1.1     ozaki lla_delete(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
    318   1.1     ozaki {
    319   1.1     ozaki 
    320   1.1     ozaki 	return (llt->llt_delete(llt, flags, l3addr));
    321   1.1     ozaki }
    322   1.1     ozaki 
    323   1.1     ozaki 
    324   1.9     ozaki int lla_rt_output(const u_char, const int, const time_t,
    325   1.9     ozaki     struct rt_addrinfo *info, int);
    326   1.1     ozaki 
    327   1.1     ozaki #endif  /* _NET_IF_LLATBL_H_ */
    328