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
)
60 struct nl_msg
*ssids
= NULL
, *freqs
= NULL
;
72 bool passive
= false, have_ssids
= false, have_freqs
= false;
76 ssids
= nlmsg_alloc();
80 freqs
= nlmsg_alloc();
86 for (i
= 0; i
< argc
; i
++) {
89 if (strcmp(argv
[i
], "freq") == 0) {
93 } else if (strcmp(argv
[i
], "ies") == 0) {
96 } else if (strcmp(argv
[i
], "ssid") == 0) {
100 } else if (strcmp(argv
[i
], "passive") == 0) {
108 freq
= strtoul(argv
[i
], &eptr
, 10);
109 if (eptr
!= argv
[i
] + strlen(argv
[i
])) {
110 /* failed to parse as number -- maybe a tag? */
115 NLA_PUT_U32(freqs
, i
, freq
);
118 ies
= parse_hex(argv
[i
], &tmp
);
120 goto nla_put_failure
;
121 NLA_PUT(msg
, NL80211_ATTR_IE
, tmp
, ies
);
126 NLA_PUT(ssids
, i
, strlen(argv
[i
]), argv
[i
]);
132 NLA_PUT(ssids
, 1, 0, "");
134 nla_put_nested(msg
, NL80211_ATTR_SCAN_SSIDS
, ssids
);
137 nla_put_nested(msg
, NL80211_ATTR_SCAN_FREQUENCIES
, freqs
);
146 static void tab_on_first(bool *first
)
154 static void print_ssid(const uint8_t type
, uint8_t len
, const uint8_t *data
)
157 print_ssid_escaped(len
, data
);
161 #define BSS_MEMBERSHIP_SELECTOR_HT_PHY 127
163 static void print_supprates(const uint8_t type
, uint8_t len
, const uint8_t *data
)
169 for (i
= 0; i
< len
; i
++) {
170 int r
= data
[i
] & 0x7f;
172 if (r
== BSS_MEMBERSHIP_SELECTOR_HT_PHY
&& data
[i
] & 0x80)
175 printf("%d.%d", r
/2, 5*(r
&1));
177 printf("%s ", data
[i
] & 0x80 ? "*" : "");
182 static void print_ds(const uint8_t type
, uint8_t len
, const uint8_t *data
)
184 printf(" channel %d\n", data
[0]);
187 static const char *country_env_str(char environment
)
189 switch (environment
) {
191 return "Indoor only";
193 return "Outdoor only";
195 return "Indoor/Outdoor";
201 static void print_country(const uint8_t type
, uint8_t len
, const uint8_t *data
)
203 printf(" %.*s", 2, data
);
205 printf("\tEnvironment: %s\n", country_env_str(data
[2]));
211 printf("\t\tNo country IE triplets present\n");
217 union ieee80211_country_ie_triplet
*triplet
= (void *) data
;
219 if (triplet
->ext
.reg_extension_id
>= IEEE80211_COUNTRY_EXTENSION_ID
) {
220 printf("\t\tExtension ID: %d Regulatory Class: %d Coverage class: %d (up to %dm)\n",
221 triplet
->ext
.reg_extension_id
,
222 triplet
->ext
.reg_class
,
223 triplet
->ext
.coverage_class
,
224 triplet
->ext
.coverage_class
* 450);
232 if (triplet
->chans
.first_channel
<= 14)
233 end_channel
= triplet
->chans
.first_channel
+ (triplet
->chans
.num_channels
- 1);
235 end_channel
= triplet
->chans
.first_channel
+ (4 * (triplet
->chans
.num_channels
- 1));
237 printf("\t\tChannels [%d - %d] @ %d dBm\n", triplet
->chans
.first_channel
, end_channel
, triplet
->chans
.max_power
);
246 static void print_powerconstraint(const uint8_t type
, uint8_t len
, const uint8_t *data
)
248 printf(" %d dB\n", data
[0]);
251 static void print_erp(const uint8_t type
, uint8_t len
, const uint8_t *data
)
254 printf(" <no flags>");
256 printf(" NonERP_Present");
258 printf(" Use_Protection");
260 printf(" Barker_Preamble_Mode");
264 static void print_cipher(const uint8_t *data
)
266 if (memcmp(data
, ms_oui
, 3) == 0) {
269 printf("Use group cipher suite");
284 printf("%.02x-%.02x-%.02x:%d",
285 data
[0], data
[1] ,data
[2], data
[3]);
288 } else if (memcmp(data
, ieee80211_oui
, 3) == 0) {
291 printf("Use group cipher suite");
306 printf("AES-128-CMAC");
312 printf("%.02x-%.02x-%.02x:%d",
313 data
[0], data
[1] ,data
[2], data
[3]);
317 printf("%.02x-%.02x-%.02x:%d",
318 data
[0], data
[1] ,data
[2], data
[3]);
321 static void print_auth(const uint8_t *data
)
323 if (memcmp(data
, ms_oui
, 3) == 0) {
326 printf("IEEE 802.1X");
332 printf("%.02x-%.02x-%.02x:%d",
333 data
[0], data
[1] ,data
[2], data
[3]);
336 } else if (memcmp(data
, ieee80211_oui
, 3) == 0) {
339 printf("IEEE 802.1X");
345 printf("FT/IEEE 802.1X");
351 printf("IEEE 802.1X/SHA-256");
354 printf("PSK/SHA-256");
357 printf("%.02x-%.02x-%.02x:%d",
358 data
[0], data
[1] ,data
[2], data
[3]);
362 printf("%.02x-%.02x-%.02x:%d",
363 data
[0], data
[1] ,data
[2], data
[3]);
366 static void print_rsn_ie(const char *defcipher
, const char *defauth
,
367 uint8_t len
, const uint8_t *data
)
370 __u16 version
, count
, capa
;
373 version
= data
[0] + (data
[1] << 8);
374 tab_on_first(&first
);
375 printf("\t * Version: %d\n", version
);
381 tab_on_first(&first
);
382 printf("\t * Group cipher: %s\n", defcipher
);
383 printf("\t * Pairwise ciphers: %s\n", defcipher
);
387 tab_on_first(&first
);
388 printf("\t * Group cipher: ");
396 tab_on_first(&first
);
397 printf("\t * Pairwise ciphers: %s\n", defcipher
);
401 count
= data
[0] | (data
[1] << 8);
402 if (2 + (count
* 4) > len
)
405 tab_on_first(&first
);
406 printf("\t * Pairwise ciphers:");
407 for (i
= 0; i
< count
; i
++) {
409 print_cipher(data
+ 2 + (i
* 4));
413 data
+= 2 + (count
* 4);
414 len
-= 2 + (count
* 4);
417 tab_on_first(&first
);
418 printf("\t * Authentication suites: %s\n", defauth
);
422 count
= data
[0] | (data
[1] << 8);
423 if (2 + (count
* 4) > len
)
426 tab_on_first(&first
);
427 printf("\t * Authentication suites:");
428 for (i
= 0; i
< count
; i
++) {
430 print_auth(data
+ 2 + (i
* 4));
434 data
+= 2 + (count
* 4);
435 len
-= 2 + (count
* 4);
438 capa
= data
[0] | (data
[1] << 8);
439 tab_on_first(&first
);
440 printf("\t * Capabilities:");
444 printf(" NoPairwise");
445 switch ((capa
& 0x000c) >> 2) {
449 printf(" 2-PTKSA-RC");
452 printf(" 4-PTKSA-RC");
455 printf(" 16-PTKSA-RC");
458 switch ((capa
& 0x0030) >> 4) {
462 printf(" 2-GTKSA-RC");
465 printf(" 4-GTKSA-RC");
468 printf(" 16-GTKSA-RC");
472 printf(" MFP-required");
474 printf(" MFP-capable");
476 printf(" Peerkey-enabled");
478 printf(" SPP-AMSDU-capable");
480 printf(" SPP-AMSDU-required");
481 printf(" (0x%.4x)\n", capa
);
487 int pmkid_count
= data
[0] | (data
[1] << 8);
489 if (len
>= 2 + 16 * pmkid_count
) {
490 tab_on_first(&first
);
491 printf("\t * %d PMKIDs\n", pmkid_count
);
492 /* not printing PMKID values */
493 data
+= 2 + 16 * pmkid_count
;
494 len
-= 2 + 16 * pmkid_count
;
500 tab_on_first(&first
);
501 printf("\t * Group mgmt cipher suite: ");
510 printf("\t\t * bogus tail data (%d):", len
);
512 printf(" %.2x", *data
);
520 static void print_rsn(const uint8_t type
, uint8_t len
, const uint8_t *data
)
522 print_rsn_ie("CCMP", "IEEE 802.1X", len
, data
);
525 static void print_ht_capa(const uint8_t type
, uint8_t len
, const uint8_t *data
)
528 print_ht_capability(data
[0] | (data
[1] << 8));
529 print_ampdu_length(data
[2] & 3);
530 print_ampdu_spacing((data
[2] >> 2) & 7);
531 print_ht_mcs(data
+ 3);
534 static void print_ht_op(const uint8_t type
, uint8_t len
, const uint8_t *data
)
536 static const char *offset
[4] = {
542 static const char *protection
[4] = {
548 static const char *sta_chan_width
[2] = {
554 printf("\t\t * primary channel: %d\n", data
[0]);
555 printf("\t\t * secondary channel offset: %s\n",
556 offset
[data
[1] & 0x3]);
557 printf("\t\t * STA channel width: %s\n", sta_chan_width
[(data
[1] & 0x4)>>2]);
558 printf("\t\t * RIFS: %d\n", (data
[1] & 0x8)>>3);
559 printf("\t\t * HT protection: %s\n", protection
[data
[2] & 0x3]);
560 printf("\t\t * non-GF present: %d\n", (data
[2] & 0x4) >> 2);
561 printf("\t\t * OBSS non-GF present: %d\n", (data
[2] & 0x10) >> 4);
562 printf("\t\t * dual beacon: %d\n", (data
[4] & 0x40) >> 6);
563 printf("\t\t * dual CTS protection: %d\n", (data
[4] & 0x80) >> 7);
564 printf("\t\t * STBC beacon: %d\n", data
[5] & 0x1);
565 printf("\t\t * L-SIG TXOP Prot: %d\n", (data
[5] & 0x2) >> 1);
566 printf("\t\t * PCO active: %d\n", (data
[5] & 0x4) >> 2);
567 printf("\t\t * PCO phase: %d\n", (data
[5] & 0x8) >> 3);
570 static void print_capabilities(const uint8_t type
, uint8_t len
, const uint8_t *data
)
576 for (i
= 0; i
< len
; i
++) {
579 for (bit
= 0; bit
< 8; bit
++) {
580 if (!(data
[i
] & (1 << bit
)))
588 switch (bit
+ base
) {
590 printf(" HT Information Exchange Supported");
593 printf(" On-demand Beacon");
596 printf(" Extended Channel Switching");
599 printf(" Wave Indication");
602 printf(" PSMP Capability");
605 printf(" Service Interval Granularity");
608 printf(" S-PSMP Capability");
620 static void print_tim(const uint8_t type
, uint8_t len
, const uint8_t *data
)
622 printf(" DTIM Count %u DTIM Period %u Bitmap Control 0x%x "
624 data
[0], data
[1], data
[2], data
[3]);
626 printf(" (+ %u octet%s)", len
- 4, len
- 4 == 1 ? "" : "s");
632 void (*print
)(const uint8_t type
, uint8_t len
, const uint8_t *data
);
633 uint8_t minlen
, maxlen
;
637 static void print_ie(const struct ie_print
*p
, const uint8_t type
,
638 uint8_t len
, const uint8_t *data
)
645 printf("\t%s:", p
->name
);
646 if (len
< p
->minlen
|| len
> p
->maxlen
) {
648 printf(" <invalid: %d bytes:", len
);
649 for (i
= 0; i
< len
; i
++)
650 printf(" %.02x", data
[i
]);
653 printf(" <invalid: 1 byte: %.02x>\n", data
[0]);
655 printf(" <invalid: no data>\n");
659 p
->print(type
, len
, data
);
662 #define PRINT_IGN { \
669 static const struct ie_print ieprinters
[] = {
670 [0] = { "SSID", print_ssid
, 0, 32, BIT(PRINT_SCAN
) | BIT(PRINT_LINK
), },
671 [1] = { "Supported rates", print_supprates
, 0, 255, BIT(PRINT_SCAN
), },
672 [3] = { "DS Parameter set", print_ds
, 1, 1, BIT(PRINT_SCAN
), },
673 [5] = { "TIM", print_tim
, 4, 255, BIT(PRINT_SCAN
), },
674 [7] = { "Country", print_country
, 3, 255, BIT(PRINT_SCAN
), },
675 [32] = { "Power constraint", print_powerconstraint
, 1, 1, BIT(PRINT_SCAN
), },
676 [42] = { "ERP", print_erp
, 1, 255, BIT(PRINT_SCAN
), },
677 [45] = { "HT capabilities", print_ht_capa
, 26, 26, BIT(PRINT_SCAN
), },
678 [61] = { "HT operation", print_ht_op
, 22, 22, BIT(PRINT_SCAN
), },
679 [48] = { "RSN", print_rsn
, 2, 255, BIT(PRINT_SCAN
), },
680 [50] = { "Extended supported rates", print_supprates
, 0, 255, BIT(PRINT_SCAN
), },
681 [114] = { "MESH ID", print_ssid
, 0, 32, BIT(PRINT_SCAN
) | BIT(PRINT_LINK
), },
682 [127] = { "Extended capabilities", print_capabilities
, 0, 255, BIT(PRINT_SCAN
), },
685 static void print_wifi_wpa(const uint8_t type
, uint8_t len
, const uint8_t *data
)
687 print_rsn_ie("TKIP", "IEEE 802.1X", len
, data
);
690 static bool print_wifi_wmm_param(const uint8_t *data
, uint8_t len
)
693 static const char *aci_tbl
[] = { "BE", "BK", "VI", "VO" };
699 printf("Parameter: not version 1: ");
703 printf("\t * Parameter version 1");
708 printf("\n\t\t * u-APSD");
712 for (i
= 0; i
< 4; i
++) {
713 printf("\n\t\t * %s:", aci_tbl
[(data
[0] >> 5) & 3]);
716 printf(" CW %d-%d", (1 << (data
[1] & 0xf)) - 1,
717 (1 << (data
[1] >> 4)) - 1);
718 printf(", AIFSN %d", data
[0] & 0xf);
719 if (data
[2] | data
[3])
720 printf(", TXOP %d usec", (data
[2] + (data
[3] << 8)) * 32);
732 static void print_wifi_wmm(const uint8_t type
, uint8_t len
, const uint8_t *data
)
738 printf(" information:");
741 if (print_wifi_wmm_param(data
+ 1, len
- 1))
745 printf(" type %d:", data
[0]);
749 for(i
= 1; i
< len
; i
++)
750 printf(" %.02x", data
[i
]);
754 static const char * wifi_wps_dev_passwd_id(uint16_t id
)
758 return "Default (PIN)";
760 return "User-specified";
762 return "Machine-specified";
768 return "Registrar-specified";
774 static void print_wifi_wps(const uint8_t type
, uint8_t len
, const uint8_t *data
)
777 __u16 subtype
, sublen
;
780 subtype
= (data
[0] << 8) + data
[1];
781 sublen
= (data
[2] << 8) + data
[3];
787 tab_on_first(&first
);
788 printf("\t * Version: %d.%d\n", data
[4] >> 4, data
[4] & 0xF);
791 tab_on_first(&first
);
792 printf("\t * Device name: %.*s\n", sublen
, data
+ 4);
796 tab_on_first(&first
);
798 printf("\t * Device Password ID: (invalid "
799 "length %d)\n", sublen
);
802 id
= data
[4] << 8 | data
[5];
803 printf("\t * Device Password ID: %u (%s)\n",
804 id
, wifi_wps_dev_passwd_id(id
));
808 tab_on_first(&first
);
809 printf("\t * Manufacturer: %.*s\n", sublen
, data
+ 4);
812 tab_on_first(&first
);
813 printf("\t * Model: %.*s\n", sublen
, data
+ 4);
816 tab_on_first(&first
);
817 printf("\t * Model Number: %.*s\n", sublen
, data
+ 4);
821 tab_on_first(&first
);
822 printf("\t * Response Type: %d%s\n",
823 val
, val
== 3 ? " (AP)" : "");
828 tab_on_first(&first
);
829 printf("\t * RF Bands: 0x%x\n", val
);
834 tab_on_first(&first
);
835 printf("\t * Selected Registrar: 0x%x\n", val
);
839 tab_on_first(&first
);
840 printf("\t * Serial Number: %.*s\n", sublen
, data
+ 4);
844 tab_on_first(&first
);
845 printf("\t * Wi-Fi Protected Setup State: %d%s%s\n",
847 val
== 1 ? " (Unconfigured)" : "",
848 val
== 2 ? " (Configured)" : "");
852 tab_on_first(&first
);
853 printf("\t * UUID: ");
855 printf("(invalid, length=%d)\n", sublen
);
858 printf("%02x%02x%02x%02x-%02x%02x-%02x%02x-"
859 "%02x%02x-%02x%02x%02x%02x%02x%02x\n",
860 data
[4], data
[5], data
[6], data
[7],
861 data
[8], data
[9], data
[10], data
[11],
862 data
[12], data
[13], data
[14], data
[15],
863 data
[16], data
[17], data
[18], data
[19]);
866 tab_on_first(&first
);
868 printf("\t * Primary Device Type: (invalid "
869 "length %d)\n", sublen
);
872 printf("\t * Primary Device Type: "
873 "%u-%02x%02x%02x%02x-%u\n",
874 data
[4] << 8 | data
[5],
875 data
[6], data
[7], data
[8], data
[9],
876 data
[10] << 8 | data
[11]);
881 tab_on_first(&first
);
882 printf("\t * AP setup locked: 0x%.2x\n", val
);
887 __u16 meth
= (data
[4] << 8) + data
[5];
889 tab_on_first(&first
);
890 printf("\t * %sConfig methods:",
891 subtype
== 0x1053 ? "Selected Registrar ": "");
892 #define T(bit, name) do { \
893 if (meth & (1<<bit)) { \
913 const __u8
*subdata
= data
+ 4;
914 __u16 tmplen
= sublen
;
916 tab_on_first(&first
);
917 printf("\t * Unknown TLV (%#.4x, %d bytes):",
920 printf(" %.2x", *subdata
);
934 printf("\t\t * bogus tail data (%d):", len
);
936 printf(" %.2x", *data
);
944 static const struct ie_print wifiprinters
[] = {
945 [1] = { "WPA", print_wifi_wpa
, 2, 255, BIT(PRINT_SCAN
), },
946 [2] = { "WMM", print_wifi_wmm
, 1, 255, BIT(PRINT_SCAN
), },
947 [4] = { "WPS", print_wifi_wps
, 0, 255, BIT(PRINT_SCAN
), },
950 static inline void print_p2p(const uint8_t type
, uint8_t len
, const uint8_t *data
)
958 sublen
= (data
[2] << 8) + data
[1];
960 if (sublen
> len
- 3)
964 case 0x02: /* capability */
965 tab_on_first(&first
);
967 printf("\t * malformed capability\n");
970 printf("\t * Group capa: 0x%.2x, Device capa: 0x%.2x\n",
973 case 0x0d: /* device info */
974 if (sublen
< 6 + 2 + 8 + 1) {
975 printf("\t * malformed device info\n");
978 /* fall through for now */
979 case 0x00: /* status */
980 case 0x01: /* minor reason */
981 case 0x03: /* device ID */
982 case 0x04: /* GO intent */
983 case 0x05: /* configuration timeout */
984 case 0x06: /* listen channel */
985 case 0x07: /* group BSSID */
986 case 0x08: /* ext listen timing */
987 case 0x09: /* intended interface address */
988 case 0x0a: /* manageability */
989 case 0x0b: /* channel list */
991 case 0x0e: /* group info */
992 case 0x0f: /* group ID */
993 case 0x10: /* interface */
994 case 0x11: /* operating channel */
995 case 0x12: /* invitation flags */
996 case 0xdd: /* vendor specific */
998 const __u8
*subdata
= data
+ 4;
999 __u16 tmplen
= sublen
;
1001 tab_on_first(&first
);
1002 printf("\t * Unknown TLV (%#.2x, %d bytes):",
1005 printf(" %.2x", *subdata
);
1019 tab_on_first(&first
);
1020 printf("\t * bogus tail data (%d):", len
);
1022 printf(" %.2x", *data
);
1030 static const struct ie_print wfa_printers
[] = {
1031 [9] = { "P2P", print_p2p
, 2, 255, BIT(PRINT_SCAN
), },
1034 static void print_vendor(unsigned char len
, unsigned char *data
,
1035 bool unknown
, enum print_ie_type ptype
)
1040 printf("\tVendor specific: <too short> data:");
1041 for(i
= 0; i
< len
; i
++)
1042 printf(" %.02x", data
[i
]);
1047 if (len
>= 4 && memcmp(data
, ms_oui
, 3) == 0) {
1048 if (data
[3] < ARRAY_SIZE(wifiprinters
) &&
1049 wifiprinters
[data
[3]].name
&&
1050 wifiprinters
[data
[3]].flags
& BIT(ptype
)) {
1051 print_ie(&wifiprinters
[data
[3]], data
[3], len
- 4, data
+ 4);
1056 printf("\tMS/WiFi %#.2x, data:", data
[3]);
1057 for(i
= 0; i
< len
- 4; i
++)
1058 printf(" %.02x", data
[i
+ 4]);
1063 if (len
>= 4 && memcmp(data
, wfa_oui
, 3) == 0) {
1064 if (data
[3] < ARRAY_SIZE(wfa_printers
) &&
1065 wfa_printers
[data
[3]].name
&&
1066 wfa_printers
[data
[3]].flags
& BIT(ptype
)) {
1067 print_ie(&wfa_printers
[data
[3]], data
[3], len
- 4, data
+ 4);
1072 printf("\tWFA %#.2x, data:", data
[3]);
1073 for(i
= 0; i
< len
- 4; i
++)
1074 printf(" %.02x", data
[i
+ 4]);
1082 printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:",
1083 data
[0], data
[1], data
[2]);
1084 for (i
= 3; i
< len
; i
++)
1085 printf(" %.2x", data
[i
]);
1089 void print_ies(unsigned char *ie
, int ielen
, bool unknown
,
1090 enum print_ie_type ptype
)
1092 while (ielen
>= 2 && ielen
>= ie
[1]) {
1093 if (ie
[0] < ARRAY_SIZE(ieprinters
) &&
1094 ieprinters
[ie
[0]].name
&&
1095 ieprinters
[ie
[0]].flags
& BIT(ptype
)) {
1096 print_ie(&ieprinters
[ie
[0]], ie
[0], ie
[1], ie
+ 2);
1097 } else if (ie
[0] == 221 /* vendor */) {
1098 print_vendor(ie
[1], ie
+ 2, unknown
, ptype
);
1099 } else if (unknown
) {
1102 printf("\tUnknown IE (%d):", ie
[0]);
1103 for (i
=0; i
<ie
[1]; i
++)
1104 printf(" %.2x", ie
[2+i
]);
1112 static int print_bss_handler(struct nl_msg
*msg
, void *arg
)
1114 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
1115 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
1116 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
1117 char mac_addr
[20], dev
[20];
1118 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1119 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
1120 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
1121 [NL80211_BSS_BSSID
] = { },
1122 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
1123 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
1124 [NL80211_BSS_INFORMATION_ELEMENTS
] = { },
1125 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
1126 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
1127 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1128 [NL80211_BSS_SEEN_MS_AGO
] = { .type
= NLA_U32
},
1129 [NL80211_BSS_BEACON_IES
] = { },
1131 struct scan_params
*params
= arg
;
1132 int show
= params
->show_both_ie_sets
? 2 : 1;
1134 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
1135 genlmsg_attrlen(gnlh
, 0), NULL
);
1137 if (!tb
[NL80211_ATTR_BSS
]) {
1138 fprintf(stderr
, "bss info missing!\n");
1141 if (nla_parse_nested(bss
, NL80211_BSS_MAX
,
1142 tb
[NL80211_ATTR_BSS
],
1144 fprintf(stderr
, "failed to parse nested attributes!\n");
1148 if (!bss
[NL80211_BSS_BSSID
])
1151 mac_addr_n2a(mac_addr
, nla_data(bss
[NL80211_BSS_BSSID
]));
1152 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), dev
);
1153 printf("BSS %s (on %s)", mac_addr
, dev
);
1155 if (bss
[NL80211_BSS_STATUS
]) {
1156 switch (nla_get_u32(bss
[NL80211_BSS_STATUS
])) {
1157 case NL80211_BSS_STATUS_AUTHENTICATED
:
1158 printf(" -- authenticated");
1160 case NL80211_BSS_STATUS_ASSOCIATED
:
1161 printf(" -- associated");
1163 case NL80211_BSS_STATUS_IBSS_JOINED
:
1164 printf(" -- joined");
1167 printf(" -- unknown status: %d",
1168 nla_get_u32(bss
[NL80211_BSS_STATUS
]));
1174 if (bss
[NL80211_BSS_TSF
]) {
1175 unsigned long long tsf
;
1176 tsf
= (unsigned long long)nla_get_u64(bss
[NL80211_BSS_TSF
]);
1177 printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n",
1178 tsf
, tsf
/1000/1000/60/60/24, (tsf
/1000/1000/60/60) % 24,
1179 (tsf
/1000/1000/60) % 60, (tsf
/1000/1000) % 60);
1181 if (bss
[NL80211_BSS_FREQUENCY
])
1182 printf("\tfreq: %d\n",
1183 nla_get_u32(bss
[NL80211_BSS_FREQUENCY
]));
1184 if (bss
[NL80211_BSS_BEACON_INTERVAL
])
1185 printf("\tbeacon interval: %d\n",
1186 nla_get_u16(bss
[NL80211_BSS_BEACON_INTERVAL
]));
1187 if (bss
[NL80211_BSS_CAPABILITY
]) {
1188 __u16 capa
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
1189 printf("\tcapability:");
1190 if (capa
& WLAN_CAPABILITY_ESS
)
1192 if (capa
& WLAN_CAPABILITY_IBSS
)
1194 if (capa
& WLAN_CAPABILITY_PRIVACY
)
1196 if (capa
& WLAN_CAPABILITY_SHORT_PREAMBLE
)
1197 printf(" ShortPreamble");
1198 if (capa
& WLAN_CAPABILITY_PBCC
)
1200 if (capa
& WLAN_CAPABILITY_CHANNEL_AGILITY
)
1201 printf(" ChannelAgility");
1202 if (capa
& WLAN_CAPABILITY_SPECTRUM_MGMT
)
1203 printf(" SpectrumMgmt");
1204 if (capa
& WLAN_CAPABILITY_QOS
)
1206 if (capa
& WLAN_CAPABILITY_SHORT_SLOT_TIME
)
1207 printf(" ShortSlotTime");
1208 if (capa
& WLAN_CAPABILITY_APSD
)
1210 if (capa
& WLAN_CAPABILITY_DSSS_OFDM
)
1211 printf(" DSSS-OFDM");
1212 printf(" (0x%.4x)\n", capa
);
1214 if (bss
[NL80211_BSS_SIGNAL_MBM
]) {
1215 int s
= nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]);
1216 printf("\tsignal: %d.%.2d dBm\n", s
/100, s
%100);
1218 if (bss
[NL80211_BSS_SIGNAL_UNSPEC
]) {
1219 unsigned char s
= nla_get_u8(bss
[NL80211_BSS_SIGNAL_UNSPEC
]);
1220 printf("\tsignal: %d/100\n", s
);
1222 if (bss
[NL80211_BSS_SEEN_MS_AGO
]) {
1223 int age
= nla_get_u32(bss
[NL80211_BSS_SEEN_MS_AGO
]);
1224 printf("\tlast seen: %d ms ago\n", age
);
1227 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
] && show
--) {
1228 if (bss
[NL80211_BSS_BEACON_IES
])
1229 printf("\tInformation elements from Probe Response "
1231 print_ies(nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]),
1232 nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]),
1233 params
->unknown
, params
->type
);
1235 if (bss
[NL80211_BSS_BEACON_IES
] && show
--) {
1236 printf("\tInformation elements from Beacon frame:\n");
1237 print_ies(nla_data(bss
[NL80211_BSS_BEACON_IES
]),
1238 nla_len(bss
[NL80211_BSS_BEACON_IES
]),
1239 params
->unknown
, params
->type
);
1245 static struct scan_params scan_params
;
1247 static int handle_scan_dump(struct nl80211_state
*state
,
1250 int argc
, char **argv
)
1255 memset(&scan_params
, 0, sizeof(scan_params
));
1257 if (argc
== 1 && !strcmp(argv
[0], "-u"))
1258 scan_params
.unknown
= true;
1259 else if (argc
== 1 && !strcmp(argv
[0], "-b"))
1260 scan_params
.show_both_ie_sets
= true;
1262 scan_params
.type
= PRINT_SCAN
;
1264 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_bss_handler
,
1269 static int handle_scan_combined(struct nl80211_state
*state
,
1272 int argc
, char **argv
)
1275 static char *dump_argv
[] = {
1281 static const __u32 cmds
[] = {
1282 NL80211_CMD_NEW_SCAN_RESULTS
,
1283 NL80211_CMD_SCAN_ABORTED
,
1285 int trig_argc
, dump_argc
, err
;
1287 if (argc
>= 3 && !strcmp(argv
[2], "-u")) {
1289 dump_argv
[3] = "-u";
1290 } else if (argc
>= 3 && !strcmp(argv
[2], "-b")) {
1292 dump_argv
[3] = "-b";
1296 trig_argc
= 3 + (argc
- 2) + (3 - dump_argc
);
1297 trig_argv
= calloc(trig_argc
, sizeof(*trig_argv
));
1300 trig_argv
[0] = argv
[0];
1301 trig_argv
[1] = "scan";
1302 trig_argv
[2] = "trigger";
1304 for (i
= 0; i
< argc
- 2 - (dump_argc
- 3); i
++)
1305 trig_argv
[i
+ 3] = argv
[i
+ 2 + (dump_argc
- 3)];
1306 err
= handle_cmd(state
, II_NETDEV
, trig_argc
, trig_argv
);
1312 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
1314 * This code has a bug, which requires creating a separate
1315 * nl80211 socket to fix:
1316 * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or
1317 * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel
1318 * before (!) we listen to it, because we only start listening
1319 * after we send our scan request.
1321 * Doing it the other way around has a race condition as well,
1322 * if you first open the events socket you may get a notification
1323 * for a previous scan.
1325 * The only proper way to fix this would be to listen to events
1326 * before sending the command, and for the kernel to send the
1327 * scan request along with the event, so that you can match up
1328 * whether the scan you requested was finished or aborted (this
1329 * may result in processing a scan that another application
1330 * requested, but that doesn't seem to be a problem).
1332 * Alas, the kernel doesn't do that (yet).
1335 if (listen_events(state
, ARRAY_SIZE(cmds
), cmds
) ==
1336 NL80211_CMD_SCAN_ABORTED
) {
1337 printf("scan aborted!\n");
1341 dump_argv
[0] = argv
[0];
1342 return handle_cmd(state
, II_NETDEV
, dump_argc
, dump_argv
);
1344 TOPLEVEL(scan
, "[-u] [freq <freq>*] [ies <hex as 00:11:..>] [ssid <ssid>*|passive]", 0, 0,
1345 CIB_NETDEV
, handle_scan_combined
,
1346 "Scan on the given frequencies and probe for the given SSIDs\n"
1347 "(or wildcard if not given) unless passive scanning is requested.\n"
1348 "If -u is specified print unknown data in the scan results.\n"
1349 "Specified (vendor) IEs must be well-formed.");
1350 COMMAND(scan
, dump
, "[-u]",
1351 NL80211_CMD_GET_SCAN
, NLM_F_DUMP
, CIB_NETDEV
, handle_scan_dump
,
1352 "Dump the current scan results. If -u is specified, print unknown\n"
1353 "data in scan results.");
1354 COMMAND(scan
, trigger
, "[freq <freq>*] [ies <hex as 00:11:..>] [ssid <ssid>*|passive]",
1355 NL80211_CMD_TRIGGER_SCAN
, 0, CIB_NETDEV
, handle_scan
,
1356 "Trigger a scan on the given frequencies with probing for the given\n"
1357 "SSIDs (or wildcard if not given) unless passive scanning is requested.");