summaryrefslogtreecommitdiff
path: root/usr/src/lib/librstp/common/stp_in.c
blob: f8bd6954f03d3f124dcfecddda235cb888f3ddc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
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
/************************************************************************ 
 * RSTP library - Rapid Spanning Tree (802.1t, 802.1w) 
 * Copyright (C) 2001-2003 Optical Access 
 * Author: Alex Rozin 
 * 
 * This file is part of RSTP library. 
 * 
 * RSTP library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by the 
 * Free Software Foundation; version 2.1 
 * 
 * RSTP library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser 
 * General Public License for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with RSTP library; see the file COPYING.  If not, write to the Free 
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
 * 02111-1307, USA. 
 **********************************************************************/

/* This file contains API from an operation system to the RSTP library */

#include "base.h"
#include "stpm.h"
#include "stp_in.h"
#include "stp_to.h"

#define _stp_in_stpm_enable stp_in_stpm_enable

STP_VECTORS_T *stp_vectors;

void *
stp_in_stpm_create (int vlan_id, char* name, int* err_code)
{
  register STPM_T* this;

  /* stp_trace ("stp_in_stpm_create(%s)", name); */
  this = stpapi_stpm_find (vlan_id);
  if (this) { /* it had just been created :( */
    *err_code = STP_Nothing_To_Do;
    return this;
  }

  this = STP_stpm_create (vlan_id, name);
  if (! this) { /* can't create stpm :( */
    *err_code = STP_Cannot_Create_Instance_For_Vlan;
    return NULL;
  }

  *err_code = STP_OK;
  return this;
}

int
_stp_in_stpm_enable (int vlan_id, char* name,
                    UID_STP_MODE_T admin_state)
{
  register STPM_T* this;
  Bool created_here = False;
  int rc, err_code;

  /* stp_trace ("_stp_in_stpm_enable(%s)", name); */
  this = stpapi_stpm_find (vlan_id);
  
  if (STP_DISABLED != admin_state) {
    if (! vlan_id) { /* STP_IN_stop_all (); */
        register STPM_T* stpm;

        for (stpm = STP_stpm_get_the_list (); stpm; stpm = stpm->next) {
          if (STP_DISABLED != stpm->admin_state) {
            STP_OUT_set_hardware_mode (stpm->vlan_id, STP_DISABLED);
            (void) STP_stpm_enable (stpm, STP_DISABLED);
          }
        }
    }
  }

  if (! this) { /* it had not yet been created */
    if (STP_ENABLED == admin_state) {/* try to create it */
      stp_trace ("implicit create to vlan '%s'", name);
      this = stp_in_stpm_create (vlan_id, name, &err_code);
      if (! this) {
        stp_trace ("implicit create to vlan '%s' failed", name);
        return STP_Implicit_Instance_Create_Failed;
      }
      created_here = True;
    } else {/* try to disable nothing ? */
      return 0;
    }
  }

  if (this->admin_state == admin_state) { /* nothing to do :) */
    return 0;
  }

  rc = STP_stpm_enable (this, admin_state);
  if (! rc) {
    STP_OUT_set_hardware_mode (vlan_id, admin_state);
  }

  if (rc && created_here) {
    STP_stpm_delete (this);
  }
    
  return rc;
}


STPM_T *
stpapi_stpm_find (int vlan_id)
{
  register STPM_T* this;

  for (this = STP_stpm_get_the_list (); this; this = this->next)
    if (vlan_id == this->vlan_id)
      return this;

  return NULL;
}

static PORT_T *
_stpapi_port_find (STPM_T* this, int port_index)
{
  register PORT_T* port;

  for (port = this->ports; port; port = port->next)
    if (port_index == port->port_index) {
      return port;
    }

  return NULL;
}


static void
_conv_br_id_2_uid (IN BRIDGE_ID* f, OUT UID_BRIDGE_ID_T* t)
{
  (void) memcpy (t, f, sizeof (UID_BRIDGE_ID_T));
}

