snmp_agent_dispatch.c
Go to the documentation of this file.
1 /**
2  * @file snmp_agent_dispatch.c
3  * @brief SNMP message dispatching
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.8
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 "snmp/snmp_agent.h"
38 #include "snmp/snmp_agent_pdu.h"
39 #include "snmp/snmp_agent_misc.h"
40 #include "mibs/mib2_module.h"
41 #include "mibs/snmp_mib_module.h"
42 #include "core/crypto.h"
43 #include "encoding/asn1.h"
44 #include "encoding/oid.h"
45 #include "debug.h"
46 
47 //Check TCP/IP stack configuration
48 #if (SNMP_AGENT_SUPPORT == ENABLED)
49 
50 
51 /**
52  * @brief Process incoming SNMP message
53  * @param[in] context Pointer to the SNMP agent context
54  * @return Error code
55  **/
56 
58 {
59  error_t error;
60 
61  //Total number of messages delivered to the SNMP entity from the
62  //transport service
63  MIB2_SNMP_INC_COUNTER32(snmpInPkts, 1);
64  SNMP_MIB_INC_COUNTER32(snmpGroup.snmpInPkts, 1);
65 
66 #if (SNMP_V3_SUPPORT == ENABLED)
67  //Refresh SNMP engine time
68  snmpRefreshEngineTime(context);
69 #endif
70 
71  //Message parsing initialization
72  snmpInitMessage(&context->request);
73 
74  //Parse SNMP message header
75  error = snmpParseMessageHeader(&context->request);
76  //Any error to report?
77  if(error)
78  {
79  return error;
80  }
81 
82  //The SNMP agent verifies the version number. If there is a mismatch,
83  //it discards the datagram and performs no further actions
84  if(context->request.version < context->versionMin ||
85  context->request.version > context->versionMax)
86  {
87  //Debug message
88  TRACE_WARNING(" Invalid SNMP version!\r\n");
89  //Discard incoming SNMP message
90  return ERROR_INVALID_VERSION;
91  }
92 
93 #if (SNMP_V1_SUPPORT == ENABLED)
94  //SNMPv1 version?
95  if(context->request.version == SNMP_VERSION_1)
96  {
97  //Process incoming SNMPv1 message
98  error = snmpv1ProcessMessage(context);
99  }
100  else
101 #endif
102 #if (SNMP_V2C_SUPPORT == ENABLED)
103  //SNMPv2c version?
104  if(context->request.version == SNMP_VERSION_2C)
105  {
106  //Process incoming SNMPv2c message
107  error = snmpv2cProcessMessage(context);
108  }
109  else
110 #endif
111 #if (SNMP_V3_SUPPORT == ENABLED)
112  //SNMPv3 version?
113  if(context->request.version == SNMP_VERSION_3)
114  {
115  //Process incoming SNMPv3 message
116  error = snmpv3ProcessMessage(context);
117  }
118  else
119 #endif
120  //Invalid SNMP version?
121  {
122  //Debug message
123  TRACE_WARNING(" Invalid SNMP version!\r\n");
124 
125  //Total number of SNMP messages which were delivered to the SNMP
126  //protocol entity and were for an unsupported SNMP version
127  MIB2_SNMP_INC_COUNTER32(snmpInBadVersions, 1);
128  SNMP_MIB_INC_COUNTER32(snmpGroup.snmpInBadVersions, 1);
129 
130  //Discard incoming SNMP message
131  error = ERROR_INVALID_VERSION;
132  }
133 
134  //Check status code
135  if(error == NO_ERROR)
136  {
137  //Total number of messages which were passed from the SNMP protocol
138  //entity to the transport service
139  MIB2_SNMP_INC_COUNTER32(snmpOutPkts, 1);
140  }
141  else if(error == ERROR_INVALID_TAG)
142  {
143  //Total number of ASN.1 or BER errors encountered by the SNMP protocol
144  //entity when decoding received SNMP messages
145  MIB2_SNMP_INC_COUNTER32(snmpInASNParseErrs, 1);
146  SNMP_MIB_INC_COUNTER32(snmpGroup.snmpInASNParseErrs, 1);
147  }
148  else if(error == ERROR_BUFFER_OVERFLOW)
149  {
150  //Total number of PDUs delivered to the SNMP entity which were silently
151  //dropped because the size of the reply was greater than the maximum
152  //message size
153  SNMP_MIB_INC_COUNTER32(snmpGroup.snmpSilentDrops, 1);
154  }
155 
156  //Return status code
157  return error;
158 }
159 
160 
161 /**
162  * @brief Process incoming SNMPv1 message
163  * @param[in] context Pointer to the SNMP agent context
164  * @return Error code
165  **/
166 
168 {
169 #if (SNMP_V1_SUPPORT == ENABLED)
170  error_t error;
171  SnmpUserEntry *community;
172 
173  //Parse community name
174  error = snmpParseCommunity(&context->request);
175  //Any error to report?
176  if(error)
177  {
178  return error;
179  }
180 
181  //Information about the community name is extracted from the local
182  //configuration datastore
183  community = snmpFindCommunityEntry(context, context->request.community,
184  context->request.communityLen);
185 
186  //Invalid community name?
187  if(community == NULL || community->status != MIB_ROW_STATUS_ACTIVE)
188  {
189  //Debug message
190  TRACE_WARNING(" Invalid community name!\r\n");
191 
192  //Total number of SNMP messages delivered to the SNMP protocol entity
193  //which used a SNMP community name not known to said entity
194  MIB2_SNMP_INC_COUNTER32(snmpInBadCommunityNames, 1);
195  SNMP_MIB_INC_COUNTER32(snmpGroup.snmpInBadCommunityNames, 1);
196 
197  //Report an error
199  }
200 
201  //Save the security profile associated with the current community
202  context->user = *community;
203 
204  //Process PDU
205  error = snmpProcessPdu(context);
206  //Any error to report?
207  if(error)
208  {
209  return error;
210  }
211 
212  //Any response?
213  if(context->response.length > 0)
214  {
215  //Format SNMP message header
216  error = snmpWriteMessageHeader(&context->response);
217  }
218 
219  //Return status code
220  return error;
221 #else
222  //Report an error
223  return ERROR_INVALID_VERSION;
224 #endif
225 }
226 
227 
228 /**
229  * @brief Process incoming SNMPv2c message
230  * @param[in] context Pointer to the SNMP agent context
231  * @return Error code
232  **/
233 
235 {
236 #if (SNMP_V2C_SUPPORT == ENABLED)
237  error_t error;
238  SnmpUserEntry *community;
239 
240  //Parse community name
241  error = snmpParseCommunity(&context->request);
242  //Any error to report?
243  if(error)
244  {
245  return error;
246  }
247 
248  //Information about the community name is extracted from the local
249  //configuration datastore
250  community = snmpFindCommunityEntry(context, context->request.community,
251  context->request.communityLen);
252 
253  //Invalid community name?
254  if(community == NULL || community->status != MIB_ROW_STATUS_ACTIVE)
255  {
256  //Debug message
257  TRACE_WARNING(" Invalid community name!\r\n");
258 
259  //Total number of SNMP messages delivered to the SNMP protocol entity
260  //which used a SNMP community name not known to said entity
261  MIB2_SNMP_INC_COUNTER32(snmpInBadCommunityNames, 1);
262  SNMP_MIB_INC_COUNTER32(snmpGroup.snmpInBadCommunityNames, 1);
263 
264  //Report an error
266  }
267 
268  //Save the security profile associated with the current community
269  context->user = *community;
270 
271  //Process PDU
272  error = snmpProcessPdu(context);
273  //Any error to report?
274  if(error)
275  {
276  return error;
277  }
278 
279  //Any response?
280  if(context->response.length > 0)
281  {
282  //Format SNMP message header
283  error = snmpWriteMessageHeader(&context->response);
284  }
285 
286  //Return status code
287  return error;
288 #else
289  //Report an error
290  return ERROR_INVALID_VERSION;
291 #endif
292 }
293 
294 
295 /**
296  * @brief Process incoming SNMPv3 message
297  * @param[in] context Pointer to the SNMP agent context
298  * @return Error code
299  **/
300 
302 {
303 #if (SNMP_V3_SUPPORT == ENABLED)
304  error_t error;
305  SnmpUserEntry *user;
306 
307  //Parse msgGlobalData field
308  error = snmpParseGlobalData(&context->request);
309  //Any error to report?
310  if(error)
311  {
312  return error;
313  }
314 
315  //Parse msgSecurityParameters field
316  error = snmpParseSecurityParameters(&context->request);
317  //Any error to report?
318  if(error)
319  {
320  return error;
321  }
322 
323  //Start of exception handling block
324  do
325  {
326 #if (SNMP_AGENT_INFORM_SUPPORT == ENABLED)
327  if(context->request.msgUserNameLen == 0 && context->request.msgFlags == 0)
328  {
329  //Clear the security profile
330  osMemset(&context->user, 0, sizeof(SnmpUserEntry));
331  }
332  else if(context->informContextEngineLen > 0 &&
333  oidComp(context->request.msgAuthEngineId, context->request.msgAuthEngineIdLen,
334  context->informContextEngine, context->informContextEngineLen) == 0)
335  {
336  //Information about the value of the msgUserName field is extracted
337  //from the local configuration datastore
338  user = snmpFindUserEntry(context, context->request.msgUserName,
339  context->request.msgUserNameLen);
340 
341  //Check security parameters
342  error = snmpCheckSecurityParameters(user, &context->request,
343  context->informContextEngine, context->informContextEngineLen);
344  //Invalid security parameters?
345  if(error)
346  {
347  break;
348  }
349 
350  //Save the security profile associated with the current user
351  context->user = *user;
352 
353  //Localize the authentication key with the engine ID of the remote
354  //SNMP device
355  if(context->user.authProtocol != SNMP_AUTH_PROTOCOL_NONE)
356  {
357  //Key localization algorithm
358  error = snmpLocalizeKey(context->user.authProtocol,
359  context->informContextEngine, context->informContextEngineLen,
360  &context->user.rawAuthKey, &context->user.localizedAuthKey);
361  //Any error to report?
362  if(error)
363  {
364  break;
365  }
366  }
367 
368  //Localize the privacy key with the engine ID of the remote SNMP device
369  if(context->user.privProtocol != SNMP_PRIV_PROTOCOL_NONE)
370  {
371  //Key localization algorithm
372  error = snmpLocalizeKey(context->user.authProtocol,
373  context->informContextEngine, context->informContextEngineLen,
374  &context->user.rawPrivKey, &context->user.localizedPrivKey);
375  //Any error to report?
376  if(error)
377  {
378  break;
379  }
380  }
381  }
382  else
383 #endif
384  {
385  //Information about the value of the msgUserName field is extracted
386  //from the local configuration datastore
387  user = snmpFindUserEntry(context, context->request.msgUserName,
388  context->request.msgUserNameLen);
389 
390  //Check security parameters
391  error = snmpCheckSecurityParameters(user, &context->request,
392  context->contextEngine, context->contextEngineLen);
393  //Invalid security parameters?
394  if(error)
395  {
396  break;
397  }
398 
399  //Save the security profile associated with the current user
400  context->user = *user;
401  }
402 
403  //Check whether the authFlag is set
404  if((context->request.msgFlags & SNMP_MSG_FLAG_AUTH) != 0)
405  {
406  //Authenticate incoming SNMP message
407  error = snmpAuthIncomingMessage(&context->user, &context->request);
408  //Data authentication failed?
409  if(error)
410  {
411  break;
412  }
413 
414  //Replay protection
415  error = snmpCheckEngineTime(context, &context->request);
416  //Message outside of the time window?
417  if(error)
418  {
419  break;
420  }
421  }
422 
423  //Check whether the privFlag is set
424  if((context->request.msgFlags & SNMP_MSG_FLAG_PRIV) != 0)
425  {
426  //Decrypt data
427  error = snmpDecryptData(&context->user, &context->request);
428  //Data decryption failed?
429  if(error)
430  {
431  break;
432  }
433  }
434 
435  //Parse scopedPDU
436  error = snmpParseScopedPdu(&context->request);
437  //Any error to report?
438  if(error)
439  {
440  break;
441  }
442 
443  //Process PDU
444  error = snmpProcessPdu(context);
445  //Any error to report?
446  if(error)
447  {
448  break;
449  }
450 
451  //End of exception handling block
452  } while(0);
453 
454  //Check error indication
455  if(error == ERROR_UNSUPPORTED_SECURITY_LEVEL ||
456  error == ERROR_NOT_IN_TIME_WINDOW ||
457  error == ERROR_UNKNOWN_USER_NAME ||
458  error == ERROR_UNKNOWN_ENGINE_ID ||
459  error == ERROR_AUTHENTICATION_FAILED ||
460  error == ERROR_DECRYPTION_FAILED ||
461  error == ERROR_UNAVAILABLE_CONTEXT ||
462  error == ERROR_UNKNOWN_CONTEXT)
463  {
464  //When the reportable flag is used, if its value is one, a Report-PDU
465  //must be returned to the sender
466  if((context->request.msgFlags & SNMP_MSG_FLAG_REPORTABLE) != 0)
467  {
468  error = snmpFormatReportPdu(context, error);
469  }
470 
471  //Any error to report?
472  if(error)
473  {
474  return error;
475  }
476  }
477  else if(error == NO_ERROR)
478  {
479  //Continue processing
480  }
481  else
482  {
483  //Stop processing
484  return error;
485  }
486 
487  //Any response?
488  if(context->response.length > 0)
489  {
490  //Format scopedPDU
491  error = snmpWriteScopedPdu(&context->response);
492  //Any error to report?
493  if(error)
494  {
495  return error;
496  }
497 
498  //Check whether the privFlag is set
499  if((context->response.msgFlags & SNMP_MSG_FLAG_PRIV) != 0)
500  {
501  //Encrypt data
502  error = snmpEncryptData(&context->user, &context->response,
503  context->salt);
504  //Any error to report?
505  if(error)
506  {
507  return error;
508  }
509  }
510 
511  //Format SNMP message header
512  error = snmpWriteMessageHeader(&context->response);
513  //Any error to report?
514  if(error)
515  {
516  return error;
517  }
518 
519  //Check whether the authFlag is set
520  if((context->response.msgFlags & SNMP_MSG_FLAG_AUTH) != 0)
521  {
522  //Authenticate outgoing SNMP message
523  error = snmpAuthOutgoingMessage(&context->user, &context->response);
524  //Any error to report?
525  if(error)
526  {
527  return error;
528  }
529  }
530  }
531 
532  //Successful processing
533  return NO_ERROR;
534 #else
535  //Report an error
536  return ERROR_INVALID_VERSION;
537 #endif
538 }
539 
540 #endif
@ ERROR_UNKNOWN_ENGINE_ID
Definition: error.h:263
MIB-II module.
error_t snmpProcessPdu(SnmpAgentContext *context)
Process PDU.
@ ERROR_UNKNOWN_USER_NAME
Definition: error.h:264
error_t snmpDecryptData(const SnmpUserEntry *user, SnmpMessage *message)
Data decryption.
@ ERROR_UNKNOWN_CONTEXT
Definition: error.h:265
SnmpUserEntry * snmpFindCommunityEntry(SnmpAgentContext *context, const char_t *community, size_t length)
Search the community table for a given community string.
error_t snmpv2cProcessMessage(SnmpAgentContext *context)
Process incoming SNMPv2c message.
@ ERROR_BUFFER_OVERFLOW
Definition: error.h:143
@ ERROR_DECRYPTION_FAILED
Definition: error.h:244
OID (Object Identifier)
error_t snmpParseGlobalData(SnmpMessage *message)
Parse msgGlobalData field.
error_t snmpWriteScopedPdu(SnmpMessage *message)
Format scopedPDU.
error_t snmpCheckEngineTime(SnmpAgentContext *context, SnmpMessage *message)
Replay protection.
#define SNMP_MIB_INC_COUNTER32(name, value)
User table entry.
@ SNMP_VERSION_2C
Definition: snmp_common.h:139
@ ERROR_INVALID_VERSION
Definition: error.h:118
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:148
SNMP agent (Simple Network Management Protocol)
@ SNMP_MSG_FLAG_PRIV
error_t snmpFormatReportPdu(SnmpAgentContext *context, error_t errorIndication)
Format Report-PDU.
@ SNMP_PRIV_PROTOCOL_NONE
No privacy.
error_t
Error codes.
Definition: error.h:43
#define MIB2_SNMP_INC_COUNTER32(name, value)
Definition: mib2_module.h:169
error_t snmpParseScopedPdu(SnmpMessage *message)
Parse scopedPDU field.
SnmpUserEntry * snmpFindUserEntry(SnmpAgentContext *context, const char_t *name, size_t length)
Search the user table for a given user name.
@ ERROR_UNAVAILABLE_CONTEXT
Definition: error.h:266
@ ERROR_NOT_IN_TIME_WINDOW
Definition: error.h:268
error_t snmpProcessMessage(SnmpAgentContext *context)
Process incoming SNMP message.
@ SNMP_MSG_FLAG_REPORTABLE
error_t snmpAuthIncomingMessage(const SnmpUserEntry *user, SnmpMessage *message)
Authenticate incoming SNMP message.
error_t snmpv3ProcessMessage(SnmpAgentContext *context)
Process incoming SNMPv3 message.
error_t snmpCheckSecurityParameters(const SnmpUserEntry *user, SnmpMessage *message, const uint8_t *engineId, size_t engineIdLen)
Check security parameters.
General definitions for cryptographic library.
Helper functions for SNMP agent.
@ SNMP_MSG_FLAG_AUTH
MibRowStatus status
Status of the user.
@ SNMP_VERSION_3
Definition: snmp_common.h:140
#define TRACE_WARNING(...)
Definition: debug.h:93
@ MIB_ROW_STATUS_ACTIVE
Definition: mib_common.h:103
error_t snmpParseSecurityParameters(SnmpMessage *message)
Parse msgSecurityParameters field.
error_t snmpParseMessageHeader(SnmpMessage *message)
Parse SNMP message header.
@ ERROR_AUTHENTICATION_FAILED
Definition: error.h:69
error_t snmpLocalizeKey(SnmpAuthProtocol authProtocol, const uint8_t *engineId, size_t engineIdLen, SnmpKey *key, SnmpKey *localizedKey)
Key localization algorithm.
@ SNMP_VERSION_1
Definition: snmp_common.h:138
#define SnmpAgentContext
Definition: snmp_agent.h:36
@ ERROR_INVALID_TAG
Definition: error.h:114
SNMP agent (PDU processing)
error_t snmpAuthOutgoingMessage(const SnmpUserEntry *user, SnmpMessage *message)
Authenticate outgoing SNMP message.
error_t snmpEncryptData(const SnmpUserEntry *user, SnmpMessage *message, uint8_t *salt)
Data encryption.
void snmpRefreshEngineTime(SnmpAgentContext *context)
Refresh SNMP engine time.
#define osMemset(p, value, length)
Definition: os_port.h:141
TCP/IP stack core.
@ SNMP_AUTH_PROTOCOL_NONE
No authentication.
error_t snmpParseCommunity(SnmpMessage *message)
Parse community name.
error_t snmpv1ProcessMessage(SnmpAgentContext *context)
Process incoming SNMPv1 message.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
ASN.1 (Abstract Syntax Notation One)
void snmpInitMessage(SnmpMessage *message)
Initialize a SNMP message.
error_t snmpWriteMessageHeader(SnmpMessage *message)
Format SNMP message header.
@ ERROR_UNSUPPORTED_SECURITY_LEVEL
Definition: error.h:267
SNMP message dispatching.