mib2_impl_if.c
Go to the documentation of this file.
1 /**
2  * @file mib2_impl_if.c
3  * @brief MIB-II module implementation (Interface group)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP 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.6.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/mib2_module.h"
38 #include "mibs/mib2_impl.h"
39 #include "mibs/mib2_impl_if.h"
40 #include "core/crypto.h"
41 #include "encoding/asn1.h"
42 #include "encoding/oid.h"
43 #include "debug.h"
44 
45 //Check TCP/IP stack configuration
46 #if (MIB2_SUPPORT == ENABLED && MIB2_IF_GROUP_SUPPORT == ENABLED)
47 
48 
49 /**
50  * @brief Get ifNumber object value
51  * @param[in] object Pointer to the MIB object descriptor
52  * @param[in] oid Object identifier (object name and instance identifier)
53  * @param[in] oidLen Length of the OID, in bytes
54  * @param[out] value Object value
55  * @param[in,out] valueLen Length of the object value, in bytes
56  * @return Error code
57  **/
58 
59 error_t mib2GetIfNumber(const MibObject *object, const uint8_t *oid,
60  size_t oidLen, MibVariant *value, size_t *valueLen)
61 {
62  NetContext *context;
63 
64  //Point to the TCP/IP stack context
65  context = netGetDefaultContext();
66 
67  //Number of network interfaces present on this system
68  value->integer = context->numInterfaces;
69 
70  //Return status code
71  return NO_ERROR;
72 }
73 
74 
75 /**
76  * @brief Set ifEntry object value
77  * @param[in] object Pointer to the MIB object descriptor
78  * @param[in] oid Object identifier (object name and instance identifier)
79  * @param[in] oidLen Length of the OID, in bytes
80  * @param[in] value Object value
81  * @param[in] valueLen Length of the object value, in bytes
82  * @param[in] commit This flag tells whether the changes shall be committed
83  * to the MIB base
84  * @return Error code
85  **/
86 
87 error_t mib2SetIfEntry(const MibObject *object, const uint8_t *oid,
88  size_t oidLen, const MibVariant *value, size_t valueLen, bool_t commit)
89 {
90  //Not implemented
91  return ERROR_WRITE_FAILED;
92 }
93 
94 
95 /**
96  * @brief Get ifEntry object value
97  * @param[in] object Pointer to the MIB object descriptor
98  * @param[in] oid Object identifier (object name and instance identifier)
99  * @param[in] oidLen Length of the OID, in bytes
100  * @param[out] value Object value
101  * @param[in,out] valueLen Length of the object value, in bytes
102  * @return Error code
103  **/
104 
105 error_t mib2GetIfEntry(const MibObject *object, const uint8_t *oid,
106  size_t oidLen, MibVariant *value, size_t *valueLen)
107 {
108  error_t error;
109  size_t n;
110  uint_t index;
111  NetContext *context;
112  NetInterface *interface;
113  NetInterface *physicalInterface;
114 
115  //Point to the TCP/IP stack context
116  context = netGetDefaultContext();
117 
118  //Point to the instance identifier
119  n = object->oidLen;
120 
121  //ifIndex is used as instance identifier
122  error = mibDecodeIndex(oid, oidLen, &n, &index);
123  //Invalid instance identifier?
124  if(error)
125  return error;
126 
127  //Sanity check
128  if(n != oidLen)
130 
131  //Check index range
132  if(index < 1 || index > context->numInterfaces)
134 
135  //Point to the underlying interface
136  interface = &context->interfaces[index - 1];
137  //Point to the physical interface
138  physicalInterface = nicGetPhysicalInterface(interface);
139 
140  //ifIndex object?
141  if(osStrcmp(object->name, "ifIndex") == 0)
142  {
143  //Get object value
144  value->integer = index;
145  }
146  //ifDescr object?
147  else if(osStrcmp(object->name, "ifDescr") == 0)
148  {
149  //Retrieve the length of the interface name
150  n = osStrlen(interface->name);
151 
152  //Make sure the buffer is large enough to hold the entire object
153  if(*valueLen >= n)
154  {
155  //Copy object value
156  osMemcpy(value->octetString, interface->name, n);
157  //Return object length
158  *valueLen = n;
159  }
160  else
161  {
162  //Report an error
163  error = ERROR_BUFFER_OVERFLOW;
164  }
165  }
166  //ifType object?
167  else if(osStrcmp(object->name, "ifType") == 0)
168  {
169 #if (ETH_VLAN_SUPPORT == ENABLED)
170  //VLAN interface?
171  if(interface->vlanId != 0)
172  {
173  //Layer 2 virtual LAN using 802.1Q
174  value->integer = MIB2_IF_TYPE_L2_VLAN;
175  }
176  else
177 #endif
178  {
179  //Sanity check
180  if(physicalInterface->nicDriver != NULL)
181  {
182  //Get interface type
183  switch(physicalInterface->nicDriver->type)
184  {
185  //Ethernet interface
186  case NIC_TYPE_ETHERNET:
188  break;
189 
190  //PPP interface
191  case NIC_TYPE_PPP:
192  value->integer = MIB2_IF_TYPE_PPP;
193  break;
194 
195  //IEEE 802.15.4 WPAN interface
196  case NIC_TYPE_6LOWPAN:
198  break;
199 
200  //Unknown interface type
201  default:
202  value->integer = MIB2_IF_TYPE_OTHER;
203  break;
204  }
205  }
206  else
207  {
208  //Unknown interface type
209  value->integer = MIB2_IF_TYPE_OTHER;
210  }
211  }
212  }
213  //ifMtu object?
214  else if(osStrcmp(object->name, "ifMtu") == 0)
215  {
216  //Get interface MTU
217  if(physicalInterface->nicDriver != NULL)
218  {
219  value->integer = physicalInterface->nicDriver->mtu;
220  }
221  else
222  {
223  value->integer = 0;
224  }
225  }
226  //ifSpeed object?
227  else if(osStrcmp(object->name, "ifSpeed") == 0)
228  {
229  //Get interface's current bandwidth
230  value->gauge32 = interface->linkSpeed;
231  }
232 #if (ETH_SUPPORT == ENABLED)
233  //ifPhysAddress object?
234  else if(osStrcmp(object->name, "ifPhysAddress") == 0)
235  {
236  NetInterface *logicalInterface;
237 
238  //Point to the logical interface
239  logicalInterface = nicGetLogicalInterface(interface);
240 
241  //Make sure the buffer is large enough to hold the entire object
242  if(*valueLen >= MIB2_PHYS_ADDRESS_SIZE)
243  {
244  //Copy object value
245  macCopyAddr(value->octetString, &logicalInterface->macAddr);
246  //Return object length
247  *valueLen = MIB2_PHYS_ADDRESS_SIZE;
248  }
249  else
250  {
251  //Report an error
252  error = ERROR_BUFFER_OVERFLOW;
253  }
254  }
255 #endif
256  //ifAdminStatus object?
257  else if(osStrcmp(object->name, "ifAdminStatus") == 0)
258  {
259  //Check whether the interface is enabled for operation
260  if(physicalInterface->nicDriver != NULL)
261  {
262  value->integer = MIB2_IF_ADMIN_STATUS_UP;
263  }
264  else
265  {
266  value->integer = MIB2_IF_ADMIN_STATUS_DOWN;
267  }
268  }
269  //ifOperStatus object?
270  else if(osStrcmp(object->name, "ifOperStatus") == 0)
271  {
272  //Get the current operational state of the interface
273  if(interface->linkState)
274  {
275  value->integer = MIB2_IF_OPER_STATUS_UP;
276  }
277  else
278  {
279  value->integer = MIB2_IF_OPER_STATUS_DOWN;
280  }
281  }
282  //ifLastChange object?
283  else if(osStrcmp(object->name, "ifLastChange") == 0)
284  {
285  //Get object value
286  value->timeTicks = interface->ifStats.lastChange;
287  }
288  //ifInOctets object?
289  else if(osStrcmp(object->name, "ifInOctets") == 0)
290  {
291  //Get object value
292  value->counter32 = (uint32_t) interface->ifStats.inOctets;
293  }
294  //ifInUcastPkts object?
295  else if(osStrcmp(object->name, "ifInUcastPkts") == 0)
296  {
297  //Get object value
298  value->counter32 = (uint32_t) interface->ifStats.inUcastPkts;
299  }
300  //ifInNUcastPkts object?
301  else if(osStrcmp(object->name, "ifInNUcastPkts") == 0)
302  {
303  //Get object value
304  value->counter32 = interface->ifStats.inNUcastPkts;
305  }
306  //ifInDiscards object?
307  else if(osStrcmp(object->name, "ifInDiscards") == 0)
308  {
309  //Get object value
310  value->counter32 = interface->ifStats.inDiscards;
311  }
312  //ifInErrors object?
313  else if(osStrcmp(object->name, "ifInErrors") == 0)
314  {
315  //Get object value
316  value->counter32 = interface->ifStats.inErrors;
317  }
318  //ifInUnknownProtos object?
319  else if(osStrcmp(object->name, "ifInUnknownProtos") == 0)
320  {
321  //Get object value
322  value->counter32 = interface->ifStats.inUnknownProtos;
323  }
324  //ifOutOctets object?
325  else if(osStrcmp(object->name, "ifOutOctets") == 0)
326  {
327  //Get object value
328  value->counter32 = (uint32_t) interface->ifStats.outOctets;
329  }
330  //ifOutUcastPkts object?
331  else if(osStrcmp(object->name, "ifOutUcastPkts") == 0)
332  {
333  //Get object value
334  value->counter32 = (uint32_t) interface->ifStats.outUcastPkts;
335  }
336  //ifOutNUcastPkts object?
337  else if(osStrcmp(object->name, "ifOutNUcastPkts") == 0)
338  {
339  //Get object value
340  value->counter32 = interface->ifStats.outNUcastPkts;
341  }
342  //ifOutDiscards object?
343  else if(osStrcmp(object->name, "ifOutDiscards") == 0)
344  {
345  //Get object value
346  value->counter32 = interface->ifStats.outDiscards;
347  }
348  //ifOutErrors object?
349  else if(osStrcmp(object->name, "ifOutErrors") == 0)
350  {
351  //Get object value
352  value->counter32 = interface->ifStats.outErrors;
353  }
354  //ifOutQLen object?
355  else if(osStrcmp(object->name, "ifOutQLen") == 0)
356  {
357  //Get object value
358  value->gauge32 = 0;
359  }
360  //ifSpecific object?
361  else if(osStrcmp(object->name, "ifSpecific") == 0)
362  {
363  //Make sure the buffer is large enough to hold the entire object
364  if(*valueLen >= 1)
365  {
366  //If this information is not present, its value should be set to the
367  //object identifier 0.0
368  value->oid[0] = 0;
369 
370  //Return object length
371  *valueLen = 1;
372  }
373  else
374  {
375  //Report an error
376  error = ERROR_BUFFER_OVERFLOW;
377  }
378  }
379  //Unknown object?
380  else
381  {
382  //The specified object does not exist
383  error = ERROR_OBJECT_NOT_FOUND;
384  }
385 
386  //Return status code
387  return error;
388 }
389 
390 
391 /**
392  * @brief Get next ifEntry object
393  * @param[in] object Pointer to the MIB object descriptor
394  * @param[in] oid Object identifier
395  * @param[in] oidLen Length of the OID, in bytes
396  * @param[out] nextOid OID of the next object in the MIB
397  * @param[out] nextOidLen Length of the next object identifier, in bytes
398  * @return Error code
399  **/
400 
401 error_t mib2GetNextIfEntry(const MibObject *object, const uint8_t *oid,
402  size_t oidLen, uint8_t *nextOid, size_t *nextOidLen)
403 {
404  error_t error;
405  size_t n;
406  uint_t index;
407  NetContext *context;
408 
409  //Point to the TCP/IP stack context
410  context = netGetDefaultContext();
411 
412  //Make sure the buffer is large enough to hold the OID prefix
413  if(*nextOidLen < object->oidLen)
414  return ERROR_BUFFER_OVERFLOW;
415 
416  //Copy OID prefix
417  osMemcpy(nextOid, object->oid, object->oidLen);
418 
419  //Loop through network interfaces
420  for(index = 1; index <= context->numInterfaces; index++)
421  {
422  //Append the instance identifier to the OID prefix
423  n = object->oidLen;
424 
425  //ifIndex is used as instance identifier
426  error = mibEncodeIndex(nextOid, *nextOidLen, &n, index);
427  //Any error to report?
428  if(error)
429  return error;
430 
431  //Check whether the resulting object identifier lexicographically
432  //follows the specified OID
433  if(oidComp(nextOid, n, oid, oidLen) > 0)
434  {
435  //Save the length of the resulting object identifier
436  *nextOidLen = n;
437  //Next object found
438  return NO_ERROR;
439  }
440  }
441 
442  //The specified OID does not lexicographically precede the name
443  //of some object
444  return ERROR_OBJECT_NOT_FOUND;
445 }
446 
447 #endif
@ MIB2_IF_TYPE_OTHER
Definition: mib2_module.h:200
MIB-II module.
#define NetContext
Definition: net.h:36
int bool_t
Definition: compiler_port.h:63
@ MIB2_IF_TYPE_L2_VLAN
Definition: mib2_module.h:211
@ ERROR_BUFFER_OVERFLOW
Definition: error.h:143
error_t mib2SetIfEntry(const MibObject *object, const uint8_t *oid, size_t oidLen, const MibVariant *value, size_t valueLen, bool_t commit)
Set ifEntry object value.
Definition: mib2_impl_if.c:87
OID (Object Identifier)
#define MIB2_PHYS_ADDRESS_SIZE
Definition: mib2_module.h:141
@ MIB2_IF_TYPE_ETHERNET_CSMACD
Definition: mib2_module.h:201
@ MIB2_IF_TYPE_PPP
Definition: mib2_module.h:203
error_t mib2GetIfEntry(const MibObject *object, const uint8_t *oid, size_t oidLen, MibVariant *value, size_t *valueLen)
Get ifEntry object value.
Definition: mib2_impl_if.c:105
@ MIB2_IF_TYPE_IEEE_802_15_4
Definition: mib2_module.h:215
#define osStrcmp(s1, s2)
Definition: os_port.h:174
#define osStrlen(s)
Definition: os_port.h:168
int_t oidComp(const uint8_t *oid1, size_t oidLen1, const uint8_t *oid2, size_t oidLen2)
Compare object identifiers.
Definition: oid.c:103
uint8_t oid[]
Definition: lldp_tlv.h:300
#define macCopyAddr(destMacAddr, srcMacAddr)
Definition: ethernet.h:127
@ MIB2_IF_ADMIN_STATUS_UP
Definition: mib2_module.h:225
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
NetInterface * nicGetPhysicalInterface(NetInterface *interface)
Retrieve physical interface.
Definition: nic.c:87
error_t
Error codes.
Definition: error.h:43
@ ERROR_INSTANCE_NOT_FOUND
Definition: error.h:258
@ MIB2_IF_OPER_STATUS_DOWN
Definition: mib2_module.h:238
#define NetInterface
Definition: net.h:40
General definitions for cryptographic algorithms.
NetContext * netGetDefaultContext(void)
Get default TCP/IP stack context.
Definition: net.c:527
@ MIB2_IF_ADMIN_STATUS_DOWN
Definition: mib2_module.h:226
@ NIC_TYPE_PPP
PPP interface.
Definition: nic.h:84
MIB-II module implementation.
error_t mibDecodeIndex(const uint8_t *oid, size_t oidLen, size_t *pos, uint_t *index)
Decode instance identifier (index)
Definition: mib_common.c:64
MIB-II module implementation (Interface group)
Common definitions for MIB modules.
uint8_t n
@ ERROR_WRITE_FAILED
Definition: error.h:223
@ ERROR_OBJECT_NOT_FOUND
Definition: error.h:257
error_t mib2GetNextIfEntry(const MibObject *object, const uint8_t *oid, size_t oidLen, uint8_t *nextOid, size_t *nextOidLen)
Get next ifEntry object.
Definition: mib2_impl_if.c:401
error_t mib2GetIfNumber(const MibObject *object, const uint8_t *oid, size_t oidLen, MibVariant *value, size_t *valueLen)
Get ifNumber object value.
Definition: mib2_impl_if.c:59
#define MibObject
Definition: mib_common.h:46
uint8_t value[]
Definition: tcp.h:376
uint8_t oidLen
Definition: lldp_tlv.h:299
error_t mibEncodeIndex(uint8_t *oid, size_t maxOidLen, size_t *pos, uint_t index)
Encode instance identifier (index)
Definition: mib_common.c:47
@ MIB2_IF_OPER_STATUS_UP
Definition: mib2_module.h:237
MibVariant
Definition: mib_common.h:196
@ NIC_TYPE_6LOWPAN
6LoWPAN interface
Definition: nic.h:87
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
NetInterface * nicGetLogicalInterface(NetInterface *interface)
Retrieve logical interface.
Definition: nic.c:51
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
ASN.1 (Abstract Syntax Notation One)
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83