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_txpower(int pwr
)
87 snprintf(buf
, sizeof(buf
), "unknown");
89 snprintf(buf
, sizeof(buf
), "%d dBm", pwr
);
94 static char * format_quality(int qual
)
99 snprintf(buf
, sizeof(buf
), "unknown");
101 snprintf(buf
, sizeof(buf
), "%d", qual
);
106 static char * format_quality_max(int qmax
)
111 snprintf(buf
, sizeof(buf
), "unknown");
113 snprintf(buf
, sizeof(buf
), "%d", qmax
);
118 static char * format_signal(int sig
)
123 snprintf(buf
, sizeof(buf
), "unknown");
125 snprintf(buf
, sizeof(buf
), "%d dBm", sig
);
130 static char * format_noise(int noise
)
135 snprintf(buf
, sizeof(buf
), "unknown");
137 snprintf(buf
, sizeof(buf
), "%d dBm", noise
);
142 static char * format_rate(int rate
)
147 snprintf(buf
, sizeof(buf
), "unknown");
149 snprintf(buf
, sizeof(buf
), "%d.%d MBit/s",
150 rate
/ 1000, (rate
% 1000) / 100);
155 static char * format_enc_ciphers(int ciphers
)
157 static char str
[128] = { 0 };
161 for (i
= 0; i
< IWINFO_CIPHER_COUNT
; i
++)
162 if (ciphers
& (1 << i
))
163 pos
+= sprintf(pos
, "%s, ", IWINFO_CIPHER_NAMES
[i
]);
170 static char * format_enc_suites(int suites
)
172 static char str
[64] = { 0 };
176 for (i
= 0; i
< IWINFO_KMGMT_COUNT
; i
++)
177 if (suites
& (1 << i
))
178 pos
+= sprintf(pos
, "%s/", IWINFO_KMGMT_NAMES
[i
]);
185 static char * format_encryption(struct iwinfo_crypto_entry
*c
)
187 static char buf
[512];
193 snprintf(buf
, sizeof(buf
), "unknown");
198 if (c
->auth_algs
&& !c
->wpa_version
)
200 if ((c
->auth_algs
& IWINFO_AUTH_OPEN
) &&
201 (c
->auth_algs
& IWINFO_AUTH_SHARED
))
203 snprintf(buf
, sizeof(buf
), "WEP Open/Shared (%s)",
204 format_enc_ciphers(c
->pair_ciphers
));
206 else if (c
->auth_algs
& IWINFO_AUTH_OPEN
)
208 snprintf(buf
, sizeof(buf
), "WEP Open System (%s)",
209 format_enc_ciphers(c
->pair_ciphers
));
211 else if (c
->auth_algs
& IWINFO_AUTH_SHARED
)
213 snprintf(buf
, sizeof(buf
), "WEP Shared Auth (%s)",
214 format_enc_ciphers(c
->pair_ciphers
));
219 else if (c
->wpa_version
)
221 for (i
= 0, n
= 0; i
< 3; i
++)
222 if (c
->wpa_version
& (1 << i
))
226 pos
+= sprintf(pos
, "mixed ");
228 for (i
= 0; i
< 3; i
++)
229 if (c
->wpa_version
& (1 << i
))
232 pos
+= sprintf(pos
, "WPA%d/", i
+ 1);
234 pos
+= sprintf(pos
, "WPA/");
239 sprintf(pos
, " %s (%s)",
240 format_enc_suites(c
->auth_suites
),
241 format_enc_ciphers(c
->pair_ciphers
| c
->group_ciphers
));
245 snprintf(buf
, sizeof(buf
), "none");
250 snprintf(buf
, sizeof(buf
), "none");
256 static char * format_hwmodes(int modes
)
258 static char buf
[32] = "802.11";
260 if (iwinfo_format_hwmodes(modes
, buf
+ 6, sizeof(buf
) - 6) < 1)
261 snprintf(buf
, sizeof(buf
), "unknown");
266 static char * format_assocrate(struct iwinfo_rate_entry
*r
)
274 snprintf(buf
, sizeof(buf
), "unknown");
278 p
+= snprintf(p
, l
, "%s", format_rate(r
->rate
));
279 l
= sizeof(buf
) - (p
- buf
);
283 p
+= snprintf(p
, l
, ", MCS %d, %dMHz", r
->mcs
, r
->mhz
);
284 l
= sizeof(buf
) - (p
- buf
);
288 p
+= snprintf(p
, l
, ", VHT-MCS %d, %dMHz", r
->mcs
, r
->mhz
);
289 l
= sizeof(buf
) - (p
- buf
);
293 p
+= snprintf(p
, l
, ", VHT-NSS %d", r
->nss
);
294 l
= sizeof(buf
) - (p
- buf
);
299 p
+= snprintf(p
, l
, ", HE-MCS %d, %dMHz", r
->mcs
, r
->mhz
);
300 l
= sizeof(buf
) - (p
- buf
);
302 p
+= snprintf(p
, l
, ", HE-NSS %d", r
->nss
);
303 l
= sizeof(buf
) - (p
- buf
);
305 p
+= snprintf(p
, l
, ", HE-GI %d", r
->he_gi
);
306 l
= sizeof(buf
) - (p
- buf
);
308 p
+= snprintf(p
, l
, ", HE-DCM %d", r
->he_dcm
);
309 l
= sizeof(buf
) - (p
- buf
);
316 static const char* format_chan_width(bool vht
, uint8_t width
)
318 if (!vht
&& width
< ARRAY_SIZE(ht_chan_width
))
319 switch (ht_chan_width
[width
]) {
320 case 20: return "20 MHz";
321 case 2040: return "40 MHz or higher";
324 if (vht
&& width
< ARRAY_SIZE(vht_chan_width
))
325 switch (vht_chan_width
[width
]) {
326 case 40: return "20 or 40 MHz";
327 case 80: return "80 MHz";
328 case 8080: return "80+80 MHz";
329 case 160: return "160 MHz";
336 static const char * print_type(const struct iwinfo_ops
*iw
, const char *ifname
)
338 const char *type
= iwinfo_type(ifname
);
339 return type
? type
: "unknown";
342 static char * print_hardware_id(const struct iwinfo_ops
*iw
, const char *ifname
)
345 struct iwinfo_hardware_id ids
;
347 if (!iw
->hardware_id(ifname
, (char *)&ids
))
349 if (strlen(ids
.compatible
) > 0)
350 snprintf(buf
, sizeof(buf
), "embedded");
351 else if (ids
.vendor_id
== 0 && ids
.device_id
== 0 &&
352 ids
.subsystem_vendor_id
!= 0 && ids
.subsystem_device_id
!= 0)
353 snprintf(buf
, sizeof(buf
), "USB %04X:%04X",
354 ids
.subsystem_vendor_id
, ids
.subsystem_device_id
);
356 snprintf(buf
, sizeof(buf
), "%04X:%04X %04X:%04X",
357 ids
.vendor_id
, ids
.device_id
,
358 ids
.subsystem_vendor_id
, ids
.subsystem_device_id
);
362 snprintf(buf
, sizeof(buf
), "unknown");
368 static char * print_hardware_name(const struct iwinfo_ops
*iw
, const char *ifname
)
370 static char buf
[128];
372 if (iw
->hardware_name(ifname
, buf
))
373 snprintf(buf
, sizeof(buf
), "unknown");
378 static char * print_txpower_offset(const struct iwinfo_ops
*iw
, const char *ifname
)
383 if (iw
->txpower_offset(ifname
, &off
))
384 snprintf(buf
, sizeof(buf
), "unknown");
386 snprintf(buf
, sizeof(buf
), "%d dB", off
);
388 snprintf(buf
, sizeof(buf
), "none");
393 static char * print_frequency_offset(const struct iwinfo_ops
*iw
, const char *ifname
)
398 if (iw
->frequency_offset(ifname
, &off
))
399 snprintf(buf
, sizeof(buf
), "unknown");
401 snprintf(buf
, sizeof(buf
), "%.3f GHz", ((float)off
/ 1000.0));
403 snprintf(buf
, sizeof(buf
), "none");
408 static char * print_ssid(const struct iwinfo_ops
*iw
, const char *ifname
)
410 char buf
[IWINFO_ESSID_MAX_SIZE
+1] = { 0 };
412 if (iw
->ssid(ifname
, buf
))
413 memset(buf
, 0, sizeof(buf
));
415 return format_ssid(buf
);
418 static char * print_bssid(const struct iwinfo_ops
*iw
, const char *ifname
)
420 static char buf
[18] = { 0 };
422 if (iw
->bssid(ifname
, buf
))
423 snprintf(buf
, sizeof(buf
), "00:00:00:00:00:00");
428 static char * print_mode(const struct iwinfo_ops
*iw
, const char *ifname
)
431 static char buf
[128];
433 if (iw
->mode(ifname
, &mode
))
434 mode
= IWINFO_OPMODE_UNKNOWN
;
436 snprintf(buf
, sizeof(buf
), "%s", IWINFO_OPMODE_NAMES
[mode
]);
441 static char * print_channel(const struct iwinfo_ops
*iw
, const char *ifname
)
444 if (iw
->channel(ifname
, &ch
))
447 return format_channel(ch
);
450 static char * print_center_chan1(const struct iwinfo_ops
*iw
, const char *ifname
)
453 if (iw
->center_chan1(ifname
, &ch
))
456 return format_channel(ch
);
459 static char * print_center_chan2(const struct iwinfo_ops
*iw
, const char *ifname
)
462 if (iw
->center_chan2(ifname
, &ch
))
465 return format_channel(ch
);
468 static char * print_frequency(const struct iwinfo_ops
*iw
, const char *ifname
)
471 if (iw
->frequency(ifname
, &freq
))
474 return format_frequency(freq
);
477 static char * print_txpower(const struct iwinfo_ops
*iw
, const char *ifname
)
480 if (iw
->txpower_offset(ifname
, &off
))
483 if (iw
->txpower(ifname
, &pwr
))
488 return format_txpower(pwr
);
491 static char * print_quality(const struct iwinfo_ops
*iw
, const char *ifname
)
494 if (iw
->quality(ifname
, &qual
))
497 return format_quality(qual
);
500 static char * print_quality_max(const struct iwinfo_ops
*iw
, const char *ifname
)
503 if (iw
->quality_max(ifname
, &qmax
))
506 return format_quality_max(qmax
);
509 static char * print_signal(const struct iwinfo_ops
*iw
, const char *ifname
)
512 if (iw
->signal(ifname
, &sig
))
515 return format_signal(sig
);
518 static char * print_noise(const struct iwinfo_ops
*iw
, const char *ifname
)
521 if (iw
->noise(ifname
, &noise
))
524 return format_noise(noise
);
527 static char * print_rate(const struct iwinfo_ops
*iw
, const char *ifname
)
530 if (iw
->bitrate(ifname
, &rate
))
533 return format_rate(rate
);
536 static char * print_encryption(const struct iwinfo_ops
*iw
, const char *ifname
)
538 struct iwinfo_crypto_entry c
= { 0 };
539 if (iw
->encryption(ifname
, (char *)&c
))
540 return format_encryption(NULL
);
542 return format_encryption(&c
);
545 static char * print_hwmodes(const struct iwinfo_ops
*iw
, const char *ifname
)
548 if (iw
->hwmodelist(ifname
, &modes
))
551 return format_hwmodes(modes
);
554 static const char *print_htmode(const struct iwinfo_ops
*iw
, const char *ifname
)
558 if (iw
->htmode(ifname
, &mode
))
561 name
= iwinfo_htmode_name(mode
);
568 static char * print_mbssid_supp(const struct iwinfo_ops
*iw
, const char *ifname
)
573 if (iw
->mbssid_support(ifname
, &supp
))
574 snprintf(buf
, sizeof(buf
), "no");
576 snprintf(buf
, sizeof(buf
), "%s", supp
? "yes" : "no");
581 static char * print_phyname(const struct iwinfo_ops
*iw
, const char *ifname
)
585 if (!iw
->phyname(ifname
, buf
))
592 static void print_info(const struct iwinfo_ops
*iw
, const char *ifname
)
594 printf("%-9s ESSID: %s\n",
596 print_ssid(iw
, ifname
));
597 printf(" Access Point: %s\n",
598 print_bssid(iw
, ifname
));
599 printf(" Mode: %s Channel: %s (%s) HT Mode: %s\n",
600 print_mode(iw
, ifname
),
601 print_channel(iw
, ifname
),
602 print_frequency(iw
, ifname
),
603 print_htmode(iw
, ifname
));
604 if (iw
->center_chan1
!= NULL
) {
605 printf(" Center Channel 1: %s",
606 print_center_chan1(iw
, ifname
));
607 printf(" 2: %s\n", print_center_chan2(iw
, ifname
));
609 printf(" Tx-Power: %s Link Quality: %s/%s\n",
610 print_txpower(iw
, ifname
),
611 print_quality(iw
, ifname
),
612 print_quality_max(iw
, ifname
));
613 printf(" Signal: %s Noise: %s\n",
614 print_signal(iw
, ifname
),
615 print_noise(iw
, ifname
));
616 printf(" Bit Rate: %s\n",
617 print_rate(iw
, ifname
));
618 printf(" Encryption: %s\n",
619 print_encryption(iw
, ifname
));
620 printf(" Type: %s HW Mode(s): %s\n",
621 print_type(iw
, ifname
),
622 print_hwmodes(iw
, ifname
));
623 printf(" Hardware: %s [%s]\n",
624 print_hardware_id(iw
, ifname
),
625 print_hardware_name(iw
, ifname
));
626 printf(" TX power offset: %s\n",
627 print_txpower_offset(iw
, ifname
));
628 printf(" Frequency offset: %s\n",
629 print_frequency_offset(iw
, ifname
));
630 printf(" Supports VAPs: %s PHY name: %s\n",
631 print_mbssid_supp(iw
, ifname
),
632 print_phyname(iw
, ifname
));
636 static void print_scanlist(const struct iwinfo_ops
*iw
, const char *ifname
)
639 char buf
[IWINFO_BUFSIZE
];
640 struct iwinfo_scanlist_entry
*e
;
642 if (iw
->scanlist(ifname
, buf
, &len
))
644 printf("Scanning not possible\n\n");
649 printf("No scan results\n\n");
653 for (i
= 0, x
= 1; i
< len
; i
+= sizeof(struct iwinfo_scanlist_entry
), x
++)
655 e
= (struct iwinfo_scanlist_entry
*) &buf
[i
];
657 printf("Cell %02d - Address: %s\n",
659 format_bssid(e
->mac
));
660 printf(" ESSID: %s\n",
661 format_ssid(e
->ssid
));
662 printf(" Mode: %s Frequency: %s Band: %s Channel: %s\n",
663 IWINFO_OPMODE_NAMES
[e
->mode
],
664 format_frequency(e
->mhz
),
665 format_band(e
->band
),
666 format_channel(e
->channel
));
667 printf(" Signal: %s Quality: %s/%s\n",
668 format_signal(e
->signal
- 0x100),
669 format_quality(e
->quality
),
670 format_quality_max(e
->quality_max
));
671 printf(" Encryption: %s\n",
672 format_encryption(&e
->crypto
));
673 printf(" HT Operation:\n");
674 printf(" Primary Channel: %d\n",
675 e
->ht_chan_info
.primary_chan
);
676 printf(" Secondary Channel Offset: %s\n",
677 ht_secondary_offset
[e
->ht_chan_info
.secondary_chan_off
]);
678 printf(" Channel Width: %s\n",
679 format_chan_width(false, e
->ht_chan_info
.chan_width
));
681 if (e
->vht_chan_info
.center_chan_1
) {
682 printf(" VHT Operation:\n");
683 printf(" Center Frequency 1: %d\n",
684 e
->vht_chan_info
.center_chan_1
);
685 printf(" Center Frequency 2: %d\n",
686 e
->vht_chan_info
.center_chan_2
);
687 printf(" Channel Width: %s\n",
688 format_chan_width(true, e
->vht_chan_info
.chan_width
));
696 static void print_txpwrlist(const struct iwinfo_ops
*iw
, const char *ifname
)
698 int len
, pwr
, off
, i
;
699 char buf
[IWINFO_BUFSIZE
];
700 struct iwinfo_txpwrlist_entry
*e
;
702 if (iw
->txpwrlist(ifname
, buf
, &len
) || len
<= 0)
704 printf("No TX power information available\n");
708 if (iw
->txpower(ifname
, &pwr
))
711 if (iw
->txpower_offset(ifname
, &off
))
714 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_txpwrlist_entry
))
716 e
= (struct iwinfo_txpwrlist_entry
*) &buf
[i
];
718 printf("%s%3d dBm (%4d mW)\n",
719 (pwr
== e
->dbm
) ? "*" : " ",
721 iwinfo_dbm2mw(e
->dbm
+ off
));
726 static void print_freqlist(const struct iwinfo_ops
*iw
, const char *ifname
)
729 char buf
[IWINFO_BUFSIZE
];
730 struct iwinfo_freqlist_entry
*e
;
732 if (iw
->freqlist(ifname
, buf
, &len
) || len
<= 0)
734 printf("No frequency information available\n");
738 if (iw
->frequency(ifname
, &freq
))
741 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_freqlist_entry
))
743 e
= (struct iwinfo_freqlist_entry
*) &buf
[i
];
745 printf("%s %s (Band: %s, Channel %s)%s\n",
746 (freq
== e
->mhz
) ? "*" : " ",
747 format_frequency(e
->mhz
),
748 format_band(e
->band
),
749 format_channel(e
->channel
),
750 e
->restricted
? " [restricted]" : "");
755 static void print_assoclist(const struct iwinfo_ops
*iw
, const char *ifname
)
758 char buf
[IWINFO_BUFSIZE
];
759 struct iwinfo_assoclist_entry
*e
;
761 if (iw
->assoclist(ifname
, buf
, &len
))
763 printf("No information available\n");
768 printf("No station connected\n");
772 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_assoclist_entry
))
774 e
= (struct iwinfo_assoclist_entry
*) &buf
[i
];
776 printf("%s %s / %s (SNR %d) %d ms ago\n",
777 format_bssid(e
->mac
),
778 format_signal(e
->signal
),
779 format_noise(e
->noise
),
780 (e
->signal
- e
->noise
),
783 printf(" RX: %-38s %8d Pkts.\n",
784 format_assocrate(&e
->rx_rate
),
788 printf(" TX: %-38s %8d Pkts.\n",
789 format_assocrate(&e
->tx_rate
),
793 printf(" expected throughput: %s\n\n",
794 format_rate(e
->thr
));
799 static char * lookup_country(char *buf
, int len
, int iso3166
)
802 struct iwinfo_country_entry
*c
;
804 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_country_entry
))
806 c
= (struct iwinfo_country_entry
*) &buf
[i
];
808 if (c
->iso3166
== iso3166
)
815 static void print_countrylist(const struct iwinfo_ops
*iw
, const char *ifname
)
818 char buf
[IWINFO_BUFSIZE
];
821 const struct iwinfo_iso3166_label
*l
;
823 if (iw
->countrylist(ifname
, buf
, &len
))
825 printf("No country code information available\n");
829 if (iw
->country(ifname
, curcode
))
830 memset(curcode
, 0, sizeof(curcode
));
832 for (l
= IWINFO_ISO3166_NAMES
; l
->iso3166
; l
++)
834 if ((ccode
= lookup_country(buf
, len
, l
->iso3166
)) != NULL
)
836 printf("%s %4s %c%c\n",
837 strncmp(ccode
, curcode
, 2) ? " " : "*",
838 ccode
, (l
->iso3166
/ 256), (l
->iso3166
% 256));
843 static void print_htmodelist(const struct iwinfo_ops
*iw
, const char *ifname
)
847 if (iw
->htmodelist(ifname
, &htmodes
))
849 printf("No HT mode information available\n");
853 for (i
= 0; i
< IWINFO_HTMODE_COUNT
; i
++)
854 if (htmodes
& (1 << i
))
855 printf("%s ", IWINFO_HTMODE_NAMES
[i
]);
860 static void lookup_phy(const struct iwinfo_ops
*iw
, const char *section
)
862 char buf
[IWINFO_BUFSIZE
];
866 fprintf(stderr
, "Not supported\n");
870 if (iw
->lookup_phy(section
, buf
))
872 fprintf(stderr
, "Phy not found\n");
880 static void lookup_path(const struct iwinfo_ops
*iw
, const char *phy
)
884 if (!iw
->phy_path
|| iw
->phy_path(phy
, &path
) || !path
)
887 printf("%s\n", path
);
890 int main(int argc
, char **argv
)
894 const struct iwinfo_ops
*iw
;
897 if (argc
> 1 && argc
< 3)
901 " iwinfo <device> info\n"
902 " iwinfo <device> scan\n"
903 " iwinfo <device> txpowerlist\n"
904 " iwinfo <device> freqlist\n"
905 " iwinfo <device> assoclist\n"
906 " iwinfo <device> countrylist\n"
907 " iwinfo <device> htmodelist\n"
908 " iwinfo <backend> phyname <section>\n"
916 glob("/sys/class/net/*", 0, NULL
, &globbuf
);
918 for (i
= 0; i
< globbuf
.gl_pathc
; i
++)
920 p
= strrchr(globbuf
.gl_pathv
[i
], '/');
925 iw
= iwinfo_backend(++p
);
940 iw
= iwinfo_backend_by_name(argv
[1]);
944 fprintf(stderr
, "No such wireless backend: %s\n", argv
[1]);
949 if (!strcmp(argv
[2], "path")) {
950 lookup_path(iw
, argv
[3]);
956 lookup_phy(iw
, argv
[3]);
960 fprintf(stderr
, "Unknown command: %s\n", argv
[2]);
967 iw
= iwinfo_backend(argv
[1]);
971 fprintf(stderr
, "No such wireless device: %s\n", argv
[1]);
976 for (i
= 2; i
< argc
; i
++)
981 print_info(iw
, argv
[1]);
985 print_scanlist(iw
, argv
[1]);
989 print_txpwrlist(iw
, argv
[1]);
993 print_freqlist(iw
, argv
[1]);
997 print_assoclist(iw
, argv
[1]);
1001 print_countrylist(iw
, argv
[1]);
1005 print_htmodelist(iw
, argv
[1]);
1009 fprintf(stderr
, "Unknown command: %s\n", argv
[i
]);