wiconfig.c revision 1.33 1 /* $NetBSD: wiconfig.c,v 1.33 2004/01/23 02:32:52 wrstuden Exp $ */
2 /*
3 * Copyright (c) 1997, 1998, 1999
4 * Bill Paul <wpaul (at) ctr.columbia.edu>. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Bill Paul.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * From: Id: wicontrol.c,v 1.6 1999/05/22 16:12:49 wpaul Exp $
34 */
35
36 #include <sys/types.h>
37 #include <sys/cdefs.h>
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42
43 #include <net/if.h>
44 #ifdef __FreeBSD__
45 #include <net/if_var.h>
46 #include <net/ethernet.h>
47
48 #include <machine/if_wavelan_ieee.h>
49 #else
50 #include <netinet/in.h>
51 #include <netinet/if_ether.h>
52 #ifdef __NetBSD__
53 #include <net80211/ieee80211.h>
54 #include <net80211/ieee80211_ioctl.h>
55 #include <dev/ic/wi_ieee.h>
56 #else
57 #include <dev/pcmcia/if_wavelan_ieee.h>
58 #endif
59 #endif
60
61 #include <stdio.h>
62 #include <string.h>
63 #include <ctype.h>
64 #include <stdlib.h>
65 #include <unistd.h>
66 #include <errno.h>
67 #include <err.h>
68
69 #if !defined(lint)
70 __COPYRIGHT(
71 "@(#) Copyright (c) 1997, 1998, 1999\
72 Bill Paul. All rights reserved.");
73 __RCSID("$NetBSD: wiconfig.c,v 1.33 2004/01/23 02:32:52 wrstuden Exp $");
74 #endif
75
76 struct wi_table {
77 int wi_type;
78 int wi_code;
79 #define WI_NONE 0x00
80 #define WI_STRING 0x01
81 #define WI_BOOL 0x02
82 #define WI_WORDS 0x03
83 #define WI_HEXBYTES 0x04
84 #define WI_KEYSTRUCT 0x05
85 #define WI_BITS 0x06
86 #define WI_VENDOR 0x07
87 char *wi_label; /* label used to print info */
88 int wi_opt; /* option character to set this */
89 char *wi_desc;
90 char *wi_optval;
91 };
92
93 /* already define in wireg.h XXX */
94 #define WI_APRATE_0 0x00 /* NONE */
95 #define WI_APRATE_1 0x0A /* 1 Mbps */
96 #define WI_APRATE_2 0x14 /* 2 Mbps */
97 #define WI_APRATE_5 0x37 /* 5.5 Mbps */
98 #define WI_APRATE_11 0x6E /* 11 Mbps */
99
100 #ifdef WI_RID_SCAN_APS
101 static void wi_apscan __P((char *));
102 static int get_if_flags __P((int, const char *));
103 static int set_if_flags __P((int, const char *, int));
104 #endif
105 static void wi_getval __P((char *, struct wi_req *));
106 static void wi_setval __P((char *, struct wi_req *));
107 static void wi_printstr __P((struct wi_req *));
108 static void wi_setstr __P((char *, int, char *));
109 static void wi_setbytes __P((char *, int, char *, int));
110 static void wi_setword __P((char *, int, int));
111 static void wi_sethex __P((char *, int, char *));
112 static void wi_printwords __P((struct wi_req *));
113 static void wi_printbool __P((struct wi_req *));
114 static void wi_printhex __P((struct wi_req *));
115 static void wi_printbits __P((struct wi_req *));
116 static void wi_dumpinfo __P((char *));
117 static void wi_printkeys __P((struct wi_req *));
118 static void wi_printvendor __P((struct wi_req *));
119 static void wi_dumpstats __P((char *));
120 static void usage __P((void));
121 static struct wi_table *
122 wi_optlookup __P((struct wi_table *, int));
123 int main __P((int argc, char **argv));
124
125 #ifdef WI_RID_SCAN_APS
126 static int get_if_flags(s, name)
127 int s;
128 const char *name;
129 {
130 struct ifreq ifreq;
131 int flags;
132
133 strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
134 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifreq) == -1)
135 err(1, "SIOCGIFFLAGS");
136 flags = ifreq.ifr_flags;
137
138 return flags;
139 }
140
141 static int set_if_flags(s, name, flags)
142 int s;
143 const char *name;
144 int flags;
145 {
146 struct ifreq ifreq;
147
148 ifreq.ifr_flags = flags;
149 strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
150 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifreq) == -1)
151 err(1, "SIOCSIFFLAGS");
152
153 return 0;
154 }
155
156 static void wi_apscan(iface)
157 char *iface;
158 {
159 struct wi_req wreq;
160 struct ifreq ifr;
161 int s;
162 int naps, rate;
163 int retries = 10;
164 int flags;
165 struct wi_apinfo *w;
166 int i, j;
167
168 if (iface == NULL)
169 errx(1, "must specify interface name");
170
171 s = socket(AF_INET, SOCK_DGRAM, 0);
172 if (s == -1)
173 err(1, "socket");
174 flags = get_if_flags(s, iface);
175 if ((flags & IFF_UP) == 0)
176 flags = set_if_flags(s, iface, flags | IFF_UP);
177
178 memset((char *)&wreq, 0, sizeof(wreq));
179
180 wreq.wi_type = WI_RID_SCAN_APS;
181 wreq.wi_len = 4;
182 /* note chan. 1 is the least significant bit */
183 wreq.wi_val[0] = htole16(0x3fff); /* 1 bit per channel, 1-14 */
184 wreq.wi_val[1] = htole16(0xf); /* tx rate */
185
186 /* write the request */
187 wi_setval(iface, &wreq);
188
189 /* now poll for a result */
190 memset((char *)&wreq, 0, sizeof(wreq));
191
192 wreq.wi_type = WI_RID_READ_APS;
193 wreq.wi_len = WI_MAX_DATALEN;
194
195 /* we have to do this ourself as opposed to
196 * using getval, because we cannot bail if
197 * the ioctl fails
198 */
199 memset((char *)&ifr, 0, sizeof(ifr));
200 strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
201 ifr.ifr_data = (caddr_t)&wreq;
202
203 printf("scanning ...");
204 fflush(stdout);
205 while (ioctl(s, SIOCGWAVELAN, &ifr) == -1) {
206 retries--;
207 if (retries >= 0) {
208 printf("."); fflush(stdout);
209 sleep(1);
210 } else
211 break;
212 errno = 0;
213 }
214
215 if (errno) {
216 set_if_flags(s, iface, flags);
217 close(s);
218 err(1, "ioctl");
219 }
220
221 naps = *(int *)wreq.wi_val;
222
223 if (naps > 0)
224 printf("\nAP Information\n");
225 else
226 printf("\nNo APs available\n");
227
228 w = (struct wi_apinfo *)(((char *)&wreq.wi_val) + sizeof(int));
229 for ( i = 0; i < naps; i++, w++) {
230 printf("ap[%d]:\n", i);
231 if (w->scanreason) {
232 static char *scanm[] = {
233 "Host initiated",
234 "Firmware initiated",
235 "Inquiry request from host"
236 };
237 printf("\tScanReason:\t\t\t[ %s ]\n",
238 scanm[w->scanreason - 1]);
239 }
240 printf("\tnetname (SSID):\t\t\t[ ");
241 for (j = 0; j < w->namelen; j++) {
242 printf("%c", w->name[j]);
243 }
244 printf(" ]\n");
245 printf("\tBSSID:\t\t\t\t[ %02x:%02x:%02x:%02x:%02x:%02x ]\n",
246 w->bssid[0]&0xff, w->bssid[1]&0xff,
247 w->bssid[2]&0xff, w->bssid[3]&0xff,
248 w->bssid[4]&0xff, w->bssid[5]&0xff);
249 printf("\tChannel:\t\t\t[ %d ]\n", w->channel);
250 printf("\tQuality/Signal/Noise [signal]:\t[ %d / %d / %d ]\n"
251 "\t [dBm]:\t[ %d / %d / %d ]\n",
252 w->quality, w->signal, w->noise,
253 w->quality, w->signal - 149, w->noise - 149);
254 printf("\tBSS Beacon Interval [msec]:\t[ %d ]\n", w->interval);
255 printf("\tCapinfo:\t\t\t[ ");
256 if (w->capinfo & IEEE80211_CAPINFO_ESS)
257 printf("ESS ");
258 if (w->capinfo & IEEE80211_CAPINFO_PRIVACY)
259 printf("WEP ");
260 printf("]\n");
261
262 switch (w->rate) {
263 case WI_APRATE_1:
264 rate = 1;
265 break;
266 case WI_APRATE_2:
267 rate = 2;
268 break;
269 case WI_APRATE_5:
270 rate = 5.5;
271 break;
272 case WI_APRATE_11:
273 rate = 11;
274 break;
275 case WI_APRATE_0:
276 default:
277 rate = 0;
278 break;
279 }
280 if (rate) printf("\tDataRate [Mbps]:\t\t[ %d ]\n", rate);
281 }
282
283 set_if_flags(s, iface, flags);
284 close(s);
285 }
286 #endif
287
288 static void wi_getval(iface, wreq)
289 char *iface;
290 struct wi_req *wreq;
291 {
292 struct ifreq ifr;
293 int s;
294
295 bzero((char *)&ifr, sizeof(ifr));
296
297 strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
298 ifr.ifr_data = (caddr_t)wreq;
299
300 s = socket(AF_INET, SOCK_DGRAM, 0);
301
302 if (s == -1)
303 err(1, "socket");
304
305 if (ioctl(s, SIOCGWAVELAN, &ifr) == -1)
306 err(1, "SIOCGWAVELAN");
307
308 close(s);
309
310 return;
311 }
312
313 static void wi_setval(iface, wreq)
314 char *iface;
315 struct wi_req *wreq;
316 {
317 struct ifreq ifr;
318 int s;
319
320 bzero((char *)&ifr, sizeof(ifr));
321
322 strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
323 ifr.ifr_data = (caddr_t)wreq;
324
325 s = socket(AF_INET, SOCK_DGRAM, 0);
326
327 if (s == -1)
328 err(1, "socket");
329
330 if (ioctl(s, SIOCSWAVELAN, &ifr) == -1)
331 err(1, "SIOCSWAVELAN");
332
333 close(s);
334
335 return;
336 }
337
338 void wi_printstr(wreq)
339 struct wi_req *wreq;
340 {
341 char *ptr;
342 int i;
343
344 if (wreq->wi_type == WI_RID_SERIALNO) {
345 ptr = (char *)&wreq->wi_val;
346 for (i = 0; i < (wreq->wi_len - 1) * 2; i++) {
347 if (ptr[i] == '\0')
348 ptr[i] = ' ';
349 }
350 } else {
351 int len = le16toh(wreq->wi_val[0]);
352
353 ptr = (char *)&wreq->wi_val[1];
354 for (i = 0; i < len; i++) {
355 if (ptr[i] == '\0')
356 ptr[i] = ' ';
357 }
358 }
359
360 ptr[i] = '\0';
361 printf("[ %s ]", ptr);
362
363 return;
364 }
365
366 void wi_setstr(iface, code, str)
367 char *iface;
368 int code;
369 char *str;
370 {
371 struct wi_req wreq;
372
373 bzero((char *)&wreq, sizeof(wreq));
374
375 if (strlen(str) > 30)
376 errx(1, "string too long");
377
378 wreq.wi_type = code;
379 wreq.wi_len = 18;
380 wreq.wi_val[0] = htole16(strlen(str));
381 bcopy(str, (char *)&wreq.wi_val[1], strlen(str));
382
383 wi_setval(iface, &wreq);
384
385 return;
386 }
387
388 void wi_setbytes(iface, code, bytes, len)
389 char *iface;
390 int code;
391 char *bytes;
392 int len;
393 {
394 struct wi_req wreq;
395
396 bzero((char *)&wreq, sizeof(wreq));
397
398 wreq.wi_type = code;
399 wreq.wi_len = (len / 2) + 1;
400 bcopy(bytes, (char *)&wreq.wi_val[0], len);
401
402 wi_setval(iface, &wreq);
403
404 return;
405 }
406
407 void wi_setword(iface, code, word)
408 char *iface;
409 int code;
410 int word;
411 {
412 struct wi_req wreq;
413
414 bzero((char *)&wreq, sizeof(wreq));
415
416 wreq.wi_type = code;
417 wreq.wi_len = 2;
418 wreq.wi_val[0] = htole16(word);
419
420 wi_setval(iface, &wreq);
421
422 return;
423 }
424
425 void wi_sethex(iface, code, str)
426 char *iface;
427 int code;
428 char *str;
429 {
430 struct ether_addr *addr;
431
432 addr = ether_aton(str);
433 if (addr == NULL)
434 errx(1, "badly formatted address");
435
436 wi_setbytes(iface, code, (char *)addr, ETHER_ADDR_LEN);
437
438 return;
439 }
440
441 static void wi_printkeys(wreq)
442 struct wi_req *wreq;
443 {
444 int i, j, bn;
445 struct wi_key *k;
446 struct wi_ltv_keys *keys;
447 char *ptr;
448
449 keys = (struct wi_ltv_keys *)wreq;
450
451 for (i = 0, bn = 0; i < 4; i++, bn = 0) {
452 k = &keys->wi_keys[i];
453 ptr = (char *)k->wi_keydat;
454 for (j = 0; j < le16toh(k->wi_keylen); j++) {
455 if (!isprint((unsigned char) ptr[j])) {
456 bn = 1;
457 break;
458 }
459 }
460
461 if (bn) {
462 printf("[ 0x");
463 for (j = 0; j < le16toh(k->wi_keylen); j++)
464 printf("%02x", ((unsigned char *) ptr)[j]);
465 printf(" ]");
466 } else {
467 ptr[j] = '\0';
468 printf("[ %s ]", ptr);
469 }
470 }
471
472 return;
473 };
474
475 void wi_printvendor(wreq)
476 struct wi_req *wreq;
477 {
478 /* id
479 * vendor
480 * firmware major
481 * minor
482 */
483 #define WI_RID_STA_IDENTITY_LUCENT 0x1
484 #define WI_RID_STA_IDENTITY_PRISMII 0x2
485 #define WI_RID_STA_IDENTITY_SAMSUNG 0x3
486 #define WI_RID_STA_IDENTITY_DLINK 0x6
487
488 const char *vendor = "Unknown";
489
490 if (wreq->wi_len < 4)
491 return;
492
493 switch (le16toh(wreq->wi_val[1])) {
494 case WI_RID_STA_IDENTITY_LUCENT:
495 vendor = "Lucent";
496 break;
497 case WI_RID_STA_IDENTITY_PRISMII:
498 vendor = "generic PRISM II";
499 break;
500 case WI_RID_STA_IDENTITY_SAMSUNG:
501 vendor = "Samsung";
502 break;
503 case WI_RID_STA_IDENTITY_DLINK:
504 vendor = "D-Link";
505 break;
506 }
507 printf("[ %s ID: %d version: %d.%d ]", vendor, le16toh(wreq->wi_val[0]),
508 le16toh(wreq->wi_val[2]), le16toh(wreq->wi_val[3]));
509 return;
510 }
511
512 void wi_printwords(wreq)
513 struct wi_req *wreq;
514 {
515 int i;
516
517 printf("[ ");
518 for (i = 0; i < wreq->wi_len - 1; i++)
519 printf("%d ", le16toh(wreq->wi_val[i]));
520 printf("]");
521
522 return;
523 }
524
525 void wi_printbool(wreq)
526 struct wi_req *wreq;
527 {
528 if (le16toh(wreq->wi_val[0]))
529 printf("[ On ]");
530 else
531 printf("[ Off ]");
532
533 return;
534 }
535
536 void wi_printhex(wreq)
537 struct wi_req *wreq;
538 {
539 int i;
540 unsigned char *c;
541
542 c = (unsigned char *)&wreq->wi_val;
543
544 printf("[ ");
545 for (i = 0; i < (wreq->wi_len - 1) * 2; i++) {
546 printf("%02x", c[i]);
547 if (i < ((wreq->wi_len - 1) * 2) - 1)
548 printf(":");
549 }
550
551 printf(" ]");
552 return;
553 }
554
555 void wi_printbits(wreq)
556 struct wi_req *wreq;
557 {
558 int i;
559 int bits = le16toh(wreq->wi_val[0]);
560
561 printf("[");
562 for (i = 0; i < 16; i++) {
563 if (bits & 0x1) {
564 printf(" %d", i+1);
565 }
566 bits >>= 1;
567 }
568 printf(" ]");
569 return;
570 }
571
572 static struct wi_table wi_table[] = {
573 { WI_RID_SERIALNO, WI_STRING, "NIC serial number:\t\t\t" },
574 { WI_RID_NODENAME, WI_STRING, "Station name:\t\t\t\t",
575 's', "station name" },
576 { WI_RID_OWN_SSID, WI_STRING, "SSID for IBSS creation:\t\t\t" },
577 { WI_RID_CURRENT_SSID, WI_STRING, "Current netname (SSID):\t\t\t" },
578 { WI_RID_DESIRED_SSID, WI_STRING, "Desired netname (SSID):\t\t\t" },
579 { WI_RID_CURRENT_BSSID, WI_HEXBYTES, "Current BSSID:\t\t\t\t" },
580 { WI_RID_CHANNEL_LIST, WI_BITS, "Channel list:\t\t\t\t" },
581 { WI_RID_OWN_CHNL, WI_WORDS, "IBSS channel:\t\t\t\t" },
582 { WI_RID_CURRENT_CHAN, WI_WORDS, "Current channel:\t\t\t" },
583 { WI_RID_COMMS_QUALITY, WI_WORDS, "Comms quality/signal/noise:\t\t" },
584 { WI_RID_PROMISC, WI_BOOL, "Promiscuous mode:\t\t\t" },
585 { WI_RID_PORTTYPE, WI_WORDS, "Port type:\t\t\t\t" },
586 { WI_RID_MAC_NODE, WI_HEXBYTES, "MAC address:\t\t\t\t",
587 'm', "MAC address" },
588 { WI_RID_TX_RATE, WI_WORDS, "TX rate (selection):\t\t\t" },
589 { WI_RID_CUR_TX_RATE, WI_WORDS, "TX rate (actual speed):\t\t\t"},
590 { WI_RID_CUR_BEACON_INT, WI_WORDS, "Beacon Interval (current) [msec]:\t"},
591 { WI_RID_MAX_DATALEN, WI_WORDS, "Maximum data length:\t\t\t",
592 'd', "maximum data length" },
593 { WI_RID_RTS_THRESH, WI_WORDS, "RTS/CTS handshake threshold:\t\t",
594 'r', "RTS threshold" },
595 { WI_RID_FRAG_THRESH, WI_WORDS, "fragmentation threshold:\t\t",
596 'g', "fragmentation threshold" },
597 { WI_RID_DBM_ADJUST, WI_WORDS, "RSSI -> dBm adjustment:\t\t\t" },
598 { WI_RID_CREATE_IBSS, WI_BOOL, "Create IBSS:\t\t\t\t" },
599 { WI_RID_MICROWAVE_OVEN, WI_WORDS, "Microwave oven robustness:\t\t",
600 'M', "microwave oven robustness enabled" },
601 { WI_RID_ROAMING_MODE, WI_WORDS, "Roaming mode(1:firm,3:disable):\t\t",
602 'R', "roaming mode" },
603 { WI_RID_SYSTEM_SCALE, WI_WORDS, "Access point density:\t\t\t",
604 'a', "system scale" },
605 { WI_RID_PM_ENABLED, WI_WORDS, "Power Mgmt (1=on, 0=off):\t\t" },
606 { WI_RID_MAX_SLEEP, WI_WORDS, "Max sleep time (msec):\t\t\t" },
607 { WI_RID_STA_IDENTITY, WI_VENDOR, "Vendor info:\t\t\t\t" },
608 { 0, WI_NONE }
609 };
610
611 static struct wi_table wi_crypt_table[] = {
612 { WI_RID_ENCRYPTION, WI_BOOL, "WEP encryption:\t\t\t\t" },
613 { WI_RID_CNFAUTHMODE, WI_WORDS, "Authentication type \n(1=OpenSys, 2=Shared Key):\t\t",
614 'A', "authentication type" },
615 { WI_RID_TX_CRYPT_KEY, WI_WORDS, "TX encryption key:\t\t\t" },
616 { WI_RID_DEFLT_CRYPT_KEYS, WI_KEYSTRUCT, "Encryption keys:\t\t\t" },
617 { 0, WI_NONE }
618 };
619
620 static struct wi_table *wi_tables[] = {
621 wi_table,
622 wi_crypt_table,
623 NULL
624 };
625
626 static struct wi_table *
627 wi_optlookup(table, opt)
628 struct wi_table *table;
629 int opt;
630 {
631 struct wi_table *wt;
632
633 for (wt = table; wt->wi_type != 0; wt++)
634 if (wt->wi_opt == opt)
635 return (wt);
636 return (NULL);
637 }
638
639 static void wi_dumpinfo(iface)
640 char *iface;
641 {
642 struct wi_req wreq;
643 int i, has_wep;
644 struct wi_table *w;
645
646 bzero((char *)&wreq, sizeof(wreq));
647
648 wreq.wi_len = WI_MAX_DATALEN;
649 wreq.wi_type = WI_RID_WEP_AVAIL;
650
651 wi_getval(iface, &wreq);
652 has_wep = le16toh(wreq.wi_val[0]);
653
654 w = wi_table;
655
656 for (i = 0; w[i].wi_code != WI_NONE; i++) {
657 bzero((char *)&wreq, sizeof(wreq));
658
659 wreq.wi_len = WI_MAX_DATALEN;
660 wreq.wi_type = w[i].wi_type;
661
662 wi_getval(iface, &wreq);
663 printf("%s", w[i].wi_label);
664 switch (w[i].wi_code) {
665 case WI_STRING:
666 wi_printstr(&wreq);
667 break;
668 case WI_WORDS:
669 wi_printwords(&wreq);
670 break;
671 case WI_BOOL:
672 wi_printbool(&wreq);
673 break;
674 case WI_HEXBYTES:
675 wi_printhex(&wreq);
676 break;
677 case WI_BITS:
678 wi_printbits(&wreq);
679 break;
680 case WI_VENDOR:
681 wi_printvendor(&wreq);
682 break;
683 default:
684 break;
685 }
686 printf("\n");
687 }
688
689 if (has_wep) {
690 w = wi_crypt_table;
691 for (i = 0; w[i].wi_code != WI_NONE; i++) {
692 bzero((char *)&wreq, sizeof(wreq));
693
694 wreq.wi_len = WI_MAX_DATALEN;
695 wreq.wi_type = w[i].wi_type;
696
697 wi_getval(iface, &wreq);
698 printf("%s", w[i].wi_label);
699 switch (w[i].wi_code) {
700 case WI_STRING:
701 wi_printstr(&wreq);
702 break;
703 case WI_WORDS:
704 if (wreq.wi_type == WI_RID_TX_CRYPT_KEY)
705 wreq.wi_val[0] =
706 htole16(le16toh(wreq.wi_val[0]) + 1);
707 wi_printwords(&wreq);
708 break;
709 case WI_BOOL:
710 wi_printbool(&wreq);
711 break;
712 case WI_HEXBYTES:
713 wi_printhex(&wreq);
714 break;
715 case WI_KEYSTRUCT:
716 wi_printkeys(&wreq);
717 break;
718 default:
719 break;
720 }
721 printf("\n");
722 }
723 }
724
725 return;
726 }
727
728 static void wi_dumpstats(iface)
729 char *iface;
730 {
731 struct wi_req wreq;
732 struct wi_counters *c;
733
734 bzero((char *)&wreq, sizeof(wreq));
735 wreq.wi_len = WI_MAX_DATALEN;
736 wreq.wi_type = WI_RID_IFACE_STATS;
737
738 wi_getval(iface, &wreq);
739
740 c = (struct wi_counters *)&wreq.wi_val;
741
742 /* XXX native byte order */
743 printf("Transmitted unicast frames:\t\t%d\n",
744 c->wi_tx_unicast_frames);
745 printf("Transmitted multicast frames:\t\t%d\n",
746 c->wi_tx_multicast_frames);
747 printf("Transmitted fragments:\t\t\t%d\n",
748 c->wi_tx_fragments);
749 printf("Transmitted unicast octets:\t\t%d\n",
750 c->wi_tx_unicast_octets);
751 printf("Transmitted multicast octets:\t\t%d\n",
752 c->wi_tx_multicast_octets);
753 printf("Single transmit retries:\t\t%d\n",
754 c->wi_tx_single_retries);
755 printf("Multiple transmit retries:\t\t%d\n",
756 c->wi_tx_multi_retries);
757 printf("Transmit retry limit exceeded:\t\t%d\n",
758 c->wi_tx_retry_limit);
759 printf("Transmit discards:\t\t\t%d\n",
760 c->wi_tx_discards);
761 printf("Transmit discards due to wrong SA:\t%d\n",
762 c->wi_tx_discards_wrong_sa);
763 printf("Received unicast frames:\t\t%d\n",
764 c->wi_rx_unicast_frames);
765 printf("Received multicast frames:\t\t%d\n",
766 c->wi_rx_multicast_frames);
767 printf("Received fragments:\t\t\t%d\n",
768 c->wi_rx_fragments);
769 printf("Received unicast octets:\t\t%d\n",
770 c->wi_rx_unicast_octets);
771 printf("Received multicast octets:\t\t%d\n",
772 c->wi_rx_multicast_octets);
773 printf("Receive FCS errors:\t\t\t%d\n",
774 c->wi_rx_fcs_errors);
775 printf("Receive discards due to no buffer:\t%d\n",
776 c->wi_rx_discards_nobuf);
777 printf("Can't decrypt WEP frame:\t\t%d\n",
778 c->wi_rx_WEP_cant_decrypt);
779 printf("Received message fragments:\t\t%d\n",
780 c->wi_rx_msg_in_msg_frags);
781 printf("Received message bad fragments:\t\t%d\n",
782 c->wi_rx_msg_in_bad_msg_frags);
783
784 return;
785 }
786
787 static void
788 usage()
789 {
790
791 fprintf(stderr,
792 "usage: %s interface "
793 "[-oD] [-s station name]\n"
794 " [-a access point density]\n"
795 " [-m MAC address] [-d max data length] [-r RTS threshold]\n"
796 " [-M 0|1] [-R 1|3] [-g fragmentation threshold]\n"
797 ,
798 getprogname());
799 exit(1);
800 }
801
802 int main(argc, argv)
803 int argc;
804 char *argv[];
805 {
806 struct wi_table *wt, **table;
807 char *iface;
808 int ch, dumpinfo, dumpstats, apscan;
809
810 #define SET_OPERAND(opr, desc) do { \
811 if ((opr) == NULL) \
812 (opr) = optarg; \
813 else \
814 warnx("%s is already specified to %s", \
815 desc, (opr)); \
816 } while (0)
817
818 dumpinfo = 1;
819 dumpstats = 0;
820 apscan = 0;
821 iface = NULL;
822
823 if (argc > 1 && argv[1][0] != '-') {
824 iface = argv[1];
825 optind++;
826 }
827
828 while ((ch = getopt(argc, argv,
829 "a:d:g:hi:m:or:s:A:M:R:D")) != -1) {
830 if (ch != 'i')
831 dumpinfo = 0;
832 /*
833 * Lookup generic options and remeber operand if found.
834 */
835 for (table = wi_tables; *table != NULL; table++)
836 if ((wt = wi_optlookup(*table, ch)) != NULL) {
837 SET_OPERAND(wt->wi_optval, wt->wi_desc);
838 break;
839 }
840 if (wt == NULL)
841 /*
842 * Handle special options.
843 */
844 switch (ch) {
845 case 'o':
846 dumpstats = 1;
847 break;
848 case 'i':
849 SET_OPERAND(iface, "interface");
850 break;
851 case 'D':
852 apscan = 1;
853 break;
854 case 'h':
855 default:
856 usage();
857 break;
858 }
859 }
860
861 if (iface == NULL)
862 usage();
863
864 for (table = wi_tables; *table != NULL; table++)
865 for (wt = *table; wt->wi_code != WI_NONE; wt++)
866 if (wt->wi_optval != NULL) {
867 switch (wt->wi_code) {
868 case WI_BOOL:
869 case WI_WORDS:
870 wi_setword(iface, wt->wi_type,
871 atoi(wt->wi_optval));
872 break;
873 case WI_STRING:
874 wi_setstr(iface, wt->wi_type,
875 wt->wi_optval);
876 break;
877 case WI_HEXBYTES:
878 wi_sethex(iface, wt->wi_type,
879 wt->wi_optval);
880 break;
881 }
882 }
883
884 if (dumpstats)
885 wi_dumpstats(iface);
886 if (dumpinfo)
887 wi_dumpinfo(iface);
888
889 if (apscan)
890 #ifdef WI_RID_SCAN_APS
891 wi_apscan(iface);
892 #else
893 errx(1, "AP scan mode is not available.");
894 #endif
895
896 exit(0);
897 }
898