summaryrefslogtreecommitdiffstats
path: root/usr.sbin/bsnmpd/modules/snmp_bridge/bridge_addrs.c
blob: fdb0fa36a06d468300af1409d9b192168bfd7509 (plain)
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*-
 * 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.
 * Bridge addresses.
 *
 * $FreeBSD$
 */

#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 <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <syslog.h>

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

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

TAILQ_HEAD(tp_entries, tp_entry);

/*
 * Free the bridge address list.
 */
static void
bridge_tpe_free(struct tp_entries *headp)
{
	struct tp_entry *t;

	while ((t = TAILQ_FIRST(headp)) != NULL) {
		TAILQ_REMOVE(headp, t, tp_e);
		free(t);
	}
}

/*
 * Free the bridge address entries from the address list,
 * for the specified bridge interface only.
 */
static void
bridge_tpe_bif_free(struct tp_entries *headp,
	struct bridge_if *bif)
{
	struct tp_entry *tp;

	while (bif->f_tpa != NULL && bif->sysindex == bif->f_tpa->sysindex) {
		tp = TAILQ_NEXT(bif->f_tpa, tp_e);
		TAILQ_REMOVE(headp, bif->f_tpa, tp_e);
		free(bif->f_tpa);
		bif->f_tpa = tp;
	}
}

/*
 * Compare two mac addresses.
 * m1 < m2 : -1
 * m1 > m2 : +1
 * m1 = m2 :  0
 */
static int
bridge_compare_macs(const uint8_t *m1, const uint8_t *m2)
{
	int i;

	for (i = 0; i < ETHER_ADDR_LEN; i++) {
		if (m1[i] < m2[i])
			return (-1);
		if (m1[i] > m2[i])
			return (1);
	}

	return (0);
}

/*
 * Insert an address entry in the bridge address TAILQ starting to search
 * for its place from the position of the first bridge address for the bridge
 * interface. Update the first bridge address if neccessary.
 */
static void
bridge_addrs_insert_at(struct tp_entries *headp,
	struct tp_entry *ta, struct tp_entry **f_tpa)
{
	struct tp_entry *t1;

	assert(f_tpa != NULL);

	for (t1 = *f_tpa;
	    t1 != NULL && ta->sysindex == t1->sysindex;
	    t1 = TAILQ_NEXT(t1, tp_e)) {
		if (bridge_compare_macs(ta->tp_addr, t1->tp_addr) < 0) {
			TAILQ_INSERT_BEFORE(t1, ta, tp_e);
			if (*f_tpa == t1)
				(*f_tpa) = ta;
			return;
		}
	}

	if (t1 == NULL)
		TAILQ_INSERT_TAIL(headp, ta, tp_e);
	else
		TAILQ_INSERT_BEFORE(t1, ta, tp_e);
}

/*
 * Find an address entry's possition in the address list
 * according to bridge interface name.
 */
static struct tp_entry *
bridge_addrs_find_pos(struct tp_entries *headp, uint32_t b_idx)
{
	uint32_t t_idx;
	struct tp_entry *t1;

	if ((t1 = TAILQ_FIRST(headp)) == NULL ||
	    bridge_compare_sysidx(b_idx, t1->sysindex) < 0)
		return (NULL);

	t_idx = t1->sysindex;

	for (t1 = TAILQ_NEXT(t1, tp_e); t1 != NULL; t1 = TAILQ_NEXT(t1, tp_e)) {

		if (t1->sysindex != t_idx) {
			if (bridge_compare_sysidx(b_idx, t1->sysindex) < 0)
				return (TAILQ_PREV(t1, tp_entries, tp_e));
			else
				t_idx = t1->sysindex;
		}
	}

	if (t1 == NULL)
		t1 = TAILQ_LAST(headp, tp_entries);

	return (t1);
}

/*
 * Insert a bridge address in the bridge addresses list.
 */
static void
bridge_addrs_bif_insert(struct tp_entries *headp, struct tp_entry *te,
    struct tp_entry **f_tpa)
{
	struct tp_entry *temp;

