xref: /netbsd/usr.sbin/ipwctl/ipwctl.c (revision b97cb66e)
1 /*	$NetBSD: ipwctl.c,v 1.5 2005/04/03 17:27:15 christos 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.5 2005/04/03 17:27:15 christos 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 SIOCSLOADFW	 _IOW('i', 137, struct ifreq)
52 #define SIOCSKILLFW	 _IOW('i', 138, struct ifreq)
53 #define SIOCGRADIO	_IOWR('i', 139, struct ifreq)
54 #define SIOCGTABLE1	_IOWR('i', 140, struct ifreq)
55 
56 static void usage(void) __attribute__((__noreturn__));
57 static int do_req(const char *, unsigned long, void *);
58 static void load_firmware(const char *, const char *);
59 static void kill_firmware(const char *);
60 static void get_radio_state(const char *);
61 static void get_statistics(const char *);
62 
63 int
64 main(int argc, char **argv)
65 {
66 	int ch;
67 	const char *iface;
68 
69 	setprogname(argv[0]);
70 	opterr = 0;
71 	ch = getopt(argc, argv, "i:");
72 	if (ch == 'i') {
73 		iface = optarg;
74 	} else {
75 		if (argc > 1 && argv[1][0] != '-') {
76 			iface = argv[1];
77 			optind = 2;
78 		} else {
79 			iface = "ipw0";
80 			optind = 1;
81 		}
82 		optreset = 1;
83 	}
84 	opterr = 1;
85 
86 	while ((ch = getopt(argc, argv, "f:kr")) != -1) {
87 		switch (ch) {
88 		case 'f':
89 			load_firmware(iface, optarg);
90 			return EX_OK;
91 
92 		case 'k':
93 			kill_firmware(iface);
94 			return EX_OK;
95 
96 		case 'r':
97 			get_radio_state(iface);
98 			return EX_OK;
99 
100 		default:
101 			usage();
102 		}
103 	}
104 
105 	get_statistics(iface);
106 
107 	return EX_OK;
108 }
109 
110 static void
111 usage(void)
112 {
113 	(void)fprintf(stderr, "Usage:  %s -i iface\n"
114 	    "\t%s -i iface -f firmware\n"
115 	    "\t%s -i iface -k\n"
116 	    "\t%s -i iface -r\n", getprogname(), getprogname(), getprogname(),
117 	    getprogname());
118 
119 	exit(EX_USAGE);
120 }
121 
122 static int
123 do_req(const char *iface, unsigned long req, void *data)
124 {
125 	int s;
126 	struct ifreq ifr;
127 	int error, serrno;
128 
129 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
130 		err(EX_OSERR, "Can't create socket");
131 
132 	memset(&ifr, 0, sizeof(ifr));
133 	strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
134 	ifr.ifr_data = data;
135 	error = ioctl(s, req, &ifr);
136 	serrno = errno;
137 	(void)close(s);
138 	errno = serrno;
139 	return error;
140 }
141 
142 static void
143 load_firmware(const char *iface, const char *firmware)
144 {
145 	int fd;
146 	struct stat st;
147 	void *map;
148 	size_t len;
149 
150 	if ((fd = open(firmware, O_RDONLY)) == -1)
151 		err(EX_OSERR, "%s", firmware);
152 
153 	if (fstat(fd, &st) == -1)
154 		err(EX_OSERR, "Unable to stat %s", firmware);
155 
156 	len = (size_t)st.st_size;
157 	map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
158 	if (map == MAP_FAILED)
159 		err(EX_OSERR, "Can't map %s into memory", firmware);
160 
161 	if (do_req(iface, SIOCSLOADFW, map) == -1)
162 		err(EX_OSERR, "Can't load %s to driver", firmware);
163 
164 	(void)munmap(map, st.st_size);
165 	(void)close(fd);
166 }
167 
168 static void
169 kill_firmware(const char *iface)
170 {
171 	if (do_req(iface, SIOCSKILLFW, NULL) == -1)
172 		err(EX_OSERR, "Can't kill firmware");
173 }
174 
175 static void
176 get_radio_state(const char *iface)
177 {
178 	int radio;
179 
180 	if (do_req(iface, SIOCGRADIO, &radio) == -1) {
181 		if (errno == ENOTTY)
182 			errx(EX_OSERR, "Can't retrieve radio transmitter "
183 			    "state: No firmware");
184 		else
185 			err(EX_OSERR, "Can't retrieve radio transmitter state");
186 	}
187 
188 	(void)printf("Radio is %s\n", radio ? "ON" : "OFF");
189 }
190 
191 struct statistic {
192 	int index;
193 	const char *desc;
194 	int unit;
195 #define INT		1
196 #define HEX		2
197 #define MASK		HEX
198 #define PERCENTAGE	3
199 #define BOOL		4
200 };
201 
202 /*-
203  * TIM  = Traffic Information Message
204  * DTIM = Delivery TIM
205  * ATIM = Announcement TIM
206  * PSP  = Power Save Poll
207  * RTS  = Request To Send
208  * CTS  = Clear To Send
209  * RSSI = Received Signal Strength Indicator
210  */
211 
212 static const struct statistic tbl[] = {
213 	{ 1, "Number of frames submitted for transfer", INT },
214 	{ 2, "Number of frames transmitted", INT },
215 	{ 3, "Number of unicast frames transmitted", INT },
216 	{ 4, "Number of unicast frames transmitted at 1Mb/s", INT },
217 	{ 5, "Number of unicast frames transmitted at 2Mb/s", INT },
218 	{ 6, "Number of unicast frames transmitted at 5.5Mb/s", INT },
219 	{ 7, "Number of unicast frames transmitted at 11Mb/s", INT },
220 
221 	{ 13, "Number of multicast frames transmitted at 1Mb/s", INT },
222 	{ 14, "Number of multicast frames transmitted at 2Mb/s", INT },
223 	{ 15, "Number of multicast frames transmitted at 5.5Mb/s", INT },
224 	{ 16, "Number of multicast frames transmitted at 11Mb/s", INT },
225 
226 	{ 21, "Number of null frames transmitted", INT },
227 	{ 22, "Number of RTS frames transmitted", INT },
228 	{ 23, "Number of CTS frames transmitted", INT },
229 	{ 24, "Number of ACK frames transmitted", INT },
230 	{ 25, "Number of association requests transmitted", INT },
231 	{ 26, "Number of association responses transmitted", INT },
232 	{ 27, "Number of reassociation requests transmitted", INT },
233 	{ 28, "Number of reassociation responses transmitted", INT },
234 	{ 29, "Number of probe requests transmitted", INT },
235 	{ 30, "Number of probe reponses transmitted", INT },
236 	{ 31, "Number of beacons transmitted", INT },
237 	{ 32, "Number of ATIM frames transmitted", INT },
238 	{ 33, "Number of disassociation requests transmitted", INT },
239 	{ 34, "Number of authentification requests transmitted", INT },
240 	{ 35, "Number of deauthentification requests transmitted", INT },
241 
242 	{ 41, "Number of bytes transmitted", INT },
243 	{ 42, "Number of transmission retries", INT },
244 	{ 43, "Number of transmission retries at 1Mb/s", INT },
245 	{ 44, "Number of transmission retries at 2Mb/s", INT },
246 	{ 45, "Number of transmission retries at 5.5Mb/s", INT },
247 	{ 46, "Number of transmission retries at 11Mb/s", INT },
248 
249 	{ 51, "Number of transmission failures", INT },
250 
251 	{ 54, "Number of transmission aborted due to DMA", INT },
252 
253 	{ 56, "Number of disassociation failures", INT },
254 
255 	{ 58, "Number of spanning tree frames transmitted", INT },
256 	{ 59, "Number of transmission errors due to missing ACK", INT },
257 
258 	{ 61, "Number of frames received", INT },
259 	{ 62, "Number of unicast frames received", INT },
260 	{ 63, "Number of unicast frames received at 1Mb/s", INT },
261 	{ 64, "Number of unicast frames received at 2Mb/s", INT },
262 	{ 65, "Number of unicast frames received at 5.5Mb/s", INT },
263 	{ 66, "Number of unicast frames received at 11Mb/s", INT },
264 
265 	{ 71, "Number of multicast frames received", INT },
266 	{ 72, "Number of multicast frames received at 1Mb/s", INT },
267 	{ 73, "Number of multicast frames received at 2Mb/s", INT },
268 	{ 74, "Number of multicast frames received at 5.5Mb/s", INT },
269 	{ 75, "Number of multicast frames received at 11Mb/s", INT },
270 
271 	{ 80, "Number of null frames received", INT },
272 	{ 81, "Number of poll frames received", INT },
273 	{ 82, "Number of RTS frames received", INT },
274 	{ 83, "Number of CTS frames received", INT },
275 	{ 84, "Number of ACK frames received", INT },
276 	{ 85, "Number of CF-End frames received", INT },
277 	{ 86, "Number of CF-End + CF-Ack frames received", INT },
278 	{ 87, "Number of association requests received", INT },
279 	{ 88, "Number of association responses received", INT },
280 	{ 89, "Number of reassociation requests received", INT },
281 	{ 90, "Number of reassociation responses received", INT },
282 	{ 91, "Number of probe requests received", INT },
283 	{ 92, "Number of probe reponses received", INT },
284 	{ 93, "Number of beacons received", INT },
285 	{ 94, "Number of ATIM frames received", INT },
286 	{ 95, "Number of disassociation requests received", INT },
287 	{ 96, "Number of authentification requests received", INT },
288 	{ 97, "Number of deauthentification requests received", INT },
289 
290 	{ 101, "Number of bytes received", INT },
291 	{ 102, "Number of frames with a bad CRC received", INT },
292 	{ 103, "Number of frames with a bad CRC received at 1Mb/s", INT },
293 	{ 104, "Number of frames with a bad CRC received at 2Mb/s", INT },
294 	{ 105, "Number of frames with a bad CRC received at 5.5Mb/s", INT },
295 	{ 106, "Number of frames with a bad CRC received at 11Mb/s", INT },
296 
297 	{ 112, "Number of duplicated frames received at 1Mb/s", INT },
298 	{ 113, "Number of duplicated frames received at 2Mb/s", INT },
299 	{ 114, "Number of duplicated frames received at 5.5Mb/s", INT },
300 	{ 115, "Number of duplicated frames received at 11Mb/s", INT },
301 
302 	{ 119, "Number of duplicated frames received", INT },
303 
304 	{ 123, "Number of frames with a bad protocol received", INT },
305 	{ 124, "Boot time", INT },
306 	{ 125, "Number of frames dropped due to missing buffer", INT },
307 	{ 126, "Number of frames dropped due to DMA", INT },
308 
309 	{ 128, "Number of frames dropped due to missing fragment", INT },
310 	{ 129, "Number of frames dropped due to non-seq fragment", INT },
311 	{ 130, "Number of frames dropped due to missing first frame", INT },
312 	{ 131, "Number of frames dropped due to uncompleted frame", INT },
313 
314 	{ 137, "Number of times adapter suspended", INT },
315 	{ 138, "Beacon timeout", INT },
316 	{ 139, "Number of poll response timeouts", INT },
317 
318 	{ 141, "Number of PSP DTIM frames received", INT },
319 	{ 142, "Number of PSP TIM frames received", INT },
320 	{ 143, "PSP station Id", INT },
321 
322 	{ 147, "RTC time of last association", INT },
323 	{ 148, "Percentage of missed beacons", PERCENTAGE },
324 	{ 149, "Percentage of missed transmission retries", PERCENTAGE },
325 
326 	{ 151, "Number of access points in access points table", INT },
327 
328 	{ 153, "Number of associations", INT },
329 	{ 154, "Number of association failures", INT },
330 	{ 156, "Number of full scans", INT },
331 	{ 157, "Card disabled", BOOL },
332 
333 	{ 160, "RSSI at time of association", INT },
334 	{ 161, "Number of reassociations due to no probe response", INT },
335 	{ 162, "Number of reassociations due to poor line quality", INT },
336 	{ 163, "Number of reassociations due to load", INT },
337 	{ 164, "Number of reassociations due to access point RSSI level", INT },
338 	{ 165, "Number of reassociations due to load leveling", INT },
339 
340 	{ 170, "Number of times authentification failed", INT },
341 	{ 171, "Number of times authentification response failed", INT },
342 	{ 172, "Number of entries in association table", INT },
343 	{ 173, "Average RSSI", INT },
344 
345 	{ 176, "Self test status", INT },
346 	{ 177, "Power mode", INT },
347 	{ 178, "Power index", INT },
348 	{ 179, "IEEE country code", HEX },
349 	{ 180, "Channels supported for this country", MASK },
350 	{ 181, "Number of adapter warm resets", INT },
351 	{ 182, "Beacon interval", INT },
352 
353 	{ 184, "Princeton version", INT },
354 	{ 185, "Antenna diversity disabled", BOOL },
355 	{ 186, "CCA RSSI", INT },
356 	{ 187, "Number of times EEPROM updated", INT },
357 	{ 188, "Beacon intervals between DTIM", INT },
358 	{ 189, "Current channel", INT },
359 	{ 190, "RTC time", INT },
360 	{ 191, "Operating mode", INT },
361 	{ 192, "Transmission rate", HEX },
362 	{ 193, "Supported transmission rates", MASK },
363 	{ 194, "ATIM window", INT },
364 	{ 195, "Supported basic transmission rates", MASK },
365 	{ 196, "Adapter highest rate", HEX },
366 	{ 197, "Access point highest rate", HEX },
367 	{ 198, "Management frame capability", BOOL },
368 	{ 199, "Type of authentification", INT },
369 	{ 200, "Adapter card platform type", INT },
370 	{ 201, "RTS threshold", INT },
371 	{ 202, "International mode", BOOL },
372 	{ 203, "Fragmentation threshold", INT },
373 
374 	{ 213, "Microcode version", INT },
375 
376 	{ 0, NULL, 0 }
377 };
378 
379 static void
380 get_statistics(const char *iface)
381 {
382 	static unsigned long stats[256]; /* XXX */
383 	const struct statistic *st;
384 
385 	if (do_req(iface, SIOCGTABLE1, stats) == -1) {
386 		if (errno == ENOTTY)
387 			errx(EX_OSERR, "Can't retrieve statistics: No "
388 			    "firmware");
389 		else
390 			err(EX_OSERR, "Can't retrieve statistics");
391 	}
392 
393 	for (st = tbl; st->index != 0; st++) {
394 		(void)printf("%-60s[", st->desc);
395 		switch (st->unit) {
396 		case INT:
397 			(void)printf("%lu", stats[st->index]);
398 			break;
399 
400 		case BOOL:
401 			(void)printf(stats[st->index] ? "true" : "false");
402 			break;
403 
404 		case PERCENTAGE:
405 			(void)printf("%lu%%", stats[st->index]);
406 			break;
407 
408 		case HEX:
409 		default:
410 			(void)printf("0x%08lX", stats[st->index]);
411 		}
412 		(void)printf("]\n");
413 	}
414 }
415