2 * iwinfo - Wireless Information Library - Command line frontend
4 * Copyright (C) 2011 Jo-Philipp Wich <xm@subsignal.org>
6 * The iwinfo library is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
10 * The iwinfo library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with the iwinfo library. If not, see http://www.gnu.org/licenses/.
25 static char * format_bssid(unsigned char *mac
)
29 snprintf(buf
, sizeof(buf
), "%02X:%02X:%02X:%02X:%02X:%02X",
30 mac
[0], mac
[1], mac
[2], mac
[3], mac
[4], mac
[5]);
35 static char * format_ssid(char *ssid
)
37 static char buf
[IWINFO_ESSID_MAX_SIZE
+3];
40 snprintf(buf
, sizeof(buf
), "\"%s\"", ssid
);
42 snprintf(buf
, sizeof(buf
), "unknown");
47 static const char *format_band(int band
)
51 name
= iwinfo_band_name(band
);
58 static char * format_channel(int ch
)
63 snprintf(buf
, sizeof(buf
), "unknown");
65 snprintf(buf
, sizeof(buf
), "%d", ch
);
70 static char * format_frequency(int freq
)
75 snprintf(buf
, sizeof(buf
), "unknown");
77 snprintf(buf
, sizeof(buf
), "%.3f GHz", ((float)freq
/ 1000.0));
82 static char * format_freqflags(uint32_t flags
)
84 static char str
[512] = "[";
91 for (i
= 0; i
< IWINFO_FREQ_FLAG_COUNT
; i
++)
93 pos
+= sprintf(pos
, "%s, ", IWINFO_FREQ_FLAG_NAMES
[i
]);
101 static char * format_txpower(int pwr
)
106 snprintf(buf
, sizeof(buf
), "unknown");
108 snprintf(buf
, sizeof(buf
), "%d dBm", pwr
);
113 static char * format_quality(int qual
)
118 snprintf(buf
, sizeof(buf
), "unknown");
120 snprintf(buf
, sizeof(buf
), "%d", qual
);
125 static char * format_quality_max(int qmax
)
130 snprintf(buf
, sizeof(buf
), "unknown");
132 snprintf(buf
, sizeof(buf
), "%d", qmax
);
137 static char * format_signal(int sig
)
142 snprintf(buf
, sizeof(buf
), "unknown");
144 snprintf(buf
, sizeof(buf
), "%d dBm", sig
);
149 static char * format_noise(int noise
)
154 snprintf(buf
, sizeof(buf
), "unknown");
156 snprintf(buf
, sizeof(buf
), "%d dBm", noise
);
161 static char * format_rate(int rate
)
166 snprintf(buf
, sizeof(buf
), "unknown");
168 snprintf(buf
, sizeof(buf
), "%d.%d MBit/s",
169 rate
/ 1000, (rate
% 1000) / 100);
174 static char * format_enc_ciphers(int ciphers
)
176 static char str
[128] = { 0 };
180 for (i
= 0; i
< IWINFO_CIPHER_COUNT
; i
++)
181 if (ciphers
& (1 << i
))
182 pos
+= sprintf(pos
, "%s, ", IWINFO_CIPHER_NAMES
[i
]);
189 static char * format_enc_suites(int suites
)
191 static char str
[64] = { 0 };
195 for (i
= 0; i
< IWINFO_KMGMT_COUNT
; i
++)
196 if (suites
& (1 << i
))
197 pos
+= sprintf(pos
, "%s/", IWINFO_KMGMT_NAMES
[i
]);
204 static char * format_encryption(struct iwinfo_crypto_entry
*c
)
206 static char buf
[512];
212 snprintf(buf
, sizeof(buf
), "unknown");
217 if (c
->auth_algs
&& !c
->wpa_version
)
219 if ((c
->auth_algs
& IWINFO_AUTH_OPEN
) &&
220 (c
->auth_algs
& IWINFO_AUTH_SHARED
))
222 snprintf(buf
, sizeof(buf
), "WEP Open/Shared (%s)",
223 format_enc_ciphers(c
->pair_ciphers
));
225 else if (c
->auth_algs
& IWINFO_AUTH_OPEN
)
227 snprintf(buf
, sizeof(buf
), "WEP Open System (%s)",
228 format_enc_ciphers(c
->pair_ciphers
));
230 else if (c
->auth_algs
& IWINFO_AUTH_SHARED
)
232 snprintf(buf
, sizeof(buf
), "WEP Shared Auth (%s)",
233 format_enc_ciphers(c
->pair_ciphers
));
238 else if (c
->wpa_version
)
240 for (i
= 0, n
= 0; i
< 3; i
++)
241 if (c
->wpa_version
& (1 << i
))
245 pos
+= sprintf(pos
, "mixed ");
247 for (i
= 0; i
< 3; i
++)
248 if (c
->wpa_version
& (1 << i
))
251 pos
+= sprintf(pos
, "WPA%d/", i
+ 1);
253 pos
+= sprintf(pos
, "WPA/");
258 sprintf(pos
, " %s (%s)",
259 format_enc_suites(c
->auth_suites
),
260 format_enc_ciphers(c
->pair_ciphers
| c
->group_ciphers
));
264 snprintf(buf
, sizeof(buf
), "none");
269 snprintf(buf
, sizeof(buf
), "none");
275 static char * format_hwmodes(int modes
)
277 static char buf
[32] = "802.11";
279 if (iwinfo_format_hwmodes(modes
, buf
+ 6, sizeof(buf
) - 6) < 1)
280 snprintf(buf
, sizeof(buf
), "unknown");
285 static char * format_assocrate(struct iwinfo_rate_entry
*r
)
293 snprintf(buf
, sizeof(buf
), "unknown");
297 p
+= snprintf(p
, l
, "%s", format_rate(r
->rate
));
298 l
= sizeof(buf
) - (p
- buf
);
302 p
+= snprintf(p
, l
, ", MCS %d, %dMHz", r
->mcs
, r
->mhz
);
303 l
= sizeof(buf
) - (p
- buf
);
307 p
+= snprintf(p
, l
, ", VHT-MCS %d, %dMHz", r
->mcs
, r
->mhz
);
308 l
= sizeof(buf
) - (p
- buf
);
312 p
+= snprintf(p
, l
, ", VHT-NSS %d", r
->nss
);
313 l
= sizeof(buf
) - (p
- buf
);
318 p
+= snprintf(p
, l
, ", HE-MCS %d, %dMHz", r
->mcs
, r
->mhz
);
319 l
= sizeof(buf
) - (p
- buf
);
321 p
+= snprintf(p
, l
, ", HE-NSS %d", r
->nss
);
322 l
= sizeof(buf
) - (p
- buf
);
324 p
+= snprintf(p
, l
, ", HE-GI %d", r
->he_gi
);
325 l
= sizeof(buf
) - (p
- buf
);
327 p
+= snprintf(p
, l
, ", HE-DCM %d", r
->he_dcm
);
328 l
= sizeof(buf
) - (p
- buf
);
335 static const char* format_chan_width(bool vht
, uint8_t width
)
337 if (!vht
&& width
< ARRAY_SIZE(ht_chan_width
))
338 switch (ht_chan_width
[width
]) {
339 case 20: return "20 MHz";
340 case 2040: return "40 MHz or higher";
343 if (vht
&& width
< ARRAY_SIZE(vht_chan_width
))
344 switch (vht_chan_width
[width
]) {
345 case 40: return "20 or 40 MHz";
346 case 80: return "80 MHz";
347 case 8080: return "80+80 MHz";
348 case 160: return "160 MHz";
355 static const char * print_type(const struct iwinfo_ops
*iw
, const char *ifname
)
357 const char *type
= iwinfo_type(ifname
);
358 return type
? type
: "unknown";
361 static char * print_hardware_id(const struct iwinfo_ops
*iw
, const char *ifname
)
364 struct iwinfo_hardware_id ids
;
366 if (!iw
->hardware_id(ifname
, (char *)&ids
))
368 if (strlen(ids
.compatible
) > 0)
369 snprintf(buf
, sizeof(buf
), "embedded");
370 else if (ids
.vendor_id
== 0 && ids
.device_id
== 0 &&
371 ids
.subsystem_vendor_id
!= 0 && ids
.subsystem_device_id
!= 0)
372 snprintf(buf
, sizeof(buf
), "USB %04X:%04X",
373 ids
.subsystem_vendor_id
, ids
.subsystem_device_id
);
375 snprintf(buf
, sizeof(buf
), "%04X:%04X %04X:%04X",
376 ids
.vendor_id
, ids
.device_id
,
377 ids
.subsystem_vendor_id
, ids
.subsystem_device_id
);
381 snprintf(buf
, sizeof(buf
), "unknown");
387 static char * print_hardware_name(const struct iwinfo_ops
*iw
, const char *ifname
)
389 static char buf
[128];
391 if (iw
->hardware_name(ifname
, buf
))
392 snprintf(buf
, sizeof(buf
), "unknown");
397 static char * print_txpower_offset(const struct iwinfo_ops
*iw
, const char *ifname
)
402 if (iw
->txpower_offset(ifname
, &off
))
403 snprintf(buf
, sizeof(buf
), "unknown");
405 snprintf(buf
, sizeof(buf
), "%d dB", off
);
407 snprintf(buf
, sizeof(buf
), "none");
412 static char * print_frequency_offset(const struct iwinfo_ops
*iw
, const char *ifname
)
417 if (iw
->frequency_offset(ifname
, &off
))
418 snprintf(buf
, sizeof(buf
), "unknown");
420 snprintf(buf
, sizeof(buf
), "%.3f GHz", ((float)off
/ 1000.0));
422 snprintf(buf
, sizeof(buf
), "none");
427 static char * print_ssid(const struct iwinfo_ops
*iw
, const char *ifname
)
429 char buf
[IWINFO_ESSID_MAX_SIZE
+1] = { 0 };
431 if (iw
->ssid(ifname
, buf
))
432 memset(buf
, 0, sizeof(buf
));
434 return format_ssid(buf
);
437 static char * print_bssid(const struct iwinfo_ops
*iw
, const char *ifname
)
439 static char buf
[18] = { 0 };
441 if (iw
->bssid(ifname
, buf
))
442 snprintf(buf
, sizeof(buf
), "00:00:00:00:00:00");
447 static char * print_mode(const struct iwinfo_ops
*iw
, const char *ifname
)
450 static char buf
[128];
452 if (iw
->mode(ifname
, &mode
))
453 mode
= IWINFO_OPMODE_UNKNOWN
;
455 snprintf(buf
, sizeof(buf
), "%s", IWINFO_OPMODE_NAMES
[mode
]);
460 static char * print_channel(const struct iwinfo_ops
*iw
, const char *ifname
)
463 if (iw
->channel(ifname
, &ch
))
466 return format_channel(ch
);
469 static char * print_center_chan1(const struct iwinfo_ops
*iw
, const char *ifname
)
472 if (iw
->center_chan1(ifname
, &ch
))
475 return format_channel(ch
);
478 static char * print_center_chan2(const struct iwinfo_ops
*iw
, const char *ifname
)
481 if (iw
->center_chan2(ifname
, &ch
))
484 return format_channel(ch
);
487 static char * print_frequency(const struct iwinfo_ops
*iw
, const char *ifname
)
490 if (iw
->frequency(ifname
, &freq
))
493 return format_frequency(freq
);
496 static char * print_txpower(const struct iwinfo_ops
*iw
, const char *ifname
)
499 if (iw
->txpower_offset(ifname
, &off
))
502 if (iw
->txpower(ifname
, &pwr
))
507 return format_txpower(pwr
);
510 static char * print_quality(const struct iwinfo_ops
*iw
, const char *ifname
)
513 if (iw
->quality(ifname
, &qual
))
516 return format_quality(qual
);
519 static char * print_quality_max(const struct iwinfo_ops
*iw
, const char *ifname
)
522 if (iw
->quality_max(ifname
, &qmax
))
525 return format_quality_max(qmax
);
528 static char * print_signal(const struct iwinfo_ops
*iw
, const char *ifname
)
531 if (iw
->signal(ifname
, &sig
))
534 return format_signal(sig
);
537 static char * print_noise(const struct iwinfo_ops
*iw
, const char *ifname
)
540 if (iw
->noise(ifname
, &noise
))
543 return format_noise(noise
);
546 static char * print_rate(const struct iwinfo_ops
*iw
, const char *ifname
)
549 if (iw
->bitrate(ifname
, &rate
))
552 return format_rate(rate
);
555 static char * print_encryption(const struct iwinfo_ops
*iw
, const char *ifname
)
557 struct iwinfo_crypto_entry c
= { 0 };
558 if (iw
->encryption(ifname
, (char *)&c
))
559 return format_encryption(NULL
);
561 return format_encryption(&c
);
564 static char * print_hwmodes(const struct iwinfo_ops
*iw
, const char *ifname
)
567 if (iw
->hwmodelist(ifname
, &modes
))
570 return format_hwmodes(modes
);
573 static const char *print_htmode(const struct iwinfo_ops
*iw
, const char *ifname
)
577 if (iw
->htmode(ifname
, &mode
))
580 name
= iwinfo_htmode_name(mode
);
587 static char * print_mbssid_supp(const struct iwinfo_ops
*iw
, const char *ifname
)
592 if (iw
->mbssid_support(ifname
, &supp
))
593 snprintf(buf
, sizeof(buf
), "no");
595 snprintf(buf
, sizeof(buf
), "%s", supp
? "yes" : "no");
600 static char * print_phyname(const struct iwinfo_ops
*iw
, const char *ifname
)
604 if (!iw
->phyname(ifname
, buf
))
611 static void print_info(const struct iwinfo_ops
*iw
, const char *ifname
)
613 printf("%-9s ESSID: %s\n",
615 print_ssid(iw
, ifname
));
616 printf(" Access Point: %s\n",
617 print_bssid(iw
, ifname
));
618 printf(" Mode: %s Channel: %s (%s) HT Mode: %s\n",
619 print_mode(iw
, ifname
),
620 print_channel(iw
, ifname
),
621 print_frequency(iw
, ifname
),
622 print_htmode(iw
, ifname
));
623 if (iw
->center_chan1
!= NULL
) {
624 printf(" Center Channel 1: %s",
625 print_center_chan1(iw
, ifname
));
626 printf(" 2: %s\n", print_center_chan2(iw
, ifname
));
628 printf(" Tx-Power: %s Link Quality: %s/%s\n",
629 print_txpower(iw
, ifname
),
630 print_quality(iw
, ifname
),
631 print_quality_max(iw
, ifname
));
632 printf(" Signal: %s Noise: %s\n",
633 print_signal(iw
, ifname
),
634 print_noise(iw
, ifname
));
635 printf(" Bit Rate: %s\n",
636 print_rate(iw
, ifname
));
637 printf(" Encryption: %s\n",
638 print_encryption(iw
, ifname
));
639 printf(" Type: %s HW Mode(s): %s\n",
640 print_type(iw
, ifname
),
641 print_hwmodes(iw
, ifname
));
642 printf(" Hardware: %s [%s]\n",
643 print_hardware_id(iw
, ifname
),
644 print_hardware_name(iw
, ifname
));
645 printf(" TX power offset: %s\n",
646 print_txpower_offset(iw
, ifname
));
647 printf(" Frequency offset: %s\n",
648 print_frequency_offset(iw
, ifname
));
649 printf(" Supports VAPs: %s PHY name: %s\n",
650 print_mbssid_supp(iw
, ifname
),
651 print_phyname(iw
, ifname
));
655 static void print_scanlist(const struct iwinfo_ops
*iw
, const char *ifname
)
658 char buf
[IWINFO_BUFSIZE
];
659 struct iwinfo_scanlist_entry
*e
;
661 if (iw
->scanlist(ifname
, buf
, &len
))
663 printf("Scanning not possible\n\n");
668 printf("No scan results\n\n");
672 for (i
= 0, x
= 1; i
< len
; i
+= sizeof(struct iwinfo_scanlist_entry
), x
++)
674 e
= (struct iwinfo_scanlist_entry
*) &buf
[i
];
676 printf("Cell %02d - Address: %s\n",
678 format_bssid(e
->mac
));
679 printf(" ESSID: %s\n",
680 format_ssid(e
->ssid
));
681 printf(" Mode: %s Frequency: %s Band: %s Channel: %s\n",
682 IWINFO_OPMODE_NAMES
[e
->mode
],
683 format_frequency(e
->mhz
),
684 format_band(e
->band
),
685 format_channel(e
->channel
));
686 printf(" Signal: %s Quality: %s/%s\n",
687 format_signal(e
->signal
- 0x100),
688 format_quality(e
->quality
),
689 format_quality_max(e
->quality_max
));
690 printf(" Encryption: %s\n",
691 format_encryption(&e
->crypto
));
692 printf(" HT Operation:\n");
693 printf(" Primary Channel: %d\n",
694 e
->ht_chan_info
.primary_chan
);
695 printf(" Secondary Channel Offset: %s\n",
696 ht_secondary_offset
[e
->ht_chan_info
.secondary_chan_off
]);
697 printf(" Channel Width: %s\n",
698 format_chan_width(false, e
->ht_chan_info
.chan_width
));
700 if (e
->vht_chan_info
.center_chan_1
) {
701 printf(" VHT Operation:\n");
702 printf(" Center Frequency 1: %d\n",
703 e
->vht_chan_info
.center_chan_1
);
704 printf(" Center Frequency 2: %d\n",
705 e
->vht_chan_info
.center_chan_2
);
706 printf(" Channel Width: %s\n",
707 format_chan_width(true, e
->vht_chan_info
.chan_width
));
715 static void print_txpwrlist(const struct iwinfo_ops
*iw
, const char *ifname
)
717 int len
, pwr
, off
, i
;
718 char buf
[IWINFO_BUFSIZE
];
719 struct iwinfo_txpwrlist_entry
*e
;
721 if (iw
->txpwrlist(ifname
, buf
, &len
) || len
<= 0)
723 printf("No TX power information available\n");
727 if (iw
->txpower(ifname
, &pwr
))
730 if (iw
->txpower_offset(ifname
, &off
))
733 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_txpwrlist_entry
))
735 e
= (struct iwinfo_txpwrlist_entry
*) &buf
[i
];
737 printf("%s%3d dBm (%4d mW)\n",
738 (pwr
== e
->dbm
) ? "*" : " ",
740 iwinfo_dbm2mw(e
->dbm
+ off
));
745 static void print_freqlist(const struct iwinfo_ops
*iw
, const char *ifname
)
748 char buf
[IWINFO_BUFSIZE
];
749 struct iwinfo_freqlist_entry
*e
;
751 if (iw
->freqlist(ifname
, buf
, &len
) || len
<= 0)
753 printf("No frequency information available\n");
757 if (iw
->frequency(ifname
, &freq
))
760 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_freqlist_entry
))
762 e
= (struct iwinfo_freqlist_entry
*) &buf
[i
];
764 printf("%s %s (Band: %s, Channel %s) %s\n",
765 (freq
== e
->mhz
) ? "*" : " ",
766 format_frequency(e
->mhz
),
767 format_band(e
->band
),
768 format_channel(e
->channel
),
769 format_freqflags(e
->flags
));
774 static void print_assoclist(const struct iwinfo_ops
*iw
, const char *ifname
)
777 char buf
[IWINFO_BUFSIZE
];
778 struct iwinfo_assoclist_entry
*e
;
780 if (iw
->assoclist(ifname
, buf
, &len
))
782 printf("No information available\n");
787 printf("No station connected\n");
791 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_assoclist_entry
))
793 e
= (struct iwinfo_assoclist_entry
*) &buf
[i
];
795 printf("%s %s / %s (SNR %d) %d ms ago\n",
796 format_bssid(e
->mac
),
797 format_signal(e
->signal
),
798 format_noise(e
->noise
),
799 (e
->signal
- e
->noise
),
802 printf(" RX: %-38s %8d Pkts.\n",
803 format_assocrate(&e
->rx_rate
),
807 printf(" TX: %-38s %8d Pkts.\n",
808 format_assocrate(&e
->tx_rate
),
812 printf(" expected throughput: %s\n\n",
813 format_rate(e
->thr
));
818 static char * lookup_country(char *buf
, int len
, int iso3166
)
821 struct iwinfo_country_entry
*c
;
823 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_country_entry
))
825 c
= (struct iwinfo_country_entry
*) &buf
[i
];
827 if (c
->iso3166
== iso3166
)
834 static void print_countrylist(const struct iwinfo_ops
*iw
, const char *ifname
)
837 char buf
[IWINFO_BUFSIZE
];
840 const struct iwinfo_iso3166_label
*l
;
842 if (iw
->countrylist(ifname
, buf
, &len
))
844 printf("No country code information available\n");
848 if (iw
->country(ifname
, curcode
))
849 memset(curcode
, 0, sizeof(curcode
));
851 for (l
= IWINFO_ISO3166_NAMES
; l
->iso3166
; l
++)
853 if ((ccode
= lookup_country(buf
, len
, l
->iso3166
)) != NULL
)
855 printf("%s %4s %c%c\n",
856 strncmp(ccode
, curcode
, 2) ? " " : "*",
857 ccode
, (l
->iso3166
/ 256), (l
->iso3166
% 256));
862 static void print_htmodelist(const struct iwinfo_ops
*iw
, const char *ifname
)
866 if (iw
->htmodelist(ifname
, &htmodes
))
868 printf("No HT mode information available\n");
872 for (i
= 0; i
< IWINFO_HTMODE_COUNT
; i
++)
873 if (htmodes
& (1 << i
))
874 printf("%s ", IWINFO_HTMODE_NAMES
[i
]);
879 static void lookup_phy(const struct iwinfo_ops
*iw
, const char *section
)
881 char buf
[IWINFO_BUFSIZE
];
885 fprintf(stderr
, "Not supported\n");
889 if (iw
->lookup_phy(section
, buf
))
891 fprintf(stderr
, "Phy not found\n");
899 static void lookup_path(const struct iwinfo_ops
*iw
, const char *phy
)
903 if (!iw
->phy_path
|| iw
->phy_path(phy
, &path
) || !path
)
906 printf("%s\n", path
);
909 int main(int argc
, char **argv
)
913 const struct iwinfo_ops
*iw
;
916 if (argc
> 1 && argc
< 3)
920 " iwinfo <device> info\n"
921 " iwinfo <device> scan\n"
922 " iwinfo <device> txpowerlist\n"
923 " iwinfo <device> freqlist\n"
924 " iwinfo <device> assoclist\n"
925 " iwinfo <device> countrylist\n"
926 " iwinfo <device> htmodelist\n"
927 " iwinfo <backend> phyname <section>\n"
935 glob("/sys/class/net/*", 0, NULL
, &globbuf
);
937 for (i
= 0; i
< globbuf
.gl_pathc
; i
++)
939 p
= strrchr(globbuf
.gl_pathv
[i
], '/');
944 iw
= iwinfo_backend(++p
);
959 iw
= iwinfo_backend_by_name(argv
[1]);
963 fprintf(stderr
, "No such wireless backend: %s\n", argv
[1]);
968 if (!strcmp(argv
[2], "path")) {
969 lookup_path(iw
, argv
[3]);
975 lookup_phy(iw
, argv
[3]);
979 fprintf(stderr
, "Unknown command: %s\n", argv
[2]);
986 iw
= iwinfo_backend(argv
[1]);
990 fprintf(stderr
, "No such wireless device: %s\n", argv
[1]);
995 for (i
= 2; i
< argc
; i
++)
1000 print_info(iw
, argv
[1]);
1004 print_scanlist(iw
, argv
[1]);
1008 print_txpwrlist(iw
, argv
[1]);
1012 print_freqlist(iw
, argv
[1]);
1016 print_assoclist(iw
, argv
[1]);
1020 print_countrylist(iw
, argv
[1]);
1024 print_htmodelist(iw
, argv
[1]);
1028 fprintf(stderr
, "Unknown command: %s\n", argv
[i
]);