From 845bbf6ba96ea62b63f3001779671a6a2d91de22 Mon Sep 17 00:00:00 2001 From: Jed Reynolds Date: Tue, 28 Apr 2020 23:07:16 -0700 Subject: [PATCH 1/3] Utils: adds mac_add and uses it in lf_associate_ap --- LANforge/Utils.pm | 18 ++++++++++++++++++ lf_associate_ap.pl | 33 ++++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/LANforge/Utils.pm b/LANforge/Utils.pm index c5c56a92..c0d56c51 100644 --- a/LANforge/Utils.pm +++ b/LANforge/Utils.pm @@ -3,6 +3,7 @@ use strict; use warnings; use Carp; use Net::Telnet; +use bigint; $| = 1; #$SIG{ __DIE__ } = sub { Carp::confess( @_ ) }; #$SIG{ __WARN__ } = sub { Carp::confess( @_ ) }; @@ -1158,6 +1159,23 @@ sub expand_unit_str { return int($num) * $multiplier; } +sub mac_add { + my ($first_mac, $second_dec) = @_; + $first_mac =~ s/[:]//g if ($first_mac =~ /[:]/); + + $first_mac = "0x".$first_mac if ($first_mac !~ /^0x/); + + my $newdec = (0+$second_dec) + hex($first_mac); + my $newhex = sprintf("%x", $newdec); + my $rv = ""; + for (my $i = length($newhex); $i > 0; $i-=2) { + $rv = substr($newhex, $i-2, 2).":$rv"; + } + return substr($newhex, 0, -1); +} + + + #### 1; __END__ diff --git a/lf_associate_ap.pl b/lf_associate_ap.pl index 04f1e026..7ef11d2d 100755 --- a/lf_associate_ap.pl +++ b/lf_associate_ap.pl @@ -43,6 +43,7 @@ use Carp; use POSIX qw(ceil floor); use Scalar::Util; #::looks_like_number; use Getopt::Long; + no warnings 'portable'; # Support for 64-bit ints required use Socket; #use Data::Dumper; @@ -83,7 +84,7 @@ use List::Util qw(first); use LANforge::Endpoint; use LANforge::Port; use LANforge::Utils; -use Net::Telnet (); +#use Net::Telnet (); our $num_stations = 1; our $netmask = "255.255.0.0"; @@ -288,6 +289,10 @@ Examples: $0 --action step2 --radio wiphy2 --ssid jedtest \\ --first_sta sta100 --first_ip DHCP --num_stations 3 \\ --security wpa2 --passphrase jedtest1 --mac_pattern 'xx:xx:xx:*:*:*' + Note: mac_pattern is NOT a regex, it is octet based tokens: + * = rand(256) + xx = parent mac octet + You can specify a numeric mac address (d0:01:00:00:af:ff) and it can get incremented ## using a second lanforge system to connect to wpa2 AP: $0 --mgr 192.168.100.1 --resource 2 --radio wiphy2 \\ @@ -454,6 +459,10 @@ sub new_mac_from_pattern { die ("::new_mac_pattern: blank parent_mac. Please debug.") if ($parent_mac eq ""); die ("::new_mac_pattern: blank pattern. Please debug.") if ($pattern eq ""); + if (($pattern !~ /x+/i) && ($pattern !~ /[*]+/)) { + return $pattern; + } + my @parent_hunks = split(":", $parent_mac); my @pattern_hunks = split(":", $pattern); @@ -710,7 +719,7 @@ sub new_wifi_station { my $rh_results = shift; die("new_wifi_station wants hash ref to place results, bye.") unless(defined $rh_results); my $wifi_m = shift; - my $sleep_amt = shift; + my $num_in_series = shift; # use this to add to non-patterned mac-address my $mac_addr = ""; #print "## new-wifi-station, sta-name: $sta_name change-mac: $change_mac" unless($::utils->isQuiet()); @@ -728,6 +737,9 @@ sub new_wifi_station { my $parent_mac = get_radio_bssid($::sta_wiphy); die("new_wifi_station: unable to find bssid of parent radio") if ($parent_mac eq ""); $mac_addr = new_mac_from_pattern($parent_mac, $::mac_pattern); + if (($mac_addr eq $parent_mac) && ($::num_stations > 1)) { + $mac_addr = $::utils->mac_add($parent_mac, $num_in_series); + } #print "new_wifi_station->new_mac_from_pattern: $mac_addr\n"; } @@ -787,9 +799,9 @@ sub new_wifi_station { #$::utils->sleep_ms(20); } - if ($sleep_amt > 0) { - sleep $sleep_amt; - } + #if ($sleep_amt > 0) { + # sleep $sleep_amt; + #} my $data = [ $mac_addr, $sta_name, $sta1_cmd ]; $rh_results->{$sta_name} = $data; } @@ -1227,7 +1239,7 @@ sub doStep_1 { for $sta_name (sort(keys %::sta_names)) { # sta, ip, rh, $ip_addr print " $sta_name "; - new_wifi_station( $sta_name, $::sta_names{$sta_name}, \%results1, $::wifi_mode, 0); + new_wifi_station( $sta_name, $::sta_names{$sta_name}, \%results1, $::wifi_mode, $i); altsleep(0.12); altsleep(0.6) if (($i % 5) == 0); $i++; @@ -1316,11 +1328,13 @@ sub doStep_2 { print "Creating new stations..."; # create new stations + my $i = 0; for my $sta_name (sort(keys %::sta_names)) { die("misconfiguration! ") if( ref($sta_name) eq "HASH"); my $ip = $::sta_names{$sta_name}; print "$sta_name " unless($::utils->isQuiet()); - new_wifi_station( $sta_name, $ip, \%results2, $::wifi_mode, 0); + new_wifi_station( $sta_name, $ip, \%results2, $::wifi_mode, $i); + $i++; # Uncomment to diagnose connection results. The IPs assigned # are unlikely to appear instantly, but the mac and entity id @@ -1389,7 +1403,7 @@ sub doAdd { die("misconfiguration! ") if( ref($sta_name) eq "HASH"); my $ip = $::sta_names{$sta_name}; print " $sta_name"; - new_wifi_station( $sta_name, $ip, \%results2, $::wifi_mode, 0); + new_wifi_station( $sta_name, $ip, \%results2, $::wifi_mode, $i); if (($i % 10) == 9) { $::utils->sleep_ms(120); } @@ -1583,7 +1597,7 @@ if (@ARGV < 2) { GetOptions ( 'mgr|m=s' => \$::lfmgr_host, - 'mgr_port|p=i' => \$lfmgr_port, + 'lf_mgr_port|lf_port|mgr_port|p=i' => \$lfmgr_port, 'resource|r=i' => \$::resource, 'resource2|r2=i' => \$::resource2, 'quiet|q=s' => \$::quiet, @@ -1648,6 +1662,7 @@ if (defined $log_cli) { # Configure our utils. our $utils = new LANforge::Utils(); +print "Connecting to $lfmgr_host, $lfmgr_port\n"; $::utils->connect($lfmgr_host, $lfmgr_port); if ($db_postload ne "" && db_exists($::db_postload)==0) { From da4d6cbe3a981e92e0633b0cc196a8386b815876 Mon Sep 17 00:00:00 2001 From: Jed Reynolds Date: Wed, 29 Apr 2020 00:44:58 -0700 Subject: [PATCH 2/3] LANforge/Utils: gains mac_add() routine; requires Math::BigInt. Tested on F24 --- LANforge/Utils.pm | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/LANforge/Utils.pm b/LANforge/Utils.pm index c0d56c51..e65ec0ec 100644 --- a/LANforge/Utils.pm +++ b/LANforge/Utils.pm @@ -3,7 +3,8 @@ use strict; use warnings; use Carp; use Net::Telnet; -use bigint; +#use bigint; +use Math::BigInt; $| = 1; #$SIG{ __DIE__ } = sub { Carp::confess( @_ ) }; #$SIG{ __WARN__ } = sub { Carp::confess( @_ ) }; @@ -1160,18 +1161,23 @@ sub expand_unit_str { } sub mac_add { - my ($first_mac, $second_dec) = @_; + my ($self, $first_mac, $second_dec) = @_; $first_mac =~ s/[:]//g if ($first_mac =~ /[:]/); - $first_mac = "0x".$first_mac if ($first_mac !~ /^0x/); - - my $newdec = (0+$second_dec) + hex($first_mac); - my $newhex = sprintf("%x", $newdec); + my $newdec = Math::BigInt->new($first_mac); + $newdec->badd(0+$second_dec); + my $pad = Math::BigInt->new('0x1000000000000'); + $newdec->badd($pad); + my $newhex = "".$newdec->as_hex(); my $rv = ""; + $newhex = substr($newhex, -12); # we begin 0x100...much bigger than we need for (my $i = length($newhex); $i > 0; $i-=2) { $rv = substr($newhex, $i-2, 2).":$rv"; } - return substr($newhex, 0, -1); + $rv =substr($rv, 0, -1); + undef($newdec); + undef($pad); + return $rv; } From d2891904ac98af3853ba416ebe4b0a5f748f34af Mon Sep 17 00:00:00 2001 From: Jed Reynolds Date: Wed, 29 Apr 2020 00:45:38 -0700 Subject: [PATCH 3/3] associate_ap: now uses Utils::mac_add when multiple stations requested but mac_pattern lacks tokens --- lf_associate_ap.pl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lf_associate_ap.pl b/lf_associate_ap.pl index 7ef11d2d..e552c95a 100755 --- a/lf_associate_ap.pl +++ b/lf_associate_ap.pl @@ -460,7 +460,7 @@ sub new_mac_from_pattern { die ("::new_mac_pattern: blank pattern. Please debug.") if ($pattern eq ""); if (($pattern !~ /x+/i) && ($pattern !~ /[*]+/)) { - return $pattern; + return $pattern; # this lacks pattern tokens } my @parent_hunks = split(":", $parent_mac); @@ -737,9 +737,11 @@ sub new_wifi_station { my $parent_mac = get_radio_bssid($::sta_wiphy); die("new_wifi_station: unable to find bssid of parent radio") if ($parent_mac eq ""); $mac_addr = new_mac_from_pattern($parent_mac, $::mac_pattern); - if (($mac_addr eq $parent_mac) && ($::num_stations > 1)) { - $mac_addr = $::utils->mac_add($parent_mac, $num_in_series); + #print "OLD MAC $::mac_pattern NEW MAC $mac_addr\n"; + if (($mac_addr eq $::mac_pattern) && ($num_in_series > 0)) { + $mac_addr = $::utils->mac_add($::mac_pattern, $num_in_series); } + #print "OLD MAC $::mac_pattern NEWER MAC $mac_addr\n"; #print "new_wifi_station->new_mac_from_pattern: $mac_addr\n"; }