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