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>
34 #include "lib_errors.h"
36 static int agentx_enabled
= 0;
38 static struct thread_master
*agentx_tm
;
39 static struct thread
*timeout_thr
= NULL
;
40 static struct list
*events
= NULL
;
42 static void agentx_events_update(void);
44 static int agentx_timeout(struct thread
*t
)
50 netsnmp_check_outstanding_agent_requests();
51 agentx_events_update();
55 static int agentx_read(struct thread
*t
)
58 int flags
, new_flags
= 0;
60 struct listnode
*ln
= THREAD_ARG(t
);
61 list_delete_node(events
, ln
);
63 /* fix for non blocking socket */
64 flags
= fcntl(THREAD_FD(t
), F_GETFL
, 0);
66 flog_err(EC_LIB_SYSTEM_CALL
, "Failed to get FD settings fcntl: %s(%d)",
67 strerror(errno
), errno
);
71 if (flags
& O_NONBLOCK
)
74 new_flags
= fcntl(THREAD_FD(t
), F_SETFL
, flags
| O_NONBLOCK
);
77 flog_err(EC_LIB_SYSTEM_CALL
, "Failed to set snmp fd non blocking: %s(%d)",
78 strerror(errno
), errno
);
81 FD_SET(THREAD_FD(t
), &fds
);
86 new_flags
= fcntl(THREAD_FD(t
), F_SETFL
, flags
);
91 "Failed to set snmp fd back to original settings: %s(%d)",
92 strerror(errno
), errno
);
95 netsnmp_check_outstanding_agent_requests();
96 agentx_events_update();
100 static void agentx_events_update(void)
104 struct timeval timeout
= {.tv_sec
= 0, .tv_usec
= 0};
110 THREAD_OFF(timeout_thr
);
113 snmp_select_info(&maxfd
, &fds
, &timeout
, &block
);
117 thread_add_timer_tv(agentx_tm
, agentx_timeout
, NULL
, &timeout
,
121 ln
= listhead(events
);
122 thr
= ln
? listgetdata(ln
) : NULL
;
123 thr_fd
= thr
? THREAD_FD(thr
) : -1;
125 /* "two-pointer" / two-list simultaneous iteration
126 * ln/thr/thr_fd point to the next existing event listener to hit while
127 * fd counts to catch up */
128 for (fd
= 0; fd
< maxfd
; fd
++) {
131 struct listnode
*nextln
= listnextnode(ln
);
132 if (!FD_ISSET(fd
, &fds
)) {
134 list_delete_node(events
, ln
);
137 thr
= ln
? listgetdata(ln
) : NULL
;
138 thr_fd
= thr
? THREAD_FD(thr
) : -1;
140 /* need listener, but haven't hit one where it would be */
141 else if (FD_ISSET(fd
, &fds
)) {
142 struct listnode
*newln
;
144 thread_add_read(agentx_tm
, agentx_read
, NULL
, fd
, &thr
);
145 newln
= listnode_add_before(events
, ln
, thr
);
150 /* leftover event listeners at this point have fd > maxfd, delete them
153 struct listnode
*nextln
= listnextnode(ln
);
154 thread_cancel(listgetdata(ln
));
155 list_delete_node(events
, ln
);
161 static struct cmd_node agentx_node
= {SMUX_NODE
,
162 "", /* AgentX has no interface. */
165 /* Logging NetSNMP messages */
166 static int agentx_log_callback(int major
, int minor
, void *serverarg
,
169 struct snmp_log_message
*slm
= (struct snmp_log_message
*)serverarg
;
170 char *msg
= XSTRDUP(MTYPE_TMP
, slm
->msg
);
172 msg
[strlen(msg
) - 1] = '\0';
173 switch (slm
->priority
) {
175 flog_err(EC_LIB_SNMP
, "snmp[emerg]: %s", msg
? msg
: slm
->msg
);
178 flog_err(EC_LIB_SNMP
, "snmp[alert]: %s", msg
? msg
: slm
->msg
);
181 flog_err(EC_LIB_SNMP
, "snmp[crit]: %s", msg
? msg
: slm
->msg
);
184 flog_err(EC_LIB_SNMP
, "snmp[err]: %s", msg
? msg
: slm
->msg
);
187 flog_warn(EC_LIB_SNMP
, "snmp[warning]: %s",
188 msg
? msg
: slm
->msg
);
191 zlog_notice("snmp[notice]: %s", msg
? msg
: slm
->msg
);
194 zlog_info("snmp[info]: %s", msg
? msg
: slm
->msg
);
197 zlog_debug("snmp[debug]: %s", msg
? msg
: slm
->msg
);
200 XFREE(MTYPE_TMP
, msg
);
201 return SNMP_ERR_NOERROR
;
204 static int config_write_agentx(struct vty
*vty
)
207 vty_out(vty
, "agentx\n");
211 DEFUN (agentx_enable
,
214 "SNMP AgentX protocol settings\n")
216 if (!agentx_enabled
) {
217 init_snmp(FRR_SMUX_NAME
);
219 agentx_events_update();
230 "SNMP AgentX protocol settings\n")
234 vty_out(vty
, "SNMP AgentX support cannot be disabled once enabled\n");
235 return CMD_WARNING_CONFIG_FAILED
;
238 void smux_init(struct thread_master
*tm
)
242 netsnmp_enable_subagent();
244 snmp_enable_calllog();
245 snmp_register_callback(SNMP_CALLBACK_LIBRARY
, SNMP_CALLBACK_LOGGING
,
246 agentx_log_callback
, NULL
);
247 init_agent(FRR_SMUX_NAME
);
249 install_node(&agentx_node
, config_write_agentx
);
250 install_element(CONFIG_NODE
, &agentx_enable_cmd
);
251 install_element(CONFIG_NODE
, &no_agentx_cmd
);
254 void smux_register_mib(const char *descr
, struct variable
*var
, size_t width
,
255 int num
, oid name
[], size_t namelen
)
257 register_mib(descr
, var
, width
, num
, name
, namelen
);
260 int smux_trap(struct variable
*vp
, size_t vp_len
, const oid
*ename
,
261 size_t enamelen
, const oid
*name
, size_t namelen
,
262 const oid
*iname
, size_t inamelen
,
263 const struct trap_object
*trapobj
, size_t trapobjlen
,
266 oid objid_snmptrap
[] = {1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0};
267 size_t objid_snmptrap_len
= sizeof objid_snmptrap
/ sizeof(oid
);
268 oid notification_oid
[MAX_OID_LEN
];
269 size_t notification_oid_len
;
272 netsnmp_variable_list
*notification_vars
= NULL
;
277 oid_copy(notification_oid
, ename
, enamelen
);
278 notification_oid
[enamelen
] = sptrap
;
279 notification_oid_len
= enamelen
+ 1;
280 snmp_varlist_add_variable(¬ification_vars
, objid_snmptrap
,
281 objid_snmptrap_len
, ASN_OBJECT_ID
,
282 (uint8_t *)notification_oid
,
283 notification_oid_len
* sizeof(oid
));
285 /* Provided bindings */
286 for (i
= 0; i
< trapobjlen
; i
++) {
288 oid oid
[MAX_OID_LEN
];
289 size_t oid_len
, onamelen
;
292 WriteMethod
*wm
= NULL
;
296 if (trapobj
[i
].namelen
> 0) {
297 /* Columnar object */
298 onamelen
= trapobj
[i
].namelen
;
299 oid_copy(oid
, name
, namelen
);
300 oid_copy(oid
+ namelen
, trapobj
[i
].name
, onamelen
);
301 oid_copy(oid
+ namelen
+ onamelen
, iname
, inamelen
);
302 oid_len
= namelen
+ onamelen
+ inamelen
;
305 onamelen
= trapobj
[i
].namelen
* (-1);
306 oid_copy(oid
, name
, namelen
);
307 oid_copy(oid
+ namelen
, trapobj
[i
].name
, onamelen
);
308 oid
[onamelen
+ namelen
] = 0;
309 oid_len
= namelen
+ onamelen
+ 1;
312 /* Locate the appropriate function and type in the MIB registry.
314 for (j
= 0; j
< vp_len
; j
++) {
315 if (oid_compare(trapobj
[i
].name
, onamelen
, vp
[j
].name
,
319 /* We found the appropriate variable in the MIB
321 oid_copy(cvp
.name
, name
, namelen
);
322 oid_copy(cvp
.name
+ namelen
, vp
[j
].name
, vp
[j
].namelen
);
323 cvp
.namelen
= namelen
+ vp
[j
].namelen
;
324 cvp
.type
= vp
[j
].type
;
325 cvp
.magic
= vp
[j
].magic
;
327 cvp
.findVar
= vp
[j
].findVar
;
328 /* Grab the result. */
329 val
= cvp
.findVar(&cvp
, oid
, &oid_len
, 1, &val_len
,
333 snmp_varlist_add_variable(¬ification_vars
, oid
,
334 oid_len
, vp
[j
].type
, val
,
341 send_v2trap(notification_vars
);
342 snmp_free_varbind(notification_vars
);
343 agentx_events_update();
347 #endif /* SNMP_AGENTX */