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