This file is indexed.

/usr/share/perl5/SNMP/Info/IPv6.pm is in libsnmp-info-perl 3.39-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# SNMP::Info::IPv6
#
# Copyright (c) 2010 Jeroen van Ingen and Carlos Vicente
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the University of California, Santa Cruz nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

package SNMP::Info::IPv6;

use strict;
use Exporter;
use SNMP::Info;

@SNMP::Info::IPv6::ISA       = qw/SNMP::Info Exporter/;
@SNMP::Info::IPv6::EXPORT_OK = qw//;

use vars qw/$VERSION %MIBS %FUNCS %GLOBALS %MUNGE $METHOD/;

use constant {
    IPMIB   => 1,
    CISCO   => 2,
    IPV6MIB => 3,
};

$VERSION = '3.39';



%MIBS = ( 
    'IP-MIB'            => 'ipv6InterfaceTableLastChange',
    'IPV6-MIB'          => 'ipv6IfTableLastChange',
    'CISCO-IETF-IP-MIB' => 'cInetNetToMediaNetAddress', 
);

%GLOBALS = ();

%FUNCS = ( 
    'ip_n2p_phys_addr'  => 'ipNetToPhysicalPhysAddress',    # IP-MIB
    'c_inet_phys_addr'  => 'cInetNetToMediaPhysAddress',    # CISCO-IETF-IP-MIB
    'i6_n2p_phys_addr'  => 'ipv6NetToMediaPhysAddress',     # IPV6-MIB

    'ip_n2p_phys_type'  => 'ipNetToPhysicalType',           # IP-MIB
    'c_inet_phys_type'  => 'cInetNetToMediaType',           # CISCO-IETF-IP-MIB
    'i6_n2p_phys_type'  => 'ipv6NetToMediaType',            # IPV6-MIB

    'ip_n2p_phys_state' => 'ipNetToPhysicalState',          # IP-MIB
    'c_inet_phys_state' => 'cInetNetToMediaState',          # CISCO-IETF-IP-MIB
    'i6_n2p_phys_state' => 'ipv6NetToMediaState',           # IPV6-MIB

    'ip_pfx_origin'     => 'ipAddressPrefixOrigin',         # IP-MIB
    'c_pfx_origin'      => 'cIpAddressPfxOrigin',           # CISCO-IETF-IP-MIB 

    'ip_addr6_pfx'      => 'ipAddressPrefix',               # IP-MIB 
    'c_addr6_pfx'       => 'cIpAddressPrefix',              # CISCO-IETF-IP-MIB 

    'ip_addr6_index'    => 'ipAddressIfIndex',              # IP-MIB
    'c_addr6_index'     => 'cIpAddressIfIndex',             # CISCO-IETF-IP-MIB 

    'ip_addr6_type'     => 'ipAddressType',                 # IP-MIB
    'c_addr6_type'      => 'cIpAddressType',                # CISCO-IETF-IP-MIB
    
);

%MUNGE = (
    'ip_n2p_phys_addr'  => \&SNMP::Info::munge_mac,
    'c_inet_phys_addr'  => \&munge_physaddr,
    'i6_n2p_phys_addr'  => \&SNMP::Info::munge_mac,
);


sub ipv6_n2p_mac {
    my $info = shift;
    my $return;
    my $phys_addr = &_test_methods( $info, {
        ip_n2p_phys_addr => IPMIB,
        c_inet_phys_addr => CISCO,
        i6_n2p_phys_addr => IPV6MIB,
    });
    return unless defined $phys_addr;
    foreach my $row (keys %$phys_addr) {
        if ($row =~ /^(\d+)\.(\d+)\.(\d+)\.([\d\.]+)$/) {
            my $ifindex = $1; my $addrtype = $2; my $addrsize = $3; my $v6addr = $4;
            if ($info::METHOD == IPV6MIB) { 
                # IPV6-MIB doesn't include the addrtype in the index; 
                # also, address syntax is IPv6Address (fixed 16 bytes) and not InetAddress (length field followed by address bytes)
                $v6addr = join('.', $addrtype, $addrsize, $v6addr);
                $addrtype = 2;
            }
            if (($addrtype == 2) && (defined $phys_addr->{$row})) { # IPv6
                $return->{$row} = substr($phys_addr->{$row}, 0, 17);
            }
        }
    }
    printf("%s: data comes from %s.\n", &_my_sub_name, $info->_method_used() ) if $info->debug();
    return $return;
}