static int
_check_stpm_config (IN UID_STP_CFG_T* uid_cfg)
{
  if (uid_cfg->bridge_priority < MIN_BR_PRIO) {
    stp_trace ("%d bridge_priority small", (int) uid_cfg->bridge_priority);
    return STP_Small_Bridge_Priority;
  }

  if (uid_cfg->bridge_priority > MAX_BR_PRIO) {
    stp_trace ("%d bridge_priority large", (int) uid_cfg->bridge_priority);
    return STP_Large_Bridge_Priority;
  }

  if (uid_cfg->hello_time < MIN_BR_HELLOT) {
    stp_trace ("%d hello_time small", (int) uid_cfg->hello_time);
    return STP_Small_Hello_Time;
  }

  if (uid_cfg->hello_time > MAX_BR_HELLOT) {
    stp_trace ("%d hello_time large", (int) uid_cfg->hello_time);
    return STP_Large_Hello_Time;
  }

  if (uid_cfg->max_age < MIN_BR_MAXAGE) {
    stp_trace ("%d max_age small", (int) uid_cfg->max_age);
    return STP_Small_Max_Age;
  }

  if (uid_cfg->max_age > MAX_BR_MAXAGE) {
    stp_trace ("%d max_age large", (int) uid_cfg->max_age);
    return STP_Large_Max_Age;
  }

  if (uid_cfg->forward_delay < MIN_BR_FWDELAY) {
    stp_trace ("%d forward_delay small", (int) uid_cfg->forward_delay);
    return STP_Small_Forward_Delay;
  }

  if (uid_cfg->forward_delay > MAX_BR_FWDELAY) {
    stp_trace ("%d forward_delay large", (int) uid_cfg->forward_delay);
    return STP_Large_Forward_Delay;
  }

  if (2 * (uid_cfg->forward_delay - 1) < uid_cfg->max_age) {
    return STP_Forward_Delay_And_Max_Age_Are_Inconsistent;
  }

  if (uid_cfg->max_age < 2 * (uid_cfg->hello_time + 1)) {
    return STP_Hello_Time_And_Max_Age_Are_Inconsistent;
  }

  return 0;
}

static void
_stp_in_enable_port_on_stpm (STPM_T* stpm, int port_index, Bool enable)
{
  register PORT_T* port;

  port = _stpapi_port_find (stpm, port_index);
  if (! port) return; 
  if (port->portEnabled == enable) {/* nothing to do :) */
    return;
  }

  port->uptime = 0;
  if (enable) { /* clear port statistics */ 
    port->rx_cfg_bpdu_cnt =
    port->rx_rstp_bpdu_cnt =
    port->rx_tcn_bpdu_cnt = 0;
  }  

#ifdef STP_DBG
  if (port->edge->debug) {
    stp_trace ("Port %s became '%s' adminEdge=%c",
        port->port_name, enable ? "enable" : "disable",
        port->adminEdge ? 'Y' : 'N');
  }
#endif

  port->adminEnable = enable;
  STP_port_init (port, stpm, False);

  port->reselect = True;
  port->selected = False;  
}

void 
STP_IN_init (STP_VECTORS_T *vectors)
{
  RSTP_INIT_CRITICAL_PATH_PROTECTIO;
  stp_vectors = vectors;
}

int
STP_IN_stpm_get_cfg (IN int vlan_id, OUT UID_STP_CFG_T* uid_cfg)
{
  register STPM_T* this;

  uid_cfg->field_mask = 0;
  
  RSTP_CRITICAL_PATH_START;  
  this = stpapi_stpm_find (vlan_id);

  if (!this) { /* it had not yet been created :( */
    RSTP_CRITICAL_PATH_END;
    return STP_Vlan_Had_Not_Yet_Been_Created;
  }

  if (this->admin_state != STP_DISABLED) {
    uid_cfg->field_mask |= BR_CFG_STATE;
  }
  uid_cfg->stp_enabled = this->admin_state;

  if (this->ForceVersion != 2) {
    uid_cfg->field_mask |= BR_CFG_FORCE_VER;
  }
  uid_cfg->force_version = this->ForceVersion;

  if (this->BrId.prio != DEF_BR_PRIO) {
    uid_cfg->field_mask |= BR_CFG_PRIO;
  }
  uid_cfg->bridge_priority = this->BrId.prio;

  if (this->BrTimes.MaxAge != DEF_BR_MAXAGE) {
    uid_cfg->field_mask |= BR_CFG_AGE;
  }
  uid_cfg->max_age = this->BrTimes.MaxAge;

  if (this->BrTimes.HelloTime != DEF_BR_HELLOT) {
    uid_cfg->field_mask |= BR_CFG_HELLO;
  }
  uid_cfg->hello_time = this->BrTimes.HelloTime;

  if (this->BrTimes.ForwardDelay != DEF_BR_FWDELAY) {
    uid_cfg->field_mask |= BR_CFG_DELAY;
  }
  uid_cfg->forward_delay = this->BrTimes.ForwardDelay;
  
  uid_cfg->hold_time = TxHoldCount;

  RSTP_CRITICAL_PATH_END;
  return 0;
}
    
