2016-10-05 01:44:27 +02:00
|
|
|
#!/usr/bin/python3
|
2017-03-26 03:16:53 +02:00
|
|
|
import os, os.path
|
2018-01-30 20:19:49 +01:00
|
|
|
import re
|
2018-05-31 01:39:05 +02:00
|
|
|
import socket
|
2019-10-29 18:50:39 +01:00
|
|
|
import select
|
|
|
|
import time
|
|
|
|
from gi.repository import GLib
|
2021-08-26 22:33:32 +02:00
|
|
|
from weakref import WeakValueDictionary
|
2020-09-11 01:12:24 +02:00
|
|
|
from config import ctx
|
2018-01-30 20:19:49 +01:00
|
|
|
|
|
|
|
chan_freq_map = [
|
|
|
|
None,
|
|
|
|
2412,
|
|
|
|
2417,
|
|
|
|
2422,
|
|
|
|
2427,
|
|
|
|
2432,
|
|
|
|
2437,
|
|
|
|
2442,
|
|
|
|
2447,
|
|
|
|
2452,
|
|
|
|
2457,
|
|
|
|
2462,
|
|
|
|
2467,
|
|
|
|
2472,
|
|
|
|
2484
|
|
|
|
]
|
2017-03-26 03:16:53 +02:00
|
|
|
|
2019-10-29 18:50:39 +01:00
|
|
|
ctrl_count = 0
|
|
|
|
mainloop = GLib.MainLoop()
|
|
|
|
|
2021-08-14 00:45:51 +02:00
|
|
|
class HostapdCLI(object):
|
2021-08-26 22:33:32 +02:00
|
|
|
_instances = WeakValueDictionary()
|
2021-08-14 00:45:51 +02:00
|
|
|
|
|
|
|
def __new__(cls, config=None, *args, **kwargs):
|
2022-07-09 02:23:32 +02:00
|
|
|
hapd = ctx.get_hapd_instance(config)
|
2021-08-14 00:45:51 +02:00
|
|
|
|
|
|
|
if not config:
|
|
|
|
config = hapd.config
|
|
|
|
|
|
|
|
if not config in cls._instances.keys():
|
2021-08-26 22:33:32 +02:00
|
|
|
obj = object.__new__(cls, *args, **kwargs)
|
|
|
|
obj._initialized = False
|
|
|
|
|
|
|
|
cls._instances[config] = obj
|
2021-08-14 00:45:51 +02:00
|
|
|
|
|
|
|
return cls._instances[config]
|
|
|
|
|
2021-08-23 20:32:21 +02:00
|
|
|
def __init__(self, config=None, *args, **kwargs):
|
2019-10-29 18:50:39 +01:00
|
|
|
global ctrl_count
|
2021-08-14 00:45:51 +02:00
|
|
|
|
2021-08-23 20:32:21 +02:00
|
|
|
if self._initialized:
|
2021-08-14 00:45:51 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
self._initialized = True
|
2020-10-20 20:02:44 +02:00
|
|
|
self.ctrl_sock = None
|
|
|
|
|
|
|
|
if not ctx.hostapd:
|
|
|
|
raise Exception("No hostapd instances are configured")
|
2020-09-11 01:12:24 +02:00
|
|
|
|
2022-07-09 02:23:32 +02:00
|
|
|
if not config and sum([len(hapd.instances) for hapd in ctx.hostapd]) > 1:
|
2020-09-11 01:12:24 +02:00
|
|
|
raise Exception('config must be provided if more than one hostapd instance exists')
|
2019-10-29 18:50:39 +01:00
|
|
|
|
2022-07-09 02:23:32 +02:00
|
|
|
hapd = ctx.get_hapd_instance(config)
|
2019-06-05 23:02:25 +02:00
|
|
|
|
2020-09-11 01:12:24 +02:00
|
|
|
self.interface = hapd.intf
|
2023-08-29 16:39:54 +02:00
|
|
|
self._config_path = "/tmp/" + hapd.config
|
|
|
|
self._default_config = self._get_config(self._config_path)
|
|
|
|
|
|
|
|
# The vendor_elements is somewhat of a special case because you can't
|
|
|
|
# set it to 'nothing' within the hostapd config. In most cases tests do
|
|
|
|
# not use this and we don't want to require they unset this value
|
|
|
|
# during initialization. Default this to '' so set_value won't throw
|
|
|
|
# an exception.
|
|
|
|
if self._default_config.get('vendor_elements', None) == None:
|
|
|
|
self._default_config['vendor_elements'] = ''
|
2019-06-05 23:02:25 +02:00
|
|
|
|
2023-12-06 21:17:52 +01:00
|
|
|
self._current_config = self._default_config.copy()
|
|
|
|
|
2020-09-11 01:12:24 +02:00
|
|
|
if not self.interface:
|
2019-10-22 18:14:00 +02:00
|
|
|
raise Exception('config %s not found' % config)
|
|
|
|
|
2020-09-11 01:12:24 +02:00
|
|
|
self.ifname = self.interface.name
|
|
|
|
self.socket_path = os.path.dirname(self.interface.ctrl_interface)
|
2016-10-06 20:06:14 +02:00
|
|
|
|
2020-09-11 01:12:24 +02:00
|
|
|
self.cmdline = ['hostapd_cli', '-p', self.socket_path, '-i', self.ifname]
|
2017-03-26 03:16:53 +02:00
|
|
|
|
2019-10-29 18:50:39 +01:00
|
|
|
self.local_ctrl = '/tmp/hostapd_' + str(os.getpid()) + '_' + \
|
|
|
|
str(ctrl_count)
|
|
|
|
self.ctrl_sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
|
|
|
|
self.ctrl_sock.bind(self.local_ctrl)
|
2020-09-11 01:12:24 +02:00
|
|
|
|
2019-10-29 18:50:39 +01:00
|
|
|
self.ctrl_sock.connect(self.socket_path + '/' + self.ifname)
|
|
|
|
|
2022-04-01 01:16:39 +02:00
|
|
|
self.events = []
|
|
|
|
self.io_watch = GLib.io_add_watch(self.ctrl_sock, GLib.IO_IN, self._handle_data_in)
|
|
|
|
|
2019-10-29 18:50:39 +01:00
|
|
|
if 'OK' not in self._ctrl_request('ATTACH'):
|
|
|
|
raise Exception('ATTACH failed')
|
|
|
|
|
|
|
|
ctrl_count = ctrl_count + 1
|
|
|
|
|
2023-08-29 16:39:54 +02:00
|
|
|
def _get_config(self, path):
|
|
|
|
f = open(path)
|
|
|
|
lines = f.readlines()
|
|
|
|
f.close()
|
|
|
|
return dict([[v.strip() for v in kv] for kv in [l.split('#', 1)[0].split('=', 1) for l in lines] if len(kv) == 2])
|
|
|
|
|
2022-04-01 01:16:39 +02:00
|
|
|
def _handle_data_in(self, sock, *args):
|
|
|
|
newdata = sock.recv(4096)
|
|
|
|
|
|
|
|
decoded = newdata.decode('utf-8')
|
|
|
|
if len(decoded) >= 3 and decoded[0] == '<' and decoded[2] == '>':
|
|
|
|
decoded = decoded[3:]
|
|
|
|
while len(decoded) and decoded[-1] == '\n':
|
|
|
|
decoded = decoded[:-1]
|
|
|
|
|
|
|
|
self.events.insert(0, decoded)
|
|
|
|
|
|
|
|
return True
|
2019-10-29 18:50:39 +01:00
|
|
|
|
2022-06-25 01:07:38 +02:00
|
|
|
def _poll_event(self, event, disallow):
|
2022-04-01 01:16:39 +02:00
|
|
|
# Look through the list (most recent is first) until the even is found.
|
|
|
|
# Once found consume this event and any older ones as to not
|
|
|
|
# accidentally trigger a false positive later on.
|
|
|
|
for idx, e in enumerate(self.events):
|
2022-06-25 01:07:38 +02:00
|
|
|
for d in disallow:
|
|
|
|
if d in e:
|
|
|
|
raise Exception('Event %s found while waiting for %s' % (d, event))
|
2022-04-01 01:16:39 +02:00
|
|
|
if event in e:
|
|
|
|
self.events = self.events[:idx]
|
|
|
|
return e
|
2019-10-29 18:50:39 +01:00
|
|
|
|
2021-08-13 18:24:39 +02:00
|
|
|
return False
|
2019-10-29 18:50:39 +01:00
|
|
|
|
2022-06-25 01:07:38 +02:00
|
|
|
def wait_for_event(self, event, timeout=10, disallow=[]):
|
2022-04-01 01:16:39 +02:00
|
|
|
if event == 'AP-ENABLED':
|
|
|
|
if self.enabled:
|
|
|
|
return 'AP-ENABLED'
|
|
|
|
|
2022-06-25 01:07:38 +02:00
|
|
|
return ctx.non_block_wait(self._poll_event, timeout, event, disallow,
|
2021-08-13 18:24:39 +02:00
|
|
|
exception=TimeoutError("waiting for event"))
|
2019-10-29 18:50:39 +01:00
|
|
|
|
2022-04-01 01:16:39 +02:00
|
|
|
def _data_available(self):
|
|
|
|
[r, w, e] = select.select([self.ctrl_sock], [], [])
|
2019-10-29 18:50:39 +01:00
|
|
|
if r:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _ctrl_request(self, command, timeout=10):
|
|
|
|
if type(command) is str:
|
|
|
|
command = str.encode(command)
|
|
|
|
|
|
|
|
self.ctrl_sock.send(bytes(command))
|
|
|
|
|
2021-08-13 18:24:39 +02:00
|
|
|
ctx.non_block_wait(self._data_available, timeout,
|
|
|
|
exception=TimeoutError("waiting for control response"))
|
2019-10-29 18:50:39 +01:00
|
|
|
|
2021-08-13 18:24:39 +02:00
|
|
|
return self.ctrl_sock.recv(4096).decode('utf-8')
|
2019-10-29 18:50:39 +01:00
|
|
|
|
2021-08-23 20:32:21 +02:00
|
|
|
def __del__(self):
|
|
|
|
if self.ctrl_sock:
|
|
|
|
self.ctrl_sock.close()
|
2021-07-30 00:21:47 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove(self.local_ctrl)
|
|
|
|
except:
|
|
|
|
pass
|
2020-07-14 20:49:07 +02:00
|
|
|
|
2021-07-14 17:42:03 +02:00
|
|
|
def set_value(self, key, value):
|
2023-08-29 16:39:54 +02:00
|
|
|
# Don't allow new settings, defaults should always be in hostapd config
|
|
|
|
if self._default_config.get(key, None) == None:
|
|
|
|
raise Exception("Untracked setting '%s'! Please set default in hostapd config" % key)
|
|
|
|
|
2023-12-06 21:17:52 +01:00
|
|
|
self._current_config[key] = value
|
|
|
|
|
2021-07-14 17:42:03 +02:00
|
|
|
cmd = self.cmdline + ['set', key, value]
|
2021-08-26 00:17:24 +02:00
|
|
|
ctx.start_process(cmd).wait()
|
2021-07-14 17:42:03 +02:00
|
|
|
|
2017-03-26 03:16:53 +02:00
|
|
|
def wps_push_button(self):
|
2021-08-26 00:17:24 +02:00
|
|
|
ctx.start_process(self.cmdline + ['wps_pbc']).wait()
|
2017-03-26 03:16:53 +02:00
|
|
|
|
2018-09-24 19:02:34 +02:00
|
|
|
def wps_pin(self, pin):
|
2020-09-11 01:12:24 +02:00
|
|
|
cmd = self.cmdline + ['wps_pin', 'any', pin]
|
2021-08-26 00:17:24 +02:00
|
|
|
ctx.start_process(cmd).wait()
|
2018-09-24 19:02:34 +02:00
|
|
|
|
2017-03-26 03:16:53 +02:00
|
|
|
def deauthenticate(self, client_address):
|
2020-09-11 01:12:24 +02:00
|
|
|
cmd = self.cmdline + ['deauthenticate', client_address]
|
2021-08-26 00:17:24 +02:00
|
|
|
ctx.start_process(cmd).wait()
|
2016-10-06 20:06:14 +02:00
|
|
|
|
2018-05-31 01:39:05 +02:00
|
|
|
def eapol_reauth(self, client_address):
|
2022-05-24 22:31:44 +02:00
|
|
|
self.events = []
|
|
|
|
cmd = 'EAPOL_REAUTH ' + client_address
|
2020-09-11 01:12:24 +02:00
|
|
|
self.ctrl_sock.sendall(cmd.encode('utf-8'))
|
2022-06-25 01:07:38 +02:00
|
|
|
self.wait_for_event('CTRL-EVENT-EAP-STARTED', disallow=['AP-STA-DISCONNECTED'])
|
|
|
|
self.wait_for_event('CTRL-EVENT-EAP-SUCCESS', disallow=['AP-STA-DISCONNECTED'])
|
2018-05-31 01:39:05 +02:00
|
|
|
|
2017-03-26 03:16:57 +02:00
|
|
|
def reload(self):
|
|
|
|
# Seemingly all three commands needed for the instance to notice
|
|
|
|
# interface's address change
|
2021-08-26 00:17:24 +02:00
|
|
|
ctx.start_process(self.cmdline + ['reload']).wait()
|
|
|
|
ctx.start_process(self.cmdline + ['disable']).wait()
|
|
|
|
ctx.start_process(self.cmdline + ['enable']).wait()
|
2017-03-26 03:16:57 +02:00
|
|
|
|
2023-08-29 16:39:54 +02:00
|
|
|
def default(self):
|
|
|
|
for k, v in self._default_config.items():
|
2023-12-06 21:17:52 +01:00
|
|
|
# Only bother setting the value if it differs
|
|
|
|
if self._current_config[k] != v:
|
|
|
|
self.set_value(k, v)
|
|
|
|
self._current_config[k] = v
|
2023-08-29 16:39:54 +02:00
|
|
|
|
|
|
|
self.reload()
|
|
|
|
|
2021-09-15 19:36:23 +02:00
|
|
|
def disable(self):
|
|
|
|
ctx.start_process(self.cmdline + ['disable']).wait()
|
|
|
|
|
2017-03-26 03:16:57 +02:00
|
|
|
def list_sta(self):
|
2021-08-26 00:17:24 +02:00
|
|
|
proc = ctx.start_process(self.cmdline + ['list_sta'])
|
|
|
|
proc.wait()
|
2017-03-26 03:16:57 +02:00
|
|
|
|
2020-11-16 23:25:11 +01:00
|
|
|
if not proc.out:
|
|
|
|
return []
|
|
|
|
|
|
|
|
return [line for line in proc.out.split('\n') if line]
|
2017-03-26 03:16:57 +02:00
|
|
|
|
|
|
|
def set_neighbor(self, addr, ssid, nr):
|
2020-09-11 20:11:28 +02:00
|
|
|
cmd = self.cmdline + ['set_neighbor', addr, 'ssid="%s"' % ssid, 'nr=%s' % nr]
|
2021-08-26 00:17:24 +02:00
|
|
|
ctx.start_process(cmd).wait()
|
2017-03-26 03:16:57 +02:00
|
|
|
|
2022-01-19 23:26:51 +01:00
|
|
|
def remove_neighbor(self, addr):
|
|
|
|
cmd = self.cmdline + ['remove_neighbor', addr]
|
|
|
|
ctx.start_process(cmd).wait()
|
|
|
|
|
2022-09-28 18:36:33 +02:00
|
|
|
def send_bss_transition(self, device, nr_list, disassoc_imminent=True):
|
2018-01-18 22:36:39 +01:00
|
|
|
# Send a BSS transition to a station (device). nr_list should be an
|
|
|
|
# array of tuples containing the BSS address and neighbor report.
|
|
|
|
# Parsing the neighbor report is a bit ugly but it makes it more
|
|
|
|
# consistent with the set_neighbor() API, i.e. the same neighbor report
|
|
|
|
# string could be used in both API's.
|
|
|
|
pref = 1
|
2020-09-11 01:12:24 +02:00
|
|
|
cmd = self.cmdline + ['bss_tm_req', device]
|
2022-09-28 18:36:33 +02:00
|
|
|
|
|
|
|
if disassoc_imminent:
|
|
|
|
cmd.append('disassoc_imminent=1')
|
|
|
|
|
2018-01-18 22:36:39 +01:00
|
|
|
for i in nr_list:
|
|
|
|
addr = i[0]
|
|
|
|
nr = i[1]
|
|
|
|
|
|
|
|
bss_info=str(int(nr[0:8], 16))
|
|
|
|
op_class=str(int(nr[8:10], 16))
|
|
|
|
chan_num=nr[10:12]
|
|
|
|
phy_num=nr[14:16]
|
|
|
|
|
2020-09-11 01:12:24 +02:00
|
|
|
cmd += ['pref=%s' % str(pref), 'neighbor=%s,%s,%s,%s,%s' % \
|
|
|
|
(addr, bss_info, op_class, chan_num, phy_num)]
|
2018-01-18 22:36:39 +01:00
|
|
|
pref += 1
|
|
|
|
|
2021-08-26 00:17:24 +02:00
|
|
|
proc = ctx.start_process(cmd)
|
|
|
|
proc.wait()
|
2021-01-28 10:26:03 +01:00
|
|
|
|
|
|
|
if 'OK' not in proc.out:
|
|
|
|
raise Exception('BSS_TM_REQ failed, is hostapd built with CONFIG_WNM_AP=y?')
|
2018-01-29 19:37:05 +01:00
|
|
|
|
2019-10-29 18:50:39 +01:00
|
|
|
def req_beacon(self, addr, request):
|
|
|
|
'''
|
|
|
|
Send a RRM Beacon request
|
|
|
|
'''
|
2020-09-11 01:12:24 +02:00
|
|
|
cmd = self.cmdline + ['req_beacon', addr, request]
|
2021-08-26 00:17:24 +02:00
|
|
|
ctx.start_process(cmd).wait()
|
2021-08-12 22:21:50 +02:00
|
|
|
|
2021-10-07 22:49:54 +02:00
|
|
|
def rekey(self, address=None):
|
|
|
|
if address:
|
|
|
|
cmd = 'REKEY_PTK %s' % address
|
|
|
|
self.ctrl_sock.sendall(cmd.encode('utf-8'))
|
2022-06-25 01:07:38 +02:00
|
|
|
self.events = []
|
|
|
|
self.wait_for_event('EAPOL-4WAY-HS-COMPLETED', disallow=['AP-STA-DISCONNECTED'])
|
2021-10-07 22:49:54 +02:00
|
|
|
return
|
|
|
|
|
2021-09-29 00:36:08 +02:00
|
|
|
cmd = 'REKEY_GTK'
|
|
|
|
self.ctrl_sock.sendall(cmd.encode('utf-8'))
|
|
|
|
|
2021-10-07 22:49:55 +02:00
|
|
|
def resend_m3(self, address):
|
|
|
|
cmd = 'RESEND_M3 %s' % address
|
|
|
|
self.ctrl_sock.sendall(cmd.encode('utf-8'))
|
|
|
|
|
2021-10-19 20:19:09 +02:00
|
|
|
def chan_switch(self, channel):
|
|
|
|
if channel > len(chan_freq_map):
|
|
|
|
raise Exception("Only 2.4GHz channels supported for chan_switch")
|
|
|
|
|
|
|
|
cmd = self.cmdline + ['chan_switch', '50', str(chan_freq_map[channel])]
|
|
|
|
ctx.start_process(cmd).wait()
|
|
|
|
self.wait_for_event('AP-CSA-FINISHED')
|
|
|
|
|
2022-04-01 01:16:39 +02:00
|
|
|
def _get_status(self):
|
|
|
|
ret = {}
|
|
|
|
|
2021-08-12 22:21:50 +02:00
|
|
|
cmd = self.cmdline + ['status']
|
2021-08-26 00:17:24 +02:00
|
|
|
proc = ctx.start_process(cmd)
|
|
|
|
proc.wait()
|
2022-04-01 01:16:39 +02:00
|
|
|
status = proc.out.strip().split('\n')
|
2021-08-12 22:21:50 +02:00
|
|
|
|
2022-04-01 01:16:39 +02:00
|
|
|
for kv in status:
|
|
|
|
k, v = kv.split('=')
|
|
|
|
ret[k] = v
|
|
|
|
|
|
|
|
return ret
|
2021-08-13 01:12:30 +02:00
|
|
|
|
2024-02-27 19:34:02 +01:00
|
|
|
def sta_status(self, address):
|
|
|
|
ret = {}
|
|
|
|
|
|
|
|
cmd = self.cmdline + ['sta', address]
|
|
|
|
proc = ctx.start_process(cmd)
|
|
|
|
proc.wait()
|
|
|
|
status = proc.out.strip().split('\n')
|
|
|
|
|
|
|
|
# Pop address
|
|
|
|
status.pop(0)
|
|
|
|
for kv in status:
|
|
|
|
k, v = kv.split('=', 1)
|
|
|
|
ret[k] = v
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
2021-08-13 01:12:30 +02:00
|
|
|
@property
|
2022-04-01 01:16:39 +02:00
|
|
|
def bssid(self):
|
|
|
|
return self._get_status()['bssid[0]']
|
2021-08-13 01:12:30 +02:00
|
|
|
|
2022-04-01 01:16:39 +02:00
|
|
|
@property
|
|
|
|
def frequency(self):
|
|
|
|
return int(self._get_status()['freq'])
|
2021-08-13 01:12:30 +02:00
|
|
|
|
2022-04-01 01:16:39 +02:00
|
|
|
@property
|
|
|
|
def enabled(self):
|
|
|
|
return self._get_status()['state'] == 'ENABLED'
|
2022-08-22 19:01:47 +02:00
|
|
|
|
|
|
|
def set_address(self, mac):
|
|
|
|
os.system('ip link set dev %s down' % self.ifname)
|
|
|
|
os.system('ip link set dev %s addr %s up' % (self.ifname, mac))
|
|
|
|
|
|
|
|
self.reload()
|
|
|
|
self.wait_for_event("AP-ENABLED")
|
|
|
|
|
|
|
|
def _add_neighbors(self, *args, op_class=81):
|
|
|
|
for hapd in args:
|
|
|
|
status = hapd._get_status()
|
|
|
|
|
|
|
|
ssid = status['ssid[0]']
|
|
|
|
bssid = status['bssid[0]']
|
|
|
|
channel = int(status['channel'])
|
|
|
|
|
|
|
|
if (channel > 14 and op_class == 81):
|
|
|
|
raise Exception("default add_neighbors assumes opclass 0x51!")
|
|
|
|
|
|
|
|
channel = '{:02x}'.format(channel)
|
|
|
|
oper_class = '{:02x}'.format(op_class)
|
|
|
|
|
|
|
|
self.set_neighbor(bssid, ssid, '%s8f000000%s%s060603000000' %
|
|
|
|
(bssid.replace(':', ''), oper_class, channel))
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def group_neighbors(cls, *args):
|
|
|
|
for hapd in args:
|
|
|
|
others = [h for h in args if h != hapd]
|
|
|
|
|
|
|
|
hapd._add_neighbors(*others)
|