2 #include <netlink/attr.h>
8 void mac_addr_n2a(char *mac_addr
, unsigned char *arg
)
13 for (i
= 0; i
< ETH_ALEN
; i
++) {
15 sprintf(mac_addr
+l
, "%02x", arg
[i
]);
18 sprintf(mac_addr
+l
, ":%02x", arg
[i
]);
24 int mac_addr_a2n(unsigned char *mac_addr
, char *arg
)
28 for (i
= 0; i
< ETH_ALEN
; i
++) {
30 char *cp
= strchr(arg
, ':');
35 if (sscanf(arg
, "%x", &temp
) != 1)
37 if (temp
< 0 || temp
> 255)
51 unsigned char *parse_hex(char *hex
, size_t *outlen
)
53 size_t len
= strlen(hex
);
54 unsigned char *result
= calloc(len
/2 + 2, 1);
64 char *cp
= strchr(hex
, ':');
69 if (sscanf(hex
, "%x", &temp
) != 1)
71 if (temp
< 0 || temp
> 255)
88 static const char *ifmodes
[NL80211_IFTYPE_MAX
+ 1] = {
101 static char modebuf
[100];
103 const char *iftype_name(enum nl80211_iftype iftype
)
105 if (iftype
<= NL80211_IFTYPE_MAX
)
106 return ifmodes
[iftype
];
107 sprintf(modebuf
, "Unknown mode (%d)", iftype
);
111 static const char *commands
[NL80211_CMD_MAX
+ 1] = {
112 [NL80211_CMD_GET_WIPHY
] = "get_wiphy",
113 [NL80211_CMD_SET_WIPHY
] = "set_wiphy",
114 [NL80211_CMD_NEW_WIPHY
] = "new_wiphy",
115 [NL80211_CMD_DEL_WIPHY
] = "del_wiphy",
116 [NL80211_CMD_GET_INTERFACE
] = "get_interface",
117 [NL80211_CMD_SET_INTERFACE
] = "set_interface",
118 [NL80211_CMD_NEW_INTERFACE
] = "new_interface",
119 [NL80211_CMD_DEL_INTERFACE
] = "del_interface",
120 [NL80211_CMD_GET_KEY
] = "get_key",
121 [NL80211_CMD_SET_KEY
] = "set_key",
122 [NL80211_CMD_NEW_KEY
] = "new_key",
123 [NL80211_CMD_DEL_KEY
] = "del_key",
124 [NL80211_CMD_GET_BEACON
] = "get_beacon",
125 [NL80211_CMD_SET_BEACON
] = "set_beacon",
126 [NL80211_CMD_NEW_BEACON
] = "new_beacon",
127 [NL80211_CMD_DEL_BEACON
] = "del_beacon",
128 [NL80211_CMD_GET_STATION
] = "get_station",
129 [NL80211_CMD_SET_STATION
] = "set_station",
130 [NL80211_CMD_NEW_STATION
] = "new_station",
131 [NL80211_CMD_DEL_STATION
] = "del_station",
132 [NL80211_CMD_GET_MPATH
] = "get_mpath",
133 [NL80211_CMD_SET_MPATH
] = "set_mpath",
134 [NL80211_CMD_NEW_MPATH
] = "new_mpath",
135 [NL80211_CMD_DEL_MPATH
] = "del_mpath",
136 [NL80211_CMD_SET_BSS
] = "set_bss",
137 [NL80211_CMD_SET_REG
] = "set_reg",
138 [NL80211_CMD_REQ_SET_REG
] = "reg_set_reg",
139 [NL80211_CMD_GET_MESH_PARAMS
] = "get_mesh_params",
140 [NL80211_CMD_SET_MESH_PARAMS
] = "set_mesh_params",
141 [NL80211_CMD_SET_MGMT_EXTRA_IE
] = "set_mgmt_extra_ie",
142 [NL80211_CMD_GET_REG
] = "get_reg",
143 [NL80211_CMD_GET_SCAN
] = "get_scan",
144 [NL80211_CMD_TRIGGER_SCAN
] = "trigger_scan",
145 [NL80211_CMD_NEW_SCAN_RESULTS
] = "new_scan_results",
146 [NL80211_CMD_SCAN_ABORTED
] = "scan_aborted",
147 [NL80211_CMD_REG_CHANGE
] = "reg_change",
148 [NL80211_CMD_AUTHENTICATE
] = "authenticate",
149 [NL80211_CMD_ASSOCIATE
] = "associate",
150 [NL80211_CMD_DEAUTHENTICATE
] = "deauthenticate",
151 [NL80211_CMD_DISASSOCIATE
] = "disassociate",
152 [NL80211_CMD_MICHAEL_MIC_FAILURE
] = "michael_mic_failure",
153 [NL80211_CMD_REG_BEACON_HINT
] = "reg_beacon_hint",
154 [NL80211_CMD_JOIN_IBSS
] = "join_ibss",
155 [NL80211_CMD_LEAVE_IBSS
] = "leave_ibss",
156 [NL80211_CMD_TESTMODE
] = "testmode",
157 [NL80211_CMD_CONNECT
] = "connect",
158 [NL80211_CMD_ROAM
] = "roam",
159 [NL80211_CMD_DISCONNECT
] = "disconnect",
160 [NL80211_CMD_SET_WIPHY_NETNS
] = "set_wiphy_netns",
161 [NL80211_CMD_GET_SURVEY
] = "get_survey",
162 [NL80211_CMD_SET_PMKSA
] = "set_pmksa",
163 [NL80211_CMD_DEL_PMKSA
] = "del_pmksa",
164 [NL80211_CMD_FLUSH_PMKSA
] = "flush_pmksa",
165 [NL80211_CMD_REMAIN_ON_CHANNEL
] = "remain_on_channel",
166 [NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL
] = "cancel_remain_on_channel",
167 [NL80211_CMD_SET_TX_BITRATE_MASK
] = "set_tx_bitrate_mask",
168 [NL80211_CMD_REGISTER_ACTION
] = "register_action",
169 [NL80211_CMD_ACTION
] = "action",
170 [NL80211_CMD_SET_CHANNEL
] = "set_channel",
173 static char cmdbuf
[100];
175 const char *command_name(enum nl80211_commands cmd
)
177 if (cmd
<= NL80211_CMD_MAX
&& commands
[cmd
])
178 return commands
[cmd
];
179 sprintf(cmdbuf
, "Unknown command (%d)", cmd
);
183 int ieee80211_channel_to_frequency(int chan
)
186 return 2407 + chan
* 5;
191 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
192 return (chan
+ 1000) * 5;
195 int ieee80211_frequency_to_channel(int freq
)
201 return (freq
- 2407) / 5;
203 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
204 return freq
/5 - 1000;
207 void print_ssid_escaped(const uint8_t len
, const uint8_t *data
)
211 for (i
= 0; i
< len
; i
++) {
212 if (isprint(data
[i
]))
213 printf("%c", data
[i
]);
215 printf("\\x%.2x", data
[i
]);
219 static int hex2num(char digit
)
221 if (!isxdigit(digit
))
225 return tolower(digit
) - 'a' + 10;
228 static int hex2byte(char *hex
)
232 d1
= hex2num(hex
[0]);
235 d2
= hex2num(hex
[1]);
238 return (d1
<< 4) | d2
;
241 static char *hex2bin(char *hex
, char *buf
)
258 int parse_keys(struct nl_msg
*msg
, char **argv
, int argc
)
262 bool have_default
= false;
268 NLA_PUT_FLAG(msg
, NL80211_ATTR_PRIVACY
);
270 keys
= nla_nest_start(msg
, NL80211_ATTR_KEYS
);
277 struct nlattr
*key
= nla_nest_start(msg
, ++i
);
283 if (arg
[pos
] == 'd') {
284 NLA_PUT_FLAG(msg
, NL80211_KEY_DEFAULT
);
291 if (!isdigit(arg
[pos
]))
293 NLA_PUT_U8(msg
, NL80211_KEY_IDX
, arg
[pos
++] - '0');
294 if (arg
[pos
++] != ':')
297 switch (strlen(keydata
)) {
299 keydata
= hex2bin(keydata
, keybuf
);
301 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
, 0x000FAC01);
305 keydata
= hex2bin(keydata
, keybuf
);
307 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
, 0x000FAC05);
317 NLA_PUT(msg
, NL80211_KEY_DATA
, keylen
, keydata
);
322 /* one key should be TX key */
323 if (!have_default
&& !argc
)
324 NLA_PUT_FLAG(msg
, NL80211_KEY_DEFAULT
);
326 nla_nest_end(msg
, key
);
329 nla_nest_end(msg
, keys
);
335 fprintf(stderr
, "key must be [d:]index:data where\n"
336 " 'd:' means default (transmit) key\n"
337 " 'index:' is a single digit (0-3)\n"
338 " 'data' must be 5 or 13 ascii chars\n"
339 " or 10 or 26 hex digits\n"
340 "for example: d:2:6162636465 is the same as d:2:abcde\n");
344 static void print_mcs_index(const __u8
*mcs
)
346 unsigned int mcs_bit
, prev_bit
= -2, prev_cont
= 0;
348 for (mcs_bit
= 0; mcs_bit
<= 76; mcs_bit
++) {
349 unsigned int mcs_octet
= mcs_bit
/8;
350 unsigned int MCS_RATE_BIT
= 1 << mcs_bit
% 8;
351 bool mcs_rate_idx_set
;
353 mcs_rate_idx_set
= !!(mcs
[mcs_octet
] & MCS_RATE_BIT
);
355 if (!mcs_rate_idx_set
)
358 if (prev_bit
!= mcs_bit
- 1) {
360 printf("%d, ", prev_bit
);
363 printf("%d", mcs_bit
);
365 } else if (!prev_cont
) {
374 printf("%d", prev_bit
);
379 * There are only 4 possible values, we just use a case instead of computing it,
380 * but technically this can also be computed through the formula:
382 * Max AMPDU length = (2 ^ (13 + exponent)) - 1 bytes
384 static __u32
compute_ampdu_length(__u8 exponent
)
387 case 0: return 8191; /* (2 ^(13 + 0)) -1 */
388 case 1: return 16383; /* (2 ^(13 + 1)) -1 */
389 case 2: return 32767; /* (2 ^(13 + 2)) -1 */
390 case 3: return 65535; /* (2 ^(13 + 3)) -1 */
395 static const char *print_ampdu_space(__u8 space
)
398 case 0: return "No restriction";
399 case 1: return "1/4 usec";
400 case 2: return "1/2 usec";
401 case 3: return "1 usec";
402 case 4: return "2 usec";
403 case 5: return "4 usec";
404 case 6: return "8 usec";
405 case 7: return "16 usec";
407 return "BUG (spacing more than 3 bits!)";
411 void print_ampdu_length(__u8 exponent
)
413 __u32 max_ampdu_length
;
415 max_ampdu_length
= compute_ampdu_length(exponent
);
417 if (max_ampdu_length
) {
418 printf("\t\tMaximum RX AMPDU length %d bytes (exponent: 0x0%02x)\n",
419 max_ampdu_length
, exponent
);
421 printf("\t\tMaximum RX AMPDU length: unrecognized bytes "
422 "(exponent: %d)\n", exponent
);
426 void print_ampdu_spacing(__u8 spacing
)
428 printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n",
429 print_ampdu_space(spacing
), spacing
);
432 void print_ht_capability(__u16 cap
)
434 #define PRINT_HT_CAP(_cond, _str) \
437 printf("\t\t\t" _str "\n"); \
440 printf("\t\tCapabilities: 0x%02x\n", cap
);
442 PRINT_HT_CAP((cap
& BIT(0)), "RX LDPC");
443 PRINT_HT_CAP((cap
& BIT(1)), "HT20/HT40");
444 PRINT_HT_CAP(!(cap
& BIT(1)), "HT20");
446 PRINT_HT_CAP(((cap
>> 2) & 0x3) == 0, "Static SM Power Save");
447 PRINT_HT_CAP(((cap
>> 2) & 0x3) == 1, "Dynamic SM Power Save");
448 PRINT_HT_CAP(((cap
>> 2) & 0x3) == 3, "SM Power Save disabled");
450 PRINT_HT_CAP((cap
& BIT(4)), "RX Greenfield");
451 PRINT_HT_CAP((cap
& BIT(5)), "RX HT20 SGI");
452 PRINT_HT_CAP((cap
& BIT(6)), "RX HT40 SGI");
453 PRINT_HT_CAP((cap
& BIT(7)), "TX STBC");
455 PRINT_HT_CAP(((cap
>> 8) & 0x3) == 0, "No RX STBC");
456 PRINT_HT_CAP(((cap
>> 8) & 0x3) == 1, "RX STBC 1-stream");
457 PRINT_HT_CAP(((cap
>> 8) & 0x3) == 2, "RX STBC 2-streams");
458 PRINT_HT_CAP(((cap
>> 8) & 0x3) == 3, "RX STBC 3-streams");
460 PRINT_HT_CAP((cap
& BIT(10)), "HT Delayed Block Ack");
462 PRINT_HT_CAP(!(cap
& BIT(11)), "Max AMSDU length: 3839 bytes");
463 PRINT_HT_CAP((cap
& BIT(11)), "Max AMSDU length: 7935 bytes");
466 * For beacons and probe response this would mean the BSS
467 * does or does not allow the usage of DSSS/CCK HT40.
468 * Otherwise it means the STA does or does not use
471 PRINT_HT_CAP((cap
& BIT(12)), "DSSS/CCK HT40");
472 PRINT_HT_CAP(!(cap
& BIT(12)), "No DSSS/CCK HT40");
474 /* BIT(13) is reserved */
476 PRINT_HT_CAP((cap
& BIT(14)), "40 MHz Intolerant");
478 PRINT_HT_CAP((cap
& BIT(15)), "L-SIG TXOP protection");
482 void print_ht_mcs(const __u8
*mcs
)
484 /* As defined in 7.3.2.57.4 Supported MCS Set field */
485 unsigned int tx_max_num_spatial_streams
, max_rx_supp_data_rate
;
486 bool tx_mcs_set_defined
, tx_mcs_set_equal
, tx_unequal_modulation
;
488 max_rx_supp_data_rate
= ((mcs
[10] >> 8) & ((mcs
[11] & 0x3) << 8));
489 tx_mcs_set_defined
= !!(mcs
[12] & (1 << 0));
490 tx_mcs_set_equal
= !(mcs
[12] & (1 << 1));
491 tx_max_num_spatial_streams
= ((mcs
[12] >> 2) & 3) + 1;
492 tx_unequal_modulation
= !!(mcs
[12] & (1 << 4));
494 if (max_rx_supp_data_rate
)
495 printf("\t\tHT Max RX data rate: %d Mbps\n", max_rx_supp_data_rate
);
496 /* XXX: else see 9.6.0e.5.3 how to get this I think */
498 if (tx_mcs_set_defined
) {
499 if (tx_mcs_set_equal
) {
500 printf("\t\tHT TX/RX MCS rate indexes supported:");
501 print_mcs_index(mcs
);
503 printf("\t\tHT RX MCS rate indexes supported:");
504 print_mcs_index(mcs
);
506 if (tx_unequal_modulation
)
507 printf("\t\tTX unequal modulation supported\n");
509 printf("\t\tTX unequal modulation not supported\n");
511 printf("\t\tHT TX Max spatial streams: %d\n",
512 tx_max_num_spatial_streams
);
514 printf("\t\tHT TX MCS rate indexes supported may differ\n");
517 printf("\t\tHT RX MCS rate indexes supported:");
518 print_mcs_index(mcs
);
519 printf("\t\tHT TX MCS rate indexes are undefined\n");