int
STP_IN_port_get_cfg (int vlan_id, int port_index, UID_STP_PORT_CFG_T* uid_cfg)
{
  register STPM_T* this;
  register PORT_T* port;
  
  RSTP_CRITICAL_PATH_START;
  this = stpapi_stpm_find (vlan_id);
    
  if (!this) { /* it had not yet been created :( */
    RSTP_CRITICAL_PATH_END;
    return STP_Vlan_Had_Not_Yet_Been_Created;
  }

  port = _stpapi_port_find (this, port_index);
  if (! port) {/* port is absent in the stpm :( */
    RSTP_CRITICAL_PATH_END;
    return STP_Port_Is_Absent_In_The_Vlan;
  }

  uid_cfg->field_mask = 0;

  uid_cfg->port_priority = port->port_id >> 8;
  if (uid_cfg->port_priority != DEF_PORT_PRIO)
    uid_cfg->field_mask |= PT_CFG_PRIO;

  uid_cfg->admin_port_path_cost = port->adminPCost;
  if (uid_cfg->admin_port_path_cost != ADMIN_PORT_PATH_COST_AUTO)
    uid_cfg->field_mask |= PT_CFG_COST;

  uid_cfg->admin_point2point = port->adminPointToPointMac;
  if (uid_cfg->admin_point2point != DEF_P2P)
    uid_cfg->field_mask |= PT_CFG_P2P;

  uid_cfg->admin_edge = port->adminEdge;
  if (uid_cfg->admin_edge != DEF_ADMIN_EDGE)
    uid_cfg->field_mask |= PT_CFG_EDGE;

  uid_cfg->admin_non_stp = port->admin_non_stp;
  if (uid_cfg->admin_non_stp != DEF_ADMIN_NON_STP)
    uid_cfg->field_mask |= PT_CFG_NON_STP;

  if (port->mcheck)
    uid_cfg->field_mask |= PT_CFG_MCHECK;

  RSTP_CRITICAL_PATH_END;
  return 0;
}

