2013-11-24 14:32:52 +01:00
|
|
|
# Testing utilities
|
2019-01-05 21:06:03 +01:00
|
|
|
# Copyright (c) 2013-2019, Jouni Malinen <j@w1.fi>
|
2013-11-24 14:32:52 +01:00
|
|
|
#
|
|
|
|
# This software may be distributed under the terms of the BSD license.
|
|
|
|
# See README for more details.
|
|
|
|
|
2016-12-13 14:47:50 +01:00
|
|
|
import binascii
|
2015-04-27 09:08:04 +02:00
|
|
|
import os
|
2019-04-16 11:08:35 +02:00
|
|
|
import socket
|
2016-12-13 14:47:50 +01:00
|
|
|
import struct
|
2019-01-06 11:28:11 +01:00
|
|
|
import subprocess
|
2015-10-12 00:05:37 +02:00
|
|
|
import time
|
2016-04-07 07:38:02 +02:00
|
|
|
import remotehost
|
2019-01-05 21:06:03 +01:00
|
|
|
import logging
|
2023-12-21 07:49:31 +01:00
|
|
|
import re
|
2019-01-05 21:06:03 +01:00
|
|
|
logger = logging.getLogger()
|
2020-04-18 10:12:54 +02:00
|
|
|
import hostapd
|
2015-04-27 09:08:04 +02:00
|
|
|
|
2013-11-24 14:32:52 +01:00
|
|
|
def get_ifnames():
|
|
|
|
ifnames = []
|
|
|
|
with open("/proc/net/dev", "r") as f:
|
|
|
|
lines = f.readlines()
|
|
|
|
for l in lines:
|
|
|
|
val = l.split(':', 1)
|
|
|
|
if len(val) == 2:
|
|
|
|
ifnames.append(val[0].strip(' '))
|
|
|
|
return ifnames
|
2015-01-07 12:41:31 +01:00
|
|
|
|
|
|
|
class HwsimSkip(Exception):
|
|
|
|
def __init__(self, reason):
|
|
|
|
self.reason = reason
|
|
|
|
def __str__(self):
|
|
|
|
return self.reason
|
2015-01-05 21:00:22 +01:00
|
|
|
|
2020-04-18 10:00:49 +02:00
|
|
|
def long_duration_test(func):
|
|
|
|
func.long_duration_test = True
|
|
|
|
return func
|
|
|
|
|
2015-06-28 20:41:56 +02:00
|
|
|
class fail_test(object):
|
2023-11-21 00:51:49 +01:00
|
|
|
_test_fail = 'TEST_FAIL'
|
|
|
|
_get_fail = 'GET_FAIL'
|
|
|
|
|
|
|
|
def __init__(self, dev, count, funcs, *args):
|
2015-06-28 20:41:56 +02:00
|
|
|
self._dev = dev
|
2023-11-21 00:51:49 +01:00
|
|
|
self._funcs = [(count, funcs)]
|
|
|
|
|
|
|
|
args = list(args)
|
|
|
|
while args:
|
|
|
|
count = args.pop(0)
|
|
|
|
funcs = args.pop(0)
|
|
|
|
self._funcs.append((count, funcs))
|
2015-06-28 20:41:56 +02:00
|
|
|
def __enter__(self):
|
2023-11-21 00:51:49 +01:00
|
|
|
patterns = ' '.join(['%d:%s' % (c, f) for c, f in self._funcs])
|
|
|
|
cmd = '%s %s' % (self._test_fail, patterns)
|
2015-06-28 20:41:56 +02:00
|
|
|
if "OK" not in self._dev.request(cmd):
|
|
|
|
raise HwsimSkip("TEST_FAIL not supported")
|
|
|
|
def __exit__(self, type, value, traceback):
|
2023-11-21 00:51:49 +01:00
|
|
|
pending = self._dev.request(self._get_fail)
|
2015-06-28 20:41:56 +02:00
|
|
|
if type is None:
|
2023-11-21 00:51:49 +01:00
|
|
|
expected = ' '.join(['0:%s' % f for c, f in self._funcs])
|
|
|
|
if pending != expected:
|
|
|
|
# Ensure the failure cannot trigger in the future
|
|
|
|
self._dev.request('%s 0:' % self._test_fail)
|
|
|
|
raise Exception("Not all failures triggered (pending: %s)" % pending)
|
|
|
|
else:
|
|
|
|
logger.info("Pending failures at time of exception: %s" % pending)
|
|
|
|
|
|
|
|
class alloc_fail(fail_test):
|
|
|
|
_test_fail = 'TEST_ALLOC_FAIL'
|
|
|
|
_get_fail = 'GET_ALLOC_FAIL'
|
2015-06-28 20:41:56 +02:00
|
|
|
|
2018-08-22 18:49:05 +02:00
|
|
|
def wait_fail_trigger(dev, cmd, note="Failure not triggered", max_iter=40,
|
|
|
|
timeout=0.05):
|
2017-11-29 12:45:39 +01:00
|
|
|
for i in range(0, max_iter):
|
2015-10-12 00:05:37 +02:00
|
|
|
if dev.request(cmd).startswith("0:"):
|
|
|
|
break
|
2017-11-29 12:45:39 +01:00
|
|
|
if i == max_iter - 1:
|
2015-12-20 10:55:18 +01:00
|
|
|
raise Exception(note)
|
2018-08-22 18:49:05 +02:00
|
|
|
time.sleep(timeout)
|
2015-10-12 00:05:37 +02:00
|
|
|
|
2015-03-02 15:30:13 +01:00
|
|
|
def require_under_vm():
|
|
|
|
with open('/proc/1/cmdline', 'r') as f:
|
|
|
|
cmd = f.read()
|
|
|
|
if "inside.sh" not in cmd:
|
|
|
|
raise HwsimSkip("Not running under VM")
|
2015-04-27 09:08:04 +02:00
|
|
|
|
|
|
|
def iface_is_in_bridge(bridge, ifname):
|
|
|
|
fname = "/sys/class/net/"+ifname+"/brport/bridge"
|
|
|
|
if not os.path.exists(fname):
|
|
|
|
return False
|
|
|
|
if not os.path.islink(fname):
|
|
|
|
return False
|
|
|
|
truebridge = os.path.basename(os.readlink(fname))
|
|
|
|
if bridge == truebridge:
|
|
|
|
return True
|
|
|
|
return False
|
2015-08-01 17:48:49 +02:00
|
|
|
|
|
|
|
def skip_with_fips(dev, reason="Not supported in FIPS mode"):
|
|
|
|
res = dev.get_capability("fips")
|
|
|
|
if res and 'FIPS' in res:
|
|
|
|
raise HwsimSkip(reason)
|
2016-04-07 07:38:02 +02:00
|
|
|
|
2020-03-22 23:32:15 +01:00
|
|
|
def check_ext_key_id_capa(dev):
|
|
|
|
res = dev.get_driver_status_field('capa.flags')
|
|
|
|
if (int(res, 0) & 0x8000000000000000) == 0:
|
|
|
|
raise HwsimSkip("Extended Key ID not supported")
|
|
|
|
|
2020-04-17 15:07:24 +02:00
|
|
|
def skip_without_tkip(dev):
|
|
|
|
res = dev.get_capability("fips")
|
|
|
|
if "TKIP" not in dev.get_capability("pairwise") or \
|
|
|
|
"TKIP" not in dev.get_capability("group"):
|
|
|
|
raise HwsimSkip("Cipher TKIP not supported")
|
|
|
|
|
2020-04-18 10:05:49 +02:00
|
|
|
def check_wep_capa(dev):
|
|
|
|
if "WEP40" not in dev.get_capability("group"):
|
|
|
|
raise HwsimSkip("WEP not supported")
|
|
|
|
|
2020-04-18 10:07:33 +02:00
|
|
|
def check_sae_capab(dev):
|
|
|
|
if "SAE" not in dev.get_capability("auth_alg"):
|
|
|
|
raise HwsimSkip("SAE not supported")
|
|
|
|
|
2020-05-30 22:30:42 +02:00
|
|
|
def check_sae_pk_capab(dev):
|
2021-03-13 23:51:23 +01:00
|
|
|
capab = dev.get_capability("sae")
|
|
|
|
if capab is None or "PK" not in capab:
|
2020-05-30 22:30:42 +02:00
|
|
|
raise HwsimSkip("SAE-PK not supported")
|
|
|
|
|
2021-02-28 22:15:57 +01:00
|
|
|
def check_erp_capa(dev):
|
|
|
|
capab = dev.get_capability("erp")
|
|
|
|
if not capab or 'ERP' not in capab:
|
|
|
|
raise HwsimSkip("ERP not supported in the build")
|
|
|
|
|
|
|
|
def check_fils_capa(dev):
|
|
|
|
capa = dev.get_capability("fils")
|
|
|
|
if capa is None or "FILS" not in capa:
|
|
|
|
raise HwsimSkip("FILS not supported")
|
|
|
|
|
|
|
|
def check_fils_sk_pfs_capa(dev):
|
|
|
|
capa = dev.get_capability("fils")
|
|
|
|
if capa is None or "FILS-SK-PFS" not in capa:
|
|
|
|
raise HwsimSkip("FILS-SK-PFS not supported")
|
|
|
|
|
2022-11-08 06:05:49 +01:00
|
|
|
def check_imsi_privacy_support(dev):
|
|
|
|
tls = dev.request("GET tls_library")
|
|
|
|
if tls.startswith("OpenSSL"):
|
|
|
|
return
|
|
|
|
raise HwsimSkip("IMSI privacy not supported with this TLS library: " + tls)
|
|
|
|
|
2020-05-16 20:58:10 +02:00
|
|
|
def check_tls_tod(dev):
|
|
|
|
tls = dev.request("GET tls_library")
|
|
|
|
if not tls.startswith("OpenSSL") and not tls.startswith("internal"):
|
|
|
|
raise HwsimSkip("TLS TOD-TOFU/STRICT not supported with this TLS library: " + tls)
|
|
|
|
|
2020-04-18 10:09:36 +02:00
|
|
|
def vht_supported():
|
|
|
|
cmd = subprocess.Popen(["iw", "reg", "get"], stdout=subprocess.PIPE)
|
2023-02-22 20:21:54 +01:00
|
|
|
reg = cmd.stdout.read().decode()
|
2020-04-18 10:09:36 +02:00
|
|
|
if "@ 80)" in reg or "@ 160)" in reg:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2023-12-21 07:49:31 +01:00
|
|
|
def eht_320mhz_supported():
|
|
|
|
cmd = subprocess.Popen(["iw", "reg", "get"],
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
cmd = subprocess.Popen(["iw", "reg", "get"], stdout=subprocess.PIPE)
|
|
|
|
reg = cmd.stdout.read().decode()
|
|
|
|
if "@ 320)" in reg:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def he_6ghz_supported(freq=5975):
|
|
|
|
cmd = subprocess.Popen(["iw", "reg", "get"],
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
reg_rules = cmd.stdout.read().decode().splitlines()
|
|
|
|
for rule in reg_rules:
|
|
|
|
m = re.search(r"\s*\(\d+\s*-\s*\d+", rule)
|
|
|
|
if not m:
|
|
|
|
continue
|
|
|
|
freqs = re.findall(r"\d+", m.group(0))
|
|
|
|
if int(freqs[0]) <= freq and freq <= int(freqs[1]):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2020-04-18 10:31:49 +02:00
|
|
|
# This function checks whether the provided dev, which may be either
|
|
|
|
# WpaSupplicant or Hostapd supports CSA.
|
|
|
|
def csa_supported(dev):
|
|
|
|
res = dev.get_driver_status()
|
|
|
|
if (int(res['capa.flags'], 0) & 0x80000000) == 0:
|
|
|
|
raise HwsimSkip("CSA not supported")
|
|
|
|
|
2016-04-07 07:38:02 +02:00
|
|
|
def get_phy(ap, ifname=None):
|
|
|
|
phy = "phy3"
|
|
|
|
try:
|
|
|
|
hostname = ap['hostname']
|
|
|
|
except:
|
|
|
|
hostname = None
|
|
|
|
host = remotehost.Host(hostname)
|
|
|
|
|
|
|
|
if ifname == None:
|
|
|
|
ifname = ap['ifname']
|
2016-04-24 11:28:18 +02:00
|
|
|
status, buf = host.execute(["iw", "dev", ifname, "info"])
|
2016-04-07 07:38:02 +02:00
|
|
|
if status != 0:
|
|
|
|
raise Exception("iw " + ifname + " info failed")
|
|
|
|
lines = buf.split("\n")
|
|
|
|
for line in lines:
|
|
|
|
if "wiphy" in line:
|
|
|
|
words = line.split()
|
|
|
|
phy = "phy" + words[1]
|
|
|
|
break
|
|
|
|
return phy
|
2016-12-13 14:47:50 +01:00
|
|
|
|
|
|
|
def parse_ie(buf):
|
|
|
|
ret = {}
|
|
|
|
data = binascii.unhexlify(buf)
|
|
|
|
while len(data) >= 2:
|
2019-03-15 11:10:37 +01:00
|
|
|
ie, elen = struct.unpack('BB', data[0:2])
|
2016-12-13 14:47:50 +01:00
|
|
|
data = data[2:]
|
|
|
|
if elen > len(data):
|
|
|
|
break
|
|
|
|
ret[ie] = data[0:elen]
|
|
|
|
data = data[elen:]
|
|
|
|
return ret
|
2019-01-05 21:06:03 +01:00
|
|
|
|
|
|
|
def wait_regdom_changes(dev):
|
|
|
|
for i in range(10):
|
|
|
|
ev = dev.wait_event(["CTRL-EVENT-REGDOM-CHANGE"], timeout=0.1)
|
|
|
|
if ev is None:
|
|
|
|
break
|
|
|
|
|
|
|
|
def clear_country(dev):
|
|
|
|
logger.info("Try to clear country")
|
|
|
|
id = dev[1].add_network()
|
|
|
|
dev[1].set_network(id, "mode", "2")
|
|
|
|
dev[1].set_network_quoted(id, "ssid", "country-clear")
|
|
|
|
dev[1].set_network(id, "key_mgmt", "NONE")
|
|
|
|
dev[1].set_network(id, "frequency", "2412")
|
|
|
|
dev[1].set_network(id, "scan_freq", "2412")
|
|
|
|
dev[1].select_network(id)
|
|
|
|
ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"])
|
|
|
|
if ev:
|
|
|
|
dev[0].connect("country-clear", key_mgmt="NONE", scan_freq="2412")
|
|
|
|
dev[1].request("DISCONNECT")
|
|
|
|
dev[0].wait_disconnected()
|
|
|
|
dev[0].request("DISCONNECT")
|
|
|
|
dev[0].request("ABORT_SCAN")
|
|
|
|
time.sleep(1)
|
|
|
|
dev[0].dump_monitor()
|
|
|
|
dev[1].dump_monitor()
|
2019-01-06 11:28:11 +01:00
|
|
|
|
2019-01-06 19:51:32 +01:00
|
|
|
def clear_regdom(hapd, dev, count=1):
|
2019-06-25 03:50:39 +02:00
|
|
|
disable_hapd(hapd)
|
|
|
|
clear_regdom_dev(dev, count)
|
|
|
|
|
|
|
|
def disable_hapd(hapd):
|
2019-01-06 11:28:11 +01:00
|
|
|
if hapd:
|
|
|
|
hapd.request("DISABLE")
|
|
|
|
time.sleep(0.1)
|
2019-05-30 10:05:55 +02:00
|
|
|
|
|
|
|
def clear_regdom_dev(dev, count=1):
|
2019-01-06 19:51:32 +01:00
|
|
|
for i in range(count):
|
|
|
|
dev[i].request("DISCONNECT")
|
2019-03-17 16:55:43 +01:00
|
|
|
for i in range(count):
|
|
|
|
dev[i].disconnect_and_stop_scan()
|
2020-03-08 14:27:03 +01:00
|
|
|
dev[0].cmd_execute(['iw', 'reg', 'set', '00'])
|
2019-01-06 11:28:11 +01:00
|
|
|
wait_regdom_changes(dev[0])
|
|
|
|
country = dev[0].get_driver_status_field("country")
|
|
|
|
logger.info("Country code at the end: " + country)
|
|
|
|
if country != "00":
|
|
|
|
clear_country(dev)
|
2019-01-06 19:51:32 +01:00
|
|
|
for i in range(count):
|
|
|
|
dev[i].flush_scan_cache()
|
2019-04-16 11:08:35 +02:00
|
|
|
|
|
|
|
def radiotap_build():
|
|
|
|
radiotap_payload = struct.pack('BB', 0x08, 0)
|
|
|
|
radiotap_payload += struct.pack('BB', 0, 0)
|
|
|
|
radiotap_payload += struct.pack('BB', 0, 0)
|
|
|
|
radiotap_hdr = struct.pack('<BBHL', 0, 0, 8 + len(radiotap_payload),
|
|
|
|
0xc002)
|
|
|
|
return radiotap_hdr + radiotap_payload
|
|
|
|
|
|
|
|
def start_monitor(ifname, freq=2412):
|
|
|
|
subprocess.check_call(["iw", ifname, "set", "type", "monitor"])
|
|
|
|
subprocess.call(["ip", "link", "set", "dev", ifname, "up"])
|
|
|
|
subprocess.check_call(["iw", ifname, "set", "freq", str(freq)])
|
|
|
|
|
|
|
|
ETH_P_ALL = 3
|
|
|
|
sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW,
|
|
|
|
socket.htons(ETH_P_ALL))
|
|
|
|
sock.bind((ifname, 0))
|
|
|
|
sock.settimeout(0.5)
|
|
|
|
return sock
|
|
|
|
|
|
|
|
def stop_monitor(ifname):
|
|
|
|
subprocess.call(["ip", "link", "set", "dev", ifname, "down"])
|
|
|
|
subprocess.call(["iw", ifname, "set", "type", "managed"])
|
2020-04-18 10:12:54 +02:00
|
|
|
|
|
|
|
def clear_scan_cache(apdev):
|
|
|
|
ifname = apdev['ifname']
|
|
|
|
hostapd.cmd_execute(apdev, ['ifconfig', ifname, 'up'])
|
|
|
|
hostapd.cmd_execute(apdev, ['iw', ifname, 'scan', 'trigger', 'freq', '2412',
|
|
|
|
'flush'])
|
|
|
|
time.sleep(0.1)
|
|
|
|
hostapd.cmd_execute(apdev, ['ifconfig', ifname, 'down'])
|
|
|
|
|
|
|
|
def set_world_reg(apdev0=None, apdev1=None, dev0=None):
|
|
|
|
if apdev0:
|
|
|
|
hostapd.cmd_execute(apdev0, ['iw', 'reg', 'set', '00'])
|
|
|
|
if apdev1:
|
|
|
|
hostapd.cmd_execute(apdev1, ['iw', 'reg', 'set', '00'])
|
|
|
|
if dev0:
|
|
|
|
dev0.cmd_execute(['iw', 'reg', 'set', '00'])
|
|
|
|
time.sleep(0.1)
|
2020-05-07 12:47:52 +02:00
|
|
|
|
|
|
|
def sysctl_write(val):
|
|
|
|
subprocess.call(['sysctl', '-w', val], stdout=open('/dev/null', 'w'))
|
|
|
|
|
2020-05-17 00:25:11 +02:00
|
|
|
def var_arg_call(fn, dev, apdev, params):
|
|
|
|
if fn.__code__.co_argcount > 2:
|
|
|
|
return fn(dev, apdev, params)
|
|
|
|
elif fn.__code__.co_argcount > 1:
|
|
|
|
return fn(dev, apdev)
|
|
|
|
return fn(dev)
|
|
|
|
|
|
|
|
def cloned_wrapper(wrapper, fn):
|
|
|
|
# we need the name set right for selecting / printing etc.
|
|
|
|
wrapper.__name__ = fn.__name__
|
|
|
|
wrapper.__doc__ = fn.__doc__
|
|
|
|
# reparent to the right module for module filtering
|
|
|
|
wrapper.__module__ = fn.__module__
|
|
|
|
return wrapper
|
|
|
|
|
2020-05-07 12:47:52 +02:00
|
|
|
def disable_ipv6(fn):
|
|
|
|
def wrapper(dev, apdev, params):
|
|
|
|
require_under_vm()
|
|
|
|
try:
|
|
|
|
sysctl_write('net.ipv6.conf.all.disable_ipv6=1')
|
|
|
|
sysctl_write('net.ipv6.conf.default.disable_ipv6=1')
|
2020-05-17 00:25:11 +02:00
|
|
|
var_arg_call(fn, dev, apdev, params)
|
2020-05-07 12:47:52 +02:00
|
|
|
finally:
|
|
|
|
sysctl_write('net.ipv6.conf.all.disable_ipv6=0')
|
|
|
|
sysctl_write('net.ipv6.conf.default.disable_ipv6=0')
|
2020-05-17 00:25:11 +02:00
|
|
|
return cloned_wrapper(wrapper, fn)
|