xref: /freebsd/contrib/bsnmp/snmpd/snmpd.h (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*
2  * Copyright (c) 2001-2003
3  *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4  *	All rights reserved.
5  *
6  * Author: Harti Brandt <harti@freebsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  * $Begemot: bsnmp/snmpd/snmpd.h,v 1.24 2004/08/06 08:47:13 brandt Exp $
30  *
31  * Private SNMPd data and functions.
32  */
33 
34 #ifdef USE_LIBBEGEMOT
35 #include <rpoll.h>
36 #else
37 #include <isc/eventlib.h>
38 #endif
39 
40 #define PATH_SYSCONFIG "/etc:/usr/etc:/usr/local/etc"
41 
42 #ifdef USE_LIBBEGEMOT
43 #define	evTimerID	int
44 #define	evFileID	int
45 #endif
46 
47 /*************************************************************
48  *
49  * Communities
50  */
51 struct community {
52 	struct lmodule *owner;	/* who created the community */
53 	u_int		private;/* private name for the module */
54 	u_int		value;	/* value of this community */
55 	u_char *	string;	/* the community string */
56 	const u_char *	descr;	/* description */
57 	TAILQ_ENTRY(community) link;
58 
59 	struct asn_oid	index;
60 };
61 /* list of all known communities */
62 extern TAILQ_HEAD(community_list, community) community_list;
63 
64 /*************************************************************
65  *
66  * Request IDs.
67  */
68 struct idrange {
69 	u_int		type;	/* type id */
70 	int32_t		base;	/* base of this range */
71 	int32_t		size;	/* size of this range */
72 	int32_t		next;	/* generator */
73 	struct lmodule *owner;	/* owner module */
74 	TAILQ_ENTRY(idrange) link;
75 };
76 
77 /* list of all known ranges */
78 extern TAILQ_HEAD(idrange_list, idrange) idrange_list;
79 
80 /* identifier generator */
81 extern u_int next_idrange;
82 
83 /* request id generator for traps */
84 extern u_int trap_reqid;
85 
86 /*************************************************************
87  *
88  * Timers
89  */
90 struct timer {
91 	void	(*func)(void *);/* user function */
92 	void	*udata;		/* user data */
93 	evTimerID id;		/* timer id */
94 	struct lmodule *owner;	/* owner of the timer */
95 	LIST_ENTRY(timer) link;
96 };
97 
98 /* list of all current timers */
99 extern LIST_HEAD(timer_list, timer) timer_list;
100 
101 
102 /*************************************************************
103  *
104  * File descriptors
105  */
106 struct fdesc {
107 	int	fd;		/* the file descriptor */
108 	void	(*func)(int, void *);/* user function */
109 	void	*udata;		/* user data */
110 	evFileID id;		/* file id */
111 	struct lmodule *owner;	/* owner module of the file */
112 	LIST_ENTRY(fdesc) link;
113 };
114 
115 /* list of all current selected files */
116 extern LIST_HEAD(fdesc_list, fdesc) fdesc_list;
117 
118 /*************************************************************
119  *
120  * Loadable modules
121  */
122 # define LM_SECTION_MAX	14
123 struct lmodule {
124 	char		section[LM_SECTION_MAX + 1]; /* and index */
125 	char		*path;
126 	u_int		flags;
127 	void		*handle;
128 	const struct snmp_module *config;
129 
130 	TAILQ_ENTRY(lmodule) link;
131 	TAILQ_ENTRY(lmodule) start;
132 
133 	struct asn_oid	index;
134 };
135 #define LM_STARTED	0x0001
136 #define LM_ONSTARTLIST	0x0002
137 
138 extern TAILQ_HEAD(lmodules, lmodule) lmodules;
139 
140 struct lmodule *lm_load(const char *, const char *);
141 void lm_unload(struct lmodule *);
142 void lm_start(struct lmodule *);
143 
144 /*************************************************************
145  *
146  * SNMP ports
147  */
148 /*
149  * Common input stuff
150  */
151 struct port_input {
152 	int		fd;		/* socket */
153 	void		*id;		/* evSelect handle */
154 
155 	u_int		stream : 1;	/* stream socket */
156 	u_int		cred : 1;	/* want credentials */
157 
158 	struct sockaddr	*peer;		/* last received packet */
159 	socklen_t	peerlen;
160 	u_int		priv : 1;	/* peer is privileged */
161 
162 	u_char		*buf;		/* receive buffer */
163 	size_t		buflen;		/* buffer length */
164 	size_t		length;		/* received length */
165 	size_t		consumed;	/* how many bytes used */
166 };
167 
168 struct tport {
169 	struct asn_oid	index;		/* table index of this tp point */
170 	TAILQ_ENTRY(tport) link;	/* table link */
171 	struct transport *transport;	/* who handles this */
172 };
173 TAILQ_HEAD(tport_list, tport);
174 
175 int snmpd_input(struct port_input *, struct tport *);
176 void snmpd_input_close(struct port_input *);
177 void snmpd_input_init(struct port_input *);
178 
179 /*
180  * Transport domain
181  */
182 #define TRANS_NAMELEN	64
183 
184 struct transport_def {
185 	const char	*name;		/* name of this transport */
186 	struct asn_oid	id;		/* OBJID of this transport */
187 
188 	int		(*start)(void);
189 	int		(*stop)(int);
190 
191 	void		(*close_port)(struct tport *);
192 	int		(*init_port)(struct tport *);
193 
194 	ssize_t		(*send)(struct tport *, const u_char *, size_t,
195 			    const struct sockaddr *, size_t);
196 	ssize_t         (*recv)(struct tport *, struct port_input *);
197 
198 	/** send via a multi-socket port */
199 	ssize_t		(*send2)(struct tport *, const u_char *, size_t,
200 			    struct port_input *);
201 };
202 struct transport {
203 	struct asn_oid	index;		/* transport table index */
204 	TAILQ_ENTRY(transport) link;	/* ... and link */
205 	u_int		or_index;	/* registration index */
206 
207 	struct tport_list table;	/* list of open ports */
208 
209 	const struct transport_def *vtab;
210 };
211 
212 TAILQ_HEAD(transport_list, transport);
213 extern struct transport_list transport_list;
214 
215 void trans_insert_port(struct transport *, struct tport *);
216 void trans_remove_port(struct tport *);
217 struct tport *trans_find_port(struct transport *,
218     const struct asn_oid *, u_int);
219 struct tport *trans_next_port(struct transport *,
220     const struct asn_oid *, u_int);
221 struct tport *trans_first_port(struct transport *);
222 struct tport *trans_iter_port(struct transport *,
223     int (*)(struct tport *, intptr_t), intptr_t);
224 
225 int trans_register(const struct transport_def *, struct transport **);
226 int trans_unregister(struct transport *);
227 
228 /*************************************************************
229  *
230  * SNMPd scalar configuration.
231  */
232 struct snmpd {
233 	/* transmit buffer size */
234 	u_int32_t	txbuf;
235 
236 	/* receive buffer size */
237 	u_int32_t	rxbuf;
238 
239 	/* disable community table */
240 	int		comm_dis;
241 
242 	/* authentication traps */
243 	int		auth_traps;
244 
245 	/* source address for V1 traps */
246 	u_char		trap1addr[4];
247 
248 	/* version enable flags */
249 	uint32_t	version_enable;
250 };
251 extern struct snmpd snmpd;
252 
253 #define	VERS_ENABLE_V1	0x00000001
254 #define	VERS_ENABLE_V2C	0x00000002
255 #define	VERS_ENABLE_V3	0x00000004
256 #define	VERS_ENABLE_ALL	(VERS_ENABLE_V1 | VERS_ENABLE_V2C | VERS_ENABLE_V3)
257 
258 /*
259  * The debug group
260  */
261 struct debug {
262 	u_int		dump_pdus;
263 	u_int		logpri;
264 	u_int		evdebug;
265 };
266 extern struct debug debug;
267 
268 
269 /*
270  * SNMPd statistics table
271  */
272 struct snmpd_stats {
273 	u_int32_t	inPkts;		/* total packets received */
274 	u_int32_t	inBadVersions;	/* unknown version number */
275 	u_int32_t	inASNParseErrs;	/* fatal parse errors */
276 	u_int32_t	inBadCommunityNames;
277 	u_int32_t	inBadCommunityUses;
278 	u_int32_t	proxyDrops;	/* dropped by proxy function */
279 	u_int32_t	silentDrops;
280 
281 	u_int32_t	inBadPduTypes;
282 	u_int32_t	inTooLong;
283 	u_int32_t	noTxbuf;
284 	u_int32_t	noRxbuf;
285 };
286 extern struct snmpd_stats snmpd_stats;
287 
288 /*
289  * SNMPd Engine
290  */
291 extern struct snmp_engine snmpd_engine;
292 
293 /*
294  * OR Table
295  */
296 struct objres {
297 	TAILQ_ENTRY(objres) link;
298 	u_int		index;
299 	struct asn_oid	oid;	/* the resource OID */
300 	char		descr[256];
301 	u_int32_t	uptime;
302 	struct lmodule	*module;
303 };
304 TAILQ_HEAD(objres_list, objres);
305 extern struct objres_list objres_list;
306 
307 /*
308  * Trap Sink Table
309  */
310 struct trapsink {
311 	TAILQ_ENTRY(trapsink) link;
312 	struct asn_oid	index;
313 	u_int		status;
314 	int		socket;
315 	u_char		comm[SNMP_COMMUNITY_MAXLEN + 1];
316 	int		version;
317 };
318 enum {
319 	TRAPSINK_ACTIVE		= 1,
320 	TRAPSINK_NOT_IN_SERVICE	= 2,
321 	TRAPSINK_NOT_READY	= 3,
322 	TRAPSINK_DESTROY	= 6,
323 
324 	TRAPSINK_V1		= 1,
325 	TRAPSINK_V2		= 2,
326 };
327 TAILQ_HEAD(trapsink_list, trapsink);
328 extern struct trapsink_list trapsink_list;
329 
330 extern const char *syspath;
331 
332 /* snmpSerialNo */
333 extern int32_t snmp_serial_no;
334 
335 int init_actvals(void);
336 
337 extern char engine_file[];
338 int init_snmpd_engine(void);
339 int set_snmpd_engine(void);
340 void update_snmpd_engine_time(void);
341 
342 int read_config(const char *, struct lmodule *);
343 int define_macro(const char *name, const char *value);
344 
345 #define	LOG_ASN1_ERRORS	0x10000000
346 #define	LOG_SNMP_ERRORS	0x20000000
347