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