snmp_mib_impl.c
Go to the documentation of this file.
1 /**
2  * @file snmp_mib_impl.c
3  * @brief SNMP MIB module implementation
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/snmp_mib_module.h"
38 #include "mibs/snmp_mib_impl.h"
39 #include "snmp/snmp_agent.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 (SNMP_MIB_SUPPORT == ENABLED)
47 
48 
49 /**
50  * @brief SNMP MIB module initialization
51  * @return Error code
52  **/
53 
55 {
56  NetContext *context;
57  SnmpMibSysGroup *sysGroup;
58  SnmpMibSnmpGroup *snmpGroup;
59  SnmpMibSetGroup *setGroup;
60 
61  //Debug message
62  TRACE_INFO("Initializing SNMP-MIB base...\r\n");
63 
64  //Clear SNMP MIB base
65  osMemset(&snmpMibBase, 0, sizeof(snmpMibBase));
66 
67  //Point to the TCP/IP stack context
68  context = netGetDefaultContext();
69 
70  //Point to the system group
71  sysGroup = &snmpMibBase.sysGroup;
72 
73 #if (SNMP_MIB_SYS_DESCR_SIZE > 0)
74  //sysDescr object
75  osStrcpy(sysGroup->sysDescr, "Description");
76  sysGroup->sysDescrLen = osStrlen(sysGroup->sysDescr);
77 #endif
78 
79 #if (SNMP_MIB_SYS_OBJECT_ID_SIZE > 0)
80  //sysObjectID object
81  sysGroup->sysObjectID[0] = 0;
82  sysGroup->sysObjectIDLen = 1;
83 #endif
84 
85 #if (SNMP_MIB_SYS_CONTACT_SIZE > 0)
86  //sysContact object
87  osStrcpy(sysGroup->sysContact, "Contact");
88  sysGroup->sysContactLen = osStrlen(sysGroup->sysContact);
89 #endif
90 
91 #if (SNMP_MIB_SYS_NAME_SIZE > 0)
92  //sysName object
93  osStrcpy(sysGroup->sysName, "Name");
94  sysGroup->sysNameLen = osStrlen(sysGroup->sysName);
95 #endif
96 
97 #if (SNMP_MIB_SYS_LOCATION_SIZE > 0)
98  //sysLocation object
99  osStrcpy(sysGroup->sysLocation, "Location");
100  sysGroup->sysLocationLen = osStrlen(sysGroup->sysLocation);
101 #endif
102 
103  //sysServices object
105 
106  //Point to the SNMP group
107  snmpGroup = &snmpMibBase.snmpGroup;
108  //snmpEnableAuthenTraps object
110 
111  //Point to the set group
112  setGroup = &snmpMibBase.setGroup;
113  //snmpSetSerialNo object
114  setGroup->snmpSetSerialNo = netGetRandRange(context, 1, INT32_MAX);
115 
116  //Successful processing
117  return NO_ERROR;
118 }
119 
120 
121 /**
122  * @brief Load SNMP MIB module
123  * @param[in] context Pointer to the SNMP agent context
124  * @return Error code
125  **/
126 
127 error_t snmpMibLoad(void *context)
128 {
129  //Register SNMP agent context
130  snmpMibBase.context = context;
131 
132  //Successful processing
133  return NO_ERROR;
134 }
135 
136 
137 /**
138  * @brief Unload SNMP MIB module
139  * @param[in] context Pointer to the SNMP agent context
140  **/
141 
142 void snmpMibUnload(void *context)
143 {
144  //Unregister SNMP agent context
145  snmpMibBase.context = NULL;
146 }
147 
148 
149 /**
150  * @brief Lock SNMP MIB base
151  **/
152 
153 void snmpMibLock(void)
154 {
155 }
156 
157 
158 /**
159  * @brief Unlock SNMP MIB base
160  **/
161 
162 void snmpMibUnlock(void)
163 {
164 }
165 
166 
167 /**
168  * @brief Get sysUpTime object value
169  * @param[in] object Pointer to the MIB object descriptor
170  * @param[in] oid Object identifier (object name and instance identifier)
171  * @param[in] oidLen Length of the OID, in bytes
172  * @param[out] value Object value
173  * @param[in,out] valueLen Length of the object value, in bytes
174  * @return Error code
175  **/
176 
177 error_t snmpMibGetSysUpTime(const MibObject *object, const uint8_t *oid,
178  size_t oidLen, MibVariant *value, size_t *valueLen)
179 {
180  //Get object value
181  value->timeTicks = osGetSystemTime64() / 10;
182 
183  //Successful processing
184  return NO_ERROR;
185 }
186 
187 
188 /**
189  * @brief Get sysOREntry object value
190  * @param[in] object Pointer to the MIB object descriptor
191  * @param[in] oid Object identifier (object name and instance identifier)
192  * @param[in] oidLen Length of the OID, in bytes
193  * @param[out] value Object value
194  * @param[in,out] valueLen Length of the object value, in bytes
195  * @return Error code
196  **/
197 
198 error_t snmpMibGetSysOREntry(const MibObject *object, const uint8_t *oid,
199  size_t oidLen, MibVariant *value, size_t *valueLen)
200 {
201  error_t error;
202  size_t n;
203  uint_t index;
204  SnmpAgentContext *context;
205  const MibModule *mibModule;
206 
207  //Point to the instance identifier
208  n = object->oidLen;
209 
210  //sysORIndex is used as instance identifier
211  error = mibDecodeIndex(oid, oidLen, &n, &index);
212  //Invalid instance identifier?
213  if(error)
214  return error;
215 
216  //Sanity check
217  if(n != oidLen)
219 
220  //Check index range
221  if(index < 1 || index > SNMP_AGENT_MAX_MIBS)
223 
224  //Point to the SNMP agent context
225  context = (SnmpAgentContext *) snmpMibBase.context;
226  //Sanity check
227  if(context == NULL)
229 
230  //Point to the MIB
231  mibModule = context->mibTable[index - 1];
232  //Make sure the MIB is properly loaded
233  if(mibModule == NULL)
235 
236  //sysORID object?
237  if(osStrcmp(object->name, "sysORID") == 0)
238  {
239  //Make sure the buffer is large enough to hold the entire object
240  if(*valueLen >= mibModule->oidLen)
241  {
242  //Copy object value
243  osMemcpy(value->octetString, mibModule->oid, mibModule->oidLen);
244  //Return object length
245  *valueLen = mibModule->oidLen;
246  }
247  else
248  {
249  //Report an error
250  error = ERROR_BUFFER_OVERFLOW;
251  }
252  }
253  //sysORDescr object?
254  else if(osStrcmp(object->name, "sysORDescr") == 0)
255  {
256  //Retrieve the length of the MIB name
257  n = osStrlen(mibModule->name);
258 
259  //Make sure the buffer is large enough to hold the entire object
260  if(*valueLen >= n)
261  {
262  //Copy object value
263  osMemcpy(value->octetString, mibModule->name, n);
264  //Return object length
265  *valueLen = n;
266  }
267  else
268  {
269  //Report an error
270  error = ERROR_BUFFER_OVERFLOW;
271  }
272  }
273  //sysORUpTime object?
274  else if(osStrcmp(object->name, "sysORUpTime") == 0)
275  {
276  //Get object value
277  value->timeTicks = 0;
278  }
279  //Unknown object?
280  else
281  {
282  //The specified object does not exist
283  error = ERROR_OBJECT_NOT_FOUND;
284  }
285 
286  //Return status code
287  return error;
288 }
289 
290 
291 /**
292  * @brief Get next sysOREntry object
293  * @param[in] object Pointer to the MIB object descriptor
294  * @param[in] oid Object identifier
295  * @param[in] oidLen Length of the OID, in bytes
296  * @param[out] nextOid OID of the next object in the MIB
297  * @param[out] nextOidLen Length of the next object identifier, in bytes
298  * @return Error code
299  **/
300 
301 error_t snmpMibGetNextSysOREntry(const MibObject *object, const uint8_t *oid,
302  size_t oidLen, uint8_t *nextOid, size_t *nextOidLen)
303 {
304  error_t error;
305  size_t n;
306  uint_t index;
307  SnmpAgentContext *context;
308 
309  //Point to the SNMP agent context
310  context = (SnmpAgentContext *) snmpMibBase.context;
311  //Sanity check
312  if(context == NULL)
313  return ERROR_OBJECT_NOT_FOUND;
314 
315  //Make sure the buffer is large enough to hold the OID prefix
316  if(*nextOidLen < object->oidLen)
317  return ERROR_BUFFER_OVERFLOW;
318 
319  //Copy OID prefix
320  osMemcpy(nextOid, object->oid, object->oidLen);
321 
322  //Loop through existing MIBs
323  for(index = 1; index <= SNMP_AGENT_MAX_MIBS; index++)
324  {
325  //Make sure the MIB is properly loaded
326  if(context->mibTable[index - 1] != NULL)
327  {
328  //Append the instance identifier to the OID prefix
329  n = object->oidLen;
330 
331  //sysORIndex is used as instance identifier
332  error = mibEncodeIndex(nextOid, *nextOidLen, &n, index);
333  //Any error to report?
334  if(error)
335  return error;
336 
337  //Check whether the resulting object identifier lexicographically
338  //follows the specified OID
339  if(oidComp(nextOid, n, oid, oidLen) > 0)
340  {
341  //Save the length of the resulting object identifier
342  *nextOidLen = n;
343  //Next object found
344  return NO_ERROR;
345  }
346  }
347  }
348 
349  //The specified OID does not lexicographically precede the name
350  //of some object
351  return ERROR_OBJECT_NOT_FOUND;
352 }
353 
354 
355 /**
356  * @brief Get snmpTrapOID object value
357  * @param[in] object Pointer to the MIB object descriptor
358  * @param[in] oid Object identifier (object name and instance identifier)
359  * @param[in] oidLen Length of the OID, in bytes
360  * @param[out] value Object value
361  * @param[in,out] valueLen Length of the object value, in bytes
362  * @return Error code
363  **/
364 
365 error_t snmpv2MibGetSnmpTrapOID(const MibObject *object, const uint8_t *oid,
366  size_t oidLen, MibVariant *value, size_t *valueLen)
367 {
368  //Not implemented
369  *valueLen = 0;
370 
371  //Successful processing
372  return NO_ERROR;
373 }
374 
375 
376 /**
377  * @brief Get snmpTrapEnterprise object value
378  * @param[in] object Pointer to the MIB object descriptor
379  * @param[in] oid Object identifier (object name and instance identifier)
380  * @param[in] oidLen Length of the OID, in bytes
381  * @param[out] value Object value
382  * @param[in,out] valueLen Length of the object value, in bytes
383  * @return Error code
384  **/
385 
386 error_t snmpv2MibGetSnmpTrapEnterprise(const MibObject *object, const uint8_t *oid,
387  size_t oidLen, MibVariant *value, size_t *valueLen)
388 {
389  SnmpAgentContext *context;
390 
391  //Point to the SNMP agent context
392  context = (SnmpAgentContext *) snmpMibBase.context;
393  //Sanity check
394  if(context == NULL)
395  return ERROR_OBJECT_NOT_FOUND;
396 
397  //Make sure the buffer is large enough to hold the enterprise OID
398  if(*valueLen < context->enterpriseOidLen)
399  return ERROR_BUFFER_OVERFLOW;
400 
401  //Copy enterprise OID
402  osMemcpy(value->octetString, context->enterpriseOid,
403  context->enterpriseOidLen);
404 
405  //Return object length
406  *valueLen = context->enterpriseOidLen;
407 
408  //Successful processing
409  return NO_ERROR;
410 }
411 
412 
413 /**
414  * @brief Set snmpSetSerialNo object value
415  * @param[in] object Pointer to the MIB object descriptor
416  * @param[in] oid Object identifier (object name and instance identifier)
417  * @param[in] oidLen Length of the OID, in bytes
418  * @param[in] value Object value
419  * @param[in] valueLen Length of the object value, in bytes
420  * @param[in] commit This flag tells whether the changes shall be committed
421  * to the MIB base
422  * @return Error code
423  **/
424 
425 error_t snmpMibSetSnmpSetSerialNo(const MibObject *object, const uint8_t *oid,
426  size_t oidLen, const MibVariant *value, size_t valueLen, bool_t commit)
427 {
428  //Test and increment spin lock
430  value->integer, commit);
431 }
432 
433 
434 /**
435  * @brief Get snmpSetSerialNo object value
436  * @param[in] object Pointer to the MIB object descriptor
437  * @param[in] oid Object identifier (object name and instance identifier)
438  * @param[in] oidLen Length of the OID, in bytes
439  * @param[out] value Object value
440  * @param[in,out] valueLen Length of the object value, in bytes
441  * @return Error code
442  **/
443 
444 error_t snmpMibGetSnmpSetSerialNo(const MibObject *object, const uint8_t *oid,
445  size_t oidLen, MibVariant *value, size_t *valueLen)
446 {
447  //Get the current value of the spin lock
449 
450  //Successful processing
451  return NO_ERROR;
452 }
453 
454 #endif
void snmpMibUnlock(void)
Unlock SNMP MIB base.
#define NetContext
Definition: net.h:36
SnmpMibSysGroup sysGroup
error_t snmpMibLoad(void *context)
Load SNMP MIB module.
int bool_t
Definition: compiler_port.h:63
char_t sysDescr[SNMP_MIB_SYS_DESCR_SIZE]
@ ERROR_BUFFER_OVERFLOW
Definition: error.h:143
error_t snmpMibGetSysUpTime(const MibObject *object, const uint8_t *oid, size_t oidLen, MibVariant *value, size_t *valueLen)
Get sysUpTime object value.
OID (Object Identifier)
uint32_t netGetRandRange(NetContext *context, uint32_t min, uint32_t max)
Generate a random value in the specified range.
Definition: net.c:473
SnmpMibSetGroup setGroup
@ SNMP_MIB_SYS_SERVICE_INTERNET
error_t snmpv2MibGetSnmpTrapEnterprise(const MibObject *object, const uint8_t *oid, size_t oidLen, MibVariant *value, size_t *valueLen)
Get snmpTrapEnterprise object value.
#define osStrcmp(s1, s2)
Definition: os_port.h:174
#define SNMP_AGENT_MAX_MIBS
Definition: snmp_agent.h:69
#define osStrlen(s)
Definition: os_port.h:168
SNMP MIB module.
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
SNMP agent (Simple Network Management Protocol)
SnmpAgentContext * context
error_t snmpv2MibGetSnmpTrapOID(const MibObject *object, const uint8_t *oid, size_t oidLen, MibVariant *value, size_t *valueLen)
Get snmpTrapOID object value.
error_t snmpMibGetSnmpSetSerialNo(const MibObject *object, const uint8_t *oid, size_t oidLen, MibVariant *value, size_t *valueLen)
Get snmpSetSerialNo object value.
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
error_t
Error codes.
Definition: error.h:43
error_t snmpMibGetNextSysOREntry(const MibObject *object, const uint8_t *oid, size_t oidLen, uint8_t *nextOid, size_t *nextOidLen)
Get next sysOREntry object.
@ ERROR_INSTANCE_NOT_FOUND
Definition: error.h:258
error_t snmpMibSetSnmpSetSerialNo(const MibObject *object, const uint8_t *oid, size_t oidLen, const MibVariant *value, size_t valueLen, bool_t commit)
Set snmpSetSerialNo object value.
error_t snmpMibGetSysOREntry(const MibObject *object, const uint8_t *oid, size_t oidLen, MibVariant *value, size_t *valueLen)
Get sysOREntry object value.
const char_t * name
Definition: mib_common.h:293
SNMP MIB module implementation.
General definitions for cryptographic algorithms.
NetContext * netGetDefaultContext(void)
Get default TCP/IP stack context.
Definition: net.c:527
SnmpMibSnmpGroup snmpGroup
System group.
uint8_t sysObjectID[SNMP_MIB_SYS_OBJECT_ID_SIZE]
#define TRACE_INFO(...)
Definition: debug.h:105
MIB module.
Definition: mib_common.h:292
size_t oidLen
Definition: mib_common.h:295
SnmpMibBase snmpMibBase
SNMP MIB base.
@ SNMP_MIB_AUTHEN_TRAPS_DISABLED
error_t snmpMibInit(void)
SNMP MIB module initialization.
Definition: snmp_mib_impl.c:54
char_t sysLocation[SNMP_MIB_SYS_LOCATION_SIZE]
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
void snmpMibLock(void)
Lock SNMP MIB base.
Common definitions for MIB modules.
uint8_t n
char_t sysName[SNMP_MIB_SYS_NAME_SIZE]
@ ERROR_OBJECT_NOT_FOUND
Definition: error.h:257
void snmpMibUnload(void *context)
Unload SNMP MIB module.
int32_t snmpEnableAuthenTraps
#define MibObject
Definition: mib_common.h:46
uint8_t value[]
Definition: tcp.h:376
uint8_t oidLen
Definition: lldp_tlv.h:299
char_t sysContact[SNMP_MIB_SYS_CONTACT_SIZE]
#define osGetSystemTime64()
#define SnmpAgentContext
Definition: snmp_agent.h:36
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 mibTestAndIncSpinLock(int32_t *spinLock, int32_t value, bool_t commit)
Test and increment spin lock.
Definition: mib_common.c:1006
MibVariant
Definition: mib_common.h:196
unsigned int uint_t
Definition: compiler_port.h:57
uint8_t oid[MIB_MAX_OID_SIZE]
Definition: mib_common.h:294
#define osMemset(p, value, length)
Definition: os_port.h:138
TCP/IP stack core.
#define osStrcpy(s1, s2)
Definition: os_port.h:210
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
ASN.1 (Abstract Syntax Notation One)