Path: blob/main/usr.sbin/bsnmpd/modules/snmp_bridge/bridge_snmp.c
107110 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2006 Shteryana Shopova <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*27* Bridge MIB implementation for SNMPd.28*/2930#include <sys/param.h>31#include <sys/queue.h>32#include <sys/socket.h>33#include <sys/types.h>3435#include <net/ethernet.h>36#include <net/if.h>37#include <net/if_mib.h>38#include <net/if_types.h>3940#include <errno.h>41#include <stdarg.h>42#include <stdlib.h>43#include <stdio.h>44#include <string.h>45#include <syslog.h>4647#include <bsnmp/snmpmod.h>48#include <bsnmp/snmp_mibII.h>4950#define SNMPTREE_TYPES51#include "bridge_tree.h"52#include "bridge_snmp.h"53#include "bridge_oid.h"5455static struct lmodule *bridge_module;5657/* For the registration. */58static const struct asn_oid oid_dot1Bridge = OIDX_dot1dBridge;59/* The registration. */60static uint reg_bridge;6162/* Periodic timer for polling all bridges' data. */63static void *bridge_data_timer;64static void *bridge_tc_timer;6566static int bridge_data_maxage = SNMP_BRIDGE_DATA_MAXAGE;67static int bridge_poll_ticks = SNMP_BRIDGE_POLL_INTERVAL * 100;68static int bridge_tc_poll_ticks = SNMP_BRIDGE_TC_POLL_INTERVAL * 100;6970/*71* Our default bridge, whose info will be visible under72* the dot1dBridge subtree and functions to set/fetch it.73*/74static char bif_default_name[IFNAMSIZ] = "bridge0";75static struct bridge_if *bif_default;7677struct bridge_if *78bridge_get_default(void)79{80struct mibif *ifp;8182if (bif_default != NULL) {8384/* Walk through the mibII interface list. */85for (ifp = mib_first_if(); ifp != NULL; ifp = mib_next_if(ifp))86if (strcmp(ifp->name, bif_default->bif_name) == 0)87break;8889if (ifp == NULL)90bif_default = NULL;91}9293return (bif_default);94}9596void97bridge_set_default(struct bridge_if *bif)98{99bif_default = bif;100101syslog(LOG_ERR, "Set default bridge interface to: %s",102bif == NULL ? "(none)" : bif->bif_name);103}104105const char *106bridge_get_default_name(void)107{108return (bif_default_name);109}110111static int112bridge_set_default_name(const char *bif_name, uint len)113{114struct bridge_if *bif;115116if (len >= IFNAMSIZ)117return (-1);118119bcopy(bif_name, bif_default_name, len);120bif_default_name[len] = '\0';121122if ((bif = bridge_if_find_ifname(bif_default_name)) == NULL) {123bif_default = NULL;124return (0);125}126127bif_default = bif;128return (1);129}130131int132bridge_get_data_maxage(void)133{134return (bridge_data_maxage);135}136137static void138bridge_set_poll_ticks(int poll_ticks)139{140if (bridge_data_timer != NULL)141timer_stop(bridge_data_timer);142143bridge_poll_ticks = poll_ticks;144bridge_data_timer = timer_start_repeat(bridge_poll_ticks,145bridge_poll_ticks, bridge_update_all, NULL, bridge_module);146}147/*148* The bridge module configuration via SNMP.149*/150static int151bridge_default_name_save(struct snmp_context *ctx, const char *bridge_default)152{153if ((ctx->scratch->int1 = strlen(bridge_default)) >= IFNAMSIZ)154return (-1);155156if ((ctx->scratch->ptr1 = malloc(IFNAMSIZ)) == NULL)157return (-1);158159strncpy(ctx->scratch->ptr1, bridge_default, ctx->scratch->int1);160return (0);161}162163int164op_begemot_bridge_config(struct snmp_context *ctx, struct snmp_value *val,165uint sub, uint iidx __unused, enum snmp_op op)166{167switch (op) {168case SNMP_OP_GET:169switch (val->var.subs[sub - 1]) {170case LEAF_begemotBridgeDefaultBridgeIf:171return (string_get(val, bridge_get_default_name(), -1));172173case LEAF_begemotBridgeDataUpdate:174val->v.integer = bridge_data_maxage;175return (SNMP_ERR_NOERROR);176177case LEAF_begemotBridgeDataPoll:178val->v.integer = bridge_poll_ticks / 100;179return (SNMP_ERR_NOERROR);180}181abort();182183case SNMP_OP_GETNEXT:184abort();185186case SNMP_OP_SET:187switch (val->var.subs[sub - 1]) {188case LEAF_begemotBridgeDefaultBridgeIf:189/*190* Cannot use string_save() here - requires either191* a fixed-sized or var-length string - not less192* than or equal.193*/194if (bridge_default_name_save(ctx,195bridge_get_default_name()) < 0)196return (SNMP_ERR_RES_UNAVAIL);197198if (bridge_set_default_name(val->v.octetstring.octets,199val->v.octetstring.len) < 0)200return (SNMP_ERR_BADVALUE);201return (SNMP_ERR_NOERROR);202203case LEAF_begemotBridgeDataUpdate:204if (val->v.integer < SNMP_BRIDGE_DATA_MAXAGE_MIN ||205val->v.integer > SNMP_BRIDGE_DATA_MAXAGE_MAX)206return (SNMP_ERR_WRONG_VALUE);207ctx->scratch->int1 = bridge_data_maxage;208bridge_data_maxage = val->v.integer;209return (SNMP_ERR_NOERROR);210211case LEAF_begemotBridgeDataPoll:212if (val->v.integer < SNMP_BRIDGE_POLL_INTERVAL_MIN ||213val->v.integer > SNMP_BRIDGE_POLL_INTERVAL_MAX)214return (SNMP_ERR_WRONG_VALUE);215ctx->scratch->int1 = val->v.integer;216return (SNMP_ERR_NOERROR);217}218abort();219220case SNMP_OP_ROLLBACK:221switch (val->var.subs[sub - 1]) {222case LEAF_begemotBridgeDefaultBridgeIf:223bridge_set_default_name(ctx->scratch->ptr1,224ctx->scratch->int1);225free(ctx->scratch->ptr1);226break;227case LEAF_begemotBridgeDataUpdate:228bridge_data_maxage = ctx->scratch->int1;229break;230}231return (SNMP_ERR_NOERROR);232233case SNMP_OP_COMMIT:234switch (val->var.subs[sub - 1]) {235case LEAF_begemotBridgeDefaultBridgeIf:236free(ctx->scratch->ptr1);237break;238case LEAF_begemotBridgeDataPoll:239bridge_set_poll_ticks(ctx->scratch->int1 * 100);240break;241}242return (SNMP_ERR_NOERROR);243}244245abort();246}247248/*249* Bridge mib module initialization hook.250* Returns 0 on success, < 0 on error.251*/252static int253bridge_init(struct lmodule * mod, int argc __unused, char *argv[] __unused)254{255bridge_module = mod;256257if (bridge_kmod_load() < 0)258return (-1);259260if (bridge_ioctl_init() < 0)261return (-1);262263/* Register to get creation messages for bridge interfaces. */264if (mib_register_newif(bridge_attach_newif, bridge_module)) {265syslog(LOG_ERR, "Cannot register newif function: %s",266strerror(errno));267return (-1);268}269270return (0);271}272273/*274* Bridge mib module finalization hook.275*/276static int277bridge_fini(void)278{279mib_unregister_newif(bridge_module);280or_unregister(reg_bridge);281282if (bridge_data_timer != NULL) {283timer_stop(bridge_data_timer);284bridge_data_timer = NULL;285}286287if (bridge_tc_timer != NULL) {288timer_stop(bridge_tc_timer);289bridge_tc_timer = NULL;290}291292bridge_ifs_fini();293bridge_ports_fini();294bridge_addrs_fini();295296return (0);297}298299/*300* Bridge mib module start operation.301*/302static void303bridge_start(void)304{305reg_bridge = or_register(&oid_dot1Bridge,306"The IETF MIB for Bridges (RFC 4188).", bridge_module);307308bridge_data_timer = timer_start_repeat(bridge_poll_ticks,309bridge_poll_ticks, bridge_update_all, NULL, bridge_module);310311bridge_tc_timer = timer_start_repeat(bridge_tc_poll_ticks,312bridge_tc_poll_ticks, bridge_update_tc_time, NULL, bridge_module);313}314315static void316bridge_dump(void)317{318struct bridge_if *bif;319320if ((bif = bridge_get_default()) == NULL)321syslog(LOG_ERR, "Dump: no default bridge interface");322else323syslog(LOG_ERR, "Dump: default bridge interface %s",324bif->bif_name);325326bridge_ifs_dump();327bridge_pf_dump();328}329330const struct snmp_module config = {331.comment = "This module implements the bridge mib (RFC 4188).",332.init = bridge_init,333.fini = bridge_fini,334.start = bridge_start,335.tree = bridge_ctree,336.dump = bridge_dump,337.tree_size = bridge_CTREE_SIZE,338};339340341