iwictl.c revision 1.1 1 /* $Id: iwictl.c,v 1.1 2005/01/11 18:28:38 skrll 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("$Id: iwictl.c,v 1.1 2005/01/11 18:28:38 skrll 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 extern char *optarg;
70 extern int optind;
71
72 static void usage(void);
73 static int do_req(char *, unsigned long, void *);
74 static void mmap_file(char *, void **, size_t *);
75 static void load_firmware(char *, char *, char *);
76 static void kill_firmware(char *);
77 static void get_radio_state(char *);
78 static void get_statistics(char *);
79
80 int
81 main(int argc, char **argv)
82 {
83 int ch;
84 char *iface = NULL, *mode = "bss", *path = NULL;
85 int noflag = 1, kflag = 0, rflag = 0;
86
87 if (argc > 1 && argv[1][0] != '-') {
88 iface = argv[1];
89 optind++;
90 }
91
92 while ((ch = getopt(argc, argv, "d:i:km:r")) != -1) {
93 if (ch != 'i')
94 noflag = 0;
95
96 switch (ch) {
97 case 'd':
98 path = optarg;
99 break;
100
101 case 'i':
102 iface = optarg;
103 break;
104
105 case 'k':
106 kflag = 1;
107 break;
108
109 case 'm':
110 mode = optarg;
111 break;
112
113 case 'r':
114 rflag = 1;
115 break;
116
117 default:
118 usage();
119 }
120 }
121
122 if (iface == NULL)
123 usage();
124
125 if (kflag && (path != NULL || rflag))
126 usage();
127
128 if (kflag)
129 kill_firmware(iface);
130
131 if (path != NULL)
132 load_firmware(iface, path, mode);
133
134 if (rflag)
135 get_radio_state(iface);
136
137 if (noflag)
138 get_statistics(iface);
139
140 return EX_OK;
141 }
142
143 static void
144 usage(void)
145 {
146 extern char *__progname;
147
148 (void)fprintf(stderr, "usage: %s iface\n"
149 "\t%s iface -d path [-m bss|ibss]\n"
150 "\t%s iface -k\n"
151 "\t%s iface -r\n", __progname, __progname, __progname,
152 __progname);
153
154 exit(EX_USAGE);
155 }
156
157 static int
158 do_req(char *iface, unsigned long req, void *data)
159 {
160 int s;
161 struct ifreq ifr;
162 int error;
163
164 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
165 err(EX_OSERR, "Can't create socket");
166
167 (void)memset(&ifr, 0, sizeof ifr);
168 (void)strncpy(ifr.ifr_name, iface, sizeof ifr.ifr_name);
169 ifr.ifr_data = data;
170 error = ioctl(s, req, &ifr);
171
172 (void)close(s);
173
174 return error;
175 }
176
177 static void
178 mmap_file(char *filename, void **addr, size_t *len)
179 {
180 int fd;
181 struct stat st;
182
183 if ((fd = open(filename, O_RDONLY)) == -1)
184 err(EX_OSERR, "%s", filename);
185
186 if (fstat(fd, &st) == -1)
187 err(EX_OSERR, "Unable to stat %s", filename);
188
189 *len = st.st_size;
190
191 if ((*addr = mmap(NULL, st.st_size, PROT_READ, 0, fd, 0)) == NULL)
192 err(EX_OSERR, "Can't map %s into memory", filename);
193
194 *(char **)addr += sizeof (struct header);
195 *len -= sizeof (struct header);
196
197 (void)close(fd);
198 }
199
200 static void
201 load_firmware(char *iface, char *path, char *mode)
202 {
203 char filename[FILENAME_MAX];
204 struct firmware fw;
205
206 (void)snprintf(filename, sizeof filename, "%s/iwi-boot.fw", path);
207 mmap_file(filename, &fw.boot, &fw.boot_size);
208
209 (void)snprintf(filename, sizeof filename, "%s/iwi-ucode-%s.fw", path,
210 mode);
211 mmap_file(filename, &fw.ucode, &fw.ucode_size);
212
213 (void)snprintf(filename, sizeof filename, "%s/iwi-%s.fw", path, mode);
214 mmap_file(filename, &fw.main, &fw.main_size);
215
216 if (do_req(iface, SIOCSLOADFW, &fw) == -1)
217 err(EX_OSERR, "Can't load firmware to driver");
218 }
219
220 static void
221 kill_firmware(char *iface)
222 {
223 if (do_req(iface, SIOCSKILLFW, NULL) == -1)
224 err(EX_OSERR, "Can't kill firmware");
225 }
226
227 static void
228 get_radio_state(char *iface)
229 {
230 int radio;
231
232 if (do_req(iface, SIOCGRADIO, &radio) == -1)
233 err(EX_OSERR, "Can't read radio");
234
235 (void)printf("Radio is %s\n", radio ? "ON" : "OFF");
236 }
237
238 struct statistic {
239 int index;
240 const char *desc;
241 };
242
243 static const struct statistic tbl[] = {
244 { 1, "Current transmission rate" },
245 { 2, "Fragmentation threshold" },
246 { 3, "RTS threshold" },
247 { 4, "Number of frames submitted for transfer" },
248 { 5, "Number of frames transmitted" },
249 { 6, "Number of unicast frames transmitted" },
250 { 7, "Number of unicast 802.11b frames transmitted at 1Mb/s" },
251 { 8, "Number of unicast 802.11b frames transmitted at 2Mb/s" },
252 { 9, "Number of unicast 802.11b frames transmitted at 5.5Mb/s" },
253 { 10, "Number of unicast 802.11b frames transmitted at 11Mb/s" },
254
255 { 19, "Number of unicast 802.11g frames transmitted at 1Mb/s" },
256 { 20, "Number of unicast 802.11g frames transmitted at 2Mb/s" },
257 { 21, "Number of unicast 802.11g frames transmitted at 5.5Mb/s" },
258 { 22, "Number of unicast 802.11g frames transmitted at 6Mb/s" },
259 { 23, "Number of unicast 802.11g frames transmitted at 9Mb/s" },
260 { 24, "Number of unicast 802.11g frames transmitted at 11Mb/s" },
261 { 25, "Number of unicast 802.11g frames transmitted at 12Mb/s" },
262 { 26, "Number of unicast 802.11g frames transmitted at 18Mb/s" },
263 { 27, "Number of unicast 802.11g frames transmitted at 24Mb/s" },
264 { 28, "Number of unicast 802.11g frames transmitted at 36Mb/s" },
265 { 29, "Number of unicast 802.11g frames transmitted at 48Mb/s" },
266 { 30, "Number of unicast 802.11g frames transmitted at 54Mb/s" },
267 { 31, "Number of multicast frames transmitted" },
268 { 32, "Number of multicast 802.11b frames transmitted at 1Mb/s" },
269 { 33, "Number of multicast 802.11b frames transmitted at 2Mb/s" },
270 { 34, "Number of multicast 802.11b frames transmitted at 5.5Mb/s" },
271 { 35, "Number of multicast 802.11b frames transmitted at 11Mb/s" },
272
273 { 44, "Number of multicast 802.11g frames transmitted at 1Mb/s" },
274 { 45, "Number of multicast 802.11g frames transmitted at 2Mb/s" },
275 { 46, "Number of multicast 802.11g frames transmitted at 5.5Mb/s" },
276 { 47, "Number of multicast 802.11g frames transmitted at 6Mb/s" },
277 { 48, "Number of multicast 802.11g frames transmitted at 9Mb/s" },
278 { 49, "Number of multicast 802.11g frames transmitted at 11Mb/s" },
279 { 50, "Number of multicast 802.11g frames transmitted at 12Mb/s" },
280 { 51, "Number of multicast 802.11g frames transmitted at 18Mb/s" },
281 { 52, "Number of multicast 802.11g frames transmitted at 24Mb/s" },
282 { 53, "Number of multicast 802.11g frames transmitted at 36Mb/s" },
283 { 54, "Number of multicast 802.11g frames transmitted at 48Mb/s" },
284 { 55, "Number of multicast 802.11g frames transmitted at 54Mb/s" },
285 { 56, "Number of transmission retries" },
286 { 57, "Number of transmission failures" },
287 { 58, "Number of frames with a bad CRC received" },
288
289 { 61, "Number of full scans" },
290 { 62, "Number of partial scans" },
291
292 { 64, "Number of bytes transmitted" },
293 { 65, "Current RSSI" },
294 { 66, "Number of beacons received" },
295 { 67, "Number of beacons missed" },
296
297 { 0, NULL }
298 };
299
300 static void
301 get_statistics(char *iface)
302 {
303 static u_int32_t stats[256];
304 const struct statistic *stat;
305
306 if (do_req(iface, SIOCGTABLE0, stats) == -1)
307 err(EX_OSERR, "Can't read statistics");
308
309 for (stat = tbl; stat->index != 0; stat++)
310 (void)printf("%-60s[%u]\n", stat->desc, stats[stat->index]);
311 }
312
313