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