2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Author: Harti Brandt <harti@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Begemot: bsnmp/snmpd/snmpmod.h,v 1.32 2006/02/14 09:04:20 brandt_h Exp $
31 * SNMP daemon data and functions exported to modules.
36 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <netinet/in.h>
42 #include "snmpagent.h"
44 #define MAX_MOD_ARGS 16
47 * These macros help to handle object lists for SNMP tables. They use
48 * tail queues to hold the objects in ascending order in the list.
49 * ordering can be done either on an integer/unsigned field, an asn_oid
50 * or an ordering function.
52 #define INSERT_OBJECT_OID_LINK_INDEX(PTR, LIST, LINK, INDEX) do { \
53 __typeof (PTR) _lelem; \
55 TAILQ_FOREACH(_lelem, (LIST), LINK) \
56 if (asn_compare_oid(&_lelem->INDEX, &(PTR)->INDEX) > 0) \
59 TAILQ_INSERT_TAIL((LIST), (PTR), LINK); \
61 TAILQ_INSERT_BEFORE(_lelem, (PTR), LINK); \
64 #define INSERT_OBJECT_INT_LINK_INDEX(PTR, LIST, LINK, INDEX) do { \
65 __typeof (PTR) _lelem; \
67 TAILQ_FOREACH(_lelem, (LIST), LINK) \
68 if ((asn_subid_t)_lelem->INDEX > (asn_subid_t)(PTR)->INDEX)\
71 TAILQ_INSERT_TAIL((LIST), (PTR), LINK); \
73 TAILQ_INSERT_BEFORE(_lelem, (PTR), LINK); \
76 #define INSERT_OBJECT_FUNC_LINK(PTR, LIST, LINK, FUNC) do { \
77 __typeof (PTR) _lelem; \
79 TAILQ_FOREACH(_lelem, (LIST), LINK) \
80 if ((FUNC)(_lelem, (PTR)) > 0) \
83 TAILQ_INSERT_TAIL((LIST), (PTR), LINK); \
85 TAILQ_INSERT_BEFORE(_lelem, (PTR), LINK); \
88 #define INSERT_OBJECT_FUNC_LINK_REV(PTR, LIST, HEAD, LINK, FUNC) do { \
89 __typeof (PTR) _lelem; \
91 TAILQ_FOREACH_REVERSE(_lelem, (LIST), HEAD, LINK) \
92 if ((FUNC)(_lelem, (PTR)) < 0) \
95 TAILQ_INSERT_HEAD((LIST), (PTR), LINK); \
97 TAILQ_INSERT_AFTER((LIST), _lelem, (PTR), LINK); \
100 #define FIND_OBJECT_OID_LINK_INDEX(LIST, OID, SUB, LINK, INDEX) ({ \
101 __typeof (TAILQ_FIRST(LIST)) _lelem; \
103 TAILQ_FOREACH(_lelem, (LIST), LINK) \
104 if (index_compare(OID, SUB, &_lelem->INDEX) == 0) \
109 #define NEXT_OBJECT_OID_LINK_INDEX(LIST, OID, SUB, LINK, INDEX) ({ \
110 __typeof (TAILQ_FIRST(LIST)) _lelem; \
112 TAILQ_FOREACH(_lelem, (LIST), LINK) \
113 if (index_compare(OID, SUB, &_lelem->INDEX) < 0) \
118 #define FIND_OBJECT_INT_LINK_INDEX(LIST, OID, SUB, LINK, INDEX) ({ \
119 __typeof (TAILQ_FIRST(LIST)) _lelem; \
121 if ((OID)->len - SUB != 1) \
124 TAILQ_FOREACH(_lelem, (LIST), LINK) \
125 if ((OID)->subs[SUB] == (asn_subid_t)_lelem->INDEX)\
130 #define NEXT_OBJECT_INT_LINK_INDEX(LIST, OID, SUB, LINK, INDEX) ({ \
131 __typeof (TAILQ_FIRST(LIST)) _lelem; \
133 if ((OID)->len - SUB == 0) \
134 _lelem = TAILQ_FIRST(LIST); \
136 TAILQ_FOREACH(_lelem, (LIST), LINK) \
137 if ((OID)->subs[SUB] < (asn_subid_t)_lelem->INDEX)\
142 #define FIND_OBJECT_FUNC_LINK(LIST, OID, SUB, LINK, FUNC) ({ \
143 __typeof (TAILQ_FIRST(LIST)) _lelem; \
145 TAILQ_FOREACH(_lelem, (LIST), LINK) \
146 if ((FUNC)(OID, SUB, _lelem) == 0) \
151 #define NEXT_OBJECT_FUNC_LINK(LIST, OID, SUB, LINK, FUNC) ({ \
152 __typeof (TAILQ_FIRST(LIST)) _lelem; \
154 TAILQ_FOREACH(_lelem, (LIST), LINK) \
155 if ((FUNC)(OID, SUB, _lelem) < 0) \
161 * Macros for the case where the index field is called 'index'
163 #define INSERT_OBJECT_OID_LINK(PTR, LIST, LINK) \
164 INSERT_OBJECT_OID_LINK_INDEX(PTR, LIST, LINK, index)
166 #define INSERT_OBJECT_INT_LINK(PTR, LIST, LINK) do { \
167 INSERT_OBJECT_INT_LINK_INDEX(PTR, LIST, LINK, index)
169 #define FIND_OBJECT_OID_LINK(LIST, OID, SUB, LINK) \
170 FIND_OBJECT_OID_LINK_INDEX(LIST, OID, SUB, LINK, index)
172 #define NEXT_OBJECT_OID_LINK(LIST, OID, SUB, LINK) \
173 NEXT_OBJECT_OID_LINK_INDEX(LIST, OID, SUB, LINK, index)
175 #define FIND_OBJECT_INT_LINK(LIST, OID, SUB, LINK) \
176 FIND_OBJECT_INT_LINK_INDEX(LIST, OID, SUB, LINK, index)
178 #define NEXT_OBJECT_INT_LINK(LIST, OID, SUB, LINK) \
179 NEXT_OBJECT_INT_LINK_INDEX(LIST, OID, SUB, LINK, index)
182 * Macros for the case where the index field is called 'index' and the
185 #define INSERT_OBJECT_OID(PTR, LIST) \
186 INSERT_OBJECT_OID_LINK_INDEX(PTR, LIST, link, index)
188 #define INSERT_OBJECT_INT(PTR, LIST) \
189 INSERT_OBJECT_INT_LINK_INDEX(PTR, LIST, link, index)
191 #define INSERT_OBJECT_FUNC_REV(PTR, LIST, HEAD, FUNC) \
192 INSERT_OBJECT_FUNC_LINK_REV(PTR, LIST, HEAD, link, FUNC)
194 #define FIND_OBJECT_OID(LIST, OID, SUB) \
195 FIND_OBJECT_OID_LINK_INDEX(LIST, OID, SUB, link, index)
197 #define FIND_OBJECT_INT(LIST, OID, SUB) \
198 FIND_OBJECT_INT_LINK_INDEX(LIST, OID, SUB, link, index)
200 #define FIND_OBJECT_FUNC(LIST, OID, SUB, FUNC) \
201 FIND_OBJECT_FUNC_LINK(LIST, OID, SUB, link, FUNC)
203 #define NEXT_OBJECT_OID(LIST, OID, SUB) \
204 NEXT_OBJECT_OID_LINK_INDEX(LIST, OID, SUB, link, index)
206 #define NEXT_OBJECT_INT(LIST, OID, SUB) \
207 NEXT_OBJECT_INT_LINK_INDEX(LIST, OID, SUB, link, index)
209 #define NEXT_OBJECT_FUNC(LIST, OID, SUB, FUNC) \
210 NEXT_OBJECT_FUNC_LINK(LIST, OID, SUB, link, FUNC)
214 /* The tick when the program was started. This is the absolute time of
215 * the start in 100th of a second. */
216 extern uint64_t start_tick;
218 /* The tick when the current packet was received. This is the absolute
219 * time in 100th of second. */
220 extern uint64_t this_tick;
222 /* Get the current absolute time in 100th of a second. */
223 uint64_t get_ticks(void);
226 * Return code for proxy function
228 enum snmpd_proxy_err {
229 /* proxy code will process the PDU */
231 /* proxy code does not process PDU */
235 /* drop because of bad community */
237 /* drop because of bad community use */
238 SNMPD_PROXY_BADCOMMUSE
244 enum snmpd_input_err {
245 /* proceed with packet */
247 /* fatal error in packet, ignore it */
249 /* value encoding has wrong length in a SET operation */
250 SNMPD_INPUT_VALBADLEN,
251 /* value encoding is out of range */
252 SNMPD_INPUT_VALRANGE,
253 /* value has bad encoding */
254 SNMPD_INPUT_VALBADENC,
255 /* need more data (truncated packet) */
257 /* unknown community */
258 SNMPD_INPUT_BAD_COMM,
262 * Every loadable module must have one of this structures with
263 * the external name 'config'.
266 /* a comment describing what this module implements */
269 /* the initialization function */
270 int (*init)(struct lmodule *, int argc, char *argv[]);
272 /* the finalisation function */
275 /* the idle function */
278 /* the dump function */
281 /* re-configuration function */
282 void (*config)(void);
284 /* start operation */
288 enum snmpd_proxy_err (*proxy)(struct snmp_pdu *, void *,
289 const struct asn_oid *, const struct sockaddr *, socklen_t,
290 enum snmpd_input_err, int32_t, int);
292 /* the tree this module is going to server */
293 const struct snmp_node *tree;
296 /* function called, when another module was unloaded/loaded */
297 void (*loading)(const struct lmodule *, int);
301 * Stuff exported to modules
309 struct asn_oid object_id;
314 u_int32_t or_last_change;
316 extern struct systemg systemg;
321 * We have 2 fixed communities for SNMP read and write access. Modules
322 * can create their communities dynamically. They are deleted automatically
323 * if the module is unloaded.
325 #define COMM_INITIALIZE 0
329 u_int comm_define(u_int, const char *descr, struct lmodule *, const char *str);
330 const char * comm_string(u_int);
332 /* community for current packet */
333 extern u_int community;
338 extern const struct asn_oid oid_zeroDotZero;
343 * A module can request a range of request ids and associate them with a
344 * type field. All ranges are deleted if a module is unloaded.
346 u_int reqid_allocate(int size, struct lmodule *);
347 int32_t reqid_next(u_int type);
348 int32_t reqid_base(u_int type);
349 int reqid_istype(int32_t reqid, u_int type);
350 u_int reqid_type(int32_t reqid);
355 void *timer_start(u_int, void (*)(void *), void *, struct lmodule *);
356 void *timer_start_repeat(u_int, u_int, void (*)(void *), void *,
358 void timer_stop(void *);
363 void *fd_select(int, void (*)(int, void *), void *, struct lmodule *);
364 void fd_deselect(void *);
365 void fd_suspend(void *);
366 int fd_resume(void *);
371 u_int or_register(const struct asn_oid *, const char *, struct lmodule *);
372 void or_unregister(u_int);
377 void *buf_alloc(int tx);
378 size_t buf_size(int tx);
380 /* decode PDU and find community */
381 enum snmpd_input_err snmp_input_start(const u_char *, size_t, const char *,
382 struct snmp_pdu *, int32_t *, size_t *);
384 /* process the pdu. returns either _OK or _FAILED */
385 enum snmpd_input_err snmp_input_finish(struct snmp_pdu *, const u_char *,
386 size_t, u_char *, size_t *, const char *, enum snmpd_input_err, int32_t,
389 void snmp_output(struct snmp_pdu *, u_char *, size_t *, const char *);
390 void snmp_send_port(void *, const struct asn_oid *, struct snmp_pdu *,
391 const struct sockaddr *, socklen_t);
394 void snmp_send_trap(const struct asn_oid *, ...);
399 int string_save(struct snmp_value *, struct snmp_context *, ssize_t, u_char **);
400 void string_commit(struct snmp_context *);
401 void string_rollback(struct snmp_context *, u_char **);
402 int string_get(struct snmp_value *, const u_char *, ssize_t);
403 int string_get_max(struct snmp_value *, const u_char *, ssize_t, size_t);
404 void string_free(struct snmp_context *);
406 int ip_save(struct snmp_value *, struct snmp_context *, u_char *);
407 void ip_rollback(struct snmp_context *, u_char *);
408 void ip_commit(struct snmp_context *);
409 int ip_get(struct snmp_value *, u_char *);
411 int oid_save(struct snmp_value *, struct snmp_context *, struct asn_oid *);
412 void oid_rollback(struct snmp_context *, struct asn_oid *);
413 void oid_commit(struct snmp_context *);
414 int oid_get(struct snmp_value *, const struct asn_oid *);
416 int index_decode(const struct asn_oid *oid, u_int sub, u_int code, ...);
417 int index_compare(const struct asn_oid *, u_int, const struct asn_oid *);
418 int index_compare_off(const struct asn_oid *, u_int, const struct asn_oid *,
420 void index_append(struct asn_oid *, u_int, const struct asn_oid *);
421 void index_append_off(struct asn_oid *, u_int, const struct asn_oid *, u_int);