sub ipv6_n2p_addr {
    my $info = shift;
    my $return;
    my $net_addr = &_test_methods( $info, {
        ip_n2p_phys_addr => IPMIB,
        c_inet_phys_addr => CISCO,
        i6_n2p_phys_addr => IPV6MIB,
    });
    return unless defined $net_addr;
    foreach my $row (keys %$net_addr) {
        if ($row =~ /^(\d+)\.(\d+)\.(\d+)\.([\d\.]+)$/) {
            my $ifindex = $1; my $addrtype = $2; my $addrsize = $3; my $v6addr = $4;
            if ($info::METHOD == IPV6MIB) { 
                # IPV6-MIB doesn't include the addrtype in the index; 
                # also, address syntax is IPv6Address (fixed 16 bytes) and not InetAddress (length field followed by address bytes)
                $v6addr = join('.', $addrtype, $addrsize, $v6addr);
                $addrtype = 2;
            }
            if ($addrtype == 2) { # IPv6
                my $v6_packed = pack("C*", split(/\./, $v6addr));
                if (length($v6_packed) == 15) {
                    # Workaround for some some IP-MIB implementations, eg on Cisco Nexus: no explicit addrsize, 
                    # so what we've collected in that variable is actually the first byte of the address.
                    $v6_packed = pack('C', $addrsize) . $v6_packed;
                }
                if (length($v6_packed) == 17) {
                    # Workaround for IPV6-MIB on Windows 2012: if the address is one byte too long, the SNMP agent probably has an incorrect
                    # implementation where a length field precedes the actual IPv6 address.
                    # In that case, the first character should be chr(16), ie 0x10; strip it if that's the case.
                    $v6_packed =~ s/^\x10//;
                }
                if (length($v6_packed) == 16) {
                    $v6addr = join(':', map { sprintf("%04x", $_) } unpack("n*", $v6_packed) );
                    $return->{$row} = $v6addr;
                } else {
                    printf("Invalid size for IPv6 address: expected 16 bytes, got %d (%s = %s)\n", length($v6_packed), $row, $net_addr->{$row});
                }
            }
        }
    }
    printf("%s: data comes from %s.\n", &_my_sub_name, $info->_method_used() ) if $info->debug();
    return $return;
}

sub ipv6_n2p_if {
    my $info = shift;
    my $return;
    my $phys_addr = &_test_methods( $info, {
        ip_n2p_phys_addr => IPMIB,
        c_inet_phys_addr => CISCO,
        i6_n2p_phys_addr => IPV6MIB,
    });
    return unless defined $phys_addr;
    foreach my $row (keys %$phys_addr) {
        if ($row =~ /^(\d+)\.(\d+)\.(\d+)\.([\d\.]+)$/) {
            my $ifindex = $1; my $addrtype = $2; my $addrsize = $3; my $v6addr = $4;
            if ($info::METHOD == IPV6MIB) { 
                # IPV6-MIB doesn't include the addrtype in the index; 
                # also, address syntax is IPv6Address (fixed 16 bytes) and not InetAddress (length field followed by address bytes)
                $v6addr = join('.', $addrtype, $addrsize, $v6addr);
                $addrtype = 2;
            }
            if ($addrtype == 2) { # IPv6
                $return->{$row} = $ifindex;
            }
        }
    }
    printf("%s: data comes from %s.\n", &_my_sub_name, $info->_method_used() ) if $info->debug();
    return $return;
}

sub ipv6_n2p_type {
    my $info = shift;
    my $return;
    my $phys_type = &_test_methods( $info, {
        ip_n2p_phys_type => IPMIB,
        c_inet_phys_type => CISCO,
        i6_n2p_phys_type => IPV6MIB,
    });
    return unless defined $phys_type;
    foreach my $row (keys %$phys_type) {
        if ($row =~ /^(\d+)\.(\d+)\.(\d+)\.([\d\.]+)$/) {
            my $ifindex = $1; my $addrtype = $2; my $addrsize = $3; my $v6addr = $4;
            if ($info::METHOD == IPV6MIB) { 
                # IPV6-MIB doesn't include the addrtype in the index; 
                # also, address syntax is IPv6Address (fixed 16 bytes) and not InetAddress (length field followed by address bytes)
                $v6addr = join('.', $addrtype, $addrsize, $v6addr);
                $addrtype = 2;
            }
            if ($addrtype == 2) { # IPv6
                $return->{$row} = $phys_type->{$row};
            }
        }
    }
    printf("%s: data comes from %s.\n", &_my_sub_name, $info->_method_used() ) if $info->debug();
    return $return;
}

