-
Notifications
You must be signed in to change notification settings - Fork 3
/
lf_associate_ap.pl
executable file
·1811 lines (1633 loc) · 65.2 KB
/
lf_associate_ap.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
## ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
## LANforge server script for associating virtual stations
## to an arbitrary SSID. You have options for creating a series
## of Layer-3 connections per station created. Support for various
## security modes for stations: wep, wpa, wpa2.
##
## Install:
## copy this script to /home/lanforge/scripts
##
## Preparation:
## This script expects a free radio (like wiphy0) to create
## wifi stations on. It also expects an upstream wired port to
## make tcp connections to. These ports should be able to
## communicate with each other.
##
## Usage Overview:
## Use -h to show options.
## There are two activities that this script presently performs:
##
## Step1: create 1 wifi station, pass traffic to the upstream port
## back and forth, and then disassociate. This activity could
## be split into several steps if testing traffic up- or
## down-stream only is desired.
##
## Step2: create many wifi stations, wait until we see IPs appear
## on them and then disassociate all of them. This activity could
## also be modified to test for wifi association instead of address
## aquisition. The present example uses static address assignment.
##
## add: create and delete WiFi Virtual Radios. Also has option to
## create station on specified virtual radio.
##
## (C) 2020, Candela Technologies Inc. [email protected]
## ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
package main;
use strict;
use warnings;
use diagnostics;
use Carp;
#$SIG{ __DIE__ } = sub { Carp::confess( @_ ) };
#$SIG{ __WARN__ } = sub { Carp::confess( @_ ) };
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;
our $binsleep = 0;
if ( -x "/bin/sleep" || -x "/usr/bin/sleep") {
$::binsleep = 1;
}
sub altsleep {
my ($time) = @_;
if ($::binsleep) {
`sleep $time`;
}
elsif ( $time < 1) {
sleep(1);
}
elsif (int($time) != $time) {
$time += 1.0;
$time = int($time);
sleep($time);
}
else {
sleep($time);
}
}
# Un-buffer output
$| = 1;
use Cwd qw(getcwd);
my $cwd = getcwd();
# use lib prepends to @INC, so put lower priority first
# This is before run-time, so cannot condition this with normal 'if' logic.
use lib '/home/lanforge/scripts';
use lib "./";
use List::Util qw(first);
use LANforge::Endpoint;
use LANforge::Port;
use LANforge::Utils;
#use Net::Telnet ();
our $num_stations = 1;
our $netmask = "255.255.0.0";
our $default_ip_addr = "DHCP"; # or IP
my $log_cli = "unset"; # use ENV{'LOG_CLI'}
# the upstream port should have an IP in same subnet range
# and we're assuming the port is on the same resource (1).
our $upstream_port = "eth1"; # Step 1 upstream port
our $sta_wiphy = "wiphy0"; # physical parent (radio) of virtual stations
our $phy_channel = ""; # channel number
our $phy_antenna = ""; # number of antennas, 0 means all
our %wiphy_bssids = ();
our $admin_down_on_add = 0;
our $ssid;
our $first_sta = "sta100";
our $passphrase = '';
our $change_mac = 0;
our $min_tx = "10000000";
our $max_tx = "SAME";
our $security = "open";
our $xsec = ""; # extra 802.1* options: use-11u,use-11u-internet,use-dot1x
our %sec_options = (
"open" => 0x0,
"wpa" => 0x10,
"wep" => 0x200,
"wpa2" => 0x400,
"no-ht40" => 0x800, # Disable ht-40
"use-scan-ssid" => 0x1000, # Enable SCAN-SSID flag in wpa_supplicant.
"use-pasv-scan" => 0x2000, # Use passive scanning (don't send probe requests).
"no-sgi" => 0x4000, # Disable SGI (Short Guard Interval).
"use-radio-migration" => 0x8000, # OK-To-Migrate (Allow migration between LANforge radios)
"use-more-debug" => 0x10000, # Verbose-Debug: more info in wpa-supplicant and hostapd logs.
"use-11u" => 0x20000, # Enable 802.11u (Interworking) feature.
"use-11u-auto" => 0x40000, # Enable 802.11u (I...) Auto-internetworking. Always enabled currently.
"use-11u-internet" => 0x80000, # AP Provides access to internet (802.11u I...)
"use-11u-x-steps" => 0x100000, # AP requires additional step for access (802.11u I...)
"use-11u-emrg-advert" => 0x200000, # AP claims emergency services reachable (802.11u I...)
"use-11u-emrg-unauth" => 0x400000, # AP provides Unauthenticated emergency services (802.11u I...)
"use-hs20" => 0x800000, # Enable Hotspot 2.0 (HS20) feature. Req WPA-2.
"no-dgaf" => 0x1000000, # AP: Disable DGAF (used by HotSpot 2.0).
"use-dot1x" => 0x2000000, # Use 802.1x (RADIUS for AP).
"use-11r-pmska" => 0x4000000, # Enable PMSKA caching for WPA2 (Rel to 802.11r).
"no-ht80" => 0x8000000, # Disable HT80 (for AC chipset NICs only)
"use-ibss" => 0x20000000, # Station should be in IBSS mode.
"use-osen" => 0x40000000, # Enable OSEN protocol (OSU Server-only Auth)
"disable_roam" => 0x80000000, # Disable automatic station roaming based on scan results.
"ht160_enable" => 0x100000000, # Enable HT160 mode.
"disable_fast_reauth" => 0x200000000, # Disable fast_reauth option for virtual stations.
"mesh_mode" => 0x400000000, # Station should be in MESH mode.
"power_save_enable" => 0x800000000, # Station should enable power-save. May not work in all drivers/configurations.
"create_admin_down" => 0x1000000000, # Station should be created admin-down.
"wds-mode" => 0x2000000000, # WDS station (sort of like a lame mesh), not supported on ath10k
"no-supp-op-class-ie" => 0x4000000000, # Do not include supported-oper-class-IE in assoc requests. May work around AP bugs.
"txo-enable" => 0x8000000000, # Enable/disable tx-offloads, typically managed by set_wifi_txo command
"wpa3" => 0x10000000000, # Enable WPA-3 (SAE Personal) mode.
"use-bss-transition" => 0x80000000000, # Enable BSS transition.
"disable-twt" => 0x100000000000, # Disable TWT mode
);
our %ieee80211w_options = (
"disabled" => 0,
"optional" => 1,
"required" => 2,
"0" => 0,
"1" => 1,
"2" => 2
);
our $ieee80211w = "NA";
our $cx_type = "tcp";
our %cx_types = (
"tcp" => "lf_tcp",
"udp" => "lf_udp",
"tcp6" => "lf_tcp6",
"udp6" => "lf_udp6",
);
our %antenna_table = (
0 => 0,
"0" => 0,
-1 => 0,
'0' => 0,
'-1' => 0,
'ALL' => 0,
'1' => 1,
'1x1' => 1,
'A' => 1,
'2' => 4,
'2x2' => 4,
'AB' => 4,
'3' => 7,
'3x3' => 7,
'ABC' => 7,
'4' => 8,
'4x4' => 8,
'ABCD' => 8,
);
our $duration = 30; # seconds to transmit in step 1
our $db_preload = ""; # use for loading before station creation
our $db_save = ""; # use for saving a scenario that we just ran
our $db_postload = ""; # use for cleanup after running/saving a scenario
our $poll_time = 5; # seconds
our $traffic_type = "separate"; # separate: download then upload, concurrent: at same time
our $default_mac_pat = "xx:xx:xx:*:*:xx";
our $mac_pattern = $::default_mac_pat;
our $gateway = "NA";
our %wifi_modes = (
"a" => "1",
"b" => "2",
"g" => "3",
"abg" => "4",
"abgn" => "5",
"bgn" => "6",
"bg" => "7",
"abgnAC" => "8",
"anAC" => "9",
"an" => "10",
"bgnAC" => "11",
"abgnAX" => "12",
"bgnAX" => "13",
"anAX" => "14"
);
our $wifi_mode ="";
our $bssid = "";
my $mode_list = join("|", sort keys %wifi_modes);
## ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- #
## Usage #
## ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- #
my $usage = qq($0 [--mgr {host-name | IP}]
[--mgr_port {ip port}] # use if on non-default management port
[--resource {resource}] # use if multiple lanforge systems; defaults to 1
[--quiet { yes | no }] # debug output; -q
[--log_cli] # enables CLI command printing to STDOUT
# same effect when setting env var LOG_CLI=STDOUT
## AP selection
[--radio {name}] # e.g. wiphy2
[--channel {channel}] # e.g. 52, 161, 153
# please check the LANforge GUI to verify resulting selection
# center channels might be selected differently than you intended
[--antenna {1,2,3,4}] # select number of antennas
[--ssid {ssid}] # e.g. jedtest
[--bssid {aa:bb:cc:00:11:22, or DEFAULT} # AP BSSID to connect to
[--security {open|wep|wpa|wpa2|wpa3}] # station authentication type, Default is open
[--xsec {comma,separated,list} ] # dot1x, 11u, other features, read script {to set flags same as in add_sta}
[--passphrase {...}] # Set security too if you want to enable security
[--wifi_mode {$mode_list}]
[--ieee80211w {disabled,optional,required}] # protected management frames (wpa2-ent/wpa3) also { NA, 0, 1, 2 }
## station configuration
[--num_stations {$num_stations}] # Defaults to 1
[--first_sta {$first_sta}]
[--first_ip {DHCP | DHCP6 | DHCP,DHCP6 |<ip address>}]
# use DHCP,DHCP6 to enable both DHCP and DHCP6
[--netmask {$netmask}]
[--gateway {$gateway}]
[--change_mac {0|1}]
# If this is set to 0, then we will not change MAC if the station already exists.
# This is now the default behaviour.
[--mac-pattern {$default_mac_pat}]
# xx : uses parent radio octet
# [0-9a-f] : use this value for octet
# * : generates random octet
# Use quotes around this argument! EG:
# --mac_pattern '00:xx:*:*:xx:xx'
## connection configuration
[--cxtype {tcp/tcp6/udp/udp6}] # use a tcp/udp connection, default tcp
[--upstream {name|$upstream_port}]
# could be AP or could be port on LANforge
# connected to WAN side of AP
[--bps-min {$min_tx}] # minimum tx bps
[--bps-max {SAME|bps-value}] # maximum tx bps, use SAME or omit for SAME
[--duration {$duration}] # connection duration, seconds, default 60
[--poll-time {$poll_time}] # nap time between connection displays
[--action {step1,step2,add,del,del_all_phy}]
# step1: creates <num_stations> stations and L3 connections
# step2: does bringup test
# add: creates station on specified radio, or radio if no stations are requested
# del: Delete the specified port.
# del_all_phy: Delete all interfaces with the specified parent device.
[--traffic_type {separate|concurrent}]
# for step1: separate does download then upload
# concurrent does upload and download at same time
[--admin_down_on_add]
# when creating stations, create them admin-down
[--db_preload {scenario name}]
# load this database before creating stations
# option intended as a cleanup step
[--db_save {name}]
# save the state of this test scenario after running the
# connections, before --db_postload
[--db_postload {scenario name}]
# load this database after running connections,
# option intended as a cleanup step
## virtual radio configuration
[--vrad_chan {channel}]
[--port_del {name}] # deletes port given
Examples:
## connecting to an open AP, at 2Mbps, for 20 minutes
$0 --action step1 --radio wiphy0 --ssid ap-test-01 \\
--bps-min 2000000 --duration 1200 --upstream eth1
$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 \\
--ssid jedtest --passphrase 'asdf1234' \\
--num_stations 10 --first_sta sta400 \\
--first_ip DHCP --upstream eth1 --action step1
## (Windows) using a beginning database and saving the resulting database:
C:\\Users\\bob> cd "c:\\Program Files (x86)\\LANforge-Server\\scripts"
C:\\Program Files (x86)\\LANforge-Server\\scripts>perl lf_associate_ap.pl --mgr jedtest \\
--resource 2 --radio wiphy2 --first_ip DHCP \\
--duration 10 --bps-min 10k --bps-max 20M --cxtype tcp \\
--ssid jedtest --passphrase jedtest1 --security wpa2 \\
--first_sta 300 --db_preload Radio2 --db_save run_results --num_stations 3
## connecting to wpa AP:
$0 --mgr 192.168.100.1 --radio wiphy0 \\
--ssid jedtest --passphrase 'asdf1234' --security wep \\
--num_stations 10 --first_sta sta400 \\
--first_ip DHCP --upstream eth1 --action step1
## creating and deleting a virtual radio:
$0 --mgr 192.168.100.1 --resource 2 \\
--radio vphy1 --vrad_chan 36 --action add
$0 --mgr 192.168.100.1 --resource 2 \\
--port_del vrad1 --action del
## Adding a station to a new or existing virtual radio:
$0 --mgr 192.168.100.1 --resource 2 \\
--radio vphy1 --first_sta sta0 --first_ip DHCP --ssid my_ssid --action add
## Add lots of stations to a radio
$0 --mgr ben-ota-1 --resource 2 --action add --radio wiphy0 --ssid Lede-ventana \\
--first_sta sta100 --first_ip DHCP --num_stations 63
## Delete all virtual devices on wiphy0
$0 --mgr ben-ota-1 --resource 2 --action del_all_phy --port_del wiphy0
## Create a station and set Flags
$0 --mgr localhost --radio wiphy1 --ssid sushant-AP --action add --num_stations 1 \\
--xsec use-bss-transition --first_ip DHCP
);
my $shelf_num = 1;
# Default values for cmd-line args.
our $report_timer = 1000; # milliseconds
our $test_mgr = "default_tm"; # name of test manager
our $resource = 1; # might be referred to as card_id
our $resource2 = 1; # might be referred to as card_id
our $begin_ip = $default_ip_addr;
# sta_names is a set of names and static IP addresses to assign them.
# As many stations as are in the set will be created. If you want to use
# DHCP, replace the ip with "DHCP".
# example
# %sta_names = (
# "sta1" => "192.168.0.1",
# "sta2" => "NEXT"
# "sta2" => "DHCP"
#);
our %sta_names = ();
our %cx_names = ();
our $quiet = "yes"; # debugging
our $action = "step1"; # default action
our $lfmgr_host = "localhost"; # LANforge manager IP
# Virtual radio defaults.
our $vrad_chan = -1; # default channel (AUTO)
# ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
# Nothing to configure below here, most likely.
# ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
my $lfmgr_port = 4001; # LANforge manager port
our $quiesce_sec = 3; # pretty standard
=pod
this fmt_cmd subroutine is now disabled, please use utils->fmt_cmd
=cut
sub db_exists {
my $db_name = shift;
die ("::db_exists: called with blank database name. Did you mean EMPTY?") if ($db_name eq "");
print "Looking for database $db_name ...";
my @db_names = split("\n", $::utils->doAsyncCmd("show_dbs"));
my @match = grep { /^$db_name\/$/ } @db_names;
return 1 if (@match > 0);
print "Warning! Scenario $db_name not found among: ".join(", ", @db_names)."\n";
return 0;
}
sub load_db {
my $db_name = shift;
die ("::load_db: called with blank database name. Did you mean EMPTY?") if ($db_name eq "");
print "Loading database $db_name ...";
$::utils->doCmd(fmt_cmd("load", $db_name, "overwrite"));
for (my $i = 20 ; $i>0; $i--) {
sleep(1);
my $up = 0;
my $has_tx_bytes = 0;
my $sta_cnt = 0;
my $prev_cnt = 0;
my $status = $::utils->doAsyncCmd($::utils->fmt_cmd("nc_show_ports", 1, $::resource, "ALL"));
my @status = split("\n", $status);
foreach (@status){
if (/^Shelf: 1, Card: \d+\s+Port: \d+\s+Type: STA\s+/) {
$sta_cnt++;
print "sta_cnt $sta_cnt up $up has_tx %has_tx_bytes\n";
}
if ($sta_cnt > $prev_cnt) {
if ( /IP: \d+\.\d+\.\d+\.\d+ / && !/IP: 0\.0\.0\.0 /) {
$up++;
}
if ( /Txb: \d+ / && !/Txb: 0 / ) {
$has_tx_bytes ++;
}
$prev_cnt = $sta_cnt if ( /^\s*$/ );
}
} # ~foreach
}
print " done\n";
}
sub save_db {
my $db_name = shift;
die ("::save_db: called with blank database name. Please debug.") if ($db_name eq "");
print "Saving database $db_name ...";
if (db_exists($db_name)==1) {
print "Warning: will over-write database $db_name! ";
}
$::utils->doCmd($::utils->fmt_cmd("save", $db_name));
print " done\n";
}
sub get_radio_bssid {
my $radio_name = shift;
die ("::get_radio_bssid: blank radio name. Please debug.") if ($radio_name eq "");
return $::wiphy_bssids{ $radio_name }
if (exists($::wiphy_bssids{ $radio_name }));
#print "* looking up $radio_name for bssid...";
my @status_lines = split("\n", $::utils->doAsyncCmd($::utils->fmt_cmd("show_port", 1, $::resource, $radio_name)));
my @mac_lines = grep { /\s+MAC:\s+[^ ]+/ } @status_lines;
die ("::get_radio_bssid: failed to find radio bssid, no MAC lines")
if (@mac_lines < 1);
my ($parent_bssid) = $mac_lines[0] =~ /\s+MAC:\s+([^ ]+)/;
die ("::get_radio_bssid: failed to find radio bssid, MAC was empty")
if ($parent_bssid eq "");
$::wiphy_bssids{ $radio_name } = $parent_bssid;
#print $parent_bssid."\n";
return $parent_bssid;
}
sub new_mac_from_pattern {
my $parent_mac = shift;
my $pattern = shift;
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; # this lacks pattern tokens
}
my @parent_hunks = split(":", $parent_mac);
my @pattern_hunks = split(":", $pattern);
die ("::new_mac_pattern: parent_mac needs to be colon-separated. Please debug.") if (@parent_hunks != 6);
die ("::new_mac_pattern: pattern needs to be colon-separated. Please debug.") if (@pattern_hunks != 6);
my @new_hunks = ();
for (my $i=0; $i < 6; $i++) {
if ($pattern_hunks[$i] =~ /xx/i) {
$new_hunks[ $i ] = $parent_hunks[ $i ];
}
elsif ($pattern_hunks[$i] =~ /[*]+/) {
my $r=int(rand(255));
if ($i == 0) {
$r |= 0x002; # sets the 'locally administered bit'
$r &= 0x0FE;
# use if this upstream routers squash local admin bit macs
# $r &= 0x0DF;
}
$new_hunks[ $i ] = sprintf("%02X", $r);
}
else {
$new_hunks[ $i ] = $pattern_hunks[ $i ];
}
}
#print "####### new_mac_from_pattern: [$parent_mac][$pattern] -> ".lc(join(":", @new_hunks))."\n";
return lc(join(":", @new_hunks));
} # ~new_mac_pattern
sub new_random_mac {
my $rv = "00:";
for (my $i=0; $i<5; $i++) {
$rv.=sprintf("%02X",int(rand(255))).(($i<4)?':':'');
}
#print "new_random_mac $rv\n";
return $rv;
}
sub fmt_vsta_cmd {
my ($resource, $sta_wiphy, $sta_name, $flags, $ssid, $passphrase, $mac, $flags_mask, $wifi_m, $bssid ) = @_;
die("fmt_vsta_cmd wants sta_wiphy name, bye.") unless($sta_wiphy);
my $key = "[BLANK]";
my $ap = "DEFAULT";
if ((defined $bssid) && ($bssid ne "")) {
$ap = $bssid;
}
my $cfg_file = "NA";
my $mode = 8; # default to a/b/g/n/AC
my $rate = "NA";
my $amsdu = "NA";
my $ampdu_factor = "NA";
my $ampdu_density = "NA";
my $sta_br_id = "NA";
$key = $passphrase if ($passphrase ne "");
if ($wifi_m ne "") {
if (exists $::wifi_modes{$wifi_m}) {
$mode = $::wifi_modes{$wifi_m};
}
else {
print "Wifi Mode [$wifi_m] not recognised. Please use:\n";
print join(", ", sort keys %::wifi_modes);
exit 1;
}
}
$flags = "+0" if ($flags == 0); # perl goes funny on zeros
$flags_mask = "+0" if ($flags_mask == 0);
$flags = "NA" if ($flags eq "");
$::ieee80211w = "NA"
if (!(defined $::ieee80211w) || ($::ieee80211w eq ""));
if ($::ieee80211w ne "NA") {
if ( exists $::ieee80211w_options{ $::ieee80211w }) {
$::ieee80211w = $::ieee80211w_options{ $::ieee80211w };
}
elsif ((int($::ieee80211w) < 0) || (int($::ieee80211w) > 2)) {
print("\n* ieee80211w value outside of values {0, 1, 2} or {disabled, optional, required} -- being set to NA\n");
$::ieee80211w = "NA";
}
# print("\n* ieee80211w value set to $::ieee80211w \n");
}
return $::utils->fmt_cmd("add_sta", 1, $resource, $sta_wiphy, $sta_name, "$flags",
"$ssid", "NA", "$key", $ap, $cfg_file, $mac,
$mode, $rate, $amsdu, $ampdu_factor, $ampdu_density,
$sta_br_id, "$flags_mask", $::ieee80211w );
}
sub fmt_vrad_cmd {
my ($resource, $sta_wiphy, $vrad_chan ) = @_;
die("fmt_vrad_cmd requires sta_wiphy.") unless($sta_wiphy);
my $mode = "NA";
my $country = "NA";
my $frequency = "NA";
my $frag_thresh = "NA";
my $rate = "NA";
my $rts = "NA";
my $txpower = "NA";
my $mac = "NA";
my $antenna = "NA";
my $flags = "0x1";
my $flags_mask = "NA";
return $::utils->fmt_cmd("set_wifi_radio", 1, $resource, $sta_wiphy, $mode, $vrad_chan,
$country, $frequency, $frag_thresh, $rate, $rts, $txpower,
$mac, "$antenna", "$flags", "$flags_mask" );
}
sub createEpPair {
my $sta_name = shift;
die("createEpPair: please pass station name, bye") unless(defined $sta_name && $sta_name ne '');
die("createEpPair: please define upstream_port, bye") unless(defined $::upstream_port && $::upstream_port ne '');
my $port_a = $sta_name;
my $port_b = $::upstream_port;
my $cx_name = $::cx_names{$sta_name}->{"cx"};
my $ep1 = $::cx_names{$sta_name}->{"ep1"};
my $ep2 = $::cx_names{$sta_name}->{"ep2"};
my %min_pkt_szs = (
'tcp' => [ 1460, 1460 ],
'tcp6' => [ 1460, 1460 ],
'udp' => [ 1472, 1472 ],
'udp6' => [ 1472, 1472 ]
);
my %max_pkt_szs = (
'tcp' => [ 1460, 1460 ],
'tcp6' => [ 1460, 1460 ],
'udp' => [ 1472, 1472 ],
'udp6' => [ 1472, 1472 ]
);
$::cx_type = "tcp" if ($::cx_type eq "");
print "\n cxtype [$::cx_type]\n" unless($::utils->isQuiet());
if ( ! exists $::cx_types{$::cx_type} ) {
die( "Please choose connection type: ".join(", ", keys(%::cx_types)));
}
my $cxtype = $::cx_types{$::cx_type};
my $rate_min = "+0"; # we will set these later
my $rate_max = "+0"; # using set_endp_tx_bounds
die("createEpPair: wants cx_name, bye.") unless(defined $cx_name && $cx_name ne '');
die("createEpPair: wants ep1 name, bye.") unless(defined $ep1 && $ep1 ne '');
die("createEpPair: wants ep2 name, bye.") unless(defined $ep2 && $ep2 ne '');
my $cmd = $::utils->fmt_cmd("add_endp", $ep1, 1, $::resource, $port_a, $cxtype,
-1, "NA", "$rate_min", "$rate_max", "NA",
$min_pkt_szs{$::cx_type}[0], @{$max_pkt_szs{$::cx_type}}[0],
"increasing", "NO", "NA", "NA", "NA");
print "EP1: $cmd\n" unless($::utils->isQuiet());
$::utils->doCmd($cmd);
$cmd = $::utils->fmt_cmd("add_endp", $ep2, 1, $::resource2, $port_b, $cxtype,
-1, "NA", "$rate_min", "$rate_max", "NA",
$min_pkt_szs{$::cx_type}[1], @{$max_pkt_szs{$::cx_type}}[1],
"increasing", "NO", "NA", "NA", "NA");
print "EP2: $cmd\n" unless($::utils->isQuiet());
$::utils->doCmd($cmd);
# Now, add the cross-connect
$::utils->doCmd($::utils->fmt_cmd("add_cx", $cx_name, $::test_mgr, $ep1, $ep2));
$::utils->doCmd($::utils->fmt_cmd("set_cx_report_timer", $::test_mgr, $cx_name, $::report_timer));
}
sub fmt_port_cmd {
my($resource, $port_id, $ip_addr, $mac_addr) = @_;
my $use_dhcp = ($ip_addr =~ /\bDHCP\b/) ? 1 : 0;
my $use_dhcp6 = ($ip_addr =~ /\bDHCP6\b/) ? 1 : 0;
my $ip = ($use_dhcp||$use_dhcp6) ? "0.0.0.0" : $ip_addr ;
$mac_addr = die("fmt_port_cmd requires mac_addr") if(!$mac_addr); # || $mac_addr eq "NA");
#print "fmt_port_cmd: RES $resource PORT $port_id IP_A $ip_addr MAC $mac_addr -> $ip\n" unless($::quiet eq "yes");
my $cmd_flags = 'NA'; #0;
my $cur_flags = 0;
$cur_flags |= 0x80000000 if ($use_dhcp);
$cur_flags |= 0x20000000000 if ($use_dhcp6);
#print "fmt_port_cmd: DHCP($use_dhcp) $cur_flags\n" unless($::quiet eq "yes");
my $ist_flags = 0;
$ist_flags |= 0x2; # check current flags
$ist_flags |= 0x4 if ($ip ne "NA");
$ist_flags |= 0x8 if ($::netmask ne "NA");
$ist_flags |= 0x10 if (($::gateway ne "NA") || ($::gateway ne "") || ($::gateway ne "0.0.0.0"));
$ist_flags |= 0x20 if ($mac_addr ne "NA");
$ist_flags |= 0x4000; # Always interested in DHCP, we either set it to DHCP or IP
$ist_flags |= 0x800000; # port up
$ist_flags |= 0x1000000; # Always interested in DHCP, we either set it to DHCP or IP
my $gw = "0.0.0.0";
if (($::gateway ne "") || ($::gateway ne "") || ($::gateway ne "0.0.0.0")) {
$gw = $::gateway;
}
my $dns_servers = "NA";
my $dhcp_client_id = "NONE";
my $flags2 = "NA";
# Ben suggests using $sta_name before using $port_id
$cur_flags = "+0" if(!$cur_flags);
$cmd_flags = "+0" if(!$cmd_flags);
$ist_flags = "+0" if(!$ist_flags);
my $cmd = $::utils->fmt_cmd("set_port", 1, $::resource, $port_id, $ip, $::netmask,
$gw, "$cmd_flags", "$cur_flags",
"$mac_addr", "NA", "NA", "NA", "$ist_flags", $::report_timer, "$flags2",
"NA","NA","NA","NA","NA","NA","NA","NA","NA","NA","NA","NA","NA",
$dns_servers, "NA", $dhcp_client_id);
print("fmt_port_cmd: ".$cmd) unless($::utils->isQuiet());
return $cmd;
}
sub fmt_port_down {
my($resource, $port_id, $ip_addr, $ip_mask) = @_;
die("fmt_port_down wants resource id, bye.") unless($resource);
die("fmt_port_down wants port_id id, bye.") unless($port_id);
die("fmt_port_down wants ip_addr id, bye.") unless($ip_addr);
die("fmt_port_down wants ip_mask id, bye.") unless($ip_mask);
my $use_dhcp = ($ip_addr =~ /\bDHCP\b/) ? 1 : 0;
my $use_dhcp6 = ($ip_addr =~ /\bDHCP6\b/) ? 1 : 0;
my $ip = ($use_dhcp||$use_dhcp6) ? "0.0.0.0" : $ip_addr ;
my $cmd_flags = "NA";
my $cur_flags = 0;
$cur_flags |= 0x1; # port down
my $ist_flags = 0;
$ist_flags |= 0x2; # check current flags
$ist_flags |= 0x800000; # port down
my $dhcp_id = "NONE";
my $netmask = "$ip_mask";
my $gw = (($::gateway eq "NA") || ($::gateway eq "") || ($::gateway eq "0.0.0.0")) ? "0.0.0.0" : $::gateway;
my $dns_servers = "NA";
my $dhcp_client_id = "NONE";
my $flags2 = "NA";
$cmd_flags = "+0" if(!$cmd_flags); # zeros are falsy in perl
$cur_flags = "+0" if(!$cur_flags);
$ist_flags = "+0" if(!$ist_flags);
my $cmd = $::utils->fmt_cmd("set_port", 1, $resource, $port_id, $ip_addr,
$netmask, $gw, "$cmd_flags", "$cur_flags",
"NA", "NA", "NA", "NA", "$ist_flags", $::report_timer, "$flags2",
"NA","NA","NA","NA","NA","NA","NA","NA","NA","NA","NA","NA","NA",
$dns_servers, "NA", $dhcp_client_id);
return $cmd;
}
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----#
# WiFi FLAGS #
# and please see the CLI users guide (flags can get updated) #
# http://www.candelatech.com/lfcli_ug.php #
# #
# 0x10 Enable WPA #
# 0x20 Use Custom wpa_supplicant config file. #
# 0x100 Use wpa_supplicant configured for WEP encryption. #
# 0x200 Use wpa_supplicant configured for WPA2 encryption. #
# 0x400 Disable HT-40 even if hardware and AP support it. #
# 0x800 Enable SCAN-SSID flag in wpa_supplicant. #
# 0x1000 Enable PCSC (used by WPA-SIM) #
# 0x2000 Disable SGI (Short Guard Interval). #
# 0x4000 OK-To-Migrate (Allow migration between LANforge radios) #
# 0x8000 Verbose-Debug: Increase debug info in wpa-supplicant and hostapd logs. #
# 0x10000 Enable 802.11u (Interworking) feature. #
# 0x20000 Enable 802.11u (Interworking) Auto-internetworking feature. #
# 0x40000 AP Provides access to internet (802.11u Interworking) #
# 0x80000 AP requires additional step for access (802.11u Interworking) #
# 0x100000 AP claims emergency services reachable (802.11u Interworking) #
# 0x200000 AP provides Unauthenticated emergency services (802.11u Interworking) #
# 0x400000 Enable Hotspot 2.0 (HS20) feature. Requires WPA-2. #
# 0x800000 AP: Disable DGAF (used by HotSpot 2.0). #
# 0x1000000 Use 802.1x (RADIUS for AP). #
# 0x2000000 Enable oportunistic PMSKA caching for WPA2 (Related to 802.11r). #
# #
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----#
sub new_wifi_station {
my $sta_name = shift;
die("new_wifi_station wants station name, bye") unless(defined $sta_name && $sta_name ne '');
my $ip_addr = shift;
die("new_wifi_station wants ip_address, bye") unless(defined $ip_addr && $ip_addr ne '');
my $rh_results = shift;
die("new_wifi_station wants hash ref to place results, bye.") unless(defined $rh_results);
my $wifi_m = 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());
if (! $::change_mac) {
my $status = $::utils->doAsyncCmd($::utils->fmt_cmd("show_port", 1, $::resource, $sta_name));
if ($status =~ /MAC:\s+(\S+)\s+/) {
$mac_addr = $1;
}
}
#print "new_wifi_station->mac_addr: $mac_addr\n";
if ($mac_addr eq "") {
# Couldn't find it, or we want to change the mac
#print "## calculating new mac-addr.." unless($::utils->isQuiet());
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);
#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";
}
#print "## $sta_name $mac_addr; " unless($::utils->isQuiet());
my $flags = +0; # set this to string later
my $flagsmask = +0; # set this to string later
# To set zero value set the bit in flags to zero.
# Set the flagsmask value to 1 if you want the value to be set to 1 or 0.
# NOTE: This script is used to change things, not just create them, so it is not
# always wrong to not set passphrase since it could be set already.
if ($::passphrase eq "") {
$::passphrase = "NA";
#if($::security ne "open") {
# die("Passphrase not set when --security [$::security] chosen. Please set passphrase.");
#}
}
if ( ! exists($::sec_options{$::security})) {
die( "Unknown security option [{$::security}]");
}
$flags |= $::sec_options{$::security};
# This doesn't work since 'open' maps to 0x0 and we need to also disable
# flags that might be set previously.
# $flagsmask |= $::sec_options{$::security};
# We are always configuring security to one thing or another, so we need to
# mask all of the bits properly.
$flagsmask |= (0x10 | 0x200 | 0x400 | 0x10000000000);
$flagsmask |= 0x1000000000 if ($::admin_down_on_add);
if (defined $::xsec && "$::xsec" ne "") {
for my $sec_op (split(',', $::xsec)) {
next if (!defined $::sec_options{$sec_op});
$flags |= $::sec_options{$sec_op};
$flagsmask |= $::sec_options{$sec_op};
}
}
$flags = "+0" if ( $flags == 0);
$flagsmask = "+0" if ( $flagsmask == 0);
# perform the station create first, then assign IP as necessary
my $sta1_cmd = fmt_vsta_cmd($::resource, $::sta_wiphy, $sta_name,
"$flags", "$::ssid", "$::passphrase",
$mac_addr, "$flagsmask", $wifi_m, $::bssid);
$::utils->doCmd($sta1_cmd);
#$::utils->sleep_ms(20);
$sta1_cmd = fmt_port_cmd($resource, $sta_name, $ip_addr, $mac_addr);
$::utils->doCmd($sta1_cmd);
#$::utils->sleep_ms(20);
#$::utils->doAsyncCmd($::utils->fmt_cmd("nc_show_port", 1, $::resource, $sta_name));
if ($::admin_down_on_add) {
my $cur_flags = 0x1; # port down
my $ist_flags = 0x800000; # port down
$sta1_cmd = $::utils->fmt_cmd("set_port", 1, $resource, $sta_name, "NA",
"NA", "NA", "NA", "$cur_flags",
"NA", "NA", "NA", "NA", "$ist_flags");
$::utils->doCmd($sta1_cmd);
#$::utils->sleep_ms(20);
}
#if ($sleep_amt > 0) {
# sleep $sleep_amt;
#}
my $data = [ $mac_addr, $sta_name, $sta1_cmd ];
$rh_results->{$sta_name} = $data;
}
sub new_wifi_radio {
my $cmd = fmt_vrad_cmd($::resource, $::sta_wiphy, $::vrad_chan );
$::utils->doCmd($cmd);
}
sub delete_port {
if (defined $::port_del) {
print "deleting port $::port_del\n" unless($::utils->isQuiet());
$::utils->doCmd($::utils->fmt_cmd("rm_vlan", 1, $::resource, $::port_del));
$::utils->sleep_ms(20);
}
}
sub get_sta_state {
my($rs_status) = @_;
die("is_assoc_state: wants ref to status string") unless($rs_status);
my @lines = split(/\r?\n/, $$rs_status);
my $careful = 0;
my $name = "unknown";
my $ip = "0.0.0.0";
my $assoc = "unknown";
my $first;
my $freq;
my @hunks;
my $mac;
my $gw;
my $mask;
my $channel;
my $mode;
my $probed_seen = 0;
for my $line (@lines) {
$first = "_";
my($key) = $line =~ m/^\s*([^:]+:)\s+/;
#print "{{{$key}}} $line\n";
next if ($line =~ /^\s*$/);
next if ($line =~ /RSLT:/);
last if ($line =~ /default@/);
$probed_seen++ if ($line =~ /Probed/);
if ($key && $key eq "MAC:" ) {
@hunks = split(/: /, $line);
$mac = (split(/ /, $hunks[1]))[0];
$name = (split(/ /, $hunks[2]))[0];
next;
}
if ($key && $key eq "IP:") {
@hunks = split(/: /, $line);
$ip = (split(/ /, $hunks[1]))[0];
$mask = (split(/ /, $hunks[2]))[0];
$gw = (split(/ /, $hunks[3]))[0];
next;
}
if ($probed_seen && ($line =~ /Mode:/)) {
@hunks = split(/: /, $line);
$careful = 1;
$mode = (split(/ /, $hunks[2]))[0];
next;
}
if( $probed_seen && $careful && ($key eq "Channel:")) {
@hunks = split(/: /, $line);
#print Dumper(\@hunks);
$channel = (split(/ /, $hunks[1]))[0];
$freq = (split(/ /, $hunks[3]))[0];
if ((@hunks > 3) && (defined $hunks[4])) {
$assoc = (split(/ /, $hunks[4]))[0];
}
}
}
my %rv = (
"assoc" => $assoc,
"freq" => $freq,
"ip" => $ip,
"mask" => $mask,
"gw" => $gw,
"mac" => $mac,
"mode" => $mode,
"name" => $name );
#print Dumper(\%rv);
return %rv;
}
sub awaitStationRemoval {
my $old_sta_count = (keys %::sta_names);
print "Waiting for $old_sta_count stations to be removed...";
while( $old_sta_count > 0 ) {
$old_sta_count = (keys %::sta_names);
for my $sta_name (sort(keys %::sta_names)) {
print " $sta_name,";
my $status = $::utils->doAsyncCmd($::utils->fmt_cmd("show_port", 1, $::resource, $sta_name));
$old_sta_count-- if( $status =~ m/Could not find/);
}
if ($old_sta_count > 0) {
#print "$old_sta_count...";
sleep 1;
}
}
print " Old stations removed\n";
}
#~expand to multiple cross-connects
sub removeOldCrossConnects {
print "Removing old cross-connects, and endpoints ...\n";
for my $sta_name (sort(keys(%::sta_names))) {
my $cx_name = $::cx_names{$sta_name}->{"cx"};
my $ep1 = $::cx_names{$sta_name}->{"ep1"};
my $ep2 = $::cx_names{$sta_name}->{"ep2"};
$::utils->doCmd("rm_cx $::test_mgr $cx_name");
$::utils->doCmd("rm_endp $ep1");
$::utils->doCmd("rm_endp $ep2");
print " $cx_name ($ep1 - $ep2)...";
}
print " done.\n";
}
sub removeOldStations {
print "Deleting ports:";
foreach my $sta_name (reverse sort(keys %::sta_names)) {
my $status = $::utils->doAsyncCmd($::utils->fmt_cmd("show_port", 1, $::resource, $sta_name));
if ($status =~ /Type:/) {
# It exists, remove it
#
print "...$sta_name ";
$::utils->doCmd($::utils->fmt_cmd("rm_vlan", 1, $::resource, $sta_name));
}
}
sleep(1);
# force a refresh on them so phantom doesn't show
foreach my $sta_name (reverse sort(keys %::sta_names)) {
my $status = $::utils->doAsyncCmd($::utils->fmt_cmd("nc_show_port", 1, $::resource, $sta_name));
}
print " done.\n";
}
sub awaitNewStations {
print "Waiting for stations to associate...";
my $new_sta_count = keys(%::sta_names);
my $found_stations = 0;
while( $new_sta_count > $found_stations ) {
$found_stations = 0;
my @are_assoc = ();
my @not_assoc = ();
for my $sta_name (sort(keys(%::sta_names))) {
my $status = $::utils->doAsyncCmd($::utils->fmt_cmd("show_port", 1, $::resource, $sta_name));
#print "STATUS: $status\n\n";
my %sta_status = get_sta_state(\$status);
#print " $sta_name ".$sta_status{"assoc"};
if( $sta_status{"assoc"} !~ /NA|Not-Associated|unknown/) {
push(@are_assoc, $sta_name);
}
else {
push(@not_assoc, $sta_name);
}
} #~foreach sta