	if (*f_tpa != NULL)
		bridge_addrs_insert_at(headp, te, f_tpa);
	else {
		temp = bridge_addrs_find_pos(headp, te->sysindex);

		if (temp == NULL)
			TAILQ_INSERT_HEAD(headp, te, tp_e);
		else
			TAILQ_INSERT_AFTER(headp, temp, te, tp_e);
		*f_tpa = te;
	}
}

static struct tp_entries tp_entries = TAILQ_HEAD_INITIALIZER(tp_entries);
static time_t address_list_age;

void
bridge_addrs_update_listage(void)
{
	address_list_age = time(NULL);
}

void
bridge_addrs_fini(void)
{
	bridge_tpe_free(&tp_entries);
}

void
bridge_addrs_free(struct bridge_if *bif)
{
	bridge_tpe_bif_free(&tp_entries, bif);
}

/*
 * Find the first address in the list.
 */
static struct tp_entry *
bridge_addrs_first(void)
{
	return (TAILQ_FIRST(&tp_entries));
}

/*
 * Find the next address in the list.
 */
static struct tp_entry *
bridge_addrs_next(struct tp_entry *te)
{
	return (TAILQ_NEXT(te, tp_e));
}

/*
 * Find the first address, learnt by the specified bridge interface.
 */
struct tp_entry *
bridge_addrs_bif_first(struct bridge_if *bif)
{
	return (bif->f_tpa);
}

/*
 * Find the next address, learnt by the specified bridge interface.
 */
struct tp_entry *
bridge_addrs_bif_next(struct tp_entry *te)
{
	struct tp_entry *te_next;

	if ((te_next = TAILQ_NEXT(te, tp_e)) == NULL ||
	    te_next->sysindex != te->sysindex)
		return (NULL);

	return (te_next);
}

/*
 * Remove a bridge address from the list.
 */
void
bridge_addrs_remove(struct tp_entry *te, struct bridge_if *bif)
{
	if (bif->f_tpa == te)
		bif->f_tpa = bridge_addrs_bif_next(te);

	TAILQ_REMOVE(&tp_entries, te, tp_e);
	free(te);
}

/*
 * Allocate memory for a new bridge address and insert it in the list.
 */
struct tp_entry *
bridge_new_addrs(uint8_t *mac, struct bridge_if *bif)
{
	struct tp_entry *te;

	if ((te = (struct tp_entry *) malloc(sizeof(*te))) == NULL) {
		syslog(LOG_ERR, "bridge new address: failed: %s",
		    strerror(errno));
		return (NULL);
	}

	bzero(te, sizeof(*te));

	te->sysindex = bif->sysindex;
	bcopy(mac, te->tp_addr, ETHER_ADDR_LEN);
	bridge_addrs_bif_insert(&tp_entries, te, &(bif->f_tpa));

	return (te);
}

/*
 * Given a mac address, learnt on a bridge,
 * find the corrsponding TP entry for it.
 */
struct tp_entry *
bridge_addrs_find(uint8_t *mac, struct bridge_if *bif)
{
	struct tp_entry *te;

	for (te = bif->f_tpa; te != NULL; te = TAILQ_NEXT(te, tp_e)) {
		if (te->sysindex != bif->sysindex) {
			te = NULL;
			break;
		}

		if (bridge_compare_macs(te->tp_addr, mac) == 0)
			break;
	}

	return (te);
}

void
bridge_addrs_dump(struct bridge_if *bif)
{
	struct tp_entry *te;

	syslog(LOG_ERR, "Addresses count - %d", bif->num_addrs);
	for (te = bridge_addrs_bif_first(bif); te != NULL;
	    te = bridge_addrs_bif_next(te)) {
		syslog(LOG_ERR, "address %x:%x:%x:%x:%x:%x on port %d.%d",
		    te->tp_addr[0], te->tp_addr[1], te->tp_addr[2],
		    te->tp_addr[3], te->tp_addr[4], te->tp_addr[5],
		    te->sysindex, te->port_no);
	}
}

/*
 * RFC4188 specifics.
 */

/*
 * Construct the SNMP index from the address DST Mac. 
 */
static void
bridge_addrs_index_append(struct asn_oid *oid, uint sub,
	const struct tp_entry *te)
{
	int i;

	oid->len = sub + ETHER_ADDR_LEN + 1;
	oid->subs[sub] = ETHER_ADDR_LEN;

	for (i = 1; i <= ETHER_ADDR_LEN; i++)
		oid->subs[sub + i] = te->tp_addr[i - 1];
}

