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
], "ssid") == 0) {
110 } else if (strcmp(argv
[i
], "passive") == 0) {
118 freq
= strtoul(argv
[i
], &eptr
, 10);
119 if (eptr
!= argv
[i
] + strlen(argv
[i
])) {
120 /* failed to parse as number -- maybe a tag? */
125 NLA_PUT_U32(freqs
, i
, freq
);
128 ies
= parse_hex(argv
[i
], &tmp
);
130 goto nla_put_failure
;
131 NLA_PUT(msg
, NL80211_ATTR_IE
, tmp
, ies
);
136 NLA_PUT(ssids
, i
, strlen(argv
[i
]), argv
[i
]);
142 NLA_PUT(ssids
, 1, 0, "");
144 nla_put_nested(msg
, NL80211_ATTR_SCAN_SSIDS
, ssids
);
147 nla_put_nested(msg
, NL80211_ATTR_SCAN_FREQUENCIES
, freqs
);
149 NLA_PUT_U32(msg
, NL80211_ATTR_SCAN_FLAGS
, flags
);
158 static void tab_on_first(bool *first
)
166 static void print_ssid(const uint8_t type
, uint8_t len
, const uint8_t *data
)
169 print_ssid_escaped(len
, data
);
173 #define BSS_MEMBERSHIP_SELECTOR_HT_PHY 127
175 static void print_supprates(const uint8_t type
, uint8_t len
, const uint8_t *data
)
181 for (i
= 0; i
< len
; i
++) {
182 int r
= data
[i
] & 0x7f;
184 if (r
== BSS_MEMBERSHIP_SELECTOR_HT_PHY
&& data
[i
] & 0x80)
187 printf("%d.%d", r
/2, 5*(r
&1));
189 printf("%s ", data
[i
] & 0x80 ? "*" : "");
194 static void print_ds(const uint8_t type
, uint8_t len
, const uint8_t *data
)
196 printf(" channel %d\n", data
[0]);
199 static const char *country_env_str(char environment
)
201 switch (environment
) {
203 return "Indoor only";
205 return "Outdoor only";
207 return "Indoor/Outdoor";
213 static void print_country(const uint8_t type
, uint8_t len
, const uint8_t *data
)
215 printf(" %.*s", 2, data
);
217 printf("\tEnvironment: %s\n", country_env_str(data
[2]));
223 printf("\t\tNo country IE triplets present\n");
229 union ieee80211_country_ie_triplet
*triplet
= (void *) data
;
231 if (triplet
->ext
.reg_extension_id
>= IEEE80211_COUNTRY_EXTENSION_ID
) {
232 printf("\t\tExtension ID: %d Regulatory Class: %d Coverage class: %d (up to %dm)\n",
233 triplet
->ext
.reg_extension_id
,
234 triplet
->ext
.reg_class
,
235 triplet
->ext
.coverage_class
,
236 triplet
->ext
.coverage_class
* 450);
244 if (triplet
->chans
.first_channel
<= 14)
245 end_channel
= triplet
->chans
.first_channel
+ (triplet
->chans
.num_channels
- 1);
247 end_channel
= triplet
->chans
.first_channel
+ (4 * (triplet
->chans
.num_channels
- 1));
249 printf("\t\tChannels [%d - %d] @ %d dBm\n", triplet
->chans
.first_channel
, end_channel
, triplet
->chans
.max_power
);
258 static void print_powerconstraint(const uint8_t type
, uint8_t len
, const uint8_t *data
)
260 printf(" %d dB\n", data
[0]);
263 static void print_erp(const uint8_t type
, uint8_t len
, const uint8_t *data
)
266 printf(" <no flags>");
268 printf(" NonERP_Present");
270 printf(" Use_Protection");
272 printf(" Barker_Preamble_Mode");
276 static void print_cipher(const uint8_t *data
)
278 if (memcmp(data
, ms_oui
, 3) == 0) {
281 printf("Use group cipher suite");
296 printf("%.02x-%.02x-%.02x:%d",
297 data
[0], data
[1] ,data
[2], data
[3]);
300 } else if (memcmp(data
, ieee80211_oui
, 3) == 0) {
303 printf("Use group cipher suite");
318 printf("AES-128-CMAC");
324 printf("%.02x-%.02x-%.02x:%d",
325 data
[0], data
[1] ,data
[2], data
[3]);
329 printf("%.02x-%.02x-%.02x:%d",
330 data
[0], data
[1] ,data
[2], data
[3]);
333 static void print_auth(const uint8_t *data
)
335 if (memcmp(data
, ms_oui
, 3) == 0) {
338 printf("IEEE 802.1X");
344 printf("%.02x-%.02x-%.02x:%d",
345 data
[0], data
[1] ,data
[2], data
[3]);
348 } else if (memcmp(data
, ieee80211_oui
, 3) == 0) {
351 printf("IEEE 802.1X");
357 printf("FT/IEEE 802.1X");
363 printf("IEEE 802.1X/SHA-256");
366 printf("PSK/SHA-256");
369 printf("%.02x-%.02x-%.02x:%d",
370 data
[0], data
[1] ,data
[2], data
[3]);
374 printf("%.02x-%.02x-%.02x:%d",
375 data
[0], data
[1] ,data
[2], data
[3]);
378 static void print_rsn_ie(const char *defcipher
, const char *defauth
,
379 uint8_t len
, const uint8_t *data
)
382 __u16 version
, count
, capa
;
385 version
= data
[0] + (data
[1] << 8);
386 tab_on_first(&first
);
387 printf("\t * Version: %d\n", version
);
393 tab_on_first(&first
);
394 printf("\t * Group cipher: %s\n", defcipher
);
395 printf("\t * Pairwise ciphers: %s\n", defcipher
);
399 tab_on_first(&first
);
400 printf("\t * Group cipher: ");
408 tab_on_first(&first
);
409 printf("\t * Pairwise ciphers: %s\n", defcipher
);
413 count
= data
[0] | (data
[1] << 8);
414 if (2 + (count
* 4) > len
)
417 tab_on_first(&first
);
418 printf("\t * Pairwise ciphers:");
419 for (i
= 0; i
< count
; i
++) {
421 print_cipher(data
+ 2 + (i
* 4));
425 data
+= 2 + (count
* 4);
426 len
-= 2 + (count
* 4);
429 tab_on_first(&first
);
430 printf("\t * Authentication suites: %s\n", defauth
);
434 count
= data
[0] | (data
[1] << 8);
435 if (2 + (count
* 4) > len
)
438 tab_on_first(&first
);
439 printf("\t * Authentication suites:");
440 for (i
= 0; i
< count
; i
++) {
442 print_auth(data
+ 2 + (i
* 4));
446 data
+= 2 + (count
* 4);
447 len
-= 2 + (count
* 4);
450 capa
= data
[0] | (data
[1] << 8);
451 tab_on_first(&first
);
452 printf("\t * Capabilities:");
456 printf(" NoPairwise");
457 switch ((capa
& 0x000c) >> 2) {
461 printf(" 2-PTKSA-RC");
464 printf(" 4-PTKSA-RC");
467 printf(" 16-PTKSA-RC");
470 switch ((capa
& 0x0030) >> 4) {
474 printf(" 2-GTKSA-RC");
477 printf(" 4-GTKSA-RC");
480 printf(" 16-GTKSA-RC");
484 printf(" MFP-required");
486 printf(" MFP-capable");
488 printf(" Peerkey-enabled");
490 printf(" SPP-AMSDU-capable");
492 printf(" SPP-AMSDU-required");
493 printf(" (0x%.4x)\n", capa
);
499 int pmkid_count
= data
[0] | (data
[1] << 8);
501 if (len
>= 2 + 16 * pmkid_count
) {
502 tab_on_first(&first
);
503 printf("\t * %d PMKIDs\n", pmkid_count
);
504 /* not printing PMKID values */
505 data
+= 2 + 16 * pmkid_count
;
506 len
-= 2 + 16 * pmkid_count
;
512 tab_on_first(&first
);
513 printf("\t * Group mgmt cipher suite: ");
522 printf("\t\t * bogus tail data (%d):", len
);
524 printf(" %.2x", *data
);
532 static void print_rsn(const uint8_t type
, uint8_t len
, const uint8_t *data
)
534 print_rsn_ie("CCMP", "IEEE 802.1X", len
, data
);
537 static void print_ht_capa(const uint8_t type
, uint8_t len
, const uint8_t *data
)
540 print_ht_capability(data
[0] | (data
[1] << 8));
541 print_ampdu_length(data
[2] & 3);
542 print_ampdu_spacing((data
[2] >> 2) & 7);
543 print_ht_mcs(data
+ 3);
546 static void print_ht_op(const uint8_t type
, uint8_t len
, const uint8_t *data
)
548 static const char *offset
[4] = {
554 static const char *protection
[4] = {
560 static const char *sta_chan_width
[2] = {
566 printf("\t\t * primary channel: %d\n", data
[0]);
567 printf("\t\t * secondary channel offset: %s\n",
568 offset
[data
[1] & 0x3]);
569 printf("\t\t * STA channel width: %s\n", sta_chan_width
[(data
[1] & 0x4)>>2]);
570 printf("\t\t * RIFS: %d\n", (data
[1] & 0x8)>>3);
571 printf("\t\t * HT protection: %s\n", protection
[data
[2] & 0x3]);
572 printf("\t\t * non-GF present: %d\n", (data
[2] & 0x4) >> 2);
573 printf("\t\t * OBSS non-GF present: %d\n", (data
[2] & 0x10) >> 4);
574 printf("\t\t * dual beacon: %d\n", (data
[4] & 0x40) >> 6);
575 printf("\t\t * dual CTS protection: %d\n", (data
[4] & 0x80) >> 7);
576 printf("\t\t * STBC beacon: %d\n", data
[5] & 0x1);
577 printf("\t\t * L-SIG TXOP Prot: %d\n", (data
[5] & 0x2) >> 1);
578 printf("\t\t * PCO active: %d\n", (data
[5] & 0x4) >> 2);
579 printf("\t\t * PCO phase: %d\n", (data
[5] & 0x8) >> 3);
582 static void print_capabilities(const uint8_t type
, uint8_t len
, const uint8_t *data
)
588 for (i
= 0; i
< len
; i
++) {
591 for (bit
= 0; bit
< 8; bit
++) {
592 if (!(data
[i
] & (1 << bit
)))
600 switch (bit
+ base
) {
602 printf(" HT Information Exchange Supported");
605 printf(" On-demand Beacon");
608 printf(" Extended Channel Switching");
611 printf(" Wave Indication");
614 printf(" PSMP Capability");
617 printf(" Service Interval Granularity");
620 printf(" S-PSMP Capability");
632 static void print_tim(const uint8_t type
, uint8_t len
, const uint8_t *data
)
634 printf(" DTIM Count %u DTIM Period %u Bitmap Control 0x%x "
636 data
[0], data
[1], data
[2], data
[3]);
638 printf(" (+ %u octet%s)", len
- 4, len
- 4 == 1 ? "" : "s");
644 void (*print
)(const uint8_t type
, uint8_t len
, const uint8_t *data
);
645 uint8_t minlen
, maxlen
;
649 static void print_ie(const struct ie_print
*p
, const uint8_t type
,
650 uint8_t len
, const uint8_t *data
)
657 printf("\t%s:", p
->name
);
658 if (len
< p
->minlen
|| len
> p
->maxlen
) {
660 printf(" <invalid: %d bytes:", len
);
661 for (i
= 0; i
< len
; i
++)
662 printf(" %.02x", data
[i
]);
665 printf(" <invalid: 1 byte: %.02x>\n", data
[0]);
667 printf(" <invalid: no data>\n");
671 p
->print(type
, len
, data
);
674 #define PRINT_IGN { \
681 static const struct ie_print ieprinters
[] = {
682 [0] = { "SSID", print_ssid
, 0, 32, BIT(PRINT_SCAN
) | BIT(PRINT_LINK
), },
683 [1] = { "Supported rates", print_supprates
, 0, 255, BIT(PRINT_SCAN
), },
684 [3] = { "DS Parameter set", print_ds
, 1, 1, BIT(PRINT_SCAN
), },
685 [5] = { "TIM", print_tim
, 4, 255, BIT(PRINT_SCAN
), },
686 [7] = { "Country", print_country
, 3, 255, BIT(PRINT_SCAN
), },
687 [32] = { "Power constraint", print_powerconstraint
, 1, 1, BIT(PRINT_SCAN
), },
688 [42] = { "ERP", print_erp
, 1, 255, BIT(PRINT_SCAN
), },
689 [45] = { "HT capabilities", print_ht_capa
, 26, 26, BIT(PRINT_SCAN
), },
690 [61] = { "HT operation", print_ht_op
, 22, 22, BIT(PRINT_SCAN
), },
691 [48] = { "RSN", print_rsn
, 2, 255, BIT(PRINT_SCAN
), },
692 [50] = { "Extended supported rates", print_supprates
, 0, 255, BIT(PRINT_SCAN
), },
693 [114] = { "MESH ID", print_ssid
, 0, 32, BIT(PRINT_SCAN
) | BIT(PRINT_LINK
), },
694 [127] = { "Extended capabilities", print_capabilities
, 0, 255, BIT(PRINT_SCAN
), },
697 static void print_wifi_wpa(const uint8_t type
, uint8_t len
, const uint8_t *data
)
699 print_rsn_ie("TKIP", "IEEE 802.1X", len
, data
);
702 static bool print_wifi_wmm_param(const uint8_t *data
, uint8_t len
)
705 static const char *aci_tbl
[] = { "BE", "BK", "VI", "VO" };
711 printf("Parameter: not version 1: ");
715 printf("\t * Parameter version 1");
720 printf("\n\t\t * u-APSD");
724 for (i
= 0; i
< 4; i
++) {
725 printf("\n\t\t * %s:", aci_tbl
[(data
[0] >> 5) & 3]);
728 printf(" CW %d-%d", (1 << (data
[1] & 0xf)) - 1,
729 (1 << (data
[1] >> 4)) - 1);
730 printf(", AIFSN %d", data
[0] & 0xf);
731 if (data
[2] | data
[3])
732 printf(", TXOP %d usec", (data
[2] + (data
[3] << 8)) * 32);
744 static void print_wifi_wmm(const uint8_t type
, uint8_t len
, const uint8_t *data
)
750 printf(" information:");
753 if (print_wifi_wmm_param(data
+ 1, len
- 1))
757 printf(" type %d:", data
[0]);
761 for(i
= 1; i
< len
; i
++)
762 printf(" %.02x", data
[i
]);
766 static const char * wifi_wps_dev_passwd_id(uint16_t id
)
770 return "Default (PIN)";
772 return "User-specified";
774 return "Machine-specified";
780 return "Registrar-specified";
786 static void print_wifi_wps(const uint8_t type
, uint8_t len
, const uint8_t *data
)
789 __u16 subtype
, sublen
;
792 subtype
= (data
[0] << 8) + data
[1];
793 sublen
= (data
[2] << 8) + data
[3];
799 tab_on_first(&first
);
800 printf("\t * Version: %d.%d\n", data
[4] >> 4, data
[4] & 0xF);
803 tab_on_first(&first
);
804 printf("\t * Device name: %.*s\n", sublen
, data
+ 4);
808 tab_on_first(&first
);
810 printf("\t * Device Password ID: (invalid "
811 "length %d)\n", sublen
);
814 id
= data
[4] << 8 | data
[5];
815 printf("\t * Device Password ID: %u (%s)\n",
816 id
, wifi_wps_dev_passwd_id(id
));
820 tab_on_first(&first
);
821 printf("\t * Manufacturer: %.*s\n", sublen
, data
+ 4);
824 tab_on_first(&first
);
825 printf("\t * Model: %.*s\n", sublen
, data
+ 4);
828 tab_on_first(&first
);
829 printf("\t * Model Number: %.*s\n", sublen
, data
+ 4);
833 tab_on_first(&first
);
834 printf("\t * Response Type: %d%s\n",
835 val
, val
== 3 ? " (AP)" : "");
840 tab_on_first(&first
);
841 printf("\t * RF Bands: 0x%x\n", val
);
846 tab_on_first(&first
);
847 printf("\t * Selected Registrar: 0x%x\n", val
);
851 tab_on_first(&first
);
852 printf("\t * Serial Number: %.*s\n", sublen
, data
+ 4);
856 tab_on_first(&first
);
857 printf("\t * Wi-Fi Protected Setup State: %d%s%s\n",
859 val
== 1 ? " (Unconfigured)" : "",
860 val
== 2 ? " (Configured)" : "");
864 tab_on_first(&first
);
865 printf("\t * UUID: ");
867 printf("(invalid, length=%d)\n", sublen
);
870 printf("%02x%02x%02x%02x-%02x%02x-%02x%02x-"
871 "%02x%02x-%02x%02x%02x%02x%02x%02x\n",
872 data
[4], data
[5], data
[6], data
[7],
873 data
[8], data
[9], data
[10], data
[11],
874 data
[12], data
[13], data
[14], data
[15],
875 data
[16], data
[17], data
[18], data
[19]);
878 tab_on_first(&first
);
880 printf("\t * Primary Device Type: (invalid "
881 "length %d)\n", sublen
);
884 printf("\t * Primary Device Type: "
885 "%u-%02x%02x%02x%02x-%u\n",
886 data
[4] << 8 | data
[5],
887 data
[6], data
[7], data
[8], data
[9],
888 data
[10] << 8 | data
[11]);
893 tab_on_first(&first
);
894 printf("\t * AP setup locked: 0x%.2x\n", val
);
899 __u16 meth
= (data
[4] << 8) + data
[5];
901 tab_on_first(&first
);
902 printf("\t * %sConfig methods:",
903 subtype
== 0x1053 ? "Selected Registrar ": "");
904 #define T(bit, name) do { \
905 if (meth & (1<<bit)) { \
925 const __u8
*subdata
= data
+ 4;
926 __u16 tmplen
= sublen
;
928 tab_on_first(&first
);
929 printf("\t * Unknown TLV (%#.4x, %d bytes):",
932 printf(" %.2x", *subdata
);
946 printf("\t\t * bogus tail data (%d):", len
);
948 printf(" %.2x", *data
);
956 static const struct ie_print wifiprinters
[] = {
957 [1] = { "WPA", print_wifi_wpa
, 2, 255, BIT(PRINT_SCAN
), },
958 [2] = { "WMM", print_wifi_wmm
, 1, 255, BIT(PRINT_SCAN
), },
959 [4] = { "WPS", print_wifi_wps
, 0, 255, BIT(PRINT_SCAN
), },
962 static inline void print_p2p(const uint8_t type
, uint8_t len
, const uint8_t *data
)
970 sublen
= (data
[2] << 8) + data
[1];
972 if (sublen
> len
- 3)
976 case 0x02: /* capability */
977 tab_on_first(&first
);
979 printf("\t * malformed capability\n");
982 printf("\t * Group capa: 0x%.2x, Device capa: 0x%.2x\n",
985 case 0x0d: /* device info */
986 if (sublen
< 6 + 2 + 8 + 1) {
987 printf("\t * malformed device info\n");
990 /* fall through for now */
991 case 0x00: /* status */
992 case 0x01: /* minor reason */
993 case 0x03: /* device ID */
994 case 0x04: /* GO intent */
995 case 0x05: /* configuration timeout */
996 case 0x06: /* listen channel */
997 case 0x07: /* group BSSID */
998 case 0x08: /* ext listen timing */
999 case 0x09: /* intended interface address */
1000 case 0x0a: /* manageability */
1001 case 0x0b: /* channel list */
1002 case 0x0c: /* NoA */
1003 case 0x0e: /* group info */
1004 case 0x0f: /* group ID */
1005 case 0x10: /* interface */
1006 case 0x11: /* operating channel */
1007 case 0x12: /* invitation flags */
1008 case 0xdd: /* vendor specific */
1010 const __u8
*subdata
= data
+ 4;
1011 __u16 tmplen
= sublen
;
1013 tab_on_first(&first
);
1014 printf("\t * Unknown TLV (%#.2x, %d bytes):",
1017 printf(" %.2x", *subdata
);
1031 tab_on_first(&first
);
1032 printf("\t * bogus tail data (%d):", len
);
1034 printf(" %.2x", *data
);
1042 static const struct ie_print wfa_printers
[] = {
1043 [9] = { "P2P", print_p2p
, 2, 255, BIT(PRINT_SCAN
), },
1046 static void print_vendor(unsigned char len
, unsigned char *data
,
1047 bool unknown
, enum print_ie_type ptype
)
1052 printf("\tVendor specific: <too short> data:");
1053 for(i
= 0; i
< len
; i
++)
1054 printf(" %.02x", data
[i
]);
1059 if (len
>= 4 && memcmp(data
, ms_oui
, 3) == 0) {
1060 if (data
[3] < ARRAY_SIZE(wifiprinters
) &&
1061 wifiprinters
[data
[3]].name
&&
1062 wifiprinters
[data
[3]].flags
& BIT(ptype
)) {
1063 print_ie(&wifiprinters
[data
[3]], data
[3], len
- 4, data
+ 4);
1068 printf("\tMS/WiFi %#.2x, data:", data
[3]);
1069 for(i
= 0; i
< len
- 4; i
++)
1070 printf(" %.02x", data
[i
+ 4]);
1075 if (len
>= 4 && memcmp(data
, wfa_oui
, 3) == 0) {
1076 if (data
[3] < ARRAY_SIZE(wfa_printers
) &&
1077 wfa_printers
[data
[3]].name
&&
1078 wfa_printers
[data
[3]].flags
& BIT(ptype
)) {
1079 print_ie(&wfa_printers
[data
[3]], data
[3], len
- 4, data
+ 4);
1084 printf("\tWFA %#.2x, data:", data
[3]);
1085 for(i
= 0; i
< len
- 4; i
++)
1086 printf(" %.02x", data
[i
+ 4]);
1094 printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:",
1095 data
[0], data
[1], data
[2]);
1096 for (i
= 3; i
< len
; i
++)
1097 printf(" %.2x", data
[i
]);
1101 void print_ies(unsigned char *ie
, int ielen
, bool unknown
,
1102 enum print_ie_type ptype
)
1104 while (ielen
>= 2 && ielen
>= ie
[1]) {
1105 if (ie
[0] < ARRAY_SIZE(ieprinters
) &&
1106 ieprinters
[ie
[0]].name
&&
1107 ieprinters
[ie
[0]].flags
& BIT(ptype
)) {
1108 print_ie(&ieprinters
[ie
[0]], ie
[0], ie
[1], ie
+ 2);
1109 } else if (ie
[0] == 221 /* vendor */) {
1110 print_vendor(ie
[1], ie
+ 2, unknown
, ptype
);
1111 } else if (unknown
) {
1114 printf("\tUnknown IE (%d):", ie
[0]);
1115 for (i
=0; i
<ie
[1]; i
++)
1116 printf(" %.2x", ie
[2+i
]);
1124 static int print_bss_handler(struct nl_msg
*msg
, void *arg
)
1126 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
1127 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
1128 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
1129 char mac_addr
[20], dev
[20];
1130 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1131 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
1132 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
1133 [NL80211_BSS_BSSID
] = { },
1134 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
1135 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
1136 [NL80211_BSS_INFORMATION_ELEMENTS
] = { },
1137 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
1138 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
1139 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1140 [NL80211_BSS_SEEN_MS_AGO
] = { .type
= NLA_U32
},
1141 [NL80211_BSS_BEACON_IES
] = { },
1143 struct scan_params
*params
= arg
;
1144 int show
= params
->show_both_ie_sets
? 2 : 1;
1146 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
1147 genlmsg_attrlen(gnlh
, 0), NULL
);
1149 if (!tb
[NL80211_ATTR_BSS
]) {
1150 fprintf(stderr
, "bss info missing!\n");
1153 if (nla_parse_nested(bss
, NL80211_BSS_MAX
,
1154 tb
[NL80211_ATTR_BSS
],
1156 fprintf(stderr
, "failed to parse nested attributes!\n");
1160 if (!bss
[NL80211_BSS_BSSID
])
1163 mac_addr_n2a(mac_addr
, nla_data(bss
[NL80211_BSS_BSSID
]));
1164 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), dev
);
1165 printf("BSS %s (on %s)", mac_addr
, dev
);
1167 if (bss
[NL80211_BSS_STATUS
]) {
1168 switch (nla_get_u32(bss
[NL80211_BSS_STATUS
])) {
1169 case NL80211_BSS_STATUS_AUTHENTICATED
:
1170 printf(" -- authenticated");
1172 case NL80211_BSS_STATUS_ASSOCIATED
:
1173 printf(" -- associated");
1175 case NL80211_BSS_STATUS_IBSS_JOINED
:
1176 printf(" -- joined");
1179 printf(" -- unknown status: %d",
1180 nla_get_u32(bss
[NL80211_BSS_STATUS
]));
1186 if (bss
[NL80211_BSS_TSF
]) {
1187 unsigned long long tsf
;
1188 tsf
= (unsigned long long)nla_get_u64(bss
[NL80211_BSS_TSF
]);
1189 printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n",
1190 tsf
, tsf
/1000/1000/60/60/24, (tsf
/1000/1000/60/60) % 24,
1191 (tsf
/1000/1000/60) % 60, (tsf
/1000/1000) % 60);
1193 if (bss
[NL80211_BSS_FREQUENCY
])
1194 printf("\tfreq: %d\n",
1195 nla_get_u32(bss
[NL80211_BSS_FREQUENCY
]));
1196 if (bss
[NL80211_BSS_BEACON_INTERVAL
])
1197 printf("\tbeacon interval: %d\n",
1198 nla_get_u16(bss
[NL80211_BSS_BEACON_INTERVAL
]));
1199 if (bss
[NL80211_BSS_CAPABILITY
]) {
1200 __u16 capa
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
1201 printf("\tcapability:");
1202 if (capa
& WLAN_CAPABILITY_ESS
)
1204 if (capa
& WLAN_CAPABILITY_IBSS
)
1206 if (capa
& WLAN_CAPABILITY_PRIVACY
)
1208 if (capa
& WLAN_CAPABILITY_SHORT_PREAMBLE
)
1209 printf(" ShortPreamble");
1210 if (capa
& WLAN_CAPABILITY_PBCC
)
1212 if (capa
& WLAN_CAPABILITY_CHANNEL_AGILITY
)
1213 printf(" ChannelAgility");
1214 if (capa
& WLAN_CAPABILITY_SPECTRUM_MGMT
)
1215 printf(" SpectrumMgmt");
1216 if (capa
& WLAN_CAPABILITY_QOS
)
1218 if (capa
& WLAN_CAPABILITY_SHORT_SLOT_TIME
)
1219 printf(" ShortSlotTime");
1220 if (capa
& WLAN_CAPABILITY_APSD
)
1222 if (capa
& WLAN_CAPABILITY_DSSS_OFDM
)
1223 printf(" DSSS-OFDM");
1224 printf(" (0x%.4x)\n", capa
);
1226 if (bss
[NL80211_BSS_SIGNAL_MBM
]) {
1227 int s
= nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]);
1228 printf("\tsignal: %d.%.2d dBm\n", s
/100, s
%100);
1230 if (bss
[NL80211_BSS_SIGNAL_UNSPEC
]) {
1231 unsigned char s
= nla_get_u8(bss
[NL80211_BSS_SIGNAL_UNSPEC
]);
1232 printf("\tsignal: %d/100\n", s
);
1234 if (bss
[NL80211_BSS_SEEN_MS_AGO
]) {
1235 int age
= nla_get_u32(bss
[NL80211_BSS_SEEN_MS_AGO
]);
1236 printf("\tlast seen: %d ms ago\n", age
);
1239 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
] && show
--) {
1240 if (bss
[NL80211_BSS_BEACON_IES
])
1241 printf("\tInformation elements from Probe Response "
1243 print_ies(nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]),
1244 nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]),
1245 params
->unknown
, params
->type
);
1247 if (bss
[NL80211_BSS_BEACON_IES
] && show
--) {
1248 printf("\tInformation elements from Beacon frame:\n");
1249 print_ies(nla_data(bss
[NL80211_BSS_BEACON_IES
]),
1250 nla_len(bss
[NL80211_BSS_BEACON_IES
]),
1251 params
->unknown
, params
->type
);
1257 static struct scan_params scan_params
;
1259 static int handle_scan_dump(struct nl80211_state
*state
,
1262 int argc
, char **argv
,
1268 memset(&scan_params
, 0, sizeof(scan_params
));
1270 if (argc
== 1 && !strcmp(argv
[0], "-u"))
1271 scan_params
.unknown
= true;
1272 else if (argc
== 1 && !strcmp(argv
[0], "-b"))
1273 scan_params
.show_both_ie_sets
= true;
1275 scan_params
.type
= PRINT_SCAN
;
1277 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_bss_handler
,
1282 static int handle_scan_combined(struct nl80211_state
*state
,
1285 int argc
, char **argv
,
1289 static char *dump_argv
[] = {
1295 static const __u32 cmds
[] = {
1296 NL80211_CMD_NEW_SCAN_RESULTS
,
1297 NL80211_CMD_SCAN_ABORTED
,
1299 int trig_argc
, dump_argc
, err
;
1301 if (argc
>= 3 && !strcmp(argv
[2], "-u")) {
1303 dump_argv
[3] = "-u";
1304 } else if (argc
>= 3 && !strcmp(argv
[2], "-b")) {
1306 dump_argv
[3] = "-b";
1310 trig_argc
= 3 + (argc
- 2) + (3 - dump_argc
);
1311 trig_argv
= calloc(trig_argc
, sizeof(*trig_argv
));
1314 trig_argv
[0] = argv
[0];
1315 trig_argv
[1] = "scan";
1316 trig_argv
[2] = "trigger";
1318 for (i
= 0; i
< argc
- 2 - (dump_argc
- 3); i
++)
1319 trig_argv
[i
+ 3] = argv
[i
+ 2 + (dump_argc
- 3)];
1320 err
= handle_cmd(state
, id
, trig_argc
, trig_argv
);
1326 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
1328 * This code has a bug, which requires creating a separate
1329 * nl80211 socket to fix:
1330 * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or
1331 * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel
1332 * before (!) we listen to it, because we only start listening
1333 * after we send our scan request.
1335 * Doing it the other way around has a race condition as well,
1336 * if you first open the events socket you may get a notification
1337 * for a previous scan.
1339 * The only proper way to fix this would be to listen to events
1340 * before sending the command, and for the kernel to send the
1341 * scan request along with the event, so that you can match up
1342 * whether the scan you requested was finished or aborted (this
1343 * may result in processing a scan that another application
1344 * requested, but that doesn't seem to be a problem).
1346 * Alas, the kernel doesn't do that (yet).
1349 if (listen_events(state
, ARRAY_SIZE(cmds
), cmds
) ==
1350 NL80211_CMD_SCAN_ABORTED
) {
1351 printf("scan aborted!\n");
1355 dump_argv
[0] = argv
[0];
1356 return handle_cmd(state
, id
, dump_argc
, dump_argv
);
1358 TOPLEVEL(scan
, "[-u] [freq <freq>*] [ies <hex as 00:11:..>] [lowpri,flush] [ssid <ssid>*|passive]", 0, 0,
1359 CIB_NETDEV
, handle_scan_combined
,
1360 "Scan on the given frequencies and probe for the given SSIDs\n"
1361 "(or wildcard if not given) unless passive scanning is requested.\n"
1362 "If -u is specified print unknown data in the scan results.\n"
1363 "Specified (vendor) IEs must be well-formed.");
1364 COMMAND(scan
, dump
, "[-u]",
1365 NL80211_CMD_GET_SCAN
, NLM_F_DUMP
, CIB_NETDEV
, handle_scan_dump
,
1366 "Dump the current scan results. If -u is specified, print unknown\n"
1367 "data in scan results.");
1368 COMMAND(scan
, trigger
, "[freq <freq>*] [ies <hex as 00:11:..>] [lowpri,flush] [ssid <ssid>*|passive]",
1369 NL80211_CMD_TRIGGER_SCAN
, 0, CIB_NETDEV
, handle_scan
,
1370 "Trigger a scan on the given frequencies with probing for the given\n"
1371 "SSIDs (or wildcard if not given) unless passive scanning is requested.");