int
STP_IN_port_get_state (IN int vlan_id, INOUT UID_STP_PORT_STATE_T* entry)
{
  register STPM_T* this;
  register PORT_T* port;

  RSTP_CRITICAL_PATH_START;
  this = stpapi_stpm_find (vlan_id);

  if (!this) { /* it had not yet been created :( */
    RSTP_CRITICAL_PATH_END;
    return STP_Vlan_Had_Not_Yet_Been_Created;
  }

  port = _stpapi_port_find (this, entry->port_no);
  if (! port) {/* port is absent in the stpm :( */
    RSTP_CRITICAL_PATH_END;
    return STP_Port_Is_Absent_In_The_Vlan;
  }

  entry->port_id = port->port_id;
  if (DisabledPort == port->role) {
    entry->state = UID_PORT_DISABLED;
  } else if (! port->forward && ! port->learn) {
    entry->state = UID_PORT_DISCARDING;
  } else if (! port->forward && port->learn) {
    entry->state = UID_PORT_LEARNING;
  } else {
    entry->state = UID_PORT_FORWARDING;
  }

  entry->uptime = port->uptime;
  entry->path_cost = port->operPCost;
  _conv_br_id_2_uid (&port->portPrio.root_bridge, &entry->designated_root);
  entry->designated_cost = port->portPrio.root_path_cost;
  _conv_br_id_2_uid (&port->portPrio.design_bridge, &entry->designated_bridge);
  entry->designated_port = port->portPrio.design_port;

  switch (port->role) {
    case DisabledPort:   entry->role = ' '; break;
    case AlternatePort:  entry->role = 'A'; break;
    case BackupPort:     entry->role = 'B'; break;
    case RootPort:       entry->role = 'R'; break;
    case DesignatedPort: entry->role = 'D'; break;
    case NonStpPort:     entry->role = '-'; break;
    default:             entry->role = '?'; break;
  }

  if (DisabledPort == port->role || NonStpPort == port->role) {
    (void) memset (&entry->designated_root, 0, sizeof (UID_BRIDGE_ID_T));
    (void) memset (&entry->designated_bridge, 0, sizeof (UID_BRIDGE_ID_T));
    entry->designated_cost = 0;
    entry->designated_port = port->port_id;
  }

  if (DisabledPort == port->role) {
    entry->oper_point2point = (P2P_FORCE_FALSE == port->adminPointToPointMac) ? 0 : 1;
    entry->oper_edge = port->adminEdge;
    entry->oper_stp_neigb = 0;
  } else {
    entry->oper_point2point = port->operPointToPointMac ? 1 : 0;
    entry->oper_edge = port->operEdge                   ? 1 : 0;
    entry->oper_stp_neigb = port->sendRSTP              ? 0 : 1;
  }
  entry->oper_port_path_cost = port->operPCost;

  entry->rx_cfg_bpdu_cnt = port->rx_cfg_bpdu_cnt;
  entry->rx_rstp_bpdu_cnt = port->rx_rstp_bpdu_cnt;
  entry->rx_tcn_bpdu_cnt = port->rx_tcn_bpdu_cnt;

  entry->fdWhile =       port->fdWhile;      /* 17.15.1 */
  entry->helloWhen =     port->helloWhen;    /* 17.15.2 */
  entry->mdelayWhile =   port->mdelayWhile;  /* 17.15.3 */
  entry->rbWhile =       port->rbWhile;      /* 17.15.4 */
  entry->rcvdInfoWhile = port->rcvdInfoWhile;/* 17.15.5 */
  entry->rrWhile =       port->rrWhile;      /* 17.15.6 */
  entry->tcWhile =       port->tcWhile;      /* 17.15.7 */
  entry->txCount =       port->txCount;      /* 17.18.40 */
  entry->lnkWhile =      port->lnkWhile;

  entry->rcvdInfoWhile = port->rcvdInfoWhile;
  entry->top_change_ack = port->tcAck;
  entry->tc = port->tc;
  
  RSTP_CRITICAL_PATH_END;
  return 0; 
}

int
STP_IN_stpm_get_state (IN int vlan_id, OUT UID_STP_STATE_T* entry)
{
  register STPM_T* this;

  RSTP_CRITICAL_PATH_START;
  this = stpapi_stpm_find (vlan_id);

  if (!this) { /* it had not yet been created :( */
    RSTP_CRITICAL_PATH_END;
    return STP_Vlan_Had_Not_Yet_Been_Created;
  }

  (void) strncpy (entry->vlan_name, this->name, NAME_LEN);
  entry->vlan_id = this->vlan_id;
  _conv_br_id_2_uid (&this->rootPrio.root_bridge, &entry->designated_root);
  entry->root_path_cost = this->rootPrio.root_path_cost;
  entry->root_port = this->rootPortId;
  entry->max_age =       this->rootTimes.MaxAge;
  entry->forward_delay = this->rootTimes.ForwardDelay;
  entry->hello_time =    this->rootTimes.HelloTime;

  _conv_br_id_2_uid (&this->BrId, &entry->bridge_id);

  entry->stp_enabled = this->admin_state;

  entry->timeSince_Topo_Change = this->timeSince_Topo_Change;
  entry->Topo_Change_Count = this->Topo_Change_Count;
  entry->Topo_Change = this->Topo_Change;
  
  RSTP_CRITICAL_PATH_END;
  return 0;
}

int
STP_IN_stpm_get_name_by_vlan_id (int vlan_id, char* name, size_t buffsize)
{
  register STPM_T* stpm;
  int iret = -1;

  RSTP_CRITICAL_PATH_START;
  for (stpm = STP_stpm_get_the_list (); stpm; stpm = stpm->next) {
    if (vlan_id ==  stpm->vlan_id) {
      if (stpm->name)
        (void) strncpy (name, stpm->name, buffsize);
      else
        (void) memset (name, 0, buffsize);
      iret = 0;
      break;
    }
  }
  RSTP_CRITICAL_PATH_END;
  return iret;
}

