xref: /freebsd/contrib/bsnmp/snmpd/snmpd.h (revision 792bbaba989533a1fc93823df1720c8c4aaf0442)
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 	int		stream : 1;	/* stream socket */
156 	int		cred : 1;	/* want credentials */
157 
158 	struct sockaddr	*peer;		/* last received packet */
159 	socklen_t	peerlen;
160 	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 
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 struct transport {
199 	struct asn_oid	index;		/* transport table index */
200 	TAILQ_ENTRY(transport) link;	/* ... and link */
201 	u_int		or_index;	/* registration index */
202 
203 	struct tport_list table;	/* list of open ports */
204 
205 	const struct transport_def *vtab;
206 };
207 
208 TAILQ_HEAD(transport_list, transport);
209 extern struct transport_list transport_list;
210 
211 void trans_insert_port(struct transport *, struct tport *);
212 void trans_remove_port(struct tport *);
213 struct tport *trans_find_port(struct transport *,
214     const struct asn_oid *, u_int);
215 struct tport *trans_next_port(struct transport *,
216     const struct asn_oid *, u_int);
217 struct tport *trans_first_port(struct transport *);
218 struct tport *trans_iter_port(struct transport *,
219     int (*)(struct tport *, intptr_t), intptr_t);
220 
221 int trans_register(const struct transport_def *, struct transport **);
222 int trans_unregister(struct transport *);
223 
224 /*************************************************************
225  *
226  * SNMPd scalar configuration.
227  */
228 struct snmpd {
229 	/* transmit buffer size */
230 	u_int32_t	txbuf;
231 
232 	/* receive buffer size */
233 	u_int32_t	rxbuf;
234 
235 	/* disable community table */
236 	int		comm_dis;
237 
238 	/* authentication traps */
239 	int		auth_traps;
240 
241 	/* source address for V1 traps */
242 	u_char		trap1addr[4];
243 
244 	/* version enable flags */
245 	uint32_t	version_enable;
246 };
247 extern struct snmpd snmpd;
248 
249 #define	VERS_ENABLE_V1	0x00000001
250 #define	VERS_ENABLE_V2C	0x00000002
251 #define	VERS_ENABLE_V3	0x00000004
252 #define	VERS_ENABLE_ALL	(VERS_ENABLE_V1 | VERS_ENABLE_V2C | VERS_ENABLE_V3)
253 
254 /*
255  * The debug group
256  */
257 struct debug {
258 	u_int		dump_pdus;
259 	u_int		logpri;
260 	u_int		evdebug;
261 };
262 extern struct debug debug;
263 
264 
265 /*
266  * SNMPd statistics table
267  */
268 struct snmpd_stats {
269 	u_int32_t	inPkts;		/* total packets received */
270 	u_int32_t	inBadVersions;	/* unknown version number */
271 	u_int32_t	inASNParseErrs;	/* fatal parse errors */
272 	u_int32_t	inBadCommunityNames;
273 	u_int32_t	inBadCommunityUses;
274 	u_int32_t	proxyDrops;	/* dropped by proxy function */
275 	u_int32_t	silentDrops;
276 
277 	u_int32_t	inBadPduTypes;
278 	u_int32_t	inTooLong;
279 	u_int32_t	noTxbuf;
280 	u_int32_t	noRxbuf;
281 };
282 extern struct snmpd_stats snmpd_stats;
283 
284 /*
285  * SNMPd Engine
286  */
287 extern struct snmp_engine snmpd_engine;
288 
289 /*
290  * OR Table
291  */
292 struct objres {
293 	TAILQ_ENTRY(objres) link;
294 	u_int		index;
295 	struct asn_oid	oid;	/* the resource OID */
296 	char		descr[256];
297 	u_int32_t	uptime;
298 	struct lmodule	*module;
299 };
300 TAILQ_HEAD(objres_list, objres);
301 extern struct objres_list objres_list;
302 
303 /*
304  * Trap Sink Table
305  */
306 struct trapsink {
307 	TAILQ_ENTRY(trapsink) link;
308 	struct asn_oid	index;
309 	u_int		status;
310 	int		socket;
311 	u_char		comm[SNMP_COMMUNITY_MAXLEN + 1];
312 	int		version;
313 };
314 enum {
315 	TRAPSINK_ACTIVE		= 1,
316 	TRAPSINK_NOT_IN_SERVICE	= 2,
317 	TRAPSINK_NOT_READY	= 3,
318 	TRAPSINK_DESTROY	= 6,
319 
320 	TRAPSINK_V1		= 1,
321 	TRAPSINK_V2		= 2,
322 };
323 TAILQ_HEAD(trapsink_list, trapsink);
324 extern struct trapsink_list trapsink_list;
325 
326 extern const char *syspath;
327 
328 /* snmpSerialNo */
329 extern int32_t snmp_serial_no;
330 
331 int init_actvals(void);
332 
333 extern char engine_file[];
334 int init_snmpd_engine(void);
335 int set_snmpd_engine(void);
336 void update_snmpd_engine_time(void);
337 
338 int read_config(const char *, struct lmodule *);
339 int define_macro(const char *name, const char *value);
340 
341 #define	LOG_ASN1_ERRORS	0x10000000
342 #define	LOG_SNMP_ERRORS	0x20000000
343