/*
 * Find the address entry for the SNMP index from the default bridge only.
 */
static struct tp_entry *
bridge_addrs_get(const struct asn_oid *oid, uint sub,
	struct bridge_if *bif)
{
	int i;
	uint8_t tp_addr[ETHER_ADDR_LEN];

	if (oid->len - sub != ETHER_ADDR_LEN + 1 ||
	    oid->subs[sub] != ETHER_ADDR_LEN)
		return (NULL);

	for (i = 0; i < ETHER_ADDR_LEN; i++)
		tp_addr[i] = oid->subs[sub + i + 1];

	return (bridge_addrs_find(tp_addr, bif));
}

/*
 * Find the next address entry for the SNMP index
 * from the default bridge only.
 */
static struct tp_entry *
bridge_addrs_getnext(const struct asn_oid *oid, uint sub,
	struct bridge_if *bif)
{
	int i;
	uint8_t tp_addr[ETHER_ADDR_LEN];
	static struct tp_entry *te;

	if (oid->len - sub == 0)
		return (bridge_addrs_bif_first(bif));

	if (oid->len - sub != ETHER_ADDR_LEN + 1 ||
	    oid->subs[sub] != ETHER_ADDR_LEN)
		return (NULL);

	for (i = 0; i < ETHER_ADDR_LEN; i++)
		tp_addr[i] = oid->subs[sub + i + 1];

	if ((te = bridge_addrs_find(tp_addr, bif)) == NULL)
		return (NULL);

	return (bridge_addrs_bif_next(te));
}

int
op_dot1d_tp_fdb(struct snmp_context *c __unused, struct snmp_value *val,
	uint sub, uint iidx __unused, enum snmp_op op)
{
	int ret;
	struct bridge_if *bif;
	struct tp_entry *te;

	if ((bif = bridge_get_default()) == NULL)
		return (SNMP_ERR_NOSUCHNAME);

	if (time(NULL) - bif->addrs_age > bridge_get_data_maxage() &&
	    bridge_update_addrs(bif) <= 0)
		return (SNMP_ERR_NOSUCHNAME);

	te = NULL; /* Make the compiler happy. */
	switch (op) {
	    case SNMP_OP_GET:
		if ((te = bridge_addrs_get(&val->var, sub, bif)) == NULL)
			return (SNMP_ERR_NOSUCHNAME);
		break;

	    case SNMP_OP_GETNEXT:
		if ((te = bridge_addrs_getnext(&val->var, sub, bif)) == NULL)
			return (SNMP_ERR_NOSUCHNAME);
		bridge_addrs_index_append(&val->var, sub, te);
		break;

	    case SNMP_OP_SET:
		return (SNMP_ERR_NOT_WRITEABLE);

	    case SNMP_OP_ROLLBACK:
	    case SNMP_OP_COMMIT:
		abort();
	}

	ret = SNMP_ERR_NOERROR;
	switch (val->var.subs[sub - 1]) {
		case LEAF_dot1dTpFdbAddress:
			ret = string_get(val, te->tp_addr, ETHER_ADDR_LEN);
			break;
		case LEAF_dot1dTpFdbPort :
			val->v.integer = te->port_no;
			break;
		case LEAF_dot1dTpFdbStatus:
			val->v.integer = te->status;
			break;
	}

	return (ret);
}

/*
 * Private BEGEMOT-BRIDGE-MIB specifics.
 */

/*
 * Construct the SNMP index from the bridge interface name
 * and the address DST Mac. 
 */
static int
bridge_addrs_begemot_index_append(struct asn_oid *oid, uint sub,
	const struct tp_entry *te)
{
	uint i, n_len;
	const char *b_name;

	if ((b_name = bridge_if_find_name(te->sysindex)) == NULL)
		return (-1);

	n_len = strlen(b_name);
	oid->len = sub++;
	oid->subs[oid->len++] = n_len;

	for (i = 1; i <= n_len; i++)
		oid->subs[oid->len++] = b_name[i - 1];

	oid->subs[oid->len++] = ETHER_ADDR_LEN;
	for (i = 1 ; i <= ETHER_ADDR_LEN; i++)
		oid->subs[oid->len++] = te->tp_addr[i - 1];

	return (0);
}

