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 int parse_hex_mask(char *hexmask
, unsigned char **result
, size_t *result_len
,
54 size_t len
= strlen(hexmask
) / 2;
55 unsigned char *result_val
;
56 unsigned char *result_mask
= NULL
;
62 result_val
= calloc(len
+ 2, 1);
67 result_mask
= calloc(DIV_ROUND_UP(len
, 8) + 2, 1);
74 char *cp
= strchr(hexmask
, ':');
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 */
88 temp
= strtoul(hexmask
, &end
, 16);
91 if (temp
< 0 || temp
> 255)
93 result_val
[pos
] = temp
;
97 result_mask
[mask_pos
] |= 1 << (pos
% 8);
115 unsigned char *parse_hex(char *hex
, size_t *outlen
)
117 unsigned char *result
;
119 if (parse_hex_mask(hex
, &result
, outlen
, NULL
))
124 static const char *ifmodes
[NL80211_IFTYPE_MAX
+ 1] = {
137 static char modebuf
[100];
139 const char *iftype_name(enum nl80211_iftype iftype
)
141 if (iftype
<= NL80211_IFTYPE_MAX
)
142 return ifmodes
[iftype
];
143 sprintf(modebuf
, "Unknown mode (%d)", iftype
);
147 static const char *commands
[NL80211_CMD_MAX
+ 1] = {
148 [NL80211_CMD_GET_WIPHY
] = "get_wiphy",
149 [NL80211_CMD_SET_WIPHY
] = "set_wiphy",
150 [NL80211_CMD_NEW_WIPHY
] = "new_wiphy",
151 [NL80211_CMD_DEL_WIPHY
] = "del_wiphy",
152 [NL80211_CMD_GET_INTERFACE
] = "get_interface",
153 [NL80211_CMD_SET_INTERFACE
] = "set_interface",
154 [NL80211_CMD_NEW_INTERFACE
] = "new_interface",
155 [NL80211_CMD_DEL_INTERFACE
] = "del_interface",
156 [NL80211_CMD_GET_KEY
] = "get_key",
157 [NL80211_CMD_SET_KEY
] = "set_key",
158 [NL80211_CMD_NEW_KEY
] = "new_key",
159 [NL80211_CMD_DEL_KEY
] = "del_key",
160 [NL80211_CMD_GET_BEACON
] = "get_beacon",
161 [NL80211_CMD_SET_BEACON
] = "set_beacon",
162 [NL80211_CMD_NEW_BEACON
] = "new_beacon",
163 [NL80211_CMD_DEL_BEACON
] = "del_beacon",
164 [NL80211_CMD_GET_STATION
] = "get_station",
165 [NL80211_CMD_SET_STATION
] = "set_station",
166 [NL80211_CMD_NEW_STATION
] = "new_station",
167 [NL80211_CMD_DEL_STATION
] = "del_station",
168 [NL80211_CMD_GET_MPATH
] = "get_mpath",
169 [NL80211_CMD_SET_MPATH
] = "set_mpath",
170 [NL80211_CMD_NEW_MPATH
] = "new_mpath",
171 [NL80211_CMD_DEL_MPATH
] = "del_mpath",
172 [NL80211_CMD_SET_BSS
] = "set_bss",
173 [NL80211_CMD_SET_REG
] = "set_reg",
174 [NL80211_CMD_REQ_SET_REG
] = "reg_set_reg",
175 [NL80211_CMD_GET_MESH_PARAMS
] = "get_mesh_params",
176 [NL80211_CMD_SET_MESH_PARAMS
] = "set_mesh_params",
177 [NL80211_CMD_SET_MGMT_EXTRA_IE
] = "set_mgmt_extra_ie",
178 [NL80211_CMD_GET_REG
] = "get_reg",
179 [NL80211_CMD_GET_SCAN
] = "get_scan",
180 [NL80211_CMD_TRIGGER_SCAN
] = "trigger_scan",
181 [NL80211_CMD_NEW_SCAN_RESULTS
] = "new_scan_results",
182 [NL80211_CMD_SCAN_ABORTED
] = "scan_aborted",
183 [NL80211_CMD_REG_CHANGE
] = "reg_change",
184 [NL80211_CMD_AUTHENTICATE
] = "authenticate",
185 [NL80211_CMD_ASSOCIATE
] = "associate",
186 [NL80211_CMD_DEAUTHENTICATE
] = "deauthenticate",
187 [NL80211_CMD_DISASSOCIATE
] = "disassociate",
188 [NL80211_CMD_MICHAEL_MIC_FAILURE
] = "michael_mic_failure",
189 [NL80211_CMD_REG_BEACON_HINT
] = "reg_beacon_hint",
190 [NL80211_CMD_JOIN_IBSS
] = "join_ibss",
191 [NL80211_CMD_LEAVE_IBSS
] = "leave_ibss",
192 [NL80211_CMD_TESTMODE
] = "testmode",
193 [NL80211_CMD_CONNECT
] = "connect",
194 [NL80211_CMD_ROAM
] = "roam",
195 [NL80211_CMD_DISCONNECT
] = "disconnect",
196 [NL80211_CMD_SET_WIPHY_NETNS
] = "set_wiphy_netns",
197 [NL80211_CMD_GET_SURVEY
] = "get_survey",
198 [NL80211_CMD_SET_PMKSA
] = "set_pmksa",
199 [NL80211_CMD_DEL_PMKSA
] = "del_pmksa",
200 [NL80211_CMD_FLUSH_PMKSA
] = "flush_pmksa",
201 [NL80211_CMD_REMAIN_ON_CHANNEL
] = "remain_on_channel",
202 [NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL
] = "cancel_remain_on_channel",
203 [NL80211_CMD_SET_TX_BITRATE_MASK
] = "set_tx_bitrate_mask",
204 [NL80211_CMD_REGISTER_ACTION
] = "register_action",
205 [NL80211_CMD_ACTION
] = "action",
206 [NL80211_CMD_SET_CHANNEL
] = "set_channel",
207 [NL80211_CMD_SET_WDS_PEER
] = "set_wds_peer",
208 [NL80211_CMD_FRAME_WAIT_CANCEL
] = "frame_wait_cancel",
209 [NL80211_CMD_JOIN_MESH
] = "join_mesh",
210 [NL80211_CMD_LEAVE_MESH
] = "leave_mesh",
211 [NL80211_CMD_SET_REKEY_OFFLOAD
] = "set_rekey_offload",
214 static char cmdbuf
[100];
216 const char *command_name(enum nl80211_commands cmd
)
218 if (cmd
<= NL80211_CMD_MAX
&& commands
[cmd
])
219 return commands
[cmd
];
220 sprintf(cmdbuf
, "Unknown command (%d)", cmd
);
224 int ieee80211_channel_to_frequency(int chan
)
227 return 2407 + chan
* 5;
232 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
233 return (chan
+ 1000) * 5;
236 int ieee80211_frequency_to_channel(int freq
)
242 return (freq
- 2407) / 5;
244 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
245 return freq
/5 - 1000;
248 void print_ssid_escaped(const uint8_t len
, const uint8_t *data
)
252 for (i
= 0; i
< len
; i
++) {
253 if (isprint(data
[i
]) && data
[i
] != ' ' && data
[i
] != '\\')
254 printf("%c", data
[i
]);
255 else if (data
[i
] == ' ' &&
256 (i
!= 0 && i
!= len
-1))
259 printf("\\x%.2x", data
[i
]);
263 static int hex2num(char digit
)
265 if (!isxdigit(digit
))
269 return tolower(digit
) - 'a' + 10;
272 static int hex2byte(char *hex
)
276 d1
= hex2num(hex
[0]);
279 d2
= hex2num(hex
[1]);
282 return (d1
<< 4) | d2
;
285 static char *hex2bin(char *hex
, char *buf
)
302 int parse_keys(struct nl_msg
*msg
, char **argv
, int argc
)
306 bool have_default
= false;
312 NLA_PUT_FLAG(msg
, NL80211_ATTR_PRIVACY
);
314 keys
= nla_nest_start(msg
, NL80211_ATTR_KEYS
);
321 struct nlattr
*key
= nla_nest_start(msg
, ++i
);
327 if (arg
[pos
] == 'd') {
328 NLA_PUT_FLAG(msg
, NL80211_KEY_DEFAULT
);
335 if (!isdigit(arg
[pos
]))
337 NLA_PUT_U8(msg
, NL80211_KEY_IDX
, arg
[pos
++] - '0');
338 if (arg
[pos
++] != ':')
341 switch (strlen(keydata
)) {
343 keydata
= hex2bin(keydata
, keybuf
);
345 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
, 0x000FAC01);
349 keydata
= hex2bin(keydata
, keybuf
);
351 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
, 0x000FAC05);
361 NLA_PUT(msg
, NL80211_KEY_DATA
, keylen
, keydata
);
366 /* one key should be TX key */
367 if (!have_default
&& !argc
)
368 NLA_PUT_FLAG(msg
, NL80211_KEY_DEFAULT
);
370 nla_nest_end(msg
, key
);
373 nla_nest_end(msg
, keys
);
379 fprintf(stderr
, "key must be [d:]index:data where\n"
380 " 'd:' means default (transmit) key\n"
381 " 'index:' is a single digit (0-3)\n"
382 " 'data' must be 5 or 13 ascii chars\n"
383 " or 10 or 26 hex digits\n"
384 "for example: d:2:6162636465 is the same as d:2:abcde\n");
388 static void print_mcs_index(const __u8
*mcs
)
390 int mcs_bit
, prev_bit
= -2, prev_cont
= 0;
392 for (mcs_bit
= 0; mcs_bit
<= 76; mcs_bit
++) {
393 unsigned int mcs_octet
= mcs_bit
/8;
394 unsigned int MCS_RATE_BIT
= 1 << mcs_bit
% 8;
395 bool mcs_rate_idx_set
;
397 mcs_rate_idx_set
= !!(mcs
[mcs_octet
] & MCS_RATE_BIT
);
399 if (!mcs_rate_idx_set
)
402 if (prev_bit
!= mcs_bit
- 1) {
404 printf("%d, ", prev_bit
);
407 printf("%d", mcs_bit
);
409 } else if (!prev_cont
) {
418 printf("%d", prev_bit
);
423 * There are only 4 possible values, we just use a case instead of computing it,
424 * but technically this can also be computed through the formula:
426 * Max AMPDU length = (2 ^ (13 + exponent)) - 1 bytes
428 static __u32
compute_ampdu_length(__u8 exponent
)
431 case 0: return 8191; /* (2 ^(13 + 0)) -1 */
432 case 1: return 16383; /* (2 ^(13 + 1)) -1 */
433 case 2: return 32767; /* (2 ^(13 + 2)) -1 */
434 case 3: return 65535; /* (2 ^(13 + 3)) -1 */
439 static const char *print_ampdu_space(__u8 space
)
442 case 0: return "No restriction";
443 case 1: return "1/4 usec";
444 case 2: return "1/2 usec";
445 case 3: return "1 usec";
446 case 4: return "2 usec";
447 case 5: return "4 usec";
448 case 6: return "8 usec";
449 case 7: return "16 usec";
451 return "BUG (spacing more than 3 bits!)";
455 void print_ampdu_length(__u8 exponent
)
457 __u32 max_ampdu_length
;
459 max_ampdu_length
= compute_ampdu_length(exponent
);
461 if (max_ampdu_length
) {
462 printf("\t\tMaximum RX AMPDU length %d bytes (exponent: 0x0%02x)\n",
463 max_ampdu_length
, exponent
);
465 printf("\t\tMaximum RX AMPDU length: unrecognized bytes "
466 "(exponent: %d)\n", exponent
);
470 void print_ampdu_spacing(__u8 spacing
)
472 printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n",
473 print_ampdu_space(spacing
), spacing
);
476 void print_ht_capability(__u16 cap
)
478 #define PRINT_HT_CAP(_cond, _str) \
481 printf("\t\t\t" _str "\n"); \
484 printf("\t\tCapabilities: 0x%02x\n", cap
);
486 PRINT_HT_CAP((cap
& BIT(0)), "RX LDPC");
487 PRINT_HT_CAP((cap
& BIT(1)), "HT20/HT40");
488 PRINT_HT_CAP(!(cap
& BIT(1)), "HT20");
490 PRINT_HT_CAP(((cap
>> 2) & 0x3) == 0, "Static SM Power Save");
491 PRINT_HT_CAP(((cap
>> 2) & 0x3) == 1, "Dynamic SM Power Save");
492 PRINT_HT_CAP(((cap
>> 2) & 0x3) == 3, "SM Power Save disabled");
494 PRINT_HT_CAP((cap
& BIT(4)), "RX Greenfield");
495 PRINT_HT_CAP((cap
& BIT(5)), "RX HT20 SGI");
496 PRINT_HT_CAP((cap
& BIT(6)), "RX HT40 SGI");
497 PRINT_HT_CAP((cap
& BIT(7)), "TX STBC");
499 PRINT_HT_CAP(((cap
>> 8) & 0x3) == 0, "No RX STBC");
500 PRINT_HT_CAP(((cap
>> 8) & 0x3) == 1, "RX STBC 1-stream");
501 PRINT_HT_CAP(((cap
>> 8) & 0x3) == 2, "RX STBC 2-streams");
502 PRINT_HT_CAP(((cap
>> 8) & 0x3) == 3, "RX STBC 3-streams");
504 PRINT_HT_CAP((cap
& BIT(10)), "HT Delayed Block Ack");
506 PRINT_HT_CAP(!(cap
& BIT(11)), "Max AMSDU length: 3839 bytes");
507 PRINT_HT_CAP((cap
& BIT(11)), "Max AMSDU length: 7935 bytes");
510 * For beacons and probe response this would mean the BSS
511 * does or does not allow the usage of DSSS/CCK HT40.
512 * Otherwise it means the STA does or does not use
515 PRINT_HT_CAP((cap
& BIT(12)), "DSSS/CCK HT40");
516 PRINT_HT_CAP(!(cap
& BIT(12)), "No DSSS/CCK HT40");
518 /* BIT(13) is reserved */
520 PRINT_HT_CAP((cap
& BIT(14)), "40 MHz Intolerant");
522 PRINT_HT_CAP((cap
& BIT(15)), "L-SIG TXOP protection");
526 void print_ht_mcs(const __u8
*mcs
)
528 /* As defined in 7.3.2.57.4 Supported MCS Set field */
529 unsigned int tx_max_num_spatial_streams
, max_rx_supp_data_rate
;
530 bool tx_mcs_set_defined
, tx_mcs_set_equal
, tx_unequal_modulation
;
532 max_rx_supp_data_rate
= ((mcs
[10] >> 8) & ((mcs
[11] & 0x3) << 8));
533 tx_mcs_set_defined
= !!(mcs
[12] & (1 << 0));
534 tx_mcs_set_equal
= !(mcs
[12] & (1 << 1));
535 tx_max_num_spatial_streams
= ((mcs
[12] >> 2) & 3) + 1;
536 tx_unequal_modulation
= !!(mcs
[12] & (1 << 4));
538 if (max_rx_supp_data_rate
)
539 printf("\t\tHT Max RX data rate: %d Mbps\n", max_rx_supp_data_rate
);
540 /* XXX: else see 9.6.0e.5.3 how to get this I think */
542 if (tx_mcs_set_defined
) {
543 if (tx_mcs_set_equal
) {
544 printf("\t\tHT TX/RX MCS rate indexes supported:");
545 print_mcs_index(mcs
);
547 printf("\t\tHT RX MCS rate indexes supported:");
548 print_mcs_index(mcs
);
550 if (tx_unequal_modulation
)
551 printf("\t\tTX unequal modulation supported\n");
553 printf("\t\tTX unequal modulation not supported\n");
555 printf("\t\tHT TX Max spatial streams: %d\n",
556 tx_max_num_spatial_streams
);
558 printf("\t\tHT TX MCS rate indexes supported may differ\n");
561 printf("\t\tHT RX MCS rate indexes supported:");
562 print_mcs_index(mcs
);
563 printf("\t\tHT TX MCS rate indexes are undefined\n");