summaryrefslogtreecommitdiff
path: root/usr.sbin/snmpd/snmp.h
blob: 7b513de8b1ad30b7b1943fc8be739cda972db7b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*	$OpenBSD: snmp.h,v 1.14 2015/06/11 18:49:09 reyk Exp $	*/

/*
 * Copyright (c) 2007, 2008, 2012 Reyk Floeter <reyk@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef SNMPD_SNMP_H
#define SNMPD_SNMP_H

#include <sys/types.h>
#include <endian.h>

/*
 * SNMP IMSG interface
 */

#define SNMP_MAX_OID_STRLEN	128	/* max size of the OID _string_ */
#define SNMP_SOCKET		"/var/run/snmpd.sock"
#define AGENTX_SOCKET		"/var/run/agentx.sock"
#define SNMP_RESTRICTED_SOCKET	"/var/run/snmpd.rsock"

enum snmp_type {
	SNMP_IPADDR		= 0,
	SNMP_COUNTER32		= 1,
	SNMP_GAUGE32		= 2,
	SNMP_UNSIGNED32		= 2,
	SNMP_TIMETICKS		= 3,
	SNMP_OPAQUE		= 4,
	SNMP_NSAPADDR		= 5,
	SNMP_COUNTER64		= 6,
	SNMP_UINTEGER32		= 7,

	SNMP_INTEGER32		= 100,
	SNMP_BITSTRING		= 101,
	SNMP_OCTETSTRING	= 102,
	SNMP_NULL		= 103,
	SNMP_OBJECT		= 104
};

enum snmp_imsg_ctl {
	IMSG_SNMP_DUMMY		= 1000,	/* something that works everywhere */
	IMSG_SNMP_ELEMENT,
	IMSG_SNMP_END,
	IMSG_SNMP_LOCK,			/* enable restricted mode */
	IMSG_SNMP_AGENTX
};

struct snmp_imsg_hdr {
	u_int32_t	 imsg_type;
	u_int16_t	 imsg_len;
	u_int16_t	 imsg_flags;
	u_int32_t	 imsg_peerid;
	u_int32_t	 imsg_pid;
};

struct snmp_imsg {
	char		 snmp_oid[SNMP_MAX_OID_STRLEN];
	u_int8_t	 snmp_type;
	u_int16_t	 snmp_len;
};

/*
 * SNMP BER types
 */

enum snmp_version {
	SNMP_V1			= 0,
	SNMP_V2			= 1,	/* SNMPv2c */
	SNMP_V3			= 3
};

enum snmp_context {
	SNMP_C_GETREQ		= 0,
	SNMP_C_GETNEXTREQ	= 1,
	SNMP_C_GETRESP		= 2,
	SNMP_C_SETREQ		= 3,
	SNMP_C_TRAP		= 4,

	/* SNMPv2 */
	SNMP_C_GETBULKREQ	= 5,
	SNMP_C_INFORMREQ	= 6,
	SNMP_C_TRAPV2		= 7,
	SNMP_C_REPORT		= 8
};

enum snmp_application {
	SNMP_T_IPADDR		= 0,
	SNMP_T_COUNTER32	= 1,
	SNMP_T_GAUGE32		= 2,
	SNMP_T_UNSIGNED32	= 2,
	SNMP_T_TIMETICKS	= 3,
	SNMP_T_OPAQUE		= 4,
	SNMP_T_NSAPADDR		= 5,
	SNMP_T_COUNTER64	= 6,
	SNMP_T_UINTEGER32	= 7
};

enum snmp_generic_trap {
	SNMP_TRAP_COLDSTART	= 0,
	SNMP_TRAP_WARMSTART	= 1,
	SNMP_TRAP_LINKDOWN	= 2,
	SNMP_TRAP_LINKUP	= 3,
	SNMP_TRAP_AUTHFAILURE	= 4,
	SNMP_TRAP_EGPNEIGHLOSS	= 5,
	SNMP_TRAP_ENTERPRISE	= 6
};

enum snmp_error {
	SNMP_ERROR_NONE		= 0,
	SNMP_ERROR_TOOBIG	= 1,
	SNMP_ERROR_NOSUCHNAME	= 2,
	SNMP_ERROR_BADVALUE	= 3,
	SNMP_ERROR_READONLY	= 4,
	SNMP_ERROR_GENERR	= 5,

