xref: /netbsd/usr.sbin/ipwctl/ipwctl.c (revision 6e7b39c9)
1 /*	$NetBSD: ipwctl.c,v 1.6 2006/04/17 17:50:51 rpaulo Exp $	*/
2 /*	Id: ipwctl.c,v 1.1.2.1 2004/08/19 16:24:50 damien Exp 	*/
3 
4 /*-
5  * Copyright (c) 2004
6  *	Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: ipwctl.c,v 1.6 2006/04/17 17:50:51 rpaulo Exp $");
33 
34 #include <sys/types.h>
35 #include <sys/ioctl.h>
36 #include <sys/mman.h>
37 #include <sys/socket.h>
38 #include <sys/stat.h>
39 
40 #include <net/if.h>
41 
42 #include <err.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <sysexits.h>
49 #include <unistd.h>
50 
51 #define SIOCSKILLFW	 _IOW('i', 138, struct ifreq)
52 #define SIOCGRADIO	_IOWR('i', 139, struct ifreq)
53 #define SIOCGTABLE1	_IOWR('i', 140, struct ifreq)
54 
55 static void usage(void) __attribute__((__noreturn__));
56 static int do_req(const char *, unsigned long, void *);
57 static void kill_firmware(const char *);
58 static void get_radio_state(const char *);
59 static void get_statistics(const char *);
60 
61 int
62 main(int argc, char **argv)
63 {
64 	int ch;
65 	const char *iface;
66 
67 	setprogname(argv[0]);
68 	opterr = 0;
69 	ch = getopt(argc, argv, "i:");
70 	if (ch == 'i') {
71 		iface = optarg;
72 	} else {
73 		if (argc > 1 && argv[1][0] != '-') {
74 			iface = argv[1];
75 			optind = 2;
76 		} else {
77 			iface = "ipw0";
78 			optind = 1;
79 		}
80 		optreset = 1;
81 	}
82 	opterr = 1;
83 
84 	while ((ch = getopt(argc, argv, "kr")) != -1) {
85 		switch (ch) {
86 		case 'k':
87 			kill_firmware(iface);
88 			return EX_OK;
89 
90 		case 'r':
91 			get_radio_state(iface);
92 			return EX_OK;
93 
94 		default:
95 			usage();
96 		}
97 	}
98 
99 	get_statistics(iface);
100 
101 	return EX_OK;
102 }
103 
104 static void
105 usage(void)
106 {
107 	(void)fprintf(stderr, "Usage:  %s -i iface\n"
108 	    "\t%s -i iface -k\n"
109 	    "\t%s -i iface -r\n", getprogname(), getprogname(), getprogname());
110 
111 	exit(EX_USAGE);
112 }
113 
114 static int
115 do_req(const char *iface, unsigned long req, void *data)
116 {
117 	int s;
118 	struct ifreq ifr;
119 	int error, serrno;
120 
121 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
122 		err(EX_OSERR, "Can't create socket");
123 
124 	memset(&ifr, 0, sizeof(ifr));
125 	strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
126 	ifr.ifr_data = data;
127 	error = ioctl(s, req, &ifr);
128 	serrno = errno;
129 	(void)close(s);
130 	errno = serrno;
131 	return error;
132 }
133 
134 static void
135 kill_firmware(const char *iface)
136 {
137 	if (do_req(iface, SIOCSKILLFW, NULL) == -1)
138 		err(EX_OSERR, "Can't kill firmware");
139 }
140 
141 static void
142 get_radio_state(const char *iface)
143 {
144 	int radio;
145 
146 	if (do_req(iface, SIOCGRADIO, &radio) == -1) {
147 		if (errno == ENOTTY)
148 			errx(EX_OSERR, "Can't retrieve radio transmitter "
149 			    "state: No firmware");
150 		else
151 			err(EX_OSERR, "Can't retrieve radio transmitter state");
152 	}
153 
154 	(void)printf("Radio is %s\n", radio ? "ON" : "OFF");
155 }
156 
157 struct statistic {
158 	int index;
159 	const char *desc;
160 	int unit;
161 #define INT		1
162 #define HEX		2
163 #define MASK		HEX
164 #define PERCENTAGE	3
165 #define BOOL		4
166 };
167 
168 /*-
169  * TIM  = Traffic Information Message
170  * DTIM = Delivery TIM
171  * ATIM = Announcement TIM
172  * PSP  = Power Save Poll
173  * RTS  = Request To Send
174  * CTS  = Clear To Send
175  * RSSI = Received Signal Strength Indicator
176  */
177 
178 static const struct statistic tbl[] = {
179 	{ 1, "Number of frames submitted for transfer", INT },
180 	{ 2, "Number of frames transmitted", INT },
181 	{ 3, "Number of unicast frames transmitted", INT },
182 	{ 4, "Number of unicast frames transmitted at 1Mb/s", INT },
183 	{ 5, "Number of unicast frames transmitted at 2Mb/s", INT },
184 	{ 6, "Number of unicast frames transmitted at 5.5Mb/s", INT },
185 	{ 7, "Number of unicast frames transmitted at 11Mb/s", INT },
186 
187 	{ 13, "Number of multicast frames transmitted at 1Mb/s", INT },
188 	{ 14, "Number of multicast frames transmitted at 2Mb/s", INT },
189 	{ 15, "Number of multicast frames transmitted at 5.5Mb/s", INT },
190 	{ 16, "Number of multicast frames transmitted at 11Mb/s", INT },
191 
192 	{ 21, "Number of null frames transmitted", INT },
193 	{ 22, "Number of RTS frames transmitted", INT },
194 	{ 23, "Number of CTS frames transmitted", INT },
195 	{ 24, "Number of ACK frames transmitted", INT },
196 	{ 25, "Number of association requests transmitted", INT },
197 	{ 26, "Number of association responses transmitted", INT },
198 	{ 27, "Number of reassociation requests transmitted", INT },
199 	{ 28, "Number of reassociation responses transmitted", INT },
200 	{ 29, "Number of probe requests transmitted", INT },
201 	{ 30, "Number of probe reponses transmitted", INT },
202 	{ 31, "Number of beacons transmitted", INT },
203 	{ 32, "Number of ATIM frames transmitted", INT },
204 	{ 33, "Number of disassociation requests transmitted", INT },
205 	{ 34, "Number of authentification requests transmitted", INT },
206 	{ 35, "Number of deauthentification requests transmitted", INT },
207 
208 	{ 41, "Number of bytes transmitted", INT },
209 	{ 42, "Number of transmission retries", INT },
210 	{ 43, "Number of transmission retries at 1Mb/s", INT },
211 	{ 44, "Number of transmission retries at 2Mb/s", INT },
212 	{ 45, "Number of transmission retries at 5.5Mb/s", INT },
213 	{ 46, "Number of transmission retries at 11Mb/s", INT },
214 
215 	{ 51, "Number of transmission failures", INT },
216 
217 	{ 54, "Number of transmission aborted due to DMA", INT },
218 
219 	{ 56, "Number of disassociation failures", INT },
220 
221 	{ 58, "Number of spanning tree frames transmitted", INT },
222 	{ 59, "Number of transmission errors due to missing ACK", INT },
223 
224 	{ 61, "Number of frames received", INT },
225 	{ 62, "Number of unicast frames received", INT },
226 	{ 63, "Number of unicast frames received at 1Mb/s", INT },
227 	{ 64, "Number of unicast frames received at 2Mb/s", INT },
228 	{ 65, "Number of unicast frames received at 5.5Mb/s", INT },
229 	{ 66, "Number of unicast frames received at 11Mb/s", INT },
230 
231 	{ 71, "Number of multicast frames received", INT },
232 	{ 72, "Number of multicast frames received at 1Mb/s", INT },
233 	{ 73, "Number of multicast frames received at 2Mb/s", INT },
234 	{ 74, "Number of multicast frames received at 5.5Mb/s", INT },
235 	{ 75, "Number of multicast frames received at 11Mb/s", INT },
236 
237 	{ 80, "Number of null frames received", INT },
238 	{ 81, "Number of poll frames received", INT },
239 	{ 82, "Number of RTS frames received", INT },
240 	{ 83, "Number of CTS frames received", INT },
241 	{ 84, "Number of ACK frames received", INT },
242 	{ 85, "Number of CF-End frames received", INT },
243 	{ 86, "Number of CF-End + CF-Ack frames received", INT },
244 	{ 87, "Number of association requests received", INT },
245 	{ 88, "Number of association responses received", INT },
246 	{ 89, "Number of reassociation requests received", INT },
247 	{ 90, "Number of reassociation responses received", INT },
248 	{ 91, "Number of probe requests received", INT },
249 	{ 92, "Number of probe reponses received", INT },
250 	{ 93, "Number of beacons received", INT },
251 	{ 94, "Number of ATIM frames received", INT },
252 	{ 95, "Number of disassociation requests received", INT },
253 	{ 96, "Number of authentification requests received", INT },
254 	{ 97, "Number of deauthentification requests received", INT },
255 
256 	{ 101, "Number of bytes received", INT },
257 	{ 102, "Number of frames with a bad CRC received", INT },
258 	{ 103, "Number of frames with a bad CRC received at 1Mb/s", INT },
259 	{ 104, "Number of frames with a bad CRC received at 2Mb/s", INT },
260 	{ 105, "Number of frames with a bad CRC received at 5.5Mb/s", INT },
261 	{ 106, "Number of frames with a bad CRC received at 11Mb/s", INT },
262 
263 	{ 112, "Number of duplicated frames received at 1Mb/s", INT },
264 	{ 113, "Number of duplicated frames received at 2Mb/s", INT },
265 	{ 114, "Number of duplicated frames received at 5.5Mb/s", INT },
266 	{ 115, "Number of duplicated frames received at 11Mb/s", INT },
267 
268 	{ 119, "Number of duplicated frames received", INT },
269 
270 	{ 123, "Number of frames with a bad protocol received", INT },
271 	{ 124, "Boot time", INT },
272 	{ 125, "Number of frames dropped due to missing buffer", INT },
273 	{ 126, "Number of frames dropped due to DMA", INT },
274 
275 	{ 128, "Number of frames dropped due to missing fragment", INT },
276 	{ 129, "Number of frames dropped due to non-seq fragment", INT },
277 	{ 130, "Number of frames dropped due to missing first frame", INT },
278 	{ 131, "Number of frames dropped due to uncompleted frame", INT },
279 
280 	{ 137, "Number of times adapter suspended", INT },
281 	{ 138, "Beacon timeout", INT },
282 	{ 139, "Number of poll response timeouts", INT },
283 
284 	{ 141, "Number of PSP DTIM frames received", INT },
285 	{ 142, "Number of PSP TIM frames received", INT },
286 	{ 143, "PSP station Id", INT },
287 
288 	{ 147, "RTC time of last association", INT },
289 	{ 148, "Percentage of missed beacons", PERCENTAGE },
290 	{ 149, "Percentage of missed transmission retries", PERCENTAGE },
291 
292 	{ 151, "Number of access points in access points table", INT },
293 
294 	{ 153, "Number of associations", INT },
295 	{ 154, "Number of association failures", INT },
296 	{ 156, "Number of full scans", INT },
297 	{ 157, "Card disabled", BOOL },
298 
299 	{ 160, "RSSI at time of association", INT },
300 	{ 161, "Number of reassociations due to no probe response", INT },
301 	{ 162, "Number of reassociations due to poor line quality", INT },
302 	{ 163, "Number of reassociations due to load", INT },
303 	{ 164, "Number of reassociations due to access point RSSI level", INT },
304 	{ 165, "Number of reassociations due to load leveling", INT },
305 
306 	{ 170, "Number of times authentification failed", INT },
307 	{ 171, "Number of times authentification response failed", INT },
308 	{ 172, "Number of entries in association table", INT },
309 	{ 173, "Average RSSI", INT },
310 
311 	{ 176, "Self test status", INT },
312 	{ 177, "Power mode", INT },
313 	{ 178, "Power index", INT },
314 	{ 179, "IEEE country code", HEX },
315 	{ 180, "Channels supported for this country", MASK },
316 	{ 181, "Number of adapter warm resets", INT },
317 	{ 182, "Beacon interval", INT },
318 
319 	{ 184, "Princeton version", INT },
320 	{ 185, "Antenna diversity disabled", BOOL },
321 	{ 186, "CCA RSSI", INT },
322 	{ 187, "Number of times EEPROM updated", INT },
323 	{ 188, "Beacon intervals between DTIM", INT },
324 	{ 189, "Current channel", INT },
325 	{ 190, "RTC time", INT },
326 	{ 191, "Operating mode", INT },
327 	{ 192, "Transmission rate", HEX },
328 	{ 193, "Supported transmission rates", MASK },
329 	{ 194, "ATIM window", INT },
330 	{ 195, "Supported basic transmission rates", MASK },
331 	{ 196, "Adapter highest rate", HEX },
332 	{ 197, "Access point highest rate", HEX },
333 	{ 198, "Management frame capability", BOOL },
334 	{ 199, "Type of authentification", INT },
335 	{ 200, "Adapter card platform type", INT },
336 	{ 201, "RTS threshold", INT },
337 	{ 202, "International mode", BOOL },
338 	{ 203, "Fragmentation threshold", INT },
339 
340 	{ 213, "Microcode version", INT },
341 
342 	{ 0, NULL, 0 }
343 };
344 
345 static void
346 get_statistics(const char *iface)
347 {
348 	static unsigned long stats[256]; /* XXX */
349 	const struct statistic *st;
350 
351 	if (do_req(iface, SIOCGTABLE1, stats) == -1) {
352 		if (errno == ENOTTY)
353 			errx(EX_OSERR, "Can't retrieve statistics: No "
354 			    "firmware");
355 		else
356 			err(EX_OSERR, "Can't retrieve statistics");
357 	}
358 
359 	for (st = tbl; st->index != 0; st++) {
360 		(void)printf("%-60s[", st->desc);
361 		switch (st->unit) {
362 		case INT:
363 			(void)printf("%lu", stats[st->index]);
364 			break;
365 
366 		case BOOL:
367 			(void)printf(stats[st->index] ? "true" : "false");
368 			break;
369 
370 		case PERCENTAGE:
371 			(void)printf("%lu%%", stats[st->index]);
372 			break;
373 
374 		case HEX:
375 		default:
376 			(void)printf("0x%08lX", stats[st->index]);
377 		}
378 		(void)printf("]\n");
379 	}
380 }
381