snmp_agent.c
Go to the documentation of this file.
1 /**
2  * @file snmp_agent.c
3  * @brief SNMP agent (Simple Network Management Protocol)
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  * @section Description
28  *
29  * SNMP is a simple protocol by which management information for a network
30  * element may be inspected or altered by logically remote users. Refer
31  * to the following RFCs for complete details:
32  * - RFC 1157: A Simple Network Management Protocol (SNMP)
33  * - RFC 1905: Protocol Operations for Version 2 of the Simple Network
34  * Management Protocol (SNMPv2)
35  * - RFC 3410: Introduction and Applicability Statements for Internet
36  * Standard Management Framework
37  * - RFC 3411: An Architecture for Describing SNMP Management Frameworks
38  * - RFC 3412: Message Processing and Dispatching for the SNMP
39  * - RFC 3413: Simple Network Management Protocol (SNMP) Applications
40  * - RFC 3584: Coexistence between Version 1, Version 2, and Version 3 of
41  * SNMP Framework
42  *
43  * @author Oryx Embedded SARL (www.oryx-embedded.com)
44  * @version 2.6.8
45  **/
46 
47 //Switch to the appropriate trace level
48 #define TRACE_LEVEL SNMP_TRACE_LEVEL
49 
50 //Dependencies
51 #include "core/net.h"
52 #include "snmp/snmp_agent.h"
54 #include "snmp/snmp_agent_pdu.h"
55 #include "snmp/snmp_agent_misc.h"
56 #include "snmp/snmp_agent_trap.h"
57 #include "snmp/snmp_agent_inform.h"
58 #include "mibs/mib2_module.h"
59 #include "core/crypto.h"
60 #include "encoding/asn1.h"
61 #include "encoding/oid.h"
62 #include "debug.h"
63 
64 //Check TCP/IP stack configuration
65 #if (SNMP_AGENT_SUPPORT == ENABLED)
66 
67 
68 /**
69  * @brief Initialize settings with default values
70  * @param[out] settings Structure that contains SNMP agent settings
71  **/
72 
74 {
75  //Default task parameters
76  settings->task = OS_TASK_DEFAULT_PARAMS;
78  settings->task.priority = SNMP_AGENT_PRIORITY;
79 
80  //TCP/IP stack context
81  settings->netContext = NULL;
82  //The SNMP agent is not bound to any interface
83  settings->interface = NULL;
84 
85  //Minimum version accepted by the SNMP agent
86  settings->versionMin = SNMP_VERSION_1;
87  //Maximum version accepted by the SNMP agent
88  settings->versionMax = SNMP_VERSION_3;
89 
90  //SNMP port number
91  settings->port = SNMP_PORT;
92  //SNMP trap port number
93  settings->trapPort = SNMP_TRAP_PORT;
94 
95  //Random data generation callback function
96  settings->randCallback = NULL;
97 }
98 
99 
100 /**
101  * @brief SNMP agent initialization
102  * @param[in] context Pointer to the SNMP agent context
103  * @param[in] settings SNMP agent specific settings
104  * @return Error code
105  **/
106 
108  const SnmpAgentSettings *settings)
109 {
110  error_t error;
111 
112  //Debug message
113  TRACE_INFO("Initializing SNMP agent...\r\n");
114 
115  //Ensure the parameters are valid
116  if(context == NULL || settings == NULL)
117  {
119  }
120 
121  //Check minimum and maximum SNMP versions
122  if(settings->versionMin > settings->versionMax)
123  {
125  }
126 
127  //Clear the SNMP agent context
128  osMemset(context, 0, sizeof(SnmpAgentContext));
129 
130  //Initialize task parameters
131  context->taskParams = settings->task;
132  context->taskId = OS_INVALID_TASK_ID;
133 
134  //Attach TCP/IP stack context
135  if(settings->netContext != NULL)
136  {
137  context->netContext = settings->netContext;
138  }
139  else if(settings->interface != NULL)
140  {
141  context->netContext = settings->interface->netContext;
142  }
143  else
144  {
145  context->netContext = netGetDefaultContext();
146  }
147 
148  //Save user settings
149  context->interface = settings->interface;
150  context->versionMin = settings->versionMin;
151  context->versionMax = settings->versionMax;
152  context->port = settings->port;
153  context->trapPort = settings->trapPort;
154  context->randCallback = settings->randCallback;
155 
156  //Initialize request identifier
157  context->requestId = netGetRandRange(context->netContext, 1, INT32_MAX);
158 
159  //Start of exception handling block
160  do
161  {
162  //Create a mutex to prevent simultaneous access to SNMP agent context
163  if(!osCreateMutex(&context->mutex))
164  {
165  //Failed to create mutex
166  error = ERROR_OUT_OF_RESOURCES;
167  break;
168  }
169 
170  //Create an event object to poll the state of the UDP socket
171  if(!osCreateEvent(&context->event))
172  {
173  //Failed to create event
174  error = ERROR_OUT_OF_RESOURCES;
175  break;
176  }
177 
178 #if (SNMP_AGENT_INFORM_SUPPORT == ENABLED)
179  //Create an event object to manage inform request retransmissions
180  if(!osCreateEvent(&context->informEvent))
181  {
182  //Failed to create event
183  error = ERROR_OUT_OF_RESOURCES;
184  break;
185  }
186 #endif
187 
188 #if (SNMP_V3_SUPPORT == ENABLED)
189  //Get current time
190  context->systemTime = osGetSystemTime();
191 
192  //Each SNMP engine maintains 2 values, snmpEngineBoots and snmpEngineTime,
193  //which taken together provide an indication of time at that SNMP engine
194  context->engineBoots = 1;
195  context->engineTime = 0;
196 
197  //Initialize message identifier
198  context->msgId = netGetRandRange(context->netContext, 1, INT32_MAX);
199 
200  //Check whether SNMPv3 is supported
201  if(settings->versionMin <= SNMP_VERSION_3 &&
202  settings->versionMax >= SNMP_VERSION_3)
203  {
204  //Make sure a random number generator has been registered
205  if(settings->randCallback == NULL)
206  {
207  //Report en error
208  error = ERROR_INVALID_PARAMETER;
209  break;
210  }
211 
212  //The salt integer is initialized to an arbitrary value at boot time
213  error = settings->randCallback(context->salt, sizeof(context->salt));
214  //Any error to report?
215  if(error)
216  {
217  break;
218  }
219  }
220 #endif
221 
222  //Successful initialization
223  error = NO_ERROR;
224 
225  //End of exception handling block
226  } while(0);
227 
228  //Any error to report?
229  if(error)
230  {
231  //Clean up side effects
232  snmpAgentDeinit(context);
233  }
234 
235  //Return status code
236  return error;
237 }
238 
239 
240 /**
241  * @brief Start SNMP agent
242  * @param[in] context Pointer to the SNMP agent context
243  * @return Error code
244  **/
245 
247 {
248  error_t error;
249 
250  //Make sure the SNMP agent context is valid
251  if(context == NULL)
252  {
254  }
255 
256  //Debug message
257  TRACE_INFO("Starting SNMP agent...\r\n");
258 
259  //Make sure the SNMP agent is not already running
260  if(context->running)
261  {
262  return ERROR_ALREADY_RUNNING;
263  }
264 
265  //Start of exception handling block
266  do
267  {
268  //Open a UDP socket
269  context->socket = socketOpenEx(context->netContext, SOCKET_TYPE_DGRAM,
271  //Failed to open socket?
272  if(context->socket == NULL)
273  {
274  error = ERROR_OPEN_FAILED;
275  break;
276  }
277 
278  //Force the socket to operate in non-blocking mode
279  error = socketSetTimeout(context->socket, 0);
280  //Any error to report?
281  if(error)
282  {
283  break;
284  }
285 
286  //Associate the socket with the relevant interface
287  error = socketBindToInterface(context->socket, context->interface);
288  //Unable to bind the socket to the desired interface?
289  if(error)
290  {
291  break;
292  }
293 
294  //The SNMP agent listens for messages on port 161
295  error = socketBind(context->socket, &IP_ADDR_ANY, context->port);
296  //Unable to bind the socket to the desired port?
297  if(error)
298  {
299  break;
300  }
301 
302  //Start the SNMP agent
303  context->stop = FALSE;
304  context->running = TRUE;
305 
306  //Create a task
307  context->taskId = osCreateTask("SNMP Agent", (OsTaskCode) snmpAgentTask,
308  context, &context->taskParams);
309 
310  //Failed to create task?
311  if(context->taskId == OS_INVALID_TASK_ID)
312  {
313  error = ERROR_OUT_OF_RESOURCES;
314  break;
315  }
316 
317  //End of exception handling block
318  } while(0);
319 
320  //Any error to report?
321  if(error)
322  {
323  //Clean up side effects
324  context->running = FALSE;
325 
326  //Close the UDP socket
327  socketClose(context->socket);
328  context->socket = NULL;
329  }
330 
331  //Return status code
332  return error;
333 }
334 
335 
336 /**
337  * @brief Stop SNMP agent
338  * @param[in] context Pointer to the SNMP agent context
339  * @return Error code
340  **/
341 
343 {
344  //Make sure the SNMP agent context is valid
345  if(context == NULL)
346  {
348  }
349 
350  //Debug message
351  TRACE_INFO("Stopping SNMP agent...\r\n");
352 
353  //Check whether the SNMP agent is running
354  if(context->running)
355  {
356 #if (NET_RTOS_SUPPORT == ENABLED)
357  //Stop the SNMP agent
358  context->stop = TRUE;
359  //Send a signal to the task to abort any blocking operation
360  osSetEvent(&context->event);
361 
362  //Wait for the task to terminate
363  while(context->running)
364  {
365  osDelayTask(1);
366  }
367 #endif
368 
369  //Close the UDP socket
370  socketClose(context->socket);
371  context->socket = NULL;
372  }
373 
374  //Successful processing
375  return NO_ERROR;
376 }
377 
378 
379 /**
380  * @brief Load a MIB module
381  * @param[in] context Pointer to the SNMP agent context
382  * @param[in] module Pointer the MIB module to be loaded
383  * @return Error code
384  **/
385 
387 {
388  error_t error;
389  uint_t i;
390 
391  //Check parameters
392  if(context == NULL || module == NULL)
393  {
395  }
396 
397  if(module->numObjects < 1)
398  {
400  }
401 
402  //Acquire exclusive access to the SNMP agent context
403  osAcquireMutex(&context->mutex);
404 
405  //Loop through existing MIBs
406  for(i = 0; i < SNMP_AGENT_MAX_MIBS; i++)
407  {
408  //Check whether the specified MIB module is already loaded
409  if(context->mibTable[i] == module)
410  {
411  break;
412  }
413  }
414 
415  //MIB module found?
416  if(i < SNMP_AGENT_MAX_MIBS)
417  {
418  //Prevent the SNMP agent from loading the same MIB multiple times
419  error = NO_ERROR;
420  }
421  else
422  {
423  //Loop through existing MIBs
424  for(i = 0; i < SNMP_AGENT_MAX_MIBS; i++)
425  {
426  //Check if the current entry is available
427  if(context->mibTable[i] == NULL)
428  {
429  break;
430  }
431  }
432 
433  //Make sure there is enough room to add the specified MIB
434  if(i < SNMP_AGENT_MAX_MIBS)
435  {
436  //Invoke user callback, if any
437  if(module->load != NULL)
438  {
439  error = module->load(context);
440  }
441  else
442  {
443  error = NO_ERROR;
444  }
445 
446  //Check status code
447  if(!error)
448  {
449  //Add the MIB to the list
450  context->mibTable[i] = module;
451  }
452  }
453  else
454  {
455  //Failed to load the specified MIB
456  error = ERROR_OUT_OF_RESOURCES;
457  }
458  }
459 
460  //Release exclusive access to the SNMP agent context
461  osReleaseMutex(&context->mutex);
462 
463  //Return status code
464  return error;
465 }
466 
467 
468 /**
469  * @brief Unload a MIB module
470  * @param[in] context Pointer to the SNMP agent context
471  * @param[in] module Pointer the MIB module to be unloaded
472  * @return Error code
473  **/
474 
476 {
477  error_t error;
478  uint_t i;
479 
480  //Check parameters
481  if(context == NULL || module == NULL)
482  {
484  }
485 
486  //Acquire exclusive access to the SNMP agent context
487  osAcquireMutex(&context->mutex);
488 
489  //Loop through existing MIBs
490  for(i = 0; i < SNMP_AGENT_MAX_MIBS; i++)
491  {
492  //Check whether the specified MIB module is already loaded
493  if(context->mibTable[i] == module)
494  {
495  break;
496  }
497  }
498 
499  //MIB module found?
500  if(i < SNMP_AGENT_MAX_MIBS)
501  {
502  //Any registered callback?
503  if(context->mibTable[i]->unload != NULL)
504  {
505  //Invoke user callback function
506  context->mibTable[i]->unload(context);
507  }
508 
509  //Remove the MIB from the list
510  context->mibTable[i] = NULL;
511 
512  //Successful processing
513  error = NO_ERROR;
514  }
515  else
516  {
517  //Failed to unload the specified MIB
518  error = ERROR_NOT_FOUND;
519  }
520 
521  //Release exclusive access to the SNMP agent context
522  osReleaseMutex(&context->mutex);
523 
524  //Return status code
525  return error;
526 }
527 
528 
529 /**
530  * @brief Set minimum and maximum versions permitted
531  * @param[in] context Pointer to the SNMP agent context
532  * @param[in] versionMin Minimum version accepted by the SNMP agent
533  * @param[in] versionMax Maximum version accepted by the SNMP agent
534  * @return Error code
535  **/
536 
538  SnmpVersion versionMin, SnmpVersion versionMax)
539 {
540  //Check parameters
541  if(context == NULL)
542  {
544  }
545 
546  if(versionMin > versionMax)
547  {
549  }
550 
551  //Acquire exclusive access to the SNMP agent context
552  osAcquireMutex(&context->mutex);
553 
554  //Set minimum and maximum versions permitted
555  context->versionMin = versionMin;
556  context->versionMax = versionMax;
557 
558  //Release exclusive access to the SNMP agent context
559  osReleaseMutex(&context->mutex);
560 
561  //Successful processing
562  return NO_ERROR;
563 }
564 
565 
566 /**
567  * @brief Set the value of the snmpEngineBoots variable
568  * @param[in] context Pointer to the SNMP agent context
569  * @param[in] engineBoots Number of times the SNMP engine has re-booted
570  * @return Error code
571  **/
572 
573 error_t snmpAgentSetEngineBoots(SnmpAgentContext *context, int32_t engineBoots)
574 {
575 #if (SNMP_V3_SUPPORT == ENABLED)
576  //Check parameters
577  if(context == NULL)
578  {
580  }
581 
582  if(engineBoots < 0)
583  {
584  return ERROR_OUT_OF_RANGE;
585  }
586 
587  //Acquire exclusive access to the SNMP agent context
588  osAcquireMutex(&context->mutex);
589 
590  //Get current time
591  context->systemTime = osGetSystemTime();
592 
593  //Set the value of the snmpEngineBoots
594  context->engineBoots = engineBoots;
595  //The snmpEngineTime is reset to zero
596  context->engineTime = 0;
597 
598  //Release exclusive access to the SNMP agent context
599  osReleaseMutex(&context->mutex);
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 Get the value of the snmpEngineBoots variable
612  * @param[in] context Pointer to the SNMP agent context
613  * @param[out] engineBoots Number of times the SNMP engine has re-booted
614  * @return Error code
615  **/
616 
617 error_t snmpAgentGetEngineBoots(SnmpAgentContext *context, int32_t *engineBoots)
618 {
619 #if (SNMP_V3_SUPPORT == ENABLED)
620  //Check parameters
621  if(context == NULL || engineBoots == NULL)
622  {
624  }
625 
626  //Acquire exclusive access to the SNMP agent context
627  osAcquireMutex(&context->mutex);
628  //Get the current value of the snmpEngineBoots
629  *engineBoots = context->engineBoots;
630  //Release exclusive access to the SNMP agent context
631  osReleaseMutex(&context->mutex);
632 
633  //Successful processing
634  return NO_ERROR;
635 #else
636  //Not implemented
637  return ERROR_NOT_IMPLEMENTED;
638 #endif
639 }
640 
641 
642 /**
643  * @brief Set enterprise OID
644  * @param[in] context Pointer to the SNMP agent context
645  * @param[in] enterpriseOid Pointer to the enterprise OID
646  * @param[in] enterpriseOidLen Length of the enterprise OID
647  * @return Error code
648  **/
649 
651  const uint8_t *enterpriseOid, size_t enterpriseOidLen)
652 {
653  //Check parameters
654  if(context == NULL || enterpriseOid == NULL)
655  {
657  }
658 
659  if(enterpriseOidLen > SNMP_MAX_OID_SIZE)
660  {
662  }
663 
664  //Acquire exclusive access to the SNMP agent context
665  osAcquireMutex(&context->mutex);
666 
667  //Set enterprise OID
668  osMemcpy(context->enterpriseOid, enterpriseOid, enterpriseOidLen);
669  //Save the length of the enterprise OID
670  context->enterpriseOidLen = enterpriseOidLen;
671 
672  //Release exclusive access to the SNMP agent context
673  osReleaseMutex(&context->mutex);
674 
675  //Successful processing
676  return NO_ERROR;
677 }
678 
679 
680 /**
681  * @brief Set context engine identifier
682  * @param[in] context Pointer to the SNMP agent context
683  * @param[in] contextEngine Pointer to the context engine identifier
684  * @param[in] contextEngineLen Length of the context engine identifier
685  * @return Error code
686  **/
687 
689  const void *contextEngine, size_t contextEngineLen)
690 {
691 #if (SNMP_V3_SUPPORT == ENABLED)
692  //Check parameters
693  if(context == NULL || contextEngine == NULL)
694  {
696  }
697 
698  if(contextEngineLen > SNMP_MAX_CONTEXT_ENGINE_SIZE)
699  {
701  }
702 
703  //Acquire exclusive access to the SNMP agent context
704  osAcquireMutex(&context->mutex);
705 
706  //Set context engine identifier
707  osMemcpy(context->contextEngine, contextEngine, contextEngineLen);
708  //Save the length of the context engine identifier
709  context->contextEngineLen = contextEngineLen;
710 
711  //Release exclusive access to the SNMP agent context
712  osReleaseMutex(&context->mutex);
713 
714  //Successful processing
715  return NO_ERROR;
716 #else
717  //Not implemented
718  return ERROR_NOT_IMPLEMENTED;
719 #endif
720 }
721 
722 
723 /**
724  * @brief Set context name
725  * @param[in] context Pointer to the SNMP agent context
726  * @param[in] contextName NULL-terminated string that contains the context name
727  * @return Error code
728  **/
729 
731  const char_t *contextName)
732 {
733 #if (SNMP_V3_SUPPORT == ENABLED)
734  size_t n;
735 
736  //Check parameters
737  if(context == NULL || contextName == NULL)
738  {
740  }
741 
742  //Retrieve the length of the context name
743  n = osStrlen(contextName);
744 
745  //Make sure the context name is valid
747  {
748  return ERROR_INVALID_LENGTH;
749  }
750 
751  //Acquire exclusive access to the SNMP agent context
752  osAcquireMutex(&context->mutex);
753  //Set context name
754  osStrcpy(context->contextName, contextName);
755  //Release exclusive access to the SNMP agent context
756  osReleaseMutex(&context->mutex);
757 
758  //Successful processing
759  return NO_ERROR;
760 #else
761  //Not implemented
762  return ERROR_NOT_IMPLEMENTED;
763 #endif
764 }
765 
766 
767 /**
768  * @brief Create a new community string
769  * @param[in] context Pointer to the SNMP agent context
770  * @param[in] community NULL-terminated string that contains the community name
771  * @param[in] mode Access rights
772  * @return Error code
773  **/
774 
776  const char_t *community, SnmpAccess mode)
777 {
778 #if (SNMP_V1_SUPPORT == ENABLED || SNMP_V2C_SUPPORT == ENABLED)
779  error_t error;
780  size_t n;
781  SnmpUserEntry *entry;
782 
783  //Check parameters
784  if(context == NULL || community == NULL)
785  {
787  }
788 
789  //Retrieve the length of the community string
790  n = osStrlen(community);
791 
792  //Make sure the community string is valid
793  if(n == 0 || n > SNMP_MAX_USER_NAME_LEN)
794  {
795  return ERROR_INVALID_LENGTH;
796  }
797 
798  //Acquire exclusive access to the SNMP agent context
799  osAcquireMutex(&context->mutex);
800 
801  //Check whether the community string already exists
802  entry = snmpFindCommunityEntry(context, community, osStrlen(community));
803 
804  //If the specified community string does not exist, then a new entry
805  //should be created
806  if(entry == NULL)
807  {
808  //Create a new entry
809  entry = snmpCreateCommunityEntry(context);
810  }
811 
812  //Any entry available?
813  if(entry != NULL)
814  {
815  //Clear the contents
816  osMemset(entry, 0, sizeof(SnmpUserEntry));
817 
818  //Save community string
819  osStrcpy(entry->name, community);
820  //Set access rights
821  entry->mode = mode;
822  //The entry is now available for use
823  entry->status = MIB_ROW_STATUS_ACTIVE;
824 
825  //Successful processing
826  error = NO_ERROR;
827  }
828  else
829  {
830  //The table runs out of space
831  error = ERROR_OUT_OF_RESOURCES;
832  }
833 
834  //Release exclusive access to the SNMP agent context
835  osReleaseMutex(&context->mutex);
836 
837  //Return error code
838  return error;
839 #else
840  //Not implemented
841  return ERROR_NOT_IMPLEMENTED;
842 #endif
843 }
844 
845 
846 /**
847  * @brief Remove a community string
848  * @param[in] context Pointer to the SNMP agent context
849  * @param[in] community NULL-terminated string that contains the community name
850  * @return Error code
851  **/
852 
854 {
855 #if (SNMP_V1_SUPPORT == ENABLED || SNMP_V2C_SUPPORT == ENABLED)
856  error_t error;
857  SnmpUserEntry *entry;
858 
859  //Check parameters
860  if(context == NULL || community == NULL)
861  {
863  }
864 
865  //Acquire exclusive access to the SNMP agent context
866  osAcquireMutex(&context->mutex);
867 
868  //Search the community table for the specified community string
869  entry = snmpFindCommunityEntry(context, community, osStrlen(community));
870 
871  //Any matching entry found?
872  if(entry != NULL)
873  {
874  //Clear the contents
875  osMemset(entry, 0, sizeof(SnmpUserEntry));
876  //Now mark the entry as free
877  entry->status = MIB_ROW_STATUS_UNUSED;
878 
879  //Successful processing
880  error = NO_ERROR;
881  }
882  else
883  {
884  //The specified community string does not exist
885  error = ERROR_NOT_FOUND;
886  }
887 
888  //Release exclusive access to the SNMP agent context
889  osReleaseMutex(&context->mutex);
890 
891  //Return status code
892  return error;
893 #else
894  //Not implemented
895  return ERROR_NOT_IMPLEMENTED;
896 #endif
897 }
898 
899 
900 /**
901  * @brief Create a new user
902  * @param[in] context Pointer to the SNMP agent context
903  * @param[in] userName NULL-terminated string that contains the user name
904  * @param[in] mode Access rights
905  * @param[in] keyFormat Key format (ASCII password or raw key)
906  * @param[in] authProtocol Authentication type
907  * @param[in] authKey Key to be used for data authentication
908  * @param[in] privProtocol Privacy type
909  * @param[in] privKey Key to be used for data encryption
910  * @return Error code
911  **/
912 
914  const char_t *userName, SnmpAccess mode, SnmpKeyFormat keyFormat,
915  SnmpAuthProtocol authProtocol, const void *authKey,
916  SnmpPrivProtocol privProtocol, const void *privKey)
917 {
918 #if (SNMP_V3_SUPPORT == ENABLED)
919  error_t error;
920  size_t n;
921  SnmpUserEntry *entry;
922 
923  //Check parameters
924  if(context == NULL || userName == NULL)
925  {
927  }
928 
929  //Data authentication?
930  if(authProtocol != SNMP_AUTH_PROTOCOL_NONE)
931  {
932  //Check key format
933  if(keyFormat != SNMP_KEY_FORMAT_TEXT &&
934  keyFormat != SNMP_KEY_FORMAT_RAW &&
935  keyFormat != SNMP_KEY_FORMAT_LOCALIZED)
936  {
938  }
939 
940  //Data authentication requires a key
941  if(authKey == NULL)
942  {
944  }
945  }
946 
947  //Data confidentiality?
948  if(privProtocol != SNMP_PRIV_PROTOCOL_NONE)
949  {
950  //Check key format
951  if(keyFormat != SNMP_KEY_FORMAT_TEXT && keyFormat != SNMP_KEY_FORMAT_RAW)
952  {
954  }
955 
956  //Data confidentiality requires a key
957  if(privKey == NULL)
958  {
960  }
961 
962  //There is no provision for data confidentiality without data authentication
963  if(authProtocol == SNMP_AUTH_PROTOCOL_NONE)
964  {
966  }
967  }
968 
969  //Retrieve the length of the user name
970  n = osStrlen(userName);
971 
972  //Make sure the user name is valid
973  if(n == 0 || n > SNMP_MAX_USER_NAME_LEN)
974  {
975  return ERROR_INVALID_LENGTH;
976  }
977 
978  //Acquire exclusive access to the SNMP agent context
979  osAcquireMutex(&context->mutex);
980 
981  //Check whether the user name already exists
982  entry = snmpFindUserEntry(context, userName, osStrlen(userName));
983 
984  //If the specified user name does not exist, then a new entry
985  //should be created
986  if(entry == NULL)
987  {
988  //Create a security profile for the new user
989  entry = snmpCreateUserEntry(context);
990  }
991 
992  //Any entry available?
993  if(entry != NULL)
994  {
995  //Clear the security profile of the user
996  osMemset(entry, 0, sizeof(SnmpUserEntry));
997 
998  //Save user name
999  osStrcpy(entry->name, userName);
1000  //Access rights
1001  entry->mode = mode;
1002  //Authentication protocol
1003  entry->authProtocol = authProtocol;
1004  //Privacy protocol
1005  entry->privProtocol = privProtocol;
1006 
1007  //Initialize status code
1008  error = NO_ERROR;
1009 
1010  //Data authentication?
1011  if(authProtocol != SNMP_AUTH_PROTOCOL_NONE)
1012  {
1013  //ASCII password or raw key?
1014  if(keyFormat == SNMP_KEY_FORMAT_TEXT)
1015  {
1016  //Generate the authentication key from the provided password
1017  error = snmpGenerateKey(authProtocol, authKey, &entry->rawAuthKey);
1018 
1019  //Check status code
1020  if(!error)
1021  {
1022  //Localize the key with the engine ID
1023  error = snmpLocalizeKey(authProtocol,
1024  context->contextEngine, context->contextEngineLen,
1025  &entry->rawAuthKey, &entry->localizedAuthKey);
1026  }
1027  }
1028  else if(keyFormat == SNMP_KEY_FORMAT_RAW)
1029  {
1030  //Save the authentication key
1031  osMemcpy(&entry->rawAuthKey, authKey, sizeof(SnmpKey));
1032 
1033  //Now localize the key with the engine ID
1034  error = snmpLocalizeKey(authProtocol,
1035  context->contextEngine, context->contextEngineLen,
1036  &entry->rawAuthKey, &entry->localizedAuthKey);
1037  }
1038  else
1039  {
1040  //The authentication key is already localized
1041  osMemcpy(&entry->localizedAuthKey, authKey, sizeof(SnmpKey));
1042  }
1043  }
1044 
1045  //Check status code
1046  if(!error)
1047  {
1048  //Data confidentiality?
1049  if(privProtocol != SNMP_PRIV_PROTOCOL_NONE)
1050  {
1051  //ASCII password or raw key?
1052  if(keyFormat == SNMP_KEY_FORMAT_TEXT)
1053  {
1054  //Generate the privacy key from the provided password
1055  error = snmpGenerateKey(authProtocol, privKey, &entry->rawPrivKey);
1056 
1057  //Check status code
1058  if(!error)
1059  {
1060  //Localize the key with the engine ID
1061  error = snmpLocalizeKey(authProtocol,
1062  context->contextEngine, context->contextEngineLen,
1063  &entry->rawPrivKey, &entry->localizedPrivKey);
1064  }
1065  }
1066  else if(keyFormat == SNMP_KEY_FORMAT_RAW)
1067  {
1068  //Save the privacy key
1069  osMemcpy(&entry->rawPrivKey, privKey, sizeof(SnmpKey));
1070 
1071  //Now localize the key with the engine ID
1072  error = snmpLocalizeKey(authProtocol,
1073  context->contextEngine, context->contextEngineLen,
1074  &entry->rawPrivKey, &entry->localizedPrivKey);
1075  }
1076  else
1077  {
1078  //The privacy key is already localized
1079  osMemcpy(&entry->localizedPrivKey, privKey, sizeof(SnmpKey));
1080  }
1081  }
1082  }
1083 
1084  //Check status code
1085  if(!error)
1086  {
1087  //The entry is now available for use
1088  entry->status = MIB_ROW_STATUS_ACTIVE;
1089  }
1090  else
1091  {
1092  //Clean up side effects
1093  osMemset(entry, 0, sizeof(SnmpUserEntry));
1094  //Now mark the entry as free
1095  entry->status = MIB_ROW_STATUS_UNUSED;
1096  }
1097  }
1098  else
1099  {
1100  //The user table runs out of space
1101  error = ERROR_OUT_OF_RESOURCES;
1102  }
1103 
1104  //Release exclusive access to the SNMP agent context
1105  osReleaseMutex(&context->mutex);
1106 
1107  //Return error code
1108  return error;
1109 #else
1110  //Not implemented
1111  return ERROR_NOT_IMPLEMENTED;
1112 #endif
1113 }
1114 
1115 
1116 /**
1117  * @brief Remove existing user
1118  * @param[in] context Pointer to the SNMP agent context
1119  * @param[in] userName NULL-terminated string that contains the user name
1120  * @return Error code
1121  **/
1122 
1124 {
1125 #if (SNMP_V3_SUPPORT == ENABLED)
1126  error_t error;
1127  SnmpUserEntry *entry;
1128 
1129  //Check parameters
1130  if(context == NULL || userName == NULL)
1131  {
1132  return ERROR_INVALID_PARAMETER;
1133  }
1134 
1135  //Acquire exclusive access to the SNMP agent context
1136  osAcquireMutex(&context->mutex);
1137 
1138  //Search the user table for the specified user name
1139  entry = snmpFindUserEntry(context, userName, osStrlen(userName));
1140 
1141  //Any matching entry found?
1142  if(entry != NULL)
1143  {
1144  //Clear the security profile of the user
1145  osMemset(entry, 0, sizeof(SnmpUserEntry));
1146  //Now mark the entry as free
1147  entry->status = MIB_ROW_STATUS_UNUSED;
1148 
1149  //Successful processing
1150  error = NO_ERROR;
1151  }
1152  else
1153  {
1154  //The specified user name does not exist
1155  error = ERROR_NOT_FOUND;
1156  }
1157 
1158  //Release exclusive access to the SNMP agent context
1159  osReleaseMutex(&context->mutex);
1160 
1161  //Return status code
1162  return error;
1163 #else
1164  //Not implemented
1165  return ERROR_NOT_IMPLEMENTED;
1166 #endif
1167 }
1168 
1169 
1170 /**
1171  * @brief Join a group of users
1172  * @param[in] context Pointer to the SNMP agent context
1173  * @param[in] userName NULL-terminated string that contains the user name
1174  * @param[in] securityModel Security model
1175  * @param[in] groupName NULL-terminated string that contains the group name
1176  * @return Error code
1177  **/
1178 
1180  SnmpSecurityModel securityModel, const char_t *groupName)
1181 {
1182 #if (SNMP_AGENT_VACM_SUPPORT == ENABLED)
1183  error_t error;
1184  size_t n;
1185  SnmpGroupEntry *entry;
1186 
1187  //Check parameters
1188  if(context == NULL || userName == NULL || groupName == NULL)
1189  {
1190  return ERROR_INVALID_PARAMETER;
1191  }
1192 
1193  //Check security model
1194  if(securityModel != SNMP_SECURITY_MODEL_V1 &&
1195  securityModel != SNMP_SECURITY_MODEL_V2C &&
1196  securityModel != SNMP_SECURITY_MODEL_USM)
1197  {
1198  return ERROR_INVALID_PARAMETER;
1199  }
1200 
1201  //Retrieve the length of the user name
1202  n = osStrlen(userName);
1203 
1204  //Make sure the user name is valid
1205  if(n == 0 || n > SNMP_MAX_USER_NAME_LEN)
1206  {
1207  return ERROR_INVALID_LENGTH;
1208  }
1209 
1210  //Retrieve the length of the group name
1211  n = osStrlen(groupName);
1212 
1213  //Make sure the group name is valid
1214  if(n == 0 || n > SNMP_MAX_GROUP_NAME_LEN)
1215  {
1216  return ERROR_INVALID_LENGTH;
1217  }
1218 
1219  //Acquire exclusive access to the SNMP agent context
1220  osAcquireMutex(&context->mutex);
1221 
1222  //Search the group table for a matching entry
1223  entry = snmpFindGroupEntry(context, securityModel, userName,
1224  osStrlen(userName));
1225 
1226  //No matching entry found?
1227  if(entry == NULL)
1228  {
1229  //Create a new entry in the group table
1230  entry = snmpCreateGroupEntry(context);
1231  }
1232 
1233  //Any entry available?
1234  if(entry != NULL)
1235  {
1236  //Clear entry
1237  osMemset(entry, 0, sizeof(SnmpGroupEntry));
1238 
1239  //Save security model
1240  entry->securityModel = securityModel;
1241  //Save user name
1242  osStrcpy(entry->securityName, userName);
1243  //Save group name
1244  osStrcpy(entry->groupName, groupName);
1245 
1246  //The entry is now available for use
1247  entry->status = MIB_ROW_STATUS_ACTIVE;
1248 
1249  //Successful processing
1250  error = NO_ERROR;
1251  }
1252  else
1253  {
1254  //The group table runs out of space
1255  error = ERROR_OUT_OF_RESOURCES;
1256  }
1257 
1258  //Release exclusive access to the SNMP agent context
1259  osReleaseMutex(&context->mutex);
1260 
1261  //Return status code
1262  return error;
1263 #else
1264  //Not implemented
1265  return ERROR_NOT_IMPLEMENTED;
1266 #endif
1267 }
1268 
1269 
1270 /**
1271  * @brief Leave a group of users
1272  * @param[in] context Pointer to the SNMP agent context
1273  * @param[in] userName NULL-terminated string that contains the user name
1274  * @param[in] securityModel Security model
1275  * @return Error code
1276  **/
1277 
1279  const char_t *userName, SnmpSecurityModel securityModel)
1280 {
1281 #if (SNMP_AGENT_VACM_SUPPORT == ENABLED)
1282  error_t error;
1283  SnmpGroupEntry *entry;
1284 
1285  //Check parameters
1286  if(context == NULL || userName == NULL)
1287  {
1288  return ERROR_INVALID_PARAMETER;
1289  }
1290 
1291  //Acquire exclusive access to the SNMP agent context
1292  osAcquireMutex(&context->mutex);
1293 
1294  //Search the group table for a matching entry
1295  entry = snmpFindGroupEntry(context, securityModel, userName,
1296  osStrlen(userName));
1297 
1298  //Any matching entry found?
1299  if(entry != NULL)
1300  {
1301  //Clear the entry
1302  osMemset(entry, 0, sizeof(SnmpGroupEntry));
1303  //Now mark the entry as free
1304  entry->status = MIB_ROW_STATUS_UNUSED;
1305 
1306  //Successful processing
1307  error = NO_ERROR;
1308  }
1309  else
1310  {
1311  //The specified entry does not exist
1312  error = ERROR_NOT_FOUND;
1313  }
1314 
1315  //Release exclusive access to the SNMP agent context
1316  osReleaseMutex(&context->mutex);
1317 
1318  //Return status code
1319  return error;
1320 #else
1321  //Not implemented
1322  return ERROR_NOT_IMPLEMENTED;
1323 #endif
1324 }
1325 
1326 
1327 /**
1328  * @brief Create access policy for the specified group name
1329  * @param[in] context Pointer to the SNMP agent context
1330  * @param[in] groupName NULL-terminated string that contains the group name
1331  * @param[in] securityModel Security model
1332  * @param[in] securityLevel Security level
1333  * @param[in] contextPrefix NULL-terminated string that contains the context name prefix
1334  * @param[in] contextMatch Context match
1335  * @param[in] readViewName NULL-terminated string that contains the read view name
1336  * @param[in] writeViewName NULL-terminated string that contains the write view name
1337  * @param[in] notifyViewName NULL-terminated string that contains the notify view name
1338  * @return Error code
1339  **/
1340 
1342  const char_t *groupName, SnmpSecurityModel securityModel,
1343  SnmpSecurityLevel securityLevel, const char_t *contextPrefix,
1344  SnmpContextMatch contextMatch, const char_t *readViewName,
1345  const char_t *writeViewName, const char_t *notifyViewName)
1346 {
1347 #if (SNMP_AGENT_VACM_SUPPORT == ENABLED)
1348  error_t error;
1349  size_t n;
1350  SnmpAccessEntry *entry;
1351 
1352  //Check parameters
1353  if(context == NULL || groupName == NULL || contextPrefix == NULL)
1354  {
1355  return ERROR_INVALID_PARAMETER;
1356  }
1357 
1358  if(readViewName == NULL || writeViewName == NULL || notifyViewName == NULL)
1359  {
1360  return ERROR_INVALID_PARAMETER;
1361  }
1362 
1363  //Check security model
1364  if(securityModel != SNMP_SECURITY_MODEL_ANY &&
1365  securityModel != SNMP_SECURITY_MODEL_V1 &&
1366  securityModel != SNMP_SECURITY_MODEL_V2C &&
1367  securityModel != SNMP_SECURITY_MODEL_USM)
1368  {
1369  return ERROR_INVALID_PARAMETER;
1370  }
1371 
1372  //Check security level
1373  if(securityLevel != SNMP_SECURITY_LEVEL_NO_AUTH_NO_PRIV &&
1374  securityLevel != SNMP_SECURITY_LEVEL_AUTH_NO_PRIV &&
1375  securityLevel != SNMP_SECURITY_LEVEL_AUTH_PRIV)
1376  {
1377  return ERROR_INVALID_PARAMETER;
1378  }
1379 
1380  //Check context match
1381  if(contextMatch != SNMP_CONTEXT_MATCH_EXACT &&
1382  contextMatch != SNMP_CONTEXT_MATCH_PREFIX)
1383  {
1384  return ERROR_INVALID_PARAMETER;
1385  }
1386 
1387  //Retrieve the length of the group name
1388  n = osStrlen(groupName);
1389 
1390  //Make sure the group name is valid
1391  if(n == 0 || n > SNMP_MAX_GROUP_NAME_LEN)
1392  {
1393  return ERROR_INVALID_LENGTH;
1394  }
1395 
1396  //Make sure the context name prefix is valid
1398  {
1399  return ERROR_INVALID_LENGTH;
1400  }
1401 
1402  //Make sure the read view name is valid
1403  if(osStrlen(readViewName) > SNMP_MAX_VIEW_NAME_LEN)
1404  {
1405  return ERROR_INVALID_LENGTH;
1406  }
1407 
1408  //Make sure the write view name is valid
1409  if(osStrlen(writeViewName) > SNMP_MAX_VIEW_NAME_LEN)
1410  {
1411  return ERROR_INVALID_LENGTH;
1412  }
1413 
1414  //Make sure the notify view name is valid
1415  if(osStrlen(notifyViewName) > SNMP_MAX_VIEW_NAME_LEN)
1416  {
1417  return ERROR_INVALID_LENGTH;
1418  }
1419 
1420  //Acquire exclusive access to the SNMP agent context
1421  osAcquireMutex(&context->mutex);
1422 
1423  //Search the access table for a matching entry
1424  entry = snmpFindAccessEntry(context, groupName, contextPrefix,
1425  securityModel, securityLevel);
1426 
1427  //No matching entry found?
1428  if(entry == NULL)
1429  {
1430  //Create a new entry in the access table
1431  entry = snmpCreateAccessEntry(context);
1432  }
1433 
1434  //Any entry available?
1435  if(entry != NULL)
1436  {
1437  //Clear entry
1438  osMemset(entry, 0, sizeof(SnmpAccessEntry));
1439 
1440  //Save group name
1441  osStrcpy(entry->groupName, groupName);
1442  //Save context name prefix
1444  //Save security model
1445  entry->securityModel = securityModel;
1446  //Save security level
1447  entry->securityLevel = securityLevel;
1448  //Save context match
1449  entry->contextMatch = contextMatch;
1450  //Save read view name
1451  osStrcpy(entry->readViewName, readViewName);
1452  //Save write view name
1453  osStrcpy(entry->writeViewName, writeViewName);
1454  //Save notify view name
1455  osStrcpy(entry->notifyViewName, notifyViewName);
1456 
1457  //The entry is now available for use
1458  entry->status = MIB_ROW_STATUS_ACTIVE;
1459 
1460  //Successful processing
1461  error = NO_ERROR;
1462  }
1463  else
1464  {
1465  //The access table runs out of space
1466  error = ERROR_OUT_OF_RESOURCES;
1467  }
1468 
1469  //Release exclusive access to the SNMP agent context
1470  osReleaseMutex(&context->mutex);
1471 
1472  //Return status code
1473  return error;
1474 #else
1475  //Not implemented
1476  return ERROR_NOT_IMPLEMENTED;
1477 #endif
1478 }
1479 
1480 
1481 /**
1482  * @brief Delete an existing access policy
1483  * @param[in] context Pointer to the SNMP agent context
1484  * @param[in] groupName NULL-terminated string that contains the group name
1485  * @param[in] securityModel Security model
1486  * @param[in] securityLevel Security level
1487  * @param[in] contextPrefix NULL-terminated string that contains the context name prefix
1488  * @return Error code
1489  **/
1490 
1492  const char_t *groupName, SnmpSecurityModel securityModel,
1493  SnmpSecurityLevel securityLevel, const char_t *contextPrefix)
1494 {
1495 #if (SNMP_AGENT_VACM_SUPPORT == ENABLED)
1496  error_t error;
1497  SnmpAccessEntry *entry;
1498 
1499  //Check parameters
1500  if(context == NULL || groupName == NULL || contextPrefix == NULL)
1501  {
1502  return ERROR_INVALID_PARAMETER;
1503  }
1504 
1505  //Acquire exclusive access to the SNMP agent context
1506  osAcquireMutex(&context->mutex);
1507 
1508  //Search the access table for a matching entry
1509  entry = snmpFindAccessEntry(context, groupName, contextPrefix,
1510  securityModel, securityLevel);
1511 
1512  //Any matching entry found?
1513  if(entry != NULL)
1514  {
1515  //Clear the entry
1516  osMemset(entry, 0, sizeof(SnmpAccessEntry));
1517  //Now mark the entry as free
1518  entry->status = MIB_ROW_STATUS_UNUSED;
1519 
1520  //Successful processing
1521  error = NO_ERROR;
1522  }
1523  else
1524  {
1525  //The specified entry does not exist
1526  error = ERROR_NOT_FOUND;
1527  }
1528 
1529  //Release exclusive access to the SNMP agent context
1530  osReleaseMutex(&context->mutex);
1531 
1532  //Return status code
1533  return error;
1534 #else
1535  //Not implemented
1536  return ERROR_NOT_IMPLEMENTED;
1537 #endif
1538 }
1539 
1540 
1541 /**
1542  * @brief Create a new MIB view
1543  * @param[in] context Pointer to the SNMP agent context
1544  * @param[in] viewName NULL-terminated string that contains the view name
1545  * @param[in] subtree Pointer to the subtree
1546  * @param[in] subtreeLen Length of the subtree, in bytes
1547  * @param[in] mask Pointer to the bit mask
1548  * @param[in] maskLen Length of the bit mask
1549  * @param[in] type View type
1550  * @return Error code
1551  **/
1552 
1554  const char_t *viewName, const uint8_t *subtree, size_t subtreeLen,
1555  const uint8_t *mask, size_t maskLen, SnmpViewType type)
1556 {
1557 #if (SNMP_AGENT_VACM_SUPPORT == ENABLED)
1558  error_t error;
1559  size_t n;
1560  SnmpViewEntry *entry;
1561 
1562  //Check parameters
1563  if(context == NULL || viewName == NULL || subtree == NULL)
1564  {
1565  return ERROR_INVALID_PARAMETER;
1566  }
1567 
1568  //Check view type
1569  if(type != SNMP_VIEW_TYPE_INCLUDED &&
1571  {
1572  return ERROR_INVALID_PARAMETER;
1573  }
1574 
1575  //Retrieve the length of the view name
1576  n = osStrlen(viewName);
1577 
1578  //Make sure the view name is valid
1579  if(n == 0 || n > SNMP_MAX_VIEW_NAME_LEN)
1580  {
1581  return ERROR_INVALID_LENGTH;
1582  }
1583 
1584  //Make sure the subtree is valid
1585  if(subtreeLen == 0 || subtreeLen > MIB_MAX_OID_SIZE)
1586  {
1587  return ERROR_INVALID_PARAMETER;
1588  }
1589 
1590  //Make sure the bit mask is valid
1591  if(maskLen > 0 && mask == NULL)
1592  {
1593  return ERROR_INVALID_PARAMETER;
1594  }
1595 
1596  if(maskLen > SNMP_MAX_BIT_MASK_SIZE)
1597  {
1598  return ERROR_INVALID_PARAMETER;
1599  }
1600 
1601  //Acquire exclusive access to the SNMP agent context
1602  osAcquireMutex(&context->mutex);
1603 
1604  //Search the view table for a matching entry
1605  entry = snmpFindViewEntry(context, viewName, subtree, subtreeLen);
1606 
1607  //No matching entry found?
1608  if(entry == NULL)
1609  {
1610  //Create a new entry in the view table
1611  entry = snmpCreateViewEntry(context);
1612  }
1613 
1614  //Any entry available?
1615  if(entry != NULL)
1616  {
1617  //Clear entry
1618  osMemset(entry, 0, sizeof(SnmpViewEntry));
1619 
1620  //Save view name
1621  osStrcpy(entry->viewName, viewName);
1622  //Save subtree
1623  osMemcpy(entry->subtree, subtree, subtreeLen);
1624  //Save the length of the subtree
1625  entry->subtreeLen = subtreeLen;
1626  //Save bit mask
1627  osMemcpy(entry->mask, mask, maskLen);
1628  //Save the length of the bit mask
1629  entry->maskLen = maskLen;
1630  //Save type
1631  entry->type = type;
1632 
1633  //The entry is now available for use
1634  entry->status = MIB_ROW_STATUS_ACTIVE;
1635 
1636  //Successful processing
1637  error = NO_ERROR;
1638  }
1639  else
1640  {
1641  //The view table runs out of space
1642  error = ERROR_OUT_OF_RESOURCES;
1643  }
1644 
1645  //Release exclusive access to the SNMP agent context
1646  osReleaseMutex(&context->mutex);
1647 
1648  //Return status code
1649  return error;
1650 #else
1651  //Not implemented
1652  return ERROR_NOT_IMPLEMENTED;
1653 #endif
1654 }
1655 
1656 
1657 /**
1658  * @brief Delete an existing MIB view
1659  * @param[in] context Pointer to the SNMP agent context
1660  * @param[in] viewName NULL-terminated string that contains the view name
1661  * @param[in] subtree Pointer to the subtree
1662  * @param[in] subtreeLen Length of the subtree, in bytes
1663  * @return Error code
1664  **/
1665 
1667  const char_t *viewName, const uint8_t *subtree, size_t subtreeLen)
1668 {
1669 #if (SNMP_AGENT_VACM_SUPPORT == ENABLED)
1670  error_t error;
1671  SnmpViewEntry *entry;
1672 
1673  //Check parameters
1674  if(context == NULL || viewName == NULL || subtree == NULL)
1675  {
1676  return ERROR_INVALID_PARAMETER;
1677  }
1678 
1679  //Acquire exclusive access to the SNMP agent context
1680  osAcquireMutex(&context->mutex);
1681 
1682  //Search the view table for a matching entry
1683  entry = snmpFindViewEntry(context, viewName, subtree, subtreeLen);
1684 
1685  //Any matching entry found?
1686  if(entry != NULL)
1687  {
1688  //Clear the entry
1689  osMemset(entry, 0, sizeof(SnmpViewEntry));
1690  //Now mark the entry as free
1691  entry->status = MIB_ROW_STATUS_UNUSED;
1692 
1693  //Successful processing
1694  error = NO_ERROR;
1695  }
1696  else
1697  {
1698  //The specified entry does not exist
1699  error = ERROR_NOT_FOUND;
1700  }
1701 
1702  //Release exclusive access to the SNMP agent context
1703  osReleaseMutex(&context->mutex);
1704 
1705  //Return status code
1706  return error;
1707 #else
1708  //Not implemented
1709  return ERROR_NOT_IMPLEMENTED;
1710 #endif
1711 }
1712 
1713 
1714 /**
1715  * @brief Send SNMP trap notification
1716  * @param[in] context Pointer to the SNMP agent context
1717  * @param[in] destIpAddr Destination IP address
1718  * @param[in] version SNMP version identifier
1719  * @param[in] userName User name or community name
1720  * @param[in] genericTrapType Generic trap type
1721  * @param[in] specificTrapCode Specific code
1722  * @param[in] objectList List of object names
1723  * @param[in] objectListSize Number of entries in the list
1724  * @return Error code
1725  **/
1726 
1728  const IpAddr *destIpAddr, SnmpVersion version, const char_t *userName,
1729  uint_t genericTrapType, uint_t specificTrapCode,
1730  const SnmpTrapObject *objectList, uint_t objectListSize)
1731 {
1732 #if (SNMP_AGENT_TRAP_SUPPORT == ENABLED)
1733  error_t error;
1734 
1735  //Check parameters
1736  if(context == NULL || destIpAddr == NULL || userName == NULL)
1737  {
1738  return ERROR_INVALID_PARAMETER;
1739  }
1740 
1741  //Make sure the list of objects is valid
1742  if(objectListSize > 0 && objectList == NULL)
1743  {
1744  return ERROR_INVALID_PARAMETER;
1745  }
1746 
1747  //Acquire exclusive access to the SNMP agent context
1748  osAcquireMutex(&context->mutex);
1749 
1750 #if (SNMP_V3_SUPPORT == ENABLED)
1751  //Refresh SNMP engine time
1752  snmpRefreshEngineTime(context);
1753 #endif
1754 
1755  //Format Trap message
1756  error = snmpFormatTrapMessage(context, version, userName,
1757  genericTrapType, specificTrapCode, objectList, objectListSize);
1758 
1759  //Check status code
1760  if(!error)
1761  {
1762  //Total number of messages which were passed from the SNMP protocol
1763  //entity to the transport service
1764  MIB2_SNMP_INC_COUNTER32(snmpOutPkts, 1);
1765 
1766  //Debug message
1767  TRACE_INFO("Sending SNMP message to %s port %" PRIu16 " (%" PRIuSIZE " bytes)...\r\n",
1768  ipAddrToString(destIpAddr, NULL), context->trapPort,
1769  context->response.length);
1770 
1771  //Display the contents of the SNMP message
1772  TRACE_DEBUG_ARRAY(" ", context->response.pos, context->response.length);
1773  //Display ASN.1 structure
1774  asn1DumpObject(context->response.pos, context->response.length, 0);
1775 
1776  //Send SNMP message
1777  error = socketSendTo(context->socket, destIpAddr, context->trapPort,
1778  context->response.pos, context->response.length, NULL, 0);
1779  }
1780 
1781  //Release exclusive access to the SNMP agent context
1782  osReleaseMutex(&context->mutex);
1783 
1784  //Return status code
1785  return error;
1786 #else
1787  //Not implemented
1788  return ERROR_NOT_IMPLEMENTED;
1789 #endif
1790 }
1791 
1792 
1793 /**
1794  * @brief Send SNMP inform request
1795  * @param[in] context Pointer to the SNMP agent context
1796  * @param[in] destIpAddr Destination IP address
1797  * @param[in] version SNMP version identifier
1798  * @param[in] userName User name or community name
1799  * @param[in] genericTrapType Generic trap type
1800  * @param[in] specificTrapCode Specific code
1801  * @param[in] objectList List of object names
1802  * @param[in] objectListSize Number of entries in the list
1803  * @return Error code
1804  **/
1805 
1807  const IpAddr *destIpAddr, SnmpVersion version, const char_t *userName,
1808  uint_t genericTrapType, uint_t specificTrapCode,
1809  const SnmpTrapObject *objectList, uint_t objectListSize)
1810 {
1811 #if (SNMP_AGENT_INFORM_SUPPORT == ENABLED)
1812  error_t error;
1813  bool_t status;
1814 
1815  //Check parameters
1816  if(context == NULL || destIpAddr == NULL || userName == NULL)
1817  {
1818  return ERROR_INVALID_PARAMETER;
1819  }
1820 
1821  //Make sure the list of objects is valid
1822  if(objectListSize > 0 && objectList == NULL)
1823  {
1824  return ERROR_INVALID_PARAMETER;
1825  }
1826 
1827  //Initialize status code
1828  error = NO_ERROR;
1829 
1830  //Acquire exclusive access to the SNMP agent context
1831  osAcquireMutex(&context->mutex);
1832 
1833  //Send an inform request and wait for the acknowledgment to be received
1834  while(!error)
1835  {
1836  //Check current state
1837  if(context->informState == SNMP_AGENT_STATE_IDLE)
1838  {
1839  //Reset event object
1840  osResetEvent(&context->informEvent);
1841  //Initialize retransmission counter
1842  context->informRetransmitCount = 0;
1843 
1844 #if (SNMP_V3_SUPPORT == ENABLED)
1845  //SNMPv3 version?
1846  if(version == SNMP_VERSION_3)
1847  {
1848  //The User-based Security Model (USM) of SNMPv3 provides a mechanism
1849  //to discover the snmpEngineID of the remote SNMP engine
1850  context->informContextEngineLen = 0;
1851  context->informEngineBoots = 0;
1852  context->informEngineTime = 0;
1853 
1854  //Perform discovery process
1855  context->informState = SNMP_AGENT_STATE_SENDING_GET_REQ;
1856  }
1857  else
1858 #endif
1859  //SNMPv2c version?
1860  {
1861  //Send an inform request message
1862  context->informState = SNMP_AGENT_STATE_SENDING_INFORM_REQ;
1863  }
1864  }
1865 #if (SNMP_V3_SUPPORT == ENABLED)
1866  else if(context->informState == SNMP_AGENT_STATE_SENDING_GET_REQ)
1867  {
1868  //Format GetRequest message
1869  error = snmpFormatGetRequestMessage(context, version);
1870 
1871  //Check status
1872  if(!error)
1873  {
1874  //Total number of messages which were passed from the SNMP protocol
1875  //entity to the transport service
1876  MIB2_SNMP_INC_COUNTER32(snmpOutPkts, 1);
1877 
1878  //Debug message
1879  TRACE_INFO("Sending SNMP message to %s port %" PRIu16 " (%" PRIuSIZE " bytes)...\r\n",
1880  ipAddrToString(destIpAddr, NULL), context->trapPort,
1881  context->response.length);
1882 
1883  //Display the contents of the SNMP message
1884  TRACE_DEBUG_ARRAY(" ", context->response.pos, context->response.length);
1885  //Display ASN.1 structure
1886  asn1DumpObject(context->response.pos, context->response.length, 0);
1887 
1888  //Send SNMP message
1889  error = socketSendTo(context->socket, destIpAddr, context->trapPort,
1890  context->response.pos, context->response.length, NULL, 0);
1891  }
1892 
1893  //Check status code
1894  if(!error)
1895  {
1896  //Save the time at which the GetResponse-PDU was sent
1897  context->informTimestamp = osGetSystemTime();
1898  //Increment retransmission counter
1899  context->informRetransmitCount++;
1900  //Wait for an Report-PDU to be received
1901  context->informState = SNMP_AGENT_STATE_WAITING_REPORT;
1902  }
1903  else
1904  {
1905  //Back to default state
1906  context->informState = SNMP_AGENT_STATE_IDLE;
1907  }
1908  }
1909  else if(context->informState == SNMP_AGENT_STATE_WAITING_REPORT)
1910  {
1911  //Release exclusive access to the SNMP agent context
1912  osReleaseMutex(&context->mutex);
1913 
1914  //Wait for a matching Report-PDU to be received
1915  status = osWaitForEvent(&context->informEvent,
1917 
1918  //Acquire exclusive access to the SNMP agent context
1919  osAcquireMutex(&context->mutex);
1920 
1921  //Any Report-PDU received?
1922  if(status && context->informContextEngineLen > 0)
1923  {
1924  //Reset event object
1925  osResetEvent(&context->informEvent);
1926  //Initialize retransmission counter
1927  context->informRetransmitCount = 0;
1928  //Send an inform request message
1929  context->informState = SNMP_AGENT_STATE_SENDING_INFORM_REQ;
1930  }
1931  else
1932  {
1933 #if (NET_RTOS_SUPPORT == DISABLED)
1934  //Get current time
1936 
1937  //Check current time
1938  if(timeCompare(time, context->informTimestamp + SNMP_AGENT_INFORM_TIMEOUT) < 0)
1939  {
1940  //Exit immediately
1941  error = ERROR_WOULD_BLOCK;
1942  }
1943  else
1944 #endif
1945  {
1946  //The request should be retransmitted if no corresponding response
1947  //is received in an appropriate time interval
1948  if(context->informRetransmitCount < SNMP_AGENT_INFORM_MAX_RETRIES)
1949  {
1950  //Retransmit the request
1951  context->informState = SNMP_AGENT_STATE_SENDING_GET_REQ;
1952  }
1953  else
1954  {
1955  //Back to default state
1956  context->informState = SNMP_AGENT_STATE_IDLE;
1957  //Report a timeout error
1958  error = ERROR_TIMEOUT;
1959  }
1960  }
1961  }
1962  }
1963 #endif
1964  else if(context->informState == SNMP_AGENT_STATE_SENDING_INFORM_REQ)
1965  {
1966  //Format InformRequest message
1967  error = snmpFormatInformRequestMessage(context, version, userName,
1968  genericTrapType, specificTrapCode, objectList, objectListSize);
1969 
1970  //Check status code
1971  if(!error)
1972  {
1973  //Total number of messages which were passed from the SNMP protocol
1974  //entity to the transport service
1975  MIB2_SNMP_INC_COUNTER32(snmpOutPkts, 1);
1976 
1977  //Debug message
1978  TRACE_INFO("Sending SNMP message to %s port %" PRIu16 " (%" PRIuSIZE " bytes)...\r\n",
1979  ipAddrToString(destIpAddr, NULL), context->trapPort,
1980  context->response.length);
1981 
1982  //Display the contents of the SNMP message
1983  TRACE_DEBUG_ARRAY(" ", context->response.pos, context->response.length);
1984  //Display ASN.1 structure
1985  asn1DumpObject(context->response.pos, context->response.length, 0);
1986 
1987  //Send SNMP message
1988  error = socketSendTo(context->socket, destIpAddr, context->trapPort,
1989  context->response.pos, context->response.length, NULL, 0);
1990  }
1991 
1992  //Check status code
1993  if(!error)
1994  {
1995  //Save the time at which the InformRequest-PDU was sent
1996  context->informTimestamp = osGetSystemTime();
1997  //Increment retransmission counter
1998  context->informRetransmitCount++;
1999  //Wait for a GetResponse-PDU to be received
2000  context->informState = SNMP_AGENT_STATE_WAITING_GET_RESP;
2001  }
2002  else
2003  {
2004  //Back to default state
2005  context->informState = SNMP_AGENT_STATE_IDLE;
2006  }
2007  }
2008  else if(context->informState == SNMP_AGENT_STATE_WAITING_GET_RESP)
2009  {
2010  //Release exclusive access to the SNMP agent context
2011  osReleaseMutex(&context->mutex);
2012 
2013  //Wait for a matching GetResponse-PDU to be received
2014  status = osWaitForEvent(&context->informEvent,
2016 
2017  //Acquire exclusive access to the SNMP agent context
2018  osAcquireMutex(&context->mutex);
2019 
2020  //Any GetResponse-PDU received?
2021  if(status)
2022  {
2023  //Back to default state
2024  context->informState = SNMP_AGENT_STATE_IDLE;
2025  //The inform request has been acknowledged
2026  error = NO_ERROR;
2027  //We are done
2028  break;
2029  }
2030  else
2031  {
2032 #if (NET_RTOS_SUPPORT == DISABLED)
2033  //Get current time
2035 
2036  //Check current time
2037  if(timeCompare(time, context->informTimestamp + SNMP_AGENT_INFORM_TIMEOUT) < 0)
2038  {
2039  //Exit immediately
2040  error = ERROR_WOULD_BLOCK;
2041  }
2042  else
2043 #endif
2044  {
2045  //The request should be retransmitted if no corresponding response
2046  //is received in an appropriate time interval
2047  if(context->informRetransmitCount < SNMP_AGENT_INFORM_MAX_RETRIES)
2048  {
2049  //Retransmit the request
2050  context->informState = SNMP_AGENT_STATE_SENDING_INFORM_REQ;
2051  }
2052  else
2053  {
2054  //Back to default state
2055  context->informState = SNMP_AGENT_STATE_IDLE;
2056  //Report a timeout error
2057  error = ERROR_TIMEOUT;
2058  }
2059  }
2060  }
2061  }
2062  else
2063  {
2064  //Back to default state
2065  context->informState = SNMP_AGENT_STATE_IDLE;
2066  //Report an error
2067  error = ERROR_WRONG_STATE;
2068  }
2069  }
2070 
2071  //Release exclusive access to the SNMP agent context
2072  osReleaseMutex(&context->mutex);
2073 
2074  //Return status code
2075  return error;
2076 #else
2077  //Not implemented
2078  return ERROR_NOT_IMPLEMENTED;
2079 #endif
2080 }
2081 
2082 
2083 /**
2084  * @brief SNMP agent task
2085  * @param[in] context Pointer to the SNMP agent context
2086  **/
2087 
2089 {
2090  error_t error;
2091  SocketMsg msg;
2092  SocketEventDesc eventDesc;
2093 
2094 #if (NET_RTOS_SUPPORT == ENABLED)
2095  //Task prologue
2096  osEnterTask();
2097 
2098  //Main loop
2099  while(1)
2100  {
2101 #endif
2102  //Specify the events the application is interested in
2103  eventDesc.socket = context->socket;
2104  eventDesc.eventMask = SOCKET_EVENT_RX_READY;
2105  eventDesc.eventFlags = 0;
2106 
2107  //Wait for an event
2108  socketPoll(&eventDesc, 1, &context->event, INFINITE_DELAY);
2109 
2110  //Stop request?
2111  if(context->stop)
2112  {
2113  //Stop SNMP agent operation
2114  context->running = FALSE;
2115  //Task epilogue
2116  osExitTask();
2117  //Kill ourselves
2119  }
2120 
2121  //Any datagram received?
2122  if(eventDesc.eventFlags != 0)
2123  {
2124  //Point to the receive buffer
2125  msg = SOCKET_DEFAULT_MSG;
2126  msg.data = context->request.buffer;
2127  msg.size = SNMP_MAX_MSG_SIZE;
2128 
2129  //Receive incoming datagram
2130  error = socketReceiveMsg(context->socket, &msg, 0);
2131 
2132  //Check status code
2133  if(!error)
2134  {
2135  //Make sure the destination IP address is a valid unicast address
2136  if(!ipIsMulticastAddr(&msg.destIpAddr))
2137  {
2138  //Acquire exclusive access to the SNMP agent context
2139  osAcquireMutex(&context->mutex);
2140 
2141  //Retrieve the length of the datagram
2142  context->request.bufferLen = msg.length;
2143 
2144  //Get the source and destination IP addresses
2145  context->localInterface = msg.interface;
2146  context->localIpAddr = msg.destIpAddr;
2147  context->remoteIpAddr = msg.srcIpAddr;
2148  context->remotePort = msg.srcPort;
2149 
2150  //Debug message
2151  TRACE_INFO("\r\nSNMP message received from %s port %" PRIu16
2152  " (%" PRIuSIZE " bytes)...\r\n",
2153  ipAddrToString(&context->remoteIpAddr, NULL),
2154  context->remotePort, context->request.bufferLen);
2155 
2156  //Display the contents of the SNMP message
2157  TRACE_DEBUG_ARRAY(" ", context->request.buffer,
2158  context->request.bufferLen);
2159  //Dump ASN.1 structure
2160  asn1DumpObject(context->request.buffer,
2161  context->request.bufferLen, 0);
2162 
2163  //Process incoming SNMP message
2164  error = snmpProcessMessage(context);
2165 
2166  //Check status code
2167  if(!error)
2168  {
2169  //Any response?
2170  if(context->response.length > 0)
2171  {
2172  //Debug message
2173  TRACE_INFO("Sending SNMP message to %s port %" PRIu16
2174  " (%" PRIuSIZE " bytes)...\r\n",
2175  ipAddrToString(&context->remoteIpAddr, NULL),
2176  context->remotePort, context->response.length);
2177 
2178  //Display the contents of the SNMP message
2179  TRACE_DEBUG_ARRAY(" ", context->response.pos,
2180  context->response.length);
2181  //Display ASN.1 structure
2182  asn1DumpObject(context->response.pos,
2183  context->response.length, 0);
2184 
2185  //Point to the send buffer
2186  msg = SOCKET_DEFAULT_MSG;
2187  msg.data = context->response.pos;
2188  msg.length = context->response.length;
2189 
2190  //Set the source and destination IP addresses
2191  msg.interface = context->localInterface;
2192  msg.srcIpAddr = context->localIpAddr;
2193  msg.destIpAddr = context->remoteIpAddr;
2194  msg.destPort = context->remotePort;
2195 
2196  //Send SNMP response message
2197  socketSendMsg(context->socket, &msg, 0);
2198  }
2199  }
2200 
2201  //Release exclusive access to the SNMP agent context
2202  osReleaseMutex(&context->mutex);
2203  }
2204  }
2205  }
2206 #if (NET_RTOS_SUPPORT == ENABLED)
2207  }
2208 #endif
2209 }
2210 
2211 
2212 /**
2213  * @brief Release SNMP agent context
2214  * @param[in] context Pointer to the SNMP agent context
2215  **/
2216 
2218 {
2219  //Make sure the SNMP agent context is valid
2220  if(context != NULL)
2221  {
2222  //Free previously allocated resources
2223  osDeleteMutex(&context->mutex);
2224  osDeleteEvent(&context->event);
2225 
2226 #if (SNMP_AGENT_INFORM_SUPPORT == ENABLED)
2227  osDeleteEvent(&context->informEvent);
2228 #endif
2229 
2230  //Clear SNMP agent context
2231  osMemset(context, 0, sizeof(SnmpAgentContext));
2232  }
2233 }
2234 
2235 #endif
uint8_t subtree[SNMP_MAX_OID_SIZE]
char_t writeViewName[SNMP_MAX_VIEW_NAME_LEN+1]
@ SNMP_SECURITY_MODEL_ANY
Any.
MIB-II module.
OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode, void *arg, const OsTaskParameters *params)
Create a task.
@ SOCKET_IP_PROTO_UDP
Definition: socket.h:108
@ SNMP_SECURITY_MODEL_V1
SNMPv1.
SnmpVersion versionMax
Maximum version accepted by the SNMP agent.
Definition: snmp_agent.h:137
SnmpSecurityModel securityModel
error_t socketBind(Socket *socket, const IpAddr *localIpAddr, uint16_t localPort)
Associate a local address with a socket.
Definition: socket.c:1423
@ SNMP_KEY_FORMAT_LOCALIZED
Localized key.
MibLoad load
Definition: mib_common.h:299
int bool_t
Definition: compiler_port.h:63
@ ERROR_NOT_FOUND
Definition: error.h:148
@ ERROR_OUT_OF_RANGE
Definition: error.h:138
bool_t osCreateMutex(OsMutex *mutex)
Create a mutex object.
error_t snmpAgentDeleteView(SnmpAgentContext *context, const char_t *viewName, const uint8_t *subtree, size_t subtreeLen)
Delete an existing MIB view.
Definition: snmp_agent.c:1666
SnmpViewEntry * snmpCreateViewEntry(SnmpAgentContext *context)
Create a new view entry.
#define osExitTask()
#define SNMP_MAX_VIEW_NAME_LEN
Definition: snmp_common.h:102
@ ERROR_WOULD_BLOCK
Definition: error.h:96
SnmpUserEntry * snmpFindCommunityEntry(SnmpAgentContext *context, const char_t *community, size_t length)
Search the community table for a given community string.
SnmpKey localizedPrivKey
Localized privacy key.
IP network address.
Definition: ip.h:97
SnmpSecurityModel
Security models.
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
error_t snmpAgentInit(SnmpAgentContext *context, const SnmpAgentSettings *settings)
SNMP agent initialization.
Definition: snmp_agent.c:107
error_t snmpAgentSetVersion(SnmpAgentContext *context, SnmpVersion versionMin, SnmpVersion versionMax)
Set minimum and maximum versions permitted.
Definition: snmp_agent.c:537
SnmpAccess mode
Access mode.
error_t snmpAgentSetContextName(SnmpAgentContext *context, const char_t *contextName)
Set context name.
Definition: snmp_agent.c:730
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:491
char_t contextPrefix[SNMP_MAX_CONTEXT_NAME_LEN+1]
@ SNMP_CONTEXT_MATCH_PREFIX
OsTaskParameters task
Task parameters.
Definition: snmp_agent.h:133
SnmpContextMatch
Context match.
#define TRUE
Definition: os_port.h:50
char_t groupName[SNMP_MAX_GROUP_NAME_LEN+1]
Message and ancillary data.
Definition: socket.h:241
#define OS_INVALID_TASK_ID
error_t snmpAgentCreateCommunity(SnmpAgentContext *context, const char_t *community, SnmpAccess mode)
Create a new community string.
Definition: snmp_agent.c:775
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:2169
size_t subtreeLen
SnmpKeyFormat
SNMP key format.
#define SNMP_AGENT_PRIORITY
Definition: snmp_agent.h:64
@ ERROR_OUT_OF_RESOURCES
Definition: error.h:64
char_t * ipAddrToString(const IpAddr *ipAddr, char_t *str)
Convert a binary IP address to a string representation.
Definition: ip.c:811
@ SNMP_CONTEXT_MATCH_EXACT
@ SOCKET_TYPE_DGRAM
Definition: socket.h:93
Object descriptor for trap notifications.
@ SNMP_AGENT_STATE_WAITING_REPORT
void * data
Pointer to the payload.
Definition: socket.h:242
uint8_t type
Definition: coap_common.h:176
error_t asn1DumpObject(const uint8_t *data, size_t length, uint_t level)
Display an ASN.1 data object.
Definition: asn1.c:1092
SnmpGroupEntry * snmpFindGroupEntry(SnmpAgentContext *context, uint_t securityModel, const char_t *securityName, size_t securityNameLen)
Search the group table.
#define SNMP_AGENT_INFORM_MAX_RETRIES
SnmpGroupEntry * snmpCreateGroupEntry(SnmpAgentContext *context)
Create a new group entry.
@ SNMP_SECURITY_MODEL_USM
User-based security model.
char_t securityName[SNMP_MAX_GROUP_NAME_LEN+1]
#define SNMP_AGENT_MAX_MIBS
Definition: snmp_agent.h:69
#define SNMP_MAX_GROUP_NAME_LEN
Definition: snmp_common.h:95
#define osStrlen(s)
Definition: os_port.h:171
SNMP trap notifications.
uint8_t version
Definition: coap_common.h:177
SnmpAccessEntry * snmpCreateAccessEntry(SnmpAgentContext *context)
Create a new access entry.
User table entry.
char_t groupName[SNMP_MAX_GROUP_NAME_LEN+1]
#define OS_SELF_TASK_ID
#define timeCompare(t1, t2)
Definition: os_port.h:40
SnmpViewType
View type.
Structure describing socket events.
Definition: socket.h:433
size_t maskLen
#define SNMP_AGENT_INFORM_TIMEOUT
SnmpUserEntry * snmpCreateCommunityEntry(SnmpAgentContext *context)
Create a new community entry.
SNMP agent (Simple Network Management Protocol)
@ ERROR_WRONG_STATE
Definition: error.h:211
SnmpVersion
SNMP version identifiers.
Definition: snmp_common.h:137
error_t snmpAgentSetContextEngine(SnmpAgentContext *context, const void *contextEngine, size_t contextEngineLen)
Set context engine identifier.
Definition: snmp_agent.c:688
uint16_t trapPort
SNMP trap port number.
Definition: snmp_agent.h:139
@ ERROR_OPEN_FAILED
Definition: error.h:75
@ SNMP_AGENT_STATE_SENDING_INFORM_REQ
char_t readViewName[SNMP_MAX_VIEW_NAME_LEN+1]
uint16_t destPort
Destination port.
Definition: socket.h:252
SnmpUserEntry * snmpCreateUserEntry(SnmpAgentContext *context)
Create a new user entry.
error_t snmpAgentDeleteAccess(SnmpAgentContext *context, const char_t *groupName, SnmpSecurityModel securityModel, SnmpSecurityLevel securityLevel, const char_t *contextPrefix)
Delete an existing access policy.
Definition: snmp_agent.c:1491
const IpAddr IP_ADDR_ANY
Definition: ip.c:53
error_t snmpAgentStart(SnmpAgentContext *context)
Start SNMP agent.
Definition: snmp_agent.c:246
error_t socketSendMsg(Socket *socket, const SocketMsg *message, uint_t flags)
Send a message to a connectionless socket.
Definition: socket.c:1760
void osDeleteTask(OsTaskId taskId)
Delete a task.
NetInterface * interface
Underlying network interface.
Definition: socket.h:248
SnmpKey localizedAuthKey
Localized authentication key.
#define SNMP_MAX_CONTEXT_NAME_LEN
Definition: snmp_common.h:74
#define FALSE
Definition: os_port.h:46
const SocketMsg SOCKET_DEFAULT_MSG
Definition: socket.c:49
uint_t numObjects
Definition: mib_common.h:297
size_t length
Actual length of the payload, in bytes.
Definition: socket.h:244
error_t snmpAgentLeaveGroup(SnmpAgentContext *context, const char_t *userName, SnmpSecurityModel securityModel)
Leave a group of users.
Definition: snmp_agent.c:1278
error_t snmpAgentLoadMib(SnmpAgentContext *context, const MibModule *module)
Load a MIB module.
Definition: snmp_agent.c:386
char_t notifyViewName[SNMP_MAX_VIEW_NAME_LEN+1]
error_t snmpAgentDeleteUser(SnmpAgentContext *context, const char_t *userName)
Remove existing user.
Definition: snmp_agent.c:1123
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
void osResetEvent(OsEvent *event)
Set the specified event object to the nonsignaled state.
#define osMemcpy(dest, src, length)
Definition: os_port.h:147
SNMP inform notifications.
@ SNMP_PRIV_PROTOCOL_NONE
No privacy.
error_t
Error codes.
Definition: error.h:43
bool_t ipIsMulticastAddr(const IpAddr *ipAddr)
Determine whether an IP address is a multicast address.
Definition: ip.c:251
#define MIB_MAX_OID_SIZE
Definition: mib_common.h:39
#define SNMP_MAX_USER_NAME_LEN
Definition: snmp_common.h:81
#define MIB2_SNMP_INC_COUNTER32(name, value)
Definition: mib2_module.h:169
void(* OsTaskCode)(void *arg)
Task routine.
SnmpContextMatch contextMatch
error_t snmpFormatInformRequestMessage(SnmpAgentContext *context, SnmpVersion version, const char_t *userName, uint_t genericTrapType, uint_t specificTrapCode, const SnmpTrapObject *objectList, uint_t objectListSize)
Format SNMP InformRequest message.
@ SNMP_AGENT_STATE_WAITING_GET_RESP
SnmpViewType type
SnmpUserEntry * snmpFindUserEntry(SnmpAgentContext *context, const char_t *name, size_t length)
Search the user table for a given user name.
SnmpPrivProtocol
@ SNMP_KEY_FORMAT_TEXT
ASCII password.
#define SNMP_TRAP_PORT
Definition: snmp_common.h:124
error_t snmpProcessMessage(SnmpAgentContext *context)
Process incoming SNMP message.
@ SNMP_VIEW_TYPE_INCLUDED
void osDeleteEvent(OsEvent *event)
Delete an event object.
@ SNMP_SECURITY_LEVEL_NO_AUTH_NO_PRIV
#define SNMP_MAX_BIT_MASK_SIZE
Definition: snmp_common.h:109
MibRowStatus status
IpAddr srcIpAddr
Source IP address.
Definition: socket.h:249
#define SNMP_MAX_OID_SIZE
Definition: snmp_common.h:116
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ MIB_ROW_STATUS_UNUSED
Definition: mib_common.h:102
General definitions for cryptographic library.
error_t socketReceiveMsg(Socket *socket, SocketMsg *message, uint_t flags)
Receive a message from a connectionless socket.
Definition: socket.c:1991
NetContext * netGetDefaultContext(void)
Get default TCP/IP stack context.
Definition: net.c:546
const OsTaskParameters OS_TASK_DEFAULT_PARAMS
SnmpViewEntry * snmpFindViewEntry(SnmpAgentContext *context, const char_t *viewName, const uint8_t *subtree, size_t subtreeLen)
Search the view table for a given entry.
uint8_t mask
Definition: web_socket.h:319
Helper functions for SNMP agent.
error_t snmpAgentJoinGroup(SnmpAgentContext *context, const char_t *userName, SnmpSecurityModel securityModel, const char_t *groupName)
Join a group of users.
Definition: snmp_agent.c:1179
@ SNMP_AGENT_STATE_IDLE
error_t snmpAgentSendTrap(SnmpAgentContext *context, const IpAddr *destIpAddr, SnmpVersion version, const char_t *userName, uint_t genericTrapType, uint_t specificTrapCode, const SnmpTrapObject *objectList, uint_t objectListSize)
Send SNMP trap notification.
Definition: snmp_agent.c:1727
@ SNMP_SECURITY_LEVEL_AUTH_PRIV
#define TRACE_INFO(...)
Definition: debug.h:105
error_t snmpAgentGetEngineBoots(SnmpAgentContext *context, int32_t *engineBoots)
Get the value of the snmpEngineBoots variable.
Definition: snmp_agent.c:617
error_t snmpAgentSendInform(SnmpAgentContext *context, const IpAddr *destIpAddr, SnmpVersion version, const char_t *userName, uint_t genericTrapType, uint_t specificTrapCode, const SnmpTrapObject *objectList, uint_t objectListSize)
Send SNMP inform request.
Definition: snmp_agent.c:1806
error_t snmpAgentSetEngineBoots(SnmpAgentContext *context, int32_t engineBoots)
Set the value of the snmpEngineBoots variable.
Definition: snmp_agent.c:573
MIB module.
Definition: mib_common.h:292
#define osEnterTask()
uint_t eventFlags
Returned events.
Definition: socket.h:436
MibRowStatus status
Status of the user.
error_t socketPoll(SocketEventDesc *eventDesc, uint_t size, OsEvent *extEvent, systime_t timeout)
Wait for one of a set of sockets to become ready to perform I/O.
Definition: socket.c:2259
#define socketBindToInterface
Definition: net_legacy.h:193
#define SNMP_AGENT_STACK_SIZE
Definition: snmp_agent.h:57
@ SNMP_VERSION_3
Definition: snmp_common.h:140
char_t viewName[SNMP_MAX_VIEW_NAME_LEN+1]
uint32_t systime_t
System time.
error_t snmpAgentCreateUser(SnmpAgentContext *context, const char_t *userName, SnmpAccess mode, SnmpKeyFormat keyFormat, SnmpAuthProtocol authProtocol, const void *authKey, SnmpPrivProtocol privProtocol, const void *privKey)
Create a new user.
Definition: snmp_agent.c:913
IpAddr destIpAddr
Destination IP address.
Definition: socket.h:251
SNMP secret key.
void snmpAgentGetDefaultSettings(SnmpAgentSettings *settings)
Initialize settings with default values.
Definition: snmp_agent.c:73
@ ERROR_TIMEOUT
Definition: error.h:95
char char_t
Definition: compiler_port.h:55
Ipv6Addr contextPrefix
Definition: ndp.h:519
@ MIB_ROW_STATUS_ACTIVE
Definition: mib_common.h:103
void snmpAgentTask(SnmpAgentContext *context)
SNMP agent task.
Definition: snmp_agent.c:2088
SnmpSecurityLevel securityLevel
uint16_t port
SNMP port number.
Definition: snmp_agent.h:138
uint32_t time
MibRowStatus status
error_t snmpAgentCreateView(SnmpAgentContext *context, const char_t *viewName, const uint8_t *subtree, size_t subtreeLen, const uint8_t *mask, size_t maskLen, SnmpViewType type)
Create a new MIB view.
Definition: snmp_agent.c:1553
#define TRACE_DEBUG_ARRAY(p, a, n)
Definition: debug.h:120
void osDeleteMutex(OsMutex *mutex)
Delete a mutex object.
SnmpAuthProtocol authProtocol
Authentication protocol.
char_t name[SNMP_MAX_USER_NAME_LEN+1]
User name.
SnmpAgentRandCallback randCallback
Random data generation callback function.
Definition: snmp_agent.h:140
@ SOCKET_EVENT_RX_READY
Definition: socket.h:179
#define SNMP_MAX_MSG_SIZE
Definition: snmp_common.h:60
uint8_t n
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
uint8_t mask[SNMP_MAX_BIT_MASK_SIZE]
NetInterface * interface
Underlying network interface.
Definition: snmp_agent.h:135
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
NetContext * netContext
TCP/IP stack context.
Definition: snmp_agent.h:134
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
Socket * socketOpenEx(NetContext *context, uint_t type, uint_t protocol)
Create a socket.
Definition: socket.c:143
View table entry.
Access table entry.
@ SNMP_AGENT_STATE_SENDING_GET_REQ
bool_t osCreateEvent(OsEvent *event)
Create an event object.
SnmpVersion versionMin
Minimum version accepted by the SNMP agent.
Definition: snmp_agent.h:136
SnmpKey rawAuthKey
Raw authentication key.
error_t snmpLocalizeKey(SnmpAuthProtocol authProtocol, const uint8_t *engineId, size_t engineIdLen, SnmpKey *key, SnmpKey *localizedKey)
Key localization algorithm.
error_t snmpFormatTrapMessage(SnmpAgentContext *context, SnmpVersion version, const char_t *userName, uint_t genericTrapType, uint_t specificTrapCode, const SnmpTrapObject *objectList, uint_t objectListSize)
Format SNMP Trap message.
size_t size
Size of the payload, in bytes.
Definition: socket.h:243
SnmpAuthProtocol
error_t snmpAgentCreateAccess(SnmpAgentContext *context, const char_t *groupName, SnmpSecurityModel securityModel, SnmpSecurityLevel securityLevel, const char_t *contextPrefix, SnmpContextMatch contextMatch, const char_t *readViewName, const char_t *writeViewName, const char_t *notifyViewName)
Create access policy for the specified group name.
Definition: snmp_agent.c:1341
error_t socketSendTo(Socket *socket, const IpAddr *destIpAddr, uint16_t destPort, const void *data, size_t length, size_t *written, uint_t flags)
Send a datagram to a specific destination.
Definition: socket.c:1626
@ SNMP_VERSION_1
Definition: snmp_common.h:138
#define SNMP_PORT
Definition: snmp_common.h:122
SnmpPrivProtocol privProtocol
Privacy protocol.
SnmpSecurityModel securityModel
#define SNMP_MAX_CONTEXT_ENGINE_SIZE
Definition: snmp_common.h:67
#define SnmpAgentContext
Definition: snmp_agent.h:36
error_t snmpGenerateKey(SnmpAuthProtocol authProtocol, const char_t *password, SnmpKey *key)
Password to key algorithm.
error_t snmpAgentUnloadMib(SnmpAgentContext *context, const MibModule *module)
Unload a MIB module.
Definition: snmp_agent.c:475
void osDelayTask(systime_t delay)
Delay routine.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
SnmpSecurityLevel
Security levels.
SNMP agent (PDU processing)
@ SNMP_SECURITY_MODEL_V2C
SNMPv2c.
Group table entry.
Socket * socket
Handle to a socket to monitor.
Definition: socket.h:434
void snmpRefreshEngineTime(SnmpAgentContext *context)
Refresh SNMP engine time.
#define PRIuSIZE
unsigned int uint_t
Definition: compiler_port.h:57
#define osMemset(p, value, length)
Definition: os_port.h:141
TCP/IP stack core.
@ SNMP_AUTH_PROTOCOL_NONE
No authentication.
void snmpAgentDeinit(SnmpAgentContext *context)
Release SNMP agent context.
Definition: snmp_agent.c:2217
@ SNMP_KEY_FORMAT_RAW
Raw key.
@ SNMP_VIEW_TYPE_EXCLUDED
error_t snmpAgentDeleteCommunity(SnmpAgentContext *context, const char_t *community)
Remove a community string.
Definition: snmp_agent.c:853
SNMP agent settings.
Definition: snmp_agent.h:132
#define osStrcpy(s1, s2)
Definition: os_port.h:213
uint16_t srcPort
Source port.
Definition: socket.h:250
error_t socketSetTimeout(Socket *socket, systime_t timeout)
Set timeout value for blocking operations.
Definition: socket.c:166
SnmpKey rawPrivKey
Raw privacy key.
SnmpAccess
Access modes.
uint_t eventMask
Requested events.
Definition: socket.h:435
@ ERROR_ALREADY_RUNNING
Definition: error.h:295
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
@ SNMP_SECURITY_LEVEL_AUTH_NO_PRIV
ASN.1 (Abstract Syntax Notation One)
error_t snmpAgentStop(SnmpAgentContext *context)
Stop SNMP agent.
Definition: snmp_agent.c:342
SnmpAccessEntry * snmpFindAccessEntry(SnmpAgentContext *context, const char_t *groupName, const char_t *contextPrefix, uint_t securityModel, uint_t securityLevel)
Search the access table for a given entry.
#define INFINITE_DELAY
Definition: os_port.h:75
error_t snmpFormatGetRequestMessage(SnmpAgentContext *context, SnmpVersion version)
Format SNMP GetRequest message.
MibRowStatus status
error_t snmpAgentSetEnterpriseOid(SnmpAgentContext *context, const uint8_t *enterpriseOid, size_t enterpriseOidLen)
Set enterprise OID.
Definition: snmp_agent.c:650
systime_t osGetSystemTime(void)
Retrieve system time.
SNMP message dispatching.
Ipv4Addr destIpAddr
Definition: ipcp.h:80