Info/CiscoVTP.pm untested

filter: Statementstmt 0.0% 0 / 253 Branchbran 0.0% 0 / 100 Conditioncond 0.0% 0 / 100 MC/DCmcdc 0.0% 0 / 59 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 540 CC 51 SCAR 42.6
CC
51
Cov
0%
i_vlan_membership
182.0
set_add_i_vlan_tagged
72.0
set_remove_i_vlan_tagged
72.0
CRAP 70.6
1 # SNMP::Info::CiscoVTP
2 #
3 # Copyright (c) 2008 Max Baker changes from version 0.8 and beyond.
4 #
5 # Copyright (c) 2003 Regents of the University of California
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
10 #
11 # * Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 # * Neither the name of the University of California, Santa Cruz nor the
17 # names of its contributors may be used to endorse or promote products
18 # derived from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31  
32 package SNMP::Info::CiscoVTP;
33  
34 use strict;
35 use warnings;
36 use Exporter;
37 use SNMP::Info;
38  
39 @SNMP::Info::CiscoVTP::ISA = qw/SNMP::Info Exporter/;
40 @SNMP::Info::CiscoVTP::EXPORT_OK = qw//;
41  
42 our ($VERSION, %MIBS, %FUNCS, %GLOBALS, %MUNGE);
43  
44 $VERSION = '3.985003';
45  
46 %MIBS = (
47 'CISCO-VTP-MIB' => 'vtpVlanName',
48 'CISCO-VLAN-MEMBERSHIP-MIB' => 'vmMembershipEntry',
49 'CISCO-VLAN-IFTABLE-RELATIONSHIP-MIB' => 'cviRoutedVlanIfIndex',
50 );
51  
52 %GLOBALS = (
53 'vtp_version' => 'vtpVersion',
54 'vtp_maxstore' => 'vtpMaxVlanStorage',
55 'vtp_notify' => 'vtpNotificationsEnabled',
56 'vtp_notify_create' => 'vtpVlanCreatedNotifEnabled',
57 'vtp_notify_delete' => 'vtpVlanDeletedNotifEnabled',
58 'vtp_trunk_set_serial' => 'vlanTrunkPortSetSerialNo',
59 );
60  
61 %FUNCS = (
62  
63 # CISCO-VTP-MIB::managementDomainTable
64 'vtp_d_name' => 'managementDomainName',
65 'vtp_d_mode' => 'managementDomainLocalMode',
66 'vtp_d_rev' => 'managementDomainConfigRevNumber',
67 'vtp_d_updater' => 'managementDomainLastUpdater',
68 'vtp_d_last' => 'managementDomainLastChange',
69 'vtp_d_status' => 'managementDomainRowStatus',
70 'vtp_d_tftp' => 'managementDomainTftpServer',
71 'vtp_d_tftp_path' => 'managementDomainTftpPathname',
72 'vtp_d_pruning' => 'managementDomainPruningState',
73 'vtp_d_ver' => 'managementDomainVersionInUse',
74  
75 # CISCO-VTP-MIB::vtpVlanTable
76 'v_state' => 'vtpVlanState',
77 'v_type' => 'vtpVlanType',
78 'v_name' => 'vtpVlanName',
79 'v_mtu' => 'vtpVlanMtu',
80 'v_said' => 'vtpVlanDot10Said',
81 'v_ring' => 'vtpVlanRingNumber',
82 'v_bridge' => 'vtpVlanBridgeNumber',
83 'v_stp' => 'vtpVlanStpType',
84 'v_parent' => 'vtpVlanParentVlan',
85 'v_trans1' => 'vtpVlanTranslationalVlan1',
86 'v_trans2' => 'vtpVlanTranslationalVlan2',
87 'v_btype' => 'vtpVlanBridgeType',
88 'v_hop_are' => 'vtpVlanAreHopCount',
89 'v_hop_ste' => 'vtpVlanSteHopCount',
90 'v_crf' => 'vtpVlanIsCRFBackup',
91 'v_type_ext' => 'vtpVlanTypeExt',
92 'v_if' => 'vtpVlanIfIndex',
93  
94 # CISCO-VLAN-MEMBERSHIP-MIB::vmMembershipTable
95 'i_vlan_type' => 'vmVlanType',
96 'i_vlan2' => 'vmVlan',
97 'i_vlan_stat' => 'vmPortStatus',
98 'i_vlan_1' => 'vmVlans',
99 'i_vlan_2' => 'vmVlans2k',
100 'i_vlan_3' => 'vmVlans3k',
101 'i_vlan_4' => 'vmVlans4k',
102  
103 # CISCO-VLAN-MEMBERSHIP-MIB::vmVoiceVlanTable
104 'i_voice_vlan' => 'vmVoiceVlanId',
105  
106 # CISCO-VLAN-IFTABLE-RELATIONSHIP-MIB
107 'v_cvi_if' => 'cviRoutedVlanIfIndex',
108  
109 # CISCO-VTP-MIB::vlanTrunkPortTable
110 'vtp_trunk_mgmt_dom' => 'vlanTrunkPortManagementDomain',
111 'vtp_trunk_encaps_t' => 'vlanTrunkPortEncapsulationType',
112 'vtp_trunk_vlans' => 'vlanTrunkPortVlansEnabled',
113 'vtp_trunk_vlans_2k' => 'vlanTrunkPortVlansEnabled2k',
114 'vtp_trunk_vlans_3k' => 'vlanTrunkPortVlansEnabled3k',
115 'vtp_trunk_vlans_4k' => 'vlanTrunkPortVlansEnabled4k',
116 'vtp_trunk_native' => 'vlanTrunkPortNativeVlan',
117 'i_pvid' => 'vlanTrunkPortNativeVlan',
118 'vtp_trunk_rstat' => 'vlanTrunkPortRowStatus',
119 'vtp_trunk_dyn' => 'vlanTrunkPortDynamicState',
120 'vtp_trunk_dyn_stat' => 'vlanTrunkPortDynamicStatus',
121 'vtp_trunk_vtp' => 'vlanTrunkPortVtpEnabled',
122 'vtp_trunk_encaps' => 'vlanTrunkPortEncapsulationOperType',
123  
124 # TODO Add these tables if someone wants them..
125 # vtpEditControlTable
126 # vtpVlanEditTable
127 # vtpStatsTable
128 );
129  
130 %MUNGE = ();
131  
132 sub v_index {
133 my $vtp = shift;
134 my $partial = shift;
135  
136 my $v_name = $vtp->v_name($partial);
137 my %v_index;
138 foreach my $idx ( keys %$v_name ) {
139 my ( $mgmtdomain, $vlan ) = split( /\./, $idx );
140 $v_index{$idx} = $vlan;
141 }
142 return \%v_index;
143 }
144  
145 sub i_vlan {
146 my $vtp = shift;
147 my $partial = shift;
148  
149 my $port_vlan = $vtp->vtp_trunk_native($partial) || {};
150 my $i_vlan = $vtp->i_vlan2($partial) || {};
151 my $trunk_dyn = $vtp->vtp_trunk_dyn($partial) || {};
152 my $trunk_dyn_stat = $vtp->vtp_trunk_dyn_stat($partial) || {};
153  
154 my %i_vlans;
155  
156 # Get access ports
157 foreach my $port ( keys %$i_vlan ) {
158 my $vlan = $i_vlan->{$port};
159 next unless defined $vlan;
160  
161 $i_vlans{$port} = $vlan;
162 }
163  
164 # Get trunk ports
165 foreach my $port ( keys %$port_vlan ) {
166 my $vlan = $port_vlan->{$port};
167 next unless defined $vlan;
168  
169 # ports up and trunking should have a trunking status
170 my $stat = $trunk_dyn_stat->{$port};
171  
172 # vtp_trunk_dyn_stat is not useful for down ports
173 # so we use vtp_trunk_dyn to see if trunking is set
174 my $dyn = $trunk_dyn->{$port};
175  
176 if (($stat and $stat =~ /^trunking/ )
177 or ($dyn and (($dyn eq 'on') or ($dyn eq 'onNoNegotiate'))))
178 {
179 $i_vlans{$port} = $vlan;
180 }
181 }
182  
183 # Check in CISCO-VLAN-IFTABLE-RELATION-MIB
184 # Used for traditional Cisco Routers and Aironet
185  
186 my $v_cvi_if = $vtp->v_cvi_if();
187 if ( defined $v_cvi_if ) {
188  
189 # Translate vlan.physical_interface -> iid
190 # to iid -> vlan
191 foreach my $i ( keys %$v_cvi_if ) {
192 my ( $vlan, $phys ) = split( /\./, $i );
193 my $iid = $v_cvi_if->{$i};
194  
195 $i_vlans{$iid} = $vlan;
196 }
197 }
198  
199 return \%i_vlans;
200 }
201  
202 sub i_subinterfaces {
203 my $vtp = shift;
204 my %i_subs = %{ $vtp->SUPER::i_subinterfaces() || {} };
205  
206 # CISCO-VLAN-IFTABLE-RELATION-MIB
207 # Used for traditional Cisco Routers and Aironet
208  
209 my $v_cvi_if = $vtp->v_cvi_if();
210 if ( defined $v_cvi_if ) {
211  
212 # Translate vlan.parent_physical_interface_iid -> iid
213 # to iid -> [iid, iid, ...]
214 foreach my $i ( keys %$v_cvi_if ) {
215 my ( $vlan, $phys ) = split( /\./, $i );
216 my $iid = $v_cvi_if->{$i};
217  
218 next unless $phys and $iid;
219 next if $phys == $iid;
220  
221 push @{ $i_subs{$phys} }, $iid;
222 }
223 }
224  
225 return \%i_subs;
226 }
227  
228 sub i_untagged {
229 my $vtp = shift;
230 my ( $ifindex ) = @_;
231  
232 # cannot defer to i_vlan - vtp_trunk_dyn_stat is not useful for down ports
233 # so we use vtp_trunk_dyn as a hint to use i_pvid
234  
235 my $trunking = eval { $vtp->vtp_trunk_dyn($ifindex)->{$ifindex} };
236 if ($trunking and (($trunking eq 'on') or ($trunking eq 'onNoNegotiate'))) {
237 return $vtp->i_pvid(@_);
238 }
239 else {
240 return $vtp->i_vlan(@_);
241 }
242 }
243  
244 sub i_vlan_membership {
245 my $vtp = shift;
246 my $partial = shift;
247  
248 my $ports_vlans = $vtp->vtp_trunk_vlans($partial) || {};
249 my $ports_vlans_2k = $vtp->vtp_trunk_vlans_2k($partial) || {};
250 my $ports_vlans_3k = $vtp->vtp_trunk_vlans_3k($partial) || {};
251 my $ports_vlans_4k = $vtp->vtp_trunk_vlans_4k($partial) || {};
252 my $vtp_vlans = $vtp->v_state();
253 my $i_vlan = $vtp->i_vlan2($partial) || {};
254 my $trunk_dyn_stat = $vtp->vtp_trunk_dyn_stat($partial) || {};
255 my $trunk_dyn = $vtp->vtp_trunk_dyn($partial) || {};
256 my $i_voice_vlan = $vtp->i_voice_vlan($partial) || {};
257  
258 my $i_vlan_membership = {};
259  
260 # Get access ports
261 foreach my $port ( keys %$i_vlan ) {
262 my $vlan = $i_vlan->{$port};
263 next unless defined $vlan;
264 my $dyn = $trunk_dyn->{$port};
265 unless ($dyn and (($dyn eq 'on') or ($dyn eq 'onNoNegotiate'))) {
266 push( @{ $i_vlan_membership->{$port} }, $vlan );
267 }
268 }
269  
270 # Get voice VLANs
271 foreach my $port ( keys %$i_voice_vlan ) {
272 my $vlan = $i_voice_vlan->{$port};
273 next unless defined $vlan;
274 next unless ($vlan =~ m/[[:digit:]]+/ and $vlan < 4095);
275 my $dyn = $trunk_dyn->{$port};
276 unless ($dyn and (($dyn eq 'on') or ($dyn eq 'onNoNegotiate'))) {
277 push( @{ $i_vlan_membership->{$port} }, $vlan );
278 }
279 }
280  
281 # Get trunk ports
282 my %oper_vlans;
283 foreach my $iid ( keys %$vtp_vlans ) {
284 my $vlan = 0;
285 my $vtp_dom = 0;
286 my $state = $vtp_vlans->{$iid};
287 next unless defined $state;
288 next if $state !~ /operational/;
289 if ( $iid =~ /(\d+)\.(\d+)/ ) {
290 $vtp_dom = $1;
291 $vlan = $2;
292 }
293 $oper_vlans{$vlan}++;
294 }
295  
296 foreach my $port ( keys %$ports_vlans ) {
297 my $stat = $trunk_dyn_stat->{$port};
298 my $dyn = $trunk_dyn->{$port};
299 if (($stat and $stat =~ /^trunking/ )
300 or ($dyn and (($dyn eq 'on') or ($dyn eq 'onNoNegotiate')))) {
301 my $k = 0;
302 my $list1 = $ports_vlans->{$port} || '0';
303 my $list2 = $ports_vlans_2k->{$port} || '0';
304 my $list3 = $ports_vlans_3k->{$port} || '0';
305 my $list4 = $ports_vlans_4k->{$port} || '0';
306 foreach my $list ( "$list1", "$list2", "$list3", "$list4" ) {
307 my $offset = 1024 * $k++;
308 next unless $list;
309 my $vlanlist = [ split( //, unpack( "B*", $list ) ) ];
310 foreach my $vlan ( keys %oper_vlans ) {
311 next if (($vlan < $offset) or ($vlan - $offset > 1024));
312 push( @{ $i_vlan_membership->{$port} }, $vlan )
313 if ( @$vlanlist[ $vlan - $offset ] );
314 }
315 }
316 }
317 }
318  
319 return $i_vlan_membership;
320 }
321  
322 sub i_vlan_membership_untagged {
323 my $vtp = shift;
324 my $partial = shift;
325  
326 my $vlans = $vtp->i_vlan($partial);
327 my $i_vlan_membership = {};
328 foreach my $port (keys %$vlans) {
329 my $vlan = $vlans->{$port};
330 push( @{ $i_vlan_membership->{$port} }, $vlan );
331 }
332  
333 return $i_vlan_membership;
334 }
335  
336 sub set_i_pvid {
337 my $vtp = shift;
338 my ( $vlan_id, $ifindex ) = @_;
339  
340 return unless ( $vtp->_validate_vlan_param( $vlan_id, $ifindex ) );
341  
342 my $native_vlan = $vtp->vtp_trunk_native($ifindex);
343 if ( defined $native_vlan ) {
344  
345 print
346 "Changing native VLAN from $native_vlan->{$ifindex} to $vlan_id on IfIndex: $ifindex\n"
347 if $vtp->debug();
348  
349 my $rv = $vtp->set_vtp_trunk_native( $vlan_id, $ifindex );
350 unless ($rv) {
351 $vtp->error_throw(
352 "Unable to change native VLAN to $vlan_id on IfIndex: $ifindex"
353 );
354 return;
355 }
356 return $rv;
357 }
358 $vtp->error_throw("Can't find ifIndex: $ifindex - Is it a trunk port?");
359 return;
360 }
361  
362 sub set_i_vlan {
363 my $vtp = shift;
364 my ( $vlan_id, $ifindex ) = @_;
365  
366 return unless ( $vtp->_validate_vlan_param( $vlan_id, $ifindex ) );
367  
368 my $i_vlan = $vtp->i_vlan2($ifindex);
369 if ( defined $i_vlan ) {
370  
371 print
372 "Changing VLAN from $i_vlan->{$ifindex} to $vlan_id on IfIndex: $ifindex\n"
373 if $vtp->debug();
374  
375 my $rv = $vtp->set_i_vlan2( $vlan_id, $ifindex );
376 unless ($rv) {
377 $vtp->error_throw(
378 "Unable to change VLAN to $vlan_id on IfIndex: $ifindex");
379 return;
380 }
381 return $rv;
382 }
383 $vtp->error_throw("Can't find ifIndex: $ifindex - Is it an access port?");
384 return;
385 }
386  
387 sub set_i_untagged {
388 my $vtp = shift;
389 my ( $vlan_id, $ifindex ) = @_;
390  
391 # cannot defer to i_vlan - vtp_trunk_dyn_stat is not useful for down ports
392 # so we use vtp_trunk_dyn as a hint to use i_pvid
393  
394 my $trunking = eval { $vtp->vtp_trunk_dyn($ifindex)->{$ifindex} };
395 if ($trunking and (($trunking eq 'on') or ($trunking eq 'onNoNegotiate'))) {
396 return $vtp->set_i_pvid(@_);
397 }
398 else {
399 return $vtp->set_i_vlan(@_);
400 }
401 }
402  
403 sub set_add_i_vlan_tagged {
404 my $vtp = shift;
405 my ( $vlan_id, $ifindex ) = @_;
406  
407 return unless ( $vtp->_validate_vlan_param( $vlan_id, $ifindex ) );
408  
409 print "Adding VLAN: $vlan_id to ifIndex: $ifindex\n" if $vtp->debug();
410  
411 my $trunk_serial = $vtp->load_vtp_trunk_set_serial();
412 my $trunk_members = $vtp->vtp_trunk_vlans($ifindex);
413  
414 unless ( defined $trunk_members ) {
415 $vtp->error_throw(
416 "Can't find ifIndex: $ifindex - Is it a trunk port?");
417 return;
418 }
419  
420 my @member_list = split( //, unpack( "B*", $trunk_members->{$ifindex} ) );
421  
422 print "Original vlan list for ifIndex: $ifindex: @member_list \n"
423 if $vtp->debug();
424 $member_list[$vlan_id] = '1';
425 print "Modified vlan list for ifIndex: $ifindex: @member_list \n"
426 if $vtp->debug();
427 my $new_list = pack( "B*", join( '', @member_list ) );
428  
429 #Add VLAN to member list
430 my $list_rv = $vtp->set_vtp_trunk_vlans( $new_list, $ifindex );
431 unless ($list_rv) {
432 $vtp->error_throw(
433 "Unable to add VLAN: $vlan_id to ifIndex: $ifindex member list");
434 return;
435 }
436  
437 #Make sure no other SNMP manager was making modifications at the same time.
438 my $serial_rv = $vtp->set_vtp_trunk_set_serial($trunk_serial);
439 unless ($serial_rv) {
440 $vtp->error_throw(
441 "Unable to increment trunk set serial number - check configuration!"
442 );
443 return;
444 }
445 return 1;
446 }
447  
448 sub set_remove_i_vlan_tagged {
449 my $vtp = shift;
450 my ( $vlan_id, $ifindex ) = @_;
451  
452 return unless ( $vtp->_validate_vlan_param( $vlan_id, $ifindex ) );
453  
454 print "Removing VLAN: $vlan_id from ifIndex: $ifindex\n" if $vtp->debug();
455  
456 my $trunk_serial = $vtp->load_vtp_trunk_set_serial();
457 my $trunk_members = $vtp->vtp_trunk_vlans($ifindex);
458  
459 unless ( defined $trunk_members ) {
460 $vtp->error_throw(
461 "Can't find ifIndex: $ifindex - Is it a trunk port?");
462 return;
463 }
464  
465 my @member_list = split( //, unpack( "B*", $trunk_members->{$ifindex} ) );
466  
467 print "Original vlan list for ifIndex: $ifindex: @member_list \n"
468 if $vtp->debug();
469 $member_list[$vlan_id] = '0';
470 print "Modified vlan list for ifIndex: $ifindex: @member_list \n"
471 if $vtp->debug();
472 my $new_list = pack( "B*", join( '', @member_list ) );
473  
474 #Remove VLAN to member list
475 my $list_rv = $vtp->set_vtp_trunk_vlans( $new_list, $ifindex );
476 unless ($list_rv) {
477 $vtp->error_throw(
478 "Error: Unable to remove VLAN: $vlan_id from ifIndex: $ifindex member list"
479 );
480 return;
481 }
482  
483 #Make sure no other manager was making modifications at the same time.
484 my $serial_rv = $vtp->set_vtp_trunk_set_serial($trunk_serial);
485 unless ($serial_rv) {
486 $vtp->error_throw(
487 "Error: Unable to increment trunk set serial number - check configuration!"
488 );
489 return;
490 }
491 return 1;
492 }
493  
494 #
495 # These are internal methods and are not documented. Do not use directly.
496 #
497 sub _validate_vlan_param {
498 my $vtp = shift;
499 my ( $vlan_id, $ifindex ) = @_;
500  
501 # VID and ifIndex should both be numeric
502 unless (defined $vlan_id
503 and defined $ifindex
504 and $vlan_id =~ /^\d+$/
505 and $ifindex =~ /^\d+$/ )
506 {
507 $vtp->error_throw("Invalid parameter");
508 return;
509 }
510  
511 # Check that ifIndex exists on device
512 my $index = $vtp->interfaces($ifindex);
513  
514 unless ( exists $index->{$ifindex} ) {
515 $vtp->error_throw("ifIndex $ifindex does not exist");
516 return;
517 }
518  
519 #Check that VLAN exists on device
520 my $vtp_vlans = $vtp->v_state();
521 my $vlan_exists = 0;
522  
523 foreach my $iid ( keys %$vtp_vlans ) {
524 my $vlan = 0;
525 my $vtp_dom = 0;
526 my $state = $vtp_vlans->{$iid};
527 next unless defined $state;
528 next if $state !~ /operational/;
529 if ( $iid =~ /(\d+)\.(\d+)/ ) {
530 $vtp_dom = $1;
531 $vlan = $2;
532 }
533  
534 $vlan_exists = 1 if ( $vlan_id eq $vlan );
535 }
536 unless ($vlan_exists) {
537 $vtp->error_throw(
538 "VLAN $vlan_id does not exist or is not operational");
539 return;
540 }
541  
542 return 1;
543 }
544  
545 1;
546 __END__