test_kernel.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Test a few kernel bugs and functionality
  2. # Copyright (c) 2016, Intel Deutschland GmbH
  3. #
  4. # Author: Johannes Berg <johannes.berg@intel.com>
  5. #
  6. # This software may be distributed under the terms of the BSD license.
  7. # See README for more details.
  8. import hostapd
  9. import binascii
  10. import os
  11. import struct
  12. from test_wnm import expect_ack
  13. from tshark import run_tshark
  14. def _test_kernel_bss_leak(dev, apdev, deauth):
  15. ssid = "test-bss-leak"
  16. passphrase = 'qwertyuiop'
  17. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  18. hapd = hostapd.add_ap(apdev[0], params)
  19. hapd.set("ext_mgmt_frame_handling", "1")
  20. dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False)
  21. while True:
  22. pkt = hapd.mgmt_rx()
  23. if not pkt:
  24. raise Exception("MGMT RX wait timed out for auth frame")
  25. if pkt['fc'] & 0xc:
  26. continue
  27. if pkt['subtype'] == 0: # assoc request
  28. if deauth:
  29. # return a deauth immediately
  30. hapd.mgmt_tx({
  31. 'fc': 0xc0,
  32. 'sa': pkt['da'],
  33. 'da': pkt['sa'],
  34. 'bssid': pkt['bssid'],
  35. 'payload': '\x01\x00',
  36. })
  37. break
  38. else:
  39. hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % (
  40. binascii.hexlify(pkt['frame']), ))
  41. hapd.set("ext_mgmt_frame_handling", "0")
  42. hapd.request("STOP_AP")
  43. dev[0].request("REMOVE_NETWORK all")
  44. dev[0].wait_disconnected()
  45. dev[0].flush_scan_cache(freq=5180)
  46. res = dev[0].request("SCAN_RESULTS")
  47. if len(res.splitlines()) > 1:
  48. raise Exception("BSS entry should no longer be around")
  49. def test_kernel_bss_leak_deauth(dev, apdev):
  50. """cfg80211/mac80211 BSS leak on deauthentication"""
  51. return _test_kernel_bss_leak(dev, apdev, deauth=True)
  52. def test_kernel_bss_leak_timeout(dev, apdev):
  53. """cfg80211/mac80211 BSS leak on timeout"""
  54. return _test_kernel_bss_leak(dev, apdev, deauth=False)
  55. MGMT_SUBTYPE_ACTION = 13
  56. def expect_no_ack(hapd):
  57. ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
  58. if ev is None:
  59. raise Exception("Missing TX status")
  60. if "ok=0" not in ev:
  61. raise Exception("Action frame unexpectedly acknowledged")
  62. def test_kernel_unknown_action_frame_rejection_sta(dev, apdev, params):
  63. """mac80211 and unknown Action frame rejection in STA mode"""
  64. hapd = hostapd.add_ap(apdev[0], { "ssid": "unknown-action" })
  65. dev[0].connect("unknown-action", key_mgmt="NONE", scan_freq="2412")
  66. bssid = hapd.own_addr()
  67. addr = dev[0].own_addr()
  68. hapd.set("ext_mgmt_frame_handling", "1")
  69. # Unicast Action frame with unknown category (response expected)
  70. msg = {}
  71. msg['fc'] = MGMT_SUBTYPE_ACTION << 4
  72. msg['da'] = addr
  73. msg['sa'] = bssid
  74. msg['bssid'] = bssid
  75. msg['payload'] = struct.pack("<BB", 0x70, 0)
  76. hapd.mgmt_tx(msg)
  77. expect_ack(hapd)
  78. # Note: mac80211 does not allow group-addressed Action frames in unknown
  79. # categories to be transmitted in AP mode, so for now, these steps are
  80. # commented out.
  81. # Multicast Action frame with unknown category (no response expected)
  82. #msg['da'] = "01:ff:ff:ff:ff:ff"
  83. #msg['payload'] = struct.pack("<BB", 0x71, 1)
  84. #hapd.mgmt_tx(msg)
  85. #expect_no_ack(hapd)
  86. # Broadcast Action frame with unknown category (no response expected)
  87. #msg['da'] = "ff:ff:ff:ff:ff:ff"
  88. #msg['payload'] = struct.pack("<BB", 0x72, 2)
  89. #hapd.mgmt_tx(msg)
  90. #expect_no_ack(hapd)
  91. # Unicast Action frame with error indication category (no response expected)
  92. msg['da'] = addr
  93. msg['payload'] = struct.pack("<BB", 0xf3, 3)
  94. hapd.mgmt_tx(msg)
  95. expect_ack(hapd)
  96. # Unicast Action frame with unknown category (response expected)
  97. msg['da'] = addr
  98. msg['payload'] = struct.pack("<BB", 0x74, 4)
  99. hapd.mgmt_tx(msg)
  100. expect_ack(hapd)
  101. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  102. "wlan.sa == %s && wlan.fc.type_subtype == 0x0d" % addr,
  103. display=["wlan_mgt.fixed.category_code"])
  104. res = out.splitlines()
  105. categ = [ int(x) for x in res ]
  106. if 0xf2 in categ or 0xf3 in categ:
  107. raise Exception("Unexpected Action frame rejection: " + str(categ))
  108. if 0xf0 not in categ or 0xf4 not in categ:
  109. raise Exception("Action frame rejection missing: " + str(categ))