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