]> git.proxmox.com Git - mirror_frr.git/blame - lib/agentx.c
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[mirror_frr.git] / lib / agentx.c
CommitLineData
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"
d6be5fb9 35
56e2c5e8
DL
36static int agentx_enabled = 0;
37
38static struct thread_master *agentx_tm;
39static struct thread *timeout_thr = NULL;
40static struct list *events = NULL;
41
42static void agentx_events_update(void);
43
d62a17ae 44static int agentx_timeout(struct thread *t)
56e2c5e8 45{
d62a17ae 46 timeout_thr = NULL;
56e2c5e8 47
d62a17ae 48 snmp_timeout();
49 run_alarms();
50 netsnmp_check_outstanding_agent_requests();
51 agentx_events_update();
52 return 0;
56e2c5e8
DL
53}
54
d62a17ae 55static int agentx_read(struct thread *t)
56e2c5e8 56{
d62a17ae 57 fd_set fds;
58 struct listnode *ln = THREAD_ARG(t);
59 list_delete_node(events, ln);
56e2c5e8 60
d62a17ae 61 FD_ZERO(&fds);
62 FD_SET(THREAD_FD(t), &fds);
63 snmp_read(&fds);
56e2c5e8 64
d62a17ae 65 netsnmp_check_outstanding_agent_requests();
66 agentx_events_update();
67 return 0;
56e2c5e8
DL
68}
69
d62a17ae 70static void agentx_events_update(void)
56e2c5e8 71{
d62a17ae 72 int maxfd = 0;
73 int block = 1;
74 struct timeval timeout = {.tv_sec = 0, .tv_usec = 0};
75 fd_set fds;
76 struct listnode *ln;
77 struct thread *thr;
78 int fd, thr_fd;
79
80 THREAD_OFF(timeout_thr);
81
82 FD_ZERO(&fds);
83 snmp_select_info(&maxfd, &fds, &timeout, &block);
84
85 if (!block) {
86 timeout_thr = NULL;
87 thread_add_timer_tv(agentx_tm, agentx_timeout, NULL, &timeout,
88 &timeout_thr);
89 }
90
91 ln = listhead(events);
92 thr = ln ? listgetdata(ln) : NULL;
93 thr_fd = thr ? THREAD_FD(thr) : -1;
94
95 /* "two-pointer" / two-list simultaneous iteration
96 * ln/thr/thr_fd point to the next existing event listener to hit while
97 * fd counts to catch up */
98 for (fd = 0; fd < maxfd; fd++) {
99 /* caught up */
100 if (thr_fd == fd) {
101 struct listnode *nextln = listnextnode(ln);
102 if (!FD_ISSET(fd, &fds)) {
103 thread_cancel(thr);
104 list_delete_node(events, ln);
105 }
106 ln = nextln;
107 thr = ln ? listgetdata(ln) : NULL;
108 thr_fd = thr ? THREAD_FD(thr) : -1;
109 }
110 /* need listener, but haven't hit one where it would be */
111 else if (FD_ISSET(fd, &fds)) {
112 struct listnode *newln;
113 thr = NULL;
114 thread_add_read(agentx_tm, agentx_read, NULL, fd, &thr);
115 newln = listnode_add_before(events, ln, thr);
116 thr->arg = newln;
117 }
118 }
119
120 /* leftover event listeners at this point have fd > maxfd, delete them
121 */
122 while (ln) {
123 struct listnode *nextln = listnextnode(ln);
124 thread_cancel(listgetdata(ln));
125 list_delete_node(events, ln);
126 ln = nextln;
127 }
56e2c5e8 128}
d6be5fb9
VB
129
130/* AgentX node. */
d62a17ae 131static struct cmd_node agentx_node = {SMUX_NODE,
132 "", /* AgentX has no interface. */
133 1};
d6be5fb9
VB
134
135/* Logging NetSNMP messages */
d62a17ae 136static int agentx_log_callback(int major, int minor, void *serverarg,
137 void *clientarg)
d6be5fb9 138{
d62a17ae 139 struct snmp_log_message *slm = (struct snmp_log_message *)serverarg;
140 char *msg = XSTRDUP(MTYPE_TMP, slm->msg);
141 if (msg)
142 msg[strlen(msg) - 1] = '\0';
143 switch (slm->priority) {
144 case LOG_EMERG:
1c50c1c0 145 flog_err(EC_LIB_SNMP, "snmp[emerg]: %s", msg ? msg : slm->msg);
d62a17ae 146 break;
147 case LOG_ALERT:
1c50c1c0 148 flog_err(EC_LIB_SNMP, "snmp[alert]: %s", msg ? msg : slm->msg);
d62a17ae 149 break;
150 case LOG_CRIT:
1c50c1c0 151 flog_err(EC_LIB_SNMP, "snmp[crit]: %s", msg ? msg : slm->msg);
d62a17ae 152 break;
153 case LOG_ERR:
1c50c1c0 154 flog_err(EC_LIB_SNMP, "snmp[err]: %s", msg ? msg : slm->msg);
d62a17ae 155 break;
156 case LOG_WARNING:
450971aa 157 flog_warn(EC_LIB_SNMP, "snmp[warning]: %s",
ade6974d 158 msg ? msg : slm->msg);
d62a17ae 159 break;
160 case LOG_NOTICE:
161 zlog_notice("snmp[notice]: %s", msg ? msg : slm->msg);
162 break;
163 case LOG_INFO:
164 zlog_info("snmp[info]: %s", msg ? msg : slm->msg);
165 break;
166 case LOG_DEBUG:
167 zlog_debug("snmp[debug]: %s", msg ? msg : slm->msg);
168 break;
169 }
170 XFREE(MTYPE_TMP, msg);
171 return SNMP_ERR_NOERROR;
d6be5fb9
VB
172}
173
d62a17ae 174static int config_write_agentx(struct vty *vty)
d6be5fb9 175{
d62a17ae 176 if (agentx_enabled)
177 vty_out(vty, "agentx\n");
178 return 1;
d6be5fb9
VB
179}
180
181DEFUN (agentx_enable,
182 agentx_enable_cmd,
183 "agentx",
007b0667 184 "SNMP AgentX protocol settings\n")
d6be5fb9 185{
d62a17ae 186 if (!agentx_enabled) {
187 init_snmp(FRR_SMUX_NAME);
188 events = list_new();
189 agentx_events_update();
190 agentx_enabled = 1;
d62a17ae 191 }
5fedee18 192
d62a17ae 193 return CMD_SUCCESS;
d6be5fb9
VB
194}
195
196DEFUN (no_agentx,
197 no_agentx_cmd,
198 "no agentx",
199 NO_STR
007b0667 200 "SNMP AgentX protocol settings\n")
d6be5fb9 201{
d62a17ae 202 if (!agentx_enabled)
203 return CMD_SUCCESS;
204 vty_out(vty, "SNMP AgentX support cannot be disabled once enabled\n");
205 return CMD_WARNING_CONFIG_FAILED;
d6be5fb9
VB
206}
207
d62a17ae 208void smux_init(struct thread_master *tm)
d6be5fb9 209{
d62a17ae 210 agentx_tm = tm;
211
212 netsnmp_enable_subagent();
213 snmp_disable_log();
214 snmp_enable_calllog();
215 snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_LOGGING,
216 agentx_log_callback, NULL);
217 init_agent(FRR_SMUX_NAME);
218
219 install_node(&agentx_node, config_write_agentx);
220 install_element(CONFIG_NODE, &agentx_enable_cmd);
221 install_element(CONFIG_NODE, &no_agentx_cmd);
d6be5fb9
VB
222}
223
d62a17ae 224void smux_register_mib(const char *descr, struct variable *var, size_t width,
225 int num, oid name[], size_t namelen)
d6be5fb9 226{
d62a17ae 227 register_mib(descr, var, width, num, name, namelen);
d6be5fb9
VB
228}
229
d62a17ae 230int smux_trap(struct variable *vp, size_t vp_len, const oid *ename,
231 size_t enamelen, const oid *name, size_t namelen,
232 const oid *iname, size_t inamelen,
233 const struct trap_object *trapobj, size_t trapobjlen,
d7c0a89a 234 uint8_t sptrap)
d6be5fb9 235{
d62a17ae 236 oid objid_snmptrap[] = {1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0};
237 size_t objid_snmptrap_len = sizeof objid_snmptrap / sizeof(oid);
238 oid notification_oid[MAX_OID_LEN];
239 size_t notification_oid_len;
240 unsigned int i;
241
242 netsnmp_variable_list *notification_vars = NULL;
243 if (!agentx_enabled)
244 return 0;
245
246 /* snmpTrapOID */
247 oid_copy(notification_oid, ename, enamelen);
248 notification_oid[enamelen] = sptrap;
249 notification_oid_len = enamelen + 1;
250 snmp_varlist_add_variable(&notification_vars, objid_snmptrap,
251 objid_snmptrap_len, ASN_OBJECT_ID,
d7c0a89a 252 (uint8_t *)notification_oid,
d62a17ae 253 notification_oid_len * sizeof(oid));
254
255 /* Provided bindings */
256 for (i = 0; i < trapobjlen; i++) {
257 unsigned int j;
258 oid oid[MAX_OID_LEN];
259 size_t oid_len, onamelen;
d7c0a89a 260 uint8_t *val;
d62a17ae 261 size_t val_len;
262 WriteMethod *wm = NULL;
263 struct variable cvp;
264
265 /* Make OID. */
266 if (trapobj[i].namelen > 0) {
267 /* Columnar object */
268 onamelen = trapobj[i].namelen;
269 oid_copy(oid, name, namelen);
270 oid_copy(oid + namelen, trapobj[i].name, onamelen);
271 oid_copy(oid + namelen + onamelen, iname, inamelen);
272 oid_len = namelen + onamelen + inamelen;
273 } else {
274 /* Scalar object */
275 onamelen = trapobj[i].namelen * (-1);
276 oid_copy(oid, name, namelen);
277 oid_copy(oid + namelen, trapobj[i].name, onamelen);
278 oid[onamelen + namelen] = 0;
279 oid_len = namelen + onamelen + 1;
280 }
281
282 /* Locate the appropriate function and type in the MIB registry.
283 */
284 for (j = 0; j < vp_len; j++) {
285 if (oid_compare(trapobj[i].name, onamelen, vp[j].name,
286 vp[j].namelen)
287 != 0)
288 continue;
289 /* We found the appropriate variable in the MIB
290 * registry. */
291 oid_copy(cvp.name, name, namelen);
292 oid_copy(cvp.name + namelen, vp[j].name, vp[j].namelen);
293 cvp.namelen = namelen + vp[j].namelen;
294 cvp.type = vp[j].type;
295 cvp.magic = vp[j].magic;
296 cvp.acl = vp[j].acl;
297 cvp.findVar = vp[j].findVar;
298 /* Grab the result. */
299 val = cvp.findVar(&cvp, oid, &oid_len, 1, &val_len,
300 &wm);
301 if (!val)
302 break;
303 snmp_varlist_add_variable(&notification_vars, oid,
304 oid_len, vp[j].type, val,
305 val_len);
306 break;
307 }
b7c0d065 308 }
b7c0d065
VB
309
310
d62a17ae 311 send_v2trap(notification_vars);
312 snmp_free_varbind(notification_vars);
313 agentx_events_update();
314 return 1;
d6be5fb9
VB
315}
316
5986b66b 317#endif /* SNMP_AGENTX */