| 1 | # SNMP::Info::IPv6 | ||
| 2 | # | ||
| 3 | # Copyright (c) 2010 Jeroen van Ingen and Carlos Vicente | ||
| 4 | # All rights reserved. | ||
| 5 | # | ||
| 6 | # Redistribution and use in source and binary forms, with or without | ||
| 7 | # modification, are permitted provided that the following conditions are met: | ||
| 8 | # | ||
| 9 | # * Redistributions of source code must retain the above copyright notice, | ||
| 10 | # this list of conditions and the following disclaimer. | ||
| 11 | # * Redistributions in binary form must reproduce the above copyright | ||
| 12 | # notice, this list of conditions and the following disclaimer in the | ||
| 13 | # documentation and/or other materials provided with the distribution. | ||
| 14 | # * Neither the name of the University of California, Santa Cruz nor the | ||
| 15 | # names of its contributors may be used to endorse or promote products | ||
| 16 | # derived from this software without specific prior written permission. | ||
| 17 | # | ||
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 19 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 20 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 21 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
| 22 | # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 23 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 24 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 25 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 26 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 27 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 28 | # POSSIBILITY OF SUCH DAMAGE. | ||
| 29 | |||
| 30 | package SNMP::Info::IPv6; | ||
| 31 | |||
| 32 | use strict; | ||
| 33 | use warnings; | ||
| 34 | use Exporter; | ||
| 35 | use SNMP::Info; | ||
| 36 | |||
| 37 | @SNMP::Info::IPv6::ISA = qw/SNMP::Info Exporter/; | ||
| 38 | @SNMP::Info::IPv6::EXPORT_OK = qw//; | ||
| 39 | |||
| 40 | our ($VERSION, %MIBS, %FUNCS, %GLOBALS, %MUNGE, $METHOD); | ||
| 41 | |||
| 42 | use constant { | ||
| 43 | IPMIB => 1, | ||
| 44 | CISCO => 2, | ||
| 45 | IPV6MIB => 3, | ||
| 46 | }; | ||
| 47 | |||
| 48 | $VERSION = '3.985003'; | ||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | %MIBS = ( | ||
| 53 | 'IP-MIB' => 'ipv6InterfaceTableLastChange', | ||
| 54 | 'IPV6-MIB' => 'ipv6IfTableLastChange', | ||
| 55 | 'CISCO-IETF-IP-MIB' => 'cInetNetToMediaNetAddress', | ||
| 56 | ); | ||
| 57 | |||
| 58 | %GLOBALS = (); | ||
| 59 | |||
| 60 | %FUNCS = ( | ||
| 61 | 'ip_n2p_phys_addr' => 'ipNetToPhysicalPhysAddress', # IP-MIB | ||
| 62 | 'c_inet_phys_addr' => 'cInetNetToMediaPhysAddress', # CISCO-IETF-IP-MIB | ||
| 63 | 'i6_n2p_phys_addr' => 'ipv6NetToMediaPhysAddress', # IPV6-MIB | ||
| 64 | |||
| 65 | 'ip_n2p_phys_type' => 'ipNetToPhysicalType', # IP-MIB | ||
| 66 | 'c_inet_phys_type' => 'cInetNetToMediaType', # CISCO-IETF-IP-MIB | ||
| 67 | 'i6_n2p_phys_type' => 'ipv6NetToMediaType', # IPV6-MIB | ||
| 68 | |||
| 69 | 'ip_n2p_phys_state' => 'ipNetToPhysicalState', # IP-MIB | ||
| 70 | 'c_inet_phys_state' => 'cInetNetToMediaState', # CISCO-IETF-IP-MIB | ||
| 71 | 'i6_n2p_phys_state' => 'ipv6IfNetToMediaState', # IPV6-MIB | ||
| 72 | |||
| 73 | 'ip_pfx_origin' => 'ipAddressPrefixOrigin', # IP-MIB | ||
| 74 | 'c_pfx_origin' => 'cIpAddressPfxOrigin', # CISCO-IETF-IP-MIB | ||
| 75 | |||
| 76 | 'ip_addr6_pfx' => 'ipAddressPrefix', # IP-MIB | ||
| 77 | 'c_addr6_pfx' => 'cIpAddressPrefix', # CISCO-IETF-IP-MIB | ||
| 78 | |||
| 79 | # Commented out are not-accessible according to MIB | ||
| 80 | #'ip_addr6_pfxlen' => 'ipAddressPrefixLength', # IP-MIB | ||
| 81 | #'c_addr6_pfxlen' => 'cIpAddressPfxLength', # CISCO-IETF-IP-MIB | ||
| 82 | 'i6_addr_pfxlen' => 'ipv6AddrPfxLength', # IPV6-MIB | ||
| 83 | 'i6_addr_anycast' => 'ipv6AddrAnycastFlag', # IPV6-MIB | ||
| 84 | |||
| 85 | 'ip_addr6_index' => 'ipAddressIfIndex', # IP-MIB | ||
| 86 | 'c_addr6_index' => 'cIpAddressIfIndex', # CISCO-IETF-IP-MIB | ||
| 87 | |||
| 88 | 'ip_addr6_type' => 'ipAddressType', # IP-MIB | ||
| 89 | 'c_addr6_type' => 'cIpAddressType', # CISCO-IETF-IP-MIB | ||
| 90 | ); | ||
| 91 | |||
| 92 | %MUNGE = ( | ||
| 93 | 'ip_n2p_phys_addr' => \&SNMP::Info::munge_mac, | ||
| 94 | 'c_inet_phys_addr' => \&munge_physaddr, | ||
| 95 | 'i6_n2p_phys_addr' => \&SNMP::Info::munge_mac, | ||
| 96 | ); | ||
| 97 | |||
| 98 | sub parse_inetaddress { | ||
| 99 | my ($info, $inetaddress) = @_; | ||
| 100 | my @parts = split(/\./, $inetaddress); | ||
| 101 | # first part is the InetAddressType | ||
| 102 | my $addrtype = shift @parts; | ||
| 103 | # second part is the number of octets | ||
| 104 | my $addrsize = shift @parts; | ||
| 105 | # InetAddressIPv4 | ||
| 106 | if ($addrtype == 1) { | ||
| 107 | if ($addrsize == 4) { | ||
| 108 | my @ipv4 = @parts[0..3]; | ||
| 109 | return { | ||
| 110 | type => $addrtype, | ||
| 111 | address => join('.', @ipv4), | ||
| 112 | }; | ||
| 113 | } | ||
| 114 | elsif ($info->debug()) { | ||
| 115 | print("invalid number of octets '$addrsize' for InetAddressIPv4 '$inetaddress'\n"); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | # InetAddressIPv6 | ||
| 119 | if ($addrtype == 2) { | ||
| 120 | if ($addrsize == 16) { | ||
| 121 | my @ipv6 = @parts[0..15]; | ||
| 122 | return { | ||
| 123 | type => $addrtype, | ||
| 124 | address => join(':', unpack('(H4)*', pack('C*', @ipv6))), | ||
| 125 | }; | ||
| 126 | } | ||
| 127 | elsif ($info->debug()) { | ||
| 128 | print("invalid number of octets '$addrsize' for InetAddressIPv4 '$inetaddress'\n"); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | # InetAddressIPv4z | ||
| 132 | elsif ($addrtype == 3) { | ||
| 133 | if ($addrsize == 8) { | ||
| 134 | my @ipv4 = @parts[0..3]; | ||
| 135 | my @zone_identifier = @parts[4..7]; | ||
| 136 | return { | ||
| 137 | type => $addrtype, | ||
| 138 | address => join('.', @ipv4), | ||
| 139 | zone_identifier => unpack("L", pack("C*", @zone_identifier)), | ||
| 140 | }; | ||
| 141 | } | ||
| 142 | elsif ($info->debug()) { | ||
| 143 | print("invalid number of octets '$addrsize' for InetAddressIPv4z '$inetaddress'\n"); | ||
| 144 | } | ||
| 145 | } | ||
| 146 | # InetAddressIPv6z | ||
| 147 | elsif ($addrtype == 4) { | ||
| 148 | if ($addrsize == 20) { | ||
| 149 | my @ipv6 = @parts[0..15]; | ||
| 150 | my @zone_identifier = @parts[16..19]; | ||
| 151 | return { | ||
| 152 | type => $addrtype, | ||
| 153 | address => join(':', unpack('(H4)*', pack('C*', @ipv6))), | ||
| 154 | zone_identifier => unpack("L", pack("C*", @zone_identifier)), | ||
| 155 | }; | ||
| 156 | } | ||
| 157 | elsif ($info->debug()) { | ||
| 158 | print("invalid number of octets '$addrsize' for InetAddressIPv6z '$inetaddress'\n"); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | elsif ($info->debug()) { | ||
| 162 | print("unsupported address type '$addrtype'\n"); | ||
| 163 | } | ||
| 164 | |||
| 165 | return; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub ipv6_n2p_mac { | ||
| 169 | my $info = shift; | ||
| 170 | my $return; | ||
| 171 | my $phys_addr = &_test_methods( $info, { | ||
| 172 | ip_n2p_phys_addr => IPMIB, | ||
| 173 | c_inet_phys_addr => CISCO, | ||
| 174 | i6_n2p_phys_addr => IPV6MIB, | ||
| 175 | }); | ||
| 176 | return unless defined $phys_addr; | ||
| 177 | foreach my $row (keys %$phys_addr) { | ||
| 178 | if ($row =~ /^(\d+)\.(\d+)\.(\d+)\.([\d\.]+)$/) { | ||
| 179 | my $ifindex = $1; my $addrtype = $2; my $addrsize = $3; my $v6addr = $4; | ||
| 180 | if ($info::METHOD == IPV6MIB) { | ||
| 181 | # IPV6-MIB doesn't include the addrtype in the index; | ||
| 182 | # also, address syntax is IPv6Address (fixed 16 bytes) and not InetAddress (length field followed by address bytes) | ||
| 183 | $v6addr = join('.', $addrtype, $addrsize, $v6addr); | ||
| 184 | $addrtype = 2; | ||
| 185 | } | ||
| 186 | if (($addrtype == 2) && (defined $phys_addr->{$row})) { # IPv6 | ||
| 187 | $return->{$row} = substr($phys_addr->{$row}, 0, 17); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | } | ||
| 191 | printf("%s: data comes from %s.\n", &_my_sub_name, $info->_method_used() ) if $info->debug(); | ||
| 192 | return $return; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub ipv6_n2p_addr { | ||
| 196 | my $info = shift; | ||
| 197 | my $return; | ||
| 198 | my $net_addr = &_test_methods( $info, { | ||
| 199 | ip_n2p_phys_addr => IPMIB, | ||
| 200 | c_inet_phys_addr => CISCO, | ||
| 201 | i6_n2p_phys_addr => IPV6MIB, | ||
| 202 | }); | ||
| 203 | return unless defined $net_addr; | ||
| 204 | foreach my $row (keys %$net_addr) { | ||
| 205 | if ($row =~ /^(\d+)\.(\d+)\.(\d+)\.([\d\.]+)$/) { | ||
| 206 | my $ifindex = $1; my $addrtype = $2; my $addrsize = $3; my $v6addr = $4; | ||
| 207 | if ($info::METHOD == IPV6MIB) { | ||
| 208 | # IPV6-MIB doesn't include the addrtype in the index; | ||
| 209 | # also, address syntax is IPv6Address (fixed 16 bytes) and not InetAddress (length field followed by address bytes) | ||
| 210 | $v6addr = join('.', $addrtype, $addrsize, $v6addr); | ||
| 211 | $addrtype = 2; | ||
| 212 | } | ||
| 213 | if ($addrtype == 2) { # IPv6 | ||
| 214 | my $v6_packed = pack("C*", split(/\./, $v6addr)); | ||
| 215 | if (length($v6_packed) == 15) { | ||
| 216 | # Workaround for some some IP-MIB implementations, eg on Cisco Nexus: no explicit addrsize, | ||
| 217 | # so what we've collected in that variable is actually the first byte of the address. | ||
| 218 | $v6_packed = pack('C', $addrsize) . $v6_packed; | ||
| 219 | } | ||
| 220 | if (length($v6_packed) == 17) { | ||
| 221 | # Workaround for IPV6-MIB on Windows 2012: if the address is one byte too long, the SNMP agent probably has an incorrect | ||
| 222 | # implementation where a length field precedes the actual IPv6 address. | ||
| 223 | # In that case, the first character should be chr(16), ie 0x10; strip it if that's the case. | ||
| 224 | $v6_packed =~ s/^\x10//; | ||
| 225 | } | ||
| 226 | if (length($v6_packed) == 16) { | ||
| 227 | $v6addr = join(':', map { sprintf("%04x", $_) } unpack("n*", $v6_packed) ); | ||
| 228 | $return->{$row} = $v6addr; | ||
| 229 | } else { | ||
| 230 | printf("Invalid size for IPv6 address: expected 16 bytes, got %d (%s = %s)\n", length($v6_packed), $row, $net_addr->{$row}); | ||
| 231 | } | ||
| 232 | } | ||
| 233 | } | ||
| 234 | } | ||
| 235 | printf("%s: data comes from %s.\n", &_my_sub_name, $info->_method_used() ) if $info->debug(); | ||
| 236 | return $return; | ||
| 237 | } | ||
| 238 | |||
| 239 | sub ipv6_n2p_if { | ||
| 240 | my $info = shift; | ||
| 241 | my $return; | ||
| 242 | my $phys_addr = &_test_methods( $info, { | ||
| 243 | ip_n2p_phys_addr => IPMIB, | ||
| 244 | c_inet_phys_addr => CISCO, | ||
| 245 | i6_n2p_phys_addr => IPV6MIB, | ||
| 246 | }); | ||
| 247 | return unless defined $phys_addr; | ||
| 248 | foreach my $row (keys %$phys_addr) { | ||
| 249 | if ($row =~ /^(\d+)\.(\d+)\.(\d+)\.([\d\.]+)$/) { | ||
| 250 | my $ifindex = $1; my $addrtype = $2; my $addrsize = $3; my $v6addr = $4; | ||
| 251 | if ($info::METHOD == IPV6MIB) { | ||
| 252 | # IPV6-MIB doesn't include the addrtype in the index; | ||
| 253 | # also, address syntax is IPv6Address (fixed 16 bytes) and not InetAddress (length field followed by address bytes) | ||
| 254 | $v6addr = join('.', $addrtype, $addrsize, $v6addr); | ||
| 255 | $addrtype = 2; | ||
| 256 | } | ||
| 257 | if ($addrtype == 2) { # IPv6 | ||
| 258 | $return->{$row} = $ifindex; | ||
| 259 | } | ||
| 260 | } | ||
| 261 | } | ||
| 262 | printf("%s: data comes from %s.\n", &_my_sub_name, $info->_method_used() ) if $info->debug(); | ||
| 263 | return $return; | ||
| 264 | } | ||
| 265 | |||
| 266 | sub ipv6_n2p_type { | ||
| 267 | my $info = shift; | ||
| 268 | my $return; | ||
| 269 | my $phys_type = &_test_methods( $info, { | ||
| 270 | ip_n2p_phys_type => IPMIB, | ||
| 271 | c_inet_phys_type => CISCO, | ||
| 272 | i6_n2p_phys_type => IPV6MIB, | ||
| 273 | }); | ||
| 274 | return unless defined $phys_type; | ||
| 275 | foreach my $row (keys %$phys_type) { | ||
| 276 | if ($row =~ /^(\d+)\.(\d+)\.(\d+)\.([\d\.]+)$/) { | ||
| 277 | my $ifindex = $1; my $addrtype = $2; my $addrsize = $3; my $v6addr = $4; | ||
| 278 | if ($info::METHOD == IPV6MIB) { | ||
| 279 | # IPV6-MIB doesn't include the addrtype in the index; | ||
| 280 | # also, address syntax is IPv6Address (fixed 16 bytes) and not InetAddress (length field followed by address bytes) | ||
| 281 | $v6addr = join('.', $addrtype, $addrsize, $v6addr); | ||
| 282 | $addrtype = 2; | ||
| 283 | } | ||
| 284 | if ($addrtype == 2) { # IPv6 | ||
| 285 | $return->{$row} = $phys_type->{$row}; | ||
| 286 | } | ||
| 287 | } | ||
| 288 | } | ||
| 289 | printf("%s: data comes from %s.\n", &_my_sub_name, $info->_method_used() ) if $info->debug(); | ||
| 290 | return $return; | ||
| 291 | } | ||
| 292 | |||
| 293 | sub ipv6_n2p_state { | ||
| 294 | my $info = shift; | ||
| 295 | my $return; | ||
| 296 | my $phys_state = &_test_methods( $info, { | ||
| 297 | ip_n2p_phys_state => IPMIB, | ||
| 298 | c_inet_phys_state => CISCO, | ||
| 299 | i6_n2p_phys_state => IPV6MIB, | ||
| 300 | }); | ||
| 301 | return unless defined $phys_state; | ||
| 302 | foreach my $row (keys %$phys_state) { | ||
| 303 | if ($row =~ /^(\d+)\.(\d+)\.(\d+)\.([\d\.]+)$/) { | ||
| 304 | my $ifindex = $1; my $addrtype = $2; my $addrsize = $3; my $v6addr = $4; | ||
| 305 | if ($info::METHOD == IPV6MIB) { | ||
| 306 | # IPV6-MIB doesn't include the addrtype in the index; | ||
| 307 | # also, address syntax is IPv6Address (fixed 16 bytes) and not InetAddress (length field followed by address bytes) | ||
| 308 | $v6addr = join('.', $addrtype, $addrsize, $v6addr); | ||
| 309 | $addrtype = 2; | ||
| 310 | } | ||
| 311 | if ($addrtype == 2) { # IPv6 | ||
| 312 | $return->{$row} = $phys_state->{$row}; | ||
| 313 | } | ||
| 314 | } | ||
| 315 | } | ||
| 316 | printf("%s: data comes from %s.\n", &_my_sub_name, $info->_method_used() ) if $info->debug(); | ||
| 317 | return $return; | ||
| 318 | } | ||
| 319 | |||
| 320 | sub ipv6_index { | ||
| 321 | my $info = shift; | ||
| 322 | my $return; | ||
| 323 | my $ipv6_index = &_test_methods( $info, { | ||
| 324 | ip_addr6_index => IPMIB, | ||
| 325 | c_addr6_index => CISCO, | ||
| 326 | }); | ||
| 327 | return $info->_i6_addr_column('ifindex') unless scalar keys %$ipv6_index; | ||
| 328 | foreach my $row (keys %$ipv6_index) { | ||
| 329 | my $parsed_address = $info->parse_inetaddress($row); | ||
| 330 | next | ||
| 331 | unless defined $parsed_address; | ||
| 332 | # only IPv6 and IPv6z (with zone identifier) | ||
| 333 | next | ||
| 334 | unless $parsed_address->{type} == 2 || $parsed_address->{type} == 4; | ||
| 335 | |||
| 336 | $return->{$row} = $ipv6_index->{$row}; | ||
| 337 | } | ||
| 338 | printf("%s: data comes from %s.\n", &_my_sub_name, $info->_method_used() ) if $info->debug(); | ||
| 339 | return $return; | ||
| 340 | } | ||
| 341 | |||
| 342 | sub ipv6_type { | ||
| 343 | my $info = shift; | ||
| 344 | my $return; | ||
| 345 | my $ipv6_type = &_test_methods( $info, { | ||
| 346 | ip_addr6_type => IPMIB, | ||
| 347 | c_addr6_type => CISCO, | ||
| 348 | }); | ||
| 349 | return $info->_i6_addr_column('type') unless scalar keys %$ipv6_type; | ||
| 350 | foreach my $row (keys %$ipv6_type){ | ||
| 351 | my $parsed_address = $info->parse_inetaddress($row); | ||
| 352 | next | ||
| 353 | unless defined $parsed_address; | ||
| 354 | # only IPv6 and IPv6z (with zone identifier) | ||
| 355 | next | ||
| 356 | unless $parsed_address->{type} == 2 || $parsed_address->{type} == 4; | ||
| 357 | |||
| 358 | $return->{$row} = $ipv6_type->{$row}; | ||
| 359 | } | ||
| 360 | printf("%s: data comes from %s.\n", &_my_sub_name, $info->_method_used() ) if $info->debug(); | ||
| 361 | return $return; | ||
| 362 | } | ||
| 363 | |||
| 364 | sub ipv6_pfx_origin { | ||
| 365 | my $info = shift; | ||
| 366 | my $return; | ||
| 367 | my $ipv6_pfx_origin = &_test_methods( $info, { | ||
| 368 | ip_pfx_origin => IPMIB, | ||
| 369 | c_pfx_origin => CISCO, | ||
| 370 | }); | ||
| 371 | return unless defined $ipv6_pfx_origin; | ||
| 372 | foreach my $row (keys %$ipv6_pfx_origin) { | ||
| 373 | if ($row =~ /^(\d+)\.(\d+)\.([\d\.]+)\.(\d+)$/) { | ||
| 374 | my $ifindex = $1; my $type = $2; my $pfx = $3; my $len = $4; | ||
| 375 | # only IPv6 and IPv6z (with zone identifier) | ||
| 376 | if ($type == 2 || $type == 4) { | ||
| 377 | $return->{$row} = $ipv6_pfx_origin->{$row}; | ||
| 378 | } | ||
| 379 | } | ||
| 380 | } | ||
| 381 | printf("%s: data comes from %s.\n", &_my_sub_name, $info->_method_used() ) if $info->debug(); | ||
| 382 | return $return; | ||
| 383 | } | ||
| 384 | |||
| 385 | sub ipv6_addr_prefix { | ||
| 386 | my $info = shift; | ||
| 387 | my $return; | ||
| 388 | my $ipv6_addr_prefix = &_test_methods( $info, { | ||
| 389 | ip_addr6_pfx => IPMIB, | ||
| 390 | c_addr6_pfx => CISCO, | ||
| 391 | }); | ||
| 392 | return unless defined $ipv6_addr_prefix; | ||
| 393 | foreach my $row (keys %$ipv6_addr_prefix) { | ||
| 394 | my $parsed_address = $info->parse_inetaddress($row); | ||
| 395 | next | ||
| 396 | unless defined $parsed_address; | ||
| 397 | # only IPv6 and IPv6z (with zone identifier) | ||
| 398 | next | ||
| 399 | unless $parsed_address->{type} == 2 || $parsed_address->{type} == 4; | ||
| 400 | |||
| 401 | # Remove interface specific part from vrf interfaces | ||
| 402 | if ($parsed_address->{type} == 4 && $row =~ /^((\d+\.){17}\d+)/) { $row = $1 } | ||
| 403 | # Remove the OID part from the value | ||
| 404 | my $val = $ipv6_addr_prefix->{$row}; | ||
| 405 | if ( $val =~ /^.+?((?:\d+\.){19}\d+)$/ ){ | ||
| 406 | $val = $1; | ||
| 407 | $return->{$row} = $val; | ||
| 408 | } | ||
| 409 | } | ||
| 410 | printf("%s: data comes from %s.\n", &_my_sub_name, $info->_method_used() ) if $info->debug(); | ||
| 411 | return $return; | ||
| 412 | } | ||
| 413 | |||
| 414 | sub ipv6_addr_prefixlength { | ||
| 415 | my $info = shift; | ||
| 416 | my $return; | ||
| 417 | my $ipv6_addr_prefix = &_test_methods( $info, { | ||
| 418 | ip_addr6_pfx => IPMIB, | ||
| 419 | c_addr6_pfx => CISCO, | ||
| 420 | }); | ||
| 421 | return $info->_i6_addr_column('pfxlen') | ||
| 422 | unless scalar keys %$ipv6_addr_prefix; | ||
| 423 | foreach my $row (keys %$ipv6_addr_prefix) { | ||
| 424 | if ($row =~ /^(\d+)\.[\d\.]+$/) { | ||
| 425 | my $type = $1; | ||
| 426 | if ($type == 2 || $type == 4) { # IPv6 or IPv6z (with zone identifier) | ||
| 427 | # Remove interface specific part from vrf interfaces | ||
| 428 | if ($row =~ /^((\d+\.){17}\d+)/) { $row = $1 } | ||
| 429 | # Remove the OID part from the value | ||
| 430 | my $val = $ipv6_addr_prefix->{$row} || ''; | ||
| 431 | if ( $val =~ /^.+?((?:\d+\.){19}(\d+))$/ ) { | ||
| 432 | $val = $2; | ||
| 433 | $return->{$row} = $val; | ||
| 434 | } | ||
| 435 | } | ||
| 436 | } | ||
| 437 | } | ||
| 438 | printf("%s: data comes from %s.\n", &_my_sub_name, $info->_method_used() ) if $info->debug(); | ||
| 439 | return $return; | ||
| 440 | } | ||
| 441 | |||
| 442 | sub ipv6_addr { | ||
| 443 | my $info = shift; | ||
| 444 | my $return = {}; | ||
| 445 | my $indexes = $info->ipv6_index(); | ||
| 446 | foreach my $row (keys %$indexes) { | ||
| 447 | my $parsed_address = $info->parse_inetaddress($row); | ||
| 448 | next | ||
| 449 | unless defined $parsed_address; | ||
| 450 | # only IPv6 and IPv6z (with zone identifier) | ||
| 451 | next | ||
| 452 | unless $parsed_address->{type} == 2 || $parsed_address->{type} == 4; | ||
| 453 | |||
| 454 | $return->{$row} = $parsed_address->{address}; | ||
| 455 | } | ||
| 456 | return $return; | ||
| 457 | } | ||
| 458 | |||
| 459 | # IPV6-MIB ipv6AddrTable is indexed by ifIndex and a 16 octet address. | ||
| 460 | # Rows are re-keyed as InetAddress (type.length.address) so ipv6_addr() can | ||
| 461 | # parse them; link-local addresses repeat across interfaces, so they become | ||
| 462 | # IPv6z with the ifIndex as zone index. The re-keyed rows match IP-MIB keys, | ||
| 463 | # so IPV6-MIB is read only when neither address table has rows; otherwise a | ||
| 464 | # missing IP-MIB column would be filled from a different MIB. | ||
| 465 | sub _i6_addr_column { | ||
| 466 | my ( $info, $column ) = @_; | ||
| 467 | |||
| 468 | foreach my $index_method (qw/ip_addr6_index c_addr6_index/) { | ||
| 469 | my $index = $info->$index_method() || {}; | ||
| 470 | return if scalar keys %$index; | ||
| 471 | } | ||
| 472 | |||
| 473 | my $pfxlens = $info->i6_addr_pfxlen() || {}; | ||
| 474 | return unless scalar keys %$pfxlens; | ||
| 475 | my $anycast = $info->i6_addr_anycast() || {}; | ||
| 476 | $info::METHOD = IPV6MIB; | ||
| 477 | |||
| 478 | my $return = {}; | ||
| 479 | foreach my $row ( keys %$pfxlens ) { | ||
| 480 | my ( $ifindex, @octets ) = split /\./, $row; | ||
| 481 | next unless scalar @octets == 16; | ||
| 482 | my $link_local = $octets[0] == 0xfe && ( $octets[1] & 0xc0 ) == 0x80; | ||
| 483 | my @zone = unpack( 'C4', pack( 'N', $ifindex ) ); | ||
| 484 | my $key = $link_local | ||
| 485 | ? join( '.', 4, 20, @octets, @zone ) | ||
| 486 | : join( '.', 2, 16, @octets ); | ||
| 487 | my %value_of = ( | ||
| 488 | ifindex => $ifindex, | ||
| 489 | pfxlen => $pfxlens->{$row}, | ||
| 490 | type => ( $anycast->{$row} || '' ) eq 'true' | ||
| 491 | ? 'anycast' : 'unicast', | ||
| 492 | ); | ||
| 493 | $return->{$key} = $value_of{$column}; | ||
| 494 | } | ||
| 495 | return $return; | ||
| 496 | } | ||
| 497 | |||
| 498 | sub _method_used { | ||
| 499 | my $info = shift; | ||
| 500 | my $return = 'none of the MIBs'; | ||
| 501 | # FIXME ugh! a global. makes order of calls important for debug. | ||
| 502 | if (defined $info::METHOD) { | ||
| 503 | if ($info::METHOD eq IPMIB) { | ||
| 504 | $return = 'IP-MIB'; | ||
| 505 | } elsif ($info::METHOD eq IPV6MIB) { | ||
| 506 | $return = 'IPV6-MIB'; | ||
| 507 | } elsif ($info::METHOD eq CISCO) { | ||
| 508 | $return = 'CISCO-IETF-IP-MIB'; | ||
| 509 | } | ||
| 510 | } | ||
| 511 | return $return; | ||
| 512 | } | ||
| 513 | |||
| 514 | sub _test_methods { | ||
| 515 | my $info = shift; | ||
| 516 | my $test = shift; | ||
| 517 | my $return = {}; | ||
| 518 | foreach my $method (sort {$test->{$a} <=> $test->{$b}} keys %$test) { | ||
| 519 | $return = $info->$method || {}; | ||
| 520 | if (scalar keys %$return) { | ||
| 521 | # FIXME ugh! a global. makes order of calls important for debug. | ||
| 522 | $info::METHOD = $test->{$method}; | ||
| 523 | last; | ||
| 524 | } | ||
| 525 | } | ||
| 526 | return $return; | ||
| 527 | } | ||
| 528 | |||
| 529 | sub _my_sub_name { | ||
| 530 | my @callinfo = caller(1); | ||
| 531 | return $callinfo[3]; | ||
| 532 | } | ||
| 533 | |||
| 534 | sub munge_physaddr { | ||
| 535 | my $addr = shift; | ||
| 536 | return unless defined $addr; | ||
| 537 | return unless length $addr; | ||
| 538 | $addr = join( ':', map { sprintf "%02x", $_ } unpack( 'C*', $addr ) ); | ||
| 539 | return $addr; | ||
| 540 | } | ||
| 541 | |||
| 542 | 1; | ||
| 543 | |||
| 544 | __END__ |