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");
309 printf("%.02x-%.02x-%.02x:%d",
310 data
[0], data
[1] ,data
[2], data
[3]);
314 printf("%.02x-%.02x-%.02x:%d",
315 data
[0], data
[1] ,data
[2], data
[3]);
318 static void print_auth(const uint8_t *data
)
320 if (memcmp(data
, ms_oui
, 3) == 0) {
323 printf("IEEE 802.1X");
329 printf("%.02x-%.02x-%.02x:%d",
330 data
[0], data
[1] ,data
[2], data
[3]);
333 } else if (memcmp(data
, ieee80211_oui
, 3) == 0) {
336 printf("IEEE 802.1X");
342 printf("FT/IEEE 802.1X");
348 printf("IEEE 802.1X/SHA-256");
351 printf("PSK/SHA-256");
354 printf("%.02x-%.02x-%.02x:%d",
355 data
[0], data
[1] ,data
[2], data
[3]);
359 printf("%.02x-%.02x-%.02x:%d",
360 data
[0], data
[1] ,data
[2], data
[3]);
363 static void print_rsn_ie(const char *defcipher
, const char *defauth
,
364 uint8_t len
, const uint8_t *data
)
367 __u16 version
, count
, capa
;
370 version
= data
[0] + (data
[1] << 8);
371 tab_on_first(&first
);
372 printf("\t * Version: %d\n", version
);
378 tab_on_first(&first
);
379 printf("\t * Group cipher: %s\n", defcipher
);
380 printf("\t * Pairwise ciphers: %s\n", defcipher
);
384 tab_on_first(&first
);
385 printf("\t * Group cipher: ");
393 tab_on_first(&first
);
394 printf("\t * Pairwise ciphers: %s\n", defcipher
);
398 count
= data
[0] | (data
[1] << 8);
399 if (2 + (count
* 4) > len
)
402 tab_on_first(&first
);
403 printf("\t * Pairwise ciphers:");
404 for (i
= 0; i
< count
; i
++) {
406 print_cipher(data
+ 2 + (i
* 4));
410 data
+= 2 + (count
* 4);
411 len
-= 2 + (count
* 4);
414 tab_on_first(&first
);
415 printf("\t * Authentication suites: %s\n", defauth
);
419 count
= data
[0] | (data
[1] << 8);
420 if (2 + (count
* 4) > len
)
423 tab_on_first(&first
);
424 printf("\t * Authentication suites:");
425 for (i
= 0; i
< count
; i
++) {
427 print_auth(data
+ 2 + (i
* 4));
431 data
+= 2 + (count
* 4);
432 len
-= 2 + (count
* 4);
435 capa
= data
[0] | (data
[1] << 8);
436 tab_on_first(&first
);
437 printf("\t * Capabilities:");
441 printf(" NoPairwise");
442 switch ((capa
& 0x000c) >> 2) {
446 printf(" 2-PTKSA-RC");
449 printf(" 4-PTKSA-RC");
452 printf(" 16-PTKSA-RC");
455 switch ((capa
& 0x0030) >> 4) {
459 printf(" 2-GTKSA-RC");
462 printf(" 4-GTKSA-RC");
465 printf(" 16-GTKSA-RC");
469 printf(" MFP-required");
471 printf(" MFP-capable");
473 printf(" Peerkey-enabled");
475 printf(" SPP-AMSDU-capable");
477 printf(" SPP-AMSDU-required");
478 printf(" (0x%.4x)\n", capa
);
484 int pmkid_count
= data
[0] | (data
[1] << 8);
486 if (len
>= 2 + 16 * pmkid_count
) {
487 tab_on_first(&first
);
488 printf("\t * %d PMKIDs\n", pmkid_count
);
489 /* not printing PMKID values */
490 data
+= 2 + 16 * pmkid_count
;
491 len
-= 2 + 16 * pmkid_count
;
497 tab_on_first(&first
);
498 printf("\t * Group mgmt cipher suite: ");
507 printf("\t\t * bogus tail data (%d):", len
);
509 printf(" %.2x", *data
);
517 static void print_rsn(const uint8_t type
, uint8_t len
, const uint8_t *data
)
519 print_rsn_ie("CCMP", "IEEE 802.1X", len
, data
);
522 static void print_ht_capa(const uint8_t type
, uint8_t len
, const uint8_t *data
)
525 print_ht_capability(data
[0] | (data
[1] << 8));
526 print_ampdu_length(data
[2] & 3);
527 print_ampdu_spacing((data
[2] >> 2) & 7);
528 print_ht_mcs(data
+ 3);
531 static void print_ht_op(const uint8_t type
, uint8_t len
, const uint8_t *data
)
533 static const char *offset
[4] = {
539 static const char *protection
[4] = {
545 static const char *sta_chan_width
[2] = {
551 printf("\t\t * primary channel: %d\n", data
[0]);
552 printf("\t\t * secondary channel offset: %s\n",
553 offset
[data
[1] & 0x3]);
554 printf("\t\t * STA channel width: %s\n", sta_chan_width
[(data
[1] & 0x4)>>2]);
555 printf("\t\t * RIFS: %d\n", (data
[1] & 0x8)>>3);
556 printf("\t\t * HT protection: %s\n", protection
[data
[2] & 0x3]);
557 printf("\t\t * non-GF present: %d\n", (data
[2] & 0x4) >> 2);
558 printf("\t\t * OBSS non-GF present: %d\n", (data
[2] & 0x10) >> 4);
559 printf("\t\t * dual beacon: %d\n", (data
[4] & 0x40) >> 6);
560 printf("\t\t * dual CTS protection: %d\n", (data
[4] & 0x80) >> 7);
561 printf("\t\t * STBC beacon: %d\n", data
[5] & 0x1);
562 printf("\t\t * L-SIG TXOP Prot: %d\n", (data
[5] & 0x2) >> 1);
563 printf("\t\t * PCO active: %d\n", (data
[5] & 0x4) >> 2);
564 printf("\t\t * PCO phase: %d\n", (data
[5] & 0x8) >> 3);
567 static void print_capabilities(const uint8_t type
, uint8_t len
, const uint8_t *data
)
573 for (i
= 0; i
< len
; i
++) {
576 for (bit
= 0; bit
< 8; bit
++) {
577 if (!(data
[i
] & (1 << bit
)))
585 switch (bit
+ base
) {
587 printf(" HT Information Exchange Supported");
590 printf(" On-demand Beacon");
593 printf(" Extended Channel Switching");
596 printf(" Wave Indication");
599 printf(" PSMP Capability");
602 printf(" Service Interval Granularity");
605 printf(" S-PSMP Capability");
617 static void print_tim(const uint8_t type
, uint8_t len
, const uint8_t *data
)
619 printf(" DTIM Count %u DTIM Period %u Bitmap Control 0x%x "
621 data
[0], data
[1], data
[2], data
[3]);
623 printf(" (+ %u octet%s)", len
- 4, len
- 4 == 1 ? "" : "s");
629 void (*print
)(const uint8_t type
, uint8_t len
, const uint8_t *data
);
630 uint8_t minlen
, maxlen
;
634 static void print_ie(const struct ie_print
*p
, const uint8_t type
,
635 uint8_t len
, const uint8_t *data
)
642 printf("\t%s:", p
->name
);
643 if (len
< p
->minlen
|| len
> p
->maxlen
) {
645 printf(" <invalid: %d bytes:", len
);
646 for (i
= 0; i
< len
; i
++)
647 printf(" %.02x", data
[i
]);
650 printf(" <invalid: 1 byte: %.02x>\n", data
[0]);
652 printf(" <invalid: no data>\n");
656 p
->print(type
, len
, data
);
659 #define PRINT_IGN { \
666 static const struct ie_print ieprinters
[] = {
667 [0] = { "SSID", print_ssid
, 0, 32, BIT(PRINT_SCAN
) | BIT(PRINT_LINK
), },
668 [1] = { "Supported rates", print_supprates
, 0, 255, BIT(PRINT_SCAN
), },
669 [3] = { "DS Parameter set", print_ds
, 1, 1, BIT(PRINT_SCAN
), },
670 [5] = { "TIM", print_tim
, 4, 255, BIT(PRINT_SCAN
), },
671 [7] = { "Country", print_country
, 3, 255, BIT(PRINT_SCAN
), },
672 [32] = { "Power constraint", print_powerconstraint
, 1, 1, BIT(PRINT_SCAN
), },
673 [42] = { "ERP", print_erp
, 1, 255, BIT(PRINT_SCAN
), },
674 [45] = { "HT capabilities", print_ht_capa
, 26, 26, BIT(PRINT_SCAN
), },
675 [61] = { "HT operation", print_ht_op
, 22, 22, BIT(PRINT_SCAN
), },
676 [48] = { "RSN", print_rsn
, 2, 255, BIT(PRINT_SCAN
), },
677 [50] = { "Extended supported rates", print_supprates
, 0, 255, BIT(PRINT_SCAN
), },
678 [114] = { "MESH ID", print_ssid
, 0, 32, BIT(PRINT_SCAN
) | BIT(PRINT_LINK
), },
679 [127] = { "Extended capabilities", print_capabilities
, 0, 255, BIT(PRINT_SCAN
), },
682 static void print_wifi_wpa(const uint8_t type
, uint8_t len
, const uint8_t *data
)
684 print_rsn_ie("TKIP", "IEEE 802.1X", len
, data
);
687 static bool print_wifi_wmm_param(const uint8_t *data
, uint8_t len
)
690 static const char *aci_tbl
[] = { "BE", "BK", "VI", "VO" };
696 printf("Parameter: not version 1: ");
700 printf("\t * Parameter version 1");
705 printf("\n\t\t * u-APSD");
709 for (i
= 0; i
< 4; i
++) {
710 printf("\n\t\t * %s:", aci_tbl
[(data
[0] >> 5) & 3]);
713 printf(" CW %d-%d", (1 << (data
[1] & 0xf)) - 1,
714 (1 << (data
[1] >> 4)) - 1);
715 printf(", AIFSN %d", data
[0] & 0xf);
716 if (data
[2] | data
[3])
717 printf(", TXOP %d usec", (data
[2] + (data
[3] << 8)) * 32);
729 static void print_wifi_wmm(const uint8_t type
, uint8_t len
, const uint8_t *data
)
735 printf(" information:");
738 if (print_wifi_wmm_param(data
+ 1, len
- 1))
742 printf(" type %d:", data
[0]);
746 for(i
= 1; i
< len
; i
++)
747 printf(" %.02x", data
[i
]);
751 static const char * wifi_wps_dev_passwd_id(uint16_t id
)
755 return "Default (PIN)";
757 return "User-specified";
759 return "Machine-specified";
765 return "Registrar-specified";
771 static void print_wifi_wps(const uint8_t type
, uint8_t len
, const uint8_t *data
)
774 __u16 subtype
, sublen
;
777 subtype
= (data
[0] << 8) + data
[1];
778 sublen
= (data
[2] << 8) + data
[3];
784 tab_on_first(&first
);
785 printf("\t * Version: %d.%d\n", data
[4] >> 4, data
[4] & 0xF);
788 tab_on_first(&first
);
789 printf("\t * Device name: %.*s\n", sublen
, data
+ 4);
793 tab_on_first(&first
);
795 printf("\t * Device Password ID: (invalid "
796 "length %d)\n", sublen
);
799 id
= data
[4] << 8 | data
[5];
800 printf("\t * Device Password ID: %u (%s)\n",
801 id
, wifi_wps_dev_passwd_id(id
));
805 tab_on_first(&first
);
806 printf("\t * Manufacturer: %.*s\n", sublen
, data
+ 4);
809 tab_on_first(&first
);
810 printf("\t * Model: %.*s\n", sublen
, data
+ 4);
813 tab_on_first(&first
);
814 printf("\t * Model Number: %.*s\n", sublen
, data
+ 4);
818 tab_on_first(&first
);
819 printf("\t * Response Type: %d%s\n",
820 val
, val
== 3 ? " (AP)" : "");
825 tab_on_first(&first
);
826 printf("\t * RF Bands: 0x%x\n", val
);
831 tab_on_first(&first
);
832 printf("\t * Selected Registrar: 0x%x\n", val
);
836 tab_on_first(&first
);
837 printf("\t * Serial Number: %.*s\n", sublen
, data
+ 4);
841 tab_on_first(&first
);
842 printf("\t * Wi-Fi Protected Setup State: %d%s%s\n",
844 val
== 1 ? " (Unconfigured)" : "",
845 val
== 2 ? " (Configured)" : "");
849 tab_on_first(&first
);
850 printf("\t * UUID: ");
852 printf("(invalid, length=%d)\n", sublen
);
855 printf("%02x%02x%02x%02x-%02x%02x-%02x%02x-"
856 "%02x%02x-%02x%02x%02x%02x%02x%02x\n",
857 data
[4], data
[5], data
[6], data
[7],
858 data
[8], data
[9], data
[10], data
[11],
859 data
[12], data
[13], data
[14], data
[15],
860 data
[16], data
[17], data
[18], data
[19]);
863 tab_on_first(&first
);
865 printf("\t * Primary Device Type: (invalid "
866 "length %d)\n", sublen
);
869 printf("\t * Primary Device Type: "
870 "%u-%02x%02x%02x%02x-%u\n",
871 data
[4] << 8 | data
[5],
872 data
[6], data
[7], data
[8], data
[9],
873 data
[10] << 8 | data
[11]);
878 tab_on_first(&first
);
879 printf("\t * AP setup locked: 0x%.2x\n", val
);
884 __u16 meth
= (data
[4] << 8) + data
[5];
886 tab_on_first(&first
);
887 printf("\t * %sConfig methods:",
888 subtype
== 0x1053 ? "Selected Registrar ": "");
889 #define T(bit, name) do { \
890 if (meth & (1<<bit)) { \
910 const __u8
*subdata
= data
+ 4;
911 __u16 tmplen
= sublen
;
913 tab_on_first(&first
);
914 printf("\t * Unknown TLV (%#.4x, %d bytes):",
917 printf(" %.2x", *subdata
);
931 printf("\t\t * bogus tail data (%d):", len
);
933 printf(" %.2x", *data
);
941 static const struct ie_print wifiprinters
[] = {
942 [1] = { "WPA", print_wifi_wpa
, 2, 255, BIT(PRINT_SCAN
), },
943 [2] = { "WMM", print_wifi_wmm
, 1, 255, BIT(PRINT_SCAN
), },
944 [4] = { "WPS", print_wifi_wps
, 0, 255, BIT(PRINT_SCAN
), },
947 static inline void print_p2p(const uint8_t type
, uint8_t len
, const uint8_t *data
)
955 sublen
= (data
[2] << 8) + data
[1];
957 if (sublen
> len
- 3)
961 case 0x02: /* capability */
962 tab_on_first(&first
);
964 printf("\t * malformed capability\n");
967 printf("\t * Group capa: 0x%.2x, Device capa: 0x%.2x\n",
970 case 0x0d: /* device info */
971 if (sublen
< 6 + 2 + 8 + 1) {
972 printf("\t * malformed device info\n");
975 /* fall through for now */
976 case 0x00: /* status */
977 case 0x01: /* minor reason */
978 case 0x03: /* device ID */
979 case 0x04: /* GO intent */
980 case 0x05: /* configuration timeout */
981 case 0x06: /* listen channel */
982 case 0x07: /* group BSSID */
983 case 0x08: /* ext listen timing */
984 case 0x09: /* intended interface address */
985 case 0x0a: /* manageability */
986 case 0x0b: /* channel list */
988 case 0x0e: /* group info */
989 case 0x0f: /* group ID */
990 case 0x10: /* interface */
991 case 0x11: /* operating channel */
992 case 0x12: /* invitation flags */
993 case 0xdd: /* vendor specific */
995 const __u8
*subdata
= data
+ 4;
996 __u16 tmplen
= sublen
;
998 tab_on_first(&first
);
999 printf("\t * Unknown TLV (%#.2x, %d bytes):",
1002 printf(" %.2x", *subdata
);
1016 tab_on_first(&first
);
1017 printf("\t * bogus tail data (%d):", len
);
1019 printf(" %.2x", *data
);
1027 static const struct ie_print wfa_printers
[] = {
1028 [9] = { "P2P", print_p2p
, 2, 255, BIT(PRINT_SCAN
), },
1031 static void print_vendor(unsigned char len
, unsigned char *data
,
1032 bool unknown
, enum print_ie_type ptype
)
1037 printf("\tVendor specific: <too short> data:");
1038 for(i
= 0; i
< len
; i
++)
1039 printf(" %.02x", data
[i
]);
1044 if (len
>= 4 && memcmp(data
, ms_oui
, 3) == 0) {
1045 if (data
[3] < ARRAY_SIZE(wifiprinters
) &&
1046 wifiprinters
[data
[3]].name
&&
1047 wifiprinters
[data
[3]].flags
& BIT(ptype
)) {
1048 print_ie(&wifiprinters
[data
[3]], data
[3], len
- 4, data
+ 4);
1053 printf("\tMS/WiFi %#.2x, data:", data
[3]);
1054 for(i
= 0; i
< len
- 4; i
++)
1055 printf(" %.02x", data
[i
+ 4]);
1060 if (len
>= 4 && memcmp(data
, wfa_oui
, 3) == 0) {
1061 if (data
[3] < ARRAY_SIZE(wfa_printers
) &&
1062 wfa_printers
[data
[3]].name
&&
1063 wfa_printers
[data
[3]].flags
& BIT(ptype
)) {
1064 print_ie(&wfa_printers
[data
[3]], data
[3], len
- 4, data
+ 4);
1069 printf("\tWFA %#.2x, data:", data
[3]);
1070 for(i
= 0; i
< len
- 4; i
++)
1071 printf(" %.02x", data
[i
+ 4]);
1079 printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:",
1080 data
[0], data
[1], data
[2]);
1081 for (i
= 3; i
< len
; i
++)
1082 printf(" %.2x", data
[i
]);
1086 void print_ies(unsigned char *ie
, int ielen
, bool unknown
,
1087 enum print_ie_type ptype
)
1089 while (ielen
>= 2 && ielen
>= ie
[1]) {
1090 if (ie
[0] < ARRAY_SIZE(ieprinters
) &&
1091 ieprinters
[ie
[0]].name
&&
1092 ieprinters
[ie
[0]].flags
& BIT(ptype
)) {
1093 print_ie(&ieprinters
[ie
[0]], ie
[0], ie
[1], ie
+ 2);
1094 } else if (ie
[0] == 221 /* vendor */) {
1095 print_vendor(ie
[1], ie
+ 2, unknown
, ptype
);
1096 } else if (unknown
) {
1099 printf("\tUnknown IE (%d):", ie
[0]);
1100 for (i
=0; i
<ie
[1]; i
++)
1101 printf(" %.2x", ie
[2+i
]);
1109 static int print_bss_handler(struct nl_msg
*msg
, void *arg
)
1111 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
1112 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
1113 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
1114 char mac_addr
[20], dev
[20];
1115 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1116 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
1117 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
1118 [NL80211_BSS_BSSID
] = { },
1119 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
1120 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
1121 [NL80211_BSS_INFORMATION_ELEMENTS
] = { },
1122 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
1123 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
1124 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1125 [NL80211_BSS_SEEN_MS_AGO
] = { .type
= NLA_U32
},
1126 [NL80211_BSS_BEACON_IES
] = { },
1128 struct scan_params
*params
= arg
;
1129 int show
= params
->show_both_ie_sets
? 2 : 1;
1131 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
1132 genlmsg_attrlen(gnlh
, 0), NULL
);
1134 if (!tb
[NL80211_ATTR_BSS
]) {
1135 fprintf(stderr
, "bss info missing!\n");
1138 if (nla_parse_nested(bss
, NL80211_BSS_MAX
,
1139 tb
[NL80211_ATTR_BSS
],
1141 fprintf(stderr
, "failed to parse nested attributes!\n");
1145 if (!bss
[NL80211_BSS_BSSID
])
1148 mac_addr_n2a(mac_addr
, nla_data(bss
[NL80211_BSS_BSSID
]));
1149 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), dev
);
1150 printf("BSS %s (on %s)", mac_addr
, dev
);
1152 if (bss
[NL80211_BSS_STATUS
]) {
1153 switch (nla_get_u32(bss
[NL80211_BSS_STATUS
])) {
1154 case NL80211_BSS_STATUS_AUTHENTICATED
:
1155 printf(" -- authenticated");
1157 case NL80211_BSS_STATUS_ASSOCIATED
:
1158 printf(" -- associated");
1160 case NL80211_BSS_STATUS_IBSS_JOINED
:
1161 printf(" -- joined");
1164 printf(" -- unknown status: %d",
1165 nla_get_u32(bss
[NL80211_BSS_STATUS
]));
1171 if (bss
[NL80211_BSS_TSF
]) {
1172 unsigned long long tsf
;
1173 tsf
= (unsigned long long)nla_get_u64(bss
[NL80211_BSS_TSF
]);
1174 printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n",
1175 tsf
, tsf
/1000/1000/60/60/24, (tsf
/1000/1000/60/60) % 24,
1176 (tsf
/1000/1000/60) % 60, (tsf
/1000/1000) % 60);
1178 if (bss
[NL80211_BSS_FREQUENCY
])
1179 printf("\tfreq: %d\n",
1180 nla_get_u32(bss
[NL80211_BSS_FREQUENCY
]));
1181 if (bss
[NL80211_BSS_BEACON_INTERVAL
])
1182 printf("\tbeacon interval: %d\n",
1183 nla_get_u16(bss
[NL80211_BSS_BEACON_INTERVAL
]));
1184 if (bss
[NL80211_BSS_CAPABILITY
]) {
1185 __u16 capa
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
1186 printf("\tcapability:");
1187 if (capa
& WLAN_CAPABILITY_ESS
)
1189 if (capa
& WLAN_CAPABILITY_IBSS
)
1191 if (capa
& WLAN_CAPABILITY_PRIVACY
)
1193 if (capa
& WLAN_CAPABILITY_SHORT_PREAMBLE
)
1194 printf(" ShortPreamble");
1195 if (capa
& WLAN_CAPABILITY_PBCC
)
1197 if (capa
& WLAN_CAPABILITY_CHANNEL_AGILITY
)
1198 printf(" ChannelAgility");
1199 if (capa
& WLAN_CAPABILITY_SPECTRUM_MGMT
)
1200 printf(" SpectrumMgmt");
1201 if (capa
& WLAN_CAPABILITY_QOS
)
1203 if (capa
& WLAN_CAPABILITY_SHORT_SLOT_TIME
)
1204 printf(" ShortSlotTime");
1205 if (capa
& WLAN_CAPABILITY_APSD
)
1207 if (capa
& WLAN_CAPABILITY_DSSS_OFDM
)
1208 printf(" DSSS-OFDM");
1209 printf(" (0x%.4x)\n", capa
);
1211 if (bss
[NL80211_BSS_SIGNAL_MBM
]) {
1212 int s
= nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]);
1213 printf("\tsignal: %d.%.2d dBm\n", s
/100, s
%100);
1215 if (bss
[NL80211_BSS_SIGNAL_UNSPEC
]) {
1216 unsigned char s
= nla_get_u8(bss
[NL80211_BSS_SIGNAL_UNSPEC
]);
1217 printf("\tsignal: %d/100\n", s
);
1219 if (bss
[NL80211_BSS_SEEN_MS_AGO
]) {
1220 int age
= nla_get_u32(bss
[NL80211_BSS_SEEN_MS_AGO
]);
1221 printf("\tlast seen: %d ms ago\n", age
);
1224 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
] && show
--) {
1225 if (bss
[NL80211_BSS_BEACON_IES
])
1226 printf("\tInformation elements from Probe Response "
1228 print_ies(nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]),
1229 nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]),
1230 params
->unknown
, params
->type
);
1232 if (bss
[NL80211_BSS_BEACON_IES
] && show
--) {
1233 printf("\tInformation elements from Beacon frame:\n");
1234 print_ies(nla_data(bss
[NL80211_BSS_BEACON_IES
]),
1235 nla_len(bss
[NL80211_BSS_BEACON_IES
]),
1236 params
->unknown
, params
->type
);
1242 static struct scan_params scan_params
;
1244 static int handle_scan_dump(struct nl80211_state
*state
,
1247 int argc
, char **argv
)
1252 memset(&scan_params
, 0, sizeof(scan_params
));
1254 if (argc
== 1 && !strcmp(argv
[0], "-u"))
1255 scan_params
.unknown
= true;
1256 else if (argc
== 1 && !strcmp(argv
[0], "-b"))
1257 scan_params
.show_both_ie_sets
= true;
1259 scan_params
.type
= PRINT_SCAN
;
1261 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_bss_handler
,
1266 static int handle_scan_combined(struct nl80211_state
*state
,
1269 int argc
, char **argv
)
1272 static char *dump_argv
[] = {
1278 static const __u32 cmds
[] = {
1279 NL80211_CMD_NEW_SCAN_RESULTS
,
1280 NL80211_CMD_SCAN_ABORTED
,
1282 int trig_argc
, dump_argc
, err
;
1284 if (argc
>= 3 && !strcmp(argv
[2], "-u")) {
1286 dump_argv
[3] = "-u";
1287 } else if (argc
>= 3 && !strcmp(argv
[2], "-b")) {
1289 dump_argv
[3] = "-b";
1293 trig_argc
= 3 + (argc
- 2) + (3 - dump_argc
);
1294 trig_argv
= calloc(trig_argc
, sizeof(*trig_argv
));
1297 trig_argv
[0] = argv
[0];
1298 trig_argv
[1] = "scan";
1299 trig_argv
[2] = "trigger";
1301 for (i
= 0; i
< argc
- 2 - (dump_argc
- 3); i
++)
1302 trig_argv
[i
+ 3] = argv
[i
+ 2 + (dump_argc
- 3)];
1303 err
= handle_cmd(state
, II_NETDEV
, trig_argc
, trig_argv
);
1309 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
1311 * This code has a bug, which requires creating a separate
1312 * nl80211 socket to fix:
1313 * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or
1314 * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel
1315 * before (!) we listen to it, because we only start listening
1316 * after we send our scan request.
1318 * Doing it the other way around has a race condition as well,
1319 * if you first open the events socket you may get a notification
1320 * for a previous scan.
1322 * The only proper way to fix this would be to listen to events
1323 * before sending the command, and for the kernel to send the
1324 * scan request along with the event, so that you can match up
1325 * whether the scan you requested was finished or aborted (this
1326 * may result in processing a scan that another application
1327 * requested, but that doesn't seem to be a problem).
1329 * Alas, the kernel doesn't do that (yet).
1332 if (listen_events(state
, ARRAY_SIZE(cmds
), cmds
) ==
1333 NL80211_CMD_SCAN_ABORTED
) {
1334 printf("scan aborted!\n");
1338 dump_argv
[0] = argv
[0];
1339 return handle_cmd(state
, II_NETDEV
, dump_argc
, dump_argv
);
1341 TOPLEVEL(scan
, "[-u] [freq <freq>*] [ies <hex as 00:11:..>] [ssid <ssid>*|passive]", 0, 0,
1342 CIB_NETDEV
, handle_scan_combined
,
1343 "Scan on the given frequencies and probe for the given SSIDs\n"
1344 "(or wildcard if not given) unless passive scanning is requested.\n"
1345 "If -u is specified print unknown data in the scan results.\n"
1346 "Specified (vendor) IEs must be well-formed.");
1347 COMMAND(scan
, dump
, "[-u]",
1348 NL80211_CMD_GET_SCAN
, NLM_F_DUMP
, CIB_NETDEV
, handle_scan_dump
,
1349 "Dump the current scan results. If -u is specified, print unknown\n"
1350 "data in scan results.");
1351 COMMAND(scan
, trigger
, "[freq <freq>*] [ies <hex as 00:11:..>] [ssid <ssid>*|passive]",
1352 NL80211_CMD_TRIGGER_SCAN
, 0, CIB_NETDEV
, handle_scan
,
1353 "Trigger a scan on the given frequencies with probing for the given\n"
1354 "SSIDs (or wildcard if not given) unless passive scanning is requested.");