xref: /netbsd-src/usr.sbin/wiconfig/wiconfig.c (revision 356b70489cfc9525d41e50a39dbebda6fb149664)
1*356b7048Sriastradh /*	$NetBSD: wiconfig.c,v 1.45 2016/06/15 13:47:26 riastradh Exp $	*/
27c002751Ssommerfeld /*
37c002751Ssommerfeld  * Copyright (c) 1997, 1998, 1999
47c002751Ssommerfeld  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
57c002751Ssommerfeld  *
67c002751Ssommerfeld  * Redistribution and use in source and binary forms, with or without
77c002751Ssommerfeld  * modification, are permitted provided that the following conditions
87c002751Ssommerfeld  * are met:
97c002751Ssommerfeld  * 1. Redistributions of source code must retain the above copyright
107c002751Ssommerfeld  *    notice, this list of conditions and the following disclaimer.
117c002751Ssommerfeld  * 2. Redistributions in binary form must reproduce the above copyright
127c002751Ssommerfeld  *    notice, this list of conditions and the following disclaimer in the
137c002751Ssommerfeld  *    documentation and/or other materials provided with the distribution.
147c002751Ssommerfeld  * 3. All advertising materials mentioning features or use of this software
157c002751Ssommerfeld  *    must display the following acknowledgement:
167c002751Ssommerfeld  *	This product includes software developed by Bill Paul.
177c002751Ssommerfeld  * 4. Neither the name of the author nor the names of any co-contributors
187c002751Ssommerfeld  *    may be used to endorse or promote products derived from this software
197c002751Ssommerfeld  *    without specific prior written permission.
207c002751Ssommerfeld  *
217c002751Ssommerfeld  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
227c002751Ssommerfeld  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
237c002751Ssommerfeld  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
247c002751Ssommerfeld  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
257c002751Ssommerfeld  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
267c002751Ssommerfeld  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
277c002751Ssommerfeld  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
287c002751Ssommerfeld  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
297c002751Ssommerfeld  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
307c002751Ssommerfeld  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
317c002751Ssommerfeld  * THE POSSIBILITY OF SUCH DAMAGE.
327c002751Ssommerfeld  *
33f1cac332Senami  *	From: Id: wicontrol.c,v 1.6 1999/05/22 16:12:49 wpaul Exp $
347c002751Ssommerfeld  */
357c002751Ssommerfeld 
367c002751Ssommerfeld #include <sys/types.h>
377c002751Ssommerfeld #include <sys/cdefs.h>
387c002751Ssommerfeld #include <sys/param.h>
397c002751Ssommerfeld #include <sys/socket.h>
407c002751Ssommerfeld #include <sys/ioctl.h>
417c002751Ssommerfeld 
427c002751Ssommerfeld #include <net/if.h>
4382cee51cSexplorer #ifdef __FreeBSD__
4482cee51cSexplorer #include <net/if_var.h>
4582cee51cSexplorer #include <net/ethernet.h>
467c002751Ssommerfeld 
4782cee51cSexplorer #include <machine/if_wavelan_ieee.h>
4882cee51cSexplorer #else
4982cee51cSexplorer #include <netinet/in.h>
5082cee51cSexplorer #include <netinet/if_ether.h>
5182cee51cSexplorer #ifdef __NetBSD__
52690ab870Sdyoung #include <net80211/ieee80211.h>
53690ab870Sdyoung #include <net80211/ieee80211_ioctl.h>
5416aa4291Sichiro #include <dev/ic/wi_ieee.h>
5582cee51cSexplorer #else
5682cee51cSexplorer #include <dev/pcmcia/if_wavelan_ieee.h>
5782cee51cSexplorer #endif
5882cee51cSexplorer #endif
597c002751Ssommerfeld 
607c002751Ssommerfeld #include <stdio.h>
617c002751Ssommerfeld #include <string.h>
6282cee51cSexplorer #include <ctype.h>
637c002751Ssommerfeld #include <stdlib.h>
647c002751Ssommerfeld #include <unistd.h>
657c002751Ssommerfeld #include <errno.h>
667c002751Ssommerfeld #include <err.h>
677c002751Ssommerfeld 
687c002751Ssommerfeld #if !defined(lint)
699c194566Slukem __COPYRIGHT("@(#) Copyright (c) 1997, 1998, 1999\
70a819d229Sthorpej  Bill Paul.  All rights reserved.");
71*356b7048Sriastradh __RCSID("$NetBSD: wiconfig.c,v 1.45 2016/06/15 13:47:26 riastradh Exp $");
727c002751Ssommerfeld #endif
737c002751Ssommerfeld 
7432687868Senami struct wi_table {
7532687868Senami 	int wi_type;
7632687868Senami 	int wi_code;
7732687868Senami #define	WI_NONE			0x00
7832687868Senami #define	WI_STRING		0x01
7932687868Senami #define	WI_BOOL			0x02
8032687868Senami #define	WI_WORDS		0x03
8132687868Senami #define	WI_HEXBYTES		0x04
8232687868Senami #define	WI_KEYSTRUCT		0x05
837ec184ceSdbj #define	WI_BITS			0x06
8469c34ee0Sperry #define	WI_VENDOR		0x07
856363aba7Slukem 	const char *wi_label;		/* label used to print info */
8632687868Senami 	int wi_opt;			/* option character to set this */
876363aba7Slukem 	const char *wi_desc;
8832687868Senami 	char *wi_optval;
8932687868Senami };
9032687868Senami 
911635f76bSichiro /* already define in wireg.h XXX */
921635f76bSichiro #define	WI_APRATE_0		0x00	/* NONE */
931635f76bSichiro #define WI_APRATE_1		0x0A	/* 1 Mbps */
941635f76bSichiro #define WI_APRATE_2		0x14	/* 2 Mbps */
951635f76bSichiro #define WI_APRATE_5		0x37	/* 5.5 Mbps */
961635f76bSichiro #define WI_APRATE_11		0x6E	/* 11 Mbps */
971635f76bSichiro 
98d08bc504Schristos #ifdef WI_RID_SCAN_APS
992d826702Sjoerg static void wi_apscan(char *);
1002d826702Sjoerg static int  get_if_flags(int, const char *);
1012d826702Sjoerg static int  set_if_flags(int, const char *, int);
102d08bc504Schristos #endif
1032d826702Sjoerg static int  wi_getval(char *, struct wi_req *);
1042d826702Sjoerg static void wi_setval(char *, struct wi_req *);
1052d826702Sjoerg static void wi_printstr(struct wi_req *);
1062d826702Sjoerg static void wi_setstr(char *, int, char *);
1072d826702Sjoerg static void wi_setbytes(char *, int, char *, int);
1082d826702Sjoerg static void wi_setword(char *, int, int);
1092d826702Sjoerg static void wi_sethex(char *, int, char *);
1102d826702Sjoerg static void wi_printwords(struct wi_req *);
1112d826702Sjoerg static void wi_printbool(struct wi_req *);
1122d826702Sjoerg static void wi_printhex(struct wi_req *);
1132d826702Sjoerg static void wi_printbits(struct wi_req *);
1142d826702Sjoerg static void wi_checkwifi(char *);
1152d826702Sjoerg static void wi_dumpinfo(char *);
1162d826702Sjoerg static void wi_printkeys(struct wi_req *);
1172d826702Sjoerg static void wi_printvendor(struct wi_req *);
1182d826702Sjoerg static void wi_dumpstats(char *);
1192d826702Sjoerg __dead static void usage(void);
1202d826702Sjoerg static struct wi_table *wi_optlookup(struct wi_table *, int);
1217c002751Ssommerfeld 
122d08bc504Schristos #ifdef WI_RID_SCAN_APS
1232d826702Sjoerg static int
get_if_flags(int s,const char * name)1242d826702Sjoerg get_if_flags(int s, const char *name)
12566a56d4eSichiro {
12666a56d4eSichiro 	struct ifreq	ifreq;
12766a56d4eSichiro 	int		flags;
12866a56d4eSichiro 
12966a56d4eSichiro 	strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
13066a56d4eSichiro 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifreq) == -1)
13166a56d4eSichiro 		  err(1, "SIOCGIFFLAGS");
13266a56d4eSichiro 	flags = ifreq.ifr_flags;
13366a56d4eSichiro 
13466a56d4eSichiro 	return flags;
13566a56d4eSichiro }
13666a56d4eSichiro 
1372d826702Sjoerg static int
set_if_flags(int s,const char * name,int flags)1382d826702Sjoerg set_if_flags(int s, const char *name, int flags)
13966a56d4eSichiro {
14066a56d4eSichiro 	struct ifreq ifreq;
14166a56d4eSichiro 
14266a56d4eSichiro 	ifreq.ifr_flags = flags;
14366a56d4eSichiro 	strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
14466a56d4eSichiro 	if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifreq) == -1)
14566a56d4eSichiro 		  err(1, "SIOCSIFFLAGS");
14666a56d4eSichiro 
14766a56d4eSichiro 	return 0;
14866a56d4eSichiro }
14966a56d4eSichiro 
1502d826702Sjoerg static void
wi_apscan(char * iface)1512d826702Sjoerg wi_apscan(char *iface)
1521635f76bSichiro {
1531635f76bSichiro 	struct wi_req		wreq;
1541635f76bSichiro 	struct ifreq		ifr;
1551635f76bSichiro 	int			s;
1561635f76bSichiro 	int			naps, rate;
1571635f76bSichiro 	int			retries = 10;
15866a56d4eSichiro 	int			flags;
159*356b7048Sriastradh 	struct wi_apinfo	aps[howmany(WI_MAX_DATALEN,
160*356b7048Sriastradh 				    sizeof(struct wi_apinfo))];
1611635f76bSichiro 	int			i, j;
1621635f76bSichiro 
1631635f76bSichiro 	if (iface == NULL)
1641635f76bSichiro 		errx(1, "must specify interface name");
16566a56d4eSichiro 
16666a56d4eSichiro 	s = socket(AF_INET, SOCK_DGRAM, 0);
16766a56d4eSichiro 	if (s == -1)
16866a56d4eSichiro 		err(1, "socket");
16966a56d4eSichiro 	flags = get_if_flags(s, iface);
17066a56d4eSichiro 	if ((flags & IFF_UP) == 0)
17166a56d4eSichiro 		flags = set_if_flags(s, iface, flags | IFF_UP);
17266a56d4eSichiro 
1731635f76bSichiro 	memset((char *)&wreq, 0, sizeof(wreq));
1741635f76bSichiro 
1751635f76bSichiro 	wreq.wi_type = WI_RID_SCAN_APS;
1761635f76bSichiro 	wreq.wi_len = 4;
1771635f76bSichiro 	/* note chan. 1 is the least significant bit */
17818652e37Sdyoung 	wreq.wi_val[0] = htole16(0x3fff);	/* 1 bit per channel, 1-14 */
17918652e37Sdyoung 	wreq.wi_val[1] = htole16(0xf);		/* tx rate */
1801635f76bSichiro 
1811635f76bSichiro 	/* write the request */
1821635f76bSichiro 	wi_setval(iface, &wreq);
1831635f76bSichiro 
1841635f76bSichiro 	/* now poll for a result */
1851635f76bSichiro 	memset((char *)&wreq, 0, sizeof(wreq));
1861635f76bSichiro 
1871635f76bSichiro 	wreq.wi_type = WI_RID_READ_APS;
1881635f76bSichiro 	wreq.wi_len = WI_MAX_DATALEN;
1891635f76bSichiro 
1901635f76bSichiro 	/* we have to do this ourself as opposed to
1913ee53752Sichiro 	 * using getval, because we cannot bail if
1921635f76bSichiro  	 * the ioctl fails
1931635f76bSichiro 	 */
1941635f76bSichiro 	memset((char *)&ifr, 0, sizeof(ifr));
195a37f9a31Sitojun         strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
1961635f76bSichiro         ifr.ifr_data = (caddr_t)&wreq;
1971635f76bSichiro 
1981635f76bSichiro 	printf("scanning ...");
19966a56d4eSichiro 	fflush(stdout);
2001635f76bSichiro 	while (ioctl(s, SIOCGWAVELAN, &ifr) == -1) {
2011635f76bSichiro 		retries--;
2021635f76bSichiro 		if (retries >= 0) {
2033ee53752Sichiro 			printf("."); fflush(stdout);
2041635f76bSichiro 			sleep(1);
2051635f76bSichiro 		} else
2061635f76bSichiro 			break;
2071635f76bSichiro 		errno = 0;
2081635f76bSichiro 	}
2091635f76bSichiro 
2101635f76bSichiro 	if (errno) {
21166a56d4eSichiro 		set_if_flags(s, iface, flags);
21266a56d4eSichiro 		close(s);
2131635f76bSichiro 		err(1, "ioctl");
2141635f76bSichiro 	}
2151635f76bSichiro 
216*356b7048Sriastradh 	memcpy(&naps, wreq.wi_val, sizeof(int));
2176e4b27cdSdbj 
2186e4b27cdSdbj 	if (naps > 0)
2196e4b27cdSdbj 		printf("\nAP Information\n");
2206e4b27cdSdbj 	else
2216e4b27cdSdbj 		printf("\nNo APs available\n");
2226e4b27cdSdbj 
223*356b7048Sriastradh 	naps = MIN((unsigned)naps,
224*356b7048Sriastradh 	    howmany(sizeof(wreq.wi_val) - sizeof(int), sizeof(*aps)));
225*356b7048Sriastradh 	memcpy(aps, (const char *)wreq.wi_val + sizeof(int),
226*356b7048Sriastradh 	    (unsigned)naps * sizeof(*aps));
227*356b7048Sriastradh 
228*356b7048Sriastradh 	for (i = 0; i < naps; i++) {
229*356b7048Sriastradh 		const struct wi_apinfo *const w = &aps[i];
230*356b7048Sriastradh 
2311635f76bSichiro 		printf("ap[%d]:\n", i);
2321635f76bSichiro 		if (w->scanreason) {
2336363aba7Slukem 			static const char *scanm[] = {
2341635f76bSichiro 				"Host initiated",
2351635f76bSichiro 				"Firmware initiated",
2361635f76bSichiro 				"Inquiry request from host"
2371635f76bSichiro 			};
2381635f76bSichiro 			printf("\tScanReason:\t\t\t[ %s ]\n",
2391635f76bSichiro 				scanm[w->scanreason - 1]);
2401635f76bSichiro 		}
2411635f76bSichiro 		printf("\tnetname (SSID):\t\t\t[ ");
2421635f76bSichiro 			for (j = 0; j < w->namelen; j++) {
2431635f76bSichiro 				printf("%c", w->name[j]);
2441635f76bSichiro 			}
2451635f76bSichiro 			printf(" ]\n");
2461635f76bSichiro 		printf("\tBSSID:\t\t\t\t[ %02x:%02x:%02x:%02x:%02x:%02x ]\n",
2471635f76bSichiro 			w->bssid[0]&0xff, w->bssid[1]&0xff,
2481635f76bSichiro 			w->bssid[2]&0xff, w->bssid[3]&0xff,
2491635f76bSichiro 			w->bssid[4]&0xff, w->bssid[5]&0xff);
2501635f76bSichiro 		printf("\tChannel:\t\t\t[ %d ]\n", w->channel);
2511635f76bSichiro 		printf("\tQuality/Signal/Noise [signal]:\t[ %d / %d / %d ]\n"
2521635f76bSichiro 		       "\t                        [dBm]:\t[ %d / %d / %d ]\n",
2531635f76bSichiro 			w->quality, w->signal, w->noise,
2541635f76bSichiro 			w->quality, w->signal - 149, w->noise - 149);
2551d7d73eeSdbj 		printf("\tBSS Beacon Interval [msec]:\t[ %d ]\n", w->interval);
2561635f76bSichiro 		printf("\tCapinfo:\t\t\t[ ");
2571635f76bSichiro 			if (w->capinfo & IEEE80211_CAPINFO_ESS)
2581635f76bSichiro 				printf("ESS ");
2591635f76bSichiro 			if (w->capinfo & IEEE80211_CAPINFO_PRIVACY)
2601635f76bSichiro 				printf("WEP ");
2611635f76bSichiro 			printf("]\n");
2621635f76bSichiro 
2631635f76bSichiro 		switch (w->rate) {
2641635f76bSichiro 		case WI_APRATE_1:
2651635f76bSichiro 			rate = 1;
2661635f76bSichiro 			break;
2671635f76bSichiro 		case WI_APRATE_2:
2681635f76bSichiro 			rate = 2;
2691635f76bSichiro 			break;
2701635f76bSichiro 		case WI_APRATE_5:
27153baf6b8Sjoerg 			rate = 5;
2721635f76bSichiro 			break;
2731635f76bSichiro 		case WI_APRATE_11:
2741635f76bSichiro 			rate = 11;
2751635f76bSichiro 			break;
2761635f76bSichiro 		case WI_APRATE_0:
2771635f76bSichiro 		default:
2781635f76bSichiro 			rate = 0;
2791635f76bSichiro 			break;
2801635f76bSichiro 		}
2811635f76bSichiro 		if (rate) printf("\tDataRate [Mbps]:\t\t[ %d ]\n", rate);
2821635f76bSichiro 	}
28366a56d4eSichiro 
28466a56d4eSichiro 	set_if_flags(s, iface, flags);
28566a56d4eSichiro 	close(s);
2861635f76bSichiro }
287d08bc504Schristos #endif
2881635f76bSichiro 
2892d826702Sjoerg static int
wi_getval(char * iface,struct wi_req * wreq)2902d826702Sjoerg wi_getval(char *iface, struct wi_req *wreq)
2917c002751Ssommerfeld {
2927c002751Ssommerfeld 	struct ifreq		ifr;
29313ad3405Selad 	int			s, error;
2947c002751Ssommerfeld 
29513ad3405Selad 	error = 0;
2967c002751Ssommerfeld 	bzero((char *)&ifr, sizeof(ifr));
2977c002751Ssommerfeld 
298a37f9a31Sitojun 	strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
2997c002751Ssommerfeld 	ifr.ifr_data = (caddr_t)wreq;
3007c002751Ssommerfeld 
3017c002751Ssommerfeld 	s = socket(AF_INET, SOCK_DGRAM, 0);
3027c002751Ssommerfeld 
3037c002751Ssommerfeld 	if (s == -1)
3047c002751Ssommerfeld 		err(1, "socket");
3057c002751Ssommerfeld 
30613ad3405Selad 	if (ioctl(s, SIOCGWAVELAN, &ifr) == -1) {
30713ad3405Selad 		warn("SIOCGWAVELAN(wreq %04x)", wreq->wi_type);
30813ad3405Selad 		error = 1;
30913ad3405Selad 	}
3107c002751Ssommerfeld 
3117c002751Ssommerfeld 	close(s);
3127c002751Ssommerfeld 
31313ad3405Selad 	return error;
3147c002751Ssommerfeld }
3157c002751Ssommerfeld 
3162d826702Sjoerg static void
wi_setval(char * iface,struct wi_req * wreq)3172d826702Sjoerg wi_setval(char *iface, struct wi_req *wreq)
3187c002751Ssommerfeld {
3197c002751Ssommerfeld 	struct ifreq		ifr;
3207c002751Ssommerfeld 	int			s;
3217c002751Ssommerfeld 
3227c002751Ssommerfeld 	bzero((char *)&ifr, sizeof(ifr));
3237c002751Ssommerfeld 
324a37f9a31Sitojun 	strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
3257c002751Ssommerfeld 	ifr.ifr_data = (caddr_t)wreq;
3267c002751Ssommerfeld 
3277c002751Ssommerfeld 	s = socket(AF_INET, SOCK_DGRAM, 0);
3287c002751Ssommerfeld 
3297c002751Ssommerfeld 	if (s == -1)
3307c002751Ssommerfeld 		err(1, "socket");
3317c002751Ssommerfeld 
3327c002751Ssommerfeld 	if (ioctl(s, SIOCSWAVELAN, &ifr) == -1)
3337c002751Ssommerfeld 		err(1, "SIOCSWAVELAN");
3347c002751Ssommerfeld 
3357c002751Ssommerfeld 	close(s);
3367c002751Ssommerfeld 
3377c002751Ssommerfeld 	return;
3387c002751Ssommerfeld }
3397c002751Ssommerfeld 
3402d826702Sjoerg static void
wi_printstr(struct wi_req * wreq)3412d826702Sjoerg wi_printstr(struct wi_req *wreq)
3427c002751Ssommerfeld {
3437c002751Ssommerfeld 	char			*ptr;
3447c002751Ssommerfeld 	int			i;
3457c002751Ssommerfeld 
3467c002751Ssommerfeld 	if (wreq->wi_type == WI_RID_SERIALNO) {
3477c002751Ssommerfeld 		ptr = (char *)&wreq->wi_val;
3487c002751Ssommerfeld 		for (i = 0; i < (wreq->wi_len - 1) * 2; i++) {
3497c002751Ssommerfeld 			if (ptr[i] == '\0')
3507c002751Ssommerfeld 				ptr[i] = ' ';
3517c002751Ssommerfeld 		}
3527c002751Ssommerfeld 	} else {
353eef5fa50Stsubai 		int len = le16toh(wreq->wi_val[0]);
354eef5fa50Stsubai 
3557c002751Ssommerfeld 		ptr = (char *)&wreq->wi_val[1];
356eef5fa50Stsubai 		for (i = 0; i < len; i++) {
3577c002751Ssommerfeld 			if (ptr[i] == '\0')
3587c002751Ssommerfeld 				ptr[i] = ' ';
3597c002751Ssommerfeld 		}
3607c002751Ssommerfeld 	}
3617c002751Ssommerfeld 
3627c002751Ssommerfeld 	ptr[i] = '\0';
3637c002751Ssommerfeld 	printf("[ %s ]", ptr);
3647c002751Ssommerfeld 
3657c002751Ssommerfeld 	return;
3667c002751Ssommerfeld }
3677c002751Ssommerfeld 
3682d826702Sjoerg static void
wi_setstr(char * iface,int code,char * str)3692d826702Sjoerg wi_setstr(char *iface, int code, char *str)
3707c002751Ssommerfeld {
3717c002751Ssommerfeld 	struct wi_req		wreq;
3727c002751Ssommerfeld 
3737c002751Ssommerfeld 	bzero((char *)&wreq, sizeof(wreq));
3747c002751Ssommerfeld 
3757c002751Ssommerfeld 	if (strlen(str) > 30)
3767c002751Ssommerfeld 		errx(1, "string too long");
3777c002751Ssommerfeld 
3787c002751Ssommerfeld 	wreq.wi_type = code;
3797c002751Ssommerfeld 	wreq.wi_len = 18;
380eef5fa50Stsubai 	wreq.wi_val[0] = htole16(strlen(str));
3817c002751Ssommerfeld 	bcopy(str, (char *)&wreq.wi_val[1], strlen(str));
3827c002751Ssommerfeld 
3837c002751Ssommerfeld 	wi_setval(iface, &wreq);
3847c002751Ssommerfeld 
3857c002751Ssommerfeld 	return;
3867c002751Ssommerfeld }
3877c002751Ssommerfeld 
3882d826702Sjoerg static void
wi_setbytes(char * iface,int code,char * bytes,int len)3892d826702Sjoerg wi_setbytes(char *iface, int code, char *bytes, int len)
3907c002751Ssommerfeld {
3917c002751Ssommerfeld 	struct wi_req		wreq;
3927c002751Ssommerfeld 
3937c002751Ssommerfeld 	bzero((char *)&wreq, sizeof(wreq));
3947c002751Ssommerfeld 
3957c002751Ssommerfeld 	wreq.wi_type = code;
3967c002751Ssommerfeld 	wreq.wi_len = (len / 2) + 1;
3977c002751Ssommerfeld 	bcopy(bytes, (char *)&wreq.wi_val[0], len);
3987c002751Ssommerfeld 
3997c002751Ssommerfeld 	wi_setval(iface, &wreq);
4007c002751Ssommerfeld 
4017c002751Ssommerfeld 	return;
4027c002751Ssommerfeld }
4037c002751Ssommerfeld 
4042d826702Sjoerg static void
wi_setword(char * iface,int code,int word)4052d826702Sjoerg wi_setword(char *iface, int code, int word)
4067c002751Ssommerfeld {
4077c002751Ssommerfeld 	struct wi_req		wreq;
4087c002751Ssommerfeld 
4097c002751Ssommerfeld 	bzero((char *)&wreq, sizeof(wreq));
4107c002751Ssommerfeld 
4117c002751Ssommerfeld 	wreq.wi_type = code;
4127c002751Ssommerfeld 	wreq.wi_len = 2;
413eef5fa50Stsubai 	wreq.wi_val[0] = htole16(word);
4147c002751Ssommerfeld 
4157c002751Ssommerfeld 	wi_setval(iface, &wreq);
4167c002751Ssommerfeld 
4177c002751Ssommerfeld 	return;
4187c002751Ssommerfeld }
4197c002751Ssommerfeld 
4202d826702Sjoerg static void
wi_sethex(char * iface,int code,char * str)4212d826702Sjoerg wi_sethex(char *iface, int code, char *str)
4227c002751Ssommerfeld {
4237c002751Ssommerfeld 	struct ether_addr	*addr;
4247c002751Ssommerfeld 
4257c002751Ssommerfeld 	addr = ether_aton(str);
4267c002751Ssommerfeld 	if (addr == NULL)
4277c002751Ssommerfeld 		errx(1, "badly formatted address");
4287c002751Ssommerfeld 
4297c002751Ssommerfeld 	wi_setbytes(iface, code, (char *)addr, ETHER_ADDR_LEN);
4307c002751Ssommerfeld 
4317c002751Ssommerfeld 	return;
4327c002751Ssommerfeld }
4337c002751Ssommerfeld 
4342d826702Sjoerg static void
wi_printkeys(struct wi_req * wreq)4352d826702Sjoerg wi_printkeys(struct wi_req *wreq)
43682cee51cSexplorer {
43782cee51cSexplorer         int                     i, j, bn;
43882cee51cSexplorer         struct wi_key           *k;
43982cee51cSexplorer         struct wi_ltv_keys      *keys;
44082cee51cSexplorer         char                    *ptr;
44182cee51cSexplorer 
44282cee51cSexplorer 	keys = (struct wi_ltv_keys *)wreq;
44382cee51cSexplorer 
44482cee51cSexplorer 	for (i = 0, bn = 0; i < 4; i++, bn = 0) {
44582cee51cSexplorer                 k = &keys->wi_keys[i];
44682cee51cSexplorer                 ptr = (char *)k->wi_keydat;
447eef5fa50Stsubai                 for (j = 0; j < le16toh(k->wi_keylen); j++) {
448c40ad144Sjdolecek 		        if (!isprint((unsigned char) ptr[j])) {
44982cee51cSexplorer 			        bn = 1;
45082cee51cSexplorer 				break;
45182cee51cSexplorer 			}
45282cee51cSexplorer 		}
45382cee51cSexplorer 
45482cee51cSexplorer 		if (bn)	{
45582cee51cSexplorer 		        printf("[ 0x");
456eef5fa50Stsubai 		        for (j = 0; j < le16toh(k->wi_keylen); j++)
45782cee51cSexplorer 			      printf("%02x", ((unsigned char *) ptr)[j]);
45882cee51cSexplorer 			printf(" ]");
45982cee51cSexplorer 		} else {
46082cee51cSexplorer 		        ptr[j] = '\0';
46182cee51cSexplorer 			printf("[ %s ]", ptr);
46282cee51cSexplorer 		}
46382cee51cSexplorer         }
46482cee51cSexplorer 
46582cee51cSexplorer         return;
46682cee51cSexplorer };
46782cee51cSexplorer 
4682d826702Sjoerg static void
wi_printvendor(struct wi_req * wreq)4692d826702Sjoerg wi_printvendor(struct wi_req *wreq)
47069c34ee0Sperry {
47169c34ee0Sperry 	/* id
47269c34ee0Sperry 	 * vendor
47369c34ee0Sperry 	 * firmware major
47469c34ee0Sperry 	 *          minor
47569c34ee0Sperry 	 */
47669c34ee0Sperry #define WI_RID_STA_IDENTITY_LUCENT	0x1
47769c34ee0Sperry #define WI_RID_STA_IDENTITY_PRISMII	0x2
47869c34ee0Sperry #define WI_RID_STA_IDENTITY_SAMSUNG	0x3
47969c34ee0Sperry #define WI_RID_STA_IDENTITY_DLINK	0x6
48069c34ee0Sperry 
48169c34ee0Sperry 	const char *vendor = "Unknown";
48269c34ee0Sperry 
48369c34ee0Sperry 	if (wreq->wi_len < 4)
48469c34ee0Sperry 		return;
48569c34ee0Sperry 
48618652e37Sdyoung 	switch (le16toh(wreq->wi_val[1])) {
48769c34ee0Sperry 	case WI_RID_STA_IDENTITY_LUCENT:
48869c34ee0Sperry 		vendor = "Lucent";
48969c34ee0Sperry 		break;
49069c34ee0Sperry 	case WI_RID_STA_IDENTITY_PRISMII:
49169c34ee0Sperry 		vendor = "generic PRISM II";
49269c34ee0Sperry 		break;
49369c34ee0Sperry 	case WI_RID_STA_IDENTITY_SAMSUNG:
49469c34ee0Sperry 		vendor = "Samsung";
49569c34ee0Sperry 		break;
49669c34ee0Sperry 	case WI_RID_STA_IDENTITY_DLINK:
49769c34ee0Sperry 		vendor = "D-Link";
49869c34ee0Sperry 		break;
49969c34ee0Sperry 	}
50018652e37Sdyoung 	printf("[ %s ID: %d version: %d.%d ]", vendor, le16toh(wreq->wi_val[0]),
50118652e37Sdyoung 	    le16toh(wreq->wi_val[2]), le16toh(wreq->wi_val[3]));
50269c34ee0Sperry 	return;
50369c34ee0Sperry }
50469c34ee0Sperry 
5052d826702Sjoerg static void
wi_printwords(struct wi_req * wreq)5062d826702Sjoerg wi_printwords(struct wi_req *wreq)
5077c002751Ssommerfeld {
5087c002751Ssommerfeld 	int			i;
5097c002751Ssommerfeld 
5107c002751Ssommerfeld 	printf("[ ");
5117c002751Ssommerfeld 	for (i = 0; i < wreq->wi_len - 1; i++)
512eef5fa50Stsubai 		printf("%d ", le16toh(wreq->wi_val[i]));
5137c002751Ssommerfeld 	printf("]");
5147c002751Ssommerfeld 
5157c002751Ssommerfeld 	return;
5167c002751Ssommerfeld }
5177c002751Ssommerfeld 
5182d826702Sjoerg static void
wi_printbool(struct wi_req * wreq)5192d826702Sjoerg wi_printbool(struct wi_req *wreq)
5207c002751Ssommerfeld {
521eef5fa50Stsubai 	if (le16toh(wreq->wi_val[0]))
5227c002751Ssommerfeld 		printf("[ On ]");
5237c002751Ssommerfeld 	else
5247c002751Ssommerfeld 		printf("[ Off ]");
5257c002751Ssommerfeld 
5267c002751Ssommerfeld 	return;
5277c002751Ssommerfeld }
5287c002751Ssommerfeld 
5292d826702Sjoerg static void
wi_printhex(struct wi_req * wreq)5302d826702Sjoerg wi_printhex(struct wi_req *wreq)
5317c002751Ssommerfeld {
5327c002751Ssommerfeld 	int			i;
5337c002751Ssommerfeld 	unsigned char		*c;
5347c002751Ssommerfeld 
5357c002751Ssommerfeld 	c = (unsigned char *)&wreq->wi_val;
5367c002751Ssommerfeld 
5377c002751Ssommerfeld 	printf("[ ");
5387c002751Ssommerfeld 	for (i = 0; i < (wreq->wi_len - 1) * 2; i++) {
5397c002751Ssommerfeld 		printf("%02x", c[i]);
5407c002751Ssommerfeld 		if (i < ((wreq->wi_len - 1) * 2) - 1)
5417c002751Ssommerfeld 			printf(":");
5427c002751Ssommerfeld 	}
5437c002751Ssommerfeld 
5447c002751Ssommerfeld 	printf(" ]");
5457c002751Ssommerfeld 	return;
5467c002751Ssommerfeld }
5477c002751Ssommerfeld 
5482d826702Sjoerg static void
wi_printbits(struct wi_req * wreq)5492d826702Sjoerg wi_printbits(struct wi_req *wreq)
5507ec184ceSdbj {
5517ec184ceSdbj 	int			i;
5527ec184ceSdbj 	int bits = le16toh(wreq->wi_val[0]);
5537ec184ceSdbj 
5547ec184ceSdbj 	printf("[");
5557ec184ceSdbj 	for (i = 0; i < 16; i++) {
5567ec184ceSdbj 		if (bits & 0x1) {
5577ec184ceSdbj 			printf(" %d", i+1);
5587ec184ceSdbj 		}
5597ec184ceSdbj 		bits >>= 1;
5607ec184ceSdbj 	}
5617ec184ceSdbj 	printf(" ]");
5627ec184ceSdbj 	return;
5637ec184ceSdbj }
5647ec184ceSdbj 
5657c002751Ssommerfeld static struct wi_table wi_table[] = {
5666363aba7Slukem 	{ WI_RID_SERIALNO, WI_STRING, "NIC serial number:\t\t\t", 0, 0, 0 },
56732687868Senami 	{ WI_RID_NODENAME, WI_STRING, "Station name:\t\t\t\t",
5686363aba7Slukem 	    's', "station name", 0, },
5696363aba7Slukem 	{ WI_RID_OWN_SSID, WI_STRING, "SSID for IBSS creation:\t\t\t", 0, 0, 0 },
5706363aba7Slukem 	{ WI_RID_CURRENT_SSID, WI_STRING, "Current netname (SSID):\t\t\t", 0, 0, 0 },
5716363aba7Slukem 	{ WI_RID_DESIRED_SSID, WI_STRING, "Desired netname (SSID):\t\t\t", 0, 0, 0 },
5726363aba7Slukem 	{ WI_RID_CURRENT_BSSID, WI_HEXBYTES, "Current BSSID:\t\t\t\t", 0, 0, 0 },
5736363aba7Slukem 	{ WI_RID_CHANNEL_LIST, WI_BITS, "Channel list:\t\t\t\t", 0, 0, 0 },
5746363aba7Slukem 	{ WI_RID_OWN_CHNL, WI_WORDS, "IBSS channel:\t\t\t\t", 0, 0, 0 },
5756363aba7Slukem 	{ WI_RID_CURRENT_CHAN, WI_WORDS, "Current channel:\t\t\t", 0, 0, 0 },
5766363aba7Slukem 	{ WI_RID_COMMS_QUALITY, WI_WORDS, "Comms quality/signal/noise:\t\t", 0, 0, 0 },
5776363aba7Slukem 	{ WI_RID_PROMISC, WI_BOOL, "Promiscuous mode:\t\t\t", 0, 0, 0 },
5786363aba7Slukem 	{ WI_RID_PORTTYPE, WI_WORDS, "Port type:\t\t\t\t", 0, 0, 0 },
57932687868Senami 	{ WI_RID_MAC_NODE, WI_HEXBYTES, "MAC address:\t\t\t\t",
5806363aba7Slukem 	    'm', "MAC address", 0, },
5816363aba7Slukem 	{ WI_RID_TX_RATE, WI_WORDS, "TX rate (selection):\t\t\t", 0, 0, 0 },
5826363aba7Slukem 	{ WI_RID_CUR_TX_RATE, WI_WORDS, "TX rate (actual speed):\t\t\t", 0, 0, 0 },
5836363aba7Slukem 	{ WI_RID_CUR_BEACON_INT, WI_WORDS, "Beacon Interval (current) [msec]:\t", 0, 0, 0 },
58432687868Senami 	{ WI_RID_MAX_DATALEN, WI_WORDS, "Maximum data length:\t\t\t",
5856363aba7Slukem 	    'd', "maximum data length", 0 },
58632687868Senami 	{ WI_RID_RTS_THRESH, WI_WORDS, "RTS/CTS handshake threshold:\t\t",
5876363aba7Slukem 	    'r', "RTS threshold", 0 },
5883e30600bSdyoung 	{ WI_RID_FRAG_THRESH, WI_WORDS, "fragmentation threshold:\t\t",
5896363aba7Slukem 	    'g', "fragmentation threshold", 0, },
5906363aba7Slukem 	{ WI_RID_DBM_ADJUST, WI_WORDS, "RSSI -> dBm adjustment:\t\t\t", 0, 0, 0 },
5916363aba7Slukem 	{ WI_RID_CREATE_IBSS, WI_BOOL, "Create IBSS:\t\t\t\t", 0, 0, 0 },
592a420303aSichiro 	{ WI_RID_MICROWAVE_OVEN, WI_WORDS, "Microwave oven robustness:\t\t",
5936363aba7Slukem 	    'M', "microwave oven robustness enabled", 0 },
594a420303aSichiro 	{ WI_RID_ROAMING_MODE, WI_WORDS, "Roaming mode(1:firm,3:disable):\t\t",
5956363aba7Slukem 	    'R', "roaming mode", 0 },
59632687868Senami 	{ WI_RID_SYSTEM_SCALE, WI_WORDS, "Access point density:\t\t\t",
5976363aba7Slukem 	    'a', "system scale", 0 },
5986363aba7Slukem 	{ WI_RID_PM_ENABLED, WI_WORDS, "Power Mgmt (1=on, 0=off):\t\t", 0, 0, 0 },
5996363aba7Slukem 	{ WI_RID_MAX_SLEEP, WI_WORDS, "Max sleep time (msec):\t\t\t", 0, 0, 0 },
6006363aba7Slukem  	{ WI_RID_STA_IDENTITY, WI_VENDOR, "Vendor info:\t\t\t\t", 0, 0, 0 },
6016363aba7Slukem 	{ 0, WI_NONE, 0, 0, 0, 0 }
6027c002751Ssommerfeld };
6037c002751Ssommerfeld 
60482cee51cSexplorer static struct wi_table wi_crypt_table[] = {
6056363aba7Slukem 	{ WI_RID_ENCRYPTION, WI_BOOL, "WEP encryption:\t\t\t\t", 0, 0, 0 },
60600675c7eSwrstuden 	{ WI_RID_CNFAUTHMODE, WI_WORDS, "Authentication type \n(1=OpenSys, 2=Shared Key):\t\t",
6076363aba7Slukem 	    'A', "authentication type", 0 },
6086363aba7Slukem         { WI_RID_TX_CRYPT_KEY, WI_WORDS, "TX encryption key:\t\t\t", 0, 0, 0 },
6096363aba7Slukem         { WI_RID_DEFLT_CRYPT_KEYS, WI_KEYSTRUCT, "Encryption keys:\t\t\t", 0, 0, 0 },
6106363aba7Slukem 	{ 0, WI_NONE, 0, 0, 0, 0 }
61182cee51cSexplorer };
61282cee51cSexplorer 
61332687868Senami static struct wi_table *wi_tables[] = {
61432687868Senami 	wi_table,
61532687868Senami 	wi_crypt_table,
61632687868Senami 	NULL
61732687868Senami };
61832687868Senami 
61932687868Senami static struct wi_table *
wi_optlookup(struct wi_table * table,int opt)6202d826702Sjoerg wi_optlookup(struct wi_table *table, int opt)
62132687868Senami {
62232687868Senami 	struct wi_table *wt;
62332687868Senami 
62432687868Senami 	for (wt = table; wt->wi_type != 0; wt++)
62532687868Senami 		if (wt->wi_opt == opt)
62632687868Senami 			return (wt);
62732687868Senami 	return (NULL);
62832687868Senami }
62932687868Senami 
6302d826702Sjoerg static void
wi_checkwifi(char * iface)6312d826702Sjoerg wi_checkwifi(char *iface)
6329dd4b8a2Ssborrill {
6339dd4b8a2Ssborrill 	struct ifreq		ifr;
6349dd4b8a2Ssborrill 	struct ieee80211_nwid	nwid;
6359dd4b8a2Ssborrill 	int			s;
6369dd4b8a2Ssborrill 
6379dd4b8a2Ssborrill 	bzero((char *)&ifr, sizeof(ifr));
6389dd4b8a2Ssborrill 
6399dd4b8a2Ssborrill 	strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
6409dd4b8a2Ssborrill 	ifr.ifr_data = (void *)&nwid;
6419dd4b8a2Ssborrill 
6429dd4b8a2Ssborrill 	s = socket(AF_INET, SOCK_DGRAM, 0);
6439dd4b8a2Ssborrill 
6449dd4b8a2Ssborrill 	if (s == -1)
6459dd4b8a2Ssborrill 		err(1, "socket");
6469dd4b8a2Ssborrill 
6479dd4b8a2Ssborrill 	/* Choice of ioctl inspired by ifconfig/ieee80211.c */
6489dd4b8a2Ssborrill 	if (ioctl(s, SIOCG80211NWID, &ifr) == -1)
6499dd4b8a2Ssborrill 		err(1, "SIOCG80211NWID");
6509dd4b8a2Ssborrill 
6519dd4b8a2Ssborrill 	close(s);
6529dd4b8a2Ssborrill }
6539dd4b8a2Ssborrill 
6542d826702Sjoerg static void
wi_dumpinfo(char * iface)6552d826702Sjoerg wi_dumpinfo(char *iface)
6567c002751Ssommerfeld {
6577c002751Ssommerfeld 	struct wi_req		wreq;
65882cee51cSexplorer 	int			i, has_wep;
6597c002751Ssommerfeld 	struct wi_table		*w;
6607c002751Ssommerfeld 
66182cee51cSexplorer 	bzero((char *)&wreq, sizeof(wreq));
66282cee51cSexplorer 
66382cee51cSexplorer 	wreq.wi_len = WI_MAX_DATALEN;
66482cee51cSexplorer 	wreq.wi_type = WI_RID_WEP_AVAIL;
66582cee51cSexplorer 
66682cee51cSexplorer 	wi_getval(iface, &wreq);
667eef5fa50Stsubai 	has_wep = le16toh(wreq.wi_val[0]);
66882cee51cSexplorer 
6697c002751Ssommerfeld 	w = wi_table;
6707c002751Ssommerfeld 
67132687868Senami 	for (i = 0; w[i].wi_code != WI_NONE; i++) {
6727c002751Ssommerfeld 		bzero((char *)&wreq, sizeof(wreq));
6737c002751Ssommerfeld 
6747c002751Ssommerfeld 		wreq.wi_len = WI_MAX_DATALEN;
67532687868Senami 		wreq.wi_type = w[i].wi_type;
6767c002751Ssommerfeld 
67732687868Senami 		printf("%s", w[i].wi_label);
67813ad3405Selad 		if (wi_getval(iface, &wreq)) {
67913ad3405Selad 			printf("[ Unknown ]\n");
68013ad3405Selad 			continue;
68113ad3405Selad 		}
68232687868Senami 		switch (w[i].wi_code) {
6837c002751Ssommerfeld 		case WI_STRING:
6847c002751Ssommerfeld 			wi_printstr(&wreq);
6857c002751Ssommerfeld 			break;
6867c002751Ssommerfeld 		case WI_WORDS:
6877c002751Ssommerfeld 			wi_printwords(&wreq);
6887c002751Ssommerfeld 			break;
6897c002751Ssommerfeld 		case WI_BOOL:
6907c002751Ssommerfeld 			wi_printbool(&wreq);
6917c002751Ssommerfeld 			break;
6927c002751Ssommerfeld 		case WI_HEXBYTES:
6937c002751Ssommerfeld 			wi_printhex(&wreq);
6947c002751Ssommerfeld 			break;
6957ec184ceSdbj 		case WI_BITS:
6967ec184ceSdbj 			wi_printbits(&wreq);
6977ec184ceSdbj 			break;
69869c34ee0Sperry 		case WI_VENDOR:
69969c34ee0Sperry 			wi_printvendor(&wreq);
70069c34ee0Sperry 			break;
7017c002751Ssommerfeld 		default:
7027c002751Ssommerfeld 			break;
7037c002751Ssommerfeld 		}
7047c002751Ssommerfeld 		printf("\n");
7057c002751Ssommerfeld 	}
7067c002751Ssommerfeld 
70782cee51cSexplorer 	if (has_wep) {
70882cee51cSexplorer 		w = wi_crypt_table;
70932687868Senami 		for (i = 0; w[i].wi_code != WI_NONE; i++) {
71082cee51cSexplorer 			bzero((char *)&wreq, sizeof(wreq));
71182cee51cSexplorer 
71282cee51cSexplorer 			wreq.wi_len = WI_MAX_DATALEN;
71332687868Senami 			wreq.wi_type = w[i].wi_type;
71482cee51cSexplorer 
71582cee51cSexplorer 			wi_getval(iface, &wreq);
71632687868Senami 			printf("%s", w[i].wi_label);
71732687868Senami 			switch (w[i].wi_code) {
71882cee51cSexplorer 			case WI_STRING:
71982cee51cSexplorer 				wi_printstr(&wreq);
72082cee51cSexplorer 				break;
72182cee51cSexplorer 			case WI_WORDS:
72282cee51cSexplorer 				if (wreq.wi_type == WI_RID_TX_CRYPT_KEY)
723eef5fa50Stsubai 					wreq.wi_val[0] =
724eef5fa50Stsubai 					  htole16(le16toh(wreq.wi_val[0]) + 1);
72582cee51cSexplorer 				wi_printwords(&wreq);
72682cee51cSexplorer 				break;
72782cee51cSexplorer 			case WI_BOOL:
72882cee51cSexplorer 				wi_printbool(&wreq);
72982cee51cSexplorer 				break;
73082cee51cSexplorer 			case WI_HEXBYTES:
73182cee51cSexplorer 				wi_printhex(&wreq);
73282cee51cSexplorer 				break;
73382cee51cSexplorer 			case WI_KEYSTRUCT:
73482cee51cSexplorer 				wi_printkeys(&wreq);
73582cee51cSexplorer 				break;
73682cee51cSexplorer 			default:
73782cee51cSexplorer 				break;
73882cee51cSexplorer 			}
73982cee51cSexplorer 			printf("\n");
74082cee51cSexplorer 		}
74182cee51cSexplorer 	}
74282cee51cSexplorer 
7437c002751Ssommerfeld 	return;
7447c002751Ssommerfeld }
7457c002751Ssommerfeld 
7462d826702Sjoerg static void
wi_dumpstats(char * iface)7472d826702Sjoerg wi_dumpstats(char *iface)
7487c002751Ssommerfeld {
7497c002751Ssommerfeld 	struct wi_req		wreq;
7507c002751Ssommerfeld 	struct wi_counters	*c;
7517c002751Ssommerfeld 
7527c002751Ssommerfeld 	bzero((char *)&wreq, sizeof(wreq));
7537c002751Ssommerfeld 	wreq.wi_len = WI_MAX_DATALEN;
7547c002751Ssommerfeld 	wreq.wi_type = WI_RID_IFACE_STATS;
7557c002751Ssommerfeld 
7567c002751Ssommerfeld 	wi_getval(iface, &wreq);
7577c002751Ssommerfeld 
7587c002751Ssommerfeld 	c = (struct wi_counters *)&wreq.wi_val;
7597c002751Ssommerfeld 
760eef5fa50Stsubai 	/* XXX native byte order */
7617c002751Ssommerfeld 	printf("Transmitted unicast frames:\t\t%d\n",
7627c002751Ssommerfeld 	    c->wi_tx_unicast_frames);
7637c002751Ssommerfeld 	printf("Transmitted multicast frames:\t\t%d\n",
7647c002751Ssommerfeld 	    c->wi_tx_multicast_frames);
7657c002751Ssommerfeld 	printf("Transmitted fragments:\t\t\t%d\n",
7667c002751Ssommerfeld 	    c->wi_tx_fragments);
7677c002751Ssommerfeld 	printf("Transmitted unicast octets:\t\t%d\n",
7687c002751Ssommerfeld 	    c->wi_tx_unicast_octets);
7697c002751Ssommerfeld 	printf("Transmitted multicast octets:\t\t%d\n",
7707c002751Ssommerfeld 	    c->wi_tx_multicast_octets);
7717c002751Ssommerfeld 	printf("Single transmit retries:\t\t%d\n",
7727c002751Ssommerfeld 	    c->wi_tx_single_retries);
7737c002751Ssommerfeld 	printf("Multiple transmit retries:\t\t%d\n",
7747c002751Ssommerfeld 	    c->wi_tx_multi_retries);
7757c002751Ssommerfeld 	printf("Transmit retry limit exceeded:\t\t%d\n",
7767c002751Ssommerfeld 	    c->wi_tx_retry_limit);
7777c002751Ssommerfeld 	printf("Transmit discards:\t\t\t%d\n",
7787c002751Ssommerfeld 	    c->wi_tx_discards);
7797c002751Ssommerfeld 	printf("Transmit discards due to wrong SA:\t%d\n",
7807c002751Ssommerfeld 	    c->wi_tx_discards_wrong_sa);
7817c002751Ssommerfeld 	printf("Received unicast frames:\t\t%d\n",
7827c002751Ssommerfeld 	    c->wi_rx_unicast_frames);
7837c002751Ssommerfeld 	printf("Received multicast frames:\t\t%d\n",
7847c002751Ssommerfeld 	    c->wi_rx_multicast_frames);
7857c002751Ssommerfeld 	printf("Received fragments:\t\t\t%d\n",
7867c002751Ssommerfeld 	    c->wi_rx_fragments);
7877c002751Ssommerfeld 	printf("Received unicast octets:\t\t%d\n",
7887c002751Ssommerfeld 	    c->wi_rx_unicast_octets);
7897c002751Ssommerfeld 	printf("Received multicast octets:\t\t%d\n",
7907c002751Ssommerfeld 	    c->wi_rx_multicast_octets);
7917c002751Ssommerfeld 	printf("Receive FCS errors:\t\t\t%d\n",
7927c002751Ssommerfeld 	    c->wi_rx_fcs_errors);
7937c002751Ssommerfeld 	printf("Receive discards due to no buffer:\t%d\n",
7947c002751Ssommerfeld 	    c->wi_rx_discards_nobuf);
7957c002751Ssommerfeld 	printf("Can't decrypt WEP frame:\t\t%d\n",
7967c002751Ssommerfeld 	    c->wi_rx_WEP_cant_decrypt);
7977c002751Ssommerfeld 	printf("Received message fragments:\t\t%d\n",
7987c002751Ssommerfeld 	    c->wi_rx_msg_in_msg_frags);
7997c002751Ssommerfeld 	printf("Received message bad fragments:\t\t%d\n",
8007c002751Ssommerfeld 	    c->wi_rx_msg_in_bad_msg_frags);
8017c002751Ssommerfeld 
8027c002751Ssommerfeld 	return;
8037c002751Ssommerfeld }
8047c002751Ssommerfeld 
80532687868Senami static void
usage(void)8062d826702Sjoerg usage(void)
8077c002751Ssommerfeld {
80832687868Senami 
80982cee51cSexplorer 	fprintf(stderr,
810c6486717Swiz 	    "usage: %s interface [-Dho] [-A 1|2] [-a access point density]\n"
811c6486717Swiz 	    "                [-d max data length] [-g fragmentation threshold] [-M 0|1]\n"
812c6486717Swiz 	    "                [-m MAC address] [-R 1|3] [-r RTS threshold] [-s station name]\n"
8135056d0ceSjhawk 	    ,
81425bdbb66Scgd 	    getprogname());
8157c002751Ssommerfeld 	exit(1);
8167c002751Ssommerfeld }
8177c002751Ssommerfeld 
8182d826702Sjoerg int
main(int argc,char * argv[])8192d826702Sjoerg main(int argc, char *argv[])
8207c002751Ssommerfeld {
82132687868Senami 	struct wi_table *wt, **table;
8223e30600bSdyoung 	char *iface;
8233e30600bSdyoung 	int ch, dumpinfo, dumpstats, apscan;
82432687868Senami 
82532687868Senami #define	SET_OPERAND(opr, desc) do {				\
82632687868Senami 	if ((opr) == NULL)					\
82732687868Senami 		(opr) = optarg;					\
82832687868Senami 	else							\
82932687868Senami 		warnx("%s is already specified to %s",		\
83032687868Senami 		    desc, (opr));				\
83132687868Senami } while (0)
83232687868Senami 
83332687868Senami 	dumpinfo = 1;
83432687868Senami 	dumpstats = 0;
8351635f76bSichiro 	apscan = 0;
8363e30600bSdyoung 	iface = NULL;
83782cee51cSexplorer 
83882cee51cSexplorer 	if (argc > 1 && argv[1][0] != '-') {
83982cee51cSexplorer 		iface = argv[1];
8407c396cadSenami 		optind++;
84182cee51cSexplorer 	}
8427c002751Ssommerfeld 
8437c002751Ssommerfeld 	while ((ch = getopt(argc, argv,
84400675c7eSwrstuden 	    "a:d:g:hi:m:or:s:A:M:R:D")) != -1) {
84532687868Senami 		if (ch != 'i')
84632687868Senami 			dumpinfo = 0;
84732687868Senami 		/*
848c6486717Swiz 		 * Lookup generic options and remember operand if found.
84932687868Senami 		 */
850311c2213Slukem 		wt = NULL;	/* XXXGCC -Wuninitialized */
85132687868Senami 		for (table = wi_tables; *table != NULL; table++)
85232687868Senami 			if ((wt = wi_optlookup(*table, ch)) != NULL) {
85332687868Senami 				SET_OPERAND(wt->wi_optval, wt->wi_desc);
85432687868Senami 				break;
85532687868Senami 			}
85632687868Senami 		if (wt == NULL)
85732687868Senami 			/*
85832687868Senami 			 * Handle special options.
85932687868Senami 			 */
8607c002751Ssommerfeld 			switch (ch) {
8617c002751Ssommerfeld 			case 'o':
86232687868Senami 				dumpstats = 1;
8637c002751Ssommerfeld 				break;
8647c002751Ssommerfeld 			case 'i':
86532687868Senami 				SET_OPERAND(iface, "interface");
86682cee51cSexplorer 				break;
8671635f76bSichiro 			case 'D':
8681635f76bSichiro 				apscan = 1;
8691635f76bSichiro 				break;
8707c002751Ssommerfeld 			case 'h':
8717c002751Ssommerfeld 			default:
87232687868Senami 				usage();
8737c002751Ssommerfeld 				break;
8747c002751Ssommerfeld 			}
8757c002751Ssommerfeld 	}
8767c002751Ssommerfeld 
8777c002751Ssommerfeld 	if (iface == NULL)
87832687868Senami 		usage();
8797c002751Ssommerfeld 
8809dd4b8a2Ssborrill 	/* Check interface is wireless. Will not return on error */
8819dd4b8a2Ssborrill 	wi_checkwifi(iface);
8829dd4b8a2Ssborrill 
88332687868Senami 	for (table = wi_tables; *table != NULL; table++)
88432687868Senami 		for (wt = *table; wt->wi_code != WI_NONE; wt++)
88532687868Senami 			if (wt->wi_optval != NULL) {
88632687868Senami 				switch (wt->wi_code) {
88732687868Senami 				case WI_BOOL:
88832687868Senami 				case WI_WORDS:
88932687868Senami 					wi_setword(iface, wt->wi_type,
89032687868Senami 					    atoi(wt->wi_optval));
89132687868Senami 					break;
89232687868Senami 				case WI_STRING:
89332687868Senami 					wi_setstr(iface, wt->wi_type,
89432687868Senami 					    wt->wi_optval);
89532687868Senami 					break;
89632687868Senami 				case WI_HEXBYTES:
89732687868Senami 					wi_sethex(iface, wt->wi_type,
89832687868Senami 					    wt->wi_optval);
89932687868Senami 					break;
90032687868Senami 				}
901a4563961Sexplorer 			}
90282cee51cSexplorer 
90332687868Senami 	if (dumpstats)
90432687868Senami 		wi_dumpstats(iface);
90532687868Senami 	if (dumpinfo)
9067c002751Ssommerfeld 		wi_dumpinfo(iface);
907d08bc504Schristos 
9081635f76bSichiro 	if (apscan)
909d08bc504Schristos #ifdef WI_RID_SCAN_APS
9101635f76bSichiro 		wi_apscan(iface);
911d08bc504Schristos #else
912d08bc504Schristos 		errx(1, "AP scan mode is not available.");
913d08bc504Schristos #endif
9147c002751Ssommerfeld 
9157c002751Ssommerfeld 	exit(0);
9167c002751Ssommerfeld }
917