summaryrefslogtreecommitdiff
path: root/usr.sbin/bsnmpd/modules/snmp_bridge/bridge_snmp.c
blob: 81acc4dc95e70a7024488d6a797f291cdd50894a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*-
 * Copyright (c) 2006 Shteryana Shopova <syrinx@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * Bridge MIB implementation for SNMPd.
 *
 * $FreeBSD$
 */

#include <sys/param.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/types.h>

#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_mib.h>
#include <net/if_types.h>

#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>

#include <bsnmp/snmpmod.h>
#include <bsnmp/snmp_mibII.h>

#include "bridge_tree.h"
#include "bridge_snmp.h"
#include "bridge_oid.h"

static struct lmodule *bridge_module;

/* For the registration. */
static const struct asn_oid oid_dot1Bridge = OIDX_dot1dBridge;
/* The registration. */
static uint reg_bridge;

/* Periodic timer for polling all bridges' data. */
static void *bridge_data_timer;
static void *bridge_tc_timer;

static int bridge_data_maxage = SNMP_BRIDGE_DATA_MAXAGE;
static int bridge_poll_ticks = SNMP_BRIDGE_POLL_INTERVAL * 100;
static int bridge_tc_poll_ticks = SNMP_BRIDGE_TC_POLL_INTERVAL * 100;

/*
 * Our default bridge, whose info will be visible under
 * the dot1dBridge subtree and functions to set/fetch it.
 */
static char bif_default_name[IFNAMSIZ] = "bridge0";
static struct bridge_if *bif_default;

struct bridge_if *
bridge_get_default(void)
{
	struct mibif *ifp;

	if (bif_default != NULL) {

		/* Walk through the mibII interface list. */
		for (ifp = mib_first_if(); ifp != NULL; ifp = mib_next_if(ifp))
			if (strcmp(ifp->name, bif_default->bif_name) == 0)
				break;

		if (ifp == NULL)
			bif_default = NULL;
	}

	return (bif_default);
}

void
bridge_set_default(struct bridge_if *bif)
{
	bif_default = bif;

	syslog(LOG_ERR, "Set default bridge interface to: %s",
	    bif == NULL ? "(none)" : bif->bif_name);
}

const char *
bridge_get_default_name(void)
{
	return (bif_default_name);
}

static int
bridge_set_default_name(const char *bif_name, uint len)
{
	struct bridge_if *bif;

	if (len >= IFNAMSIZ)
		return (-1);

	bcopy(bif_name, bif_default_name, len);
	bif_default_name[len] = '\0';

	if ((bif = bridge_if_find_ifname(bif_default_name)) == NULL) {
		bif_default = NULL;
		return (0);
	}

	bif_default = bif;
	return (1);
}

int
bridge_get_data_maxage(void)
{
	return (bridge_data_maxage);
}

static void
bridge_set_poll_ticks(int poll_ticks)
{
	if (bridge_data_timer != NULL)
		timer_stop(bridge_data_timer);

	bridge_poll_ticks = poll_ticks;
	bridge_data_timer = timer_start_repeat(bridge_poll_ticks,
	    bridge_poll_ticks, bridge_update_all, NULL, bridge_module);
}
/*
 * The bridge module configuration via SNMP.
 */
static int
bridge_default_name_save(struct snmp_context *ctx, const char *bridge_default)
{
	if ((ctx->scratch->int1 = strlen(bridge_default)) >= IFNAMSIZ)
		return (-1);

	if ((ctx->scratch->ptr1 = malloc(IFNAMSIZ)) == NULL)
		return (-1);

	strncpy(ctx->scratch->ptr1, bridge_default, ctx->scratch->int1);
	return (0);
}

