Info/Layer3.pm untested

filter: Statementstmt 0.0% 0 / 198 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 92 MC/DCmcdc 0.0% 0 / 51 Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 458 CC 37 SCAR 29.9
CC
37
Cov
0%
root_ip
42.0
i_duplex
42.0
_n2p_ipv4_column
42.0
CRAP 19.9
1 # SNMP::Info::Layer3 - SNMP Interface to Layer3 devices
2 #
3 # Copyright (c) 2008 Max Baker -- All changes from Version 0.7 on
4 #
5 # Copyright (c) 2002,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::Layer3;
33  
34 use strict;
35 use warnings;
36 use Exporter;
37 use SNMP::Info;
38 use SNMP::Info::Bridge;
39 use SNMP::Info::EtherLike;
40 use SNMP::Info::Entity;
41 use SNMP::Info::PowerEthernet;
42 use SNMP::Info::IPv6;
43 use SNMP::Info::AdslLine;
44 use SNMP::Info::LLDP;
45 use SNMP::Info::DocsisHE;
46 use SNMP::Info::VRF;
47 use SNMP::Info::IEEE802dot3ad;
48 use SNMP::Info::L2Service;
49  
50 @SNMP::Info::Layer3::ISA = qw/
51 SNMP::Info::PowerEthernet SNMP::Info::IPv6
52 SNMP::Info::Entity SNMP::Info::EtherLike
53 SNMP::Info::Bridge SNMP::Info::AdslLine
54 SNMP::Info::LLDP SNMP::Info::DocsisHE
55 SNMP::Info::VRF SNMP::Info SNMP::Info::IEEE802dot3ad
56 SNMP::Info::L2Service Exporter/;
57 @SNMP::Info::Layer3::EXPORT_OK = qw//;
58  
59 our ($VERSION, %GLOBALS, %FUNCS, %MIBS, %MUNGE);
60  
61 $VERSION = '3.985003';
62  
63 %MIBS = (
64 %SNMP::Info::MIBS,
65 %SNMP::Info::VRF::MIBS,
66 %SNMP::Info::AdslLine::MIBS,
67 %SNMP::Info::Bridge::MIBS,
68 %SNMP::Info::DocsisHE::MIBS,
69 %SNMP::Info::EtherLike::MIBS,
70 %SNMP::Info::Entity::MIBS,
71 %SNMP::Info::PowerEthernet::MIBS,
72 %SNMP::Info::IPv6::MIBS,
73 %SNMP::Info::LLDP::MIBS,
74 %SNMP::Info::IEEE802dot3ad::MIBS,
75 %SNMP::Info::L2Service::MIBS,
76 'IP-MIB' => 'ipNetToMediaIfIndex',
77 'OSPF-MIB' => 'ospfRouterId',
78 'ISIS-MIB' => 'isisSysID',
79 'BGP4-MIB' => 'bgpIdentifier',
80 );
81  
82 %GLOBALS = (
83  
84 # Inherit the super class ones
85 %SNMP::Info::GLOBALS,
86 %SNMP::Info::VRF::GLOBALS,
87 %SNMP::Info::AdslLine::GLOBALS,
88 %SNMP::Info::Bridge::GLOBALS,
89 %SNMP::Info::DocsisHE::GLOBALS,
90 %SNMP::Info::EtherLike::GLOBALS,
91 %SNMP::Info::Entity::GLOBALS,
92 %SNMP::Info::PowerEthernet::GLOBALS,
93 %SNMP::Info::IPv6::GLOBALS,
94 %SNMP::Info::LLDP::GLOBALS,
95 %SNMP::Info::IEEE802dot3ad::GLOBALS,
96 %SNMP::Info::L2Service::GLOBALS,
97 'mac' => 'ifPhysAddress.1',
98 'serial1' =>
99 '.1.3.6.1.4.1.9.3.6.3.0', # OLD-CISCO-CHASSIS-MIB::chassisId.0
100 'router_ip' => 'ospfRouterId.0',
101 'bgp_id' => 'bgpIdentifier.0',
102 'bgp_local_as' => 'bgpLocalAs.0',
103 );
104  
105 %FUNCS = (
106 %SNMP::Info::FUNCS,
107 %SNMP::Info::VRF::FUNCS,
108 %SNMP::Info::AdslLine::FUNCS,
109 %SNMP::Info::Bridge::FUNCS,
110 %SNMP::Info::DocsisHE::FUNCS,
111 %SNMP::Info::EtherLike::FUNCS,
112 %SNMP::Info::Entity::FUNCS,
113 %SNMP::Info::PowerEthernet::FUNCS,
114 %SNMP::Info::IPv6::FUNCS,
115 %SNMP::Info::LLDP::FUNCS,
116 %SNMP::Info::IEEE802dot3ad::FUNCS,
117 %SNMP::Info::L2Service::FUNCS,
118  
119 # Obsolete Address Translation Table (ARP Cache)
120 'old_at_index' => 'atIfIndex',
121 'old_at_paddr' => 'atPhysAddress',
122 'old_at_netaddr' => 'atNetAddress',
123  
124 # IP-MIB IP Net to Media Table (ARP Cache)
125 'at_index' => 'ipNetToMediaIfIndex',
126 'at_paddr' => 'ipNetToMediaPhysAddress',
127 'at_netaddr' => 'ipNetToMediaNetAddress',
128  
129 # OSPF-MIB::ospfIfTable
130 'ospf_if_ip' => 'ospfIfIpAddress',
131 'ospf_if_area' => 'ospfIfAreaId',
132 'ospf_if_type' => 'ospfIfType',
133 'ospf_if_hello' => 'ospfIfHelloInterval',
134 'ospf_if_dead' => 'ospfIfRtrDeadInterval',
135 'ospf_if_admin' => 'ospfIfAdminStat',
136 'ospf_if_state' => 'ospfIfState',
137  
138 # OSPF-MIB::ospfNbrTable
139 'ospf_ip' => 'ospfHostIpAddress',
140 'ospf_peers' => 'ospfNbrIpAddr',
141 'ospf_peer_id' => 'ospfNbrRtrId',
142 'ospf_peer_state' => 'ospfNbrState',
143  
144 # ISIS-MIB::isisCircTable
145 'isis_circ_if_idx' => 'isisCircIfIndex' ,
146 'isis_circ_admin' => 'isisCircAdminState',
147 'isis_circ_type' => 'isisCircType',
148 'isis_circ_level_type' => 'isisCircLevelType',
149  
150 # ISIS-MIB::isisISAdjTable
151 'isis_adj_state' => 'isisISAdjState',
152 'isis_adj_type' => 'isisISAdjNeighSysType',
153 'isis_adj_usage' => 'isisISAdjUsage',
154 'isis_adj_id' => 'isisISAdjNeighSysID',
155 'isis_adj_ip_type' => 'isisISAdjIPAddrType',
156 'isis_adj' => 'isisISAdjIPAddrAddress',
157  
158 # BGP4-MIB::bgpPeerTable
159 'bgp_peers' => 'bgpPeerLocalAddr',
160 'bgp_peer_id' => 'bgpPeerIdentifier',
161 'bgp_peer_state' => 'bgpPeerState',
162 'bgp_peer_as' => 'bgpPeerRemoteAs',
163 'bgp_peer_addr' => 'bgpPeerRemoteAddr',
164 'bgp_peer_fsm_est_trans' => 'bgpPeerFsmEstablishedTransitions',
165 'bgp_peer_in_tot_msgs' => 'bgpPeerInTotalMessages',
166 'bgp_peer_in_upd_el_time' => 'bgpPeerInUpdateElapsedTime',
167 'bgp_peer_in_upd' => 'bgpPeerInUpdates',
168 'bgp_peer_out_tot_msgs' => 'bgpPeerOutTotalMessages',
169 'bgp_peer_out_upd' => 'bgpPeerOutUpdates',
170  
171 # IP-MIB Net to Physical Table (ARP Cache)
172 'n2p_paddr' => 'ipNetToPhysicalPhysAddress',
173 'n2p_lastupdate' => 'ipNetToPhysicalLastUpdated',
174 'n2p_ptype' => 'ipNetToPhysicalType',
175 'n2p_pstate' => 'ipNetToPhysicalState',
176 'n2p_pstatus' => 'ipNetToPhysicalRowStatus',
177  
178 );
179  
180 %MUNGE = (
181  
182 # Inherit all the built in munging
183 %SNMP::Info::MUNGE,
184 %SNMP::Info::VRF::MUNGE,
185 %SNMP::Info::AdslLine::MUNGE,
186 %SNMP::Info::Bridge::MUNGE,
187 %SNMP::Info::EtherLike::MUNGE,
188 %SNMP::Info::DocsisHE::MUNGE,
189 %SNMP::Info::Entity::MUNGE,
190 %SNMP::Info::PowerEthernet::MUNGE,
191 %SNMP::Info::IPv6::MUNGE,
192 %SNMP::Info::LLDP::MUNGE,
193 %SNMP::Info::IEEE802dot3ad::MUNGE,
194 %SNMP::Info::L2Service::MUNGE,
195 'old_at_paddr' => \&SNMP::Info::munge_mac,
196 'at_paddr' => \&SNMP::Info::munge_mac,
197 'n2p_paddr' => \&SNMP::Info::munge_mac,
198 );
199  
200 sub isis_peers {
201 my $l3 = shift;
202  
203 my $isis_peers = {};
204  
205 # Returns hexstrings. Need to convert to IPv4 dotted or IPv6 hex notation
206 my $adjacencies = $l3->isis_adj();
207 foreach my $key (keys %$adjacencies) {
208 my $hexstr = $adjacencies->{$key};
209 my $l = length $hexstr;
210 my $ip;
211 # 4 bytes = IPv4
212 if ($l == 4) {
213 $ip = join(".", unpack("C*", $hexstr));
214 $isis_peers->{$key} = $ip;
215 }
216 # 16 bytes = IPv6
217 elsif ($l == 16) {
218 $ip = unpack("H*", $hexstr);
219 $ip =~ s/....(?=.)\K/:/sg ;
220 $isis_peers->{$key} = $ip;
221 }
222 }
223 return $isis_peers;
224 }
225  
226 # Method OverRides
227  
228 sub root_ip {
229 my $l3 = shift;
230  
231 my $router_ip = $l3->router_ip();
232 my $ospf_ip = $l3->ospf_ip();
233  
234 # if the router ip exists and is a route advertised by the device we prefer
235 # it over the others
236 return $router_ip
237 if (( defined $router_ip )
238 and ( $router_ip ne '0.0.0.0' )
239 and ( grep { $ospf_ip->{$_} eq $router_ip } ( keys %$ospf_ip ) )
240 and ( $l3->snmp_connect_ip($router_ip) ) );
241  
242 # return the first one found here (should be only one)
243 if ( defined $ospf_ip and scalar( keys %$ospf_ip ) ) {
244 foreach my $key ( keys %$ospf_ip ) {
245 my $ip = $ospf_ip->{$key};
246 next if $ip eq '0.0.0.0';
247 next unless $l3->snmp_connect_ip($ip);
248 print " SNMP::Layer3::root_ip() using $ip\n" if $l3->debug();
249 return $ip;
250 }
251 }
252  
253 return;
254 }
255  
256 sub serial {
257 my $l3 = shift;
258  
259 my $entity_serial = $l3->entity_derived_serial();
260 if ( defined $entity_serial and $entity_serial !~ /^\s*$/ ){
261 return $entity_serial;
262 }
263  
264 my $serial1 = $l3->serial1();
265 if ( defined $serial1 and $serial1 !~ /^\s*$/ ) {
266 return $serial1;
267 }
268  
269 return;
270  
271 }
272  
273 # $l3->model() - the sysObjectID returns an IID to an entry in
274 # the CISCO-PRODUCT-MIB. Look it up and return it.
275 sub model {
276 my $l3 = shift;
277 my $id = $l3->id();
278  
279 unless ( defined $id ) {
280 print
281 " SNMP::Info::Layer3::model() - Device does not support sysObjectID\n"
282 if $l3->debug();
283 return;
284 }
285  
286 my $model = &SNMP::translateObj($id);
287  
288 return $id unless defined $model;
289  
290 $model =~ s/^cisco//i;
291 $model =~ s/^catalyst//;
292 $model =~ s/^cat//;
293 return $model;
294 }
295  
296 sub i_subinterfaces {
297 my $dev = shift;
298 my $partial = shift;
299  
300 my $ifstack = $dev->i_stack_status() || {};
301 # TODO: if we want to do partial, we need to use inverse status
302 my $iftype = $dev->i_type() || {};
303  
304 my $ret = $dev->SUPER::i_subinterfaces() || {};
305  
306 foreach my $idx ( keys %$ifstack ) {
307 next unless $ifstack->{$idx} eq 'active';
308  
309 my ( $higher, $lower ) = split /\./, $idx;
310 next if ( $higher == 0 or $lower == 0 );
311  
312 if ( $iftype->{ $higher } and
313 ($iftype->{ $higher } eq 'l2vlan' or $iftype->{ $higher } eq 'l3ipvlan') ) {
314 push @{ $ret->{ $lower } }, $higher;
315 }
316 }
317  
318 return $ret;
319 }
320  
321 sub i_name {
322 my $l3 = shift;
323 my $partial = shift;
324  
325 my $i_index = $l3->i_index($partial);
326 my $i_alias = $l3->i_alias($partial);
327 my $i_name2 = $l3->orig_i_name($partial);
328  
329 my %i_name;
330 foreach my $iid ( keys %$i_name2 ) {
331 my $name = $i_name2->{$iid};
332 my $alias = $i_alias->{$iid};
333 $i_name{$iid}
334 = ( defined $alias and $alias !~ /^\s*$/ )
335 ? $alias
336 : $name;
337 }
338  
339 return \%i_name;
340 }
341  
342 sub i_duplex {
343 my $l3 = shift;
344 my $partial = shift;
345  
346 my $el_index = $l3->el_index($partial);
347 my $el_duplex = $l3->el_duplex($partial);
348  
349 my %i_index;
350 foreach my $el_port ( keys %$el_duplex ) {
351 my $iid = $el_index->{$el_port};
352 next unless defined $iid;
353 my $duplex = $el_duplex->{$el_port};
354 next unless defined $duplex;
355  
356 $i_index{$iid} = 'half' if $duplex =~ /half/i;
357 $i_index{$iid} = 'full' if $duplex =~ /full/i;
358 $i_index{$iid} = 'auto' if $duplex =~ /auto/i;
359 }
360  
361 return \%i_index;
362 }
363  
364 # $l3->interfaces() - Map the Interfaces to their physical names
365 sub interfaces {
366 my $l3 = shift;
367 my $partial = shift;
368  
369 my $interfaces = $l3->i_index($partial);
370 my $i_descr = $l3->i_description($partial);
371  
372 # Check for duplicates in ifDescr, if so uniquely identify by adding
373 # ifIndex to repeated values
374 my (%seen, %first_seen_as);
375 foreach my $iid ( sort keys %$i_descr ) {
376 my $port = $i_descr->{$iid};
377 next unless defined $port;
378  
379 $port = SNMP::Info::munge_null($port);
380 $port =~ s/^\s+//; $port =~ s/\s+$//;
381 next unless length $port;
382  
383 if ( $seen{$port}++ ) {
384 # (#320) also fixup the port this is a duplicate of
385 $interfaces->{ $first_seen_as{$port} }
386 = sprintf( "%s (%d)", $port, $first_seen_as{$port} );
387  
388 $interfaces->{$iid} = sprintf( "%s (%d)", $port, $iid );
389 }
390 else {
391 $interfaces->{$iid} = $port;
392 $first_seen_as{$port} = $iid;
393 }
394 }
395 return $interfaces;
396 }
397  
398 sub vendor {
399 my $l3 = shift;
400  
401 my $descr = $l3->description() || '';
402 my $id = $l3->id() || '';
403  
404 # .1.3.6.1.4.1.9.1 is the CISCO-PRODUCTS-MIB
405 # .1.3.6.1.4.1.9.9.368.4 is an old tree that Cisco CSSs were numbered from
406 return 'cisco' if $id =~ /^\Q.1.3.6.1.4.1.9.1.\E\d+$/;
407 return 'cisco' if $id =~ /^\Q.1.3.6.1.4.1.9.9.368.4.\E\d+/;
408 return 'cisco' if ( $descr =~ /(cisco|\bios\b)/i );
409 return 'brocade' if ( $descr =~ /foundry/i );
410  
411 return 'unknown';
412  
413 }
414  
415 sub at_index {
416 my $l3 = shift;
417 my $partial = shift;
418  
419 return scalar( $l3->_n2p_over_loopback( 'index', $partial )
420 || $l3->orig_at_index($partial)
421 || $l3->old_at_index($partial)
422 || $l3->_n2p_ipv4_column( 'index', $partial ) );
423 }
424  
425 sub at_paddr {
426 my $l3 = shift;
427 my $partial = shift;
428  
429 return scalar( $l3->_n2p_over_loopback( 'paddr', $partial )
430 || $l3->orig_at_paddr($partial)
431 || $l3->old_at_paddr($partial)
432 || $l3->_n2p_ipv4_column( 'paddr', $partial ) );
433 }
434  
435 sub at_netaddr {
436 my $l3 = shift;
437 my $partial = shift;
438  
439 return scalar( $l3->_n2p_over_loopback( 'netaddr', $partial )
440 || $l3->orig_at_netaddr($partial)
441 || $l3->old_at_netaddr($partial)
442 || $l3->_n2p_ipv4_column( 'netaddr', $partial ) );
443 }
444  
445 # Some routers, an Extreme VDX among them, list only their own 127.x entries
446 # in ipNetToMediaTable and keep the ARP cache in ipNetToPhysicalTable. All
447 # three columns then come from ipNetToPhysicalTable, when it has IPv4 rows.
448 sub _n2p_over_loopback {
449 my ( $l3, $column, $partial ) = @_;
450  
451 return unless $l3->_at_loopback_only();
452 my $rows = $l3->_n2p_ipv4_column( $column, $partial ) or return;
453 return %$rows ? $rows : undef;
454 }
455  
456 sub _at_loopback_only {
457 my $l3 = shift;
458 my $older = $l3->orig_at_paddr() || $l3->old_at_paddr();
459 return unless $older and %$older;
460 return !grep { !/(?:^|\.)127\.\d+\.\d+\.\d+$/ } keys %$older;
461 }
462  
463 # ipNetToPhysicalTable is indexed by ifIndex, InetAddressType and an
464 # InetAddress, which Cisco ASA and FTD send without its length octet. Rows
465 # are re-keyed as ifIndex.address to match ipNetToMediaTable, so a partial
466 # fetch filters the whole table rather than walking from an index. The
467 # at_* methods call it in scalar context because subclasses such as ArubaCX
468 # pass their result as an argument, where an empty list would shift the rest.
469 # Its keys match ipNetToMediaTable keys, so it is read only when neither older
470 # table has MAC addresses outside 127.0.0.0/8; otherwise a missing column
471 # would be filled from a different table. It reads the IPv6 neighbor methods'
472 # leaf, so arpnip walks ipNetToPhysicalTable once.
473 sub _n2p_ipv4_column {
474 my ( $l3, $column, $partial ) = @_;
475  
476 return
477 if ( $l3->orig_at_paddr() or $l3->old_at_paddr() )
478 and !$l3->_at_loopback_only();
479 my $paddrs = $l3->ip_n2p_phys_addr() or return;
480  
481 my $return = {};
482 foreach my $row ( keys %$paddrs ) {
483 my ( $ifindex, $addr_type, @octets ) = split /\./, $row;
484 next unless defined $addr_type and $addr_type == 1;
485 shift @octets if scalar @octets == 5 and $octets[0] == 4;
486 next unless scalar @octets == 4;
487  
488 my $address = join( '.', @octets );
489 my $key = "$ifindex.$address";
490 next if defined $partial and $key !~ /^\Q$partial\E(?:\.|$)/;
491  
492 my %value_of = (
493 index => $ifindex,
494 paddr => $paddrs->{$row},
495 netaddr => $address,
496 );
497 $return->{$key} = $value_of{$column};
498 }
499 return $return;
500 }
501  
502 1;
503  
504 __END__