ca003c4c2ab8be1be760bae93b8f803bd7543c34
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 };
149 if (ciphers
& IWINFO_CIPHER_WEP40
)
150 pos
+= sprintf(pos
, "WEP-40, ");
152 if (ciphers
& IWINFO_CIPHER_WEP104
)
153 pos
+= sprintf(pos
, "WEP-104, ");
155 if (ciphers
& IWINFO_CIPHER_TKIP
)
156 pos
+= sprintf(pos
, "TKIP, ");
158 if (ciphers
& IWINFO_CIPHER_CCMP
)
159 pos
+= sprintf(pos
, "CCMP, ");
161 if (ciphers
& IWINFO_CIPHER_WRAP
)
162 pos
+= sprintf(pos
, "WRAP, ");
164 if (ciphers
& IWINFO_CIPHER_AESOCB
)
165 pos
+= sprintf(pos
, "AES-OCB, ");
167 if (ciphers
& IWINFO_CIPHER_CKIP
)
168 pos
+= sprintf(pos
, "CKIP, ");
170 if (!ciphers
|| (ciphers
& IWINFO_CIPHER_NONE
))
171 pos
+= sprintf(pos
, "NONE, ");
178 static char * format_enc_suites(int suites
)
180 static char str
[64] = { 0 };
183 if (suites
& IWINFO_KMGMT_PSK
)
184 pos
+= sprintf(pos
, "PSK/");
186 if (suites
& IWINFO_KMGMT_8021x
)
187 pos
+= sprintf(pos
, "802.1X/");
189 if (suites
& IWINFO_KMGMT_SAE
)
190 pos
+= sprintf(pos
, "SAE/");
192 if (suites
& IWINFO_KMGMT_OWE
)
193 pos
+= sprintf(pos
, "OWE/");
195 if (!suites
|| (suites
& IWINFO_KMGMT_NONE
))
196 pos
+= sprintf(pos
, "NONE/");
203 static char * format_encryption(struct iwinfo_crypto_entry
*c
)
205 static char buf
[512];
211 snprintf(buf
, sizeof(buf
), "unknown");
216 if (c
->auth_algs
&& !c
->wpa_version
)
218 if ((c
->auth_algs
& IWINFO_AUTH_OPEN
) &&
219 (c
->auth_algs
& IWINFO_AUTH_SHARED
))
221 snprintf(buf
, sizeof(buf
), "WEP Open/Shared (%s)",
222 format_enc_ciphers(c
->pair_ciphers
));
224 else if (c
->auth_algs
& IWINFO_AUTH_OPEN
)
226 snprintf(buf
, sizeof(buf
), "WEP Open System (%s)",
227 format_enc_ciphers(c
->pair_ciphers
));
229 else if (c
->auth_algs
& IWINFO_AUTH_SHARED
)
231 snprintf(buf
, sizeof(buf
), "WEP Shared Auth (%s)",
232 format_enc_ciphers(c
->pair_ciphers
));
237 else if (c
->wpa_version
)
239 for (i
= 0, n
= 0; i
< 3; i
++)
240 if (c
->wpa_version
& (1 << i
))
244 pos
+= sprintf(pos
, "mixed ");
246 for (i
= 0; i
< 3; i
++)
247 if (c
->wpa_version
& (1 << i
))
249 pos
+= sprintf(pos
, "WPA%d/", i
+ 1);
251 pos
+= sprintf(pos
, "WPA/");
255 sprintf(pos
, " %s (%s)",
256 format_enc_suites(c
->auth_suites
),
257 format_enc_ciphers(c
->pair_ciphers
| c
->group_ciphers
));
261 snprintf(buf
, sizeof(buf
), "none");
266 snprintf(buf
, sizeof(buf
), "none");
272 static char * format_hwmodes(int modes
)
277 snprintf(buf
, sizeof(buf
), "unknown");
279 snprintf(buf
, sizeof(buf
), "802.11%s%s%s%s%s%s",
280 (modes
& IWINFO_80211_A
) ? "a" : "",
281 (modes
& IWINFO_80211_B
) ? "b" : "",
282 (modes
& IWINFO_80211_G
) ? "g" : "",
283 (modes
& IWINFO_80211_N
) ? "n" : "",
284 (modes
& IWINFO_80211_AC
) ? "ac" : "",
285 (modes
& IWINFO_80211_AD
) ? "ad" : "");
290 static char * format_assocrate(struct iwinfo_rate_entry
*r
)
298 snprintf(buf
, sizeof(buf
), "unknown");
302 p
+= snprintf(p
, l
, "%s", format_rate(r
->rate
));
303 l
= sizeof(buf
) - (p
- buf
);
307 p
+= snprintf(p
, l
, ", MCS %d, %dMHz", r
->mcs
, r
->mhz
);
308 l
= sizeof(buf
) - (p
- buf
);
312 p
+= snprintf(p
, l
, ", VHT-MCS %d, %dMHz", r
->mcs
, r
->mhz
);
313 l
= sizeof(buf
) - (p
- buf
);
317 p
+= snprintf(p
, l
, ", VHT-NSS %d", r
->nss
);
318 l
= sizeof(buf
) - (p
- buf
);
326 static const char* format_chan_width(uint16_t width
)
329 case 20: return "20 MHz";
330 case 2040: return "40 MHz and upper or 20 MHz with intolerant bit";
331 case 40: return "40 MHz or lower";
332 case 80: return "80 MHz";
333 case 8080: return "80+80 MHz";
334 case 160: return "160 MHz";
341 static const char * print_type(const struct iwinfo_ops
*iw
, const char *ifname
)
343 const char *type
= iwinfo_type(ifname
);
344 return type
? type
: "unknown";
347 static char * print_hardware_id(const struct iwinfo_ops
*iw
, const char *ifname
)
350 struct iwinfo_hardware_id ids
;
352 if (!iw
->hardware_id(ifname
, (char *)&ids
))
354 snprintf(buf
, sizeof(buf
), "%04X:%04X %04X:%04X",
355 ids
.vendor_id
, ids
.device_id
,
356 ids
.subsystem_vendor_id
, ids
.subsystem_device_id
);
360 snprintf(buf
, sizeof(buf
), "unknown");
366 static char * print_hardware_name(const struct iwinfo_ops
*iw
, const char *ifname
)
368 static char buf
[128];
370 if (iw
->hardware_name(ifname
, buf
))
371 snprintf(buf
, sizeof(buf
), "unknown");
376 static char * print_txpower_offset(const struct iwinfo_ops
*iw
, const char *ifname
)
381 if (iw
->txpower_offset(ifname
, &off
))
382 snprintf(buf
, sizeof(buf
), "unknown");
384 snprintf(buf
, sizeof(buf
), "%d dB", off
);
386 snprintf(buf
, sizeof(buf
), "none");
391 static char * print_frequency_offset(const struct iwinfo_ops
*iw
, const char *ifname
)
396 if (iw
->frequency_offset(ifname
, &off
))
397 snprintf(buf
, sizeof(buf
), "unknown");
399 snprintf(buf
, sizeof(buf
), "%.3f GHz", ((float)off
/ 1000.0));
401 snprintf(buf
, sizeof(buf
), "none");
406 static char * print_ssid(const struct iwinfo_ops
*iw
, const char *ifname
)
408 char buf
[IWINFO_ESSID_MAX_SIZE
+1] = { 0 };
410 if (iw
->ssid(ifname
, buf
))
411 memset(buf
, 0, sizeof(buf
));
413 return format_ssid(buf
);
416 static char * print_bssid(const struct iwinfo_ops
*iw
, const char *ifname
)
418 static char buf
[18] = { 0 };
420 if (iw
->bssid(ifname
, buf
))
421 snprintf(buf
, sizeof(buf
), "00:00:00:00:00:00");
426 static char * print_mode(const struct iwinfo_ops
*iw
, const char *ifname
)
429 static char buf
[128];
431 if (iw
->mode(ifname
, &mode
))
432 mode
= IWINFO_OPMODE_UNKNOWN
;
434 snprintf(buf
, sizeof(buf
), "%s", IWINFO_OPMODE_NAMES
[mode
]);
439 static char * print_channel(const struct iwinfo_ops
*iw
, const char *ifname
)
442 if (iw
->channel(ifname
, &ch
))
445 return format_channel(ch
);
448 static char * print_frequency(const struct iwinfo_ops
*iw
, const char *ifname
)
451 if (iw
->frequency(ifname
, &freq
))
454 return format_frequency(freq
);
457 static char * print_txpower(const struct iwinfo_ops
*iw
, const char *ifname
)
460 if (iw
->txpower_offset(ifname
, &off
))
463 if (iw
->txpower(ifname
, &pwr
))
468 return format_txpower(pwr
);
471 static char * print_quality(const struct iwinfo_ops
*iw
, const char *ifname
)
474 if (iw
->quality(ifname
, &qual
))
477 return format_quality(qual
);
480 static char * print_quality_max(const struct iwinfo_ops
*iw
, const char *ifname
)
483 if (iw
->quality_max(ifname
, &qmax
))
486 return format_quality_max(qmax
);
489 static char * print_signal(const struct iwinfo_ops
*iw
, const char *ifname
)
492 if (iw
->signal(ifname
, &sig
))
495 return format_signal(sig
);
498 static char * print_noise(const struct iwinfo_ops
*iw
, const char *ifname
)
501 if (iw
->noise(ifname
, &noise
))
504 return format_noise(noise
);
507 static char * print_rate(const struct iwinfo_ops
*iw
, const char *ifname
)
510 if (iw
->bitrate(ifname
, &rate
))
513 return format_rate(rate
);
516 static char * print_encryption(const struct iwinfo_ops
*iw
, const char *ifname
)
518 struct iwinfo_crypto_entry c
= { 0 };
519 if (iw
->encryption(ifname
, (char *)&c
))
520 return format_encryption(NULL
);
522 return format_encryption(&c
);
525 static char * print_hwmodes(const struct iwinfo_ops
*iw
, const char *ifname
)
528 if (iw
->hwmodelist(ifname
, &modes
))
531 return format_hwmodes(modes
);
534 static char * print_mbssid_supp(const struct iwinfo_ops
*iw
, const char *ifname
)
539 if (iw
->mbssid_support(ifname
, &supp
))
540 snprintf(buf
, sizeof(buf
), "no");
542 snprintf(buf
, sizeof(buf
), "%s", supp
? "yes" : "no");
547 static char * print_phyname(const struct iwinfo_ops
*iw
, const char *ifname
)
551 if (!iw
->phyname(ifname
, buf
))
558 static void print_info(const struct iwinfo_ops
*iw
, const char *ifname
)
560 printf("%-9s ESSID: %s\n",
562 print_ssid(iw
, ifname
));
563 printf(" Access Point: %s\n",
564 print_bssid(iw
, ifname
));
565 printf(" Mode: %s Channel: %s (%s)\n",
566 print_mode(iw
, ifname
),
567 print_channel(iw
, ifname
),
568 print_frequency(iw
, ifname
));
569 printf(" Tx-Power: %s Link Quality: %s/%s\n",
570 print_txpower(iw
, ifname
),
571 print_quality(iw
, ifname
),
572 print_quality_max(iw
, ifname
));
573 printf(" Signal: %s Noise: %s\n",
574 print_signal(iw
, ifname
),
575 print_noise(iw
, ifname
));
576 printf(" Bit Rate: %s\n",
577 print_rate(iw
, ifname
));
578 printf(" Encryption: %s\n",
579 print_encryption(iw
, ifname
));
580 printf(" Type: %s HW Mode(s): %s\n",
581 print_type(iw
, ifname
),
582 print_hwmodes(iw
, ifname
));
583 printf(" Hardware: %s [%s]\n",
584 print_hardware_id(iw
, ifname
),
585 print_hardware_name(iw
, ifname
));
586 printf(" TX power offset: %s\n",
587 print_txpower_offset(iw
, ifname
));
588 printf(" Frequency offset: %s\n",
589 print_frequency_offset(iw
, ifname
));
590 printf(" Supports VAPs: %s PHY name: %s\n",
591 print_mbssid_supp(iw
, ifname
),
592 print_phyname(iw
, ifname
));
596 static void print_scanlist(const struct iwinfo_ops
*iw
, const char *ifname
)
599 char buf
[IWINFO_BUFSIZE
];
600 struct iwinfo_scanlist_entry
*e
;
602 if (iw
->scanlist(ifname
, buf
, &len
))
604 printf("Scanning not possible\n\n");
609 printf("No scan results\n\n");
613 for (i
= 0, x
= 1; i
< len
; i
+= sizeof(struct iwinfo_scanlist_entry
), x
++)
615 e
= (struct iwinfo_scanlist_entry
*) &buf
[i
];
617 printf("Cell %02d - Address: %s\n",
619 format_bssid(e
->mac
));
620 printf(" ESSID: %s\n",
621 format_ssid(e
->ssid
));
622 printf(" Mode: %s Channel: %s\n",
623 IWINFO_OPMODE_NAMES
[e
->mode
],
624 format_channel(e
->channel
));
625 printf(" Signal: %s Quality: %s/%s\n",
626 format_signal(e
->signal
- 0x100),
627 format_quality(e
->quality
),
628 format_quality_max(e
->quality_max
));
629 printf(" Encryption: %s\n",
630 format_encryption(&e
->crypto
));
631 printf(" HT Operation:\n");
632 printf(" Primary Channel: %d\n",
633 e
->ht_chan_info
.primary_chan
);
634 printf(" Secondary Channel Offset: %s\n",
635 ht_secondary_offset
[e
->ht_chan_info
.secondary_chan_off
]);
636 printf(" Channel Width: %s\n",
637 format_chan_width(e
->ht_chan_info
.chan_width
));
639 if (e
->vht_chan_info
.center_chan_1
) {
640 printf(" VHT Operation:\n");
641 printf(" Channel Width: %s\n",
642 format_chan_width(e
->vht_chan_info
.chan_width
));
643 printf(" Center Frequency 1: %d\n",
644 e
->vht_chan_info
.center_chan_1
);
645 printf(" Center Frequency 2: %d\n",
646 e
->vht_chan_info
.center_chan_2
);
654 static void print_txpwrlist(const struct iwinfo_ops
*iw
, const char *ifname
)
656 int len
, pwr
, off
, i
;
657 char buf
[IWINFO_BUFSIZE
];
658 struct iwinfo_txpwrlist_entry
*e
;
660 if (iw
->txpwrlist(ifname
, buf
, &len
) || len
<= 0)
662 printf("No TX power information available\n");
666 if (iw
->txpower(ifname
, &pwr
))
669 if (iw
->txpower_offset(ifname
, &off
))
672 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_txpwrlist_entry
))
674 e
= (struct iwinfo_txpwrlist_entry
*) &buf
[i
];
676 printf("%s%3d dBm (%4d mW)\n",
677 (pwr
== e
->dbm
) ? "*" : " ",
679 iwinfo_dbm2mw(e
->dbm
+ off
));
684 static void print_freqlist(const struct iwinfo_ops
*iw
, const char *ifname
)
687 char buf
[IWINFO_BUFSIZE
];
688 struct iwinfo_freqlist_entry
*e
;
690 if (iw
->freqlist(ifname
, buf
, &len
) || len
<= 0)
692 printf("No frequency information available\n");
696 if (iw
->channel(ifname
, &ch
))
699 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_freqlist_entry
))
701 e
= (struct iwinfo_freqlist_entry
*) &buf
[i
];
703 printf("%s %s (Channel %s)%s\n",
704 (ch
== e
->channel
) ? "*" : " ",
705 format_frequency(e
->mhz
),
706 format_channel(e
->channel
),
707 e
->restricted
? " [restricted]" : "");
712 static void print_assoclist(const struct iwinfo_ops
*iw
, const char *ifname
)
715 char buf
[IWINFO_BUFSIZE
];
716 struct iwinfo_assoclist_entry
*e
;
718 if (iw
->assoclist(ifname
, buf
, &len
))
720 printf("No information available\n");
725 printf("No station connected\n");
729 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_assoclist_entry
))
731 e
= (struct iwinfo_assoclist_entry
*) &buf
[i
];
733 printf("%s %s / %s (SNR %d) %d ms ago\n",
734 format_bssid(e
->mac
),
735 format_signal(e
->signal
),
736 format_noise(e
->noise
),
737 (e
->signal
- e
->noise
),
740 printf(" RX: %-38s %8d Pkts.\n",
741 format_assocrate(&e
->rx_rate
),
745 printf(" TX: %-38s %8d Pkts.\n",
746 format_assocrate(&e
->tx_rate
),
750 printf(" expected throughput: %s\n\n",
751 format_rate(e
->thr
));
756 static char * lookup_country(char *buf
, int len
, int iso3166
)
759 struct iwinfo_country_entry
*c
;
761 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_country_entry
))
763 c
= (struct iwinfo_country_entry
*) &buf
[i
];
765 if (c
->iso3166
== iso3166
)
772 static void print_countrylist(const struct iwinfo_ops
*iw
, const char *ifname
)
775 char buf
[IWINFO_BUFSIZE
];
778 const struct iwinfo_iso3166_label
*l
;
780 if (iw
->countrylist(ifname
, buf
, &len
))
782 printf("No country code information available\n");
786 if (iw
->country(ifname
, curcode
))
787 memset(curcode
, 0, sizeof(curcode
));
789 for (l
= IWINFO_ISO3166_NAMES
; l
->iso3166
; l
++)
791 if ((ccode
= lookup_country(buf
, len
, l
->iso3166
)) != NULL
)
793 printf("%s %4s %c%c\n",
794 strncmp(ccode
, curcode
, 2) ? " " : "*",
795 ccode
, (l
->iso3166
/ 256), (l
->iso3166
% 256));
800 static void print_htmodelist(const struct iwinfo_ops
*iw
, const char *ifname
)
804 if (iw
->htmodelist(ifname
, &htmodes
))
806 printf("No HT mode information available\n");
810 for (i
= 0; i
< ARRAY_SIZE(IWINFO_HTMODE_NAMES
); i
++)
811 if (htmodes
& (1 << i
))
812 printf("%s ", IWINFO_HTMODE_NAMES
[i
]);
817 static void lookup_phy(const struct iwinfo_ops
*iw
, const char *section
)
819 char buf
[IWINFO_BUFSIZE
];
823 fprintf(stderr
, "Not supported\n");
827 if (iw
->lookup_phy(section
, buf
))
829 fprintf(stderr
, "Phy not found\n");
837 int main(int argc
, char **argv
)
841 const struct iwinfo_ops
*iw
;
844 if (argc
> 1 && argc
< 3)
848 " iwinfo <device> info\n"
849 " iwinfo <device> scan\n"
850 " iwinfo <device> txpowerlist\n"
851 " iwinfo <device> freqlist\n"
852 " iwinfo <device> assoclist\n"
853 " iwinfo <device> countrylist\n"
854 " iwinfo <device> htmodelist\n"
855 " iwinfo <backend> phyname <section>\n"
863 glob("/sys/class/net/*", 0, NULL
, &globbuf
);
865 for (i
= 0; i
< globbuf
.gl_pathc
; i
++)
867 p
= strrchr(globbuf
.gl_pathv
[i
], '/');
872 iw
= iwinfo_backend(++p
);
887 iw
= iwinfo_backend_by_name(argv
[1]);
891 fprintf(stderr
, "No such wireless backend: %s\n", argv
[1]);
899 lookup_phy(iw
, argv
[3]);
903 fprintf(stderr
, "Unknown command: %s\n", argv
[2]);
910 iw
= iwinfo_backend(argv
[1]);
914 fprintf(stderr
, "No such wireless device: %s\n", argv
[1]);
919 for (i
= 2; i
< argc
; i
++)
924 print_info(iw
, argv
[1]);
928 print_scanlist(iw
, argv
[1]);
932 print_txpwrlist(iw
, argv
[1]);
936 print_freqlist(iw
, argv
[1]);
940 print_assoclist(iw
, argv
[1]);
944 print_countrylist(iw
, argv
[1]);
948 print_htmodelist(iw
, argv
[1]);
952 fprintf(stderr
, "Unknown command: %s\n", argv
[i
]);