wiconfig.c revision 1.17 1 /* $NetBSD: wiconfig.c,v 1.17 2002/01/22 02:09:11 ichiro 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 <net/if_ieee80211.h>
54 #include <dev/ic/wi_ieee.h>
55 #else
56 #include <dev/pcmcia/if_wavelan_ieee.h>
57 #endif
58 #endif
59
60 #include <stdio.h>
61 #include <string.h>
62 #include <ctype.h>
63 #include <stdlib.h>
64 #include <unistd.h>
65 #include <errno.h>
66 #include <err.h>
67
68 #if !defined(lint)
69 static const char copyright[] = "@(#) Copyright (c) 1997, 1998, 1999\
70 Bill Paul. All rights reserved.";
71 static const char rcsid[] =
72 "@(#) $Id: wiconfig.c,v 1.17 2002/01/22 02:09:11 ichiro Exp $";
73 #endif
74
75 struct wi_table {
76 int wi_type;
77 int wi_code;
78 #define WI_NONE 0x00
79 #define WI_STRING 0x01
80 #define WI_BOOL 0x02
81 #define WI_WORDS 0x03
82 #define WI_HEXBYTES 0x04
83 #define WI_KEYSTRUCT 0x05
84 char *wi_label; /* label used to print info */
85 int wi_opt; /* option character to set this */
86 char *wi_desc;
87 char *wi_optval;
88 };
89
90 /* already define in wireg.h XXX */
91 #define WI_APRATE_0 0x00 /* NONE */
92 #define WI_APRATE_1 0x0A /* 1 Mbps */
93 #define WI_APRATE_2 0x14 /* 2 Mbps */
94 #define WI_APRATE_5 0x37 /* 5.5 Mbps */
95 #define WI_APRATE_11 0x6E /* 11 Mbps */
96
97 static void wi_apscan __P((char *));
98 static void wi_getval __P((char *, struct wi_req *));
99 static void wi_setval __P((char *, struct wi_req *));
100 static void wi_printstr __P((struct wi_req *));
101 static void wi_setstr __P((char *, int, char *));
102 static void wi_setbytes __P((char *, int, char *, int));
103 static void wi_setword __P((char *, int, int));
104 static void wi_sethex __P((char *, int, char *));
105 static void wi_printwords __P((struct wi_req *));
106 static void wi_printbool __P((struct wi_req *));
107 static void wi_printhex __P((struct wi_req *));
108 static void wi_dumpinfo __P((char *));
109 static void wi_setkeys __P((char *, char *, int));
110 static void wi_printkeys __P((struct wi_req *));
111 static void wi_dumpstats __P((char *));
112 static int get_if_flags __P((int, const char *));
113 static int set_if_flags __P((int, const char *, int));
114 static void usage __P((void));
115 static struct wi_table *
116 wi_optlookup __P((struct wi_table *, int));
117 static int wi_hex2int(char c);
118 static void wi_str2key __P((char *, struct wi_key *));
119 int main __P((int argc, char **argv));
120
121 static int get_if_flags(s, name)
122 int s;
123 const char *name;
124 {
125 struct ifreq ifreq;
126 int flags;
127
128 strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
129 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifreq) == -1)
130 err(1, "SIOCGIFFLAGS");
131 flags = ifreq.ifr_flags;
132
133 return flags;
134 }
135
136 static int set_if_flags(s, name, flags)
137 int s;
138 const char *name;
139 int flags;
140 {
141 struct ifreq ifreq;
142
143 ifreq.ifr_flags = flags;
144 strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
145 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifreq) == -1)
146 err(1, "SIOCSIFFLAGS");
147
148 return 0;
149 }
150
151 static void wi_apscan(iface)
152 char *iface;
153 {
154 struct wi_req wreq;
155 struct ifreq ifr;
156 int s;
157 int naps, rate;
158 int retries = 10;
159 int flags;
160 struct wi_apinfo *w;
161 int i, j;
162
163 if (iface == NULL)
164 errx(1, "must specify interface name");
165
166 s = socket(AF_INET, SOCK_DGRAM, 0);
167 if (s == -1)
168 err(1, "socket");
169 flags = get_if_flags(s, iface);
170 if ((flags & IFF_UP) == 0)
171 flags = set_if_flags(s, iface, flags | IFF_UP);
172
173 memset((char *)&wreq, 0, sizeof(wreq));
174
175 wreq.wi_type = WI_RID_SCAN_APS;
176 wreq.wi_len = 4;
177 /* note chan. 1 is the least significant bit */
178 wreq.wi_val[0] = 0x7ff; /* 1 bit per channel, 1-11 */
179 wreq.wi_val[1] = 3; /* tx rate */
180
181 /* write the request */
182 wi_setval(iface, &wreq);
183
184 /* now poll for a result */
185 memset((char *)&wreq, 0, sizeof(wreq));
186
187 wreq.wi_type = WI_RID_READ_APS;
188 wreq.wi_len = WI_MAX_DATALEN;
189
190 /* we have to do this ourself as opposed to
191 * using setval, because we cannot bail if
192 * the ioctl fails
193 */
194 memset((char *)&ifr, 0, sizeof(ifr));
195 strcpy(ifr.ifr_name, iface);
196 ifr.ifr_data = (caddr_t)&wreq;
197
198 printf("scanning ...");
199 fflush(stdout);
200 while (ioctl(s, SIOCGWAVELAN, &ifr) == -1) {
201 retries--;
202 if (retries >= 0) {
203 printf(".");
204 sleep(1);
205 } else
206 break;
207 errno = 0;
208 }
209
210 if (errno) {
211 set_if_flags(s, iface, flags);
212 close(s);
213 err(1, "ioctl");
214 }
215 printf("\nAP Information\n");
216
217 naps = (int)wreq.wi_val[0];
218 w = (struct wi_apinfo *)(((char *)&wreq.wi_val) + sizeof(int));
219 for ( i = 0; i < naps; i++, w++) {
220 printf("ap[%d]:\n", i);
221 if (w->scanreason) {
222 static char *scanm[] = {
223 "Host initiated",
224 "Firmware initiated",
225 "Inquiry request from host"
226 };
227 printf("\tScanReason:\t\t\t[ %s ]\n",
228 scanm[w->scanreason - 1]);
229 }
230 printf("\tnetname (SSID):\t\t\t[ ");
231 for (j = 0; j < w->namelen; j++) {
232 printf("%c", w->name[j]);
233 }
234 printf(" ]\n");
235 printf("\tBSSID:\t\t\t\t[ %02x:%02x:%02x:%02x:%02x:%02x ]\n",
236 w->bssid[0]&0xff, w->bssid[1]&0xff,
237 w->bssid[2]&0xff, w->bssid[3]&0xff,
238 w->bssid[4]&0xff, w->bssid[5]&0xff);
239 printf("\tChannel:\t\t\t[ %d ]\n", w->channel);
240 printf("\tQuality/Signal/Noise [signal]:\t[ %d / %d / %d ]\n"
241 "\t [dBm]:\t[ %d / %d / %d ]\n",
242 w->quality, w->signal, w->noise,
243 w->quality, w->signal - 149, w->noise - 149);
244 printf("\tBSS Beacon Interval [Kusec]:\t[ %d ]\n", w->interval);
245 printf("\tCapinfo:\t\t\t[ ");
246 if (w->capinfo & IEEE80211_CAPINFO_ESS)
247 printf("ESS ");
248 if (w->capinfo & IEEE80211_CAPINFO_PRIVACY)
249 printf("WEP ");
250 printf("]\n");
251
252 switch (w->rate) {
253 case WI_APRATE_1:
254 rate = 1;
255 break;
256 case WI_APRATE_2:
257 rate = 2;
258 break;
259 case WI_APRATE_5:
260 rate = 5.5;
261 break;
262 case WI_APRATE_11:
263 rate = 11;
264 break;
265 case WI_APRATE_0:
266 default:
267 rate = 0;
268 break;
269 }
270 if (rate) printf("\tDataRate [Mbps]:\t\t[ %d ]\n", rate);
271 }
272
273 set_if_flags(s, iface, flags);
274 close(s);
275 }
276
277 static void wi_getval(iface, wreq)
278 char *iface;
279 struct wi_req *wreq;
280 {
281 struct ifreq ifr;
282 int s;
283
284 bzero((char *)&ifr, sizeof(ifr));
285
286 strcpy(ifr.ifr_name, iface);
287 ifr.ifr_data = (caddr_t)wreq;
288
289 s = socket(AF_INET, SOCK_DGRAM, 0);
290
291 if (s == -1)
292 err(1, "socket");
293
294 if (ioctl(s, SIOCGWAVELAN, &ifr) == -1)
295 err(1, "SIOCGWAVELAN");
296
297 close(s);
298
299 return;
300 }
301
302 static void wi_setval(iface, wreq)
303 char *iface;
304 struct wi_req *wreq;
305 {
306 struct ifreq ifr;
307 int s;
308
309 bzero((char *)&ifr, sizeof(ifr));
310
311 strcpy(ifr.ifr_name, iface);
312 ifr.ifr_data = (caddr_t)wreq;
313
314 s = socket(AF_INET, SOCK_DGRAM, 0);
315
316 if (s == -1)
317 err(1, "socket");
318
319 if (ioctl(s, SIOCSWAVELAN, &ifr) == -1)
320 err(1, "SIOCSWAVELAN");
321
322 close(s);
323
324 return;
325 }
326
327 void wi_printstr(wreq)
328 struct wi_req *wreq;
329 {
330 char *ptr;
331 int i;
332
333 if (wreq->wi_type == WI_RID_SERIALNO) {
334 ptr = (char *)&wreq->wi_val;
335 for (i = 0; i < (wreq->wi_len - 1) * 2; i++) {
336 if (ptr[i] == '\0')
337 ptr[i] = ' ';
338 }
339 } else {
340 int len = le16toh(wreq->wi_val[0]);
341
342 ptr = (char *)&wreq->wi_val[1];
343 for (i = 0; i < len; i++) {
344 if (ptr[i] == '\0')
345 ptr[i] = ' ';
346 }
347 }
348
349 ptr[i] = '\0';
350 printf("[ %s ]", ptr);
351
352 return;
353 }
354
355 void wi_setstr(iface, code, str)
356 char *iface;
357 int code;
358 char *str;
359 {
360 struct wi_req wreq;
361
362 bzero((char *)&wreq, sizeof(wreq));
363
364 if (strlen(str) > 30)
365 errx(1, "string too long");
366
367 wreq.wi_type = code;
368 wreq.wi_len = 18;
369 wreq.wi_val[0] = htole16(strlen(str));
370 bcopy(str, (char *)&wreq.wi_val[1], strlen(str));
371
372 wi_setval(iface, &wreq);
373
374 return;
375 }
376
377 void wi_setbytes(iface, code, bytes, len)
378 char *iface;
379 int code;
380 char *bytes;
381 int len;
382 {
383 struct wi_req wreq;
384
385 bzero((char *)&wreq, sizeof(wreq));
386
387 wreq.wi_type = code;
388 wreq.wi_len = (len / 2) + 1;
389 bcopy(bytes, (char *)&wreq.wi_val[0], len);
390
391 wi_setval(iface, &wreq);
392
393 return;
394 }
395
396 void wi_setword(iface, code, word)
397 char *iface;
398 int code;
399 int word;
400 {
401 struct wi_req wreq;
402
403 bzero((char *)&wreq, sizeof(wreq));
404
405 wreq.wi_type = code;
406 wreq.wi_len = 2;
407 wreq.wi_val[0] = htole16(word);
408
409 wi_setval(iface, &wreq);
410
411 return;
412 }
413
414 void wi_sethex(iface, code, str)
415 char *iface;
416 int code;
417 char *str;
418 {
419 struct ether_addr *addr;
420
421 addr = ether_aton(str);
422 if (addr == NULL)
423 errx(1, "badly formatted address");
424
425 wi_setbytes(iface, code, (char *)addr, ETHER_ADDR_LEN);
426
427 return;
428 }
429
430 static int
431 wi_hex2int(char c)
432 {
433 if (c >= '0' && c <= '9')
434 return (c - '0');
435 if (c >= 'A' && c <= 'F')
436 return (c - 'A' + 10);
437 if (c >= 'a' && c <= 'f')
438 return (c - 'a' + 10);
439
440 return (0);
441 }
442
443 static void wi_str2key(s, k)
444 char *s;
445 struct wi_key *k;
446 {
447 int n, i;
448 char *p;
449
450 /* Is this a hex string? */
451 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
452 /* Yes, convert to int. */
453 n = 0;
454 p = (char *)&k->wi_keydat[0];
455 for (i = 2; i < strlen(s); i+= 2) {
456 *p++ = (wi_hex2int(s[i]) << 4) + wi_hex2int(s[i + 1]);
457 n++;
458 }
459 k->wi_keylen = htole16(n);
460 } else {
461 /* No, just copy it in. */
462 bcopy(s, k->wi_keydat, strlen(s));
463 k->wi_keylen = htole16(strlen(s));
464 }
465
466 return;
467 }
468
469 static void wi_setkeys(iface, key, idx)
470 char *iface;
471 char *key;
472 int idx;
473 {
474 struct wi_req wreq;
475 struct wi_ltv_keys *keys;
476 struct wi_key *k;
477
478 bzero((char *)&wreq, sizeof(wreq));
479 wreq.wi_len = WI_MAX_DATALEN;
480 wreq.wi_type = WI_RID_WEP_AVAIL;
481
482 wi_getval(iface, &wreq);
483 if (le16toh(wreq.wi_val[0]) == 0)
484 err(1, "no WEP option available on this card");
485
486 bzero((char *)&wreq, sizeof(wreq));
487 wreq.wi_len = WI_MAX_DATALEN;
488 wreq.wi_type = WI_RID_DEFLT_CRYPT_KEYS;
489
490 wi_getval(iface, &wreq);
491 keys = (struct wi_ltv_keys *)&wreq;
492
493 if (key[0] == '0' && (key[1] == 'x' || key[1] == 'X')) {
494 if (strlen(key) > 30)
495 err(1, "encryption key must be no "
496 "more than 28 hex digits long");
497 } else {
498 if (strlen(key) > 14)
499 err(1, "encryption key must be no "
500 "more than 14 characters long");
501 }
502
503 if (idx > 3)
504 err(1, "only 4 encryption keys available");
505
506 k = &keys->wi_keys[idx];
507 wi_str2key(key, k);
508
509 wreq.wi_len = (sizeof(struct wi_ltv_keys) / 2) + 1;
510 wreq.wi_type = WI_RID_DEFLT_CRYPT_KEYS;
511 wi_setval(iface, &wreq);
512
513 return;
514 }
515
516 static void wi_printkeys(wreq)
517 struct wi_req *wreq;
518 {
519 int i, j, bn;
520 struct wi_key *k;
521 struct wi_ltv_keys *keys;
522 char *ptr;
523
524 keys = (struct wi_ltv_keys *)wreq;
525
526 for (i = 0, bn = 0; i < 4; i++, bn = 0) {
527 k = &keys->wi_keys[i];
528 ptr = (char *)k->wi_keydat;
529 for (j = 0; j < le16toh(k->wi_keylen); j++) {
530 if (!isprint((unsigned char) ptr[j])) {
531 bn = 1;
532 break;
533 }
534 }
535
536 if (bn) {
537 printf("[ 0x");
538 for (j = 0; j < le16toh(k->wi_keylen); j++)
539 printf("%02x", ((unsigned char *) ptr)[j]);
540 printf(" ]");
541 } else {
542 ptr[j] = '\0';
543 printf("[ %s ]", ptr);
544 }
545 }
546
547 return;
548 };
549
550 void wi_printwords(wreq)
551 struct wi_req *wreq;
552 {
553 int i;
554
555 printf("[ ");
556 for (i = 0; i < wreq->wi_len - 1; i++)
557 printf("%d ", le16toh(wreq->wi_val[i]));
558 printf("]");
559
560 return;
561 }
562
563 void wi_printbool(wreq)
564 struct wi_req *wreq;
565 {
566 if (le16toh(wreq->wi_val[0]))
567 printf("[ On ]");
568 else
569 printf("[ Off ]");
570
571 return;
572 }
573
574 void wi_printhex(wreq)
575 struct wi_req *wreq;
576 {
577 int i;
578 unsigned char *c;
579
580 c = (unsigned char *)&wreq->wi_val;
581
582 printf("[ ");
583 for (i = 0; i < (wreq->wi_len - 1) * 2; i++) {
584 printf("%02x", c[i]);
585 if (i < ((wreq->wi_len - 1) * 2) - 1)
586 printf(":");
587 }
588
589 printf(" ]");
590 return;
591 }
592
593 static struct wi_table wi_table[] = {
594 { WI_RID_SERIALNO, WI_STRING, "NIC serial number:\t\t\t" },
595 { WI_RID_NODENAME, WI_STRING, "Station name:\t\t\t\t",
596 's', "station name" },
597 { WI_RID_OWN_SSID, WI_STRING, "SSID for IBSS creation:\t\t\t",
598 'q', "own SSID" },
599 { WI_RID_CURRENT_SSID, WI_STRING, "Current netname (SSID):\t\t\t" },
600 { WI_RID_DESIRED_SSID, WI_STRING, "Desired netname (SSID):\t\t\t",
601 'n', "network name" },
602 { WI_RID_CURRENT_BSSID, WI_HEXBYTES, "Current BSSID:\t\t\t\t" },
603 { WI_RID_CHANNEL_LIST, WI_WORDS, "Channel list:\t\t\t\t" },
604 { WI_RID_OWN_CHNL, WI_WORDS, "IBSS channel:\t\t\t\t",
605 'f', "frequency" },
606 { WI_RID_CURRENT_CHAN, WI_WORDS, "Current channel:\t\t\t" },
607 { WI_RID_COMMS_QUALITY, WI_WORDS, "Comms quality/signal/noise:\t\t" },
608 { WI_RID_PROMISC, WI_BOOL, "Promiscuous mode:\t\t\t" },
609 { WI_RID_PORTTYPE, WI_WORDS, "Port type (1=BSS, 3=ad-hoc):\t\t",
610 'p', "port type" },
611 { WI_RID_MAC_NODE, WI_HEXBYTES, "MAC address:\t\t\t\t",
612 'm', "MAC address" },
613 { WI_RID_TX_RATE, WI_WORDS, "TX rate (selection):\t\t\t",
614 't', "TX rate" },
615 { WI_RID_CUR_TX_RATE, WI_WORDS, "TX rate (actual speed):\t\t\t"},
616 { WI_RID_OWN_BEACON_INT, WI_WORDS, "Beacon Interval (current) [Kusec]:\t"},
617 { WI_RID_MAX_DATALEN, WI_WORDS, "Maximum data length:\t\t\t",
618 'd', "maximum data length" },
619 { WI_RID_RTS_THRESH, WI_WORDS, "RTS/CTS handshake threshold:\t\t",
620 'r', "RTS threshold" },
621 { WI_RID_CREATE_IBSS, WI_BOOL, "Create IBSS:\t\t\t\t",
622 'c', "create ibss" },
623 { WI_RID_MICROWAVE_OVEN, WI_WORDS, "Microwave oven robustness:\t\t",
624 'M', "microwave oven robustness enabled" },
625 { WI_RID_ROAMING_MODE, WI_WORDS, "Roaming mode(1:firm,3:disable):\t\t",
626 'R', "roaming mode" },
627 { WI_RID_SYSTEM_SCALE, WI_WORDS, "Access point density:\t\t\t",
628 'a', "system scale" },
629 { WI_RID_PM_ENABLED, WI_WORDS, "Power Mgmt (1=on, 0=off):\t\t",
630 'P', "power management enabled" },
631 { WI_RID_MAX_SLEEP, WI_WORDS, "Max sleep time (msec):\t\t\t",
632 'S', "max sleep duration" },
633 { 0, WI_NONE }
634 };
635
636 static struct wi_table wi_crypt_table[] = {
637 { WI_RID_ENCRYPTION, WI_BOOL, "WEP encryption:\t\t\t\t",
638 'e', "encryption" },
639 { WI_RID_AUTH_CNTL, WI_WORDS, "Authentication type \n(1=OpenSys, 2=Shared Key):\t\t",
640 'A', "authentication type" },
641 { WI_RID_TX_CRYPT_KEY, WI_WORDS, "TX encryption key:\t\t\t" },
642 { WI_RID_DEFLT_CRYPT_KEYS, WI_KEYSTRUCT, "Encryption keys:\t\t\t" },
643 { 0, WI_NONE }
644 };
645
646 static struct wi_table *wi_tables[] = {
647 wi_table,
648 wi_crypt_table,
649 NULL
650 };
651
652 static struct wi_table *
653 wi_optlookup(table, opt)
654 struct wi_table *table;
655 int opt;
656 {
657 struct wi_table *wt;
658
659 for (wt = table; wt->wi_type != 0; wt++)
660 if (wt->wi_opt == opt)
661 return (wt);
662 return (NULL);
663 }
664
665 static void wi_dumpinfo(iface)
666 char *iface;
667 {
668 struct wi_req wreq;
669 int i, has_wep;
670 struct wi_table *w;
671
672 bzero((char *)&wreq, sizeof(wreq));
673
674 wreq.wi_len = WI_MAX_DATALEN;
675 wreq.wi_type = WI_RID_WEP_AVAIL;
676
677 wi_getval(iface, &wreq);
678 has_wep = le16toh(wreq.wi_val[0]);
679
680 w = wi_table;
681
682 for (i = 0; w[i].wi_code != WI_NONE; i++) {
683 bzero((char *)&wreq, sizeof(wreq));
684
685 wreq.wi_len = WI_MAX_DATALEN;
686 wreq.wi_type = w[i].wi_type;
687
688 wi_getval(iface, &wreq);
689 printf("%s", w[i].wi_label);
690 switch (w[i].wi_code) {
691 case WI_STRING:
692 wi_printstr(&wreq);
693 break;
694 case WI_WORDS:
695 wi_printwords(&wreq);
696 break;
697 case WI_BOOL:
698 wi_printbool(&wreq);
699 break;
700 case WI_HEXBYTES:
701 wi_printhex(&wreq);
702 break;
703 default:
704 break;
705 }
706 printf("\n");
707 }
708
709 if (has_wep) {
710 w = wi_crypt_table;
711 for (i = 0; w[i].wi_code != WI_NONE; i++) {
712 bzero((char *)&wreq, sizeof(wreq));
713
714 wreq.wi_len = WI_MAX_DATALEN;
715 wreq.wi_type = w[i].wi_type;
716
717 wi_getval(iface, &wreq);
718 printf("%s", w[i].wi_label);
719 switch (w[i].wi_code) {
720 case WI_STRING:
721 wi_printstr(&wreq);
722 break;
723 case WI_WORDS:
724 if (wreq.wi_type == WI_RID_TX_CRYPT_KEY)
725 wreq.wi_val[0] =
726 htole16(le16toh(wreq.wi_val[0]) + 1);
727 wi_printwords(&wreq);
728 break;
729 case WI_BOOL:
730 wi_printbool(&wreq);
731 break;
732 case WI_HEXBYTES:
733 wi_printhex(&wreq);
734 break;
735 case WI_KEYSTRUCT:
736 wi_printkeys(&wreq);
737 break;
738 default:
739 break;
740 }
741 printf("\n");
742 }
743 }
744
745 return;
746 }
747
748 static void wi_dumpstats(iface)
749 char *iface;
750 {
751 struct wi_req wreq;
752 struct wi_counters *c;
753
754 bzero((char *)&wreq, sizeof(wreq));
755 wreq.wi_len = WI_MAX_DATALEN;
756 wreq.wi_type = WI_RID_IFACE_STATS;
757
758 wi_getval(iface, &wreq);
759
760 c = (struct wi_counters *)&wreq.wi_val;
761
762 /* XXX native byte order */
763 printf("Transmitted unicast frames:\t\t%d\n",
764 c->wi_tx_unicast_frames);
765 printf("Transmitted multicast frames:\t\t%d\n",
766 c->wi_tx_multicast_frames);
767 printf("Transmitted fragments:\t\t\t%d\n",
768 c->wi_tx_fragments);
769 printf("Transmitted unicast octets:\t\t%d\n",
770 c->wi_tx_unicast_octets);
771 printf("Transmitted multicast octets:\t\t%d\n",
772 c->wi_tx_multicast_octets);
773 printf("Single transmit retries:\t\t%d\n",
774 c->wi_tx_single_retries);
775 printf("Multiple transmit retries:\t\t%d\n",
776 c->wi_tx_multi_retries);
777 printf("Transmit retry limit exceeded:\t\t%d\n",
778 c->wi_tx_retry_limit);
779 printf("Transmit discards:\t\t\t%d\n",
780 c->wi_tx_discards);
781 printf("Transmit discards due to wrong SA:\t%d\n",
782 c->wi_tx_discards_wrong_sa);
783 printf("Received unicast frames:\t\t%d\n",
784 c->wi_rx_unicast_frames);
785 printf("Received multicast frames:\t\t%d\n",
786 c->wi_rx_multicast_frames);
787 printf("Received fragments:\t\t\t%d\n",
788 c->wi_rx_fragments);
789 printf("Received unicast octets:\t\t%d\n",
790 c->wi_rx_unicast_octets);
791 printf("Received multicast octets:\t\t%d\n",
792 c->wi_rx_multicast_octets);
793 printf("Receive FCS errors:\t\t\t%d\n",
794 c->wi_rx_fcs_errors);
795 printf("Receive discards due to no buffer:\t%d\n",
796 c->wi_rx_discards_nobuf);
797 printf("Can't decrypt WEP frame:\t\t%d\n",
798 c->wi_rx_WEP_cant_decrypt);
799 printf("Received message fragments:\t\t%d\n",
800 c->wi_rx_msg_in_msg_frags);
801 printf("Received message bad fragments:\t\t%d\n",
802 c->wi_rx_msg_in_bad_msg_frags);
803
804 return;
805 }
806
807 static void
808 usage()
809 {
810
811 fprintf(stderr,
812 "usage: %s interface "
813 "[-oD] [-t tx rate] [-n network name] [-s station name]\n"
814 " [-e 0|1] [-k key [-v 1|2|3|4]] [-T 1|2|3|4]\n"
815 " [-c 0|1] [-q SSID] [-p port type] [-a access point density]\n"
816 " [-m MAC address] [-d max data length] [-r RTS threshold]\n"
817 " [-f frequency] [-M 0|1] [-P 0|1] [-S max sleep duration]\n"
818 " [-A 0|1 ] [-R 1|3]\n"
819 ,
820 getprogname());
821 exit(1);
822 }
823
824 int main(argc, argv)
825 int argc;
826 char *argv[];
827 {
828 struct wi_table *wt, **table;
829 char *iface, *key, *keyv[4], *tx_crypt_key;
830 int ch, dumpinfo, dumpstats, modifier, oldind, apscan;
831
832 #define SET_OPERAND(opr, desc) do { \
833 if ((opr) == NULL) \
834 (opr) = optarg; \
835 else \
836 warnx("%s is already specified to %s", \
837 desc, (opr)); \
838 } while (0)
839
840 dumpinfo = 1;
841 dumpstats = 0;
842 apscan = 0;
843 iface = key = keyv[0] = keyv[1] = keyv[2] = keyv[3] =
844 tx_crypt_key = NULL;
845
846 if (argc > 1 && argv[1][0] != '-') {
847 iface = argv[1];
848 optind++;
849 }
850
851 while ((ch = getopt(argc, argv,
852 "a:c:d:e:f:hi:k:m:n:op:q:r:s:t:A:M:S:P:R:T:DZ:")) != -1) {
853 if (ch != 'i')
854 dumpinfo = 0;
855 /*
856 * Lookup generic options and remeber operand if found.
857 */
858 for (table = wi_tables; *table != NULL; table++)
859 if ((wt = wi_optlookup(*table, ch)) != NULL) {
860 SET_OPERAND(wt->wi_optval, wt->wi_desc);
861 break;
862 }
863 if (wt == NULL)
864 /*
865 * Handle special options.
866 */
867 switch (ch) {
868 case 'o':
869 dumpstats = 1;
870 break;
871 case 'i':
872 SET_OPERAND(iface, "interface");
873 break;
874 case 'k':
875 key = optarg;
876 oldind = optind;
877 opterr = 0;
878 ch = getopt(argc, argv, "v:");
879 opterr = 1;
880 switch (ch) {
881 case 'v':
882 modifier = atoi(optarg) - 1;
883 break;
884 default:
885 modifier = 0;
886 optind = oldind;
887 break;
888 }
889 keyv[modifier] = key;
890 break;
891 case 'T':
892 SET_OPERAND(tx_crypt_key, "TX encryption key");
893 break;
894 case 'D':
895 apscan = 1;
896 break;
897 case 'h':
898 default:
899 usage();
900 break;
901 }
902 }
903
904 if (iface == NULL)
905 usage();
906
907 for (table = wi_tables; *table != NULL; table++)
908 for (wt = *table; wt->wi_code != WI_NONE; wt++)
909 if (wt->wi_optval != NULL) {
910 switch (wt->wi_code) {
911 case WI_BOOL:
912 case WI_WORDS:
913 wi_setword(iface, wt->wi_type,
914 atoi(wt->wi_optval));
915 break;
916 case WI_STRING:
917 wi_setstr(iface, wt->wi_type,
918 wt->wi_optval);
919 break;
920 case WI_HEXBYTES:
921 wi_sethex(iface, wt->wi_type,
922 wt->wi_optval);
923 break;
924 }
925 }
926
927 if (tx_crypt_key != NULL)
928 wi_setword(iface, WI_RID_TX_CRYPT_KEY, atoi(tx_crypt_key) - 1);
929
930 for (modifier = 0; modifier < sizeof(keyv) / sizeof(keyv[0]);
931 modifier++)
932 if (keyv[modifier] != NULL)
933 wi_setkeys(iface, keyv[modifier], modifier);
934
935 if (dumpstats)
936 wi_dumpstats(iface);
937 if (dumpinfo)
938 wi_dumpinfo(iface);
939 if (apscan)
940 wi_apscan(iface);
941
942 exit(0);
943 }
944