int /* call it, when link Up/Down */
STP_IN_enable_port (int port_index, Bool enable)
{
  register STPM_T* stpm;

  RSTP_CRITICAL_PATH_START;
  if (! enable) {
#ifdef STP_DBG
    stp_trace("%s (p%02d, all, %s, '%s')",
        "clearFDB", (int) port_index, "this port", "disable port");
#endif
    STP_OUT_flush_lt (port_index, 0, LT_FLASH_ONLY_THE_PORT, "disable port");
  }

  for (stpm = STP_stpm_get_the_list (); stpm; stpm = stpm->next) {
    if (STP_ENABLED != stpm->admin_state) continue;
    
    _stp_in_enable_port_on_stpm (stpm, port_index, enable);
  	/* STP_stpm_update (stpm);*/
  }

  RSTP_CRITICAL_PATH_END;
  return 0;
}

int /* call it, when port speed has been changed, speed in Kb/s  */
STP_IN_changed_port_speed (int port_index, long speed)
{
  register STPM_T* stpm;
  register PORT_T* port;

  RSTP_CRITICAL_PATH_START;
  for (stpm = STP_stpm_get_the_list (); stpm; stpm = stpm->next) {
    if (STP_ENABLED != stpm->admin_state) continue;
    
    port = _stpapi_port_find (stpm, port_index);
    if (! port) continue; 
    port->operSpeed = speed;
#ifdef STP_DBG
    if (port->pcost->debug) {
      stp_trace ("changed operSpeed=%lu", port->operSpeed);
    }
#endif

    port->reselect = True;
    port->selected = False;
  }
  RSTP_CRITICAL_PATH_END;
  return 0;
}

int /* call it, when port duplex mode has been changed  */
STP_IN_changed_port_duplex (int port_index)
{
  register STPM_T* stpm;
  register PORT_T* port;

  RSTP_CRITICAL_PATH_START;
  for (stpm = STP_stpm_get_the_list (); stpm; stpm = stpm->next) {
    if (STP_ENABLED != stpm->admin_state) continue;
    
    port = _stpapi_port_find (stpm, port_index);
    if (! port) continue; 
#ifdef STP_DBG
    if (port->p2p->debug) {
      stp_trace ("STP_IN_changed_port_duplex(%s)", port->port_name);
    }
#endif
    port->p2p_recompute = True;
    port->reselect = True;
    port->selected = False;
  }
  RSTP_CRITICAL_PATH_END;
  return 0;
}

int
STP_IN_check_bpdu_header (BPDU_T* bpdu, size_t len)
{
  unsigned short len8023;

  /* LINTED: alignment */
  len8023 = ntohs (*(unsigned short*) bpdu->eth.len8023);
  if (len8023 > 1500) {/* big len8023 format :( */
    return STP_Big_len8023_Format;
  }

  if (len8023 < MIN_BPDU) { /* small len8023 format :( */
    return STP_Small_len8023_Format;
  }

  if (len8023 + 14 > len) { /* len8023 format gt len :( */
    return STP_len8023_Format_Gt_Len;
  }

  if (bpdu->eth.dsap != BPDU_L_SAP                 ||
      bpdu->eth.ssap != BPDU_L_SAP                 ||
      bpdu->eth.llc != LLC_UI) {
    /* this is not a proper 802.3 pkt! :( */
    return STP_Not_Proper_802_3_Packet;
  }

  if (bpdu->hdr.protocol[0] || bpdu->hdr.protocol[1]) {
    return STP_Invalid_Protocol;
  }

#if 0
  if (bpdu->hdr.version != BPDU_VERSION_ID) {
    return STP_Invalid_Version;  
  }
#endif
  /* see also 9.3.4: think & TBD :( */
  return 0;
}

#ifdef STP_DBG
int dbg_rstp_deny = 0;
#endif


