2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Author: Harti Brandt <harti@freebsd.org>
8 * Redistribution of this software and documentation and use in source and
9 * binary forms, with or without modification, are permitted provided that
10 * the following conditions are met:
12 * 1. Redistributions of source code or documentation must retain the above
13 * copyright notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY FRAUNHOFER FOKUS
19 * AND ITS CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 * FRAUNHOFER FOKUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
25 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Netgraph interface for SNMPd.
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/linker.h>
37 #include <sys/socket.h>
38 #include <sys/syslog.h>
39 #include <sys/queue.h>
40 #include <sys/sysctl.h>
47 #include <bsnmp/snmpmod.h>
48 #include "snmp_netgraph.h"
49 #include "netgraph_tree.h"
50 #include "netgraph_oid.h"
52 /* maximum message size */
53 #define RESBUFSIZ 20000
55 /* default node name */
56 #define NODENAME "NgSnmpd"
60 u_char *snmp_nodename;
62 /* the Object Resource registration index */
63 static u_int reg_index;
64 static const struct asn_oid oid_begemotNg = OIDX_begemotNg;
67 /* this must be smaller than int32_t because the functions in libnetgraph
68 * falsely return an int */
69 static size_t resbufsiz = RESBUFSIZ;
70 static u_int timeout = 1000;
71 static u_int debug_level;
73 /* number of microseconds per clock tick */
74 static struct clockinfo clockinfo;
76 /* Csock buffers. Communication on the csock is asynchronuous. This means
77 * if we wait for a specific response, we may get other messages. Put these
78 * into a queue and execute them when we are idle. */
80 STAILQ_ENTRY(csock_buf) link;
82 char path[NG_PATHSIZ];
84 static STAILQ_HEAD(, csock_buf) csock_bufs =
85 STAILQ_HEAD_INITIALIZER(csock_bufs);
88 * We dispatch unsolicieted messages by node cookies and ids.
89 * So we must keep a list of hook names and dispatch functions.
96 const struct lmodule *mod;
97 SLIST_ENTRY(msgreg) link;
99 static SLIST_HEAD(, msgreg) msgreg_list =
100 SLIST_HEAD_INITIALIZER(msgreg_list);
103 * Data messages are dispatched by hook names.
106 char hook[NG_HOOKSIZ];
109 const struct lmodule *mod;
110 SLIST_ENTRY(datareg) link;
112 static SLIST_HEAD(, datareg) datareg_list =
113 SLIST_HEAD_INITIALIZER(datareg_list);
115 /* the netgraph sockets */
116 static int csock, dsock;
117 static void *csock_fd, *dsock_fd;
119 /* our module handle */
120 static struct lmodule *module;
123 static u_int32_t stats[LEAF_begemotNgTooLargeDatas+1];
125 /* netgraph type list */
127 char name[NG_TYPESIZ];
128 struct asn_oid index;
129 TAILQ_ENTRY(ngtype) link;
131 TAILQ_HEAD(ngtype_list, ngtype);
133 static struct ngtype_list ngtype_list;
134 static uint64_t ngtype_tick;
138 * Register a function to receive unsolicited messages
141 ng_register_cookie(const struct lmodule *mod, u_int32_t cookie, ng_ID_t id,
142 ng_cookie_f *func, void *arg)
146 if ((d = malloc(sizeof(*d))) == NULL)
155 SLIST_INSERT_HEAD(&msgreg_list, d, link);
161 * Remove a registration.
164 ng_unregister_cookie(void *dd)
166 struct msgreg *d = dd;
168 SLIST_REMOVE(&msgreg_list, d, msgreg, link);
173 * Register a function for hook data.
176 ng_register_hook(const struct lmodule *mod, const char *hook,
177 ng_hook_f *func, void *arg)
181 if ((d = malloc(sizeof(*d))) == NULL)
184 strcpy(d->hook, hook);
189 SLIST_INSERT_HEAD(&datareg_list, d, link);
195 * Unregister a hook function
198 ng_unregister_hook(void *dd)
200 struct datareg *d = dd;
202 SLIST_REMOVE(&datareg_list, d, datareg, link);
207 * Unregister all hooks and cookies for that module. Note: doesn't disconnect
211 ng_unregister_module(const struct lmodule *mod)
213 struct msgreg *m, *m1;
214 struct datareg *d, *d1;
216 m = SLIST_FIRST(&msgreg_list);
218 m1 = SLIST_NEXT(m, link);
220 SLIST_REMOVE(&msgreg_list, m, msgreg, link);
226 d = SLIST_FIRST(&datareg_list);
228 d1 = SLIST_NEXT(d, link);
230 SLIST_REMOVE(&datareg_list, d, datareg, link);
238 * Dispatch a message to the correct module and delete it. More than one
239 * module can get a message.
242 csock_handle(struct ng_mesg *mesg, const char *path)
244 struct msgreg *d, *d1;
248 if (sscanf(path, "[%x]:%n", &id, &len) != 1 ||
249 (u_int)len != strlen(path)) {
250 syslog(LOG_ERR, "cannot parse message path '%s'", path);
254 d = SLIST_FIRST(&msgreg_list);
256 d1 = SLIST_NEXT(d, link);
257 if (d->cookie == mesg->header.typecookie &&
258 (d->id == 0 || d->id == id || id == 0))
259 (*d->func)(mesg, path, id, d->arg);
266 * Input from the control socket.
268 static struct ng_mesg *
269 csock_read(char *path)
271 struct ng_mesg *mesg;
274 if ((mesg = malloc(resbufsiz + 1)) == NULL) {
275 stats[LEAF_begemotNgNoMems]++;
276 syslog(LOG_CRIT, "out of memory");
280 if ((ret = NgRecvMsg(csock, mesg, resbufsiz + 1, path)) < 0) {
283 if (errno == EWOULDBLOCK) {
287 stats[LEAF_begemotNgMsgReadErrs]++;
288 syslog(LOG_WARNING, "read from csock: %m");
293 syslog(LOG_DEBUG, "node closed -- exiting");
296 if ((size_t)ret > resbufsiz) {
297 stats[LEAF_begemotNgTooLargeMsgs]++;
298 syslog(LOG_WARNING, "ng message too large");
307 csock_input(int fd __unused, void *udata __unused)
309 struct ng_mesg *mesg;
310 char path[NG_PATHSIZ];
312 if ((mesg = csock_read(path)) == NULL)
315 csock_handle(mesg, path);
319 * Write a message to a node.
322 ng_output(const char *path, u_int cookie, u_int opcode,
323 const void *arg, size_t arglen)
325 return (NgSendMsg(csock, path, (int)cookie, (int)opcode, arg, arglen));
328 ng_output_node(const char *node, u_int cookie, u_int opcode,
329 const void *arg, size_t arglen)
331 char path[NG_PATHSIZ];
333 sprintf(path, "%s:", node);
334 return (ng_output(path, cookie, opcode, arg, arglen));
337 ng_output_id(ng_ID_t node, u_int cookie, u_int opcode,
338 const void *arg, size_t arglen)
340 char path[NG_PATHSIZ];
342 sprintf(path, "[%x]:", node);
343 return (ng_output(path, cookie, opcode, arg, arglen));
349 * Execute a synchronuous dialog with the csock. All message we receive, that
350 * do not match our request, are queue until the next call to the IDLE function.
353 ng_dialog(const char *path, u_int cookie, u_int opcode,
354 const void *arg, size_t arglen)
357 struct ng_mesg *mesg;
358 char rpath[NG_PATHSIZ];
360 struct timeval end, tv;
362 if ((token = ng_output(path, cookie, opcode, arg, arglen)) < 0)
366 fd_suspend(csock_fd);
368 gettimeofday(&end, NULL);
369 tv.tv_sec = timeout / 1000;
370 tv.tv_usec = (timeout % 1000) * 1000;
371 timeradd(&end, &tv, &end);
374 gettimeofday(&tv, NULL);
375 if (timercmp(&tv, &end, >=)) {
377 syslog(LOG_WARNING, "no response for request %u/%u",
382 timersub(&end, &tv, &tv);
383 if (tv.tv_sec == 0 && tv.tv_usec < clockinfo.tick)
386 if (setsockopt(csock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1)
387 syslog(LOG_WARNING, "setsockopt(SO_RCVTIMEO): %m");
388 if ((mesg = csock_read(rpath)) == NULL) {
389 if (errno == EWOULDBLOCK)
393 if (mesg->header.token == (u_int)token)
395 if ((b = malloc(sizeof(*b))) == NULL) {
396 stats[LEAF_begemotNgNoMems]++;
397 syslog(LOG_ERR, "out of memory");
402 strcpy(b->path, rpath);
403 STAILQ_INSERT_TAIL(&csock_bufs, b, link);
408 if (setsockopt(csock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1)
409 syslog(LOG_WARNING, "setsockopt(SO_RCVTIMEO,0): %m");
420 ng_dialog_node(const char *node, u_int cookie, u_int opcode,
421 const void *arg, size_t arglen)
423 char path[NG_PATHSIZ];
425 sprintf(path, "%s:", node);
426 return (ng_dialog(path, cookie, opcode, arg, arglen));
429 ng_dialog_id(ng_ID_t id, u_int cookie, u_int opcode,
430 const void *arg, size_t arglen)
432 char path[NG_PATHSIZ];
434 sprintf(path, "[%x]:", id);
435 return (ng_dialog(path, cookie, opcode, arg, arglen));
440 * Send a data message to a given hook.
443 ng_send_data(const char *hook, const void *sndbuf, size_t sndlen)
445 return (NgSendData(dsock, hook, sndbuf, sndlen));
449 * Input from a data socket. Dispatch to the function for that hook.
452 dsock_input(int fd __unused, void *udata __unused)
454 u_char *resbuf, embuf[100];
456 char hook[NG_HOOKSIZ];
457 struct datareg *d, *d1;
459 if ((resbuf = malloc(resbufsiz + 1)) == NULL) {
460 stats[LEAF_begemotNgNoMems]++;
461 syslog(LOG_CRIT, "out of memory");
462 (void)NgRecvData(fd, embuf, sizeof(embuf), hook);
466 if ((len = NgRecvData(fd, resbuf, resbufsiz + 1, hook)) == -1) {
467 stats[LEAF_begemotNgDataReadErrs]++;
468 syslog(LOG_ERR, "reading message: %m");
476 if ((size_t)len == resbufsiz + 1) {
477 stats[LEAF_begemotNgTooLargeDatas]++;
478 syslog(LOG_WARNING, "message too long");
484 * Dispatch message. Maybe dispatched to more than one function.
486 d = SLIST_FIRST(&datareg_list);
488 d1 = SLIST_NEXT(d, link);
489 if (strcmp(hook, d->hook) == 0)
490 (*d->func)(hook, resbuf, len, d->arg);
498 * The SNMP daemon is about to wait for an event. Look whether we have
499 * netgraph messages waiting. If yes, drain the queue.
506 /* execute waiting csock_bufs */
507 while ((b = STAILQ_FIRST(&csock_bufs)) != NULL) {
508 STAILQ_REMOVE_HEAD(&csock_bufs, link);
509 csock_handle(b->mesg, b->path);
515 * Called when the module is loaded. Returning a non-zero value means,
516 * rejecting the initialisation.
518 * We make the netgraph socket.
521 ng_init(struct lmodule *mod, int argc, char *argv[])
529 if ((snmp_nodename = malloc(strlen(NODENAME) + 1)) == NULL)
531 strcpy(snmp_nodename, NODENAME);
533 if ((snmp_nodename = malloc(NG_NODESIZ)) == NULL)
535 strlcpy(snmp_nodename, argv[0], NG_NODESIZ);
538 /* fetch clockinfo (for the number of microseconds per tick) */
540 name[1] = KERN_CLOCKRATE;
541 len = sizeof(clockinfo);
542 if (sysctl(name, 2, &clockinfo, &len, NULL, 0) == -1)
545 TAILQ_INIT(&ngtype_list);
551 * Get the node Id/name/type of a node.
554 ng_node_id(const char *path)
556 struct ng_mesg *resp;
559 if ((resp = ng_dialog(path, NGM_GENERIC_COOKIE, NGM_NODEINFO,
562 id = ((struct nodeinfo *)(void *)resp->data)->id;
567 ng_node_id_node(const char *node)
569 struct ng_mesg *resp;
572 if ((resp = ng_dialog_node(node, NGM_GENERIC_COOKIE, NGM_NODEINFO,
575 id = ((struct nodeinfo *)(void *)resp->data)->id;
580 ng_node_name(ng_ID_t id, char *name)
582 struct ng_mesg *resp;
584 if ((resp = ng_dialog_id(id, NGM_GENERIC_COOKIE, NGM_NODEINFO,
587 strcpy(name, ((struct nodeinfo *)(void *)resp->data)->name);
593 ng_node_type(ng_ID_t id, char *type)
595 struct ng_mesg *resp;
597 if ((resp = ng_dialog_id(id, NGM_GENERIC_COOKIE, NGM_NODEINFO,
600 strcpy(type, ((struct nodeinfo *)(void *)resp->data)->type);
606 * Connect our node to some other node
609 ng_connect_node(const char *node, const char *ourhook, const char *peerhook)
611 struct ngm_connect conn;
613 snprintf(conn.path, NG_PATHSIZ, "%s:", node);
614 strlcpy(conn.ourhook, ourhook, NG_HOOKSIZ);
615 strlcpy(conn.peerhook, peerhook, NG_HOOKSIZ);
616 return (NgSendMsg(csock, ".:",
617 NGM_GENERIC_COOKIE, NGM_CONNECT, &conn, sizeof(conn)));
620 ng_connect_id(ng_ID_t id, const char *ourhook, const char *peerhook)
622 struct ngm_connect conn;
624 snprintf(conn.path, NG_PATHSIZ, "[%x]:", id);
625 strlcpy(conn.ourhook, ourhook, NG_HOOKSIZ);
626 strlcpy(conn.peerhook, peerhook, NG_HOOKSIZ);
627 return (NgSendMsg(csock, ".:",
628 NGM_GENERIC_COOKIE, NGM_CONNECT, &conn, sizeof(conn)));
632 ng_connect2_id(ng_ID_t id, ng_ID_t peer, const char *ourhook,
633 const char *peerhook)
635 struct ngm_connect conn;
636 char path[NG_PATHSIZ];
638 snprintf(path, NG_PATHSIZ, "[%x]:", id);
640 snprintf(conn.path, NG_PATHSIZ, "[%x]:", peer);
641 strlcpy(conn.ourhook, ourhook, NG_HOOKSIZ);
642 strlcpy(conn.peerhook, peerhook, NG_HOOKSIZ);
643 return (NgSendMsg(csock, path,
644 NGM_GENERIC_COOKIE, NGM_CONNECT, &conn, sizeof(conn)));
648 ng_connect2_tee_id(ng_ID_t id, ng_ID_t peer, const char *ourhook,
649 const char *peerhook)
651 struct ngm_connect conn;
652 char path[NG_PATHSIZ];
655 if ((tee = ng_mkpeer_id(id, NULL, "tee", ourhook, "left")) == 0)
658 snprintf(path, NG_PATHSIZ, "[%x]:", tee);
660 snprintf(conn.path, NG_PATHSIZ, "[%x]:", peer);
661 strlcpy(conn.ourhook, "right", NG_HOOKSIZ);
662 strlcpy(conn.peerhook, peerhook, NG_HOOKSIZ);
663 return (NgSendMsg(csock, path,
664 NGM_GENERIC_COOKIE, NGM_CONNECT, &conn, sizeof(conn)));
668 * Ensure that a node of type 'type' is connected to 'hook' of 'node'
669 * and return its node id. tee nodes between node and the target node
670 * are skipped. If the type is wrong, or the hook is a dead-end return 0.
671 * If type is NULL, it is not checked.
674 ng_next_node_id_internal(ng_ID_t node, const char *type, const char *hook,
677 struct ng_mesg *resp;
678 struct hooklist *hooklist;
681 if ((resp = ng_dialog_id(node, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
683 syslog(LOG_ERR, "get hook list: %m");
686 hooklist = (struct hooklist *)(void *)resp->data;
688 for (i = 0; i < hooklist->nodeinfo.hooks; i++)
689 if (strcmp(hooklist->link[i].ourhook, hook) == 0)
692 if (i == hooklist->nodeinfo.hooks) {
697 node = hooklist->link[i].nodeinfo.id;
699 if (skip_tee && strcmp(hooklist->link[i].nodeinfo.type, "tee") == 0) {
700 if (strcmp(hooklist->link[i].peerhook, "left") == 0)
701 node = ng_next_node_id(node, type, "right");
702 else if (strcmp(hooklist->link[i].peerhook, "right") == 0)
703 node = ng_next_node_id(node, type, "left");
704 else if (type != NULL &&
705 strcmp(hooklist->link[i].nodeinfo.type, type) != 0)
708 } else if (type != NULL &&
709 strcmp(hooklist->link[i].nodeinfo.type, type) != 0)
718 * Ensure that a node of type 'type' is connected to 'hook' of 'node'
719 * and return its node id. tee nodes between node and the target node
720 * are skipped. If the type is wrong, or the hook is a dead-end return 0.
721 * If type is NULL, it is not checked.
724 ng_next_node_id(ng_ID_t node, const char *type, const char *hook)
726 return (ng_next_node_id_internal(node, type, hook, 1));
730 ng_mkpeer_id(ng_ID_t id, const char *nodename, const char *type,
731 const char *hook, const char *peerhook)
733 char path[NG_PATHSIZ];
734 struct ngm_mkpeer mkpeer;
735 struct ngm_name name;
737 strlcpy(mkpeer.type, type, NG_TYPESIZ);
738 strlcpy(mkpeer.ourhook, hook, NG_HOOKSIZ);
739 strlcpy(mkpeer.peerhook, peerhook, NG_HOOKSIZ);
741 sprintf(path, "[%x]:", id);
742 if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, NGM_MKPEER,
743 &mkpeer, sizeof(mkpeer)) == -1)
746 if ((id = ng_next_node_id_internal(id, NULL, hook, 0)) == 0)
749 if (nodename != NULL) {
750 strcpy(name.name, nodename);
751 sprintf(path, "[%x]:", id);
752 if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, NGM_NAME,
753 &name, sizeof(name)) == -1)
763 ng_shutdown_id(ng_ID_t id)
765 char path[NG_PATHSIZ];
767 snprintf(path, NG_PATHSIZ, "[%x]:", id);
768 return (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
769 NGM_SHUTDOWN, NULL, 0));
773 * Disconnect one of our hooks
776 ng_rmhook(const char *ourhook)
778 struct ngm_rmhook rmhook;
780 strlcpy(rmhook.ourhook, ourhook, NG_HOOKSIZ);
781 return (NgSendMsg(csock, ".:",
782 NGM_GENERIC_COOKIE, NGM_RMHOOK, &rmhook, sizeof(rmhook)));
786 * Disconnect a hook of a node
789 ng_rmhook_id(ng_ID_t id, const char *hook)
791 struct ngm_rmhook rmhook;
792 char path[NG_PATHSIZ];
794 strlcpy(rmhook.ourhook, hook, NG_HOOKSIZ);
795 snprintf(path, NG_PATHSIZ, "[%x]:", id);
796 return (NgSendMsg(csock, path,
797 NGM_GENERIC_COOKIE, NGM_RMHOOK, &rmhook, sizeof(rmhook)));
801 * Disconnect a hook and shutdown all tee nodes that were connected to that
805 ng_rmhook_tee_id(ng_ID_t node, const char *hook)
807 struct ng_mesg *resp;
808 struct hooklist *hooklist;
812 const char *next_hook;
815 /* if we have just shutdown a tee node, which had no other hooks
816 * connected, the node id may already be wrong here. */
817 if ((resp = ng_dialog_id(node, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
821 hooklist = (struct hooklist *)(void *)resp->data;
823 for (i = 0; i < hooklist->nodeinfo.hooks; i++)
824 if (strcmp(hooklist->link[i].ourhook, hook) == 0)
827 if (i == hooklist->nodeinfo.hooks) {
834 if (strcmp(hooklist->link[i].nodeinfo.type, "tee") == 0) {
835 if (strcmp(hooklist->link[i].peerhook, "left") == 0) {
836 next_node = hooklist->link[i].nodeinfo.id;
838 } else if (strcmp(hooklist->link[i].peerhook, "right") == 0) {
839 next_node = hooklist->link[i].nodeinfo.id;
846 ng_rmhook_id(node, hook);
849 ng_shutdown_id(node);
851 if ((node = next_node) == 0)
859 * Get the peer hook of a hook on a given node. Skip any tee nodes in between
862 ng_peer_hook_id(ng_ID_t node, const char *hook, char *peerhook)
864 struct ng_mesg *resp;
865 struct hooklist *hooklist;
869 if ((resp = ng_dialog_id(node, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
871 syslog(LOG_ERR, "get hook list: %m");
874 hooklist = (struct hooklist *)(void *)resp->data;
876 for (i = 0; i < hooklist->nodeinfo.hooks; i++)
877 if (strcmp(hooklist->link[i].ourhook, hook) == 0)
880 if (i == hooklist->nodeinfo.hooks) {
885 node = hooklist->link[i].nodeinfo.id;
888 if (strcmp(hooklist->link[i].nodeinfo.type, "tee") == 0) {
889 if (strcmp(hooklist->link[i].peerhook, "left") == 0)
890 ret = ng_peer_hook_id(node, "right", peerhook);
891 else if (strcmp(hooklist->link[i].peerhook, "right") == 0)
892 ret = ng_peer_hook_id(node, "left", peerhook);
894 strcpy(peerhook, hooklist->link[i].peerhook);
897 strcpy(peerhook, hooklist->link[i].peerhook);
906 * Now the module is started. Select on the sockets, so that we can get
912 if (snmp_node == 0) {
913 if (NgMkSockNode(snmp_nodename, &csock, &dsock) < 0) {
914 syslog(LOG_ERR, "NgMkSockNode: %m");
917 snmp_node = ng_node_id(".:");
920 if ((csock_fd = fd_select(csock, csock_input, NULL, module)) == NULL) {
921 syslog(LOG_ERR, "fd_select failed on csock: %m");
924 if ((dsock_fd = fd_select(dsock, dsock_input, NULL, module)) == NULL) {
925 syslog(LOG_ERR, "fd_select failed on dsock: %m");
929 reg_index = or_register(&oid_begemotNg,
930 "The MIB for the NetGraph access module for SNMP.", module);
934 * Called, when the module is to be unloaded after it was successfully loaded
941 while ((t = TAILQ_FIRST(&ngtype_list)) != NULL) {
942 TAILQ_REMOVE(&ngtype_list, t, link);
946 if (csock_fd != NULL)
947 fd_deselect(csock_fd);
950 if (dsock_fd != NULL)
951 fd_deselect(dsock_fd);
956 or_unregister(reg_index);
961 const struct snmp_module config = {
962 "This module implements access to the netgraph sub-system",
976 op_ng_config(struct snmp_context *ctx, struct snmp_value *value,
977 u_int sub, u_int iidx __unused, enum snmp_op op)
979 asn_subid_t which = value->var.subs[sub - 1];
984 case SNMP_OP_GETNEXT:
989 * Come here for GET, GETNEXT and COMMIT
993 case LEAF_begemotNgControlNodeName:
994 return (string_get(value, snmp_nodename, -1));
996 case LEAF_begemotNgResBufSiz:
997 value->v.integer = resbufsiz;
1000 case LEAF_begemotNgTimeout:
1001 value->v.integer = timeout;
1004 case LEAF_begemotNgDebugLevel:
1005 value->v.uint32 = debug_level;
1011 return (SNMP_ERR_NOERROR);
1016 case LEAF_begemotNgControlNodeName:
1017 /* only at initialisation */
1018 if (community != COMM_INITIALIZE)
1019 return (SNMP_ERR_NOT_WRITEABLE);
1022 return (SNMP_ERR_NOT_WRITEABLE);
1024 if ((ret = string_save(value, ctx, -1, &snmp_nodename))
1025 != SNMP_ERR_NOERROR)
1028 if (NgMkSockNode(snmp_nodename, &csock, &dsock) < 0) {
1029 syslog(LOG_ERR, "NgMkSockNode: %m");
1030 string_rollback(ctx, &snmp_nodename);
1031 return (SNMP_ERR_GENERR);
1033 snmp_node = ng_node_id(".:");
1035 return (SNMP_ERR_NOERROR);
1037 case LEAF_begemotNgResBufSiz:
1038 ctx->scratch->int1 = resbufsiz;
1039 if (value->v.integer < 1024 ||
1040 value->v.integer > 0x10000)
1041 return (SNMP_ERR_WRONG_VALUE);
1042 resbufsiz = value->v.integer;
1043 return (SNMP_ERR_NOERROR);
1045 case LEAF_begemotNgTimeout:
1046 ctx->scratch->int1 = timeout;
1047 if (value->v.integer < 10 ||
1048 value->v.integer > 10000)
1049 return (SNMP_ERR_WRONG_VALUE);
1050 timeout = value->v.integer;
1051 return (SNMP_ERR_NOERROR);
1053 case LEAF_begemotNgDebugLevel:
1054 ctx->scratch->int1 = debug_level;
1055 debug_level = value->v.uint32;
1056 NgSetDebug(debug_level);
1057 return (SNMP_ERR_NOERROR);
1061 case SNMP_OP_ROLLBACK:
1064 case LEAF_begemotNgControlNodeName:
1065 string_rollback(ctx, &snmp_nodename);
1069 return (SNMP_ERR_NOERROR);
1071 case LEAF_begemotNgResBufSiz:
1072 resbufsiz = ctx->scratch->int1;
1073 return (SNMP_ERR_NOERROR);
1075 case LEAF_begemotNgTimeout:
1076 timeout = ctx->scratch->int1;
1077 return (SNMP_ERR_NOERROR);
1079 case LEAF_begemotNgDebugLevel:
1080 debug_level = ctx->scratch->int1;
1081 NgSetDebug(debug_level);
1082 return (SNMP_ERR_NOERROR);
1086 case SNMP_OP_COMMIT:
1089 case LEAF_begemotNgControlNodeName:
1091 return (SNMP_ERR_NOERROR);
1093 case LEAF_begemotNgResBufSiz:
1094 case LEAF_begemotNgTimeout:
1095 case LEAF_begemotNgDebugLevel:
1096 return (SNMP_ERR_NOERROR);
1104 op_ng_stats(struct snmp_context *ctx __unused, struct snmp_value *value,
1105 u_int sub, u_int iidx __unused, enum snmp_op op)
1109 case SNMP_OP_GETNEXT:
1113 value->v.uint32 = stats[value->var.subs[sub - 1] - 1];
1114 return (SNMP_ERR_NOERROR);
1117 return (SNMP_ERR_NOT_WRITEABLE);
1119 case SNMP_OP_ROLLBACK:
1120 case SNMP_OP_COMMIT:
1127 * Netgraph type table
1133 struct typelist *typelist;
1134 struct ng_mesg *resp;
1137 if (this_tick <= ngtype_tick)
1140 while ((t = TAILQ_FIRST(&ngtype_list)) != NULL) {
1141 TAILQ_REMOVE(&ngtype_list, t, link);
1145 if ((resp = ng_dialog_id(snmp_node, NGM_GENERIC_COOKIE,
1146 NGM_LISTTYPES, NULL, 0)) == NULL)
1147 return (SNMP_ERR_GENERR);
1148 typelist = (struct typelist *)(void *)resp->data;
1150 for (u = 0; u < typelist->numtypes; u++) {
1151 if ((t = malloc(sizeof(*t))) == NULL) {
1153 return (SNMP_ERR_GENERR);
1155 strcpy(t->name, typelist->typeinfo[u].type_name);
1156 t->index.subs[0] = strlen(t->name);
1157 t->index.len = t->index.subs[0] + 1;
1158 for (i = 0; i < t->index.subs[0]; i++)
1159 t->index.subs[i + 1] = t->name[i];
1161 INSERT_OBJECT_OID(t, &ngtype_list);
1164 ngtype_tick = this_tick;
1171 * Try to load the netgraph type with the given name. We assume, that
1172 * type 'type' is implemented in the kernel module 'ng_type'.
1175 ngtype_load(const u_char *name, size_t namelen)
1180 if ((mod = malloc(namelen + 4)) == NULL)
1183 strncpy(mod + 3, name, namelen);
1184 mod[namelen + 3] = '\0';
1192 * Unload a netgraph type.
1195 ngtype_unload(const u_char *name, size_t namelen)
1200 if ((mod = malloc(namelen + 4)) == NULL)
1203 strncpy(mod + 3, name, namelen);
1204 mod[namelen + 3] = '\0';
1206 if ((id = kldfind(mod)) == -1) {
1211 return (kldunload(id));
1215 op_ng_type(struct snmp_context *ctx, struct snmp_value *value,
1216 u_int sub, u_int iidx, enum snmp_op op)
1218 asn_subid_t which = value->var.subs[sub - 1];
1227 case SNMP_OP_GETNEXT:
1228 if ((ret = fetch_types()) != 0)
1230 if ((t = NEXT_OBJECT_OID(&ngtype_list, &value->var, sub)) == NULL)
1231 return (SNMP_ERR_NOSUCHNAME);
1232 index_append(&value->var, sub, &t->index);
1236 if ((ret = fetch_types()) != 0)
1238 if ((t = FIND_OBJECT_OID(&ngtype_list, &value->var, sub)) == NULL)
1239 return (SNMP_ERR_NOSUCHNAME);
1243 if (index_decode(&value->var, sub, iidx, &name, &namelen))
1244 return (SNMP_ERR_NO_CREATION);
1245 if (namelen == 0 || namelen >= NG_TYPESIZ) {
1247 return (SNMP_ERR_NO_CREATION);
1249 if ((ret = fetch_types()) != 0) {
1253 t = FIND_OBJECT_OID(&ngtype_list, &value->var, sub);
1255 if (which != LEAF_begemotNgTypeStatus) {
1258 return (SNMP_ERR_NOT_WRITEABLE);
1259 return (SNMP_ERR_NO_CREATION);
1261 if (!TRUTH_OK(value->v.integer)) {
1263 return (SNMP_ERR_WRONG_VALUE);
1265 ctx->scratch->int1 = TRUTH_GET(value->v.integer);
1266 ctx->scratch->int1 |= (t != NULL) << 1;
1267 ctx->scratch->ptr2 = name;
1268 ctx->scratch->int2 = namelen;
1271 /* type not loaded */
1272 if (ctx->scratch->int1 & 1) {
1273 /* request to load */
1274 if (ngtype_load(name, namelen) == -1) {
1276 if (errno == ENOENT)
1277 return (SNMP_ERR_INCONS_NAME);
1279 return (SNMP_ERR_GENERR);
1283 /* is type loaded */
1284 if (!(ctx->scratch->int1 & 1)) {
1285 /* request to unload */
1286 if (ngtype_unload(name, namelen) == -1) {
1288 return (SNMP_ERR_GENERR);
1292 return (SNMP_ERR_NOERROR);
1294 case SNMP_OP_ROLLBACK:
1295 ret = SNMP_ERR_NOERROR;
1296 if (!(ctx->scratch->int1 & 2)) {
1298 if (ctx->scratch->int1 & 1) {
1299 /* request to load - unload */
1300 if (ngtype_unload(ctx->scratch->ptr2,
1301 ctx->scratch->int2) == -1)
1302 ret = SNMP_ERR_UNDO_FAILED;
1306 if (!(ctx->scratch->int1 & 1)) {
1307 /* request to unload - reload */
1308 if (ngtype_load(ctx->scratch->ptr2,
1309 ctx->scratch->int2) == -1)
1310 ret = SNMP_ERR_UNDO_FAILED;
1313 free(ctx->scratch->ptr2);
1316 case SNMP_OP_COMMIT:
1317 free(ctx->scratch->ptr2);
1318 return (SNMP_ERR_NOERROR);
1325 * Come here for GET and COMMIT
1329 case LEAF_begemotNgTypeStatus:
1330 value->v.integer = status;
1336 return (SNMP_ERR_NOERROR);
1340 * Implement the node table
1343 find_node(const struct asn_oid *oid, u_int sub, struct nodeinfo *info)
1345 ng_ID_t id = oid->subs[sub];
1346 struct ng_mesg *resp;
1348 if ((resp = ng_dialog_id(id, NGM_GENERIC_COOKIE, NGM_NODEINFO,
1352 *info = *(struct nodeinfo *)(void *)resp->data;
1358 ncmp(const void *p1, const void *p2)
1360 const struct nodeinfo *i1 = p1;
1361 const struct nodeinfo *i2 = p2;
1363 if (i1->id < i2->id)
1365 if (i1->id > i2->id)
1371 find_node_next(const struct asn_oid *oid, u_int sub, struct nodeinfo *info)
1373 u_int idxlen = oid->len - sub;
1374 struct ng_mesg *resp;
1375 struct namelist *list;
1379 if ((resp = ng_dialog_id(snmp_node, NGM_GENERIC_COOKIE, NGM_LISTNODES,
1382 list = (struct namelist *)(void *)resp->data;
1384 qsort(list->nodeinfo, list->numnames, sizeof(list->nodeinfo[0]), ncmp);
1387 if (list->numnames == 0) {
1391 *info = list->nodeinfo[0];
1395 id = oid->subs[sub];
1397 for (i = 0; i < list->numnames; i++)
1398 if (list->nodeinfo[i].id > id) {
1399 *info = list->nodeinfo[i];
1409 op_ng_node(struct snmp_context *ctx __unused, struct snmp_value *value,
1410 u_int sub, u_int iidx __unused, enum snmp_op op)
1412 asn_subid_t which = value->var.subs[sub - 1];
1413 u_int idxlen = value->var.len - sub;
1414 struct nodeinfo nodeinfo;
1418 case SNMP_OP_GETNEXT:
1419 if (find_node_next(&value->var, sub, &nodeinfo) == -1)
1420 return (SNMP_ERR_NOSUCHNAME);
1421 value->var.len = sub + 1;
1422 value->var.subs[sub] = nodeinfo.id;
1427 return (SNMP_ERR_NOSUCHNAME);
1428 if (find_node(&value->var, sub, &nodeinfo) == -1)
1429 return (SNMP_ERR_NOSUCHNAME);
1434 return (SNMP_ERR_NO_CREATION);
1435 if (find_node(&value->var, sub, &nodeinfo) == -1)
1436 return (SNMP_ERR_NO_CREATION);
1437 return (SNMP_ERR_NOT_WRITEABLE);
1439 case SNMP_OP_ROLLBACK:
1440 case SNMP_OP_COMMIT:
1446 * Come here for GET and COMMIT
1450 case LEAF_begemotNgNodeStatus:
1451 value->v.integer = 1;
1453 case LEAF_begemotNgNodeName:
1454 return (string_get(value, nodeinfo.name, -1));
1455 case LEAF_begemotNgNodeType:
1456 return (string_get(value, nodeinfo.type, -1));
1457 case LEAF_begemotNgNodeHooks:
1458 value->v.uint32 = nodeinfo.hooks;
1464 return (SNMP_ERR_NOERROR);
1468 * Implement the hook table
1471 find_hook(int32_t id, const u_char *hook, size_t hooklen, struct linkinfo *info)
1473 struct ng_mesg *resp;
1474 struct hooklist *list;
1477 if ((resp = ng_dialog_id(id, NGM_GENERIC_COOKIE,
1478 NGM_LISTHOOKS, NULL, 0)) == NULL)
1481 list = (struct hooklist *)(void *)resp->data;
1483 for (i = 0; i < list->nodeinfo.hooks; i++) {
1484 if (strlen(list->link[i].ourhook) == hooklen &&
1485 strncmp(list->link[i].ourhook, hook, hooklen) == 0) {
1486 *info = list->link[i];
1496 hook_cmp(const void *p1, const void *p2)
1498 const struct linkinfo *i1 = p1;
1499 const struct linkinfo *i2 = p2;
1501 if (strlen(i1->ourhook) < strlen(i2->ourhook))
1503 if (strlen(i1->ourhook) > strlen(i2->ourhook))
1505 return (strcmp(i1->ourhook, i2->ourhook));
1509 find_hook_next(const struct asn_oid *oid, u_int sub, struct nodeinfo *nodeinfo,
1510 struct linkinfo *linkinfo)
1512 u_int idxlen = oid->len - sub;
1513 struct namelist *list;
1514 struct ng_mesg *resp;
1515 struct hooklist *hooks;
1516 struct ng_mesg *resp1;
1522 * Get and sort Node list
1524 if ((resp = ng_dialog_id(snmp_node, NGM_GENERIC_COOKIE, NGM_LISTNODES,
1527 list = (struct namelist *)(void *)resp->data;
1529 qsort(list->nodeinfo, list->numnames, sizeof(list->nodeinfo[0]), ncmp);
1532 * If we have no index, take the first node and return the
1537 goto return_first_hook;
1543 for (node_index = 0; node_index < list->numnames; node_index++)
1544 if (list->nodeinfo[node_index].id >= oid->subs[sub])
1548 * If we have only the node part of the index take, or
1549 * there is no node with that Id, take the first hook of that node.
1551 if (idxlen == 1 || node_index >= list->numnames ||
1552 list->nodeinfo[node_index].id > oid->subs[sub])
1553 goto return_first_hook;
1556 * We had an exact match on the node id and have (at last part)
1557 * of the hook name index. Loop through the hooks of the node
1558 * and find the next one.
1560 if ((resp1 = ng_dialog_id(list->nodeinfo[node_index].id,
1561 NGM_GENERIC_COOKIE, NGM_LISTHOOKS, NULL, 0)) == NULL) {
1565 hooks = (struct hooklist *)(void *)resp1->data;
1566 if (hooks->nodeinfo.hooks > 0) {
1567 qsort(hooks->link, hooks->nodeinfo.hooks,
1568 sizeof(hooks->link[0]), hook_cmp);
1569 for (i = 0; i < hooks->nodeinfo.hooks; i++) {
1570 idx.len = strlen(hooks->link[i].ourhook) + 1;
1571 idx.subs[0] = idx.len - 1;
1572 for (j = 0; j < idx.len; j++)
1573 idx.subs[j + 1] = hooks->link[i].ourhook[j];
1574 if (index_compare(oid, sub + 1, &idx) < 0)
1577 if (i < hooks->nodeinfo.hooks) {
1578 *nodeinfo = hooks->nodeinfo;
1579 *linkinfo = hooks->link[i];
1587 /* no hook found larger than the index on the index node - take
1588 * first hook of next node */
1593 while (node_index < list->numnames) {
1594 if ((resp1 = ng_dialog_id(list->nodeinfo[node_index].id,
1595 NGM_GENERIC_COOKIE, NGM_LISTHOOKS, NULL, 0)) == NULL)
1597 hooks = (struct hooklist *)(void *)resp1->data;
1598 if (hooks->nodeinfo.hooks > 0) {
1599 qsort(hooks->link, hooks->nodeinfo.hooks,
1600 sizeof(hooks->link[0]), hook_cmp);
1602 *nodeinfo = hooks->nodeinfo;
1603 *linkinfo = hooks->link[0];
1610 /* if we don't have hooks, try next node */
1620 op_ng_hook(struct snmp_context *ctx __unused, struct snmp_value *value,
1621 u_int sub, u_int iidx, enum snmp_op op)
1623 asn_subid_t which = value->var.subs[sub - 1];
1624 struct linkinfo linkinfo;
1625 struct nodeinfo nodeinfo;
1633 case SNMP_OP_GETNEXT:
1634 if (find_hook_next(&value->var, sub, &nodeinfo, &linkinfo) == -1)
1635 return (SNMP_ERR_NOSUCHNAME);
1637 value->var.len = sub + 1 + 1 + strlen(linkinfo.ourhook);
1638 value->var.subs[sub] = nodeinfo.id;
1639 value->var.subs[sub + 1] = strlen(linkinfo.ourhook);
1640 for (i = 0; i < strlen(linkinfo.ourhook); i++)
1641 value->var.subs[sub + i + 2] =
1642 linkinfo.ourhook[i];
1646 if (index_decode(&value->var, sub, iidx, &lid,
1648 return (SNMP_ERR_NOSUCHNAME);
1649 if (find_hook(lid, hook, hooklen, &linkinfo) == -1) {
1651 return (SNMP_ERR_NOSUCHNAME);
1657 if (index_decode(&value->var, sub, iidx, &lid,
1659 return (SNMP_ERR_NO_CREATION);
1660 if (find_hook(lid, hook, hooklen, &linkinfo) == -1) {
1662 return (SNMP_ERR_NO_CREATION);
1665 return (SNMP_ERR_NOT_WRITEABLE);
1667 case SNMP_OP_ROLLBACK:
1668 case SNMP_OP_COMMIT:
1676 case LEAF_begemotNgHookStatus:
1677 value->v.integer = 1;
1679 case LEAF_begemotNgHookPeerNodeId:
1680 value->v.uint32 = linkinfo.nodeinfo.id;
1682 case LEAF_begemotNgHookPeerHook:
1683 return (string_get(value, linkinfo.peerhook, -1));
1684 case LEAF_begemotNgHookPeerType:
1685 return (string_get(value, linkinfo.nodeinfo.type, -1));
1689 return (SNMP_ERR_NOERROR);