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