8da216a6efab6e6dc5af86124092d477aeba8ee9
[openwrt/staging/lynxis/omap.git] / package / network / utils / iwinfo / src / iwinfo_cli.c
1 /*
2 * iwinfo - Wireless Information Library - Command line frontend
3 *
4 * Copyright (C) 2011 Jo-Philipp Wich <xm@subsignal.org>
5 *
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.
9 *
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.
14 *
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/.
17 */
18
19 #include <stdio.h>
20 #include <glob.h>
21
22 #include "iwinfo.h"
23
24
25 static char * format_bssid(unsigned char *mac)
26 {
27 static char buf[18];
28
29 snprintf(buf, sizeof(buf), "%02X:%02X:%02X:%02X:%02X:%02X",
30 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
31
32 return buf;
33 }
34
35 static char * format_ssid(char *ssid)
36 {
37 static char buf[IWINFO_ESSID_MAX_SIZE+3];
38
39 if (ssid && ssid[0])
40 snprintf(buf, sizeof(buf), "\"%s\"", ssid);
41 else
42 snprintf(buf, sizeof(buf), "unknown");
43
44 return buf;
45 }
46
47 static char * format_channel(int ch)
48 {
49 static char buf[8];
50
51 if (ch <= 0)
52 snprintf(buf, sizeof(buf), "unknown");
53 else
54 snprintf(buf, sizeof(buf), "%d", ch);
55
56 return buf;
57 }
58
59 static char * format_frequency(int freq)
60 {
61 static char buf[10];
62
63 if (freq <= 0)
64 snprintf(buf, sizeof(buf), "unknown");
65 else
66 snprintf(buf, sizeof(buf), "%.3f GHz", ((float)freq / 1000.0));
67
68 return buf;
69 }
70
71 static char * format_txpower(int pwr)
72 {
73 static char buf[10];
74
75 if (pwr < 0)
76 snprintf(buf, sizeof(buf), "unknown");
77 else
78 snprintf(buf, sizeof(buf), "%d dBm", pwr);
79
80 return buf;
81 }
82
83 static char * format_quality(int qual)
84 {
85 static char buf[8];
86
87 if (qual < 0)
88 snprintf(buf, sizeof(buf), "unknown");
89 else
90 snprintf(buf, sizeof(buf), "%d", qual);
91
92 return buf;
93 }
94
95 static char * format_quality_max(int qmax)
96 {
97 static char buf[8];
98
99 if (qmax < 0)
100 snprintf(buf, sizeof(buf), "unknown");
101 else
102 snprintf(buf, sizeof(buf), "%d", qmax);
103
104 return buf;
105 }
106
107 static char * format_signal(int sig)
108 {
109 static char buf[10];
110
111 if (!sig)
112 snprintf(buf, sizeof(buf), "unknown");
113 else
114 snprintf(buf, sizeof(buf), "%d dBm", sig);
115
116 return buf;
117 }
118
119 static char * format_noise(int noise)
120 {
121 static char buf[10];
122
123 if (!noise)
124 snprintf(buf, sizeof(buf), "unknown");
125 else
126 snprintf(buf, sizeof(buf), "%d dBm", noise);
127
128 return buf;
129 }
130
131 static char * format_rate(int rate)
132 {
133 static char buf[14];
134
135 if (rate <= 0)
136 snprintf(buf, sizeof(buf), "unknown");
137 else
138 snprintf(buf, sizeof(buf), "%d.%d MBit/s",
139 rate / 1000, (rate % 1000) / 100);
140
141 return buf;
142 }
143
144 static char * format_enc_ciphers(int ciphers)
145 {
146 static char str[128] = { 0 };
147 char *pos = str;
148
149 if (ciphers & IWINFO_CIPHER_WEP40)
150 pos += sprintf(pos, "WEP-40, ");
151
152 if (ciphers & IWINFO_CIPHER_WEP104)
153 pos += sprintf(pos, "WEP-104, ");
154
155 if (ciphers & IWINFO_CIPHER_TKIP)
156 pos += sprintf(pos, "TKIP, ");
157
158 if (ciphers & IWINFO_CIPHER_CCMP)
159 pos += sprintf(pos, "CCMP, ");
160
161 if (ciphers & IWINFO_CIPHER_WRAP)
162 pos += sprintf(pos, "WRAP, ");
163
164 if (ciphers & IWINFO_CIPHER_AESOCB)
165 pos += sprintf(pos, "AES-OCB, ");
166
167 if (ciphers & IWINFO_CIPHER_CKIP)
168 pos += sprintf(pos, "CKIP, ");
169
170 if (!ciphers || (ciphers & IWINFO_CIPHER_NONE))
171 pos += sprintf(pos, "NONE, ");
172
173 *(pos - 2) = 0;
174
175 return str;
176 }
177
178 static char * format_enc_suites(int suites)
179 {
180 static char str[64] = { 0 };
181 char *pos = str;
182
183 if (suites & IWINFO_KMGMT_PSK)
184 pos += sprintf(pos, "PSK/");
185
186 if (suites & IWINFO_KMGMT_8021x)
187 pos += sprintf(pos, "802.1X/");
188
189 if (!suites || (suites & IWINFO_KMGMT_NONE))
190 pos += sprintf(pos, "NONE/");
191
192 *(pos - 1) = 0;
193
194 return str;
195 }
196
197 static char * format_encryption(struct iwinfo_crypto_entry *c)
198 {
199 static char buf[512];
200
201 if (!c)
202 {
203 snprintf(buf, sizeof(buf), "unknown");
204 }
205 else if (c->enabled)
206 {
207 /* WEP */
208 if (c->auth_algs && !c->wpa_version)
209 {
210 if ((c->auth_algs & IWINFO_AUTH_OPEN) &&
211 (c->auth_algs & IWINFO_AUTH_SHARED))
212 {
213 snprintf(buf, sizeof(buf), "WEP Open/Shared (%s)",
214 format_enc_ciphers(c->pair_ciphers));
215 }
216 else if (c->auth_algs & IWINFO_AUTH_OPEN)
217 {
218 snprintf(buf, sizeof(buf), "WEP Open System (%s)",
219 format_enc_ciphers(c->pair_ciphers));
220 }
221 else if (c->auth_algs & IWINFO_AUTH_SHARED)
222 {
223 snprintf(buf, sizeof(buf), "WEP Shared Auth (%s)",
224 format_enc_ciphers(c->pair_ciphers));
225 }
226 }
227
228 /* WPA */
229 else if (c->wpa_version)
230 {
231 switch (c->wpa_version) {
232 case 3:
233 snprintf(buf, sizeof(buf), "mixed WPA/WPA2 %s (%s)",
234 format_enc_suites(c->auth_suites),
235 format_enc_ciphers(c->pair_ciphers | c->group_ciphers));
236 break;
237
238 case 2:
239 snprintf(buf, sizeof(buf), "WPA2 %s (%s)",
240 format_enc_suites(c->auth_suites),
241 format_enc_ciphers(c->pair_ciphers | c->group_ciphers));
242 break;
243
244 case 1:
245 snprintf(buf, sizeof(buf), "WPA %s (%s)",
246 format_enc_suites(c->auth_suites),
247 format_enc_ciphers(c->pair_ciphers | c->group_ciphers));
248 break;
249 }
250 }
251 else
252 {
253 snprintf(buf, sizeof(buf), "none");
254 }
255 }
256 else
257 {
258 snprintf(buf, sizeof(buf), "none");
259 }
260
261 return buf;
262 }
263
264 static char * format_hwmodes(int modes)
265 {
266 static char buf[12];
267
268 if (modes <= 0)
269 snprintf(buf, sizeof(buf), "unknown");
270 else
271 snprintf(buf, sizeof(buf), "802.11%s%s%s%s",
272 (modes & IWINFO_80211_A) ? "a" : "",
273 (modes & IWINFO_80211_B) ? "b" : "",
274 (modes & IWINFO_80211_G) ? "g" : "",
275 (modes & IWINFO_80211_N) ? "n" : "");
276
277 return buf;
278 }
279
280 static char * format_assocrate(struct iwinfo_rate_entry *r)
281 {
282 static char buf[40];
283 char *p = buf;
284 int l = sizeof(buf);
285
286 if (r->rate <= 0)
287 {
288 snprintf(buf, sizeof(buf), "unknown");
289 }
290 else
291 {
292 p += snprintf(p, l, "%s", format_rate(r->rate));
293 l = sizeof(buf) - (p - buf);
294
295 if (r->mcs >= 0)
296 {
297 p += snprintf(p, l, ", MCS %d, %dMHz", r->mcs, 20 + r->is_40mhz*20);
298 l = sizeof(buf) - (p - buf);
299
300 if (r->is_short_gi)
301 p += snprintf(p, l, ", short GI");
302 }
303 }
304
305 return buf;
306 }
307
308
309 static const char * print_type(const struct iwinfo_ops *iw, const char *ifname)
310 {
311 const char *type = iwinfo_type(ifname);
312 return type ? type : "unknown";
313 }
314
315 static char * print_hardware_id(const struct iwinfo_ops *iw, const char *ifname)
316 {
317 static char buf[20];
318 struct iwinfo_hardware_id ids;
319
320 if (!iw->hardware_id(ifname, (char *)&ids))
321 {
322 snprintf(buf, sizeof(buf), "%04X:%04X %04X:%04X",
323 ids.vendor_id, ids.device_id,
324 ids.subsystem_vendor_id, ids.subsystem_device_id);
325 }
326 else
327 {
328 snprintf(buf, sizeof(buf), "unknown");
329 }
330
331 return buf;
332 }
333
334 static char * print_hardware_name(const struct iwinfo_ops *iw, const char *ifname)
335 {
336 static char buf[128];
337
338 if (iw->hardware_name(ifname, buf))
339 snprintf(buf, sizeof(buf), "unknown");
340
341 return buf;
342 }
343
344 static char * print_txpower_offset(const struct iwinfo_ops *iw, const char *ifname)
345 {
346 int off;
347 static char buf[12];
348
349 if (iw->txpower_offset(ifname, &off))
350 snprintf(buf, sizeof(buf), "unknown");
351 else if (off != 0)
352 snprintf(buf, sizeof(buf), "%d dB", off);
353 else
354 snprintf(buf, sizeof(buf), "none");
355
356 return buf;
357 }
358
359 static char * print_frequency_offset(const struct iwinfo_ops *iw, const char *ifname)
360 {
361 int off;
362 static char buf[12];
363
364 if (iw->frequency_offset(ifname, &off))
365 snprintf(buf, sizeof(buf), "unknown");
366 else if (off != 0)
367 snprintf(buf, sizeof(buf), "%.3f GHz", ((float)off / 1000.0));
368 else
369 snprintf(buf, sizeof(buf), "none");
370
371 return buf;
372 }
373
374 static char * print_ssid(const struct iwinfo_ops *iw, const char *ifname)
375 {
376 char buf[IWINFO_ESSID_MAX_SIZE+1] = { 0 };
377
378 if (iw->ssid(ifname, buf))
379 memset(buf, 0, sizeof(buf));
380
381 return format_ssid(buf);
382 }
383
384 static char * print_bssid(const struct iwinfo_ops *iw, const char *ifname)
385 {
386 static char buf[18] = { 0 };
387
388 if (iw->bssid(ifname, buf))
389 snprintf(buf, sizeof(buf), "00:00:00:00:00:00");
390
391 return buf;
392 }
393
394 static char * print_mode(const struct iwinfo_ops *iw, const char *ifname)
395 {
396 int mode;
397 static char buf[128];
398
399 if (iw->mode(ifname, &mode))
400 mode = IWINFO_OPMODE_UNKNOWN;
401
402 snprintf(buf, sizeof(buf), "%s", IWINFO_OPMODE_NAMES[mode]);
403
404 return buf;
405 }
406
407 static char * print_channel(const struct iwinfo_ops *iw, const char *ifname)
408 {
409 int ch;
410 if (iw->channel(ifname, &ch))
411 ch = -1;
412
413 return format_channel(ch);
414 }
415
416 static char * print_frequency(const struct iwinfo_ops *iw, const char *ifname)
417 {
418 int freq;
419 if (iw->frequency(ifname, &freq))
420 freq = -1;
421
422 return format_frequency(freq);
423 }
424
425 static char * print_txpower(const struct iwinfo_ops *iw, const char *ifname)
426 {
427 int pwr, off;
428 if (iw->txpower_offset(ifname, &off))
429 off = 0;
430
431 if (iw->txpower(ifname, &pwr))
432 pwr = -1;
433 else
434 pwr += off;
435
436 return format_txpower(pwr);
437 }
438
439 static char * print_quality(const struct iwinfo_ops *iw, const char *ifname)
440 {
441 int qual;
442 if (iw->quality(ifname, &qual))
443 qual = -1;
444
445 return format_quality(qual);
446 }
447
448 static char * print_quality_max(const struct iwinfo_ops *iw, const char *ifname)
449 {
450 int qmax;
451 if (iw->quality_max(ifname, &qmax))
452 qmax = -1;
453
454 return format_quality_max(qmax);
455 }
456
457 static char * print_signal(const struct iwinfo_ops *iw, const char *ifname)
458 {
459 int sig;
460 if (iw->signal(ifname, &sig))
461 sig = 0;
462
463 return format_signal(sig);
464 }
465
466 static char * print_noise(const struct iwinfo_ops *iw, const char *ifname)
467 {
468 int noise;
469 if (iw->noise(ifname, &noise))
470 noise = 0;
471
472 return format_noise(noise);
473 }
474
475 static char * print_rate(const struct iwinfo_ops *iw, const char *ifname)
476 {
477 int rate;
478 if (iw->bitrate(ifname, &rate))
479 rate = -1;
480
481 return format_rate(rate);
482 }
483
484 static char * print_encryption(const struct iwinfo_ops *iw, const char *ifname)
485 {
486 struct iwinfo_crypto_entry c = { 0 };
487 if (iw->encryption(ifname, (char *)&c))
488 return format_encryption(NULL);
489
490 return format_encryption(&c);
491 }
492
493 static char * print_hwmodes(const struct iwinfo_ops *iw, const char *ifname)
494 {
495 int modes;
496 if (iw->hwmodelist(ifname, &modes))
497 modes = -1;
498
499 return format_hwmodes(modes);
500 }
501
502 static char * print_mbssid_supp(const struct iwinfo_ops *iw, const char *ifname)
503 {
504 int supp;
505 static char buf[4];
506
507 if (iw->mbssid_support(ifname, &supp))
508 snprintf(buf, sizeof(buf), "no");
509 else
510 snprintf(buf, sizeof(buf), "%s", supp ? "yes" : "no");
511
512 return buf;
513 }
514
515
516 static void print_info(const struct iwinfo_ops *iw, const char *ifname)
517 {
518 printf("%-9s ESSID: %s\n",
519 ifname,
520 print_ssid(iw, ifname));
521 printf(" Access Point: %s\n",
522 print_bssid(iw, ifname));
523 printf(" Mode: %s Channel: %s (%s)\n",
524 print_mode(iw, ifname),
525 print_channel(iw, ifname),
526 print_frequency(iw, ifname));
527 printf(" Tx-Power: %s Link Quality: %s/%s\n",
528 print_txpower(iw, ifname),
529 print_quality(iw, ifname),
530 print_quality_max(iw, ifname));
531 printf(" Signal: %s Noise: %s\n",
532 print_signal(iw, ifname),
533 print_noise(iw, ifname));
534 printf(" Bit Rate: %s\n",
535 print_rate(iw, ifname));
536 printf(" Encryption: %s\n",
537 print_encryption(iw, ifname));
538 printf(" Type: %s HW Mode(s): %s\n",
539 print_type(iw, ifname),
540 print_hwmodes(iw, ifname));
541 printf(" Hardware: %s [%s]\n",
542 print_hardware_id(iw, ifname),
543 print_hardware_name(iw, ifname));
544 printf(" TX power offset: %s\n",
545 print_txpower_offset(iw, ifname));
546 printf(" Frequency offset: %s\n",
547 print_frequency_offset(iw, ifname));
548 printf(" Supports VAPs: %s\n",
549 print_mbssid_supp(iw, ifname));
550 }
551
552
553 static void print_scanlist(const struct iwinfo_ops *iw, const char *ifname)
554 {
555 int i, x, len;
556 char buf[IWINFO_BUFSIZE];
557 struct iwinfo_scanlist_entry *e;
558
559 if (iw->scanlist(ifname, buf, &len))
560 {
561 printf("Scanning not possible\n\n");
562 return;
563 }
564 else if (len <= 0)
565 {
566 printf("No scan results\n\n");
567 return;
568 }
569
570 for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++)
571 {
572 e = (struct iwinfo_scanlist_entry *) &buf[i];
573
574 printf("Cell %02d - Address: %s\n",
575 x,
576 format_bssid(e->mac));
577 printf(" ESSID: %s\n",
578 format_ssid(e->ssid));
579 printf(" Mode: %s Channel: %s\n",
580 IWINFO_OPMODE_NAMES[e->mode],
581 format_channel(e->channel));
582 printf(" Signal: %s Quality: %s/%s\n",
583 format_signal(e->signal - 0x100),
584 format_quality(e->quality),
585 format_quality_max(e->quality_max));
586 printf(" Encryption: %s\n\n",
587 format_encryption(&e->crypto));
588 }
589 }
590
591
592 static void print_txpwrlist(const struct iwinfo_ops *iw, const char *ifname)
593 {
594 int len, pwr, off, i;
595 char buf[IWINFO_BUFSIZE];
596 struct iwinfo_txpwrlist_entry *e;
597
598 if (iw->txpwrlist(ifname, buf, &len) || len <= 0)
599 {
600 printf("No TX power information available\n");
601 return;
602 }
603
604 if (iw->txpower(ifname, &pwr))
605 pwr = -1;
606
607 if (iw->txpower_offset(ifname, &off))
608 off = 0;
609
610 for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
611 {
612 e = (struct iwinfo_txpwrlist_entry *) &buf[i];
613
614 printf("%s%3d dBm (%4d mW)\n",
615 (pwr == e->dbm) ? "*" : " ",
616 e->dbm + off,
617 iwinfo_dbm2mw(e->dbm + off));
618 }
619 }
620
621
622 static void print_freqlist(const struct iwinfo_ops *iw, const char *ifname)
623 {
624 int i, len, ch;
625 char buf[IWINFO_BUFSIZE];
626 struct iwinfo_freqlist_entry *e;
627
628 if (iw->freqlist(ifname, buf, &len) || len <= 0)
629 {
630 printf("No frequency information available\n");
631 return;
632 }
633
634 if (iw->channel(ifname, &ch))
635 ch = -1;
636
637 for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
638 {
639 e = (struct iwinfo_freqlist_entry *) &buf[i];
640
641 printf("%s %s (Channel %s)%s\n",
642 (ch == e->channel) ? "*" : " ",
643 format_frequency(e->mhz),
644 format_channel(e->channel),
645 e->restricted ? " [restricted]" : "");
646 }
647 }
648
649
650 static void print_assoclist(const struct iwinfo_ops *iw, const char *ifname)
651 {
652 int i, len;
653 char buf[IWINFO_BUFSIZE];
654 struct iwinfo_assoclist_entry *e;
655
656 if (iw->assoclist(ifname, buf, &len))
657 {
658 printf("No information available\n");
659 return;
660 }
661 else if (len <= 0)
662 {
663 printf("No station connected\n");
664 return;
665 }
666
667 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
668 {
669 e = (struct iwinfo_assoclist_entry *) &buf[i];
670
671 printf("%s %s / %s (SNR %d) %d ms ago\n",
672 format_bssid(e->mac),
673 format_signal(e->signal),
674 format_noise(e->noise),
675 (e->signal - e->noise),
676 e->inactive);
677
678 printf(" RX: %-38s %8d Pkts.\n",
679 format_assocrate(&e->rx_rate),
680 e->rx_packets
681 );
682
683 printf(" TX: %-38s %8d Pkts.\n\n",
684 format_assocrate(&e->tx_rate),
685 e->tx_packets
686 );
687 }
688 }
689
690
691 static char * lookup_country(char *buf, int len, int iso3166)
692 {
693 int i;
694 struct iwinfo_country_entry *c;
695
696 for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
697 {
698 c = (struct iwinfo_country_entry *) &buf[i];
699
700 if (c->iso3166 == iso3166)
701 return c->ccode;
702 }
703
704 return NULL;
705 }
706
707 static void print_countrylist(const struct iwinfo_ops *iw, const char *ifname)
708 {
709 int len;
710 char buf[IWINFO_BUFSIZE];
711 char *ccode;
712 char curcode[3];
713 const struct iwinfo_iso3166_label *l;
714
715 if (iw->countrylist(ifname, buf, &len))
716 {
717 printf("No country code information available\n");
718 return;
719 }
720
721 if (iw->country(ifname, curcode))
722 memset(curcode, 0, sizeof(curcode));
723
724 for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
725 {
726 if ((ccode = lookup_country(buf, len, l->iso3166)) != NULL)
727 {
728 printf("%s %4s %c%c\n",
729 strncmp(ccode, curcode, 2) ? " " : "*",
730 ccode, (l->iso3166 / 256), (l->iso3166 % 256));
731 }
732 }
733 }
734
735
736 int main(int argc, char **argv)
737 {
738 int i;
739 char *p;
740 const struct iwinfo_ops *iw;
741 glob_t globbuf;
742
743 if (argc > 1 && argc < 3)
744 {
745 fprintf(stderr,
746 "Usage:\n"
747 " iwinfo <device> info\n"
748 " iwinfo <device> scan\n"
749 " iwinfo <device> txpowerlist\n"
750 " iwinfo <device> freqlist\n"
751 " iwinfo <device> assoclist\n"
752 " iwinfo <device> countrylist\n"
753 );
754
755 return 1;
756 }
757
758 if (argc == 1)
759 {
760 glob("/sys/class/net/*", 0, NULL, &globbuf);
761
762 for (i = 0; i < globbuf.gl_pathc; i++)
763 {
764 p = strrchr(globbuf.gl_pathv[i], '/');
765
766 if (!p)
767 continue;
768
769 iw = iwinfo_backend(++p);
770
771 if (!iw)
772 continue;
773
774 print_info(iw, p);
775 printf("\n");
776 }
777
778 globfree(&globbuf);
779 return 0;
780 }
781
782 iw = iwinfo_backend(argv[1]);
783
784 if (!iw)
785 {
786 fprintf(stderr, "No such wireless device: %s\n", argv[1]);
787 return 1;
788 }
789
790 for (i = 2; i < argc; i++)
791 {
792 switch(argv[i][0])
793 {
794 case 'i':
795 print_info(iw, argv[1]);
796 break;
797
798 case 's':
799 print_scanlist(iw, argv[1]);
800 break;
801
802 case 't':
803 print_txpwrlist(iw, argv[1]);
804 break;
805
806 case 'f':
807 print_freqlist(iw, argv[1]);
808 break;
809
810 case 'a':
811 print_assoclist(iw, argv[1]);
812 break;
813
814 case 'c':
815 print_countrylist(iw, argv[1]);
816 break;
817
818 default:
819 fprintf(stderr, "Unknown command: %s\n", argv[i]);
820 return 1;
821 }
822 }
823
824 iwinfo_finish();
825
826 return 0;
827 }