]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/raycontrol/raycontrol.c
This commit was generated by cvs2svn to compensate for changes in r93982,
[FreeBSD/FreeBSD.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$
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 (len > 256)
79                 err(1, "Byte string too long");
80
81         sprintf(buf, "%02x", *d);
82         for (p = buf + 2, i = 1; i < len; i++) {
83                 sprintf(p, "%s%02x", s, *(d+i));
84                 p = p + 2 + strlen(s);
85         }
86
87         return(buf);
88 }
89
90 static void
91 ray_getval(char *iface, struct ray_param_req *rreq)
92 {
93         struct ifreq ifr;
94         int s;
95
96         bzero((char *)&ifr, sizeof(ifr));
97
98         strlcpy(ifr.ifr_name, iface, IFNAMSIZ);
99         ifr.ifr_data = (caddr_t)rreq;
100
101         s = socket(AF_INET, SOCK_DGRAM, 0);
102
103         if (s == -1)
104                 err(1, "socket");
105
106         if (ioctl(s, SIOCGRAYPARAM, &ifr) == -1)
107                 warn("SIOCGRAYPARAM failed with failcode 0x%02x",
108                     rreq->r_failcause);
109
110         close(s);
111 }
112
113 static void
114 ray_getsiglev(char *iface, struct ray_siglev *siglev)
115 {
116         struct ifreq ifr;
117         int s;
118
119         bzero((char *)&ifr, sizeof(ifr));
120
121         strcpy(ifr.ifr_name, iface);
122         ifr.ifr_data = (caddr_t)siglev;
123
124         s = socket(AF_INET, SOCK_DGRAM, 0);
125
126         if (s == -1)
127                 err(1, "socket");
128
129         if (ioctl(s, SIOCGRAYSIGLEV, &ifr) == -1)
130                 err(1, "SIOCGRAYSIGLEV failed");
131
132         close(s);
133 }
134
135 static void
136 ray_getstats(char *iface, struct ray_stats_req *sreq)
137 {
138         struct ifreq ifr;
139         int s;
140
141         bzero((char *)&ifr, sizeof(ifr));
142
143         strcpy(ifr.ifr_name, iface);
144         ifr.ifr_data = (caddr_t)sreq;
145
146         s = socket(AF_INET, SOCK_DGRAM, 0);
147
148         if (s == -1)
149                 err(1, "socket");
150
151         if (ioctl(s, SIOCGRAYSTATS, &ifr) == -1)
152                 err(1, "SIOCGRAYSTATS failed");
153
154         close(s);
155 }
156
157 static int
158 ray_version(char *iface)
159 {
160         struct ray_param_req rreq;
161
162         if (iface == NULL)
163                 errx(1, "must specify interface name");
164
165         bzero((char *)&rreq, sizeof(rreq));
166         rreq.r_paramid = RAY_MIB_VERSION;
167         ray_getval(iface, &rreq);
168         return(*rreq.r_data);
169 }
170
171 static void
172 ray_dumpinfo(char *iface)
173 {
174         struct ray_param_req rreq;
175         u_int8_t mib, version;
176
177         if (iface == NULL)
178                 errx(1, "must specify interface name");
179
180         bzero((char *)&rreq, sizeof(rreq));
181
182         version = ray_version(iface);
183         printf("%-26s\t",  mib_strings[RAY_MIB_VERSION]);
184         printf("%d\n", 3+version);
185
186         for (mib = RAY_MIB_NET_TYPE; mib <= RAY_MIB_MAX; mib++) {
187
188                 if ((mib_info[mib][0] & version) == 0)
189                         continue;
190                 if (mib == RAY_MIB_VERSION)
191                         continue;
192
193                 rreq.r_paramid = mib;
194                 ray_getval(iface, &rreq);
195                 printf("%-26s\t",  mib_strings[mib]);
196                 switch (rreq.r_len) {
197
198                 case 2:
199                         printf("0x%02x%02x", *rreq.r_data, *(rreq.r_data+1));
200                         break;
201
202                 case ETHER_ADDR_LEN:
203                         printf("%s",
204                             ray_printhex(rreq.r_data, ":", rreq.r_len));
205                         break;
206
207                 case IEEE80211_NWID_LEN:
208                         printf("%-32s", (char *)rreq.r_data);
209                         break;
210
211
212                 case 1:
213                 default:
214                         printf("0x%02x", *rreq.r_data);
215                         break;
216                 }
217                 printf("\t%s\n",  mib_help_strings[mib]);
218         }
219 }
220
221 static void
222 ray_dumpsiglev(char *iface)
223 {
224         struct ray_siglev siglevs[RAY_NSIGLEVRECS];
225         int i;
226
227         if (iface == NULL)
228                 errx(1, "must specify interface name");
229
230         bzero((char *)siglevs, sizeof(siglevs));
231
232         ray_getsiglev(iface, siglevs);
233
234         for (i = 0; i < RAY_NSIGLEVRECS; i++) {
235                 printf("Slot %d: %s", i,
236                     ray_printhex(siglevs[i].rsl_host, ":", ETHER_ADDR_LEN));
237                 printf("  %s",
238                     ray_printhex(siglevs[i].rsl_siglevs, ",", RAY_NSIGLEV));
239                 printf("  %s\n",
240                     ray_printhex(siglevs[i].rsl_antennas, "", RAY_NANTENNA));
241         }
242
243 }
244
245 static void
246 ray_dumpstats(char *iface)
247 {
248         struct ray_stats_req sreq;
249
250         if (iface == NULL)
251                 errx(1, "must specify interface name");
252
253         bzero((char *)&sreq, sizeof(sreq));
254
255         ray_getstats(iface, &sreq);
256
257         printf("Receiver overflows        %lu\n",
258             (unsigned long int)sreq.rxoverflow);
259         printf("Receiver checksum errors  %lu\n",
260             (unsigned long int)sreq.rxcksum);
261         printf("Header checksum errors    %lu\n",
262             (unsigned long int)sreq.rxhcksum);
263         printf("Clear channel noise level %u\n", sreq.rxnoise);
264 }
265
266 static void
267 ray_setval(char *iface, struct ray_param_req *rreq)
268 {
269         struct ifreq ifr;
270         int s;
271
272         bzero((char *)&ifr, sizeof(ifr));
273
274         strcpy(ifr.ifr_name, iface);
275         ifr.ifr_data = (caddr_t)rreq;
276
277         s = socket(AF_INET, SOCK_DGRAM, 0);
278
279         if (s == -1)
280                 err(1, "socket");
281
282         if (ioctl(s, SIOCSRAYPARAM, &ifr) == -1) {
283                 err(1, "SIOCSRAYPARAM failed with failcode 0x%02x",
284                     rreq->r_failcause);
285         }
286
287         close(s);
288 }
289
290 static void
291 ray_setword(char *iface, u_int8_t mib, u_int16_t v)
292 {
293         struct ray_param_req rreq;
294
295         if (iface == NULL)
296                 errx(1, "must specify interface name");
297
298         bzero((char *)&rreq, sizeof(rreq));
299
300         rreq.r_paramid = mib;
301         rreq.r_len = RAY_MIB_SIZE(mib_info, mib, ray_version(iface));
302         switch (rreq.r_len) {
303
304         case 1:
305                 *rreq.r_data = (u_int8_t)(v & 0xff);
306                 break;
307
308         case 2:
309                 *rreq.r_data = (u_int8_t)((v & 0xff00) >> 8);
310                 *(rreq.r_data+1) = (u_int8_t)(v & 0xff);
311                 break;
312
313         default:
314                 break;
315         }
316
317         ray_setval(iface, &rreq);
318 }
319
320 static void
321 ray_setstr(char *iface, u_int8_t mib, char *s)
322 {
323         struct ray_param_req rreq;
324
325         if (iface == NULL)
326                 errx(1, "must specify interface name");
327         if (s == NULL)
328                 errx(1, "must specify string");
329         if (strlen(s) > RAY_MIB_SIZE(mib_info, mib, ray_version(iface)))
330                 errx(1, "string too long");
331
332         bzero((char *)&rreq, sizeof(rreq));
333
334         rreq.r_paramid = mib;
335         rreq.r_len = RAY_MIB_SIZE(mib_info, mib, ray_version(iface));
336         bcopy(s, (char *)rreq.r_data, strlen(s));
337
338         ray_setval(iface, &rreq);
339 }
340
341 static void
342 usage(char *p)
343 {
344         fprintf(stderr, "usage:  %s -i iface\n", p);
345         fprintf(stderr, "\t%s -i iface -o\n", p);
346         fprintf(stderr, "\t%s -i iface -t tx rate\n", p);
347         fprintf(stderr, "\t%s -i iface -n network name\n", p);
348         fprintf(stderr, "\t%s -i iface -p port type\n", p);
349         fprintf(stderr, "\t%s -i iface -m mac address\n", p);
350         fprintf(stderr, "\t%s -i iface -d max data length\n", p);
351         fprintf(stderr, "\t%s -i iface -r RTS threshold\n", p);
352         fprintf(stderr, "\t%s -i iface -f hopset\n", p);
353         fprintf(stderr, "\t%s -i iface -P 0|1\n", p);
354         fprintf(stderr, "\t%s -i iface -S max sleep duration\n", p);
355         fprintf(stderr, "\t%s -i iface -C print signal cache\n", p);
356
357         exit(1);
358 }
359
360 int
361 main(int argc, char *argv[])
362 {
363
364         char *iface, *p;
365         int ch, val;
366
367         iface = NULL;
368         p = argv[0];
369
370         /* Get the interface name */
371         opterr = 0;
372         ch = getopt(argc, argv, "i:");
373         if (ch == 'i') {
374                 iface = optarg;
375         } else {
376                 if (argc > 1 && *argv[1] != '-') {
377                         iface = argv[1];
378                         optind = 2; 
379                 } else {
380                         iface = "ray0";
381                         optind = 1;
382                 }
383                 optreset = 1;
384         }
385         opterr = 1;
386
387         while ((ch = getopt(argc, argv, "hoCi:d:f:n:p:r:t:W:")) != -1) {
388                 switch (ch) {
389
390                 case 'i':
391                         iface = optarg;
392                         break;
393
394                 case 'd':
395                         val = atoi(optarg);
396                         if (((val < 350) &&
397                             (val != -1)) || (val > RAY_MIB_FRAG_THRESH_MAXIMUM))
398                                 usage(p);
399                         if (val == -1)
400                                 val = 0x7fff;
401                         ray_setword(iface, RAY_MIB_FRAG_THRESH, val);
402                         exit(0);
403                         break;
404
405                 case 'f':
406                         val = atoi(optarg);
407                         if ((val < RAY_MIB_COUNTRY_CODE_MIMIMUM) ||
408                             (val > RAY_MIB_COUNTRY_CODE_MAXIMUM))
409                                 usage(p);
410                         ray_setword(iface, RAY_MIB_COUNTRY_CODE, val);
411                         exit(0);
412                         break;
413
414                 case 'n':
415                         ray_setstr(iface, RAY_MIB_SSID, optarg);
416                         exit(0);
417                         break;
418
419                 case 'o':
420                         ray_dumpstats(iface);
421                         exit(0);
422                         break;
423
424                 case 'p':
425                         val = atoi(optarg);
426                         if ((val < 0) || (val > 1))
427                                 usage(p);
428                         ray_setword(iface, RAY_MIB_NET_TYPE, val);
429                         exit(0);
430                         break;
431
432
433                 case 'r':
434                         val = atoi(optarg);
435                         if ((val < -1) || (val > RAY_MIB_RTS_THRESH_MAXIMUM))
436                                 usage(p);
437                         if (val == -1)
438                                 val = 0x7fff;
439                         ray_setword(iface, RAY_MIB_RTS_THRESH, val);
440                         exit(0);
441                         break;
442
443                 case 't':
444                         val = atoi(optarg);
445                         if ((val < RAY_MIB_BASIC_RATE_SET_MINIMUM) ||
446                             (val > RAY_MIB_BASIC_RATE_SET_MAXIMUM))
447                                 usage(p);
448                         ray_setword(iface, RAY_MIB_BASIC_RATE_SET, val);
449                         exit(0);
450                         break;
451
452                 case 'C':
453                         ray_dumpsiglev(iface);
454                         exit(0);
455                         break;
456
457                 case 'W':
458                         {
459                                 char *stringp, **ap, *av[5];
460                                 u_int8_t mib;
461
462                                 stringp = optarg;
463                                 ap = av;
464                                 *ap = strsep(&stringp, ":");
465                                 ap++;
466                                 *ap = strsep(&stringp, ":");
467                                 mib = atoi(av[0]);
468                                 sscanf(av[1], "%x", &val);
469                                 printf("mib %d, val 0x%02x\n", mib, val);
470                                 ray_setword(iface, mib, val);
471                         }
472                         exit(0);
473                         break;
474
475                 case 'h':
476                 default:
477                         usage(p);
478
479                 }
480         }
481
482         if (iface == NULL)
483                 usage(p);
484
485         ray_dumpinfo(iface);
486
487         exit(0);
488 }