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