test_cfg80211.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. import logging
  7. logger = logging.getLogger()
  8. import binascii
  9. import os
  10. import subprocess
  11. import time
  12. import hostapd
  13. import hwsim_utils
  14. from nl80211 import *
  15. def nl80211_command(dev, cmd, attr):
  16. res = dev.request("VENDOR ffffffff {} {}".format(nl80211_cmd[cmd],
  17. binascii.hexlify(attr)))
  18. if "FAIL" in res:
  19. raise Exception("nl80211 command failed")
  20. return binascii.unhexlify(res)
  21. def test_cfg80211_disassociate(dev, apdev):
  22. """cfg80211 disassociation command"""
  23. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  24. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  25. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  26. if ev is None:
  27. raise Exception("No connection event received from hostapd")
  28. ifindex = int(dev[0].get_driver_status_field("ifindex"))
  29. attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
  30. attrs += build_nl80211_attr_u16('REASON_CODE', 1)
  31. attrs += build_nl80211_attr_mac('MAC', apdev[0]['bssid'])
  32. nl80211_command(dev[0], 'DISASSOCIATE', attrs)
  33. ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=5)
  34. if ev is None:
  35. raise Exception("No disconnection event received from hostapd")
  36. def nl80211_frame(dev, ifindex, frame, freq=None, duration=None, offchannel_tx_ok=False):
  37. attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
  38. if freq is not None:
  39. attrs += build_nl80211_attr_u32('WIPHY_FREQ', freq)
  40. if duration is not None:
  41. attrs += build_nl80211_attr_u32('DURATION', duration)
  42. if offchannel_tx_ok:
  43. attrs += build_nl80211_attr_flag('OFFCHANNEL_TX_OK')
  44. attrs += build_nl80211_attr('FRAME', frame)
  45. return parse_nl80211_attrs(nl80211_command(dev, 'FRAME', attrs))
  46. def nl80211_frame_wait_cancel(dev, ifindex, cookie):
  47. attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
  48. attrs += build_nl80211_attr('COOKIE', cookie)
  49. return nl80211_command(dev, 'FRAME_WAIT_CANCEL', attrs)
  50. def nl80211_remain_on_channel(dev, ifindex, freq, duration):
  51. attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
  52. attrs += build_nl80211_attr_u32('WIPHY_FREQ', freq)
  53. attrs += build_nl80211_attr_u32('DURATION', duration)
  54. return nl80211_command(dev, 'REMAIN_ON_CHANNEL', attrs)
  55. def test_cfg80211_tx_frame(dev, apdev, params):
  56. """cfg80211 offchannel TX frame command"""
  57. ifindex = int(dev[0].get_driver_status_field("ifindex"))
  58. frame = binascii.unhexlify("d000000002000000010002000000000002000000010000000409506f9a090001dd5e506f9a0902020025080401001f0502006414060500585804510b0906000200000000000b1000585804510b0102030405060708090a0b0d1d000200000000000108000000000000000000101100084465766963652041110500585804510bdd190050f204104a0001101012000200011049000600372a000120")
  59. dev[0].request("P2P_GROUP_ADD freq=2412")
  60. res = nl80211_frame(dev[0], ifindex, frame, freq=2422, duration=500,
  61. offchannel_tx_ok=True)
  62. time.sleep(0.1)
  63. # note: Uncommenting this seems to remove the incorrect channel issue
  64. #nl80211_frame_wait_cancel(dev[0], ifindex, res[nl80211_attr['COOKIE']])
  65. # note: this Action frame ends up getting sent incorrectly on 2422 MHz
  66. nl80211_frame(dev[0], ifindex, frame, freq=2412)
  67. time.sleep(1.5)
  68. # note: also the Deauthenticate frame sent by the GO going down ends up
  69. # being transmitted incorrectly on 2422 MHz.
  70. try:
  71. arg = [ "tshark",
  72. "-r", os.path.join(params['logdir'], "hwsim0.pcapng"),
  73. "-Y", "wlan.fc.type_subtype == 13",
  74. "-Tfields", "-e", "radiotap.channel.freq" ]
  75. cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
  76. stderr=open('/dev/null', 'w'))
  77. except Exception, e:
  78. logger.info("Could run run tshark check: " + str(e))
  79. cmd = None
  80. pass
  81. if cmd:
  82. freq = cmd.stdout.read().splitlines()
  83. if len(freq) != 2:
  84. raise Exception("Unexpected number of Action frames (%d)" % len(freq))
  85. if freq[0] != "2422":
  86. raise Exception("First Action frame on unexpected channel: %s MHz" % freq[0])
  87. if freq[1] != "2412":
  88. raise Exception("Second Action frame on unexpected channel: %s MHz" % freq[1])
  89. def test_cfg80211_wep_key_idx_change(dev, apdev):
  90. """WEP Shared Key authentication and key index change without deauth"""
  91. hapd = hostapd.add_ap(apdev[0]['ifname'],
  92. { "ssid": "wep-shared-key",
  93. "wep_key0": '"hello12345678"',
  94. "wep_key1": '"other12345678"',
  95. "auth_algs": "2" })
  96. id = dev[0].connect("wep-shared-key", key_mgmt="NONE", auth_alg="SHARED",
  97. wep_key0='"hello12345678"',
  98. wep_key1='"other12345678"',
  99. wep_tx_keyidx="0",
  100. scan_freq="2412")
  101. hwsim_utils.test_connectivity(dev[0], hapd)
  102. dev[0].set_network(id, "wep_tx_keyidx", "1")
  103. # clear cfg80211 auth state to allow new auth without deauth frame
  104. ifindex = int(dev[0].get_driver_status_field("ifindex"))
  105. attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
  106. attrs += build_nl80211_attr_u16('REASON_CODE', 1)
  107. attrs += build_nl80211_attr_mac('MAC', apdev[0]['bssid'])
  108. attrs += build_nl80211_attr_flag('LOCAL_STATE_CHANGE')
  109. nl80211_command(dev[0], 'DEAUTHENTICATE', attrs)
  110. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=5)
  111. if ev is None:
  112. raise Exception("Local-deauth timed out")
  113. # the previous command results in deauth event followed by auto-reconnect
  114. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
  115. if ev is None:
  116. raise Exception("Reassociation with the AP timed out")
  117. hwsim_utils.test_connectivity(dev[0], hapd)