2 * Copyright (c) 1999, 2000
3 * Dr. Duncan McLennan Barclay, dmlb@ragnet.demon.co.uk. All Rights Reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY DUNCAN BARCLAY AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL DUNCAN BARCLAY OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/usr.sbin/raycontrol/raycontrol.c,v 1.7 2004/04/04 19:38:08 charnier Exp $
33 * $DragonFly: src/usr.sbin/raycontrol/raycontrol.c,v 1.5 2004/07/27 15:39:46 joerg Exp $
36 #include <sys/param.h>
37 #include <sys/socket.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
42 #include <net/ethernet.h>
43 #include <netproto/802_11/ieee80211.h>
44 #include <netproto/802_11/ieee80211_ioctl.h>
46 #include <dev/netif/ray/if_rayreg.h>
47 #include <dev/netif/ray/if_raymib.h>
56 static char * ray_printhex(u_int8_t
*d
, const char *s
, int len
);
57 static void ray_getval(const char *iface
, struct ray_param_req
*rreq
);
58 static void ray_getstats(const char *iface
, struct ray_stats_req
*sreq
);
59 static int ray_version(const char *iface
);
60 static void ray_dumpstats(const char *iface
);
61 static void ray_dumpinfo(const char *iface
);
62 static void ray_setstr(const char *iface
, u_int8_t mib
, char *s
);
63 static void ray_setword(const char *iface
, u_int8_t mib
, u_int16_t v
);
64 static void ray_setval(const char *iface
, struct ray_param_req
*rreq
);
65 static void usage(const char *p
);
67 static const char *mib_strings
[] = RAY_MIB_STRINGS
;
68 static const char *mib_help_strings
[] = RAY_MIB_HELP_STRINGS
;
69 static int mib_info
[RAY_MIB_MAX
+1][3] = RAY_MIB_INFO
;
72 ray_printhex(u_int8_t
*d
, const char *s
, int len
)
74 static char buf
[3*256];
78 if (2 * len
+ strlen(s
) * (len
- 1) > sizeof(buf
) - 1)
79 errx(1, "byte string too long");
81 sprintf(buf
, "%02x", *d
);
82 for (p
= buf
+ 2, i
= 1; i
< len
; i
++)
83 p
+= sprintf(p
, "%s%02x", s
, *(d
+i
));
89 ray_getval(const char *iface
, struct ray_param_req
*rreq
)
94 bzero((char *)&ifr
, sizeof(ifr
));
96 strlcpy(ifr
.ifr_name
, iface
, IFNAMSIZ
);
97 ifr
.ifr_data
= (caddr_t
)rreq
;
99 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
104 if (ioctl(s
, SIOCGRAYPARAM
, &ifr
) == -1)
105 warn("SIOCGRAYPARAM failed with failcode 0x%02x",
112 ray_getsiglev(const char *iface
, struct ray_siglev
*siglev
)
117 bzero((char *)&ifr
, sizeof(ifr
));
119 strlcpy(ifr
.ifr_name
, iface
, sizeof(ifr
.ifr_name
));
120 ifr
.ifr_data
= (caddr_t
)siglev
;
122 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
127 if (ioctl(s
, SIOCGRAYSIGLEV
, &ifr
) == -1)
128 err(1, "SIOCGRAYSIGLEV failed");
134 ray_getstats(const char *iface
, struct ray_stats_req
*sreq
)
139 bzero((char *)&ifr
, sizeof(ifr
));
141 strlcpy(ifr
.ifr_name
, iface
, sizeof(ifr
.ifr_name
));
142 ifr
.ifr_data
= (caddr_t
)sreq
;
144 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
149 if (ioctl(s
, SIOCGRAYSTATS
, &ifr
) == -1)
150 err(1, "SIOCGRAYSTATS failed");
156 ray_version(const char *iface
)
158 struct ray_param_req rreq
;
161 errx(1, "must specify interface name");
163 bzero((char *)&rreq
, sizeof(rreq
));
164 rreq
.r_paramid
= RAY_MIB_VERSION
;
165 ray_getval(iface
, &rreq
);
166 return(*rreq
.r_data
);
170 ray_dumpinfo(const char *iface
)
172 struct ray_param_req rreq
;
173 u_int8_t mib
, version
;
176 errx(1, "must specify interface name");
178 bzero((char *)&rreq
, sizeof(rreq
));
180 version
= ray_version(iface
);
181 printf("%-26s\t", mib_strings
[RAY_MIB_VERSION
]);
182 printf("%d\n", 3+version
);
184 for (mib
= RAY_MIB_NET_TYPE
; mib
<= RAY_MIB_MAX
; mib
++) {
186 if ((mib_info
[mib
][0] & version
) == 0)
188 if (mib
== RAY_MIB_VERSION
)
191 rreq
.r_paramid
= mib
;
192 ray_getval(iface
, &rreq
);
193 printf("%-26s\t", mib_strings
[mib
]);
194 switch (rreq
.r_len
) {
197 printf("0x%02x%02x", *rreq
.r_data
, *(rreq
.r_data
+1));
202 ray_printhex(rreq
.r_data
, ":", rreq
.r_len
));
205 case IEEE80211_NWID_LEN
:
206 printf("%-32s", (char *)rreq
.r_data
);
212 printf("0x%02x", *rreq
.r_data
);
215 printf("\t%s\n", mib_help_strings
[mib
]);
220 ray_dumpsiglev(const char *iface
)
222 struct ray_siglev siglevs
[RAY_NSIGLEVRECS
];
226 errx(1, "must specify interface name");
228 bzero((char *)siglevs
, sizeof(siglevs
));
230 ray_getsiglev(iface
, siglevs
);
232 for (i
= 0; i
< RAY_NSIGLEVRECS
; i
++) {
233 printf("Slot %d: %s", i
,
234 ray_printhex(siglevs
[i
].rsl_host
, ":", ETHER_ADDR_LEN
));
236 ray_printhex(siglevs
[i
].rsl_siglevs
, ",", RAY_NSIGLEV
));
238 ray_printhex(siglevs
[i
].rsl_antennas
, "", RAY_NANTENNA
));
244 ray_dumpstats(const char *iface
)
246 struct ray_stats_req sreq
;
249 errx(1, "must specify interface name");
251 bzero((char *)&sreq
, sizeof(sreq
));
253 ray_getstats(iface
, &sreq
);
255 printf("Receiver overflows %lu\n",
256 (unsigned long int)sreq
.rxoverflow
);
257 printf("Receiver checksum errors %lu\n",
258 (unsigned long int)sreq
.rxcksum
);
259 printf("Header checksum errors %lu\n",
260 (unsigned long int)sreq
.rxhcksum
);
261 printf("Clear channel noise level %u\n", sreq
.rxnoise
);
265 ray_setval(const char *iface
, struct ray_param_req
*rreq
)
270 bzero((char *)&ifr
, sizeof(ifr
));
272 strlcpy(ifr
.ifr_name
, iface
, sizeof(ifr
.ifr_name
));
273 ifr
.ifr_data
= (caddr_t
)rreq
;
275 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
280 if (ioctl(s
, SIOCSRAYPARAM
, &ifr
) == -1) {
281 err(1, "SIOCSRAYPARAM failed with failcode 0x%02x",
289 ray_setword(const char *iface
, u_int8_t mib
, u_int16_t v
)
291 struct ray_param_req rreq
;
294 errx(1, "must specify interface name");
296 bzero((char *)&rreq
, sizeof(rreq
));
298 rreq
.r_paramid
= mib
;
299 rreq
.r_len
= RAY_MIB_SIZE(mib_info
, mib
, ray_version(iface
));
300 switch (rreq
.r_len
) {
303 *rreq
.r_data
= (u_int8_t
)(v
& 0xff);
307 *rreq
.r_data
= (u_int8_t
)((v
& 0xff00) >> 8);
308 *(rreq
.r_data
+1) = (u_int8_t
)(v
& 0xff);
315 ray_setval(iface
, &rreq
);
319 ray_setstr(const char *iface
, u_int8_t mib
, char *s
)
321 struct ray_param_req rreq
;
325 errx(1, "must specify interface name");
327 errx(1, "must specify string");
329 if (len
> SSIZE_MAX
||
330 (ssize_t
)len
> RAY_MIB_SIZE(mib_info
, mib
, ray_version(iface
)))
331 errx(1, "string too long");
333 bzero(&rreq
, sizeof(rreq
));
335 rreq
.r_paramid
= mib
;
336 rreq
.r_len
= RAY_MIB_SIZE(mib_info
, mib
, ray_version(iface
));
337 bcopy(s
, rreq
.r_data
, strlen(s
));
339 ray_setval(iface
, &rreq
);
345 fprintf(stderr
, "usage: %s -i iface\n", p
);
346 fprintf(stderr
, "\t%s -i iface -o\n", p
);
347 fprintf(stderr
, "\t%s -i iface -t tx rate\n", p
);
348 fprintf(stderr
, "\t%s -i iface -n network name\n", p
);
349 fprintf(stderr
, "\t%s -i iface -p port type\n", p
);
350 fprintf(stderr
, "\t%s -i iface -m mac address\n", p
);
351 fprintf(stderr
, "\t%s -i iface -d max data length\n", p
);
352 fprintf(stderr
, "\t%s -i iface -r RTS threshold\n", p
);
353 fprintf(stderr
, "\t%s -i iface -f hopset\n", p
);
354 fprintf(stderr
, "\t%s -i iface -P 0|1\n", p
);
355 fprintf(stderr
, "\t%s -i iface -S max sleep duration\n", p
);
356 fprintf(stderr
, "\t%s -i iface -C print signal cache\n", p
);
362 main(int argc
, char *argv
[])
371 /* Get the interface name */
373 ch
= getopt(argc
, argv
, "i:");
377 if (argc
> 1 && *argv
[1] != '-') {
388 while ((ch
= getopt(argc
, argv
, "hoCi:d:f:n:p:r:t:W:")) != -1) {
398 (val
!= -1)) || (val
> RAY_MIB_FRAG_THRESH_MAXIMUM
))
402 ray_setword(iface
, RAY_MIB_FRAG_THRESH
, val
);
408 if ((val
< RAY_MIB_COUNTRY_CODE_MIMIMUM
) ||
409 (val
> RAY_MIB_COUNTRY_CODE_MAXIMUM
))
411 ray_setword(iface
, RAY_MIB_COUNTRY_CODE
, val
);
416 ray_setstr(iface
, RAY_MIB_SSID
, optarg
);
421 ray_dumpstats(iface
);
427 if ((val
< 0) || (val
> 1))
429 ray_setword(iface
, RAY_MIB_NET_TYPE
, val
);
436 if ((val
< -1) || (val
> RAY_MIB_RTS_THRESH_MAXIMUM
))
440 ray_setword(iface
, RAY_MIB_RTS_THRESH
, val
);
446 if ((val
< RAY_MIB_BASIC_RATE_SET_MINIMUM
) ||
447 (val
> RAY_MIB_BASIC_RATE_SET_MAXIMUM
))
449 ray_setword(iface
, RAY_MIB_BASIC_RATE_SET
, val
);
454 ray_dumpsiglev(iface
);
460 char *stringp
, **ap
, *av
[5];
465 *ap
= strsep(&stringp
, ":");
469 *ap
= strsep(&stringp
, ":");
471 sscanf(av
[1], "%x", &val
);
472 printf("mib %d, val 0x%02x\n", mib
, val
);
473 ray_setword(iface
, mib
, val
);