blob: 76ef788fb67931b1d0bd6f635d00ba74356e4c07 [file] [log] [blame]
Johannes Berg3563f4c2008-09-19 05:08:11 +02001#include <net/if.h>
2#include <errno.h>
3#include <string.h>
4#include <ctype.h>
Johannes Berg764fe752009-02-12 10:30:32 +01005#include <stdbool.h>
Johannes Berg3563f4c2008-09-19 05:08:11 +02006
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>
12
13#include "nl80211.h"
14#include "iw.h"
15
Marcel Holtmann92a04ec2009-05-04 01:48:45 -070016#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)
29
Marcel Holtmann857d9662009-05-04 01:48:47 -070030static unsigned char wifi_oui[3] = { 0x00, 0x50, 0xf2 };
31static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac };
32
Johannes Berg764fe752009-02-12 10:30:32 +010033struct scan_params {
34 bool unknown;
Johannes Bergfebeb0c2009-07-25 17:31:08 +020035 enum print_ie_type type;
Jouni Malinen575280c2010-01-06 17:53:59 +020036 bool show_both_ie_sets;
Johannes Berg764fe752009-02-12 10:30:32 +010037};
38
Luis R. Rodriguez2b690f02010-02-19 13:55:58 -050039#define IEEE80211_COUNTRY_EXTENSION_ID 201
40
Johannes Berg72725712010-09-22 11:32:54 +020041union ieee80211_country_ie_triplet {
42 struct {
43 __u8 first_channel;
44 __u8 num_channels;
45 __s8 max_power;
46 } __attribute__ ((packed)) chans;
47 struct {
48 __u8 reg_extension_id;
49 __u8 reg_class;
50 __u8 coverage_class;
51 } __attribute__ ((packed)) ext;
Luis R. Rodriguez2b690f02010-02-19 13:55:58 -050052} __attribute__ ((packed));
53
Johannes Berg7c37a242009-04-08 13:13:28 +020054static int handle_scan(struct nl80211_state *state,
55 struct nl_cb *cb,
Johannes Berg3563f4c2008-09-19 05:08:11 +020056 struct nl_msg *msg,
57 int argc, char **argv)
58{
Johannes Berg559a1712009-05-04 14:06:33 +020059 struct nl_msg *ssids = NULL, *freqs = NULL;
60 char *eptr;
Johannes Berg3563f4c2008-09-19 05:08:11 +020061 int err = -ENOBUFS;
Johannes Berg559a1712009-05-04 14:06:33 +020062 int i;
63 enum {
64 NONE,
65 FREQ,
Johannes Berg64797a72010-03-24 23:38:25 -070066 IES,
Johannes Berg559a1712009-05-04 14:06:33 +020067 SSID,
68 DONE,
69 } parse = NONE;
70 int freq;
71 bool passive = false, have_ssids = false, have_freqs = false;
Johannes Berg64797a72010-03-24 23:38:25 -070072 size_t tmp;
73 unsigned char *ies;
Johannes Berg3563f4c2008-09-19 05:08:11 +020074
75 ssids = nlmsg_alloc();
76 if (!ssids)
77 return -ENOMEM;
Johannes Berg559a1712009-05-04 14:06:33 +020078
79 freqs = nlmsg_alloc();
80 if (!freqs) {
81 nlmsg_free(ssids);
82 return -ENOMEM;
83 }
84
85 for (i = 0; i < argc; i++) {
Johannes Berg559a1712009-05-04 14:06:33 +020086 switch (parse) {
87 case NONE:
Johannes Berg64797a72010-03-24 23:38:25 -070088 if (strcmp(argv[i], "freq") == 0) {
89 parse = FREQ;
90 have_freqs = true;
91 break;
92 } else if (strcmp(argv[i], "ies") == 0) {
93 parse = IES;
94 break;
95 } else if (strcmp(argv[i], "ssid") == 0) {
96 parse = SSID;
97 have_ssids = true;
98 break;
99 } else if (strcmp(argv[i], "passive") == 0) {
100 parse = DONE;
101 passive = true;
102 break;
103 }
Johannes Berg559a1712009-05-04 14:06:33 +0200104 case DONE:
105 return 1;
106 case FREQ:
107 freq = strtoul(argv[i], &eptr, 10);
Johannes Bergcc12e892011-03-02 14:45:32 +0100108 if (eptr != argv[i] + strlen(argv[i])) {
109 /* failed to parse as number -- maybe a tag? */
110 i--;
111 parse = NONE;
112 continue;
113 }
Johannes Berg559a1712009-05-04 14:06:33 +0200114 NLA_PUT_U32(freqs, i, freq);
Johannes Berg64797a72010-03-24 23:38:25 -0700115 break;
116 case IES:
117 ies = parse_hex(argv[i], &tmp);
118 if (!ies)
119 goto nla_put_failure;
120 NLA_PUT(msg, NL80211_ATTR_IE, tmp, ies);
121 free(ies);
122 parse = NONE;
Johannes Berg559a1712009-05-04 14:06:33 +0200123 break;
124 case SSID:
125 NLA_PUT(ssids, i, strlen(argv[i]), argv[i]);
126 break;
127 }
128 }
129
130 if (!have_ssids)
131 NLA_PUT(ssids, 1, 0, "");
132 if (!passive)
133 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
134
135 if (have_freqs)
136 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
Johannes Berg3563f4c2008-09-19 05:08:11 +0200137
138 err = 0;
139 nla_put_failure:
140 nlmsg_free(ssids);
Johannes Berg559a1712009-05-04 14:06:33 +0200141 nlmsg_free(freqs);
Johannes Berg3563f4c2008-09-19 05:08:11 +0200142 return err;
143}
Johannes Berg3563f4c2008-09-19 05:08:11 +0200144
Marcel Holtmann857d9662009-05-04 01:48:47 -0700145static void tab_on_first(bool *first)
146{
147 if (!*first)
148 printf("\t");
149 else
150 *first = false;
151}
152
Johannes Berg83b49342009-05-04 13:35:22 +0200153static void print_ssid(const uint8_t type, uint8_t len, const uint8_t *data)
Johannes Berg3563f4c2008-09-19 05:08:11 +0200154{
Johannes Berg83b49342009-05-04 13:35:22 +0200155 printf(" ");
Johannes Berg748f8482009-05-24 16:48:17 +0200156 print_ssid_escaped(len, data);
Johannes Berg3563f4c2008-09-19 05:08:11 +0200157 printf("\n");
158}
159
Johannes Berg83b49342009-05-04 13:35:22 +0200160static void print_supprates(const uint8_t type, uint8_t len, const uint8_t *data)
Johannes Berg3563f4c2008-09-19 05:08:11 +0200161{
162 int i;
163
Johannes Berg83b49342009-05-04 13:35:22 +0200164 printf(" ");
Johannes Berg3563f4c2008-09-19 05:08:11 +0200165
Johannes Berg83b49342009-05-04 13:35:22 +0200166 for (i = 0; i < len; i++) {
Johannes Berg3563f4c2008-09-19 05:08:11 +0200167 int r = data[i] & 0x7f;
168 printf("%d.%d%s ", r/2, 5*(r&1), data[i] & 0x80 ? "*":"");
169 }
170 printf("\n");
171}
172
Johannes Berg83b49342009-05-04 13:35:22 +0200173static void print_ds(const uint8_t type, uint8_t len, const uint8_t *data)
Johannes Berg3563f4c2008-09-19 05:08:11 +0200174{
Johannes Berg83b49342009-05-04 13:35:22 +0200175 printf(" channel %d\n", data[0]);
Johannes Berg3563f4c2008-09-19 05:08:11 +0200176}
177
Luis R. Rodriguez2b690f02010-02-19 13:55:58 -0500178static const char *country_env_str(char environment)
179{
180 switch (environment) {
181 case 'I':
182 return "Indoor only";
183 case 'O':
184 return "Outdoor only";
185 case ' ':
186 return "Indoor/Outdoor";
187 default:
188 return "bogus";
189 }
190}
191
Johannes Berg83b49342009-05-04 13:35:22 +0200192static void print_country(const uint8_t type, uint8_t len, const uint8_t *data)
Marcel Holtmannb7e8fa32009-05-04 01:48:43 -0700193{
Johannes Berg83b49342009-05-04 13:35:22 +0200194 printf(" %.*s", 2, data);
Luis R. Rodriguez2b690f02010-02-19 13:55:58 -0500195
196 printf("\tEnvironment: %s\n", country_env_str(data[2]));
197
198 data += 3;
199 len -= 3;
200
201 if (len < 3) {
202 printf("\t\tNo country IE triplets present\n");
203 return;
Marcel Holtmannb7e8fa32009-05-04 01:48:43 -0700204 }
Luis R. Rodriguez2b690f02010-02-19 13:55:58 -0500205
206 while (len >= 3) {
207 int end_channel;
Johannes Berg72725712010-09-22 11:32:54 +0200208 union ieee80211_country_ie_triplet *triplet = (void *) data;
Luis R. Rodriguez2b690f02010-02-19 13:55:58 -0500209
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);
216
217 data += 3;
218 len -= 3;
219 continue;
220 }
221
222 /* 2 GHz */
223 if (triplet->chans.first_channel <= 14)
224 end_channel = triplet->chans.first_channel + (triplet->chans.num_channels - 1);
225 else
226 end_channel = triplet->chans.first_channel + (4 * (triplet->chans.num_channels - 1));
227
Luis R. Rodrigueza9859d32010-10-18 11:37:56 -0700228 printf("\t\tChannels [%d - %d] @ %d dBm\n", triplet->chans.first_channel, end_channel, triplet->chans.max_power);
Luis R. Rodriguez2b690f02010-02-19 13:55:58 -0500229
230 data += 3;
231 len -= 3;
232 }
233
234 return;
Marcel Holtmannb7e8fa32009-05-04 01:48:43 -0700235}
236
Marcel Holtmannd1563a12009-05-04 09:06:07 -0700237static void print_powerconstraint(const uint8_t type, uint8_t len, const uint8_t *data)
238{
239 printf(" %d dB\n", data[0]);
240}
241
Johannes Berg83b49342009-05-04 13:35:22 +0200242static void print_erp(const uint8_t type, uint8_t len, const uint8_t *data)
Marcel Holtmannfc4d1482009-05-04 01:48:42 -0700243{
244 if (data[0] == 0x00)
Johannes Berg83b49342009-05-04 13:35:22 +0200245 printf(" <no flags>");
Marcel Holtmannfc4d1482009-05-04 01:48:42 -0700246 if (data[0] & 0x01)
247 printf(" NonERP_Present");
248 if (data[0] & 0x02)
249 printf(" Use_Protection");
250 if (data[0] & 0x04)
251 printf(" Barker_Preamble_Mode");
252 printf("\n");
253}
254
Johannes Berg83b49342009-05-04 13:35:22 +0200255static void print_cipher(const uint8_t *data)
Marcel Holtmann857d9662009-05-04 01:48:47 -0700256{
257 if (memcmp(data, wifi_oui, 3) == 0) {
258 switch (data[3]) {
Johannes Berg510e0e22009-05-05 10:45:30 +0200259 case 0:
Marcel Holtmann857d9662009-05-04 01:48:47 -0700260 printf("Use group cipher suite");
261 break;
Johannes Berg510e0e22009-05-05 10:45:30 +0200262 case 1:
Marcel Holtmann857d9662009-05-04 01:48:47 -0700263 printf("WEP-40");
264 break;
Johannes Berg510e0e22009-05-05 10:45:30 +0200265 case 2:
Marcel Holtmann857d9662009-05-04 01:48:47 -0700266 printf("TKIP");
267 break;
Johannes Berg510e0e22009-05-05 10:45:30 +0200268 case 4:
Marcel Holtmann857d9662009-05-04 01:48:47 -0700269 printf("CCMP");
270 break;
Johannes Berg510e0e22009-05-05 10:45:30 +0200271 case 5:
Marcel Holtmann857d9662009-05-04 01:48:47 -0700272 printf("WEP-104");
273 break;
274 default:
Johannes Berg332769c2009-05-05 10:43:33 +0200275 printf("%.02x-%.02x-%.02x:%d",
Johannes Berg5594fd22009-05-04 13:06:07 +0200276 data[0], data[1] ,data[2], data[3]);
Marcel Holtmann857d9662009-05-04 01:48:47 -0700277 break;
278 }
279 } else if (memcmp(data, ieee80211_oui, 3) == 0) {
280 switch (data[3]) {
Johannes Berg510e0e22009-05-05 10:45:30 +0200281 case 0:
Marcel Holtmann857d9662009-05-04 01:48:47 -0700282 printf("Use group cipher suite");
283 break;
Johannes Berg510e0e22009-05-05 10:45:30 +0200284 case 1:
Marcel Holtmann857d9662009-05-04 01:48:47 -0700285 printf("WEP-40");
286 break;
Johannes Berg510e0e22009-05-05 10:45:30 +0200287 case 2:
Marcel Holtmann857d9662009-05-04 01:48:47 -0700288 printf("TKIP");
289 break;
Johannes Berg510e0e22009-05-05 10:45:30 +0200290 case 4:
Marcel Holtmann857d9662009-05-04 01:48:47 -0700291 printf("CCMP");
292 break;
Johannes Berg510e0e22009-05-05 10:45:30 +0200293 case 5:
Marcel Holtmann857d9662009-05-04 01:48:47 -0700294 printf("WEP-104");
295 break;
Johannes Berg510e0e22009-05-05 10:45:30 +0200296 case 6:
Marcel Holtmann857d9662009-05-04 01:48:47 -0700297 printf("AES-128-CMAC");
298 break;
299 default:
Johannes Berg332769c2009-05-05 10:43:33 +0200300 printf("%.02x-%.02x-%.02x:%d",
Johannes Berg5594fd22009-05-04 13:06:07 +0200301 data[0], data[1] ,data[2], data[3]);
Marcel Holtmann857d9662009-05-04 01:48:47 -0700302 break;
303 }
304 } else
Johannes Berg332769c2009-05-05 10:43:33 +0200305 printf("%.02x-%.02x-%.02x:%d",
Johannes Berg5594fd22009-05-04 13:06:07 +0200306 data[0], data[1] ,data[2], data[3]);
Marcel Holtmann857d9662009-05-04 01:48:47 -0700307}
308
Johannes Berg83b49342009-05-04 13:35:22 +0200309static void print_auth(const uint8_t *data)
Marcel Holtmann857d9662009-05-04 01:48:47 -0700310{
311 if (memcmp(data, wifi_oui, 3) == 0) {
312 switch (data[3]) {
Johannes Berg510e0e22009-05-05 10:45:30 +0200313 case 1:
Marcel Holtmann857d9662009-05-04 01:48:47 -0700314 printf("IEEE 802.1X");
315 break;
Johannes Berg510e0e22009-05-05 10:45:30 +0200316 case 2:
Marcel Holtmann857d9662009-05-04 01:48:47 -0700317 printf("PSK");
318 break;
319 default:
Johannes Berg332769c2009-05-05 10:43:33 +0200320 printf("%.02x-%.02x-%.02x:%d",
Johannes Berg5594fd22009-05-04 13:06:07 +0200321 data[0], data[1] ,data[2], data[3]);
Marcel Holtmann857d9662009-05-04 01:48:47 -0700322 break;
323 }
324 } else if (memcmp(data, ieee80211_oui, 3) == 0) {
325 switch (data[3]) {
Johannes Berg510e0e22009-05-05 10:45:30 +0200326 case 1:
Marcel Holtmann857d9662009-05-04 01:48:47 -0700327 printf("IEEE 802.1X");
328 break;
Johannes Berg510e0e22009-05-05 10:45:30 +0200329 case 2:
Marcel Holtmann857d9662009-05-04 01:48:47 -0700330 printf("PSK");
331 break;
Johannes Berg0fe1c412009-05-05 11:21:13 +0200332 case 3:
333 printf("FT/IEEE 802.1X");
334 break;
335 case 4:
336 printf("FT/PSK");
337 break;
338 case 5:
339 printf("IEEE 802.1X/SHA-256");
340 break;
341 case 6:
342 printf("PSK/SHA-256");
343 break;
Marcel Holtmann857d9662009-05-04 01:48:47 -0700344 default:
Johannes Berg332769c2009-05-05 10:43:33 +0200345 printf("%.02x-%.02x-%.02x:%d",
Johannes Berg5594fd22009-05-04 13:06:07 +0200346 data[0], data[1] ,data[2], data[3]);
Marcel Holtmann857d9662009-05-04 01:48:47 -0700347 break;
348 }
349 } else
Johannes Berg332769c2009-05-05 10:43:33 +0200350 printf("%.02x-%.02x-%.02x:%d",
Johannes Berg5594fd22009-05-04 13:06:07 +0200351 data[0], data[1] ,data[2], data[3]);
Marcel Holtmann857d9662009-05-04 01:48:47 -0700352}
353
Johannes Berg83b49342009-05-04 13:35:22 +0200354static void print_rsn_ie(const char *defcipher, const char *defauth,
355 uint8_t len, const uint8_t *data)
Marcel Holtmann857d9662009-05-04 01:48:47 -0700356{
357 bool first = true;
358 __u16 version, count, capa;
359 int i;
360
Marcel Holtmann857d9662009-05-04 01:48:47 -0700361 version = data[0] + (data[1] << 8);
362 tab_on_first(&first);
363 printf("\t * Version: %d\n", version);
364
365 data += 2;
366 len -= 2;
367
368 if (len < 4) {
369 tab_on_first(&first);
370 printf("\t * Group cipher: %s\n", defcipher);
371 printf("\t * Pairwise ciphers: %s\n", defcipher);
372 return;
373 }
374
375 tab_on_first(&first);
376 printf("\t * Group cipher: ");
377 print_cipher(data);
378 printf("\n");
379
380 data += 4;
381 len -= 4;
382
383 if (len < 2) {
384 tab_on_first(&first);
385 printf("\t * Pairwise ciphers: %s\n", defcipher);
386 return;
387 }
388
389 count = data[0] | (data[1] << 8);
Marcel Holtmann31d477f2009-05-04 09:49:09 -0700390 if (2 + (count * 4) > len)
391 goto invalid;
392
Marcel Holtmann857d9662009-05-04 01:48:47 -0700393 tab_on_first(&first);
394 printf("\t * Pairwise ciphers:");
Marcel Holtmann31d477f2009-05-04 09:49:09 -0700395 for (i = 0; i < count; i++) {
Marcel Holtmann857d9662009-05-04 01:48:47 -0700396 printf(" ");
397 print_cipher(data + 2 + (i * 4));
398 }
399 printf("\n");
400
401 data += 2 + (count * 4);
402 len -= 2 + (count * 4);
403
404 if (len < 2) {
405 tab_on_first(&first);
406 printf("\t * Authentication suites: %s\n", defauth);
407 return;
408 }
409
410 count = data[0] | (data[1] << 8);
Marcel Holtmann31d477f2009-05-04 09:49:09 -0700411 if (2 + (count * 4) > len)
412 goto invalid;
413
Marcel Holtmann857d9662009-05-04 01:48:47 -0700414 tab_on_first(&first);
415 printf("\t * Authentication suites:");
Johannes Berg83b49342009-05-04 13:35:22 +0200416 for (i = 0; i < count; i++) {
Marcel Holtmann857d9662009-05-04 01:48:47 -0700417 printf(" ");
418 print_auth(data + 2 + (i * 4));
419 }
420 printf("\n");
421
422 data += 2 + (count * 4);
423 len -= 2 + (count * 4);
424
Johannes Berg6a4f24e2009-05-04 18:57:03 +0200425 if (len >= 2) {
426 capa = data[0] | (data[1] << 8);
427 tab_on_first(&first);
Johannes Bergcadbe892009-05-05 11:33:34 +0200428 printf("\t * Capabilities:");
429 if (capa & 0x0001)
430 printf(" PreAuth");
431 if (capa & 0x0002)
432 printf(" NoPairwise");
433 switch ((capa & 0x000c) >> 2) {
434 case 0:
435 break;
436 case 1:
437 printf(" 2-PTKSA-RC");
438 break;
439 case 2:
440 printf(" 4-PTKSA-RC");
441 break;
442 case 3:
443 printf(" 16-PTKSA-RC");
444 break;
445 }
446 switch ((capa & 0x0030) >> 4) {
447 case 0:
448 break;
449 case 1:
450 printf(" 2-GTKSA-RC");
451 break;
452 case 2:
453 printf(" 4-GTKSA-RC");
454 break;
455 case 3:
456 printf(" 16-GTKSA-RC");
457 break;
458 }
459 if (capa & 0x0040)
460 printf(" MFP-required");
461 if (capa & 0x0080)
462 printf(" MFP-capable");
463 if (capa & 0x0200)
464 printf(" Peerkey-enabled");
465 if (capa & 0x0400)
466 printf(" SPP-AMSDU-capable");
467 if (capa & 0x0800)
468 printf(" SPP-AMSDU-required");
469 printf(" (0x%.4x)\n", capa);
Johannes Berg6a4f24e2009-05-04 18:57:03 +0200470 data += 2;
471 len -= 2;
472 }
Marcel Holtmann31d477f2009-05-04 09:49:09 -0700473
Johannes Berg5ba3f522010-09-29 12:45:06 +0200474 if (len >= 2) {
475 int pmkid_count = data[0] | (data[1] << 8);
476
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;
483 } else
484 goto invalid;
485 }
486
487 if (len >= 4) {
488 tab_on_first(&first);
489 printf("\t * Group mgmt cipher suite: ");
490 print_cipher(data);
491 printf("\n");
492 data += 4;
493 len -= 4;
494 }
495
Johannes Bergcadbe892009-05-05 11:33:34 +0200496 invalid:
Marcel Holtmann31d477f2009-05-04 09:49:09 -0700497 if (len != 0) {
498 printf("\t\t * bogus tail data (%d):", len);
499 while (len) {
500 printf(" %.2x", *data);
501 data++;
502 len--;
503 }
504 printf("\n");
505 }
Marcel Holtmann857d9662009-05-04 01:48:47 -0700506}
507
Johannes Berg83b49342009-05-04 13:35:22 +0200508static void print_rsn(const uint8_t type, uint8_t len, const uint8_t *data)
Marcel Holtmann857d9662009-05-04 01:48:47 -0700509{
Johannes Berg83b49342009-05-04 13:35:22 +0200510 print_rsn_ie("CCMP", "IEEE 802.1X", len, data);
Marcel Holtmann857d9662009-05-04 01:48:47 -0700511}
512
Luis R. Rodriguez0c445c22009-12-07 20:55:25 -0500513static void print_ht_capa(const uint8_t type, uint8_t len, const uint8_t *data)
514{
Luis R. Rodriguez357c1a52009-12-07 22:05:42 -0500515 printf("\n");
Johannes Berg7ddfb672009-12-08 10:11:22 +0100516 print_ht_capability(data[0] | (data[1] << 8));
517 print_ampdu_length(data[2] & 3);
Christian Lamparterc79c7462010-06-27 00:51:23 +0200518 print_ampdu_spacing((data[2] >> 2) & 7);
Johannes Berg7ddfb672009-12-08 10:11:22 +0100519 print_ht_mcs(data + 3);
Luis R. Rodriguez0c445c22009-12-07 20:55:25 -0500520}
521
Johannes Bergbe7602f2010-09-01 19:43:54 +0200522static void print_ht_op(const uint8_t type, uint8_t len, const uint8_t *data)
523{
524 static const char *offset[4] = {
525 "no secondary",
526 "above",
527 "[reserved!]",
528 "below",
529 };
530 static const char *protection[4] = {
531 "no",
532 "nonmember",
533 "20 MHz",
534 "non-HT mixed",
535 };
536 static const char *sta_chan_width[2] = {
537 "20 MHz",
538 "any",
539 };
540
541 printf("\n");
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);
556}
557
Johannes Berg83b49342009-05-04 13:35:22 +0200558static void print_capabilities(const uint8_t type, uint8_t len, const uint8_t *data)
Marcel Holtmann9b880b02009-05-04 01:48:44 -0700559{
Johannes Berg31d2d252009-05-05 11:44:04 +0200560 int i, base, bit;
561 bool first = true;
Marcel Holtmann9b880b02009-05-04 01:48:44 -0700562
Johannes Berg31d2d252009-05-05 11:44:04 +0200563
564 for (i = 0; i < len; i++) {
565 base = i * 8;
566
567 for (bit = 0; bit < 8; bit++) {
568 if (!(data[i] & (1 << bit)))
569 continue;
570
571 if (!first)
572 printf(",");
573 else
574 first = false;
575
576 switch (bit + base) {
577 case 0:
578 printf(" HT Information Exchange Supported");
579 break;
580 case 1:
581 printf(" On-demand Beacon");
582 break;
583 case 2:
584 printf(" Extended Channel Switching");
585 break;
586 case 3:
587 printf(" Wave Indication");
588 break;
589 case 4:
590 printf(" PSMP Capability");
591 break;
592 case 5:
593 printf(" Service Interval Granularity");
594 break;
595 case 6:
596 printf(" S-PSMP Capability");
597 break;
598 default:
599 printf(" %d", bit);
600 break;
601 }
602 }
603 }
604
Marcel Holtmann9b880b02009-05-04 01:48:44 -0700605 printf("\n");
606}
607
Jouni Malinen575280c2010-01-06 17:53:59 +0200608static void print_tim(const uint8_t type, uint8_t len, const uint8_t *data)
609{
610 printf(" DTIM Count %u DTIM Period %u Bitmap Control 0x%x "
611 "Bitmap[0] 0x%x",
612 data[0], data[1], data[2], data[3]);
613 if (len - 4)
614 printf(" (+ %u octet%s)", len - 4, len - 4 == 1 ? "" : "s");
615 printf("\n");
616}
617
Johannes Berg83b49342009-05-04 13:35:22 +0200618struct ie_print {
619 const char *name;
620 void (*print)(const uint8_t type, uint8_t len, const uint8_t *data);
621 uint8_t minlen, maxlen;
Johannes Bergfebeb0c2009-07-25 17:31:08 +0200622 uint8_t flags;
Johannes Berg764fe752009-02-12 10:30:32 +0100623};
624
Johannes Berg83b49342009-05-04 13:35:22 +0200625static void print_ie(const struct ie_print *p, const uint8_t type,
626 uint8_t len, const uint8_t *data)
Marcel Holtmann6ff0c932009-05-04 01:48:46 -0700627{
628 int i;
629
Johannes Berg83b49342009-05-04 13:35:22 +0200630 if (!p->print)
631 return;
632
633 printf("\t%s:", p->name);
634 if (len < p->minlen || len > p->maxlen) {
635 if (len > 1) {
636 printf(" <invalid: %d bytes:", len);
637 for (i = 0; i < len; i++)
638 printf(" %.02x", data[i]);
639 printf(">\n");
640 } else if (len)
641 printf(" <invalid: 1 byte: %.02x>\n", data[0]);
642 else
643 printf(" <invalid: no data>\n");
644 return;
645 }
646
647 p->print(type, len, data);
648}
649
650#define PRINT_IGN { \
651 .name = "IGNORE", \
652 .print = NULL, \
653 .minlen = 0, \
654 .maxlen = 255, \
655}
656
657static const struct ie_print ieprinters[] = {
Johannes Bergfebeb0c2009-07-25 17:31:08 +0200658 [0] = { "SSID", print_ssid, 0, 32, BIT(PRINT_SCAN) | BIT(PRINT_LINK), },
659 [1] = { "Supported rates", print_supprates, 0, 255, BIT(PRINT_SCAN), },
Johannes Berg014d5812009-08-10 15:37:20 +0200660 [3] = { "DS Parameter set", print_ds, 1, 1, BIT(PRINT_SCAN), },
Jouni Malinen575280c2010-01-06 17:53:59 +0200661 [5] = { "TIM", print_tim, 4, 255, BIT(PRINT_SCAN), },
Johannes Bergfebeb0c2009-07-25 17:31:08 +0200662 [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), },
Johannes Berga2e61862010-09-01 19:28:22 +0200665 [45] = { "HT capabilities", print_ht_capa, 26, 26, BIT(PRINT_SCAN), },
Johannes Bergbe7602f2010-09-01 19:43:54 +0200666 [61] = { "HT operation", print_ht_op, 22, 22, BIT(PRINT_SCAN), },
Johannes Bergfebeb0c2009-07-25 17:31:08 +0200667 [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), },
Johannes Berg83b49342009-05-04 13:35:22 +0200670};
671
672static void print_wifi_wpa(const uint8_t type, uint8_t len, const uint8_t *data)
673{
674 print_rsn_ie("TKIP", "IEEE 802.1X", len, data);
675}
676
Johannes Berg1cab57e2009-08-10 15:15:49 +0200677static bool print_wifi_wmm_param(const uint8_t *data, uint8_t len)
678{
679 int i;
680 static const char *aci_tbl[] = { "BE", "BK", "VI", "VO" };
681
682 if (len < 19)
683 goto invalid;
684
685 if (data[0] != 1) {
Johannes Bergcee4fe22009-08-10 15:40:11 +0200686 printf("Parameter: not version 1: ");
Johannes Berg1cab57e2009-08-10 15:15:49 +0200687 return false;
688 }
689
Marcel Holtmann89ea7062009-12-19 19:49:24 -0800690 printf("\t * Parameter version 1");
Johannes Berg1cab57e2009-08-10 15:15:49 +0200691
692 data++;
693
694 if (data[0] & 0x80)
Marcel Holtmann89ea7062009-12-19 19:49:24 -0800695 printf("\n\t\t * u-APSD");
Johannes Berg1cab57e2009-08-10 15:15:49 +0200696
697 data += 2;
698
699 for (i = 0; i < 4; i++) {
Marcel Holtmann89ea7062009-12-19 19:49:24 -0800700 printf("\n\t\t * %s:", aci_tbl[(data[0] >> 5) & 3]);
Johannes Berg1cab57e2009-08-10 15:15:49 +0200701 if (data[4] & 0x10)
702 printf(" acm");
703 printf(" CW %d-%d", (1 << (data[1] & 0xf)) - 1,
704 (1 << (data[1] >> 4)) - 1);
Johannes Berga2a4c262009-08-10 16:40:51 +0200705 printf(", AIFSN %d", data[0] & 0xf);
Johannes Berg1cab57e2009-08-10 15:15:49 +0200706 if (data[2] | data[3])
Johannes Bergcee4fe22009-08-10 15:40:11 +0200707 printf(", TXOP %d usec", (data[2] + (data[3] << 8)) * 32);
Johannes Berg1cab57e2009-08-10 15:15:49 +0200708 data += 4;
709 }
710
711 printf("\n");
712 return true;
713
714 invalid:
715 printf("invalid: ");
716 return false;
717}
718
Johannes Berg83b49342009-05-04 13:35:22 +0200719static void print_wifi_wmm(const uint8_t type, uint8_t len, const uint8_t *data)
720{
721 int i;
722
Marcel Holtmann6ff0c932009-05-04 01:48:46 -0700723 switch (data[0]) {
724 case 0x00:
Johannes Berg83b49342009-05-04 13:35:22 +0200725 printf(" information:");
Marcel Holtmann6ff0c932009-05-04 01:48:46 -0700726 break;
727 case 0x01:
Johannes Berg1cab57e2009-08-10 15:15:49 +0200728 if (print_wifi_wmm_param(data + 1, len - 1))
729 return;
Marcel Holtmann6ff0c932009-05-04 01:48:46 -0700730 break;
731 default:
Johannes Berg83b49342009-05-04 13:35:22 +0200732 printf(" type %d:", data[0]);
Marcel Holtmann6ff0c932009-05-04 01:48:46 -0700733 break;
734 }
735
Johannes Berg1cab57e2009-08-10 15:15:49 +0200736 for(i = 1; i < len; i++)
737 printf(" %.02x", data[i]);
Marcel Holtmann6ff0c932009-05-04 01:48:46 -0700738 printf("\n");
739}
740
Jouni Malinena6816962010-04-06 18:03:57 +0300741static const char * wifi_wps_dev_passwd_id(uint16_t id)
742{
743 switch (id) {
744 case 0:
745 return "Default (PIN)";
746 case 1:
747 return "User-specified";
748 case 2:
749 return "Machine-specified";
750 case 3:
751 return "Rekey";
752 case 4:
753 return "PushButton";
754 case 5:
755 return "Registrar-specified";
756 default:
757 return "??";
758 }
759}
760
Johannes Berg83b49342009-05-04 13:35:22 +0200761static void print_wifi_wps(const uint8_t type, uint8_t len, const uint8_t *data)
Johannes Berg4673a892009-04-30 20:05:59 +0200762{
763 bool first = true;
764 __u16 subtype, sublen;
765
Johannes Berg4673a892009-04-30 20:05:59 +0200766 while (len >= 4) {
767 subtype = (data[0] << 8) + data[1];
768 sublen = (data[2] << 8) + data[3];
769 if (sublen > len)
770 break;
771
772 switch (subtype) {
773 case 0x104a:
774 tab_on_first(&first);
Johannes Bergdffc6752009-05-04 13:07:16 +0200775 printf("\t * Version: %d.%d\n", data[4] >> 4, data[4] & 0xF);
Johannes Berg4673a892009-04-30 20:05:59 +0200776 break;
777 case 0x1011:
778 tab_on_first(&first);
779 printf("\t * Device name: %.*s\n", sublen, data + 4);
780 break;
Jouni Malinena6816962010-04-06 18:03:57 +0300781 case 0x1012: {
782 uint16_t id;
783 tab_on_first(&first);
784 if (sublen != 2) {
785 printf("\t * Device Password ID: (invalid "
786 "length %d)\n", sublen);
787 break;
788 }
789 id = data[4] << 8 | data[5];
790 printf("\t * Device Password ID: %u (%s)\n",
791 id, wifi_wps_dev_passwd_id(id));
792 break;
793 }
Johannes Berg4673a892009-04-30 20:05:59 +0200794 case 0x1021:
795 tab_on_first(&first);
796 printf("\t * Manufacturer: %.*s\n", sublen, data + 4);
797 break;
798 case 0x1023:
799 tab_on_first(&first);
800 printf("\t * Model: %.*s\n", sublen, data + 4);
801 break;
Jouni Malinena6816962010-04-06 18:03:57 +0300802 case 0x1024:
803 tab_on_first(&first);
804 printf("\t * Model Number: %.*s\n", sublen, data + 4);
805 break;
806 case 0x103b: {
807 __u8 val = data[4];
808 tab_on_first(&first);
809 printf("\t * Response Type: %d%s\n",
810 val, val == 3 ? " (AP)" : "");
811 break;
812 }
813 case 0x103c: {
814 __u8 val = data[4];
815 tab_on_first(&first);
816 printf("\t * RF Bands: 0x%x\n", val);
817 break;
818 }
819 case 0x1041: {
820 __u8 val = data[4];
821 tab_on_first(&first);
822 printf("\t * Selected Registrar: 0x%x\n", val);
823 break;
824 }
825 case 0x1042:
826 tab_on_first(&first);
827 printf("\t * Serial Number: %.*s\n", sublen, data + 4);
828 break;
829 case 0x1044: {
830 __u8 val = data[4];
831 tab_on_first(&first);
832 printf("\t * Wi-Fi Protected Setup State: %d%s%s\n",
833 val,
834 val == 1 ? " (Unconfigured)" : "",
835 val == 2 ? " (Configured)" : "");
836 break;
837 }
838 case 0x1054: {
839 tab_on_first(&first);
840 if (sublen != 8) {
841 printf("\t * Primary Device Type: (invalid "
842 "length %d)\n", sublen);
843 break;
844 }
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]);
850 break;
851 }
Johannes Berg7ee5a862009-05-01 11:52:51 +0200852 case 0x1057: {
Johannes Bergfe31a222009-06-08 12:46:43 +0200853 __u8 val = data[4];
Johannes Berg7ee5a862009-05-01 11:52:51 +0200854 tab_on_first(&first);
Johannes Bergfe31a222009-06-08 12:46:43 +0200855 printf("\t * AP setup locked: 0x%.2x\n", val);
Johannes Berg7ee5a862009-05-01 11:52:51 +0200856 break;
857 }
Jouni Malinena6816962010-04-06 18:03:57 +0300858 case 0x1008:
859 case 0x1053: {
Johannes Berg4673a892009-04-30 20:05:59 +0200860 __u16 meth = (data[4] << 8) + data[5];
861 bool comma = false;
862 tab_on_first(&first);
Jouni Malinena6816962010-04-06 18:03:57 +0300863 printf("\t * %sConfig methods:",
864 subtype == 0x1053 ? "Selected Registrar ": "");
Johannes Berg4673a892009-04-30 20:05:59 +0200865#define T(bit, name) do { \
866 if (meth & (1<<bit)) { \
867 if (comma) \
868 printf(","); \
869 comma = true; \
870 printf(" " name); \
871 } } while (0)
872 T(0, "USB");
873 T(1, "Ethernet");
874 T(2, "Label");
875 T(3, "Display");
876 T(4, "Ext. NFC");
877 T(5, "Int. NFC");
878 T(6, "NFC Intf.");
879 T(7, "PBC");
880 T(8, "Keypad");
881 printf("\n");
882 break;
883#undef T
884 }
Johannes Berg2650d462010-03-24 23:09:52 -0700885 default: {
886 const __u8 *subdata = data + 4;
887 __u16 tmplen = sublen;
888
889 tab_on_first(&first);
890 printf("\t * Unknown TLV (%#.4x, %d bytes):",
891 subtype, tmplen);
892 while (tmplen) {
893 printf(" %.2x", *subdata);
894 subdata++;
895 tmplen--;
896 }
897 printf("\n");
Johannes Berg4673a892009-04-30 20:05:59 +0200898 break;
899 }
Johannes Berg2650d462010-03-24 23:09:52 -0700900 }
Johannes Berg4673a892009-04-30 20:05:59 +0200901
902 data += sublen + 4;
903 len -= sublen + 4;
904 }
905
906 if (len != 0) {
907 printf("\t\t * bogus tail data (%d):", len);
908 while (len) {
909 printf(" %.2x", *data);
910 data++;
911 len--;
912 }
913 printf("\n");
914 }
915}
916
Johannes Berg83b49342009-05-04 13:35:22 +0200917static const struct ie_print wifiprinters[] = {
Johannes Bergfebeb0c2009-07-25 17:31:08 +0200918 [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), },
Johannes Berg4673a892009-04-30 20:05:59 +0200921};
922
Johannes Berg764fe752009-02-12 10:30:32 +0100923static void print_vendor(unsigned char len, unsigned char *data,
Johannes Bergfebeb0c2009-07-25 17:31:08 +0200924 bool unknown, enum print_ie_type ptype)
Johannes Berg3563f4c2008-09-19 05:08:11 +0200925{
926 int i;
927
Johannes Bergfbf80af2009-04-30 18:49:44 +0200928 if (len < 3) {
Johannes Berg4673a892009-04-30 20:05:59 +0200929 printf("\tVendor specific: <too short> data:");
Johannes Bergfbf80af2009-04-30 18:49:44 +0200930 for(i = 0; i < len; i++)
931 printf(" %.02x", data[i]);
932 printf("\n");
933 return;
934 }
935
Marcel Holtmann857d9662009-05-04 01:48:47 -0700936 if (len >= 4 && memcmp(data, wifi_oui, 3) == 0) {
Johannes Bergfebeb0c2009-07-25 17:31:08 +0200937 if (data[3] < ARRAY_SIZE(wifiprinters) &&
938 wifiprinters[data[3]].name &&
939 wifiprinters[data[3]].flags & BIT(ptype)) {
Johannes Berg83b49342009-05-04 13:35:22 +0200940 print_ie(&wifiprinters[data[3]], data[3], len - 4, data + 4);
941 return;
942 }
Johannes Bergfebeb0c2009-07-25 17:31:08 +0200943 if (!unknown)
Johannes Berg4673a892009-04-30 20:05:59 +0200944 return;
Marcel Holtmann857d9662009-05-04 01:48:47 -0700945 printf("\tWiFi OUI %#.2x, data:", data[3]);
Johannes Berg4673a892009-04-30 20:05:59 +0200946 for(i = 0; i < len - 4; i++)
947 printf(" %.02x", data[i + 4]);
948 printf("\n");
949 return;
950 }
951
Johannes Bergfebeb0c2009-07-25 17:31:08 +0200952 if (!unknown)
Johannes Berg764fe752009-02-12 10:30:32 +0100953 return;
954
Johannes Bergfbf80af2009-04-30 18:49:44 +0200955 printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:",
Johannes Berg3563f4c2008-09-19 05:08:11 +0200956 data[0], data[1], data[2]);
Johannes Bergfbf80af2009-04-30 18:49:44 +0200957 for (i = 3; i < len; i++)
958 printf(" %.2x", data[i]);
Johannes Berg3563f4c2008-09-19 05:08:11 +0200959 printf("\n");
960}
961
Johannes Bergfebeb0c2009-07-25 17:31:08 +0200962void print_ies(unsigned char *ie, int ielen, bool unknown,
963 enum print_ie_type ptype)
Johannes Berg3563f4c2008-09-19 05:08:11 +0200964{
965 while (ielen >= 2 && ielen >= ie[1]) {
Johannes Bergfebeb0c2009-07-25 17:31:08 +0200966 if (ie[0] < ARRAY_SIZE(ieprinters) &&
967 ieprinters[ie[0]].name &&
968 ieprinters[ie[0]].flags & BIT(ptype)) {
Johannes Berg83b49342009-05-04 13:35:22 +0200969 print_ie(&ieprinters[ie[0]], ie[0], ie[1], ie + 2);
Johannes Berg764fe752009-02-12 10:30:32 +0100970 } else if (ie[0] == 221 /* vendor */) {
Johannes Bergfebeb0c2009-07-25 17:31:08 +0200971 print_vendor(ie[1], ie + 2, unknown, ptype);
972 } else if (unknown) {
Johannes Berg3563f4c2008-09-19 05:08:11 +0200973 int i;
974
Johannes Berg8086b702009-04-30 18:57:06 +0200975 printf("\tUnknown IE (%d):", ie[0]);
Johannes Berg3563f4c2008-09-19 05:08:11 +0200976 for (i=0; i<ie[1]; i++)
Johannes Berg8086b702009-04-30 18:57:06 +0200977 printf(" %.2x", ie[2+i]);
Johannes Berg3563f4c2008-09-19 05:08:11 +0200978 printf("\n");
979 }
980 ielen -= ie[1] + 2;
981 ie += ie[1] + 2;
982 }
983}
984
985static int print_bss_handler(struct nl_msg *msg, void *arg)
986{
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] = { },
Johannes Bergf2e17e12009-01-07 22:05:49 +0100998 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
999 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
Johannes Berga56117a2009-07-10 18:31:59 +02001000 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
Holger Schurigc04a78d2009-09-24 11:20:47 +02001001 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
Jouni Malinen575280c2010-01-06 17:53:59 +02001002 [NL80211_BSS_BEACON_IES] = { },
Johannes Berg3563f4c2008-09-19 05:08:11 +02001003 };
Johannes Bergfebeb0c2009-07-25 17:31:08 +02001004 struct scan_params *params = arg;
Johannes Berg1c5bcd92010-02-06 15:16:29 +01001005 int show = params->show_both_ie_sets ? 2 : 1;
Johannes Berg3563f4c2008-09-19 05:08:11 +02001006
1007 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1008 genlmsg_attrlen(gnlh, 0), NULL);
1009
1010 if (!tb[NL80211_ATTR_BSS]) {
Johannes Berg5fe70c02009-11-11 15:18:43 +01001011 fprintf(stderr, "bss info missing!\n");
Johannes Berg3563f4c2008-09-19 05:08:11 +02001012 return NL_SKIP;
1013 }
1014 if (nla_parse_nested(bss, NL80211_BSS_MAX,
1015 tb[NL80211_ATTR_BSS],
1016 bss_policy)) {
Johannes Berg5fe70c02009-11-11 15:18:43 +01001017 fprintf(stderr, "failed to parse nested attributes!\n");
Johannes Berg3563f4c2008-09-19 05:08:11 +02001018 return NL_SKIP;
1019 }
1020
1021 if (!bss[NL80211_BSS_BSSID])
1022 return NL_SKIP;
1023
1024 mac_addr_n2a(mac_addr, nla_data(bss[NL80211_BSS_BSSID]));
1025 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
Johannes Berga56117a2009-07-10 18:31:59 +02001026 printf("BSS %s (on %s)", mac_addr, dev);
1027
1028 if (bss[NL80211_BSS_STATUS]) {
1029 switch (nla_get_u32(bss[NL80211_BSS_STATUS])) {
1030 case NL80211_BSS_STATUS_AUTHENTICATED:
1031 printf(" -- authenticated");
1032 break;
1033 case NL80211_BSS_STATUS_ASSOCIATED:
1034 printf(" -- associated");
1035 break;
1036 case NL80211_BSS_STATUS_IBSS_JOINED:
1037 printf(" -- joined");
1038 break;
1039 default:
1040 printf(" -- unknown status: %d",
1041 nla_get_u32(bss[NL80211_BSS_STATUS]));
1042 break;
1043 }
1044 }
1045 printf("\n");
Johannes Berg3563f4c2008-09-19 05:08:11 +02001046
Johannes Berge7109a82009-02-10 21:14:45 +01001047 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);
1053 }
Johannes Berg3563f4c2008-09-19 05:08:11 +02001054 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]));
Marcel Holtmann92a04ec2009-05-04 01:48:45 -07001060 if (bss[NL80211_BSS_CAPABILITY]) {
1061 __u16 capa = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
1062 printf("\tcapability:");
1063 if (capa & WLAN_CAPABILITY_ESS)
1064 printf(" ESS");
1065 if (capa & WLAN_CAPABILITY_IBSS)
1066 printf(" IBSS");
1067 if (capa & WLAN_CAPABILITY_PRIVACY)
1068 printf(" Privacy");
1069 if (capa & WLAN_CAPABILITY_SHORT_PREAMBLE)
1070 printf(" ShortPreamble");
1071 if (capa & WLAN_CAPABILITY_PBCC)
1072 printf(" 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)
1078 printf(" QoS");
1079 if (capa & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1080 printf(" ShortSlotTime");
1081 if (capa & WLAN_CAPABILITY_APSD)
1082 printf(" APSD");
1083 if (capa & WLAN_CAPABILITY_DSSS_OFDM)
1084 printf(" DSSS-OFDM");
1085 printf(" (0x%.4x)\n", capa);
1086 }
Johannes Bergf2e17e12009-01-07 22:05:49 +01001087 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);
1090 }
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);
1094 }
Holger Schurigc04a78d2009-09-24 11:20:47 +02001095 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);
1098 }
Johannes Berg1c5bcd92010-02-06 15:16:29 +01001099
1100 if (bss[NL80211_BSS_INFORMATION_ELEMENTS] && show--) {
Jouni Malinen575280c2010-01-06 17:53:59 +02001101 if (bss[NL80211_BSS_BEACON_IES])
1102 printf("\tInformation elements from Probe Response "
1103 "frame:\n");
Johannes Berg3563f4c2008-09-19 05:08:11 +02001104 print_ies(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
Johannes Berg764fe752009-02-12 10:30:32 +01001105 nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
Johannes Bergfebeb0c2009-07-25 17:31:08 +02001106 params->unknown, params->type);
Jouni Malinen575280c2010-01-06 17:53:59 +02001107 }
Johannes Berg1c5bcd92010-02-06 15:16:29 +01001108 if (bss[NL80211_BSS_BEACON_IES] && show--) {
Jouni Malinen575280c2010-01-06 17:53:59 +02001109 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);
1113 }
Johannes Berg3563f4c2008-09-19 05:08:11 +02001114
1115 return NL_SKIP;
1116}
1117
Johannes Berg764fe752009-02-12 10:30:32 +01001118static struct scan_params scan_params;
Johannes Berg3563f4c2008-09-19 05:08:11 +02001119
Johannes Berg7c37a242009-04-08 13:13:28 +02001120static int handle_scan_dump(struct nl80211_state *state,
1121 struct nl_cb *cb,
Johannes Berg3563f4c2008-09-19 05:08:11 +02001122 struct nl_msg *msg,
1123 int argc, char **argv)
1124{
Johannes Berg764fe752009-02-12 10:30:32 +01001125 if (argc > 1)
1126 return 1;
1127
Johannes Berg1c5bcd92010-02-06 15:16:29 +01001128 memset(&scan_params, 0, sizeof(scan_params));
1129
Johannes Berg764fe752009-02-12 10:30:32 +01001130 if (argc == 1 && !strcmp(argv[0], "-u"))
1131 scan_params.unknown = true;
Jouni Malinen575280c2010-01-06 17:53:59 +02001132 else if (argc == 1 && !strcmp(argv[0], "-b"))
1133 scan_params.show_both_ie_sets = true;
Johannes Berg764fe752009-02-12 10:30:32 +01001134
Johannes Bergfebeb0c2009-07-25 17:31:08 +02001135 scan_params.type = PRINT_SCAN;
1136
Johannes Berg764fe752009-02-12 10:30:32 +01001137 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_bss_handler,
1138 &scan_params);
Johannes Berg3563f4c2008-09-19 05:08:11 +02001139 return 0;
1140}
Johannes Berga5fe4ef2009-04-08 14:11:47 +02001141
1142static int handle_scan_combined(struct nl80211_state *state,
1143 struct nl_cb *cb,
1144 struct nl_msg *msg,
1145 int argc, char **argv)
1146{
Johannes Berg559a1712009-05-04 14:06:33 +02001147 char **trig_argv;
Johannes Berga5fe4ef2009-04-08 14:11:47 +02001148 static char *dump_argv[] = {
1149 NULL,
1150 "scan",
1151 "dump",
Marcel Holtmann92649ea2009-05-02 13:07:24 -07001152 NULL,
Johannes Berga5fe4ef2009-04-08 14:11:47 +02001153 };
1154 static const __u32 cmds[] = {
1155 NL80211_CMD_NEW_SCAN_RESULTS,
1156 NL80211_CMD_SCAN_ABORTED,
1157 };
Johannes Berg559a1712009-05-04 14:06:33 +02001158 int trig_argc, dump_argc, err;
Johannes Berga5fe4ef2009-04-08 14:11:47 +02001159
Johannes Berg559a1712009-05-04 14:06:33 +02001160 if (argc >= 3 && !strcmp(argv[2], "-u")) {
1161 dump_argc = 4;
1162 dump_argv[3] = "-u";
Jouni Malinen575280c2010-01-06 17:53:59 +02001163 } else if (argc >= 3 && !strcmp(argv[2], "-b")) {
1164 dump_argc = 4;
1165 dump_argv[3] = "-b";
Johannes Berg559a1712009-05-04 14:06:33 +02001166 } else
1167 dump_argc = 3;
1168
1169 trig_argc = 3 + (argc - 2) + (3 - dump_argc);
1170 trig_argv = calloc(trig_argc, sizeof(*trig_argv));
1171 if (!trig_argv)
1172 return -ENOMEM;
Johannes Berga5fe4ef2009-04-08 14:11:47 +02001173 trig_argv[0] = argv[0];
Johannes Berg559a1712009-05-04 14:06:33 +02001174 trig_argv[1] = "scan";
1175 trig_argv[2] = "trigger";
1176 int i;
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);
1180 free(trig_argv);
Johannes Berga5fe4ef2009-04-08 14:11:47 +02001181 if (err)
1182 return err;
1183
Johannes Berg61725db2009-04-23 15:57:38 +02001184 /*
1185 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
1186 *
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.
1193 *
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.
1197 *
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).
1204 *
1205 * Alas, the kernel doesn't do that (yet).
1206 */
1207
Johannes Berga5fe4ef2009-04-08 14:11:47 +02001208 if (listen_events(state, ARRAY_SIZE(cmds), cmds) ==
1209 NL80211_CMD_SCAN_ABORTED) {
1210 printf("scan aborted!\n");
1211 return 0;
1212 }
1213
1214 dump_argv[0] = argv[0];
Marcel Holtmann92649ea2009-05-02 13:07:24 -07001215 return handle_cmd(state, II_NETDEV, dump_argc, dump_argv);
Johannes Berga5fe4ef2009-04-08 14:11:47 +02001216}
Johannes Berg64797a72010-03-24 23:38:25 -07001217TOPLEVEL(scan, "[-u] [freq <freq>*] [ies <hex as 00:11:..>] [ssid <ssid>*|passive]", 0, 0,
Johannes Berg6ca98d22009-05-05 15:00:56 +02001218 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"
Johannes Berg64797a72010-03-24 23:38:25 -07001221 "If -u is specified print unknown data in the scan results.\n"
1222 "Specified (vendor) IEs must be well-formed.");
Johannes Berg4698bfc2009-08-24 12:53:34 +02001223COMMAND(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.");
Johannes Berg64797a72010-03-24 23:38:25 -07001227COMMAND(scan, trigger, "[freq <freq>*] [ies <hex as 00:11:..>] [ssid <ssid>*|passive]",
Johannes Berg4698bfc2009-08-24 12:53:34 +02001228 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.");