2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Shteryana Shopova <syrinx@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Bridge MIB implementation for SNMPd.
33 #include <sys/param.h>
34 #include <sys/queue.h>
35 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <net/ethernet.h>
40 #include <net/if_mib.h>
41 #include <net/if_types.h>
50 #include <bsnmp/snmpmod.h>
51 #include <bsnmp/snmp_mibII.h>
53 #define SNMPTREE_TYPES
54 #include "bridge_tree.h"
55 #include "bridge_snmp.h"
56 #include "bridge_oid.h"
58 static struct lmodule *bridge_module;
60 /* For the registration. */
61 static const struct asn_oid oid_dot1Bridge = OIDX_dot1dBridge;
62 /* The registration. */
63 static uint reg_bridge;
65 /* Periodic timer for polling all bridges' data. */
66 static void *bridge_data_timer;
67 static void *bridge_tc_timer;
69 static int bridge_data_maxage = SNMP_BRIDGE_DATA_MAXAGE;
70 static int bridge_poll_ticks = SNMP_BRIDGE_POLL_INTERVAL * 100;
71 static int bridge_tc_poll_ticks = SNMP_BRIDGE_TC_POLL_INTERVAL * 100;
74 * Our default bridge, whose info will be visible under
75 * the dot1dBridge subtree and functions to set/fetch it.
77 static char bif_default_name[IFNAMSIZ] = "bridge0";
78 static struct bridge_if *bif_default;
81 bridge_get_default(void)
85 if (bif_default != NULL) {
87 /* Walk through the mibII interface list. */
88 for (ifp = mib_first_if(); ifp != NULL; ifp = mib_next_if(ifp))
89 if (strcmp(ifp->name, bif_default->bif_name) == 0)
100 bridge_set_default(struct bridge_if *bif)
104 syslog(LOG_ERR, "Set default bridge interface to: %s",
105 bif == NULL ? "(none)" : bif->bif_name);
109 bridge_get_default_name(void)
111 return (bif_default_name);
115 bridge_set_default_name(const char *bif_name, uint len)
117 struct bridge_if *bif;
122 bcopy(bif_name, bif_default_name, len);
123 bif_default_name[len] = '\0';
125 if ((bif = bridge_if_find_ifname(bif_default_name)) == NULL) {
135 bridge_get_data_maxage(void)
137 return (bridge_data_maxage);
141 bridge_set_poll_ticks(int poll_ticks)
143 if (bridge_data_timer != NULL)
144 timer_stop(bridge_data_timer);
146 bridge_poll_ticks = poll_ticks;
147 bridge_data_timer = timer_start_repeat(bridge_poll_ticks,
148 bridge_poll_ticks, bridge_update_all, NULL, bridge_module);
151 * The bridge module configuration via SNMP.
154 bridge_default_name_save(struct snmp_context *ctx, const char *bridge_default)
156 if ((ctx->scratch->int1 = strlen(bridge_default)) >= IFNAMSIZ)
159 if ((ctx->scratch->ptr1 = malloc(IFNAMSIZ)) == NULL)
162 strncpy(ctx->scratch->ptr1, bridge_default, ctx->scratch->int1);
167 op_begemot_bridge_config(struct snmp_context *ctx, struct snmp_value *val,
168 uint sub, uint iidx __unused, enum snmp_op op)
172 switch (val->var.subs[sub - 1]) {
173 case LEAF_begemotBridgeDefaultBridgeIf:
174 return (string_get(val, bridge_get_default_name(), -1));
176 case LEAF_begemotBridgeDataUpdate:
177 val->v.integer = bridge_data_maxage;
178 return (SNMP_ERR_NOERROR);
180 case LEAF_begemotBridgeDataPoll:
181 val->v.integer = bridge_poll_ticks / 100;
182 return (SNMP_ERR_NOERROR);
186 case SNMP_OP_GETNEXT:
190 switch (val->var.subs[sub - 1]) {
191 case LEAF_begemotBridgeDefaultBridgeIf:
193 * Cannot use string_save() here - requires either
194 * a fixed-sized or var-length string - not less
197 if (bridge_default_name_save(ctx,
198 bridge_get_default_name()) < 0)
199 return (SNMP_ERR_RES_UNAVAIL);
201 if (bridge_set_default_name(val->v.octetstring.octets,
202 val->v.octetstring.len) < 0)
203 return (SNMP_ERR_BADVALUE);
204 return (SNMP_ERR_NOERROR);
206 case LEAF_begemotBridgeDataUpdate:
207 if (val->v.integer < SNMP_BRIDGE_DATA_MAXAGE_MIN ||
208 val->v.integer > SNMP_BRIDGE_DATA_MAXAGE_MAX)
209 return (SNMP_ERR_WRONG_VALUE);
210 ctx->scratch->int1 = bridge_data_maxage;
211 bridge_data_maxage = val->v.integer;
212 return (SNMP_ERR_NOERROR);
214 case LEAF_begemotBridgeDataPoll:
215 if (val->v.integer < SNMP_BRIDGE_POLL_INTERVAL_MIN ||
216 val->v.integer > SNMP_BRIDGE_POLL_INTERVAL_MAX)
217 return (SNMP_ERR_WRONG_VALUE);
218 ctx->scratch->int1 = val->v.integer;
219 return (SNMP_ERR_NOERROR);
223 case SNMP_OP_ROLLBACK:
224 switch (val->var.subs[sub - 1]) {
225 case LEAF_begemotBridgeDefaultBridgeIf:
226 bridge_set_default_name(ctx->scratch->ptr1,
228 free(ctx->scratch->ptr1);
230 case LEAF_begemotBridgeDataUpdate:
231 bridge_data_maxage = ctx->scratch->int1;
234 return (SNMP_ERR_NOERROR);
237 switch (val->var.subs[sub - 1]) {
238 case LEAF_begemotBridgeDefaultBridgeIf:
239 free(ctx->scratch->ptr1);
241 case LEAF_begemotBridgeDataPoll:
242 bridge_set_poll_ticks(ctx->scratch->int1 * 100);
245 return (SNMP_ERR_NOERROR);
252 * Bridge mib module initialization hook.
253 * Returns 0 on success, < 0 on error.
256 bridge_init(struct lmodule * mod, int argc __unused, char *argv[] __unused)
260 if (bridge_kmod_load() < 0)
263 if (bridge_ioctl_init() < 0)
266 /* Register to get creation messages for bridge interfaces. */
267 if (mib_register_newif(bridge_attach_newif, bridge_module)) {
268 syslog(LOG_ERR, "Cannot register newif function: %s",
277 * Bridge mib module finalization hook.
282 mib_unregister_newif(bridge_module);
283 or_unregister(reg_bridge);
285 if (bridge_data_timer != NULL) {
286 timer_stop(bridge_data_timer);
287 bridge_data_timer = NULL;
290 if (bridge_tc_timer != NULL) {
291 timer_stop(bridge_tc_timer);
292 bridge_tc_timer = NULL;
303 * Bridge mib module start operation.
308 reg_bridge = or_register(&oid_dot1Bridge,
309 "The IETF MIB for Bridges (RFC 4188).", bridge_module);
311 bridge_data_timer = timer_start_repeat(bridge_poll_ticks,
312 bridge_poll_ticks, bridge_update_all, NULL, bridge_module);
314 bridge_tc_timer = timer_start_repeat(bridge_tc_poll_ticks,
315 bridge_tc_poll_ticks, bridge_update_tc_time, NULL, bridge_module);
321 struct bridge_if *bif;
323 if ((bif = bridge_get_default()) == NULL)
324 syslog(LOG_ERR, "Dump: no default bridge interface");
326 syslog(LOG_ERR, "Dump: default bridge interface %s",
333 const struct snmp_module config = {
334 .comment = "This module implements the bridge mib (RFC 4188).",
337 .start = bridge_start,
338 .tree = bridge_ctree,
340 .tree_size = bridge_CTREE_SIZE,