STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
snmp_traps.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  *
29  * Author: Martin Hentschel
30  * Christiaan Simons <christiaan.simons@axon.tv>
31  *
32  */
33 
34 #include "lwip/apps/snmp_opts.h"
35 
36 #if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
37 
38 #include "lwip/snmp.h"
39 #include "lwip/sys.h"
40 #include "lwip/apps/snmp.h"
41 #include "lwip/apps/snmp_core.h"
42 #include "snmp_msg.h"
43 #include "snmp_asn1.h"
44 
45 #include <string.h>
46 
47 struct snmp_msg_trap
48 {
49  /* source enterprise ID (sysObjectID) */
50  const struct snmp_obj_id *enterprise;
51  /* source IP address, raw network order format */
52  ip_addr_t sip;
53  /* generic trap code */
54  u32_t gen_trap;
55  /* specific trap code */
56  u32_t spc_trap;
57  /* timestamp */
58  u32_t ts;
59  /* snmp_version */
60  u32_t snmp_version;
61 
62  /* output trap lengths used in ASN encoding */
63  /* encoding pdu length */
64  u16_t pdulen;
65  /* encoding community length */
66  u16_t comlen;
67  /* encoding sequence length */
68  u16_t seqlen;
69 };
70 
71 static u16_t snmp_trap_header_sum(struct snmp_msg_trap *trap);
72 static void snmp_trap_header_enc(struct snmp_msg_trap *trap, struct snmp_pbuf_stream *pbuf_stream);
73 
75 extern const char *snmp_community_trap;
76 
77 void* snmp_traps_handle;
78 
79 struct snmp_trap_dst
80 {
81  /* destination IP address in network order */
82  ip_addr_t dip;
83  /* set to 0 when disabled, >0 when enabled */
84  u8_t enable;
85 };
86 static struct snmp_trap_dst trap_dst[SNMP_TRAP_DESTINATIONS];
87 
88 static u8_t snmp_auth_traps_enabled = 0;
89 
95 void
96 snmp_trap_dst_enable(u8_t dst_idx, u8_t enable)
97 {
98  if (dst_idx < SNMP_TRAP_DESTINATIONS) {
99  trap_dst[dst_idx].enable = enable;
100  }
101 }
102 
108 void
109 snmp_trap_dst_ip_set(u8_t dst_idx, const ip_addr_t *dst)
110 {
111  if (dst_idx < SNMP_TRAP_DESTINATIONS) {
112  ip_addr_set(&trap_dst[dst_idx].dip, dst);
113  }
114 }
115 
116 void
117 snmp_set_auth_traps_enabled(u8_t enable)
118 {
119  snmp_auth_traps_enabled = enable;
120 }
121 
122 u8_t
123 snmp_get_auth_traps_enabled(void)
124 {
125  return snmp_auth_traps_enabled;
126 }
127 
128 
143 static err_t
144 snmp_send_trap(const struct snmp_obj_id *device_enterprise_oid, s32_t generic_trap, s32_t specific_trap)
145 {
146  struct snmp_msg_trap trap_msg;
147  struct snmp_trap_dst *td;
148  struct pbuf *p;
149  u16_t i, tot_len;
150  err_t err = ERR_OK;
151 
152  trap_msg.snmp_version = 0;
153 
154  for (i = 0, td = &trap_dst[0]; i < SNMP_TRAP_DESTINATIONS; i++, td++) {
155  if ((td->enable != 0) && !ip_addr_isany(&td->dip)) {
156  /* lookup current source address for this dst */
157  if (snmp_get_local_ip_for_dst(snmp_traps_handle, &td->dip, &trap_msg.sip)) {
158  if (device_enterprise_oid == NULL) {
159  trap_msg.enterprise = snmp_get_device_enterprise_oid();
160  } else {
161  trap_msg.enterprise = device_enterprise_oid;
162  }
163 
164  trap_msg.gen_trap = generic_trap;
165  if (generic_trap == SNMP_GENTRAP_ENTERPRISE_SPECIFIC) {
166  trap_msg.spc_trap = specific_trap;
167  } else {
168  trap_msg.spc_trap = 0;
169  }
170 
171  MIB2_COPY_SYSUPTIME_TO(&trap_msg.ts);
172 
173  /* pass 0, calculate length fields */
174  tot_len = snmp_trap_header_sum(&trap_msg);
175 
176  /* allocate pbuf(s) */
177  p = pbuf_alloc(PBUF_TRANSPORT, tot_len, PBUF_RAM);
178  if (p != NULL) {
179  struct snmp_pbuf_stream pbuf_stream;
180  snmp_pbuf_stream_init(&pbuf_stream, p, 0, tot_len);
181 
182  /* pass 1, encode packet ino the pbuf(s) */
183  snmp_trap_header_enc(&trap_msg, &pbuf_stream);
184 
185  snmp_stats.outtraps++;
186  snmp_stats.outpkts++;
187 
189  snmp_sendto(snmp_traps_handle, p, &td->dip, SNMP_TRAP_PORT);
190  } else {
191  err = ERR_MEM;
192  }
193  } else {
194  /* routing error */
195  err = ERR_RTE;
196  }
197  }
198  }
199  return err;
200 }
201 
202 err_t
203 snmp_send_trap_generic(s32_t generic_trap)
204 {
205  return snmp_send_trap(NULL, generic_trap, 0);
206 }
207 
208 err_t
209 snmp_send_trap_specific(s32_t specific_trap)
210 {
211  return snmp_send_trap(NULL, SNMP_GENTRAP_ENTERPRISE_SPECIFIC, specific_trap);
212 }
213 
214 void
215 snmp_coldstart_trap(void)
216 {
217  snmp_send_trap_generic(SNMP_GENTRAP_COLDSTART);
218 }
219 
220 void
221 snmp_authfail_trap(void)
222 {
223  if (snmp_auth_traps_enabled != 0) {
224  snmp_send_trap_generic(SNMP_GENTRAP_AUTH_FAILURE);
225  }
226 }
227 
236 static u16_t
237 snmp_trap_header_sum(struct snmp_msg_trap *trap)
238 {
239  u16_t tot_len;
240  u16_t len;
241  u8_t lenlen;
242 
243  tot_len = 0;
244 
245  snmp_asn1_enc_u32t_cnt(trap->ts, &len);
246  snmp_asn1_enc_length_cnt(len, &lenlen);
247  tot_len += 1 + len + lenlen;
248 
249  snmp_asn1_enc_s32t_cnt(trap->spc_trap, &len);
250  snmp_asn1_enc_length_cnt(len, &lenlen);
251  tot_len += 1 + len + lenlen;
252 
253  snmp_asn1_enc_s32t_cnt(trap->gen_trap, &len);
254  snmp_asn1_enc_length_cnt(len, &lenlen);
255  tot_len += 1 + len + lenlen;
256 
257  if(IP_IS_V6_VAL(trap->sip)) {
258 #if LWIP_IPV6
259  len = sizeof(ip_2_ip6(&trap->sip)->addr);
260 #endif
261  } else {
262 #if LWIP_IPV4
263  len = sizeof(ip_2_ip4(&trap->sip)->addr);
264 #endif
265  }
266  snmp_asn1_enc_length_cnt(len, &lenlen);
267  tot_len += 1 + len + lenlen;
268 
269  snmp_asn1_enc_oid_cnt(trap->enterprise->id, trap->enterprise->len, &len);
270  snmp_asn1_enc_length_cnt(len, &lenlen);
271  tot_len += 1 + len + lenlen;
272 
273  trap->pdulen = tot_len;
274  snmp_asn1_enc_length_cnt(trap->pdulen, &lenlen);
275  tot_len += 1 + lenlen;
276 
277  trap->comlen = (u16_t)strlen(snmp_community_trap);
278  snmp_asn1_enc_length_cnt(trap->comlen, &lenlen);
279  tot_len += 1 + lenlen + trap->comlen;
280 
281  snmp_asn1_enc_s32t_cnt(trap->snmp_version, &len);
282  snmp_asn1_enc_length_cnt(len, &lenlen);
283  tot_len += 1 + len + lenlen;
284 
285  trap->seqlen = tot_len;
286  snmp_asn1_enc_length_cnt(trap->seqlen, &lenlen);
287  tot_len += 1 + lenlen;
288 
289  return tot_len;
290 }
291 
295 static void
296 snmp_trap_header_enc(struct snmp_msg_trap *trap, struct snmp_pbuf_stream *pbuf_stream)
297 {
298  struct snmp_asn1_tlv tlv;
299 
300  /* 'Message' sequence */
301  SNMP_ASN1_SET_TLV_PARAMS(tlv, SNMP_ASN1_TYPE_SEQUENCE, 0, trap->seqlen);
302  snmp_ans1_enc_tlv(pbuf_stream, &tlv);
303 
304  /* version */
305  SNMP_ASN1_SET_TLV_PARAMS(tlv, SNMP_ASN1_TYPE_INTEGER, 0, 0);
306  snmp_asn1_enc_s32t_cnt(trap->snmp_version, &tlv.value_len);
307  snmp_ans1_enc_tlv(pbuf_stream, &tlv);
308  snmp_asn1_enc_s32t(pbuf_stream, tlv.value_len, trap->snmp_version);
309 
310  /* community */
311  SNMP_ASN1_SET_TLV_PARAMS(tlv, SNMP_ASN1_TYPE_OCTET_STRING, 0, trap->comlen);
312  snmp_ans1_enc_tlv(pbuf_stream, &tlv);
313  snmp_asn1_enc_raw(pbuf_stream, (const u8_t *)snmp_community_trap, trap->comlen);
314 
315  /* 'PDU' sequence */
316  SNMP_ASN1_SET_TLV_PARAMS(tlv, (SNMP_ASN1_CLASS_CONTEXT | SNMP_ASN1_CONTENTTYPE_CONSTRUCTED | SNMP_ASN1_CONTEXT_PDU_TRAP), 0, trap->pdulen);
317  snmp_ans1_enc_tlv(pbuf_stream, &tlv);
318 
319  /* object ID */
320  SNMP_ASN1_SET_TLV_PARAMS(tlv, SNMP_ASN1_TYPE_OBJECT_ID, 0, 0);
321  snmp_asn1_enc_oid_cnt(trap->enterprise->id, trap->enterprise->len, &tlv.value_len);
322  snmp_ans1_enc_tlv(pbuf_stream, &tlv);
323  snmp_asn1_enc_oid(pbuf_stream, trap->enterprise->id, trap->enterprise->len);
324 
325  /* IP addr */
326  if(IP_IS_V6_VAL(trap->sip)) {
327 #if LWIP_IPV6
328  SNMP_ASN1_SET_TLV_PARAMS(tlv, SNMP_ASN1_TYPE_IPADDR, 0, sizeof(ip_2_ip6(&trap->sip)->addr));
329  snmp_ans1_enc_tlv(pbuf_stream, &tlv);
330  snmp_asn1_enc_raw(pbuf_stream, (const u8_t *)&ip_2_ip6(&trap->sip)->addr, sizeof(ip_2_ip6(&trap->sip)->addr));
331 #endif
332  } else {
333 #if LWIP_IPV4
334  SNMP_ASN1_SET_TLV_PARAMS(tlv, SNMP_ASN1_TYPE_IPADDR, 0, sizeof(ip_2_ip4(&trap->sip)->addr));
335  snmp_ans1_enc_tlv(pbuf_stream, &tlv);
336  snmp_asn1_enc_raw(pbuf_stream, (const u8_t *)&ip_2_ip4(&trap->sip)->addr, sizeof(ip_2_ip4(&trap->sip)->addr));
337 #endif
338  }
339 
340  /* trap length */
341  SNMP_ASN1_SET_TLV_PARAMS(tlv, SNMP_ASN1_TYPE_INTEGER, 0, 0);
342  snmp_asn1_enc_s32t_cnt(trap->gen_trap, &tlv.value_len);
343  snmp_ans1_enc_tlv(pbuf_stream, &tlv);
344  snmp_asn1_enc_s32t(pbuf_stream, tlv.value_len, trap->gen_trap);
345 
346  /* specific trap */
347  SNMP_ASN1_SET_TLV_PARAMS(tlv, SNMP_ASN1_TYPE_INTEGER, 0, 0);
348  snmp_asn1_enc_s32t_cnt(trap->spc_trap, &tlv.value_len);
349  snmp_ans1_enc_tlv(pbuf_stream, &tlv);
350  snmp_asn1_enc_s32t(pbuf_stream, tlv.value_len, trap->spc_trap);
351 
352  /* timestamp */
353  SNMP_ASN1_SET_TLV_PARAMS(tlv, SNMP_ASN1_TYPE_TIMETICKS, 0, 0);
354  snmp_asn1_enc_s32t_cnt(trap->ts, &tlv.value_len);
355  snmp_ans1_enc_tlv(pbuf_stream, &tlv);
356  snmp_asn1_enc_s32t(pbuf_stream, tlv.value_len, trap->ts);
357 }
358 
359 #endif /* LWIP_SNMP */
#define ip_addr_isany(ipaddr)
Definition: ip_addr.h:215
#define ip_2_ip6(ipaddr)
Definition: ip_addr.h:200
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:199
Definition: pbuf.h:77
#define NULL
Definition: usbd_def.h:53
unsigned long u32_t
Definition: cc.h:42
#define ERR_RTE
Definition: err.h:56
#define MIB2_COPY_SYSUPTIME_TO(ptrToVal)
Definition: snmp.h:118
#define ERR_OK
Definition: err.h:52
u16_t tot_len
Definition: pbuf.h:122
#define IP_IS_V6_VAL(ipaddr)
Definition: ip_addr.h:196
Definition: pbuf.h:108
s8_t err_t
Definition: err.h:47
#define ip_addr_set(dest, src)
Definition: ip_addr.h:205
#define SNMP_TRAP_DESTINATIONS
Definition: snmp_opts.h:96
unsigned char u8_t
Definition: cc.h:38
ip6_addr_t ip_addr_t
Definition: ip_addr.h:194
signed long s32_t
Definition: cc.h:43
#define ERR_MEM
Definition: err.h:53
unsigned short u16_t
Definition: cc.h:40