int
op_begemot_bridge_config(struct snmp_context *ctx, struct snmp_value *val,
    uint sub, uint iidx __unused, enum snmp_op op)
{
	switch (op) {
	    case SNMP_OP_GET:
		switch (val->var.subs[sub - 1]) {
		    case LEAF_begemotBridgeDefaultBridgeIf:
			return (string_get(val, bridge_get_default_name(), -1));

		    case LEAF_begemotBridgeDataUpdate:
			val->v.integer = bridge_data_maxage;
			return (SNMP_ERR_NOERROR);

		    case LEAF_begemotBridgeDataPoll:
			val->v.integer = bridge_poll_ticks / 100;
			return (SNMP_ERR_NOERROR);
		}
		abort();

	    case SNMP_OP_GETNEXT:
		abort();

	    case SNMP_OP_SET:
		switch (val->var.subs[sub - 1]) {
		    case LEAF_begemotBridgeDefaultBridgeIf:
			/*
			 * Cannot use string_save() here - requires either
			 * a fixed-sized or var-length string - not less
			 * than or equal.
			 */
			if (bridge_default_name_save(ctx,
			    bridge_get_default_name()) < 0)
				return (SNMP_ERR_RES_UNAVAIL);

			if (bridge_set_default_name(val->v.octetstring.octets,
			    val->v.octetstring.len) < 0)
				return (SNMP_ERR_BADVALUE);
			return (SNMP_ERR_NOERROR);

		    case LEAF_begemotBridgeDataUpdate:
			if (val->v.integer < SNMP_BRIDGE_DATA_MAXAGE_MIN ||
			    val->v.integer > SNMP_BRIDGE_DATA_MAXAGE_MAX)
				return (SNMP_ERR_WRONG_VALUE);
			ctx->scratch->int1 = bridge_data_maxage;
			bridge_data_maxage = val->v.integer;
			return (SNMP_ERR_NOERROR);

		    case LEAF_begemotBridgeDataPoll:
			if (val->v.integer < SNMP_BRIDGE_POLL_INTERVAL_MIN ||
			    val->v.integer > SNMP_BRIDGE_POLL_INTERVAL_MAX)
				return (SNMP_ERR_WRONG_VALUE);
			ctx->scratch->int1 = val->v.integer;
			return (SNMP_ERR_NOERROR);
		}
		abort();

	    case SNMP_OP_ROLLBACK:
		switch (val->var.subs[sub - 1]) {
		    case LEAF_begemotBridgeDefaultBridgeIf:
			bridge_set_default_name(ctx->scratch->ptr1,
			    ctx->scratch->int1);
			free(ctx->scratch->ptr1);
			break;
		    case LEAF_begemotBridgeDataUpdate:
			bridge_data_maxage = ctx->scratch->int1;
			break;
		}
		return (SNMP_ERR_NOERROR);

	    case SNMP_OP_COMMIT:
		switch (val->var.subs[sub - 1]) {
		    case LEAF_begemotBridgeDefaultBridgeIf:
			free(ctx->scratch->ptr1);
			break;
		    case LEAF_begemotBridgeDataPoll:
			bridge_set_poll_ticks(ctx->scratch->int1 * 100);
			break;
		}
		return (SNMP_ERR_NOERROR);
	}

	abort();
}

/*
 * Bridge mib module initialization hook.
 * Returns 0 on success, < 0 on error.
 */
static int
bridge_init(struct lmodule * mod, int argc __unused, char *argv[] __unused)
{
	bridge_module = mod;

	if (bridge_kmod_load() < 0)
		return (-1);

	if (bridge_ioctl_init() < 0)
		return (-1);

	/* Register to get creation messages for bridge interfaces. */
	if (mib_register_newif(bridge_attach_newif, bridge_module)) {
		syslog(LOG_ERR, "Cannot register newif function: %s",
		    strerror(errno));
		return (-1);
	}

	return (0);
}

/*
 * Bridge mib module finalization hook.
 */
static int
bridge_fini(void)
{
	mib_unregister_newif(bridge_module);
	or_unregister(reg_bridge);

	if (bridge_data_timer != NULL) {
		timer_stop(bridge_data_timer);
		bridge_data_timer = NULL;
	}

	if (bridge_tc_timer != NULL) {
		timer_stop(bridge_tc_timer);
		bridge_tc_timer = NULL;
	}

	bridge_ifs_fini();
	bridge_ports_fini();
	bridge_addrs_fini();

	return (0);
}

/*
 * Bridge mib module start operation.
 */
static void
bridge_start(void)
{
	reg_bridge = or_register(&oid_dot1Bridge,
	    "The IETF MIB for Bridges (RFC 4188).", bridge_module);

	bridge_data_timer = timer_start_repeat(bridge_poll_ticks,
	    bridge_poll_ticks, bridge_update_all, NULL, bridge_module);

	bridge_tc_timer = timer_start_repeat(bridge_tc_poll_ticks,
	    bridge_tc_poll_ticks, bridge_update_tc_time, NULL, bridge_module);
}

static void
bridge_dump(void)
{
	struct bridge_if *bif;

	if ((bif = bridge_get_default()) == NULL)
		syslog(LOG_ERR, "Dump: no default bridge interface");
	else
		syslog(LOG_ERR, "Dump: default bridge interface %s",
		     bif->bif_name);

	bridge_ifs_dump();
	bridge_pf_dump();
}

const struct snmp_module config = {
	.comment = "This module implements the bridge mib (RFC 4188).",
	.init =		bridge_init,
	.fini =		bridge_fini,
	.start =	bridge_start,
	.tree =		bridge_ctree,
	.dump =		bridge_dump,
	.tree_size =	bridge_CTREE_SIZE,
};