wlconfig.c revision 27928
1/*
2 * Copyright (C) 1996
3 *      Michael Smith.  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 *
14 * THIS SOFTWARE IS PROVIDED BY Michael Smith AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL Michael Smith OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $Id: wlconfig.c,v 1.3 1997/08/01 03:50:23 msmith Exp $
27 *
28 */
29/*
30 * wlconfig.c
31 *
32 * utility to read out and change various WAVELAN parameters.
33 * Currently supports NWID and IRQ values.
34 *
35 * The NWID is used by 2 or more wavelan controllers to determine
36 * if packets should be received or not.  It is a filter that
37 * is roughly analogous to the "channel" setting with a garage
38 * door controller.  Two companies side by side with wavelan devices
39 * that could actually hear each other can use different NWIDs
40 * and ignore packets.  In truth however, the air space is shared,
41 * and the NWID is a virtual filter.
42 *
43 * In the current set of wavelan drivers, ioctls changed only
44 * the runtime radio modem registers which act in a manner analogous
45 * to an ethernet transceiver.  The ioctls do not change the
46 * stored nvram PSA (or parameter storage area).  At boot, the PSA
47 * values are stored in the radio modem.   Thus when the
48 * system reboots it will restore the wavelan NWID to the value
49 * stored in the PSA.  The NCR/ATT dos utilities must be used to
50 * change the initial NWID values in the PSA.  The wlconfig utility
51 * may be used to set a different NWID at runtime; this is only
52 * permitted while the interface is up and running.
53 *
54 * By contrast, the IRQ value can only be changed while the
55 * Wavelan card is down and unconfigured, and it will remain
56 * disabled after an IRQ change until reboot.
57 *
58 */
59
60#include <sys/param.h>
61#include <sys/socket.h>
62#include <sys/ioctl.h>
63#include <sys/time.h>
64#include <machine/if_wl_wavelan.h>
65
66#include <net/if.h>
67#if __FreeBSD__ >= 3
68#include <net/if_var.h>
69#endif
70#include <netinet/in.h>
71#include <netinet/if_ether.h>
72extern struct ether_addr *ether_aton(char *a);
73
74#include <err.h>
75#include <stdio.h>
76#include <stdlib.h>
77#include <string.h>
78#include <unistd.h>
79#include <limits.h>
80
81/* translate IRQ bit to number */
82/* array for maping irq numbers to values for the irq parameter register */
83static int irqvals[16] = {
84    0, 0, 0, 0x01, 0x02, 0x04, 0, 0x08, 0, 0, 0x10, 0x20, 0x40, 0, 0, 0x80
85};
86
87/* cache */
88static int w_sigitems;	/* count of valid items */
89static struct w_sigcache wsc[MAXCACHEITEMS];
90
91int
92wlirq(int irqval)
93{
94    int irq;
95
96    for(irq = 0; irq < 16; irq++)
97	if(irqvals[irq] == irqval)
98	    return(irq);
99    return 0;
100}
101
102char *compat_type[] = {
103    "PC-AT 915MHz",
104    "PC-MC 915MHz",
105    "PC-AT 2.4GHz",
106    "PC-MC 2.4GHz",
107    "PCCARD or 1/2 size AT, 915MHz or 2.4GHz"
108};
109
110char *subband[] = {
111    "915MHz/see WaveModem",
112    "2425MHz",
113    "2460MHz",
114    "2484MHz",
115    "2430.5MHz"
116};
117
118
119/*
120** print_psa
121**
122** Given a pointer to a PSA structure, print it out
123*/
124void
125print_psa(u_char *psa, int currnwid)
126{
127    int		nwid;
128
129    /*
130    ** Work out what sort of board we have
131    */
132    if (psa[0] == 0x14) {
133	printf("Board type            : Microchannel\n");
134    } else {
135	if (psa[1] == 0) {
136	    printf("Board type            : PCCARD\n");
137	} else {
138	    printf("Board type            : ISA");
139	    if ((psa[4] == 0) &&
140		(psa[5] == 0) &&
141		(psa[6] == 0))
142		printf(" (DEC OEM)");
143	    printf("\n");
144	    printf("Base address options  : 0x300, 0x%02x0, 0x%02x0, 0x%02x0\n",
145		   (int)psa[1], (int)psa[2], (int)psa[3]);
146	    printf("Waitstates            : %d\n",psa[7] & 0xf);
147	    printf("Bus mode              : %s\n",(psa[7] & 0x10) ? "EISA" : "ISA");
148	    printf("IRQ                   : %d\n",wlirq(psa[8]));
149	}
150    }
151    printf("Default MAC address   : %02x:%02x:%02x:%02x:%02x:%02x\n",
152	   psa[0x10],psa[0x11],psa[0x12],psa[0x13],psa[0x14],psa[0x15]);
153    printf("Soft MAC address      : %02x:%02x:%02x:%02x:%02x:%02x\n",
154	   psa[0x16],psa[0x17],psa[0x18],psa[0x19],psa[0x1a],psa[0x1b]);
155    printf("Current MAC address   : %s\n",(psa[0x1c] & 0x1) ? "Soft" : "Default");
156    printf("Adapter compatability : ");
157    if (psa[0x1d] < 5) {
158	printf("%s\n",compat_type[psa[0x1d]]);
159    } else {
160	printf("unknown\n");
161    }
162    printf("Threshold preset      : %d\n",psa[0x1e]);
163    printf("Call code required    : %s\n",(psa[0x1f] & 0x1) ? "YES" : "NO");
164    if (psa[0x1f] & 0x1)
165	printf("Call code             : 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
166	       psa[0x30],psa[0x31],psa[0x32],psa[0x33],psa[0x34],psa[0x35],psa[0x36],psa[0x37]);
167    printf("Subband               : %s\n",subband[psa[0x20] & 0xf]);
168    printf("Quality threshold     : %d\n",psa[0x21]);
169    printf("Hardware version      : %d (%s)\n",psa[0x22],psa[0x22] ? "Rel3" : "Rel1/Rel2");
170    printf("Network ID enable     : %s\n",(psa[0x25] & 0x1) ? "YES" : "NO");
171    if (psa[0x25] & 0x1) {
172	nwid = (psa[0x23] << 8) + psa[0x24];
173	printf("NWID                  : 0x%04x\n",nwid);
174	if (nwid != currnwid) {
175	    printf("Current NWID          : 0x%04x\n",currnwid);
176	}
177    }
178    printf("Datalink security     : %s\n",(psa[0x26] & 0x1) ? "YES" : "NO");
179    if (psa[0x26] & 0x1) {
180	printf("Encryption key        : ");
181	if (psa[0x27] == 0) {
182	    printf("DENIED\n");
183	} else {
184	    printf("0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
185		   psa[0x27],psa[0x28],psa[0x29],psa[0x2a],psa[0x2b],psa[0x2c],psa[0x2d],psa[0x2e]);
186	}
187    }
188    printf("Databus width         : %d (%s)\n",
189	   (psa[0x2f] & 0x1) ? 16 : 8, (psa[0x2f] & 0x80) ? "fixed" : "variable");
190    printf("Configuration state   : %sconfigured\n",(psa[0x38] & 0x1) ? "" : "un");
191    printf("CRC-16                : 0x%02x%02x\n",psa[0x3e],psa[0x3d]);
192    printf("CRC status            : ");
193    switch(psa[0x3f]) {
194    case 0xaa:
195	printf("OK\n");
196	break;
197    case 0x55:
198	printf("BAD\n");
199	break;
200    default:
201	printf("Error\n");
202	break;
203    }
204}
205
206
207void
208syntax(char *pname)
209{
210    fprintf(stderr,"Usage: %s <ifname> [<param> <value> ...]\n",pname);
211    fprintf(stderr,"    <ifname>    Wavelan interface name.\n");
212    fprintf(stderr,"    <param>     Parameter name (see below)\n");
213    fprintf(stderr,"    <value>     New value for parameter.\n");
214    fprintf(stderr," Parameter name:        Value:\n");
215    fprintf(stderr,"     irq		3,4,5,6,10,11,12,15\n");
216    fprintf(stderr,"     mac		soft ethernet address\n");
217    fprintf(stderr,"     macsel		soft or default\n");
218    fprintf(stderr,"     nwid		default NWID (0x0-0xffff)\n");
219    fprintf(stderr,"     currnwid       current NWID (0x0-0xffff) or 'get'\n");
220    fprintf(stderr,"     cache          signal strength cache\n");
221    fprintf(stderr,"     cache values = { raw, scale, zero }\n");
222    exit(1);
223}
224
225
226void
227get_cache(int sd, struct ifreq *ifr)
228{
229    /* get the cache count */
230    if (ioctl(sd, SIOCGWLCITEM, (caddr_t)ifr)) {
231	perror("SIOCGWLCITEM - get cache count");
232	exit(1);
233    }
234    w_sigitems = (int) ifr->ifr_data;
235
236    ifr->ifr_data = (caddr_t) &wsc;
237    /* get the cache */
238    if (ioctl(sd, SIOCGWLCACHE, (caddr_t)ifr)) {
239	perror("SIOCGWLCACHE - get cache count");
240	exit(1);
241    }
242}
243
244static int
245scale_value(int value, int max)
246{
247	double dmax = (double) max;
248	if (value > max)
249		return(100);
250	return((value/dmax) * 100);
251}
252
253static void
254dump_cache(int rawFlag)
255{
256	int i;
257	int signal, silence, quality;
258
259	if (rawFlag)
260		printf("signal range 0..63: silence 0..63: quality 0..15\n");
261	else
262		printf("signal range 0..100: silence 0..100: quality 0..100\n");
263
264	/* after you read it, loop through structure,i.e. wsc
265         * print each item:
266	 */
267	for(i = 0; i < w_sigitems; i++) {
268		printf("[%d:%d]>\n", i+1, w_sigitems);
269        	printf("\tip: %d.%d.%d.%d,",((wsc[i].ipsrc >> 0) & 0xff),
270				        ((wsc[i].ipsrc >> 8) & 0xff),
271				        ((wsc[i].ipsrc >> 16) & 0xff),
272				        ((wsc[i].ipsrc >> 24) & 0xff));
273		printf(" mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
274		  		    	wsc[i].macsrc[0]&0xff,
275		  		    	wsc[i].macsrc[1]&0xff,
276		   		    	wsc[i].macsrc[2]&0xff,
277		   			wsc[i].macsrc[3]&0xff,
278		   			wsc[i].macsrc[4]&0xff,
279		   			wsc[i].macsrc[5]&0xff);
280		if (rawFlag) {
281			signal = wsc[i].signal;
282			silence = wsc[i].silence;
283			quality = wsc[i].quality;
284		}
285		else {
286			signal = scale_value(wsc[i].signal, 63);
287			silence = scale_value(wsc[i].silence, 63);
288			quality = scale_value(wsc[i].quality, 15);
289		}
290		printf("\tsignal: %d, silence: %d, quality: %d, ",
291		   			signal,
292		   			silence,
293		   			quality);
294		printf("snr: %d\n", signal - silence);
295	}
296}
297
298#define raw_cache()	dump_cache(1)
299#define scale_cache()	dump_cache(0)
300
301void
302main(int argc, char *argv[])
303{
304    int 		sd;
305    struct ifreq	ifr;
306    u_char		psabuf[0x40];
307    int			val, argind, i;
308    char		*cp, *param, *value;
309    struct ether_addr	*ea;
310    int			work = 0;
311    int			currnwid;
312
313    if ((argc < 2) || (argc % 2))
314	syntax(argv[0]);
315
316    /* get a socket */
317    sd = socket(AF_INET, SOCK_DGRAM, 0);
318    if (sd < 0)
319	err(1,"socket");
320    strncpy(ifr.ifr_name, argv[1], sizeof(ifr.ifr_name));
321    ifr.ifr_addr.sa_family = AF_INET;
322
323    /* get the PSA */
324    ifr.ifr_data = (caddr_t)psabuf;
325    if (ioctl(sd, SIOCGWLPSA, (caddr_t)&ifr))
326	err(1,"Get PSA");
327
328    /* get the current NWID */
329    if (ioctl(sd, SIOCGWLCNWID, (caddr_t)&ifr))
330	err(1,"Get NWID");
331    currnwid = (int)ifr.ifr_data;
332
333    /* just dump and exit? */
334    if (argc == 2) {
335	print_psa(psabuf, currnwid);
336	exit(0);
337    }
338
339    /* loop reading arg pairs */
340    for (argind = 2; argind < argc; argind += 2) {
341
342	param = argv[argind];
343	value = argv[argind+1];
344
345	/* What to do? */
346
347	if (!strcasecmp(param,"currnwid")) {		/* set current NWID */
348	    val = strtol(value,&cp,0);
349	    if ((val < 0) || (val > 0xffff) || (cp == value))
350		errx(1,"Bad NWID '%s'",value);
351
352	    ifr.ifr_data = (caddr_t)val;
353	    if (ioctl(sd, SIOCSWLCNWID, (caddr_t)&ifr))
354		err(1,"Set NWID (interface not up?)");
355	    continue ;
356	}
357
358	if (!strcasecmp(param,"irq")) {
359	    val = strtol(value,&cp,0);
360	    val = irqvals[val];
361	    if ((val == 0) || (cp == value))
362		errx(1,"Bad IRQ '%s'",value);
363	    psabuf[WLPSA_IRQNO] = (u_char)val;
364	    work = 1;
365	    continue;
366	}
367
368	if (!strcasecmp(param,"mac")) {
369	    if ((ea = ether_aton(value)) == NULL)
370		errx(1,"Bad ethernet address '%s'",value);
371	    for (i = 0; i < 6; i++)
372		psabuf[WLPSA_LOCALMAC + i] = ea->octet[i];
373	    work = 1;
374	    continue;
375	}
376
377	if (!strcasecmp(param,"macsel")) {
378	    if (!strcasecmp(value,"local")) {
379		psabuf[WLPSA_MACSEL] |= 0x1;
380		work = 1;
381		continue;
382	    }
383	    if (!strcasecmp(value,"universal")) {
384		psabuf[WLPSA_MACSEL] &= ~0x1;
385		work = 1;
386		continue;
387	    }
388	    errx(1,"Bad macsel value '%s'",value);
389	}
390
391	if (!strcasecmp(param,"nwid")) {
392	    val = strtol(value,&cp,0);
393	    if ((val < 0) || (val > 0xffff) || (cp == value))
394		errx(1,"Bad NWID '%s'",value);
395	    psabuf[WLPSA_NWID] = (val >> 8) & 0xff;
396	    psabuf[WLPSA_NWID+1] = val & 0xff;
397	    work = 1;
398	    continue;
399	}
400	if (!strcasecmp(param,"cache")) {
401
402            /* raw cache dump
403	    */
404	    if (!strcasecmp(value,"raw")) {
405	    	get_cache(sd, &ifr);
406		raw_cache();
407		continue;
408	    }
409            /* scaled cache dump
410	    */
411	    else if (!strcasecmp(value,"scale")) {
412	    	get_cache(sd, &ifr);
413		scale_cache();
414		continue;
415	    }
416	    /* zero out cache
417	    */
418	    else if (!strcasecmp(value,"zero")) {
419		if (ioctl(sd, SIOCDWLCACHE, (caddr_t)&ifr))
420		    err(1,"Zero cache");
421		continue;
422	    }
423	    errx(1,"Unknown value '%s'", value);
424 	}
425	errx(1,"Unknown parameter '%s'",param);
426    }
427    if (work) {
428	ifr.ifr_data = (caddr_t)psabuf;
429	if (ioctl(sd, SIOCSWLPSA, (caddr_t)&ifr))
430	    err(1,"Set PSA");
431    }
432}
433