-
Notifications
You must be signed in to change notification settings - Fork 5
/
rssi.php
275 lines (269 loc) · 15.5 KB
/
rssi.php
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
<?php
function logToConsole($string){ // simple console logger
echo date('Y-m-d H:i:s',time()).' | '.$string."\n";
}
function ascii2hex($ascii){ // convert ascii to hex
$hex=array();
for($i=0;$i<strlen($ascii);$i++){
$byte=strtoupper(dechex(ord(substr($ascii,$i,1))));
$byte=str_repeat('0',2-strlen($byte)).$byte;
$hex[]=$byte;
}
return implode($hex);
}
function getDeviceRSSI($mac){ // fetch station details using iw, check all adapters, and use the freshest one
global $data;
$final=array('freq'=>false);
$command=str_replace('{MAC}',$mac,$data['iwdumpcmd']);
$output=shell_exec($command);
if(preg_match('/Station\s'.preg_quote($mac).'\s\(on\s([^\)]+)\).*inactive\stime\:[\s\t]+([0-9]+)\sms.*rx\spackets\:[\s\t]+([0-9]+)[\s\t]+.*signal\:[\s\t]+\-([0-9]+)[\s\t]+.*connected\stime\:[\s\t]+([0-9]+)[\s\t]+/isU',$output,$matches)){
$freqmap=array_combine(array_column($data['adapters'],0),array_keys($data['adapters']));
$final['freq']=$freqmap[$matches[1]];
$final['rssi']=$matches[4];
$final['inactivetime']=$matches[2];
$final['connectedtime']=$matches[5];
$final['rxpackets']=$matches[3];
$final['apmac']=$data['adapters'][$freqmap[$matches[1]]][1];
$final['apdevice']=$matches[1];
}
// if freq is false, we can assume the client is not connected
if($final['freq']===false){
return false;
}
else{
return $final;
}
}
function fetchRssi($params,$timeout=2){ // send out a beacon request, and wait for hostapd_cli to store it in the temp file
global $data;
$target=neighborFromMac($params['targetmac'],false);
$parts=array();
// op class
$parts[]=str_pad(dechex($target['opclass']),2,'0',STR_PAD_LEFT);
// channel
$parts[]=str_pad(dechex($target['chan']),2,'0',STR_PAD_LEFT);
// randomization interval *** not sure what this is used for, sounds like how long to wait before starting a scan ***
$parts[]='0000';
// duration [ 100 dec = 64 hex ]
$parts[]='0064';
// mode [ 0 = passive, 1 = active, 2 = table ]
$parts[]='01';
// target mac
$parts[]=strtoupper(str_replace(':','',$params['targetmac']));
$packet=implode($parts);
// where we are expecting the beacon response to be saved to via the hostapd_cli -a script
$beaconfile='/tmp/beaconresp.'.$params['stamac'];
// send out the beacon request and wait for a response in the target file
$command='hostapd_cli -i '.$params['staadapter'].' req_beacon '.$params['stamac'].' '.$packet.' > /dev/null 2>&1 & inotifywait -t '.$timeout.' -q -q -e CLOSE_WRITE,CLOSE '.$beaconfile;
system($command,$ret);
if($ret<1){
$content=file_get_contents($beaconfile); // we have a response.
if($content=='FAILED'){
return NULL;
}
elseif(strlen($content)<64){ // 64 is just a random length, but these beacon responses are quite long, so 64 is enough
return false;
}
// parse out the returned mac address
$mac=substr($content,30,12);
if($mac!=str_replace(':','',$params['targetmac'])){
return NULL;
}
$rssi=unpack("l", pack("l", hexdec("FFFFFF".strtoupper(substr($content,26,2)))))[1]; // pull out the rssi from the beacon response
// the rssi is returned in a -XXdBm format, convert to positive to make more readable
return ($rssi*-1);
}
// no response in $timeout seconds
return false;
}
function neighborFromMac($mac,$returnparsed=true){ // parse out various details from a neighbor report and return as array or hostapd_cli neighbor=___ string
global $neigh;
if(!isset($neigh[$mac])){
return false;
}
preg_match('/^'.str_replace(':','',$mac).'([a-z0-9]{8})([a-z0-9]{2})([a-z0-9]{2})([a-z0-9]{2})/',$neigh[$mac],$matches);
$op=hexdec($matches[2]);
$chan=hexdec($matches[3]);
$phy=(int)$matches[4];
if($returnparsed){ // we are returning as a string for hostapd_cli to use
$final=$mac.',0x0000,'.$op.','.$chan.','.$phy;
}
else{ // we are returning as an array
$final=array();
$final['opclass']=$op;
$final['chan']=$chan;
$final['phytype']=$phy;
}
return $final;
}
// include the config file
include('rssi.config.php');
// create beacon temp files
foreach(array_keys($roamers) as $mac){
$filename='/tmp/beaconresp.'.$mac;
touch($filename);
}
// when the script is started, purge the neighbor report for each of the adapters
foreach($data['adapters'] as $freq=>$device){
if($data['servertype']=='openwrt'){
$command='ubus call hostapd.'.$device[0].' rrm_nr_set \'{"list":[]}\'';
system($command);
}
elseif($data['servertype']=='hostapd'){
$output=array();
exec('hostapd_cli -i '.$device[0].' show_neighbor',$output);
$output=implode("\n",$output);
if(preg_match_all('/([a-z0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2})/i',$output,$matches)){
foreach($matches[1] as $mac){
// do not remove our own neighbor report
if($mac==$device[1]){
continue;
}
system('hostapd_cli -i '.$device[0].' remove_neighbor '.$mac.' > /dev/null 2>&1');
}
}
}
}
// this script should never timeout
set_time_limit('0');
// infinite loop starts
while(true){
// just in case hostapd was restarted or maybe dropped the neighbor reports (not sure if they expire?), update them every 2 minutes
if(time()-$data['lastneighupdate']>=120){
$data['lastneighupdate']=time();
foreach($data['adapters'] as $freq=>$device){
// pull the neighbor table from hostapd to prevent re-entering the same entry every time this block is executed
$present=array();
if($data['servertype']=='hostapd'){
$command='hostapd_cli -i '.$device[0].' show_neighbor';
}
else{
$command='ubus call hostapd.'.$device[0].' rrm_nr_list';
}
$output=array();
exec($command,$output);
// yes, ubus returns a nice json but just incase someone out there does not have the json module installed due to storage considerations, just preg_match it
if(preg_match_all('/([a-z0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2})/i',implode("\n",$output),$matches)){
$present=$matches[1];
}
foreach($neigh as $mac=>$nr){
// do not overwrite our own neighbor report
if(($freq==2&&isset($data['adapters'][2][1])&&$data['adapters'][2][1]==$mac)||($freq==5&&isset($data['adapters'][5][1])&&$data['adapters'][5][1]==$mac)){
continue;
}
elseif(in_array($mac,$present)){ // do not overwrite / reinsert already stored neighbors
continue;
}
// add the neighbor report entry to the list
$command='hostapd_cli -i '.$device[0].' set_neighbor '.$mac.' ssid='.strtolower(ascii2hex($data['bssid'])).' nr='.$nr.' > /dev/null 2>&1';
system($command);
}
}
}
// i only have 1 roamer i need to worry about, but this lays the foundation for making this script handle more than one
foreach($roamers as $mac=>$devicedata){
if(!$rssidata=getDeviceRSSI($mac)){ // fetch rssi via iw, if not connected, wait 50ms and try again
$roamers[$mac]['failedbeacons']=0;
continue;
}
elseif($rssidata['connectedtime']<1){ // wait until client has been connected for at least 1 second
$roamers[$mac]['failedbeacons']=0;
continue;
}
// fetch own rssi so its nice and fresh
$start=microtime(true);
$params=array();
$params['stamac']=$mac;
$params['staadapter']=$rssidata['apdevice'];
$params['targetmac']=$rssidata['apmac'];
$params['targetnr']=$neigh[$rssidata['apmac']];
$result=fetchRssi($params,2);
$rssidata['fastact']=(microtime(true)-$start<=0.5?true:false);
if($result===NULL||$result===false){
// track and handle failed beacons
$roamers[$mac]['failedbeacons']++;
if($roamers[$mac]['failedbeacons']>=10){
// make sure no activity took place durring beacon check
$rxpackets=$rssidata['rxpackets'];
$rssidata=getDeviceRSSI($mac);
if(!isset($rssidata['rxpackets'])||$rssidata['rxpackets']!=$rxpackets){
continue;
}
// deauth this client as its not responding to beacon requests ( ack=0 )
$command='hostapd_cli -i '.$rssidata['apdevice'].' deauthenticate '.$mac.' 2>&1';
logToConsole('['.$mac.' @ '.$rssidata['freq'].':'.$rssidata['rssi'].'] Deauthenticating '.$mac.' via '.$command.' ['.trim(shell_exec($command)).']');
$roamers[$mac]['failedbeacons']=0;
continue;
}
continue;
}
$roamers[$mac]['failedbeacons']=0;
$rssidata['rssi']=$result;
if(isset($data['adapters']['5'])&&$rssidata['freq']==5&&$rssidata['rssi']>=$data['rssi-5-to-other']){ // connected at 5 + rssi above rssi-5-to-other = try to roam to $beaconcheck[5]
$candidates=array();
foreach($beaconcheck[5] as $key=>$target){
$params=array();
$params['stamac']=$mac;
$params['staadapter']=$data['adapters'][5][0];
$params['targetmac']=$target;
$params['targetnr']=$neigh[$target];
$result=fetchRssi($params,($rssidata['fastact']?1:2));
if($result!==NULL&&$result!==false&&$result<=$data['rssi-5-to-other-min']){
$command='hostapd_cli -i '.$data['adapters'][5][0].' bss_tm_req '.$mac.' neighbor='.neighborFromMac($target).' pref=1 abridged=1 2>&1';
logToConsole('['.$mac.' @ '.$rssidata['freq'].':'.$rssidata['rssi'].'] Candidate '.$target.' has rssi of '.$result.', forced roam via '.$command.' ['.trim(shell_exec($command)).']');
sleep(2);
continue 2;
}
$candidates[$target]=($result?$result:81);
}
asort($candidates);
$beaconcheck[5]=array_keys($candidates);
}
elseif(isset($data['adapters']['2'])&&$rssidata['freq']==2&&$rssidata['rssi']<=$data['rssi-2-to-5']){ // connected at 2 + rssi below rssi-2-to-5 = try to roam to same ap 5
$params=array();
$params['stamac']=$mac;
$params['staadapter']=$data['adapters'][2][0];
$params['targetmac']=$data['adapters'][5][1];
$params['targetnr']=$neigh[$data['adapters'][5][1]];
$result=fetchRssi($params,($rssidata['fastact']?1:2));
if($result!==NULL&&$result!==false&&$result<=$data['rssi-2-to-5-min']){
$command='hostapd_cli -i '.$data['adapters'][2][0].' bss_tm_req '.$mac.' neighbor='.neighborFromMac($data['adapters'][5][1]).' pref=1 abridged=1 2>&1';
logToConsole('['.$mac.' @ '.$rssidata['freq'].':'.$rssidata['rssi'].'] 5ghz band has rssi of '.$result.', forced roam via '.$command.' ['.trim(shell_exec($command)).']');
$start=time();
while(time()-$start<=3){
$roamdata=getDeviceRSSI($mac);
if(!is_array($roamdata)){
usleep(50000);
continue;
}
elseif($roamdata['freq']==5){
logToConsole('['.$mac.' @ '.$rssidata['freq'].':'.$rssidata['rssi'].'] Success roaming to 5ghz!');
break;
}
usleep(50000);
}
}
}
elseif(isset($data['adapters']['2'])&&$rssidata['freq']==2&&$rssidata['rssi']>=$data['rssi-2-to-other']){ // connected at 2 + rssi above rssi-2-to-other = try to roam to $beaconcheck[2]
$candidates=array();
foreach($beaconcheck[2] as $key=>$target){
$params=array();
$params['stamac']=$mac;
$params['staadapter']=$data['adapters'][2][0];
$params['targetmac']=$target;
$params['targetnr']=$neigh[$target];
$result=fetchRssi($params,($rssidata['fastact']?1:2));
if($result!==NULL&&$result!==false&&$result<=$data['rssi-2-to-other-min']){
$command='hostapd_cli -i '.$data['adapters'][2][0].' bss_tm_req '.$mac.' neighbor='.neighborFromMac($target).' pref=1 abridged=1 2>&1';
logToConsole('['.$mac.' @ '.$rssidata['freq'].':'.$rssidata['rssi'].'] Candidate '.$target.' has rssi of '.$result.', forced roam via '.$command.' ['.trim(shell_exec($command)).']');
break;
}
$candidates[$target]=($result?$result:81);
}
asort($candidates);
$beaconcheck[2]=array_keys($candidates);
}
}
usleep(500000);
}
?>