2 * Copyright (c) 2006 Shteryana Shopova <syrinx@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * Bridge MIB implementation for SNMPd.
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/socket.h>
34 #include <sys/types.h>
36 #include <net/ethernet.h>
38 #include <net/if_mib.h>
39 #include <net/if_types.h>
48 #include <bsnmp/snmpmod.h>
49 #include <bsnmp/snmp_mibII.h>
51 #include "bridge_tree.h"
52 #include "bridge_snmp.h"
53 #include "bridge_oid.h"
55 static struct lmodule *bridge_module;
57 /* For the registration. */
58 static const struct asn_oid oid_dot1Bridge = OIDX_dot1dBridge;
59 /* The registration. */
60 static uint reg_bridge;
62 /* Periodic timer for polling all bridges' data. */
63 static void *bridge_data_timer;
64 static void *bridge_tc_timer;
66 static int bridge_data_maxage = SNMP_BRIDGE_DATA_MAXAGE;
67 static int bridge_poll_ticks = SNMP_BRIDGE_POLL_INTERVAL * 100;
68 static int bridge_tc_poll_ticks = SNMP_BRIDGE_TC_POLL_INTERVAL * 100;
71 * Our default bridge, whose info will be visible under
72 * the dot1dBridge subtree and functions to set/fetch it.
74 static char bif_default_name[IFNAMSIZ] = "bridge0";
75 static struct bridge_if *bif_default;
78 bridge_get_default(void)
82 if (bif_default != NULL) {
84 /* Walk through the mibII interface list. */
85 for (ifp = mib_first_if(); ifp != NULL; ifp = mib_next_if(ifp))
86 if (strcmp(ifp->name, bif_default->bif_name) == 0)
97 bridge_set_default(struct bridge_if *bif)
101 syslog(LOG_ERR, "Set default bridge interface to: %s",
102 bif == NULL ? "(none)" : bif->bif_name);
106 bridge_get_default_name(void)
108 return (bif_default_name);
112 bridge_set_default_name(const char *bif_name, uint len)
114 struct bridge_if *bif;
119 bcopy(bif_name, bif_default_name, len);
120 bif_default_name[len] = '\0';
122 if ((bif = bridge_if_find_ifname(bif_default_name)) == NULL) {
132 bridge_get_data_maxage(void)
134 return (bridge_data_maxage);
138 bridge_set_poll_ticks(int poll_ticks)
140 if (bridge_data_timer != NULL)
141 timer_stop(bridge_data_timer);
143 bridge_poll_ticks = poll_ticks;
144 bridge_data_timer = timer_start_repeat(bridge_poll_ticks,
145 bridge_poll_ticks, bridge_update_all, NULL, bridge_module);
148 * The bridge module configuration via SNMP.
151 bridge_default_name_save(struct snmp_context *ctx, const char *bridge_default)
153 if ((ctx->scratch->int1 = strlen(bridge_default)) >= IFNAMSIZ)
156 if ((ctx->scratch->ptr1 = malloc(IFNAMSIZ)) == NULL)
159 strncpy(ctx->scratch->ptr1, bridge_default, ctx->scratch->int1);
164 op_begemot_bridge_config(struct snmp_context *ctx, struct snmp_value *val,
165 uint sub, uint iidx __unused, enum snmp_op op)
169 switch (val->var.subs[sub - 1]) {
170 case LEAF_begemotBridgeDefaultBridgeIf:
171 return (string_get(val, bridge_get_default_name(), -1));
173 case LEAF_begemotBridgeDataUpdate:
174 val->v.integer = bridge_data_maxage;
175 return (SNMP_ERR_NOERROR);
177 case LEAF_begemotBridgeDataPoll:
178 val->v.integer = bridge_poll_ticks / 100;
179 return (SNMP_ERR_NOERROR);
183 case SNMP_OP_GETNEXT:
187 switch (val->var.subs[sub - 1]) {
188 case LEAF_begemotBridgeDefaultBridgeIf:
190 * Cannot use string_save() here - requires either
191 * a fixed-sized or var-length string - not less
194 if (bridge_default_name_save(ctx,
195 bridge_get_default_name()) < 0)
196 return (SNMP_ERR_RES_UNAVAIL);
198 if (bridge_set_default_name(val->v.octetstring.octets,
199 val->v.octetstring.len) < 0)
200 return (SNMP_ERR_BADVALUE);
201 return (SNMP_ERR_NOERROR);
203 case LEAF_begemotBridgeDataUpdate:
204 if (val->v.integer < SNMP_BRIDGE_DATA_MAXAGE_MIN ||
205 val->v.integer > SNMP_BRIDGE_DATA_MAXAGE_MAX)
206 return (SNMP_ERR_WRONG_VALUE);
207 ctx->scratch->int1 = bridge_data_maxage;
208 bridge_data_maxage = val->v.integer;
209 return (SNMP_ERR_NOERROR);
211 case LEAF_begemotBridgeDataPoll:
212 if (val->v.integer < SNMP_BRIDGE_POLL_INTERVAL_MIN ||
213 val->v.integer > SNMP_BRIDGE_POLL_INTERVAL_MAX)
214 return (SNMP_ERR_WRONG_VALUE);
215 ctx->scratch->int1 = val->v.integer;
216 return (SNMP_ERR_NOERROR);
220 case SNMP_OP_ROLLBACK:
221 switch (val->var.subs[sub - 1]) {
222 case LEAF_begemotBridgeDefaultBridgeIf:
223 bridge_set_default_name(ctx->scratch->ptr1,
225 free(ctx->scratch->ptr1);
227 case LEAF_begemotBridgeDataUpdate:
228 bridge_data_maxage = ctx->scratch->int1;
231 return (SNMP_ERR_NOERROR);
234 switch (val->var.subs[sub - 1]) {
235 case LEAF_begemotBridgeDefaultBridgeIf:
236 free(ctx->scratch->ptr1);
238 case LEAF_begemotBridgeDataPoll:
239 bridge_set_poll_ticks(ctx->scratch->int1 * 100);
242 return (SNMP_ERR_NOERROR);
249 * Bridge mib module initialization hook.
250 * Returns 0 on success, < 0 on error.
253 bridge_init(struct lmodule * mod, int argc __unused, char *argv[] __unused)
257 if (bridge_kmod_load() < 0)
260 if (bridge_ioctl_init() < 0)
263 /* Register to get creation messages for bridge interfaces. */
264 if (mib_register_newif(bridge_attach_newif, bridge_module)) {
265 syslog(LOG_ERR, "Cannot register newif function: %s",
274 * Bridge mib module finalization hook.
279 mib_unregister_newif(bridge_module);
280 or_unregister(reg_bridge);
282 if (bridge_data_timer != NULL) {
283 timer_stop(bridge_data_timer);
284 bridge_data_timer = NULL;
287 if (bridge_tc_timer != NULL) {
288 timer_stop(bridge_tc_timer);
289 bridge_tc_timer = NULL;
300 * Bridge mib module start operation.
305 reg_bridge = or_register(&oid_dot1Bridge,
306 "The IETF MIB for Bridges (RFC 4188).", bridge_module);
308 bridge_data_timer = timer_start_repeat(bridge_poll_ticks,
309 bridge_poll_ticks, bridge_update_all, NULL, bridge_module);
311 bridge_tc_timer = timer_start_repeat(bridge_tc_poll_ticks,
312 bridge_tc_poll_ticks, bridge_update_tc_time, NULL, bridge_module);
318 struct bridge_if *bif;
320 if ((bif = bridge_get_default()) == NULL)
321 syslog(LOG_ERR, "Dump: no default bridge interface");
323 syslog(LOG_ERR, "Dump: default bridge interface %s",
330 const struct snmp_module config = {
331 .comment = "This module implements the bridge mib (RFC 4188).",
334 .start = bridge_start,
335 .tree = bridge_ctree,
337 .tree_size = bridge_CTREE_SIZE,