- Test m_pkthdr.fw_flags against DUMMYNET_MBUF_TAGGED before trying to locate
[dragonfly/netmp.git] / usr.sbin / iwicontrol / iwicontrol.c
blobc656968f4fe346e2ca70e5cba619805c4280a6e1
1 /*
2 * Copyright (c) 2004, 2005
3 * Damien Bergamini <damien.bergamini@free.fr>.
4 * Copyright (c) 2004, 2005
5 * Andrew Atrens <atrens@nortelnetworks.com>.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
14 * disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
31 * $DragonFly: src/usr.sbin/iwicontrol/iwicontrol.c,v 1.3 2006/05/18 13:51:45 sephe Exp $
34 #include <sys/cdefs.h>
36 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <sys/mman.h>
39 #include <sys/socket.h>
40 #include <sys/stat.h>
41 #include <sys/sysctl.h>
43 #include <net/if.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <sysexits.h>
52 #include <unistd.h>
54 #define SIOCSLOADFW _IOW('i', 137, struct ifreq)
55 #define SIOCSKILLFW _IOW('i', 138, struct ifreq)
57 struct firmware {
58 char *boot;
59 int boot_size;
60 char *ucode;
61 int ucode_size;
62 char *main;
63 int main_size;
66 struct header {
67 u_int32_t version;
68 u_int32_t mode;
69 } __attribute__((__packed__));
71 static void usage(void);
72 static int do_req(const char *, unsigned long, void *);
73 static void mmap_file(const char *, char **, size_t *);
74 static void load_firmware(const char *, const char *, const char *);
75 static void kill_firmware(const char *);
76 static void get_radio_state(const char *);
77 static void get_statistics(const char *);
78 static void dump_debug(const char *);
80 int
81 main(int argc, char **argv)
83 int ch;
84 const char *iface = NULL, *mode = "bss", *path = NULL;
85 int noflag = 1, kflag = 0, rflag = 0, dflag = 0;
87 if (argc > 1 && argv[1][0] != '-') {
88 iface = argv[1];
89 optind++;
92 while ((ch = getopt(argc, argv, "d:i:km:rD")) != -1) {
93 if (ch != 'i')
94 noflag = 0;
96 switch (ch) {
97 case 'd':
98 path = optarg;
99 break;
101 case 'i':
102 iface = optarg;
103 break;
105 case 'k':
106 kflag = 1;
107 break;
109 case 'm':
110 mode = optarg;
111 break;
113 case 'r':
114 rflag = 1;
115 break;
117 case 'D':
118 dflag = 1;
119 break;
121 default:
122 usage();
126 if (iface == NULL)
127 usage();
129 if (kflag && (path != NULL || rflag))
130 usage();
132 if (kflag)
133 kill_firmware(iface);
135 if (path != NULL)
136 load_firmware(iface, path, mode);
138 if (rflag)
139 get_radio_state(iface);
141 if (dflag)
142 dump_debug(iface);
144 if (noflag)
145 get_statistics(iface);
147 return EX_OK;
150 extern char *__progname;
152 static void
153 usage(void)
155 fprintf(stderr, "usage: %s iface\n"
156 "\t%s iface -d path [-m bss|ibss]\n"
157 "\t%s iface -k\n"
158 "\t%s iface -r\n", __progname, __progname, __progname,
159 __progname);
161 exit(EX_USAGE);
164 static int
165 do_req(const char *iface, unsigned long req, void *data)
167 int s;
168 struct ifreq ifr;
169 int error;
171 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
172 err(EX_OSERR, "Can't create socket");
174 memset(&ifr, 0, sizeof ifr);
175 strncpy(ifr.ifr_name, iface, sizeof ifr.ifr_name);
176 ifr.ifr_data = data;
177 error = ioctl(s, req, &ifr);
179 close(s);
181 return error;
184 static void
185 mmap_file(const char *filename, char **addr, size_t *len)
187 int fd;
188 struct stat st;
190 if ((fd = open(filename, O_RDONLY)) == -1)
191 err(EX_OSERR, "%s", filename);
193 if (fstat(fd, &st) == -1)
194 err(EX_OSERR, "Unable to stat %s", filename);
196 *len = st.st_size;
198 if ((*addr = mmap(NULL, st.st_size, PROT_READ, 0, fd, 0)) == NULL)
199 err(EX_OSERR, "Can't map %s into memory", filename);
201 *addr += sizeof (struct header);
202 *len -= sizeof (struct header);
204 close(fd);
207 static void
208 load_firmware(const char *iface, const char *path, const char *mode)
210 char filename[FILENAME_MAX];
211 struct firmware fw;
213 snprintf(filename, sizeof filename, "%s/iwi-boot.fw", path);
214 mmap_file(filename, &fw.boot, &fw.boot_size);
216 snprintf(filename, sizeof filename, "%s/iwi-ucode-%s.fw", path,
217 mode);
218 mmap_file(filename, &fw.ucode, &fw.ucode_size);
220 snprintf(filename, sizeof filename, "%s/iwi-%s.fw", path, mode);
221 mmap_file(filename, &fw.main, &fw.main_size);
223 if (do_req(iface, SIOCSLOADFW, &fw) == -1)
224 err(EX_OSERR, "Can't load firmware to driver");
227 static void
228 kill_firmware(const char *iface)
230 if (do_req(iface, SIOCSKILLFW, NULL) == -1)
231 err(EX_OSERR, "Can't kill firmware");
234 static void
235 dump_debug(const char *iface)
237 int dump = 1, len = sizeof dump;
238 char oid_name[128];
239 snprintf(oid_name, sizeof oid_name, "hw.%s.firmware_logs", iface );
240 if (sysctlbyname(oid_name, 0, 0, &dump, len) == -1)
241 err(EX_OSERR, "Can't dump firmware logs");
242 printf("All firmware logs dumped.\n");
246 static void
247 get_radio_state(const char *iface)
249 int radio, len;
250 char oid_name[128];
252 snprintf(oid_name, sizeof oid_name, "hw.%s.radio", iface );
253 len = sizeof radio;
254 if (sysctlbyname(oid_name, &radio, &len, NULL, 0) == -1)
255 err(EX_OSERR, "Can't get radio transmitter state");
257 printf("Radio is %s\n", radio ? "ON" : "OFF");
260 struct statistic {
261 int index;
262 const char *desc;
265 static const struct statistic tbl[] = {
266 { 1, "Current transmission rate" },
267 { 2, "Fragmentation threshold" },
268 { 3, "RTS threshold" },
269 { 4, "Number of frames submitted for transfer" },
270 { 5, "Number of frames transmitted" },
271 { 6, "Number of unicast frames transmitted" },
272 { 7, "Number of unicast 802.11b frames transmitted at 1Mb/s" },
273 { 8, "Number of unicast 802.11b frames transmitted at 2Mb/s" },
274 { 9, "Number of unicast 802.11b frames transmitted at 5.5Mb/s" },
275 { 10, "Number of unicast 802.11b frames transmitted at 11Mb/s" },
277 { 19, "Number of unicast 802.11g frames transmitted at 1Mb/s" },
278 { 20, "Number of unicast 802.11g frames transmitted at 2Mb/s" },
279 { 21, "Number of unicast 802.11g frames transmitted at 5.5Mb/s" },
280 { 22, "Number of unicast 802.11g frames transmitted at 6Mb/s" },
281 { 23, "Number of unicast 802.11g frames transmitted at 9Mb/s" },
282 { 24, "Number of unicast 802.11g frames transmitted at 11Mb/s" },
283 { 25, "Number of unicast 802.11g frames transmitted at 12Mb/s" },
284 { 26, "Number of unicast 802.11g frames transmitted at 18Mb/s" },
285 { 27, "Number of unicast 802.11g frames transmitted at 24Mb/s" },
286 { 28, "Number of unicast 802.11g frames transmitted at 36Mb/s" },
287 { 29, "Number of unicast 802.11g frames transmitted at 48Mb/s" },
288 { 30, "Number of unicast 802.11g frames transmitted at 54Mb/s" },
289 { 31, "Number of multicast frames transmitted" },
290 { 32, "Number of multicast 802.11b frames transmitted at 1Mb/s" },
291 { 33, "Number of multicast 802.11b frames transmitted at 2Mb/s" },
292 { 34, "Number of multicast 802.11b frames transmitted at 5.5Mb/s" },
293 { 35, "Number of multicast 802.11b frames transmitted at 11Mb/s" },
295 { 44, "Number of multicast 802.11g frames transmitted at 1Mb/s" },
296 { 45, "Number of multicast 802.11g frames transmitted at 2Mb/s" },
297 { 46, "Number of multicast 802.11g frames transmitted at 5.5Mb/s" },
298 { 47, "Number of multicast 802.11g frames transmitted at 6Mb/s" },
299 { 48, "Number of multicast 802.11g frames transmitted at 9Mb/s" },
300 { 49, "Number of multicast 802.11g frames transmitted at 11Mb/s" },
301 { 50, "Number of multicast 802.11g frames transmitted at 12Mb/s" },
302 { 51, "Number of multicast 802.11g frames transmitted at 18Mb/s" },
303 { 52, "Number of multicast 802.11g frames transmitted at 24Mb/s" },
304 { 53, "Number of multicast 802.11g frames transmitted at 36Mb/s" },
305 { 54, "Number of multicast 802.11g frames transmitted at 48Mb/s" },
306 { 55, "Number of multicast 802.11g frames transmitted at 54Mb/s" },
307 { 56, "Number of transmission retries" },
308 { 57, "Number of transmission failures" },
309 { 58, "Number of frames with a bad CRC received" },
311 { 61, "Number of full scans" },
312 { 62, "Number of partial scans" },
314 { 64, "Number of bytes transmitted" },
315 { 65, "Current RSSI" },
316 { 66, "Number of beacons received" },
317 { 67, "Number of beacons missed" },
319 { 0, NULL }
322 static void
323 get_statistics(const char *iface)
325 static u_int32_t stats[256];
326 char oid_name[128];
327 const struct statistic *stt;
328 int len;
330 snprintf(oid_name, sizeof oid_name, "hw.%s.stats", iface );
331 len = sizeof stats;
332 if (sysctlbyname(oid_name, stats, &len, NULL, 0) == -1)
333 err(EX_OSERR, "Can't retrieve statistics");
335 for (stt = tbl; stt->index != 0; stt++)
336 printf("%-60s[%u]\n", stt->desc, stats[stt->index]);