	/* SNMPv2 */
	SNMP_ERROR_NOACCESS	= 6,
	SNMP_ERROR_WRONGTYPE	= 7,
	SNMP_ERROR_WRONGLENGTH	= 8,
	SNMP_ERROR_WRONGENC	= 9,
	SNMP_ERROR_WRONGVALUE	= 10,
	SNMP_ERROR_NOCREATION	= 11,
	SNMP_ERROR_INCONVALUE	= 12,
	SNMP_ERROR_RESUNAVAIL	= 13, /* EGAIN */
	SNMP_ERROR_COMMITFAILED	= 14,
	SNMP_ERROR_UNDOFAILED	= 15,
	SNMP_ERROR_AUTHERROR	= 16,
	SNMP_ERROR_NOTWRITABLE	= 17,
	SNMP_ERROR_INCONNAME	= 18
};

enum snmp_security_model {
	SNMP_SEC_ANY = 0,
	SNMP_SEC_SNMPv1 = 1,
	SNMP_SEC_SNMPv2c = 2,
	SNMP_SEC_USM = 3,
	SNMP_SEC_TSM = 4
};

#define SNMP_MSGFLAG_AUTH	0x01
#define SNMP_MSGFLAG_PRIV	0x02
#define SNMP_MSGFLAG_SECMASK	(SNMP_MSGFLAG_AUTH | SNMP_MSGFLAG_PRIV)
#define SNMP_MSGFLAG_REPORT	0x04

#define SNMP_MAX_TIMEWINDOW	150	/* RFC3414 */

#define SNMP_MIN_OID_LEN	2	/* OBJECT */
#define SNMP_MAX_OID_LEN	32	/* OBJECT */

struct snmp_oid {
	u_int32_t	o_id[SNMP_MAX_OID_LEN + 1];
	size_t		o_n;
};

/* AgentX protocol, as outlined in RFC 2741 */

/* version */
#define AGENTX_VERSION			1

/* type */
#define	AGENTX_OPEN			1
#define	AGENTX_CLOSE			2
#define	AGENTX_REGISTER			3
#define	AGENTX_UNREGISTER		4
#define	AGENTX_GET			5
#define	AGENTX_GET_NEXT			6
#define	AGENTX_GET_BULK			7
#define	AGENTX_TEST_SET			8
#define	AGENTX_COMMIT_SET		9
#define	AGENTX_UNDO_SET			10
#define	AGENTX_CLEANUP_SET		11
#define	AGENTX_NOTIFY			12
#define	AGENTX_PING			13
#define	AGENTX_INDEX_ALLOCATE		14
#define	AGENTX_INDEX_DEALLOCATE		15
#define	AGENTX_ADD_AGENT_CAPS		16
#define	AGENTX_REMOVE_AGENT_CAPS	17
#define	AGENTX_RESPONSE			18

/* error return codes */
#define	AGENTX_ERR_NONE				0
#define	AGENTX_ERR_OPEN_FAILED			256
#define	AGENTX_ERR_NOT_OPEN			257
#define	AGENTX_ERR_INDEX_WRONG_TYPE		258
#define	AGENTX_ERR_INDEX_ALREADY_ALLOCATED	259
#define	AGENTX_ERR_INDEX_NONE_AVAILABLE		260
#define	AGENTX_ERR_INDEX_NOT_ALLOCATED		261
#define	AGENTX_ERR_UNSUPPORTED_CONTEXT		262
#define	AGENTX_ERR_DUPLICATE_REGISTRATION	263
#define	AGENTX_ERR_UNKNOWN_REGISTRATION		264
#define	AGENTX_ERR_UNKNOWN_AGENT_CAPS		265
#define	AGENTX_ERR_PARSE_ERROR			266
#define	AGENTX_ERR_REQUEST_DENIED		267
#define	AGENTX_ERR_PROCESSING_ERROR		268

/* flags */
#define	AGENTX_INSTANCE_REGISTRATION	0x01
#define	AGENTX_NEW_INDEX		0x02
#define	AGENTX_ANY_INDEX		0x04
#define	AGENTX_NON_DEFAULT_CONTEXT	0x08
#define	AGENTX_NETWORK_BYTE_ORDER	0x10
#define	AGENTX_FLAGS_MASK		0x1f

