Contiki-NG
snmp-engine.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*---------------------------------------------------------------------------*/
32 
33 /**
34  * \file
35  * An implementation of the Simple Network Management Protocol (RFC 3411-3418)
36  * \author
37  * Yago Fontoura do Rosario <yago.rosario@hotmail.com.br
38  */
39 
40 #include "contiki.h"
41 
42 #include "snmp-engine.h"
43 #include "snmp-message.h"
44 #include "snmp-mib.h"
45 #include "snmp-oid.h"
46 
47 #define LOG_MODULE "SNMP [engine]"
48 #define LOG_LEVEL LOG_LEVEL_SNMP
49 
50 /*---------------------------------------------------------------------------*/
51 int
52 snmp_engine_get(snmp_header_t *header, snmp_varbind_t *varbinds, uint32_t varbinds_length)
53 {
54  snmp_mib_resource_t *resource;
55  uint32_t i;
56 
57  for(i = 0; i < varbinds_length; i++) {
58  resource = snmp_mib_find(varbinds[i].oid);
59  if(!resource) {
60  switch(header->version) {
61  case SNMP_VERSION_1:
62  header->error_status_non_repeaters.error_status = SNMP_STATUS_NO_SUCH_NAME;
63  /*
64  * Varbinds are 1 indexed
65  */
66  header->error_index_max_repetitions.error_index = i + 1;
67  break;
68  case SNMP_VERSION_2C:
69  (&varbinds[i])->value_type = SNMP_DATA_TYPE_NO_SUCH_INSTANCE;
70  break;
71  default:
72  header->error_status_non_repeaters.error_status = SNMP_STATUS_NO_SUCH_NAME;
73  header->error_index_max_repetitions.error_index = 0;
74  }
75  } else {
76  resource->handler(&varbinds[i], resource->oid);
77  }
78  }
79 
80  return 0;
81 }
82 /*---------------------------------------------------------------------------*/
83 int
84 snmp_engine_get_next(snmp_header_t *header, snmp_varbind_t *varbinds, uint32_t varbinds_length)
85 {
86  snmp_mib_resource_t *resource;
87  uint32_t i;
88 
89  for(i = 0; i < varbinds_length; i++) {
90  resource = snmp_mib_find_next(varbinds[i].oid);
91  if(!resource) {
92  switch(header->version) {
93  case SNMP_VERSION_1:
94  header->error_status_non_repeaters.error_status = SNMP_STATUS_NO_SUCH_NAME;
95  /*
96  * Varbinds are 1 indexed
97  */
98  header->error_index_max_repetitions.error_index = i + 1;
99  break;
100  case SNMP_VERSION_2C:
101  (&varbinds[i])->value_type = SNMP_DATA_TYPE_END_OF_MIB_VIEW;
102  break;
103  default:
104  header->error_status_non_repeaters.error_status = SNMP_STATUS_NO_SUCH_NAME;
105  header->error_index_max_repetitions.error_index = 0;
106  }
107  } else {
108  resource->handler(&varbinds[i], resource->oid);
109  }
110  }
111 
112  return 0;
113 }
114 /*---------------------------------------------------------------------------*/
115 int
116 snmp_engine_get_bulk(snmp_header_t *header, snmp_varbind_t *varbinds, uint32_t *varbinds_length)
117 {
118  snmp_mib_resource_t *resource;
119  uint32_t i, j, original_varbinds_length;
121  uint8_t repeater;
122 
123  /*
124  * A local copy of the requested oids must be kept since
125  * the varbinds are modified on the fly
126  */
127  original_varbinds_length = *varbinds_length;
128  for(i = 0; i < original_varbinds_length; i++) {
129  snmp_oid_copy(oid[i], varbinds[i].oid);
130  }
131 
132  *varbinds_length = 0;
133  for(i = 0; i < original_varbinds_length; i++) {
134  if(i >= header->error_status_non_repeaters.non_repeaters) {
135  break;
136  }
137 
138  resource = snmp_mib_find_next(oid[i]);
139  if(!resource) {
140  switch(header->version) {
141  case SNMP_VERSION_1:
142  header->error_status_non_repeaters.error_status = SNMP_STATUS_NO_SUCH_NAME;
143  /*
144  * Varbinds are 1 indexed
145  */
146  header->error_index_max_repetitions.error_index = i + 1;
147  break;
148  case SNMP_VERSION_2C:
149  (&varbinds[i])->value_type = SNMP_DATA_TYPE_END_OF_MIB_VIEW;
150  break;
151  default:
152  header->error_status_non_repeaters.error_status = SNMP_STATUS_NO_SUCH_NAME;
153  header->error_index_max_repetitions.error_index = 0;
154  }
155  } else {
156  if(*varbinds_length < SNMP_MAX_NR_VALUES) {
157  resource->handler(&varbinds[*varbinds_length], resource->oid);
158  (*varbinds_length)++;
159  }
160  }
161  }
162 
163  for(i = 0; i < header->error_index_max_repetitions.max_repetitions; i++) {
164  repeater = 0;
165  for(j = header->error_status_non_repeaters.non_repeaters; j < original_varbinds_length; j++) {
166  resource = snmp_mib_find_next(oid[j]);
167  if(!resource) {
168  switch(header->version) {
169  case SNMP_VERSION_1:
170  header->error_status_non_repeaters.error_status = SNMP_STATUS_NO_SUCH_NAME;
171  /*
172  * Varbinds are 1 indexed
173  */
174  header->error_index_max_repetitions.error_index = *varbinds_length + 1;
175  break;
176  case SNMP_VERSION_2C:
177  if(*varbinds_length < SNMP_MAX_NR_VALUES) {
178  (&varbinds[*varbinds_length])->value_type = SNMP_DATA_TYPE_END_OF_MIB_VIEW;
179  snmp_oid_copy((&varbinds[*varbinds_length])->oid, oid[j]);
180  (*varbinds_length)++;
181  }
182  break;
183  default:
184  header->error_status_non_repeaters.error_status = SNMP_STATUS_NO_SUCH_NAME;
185  header->error_index_max_repetitions.error_index = 0;
186  }
187  } else {
188  if(*varbinds_length < SNMP_MAX_NR_VALUES) {
189  resource->handler(&varbinds[*varbinds_length], resource->oid);
190  (*varbinds_length)++;
191  snmp_oid_copy(oid[j], resource->oid);
192  repeater++;
193  }
194  }
195  }
196  if(repeater == 0) {
197  break;
198  }
199  }
200 
201  return 0;
202 }
203 /*---------------------------------------------------------------------------*/
204 unsigned char *
205 snmp_engine(unsigned char *buff, uint32_t buff_len, unsigned char *out, uint32_t *out_len)
206 {
207  static snmp_header_t header;
208  static snmp_varbind_t varbinds[SNMP_MAX_NR_VALUES];
209  static uint32_t varbind_length;
210 
211  buff = snmp_message_decode(buff, buff_len, &header, varbinds, &varbind_length);
212  if(buff == NULL) {
213  return NULL;
214  }
215 
216  if(header.version != SNMP_VERSION_1) {
217  if(strncmp(header.community.community, SNMP_COMMUNITY, header.community.length)) {
218  LOG_ERR("Request with invalid community\n");
219  return NULL;
220  }
221  }
222 
223  /*
224  * Now handle the SNMP requests depending on their type
225  */
226  switch(header.pdu_type) {
227  case SNMP_DATA_TYPE_PDU_GET_REQUEST:
228  if(snmp_engine_get(&header, varbinds, varbind_length) == -1) {
229  return NULL;
230  }
231  break;
232 
233  case SNMP_DATA_TYPE_PDU_GET_NEXT_REQUEST:
234  if(snmp_engine_get_next(&header, varbinds, varbind_length) == -1) {
235  return NULL;
236  }
237  break;
238 
239  case SNMP_DATA_TYPE_PDU_GET_BULK:
240  if(snmp_engine_get_bulk(&header, varbinds, &varbind_length) == -1) {
241  return NULL;
242  }
243  break;
244 
245  default:
246  LOG_ERR("Invalid request type");
247  return NULL;
248  }
249 
250  header.pdu_type = SNMP_DATA_TYPE_PDU_GET_RESPONSE;
251  out = snmp_message_encode(out, out_len, &header, varbinds, varbind_length);
252 
253  return ++out;
254 }
#define SNMP_STATUS_NO_SUCH_NAME
SNMP No Such Name error code.
Definition: snmp.h:80
uint32_t version
SNMP Version.
Definition: snmp.h:96
snmp_mib_resource_handler_t handler
The function handler that is called for this resource.
Definition: snmp-mib.h:77
const char * community
A pointer to the community.
Definition: snmp.h:106
void snmp_oid_copy(uint32_t *dst, uint32_t *src)
Copies a Oid.
Definition: snmp-oid.c:185
An implementation of the Simple Network Management Protocol (RFC 3411-3418)
#define SNMP_COMMUNITY
Default SNMP Community.
Definition: snmp-conf.h:62
The varbind struct.
Definition: snmp.h:157
uint32_t error_index
The error index.
Definition: snmp.h:146
snmp_mib_resource_t * snmp_mib_find_next(uint32_t *oid)
Finds the next MIB Resource after this OID.
Definition: snmp-mib.c:68
uint32_t error_status
The error status.
Definition: snmp.h:131
The MIB Resource struct.
Definition: snmp-mib.h:61
uint32_t non_repeaters
The non repeaters.
Definition: snmp.h:135
The SNMP header struct.
Definition: snmp.h:92
#define SNMP_VERSION_1
SNMP Version 1 code.
Definition: snmp.h:71
uint8_t pdu_type
The PDU type.
Definition: snmp.h:117
#define SNMP_MSG_OID_MAX_LEN
Default maximum number of IDs in one OID.
Definition: snmp-conf.h:74
An implementation of the Simple Network Management Protocol (RFC 3411-3418)
uint32_t max_repetitions
The max repetitions.
Definition: snmp.h:150
#define SNMP_VERSION_2C
SNMP Version 2c code.
Definition: snmp.h:75
uint32_t length
The string length.
Definition: snmp.h:112
snmp_mib_resource_t * snmp_mib_find(uint32_t *oid)
Finds the MIB Resource for this OID.
Definition: snmp-mib.c:52
unsigned char * snmp_message_encode(unsigned char *out, uint32_t *out_len, snmp_header_t *header, snmp_varbind_t *varbinds, uint32_t varbind_num)
Encodes a SNMP message.
Definition: snmp-message.c:50
uint32_t * oid
A array that represents the OID.
Definition: snmp-mib.h:73
#define SNMP_MAX_NR_VALUES
Default maximum number of OIDs in one response.
Definition: snmp-conf.h:86
unsigned char * snmp_engine(unsigned char *buff, uint32_t buff_len, unsigned char *out, uint32_t *out_len)
Process the SNMP packet and prepares the response.
Definition: snmp-engine.c:205
An implementation of the Simple Network Management Protocol (RFC 3411-3418)
uint8_t * snmp_message_decode(uint8_t *buf, uint32_t buf_len, snmp_header_t *header, snmp_varbind_t *varbinds, uint32_t *varbind_num)
Definition: snmp-message.c:114
An implementation of the Simple Network Management Protocol (RFC 3411-3418)