2 * Copyright (c) 2005-2006 The FreeBSD Project
5 * Author: Victor Cruceru <soc-victor@freebsd.org>
7 * Redistribution of this software and documentation and use in source and
8 * binary forms, with or without modification, are permitted provided that
9 * the following conditions are met:
11 * 1. Redistributions of source code or documentation must retain the above
12 * copyright notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Host Resources MIB implementation for SNMPd: instrumentation for
37 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40 #include <sys/sysctl.h>
43 #include <net/if_mib.h>
56 #include "hostres_snmp.h"
57 #include "hostres_oid.h"
58 #include "hostres_tree.h"
60 #include <bsnmp/snmp_mibII.h>
63 * This structure is used to hold a SNMP table entry
64 * for HOST-RESOURCES-MIB's hrNetworkTable
66 struct network_entry {
69 TAILQ_ENTRY(network_entry) link;
70 #define HR_NETWORK_FOUND 0x001
74 TAILQ_HEAD(network_tbl, network_entry);
76 /* the head of the list with hrNetworkTable's entries */
77 static struct network_tbl network_tbl = TAILQ_HEAD_INITIALIZER(network_tbl);
79 /* last (agent) tick when hrNetworkTable was updated */
80 static uint64_t network_tick;
82 /* maximum number of ticks between updates of network table */
83 uint32_t network_tbl_refresh = HR_NETWORK_TBL_REFRESH * 100;
86 static const struct asn_oid OIDX_hrDeviceNetwork_c = OIDX_hrDeviceNetwork;
89 * Create a new entry into the network table
91 static struct network_entry *
92 network_entry_create(const struct device_entry *devEntry)
94 struct network_entry *entry;
96 assert(devEntry != NULL);
100 if ((entry = malloc(sizeof(*entry))) == NULL) {
101 syslog(LOG_WARNING, "%s: %m", __func__);
105 memset(entry, 0, sizeof(*entry));
106 entry->index = devEntry->index;
107 INSERT_OBJECT_INT(entry, &network_tbl);
113 * Delete an entry in the network table
116 network_entry_delete(struct network_entry* entry)
119 TAILQ_REMOVE(&network_tbl, entry, link);
124 * Fetch the interfaces from the mibII module, get their real name from the
125 * kernel and try to find it in the device table.
128 network_get_interfaces(void)
130 struct device_entry *dev;
131 struct network_entry *net;
139 name[2] = NETLINK_GENERIC;
140 name[3] = IFMIB_IFDATA;
141 name[5] = IFDATA_DRIVERNAME;
143 for (ifp = mib_first_if(); ifp != NULL; ifp = mib_next_if(ifp)) {
144 HRDBG("%s %s", ifp->name, ifp->descr);
146 name[4] = ifp->sysindex;
148 /* get the original name */
150 if (sysctl(name, 6, NULL, &len, 0, 0) < 0) {
151 syslog(LOG_ERR, "sysctl(net.link.ifdata.%d."
152 "drivername): %m", ifp->sysindex);
155 if ((dname = malloc(len)) == NULL) {
156 syslog(LOG_ERR, "malloc: %m");
159 if (sysctl(name, 6, dname, &len, 0, 0) < 0) {
160 syslog(LOG_ERR, "sysctl(net.link.ifdata.%d."
161 "drivername): %m", ifp->sysindex);
166 HRDBG("got device %s (%s)", ifp->name, dname);
168 if ((dev = device_find_by_name(dname)) == NULL) {
169 HRDBG("%s not in hrDeviceTable", dname);
173 HRDBG("%s found in hrDeviceTable", dname);
175 dev->type = &OIDX_hrDeviceNetwork_c;
176 dev->flags |= HR_DEVICE_IMMUTABLE;
180 /* Then check hrNetworkTable for this device */
181 TAILQ_FOREACH(net, &network_tbl, link)
182 if (net->index == dev->index)
185 if (net == NULL && (net = network_entry_create(dev)) == NULL)
188 net->flags |= HR_NETWORK_FOUND;
189 net->ifIndex = ifp->index;
192 network_tick = this_tick;
196 * Finalization routine for hrNetworkTable.
197 * It destroys the lists and frees any allocated heap memory.
200 fini_network_tbl(void)
202 struct network_entry *n1;
204 while ((n1 = TAILQ_FIRST(&network_tbl)) != NULL) {
205 TAILQ_REMOVE(&network_tbl, n1, link);
211 * Get the interface list from mibII only at this point to be sure that
212 * it is there already.
215 start_network_tbl(void)
218 mib_refresh_iflist();
219 network_get_interfaces();
226 refresh_network_tbl(void)
228 struct network_entry *entry, *entry_tmp;
230 if (this_tick - network_tick < network_tbl_refresh) {
231 HRDBG("no refresh needed");
235 /* mark each entry as missing */
236 TAILQ_FOREACH(entry, &network_tbl, link)
237 entry->flags &= ~HR_NETWORK_FOUND;
239 network_get_interfaces();
242 * Purge items that disappeared
244 TAILQ_FOREACH_SAFE(entry, &network_tbl, link, entry_tmp) {
245 if (!(entry->flags & HR_NETWORK_FOUND))
246 network_entry_delete(entry);
249 HRDBG("refresh DONE");
253 * This is the implementation for a generated (by our SNMP tool)
254 * function prototype, see hostres_tree.h
255 * It handles the SNMP operations for hrNetworkTable
258 op_hrNetworkTable(struct snmp_context *ctx __unused, struct snmp_value *value,
259 u_int sub, u_int iidx __unused, enum snmp_op curr_op)
261 struct network_entry *entry;
263 refresh_network_tbl();
267 case SNMP_OP_GETNEXT:
268 if ((entry = NEXT_OBJECT_INT(&network_tbl,
269 &value->var, sub)) == NULL)
270 return (SNMP_ERR_NOSUCHNAME);
271 value->var.len = sub + 1;
272 value->var.subs[sub] = entry->index;
276 if ((entry = FIND_OBJECT_INT(&network_tbl,
277 &value->var, sub)) == NULL)
278 return (SNMP_ERR_NOSUCHNAME);
282 if ((entry = FIND_OBJECT_INT(&network_tbl,
283 &value->var, sub)) == NULL)
284 return (SNMP_ERR_NO_CREATION);
285 return (SNMP_ERR_NOT_WRITEABLE);
287 case SNMP_OP_ROLLBACK:
294 switch (value->var.subs[sub - 1]) {
296 case LEAF_hrNetworkIfIndex:
297 value->v.integer = entry->ifIndex;
298 return (SNMP_ERR_NOERROR);