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