sub ipv6_n2p_state {
    my $info = shift;
    my $return;
    my $phys_state = &_test_methods( $info, {
        ip_n2p_phys_state => IPMIB,
        c_inet_phys_state => CISCO,
        i6_n2p_phys_state => IPV6MIB,
    });
    return unless defined $phys_state;
    foreach my $row (keys %$phys_state) {
        if ($row =~ /^(\d+)\.(\d+)\.(\d+)\.([\d\.]+)$/) {
            my $ifindex = $1; my $addrtype = $2; my $addrsize = $3; my $v6addr = $4;
            if ($info::METHOD == IPV6MIB) { 
                # IPV6-MIB doesn't include the addrtype in the index; 
                # also, address syntax is IPv6Address (fixed 16 bytes) and not InetAddress (length field followed by address bytes)
                $v6addr = join('.', $addrtype, $addrsize, $v6addr);
                $addrtype = 2;
            }
            if ($addrtype == 2) { # IPv6
                $return->{$row} = $phys_state->{$row};
            }
        }
    }
    printf("%s: data comes from %s.\n", &_my_sub_name, $info->_method_used() ) if $info->debug();
    return $return;
}

sub ipv6_index {
    my $info = shift;
    my $return;
    my $ipv6_index = &_test_methods( $info, {
	ip_addr6_index  => IPMIB,
	c_addr6_index   => CISCO,
				    });
    return unless defined $ipv6_index;
    foreach my $row (keys %$ipv6_index){
        if ($row =~ /^(\d+)\.([\d\.]+)$/) {
            my $addrtype = $1; my $v6addr = $2;
            if ($addrtype == 2) { # IPv6
		$return->{$row} = $ipv6_index->{$row};
	    }
	}
    }
    printf("%s: data comes from %s.\n", &_my_sub_name, $info->_method_used() ) if $info->debug();
    return $return;
}

sub ipv6_type {
    my $info = shift;
    my $return;
    my $ipv6_type = &_test_methods( $info, {
	ip_addr6_type  => IPMIB,
	c_addr6_type   => CISCO,
				    });
    return unless defined $ipv6_type;
    foreach my $row (keys %$ipv6_type){
        if ($row =~ /^(\d+)\.([\d\.]+)$/) {
            my $addrtype = $1; my $v6addr = $2;
            if ($addrtype == 2) { # IPv6
		$return->{$row} = $ipv6_type->{$row};
	    }
	}
    }
    printf("%s: data comes from %s.\n", &_my_sub_name, $info->_method_used() ) if $info->debug();
    return $return;
}

sub ipv6_pfx_origin {
    my $info = shift;
    my $return;
    my $ipv6_pfx_origin = &_test_methods( $info, {
	ip_pfx_origin  => IPMIB,
	c_pfx_origin   => CISCO,
				    });
    return unless defined $ipv6_pfx_origin;
    foreach my $row (keys %$ipv6_pfx_origin){
        if ($row =~ /^(\d+)\.(\d+)\.([\d\.]+)\.(\d+)$/) {
            my $ifindex = $1; my $type = $2; my $pfx = $3; my $len = $4;
            if ($type == 2) { # IPv6
		$return->{$row} = $ipv6_pfx_origin->{$row};
	    }
	}
    }
    printf("%s: data comes from %s.\n", &_my_sub_name, $info->_method_used() ) if $info->debug();
    return $return;
}

sub ipv6_addr_prefix {
    my $info = shift;
    my $return;
    my $ipv6_addr_prefix = &_test_methods( $info, {
	ip_addr6_pfx  => IPMIB,
	c_addr6_pfx   => CISCO,
				    });
    return unless defined $ipv6_addr_prefix;
    foreach my $row (keys %$ipv6_addr_prefix){
        if ($row =~ /^(\d+)\.[\d\.]+$/) {
            my $type = $1;
	    if (($type == 2) or ($type == 4)) { # IPv6
		# Remove interface specific part from vrf interfaces
		if ($row =~ /^((\d+\.){17}\d+)/) { $row = $1 }
		# Remove the OID part from the value
		my $val = $ipv6_addr_prefix->{$row};
		if ( $val =~ /^.+?((?:\d+\.){19}\d+)$/ ){
		    $val = $1;
		    $return->{$row} = $val;
		}
	    }
	}
    }
    printf("%s: data comes from %s.\n", &_my_sub_name, $info->_method_used() ) if $info->debug();
    return $return;
}

