forked from iwilson16/mstpd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mstp.c
5125 lines (4543 loc) · 152 KB
/
mstp.c
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
/*
* mstp.c State machines from IEEE 802.1Q-2005
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* Authors: Vitalii Demianets <[email protected]>
*/
/* NOTE: The standard messes up Hello_Time timer management.
* The portTimes and msgTimes structures have it, while
* designatedTimes, rootTimes and BridgeTimes do not!
* And there are places, where standard says:
* "portTimes = designatedTimes" (13.32)
* "rootTimes = portTimes" (13.26.23)
* ---- Bad IEEE! ----
* For now I decide: All structures will hold Hello_Time,
* because in 802.1D they do.
* Besides, it is necessary for compatibility with old STP implementations.
*/
/* 802.1Q-2005 does not define but widely use variable name newInfoXst.
* From the 802.1s I can guess that it means:
* - "newInfo" when tree is CIST;
* - "newInfoMsti" when tree is not CIST (is MSTI).
* But that is only a guess and I could be wrong here ;)
*/
/* Important coding convention:
* All functions that have dry_run argument must follow the
* return value convention:
* They should return true if state change detected during dry run.
* Otherwise (!dry_run || !state_change) they return false.
*/
#include <string.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <linux/if_bridge.h>
#include <asm/byteorder.h>
#include "mstp.h"
#include "log.h"
#include "driver.h"
#include "config.h"
static void PTSM_tick(port_t *prt);
static bool TCSM_run(per_tree_port_t *ptp, bool dry_run);
static void BDSM_begin(port_t *prt);
static void br_state_machines_begin(bridge_t *br);
static void prt_state_machines_begin(port_t *prt);
static void tree_state_machines_begin(tree_t *tree);
static void br_state_machines_run(bridge_t *br);
static void updtbrAssuRcvdInfoWhile(port_t *prt);
#define FOREACH_PORT_IN_BRIDGE(port, bridge) \
list_for_each_entry((port), &(bridge)->ports, br_list)
#define FOREACH_TREE_IN_BRIDGE(tree, bridge) \
list_for_each_entry((tree), &(bridge)->trees, bridge_list)
#define FOREACH_PTP_IN_TREE(ptp, tree) \
list_for_each_entry((ptp), &(tree)->ports, tree_list)
#define FOREACH_PTP_IN_PORT(ptp, port) \
list_for_each_entry((ptp), &(port)->trees, port_list)
/* 17.20.11 of 802.1D */
#define rstpVersion(br) ((br)->ForceProtocolVersion >= protoRSTP)
/* Bridge assurance is operational only when NetworkPort type is configured
* and the operation status is pointToPoint and version is RSTP/MSTP
*/
#define assurancePort(prt) ((prt)->NetworkPort && (prt)->operPointToPointMAC \
&& (prt)->sendRSTP)
/*
* Recalculate configuration digest. (13.7)
*/
static void RecalcConfigDigest(bridge_t *br)
{
__be16 vid2mstid[MAX_VID + 2];
unsigned char mstp_key[] = HMAC_KEY;
int vid;
vid2mstid[0] = vid2mstid[MAX_VID + 1] = 0;
for(vid = 1; vid <= MAX_VID; ++vid)
vid2mstid[vid] = br->fid2mstid[br->vid2fid[vid]];
hmac_md5((void *)vid2mstid, sizeof(vid2mstid), mstp_key, sizeof(mstp_key),
(caddr_t)br->MstConfigId.s.configuration_digest);
}
/*
* 13.37.1 - Table 13-3
*/
static __u32 compute_pcost(int speed)
{
/* speed is in MB/s*/
if(speed > 0)
return (speed < 20000000) ? 20000000 / speed : 1;
else
return MAX_PATH_COST;
}
static void bridge_default_internal_vars(bridge_t *br)
{
br->uptime = 0;
}
static void tree_default_internal_vars(tree_t *tree)
{
/* 12.8.1.1.3.(b,c,d) */
tree->time_since_topology_change = 0;
tree->topology_change_count = 0;
tree->topology_change = false; /* since all tcWhile are initialized to 0 */
strncpy(tree->topology_change_port, "None", IFNAMSIZ);
strncpy(tree->last_topology_change_port, "None", IFNAMSIZ);
/* The following are initialized in BEGIN state:
* - rootPortId, rootPriority, rootTimes: in Port Role Selection SM
*/
}
static void port_default_internal_vars(port_t *prt)
{
prt->infoInternal = false;
prt->rcvdInternal = false;
prt->rcvdTcAck = false;
prt->rcvdTcn = false;
assign(prt->rapidAgeingWhile, 0u);
assign(prt->brAssuRcvdInfoWhile, 0u);
prt->BaInconsistent = false;
prt->num_rx_bpdu = 0;
prt->num_rx_tcn = 0;
prt->num_tx_bpdu = 0;
prt->num_tx_tcn = 0;
prt->num_trans_fwd = 0;
prt->num_trans_blk = 0;
/* The following are initialized in BEGIN state:
* - mdelayWhile. mcheck, sendRSTP: in Port Protocol Migration SM
* - helloWhen, newInfo, newInfoMsti, txCount: in Port Transmit SM
* - edgeDelayWhile, rcvdBpdu, rcvdRSTP, rcvdSTP : in Port Receive SM
* - operEdge: in Bridge Detection SM
* - tcAck: in Topology Change SM
*/
}
static void ptp_default_internal_vars(per_tree_port_t *ptp)
{
ptp->rcvdTc = false;
ptp->tcProp = false;
ptp->updtInfo = false;
ptp->master = false; /* 13.24.5 */
ptp->disputed = false;
assign(ptp->rcvdInfo, (port_info_t)0);
ptp->mastered = false;
memset(&ptp->msgPriority, 0, sizeof(ptp->msgPriority));
memset(&ptp->msgTimes, 0, sizeof(ptp->msgTimes));
/* The following are initialized in BEGIN state:
* - rcvdMsg: in Port Receive SM
* - fdWhile, rrWhile, rbWhile, role, learn, forward,
* sync, synced, reRoot: in Port Role Transitions SM
* - tcWhile, fdbFlush: Topology Change SM
* - rcvdInfoWhile, proposed, proposing, agree, agreed,
* infoIs, reselect, selected: Port Information SM
* - forwarding, learning: Port State Transition SM
* - selectedRole, designatedPriority, designatedTimes: Port Role Selection SM
*/
}
static tree_t * create_tree(bridge_t *br, __u8 *macaddr, __be16 MSTID)
{
/* Initialize all fields except anchor */
tree_t *tree = calloc(1, sizeof(*tree));
if(!tree)
{
ERROR_BRNAME(br, "Out of memory");
return NULL;
}
tree->bridge = br;
tree->MSTID = MSTID;
INIT_LIST_HEAD(&tree->ports);
memcpy(tree->BridgeIdentifier.s.mac_address, macaddr, ETH_ALEN);
/* 0x8000 = default bridge priority (17.14 of 802.1D) */
tree->BridgeIdentifier.s.priority = __constant_cpu_to_be16(0x8000) | MSTID;
assign(tree->BridgePriority.RootID, tree->BridgeIdentifier);
assign(tree->BridgePriority.RRootID, tree->BridgeIdentifier);
assign(tree->BridgePriority.DesignatedBridgeID, tree->BridgeIdentifier);
/* 13.23.4 */
assign(tree->BridgeTimes.remainingHops, br->MaxHops);
assign(tree->BridgeTimes.Forward_Delay, br->Forward_Delay);
assign(tree->BridgeTimes.Max_Age, br->Max_Age);
assign(tree->BridgeTimes.Message_Age, (__u8)0);
assign(tree->BridgeTimes.Hello_Time, br->Hello_Time);
tree_default_internal_vars(tree);
return tree;
}
static per_tree_port_t * create_ptp(tree_t *tree, port_t *prt)
{
/* Initialize all fields except anchors */
per_tree_port_t *ptp = calloc(1, sizeof(*ptp));
if(!ptp)
{
ERROR_PRTNAME(prt->bridge, prt, "Out of memory");
return NULL;
}
ptp->port = prt;
ptp->tree = tree;
ptp->MSTID = tree->MSTID;
ptp->state = BR_STATE_DISABLED;
/* 0x80 = default port priority (17.14 of 802.1D) */
ptp->portId = __constant_cpu_to_be16(0x8000) | prt->port_number;
assign(ptp->AdminInternalPortPathCost, 0u);
assign(ptp->InternalPortPathCost, compute_pcost(GET_PORT_SPEED(prt)));
/* 802.1Q leaves portPriority and portTimes uninitialized */
assign(ptp->portPriority, tree->BridgePriority);
assign(ptp->portTimes, tree->BridgeTimes);
ptp->calledFromFlushRoutine = false;
ptp_default_internal_vars(ptp);
return ptp;
}
/* External events */
bool MSTP_IN_bridge_create(bridge_t *br, __u8 *macaddr)
{
tree_t *cist;
if (!driver_create_bridge(br, macaddr))
return false;
/* Initialize all fields except sysdeps and anchor */
INIT_LIST_HEAD(&br->ports);
INIT_LIST_HEAD(&br->trees);
br->bridgeEnabled = false;
memset(br->vid2fid, 0, sizeof(br->vid2fid));
memset(br->fid2mstid, 0, sizeof(br->fid2mstid));
assign(br->MstConfigId.s.selector, (__u8)0);
sprintf((char *)br->MstConfigId.s.configuration_name,
"%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX",
macaddr[0], macaddr[1], macaddr[2],
macaddr[3], macaddr[4], macaddr[5]);
assign(br->MstConfigId.s.revision_level, __constant_cpu_to_be16(0));
RecalcConfigDigest(br); /* set br->MstConfigId.s.configuration_digest */
br->ForceProtocolVersion = protoMSTP;
assign(br->MaxHops, (__u8)20); /* 13.37.3 */
assign(br->Forward_Delay, (__u8)15); /* 17.14 of 802.1D */
assign(br->Max_Age, (__u8)20); /* 17.14 of 802.1D */
assign(br->Transmit_Hold_Count, 6u); /* 17.14 of 802.1D */
assign(br->Migrate_Time, 3u); /* 17.14 of 802.1D */
assign(br->Ageing_Time, 300u);/* 8.8.3 Table 8-3 */
assign(br->Hello_Time, (__u8)2); /* 17.14 of 802.1D */
bridge_default_internal_vars(br);
/* Create CIST */
if(!(cist = create_tree(br, macaddr, 0)))
return false;
list_add_tail(&cist->bridge_list, &br->trees);
return true;
}
bool MSTP_IN_port_create_and_add_tail(port_t *prt, __u16 portno)
{
tree_t *tree;
per_tree_port_t *ptp, *nxt;
bridge_t *br = prt->bridge;
if (!driver_create_port(prt, portno))
return false;
/* Initialize all fields except sysdeps and bridge */
INIT_LIST_HEAD(&prt->trees);
prt->port_number = __cpu_to_be16(portno);
assign(prt->AdminExternalPortPathCost, 0u);
/* Default for operP2P is false because by default AdminP2P
* says to auto-detect p2p state, and it is derived from duplex
* and initially port is in down state and in this down state
* duplex is set to false (half) */
prt->AdminP2P = p2pAuto;
prt->operPointToPointMAC = false;
prt->portEnabled = false;
prt->restrictedRole = false; /* 13.25.14 */
prt->restrictedTcn = false; /* 13.25.15 */
assign(prt->ExternalPortPathCost, MAX_PATH_COST); /* 13.37.1 */
prt->AdminEdgePort = false; /* 13.25 */
prt->AutoEdge = true; /* 13.25 */
prt->BpduGuardPort = false;
prt->BpduGuardError = false;
prt->NetworkPort = false;
prt->dontTxmtBpdu = false;
prt->deleted = false;
port_default_internal_vars(prt);
/* Create PerTreePort structures for all existing trees */
FOREACH_TREE_IN_BRIDGE(tree, br)
{
if(!(ptp = create_ptp(tree, prt)))
{
/* Remove and free all previously created entries in port's list */
list_for_each_entry_safe(ptp, nxt, &prt->trees, port_list)
{
list_del(&ptp->port_list);
list_del(&ptp->tree_list);
free(ptp);
}
return false;
}
list_add_tail(&ptp->port_list, &prt->trees);
list_add_tail(&ptp->tree_list, &tree->ports);
}
/* Add new port to the tail of the list in the bridge */
/* NOTE: if one wants add port NOT to the tail of the list of ports,
* one should revise above loop (FOREACH_TREE_IN_BRIDGE)
* because it heavily depends on the fact that port is added to the tail.
*/
list_add_tail(&prt->br_list, &br->ports);
prt_state_machines_begin(prt);
return true;
}
void MSTP_IN_delete_port(port_t *prt)
{
per_tree_port_t *ptp, *nxt;
bridge_t *br = prt->bridge;
driver_delete_port(prt);
prt->deleted = true;
if(prt->portEnabled)
{
prt->portEnabled = false;
br_state_machines_run(br);
}
list_for_each_entry_safe(ptp, nxt, &prt->trees, port_list)
{
list_del(&ptp->port_list);
list_del(&ptp->tree_list);
free(ptp);
}
list_del(&prt->br_list);
br_state_machines_run(br);
}
void MSTP_IN_delete_bridge(bridge_t *br)
{
tree_t *tree, *nxt_tree;
port_t *prt, *nxt_prt;
driver_delete_bridge(br);
br->bridgeEnabled = false;
/* We SHOULD first delete all ports and only THEN delete all tree_t
* structures as the tree_t structure contains the head for the per-port
* list of tree data (tree_t.ports).
* If this list_head will be deleted before all the per_tree_ports
* bad things will happen ;)
*/
list_for_each_entry_safe(prt, nxt_prt, &br->ports, br_list)
{
MSTP_IN_delete_port(prt);
free(prt);
}
list_for_each_entry_safe(tree, nxt_tree, &br->trees, bridge_list)
{
list_del(&tree->bridge_list);
free(tree);
}
}
void MSTP_IN_set_bridge_address(bridge_t *br, __u8 *macaddr)
{
tree_t *tree;
bool changed = false;
FOREACH_TREE_IN_BRIDGE(tree, br)
{
if(0 == memcmp(tree->BridgeIdentifier.s.mac_address, macaddr, ETH_ALEN))
continue;
changed = true;
memcpy(tree->BridgeIdentifier.s.mac_address, macaddr, ETH_ALEN);
tree->BridgePriority.RootID = tree->BridgePriority.RRootID =
tree->BridgePriority.DesignatedBridgeID = tree->BridgeIdentifier;
}
if(changed)
br_state_machines_begin(br);
}
void MSTP_IN_set_bridge_enable(bridge_t *br, bool up)
{
port_t *prt;
per_tree_port_t *ptp;
tree_t *tree;
if(br->bridgeEnabled == up)
return;
br->bridgeEnabled = up;
/* Reset all internal states and variables,
* except those which are user-configurable */
bridge_default_internal_vars(br);
FOREACH_TREE_IN_BRIDGE(tree, br)
{
tree_default_internal_vars(tree);
}
FOREACH_PORT_IN_BRIDGE(prt, br)
{
/* NOTE: Don't check prt->deleted here, as it is imposible condition */
/* NOTE: In the port_default_internal_vars() rapidAgeingWhile will be
* reset, so we should stop rapid ageing procedure here.
*/
if(prt->rapidAgeingWhile)
{
MSTP_OUT_set_ageing_time(prt, br->Ageing_Time);
}
port_default_internal_vars(prt);
FOREACH_PTP_IN_PORT(ptp, prt)
{
if(BR_STATE_DISABLED != ptp->state)
{
MSTP_OUT_set_state(ptp, BR_STATE_DISABLED);
}
ptp_default_internal_vars(ptp);
}
}
br_state_machines_begin(br);
}
void MSTP_IN_set_port_enable(port_t *prt, bool up, int speed, int duplex)
{
__u32 computed_pcost, new_ExternalPathCost, new_InternalPathCost;
per_tree_port_t *ptp;
bool new_p2p;
bool changed = false;
if(up)
{
computed_pcost = compute_pcost(speed);
new_ExternalPathCost = (0 == prt->AdminExternalPortPathCost) ?
computed_pcost
: prt->AdminExternalPortPathCost;
if(prt->ExternalPortPathCost != new_ExternalPathCost)
{
assign(prt->ExternalPortPathCost, new_ExternalPathCost);
changed = true;
}
FOREACH_PTP_IN_PORT(ptp, prt)
{
new_InternalPathCost = (0 == ptp->AdminInternalPortPathCost) ?
computed_pcost
: ptp->AdminInternalPortPathCost;
if(ptp->InternalPortPathCost != new_InternalPathCost)
{
assign(ptp->InternalPortPathCost, new_InternalPathCost);
changed = true;
}
}
switch(prt->AdminP2P)
{
case p2pForceTrue:
new_p2p = true;
break;
case p2pForceFalse:
new_p2p = false;
break;
case p2pAuto:
default:
new_p2p = !!duplex;
break;
}
if(prt->operPointToPointMAC != new_p2p)
{
prt->operPointToPointMAC = new_p2p;
changed = true;
}
if(!prt->portEnabled)
{
prt->portEnabled = true;
prt->BpduGuardError = false;
prt->BaInconsistent = false;
prt->num_rx_bpdu = 0;
prt->num_rx_tcn = 0;
prt->num_tx_bpdu = 0;
prt->num_tx_tcn = 0;
changed = true;
/* When port is enabled, initialize bridge assurance timer,
* so that enough time is given before port is put in
* inconsistent state.
*/
updtbrAssuRcvdInfoWhile(prt);
}
}
else
{
if(prt->portEnabled)
{
prt->portEnabled = false;
changed = true;
}
}
if(changed)
br_state_machines_run(prt->bridge);
}
void MSTP_IN_one_second(bridge_t *br)
{
port_t *prt;
tree_t *tree;
++(br->uptime);
if(!br->bridgeEnabled)
return;
FOREACH_TREE_IN_BRIDGE(tree, br)
if(!(tree->topology_change))
++(tree->time_since_topology_change);
FOREACH_PORT_IN_BRIDGE(prt, br)
{
PTSM_tick(prt);
/* support for rapid ageing */
if(prt->rapidAgeingWhile)
{
if((--(prt->rapidAgeingWhile)) == 0)
{
if(!prt->deleted)
MSTP_OUT_set_ageing_time(prt, br->Ageing_Time);
}
}
}
br_state_machines_run(br);
}
void MSTP_IN_all_fids_flushed(per_tree_port_t *ptp)
{
bridge_t *br = ptp->port->bridge;
ptp->fdbFlush = false;
if(!br->bridgeEnabled)
return;
if(!ptp->calledFromFlushRoutine)
{
TCSM_run(ptp, false /* actual run */);
br_state_machines_run(br);
}
}
/* NOTE: bpdu pointer is unaligned, but it works because
* bpdu_t is packed. Don't try to cast bpdu to non-packed type ;)
*/
void MSTP_IN_rx_bpdu(port_t *prt, bpdu_t *bpdu, int size)
{
int mstis_size;
bridge_t *br = prt->bridge;
++(prt->num_rx_bpdu);
if(prt->BpduGuardPort)
{
prt->BpduGuardError = true;
ERROR_PRTNAME(br, prt,
"Received BPDU on BPDU Guarded Port - Port Down");
MSTP_OUT_shutdown_port(prt);
return;
}
if(!br->bridgeEnabled)
{
INFO_PRTNAME(br, prt, "Received BPDU while bridge is disabled");
return;
}
if(prt->rcvdBpdu)
{
ERROR_PRTNAME(br, prt, "Port hasn't processed previous BPDU");
return;
}
/* 14.4 Validation */
if((TCN_BPDU_SIZE > size) || (0 != bpdu->protocolIdentifier))
{
bpdu_validation_failed:
INFO_PRTNAME(br, prt, "BPDU validation failed");
return;
}
switch(bpdu->bpduType)
{
case bpduTypeTCN:
/* 14.4.b) */
/* Valid TCN BPDU */
bpdu->protocolVersion = protoSTP;
LOG_PRTNAME(br, prt, "received TCN BPDU");
break;
case bpduTypeConfig:
/* 14.4.a) */
if(CONFIG_BPDU_SIZE > size)
goto bpdu_validation_failed;
/* Valid Config BPDU */
bpdu->protocolVersion = protoSTP;
LOG_PRTNAME(br, prt, "received Config BPDU%s",
(bpdu->flags & (1 << offsetTc)) ? ", tcFlag" : ""
);
break;
case bpduTypeRST:
if(protoRSTP == bpdu->protocolVersion)
{ /* 14.4.c) */
if(RST_BPDU_SIZE > size)
goto bpdu_validation_failed;
/* Valid RST BPDU */
/* bpdu->protocolVersion = protoRSTP; */
LOG_PRTNAME(br, prt, "received RST BPDU%s",
(bpdu->flags & (1 << offsetTc)) ? ", tcFlag" : ""
);
break;
}
if(protoMSTP > bpdu->protocolVersion)
goto bpdu_validation_failed;
/* Yes, 802.1Q-2005 says here to check if it contains
* "35 or more octets", not 36! (see 14.4.d).1) )
* That's why I check size against CONFIG_BPDU_SIZE
* and not RST_BPDU_SIZE.
*/
if(CONFIG_BPDU_SIZE > size)
goto bpdu_validation_failed;
mstis_size = __be16_to_cpu(bpdu->version3_len)
- MST_BPDU_VER3LEN_WO_MSTI_MSGS;
if((MST_BPDU_SIZE_WO_MSTI_MSGS > size) || (0 != bpdu->version1_len)
|| (0 > mstis_size)
|| ((MAX_STANDARD_MSTIS * sizeof(msti_configuration_message_t))
< mstis_size)
|| (0 != (mstis_size % sizeof(msti_configuration_message_t)))
)
{ /* 14.4.d) */
/* Valid RST BPDU */
bpdu->protocolVersion = protoRSTP;
LOG_PRTNAME(br, prt, "received RST BPDU");
break;
}
/* 14.4.e) */
/* Valid MST BPDU */
bpdu->protocolVersion = protoMSTP;
prt->rcvdBpduNumOfMstis = mstis_size
/ sizeof(msti_configuration_message_t);
LOG_PRTNAME(br, prt, "received MST BPDU%s with %d MSTIs",
(bpdu->flags & (1 << offsetTc)) ? ", tcFlag" : "",
prt->rcvdBpduNumOfMstis
);
break;
default:
goto bpdu_validation_failed;
}
if((protoSTP == bpdu->protocolVersion) && (bpduTypeTCN == bpdu->bpduType))
{
++(prt->num_rx_tcn);
}
else
{
if(bpdu->flags & (1 << offsetTc))
++(prt->num_rx_tcn);
}
assign(prt->rcvdBpduData, *bpdu);
prt->rcvdBpdu = true;
/* Reset bridge assurance on receipt of valid BPDU */
if(prt->BaInconsistent)
{
prt->BaInconsistent = false;
INFO_PRTNAME(br, prt, "Clear Bridge assurance inconsistency");
}
updtbrAssuRcvdInfoWhile(prt);
br_state_machines_run(br);
}
/* 12.8.1.1 Read CIST Bridge Protocol Parameters */
void MSTP_IN_get_cist_bridge_status(bridge_t *br, CIST_BridgeStatus *status)
{
tree_t *cist = GET_CIST_TREE(br);
assign(status->bridge_id, cist->BridgeIdentifier);
assign(status->time_since_topology_change,
cist->time_since_topology_change);
assign(status->topology_change_count, cist->topology_change_count);
status->topology_change = cist->topology_change;
strncpy(status->topology_change_port, cist->topology_change_port,
IFNAMSIZ);
strncpy(status->last_topology_change_port, cist->last_topology_change_port,
IFNAMSIZ);
assign(status->designated_root, cist->rootPriority.RootID);
assign(status->root_path_cost,
__be32_to_cpu(cist->rootPriority.ExtRootPathCost));
assign(status->regional_root, cist->rootPriority.RRootID);
assign(status->internal_path_cost,
__be32_to_cpu(cist->rootPriority.IntRootPathCost));
assign(status->root_port_id, cist->rootPortId);
assign(status->root_max_age, cist->rootTimes.Max_Age);
assign(status->root_forward_delay, cist->rootTimes.Forward_Delay);
assign(status->bridge_max_age, br->Max_Age);
assign(status->bridge_forward_delay, br->Forward_Delay);
assign(status->max_hops, br->MaxHops);
assign(status->tx_hold_count, br->Transmit_Hold_Count);
status->protocol_version = br->ForceProtocolVersion;
status->enabled = br->bridgeEnabled;
assign(status->bridge_hello_time, br->Hello_Time);
assign(status->Ageing_Time, br->Ageing_Time);
}
/* 12.8.1.2 Read MSTI Bridge Protocol Parameters */
void MSTP_IN_get_msti_bridge_status(tree_t *tree, MSTI_BridgeStatus *status)
{
assign(status->bridge_id, tree->BridgeIdentifier);
assign(status->time_since_topology_change,
tree->time_since_topology_change);
assign(status->topology_change_count, tree->topology_change_count);
status->topology_change = tree->topology_change;
strncpy(status->topology_change_port, tree->topology_change_port,
IFNAMSIZ);
strncpy(status->last_topology_change_port, tree->last_topology_change_port,
IFNAMSIZ);
assign(status->regional_root, tree->rootPriority.RRootID);
assign(status->internal_path_cost,
__be32_to_cpu(tree->rootPriority.IntRootPathCost));
assign(status->root_port_id, tree->rootPortId);
}
/* 12.8.1.3 Set CIST Bridge Protocol Parameters */
int MSTP_IN_set_cist_bridge_config(bridge_t *br, CIST_BridgeConfig *cfg)
{
bool changed, changedBridgeTimes, init;
int r = 0;
__u8 new_forward_delay, new_max_age;
tree_t *tree;
port_t *prt;
per_tree_port_t *ptp;
/* Firstly, validation */
if(cfg->set_bridge_max_age)
{
new_max_age = cfg->bridge_max_age;
if((6 > new_max_age) || (40 < new_max_age))
{
ERROR_BRNAME(br,
"Bridge Max Age must be between 6 and 40 seconds");
r = -1;
}
}
else
new_max_age = br->Max_Age;
if(cfg->set_bridge_forward_delay)
{
new_forward_delay = cfg->bridge_forward_delay;
if((4 > new_forward_delay) || (30 < new_forward_delay))
{
ERROR_BRNAME(br,
"Bridge Forward Delay must be between 4 and 30 seconds");
r = -1;
}
}
else
new_forward_delay = br->Forward_Delay;
if(cfg->set_bridge_max_age || cfg->set_bridge_forward_delay)
{
if((2 * (new_forward_delay - 1)) < new_max_age)
{
ERROR_BRNAME(br, "Configured Bridge Times don't meet "
"2 * (Bridge Foward Delay - 1 second) >= Bridge Max Age");
r = -1;
}
}
if(cfg->set_protocol_version)
{
switch(cfg->protocol_version)
{
case protoSTP:
case protoRSTP:
case protoMSTP:
break;
default:
ERROR_BRNAME(br, "Bad protocol version (%d)",
cfg->protocol_version);
r = -1;
}
}
if(cfg->set_tx_hold_count)
{
if((1 > cfg->tx_hold_count) || (10 < cfg->tx_hold_count))
{
ERROR_BRNAME(br,
"Transmit Hold Count must be between 1 and 10 seconds");
r = -1;
}
}
if(cfg->set_max_hops)
{
if((6 > cfg->max_hops) || (40 < cfg->max_hops))
{
ERROR_BRNAME(br, "Bridge Max Hops must be between 6 and 40");
r = -1;
}
}
if(cfg->set_bridge_hello_time)
{
if((1 > cfg->bridge_hello_time) || (10 < cfg->bridge_hello_time))
{
ERROR_BRNAME(br, "Bridge Hello Time must be between 1 and 10");
r = -1;
}
}
if(cfg->set_bridge_ageing_time)
{
if((10 > cfg->bridge_ageing_time)||(1000000 < cfg->bridge_ageing_time))
{
ERROR_BRNAME(br,
"Bridge Ageing Time must be between 10 and 1000000 seconds");
r = -1;
}
}
if(r)
return r;
/* Secondly, do set */
changed = changedBridgeTimes = init = false;
if(cfg->set_bridge_max_age || cfg->set_bridge_forward_delay)
{
if(cmp(new_max_age, !=, br->Max_Age)
|| cmp(new_forward_delay, !=, br->Forward_Delay)
)
{
assign(br->Max_Age, new_max_age);
assign(br->Forward_Delay, new_forward_delay);
changed = changedBridgeTimes = true;
}
}
if((cfg->set_protocol_version)
&& (cfg->protocol_version != br->ForceProtocolVersion)
)
{
br->ForceProtocolVersion = cfg->protocol_version;
changed = init = true;
}
if(cfg->set_tx_hold_count)
{
if(cfg->tx_hold_count != br->Transmit_Hold_Count)
{
assign(br->Transmit_Hold_Count, cfg->tx_hold_count);
FOREACH_PORT_IN_BRIDGE(prt, br)
assign(prt->txCount, 0u);
changed = true;
}
}
if(cfg->set_max_hops)
{
if(cfg->max_hops != br->MaxHops)
{
assign(br->MaxHops, cfg->max_hops);
changed = changedBridgeTimes = true;
}
}
if(cfg->set_bridge_hello_time)
{
if(cfg->bridge_hello_time != br->Hello_Time)
{
INFO_BRNAME(br, "bridge hello_time new=%hhu, old=%hhu",
cfg->bridge_hello_time, br->Hello_Time);
assign(br->Hello_Time, cfg->bridge_hello_time);
changed = changedBridgeTimes = true;
}
}
if(cfg->set_bridge_ageing_time)
{
if(cfg->bridge_ageing_time != br->Ageing_Time)
{
INFO_BRNAME(br, "bridge ageing_time new=%u, old=%u",
cfg->bridge_ageing_time, br->Ageing_Time);
assign(br->Ageing_Time, cfg->bridge_ageing_time);
}
}
/* Thirdly, finalize changes */
if(changedBridgeTimes)
{
FOREACH_TREE_IN_BRIDGE(tree, br)
{
assign(tree->BridgeTimes.remainingHops, br->MaxHops);
assign(tree->BridgeTimes.Forward_Delay, br->Forward_Delay);
assign(tree->BridgeTimes.Max_Age, br->Max_Age);
assign(tree->BridgeTimes.Hello_Time, br->Hello_Time);
/* Comment found in rstpd by Srinivas Aji:
* Do this for any change in BridgeTimes.
* Otherwise we fail UNH rstp.op_D test 3.2 since when administratively
* setting BridgeForwardDelay, etc, the values don't propagate from
* rootTimes to designatedTimes immediately without this change.
*/
FOREACH_PTP_IN_TREE(ptp, tree)
{
ptp->selected = false;
ptp->reselect = true;
/* TODO: change this when Hello_Time will be configurable
* per-port. For now, copy Bridge's Hello_Time
* to the port's Hello_Time.
*/
assign(ptp->portTimes.Hello_Time, br->Hello_Time);
}
}
}
if(changed && br->bridgeEnabled)
{
if(init)
br_state_machines_begin(br);
else
br_state_machines_run(br);
}
return 0;
}
/* 12.8.1.4 Set MSTI Bridge Protocol Parameters */
int MSTP_IN_set_msti_bridge_config(tree_t *tree, __u8 bridge_priority)
{
per_tree_port_t *ptp;
__u8 valuePri;
if(15 < bridge_priority)
{
ERROR_BRNAME(tree->bridge,
"MSTI %hu: Bridge Priority must be between 0 and 15",
__be16_to_cpu(tree->MSTID));
return -1;
}
valuePri = bridge_priority << 4;
if(GET_PRIORITY_FROM_IDENTIFIER(tree->BridgeIdentifier) == valuePri)
return 0;
SET_PRIORITY_IN_IDENTIFIER(valuePri, tree->BridgeIdentifier);
tree->BridgePriority.RootID = tree->BridgePriority.RRootID =
tree->BridgePriority.DesignatedBridgeID = tree->BridgeIdentifier;
/* 12.8.1.4.4 do not require reselect, but I think it is needed,
* because 12.8.1.3.4.c) requires it */
FOREACH_PTP_IN_TREE(ptp, tree)
{
ptp->selected = false;
ptp->reselect = true;
}
return 0;
}
/* 12.8.2.1 Read CIST Port Parameters */
void MSTP_IN_get_cist_port_status(port_t *prt, CIST_PortStatus *status)
{
per_tree_port_t *cist = GET_CIST_PTP_FROM_PORT(prt);
/* 12.8.2.2.3 b) */
status->uptime = (signed int)((prt->bridge)->uptime)
- (signed int)(cist->start_time);
status->state = cist->state;
assign(status->port_id, cist->portId);
assign(status->admin_external_port_path_cost,
prt->AdminExternalPortPathCost);
assign(status->external_port_path_cost, prt->ExternalPortPathCost);
assign(status->designated_root, cist->portPriority.RootID);
assign(status->designated_external_cost,
__be32_to_cpu(cist->portPriority.ExtRootPathCost));