Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * 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 ports.
 *
 * $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>

#define	SNMPTREE_TYPES
#include "bridge_tree.h"
#include "bridge_snmp.h"

TAILQ_HEAD(bridge_ports, bridge_port);

/*
 * Free the bridge base ports list.
 */
static void
bridge_ports_free(struct bridge_ports *headp)
{
	struct bridge_port *bp;

	while ((bp = TAILQ_FIRST(headp)) != NULL) {
		TAILQ_REMOVE(headp, bp, b_p);
		free(bp);
	}
}

/*
 * Free the bridge base ports from the base ports list,
 * members of a specified bridge interface only.
 */
static void
bridge_port_memif_free(struct bridge_ports *headp,
	struct bridge_if *bif)
{
	struct bridge_port *bp;

	while (bif->f_bp != NULL && bif->sysindex == bif->f_bp->sysindex) {
		bp = TAILQ_NEXT(bif->f_bp, b_p);
		TAILQ_REMOVE(headp, bif->f_bp, b_p);
		free(bif->f_bp);
		bif->f_bp = bp;
	}
}

/*
 * Insert a port entry in the base port TAILQ starting to search
 * for its place from the position of the first bridge port for the bridge
 * interface. Update the first bridge port if necessary.
 */
static void
bridge_port_insert_at(struct bridge_ports *headp,
	struct bridge_port *bp, struct bridge_port **f_bp)
{
	struct bridge_port *t1;

	assert(f_bp != NULL);

	for (t1 = *f_bp;
	    t1 != NULL && bp->sysindex == t1->sysindex;
	    t1 = TAILQ_NEXT(t1, b_p)) {
		if (bp->if_idx < t1->if_idx) {
			TAILQ_INSERT_BEFORE(t1, bp, b_p);
			if (*f_bp == t1)
				*f_bp = bp;
			return;
		}
	}

	/*
	 * Handle the case when our first port was actually the
	 * last element of the TAILQ.
	 */
	if (t1 == NULL)
		TAILQ_INSERT_TAIL(headp, bp, b_p);
	else
		TAILQ_INSERT_BEFORE(t1, bp, b_p);
}

/*
 * Find a port entry's position in the ports list according
 * to it's parent bridge interface name. Returns a NULL if
 * we should be at the TAILQ head, otherwise the entry after
 * which we should be inserted.
 */
static struct bridge_port *
bridge_port_find_pos(struct bridge_ports *headp, uint32_t b_idx)
{
	uint32_t t_idx;
	struct bridge_port *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, b_p); t1 != NULL; t1 = TAILQ_NEXT(t1, b_p)) {
		if (t1->sysindex != t_idx) {
			if (bridge_compare_sysidx(b_idx, t1->sysindex) < 0)
				return (TAILQ_PREV(t1, bridge_ports, b_p));
			else
				t_idx = t1->sysindex;
		}
	}

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

	return (t1);
}

/*
 * Insert a bridge member interface in the ports TAILQ.
 */
static void
bridge_port_memif_insert(struct bridge_ports *headp,
	struct bridge_port *bp, struct bridge_port **f_bp)
{
	struct bridge_port *temp;

	if (*f_bp != NULL)
		bridge_port_insert_at(headp, bp, f_bp);
	else {
		temp = bridge_port_find_pos(headp, bp->sysindex);

		if (temp == NULL)
			TAILQ_INSERT_HEAD(headp, bp, b_p);
		else
			TAILQ_INSERT_AFTER(headp, temp, bp, b_p);
		*f_bp = bp;
	}
}

/* The global ports list. */
static struct bridge_ports bridge_ports = TAILQ_HEAD_INITIALIZER(bridge_ports);
static time_t ports_list_age;

void
bridge_ports_update_listage(void)
{
	ports_list_age = time(NULL);
}

void
bridge_ports_fini(void)
{
	bridge_ports_free(&bridge_ports);
}

void
bridge_members_free(struct bridge_if *bif)
{
	bridge_port_memif_free(&bridge_ports, bif);
}

/*
 * Find the first port in the ports list.
 */
static struct bridge_port *
bridge_port_first(void)
{
	return (TAILQ_FIRST(&bridge_ports));
}

/*
 * Find the next port in the ports list.
 */
static struct bridge_port *
bridge_port_next(struct bridge_port *bp)
{
	return (TAILQ_NEXT(bp, b_p));
}

/*
 * Find the first member of the specified bridge interface.
 */
struct bridge_port *
bridge_port_bif_first(struct bridge_if *bif)
{
	return (bif->f_bp);
}

/*
 * Find the next member of the specified bridge interface.
 */
struct bridge_port *
bridge_port_bif_next(struct bridge_port *bp)
{
	struct bridge_port *bp_next;

	if ((bp_next = TAILQ_NEXT(bp, b_p)) == NULL ||
	    bp_next->sysindex != bp->sysindex)
		return (NULL);

	return (bp_next);
}

/*
 * Remove a bridge port from the ports list.
 */
void
bridge_port_remove(struct bridge_port *bp, struct bridge_if *bif)
{
	if (bif->f_bp == bp)
		bif->f_bp = bridge_port_bif_next(bp);

	TAILQ_REMOVE(&bridge_ports, bp, b_p);
	free(bp);
}

/*
 * Allocate memory for a new bridge port and insert it
 * in the base ports list. Return a pointer to the port's
 * structure in case we want to do anything else with it.
 */
struct bridge_port *
bridge_new_port(struct mibif *mif, struct bridge_if *bif)
{
	struct bridge_port *bp;

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

	bzero(bp, sizeof(*bp));

	bp->sysindex = bif->sysindex;
	bp->if_idx = mif->index;
	bp->port_no = mif->sysindex;
	strlcpy(bp->p_name, mif->name, IFNAMSIZ);
	bp->circuit = oid_zeroDotZero;

	/*
	 * Initialize all rstpMib specific values to false/default.
	 * These will be set to their true values later if the bridge
	 * supports RSTP.
	 */
	bp->proto_migr = TruthValue_false;
	bp->admin_edge = TruthValue_false;
	bp->oper_edge = TruthValue_false;
	bp->oper_ptp = TruthValue_false;
	bp->admin_ptp = StpPortAdminPointToPointType_auto;

	bridge_port_memif_insert(&bridge_ports, bp, &(bif->f_bp));

	return (bp);
}

