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