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