wlantest.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. # Python class for controlling wlantest
  2. # Copyright (c) 2013-2014, Jouni Malinen <j@w1.fi>
  3. #
  4. # This software may be distributed under the terms of the BSD license.
  5. # See README for more details.
  6. import re
  7. import os
  8. import posixpath
  9. import time
  10. import subprocess
  11. import logging
  12. import wpaspy
  13. logger = logging.getLogger()
  14. class Wlantest:
  15. remote_host = None
  16. setup_params = None
  17. exe_thread = None
  18. exe_res = []
  19. monitor_mod = None
  20. setup_done = False
  21. @classmethod
  22. def stop_remote_wlantest(cls):
  23. if cls.exe_thread is None:
  24. # Local flow - no need for remote operations
  25. return
  26. cls.remote_host.execute(["killall", "-9", "wlantest"])
  27. cls.remote_host.wait_execute_complete(cls.exe_thread, 5)
  28. cls.exe_thread = None
  29. cls.exe_res = []
  30. @classmethod
  31. def reset_remote_wlantest(cls):
  32. cls.stop_remote_wlantest()
  33. cls.remote_host = None
  34. cls.setup_params = None
  35. cls.exe_thread = None
  36. cls.exe_res = []
  37. cls.monitor_mod = None
  38. cls.setup_done = False
  39. @classmethod
  40. def start_remote_wlantest(cls):
  41. if cls.remote_host is None:
  42. # Local flow - no need for remote operations
  43. return
  44. if cls.exe_thread is not None:
  45. raise Exception("Cannot start wlantest twice")
  46. log_dir = cls.setup_params['log_dir']
  47. ifaces = re.split('; | |, ', cls.remote_host.ifname)
  48. ifname = ifaces[0]
  49. exe = cls.setup_params["wlantest"]
  50. tc_name = cls.setup_params["tc_name"]
  51. base_log_name = tc_name + "_wlantest_" + \
  52. cls.remote_host.name + "_" + ifname
  53. log_file = posixpath.join(log_dir, base_log_name + ".log")
  54. pcap_file = posixpath.join(log_dir, base_log_name + ".pcapng")
  55. cmd = "{} -i {} -n {} -c -dtN -L {}".format(exe, ifname,
  56. pcap_file, log_file)
  57. cls.remote_host.add_log(log_file)
  58. cls.remote_host.add_log(pcap_file)
  59. cls.exe_thread = cls.remote_host.execute_run(cmd.split(), cls.exe_res)
  60. # Give wlantest a chance to start working
  61. time.sleep(1)
  62. @classmethod
  63. def register_remote_wlantest(cls, host, setup_params, monitor_mod):
  64. if cls.remote_host is not None:
  65. raise Exception("Cannot register remote wlantest twice")
  66. cls.remote_host = host
  67. cls.setup_params = setup_params
  68. cls.monitor_mod = monitor_mod
  69. status, buf = host.execute(["which", setup_params['wlantest']])
  70. if status != 0:
  71. raise Exception(host.name + " - wlantest: " + buf)
  72. status, buf = host.execute(["which", setup_params['wlantest_cli']])
  73. if status != 0:
  74. raise Exception(host.name + " - wlantest_cli: " + buf)
  75. @classmethod
  76. def chan_from_wpa(cls, wpa, is_p2p=False):
  77. if cls.monitor_mod is None:
  78. return
  79. m = cls.monitor_mod
  80. return m.setup(cls.remote_host, [m.get_monitor_params(wpa, is_p2p)])
  81. @classmethod
  82. def setup(cls, wpa, is_p2p=False):
  83. cls.chan_from_wpa(wpa, is_p2p)
  84. cls.start_remote_wlantest()
  85. cls.setup_done = True
  86. def __init__(self):
  87. if not self.setup_done:
  88. raise Exception("Cannot create Wlantest instance before setup()")
  89. if os.path.isfile('../../wlantest/wlantest_cli'):
  90. self.wlantest_cli = '../../wlantest/wlantest_cli'
  91. else:
  92. self.wlantest_cli = 'wlantest_cli'
  93. def cli_cmd(self, params):
  94. if self.remote_host is not None:
  95. exe = self.setup_params["wlantest_cli"]
  96. ret = self.remote_host.execute([exe] + params)
  97. if ret[0] != 0:
  98. raise Exception("wlantest_cli failed")
  99. return ret[1]
  100. else:
  101. return subprocess.check_output([self.wlantest_cli] + params)
  102. def flush(self):
  103. res = self.cli_cmd(["flush"])
  104. if "FAIL" in res:
  105. raise Exception("wlantest_cli flush failed")
  106. def relog(self):
  107. res = self.cli_cmd(["relog"])
  108. if "FAIL" in res:
  109. raise Exception("wlantest_cli relog failed")
  110. def add_passphrase(self, passphrase):
  111. res = self.cli_cmd(["add_passphrase", passphrase])
  112. if "FAIL" in res:
  113. raise Exception("wlantest_cli add_passphrase failed")
  114. def add_wepkey(self, key):
  115. res = self.cli_cmd(["add_wepkey", key])
  116. if "FAIL" in res:
  117. raise Exception("wlantest_cli add_key failed")
  118. def info_bss(self, field, bssid):
  119. res = self.cli_cmd(["info_bss", field, bssid])
  120. if "FAIL" in res:
  121. raise Exception("Could not get BSS info from wlantest for " + bssid)
  122. return res
  123. def get_bss_counter(self, field, bssid):
  124. try:
  125. res = self.cli_cmd(["get_bss_counter", field, bssid])
  126. except Exception, e:
  127. return 0
  128. if "FAIL" in res:
  129. return 0
  130. return int(res)
  131. def clear_bss_counters(self, bssid):
  132. self.cli_cmd(["clear_bss_counters", bssid])
  133. def info_sta(self, field, bssid, addr):
  134. res = self.cli_cmd(["info_sta", field, bssid, addr])
  135. if "FAIL" in res:
  136. raise Exception("Could not get STA info from wlantest for " + addr)
  137. return res
  138. def get_sta_counter(self, field, bssid, addr):
  139. res = self.cli_cmd(["get_sta_counter", field, bssid, addr])
  140. if "FAIL" in res:
  141. raise Exception("wlantest_cli command failed")
  142. return int(res)
  143. def clear_sta_counters(self, bssid, addr):
  144. res = self.cli_cmd(["clear_sta_counters", bssid, addr])
  145. if "FAIL" in res:
  146. raise Exception("wlantest_cli command failed")
  147. def tdls_clear(self, bssid, addr1, addr2):
  148. self.cli_cmd(["clear_tdls_counters", bssid, addr1, addr2])
  149. def get_tdls_counter(self, field, bssid, addr1, addr2):
  150. res = self.cli_cmd(["get_tdls_counter", field, bssid, addr1, addr2])
  151. if "FAIL" in res:
  152. raise Exception("wlantest_cli command failed")
  153. return int(res)
  154. def require_ap_pmf_mandatory(self, bssid):
  155. res = self.info_bss("rsn_capab", bssid)
  156. if "MFPR" not in res:
  157. raise Exception("AP did not require PMF")
  158. if "MFPC" not in res:
  159. raise Exception("AP did not enable PMF")
  160. res = self.info_bss("key_mgmt", bssid)
  161. if "PSK-SHA256" not in res:
  162. raise Exception("AP did not enable SHA256-based AKM for PMF")
  163. def require_ap_pmf_optional(self, bssid):
  164. res = self.info_bss("rsn_capab", bssid)
  165. if "MFPR" in res:
  166. raise Exception("AP required PMF")
  167. if "MFPC" not in res:
  168. raise Exception("AP did not enable PMF")
  169. def require_ap_no_pmf(self, bssid):
  170. res = self.info_bss("rsn_capab", bssid)
  171. if "MFPR" in res:
  172. raise Exception("AP required PMF")
  173. if "MFPC" in res:
  174. raise Exception("AP enabled PMF")
  175. def require_sta_pmf_mandatory(self, bssid, addr):
  176. res = self.info_sta("rsn_capab", bssid, addr)
  177. if "MFPR" not in res:
  178. raise Exception("STA did not require PMF")
  179. if "MFPC" not in res:
  180. raise Exception("STA did not enable PMF")
  181. def require_sta_pmf(self, bssid, addr):
  182. res = self.info_sta("rsn_capab", bssid, addr)
  183. if "MFPC" not in res:
  184. raise Exception("STA did not enable PMF")
  185. def require_sta_no_pmf(self, bssid, addr):
  186. res = self.info_sta("rsn_capab", bssid, addr)
  187. if "MFPC" in res:
  188. raise Exception("STA enabled PMF")
  189. def require_sta_key_mgmt(self, bssid, addr, key_mgmt):
  190. res = self.info_sta("key_mgmt", bssid, addr)
  191. if key_mgmt not in res:
  192. raise Exception("Unexpected STA key_mgmt")
  193. def get_tx_tid(self, bssid, addr, tid):
  194. res = self.cli_cmd(["get_tx_tid", bssid, addr, str(tid)])
  195. if "FAIL" in res:
  196. raise Exception("wlantest_cli command failed")
  197. return int(res)
  198. def get_rx_tid(self, bssid, addr, tid):
  199. res = self.cli_cmd(["get_rx_tid", bssid, addr, str(tid)])
  200. if "FAIL" in res:
  201. raise Exception("wlantest_cli command failed")
  202. return int(res)
  203. def get_tid_counters(self, bssid, addr):
  204. tx = {}
  205. rx = {}
  206. for tid in range(0, 17):
  207. tx[tid] = self.get_tx_tid(bssid, addr, tid)
  208. rx[tid] = self.get_rx_tid(bssid, addr, tid)
  209. return [ tx, rx ]