/*
 * Find a bridge address entry by the bridge interface name
 * and the address DST Mac. 
 */
static struct tp_entry *
bridge_addrs_begemot_get(const struct asn_oid *oid, uint sub)
{
	uint i, n_len;
	uint8_t tp_addr[ETHER_ADDR_LEN];
	char bif_name[IFNAMSIZ];
	struct bridge_if *bif;

	n_len = oid->subs[sub];
	if (oid->len - sub != n_len + ETHER_ADDR_LEN + 3 ||
	    n_len >= IFNAMSIZ || oid->subs[sub + n_len + 1] != ETHER_ADDR_LEN)
		return (NULL);

	for (i = 0; i < n_len; i++)
		bif_name[i] = oid->subs[n_len + i + 1];
	bif_name[i] = '\0';

	for (i = 1; i <= ETHER_ADDR_LEN; i++)
		tp_addr[i - 1] = oid->subs[n_len + i + 1];

	if ((bif = bridge_if_find_ifname(bif_name)) == NULL)
		return (NULL);

	return (bridge_addrs_find(tp_addr, bif));
}

/*
 * Find the next bridge address entry by the bridge interface name
 * and the address DST Mac. 
 */
static struct tp_entry *
bridge_addrs_begemot_getnext(const struct asn_oid *oid, uint sub)
{
	uint i, n_len;
	uint8_t tp_addr[ETHER_ADDR_LEN];
	char bif_name[IFNAMSIZ];
	struct bridge_if *bif;
	struct tp_entry *tp;

	if (oid->len - sub == 0)
		return (bridge_addrs_first());

	n_len = oid->subs[sub];
	if (oid->len - sub != n_len + ETHER_ADDR_LEN + 2 ||
	    n_len >= IFNAMSIZ || oid->subs[sub + n_len + 1] != ETHER_ADDR_LEN)
		return (NULL);

	for (i = 1; i <= n_len; i++)
		bif_name[i - 1] = oid->subs[sub + i];

	bif_name[i - 1] = '\0';

	for (i = 1; i <= ETHER_ADDR_LEN; i++)
		tp_addr[i - 1] = oid->subs[sub + n_len + i + 1];

	if ((bif = bridge_if_find_ifname(bif_name)) == NULL ||
	    (tp = bridge_addrs_find(tp_addr, bif)) == NULL)
		return (NULL);

	return (bridge_addrs_next(tp));
}

int
op_begemot_tp_fdb(struct snmp_context *c __unused, struct snmp_value *val,
	uint sub, uint iidx __unused, enum snmp_op op)
{
	int ret;
	struct tp_entry *te = NULL;

	if (time(NULL) - address_list_age > bridge_get_data_maxage())
		bridge_update_all_addrs();

	switch (op) {
	    case SNMP_OP_GET:
		if ((te = bridge_addrs_begemot_get(&val->var, sub)) == NULL)
		    return (SNMP_ERR_NOSUCHNAME);
		break;

	    case SNMP_OP_GETNEXT:
		if ((te = bridge_addrs_begemot_getnext(&val->var,
		    sub)) == NULL ||
		    bridge_addrs_begemot_index_append(&val->var,
		    sub, te) < 0)
			return (SNMP_ERR_NOSUCHNAME);
		break;

	    case SNMP_OP_SET:
		return (SNMP_ERR_NOT_WRITEABLE);

	    case SNMP_OP_ROLLBACK:
	    case SNMP_OP_COMMIT:
		return (SNMP_ERR_NOERROR);
	}

	ret = SNMP_ERR_NOERROR;
	switch (val->var.subs[sub - 1]) {
	    case LEAF_begemotBridgeTpFdbAddress:
		ret = string_get(val, te->tp_addr, ETHER_ADDR_LEN);
		break;
	    case LEAF_begemotBridgeTpFdbPort:
		val->v.integer = te->port_no;
		break;
	    case LEAF_begemotBridgeTpFdbStatus:
		val->v.integer = te->status;
		break;
	}

	return (ret);
}
OpenPOWER on IntegriCloud