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 char * format_channel(int ch
)
52 snprintf(buf
, sizeof(buf
), "unknown");
54 snprintf(buf
, sizeof(buf
), "%d", ch
);
59 static char * format_frequency(int freq
)
64 snprintf(buf
, sizeof(buf
), "unknown");
66 snprintf(buf
, sizeof(buf
), "%.3f GHz", ((float)freq
/ 1000.0));
71 static char * format_txpower(int pwr
)
76 snprintf(buf
, sizeof(buf
), "unknown");
78 snprintf(buf
, sizeof(buf
), "%d dBm", pwr
);
83 static char * format_quality(int qual
)
88 snprintf(buf
, sizeof(buf
), "unknown");
90 snprintf(buf
, sizeof(buf
), "%d", qual
);
95 static char * format_quality_max(int qmax
)
100 snprintf(buf
, sizeof(buf
), "unknown");
102 snprintf(buf
, sizeof(buf
), "%d", qmax
);
107 static char * format_signal(int sig
)
112 snprintf(buf
, sizeof(buf
), "unknown");
114 snprintf(buf
, sizeof(buf
), "%d dBm", sig
);
119 static char * format_noise(int noise
)
124 snprintf(buf
, sizeof(buf
), "unknown");
126 snprintf(buf
, sizeof(buf
), "%d dBm", noise
);
131 static char * format_rate(int rate
)
136 snprintf(buf
, sizeof(buf
), "unknown");
138 snprintf(buf
, sizeof(buf
), "%d.%d MBit/s",
139 rate
/ 1000, (rate
% 1000) / 100);
144 static char * format_enc_ciphers(int ciphers
)
146 static char str
[128] = { 0 };
150 for (i
= 0; i
< IWINFO_CIPHER_COUNT
; i
++)
151 if (ciphers
& (1 << i
))
152 pos
+= sprintf(pos
, "%s, ", IWINFO_CIPHER_NAMES
[i
]);
159 static char * format_enc_suites(int suites
)
161 static char str
[64] = { 0 };
165 for (i
= 0; i
< IWINFO_KMGMT_COUNT
; i
++)
166 if (suites
& (1 << i
))
167 pos
+= sprintf(pos
, "%s/", IWINFO_KMGMT_NAMES
[i
]);
174 static char * format_encryption(struct iwinfo_crypto_entry
*c
)
176 static char buf
[512];
182 snprintf(buf
, sizeof(buf
), "unknown");
187 if (c
->auth_algs
&& !c
->wpa_version
)
189 if ((c
->auth_algs
& IWINFO_AUTH_OPEN
) &&
190 (c
->auth_algs
& IWINFO_AUTH_SHARED
))
192 snprintf(buf
, sizeof(buf
), "WEP Open/Shared (%s)",
193 format_enc_ciphers(c
->pair_ciphers
));
195 else if (c
->auth_algs
& IWINFO_AUTH_OPEN
)
197 snprintf(buf
, sizeof(buf
), "WEP Open System (%s)",
198 format_enc_ciphers(c
->pair_ciphers
));
200 else if (c
->auth_algs
& IWINFO_AUTH_SHARED
)
202 snprintf(buf
, sizeof(buf
), "WEP Shared Auth (%s)",
203 format_enc_ciphers(c
->pair_ciphers
));
208 else if (c
->wpa_version
)
210 for (i
= 0, n
= 0; i
< 3; i
++)
211 if (c
->wpa_version
& (1 << i
))
215 pos
+= sprintf(pos
, "mixed ");
217 for (i
= 0; i
< 3; i
++)
218 if (c
->wpa_version
& (1 << i
))
221 pos
+= sprintf(pos
, "WPA%d/", i
+ 1);
223 pos
+= sprintf(pos
, "WPA/");
228 sprintf(pos
, " %s (%s)",
229 format_enc_suites(c
->auth_suites
),
230 format_enc_ciphers(c
->pair_ciphers
| c
->group_ciphers
));
234 snprintf(buf
, sizeof(buf
), "none");
239 snprintf(buf
, sizeof(buf
), "none");
245 static char * format_hwmodes(int modes
)
247 static char buf
[32] = "802.11";
249 if (iwinfo_format_hwmodes(modes
, buf
+ 6, sizeof(buf
) - 6) < 1)
250 snprintf(buf
, sizeof(buf
), "unknown");
255 static char * format_assocrate(struct iwinfo_rate_entry
*r
)
263 snprintf(buf
, sizeof(buf
), "unknown");
267 p
+= snprintf(p
, l
, "%s", format_rate(r
->rate
));
268 l
= sizeof(buf
) - (p
- buf
);
272 p
+= snprintf(p
, l
, ", MCS %d, %dMHz", r
->mcs
, r
->mhz
);
273 l
= sizeof(buf
) - (p
- buf
);
277 p
+= snprintf(p
, l
, ", VHT-MCS %d, %dMHz", r
->mcs
, r
->mhz
);
278 l
= sizeof(buf
) - (p
- buf
);
282 p
+= snprintf(p
, l
, ", VHT-NSS %d", r
->nss
);
283 l
= sizeof(buf
) - (p
- buf
);
288 p
+= snprintf(p
, l
, ", HE-MCS %d, %dMHz", r
->mcs
, r
->mhz
);
289 l
= sizeof(buf
) - (p
- buf
);
291 p
+= snprintf(p
, l
, ", HE-NSS %d", r
->nss
);
292 l
= sizeof(buf
) - (p
- buf
);
294 p
+= snprintf(p
, l
, ", HE-GI %d", r
->he_gi
);
295 l
= sizeof(buf
) - (p
- buf
);
297 p
+= snprintf(p
, l
, ", HE-DCM %d", r
->he_dcm
);
298 l
= sizeof(buf
) - (p
- buf
);
305 static const char* format_chan_width(bool vht
, uint8_t width
)
307 if (!vht
&& width
< ARRAY_SIZE(ht_chan_width
))
308 switch (ht_chan_width
[width
]) {
309 case 20: return "20 MHz";
310 case 2040: return "40 MHz or higher";
313 if (vht
&& width
< ARRAY_SIZE(vht_chan_width
))
314 switch (vht_chan_width
[width
]) {
315 case 40: return "20 or 40 MHz";
316 case 80: return "80 MHz";
317 case 8080: return "80+80 MHz";
318 case 160: return "160 MHz";
325 static const char * print_type(const struct iwinfo_ops
*iw
, const char *ifname
)
327 const char *type
= iwinfo_type(ifname
);
328 return type
? type
: "unknown";
331 static char * print_hardware_id(const struct iwinfo_ops
*iw
, const char *ifname
)
334 struct iwinfo_hardware_id ids
;
336 if (!iw
->hardware_id(ifname
, (char *)&ids
))
338 snprintf(buf
, sizeof(buf
), "%04X:%04X %04X:%04X",
339 ids
.vendor_id
, ids
.device_id
,
340 ids
.subsystem_vendor_id
, ids
.subsystem_device_id
);
344 snprintf(buf
, sizeof(buf
), "unknown");
350 static char * print_hardware_name(const struct iwinfo_ops
*iw
, const char *ifname
)
352 static char buf
[128];
354 if (iw
->hardware_name(ifname
, buf
))
355 snprintf(buf
, sizeof(buf
), "unknown");
360 static char * print_txpower_offset(const struct iwinfo_ops
*iw
, const char *ifname
)
365 if (iw
->txpower_offset(ifname
, &off
))
366 snprintf(buf
, sizeof(buf
), "unknown");
368 snprintf(buf
, sizeof(buf
), "%d dB", off
);
370 snprintf(buf
, sizeof(buf
), "none");
375 static char * print_frequency_offset(const struct iwinfo_ops
*iw
, const char *ifname
)
380 if (iw
->frequency_offset(ifname
, &off
))
381 snprintf(buf
, sizeof(buf
), "unknown");
383 snprintf(buf
, sizeof(buf
), "%.3f GHz", ((float)off
/ 1000.0));
385 snprintf(buf
, sizeof(buf
), "none");
390 static char * print_ssid(const struct iwinfo_ops
*iw
, const char *ifname
)
392 char buf
[IWINFO_ESSID_MAX_SIZE
+1] = { 0 };
394 if (iw
->ssid(ifname
, buf
))
395 memset(buf
, 0, sizeof(buf
));
397 return format_ssid(buf
);
400 static char * print_bssid(const struct iwinfo_ops
*iw
, const char *ifname
)
402 static char buf
[18] = { 0 };
404 if (iw
->bssid(ifname
, buf
))
405 snprintf(buf
, sizeof(buf
), "00:00:00:00:00:00");
410 static char * print_mode(const struct iwinfo_ops
*iw
, const char *ifname
)
413 static char buf
[128];
415 if (iw
->mode(ifname
, &mode
))
416 mode
= IWINFO_OPMODE_UNKNOWN
;
418 snprintf(buf
, sizeof(buf
), "%s", IWINFO_OPMODE_NAMES
[mode
]);
423 static char * print_channel(const struct iwinfo_ops
*iw
, const char *ifname
)
426 if (iw
->channel(ifname
, &ch
))
429 return format_channel(ch
);
432 static char * print_center_chan1(const struct iwinfo_ops
*iw
, const char *ifname
)
435 if (iw
->center_chan1(ifname
, &ch
))
438 return format_channel(ch
);
441 static char * print_center_chan2(const struct iwinfo_ops
*iw
, const char *ifname
)
444 if (iw
->center_chan2(ifname
, &ch
))
447 return format_channel(ch
);
450 static char * print_frequency(const struct iwinfo_ops
*iw
, const char *ifname
)
453 if (iw
->frequency(ifname
, &freq
))
456 return format_frequency(freq
);
459 static char * print_txpower(const struct iwinfo_ops
*iw
, const char *ifname
)
462 if (iw
->txpower_offset(ifname
, &off
))
465 if (iw
->txpower(ifname
, &pwr
))
470 return format_txpower(pwr
);
473 static char * print_quality(const struct iwinfo_ops
*iw
, const char *ifname
)
476 if (iw
->quality(ifname
, &qual
))
479 return format_quality(qual
);
482 static char * print_quality_max(const struct iwinfo_ops
*iw
, const char *ifname
)
485 if (iw
->quality_max(ifname
, &qmax
))
488 return format_quality_max(qmax
);
491 static char * print_signal(const struct iwinfo_ops
*iw
, const char *ifname
)
494 if (iw
->signal(ifname
, &sig
))
497 return format_signal(sig
);
500 static char * print_noise(const struct iwinfo_ops
*iw
, const char *ifname
)
503 if (iw
->noise(ifname
, &noise
))
506 return format_noise(noise
);
509 static char * print_rate(const struct iwinfo_ops
*iw
, const char *ifname
)
512 if (iw
->bitrate(ifname
, &rate
))
515 return format_rate(rate
);
518 static char * print_encryption(const struct iwinfo_ops
*iw
, const char *ifname
)
520 struct iwinfo_crypto_entry c
= { 0 };
521 if (iw
->encryption(ifname
, (char *)&c
))
522 return format_encryption(NULL
);
524 return format_encryption(&c
);
527 static char * print_hwmodes(const struct iwinfo_ops
*iw
, const char *ifname
)
530 if (iw
->hwmodelist(ifname
, &modes
))
533 return format_hwmodes(modes
);
536 static const char *print_htmode(const struct iwinfo_ops
*iw
, const char *ifname
)
540 if (iw
->htmode(ifname
, &mode
))
543 name
= iwinfo_htmode_name(mode
);
550 static char * print_mbssid_supp(const struct iwinfo_ops
*iw
, const char *ifname
)
555 if (iw
->mbssid_support(ifname
, &supp
))
556 snprintf(buf
, sizeof(buf
), "no");
558 snprintf(buf
, sizeof(buf
), "%s", supp
? "yes" : "no");
563 static char * print_phyname(const struct iwinfo_ops
*iw
, const char *ifname
)
567 if (!iw
->phyname(ifname
, buf
))
574 static void print_info(const struct iwinfo_ops
*iw
, const char *ifname
)
576 printf("%-9s ESSID: %s\n",
578 print_ssid(iw
, ifname
));
579 printf(" Access Point: %s\n",
580 print_bssid(iw
, ifname
));
581 printf(" Mode: %s Channel: %s (%s) HT Mode: %s\n",
582 print_mode(iw
, ifname
),
583 print_channel(iw
, ifname
),
584 print_frequency(iw
, ifname
),
585 print_htmode(iw
, ifname
));
586 if (iw
->center_chan1
!= NULL
) {
587 printf(" Center Channel 1: %s",
588 print_center_chan1(iw
, ifname
));
589 printf(" 2: %s\n", print_center_chan2(iw
, ifname
));
591 printf(" Tx-Power: %s Link Quality: %s/%s\n",
592 print_txpower(iw
, ifname
),
593 print_quality(iw
, ifname
),
594 print_quality_max(iw
, ifname
));
595 printf(" Signal: %s Noise: %s\n",
596 print_signal(iw
, ifname
),
597 print_noise(iw
, ifname
));
598 printf(" Bit Rate: %s\n",
599 print_rate(iw
, ifname
));
600 printf(" Encryption: %s\n",
601 print_encryption(iw
, ifname
));
602 printf(" Type: %s HW Mode(s): %s\n",
603 print_type(iw
, ifname
),
604 print_hwmodes(iw
, ifname
));
605 printf(" Hardware: %s [%s]\n",
606 print_hardware_id(iw
, ifname
),
607 print_hardware_name(iw
, ifname
));
608 printf(" TX power offset: %s\n",
609 print_txpower_offset(iw
, ifname
));
610 printf(" Frequency offset: %s\n",
611 print_frequency_offset(iw
, ifname
));
612 printf(" Supports VAPs: %s PHY name: %s\n",
613 print_mbssid_supp(iw
, ifname
),
614 print_phyname(iw
, ifname
));
618 static void print_scanlist(const struct iwinfo_ops
*iw
, const char *ifname
)
621 char buf
[IWINFO_BUFSIZE
];
622 struct iwinfo_scanlist_entry
*e
;
624 if (iw
->scanlist(ifname
, buf
, &len
))
626 printf("Scanning not possible\n\n");
631 printf("No scan results\n\n");
635 for (i
= 0, x
= 1; i
< len
; i
+= sizeof(struct iwinfo_scanlist_entry
), x
++)
637 e
= (struct iwinfo_scanlist_entry
*) &buf
[i
];
639 printf("Cell %02d - Address: %s\n",
641 format_bssid(e
->mac
));
642 printf(" ESSID: %s\n",
643 format_ssid(e
->ssid
));
644 printf(" Mode: %s Channel: %s\n",
645 IWINFO_OPMODE_NAMES
[e
->mode
],
646 format_channel(e
->channel
));
647 printf(" Signal: %s Quality: %s/%s\n",
648 format_signal(e
->signal
- 0x100),
649 format_quality(e
->quality
),
650 format_quality_max(e
->quality_max
));
651 printf(" Encryption: %s\n",
652 format_encryption(&e
->crypto
));
653 printf(" HT Operation:\n");
654 printf(" Primary Channel: %d\n",
655 e
->ht_chan_info
.primary_chan
);
656 printf(" Secondary Channel Offset: %s\n",
657 ht_secondary_offset
[e
->ht_chan_info
.secondary_chan_off
]);
658 printf(" Channel Width: %s\n",
659 format_chan_width(false, e
->ht_chan_info
.chan_width
));
661 if (e
->vht_chan_info
.center_chan_1
) {
662 printf(" VHT Operation:\n");
663 printf(" Center Frequency 1: %d\n",
664 e
->vht_chan_info
.center_chan_1
);
665 printf(" Center Frequency 2: %d\n",
666 e
->vht_chan_info
.center_chan_2
);
667 printf(" Channel Width: %s\n",
668 format_chan_width(true, e
->vht_chan_info
.chan_width
));
676 static void print_txpwrlist(const struct iwinfo_ops
*iw
, const char *ifname
)
678 int len
, pwr
, off
, i
;
679 char buf
[IWINFO_BUFSIZE
];
680 struct iwinfo_txpwrlist_entry
*e
;
682 if (iw
->txpwrlist(ifname
, buf
, &len
) || len
<= 0)
684 printf("No TX power information available\n");
688 if (iw
->txpower(ifname
, &pwr
))
691 if (iw
->txpower_offset(ifname
, &off
))
694 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_txpwrlist_entry
))
696 e
= (struct iwinfo_txpwrlist_entry
*) &buf
[i
];
698 printf("%s%3d dBm (%4d mW)\n",
699 (pwr
== e
->dbm
) ? "*" : " ",
701 iwinfo_dbm2mw(e
->dbm
+ off
));
706 static void print_freqlist(const struct iwinfo_ops
*iw
, const char *ifname
)
709 char buf
[IWINFO_BUFSIZE
];
710 struct iwinfo_freqlist_entry
*e
;
712 if (iw
->freqlist(ifname
, buf
, &len
) || len
<= 0)
714 printf("No frequency information available\n");
718 if (iw
->frequency(ifname
, &freq
))
721 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_freqlist_entry
))
723 e
= (struct iwinfo_freqlist_entry
*) &buf
[i
];
725 printf("%s %s (Channel %s)%s\n",
726 (freq
== e
->mhz
) ? "*" : " ",
727 format_frequency(e
->mhz
),
728 format_channel(e
->channel
),
729 e
->restricted
? " [restricted]" : "");
734 static void print_assoclist(const struct iwinfo_ops
*iw
, const char *ifname
)
737 char buf
[IWINFO_BUFSIZE
];
738 struct iwinfo_assoclist_entry
*e
;
740 if (iw
->assoclist(ifname
, buf
, &len
))
742 printf("No information available\n");
747 printf("No station connected\n");
751 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_assoclist_entry
))
753 e
= (struct iwinfo_assoclist_entry
*) &buf
[i
];
755 printf("%s %s / %s (SNR %d) %d ms ago\n",
756 format_bssid(e
->mac
),
757 format_signal(e
->signal
),
758 format_noise(e
->noise
),
759 (e
->signal
- e
->noise
),
762 printf(" RX: %-38s %8d Pkts.\n",
763 format_assocrate(&e
->rx_rate
),
767 printf(" TX: %-38s %8d Pkts.\n",
768 format_assocrate(&e
->tx_rate
),
772 printf(" expected throughput: %s\n\n",
773 format_rate(e
->thr
));
778 static char * lookup_country(char *buf
, int len
, int iso3166
)
781 struct iwinfo_country_entry
*c
;
783 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_country_entry
))
785 c
= (struct iwinfo_country_entry
*) &buf
[i
];
787 if (c
->iso3166
== iso3166
)
794 static void print_countrylist(const struct iwinfo_ops
*iw
, const char *ifname
)
797 char buf
[IWINFO_BUFSIZE
];
800 const struct iwinfo_iso3166_label
*l
;
802 if (iw
->countrylist(ifname
, buf
, &len
))
804 printf("No country code information available\n");
808 if (iw
->country(ifname
, curcode
))
809 memset(curcode
, 0, sizeof(curcode
));
811 for (l
= IWINFO_ISO3166_NAMES
; l
->iso3166
; l
++)
813 if ((ccode
= lookup_country(buf
, len
, l
->iso3166
)) != NULL
)
815 printf("%s %4s %c%c\n",
816 strncmp(ccode
, curcode
, 2) ? " " : "*",
817 ccode
, (l
->iso3166
/ 256), (l
->iso3166
% 256));
822 static void print_htmodelist(const struct iwinfo_ops
*iw
, const char *ifname
)
826 if (iw
->htmodelist(ifname
, &htmodes
))
828 printf("No HT mode information available\n");
832 for (i
= 0; i
< IWINFO_HTMODE_COUNT
; i
++)
833 if (htmodes
& (1 << i
))
834 printf("%s ", IWINFO_HTMODE_NAMES
[i
]);
839 static void lookup_phy(const struct iwinfo_ops
*iw
, const char *section
)
841 char buf
[IWINFO_BUFSIZE
];
845 fprintf(stderr
, "Not supported\n");
849 if (iw
->lookup_phy(section
, buf
))
851 fprintf(stderr
, "Phy not found\n");
859 static void lookup_path(const struct iwinfo_ops
*iw
, const char *phy
)
863 if (!iw
->phy_path
|| iw
->phy_path(phy
, &path
) || !path
)
866 printf("%s\n", path
);
869 int main(int argc
, char **argv
)
873 const struct iwinfo_ops
*iw
;
876 if (argc
> 1 && argc
< 3)
880 " iwinfo <device> info\n"
881 " iwinfo <device> scan\n"
882 " iwinfo <device> txpowerlist\n"
883 " iwinfo <device> freqlist\n"
884 " iwinfo <device> assoclist\n"
885 " iwinfo <device> countrylist\n"
886 " iwinfo <device> htmodelist\n"
887 " iwinfo <backend> phyname <section>\n"
895 glob("/sys/class/net/*", 0, NULL
, &globbuf
);
897 for (i
= 0; i
< globbuf
.gl_pathc
; i
++)
899 p
= strrchr(globbuf
.gl_pathv
[i
], '/');
904 iw
= iwinfo_backend(++p
);
919 iw
= iwinfo_backend_by_name(argv
[1]);
923 fprintf(stderr
, "No such wireless backend: %s\n", argv
[1]);
928 if (!strcmp(argv
[2], "path")) {
929 lookup_path(iw
, argv
[3]);
935 lookup_phy(iw
, argv
[3]);
939 fprintf(stderr
, "Unknown command: %s\n", argv
[2]);
946 iw
= iwinfo_backend(argv
[1]);
950 fprintf(stderr
, "No such wireless device: %s\n", argv
[1]);
955 for (i
= 2; i
< argc
; i
++)
960 print_info(iw
, argv
[1]);
964 print_scanlist(iw
, argv
[1]);
968 print_txpwrlist(iw
, argv
[1]);
972 print_freqlist(iw
, argv
[1]);
976 print_assoclist(iw
, argv
[1]);
980 print_countrylist(iw
, argv
[1]);
984 print_htmodelist(iw
, argv
[1]);
988 fprintf(stderr
, "Unknown command: %s\n", argv
[i
]);