sub ipv6_addr {
    my $info = shift;
    my $return;
    my $indexes = $info->ipv6_index();
    foreach my $row (keys %$indexes) {
        my @parts = split(/\./, $row);
        my $is_valid = 0;
        if (scalar @parts == 18) {
            my $addrtype = shift @parts;
            $is_valid = 1;
        } elsif (scalar @parts == 17) {
            $is_valid = 1;
        }
        my $addrsize = shift @parts; # First element now is addrsize, should be 16
        if ($is_valid && $addrsize == 16) {
            $return->{$row} = join(':', unpack('(H4)*', pack('C*', @parts)));
        } else {
            warn sprintf("%s: unable to decode table index to IPv6 address. Raw data is [%s].\n", &_my_sub_name, $row);
        }
    }
    return $return;
}

sub _method_used {
    my $info = shift;
    my $return = 'none of the MIBs';
    if (defined $info::METHOD) {
        if ($info::METHOD eq IPMIB) {
            $return = 'IP-MIB';
        } elsif ($info::METHOD eq IPV6MIB) {
            $return = 'IPV6-MIB';
        } elsif ($info::METHOD eq CISCO) {
            $return = 'CISCO-IETF-IP-MIB';
        }
    }
    return $return;
}

sub _test_methods {
    my $info = shift;
    my $test = shift;
    my $return = {};
    foreach my $method (sort {$test->{$a} <=> $test->{$b}} keys %$test) {
        $return = $info->$method || {};
        if (scalar keys %$return) {
            $info::METHOD = $test->{$method};
            last;
        }
    }
    return $return;
}

sub _my_sub_name {
    my @callinfo = caller(1);
    return $callinfo[3];
}

sub munge_physaddr {
    my $addr = shift;
    return unless defined $addr;
    return unless length $addr;
    $addr = join( ':', map { sprintf "%02x", $_ } unpack( 'C*', $addr ) );
    return $addr;
}

1;

__END__

=head1 NAME

SNMP::Info::IPv6 - SNMP Interface for obtaining IPv6 addresses and IPv6
address mappings

=head1 AUTHOR

Jeroen van Ingen and Carlos Vicente

=head1 SYNOPSIS

 # Let SNMP::Info determine the correct subclass for you. 
 my $info = new SNMP::Info(
                          AutoSpecify => 1,
                          Debug       => 1,
                          DestHost    => 'myswitch',
                          Community   => 'public',
                          Version     => 2
                        ) 
    or die "Can't connect to DestHost.\n";

 my $class      = $info->class();
 print "SNMP::Info determined this device to fall under subclass : $class\n";

=head1 DESCRIPTION

The SNMP::Info::IPv6 class implements functions to for mapping IPv6 addresses 
to MAC addresses, interfaces and more. It will use data from the F<IP-MIB>,
F<IPV6-MIB>, or the F<CISCO-IETF-IP-MIB>, whichever is supported by the
device.

This class is inherited by Info::Layer3 to provide IPv6 node tracking across  
device classes.

For debugging purposes you can call this class directly as you would
SNMP::Info

 my $info = new SNMP::Info::IPv6 (...);

=head2 Inherited Classes

none.

=head2 Required MIBs

=over

=item F<IP-MIB>
=item F<IPV6-MIB>
=item F<CISCO-IETF-IP-MIB>

=back

=head1 GLOBALS

none.

=head1 TABLE METHODS

These are methods that return tables of information in the form of a reference
to a hash.

=head2  Internet Address Table

=over

=item $info->ipv6_n2p_addr()

=item $info->ipv6_n2p_if()

=item $info->ipv6_n2p_mac()

=item $info->ipv6_n2p_state()

=item $info->ipv6_n2p_type()

=item $info->ipv6_index()

Maps an IPv6 address to an interface C<ifIndex>

=item $info->ipv6_type()

Maps an IPv6 address to its type (unicast, anycast, etc.)

=item $info->ipv6_pfx_origin()

Maps an IPv6 prefix with its origin (manual, well-known, dhcp, etc.)

=item $info->ipv6_addr_prefix() 

Maps IPv6 addresses with their prefixes

=item $info->ipv6_addr()

Maps a table instance to an IPv6 address

=back

=head2  Internet Address Translation Table

=over

=item $info->c_inet_phys_address()

Maps an address of type C<cInetNetToMediaNetAddressType> on interface C<ifIndex> to a physical address.

=back

=head1 MUNGES

=over 

=item munge_physaddr()

Takes an octet stream (HEX-STRING) and returns a colon separated ASCII hex
string.

=back

=cut