bridge_mib_impl.c
Go to the documentation of this file.
1 /**
2  * @file bridge_mib_impl.c
3  * @brief Bridge MIB module implementation
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2019-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneSTP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL SNMP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "mibs/mib_common.h"
37 #include "mibs/bridge_mib_module.h"
38 #include "mibs/bridge_mib_impl.h"
39 #include "core/crypto.h"
40 #include "encoding/asn1.h"
41 #include "encoding/oid.h"
42 #include "stp/stp.h"
43 #include "stp/stp_mgmt.h"
44 #include "stp/stp_misc.h"
45 #include "rstp/rstp.h"
46 #include "rstp/rstp_mgmt.h"
47 #include "rstp/rstp_misc.h"
48 #include "debug.h"
49 
50 //Check TCP/IP stack configuration
51 #if (BRIDGE_MIB_SUPPORT == ENABLED)
52 
53 
54 /**
55  * @brief Bridge MIB module initialization
56  * @return Error code
57  **/
58 
60 {
61  //Debug message
62  TRACE_INFO("Initializing Bridge MIB base...\r\n");
63 
64  //Clear Bridge MIB base
65  memset(&bridgeMibBase, 0, sizeof(bridgeMibBase));
66 
67  //Type of bridging this bridge can perform
69  //Version of the Spanning Tree Protocol
71 
72  //Successful processing
73  return NO_ERROR;
74 }
75 
76 
77 /**
78  * @brief Attach STP bridge context
79  * @param[in] context Pointer to the STP bridge context
80  * @return Error code
81  **/
82 
84 {
85 #if (STP_SUPPORT == ENABLED)
86  //Make sure the STP bridge context is valid
87  if(context != NULL)
88  {
90  bridgeMibBase.interface = context->interface;
91  }
92  else
93  {
95  bridgeMibBase.interface = NULL;
96  }
97 
98  //Successful processing
99  return NO_ERROR;
100 #else
101  //Not implemented
102  return ERROR_NOT_IMPLEMENTED;
103 #endif
104 }
105 
106 
107 /**
108  * @brief Attach RSTP bridge context
109  * @param[in] context Pointer to the RSTP bridge context
110  * @return Error code
111  **/
112 
114 {
115 #if (RSTP_SUPPORT == ENABLED)
116  //Make sure the RSTP bridge context is valid
117  if(context != NULL)
118  {
120  bridgeMibBase.interface = context->interface;
121  }
122  else
123  {
125  bridgeMibBase.interface = NULL;
126  }
127 
128  //Successful processing
129  return NO_ERROR;
130 #else
131  //Not implemented
132  return ERROR_NOT_IMPLEMENTED;
133 #endif
134 }
135 
136 
137 /**
138  * @brief Get the number of ports
139  * @return Number of ports
140  **/
141 
143 {
144  uint_t numPorts;
145 
146  //Initialize value
147  numPorts = 0;
148 
149 #if (STP_SUPPORT == ENABLED)
150  //Valid STP bridge context?
151  if(bridgeMibBase.stpBridgeContext != NULL)
152  {
153  numPorts = bridgeMibBase.stpBridgeContext->numPorts;
154  }
155  else
156 #endif
157 #if (RSTP_SUPPORT == ENABLED)
158  //Valid RSTP bridge context?
159  if(bridgeMibBase.rstpBridgeContext != NULL)
160  {
161  numPorts = bridgeMibBase.rstpBridgeContext->numPorts;
162  }
163  else
164 #endif
165  //Invalid bridge context?
166  {
167  //Just for sanity
168  }
169 
170  //Return the number of ports
171  return numPorts;
172 }
173 
174 
175 /**
176  * @brief Get the port index that matches the specified port number
177  * @param[in] portNum Port number
178  * @return Port index
179  **/
180 
181 uint_t bridgeMibGetPortIndex(uint16_t portNum)
182 {
183  uint_t portIndex;
184 
185  //Initialize value
186  portIndex = 0;
187 
188 #if (STP_SUPPORT == ENABLED)
189  //Valid STP bridge context?
190  if(bridgeMibBase.stpBridgeContext != NULL)
191  {
193 
194  //Retrieve the port that matches the specified port number
196 
197  //Valid port number?
198  if(port != NULL)
199  {
200  //Get the port index
201  portIndex = port->portIndex;
202  }
203  }
204  else
205 #endif
206 #if (RSTP_SUPPORT == ENABLED)
207  //Valid RSTP bridge context?
208  if(bridgeMibBase.rstpBridgeContext != NULL)
209  {
211 
212  //Retrieve the port that matches the specified port number
214 
215  //Valid port number?
216  if(port != NULL)
217  {
218  //Get the port index
219  portIndex = port->portIndex;
220  }
221  }
222  else
223 #endif
224  //Invalid bridge context?
225  {
226  //Just for sanity
227  }
228 
229  //Return the port index
230  return portIndex;
231 }
232 
233 
234 /**
235  * @brief Get the port number that matches the specified port index
236  * @param[in] portIndex Port index
237  * @return Port number
238  **/
239 
240 uint16_t bridgeMibGetPortNum(uint16_t portIndex)
241 {
242  uint_t portNum;
243 
244  //Initialize value
245  portNum = 0;
246 
247 #if (STP_SUPPORT == ENABLED)
248  //Valid STP bridge context?
249  if(bridgeMibBase.stpBridgeContext != NULL)
250  {
251  //Valid port index?
252  if(portIndex >= 1 && portIndex <= bridgeMibBase.stpBridgeContext->numPorts)
253  {
255 
256  //Point to the port that matches the specified port index
257  port = &bridgeMibBase.stpBridgeContext->ports[portIndex - 1];
258  //Get the port number
259  portNum = port->portId & STP_PORT_NUM_MASK;
260  }
261  }
262  else
263 #endif
264 #if (RSTP_SUPPORT == ENABLED)
265  //Valid RSTP bridge context?
266  if(bridgeMibBase.rstpBridgeContext != NULL)
267  {
268  //Valid port index?
269  if(portIndex >= 1 && portIndex <= bridgeMibBase.rstpBridgeContext->numPorts)
270  {
272 
273  //Point to the port that matches the specified port index
274  port = &bridgeMibBase.rstpBridgeContext->ports[portIndex - 1];
275  //Get the port number
276  portNum = port->portId & RSTP_PORT_NUM_MASK;
277  }
278  }
279  else
280 #endif
281  //Invalid bridge context?
282  {
283  //Just for sanity
284  }
285 
286  //Return the port number
287  return portNum;
288 }
289 
290 #endif
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
OID (Object Identifier)
Bridge MIB module implementation.
error_t bridgeMibSetRstpBridgeContext(RstpBridgeContext *context)
Attach RSTP bridge context.
RSTP helper functions.
@ BRIDGE_MIB_BASE_TYPE_TRANSPARENT_ONLY
#define StpBridgePort
Definition: stp.h:40
Management of the RSTP bridge.
STP (Spanning Tree Protocol)
#define RstpBridgeContext
Definition: rstp.h:36
#define StpBridgeContext
Definition: stp.h:36
uint_t bridgeMibGetPortIndex(uint16_t portNum)
Get the port index that matches the specified port number.
error_t bridgeMibInit(void)
Bridge MIB module initialization.
error_t
Error codes.
Definition: error.h:43
int32_t dot1dStpProtocolSpecification
RSTP (Rapid Spanning Tree Protocol)
General definitions for cryptographic algorithms.
error_t bridgeMibSetStpBridgeContext(StpBridgeContext *context)
Attach STP bridge context.
#define RSTP_PORT_NUM_MASK
Definition: rstp_bpdu.h:44
#define TRACE_INFO(...)
Definition: debug.h:95
uint_t bridgeMibGetNumPorts(void)
Get the number of ports.
uint16_t bridgeMibGetPortNum(uint16_t portIndex)
Get the port number that matches the specified port index.
RstpBridgeContext * rstpBridgeContext
uint16_t port
Definition: dns_common.h:267
RstpBridgePort * rstpGetBridgePort(RstpBridgeContext *context, uint16_t portId)
Retrieve the port that matches the specified port number.
Definition: rstp_misc.c:187
Common definitions for MIB modules.
STP helper functions.
NetInterface * interface
#define STP_PORT_NUM_MASK
Definition: stp_bpdu.h:43
@ BRIDGE_MIB_PROTOCOL_SPEC_IEEE802_1D
StpBridgePort * stpGetBridgePort(StpBridgeContext *context, uint16_t portId)
Retrieve the port that matches the specified port number.
Definition: stp_misc.c:212
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
Bridge MIB module.
BridgeMibBase bridgeMibBase
Bridge MIB base.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
ASN.1 (Abstract Syntax Notation One)
#define RstpBridgePort
Definition: rstp.h:40
Management of the STP bridge.
StpBridgeContext * stpBridgeContext