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] = {
138 static char modebuf
[100];
140 const char *iftype_name(enum nl80211_iftype iftype
)
142 if (iftype
<= NL80211_IFTYPE_MAX
&& ifmodes
[iftype
])
143 return ifmodes
[iftype
];
144 sprintf(modebuf
, "Unknown mode (%d)", iftype
);
148 static const char *commands
[NL80211_CMD_MAX
+ 1] = {
150 * sed 's/^\tNL80211_CMD_//;t n;d;:n s%^\([^=]*\),.*%\t[NL80211_CMD_\1] = \"\L\1\",%;t;d' nl80211.h
152 [NL80211_CMD_UNSPEC
] = "unspec",
153 [NL80211_CMD_GET_WIPHY
] = "get_wiphy",
154 [NL80211_CMD_SET_WIPHY
] = "set_wiphy",
155 [NL80211_CMD_NEW_WIPHY
] = "new_wiphy",
156 [NL80211_CMD_DEL_WIPHY
] = "del_wiphy",
157 [NL80211_CMD_GET_INTERFACE
] = "get_interface",
158 [NL80211_CMD_SET_INTERFACE
] = "set_interface",
159 [NL80211_CMD_NEW_INTERFACE
] = "new_interface",
160 [NL80211_CMD_DEL_INTERFACE
] = "del_interface",
161 [NL80211_CMD_GET_KEY
] = "get_key",
162 [NL80211_CMD_SET_KEY
] = "set_key",
163 [NL80211_CMD_NEW_KEY
] = "new_key",
164 [NL80211_CMD_DEL_KEY
] = "del_key",
165 [NL80211_CMD_GET_BEACON
] = "get_beacon",
166 [NL80211_CMD_SET_BEACON
] = "set_beacon",
167 [NL80211_CMD_START_AP
] = "start_ap",
168 [NL80211_CMD_STOP_AP
] = "stop_ap",
169 [NL80211_CMD_GET_STATION
] = "get_station",
170 [NL80211_CMD_SET_STATION
] = "set_station",
171 [NL80211_CMD_NEW_STATION
] = "new_station",
172 [NL80211_CMD_DEL_STATION
] = "del_station",
173 [NL80211_CMD_GET_MPATH
] = "get_mpath",
174 [NL80211_CMD_SET_MPATH
] = "set_mpath",
175 [NL80211_CMD_NEW_MPATH
] = "new_mpath",
176 [NL80211_CMD_DEL_MPATH
] = "del_mpath",
177 [NL80211_CMD_SET_BSS
] = "set_bss",
178 [NL80211_CMD_SET_REG
] = "set_reg",
179 [NL80211_CMD_REQ_SET_REG
] = "req_set_reg",
180 [NL80211_CMD_GET_MESH_CONFIG
] = "get_mesh_config",
181 [NL80211_CMD_SET_MESH_CONFIG
] = "set_mesh_config",
182 [NL80211_CMD_GET_REG
] = "get_reg",
183 [NL80211_CMD_GET_SCAN
] = "get_scan",
184 [NL80211_CMD_TRIGGER_SCAN
] = "trigger_scan",
185 [NL80211_CMD_NEW_SCAN_RESULTS
] = "new_scan_results",
186 [NL80211_CMD_SCAN_ABORTED
] = "scan_aborted",
187 [NL80211_CMD_REG_CHANGE
] = "reg_change",
188 [NL80211_CMD_AUTHENTICATE
] = "authenticate",
189 [NL80211_CMD_ASSOCIATE
] = "associate",
190 [NL80211_CMD_DEAUTHENTICATE
] = "deauthenticate",
191 [NL80211_CMD_DISASSOCIATE
] = "disassociate",
192 [NL80211_CMD_MICHAEL_MIC_FAILURE
] = "michael_mic_failure",
193 [NL80211_CMD_REG_BEACON_HINT
] = "reg_beacon_hint",
194 [NL80211_CMD_JOIN_IBSS
] = "join_ibss",
195 [NL80211_CMD_LEAVE_IBSS
] = "leave_ibss",
196 [NL80211_CMD_TESTMODE
] = "testmode",
197 [NL80211_CMD_CONNECT
] = "connect",
198 [NL80211_CMD_ROAM
] = "roam",
199 [NL80211_CMD_DISCONNECT
] = "disconnect",
200 [NL80211_CMD_SET_WIPHY_NETNS
] = "set_wiphy_netns",
201 [NL80211_CMD_GET_SURVEY
] = "get_survey",
202 [NL80211_CMD_NEW_SURVEY_RESULTS
] = "new_survey_results",
203 [NL80211_CMD_SET_PMKSA
] = "set_pmksa",
204 [NL80211_CMD_DEL_PMKSA
] = "del_pmksa",
205 [NL80211_CMD_FLUSH_PMKSA
] = "flush_pmksa",
206 [NL80211_CMD_REMAIN_ON_CHANNEL
] = "remain_on_channel",
207 [NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL
] = "cancel_remain_on_channel",
208 [NL80211_CMD_SET_TX_BITRATE_MASK
] = "set_tx_bitrate_mask",
209 [NL80211_CMD_REGISTER_FRAME
] = "register_frame",
210 [NL80211_CMD_FRAME
] = "frame",
211 [NL80211_CMD_FRAME_TX_STATUS
] = "frame_tx_status",
212 [NL80211_CMD_SET_POWER_SAVE
] = "set_power_save",
213 [NL80211_CMD_GET_POWER_SAVE
] = "get_power_save",
214 [NL80211_CMD_SET_CQM
] = "set_cqm",
215 [NL80211_CMD_NOTIFY_CQM
] = "notify_cqm",
216 [NL80211_CMD_SET_CHANNEL
] = "set_channel",
217 [NL80211_CMD_SET_WDS_PEER
] = "set_wds_peer",
218 [NL80211_CMD_FRAME_WAIT_CANCEL
] = "frame_wait_cancel",
219 [NL80211_CMD_JOIN_MESH
] = "join_mesh",
220 [NL80211_CMD_LEAVE_MESH
] = "leave_mesh",
221 [NL80211_CMD_UNPROT_DEAUTHENTICATE
] = "unprot_deauthenticate",
222 [NL80211_CMD_UNPROT_DISASSOCIATE
] = "unprot_disassociate",
223 [NL80211_CMD_NEW_PEER_CANDIDATE
] = "new_peer_candidate",
224 [NL80211_CMD_GET_WOWLAN
] = "get_wowlan",
225 [NL80211_CMD_SET_WOWLAN
] = "set_wowlan",
226 [NL80211_CMD_START_SCHED_SCAN
] = "start_sched_scan",
227 [NL80211_CMD_STOP_SCHED_SCAN
] = "stop_sched_scan",
228 [NL80211_CMD_SCHED_SCAN_RESULTS
] = "sched_scan_results",
229 [NL80211_CMD_SCHED_SCAN_STOPPED
] = "sched_scan_stopped",
230 [NL80211_CMD_SET_REKEY_OFFLOAD
] = "set_rekey_offload",
231 [NL80211_CMD_PMKSA_CANDIDATE
] = "pmksa_candidate",
232 [NL80211_CMD_TDLS_OPER
] = "tdls_oper",
233 [NL80211_CMD_TDLS_MGMT
] = "tdls_mgmt",
234 [NL80211_CMD_UNEXPECTED_FRAME
] = "unexpected_frame",
235 [NL80211_CMD_PROBE_CLIENT
] = "probe_client",
236 [NL80211_CMD_REGISTER_BEACONS
] = "register_beacons",
237 [NL80211_CMD_UNEXPECTED_4ADDR_FRAME
] = "unexpected_4addr_frame",
238 [NL80211_CMD_SET_NOACK_MAP
] = "set_noack_map",
239 [NL80211_CMD_CH_SWITCH_NOTIFY
] = "ch_switch_notify",
240 [NL80211_CMD_START_P2P_DEVICE
] = "start_p2p_device",
241 [NL80211_CMD_STOP_P2P_DEVICE
] = "stop_p2p_device",
242 [NL80211_CMD_CONN_FAILED
] = "conn_failed",
245 static char cmdbuf
[100];
247 const char *command_name(enum nl80211_commands cmd
)
249 if (cmd
<= NL80211_CMD_MAX
&& commands
[cmd
])
250 return commands
[cmd
];
251 sprintf(cmdbuf
, "Unknown command (%d)", cmd
);
255 int ieee80211_channel_to_frequency(int chan
)
258 return 2407 + chan
* 5;
263 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
264 return (chan
+ 1000) * 5;
267 int ieee80211_frequency_to_channel(int freq
)
273 return (freq
- 2407) / 5;
275 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
277 return freq
/5 - 1000;
279 if (freq
>= 58320 && freq
<= 64800)
280 return (freq
- 56160) / 2160;
285 void print_ssid_escaped(const uint8_t len
, const uint8_t *data
)
289 for (i
= 0; i
< len
; i
++) {
290 if (isprint(data
[i
]) && data
[i
] != ' ' && data
[i
] != '\\')
291 printf("%c", data
[i
]);
292 else if (data
[i
] == ' ' &&
293 (i
!= 0 && i
!= len
-1))
296 printf("\\x%.2x", data
[i
]);
300 static int hex2num(char digit
)
302 if (!isxdigit(digit
))
306 return tolower(digit
) - 'a' + 10;
309 static int hex2byte(char *hex
)
313 d1
= hex2num(hex
[0]);
316 d2
= hex2num(hex
[1]);
319 return (d1
<< 4) | d2
;
322 static char *hex2bin(char *hex
, char *buf
)
339 int parse_keys(struct nl_msg
*msg
, char **argv
, int argc
)
343 bool have_default
= false;
349 NLA_PUT_FLAG(msg
, NL80211_ATTR_PRIVACY
);
351 keys
= nla_nest_start(msg
, NL80211_ATTR_KEYS
);
358 struct nlattr
*key
= nla_nest_start(msg
, ++i
);
364 if (arg
[pos
] == 'd') {
365 NLA_PUT_FLAG(msg
, NL80211_KEY_DEFAULT
);
372 if (!isdigit(arg
[pos
]))
374 NLA_PUT_U8(msg
, NL80211_KEY_IDX
, arg
[pos
++] - '0');
375 if (arg
[pos
++] != ':')
378 switch (strlen(keydata
)) {
380 keydata
= hex2bin(keydata
, keybuf
);
382 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
, 0x000FAC01);
386 keydata
= hex2bin(keydata
, keybuf
);
388 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
, 0x000FAC05);
398 NLA_PUT(msg
, NL80211_KEY_DATA
, keylen
, keydata
);
403 /* one key should be TX key */
404 if (!have_default
&& !argc
)
405 NLA_PUT_FLAG(msg
, NL80211_KEY_DEFAULT
);
407 nla_nest_end(msg
, key
);
410 nla_nest_end(msg
, keys
);
416 fprintf(stderr
, "key must be [d:]index:data where\n"
417 " 'd:' means default (transmit) key\n"
418 " 'index:' is a single digit (0-3)\n"
419 " 'data' must be 5 or 13 ascii chars\n"
420 " or 10 or 26 hex digits\n"
421 "for example: d:2:6162636465 is the same as d:2:abcde\n");
425 static void print_mcs_index(const __u8
*mcs
)
427 int mcs_bit
, prev_bit
= -2, prev_cont
= 0;
429 for (mcs_bit
= 0; mcs_bit
<= 76; mcs_bit
++) {
430 unsigned int mcs_octet
= mcs_bit
/8;
431 unsigned int MCS_RATE_BIT
= 1 << mcs_bit
% 8;
432 bool mcs_rate_idx_set
;
434 mcs_rate_idx_set
= !!(mcs
[mcs_octet
] & MCS_RATE_BIT
);
436 if (!mcs_rate_idx_set
)
439 if (prev_bit
!= mcs_bit
- 1) {
441 printf("%d, ", prev_bit
);
444 printf("%d", mcs_bit
);
446 } else if (!prev_cont
) {
455 printf("%d", prev_bit
);
460 * There are only 4 possible values, we just use a case instead of computing it,
461 * but technically this can also be computed through the formula:
463 * Max AMPDU length = (2 ^ (13 + exponent)) - 1 bytes
465 static __u32
compute_ampdu_length(__u8 exponent
)
468 case 0: return 8191; /* (2 ^(13 + 0)) -1 */
469 case 1: return 16383; /* (2 ^(13 + 1)) -1 */
470 case 2: return 32767; /* (2 ^(13 + 2)) -1 */
471 case 3: return 65535; /* (2 ^(13 + 3)) -1 */
476 static const char *print_ampdu_space(__u8 space
)
479 case 0: return "No restriction";
480 case 1: return "1/4 usec";
481 case 2: return "1/2 usec";
482 case 3: return "1 usec";
483 case 4: return "2 usec";
484 case 5: return "4 usec";
485 case 6: return "8 usec";
486 case 7: return "16 usec";
488 return "BUG (spacing more than 3 bits!)";
492 void print_ampdu_length(__u8 exponent
)
494 __u32 max_ampdu_length
;
496 max_ampdu_length
= compute_ampdu_length(exponent
);
498 if (max_ampdu_length
) {
499 printf("\t\tMaximum RX AMPDU length %d bytes (exponent: 0x0%02x)\n",
500 max_ampdu_length
, exponent
);
502 printf("\t\tMaximum RX AMPDU length: unrecognized bytes "
503 "(exponent: %d)\n", exponent
);
507 void print_ampdu_spacing(__u8 spacing
)
509 printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n",
510 print_ampdu_space(spacing
), spacing
);
513 void print_ht_capability(__u16 cap
)
515 #define PRINT_HT_CAP(_cond, _str) \
518 printf("\t\t\t" _str "\n"); \
521 printf("\t\tCapabilities: 0x%02x\n", cap
);
523 PRINT_HT_CAP((cap
& BIT(0)), "RX LDPC");
524 PRINT_HT_CAP((cap
& BIT(1)), "HT20/HT40");
525 PRINT_HT_CAP(!(cap
& BIT(1)), "HT20");
527 PRINT_HT_CAP(((cap
>> 2) & 0x3) == 0, "Static SM Power Save");
528 PRINT_HT_CAP(((cap
>> 2) & 0x3) == 1, "Dynamic SM Power Save");
529 PRINT_HT_CAP(((cap
>> 2) & 0x3) == 3, "SM Power Save disabled");
531 PRINT_HT_CAP((cap
& BIT(4)), "RX Greenfield");
532 PRINT_HT_CAP((cap
& BIT(5)), "RX HT20 SGI");
533 PRINT_HT_CAP((cap
& BIT(6)), "RX HT40 SGI");
534 PRINT_HT_CAP((cap
& BIT(7)), "TX STBC");
536 PRINT_HT_CAP(((cap
>> 8) & 0x3) == 0, "No RX STBC");
537 PRINT_HT_CAP(((cap
>> 8) & 0x3) == 1, "RX STBC 1-stream");
538 PRINT_HT_CAP(((cap
>> 8) & 0x3) == 2, "RX STBC 2-streams");
539 PRINT_HT_CAP(((cap
>> 8) & 0x3) == 3, "RX STBC 3-streams");
541 PRINT_HT_CAP((cap
& BIT(10)), "HT Delayed Block Ack");
543 PRINT_HT_CAP(!(cap
& BIT(11)), "Max AMSDU length: 3839 bytes");
544 PRINT_HT_CAP((cap
& BIT(11)), "Max AMSDU length: 7935 bytes");
547 * For beacons and probe response this would mean the BSS
548 * does or does not allow the usage of DSSS/CCK HT40.
549 * Otherwise it means the STA does or does not use
552 PRINT_HT_CAP((cap
& BIT(12)), "DSSS/CCK HT40");
553 PRINT_HT_CAP(!(cap
& BIT(12)), "No DSSS/CCK HT40");
555 /* BIT(13) is reserved */
557 PRINT_HT_CAP((cap
& BIT(14)), "40 MHz Intolerant");
559 PRINT_HT_CAP((cap
& BIT(15)), "L-SIG TXOP protection");
563 void print_ht_mcs(const __u8
*mcs
)
565 /* As defined in 7.3.2.57.4 Supported MCS Set field */
566 unsigned int tx_max_num_spatial_streams
, max_rx_supp_data_rate
;
567 bool tx_mcs_set_defined
, tx_mcs_set_equal
, tx_unequal_modulation
;
569 max_rx_supp_data_rate
= ((mcs
[10] >> 8) & ((mcs
[11] & 0x3) << 8));
570 tx_mcs_set_defined
= !!(mcs
[12] & (1 << 0));
571 tx_mcs_set_equal
= !(mcs
[12] & (1 << 1));
572 tx_max_num_spatial_streams
= ((mcs
[12] >> 2) & 3) + 1;
573 tx_unequal_modulation
= !!(mcs
[12] & (1 << 4));
575 if (max_rx_supp_data_rate
)
576 printf("\t\tHT Max RX data rate: %d Mbps\n", max_rx_supp_data_rate
);
577 /* XXX: else see 9.6.0e.5.3 how to get this I think */
579 if (tx_mcs_set_defined
) {
580 if (tx_mcs_set_equal
) {
581 printf("\t\tHT TX/RX MCS rate indexes supported:");
582 print_mcs_index(mcs
);
584 printf("\t\tHT RX MCS rate indexes supported:");
585 print_mcs_index(mcs
);
587 if (tx_unequal_modulation
)
588 printf("\t\tTX unequal modulation supported\n");
590 printf("\t\tTX unequal modulation not supported\n");
592 printf("\t\tHT TX Max spatial streams: %d\n",
593 tx_max_num_spatial_streams
);
595 printf("\t\tHT TX MCS rate indexes supported may differ\n");
598 printf("\t\tHT RX MCS rate indexes supported:");
599 print_mcs_index(mcs
);
600 printf("\t\tHT TX MCS rate indexes are undefined\n");
604 void print_vht_info(__u32 capa
, const __u8
*mcs
)
609 printf("\t\tVHT Capabilities (0x%.8x):\n", capa
);
611 #define PRINT_VHT_CAPA(_bit, _str) \
613 if (capa & BIT(_bit)) \
614 printf("\t\t\t" _str "\n"); \
617 printf("\t\t\tMax MPDU length: ");
619 case 0: printf("3895\n"); break;
620 case 1: printf("7991\n"); break;
621 case 2: printf("11454\n"); break;
622 case 3: printf("(reserved)\n");
624 printf("\t\t\tSupported Channel Width: ");
625 switch ((capa
>> 2) & 3) {
626 case 0: printf("neither 160 nor 80+80\n"); break;
627 case 1: printf("160 MHz\n"); break;
628 case 2: printf("160 MHz, 80+80 MHz\n"); break;
629 case 3: printf("(reserved)\n");
631 PRINT_VHT_CAPA(4, "RX LDPC");
632 PRINT_VHT_CAPA(5, "short GI (80 MHz)");
633 PRINT_VHT_CAPA(6, "short GI (160/80+80 MHz)");
634 PRINT_VHT_CAPA(7, "TX STBC");
636 PRINT_VHT_CAPA(11, "SU Beamformer");
637 PRINT_VHT_CAPA(12, "SU Beamformee");
638 /* compressed steering */
639 /* # of sounding dimensions */
640 PRINT_VHT_CAPA(19, "MU Beamformer");
641 PRINT_VHT_CAPA(20, "MU Beamformee");
642 PRINT_VHT_CAPA(21, "VHT TXOP PS");
643 PRINT_VHT_CAPA(22, "+HTC-VHT");
645 /* VHT link adaptation */
646 PRINT_VHT_CAPA(29, "RX antenna pattern consistency");
647 PRINT_VHT_CAPA(30, "TX antenna pattern consistency");
649 printf("\t\tVHT RX MCS set:\n");
650 tmp
= mcs
[0] | (mcs
[1] << 8);
651 for (i
= 1; i
<= 8; i
++) {
652 printf("\t\t\t%d streams: ", i
);
653 switch ((tmp
>> ((i
-1)*2) ) & 3) {
654 case 0: printf("MCS 0-7\n"); break;
655 case 1: printf("MCS 0-8\n"); break;
656 case 2: printf("MCS 0-9\n"); break;
657 case 3: printf("not supported\n"); break;
660 tmp
= mcs
[2] | (mcs
[3] << 8);
661 printf("\t\tVHT RX highest supported: %d Mbps\n", tmp
& 0x1fff);
663 printf("\t\tVHT TX MCS set:\n");
664 tmp
= mcs
[4] | (mcs
[5] << 8);
665 for (i
= 1; i
<= 8; i
++) {
666 printf("\t\t\t%d streams: ", i
);
667 switch ((tmp
>> ((i
-1)*2) ) & 3) {
668 case 0: printf("MCS 0-7\n"); break;
669 case 1: printf("MCS 0-8\n"); break;
670 case 2: printf("MCS 0-9\n"); break;
671 case 3: printf("not supported\n"); break;
674 tmp
= mcs
[6] | (mcs
[7] << 8);
675 printf("\t\tVHT TX highest supported: %d Mbps\n", tmp
& 0x1fff);