int
STP_IN_rx_bpdu (int vlan_id, int port_index, BPDU_T* bpdu, size_t len)
{
  register PORT_T* port;
  register STPM_T* this;
  int              iret;

#ifdef STP_DBG
  if (1 == dbg_rstp_deny) {
    return 0;
  }
#endif

  RSTP_CRITICAL_PATH_START;
  this = stpapi_stpm_find (vlan_id);
  if (! this) { /*  the stpm had not yet been created :( */
    RSTP_CRITICAL_PATH_END;
    return STP_Vlan_Had_Not_Yet_Been_Created;
  }

  if (STP_DISABLED == this->admin_state) {/* the stpm had not yet been enabled :( */
    RSTP_CRITICAL_PATH_END;
    return STP_Had_Not_Yet_Been_Enabled_On_The_Vlan;
  }

  port = _stpapi_port_find (this, port_index);
  if (! port) {/* port is absent in the stpm :( */
#ifdef STP_DBG
    stp_trace ("RX bpdu vlan_id=%d port=%d port is absent in the stpm :(", (int) vlan_id, (int) port_index);
#endif
    RSTP_CRITICAL_PATH_END;
    return STP_Port_Is_Absent_In_The_Vlan;
  }

#ifdef STP_DBG
  if (port->skip_rx > 0) {
    if (1 == port->skip_rx)
      stp_trace ("port %s stop rx skipping",
                 port->port_name);
    else
      stp_trace ("port %s skip rx %d",
                 port->port_name, port->skip_rx);
    port->skip_rx--;
    RSTP_CRITICAL_PATH_END;
    return STP_Nothing_To_Do;
  }
#endif

  if (port->operEdge && ! port->lnkWhile && port->portEnabled) {
#ifdef STP_DBG
	  if (port->topoch->debug) {
    	stp_trace ("port %s tc=TRUE by operEdge", port->port_name);
	  }
#endif
    port->tc = True; /* IEEE 802.1y, 17.30 */
  }

/* port link change indication will come later :( */
  if (! port->portEnabled &&
      STP_OUT_get_port_link_status (port->port_index)) {
    _stp_in_enable_port_on_stpm (this, port->port_index, True);
  }

#ifdef STP_DBG
  if (port->edge->debug && port->operEdge) {
    stp_trace ("port %s not operEdge !", port->port_name);
  }
#endif

  port->operEdge = False;
  port->wasInitBpdu = True;
  
  iret = STP_port_rx_bpdu (port, bpdu, len);
  (void) STP_stpm_update (this);
  RSTP_CRITICAL_PATH_END;

  return iret;
}

int
STP_IN_one_second (void)
{
  register STPM_T* stpm;
  register int     dbg_cnt = 0;

  RSTP_CRITICAL_PATH_START;
  for (stpm = STP_stpm_get_the_list (); stpm; stpm = stpm->next) {
    if (STP_ENABLED == stpm->admin_state) {
      /* stp_trace ("STP_IN_one_second vlan_id=%d", (int) stpm->vlan_id); */
      STP_stpm_one_second (stpm);
      dbg_cnt++;
    }
  }
  
  RSTP_CRITICAL_PATH_END;

  return dbg_cnt;
}

int
STP_IN_stpm_set_cfg (IN int vlan_id,
                     IN UID_STP_CFG_T* uid_cfg)
{
  int rc = 0, prev_prio, err_code;
  Bool created_here, enabled_here;
  register STPM_T* this;
  UID_STP_CFG_T old;
           
  /* stp_trace ("STP_IN_stpm_set_cfg"); */
  if (0 != STP_IN_stpm_get_cfg (vlan_id, &old)) {
    STP_OUT_get_init_stpm_cfg (vlan_id, &old);
  }
  
  RSTP_CRITICAL_PATH_START;
  if (BR_CFG_PRIO & uid_cfg->field_mask) {
    old.bridge_priority = uid_cfg->bridge_priority;
  }

  if (BR_CFG_AGE & uid_cfg->field_mask) {
    old.max_age = uid_cfg->max_age;
  }

  if (BR_CFG_HELLO & uid_cfg->field_mask) {
    old.hello_time = uid_cfg->hello_time;
  }

  if (BR_CFG_DELAY & uid_cfg->field_mask) {
    old.forward_delay = uid_cfg->forward_delay;
  }

  if (BR_CFG_FORCE_VER & uid_cfg->field_mask) {
    old.force_version = uid_cfg->force_version;
  }

  rc = _check_stpm_config (&old);
  if (0 != rc) {
    stp_trace ("_check_stpm_config failed %d", (int) rc);
    RSTP_CRITICAL_PATH_END;
    return rc;
  }

  if ((BR_CFG_STATE & uid_cfg->field_mask) &&
      (STP_DISABLED == uid_cfg->stp_enabled)) {
    rc = _stp_in_stpm_enable (vlan_id, uid_cfg->vlan_name, STP_DISABLED);
    if (0 != rc) {
      stp_trace ("can't disable rc=%d", (int) rc);
      RSTP_CRITICAL_PATH_END;
      return rc;
    }
    uid_cfg->field_mask &= ~BR_CFG_STATE;
    if (! uid_cfg->field_mask)  {
      RSTP_CRITICAL_PATH_END;
      return 0;
    }
  }

  /* get current state */
  this = stpapi_stpm_find (vlan_id);
  created_here = False;
  enabled_here = False;
  if (! this) { /* it had not yet been created */
    this = stp_in_stpm_create (vlan_id, uid_cfg->vlan_name, &err_code);
    if (! this) {
      RSTP_CRITICAL_PATH_END;
      return err_code;
    }
  }

  prev_prio = this->BrId.prio;
  this->BrId.prio = old.bridge_priority;
  if (STP_ENABLED == this->admin_state) {
    if (0 != STP_stpm_check_bridge_priority (this)) {
      this->BrId.prio = prev_prio;
      stp_trace ("%s", "STP_stpm_check_bridge_priority failed");
      RSTP_CRITICAL_PATH_END;
      return STP_Invalid_Bridge_Priority;
    }
  }

  this->BrTimes.MaxAge = old.max_age;
  this->BrTimes.HelloTime = old.hello_time;
  this->BrTimes.ForwardDelay = old.forward_delay;
  this->ForceVersion = (PROTOCOL_VERSION_T) old.force_version;

  if ((BR_CFG_STATE & uid_cfg->field_mask) &&
      STP_DISABLED != uid_cfg->stp_enabled &&
      STP_DISABLED == this->admin_state) {
    rc = _stp_in_stpm_enable (vlan_id, uid_cfg->vlan_name, uid_cfg->stp_enabled);
    if (0 != rc) {
      stp_trace ("%s", "cannot enable");
      if (created_here) {
        STP_stpm_delete (this);
      }
      RSTP_CRITICAL_PATH_END;
      return rc;
    }
    enabled_here = True;
  }

  if (! enabled_here && STP_DISABLED != this->admin_state) {
    STP_stpm_update_after_bridge_management (this);
  }
  RSTP_CRITICAL_PATH_END;
  return 0;
}