/* encoded data types */
#define	AGENTX_INTEGER			2
#define	AGENTX_OCTET_STRING		4
#define	AGENTX_NULL			5
#define	AGENTX_OBJECT_IDENTIFIER	6
#define	AGENTX_IP_ADDRESS		64
#define	AGENTX_COUNTER32		65
#define	AGENTX_GAUGE32			66
#define	AGENTX_TIME_TICKS		67
#define	AGENTX_OPAQUE			68
#define	AGENTX_COUNTER64		70
#define	AGENTX_NO_SUCH_OBJECT		128
#define	AGENTX_NO_SUCH_INSTANCE		129
#define	AGENTX_END_OF_MIB_VIEW		130

/* for registered MIB overlap */
#define	AGENTX_REGISTER_PRIO_DEFAULT	127

/* reasons for request of close */
#define AGENTX_CLOSE_OTHER		1
#define AGENTX_CLOSE_PARSE_ERROR	2
#define AGENTX_CLOSE_PROTOCOL_ERROR	3
#define AGENTX_CLOSE_TIMEOUTS		4
#define AGENTX_CLOSE_SHUTDOWN		5
#define AGENTX_CLOSE_BY_MANAGER		6

#define	AGENTX_DEFAULT_TIMEOUT		3

#define	MIN_OID_LEN		2       /* OBJECT */
#define	MAX_OID_LEN		32      /* OBJECT */

/*
 * Protocol header prefixed to all messages
 */
struct agentx_hdr {
	uint8_t		version;
	uint8_t		type;
	uint8_t		flags;
	uint8_t		reserved;
	uint32_t	sessionid;	/* chosen by agent */
	uint32_t	transactid;	/* chosen by subagent */
	uint32_t	packetid;	/* per-request id */
	uint32_t	length;
} __packed;

/*
 * Prefixed to a series of 4-byte values indicating the OID
 */
struct agentx_oid_hdr {
	uint8_t		n_subid;	/* # of oid elements (named in RFC) */
	uint8_t		prefix;		/* if not 0, OID is 1.3.6.1.<prefix> */
	uint8_t		include;	/* is OID included in search range */
	uint8_t		reserved;	/* always 0 */
} __packed;

struct agentx_response_data {
	uint32_t	sysuptime;	/* uptime of SNMP context */
	uint16_t	error;		/* status of request */
	uint16_t	index;		/* index of failed variable binding */
} __packed;

struct agentx_open_timeout {
	uint8_t		timeout;
	uint8_t		reserved[3];
} __packed;

struct agentx_register_hdr {
	uint8_t		timeout;
	uint8_t		priority;
	uint8_t		subrange;
	uint8_t		reserved;
} __packed;

struct agentx_unregister_hdr {
	uint8_t		reserved1;
	uint8_t		priority;
	uint8_t		subrange;
	uint8_t		reserved2;
} __packed;

struct agentx_null_oid {
	uint8_t		padding[4];
} __packed;

#define	AGENTX_NULL_OID	{ 0, 0, 0, 0 }

struct agentx_varbind_hdr {
	uint16_t	type;
	uint16_t	reserved;
} __packed;

struct agentx_response {
	struct agentx_hdr		hdr;
	struct agentx_response_data	data;
} __packed;

struct agentx_close_request_data {
	uint8_t			reason;
	uint8_t			padding[3];
} __packed;

struct agentx_close_request {
	struct agentx_hdr		hdr;
	struct agentx_close_request_data data;
} __packed;

struct agentx_getbulk_repeaters {
	uint16_t		nonrepeaters;
	uint16_t		maxrepetitions;
} __packed;

struct agentx_pdu {
	uint8_t		*buffer;
	uint8_t		*ptr;
	uint8_t		*ioptr;
	size_t		 buflen;
	size_t		 datalen;
	struct agentx_hdr *hdr;

	char		*context;
	uint32_t	 contextlen;

	void		  *cookie;
	struct agentx_pdu *request;	/* request this is a response to */
	TAILQ_ENTRY(agentx_pdu) entry;
};
TAILQ_HEAD(agentx_pdulist, agentx_pdu);

struct agentx_handle {
	int		 fd;
	uint32_t	 sessionid;
	uint32_t	 transactid;
	uint32_t	 packetid;
	int		 timeout;	/* in seconds */
	int		 error;
	int		 erridx;

	struct agentx_pdulist w;
	struct agentx_pdulist inflight;

	struct agentx_pdu *r;
};

struct agentx_search_range {
	struct snmp_oid	start;
	struct snmp_oid	end;
	int		include; /* is start oid included in search range */
};

struct agentx_handle *
	snmp_agentx_alloc(int);
struct agentx_handle *
	snmp_agentx_open(const char *, char *, struct snmp_oid *);
