]>
Commit | Line | Data |
---|---|---|
d6be5fb9 VB |
1 | /* SNMP support |
2 | * Copyright (C) 2012 Vincent Bernat <bernat@luffy.cx> | |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
896014f4 DL |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
d6be5fb9 VB |
19 | */ |
20 | ||
21 | #include <zebra.h> | |
22 | ||
5986b66b | 23 | #ifdef SNMP_AGENTX |
d6be5fb9 VB |
24 | #include <net-snmp/net-snmp-config.h> |
25 | #include <net-snmp/net-snmp-includes.h> | |
56e2c5e8 DL |
26 | #include <net-snmp/agent/net-snmp-agent-includes.h> |
27 | #include <net-snmp/agent/snmp_vars.h> | |
d6be5fb9 VB |
28 | |
29 | #include "command.h" | |
30 | #include "smux.h" | |
d11f748b | 31 | #include "memory.h" |
56e2c5e8 | 32 | #include "linklist.h" |
ae435b19 | 33 | #include "version.h" |
220d7368 | 34 | #include "lib_errors.h" |
b2fa8c0f DL |
35 | #include "xref.h" |
36 | ||
80413c20 | 37 | XREF_SETUP(); |
d6be5fb9 | 38 | |
8451921b | 39 | DEFINE_HOOK(agentx_enabled, (), ()); |
ec48460e | 40 | |
56e2c5e8 DL |
41 | static int agentx_enabled = 0; |
42 | ||
43 | static struct thread_master *agentx_tm; | |
44 | static struct thread *timeout_thr = NULL; | |
45 | static struct list *events = NULL; | |
46 | ||
47 | static void agentx_events_update(void); | |
48 | ||
d62a17ae | 49 | static int agentx_timeout(struct thread *t) |
56e2c5e8 | 50 | { |
d62a17ae | 51 | timeout_thr = NULL; |
56e2c5e8 | 52 | |
d62a17ae | 53 | snmp_timeout(); |
54 | run_alarms(); | |
55 | netsnmp_check_outstanding_agent_requests(); | |
56 | agentx_events_update(); | |
57 | return 0; | |
56e2c5e8 DL |
58 | } |
59 | ||
d62a17ae | 60 | static int agentx_read(struct thread *t) |
56e2c5e8 | 61 | { |
d62a17ae | 62 | fd_set fds; |
19d95d40 | 63 | int flags, new_flags = 0; |
3b717261 | 64 | int nonblock = false; |
d62a17ae | 65 | struct listnode *ln = THREAD_ARG(t); |
66 | list_delete_node(events, ln); | |
56e2c5e8 | 67 | |
3b717261 | 68 | /* fix for non blocking socket */ |
69 | flags = fcntl(THREAD_FD(t), F_GETFL, 0); | |
19d95d40 DS |
70 | if (-1 == flags) { |
71 | flog_err(EC_LIB_SYSTEM_CALL, "Failed to get FD settings fcntl: %s(%d)", | |
72 | strerror(errno), errno); | |
3b717261 | 73 | return -1; |
19d95d40 | 74 | } |
3b717261 | 75 | |
76 | if (flags & O_NONBLOCK) | |
77 | nonblock = true; | |
78 | else | |
19d95d40 DS |
79 | new_flags = fcntl(THREAD_FD(t), F_SETFL, flags | O_NONBLOCK); |
80 | ||
81 | if (new_flags == -1) | |
82 | flog_err(EC_LIB_SYSTEM_CALL, "Failed to set snmp fd non blocking: %s(%d)", | |
83 | strerror(errno), errno); | |
3b717261 | 84 | |
d62a17ae | 85 | FD_ZERO(&fds); |
86 | FD_SET(THREAD_FD(t), &fds); | |
87 | snmp_read(&fds); | |
56e2c5e8 | 88 | |
3b717261 | 89 | /* Reset the flag */ |
19d95d40 DS |
90 | if (!nonblock) { |
91 | new_flags = fcntl(THREAD_FD(t), F_SETFL, flags); | |
92 | ||
93 | if (new_flags == -1) | |
94 | flog_err( | |
95 | EC_LIB_SYSTEM_CALL, | |
96 | "Failed to set snmp fd back to original settings: %s(%d)", | |
97 | strerror(errno), errno); | |
98 | } | |
3b717261 | 99 | |
d62a17ae | 100 | netsnmp_check_outstanding_agent_requests(); |
101 | agentx_events_update(); | |
102 | return 0; | |
56e2c5e8 DL |
103 | } |
104 | ||
d62a17ae | 105 | static void agentx_events_update(void) |
56e2c5e8 | 106 | { |
d62a17ae | 107 | int maxfd = 0; |
108 | int block = 1; | |
109 | struct timeval timeout = {.tv_sec = 0, .tv_usec = 0}; | |
110 | fd_set fds; | |
111 | struct listnode *ln; | |
112 | struct thread *thr; | |
113 | int fd, thr_fd; | |
114 | ||
50478845 | 115 | thread_cancel(&timeout_thr); |
d62a17ae | 116 | |
117 | FD_ZERO(&fds); | |
118 | snmp_select_info(&maxfd, &fds, &timeout, &block); | |
119 | ||
120 | if (!block) { | |
121 | timeout_thr = NULL; | |
122 | thread_add_timer_tv(agentx_tm, agentx_timeout, NULL, &timeout, | |
123 | &timeout_thr); | |
124 | } | |
125 | ||
126 | ln = listhead(events); | |
127 | thr = ln ? listgetdata(ln) : NULL; | |
128 | thr_fd = thr ? THREAD_FD(thr) : -1; | |
129 | ||
130 | /* "two-pointer" / two-list simultaneous iteration | |
131 | * ln/thr/thr_fd point to the next existing event listener to hit while | |
132 | * fd counts to catch up */ | |
133 | for (fd = 0; fd < maxfd; fd++) { | |
134 | /* caught up */ | |
135 | if (thr_fd == fd) { | |
136 | struct listnode *nextln = listnextnode(ln); | |
137 | if (!FD_ISSET(fd, &fds)) { | |
b3d6bc6e | 138 | thread_cancel(&thr); |
d62a17ae | 139 | list_delete_node(events, ln); |
140 | } | |
141 | ln = nextln; | |
142 | thr = ln ? listgetdata(ln) : NULL; | |
143 | thr_fd = thr ? THREAD_FD(thr) : -1; | |
144 | } | |
145 | /* need listener, but haven't hit one where it would be */ | |
146 | else if (FD_ISSET(fd, &fds)) { | |
147 | struct listnode *newln; | |
148 | thr = NULL; | |
149 | thread_add_read(agentx_tm, agentx_read, NULL, fd, &thr); | |
150 | newln = listnode_add_before(events, ln, thr); | |
151 | thr->arg = newln; | |
152 | } | |
153 | } | |
154 | ||
155 | /* leftover event listeners at this point have fd > maxfd, delete them | |
156 | */ | |
157 | while (ln) { | |
158 | struct listnode *nextln = listnextnode(ln); | |
b3d6bc6e MS |
159 | thr = listgetdata(ln); |
160 | thread_cancel(&thr); | |
d62a17ae | 161 | list_delete_node(events, ln); |
162 | ln = nextln; | |
163 | } | |
56e2c5e8 | 164 | } |
d6be5fb9 VB |
165 | |
166 | /* AgentX node. */ | |
612c2c15 | 167 | static int config_write_agentx(struct vty *vty); |
62b346ee | 168 | static struct cmd_node agentx_node = { |
f4b8291f | 169 | .name = "smux", |
62b346ee DL |
170 | .node = SMUX_NODE, |
171 | .prompt = "", | |
612c2c15 | 172 | .config_write = config_write_agentx, |
62b346ee | 173 | }; |
d6be5fb9 VB |
174 | |
175 | /* Logging NetSNMP messages */ | |
d62a17ae | 176 | static int agentx_log_callback(int major, int minor, void *serverarg, |
177 | void *clientarg) | |
d6be5fb9 | 178 | { |
d62a17ae | 179 | struct snmp_log_message *slm = (struct snmp_log_message *)serverarg; |
180 | char *msg = XSTRDUP(MTYPE_TMP, slm->msg); | |
181 | if (msg) | |
182 | msg[strlen(msg) - 1] = '\0'; | |
183 | switch (slm->priority) { | |
184 | case LOG_EMERG: | |
1c50c1c0 | 185 | flog_err(EC_LIB_SNMP, "snmp[emerg]: %s", msg ? msg : slm->msg); |
d62a17ae | 186 | break; |
187 | case LOG_ALERT: | |
1c50c1c0 | 188 | flog_err(EC_LIB_SNMP, "snmp[alert]: %s", msg ? msg : slm->msg); |
d62a17ae | 189 | break; |
190 | case LOG_CRIT: | |
1c50c1c0 | 191 | flog_err(EC_LIB_SNMP, "snmp[crit]: %s", msg ? msg : slm->msg); |
d62a17ae | 192 | break; |
193 | case LOG_ERR: | |
1c50c1c0 | 194 | flog_err(EC_LIB_SNMP, "snmp[err]: %s", msg ? msg : slm->msg); |
d62a17ae | 195 | break; |
196 | case LOG_WARNING: | |
450971aa | 197 | flog_warn(EC_LIB_SNMP, "snmp[warning]: %s", |
ade6974d | 198 | msg ? msg : slm->msg); |
d62a17ae | 199 | break; |
200 | case LOG_NOTICE: | |
201 | zlog_notice("snmp[notice]: %s", msg ? msg : slm->msg); | |
202 | break; | |
203 | case LOG_INFO: | |
204 | zlog_info("snmp[info]: %s", msg ? msg : slm->msg); | |
205 | break; | |
206 | case LOG_DEBUG: | |
207 | zlog_debug("snmp[debug]: %s", msg ? msg : slm->msg); | |
208 | break; | |
209 | } | |
210 | XFREE(MTYPE_TMP, msg); | |
211 | return SNMP_ERR_NOERROR; | |
d6be5fb9 VB |
212 | } |
213 | ||
d62a17ae | 214 | static int config_write_agentx(struct vty *vty) |
d6be5fb9 | 215 | { |
d62a17ae | 216 | if (agentx_enabled) |
217 | vty_out(vty, "agentx\n"); | |
218 | return 1; | |
d6be5fb9 VB |
219 | } |
220 | ||
221 | DEFUN (agentx_enable, | |
222 | agentx_enable_cmd, | |
223 | "agentx", | |
007b0667 | 224 | "SNMP AgentX protocol settings\n") |
d6be5fb9 | 225 | { |
d62a17ae | 226 | if (!agentx_enabled) { |
227 | init_snmp(FRR_SMUX_NAME); | |
228 | events = list_new(); | |
229 | agentx_events_update(); | |
230 | agentx_enabled = 1; | |
ec48460e | 231 | hook_call(agentx_enabled); |
d62a17ae | 232 | } |
5fedee18 | 233 | |
d62a17ae | 234 | return CMD_SUCCESS; |
d6be5fb9 VB |
235 | } |
236 | ||
237 | DEFUN (no_agentx, | |
238 | no_agentx_cmd, | |
239 | "no agentx", | |
240 | NO_STR | |
007b0667 | 241 | "SNMP AgentX protocol settings\n") |
d6be5fb9 | 242 | { |
d62a17ae | 243 | if (!agentx_enabled) |
244 | return CMD_SUCCESS; | |
245 | vty_out(vty, "SNMP AgentX support cannot be disabled once enabled\n"); | |
246 | return CMD_WARNING_CONFIG_FAILED; | |
d6be5fb9 VB |
247 | } |
248 | ||
1ee746d9 | 249 | int smux_enabled(void) |
250 | { | |
251 | return agentx_enabled; | |
252 | } | |
253 | ||
d62a17ae | 254 | void smux_init(struct thread_master *tm) |
d6be5fb9 | 255 | { |
d62a17ae | 256 | agentx_tm = tm; |
257 | ||
258 | netsnmp_enable_subagent(); | |
259 | snmp_disable_log(); | |
260 | snmp_enable_calllog(); | |
261 | snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_LOGGING, | |
262 | agentx_log_callback, NULL); | |
263 | init_agent(FRR_SMUX_NAME); | |
264 | ||
612c2c15 | 265 | install_node(&agentx_node); |
d62a17ae | 266 | install_element(CONFIG_NODE, &agentx_enable_cmd); |
267 | install_element(CONFIG_NODE, &no_agentx_cmd); | |
d6be5fb9 VB |
268 | } |
269 | ||
ec48460e KS |
270 | void smux_agentx_enable(void) |
271 | { | |
272 | if (!agentx_enabled) { | |
273 | init_snmp(FRR_SMUX_NAME); | |
274 | events = list_new(); | |
275 | agentx_events_update(); | |
276 | agentx_enabled = 1; | |
277 | } | |
278 | } | |
279 | ||
d62a17ae | 280 | void smux_register_mib(const char *descr, struct variable *var, size_t width, |
281 | int num, oid name[], size_t namelen) | |
d6be5fb9 | 282 | { |
d62a17ae | 283 | register_mib(descr, var, width, num, name, namelen); |
d6be5fb9 VB |
284 | } |
285 | ||
97e21b4b DS |
286 | void smux_trap(struct variable *vp, size_t vp_len, const oid *ename, |
287 | size_t enamelen, const oid *name, size_t namelen, | |
288 | const oid *iname, size_t inamelen, | |
289 | const struct trap_object *trapobj, size_t trapobjlen, | |
290 | uint8_t sptrap) | |
4eb8c74f PR |
291 | { |
292 | struct index_oid trap_index[1]; | |
293 | ||
294 | /* copy the single index into the multi-index format */ | |
295 | oid_copy(trap_index[0].indexname, iname, inamelen); | |
296 | trap_index[0].indexlen = inamelen; | |
297 | ||
97e21b4b DS |
298 | smux_trap_multi_index(vp, vp_len, ename, enamelen, name, namelen, |
299 | trap_index, array_size(trap_index), trapobj, | |
300 | trapobjlen, sptrap); | |
4eb8c74f PR |
301 | } |
302 | ||
303 | int smux_trap_multi_index(struct variable *vp, size_t vp_len, const oid *ename, | |
304 | size_t enamelen, const oid *name, size_t namelen, | |
305 | struct index_oid *iname, size_t index_len, | |
306 | const struct trap_object *trapobj, size_t trapobjlen, | |
307 | uint8_t sptrap) | |
d6be5fb9 | 308 | { |
d62a17ae | 309 | oid objid_snmptrap[] = {1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0}; |
0d6f7fd6 | 310 | size_t objid_snmptrap_len = sizeof(objid_snmptrap) / sizeof(oid); |
d62a17ae | 311 | oid notification_oid[MAX_OID_LEN]; |
312 | size_t notification_oid_len; | |
313 | unsigned int i; | |
314 | ||
315 | netsnmp_variable_list *notification_vars = NULL; | |
316 | if (!agentx_enabled) | |
317 | return 0; | |
318 | ||
319 | /* snmpTrapOID */ | |
320 | oid_copy(notification_oid, ename, enamelen); | |
321 | notification_oid[enamelen] = sptrap; | |
322 | notification_oid_len = enamelen + 1; | |
323 | snmp_varlist_add_variable(¬ification_vars, objid_snmptrap, | |
324 | objid_snmptrap_len, ASN_OBJECT_ID, | |
d7c0a89a | 325 | (uint8_t *)notification_oid, |
d62a17ae | 326 | notification_oid_len * sizeof(oid)); |
327 | ||
328 | /* Provided bindings */ | |
329 | for (i = 0; i < trapobjlen; i++) { | |
330 | unsigned int j; | |
331 | oid oid[MAX_OID_LEN]; | |
332 | size_t oid_len, onamelen; | |
d7c0a89a | 333 | uint8_t *val; |
d62a17ae | 334 | size_t val_len; |
335 | WriteMethod *wm = NULL; | |
336 | struct variable cvp; | |
4eb8c74f PR |
337 | unsigned int iindex; |
338 | /* | |
339 | * this allows the behaviour of smux_trap with a singe index | |
340 | * for all objects to be maintained whilst allowing traps which | |
341 | * have different indices per object to be supported | |
342 | */ | |
343 | iindex = (index_len == 1) ? 0 : i; | |
d62a17ae | 344 | |
345 | /* Make OID. */ | |
346 | if (trapobj[i].namelen > 0) { | |
347 | /* Columnar object */ | |
348 | onamelen = trapobj[i].namelen; | |
349 | oid_copy(oid, name, namelen); | |
350 | oid_copy(oid + namelen, trapobj[i].name, onamelen); | |
4eb8c74f PR |
351 | oid_copy(oid + namelen + onamelen, |
352 | iname[iindex].indexname, | |
353 | iname[iindex].indexlen); | |
354 | oid_len = namelen + onamelen + iname[iindex].indexlen; | |
d62a17ae | 355 | } else { |
356 | /* Scalar object */ | |
357 | onamelen = trapobj[i].namelen * (-1); | |
358 | oid_copy(oid, name, namelen); | |
359 | oid_copy(oid + namelen, trapobj[i].name, onamelen); | |
360 | oid[onamelen + namelen] = 0; | |
361 | oid_len = namelen + onamelen + 1; | |
362 | } | |
363 | ||
364 | /* Locate the appropriate function and type in the MIB registry. | |
365 | */ | |
366 | for (j = 0; j < vp_len; j++) { | |
367 | if (oid_compare(trapobj[i].name, onamelen, vp[j].name, | |
368 | vp[j].namelen) | |
369 | != 0) | |
370 | continue; | |
371 | /* We found the appropriate variable in the MIB | |
372 | * registry. */ | |
373 | oid_copy(cvp.name, name, namelen); | |
374 | oid_copy(cvp.name + namelen, vp[j].name, vp[j].namelen); | |
375 | cvp.namelen = namelen + vp[j].namelen; | |
376 | cvp.type = vp[j].type; | |
377 | cvp.magic = vp[j].magic; | |
378 | cvp.acl = vp[j].acl; | |
379 | cvp.findVar = vp[j].findVar; | |
4eb8c74f | 380 | |
d62a17ae | 381 | /* Grab the result. */ |
382 | val = cvp.findVar(&cvp, oid, &oid_len, 1, &val_len, | |
383 | &wm); | |
384 | if (!val) | |
385 | break; | |
386 | snmp_varlist_add_variable(¬ification_vars, oid, | |
387 | oid_len, vp[j].type, val, | |
388 | val_len); | |
389 | break; | |
390 | } | |
b7c0d065 | 391 | } |
b7c0d065 VB |
392 | |
393 | ||
d62a17ae | 394 | send_v2trap(notification_vars); |
395 | snmp_free_varbind(notification_vars); | |
396 | agentx_events_update(); | |
397 | return 1; | |
d6be5fb9 VB |
398 | } |
399 | ||
1ee746d9 | 400 | void smux_events_update(void) |
401 | { | |
402 | agentx_events_update(); | |
403 | } | |
404 | ||
5986b66b | 405 | #endif /* SNMP_AGENTX */ |