Home | History | Annotate | Line # | Download | only in wiconfig
wiconfig.c revision 1.32
      1 /*	$NetBSD: wiconfig.c,v 1.32 2003/11/16 09:41:01 dyoung 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.32 2003/11/16 09:41:01 dyoung 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         { WI_RID_TX_CRYPT_KEY, WI_WORDS, "TX encryption key:\t\t\t" },
    615         { WI_RID_DEFLT_CRYPT_KEYS, WI_KEYSTRUCT, "Encryption keys:\t\t\t" },
    616 	{ 0, WI_NONE }
    617 };
    618 
    619 static struct wi_table *wi_tables[] = {
    620 	wi_table,
    621 	wi_crypt_table,
    622 	NULL
    623 };
    624 
    625 static struct wi_table *
    626 wi_optlookup(table, opt)
    627 	struct wi_table *table;
    628 	int opt;
    629 {
    630 	struct wi_table *wt;
    631 
    632 	for (wt = table; wt->wi_type != 0; wt++)
    633 		if (wt->wi_opt == opt)
    634 			return (wt);
    635 	return (NULL);
    636 }
    637 
    638 static void wi_dumpinfo(iface)
    639 	char			*iface;
    640 {
    641 	struct wi_req		wreq;
    642 	int			i, has_wep;
    643 	struct wi_table		*w;
    644 
    645 	bzero((char *)&wreq, sizeof(wreq));
    646 
    647 	wreq.wi_len = WI_MAX_DATALEN;
    648 	wreq.wi_type = WI_RID_WEP_AVAIL;
    649 
    650 	wi_getval(iface, &wreq);
    651 	has_wep = le16toh(wreq.wi_val[0]);
    652 
    653 	w = wi_table;
    654 
    655 	for (i = 0; w[i].wi_code != WI_NONE; i++) {
    656 		bzero((char *)&wreq, sizeof(wreq));
    657 
    658 		wreq.wi_len = WI_MAX_DATALEN;
    659 		wreq.wi_type = w[i].wi_type;
    660 
    661 		wi_getval(iface, &wreq);
    662 		printf("%s", w[i].wi_label);
    663 		switch (w[i].wi_code) {
    664 		case WI_STRING:
    665 			wi_printstr(&wreq);
    666 			break;
    667 		case WI_WORDS:
    668 			wi_printwords(&wreq);
    669 			break;
    670 		case WI_BOOL:
    671 			wi_printbool(&wreq);
    672 			break;
    673 		case WI_HEXBYTES:
    674 			wi_printhex(&wreq);
    675 			break;
    676 		case WI_BITS:
    677 			wi_printbits(&wreq);
    678 			break;
    679 		case WI_VENDOR:
    680 			wi_printvendor(&wreq);
    681 			break;
    682 		default:
    683 			break;
    684 		}
    685 		printf("\n");
    686 	}
    687 
    688 	if (has_wep) {
    689 		w = wi_crypt_table;
    690 		for (i = 0; w[i].wi_code != WI_NONE; i++) {
    691 			bzero((char *)&wreq, sizeof(wreq));
    692 
    693 			wreq.wi_len = WI_MAX_DATALEN;
    694 			wreq.wi_type = w[i].wi_type;
    695 
    696 			wi_getval(iface, &wreq);
    697 			printf("%s", w[i].wi_label);
    698 			switch (w[i].wi_code) {
    699 			case WI_STRING:
    700 				wi_printstr(&wreq);
    701 				break;
    702 			case WI_WORDS:
    703 				if (wreq.wi_type == WI_RID_TX_CRYPT_KEY)
    704 					wreq.wi_val[0] =
    705 					  htole16(le16toh(wreq.wi_val[0]) + 1);
    706 				wi_printwords(&wreq);
    707 				break;
    708 			case WI_BOOL:
    709 				wi_printbool(&wreq);
    710 				break;
    711 			case WI_HEXBYTES:
    712 				wi_printhex(&wreq);
    713 				break;
    714 			case WI_KEYSTRUCT:
    715 				wi_printkeys(&wreq);
    716 				break;
    717 			default:
    718 				break;
    719 			}
    720 			printf("\n");
    721 		}
    722 	}
    723 
    724 	return;
    725 }
    726 
    727 static void wi_dumpstats(iface)
    728 	char			*iface;
    729 {
    730 	struct wi_req		wreq;
    731 	struct wi_counters	*c;
    732 
    733 	bzero((char *)&wreq, sizeof(wreq));
    734 	wreq.wi_len = WI_MAX_DATALEN;
    735 	wreq.wi_type = WI_RID_IFACE_STATS;
    736 
    737 	wi_getval(iface, &wreq);
    738 
    739 	c = (struct wi_counters *)&wreq.wi_val;
    740 
    741 	/* XXX native byte order */
    742 	printf("Transmitted unicast frames:\t\t%d\n",
    743 	    c->wi_tx_unicast_frames);
    744 	printf("Transmitted multicast frames:\t\t%d\n",
    745 	    c->wi_tx_multicast_frames);
    746 	printf("Transmitted fragments:\t\t\t%d\n",
    747 	    c->wi_tx_fragments);
    748 	printf("Transmitted unicast octets:\t\t%d\n",
    749 	    c->wi_tx_unicast_octets);
    750 	printf("Transmitted multicast octets:\t\t%d\n",
    751 	    c->wi_tx_multicast_octets);
    752 	printf("Single transmit retries:\t\t%d\n",
    753 	    c->wi_tx_single_retries);
    754 	printf("Multiple transmit retries:\t\t%d\n",
    755 	    c->wi_tx_multi_retries);
    756 	printf("Transmit retry limit exceeded:\t\t%d\n",
    757 	    c->wi_tx_retry_limit);
    758 	printf("Transmit discards:\t\t\t%d\n",
    759 	    c->wi_tx_discards);
    760 	printf("Transmit discards due to wrong SA:\t%d\n",
    761 	    c->wi_tx_discards_wrong_sa);
    762 	printf("Received unicast frames:\t\t%d\n",
    763 	    c->wi_rx_unicast_frames);
    764 	printf("Received multicast frames:\t\t%d\n",
    765 	    c->wi_rx_multicast_frames);
    766 	printf("Received fragments:\t\t\t%d\n",
    767 	    c->wi_rx_fragments);
    768 	printf("Received unicast octets:\t\t%d\n",
    769 	    c->wi_rx_unicast_octets);
    770 	printf("Received multicast octets:\t\t%d\n",
    771 	    c->wi_rx_multicast_octets);
    772 	printf("Receive FCS errors:\t\t\t%d\n",
    773 	    c->wi_rx_fcs_errors);
    774 	printf("Receive discards due to no buffer:\t%d\n",
    775 	    c->wi_rx_discards_nobuf);
    776 	printf("Can't decrypt WEP frame:\t\t%d\n",
    777 	    c->wi_rx_WEP_cant_decrypt);
    778 	printf("Received message fragments:\t\t%d\n",
    779 	    c->wi_rx_msg_in_msg_frags);
    780 	printf("Received message bad fragments:\t\t%d\n",
    781 	    c->wi_rx_msg_in_bad_msg_frags);
    782 
    783 	return;
    784 }
    785 
    786 static void
    787 usage()
    788 {
    789 
    790 	fprintf(stderr,
    791 	    "usage: %s interface "
    792 	    "[-oD] [-s station name]\n"
    793 	    "       [-a access point density]\n"
    794 	    "       [-m MAC address] [-d max data length] [-r RTS threshold]\n"
    795 	    "       [-M 0|1] [-R 1|3] [-g fragmentation threshold]\n"
    796 	    ,
    797 	    getprogname());
    798 	exit(1);
    799 }
    800 
    801 int main(argc, argv)
    802 	int			argc;
    803 	char			*argv[];
    804 {
    805 	struct wi_table *wt, **table;
    806 	char *iface;
    807 	int ch, dumpinfo, dumpstats, apscan;
    808 
    809 #define	SET_OPERAND(opr, desc) do {				\
    810 	if ((opr) == NULL)					\
    811 		(opr) = optarg;					\
    812 	else							\
    813 		warnx("%s is already specified to %s",		\
    814 		    desc, (opr));				\
    815 } while (0)
    816 
    817 	dumpinfo = 1;
    818 	dumpstats = 0;
    819 	apscan = 0;
    820 	iface = NULL;
    821 
    822 	if (argc > 1 && argv[1][0] != '-') {
    823 		iface = argv[1];
    824 		optind++;
    825 	}
    826 
    827 	while ((ch = getopt(argc, argv,
    828 	    "a:d:g:hi:m:or:s:M:R:D")) != -1) {
    829 		if (ch != 'i')
    830 			dumpinfo = 0;
    831 		/*
    832 		 * Lookup generic options and remeber operand if found.
    833 		 */
    834 		for (table = wi_tables; *table != NULL; table++)
    835 			if ((wt = wi_optlookup(*table, ch)) != NULL) {
    836 				SET_OPERAND(wt->wi_optval, wt->wi_desc);
    837 				break;
    838 			}
    839 		if (wt == NULL)
    840 			/*
    841 			 * Handle special options.
    842 			 */
    843 			switch (ch) {
    844 			case 'o':
    845 				dumpstats = 1;
    846 				break;
    847 			case 'i':
    848 				SET_OPERAND(iface, "interface");
    849 				break;
    850 			case 'D':
    851 				apscan = 1;
    852 				break;
    853 			case 'h':
    854 			default:
    855 				usage();
    856 				break;
    857 			}
    858 	}
    859 
    860 	if (iface == NULL)
    861 		usage();
    862 
    863 	for (table = wi_tables; *table != NULL; table++)
    864 		for (wt = *table; wt->wi_code != WI_NONE; wt++)
    865 			if (wt->wi_optval != NULL) {
    866 				switch (wt->wi_code) {
    867 				case WI_BOOL:
    868 				case WI_WORDS:
    869 					wi_setword(iface, wt->wi_type,
    870 					    atoi(wt->wi_optval));
    871 					break;
    872 				case WI_STRING:
    873 					wi_setstr(iface, wt->wi_type,
    874 					    wt->wi_optval);
    875 					break;
    876 				case WI_HEXBYTES:
    877 					wi_sethex(iface, wt->wi_type,
    878 					    wt->wi_optval);
    879 					break;
    880 				}
    881 			}
    882 
    883 	if (dumpstats)
    884 		wi_dumpstats(iface);
    885 	if (dumpinfo)
    886 		wi_dumpinfo(iface);
    887 
    888 	if (apscan)
    889 #ifdef WI_RID_SCAN_APS
    890 		wi_apscan(iface);
    891 #else
    892 		errx(1, "AP scan mode is not available.");
    893 #endif
    894 
    895 	exit(0);
    896 }
    897