| 1 | /*	$OpenBSD: if_etherbridge.h,v 1.7 2025/11/21 04:44:26 dlg Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2018, 2021 David Gwynne <dlg@openbsd.org> |
| 5 | * |
| 6 | * Permission to use, copy, modify, and distribute this software for any |
| 7 | * purpose with or without fee is hereby granted, provided that the above |
| 8 | * copyright notice and this permission notice appear in all copies. |
| 9 | * |
| 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | */ |
| 18 | |
| 19 | #ifndef _NET_ETHERBRIDGE_H_ |
| 20 | #define _NET_ETHERBRIDGE_H_ |
| 21 | |
| 22 | #define ETHERBRIDGE_TABLE_BITS		8 |
| 23 | #define ETHERBRIDGE_TABLE_SIZE		(1U << ETHERBRIDGE_TABLE_BITS) |
| 24 | #define ETHERBRIDGE_TABLE_MASK		(ETHERBRIDGE_TABLE_SIZE - 1) |
| 25 | |
| 26 | struct etherbridge_ops { |
| 27 | 	int	 (*eb_op_port_eq)(void *, void *, void *); |
| 28 | 	void	*(*eb_op_port_take)(void *, void *); |
| 29 | 	void	 (*eb_op_port_rele)(void *, void *); |
| 30 | 	size_t	 (*eb_op_port_ifname)(void *, char *, size_t, void *); |
| 31 | 	void	 (*eb_op_port_sa)(void *, struct sockaddr_storage *, void *); |
| 32 | }; |
| 33 | |
| 34 | struct etherbridge; |
| 35 | |
| 36 | struct eb_entry { |
| 37 | 	SMR_TAILQ_ENTRY(eb_entry)	 ebe_lentry; |
| 38 | 	union { |
| 39 | 		RBT_ENTRY(eb_entry)	 _ebe_tentry; |
| 40 | 		TAILQ_ENTRY(eb_entry)	 _ebe_qentry; |
| 41 | 	}				 _ebe_entries; |
| 42 | #define ebe_tentry	_ebe_entries._ebe_tentry |
| 43 | #define ebe_qentry	_ebe_entries._ebe_qentry |
| 44 | |
| 45 | 	uint64_t			 ebe_addr; |
| 46 | 	void				*ebe_port; |
| 47 | 	uint16_t			 ebe_vs; /* secondary vid */ |
| 48 | 	unsigned int			 ebe_type; |
| 49 | #define EBE_DYNAMIC				0x0 |
| 50 | #define EBE_STATIC				0x1 |
| 51 | #define EBE_DEAD				0xdead |
| 52 | 	time_t				 ebe_created; |
| 53 | 	time_t				 ebe_age; |
| 54 | |
| 55 | 	struct etherbridge		*ebe_etherbridge; |
| 56 | 	struct smr_entry		 ebe_smr_entry; |
| 57 | }; |
| 58 | |
| 59 | #define etherbridge_port(_ebe)	((_ebe)->ebe_port) |
| 60 | #define etherbridge_vs(_ebe)	((_ebe)->ebe_vs) |
| 61 | |
| 62 | SMR_TAILQ_HEAD(eb_list, eb_entry); |
| 63 | RBT_HEAD(eb_tree, eb_entry); |
| 64 | TAILQ_HEAD(eb_queue, eb_entry); |
| 65 | |
| 66 | struct etherbridge { |
| 67 | 	const char			*eb_name; |
| 68 | 	const struct etherbridge_ops	*eb_ops; |
| 69 | 	void				*eb_cookie; |
| 70 | |
| 71 | 	struct mutex			 eb_lock; |
| 72 | 	unsigned int			 eb_num; |
| 73 | 	unsigned int			 eb_max; |
| 74 | 	int				 eb_max_age; /* seconds */ |
| 75 | 	struct timeout			 eb_tmo_age; |
| 76 | |
| 77 | 	struct eb_list			*eb_table; |
| 78 | 	struct eb_tree			 eb_tree; |
| 79 | |
| 80 | }; |
| 81 | |
| 82 | int	 etherbridge_init(struct etherbridge *, const char *, |
| 83 | 	 const struct etherbridge_ops *, void *); |
| 84 | int	 etherbridge_up(struct etherbridge *); |
| 85 | int	 etherbridge_down(struct etherbridge *); |
| 86 | void	 etherbridge_destroy(struct etherbridge *); |
| 87 | |
| 88 | void	 etherbridge_map(struct etherbridge *, void *, |
| 89 | 	 uint16_t, uint16_t, uint64_t); |
| 90 | void	 etherbridge_map_ea(struct etherbridge *, void *, |
| 91 | 	 uint16_t, uint16_t, const struct ether_addr *); |
| 92 | struct eb_entry * |
| 93 | 	 etherbridge_resolve_entry(struct etherbridge *, |
| 94 | 	 uint16_t, uint64_t); |
| 95 | void	*etherbridge_resolve(struct etherbridge *, uint16_t, uint64_t); |
| 96 | void	*etherbridge_resolve_ea(struct etherbridge *, |
| 97 | 	 uint16_t, const struct ether_addr *); |
| 98 | void	 etherbridge_detach_port(struct etherbridge *, void *); |
| 99 | void	 etherbridge_filter(struct etherbridge *, |
| 100 | 	 int (*)(struct etherbridge *, struct eb_entry *, void *), void *); |
| 101 | |
| 102 | /* ioctl support */ |
| 103 | int	 etherbridge_set_max(struct etherbridge *, struct ifbrparam *); |
| 104 | int	 etherbridge_get_max(struct etherbridge *, struct ifbrparam *); |
| 105 | int	 etherbridge_set_tmo(struct etherbridge *, struct ifbrparam *); |
| 106 | int	 etherbridge_get_tmo(struct etherbridge *, struct ifbrparam *); |
| 107 | int	 etherbridge_rtfind(struct etherbridge *, struct ifbaconf *); |
| 108 | int	 etherbridge_vareq(struct etherbridge *, struct ifbaconf *); |
| 109 | int	 etherbridge_add_addr(struct etherbridge *, void *, |
| 110 | 	 uint16_t, uint16_t, const struct ether_addr *, unsigned int); |
| 111 | int	 etherbridge_del_addr(struct etherbridge *, |
| 112 | 	 uint16_t, const struct ether_addr *); |
| 113 | void	 etherbridge_flush(struct etherbridge *, uint32_t); |
| 114 | |
| 115 | #endif /* _NET_ETHERBRIDGE_H_ */ |