| 1 | package App::Netdisco::Worker::Plugin::Macsuck::Nodes; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use App::Netdisco::Worker::Plugin; | ||
| 5 | use aliased 'App::Netdisco::Worker::Status'; | ||
| 6 | |||
| 7 | use App::Netdisco::Transport::SSH (); | ||
| 8 | use App::Netdisco::Transport::SNMP (); | ||
| 9 | use App::Netdisco::Util::Permission 'acl_matches'; | ||
| 10 | use App::Netdisco::Util::PortMAC 'get_port_macs'; | ||
| 11 | use App::Netdisco::Util::Device 'match_to_setting'; | ||
| 12 | use App::Netdisco::Util::Node 'check_mac'; | ||
| 13 | use App::Netdisco::Util::SNMP 'snmp_comm_reindex'; | ||
| 14 | use App::Netdisco::Util::Web 'sort_port'; | ||
| 15 | use App::Netdisco::Util::SNMPText 'decode_snmp_text'; | ||
| 16 | |||
| 17 | use Dancer::Plugin::DBIC 'schema'; | ||
| 18 | use Time::HiRes 'gettimeofday'; | ||
| 19 | use Scope::Guard 'guard'; | ||
| 20 | use Regexp::Common 'net'; | ||
| 21 | use NetAddr::MAC (); | ||
| 22 | use List::MoreUtils (); | ||
| 23 | |||
| 24 | register_worker({ phase => 'early', | ||
| 25 | title => 'prepare common data' }, sub { | ||
| 26 | |||
| 27 | my ($job, $workerconf) = @_; | ||
| 28 | my $device = $job->device; | ||
| 29 | |||
| 30 | # would be possible just to use LOCALTIMESTAMP on updated records, but by using this | ||
| 31 | # same value for them all, we can if we want add a job at the end to | ||
| 32 | # select and do something with the updated set (see set archive, below) | ||
| 33 | vars->{'timestamp'} = ($job->is_offline and $job->entered) | ||
| 34 | ? (schema('netdisco')->storage->dbh->quote($job->entered) .'::timestamp') | ||
| 35 | : 'to_timestamp('. (join '.', gettimeofday) .')::timestamp'; | ||
| 36 | |||
| 37 | # initialise the cache | ||
| 38 | vars->{'fwtable'} = {}; | ||
| 39 | # VLAN names the SNMP worker read, for the store phase to filter by | ||
| 40 | vars->{'macsuck_vlan_names'} = undef; | ||
| 41 | |||
| 42 | # cache the device ports to save hitting the database for many single rows | ||
| 43 | vars->{'device_ports'} = {map {($_->port => $_)} | ||
| 44 | $device->ports(undef, {prefetch => ['properties', | ||
| 45 | {neighbor_alias => 'device'}]})->all}; | ||
| 46 | }); | ||
| 47 | |||
| 48 | register_worker({ phase => 'main', driver => 'direct', | ||
| 49 | title => 'gather macs from file' }, sub { | ||
| 50 | |||
| 51 | my ($job, $workerconf) = @_; | ||
| 52 | my $device = $job->device; | ||
| 53 | |||
| 54 | return Status->info('skip: fwtable data supplied by other source') | ||
| 55 | unless $job->is_offline; | ||
| 56 | |||
| 57 | # load cache from file or copy from job param | ||
| 58 | my $data = $job->extra; | ||
| 59 | my @fwtable = (length $data ? @{ from_json($data) } : ()); | ||
| 60 | |||
| 61 | return $job->cancel('data provided but 0 fwd entries found') | ||
| 62 | unless scalar @fwtable; | ||
| 63 | |||
| 64 | debug sprintf ' [%s] macsuck - %s forwarding table entries provided', | ||
| 65 | $device->ip, scalar @fwtable; | ||
| 66 | |||
| 67 | # rebuild fwtable in format for filtering more easily | ||
| 68 | foreach my $node (@fwtable) { | ||
| 69 | my $mac = NetAddr::MAC->new(mac => ($node->{'mac'} || '')); | ||
| 70 | next unless $node->{'port'} and $mac; | ||
| 71 | next if (($mac->as_ieee eq '00:00:00:00:00:00') or ($mac->as_ieee !~ m{^$RE{net}{MAC}$}i)); | ||
| 72 | |||
| 73 | vars->{'fwtable'}->{ $node->{'vlan'} || 0 } | ||
| 74 | ->{ $node->{'port'} } | ||
| 75 | ->{ $mac->as_ieee } += 1; | ||
| 76 | } | ||
| 77 | |||
| 78 | return Status->done("Received MAC addresses for $device"); | ||
| 79 | }); | ||
| 80 | |||
| 81 | register_worker({ phase => 'main', driver => 'cli', | ||
| 82 | title => 'gather macs from CLI'}, sub { | ||
| 83 | |||
| 84 | my ($job, $workerconf) = @_; | ||
| 85 | my $device = $job->device; | ||
| 86 | |||
| 87 | my $cli = App::Netdisco::Transport::SSH->session_for($device) | ||
| 88 | or return Status->defer("macsuck failed: could not SSH connect to $device"); | ||
| 89 | |||
| 90 | # Retrieve data through SSH connection | ||
| 91 | my $macs = $cli->macsuck; | ||
| 92 | |||
| 93 | my $nodecount = 0; | ||
| 94 | foreach my $vlan (keys %{ $macs }) { | ||
| 95 | foreach my $port (keys %{ $macs->{$vlan} }) { | ||
| 96 | foreach my $node (keys %{ $macs->{$vlan}->{$port} }) { | ||
| 97 | vars->{'fwtable'}->{ $vlan } | ||
| 98 | ->{ $port } | ||
| 99 | ->{ $node } = $macs->{$vlan}->{$port}->{$node}; | ||
| 100 | $nodecount += 1; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | debug sprintf ' [%s] macsuck (ssh) - %s forwarding table entries provided', | ||
| 106 | $device->ip, $nodecount; | ||
| 107 | |||
| 108 | return Status->done("Gathered MAC addresses for $device"); | ||
| 109 | }); | ||
| 110 | |||
| 111 | register_worker({ phase => 'main', driver => 'snmp', | ||
| 112 | title => 'gather macs from snmp'}, sub { | ||
| 113 | |||
| 114 | my ($job, $workerconf) = @_; | ||
| 115 | my $device = $job->device; | ||
| 116 | |||
| 117 | my $snmp = App::Netdisco::Transport::SNMP->reader_for($device) | ||
| 118 | or return Status->defer("macsuck failed: could not SNMP connect to $device"); | ||
| 119 | |||
| 120 | # get forwarding table data via basic snmp connection | ||
| 121 | my $interfaces = $snmp->interfaces || {}; | ||
| 122 | my $macs = walk_fwtable($snmp, $device, $interfaces); | ||
| 123 | |||
| 124 | foreach my $vlan (keys %{ $macs }) { | ||
| 125 | foreach my $port (keys %{ $macs->{$vlan} }) { | ||
| 126 | foreach my $node (keys %{ $macs->{$vlan}->{$port} }) { | ||
| 127 | vars->{'fwtable'}->{ $vlan } | ||
| 128 | ->{ $port } | ||
| 129 | ->{ $node } = $macs->{$vlan}->{$port}->{$node}; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | # ...then per-vlan if supported | ||
| 135 | # this will duplicate call sanity_vlans (same as store) but helps efficiency | ||
| 136 | my @vlan_list = get_vlan_list($snmp, $device); | ||
| 137 | { | ||
| 138 | my $guard = guard { snmp_comm_reindex($snmp, $device, 0) }; | ||
| 139 | foreach my $vlan (@vlan_list) { | ||
| 140 | snmp_comm_reindex($snmp, $device, $vlan); | ||
| 141 | my $pv_fwtable = | ||
| 142 | walk_fwtable($snmp, $device, $interfaces, $vlan); | ||
| 143 | |||
| 144 | foreach my $vlan (keys %{ $pv_fwtable }) { | ||
| 145 | foreach my $port (keys %{ $pv_fwtable->{$vlan} }) { | ||
| 146 | foreach my $node (keys %{ $pv_fwtable->{$vlan}->{$port} }) { | ||
| 147 | vars->{'fwtable'}->{ $vlan } | ||
| 148 | ->{ $port } | ||
| 149 | ->{ $node } = $pv_fwtable->{$vlan}->{$port}->{$node}; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | return Status->done("Gathered MAC addresses for $device"); | ||
| 157 | }); | ||
| 158 | |||
| 159 | register_worker({ phase => 'store', | ||
| 160 | title => 'save macs to database'}, sub { | ||
| 161 | |||
| 162 | my ($job, $workerconf) = @_; | ||
| 163 | my $device = $job->device; | ||
| 164 | |||
| 165 | # remove macs on forbidden vlans | ||
| 166 | my @vlans = (0, sanity_vlans($device, vars->{'fwtable'}, | ||
| 167 | vars->{'macsuck_vlan_names'}, {})); | ||
| 168 | foreach my $vlan (keys %{ vars->{'fwtable'} }) { | ||
| 169 | delete vars->{'fwtable'}->{$vlan} | ||
| 170 | unless scalar grep {$_ eq $vlan} @vlans; | ||
| 171 | } | ||
| 172 | |||
| 173 | # sanity filter the MAC addresses from the device | ||
| 174 | vars->{'fwtable'} = sanity_macs( $device, vars->{'fwtable'}, vars->{'device_ports'} ); | ||
| 175 | |||
| 176 | # reverse sort allows vlan 0 entries to be included only as fallback | ||
| 177 | my $node_count = 0; | ||
| 178 | foreach my $vlan (reverse sort keys %{ vars->{'fwtable'} }) { | ||
| 179 | foreach my $port (keys %{ vars->{'fwtable'}->{$vlan} }) { | ||
| 180 | my $vlabel = ($vlan ? $vlan : 'unknown'); | ||
| 181 | debug sprintf ' [%s] macsuck - port %s vlan %s : %s nodes', | ||
| 182 | $device->ip, $port, $vlabel, scalar keys %{ vars->{'fwtable'}->{$vlan}->{$port} }; | ||
| 183 | |||
| 184 | foreach my $mac (keys %{ vars->{'fwtable'}->{$vlan}->{$port} }) { | ||
| 185 | |||
| 186 | # remove vlan 0 entry for this MAC addr | ||
| 187 | delete vars->{'fwtable'}->{0}->{$_}->{$mac} | ||
| 188 | for keys %{ vars->{'fwtable'}->{0} }; | ||
| 189 | |||
| 190 | store_node($device->ip, $vlan, $port, $mac, vars->{'timestamp'}); | ||
| 191 | ++$node_count; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | debug sprintf ' [%s] macsuck - stored %s forwarding table entries', | ||
| 197 | $device->ip, $node_count; | ||
| 198 | |||
| 199 | # a use for $now ... need to archive disappeared nodes | ||
| 200 | my $now = vars->{'timestamp'}; | ||
| 201 | my $archived = archive_stale_nodes($device, $now); | ||
| 202 | |||
| 203 | debug sprintf ' [%s] macsuck - removed %d fwd table entries to archive', | ||
| 204 | $device->ip, $archived; | ||
| 205 | |||
| 206 | my $status = $job->best_status; | ||
| 207 | if (Status->$status->level == Status->done->level) { | ||
| 208 | $device->update({last_macsuck => \$now}); | ||
| 209 | } | ||
| 210 | |||
| 211 | return Status->$status("Ended macsuck for $device"); | ||
| 212 | }); | ||
| 213 | |||
| 214 | |||
| 215 | # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 216 | |||
| 217 | =head2 store_node( $ip, $vlan, $port, $mac, $now? ) | ||
| 218 | |||
| 219 | Writes a fresh entry to the Netdisco C<node> database table. Will mark old | ||
| 220 | entries for this data as no longer C<active>. | ||
| 221 | |||
| 222 | All four fields in the tuple are required. If you don't know the VLAN ID, | ||
| 223 | Netdisco supports using ID "0". | ||
| 224 | |||
| 225 | Optionally, a fifth argument can be the literal string passed to the time_last | ||
| 226 | field of the database record. If not provided, it defaults to C<LOCALTIMESTAMP>. | ||
| 227 | |||
| 228 | =cut | ||
| 229 | |||
| 230 | sub archive_stale_nodes { | ||
| 231 | my ($device, $now) = @_; | ||
| 232 | return 0 unless setting('node_freshness'); | ||
| 233 | |||
| 234 | return schema('netdisco')->resultset('Node')->search({ | ||
| 235 | switch => $device->ip, | ||
| 236 | -bool => 'active', | ||
| 237 | time_last => \[ "< ($now - ?::interval)", | ||
| 238 | setting('node_freshness') .' minutes' ], | ||
| 239 | })->update({ active => \'false' }); | ||
| 240 | } | ||
| 241 | |||
| 242 | sub store_node { | ||
| 243 | my ($ip, $vlan, $port, $mac, $now) = @_; | ||
| 244 | $now ||= 'LOCALTIMESTAMP'; | ||
| 245 | $vlan ||= 0; | ||
| 246 | |||
| 247 | # ideally we just store the first 36 bits of the mac in the oui field | ||
| 248 | # and then no need for this query. haven't yet worked out the SQL for that. | ||
| 249 | my $oui = schema('netdisco')->resultset('Manufacturer') | ||
| 250 | ->search({ range => { '@>' => | ||
| 251 | \[q{('x' || lpad( translate( ? ::text, ':', ''), 16, '0')) ::bit(64) ::bigint}, $mac]} }, | ||
| 252 | { rows => 1, columns => 'base' })->first; | ||
| 253 | |||
| 254 | schema('netdisco')->txn_do(sub { | ||
| 255 | my $nodes = schema('netdisco')->resultset('Node'); | ||
| 256 | |||
| 257 | my $old = $nodes->search( | ||
| 258 | { mac => $mac, | ||
| 259 | # where vlan is unknown, need to archive on all other vlans | ||
| 260 | ($vlan ? (vlan => $vlan) : ()), | ||
| 261 | -bool => 'active', | ||
| 262 | -not => { | ||
| 263 | switch => $ip, | ||
| 264 | port => $port, | ||
| 265 | }, | ||
| 266 | })->update( { active => \'false' } ); | ||
| 267 | |||
| 268 | # new data | ||
| 269 | my $row = $nodes->update_or_new( | ||
| 270 | { | ||
| 271 | switch => $ip, | ||
| 272 | port => $port, | ||
| 273 | vlan => $vlan, | ||
| 274 | mac => $mac, | ||
| 275 | active => \'true', | ||
| 276 | oui => ($oui ? $oui->base : undef), | ||
| 277 | time_last => \$now, | ||
| 278 | (($old != 0) ? (time_recent => \$now) : ()), | ||
| 279 | }, | ||
| 280 | { | ||
| 281 | key => 'primary', | ||
| 282 | for => 'update', | ||
| 283 | } | ||
| 284 | ); | ||
| 285 | |||
| 286 | if (! $row->in_storage) { | ||
| 287 | $row->set_column(time_first => \$now); | ||
| 288 | $row->insert; | ||
| 289 | } | ||
| 290 | }); | ||
| 291 | } | ||
| 292 | |||
| 293 | # return a list of vlan numbers which are OK to macsuck on this device | ||
| 294 | sub get_vlan_list { | ||
| 295 | my ($snmp, $device) = @_; | ||
| 296 | return () unless $snmp->cisco_comm_indexing; | ||
| 297 | |||
| 298 | my (%vlans, %vlan_names, %vlan_states); | ||
| 299 | my $i_vlan = $snmp->i_vlan || {}; | ||
| 300 | my $trunks = $snmp->i_vlan_membership || {}; | ||
| 301 | my $i_type = $snmp->i_type || {}; | ||
| 302 | my $auth_vlans = $snmp->can('i_auth_vlan_membership') | ||
| 303 | ? ($snmp->i_auth_vlan_membership || {}) | ||
| 304 | : {}; | ||
| 305 | |||
| 306 | # get list of vlans in use | ||
| 307 | while (my ($idx, $vlan) = each %$i_vlan) { | ||
| 308 | # hack: if vlan id comes as 1.142 instead of 142 | ||
| 309 | $vlan =~ s/^\d+\.//; | ||
| 310 | |||
| 311 | # VLANs are ports interfaces capture VLAN, but don't count as in use | ||
| 312 | # Port channels are also 'propVirtual', but capture while checking | ||
| 313 | # trunk VLANs below | ||
| 314 | if (exists $i_type->{$idx} and $i_type->{$idx} eq 'propVirtual') { | ||
| 315 | $vlans{$vlan} ||= 0; | ||
| 316 | } | ||
| 317 | else { | ||
| 318 | ++$vlans{$vlan}; | ||
| 319 | } | ||
| 320 | foreach my $t_vlan (@{$trunks->{$idx}}) { | ||
| 321 | ++$vlans{$t_vlan}; | ||
| 322 | } | ||
| 323 | } | ||
| 324 | |||
| 325 | # Authentication frameworks such as Cisco ISE can assign VLANs | ||
| 326 | # dynamically to individual authentication sessions. These VLANs may | ||
| 327 | # not be reflected by the configured interface VLAN membership. | ||
| 328 | while (my ($idx, $memberships) = each %$auth_vlans) { | ||
| 329 | foreach my $vlan (@{$memberships || []}) { | ||
| 330 | ++$vlans{$vlan}; | ||
| 331 | } | ||
| 332 | } | ||
| 333 | |||
| 334 | unless (scalar keys %vlans) { | ||
| 335 | debug sprintf ' [%s] macsuck - no VLANs found.', $device->ip; | ||
| 336 | return (); | ||
| 337 | } | ||
| 338 | |||
| 339 | my $v_name = $snmp->v_name || {}; | ||
| 340 | |||
| 341 | # get vlan names (required for config which filters by name) | ||
| 342 | while (my ($idx, $name) = each %$v_name) { | ||
| 343 | # hack: if vlan id comes as 1.142 instead of 142 | ||
| 344 | (my $vlan = $idx) =~ s/^\d+\.//; | ||
| 345 | |||
| 346 | # just in case i_vlan is different to v_name set | ||
| 347 | # capture the VLAN, but it's not in use on a port | ||
| 348 | $vlans{$vlan} ||= 0; | ||
| 349 | |||
| 350 | $vlan_names{$vlan} = decode_snmp_text($name, $device, "vlan $vlan name"); | ||
| 351 | } | ||
| 352 | |||
| 353 | debug sprintf ' [%s] macsuck - VLANs: %s', $device->ip, | ||
| 354 | (join ',', sort grep {$_} keys %vlans); | ||
| 355 | |||
| 356 | my $v_state = $snmp->v_state || {}; | ||
| 357 | |||
| 358 | # get vlan states (required for ignoring suspended vlans) | ||
| 359 | while (my ($idx, $state) = each %$v_state) { | ||
| 360 | # hack: if vlan id comes as 1.142 instead of 142 | ||
| 361 | (my $vlan = $idx) =~ s/^\d+\.//; | ||
| 362 | |||
| 363 | # just in case i_vlan is different to v_name set | ||
| 364 | # capture the VLAN, but it's not in use on a port | ||
| 365 | $vlans{$vlan} ||= 0; | ||
| 366 | |||
| 367 | $vlan_states{$vlan} = $state; | ||
| 368 | } | ||
| 369 | |||
| 370 | # the store phase filters the VLANs again, and needs the names for it | ||
| 371 | vars->{'macsuck_vlan_names'} = { %vlan_names }; | ||
| 372 | |||
| 373 | return sanity_vlans($device, \%vlans, \%vlan_names, \%vlan_states); | ||
| 374 | } | ||
| 375 | |||
| 376 | sub sanity_vlans { | ||
| 377 | my ($device, $vlans, $vlan_names, $vlan_states) = @_; | ||
| 378 | |||
| 379 | # names are undef when no worker read them (CLI, offline, non-Cisco): | ||
| 380 | # macsuck_no_unnamed then has nothing to go on and every VLAN would look | ||
| 381 | # unnamed, so it is not applied | ||
| 382 | my $have_names = defined $vlan_names; | ||
| 383 | $vlan_names ||= {}; | ||
| 384 | |||
| 385 | my @ok_vlans = (); | ||
| 386 | foreach my $vlan (sort keys %$vlans) { | ||
| 387 | my $name = $vlan_names->{$vlan} || '(unnamed)'; | ||
| 388 | my $state = $vlan_states->{$vlan} || '(unknown)'; | ||
| 389 | |||
| 390 | if (ref [] eq ref setting('macsuck_no_vlan')) { | ||
| 391 | my $ignore = setting('macsuck_no_vlan'); | ||
| 392 | |||
| 393 | if ((scalar grep {$_ eq $vlan} @$ignore) or | ||
| 394 | (scalar grep {$_ eq $name} @$ignore)) { | ||
| 395 | |||
| 396 | debug sprintf | ||
| 397 | ' [%s] macsuck VLAN %s - skipped by macsuck_no_vlan config', | ||
| 398 | $device->ip, $vlan; | ||
| 399 | next; | ||
| 400 | } | ||
| 401 | } | ||
| 402 | |||
| 403 | if (ref [] eq ref setting('macsuck_no_devicevlan')) { | ||
| 404 | my $ignore = setting('macsuck_no_devicevlan'); | ||
| 405 | my $ip = $device->ip; | ||
| 406 | |||
| 407 | if ((scalar grep {$_ eq "$ip:$vlan"} @$ignore) or | ||
| 408 | (scalar grep {$_ eq "$ip:$name"} @$ignore)) { | ||
| 409 | |||
| 410 | debug sprintf | ||
| 411 | ' [%s] macsuck VLAN %s - skipped by macsuck_no_devicevlan config', | ||
| 412 | $device->ip, $vlan; | ||
| 413 | next; | ||
| 414 | } | ||
| 415 | } | ||
| 416 | |||
| 417 | if (setting('macsuck_no_unnamed') and $have_names | ||
| 418 | and $name eq '(unnamed)') { | ||
| 419 | debug sprintf | ||
| 420 | ' [%s] macsuck VLAN %s - skipped by macsuck_no_unnamed config', | ||
| 421 | $device->ip, $vlan; | ||
| 422 | next; | ||
| 423 | } | ||
| 424 | |||
| 425 | if ($vlan > 4094) { | ||
| 426 | debug sprintf ' [%s] macsuck - invalid VLAN number %s', | ||
| 427 | $device->ip, $vlan; | ||
| 428 | next; | ||
| 429 | } | ||
| 430 | next if $vlan == 0; # quietly skip | ||
| 431 | |||
| 432 | # check in use by a port on this device | ||
| 433 | if (not $vlans->{$vlan} and not setting('macsuck_all_vlans')) { | ||
| 434 | debug sprintf | ||
| 435 | ' [%s] macsuck VLAN %s/%s - not in use by any port - skipping.', | ||
| 436 | $device->ip, $vlan, $name; | ||
| 437 | next; | ||
| 438 | } | ||
| 439 | |||
| 440 | # check if vlan is in state 'suspended' | ||
| 441 | if ($state eq 'suspended') { | ||
| 442 | debug sprintf | ||
| 443 | ' [%s] macsuck VLAN %s - VLAN is suspended - skipping.', | ||
| 444 | $device->ip, $vlan; | ||
| 445 | next; | ||
| 446 | } | ||
| 447 | |||
| 448 | push @ok_vlans, $vlan; | ||
| 449 | } | ||
| 450 | |||
| 451 | return @ok_vlans; | ||
| 452 | } | ||
| 453 | |||
| 454 | # walks the forwarding table (BRIDGE-MIB) for the device and returns a | ||
| 455 | # table of node entries. | ||
| 456 | sub walk_fwtable { | ||
| 457 | my ($snmp, $device, $interfaces, $comm_vlan) = @_; | ||
| 458 | my $cache = {}; | ||
| 459 | |||
| 460 | my $fw_mac = $snmp->fw_mac || {}; | ||
| 461 | my $fw_port = $snmp->fw_port || {}; | ||
| 462 | my $fw_vlan = ($snmp->can('cisco_comm_indexing') and $snmp->cisco_comm_indexing()) | ||
| 463 | ? {} : $snmp->qb_fw_vlan; | ||
| 464 | my $bp_index = $snmp->bp_index || {}; | ||
| 465 | |||
| 466 | # to map forwarding table port to device port we have | ||
| 467 | # fw_port -> bp_index -> interfaces | ||
| 468 | |||
| 469 | MAC: while (my ($idx, $mac) = each %$fw_mac) { | ||
| 470 | my $bp_id = $fw_port->{$idx}; | ||
| 471 | next unless defined $mac; | ||
| 472 | |||
| 473 | # SNMP glitch: some macs have a mis-encoded MacAddress in the OID. | ||
| 474 | # The MacAddress in the OID is required to be 6 bytes long | ||
| 475 | # if it's 7 bytes with a leading 0x06 then it's pretty obviously mis-encoded | ||
| 476 | if ($mac =~ m{^06:($RE{net}{MAC})$}i) { | ||
| 477 | $mac = $1; | ||
| 478 | } | ||
| 479 | |||
| 480 | unless (defined $bp_id) { | ||
| 481 | debug sprintf | ||
| 482 | ' [%s] macsuck %s - %s has no fw_port mapping - skipping.', | ||
| 483 | $device->ip, $mac, $idx; | ||
| 484 | next MAC; | ||
| 485 | } | ||
| 486 | |||
| 487 | my $iid = $bp_index->{$bp_id}; | ||
| 488 | my $vlan = $fw_vlan->{$idx} || $comm_vlan || '0'; | ||
| 489 | |||
| 490 | unless (defined $iid) { | ||
| 491 | debug sprintf | ||
| 492 | ' [%s] macsuck %s - port %s has no bp_index mapping - skipping.', | ||
| 493 | $device->ip, $mac, $bp_id; | ||
| 494 | next MAC; | ||
| 495 | } | ||
| 496 | |||
| 497 | # WRT #475 this is SAFE because we check against known ports below | ||
| 498 | # but we do need the SNMP interface IDs to get the job done | ||
| 499 | my $port = $interfaces->{$iid}; | ||
| 500 | |||
| 501 | unless (defined $port) { | ||
| 502 | debug sprintf | ||
| 503 | ' [%s] macsuck %s - iid %s has no port mapping - skipping.', | ||
| 504 | $device->ip, $mac, $iid; | ||
| 505 | next MAC; | ||
| 506 | } | ||
| 507 | |||
| 508 | ++$cache->{$vlan}->{$port}->{$mac}; | ||
| 509 | } | ||
| 510 | |||
| 511 | return $cache; | ||
| 512 | } | ||
| 513 | |||
| 514 | sub sanity_macs { | ||
| 515 | my ($device, $cache, $device_ports) = @_; | ||
| 516 | |||
| 517 | # note any of the MACs which are actually device or device_port MACs | ||
| 518 | # used to spot uplink ports (neighborport) | ||
| 519 | my @fw_mac_list = (); | ||
| 520 | foreach my $vlan (keys %{ $cache }) { | ||
| 521 | foreach my $port (keys %{ $cache->{$vlan} }) { | ||
| 522 | push @fw_mac_list, keys %{ $cache->{$vlan}->{$port} }; | ||
| 523 | } | ||
| 524 | } | ||
| 525 | @fw_mac_list = List::MoreUtils::uniq( @fw_mac_list ); | ||
| 526 | my $port_macs = get_port_macs(\@fw_mac_list); | ||
| 527 | |||
| 528 | my $neighborport = {}; # ports through which we can see another device | ||
| 529 | my $ignoreport = {}; # ports suppressed by macsuck_no_deviceports | ||
| 530 | |||
| 531 | if (scalar @{ setting('macsuck_no_deviceports') }) { | ||
| 532 | my @ignoremaps = @{ setting('macsuck_no_deviceports') }; | ||
| 533 | |||
| 534 | foreach my $map (@ignoremaps) { | ||
| 535 | next unless ref {} eq ref $map; | ||
| 536 | |||
| 537 | foreach my $key (sort keys %$map) { | ||
| 538 | # lhs matches device, rhs matches port | ||
| 539 | next unless $key and $map->{$key}; | ||
| 540 | next unless acl_matches($device, $key); | ||
| 541 | |||
| 542 | foreach my $port (sort { sort_port($a, $b) } keys %{ $device_ports }) { | ||
| 543 | next unless acl_matches($device_ports->{$port}, $map->{$key}); | ||
| 544 | |||
| 545 | debug sprintf ' [%s] macsuck %s - port suppressed by macsuck_no_deviceports', | ||
| 546 | $device->ip, $port; | ||
| 547 | ++$ignoreport->{$port}; | ||
| 548 | } | ||
| 549 | } | ||
| 550 | } | ||
| 551 | } | ||
| 552 | |||
| 553 | |||
| 554 | foreach my $vlan (keys %{ $cache }) { | ||
| 555 | foreach my $port (keys %{ $cache->{$vlan} }) { | ||
| 556 | MAC: foreach my $mac (keys %{ $cache->{$vlan}->{$port} }) { | ||
| 557 | |||
| 558 | unless (check_mac($mac, $device)) { | ||
| 559 | delete $cache->{$vlan}->{$port}->{$mac}; | ||
| 560 | next MAC; | ||
| 561 | } | ||
| 562 | |||
| 563 | # this uses the cached $ports resultset to limit hits on the db | ||
| 564 | my $device_port = $device_ports->{$port}; | ||
| 565 | |||
| 566 | # WRT #475 ... see? :-) | ||
| 567 | unless (defined $device_port) { | ||
| 568 | debug sprintf | ||
| 569 | ' [%s] macsuck %s - port %s is not in database - skipping.', | ||
| 570 | $device->ip, $mac, $port; | ||
| 571 | delete $cache->{$vlan}->{$port}->{$mac}; | ||
| 572 | next MAC; | ||
| 573 | } | ||
| 574 | |||
| 575 | if (exists $ignoreport->{$port}) { | ||
| 576 | debug sprintf | ||
| 577 | ' [%s] macsuck %s - port %s is suppressed by config - skipping.', | ||
| 578 | $device->ip, $mac, $port; | ||
| 579 | delete $cache->{$vlan}->{$port}->{$mac}; | ||
| 580 | next MAC; | ||
| 581 | } | ||
| 582 | |||
| 583 | if (exists $neighborport->{$port}) { | ||
| 584 | debug sprintf | ||
| 585 | ' [%s] macsuck %s - seen another device thru port %s - skipping.', | ||
| 586 | $device->ip, $mac, $port; | ||
| 587 | delete $cache->{$vlan}->{$port}->{$mac}; | ||
| 588 | next MAC; | ||
| 589 | } | ||
| 590 | |||
| 591 | # check to see if the port is connected to another device | ||
| 592 | # and if we have that device in the database. | ||
| 593 | |||
| 594 | # carefully be aware: "uplink" here means "connected to another device" | ||
| 595 | # it does _not_ mean that the user wants nodes gathered on the remote dev. | ||
| 596 | |||
| 597 | # we have two ways to detect "uplink" port status: | ||
| 598 | # * a neighbor was discovered using CDP/LLDP | ||
| 599 | # * a mac addr is seen which belongs to any device port/interface | ||
| 600 | |||
| 601 | # allow to gather MACs on upstream (local) port for some kinds | ||
| 602 | # of device that do not expose MAC address tables via SNMP. | ||
| 603 | # relies on prefetched neighbors otherwise it would kill the DB | ||
| 604 | # with device lookups. | ||
| 605 | |||
| 606 | my $neigh_cannot_macsuck = eval { # can fail | ||
| 607 | acl_matches(($device_port->neighbor || "0 but true"), 'macsuck_unsupported') || | ||
| 608 | match_to_setting($device_port->remote_type, 'macsuck_unsupported_type') }; | ||
| 609 | |||
| 610 | # here, is_uplink comes from Discover::Neighbors finding LLDP remnants | ||
| 611 | if ($device_port->is_uplink) { | ||
| 612 | if ($neigh_cannot_macsuck) { | ||
| 613 | debug sprintf | ||
| 614 | ' [%s] macsuck %s - port %s neighbor %s without macsuck support', | ||
| 615 | $device->ip, $mac, $port, | ||
| 616 | (eval { $device_port->neighbor->ip } | ||
| 617 | || ($device_port->remote_ip | ||
| 618 | || $device_port->remote_id || '?')); | ||
| 619 | # continue!! | ||
| 620 | } | ||
| 621 | elsif (my $neighbor = $device_port->neighbor) { | ||
| 622 | debug sprintf | ||
| 623 | ' [%s] macsuck %s - port %s has neighbor %s - skipping.', | ||
| 624 | $device->ip, $mac, $port, $neighbor->ip; | ||
| 625 | delete $cache->{$vlan}->{$port}->{$mac}; | ||
| 626 | next MAC; | ||
| 627 | } | ||
| 628 | elsif (my $remote = $device_port->remote_ip) { | ||
| 629 | debug sprintf | ||
| 630 | ' [%s] macsuck %s - port %s has undiscovered neighbor %s', | ||
| 631 | $device->ip, $mac, $port, $remote; | ||
| 632 | # continue!! | ||
| 633 | } | ||
| 634 | elsif (not setting('macsuck_bleed')) { | ||
| 635 | debug sprintf | ||
| 636 | ' [%s] macsuck %s - port %s is detected uplink - skipping.', | ||
| 637 | $device->ip, $mac, $port; | ||
| 638 | |||
| 639 | $neighborport->{$port} = [ $vlan, $mac ] # remember neighbor port mac | ||
| 640 | if exists $port_macs->{$mac}; | ||
| 641 | delete $cache->{$vlan}->{$port}->{$mac}; | ||
| 642 | next MAC; | ||
| 643 | } | ||
| 644 | } | ||
| 645 | |||
| 646 | # here, the MAC is known as belonging to a device switchport | ||
| 647 | if (exists $port_macs->{$mac}) { | ||
| 648 | my $switch_ip = $port_macs->{$mac}; | ||
| 649 | if ($device->ip eq $switch_ip) { | ||
| 650 | debug sprintf | ||
| 651 | ' [%s] macsuck %s - port %s connects to self - skipping.', | ||
| 652 | $device->ip, $mac, $port; | ||
| 653 | delete $cache->{$vlan}->{$port}->{$mac}; | ||
| 654 | next MAC; | ||
| 655 | } | ||
| 656 | |||
| 657 | debug sprintf ' [%s] macsuck %s - port %s is probably an uplink', | ||
| 658 | $device->ip, $mac, $port; | ||
| 659 | $device_port->update({is_uplink => \'true'}) | ||
| 660 | unless $device_port->is_uplink; | ||
| 661 | |||
| 662 | if ($neigh_cannot_macsuck) { | ||
| 663 | # neighbor exists and Netdisco can speak to it, so we don't want | ||
| 664 | # its MAC address. however don't add to neighborport as that would | ||
| 665 | # clear all other MACs on the port. | ||
| 666 | delete $cache->{$vlan}->{$port}->{$mac}; | ||
| 667 | next MAC; | ||
| 668 | } | ||
| 669 | |||
| 670 | # when there's no CDP/LLDP, we only want to gather macs at the | ||
| 671 | # topology edge, hence skip ports with known device macs. | ||
| 672 | if (not setting('macsuck_bleed')) { | ||
| 673 | debug sprintf ' [%s] macsuck %s - port %s is at topology edge', | ||
| 674 | $device->ip, $mac, $port; | ||
| 675 | |||
| 676 | $neighborport->{$port} = [ $vlan, $mac ]; # remember for later | ||
| 677 | delete $cache->{$vlan}->{$port}->{$mac}; | ||
| 678 | next MAC; | ||
| 679 | } | ||
| 680 | } | ||
| 681 | |||
| 682 | # possibly move node to lag master | ||
| 683 | if (defined $device_port->slave_of | ||
| 684 | and exists $device_ports->{$device_port->slave_of}) { | ||
| 685 | |||
| 686 | my $parent = $device_port->slave_of; | ||
| 687 | $device_ports->{$parent}->update({is_uplink => \'true'}) | ||
| 688 | unless $device_ports->{$parent}->is_uplink; | ||
| 689 | |||
| 690 | # VLAN subinterfaces can be set uplink, | ||
| 691 | # but we don't want to move nodes there (so check is_master). | ||
| 692 | if ($device_ports->{$parent}->is_master) { | ||
| 693 | delete $cache->{$vlan}->{$port}->{$mac}; | ||
| 694 | ++$cache->{$vlan}->{$parent}->{$mac}; | ||
| 695 | } | ||
| 696 | } | ||
| 697 | } | ||
| 698 | } | ||
| 699 | } | ||
| 700 | |||
| 701 | # restore MACs of neighbor devices. | ||
| 702 | # this is when we have a "possible uplink" detected but we still want to | ||
| 703 | # record the single MAC of the neighbor device so it works in Node search. | ||
| 704 | foreach my $port (keys %$neighborport) { | ||
| 705 | my ($vlan, $mac) = @{ $neighborport->{$port} }; | ||
| 706 | delete $cache->{$_}->{$port} for keys %$cache; # nuke nodes on all VLANs | ||
| 707 | ++$cache->{$vlan}->{$port}->{$mac}; | ||
| 708 | } | ||
| 709 | |||
| 710 | return $cache; | ||
| 711 | } | ||
| 712 | |||
| 713 | true; |