xref: /openbsd/usr.sbin/snmpd/ax.h (revision b26e5074)
1 /*	$OpenBSD: ax.h,v 1.4 2024/02/20 12:25:43 martijn Exp $ */
2 /*
3  * Copyright (c) 2019 Martijn van Duren <martijn@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/types.h>
19 
20 #include <endian.h>
21 #include <stdint.h>
22 
23 #define AX_PDU_FLAG_INSTANCE_REGISTRATION (1 << 0)
24 #define AX_PDU_FLAG_NEW_INDEX (1 << 1)
25 #define AX_PDU_FLAG_ANY_INDEX (1 << 2)
26 #define AX_PDU_FLAG_NON_DEFAULT_CONTEXT (1 << 3)
27 #define AX_PDU_FLAG_NETWORK_BYTE_ORDER (1 << 4)
28 
29 #define AX_PRIORITY_DEFAULT 127
30 
31 enum ax_byte_order {
32 	AX_BYTE_ORDER_BE,
33 	AX_BYTE_ORDER_LE
34 };
35 
36 #if BYTE_ORDER == BIG_ENDIAN
37 #define AX_BYTE_ORDER_NATIVE AX_BYTE_ORDER_BE
38 #else
39 #define AX_BYTE_ORDER_NATIVE AX_BYTE_ORDER_LE
40 #endif
41 
42 enum ax_pdu_type {
43 	AX_PDU_TYPE_OPEN		= 1,
44 	AX_PDU_TYPE_CLOSE		= 2,
45 	AX_PDU_TYPE_REGISTER		= 3,
46 	AX_PDU_TYPE_UNREGISTER		= 4,
47 	AX_PDU_TYPE_GET			= 5,
48 	AX_PDU_TYPE_GETNEXT		= 6,
49 	AX_PDU_TYPE_GETBULK		= 7,
50 	AX_PDU_TYPE_TESTSET		= 8,
51 	AX_PDU_TYPE_COMMITSET		= 9,
52 	AX_PDU_TYPE_UNDOSET		= 10,
53 	AX_PDU_TYPE_CLEANUPSET		= 11,
54 	AX_PDU_TYPE_NOTIFY		= 12,
55 	AX_PDU_TYPE_PING		= 13,
56 	AX_PDU_TYPE_INDEXALLOCATE	= 14,
57 	AX_PDU_TYPE_INDEXDEALLOCATE	= 15,
58 	AX_PDU_TYPE_ADDAGENTCAPS	= 16,
59 	AX_PDU_TYPE_REMOVEAGENTCAPS	= 17,
60 	AX_PDU_TYPE_RESPONSE		= 18
61 };
62 
63 enum ax_pdu_error {
64 	AX_PDU_ERROR_NOERROR			= 0,
65 	AX_PDU_ERROR_GENERR			= 5,
66 	AX_PDU_ERROR_NOACCESS			= 6,
67 	AX_PDU_ERROR_WRONGTYPE			= 7,
68 	AX_PDU_ERROR_WRONGLENGTH		= 8,
69 	AX_PDU_ERROR_WRONGENCODING		= 9,
70 	AX_PDU_ERROR_WRONGVALUE			= 10,
71 	AX_PDU_ERROR_NOCREATION			= 11,
72 	AX_PDU_ERROR_INCONSISTENTVALUE		= 12,
73 	AX_PDU_ERROR_RESOURCEUNAVAILABLE	= 13,
74 	AX_PDU_ERROR_COMMITFAILED		= 14,
75 	AX_PDU_ERROR_UNDOFAILED			= 15,
76 	AX_PDU_ERROR_NOTWRITABLE		= 17,
77 	AX_PDU_ERROR_INCONSISTENTNAME		= 18,
78 	AX_PDU_ERROR_OPENFAILED			= 256,
79 	AX_PDU_ERROR_NOTOPEN			= 257,
80 	AX_PDU_ERROR_INDEXWRONGTYPE		= 258,
81 	AX_PDU_ERROR_INDEXALREADYALLOCATED	= 259,
82 	AX_PDU_ERROR_INDEXNONEAVAILABLE		= 260,
83 	AX_PDU_ERROR_INDEXNOTALLOCATED		= 261,
84 	AX_PDU_ERROR_UNSUPPORTEDCONETXT		= 262,
85 	AX_PDU_ERROR_DUPLICATEREGISTRATION	= 263,
86 	AX_PDU_ERROR_UNKNOWNREGISTRATION	= 264,
87 	AX_PDU_ERROR_UNKNOWNAGENTCAPS		= 265,
88 	AX_PDU_ERROR_PARSEERROR			= 266,
89 	AX_PDU_ERROR_REQUESTDENIED		= 267,
90 	AX_PDU_ERROR_PROCESSINGERROR		= 268
91 };
92 
93 enum ax_data_type {
94 	AX_DATA_TYPE_INTEGER		= 2,
95 	AX_DATA_TYPE_OCTETSTRING	= 4,
96 	AX_DATA_TYPE_NULL		= 5,
97 	AX_DATA_TYPE_OID		= 6,
98 	AX_DATA_TYPE_IPADDRESS		= 64,
99 	AX_DATA_TYPE_COUNTER32		= 65,
100 	AX_DATA_TYPE_GAUGE32		= 66,
101 	AX_DATA_TYPE_TIMETICKS		= 67,
102 	AX_DATA_TYPE_OPAQUE		= 68,
103 	AX_DATA_TYPE_COUNTER64		= 70,
104 	AX_DATA_TYPE_NOSUCHOBJECT	= 128,
105 	AX_DATA_TYPE_NOSUCHINSTANCE	= 129,
106 	AX_DATA_TYPE_ENDOFMIBVIEW	= 130
107 };
108 
109 enum ax_close_reason {
110 	AX_CLOSE_OTHER			= 1,
111 	AX_CLOSEN_PARSEERROR		= 2,
112 	AX_CLOSE_PROTOCOLERROR		= 3,
113 	AX_CLOSE_TIMEOUTS		= 4,
114 	AX_CLOSE_SHUTDOWN		= 5,
115 	AX_CLOSE_BYMANAGER		= 6
116 };
117 
118 struct ax {
119 	int ax_fd;
120 	enum ax_byte_order ax_byteorder;
121 	uint8_t *ax_rbuf;
122 	size_t ax_rblen;
123 	size_t ax_rbsize;
124 	uint8_t *ax_wbuf;
125 	size_t ax_wblen;
126 	size_t ax_wbtlen;
127 	size_t ax_wbsize;
128 };
129 
130 #ifndef AX_PRIMITIVE
131 #define AX_PRIMITIVE
132 
133 #define AX_OID_MAX_LEN 128
134 
135 struct ax_oid {
136 	uint8_t aoi_include;
137 	uint32_t aoi_id[AX_OID_MAX_LEN];
138 	size_t aoi_idlen;
139 };
140 
141 struct ax_ostring {
142 	unsigned char *aos_string;
143 	uint32_t aos_slen;
144 };
145 #endif
146 
147 struct ax_searchrange {
148 	struct ax_oid asr_start;
149 	struct ax_oid asr_stop;
150 };
151 
152 struct ax_pdu_header {
153 	uint8_t	aph_version;
154 	uint8_t	aph_type;
155 	uint8_t	aph_flags;
156 	uint8_t	aph_reserved;
157 	uint32_t aph_sessionid;
158 	uint32_t aph_transactionid;
159 	uint32_t aph_packetid;
160 	uint32_t aph_plength;
161 };
162 
163 struct ax_varbind {
164 	enum ax_data_type avb_type;
165 	struct ax_oid avb_oid;
166 	union ax_data {
167 		int32_t avb_int32;
168 		uint32_t avb_uint32;
169 		uint64_t avb_uint64;
170 		struct ax_ostring avb_ostring;
171 		struct ax_oid avb_oid;
172 	} avb_data;
173 };
174 
175 struct ax_pdu {
176 	struct ax_pdu_header ap_header;
177 	struct ax_ostring ap_context;
178 	union {
179 		struct ax_pdu_open {
180 			uint8_t ap_timeout;
181 			struct ax_oid ap_oid;
182 			struct ax_ostring ap_descr;
183 		} ap_open;
184 		struct ax_pdu_close {
185 			enum ax_close_reason ap_reason;
186 		} ap_close;
187 		struct ax_pdu_register {
188 			uint8_t ap_timeout;
189 			uint8_t ap_priority;
190 			uint8_t ap_range_subid;
191 			struct ax_oid ap_subtree;
192 			uint32_t ap_upper_bound;
193 		} ap_register;
194 		struct ax_pdu_unregister {
195 			uint8_t ap_priority;
196 			uint8_t ap_range_subid;
197 			struct ax_oid ap_subtree;
198 			uint32_t ap_upper_bound;
199 		} ap_unregister;
200 		struct ax_pdu_searchrangelist {
201 			size_t ap_nsr;
202 			struct ax_searchrange *ap_sr;
203 		} ap_srl;
204 		struct ax_pdu_getbulk {
205 			uint16_t ap_nonrep;
206 			uint16_t ap_maxrep;
207 			struct ax_pdu_searchrangelist ap_srl;
208 		} ap_getbulk;
209 		struct ax_pdu_varbindlist {
210 			struct ax_varbind *ap_varbind;
211 			size_t ap_nvarbind;
212 		} ap_vbl;
213 		struct ax_pdu_addagentcaps {
214 			struct ax_oid ap_oid;
215 			struct ax_ostring ap_descr;
216 		} ap_addagentcaps;
217 		struct ax_pdu_removeagentcaps {
218 			struct ax_oid ap_oid;
219 		} ap_removeagentcaps;
220 		struct ax_pdu_response {
221 			uint32_t ap_uptime;
222 			enum ax_pdu_error ap_error;
223 			uint16_t ap_index;
224 			struct ax_varbind *ap_varbindlist;
225 			size_t ap_nvarbind;
226 		} ap_response;
227 	} ap_payload;
228 };
229 
230 struct ax *ax_new(int);
231 void ax_free(struct ax *);
232 struct ax_pdu *ax_recv(struct ax *);
233 ssize_t ax_send(struct ax *);
234 uint32_t ax_open(struct ax *, uint8_t, struct ax_oid *,
235     struct ax_ostring *);
236 uint32_t ax_close(struct ax *, uint32_t, enum ax_close_reason);
237 int ax_get(struct ax *, uint32_t, uint32_t, uint32_t, struct ax_ostring *,
238     struct ax_searchrange *, size_t);
239 int ax_getnext(struct ax *, uint32_t, uint32_t, uint32_t, struct ax_ostring *,
240     struct ax_searchrange *, size_t);
241 uint32_t ax_indexallocate(struct ax *, uint8_t, uint32_t,
242     struct ax_ostring *, struct ax_varbind *, size_t);
243 uint32_t ax_indexdeallocate(struct ax *, uint32_t,
244     struct ax_ostring *, struct ax_varbind *, size_t);
245 uint32_t ax_addagentcaps(struct ax *, uint32_t, struct ax_ostring *,
246     struct ax_oid *, struct ax_ostring *);
247 uint32_t ax_removeagentcaps(struct ax *, uint32_t,
248     struct ax_ostring *, struct ax_oid *);
249 uint32_t ax_register(struct ax *, uint8_t, uint32_t,
250     struct ax_ostring *, uint8_t, uint8_t, uint8_t, struct ax_oid *,
251     uint32_t);
252 uint32_t ax_unregister(struct ax *, uint32_t, struct ax_ostring *,
253     uint8_t, uint8_t, struct ax_oid *, uint32_t);
254 int ax_response(struct ax *, uint32_t, uint32_t, uint32_t,
255     uint32_t, uint16_t, uint16_t, struct ax_varbind *, size_t);
256 void ax_pdu_free(struct ax_pdu *);
257 void ax_varbind_free(struct ax_varbind *);
258 const char *ax_error2string(enum ax_pdu_error);
259 const char *ax_pdutype2string(enum ax_pdu_type);
260 const char *ax_oid2string(struct ax_oid *);
261 const char *ax_oidrange2string(struct ax_oid *, uint8_t, uint32_t);
262 const char *ax_varbind2string(struct ax_varbind *);
263 const char *ax_closereason2string(enum ax_close_reason);
264 int ax_oid_cmp(struct ax_oid *, struct ax_oid *);
265 int ax_oid_add(struct ax_oid *, uint32_t);
266