snmp_agent_pdu.c
Go to the documentation of this file.
1 /**
2  * @file snmp_agent_pdu.c
3  * @brief SNMP agent (PDU processing)
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"
37 #include "snmp/snmp_agent_pdu.h"
38 #include "snmp/snmp_agent_misc.h"
39 #include "snmp/snmp_agent_object.h"
40 #include "mibs/mib2_module.h"
41 #include "mibs/snmp_mib_module.h"
43 #include "core/crypto.h"
44 #include "encoding/asn1.h"
45 #include "encoding/oid.h"
46 #include "debug.h"
47 
48 //Check TCP/IP stack configuration
49 #if (SNMP_AGENT_SUPPORT == ENABLED)
50 
51 //snmpUnavailableContexts.0 object (1.3.6.1.6.3.12.1.4.0)
52 const uint8_t snmpUnavailableContextsObject[9] = {43, 6, 1, 6, 3, 12, 1, 4, 0};
53 //snmpUnknownContexts.0 object (1.3.6.1.6.3.12.1.5.0)
54 const uint8_t snmpUnknownContextsObject[9] = {43, 6, 1, 6, 3, 12, 1, 5, 0};
55 
56 
57 /**
58  * @brief Process PDU
59  * @param[in] context Pointer to the SNMP agent context
60  * @return Error code
61  **/
62 
64 {
65  error_t error;
66 
67  //Parse PDU header
68  error = snmpParsePduHeader(&context->request);
69  //Any error to report?
70  if(error)
71  {
72  return error;
73  }
74 
75  //Initialize response message
76  snmpInitMessage(&context->response);
77 
78  //Check PDU type
79  switch(context->request.pduType)
80  {
83  //Process GetRequest-PDU or GetNextRequest-PDU
84  error = snmpProcessGetRequestPdu(context);
85  break;
87  //Process GetBulkRequest-PDU
88  error = snmpProcessGetBulkRequestPdu(context);
89  break;
91  //Process SetRequest-PDU
92  error = snmpProcessSetRequestPdu(context);
93  break;
94 #if (SNMP_AGENT_INFORM_SUPPORT == ENABLED)
96  //Process GetResponse-PDU
97  error = snmpProcessGetResponsePdu(context);
98  break;
99  case SNMP_PDU_REPORT:
100  //Process Report-PDU
101  error = snmpProcessReportPdu(context);
102  break;
103 #endif
104  default:
105  //Invalid PDU type
106  error = ERROR_INVALID_TYPE;
107  break;
108  }
109 
110  //Check status code
111  if(!error)
112  {
113  //A GetResponse-PDU is generated by a protocol entity only upon receipt
114  //of the GetRequest-PDU, GetNextRequest-PDU, GetBulkRequest-PDU or
115  //SetRequest-PDU
116  if(context->request.pduType == SNMP_PDU_GET_REQUEST ||
117  context->request.pduType == SNMP_PDU_GET_NEXT_REQUEST ||
118  context->request.pduType == SNMP_PDU_GET_BULK_REQUEST ||
119  context->request.pduType == SNMP_PDU_SET_REQUEST)
120  {
121  //Total number of SNMP Get-Response PDUs which have been generated
122  //by the SNMP protocol entity
123  MIB2_SNMP_INC_COUNTER32(snmpOutGetResponses, 1);
124 
125  //Format PDU header
126  error = snmpWritePduHeader(&context->response);
127  }
128  }
129 
130  //Return status code
131  return error;
132 }
133 
134 
135 /**
136  * @brief Process GetRequest-PDU or GetNextRequest-PDU
137  * @param[in] context Pointer to the SNMP agent context
138  * @return Error code
139  **/
140 
142 {
143  error_t error;
144  int_t index;
145  size_t n;
146  size_t length;
147  const uint8_t *p;
148  SnmpVarBind var;
149 
150  //Check PDU type
151  if(context->request.pduType == SNMP_PDU_GET_REQUEST)
152  {
153  //Debug message
154  TRACE_INFO("Parsing GetRequest-PDU...\r\n");
155 
156  //Total number of SNMP Get-Request PDUs which have been accepted and
157  //processed by the SNMP protocol entity
158  MIB2_SNMP_INC_COUNTER32(snmpInGetRequests, 1);
159  }
160  else if(context->request.pduType == SNMP_PDU_GET_NEXT_REQUEST)
161  {
162  //Debug message
163  TRACE_INFO("Parsing GetNextRequest-PDU...\r\n");
164 
165  //Total number of SNMP Get-NextRequest PDUs which have been accepted
166  //and processed by the SNMP protocol entity
167  MIB2_SNMP_INC_COUNTER32(snmpInGetNexts, 1);
168  }
169 
170  //Enforce access policy
171  if(context->user.mode != SNMP_ACCESS_READ_ONLY &&
172  context->user.mode != SNMP_ACCESS_READ_WRITE)
173  {
174  //Total number of SNMP messages delivered to the SNMP protocol entity
175  //which represented an SNMP operation which was not allowed by the SNMP
176  MIB2_SNMP_INC_COUNTER32(snmpInBadCommunityUses, 1);
177  SNMP_MIB_INC_COUNTER32(snmpGroup.snmpInBadCommunityUses, 1);
178 
179  //Report an error
180  return ERROR_ACCESS_DENIED;
181  }
182 
183  //Initialize response message
184  error = snmpInitResponse(context);
185  //Any error to report?
186  if(error)
187  {
188  return error;
189  }
190 
191  //Point to the first variable binding of the request
192  p = context->request.varBindList;
193  length = context->request.varBindListLen;
194 
195  //Lock access to MIB bases
196  snmpLockMib(context);
197 
198  //Loop through the list
199  for(index = 1; length > 0; index++)
200  {
201  //Parse variable binding
202  error = snmpParseVarBinding(p, length, &var, &n);
203  //Failed to parse variable binding?
204  if(error)
205  {
206  break;
207  }
208 
209  //Make sure that the object identifier is valid
210  error = oidCheck(var.oid, var.oidLen);
211  //Invalid object identifier?
212  if(error)
213  {
214  break;
215  }
216 
217  //GetRequest-PDU?
218  if(context->request.pduType == SNMP_PDU_GET_REQUEST)
219  {
220  //Retrieve object value
221  error = snmpGetObjectValue(context, &context->request, &var);
222  }
223  //GetNextRequest-PDU?
224  else
225  {
226  //Search the MIB for the next object
227  error = snmpGetNextObject(context, &context->request, &var);
228 
229  //SNMPv1 version?
230  if(context->request.version == SNMP_VERSION_1)
231  {
232  //Check status code
233  if(error == NO_ERROR)
234  {
235  //Retrieve object value
236  error = snmpGetObjectValue(context, &context->request, &var);
237  }
238  else
239  {
240  //Stop immediately
241  break;
242  }
243  }
244  //SNMPv2c or SNMPv3 version?
245  else
246  {
247  //Check status code
248  if(error == NO_ERROR)
249  {
250  //Retrieve object value
251  error = snmpGetObjectValue(context, &context->request, &var);
252  }
253  else if(error == ERROR_OBJECT_NOT_FOUND)
254  {
255  //The variable binding's value field is set to endOfMibView
258  var.valueLen = 0;
259 
260  //Catch exception
261  error = NO_ERROR;
262  }
263  else
264  {
265  //Stop immediately
266  break;
267  }
268  }
269  }
270 
271  //Failed to retrieve object value?
272  if(error)
273  {
274  //SNMPv1 version?
275  if(context->request.version == SNMP_VERSION_1)
276  {
277  //Stop immediately
278  break;
279  }
280  //SNMPv2c or SNMPv3 version?
281  else
282  {
283  //Catch exception
284  if(error == ERROR_ACCESS_DENIED ||
285  error == ERROR_OBJECT_NOT_FOUND)
286  {
287  //The variable binding's value field is set to noSuchObject
290  var.valueLen = 0;
291  }
292  else if(error == ERROR_INSTANCE_NOT_FOUND)
293  {
294  //The variable binding's value field is set to noSuchInstance
297  var.valueLen = 0;
298  }
299  else
300  {
301  //Stop immediately
302  break;
303  }
304  }
305  }
306  else
307  {
308  //Total number of MIB objects which have been retrieved successfully
309  //by the SNMP protocol entity as the result of receiving valid SNMP
310  //Get-Request and Get-NextRequest PDUs
311  MIB2_SNMP_INC_COUNTER32(snmpInTotalReqVars, 1);
312  }
313 
314  //Append variable binding to the list
315  error = snmpWriteVarBinding(context, &var);
316  //Any error to report?
317  if(error)
318  {
319  break;
320  }
321 
322  //Advance data pointer
323  p += n;
324  length -= n;
325  }
326 
327  //Unlock access to MIB bases
328  snmpUnlockMib(context);
329 
330  //Check status code
331  if(error)
332  {
333  //Set error-status and error-index fields
334  error = snmpTranslateStatusCode(&context->response, error, index);
335  //If the parsing of the request fails, the SNMP agent discards the message
336  if(error)
337  {
338  return error;
339  }
340 
341  //Check whether an alternate Response-PDU should be sent
342  if(context->response.version != SNMP_VERSION_1 &&
343  context->response.errorStatus == SNMP_ERROR_TOO_BIG)
344  {
345  //The alternate Response-PDU is formatted with the same value in its
346  //request-id field as the received GetRequest-PDU and an empty
347  //variable-bindings field
348  context->response.varBindListLen = 0;
349  }
350  else
351  {
352  //The Response-PDU is re-formatted with the same values in its request-id
353  //and variable-bindings fields as the received GetRequest-PDU
354  error = snmpCopyVarBindingList(context);
355  //Any error to report?
356  if(error)
357  {
358  return error;
359  }
360  }
361  }
362 
363  //Successful processing
364  return NO_ERROR;
365 }
366 
367 
368 /**
369  * @brief Process GetBulkRequest-PDU
370  * @param[in] context Pointer to the SNMP agent context
371  * @return Error code
372  **/
373 
375 {
376 #if (SNMP_V2C_SUPPORT == ENABLED || SNMP_V3_SUPPORT == ENABLED)
377  error_t error;
378  int_t index;
379  size_t n;
380  size_t m;
381  size_t length;
382  bool_t endOfMibView;
383  const uint8_t *p;
384  const uint8_t *next;
385  SnmpVarBind var;
386 
387  //Debug message
388  TRACE_INFO("Parsing GetBulkRequest-PDU...\r\n");
389 
390  //Initialize variables
391  m = 0;
392  endOfMibView = FALSE;
393  next = NULL;
394 
395  //Make sure the SNMP version identifier is valid
396  if(context->request.version == SNMP_VERSION_1)
397  {
398  //The SNMP version is not acceptable
399  return ERROR_INVALID_TYPE;
400  }
401 
402  //Enforce access policy
403  if(context->user.mode != SNMP_ACCESS_READ_ONLY &&
404  context->user.mode != SNMP_ACCESS_READ_WRITE)
405  {
406  //Total number of SNMP messages delivered to the SNMP protocol entity
407  //which represented an SNMP operation which was not allowed by the SNMP
408  MIB2_SNMP_INC_COUNTER32(snmpInBadCommunityUses, 1);
409  SNMP_MIB_INC_COUNTER32(snmpGroup.snmpInBadCommunityUses, 1);
410 
411  //Report an error
412  return ERROR_ACCESS_DENIED;
413  }
414 
415  //Initialize response message
416  error = snmpInitResponse(context);
417  //Any error to report?
418  if(error)
419  {
420  return error;
421  }
422 
423  //Point to the first variable binding of the request
424  p = context->request.varBindList;
425  length = context->request.varBindListLen;
426 
427  //Lock access to MIB bases
428  snmpLockMib(context);
429 
430  //Loop through the list
431  for(index = 1; length > 0; index++)
432  {
433  //The non-repeaters field specifies the number of non-repeating objects
434  //at the start of the variable binding list
435  if((index - 1) == context->request.nonRepeaters)
436  {
437  //Pointer to the first variable binding that will be processed during
438  //the next iteration
439  next = context->response.varBindList + context->response.varBindListLen;
440 
441  //Actual size of the variable binding list
442  m = context->response.varBindListLen;
443 
444  //This flag tells whether all variable bindings have the value field
445  //set to endOfMibView for a given iteration
446  endOfMibView = TRUE;
447 
448  //If the max-repetitions field is zero, the list is trimmed to the
449  //first non-repeating variable bindings
450  if(context->request.maxRepetitions == 0)
451  {
452  break;
453  }
454  }
455 
456  //Parse variable binding
457  error = snmpParseVarBinding(p, length, &var, &n);
458  //Failed to parse variable binding?
459  if(error)
460  {
461  break;
462  }
463 
464  //Make sure that the object identifier is valid
465  error = oidCheck(var.oid, var.oidLen);
466  //Invalid object identifier?
467  if(error)
468  {
469  break;
470  }
471 
472  //Search the MIB for the next object
473  error = snmpGetNextObject(context, &context->request, &var);
474 
475  //Check status code
476  if(error == NO_ERROR)
477  {
478  //Next object found
479  endOfMibView = FALSE;
480  //Retrieve object value
481  error = snmpGetObjectValue(context, &context->request, &var);
482  }
483  else if(error == ERROR_OBJECT_NOT_FOUND)
484  {
485  //The variable binding's value field is set to endOfMibView
488  var.valueLen = 0;
489 
490  //Catch exception
491  error = NO_ERROR;
492  }
493  else
494  {
495  //Stop immediately
496  break;
497  }
498 
499  //Failed to retrieve object value?
500  if(error)
501  {
502  //Catch exception
503  if(error == ERROR_ACCESS_DENIED ||
504  error == ERROR_OBJECT_NOT_FOUND)
505  {
506  //The variable binding's value field is set to noSuchObject
509  var.valueLen = 0;
510  }
511  else if(error == ERROR_INSTANCE_NOT_FOUND)
512  {
513  //The variable binding's value field is set to noSuchInstance
516  var.valueLen = 0;
517  }
518  else
519  {
520  //Stop immediately
521  break;
522  }
523  }
524  else
525  {
526  //Total number of MIB objects which have been retrieved successfully
527  //by the SNMP protocol entity as the result of receiving valid SNMP
528  //Get-Request and Get-NextRequest PDUs
529  MIB2_SNMP_INC_COUNTER32(snmpInTotalReqVars, 1);
530  }
531 
532  //Append variable binding to the list
533  error = snmpWriteVarBinding(context, &var);
534  //Any error to report?
535  if(error)
536  {
537  break;
538  }
539 
540  //Advance data pointer
541  p += n;
542  length -= n;
543 
544  //Next iteration?
545  if(length == 0 && index > context->request.nonRepeaters)
546  {
547  //Decrement repeat counter
548  context->request.maxRepetitions--;
549 
550  //Last iteration?
551  if(!context->request.maxRepetitions)
552  {
553  break;
554  }
555 
556  //All variable bindings have the value field set to endOfMibView?
557  if(endOfMibView)
558  {
559  break;
560  }
561 
562  //Point to the first variable binding to be processed
563  p = next;
564  //Number of bytes to be processed
565  length = context->response.varBindListLen - m;
566  //Rewind index
567  index = context->request.nonRepeaters;
568  }
569  }
570 
571  //Unlock access to MIB bases
572  snmpUnlockMib(context);
573 
574  //Check status code
575  if(error == ERROR_BUFFER_OVERFLOW)
576  {
577  //If the size of the message containing the requested number of variable
578  //bindings would be greater than the maximum message size, then the
579  //response is generated with a lesser number of variable bindings
580  }
581  else if(error)
582  {
583  //Set error-status and error-index fields
584  error = snmpTranslateStatusCode(&context->response, error, index);
585  //If the parsing of the request fails, the SNMP agent discards the message
586  if(error)
587  {
588  return error;
589  }
590 
591  //The Response-PDU is re-formatted with the same values in its request-id
592  //and variable-bindings fields as the received GetRequest-PDU
593  error = snmpCopyVarBindingList(context);
594  //Any error to report?
595  if(error)
596  {
597  return error;
598  }
599  }
600 
601  //Successful processing
602  return NO_ERROR;
603 #else
604  //Not implemented
605  return ERROR_NOT_IMPLEMENTED;
606 #endif
607 }
608 
609 
610 /**
611  * @brief Process SetRequest-PDU
612  * @param[in] context Pointer to the SNMP agent context
613  * @return Error code
614  **/
615 
617 {
618  error_t error;
619  int_t index;
620  size_t n;
621  size_t length;
622  const uint8_t *p;
623  SnmpVarBind var;
624 
625  //Debug message
626  TRACE_INFO("Parsing SetRequest-PDU...\r\n");
627 
628  //Total number of SNMP Set-Request PDUs which have been accepted and
629  //processed by the SNMP protocol entity
630  MIB2_SNMP_INC_COUNTER32(snmpInSetRequests, 1);
631 
632  //Enforce access policy
633  if(context->user.mode != SNMP_ACCESS_WRITE_ONLY &&
634  context->user.mode != SNMP_ACCESS_READ_WRITE)
635  {
636  //Total number of SNMP messages delivered to the SNMP protocol entity
637  //which represented an SNMP operation which was not allowed by the SNMP
638  MIB2_SNMP_INC_COUNTER32(snmpInBadCommunityUses, 1);
639  SNMP_MIB_INC_COUNTER32(snmpGroup.snmpInBadCommunityUses, 1);
640 
641  //Report an error
642  return ERROR_ACCESS_DENIED;
643  }
644 
645  //Initialize response message
646  error = snmpInitResponse(context);
647  //Any error to report?
648  if(error)
649  {
650  return error;
651  }
652 
653  //The variable bindings are processed as a two phase operation. In the
654  //first phase, each variable binding is validated
655  p = context->request.varBindList;
656  length = context->request.varBindListLen;
657 
658  //Lock access to MIB bases
659  snmpLockMib(context);
660 
661  //Loop through the list
662  for(index = 1; length > 0; index++)
663  {
664  //Parse variable binding
665  error = snmpParseVarBinding(p, length, &var, &n);
666  //Failed to parse variable binding?
667  if(error)
668  {
669  break;
670  }
671 
672  //Assign object value
673  error = snmpSetObjectValue(context, &context->request, &var, FALSE);
674  //Any error to report?
675  if(error)
676  {
677  break;
678  }
679 
680  //Advance data pointer
681  p += n;
682  length -= n;
683  }
684 
685  //If all validations are successful, then each variable is altered in
686  //the second phase
687  if(!error)
688  {
689  //The changes are committed to the MIB base during the second phase
690  p = context->request.varBindList;
691  length = context->request.varBindListLen;
692 
693  //Loop through the list
694  for(index = 1; length > 0; index++)
695  {
696  //Parse variable binding
697  error = snmpParseVarBinding(p, length, &var, &n);
698  //Failed to parse variable binding?
699  if(error)
700  {
701  break;
702  }
703 
704  //Assign object value
705  error = snmpSetObjectValue(context, &context->request, &var, TRUE);
706  //Any error to report?
707  if(error)
708  {
709  break;
710  }
711 
712  //Total number of MIB objects which have been altered successfully
713  //by the SNMP protocol entity as the result of receiving valid
714  //SNMP Set-Request PDUs
715  MIB2_SNMP_INC_COUNTER32(snmpInTotalSetVars, 1);
716 
717  //Advance data pointer
718  p += n;
719  length -= n;
720  }
721  }
722 
723  //Unlock access to MIB bases
724  snmpUnlockMib(context);
725 
726  //Any error to report?
727  if(error)
728  {
729  //Set error-status and error-index fields
730  error = snmpTranslateStatusCode(&context->response, error, index);
731  //If the parsing of the request fails, the SNMP agent discards the message
732  if(error)
733  {
734  return error;
735  }
736  }
737 
738  //The SNMP agent sends back a GetResponse-PDU of identical form
739  error = snmpCopyVarBindingList(context);
740  //Return status code
741  return error;
742 }
743 
744 
745 /**
746  * @brief Format Report-PDU
747  * @param[in] context Pointer to the SNMP agent context
748  * @param[in] errorIndication Error indication
749  * @return Error code
750  **/
751 
753 {
754  error_t error;
755 
756 #if (SNMP_V3_SUPPORT == ENABLED)
757  size_t n;
758  uint32_t counter;
759  SnmpVarBind var;
760 
761  //Initialize SNMP message
762  snmpInitMessage(&context->response);
763 
764  //SNMP version identifier
765  context->response.version = context->request.version;
766 
767  //Message identifier
768  context->response.msgId = context->request.msgId;
769  //Maximum message size supported by the sender
770  context->response.msgMaxSize = SNMP_MAX_MSG_SIZE;
771  //Bit fields which control processing of the message
772  context->response.msgFlags = 0;
773  //Security model used by the sender
774  context->response.msgSecurityModel = SNMP_SECURITY_MODEL_USM;
775 
776  //Authoritative engine identifier
777  context->response.msgAuthEngineId = context->contextEngine;
778  context->response.msgAuthEngineIdLen = context->contextEngineLen;
779 
780  //Number of times the SNMP engine has rebooted
781  context->response.msgAuthEngineBoots = context->engineBoots;
782  //Number of seconds since last reboot
783  context->response.msgAuthEngineTime = context->engineTime;
784 
785  //Context engine identifier
786  context->response.contextEngineId = context->contextEngine;
787  context->response.contextEngineIdLen = context->contextEngineLen;
788 
789  //Context name
790  context->response.contextName = context->contextName;
791  context->response.contextNameLen = osStrlen(context->contextName);
792 
793  //PDU type
794  context->response.pduType = SNMP_PDU_REPORT;
795  //Request identifier
796  context->response.requestId = context->request.requestId;
797 
798  //If the message is considered to be outside of the time window, the error
799  //must be reported with a securityLevel of authNoPriv (refer to RFC 3414,
800  //section 3.2)
801  if(errorIndication == ERROR_NOT_IN_TIME_WINDOW)
802  {
803  //Bit fields which control processing of the message
804  context->response.msgFlags = context->request.msgFlags &
806 
807  //User name
808  context->response.msgUserName = context->request.msgUserName;
809  context->response.msgUserNameLen = context->request.msgUserNameLen;
810 
811  //Authentication parameters
812  context->response.msgAuthParameters = NULL;
813  context->response.msgAuthParametersLen = context->request.msgAuthParametersLen;
814 
815  //Privacy parameters
816  context->response.msgPrivParameters = context->privParameters;
817  context->response.msgPrivParametersLen = context->request.msgPrivParametersLen;
818  }
819 
820  //Make room for the message header at the beginning of the buffer
821  error = snmpComputeMessageOverhead(&context->response);
822  //Any error to report?
823  if(error)
824  {
825  return error;
826  }
827 
828  //Initialize counter value
829  counter = 1;
830 
831  //Check error indication
832  switch(errorIndication)
833  {
835  //Total number of packets received by the SNMP engine which were dropped
836  //because they requested a securityLevel that was unknown to the SNMP
837  //engine or otherwise unavailable
838  SNMP_USM_MIB_INC_COUNTER32(usmStatsUnsupportedSecLevels, 1);
839  SNMP_USM_MIB_GET_COUNTER32(counter, usmStatsUnsupportedSecLevels);
840 
841  //Add the usmStatsUnsupportedSecLevels counter in the varBindList
844  break;
845 
847  //Total number of packets received by the SNMP engine which were dropped
848  //because they appeared outside of the authoritative SNMP engine's window
849  SNMP_USM_MIB_INC_COUNTER32(usmStatsNotInTimeWindows, 1);
850  SNMP_USM_MIB_GET_COUNTER32(counter, usmStatsNotInTimeWindows);
851 
852  //Add the usmStatsNotInTimeWindows counter in the varBindList
855  break;
856 
858  //Total number of packets received by the SNMP engine which were dropped
859  //because they referenced a user that was not known to the SNMP engine
860  SNMP_USM_MIB_INC_COUNTER32(usmStatsUnknownUserNames, 1);
861  SNMP_USM_MIB_GET_COUNTER32(counter, usmStatsUnknownUserNames);
862 
863  //Add the usmStatsUnknownUserNames counter in the varBindList
866  break;
867 
869  //Total number of packets received by the SNMP engine which were dropped
870  //because they referenced an snmpEngineID that was not known to the SNMP
871  //engine
872  SNMP_USM_MIB_INC_COUNTER32(usmStatsUnknownEngineIDs, 1);
873  SNMP_USM_MIB_GET_COUNTER32(counter, usmStatsUnknownEngineIDs);
874 
875  //Add the usmStatsUnknownEngineIDs counter in the varBindList
878  break;
879 
881  //Total number of packets received by the SNMP engine which were dropped
882  //because they didn't contain the expected digest value
883  SNMP_USM_MIB_INC_COUNTER32(usmStatsWrongDigests, 1);
884  SNMP_USM_MIB_GET_COUNTER32(counter, usmStatsWrongDigests);
885 
886  //Add the usmStatsWrongDigests counter in the varBindList
888  var.oidLen = sizeof(usmStatsWrongDigestsObject);
889  break;
890 
892  //Total number of packets received by the SNMP engine which were dropped
893  //because they could not be decrypted
894  SNMP_USM_MIB_INC_COUNTER32(usmStatsDecryptionErrors, 1);
895  SNMP_USM_MIB_GET_COUNTER32(counter, usmStatsDecryptionErrors);
896 
897  //Add the usmStatsDecryptionErrors counter in the varBindList
900  break;
901 
903  //Total number of packets received by the SNMP engine which were dropped
904  //because the context contained in the message was unavailable
905  counter = 1;
906 
907  //Add the snmpUnavailableContexts counter in the varBindList
910  break;
911 
913  //Total number of packets received by the SNMP engine which were dropped
914  //because the context contained in the message was unknown
915  counter = 1;
916 
917  //Add the snmpUnknownContexts counter in the varBindList
919  var.oidLen = sizeof(snmpUnknownContextsObject);
920  break;
921 
922  default:
923  //Just for sanity's sake...
924  var.oid = NULL;
925  var.oidLen = 0;
926  break;
927  }
928 
929  //Encode the object value using ASN.1 rules
930  error = snmpEncodeUnsignedInt32(counter, context->response.buffer, &n);
931  //Any error to report?
932  if(error)
933  {
934  return error;
935  }
936 
937  //The counter is encoded in ASN.1 format
940  var.value = context->response.buffer;
941  var.valueLen = n;
942 
943  //Append the variable binding list to the varBindList
944  error = snmpWriteVarBinding(context, &var);
945  //Any error to report?
946  if(error)
947  {
948  return error;
949  }
950 
951  //Format PDU header
952  error = snmpWritePduHeader(&context->response);
953 #else
954  //SNMPv3 is not supported
955  error = ERROR_NOT_IMPLEMENTED;
956 #endif
957 
958  //Return status code
959  return error;
960 }
961 
962 #endif
@ ERROR_UNKNOWN_ENGINE_ID
Definition: error.h:263
MIB-II module.
error_t snmpProcessReportPdu(SnmpAgentContext *context)
Process Report-PDU.
error_t snmpProcessPdu(SnmpAgentContext *context)
Process PDU.
@ ERROR_UNKNOWN_USER_NAME
Definition: error.h:264
int bool_t
Definition: compiler_port.h:63
#define SNMP_USM_MIB_INC_COUNTER32(name, value)
@ SNMP_PDU_GET_REQUEST
Definition: snmp_common.h:150
error_t snmpEncodeUnsignedInt32(uint32_t value, uint8_t *dest, size_t *length)
Encode a 32-bit unsigned integer.
@ ERROR_UNKNOWN_CONTEXT
Definition: error.h:265
const uint8_t * value
@ SNMP_ACCESS_READ_WRITE
Variable binding.
signed int int_t
Definition: compiler_port.h:56
@ SNMP_EXCEPTION_END_OF_MIB_VIEW
Definition: snmp_common.h:214
@ ERROR_BUFFER_OVERFLOW
Definition: error.h:143
@ SNMP_ERROR_TOO_BIG
Definition: snmp_common.h:185
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
@ ERROR_DECRYPTION_FAILED
Definition: error.h:244
OID (Object Identifier)
uint8_t p
Definition: ndp.h:300
@ SNMP_PDU_GET_RESPONSE
Definition: snmp_common.h:152
@ SNMP_PDU_SET_REQUEST
Definition: snmp_common.h:153
#define SNMP_USM_MIB_GET_COUNTER32(value, name)
#define TRUE
Definition: os_port.h:50
error_t snmpParsePduHeader(SnmpMessage *message)
Parse PDU header.
error_t snmpCopyVarBindingList(SnmpAgentContext *context)
Copy the list of variable bindings.
error_t snmpSetObjectValue(SnmpAgentContext *context, const SnmpMessage *message, SnmpVarBind *var, bool_t commit)
Assign object value.
@ SNMP_SECURITY_MODEL_USM
User-based security model.
@ SNMP_EXCEPTION_NO_SUCH_OBJECT
Definition: snmp_common.h:212
#define osStrlen(s)
Definition: os_port.h:171
#define SNMP_MIB_INC_COUNTER32(name, value)
error_t snmpProcessGetRequestPdu(SnmpAgentContext *context)
Process GetRequest-PDU or GetNextRequest-PDU.
SNMP MIB module.
const uint8_t snmpUnknownContextsObject[9]
SNMP agent (Simple Network Management Protocol)
@ SNMP_MSG_FLAG_PRIV
error_t snmpTranslateStatusCode(SnmpMessage *message, error_t status, uint_t index)
Translate status code.
error_t snmpFormatReportPdu(SnmpAgentContext *context, error_t errorIndication)
Format Report-PDU.
const uint8_t usmStatsNotInTimeWindowsObject[10]
error_t snmpWriteVarBinding(SnmpAgentContext *context, const SnmpVarBind *var)
Write variable binding.
#define FALSE
Definition: os_port.h:46
@ MIB_TYPE_COUNTER32
Definition: mib_common.h:61
error_t snmpInitResponse(SnmpAgentContext *context)
Initialize a GetResponse-PDU.
const uint8_t * oid
error_t
Error codes.
Definition: error.h:43
@ SNMP_ACCESS_READ_ONLY
@ ERROR_INSTANCE_NOT_FOUND
Definition: error.h:259
#define MIB2_SNMP_INC_COUNTER32(name, value)
Definition: mib2_module.h:169
error_t snmpGetNextObject(SnmpAgentContext *context, const SnmpMessage *message, SnmpVarBind *var)
Search MIBs for the next object.
@ ERROR_UNAVAILABLE_CONTEXT
Definition: error.h:266
@ ERROR_NOT_IN_TIME_WINDOW
Definition: error.h:268
@ SNMP_PDU_GET_BULK_REQUEST
Definition: snmp_common.h:155
General definitions for cryptographic library.
const uint8_t usmStatsWrongDigestsObject[10]
Helper functions for SNMP agent.
const uint8_t usmStatsUnknownEngineIdsObject[10]
void snmpLockMib(SnmpAgentContext *context)
Lock MIB bases.
@ ERROR_ACCESS_DENIED
Definition: error.h:149
@ ERROR_INVALID_TYPE
Definition: error.h:115
@ SNMP_MSG_FLAG_AUTH
#define TRACE_INFO(...)
Definition: debug.h:105
uint8_t length
Definition: tcp.h:375
const uint8_t snmpUnavailableContextsObject[9]
error_t snmpProcessSetRequestPdu(SnmpAgentContext *context)
Process SetRequest-PDU.
@ SNMP_ACCESS_WRITE_ONLY
@ SNMP_PDU_GET_NEXT_REQUEST
Definition: snmp_common.h:151
@ SNMP_EXCEPTION_NO_SUCH_INSTANCE
Definition: snmp_common.h:213
error_t oidCheck(const uint8_t *oid, size_t oidLen)
Check whether the specified object identifier is valid.
Definition: oid.c:50
@ SNMP_PDU_REPORT
Definition: snmp_common.h:158
uint8_t m
Definition: ndp.h:304
#define SNMP_MAX_MSG_SIZE
Definition: snmp_common.h:60
uint8_t n
error_t snmpParseVarBinding(const uint8_t *p, size_t length, SnmpVarBind *var, size_t *consumed)
Parse variable binding.
const uint8_t usmStatsDecryptionErrorsObject[10]
@ ERROR_AUTHENTICATION_FAILED
Definition: error.h:69
error_t snmpWritePduHeader(SnmpMessage *message)
Format PDU header.
@ ERROR_OBJECT_NOT_FOUND
Definition: error.h:258
#define ASN1_CLASS_CONTEXT_SPECIFIC
Definition: asn1.h:54
@ SNMP_VERSION_1
Definition: snmp_common.h:138
error_t snmpProcessGetResponsePdu(SnmpAgentContext *context)
Process GetResponse-PDU.
#define SnmpAgentContext
Definition: snmp_agent.h:36
MIB object access.
error_t snmpGetObjectValue(SnmpAgentContext *context, const SnmpMessage *message, SnmpVarBind *var)
Retrieve object value.
SNMP USM MIB module.
SNMP agent (PDU processing)
error_t snmpComputeMessageOverhead(SnmpMessage *message)
Compute SNMP message overhead.
const uint8_t usmStatsUnsupportedSecLevelsObject[10]
error_t snmpProcessGetBulkRequestPdu(SnmpAgentContext *context)
Process GetBulkRequest-PDU.
TCP/IP stack core.
#define ASN1_CLASS_APPLICATION
Definition: asn1.h:53
uint16_t next
Definition: ipv4_frag.h:106
void snmpUnlockMib(SnmpAgentContext *context)
Unlock MIB bases.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
ASN.1 (Abstract Syntax Notation One)
void snmpInitMessage(SnmpMessage *message)
Initialize a SNMP message.
const uint8_t usmStatsUnknownUserNamesObject[10]
@ ERROR_UNSUPPORTED_SECURITY_LEVEL
Definition: error.h:267