test_cfg80211.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # cfg80211 test cases
  2. # Copyright (c) 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. from remotehost import remote_compatible
  7. import logging
  8. logger = logging.getLogger()
  9. import binascii
  10. import os
  11. import time
  12. import hostapd
  13. import hwsim_utils
  14. from tshark import run_tshark
  15. from nl80211 import *
  16. from wpasupplicant import WpaSupplicant
  17. def nl80211_command(dev, cmd, attr):
  18. res = dev.request("VENDOR ffffffff {} {}".format(nl80211_cmd[cmd],
  19. binascii.hexlify(attr)))
  20. if "FAIL" in res:
  21. raise Exception("nl80211 command failed")
  22. return binascii.unhexlify(res)
  23. @remote_compatible
  24. def test_cfg80211_disassociate(dev, apdev):
  25. """cfg80211 disassociation command"""
  26. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  27. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  28. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  29. if ev is None:
  30. raise Exception("No connection event received from hostapd")
  31. ifindex = int(dev[0].get_driver_status_field("ifindex"))
  32. attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
  33. attrs += build_nl80211_attr_u16('REASON_CODE', 1)
  34. attrs += build_nl80211_attr_mac('MAC', apdev[0]['bssid'])
  35. nl80211_command(dev[0], 'DISASSOCIATE', attrs)
  36. ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=5)
  37. if ev is None:
  38. raise Exception("No disconnection event received from hostapd")
  39. def nl80211_frame(dev, ifindex, frame, freq=None, duration=None, offchannel_tx_ok=False):
  40. attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
  41. if freq is not None:
  42. attrs += build_nl80211_attr_u32('WIPHY_FREQ', freq)
  43. if duration is not None:
  44. attrs += build_nl80211_attr_u32('DURATION', duration)
  45. if offchannel_tx_ok:
  46. attrs += build_nl80211_attr_flag('OFFCHANNEL_TX_OK')
  47. attrs += build_nl80211_attr('FRAME', frame)
  48. return parse_nl80211_attrs(nl80211_command(dev, 'FRAME', attrs))
  49. def nl80211_frame_wait_cancel(dev, ifindex, cookie):
  50. attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
  51. attrs += build_nl80211_attr('COOKIE', cookie)
  52. return nl80211_command(dev, 'FRAME_WAIT_CANCEL', attrs)
  53. def nl80211_remain_on_channel(dev, ifindex, freq, duration):
  54. attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
  55. attrs += build_nl80211_attr_u32('WIPHY_FREQ', freq)
  56. attrs += build_nl80211_attr_u32('DURATION', duration)
  57. return nl80211_command(dev, 'REMAIN_ON_CHANNEL', attrs)
  58. def test_cfg80211_tx_frame(dev, apdev, params):
  59. """cfg80211 offchannel TX frame command"""
  60. dev[0].p2p_start_go(freq='2412')
  61. go = WpaSupplicant(dev[0].group_ifname)
  62. frame = binascii.unhexlify("d0000000020000000100" + go.own_addr().translate(None, ':') + "02000000010000000409506f9a090001dd5e506f9a0902020025080401001f0502006414060500585804510b0906000200000000000b1000585804510b0102030405060708090a0b0d1d000200000000000108000000000000000000101100084465766963652041110500585804510bdd190050f204104a0001101012000200011049000600372a000120")
  63. ifindex = int(go.get_driver_status_field("ifindex"))
  64. res = nl80211_frame(go, ifindex, frame, freq=2422, duration=500,
  65. offchannel_tx_ok=True)
  66. time.sleep(0.1)
  67. # note: Uncommenting this seems to remove the incorrect channel issue
  68. #nl80211_frame_wait_cancel(dev[0], ifindex, res[nl80211_attr['COOKIE']])
  69. # note: this Action frame ends up getting sent incorrectly on 2422 MHz
  70. nl80211_frame(go, ifindex, frame, freq=2412)
  71. time.sleep(1.5)
  72. # note: also the Deauthenticate frame sent by the GO going down ends up
  73. # being transmitted incorrectly on 2422 MHz.
  74. del go
  75. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  76. "wlan.fc.type_subtype == 13", ["radiotap.channel.freq"])
  77. if out is not None:
  78. freq = out.splitlines()
  79. if len(freq) != 2:
  80. raise Exception("Unexpected number of Action frames (%d)" % len(freq))
  81. if freq[0] != "2422":
  82. raise Exception("First Action frame on unexpected channel: %s MHz" % freq[0])
  83. if freq[1] != "2412":
  84. raise Exception("Second Action frame on unexpected channel: %s MHz" % freq[1])
  85. @remote_compatible
  86. def test_cfg80211_wep_key_idx_change(dev, apdev):
  87. """WEP Shared Key authentication and key index change without deauth"""
  88. hapd = hostapd.add_ap(apdev[0],
  89. { "ssid": "wep-shared-key",
  90. "wep_key0": '"hello12345678"',
  91. "wep_key1": '"other12345678"',
  92. "auth_algs": "2" })
  93. id = dev[0].connect("wep-shared-key", key_mgmt="NONE", auth_alg="SHARED",
  94. wep_key0='"hello12345678"',
  95. wep_key1='"other12345678"',
  96. wep_tx_keyidx="0",
  97. scan_freq="2412")
  98. hwsim_utils.test_connectivity(dev[0], hapd)
  99. dev[0].set_network(id, "wep_tx_keyidx", "1")
  100. # clear cfg80211 auth state to allow new auth without deauth frame
  101. ifindex = int(dev[0].get_driver_status_field("ifindex"))
  102. attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
  103. attrs += build_nl80211_attr_u16('REASON_CODE', 1)
  104. attrs += build_nl80211_attr_mac('MAC', apdev[0]['bssid'])
  105. attrs += build_nl80211_attr_flag('LOCAL_STATE_CHANGE')
  106. nl80211_command(dev[0], 'DEAUTHENTICATE', attrs)
  107. dev[0].wait_disconnected(timeout=5, error="Local-deauth timed out")
  108. # the previous command results in deauth event followed by auto-reconnect
  109. dev[0].wait_connected(timeout=10, error="Reassociation timed out")
  110. hwsim_utils.test_connectivity(dev[0], hapd)
  111. @remote_compatible
  112. def test_cfg80211_hostapd_ext_sta_remove(dev, apdev):
  113. """cfg80211 DEL_STATION issued externally to hostapd"""
  114. hapd = hostapd.add_ap(apdev[0],
  115. { "ssid": "open" })
  116. id = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  117. ifindex = int(hapd.get_driver_status_field("ifindex"))
  118. attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
  119. attrs += build_nl80211_attr_u16('REASON_CODE', 1)
  120. attrs += build_nl80211_attr_u8('MGMT_SUBTYPE', 12)
  121. attrs += build_nl80211_attr_mac('MAC', dev[0].own_addr())
  122. nl80211_command(hapd, 'DEL_STATION', attrs)
  123. # Currently, hostapd ignores the NL80211_CMD_DEL_STATION event if
  124. # drv->device_ap_sme == 0 (which is the case with mac80211_hwsim), so no
  125. # further action happens here. If that event were to be used to remove the
  126. # STA entry from hostapd even in device_ap_sme == 0 case, this test case
  127. # could be extended to cover additional operations.