test_cfg80211.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. from nl80211 import *
  14. def nl80211_command(dev, cmd, attr):
  15. res = dev.request("VENDOR ffffffff {} {}".format(nl80211_cmd[cmd],
  16. binascii.hexlify(attr)))
  17. if "FAIL" in res:
  18. raise Exception("nl80211 command failed")
  19. return binascii.unhexlify(res)
  20. def test_cfg80211_disassociate(dev, apdev):
  21. """cfg80211 disassociation command"""
  22. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  23. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  24. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  25. if ev is None:
  26. raise Exception("No connection event received from hostapd")
  27. ifindex = int(dev[0].get_driver_status_field("ifindex"))
  28. attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
  29. attrs += build_nl80211_attr_u16('REASON_CODE', 1)
  30. attrs += build_nl80211_attr_mac('MAC', apdev[0]['bssid'])
  31. nl80211_command(dev[0], 'DISASSOCIATE', attrs)
  32. ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=5)
  33. if ev is None:
  34. raise Exception("No disconnection event received from hostapd")
  35. def nl80211_frame(dev, ifindex, frame, freq=None, duration=None, offchannel_tx_ok=False):
  36. attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
  37. if freq is not None:
  38. attrs += build_nl80211_attr_u32('WIPHY_FREQ', freq)
  39. if duration is not None:
  40. attrs += build_nl80211_attr_u32('DURATION', duration)
  41. if offchannel_tx_ok:
  42. attrs += build_nl80211_attr_flag('OFFCHANNEL_TX_OK')
  43. attrs += build_nl80211_attr('FRAME', frame)
  44. return parse_nl80211_attrs(nl80211_command(dev, 'FRAME', attrs))
  45. def nl80211_frame_wait_cancel(dev, ifindex, cookie):
  46. attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
  47. attrs += build_nl80211_attr('COOKIE', cookie)
  48. return nl80211_command(dev, 'FRAME_WAIT_CANCEL', attrs)
  49. def nl80211_remain_on_channel(dev, ifindex, freq, duration):
  50. attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
  51. attrs += build_nl80211_attr_u32('WIPHY_FREQ', freq)
  52. attrs += build_nl80211_attr_u32('DURATION', duration)
  53. return nl80211_command(dev, 'REMAIN_ON_CHANNEL', attrs)
  54. def test_cfg80211_tx_frame(dev, apdev, params):
  55. """cfg80211 offchannel TX frame command"""
  56. ifindex = int(dev[0].get_driver_status_field("ifindex"))
  57. frame = binascii.unhexlify("d000000002000000010002000000000002000000010000000409506f9a090001dd5e506f9a0902020025080401001f0502006414060500585804510b0906000200000000000b1000585804510b0102030405060708090a0b0d1d000200000000000108000000000000000000101100084465766963652041110500585804510bdd190050f204104a0001101012000200011049000600372a000120")
  58. dev[0].request("P2P_GROUP_ADD freq=2412")
  59. res = nl80211_frame(dev[0], ifindex, frame, freq=2422, duration=500,
  60. offchannel_tx_ok=True)
  61. time.sleep(0.1)
  62. # note: Uncommenting this seems to remove the incorrect channel issue
  63. #nl80211_frame_wait_cancel(dev[0], ifindex, res[nl80211_attr['COOKIE']])
  64. # note: this Action frame ends up getting sent incorrectly on 2422 MHz
  65. nl80211_frame(dev[0], ifindex, frame, freq=2412)
  66. time.sleep(1.5)
  67. # note: also the Deauthenticate frame sent by the GO going down ends up
  68. # being transmitted incorrectly on 2422 MHz.
  69. try:
  70. arg = [ "tshark",
  71. "-r", os.path.join(params['logdir'], "hwsim0.pcapng"),
  72. "-R", "wlan.fc.type_subtype == 13",
  73. "-Tfields", "-e", "radiotap.channel.freq" ]
  74. cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
  75. stderr=open('/dev/null', 'w'))
  76. except Exception, e:
  77. logger.info("Could run run tshark check: " + str(e))
  78. cmd = None
  79. pass
  80. if cmd:
  81. freq = cmd.stdout.read().splitlines()
  82. if len(freq) != 2:
  83. raise Exception("Unexpected number of Action frames (%d)" % len(freq))
  84. if freq[0] != "2422":
  85. raise Exception("First Action frame on unexpected channel: %s MHz" % freq[0])
  86. if freq[1] != "2412":
  87. raise Exception("Second Action frame on unexpected channel: %s MHz" % freq[1])