int
STP_IN_port_set_cfg (IN int vlan_id, IN int port_index,
    IN UID_STP_PORT_CFG_T* uid_cfg)
{
  register STPM_T* this;
  register PORT_T* port;

  RSTP_CRITICAL_PATH_START;
  this = stpapi_stpm_find (vlan_id);
  if (! this) { /* it had not yet been created :( */
    RSTP_CRITICAL_PATH_END;
    stp_trace ("RSTP instance with tag %d hasn't been created\n", vlan_id);
    return STP_Vlan_Had_Not_Yet_Been_Created;
  }

  port = _stpapi_port_find (this, port_index);
  if (! port) {/* port is absent in the stpm :( */
    return STP_Port_Is_Absent_In_The_Vlan;
  }

  if (PT_CFG_MCHECK & uid_cfg->field_mask) {
    if (this->ForceVersion >= NORMAL_RSTP)
      port->mcheck = True;
  }

  if (PT_CFG_COST & uid_cfg->field_mask) {
    port->adminPCost = uid_cfg->admin_port_path_cost;
  }
  
  if (PT_CFG_PRIO & uid_cfg->field_mask) {
    port->port_id = (uid_cfg->port_priority << 8) + port_index;
  }
  
  if (PT_CFG_P2P & uid_cfg->field_mask) {
    port->adminPointToPointMac = uid_cfg->admin_point2point;
    port->p2p_recompute = True;
  }
  
  if (PT_CFG_EDGE & uid_cfg->field_mask) {
    port->adminEdge = uid_cfg->admin_edge;
    port->operEdge = port->adminEdge;
#ifdef STP_DBG
    if (port->edge->debug) {
      stp_trace ("port %s is operEdge=%c in STP_IN_port_set_cfg",
          port->port_name,
          port->operEdge ? 'Y' : 'n');
    }
#endif
  }

  if (PT_CFG_NON_STP & uid_cfg->field_mask) {
#ifdef STP_DBG
    if (port->roletrns->debug && port->admin_non_stp != uid_cfg->admin_non_stp) {
      stp_trace ("port %s is adminNonStp=%c in STP_IN_port_set_cfg",
          port->port_name,
          uid_cfg->admin_non_stp ? 'Y' : 'n');
    }
#endif
    port->admin_non_stp = uid_cfg->admin_non_stp;
  }

#ifdef STP_DBG
  if (PT_CFG_DBG_SKIP_RX & uid_cfg->field_mask) {
    port->skip_rx = uid_cfg->skip_rx;
  }

  if (PT_CFG_DBG_SKIP_TX & uid_cfg->field_mask) {
    port->skip_tx = uid_cfg->skip_tx;
  }

#endif

  port->reselect = True;
  port->selected = False;
  
  (void) STP_stpm_update (this);
  
  RSTP_CRITICAL_PATH_END;

  return 0;
}

