bridge_mib_impl_tp.c
Go to the documentation of this file.
1 /**
2  * @file bridge_mib_impl.c
3  * @brief Bridge MIB module implementation (dot1dTp subtree)
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.0
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"
40 #include "core/crypto.h"
41 #include "encoding/asn1.h"
42 #include "encoding/oid.h"
43 #include "stp/stp.h"
44 #include "stp/stp_mgmt.h"
45 #include "rstp/rstp.h"
46 #include "rstp/rstp_mgmt.h"
47 #include "debug.h"
48 
49 //Check TCP/IP stack configuration
50 #if (BRIDGE_MIB_SUPPORT == ENABLED)
51 
52 
53 /**
54  * @brief Get dot1dTpLearnedEntryDiscards object value
55  * @param[in] object Pointer to the MIB object descriptor
56  * @param[in] oid Object identifier (object name and instance identifier)
57  * @param[in] oidLen Length of the OID, in bytes
58  * @param[out] value Object value
59  * @param[in,out] valueLen Length of the object value, in bytes
60  * @return Error code
61  **/
62 
64  size_t oidLen, MibVariant *value, size_t *valueLen)
65 {
66  //The total number of forwarding database entries that have been or would
67  //have been learned, but have been discarded due to a lack of storage space
68  //in the forwarding database
69  value->counter32 = 0;
70 
71  //Successful processing
72  return NO_ERROR;
73 }
74 
75 
76 /**
77  * @brief Set dot1dTpAgingTime object value
78  * @param[in] object Pointer to the MIB object descriptor
79  * @param[in] oid Object identifier (object name and instance identifier)
80  * @param[in] oidLen Length of the OID, in bytes
81  * @param[in] value Object value
82  * @param[in] valueLen Length of the object value, in bytes
83  * @param[in] commit This flag tells whether the changes shall be committed
84  * to the MIB base
85  * @return Error code
86  **/
87 
88 error_t bridgeMibSetDot1dTpAgingTime(const MibObject *object, const uint8_t *oid,
89  size_t oidLen, const MibVariant *value, size_t valueLen, bool_t commit)
90 {
91 #if (BRIDGE_MIB_SET_SUPPORT == ENABLED)
92  error_t error;
93 
94  //Ensure that the supplied value is valid
95  if(value->integer >= 0)
96  {
97 #if (STP_SUPPORT == ENABLED)
98  //Valid STP bridge context?
100  {
101  //This object specifies the timeout period in seconds for aging out
102  //dynamically-learned forwarding information
104  value->integer, commit);
105  }
106  else
107 #endif
108 #if (RSTP_SUPPORT == ENABLED)
109  //Valid RSTP bridge context?
110  if(bridgeMibBase.rstpBridgeContext != NULL)
111  {
112  //This object specifies the timeout period in seconds for aging out
113  //dynamically-learned forwarding information
115  value->integer, commit);
116  }
117  else
118 #endif
119  //Invalid bridge context?
120  {
121  //Report an error
122  error = ERROR_WRITE_FAILED;
123  }
124  }
125  else
126  {
127  //Report an error
128  error = ERROR_WRONG_VALUE;
129  }
130 
131  //Return status code
132  return error;
133 #else
134  //SET operation is not supported
135  return ERROR_WRITE_FAILED;
136 #endif
137 }
138 
139 
140 /**
141  * @brief Get dot1dTpAgingTime object value
142  * @param[in] object Pointer to the MIB object descriptor
143  * @param[in] oid Object identifier (object name and instance identifier)
144  * @param[in] oidLen Length of the OID, in bytes
145  * @param[out] value Object value
146  * @param[in,out] valueLen Length of the object value, in bytes
147  * @return Error code
148  **/
149 
150 error_t bridgeMibGetDot1dTpAgingTime(const MibObject *object, const uint8_t *oid,
151  size_t oidLen, MibVariant *value, size_t *valueLen)
152 {
153  error_t error;
154  uint_t ageingTime;
155 
156  //Initialize object value
157  ageingTime = 0;
158 
159 #if (STP_SUPPORT == ENABLED)
160  //Valid STP bridge context?
161  if(bridgeMibBase.stpBridgeContext != NULL)
162  {
163  //This object specifies the timeout period in seconds for aging out
164  //dynamically-learned forwarding information
166  &ageingTime);
167  }
168  else
169 #endif
170 #if (RSTP_SUPPORT == ENABLED)
171  //Valid RSTP bridge context?
172  if(bridgeMibBase.rstpBridgeContext != NULL)
173  {
174  //This object specifies the timeout period in seconds for aging out
175  //dynamically-learned forwarding information
177  &ageingTime);
178  }
179  else
180 #endif
181  //Invalid bridge context?
182  {
183  //Report an error
184  error = ERROR_READ_FAILED;
185  }
186 
187  //Check status code
188  if(!error)
189  {
190  //Return the value of the object
191  value->integer = ageingTime;
192  }
193 
194  //Return status code
195  return error;
196 }
197 
198 
199 /**
200  * @brief Get dot1dTpFdbEntry object value
201  * @param[in] object Pointer to the MIB object descriptor
202  * @param[in] oid Object identifier (object name and instance identifier)
203  * @param[in] oidLen Length of the OID, in bytes
204  * @param[out] value Object value
205  * @param[in,out] valueLen Length of the object value, in bytes
206  * @return Error code
207  **/
208 
209 error_t bridgeMibGetDot1dTpFdbEntry(const MibObject *object, const uint8_t *oid,
210  size_t oidLen, MibVariant *value, size_t *valueLen)
211 {
212  error_t error;
213  uint_t i;
214  size_t n;
215  SwitchFdbEntry entry;
216  MacAddr dot1dTpFdbAddress;
217  NetInterface *interface;
218 
219  //Make sure the network interface is valid
220  if(bridgeMibBase.interface == NULL)
221  return ERROR_READ_FAILED;
222 
223  //Point to the underlying interface
224  interface = bridgeMibBase.interface;
225 
226  //Point to the instance identifier
227  n = object->oidLen;
228 
229  //dot1dTpFdbAddress is used as instance identifier
230  error = mibDecodeMacAddr(oid, oidLen, &n, &dot1dTpFdbAddress);
231  //Invalid instance identifier?
232  if(error)
233  return error;
234 
235  //Sanity check
236  if(n != oidLen)
238 
239  //Search the dynamic MAC table for the specified address
240  for(i = 0; !error; i++)
241  {
242  //Get the next entry
243  error = interface->switchDriver->getDynamicFdbEntry(interface, i, &entry);
244 
245  //Check status code
246  if(error == NO_ERROR)
247  {
248  //Check whether the current entry matches the specified address
249  if(macCompAddr(&entry.macAddr, &dot1dTpFdbAddress))
250  break;
251  }
252  else if(error == ERROR_INVALID_ENTRY)
253  {
254  //Skip current entry
255  error = NO_ERROR;
256  }
257  else
258  {
259  //Exit immediately
260  }
261  }
262 
263  //No matching entry?
264  if(error)
266 
267  //dot1dTpFdbAddress object?
268  if(!strcmp(object->name, "dot1dTpFdbAddress"))
269  {
270  //Make sure the buffer is large enough to hold the entire object
271  if(*valueLen >= sizeof(MacAddr))
272  {
273  //This object specifies a unicast MAC address for which the bridge has
274  //forwarding and/or filtering information
275  macCopyAddr(value->octetString, &entry.macAddr);
276 
277  //A MAC address shall be encoded as six octets
278  *valueLen = sizeof(MacAddr);
279  }
280  else
281  {
282  //Report an error
283  error = ERROR_BUFFER_OVERFLOW;
284  }
285  }
286  //dot1dTpFdbPort object?
287  else if(!strcmp(object->name, "dot1dTpFdbPort"))
288  {
289  //This object indicates the port number of the port on which a frame
290  //having a source address equal to the value of the corresponding
291  //instance of dot1dTpFdbAddress has been seen
292  value->integer = entry.srcPort;
293  }
294  //dot1dTpFdbStatus object?
295  else if(!strcmp(object->name, "dot1dTpFdbStatus"))
296  {
297  //This object indicates the status of this entry
299  }
300  //Unknown object?
301  else
302  {
303  //The specified object does not exist
304  error = ERROR_OBJECT_NOT_FOUND;
305  }
306 
307  //Return status code
308  return error;
309 }
310 
311 
312 /**
313  * @brief Get next dot1dTpFdbEntry object
314  * @param[in] object Pointer to the MIB object descriptor
315  * @param[in] oid Object identifier
316  * @param[in] oidLen Length of the OID, in bytes
317  * @param[out] nextOid OID of the next object in the MIB
318  * @param[out] nextOidLen Length of the next object identifier, in bytes
319  * @return Error code
320  **/
321 
322 error_t bridgeMibGetNextDot1dTpFdbEntry(const MibObject *object, const uint8_t *oid,
323  size_t oidLen, uint8_t *nextOid, size_t *nextOidLen)
324 {
325  error_t error;
326  uint_t i;
327  size_t n;
328  MacAddr macAddr;
329  SwitchFdbEntry entry;
330  NetInterface *interface;
331 
332  //Initialize variable
333  macAddr = MAC_UNSPECIFIED_ADDR;
334 
335  //Make sure the network interface is valid
336  if(bridgeMibBase.interface == NULL)
337  return ERROR_OBJECT_NOT_FOUND;
338 
339  //Point to the underlying interface
340  interface = bridgeMibBase.interface;
341 
342  //Make sure the buffer is large enough to hold the OID prefix
343  if(*nextOidLen < object->oidLen)
344  return ERROR_BUFFER_OVERFLOW;
345 
346  //Copy OID prefix
347  osMemcpy(nextOid, object->oid, object->oidLen);
348 
349  //Initialize status code
350  error = NO_ERROR;
351 
352  //Loop through the dynamic MAC table
353  for(i = 0; !error; i++)
354  {
355  //Get the next entry
356  error = interface->switchDriver->getDynamicFdbEntry(interface, i, &entry);
357 
358  //Check status code
359  if(error == NO_ERROR)
360  {
361  //Append the instance identifier to the OID prefix
362  n = object->oidLen;
363 
364  //dot1dTpFdbAddress is used as instance identifier
365  error = mibEncodeMacAddr(nextOid, *nextOidLen, &n, &entry.macAddr);
366  //Any error to report?
367  if(error)
368  return error;
369 
370  //Check whether the resulting object identifier lexicographically
371  //follows the specified OID
372  if(oidComp(nextOid, n, oid, oidLen) > 0)
373  {
374  //Save the closest object identifier that follows the specified
375  //OID in lexicographic order
376  if(mibCompMacAddr(&macAddr, &MAC_UNSPECIFIED_ADDR) == 0 ||
377  mibCompMacAddr(&entry.macAddr, &macAddr) < 0)
378  {
379  macAddr = entry.macAddr;
380  }
381  }
382  }
383  else if(error == ERROR_INVALID_ENTRY)
384  {
385  //Skip current entry
386  error = NO_ERROR;
387  }
388  else
389  {
390  //Exit immediately
391  }
392  }
393 
394  //The specified OID does not lexicographically precede the name
395  //of some object?
396  if(mibCompMacAddr(&macAddr, &MAC_UNSPECIFIED_ADDR) == 0)
397  return ERROR_OBJECT_NOT_FOUND;
398 
399  //Append the instance identifier to the OID prefix
400  n = object->oidLen;
401 
402  //dot1dTpFdbAddress is used as instance identifier
403  error = mibEncodeMacAddr(nextOid, *nextOidLen, &n, &macAddr);
404  //Any error to report?
405  if(error)
406  return error;
407 
408  //Save the length of the resulting object identifier
409  *nextOidLen = n;
410  //Next object found
411  return NO_ERROR;
412 }
413 
414 
415 /**
416  * @brief Get dot1dTpPortEntry object value
417  * @param[in] object Pointer to the MIB object descriptor
418  * @param[in] oid Object identifier (object name and instance identifier)
419  * @param[in] oidLen Length of the OID, in bytes
420  * @param[out] value Object value
421  * @param[in,out] valueLen Length of the object value, in bytes
422  * @return Error code
423  **/
424 
425 error_t bridgeMibGetDot1dTpPortEntry(const MibObject *object, const uint8_t *oid,
426  size_t oidLen, MibVariant *value, size_t *valueLen)
427 {
428  error_t error;
429  size_t n;
430  uint16_t dot1dTpPort;
431 
432  //Point to the instance identifier
433  n = object->oidLen;
434 
435  //dot1dTpPort is used as instance identifier
436  error = mibDecodePort(oid, oidLen, &n, &dot1dTpPort);
437  //Invalid instance identifier?
438  if(error)
439  return error;
440 
441  //Sanity check
442  if(n != oidLen)
444 
445  //Invalid port number?
446  if(bridgeMibGetPortIndex(dot1dTpPort) == 0)
448 
449  //dot1dTpPort object?
450  if(!strcmp(object->name, "dot1dTpPort"))
451  {
452  //This object indicates the port number of the port for which this entry
453  //contains transparent bridging management information
454  value->integer = dot1dTpPort;
455  }
456  //dot1dTpPortMaxInfo object?
457  else if(!strcmp(object->name, "dot1dTpPortMaxInfo"))
458  {
459  //The maximum size of the INFO (non-MAC) field that this port will
460  //receive or transmit
461  value->integer = ETH_MTU;
462  }
463  //dot1dTpPortInFrames object?
464  else if(!strcmp(object->name, "dot1dTpPortInFrames"))
465  {
466  //The number of frames that have been received by this port from its
467  //segment
468  value->counter32 = 0;
469  }
470  //dot1dTpPortOutFrames object?
471  else if(!strcmp(object->name, "dot1dTpPortOutFrames"))
472  {
473  //The number of frames that have been transmitted by this port to its
474  //segment
475  value->counter32 = 0;
476  }
477  //dot1dTpPortInDiscards object?
478  else if(!strcmp(object->name, "dot1dTpPortInDiscards"))
479  {
480  //Count of received valid frames that were discarded by forwarding
481  //process
482  value->counter32 = 0;
483  }
484  //Unknown object?
485  else
486  {
487  //The specified object does not exist
488  error = ERROR_OBJECT_NOT_FOUND;
489  }
490 
491  //Return status code
492  return error;
493 }
494 
495 
496 /**
497  * @brief Get next dot1dTpPortEntry object
498  * @param[in] object Pointer to the MIB object descriptor
499  * @param[in] oid Object identifier
500  * @param[in] oidLen Length of the OID, in bytes
501  * @param[out] nextOid OID of the next object in the MIB
502  * @param[out] nextOidLen Length of the next object identifier, in bytes
503  * @return Error code
504  **/
505 
507  size_t oidLen, uint8_t *nextOid, size_t *nextOidLen)
508 {
509  error_t error;
510  uint_t i;
511  size_t n;
512  uint_t numPorts;
513  uint16_t portNum;
514  uint16_t curPortNum;
515 
516  //Initialize variable
517  portNum = 0;
518 
519  //Make sure the buffer is large enough to hold the OID prefix
520  if(*nextOidLen < object->oidLen)
521  return ERROR_BUFFER_OVERFLOW;
522 
523  //Copy OID prefix
524  osMemcpy(nextOid, object->oid, object->oidLen);
525 
526  //Retrieve the number of ports
527  numPorts = bridgeMibGetNumPorts();
528 
529  //Loop through the ports of the bridge
530  for(i = 1; i <= numPorts; i++)
531  {
532  //Retrieve the port number associated with the current port
533  curPortNum = bridgeMibGetPortNum(i);
534 
535  //Append the instance identifier to the OID prefix
536  n = object->oidLen;
537 
538  //dot1dTpPort is used as instance identifier
539  error = mibEncodeIndex(nextOid, *nextOidLen, &n, curPortNum);
540  //Any error to report?
541  if(error)
542  return error;
543 
544  //Check whether the resulting object identifier lexicographically
545  //follows the specified OID
546  if(oidComp(nextOid, n, oid, oidLen) > 0)
547  {
548  //Save the closest object identifier that follows the specified
549  //OID in lexicographic order
550  if(portNum == 0 || curPortNum < portNum)
551  {
552  portNum = curPortNum;
553  }
554  }
555  }
556 
557  //The specified OID does not lexicographically precede the name
558  //of some object?
559  if(portNum == 0)
560  return ERROR_OBJECT_NOT_FOUND;
561 
562  //Append the instance identifier to the OID prefix
563  n = object->oidLen;
564 
565  //dot1dTpPort is used as instance identifier
566  error = mibEncodeIndex(nextOid, *nextOidLen, &n, portNum);
567  //Any error to report?
568  if(error)
569  return error;
570 
571  //Save the length of the resulting object identifier
572  *nextOidLen = n;
573  //Next object found
574  return NO_ERROR;
575 }
576 
577 #endif
ASN.1 (Abstract Syntax Notation One)
uint16_t bridgeMibGetPortNum(uint16_t portIndex)
Get the port number that matches the specified port index.
uint_t bridgeMibGetNumPorts(void)
Get the number of ports.
uint_t bridgeMibGetPortIndex(uint16_t portNum)
Get the port index that matches the specified port number.
Bridge MIB module implementation.
error_t bridgeMibSetDot1dTpAgingTime(const MibObject *object, const uint8_t *oid, size_t oidLen, const MibVariant *value, size_t valueLen, bool_t commit)
Set dot1dTpAgingTime object value.
error_t bridgeMibGetDot1dTpAgingTime(const MibObject *object, const uint8_t *oid, size_t oidLen, MibVariant *value, size_t *valueLen)
Get dot1dTpAgingTime object value.
error_t bridgeMibGetNextDot1dTpPortEntry(const MibObject *object, const uint8_t *oid, size_t oidLen, uint8_t *nextOid, size_t *nextOidLen)
Get next dot1dTpPortEntry object.
error_t bridgeMibGetNextDot1dTpFdbEntry(const MibObject *object, const uint8_t *oid, size_t oidLen, uint8_t *nextOid, size_t *nextOidLen)
Get next dot1dTpFdbEntry object.
error_t bridgeMibGetDot1dTpFdbEntry(const MibObject *object, const uint8_t *oid, size_t oidLen, MibVariant *value, size_t *valueLen)
Get dot1dTpFdbEntry object value.
error_t bridgeMibGetDot1dTpLearnedEntryDiscards(const MibObject *object, const uint8_t *oid, size_t oidLen, MibVariant *value, size_t *valueLen)
Get dot1dTpLearnedEntryDiscards object value.
error_t bridgeMibGetDot1dTpPortEntry(const MibObject *object, const uint8_t *oid, size_t oidLen, MibVariant *value, size_t *valueLen)
Get dot1dTpPortEntry object value.
BridgeMibBase bridgeMibBase
Bridge MIB base.
Bridge MIB module.
@ BRIDGE_MIB_FDB_STATUS_LEARNED
unsigned int uint_t
Definition: compiler_port.h:50
int bool_t
Definition: compiler_port.h:53
General definitions for cryptographic algorithms.
Debugging facilities.
uint8_t n
error_t
Error codes.
Definition: error.h:43
@ ERROR_WRITE_FAILED
Definition: error.h:221
@ ERROR_OBJECT_NOT_FOUND
Definition: error.h:255
@ ERROR_INSTANCE_NOT_FOUND
Definition: error.h:256
@ ERROR_INVALID_ENTRY
Definition: error.h:288
@ ERROR_WRONG_VALUE
Definition: error.h:123
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_BUFFER_OVERFLOW
Definition: error.h:142
@ ERROR_READ_FAILED
Definition: error.h:222
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:53
#define ETH_MTU
Definition: ethernet.h:116
#define macCompAddr(macAddr1, macAddr2)
Definition: ethernet.h:130
#define macCopyAddr(destMacAddr, srcMacAddr)
Definition: ethernet.h:127
MacAddr
Definition: ethernet.h:195
uint8_t oid[]
Definition: lldp_tlv.h:300
uint8_t oidLen
Definition: lldp_tlv.h:299
error_t mibDecodeMacAddr(const uint8_t *oid, size_t oidLen, size_t *pos, MacAddr *macAddr)
Decode instance identifier (MAC address)
Definition: mib_common.c:558
error_t mibDecodePort(const uint8_t *oid, size_t oidLen, size_t *pos, uint16_t *port)
Decode instance identifier (port number)
Definition: mib_common.c:495
int_t mibCompMacAddr(const MacAddr *macAddr1, const MacAddr *macAddr2)
Compare MAC addresses.
Definition: mib_common.c:954
error_t mibEncodeIndex(uint8_t *oid, size_t maxOidLen, size_t *pos, uint_t index)
Encode instance identifier (index)
Definition: mib_common.c:47
error_t mibEncodeMacAddr(uint8_t *oid, size_t maxOidLen, size_t *pos, const MacAddr *macAddr)
Encode instance identifier (MAC address)
Definition: mib_common.c:528
Common definitions for MIB modules.
#define MibObject
Definition: mib_common.h:46
MibVariant
Definition: mib_common.h:196
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
int_t oidComp(const uint8_t *oid1, size_t oidLen1, const uint8_t *oid2, size_t oidLen2)
Compare object identifiers.
Definition: oid.c:103
OID (Object Identifier)
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
RSTP (Rapid Spanning Tree Protocol)
error_t rstpMgmtGetAgeingTime(RstpBridgeContext *context, uint_t *value)
Get the assigned value of the Ageing Time parameter.
Definition: rstp_mgmt.c:604
error_t rstpMgmtSetAgeingTime(RstpBridgeContext *context, uint_t value, bool_t commit)
Set Ageing Time parameter.
Definition: rstp_mgmt.c:391
Management of the RSTP bridge.
STP (Spanning Tree Protocol)
error_t stpMgmtSetAgeingTime(StpBridgeContext *context, uint_t value, bool_t commit)
Set Ageing Time parameter.
Definition: stp_mgmt.c:314
error_t stpMgmtGetAgeingTime(StpBridgeContext *context, uint_t *value)
Get the assigned value of the Ageing Time parameter.
Definition: stp_mgmt.c:502
Management of the STP bridge.
StpBridgeContext * stpBridgeContext
RstpBridgeContext * rstpBridgeContext
NetInterface * interface
Forwarding database entry.
Definition: nic.h:149
MacAddr macAddr
Definition: nic.h:150
uint8_t srcPort
Definition: nic.h:151
uint8_t value[]
Definition: tcp.h:369