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