]> git.proxmox.com Git - mirror_frr.git/blob - lib/agentx.c
Merge branch 'warnings'
[mirror_frr.git] / lib / agentx.c
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 *
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
19 */
20
21 #include <zebra.h>
22
23 #ifdef SNMP_AGENTX
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>
28
29 #include "command.h"
30 #include "smux.h"
31 #include "memory.h"
32 #include "linklist.h"
33 #include "version.h"
34 #include "lib_errors.h"
35
36 static int agentx_enabled = 0;
37
38 static struct thread_master *agentx_tm;
39 static struct thread *timeout_thr = NULL;
40 static struct list *events = NULL;
41
42 static void agentx_events_update(void);
43
44 static int agentx_timeout(struct thread *t)
45 {
46 timeout_thr = NULL;
47
48 snmp_timeout();
49 run_alarms();
50 netsnmp_check_outstanding_agent_requests();
51 agentx_events_update();
52 return 0;
53 }
54
55 static int agentx_read(struct thread *t)
56 {
57 fd_set fds;
58 struct listnode *ln = THREAD_ARG(t);
59 list_delete_node(events, ln);
60
61 FD_ZERO(&fds);
62 FD_SET(THREAD_FD(t), &fds);
63 snmp_read(&fds);
64
65 netsnmp_check_outstanding_agent_requests();
66 agentx_events_update();
67 return 0;
68 }
69
70 static void agentx_events_update(void)
71 {
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 }
128 }
129
130 /* AgentX node. */
131 static struct cmd_node agentx_node = {SMUX_NODE,
132 "", /* AgentX has no interface. */
133 1};
134
135 /* Logging NetSNMP messages */
136 static int agentx_log_callback(int major, int minor, void *serverarg,
137 void *clientarg)
138 {
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:
145 flog_err(LIB_ERR_SNMP,
146 "snmp[emerg]: %s", msg ? msg : slm->msg);
147 break;
148 case LOG_ALERT:
149 flog_err(LIB_ERR_SNMP,
150 "snmp[alert]: %s", msg ? msg : slm->msg);
151 break;
152 case LOG_CRIT:
153 flog_err(LIB_ERR_SNMP,
154 "snmp[crit]: %s", msg ? msg : slm->msg);
155 break;
156 case LOG_ERR:
157 flog_err(LIB_ERR_SNMP,
158 "snmp[err]: %s", msg ? msg : slm->msg);
159 break;
160 case LOG_WARNING:
161 flog_warn(LIB_WARN_SNMP, "snmp[warning]: %s",
162 msg ? msg : slm->msg);
163 break;
164 case LOG_NOTICE:
165 zlog_notice("snmp[notice]: %s", msg ? msg : slm->msg);
166 break;
167 case LOG_INFO:
168 zlog_info("snmp[info]: %s", msg ? msg : slm->msg);
169 break;
170 case LOG_DEBUG:
171 zlog_debug("snmp[debug]: %s", msg ? msg : slm->msg);
172 break;
173 }
174 XFREE(MTYPE_TMP, msg);
175 return SNMP_ERR_NOERROR;
176 }
177
178 static int config_write_agentx(struct vty *vty)
179 {
180 if (agentx_enabled)
181 vty_out(vty, "agentx\n");
182 return 1;
183 }
184
185 DEFUN (agentx_enable,
186 agentx_enable_cmd,
187 "agentx",
188 "SNMP AgentX protocol settings\n")
189 {
190 if (!agentx_enabled) {
191 init_snmp(FRR_SMUX_NAME);
192 events = list_new();
193 agentx_events_update();
194 agentx_enabled = 1;
195 return CMD_SUCCESS;
196 }
197 vty_out(vty, "SNMP AgentX already enabled\n");
198 return CMD_SUCCESS;
199 }
200
201 DEFUN (no_agentx,
202 no_agentx_cmd,
203 "no agentx",
204 NO_STR
205 "SNMP AgentX protocol settings\n")
206 {
207 if (!agentx_enabled)
208 return CMD_SUCCESS;
209 vty_out(vty, "SNMP AgentX support cannot be disabled once enabled\n");
210 return CMD_WARNING_CONFIG_FAILED;
211 }
212
213 void smux_init(struct thread_master *tm)
214 {
215 agentx_tm = tm;
216
217 netsnmp_enable_subagent();
218 snmp_disable_log();
219 snmp_enable_calllog();
220 snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_LOGGING,
221 agentx_log_callback, NULL);
222 init_agent(FRR_SMUX_NAME);
223
224 install_node(&agentx_node, config_write_agentx);
225 install_element(CONFIG_NODE, &agentx_enable_cmd);
226 install_element(CONFIG_NODE, &no_agentx_cmd);
227 }
228
229 void smux_register_mib(const char *descr, struct variable *var, size_t width,
230 int num, oid name[], size_t namelen)
231 {
232 register_mib(descr, var, width, num, name, namelen);
233 }
234
235 int smux_trap(struct variable *vp, size_t vp_len, const oid *ename,
236 size_t enamelen, const oid *name, size_t namelen,
237 const oid *iname, size_t inamelen,
238 const struct trap_object *trapobj, size_t trapobjlen,
239 uint8_t sptrap)
240 {
241 oid objid_snmptrap[] = {1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0};
242 size_t objid_snmptrap_len = sizeof objid_snmptrap / sizeof(oid);
243 oid notification_oid[MAX_OID_LEN];
244 size_t notification_oid_len;
245 unsigned int i;
246
247 netsnmp_variable_list *notification_vars = NULL;
248 if (!agentx_enabled)
249 return 0;
250
251 /* snmpTrapOID */
252 oid_copy(notification_oid, ename, enamelen);
253 notification_oid[enamelen] = sptrap;
254 notification_oid_len = enamelen + 1;
255 snmp_varlist_add_variable(&notification_vars, objid_snmptrap,
256 objid_snmptrap_len, ASN_OBJECT_ID,
257 (uint8_t *)notification_oid,
258 notification_oid_len * sizeof(oid));
259
260 /* Provided bindings */
261 for (i = 0; i < trapobjlen; i++) {
262 unsigned int j;
263 oid oid[MAX_OID_LEN];
264 size_t oid_len, onamelen;
265 uint8_t *val;
266 size_t val_len;
267 WriteMethod *wm = NULL;
268 struct variable cvp;
269
270 /* Make OID. */
271 if (trapobj[i].namelen > 0) {
272 /* Columnar object */
273 onamelen = trapobj[i].namelen;
274 oid_copy(oid, name, namelen);
275 oid_copy(oid + namelen, trapobj[i].name, onamelen);
276 oid_copy(oid + namelen + onamelen, iname, inamelen);
277 oid_len = namelen + onamelen + inamelen;
278 } else {
279 /* Scalar object */
280 onamelen = trapobj[i].namelen * (-1);
281 oid_copy(oid, name, namelen);
282 oid_copy(oid + namelen, trapobj[i].name, onamelen);
283 oid[onamelen + namelen] = 0;
284 oid_len = namelen + onamelen + 1;
285 }
286
287 /* Locate the appropriate function and type in the MIB registry.
288 */
289 for (j = 0; j < vp_len; j++) {
290 if (oid_compare(trapobj[i].name, onamelen, vp[j].name,
291 vp[j].namelen)
292 != 0)
293 continue;
294 /* We found the appropriate variable in the MIB
295 * registry. */
296 oid_copy(cvp.name, name, namelen);
297 oid_copy(cvp.name + namelen, vp[j].name, vp[j].namelen);
298 cvp.namelen = namelen + vp[j].namelen;
299 cvp.type = vp[j].type;
300 cvp.magic = vp[j].magic;
301 cvp.acl = vp[j].acl;
302 cvp.findVar = vp[j].findVar;
303 /* Grab the result. */
304 val = cvp.findVar(&cvp, oid, &oid_len, 1, &val_len,
305 &wm);
306 if (!val)
307 break;
308 snmp_varlist_add_variable(&notification_vars, oid,
309 oid_len, vp[j].type, val,
310 val_len);
311 break;
312 }
313 }
314
315
316 send_v2trap(notification_vars);
317 snmp_free_varbind(notification_vars);
318 agentx_events_update();
319 return 1;
320 }
321
322 #endif /* SNMP_AGENTX */