nl80211.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. # nl80211 definitions
  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 binascii
  7. import struct
  8. nl80211_cmd = {
  9. 'GET_WIPHY': 1,
  10. 'SET_WIPHY': 2,
  11. 'NEW_WIPHY': 3,
  12. 'DEL_WIPHY': 4,
  13. 'GET_INTERFACE': 5,
  14. 'SET_INTERFACE': 6,
  15. 'NEW_INTERFACE': 7,
  16. 'DEL_INTERFACE': 8,
  17. 'GET_KEY': 9,
  18. 'SET_KEY': 10,
  19. 'NEW_KEY': 11,
  20. 'DEL_KEY': 12,
  21. 'GET_BEACON': 13,
  22. 'SET_BEACON': 14,
  23. 'START_AP': 15,
  24. 'STOP_AP': 16,
  25. 'GET_STATION': 17,
  26. 'SET_STATION': 18,
  27. 'NEW_STATION': 19,
  28. 'DEL_STATION': 20,
  29. 'GET_MPATH': 21,
  30. 'SET_MPATH': 22,
  31. 'NEW_MPATH': 23,
  32. 'DEL_MPATH': 24,
  33. 'SET_BSS': 25,
  34. 'SET_REG': 26,
  35. 'REQ_SET_REG': 27,
  36. 'GET_MESH_CONFIG': 28,
  37. 'SET_MESH_CONFIG': 29,
  38. 'SET_MGMT_EXTRA_IE[RESERVED]': 30,
  39. 'GET_REG': 31,
  40. 'GET_SCAN': 32,
  41. 'TRIGGER_SCAN': 33,
  42. 'NEW_SCAN_RESULTS': 34,
  43. 'SCAN_ABORTED': 35,
  44. 'REG_CHANGE': 36,
  45. 'AUTHENTICATE': 37,
  46. 'ASSOCIATE': 38,
  47. 'DEAUTHENTICATE': 39,
  48. 'DISASSOCIATE': 40,
  49. 'MICHAEL_MIC_FAILURE': 41,
  50. 'REG_BEACON_HINT': 42,
  51. 'JOIN_IBSS': 43,
  52. 'LEAVE_IBSS': 44,
  53. 'TESTMODE': 45,
  54. 'CONNECT': 46,
  55. 'ROAM': 47,
  56. 'DISCONNECT': 48,
  57. 'SET_WIPHY_NETNS': 49,
  58. 'GET_SURVEY': 50,
  59. 'NEW_SURVEY_RESULTS': 51,
  60. 'SET_PMKSA': 52,
  61. 'DEL_PMKSA': 53,
  62. 'FLUSH_PMKSA': 54,
  63. 'REMAIN_ON_CHANNEL': 55,
  64. 'CANCEL_REMAIN_ON_CHANNEL': 56,
  65. 'SET_TX_BITRATE_MASK': 57,
  66. 'REGISTER_FRAME': 58,
  67. 'FRAME': 59,
  68. 'FRAME_TX_STATUS': 60,
  69. 'SET_POWER_SAVE': 61,
  70. 'GET_POWER_SAVE': 62,
  71. 'SET_CQM': 63,
  72. 'NOTIFY_CQM': 64,
  73. 'SET_CHANNEL': 65,
  74. 'SET_WDS_PEER': 66,
  75. 'FRAME_WAIT_CANCEL': 67,
  76. 'JOIN_MESH': 68,
  77. 'LEAVE_MESH': 69,
  78. 'UNPROT_DEAUTHENTICATE': 70,
  79. 'UNPROT_DISASSOCIATE': 71,
  80. 'NEW_PEER_CANDIDATE': 72,
  81. 'GET_WOWLAN': 73,
  82. 'SET_WOWLAN': 74,
  83. 'START_SCHED_SCAN': 75,
  84. 'STOP_SCHED_SCAN': 76,
  85. 'SCHED_SCAN_RESULTS': 77,
  86. 'SCHED_SCAN_STOPPED': 78,
  87. 'SET_REKEY_OFFLOAD': 79,
  88. 'PMKSA_CANDIDATE': 80,
  89. 'TDLS_OPER': 81,
  90. 'TDLS_MGMT': 82,
  91. 'UNEXPECTED_FRAME': 83,
  92. 'PROBE_CLIENT': 84,
  93. 'REGISTER_BEACONS': 85,
  94. 'UNEXPECTED_4ADDR_FRAME': 86,
  95. 'SET_NOACK_MAP': 87,
  96. 'CH_SWITCH_NOTIFY': 88,
  97. 'START_P2P_DEVICE': 89,
  98. 'STOP_P2P_DEVICE': 90,
  99. 'CONN_FAILED': 91,
  100. 'SET_MCAST_RATE': 92,
  101. 'SET_MAC_ACL': 93,
  102. 'RADAR_DETECT': 94,
  103. 'GET_PROTOCOL_FEATURES': 95,
  104. 'UPDATE_FT_IES': 96,
  105. 'FT_EVENT': 97,
  106. 'CRIT_PROTOCOL_START': 98,
  107. 'CRIT_PROTOCOL_STOP': 99,
  108. 'GET_COALESCE': 100,
  109. 'SET_COALESCE': 101,
  110. 'CHANNEL_SWITCH': 102,
  111. 'VENDOR': 103,
  112. 'SET_QOS_MAP': 104,
  113. }
  114. nl80211_attr = {
  115. 'WIPHY': 1,
  116. 'WIPHY_NAME': 2,
  117. 'IFINDEX': 3,
  118. 'IFNAME': 4,
  119. 'IFTYPE': 5,
  120. 'MAC': 6,
  121. 'KEY_DATA': 7,
  122. 'KEY_IDX': 8,
  123. 'KEY_CIPHER': 9,
  124. 'KEY_SEQ': 10,
  125. 'KEY_DEFAULT': 11,
  126. 'BEACON_INTERVAL': 12,
  127. 'DTIM_PERIOD': 13,
  128. 'BEACON_HEAD': 14,
  129. 'BEACON_TAIL': 15,
  130. 'STA_AID': 16,
  131. 'STA_FLAGS': 17,
  132. 'STA_LISTEN_INTERVAL': 18,
  133. 'STA_SUPPORTED_RATES': 19,
  134. 'STA_VLAN': 20,
  135. 'STA_INFO': 21,
  136. 'WIPHY_BANDS': 22,
  137. 'MNTR_FLAGS': 23,
  138. 'MESH_ID': 24,
  139. 'STA_PLINK_ACTION': 25,
  140. 'MPATH_NEXT_HOP': 26,
  141. 'MPATH_INFO': 27,
  142. 'BSS_CTS_PROT': 28,
  143. 'BSS_SHORT_PREAMBLE': 29,
  144. 'BSS_SHORT_SLOT_TIME': 30,
  145. 'HT_CAPABILITY': 31,
  146. 'SUPPORTED_IFTYPES': 32,
  147. 'REG_ALPHA2': 33,
  148. 'REG_RULES': 34,
  149. 'MESH_CONFIG': 35,
  150. 'BSS_BASIC_RATES': 36,
  151. 'WIPHY_TXQ_PARAMS': 37,
  152. 'WIPHY_FREQ': 38,
  153. 'WIPHY_CHANNEL_TYPE': 39,
  154. 'KEY_DEFAULT_MGMT': 40,
  155. 'MGMT_SUBTYPE': 41,
  156. 'IE': 42,
  157. 'MAX_NUM_SCAN_SSIDS': 43,
  158. 'SCAN_FREQUENCIES': 44,
  159. 'SCAN_SSIDS': 45,
  160. 'GENERATION': 46,
  161. 'BSS': 47,
  162. 'REG_INITIATOR': 48,
  163. 'REG_TYPE': 49,
  164. 'SUPPORTED_COMMANDS': 50,
  165. 'FRAME': 51,
  166. 'SSID': 52,
  167. 'AUTH_TYPE': 53,
  168. 'REASON_CODE': 54,
  169. 'KEY_TYPE': 55,
  170. 'MAX_SCAN_IE_LEN': 56,
  171. 'CIPHER_SUITES': 57,
  172. 'FREQ_BEFORE': 58,
  173. 'FREQ_AFTER': 59,
  174. 'FREQ_FIXED': 60,
  175. 'WIPHY_RETRY_SHORT': 61,
  176. 'WIPHY_RETRY_LONG': 62,
  177. 'WIPHY_FRAG_THRESHOLD': 63,
  178. 'WIPHY_RTS_THRESHOLD': 64,
  179. 'TIMED_OUT': 65,
  180. 'USE_MFP': 66,
  181. 'STA_FLAGS2': 67,
  182. 'CONTROL_PORT': 68,
  183. 'TESTDATA': 69,
  184. 'PRIVACY': 70,
  185. 'DISCONNECTED_BY_AP': 71,
  186. 'STATUS_CODE': 72,
  187. 'CIPHER_SUITES_PAIRWISE': 73,
  188. 'CIPHER_SUITE_GROUP': 74,
  189. 'WPA_VERSIONS': 75,
  190. 'AKM_SUITES': 76,
  191. 'REQ_IE': 77,
  192. 'RESP_IE': 78,
  193. 'PREV_BSSID': 79,
  194. 'KEY': 80,
  195. 'KEYS': 81,
  196. 'PID': 82,
  197. '4ADDR': 83,
  198. 'SURVEY_INFO': 84,
  199. 'PMKID': 85,
  200. 'MAX_NUM_PMKIDS': 86,
  201. 'DURATION': 87,
  202. 'COOKIE': 88,
  203. 'WIPHY_COVERAGE_CLASS': 89,
  204. 'TX_RATES': 90,
  205. 'FRAME_MATCH': 91,
  206. 'ACK': 92,
  207. 'PS_STATE': 93,
  208. 'CQM': 94,
  209. 'LOCAL_STATE_CHANGE': 95,
  210. 'AP_ISOLATE': 96,
  211. 'WIPHY_TX_POWER_SETTING': 97,
  212. 'WIPHY_TX_POWER_LEVEL': 98,
  213. 'TX_FRAME_TYPES': 99,
  214. 'RX_FRAME_TYPES': 100,
  215. 'FRAME_TYPE': 101,
  216. 'CONTROL_PORT_ETHERTYPE': 102,
  217. 'CONTROL_PORT_NO_ENCRYPT': 103,
  218. 'SUPPORT_IBSS_RSN': 104,
  219. 'WIPHY_ANTENNA_TX': 105,
  220. 'WIPHY_ANTENNA_RX': 106,
  221. 'MCAST_RATE': 107,
  222. 'OFFCHANNEL_TX_OK': 108,
  223. 'BSS_HT_OPMODE': 109,
  224. 'KEY_DEFAULT_TYPES': 110,
  225. 'MAX_REMAIN_ON_CHANNEL_DURATION': 111,
  226. 'MESH_SETUP': 112,
  227. 'WIPHY_ANTENNA_AVAIL_TX': 113,
  228. 'WIPHY_ANTENNA_AVAIL_RX': 114,
  229. 'SUPPORT_MESH_AUTH': 115,
  230. 'STA_PLINK_STATE': 116,
  231. 'WOWLAN_TRIGGERS': 117,
  232. 'WOWLAN_TRIGGERS_SUPPORTED': 118,
  233. 'SCHED_SCAN_INTERVAL': 119,
  234. 'INTERFACE_COMBINATIONS': 120,
  235. 'SOFTWARE_IFTYPES': 121,
  236. 'REKEY_DATA': 122,
  237. 'MAX_NUM_SCHED_SCAN_SSIDS': 123,
  238. 'MAX_SCHED_SCAN_IE_LEN': 124,
  239. 'SCAN_SUPP_RATES': 125,
  240. 'HIDDEN_SSID': 126,
  241. 'IE_PROBE_RESP': 127,
  242. 'IE_ASSOC_RESP': 128,
  243. 'STA_WME': 129,
  244. 'SUPPORT_AP_UAPSD': 130,
  245. 'ROAM_SUPPORT': 131,
  246. 'SCHED_SCAN_MATCH': 132,
  247. 'MAX_MATCH_SETS': 133,
  248. 'PMKSA_CANDIDATE': 134,
  249. 'TX_NO_CCK_RATE': 135,
  250. 'TDLS_ACTION': 136,
  251. 'TDLS_DIALOG_TOKEN': 137,
  252. 'TDLS_OPERATION': 138,
  253. 'TDLS_SUPPORT': 139,
  254. 'TDLS_EXTERNAL_SETUP': 140,
  255. 'DEVICE_AP_SME': 141,
  256. 'DONT_WAIT_FOR_ACK': 142,
  257. 'FEATURE_FLAGS': 143,
  258. 'PROBE_RESP_OFFLOAD': 144,
  259. 'PROBE_RESP': 145,
  260. 'DFS_REGION': 146,
  261. 'DISABLE_HT': 147,
  262. 'HT_CAPABILITY_MASK': 148,
  263. 'NOACK_MAP': 149,
  264. 'INACTIVITY_TIMEOUT': 150,
  265. 'RX_SIGNAL_DBM': 151,
  266. 'BG_SCAN_PERIOD': 152,
  267. 'WDEV': 153,
  268. 'USER_REG_HINT_TYPE': 154,
  269. 'CONN_FAILED_REASON': 155,
  270. 'SAE_DATA': 156,
  271. 'VHT_CAPABILITY': 157,
  272. 'SCAN_FLAGS': 158,
  273. 'CHANNEL_WIDTH': 159,
  274. 'CENTER_FREQ1': 160,
  275. 'CENTER_FREQ2': 161,
  276. 'P2P_CTWINDOW': 162,
  277. 'P2P_OPPPS': 163,
  278. 'LOCAL_MESH_POWER_MODE': 164,
  279. 'ACL_POLICY': 165,
  280. 'MAC_ADDRS': 166,
  281. 'MAC_ACL_MAX': 167,
  282. 'RADAR_EVENT': 168,
  283. 'EXT_CAPA': 169,
  284. 'EXT_CAPA_MASK': 170,
  285. 'STA_CAPABILITY': 171,
  286. 'STA_EXT_CAPABILITY': 172,
  287. 'PROTOCOL_FEATURES': 173,
  288. 'SPLIT_WIPHY_DUMP': 174,
  289. 'DISABLE_VHT': 175,
  290. 'VHT_CAPABILITY_MASK': 176,
  291. 'MDID': 177,
  292. 'IE_RIC': 178,
  293. 'CRIT_PROT_ID': 179,
  294. 'MAX_CRIT_PROT_DURATION': 180,
  295. 'PEER_AID': 181,
  296. 'COALESCE_RULE': 182,
  297. 'CH_SWITCH_COUNT': 183,
  298. 'CH_SWITCH_BLOCK_TX': 184,
  299. 'CSA_IES': 185,
  300. 'CSA_C_OFF_BEACON': 186,
  301. 'CSA_C_OFF_PRESP': 187,
  302. 'RXMGMT_FLAGS': 188,
  303. 'STA_SUPPORTED_CHANNELS': 189,
  304. 'STA_SUPPORTED_OPER_CLASSES': 190,
  305. 'HANDLE_DFS': 191,
  306. 'SUPPORT_5_MHZ': 192,
  307. 'SUPPORT_10_MHZ': 193,
  308. 'OPMODE_NOTIF': 194,
  309. 'VENDOR_ID': 195,
  310. 'VENDOR_SUBCMD': 196,
  311. 'VENDOR_DATA': 197,
  312. 'VENDOR_EVENTS': 198,
  313. 'QOS_MAP': 199,
  314. 'MAC_HINT': 200,
  315. 'WIPHY_FREQ_HINT': 201,
  316. 'MAX_AP_ASSOC_STA': 202,
  317. }
  318. def build_nl80211_attr(id, val):
  319. attr = struct.pack("@HH", 4 + len(val), nl80211_attr[id]) + val
  320. if len(attr) % 4 != 0:
  321. attr += '\0' * (4 - (len(attr) % 4))
  322. return attr
  323. def build_nl80211_attr_u32(id, val):
  324. return build_nl80211_attr(id, struct.pack("@I", val))
  325. def build_nl80211_attr_u16(id, val):
  326. return build_nl80211_attr(id, struct.pack("@H", val))
  327. def build_nl80211_attr_u8(id, val):
  328. return build_nl80211_attr(id, struct.pack("@B", val))
  329. def build_nl80211_attr_flag(id):
  330. return build_nl80211_attr(id, '')
  331. def build_nl80211_attr_mac(id, val):
  332. addr = struct.unpack('6B', binascii.unhexlify(val.replace(':','')))
  333. aval = struct.pack('<6B', *addr)
  334. return build_nl80211_attr(id, aval)
  335. def parse_nl80211_attrs(msg):
  336. attrs = {}
  337. while len(msg) >= 4:
  338. alen,attr = struct.unpack("@HH", msg[0:4])
  339. if alen < 4:
  340. raise Exception("Too short nl80211 attribute")
  341. alen -= 4
  342. msg = msg[4:]
  343. if alen > len(msg):
  344. raise Exception("nl80211 attribute underflow")
  345. attrs[attr] = msg[0:alen]
  346. msg = msg[alen:]
  347. return attrs