struct agentx_handle *
	snmp_agentx_fdopen(int, char *, struct snmp_oid *);
int	snmp_agentx_response(struct agentx_handle *, struct agentx_pdu *);
int	snmp_agentx_read_response(struct agentx_pdu *, struct agentx_response_data *);
int	snmp_agentx_open_response(struct agentx_handle *, struct agentx_pdu *);
struct agentx_pdu *
	snmp_agentx_open_pdu(struct agentx_handle *, char *descr,
	    struct snmp_oid *);
struct agentx_pdu *
	snmp_agentx_close_pdu(struct agentx_handle *, uint8_t);
int	snmp_agentx_close(struct agentx_handle *, uint8_t);
void	snmp_agentx_free(struct agentx_handle *);
int	snmp_agentx_ping(struct agentx_handle *);
struct agentx_pdu *
	snmp_agentx_ping_pdu(void);
struct agentx_pdu *
	snmp_agentx_notify_pdu(struct snmp_oid *);
struct agentx_pdu *
	snmp_agentx_request(struct agentx_handle *, struct agentx_pdu *);
int	snmp_agentx_varbind(struct agentx_pdu *, struct snmp_oid *, int,
	    void *, int);
int	snmp_agentx_send(struct agentx_handle *, struct agentx_pdu *);
int	snmp_agentx_enqueue(struct agentx_handle *, struct agentx_pdu *);
int	snmp_agentx_flush(struct agentx_handle *);
struct agentx_pdu *
	snmp_agentx_recv(struct agentx_handle *);
struct agentx_pdu *
	snmp_agentx_response_pdu(int, int, int);
struct agentx_pdu *
	snmp_agentx_register_pdu(struct snmp_oid *, int, int, int);
struct agentx_pdu *
	snmp_agentx_unregister_pdu(struct snmp_oid *, int, int);
struct agentx_pdu *
	snmp_agentx_get_pdu(struct snmp_oid *, int);
struct agentx_pdu *
	snmp_agentx_getnext_pdu(struct snmp_oid *, int);
char	*snmp_agentx_read_octetstr(struct agentx_pdu *, int *);
int	snmp_agentx_read_oid(struct agentx_pdu *, struct snmp_oid *);
int	snmp_agentx_read_searchrange(struct agentx_pdu *,
	    struct agentx_search_range *);
int	snmp_agentx_read_raw(struct agentx_pdu *, void *, int);
int	snmp_agentx_copy_raw(struct agentx_pdu *, void *, int);
char	*snmp_agentx_type2name(int);
int	snmp_agentx_read_int(struct agentx_pdu *, uint32_t *);
int	snmp_agentx_read_int64(struct agentx_pdu *, uint64_t *);
int	snmp_agentx_raw(struct agentx_pdu *, void *, int);
int	snmp_agentx_read_vbhdr(struct agentx_pdu *, struct
	    agentx_varbind_hdr *);
struct agentx_pdu *snmp_agentx_pdu_alloc(void);
void	snmp_agentx_pdu_free(struct agentx_pdu *);
char	*snmp_oid2string(struct snmp_oid *, char *, size_t);
int	snmp_oid_cmp(struct snmp_oid *, struct snmp_oid *);
void	snmp_oid_increment(struct snmp_oid *);

#if BYTE_ORDER == BIG_ENDIAN

static __inline int
snmp_agentx_byteorder_native(struct agentx_hdr *h)
{
	return ((h->flags & AGENTX_NETWORK_BYTE_ORDER) != 0);
}

#define AGENTX_LOCAL_BYTE_ORDER_FLAG AGENTX_NETWORK_BYTE_ORDER
#define snmp_agentx_int_byteswap(_i)	htole32(_i)
#define snmp_agentx_int16_byteswap(_i)	htole16(_i)
#define snmp_agentx_int64_byteswap(_i)	htole64(_i)

#elif BYTE_ORDER == LITTLE_ENDIAN

static __inline int
snmp_agentx_byteorder_native(struct agentx_hdr *h)
{
	return ((h->flags & AGENTX_NETWORK_BYTE_ORDER) == 0);
}

#define AGENTX_LOCAL_BYTE_ORDER_FLAG 0
#define snmp_agentx_int_byteswap(_i)	htobe32(_i)
#define snmp_agentx_int16_byteswap(_i)	htobe16(_i)
#define snmp_agentx_int64_byteswap(_i)	htobe64(_i)

#else
#error "Unknown host byte order"
#endif

#endif /* SNMPD_SNMP_H */