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