wlantest.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/python
  2. #
  3. # Python class for controlling wlantest
  4. # Copyright (c) 2013-2014, Jouni Malinen <j@w1.fi>
  5. #
  6. # This software may be distributed under the terms of the BSD license.
  7. # See README for more details.
  8. import os
  9. import time
  10. import subprocess
  11. import logging
  12. import wpaspy
  13. logger = logging.getLogger()
  14. class Wlantest:
  15. def __init__(self):
  16. if os.path.isfile('../../wlantest/wlantest_cli'):
  17. self.wlantest_cli = '../../wlantest/wlantest_cli'
  18. else:
  19. self.wlantest_cli = 'wlantest_cli'
  20. def flush(self):
  21. res = subprocess.check_output([self.wlantest_cli, "flush"])
  22. if "FAIL" in res:
  23. raise Exception("wlantest_cli flush failed")
  24. def relog(self):
  25. res = subprocess.check_output([self.wlantest_cli, "relog"])
  26. if "FAIL" in res:
  27. raise Exception("wlantest_cli relog failed")
  28. def add_passphrase(self, passphrase):
  29. res = subprocess.check_output([self.wlantest_cli, "add_passphrase",
  30. passphrase])
  31. if "FAIL" in res:
  32. raise Exception("wlantest_cli add_passphrase failed")
  33. def add_wepkey(self, key):
  34. res = subprocess.check_output([self.wlantest_cli, "add_wepkey", key])
  35. if "FAIL" in res:
  36. raise Exception("wlantest_cli add_key failed")
  37. def info_bss(self, field, bssid):
  38. res = subprocess.check_output([self.wlantest_cli, "info_bss",
  39. field, bssid])
  40. if "FAIL" in res:
  41. raise Exception("Could not get BSS info from wlantest for " + bssid)
  42. return res
  43. def get_bss_counter(self, field, bssid):
  44. try:
  45. res = subprocess.check_output([self.wlantest_cli, "get_bss_counter",
  46. field, bssid]);
  47. except Exception, e:
  48. return 0
  49. if "FAIL" in res:
  50. return 0
  51. return int(res)
  52. def clear_bss_counters(self, bssid):
  53. subprocess.call([self.wlantest_cli, "clear_bss_counters", bssid]);
  54. def info_sta(self, field, bssid, addr):
  55. res = subprocess.check_output([self.wlantest_cli, "info_sta",
  56. field, bssid, addr])
  57. if "FAIL" in res:
  58. raise Exception("Could not get STA info from wlantest for " + addr)
  59. return res
  60. def get_sta_counter(self, field, bssid, addr):
  61. res = subprocess.check_output([self.wlantest_cli, "get_sta_counter",
  62. field, bssid, addr]);
  63. if "FAIL" in res:
  64. raise Exception("wlantest_cli command failed")
  65. return int(res)
  66. def clear_sta_counters(self, bssid, addr):
  67. res = subprocess.check_output([self.wlantest_cli, "clear_sta_counters",
  68. bssid, addr]);
  69. if "FAIL" in res:
  70. raise Exception("wlantest_cli command failed")
  71. def tdls_clear(self, bssid, addr1, addr2):
  72. res = subprocess.check_output([self.wlantest_cli, "clear_tdls_counters",
  73. bssid, addr1, addr2]);
  74. def get_tdls_counter(self, field, bssid, addr1, addr2):
  75. res = subprocess.check_output([self.wlantest_cli, "get_tdls_counter",
  76. field, bssid, addr1, addr2]);
  77. if "FAIL" in res:
  78. raise Exception("wlantest_cli command failed")
  79. return int(res)
  80. def require_ap_pmf_mandatory(self, bssid):
  81. res = self.info_bss("rsn_capab", bssid)
  82. if "MFPR" not in res:
  83. raise Exception("AP did not require PMF")
  84. if "MFPC" not in res:
  85. raise Exception("AP did not enable PMF")
  86. res = self.info_bss("key_mgmt", bssid)
  87. if "PSK-SHA256" not in res:
  88. raise Exception("AP did not enable SHA256-based AKM for PMF")
  89. def require_ap_pmf_optional(self, bssid):
  90. res = self.info_bss("rsn_capab", bssid)
  91. if "MFPR" in res:
  92. raise Exception("AP required PMF")
  93. if "MFPC" not in res:
  94. raise Exception("AP did not enable PMF")
  95. def require_ap_no_pmf(self, bssid):
  96. res = self.info_bss("rsn_capab", bssid)
  97. if "MFPR" in res:
  98. raise Exception("AP required PMF")
  99. if "MFPC" in res:
  100. raise Exception("AP enabled PMF")
  101. def require_sta_pmf_mandatory(self, bssid, addr):
  102. res = self.info_sta("rsn_capab", bssid, addr)
  103. if "MFPR" not in res:
  104. raise Exception("STA did not require PMF")
  105. if "MFPC" not in res:
  106. raise Exception("STA did not enable PMF")
  107. def require_sta_pmf(self, bssid, addr):
  108. res = self.info_sta("rsn_capab", bssid, addr)
  109. if "MFPC" not in res:
  110. raise Exception("STA did not enable PMF")
  111. def require_sta_key_mgmt(self, bssid, addr, key_mgmt):
  112. res = self.info_sta("key_mgmt", bssid, addr)
  113. if key_mgmt not in res:
  114. raise Exception("Unexpected STA key_mgmt")
  115. def get_tx_tid(self, bssid, addr, tid):
  116. res = subprocess.check_output([self.wlantest_cli, "get_tx_tid",
  117. bssid, addr, str(tid)]);
  118. if "FAIL" in res:
  119. raise Exception("wlantest_cli command failed")
  120. return int(res)
  121. def get_rx_tid(self, bssid, addr, tid):
  122. res = subprocess.check_output([self.wlantest_cli, "get_rx_tid",
  123. bssid, addr, str(tid)]);
  124. if "FAIL" in res:
  125. raise Exception("wlantest_cli command failed")
  126. return int(res)
  127. def get_tid_counters(self, bssid, addr):
  128. tx = {}
  129. rx = {}
  130. for tid in range(0, 17):
  131. tx[tid] = self.get_tx_tid(bssid, addr, tid)
  132. rx[tid] = self.get_rx_tid(bssid, addr, tid)
  133. return [ tx, rx ]