7 #include <netlink/genl/genl.h>
8 #include <netlink/genl/family.h>
9 #include <netlink/genl/ctrl.h>
10 #include <netlink/msg.h>
11 #include <netlink/attr.h>
16 #define WLAN_CAPABILITY_ESS (1<<0)
17 #define WLAN_CAPABILITY_IBSS (1<<1)
18 #define WLAN_CAPABILITY_CF_POLLABLE (1<<2)
19 #define WLAN_CAPABILITY_CF_POLL_REQUEST (1<<3)
20 #define WLAN_CAPABILITY_PRIVACY (1<<4)
21 #define WLAN_CAPABILITY_SHORT_PREAMBLE (1<<5)
22 #define WLAN_CAPABILITY_PBCC (1<<6)
23 #define WLAN_CAPABILITY_CHANNEL_AGILITY (1<<7)
24 #define WLAN_CAPABILITY_SPECTRUM_MGMT (1<<8)
25 #define WLAN_CAPABILITY_QOS (1<<9)
26 #define WLAN_CAPABILITY_SHORT_SLOT_TIME (1<<10)
27 #define WLAN_CAPABILITY_APSD (1<<11)
28 #define WLAN_CAPABILITY_DSSS_OFDM (1<<13)
30 static unsigned char ms_oui
[3] = { 0x00, 0x50, 0xf2 };
31 static unsigned char ieee80211_oui
[3] = { 0x00, 0x0f, 0xac };
32 static unsigned char wfa_oui
[3] = { 0x50, 0x6f, 0x9a };
36 enum print_ie_type type
;
37 bool show_both_ie_sets
;
40 #define IEEE80211_COUNTRY_EXTENSION_ID 201
42 union ieee80211_country_ie_triplet
{
47 } __attribute__ ((packed
)) chans
;
49 __u8 reg_extension_id
;
52 } __attribute__ ((packed
)) ext
;
53 } __attribute__ ((packed
));
55 static int handle_scan(struct nl80211_state
*state
,
58 int argc
, char **argv
,
61 struct nl_msg
*ssids
= NULL
, *freqs
= NULL
;
73 bool passive
= false, have_ssids
= false, have_freqs
= false;
78 ssids
= nlmsg_alloc();
82 freqs
= nlmsg_alloc();
88 for (i
= 0; i
< argc
; i
++) {
91 if (strcmp(argv
[i
], "freq") == 0) {
95 } else if (strcmp(argv
[i
], "ies") == 0) {
98 } else if (strcmp(argv
[i
], "lowpri") == 0) {
100 flags
|= NL80211_SCAN_FLAG_LOW_PRIORITY
;
102 } else if (strcmp(argv
[i
], "flush") == 0) {
104 flags
|= NL80211_SCAN_FLAG_FLUSH
;
106 } else if (strcmp(argv
[i
], "ap-force") == 0) {
108 flags
|= NL80211_SCAN_FLAG_AP
;
110 } else if (strcmp(argv
[i
], "ssid") == 0) {
114 } else if (strcmp(argv
[i
], "passive") == 0) {
122 freq
= strtoul(argv
[i
], &eptr
, 10);
123 if (eptr
!= argv
[i
] + strlen(argv
[i
])) {
124 /* failed to parse as number -- maybe a tag? */
129 NLA_PUT_U32(freqs
, i
, freq
);
132 ies
= parse_hex(argv
[i
], &tmp
);
134 goto nla_put_failure
;
135 NLA_PUT(msg
, NL80211_ATTR_IE
, tmp
, ies
);
140 NLA_PUT(ssids
, i
, strlen(argv
[i
]), argv
[i
]);
146 NLA_PUT(ssids
, 1, 0, "");
148 nla_put_nested(msg
, NL80211_ATTR_SCAN_SSIDS
, ssids
);
151 nla_put_nested(msg
, NL80211_ATTR_SCAN_FREQUENCIES
, freqs
);
153 NLA_PUT_U32(msg
, NL80211_ATTR_SCAN_FLAGS
, flags
);
162 static void tab_on_first(bool *first
)
170 static void print_ssid(const uint8_t type
, uint8_t len
, const uint8_t *data
)
173 print_ssid_escaped(len
, data
);
177 #define BSS_MEMBERSHIP_SELECTOR_HT_PHY 127
179 static void print_supprates(const uint8_t type
, uint8_t len
, const uint8_t *data
)
185 for (i
= 0; i
< len
; i
++) {
186 int r
= data
[i
] & 0x7f;
188 if (r
== BSS_MEMBERSHIP_SELECTOR_HT_PHY
&& data
[i
] & 0x80)
191 printf("%d.%d", r
/2, 5*(r
&1));
193 printf("%s ", data
[i
] & 0x80 ? "*" : "");
198 static void print_ds(const uint8_t type
, uint8_t len
, const uint8_t *data
)
200 printf(" channel %d\n", data
[0]);
203 static const char *country_env_str(char environment
)
205 switch (environment
) {
207 return "Indoor only";
209 return "Outdoor only";
211 return "Indoor/Outdoor";
217 static void print_country(const uint8_t type
, uint8_t len
, const uint8_t *data
)
219 printf(" %.*s", 2, data
);
221 printf("\tEnvironment: %s\n", country_env_str(data
[2]));
227 printf("\t\tNo country IE triplets present\n");
233 union ieee80211_country_ie_triplet
*triplet
= (void *) data
;
235 if (triplet
->ext
.reg_extension_id
>= IEEE80211_COUNTRY_EXTENSION_ID
) {
236 printf("\t\tExtension ID: %d Regulatory Class: %d Coverage class: %d (up to %dm)\n",
237 triplet
->ext
.reg_extension_id
,
238 triplet
->ext
.reg_class
,
239 triplet
->ext
.coverage_class
,
240 triplet
->ext
.coverage_class
* 450);
248 if (triplet
->chans
.first_channel
<= 14)
249 end_channel
= triplet
->chans
.first_channel
+ (triplet
->chans
.num_channels
- 1);
251 end_channel
= triplet
->chans
.first_channel
+ (4 * (triplet
->chans
.num_channels
- 1));
253 printf("\t\tChannels [%d - %d] @ %d dBm\n", triplet
->chans
.first_channel
, end_channel
, triplet
->chans
.max_power
);
262 static void print_powerconstraint(const uint8_t type
, uint8_t len
, const uint8_t *data
)
264 printf(" %d dB\n", data
[0]);
267 static void print_erp(const uint8_t type
, uint8_t len
, const uint8_t *data
)
270 printf(" <no flags>");
272 printf(" NonERP_Present");
274 printf(" Use_Protection");
276 printf(" Barker_Preamble_Mode");
280 static void print_cipher(const uint8_t *data
)
282 if (memcmp(data
, ms_oui
, 3) == 0) {
285 printf("Use group cipher suite");
300 printf("%.02x-%.02x-%.02x:%d",
301 data
[0], data
[1] ,data
[2], data
[3]);
304 } else if (memcmp(data
, ieee80211_oui
, 3) == 0) {
307 printf("Use group cipher suite");
322 printf("AES-128-CMAC");
328 printf("%.02x-%.02x-%.02x:%d",
329 data
[0], data
[1] ,data
[2], data
[3]);
333 printf("%.02x-%.02x-%.02x:%d",
334 data
[0], data
[1] ,data
[2], data
[3]);
337 static void print_auth(const uint8_t *data
)
339 if (memcmp(data
, ms_oui
, 3) == 0) {
342 printf("IEEE 802.1X");
348 printf("%.02x-%.02x-%.02x:%d",
349 data
[0], data
[1] ,data
[2], data
[3]);
352 } else if (memcmp(data
, ieee80211_oui
, 3) == 0) {
355 printf("IEEE 802.1X");
361 printf("FT/IEEE 802.1X");
367 printf("IEEE 802.1X/SHA-256");
370 printf("PSK/SHA-256");
373 printf("%.02x-%.02x-%.02x:%d",
374 data
[0], data
[1] ,data
[2], data
[3]);
378 printf("%.02x-%.02x-%.02x:%d",
379 data
[0], data
[1] ,data
[2], data
[3]);
382 static void print_rsn_ie(const char *defcipher
, const char *defauth
,
383 uint8_t len
, const uint8_t *data
)
386 __u16 version
, count
, capa
;
389 version
= data
[0] + (data
[1] << 8);
390 tab_on_first(&first
);
391 printf("\t * Version: %d\n", version
);
397 tab_on_first(&first
);
398 printf("\t * Group cipher: %s\n", defcipher
);
399 printf("\t * Pairwise ciphers: %s\n", defcipher
);
403 tab_on_first(&first
);
404 printf("\t * Group cipher: ");
412 tab_on_first(&first
);
413 printf("\t * Pairwise ciphers: %s\n", defcipher
);
417 count
= data
[0] | (data
[1] << 8);
418 if (2 + (count
* 4) > len
)
421 tab_on_first(&first
);
422 printf("\t * Pairwise ciphers:");
423 for (i
= 0; i
< count
; i
++) {
425 print_cipher(data
+ 2 + (i
* 4));
429 data
+= 2 + (count
* 4);
430 len
-= 2 + (count
* 4);
433 tab_on_first(&first
);
434 printf("\t * Authentication suites: %s\n", defauth
);
438 count
= data
[0] | (data
[1] << 8);
439 if (2 + (count
* 4) > len
)
442 tab_on_first(&first
);
443 printf("\t * Authentication suites:");
444 for (i
= 0; i
< count
; i
++) {
446 print_auth(data
+ 2 + (i
* 4));
450 data
+= 2 + (count
* 4);
451 len
-= 2 + (count
* 4);
454 capa
= data
[0] | (data
[1] << 8);
455 tab_on_first(&first
);
456 printf("\t * Capabilities:");
460 printf(" NoPairwise");
461 switch ((capa
& 0x000c) >> 2) {
465 printf(" 2-PTKSA-RC");
468 printf(" 4-PTKSA-RC");
471 printf(" 16-PTKSA-RC");
474 switch ((capa
& 0x0030) >> 4) {
478 printf(" 2-GTKSA-RC");
481 printf(" 4-GTKSA-RC");
484 printf(" 16-GTKSA-RC");
488 printf(" MFP-required");
490 printf(" MFP-capable");
492 printf(" Peerkey-enabled");
494 printf(" SPP-AMSDU-capable");
496 printf(" SPP-AMSDU-required");
497 printf(" (0x%.4x)\n", capa
);
503 int pmkid_count
= data
[0] | (data
[1] << 8);
505 if (len
>= 2 + 16 * pmkid_count
) {
506 tab_on_first(&first
);
507 printf("\t * %d PMKIDs\n", pmkid_count
);
508 /* not printing PMKID values */
509 data
+= 2 + 16 * pmkid_count
;
510 len
-= 2 + 16 * pmkid_count
;
516 tab_on_first(&first
);
517 printf("\t * Group mgmt cipher suite: ");
526 printf("\t\t * bogus tail data (%d):", len
);
528 printf(" %.2x", *data
);
536 static void print_rsn(const uint8_t type
, uint8_t len
, const uint8_t *data
)
538 print_rsn_ie("CCMP", "IEEE 802.1X", len
, data
);
541 static void print_ht_capa(const uint8_t type
, uint8_t len
, const uint8_t *data
)
544 print_ht_capability(data
[0] | (data
[1] << 8));
545 print_ampdu_length(data
[2] & 3);
546 print_ampdu_spacing((data
[2] >> 2) & 7);
547 print_ht_mcs(data
+ 3);
550 static void print_ht_op(const uint8_t type
, uint8_t len
, const uint8_t *data
)
552 static const char *offset
[4] = {
558 static const char *protection
[4] = {
564 static const char *sta_chan_width
[2] = {
570 printf("\t\t * primary channel: %d\n", data
[0]);
571 printf("\t\t * secondary channel offset: %s\n",
572 offset
[data
[1] & 0x3]);
573 printf("\t\t * STA channel width: %s\n", sta_chan_width
[(data
[1] & 0x4)>>2]);
574 printf("\t\t * RIFS: %d\n", (data
[1] & 0x8)>>3);
575 printf("\t\t * HT protection: %s\n", protection
[data
[2] & 0x3]);
576 printf("\t\t * non-GF present: %d\n", (data
[2] & 0x4) >> 2);
577 printf("\t\t * OBSS non-GF present: %d\n", (data
[2] & 0x10) >> 4);
578 printf("\t\t * dual beacon: %d\n", (data
[4] & 0x40) >> 6);
579 printf("\t\t * dual CTS protection: %d\n", (data
[4] & 0x80) >> 7);
580 printf("\t\t * STBC beacon: %d\n", data
[5] & 0x1);
581 printf("\t\t * L-SIG TXOP Prot: %d\n", (data
[5] & 0x2) >> 1);
582 printf("\t\t * PCO active: %d\n", (data
[5] & 0x4) >> 2);
583 printf("\t\t * PCO phase: %d\n", (data
[5] & 0x8) >> 3);
586 static void print_capabilities(const uint8_t type
, uint8_t len
, const uint8_t *data
)
592 for (i
= 0; i
< len
; i
++) {
595 for (bit
= 0; bit
< 8; bit
++) {
596 if (!(data
[i
] & (1 << bit
)))
604 #define CAPA(bit, name) case bit: printf(" " name); break
606 switch (bit
+ base
) {
607 CAPA(0, "HT Information Exchange Supported");
608 CAPA(1, "reserved (On-demand Beacon)");
609 CAPA(2, "Extended Channel Switching");
610 CAPA(3, "reserved (Wave Indication)");
611 CAPA(4, "PSMP Capability");
612 CAPA(5, "reserved (Service Interval Granularity)");
613 CAPA(6, "S-PSMP Capability");
615 CAPA(8, "Diagnostics");
616 CAPA(9, "Multicast Diagnostics");
617 CAPA(10, "Location Tracking");
619 CAPA(12, "Proxy ARP Service");
620 CAPA(13, "Collocated Interference Reporting");
621 CAPA(14, "Civic Location");
622 CAPA(15, "Geospatial Location");
624 CAPA(17, "WNM-Sleep Mode");
625 CAPA(18, "TIM Broadcast");
626 CAPA(19, "BSS Transition");
627 CAPA(20, "QoS Traffic Capability");
628 CAPA(21, "AC Station Count");
629 CAPA(22, "Multiple BSSID");
630 CAPA(23, "Timing Measurement");
631 CAPA(24, "Channel Usage");
632 CAPA(25, "SSID List");
634 CAPA(27, "UTC TSF Offset");
635 CAPA(28, "TDLS Peer U-APSD Buffer STA Support");
636 CAPA(29, "TDLS Peer PSM Support");
637 CAPA(30, "TDLS channel switching");
638 CAPA(31, "Interworking");
641 CAPA(34, "SSPN Interface");
642 CAPA(35, "Reserved");
643 CAPA(36, "MSGCF Capability");
644 CAPA(37, "TDLS Support");
645 CAPA(38, "TDLS Prohibited");
646 CAPA(39, "TDLS Channel Switching Prohibited");
647 CAPA(40, "Reject Unadmitted Frame");
648 CAPA(44, "Identifier Location");
649 CAPA(45, "U-APSD Coexistence");
650 CAPA(46, "WNM-Notification");
651 CAPA(47, "Reserved");
652 CAPA(48, "UTF-8 SSID");
664 static void print_tim(const uint8_t type
, uint8_t len
, const uint8_t *data
)
666 printf(" DTIM Count %u DTIM Period %u Bitmap Control 0x%x "
668 data
[0], data
[1], data
[2], data
[3]);
670 printf(" (+ %u octet%s)", len
- 4, len
- 4 == 1 ? "" : "s");
674 static void print_vht_capa(const uint8_t type
, uint8_t len
, const uint8_t *data
)
677 print_vht_info(data
[0] | (data
[1] << 8) |
678 (data
[2] << 16) | (data
[3] << 24),
684 void (*print
)(const uint8_t type
, uint8_t len
, const uint8_t *data
);
685 uint8_t minlen
, maxlen
;
689 static void print_ie(const struct ie_print
*p
, const uint8_t type
,
690 uint8_t len
, const uint8_t *data
)
697 printf("\t%s:", p
->name
);
698 if (len
< p
->minlen
|| len
> p
->maxlen
) {
700 printf(" <invalid: %d bytes:", len
);
701 for (i
= 0; i
< len
; i
++)
702 printf(" %.02x", data
[i
]);
705 printf(" <invalid: 1 byte: %.02x>\n", data
[0]);
707 printf(" <invalid: no data>\n");
711 p
->print(type
, len
, data
);
714 #define PRINT_IGN { \
721 static const struct ie_print ieprinters
[] = {
722 [0] = { "SSID", print_ssid
, 0, 32, BIT(PRINT_SCAN
) | BIT(PRINT_LINK
), },
723 [1] = { "Supported rates", print_supprates
, 0, 255, BIT(PRINT_SCAN
), },
724 [3] = { "DS Parameter set", print_ds
, 1, 1, BIT(PRINT_SCAN
), },
725 [5] = { "TIM", print_tim
, 4, 255, BIT(PRINT_SCAN
), },
726 [7] = { "Country", print_country
, 3, 255, BIT(PRINT_SCAN
), },
727 [32] = { "Power constraint", print_powerconstraint
, 1, 1, BIT(PRINT_SCAN
), },
728 [42] = { "ERP", print_erp
, 1, 255, BIT(PRINT_SCAN
), },
729 [45] = { "HT capabilities", print_ht_capa
, 26, 26, BIT(PRINT_SCAN
), },
730 [61] = { "HT operation", print_ht_op
, 22, 22, BIT(PRINT_SCAN
), },
731 [191] = { "VHT capabilities", print_vht_capa
, 12, 255, BIT(PRINT_SCAN
), },
732 [48] = { "RSN", print_rsn
, 2, 255, BIT(PRINT_SCAN
), },
733 [50] = { "Extended supported rates", print_supprates
, 0, 255, BIT(PRINT_SCAN
), },
734 [114] = { "MESH ID", print_ssid
, 0, 32, BIT(PRINT_SCAN
) | BIT(PRINT_LINK
), },
735 [127] = { "Extended capabilities", print_capabilities
, 0, 255, BIT(PRINT_SCAN
), },
738 static void print_wifi_wpa(const uint8_t type
, uint8_t len
, const uint8_t *data
)
740 print_rsn_ie("TKIP", "IEEE 802.1X", len
, data
);
743 static bool print_wifi_wmm_param(const uint8_t *data
, uint8_t len
)
746 static const char *aci_tbl
[] = { "BE", "BK", "VI", "VO" };
752 printf("Parameter: not version 1: ");
756 printf("\t * Parameter version 1");
761 printf("\n\t\t * u-APSD");
765 for (i
= 0; i
< 4; i
++) {
766 printf("\n\t\t * %s:", aci_tbl
[(data
[0] >> 5) & 3]);
769 printf(" CW %d-%d", (1 << (data
[1] & 0xf)) - 1,
770 (1 << (data
[1] >> 4)) - 1);
771 printf(", AIFSN %d", data
[0] & 0xf);
772 if (data
[2] | data
[3])
773 printf(", TXOP %d usec", (data
[2] + (data
[3] << 8)) * 32);
785 static void print_wifi_wmm(const uint8_t type
, uint8_t len
, const uint8_t *data
)
791 printf(" information:");
794 if (print_wifi_wmm_param(data
+ 1, len
- 1))
798 printf(" type %d:", data
[0]);
802 for(i
= 1; i
< len
; i
++)
803 printf(" %.02x", data
[i
]);
807 static const char * wifi_wps_dev_passwd_id(uint16_t id
)
811 return "Default (PIN)";
813 return "User-specified";
815 return "Machine-specified";
821 return "Registrar-specified";
827 static void print_wifi_wps(const uint8_t type
, uint8_t len
, const uint8_t *data
)
830 __u16 subtype
, sublen
;
833 subtype
= (data
[0] << 8) + data
[1];
834 sublen
= (data
[2] << 8) + data
[3];
840 tab_on_first(&first
);
841 printf("\t * Version: %d.%d\n", data
[4] >> 4, data
[4] & 0xF);
844 tab_on_first(&first
);
845 printf("\t * Device name: %.*s\n", sublen
, data
+ 4);
849 tab_on_first(&first
);
851 printf("\t * Device Password ID: (invalid "
852 "length %d)\n", sublen
);
855 id
= data
[4] << 8 | data
[5];
856 printf("\t * Device Password ID: %u (%s)\n",
857 id
, wifi_wps_dev_passwd_id(id
));
861 tab_on_first(&first
);
862 printf("\t * Manufacturer: %.*s\n", sublen
, data
+ 4);
865 tab_on_first(&first
);
866 printf("\t * Model: %.*s\n", sublen
, data
+ 4);
869 tab_on_first(&first
);
870 printf("\t * Model Number: %.*s\n", sublen
, data
+ 4);
874 tab_on_first(&first
);
875 printf("\t * Response Type: %d%s\n",
876 val
, val
== 3 ? " (AP)" : "");
881 tab_on_first(&first
);
882 printf("\t * RF Bands: 0x%x\n", val
);
887 tab_on_first(&first
);
888 printf("\t * Selected Registrar: 0x%x\n", val
);
892 tab_on_first(&first
);
893 printf("\t * Serial Number: %.*s\n", sublen
, data
+ 4);
897 tab_on_first(&first
);
898 printf("\t * Wi-Fi Protected Setup State: %d%s%s\n",
900 val
== 1 ? " (Unconfigured)" : "",
901 val
== 2 ? " (Configured)" : "");
905 tab_on_first(&first
);
906 printf("\t * UUID: ");
908 printf("(invalid, length=%d)\n", sublen
);
911 printf("%02x%02x%02x%02x-%02x%02x-%02x%02x-"
912 "%02x%02x-%02x%02x%02x%02x%02x%02x\n",
913 data
[4], data
[5], data
[6], data
[7],
914 data
[8], data
[9], data
[10], data
[11],
915 data
[12], data
[13], data
[14], data
[15],
916 data
[16], data
[17], data
[18], data
[19]);
919 tab_on_first(&first
);
921 printf("\t * Primary Device Type: (invalid "
922 "length %d)\n", sublen
);
925 printf("\t * Primary Device Type: "
926 "%u-%02x%02x%02x%02x-%u\n",
927 data
[4] << 8 | data
[5],
928 data
[6], data
[7], data
[8], data
[9],
929 data
[10] << 8 | data
[11]);
934 tab_on_first(&first
);
935 printf("\t * AP setup locked: 0x%.2x\n", val
);
940 __u16 meth
= (data
[4] << 8) + data
[5];
942 tab_on_first(&first
);
943 printf("\t * %sConfig methods:",
944 subtype
== 0x1053 ? "Selected Registrar ": "");
945 #define T(bit, name) do { \
946 if (meth & (1<<bit)) { \
966 const __u8
*subdata
= data
+ 4;
967 __u16 tmplen
= sublen
;
969 tab_on_first(&first
);
970 printf("\t * Unknown TLV (%#.4x, %d bytes):",
973 printf(" %.2x", *subdata
);
987 printf("\t\t * bogus tail data (%d):", len
);
989 printf(" %.2x", *data
);
997 static const struct ie_print wifiprinters
[] = {
998 [1] = { "WPA", print_wifi_wpa
, 2, 255, BIT(PRINT_SCAN
), },
999 [2] = { "WMM", print_wifi_wmm
, 1, 255, BIT(PRINT_SCAN
), },
1000 [4] = { "WPS", print_wifi_wps
, 0, 255, BIT(PRINT_SCAN
), },
1003 static inline void print_p2p(const uint8_t type
, uint8_t len
, const uint8_t *data
)
1011 sublen
= (data
[2] << 8) + data
[1];
1013 if (sublen
> len
- 3)
1017 case 0x02: /* capability */
1018 tab_on_first(&first
);
1020 printf("\t * malformed capability\n");
1023 printf("\t * Group capa: 0x%.2x, Device capa: 0x%.2x\n",
1026 case 0x0d: /* device info */
1027 if (sublen
< 6 + 2 + 8 + 1) {
1028 printf("\t * malformed device info\n");
1031 /* fall through for now */
1032 case 0x00: /* status */
1033 case 0x01: /* minor reason */
1034 case 0x03: /* device ID */
1035 case 0x04: /* GO intent */
1036 case 0x05: /* configuration timeout */
1037 case 0x06: /* listen channel */
1038 case 0x07: /* group BSSID */
1039 case 0x08: /* ext listen timing */
1040 case 0x09: /* intended interface address */
1041 case 0x0a: /* manageability */
1042 case 0x0b: /* channel list */
1043 case 0x0c: /* NoA */
1044 case 0x0e: /* group info */
1045 case 0x0f: /* group ID */
1046 case 0x10: /* interface */
1047 case 0x11: /* operating channel */
1048 case 0x12: /* invitation flags */
1049 case 0xdd: /* vendor specific */
1051 const __u8
*subdata
= data
+ 4;
1052 __u16 tmplen
= sublen
;
1054 tab_on_first(&first
);
1055 printf("\t * Unknown TLV (%#.2x, %d bytes):",
1058 printf(" %.2x", *subdata
);
1072 tab_on_first(&first
);
1073 printf("\t * bogus tail data (%d):", len
);
1075 printf(" %.2x", *data
);
1083 static const struct ie_print wfa_printers
[] = {
1084 [9] = { "P2P", print_p2p
, 2, 255, BIT(PRINT_SCAN
), },
1087 static void print_vendor(unsigned char len
, unsigned char *data
,
1088 bool unknown
, enum print_ie_type ptype
)
1093 printf("\tVendor specific: <too short> data:");
1094 for(i
= 0; i
< len
; i
++)
1095 printf(" %.02x", data
[i
]);
1100 if (len
>= 4 && memcmp(data
, ms_oui
, 3) == 0) {
1101 if (data
[3] < ARRAY_SIZE(wifiprinters
) &&
1102 wifiprinters
[data
[3]].name
&&
1103 wifiprinters
[data
[3]].flags
& BIT(ptype
)) {
1104 print_ie(&wifiprinters
[data
[3]], data
[3], len
- 4, data
+ 4);
1109 printf("\tMS/WiFi %#.2x, data:", data
[3]);
1110 for(i
= 0; i
< len
- 4; i
++)
1111 printf(" %.02x", data
[i
+ 4]);
1116 if (len
>= 4 && memcmp(data
, wfa_oui
, 3) == 0) {
1117 if (data
[3] < ARRAY_SIZE(wfa_printers
) &&
1118 wfa_printers
[data
[3]].name
&&
1119 wfa_printers
[data
[3]].flags
& BIT(ptype
)) {
1120 print_ie(&wfa_printers
[data
[3]], data
[3], len
- 4, data
+ 4);
1125 printf("\tWFA %#.2x, data:", data
[3]);
1126 for(i
= 0; i
< len
- 4; i
++)
1127 printf(" %.02x", data
[i
+ 4]);
1135 printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:",
1136 data
[0], data
[1], data
[2]);
1137 for (i
= 3; i
< len
; i
++)
1138 printf(" %.2x", data
[i
]);
1142 void print_ies(unsigned char *ie
, int ielen
, bool unknown
,
1143 enum print_ie_type ptype
)
1145 while (ielen
>= 2 && ielen
>= ie
[1]) {
1146 if (ie
[0] < ARRAY_SIZE(ieprinters
) &&
1147 ieprinters
[ie
[0]].name
&&
1148 ieprinters
[ie
[0]].flags
& BIT(ptype
)) {
1149 print_ie(&ieprinters
[ie
[0]], ie
[0], ie
[1], ie
+ 2);
1150 } else if (ie
[0] == 221 /* vendor */) {
1151 print_vendor(ie
[1], ie
+ 2, unknown
, ptype
);
1152 } else if (unknown
) {
1155 printf("\tUnknown IE (%d):", ie
[0]);
1156 for (i
=0; i
<ie
[1]; i
++)
1157 printf(" %.2x", ie
[2+i
]);
1165 static int print_bss_handler(struct nl_msg
*msg
, void *arg
)
1167 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
1168 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
1169 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
1170 char mac_addr
[20], dev
[20];
1171 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1172 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
1173 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
1174 [NL80211_BSS_BSSID
] = { },
1175 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
1176 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
1177 [NL80211_BSS_INFORMATION_ELEMENTS
] = { },
1178 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
1179 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
1180 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1181 [NL80211_BSS_SEEN_MS_AGO
] = { .type
= NLA_U32
},
1182 [NL80211_BSS_BEACON_IES
] = { },
1184 struct scan_params
*params
= arg
;
1185 int show
= params
->show_both_ie_sets
? 2 : 1;
1187 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
1188 genlmsg_attrlen(gnlh
, 0), NULL
);
1190 if (!tb
[NL80211_ATTR_BSS
]) {
1191 fprintf(stderr
, "bss info missing!\n");
1194 if (nla_parse_nested(bss
, NL80211_BSS_MAX
,
1195 tb
[NL80211_ATTR_BSS
],
1197 fprintf(stderr
, "failed to parse nested attributes!\n");
1201 if (!bss
[NL80211_BSS_BSSID
])
1204 mac_addr_n2a(mac_addr
, nla_data(bss
[NL80211_BSS_BSSID
]));
1205 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), dev
);
1206 printf("BSS %s (on %s)", mac_addr
, dev
);
1208 if (bss
[NL80211_BSS_STATUS
]) {
1209 switch (nla_get_u32(bss
[NL80211_BSS_STATUS
])) {
1210 case NL80211_BSS_STATUS_AUTHENTICATED
:
1211 printf(" -- authenticated");
1213 case NL80211_BSS_STATUS_ASSOCIATED
:
1214 printf(" -- associated");
1216 case NL80211_BSS_STATUS_IBSS_JOINED
:
1217 printf(" -- joined");
1220 printf(" -- unknown status: %d",
1221 nla_get_u32(bss
[NL80211_BSS_STATUS
]));
1227 if (bss
[NL80211_BSS_TSF
]) {
1228 unsigned long long tsf
;
1229 tsf
= (unsigned long long)nla_get_u64(bss
[NL80211_BSS_TSF
]);
1230 printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n",
1231 tsf
, tsf
/1000/1000/60/60/24, (tsf
/1000/1000/60/60) % 24,
1232 (tsf
/1000/1000/60) % 60, (tsf
/1000/1000) % 60);
1234 if (bss
[NL80211_BSS_FREQUENCY
])
1235 printf("\tfreq: %d\n",
1236 nla_get_u32(bss
[NL80211_BSS_FREQUENCY
]));
1237 if (bss
[NL80211_BSS_BEACON_INTERVAL
])
1238 printf("\tbeacon interval: %d\n",
1239 nla_get_u16(bss
[NL80211_BSS_BEACON_INTERVAL
]));
1240 if (bss
[NL80211_BSS_CAPABILITY
]) {
1241 __u16 capa
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
1242 printf("\tcapability:");
1243 if (capa
& WLAN_CAPABILITY_ESS
)
1245 if (capa
& WLAN_CAPABILITY_IBSS
)
1247 if (capa
& WLAN_CAPABILITY_PRIVACY
)
1249 if (capa
& WLAN_CAPABILITY_SHORT_PREAMBLE
)
1250 printf(" ShortPreamble");
1251 if (capa
& WLAN_CAPABILITY_PBCC
)
1253 if (capa
& WLAN_CAPABILITY_CHANNEL_AGILITY
)
1254 printf(" ChannelAgility");
1255 if (capa
& WLAN_CAPABILITY_SPECTRUM_MGMT
)
1256 printf(" SpectrumMgmt");
1257 if (capa
& WLAN_CAPABILITY_QOS
)
1259 if (capa
& WLAN_CAPABILITY_SHORT_SLOT_TIME
)
1260 printf(" ShortSlotTime");
1261 if (capa
& WLAN_CAPABILITY_APSD
)
1263 if (capa
& WLAN_CAPABILITY_DSSS_OFDM
)
1264 printf(" DSSS-OFDM");
1265 printf(" (0x%.4x)\n", capa
);
1267 if (bss
[NL80211_BSS_SIGNAL_MBM
]) {
1268 int s
= nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]);
1269 printf("\tsignal: %d.%.2d dBm\n", s
/100, s
%100);
1271 if (bss
[NL80211_BSS_SIGNAL_UNSPEC
]) {
1272 unsigned char s
= nla_get_u8(bss
[NL80211_BSS_SIGNAL_UNSPEC
]);
1273 printf("\tsignal: %d/100\n", s
);
1275 if (bss
[NL80211_BSS_SEEN_MS_AGO
]) {
1276 int age
= nla_get_u32(bss
[NL80211_BSS_SEEN_MS_AGO
]);
1277 printf("\tlast seen: %d ms ago\n", age
);
1280 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
] && show
--) {
1281 if (bss
[NL80211_BSS_BEACON_IES
])
1282 printf("\tInformation elements from Probe Response "
1284 print_ies(nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]),
1285 nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]),
1286 params
->unknown
, params
->type
);
1288 if (bss
[NL80211_BSS_BEACON_IES
] && show
--) {
1289 printf("\tInformation elements from Beacon frame:\n");
1290 print_ies(nla_data(bss
[NL80211_BSS_BEACON_IES
]),
1291 nla_len(bss
[NL80211_BSS_BEACON_IES
]),
1292 params
->unknown
, params
->type
);
1298 static struct scan_params scan_params
;
1300 static int handle_scan_dump(struct nl80211_state
*state
,
1303 int argc
, char **argv
,
1309 memset(&scan_params
, 0, sizeof(scan_params
));
1311 if (argc
== 1 && !strcmp(argv
[0], "-u"))
1312 scan_params
.unknown
= true;
1313 else if (argc
== 1 && !strcmp(argv
[0], "-b"))
1314 scan_params
.show_both_ie_sets
= true;
1316 scan_params
.type
= PRINT_SCAN
;
1318 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_bss_handler
,
1323 static int handle_scan_combined(struct nl80211_state
*state
,
1326 int argc
, char **argv
,
1330 static char *dump_argv
[] = {
1336 static const __u32 cmds
[] = {
1337 NL80211_CMD_NEW_SCAN_RESULTS
,
1338 NL80211_CMD_SCAN_ABORTED
,
1340 int trig_argc
, dump_argc
, err
;
1342 if (argc
>= 3 && !strcmp(argv
[2], "-u")) {
1344 dump_argv
[3] = "-u";
1345 } else if (argc
>= 3 && !strcmp(argv
[2], "-b")) {
1347 dump_argv
[3] = "-b";
1351 trig_argc
= 3 + (argc
- 2) + (3 - dump_argc
);
1352 trig_argv
= calloc(trig_argc
, sizeof(*trig_argv
));
1355 trig_argv
[0] = argv
[0];
1356 trig_argv
[1] = "scan";
1357 trig_argv
[2] = "trigger";
1359 for (i
= 0; i
< argc
- 2 - (dump_argc
- 3); i
++)
1360 trig_argv
[i
+ 3] = argv
[i
+ 2 + (dump_argc
- 3)];
1361 err
= handle_cmd(state
, id
, trig_argc
, trig_argv
);
1367 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
1369 * This code has a bug, which requires creating a separate
1370 * nl80211 socket to fix:
1371 * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or
1372 * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel
1373 * before (!) we listen to it, because we only start listening
1374 * after we send our scan request.
1376 * Doing it the other way around has a race condition as well,
1377 * if you first open the events socket you may get a notification
1378 * for a previous scan.
1380 * The only proper way to fix this would be to listen to events
1381 * before sending the command, and for the kernel to send the
1382 * scan request along with the event, so that you can match up
1383 * whether the scan you requested was finished or aborted (this
1384 * may result in processing a scan that another application
1385 * requested, but that doesn't seem to be a problem).
1387 * Alas, the kernel doesn't do that (yet).
1390 if (listen_events(state
, ARRAY_SIZE(cmds
), cmds
) ==
1391 NL80211_CMD_SCAN_ABORTED
) {
1392 printf("scan aborted!\n");
1396 dump_argv
[0] = argv
[0];
1397 return handle_cmd(state
, id
, dump_argc
, dump_argv
);
1399 TOPLEVEL(scan
, "[-u] [freq <freq>*] [ies <hex as 00:11:..>] [lowpri,flush,ap-force] [ssid <ssid>*|passive]", 0, 0,
1400 CIB_NETDEV
, handle_scan_combined
,
1401 "Scan on the given frequencies and probe for the given SSIDs\n"
1402 "(or wildcard if not given) unless passive scanning is requested.\n"
1403 "If -u is specified print unknown data in the scan results.\n"
1404 "Specified (vendor) IEs must be well-formed.");
1405 COMMAND(scan
, dump
, "[-u]",
1406 NL80211_CMD_GET_SCAN
, NLM_F_DUMP
, CIB_NETDEV
, handle_scan_dump
,
1407 "Dump the current scan results. If -u is specified, print unknown\n"
1408 "data in scan results.");
1409 COMMAND(scan
, trigger
, "[freq <freq>*] [ies <hex as 00:11:..>] [lowpri,flush,ap-force] [ssid <ssid>*|passive]",
1410 NL80211_CMD_TRIGGER_SCAN
, 0, CIB_NETDEV
, handle_scan
,
1411 "Trigger a scan on the given frequencies with probing for the given\n"
1412 "SSIDs (or wildcard if not given) unless passive scanning is requested.");