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] = {
99 static char modebuf
[100];
101 const char *iftype_name(enum nl80211_iftype iftype
)
103 if (iftype
<= NL80211_IFTYPE_MAX
)
104 return ifmodes
[iftype
];
105 sprintf(modebuf
, "Unknown mode (%d)", iftype
);
109 static const char *commands
[NL80211_CMD_MAX
+ 1] = {
110 [NL80211_CMD_GET_WIPHY
] = "get_wiphy",
111 [NL80211_CMD_SET_WIPHY
] = "set_wiphy",
112 [NL80211_CMD_NEW_WIPHY
] = "new_wiphy",
113 [NL80211_CMD_DEL_WIPHY
] = "del_wiphy",
114 [NL80211_CMD_GET_INTERFACE
] = "get_interface",
115 [NL80211_CMD_SET_INTERFACE
] = "set_interface",
116 [NL80211_CMD_NEW_INTERFACE
] = "new_interface",
117 [NL80211_CMD_DEL_INTERFACE
] = "del_interface",
118 [NL80211_CMD_GET_KEY
] = "get_key",
119 [NL80211_CMD_SET_KEY
] = "set_key",
120 [NL80211_CMD_NEW_KEY
] = "new_key",
121 [NL80211_CMD_DEL_KEY
] = "del_key",
122 [NL80211_CMD_GET_BEACON
] = "get_beacon",
123 [NL80211_CMD_SET_BEACON
] = "set_beacon",
124 [NL80211_CMD_NEW_BEACON
] = "new_beacon",
125 [NL80211_CMD_DEL_BEACON
] = "del_beacon",
126 [NL80211_CMD_GET_STATION
] = "get_station",
127 [NL80211_CMD_SET_STATION
] = "set_station",
128 [NL80211_CMD_NEW_STATION
] = "new_station",
129 [NL80211_CMD_DEL_STATION
] = "del_station",
130 [NL80211_CMD_GET_MPATH
] = "get_mpath",
131 [NL80211_CMD_SET_MPATH
] = "set_mpath",
132 [NL80211_CMD_NEW_MPATH
] = "new_mpath",
133 [NL80211_CMD_DEL_MPATH
] = "del_mpath",
134 [NL80211_CMD_SET_BSS
] = "set_bss",
135 [NL80211_CMD_SET_REG
] = "set_reg",
136 [NL80211_CMD_REQ_SET_REG
] = "reg_set_reg",
137 [NL80211_CMD_GET_MESH_PARAMS
] = "get_mesh_params",
138 [NL80211_CMD_SET_MESH_PARAMS
] = "set_mesh_params",
139 [NL80211_CMD_SET_MGMT_EXTRA_IE
] = "set_mgmt_extra_ie",
140 [NL80211_CMD_GET_REG
] = "get_reg",
141 [NL80211_CMD_GET_SCAN
] = "get_scan",
142 [NL80211_CMD_TRIGGER_SCAN
] = "trigger_scan",
143 [NL80211_CMD_NEW_SCAN_RESULTS
] = "new_scan_results",
144 [NL80211_CMD_SCAN_ABORTED
] = "scan_aborted",
145 [NL80211_CMD_REG_CHANGE
] = "reg_change",
146 [NL80211_CMD_AUTHENTICATE
] = "authenticate",
147 [NL80211_CMD_ASSOCIATE
] = "associate",
148 [NL80211_CMD_DEAUTHENTICATE
] = "deauthenticate",
149 [NL80211_CMD_DISASSOCIATE
] = "disassociate",
150 [NL80211_CMD_MICHAEL_MIC_FAILURE
] = "michael_mic_failure",
151 [NL80211_CMD_REG_BEACON_HINT
] = "reg_beacon_hint",
152 [NL80211_CMD_JOIN_IBSS
] = "join_ibss",
153 [NL80211_CMD_LEAVE_IBSS
] = "leave_ibss",
154 [NL80211_CMD_TESTMODE
] = "testmode",
155 [NL80211_CMD_CONNECT
] = "connect",
156 [NL80211_CMD_ROAM
] = "roam",
157 [NL80211_CMD_DISCONNECT
] = "disconnect",
158 [NL80211_CMD_SET_WIPHY_NETNS
] = "set_wiphy_netns",
159 [NL80211_CMD_GET_SURVEY
] = "get_survey",
160 [NL80211_CMD_SET_PMKSA
] = "set_pmksa",
161 [NL80211_CMD_DEL_PMKSA
] = "del_pmksa",
162 [NL80211_CMD_FLUSH_PMKSA
] = "flush_pmksa",
163 [NL80211_CMD_REMAIN_ON_CHANNEL
] = "remain_on_channel",
164 [NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL
] = "cancel_remain_on_channel",
165 [NL80211_CMD_SET_TX_BITRATE_MASK
] = "set_tx_bitrate_mask",
166 [NL80211_CMD_REGISTER_ACTION
] = "register_action",
167 [NL80211_CMD_ACTION
] = "action",
170 static char cmdbuf
[100];
172 const char *command_name(enum nl80211_commands cmd
)
174 if (cmd
<= NL80211_CMD_MAX
&& commands
[cmd
])
175 return commands
[cmd
];
176 sprintf(cmdbuf
, "Unknown command (%d)", cmd
);
180 int ieee80211_channel_to_frequency(int chan
)
183 return 2407 + chan
* 5;
188 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
189 return (chan
+ 1000) * 5;
192 int ieee80211_frequency_to_channel(int freq
)
198 return (freq
- 2407) / 5;
200 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
201 return freq
/5 - 1000;
204 void print_ssid_escaped(const uint8_t len
, const uint8_t *data
)
208 for (i
= 0; i
< len
; i
++) {
209 if (isprint(data
[i
]))
210 printf("%c", data
[i
]);
212 printf("\\x%.2x", data
[i
]);
216 static int hex2num(char digit
)
218 if (!isxdigit(digit
))
222 return tolower(digit
) - 'a' + 10;
225 static int hex2byte(char *hex
)
229 d1
= hex2num(hex
[0]);
232 d2
= hex2num(hex
[1]);
235 return (d1
<< 4) | d2
;
238 static char *hex2bin(char *hex
, char *buf
)
255 int parse_keys(struct nl_msg
*msg
, char **argv
, int argc
)
259 bool have_default
= false;
265 NLA_PUT_FLAG(msg
, NL80211_ATTR_PRIVACY
);
267 keys
= nla_nest_start(msg
, NL80211_ATTR_KEYS
);
274 struct nlattr
*key
= nla_nest_start(msg
, ++i
);
280 if (arg
[pos
] == 'd') {
281 NLA_PUT_FLAG(msg
, NL80211_KEY_DEFAULT
);
288 if (!isdigit(arg
[pos
]))
290 NLA_PUT_U8(msg
, NL80211_KEY_IDX
, arg
[pos
++] - '0');
291 if (arg
[pos
++] != ':')
294 switch (strlen(keydata
)) {
296 keydata
= hex2bin(keydata
, keybuf
);
298 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
, 0x000FAC01);
302 keydata
= hex2bin(keydata
, keybuf
);
304 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
, 0x000FAC05);
314 NLA_PUT(msg
, NL80211_KEY_DATA
, keylen
, keydata
);
319 /* one key should be TX key */
320 if (!have_default
&& !argc
)
321 NLA_PUT_FLAG(msg
, NL80211_KEY_DEFAULT
);
323 nla_nest_end(msg
, key
);
326 nla_nest_end(msg
, keys
);
332 fprintf(stderr
, "key must be [d:]index:data where\n"
333 " 'd:' means default (transmit) key\n"
334 " 'index:' is a single digit (0-3)\n"
335 " 'data' must be 5 or 13 ascii chars\n"
336 " or 10 or 26 hex digits\n"
337 "for example: d:2:6162636465 is the same as d:2:abcde\n");
341 static void print_mcs_index(const __u8
*mcs
)
343 unsigned int mcs_bit
, prev_bit
= -2, prev_cont
= 0;
345 for (mcs_bit
= 0; mcs_bit
<= 76; mcs_bit
++) {
346 unsigned int mcs_octet
= mcs_bit
/8;
347 unsigned int MCS_RATE_BIT
= 1 << mcs_bit
% 8;
348 bool mcs_rate_idx_set
;
350 mcs_rate_idx_set
= !!(mcs
[mcs_octet
] & MCS_RATE_BIT
);
352 if (!mcs_rate_idx_set
)
355 if (prev_bit
!= mcs_bit
- 1) {
357 printf("%d, ", prev_bit
);
360 printf("%d", mcs_bit
);
362 } else if (!prev_cont
) {
371 printf("%d", prev_bit
);
376 * There are only 4 possible values, we just use a case instead of computing it,
377 * but technically this can also be computed through the formula:
379 * Max AMPDU length = (2 ^ (13 + exponent)) - 1 bytes
381 static __u32
compute_ampdu_length(__u8 exponent
)
384 case 0: return 8191; /* (2 ^(13 + 0)) -1 */
385 case 1: return 16383; /* (2 ^(13 + 1)) -1 */
386 case 2: return 32767; /* (2 ^(13 + 2)) -1 */
387 case 3: return 65535; /* (2 ^(13 + 3)) -1 */
392 static const char *print_ampdu_space(__u8 space
)
395 case 0: return "No restriction";
396 case 1: return "1/4 usec";
397 case 2: return "1/2 usec";
398 case 3: return "1 usec";
399 case 4: return "2 usec";
400 case 5: return "4 usec";
401 case 6: return "8 usec";
402 case 7: return "16 usec";
404 return "BUG (spacing more than 3 bits!)";
408 void print_ampdu_length(__u8 exponent
)
410 __u32 max_ampdu_length
;
412 max_ampdu_length
= compute_ampdu_length(exponent
);
414 if (max_ampdu_length
) {
415 printf("\t\tMaximum RX AMPDU length %d bytes (exponent: 0x0%02x)\n",
416 max_ampdu_length
, exponent
);
418 printf("\t\tMaximum RX AMPDU length: unrecognized bytes "
419 "(exponent: %d)\n", exponent
);
423 void print_ampdu_spacing(__u8 spacing
)
425 printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n",
426 print_ampdu_space(spacing
), spacing
);
429 void print_ht_capability(__u16 cap
)
431 #define PRINT_HT_CAP(_cond, _str) \
434 printf("\t\t\t" _str "\n"); \
437 printf("\t\tCapabilities: 0x%02x\n", cap
);
439 PRINT_HT_CAP((cap
& BIT(0)), "RX LDCP");
440 PRINT_HT_CAP((cap
& BIT(1)), "HT20/HT40");
441 PRINT_HT_CAP(!(cap
& BIT(1)), "HT20");
443 PRINT_HT_CAP(((cap
>> 2) & 0x3) == 0, "Static SM Power Save");
444 PRINT_HT_CAP(((cap
>> 2) & 0x3) == 1, "Dynamic SM Power Save");
445 PRINT_HT_CAP(((cap
>> 2) & 0x3) == 3, "SM Power Save disabled");
447 PRINT_HT_CAP((cap
& BIT(4)), "RX Greenfield");
448 PRINT_HT_CAP((cap
& BIT(5)), "RX HT20 SGI");
449 PRINT_HT_CAP((cap
& BIT(6)), "RX HT40 SGI");
450 PRINT_HT_CAP((cap
& BIT(7)), "TX STBC");
452 PRINT_HT_CAP(((cap
>> 8) & 0x3) == 0, "No RX STBC");
453 PRINT_HT_CAP(((cap
>> 8) & 0x3) == 1, "RX STBC 1-stream");
454 PRINT_HT_CAP(((cap
>> 8) & 0x3) == 2, "RX STBC 2-streams");
455 PRINT_HT_CAP(((cap
>> 8) & 0x3) == 3, "RX STBC 3-streams");
457 PRINT_HT_CAP((cap
& BIT(10)), "HT Delayed Block Ack");
459 PRINT_HT_CAP((cap
& BIT(11)), "Max AMSDU length: 3839 bytes");
460 PRINT_HT_CAP(!(cap
& BIT(11)), "Max AMSDU length: 7935 bytes");
463 * For beacons and probe response this would mean the BSS
464 * does or does not allow the usage of DSSS/CCK HT40.
465 * Otherwise it means the STA does or does not use
468 PRINT_HT_CAP((cap
& BIT(12)), "DSSS/CCK HT40");
469 PRINT_HT_CAP(!(cap
& BIT(12)), "No DSSS/CCK HT40");
471 /* BIT(13) is reserved */
473 PRINT_HT_CAP((cap
& BIT(14)), "40 MHz Intolerant");
475 PRINT_HT_CAP((cap
& BIT(15)), "L-SIG TXOP protection");
479 void print_ht_mcs(const __u8
*mcs
)
481 /* As defined in 7.3.2.57.4 Supported MCS Set field */
482 unsigned int tx_max_num_spatial_streams
, max_rx_supp_data_rate
;
483 bool tx_mcs_set_defined
, tx_mcs_set_equal
, tx_unequal_modulation
;
485 max_rx_supp_data_rate
= ((mcs
[10] >> 8) & ((mcs
[11] & 0x3) << 8));
486 tx_mcs_set_defined
= !!(mcs
[12] & (1 << 0));
487 tx_mcs_set_equal
= !(mcs
[12] & (1 << 1));
488 tx_max_num_spatial_streams
= ((mcs
[12] >> 2) & 3) + 1;
489 tx_unequal_modulation
= !!(mcs
[12] & (1 << 4));
491 if (max_rx_supp_data_rate
)
492 printf("\t\tHT Max RX data rate: %d Mbps\n", max_rx_supp_data_rate
);
493 /* XXX: else see 9.6.0e.5.3 how to get this I think */
495 if (tx_mcs_set_defined
) {
496 if (tx_mcs_set_equal
) {
497 printf("\t\tHT TX/RX MCS rate indexes supported:");
498 print_mcs_index(mcs
);
500 printf("\t\tHT RX MCS rate indexes supported:");
501 print_mcs_index(mcs
);
503 if (tx_unequal_modulation
)
504 printf("\t\tTX unequal modulation supported\n");
506 printf("\t\tTX unequal modulation not supported\n");
508 printf("\t\tHT TX Max spatial streams: %d\n",
509 tx_max_num_spatial_streams
);
511 printf("\t\tHT TX MCS rate indexes supported may differ\n");
514 printf("\t\tHT RX MCS rate indexes supported:");
515 print_mcs_index(mcs
);
516 printf("\t\tHT TX MCS rate indexes are undefined\n");