/*
 * Update our info from the corresponding mibII interface info.
 */
void
bridge_port_getinfo_mibif(struct mibif *m_if, struct bridge_port *bp)
{
	bp->max_info = m_if->mib.ifmd_data.ifi_mtu;
	bp->in_frames = m_if->mib.ifmd_data.ifi_ipackets;
	bp->out_frames = m_if->mib.ifmd_data.ifi_opackets;
	bp->in_drops = m_if->mib.ifmd_data.ifi_iqdrops;
}

/*
 * Find a port, whose SNMP's mibII ifIndex matches one of the ports,
 * members of the specified bridge interface.
 */
struct bridge_port *
bridge_port_find(int32_t if_idx, struct bridge_if *bif)
{
	struct bridge_port *bp;

	for (bp = bif->f_bp; bp != NULL; bp = TAILQ_NEXT(bp, b_p)) {
		if (bp->sysindex != bif->sysindex) {
			bp = NULL;
			break;
		}

		if (bp->if_idx == if_idx)
			break;
	}

	return (bp);
}

void
bridge_ports_dump(struct bridge_if *bif)
{
	struct bridge_port *bp;

	for (bp = bridge_port_bif_first(bif); bp != NULL;
	    bp = bridge_port_bif_next(bp)) {
		syslog(LOG_ERR, "memif - %s, index - %d",
		bp->p_name, bp->port_no);
	}
}

/*
 * RFC4188 specifics.
 */
int
op_dot1d_base_port(struct snmp_context *c __unused, struct snmp_value *val,
	uint sub, uint iidx __unused, enum snmp_op op)
{
	struct bridge_if *bif;
	struct bridge_port *bp;

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

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

	switch (op) {
		case SNMP_OP_GET:
		    if (val->var.len - sub != 1)
			return (SNMP_ERR_NOSUCHNAME);
		    if ((bp = bridge_port_find(val->var.subs[sub],
			bif)) == NULL)
			    return (SNMP_ERR_NOSUCHNAME);
		    goto get;

		case SNMP_OP_GETNEXT:
		    if (val->var.len - sub == 0) {
			if ((bp = bridge_port_bif_first(bif)) == NULL)
			    return (SNMP_ERR_NOSUCHNAME);
		    } else {
			if ((bp = bridge_port_find(val->var.subs[sub],
			    bif)) == NULL ||
			    (bp = bridge_port_bif_next(bp)) == NULL)
				return (SNMP_ERR_NOSUCHNAME);
		    }
		    val->var.len = sub + 1;
		    val->var.subs[sub] = bp->port_no;
		    goto get;

		case SNMP_OP_SET:
		    return (SNMP_ERR_NOT_WRITEABLE);

		case SNMP_OP_ROLLBACK:
		case SNMP_OP_COMMIT:
		    break;
	}
	abort();

get:
	switch (val->var.subs[sub - 1]) {
	    case LEAF_dot1dBasePort:
		val->v.integer = bp->port_no;
		return (SNMP_ERR_NOERROR);

	    case LEAF_dot1dBasePortIfIndex:
		val->v.integer = bp->if_idx;
		return (SNMP_ERR_NOERROR);

	    case LEAF_dot1dBasePortCircuit:
		val->v.oid = bp->circuit;
		return (SNMP_ERR_NOERROR);

	    case LEAF_dot1dBasePortDelayExceededDiscards:
		val->v.uint32 = bp->dly_ex_drops;
		return (SNMP_ERR_NOERROR);

	    case LEAF_dot1dBasePortMtuExceededDiscards:
		val->v.uint32 = bp->dly_mtu_drops;
		return (SNMP_ERR_NOERROR);
	}

	abort();
}

