Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / raycontrol / raycontrol.c
1 /*
2  * Copyright (c) 1999, 2000
3  * Dr. Duncan McLennan Barclay, dmlb@ragnet.demon.co.uk.  All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD: src/usr.sbin/raycontrol/raycontrol.c,v 1.1.2.3 2002/12/16 08:39:41 roam Exp $
33  */
34
35 #include <sys/types.h>
36 #include <sys/cdefs.h>
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41
42 #include <net/if.h>
43 #include <net/ethernet.h>
44 #include <net/if_ieee80211.h>
45
46 #include <dev/ray/if_rayreg.h>
47 #include <dev/ray/if_raymib.h>
48
49 #include <stdio.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 #include <errno.h>
54 #include <err.h>
55
56 static char *   ray_printhex    (u_int8_t *d, char *s, int len);
57 static void     ray_getval      (char *iface, struct ray_param_req *rreq);
58 static void     ray_getstats    (char *iface, struct ray_stats_req *sreq);
59 static int      ray_version     (char *iface);
60 static void     ray_dumpstats   (char *iface);
61 static void     ray_dumpinfo    (char *iface);
62 static void     ray_setstr      (char *iface, u_int8_t mib, char *s);
63 static void     ray_setword     (char *iface, u_int8_t mib, u_int16_t v);
64 static void     ray_setval      (char *iface, struct ray_param_req *rreq);
65 static void     usage           (char *p);
66
67 static char     *mib_strings[] = RAY_MIB_STRINGS;
68 static char     *mib_help_strings[] = RAY_MIB_HELP_STRINGS;
69 static int      mib_info[RAY_MIB_MAX+1][3] = RAY_MIB_INFO;
70
71 static char *
72 ray_printhex(u_int8_t *d, char *s, int len)
73 {
74         static char buf[3*256];
75         char *p;
76         int i;
77
78         if (2 * len + strlen(s) * (len - 1) > sizeof(buf) - 1)
79                 err(1, "Byte string too long");
80
81         sprintf(buf, "%02x", *d);
82         for (p = buf + 2, i = 1; i < len; i++)
83                 p += sprintf(p, "%s%02x", s, *(d+i));
84
85         return(buf);
86 }
87
88 static void
89 ray_getval(char *iface, struct ray_param_req *rreq)
90 {
91         struct ifreq ifr;
92         int s;
93
94         bzero((char *)&ifr, sizeof(ifr));
95
96         strcpy(ifr.ifr_name, iface);
97         ifr.ifr_data = (caddr_t)rreq;
98
99         s = socket(AF_INET, SOCK_DGRAM, 0);
100
101         if (s == -1)
102                 err(1, "socket");
103
104         if (ioctl(s, SIOCGRAYPARAM, &ifr) == -1)
105                 warn("SIOCGRAYPARAM failed with failcode 0x%02x",
106                     rreq->r_failcause);
107
108         close(s);
109 }
110
111 static void
112 ray_getsiglev(char *iface, struct ray_siglev *siglev)
113 {
114         struct ifreq ifr;
115         int s;
116
117         bzero((char *)&ifr, sizeof(ifr));
118
119         strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
120         ifr.ifr_data = (caddr_t)siglev;
121
122         s = socket(AF_INET, SOCK_DGRAM, 0);
123
124         if (s == -1)
125                 err(1, "socket");
126
127         if (ioctl(s, SIOCGRAYSIGLEV, &ifr) == -1)
128                 err(1, "SIOCGRAYSIGLEV failed");
129
130         close(s);
131 }
132
133 static void
134 ray_getstats(char *iface, struct ray_stats_req *sreq)
135 {
136         struct ifreq ifr;
137         int s;
138
139         bzero((char *)&ifr, sizeof(ifr));
140
141         strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
142         ifr.ifr_data = (caddr_t)sreq;
143
144         s = socket(AF_INET, SOCK_DGRAM, 0);
145
146         if (s == -1)
147                 err(1, "socket");
148
149         if (ioctl(s, SIOCGRAYSTATS, &ifr) == -1)
150                 err(1, "SIOCGRAYSTATS failed");
151
152         close(s);
153 }
154
155 static int
156 ray_version(char *iface)
157 {
158         struct ray_param_req rreq;
159
160         if (iface == NULL)
161                 errx(1, "must specify interface name");
162
163         bzero((char *)&rreq, sizeof(rreq));
164         rreq.r_paramid = RAY_MIB_VERSION;
165         ray_getval(iface, &rreq);
166         return(*rreq.r_data);
167 }
168
169 static void
170 ray_dumpinfo(char *iface)
171 {
172         struct ray_param_req rreq;
173         u_int8_t mib, version;
174
175         if (iface == NULL)
176                 errx(1, "must specify interface name");
177
178         bzero((char *)&rreq, sizeof(rreq));
179
180         version = ray_version(iface);
181         printf("%-26s\t",  mib_strings[RAY_MIB_VERSION]);
182         printf("%d\n", 3+version);
183
184         for (mib = RAY_MIB_NET_TYPE; mib <= RAY_MIB_MAX; mib++) {
185
186                 if ((mib_info[mib][0] & version) == 0)
187                         continue;
188                 if (mib == RAY_MIB_VERSION)
189                         continue;
190
191                 rreq.r_paramid = mib;
192                 ray_getval(iface, &rreq);
193                 printf("%-26s\t",  mib_strings[mib]);
194                 switch (rreq.r_len) {
195
196                 case 2:
197                         printf("0x%02x%02x", *rreq.r_data, *(rreq.r_data+1));
198                         break;
199
200                 case ETHER_ADDR_LEN:
201                         printf("%s",
202                             ray_printhex(rreq.r_data, ":", rreq.r_len));
203                         break;
204
205                 case IEEE80211_NWID_LEN:
206                         printf("%-32s", (char *)rreq.r_data);
207                         break;
208
209
210                 case 1:
211                 default:
212                         printf("0x%02x", *rreq.r_data);
213                         break;
214                 }
215                 printf("\t%s\n",  mib_help_strings[mib]);
216         }
217 }
218
219 static void
220 ray_dumpsiglev(char *iface)
221 {
222         struct ray_siglev siglevs[RAY_NSIGLEVRECS];
223         int i;
224
225         if (iface == NULL)
226                 errx(1, "must specify interface name");
227
228         bzero((char *)siglevs, sizeof(siglevs));
229
230         ray_getsiglev(iface, siglevs);
231
232         for (i = 0; i < RAY_NSIGLEVRECS; i++) {
233                 printf("Slot %d: %s", i,
234                     ray_printhex(siglevs[i].rsl_host, ":", ETHER_ADDR_LEN));
235                 printf("  %s",
236                     ray_printhex(siglevs[i].rsl_siglevs, ",", RAY_NSIGLEV));
237                 printf("  %s\n",
238                     ray_printhex(siglevs[i].rsl_antennas, "", RAY_NANTENNA));
239         }
240
241 }
242
243 static void
244 ray_dumpstats(char *iface)
245 {
246         struct ray_stats_req sreq;
247
248         if (iface == NULL)
249                 errx(1, "must specify interface name");
250
251         bzero((char *)&sreq, sizeof(sreq));
252
253         ray_getstats(iface, &sreq);
254
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);
262 }
263
264 static void
265 ray_setval(char *iface, struct ray_param_req *rreq)
266 {
267         struct ifreq ifr;
268         int s;
269
270         bzero((char *)&ifr, sizeof(ifr));
271
272         strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
273         ifr.ifr_data = (caddr_t)rreq;
274
275         s = socket(AF_INET, SOCK_DGRAM, 0);
276
277         if (s == -1)
278                 err(1, "socket");
279
280         if (ioctl(s, SIOCSRAYPARAM, &ifr) == -1) {
281                 err(1, "SIOCSRAYPARAM failed with failcode 0x%02x",
282                     rreq->r_failcause);
283         }
284
285         close(s);
286 }
287
288 static void
289 ray_setword(char *iface, u_int8_t mib, u_int16_t v)
290 {
291         struct ray_param_req rreq;
292
293         if (iface == NULL)
294                 errx(1, "must specify interface name");
295
296         bzero((char *)&rreq, sizeof(rreq));
297
298         rreq.r_paramid = mib;
299         rreq.r_len = RAY_MIB_SIZE(mib_info, mib, ray_version(iface));
300         switch (rreq.r_len) {
301
302         case 1:
303                 *rreq.r_data = (u_int8_t)(v & 0xff);
304                 break;
305
306         case 2:
307                 *rreq.r_data = (u_int8_t)((v & 0xff00) >> 8);
308                 *(rreq.r_data+1) = (u_int8_t)(v & 0xff);
309                 break;
310
311         default:
312                 break;
313         }
314
315         ray_setval(iface, &rreq);
316 }
317
318 static void
319 ray_setstr(char *iface, u_int8_t mib, char *s)
320 {
321         struct ray_param_req rreq;
322
323         if (iface == NULL)
324                 errx(1, "must specify interface name");
325         if (s == NULL)
326                 errx(1, "must specify string");
327         if (strlen(s) > RAY_MIB_SIZE(mib_info, mib, ray_version(iface)))
328                 errx(1, "string too long");
329
330         bzero((char *)&rreq, sizeof(rreq));
331
332         rreq.r_paramid = mib;
333         rreq.r_len = RAY_MIB_SIZE(mib_info, mib, ray_version(iface));
334         bcopy(s, (char *)rreq.r_data, strlen(s));
335
336         ray_setval(iface, &rreq);
337 }
338
339 static void
340 usage(char *p)
341 {
342         fprintf(stderr, "usage:  %s -i iface\n", p);
343         fprintf(stderr, "\t%s -i iface -o\n", p);
344         fprintf(stderr, "\t%s -i iface -t tx rate\n", p);
345         fprintf(stderr, "\t%s -i iface -n network name\n", p);
346         fprintf(stderr, "\t%s -i iface -p port type\n", p);
347         fprintf(stderr, "\t%s -i iface -m mac address\n", p);
348         fprintf(stderr, "\t%s -i iface -d max data length\n", p);
349         fprintf(stderr, "\t%s -i iface -r RTS threshold\n", p);
350         fprintf(stderr, "\t%s -i iface -f hopset\n", p);
351         fprintf(stderr, "\t%s -i iface -P 0|1\n", p);
352         fprintf(stderr, "\t%s -i iface -S max sleep duration\n", p);
353         fprintf(stderr, "\t%s -i iface -C print signal cache\n", p);
354
355         exit(1);
356 }
357
358 int
359 main(int argc, char *argv[])
360 {
361
362         char *iface, *p;
363         int ch, val;
364
365         iface = NULL;
366         p = argv[0];
367
368         /* Get the interface name */
369         opterr = 0;
370         ch = getopt(argc, argv, "i:");
371         if (ch == 'i') {
372                 iface = optarg;
373         } else {
374                 if (argc > 1 && *argv[1] != '-') {
375                         iface = argv[1];
376                         optind = 2; 
377                 } else {
378                         iface = "ray0";
379                         optind = 1;
380                 }
381                 optreset = 1;
382         }
383         opterr = 1;
384
385         while ((ch = getopt(argc, argv, "hoCi:d:f:n:p:r:t:W:")) != -1) {
386                 switch (ch) {
387
388                 case 'i':
389                         iface = optarg;
390                         break;
391
392                 case 'd':
393                         val = atoi(optarg);
394                         if (((val < 350) &&
395                             (val != -1)) || (val > RAY_MIB_FRAG_THRESH_MAXIMUM))
396                                 usage(p);
397                         if (val == -1)
398                                 val = 0x7fff;
399                         ray_setword(iface, RAY_MIB_FRAG_THRESH, val);
400                         exit(0);
401                         break;
402
403                 case 'f':
404                         val = atoi(optarg);
405                         if ((val < RAY_MIB_COUNTRY_CODE_MIMIMUM) ||
406                             (val > RAY_MIB_COUNTRY_CODE_MAXIMUM))
407                                 usage(p);
408                         ray_setword(iface, RAY_MIB_COUNTRY_CODE, val);
409                         exit(0);
410                         break;
411
412                 case 'n':
413                         ray_setstr(iface, RAY_MIB_SSID, optarg);
414                         exit(0);
415                         break;
416
417                 case 'o':
418                         ray_dumpstats(iface);
419                         exit(0);
420                         break;
421
422                 case 'p':
423                         val = atoi(optarg);
424                         if ((val < 0) || (val > 1))
425                                 usage(p);
426                         ray_setword(iface, RAY_MIB_NET_TYPE, val);
427                         exit(0);
428                         break;
429
430
431                 case 'r':
432                         val = atoi(optarg);
433                         if ((val < -1) || (val > RAY_MIB_RTS_THRESH_MAXIMUM))
434                                 usage(p);
435                         if (val == -1)
436                                 val = 0x7fff;
437                         ray_setword(iface, RAY_MIB_RTS_THRESH, val);
438                         exit(0);
439                         break;
440
441                 case 't':
442                         val = atoi(optarg);
443                         if ((val < RAY_MIB_BASIC_RATE_SET_MINIMUM) ||
444                             (val > RAY_MIB_BASIC_RATE_SET_MAXIMUM))
445                                 usage(p);
446                         ray_setword(iface, RAY_MIB_BASIC_RATE_SET, val);
447                         exit(0);
448                         break;
449
450                 case 'C':
451                         ray_dumpsiglev(iface);
452                         exit(0);
453                         break;
454
455                 case 'W':
456                         {
457                                 char *stringp, **ap, *av[5];
458                                 u_int8_t mib;
459
460                                 stringp = optarg;
461                                 ap = av;
462                                 *ap = strsep(&stringp, ":");
463                                 ap++;
464                                 *ap = strsep(&stringp, ":");
465                                 mib = atoi(av[0]);
466                                 sscanf(av[1], "%x", &val);
467                                 printf("mib %d, val 0x%02x\n", mib, val);
468                                 ray_setword(iface, mib, val);
469                         }
470                         exit(0);
471                         break;
472
473                 case 'h':
474                 default:
475                         usage(p);
476
477                 }
478         }
479
480         if (iface == NULL)
481                 usage(p);
482
483         ray_dumpinfo(iface);
484
485         exit(0);
486 }