| 1 |
|
| # SNMP::Info::Bridge |
| 2 |
|
| # |
| 3 |
|
| # Changes since Version 0.7 Copyright (c) 2004 Max Baker |
| 4 |
|
| # All rights reserved. |
| 5 |
|
| # |
| 6 |
|
| # Copyright (c) 2002,2003 Regents of the University of California |
| 7 |
|
| # All rights reserved. |
| 8 |
|
| # |
| 9 |
|
| # Redistribution and use in source and binary forms, with or without |
| 10 |
|
| # modification, are permitted provided that the following conditions are met: |
| 11 |
|
| # |
| 12 |
|
| # * Redistributions of source code must retain the above copyright notice, |
| 13 |
|
| # this list of conditions and the following disclaimer. |
| 14 |
|
| # * Redistributions in binary form must reproduce the above copyright |
| 15 |
|
| # notice, this list of conditions and the following disclaimer in the |
| 16 |
|
| # documentation and/or other materials provided with the distribution. |
| 17 |
|
| # * Neither the name of the University of California, Santa Cruz nor the |
| 18 |
|
| # names of its contributors may be used to endorse or promote products |
| 19 |
|
| # derived from this software without specific prior written permission. |
| 20 |
|
| # |
| 21 |
|
| # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 |
|
| # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 |
|
| # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 |
|
| # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 25 |
|
| # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 |
|
| # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 |
|
| # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 |
|
| # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 29 |
|
| # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 30 |
|
| # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 |
|
| # POSSIBILITY OF SUCH DAMAGE. |
| 32 |
|
| |
| 33 |
|
| package SNMP::Info::Bridge; |
| 34 |
|
| |
| 35 |
|
| use strict; |
| 36 |
|
| use warnings; |
| 37 |
|
| use Exporter; |
| 38 |
|
| use SNMP::Info; |
| 39 |
|
| |
| 40 |
|
| @SNMP::Info::Bridge::ISA = qw/SNMP::Info Exporter/; |
| 41 |
|
| @SNMP::Info::Bridge::EXPORT_OK = qw//; |
| 42 |
|
| |
| 43 |
|
| our ($VERSION, $DEBUG, %MIBS, %FUNCS, %GLOBALS, %MUNGE, $INIT); |
| 44 |
|
| |
| 45 |
|
| $VERSION = '3.985003'; |
| 46 |
|
| |
| 47 |
|
| %MIBS = ( |
| 48 |
|
| 'BRIDGE-MIB' => 'dot1dBaseBridgeAddress', |
| 49 |
|
| 'Q-BRIDGE-MIB' => 'dot1qPvid', |
| 50 |
|
| 'RSTP-MIB' => 'dot1dStpPortOperEdgePort', |
| 51 |
|
| ); |
| 52 |
|
| |
| 53 |
|
| %GLOBALS = ( |
| 54 |
|
| 'b_mac' => 'dot1dBaseBridgeAddress', |
| 55 |
|
| 'b_ports' => 'dot1dBaseNumPorts', |
| 56 |
|
| 'b_type' => 'dot1dBaseType', |
| 57 |
|
| |
| 58 |
|
| # Spanning Tree Protocol |
| 59 |
|
| 'stp_ver' => 'dot1dStpProtocolSpecification', |
| 60 |
|
| 'stp_time' => 'dot1dStpTimeSinceTopologyChange', |
| 61 |
|
| 'stp_root' => 'dot1dStpDesignatedRoot', |
| 62 |
|
| 'stp_root_port' => 'dot1dStpRootPort', |
| 63 |
|
| 'stp_priority' => 'dot1dStpPriority', |
| 64 |
|
| |
| 65 |
|
| # Q-BRIDGE-MIB |
| 66 |
|
| 'qb_vlans_max' => 'dot1qMaxSupportedVlans', |
| 67 |
|
| 'qb_vlans' => 'dot1qNumVlans', |
| 68 |
|
| 'qb_next_vlan_index' => 'dot1qNextFreeLocalVlanIndex', |
| 69 |
|
| ); |
| 70 |
|
| |
| 71 |
|
| %FUNCS = ( |
| 72 |
|
| |
| 73 |
|
| # Forwarding Table: Dot1dTpFdbEntry |
| 74 |
|
| 'fw_mac' => 'dot1dTpFdbAddress', |
| 75 |
|
| 'fw_port' => 'dot1dTpFdbPort', |
| 76 |
|
| 'fw_status' => 'dot1dTpFdbStatus', |
| 77 |
|
| |
| 78 |
|
| # Bridge Port Table: Dot1dBasePortEntry |
| 79 |
|
| 'bp_index' => 'dot1dBasePortIfIndex', |
| 80 |
|
| 'bp_port' => 'dot1dBasePortCircuit', |
| 81 |
|
| |
| 82 |
|
| # Bridge Static (Destination-Address Filtering) Database |
| 83 |
|
| 'bs_mac' => 'dot1dStaticAddress', |
| 84 |
|
| 'bs_port' => 'dot1dStaticReceivePort', |
| 85 |
|
| 'bs_to' => 'dot1dStaticAllowedToGoTo', |
| 86 |
|
| 'bs_status' => 'dot1dStaticStatus', |
| 87 |
|
| |
| 88 |
|
| # These leafs are not part of a table, but placed here |
| 89 |
|
| # to return a hash reference to ease API compatibility with |
| 90 |
|
| # MST and PVST implementations indexed by a spanning tree |
| 91 |
|
| # instance id |
| 92 |
|
| 'stp_i_mac' => 'dot1dBaseBridgeAddress', |
| 93 |
|
| 'stp_i_time' => 'dot1dStpTimeSinceTopologyChange', |
| 94 |
|
| 'stp_i_ntop' => 'dot1dStpTopChanges', |
| 95 |
|
| 'stp_i_root' => 'dot1dStpDesignatedRoot', |
| 96 |
|
| 'stp_i_root_port' => 'dot1dStpRootPort', |
| 97 |
|
| 'stp_i_priority' => 'dot1dStpPriority', |
| 98 |
|
| |
| 99 |
|
| # Spanning Tree Protocol Table : dot1dStpPortTable |
| 100 |
|
| 'stp_p_id' => 'dot1dStpPort', |
| 101 |
|
| 'stp_p_priority' => 'dot1dStpPortPriority', |
| 102 |
|
| 'stp_p_state' => 'dot1dStpPortState', |
| 103 |
|
| 'stp_p_enable' => 'dot1dStpPortEnable', |
| 104 |
|
| 'stp_p_cost' => 'dot1dStpPortPathCost', |
| 105 |
|
| 'stp_p_root' => 'dot1dStpPortDesignatedRoot', |
| 106 |
|
| 'stp_p_bridge' => 'dot1dStpPortDesignatedBridge', |
| 107 |
|
| 'stp_p_port' => 'dot1dStpPortDesignatedPort', |
| 108 |
|
| |
| 109 |
|
| # Rapid Spanning Tree Protocol Table : dot1dStpExtPortEntry |
| 110 |
|
| 'is_edgeport_admin' => 'dot1dStpPortAdminEdgePort', |
| 111 |
|
| 'is_edgeport_oper' => 'dot1dStpPortOperEdgePort', |
| 112 |
|
| |
| 113 |
|
| # Q-BRIDGE-MIB : dot1qPortVlanTable |
| 114 |
|
| 'qb_i_vlan' => 'dot1qPvid', |
| 115 |
|
| 'qb_i_vlan_type' => 'dot1qPortAcceptableFrameTypes', |
| 116 |
|
| 'qb_i_vlan_in_flt' => 'dot1qPortIngressFiltering', |
| 117 |
|
| |
| 118 |
|
| # Q-BRIDGE-MIB : dot1qVlanCurrentTable |
| 119 |
|
| 'qb_cv_egress' => 'dot1qVlanCurrentEgressPorts', |
| 120 |
|
| 'qb_cv_untagged' => 'dot1qVlanCurrentUntaggedPorts', |
| 121 |
|
| 'qb_cv_stat' => 'dot1qVlanStatus', |
| 122 |
|
| |
| 123 |
|
| # Q-BRIDGE-MIB : dot1qVlanStaticTable |
| 124 |
|
| 'v_name' => 'dot1qVlanStaticName', |
| 125 |
|
| 'qb_v_name' => 'dot1qVlanStaticName', |
| 126 |
|
| 'qb_v_egress' => 'dot1qVlanStaticEgressPorts', |
| 127 |
|
| 'qb_v_fbdn_egress' => 'dot1qVlanForbiddenEgressPorts', |
| 128 |
|
| 'qb_v_untagged' => 'dot1qVlanStaticUntaggedPorts', |
| 129 |
|
| 'qb_v_stat' => 'dot1qVlanStaticRowStatus', |
| 130 |
|
| |
| 131 |
|
| # VLAN Forwarding Table: Dot1qTpFdbEntry |
| 132 |
|
| 'qb_fw_port' => 'dot1qTpFdbPort', |
| 133 |
|
| 'qb_fw_status' => 'dot1qTpFdbStatus', |
| 134 |
|
| ); |
| 135 |
|
| |
| 136 |
|
| %MUNGE = ( |
| 137 |
|
| |
| 138 |
|
| # Inherit all the built in munging |
| 139 |
|
| %SNMP::Info::MUNGE, |
| 140 |
|
| |
| 141 |
|
| # Add ones for our class |
| 142 |
|
| 'b_mac' => \&SNMP::Info::munge_mac, |
| 143 |
|
| 'fw_mac' => \&SNMP::Info::munge_mac, |
| 144 |
|
| 'bs_mac' => \&SNMP::Info::munge_mac, |
| 145 |
|
| 'stp_root' => \&SNMP::Info::munge_prio_mac, |
| 146 |
|
| 'stp_i_mac' => \&SNMP::Info::munge_mac, |
| 147 |
|
| 'stp_i_root' => \&SNMP::Info::munge_prio_mac, |
| 148 |
|
| 'stp_p_root' => \&SNMP::Info::munge_prio_mac, |
| 149 |
|
| 'stp_p_bridge' => \&SNMP::Info::munge_prio_mac, |
| 150 |
|
| 'stp_p_port' => \&SNMP::Info::munge_prio_port, |
| 151 |
|
| 'qb_cv_egress' => \&SNMP::Info::munge_port_list, |
| 152 |
|
| 'qb_cv_untagged' => \&SNMP::Info::munge_port_list, |
| 153 |
|
| 'qb_v_egress' => \&SNMP::Info::munge_port_list, |
| 154 |
|
| 'qb_v_fbdn_egress' => \&SNMP::Info::munge_port_list, |
| 155 |
|
| 'qb_v_untagged' => \&SNMP::Info::munge_port_list, |
| 156 |
|
| 'qb_cv_untagged' => \&SNMP::Info::munge_port_list, |
| 157 |
|
| |
| 158 |
|
| ); |
| 159 |
|
| |
| 160 |
|
| # break up the Dot1qTpFdbEntry INDEX into FDB ID and MAC Address. |
| 161 |
|
| sub _qb_fdbtable_index { |
| 162 |
|
| my $idx = shift; |
| 163 |
|
| my @values = split( /\./, $idx ); |
| 164 |
|
| my $fdb_id = shift(@values); |
| 165 |
|
| return ( $fdb_id, join( ':', map { sprintf "%02x", $_ } @values ) ); |
| 166 |
|
| } |
| 167 |
|
| |
| 168 |
|
| sub qb_fw_mac { |
| 169 |
|
| my $bridge = shift; |
| 170 |
|
| my $partial = shift; |
| 171 |
|
| |
| 172 |
|
| my $qb_fw_port = $bridge->qb_fw_port($partial); |
| 173 |
|
| my $qb_fw_mac = {}; |
| 174 |
|
| foreach my $idx ( keys %$qb_fw_port ) { |
| 175 |
|
| my ( $fdb_id, $mac ) = _qb_fdbtable_index($idx); |
| 176 |
|
| $qb_fw_mac->{$idx} = $mac; |
| 177 |
|
| } |
| 178 |
|
| return $qb_fw_mac; |
| 179 |
|
| } |
| 180 |
|
| |
| 181 |
|
| sub qb_fw_vlan { |
| 182 |
|
| my $bridge = shift; |
| 183 |
|
| my $partial = shift; |
| 184 |
|
| |
| 185 |
|
| my $qb_fw_port = $bridge->qb_fw_port($partial); |
| 186 |
|
| my $qb_fdb_ids = $bridge->qb_fdb_index() || {}; |
| 187 |
|
| |
| 188 |
|
| |
| 189 |
|
| my $qb_fw_vlan = {}; |
| 190 |
|
| foreach my $idx ( keys %$qb_fw_port ) { |
| 191 |
|
| my ( $fdb_id, $mac ) = _qb_fdbtable_index($idx); |
| 192 |
|
| # Many devices do not populate the dot1qVlanCurrentTable, so default |
| 193 |
|
| # to FDB ID = VID, but if we have a mapping use it. |
| 194 |
|
| my $vlan = $fdb_id; |
| 195 |
|
| # exists as test since some devices have a vlan 0, and a shared |
| 196 |
|
| # FDB ID maps to undef because its entries name no single VLAN |
| 197 |
|
| if (exists $qb_fdb_ids->{$fdb_id}) { |
| 198 |
|
| $vlan = $qb_fdb_ids->{$fdb_id}; |
| 199 |
|
| } |
| 200 |
|
| $qb_fw_vlan->{$idx} = $vlan; |
| 201 |
|
| } |
| 202 |
|
| return $qb_fw_vlan; |
| 203 |
|
| } |
| 204 |
|
| |
| 205 |
|
| sub qb_i_vlan_t { |
| 206 |
|
| my $bridge = shift; |
| 207 |
|
| my $partial = shift; |
| 208 |
|
| |
| 209 |
|
| my $qb_i_vlan = $bridge->qb_i_vlan($partial); |
| 210 |
|
| my $qb_i_vlan_type = $bridge->qb_i_vlan_type($partial); |
| 211 |
|
| |
| 212 |
|
| my $i_vlan = {}; |
| 213 |
|
| |
| 214 |
|
| foreach my $if ( keys %$qb_i_vlan ) { |
| 215 |
|
| my $vlan = $qb_i_vlan->{$if}; |
| 216 |
|
| my $tagged = $qb_i_vlan_type->{$if} || ''; |
| 217 |
|
| next unless defined $vlan; |
| 218 |
|
| $i_vlan->{$if} = $tagged eq 'admitOnlyVlanTagged' ? 'trunk' : $vlan; |
| 219 |
|
| } |
| 220 |
|
| return $i_vlan; |
| 221 |
|
| } |
| 222 |
|
| |
| 223 |
|
| sub qb_fdb_index { |
| 224 |
|
| my $bridge = shift; |
| 225 |
|
| my $partial = shift; |
| 226 |
|
| |
| 227 |
|
| # Some devices may not implement TimeFilter in a standard manner |
| 228 |
|
| # appearing to loop on this request. Override in the device class, |
| 229 |
|
| # see Enterasys for example. |
| 230 |
|
| my $qb_fdb_ids = $bridge->dot1qVlanFdbId() || {}; |
| 231 |
|
| |
| 232 |
|
| return $bridge->_qb_fdb_vlan_map($qb_fdb_ids); |
| 233 |
|
| } |
| 234 |
|
| |
| 235 |
|
| # Strip the TimeFilter from dot1qVlanFdbId and invert it. With shared VLAN |
| 236 |
|
| # learning several VLANs report one FDB ID; it maps to undef rather than to |
| 237 |
|
| # whichever of those VLANs hash order happened to visit last. |
| 238 |
|
| sub _qb_fdb_vlan_map { |
| 239 |
|
| my ( $bridge, $qb_fdb_ids ) = @_; |
| 240 |
|
| |
| 241 |
|
| my %vlans_of; |
| 242 |
|
| foreach my $fdb_entry ( keys %$qb_fdb_ids ) { |
| 243 |
|
| ( my $vlan = $fdb_entry ) =~ s/^\d+\.//; |
| 244 |
|
| $vlans_of{ $qb_fdb_ids->{$fdb_entry} }{$vlan} = 1; |
| 245 |
|
| } |
| 246 |
|
| |
| 247 |
|
| my $vl_fdb_index = {}; |
| 248 |
|
| foreach my $fdb_id ( keys %vlans_of ) { |
| 249 |
|
| my @vlans = keys %{ $vlans_of{$fdb_id} }; |
| 250 |
|
| $vl_fdb_index->{$fdb_id} = scalar @vlans == 1 ? $vlans[0] : undef; |
| 251 |
|
| } |
| 252 |
|
| return $vl_fdb_index; |
| 253 |
|
| } |
| 254 |
|
| |
| 255 |
|
| # Most devices now support Q-BRIDGE-MIB, fall back to |
| 256 |
|
| # BRIDGE-MIB for those that don't. |
| 257 |
|
| sub fw_mac { |
| 258 |
|
| my $bridge = shift; |
| 259 |
|
| |
| 260 |
|
| unless ($bridge->can('cisco_comm_indexing') && $bridge->cisco_comm_indexing()){ |
| 261 |
|
| my $qb = $bridge->qb_fw_mac(); |
| 262 |
|
| return $qb if (ref {} eq ref $qb and scalar keys %$qb); |
| 263 |
|
| } |
| 264 |
|
| |
| 265 |
|
| return $bridge->SUPER::fw_mac(); |
| 266 |
|
| } |
| 267 |
|
| |
| 268 |
|
| sub fw_port { |
| 269 |
|
| my $bridge = shift; |
| 270 |
|
| |
| 271 |
|
| unless ($bridge->can('cisco_comm_indexing') && $bridge->cisco_comm_indexing()){ |
| 272 |
|
| my $qb = $bridge->qb_fw_port(); |
| 273 |
|
| return $qb if (ref {} eq ref $qb and scalar keys %$qb); |
| 274 |
|
| } |
| 275 |
|
| |
| 276 |
|
| return $bridge->SUPER::fw_port(); |
| 277 |
|
| } |
| 278 |
|
| |
| 279 |
|
| sub fw_status { |
| 280 |
|
| my $bridge = shift; |
| 281 |
|
| |
| 282 |
|
| unless ($bridge->can('cisco_comm_indexing') && $bridge->cisco_comm_indexing()){ |
| 283 |
|
| my $qb = $bridge->qb_fw_status(); |
| 284 |
|
| return $qb if (ref {} eq ref $qb and scalar keys %$qb); |
| 285 |
|
| } |
| 286 |
|
| |
| 287 |
|
| return $bridge->SUPER::fw_status(); |
| 288 |
|
| } |
| 289 |
|
| |
| 290 |
|
| sub i_stp_state { |
| 291 |
|
| my $bridge = shift; |
| 292 |
|
| my $partial = shift; |
| 293 |
|
| |
| 294 |
|
| my $bp_index = $bridge->bp_index($partial); |
| 295 |
|
| my $stp_p_state = $bridge->stp_p_state($partial); |
| 296 |
|
| |
| 297 |
|
| my %i_stp_state; |
| 298 |
|
| |
| 299 |
|
| foreach my $index ( keys %$stp_p_state ) { |
| 300 |
|
| my $state = $stp_p_state->{$index}; |
| 301 |
|
| my $iid = $bp_index->{$index}; |
| 302 |
|
| next unless defined $iid; |
| 303 |
|
| next unless defined $state; |
| 304 |
|
| $i_stp_state{$iid} = $state; |
| 305 |
|
| } |
| 306 |
|
| |
| 307 |
|
| return \%i_stp_state; |
| 308 |
|
| } |
| 309 |
|
| |
| 310 |
|
| sub i_edgeport_admin { |
| 311 |
|
| my $bridge = shift; |
| 312 |
|
| my $partial = shift; |
| 313 |
|
| |
| 314 |
|
| my $edge_admin = $bridge->is_edgeport_admin(); |
| 315 |
|
| return _stp_edge_by_ifindex( $bridge, $edge_admin, $partial ); |
| 316 |
|
| } |
| 317 |
|
| |
| 318 |
|
| sub i_edgeport_oper { |
| 319 |
|
| my $bridge = shift; |
| 320 |
|
| my $partial = shift; |
| 321 |
|
| |
| 322 |
|
| my $edge_oper = $bridge->is_edgeport_oper(); |
| 323 |
|
| return _stp_edge_by_ifindex( $bridge, $edge_oper, $partial ); |
| 324 |
|
| } |
| 325 |
|
| |
| 326 |
|
| # Edge values keyed by bridge port, mapped to ifIndex through bp_index |
| 327 |
|
| # (or $port_index, when the caller knows the keys are not bridge ports) and |
| 328 |
|
| # read as 1 or 0 by $decode (default _stp_enabled); a port missing from the |
| 329 |
|
| # map, or a value $decode does not know, is absent. Called as a function by |
| 330 |
|
| # classes outside Bridge's inheritance (RapidCity). |
| 331 |
|
| sub _stp_edge_by_ifindex { |
| 332 |
|
| my ( $bridge, $edge, $partial, $decode, $port_index ) = @_; |
| 333 |
|
| $decode ||= \&_stp_enabled; |
| 334 |
|
| return {} unless keys %{ $edge || {} }; |
| 335 |
|
| |
| 336 |
|
| my $bp_index = $port_index || $bridge->bp_index($partial) || {}; |
| 337 |
|
| |
| 338 |
|
| my %i_edgeport; |
| 339 |
|
| foreach my $port ( keys %{ $edge || {} } ) { |
| 340 |
|
| my $iid = $bp_index->{$port}; |
| 341 |
|
| my $enabled = $decode->( $edge->{$port} ); |
| 342 |
|
| next unless defined $iid; |
| 343 |
|
| next unless defined $enabled; |
| 344 |
|
| $i_edgeport{$iid} = $enabled; |
| 345 |
|
| } |
| 346 |
|
| return \%i_edgeport; |
| 347 |
|
| } |
| 348 |
|
| |
| 349 |
|
| sub i_stp_port { |
| 350 |
|
| my $bridge = shift; |
| 351 |
|
| my $partial = shift; |
| 352 |
|
| |
| 353 |
|
| my $bp_index = $bridge->bp_index($partial); |
| 354 |
|
| my $stp_p_port = $bridge->stp_p_port($partial); |
| 355 |
|
| |
| 356 |
|
| my %i_stp_port; |
| 357 |
|
| |
| 358 |
|
| foreach my $index ( keys %$stp_p_port ) { |
| 359 |
|
| my $bport = $stp_p_port->{$index}; |
| 360 |
|
| my $iid = $bp_index->{$index}; |
| 361 |
|
| next unless defined $iid; |
| 362 |
|
| next unless defined $bport; |
| 363 |
|
| $i_stp_port{$iid} = $bport; |
| 364 |
|
| } |
| 365 |
|
| return \%i_stp_port; |
| 366 |
|
| } |
| 367 |
|
| |
| 368 |
|
| sub i_stp_id { |
| 369 |
|
| my $bridge = shift; |
| 370 |
|
| my $partial = shift; |
| 371 |
|
| |
| 372 |
|
| my $bp_index = $bridge->bp_index($partial); |
| 373 |
|
| my $stp_p_id = $bridge->stp_p_id($partial); |
| 374 |
|
| |
| 375 |
|
| my %i_stp_id; |
| 376 |
|
| |
| 377 |
|
| foreach my $index ( keys %$stp_p_id ) { |
| 378 |
|
| my $bport = $stp_p_id->{$index}; |
| 379 |
|
| my $iid = $bp_index->{$index}; |
| 380 |
|
| next unless defined $iid; |
| 381 |
|
| next unless defined $bport; |
| 382 |
|
| $i_stp_id{$iid} = $bport; |
| 383 |
|
| } |
| 384 |
|
| return \%i_stp_id; |
| 385 |
|
| } |
| 386 |
|
| |
| 387 |
|
| sub i_stp_bridge { |
| 388 |
|
| my $bridge = shift; |
| 389 |
|
| my $partial = shift; |
| 390 |
|
| |
| 391 |
|
| my $bp_index = $bridge->bp_index($partial); |
| 392 |
|
| my $stp_p_bridge = $bridge->stp_p_bridge($partial); |
| 393 |
|
| |
| 394 |
|
| my %i_stp_bridge; |
| 395 |
|
| |
| 396 |
|
| foreach my $index ( keys %$stp_p_bridge ) { |
| 397 |
|
| my $bport = $stp_p_bridge->{$index}; |
| 398 |
|
| my $iid = $bp_index->{$index}; |
| 399 |
|
| next unless defined $iid; |
| 400 |
|
| next unless defined $bport; |
| 401 |
|
| $i_stp_bridge{$iid} = $bport; |
| 402 |
|
| } |
| 403 |
|
| return \%i_stp_bridge; |
| 404 |
|
| } |
| 405 |
|
| |
| 406 |
|
| sub stp_protocols { |
| 407 |
|
| my $bridge = shift; |
| 408 |
|
| |
| 409 |
|
| my $trees = $bridge->stp_trees() || {}; |
| 410 |
|
| return unless keys %$trees; |
| 411 |
|
| return ['dot1d']; |
| 412 |
|
| } |
| 413 |
|
| |
| 414 |
|
| sub stp_trees { |
| 415 |
|
| my $bridge = shift; |
| 416 |
|
| |
| 417 |
|
| return unless $bridge->_stp_default_context_ok(); |
| 418 |
|
| return $bridge->{_stp_trees} if $bridge->{_stp_trees}; |
| 419 |
|
| return {} unless $bridge->_stp_default_is_common(); |
| 420 |
|
| |
| 421 |
|
| my $record = $bridge->_stp_dot1d_tree() or return {}; |
| 422 |
|
| $bridge->{_stp_trees} = { 'cist.0' => $record }; |
| 423 |
|
| return $bridge->{_stp_trees}; |
| 424 |
|
| } |
| 425 |
|
| |
| 426 |
|
| # The BRIDGE-MIB common tree whatever _stp_default_is_common() says, so a |
| 427 |
|
| # class that keeps the default off can still build it. Callers set |
| 428 |
|
| # protocol and measured on a copy. |
| 429 |
|
| sub _stp_dot1d_tree { |
| 430 |
|
| my $bridge = shift; |
| 431 |
|
| |
| 432 |
|
| my $ports = $bridge->stp_p_id() || {}; |
| 433 |
|
| my $root = $bridge->stp_root(); |
| 434 |
|
| return unless keys %$ports or defined $root; |
| 435 |
|
| |
| 436 |
|
| my $bp_index = $bridge->bp_index() || {}; |
| 437 |
|
| my $unmapped = grep { !defined $bp_index->{$_} } values %$ports; |
| 438 |
|
| |
| 439 |
|
| return { |
| 440 |
|
| type => 'cist', |
| 441 |
|
| id => 0, |
| 442 |
|
| protocol => undef, |
| 443 |
|
| measured => 0, |
| 444 |
|
| ports_complete => $unmapped ? 0 : 1, |
| 445 |
|
| contexts => [ { kind => 'none' } ], |
| 446 |
|
| bridge => scalar $bridge->_stp_own_bridge_id(), |
| 447 |
|
| root => scalar _stp_decode_bridge_id($root), |
| 448 |
|
| }; |
| 449 |
|
| } |
| 450 |
|
| |
| 451 |
|
| sub stp_vlan_map { return; } |
| 452 |
|
| |
| 453 |
|
| # Only the default context serves the whole tree list |
| 454 |
|
| sub _stp_default_context_ok { |
| 455 |
|
| my $bridge = shift; |
| 456 |
|
| |
| 457 |
|
| my $context = $bridge->_walk_context(); |
| 458 |
|
| return 1 unless length $context; |
| 459 |
|
| |
| 460 |
|
| my $method = ( caller(1) )[3]; |
| 461 |
|
| $bridge->error_throw( |
| 462 |
|
| "$method() reads the tree list, which only the default context" |
| 463 |
|
| . " serves whole. This session is in context '$context'. Call" |
| 464 |
|
| . " update() back to the default context, read the list, and" |
| 465 |
|
| . " keep it while you switch contexts.\n" ); |
| 466 |
|
| return 0; |
| 467 |
|
| } |
| 468 |
|
| |
| 469 |
|
| sub stp_tree_ports { |
| 470 |
|
| my $bridge = shift; |
| 471 |
|
| my $tree_key = shift; |
| 472 |
|
| |
| 473 |
|
| my $tree = $bridge->_stp_tree_for_key($tree_key) or return; |
| 474 |
|
| return $bridge->_stp_dot1d_port_rows($tree); |
| 475 |
|
| } |
| 476 |
|
| |
| 477 |
|
| # Error names the method that called stp_tree_ports, not this helper |
| 478 |
|
| sub _stp_tree_for_key { |
| 479 |
|
| my $bridge = shift; |
| 480 |
|
| my $tree_key = shift; |
| 481 |
|
| |
| 482 |
|
| my $trees = $bridge->stp_trees() || {}; |
| 483 |
|
| my $tree = defined $tree_key ? $trees->{$tree_key} : undef; |
| 484 |
|
| return $tree if $tree; |
| 485 |
|
| |
| 486 |
|
| my $key = defined $tree_key ? $tree_key : 'undef'; |
| 487 |
|
| my $method = ( caller(1) )[3] // 'stp_tree_ports'; |
| 488 |
|
| $bridge->error_throw( "$method() has no tree '$key'. Pass a key from" |
| 489 |
|
| . " stp_trees(), read in the default context.\n" ); |
| 490 |
|
| return; |
| 491 |
|
| } |
| 492 |
|
| |
| 493 |
|
| # BRIDGE-MIB port rows in the session's current context, labeled with $tree |
| 494 |
|
| sub _stp_dot1d_port_rows { |
| 495 |
|
| my $bridge = shift; |
| 496 |
|
| my $tree = shift; |
| 497 |
|
| |
| 498 |
|
| my $ports = $bridge->stp_p_id() || {}; |
| 499 |
|
| my $states = $bridge->stp_p_state() || {}; |
| 500 |
|
| my $enable = $bridge->stp_p_enable() || {}; |
| 501 |
|
| my $d_bridge = $bridge->stp_p_bridge() || {}; |
| 502 |
|
| my $d_port = $bridge->stp_p_port() || {}; |
| 503 |
|
| my $bp_index = $bridge->bp_index() || {}; |
| 504 |
|
| |
| 505 |
|
| my %tree_ports; |
| 506 |
|
| foreach my $index ( keys %$ports ) { |
| 507 |
|
| my $iid = $bp_index->{ $ports->{$index} }; |
| 508 |
|
| next unless defined $iid; |
| 509 |
|
| $tree_ports{$iid} = { |
| 510 |
|
| tree_type => $tree->{type}, |
| 511 |
|
| tree_id => $tree->{id}, |
| 512 |
|
| state => $states->{$index}, |
| 513 |
|
| role => undef, |
| 514 |
|
| role_raw => undef, |
| 515 |
|
| enabled => scalar _stp_enabled( $enable->{$index} ), |
| 516 |
|
| designated_bridge => |
| 517 |
|
| scalar _stp_decode_bridge_id( $d_bridge->{$index} ), |
| 518 |
|
| designated_port => |
| 519 |
|
| scalar _stp_decode_port_id( $d_port->{$index} ), |
| 520 |
|
| }; |
| 521 |
|
| } |
| 522 |
|
| return \%tree_ports; |
| 523 |
|
| } |
| 524 |
|
| |
| 525 |
|
| sub stp_trees_complete { |
| 526 |
|
| my $bridge = shift; |
| 527 |
|
| |
| 528 |
|
| # A class that keeps the default off has trees this class does not read |
| 529 |
|
| return 0 unless $bridge->_stp_default_is_common(); |
| 530 |
|
| |
| 531 |
|
| my $trees = $bridge->stp_trees() || {}; |
| 532 |
|
| return 0 if length $bridge->_walk_context(); |
| 533 |
|
| return unless keys %$trees; |
| 534 |
|
| return 0; |
| 535 |
|
| } |
| 536 |
|
| |
| 537 |
|
| sub _stp_default_is_common { return 1; } |
| 538 |
|
| |
| 539 |
|
| # walk_status is set by the walk, so call this after reading the columns |
| 540 |
|
| sub _stp_walk_incomplete { |
| 541 |
|
| my ( $bridge, @methods ) = @_; |
| 542 |
|
| |
| 543 |
|
| foreach my $method (@methods) { |
| 544 |
|
| my $status = $bridge->walk_status($method); |
| 545 |
|
| return 1 if $status and !$status->{complete}; |
| 546 |
|
| } |
| 547 |
|
| return 0; |
| 548 |
|
| } |
| 549 |
|
| |
| 550 |
|
| sub _stp_own_bridge_id { |
| 551 |
|
| my $bridge = shift; |
| 552 |
|
| |
| 553 |
|
| return _stp_bridge_id_from( $bridge->stp_priority(), $bridge->b_mac() ); |
| 554 |
|
| } |
| 555 |
|
| |
| 556 |
|
| # EnabledStatus and TruthValue both use 1 and 2 |
| 557 |
|
| sub _stp_enabled { |
| 558 |
|
| my $value = shift; |
| 559 |
|
| return unless defined $value; |
| 560 |
|
| |
| 561 |
|
| return 1 if $value =~ /^(?:enabled|true|1)$/; |
| 562 |
|
| return 0 if $value =~ /^(?:disabled|false|2)$/; |
| 563 |
|
| return; |
| 564 |
|
| } |
| 565 |
|
| |
| 566 |
|
| sub _stp_bridge_id_from { |
| 567 |
|
| my ( $priority, $mac ) = @_; |
| 568 |
|
| return unless defined $priority and defined $mac; |
| 569 |
|
| |
| 570 |
|
| my $prio_hex = sprintf( '%04x', $priority ); |
| 571 |
|
| my $prio_octets = join( ':', unpack( '(A2)2', $prio_hex ) ); |
| 572 |
|
| return _stp_decode_bridge_id( join( ':', $prio_octets, $mac ) ); |
| 573 |
|
| } |
| 574 |
|
| |
| 575 |
|
| # 802.1t fixed format: top 4 bits priority, low 12 bits system ID extension |
| 576 |
|
| sub _stp_decode_bridge_id { |
| 577 |
|
| my $id = shift; |
| 578 |
|
| return unless defined $id; |
| 579 |
|
| |
| 580 |
|
| my @octets = split /:/, $id; |
| 581 |
|
| return unless @octets == 8; |
| 582 |
|
| |
| 583 |
|
| my $prio = hex( $octets[0] . $octets[1] ); |
| 584 |
|
| return { |
| 585 |
|
| priority => ( $prio >> 12 ) * 4096, |
| 586 |
|
| sys_id_ext => $prio & 0x0fff, |
| 587 |
|
| mac => join( ':', @octets[ 2 .. 7 ] ), |
| 588 |
|
| raw => $id, |
| 589 |
|
| }; |
| 590 |
|
| } |
| 591 |
|
| |
| 592 |
|
| # 802.1t fixed format: top 4 bits priority, low 12 bits port number |
| 593 |
|
| sub _stp_decode_port_id { |
| 594 |
|
| my $id = shift; |
| 595 |
|
| return unless defined $id; |
| 596 |
|
| |
| 597 |
|
| my @octets = split /:/, $id; |
| 598 |
|
| return unless @octets == 2; |
| 599 |
|
| |
| 600 |
|
| my $value = hex( $octets[0] . $octets[1] ); |
| 601 |
|
| return { |
| 602 |
|
| priority => ( $value >> 12 ) * 16, |
| 603 |
|
| port => $value & 0x0fff, |
| 604 |
|
| raw => $id, |
| 605 |
|
| }; |
| 606 |
|
| } |
| 607 |
|
| |
| 608 |
|
| # Non-accessible, but needed for consistency with other classes |
| 609 |
|
| sub v_index { |
| 610 |
|
| my $bridge = shift; |
| 611 |
|
| my $partial = shift; |
| 612 |
|
| |
| 613 |
|
| my $v_name = $bridge->v_name($partial); |
| 614 |
|
| my %v_index; |
| 615 |
|
| foreach my $idx ( keys %$v_name ) { |
| 616 |
|
| $v_index{$idx} = $idx; |
| 617 |
|
| } |
| 618 |
|
| return \%v_index; |
| 619 |
|
| } |
| 620 |
|
| |
| 621 |
|
| sub i_vlan { |
| 622 |
|
| my $bridge = shift; |
| 623 |
|
| my $partial = shift; |
| 624 |
|
| |
| 625 |
|
| my $index = $bridge->bp_index() or return {}; |
| 626 |
|
| |
| 627 |
|
| # If given a partial it will be an ifIndex, we need to use dot1dBasePort |
| 628 |
|
| if ($partial) { |
| 629 |
|
| my %r_index = reverse %$index; |
| 630 |
|
| $partial = $r_index{$partial}; |
| 631 |
|
| } |
| 632 |
|
| |
| 633 |
|
| my $i_pvid = $bridge->qb_i_vlan($partial) || {}; |
| 634 |
|
| my $i_vlan = {}; |
| 635 |
|
| |
| 636 |
|
| foreach my $bport ( keys %$i_pvid ) { |
| 637 |
|
| my $vlan = $i_pvid->{$bport}; |
| 638 |
|
| my $ifindex = $index->{$bport}; |
| 639 |
|
| unless ( defined $ifindex ) { |
| 640 |
|
| print " Port $bport has no bp_index mapping. Skipping.\n" |
| 641 |
|
| if $DEBUG; |
| 642 |
|
| next; |
| 643 |
|
| } |
| 644 |
|
| $i_vlan->{$ifindex} = $vlan; |
| 645 |
|
| } |
| 646 |
|
| |
| 647 |
|
| return $i_vlan; |
| 648 |
|
| } |
| 649 |
|
| |
| 650 |
|
| sub i_untagged { goto &i_vlan } |
| 651 |
|
| |
| 652 |
|
| sub i_vlan_membership { |
| 653 |
|
| my $bridge = shift; |
| 654 |
|
| my $partial = shift; |
| 655 |
|
| |
| 656 |
|
| # Use VlanCurrentTable if available since it will include dynamic |
| 657 |
|
| # VLANs. However, some devices do not populate the table. |
| 658 |
|
| my $v_ports = $bridge->qb_cv_egress() || $bridge->qb_v_egress(); |
| 659 |
|
| |
| 660 |
|
| return $bridge->_vlan_hoa($v_ports, $partial); |
| 661 |
|
| } |
| 662 |
|
| |
| 663 |
|
| sub i_vlan_membership_untagged { |
| 664 |
|
| my $bridge = shift; |
| 665 |
|
| my $partial = shift; |
| 666 |
|
| |
| 667 |
|
| # Use VlanCurrentTable if available since it will include dynamic |
| 668 |
|
| # VLANs. However, some devices do not populate the table. |
| 669 |
|
| my $v_ports = $bridge->qb_cv_untagged() || $bridge->qb_v_untagged(); |
| 670 |
|
| |
| 671 |
|
| return $bridge->_vlan_hoa($v_ports, $partial); |
| 672 |
|
| } |
| 673 |
|
| |
| 674 |
|
| sub _vlan_hoa { |
| 675 |
|
| my $bridge = shift; |
| 676 |
|
| my ( $v_ports, $partial ) = @_; |
| 677 |
|
| |
| 678 |
|
| my $index = $bridge->bp_index(); |
| 679 |
|
| |
| 680 |
|
| my $vlan_hoa = {}; |
| 681 |
|
| foreach my $idx ( keys %$v_ports ) { |
| 682 |
|
| next unless ( defined $v_ports->{$idx} ); |
| 683 |
|
| my $portlist = $v_ports->{$idx}; |
| 684 |
|
| my $ret = []; |
| 685 |
|
| my $vlan; |
| 686 |
|
| |
| 687 |
|
| # Strip TimeFilter if we're using VlanCurrentTable |
| 688 |
|
| ($vlan = $idx) =~ s/^(\d+\.)*//g; |
| 689 |
|
| |
| 690 |
|
| # Convert portlist bit array to bp_index array |
| 691 |
|
| for ( my $i = 0; $i <= $#$portlist; $i++ ) { |
| 692 |
|
| push( @{$ret}, $i + 1 ) if ( @$portlist[$i] ); |
| 693 |
|
| } |
| 694 |
|
| |
| 695 |
|
| #Create HoA ifIndex -> VLAN array |
| 696 |
|
| foreach my $port ( @{$ret} ) { |
| 697 |
|
| my $ifindex = $index->{$port}; |
| 698 |
|
| next unless ( defined($ifindex) ); # shouldn't happen |
| 699 |
|
| next if ( defined $partial and $ifindex !~ /^$partial$/ ); |
| 700 |
|
| push( @{ $vlan_hoa->{$ifindex} }, $vlan ); |
| 701 |
|
| } |
| 702 |
|
| } |
| 703 |
|
| return $vlan_hoa; |
| 704 |
|
| } |
| 705 |
|
| |
| 706 |
|
| sub set_i_pvid { |
| 707 |
|
| my $bridge = shift; |
| 708 |
|
| |
| 709 |
|
| $bridge->error_throw("VLAN set not supported."); |
| 710 |
|
| return; |
| 711 |
|
| } |
| 712 |
|
| |
| 713 |
|
| sub set_i_vlan { |
| 714 |
|
| my $bridge = shift; |
| 715 |
|
| |
| 716 |
|
| $bridge->error_throw("VLAN set not supported."); |
| 717 |
|
| return; |
| 718 |
|
| } |
| 719 |
|
| |
| 720 |
|
| sub set_i_untagged { goto &set_i_vlan } |
| 721 |
|
| |
| 722 |
|
| sub set_add_i_vlan_tagged { |
| 723 |
|
| my $bridge = shift; |
| 724 |
|
| |
| 725 |
|
| $bridge->error_throw("VLAN set not supported."); |
| 726 |
|
| return; |
| 727 |
|
| } |
| 728 |
|
| |
| 729 |
|
| sub set_remove_i_vlan_tagged { |
| 730 |
|
| my $bridge = shift; |
| 731 |
|
| |
| 732 |
|
| $bridge->error_throw("VLAN set not supported."); |
| 733 |
|
| return; |
| 734 |
|
| } |
| 735 |
|
| |
| 736 |
|
| # |
| 737 |
|
| # These are internal methods and are not documented. Do not use directly. |
| 738 |
|
| # |
| 739 |
|
| sub _check_forbidden_ports { |
| 740 |
|
| my $bridge = shift; |
| 741 |
|
| my ( $vlan_id, $index ) = @_; |
| 742 |
|
| return unless ( $vlan_id =~ /\d+/ and $index =~ /\d+/ ); |
| 743 |
|
| |
| 744 |
|
| my $iv_forbidden = $bridge->qb_v_fbdn_egress($vlan_id); |
| 745 |
|
| |
| 746 |
|
| my $forbidden_ports = $iv_forbidden->{$vlan_id}; |
| 747 |
|
| print "Forbidden ports: @$forbidden_ports\n" if $bridge->debug(); |
| 748 |
|
| if ( defined( @$forbidden_ports[ $index - 1 ] ) |
| 749 |
|
| and ( @$forbidden_ports[ $index - 1 ] eq "1" ) ) |
| 750 |
|
| { |
| 751 |
|
| print |
| 752 |
|
| "Error: Index: $index in forbidden list for VLAN: $vlan_id unable to add.\n" |
| 753 |
|
| if $bridge->debug(); |
| 754 |
|
| return; |
| 755 |
|
| } |
| 756 |
|
| return 1; |
| 757 |
|
| } |
| 758 |
|
| |
| 759 |
|
| sub _validate_vlan_param { |
| 760 |
|
| my $bridge = shift; |
| 761 |
|
| my ( $vlan_id, $ifindex ) = @_; |
| 762 |
|
| |
| 763 |
|
| # VID and ifIndex should both be numeric |
| 764 |
|
| unless (defined $vlan_id |
| 765 |
|
| and defined $ifindex |
| 766 |
|
| and $vlan_id =~ /^\d+$/ |
| 767 |
|
| and $ifindex =~ /^\d+$/ ) |
| 768 |
|
| { |
| 769 |
|
| $bridge->error_throw("Invalid parameter."); |
| 770 |
|
| return; |
| 771 |
|
| } |
| 772 |
|
| |
| 773 |
|
| # Check that ifIndex exists on device |
| 774 |
|
| my $index = $bridge->interfaces($ifindex); |
| 775 |
|
| |
| 776 |
|
| unless ( exists $index->{$ifindex} ) { |
| 777 |
|
| $bridge->error_throw("ifIndex $ifindex does not exist."); |
| 778 |
|
| return; |
| 779 |
|
| } |
| 780 |
|
| |
| 781 |
|
| #Check that VLAN exists on device |
| 782 |
|
| my $vtp_vlans = $bridge->load_qb_cv_stat() || $bridge->load_qb_v_stat(); |
| 783 |
|
| my $vlan_exists = 0; |
| 784 |
|
| |
| 785 |
|
| foreach my $iid ( keys %$vtp_vlans ) { |
| 786 |
|
| my $vlan = 0; |
| 787 |
|
| my $state = $vtp_vlans->{$iid}; |
| 788 |
|
| next unless defined $state; |
| 789 |
|
| if ( $iid =~ /(\d+)$/ ) { |
| 790 |
|
| $vlan = $1; |
| 791 |
|
| } |
| 792 |
|
| |
| 793 |
|
| $vlan_exists = 1 if ( $vlan_id eq $vlan ); |
| 794 |
|
| } |
| 795 |
|
| unless ($vlan_exists) { |
| 796 |
|
| $bridge->error_throw( |
| 797 |
|
| "VLAN $vlan_id does not exist or is not operational."); |
| 798 |
|
| return; |
| 799 |
|
| } |
| 800 |
|
| |
| 801 |
|
| return 1; |
| 802 |
|
| } |
| 803 |
|
| |
| 804 |
|
| 1; |
| 805 |
|
| |
| 806 |
|
| __END__ |