1116742Ssam/*	$NetBSD: ipwctl.c,v 1.8 2006/08/06 07:31:32 skrll Exp $	*/
2116904Ssam/*	Id: ipwctl.c,v 1.1.2.1 2004/08/19 16:24:50 damien Exp 	*/
3186904Ssam
4116742Ssam/*-
5116742Ssam * Copyright (c) 2004
6116742Ssam *	Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
7116742Ssam *
8116742Ssam * Redistribution and use in source and binary forms, with or without
9116742Ssam * modification, are permitted provided that the following conditions
10116904Ssam * are met:
11116904Ssam * 1. Redistributions of source code must retain the above copyright
12116904Ssam *    notice unmodified, this list of conditions, and the following
13116904Ssam *    disclaimer.
14116742Ssam * 2. Redistributions in binary form must reproduce the above copyright
15116904Ssam *    notice, this list of conditions and the following disclaimer in the
16116904Ssam *    documentation and/or other materials provided with the distribution.
17116904Ssam *
18116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19116904Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20116904Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21116904Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22116904Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23116904Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24116904Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25116742Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26116742Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27116742Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28116742Ssam * SUCH DAMAGE.
29116742Ssam */
30178354Ssam
31178354Ssam#include <sys/cdefs.h>
32116742Ssam__RCSID("$NetBSD: ipwctl.c,v 1.8 2006/08/06 07:31:32 skrll Exp $");
33116742Ssam
34116742Ssam#include <sys/types.h>
35116742Ssam#include <sys/ioctl.h>
36116742Ssam#include <sys/mman.h>
37138568Ssam#include <sys/socket.h>
38116742Ssam#include <sys/stat.h>
39116742Ssam
40116742Ssam#include <net/if.h>
41257176Sglebius
42116742Ssam#include <err.h>
43116742Ssam#include <errno.h>
44116742Ssam#include <fcntl.h>
45116742Ssam#include <stdio.h>
46178354Ssam#include <stdlib.h>
47190391Ssam#include <string.h>
48190391Ssam#include <sysexits.h>
49190391Ssam#include <unistd.h>
50186904Ssam
51186904Ssam#define SIOCGRADIO	_IOWR('i', 139, struct ifreq)
52186904Ssam#define SIOCGTABLE1	_IOWR('i', 140, struct ifreq)
53178354Ssam
54195618Srpaulostatic void usage(void) __dead;
55206358Srpaulostatic int do_req(const char *, unsigned long, void *);
56116742Ssamstatic void get_radio_state(const char *);
57116742Ssamstatic void get_statistics(const char *);
58116742Ssam
59147221Ssamint
60195618Srpaulomain(int argc, char **argv)
61195618Srpaulo{
62195618Srpaulo	int ch;
63195618Srpaulo	const char *iface;
64195618Srpaulo
65147221Ssam	setprogname(argv[0]);
66147221Ssam	opterr = 0;
67178354Ssam	ch = getopt(argc, argv, "i:");
68178354Ssam	if (ch == 'i') {
69178354Ssam		iface = optarg;
70178354Ssam	} else {
71178354Ssam		if (argc > 1 && argv[1][0] != '-') {
72178354Ssam			iface = argv[1];
73178354Ssam			optind = 2;
74178354Ssam		} else {
75147221Ssam			iface = "ipw0";
76170530Ssam			optind = 1;
77170530Ssam		}
78179643Ssam		optreset = 1;
79179643Ssam	}
80138568Ssam	opterr = 1;
81138568Ssam
82178354Ssam	while ((ch = getopt(argc, argv, "kr")) != -1) {
83170530Ssam		switch (ch) {
84170530Ssam		case 'r':
85178354Ssam			get_radio_state(iface);
86178354Ssam			return EX_OK;
87116742Ssam
88138568Ssam		default:
89120104Ssam			usage();
90282372Sadrian		}
91282372Sadrian	}
92138568Ssam
93148863Ssam	get_statistics(iface);
94170530Ssam
95178354Ssam	return EX_OK;
96178354Ssam}
97138568Ssam
98172211Ssamstatic void
99138568Ssamusage(void)
100127876Ssam{
101178354Ssam	(void)fprintf(stderr, "Usage:  %s -i iface\n"
102120481Ssam	    "\t%s -i iface -r\n", getprogname(), getprogname());
103116742Ssam
104138568Ssam	exit(EX_USAGE);
105116742Ssam}
106195379Ssam
107195379Ssamstatic int
108195379Ssamdo_req(const char *iface, unsigned long req, void *data)
109178354Ssam{
110178354Ssam	int s;
111283291Sjkim	struct ifreq ifr;
112178354Ssam	int error, serrno;
113178354Ssam
114116742Ssam	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
115138568Ssam		err(EX_OSERR, "Can't create socket");
116138568Ssam
117138568Ssam	memset(&ifr, 0, sizeof(ifr));
118178354Ssam	strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
119178354Ssam	ifr.ifr_data = data;
120138568Ssam	error = ioctl(s, req, &ifr);
121170530Ssam	serrno = errno;
122178354Ssam	(void)close(s);
123138568Ssam	errno = serrno;
124178354Ssam	return error;
125178354Ssam}
126178354Ssam
127178354Ssamstatic void
128178354Ssamget_radio_state(const char *iface)
129178354Ssam{
130138568Ssam	int radio;
131178354Ssam
132178354Ssam	if (do_req(iface, SIOCGRADIO, &radio) == -1) {
133178354Ssam		if (errno == ENOTTY)
134170530Ssam			errx(EX_OSERR, "Can't retrieve radio transmitter "
135178354Ssam			    "state: No firmware");
136178354Ssam		else
137195379Ssam			err(EX_OSERR, "Can't retrieve radio transmitter state");
138178354Ssam	}
139172062Ssam
140178354Ssam	(void)printf("Radio is %s\n", radio ? "ON" : "OFF");
141178354Ssam}
142178354Ssam
143178354Ssamstruct statistic {
144178354Ssam	int index;
145178354Ssam	const char *desc;
146178354Ssam	int unit;
147178354Ssam#define INT		1
148178354Ssam#define HEX		2
149178354Ssam#define MASK		HEX
150178354Ssam#define PERCENTAGE	3
151184277Ssam#define BOOL		4
152184277Ssam};
153184277Ssam
154184277Ssam/*-
155184277Ssam * TIM  = Traffic Information Message
156148863Ssam * DTIM = Delivery TIM
157148863Ssam * ATIM = Announcement TIM
158148863Ssam * PSP  = Power Save Poll
159178354Ssam * RTS  = Request To Send
160148863Ssam * CTS  = Clear To Send
161178354Ssam * RSSI = Received Signal Strength Indicator
162178354Ssam */
163178354Ssam
164178354Ssamstatic const struct statistic tbl[] = {
165178354Ssam	{ 1, "Number of frames submitted for transfer", INT },
166178354Ssam	{ 2, "Number of frames transmitted", INT },
167178354Ssam	{ 3, "Number of unicast frames transmitted", INT },
168178354Ssam	{ 4, "Number of unicast frames transmitted at 1Mb/s", INT },
169283538Sadrian	{ 5, "Number of unicast frames transmitted at 2Mb/s", INT },
170184210Sdes	{ 6, "Number of unicast frames transmitted at 5.5Mb/s", INT },
171283538Sadrian	{ 7, "Number of unicast frames transmitted at 11Mb/s", INT },
172283538Sadrian
173178354Ssam	{ 13, "Number of multicast frames transmitted at 1Mb/s", INT },
174178354Ssam	{ 14, "Number of multicast frames transmitted at 2Mb/s", INT },
175178354Ssam	{ 15, "Number of multicast frames transmitted at 5.5Mb/s", INT },
176178354Ssam	{ 16, "Number of multicast frames transmitted at 11Mb/s", INT },
177178354Ssam
178178354Ssam	{ 21, "Number of null frames transmitted", INT },
179138568Ssam	{ 22, "Number of RTS frames transmitted", INT },
180138568Ssam	{ 23, "Number of CTS frames transmitted", INT },
181178354Ssam	{ 24, "Number of ACK frames transmitted", INT },
182118887Ssam	{ 25, "Number of association requests transmitted", INT },
183178354Ssam	{ 26, "Number of association responses transmitted", INT },
184116742Ssam	{ 27, "Number of reassociation requests transmitted", INT },
185116742Ssam	{ 28, "Number of reassociation responses transmitted", INT },
186116742Ssam	{ 29, "Number of probe requests transmitted", INT },
187178354Ssam	{ 30, "Number of probe reponses transmitted", INT },
188116742Ssam	{ 31, "Number of beacons transmitted", INT },
189178354Ssam	{ 32, "Number of ATIM frames transmitted", INT },
190116742Ssam	{ 33, "Number of disassociation requests transmitted", INT },
191178354Ssam	{ 34, "Number of authentification requests transmitted", INT },
192178354Ssam	{ 35, "Number of deauthentification requests transmitted", INT },
193178354Ssam
194178354Ssam	{ 41, "Number of bytes transmitted", INT },
195138568Ssam	{ 42, "Number of transmission retries", INT },
196178354Ssam	{ 43, "Number of transmission retries at 1Mb/s", INT },
197283538Sadrian	{ 44, "Number of transmission retries at 2Mb/s", INT },
198178354Ssam	{ 45, "Number of transmission retries at 5.5Mb/s", INT },
199138568Ssam	{ 46, "Number of transmission retries at 11Mb/s", INT },
200116742Ssam
201116742Ssam	{ 51, "Number of transmission failures", INT },
202138568Ssam
203138568Ssam	{ 54, "Number of transmission aborted due to DMA", INT },
204138568Ssam
205138568Ssam	{ 56, "Number of disassociation failures", INT },
206138568Ssam
207148302Ssam	{ 58, "Number of spanning tree frames transmitted", INT },
208138568Ssam	{ 59, "Number of transmission errors due to missing ACK", INT },
209184277Ssam
210184277Ssam	{ 61, "Number of frames received", INT },
211138568Ssam	{ 62, "Number of unicast frames received", INT },
212184277Ssam	{ 63, "Number of unicast frames received at 1Mb/s", INT },
213172062Ssam	{ 64, "Number of unicast frames received at 2Mb/s", INT },
214184277Ssam	{ 65, "Number of unicast frames received at 5.5Mb/s", INT },
215184277Ssam	{ 66, "Number of unicast frames received at 11Mb/s", INT },
216184277Ssam
217138568Ssam	{ 71, "Number of multicast frames received", INT },
218138568Ssam	{ 72, "Number of multicast frames received at 1Mb/s", INT },
219138568Ssam	{ 73, "Number of multicast frames received at 2Mb/s", INT },
220148302Ssam	{ 74, "Number of multicast frames received at 5.5Mb/s", INT },
221138568Ssam	{ 75, "Number of multicast frames received at 11Mb/s", INT },
222184277Ssam
223184277Ssam	{ 80, "Number of null frames received", INT },
224138568Ssam	{ 81, "Number of poll frames received", INT },
225184277Ssam	{ 82, "Number of RTS frames received", INT },
226172062Ssam	{ 83, "Number of CTS frames received", INT },
227172062Ssam	{ 84, "Number of ACK frames received", INT },
228184277Ssam	{ 85, "Number of CF-End frames received", INT },
229184277Ssam	{ 86, "Number of CF-End + CF-Ack frames received", INT },
230184277Ssam	{ 87, "Number of association requests received", INT },
231184277Ssam	{ 88, "Number of association responses received", INT },
232138568Ssam	{ 89, "Number of reassociation requests received", INT },
233138568Ssam	{ 90, "Number of reassociation responses received", INT },
234116742Ssam	{ 91, "Number of probe requests received", INT },
235183251Ssam	{ 92, "Number of probe reponses received", INT },
236183251Ssam	{ 93, "Number of beacons received", INT },
237193966Ssam	{ 94, "Number of ATIM frames received", INT },
238193966Ssam	{ 95, "Number of disassociation requests received", INT },
239183251Ssam	{ 96, "Number of authentification requests received", INT },
240183251Ssam	{ 97, "Number of deauthentification requests received", INT },
241187898Ssam
242183251Ssam	{ 101, "Number of bytes received", INT },
243183251Ssam	{ 102, "Number of frames with a bad CRC received", INT },
244183251Ssam	{ 103, "Number of frames with a bad CRC received at 1Mb/s", INT },
245187898Ssam	{ 104, "Number of frames with a bad CRC received at 2Mb/s", INT },
246183251Ssam	{ 105, "Number of frames with a bad CRC received at 5.5Mb/s", INT },
247187898Ssam	{ 106, "Number of frames with a bad CRC received at 11Mb/s", INT },
248183251Ssam
249187898Ssam	{ 112, "Number of duplicated frames received at 1Mb/s", INT },
250187898Ssam	{ 113, "Number of duplicated frames received at 2Mb/s", INT },
251188782Ssam	{ 114, "Number of duplicated frames received at 5.5Mb/s", INT },
252188782Ssam	{ 115, "Number of duplicated frames received at 11Mb/s", INT },
253188782Ssam
254188782Ssam	{ 119, "Number of duplicated frames received", INT },
255191015Ssam
256187898Ssam	{ 123, "Number of frames with a bad protocol received", INT },
257187898Ssam	{ 124, "Boot time", INT },
258191015Ssam	{ 125, "Number of frames dropped due to missing buffer", INT },
259191015Ssam	{ 126, "Number of frames dropped due to DMA", INT },
260187898Ssam
261183251Ssam	{ 128, "Number of frames dropped due to missing fragment", INT },
262187898Ssam	{ 129, "Number of frames dropped due to non-seq fragment", INT },
263183251Ssam	{ 130, "Number of frames dropped due to missing first frame", INT },
264187898Ssam	{ 131, "Number of frames dropped due to uncompleted frame", INT },
265183251Ssam
266183251Ssam	{ 137, "Number of times adapter suspended", INT },
267183251Ssam	{ 138, "Beacon timeout", INT },
268138568Ssam	{ 139, "Number of poll response timeouts", INT },
269138568Ssam
270178354Ssam	{ 141, "Number of PSP DTIM frames received", INT },
271138568Ssam	{ 142, "Number of PSP TIM frames received", INT },
272178354Ssam	{ 143, "PSP station Id", INT },
273178354Ssam
274178354Ssam	{ 147, "RTC time of last association", INT },
275138568Ssam	{ 148, "Percentage of missed beacons", PERCENTAGE },
276178354Ssam	{ 149, "Percentage of missed transmission retries", PERCENTAGE },
277183251Ssam
278183251Ssam	{ 151, "Number of access points in access points table", INT },
279170530Ssam
280178354Ssam	{ 153, "Number of associations", INT },
281178354Ssam	{ 154, "Number of association failures", INT },
282138568Ssam	{ 156, "Number of full scans", INT },
283183251Ssam	{ 157, "Card disabled", BOOL },
284170530Ssam
285170530Ssam	{ 160, "RSSI at time of association", INT },
286219602Sbschmidt	{ 161, "Number of reassociations due to no probe response", INT },
287170530Ssam	{ 162, "Number of reassociations due to poor line quality", INT },
288170530Ssam	{ 163, "Number of reassociations due to load", INT },
289170530Ssam	{ 164, "Number of reassociations due to access point RSSI level", INT },
290183251Ssam	{ 165, "Number of reassociations due to load leveling", INT },
291183251Ssam
292183251Ssam	{ 170, "Number of times authentification failed", INT },
293183251Ssam	{ 171, "Number of times authentification response failed", INT },
294183251Ssam	{ 172, "Number of entries in association table", INT },
295183251Ssam	{ 173, "Average RSSI", INT },
296193655Ssam
297183251Ssam	{ 176, "Self test status", INT },
298183251Ssam	{ 177, "Power mode", INT },
299193655Ssam	{ 178, "Power index", INT },
300183251Ssam	{ 179, "IEEE country code", HEX },
301183251Ssam	{ 180, "Channels supported for this country", MASK },
302183251Ssam	{ 181, "Number of adapter warm resets", INT },
303183251Ssam	{ 182, "Beacon interval", INT },
304170530Ssam
305183251Ssam	{ 184, "Princeton version", INT },
306165569Ssam	{ 185, "Antenna diversity disabled", BOOL },
307138568Ssam	{ 186, "CCA RSSI", INT },
308138568Ssam	{ 187, "Number of times EEPROM updated", INT },
309141658Ssam	{ 188, "Beacon intervals between DTIM", INT },
310141658Ssam	{ 189, "Current channel", INT },
311141658Ssam	{ 190, "RTC time", INT },
312141658Ssam	{ 191, "Operating mode", INT },
313141658Ssam	{ 192, "Transmission rate", HEX },
314141658Ssam	{ 193, "Supported transmission rates", MASK },
315141658Ssam	{ 194, "ATIM window", INT },
316141658Ssam	{ 195, "Supported basic transmission rates", MASK },
317178354Ssam	{ 196, "Adapter highest rate", HEX },
318141658Ssam	{ 197, "Access point highest rate", HEX },
319141658Ssam	{ 198, "Management frame capability", BOOL },
320116742Ssam	{ 199, "Type of authentification", INT },
321178354Ssam	{ 200, "Adapter card platform type", INT },
322116742Ssam	{ 201, "RTS threshold", INT },
323178354Ssam	{ 202, "International mode", BOOL },
324116742Ssam	{ 203, "Fragmentation threshold", INT },
325116742Ssam
326178354Ssam	{ 213, "Microcode version", INT },
327297731Sadrian
328195618Srpaulo	{ 0, NULL, 0 }
329297731Sadrian};
330297731Sadrian
331138568Ssamstatic void
332178354Ssamget_statistics(const char *iface)
333140753Ssam{
334140753Ssam	static unsigned long stats[256]; /* XXX */
335138568Ssam	const struct statistic *st;
336138568Ssam
337178354Ssam	if (do_req(iface, SIOCGTABLE1, stats) == -1) {
338178354Ssam		if (errno == ENOTTY)
339178354Ssam			errx(EX_OSERR, "Can't retrieve statistics: No "
340178354Ssam			    "firmware");
341178354Ssam		else
342148843Ssam			err(EX_OSERR, "Can't retrieve statistics");
343178354Ssam	}
344116742Ssam
345116742Ssam	for (st = tbl; st->index != 0; st++) {
346116742Ssam		(void)printf("%-60s[", st->desc);
347116742Ssam		switch (st->unit) {
348116742Ssam		case INT:
349178354Ssam			(void)printf("%lu", stats[st->index]);
350178354Ssam			break;
351138568Ssam
352178354Ssam		case BOOL:
353178354Ssam			(void)printf(stats[st->index] ? "true" : "false");
354167282Ssam			break;
355167282Ssam
356167282Ssam		case PERCENTAGE:
357167282Ssam			(void)printf("%lu%%", stats[st->index]);
358167282Ssam			break;
359178354Ssam
360178354Ssam		case HEX:
361178354Ssam		default:
362153403Ssam			(void)printf("0x%08lX", stats[st->index]);
363186904Ssam		}
364186904Ssam		(void)printf("]\n");
365186904Ssam	}
366153403Ssam}
367195618Srpaulo