sdk-core v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
Loading...
Searching...
No Matches
snmp_core.h
Go to the documentation of this file.
1
6
7/*
8 * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31 * OF SUCH DAMAGE.
32 *
33 * Author: Christiaan Simons <christiaan.simons@axon.tv>
34 * Martin Hentschel <info@cl-soft.de>
35 */
36
37#ifndef LWIP_HDR_APPS_SNMP_CORE_H
38#define LWIP_HDR_APPS_SNMP_CORE_H
39
40#include "lwip/apps/snmp_opts.h"
41
42#if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
43
44#include "lwip/ip_addr.h"
45#include "lwip/err.h"
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
51/* basic ASN1 defines */
52#define SNMP_ASN1_CLASS_UNIVERSAL 0x00
53#define SNMP_ASN1_CLASS_APPLICATION 0x40
54#define SNMP_ASN1_CLASS_CONTEXT 0x80
55#define SNMP_ASN1_CLASS_PRIVATE 0xC0
56
57#define SNMP_ASN1_CONTENTTYPE_PRIMITIVE 0x00
58#define SNMP_ASN1_CONTENTTYPE_CONSTRUCTED 0x20
59
60/* universal tags (from ASN.1 spec.) */
61#define SNMP_ASN1_UNIVERSAL_END_OF_CONTENT 0
62#define SNMP_ASN1_UNIVERSAL_INTEGER 2
63#define SNMP_ASN1_UNIVERSAL_OCTET_STRING 4
64#define SNMP_ASN1_UNIVERSAL_NULL 5
65#define SNMP_ASN1_UNIVERSAL_OBJECT_ID 6
66#define SNMP_ASN1_UNIVERSAL_SEQUENCE_OF 16
67
68/* application specific (SNMP) tags (from SNMPv2-SMI) */
69#define SNMP_ASN1_APPLICATION_IPADDR 0 /* [APPLICATION 0] IMPLICIT OCTET STRING (SIZE (4)) */
70#define SNMP_ASN1_APPLICATION_COUNTER 1 /* [APPLICATION 1] IMPLICIT INTEGER (0..4294967295) => u32_t */
71#define SNMP_ASN1_APPLICATION_GAUGE 2 /* [APPLICATION 2] IMPLICIT INTEGER (0..4294967295) => u32_t */
72#define SNMP_ASN1_APPLICATION_TIMETICKS 3 /* [APPLICATION 3] IMPLICIT INTEGER (0..4294967295) => u32_t */
73#define SNMP_ASN1_APPLICATION_OPAQUE 4 /* [APPLICATION 4] IMPLICIT OCTET STRING */
74#define SNMP_ASN1_APPLICATION_COUNTER64 6 /* [APPLICATION 6] IMPLICIT INTEGER (0..18446744073709551615) */
75
76/* context specific (SNMP) tags (from RFC 1905) */
77#define SNMP_ASN1_CONTEXT_VARBIND_NO_SUCH_INSTANCE 1
78
79/* full ASN1 type defines */
80#define SNMP_ASN1_TYPE_END_OF_CONTENT (SNMP_ASN1_CLASS_UNIVERSAL | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_UNIVERSAL_END_OF_CONTENT)
81#define SNMP_ASN1_TYPE_INTEGER (SNMP_ASN1_CLASS_UNIVERSAL | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_UNIVERSAL_INTEGER)
82#define SNMP_ASN1_TYPE_OCTET_STRING (SNMP_ASN1_CLASS_UNIVERSAL | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_UNIVERSAL_OCTET_STRING)
83#define SNMP_ASN1_TYPE_NULL (SNMP_ASN1_CLASS_UNIVERSAL | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_UNIVERSAL_NULL)
84#define SNMP_ASN1_TYPE_OBJECT_ID (SNMP_ASN1_CLASS_UNIVERSAL | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_UNIVERSAL_OBJECT_ID)
85#define SNMP_ASN1_TYPE_SEQUENCE (SNMP_ASN1_CLASS_UNIVERSAL | SNMP_ASN1_CONTENTTYPE_CONSTRUCTED | SNMP_ASN1_UNIVERSAL_SEQUENCE_OF)
86#define SNMP_ASN1_TYPE_IPADDR (SNMP_ASN1_CLASS_APPLICATION | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_APPLICATION_IPADDR)
87#define SNMP_ASN1_TYPE_IPADDRESS SNMP_ASN1_TYPE_IPADDR
88#define SNMP_ASN1_TYPE_COUNTER (SNMP_ASN1_CLASS_APPLICATION | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_APPLICATION_COUNTER)
89#define SNMP_ASN1_TYPE_COUNTER32 SNMP_ASN1_TYPE_COUNTER
90#define SNMP_ASN1_TYPE_GAUGE (SNMP_ASN1_CLASS_APPLICATION | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_APPLICATION_GAUGE)
91#define SNMP_ASN1_TYPE_GAUGE32 SNMP_ASN1_TYPE_GAUGE
92#define SNMP_ASN1_TYPE_UNSIGNED32 SNMP_ASN1_TYPE_GAUGE
93#define SNMP_ASN1_TYPE_TIMETICKS (SNMP_ASN1_CLASS_APPLICATION | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_APPLICATION_TIMETICKS)
94#define SNMP_ASN1_TYPE_OPAQUE (SNMP_ASN1_CLASS_APPLICATION | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_APPLICATION_OPAQUE)
95#if LWIP_HAVE_INT64
96#define SNMP_ASN1_TYPE_COUNTER64 (SNMP_ASN1_CLASS_APPLICATION | SNMP_ASN1_CONTENTTYPE_PRIMITIVE | SNMP_ASN1_APPLICATION_COUNTER64)
97#endif
98
99#define SNMP_VARBIND_EXCEPTION_OFFSET 0xF0
100#define SNMP_VARBIND_EXCEPTION_MASK 0x0F
101
103typedef enum {
104 SNMP_ERR_NOERROR = 0,
105/*
106outdated v1 error codes. do not use anmore!
107#define SNMP_ERR_NOSUCHNAME 2 use SNMP_ERR_NOSUCHINSTANCE instead
108#define SNMP_ERR_BADVALUE 3 use SNMP_ERR_WRONGTYPE,SNMP_ERR_WRONGLENGTH,SNMP_ERR_WRONGENCODING or SNMP_ERR_WRONGVALUE instead
109#define SNMP_ERR_READONLY 4 use SNMP_ERR_NOTWRITABLE instead
110*/
111 SNMP_ERR_GENERROR = 5,
112 SNMP_ERR_NOACCESS = 6,
113 SNMP_ERR_WRONGTYPE = 7,
114 SNMP_ERR_WRONGLENGTH = 8,
115 SNMP_ERR_WRONGENCODING = 9,
116 SNMP_ERR_WRONGVALUE = 10,
117 SNMP_ERR_NOCREATION = 11,
118 SNMP_ERR_INCONSISTENTVALUE = 12,
119 SNMP_ERR_RESOURCEUNAVAILABLE = 13,
120 SNMP_ERR_COMMITFAILED = 14,
121 SNMP_ERR_UNDOFAILED = 15,
122 SNMP_ERR_NOTWRITABLE = 17,
123 SNMP_ERR_INCONSISTENTNAME = 18,
124
125 SNMP_ERR_NOSUCHINSTANCE = SNMP_VARBIND_EXCEPTION_OFFSET + SNMP_ASN1_CONTEXT_VARBIND_NO_SUCH_INSTANCE
126} snmp_err_t;
127
129struct snmp_obj_id
130{
131 u8_t len;
132 u32_t id[SNMP_MAX_OBJ_ID_LEN];
133};
134
135struct snmp_obj_id_const_ref
136{
137 u8_t len;
138 const u32_t* id;
139};
140
141extern const struct snmp_obj_id_const_ref snmp_zero_dot_zero; /* administrative identifier from SNMPv2-SMI */
142
144union snmp_variant_value
145{
146 void* ptr;
147 const void* const_ptr;
148 u32_t u32;
149 s32_t s32;
150#if LWIP_HAVE_INT64
151 u64_t u64;
152#endif
153};
154
155
162#define SNMP_NODE_TREE 0x00
163/* predefined leaf node types */
164#define SNMP_NODE_SCALAR 0x01
165#define SNMP_NODE_SCALAR_ARRAY 0x02
166#define SNMP_NODE_TABLE 0x03
167#define SNMP_NODE_THREADSYNC 0x04
168
170struct snmp_node
171{
173 u8_t node_type;
175 u32_t oid;
176};
177
179typedef enum {
180 SNMP_NODE_INSTANCE_ACCESS_READ = 1,
181 SNMP_NODE_INSTANCE_ACCESS_WRITE = 2,
182 SNMP_NODE_INSTANCE_READ_ONLY = SNMP_NODE_INSTANCE_ACCESS_READ,
183 SNMP_NODE_INSTANCE_READ_WRITE = (SNMP_NODE_INSTANCE_ACCESS_READ | SNMP_NODE_INSTANCE_ACCESS_WRITE),
184 SNMP_NODE_INSTANCE_WRITE_ONLY = SNMP_NODE_INSTANCE_ACCESS_WRITE,
185 SNMP_NODE_INSTANCE_NOT_ACCESSIBLE = 0
186} snmp_access_t;
187
188struct snmp_node_instance;
189
190typedef s16_t (*node_instance_get_value_method)(struct snmp_node_instance*, void*);
191typedef snmp_err_t (*node_instance_set_test_method)(struct snmp_node_instance*, u16_t, void*);
192typedef snmp_err_t (*node_instance_set_value_method)(struct snmp_node_instance*, u16_t, void*);
193typedef void (*node_instance_release_method)(struct snmp_node_instance*);
194
195#define SNMP_GET_VALUE_RAW_DATA 0x4000 /* do not use 0x8000 because return value of node_instance_get_value_method is signed16 and 0x8000 would be the signed bit */
196
198struct snmp_node_instance
199{
201 const struct snmp_node* node;
203 struct snmp_obj_id instance_oid;
204
206 u8_t asn1_type;
208 snmp_access_t access;
209
211 node_instance_get_value_method get_value;
213 node_instance_set_test_method set_test;
215 node_instance_set_value_method set_value;
217 node_instance_release_method release_instance;
218
220 union snmp_variant_value reference;
222 u32_t reference_len;
223};
224
225
227struct snmp_tree_node
228{
230 struct snmp_node node;
231 u16_t subnode_count;
232 const struct snmp_node* const *subnodes;
233};
234
235#define SNMP_CREATE_TREE_NODE(oid, subnodes) \
236 {{ SNMP_NODE_TREE, (oid) }, \
237 (u16_t)LWIP_ARRAYSIZE(subnodes), (subnodes) }
238
239#define SNMP_CREATE_EMPTY_TREE_NODE(oid) \
240 {{ SNMP_NODE_TREE, (oid) }, \
241 0, NULL }
242
244struct snmp_leaf_node
245{
247 struct snmp_node node;
248 snmp_err_t (*get_instance)(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance* instance);
249 snmp_err_t (*get_next_instance)(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance* instance);
250};
251
253struct snmp_mib
254{
255 const u32_t *base_oid;
256 u8_t base_oid_len;
257 const struct snmp_node *root_node;
258};
259
260#define SNMP_MIB_CREATE(oid_list, root_node) { (oid_list), (u8_t)LWIP_ARRAYSIZE(oid_list), root_node }
261
263struct snmp_oid_range
264{
265 u32_t min;
266 u32_t max;
267};
268
270u8_t snmp_oid_in_range(const u32_t *oid_in, u8_t oid_len, const struct snmp_oid_range *oid_ranges, u8_t oid_ranges_len);
271
272typedef enum {
273 SNMP_NEXT_OID_STATUS_SUCCESS,
274 SNMP_NEXT_OID_STATUS_NO_MATCH,
275 SNMP_NEXT_OID_STATUS_BUF_TO_SMALL
276} snmp_next_oid_status_t;
277
279struct snmp_next_oid_state
280{
281 const u32_t* start_oid;
282 u8_t start_oid_len;
283
284 u32_t* next_oid;
285 u8_t next_oid_len;
286 u8_t next_oid_max_len;
287
288 snmp_next_oid_status_t status;
289 void* reference;
290};
291
292void snmp_next_oid_init(struct snmp_next_oid_state *state,
293 const u32_t *start_oid, u8_t start_oid_len,
294 u32_t *next_oid_buf, u8_t next_oid_max_len);
295u8_t snmp_next_oid_precheck(struct snmp_next_oid_state *state, const u32_t *oid, u8_t oid_len);
296u8_t snmp_next_oid_check(struct snmp_next_oid_state *state, const u32_t *oid, u8_t oid_len, void* reference);
297
298void snmp_oid_assign(struct snmp_obj_id* target, const u32_t *oid, u8_t oid_len);
299void snmp_oid_combine(struct snmp_obj_id* target, const u32_t *oid1, u8_t oid1_len, const u32_t *oid2, u8_t oid2_len);
300void snmp_oid_prefix(struct snmp_obj_id* target, const u32_t *oid, u8_t oid_len);
301void snmp_oid_append(struct snmp_obj_id* target, const u32_t *oid, u8_t oid_len);
302u8_t snmp_oid_equal(const u32_t *oid1, u8_t oid1_len, const u32_t *oid2, u8_t oid2_len);
303s8_t snmp_oid_compare(const u32_t *oid1, u8_t oid1_len, const u32_t *oid2, u8_t oid2_len);
304
305#if LWIP_IPV4
306u8_t snmp_oid_to_ip4(const u32_t *oid, ip4_addr_t *ip);
307void snmp_ip4_to_oid(const ip4_addr_t *ip, u32_t *oid);
308#endif /* LWIP_IPV4 */
309#if LWIP_IPV6
310u8_t snmp_oid_to_ip6(const u32_t *oid, ip6_addr_t *ip);
311void snmp_ip6_to_oid(const ip6_addr_t *ip, u32_t *oid);
312#endif /* LWIP_IPV6 */
313#if LWIP_IPV4 || LWIP_IPV6
314u8_t snmp_ip_to_oid(const ip_addr_t *ip, u32_t *oid);
315u8_t snmp_ip_port_to_oid(const ip_addr_t *ip, u16_t port, u32_t *oid);
316
317u8_t snmp_oid_to_ip(const u32_t *oid, u8_t oid_len, ip_addr_t *ip);
318u8_t snmp_oid_to_ip_port(const u32_t *oid, u8_t oid_len, ip_addr_t *ip, u16_t *port);
319#endif /* LWIP_IPV4 || LWIP_IPV6 */
320
321struct netif;
322u8_t netif_to_num(const struct netif *netif);
323
324snmp_err_t snmp_set_test_ok(struct snmp_node_instance* instance, u16_t value_len, void* value); /* generic function which can be used if test is always successful */
325
326err_t snmp_decode_bits(const u8_t *buf, u32_t buf_len, u32_t *bit_value);
327err_t snmp_decode_truthvalue(const s32_t *asn1_value, u8_t *bool_value);
328u8_t snmp_encode_bits(u8_t *buf, u32_t buf_len, u32_t bit_value, u8_t bit_count);
329u8_t snmp_encode_truthvalue(s32_t *asn1_value, u32_t bool_value);
330
331struct snmp_statistics
332{
333 u32_t inpkts;
334 u32_t outpkts;
335 u32_t inbadversions;
336 u32_t inbadcommunitynames;
337 u32_t inbadcommunityuses;
338 u32_t inasnparseerrs;
339 u32_t intoobigs;
340 u32_t innosuchnames;
341 u32_t inbadvalues;
342 u32_t inreadonlys;
343 u32_t ingenerrs;
344 u32_t intotalreqvars;
345 u32_t intotalsetvars;
346 u32_t ingetrequests;
347 u32_t ingetnexts;
348 u32_t insetrequests;
349 u32_t ingetresponses;
350 u32_t intraps;
351 u32_t outtoobigs;
352 u32_t outnosuchnames;
353 u32_t outbadvalues;
354 u32_t outgenerrs;
355 u32_t outgetrequests;
356 u32_t outgetnexts;
357 u32_t outsetrequests;
358 u32_t outgetresponses;
359 u32_t outtraps;
360#if LWIP_SNMP_V3
361 u32_t unsupportedseclevels;
362 u32_t notintimewindows;
363 u32_t unknownusernames;
364 u32_t unknownengineids;
365 u32_t wrongdigests;
366 u32_t decryptionerrors;
367#endif
368};
369
370extern struct snmp_statistics snmp_stats;
371
372#ifdef __cplusplus
373}
374#endif
375
376#endif /* LWIP_SNMP */
377
378#endif /* LWIP_HDR_APPS_SNMP_CORE_H */
struct ip6_addr ip6_addr_t
Definition tcpip.h:434
#define SNMP_MAX_OBJ_ID_LEN
Definition snmp_opts.h:131
struct ip4_addr ip4_addr_t
Definition ps2ip_rpc.h:41