2 * Copyright (C) 2012 Vincent Bernat <bernat@luffy.cx>
4 * This file is part of GNU Zebra.
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
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.
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
24 #include <net-snmp/net-snmp-config.h>
25 #include <net-snmp/net-snmp-includes.h>
26 #include <net-snmp/agent/net-snmp-agent-includes.h>
27 #include <net-snmp/agent/snmp_vars.h>
33 #include "lib/version.h"
34 #include "lib_errors.h"
41 DEFINE_HOOK(agentx_enabled
, (), ());
43 static bool agentx_enabled
= false;
45 static struct thread_master
*agentx_tm
;
46 static struct thread
*timeout_thr
= NULL
;
47 static struct list
*events
= NULL
;
49 static void agentx_events_update(void);
51 static void agentx_timeout(struct thread
*t
)
55 netsnmp_check_outstanding_agent_requests();
56 agentx_events_update();
59 static void agentx_read(struct thread
*t
)
62 int flags
, new_flags
= 0;
64 struct listnode
*ln
= THREAD_ARG(t
);
65 struct thread
**thr
= listgetdata(ln
);
66 XFREE(MTYPE_TMP
, thr
);
67 list_delete_node(events
, ln
);
69 /* fix for non blocking socket */
70 flags
= fcntl(THREAD_FD(t
), F_GETFL
, 0);
72 flog_err(EC_LIB_SYSTEM_CALL
, "Failed to get FD settings fcntl: %s(%d)",
73 strerror(errno
), errno
);
77 if (flags
& O_NONBLOCK
)
80 new_flags
= fcntl(THREAD_FD(t
), F_SETFL
, flags
| O_NONBLOCK
);
83 flog_err(EC_LIB_SYSTEM_CALL
, "Failed to set snmp fd non blocking: %s(%d)",
84 strerror(errno
), errno
);
87 FD_SET(THREAD_FD(t
), &fds
);
92 new_flags
= fcntl(THREAD_FD(t
), F_SETFL
, flags
);
97 "Failed to set snmp fd back to original settings: %s(%d)",
98 strerror(errno
), errno
);
101 netsnmp_check_outstanding_agent_requests();
102 agentx_events_update();
105 static void agentx_events_update(void)
109 struct timeval timeout
= {.tv_sec
= 0, .tv_usec
= 0};
115 thread_cancel(&timeout_thr
);
118 snmp_select_info(&maxfd
, &fds
, &timeout
, &block
);
121 thread_add_timer_tv(agentx_tm
, agentx_timeout
, NULL
, &timeout
,
125 ln
= listhead(events
);
126 thr
= ln
? listgetdata(ln
) : NULL
;
127 thr_fd
= thr
? THREAD_FD(*thr
) : -1;
129 /* "two-pointer" / two-list simultaneous iteration
130 * ln/thr/thr_fd point to the next existing event listener to hit while
131 * fd counts to catch up */
132 for (fd
= 0; fd
< maxfd
; fd
++) {
135 struct listnode
*nextln
= listnextnode(ln
);
136 if (!FD_ISSET(fd
, &fds
)) {
138 XFREE(MTYPE_TMP
, thr
);
139 list_delete_node(events
, ln
);
142 thr
= ln
? listgetdata(ln
) : NULL
;
143 thr_fd
= thr
? THREAD_FD(*thr
) : -1;
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
= XCALLOC(MTYPE_TMP
, sizeof(struct thread
*));
150 newln
= listnode_add_before(events
, ln
, thr
);
151 thread_add_read(agentx_tm
, agentx_read
, newln
, fd
, thr
);
155 /* leftover event listeners at this point have fd > maxfd, delete them
158 struct listnode
*nextln
= listnextnode(ln
);
159 thr
= listgetdata(ln
);
161 XFREE(MTYPE_TMP
, thr
);
162 list_delete_node(events
, ln
);
168 static int config_write_agentx(struct vty
*vty
);
169 static struct cmd_node agentx_node
= {
173 .config_write
= config_write_agentx
,
176 /* Logging NetSNMP messages */
177 static int agentx_log_callback(int major
, int minor
, void *serverarg
,
180 struct snmp_log_message
*slm
= (struct snmp_log_message
*)serverarg
;
181 char *msg
= XSTRDUP(MTYPE_TMP
, slm
->msg
);
183 msg
[strlen(msg
) - 1] = '\0';
184 switch (slm
->priority
) {
186 flog_err(EC_LIB_SNMP
, "snmp[emerg]: %s", msg
? msg
: slm
->msg
);
189 flog_err(EC_LIB_SNMP
, "snmp[alert]: %s", msg
? msg
: slm
->msg
);
192 flog_err(EC_LIB_SNMP
, "snmp[crit]: %s", msg
? msg
: slm
->msg
);
195 flog_err(EC_LIB_SNMP
, "snmp[err]: %s", msg
? msg
: slm
->msg
);
198 flog_warn(EC_LIB_SNMP
, "snmp[warning]: %s",
199 msg
? msg
: slm
->msg
);
202 zlog_notice("snmp[notice]: %s", msg
? msg
: slm
->msg
);
205 zlog_info("snmp[info]: %s", msg
? msg
: slm
->msg
);
208 zlog_debug("snmp[debug]: %s", msg
? msg
: slm
->msg
);
211 XFREE(MTYPE_TMP
, msg
);
212 return SNMP_ERR_NOERROR
;
215 static int config_write_agentx(struct vty
*vty
)
218 vty_out(vty
, "agentx\n");
222 DEFUN (agentx_enable
,
225 "SNMP AgentX protocol settings\n")
227 if (!agentx_enabled
) {
228 init_snmp(FRR_SMUX_NAME
);
230 agentx_events_update();
231 agentx_enabled
= true;
232 hook_call(agentx_enabled
);
242 "SNMP AgentX protocol settings\n")
246 vty_out(vty
, "SNMP AgentX support cannot be disabled once enabled\n");
247 return CMD_WARNING_CONFIG_FAILED
;
250 static int smux_disable(void)
252 agentx_enabled
= false;
257 bool smux_enabled(void)
259 return agentx_enabled
;
262 void smux_init(struct thread_master
*tm
)
266 netsnmp_enable_subagent();
268 snmp_enable_calllog();
269 snmp_register_callback(SNMP_CALLBACK_LIBRARY
, SNMP_CALLBACK_LOGGING
,
270 agentx_log_callback
, NULL
);
271 init_agent(FRR_SMUX_NAME
);
273 install_node(&agentx_node
);
274 install_element(CONFIG_NODE
, &agentx_enable_cmd
);
275 install_element(CONFIG_NODE
, &no_agentx_cmd
);
277 hook_register(frr_early_fini
, smux_disable
);
280 void smux_agentx_enable(void)
282 if (!agentx_enabled
) {
283 init_snmp(FRR_SMUX_NAME
);
285 agentx_events_update();
286 agentx_enabled
= true;
290 void smux_register_mib(const char *descr
, struct variable
*var
, size_t width
,
291 int num
, oid name
[], size_t namelen
)
293 register_mib(descr
, var
, width
, num
, name
, namelen
);
296 void smux_trap(struct variable
*vp
, size_t vp_len
, const oid
*ename
,
297 size_t enamelen
, const oid
*name
, size_t namelen
,
298 const oid
*iname
, size_t inamelen
,
299 const struct trap_object
*trapobj
, size_t trapobjlen
,
302 struct index_oid trap_index
[1];
304 /* copy the single index into the multi-index format */
305 oid_copy(trap_index
[0].indexname
, iname
, inamelen
);
306 trap_index
[0].indexlen
= inamelen
;
308 smux_trap_multi_index(vp
, vp_len
, ename
, enamelen
, name
, namelen
,
309 trap_index
, array_size(trap_index
), trapobj
,
313 int smux_trap_multi_index(struct variable
*vp
, size_t vp_len
, const oid
*ename
,
314 size_t enamelen
, const oid
*name
, size_t namelen
,
315 struct index_oid
*iname
, size_t index_len
,
316 const struct trap_object
*trapobj
, size_t trapobjlen
,
319 oid objid_snmptrap
[] = {1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0};
320 size_t objid_snmptrap_len
= sizeof(objid_snmptrap
) / sizeof(oid
);
321 oid notification_oid
[MAX_OID_LEN
];
322 size_t notification_oid_len
;
325 netsnmp_variable_list
*notification_vars
= NULL
;
330 oid_copy(notification_oid
, ename
, enamelen
);
331 notification_oid
[enamelen
] = sptrap
;
332 notification_oid_len
= enamelen
+ 1;
333 snmp_varlist_add_variable(¬ification_vars
, objid_snmptrap
,
334 objid_snmptrap_len
, ASN_OBJECT_ID
,
335 (uint8_t *)notification_oid
,
336 notification_oid_len
* sizeof(oid
));
338 /* Provided bindings */
339 for (i
= 0; i
< trapobjlen
; i
++) {
341 oid oid
[MAX_OID_LEN
];
342 size_t oid_len
, onamelen
;
345 WriteMethod
*wm
= NULL
;
349 * this allows the behaviour of smux_trap with a singe index
350 * for all objects to be maintained whilst allowing traps which
351 * have different indices per object to be supported
353 iindex
= (index_len
== 1) ? 0 : i
;
356 if (trapobj
[i
].namelen
> 0) {
357 /* Columnar object */
358 onamelen
= trapobj
[i
].namelen
;
359 oid_copy(oid
, name
, namelen
);
360 oid_copy(oid
+ namelen
, trapobj
[i
].name
, onamelen
);
361 oid_copy(oid
+ namelen
+ onamelen
,
362 iname
[iindex
].indexname
,
363 iname
[iindex
].indexlen
);
364 oid_len
= namelen
+ onamelen
+ iname
[iindex
].indexlen
;
367 onamelen
= trapobj
[i
].namelen
* (-1);
368 oid_copy(oid
, name
, namelen
);
369 oid_copy(oid
+ namelen
, trapobj
[i
].name
, onamelen
);
370 oid
[onamelen
+ namelen
] = 0;
371 oid_len
= namelen
+ onamelen
+ 1;
374 /* Locate the appropriate function and type in the MIB registry.
376 for (j
= 0; j
< vp_len
; j
++) {
377 if (oid_compare(trapobj
[i
].name
, onamelen
, vp
[j
].name
,
381 /* We found the appropriate variable in the MIB
383 oid_copy(cvp
.name
, name
, namelen
);
384 oid_copy(cvp
.name
+ namelen
, vp
[j
].name
, vp
[j
].namelen
);
385 cvp
.namelen
= namelen
+ vp
[j
].namelen
;
386 cvp
.type
= vp
[j
].type
;
387 cvp
.magic
= vp
[j
].magic
;
389 cvp
.findVar
= vp
[j
].findVar
;
391 /* Grab the result. */
392 val
= cvp
.findVar(&cvp
, oid
, &oid_len
, 1, &val_len
,
396 snmp_varlist_add_variable(¬ification_vars
, oid
,
397 oid_len
, vp
[j
].type
, val
,
404 send_v2trap(notification_vars
);
405 snmp_free_varbind(notification_vars
);
406 agentx_events_update();
410 void smux_events_update(void)
412 agentx_events_update();
415 #endif /* SNMP_AGENTX */