int
op_dot1d_stp_port(struct snmp_context *ctx, struct snmp_value *val,
	 uint sub, uint iidx __unused, enum snmp_op op)
{
	struct bridge_if *bif;
	struct bridge_port *bp;

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

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

	switch (op) {
		case SNMP_OP_GET:
		    if (val->var.len - sub != 1)
			return (SNMP_ERR_NOSUCHNAME);
		    if ((bp = bridge_port_find(val->var.subs[sub],
			bif)) == NULL)
			    return (SNMP_ERR_NOSUCHNAME);
		    goto get;

		case SNMP_OP_GETNEXT:
		    if (val->var.len - sub == 0) {
			if ((bp = bridge_port_bif_first(bif)) == NULL)
			    return (SNMP_ERR_NOSUCHNAME);
		    } else {
			if ((bp = bridge_port_find(val->var.subs[sub],
			    bif)) == NULL ||
			    (bp = bridge_port_bif_next(bp)) == NULL)
				return (SNMP_ERR_NOSUCHNAME);
		    }
		    val->var.len = sub + 1;
		    val->var.subs[sub] = bp->port_no;
		    goto get;

		case SNMP_OP_SET:
		    if (val->var.len - sub != 1)
			return (SNMP_ERR_NOSUCHNAME);
		    if ((bp = bridge_port_find(val->var.subs[sub],
			bif)) == NULL)
			    return (SNMP_ERR_NOSUCHNAME);

		    switch (val->var.subs[sub - 1]) {
			case LEAF_dot1dStpPortPriority:
			    if (val->v.integer < 0 || val->v.integer > 255)
				return (SNMP_ERR_WRONG_VALUE);

			    ctx->scratch->int1 = bp->priority;
			    if (bridge_port_set_priority(bif->bif_name, bp,
				val->v.integer) < 0)
				return (SNMP_ERR_GENERR);
			    return (SNMP_ERR_NOERROR);

			case LEAF_dot1dStpPortEnable:
			    if (val->v.integer != dot1dStpPortEnable_enabled &&
				val->v.integer != dot1dStpPortEnable_disabled)
				return (SNMP_ERR_WRONG_VALUE);

			    ctx->scratch->int1 = bp->enable;
			    if (bridge_port_set_stp_enable(bif->bif_name,
				bp, val->v.integer) < 0)
				return (SNMP_ERR_GENERR);
			    return (SNMP_ERR_NOERROR);

			case LEAF_dot1dStpPortPathCost:
			    if (val->v.integer < SNMP_PORT_MIN_PATHCOST ||
				val->v.integer > SNMP_PORT_MAX_PATHCOST)
				return (SNMP_ERR_WRONG_VALUE);

			    ctx->scratch->int1 = bp->path_cost;
			    if (bridge_port_set_path_cost(bif->bif_name, bp,
				val->v.integer) < 0)
				return (SNMP_ERR_GENERR);
			    return (SNMP_ERR_NOERROR);

			case LEAF_dot1dStpPort:
			case LEAF_dot1dStpPortState:
			case LEAF_dot1dStpPortDesignatedRoot:
			case LEAF_dot1dStpPortDesignatedCost:
			case LEAF_dot1dStpPortDesignatedBridge:
			case LEAF_dot1dStpPortDesignatedPort:
			case LEAF_dot1dStpPortForwardTransitions:
			    return (SNMP_ERR_NOT_WRITEABLE);
		    }
		    abort();

		case SNMP_OP_ROLLBACK:
		    if ((bp = bridge_port_find(val->var.subs[sub],
			bif)) == NULL)
			    return (SNMP_ERR_GENERR);
		    switch (val->var.subs[sub - 1]) {
			case LEAF_dot1dStpPortPriority:
			    bridge_port_set_priority(bif->bif_name, bp,
				ctx->scratch->int1);
			    break;
			case LEAF_dot1dStpPortEnable:
			    bridge_port_set_stp_enable(bif->bif_name, bp,
				ctx->scratch->int1);
			    break;
			case LEAF_dot1dStpPortPathCost:
			    bridge_port_set_path_cost(bif->bif_name, bp,
				ctx->scratch->int1);
			    break;
		    }
		    return (SNMP_ERR_NOERROR);

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

get:
	switch (val->var.subs[sub - 1]) {
		case LEAF_dot1dStpPort:
			val->v.integer = bp->port_no;
			return (SNMP_ERR_NOERROR);

		case LEAF_dot1dStpPortPriority:
			val->v.integer = bp->priority;
			return (SNMP_ERR_NOERROR);

		case LEAF_dot1dStpPortState:
			val->v.integer = bp->state;
			return (SNMP_ERR_NOERROR);

		case LEAF_dot1dStpPortEnable:
			val->v.integer = bp->enable;
			return (SNMP_ERR_NOERROR);

		case LEAF_dot1dStpPortPathCost:
			val->v.integer = bp->path_cost;
			return (SNMP_ERR_NOERROR);

		case LEAF_dot1dStpPortDesignatedRoot:
			return (string_get(val, bp->design_root,
			    SNMP_BRIDGE_ID_LEN));

		case LEAF_dot1dStpPortDesignatedCost:
			val->v.integer = bp->design_cost;
			return (SNMP_ERR_NOERROR);

		case LEAF_dot1dStpPortDesignatedBridge:
			return (string_get(val, bp->design_bridge,
			    SNMP_BRIDGE_ID_LEN));

		case LEAF_dot1dStpPortDesignatedPort:
			return (string_get(val, bp->design_port, 2));

		case LEAF_dot1dStpPortForwardTransitions:
			val->v.uint32 = bp->fwd_trans;
			return (SNMP_ERR_NOERROR);
	}

	abort();
}

int
op_dot1d_stp_ext_port(struct snmp_context *ctx, struct snmp_value *val,
    uint sub, uint iidx __unused, enum snmp_op op)
{
	struct bridge_if *bif;
	struct bridge_port *bp;

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

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

	switch (op) {
		case SNMP_OP_GET:
		    if (val->var.len - sub != 1)
			return (SNMP_ERR_NOSUCHNAME);
		    if ((bp = bridge_port_find(val->var.subs[sub],
			bif)) == NULL)
			    return (SNMP_ERR_NOSUCHNAME);
		    goto get;

		case SNMP_OP_GETNEXT:
		    if (val->var.len - sub == 0) {
			if ((bp = bridge_port_bif_first(bif)) == NULL)
			    return (SNMP_ERR_NOSUCHNAME);
		    } else {
			if ((bp = bridge_port_find(val->var.subs[sub],
			    bif)) == NULL ||
			    (bp = bridge_port_bif_next(bp)) == NULL)
				return (SNMP_ERR_NOSUCHNAME);
		    }
		    val->var.len = sub + 1;
		    val->var.subs[sub] = bp->port_no;
		    goto get;

		case SNMP_OP_SET:
		    if (val->var.len - sub != 1)
			return (SNMP_ERR_NOSUCHNAME);
		    if ((bp = bridge_port_find(val->var.subs[sub],
			bif)) == NULL)
			    return (SNMP_ERR_NOSUCHNAME);

		    switch (val->var.subs[sub - 1]) {
			case LEAF_dot1dStpPortAdminEdgePort:
			    if (val->v.integer != TruthValue_true &&
				val->v.integer != TruthValue_false)
				return (SNMP_ERR_WRONG_VALUE);

			    ctx->scratch->int1 = bp->admin_edge;
			    if (bridge_port_set_admin_edge(bif->bif_name, bp,
				val->v.integer) < 0)
				return (SNMP_ERR_GENERR);
			    return (SNMP_ERR_NOERROR);

			case LEAF_dot1dStpPortAdminPointToPoint:
			    if (val->v.integer < 0 || val->v.integer >
				StpPortAdminPointToPointType_auto)
				return (SNMP_ERR_WRONG_VALUE);

			    ctx->scratch->int1 = bp->admin_ptp;
			    if (bridge_port_set_admin_ptp(bif->bif_name, bp,
				val->v.integer) < 0)
				return (SNMP_ERR_GENERR);
			    return (SNMP_ERR_NOERROR);

			case LEAF_dot1dStpPortAdminPathCost:
			    if (val->v.integer < SNMP_PORT_MIN_PATHCOST ||
				val->v.integer > SNMP_PORT_MAX_PATHCOST)
				return (SNMP_ERR_WRONG_VALUE);

			    ctx->scratch->int1 = bp->admin_path_cost;
			    if (bridge_port_set_path_cost(bif->bif_name, bp,
				val->v.integer) < 0)
				return (SNMP_ERR_GENERR);
			    return (SNMP_ERR_NOERROR);

			case LEAF_dot1dStpPortProtocolMigration:
			case LEAF_dot1dStpPortOperEdgePort:
			case LEAF_dot1dStpPortOperPointToPoint:
			    return (SNMP_ERR_NOT_WRITEABLE);
		    }
		    abort();

		case SNMP_OP_ROLLBACK:
		    if ((bp = bridge_port_find(val->var.subs[sub],
			bif)) == NULL)
			    return (SNMP_ERR_GENERR);

		    switch (val->var.subs[sub - 1]) {
			case LEAF_dot1dStpPortAdminEdgePort:
			    bridge_port_set_admin_edge(bif->bif_name, bp,
				ctx->scratch->int1);
			    break;
			case LEAF_dot1dStpPortAdminPointToPoint:
			    bridge_port_set_admin_ptp(bif->bif_name, bp,
				ctx->scratch->int1);
			    break;
			case LEAF_dot1dStpPortAdminPathCost:
			    bridge_port_set_path_cost(bif->bif_name, bp,
				ctx->scratch->int1);
			    break;
		    }
		    return (SNMP_ERR_NOERROR);

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

get:
	switch (val->var.subs[sub - 1]) {
		case LEAF_dot1dStpPortProtocolMigration:
			val->v.integer = bp->proto_migr;
			return (SNMP_ERR_NOERROR);

		case LEAF_dot1dStpPortAdminEdgePort:
			val->v.integer = bp->admin_edge;
			return (SNMP_ERR_NOERROR);

		case LEAF_dot1dStpPortOperEdgePort:
			val->v.integer = bp->oper_edge;
			return (SNMP_ERR_NOERROR);

		case LEAF_dot1dStpPortAdminPointToPoint:
			val->v.integer = bp->admin_ptp;
			return (SNMP_ERR_NOERROR);

		case LEAF_dot1dStpPortOperPointToPoint:
			val->v.integer = bp->oper_ptp;
			return (SNMP_ERR_NOERROR);

		case LEAF_dot1dStpPortAdminPathCost:
			val->v.integer = bp->admin_path_cost;
			return (SNMP_ERR_NOERROR);
	}

	abort();
}

int
op_dot1d_tp_port(struct snmp_context *c __unused, struct snmp_value *val,
    uint sub, uint iidx __unused, enum snmp_op op)
{
	struct bridge_if *bif;
	struct bridge_port *bp;

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

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

	switch (op) {
		case SNMP_OP_GET:
		    if (val->var.len - sub != 1)
			return (SNMP_ERR_NOSUCHNAME);
		    if ((bp = bridge_port_find(val->var.subs[sub],
			bif)) == NULL)
			    return (SNMP_ERR_NOSUCHNAME);
		    goto get;

		case SNMP_OP_GETNEXT:
		    if (val->var.len - sub == 0) {
			if ((bp = bridge_port_bif_first(bif)) == NULL)
			    return (SNMP_ERR_NOSUCHNAME);
		    } else {
			if ((bp = bridge_port_find(val->var.subs[sub],
			    bif)) == NULL ||
			    (bp = bridge_port_bif_next(bp)) == NULL)
				return (SNMP_ERR_NOSUCHNAME);
		    }
		    val->var.len = sub + 1;
		    val->var.subs[sub] = bp->port_no;
		    goto get;

		case SNMP_OP_SET:
		    return (SNMP_ERR_NOT_WRITEABLE);

		case SNMP_OP_ROLLBACK:
		case SNMP_OP_COMMIT:
		    break;
	}
	abort();

get:
	switch (val->var.subs[sub - 1]) {
		case LEAF_dot1dTpPort:
			val->v.integer = bp->port_no;
			return (SNMP_ERR_NOERROR);

		case LEAF_dot1dTpPortMaxInfo:
			val->v.integer = bp->max_info;
			return (SNMP_ERR_NOERROR);

		case LEAF_dot1dTpPortInFrames:
			val->v.uint32 = bp->in_frames;
			return (SNMP_ERR_NOERROR);

		case LEAF_dot1dTpPortOutFrames:
			val->v.uint32 = bp->out_frames;
			return (SNMP_ERR_NOERROR);

		case LEAF_dot1dTpPortInDiscards:
			val->v.uint32 = bp->in_drops;
			return (SNMP_ERR_NOERROR);
	}

	abort();
}

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

/*
 * Construct a bridge port entry index.
 */
static int
bridge_port_index_append(struct asn_oid *oid, uint sub,
	const struct bridge_port *bp)
{
	uint i;
	const char *b_name;

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

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

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

	oid->subs[sub + i] = bp->port_no;

	return (0);
}

/*
 * Get the port entry from an entry's index.
 */
static struct bridge_port *
bridge_port_index_get(const struct asn_oid *oid, uint sub, int8_t status)
{
	uint i;
	int32_t port_no;
	char bif_name[IFNAMSIZ];
	struct bridge_if *bif;
	struct bridge_port *bp;

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

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

	port_no = oid->subs[sub + i + 1];

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

	if ((bp = bridge_port_find(port_no, bif)) == NULL ||
	    (status == 0 && bp->status != RowStatus_active))
		return (NULL);

	return (bp);
}

/*
 * Get the next port entry from an entry's index.
 */
static struct bridge_port *
bridge_port_index_getnext(const struct asn_oid *oid, uint sub, int8_t status)
{
	uint i;
	int32_t port_no;
	char bif_name[IFNAMSIZ];
	struct bridge_if *bif;
	struct bridge_port *bp;

	if (oid->len - sub == 0)
		bp = bridge_port_first();
	else {
		if (oid->len - sub != oid->subs[sub] + 2 ||
		    oid->subs[sub] >= IFNAMSIZ)
			return (NULL);

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

		port_no = oid->subs[sub + i + 1];

		if ((bif = bridge_if_find_ifname(bif_name)) == NULL ||
		    (bp = bridge_port_find(port_no, bif)) == NULL)
			return (NULL);

		bp = bridge_port_next(bp);
	}

	if (status == 1)
		return (bp);

	while (bp != NULL) {
		if (bp->status == RowStatus_active)
			break;
		bp = bridge_port_next(bp);
	}

	return (bp);
}

/*
 * Read the bridge name and port index from a ASN OID structure.
 */
static int
bridge_port_index_decode(const struct asn_oid *oid, uint sub,
	char *b_name, int32_t *idx)
{
	uint i;

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

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

	*idx = oid->subs[sub + i + 1];
	return (0);
}

static int
bridge_port_set_status(struct snmp_context *ctx,
	struct snmp_value *val, uint sub)
{
	int32_t if_idx;
	char b_name[IFNAMSIZ];
	struct bridge_if *bif;
	struct bridge_port *bp;
	struct mibif *mif;

	if (bridge_port_index_decode(&val->var, sub, b_name, &if_idx) < 0)
		return (SNMP_ERR_INCONS_VALUE);

	if ((bif = bridge_if_find_ifname(b_name)) == NULL ||
	    (mif = mib_find_if(if_idx)) == NULL)
		return (SNMP_ERR_INCONS_VALUE);

	bp = bridge_port_find(if_idx, bif);

	switch (val->v.integer) {
	    case RowStatus_active:
		if (bp == NULL)
		    return (SNMP_ERR_INCONS_VALUE);

		if (bp->span_enable == 0)
		    return (SNMP_ERR_INCONS_VALUE);

		ctx->scratch->int1 = bp->status;
		bp->status = RowStatus_active;
		break;

	    case RowStatus_notInService:
		if (bp == NULL || bp->span_enable == 0 ||
		    bp->status == RowStatus_active)
			return (SNMP_ERR_INCONS_VALUE);

		ctx->scratch->int1 = bp->status;
		bp->status = RowStatus_notInService;

	    case RowStatus_notReady:
		/* FALLTHROUGH */
	    case RowStatus_createAndGo:
		return (SNMP_ERR_INCONS_VALUE);

	    case RowStatus_createAndWait:
		if (bp != NULL)
		    return (SNMP_ERR_INCONS_VALUE);

		if ((bp = bridge_new_port(mif, bif)) == NULL)
			return (SNMP_ERR_GENERR);

		ctx->scratch->int1 = RowStatus_destroy;
		bp->status = RowStatus_notReady;
		break;

	    case RowStatus_destroy:
		if (bp == NULL)
		    return (SNMP_ERR_INCONS_VALUE);

		ctx->scratch->int1 = bp->status;
		bp->status = RowStatus_destroy;
		break;
	}

	return (SNMP_ERR_NOERROR);
}

static int
bridge_port_rollback_status(struct snmp_context *ctx,
	struct snmp_value *val, uint sub)
{
	int32_t if_idx;
	char b_name[IFNAMSIZ];
	struct bridge_if *bif;
	struct bridge_port *bp;

	if (bridge_port_index_decode(&val->var, sub, b_name, &if_idx) < 0)
		return (SNMP_ERR_GENERR);

	if ((bif = bridge_if_find_ifname(b_name)) == NULL ||
	    (bp = bridge_port_find(if_idx, bif)) == NULL)
		return (SNMP_ERR_GENERR);

	if (ctx->scratch->int1 == RowStatus_destroy)
		bridge_port_remove(bp, bif);
	else
		bp->status = ctx->scratch->int1;

	return (SNMP_ERR_NOERROR);
}

static int
bridge_port_commit_status(struct snmp_value *val, uint sub)
{
	int32_t if_idx;
	char b_name[IFNAMSIZ];
	struct bridge_if *bif;
	struct bridge_port *bp;

	if (bridge_port_index_decode(&val->var, sub, b_name, &if_idx) < 0)
		return (SNMP_ERR_GENERR);

	if ((bif = bridge_if_find_ifname(b_name)) == NULL ||
	    (bp = bridge_port_find(if_idx, bif)) == NULL)
		return (SNMP_ERR_GENERR);

	switch (bp->status) {
		case RowStatus_active:
			if (bridge_port_addm(bp, b_name) < 0)
				return (SNMP_ERR_COMMIT_FAILED);
			break;

		case RowStatus_destroy:
			if (bridge_port_delm(bp, b_name) < 0)
				return (SNMP_ERR_COMMIT_FAILED);
			bridge_port_remove(bp, bif);
			break;
	}

	return (SNMP_ERR_NOERROR);
}

static int
bridge_port_set_span_enable(struct snmp_context *ctx,
		struct snmp_value *val, uint sub)
{
	int32_t if_idx;
	char b_name[IFNAMSIZ];
	struct bridge_if *bif;
	struct bridge_port *bp;
	struct mibif *mif;

	if (val->v.integer != begemotBridgeBaseSpanEnabled_enabled &&
	    val->v.integer != begemotBridgeBaseSpanEnabled_disabled)
		return (SNMP_ERR_BADVALUE);

	if (bridge_port_index_decode(&val->var, sub, b_name, &if_idx) < 0)
		return (SNMP_ERR_INCONS_VALUE);

	if ((bif = bridge_if_find_ifname(b_name)) == NULL)
		return (SNMP_ERR_INCONS_VALUE);

	if ((bp = bridge_port_find(if_idx, bif)) == NULL) {
		if ((mif = mib_find_if(if_idx)) == NULL)
			return (SNMP_ERR_INCONS_VALUE);

		if ((bp = bridge_new_port(mif, bif)) == NULL)
			return (SNMP_ERR_GENERR);

		ctx->scratch->int1 = RowStatus_destroy;
	} else if (bp->status == RowStatus_active) {
		return (SNMP_ERR_INCONS_VALUE);
	} else {
		ctx->scratch->int1 = bp->status;
	}

	bp->span_enable = val->v.integer;
	bp->status = RowStatus_notInService;

	return (SNMP_ERR_NOERROR);
}

int
op_begemot_base_port(struct snmp_context *ctx, struct snmp_value *val,
	uint sub, uint iidx __unused, enum snmp_op op)
{
	int8_t status, which;
	const char *bname;
	struct bridge_port *bp;

	if (time(NULL) - ports_list_age > bridge_get_data_maxage())
		bridge_update_all_ports();

	which = val->var.subs[sub - 1];
	status = 0;

	switch (op) {
	    case SNMP_OP_GET:
		if (which == LEAF_begemotBridgeBaseSpanEnabled ||
		    which == LEAF_begemotBridgeBasePortStatus)
			status = 1;
		if ((bp = bridge_port_index_get(&val->var, sub,
		    status)) == NULL)
			return (SNMP_ERR_NOSUCHNAME);
		goto get;

	    case SNMP_OP_GETNEXT:
		if (which == LEAF_begemotBridgeBaseSpanEnabled ||
		    which == LEAF_begemotBridgeBasePortStatus)
			status = 1;
		if ((bp = bridge_port_index_getnext(&val->var, sub,
		    status)) == NULL ||
		    bridge_port_index_append(&val->var, sub, bp) < 0)
			return (SNMP_ERR_NOSUCHNAME);
		goto get;

	    case SNMP_OP_SET:
		switch (which) {
		    case LEAF_begemotBridgeBaseSpanEnabled:
			return (bridge_port_set_span_enable(ctx, val, sub));

		    case LEAF_begemotBridgeBasePortStatus:
			return (bridge_port_set_status(ctx, val, sub));

		    case LEAF_begemotBridgeBasePortPrivate:
			if ((bp = bridge_port_index_get(&val->var, sub,
			    status)) == NULL)
				return (SNMP_ERR_NOSUCHNAME);
			if ((bname = bridge_if_find_name(bp->sysindex)) == NULL)
				return (SNMP_ERR_GENERR);
			ctx->scratch->int1 = bp->priv_set;
			return (bridge_port_set_private(bname, bp,
			    val->v.integer));

		    case LEAF_begemotBridgeBasePort:
		    case LEAF_begemotBridgeBasePortIfIndex:
		    case LEAF_begemotBridgeBasePortDelayExceededDiscards:
		    case LEAF_begemotBridgeBasePortMtuExceededDiscards:
			return (SNMP_ERR_NOT_WRITEABLE);
		}
		abort();

	    case SNMP_OP_ROLLBACK:
		switch (which) {
		    case LEAF_begemotBridgeBaseSpanEnabled:
			/* FALLTHROUGH */
		    case LEAF_begemotBridgeBasePortStatus:
			return (bridge_port_rollback_status(ctx, val, sub));
		    case LEAF_begemotBridgeBasePortPrivate:
			if ((bp = bridge_port_index_get(&val->var, sub,
			    status)) == NULL)
				return (SNMP_ERR_GENERR);
			if ((bname = bridge_if_find_name(bp->sysindex)) == NULL)
				return (SNMP_ERR_GENERR);
			return (bridge_port_set_private(bname, bp,
			    ctx->scratch->int1));
		}
		return (SNMP_ERR_NOERROR);

	    case SNMP_OP_COMMIT:
		if (which == LEAF_begemotBridgeBasePortStatus)
			return (bridge_port_commit_status(val, sub));

		return (SNMP_ERR_NOERROR);
	}
	abort();

get:
	switch (which) {
	    case LEAF_begemotBridgeBasePort:
		val->v.integer = bp->port_no;
		return (SNMP_ERR_NOERROR);

	    case LEAF_begemotBridgeBasePortIfIndex:
		val->v.integer = bp->if_idx;
		return (SNMP_ERR_NOERROR);

	    case LEAF_begemotBridgeBaseSpanEnabled:
		val->v.integer = bp->span_enable;
		return (SNMP_ERR_NOERROR);

	    case LEAF_begemotBridgeBasePortDelayExceededDiscards:
		val->v.uint32 = bp->dly_ex_drops;
		return (SNMP_ERR_NOERROR);

	    case LEAF_begemotBridgeBasePortMtuExceededDiscards:
		val->v.uint32 = bp->dly_mtu_drops;
		return (SNMP_ERR_NOERROR);

	    case LEAF_begemotBridgeBasePortStatus:
		val->v.integer = bp->status;
		return (SNMP_ERR_NOERROR);

	    case LEAF_begemotBridgeBasePortPrivate:
		val->v.integer = bp->priv_set;
		return (SNMP_ERR_NOERROR);
	}

	abort();
}

int
op_begemot_stp_port(struct snmp_context *ctx, struct snmp_value *val,
	uint sub, uint iidx __unused, enum snmp_op op)
{
	struct bridge_port *bp;
	const char *b_name;

	if (time(NULL) - ports_list_age > bridge_get_data_maxage())
		bridge_update_all_ports();

	switch (op) {
	    case SNMP_OP_GET:
		if ((bp = bridge_port_index_get(&val->var, sub, 0)) == NULL)
		    return (SNMP_ERR_NOSUCHNAME);
		goto get;

	    case SNMP_OP_GETNEXT:
		if ((bp = bridge_port_index_getnext(&val->var, sub, 0)) ==
		    NULL || bridge_port_index_append(&val->var, sub, bp) < 0)
			return (SNMP_ERR_NOSUCHNAME);
		goto get;

	    case SNMP_OP_SET:
		if ((bp = bridge_port_index_get(&val->var, sub, 0)) == NULL)
			return (SNMP_ERR_NOSUCHNAME);
		if ((b_name = bridge_if_find_name(bp->sysindex)) == NULL)
			return (SNMP_ERR_GENERR);

		switch (val->var.subs[sub - 1]) {
		    case LEAF_begemotBridgeStpPortPriority:
			if (val->v.integer < 0 || val->v.integer > 255)
			    return (SNMP_ERR_WRONG_VALUE);

			ctx->scratch->int1 = bp->priority;
			if (bridge_port_set_priority(b_name, bp,
			    val->v.integer) < 0)
			    return (SNMP_ERR_GENERR);
			return (SNMP_ERR_NOERROR);

		    case LEAF_begemotBridgeStpPortEnable:
			if (val->v.integer !=
			    (int32_t)begemotBridgeStpPortEnable_enabled ||
			    val->v.integer !=
			    (int32_t)begemotBridgeStpPortEnable_disabled)
			    return (SNMP_ERR_WRONG_VALUE);

			ctx->scratch->int1 = bp->enable;
			if (bridge_port_set_stp_enable(b_name, bp,
			    val->v.integer) < 0)
			    return (SNMP_ERR_GENERR);
			return (SNMP_ERR_NOERROR);

		    case LEAF_begemotBridgeStpPortPathCost:
			if (val->v.integer < SNMP_PORT_MIN_PATHCOST ||
			    val->v.integer > SNMP_PORT_MAX_PATHCOST)
			    return (SNMP_ERR_WRONG_VALUE);

			ctx->scratch->int1 = bp->path_cost;
			if (bridge_port_set_path_cost(b_name, bp,
			    val->v.integer) < 0)
			    return (SNMP_ERR_GENERR);
			return (SNMP_ERR_NOERROR);

		    case LEAF_begemotBridgeStpPort:
		    case LEAF_begemotBridgeStpPortState:
		    case LEAF_begemotBridgeStpPortDesignatedRoot:
		    case LEAF_begemotBridgeStpPortDesignatedCost:
		    case LEAF_begemotBridgeStpPortDesignatedBridge:
		    case LEAF_begemotBridgeStpPortDesignatedPort:
		    case LEAF_begemotBridgeStpPortForwardTransitions:
			return (SNMP_ERR_NOT_WRITEABLE);
		}
		abort();

	    case SNMP_OP_ROLLBACK:
		if ((bp = bridge_port_index_get(&val->var, sub, 0)) == NULL ||
		    (b_name = bridge_if_find_name(bp->sysindex)) == NULL)
			return (SNMP_ERR_GENERR);

		switch (val->var.subs[sub - 1]) {
		    case LEAF_begemotBridgeStpPortPriority:
			bridge_port_set_priority(b_name, bp,
			    ctx->scratch->int1);
			break;
		    case LEAF_begemotBridgeStpPortEnable:
			bridge_port_set_stp_enable(b_name, bp,
			    ctx->scratch->int1);
			break;
		    case LEAF_begemotBridgeStpPortPathCost:
			bridge_port_set_path_cost(b_name, bp,
			    ctx->scratch->int1);
			break;
		}
		return (SNMP_ERR_NOERROR);

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

get:
	switch (val->var.subs[sub - 1]) {
	    case LEAF_begemotBridgeStpPort:
		val->v.integer = bp->port_no;
		return (SNMP_ERR_NOERROR);

	    case LEAF_begemotBridgeStpPortPriority:
		val->v.integer = bp->priority;
		return (SNMP_ERR_NOERROR);

	    case LEAF_begemotBridgeStpPortState:
		val->v.integer = bp->state;
		return (SNMP_ERR_NOERROR);

	    case LEAF_begemotBridgeStpPortEnable:
		val->v.integer = bp->enable;
		return (SNMP_ERR_NOERROR);

	    case LEAF_begemotBridgeStpPortPathCost:
		val->v.integer = bp->path_cost;
		return (SNMP_ERR_NOERROR);

	    case LEAF_begemotBridgeStpPortDesignatedRoot:
		return (string_get(val, bp->design_root, SNMP_BRIDGE_ID_LEN));

	    case LEAF_begemotBridgeStpPortDesignatedCost:
		val->v.integer = bp->design_cost;
		return (SNMP_ERR_NOERROR);

	    case LEAF_begemotBridgeStpPortDesignatedBridge:
		return (string_get(val, bp->design_bridge, SNMP_BRIDGE_ID_LEN));

	    case LEAF_begemotBridgeStpPortDesignatedPort:
		return (string_get(val, bp->design_port, 2));

	    case LEAF_begemotBridgeStpPortForwardTransitions:
		val->v.uint32 = bp->fwd_trans;
		return (SNMP_ERR_NOERROR);
	}

	abort();
}

int
op_begemot_stp_ext_port(struct snmp_context *ctx, struct snmp_value *val,
    uint sub, uint iidx __unused, enum snmp_op op)
{
	struct bridge_port *bp;
	const char *b_name;

	if (time(NULL) - ports_list_age > bridge_get_data_maxage())
		bridge_update_all_ports();

	switch (op) {
	    case SNMP_OP_GET:
		if ((bp = bridge_port_index_get(&val->var, sub, 0)) == NULL)
		    return (SNMP_ERR_NOSUCHNAME);
		goto get;

	    case SNMP_OP_GETNEXT:
		if ((bp = bridge_port_index_getnext(&val->var, sub, 0)) ==
		    NULL || bridge_port_index_append(&val->var, sub, bp) < 0)
			return (SNMP_ERR_NOSUCHNAME);
		goto get;

	    case SNMP_OP_SET:
		if ((bp = bridge_port_index_get(&val->var, sub, 0)) == NULL)
			return (SNMP_ERR_NOSUCHNAME);
		if ((b_name = bridge_if_find_name(bp->sysindex)) == NULL)
			return (SNMP_ERR_GENERR);

		switch (val->var.subs[sub - 1]) {
		    case LEAF_begemotBridgeStpPortAdminEdgePort:
			if (val->v.integer != TruthValue_true &&
			    val->v.integer != TruthValue_false)
			    return (SNMP_ERR_WRONG_VALUE);

			ctx->scratch->int1 = bp->admin_edge;
			if (bridge_port_set_admin_edge(b_name, bp,
			    val->v.integer) < 0)
			    return (SNMP_ERR_GENERR);
			return (SNMP_ERR_NOERROR);

		    case LEAF_begemotBridgeStpPortAdminPointToPoint:
			if (val->v.integer < 0 || val->v.integer >
			    StpPortAdminPointToPointType_auto)
			    return (SNMP_ERR_WRONG_VALUE);

			ctx->scratch->int1 = bp->admin_ptp;
			if (bridge_port_set_admin_ptp(b_name, bp,
			    val->v.integer) < 0)
			    return (SNMP_ERR_GENERR);
			return (SNMP_ERR_NOERROR);

		    case LEAF_begemotBridgeStpPortAdminPathCost:
			if (val->v.integer < SNMP_PORT_MIN_PATHCOST ||
			    val->v.integer > SNMP_PORT_MAX_PATHCOST)
			    return (SNMP_ERR_WRONG_VALUE);

			ctx->scratch->int1 = bp->admin_path_cost;
			if (bridge_port_set_path_cost(b_name, bp,
			    val->v.integer) < 0)
			    return (SNMP_ERR_GENERR);
			return (SNMP_ERR_NOERROR);

		    case LEAF_begemotBridgeStpPortProtocolMigration:
		    case LEAF_begemotBridgeStpPortOperEdgePort:
		    case LEAF_begemotBridgeStpPortOperPointToPoint:
			return (SNMP_ERR_NOT_WRITEABLE);
		}
		abort();

	    case SNMP_OP_ROLLBACK:
		if ((bp = bridge_port_index_get(&val->var, sub, 0)) == NULL ||
		    (b_name = bridge_if_find_name(bp->sysindex)) == NULL)
			return (SNMP_ERR_GENERR);

		switch (val->var.subs[sub - 1]) {
		    case LEAF_begemotBridgeStpPortAdminEdgePort:
			bridge_port_set_admin_edge(b_name, bp,
			    ctx->scratch->int1);
			break;
		    case LEAF_begemotBridgeStpPortAdminPointToPoint:
			bridge_port_set_admin_ptp(b_name, bp,
			    ctx->scratch->int1);
			break;
		    case LEAF_begemotBridgeStpPortAdminPathCost:
			bridge_port_set_path_cost(b_name, bp,
			    ctx->scratch->int1);
			break;
		}
		return (SNMP_ERR_NOERROR);

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

get:
	switch (val->var.subs[sub - 1]) {
		case LEAF_begemotBridgeStpPortProtocolMigration:
			val->v.integer = bp->proto_migr;
			return (SNMP_ERR_NOERROR);

		case LEAF_begemotBridgeStpPortAdminEdgePort:
			val->v.integer = bp->admin_edge;
			return (SNMP_ERR_NOERROR);

		case LEAF_begemotBridgeStpPortOperEdgePort:
			val->v.integer = bp->oper_edge;
			return (SNMP_ERR_NOERROR);

		case LEAF_begemotBridgeStpPortAdminPointToPoint:
			val->v.integer = bp->admin_ptp;
			return (SNMP_ERR_NOERROR);

		case LEAF_begemotBridgeStpPortOperPointToPoint:
			val->v.integer = bp->oper_ptp;
			return (SNMP_ERR_NOERROR);

		case LEAF_begemotBridgeStpPortAdminPathCost:
			val->v.integer = bp->admin_path_cost;
			return (SNMP_ERR_NOERROR);
	}

	abort();
}

int
op_begemot_tp_port(struct snmp_context *c __unused, struct snmp_value *val,
	uint sub, uint iidx __unused, enum snmp_op op)
{
	struct bridge_port *bp;

	if (time(NULL) - ports_list_age > bridge_get_data_maxage())
		bridge_update_all_ports();

	switch (op) {
	    case SNMP_OP_GET:
		if ((bp = bridge_port_index_get(&val->var, sub, 0)) == NULL)
		    return (SNMP_ERR_NOSUCHNAME);
		goto get;

	    case SNMP_OP_GETNEXT:
		if ((bp = bridge_port_index_getnext(&val->var, sub, 0)) ==
		    NULL || bridge_port_index_append(&val->var, sub, bp) < 0)
		    return (SNMP_ERR_NOSUCHNAME);
		goto get;

	    case SNMP_OP_SET:
		return (SNMP_ERR_NOT_WRITEABLE);

	    case SNMP_OP_ROLLBACK:
	    case SNMP_OP_COMMIT:
		break;
	}
	abort();

get:
	switch (val->var.subs[sub - 1]) {
	    case LEAF_begemotBridgeTpPort:
		val->v.integer = bp->port_no;
		return (SNMP_ERR_NOERROR);

	    case LEAF_begemotBridgeTpPortMaxInfo:
		val->v.integer = bp->max_info;
		return (SNMP_ERR_NOERROR);

	    case LEAF_begemotBridgeTpPortInFrames:
		val->v.uint32 = bp->in_frames;
		return (SNMP_ERR_NOERROR);

	    case LEAF_begemotBridgeTpPortOutFrames:
		val->v.uint32 = bp->out_frames;
		return (SNMP_ERR_NOERROR);

	    case LEAF_begemotBridgeTpPortInDiscards:
		val->v.uint32 = bp->in_drops;
		return (SNMP_ERR_NOERROR);
	}

	abort();
}