Home | History | Annotate | Line # | Download | only in iwictl
iwictl.c revision 1.3
      1 /*	$NetBSD: iwictl.c,v 1.3 2005/04/03 17:21:29 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2004, 2005
      5  *	Damien Bergamini <damien.bergamini (at) free.fr>. All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice unmodified, this list of conditions, and the following
     12  *    disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __RCSID("$NetBSD: iwictl.c,v 1.3 2005/04/03 17:21:29 christos Exp $");
     32 
     33 #include <sys/types.h>
     34 #include <sys/ioctl.h>
     35 #include <sys/mman.h>
     36 #include <sys/socket.h>
     37 #include <sys/stat.h>
     38 
     39 #include <net/if.h>
     40 
     41 #include <err.h>
     42 #include <errno.h>
     43 #include <fcntl.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <sysexits.h>
     48 #include <unistd.h>
     49 
     50 #define SIOCSLOADFW	 _IOW('i', 137, struct ifreq)
     51 #define SIOCSKILLFW	 _IOW('i', 138, struct ifreq)
     52 #define SIOCGRADIO	_IOWR('i', 139, struct ifreq)
     53 #define SIOCGTABLE0	_IOWR('i', 140, struct ifreq)
     54 
     55 struct firmware {
     56 	void	*boot;
     57 	int	boot_size;
     58 	void	*ucode;
     59 	int	ucode_size;
     60 	void	*main;
     61 	int	main_size;
     62 };
     63 
     64 struct header {
     65 	u_int32_t	version;
     66 	u_int32_t	mode;
     67 } __attribute__((__packed__));
     68 
     69 static void usage(void) __attribute__((__noreturn__));
     70 static int do_req(const char *, unsigned long, void *);
     71 static void mmap_file(const char *, void **, size_t *);
     72 static void load_firmware(const char *, const char *, const char *);
     73 static void kill_firmware(const char *);
     74 static void get_radio_state(const char *);
     75 static void get_statistics(const char *);
     76 
     77 int
     78 main(int argc, char **argv)
     79 {
     80 	int ch;
     81 	char *iface = NULL, *path = NULL;
     82 	const char *mode = "bss";
     83 	int noflag = 1, kflag = 0, rflag = 0;
     84 
     85 	setprogname(argv[0]);
     86 	if (argc > 1 && argv[1][0] != '-') {
     87 		iface = argv[1];
     88 		optind++;
     89 	}
     90 
     91 	while ((ch = getopt(argc, argv, "d:i:km:r")) != -1) {
     92 		if (ch != 'i')
     93 			noflag = 0;
     94 
     95 		switch (ch) {
     96 		case 'd':
     97 			path = optarg;
     98 			break;
     99 
    100 		case 'i':
    101 			iface = optarg;
    102 			break;
    103 
    104 		case 'k':
    105 			kflag = 1;
    106 			break;
    107 
    108 		case 'm':
    109 			mode = optarg;
    110 			break;
    111 
    112 		case 'r':
    113 			rflag = 1;
    114 			break;
    115 
    116 		default:
    117 			usage();
    118 		}
    119 	}
    120 
    121 	if (iface == NULL)
    122 		usage();
    123 
    124 	if (kflag && (path != NULL || rflag))
    125 		usage();
    126 
    127 	if (kflag)
    128 		kill_firmware(iface);
    129 
    130 	if (path != NULL)
    131 		load_firmware(iface, path, mode);
    132 
    133 	if (rflag)
    134 		get_radio_state(iface);
    135 
    136 	if (noflag)
    137 		get_statistics(iface);
    138 
    139 	return EX_OK;
    140 }
    141 
    142 static void
    143 usage(void)
    144 {
    145 	(void)fprintf(stderr, "Usage:  %s iface\n"
    146 	    "\t%s iface -d path [-m bss|ibss]\n"
    147 	    "\t%s iface -k\n"
    148 	    "\t%s iface -r\n", getprogname(), getprogname(), getprogname(),
    149 	    getprogname());
    150 
    151 	exit(EX_USAGE);
    152 }
    153 
    154 static int
    155 do_req(const char *iface, unsigned long req, void *data)
    156 {
    157 	int s;
    158 	struct ifreq ifr;
    159 	int error, serrno;
    160 
    161 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
    162 		err(EX_OSERR, "Can't create socket");
    163 
    164 	(void)memset(&ifr, 0, sizeof(ifr));
    165 	(void)strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
    166 	ifr.ifr_data = data;
    167 	error = ioctl(s, req, &ifr);
    168 	serrno = errno;
    169 	(void)close(s);
    170 	errno = serrno;
    171 
    172 	return error;
    173 }
    174 
    175 static void
    176 mmap_file(const char *filename, void **addr, size_t *len)
    177 {
    178 	int fd;
    179 	struct stat st;
    180 
    181 	if ((fd = open(filename, O_RDONLY)) == -1)
    182 		err(EX_OSERR, "%s", filename);
    183 
    184 	if (fstat(fd, &st) == -1)
    185 		err(EX_OSERR, "Unable to stat %s", filename);
    186 
    187 	*len = (size_t)st.st_size;
    188 
    189 	*addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
    190 	if (*addr == MAP_FAILED)
    191 		err(EX_OSERR, "Can't map %s into memory", filename);
    192 
    193 	*(char **)addr += sizeof(struct header);
    194 	*len -= sizeof(struct header);
    195 
    196 	(void)close(fd);
    197 }
    198 
    199 static void
    200 load_firmware(const char *iface, const char *path, const char *mode)
    201 {
    202 	char filename[FILENAME_MAX];
    203 	struct firmware fw;
    204 
    205 	(void)snprintf(filename, sizeof(filename), "%s/iwi-boot.fw", path);
    206 	mmap_file(filename, &fw.boot, &fw.boot_size);
    207 
    208 	(void)snprintf(filename, sizeof(filename), "%s/iwi-ucode-%s.fw", path,
    209 	    mode);
    210 	mmap_file(filename, &fw.ucode, &fw.ucode_size);
    211 
    212 	(void)snprintf(filename, sizeof(filename), "%s/iwi-%s.fw", path, mode);
    213 	mmap_file(filename, &fw.main, &fw.main_size);
    214 
    215 	if (do_req(iface, SIOCSLOADFW, &fw) == -1)
    216 		err(EX_OSERR, "Can't load firmware to driver");
    217 }
    218 
    219 static void
    220 kill_firmware(const char *iface)
    221 {
    222 	if (do_req(iface, SIOCSKILLFW, NULL) == -1)
    223 		err(EX_OSERR, "Can't kill firmware");
    224 }
    225 
    226 static void
    227 get_radio_state(const char *iface)
    228 {
    229 	int radio;
    230 
    231 	if (do_req(iface, SIOCGRADIO, &radio) == -1)
    232 		err(EX_OSERR, "Can't read radio");
    233 
    234 	(void)printf("Radio is %s\n", radio ? "ON" : "OFF");
    235 }
    236 
    237 struct statistic {
    238 	int 		index;
    239 	const char	*desc;
    240 };
    241 
    242 static const struct statistic tbl[] = {
    243 	{  1, "Current transmission rate" },
    244 	{  2, "Fragmentation threshold" },
    245 	{  3, "RTS threshold" },
    246 	{  4, "Number of frames submitted for transfer" },
    247 	{  5, "Number of frames transmitted" },
    248 	{  6, "Number of unicast frames transmitted" },
    249 	{  7, "Number of unicast 802.11b frames transmitted at 1Mb/s" },
    250 	{  8, "Number of unicast 802.11b frames transmitted at 2Mb/s" },
    251 	{  9, "Number of unicast 802.11b frames transmitted at 5.5Mb/s" },
    252 	{ 10, "Number of unicast 802.11b frames transmitted at 11Mb/s" },
    253 
    254 	{ 19, "Number of unicast 802.11g frames transmitted at 1Mb/s" },
    255 	{ 20, "Number of unicast 802.11g frames transmitted at 2Mb/s" },
    256 	{ 21, "Number of unicast 802.11g frames transmitted at 5.5Mb/s" },
    257 	{ 22, "Number of unicast 802.11g frames transmitted at 6Mb/s" },
    258 	{ 23, "Number of unicast 802.11g frames transmitted at 9Mb/s" },
    259 	{ 24, "Number of unicast 802.11g frames transmitted at 11Mb/s" },
    260 	{ 25, "Number of unicast 802.11g frames transmitted at 12Mb/s" },
    261 	{ 26, "Number of unicast 802.11g frames transmitted at 18Mb/s" },
    262 	{ 27, "Number of unicast 802.11g frames transmitted at 24Mb/s" },
    263 	{ 28, "Number of unicast 802.11g frames transmitted at 36Mb/s" },
    264 	{ 29, "Number of unicast 802.11g frames transmitted at 48Mb/s" },
    265 	{ 30, "Number of unicast 802.11g frames transmitted at 54Mb/s" },
    266 	{ 31, "Number of multicast frames transmitted" },
    267 	{ 32, "Number of multicast 802.11b frames transmitted at 1Mb/s" },
    268 	{ 33, "Number of multicast 802.11b frames transmitted at 2Mb/s" },
    269 	{ 34, "Number of multicast 802.11b frames transmitted at 5.5Mb/s" },
    270 	{ 35, "Number of multicast 802.11b frames transmitted at 11Mb/s" },
    271 
    272 	{ 44, "Number of multicast 802.11g frames transmitted at 1Mb/s" },
    273 	{ 45, "Number of multicast 802.11g frames transmitted at 2Mb/s" },
    274 	{ 46, "Number of multicast 802.11g frames transmitted at 5.5Mb/s" },
    275 	{ 47, "Number of multicast 802.11g frames transmitted at 6Mb/s" },
    276 	{ 48, "Number of multicast 802.11g frames transmitted at 9Mb/s" },
    277 	{ 49, "Number of multicast 802.11g frames transmitted at 11Mb/s" },
    278 	{ 50, "Number of multicast 802.11g frames transmitted at 12Mb/s" },
    279 	{ 51, "Number of multicast 802.11g frames transmitted at 18Mb/s" },
    280 	{ 52, "Number of multicast 802.11g frames transmitted at 24Mb/s" },
    281 	{ 53, "Number of multicast 802.11g frames transmitted at 36Mb/s" },
    282 	{ 54, "Number of multicast 802.11g frames transmitted at 48Mb/s" },
    283 	{ 55, "Number of multicast 802.11g frames transmitted at 54Mb/s" },
    284 	{ 56, "Number of transmission retries" },
    285 	{ 57, "Number of transmission failures" },
    286 	{ 58, "Number of frames with a bad CRC received" },
    287 
    288 	{ 61, "Number of full scans" },
    289 	{ 62, "Number of partial scans" },
    290 
    291 	{ 64, "Number of bytes transmitted" },
    292 	{ 65, "Current RSSI" },
    293 	{ 66, "Number of beacons received" },
    294 	{ 67, "Number of beacons missed" },
    295 
    296 	{ 0, NULL }
    297 };
    298 
    299 static void
    300 get_statistics(const char *iface)
    301 {
    302 	static u_int32_t stats[256];
    303 	const struct statistic *st;
    304 
    305 	if (do_req(iface, SIOCGTABLE0, stats) == -1)
    306 		err(EX_OSERR, "Can't read statistics");
    307 
    308 	for (st = tbl; st->index != 0; st++)
    309 		(void)printf("%-60s[%u]\n", st->desc, stats[st->index]);
    310 }
    311