snmp_agent_trap.c
Go to the documentation of this file.
1 /**
2  * @file snmp_agent_trap.c
3  * @brief SNMP trap notifications
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.8
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL SNMP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "snmp/snmp_agent.h"
37 #include "snmp/snmp_agent_misc.h"
38 #include "snmp/snmp_agent_trap.h"
39 #include "mibs/mib2_module.h"
40 #include "debug.h"
41 
42 //Check TCP/IP stack configuration
43 #if (SNMP_AGENT_SUPPORT == ENABLED && SNMP_AGENT_TRAP_SUPPORT == ENABLED)
44 
45 
46 /**
47  * @brief Format SNMP Trap message
48  * @param[in] context Pointer to the SNMP agent context
49  * @param[in] version SNMP version identifier
50  * @param[in] userName User name or community name
51  * @param[in] genericTrapType Generic trap type
52  * @param[in] specificTrapCode Specific code
53  * @param[in] objectList List of object names
54  * @param[in] objectListSize Number of entries in the list
55  * @return Error code
56  **/
57 
59  const char_t *userName, uint_t genericTrapType, uint_t specificTrapCode,
60  const SnmpTrapObject *objectList, uint_t objectListSize)
61 {
62  error_t error;
63 
64 #if (SNMP_V1_SUPPORT == ENABLED)
65  //SNMPv1 version?
66  if(version == SNMP_VERSION_1)
67  {
68  //Format Trap-PDU
69  error = snmpFormatTrapPdu(context, version, userName,
70  genericTrapType, specificTrapCode, objectList, objectListSize);
71  //Any error to report?
72  if(error)
73  {
74  return error;
75  }
76 
77  //Format SMNP message header
78  error = snmpWriteMessageHeader(&context->response);
79  //Any error to report?
80  if(error)
81  {
82  return error;
83  }
84  }
85  else
86 #endif
87 #if (SNMP_V2C_SUPPORT == ENABLED)
88  //SNMPv2c version?
90  {
91  //Format SNMPv2-Trap-PDU
92  error = snmpFormatTrapPdu(context, version, userName,
93  genericTrapType, specificTrapCode, objectList, objectListSize);
94  //Any error to report?
95  if(error)
96  {
97  return error;
98  }
99 
100  //Format SMNP message header
101  error = snmpWriteMessageHeader(&context->response);
102  //Any error to report?
103  if(error)
104  {
105  return error;
106  }
107  }
108  else
109 #endif
110 #if (SNMP_V3_SUPPORT == ENABLED)
111  //SNMPv3 version?
112  if(version == SNMP_VERSION_3)
113  {
114  SnmpUserEntry *user;
115 
116  //Information about the user name is extracted from the local
117  //configuration datastore
118  user = snmpFindUserEntry(context, userName, osStrlen(userName));
119 
120  //Invalid user name?
121  if(user == NULL || user->status != MIB_ROW_STATUS_ACTIVE)
122  {
124  }
125 
126  //Save the security profile associated with the current user
127  context->user = *user;
128 
129  //Format SNMPv2-Trap-PDU
130  error = snmpFormatTrapPdu(context, version, userName,
131  genericTrapType, specificTrapCode, objectList, objectListSize);
132  //Any error to report?
133  if(error)
134  {
135  return error;
136  }
137 
138  //Format scopedPDU
139  error = snmpWriteScopedPdu(&context->response);
140  //Any error to report?
141  if(error)
142  {
143  return error;
144  }
145 
146  //Check whether the privFlag is set
147  if((context->response.msgFlags & SNMP_MSG_FLAG_PRIV) != 0)
148  {
149  //Encrypt data
150  error = snmpEncryptData(&context->user, &context->response,
151  context->salt);
152  //Any error to report?
153  if(error)
154  {
155  return error;
156  }
157  }
158 
159  //Format SMNP message header
160  error = snmpWriteMessageHeader(&context->response);
161  //Any error to report?
162  if(error)
163  {
164  return error;
165  }
166 
167  //Check whether the authFlag is set
168  if((context->response.msgFlags & SNMP_MSG_FLAG_AUTH) != 0)
169  {
170  //Authenticate outgoing SNMP message
171  error = snmpAuthOutgoingMessage(&context->user, &context->response);
172  //Any error to report?
173  if(error)
174  {
175  return error;
176  }
177  }
178  }
179  else
180 #endif
181  //Invalid SNMP version?
182  {
183  //Debug message
184  TRACE_WARNING(" Invalid SNMP version!\r\n");
185  //Report an error
186  return ERROR_INVALID_VERSION;
187  }
188 
189  //Successful processing
190  return NO_ERROR;
191 }
192 
193 
194 /**
195  * @brief Format Trap-PDU or SNMPv2-Trap-PDU
196  * @param[in] context Pointer to the SNMP agent context
197  * @param[in] version SNMP version identifier
198  * @param[in] userName User name or community name
199  * @param[in] genericTrapType Generic trap type
200  * @param[in] specificTrapCode Specific code
201  * @param[in] objectList List of object names
202  * @param[in] objectListSize Number of entries in the list
203  * @return Error code
204  **/
205 
207  const char_t *userName, uint_t genericTrapType, uint_t specificTrapCode,
208  const SnmpTrapObject *objectList, uint_t objectListSize)
209 {
210  error_t error;
212 
213  //Point to the SNMP message
214  message = &context->response;
215  //Initialize SNMP message
217 
218  //SNMP version identifier
219  message->version = version;
220 
221 #if (SNMP_V1_SUPPORT == ENABLED)
222  //SNMPv1 version?
223  if(version == SNMP_VERSION_1)
224  {
225 #if (IPV4_SUPPORT == ENABLED)
226  NetInterface *interface;
227 
228  //Point to the underlying network interface
229  interface = context->interface;
230 #endif
231 
232  //Community name
233  message->community = userName;
234  message->communityLen = osStrlen(userName);
235 
236  //Prepare to send a Trap-PDU
237  message->pduType = SNMP_PDU_TRAP;
238  //Type of object generating trap
239  message->enterpriseOid = context->enterpriseOid;
240  message->enterpriseOidLen = context->enterpriseOidLen;
241 
242 #if (IPV4_SUPPORT == ENABLED)
243  //Address of object generating trap
244  if(interface != NULL)
245  {
246  message->agentAddr = interface->ipv4Context.addrList[0].addr;
247  }
248 #endif
249 
250  //Generic trap type
251  message->genericTrapType = genericTrapType;
252  //Specific trap code
253  message->specificTrapCode = specificTrapCode;
254  //Timestamp
255  message->timestamp = osGetSystemTime64() / 10;
256  }
257  else
258 #endif
259 #if (SNMP_V2C_SUPPORT == ENABLED)
260  //SNMPv2c version?
261  if(version == SNMP_VERSION_2C)
262  {
263  //Community name
264  message->community = userName;
265  message->communityLen = osStrlen(userName);
266 
267  //Prepare to send a SNMPv2-Trap-PDU
268  message->pduType = SNMP_PDU_TRAP_V2;
269  }
270  else
271 #endif
272 #if (SNMP_V3_SUPPORT == ENABLED)
273  //SNMPv3 version?
274  if(version == SNMP_VERSION_3)
275  {
276  //Increment message identifier
277  message->msgId = context->msgId++;
278  //Wrap around if necessary
279  if(context->msgId < 0)
280  {
281  context->msgId = 0;
282  }
283 
284  //Maximum message size supported by the sender
285  message->msgMaxSize = SNMP_MAX_MSG_SIZE;
286 
287  //Bit fields which control processing of the message
288  if(context->user.authProtocol != SNMP_AUTH_PROTOCOL_NONE)
289  {
290  message->msgFlags |= SNMP_MSG_FLAG_AUTH;
291  }
292 
293  if(context->user.privProtocol != SNMP_PRIV_PROTOCOL_NONE)
294  {
295  message->msgFlags |= SNMP_MSG_FLAG_PRIV;
296  }
297 
298  //Security model used by the sender
299  message->msgSecurityModel = SNMP_SECURITY_MODEL_USM;
300 
301  //Authoritative engine identifier
302  message->msgAuthEngineId = context->contextEngine;
303  message->msgAuthEngineIdLen = context->contextEngineLen;
304  //Number of times the SNMP engine has rebooted
305  message->msgAuthEngineBoots = context->engineBoots;
306  //Number of seconds since last reboot
307  message->msgAuthEngineTime = context->engineTime;
308  //User name
309  message->msgUserName = userName;
310  message->msgUserNameLen = osStrlen(userName);
311  //Authentication parameters
312  message->msgAuthParameters = NULL;
313  //Length of the authentication parameters
314  message->msgAuthParametersLen = snmpGetMacLength(context->user.authProtocol);
315  //Privacy parameters
316  message->msgPrivParameters = context->privParameters;
317 
318  //Length of the privacy parameters
319  if(context->user.privProtocol == SNMP_PRIV_PROTOCOL_DES)
320  {
321  message->msgPrivParametersLen = 8;
322  }
323  else if(context->user.privProtocol == SNMP_PRIV_PROTOCOL_AES)
324  {
325  message->msgPrivParametersLen = 8;
326  }
327  else
328  {
329  message->msgPrivParametersLen = 0;
330  }
331 
332  //Context engine identifier
333  message->contextEngineId = context->contextEngine;
334  message->contextEngineIdLen = context->contextEngineLen;
335  //Context name
336  message->contextName = context->contextName;
337  message->contextNameLen = osStrlen(context->contextName);
338 
339  //Prepare to send a SNMPv2-Trap-PDU
340  message->pduType = SNMP_PDU_TRAP_V2;
341  }
342  else
343 #endif
344  //Invalid SNMP version?
345  {
346  //Report an error
347  return ERROR_INVALID_VERSION;
348  }
349 
350  //Increment request identifier
351  message->requestId = context->requestId++;
352  //Wrap around if necessary
353  if(context->requestId < 0)
354  {
355  context->requestId = 0;
356  }
357 
358  //Make room for the message header at the beginning of the buffer
359  error = snmpComputeMessageOverhead(&context->response);
360  //Any error to report?
361  if(error)
362  {
363  return error;
364  }
365 
366  //Format the list of variable bindings
367  error = snmpWriteTrapVarBindingList(context, genericTrapType,
368  specificTrapCode, objectList, objectListSize);
369  //Any error to report?
370  if(error)
371  {
372  return error;
373  }
374 
375  //Total number of SNMP Trap PDUs which have been generated by
376  //the SNMP protocol entity
377  MIB2_SNMP_INC_COUNTER32(snmpOutTraps, 1);
378 
379  //Format PDU header
380  error = snmpWritePduHeader(&context->response);
381  //Return status code
382  return error;
383 }
384 
385 #endif
error_t snmpFormatTrapPdu(SnmpAgentContext *context, SnmpVersion version, const char_t *userName, uint_t genericTrapType, uint_t specificTrapCode, const SnmpTrapObject *objectList, uint_t objectListSize)
Format Trap-PDU or SNMPv2-Trap-PDU.
MIB-II module.
@ SNMP_PDU_TRAP
Definition: snmp_common.h:154
@ ERROR_UNKNOWN_USER_NAME
Definition: error.h:264
@ SNMP_PDU_TRAP_V2
Definition: snmp_common.h:157
uint8_t message[]
Definition: chap.h:154
error_t snmpWriteScopedPdu(SnmpMessage *message)
Format scopedPDU.
Object descriptor for trap notifications.
@ SNMP_SECURITY_MODEL_USM
User-based security model.
#define osStrlen(s)
Definition: os_port.h:171
SNMP trap notifications.
uint8_t version
Definition: coap_common.h:177
User table entry.
@ SNMP_VERSION_2C
Definition: snmp_common.h:139
@ ERROR_INVALID_VERSION
Definition: error.h:118
SNMP agent (Simple Network Management Protocol)
SnmpVersion
SNMP version identifiers.
Definition: snmp_common.h:137
@ SNMP_MSG_FLAG_PRIV
@ SNMP_PRIV_PROTOCOL_NONE
No privacy.
error_t
Error codes.
Definition: error.h:43
#define MIB2_SNMP_INC_COUNTER32(name, value)
Definition: mib2_module.h:169
SnmpUserEntry * snmpFindUserEntry(SnmpAgentContext *context, const char_t *name, size_t length)
Search the user table for a given user name.
#define NetInterface
Definition: net.h:40
Helper functions for SNMP agent.
@ SNMP_MSG_FLAG_AUTH
MibRowStatus status
Status of the user.
@ SNMP_VERSION_3
Definition: snmp_common.h:140
size_t snmpGetMacLength(SnmpAuthProtocol authProtocol)
Get the length of the truncated MAC for a given authentication protocol.
#define TRACE_WARNING(...)
Definition: debug.h:93
char char_t
Definition: compiler_port.h:55
@ MIB_ROW_STATUS_ACTIVE
Definition: mib_common.h:103
#define SNMP_MAX_MSG_SIZE
Definition: snmp_common.h:60
error_t snmpWritePduHeader(SnmpMessage *message)
Format PDU header.
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.
#define osGetSystemTime64()
SNMP message.
@ SNMP_VERSION_1
Definition: snmp_common.h:138
#define SnmpAgentContext
Definition: snmp_agent.h:36
error_t snmpComputeMessageOverhead(SnmpMessage *message)
Compute SNMP message overhead.
error_t snmpAuthOutgoingMessage(const SnmpUserEntry *user, SnmpMessage *message)
Authenticate outgoing SNMP message.
@ SNMP_PRIV_PROTOCOL_AES
AES-128-CFB.
error_t snmpEncryptData(const SnmpUserEntry *user, SnmpMessage *message, uint8_t *salt)
Data encryption.
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
@ SNMP_AUTH_PROTOCOL_NONE
No authentication.
@ SNMP_PRIV_PROTOCOL_DES
DES-CBC.
error_t snmpWriteTrapVarBindingList(SnmpAgentContext *context, uint_t genericTrapType, uint_t specificTrapCode, const SnmpTrapObject *objectList, uint_t objectListSize)
Format the variable binding list for Trap-PDU or SNMPv2-Trap-PDU.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
void snmpInitMessage(SnmpMessage *message)
Initialize a SNMP message.
error_t snmpWriteMessageHeader(SnmpMessage *message)
Format SNMP message header.