#ifdef STP_DBG
int

STP_IN_dbg_set_port_trace (char* mach_name, int enadis,
    int vlan_id, int port_no)
{
  register STPM_T* this;
  register PORT_T* port;
  int rc;

  RSTP_CRITICAL_PATH_START;
  this = stpapi_stpm_find (vlan_id);
  if (! this) { /* it had not yet been created :( */
    RSTP_CRITICAL_PATH_END;
    stp_trace ("RSTP instance with tag %d hasn't been created\n", vlan_id);
    return STP_Vlan_Had_Not_Yet_Been_Created;
  }

  port = _stpapi_port_find (this, port_no);
  if (! port) {/* port is absent in the stpm :( */
    return STP_Port_Is_Absent_In_The_Vlan;
  }
  rc = STP_port_trace_state_machine (port, mach_name, enadis);
  
  RSTP_CRITICAL_PATH_END;

  return rc;
}

#endif

const char*
STP_IN_get_error_explanation (int rstp_err_no)
{
#define CHOOSE(a) #a
static char* rstp_error_names[] = RSTP_ERRORS;
#undef CHOOSE
  if (rstp_err_no < STP_OK) {
    return "Too small error code :(";
  }
  if (rstp_err_no >= STP_LAST_DUMMY) {
    return "Too big error code :(";
  }
  
  return rstp_error_names[rstp_err_no];
}

int
STP_IN_port_add(int vlan_id, int port_index)
{
  STPM_T *this;
  PORT_T *port;
  int rc = STP_OK;

  RSTP_CRITICAL_PATH_START;
  this = stpapi_stpm_find (vlan_id);
    
  if (!this) { /* it had not yet been created :( */
    RSTP_CRITICAL_PATH_END;
    return STP_Vlan_Had_Not_Yet_Been_Created;
  }

  port = this->ports;

  if (! STP_port_create (this, port_index)) {
    /* can't add port :( */
    stp_trace ("can't create port %d", port_index);
    RSTP_CRITICAL_PATH_END;
    return STP_Cannot_Create_Instance_For_Port;
  }

  if (!port)
    rc = STP_stpm_start (this);

  RSTP_CRITICAL_PATH_END;

  return rc;
}

int
STP_IN_port_remove(int vlan_id, int port_index)
{
  STPM_T *this;
  PORT_T *port;

  RSTP_CRITICAL_PATH_START;
  this = stpapi_stpm_find (vlan_id);
    
  if (!this) { /* it had not yet been created :( */
    RSTP_CRITICAL_PATH_END;
    return STP_Vlan_Had_Not_Yet_Been_Created;
  }

  port = _stpapi_port_find (this, port_index);
  if (! port) {/* port is absent in the stpm :( */
    RSTP_CRITICAL_PATH_END;
    return STP_Port_Is_Absent_In_The_Vlan;
  }

  STP_port_delete (port);

  if (!this->ports)
    STP_stpm_stop (this);
  RSTP_CRITICAL_PATH_END;

  return STP_OK;
}

void
STP_IN_get_bridge_id(int vlan_id, unsigned short *priority, unsigned char *mac)
{
  STPM_T *this;

  RSTP_CRITICAL_PATH_START;
  this = stpapi_stpm_find (vlan_id);
  *priority = this->BrId.prio;
  (void) memcpy(mac, this->BrId.addr, 6);
  RSTP_CRITICAL_PATH_END;
}

const char *
STP_IN_state2str(RSTP_PORT_STATE state)
{
	switch (state) {
	case UID_PORT_DISABLED:
		return ("disabled");
	case UID_PORT_DISCARDING:
		return ("discarding");
	case UID_PORT_LEARNING:
		return ("learning");
	case UID_PORT_FORWARDING:
		return ("forwarding");
	case UID_PORT_NON_STP:
		return ("non-stp");
	case UID_PORT_BADSDU:		/* synthetic state used by daemon */
		return ("bad-mtu");
	}
	return ("unknown");
}