1 /** @file
2  * @brief Ethernet Bridge public header file
3  *
4  * Ethernet Bridges connect two or more Ethernet networks together and
5  * transparently forward packets from one network to the others as if
6  * they were part of the same network.
7  */
8 
9 /*
10  * Copyright (c) 2021 BayLibre SAS
11  * Copyright (c) 2024 Nordic Semiconductor
12  *
13  * SPDX-License-Identifier: Apache-2.0
14  */
15 
16 #ifndef ZEPHYR_INCLUDE_NET_ETHERNET_BRIDGE_H_
17 #define ZEPHYR_INCLUDE_NET_ETHERNET_BRIDGE_H_
18 
19 #include <zephyr/sys/slist.h>
20 #include <zephyr/sys/iterable_sections.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /**
27  * @brief Ethernet Bridging API
28  * @defgroup eth_bridge Ethernet Bridging API
29  * @since 2.7
30  * @version 0.8.0
31  * @ingroup networking
32  * @{
33  */
34 
35 /** @cond INTERNAL_HIDDEN */
36 
37 #if defined(CONFIG_NET_ETHERNET_BRIDGE)
38 #define NET_ETHERNET_BRIDGE_ETH_INTERFACE_COUNT CONFIG_NET_ETHERNET_BRIDGE_ETH_INTERFACE_COUNT
39 #else
40 #define NET_ETHERNET_BRIDGE_ETH_INTERFACE_COUNT 1
41 #endif
42 
43 struct eth_bridge_iface_context {
44 	/* Lock to protect access to interface array below */
45 	struct k_mutex lock;
46 
47 	/* The actual bridge virtual interface  */
48 	struct net_if *iface;
49 
50 	/* What Ethernet interfaces are bridged together */
51 	struct net_if *eth_iface[NET_ETHERNET_BRIDGE_ETH_INTERFACE_COUNT];
52 
53 	/* How many interfaces are bridged atm */
54 	size_t count;
55 
56 	/* Bridge instance id */
57 	int id;
58 
59 	/* Is the bridge interface initialized */
60 	bool is_init : 1;
61 
62 	/* Has user configured the bridge */
63 	bool is_setup : 1;
64 
65 	/* Is the interface enabled or not */
66 	bool status : 1;
67 };
68 
69 /** @endcond */
70 
71 /**
72  * @brief Add an Ethernet network interface to a bridge
73  *
74  * This adds a network interface to a bridge. The interface is then put
75  * into promiscuous mode. After more than one Ethernet interfaces are
76  * added to the bridge interface, the bridge interface is setup.
77  * After the setup is done, the bridge interface can be brought up so
78  * that it can start bridging L2 traffic.
79  *
80  * @param br A pointer to a bridge interface
81  * @param iface Interface to add
82  *
83  * @return 0 if OK, negative error code otherwise.
84  */
85 int eth_bridge_iface_add(struct net_if *br, struct net_if *iface);
86 
87 /**
88  * @brief Remove an Ethernet network interface from a bridge.
89  *
90  * If the bridge interface setup has only one Ethernet interface left
91  * after this function call, the bridge is disabled as it cannot bridge
92  * the L2 traffic any more. The bridge interface is left in UP state
93  * if this case.
94  *
95  * @param br A pointer to a bridge interface
96  * @param iface Interface to remove
97  *
98  * @return 0 if OK, negative error code otherwise.
99  */
100 int eth_bridge_iface_remove(struct net_if *br, struct net_if *iface);
101 
102 /**
103  * @brief Get bridge index according to pointer
104  *
105  * @param br Pointer to bridge instance
106  *
107  * @return Bridge index
108  */
109 int eth_bridge_get_index(struct net_if *br);
110 
111 /**
112  * @brief Get bridge instance according to index
113  *
114  * @param index Bridge instance index
115  *
116  * @return Pointer to bridge interface or NULL if not found.
117  */
118 struct net_if *eth_bridge_get_by_index(int index);
119 
120 /**
121  * @typedef eth_bridge_cb_t
122  * @brief Callback used while iterating over bridge instances
123  *
124  * @param br Pointer to bridge context instance
125  * @param user_data User supplied data
126  */
127 typedef void (*eth_bridge_cb_t)(struct eth_bridge_iface_context *br, void *user_data);
128 
129 /**
130  * @brief Go through all the bridge context instances in order to get
131  *        information about them. This is mainly useful in
132  *        net-shell to print data about currently active bridges.
133  *
134  * @param cb Callback to call for each bridge instance
135  * @param user_data User supplied data
136  */
137 void net_eth_bridge_foreach(eth_bridge_cb_t cb, void *user_data);
138 
139 /**
140  * @}
141  */
142 
143 #ifdef __cplusplus
144 }
145 #endif
146 
147 #endif /* ZEPHYR_INCLUDE_NET_ETHERNET_BRIDGE_H_ */
148