1 /* $OpenBSD: if_etherbridge.h,v 1.3 2021/02/26 01:28:51 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 unsigned int ebe_type; 48 #define EBE_DYNAMIC 0x0 49 #define EBE_STATIC 0x1 50 #define EBE_DEAD 0xdead 51 time_t ebe_age; 52 53 struct etherbridge *ebe_etherbridge; 54 struct refcnt ebe_refs; 55 struct smr_entry ebe_smr_entry; 56 }; 57 58 SMR_TAILQ_HEAD(eb_list, eb_entry); 59 RBT_HEAD(eb_tree, eb_entry); 60 TAILQ_HEAD(eb_queue, eb_entry); 61 62 struct etherbridge { 63 const char *eb_name; 64 const struct etherbridge_ops *eb_ops; 65 void *eb_cookie; 66 67 struct mutex eb_lock; 68 unsigned int eb_num; 69 unsigned int eb_max; 70 int eb_max_age; /* seconds */ 71 struct timeout eb_tmo_age; 72 73 struct eb_list *eb_table; 74 struct eb_tree eb_tree; 75 76 }; 77 78 int etherbridge_init(struct etherbridge *, const char *, 79 const struct etherbridge_ops *, void *); 80 int etherbridge_up(struct etherbridge *); 81 int etherbridge_down(struct etherbridge *); 82 void etherbridge_destroy(struct etherbridge *); 83 84 void etherbridge_map(struct etherbridge *, void *, uint64_t); 85 void etherbridge_map_ea(struct etherbridge *, void *, 86 const struct ether_addr *); 87 void *etherbridge_resolve(struct etherbridge *, uint64_t); 88 void *etherbridge_resolve_ea(struct etherbridge *, 89 const struct ether_addr *); 90 void etherbridge_detach_port(struct etherbridge *, void *); 91 92 /* ioctl support */ 93 int etherbridge_set_max(struct etherbridge *, struct ifbrparam *); 94 int etherbridge_get_max(struct etherbridge *, struct ifbrparam *); 95 int etherbridge_set_tmo(struct etherbridge *, struct ifbrparam *); 96 int etherbridge_get_tmo(struct etherbridge *, struct ifbrparam *); 97 int etherbridge_rtfind(struct etherbridge *, struct ifbaconf *); 98 int etherbridge_add_addr(struct etherbridge *, void *, 99 const struct ether_addr *, unsigned int); 100 int etherbridge_del_addr(struct etherbridge *, const struct ether_addr *); 101 void etherbridge_flush(struct etherbridge *, uint32_t); 102 103 static inline unsigned int 104 etherbridge_num(const struct etherbridge *eb) 105 { 106 return (eb->eb_num); 107 } 108 109 #endif /* _NET_ETHERBRIDGE_H_ */ 110