1277323Sdim/* $NetBSD: ipwctl.c,v 1.10 2023/06/24 05:26:01 msaitoh Exp $ */ 2277323Sdim/* Id: ipwctl.c,v 1.1.2.1 2004/08/19 16:24:50 damien Exp */ 3277323Sdim 4277323Sdim/*- 5277323Sdim * Copyright (c) 2004 6277323Sdim * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved. 7277323Sdim * 8277323Sdim * Redistribution and use in source and binary forms, with or without 9277323Sdim * modification, are permitted provided that the following conditions 10277323Sdim * are met: 11277323Sdim * 1. Redistributions of source code must retain the above copyright 12277323Sdim * notice unmodified, this list of conditions, and the following 13277323Sdim * disclaimer. 14277323Sdim * 2. Redistributions in binary form must reproduce the above copyright 15277323Sdim * notice, this list of conditions and the following disclaimer in the 16277323Sdim * documentation and/or other materials provided with the distribution. 17277323Sdim * 18277323Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19277323Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20277323Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21288943Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22277323Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23277323Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24277323Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25277323Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26277323Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27277323Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28277323Sdim * SUCH DAMAGE. 29277323Sdim */ 30277323Sdim 31277323Sdim#include <sys/cdefs.h> 32277323Sdim__RCSID("$NetBSD: ipwctl.c,v 1.10 2023/06/24 05:26:01 msaitoh Exp $"); 33277323Sdim 34277323Sdim#include <sys/types.h> 35277323Sdim#include <sys/ioctl.h> 36277323Sdim#include <sys/mman.h> 37277323Sdim#include <sys/socket.h> 38277323Sdim#include <sys/stat.h> 39277323Sdim 40277323Sdim#include <net/if.h> 41277323Sdim 42277323Sdim#include <err.h> 43277323Sdim#include <errno.h> 44277323Sdim#include <fcntl.h> 45277323Sdim#include <stdio.h> 46277323Sdim#include <stdlib.h> 47277323Sdim#include <string.h> 48277323Sdim#include <sysexits.h> 49277323Sdim#include <unistd.h> 50277323Sdim 51277323Sdim#define SIOCGRADIO _IOWR('i', 139, struct ifreq) 52277323Sdim#define SIOCGTABLE1 _IOWR('i', 140, struct ifreq) 53277323Sdim 54277323Sdimstatic void usage(void) __dead; 55277323Sdimstatic int do_req(const char *, unsigned long, void *); 56277323Sdimstatic void get_radio_state(const char *); 57277323Sdimstatic void get_statistics(const char *); 58277323Sdim 59277323Sdimint 60277323Sdimmain(int argc, char **argv) 61277323Sdim{ 62277323Sdim int ch; 63277323Sdim const char *iface; 64277323Sdim 65277323Sdim setprogname(argv[0]); 66277323Sdim opterr = 0; 67277323Sdim ch = getopt(argc, argv, "i:"); 68277323Sdim if (ch == 'i') { 69277323Sdim iface = optarg; 70277323Sdim } else { 71277323Sdim if (argc > 1 && argv[1][0] != '-') { 72277323Sdim iface = argv[1]; 73277323Sdim optind = 2; 74277323Sdim } else { 75277323Sdim iface = "ipw0"; 76277323Sdim optind = 1; 77288943Sdim } 78288943Sdim optreset = 1; 79288943Sdim } 80288943Sdim opterr = 1; 81288943Sdim 82288943Sdim while ((ch = getopt(argc, argv, "kr")) != -1) { 83288943Sdim switch (ch) { 84288943Sdim case 'r': 85288943Sdim get_radio_state(iface); 86288943Sdim return EX_OK; 87288943Sdim 88288943Sdim default: 89288943Sdim usage(); 90288943Sdim } 91277323Sdim } 92277323Sdim 93277323Sdim get_statistics(iface); 94277323Sdim 95277323Sdim return EX_OK; 96277323Sdim} 97277323Sdim 98277323Sdimstatic void 99277323Sdimusage(void) 100277323Sdim{ 101277323Sdim (void)fprintf(stderr, "Usage: %s -i iface\n" 102277323Sdim "\t%s -i iface -r\n", getprogname(), getprogname()); 103277323Sdim 104277323Sdim exit(EX_USAGE); 105277323Sdim} 106277323Sdim 107277323Sdimstatic int 108277323Sdimdo_req(const char *iface, unsigned long req, void *data) 109277323Sdim{ 110277323Sdim int s; 111277323Sdim struct ifreq ifr; 112277323Sdim int error, serrno; 113277323Sdim 114277323Sdim if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) 115277323Sdim err(EX_OSERR, "Can't create socket"); 116277323Sdim 117277323Sdim memset(&ifr, 0, sizeof(ifr)); 118277323Sdim strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name)); 119277323Sdim ifr.ifr_data = data; 120277323Sdim error = ioctl(s, req, &ifr); 121277323Sdim serrno = errno; 122277323Sdim (void)close(s); 123277323Sdim errno = serrno; 124277323Sdim return error; 125277323Sdim} 126277323Sdim 127277323Sdimstatic void 128277323Sdimget_radio_state(const char *iface) 129277323Sdim{ 130277323Sdim int radio; 131277323Sdim 132277323Sdim if (do_req(iface, SIOCGRADIO, &radio) == -1) { 133277323Sdim if (errno == ENOTTY) 134277323Sdim errx(EX_OSERR, "Can't retrieve radio transmitter " 135277323Sdim "state: No firmware"); 136277323Sdim else 137277323Sdim err(EX_OSERR, "Can't retrieve radio transmitter state"); 138277323Sdim } 139277323Sdim 140277323Sdim (void)printf("Radio is %s\n", radio ? "ON" : "OFF"); 141} 142 143struct 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 164static 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 331static void 332get_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