]>
Commit | Line | Data |
---|---|---|
748f8489 | 1 | #include <ctype.h> |
51e9bd80 JB |
2 | #include <netlink/attr.h> |
3 | #include <errno.h> | |
4 | #include <stdbool.h> | |
3d1e8704 | 5 | #include "iw.h" |
f5f7b1d0 | 6 | #include "nl80211.h" |
3d1e8704 | 7 | |
7f87d3cf | 8 | void mac_addr_n2a(char *mac_addr, const unsigned char *arg) |
3d1e8704 | 9 | { |
53e5ce7a | 10 | int i, l; |
3d1e8704 LCC |
11 | |
12 | l = 0; | |
13 | for (i = 0; i < ETH_ALEN ; i++) { | |
14 | if (i == 0) { | |
53e5ce7a | 15 | sprintf(mac_addr+l, "%02x", arg[i]); |
3d1e8704 LCC |
16 | l += 2; |
17 | } else { | |
53e5ce7a | 18 | sprintf(mac_addr+l, ":%02x", arg[i]); |
3d1e8704 LCC |
19 | l += 3; |
20 | } | |
21 | } | |
3d1e8704 LCC |
22 | } |
23 | ||
24 | int mac_addr_a2n(unsigned char *mac_addr, char *arg) | |
25 | { | |
26 | int i; | |
27 | ||
28 | for (i = 0; i < ETH_ALEN ; i++) { | |
29 | int temp; | |
30 | char *cp = strchr(arg, ':'); | |
31 | if (cp) { | |
32 | *cp = 0; | |
33 | cp++; | |
34 | } | |
35 | if (sscanf(arg, "%x", &temp) != 1) | |
36 | return -1; | |
37 | if (temp < 0 || temp > 255) | |
38 | return -1; | |
39 | ||
40 | mac_addr[i] = temp; | |
41 | if (!cp) | |
42 | break; | |
43 | arg = cp; | |
44 | } | |
45 | if (i < ETH_ALEN - 1) | |
46 | return -1; | |
47 | ||
48 | return 0; | |
49 | } | |
541ef425 | 50 | |
3ff24563 JB |
51 | int parse_hex_mask(char *hexmask, unsigned char **result, size_t *result_len, |
52 | unsigned char **mask) | |
236d4191 | 53 | { |
3ff24563 JB |
54 | size_t len = strlen(hexmask) / 2; |
55 | unsigned char *result_val; | |
56 | unsigned char *result_mask = NULL; | |
57 | ||
236d4191 JB |
58 | int pos = 0; |
59 | ||
3ff24563 | 60 | *result_len = 0; |
236d4191 | 61 | |
3ff24563 JB |
62 | result_val = calloc(len + 2, 1); |
63 | if (!result_val) | |
64 | goto error; | |
65 | *result = result_val; | |
66 | if (mask) { | |
67 | result_mask = calloc(DIV_ROUND_UP(len, 8) + 2, 1); | |
68 | if (!result_mask) | |
69 | goto error; | |
70 | *mask = result_mask; | |
71 | } | |
236d4191 JB |
72 | |
73 | while (1) { | |
3ff24563 | 74 | char *cp = strchr(hexmask, ':'); |
236d4191 JB |
75 | if (cp) { |
76 | *cp = 0; | |
77 | cp++; | |
78 | } | |
236d4191 | 79 | |
3ff24563 JB |
80 | if (result_mask && (strcmp(hexmask, "-") == 0 || |
81 | strcmp(hexmask, "xx") == 0 || | |
82 | strcmp(hexmask, "--") == 0)) { | |
83 | /* skip this byte and leave mask bit unset */ | |
84 | } else { | |
85 | int temp, mask_pos; | |
86 | char *end; | |
87 | ||
88 | temp = strtoul(hexmask, &end, 16); | |
89 | if (*end) | |
90 | goto error; | |
91 | if (temp < 0 || temp > 255) | |
92 | goto error; | |
93 | result_val[pos] = temp; | |
94 | ||
95 | mask_pos = pos / 8; | |
96 | if (result_mask) | |
97 | result_mask[mask_pos] |= 1 << (pos % 8); | |
98 | } | |
99 | ||
100 | (*result_len)++; | |
101 | pos++; | |
236d4191 | 102 | |
236d4191 JB |
103 | if (!cp) |
104 | break; | |
3ff24563 | 105 | hexmask = cp; |
236d4191 JB |
106 | } |
107 | ||
3ff24563 | 108 | return 0; |
236d4191 | 109 | error: |
3ff24563 JB |
110 | free(result_val); |
111 | free(result_mask); | |
112 | return -1; | |
113 | } | |
114 | ||
115 | unsigned char *parse_hex(char *hex, size_t *outlen) | |
116 | { | |
117 | unsigned char *result; | |
118 | ||
119 | if (parse_hex_mask(hex, &result, outlen, NULL)) | |
120 | return NULL; | |
121 | return result; | |
236d4191 JB |
122 | } |
123 | ||
541ef425 JB |
124 | static const char *ifmodes[NL80211_IFTYPE_MAX + 1] = { |
125 | "unspecified", | |
126 | "IBSS", | |
34e78ed0 | 127 | "managed", |
541ef425 | 128 | "AP", |
34e78ed0 | 129 | "AP/VLAN", |
541ef425 | 130 | "WDS", |
34e78ed0 | 131 | "monitor", |
a4464243 JB |
132 | "mesh point", |
133 | "P2P-client", | |
134 | "P2P-GO", | |
add40bbd | 135 | "P2P-device", |
3955e524 | 136 | "outside context of a BSS", |
ed9b77ec | 137 | "NAN", |
541ef425 JB |
138 | }; |
139 | ||
140 | static char modebuf[100]; | |
141 | ||
142 | const char *iftype_name(enum nl80211_iftype iftype) | |
143 | { | |
a66b3a35 | 144 | if (iftype <= NL80211_IFTYPE_MAX && ifmodes[iftype]) |
541ef425 JB |
145 | return ifmodes[iftype]; |
146 | sprintf(modebuf, "Unknown mode (%d)", iftype); | |
147 | return modebuf; | |
148 | } | |
379f8397 | 149 | |
9990c1e9 | 150 | static const char *commands[NL80211_CMD_MAX + 1] = { |
02aef469 | 151 | /* |
f45d4583 | 152 | * sed 's%^\tNL80211_CMD_%%;t n;d;:n s%^\([^=]*\),.*%\t[NL80211_CMD_\1] = \"\L\1\",%;t;d' nl80211.h | grep -v "reserved" |
02aef469 JB |
153 | */ |
154 | [NL80211_CMD_UNSPEC] = "unspec", | |
06339499 JB |
155 | [NL80211_CMD_GET_WIPHY] = "get_wiphy", |
156 | [NL80211_CMD_SET_WIPHY] = "set_wiphy", | |
157 | [NL80211_CMD_NEW_WIPHY] = "new_wiphy", | |
158 | [NL80211_CMD_DEL_WIPHY] = "del_wiphy", | |
159 | [NL80211_CMD_GET_INTERFACE] = "get_interface", | |
160 | [NL80211_CMD_SET_INTERFACE] = "set_interface", | |
161 | [NL80211_CMD_NEW_INTERFACE] = "new_interface", | |
162 | [NL80211_CMD_DEL_INTERFACE] = "del_interface", | |
163 | [NL80211_CMD_GET_KEY] = "get_key", | |
164 | [NL80211_CMD_SET_KEY] = "set_key", | |
165 | [NL80211_CMD_NEW_KEY] = "new_key", | |
166 | [NL80211_CMD_DEL_KEY] = "del_key", | |
167 | [NL80211_CMD_GET_BEACON] = "get_beacon", | |
168 | [NL80211_CMD_SET_BEACON] = "set_beacon", | |
02aef469 JB |
169 | [NL80211_CMD_START_AP] = "start_ap", |
170 | [NL80211_CMD_STOP_AP] = "stop_ap", | |
06339499 JB |
171 | [NL80211_CMD_GET_STATION] = "get_station", |
172 | [NL80211_CMD_SET_STATION] = "set_station", | |
173 | [NL80211_CMD_NEW_STATION] = "new_station", | |
174 | [NL80211_CMD_DEL_STATION] = "del_station", | |
175 | [NL80211_CMD_GET_MPATH] = "get_mpath", | |
176 | [NL80211_CMD_SET_MPATH] = "set_mpath", | |
177 | [NL80211_CMD_NEW_MPATH] = "new_mpath", | |
178 | [NL80211_CMD_DEL_MPATH] = "del_mpath", | |
179 | [NL80211_CMD_SET_BSS] = "set_bss", | |
180 | [NL80211_CMD_SET_REG] = "set_reg", | |
02aef469 JB |
181 | [NL80211_CMD_REQ_SET_REG] = "req_set_reg", |
182 | [NL80211_CMD_GET_MESH_CONFIG] = "get_mesh_config", | |
183 | [NL80211_CMD_SET_MESH_CONFIG] = "set_mesh_config", | |
06339499 JB |
184 | [NL80211_CMD_GET_REG] = "get_reg", |
185 | [NL80211_CMD_GET_SCAN] = "get_scan", | |
186 | [NL80211_CMD_TRIGGER_SCAN] = "trigger_scan", | |
187 | [NL80211_CMD_NEW_SCAN_RESULTS] = "new_scan_results", | |
188 | [NL80211_CMD_SCAN_ABORTED] = "scan_aborted", | |
189 | [NL80211_CMD_REG_CHANGE] = "reg_change", | |
190 | [NL80211_CMD_AUTHENTICATE] = "authenticate", | |
191 | [NL80211_CMD_ASSOCIATE] = "associate", | |
192 | [NL80211_CMD_DEAUTHENTICATE] = "deauthenticate", | |
193 | [NL80211_CMD_DISASSOCIATE] = "disassociate", | |
194 | [NL80211_CMD_MICHAEL_MIC_FAILURE] = "michael_mic_failure", | |
195 | [NL80211_CMD_REG_BEACON_HINT] = "reg_beacon_hint", | |
196 | [NL80211_CMD_JOIN_IBSS] = "join_ibss", | |
197 | [NL80211_CMD_LEAVE_IBSS] = "leave_ibss", | |
198 | [NL80211_CMD_TESTMODE] = "testmode", | |
199 | [NL80211_CMD_CONNECT] = "connect", | |
200 | [NL80211_CMD_ROAM] = "roam", | |
201 | [NL80211_CMD_DISCONNECT] = "disconnect", | |
202 | [NL80211_CMD_SET_WIPHY_NETNS] = "set_wiphy_netns", | |
203 | [NL80211_CMD_GET_SURVEY] = "get_survey", | |
02aef469 | 204 | [NL80211_CMD_NEW_SURVEY_RESULTS] = "new_survey_results", |
06339499 JB |
205 | [NL80211_CMD_SET_PMKSA] = "set_pmksa", |
206 | [NL80211_CMD_DEL_PMKSA] = "del_pmksa", | |
207 | [NL80211_CMD_FLUSH_PMKSA] = "flush_pmksa", | |
208 | [NL80211_CMD_REMAIN_ON_CHANNEL] = "remain_on_channel", | |
209 | [NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = "cancel_remain_on_channel", | |
210 | [NL80211_CMD_SET_TX_BITRATE_MASK] = "set_tx_bitrate_mask", | |
02aef469 JB |
211 | [NL80211_CMD_REGISTER_FRAME] = "register_frame", |
212 | [NL80211_CMD_FRAME] = "frame", | |
213 | [NL80211_CMD_FRAME_TX_STATUS] = "frame_tx_status", | |
214 | [NL80211_CMD_SET_POWER_SAVE] = "set_power_save", | |
215 | [NL80211_CMD_GET_POWER_SAVE] = "get_power_save", | |
216 | [NL80211_CMD_SET_CQM] = "set_cqm", | |
217 | [NL80211_CMD_NOTIFY_CQM] = "notify_cqm", | |
7b96e9e2 | 218 | [NL80211_CMD_SET_CHANNEL] = "set_channel", |
dcf57038 JB |
219 | [NL80211_CMD_SET_WDS_PEER] = "set_wds_peer", |
220 | [NL80211_CMD_FRAME_WAIT_CANCEL] = "frame_wait_cancel", | |
221 | [NL80211_CMD_JOIN_MESH] = "join_mesh", | |
222 | [NL80211_CMD_LEAVE_MESH] = "leave_mesh", | |
02aef469 JB |
223 | [NL80211_CMD_UNPROT_DEAUTHENTICATE] = "unprot_deauthenticate", |
224 | [NL80211_CMD_UNPROT_DISASSOCIATE] = "unprot_disassociate", | |
225 | [NL80211_CMD_NEW_PEER_CANDIDATE] = "new_peer_candidate", | |
226 | [NL80211_CMD_GET_WOWLAN] = "get_wowlan", | |
227 | [NL80211_CMD_SET_WOWLAN] = "set_wowlan", | |
228 | [NL80211_CMD_START_SCHED_SCAN] = "start_sched_scan", | |
229 | [NL80211_CMD_STOP_SCHED_SCAN] = "stop_sched_scan", | |
230 | [NL80211_CMD_SCHED_SCAN_RESULTS] = "sched_scan_results", | |
231 | [NL80211_CMD_SCHED_SCAN_STOPPED] = "sched_scan_stopped", | |
850ab15d | 232 | [NL80211_CMD_SET_REKEY_OFFLOAD] = "set_rekey_offload", |
02aef469 JB |
233 | [NL80211_CMD_PMKSA_CANDIDATE] = "pmksa_candidate", |
234 | [NL80211_CMD_TDLS_OPER] = "tdls_oper", | |
235 | [NL80211_CMD_TDLS_MGMT] = "tdls_mgmt", | |
236 | [NL80211_CMD_UNEXPECTED_FRAME] = "unexpected_frame", | |
237 | [NL80211_CMD_PROBE_CLIENT] = "probe_client", | |
238 | [NL80211_CMD_REGISTER_BEACONS] = "register_beacons", | |
239 | [NL80211_CMD_UNEXPECTED_4ADDR_FRAME] = "unexpected_4addr_frame", | |
240 | [NL80211_CMD_SET_NOACK_MAP] = "set_noack_map", | |
241 | [NL80211_CMD_CH_SWITCH_NOTIFY] = "ch_switch_notify", | |
65d07957 JB |
242 | [NL80211_CMD_START_P2P_DEVICE] = "start_p2p_device", |
243 | [NL80211_CMD_STOP_P2P_DEVICE] = "stop_p2p_device", | |
244 | [NL80211_CMD_CONN_FAILED] = "conn_failed", | |
240aa0ef JB |
245 | [NL80211_CMD_SET_MCAST_RATE] = "set_mcast_rate", |
246 | [NL80211_CMD_SET_MAC_ACL] = "set_mac_acl", | |
247 | [NL80211_CMD_RADAR_DETECT] = "radar_detect", | |
248 | [NL80211_CMD_GET_PROTOCOL_FEATURES] = "get_protocol_features", | |
249 | [NL80211_CMD_UPDATE_FT_IES] = "update_ft_ies", | |
250 | [NL80211_CMD_FT_EVENT] = "ft_event", | |
251 | [NL80211_CMD_CRIT_PROTOCOL_START] = "crit_protocol_start", | |
252 | [NL80211_CMD_CRIT_PROTOCOL_STOP] = "crit_protocol_stop", | |
0624c45d JB |
253 | [NL80211_CMD_GET_COALESCE] = "get_coalesce", |
254 | [NL80211_CMD_SET_COALESCE] = "set_coalesce", | |
255 | [NL80211_CMD_CHANNEL_SWITCH] = "channel_switch", | |
256 | [NL80211_CMD_VENDOR] = "vendor", | |
b8820d2b JB |
257 | [NL80211_CMD_SET_QOS_MAP] = "set_qos_map", |
258 | [NL80211_CMD_ADD_TX_TS] = "add_tx_ts", | |
259 | [NL80211_CMD_DEL_TX_TS] = "del_tx_ts", | |
260 | [NL80211_CMD_GET_MPP] = "get_mpp", | |
261 | [NL80211_CMD_JOIN_OCB] = "join_ocb", | |
262 | [NL80211_CMD_LEAVE_OCB] = "leave_ocb", | |
263 | [NL80211_CMD_CH_SWITCH_STARTED_NOTIFY] = "ch_switch_started_notify", | |
8823f67b JB |
264 | [NL80211_CMD_TDLS_CHANNEL_SWITCH] = "tdls_channel_switch", |
265 | [NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH] = "tdls_cancel_channel_switch", | |
266 | [NL80211_CMD_WIPHY_REG_CHANGE] = "wiphy_reg_change", | |
478438c0 | 267 | [NL80211_CMD_ABORT_SCAN] = "abort_scan", |
2781d641 JB |
268 | [NL80211_CMD_START_NAN] = "start_nan", |
269 | [NL80211_CMD_STOP_NAN] = "stop_nan", | |
270 | [NL80211_CMD_ADD_NAN_FUNCTION] = "add_nan_function", | |
271 | [NL80211_CMD_DEL_NAN_FUNCTION] = "del_nan_function", | |
272 | [NL80211_CMD_CHANGE_NAN_CONFIG] = "change_nan_config", | |
273 | [NL80211_CMD_NAN_MATCH] = "nan_match", | |
8a9e0b30 JB |
274 | [NL80211_CMD_SET_MULTICAST_TO_UNICAST] = "set_multicast_to_unicast", |
275 | [NL80211_CMD_UPDATE_CONNECT_PARAMS] = "update_connect_params", | |
378286c9 JB |
276 | [NL80211_CMD_SET_PMK] = "set_pmk", |
277 | [NL80211_CMD_DEL_PMK] = "del_pmk", | |
278 | [NL80211_CMD_PORT_AUTHORIZED] = "port_authorized", | |
279 | [NL80211_CMD_RELOAD_REGDB] = "reload_regdb", | |
806bb9ac JB |
280 | [NL80211_CMD_EXTERNAL_AUTH] = "external_auth", |
281 | [NL80211_CMD_STA_OPMODE_CHANGED] = "sta_opmode_changed", | |
282 | [NL80211_CMD_CONTROL_PORT_FRAME] = "control_port_frame", | |
283 | [NL80211_CMD_GET_FTM_RESPONDER_STATS] = "get_ftm_responder_stats", | |
284 | [NL80211_CMD_PEER_MEASUREMENT_START] = "peer_measurement_start", | |
285 | [NL80211_CMD_PEER_MEASUREMENT_RESULT] = "peer_measurement_result", | |
286 | [NL80211_CMD_PEER_MEASUREMENT_COMPLETE] = "peer_measurement_complete", | |
287 | [NL80211_CMD_NOTIFY_RADAR] = "notify_radar", | |
9990c1e9 MH |
288 | }; |
289 | ||
290 | static char cmdbuf[100]; | |
291 | ||
292 | const char *command_name(enum nl80211_commands cmd) | |
293 | { | |
73780397 | 294 | if (cmd <= NL80211_CMD_MAX && commands[cmd]) |
9990c1e9 MH |
295 | return commands[cmd]; |
296 | sprintf(cmdbuf, "Unknown command (%d)", cmd); | |
297 | return cmdbuf; | |
298 | } | |
299 | ||
58b46da2 | 300 | int ieee80211_channel_to_frequency(int chan, enum nl80211_band band) |
379f8397 | 301 | { |
58b46da2 BR |
302 | /* see 802.11 17.3.8.3.2 and Annex J |
303 | * there are overlapping channel numbers in 5GHz and 2GHz bands */ | |
304 | if (chan <= 0) | |
305 | return 0; /* not supported */ | |
306 | switch (band) { | |
307 | case NL80211_BAND_2GHZ: | |
308 | if (chan == 14) | |
309 | return 2484; | |
310 | else if (chan < 14) | |
311 | return 2407 + chan * 5; | |
312 | break; | |
313 | case NL80211_BAND_5GHZ: | |
314 | if (chan >= 182 && chan <= 196) | |
315 | return 4000 + chan * 5; | |
316 | else | |
317 | return 5000 + chan * 5; | |
318 | break; | |
319 | case NL80211_BAND_60GHZ: | |
320 | if (chan < 5) | |
321 | return 56160 + chan * 2160; | |
322 | break; | |
323 | default: | |
324 | ; | |
325 | } | |
326 | return 0; /* not supported */ | |
379f8397 JB |
327 | } |
328 | ||
329 | int ieee80211_frequency_to_channel(int freq) | |
330 | { | |
58b46da2 | 331 | /* see 802.11-2007 17.3.8.3.2 and Annex J */ |
379f8397 JB |
332 | if (freq == 2484) |
333 | return 14; | |
58b46da2 | 334 | else if (freq < 2484) |
379f8397 | 335 | return (freq - 2407) / 5; |
58b46da2 BR |
336 | else if (freq >= 4910 && freq <= 4980) |
337 | return (freq - 4000) / 5; | |
338 | else if (freq <= 45000) /* DMG band lower limit */ | |
339 | return (freq - 5000) / 5; | |
340 | else if (freq >= 58320 && freq <= 64800) | |
d56e86bc | 341 | return (freq - 56160) / 2160; |
58b46da2 BR |
342 | else |
343 | return 0; | |
379f8397 | 344 | } |
748f8489 JB |
345 | |
346 | void print_ssid_escaped(const uint8_t len, const uint8_t *data) | |
347 | { | |
348 | int i; | |
349 | ||
350 | for (i = 0; i < len; i++) { | |
3f612733 | 351 | if (isprint(data[i]) && data[i] != ' ' && data[i] != '\\') |
748f8489 | 352 | printf("%c", data[i]); |
3f612733 JB |
353 | else if (data[i] == ' ' && |
354 | (i != 0 && i != len -1)) | |
355 | printf(" "); | |
748f8489 JB |
356 | else |
357 | printf("\\x%.2x", data[i]); | |
358 | } | |
359 | } | |
51e9bd80 JB |
360 | |
361 | static int hex2num(char digit) | |
362 | { | |
363 | if (!isxdigit(digit)) | |
364 | return -1; | |
365 | if (isdigit(digit)) | |
366 | return digit - '0'; | |
367 | return tolower(digit) - 'a' + 10; | |
368 | } | |
369 | ||
9ad3cc24 | 370 | static int hex2byte(const char *hex) |
51e9bd80 JB |
371 | { |
372 | int d1, d2; | |
373 | ||
374 | d1 = hex2num(hex[0]); | |
375 | if (d1 < 0) | |
376 | return -1; | |
377 | d2 = hex2num(hex[1]); | |
378 | if (d2 < 0) | |
379 | return -1; | |
380 | return (d1 << 4) | d2; | |
381 | } | |
382 | ||
60b6c638 | 383 | char *hex2bin(const char *hex, char *buf) |
51e9bd80 JB |
384 | { |
385 | char *result = buf; | |
386 | int d; | |
387 | ||
388 | while (hex[0]) { | |
389 | d = hex2byte(hex); | |
390 | if (d < 0) | |
391 | return NULL; | |
392 | buf[0] = d; | |
393 | buf++; | |
394 | hex += 2; | |
395 | } | |
396 | ||
397 | return result; | |
398 | } | |
399 | ||
6c2a0121 EG |
400 | static int parse_akm_suite(const char *cipher_str) |
401 | { | |
402 | ||
403 | if (!strcmp(cipher_str, "PSK")) | |
404 | return 0x000FAC02; | |
405 | if (!strcmp(cipher_str, "FT/PSK")) | |
406 | return 0x000FAC03; | |
407 | if (!strcmp(cipher_str, "PSK/SHA-256")) | |
408 | return 0x000FAC06; | |
409 | return -EINVAL; | |
410 | } | |
411 | ||
412 | static int parse_cipher_suite(const char *cipher_str) | |
413 | { | |
414 | ||
415 | if (!strcmp(cipher_str, "TKIP")) | |
416 | return 0x000FAC02; | |
d7924705 | 417 | if (!strcmp(cipher_str, "CCMP") || !strcmp(cipher_str, "CCMP-128")) |
6c2a0121 | 418 | return 0x000FAC04; |
d7924705 | 419 | if (!strcmp(cipher_str, "GCMP") || !strcmp(cipher_str, "GCMP-128")) |
6c2a0121 EG |
420 | return 0x000FAC08; |
421 | if (!strcmp(cipher_str, "GCMP-256")) | |
422 | return 0x000FAC09; | |
423 | if (!strcmp(cipher_str, "CCMP-256")) | |
424 | return 0x000FAC0A; | |
425 | return -EINVAL; | |
426 | } | |
427 | ||
0e39f109 | 428 | int parse_keys(struct nl_msg *msg, char **argv[], int *argc) |
51e9bd80 JB |
429 | { |
430 | struct nlattr *keys; | |
431 | int i = 0; | |
041581ce | 432 | bool have_default = false; |
0e39f109 | 433 | char *arg = **argv; |
51e9bd80 | 434 | char keybuf[13]; |
6c2a0121 | 435 | int pos = 0; |
51e9bd80 | 436 | |
0e39f109 | 437 | if (!*argc) |
51e9bd80 JB |
438 | return 1; |
439 | ||
6c2a0121 EG |
440 | if (!memcmp(&arg[pos], "psk", 3)) { |
441 | char psk_keybuf[32]; | |
442 | int cipher_suite, akm_suite; | |
443 | ||
0e39f109 | 444 | if (*argc < 4) |
6c2a0121 EG |
445 | goto explain; |
446 | ||
447 | pos+=3; | |
448 | if (arg[pos] != ':') | |
449 | goto explain; | |
450 | pos++; | |
451 | ||
452 | NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, NL80211_WPA_VERSION_2); | |
453 | ||
454 | if (strlen(&arg[pos]) != (sizeof(psk_keybuf) * 2) || !hex2bin(&arg[pos], psk_keybuf)) { | |
455 | printf("Bad PSK\n"); | |
456 | return -EINVAL; | |
457 | } | |
458 | ||
459 | NLA_PUT(msg, NL80211_ATTR_PMK, 32, psk_keybuf); | |
460 | NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, NL80211_AUTHTYPE_OPEN_SYSTEM); | |
461 | ||
0e39f109 EG |
462 | *argv += 1; |
463 | *argc -= 1; | |
464 | arg = **argv; | |
6c2a0121 EG |
465 | |
466 | akm_suite = parse_akm_suite(arg); | |
467 | if (akm_suite < 0) | |
468 | goto explain; | |
469 | ||
470 | NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, akm_suite); | |
471 | ||
0e39f109 EG |
472 | *argv += 1; |
473 | *argc -= 1; | |
474 | arg = **argv; | |
6c2a0121 EG |
475 | |
476 | cipher_suite = parse_cipher_suite(arg); | |
477 | if (cipher_suite < 0) | |
478 | goto explain; | |
479 | ||
480 | NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher_suite); | |
481 | ||
0e39f109 EG |
482 | *argv += 1; |
483 | *argc -= 1; | |
484 | arg = **argv; | |
6c2a0121 EG |
485 | |
486 | cipher_suite = parse_cipher_suite(arg); | |
487 | if (cipher_suite < 0) | |
488 | goto explain; | |
489 | ||
490 | NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher_suite); | |
491 | ||
d4f1ea11 IP |
492 | *argv += 1; |
493 | *argc -= 1; | |
6c2a0121 EG |
494 | return 0; |
495 | } | |
496 | ||
51e9bd80 JB |
497 | NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY); |
498 | ||
499 | keys = nla_nest_start(msg, NL80211_ATTR_KEYS); | |
500 | if (!keys) | |
501 | return -ENOBUFS; | |
502 | ||
503 | do { | |
6c2a0121 | 504 | int keylen; |
51e9bd80 JB |
505 | struct nlattr *key = nla_nest_start(msg, ++i); |
506 | char *keydata; | |
507 | ||
0e39f109 | 508 | arg = **argv; |
6c2a0121 EG |
509 | pos = 0; |
510 | ||
51e9bd80 JB |
511 | if (!key) |
512 | return -ENOBUFS; | |
513 | ||
514 | if (arg[pos] == 'd') { | |
515 | NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT); | |
516 | pos++; | |
517 | if (arg[pos] == ':') | |
518 | pos++; | |
041581ce | 519 | have_default = true; |
51e9bd80 JB |
520 | } |
521 | ||
522 | if (!isdigit(arg[pos])) | |
523 | goto explain; | |
524 | NLA_PUT_U8(msg, NL80211_KEY_IDX, arg[pos++] - '0'); | |
525 | if (arg[pos++] != ':') | |
526 | goto explain; | |
527 | keydata = arg + pos; | |
528 | switch (strlen(keydata)) { | |
529 | case 10: | |
530 | keydata = hex2bin(keydata, keybuf); | |
6ab936f0 | 531 | /* fall through */ |
51e9bd80 JB |
532 | case 5: |
533 | NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC01); | |
534 | keylen = 5; | |
535 | break; | |
536 | case 26: | |
537 | keydata = hex2bin(keydata, keybuf); | |
6ab936f0 | 538 | /* fall through */ |
51e9bd80 JB |
539 | case 13: |
540 | NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC05); | |
541 | keylen = 13; | |
542 | break; | |
543 | default: | |
544 | goto explain; | |
545 | } | |
546 | ||
547 | if (!keydata) | |
548 | goto explain; | |
549 | ||
550 | NLA_PUT(msg, NL80211_KEY_DATA, keylen, keydata); | |
551 | ||
0e39f109 EG |
552 | *argv += 1; |
553 | *argc -= 1; | |
041581ce JB |
554 | |
555 | /* one key should be TX key */ | |
0e39f109 | 556 | if (!have_default && !*argc) |
041581ce JB |
557 | NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT); |
558 | ||
559 | nla_nest_end(msg, key); | |
0e39f109 | 560 | } while (*argc); |
51e9bd80 JB |
561 | |
562 | nla_nest_end(msg, keys); | |
563 | ||
564 | return 0; | |
565 | nla_put_failure: | |
566 | return -ENOBUFS; | |
567 | explain: | |
568 | fprintf(stderr, "key must be [d:]index:data where\n" | |
569 | " 'd:' means default (transmit) key\n" | |
570 | " 'index:' is a single digit (0-3)\n" | |
571 | " 'data' must be 5 or 13 ascii chars\n" | |
572 | " or 10 or 26 hex digits\n" | |
6c2a0121 EG |
573 | "for example: d:2:6162636465 is the same as d:2:abcde\n" |
574 | "or psk:data <AKM Suite> <pairwise CIPHER> <groupwise CIPHER> where\n" | |
575 | " 'data' is the PSK (output of wpa_passphrase and the CIPHER can be CCMP or GCMP\n" | |
576 | "for example: psk:0123456789abcdef PSK CCMP CCMP\n" | |
577 | "The allowed AKM suites are PSK, FT/PSK, PSK/SHA-256\n" | |
578 | "The allowed Cipher suites are TKIP, CCMP, GCMP, GCMP-256, CCMP-256\n"); | |
51e9bd80 JB |
579 | return 2; |
580 | } | |
deb3501c | 581 | |
c37f6c64 | 582 | enum nl80211_chan_width str_to_bw(const char *str) |
997c60fd BB |
583 | { |
584 | static const struct { | |
585 | const char *name; | |
586 | unsigned int val; | |
587 | } bwmap[] = { | |
588 | { .name = "5", .val = NL80211_CHAN_WIDTH_5, }, | |
589 | { .name = "10", .val = NL80211_CHAN_WIDTH_10, }, | |
590 | { .name = "20", .val = NL80211_CHAN_WIDTH_20, }, | |
591 | { .name = "40", .val = NL80211_CHAN_WIDTH_40, }, | |
592 | { .name = "80", .val = NL80211_CHAN_WIDTH_80, }, | |
593 | { .name = "80+80", .val = NL80211_CHAN_WIDTH_80P80, }, | |
594 | { .name = "160", .val = NL80211_CHAN_WIDTH_160, }, | |
595 | }; | |
c37f6c64 JB |
596 | unsigned int i; |
597 | ||
598 | for (i = 0; i < ARRAY_SIZE(bwmap); i++) { | |
599 | if (strcasecmp(bwmap[i].name, str) == 0) | |
600 | return bwmap[i].val; | |
601 | } | |
602 | ||
603 | return NL80211_CHAN_WIDTH_20_NOHT; | |
604 | } | |
605 | ||
606 | static int parse_freqs(struct chandef *chandef, int argc, char **argv, | |
607 | int *parsed) | |
608 | { | |
997c60fd | 609 | uint32_t freq; |
997c60fd | 610 | char *end; |
4871fcf5 | 611 | bool need_cf1 = false, need_cf2 = false; |
997c60fd BB |
612 | |
613 | if (argc < 1) | |
614 | return 0; | |
615 | ||
c37f6c64 | 616 | chandef->width = str_to_bw(argv[0]); |
997c60fd | 617 | |
4871fcf5 JB |
618 | switch (chandef->width) { |
619 | case NL80211_CHAN_WIDTH_20_NOHT: | |
620 | /* First argument was not understood, give up gracefully. */ | |
997c60fd | 621 | return 0; |
4871fcf5 JB |
622 | case NL80211_CHAN_WIDTH_20: |
623 | case NL80211_CHAN_WIDTH_5: | |
624 | case NL80211_CHAN_WIDTH_10: | |
625 | break; | |
626 | case NL80211_CHAN_WIDTH_80P80: | |
627 | need_cf2 = true; | |
628 | /* fall through */ | |
629 | case NL80211_CHAN_WIDTH_40: | |
630 | case NL80211_CHAN_WIDTH_80: | |
631 | case NL80211_CHAN_WIDTH_160: | |
632 | need_cf1 = true; | |
633 | break; | |
634 | } | |
997c60fd | 635 | |
c37f6c64 JB |
636 | *parsed += 1; |
637 | ||
4871fcf5 | 638 | if (!need_cf1) |
997c60fd BB |
639 | return 0; |
640 | ||
4871fcf5 JB |
641 | if (argc < 2) |
642 | return 1; | |
643 | ||
997c60fd BB |
644 | /* center freq 1 */ |
645 | if (!*argv[1]) | |
4871fcf5 | 646 | return 1; |
997c60fd BB |
647 | freq = strtoul(argv[1], &end, 10); |
648 | if (*end) | |
4871fcf5 | 649 | return 1; |
997c60fd BB |
650 | *parsed += 1; |
651 | ||
652 | chandef->center_freq1 = freq; | |
653 | ||
4871fcf5 | 654 | if (!need_cf2) |
997c60fd BB |
655 | return 0; |
656 | ||
4871fcf5 JB |
657 | if (argc < 3) |
658 | return 1; | |
659 | ||
997c60fd BB |
660 | /* center freq 2 */ |
661 | if (!*argv[2]) | |
4871fcf5 | 662 | return 1; |
997c60fd BB |
663 | freq = strtoul(argv[2], &end, 10); |
664 | if (*end) | |
4871fcf5 | 665 | return 1; |
997c60fd BB |
666 | chandef->center_freq2 = freq; |
667 | ||
668 | *parsed += 1; | |
669 | ||
670 | return 0; | |
671 | } | |
672 | ||
673 | ||
674 | /** | |
675 | * parse_freqchan - Parse frequency or channel definition | |
676 | * | |
677 | * @chandef: chandef structure to be filled in | |
678 | * @chan: Boolean whether to parse a channel or frequency based specifier | |
679 | * @argc: Number of arguments | |
680 | * @argv: Array of string arguments | |
681 | * @parsed: Pointer to return the number of used arguments, or NULL to error | |
682 | * out if any argument is left unused. | |
683 | * | |
684 | * The given chandef structure will be filled in from the command line | |
685 | * arguments. argc/argv will be updated so that further arguments from the | |
686 | * command line can be parsed. | |
687 | * | |
4871fcf5 JB |
688 | * Note that despite the fact that the function knows how many center freqs |
689 | * are needed, there's an ambiguity if the next argument after this is an | |
690 | * integer argument, since the valid channel width values are interpreted | |
691 | * as such, rather than a following argument. This can be avoided by the | |
692 | * user by giving "NOHT" instead. | |
997c60fd BB |
693 | * |
694 | * The working specifier if chan is set are: | |
695 | * <channel> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz] | |
696 | * | |
697 | * And if frequency is set: | |
698 | * <freq> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz] | |
699 | * <control freq> [5|10|20|40|80|80+80|160] [<center1_freq> [<center2_freq>]] | |
700 | * | |
701 | * If the mode/channel width is not given the NOHT is assumed. | |
702 | * | |
703 | * Return: Number of used arguments, zero or negative error number otherwise | |
704 | */ | |
705 | int parse_freqchan(struct chandef *chandef, bool chan, int argc, char **argv, | |
706 | int *parsed) | |
707 | { | |
708 | char *end; | |
709 | static const struct chanmode chanmode[] = { | |
710 | { .name = "HT20", | |
711 | .width = NL80211_CHAN_WIDTH_20, | |
712 | .freq1_diff = 0, | |
713 | .chantype = NL80211_CHAN_HT20 }, | |
714 | { .name = "HT40+", | |
715 | .width = NL80211_CHAN_WIDTH_40, | |
716 | .freq1_diff = 10, | |
717 | .chantype = NL80211_CHAN_HT40PLUS }, | |
718 | { .name = "HT40-", | |
719 | .width = NL80211_CHAN_WIDTH_40, | |
720 | .freq1_diff = -10, | |
721 | .chantype = NL80211_CHAN_HT40MINUS }, | |
722 | { .name = "NOHT", | |
723 | .width = NL80211_CHAN_WIDTH_20_NOHT, | |
724 | .freq1_diff = 0, | |
725 | .chantype = NL80211_CHAN_NO_HT }, | |
726 | { .name = "5MHz", | |
727 | .width = NL80211_CHAN_WIDTH_5, | |
728 | .freq1_diff = 0, | |
729 | .chantype = -1 }, | |
730 | { .name = "10MHz", | |
731 | .width = NL80211_CHAN_WIDTH_10, | |
732 | .freq1_diff = 0, | |
733 | .chantype = -1 }, | |
734 | { .name = "80MHz", | |
735 | .width = NL80211_CHAN_WIDTH_80, | |
736 | .freq1_diff = 0, | |
737 | .chantype = -1 }, | |
738 | }; | |
739 | const struct chanmode *chanmode_selected = NULL; | |
740 | unsigned int freq; | |
741 | unsigned int i; | |
742 | int _parsed = 0; | |
743 | int res = 0; | |
744 | ||
745 | if (argc < 1) | |
746 | return 1; | |
747 | ||
748 | if (!argv[0]) | |
749 | goto out; | |
750 | freq = strtoul(argv[0], &end, 10); | |
751 | if (*end) { | |
752 | res = 1; | |
753 | goto out; | |
754 | } | |
755 | ||
756 | _parsed += 1; | |
757 | ||
758 | memset(chandef, 0, sizeof(struct chandef)); | |
759 | ||
760 | if (chan) { | |
761 | enum nl80211_band band; | |
762 | ||
763 | band = freq <= 14 ? NL80211_BAND_2GHZ : NL80211_BAND_5GHZ; | |
764 | freq = ieee80211_channel_to_frequency(freq, band); | |
765 | } | |
766 | chandef->control_freq = freq; | |
767 | /* Assume 20MHz NOHT channel for now. */ | |
768 | chandef->center_freq1 = freq; | |
769 | ||
770 | /* Try to parse HT mode definitions */ | |
771 | if (argc > 1) { | |
772 | for (i = 0; i < ARRAY_SIZE(chanmode); i++) { | |
773 | if (strcasecmp(chanmode[i].name, argv[1]) == 0) { | |
774 | chanmode_selected = &chanmode[i]; | |
775 | _parsed += 1; | |
776 | break; | |
777 | } | |
778 | } | |
779 | } | |
780 | ||
781 | /* channel mode given, use it and return. */ | |
782 | if (chanmode_selected) { | |
783 | chandef->center_freq1 = get_cf1(chanmode_selected, freq); | |
784 | chandef->width = chanmode_selected->width; | |
785 | goto out; | |
786 | } | |
787 | ||
788 | /* This was a only a channel definition, nothing further may follow. */ | |
789 | if (chan) | |
790 | goto out; | |
791 | ||
792 | res = parse_freqs(chandef, argc - 1, argv + 1, &_parsed); | |
793 | ||
794 | out: | |
795 | /* Error out if parsed is NULL. */ | |
796 | if (!parsed && _parsed != argc) | |
797 | return 1; | |
798 | ||
799 | if (parsed) | |
800 | *parsed = _parsed; | |
801 | ||
802 | return res; | |
803 | } | |
804 | ||
805 | int put_chandef(struct nl_msg *msg, struct chandef *chandef) | |
806 | { | |
807 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chandef->control_freq); | |
808 | NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width); | |
809 | ||
810 | switch (chandef->width) { | |
811 | case NL80211_CHAN_WIDTH_20_NOHT: | |
812 | NLA_PUT_U32(msg, | |
813 | NL80211_ATTR_WIPHY_CHANNEL_TYPE, | |
814 | NL80211_CHAN_NO_HT); | |
815 | break; | |
816 | case NL80211_CHAN_WIDTH_20: | |
817 | NLA_PUT_U32(msg, | |
818 | NL80211_ATTR_WIPHY_CHANNEL_TYPE, | |
819 | NL80211_CHAN_HT20); | |
820 | break; | |
821 | case NL80211_CHAN_WIDTH_40: | |
822 | if (chandef->control_freq > chandef->center_freq1) | |
823 | NLA_PUT_U32(msg, | |
824 | NL80211_ATTR_WIPHY_CHANNEL_TYPE, | |
825 | NL80211_CHAN_HT40MINUS); | |
826 | else | |
827 | NLA_PUT_U32(msg, | |
828 | NL80211_ATTR_WIPHY_CHANNEL_TYPE, | |
829 | NL80211_CHAN_HT40PLUS); | |
830 | break; | |
831 | default: | |
832 | break; | |
833 | } | |
834 | ||
835 | if (chandef->center_freq1) | |
836 | NLA_PUT_U32(msg, | |
837 | NL80211_ATTR_CENTER_FREQ1, | |
838 | chandef->center_freq1); | |
839 | ||
840 | if (chandef->center_freq2) | |
841 | NLA_PUT_U32(msg, | |
842 | NL80211_ATTR_CENTER_FREQ2, | |
843 | chandef->center_freq2); | |
844 | ||
845 | return 0; | |
846 | ||
847 | nla_put_failure: | |
848 | return -ENOBUFS; | |
849 | } | |
850 | ||
7ddfb679 | 851 | static void print_mcs_index(const __u8 *mcs) |
deb3501c | 852 | { |
9fea9777 | 853 | int mcs_bit, prev_bit = -2, prev_cont = 0; |
04953e90 JB |
854 | |
855 | for (mcs_bit = 0; mcs_bit <= 76; mcs_bit++) { | |
856 | unsigned int mcs_octet = mcs_bit/8; | |
857 | unsigned int MCS_RATE_BIT = 1 << mcs_bit % 8; | |
858 | bool mcs_rate_idx_set; | |
859 | ||
860 | mcs_rate_idx_set = !!(mcs[mcs_octet] & MCS_RATE_BIT); | |
861 | ||
862 | if (!mcs_rate_idx_set) | |
863 | continue; | |
864 | ||
865 | if (prev_bit != mcs_bit - 1) { | |
866 | if (prev_bit != -2) | |
867 | printf("%d, ", prev_bit); | |
868 | else | |
869 | printf(" "); | |
870 | printf("%d", mcs_bit); | |
871 | prev_cont = 0; | |
872 | } else if (!prev_cont) { | |
873 | printf("-"); | |
874 | prev_cont = 1; | |
875 | } | |
876 | ||
877 | prev_bit = mcs_bit; | |
878 | } | |
deb3501c | 879 | |
04953e90 JB |
880 | if (prev_cont) |
881 | printf("%d", prev_bit); | |
882 | printf("\n"); | |
deb3501c | 883 | } |
0950993f LR |
884 | |
885 | /* | |
886 | * There are only 4 possible values, we just use a case instead of computing it, | |
887 | * but technically this can also be computed through the formula: | |
888 | * | |
889 | * Max AMPDU length = (2 ^ (13 + exponent)) - 1 bytes | |
890 | */ | |
891 | static __u32 compute_ampdu_length(__u8 exponent) | |
892 | { | |
893 | switch (exponent) { | |
894 | case 0: return 8191; /* (2 ^(13 + 0)) -1 */ | |
895 | case 1: return 16383; /* (2 ^(13 + 1)) -1 */ | |
896 | case 2: return 32767; /* (2 ^(13 + 2)) -1 */ | |
897 | case 3: return 65535; /* (2 ^(13 + 3)) -1 */ | |
898 | default: return 0; | |
899 | } | |
900 | } | |
901 | ||
902 | static const char *print_ampdu_space(__u8 space) | |
903 | { | |
904 | switch (space) { | |
905 | case 0: return "No restriction"; | |
906 | case 1: return "1/4 usec"; | |
907 | case 2: return "1/2 usec"; | |
908 | case 3: return "1 usec"; | |
909 | case 4: return "2 usec"; | |
910 | case 5: return "4 usec"; | |
911 | case 6: return "8 usec"; | |
912 | case 7: return "16 usec"; | |
913 | default: | |
7ae93cd5 | 914 | return "BUG (spacing more than 3 bits!)"; |
0950993f LR |
915 | } |
916 | } | |
917 | ||
918 | void print_ampdu_length(__u8 exponent) | |
919 | { | |
04953e90 | 920 | __u32 max_ampdu_length; |
0950993f LR |
921 | |
922 | max_ampdu_length = compute_ampdu_length(exponent); | |
923 | ||
924 | if (max_ampdu_length) { | |
925 | printf("\t\tMaximum RX AMPDU length %d bytes (exponent: 0x0%02x)\n", | |
926 | max_ampdu_length, exponent); | |
3f362f8b | 927 | } else { |
0950993f LR |
928 | printf("\t\tMaximum RX AMPDU length: unrecognized bytes " |
929 | "(exponent: %d)\n", exponent); | |
930 | } | |
931 | } | |
932 | ||
933 | void print_ampdu_spacing(__u8 spacing) | |
934 | { | |
3f362f8b NB |
935 | printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n", |
936 | print_ampdu_space(spacing), spacing); | |
0950993f | 937 | } |
357c1a5d LR |
938 | |
939 | void print_ht_capability(__u16 cap) | |
940 | { | |
941 | #define PRINT_HT_CAP(_cond, _str) \ | |
942 | do { \ | |
943 | if (_cond) \ | |
944 | printf("\t\t\t" _str "\n"); \ | |
945 | } while (0) | |
946 | ||
947 | printf("\t\tCapabilities: 0x%02x\n", cap); | |
948 | ||
028c0de5 | 949 | PRINT_HT_CAP((cap & BIT(0)), "RX LDPC"); |
357c1a5d LR |
950 | PRINT_HT_CAP((cap & BIT(1)), "HT20/HT40"); |
951 | PRINT_HT_CAP(!(cap & BIT(1)), "HT20"); | |
952 | ||
953 | PRINT_HT_CAP(((cap >> 2) & 0x3) == 0, "Static SM Power Save"); | |
954 | PRINT_HT_CAP(((cap >> 2) & 0x3) == 1, "Dynamic SM Power Save"); | |
955 | PRINT_HT_CAP(((cap >> 2) & 0x3) == 3, "SM Power Save disabled"); | |
956 | ||
957 | PRINT_HT_CAP((cap & BIT(4)), "RX Greenfield"); | |
958 | PRINT_HT_CAP((cap & BIT(5)), "RX HT20 SGI"); | |
959 | PRINT_HT_CAP((cap & BIT(6)), "RX HT40 SGI"); | |
960 | PRINT_HT_CAP((cap & BIT(7)), "TX STBC"); | |
961 | ||
962 | PRINT_HT_CAP(((cap >> 8) & 0x3) == 0, "No RX STBC"); | |
963 | PRINT_HT_CAP(((cap >> 8) & 0x3) == 1, "RX STBC 1-stream"); | |
964 | PRINT_HT_CAP(((cap >> 8) & 0x3) == 2, "RX STBC 2-streams"); | |
965 | PRINT_HT_CAP(((cap >> 8) & 0x3) == 3, "RX STBC 3-streams"); | |
966 | ||
967 | PRINT_HT_CAP((cap & BIT(10)), "HT Delayed Block Ack"); | |
968 | ||
c79c7464 CL |
969 | PRINT_HT_CAP(!(cap & BIT(11)), "Max AMSDU length: 3839 bytes"); |
970 | PRINT_HT_CAP((cap & BIT(11)), "Max AMSDU length: 7935 bytes"); | |
357c1a5d LR |
971 | |
972 | /* | |
973 | * For beacons and probe response this would mean the BSS | |
974 | * does or does not allow the usage of DSSS/CCK HT40. | |
975 | * Otherwise it means the STA does or does not use | |
976 | * DSSS/CCK HT40. | |
977 | */ | |
978 | PRINT_HT_CAP((cap & BIT(12)), "DSSS/CCK HT40"); | |
979 | PRINT_HT_CAP(!(cap & BIT(12)), "No DSSS/CCK HT40"); | |
980 | ||
981 | /* BIT(13) is reserved */ | |
982 | ||
983 | PRINT_HT_CAP((cap & BIT(14)), "40 MHz Intolerant"); | |
984 | ||
985 | PRINT_HT_CAP((cap & BIT(15)), "L-SIG TXOP protection"); | |
986 | #undef PRINT_HT_CAP | |
987 | } | |
7ddfb679 JB |
988 | |
989 | void print_ht_mcs(const __u8 *mcs) | |
990 | { | |
991 | /* As defined in 7.3.2.57.4 Supported MCS Set field */ | |
992 | unsigned int tx_max_num_spatial_streams, max_rx_supp_data_rate; | |
993 | bool tx_mcs_set_defined, tx_mcs_set_equal, tx_unequal_modulation; | |
994 | ||
5ba6a62b | 995 | max_rx_supp_data_rate = (mcs[10] | ((mcs[11] & 0x3) << 8)); |
7ddfb679 JB |
996 | tx_mcs_set_defined = !!(mcs[12] & (1 << 0)); |
997 | tx_mcs_set_equal = !(mcs[12] & (1 << 1)); | |
998 | tx_max_num_spatial_streams = ((mcs[12] >> 2) & 3) + 1; | |
999 | tx_unequal_modulation = !!(mcs[12] & (1 << 4)); | |
1000 | ||
1001 | if (max_rx_supp_data_rate) | |
1002 | printf("\t\tHT Max RX data rate: %d Mbps\n", max_rx_supp_data_rate); | |
1003 | /* XXX: else see 9.6.0e.5.3 how to get this I think */ | |
1004 | ||
1005 | if (tx_mcs_set_defined) { | |
1006 | if (tx_mcs_set_equal) { | |
2a79feb0 | 1007 | printf("\t\tHT TX/RX MCS rate indexes supported:"); |
7ddfb679 JB |
1008 | print_mcs_index(mcs); |
1009 | } else { | |
1010 | printf("\t\tHT RX MCS rate indexes supported:"); | |
1011 | print_mcs_index(mcs); | |
1012 | ||
1013 | if (tx_unequal_modulation) | |
1014 | printf("\t\tTX unequal modulation supported\n"); | |
1015 | else | |
1016 | printf("\t\tTX unequal modulation not supported\n"); | |
1017 | ||
1018 | printf("\t\tHT TX Max spatial streams: %d\n", | |
1019 | tx_max_num_spatial_streams); | |
1020 | ||
1021 | printf("\t\tHT TX MCS rate indexes supported may differ\n"); | |
1022 | } | |
1023 | } else { | |
1024 | printf("\t\tHT RX MCS rate indexes supported:"); | |
1025 | print_mcs_index(mcs); | |
089bb35d | 1026 | printf("\t\tHT TX MCS rate indexes are undefined\n"); |
7ddfb679 JB |
1027 | } |
1028 | } | |
54eb1613 JB |
1029 | |
1030 | void print_vht_info(__u32 capa, const __u8 *mcs) | |
1031 | { | |
1032 | __u16 tmp; | |
1033 | int i; | |
1034 | ||
1035 | printf("\t\tVHT Capabilities (0x%.8x):\n", capa); | |
1036 | ||
1037 | #define PRINT_VHT_CAPA(_bit, _str) \ | |
1038 | do { \ | |
1039 | if (capa & BIT(_bit)) \ | |
1040 | printf("\t\t\t" _str "\n"); \ | |
1041 | } while (0) | |
1042 | ||
1043 | printf("\t\t\tMax MPDU length: "); | |
1044 | switch (capa & 3) { | |
1045 | case 0: printf("3895\n"); break; | |
1046 | case 1: printf("7991\n"); break; | |
1047 | case 2: printf("11454\n"); break; | |
1048 | case 3: printf("(reserved)\n"); | |
1049 | } | |
1050 | printf("\t\t\tSupported Channel Width: "); | |
1051 | switch ((capa >> 2) & 3) { | |
1052 | case 0: printf("neither 160 nor 80+80\n"); break; | |
1053 | case 1: printf("160 MHz\n"); break; | |
1054 | case 2: printf("160 MHz, 80+80 MHz\n"); break; | |
1055 | case 3: printf("(reserved)\n"); | |
1056 | } | |
1057 | PRINT_VHT_CAPA(4, "RX LDPC"); | |
1058 | PRINT_VHT_CAPA(5, "short GI (80 MHz)"); | |
1059 | PRINT_VHT_CAPA(6, "short GI (160/80+80 MHz)"); | |
1060 | PRINT_VHT_CAPA(7, "TX STBC"); | |
1061 | /* RX STBC */ | |
1062 | PRINT_VHT_CAPA(11, "SU Beamformer"); | |
1063 | PRINT_VHT_CAPA(12, "SU Beamformee"); | |
1064 | /* compressed steering */ | |
1065 | /* # of sounding dimensions */ | |
1066 | PRINT_VHT_CAPA(19, "MU Beamformer"); | |
1067 | PRINT_VHT_CAPA(20, "MU Beamformee"); | |
1068 | PRINT_VHT_CAPA(21, "VHT TXOP PS"); | |
1069 | PRINT_VHT_CAPA(22, "+HTC-VHT"); | |
1070 | /* max A-MPDU */ | |
1071 | /* VHT link adaptation */ | |
75271051 MB |
1072 | PRINT_VHT_CAPA(28, "RX antenna pattern consistency"); |
1073 | PRINT_VHT_CAPA(29, "TX antenna pattern consistency"); | |
54eb1613 JB |
1074 | |
1075 | printf("\t\tVHT RX MCS set:\n"); | |
1076 | tmp = mcs[0] | (mcs[1] << 8); | |
1077 | for (i = 1; i <= 8; i++) { | |
1078 | printf("\t\t\t%d streams: ", i); | |
1079 | switch ((tmp >> ((i-1)*2) ) & 3) { | |
1080 | case 0: printf("MCS 0-7\n"); break; | |
1081 | case 1: printf("MCS 0-8\n"); break; | |
1082 | case 2: printf("MCS 0-9\n"); break; | |
1083 | case 3: printf("not supported\n"); break; | |
1084 | } | |
1085 | } | |
1086 | tmp = mcs[2] | (mcs[3] << 8); | |
1087 | printf("\t\tVHT RX highest supported: %d Mbps\n", tmp & 0x1fff); | |
1088 | ||
1089 | printf("\t\tVHT TX MCS set:\n"); | |
1090 | tmp = mcs[4] | (mcs[5] << 8); | |
1091 | for (i = 1; i <= 8; i++) { | |
1092 | printf("\t\t\t%d streams: ", i); | |
1093 | switch ((tmp >> ((i-1)*2) ) & 3) { | |
1094 | case 0: printf("MCS 0-7\n"); break; | |
1095 | case 1: printf("MCS 0-8\n"); break; | |
1096 | case 2: printf("MCS 0-9\n"); break; | |
1097 | case 3: printf("not supported\n"); break; | |
1098 | } | |
1099 | } | |
1100 | tmp = mcs[6] | (mcs[7] << 8); | |
1101 | printf("\t\tVHT TX highest supported: %d Mbps\n", tmp & 0x1fff); | |
1102 | } | |
492354de JD |
1103 | |
1104 | void iw_hexdump(const char *prefix, const __u8 *buf, size_t size) | |
1105 | { | |
0ee571d5 | 1106 | size_t i; |
492354de JD |
1107 | |
1108 | printf("%s: ", prefix); | |
1109 | for (i = 0; i < size; i++) { | |
1110 | if (i && i % 16 == 0) | |
1111 | printf("\n%s: ", prefix); | |
1112 | printf("%02x ", buf[i]); | |
1113 | } | |
1114 | printf("\n\n"); | |
1115 | } | |
c1b2b633 SE |
1116 | |
1117 | int get_cf1(const struct chanmode *chanmode, unsigned long freq) | |
1118 | { | |
1119 | unsigned int cf1 = freq, j; | |
1120 | unsigned int vht80[] = { 5180, 5260, 5500, 5580, 5660, 5745 }; | |
1121 | ||
1122 | switch (chanmode->width) { | |
1123 | case NL80211_CHAN_WIDTH_80: | |
1124 | /* setup center_freq1 */ | |
1125 | for (j = 0; j < ARRAY_SIZE(vht80); j++) { | |
1126 | if (freq >= vht80[j] && freq < vht80[j] + 80) | |
1127 | break; | |
1128 | } | |
1129 | ||
1130 | if (j == ARRAY_SIZE(vht80)) | |
1131 | break; | |
1132 | ||
1133 | cf1 = vht80[j] + 30; | |
1134 | break; | |
1135 | default: | |
1136 | cf1 = freq + chanmode->freq1_diff; | |
1137 | break; | |
1138 | } | |
1139 | ||
1140 | return cf1; | |
1141 | } | |
3c0117c1 JB |
1142 | |
1143 | int parse_random_mac_addr(struct nl_msg *msg, char *addrs) | |
1144 | { | |
1145 | char *a_addr, *a_mask, *sep; | |
1146 | unsigned char addr[ETH_ALEN], mask[ETH_ALEN]; | |
1147 | ||
1148 | if (!*addrs) { | |
1149 | /* randomise all but the multicast bit */ | |
1150 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, | |
1151 | "\x00\x00\x00\x00\x00\x00"); | |
1152 | NLA_PUT(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN, | |
1153 | "\x01\x00\x00\x00\x00\x00"); | |
1154 | return 0; | |
1155 | } | |
1156 | ||
1157 | if (*addrs != '=') | |
1158 | return 1; | |
1159 | ||
1160 | addrs++; | |
1161 | sep = strchr(addrs, '/'); | |
1162 | a_addr = addrs; | |
1163 | ||
1164 | if (!sep) | |
1165 | return 1; | |
1166 | ||
1167 | *sep = 0; | |
1168 | a_mask = sep + 1; | |
1169 | if (mac_addr_a2n(addr, a_addr) || mac_addr_a2n(mask, a_mask)) | |
1170 | return 1; | |
1171 | ||
1172 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); | |
1173 | NLA_PUT(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN, mask); | |
1174 | ||
1175 | return 0; | |
1176 | nla_put_failure: | |
1177 | return -ENOBUFS; | |
1178 | } |