Home | History | Annotate | Line # | Download | only in wiconfig
wiconfig.c revision 1.15
      1 /*	$NetBSD: wiconfig.c,v 1.15 2002/01/21 11:35:06 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.15 2002/01/21 11:35:06 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_MAX_DATALEN, WI_WORDS, "Maximum data length:\t\t\t",
    577 	    'd', "maximum data length" },
    578 	{ WI_RID_RTS_THRESH, WI_WORDS, "RTS/CTS handshake threshold:\t\t",
    579 	    'r', "RTS threshold" },
    580 	{ WI_RID_CREATE_IBSS, WI_BOOL, "Create IBSS:\t\t\t\t",
    581 	    'c', "create ibss" },
    582 	{ WI_RID_MICROWAVE_OVEN, WI_WORDS, "Microwave oven robustness:\t\t",
    583 	    'M', "microwave oven robustness enabled" },
    584 	{ WI_RID_ROAMING_MODE, WI_WORDS, "Roaming mode(1:firm,3:disable):\t\t",
    585 	    'R', "roaming mode" },
    586 	{ WI_RID_SYSTEM_SCALE, WI_WORDS, "Access point density:\t\t\t",
    587 	    'a', "system scale" },
    588 	{ WI_RID_PM_ENABLED, WI_WORDS, "Power Mgmt (1=on, 0=off):\t\t",
    589 	    'P', "power management enabled" },
    590 	{ WI_RID_MAX_SLEEP, WI_WORDS, "Max sleep time (msec):\t\t\t",
    591 	    'S', "max sleep duration" },
    592 	{ 0, WI_NONE }
    593 };
    594 
    595 static struct wi_table wi_crypt_table[] = {
    596 	{ WI_RID_ENCRYPTION, WI_BOOL, "WEP encryption:\t\t\t\t",
    597 	    'e', "encryption" },
    598 	{ WI_RID_AUTH_CNTL, WI_WORDS, "Authentication type \n(1=OpenSys, 2=Shared Key):\t\t",
    599 	    'A', "authentication type" },
    600         { WI_RID_TX_CRYPT_KEY, WI_WORDS, "TX encryption key:\t\t\t" },
    601         { WI_RID_DEFLT_CRYPT_KEYS, WI_KEYSTRUCT, "Encryption keys:\t\t\t" },
    602 	{ 0, WI_NONE }
    603 };
    604 
    605 static struct wi_table *wi_tables[] = {
    606 	wi_table,
    607 	wi_crypt_table,
    608 	NULL
    609 };
    610 
    611 static struct wi_table *
    612 wi_optlookup(table, opt)
    613 	struct wi_table *table;
    614 	int opt;
    615 {
    616 	struct wi_table *wt;
    617 
    618 	for (wt = table; wt->wi_type != 0; wt++)
    619 		if (wt->wi_opt == opt)
    620 			return (wt);
    621 	return (NULL);
    622 }
    623 
    624 static void wi_dumpinfo(iface)
    625 	char			*iface;
    626 {
    627 	struct wi_req		wreq;
    628 	int			i, has_wep;
    629 	struct wi_table		*w;
    630 
    631 	bzero((char *)&wreq, sizeof(wreq));
    632 
    633 	wreq.wi_len = WI_MAX_DATALEN;
    634 	wreq.wi_type = WI_RID_WEP_AVAIL;
    635 
    636 	wi_getval(iface, &wreq);
    637 	has_wep = le16toh(wreq.wi_val[0]);
    638 
    639 	w = wi_table;
    640 
    641 	for (i = 0; w[i].wi_code != WI_NONE; i++) {
    642 		bzero((char *)&wreq, sizeof(wreq));
    643 
    644 		wreq.wi_len = WI_MAX_DATALEN;
    645 		wreq.wi_type = w[i].wi_type;
    646 
    647 		wi_getval(iface, &wreq);
    648 		printf("%s", w[i].wi_label);
    649 		switch (w[i].wi_code) {
    650 		case WI_STRING:
    651 			wi_printstr(&wreq);
    652 			break;
    653 		case WI_WORDS:
    654 			wi_printwords(&wreq);
    655 			break;
    656 		case WI_BOOL:
    657 			wi_printbool(&wreq);
    658 			break;
    659 		case WI_HEXBYTES:
    660 			wi_printhex(&wreq);
    661 			break;
    662 		default:
    663 			break;
    664 		}
    665 		printf("\n");
    666 	}
    667 
    668 	if (has_wep) {
    669 		w = wi_crypt_table;
    670 		for (i = 0; w[i].wi_code != WI_NONE; i++) {
    671 			bzero((char *)&wreq, sizeof(wreq));
    672 
    673 			wreq.wi_len = WI_MAX_DATALEN;
    674 			wreq.wi_type = w[i].wi_type;
    675 
    676 			wi_getval(iface, &wreq);
    677 			printf("%s", w[i].wi_label);
    678 			switch (w[i].wi_code) {
    679 			case WI_STRING:
    680 				wi_printstr(&wreq);
    681 				break;
    682 			case WI_WORDS:
    683 				if (wreq.wi_type == WI_RID_TX_CRYPT_KEY)
    684 					wreq.wi_val[0] =
    685 					  htole16(le16toh(wreq.wi_val[0]) + 1);
    686 				wi_printwords(&wreq);
    687 				break;
    688 			case WI_BOOL:
    689 				wi_printbool(&wreq);
    690 				break;
    691 			case WI_HEXBYTES:
    692 				wi_printhex(&wreq);
    693 				break;
    694 			case WI_KEYSTRUCT:
    695 				wi_printkeys(&wreq);
    696 				break;
    697 			default:
    698 				break;
    699 			}
    700 			printf("\n");
    701 		}
    702 	}
    703 
    704 	return;
    705 }
    706 
    707 static void wi_dumpstats(iface)
    708 	char			*iface;
    709 {
    710 	struct wi_req		wreq;
    711 	struct wi_counters	*c;
    712 
    713 	bzero((char *)&wreq, sizeof(wreq));
    714 	wreq.wi_len = WI_MAX_DATALEN;
    715 	wreq.wi_type = WI_RID_IFACE_STATS;
    716 
    717 	wi_getval(iface, &wreq);
    718 
    719 	c = (struct wi_counters *)&wreq.wi_val;
    720 
    721 	/* XXX native byte order */
    722 	printf("Transmitted unicast frames:\t\t%d\n",
    723 	    c->wi_tx_unicast_frames);
    724 	printf("Transmitted multicast frames:\t\t%d\n",
    725 	    c->wi_tx_multicast_frames);
    726 	printf("Transmitted fragments:\t\t\t%d\n",
    727 	    c->wi_tx_fragments);
    728 	printf("Transmitted unicast octets:\t\t%d\n",
    729 	    c->wi_tx_unicast_octets);
    730 	printf("Transmitted multicast octets:\t\t%d\n",
    731 	    c->wi_tx_multicast_octets);
    732 	printf("Single transmit retries:\t\t%d\n",
    733 	    c->wi_tx_single_retries);
    734 	printf("Multiple transmit retries:\t\t%d\n",
    735 	    c->wi_tx_multi_retries);
    736 	printf("Transmit retry limit exceeded:\t\t%d\n",
    737 	    c->wi_tx_retry_limit);
    738 	printf("Transmit discards:\t\t\t%d\n",
    739 	    c->wi_tx_discards);
    740 	printf("Transmit discards due to wrong SA:\t%d\n",
    741 	    c->wi_tx_discards_wrong_sa);
    742 	printf("Received unicast frames:\t\t%d\n",
    743 	    c->wi_rx_unicast_frames);
    744 	printf("Received multicast frames:\t\t%d\n",
    745 	    c->wi_rx_multicast_frames);
    746 	printf("Received fragments:\t\t\t%d\n",
    747 	    c->wi_rx_fragments);
    748 	printf("Received unicast octets:\t\t%d\n",
    749 	    c->wi_rx_unicast_octets);
    750 	printf("Received multicast octets:\t\t%d\n",
    751 	    c->wi_rx_multicast_octets);
    752 	printf("Receive FCS errors:\t\t\t%d\n",
    753 	    c->wi_rx_fcs_errors);
    754 	printf("Receive discards due to no buffer:\t%d\n",
    755 	    c->wi_rx_discards_nobuf);
    756 	printf("Can't decrypt WEP frame:\t\t%d\n",
    757 	    c->wi_rx_WEP_cant_decrypt);
    758 	printf("Received message fragments:\t\t%d\n",
    759 	    c->wi_rx_msg_in_msg_frags);
    760 	printf("Received message bad fragments:\t\t%d\n",
    761 	    c->wi_rx_msg_in_bad_msg_frags);
    762 
    763 	return;
    764 }
    765 
    766 static void
    767 usage()
    768 {
    769 
    770 	fprintf(stderr,
    771 	    "usage: %s interface "
    772 	    "[-oD] [-t tx rate] [-n network name] [-s station name]\n"
    773 	    "       [-e 0|1] [-k key [-v 1|2|3|4]] [-T 1|2|3|4]\n"
    774 	    "       [-c 0|1] [-q SSID] [-p port type] [-a access point density]\n"
    775 	    "       [-m MAC address] [-d max data length] [-r RTS threshold]\n"
    776 	    "       [-f frequency] [-M 0|1] [-P 0|1] [-S max sleep duration]\n"
    777 	    "       [-A 0|1 ] [-R 1|3]\n"
    778 	    ,
    779 	    getprogname());
    780 	exit(1);
    781 }
    782 
    783 int main(argc, argv)
    784 	int			argc;
    785 	char			*argv[];
    786 {
    787 	struct wi_table *wt, **table;
    788 	char *iface, *key, *keyv[4], *tx_crypt_key;
    789 	int ch, dumpinfo, dumpstats, modifier, oldind, apscan;
    790 
    791 #define	SET_OPERAND(opr, desc) do {				\
    792 	if ((opr) == NULL)					\
    793 		(opr) = optarg;					\
    794 	else							\
    795 		warnx("%s is already specified to %s",		\
    796 		    desc, (opr));				\
    797 } while (0)
    798 
    799 	dumpinfo = 1;
    800 	dumpstats = 0;
    801 	apscan = 0;
    802 	iface = key = keyv[0] = keyv[1] = keyv[2] = keyv[3] =
    803 	    tx_crypt_key = NULL;
    804 
    805 	if (argc > 1 && argv[1][0] != '-') {
    806 		iface = argv[1];
    807 		optind++;
    808 	}
    809 
    810 	while ((ch = getopt(argc, argv,
    811 	    "a:c:d:e:f:hi:k:m:n:op:q:r:s:t:A:M:S:P:R:T:DZ:")) != -1) {
    812 		if (ch != 'i')
    813 			dumpinfo = 0;
    814 		/*
    815 		 * Lookup generic options and remeber operand if found.
    816 		 */
    817 		for (table = wi_tables; *table != NULL; table++)
    818 			if ((wt = wi_optlookup(*table, ch)) != NULL) {
    819 				SET_OPERAND(wt->wi_optval, wt->wi_desc);
    820 				break;
    821 			}
    822 		if (wt == NULL)
    823 			/*
    824 			 * Handle special options.
    825 			 */
    826 			switch (ch) {
    827 			case 'o':
    828 				dumpstats = 1;
    829 				break;
    830 			case 'i':
    831 				SET_OPERAND(iface, "interface");
    832 				break;
    833 			case 'k':
    834 				key = optarg;
    835 				oldind = optind;
    836 				opterr = 0;
    837 				ch = getopt(argc, argv, "v:");
    838 				opterr = 1;
    839 				switch (ch) {
    840 				case 'v':
    841 					modifier = atoi(optarg) - 1;
    842 					break;
    843 				default:
    844 					modifier = 0;
    845 					optind = oldind;
    846 					break;
    847 				}
    848 				keyv[modifier] = key;
    849 				break;
    850 			case 'T':
    851 				SET_OPERAND(tx_crypt_key, "TX encryption key");
    852 				break;
    853 			case 'D':
    854 				apscan = 1;
    855 				break;
    856 			case 'h':
    857 			default:
    858 				usage();
    859 				break;
    860 			}
    861 	}
    862 
    863 	if (iface == NULL)
    864 		usage();
    865 
    866 	for (table = wi_tables; *table != NULL; table++)
    867 		for (wt = *table; wt->wi_code != WI_NONE; wt++)
    868 			if (wt->wi_optval != NULL) {
    869 				switch (wt->wi_code) {
    870 				case WI_BOOL:
    871 				case WI_WORDS:
    872 					wi_setword(iface, wt->wi_type,
    873 					    atoi(wt->wi_optval));
    874 					break;
    875 				case WI_STRING:
    876 					wi_setstr(iface, wt->wi_type,
    877 					    wt->wi_optval);
    878 					break;
    879 				case WI_HEXBYTES:
    880 					wi_sethex(iface, wt->wi_type,
    881 					    wt->wi_optval);
    882 					break;
    883 				}
    884 			}
    885 
    886 	if (tx_crypt_key != NULL)
    887 		wi_setword(iface, WI_RID_TX_CRYPT_KEY, atoi(tx_crypt_key) - 1);
    888 
    889 	for (modifier = 0; modifier < sizeof(keyv) / sizeof(keyv[0]);
    890 	    modifier++)
    891 		if (keyv[modifier] != NULL)
    892 			wi_setkeys(iface, keyv[modifier], modifier);
    893 
    894 	if (dumpstats)
    895 		wi_dumpstats(iface);
    896 	if (dumpinfo)
    897 		wi_dumpinfo(iface);
    898 	if (apscan)
    899 		wi_apscan(iface);
    900 
    901 	exit(0);
    902 }
    903