| 1 | # SNMP::Info::VRF | ||
| 2 | # | ||
| 3 | # Copyright (c) 2026 by SNMP::Info Developers. | ||
| 4 | # | ||
| 5 | # Redistribution and use in source and binary forms, with or without | ||
| 6 | # modification, are permitted provided that the following conditions are met: | ||
| 7 | # | ||
| 8 | # * Redistributions of source code must retain the above copyright notice, | ||
| 9 | # this list of conditions and the following disclaimer. | ||
| 10 | # * Redistributions in binary form must reproduce the above copyright | ||
| 11 | # notice, this list of conditions and the following disclaimer in the | ||
| 12 | # documentation and/or other materials provided with the distribution. | ||
| 13 | # * Neither the name of the University of California, Santa Cruz nor the | ||
| 14 | # names of its contributors may be used to endorse or promote products | ||
| 15 | # derived from this software without specific prior written permission. | ||
| 16 | # | ||
| 17 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 18 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 19 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 20 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
| 21 | # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 22 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 23 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 24 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 25 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 26 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 27 | # POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | |||
| 29 | package SNMP::Info::VRF; | ||
| 30 | |||
| 31 | use strict; | ||
| 32 | use warnings; | ||
| 33 | use Exporter; | ||
| 34 | use SNMP::Info; | ||
| 35 | |||
| 36 | @SNMP::Info::VRF::ISA = qw/SNMP::Info Exporter/; | ||
| 37 | @SNMP::Info::VRF::EXPORT_OK = qw//; | ||
| 38 | |||
| 39 | our ( $VERSION, %MIBS, %FUNCS, %GLOBALS, %MUNGE ); | ||
| 40 | |||
| 41 | $VERSION = '3.985003'; | ||
| 42 | |||
| 43 | %MIBS = ( | ||
| 44 | 'MPLS-L3VPN-STD-MIB' => 'mplsL3VpnConfiguredVrfs', | ||
| 45 | 'IP-FORWARD-MIB' => 'inetCidrRouteNumber', | ||
| 46 | ); | ||
| 47 | |||
| 48 | %GLOBALS = ( 'vrf_configured' => 'mplsL3VpnConfiguredVrfs', ); | ||
| 49 | |||
| 50 | %FUNCS = ( | ||
| 51 | 'vrf_l3vpn_descr' => 'mplsL3VpnVrfDescription', | ||
| 52 | 'vrf_l3vpn_rd' => 'mplsL3VpnVrfRD', | ||
| 53 | 'vrf_l3vpn_oper' => 'mplsL3VpnVrfOperStatus', | ||
| 54 | 'vrf_l3vpn_if' => 'mplsL3VpnIfConfRowStatus', | ||
| 55 | 'vrf_l3vpn_rt' => 'mplsL3VpnVrfRT', | ||
| 56 | 'vrf_l3vpn_rte_if' => 'mplsL3VpnVrfRteInetCidrIfIndex', | ||
| 57 | 'vrf_l3vpn_rte_proto' => 'mplsL3VpnVrfRteInetCidrProto', | ||
| 58 | |||
| 59 | # IP-FORWARD-MIB, context mode only | ||
| 60 | 'vrf_cidr_if' => 'inetCidrRouteIfIndex', | ||
| 61 | 'vrf_cidr_proto' => 'inetCidrRouteProto', | ||
| 62 | ); | ||
| 63 | |||
| 64 | %MUNGE = (); | ||
| 65 | |||
| 66 | my $LIST_SOURCE = 'MPLS-L3VPN-STD-MIB'; | ||
| 67 | |||
| 68 | # The IPv6 role's ipv6_n2p_* subs walk these columns, which walk_status names | ||
| 69 | my @ND_COLUMNS = qw/ip_n2p_phys_addr c_inet_phys_addr i6_n2p_phys_addr/; | ||
| 70 | |||
| 71 | # Layer3's at_* subs fall back to atTable, then ipNetToPhysicalTable, and | ||
| 72 | # its at_netaddr fallback reads the paddr columns to choose | ||
| 73 | my @ARP_PADDR_COLUMNS = qw/at_paddr old_at_paddr ip_n2p_phys_addr/; | ||
| 74 | my @ARP_NETADDR_COLUMNS | ||
| 75 | = ( qw/at_netaddr old_at_netaddr/, @ARP_PADDR_COLUMNS ); | ||
| 76 | |||
| 77 | # mplsL3VpnVrfRTType values, which sit in the last arc of the index | ||
| 78 | my %RT_TYPE = ( 1 => 'import', 2 => 'export', 3 => 'both' ); | ||
| 79 | |||
| 80 | # ipAddressTable index types: bytes is the address part, length the whole | ||
| 81 | my %INET_TYPE = ( | ||
| 82 | 1 => { family => 'ipv4', bytes => 4, length => 4 }, | ||
| 83 | 2 => { family => 'ipv6', bytes => 16, length => 16 }, | ||
| 84 | 3 => { family => 'ipv4', bytes => 4, length => 8 }, | ||
| 85 | 4 => { family => 'ipv6', bytes => 16, length => 20 }, | ||
| 86 | ); | ||
| 87 | |||
| 88 | # ipAddressType values that are the device's own address. An anycast | ||
| 89 | # address still is, and EXOS types its IPv6 interface addresses so. | ||
| 90 | my %OWN_ADDRESS_TYPE = ( unicast => 1, anycast => 1 ); | ||
| 91 | |||
| 92 | sub vrf_instances { | ||
| 93 | my $vrf = shift; | ||
| 94 | my $caller_names = shift; | ||
| 95 | |||
| 96 | return unless $vrf->_vrf_default_context_ok(); | ||
| 97 | my $instances = $vrf->{_vrf_instances} ||= $vrf->_vrf_all_instances(); | ||
| 98 | my $names = ref $caller_names eq 'ARRAY' ? $caller_names : []; | ||
| 99 | return $instances if !@$names or $vrf->_vrf_device_lists($instances); | ||
| 100 | |||
| 101 | my %with_caller = %$instances; | ||
| 102 | foreach my $name (@$names) { | ||
| 103 | next if $with_caller{$name}; | ||
| 104 | $with_caller{$name} = _vrf_record( $name, 'vrf', 'caller' ); | ||
| 105 | $vrf->_vrf_set_contexts( $with_caller{$name} ); | ||
| 106 | } | ||
| 107 | return \%with_caller; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub _vrf_all_instances { | ||
| 111 | my $vrf = shift; | ||
| 112 | |||
| 113 | my $instances = $vrf->_vrf_served_instances(); | ||
| 114 | $instances->{default} ||= _vrf_record( 'default', 'default', 'device' ); | ||
| 115 | $instances->{default}{kind} = 'default'; | ||
| 116 | return $instances; | ||
| 117 | } | ||
| 118 | |||
| 119 | # Where the class has a VRF context kind, a non-default instance lists its | ||
| 120 | # own context, or none when the device gives it none: the default context | ||
| 121 | # never serves its rows. | ||
| 122 | sub _vrf_set_contexts { | ||
| 123 | my $vrf = shift; | ||
| 124 | my $record = shift; | ||
| 125 | |||
| 126 | return if $record->{name} eq 'default'; | ||
| 127 | return unless $vrf->context_kinds('vrf'); | ||
| 128 | my $context = $vrf->snmp_context( 'vrf', $record->{name} ); | ||
| 129 | $record->{contexts} = $context ? [$context] : []; | ||
| 130 | return; | ||
| 131 | } | ||
| 132 | |||
| 133 | # True when the device served a VRF row, so caller names have nothing to | ||
| 134 | # add. A configured count alone does not list: an agent can count zero | ||
| 135 | # MPLS-L3VPN VRFs on a device that has instances of another kind. | ||
| 136 | sub _vrf_device_lists { | ||
| 137 | my $vrf = shift; | ||
| 138 | my $instances = shift; | ||
| 139 | |||
| 140 | return ( grep { $_->{sources}{list} } values %$instances ) ? 1 : 0; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub _vrf_served_instances { | ||
| 144 | my $vrf = shift; | ||
| 145 | |||
| 146 | my $instances = $vrf->_vrf_mpls_records(); | ||
| 147 | $vrf->{_vrf_vendor_listed} = %$instances ? 0 : 1; | ||
| 148 | $instances = $vrf->_vrf_vendor_records() if !%$instances; | ||
| 149 | $vrf->_vrf_list_default( $instances, $LIST_SOURCE ) | ||
| 150 | if $vrf->{_vrf_mpls_served}; | ||
| 151 | $vrf->_vrf_add_route_targets($instances); | ||
| 152 | $vrf->_vrf_add_members($instances); | ||
| 153 | return $instances; | ||
| 154 | } | ||
| 155 | |||
| 156 | # The one rule for a served default. A list source that served any row, | ||
| 157 | # even only the global or an internal instance's, lists default too, so a | ||
| 158 | # device with zero VRFs reads none rather than unsupported, and caller | ||
| 159 | # names add nothing. The first source to call it names default's list. | ||
| 160 | sub _vrf_list_default { | ||
| 161 | my ( $vrf, $instances, $source ) = @_; | ||
| 162 | |||
| 163 | $instances->{default} ||= _vrf_record( 'default', 'default', 'device' ); | ||
| 164 | $instances->{default}{sources}{list} ||= $source; | ||
| 165 | return; | ||
| 166 | } | ||
| 167 | |||
| 168 | # True when the list came from the vendor hook: MPLS-L3VPN-STD-MIB served | ||
| 169 | # no instance row. Memoized because a second read of the MPLS table would | ||
| 170 | # replace its walk status. | ||
| 171 | sub _vrf_vendor_listed { | ||
| 172 | my $vrf = shift; | ||
| 173 | |||
| 174 | $vrf->{_vrf_vendor_listed} //= %{ $vrf->_vrf_mpls_records() } ? 0 : 1; | ||
| 175 | return $vrf->{_vrf_vendor_listed}; | ||
| 176 | } | ||
| 177 | |||
| 178 | # One record per row mplsL3VpnVrfTable serves, minus the internal ones | ||
| 179 | sub _vrf_mpls_records { | ||
| 180 | my $vrf = shift; | ||
| 181 | |||
| 182 | my $descr = $vrf->vrf_l3vpn_descr() || {}; | ||
| 183 | my $rd = $vrf->vrf_l3vpn_rd() || {}; | ||
| 184 | my $oper = $vrf->vrf_l3vpn_oper() || {}; | ||
| 185 | my %iids = map { $_ => 1 } keys %$descr, keys %$rd, keys %$oper; | ||
| 186 | $vrf->{_vrf_mpls_served} = %iids ? 1 : 0; | ||
| 187 | |||
| 188 | my %instances; | ||
| 189 | foreach my $iid ( keys %iids ) { | ||
| 190 | my ($name) = $vrf->_vrf_decode_instance( $iid, 0, 0 ); | ||
| 191 | next if !defined $name or $vrf->_vrf_internal_instance($name); | ||
| 192 | |||
| 193 | my $record = _vrf_record( $name, 'vrf', 'device' ); | ||
| 194 | $record->{description} = _vrf_text( $descr->{$iid} ); | ||
| 195 | $record->{oper} = _vrf_text( $oper->{$iid} ); | ||
| 196 | $record->{rd} = $vrf->_vrf_rd( $rd->{$iid} ); | ||
| 197 | $record->{sources}{list} = $LIST_SOURCE; | ||
| 198 | $record->{sources}{rd} = $LIST_SOURCE if defined $record->{rd}; | ||
| 199 | $vrf->_vrf_set_contexts($record); | ||
| 200 | $instances{$name} = $record; | ||
| 201 | } | ||
| 202 | return \%instances; | ||
| 203 | } | ||
| 204 | |||
| 205 | # The vendor list, used only when MPLS-L3VPN-STD-MIB served no instance row | ||
| 206 | sub _vrf_vendor_records { | ||
| 207 | my $vrf = shift; | ||
| 208 | |||
| 209 | my $records = $vrf->_vrf_vendor_instances() || {}; | ||
| 210 | $vrf->_vrf_set_contexts($_) for values %$records; | ||
| 211 | return $records; | ||
| 212 | } | ||
| 213 | |||
| 214 | # Membership is the sources key; i_vrf holds the ports themselves | ||
| 215 | sub _vrf_add_members { | ||
| 216 | my $vrf = shift; | ||
| 217 | my $instances = shift; | ||
| 218 | |||
| 219 | my $vendor_ports | ||
| 220 | = $vrf->_vrf_vendor_listed() ? $vrf->_vrf_vendor_membership() : {}; | ||
| 221 | my %vendor_named = map { $_ => 1 } values %$vendor_ports; | ||
| 222 | foreach my $name ( values %{ $vrf->_vrf_membership() } ) { | ||
| 223 | next unless $instances->{$name}; | ||
| 224 | $instances->{$name}{sources}{members} | ||
| 225 | = $vendor_named{$name} | ||
| 226 | ? $instances->{$name}{sources}{list} | ||
| 227 | : $LIST_SOURCE; | ||
| 228 | } | ||
| 229 | return; | ||
| 230 | } | ||
| 231 | |||
| 232 | # { ifIndex => name } for every active row of mplsL3VpnIfConfTable. The | ||
| 233 | # index is the VRF name then the ifIndex. clear_cache drops the memo. | ||
| 234 | sub _vrf_membership { | ||
| 235 | my $vrf = shift; | ||
| 236 | |||
| 237 | return $vrf->{_vrf_members} ||= $vrf->_vrf_decode_membership(); | ||
| 238 | } | ||
| 239 | |||
| 240 | sub _vrf_decode_membership { | ||
| 241 | my $vrf = shift; | ||
| 242 | |||
| 243 | my $rows = $vrf->vrf_l3vpn_if() || {}; | ||
| 244 | my %ports = $vrf->_vrf_vendor_listed() | ||
| 245 | ? %{ $vrf->_vrf_vendor_membership() } | ||
| 246 | : (); | ||
| 247 | foreach my $iid ( keys %$rows ) { | ||
| 248 | next unless $rows->{$iid} =~ /^(?:active|1)$/; | ||
| 249 | my ( $name, $ifindex, @extra ) | ||
| 250 | = $vrf->_vrf_decode_instance( $iid, 1, 0 ); | ||
| 251 | next if !defined $ifindex or @extra; | ||
| 252 | next if $name eq 'default' or $vrf->_vrf_internal_instance($name); | ||
| 253 | $ports{$ifindex} = $name; | ||
| 254 | } | ||
| 255 | return \%ports; | ||
| 256 | } | ||
| 257 | |||
| 258 | sub _vrf_record { | ||
| 259 | my ( $name, $kind, $listed_by ) = @_; | ||
| 260 | |||
| 261 | return { | ||
| 262 | name => $name, | ||
| 263 | kind => $kind, | ||
| 264 | description => undef, | ||
| 265 | oper => undef, | ||
| 266 | rd => undef, | ||
| 267 | rt_import => [], | ||
| 268 | rt_export => [], | ||
| 269 | l3_vni => undef, | ||
| 270 | l3_isid => undef, | ||
| 271 | listed_by => $listed_by, | ||
| 272 | sources => {}, | ||
| 273 | contexts => [ { kind => 'none' } ], | ||
| 274 | }; | ||
| 275 | } | ||
| 276 | |||
| 277 | sub _vrf_text { | ||
| 278 | my $value = shift; | ||
| 279 | |||
| 280 | return ( defined $value and length $value ) ? $value : undef; | ||
| 281 | } | ||
| 282 | |||
| 283 | # Index is name, rtIndex, rtType; a 'both' target belongs in each list | ||
| 284 | sub _vrf_add_route_targets { | ||
| 285 | my $vrf = shift; | ||
| 286 | my $instances = shift; | ||
| 287 | |||
| 288 | my $targets = $vrf->vrf_l3vpn_rt() || {}; | ||
| 289 | my ( %import, %export ); | ||
| 290 | foreach my $iid ( keys %$targets ) { | ||
| 291 | my ( $name, undef, $type_arc ) | ||
| 292 | = $vrf->_vrf_decode_instance( $iid, 1, 1 ); | ||
| 293 | next unless defined $type_arc and $instances->{$name}; | ||
| 294 | next unless defined $targets->{$iid} and length $targets->{$iid}; | ||
| 295 | |||
| 296 | my $type = $RT_TYPE{$type_arc} or next; | ||
| 297 | $import{$name}{ $targets->{$iid} } = 1 if $type ne 'export'; | ||
| 298 | $export{$name}{ $targets->{$iid} } = 1 if $type ne 'import'; | ||
| 299 | } | ||
| 300 | |||
| 301 | foreach my $name ( keys %$instances ) { | ||
| 302 | my $record = $instances->{$name}; | ||
| 303 | $record->{rt_import} = [ sort keys %{ $import{$name} } ]; | ||
| 304 | $record->{rt_export} = [ sort keys %{ $export{$name} } ]; | ||
| 305 | $record->{sources}{rt} = $LIST_SOURCE | ||
| 306 | if @{ $record->{rt_import} } | ||
| 307 | or @{ $record->{rt_export} }; | ||
| 308 | } | ||
| 309 | return; | ||
| 310 | } | ||
| 311 | |||
| 312 | # Decodes a length-prefixed octet string index. Returns the string and the | ||
| 313 | # arcs after it, or an empty list when the index is shorter than its prefix. | ||
| 314 | sub _vrf_decode_name { | ||
| 315 | my $vrf = shift; | ||
| 316 | my $iid = shift; | ||
| 317 | |||
| 318 | return unless defined $iid and $iid =~ /^\d+(?:\.\d+)*$/; | ||
| 319 | my ( $length, @arcs ) = split /\./, $iid; | ||
| 320 | return if $length > @arcs; | ||
| 321 | my @octets = splice @arcs, 0, $length; | ||
| 322 | return if grep { $_ > 255 } @octets; | ||
| 323 | |||
| 324 | return ( join( q{}, map {chr} @octets ), @arcs ); | ||
| 325 | } | ||
| 326 | |||
| 327 | # As _vrf_decode_name, with the empty name, the global table, as default. | ||
| 328 | # FRR writes the name with no length (oid_copy_str in lib/snmp.c); a first | ||
| 329 | # arc above 31 shows it, as MplsL3VpnName is at most 31 octets. $packed and | ||
| 330 | # $tail give the shape after the name, used only for that form. | ||
| 331 | sub _vrf_decode_instance { | ||
| 332 | my ( $vrf, $iid, $packed, $tail ) = @_; | ||
| 333 | |||
| 334 | return $vrf->_vrf_decode_unprefixed( $iid, $packed, $tail ) | ||
| 335 | if _vrf_is_unprefixed($iid); | ||
| 336 | my ( $name, @arcs ) = $vrf->_vrf_decode_name($iid) or return; | ||
| 337 | return ( ( length $name ? $name : 'default' ), @arcs ); | ||
| 338 | } | ||
| 339 | |||
| 340 | sub _vrf_is_unprefixed { | ||
| 341 | my $iid = shift; | ||
| 342 | |||
| 343 | return 0 unless defined $iid and $iid =~ /^(\d+)(?:\.\d+)*$/; | ||
| 344 | return $1 > 31 ? 1 : 0; | ||
| 345 | } | ||
| 346 | |||
| 347 | # With no length to split by, the index is matched against the names the | ||
| 348 | # VRF table serves. FRR writes each of the $packed integers after the name | ||
| 349 | # as four arcs, one per byte, big-endian (oid_copy_int). $tail counts the | ||
| 350 | # arcs after those, undef for any; it tells red2's rows from red's. | ||
| 351 | sub _vrf_decode_unprefixed { | ||
| 352 | my ( $vrf, $iid, $packed, $tail ) = @_; | ||
| 353 | |||
| 354 | $packed //= 0; | ||
| 355 | foreach my $name ( @{ $vrf->_vrf_unprefixed_names() } ) { | ||
| 356 | my $rest = _vrf_arcs_after_name( $name, $iid ) or next; | ||
| 357 | next if @$rest < 4 * $packed; | ||
| 358 | next if defined $tail and @$rest != 4 * $packed + $tail; | ||
| 359 | |||
| 360 | my @integers | ||
| 361 | = map { _vrf_unpack_integer( splice @$rest, 0, 4 ) } 1 .. $packed; | ||
| 362 | return ( $name, @integers, @$rest ); | ||
| 363 | } | ||
| 364 | return; | ||
| 365 | } | ||
| 366 | |||
| 367 | # Names of the VRF table rows indexed by the name alone, longest first | ||
| 368 | sub _vrf_unprefixed_names { | ||
| 369 | my $vrf = shift; | ||
| 370 | |||
| 371 | my %iids = map { $_ => 1 } | ||
| 372 | map { keys %{ $vrf->$_() || {} } } | ||
| 373 | qw/vrf_l3vpn_descr vrf_l3vpn_rd vrf_l3vpn_oper/; | ||
| 374 | my @names; | ||
| 375 | foreach my $iid ( grep { _vrf_is_unprefixed($_) } keys %iids ) { | ||
| 376 | my @octets = split /\./, $iid; | ||
| 377 | next if grep { $_ > 255 } @octets; | ||
| 378 | push @names, join q{}, map {chr} @octets; | ||
| 379 | } | ||
| 380 | return [ sort { length $b <=> length $a or $a cmp $b } @names ]; | ||
| 381 | } | ||
| 382 | |||
| 383 | sub _vrf_arcs_after_name { | ||
| 384 | my ( $name, $iid ) = @_; | ||
| 385 | |||
| 386 | my $prefix = join '.', map {ord} split //, $name; | ||
| 387 | return [] if $iid eq $prefix; | ||
| 388 | return if index( $iid, "$prefix." ) != 0; | ||
| 389 | return [ split /\./, substr( $iid, length($prefix) + 1 ) ]; | ||
| 390 | } | ||
| 391 | |||
| 392 | sub _vrf_unpack_integer { | ||
| 393 | my @bytes = @_; | ||
| 394 | |||
| 395 | my $value = 0; | ||
| 396 | $value = $value * 256 + $_ for @bytes; | ||
| 397 | return $value; | ||
| 398 | } | ||
| 399 | |||
| 400 | # Empty and 0:0 mean no distinguisher; printable text is kept as served and | ||
| 401 | # anything else is binary, shown as colon separated hex | ||
| 402 | sub _vrf_rd { | ||
| 403 | my $vrf = shift; | ||
| 404 | my $raw = shift; | ||
| 405 | |||
| 406 | return if !defined $raw or $raw eq q{} or $raw eq '0:0'; | ||
| 407 | return $raw if $raw =~ /^[\x20-\x7e]+$/; | ||
| 408 | return join ':', map { sprintf '%02x', ord } split //, $raw; | ||
| 409 | } | ||
| 410 | |||
| 411 | sub i_vrf { | ||
| 412 | my $vrf = shift; | ||
| 413 | |||
| 414 | return $vrf->_vrf_context_i_vrf() if $vrf->_vrf_context_filters(); | ||
| 415 | return unless $vrf->_vrf_default_context_ok(); | ||
| 416 | return $vrf->_vrf_membership(); | ||
| 417 | } | ||
| 418 | |||
| 419 | # Context mode: the ports of the instance whose context the session is in | ||
| 420 | sub _vrf_context_i_vrf { | ||
| 421 | my $vrf = shift; | ||
| 422 | |||
| 423 | my $name = $vrf->_vrf_context_instance(); | ||
| 424 | return $vrf->_vrf_membership() | ||
| 425 | if defined $name | ||
| 426 | and $name eq 'default' | ||
| 427 | and ( $vrf->_vrf_default_context_leaks() | ||
| 428 | or $vrf->_vrf_default_context_members() ); | ||
| 429 | return $vrf->_vrf_context_ports($name) if defined $name; | ||
| 430 | |||
| 431 | my $context = $vrf->_vrf_session_context(); | ||
| 432 | $vrf->error_throw( | ||
| 433 | "i_vrf() maps the ports of the instance whose context this" | ||
| 434 | . " session is in, but no instance in vrf_instances() has the" | ||
| 435 | . " context '$context'. Switch to the context snmp_context('vrf')" | ||
| 436 | . " gives for a name vrf_instances() lists.\n" ); | ||
| 437 | return; | ||
| 438 | } | ||
| 439 | |||
| 440 | # default in the default context, else the instance whose context is the | ||
| 441 | # session's: the context name itself where the class says so, otherwise | ||
| 442 | # the listed instance with that context. The list is read without the | ||
| 443 | # default context guard, since the context map serves it in every context. | ||
| 444 | sub _vrf_context_instance { | ||
| 445 | my $vrf = shift; | ||
| 446 | |||
| 447 | my $context = $vrf->_vrf_session_context(); | ||
| 448 | return 'default' unless length $context; | ||
| 449 | return $context if $vrf->_vrf_context_is_instance_name(); | ||
| 450 | |||
| 451 | my $instances = $vrf->{_vrf_instances} ||= $vrf->_vrf_all_instances(); | ||
| 452 | foreach my $name ( sort keys %$instances ) { | ||
| 453 | next if $name eq 'default'; | ||
| 454 | return $name | ||
| 455 | if grep { $_ eq $context } $vrf->_vrf_contexts_of($name); | ||
| 456 | } | ||
| 457 | return; | ||
| 458 | } | ||
| 459 | |||
| 460 | # { ifIndex => $name } for each port holding an address in this context, | ||
| 461 | # or, where the class says so, each port a route of this context uses. The | ||
| 462 | # walks decide membership, so they count toward the read. | ||
| 463 | sub _vrf_context_ports { | ||
| 464 | my $vrf = shift; | ||
| 465 | my $name = shift; | ||
| 466 | |||
| 467 | return $vrf->_vrf_route_ports($name) | ||
| 468 | if $vrf->_vrf_context_ports_from_routes(); | ||
| 469 | my $rows = $vrf->_vrf_address_rows($name); | ||
| 470 | return { map { $_->{ifindex} => $name } values %$rows }; | ||
| 471 | } | ||
| 472 | |||
| 473 | # The ports inetCidrRouteIfIndex gives the routes of this context; 0 is no | ||
| 474 | # interface | ||
| 475 | sub _vrf_route_ports { | ||
| 476 | my $vrf = shift; | ||
| 477 | my $name = shift; | ||
| 478 | |||
| 479 | my $ifs = $vrf->_vrf_column( 'vrf_cidr_if', $name ); | ||
| 480 | my %ports | ||
| 481 | = map { $_ => $name } | ||
| 482 | grep { defined _vrf_route_ifindex($_) } values %$ifs; | ||
| 483 | return \%ports; | ||
| 484 | } | ||
| 485 | |||
| 486 | # The contexts _vrf_session_context() reports in $name's context: the v3 | ||
| 487 | # name, and the context of a community the v2c template builds. Where the | ||
| 488 | # class has no VRF context kind the instance's context is its name; where | ||
| 489 | # it has one and gives this instance none, the instance has no context. | ||
| 490 | sub _vrf_contexts_of { | ||
| 491 | my $vrf = shift; | ||
| 492 | my $name = shift; | ||
| 493 | |||
| 494 | return $name unless $vrf->context_kinds('vrf'); | ||
| 495 | my $context = $vrf->snmp_context( 'vrf', $name ) or return; | ||
| 496 | my $v2c = $context->{v2c}; | ||
| 497 | my @v2c | ||
| 498 | = !defined $v2c ? () | ||
| 499 | : $v2c =~ /^%s\@(.+)$/ ? $1 | ||
| 500 | : $v2c =~ /^(.+)\@%s$/ ? $1 | ||
| 501 | : (); | ||
| 502 | return grep { defined and length } $context->{v3}, @v2c; | ||
| 503 | } | ||
| 504 | |||
| 505 | # 1 when the list is whole, 0 when it may be short or a list walk failed, | ||
| 506 | # undef when the device serves no VRF list and the caller named none | ||
| 507 | sub vrf_instances_complete { | ||
| 508 | my $vrf = shift; | ||
| 509 | my $caller_names = shift; | ||
| 510 | |||
| 511 | my $instances = $vrf->vrf_instances($caller_names) or return; | ||
| 512 | return 0 if grep { $_->{listed_by} eq 'caller' } values %$instances; | ||
| 513 | |||
| 514 | my @listed = grep { $_->{sources}{list} } values %$instances; | ||
| 515 | my $configured = $vrf->vrf_configured(); | ||
| 516 | return 0 if $vrf->_vrf_walk_incomplete(); | ||
| 517 | return if !defined $configured and !@listed; | ||
| 518 | my @rows = grep { $_->{name} ne 'default' } @listed; | ||
| 519 | return 0 if defined $configured and $configured > @rows; | ||
| 520 | return 1 if $vrf->_vrf_context_filters(); | ||
| 521 | return 0 if $vrf->_vrf_split_agent( \@rows, $configured ); | ||
| 522 | return 1; | ||
| 523 | } | ||
| 524 | |||
| 525 | sub _vrf_walk_incomplete { | ||
| 526 | my $vrf = shift; | ||
| 527 | |||
| 528 | my @walked = qw/vrf_l3vpn_descr vrf_l3vpn_rd vrf_l3vpn_oper | ||
| 529 | vrf_l3vpn_if vrf_l3vpn_rt/; | ||
| 530 | push @walked, $vrf->_vrf_vendor_list_methods() | ||
| 531 | if $vrf->_vrf_vendor_listed(); | ||
| 532 | foreach my $method (@walked) { | ||
| 533 | my $status = $vrf->walk_status($method) or next; | ||
| 534 | return 1 unless $status->{complete}; | ||
| 535 | } | ||
| 536 | return 0; | ||
| 537 | } | ||
| 538 | |||
| 539 | # A row with no port is fine when the agent serves ports for other rows; it | ||
| 540 | # is a gap when the agent cannot say whether it serves ports at all | ||
| 541 | sub _vrf_split_agent { | ||
| 542 | my $vrf = shift; | ||
| 543 | my $rows = shift; | ||
| 544 | my $configured = shift; | ||
| 545 | |||
| 546 | my $members = $vrf->i_vrf() || {}; | ||
| 547 | my %with_port = map { $_ => 1 } values %$members; | ||
| 548 | return 0 unless grep { !$with_port{ $_->{name} } } @$rows; | ||
| 549 | return 1 if !defined $configured; | ||
| 550 | return %$members ? 0 : 1; | ||
| 551 | } | ||
| 552 | |||
| 553 | # ARP and IPv6 neighbor rows of one instance, keyed as the source tables | ||
| 554 | # are: ifIndex, then the address arcs. Attribution mode keeps the rows whose | ||
| 555 | # port belongs to the instance; context mode keeps the rows on the ports of | ||
| 556 | # this context (_vrf_context_ports), since a context can serve rows for | ||
| 557 | # ports of other instances. | ||
| 558 | sub vrf_neighbors { | ||
| 559 | my $vrf = shift; | ||
| 560 | my $name = shift; | ||
| 561 | |||
| 562 | return unless defined $name and $vrf->_vrf_in_reader_context($name); | ||
| 563 | my $context_mode = $vrf->_vrf_context_filters(); | ||
| 564 | return if !$context_mode and !$vrf->_vrf_is_listed($name); | ||
| 565 | |||
| 566 | my %rows | ||
| 567 | = ( %{ $vrf->_vrf_arp_rows($name) }, %{ $vrf->_vrf_nd_rows($name) }, | ||
| 568 | ); | ||
| 569 | my $kept | ||
| 570 | = $context_mode | ||
| 571 | ? $vrf->_vrf_keep_context_ports( \%rows, $name ) | ||
| 572 | : $vrf->_vrf_keep_owned( \%rows, $name ); | ||
| 573 | return $vrf->_vrf_drop_own_rows( $kept, $name ); | ||
| 574 | } | ||
| 575 | |||
| 576 | # Drops the rows that are the instance's own addresses: a global address | ||
| 577 | # the instance holds on any port, a link-local one it holds on that port, | ||
| 578 | # and any row at the MAC of its own port. Devices type a host's row local | ||
| 579 | # as well as their own, so the type is not used. The address and port MAC | ||
| 580 | # walks decide which rows go, so they count toward the read. | ||
| 581 | sub _vrf_drop_own_rows { | ||
| 582 | my ( $vrf, $rows, $name ) = @_; | ||
| 583 | |||
| 584 | my %own; | ||
| 585 | foreach my $address ( values %{ $vrf->_vrf_instance_addresses($name) } ) { | ||
| 586 | $own{ _vrf_own_key( $address->{ifindex}, $address->{ip} ) } = 1; | ||
| 587 | } | ||
| 588 | my $port_macs = $vrf->_vrf_column( 'i_mac', $name ); | ||
| 589 | foreach my $key ( keys %$rows ) { | ||
| 590 | my $row = $rows->{$key}; | ||
| 591 | delete $rows->{$key} | ||
| 592 | if $own{ _vrf_own_key( @$row{qw/ifindex ip/} ) } | ||
| 593 | or _vrf_at_port_mac( $row, $port_macs ); | ||
| 594 | } | ||
| 595 | return $rows; | ||
| 596 | } | ||
| 597 | |||
| 598 | sub _vrf_at_port_mac { | ||
| 599 | my ( $row, $port_macs ) = @_; | ||
| 600 | |||
| 601 | my $port_mac = $port_macs->{ $row->{ifindex} }; | ||
| 602 | return 0 unless defined $port_mac; | ||
| 603 | return lc $port_mac eq lc $row->{mac} ? 1 : 0; | ||
| 604 | } | ||
| 605 | |||
| 606 | # A link-local address is one per link, so its port is part of the key | ||
| 607 | sub _vrf_own_key { | ||
| 608 | my ( $ifindex, $ip ) = @_; | ||
| 609 | |||
| 610 | $ip = lc $ip; | ||
| 611 | return $ip =~ /^fe[89ab]/ ? "$ifindex $ip" : $ip; | ||
| 612 | } | ||
| 613 | |||
| 614 | # Interface addresses of one instance, keyed ifIndex.address. The device | ||
| 615 | # serves one address table per port, so an address two VRFs share survives | ||
| 616 | # in each, and ownership comes from the port as for neighbors. In context | ||
| 617 | # mode the addresses decide the ports, so all are kept, unless the routes | ||
| 618 | # decide them; then only those on the context's ports are. | ||
| 619 | sub vrf_addresses { | ||
| 620 | my $vrf = shift; | ||
| 621 | my $name = shift; | ||
| 622 | |||
| 623 | return unless defined $name and $vrf->_vrf_in_reader_context($name); | ||
| 624 | my $context_mode = $vrf->_vrf_context_filters(); | ||
| 625 | return if !$context_mode and !$vrf->_vrf_is_listed($name); | ||
| 626 | |||
| 627 | return $vrf->_vrf_instance_addresses($name); | ||
| 628 | } | ||
| 629 | |||
| 630 | # vrf_addresses() past its guards | ||
| 631 | sub _vrf_instance_addresses { | ||
| 632 | my $vrf = shift; | ||
| 633 | my $name = shift; | ||
| 634 | |||
| 635 | my $rows = $vrf->_vrf_address_rows($name); | ||
| 636 | return $vrf->_vrf_keep_context_ports( $rows, $name ) | ||
| 637 | if $vrf->_vrf_context_filters() | ||
| 638 | and $vrf->_vrf_context_ports_from_routes(); | ||
| 639 | return $vrf->_vrf_drop_leaked( $rows, $name ) | ||
| 640 | if $vrf->_vrf_context_filters(); | ||
| 641 | return $vrf->_vrf_keep_owned( $rows, $name ); | ||
| 642 | } | ||
| 643 | |||
| 644 | # Context mode: drops the rows on ports another instance holds | ||
| 645 | sub _vrf_drop_leaked { | ||
| 646 | my ( $vrf, $rows, $name ) = @_; | ||
| 647 | |||
| 648 | my $leaked = $vrf->_vrf_leaked_ports($name); | ||
| 649 | foreach my $key ( keys %$rows ) { | ||
| 650 | delete $rows->{$key} if exists $leaked->{ $rows->{$key}{ifindex} }; | ||
| 651 | } | ||
| 652 | return $rows; | ||
| 653 | } | ||
| 654 | |||
| 655 | # Context mode: drops the rows on ports that are not this context's | ||
| 656 | sub _vrf_keep_context_ports { | ||
| 657 | my ( $vrf, $rows, $name ) = @_; | ||
| 658 | |||
| 659 | my $ports = $vrf->_vrf_context_ports($name); | ||
| 660 | my $leaked = $vrf->_vrf_leaked_ports($name); | ||
| 661 | foreach my $key ( keys %$rows ) { | ||
| 662 | my $ifindex = $rows->{$key}{ifindex}; | ||
| 663 | delete $rows->{$key} | ||
| 664 | if !exists $ports->{$ifindex} | ||
| 665 | or exists $leaked->{$ifindex}; | ||
| 666 | } | ||
| 667 | return $rows; | ||
| 668 | } | ||
| 669 | |||
| 670 | # The ports the default context's membership tables give another instance, | ||
| 671 | # where the default context serves their rows too; {} everywhere else. The | ||
| 672 | # membership walk decides which rows go, so it counts toward the read. | ||
| 673 | sub _vrf_leaked_ports { | ||
| 674 | my $vrf = shift; | ||
| 675 | my $name = shift; | ||
| 676 | |||
| 677 | return {} | ||
| 678 | unless $name eq 'default' and $vrf->_vrf_default_context_leaks(); | ||
| 679 | $vrf->_vrf_record_walk( $name, 'vrf_l3vpn_if' ); | ||
| 680 | my %leaked = %{ $vrf->_vrf_membership() || {} }; | ||
| 681 | %leaked = ( %leaked, %$_ ) for values %{ $vrf->{_vrf_excluded} || {} }; | ||
| 682 | return \%leaked; | ||
| 683 | } | ||
| 684 | |||
| 685 | # Drops the rows whose port is not in the instance. The membership walk | ||
| 686 | # decides ownership, so it counts toward the read. | ||
| 687 | sub _vrf_keep_owned { | ||
| 688 | my ( $vrf, $rows, $name ) = @_; | ||
| 689 | |||
| 690 | $vrf->_vrf_record_walk( $name, 'vrf_l3vpn_if' ); | ||
| 691 | $vrf->_vrf_record_walk( $name, $vrf->_vrf_vendor_membership_methods() ); | ||
| 692 | my $members = $vrf->i_vrf(); | ||
| 693 | foreach my $key ( keys %$rows ) { | ||
| 694 | my $owner = _vrf_owner_of( $members, $rows->{$key}{ifindex} ); | ||
| 695 | delete $rows->{$key} if !defined $owner or $owner ne $name; | ||
| 696 | } | ||
| 697 | return $rows; | ||
| 698 | } | ||
| 699 | |||
| 700 | # Subnets of one instance: the network of each address that has a prefix | ||
| 701 | # length, plus the connected routes when the class enables them. A prefix | ||
| 702 | # found both ways lists 'address' first. | ||
| 703 | sub vrf_subnets { | ||
| 704 | my $vrf = shift; | ||
| 705 | my $name = shift; | ||
| 706 | |||
| 707 | return unless defined $name and $vrf->_vrf_in_reader_context($name); | ||
| 708 | my $context_mode = $vrf->_vrf_context_filters(); | ||
| 709 | return if !$context_mode and !$vrf->_vrf_is_listed($name); | ||
| 710 | |||
| 711 | my %subnets; | ||
| 712 | my $addresses = $vrf->vrf_addresses($name) || {}; | ||
| 713 | foreach my $key ( sort keys %$addresses ) { | ||
| 714 | my $subnet = _vrf_address_subnet( $addresses->{$key} ) or next; | ||
| 715 | next if _vrf_link_local($subnet); | ||
| 716 | _vrf_add_subnet( \%subnets, $subnet, 'address' ); | ||
| 717 | } | ||
| 718 | return \%subnets unless $vrf->_vrf_subnets_from_routes(); | ||
| 719 | |||
| 720 | my $routes | ||
| 721 | = $context_mode | ||
| 722 | ? $vrf->_vrf_context_route_subnets($name) | ||
| 723 | : $vrf->_vrf_attributed_route_subnets($name); | ||
| 724 | foreach my $route (@$routes) { | ||
| 725 | next | ||
| 726 | if _vrf_special_purpose($route) | ||
| 727 | and !_vrf_has_subnet( \%subnets, $route ); | ||
| 728 | _vrf_add_subnet( \%subnets, $route, 'route' ); | ||
| 729 | } | ||
| 730 | return \%subnets; | ||
| 731 | } | ||
| 732 | |||
| 733 | # 1 unless a table a reader walked for $name in this session is known to be | ||
| 734 | # incomplete; an absent table is complete. undef when no reader walked for | ||
| 735 | # $name since the cache was last cleared. | ||
| 736 | sub vrf_read_complete { | ||
| 737 | my $vrf = shift; | ||
| 738 | my $name = shift; | ||
| 739 | |||
| 740 | my $whole = $vrf->_vrf_walks_whole($name); | ||
| 741 | return $whole unless $whole; | ||
| 742 | return 0 if $vrf->_vrf_leak_unfiltered($name); | ||
| 743 | return 1; | ||
| 744 | } | ||
| 745 | |||
| 746 | # vrf_read_complete() without the leak: undef, 0 or 1 | ||
| 747 | sub _vrf_walks_whole { | ||
| 748 | my $vrf = shift; | ||
| 749 | my $name = shift; | ||
| 750 | |||
| 751 | my $walked = defined $name ? $vrf->{_vrf_walked}{$name} : undef; | ||
| 752 | return unless $walked; | ||
| 753 | foreach my $method ( keys %$walked ) { | ||
| 754 | my $status = $vrf->walk_status($method) or next; | ||
| 755 | return 0 unless $status->{complete}; | ||
| 756 | } | ||
| 757 | return 1; | ||
| 758 | } | ||
| 759 | |||
| 760 | # The instances that alone make a whole default read partial | ||
| 761 | sub vrf_read_leaked { | ||
| 762 | my $vrf = shift; | ||
| 763 | my $name = shift; | ||
| 764 | |||
| 765 | return unless defined $name and $name eq 'default'; | ||
| 766 | return unless $vrf->_vrf_default_context_leaks(); | ||
| 767 | return if length $vrf->_vrf_session_context(); | ||
| 768 | return unless $vrf->_vrf_walks_whole($name); | ||
| 769 | $vrf->{_vrf_instances} ||= $vrf->_vrf_all_instances(); | ||
| 770 | return if $vrf->_vrf_walk_incomplete(); | ||
| 771 | my @names = sort $vrf->_vrf_unfiltered_names(); | ||
| 772 | return unless @names; | ||
| 773 | return \@names; | ||
| 774 | } | ||
| 775 | |||
| 776 | # True in the default context of a class whose default context leaks while | ||
| 777 | # a listed instance is one the membership table does not cover, such as a | ||
| 778 | # virtual router: its rows cannot be told from the default instance's. A | ||
| 779 | # partial list walk may hide such an instance. An MPLS-listed VRF with no | ||
| 780 | # port has no row to leak. | ||
| 781 | sub _vrf_leak_unfiltered { | ||
| 782 | my $vrf = shift; | ||
| 783 | my $name = shift; | ||
| 784 | |||
| 785 | return 0 unless $name eq 'default' and $vrf->_vrf_default_context_leaks(); | ||
| 786 | return 0 if length $vrf->_vrf_session_context(); | ||
| 787 | $vrf->{_vrf_instances} ||= $vrf->_vrf_all_instances(); | ||
| 788 | return 1 if $vrf->_vrf_walk_incomplete(); | ||
| 789 | return $vrf->_vrf_unfiltered_names() ? 1 : 0; | ||
| 790 | } | ||
| 791 | |||
| 792 | # Listed instances other than default that neither a membership row nor | ||
| 793 | # the caller's exclusion covers | ||
| 794 | sub _vrf_unfiltered_names { | ||
| 795 | my $vrf = shift; | ||
| 796 | |||
| 797 | my $instances = $vrf->{_vrf_instances} ||= $vrf->_vrf_all_instances(); | ||
| 798 | my $excluded = $vrf->{_vrf_excluded} || {}; | ||
| 799 | return map { $_->{name} } | ||
| 800 | grep { | ||
| 801 | $_->{name} ne 'default' | ||
| 802 | and !$excluded->{ $_->{name} } | ||
| 803 | and _vrf_uncovered($_) | ||
| 804 | } values %$instances; | ||
| 805 | } | ||
| 806 | |||
| 807 | # Records the ports of an instance read in its own context, for the | ||
| 808 | # default readers to drop. clear_cache forgets them. | ||
| 809 | sub vrf_exclude_ports { | ||
| 810 | my ( $vrf, $name, $ports ) = @_; | ||
| 811 | |||
| 812 | return 0 unless $vrf->_vrf_default_context_leaks(); | ||
| 813 | return 0 if $vrf->_vrf_subnets_from_routes(); | ||
| 814 | return unless $vrf->_vrf_exclusion_ok( $name, $ports ); | ||
| 815 | $vrf->{_vrf_excluded}{$name} = { map { $_ => $name } @$ports }; | ||
| 816 | return 1; | ||
| 817 | } | ||
| 818 | |||
| 819 | sub _vrf_exclusion_ok { | ||
| 820 | my ( $vrf, $name, $ports ) = @_; | ||
| 821 | |||
| 822 | my $context = $vrf->_vrf_session_context(); | ||
| 823 | return $vrf->error_throw( | ||
| 824 | "vrf_exclude_ports() drops ports from the default context's" | ||
| 825 | . " readers, but this session is in context '$context'. Call" | ||
| 826 | . " update() back to the default context, then call" | ||
| 827 | . " vrf_exclude_ports() before the readers.\n" ) | ||
| 828 | if length $context; | ||
| 829 | return $vrf->error_throw( | ||
| 830 | "vrf_exclude_ports() cannot exclude the default instance: the" | ||
| 831 | . " exclusion removes other instances' rows from default's read." | ||
| 832 | . " Pass a name vrf_read_leaked('default') returns.\n" ) | ||
| 833 | if !defined $name or $name eq 'default'; | ||
| 834 | my $instances = $vrf->{_vrf_instances} ||= $vrf->_vrf_all_instances(); | ||
| 835 | return $vrf->error_throw( | ||
| 836 | "vrf_exclude_ports() was given '$name', which is not an" | ||
| 837 | . " instance vrf_instances() lists. Pass a name" | ||
| 838 | . " vrf_read_leaked('default') returns.\n" ) | ||
| 839 | unless $instances->{$name}; | ||
| 840 | return $vrf->error_throw( | ||
| 841 | "vrf_exclude_ports('$name', ...) takes an array reference of" | ||
| 842 | . " ifIndexes: the keys of i_vrf() read in '$name''s own" | ||
| 843 | . " context.\n" ) | ||
| 844 | unless ref $ports eq 'ARRAY'; | ||
| 845 | return 1; | ||
| 846 | } | ||
| 847 | |||
| 848 | sub _vrf_uncovered { | ||
| 849 | my $record = shift; | ||
| 850 | |||
| 851 | return 0 if $record->{sources}{members}; | ||
| 852 | return ( $record->{sources}{list} // q() ) ne $LIST_SOURCE ? 1 : 0; | ||
| 853 | } | ||
| 854 | |||
| 855 | sub _vrf_has_subnet { | ||
| 856 | my ( $subnets, $subnet ) = @_; | ||
| 857 | |||
| 858 | return exists $subnets->{"$subnet->{prefix}/$subnet->{len}"}; | ||
| 859 | } | ||
| 860 | |||
| 861 | # Space no VRF routes to a neighbor: this and loopback networks, multicast, | ||
| 862 | # and limited broadcast for IPv4; unspecified and loopback, link-local and | ||
| 863 | # multicast for IPv6. A route-only subnet within it is not a VRF subnet. | ||
| 864 | my @SPECIAL_PURPOSE = ( | ||
| 865 | [ ipv4 => '0.0.0.0', 8 ], | ||
| 866 | [ ipv4 => '127.0.0.0', 8 ], | ||
| 867 | [ ipv4 => '224.0.0.0', 4 ], | ||
| 868 | [ ipv4 => '255.255.255.255', 32 ], | ||
| 869 | [ ipv6 => '0:0:0:0:0:0:0:0', 127 ], | ||
| 870 | [ ipv6 => 'fe80:0:0:0:0:0:0:0', 10 ], | ||
| 871 | [ ipv6 => 'ff00:0:0:0:0:0:0:0', 8 ], | ||
| 872 | ); | ||
| 873 | |||
| 874 | sub _vrf_special_purpose { | ||
| 875 | my $subnet = shift; | ||
| 876 | |||
| 877 | foreach my $special (@SPECIAL_PURPOSE) { | ||
| 878 | return 1 if _vrf_within_range( $subnet, $special ); | ||
| 879 | } | ||
| 880 | return 0; | ||
| 881 | } | ||
| 882 | |||
| 883 | # IPv6 link-local space is per link, so it is no VRF subnet whatever | ||
| 884 | # table served it: a device may list its fe80::1 with a short prefix length | ||
| 885 | sub _vrf_link_local { | ||
| 886 | my $subnet = shift; | ||
| 887 | |||
| 888 | return _vrf_within_range( $subnet, [ ipv6 => 'fe80:0:0:0:0:0:0:0', 10 ] ); | ||
| 889 | } | ||
| 890 | |||
| 891 | sub _vrf_within_range { | ||
| 892 | my ( $subnet, $range ) = @_; | ||
| 893 | |||
| 894 | my ( $family, $address, $length ) = @$range; | ||
| 895 | return 0 if $family ne $subnet->{family} or $subnet->{len} < $length; | ||
| 896 | my $bits = unpack 'B*', pack 'C*', | ||
| 897 | _vrf_ip_octets( $subnet->{family}, $subnet->{prefix} ); | ||
| 898 | my $range_bits = unpack 'B*', pack 'C*', | ||
| 899 | _vrf_ip_octets( $family, $address ); | ||
| 900 | return substr( $bits, 0, $length ) eq substr( $range_bits, 0, $length ) | ||
| 901 | ? 1 | ||
| 902 | : 0; | ||
| 903 | } | ||
| 904 | |||
| 905 | # Adds one source to the subnet; the first port seen keeps the record | ||
| 906 | sub _vrf_add_subnet { | ||
| 907 | my ( $subnets, $subnet, $source ) = @_; | ||
| 908 | |||
| 909 | my $key = "$subnet->{prefix}/$subnet->{len}"; | ||
| 910 | my $record = $subnets->{$key} ||= { | ||
| 911 | prefix => $subnet->{prefix}, | ||
| 912 | len => $subnet->{len}, | ||
| 913 | family => $subnet->{family}, | ||
| 914 | ifindex => $subnet->{ifindex}, | ||
| 915 | sources => [], | ||
| 916 | }; | ||
| 917 | $record->{ifindex} //= $subnet->{ifindex}; | ||
| 918 | push @{ $record->{sources} }, $source | ||
| 919 | unless grep { $_ eq $source } @{ $record->{sources} }; | ||
| 920 | return; | ||
| 921 | } | ||
| 922 | |||
| 923 | sub _vrf_address_subnet { | ||
| 924 | my $address = shift; | ||
| 925 | |||
| 926 | return unless defined $address->{prefix_len}; | ||
| 927 | my @octets = _vrf_ip_octets( $address->{family}, $address->{ip} ) | ||
| 928 | or return; | ||
| 929 | my $prefix | ||
| 930 | = _vrf_network( $address->{family}, \@octets, $address->{prefix_len} ) | ||
| 931 | or return; | ||
| 932 | |||
| 933 | return { | ||
| 934 | prefix => $prefix, | ||
| 935 | len => $address->{prefix_len}, | ||
| 936 | family => $address->{family}, | ||
| 937 | ifindex => $address->{ifindex}, | ||
| 938 | }; | ||
| 939 | } | ||
| 940 | |||
| 941 | # The octets of an address in the form vrf_addresses gives it | ||
| 942 | sub _vrf_ip_octets { | ||
| 943 | my ( $family, $ip ) = @_; | ||
| 944 | |||
| 945 | if ( $family eq 'ipv4' ) { | ||
| 946 | my @octets = split /\./, $ip; | ||
| 947 | return @octets == 4 ? @octets : (); | ||
| 948 | } | ||
| 949 | my @groups = split /:/, $ip; | ||
| 950 | return if @groups != 8; | ||
| 951 | return unpack 'C*', pack 'n8', map {hex} @groups; | ||
| 952 | } | ||
| 953 | |||
| 954 | # Zeroes the host bits and formats the result as vrf_addresses does | ||
| 955 | sub _vrf_network { | ||
| 956 | my ( $family, $octets, $length ) = @_; | ||
| 957 | |||
| 958 | my $bits = unpack 'B*', pack 'C*', @$octets; | ||
| 959 | return if $length > length $bits; | ||
| 960 | my $masked | ||
| 961 | = substr( $bits, 0, $length ) . ( '0' x ( length($bits) - $length ) ); | ||
| 962 | my @network = unpack 'C*', pack 'B*', $masked; | ||
| 963 | |||
| 964 | return join q{.}, @network if $family eq 'ipv4'; | ||
| 965 | return join q{:}, unpack '(H4)*', pack 'C*', @network; | ||
| 966 | } | ||
| 967 | |||
| 968 | # Connected routes of mplsL3VpnVrfRteTable whose index starts with $name | ||
| 969 | sub _vrf_attributed_route_subnets { | ||
| 970 | my $vrf = shift; | ||
| 971 | my $name = shift; | ||
| 972 | |||
| 973 | my $protos = $vrf->_vrf_column( 'vrf_l3vpn_rte_proto', $name ); | ||
| 974 | my $ifs = $vrf->_vrf_column( 'vrf_l3vpn_rte_if', $name ); | ||
| 975 | my @subnets; | ||
| 976 | foreach my $iid ( sort keys %$protos ) { | ||
| 977 | next unless _vrf_connected_proto( $protos->{$iid} ); | ||
| 978 | my ( $route_name, @arcs ) = $vrf->_vrf_decode_instance($iid); | ||
| 979 | next if !defined $route_name or $route_name ne $name; | ||
| 980 | my $subnet = _vrf_route_subnet(@arcs) or next; | ||
| 981 | $subnet->{ifindex} = _vrf_route_ifindex( $ifs->{$iid} ); | ||
| 982 | push @subnets, $subnet; | ||
| 983 | } | ||
| 984 | return \@subnets; | ||
| 985 | } | ||
| 986 | |||
| 987 | # Connected routes of inetCidrRouteTable, which has no name in its index | ||
| 988 | sub _vrf_context_route_subnets { | ||
| 989 | my $vrf = shift; | ||
| 990 | my $name = shift; | ||
| 991 | |||
| 992 | my $protos = $vrf->_vrf_column( 'vrf_cidr_proto', $name ); | ||
| 993 | my $ifs = $vrf->_vrf_column( 'vrf_cidr_if', $name ); | ||
| 994 | my $leaked = $vrf->_vrf_leaked_ports($name); | ||
| 995 | my @subnets; | ||
| 996 | foreach my $iid ( sort keys %$protos ) { | ||
| 997 | next unless $iid =~ /^\d+(?:\.\d+)*$/; | ||
| 998 | next unless _vrf_connected_proto( $protos->{$iid} ); | ||
| 999 | my $subnet = _vrf_route_subnet( split /\./, $iid ) or next; | ||
| 1000 | $subnet->{ifindex} = _vrf_route_ifindex( $ifs->{$iid} ); | ||
| 1001 | next | ||
| 1002 | if defined $subnet->{ifindex} | ||
| 1003 | and exists $leaked->{ $subnet->{ifindex} }; | ||
| 1004 | push @subnets, $subnet; | ||
| 1005 | } | ||
| 1006 | return \@subnets; | ||
| 1007 | } | ||
| 1008 | |||
| 1009 | # InterfaceIndexOrZero: 0 means the route has no interface | ||
| 1010 | sub _vrf_route_ifindex { | ||
| 1011 | my $ifindex = shift; | ||
| 1012 | |||
| 1013 | return ( defined $ifindex and $ifindex ne '0' ) ? $ifindex : undef; | ||
| 1014 | } | ||
| 1015 | |||
| 1016 | sub _vrf_connected_proto { | ||
| 1017 | my $proto = shift; | ||
| 1018 | |||
| 1019 | return ( defined $proto and $proto =~ /^(?:local|2)$/ ) ? 1 : 0; | ||
| 1020 | } | ||
| 1021 | |||
| 1022 | # Parses destType.destLen.dest.pfxLen.policyLen.policy.nhType.nhLen.nh by | ||
| 1023 | # the length octets. Gives nothing for a malformed index or a host route. | ||
| 1024 | sub _vrf_route_subnet { | ||
| 1025 | my @arcs = @_; | ||
| 1026 | |||
| 1027 | my ( $type, $dest_length ) = splice @arcs, 0, 2; | ||
| 1028 | my $shape = $INET_TYPE{ $type // q{} } or return; | ||
| 1029 | return if !defined $dest_length or $dest_length != $shape->{length}; | ||
| 1030 | return if @arcs < $dest_length + 2; | ||
| 1031 | |||
| 1032 | my @dest = splice @arcs, 0, $dest_length; | ||
| 1033 | my ( $length, $policy_length ) = splice @arcs, 0, 2; | ||
| 1034 | return if @arcs < $policy_length + 2; | ||
| 1035 | |||
| 1036 | splice @arcs, 0, $policy_length; | ||
| 1037 | my ( undef, $hop_length ) = splice @arcs, 0, 2; | ||
| 1038 | return if @arcs != $hop_length; | ||
| 1039 | return if $length >= $shape->{bytes} * 8; | ||
| 1040 | |||
| 1041 | my @octets = @dest[ 0 .. $shape->{bytes} - 1 ]; | ||
| 1042 | my $prefix = _vrf_network( $shape->{family}, \@octets, $length ) | ||
| 1043 | or return; | ||
| 1044 | return { | ||
| 1045 | prefix => $prefix, | ||
| 1046 | len => $length + 0, | ||
| 1047 | family => $shape->{family} | ||
| 1048 | }; | ||
| 1049 | } | ||
| 1050 | |||
| 1051 | # ipAddressTable, with ipAdEntTable standing in for IPv4 when the first | ||
| 1052 | # serves no IPv4 row; a vendor row stands in for a (port, address) the | ||
| 1053 | # standard tables lack. An unspecified address (0.0.0.0, ::) is no | ||
| 1054 | # interface address: VOSS serves 0.0.0.0 in ipAddrTable. | ||
| 1055 | sub _vrf_address_rows { | ||
| 1056 | my $vrf = shift; | ||
| 1057 | my $name = shift; | ||
| 1058 | |||
| 1059 | my $rows = $vrf->_vrf_inet_address_rows($name); | ||
| 1060 | $rows = { %$rows, %{ $vrf->_vrf_ipadent_rows($name) } } | ||
| 1061 | if !grep { $_->{family} eq 'ipv4' } values %$rows; | ||
| 1062 | $rows = { %{ $vrf->_vrf_vendor_address_rows($name) }, %$rows }; | ||
| 1063 | delete @$rows{ grep { _vrf_unspecified( $rows->{$_}{ip} ) } keys %$rows }; | ||
| 1064 | return $rows; | ||
| 1065 | } | ||
| 1066 | |||
| 1067 | sub _vrf_unspecified { | ||
| 1068 | my $ip = shift; | ||
| 1069 | |||
| 1070 | return ( $ip // q{} ) =~ /^[0.:]+$/ ? 1 : 0; | ||
| 1071 | } | ||
| 1072 | |||
| 1073 | # Index is type, length, address octets; types 3 and 4 end with four zone | ||
| 1074 | # octets that are not part of the address. Two rows on one port that differ | ||
| 1075 | # only by zone are one address: the row first in sorted index order is | ||
| 1076 | # kept, so the choice does not depend on hash order. | ||
| 1077 | sub _vrf_inet_address_rows { | ||
| 1078 | my $vrf = shift; | ||
| 1079 | my $name = shift; | ||
| 1080 | |||
| 1081 | my $ifindexes = $vrf->_vrf_column( 'new_ip_index', $name ); | ||
| 1082 | my $types = $vrf->_vrf_column( 'new_ip_type', $name ); | ||
| 1083 | my $prefixes = $vrf->_vrf_column( 'new_ip_prefix', $name ); | ||
| 1084 | |||
| 1085 | my %rows; | ||
| 1086 | foreach my $iid ( sort keys %$ifindexes ) { | ||
| 1087 | next unless $OWN_ADDRESS_TYPE{ $types->{$iid} // q{} }; | ||
| 1088 | my $address = _vrf_parse_address($iid) or next; | ||
| 1089 | my $key = "$ifindexes->{$iid}.$address->{ip}"; | ||
| 1090 | next if exists $rows{$key}; | ||
| 1091 | |||
| 1092 | $rows{$key} = { | ||
| 1093 | ifindex => $ifindexes->{$iid}, | ||
| 1094 | ip => $address->{ip}, | ||
| 1095 | prefix_len => scalar _vrf_pointer_length( $prefixes->{$iid} ), | ||
| 1096 | family => $address->{family}, | ||
| 1097 | }; | ||
| 1098 | } | ||
| 1099 | return \%rows; | ||
| 1100 | } | ||
| 1101 | |||
| 1102 | # { ip, family } from type.length.octets, or nothing for a shape the | ||
| 1103 | # address types do not allow | ||
| 1104 | sub _vrf_parse_address { | ||
| 1105 | my $iid = shift; | ||
| 1106 | |||
| 1107 | my ( $type, $length, @octets ) = split /\./, $iid; | ||
| 1108 | my $shape = $INET_TYPE{ $type // q{} } or return; | ||
| 1109 | return unless defined $length and $length == $shape->{length}; | ||
| 1110 | return unless @octets == $length; | ||
| 1111 | |||
| 1112 | my @address = @octets[ 0 .. $shape->{bytes} - 1 ]; | ||
| 1113 | return { | ||
| 1114 | family => $shape->{family}, | ||
| 1115 | ip => $shape->{family} eq 'ipv4' | ||
| 1116 | ? join( q{.}, @address ) | ||
| 1117 | : join( q{:}, unpack '(H4)*', pack 'C*', @address ), | ||
| 1118 | }; | ||
| 1119 | } | ||
| 1120 | |||
| 1121 | # The last arc of an ipAddressPrefix RowPointer is the prefix length; the | ||
| 1122 | # null pointer 0.0 says the device holds none | ||
| 1123 | sub _vrf_pointer_length { | ||
| 1124 | my $pointer = shift; | ||
| 1125 | |||
| 1126 | return if !defined $pointer or $pointer =~ /^(?:\.?0\.0|zeroDotZero)$/; | ||
| 1127 | return $pointer =~ /\.(\d+)$/ ? $1 + 0 : undef; | ||
| 1128 | } | ||
| 1129 | |||
| 1130 | sub _vrf_ipadent_rows { | ||
| 1131 | my $vrf = shift; | ||
| 1132 | my $name = shift; | ||
| 1133 | |||
| 1134 | my $ifindexes = $vrf->_vrf_column( 'old_ip_index', $name ); | ||
| 1135 | my $masks = $vrf->_vrf_column( 'old_ip_netmask', $name ); | ||
| 1136 | |||
| 1137 | my %rows; | ||
| 1138 | foreach my $ip ( keys %$ifindexes ) { | ||
| 1139 | $rows{"$ifindexes->{$ip}.$ip"} = { | ||
| 1140 | ifindex => $ifindexes->{$ip}, | ||
| 1141 | ip => $ip, | ||
| 1142 | prefix_len => scalar _vrf_mask_length( $masks->{$ip} ), | ||
| 1143 | family => 'ipv4', | ||
| 1144 | }; | ||
| 1145 | } | ||
| 1146 | return \%rows; | ||
| 1147 | } | ||
| 1148 | |||
| 1149 | # 255.255.255.0 is 24; a mask with a gap in it has no length | ||
| 1150 | sub _vrf_mask_length { | ||
| 1151 | my $mask = shift; | ||
| 1152 | |||
| 1153 | return unless defined $mask and $mask =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/; | ||
| 1154 | my $bits = unpack 'B32', pack 'C4', $1, $2, $3, $4; | ||
| 1155 | return $bits =~ /^(1*)0*$/ ? length $1 : undef; | ||
| 1156 | } | ||
| 1157 | |||
| 1158 | sub _vrf_is_listed { | ||
| 1159 | my $vrf = shift; | ||
| 1160 | my $name = shift; | ||
| 1161 | |||
| 1162 | my $instances = $vrf->vrf_instances() or return 0; | ||
| 1163 | return 1 if exists $instances->{$name}; | ||
| 1164 | |||
| 1165 | my $method = ( caller(1) )[3]; | ||
| 1166 | $vrf->error_throw( "$method() reads instance '$name', which is not in" | ||
| 1167 | . " vrf_instances() on this device. In attribution mode the" | ||
| 1168 | . " readers take only names the device lists; a name passed to" | ||
| 1169 | . " vrf_instances() is not one. Pass a key of vrf_instances().\n" | ||
| 1170 | ); | ||
| 1171 | return 0; | ||
| 1172 | } | ||
| 1173 | |||
| 1174 | # A method the class does not have contributes no rows. The read of $name | ||
| 1175 | # records the tables walked, which are @walked when the method is a sub | ||
| 1176 | # over other columns. | ||
| 1177 | sub _vrf_column { | ||
| 1178 | my ( $vrf, $method, $name, @walked ) = @_; | ||
| 1179 | |||
| 1180 | return {} unless $vrf->can($method); | ||
| 1181 | $vrf->_vrf_record_walk( $name, @walked ? @walked : $method ); | ||
| 1182 | return $vrf->$method() || {}; | ||
| 1183 | } | ||
| 1184 | |||
| 1185 | # clear_cache, and so update(), empties the record with the cache | ||
| 1186 | sub _vrf_record_walk { | ||
| 1187 | my ( $vrf, $name, @methods ) = @_; | ||
| 1188 | |||
| 1189 | $vrf->{_vrf_walked}{$name}{$_} = 1 for @methods; | ||
| 1190 | return; | ||
| 1191 | } | ||
| 1192 | |||
| 1193 | sub _vrf_arp_rows { | ||
| 1194 | my $vrf = shift; | ||
| 1195 | my $name = shift; | ||
| 1196 | |||
| 1197 | my $macs = $vrf->_vrf_column( 'at_paddr', $name, @ARP_PADDR_COLUMNS ); | ||
| 1198 | my $ips = $vrf->_vrf_column( 'at_netaddr', $name, @ARP_NETADDR_COLUMNS ); | ||
| 1199 | my %rows; | ||
| 1200 | foreach my $key ( keys %$macs ) { | ||
| 1201 | my ($ifindex) = $key =~ /^(\d+)\./ or next; | ||
| 1202 | next unless defined $ips->{$key} and _vrf_has_mac( $macs->{$key} ); | ||
| 1203 | $rows{$key} = { | ||
| 1204 | ifindex => $ifindex, | ||
| 1205 | ip => $ips->{$key}, | ||
| 1206 | mac => $macs->{$key}, | ||
| 1207 | family => 'ipv4', | ||
| 1208 | }; | ||
| 1209 | } | ||
| 1210 | return \%rows; | ||
| 1211 | } | ||
| 1212 | |||
| 1213 | sub _vrf_nd_rows { | ||
| 1214 | my $vrf = shift; | ||
| 1215 | my $name = shift; | ||
| 1216 | |||
| 1217 | my $macs = $vrf->_vrf_column( 'ipv6_n2p_mac', $name, @ND_COLUMNS ); | ||
| 1218 | my $ips = $vrf->_vrf_column( 'ipv6_n2p_addr', $name, @ND_COLUMNS ); | ||
| 1219 | my $ifs = $vrf->_vrf_column( 'ipv6_n2p_if', $name, @ND_COLUMNS ); | ||
| 1220 | my %rows; | ||
| 1221 | foreach my $key ( keys %$macs ) { | ||
| 1222 | next unless defined $ips->{$key} and defined $ifs->{$key}; | ||
| 1223 | next unless _vrf_has_mac( $macs->{$key} ); | ||
| 1224 | $rows{$key} = { | ||
| 1225 | ifindex => $ifs->{$key}, | ||
| 1226 | ip => $ips->{$key}, | ||
| 1227 | mac => $macs->{$key}, | ||
| 1228 | family => 'ipv6', | ||
| 1229 | }; | ||
| 1230 | } | ||
| 1231 | $vrf->_vrf_add_ipv6_mib_rows( \%rows, $name ) | ||
| 1232 | if $vrf->_vrf_nd_from_ipv6_mib(); | ||
| 1233 | return \%rows; | ||
| 1234 | } | ||
| 1235 | |||
| 1236 | # ipv6NetToMediaTable rows, indexed ifIndex then 16 address octets, for | ||
| 1237 | # the (port, address) pairs the IP-MIB rows lack | ||
| 1238 | sub _vrf_add_ipv6_mib_rows { | ||
| 1239 | my ( $vrf, $rows, $name ) = @_; | ||
| 1240 | |||
| 1241 | my %listed = map { "$_->{ifindex} " . lc $_->{ip} => 1 } values %$rows; | ||
| 1242 | my $macs = $vrf->_vrf_column( 'i6_n2p_phys_addr', $name ); | ||
| 1243 | foreach my $key ( keys %$macs ) { | ||
| 1244 | my ( $ifindex, @octets ) = split /\./, $key; | ||
| 1245 | next if @octets != 16 or grep { $_ > 255 } @octets; | ||
| 1246 | next unless _vrf_has_mac( $macs->{$key} ); | ||
| 1247 | my $ip = join q{:}, unpack '(H4)*', pack 'C*', @octets; | ||
| 1248 | next if $listed{"$ifindex $ip"}; | ||
| 1249 | $rows->{$key} = { | ||
| 1250 | ifindex => $ifindex, | ||
| 1251 | ip => $ip, | ||
| 1252 | mac => substr( $macs->{$key}, 0, 17 ), | ||
| 1253 | family => 'ipv6', | ||
| 1254 | }; | ||
| 1255 | } | ||
| 1256 | return; | ||
| 1257 | } | ||
| 1258 | |||
| 1259 | # IPV6-MIB ipv6AddrTable rows, indexed ifIndex then 16 address octets, | ||
| 1260 | # keyed and shaped as _vrf_address_rows keys them. ipv6AddrAnycastFlag is | ||
| 1261 | # not read: an anycast address is still the device's own, as in | ||
| 1262 | # ipAddressTable. | ||
| 1263 | sub _vrf_ipv6_mib_address_rows { | ||
| 1264 | my $vrf = shift; | ||
| 1265 | my $name = shift; | ||
| 1266 | |||
| 1267 | my $lengths = $vrf->_vrf_column( 'i6_addr_pfxlen', $name ); | ||
| 1268 | my %rows; | ||
| 1269 | foreach my $iid ( keys %$lengths ) { | ||
| 1270 | next unless $iid =~ /^\d+(?:\.\d+){16}$/; | ||
| 1271 | my ( $ifindex, @octets ) = split /\./, $iid; | ||
| 1272 | next if grep { $_ > 255 } @octets; | ||
| 1273 | my $ip = join q{:}, unpack '(H4)*', pack 'C*', @octets; | ||
| 1274 | my $length = $lengths->{$iid}; | ||
| 1275 | $rows{"$ifindex.$ip"} = { | ||
| 1276 | ifindex => $ifindex, | ||
| 1277 | ip => $ip, | ||
| 1278 | prefix_len => ( $length // q{} ) =~ /^\d+$/ ? $length + 0 : undef, | ||
| 1279 | family => 'ipv6', | ||
| 1280 | }; | ||
| 1281 | } | ||
| 1282 | return \%rows; | ||
| 1283 | } | ||
| 1284 | |||
| 1285 | # An undef, empty or all-zero MAC is no hardware address: the device | ||
| 1286 | # serves its own addresses that way. The broadcast MAC names no neighbor | ||
| 1287 | # either: a VOSS VRF context serves the subnet broadcast address at it. | ||
| 1288 | sub _vrf_has_mac { | ||
| 1289 | my $mac = shift; | ||
| 1290 | |||
| 1291 | return 0 unless defined $mac and $mac =~ /[^0:.\-]/; | ||
| 1292 | ( my $digits = lc $mac ) =~ s/[:.\-]//g; | ||
| 1293 | return $digits eq 'ffffffffffff' ? 0 : 1; | ||
| 1294 | } | ||
| 1295 | |||
| 1296 | sub _vrf_context_filters { return 0; } | ||
| 1297 | |||
| 1298 | # True where the default context also serves other instances' rows on their | ||
| 1299 | # ports, which only the default context's membership tables can name | ||
| 1300 | sub _vrf_default_context_leaks { return 0; } | ||
| 1301 | |||
| 1302 | # True where the default context serves only the default instance's rows, | ||
| 1303 | # yet i_vrf() there maps every instance's ports from the membership table | ||
| 1304 | sub _vrf_default_context_members { return 0; } | ||
| 1305 | |||
| 1306 | # True where a VRF context's name is its instance's name and the context | ||
| 1307 | # serves no VRF list, so the instance comes from the name alone | ||
| 1308 | sub _vrf_context_is_instance_name { return 0; } | ||
| 1309 | |||
| 1310 | # True where host link-locals are only in IPV6-MIB ipv6NetToMediaTable | ||
| 1311 | sub _vrf_nd_from_ipv6_mib { return 0; } | ||
| 1312 | |||
| 1313 | # { name => record } a vendor MIB lists where MPLS-L3VPN-STD-MIB serves | ||
| 1314 | # none; records come from _vrf_record with sources->{list} set. | ||
| 1315 | sub _vrf_vendor_instances { return {}; } | ||
| 1316 | |||
| 1317 | # The table methods _vrf_vendor_instances walks, for walk completeness | ||
| 1318 | sub _vrf_vendor_list_methods { return (); } | ||
| 1319 | |||
| 1320 | # { ifIndex => name } the vendor MIB holds, used only when the vendor list | ||
| 1321 | # was used; the tables it walks are named by _vrf_vendor_membership_methods | ||
| 1322 | sub _vrf_vendor_membership { return {}; } | ||
| 1323 | |||
| 1324 | sub _vrf_vendor_membership_methods { | ||
| 1325 | my $vrf = shift; | ||
| 1326 | |||
| 1327 | return $vrf->_vrf_vendor_listed() ? $vrf->_vrf_vendor_list_methods() : (); | ||
| 1328 | } | ||
| 1329 | |||
| 1330 | # Address rows from a vendor MIB, keyed and shaped as _vrf_address_rows | ||
| 1331 | # keys them (ifIndex.address). Walk them with _vrf_column so they count | ||
| 1332 | # toward vrf_read_complete(). | ||
| 1333 | sub _vrf_vendor_address_rows { return {}; } | ||
| 1334 | |||
| 1335 | sub _vrf_subnets_from_routes { return 0; } | ||
| 1336 | |||
| 1337 | # True where a context serves the whole address table of the device, so | ||
| 1338 | # the ports of the context are those its routes use | ||
| 1339 | sub _vrf_context_ports_from_routes { return 0; } | ||
| 1340 | |||
| 1341 | sub _vrf_internal_instance { return 0; } | ||
| 1342 | |||
| 1343 | # The VRF context this session reads: the SNMPv3 Context or community | ||
| 1344 | # suffix _walk_context() finds, else, over SNMPv1 or SNMPv2c only, the | ||
| 1345 | # context the device maps the session's community to. A class whose device | ||
| 1346 | # ignores a community suffix has the suffix skipped. '' is the default | ||
| 1347 | # context, including when the device cannot say. | ||
| 1348 | sub _vrf_session_context { | ||
| 1349 | my $vrf = shift; | ||
| 1350 | |||
| 1351 | my $version = ( $vrf->session() || {} )->{Version}; | ||
| 1352 | my $is_v3 = defined $version && $version eq '3'; | ||
| 1353 | if ( $is_v3 or $vrf->_vrf_community_suffix_is_context() ) { | ||
| 1354 | my $context = $vrf->_walk_context(); | ||
| 1355 | return $context if length $context; | ||
| 1356 | } | ||
| 1357 | return '' if $is_v3; | ||
| 1358 | |||
| 1359 | my $mapped = $vrf->_vrf_community_context(); | ||
| 1360 | return defined $mapped ? $mapped : ''; | ||
| 1361 | } | ||
| 1362 | |||
| 1363 | # True where the device serves a context for a community suffix (X@base). | ||
| 1364 | # A class whose device ignores the suffix returns 0. | ||
| 1365 | sub _vrf_community_suffix_is_context { return 1; } | ||
| 1366 | |||
| 1367 | # The context the device maps the session's community to: a name, '' for | ||
| 1368 | # the default context, or undef when the class cannot tell. A class whose | ||
| 1369 | # device maps communities to contexts overrides this. | ||
| 1370 | sub _vrf_community_context { return; } | ||
| 1371 | |||
| 1372 | # Only the default context serves the whole VRF list | ||
| 1373 | sub _vrf_default_context_ok { | ||
| 1374 | my $vrf = shift; | ||
| 1375 | |||
| 1376 | my $context = $vrf->_vrf_session_context(); | ||
| 1377 | return 1 unless length $context; | ||
| 1378 | |||
| 1379 | my $method = ( caller(1) )[3]; | ||
| 1380 | $vrf->error_throw( | ||
| 1381 | "$method() reads the VRF list, which only the default context" | ||
| 1382 | . " serves whole. This session is in context '$context'. Call" | ||
| 1383 | . " update() back to the default context, read the list, and" | ||
| 1384 | . " keep it while you switch contexts.\n" ); | ||
| 1385 | return 0; | ||
| 1386 | } | ||
| 1387 | |||
| 1388 | # Attribution mode reads the default context and sorts rows by port. Context | ||
| 1389 | # mode reads one instance per context, so the default context only serves | ||
| 1390 | # the instance named default. | ||
| 1391 | sub _vrf_in_reader_context { | ||
| 1392 | my $vrf = shift; | ||
| 1393 | my $name = shift; | ||
| 1394 | |||
| 1395 | my $context = $vrf->_vrf_session_context(); | ||
| 1396 | my $method = ( caller(1) )[3]; | ||
| 1397 | return $vrf->_vrf_in_own_context( $method, $name, $context ) | ||
| 1398 | if $vrf->_vrf_context_filters(); | ||
| 1399 | |||
| 1400 | return 1 unless length $context; | ||
| 1401 | $vrf->error_throw( "$method() reads instance '$name' from the default" | ||
| 1402 | . " context, attributing rows by port, on this device." | ||
| 1403 | . " This session is in context '$context'. Call update()" | ||
| 1404 | . " back to the default context first.\n" ); | ||
| 1405 | return 0; | ||
| 1406 | } | ||
| 1407 | |||
| 1408 | # Context mode: each instance reads only in its own context, which for | ||
| 1409 | # default is the default context | ||
| 1410 | sub _vrf_in_own_context { | ||
| 1411 | my ( $vrf, $method, $name, $context ) = @_; | ||
| 1412 | |||
| 1413 | return 1 if $name eq 'default' and !length $context; | ||
| 1414 | if ( $name eq 'default' ) { | ||
| 1415 | $vrf->error_throw( "$method() reads instance 'default' from the" | ||
| 1416 | . " default context on this device. This session is in" | ||
| 1417 | . " context '$context'. Call update() back to the default" | ||
| 1418 | . " context first.\n" ); | ||
| 1419 | return 0; | ||
| 1420 | } | ||
| 1421 | if ( !length $context ) { | ||
| 1422 | $vrf->error_throw( | ||
| 1423 | "$method() reads instance '$name' from its own context on" | ||
| 1424 | . " this device, but this session is in the default context." | ||
| 1425 | . " Switch with snmp_context('vrf', '$name') first.\n" ); | ||
| 1426 | return 0; | ||
| 1427 | } | ||
| 1428 | |||
| 1429 | my @contexts = $vrf->_vrf_contexts_of($name); | ||
| 1430 | return 1 if grep { $_ eq $context } @contexts; | ||
| 1431 | if ( !@contexts ) { | ||
| 1432 | $vrf->error_throw( "$method() cannot confirm that context" | ||
| 1433 | . " '$context' belongs to instance '$name': the instance list" | ||
| 1434 | . " this session reads names no context for '$name'. Read" | ||
| 1435 | . " vrf_instances() in the default context to check the" | ||
| 1436 | . " name, and use the context snmp_context('vrf', '$name')" | ||
| 1437 | . " gives there.\n" ); | ||
| 1438 | return 0; | ||
| 1439 | } | ||
| 1440 | $vrf->error_throw( "$method() reads instance '$name' from its own" | ||
| 1441 | . " context, but this session is in context '$context'." | ||
| 1442 | . " Switch with snmp_context('vrf', '$name') first.\n" ); | ||
| 1443 | return 0; | ||
| 1444 | } | ||
| 1445 | |||
| 1446 | # A port the membership table does not list belongs to the global table; | ||
| 1447 | # with no membership hash the owner is unknown | ||
| 1448 | sub _vrf_owner_of { | ||
| 1449 | my ( $members, $ifindex ) = @_; | ||
| 1450 | |||
| 1451 | return unless $members; | ||
| 1452 | return exists $members->{$ifindex} ? $members->{$ifindex} : 'default'; | ||
| 1453 | } | ||
| 1454 | |||
| 1455 | 1; | ||
| 1456 | |||
| 1457 | __END__ |