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 };
37 static int handle_scan(struct nl80211_state
*state
,
40 int argc
, char **argv
)
42 struct nl_msg
*ssids
= NULL
, *freqs
= NULL
;
53 bool passive
= false, have_ssids
= false, have_freqs
= false;
55 ssids
= nlmsg_alloc();
59 freqs
= nlmsg_alloc();
65 for (i
= 0; i
< argc
; i
++) {
66 if (parse
== NONE
&& strcmp(argv
[i
], "freq") == 0) {
70 } else if (parse
< SSID
&& strcmp(argv
[i
], "ssid") == 0) {
74 } else if (parse
< SSID
&& strcmp(argv
[i
], "passive") == 0) {
85 freq
= strtoul(argv
[i
], &eptr
, 10);
86 if (eptr
!= argv
[i
] + strlen(argv
[i
]))
88 NLA_PUT_U32(freqs
, i
, freq
);
91 NLA_PUT(ssids
, i
, strlen(argv
[i
]), argv
[i
]);
97 NLA_PUT(ssids
, 1, 0, "");
99 nla_put_nested(msg
, NL80211_ATTR_SCAN_SSIDS
, ssids
);
102 nla_put_nested(msg
, NL80211_ATTR_SCAN_FREQUENCIES
, freqs
);
110 COMMAND(scan
, trigger
, "[freq <freq>*] [ssid <ssid>*|passive]",
111 NL80211_CMD_TRIGGER_SCAN
, 0, CIB_NETDEV
, handle_scan
,
112 "Trigger a scan on the given frequencies with probing for the given\n"
113 "SSIDs (or wildcard if not given) unless passive scanning is requested.");
115 static void tab_on_first(bool *first
)
123 static void print_ssid(const uint8_t type
, uint8_t len
, const uint8_t *data
)
126 print_ssid_escaped(len
, data
);
130 static void print_supprates(const uint8_t type
, uint8_t len
, const uint8_t *data
)
136 for (i
= 0; i
< len
; i
++) {
137 int r
= data
[i
] & 0x7f;
138 printf("%d.%d%s ", r
/2, 5*(r
&1), data
[i
] & 0x80 ? "*":"");
143 static void print_ds(const uint8_t type
, uint8_t len
, const uint8_t *data
)
145 printf(" channel %d\n", data
[0]);
148 static void print_country(const uint8_t type
, uint8_t len
, const uint8_t *data
)
152 printf(" %.*s", 2, data
);
158 printf(" (outdoor)");
161 printf(" (in/outdoor)");
164 printf(" (invalid environment)");
168 for(i
=0; i
<len
-3; i
++)
169 printf(" %.02x", data
[i
+ 3]);
173 static void print_powerconstraint(const uint8_t type
, uint8_t len
, const uint8_t *data
)
175 printf(" %d dB\n", data
[0]);
178 static void print_erp(const uint8_t type
, uint8_t len
, const uint8_t *data
)
181 printf(" <no flags>");
183 printf(" NonERP_Present");
185 printf(" Use_Protection");
187 printf(" Barker_Preamble_Mode");
191 static void print_cipher(const uint8_t *data
)
193 if (memcmp(data
, wifi_oui
, 3) == 0) {
196 printf("Use group cipher suite");
211 printf("%.02x-%.02x-%.02x:%d",
212 data
[0], data
[1] ,data
[2], data
[3]);
215 } else if (memcmp(data
, ieee80211_oui
, 3) == 0) {
218 printf("Use group cipher suite");
233 printf("AES-128-CMAC");
236 printf("%.02x-%.02x-%.02x:%d",
237 data
[0], data
[1] ,data
[2], data
[3]);
241 printf("%.02x-%.02x-%.02x:%d",
242 data
[0], data
[1] ,data
[2], data
[3]);
245 static void print_auth(const uint8_t *data
)
247 if (memcmp(data
, wifi_oui
, 3) == 0) {
250 printf("IEEE 802.1X");
256 printf("%.02x-%.02x-%.02x:%d",
257 data
[0], data
[1] ,data
[2], data
[3]);
260 } else if (memcmp(data
, ieee80211_oui
, 3) == 0) {
263 printf("IEEE 802.1X");
269 printf("FT/IEEE 802.1X");
275 printf("IEEE 802.1X/SHA-256");
278 printf("PSK/SHA-256");
281 printf("%.02x-%.02x-%.02x:%d",
282 data
[0], data
[1] ,data
[2], data
[3]);
286 printf("%.02x-%.02x-%.02x:%d",
287 data
[0], data
[1] ,data
[2], data
[3]);
290 static void print_rsn_ie(const char *defcipher
, const char *defauth
,
291 uint8_t len
, const uint8_t *data
)
294 __u16 version
, count
, capa
;
297 version
= data
[0] + (data
[1] << 8);
298 tab_on_first(&first
);
299 printf("\t * Version: %d\n", version
);
305 tab_on_first(&first
);
306 printf("\t * Group cipher: %s\n", defcipher
);
307 printf("\t * Pairwise ciphers: %s\n", defcipher
);
311 tab_on_first(&first
);
312 printf("\t * Group cipher: ");
320 tab_on_first(&first
);
321 printf("\t * Pairwise ciphers: %s\n", defcipher
);
325 count
= data
[0] | (data
[1] << 8);
326 if (2 + (count
* 4) > len
)
329 tab_on_first(&first
);
330 printf("\t * Pairwise ciphers:");
331 for (i
= 0; i
< count
; i
++) {
333 print_cipher(data
+ 2 + (i
* 4));
337 data
+= 2 + (count
* 4);
338 len
-= 2 + (count
* 4);
341 tab_on_first(&first
);
342 printf("\t * Authentication suites: %s\n", defauth
);
346 count
= data
[0] | (data
[1] << 8);
347 if (2 + (count
* 4) > len
)
350 tab_on_first(&first
);
351 printf("\t * Authentication suites:");
352 for (i
= 0; i
< count
; i
++) {
354 print_auth(data
+ 2 + (i
* 4));
358 data
+= 2 + (count
* 4);
359 len
-= 2 + (count
* 4);
362 capa
= data
[0] | (data
[1] << 8);
363 tab_on_first(&first
);
364 printf("\t * Capabilities:");
368 printf(" NoPairwise");
369 switch ((capa
& 0x000c) >> 2) {
373 printf(" 2-PTKSA-RC");
376 printf(" 4-PTKSA-RC");
379 printf(" 16-PTKSA-RC");
382 switch ((capa
& 0x0030) >> 4) {
386 printf(" 2-GTKSA-RC");
389 printf(" 4-GTKSA-RC");
392 printf(" 16-GTKSA-RC");
396 printf(" MFP-required");
398 printf(" MFP-capable");
400 printf(" Peerkey-enabled");
402 printf(" SPP-AMSDU-capable");
404 printf(" SPP-AMSDU-required");
405 printf(" (0x%.4x)\n", capa
);
412 printf("\t\t * bogus tail data (%d):", len
);
414 printf(" %.2x", *data
);
422 static void print_rsn(const uint8_t type
, uint8_t len
, const uint8_t *data
)
424 print_rsn_ie("CCMP", "IEEE 802.1X", len
, data
);
427 static void print_capabilities(const uint8_t type
, uint8_t len
, const uint8_t *data
)
433 for (i
= 0; i
< len
; i
++) {
436 for (bit
= 0; bit
< 8; bit
++) {
437 if (!(data
[i
] & (1 << bit
)))
445 switch (bit
+ base
) {
447 printf(" HT Information Exchange Supported");
450 printf(" On-demand Beacon");
453 printf(" Extended Channel Switching");
456 printf(" Wave Indication");
459 printf(" PSMP Capability");
462 printf(" Service Interval Granularity");
465 printf(" S-PSMP Capability");
479 void (*print
)(const uint8_t type
, uint8_t len
, const uint8_t *data
);
480 uint8_t minlen
, maxlen
;
483 static void print_ie(const struct ie_print
*p
, const uint8_t type
,
484 uint8_t len
, const uint8_t *data
)
491 printf("\t%s:", p
->name
);
492 if (len
< p
->minlen
|| len
> p
->maxlen
) {
494 printf(" <invalid: %d bytes:", len
);
495 for (i
= 0; i
< len
; i
++)
496 printf(" %.02x", data
[i
]);
499 printf(" <invalid: 1 byte: %.02x>\n", data
[0]);
501 printf(" <invalid: no data>\n");
505 p
->print(type
, len
, data
);
508 #define PRINT_IGN { \
515 static const struct ie_print ieprinters
[] = {
516 [0] = { "SSID", print_ssid
, 0, 32, },
517 [1] = { "Supported rates", print_supprates
, 0, 255, },
518 [3] = { "DS Paramater set", print_ds
, 1, 1, },
520 [7] = { "Country", print_country
, 3, 255, },
521 [32] = { "Power constraint", print_powerconstraint
, 1, 1, },
522 [42] = { "ERP", print_erp
, 1, 255, },
523 [48] = { "RSN", print_rsn
, 2, 255, },
524 [50] = { "Extended supported rates", print_supprates
, 0, 255, },
525 [127] = { "Extended capabilities", print_capabilities
, 0, 255, },
528 static void print_wifi_wpa(const uint8_t type
, uint8_t len
, const uint8_t *data
)
530 print_rsn_ie("TKIP", "IEEE 802.1X", len
, data
);
533 static void print_wifi_wmm(const uint8_t type
, uint8_t len
, const uint8_t *data
)
539 printf(" information:");
542 printf(" parameter:");
545 printf(" type %d:", data
[0]);
549 for(i
= 0; i
< len
- 1; i
++)
550 printf(" %.02x", data
[i
+ 1]);
554 static void print_wifi_wps(const uint8_t type
, uint8_t len
, const uint8_t *data
)
557 __u16 subtype
, sublen
;
560 subtype
= (data
[0] << 8) + data
[1];
561 sublen
= (data
[2] << 8) + data
[3];
567 tab_on_first(&first
);
568 printf("\t * Version: %d.%d\n", data
[4] >> 4, data
[4] & 0xF);
571 tab_on_first(&first
);
572 printf("\t * Device name: %.*s\n", sublen
, data
+ 4);
575 tab_on_first(&first
);
576 printf("\t * Manufacturer: %.*s\n", sublen
, data
+ 4);
579 tab_on_first(&first
);
580 printf("\t * Model: %.*s\n", sublen
, data
+ 4);
583 __u16 val
= (data
[4] << 8) | data
[5];
584 tab_on_first(&first
);
585 printf("\t * AP setup locked: 0x%.4x\n", val
);
589 __u16 meth
= (data
[4] << 8) + data
[5];
591 tab_on_first(&first
);
592 printf("\t * Config methods:");
593 #define T(bit, name) do { \
594 if (meth & (1<<bit)) { \
622 printf("\t\t * bogus tail data (%d):", len
);
624 printf(" %.2x", *data
);
632 static const struct ie_print wifiprinters
[] = {
633 [1] = { "WPA", print_wifi_wpa
, 2, 255, },
634 [2] = { "WMM", print_wifi_wmm
, 1, 255, },
635 [4] = { "WPS", print_wifi_wps
, 0, 255, },
638 static void print_vendor(unsigned char len
, unsigned char *data
,
639 struct scan_params
*params
)
644 printf("\tVendor specific: <too short> data:");
645 for(i
= 0; i
< len
; i
++)
646 printf(" %.02x", data
[i
]);
651 if (len
>= 4 && memcmp(data
, wifi_oui
, 3) == 0) {
652 if (data
[3] < ARRAY_SIZE(wifiprinters
) && wifiprinters
[data
[3]].name
) {
653 print_ie(&wifiprinters
[data
[3]], data
[3], len
- 4, data
+ 4);
656 if (!params
->unknown
)
658 printf("\tWiFi OUI %#.2x, data:", data
[3]);
659 for(i
= 0; i
< len
- 4; i
++)
660 printf(" %.02x", data
[i
+ 4]);
665 if (!params
->unknown
)
668 printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:",
669 data
[0], data
[1], data
[2]);
670 for (i
= 3; i
< len
; i
++)
671 printf(" %.2x", data
[i
]);
675 static void print_ies(unsigned char *ie
, int ielen
, struct scan_params
*params
)
677 while (ielen
>= 2 && ielen
>= ie
[1]) {
678 if (ie
[0] < ARRAY_SIZE(ieprinters
) && ieprinters
[ie
[0]].name
) {
679 print_ie(&ieprinters
[ie
[0]], ie
[0], ie
[1], ie
+ 2);
680 } else if (ie
[0] == 221 /* vendor */) {
681 print_vendor(ie
[1], ie
+ 2, params
);
682 } else if (params
->unknown
) {
685 printf("\tUnknown IE (%d):", ie
[0]);
686 for (i
=0; i
<ie
[1]; i
++)
687 printf(" %.2x", ie
[2+i
]);
695 static int print_bss_handler(struct nl_msg
*msg
, void *arg
)
697 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
698 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
699 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
700 char mac_addr
[20], dev
[20];
701 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
702 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
703 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
704 [NL80211_BSS_BSSID
] = { },
705 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
706 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
707 [NL80211_BSS_INFORMATION_ELEMENTS
] = { },
708 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
709 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
712 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
713 genlmsg_attrlen(gnlh
, 0), NULL
);
715 if (!tb
[NL80211_ATTR_BSS
]) {
716 fprintf(stderr
, "bss info missing!");
719 if (nla_parse_nested(bss
, NL80211_BSS_MAX
,
720 tb
[NL80211_ATTR_BSS
],
722 fprintf(stderr
, "failed to parse nested attributes!");
726 if (!bss
[NL80211_BSS_BSSID
])
729 mac_addr_n2a(mac_addr
, nla_data(bss
[NL80211_BSS_BSSID
]));
730 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), dev
);
731 printf("BSS %s (on %s)\n", mac_addr
, dev
);
733 if (bss
[NL80211_BSS_TSF
]) {
734 unsigned long long tsf
;
735 tsf
= (unsigned long long)nla_get_u64(bss
[NL80211_BSS_TSF
]);
736 printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n",
737 tsf
, tsf
/1000/1000/60/60/24, (tsf
/1000/1000/60/60) % 24,
738 (tsf
/1000/1000/60) % 60, (tsf
/1000/1000) % 60);
740 if (bss
[NL80211_BSS_FREQUENCY
])
741 printf("\tfreq: %d\n",
742 nla_get_u32(bss
[NL80211_BSS_FREQUENCY
]));
743 if (bss
[NL80211_BSS_BEACON_INTERVAL
])
744 printf("\tbeacon interval: %d\n",
745 nla_get_u16(bss
[NL80211_BSS_BEACON_INTERVAL
]));
746 if (bss
[NL80211_BSS_CAPABILITY
]) {
747 __u16 capa
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
748 printf("\tcapability:");
749 if (capa
& WLAN_CAPABILITY_ESS
)
751 if (capa
& WLAN_CAPABILITY_IBSS
)
753 if (capa
& WLAN_CAPABILITY_PRIVACY
)
755 if (capa
& WLAN_CAPABILITY_SHORT_PREAMBLE
)
756 printf(" ShortPreamble");
757 if (capa
& WLAN_CAPABILITY_PBCC
)
759 if (capa
& WLAN_CAPABILITY_CHANNEL_AGILITY
)
760 printf(" ChannelAgility");
761 if (capa
& WLAN_CAPABILITY_SPECTRUM_MGMT
)
762 printf(" SpectrumMgmt");
763 if (capa
& WLAN_CAPABILITY_QOS
)
765 if (capa
& WLAN_CAPABILITY_SHORT_SLOT_TIME
)
766 printf(" ShortSlotTime");
767 if (capa
& WLAN_CAPABILITY_APSD
)
769 if (capa
& WLAN_CAPABILITY_DSSS_OFDM
)
770 printf(" DSSS-OFDM");
771 printf(" (0x%.4x)\n", capa
);
773 if (bss
[NL80211_BSS_SIGNAL_MBM
]) {
774 int s
= nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]);
775 printf("\tsignal: %d.%.2d dBm\n", s
/100, s
%100);
777 if (bss
[NL80211_BSS_SIGNAL_UNSPEC
]) {
778 unsigned char s
= nla_get_u8(bss
[NL80211_BSS_SIGNAL_UNSPEC
]);
779 printf("\tsignal: %d/100\n", s
);
781 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
782 print_ies(nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]),
783 nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]),
789 static struct scan_params scan_params
;
791 static int handle_scan_dump(struct nl80211_state
*state
,
794 int argc
, char **argv
)
799 scan_params
.unknown
= false;
800 if (argc
== 1 && !strcmp(argv
[0], "-u"))
801 scan_params
.unknown
= true;
803 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_bss_handler
,
807 COMMAND(scan
, dump
, "[-u]",
808 NL80211_CMD_GET_SCAN
, NLM_F_DUMP
, CIB_NETDEV
, handle_scan_dump
,
809 "Dump the current scan results. If -u is specified, print unknown\n"
810 "data in scan results.");
812 static int handle_scan_combined(struct nl80211_state
*state
,
815 int argc
, char **argv
)
818 static char *dump_argv
[] = {
824 static const __u32 cmds
[] = {
825 NL80211_CMD_NEW_SCAN_RESULTS
,
826 NL80211_CMD_SCAN_ABORTED
,
828 int trig_argc
, dump_argc
, err
;
830 if (argc
>= 3 && !strcmp(argv
[2], "-u")) {
836 trig_argc
= 3 + (argc
- 2) + (3 - dump_argc
);
837 trig_argv
= calloc(trig_argc
, sizeof(*trig_argv
));
840 trig_argv
[0] = argv
[0];
841 trig_argv
[1] = "scan";
842 trig_argv
[2] = "trigger";
844 for (i
= 0; i
< argc
- 2 - (dump_argc
- 3); i
++)
845 trig_argv
[i
+ 3] = argv
[i
+ 2 + (dump_argc
- 3)];
846 err
= handle_cmd(state
, II_NETDEV
, trig_argc
, trig_argv
);
852 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
854 * This code has a bug, which requires creating a separate
855 * nl80211 socket to fix:
856 * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or
857 * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel
858 * before (!) we listen to it, because we only start listening
859 * after we send our scan request.
861 * Doing it the other way around has a race condition as well,
862 * if you first open the events socket you may get a notification
863 * for a previous scan.
865 * The only proper way to fix this would be to listen to events
866 * before sending the command, and for the kernel to send the
867 * scan request along with the event, so that you can match up
868 * whether the scan you requested was finished or aborted (this
869 * may result in processing a scan that another application
870 * requested, but that doesn't seem to be a problem).
872 * Alas, the kernel doesn't do that (yet).
875 if (listen_events(state
, ARRAY_SIZE(cmds
), cmds
) ==
876 NL80211_CMD_SCAN_ABORTED
) {
877 printf("scan aborted!\n");
881 dump_argv
[0] = argv
[0];
882 return handle_cmd(state
, II_NETDEV
, dump_argc
, dump_argv
);
884 TOPLEVEL(scan
, "[-u] [freq <freq>*] [ssid <ssid>*|passive]", 0, 0,
885 CIB_NETDEV
, handle_scan_combined
,
886 "Scan on the given frequencies and probe for the given SSIDs\n"
887 "(or wildcard if not given) unless passive scanning is requested.\n"
888 "If -u is specified print unknown data in the scan results.");