2013-03-29 17:37:03 +01:00
|
|
|
# Python class for controlling hostapd
|
2019-01-04 19:35:47 +01:00
|
|
|
# Copyright (c) 2013-2019, Jouni Malinen <j@w1.fi>
|
2013-03-29 17:37:03 +01:00
|
|
|
#
|
|
|
|
# This software may be distributed under the terms of the BSD license.
|
|
|
|
# See README for more details.
|
|
|
|
|
|
|
|
import os
|
2020-01-12 23:02:24 +01:00
|
|
|
import re
|
2013-03-29 17:37:03 +01:00
|
|
|
import time
|
|
|
|
import logging
|
2013-12-26 09:52:11 +01:00
|
|
|
import binascii
|
|
|
|
import struct
|
2020-02-23 16:38:23 +01:00
|
|
|
import tempfile
|
2013-03-29 17:37:03 +01:00
|
|
|
import wpaspy
|
2016-03-08 14:28:04 +01:00
|
|
|
import remotehost
|
2016-04-07 07:38:02 +02:00
|
|
|
import utils
|
2016-06-07 15:42:40 +02:00
|
|
|
import subprocess
|
2013-03-29 17:37:03 +01:00
|
|
|
|
2013-10-31 11:46:42 +01:00
|
|
|
logger = logging.getLogger()
|
2013-03-29 17:37:03 +01:00
|
|
|
hapd_ctrl = '/var/run/hostapd'
|
2013-05-10 16:09:55 +02:00
|
|
|
hapd_global = '/var/run/hostapd-global'
|
2013-03-29 17:37:03 +01:00
|
|
|
|
2013-12-26 09:52:11 +01:00
|
|
|
def mac2tuple(mac):
|
2019-03-15 11:10:37 +01:00
|
|
|
return struct.unpack('6B', binascii.unhexlify(mac.replace(':', '')))
|
2013-12-26 09:52:11 +01:00
|
|
|
|
2013-03-29 17:37:03 +01:00
|
|
|
class HostapdGlobal:
|
2019-01-04 19:35:47 +01:00
|
|
|
def __init__(self, apdev=None, global_ctrl_override=None):
|
2016-04-07 07:38:03 +02:00
|
|
|
try:
|
|
|
|
hostname = apdev['hostname']
|
|
|
|
port = apdev['port']
|
|
|
|
except:
|
|
|
|
hostname = None
|
|
|
|
port = 8878
|
2016-03-08 14:28:04 +01:00
|
|
|
self.host = remotehost.Host(hostname)
|
2016-03-04 10:20:35 +01:00
|
|
|
self.hostname = hostname
|
|
|
|
self.port = port
|
|
|
|
if hostname is None:
|
2019-01-04 19:35:47 +01:00
|
|
|
global_ctrl = hapd_global
|
|
|
|
if global_ctrl_override:
|
|
|
|
global_ctrl = global_ctrl_override
|
|
|
|
self.ctrl = wpaspy.Ctrl(global_ctrl)
|
|
|
|
self.mon = wpaspy.Ctrl(global_ctrl)
|
2016-03-08 14:28:05 +01:00
|
|
|
self.dbg = ""
|
2016-03-04 10:20:35 +01:00
|
|
|
else:
|
|
|
|
self.ctrl = wpaspy.Ctrl(hostname, port)
|
|
|
|
self.mon = wpaspy.Ctrl(hostname, port)
|
2016-03-08 14:28:05 +01:00
|
|
|
self.dbg = hostname + "/" + str(port)
|
2014-08-05 17:25:59 +02:00
|
|
|
self.mon.attach()
|
|
|
|
|
2016-06-27 19:10:23 +02:00
|
|
|
def cmd_execute(self, cmd_array, shell=False):
|
2016-06-07 15:42:40 +02:00
|
|
|
if self.hostname is None:
|
2016-06-27 19:10:23 +02:00
|
|
|
if shell:
|
|
|
|
cmd = ' '.join(cmd_array)
|
|
|
|
else:
|
|
|
|
cmd = cmd_array
|
2016-06-07 15:42:40 +02:00
|
|
|
proc = subprocess.Popen(cmd, stderr=subprocess.STDOUT,
|
2016-06-27 19:10:23 +02:00
|
|
|
stdout=subprocess.PIPE, shell=shell)
|
2016-06-07 15:42:40 +02:00
|
|
|
out = proc.communicate()[0]
|
|
|
|
ret = proc.returncode
|
2019-02-02 11:48:30 +01:00
|
|
|
return ret, out.decode()
|
2016-06-07 15:42:40 +02:00
|
|
|
else:
|
|
|
|
return self.host.execute(cmd_array)
|
|
|
|
|
2016-03-08 14:28:05 +01:00
|
|
|
def request(self, cmd, timeout=10):
|
|
|
|
logger.debug(self.dbg + ": CTRL(global): " + cmd)
|
|
|
|
return self.ctrl.request(cmd, timeout)
|
2014-08-05 17:25:59 +02:00
|
|
|
|
|
|
|
def wait_event(self, events, timeout):
|
|
|
|
start = os.times()[4]
|
|
|
|
while True:
|
|
|
|
while self.mon.pending():
|
|
|
|
ev = self.mon.recv()
|
2016-03-08 14:28:05 +01:00
|
|
|
logger.debug(self.dbg + "(global): " + ev)
|
2014-08-05 17:25:59 +02:00
|
|
|
for event in events:
|
|
|
|
if event in ev:
|
|
|
|
return ev
|
|
|
|
now = os.times()[4]
|
|
|
|
remaining = start + timeout - now
|
|
|
|
if remaining <= 0:
|
|
|
|
break
|
|
|
|
if not self.mon.pending(timeout=remaining):
|
|
|
|
break
|
|
|
|
return None
|
2013-03-29 17:37:03 +01:00
|
|
|
|
2015-07-17 22:57:04 +02:00
|
|
|
def add(self, ifname, driver=None):
|
|
|
|
cmd = "ADD " + ifname + " " + hapd_ctrl
|
|
|
|
if driver:
|
|
|
|
cmd += " " + driver
|
2016-03-08 14:28:05 +01:00
|
|
|
res = self.request(cmd)
|
2019-03-15 10:34:32 +01:00
|
|
|
if "OK" not in res:
|
2013-03-29 17:37:03 +01:00
|
|
|
raise Exception("Could not add hostapd interface " + ifname)
|
|
|
|
|
2013-11-03 19:20:50 +01:00
|
|
|
def add_iface(self, ifname, confname):
|
2016-03-08 14:28:05 +01:00
|
|
|
res = self.request("ADD " + ifname + " config=" + confname)
|
2019-03-15 10:34:32 +01:00
|
|
|
if "OK" not in res:
|
2013-11-03 19:20:50 +01:00
|
|
|
raise Exception("Could not add hostapd interface")
|
|
|
|
|
2013-10-31 16:28:43 +01:00
|
|
|
def add_bss(self, phy, confname, ignore_error=False):
|
2016-03-08 14:28:05 +01:00
|
|
|
res = self.request("ADD bss_config=" + phy + ":" + confname)
|
2019-03-15 10:34:32 +01:00
|
|
|
if "OK" not in res:
|
2013-10-31 16:28:43 +01:00
|
|
|
if not ignore_error:
|
|
|
|
raise Exception("Could not add hostapd BSS")
|
|
|
|
|
2023-05-22 21:34:12 +02:00
|
|
|
def add_link(self, ifname, confname):
|
|
|
|
res = self.request("ADD " + ifname + " config=" + confname)
|
|
|
|
if "OK" not in res:
|
|
|
|
raise Exception("Could not add hostapd link")
|
|
|
|
|
2013-03-29 17:37:03 +01:00
|
|
|
def remove(self, ifname):
|
2016-03-08 14:28:05 +01:00
|
|
|
self.request("REMOVE " + ifname, timeout=30)
|
2013-03-29 17:37:03 +01:00
|
|
|
|
2013-11-02 10:53:38 +01:00
|
|
|
def relog(self):
|
2016-03-08 14:28:05 +01:00
|
|
|
self.request("RELOG")
|
2013-11-02 10:53:38 +01:00
|
|
|
|
2013-11-28 14:50:47 +01:00
|
|
|
def flush(self):
|
2016-03-08 14:28:05 +01:00
|
|
|
self.request("FLUSH")
|
2013-11-28 14:50:47 +01:00
|
|
|
|
2016-03-04 10:20:38 +01:00
|
|
|
def get_ctrl_iface_port(self, ifname):
|
|
|
|
if self.hostname is None:
|
|
|
|
return None
|
|
|
|
|
2016-03-08 14:28:05 +01:00
|
|
|
res = self.request("INTERFACES ctrl")
|
2016-03-04 10:20:38 +01:00
|
|
|
lines = res.splitlines()
|
|
|
|
found = False
|
|
|
|
for line in lines:
|
|
|
|
words = line.split()
|
|
|
|
if words[0] == ifname:
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
if not found:
|
|
|
|
raise Exception("Could not find UDP port for " + ifname)
|
|
|
|
res = line.find("ctrl_iface=udp:")
|
|
|
|
if res == -1:
|
|
|
|
raise Exception("Wrong ctrl_interface format")
|
|
|
|
words = line.split(":")
|
|
|
|
return int(words[1])
|
2013-03-29 17:37:03 +01:00
|
|
|
|
2016-03-04 10:20:40 +01:00
|
|
|
def terminate(self):
|
|
|
|
self.mon.detach()
|
|
|
|
self.mon.close()
|
|
|
|
self.mon = None
|
|
|
|
self.ctrl.terminate()
|
|
|
|
self.ctrl = None
|
|
|
|
|
2020-01-12 23:02:21 +01:00
|
|
|
def send_file(self, src, dst):
|
|
|
|
self.host.send_file(src, dst)
|
|
|
|
|
2013-03-29 17:37:03 +01:00
|
|
|
class Hostapd:
|
2023-05-22 21:34:12 +02:00
|
|
|
def __init__(self, ifname, bssidx=0, hostname=None, ctrl=hapd_ctrl,
|
|
|
|
port=8877):
|
2016-05-24 13:02:33 +02:00
|
|
|
self.hostname = hostname
|
2016-03-08 14:28:04 +01:00
|
|
|
self.host = remotehost.Host(hostname, ifname)
|
2013-03-29 17:37:03 +01:00
|
|
|
self.ifname = ifname
|
2016-03-04 10:20:35 +01:00
|
|
|
if hostname is None:
|
2023-05-22 21:34:12 +02:00
|
|
|
self.ctrl = wpaspy.Ctrl(os.path.join(ctrl, ifname))
|
|
|
|
self.mon = wpaspy.Ctrl(os.path.join(ctrl, ifname))
|
2016-03-08 14:28:05 +01:00
|
|
|
self.dbg = ifname
|
2016-03-04 10:20:35 +01:00
|
|
|
else:
|
|
|
|
self.ctrl = wpaspy.Ctrl(hostname, port)
|
|
|
|
self.mon = wpaspy.Ctrl(hostname, port)
|
2016-03-08 14:28:05 +01:00
|
|
|
self.dbg = hostname + "/" + ifname
|
2013-11-03 19:50:39 +01:00
|
|
|
self.mon.attach()
|
2014-10-19 19:55:02 +02:00
|
|
|
self.bssid = None
|
2015-04-27 09:08:04 +02:00
|
|
|
self.bssidx = bssidx
|
2023-05-22 21:34:12 +02:00
|
|
|
self.mld_addr = None
|
2014-10-19 19:55:02 +02:00
|
|
|
|
2016-06-27 19:10:23 +02:00
|
|
|
def cmd_execute(self, cmd_array, shell=False):
|
2016-06-07 15:42:40 +02:00
|
|
|
if self.hostname is None:
|
2016-06-27 19:10:23 +02:00
|
|
|
if shell:
|
|
|
|
cmd = ' '.join(cmd_array)
|
|
|
|
else:
|
|
|
|
cmd = cmd_array
|
2016-06-07 15:42:40 +02:00
|
|
|
proc = subprocess.Popen(cmd, stderr=subprocess.STDOUT,
|
2016-06-27 19:10:23 +02:00
|
|
|
stdout=subprocess.PIPE, shell=shell)
|
2016-06-07 15:42:40 +02:00
|
|
|
out = proc.communicate()[0]
|
|
|
|
ret = proc.returncode
|
2019-02-02 11:48:30 +01:00
|
|
|
return ret, out.decode()
|
2016-06-07 15:42:40 +02:00
|
|
|
else:
|
|
|
|
return self.host.execute(cmd_array)
|
|
|
|
|
2016-03-04 10:20:40 +01:00
|
|
|
def close_ctrl(self):
|
|
|
|
if self.mon is not None:
|
|
|
|
self.mon.detach()
|
|
|
|
self.mon.close()
|
|
|
|
self.mon = None
|
|
|
|
self.ctrl.close()
|
|
|
|
self.ctrl = None
|
|
|
|
|
2014-10-19 19:55:02 +02:00
|
|
|
def own_addr(self):
|
|
|
|
if self.bssid is None:
|
2015-04-27 09:08:04 +02:00
|
|
|
self.bssid = self.get_status_field('bssid[%d]' % self.bssidx)
|
2014-10-19 19:55:02 +02:00
|
|
|
return self.bssid
|
2013-03-29 17:37:03 +01:00
|
|
|
|
2023-05-22 21:34:12 +02:00
|
|
|
def own_mld_addr(self):
|
|
|
|
if self.mld_addr is None:
|
|
|
|
self.mld_addr = self.get_status_field('mld_addr[%d]' % self.bssidx)
|
|
|
|
return self.mld_addr
|
|
|
|
|
2020-02-05 08:13:48 +01:00
|
|
|
def get_addr(self, group=False):
|
2023-05-22 21:34:12 +02:00
|
|
|
if self.own_mld_addr() is None:
|
|
|
|
return self.own_addr()
|
|
|
|
return self.own_mld_addr()
|
2020-02-05 08:13:48 +01:00
|
|
|
|
2013-03-29 17:37:03 +01:00
|
|
|
def request(self, cmd):
|
2016-03-08 14:28:05 +01:00
|
|
|
logger.debug(self.dbg + ": CTRL: " + cmd)
|
2013-03-29 17:37:03 +01:00
|
|
|
return self.ctrl.request(cmd)
|
|
|
|
|
|
|
|
def ping(self):
|
|
|
|
return "PONG" in self.request("PING")
|
|
|
|
|
|
|
|
def set(self, field, value):
|
2019-03-15 10:34:32 +01:00
|
|
|
if "OK" not in self.request("SET " + field + " " + value):
|
2020-04-17 17:16:13 +02:00
|
|
|
if "TKIP" in value and (field == "wpa_pairwise" or \
|
|
|
|
field == "rsn_pairwise"):
|
|
|
|
raise utils.HwsimSkip("Cipher TKIP not supported")
|
2013-03-29 17:37:03 +01:00
|
|
|
raise Exception("Failed to set hostapd parameter " + field)
|
|
|
|
|
2022-04-28 22:58:03 +02:00
|
|
|
def set_defaults(self, set_channel=True):
|
2013-03-29 17:37:03 +01:00
|
|
|
self.set("driver", "nl80211")
|
2022-04-28 22:58:03 +02:00
|
|
|
if set_channel:
|
|
|
|
self.set("hw_mode", "g")
|
|
|
|
self.set("channel", "1")
|
|
|
|
self.set("ieee80211n", "1")
|
2013-10-29 23:31:14 +01:00
|
|
|
self.set("logger_stdout", "-1")
|
|
|
|
self.set("logger_stdout_level", "0")
|
2013-03-29 17:37:03 +01:00
|
|
|
|
|
|
|
def set_open(self, ssid):
|
|
|
|
self.set_defaults()
|
|
|
|
self.set("ssid", ssid)
|
|
|
|
|
|
|
|
def set_wpa2_psk(self, ssid, passphrase):
|
|
|
|
self.set_defaults()
|
|
|
|
self.set("ssid", ssid)
|
|
|
|
self.set("wpa_passphrase", passphrase)
|
|
|
|
self.set("wpa", "2")
|
|
|
|
self.set("wpa_key_mgmt", "WPA-PSK")
|
|
|
|
self.set("rsn_pairwise", "CCMP")
|
|
|
|
|
2013-03-29 17:51:48 +01:00
|
|
|
def set_wpa_psk(self, ssid, passphrase):
|
|
|
|
self.set_defaults()
|
|
|
|
self.set("ssid", ssid)
|
|
|
|
self.set("wpa_passphrase", passphrase)
|
|
|
|
self.set("wpa", "1")
|
|
|
|
self.set("wpa_key_mgmt", "WPA-PSK")
|
|
|
|
self.set("wpa_pairwise", "TKIP")
|
|
|
|
|
|
|
|
def set_wpa_psk_mixed(self, ssid, passphrase):
|
|
|
|
self.set_defaults()
|
|
|
|
self.set("ssid", ssid)
|
|
|
|
self.set("wpa_passphrase", passphrase)
|
|
|
|
self.set("wpa", "3")
|
|
|
|
self.set("wpa_key_mgmt", "WPA-PSK")
|
|
|
|
self.set("wpa_pairwise", "TKIP")
|
|
|
|
self.set("rsn_pairwise", "CCMP")
|
|
|
|
|
2013-03-29 18:15:43 +01:00
|
|
|
def set_wep(self, ssid, key):
|
|
|
|
self.set_defaults()
|
|
|
|
self.set("ssid", ssid)
|
|
|
|
self.set("wep_key0", key)
|
|
|
|
|
2013-03-29 17:37:03 +01:00
|
|
|
def enable(self):
|
2019-03-15 10:34:32 +01:00
|
|
|
if "OK" not in self.request("ENABLE"):
|
2013-03-29 17:37:03 +01:00
|
|
|
raise Exception("Failed to enable hostapd interface " + self.ifname)
|
|
|
|
|
|
|
|
def disable(self):
|
2019-03-15 10:34:32 +01:00
|
|
|
if "OK" not in self.request("DISABLE"):
|
2013-03-29 17:37:03 +01:00
|
|
|
raise Exception("Failed to disable hostapd interface " + self.ifname)
|
2013-03-29 19:33:25 +01:00
|
|
|
|
2023-11-21 00:51:55 +01:00
|
|
|
def link_remove(self, count=10):
|
|
|
|
if "OK" not in self.request("LINK_REMOVE %u" % count):
|
|
|
|
raise Exception("Failed to remove hostapd link " + self.ifname)
|
|
|
|
|
2013-11-03 19:50:39 +01:00
|
|
|
def dump_monitor(self):
|
|
|
|
while self.mon.pending():
|
|
|
|
ev = self.mon.recv()
|
2016-03-08 14:28:05 +01:00
|
|
|
logger.debug(self.dbg + ": " + ev)
|
2013-11-03 19:50:39 +01:00
|
|
|
|
|
|
|
def wait_event(self, events, timeout):
|
2019-08-07 11:41:12 +02:00
|
|
|
if not isinstance(events, list):
|
|
|
|
raise Exception("Hostapd.wait_event() called with incorrect events argument type")
|
2014-01-05 07:02:06 +01:00
|
|
|
start = os.times()[4]
|
|
|
|
while True:
|
2013-11-03 19:50:39 +01:00
|
|
|
while self.mon.pending():
|
|
|
|
ev = self.mon.recv()
|
2016-03-08 14:28:05 +01:00
|
|
|
logger.debug(self.dbg + ": " + ev)
|
2013-11-03 19:50:39 +01:00
|
|
|
for event in events:
|
|
|
|
if event in ev:
|
|
|
|
return ev
|
2014-01-05 07:02:06 +01:00
|
|
|
now = os.times()[4]
|
|
|
|
remaining = start + timeout - now
|
|
|
|
if remaining <= 0:
|
|
|
|
break
|
|
|
|
if not self.mon.pending(timeout=remaining):
|
|
|
|
break
|
2013-11-03 19:50:39 +01:00
|
|
|
return None
|
|
|
|
|
2023-12-09 12:04:14 +01:00
|
|
|
def wait_sta(self, addr=None, timeout=2, wait_4way_hs=False):
|
2019-08-07 11:34:24 +02:00
|
|
|
ev = self.wait_event(["AP-STA-CONNECT"], timeout=timeout)
|
2019-08-04 14:16:46 +02:00
|
|
|
if ev is None:
|
|
|
|
raise Exception("AP did not report STA connection")
|
|
|
|
if addr and addr not in ev:
|
|
|
|
raise Exception("Unexpected STA address in connection event: " + ev)
|
2023-12-09 12:04:14 +01:00
|
|
|
if wait_4way_hs:
|
|
|
|
ev2 = self.wait_event(["EAPOL-4WAY-HS-COMPLETED"],
|
|
|
|
timeout=timeout)
|
|
|
|
if ev2 is None:
|
|
|
|
raise Exception("AP did not report 4-way handshake completion")
|
|
|
|
if addr and addr not in ev2:
|
|
|
|
raise Exception("Unexpected STA address in 4-way handshake completion event: " + ev2)
|
|
|
|
return ev
|
|
|
|
|
|
|
|
def wait_sta_disconnect(self, addr=None, timeout=2):
|
|
|
|
ev = self.wait_event(["AP-STA-DISCONNECT"], timeout=timeout)
|
|
|
|
if ev is None:
|
|
|
|
raise Exception("AP did not report STA disconnection")
|
|
|
|
if addr and addr not in ev:
|
|
|
|
raise Exception("Unexpected STA address in disconnection event: " + ev)
|
2022-11-29 15:37:54 +01:00
|
|
|
return ev
|
2023-12-10 10:33:19 +01:00
|
|
|
|
|
|
|
def wait_4way_hs(self, addr=None, timeout=1):
|
|
|
|
ev = self.wait_event(["EAPOL-4WAY-HS-COMPLETED"], timeout=timeout)
|
|
|
|
if ev is None:
|
|
|
|
raise Exception("hostapd did not report 4-way handshake completion")
|
|
|
|
if addr and addr not in ev:
|
|
|
|
raise Exception("Unexpected STA address in 4-way handshake completion event: " + ev)
|
|
|
|
return ev
|
2019-08-04 14:16:46 +02:00
|
|
|
|
2019-12-27 14:58:32 +01:00
|
|
|
def wait_ptkinitdone(self, addr, timeout=2):
|
|
|
|
while timeout > 0:
|
|
|
|
sta = self.get_sta(addr)
|
|
|
|
if 'hostapdWPAPTKState' not in sta:
|
|
|
|
raise Exception("GET_STA did not return hostapdWPAPTKState")
|
|
|
|
state = sta['hostapdWPAPTKState']
|
|
|
|
if state == "11":
|
|
|
|
return
|
2020-01-09 00:03:02 +01:00
|
|
|
time.sleep(0.1)
|
2019-12-27 14:58:32 +01:00
|
|
|
timeout -= 0.1
|
|
|
|
raise Exception("Timeout while waiting for PTKINITDONE")
|
|
|
|
|
2013-11-03 19:50:39 +01:00
|
|
|
def get_status(self):
|
|
|
|
res = self.request("STATUS")
|
|
|
|
lines = res.splitlines()
|
|
|
|
vals = dict()
|
|
|
|
for l in lines:
|
2019-03-15 11:10:37 +01:00
|
|
|
[name, value] = l.split('=', 1)
|
2013-11-03 19:50:39 +01:00
|
|
|
vals[name] = value
|
|
|
|
return vals
|
|
|
|
|
|
|
|
def get_status_field(self, field):
|
|
|
|
vals = self.get_status()
|
|
|
|
if field in vals:
|
|
|
|
return vals[field]
|
|
|
|
return None
|
|
|
|
|
2014-01-05 20:54:46 +01:00
|
|
|
def get_driver_status(self):
|
|
|
|
res = self.request("STATUS-DRIVER")
|
|
|
|
lines = res.splitlines()
|
|
|
|
vals = dict()
|
|
|
|
for l in lines:
|
2019-03-15 11:10:37 +01:00
|
|
|
[name, value] = l.split('=', 1)
|
2014-01-05 20:54:46 +01:00
|
|
|
vals[name] = value
|
|
|
|
return vals
|
|
|
|
|
|
|
|
def get_driver_status_field(self, field):
|
|
|
|
vals = self.get_driver_status()
|
|
|
|
if field in vals:
|
|
|
|
return vals[field]
|
|
|
|
return None
|
|
|
|
|
2014-03-22 17:57:44 +01:00
|
|
|
def get_config(self):
|
|
|
|
res = self.request("GET_CONFIG")
|
|
|
|
lines = res.splitlines()
|
|
|
|
vals = dict()
|
|
|
|
for l in lines:
|
2019-03-15 11:10:37 +01:00
|
|
|
[name, value] = l.split('=', 1)
|
2014-03-22 17:57:44 +01:00
|
|
|
vals[name] = value
|
|
|
|
return vals
|
|
|
|
|
2013-12-26 09:52:11 +01:00
|
|
|
def mgmt_rx(self, timeout=5):
|
|
|
|
ev = self.wait_event(["MGMT-RX"], timeout=timeout)
|
|
|
|
if ev is None:
|
|
|
|
return None
|
|
|
|
msg = {}
|
|
|
|
frame = binascii.unhexlify(ev.split(' ')[1])
|
|
|
|
msg['frame'] = frame
|
|
|
|
|
|
|
|
hdr = struct.unpack('<HH6B6B6BH', frame[0:24])
|
|
|
|
msg['fc'] = hdr[0]
|
|
|
|
msg['subtype'] = (hdr[0] >> 4) & 0xf
|
|
|
|
hdr = hdr[1:]
|
|
|
|
msg['duration'] = hdr[0]
|
|
|
|
hdr = hdr[1:]
|
|
|
|
msg['da'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
|
|
|
|
hdr = hdr[6:]
|
|
|
|
msg['sa'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
|
|
|
|
hdr = hdr[6:]
|
|
|
|
msg['bssid'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
|
|
|
|
hdr = hdr[6:]
|
|
|
|
msg['seq_ctrl'] = hdr[0]
|
|
|
|
msg['payload'] = frame[24:]
|
|
|
|
|
|
|
|
return msg
|
|
|
|
|
|
|
|
def mgmt_tx(self, msg):
|
|
|
|
t = (msg['fc'], 0) + mac2tuple(msg['da']) + mac2tuple(msg['sa']) + mac2tuple(msg['bssid']) + (0,)
|
|
|
|
hdr = struct.pack('<HH6B6B6BH', *t)
|
2019-02-02 12:01:36 +01:00
|
|
|
res = self.request("MGMT_TX " + binascii.hexlify(hdr + msg['payload']).decode())
|
|
|
|
if "OK" not in res:
|
2016-12-18 11:22:13 +01:00
|
|
|
raise Exception("MGMT_TX command to hostapd failed")
|
2013-12-26 09:52:11 +01:00
|
|
|
|
2014-01-31 22:12:38 +01:00
|
|
|
def get_sta(self, addr, info=None, next=False):
|
|
|
|
cmd = "STA-NEXT " if next else "STA "
|
|
|
|
if addr is None:
|
|
|
|
res = self.request("STA-FIRST")
|
|
|
|
elif info:
|
|
|
|
res = self.request(cmd + addr + " " + info)
|
2014-01-02 17:10:30 +01:00
|
|
|
else:
|
2014-01-31 22:12:38 +01:00
|
|
|
res = self.request(cmd + addr)
|
2013-12-26 20:09:55 +01:00
|
|
|
lines = res.splitlines()
|
|
|
|
vals = dict()
|
|
|
|
first = True
|
|
|
|
for l in lines:
|
2015-07-11 23:51:00 +02:00
|
|
|
if first and '=' not in l:
|
2013-12-26 20:09:55 +01:00
|
|
|
vals['addr'] = l
|
|
|
|
first = False
|
|
|
|
else:
|
2019-03-15 11:10:37 +01:00
|
|
|
[name, value] = l.split('=', 1)
|
2013-12-26 20:09:55 +01:00
|
|
|
vals[name] = value
|
|
|
|
return vals
|
|
|
|
|
2014-02-15 14:57:21 +01:00
|
|
|
def get_mib(self, param=None):
|
|
|
|
if param:
|
|
|
|
res = self.request("MIB " + param)
|
|
|
|
else:
|
|
|
|
res = self.request("MIB")
|
2013-12-31 12:57:49 +01:00
|
|
|
lines = res.splitlines()
|
|
|
|
vals = dict()
|
|
|
|
for l in lines:
|
2014-02-15 14:57:21 +01:00
|
|
|
name_val = l.split('=', 1)
|
|
|
|
if len(name_val) > 1:
|
|
|
|
vals[name_val[0]] = name_val[1]
|
2013-12-31 12:57:49 +01:00
|
|
|
return vals
|
|
|
|
|
2016-03-20 17:06:03 +01:00
|
|
|
def get_pmksa(self, addr):
|
|
|
|
res = self.request("PMKSA")
|
|
|
|
lines = res.splitlines()
|
|
|
|
for l in lines:
|
|
|
|
if addr not in l:
|
|
|
|
continue
|
|
|
|
vals = dict()
|
2019-03-15 11:10:37 +01:00
|
|
|
[index, aa, pmkid, expiration, opportunistic] = l.split(' ')
|
2016-03-20 17:06:03 +01:00
|
|
|
vals['index'] = index
|
|
|
|
vals['pmkid'] = pmkid
|
|
|
|
vals['expiration'] = expiration
|
|
|
|
vals['opportunistic'] = opportunistic
|
|
|
|
return vals
|
|
|
|
return None
|
|
|
|
|
2019-03-09 15:50:48 +01:00
|
|
|
def dpp_qr_code(self, uri):
|
|
|
|
res = self.request("DPP_QR_CODE " + uri)
|
|
|
|
if "FAIL" in res:
|
|
|
|
raise Exception("Failed to parse QR Code URI")
|
|
|
|
return int(res)
|
2019-03-09 17:13:10 +01:00
|
|
|
|
2021-03-21 15:39:46 +01:00
|
|
|
def dpp_nfc_uri(self, uri):
|
|
|
|
res = self.request("DPP_NFC_URI " + uri)
|
|
|
|
if "FAIL" in res:
|
|
|
|
raise Exception("Failed to parse NFC URI")
|
|
|
|
return int(res)
|
|
|
|
|
2019-03-09 17:13:10 +01:00
|
|
|
def dpp_bootstrap_gen(self, type="qrcode", chan=None, mac=None, info=None,
|
2022-05-19 16:29:51 +02:00
|
|
|
curve=None, key=None, supported_curves=None,
|
|
|
|
host=None):
|
2019-03-09 17:13:10 +01:00
|
|
|
cmd = "DPP_BOOTSTRAP_GEN type=" + type
|
|
|
|
if chan:
|
|
|
|
cmd += " chan=" + chan
|
|
|
|
if mac:
|
|
|
|
if mac is True:
|
|
|
|
mac = self.own_addr()
|
|
|
|
cmd += " mac=" + mac.replace(':', '')
|
|
|
|
if info:
|
|
|
|
cmd += " info=" + info
|
|
|
|
if curve:
|
|
|
|
cmd += " curve=" + curve
|
|
|
|
if key:
|
|
|
|
cmd += " key=" + key
|
2022-04-14 15:59:15 +02:00
|
|
|
if supported_curves:
|
|
|
|
cmd += " supported_curves=" + supported_curves
|
2022-05-19 16:29:51 +02:00
|
|
|
if host:
|
|
|
|
cmd += " host=" + host
|
2019-03-09 17:13:10 +01:00
|
|
|
res = self.request(cmd)
|
|
|
|
if "FAIL" in res:
|
|
|
|
raise Exception("Failed to generate bootstrapping info")
|
|
|
|
return int(res)
|
2019-03-09 15:50:48 +01:00
|
|
|
|
2021-03-07 12:18:01 +01:00
|
|
|
def dpp_bootstrap_set(self, id, conf=None, configurator=None, ssid=None,
|
|
|
|
extra=None):
|
|
|
|
cmd = "DPP_BOOTSTRAP_SET %d" % id
|
|
|
|
if ssid:
|
|
|
|
cmd += " ssid=" + binascii.hexlify(ssid.encode()).decode()
|
|
|
|
if extra:
|
|
|
|
cmd += " " + extra
|
|
|
|
if conf:
|
|
|
|
cmd += " conf=" + conf
|
|
|
|
if configurator is not None:
|
|
|
|
cmd += " configurator=%d" % configurator
|
|
|
|
if "OK" not in self.request(cmd):
|
|
|
|
raise Exception("Failed to set bootstrapping parameters")
|
|
|
|
|
2019-03-17 17:19:58 +01:00
|
|
|
def dpp_listen(self, freq, netrole=None, qr=None, role=None):
|
|
|
|
cmd = "DPP_LISTEN " + str(freq)
|
|
|
|
if netrole:
|
|
|
|
cmd += " netrole=" + netrole
|
|
|
|
if qr:
|
|
|
|
cmd += " qr=" + qr
|
|
|
|
if role:
|
|
|
|
cmd += " role=" + role
|
|
|
|
if "OK" not in self.request(cmd):
|
|
|
|
raise Exception("Failed to start listen operation")
|
|
|
|
|
2019-03-18 10:36:43 +01:00
|
|
|
def dpp_auth_init(self, peer=None, uri=None, conf=None, configurator=None,
|
|
|
|
extra=None, own=None, role=None, neg_freq=None,
|
2021-03-20 11:19:12 +01:00
|
|
|
ssid=None, passphrase=None, expect_fail=False,
|
2021-03-21 15:39:46 +01:00
|
|
|
conn_status=False, nfc_uri=None):
|
2019-03-18 10:36:43 +01:00
|
|
|
cmd = "DPP_AUTH_INIT"
|
|
|
|
if peer is None:
|
2021-03-21 15:39:46 +01:00
|
|
|
if nfc_uri:
|
|
|
|
peer = self.dpp_nfc_uri(nfc_uri)
|
|
|
|
else:
|
|
|
|
peer = self.dpp_qr_code(uri)
|
2019-03-18 10:36:43 +01:00
|
|
|
cmd += " peer=%d" % peer
|
|
|
|
if own is not None:
|
|
|
|
cmd += " own=%d" % own
|
|
|
|
if role:
|
|
|
|
cmd += " role=" + role
|
|
|
|
if extra:
|
|
|
|
cmd += " " + extra
|
|
|
|
if conf:
|
|
|
|
cmd += " conf=" + conf
|
|
|
|
if configurator is not None:
|
|
|
|
cmd += " configurator=%d" % configurator
|
|
|
|
if neg_freq:
|
|
|
|
cmd += " neg_freq=%d" % neg_freq
|
|
|
|
if ssid:
|
|
|
|
cmd += " ssid=" + binascii.hexlify(ssid.encode()).decode()
|
|
|
|
if passphrase:
|
|
|
|
cmd += " pass=" + binascii.hexlify(passphrase.encode()).decode()
|
2021-03-20 11:19:12 +01:00
|
|
|
if conn_status:
|
|
|
|
cmd += " conn_status=1"
|
2019-03-18 10:36:43 +01:00
|
|
|
res = self.request(cmd)
|
|
|
|
if expect_fail:
|
|
|
|
if "FAIL" not in res:
|
|
|
|
raise Exception("DPP authentication started unexpectedly")
|
|
|
|
return
|
|
|
|
if "OK" not in res:
|
|
|
|
raise Exception("Failed to initiate DPP Authentication")
|
|
|
|
|
2019-03-17 18:22:11 +01:00
|
|
|
def dpp_pkex_init(self, identifier, code, role=None, key=None, curve=None,
|
2022-01-25 19:16:18 +01:00
|
|
|
extra=None, use_id=None, ver=None):
|
2019-03-17 18:22:11 +01:00
|
|
|
if use_id is None:
|
|
|
|
id1 = self.dpp_bootstrap_gen(type="pkex", key=key, curve=curve)
|
|
|
|
else:
|
|
|
|
id1 = use_id
|
|
|
|
cmd = "own=%d " % id1
|
|
|
|
if identifier:
|
|
|
|
cmd += "identifier=%s " % identifier
|
2022-01-25 19:16:18 +01:00
|
|
|
cmd += "init=1 "
|
|
|
|
if ver is not None:
|
|
|
|
cmd += "ver=" + str(ver) + " "
|
2019-03-17 18:22:11 +01:00
|
|
|
if role:
|
|
|
|
cmd += "role=%s " % role
|
|
|
|
if extra:
|
|
|
|
cmd += extra + " "
|
|
|
|
cmd += "code=%s" % code
|
|
|
|
res = self.request("DPP_PKEX_ADD " + cmd)
|
|
|
|
if "FAIL" in res:
|
|
|
|
raise Exception("Failed to set PKEX data (initiator)")
|
|
|
|
return id1
|
|
|
|
|
|
|
|
def dpp_pkex_resp(self, freq, identifier, code, key=None, curve=None,
|
|
|
|
listen_role=None):
|
|
|
|
id0 = self.dpp_bootstrap_gen(type="pkex", key=key, curve=curve)
|
|
|
|
cmd = "own=%d " % id0
|
|
|
|
if identifier:
|
|
|
|
cmd += "identifier=%s " % identifier
|
|
|
|
cmd += "code=%s" % code
|
|
|
|
res = self.request("DPP_PKEX_ADD " + cmd)
|
|
|
|
if "FAIL" in res:
|
|
|
|
raise Exception("Failed to set PKEX data (responder)")
|
|
|
|
self.dpp_listen(freq, role=listen_role)
|
|
|
|
|
2022-03-09 00:05:17 +01:00
|
|
|
def dpp_configurator_add(self, curve=None, key=None,
|
|
|
|
net_access_key_curve=None):
|
2019-03-18 12:36:32 +01:00
|
|
|
cmd = "DPP_CONFIGURATOR_ADD"
|
|
|
|
if curve:
|
|
|
|
cmd += " curve=" + curve
|
2022-03-09 00:05:17 +01:00
|
|
|
if net_access_key_curve:
|
|
|
|
cmd += " net_access_key_curve=" + curve
|
2019-03-18 12:36:32 +01:00
|
|
|
if key:
|
|
|
|
cmd += " key=" + key
|
|
|
|
res = self.request(cmd)
|
|
|
|
if "FAIL" in res:
|
|
|
|
raise Exception("Failed to add configurator")
|
|
|
|
return int(res)
|
|
|
|
|
|
|
|
def dpp_configurator_remove(self, conf_id):
|
|
|
|
res = self.request("DPP_CONFIGURATOR_REMOVE %d" % conf_id)
|
|
|
|
if "OK" not in res:
|
|
|
|
raise Exception("DPP_CONFIGURATOR_REMOVE failed")
|
|
|
|
|
2019-04-19 22:46:00 +02:00
|
|
|
def note(self, txt):
|
|
|
|
self.request("NOTE " + txt)
|
|
|
|
|
2020-01-12 23:02:21 +01:00
|
|
|
def send_file(self, src, dst):
|
|
|
|
self.host.send_file(src, dst)
|
|
|
|
|
2020-12-16 12:00:33 +01:00
|
|
|
def get_ptksa(self, bssid, cipher):
|
|
|
|
res = self.request("PTKSA_CACHE_LIST")
|
|
|
|
lines = res.splitlines()
|
|
|
|
for l in lines:
|
|
|
|
if bssid not in l or cipher not in l:
|
|
|
|
continue
|
|
|
|
vals = dict()
|
|
|
|
[index, addr, cipher, expiration, tk, kdk] = l.split(' ', 5)
|
|
|
|
vals['index'] = index
|
|
|
|
vals['addr'] = addr
|
|
|
|
vals['cipher'] = cipher
|
|
|
|
vals['expiration'] = expiration
|
|
|
|
vals['tk'] = tk
|
|
|
|
vals['kdk'] = kdk
|
|
|
|
return vals
|
|
|
|
return None
|
|
|
|
|
2019-01-04 19:35:47 +01:00
|
|
|
def add_ap(apdev, params, wait_enabled=True, no_enable=False, timeout=30,
|
2022-04-28 22:58:03 +02:00
|
|
|
global_ctrl_override=None, driver=False, set_channel=True):
|
2016-03-30 10:55:56 +02:00
|
|
|
if isinstance(apdev, dict):
|
|
|
|
ifname = apdev['ifname']
|
|
|
|
try:
|
|
|
|
hostname = apdev['hostname']
|
|
|
|
port = apdev['port']
|
|
|
|
logger.info("Starting AP " + hostname + "/" + port + " " + ifname)
|
|
|
|
except:
|
|
|
|
logger.info("Starting AP " + ifname)
|
|
|
|
hostname = None
|
|
|
|
port = 8878
|
|
|
|
else:
|
|
|
|
ifname = apdev
|
|
|
|
logger.info("Starting AP " + ifname + " (old add_ap argument type)")
|
|
|
|
hostname = None
|
|
|
|
port = 8878
|
2019-01-04 19:35:47 +01:00
|
|
|
hapd_global = HostapdGlobal(apdev,
|
|
|
|
global_ctrl_override=global_ctrl_override)
|
2013-03-29 19:33:25 +01:00
|
|
|
hapd_global.remove(ifname)
|
2019-05-06 22:36:30 +02:00
|
|
|
hapd_global.add(ifname, driver=driver)
|
2016-03-04 10:20:38 +01:00
|
|
|
port = hapd_global.get_ctrl_iface_port(ifname)
|
|
|
|
hapd = Hostapd(ifname, hostname=hostname, port=port)
|
2013-03-29 19:33:25 +01:00
|
|
|
if not hapd.ping():
|
|
|
|
raise Exception("Could not ping hostapd")
|
2022-04-28 22:58:03 +02:00
|
|
|
hapd.set_defaults(set_channel=set_channel)
|
2019-03-15 11:10:37 +01:00
|
|
|
fields = ["ssid", "wpa_passphrase", "nas_identifier", "wpa_key_mgmt",
|
2020-01-10 23:19:10 +01:00
|
|
|
"wpa", "wpa_deny_ptk0_rekey",
|
2019-03-15 11:10:37 +01:00
|
|
|
"wpa_pairwise", "rsn_pairwise", "auth_server_addr",
|
|
|
|
"acct_server_addr", "osu_server_uri"]
|
2013-03-29 19:33:25 +01:00
|
|
|
for field in fields:
|
|
|
|
if field in params:
|
|
|
|
hapd.set(field, params[field])
|
2019-03-15 11:10:37 +01:00
|
|
|
for f, v in list(params.items()):
|
2013-04-01 00:01:24 +02:00
|
|
|
if f in fields:
|
|
|
|
continue
|
|
|
|
if isinstance(v, list):
|
|
|
|
for val in v:
|
|
|
|
hapd.set(f, val)
|
|
|
|
else:
|
|
|
|
hapd.set(f, v)
|
2014-03-29 08:51:54 +01:00
|
|
|
if no_enable:
|
|
|
|
return hapd
|
2013-03-29 19:33:25 +01:00
|
|
|
hapd.enable()
|
2013-12-27 07:27:44 +01:00
|
|
|
if wait_enabled:
|
2015-10-04 17:27:54 +02:00
|
|
|
ev = hapd.wait_event(["AP-ENABLED", "AP-DISABLED"], timeout=timeout)
|
2013-12-27 07:27:44 +01:00
|
|
|
if ev is None:
|
|
|
|
raise Exception("AP startup timed out")
|
2014-03-12 12:35:10 +01:00
|
|
|
if "AP-ENABLED" not in ev:
|
|
|
|
raise Exception("AP startup failed")
|
2013-11-03 19:50:39 +01:00
|
|
|
return hapd
|
2013-03-29 19:33:25 +01:00
|
|
|
|
2016-04-07 07:38:02 +02:00
|
|
|
def add_bss(apdev, ifname, confname, ignore_error=False):
|
|
|
|
phy = utils.get_phy(apdev)
|
|
|
|
try:
|
|
|
|
hostname = apdev['hostname']
|
|
|
|
port = apdev['port']
|
|
|
|
logger.info("Starting BSS " + hostname + "/" + port + " phy=" + phy + " ifname=" + ifname)
|
|
|
|
except:
|
|
|
|
logger.info("Starting BSS phy=" + phy + " ifname=" + ifname)
|
|
|
|
hostname = None
|
|
|
|
port = 8878
|
2016-04-07 07:38:03 +02:00
|
|
|
hapd_global = HostapdGlobal(apdev)
|
2020-01-12 23:02:24 +01:00
|
|
|
confname = cfg_file(apdev, confname, ifname)
|
|
|
|
hapd_global.send_file(confname, confname)
|
2013-10-31 16:28:43 +01:00
|
|
|
hapd_global.add_bss(phy, confname, ignore_error)
|
2016-03-04 10:20:38 +01:00
|
|
|
port = hapd_global.get_ctrl_iface_port(ifname)
|
|
|
|
hapd = Hostapd(ifname, hostname=hostname, port=port)
|
2013-10-31 16:28:43 +01:00
|
|
|
if not hapd.ping():
|
|
|
|
raise Exception("Could not ping hostapd")
|
2016-04-07 07:38:04 +02:00
|
|
|
return hapd
|
2013-10-31 16:28:43 +01:00
|
|
|
|
2016-04-07 07:38:01 +02:00
|
|
|
def add_iface(apdev, confname):
|
|
|
|
ifname = apdev['ifname']
|
|
|
|
try:
|
|
|
|
hostname = apdev['hostname']
|
|
|
|
port = apdev['port']
|
|
|
|
logger.info("Starting interface " + hostname + "/" + port + " " + ifname)
|
|
|
|
except:
|
|
|
|
logger.info("Starting interface " + ifname)
|
|
|
|
hostname = None
|
|
|
|
port = 8878
|
2016-04-07 07:38:03 +02:00
|
|
|
hapd_global = HostapdGlobal(apdev)
|
2020-01-12 23:02:24 +01:00
|
|
|
confname = cfg_file(apdev, confname, ifname)
|
|
|
|
hapd_global.send_file(confname, confname)
|
2013-11-03 19:20:50 +01:00
|
|
|
hapd_global.add_iface(ifname, confname)
|
2016-03-04 10:20:38 +01:00
|
|
|
port = hapd_global.get_ctrl_iface_port(ifname)
|
|
|
|
hapd = Hostapd(ifname, hostname=hostname, port=port)
|
2013-11-03 19:20:50 +01:00
|
|
|
if not hapd.ping():
|
|
|
|
raise Exception("Could not ping hostapd")
|
2016-04-07 07:38:04 +02:00
|
|
|
return hapd
|
2013-11-03 19:20:50 +01:00
|
|
|
|
2023-05-22 21:34:12 +02:00
|
|
|
def add_mld_link(apdev, params):
|
|
|
|
if isinstance(apdev, dict):
|
|
|
|
ifname = apdev['ifname']
|
|
|
|
try:
|
|
|
|
hostname = apdev['hostname']
|
|
|
|
port = apdev['port']
|
|
|
|
logger.info("Adding link on: " + hostname + "/" + port + " ifname=" + ifname)
|
|
|
|
except:
|
|
|
|
logger.info("Adding link on: ifname=" + ifname)
|
|
|
|
hostname = None
|
|
|
|
port = 8878
|
|
|
|
else:
|
|
|
|
ifname = apdev
|
|
|
|
logger.info("Adding link on: ifname=" + ifname)
|
|
|
|
hostname = None
|
|
|
|
port = 8878
|
|
|
|
|
|
|
|
hapd_global = HostapdGlobal(apdev)
|
|
|
|
confname, ctrl_iface = cfg_mld_link_file(ifname, params)
|
|
|
|
hapd_global.send_file(confname, confname)
|
|
|
|
try:
|
|
|
|
hapd_global.add_link(ifname, confname)
|
|
|
|
except Exception as e:
|
|
|
|
if str(e) == "Could not add hostapd link":
|
|
|
|
raise utils.HwsimSkip("No MLO support in hostapd")
|
|
|
|
port = hapd_global.get_ctrl_iface_port(ifname)
|
|
|
|
hapd = Hostapd(ifname, hostname=hostname, ctrl=ctrl_iface, port=port)
|
|
|
|
if not hapd.ping():
|
|
|
|
raise Exception("Could not ping hostapd")
|
|
|
|
return hapd
|
|
|
|
|
2016-04-07 07:37:59 +02:00
|
|
|
def remove_bss(apdev, ifname=None):
|
|
|
|
if ifname == None:
|
|
|
|
ifname = apdev['ifname']
|
|
|
|
try:
|
|
|
|
hostname = apdev['hostname']
|
|
|
|
port = apdev['port']
|
|
|
|
logger.info("Removing BSS " + hostname + "/" + port + " " + ifname)
|
|
|
|
except:
|
|
|
|
logger.info("Removing BSS " + ifname)
|
2016-04-07 07:38:03 +02:00
|
|
|
hapd_global = HostapdGlobal(apdev)
|
2013-10-31 16:28:43 +01:00
|
|
|
hapd_global.remove(ifname)
|
|
|
|
|
2016-04-07 07:38:00 +02:00
|
|
|
def terminate(apdev):
|
|
|
|
try:
|
|
|
|
hostname = apdev['hostname']
|
|
|
|
port = apdev['port']
|
2016-04-07 07:38:03 +02:00
|
|
|
logger.info("Terminating hostapd " + hostname + "/" + port)
|
2016-04-07 07:38:00 +02:00
|
|
|
except:
|
|
|
|
logger.info("Terminating hostapd")
|
2016-04-07 07:38:03 +02:00
|
|
|
hapd_global = HostapdGlobal(apdev)
|
2016-03-04 10:20:40 +01:00
|
|
|
hapd_global.terminate()
|
|
|
|
|
2022-11-24 11:06:59 +01:00
|
|
|
def wpa3_params(ssid=None, password=None, wpa_key_mgmt="SAE",
|
|
|
|
ieee80211w="2"):
|
|
|
|
params = {"wpa": "2",
|
|
|
|
"wpa_key_mgmt": wpa_key_mgmt,
|
|
|
|
"ieee80211w": ieee80211w,
|
|
|
|
"rsn_pairwise": "CCMP"}
|
|
|
|
if ssid:
|
|
|
|
params["ssid"] = ssid
|
|
|
|
if password:
|
|
|
|
params["sae_password"] = password
|
|
|
|
return params
|
|
|
|
|
2020-01-04 19:41:52 +01:00
|
|
|
def wpa2_params(ssid=None, passphrase=None, wpa_key_mgmt="WPA-PSK",
|
|
|
|
ieee80211w=None):
|
2019-03-15 11:10:37 +01:00
|
|
|
params = {"wpa": "2",
|
2020-01-04 19:41:52 +01:00
|
|
|
"wpa_key_mgmt": wpa_key_mgmt,
|
2019-03-15 11:10:37 +01:00
|
|
|
"rsn_pairwise": "CCMP"}
|
2013-03-29 19:33:25 +01:00
|
|
|
if ssid:
|
|
|
|
params["ssid"] = ssid
|
|
|
|
if passphrase:
|
|
|
|
params["wpa_passphrase"] = passphrase
|
2020-01-04 19:41:52 +01:00
|
|
|
if ieee80211w is not None:
|
|
|
|
params["ieee80211w"] = ieee80211w
|
2013-03-29 19:33:25 +01:00
|
|
|
return params
|
|
|
|
|
|
|
|
def wpa_params(ssid=None, passphrase=None):
|
2019-03-15 11:10:37 +01:00
|
|
|
params = {"wpa": "1",
|
|
|
|
"wpa_key_mgmt": "WPA-PSK",
|
|
|
|
"wpa_pairwise": "TKIP"}
|
2013-03-29 19:33:25 +01:00
|
|
|
if ssid:
|
|
|
|
params["ssid"] = ssid
|
|
|
|
if passphrase:
|
|
|
|
params["wpa_passphrase"] = passphrase
|
|
|
|
return params
|
|
|
|
|
|
|
|
def wpa_mixed_params(ssid=None, passphrase=None):
|
2019-03-15 11:10:37 +01:00
|
|
|
params = {"wpa": "3",
|
|
|
|
"wpa_key_mgmt": "WPA-PSK",
|
|
|
|
"wpa_pairwise": "TKIP",
|
|
|
|
"rsn_pairwise": "CCMP"}
|
2013-03-29 19:33:25 +01:00
|
|
|
if ssid:
|
|
|
|
params["ssid"] = ssid
|
|
|
|
if passphrase:
|
|
|
|
params["wpa_passphrase"] = passphrase
|
|
|
|
return params
|
2013-09-29 19:35:26 +02:00
|
|
|
|
|
|
|
def radius_params():
|
2019-03-15 11:10:37 +01:00
|
|
|
params = {"auth_server_addr": "127.0.0.1",
|
|
|
|
"auth_server_port": "1812",
|
|
|
|
"auth_server_shared_secret": "radius",
|
|
|
|
"nas_identifier": "nas.w1.fi"}
|
2013-09-29 19:35:26 +02:00
|
|
|
return params
|
|
|
|
|
2013-12-28 11:30:28 +01:00
|
|
|
def wpa_eap_params(ssid=None):
|
|
|
|
params = radius_params()
|
|
|
|
params["wpa"] = "1"
|
|
|
|
params["wpa_key_mgmt"] = "WPA-EAP"
|
|
|
|
params["wpa_pairwise"] = "TKIP"
|
|
|
|
params["ieee8021x"] = "1"
|
|
|
|
if ssid:
|
|
|
|
params["ssid"] = ssid
|
|
|
|
return params
|
|
|
|
|
2013-09-29 19:35:26 +02:00
|
|
|
def wpa2_eap_params(ssid=None):
|
|
|
|
params = radius_params()
|
|
|
|
params["wpa"] = "2"
|
|
|
|
params["wpa_key_mgmt"] = "WPA-EAP"
|
|
|
|
params["rsn_pairwise"] = "CCMP"
|
|
|
|
params["ieee8021x"] = "1"
|
|
|
|
if ssid:
|
|
|
|
params["ssid"] = ssid
|
|
|
|
return params
|
2016-03-04 10:20:41 +01:00
|
|
|
|
|
|
|
def b_only_params(channel="1", ssid=None, country=None):
|
2019-03-15 11:10:37 +01:00
|
|
|
params = {"hw_mode": "b",
|
|
|
|
"channel": channel}
|
2016-03-04 10:20:41 +01:00
|
|
|
if ssid:
|
|
|
|
params["ssid"] = ssid
|
|
|
|
if country:
|
|
|
|
params["country_code"] = country
|
|
|
|
return params
|
|
|
|
|
|
|
|
def g_only_params(channel="1", ssid=None, country=None):
|
2019-03-15 11:10:37 +01:00
|
|
|
params = {"hw_mode": "g",
|
|
|
|
"channel": channel}
|
2016-03-04 10:20:41 +01:00
|
|
|
if ssid:
|
|
|
|
params["ssid"] = ssid
|
|
|
|
if country:
|
|
|
|
params["country_code"] = country
|
|
|
|
return params
|
|
|
|
|
|
|
|
def a_only_params(channel="36", ssid=None, country=None):
|
2019-03-15 11:10:37 +01:00
|
|
|
params = {"hw_mode": "a",
|
|
|
|
"channel": channel}
|
2016-03-04 10:20:41 +01:00
|
|
|
if ssid:
|
|
|
|
params["ssid"] = ssid
|
|
|
|
if country:
|
|
|
|
params["country_code"] = country
|
|
|
|
return params
|
|
|
|
|
|
|
|
def ht20_params(channel="1", ssid=None, country=None):
|
2019-03-15 11:10:37 +01:00
|
|
|
params = {"ieee80211n": "1",
|
|
|
|
"channel": channel,
|
|
|
|
"hw_mode": "g"}
|
2016-03-04 10:20:41 +01:00
|
|
|
if int(channel) > 14:
|
|
|
|
params["hw_mode"] = "a"
|
|
|
|
if ssid:
|
|
|
|
params["ssid"] = ssid
|
|
|
|
if country:
|
|
|
|
params["country_code"] = country
|
|
|
|
return params
|
|
|
|
|
|
|
|
def ht40_plus_params(channel="1", ssid=None, country=None):
|
|
|
|
params = ht20_params(channel, ssid, country)
|
|
|
|
params['ht_capab'] = "[HT40+]"
|
|
|
|
return params
|
|
|
|
|
|
|
|
def ht40_minus_params(channel="1", ssid=None, country=None):
|
|
|
|
params = ht20_params(channel, ssid, country)
|
|
|
|
params['ht_capab'] = "[HT40-]"
|
|
|
|
return params
|
2016-06-07 15:42:40 +02:00
|
|
|
|
2023-12-21 07:49:31 +01:00
|
|
|
def he_params(ssid=None):
|
|
|
|
params = {"ssid": "he6ghz",
|
|
|
|
"ieee80211n": "1",
|
|
|
|
"ieee80211ac": "1",
|
|
|
|
"wmm_enabled": "1",
|
|
|
|
"channel": "5",
|
|
|
|
"op_class": "131",
|
|
|
|
"ieee80211ax": "1",
|
|
|
|
"hw_mode": "a",
|
|
|
|
"he_oper_centr_freq_seg0_idx": "15",
|
|
|
|
"he_oper_chwidth": "2",
|
|
|
|
"vht_oper_chwidth": "2"}
|
|
|
|
if ssid:
|
|
|
|
params["ssid"] = ssid
|
|
|
|
|
|
|
|
return params
|
|
|
|
|
|
|
|
def he_wpa2_params(ssid=None, wpa_key_mgmt="SAE", rsn_pairwise="CCMP",
|
|
|
|
group_cipher="CCMP", sae_pwe="1", passphrase=None):
|
|
|
|
params = he_params(ssid)
|
|
|
|
params["wpa"] = "2"
|
|
|
|
params["wpa_key_mgmt"] = wpa_key_mgmt
|
|
|
|
params["rsn_pairwise"] = rsn_pairwise
|
|
|
|
params["group_cipher"] = group_cipher
|
|
|
|
params["ieee80211w"] = "2"
|
|
|
|
if "SAE" in wpa_key_mgmt:
|
|
|
|
params["sae_pwe"] = sae_pwe
|
|
|
|
params["sae_groups"] = "19"
|
|
|
|
|
|
|
|
if passphrase:
|
|
|
|
params["wpa_passphrase"] = passphrase
|
|
|
|
|
|
|
|
return params
|
|
|
|
|
2016-06-27 19:10:23 +02:00
|
|
|
def cmd_execute(apdev, cmd, shell=False):
|
2016-06-07 15:42:40 +02:00
|
|
|
hapd_global = HostapdGlobal(apdev)
|
2016-06-27 19:10:23 +02:00
|
|
|
return hapd_global.cmd_execute(cmd, shell=shell)
|
2020-01-12 23:02:21 +01:00
|
|
|
|
|
|
|
def send_file(apdev, src, dst):
|
|
|
|
hapd_global = HostapdGlobal(apdev)
|
|
|
|
return hapd_global.send_file(src, dst)
|
2020-01-12 23:02:22 +01:00
|
|
|
|
|
|
|
def acl_file(dev, apdev, conf):
|
2020-02-23 16:38:23 +01:00
|
|
|
fd, filename = tempfile.mkstemp(dir='/tmp', prefix=conf + '-')
|
|
|
|
f = os.fdopen(fd, 'w')
|
2020-01-12 23:02:22 +01:00
|
|
|
|
|
|
|
if conf == 'hostapd.macaddr':
|
2020-02-23 16:38:23 +01:00
|
|
|
mac0 = dev[0].get_status_field("address")
|
|
|
|
f.write(mac0 + '\n')
|
|
|
|
f.write("02:00:00:00:00:12\n")
|
|
|
|
f.write("02:00:00:00:00:34\n")
|
|
|
|
f.write("-02:00:00:00:00:12\n")
|
|
|
|
f.write("-02:00:00:00:00:34\n")
|
|
|
|
f.write("01:01:01:01:01:01\n")
|
|
|
|
f.write("03:01:01:01:01:03\n")
|
2020-01-12 23:02:22 +01:00
|
|
|
elif conf == 'hostapd.accept':
|
2020-02-23 16:38:23 +01:00
|
|
|
mac0 = dev[0].get_status_field("address")
|
|
|
|
mac1 = dev[1].get_status_field("address")
|
|
|
|
f.write(mac0 + " 1\n")
|
|
|
|
f.write(mac1 + " 2\n")
|
2020-01-12 23:02:22 +01:00
|
|
|
elif conf == 'hostapd.accept2':
|
2020-02-23 16:38:23 +01:00
|
|
|
mac0 = dev[0].get_status_field("address")
|
|
|
|
mac1 = dev[1].get_status_field("address")
|
|
|
|
mac2 = dev[2].get_status_field("address")
|
|
|
|
f.write(mac0 + " 1\n")
|
|
|
|
f.write(mac1 + " 2\n")
|
|
|
|
f.write(mac2 + " 3\n")
|
2020-01-12 23:02:22 +01:00
|
|
|
else:
|
2020-02-23 16:38:23 +01:00
|
|
|
f.close()
|
|
|
|
os.unlink(filename)
|
2020-01-12 23:02:22 +01:00
|
|
|
return conf
|
|
|
|
|
|
|
|
return filename
|
2020-01-12 23:02:24 +01:00
|
|
|
|
|
|
|
def bssid_inc(apdev, inc=1):
|
|
|
|
parts = apdev['bssid'].split(':')
|
|
|
|
parts[5] = '%02x' % (int(parts[5], 16) + int(inc))
|
|
|
|
bssid = '%s:%s:%s:%s:%s:%s' % (parts[0], parts[1], parts[2],
|
|
|
|
parts[3], parts[4], parts[5])
|
|
|
|
return bssid
|
|
|
|
|
|
|
|
def cfg_file(apdev, conf, ifname=None):
|
2020-02-23 16:13:04 +01:00
|
|
|
match = re.search(r'^bss-.+', conf)
|
2020-01-12 23:02:24 +01:00
|
|
|
if match:
|
2020-02-23 16:38:23 +01:00
|
|
|
# put cfg file in /tmp directory
|
|
|
|
fd, fname = tempfile.mkstemp(dir='/tmp', prefix=conf + '-')
|
|
|
|
f = os.fdopen(fd, 'w')
|
|
|
|
idx = ''.join(filter(str.isdigit, conf.split('-')[-1]))
|
|
|
|
if ifname is None:
|
|
|
|
ifname = apdev['ifname']
|
|
|
|
if idx != '1':
|
|
|
|
ifname = ifname + '-' + idx
|
|
|
|
|
|
|
|
f.write("driver=nl80211\n")
|
|
|
|
f.write("ctrl_interface=/var/run/hostapd\n")
|
|
|
|
f.write("hw_mode=g\n")
|
|
|
|
f.write("channel=1\n")
|
|
|
|
f.write("ieee80211n=1\n")
|
|
|
|
if conf.startswith('bss-ht40-'):
|
|
|
|
f.write("ht_capab=[HT40+]\n")
|
|
|
|
f.write("interface=%s\n" % ifname)
|
|
|
|
|
|
|
|
f.write("ssid=bss-%s\n" % idx)
|
|
|
|
if conf == 'bss-2-dup.conf':
|
|
|
|
bssid = apdev['bssid']
|
|
|
|
else:
|
|
|
|
bssid = bssid_inc(apdev, int(idx) - 1)
|
|
|
|
f.write("bssid=%s\n" % bssid)
|
|
|
|
|
|
|
|
return fname
|
2020-01-12 23:02:24 +01:00
|
|
|
|
2020-02-23 16:38:23 +01:00
|
|
|
return conf
|
2023-05-22 21:34:12 +02:00
|
|
|
|
|
|
|
idx = 0
|
|
|
|
def cfg_mld_link_file(ifname, params):
|
|
|
|
global idx
|
|
|
|
ctrl_iface="/var/run/hostapd"
|
|
|
|
conf = "link-%d.conf" % idx
|
|
|
|
|
|
|
|
fd, fname = tempfile.mkstemp(dir='/tmp', prefix=conf + '-')
|
|
|
|
f = os.fdopen(fd, 'w')
|
|
|
|
|
|
|
|
if idx != 0:
|
|
|
|
ctrl_iface="/var/run/hostapd_%d" % idx
|
|
|
|
|
|
|
|
f.write("ctrl_interface=%s\n" % ctrl_iface)
|
|
|
|
f.write("driver=nl80211\n")
|
|
|
|
f.write("ieee80211n=1\n")
|
2024-01-25 18:41:33 +01:00
|
|
|
if 'hw_mode' in params and params['hw_mode'] == 'a' and \
|
|
|
|
('op_class' not in params or \
|
|
|
|
int(params['op_class']) not in [131, 132, 133, 134, 135, 136, 137]):
|
|
|
|
f.write("ieee80211ac=1\n")
|
2023-05-22 21:34:12 +02:00
|
|
|
f.write("ieee80211ax=1\n")
|
|
|
|
f.write("ieee80211be=1\n")
|
|
|
|
f.write("interface=%s\n" % ifname)
|
|
|
|
f.write("mld_ap=1\n")
|
|
|
|
|
|
|
|
for k, v in list(params.items()):
|
|
|
|
f.write("{}={}\n".format(k,v))
|
|
|
|
|
|
|
|
idx = idx + 1
|
|
|
|
|
|
|
|
return fname, ctrl_iface
|