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
|
/*-
* 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 OS specific ioctls.
*
* $FreeBSD$
*/
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/module.h>
#include <sys/linker.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#if __FreeBSD_version > 700018
#include <net/bridgestp.h>
#endif
#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_bridgevar.h>
#include <net/if_dl.h>
#include <net/if_mib.h>
#include <net/if_types.h>
#include <netinet/in.h>
#include <errno.h>
#include <ifaddrs.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"
int sock = -1;
int
bridge_ioctl_init(void)
{
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
syslog(LOG_ERR, "cannot open socket : %s", strerror(errno));
return (-1);
}
return (0);
}
/*
* Load the if_bridge.ko module in kernel if not already there.
*/
int
bridge_kmod_load(void)
{
int fileid, modid;
const char mod_name[] = "if_bridge";
struct module_stat mstat;
/* Scan files in kernel. */
mstat.version = sizeof(struct module_stat);
for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
/* Scan modules in file. */
for (modid = kldfirstmod(fileid); modid > 0;
modid = modfnext(modid)) {
if (modstat(modid, &mstat) < 0)
continue;
if (strcmp(mod_name, mstat.name) == 0)
return (0);
}
}
/* Not present - load it. */
if (kldload(mod_name) < 0) {
syslog(LOG_ERR, "failed to load %s kernel module", mod_name);
return (-1);
}
return (1);
}
/************************************************************************
* Bridge interfaces.
*/
/*
* Convert the kernel uint64_t value for a bridge id
*/
static void
snmp_uint64_to_bridgeid(uint64_t id, bridge_id b_id)
{
int i;
u_char *o;
o = (u_char *) &id;
for (i = 0; i < SNMP_BRIDGE_ID_LEN; i++, o++)
b_id[SNMP_BRIDGE_ID_LEN - i - 1] = *o;
}
/*
* Fetch the bridge configuration parameters from the kernel excluding
* it's base MAC address.
*/
static int
bridge_get_conf_param(struct bridge_if *bif)
{
struct ifdrv ifd;
struct ifbrparam b_param;
strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
ifd.ifd_len = sizeof(b_param);
ifd.ifd_data = &b_param;
/* Bridge priority. */
ifd.ifd_cmd = BRDGGPRI;
if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "update bridge: ioctl(BRDGGPRI) failed: %s",
strerror(errno));
return (-1);
}
bif->priority = b_param.ifbrp_prio;
/* Configured max age. */
ifd.ifd_cmd = BRDGGMA;
if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "update bridge: ioctl(BRDGGMA) failed: %s",
strerror(errno));
return (-1);
}
/* Centi-seconds. */
bif->bridge_max_age = 100 * b_param.ifbrp_maxage;
/* Configured hello time. */
ifd.ifd_cmd = BRDGGHT;
if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "update bridge: ioctl(BRDGGHT) failed: %s",
strerror(errno));
return (-1);
}
bif->bridge_hello_time = 100 * b_param.ifbrp_hellotime;
/* Forward delay. */
ifd.ifd_cmd = BRDGGFD;
if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "update bridge: ioctl(BRDGGFD) failed: %s",
strerror(errno));
return (-1);
}
bif->bridge_fwd_delay = 100 * b_param.ifbrp_fwddelay;
/* Number of dropped addresses. */
ifd.ifd_cmd = BRDGGRTE;
if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "update bridge: ioctl(BRDGGRTE) failed: %s",
strerror(errno));
return (-1);
}
bif->lrnt_drops = b_param.ifbrp_cexceeded;
/* Address table timeout. */
ifd.ifd_cmd = BRDGGTO;
if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "update bridge: ioctl(BRDGGTO) failed: %s",
strerror(errno));
return (-1);
}
bif->age_time = b_param.ifbrp_ctime;
/* Address table size. */
ifd.ifd_cmd = BRDGGCACHE;
if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "update bridge: ioctl(BRDGGCACHE) "
"failed: %s", strerror(errno));
return (-1);
}
bif->max_addrs = b_param.ifbrp_csize;
return (0);
}
/*
* Fetch the current bridge STP operational parameters.
* Returns: -1 - on error;
* 0 - old TC time and Root Port values are same;
* 1 - topologyChange notification should be sent;
* 2 - newRoot notification should be sent.
*/
int
bridge_get_op_param(struct bridge_if *bif)
{
int new_root_send;
struct ifdrv ifd;
struct ifbropreq b_req;
strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
ifd.ifd_len = sizeof(b_req);
ifd.ifd_data = &b_req;
ifd.ifd_cmd = BRDGPARAM;
if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "update bridge: ioctl(BRDGPARAM) failed: %s",
strerror(errno));
return (-1);
}
bif->max_age = 100 * b_req.ifbop_maxage;
bif->hello_time = 100 * b_req.ifbop_hellotime;
bif->fwd_delay = 100 * b_req.ifbop_fwddelay;
#if __FreeBSD_version > 700024
bif->stp_version = b_req.ifbop_protocol;
bif->tx_hold_count = b_req.ifbop_holdcount;
#endif
if (b_req.ifbop_root_port == 0 &&
bif->root_port != b_req.ifbop_root_port)
new_root_send = 2;
else
new_root_send = 0;
bif->root_port = b_req.ifbop_root_port;
bif->root_cost = b_req.ifbop_root_path_cost;
snmp_uint64_to_bridgeid(b_req.ifbop_designated_root,
bif->design_root);
if (bif->last_tc_time.tv_sec != b_req.ifbop_last_tc_time.tv_sec) {
bif->top_changes++;
bif->last_tc_time.tv_sec = b_req.ifbop_last_tc_time.tv_sec;
bif->last_tc_time.tv_usec = b_req.ifbop_last_tc_time.tv_usec;
/*
* "The trap is not sent if a (begemotBridge)NewRoot
* trap is sent for the same transition."
*/
if (new_root_send == 0)
return (1);
}
return (new_root_send);
}
int
bridge_getinfo_bif(struct bridge_if *bif)
{
if (bridge_get_conf_param(bif) < 0)
return (-1);
return (bridge_get_op_param(bif));
}
int
bridge_set_priority(struct bridge_if *bif, int32_t priority)
{
struct ifdrv ifd;
struct ifbrparam b_param;
strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
ifd.ifd_len = sizeof(b_param);
ifd.ifd_data = &b_param;
b_param.ifbrp_prio = (uint32_t) priority;
ifd.ifd_cmd = BRDGSPRI;
if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "set bridge param: ioctl(BRDGSPRI) "
"failed: %s", strerror(errno));
return (-1);
}
/*
* Re-fetching the data from the driver after that might be a good
* idea, since changing our bridge's priority should invoke
* recalculation of the active spanning tree topology in the network.
*/
bif->priority = priority;
return (0);
}
/*
* Convert 1/100 of seconds to 1/256 of seconds.
* Timeout ::= TEXTUAL-CONVENTION.
* To convert a Timeout value into a value in units of
* 1/256 seconds, the following algorithm should be used:
* b = floor( (n * 256) / 100)
* The conversion to 1/256 of a second happens in the kernel -
* just make sure we correctly convert the seconds to Timout
* and vice versa.
*/
static uint32_t
snmp_timeout2_sec(int32_t secs)
{
return (secs / 100);
}
int
bridge_set_maxage(struct bridge_if *bif, int32_t max_age)
{
struct ifdrv ifd;
struct ifbrparam b_param;
strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
ifd.ifd_len = sizeof(b_param);
ifd.ifd_data = &b_param;
b_param.ifbrp_maxage = snmp_timeout2_sec(max_age);
ifd.ifd_cmd = BRDGSMA;
if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "set bridge param: ioctl(BRDGSMA) "
"failed: %s", strerror(errno));
return (-1);
}
bif->bridge_max_age = max_age;
return (0);
}
int
bridge_set_hello_time(struct bridge_if *bif, int32_t hello_time)
{
struct ifdrv ifd;
struct ifbrparam b_param;
strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
ifd.ifd_len = sizeof(b_param);
ifd.ifd_data = &b_param;
b_param.ifbrp_hellotime = snmp_timeout2_sec(hello_time);
ifd.ifd_cmd = BRDGSHT;
if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "set bridge param: ioctl(BRDGSHT) "
"failed: %s", strerror(errno));
return (-1);
}
bif->bridge_hello_time = b_param.ifbrp_hellotime;
return (0);
}
int
bridge_set_forward_delay(struct bridge_if *bif, int32_t fwd_delay)
{
struct ifdrv ifd;
struct ifbrparam b_param;
strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
ifd.ifd_len = sizeof(b_param);
ifd.ifd_data = &b_param;
b_param.ifbrp_fwddelay = snmp_timeout2_sec(fwd_delay);
ifd.ifd_cmd = BRDGSFD;
if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "set bridge param: ioctl(BRDGSFD) "
"failed: %s", strerror(errno));
return (-1);
}
bif->bridge_fwd_delay = b_param.ifbrp_fwddelay;
return (0);
}
int
bridge_set_aging_time(struct bridge_if *bif, int32_t age_time)
{
struct ifdrv ifd;
struct ifbrparam b_param;
strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
ifd.ifd_len = sizeof(b_param);
ifd.ifd_data = &b_param;
b_param.ifbrp_ctime = (uint32_t) age_time;
ifd.ifd_cmd = BRDGSTO;
if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "set bridge param: ioctl(BRDGSTO) "
"failed: %s", strerror(errno));
return (-1);
}
bif->age_time = age_time;
return (0);
}
int
bridge_set_max_cache(struct bridge_if *bif, int32_t max_cache)
{
struct ifdrv ifd;
struct ifbrparam b_param;
strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
ifd.ifd_len = sizeof(b_param);
ifd.ifd_data = &b_param;
b_param.ifbrp_csize = max_cache;
ifd.ifd_cmd = BRDGSCACHE;
if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "set bridge param: ioctl(BRDGSCACHE) "
"failed: %s", strerror(errno));
return (-1);
}
bif->max_addrs = b_param.ifbrp_csize;
return (0);
}
int
bridge_set_tx_hold_count(struct bridge_if *bif __unused,
int32_t tx_hc __unused)
{
#if __FreeBSD_version > 700024
struct ifdrv ifd;
struct ifbrparam b_param;
if (tx_hc < SNMP_BRIDGE_MIN_TXHC || tx_hc > SNMP_BRIDGE_MAX_TXHC)
return (-1);
strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
ifd.ifd_len = sizeof(b_param);
ifd.ifd_data = &b_param;
b_param.ifbrp_txhc = tx_hc;
ifd.ifd_cmd = BRDGSTXHC;
if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "set bridge param: ioctl(BRDGSTXHC) "
"failed: %s", strerror(errno));
return (-1);
}
bif->tx_hold_count = b_param.ifbrp_txhc;
return (0);
#else
return (-1);
#endif
}
int
bridge_set_stp_version(struct bridge_if *bif __unused,
int32_t stp_proto __unused)
{
#if __FreeBSD_version > 700024
struct ifdrv ifd;
struct ifbrparam b_param;
strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
ifd.ifd_len = sizeof(b_param);
ifd.ifd_data = &b_param;
b_param.ifbrp_proto = stp_proto;
ifd.ifd_cmd = BRDGSPROTO;
if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "set bridge param: ioctl(BRDGSPROTO) "
"failed: %s", strerror(errno));
return (-1);
}
bif->stp_version = b_param.ifbrp_proto;
return (0);
#else
return (-1);
#endif
}
/*
* Set the bridge interface status to up/down.
*/
int
bridge_set_if_up(const char* b_name, int8_t up)
{
int flags;
struct ifreq ifr;
bzero(&ifr, sizeof(ifr));
strcpy(ifr.ifr_name, b_name);
if (ioctl(sock, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
syslog(LOG_ERR, "set bridge up: ioctl(SIOCGIFFLAGS) "
"failed: %s", strerror(errno));
return (-1);
}
flags = (ifr.ifr_flags & 0xffff) | (ifr.ifr_flagshigh << 16);
if (up == 1)
flags |= IFF_UP;
else
flags &= ~IFF_UP;
ifr.ifr_flags = flags & 0xffff;
ifr.ifr_flagshigh = flags >> 16;
if (ioctl(sock, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
syslog(LOG_ERR, "set bridge up: ioctl(SIOCSIFFLAGS) "
"failed: %s", strerror(errno));
return (-1);
}
return (0);
}
int
bridge_create(const char *b_name)
{
char *new_name;
struct ifreq ifr;
bzero(&ifr, sizeof(ifr));
strcpy(ifr.ifr_name, b_name);
if (ioctl(sock, SIOCIFCREATE, &ifr) < 0) {
syslog(LOG_ERR, "create bridge: ioctl(SIOCIFCREATE) "
"failed: %s", strerror(errno));
return (-1);
}
if (strcmp(b_name, ifr.ifr_name) == 0)
return (0);
if ((new_name = strdup(b_name)) == NULL) {
syslog(LOG_ERR, "create bridge: strdup() failed");
return (-1);
}
ifr.ifr_data = new_name;
if (ioctl(sock, SIOCSIFNAME, (caddr_t) &ifr) < 0) {
syslog(LOG_ERR, "create bridge: ioctl(SIOCSIFNAME) "
"failed: %s", strerror(errno));
free(new_name);
return (-1);
}
return (0);
}
int
bridge_destroy(const char *b_name)
{
struct ifreq ifr;
bzero(&ifr, sizeof(ifr));
strcpy(ifr.ifr_name, b_name);
if (ioctl(sock, SIOCIFDESTROY, &ifr) < 0) {
syslog(LOG_ERR, "destroy bridge: ioctl(SIOCIFDESTROY) "
"failed: %s", strerror(errno));
return (-1);
}
return (0);
}
/*
* Fetch the bridge base MAC address. Return pointer to the
* buffer containing the MAC address, NULL on failure.
*/
u_char *
bridge_get_basemac(const char *bif_name, u_char *mac, size_t mlen)
{
int len;
char if_name[IFNAMSIZ];
struct ifaddrs *ifap, *ifa;
struct sockaddr_dl sdl;
if (getifaddrs(&ifap) != 0) {
syslog(LOG_ERR, "bridge get mac: getifaddrs() failed - %s",
strerror(errno));
return (NULL);
}
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr->sa_family != AF_LINK)
continue;
/*
* Not just casting because of alignment constraints
* on sparc64 and ia64.
*/
bcopy(ifa->ifa_addr, &sdl, sizeof(struct sockaddr_dl));
if (sdl.sdl_alen > mlen)
continue;
if ((len = sdl.sdl_nlen) >= IFNAMSIZ)
len = IFNAMSIZ - 1;
bcopy(sdl.sdl_data, if_name, len);
if_name[len] = '\0';
if (strcmp(bif_name, if_name) == 0) {
bcopy(sdl.sdl_data + sdl.sdl_nlen, mac, sdl.sdl_alen);
freeifaddrs(ifap);
return (mac);
}
}
freeifaddrs(ifap);
return (NULL);
}
/************************************************************************
* Bridge ports.
*/
/*
* Convert the kernel STP port state into
* the corresopnding enumerated type from SNMP Bridge MIB.
*/
static int
state2snmp_st(uint8_t ifbr_state)
{
switch (ifbr_state) {
case BSTP_IFSTATE_DISABLED:
return (StpPortState_disabled);
case BSTP_IFSTATE_LISTENING:
return (StpPortState_listening);
case BSTP_IFSTATE_LEARNING:
return (StpPortState_learning);
case BSTP_IFSTATE_FORWARDING:
return (StpPortState_forwarding);
case BSTP_IFSTATE_BLOCKING:
#if __FreeBSD_version > 700024
case BSTP_IFSTATE_DISCARDING:
#endif
return (StpPortState_blocking);
}
return (StpPortState_broken);
}
/*
* Fill in a bridge member information according to data polled from kernel.
*/
static void
bridge_port_getinfo_conf(struct ifbreq *k_info, struct bridge_port *bp)
{
bp->state = state2snmp_st(k_info->ifbr_state);
bp->priority = k_info->ifbr_priority;
/*
* RFC 4188:
* "New implementations should support dot1dStpPortPathCost32.
* If the port path costs exceeds the maximum value of this
* object then this object should report the maximum value,
* namely 65535. Applications should try to read the
* dot1dStpPortPathCost32 object if this object reports
* the maximum value."
*/
#if __FreeBSD_version > 700024
if (k_info->ifbr_ifsflags & IFBIF_BSTP_ADMCOST)
bp->admin_path_cost = k_info->ifbr_path_cost;
else
bp->admin_path_cost = 0;
#endif
bp->path_cost = k_info->ifbr_path_cost;
if (k_info->ifbr_ifsflags & IFBIF_STP)
bp->enable = dot1dStpPortEnable_enabled;
else
bp->enable = dot1dStpPortEnable_disabled;
/* Begemot Bridge MIB only. */
if (k_info->ifbr_ifsflags & IFBIF_SPAN)
bp->span_enable = begemotBridgeBaseSpanEnabled_enabled;
else
bp->span_enable = begemotBridgeBaseSpanEnabled_disabled;
#if __FreeBSD_version > 700024
if (k_info->ifbr_ifsflags & IFBIF_BSTP_ADMEDGE)
bp->admin_edge = TruthValue_true;
else
bp->admin_edge = TruthValue_false;
if (k_info->ifbr_ifsflags & IFBIF_BSTP_EDGE)
bp->oper_edge = TruthValue_true;
else
bp->oper_edge = TruthValue_false;
if (k_info->ifbr_ifsflags & IFBIF_BSTP_AUTOPTP) {
bp->admin_ptp = StpPortAdminPointToPointType_auto;
if (k_info->ifbr_ifsflags & IFBIF_BSTP_PTP)
bp->oper_ptp = TruthValue_true;
else
bp->oper_ptp = TruthValue_false;
} else if (k_info->ifbr_ifsflags & IFBIF_BSTP_PTP) {
bp->admin_ptp = StpPortAdminPointToPointType_forceTrue;
bp->oper_ptp = TruthValue_true;
} else {
bp->admin_ptp = StpPortAdminPointToPointType_forceFalse;
bp->oper_ptp = TruthValue_false;
}
#endif
}
/*
* Fill in a bridge interface STP information according to
* data polled from kernel.
*/
static void
bridge_port_getinfo_opstp(struct ifbpstpreq *bp_stp, struct bridge_port *bp)
{
bp->enable = dot1dStpPortEnable_enabled;
bp->fwd_trans = bp_stp->ifbp_fwd_trans;
bp->design_cost = bp_stp->ifbp_design_cost;
snmp_uint64_to_bridgeid(bp_stp->ifbp_design_root, bp->design_root);
snmp_uint64_to_bridgeid(bp_stp->ifbp_design_bridge, bp->design_bridge);
bcopy(&(bp_stp->ifbp_design_port), &(bp->design_port),
sizeof(uint16_t));
}
/*
* Clear a bridge interface STP information.
*/
static void
bridge_port_clearinfo_opstp(struct bridge_port *bp)
{
if (bp->enable == dot1dStpPortEnable_enabled) {
bp->design_cost = 0;
bzero(&(bp->design_root), sizeof(bridge_id));
bzero(&(bp->design_bridge), sizeof(bridge_id));
bzero(&(bp->design_port), sizeof(port_id));
bp->fwd_trans = 0;
}
bp->enable = dot1dStpPortEnable_disabled;
}
/*
* Set a bridge member priority.
*/
int
bridge_port_set_priority(const char *bif_name, struct bridge_port *bp,
int32_t priority)
{
struct ifdrv ifd;
struct ifbreq b_req;
strlcpy(ifd.ifd_name, bif_name, sizeof(ifd.ifd_name));
ifd.ifd_len = sizeof(b_req);
ifd.ifd_data = &b_req;
strlcpy(b_req.ifbr_ifsname, bp->p_name, sizeof(b_req.ifbr_ifsname));
b_req.ifbr_priority = (uint8_t) priority;
ifd.ifd_cmd = BRDGSIFPRIO;
if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "set member %s param: ioctl(BRDGSIFPRIO) "
"failed: %s", bp->p_name, strerror(errno));
return (-1);
}
bp->priority = priority;
return (0);
}
/*
* Set a bridge member STP-enabled flag.
*/
int
bridge_port_set_stp_enable(const char *bif_name, struct bridge_port *bp,
uint32_t enable)
{
struct ifdrv ifd;
struct ifbreq b_req;
if (bp->enable == enable)
return (0);
bzero(&b_req, sizeof(b_req));
strlcpy(ifd.ifd_name, bif_name, sizeof(ifd.ifd_name));
ifd.ifd_len = sizeof(b_req);
ifd.ifd_data = &b_req;
strlcpy(b_req.ifbr_ifsname, bp->p_name, sizeof(b_req.ifbr_ifsname));
ifd.ifd_cmd = BRDGGIFFLGS;
if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "get member %s param: ioctl(BRDGGIFFLGS) "
"failed: %s", bp->p_name, strerror(errno));
return (-1);
}
if (enable == dot1dStpPortEnable_enabled)
b_req.ifbr_ifsflags |= IFBIF_STP;
else
b_req.ifbr_ifsflags &= ~IFBIF_STP;
ifd.ifd_cmd = BRDGSIFFLGS;
if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "set member %s param: ioctl(BRDGSIFFLGS) "
"failed: %s", bp->p_name, strerror(errno));
return (-1);
}
bp->enable = enable;
return (0);
}
/*
* Set a bridge member STP path cost.
*/
int
bridge_port_set_path_cost(const char *bif_name, struct bridge_port *bp,
int32_t path_cost)
{
struct ifdrv ifd;
struct ifbreq b_req;
#if __FreeBSD_version < 700025
if (path_cost < SNMP_PORT_MIN_PATHCOST ||
path_cost > SNMP_PORT_PATHCOST_OBSOLETE)
return (-2);
#endif
strlcpy(ifd.ifd_name, bif_name, sizeof(ifd.ifd_name));
ifd.ifd_len = sizeof(b_req);
ifd.ifd_data = &b_req;
strlcpy(b_req.ifbr_ifsname, bp->p_name, sizeof(b_req.ifbr_ifsname));
b_req.ifbr_path_cost = path_cost;
ifd.ifd_cmd = BRDGSIFCOST;
if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "set member %s param: ioctl(BRDGSIFCOST) "
"failed: %s", bp->p_name, strerror(errno));
return (-1);
}
#if __FreeBSD_version > 700024
bp->admin_path_cost = path_cost;
#else
bp->path_cost = path_cost;
#endif
return (0);
}
/*
* Set the PonitToPoint status of the link administratively.
*/
int
bridge_port_set_admin_ptp(const char *bif_name __unused,
struct bridge_port *bp __unused, uint32_t admin_ptp __unused)
{
#if __FreeBSD_version > 700024
struct ifdrv ifd;
struct ifbreq b_req;
if (bp->admin_ptp == admin_ptp)
return (0);
bzero(&b_req, sizeof(b_req));
strlcpy(ifd.ifd_name, bif_name, sizeof(ifd.ifd_name));
ifd.ifd_len = sizeof(b_req);
ifd.ifd_data = &b_req;
strlcpy(b_req.ifbr_ifsname, bp->p_name, sizeof(b_req.ifbr_ifsname));
ifd.ifd_cmd = BRDGGIFFLGS;
if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "get member %s param: ioctl(BRDGGIFFLGS) "
"failed: %s", bp->p_name, strerror(errno));
return (-1);
}
switch (admin_ptp) {
case StpPortAdminPointToPointType_forceTrue:
b_req.ifbr_ifsflags &= ~IFBIF_BSTP_AUTOPTP;
b_req.ifbr_ifsflags |= IFBIF_BSTP_PTP;
break;
case StpPortAdminPointToPointType_forceFalse:
b_req.ifbr_ifsflags &= ~IFBIF_BSTP_AUTOPTP;
b_req.ifbr_ifsflags &= ~IFBIF_BSTP_PTP;
break;
case StpPortAdminPointToPointType_auto:
b_req.ifbr_ifsflags |= IFBIF_BSTP_AUTOPTP;
break;
}
ifd.ifd_cmd = BRDGSIFFLGS;
if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "set member %s param: ioctl(BRDGSIFFLGS) "
"failed: %s", bp->p_name, strerror(errno));
return (-1);
}
bp->admin_ptp = admin_ptp;
return (0);
#else
return (-1);
#endif
}
/*
* Set admin edge.
*/
int
bridge_port_set_admin_edge(const char *bif_name __unused,
struct bridge_port *bp __unused, uint32_t enable __unused)
{
#if __FreeBSD_version > 700024
struct ifdrv ifd;
struct ifbreq b_req;
if (bp->admin_edge == enable)
return (0);
bzero(&b_req, sizeof(b_req));
strlcpy(ifd.ifd_name, bif_name, sizeof(ifd.ifd_name));
ifd.ifd_len = sizeof(b_req);
ifd.ifd_data = &b_req;
strlcpy(b_req.ifbr_ifsname, bp->p_name, sizeof(b_req.ifbr_ifsname));
ifd.ifd_cmd = BRDGGIFFLGS;
if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "get member %s param: ioctl(BRDGGIFFLGS) "
"failed: %s", bp->p_name, strerror(errno));
return (-1);
}
if (enable == TruthValue_true) {
b_req.ifbr_ifsflags &= ~IFBIF_BSTP_AUTOEDGE;
b_req.ifbr_ifsflags |= IFBIF_BSTP_EDGE;
} else
b_req.ifbr_ifsflags &= ~IFBIF_BSTP_EDGE;
ifd.ifd_cmd = BRDGSIFFLGS;
if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "set member %s param: ioctl(BRDGSIFFLGS) "
"failed: %s", bp->p_name, strerror(errno));
return (-1);
}
bp->admin_edge = enable;
return (0);
#else
return (-1);
#endif
}
/*
* Add a bridge member port.
*/
int
bridge_port_addm(struct bridge_port *bp, const char *b_name)
{
struct ifdrv ifd;
struct ifbreq b_req;
bzero(&ifd, sizeof(ifd));
bzero(&b_req, sizeof(b_req));
strlcpy(ifd.ifd_name, b_name, sizeof(ifd.ifd_name));
ifd.ifd_len = sizeof(b_req);
ifd.ifd_data = &b_req;
strlcpy(b_req.ifbr_ifsname, bp->p_name, sizeof(b_req.ifbr_ifsname));
if (bp->span_enable == begemotBridgeBaseSpanEnabled_enabled)
ifd.ifd_cmd = BRDGADDS;
else
ifd.ifd_cmd = BRDGADD;
if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "%s - add member : ioctl(%s) failed: %s",
bp->p_name,
(ifd.ifd_cmd == BRDGADDS ? "BRDGADDS" : "BRDGADD"),
strerror(errno));
return (-1);
}
return (0);
}
/*
* Delete a bridge member port.
*/
int
bridge_port_delm(struct bridge_port *bp, const char *b_name)
{
struct ifdrv ifd;
struct ifbreq b_req;
bzero(&ifd, sizeof(ifd));
bzero(&b_req, sizeof(b_req));
strlcpy(ifd.ifd_name, b_name, sizeof(ifd.ifd_name));
ifd.ifd_len = sizeof(b_req);
ifd.ifd_data = &b_req;
strlcpy(b_req.ifbr_ifsname, bp->p_name, sizeof(b_req.ifbr_ifsname));
if (bp->span_enable == begemotBridgeBaseSpanEnabled_enabled)
ifd.ifd_cmd = BRDGDELS;
else
ifd.ifd_cmd = BRDGDEL;
if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "%s - add member : ioctl(%s) failed: %s",
bp->p_name,
(ifd.ifd_cmd == BRDGDELS ? "BRDGDELS" : "BRDGDEL"),
strerror(errno));
return (-1);
}
return (0);
}
/*
* Fetch the bridge member list from kernel.
* Return -1 on error, or buffer len if successful.
*/
static int32_t
bridge_port_get_iflist(struct bridge_if *bif, struct ifbreq **buf)
{
int n = 128;
uint32_t len;
struct ifbreq *ninbuf;
struct ifbifconf ifbc;
struct ifdrv ifd;
*buf = NULL;
strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
ifd.ifd_cmd = BRDGGIFS;
ifd.ifd_len = sizeof(ifbc);
ifd.ifd_data = &ifbc;
for ( ; ; ) {
len = n * sizeof(struct ifbreq);
if ((ninbuf = (struct ifbreq *)realloc(*buf, len)) == NULL) {
syslog(LOG_ERR, "get bridge member list: "
"realloc failed: %s", strerror(errno));
free(*buf);
*buf = NULL;
return (-1);
}
ifbc.ifbic_len = len;
ifbc.ifbic_req = *buf = ninbuf;
if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "get bridge member list: ioctl "
"(BRDGGIFS) failed: %s", strerror(errno));
free(*buf);
buf = NULL;
return (-1);
}
if ((ifbc.ifbic_len + sizeof(struct ifbreq)) < len)
break;
n += 64;
}
return (ifbc.ifbic_len);
}
/*
* Fetch the bridge STP member list from kernel.
* Return -1 on error, or buffer len if successful.
*/
static int32_t
bridge_port_get_ifstplist(struct bridge_if *bif, struct ifbpstpreq **buf)
{
int n = 128;
uint32_t len;
struct ifbpstpreq *ninbuf;
struct ifbpstpconf ifbstp;
struct ifdrv ifd;
*buf = NULL;
strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
ifd.ifd_cmd = BRDGGIFSSTP;
ifd.ifd_len = sizeof(ifbstp);
ifd.ifd_data = &ifbstp;
for ( ; ; ) {
len = n * sizeof(struct ifbpstpreq);
if ((ninbuf = (struct ifbpstpreq *)
realloc(*buf, len)) == NULL) {
syslog(LOG_ERR, "get bridge STP ports list: "
"realloc failed: %s", strerror(errno));
free(*buf);
*buf = NULL;
return (-1);
}
ifbstp.ifbpstp_len = len;
ifbstp.ifbpstp_req = *buf = ninbuf;
if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "get bridge STP ports list: ioctl "
"(BRDGGIFSSTP) failed: %s", strerror(errno));
free(*buf);
buf = NULL;
return (-1);
}
if ((ifbstp.ifbpstp_len + sizeof(struct ifbpstpreq)) < len)
break;
n += 64;
}
return (ifbstp.ifbpstp_len);
}
/*
* Locate a bridge if STP params structure in a buffer.
*/
static struct ifbpstpreq *
bridge_port_find_ifstplist(uint8_t port_no, struct ifbpstpreq *buf,
uint32_t buf_len)
{
uint32_t i;
struct ifbpstpreq *bstp;
for (i = 0; i < buf_len / sizeof(struct ifbpstpreq); i++) {
bstp = buf + i;
if (bstp->ifbp_portno == port_no)
return (bstp);
}
return (NULL);
}
/*
* Read the initial info for all members of a bridge interface.
* Returns the number of ports, 0 - if none, otherwise
* -1 if some other error occured.
*/
int
bridge_getinfo_bif_ports(struct bridge_if *bif)
{
uint32_t i;
int32_t buf_len;
struct ifbreq *b_req_buf, *b_req;
struct ifbpstpreq *bs_req_buf, *bs_req;
struct bridge_port *bp;
struct mibif *m_if;
if ((buf_len = bridge_port_get_iflist(bif, &b_req_buf)) < 0)
return (-1);
for (i = 0; i < buf_len / sizeof(struct ifbreq); i++) {
b_req = b_req_buf + i;
if ((m_if = mib_find_if_sys(b_req->ifbr_portno)) != NULL) {
/* Hopefully we will not fail here. */
if ((bp = bridge_new_port(m_if, bif)) != NULL) {
bp->status = RowStatus_active;
bridge_port_getinfo_conf(b_req, bp);
bridge_port_getinfo_mibif(m_if, bp);
}
} else {
syslog(LOG_ERR, "bridge member %s not present "
"in mibII ifTable", b_req->ifbr_ifsname);
}
}
free(b_req_buf);
if ((buf_len = bridge_port_get_ifstplist(bif, &bs_req_buf)) < 0)
return (-1);
for (bp = bridge_port_bif_first(bif); bp != NULL;
bp = bridge_port_bif_next(bp)) {
if ((bs_req = bridge_port_find_ifstplist(bp->port_no,
bs_req_buf, buf_len)) == NULL)
bridge_port_clearinfo_opstp(bp);
else
bridge_port_getinfo_opstp(bs_req, bp);
}
free(bs_req_buf);
return (i);
}
/*
* Update the information for the bridge interface members.
*/
int
bridge_update_memif(struct bridge_if *bif)
{
int added, updated;
uint32_t i;
int32_t buf_len;
struct ifbreq *b_req_buf, *b_req;
struct ifbpstpreq *bs_req_buf, *bs_req;
struct bridge_port *bp, *bp_next;
struct mibif *m_if;
if ((buf_len = bridge_port_get_iflist(bif, &b_req_buf)) < 0)
return (-1);
added = updated = 0;
#define BP_FOUND 0x01
for (i = 0; i < buf_len / sizeof(struct ifbreq); i++) {
b_req = b_req_buf + i;
if ((m_if = mib_find_if_sys(b_req->ifbr_portno)) == NULL) {
syslog(LOG_ERR, "bridge member %s not present "
"in mibII ifTable", b_req->ifbr_ifsname);
continue;
}
if ((bp = bridge_port_find(m_if->index, bif)) == NULL &&
(bp = bridge_new_port(m_if, bif)) != NULL) {
bp->status = RowStatus_active;
added++;
}
if (bp != NULL) {
updated++;
bridge_port_getinfo_conf(b_req, bp);
bridge_port_getinfo_mibif(m_if, bp);
bp->flags |= BP_FOUND;
}
}
free(b_req_buf);
/* Clean up list. */
for (bp = bridge_port_bif_first(bif); bp != NULL; bp = bp_next) {
bp_next = bridge_port_bif_next(bp);
if ((bp->flags & BP_FOUND) == 0 &&
bp->status == RowStatus_active)
bridge_port_remove(bp, bif);
else
bp->flags |= ~BP_FOUND;
}
#undef BP_FOUND
if ((buf_len = bridge_port_get_ifstplist(bif, &bs_req_buf)) < 0)
return (-1);
for (bp = bridge_port_bif_first(bif); bp != NULL;
bp = bridge_port_bif_next(bp)) {
if ((bs_req = bridge_port_find_ifstplist(bp->port_no,
bs_req_buf, buf_len)) == NULL)
bridge_port_clearinfo_opstp(bp);
else
bridge_port_getinfo_opstp(bs_req, bp);
}
free(bs_req_buf);
bif->ports_age = time(NULL);
return (updated);
}
/************************************************************************
* Bridge addresses.
*/
/*
* Update the bridge address info according to the polled data.
*/
static void
bridge_addrs_info_ifaddrlist(struct ifbareq *ifba, struct tp_entry *tpe)
{
tpe->port_no = if_nametoindex(ifba->ifba_ifsname);
if ((ifba->ifba_flags & IFBAF_TYPEMASK) == IFBAF_STATIC)
tpe->status = TpFdbStatus_mgmt;
else
tpe->status = TpFdbStatus_learned;
}
/*
* Read the bridge addresses from kernel.
* Return -1 on error, or buffer len if successful.
*/
static int32_t
bridge_addrs_getinfo_ifalist(struct bridge_if *bif, struct ifbareq **buf)
{
int n = 128;
uint32_t len;
struct ifbareq *ninbuf;
struct ifbaconf bac;
struct ifdrv ifd;
*buf = NULL;
strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
ifd.ifd_cmd = BRDGRTS;
ifd.ifd_len = sizeof(bac);
ifd.ifd_data = &bac;
for ( ; ; ) {
len = n * sizeof(struct ifbareq);
if ((ninbuf = (struct ifbareq *)realloc(*buf, len)) == NULL) {
syslog(LOG_ERR, "get bridge address list: "
" realloc failed: %s", strerror(errno));
free(*buf);
*buf = NULL;
return (-1);
}
bac.ifbac_len = len;
bac.ifbac_req = *buf = ninbuf;
if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
syslog(LOG_ERR, "get bridge address list: "
"ioctl(BRDGRTS) failed: %s", strerror(errno));
free(*buf);
buf = NULL;
return (-1);
}
if ((bac.ifbac_len + sizeof(struct ifbareq)) < len)
break;
n += 64;
}
return (bac.ifbac_len);
}
/*
* Read the initial info for all addresses on a bridge interface.
* Returns the number of addresses, 0 - if none, otherwise
* -1 if some other error occured.
*/
int
bridge_getinfo_bif_addrs(struct bridge_if *bif)
{
uint32_t i;
int32_t buf_len;
struct ifbareq *addr_req_buf, *addr_req;
struct tp_entry *te;
if ((buf_len = bridge_addrs_getinfo_ifalist(bif, &addr_req_buf)) < 0)
return (-1);
for (i = 0; i < buf_len / sizeof(struct ifbareq); i++) {
addr_req = addr_req_buf + i;
if ((te = bridge_new_addrs(addr_req->ifba_dst, bif)) != NULL)
bridge_addrs_info_ifaddrlist(addr_req, te);
}
free(addr_req_buf);
return (i);
}
/*
* Update the addresses for the bridge interface.
*/
int
bridge_update_addrs(struct bridge_if *bif)
{
int added, updated;
uint32_t i;
int32_t buf_len;
struct tp_entry *te, *te_next;
struct ifbareq *addr_req_buf, *addr_req;
if ((buf_len = bridge_addrs_getinfo_ifalist(bif, &addr_req_buf)) < 0)
return (-1);
added = updated = 0;
#define BA_FOUND 0x01
for (i = 0; i < buf_len / sizeof(struct ifbareq); i++) {
addr_req = addr_req_buf + i;
if ((te = bridge_addrs_find(addr_req->ifba_dst, bif)) == NULL) {
added++;
if ((te = bridge_new_addrs(addr_req->ifba_dst, bif))
== NULL)
continue;
} else
updated++;
bridge_addrs_info_ifaddrlist(addr_req, te);
te-> flags |= BA_FOUND;
}
free(addr_req_buf);
for (te = bridge_addrs_bif_first(bif); te != NULL; te = te_next) {
te_next = bridge_addrs_bif_next(te);
if ((te-> flags & BA_FOUND) == 0)
bridge_addrs_remove(te, bif);
else
te-> flags &= ~BA_FOUND;
}
#undef BA_FOUND
bif->addrs_age = time(NULL);
return (updated + added);
}
/************************************************************************
* Bridge packet filtering.
*/
const char bridge_sysctl[] = "net.link.bridge.";
static struct {
int32_t val;
const char *name;
} bridge_pf_sysctl[] = {
{ 1, "pfil_bridge" },
{ 1, "pfil_member" },
{ 1, "pfil_onlyip" },
{ 0, "ipfw" },
};
int32_t
bridge_get_pfval(uint8_t which)
{
if (which > sizeof(bridge_pf_sysctl) / sizeof(bridge_pf_sysctl[0])
|| which < 1)
return (-1);
return (bridge_pf_sysctl[which - 1].val);
}
int32_t
bridge_do_pfctl(int32_t bridge_ctl, enum snmp_op op, int32_t *val)
{
char mib_name[100];
int32_t i, s_i;
size_t len, s_len;
if (bridge_ctl >= LEAF_begemotBridgeLayer2PfStatus)
return (-2);
if (op == SNMP_OP_SET) {
s_i = *val;
s_len = sizeof(s_i);
} else
s_len = 0;
len = sizeof(i);
strcpy(mib_name, bridge_sysctl);
if (sysctlbyname(strcat(mib_name,
bridge_pf_sysctl[bridge_ctl].name), &i, &len,
(op == SNMP_OP_SET ? &s_i : NULL), s_len) == -1) {
syslog(LOG_ERR, "sysctl(%s%s) failed - %s", bridge_sysctl,
bridge_pf_sysctl[bridge_ctl].name, strerror(errno));
return (-1);
}
bridge_pf_sysctl[bridge_ctl].val = i;
*val = i;
return (i);
}
void
bridge_pf_dump(void)
{
uint8_t i;
for (i = 0; i < sizeof(bridge_pf_sysctl) / sizeof(bridge_pf_sysctl[0]);
i++) {
syslog(LOG_ERR, "%s%s = %d", bridge_sysctl,
bridge_pf_sysctl[i].name, bridge_pf_sysctl[i].val);
}
}
|