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 wifi_oui
[3] = { 0x00, 0x50, 0xf2 };
31 static unsigned char ieee80211_oui
[3] = { 0x00, 0x0f, 0xac };
35 enum print_ie_type type
;
36 bool show_both_ie_sets
;
39 #define IEEE80211_COUNTRY_EXTENSION_ID 201
41 union ieee80211_country_ie_triplet
{
46 } __attribute__ ((packed
)) chans
;
48 __u8 reg_extension_id
;
51 } __attribute__ ((packed
)) ext
;
52 } __attribute__ ((packed
));
54 static int handle_scan(struct nl80211_state
*state
,
57 int argc
, char **argv
)
59 struct nl_msg
*ssids
= NULL
, *freqs
= NULL
;
71 bool passive
= false, have_ssids
= false, have_freqs
= false;
75 ssids
= nlmsg_alloc();
79 freqs
= nlmsg_alloc();
85 for (i
= 0; i
< argc
; i
++) {
88 if (strcmp(argv
[i
], "freq") == 0) {
92 } else if (strcmp(argv
[i
], "ies") == 0) {
95 } else if (strcmp(argv
[i
], "ssid") == 0) {
99 } else if (strcmp(argv
[i
], "passive") == 0) {
107 freq
= strtoul(argv
[i
], &eptr
, 10);
108 if (eptr
!= argv
[i
] + strlen(argv
[i
])) {
109 /* failed to parse as number -- maybe a tag? */
114 NLA_PUT_U32(freqs
, i
, freq
);
117 ies
= parse_hex(argv
[i
], &tmp
);
119 goto nla_put_failure
;
120 NLA_PUT(msg
, NL80211_ATTR_IE
, tmp
, ies
);
125 NLA_PUT(ssids
, i
, strlen(argv
[i
]), argv
[i
]);
131 NLA_PUT(ssids
, 1, 0, "");
133 nla_put_nested(msg
, NL80211_ATTR_SCAN_SSIDS
, ssids
);
136 nla_put_nested(msg
, NL80211_ATTR_SCAN_FREQUENCIES
, freqs
);
145 static void tab_on_first(bool *first
)
153 static void print_ssid(const uint8_t type
, uint8_t len
, const uint8_t *data
)
156 print_ssid_escaped(len
, data
);
160 static void print_supprates(const uint8_t type
, uint8_t len
, const uint8_t *data
)
166 for (i
= 0; i
< len
; i
++) {
167 int r
= data
[i
] & 0x7f;
168 printf("%d.%d%s ", r
/2, 5*(r
&1), data
[i
] & 0x80 ? "*":"");
173 static void print_ds(const uint8_t type
, uint8_t len
, const uint8_t *data
)
175 printf(" channel %d\n", data
[0]);
178 static const char *country_env_str(char environment
)
180 switch (environment
) {
182 return "Indoor only";
184 return "Outdoor only";
186 return "Indoor/Outdoor";
192 static void print_country(const uint8_t type
, uint8_t len
, const uint8_t *data
)
194 printf(" %.*s", 2, data
);
196 printf("\tEnvironment: %s\n", country_env_str(data
[2]));
202 printf("\t\tNo country IE triplets present\n");
208 union ieee80211_country_ie_triplet
*triplet
= (void *) data
;
210 if (triplet
->ext
.reg_extension_id
>= IEEE80211_COUNTRY_EXTENSION_ID
) {
211 printf("\t\tExtension ID: %d Regulatory Class: %d Coverage class: %d (up to %dm)\n",
212 triplet
->ext
.reg_extension_id
,
213 triplet
->ext
.reg_class
,
214 triplet
->ext
.coverage_class
,
215 triplet
->ext
.coverage_class
* 450);
223 if (triplet
->chans
.first_channel
<= 14)
224 end_channel
= triplet
->chans
.first_channel
+ (triplet
->chans
.num_channels
- 1);
226 end_channel
= triplet
->chans
.first_channel
+ (4 * (triplet
->chans
.num_channels
- 1));
228 printf("\t\tChannels [%d - %d] @ %d dBm\n", triplet
->chans
.first_channel
, end_channel
, triplet
->chans
.max_power
);
237 static void print_powerconstraint(const uint8_t type
, uint8_t len
, const uint8_t *data
)
239 printf(" %d dB\n", data
[0]);
242 static void print_erp(const uint8_t type
, uint8_t len
, const uint8_t *data
)
245 printf(" <no flags>");
247 printf(" NonERP_Present");
249 printf(" Use_Protection");
251 printf(" Barker_Preamble_Mode");
255 static void print_cipher(const uint8_t *data
)
257 if (memcmp(data
, wifi_oui
, 3) == 0) {
260 printf("Use group cipher suite");
275 printf("%.02x-%.02x-%.02x:%d",
276 data
[0], data
[1] ,data
[2], data
[3]);
279 } else if (memcmp(data
, ieee80211_oui
, 3) == 0) {
282 printf("Use group cipher suite");
297 printf("AES-128-CMAC");
300 printf("%.02x-%.02x-%.02x:%d",
301 data
[0], data
[1] ,data
[2], data
[3]);
305 printf("%.02x-%.02x-%.02x:%d",
306 data
[0], data
[1] ,data
[2], data
[3]);
309 static void print_auth(const uint8_t *data
)
311 if (memcmp(data
, wifi_oui
, 3) == 0) {
314 printf("IEEE 802.1X");
320 printf("%.02x-%.02x-%.02x:%d",
321 data
[0], data
[1] ,data
[2], data
[3]);
324 } else if (memcmp(data
, ieee80211_oui
, 3) == 0) {
327 printf("IEEE 802.1X");
333 printf("FT/IEEE 802.1X");
339 printf("IEEE 802.1X/SHA-256");
342 printf("PSK/SHA-256");
345 printf("%.02x-%.02x-%.02x:%d",
346 data
[0], data
[1] ,data
[2], data
[3]);
350 printf("%.02x-%.02x-%.02x:%d",
351 data
[0], data
[1] ,data
[2], data
[3]);
354 static void print_rsn_ie(const char *defcipher
, const char *defauth
,
355 uint8_t len
, const uint8_t *data
)
358 __u16 version
, count
, capa
;
361 version
= data
[0] + (data
[1] << 8);
362 tab_on_first(&first
);
363 printf("\t * Version: %d\n", version
);
369 tab_on_first(&first
);
370 printf("\t * Group cipher: %s\n", defcipher
);
371 printf("\t * Pairwise ciphers: %s\n", defcipher
);
375 tab_on_first(&first
);
376 printf("\t * Group cipher: ");
384 tab_on_first(&first
);
385 printf("\t * Pairwise ciphers: %s\n", defcipher
);
389 count
= data
[0] | (data
[1] << 8);
390 if (2 + (count
* 4) > len
)
393 tab_on_first(&first
);
394 printf("\t * Pairwise ciphers:");
395 for (i
= 0; i
< count
; i
++) {
397 print_cipher(data
+ 2 + (i
* 4));
401 data
+= 2 + (count
* 4);
402 len
-= 2 + (count
* 4);
405 tab_on_first(&first
);
406 printf("\t * Authentication suites: %s\n", defauth
);
410 count
= data
[0] | (data
[1] << 8);
411 if (2 + (count
* 4) > len
)
414 tab_on_first(&first
);
415 printf("\t * Authentication suites:");
416 for (i
= 0; i
< count
; i
++) {
418 print_auth(data
+ 2 + (i
* 4));
422 data
+= 2 + (count
* 4);
423 len
-= 2 + (count
* 4);
426 capa
= data
[0] | (data
[1] << 8);
427 tab_on_first(&first
);
428 printf("\t * Capabilities:");
432 printf(" NoPairwise");
433 switch ((capa
& 0x000c) >> 2) {
437 printf(" 2-PTKSA-RC");
440 printf(" 4-PTKSA-RC");
443 printf(" 16-PTKSA-RC");
446 switch ((capa
& 0x0030) >> 4) {
450 printf(" 2-GTKSA-RC");
453 printf(" 4-GTKSA-RC");
456 printf(" 16-GTKSA-RC");
460 printf(" MFP-required");
462 printf(" MFP-capable");
464 printf(" Peerkey-enabled");
466 printf(" SPP-AMSDU-capable");
468 printf(" SPP-AMSDU-required");
469 printf(" (0x%.4x)\n", capa
);
475 int pmkid_count
= data
[0] | (data
[1] << 8);
477 if (len
>= 2 + 16 * pmkid_count
) {
478 tab_on_first(&first
);
479 printf("\t * %d PMKIDs\n", pmkid_count
);
480 /* not printing PMKID values */
481 data
+= 2 + 16 * pmkid_count
;
482 len
-= 2 + 16 * pmkid_count
;
488 tab_on_first(&first
);
489 printf("\t * Group mgmt cipher suite: ");
498 printf("\t\t * bogus tail data (%d):", len
);
500 printf(" %.2x", *data
);
508 static void print_rsn(const uint8_t type
, uint8_t len
, const uint8_t *data
)
510 print_rsn_ie("CCMP", "IEEE 802.1X", len
, data
);
513 static void print_ht_capa(const uint8_t type
, uint8_t len
, const uint8_t *data
)
516 print_ht_capability(data
[0] | (data
[1] << 8));
517 print_ampdu_length(data
[2] & 3);
518 print_ampdu_spacing((data
[2] >> 2) & 7);
519 print_ht_mcs(data
+ 3);
522 static void print_ht_op(const uint8_t type
, uint8_t len
, const uint8_t *data
)
524 static const char *offset
[4] = {
530 static const char *protection
[4] = {
536 static const char *sta_chan_width
[2] = {
542 printf("\t\t * primary channel: %d\n", data
[0]);
543 printf("\t\t * secondary channel offset: %s\n",
544 offset
[data
[1] & 0x3]);
545 printf("\t\t * STA channel width: %s\n", sta_chan_width
[(data
[1] & 0x4)>>2]);
546 printf("\t\t * RIFS: %d\n", (data
[1] & 0x8)>>3);
547 printf("\t\t * HT protection: %s\n", protection
[data
[2] & 0x3]);
548 printf("\t\t * non-GF present: %d\n", (data
[2] & 0x4) >> 2);
549 printf("\t\t * OBSS non-GF present: %d\n", (data
[2] & 0x10) >> 4);
550 printf("\t\t * dual beacon: %d\n", (data
[4] & 0x40) >> 6);
551 printf("\t\t * dual CTS protection: %d\n", (data
[4] & 0x80) >> 7);
552 printf("\t\t * STBC beacon: %d\n", data
[5] & 0x1);
553 printf("\t\t * L-SIG TXOP Prot: %d\n", (data
[5] & 0x2) >> 1);
554 printf("\t\t * PCO active: %d\n", (data
[5] & 0x4) >> 2);
555 printf("\t\t * PCO phase: %d\n", (data
[5] & 0x8) >> 3);
558 static void print_capabilities(const uint8_t type
, uint8_t len
, const uint8_t *data
)
564 for (i
= 0; i
< len
; i
++) {
567 for (bit
= 0; bit
< 8; bit
++) {
568 if (!(data
[i
] & (1 << bit
)))
576 switch (bit
+ base
) {
578 printf(" HT Information Exchange Supported");
581 printf(" On-demand Beacon");
584 printf(" Extended Channel Switching");
587 printf(" Wave Indication");
590 printf(" PSMP Capability");
593 printf(" Service Interval Granularity");
596 printf(" S-PSMP Capability");
608 static void print_tim(const uint8_t type
, uint8_t len
, const uint8_t *data
)
610 printf(" DTIM Count %u DTIM Period %u Bitmap Control 0x%x "
612 data
[0], data
[1], data
[2], data
[3]);
614 printf(" (+ %u octet%s)", len
- 4, len
- 4 == 1 ? "" : "s");
620 void (*print
)(const uint8_t type
, uint8_t len
, const uint8_t *data
);
621 uint8_t minlen
, maxlen
;
625 static void print_ie(const struct ie_print
*p
, const uint8_t type
,
626 uint8_t len
, const uint8_t *data
)
633 printf("\t%s:", p
->name
);
634 if (len
< p
->minlen
|| len
> p
->maxlen
) {
636 printf(" <invalid: %d bytes:", len
);
637 for (i
= 0; i
< len
; i
++)
638 printf(" %.02x", data
[i
]);
641 printf(" <invalid: 1 byte: %.02x>\n", data
[0]);
643 printf(" <invalid: no data>\n");
647 p
->print(type
, len
, data
);
650 #define PRINT_IGN { \
657 static const struct ie_print ieprinters
[] = {
658 [0] = { "SSID", print_ssid
, 0, 32, BIT(PRINT_SCAN
) | BIT(PRINT_LINK
), },
659 [1] = { "Supported rates", print_supprates
, 0, 255, BIT(PRINT_SCAN
), },
660 [3] = { "DS Parameter set", print_ds
, 1, 1, BIT(PRINT_SCAN
), },
661 [5] = { "TIM", print_tim
, 4, 255, BIT(PRINT_SCAN
), },
662 [7] = { "Country", print_country
, 3, 255, BIT(PRINT_SCAN
), },
663 [32] = { "Power constraint", print_powerconstraint
, 1, 1, BIT(PRINT_SCAN
), },
664 [42] = { "ERP", print_erp
, 1, 255, BIT(PRINT_SCAN
), },
665 [45] = { "HT capabilities", print_ht_capa
, 26, 26, BIT(PRINT_SCAN
), },
666 [61] = { "HT operation", print_ht_op
, 22, 22, BIT(PRINT_SCAN
), },
667 [48] = { "RSN", print_rsn
, 2, 255, BIT(PRINT_SCAN
), },
668 [50] = { "Extended supported rates", print_supprates
, 0, 255, BIT(PRINT_SCAN
), },
669 [127] = { "Extended capabilities", print_capabilities
, 0, 255, BIT(PRINT_SCAN
), },
672 static void print_wifi_wpa(const uint8_t type
, uint8_t len
, const uint8_t *data
)
674 print_rsn_ie("TKIP", "IEEE 802.1X", len
, data
);
677 static bool print_wifi_wmm_param(const uint8_t *data
, uint8_t len
)
680 static const char *aci_tbl
[] = { "BE", "BK", "VI", "VO" };
686 printf("Parameter: not version 1: ");
690 printf("\t * Parameter version 1");
695 printf("\n\t\t * u-APSD");
699 for (i
= 0; i
< 4; i
++) {
700 printf("\n\t\t * %s:", aci_tbl
[(data
[0] >> 5) & 3]);
703 printf(" CW %d-%d", (1 << (data
[1] & 0xf)) - 1,
704 (1 << (data
[1] >> 4)) - 1);
705 printf(", AIFSN %d", data
[0] & 0xf);
706 if (data
[2] | data
[3])
707 printf(", TXOP %d usec", (data
[2] + (data
[3] << 8)) * 32);
719 static void print_wifi_wmm(const uint8_t type
, uint8_t len
, const uint8_t *data
)
725 printf(" information:");
728 if (print_wifi_wmm_param(data
+ 1, len
- 1))
732 printf(" type %d:", data
[0]);
736 for(i
= 1; i
< len
; i
++)
737 printf(" %.02x", data
[i
]);
741 static const char * wifi_wps_dev_passwd_id(uint16_t id
)
745 return "Default (PIN)";
747 return "User-specified";
749 return "Machine-specified";
755 return "Registrar-specified";
761 static void print_wifi_wps(const uint8_t type
, uint8_t len
, const uint8_t *data
)
764 __u16 subtype
, sublen
;
767 subtype
= (data
[0] << 8) + data
[1];
768 sublen
= (data
[2] << 8) + data
[3];
774 tab_on_first(&first
);
775 printf("\t * Version: %d.%d\n", data
[4] >> 4, data
[4] & 0xF);
778 tab_on_first(&first
);
779 printf("\t * Device name: %.*s\n", sublen
, data
+ 4);
783 tab_on_first(&first
);
785 printf("\t * Device Password ID: (invalid "
786 "length %d)\n", sublen
);
789 id
= data
[4] << 8 | data
[5];
790 printf("\t * Device Password ID: %u (%s)\n",
791 id
, wifi_wps_dev_passwd_id(id
));
795 tab_on_first(&first
);
796 printf("\t * Manufacturer: %.*s\n", sublen
, data
+ 4);
799 tab_on_first(&first
);
800 printf("\t * Model: %.*s\n", sublen
, data
+ 4);
803 tab_on_first(&first
);
804 printf("\t * Model Number: %.*s\n", sublen
, data
+ 4);
808 tab_on_first(&first
);
809 printf("\t * Response Type: %d%s\n",
810 val
, val
== 3 ? " (AP)" : "");
815 tab_on_first(&first
);
816 printf("\t * RF Bands: 0x%x\n", val
);
821 tab_on_first(&first
);
822 printf("\t * Selected Registrar: 0x%x\n", val
);
826 tab_on_first(&first
);
827 printf("\t * Serial Number: %.*s\n", sublen
, data
+ 4);
831 tab_on_first(&first
);
832 printf("\t * Wi-Fi Protected Setup State: %d%s%s\n",
834 val
== 1 ? " (Unconfigured)" : "",
835 val
== 2 ? " (Configured)" : "");
839 tab_on_first(&first
);
841 printf("\t * Primary Device Type: (invalid "
842 "length %d)\n", sublen
);
845 printf("\t * Primary Device Type: "
846 "%u-%02x%02x%02x%02x-%u\n",
847 data
[4] << 8 | data
[5],
848 data
[6], data
[7], data
[8], data
[9],
849 data
[10] << 8 | data
[11]);
854 tab_on_first(&first
);
855 printf("\t * AP setup locked: 0x%.2x\n", val
);
860 __u16 meth
= (data
[4] << 8) + data
[5];
862 tab_on_first(&first
);
863 printf("\t * %sConfig methods:",
864 subtype
== 0x1053 ? "Selected Registrar ": "");
865 #define T(bit, name) do { \
866 if (meth & (1<<bit)) { \
886 const __u8
*subdata
= data
+ 4;
887 __u16 tmplen
= sublen
;
889 tab_on_first(&first
);
890 printf("\t * Unknown TLV (%#.4x, %d bytes):",
893 printf(" %.2x", *subdata
);
907 printf("\t\t * bogus tail data (%d):", len
);
909 printf(" %.2x", *data
);
917 static const struct ie_print wifiprinters
[] = {
918 [1] = { "WPA", print_wifi_wpa
, 2, 255, BIT(PRINT_SCAN
), },
919 [2] = { "WMM", print_wifi_wmm
, 1, 255, BIT(PRINT_SCAN
), },
920 [4] = { "WPS", print_wifi_wps
, 0, 255, BIT(PRINT_SCAN
), },
923 static void print_vendor(unsigned char len
, unsigned char *data
,
924 bool unknown
, enum print_ie_type ptype
)
929 printf("\tVendor specific: <too short> data:");
930 for(i
= 0; i
< len
; i
++)
931 printf(" %.02x", data
[i
]);
936 if (len
>= 4 && memcmp(data
, wifi_oui
, 3) == 0) {
937 if (data
[3] < ARRAY_SIZE(wifiprinters
) &&
938 wifiprinters
[data
[3]].name
&&
939 wifiprinters
[data
[3]].flags
& BIT(ptype
)) {
940 print_ie(&wifiprinters
[data
[3]], data
[3], len
- 4, data
+ 4);
945 printf("\tWiFi OUI %#.2x, data:", data
[3]);
946 for(i
= 0; i
< len
- 4; i
++)
947 printf(" %.02x", data
[i
+ 4]);
955 printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:",
956 data
[0], data
[1], data
[2]);
957 for (i
= 3; i
< len
; i
++)
958 printf(" %.2x", data
[i
]);
962 void print_ies(unsigned char *ie
, int ielen
, bool unknown
,
963 enum print_ie_type ptype
)
965 while (ielen
>= 2 && ielen
>= ie
[1]) {
966 if (ie
[0] < ARRAY_SIZE(ieprinters
) &&
967 ieprinters
[ie
[0]].name
&&
968 ieprinters
[ie
[0]].flags
& BIT(ptype
)) {
969 print_ie(&ieprinters
[ie
[0]], ie
[0], ie
[1], ie
+ 2);
970 } else if (ie
[0] == 221 /* vendor */) {
971 print_vendor(ie
[1], ie
+ 2, unknown
, ptype
);
972 } else if (unknown
) {
975 printf("\tUnknown IE (%d):", ie
[0]);
976 for (i
=0; i
<ie
[1]; i
++)
977 printf(" %.2x", ie
[2+i
]);
985 static int print_bss_handler(struct nl_msg
*msg
, void *arg
)
987 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
988 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
989 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
990 char mac_addr
[20], dev
[20];
991 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
992 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
993 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
994 [NL80211_BSS_BSSID
] = { },
995 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
996 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
997 [NL80211_BSS_INFORMATION_ELEMENTS
] = { },
998 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
999 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
1000 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1001 [NL80211_BSS_SEEN_MS_AGO
] = { .type
= NLA_U32
},
1002 [NL80211_BSS_BEACON_IES
] = { },
1004 struct scan_params
*params
= arg
;
1005 int show
= params
->show_both_ie_sets
? 2 : 1;
1007 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
1008 genlmsg_attrlen(gnlh
, 0), NULL
);
1010 if (!tb
[NL80211_ATTR_BSS
]) {
1011 fprintf(stderr
, "bss info missing!\n");
1014 if (nla_parse_nested(bss
, NL80211_BSS_MAX
,
1015 tb
[NL80211_ATTR_BSS
],
1017 fprintf(stderr
, "failed to parse nested attributes!\n");
1021 if (!bss
[NL80211_BSS_BSSID
])
1024 mac_addr_n2a(mac_addr
, nla_data(bss
[NL80211_BSS_BSSID
]));
1025 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), dev
);
1026 printf("BSS %s (on %s)", mac_addr
, dev
);
1028 if (bss
[NL80211_BSS_STATUS
]) {
1029 switch (nla_get_u32(bss
[NL80211_BSS_STATUS
])) {
1030 case NL80211_BSS_STATUS_AUTHENTICATED
:
1031 printf(" -- authenticated");
1033 case NL80211_BSS_STATUS_ASSOCIATED
:
1034 printf(" -- associated");
1036 case NL80211_BSS_STATUS_IBSS_JOINED
:
1037 printf(" -- joined");
1040 printf(" -- unknown status: %d",
1041 nla_get_u32(bss
[NL80211_BSS_STATUS
]));
1047 if (bss
[NL80211_BSS_TSF
]) {
1048 unsigned long long tsf
;
1049 tsf
= (unsigned long long)nla_get_u64(bss
[NL80211_BSS_TSF
]);
1050 printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n",
1051 tsf
, tsf
/1000/1000/60/60/24, (tsf
/1000/1000/60/60) % 24,
1052 (tsf
/1000/1000/60) % 60, (tsf
/1000/1000) % 60);
1054 if (bss
[NL80211_BSS_FREQUENCY
])
1055 printf("\tfreq: %d\n",
1056 nla_get_u32(bss
[NL80211_BSS_FREQUENCY
]));
1057 if (bss
[NL80211_BSS_BEACON_INTERVAL
])
1058 printf("\tbeacon interval: %d\n",
1059 nla_get_u16(bss
[NL80211_BSS_BEACON_INTERVAL
]));
1060 if (bss
[NL80211_BSS_CAPABILITY
]) {
1061 __u16 capa
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
1062 printf("\tcapability:");
1063 if (capa
& WLAN_CAPABILITY_ESS
)
1065 if (capa
& WLAN_CAPABILITY_IBSS
)
1067 if (capa
& WLAN_CAPABILITY_PRIVACY
)
1069 if (capa
& WLAN_CAPABILITY_SHORT_PREAMBLE
)
1070 printf(" ShortPreamble");
1071 if (capa
& WLAN_CAPABILITY_PBCC
)
1073 if (capa
& WLAN_CAPABILITY_CHANNEL_AGILITY
)
1074 printf(" ChannelAgility");
1075 if (capa
& WLAN_CAPABILITY_SPECTRUM_MGMT
)
1076 printf(" SpectrumMgmt");
1077 if (capa
& WLAN_CAPABILITY_QOS
)
1079 if (capa
& WLAN_CAPABILITY_SHORT_SLOT_TIME
)
1080 printf(" ShortSlotTime");
1081 if (capa
& WLAN_CAPABILITY_APSD
)
1083 if (capa
& WLAN_CAPABILITY_DSSS_OFDM
)
1084 printf(" DSSS-OFDM");
1085 printf(" (0x%.4x)\n", capa
);
1087 if (bss
[NL80211_BSS_SIGNAL_MBM
]) {
1088 int s
= nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]);
1089 printf("\tsignal: %d.%.2d dBm\n", s
/100, s
%100);
1091 if (bss
[NL80211_BSS_SIGNAL_UNSPEC
]) {
1092 unsigned char s
= nla_get_u8(bss
[NL80211_BSS_SIGNAL_UNSPEC
]);
1093 printf("\tsignal: %d/100\n", s
);
1095 if (bss
[NL80211_BSS_SEEN_MS_AGO
]) {
1096 int age
= nla_get_u32(bss
[NL80211_BSS_SEEN_MS_AGO
]);
1097 printf("\tlast seen: %d ms ago\n", age
);
1100 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
] && show
--) {
1101 if (bss
[NL80211_BSS_BEACON_IES
])
1102 printf("\tInformation elements from Probe Response "
1104 print_ies(nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]),
1105 nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]),
1106 params
->unknown
, params
->type
);
1108 if (bss
[NL80211_BSS_BEACON_IES
] && show
--) {
1109 printf("\tInformation elements from Beacon frame:\n");
1110 print_ies(nla_data(bss
[NL80211_BSS_BEACON_IES
]),
1111 nla_len(bss
[NL80211_BSS_BEACON_IES
]),
1112 params
->unknown
, params
->type
);
1118 static struct scan_params scan_params
;
1120 static int handle_scan_dump(struct nl80211_state
*state
,
1123 int argc
, char **argv
)
1128 memset(&scan_params
, 0, sizeof(scan_params
));
1130 if (argc
== 1 && !strcmp(argv
[0], "-u"))
1131 scan_params
.unknown
= true;
1132 else if (argc
== 1 && !strcmp(argv
[0], "-b"))
1133 scan_params
.show_both_ie_sets
= true;
1135 scan_params
.type
= PRINT_SCAN
;
1137 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_bss_handler
,
1142 static int handle_scan_combined(struct nl80211_state
*state
,
1145 int argc
, char **argv
)
1148 static char *dump_argv
[] = {
1154 static const __u32 cmds
[] = {
1155 NL80211_CMD_NEW_SCAN_RESULTS
,
1156 NL80211_CMD_SCAN_ABORTED
,
1158 int trig_argc
, dump_argc
, err
;
1160 if (argc
>= 3 && !strcmp(argv
[2], "-u")) {
1162 dump_argv
[3] = "-u";
1163 } else if (argc
>= 3 && !strcmp(argv
[2], "-b")) {
1165 dump_argv
[3] = "-b";
1169 trig_argc
= 3 + (argc
- 2) + (3 - dump_argc
);
1170 trig_argv
= calloc(trig_argc
, sizeof(*trig_argv
));
1173 trig_argv
[0] = argv
[0];
1174 trig_argv
[1] = "scan";
1175 trig_argv
[2] = "trigger";
1177 for (i
= 0; i
< argc
- 2 - (dump_argc
- 3); i
++)
1178 trig_argv
[i
+ 3] = argv
[i
+ 2 + (dump_argc
- 3)];
1179 err
= handle_cmd(state
, II_NETDEV
, trig_argc
, trig_argv
);
1185 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
1187 * This code has a bug, which requires creating a separate
1188 * nl80211 socket to fix:
1189 * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or
1190 * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel
1191 * before (!) we listen to it, because we only start listening
1192 * after we send our scan request.
1194 * Doing it the other way around has a race condition as well,
1195 * if you first open the events socket you may get a notification
1196 * for a previous scan.
1198 * The only proper way to fix this would be to listen to events
1199 * before sending the command, and for the kernel to send the
1200 * scan request along with the event, so that you can match up
1201 * whether the scan you requested was finished or aborted (this
1202 * may result in processing a scan that another application
1203 * requested, but that doesn't seem to be a problem).
1205 * Alas, the kernel doesn't do that (yet).
1208 if (listen_events(state
, ARRAY_SIZE(cmds
), cmds
) ==
1209 NL80211_CMD_SCAN_ABORTED
) {
1210 printf("scan aborted!\n");
1214 dump_argv
[0] = argv
[0];
1215 return handle_cmd(state
, II_NETDEV
, dump_argc
, dump_argv
);
1217 TOPLEVEL(scan
, "[-u] [freq <freq>*] [ies <hex as 00:11:..>] [ssid <ssid>*|passive]", 0, 0,
1218 CIB_NETDEV
, handle_scan_combined
,
1219 "Scan on the given frequencies and probe for the given SSIDs\n"
1220 "(or wildcard if not given) unless passive scanning is requested.\n"
1221 "If -u is specified print unknown data in the scan results.\n"
1222 "Specified (vendor) IEs must be well-formed.");
1223 COMMAND(scan
, dump
, "[-u]",
1224 NL80211_CMD_GET_SCAN
, NLM_F_DUMP
, CIB_NETDEV
, handle_scan_dump
,
1225 "Dump the current scan results. If -u is specified, print unknown\n"
1226 "data in scan results.");
1227 COMMAND(scan
, trigger
, "[freq <freq>*] [ies <hex as 00:11:..>] [ssid <ssid>*|passive]",
1228 NL80211_CMD_TRIGGER_SCAN
, 0, CIB_NETDEV
, handle_scan
,
1229 "Trigger a scan on the given frequencies with probing for the given\n"
1230 "SSIDs (or wildcard if not given) unless passive scanning is requested.");