wlconfig.c revision 32570
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
27#ifndef lint
28static const char rcsid[] =
29	"$Id: wlconfig.c,v 1.6 1997/10/27 12:23:08 charnier Exp $";
30#endif /* not lint */
31
32/*
33 * wlconfig.c
34 *
35 * utility to read out and change various WAVELAN parameters.
36 * Currently supports NWID and IRQ values.
37 *
38 * The NWID is used by 2 or more wavelan controllers to determine
39 * if packets should be received or not.  It is a filter that
40 * is roughly analogous to the "channel" setting with a garage
41 * door controller.  Two companies side by side with wavelan devices
42 * that could actually hear each other can use different NWIDs
43 * and ignore packets.  In truth however, the air space is shared,
44 * and the NWID is a virtual filter.
45 *
46 * In the current set of wavelan drivers, ioctls changed only
47 * the runtime radio modem registers which act in a manner analogous
48 * to an ethernet transceiver.  The ioctls do not change the
49 * stored nvram PSA (or parameter storage area).  At boot, the PSA
50 * values are stored in the radio modem.   Thus when the
51 * system reboots it will restore the wavelan NWID to the value
52 * stored in the PSA.  The NCR/ATT dos utilities must be used to
53 * change the initial NWID values in the PSA.  The wlconfig utility
54 * may be used to set a different NWID at runtime; this is only
55 * permitted while the interface is up and running.
56 *
57 * By contrast, the IRQ value can only be changed while the
58 * Wavelan card is down and unconfigured, and it will remain
59 * disabled after an IRQ change until reboot.
60 *
61 */
62
63#include <sys/param.h>
64#include <sys/socket.h>
65#include <sys/ioctl.h>
66#include <sys/time.h>
67#include <machine/if_wl_wavelan.h>
68
69#include <net/if.h>
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
207static void
208usage()
209{
210    fprintf(stderr,"usage: wlconfig ifname [param value ...]\n");
211    exit(1);
212}
213
214
215void
216get_cache(int sd, struct ifreq *ifr)
217{
218    /* get the cache count */
219    if (ioctl(sd, SIOCGWLCITEM, (caddr_t)ifr))
220	err(1, "SIOCGWLCITEM - get cache count");
221    w_sigitems = (int) ifr->ifr_data;
222
223    ifr->ifr_data = (caddr_t) &wsc;
224    /* get the cache */
225    if (ioctl(sd, SIOCGWLCACHE, (caddr_t)ifr))
226	err(1, "SIOCGWLCACHE - get cache count");
227}
228
229static int
230scale_value(int value, int max)
231{
232	double dmax = (double) max;
233	if (value > max)
234		return(100);
235	return((value/dmax) * 100);
236}
237
238static void
239dump_cache(int rawFlag)
240{
241	int i;
242	int signal, silence, quality;
243
244	if (rawFlag)
245		printf("signal range 0..63: silence 0..63: quality 0..15\n");
246	else
247		printf("signal range 0..100: silence 0..100: quality 0..100\n");
248
249	/* after you read it, loop through structure,i.e. wsc
250         * print each item:
251	 */
252	for(i = 0; i < w_sigitems; i++) {
253		printf("[%d:%d]>\n", i+1, w_sigitems);
254        	printf("\tip: %d.%d.%d.%d,",((wsc[i].ipsrc >> 0) & 0xff),
255				        ((wsc[i].ipsrc >> 8) & 0xff),
256				        ((wsc[i].ipsrc >> 16) & 0xff),
257				        ((wsc[i].ipsrc >> 24) & 0xff));
258		printf(" mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
259		  		    	wsc[i].macsrc[0]&0xff,
260		  		    	wsc[i].macsrc[1]&0xff,
261		   		    	wsc[i].macsrc[2]&0xff,
262		   			wsc[i].macsrc[3]&0xff,
263		   			wsc[i].macsrc[4]&0xff,
264		   			wsc[i].macsrc[5]&0xff);
265		if (rawFlag) {
266			signal = wsc[i].signal;
267			silence = wsc[i].silence;
268			quality = wsc[i].quality;
269		}
270		else {
271			signal = scale_value(wsc[i].signal, 63);
272			silence = scale_value(wsc[i].silence, 63);
273			quality = scale_value(wsc[i].quality, 15);
274		}
275		printf("\tsignal: %d, silence: %d, quality: %d, ",
276		   			signal,
277		   			silence,
278		   			quality);
279		printf("snr: %d\n", signal - silence);
280	}
281}
282
283#define raw_cache()	dump_cache(1)
284#define scale_cache()	dump_cache(0)
285
286int
287main(int argc, char *argv[])
288{
289    int 		sd;
290    struct ifreq	ifr;
291    u_char		psabuf[0x40];
292    int			val, argind, i;
293    char		*cp, *param, *value;
294    struct ether_addr	*ea;
295    int			work = 0;
296    int			currnwid;
297
298    if ((argc < 2) || (argc % 2))
299	usage();
300
301    /* get a socket */
302    sd = socket(AF_INET, SOCK_DGRAM, 0);
303    if (sd < 0)
304	err(1,"socket");
305    strncpy(ifr.ifr_name, argv[1], sizeof(ifr.ifr_name));
306    ifr.ifr_addr.sa_family = AF_INET;
307
308    /* get the PSA */
309    ifr.ifr_data = (caddr_t)psabuf;
310    if (ioctl(sd, SIOCGWLPSA, (caddr_t)&ifr))
311	err(1,"get PSA");
312
313    /* get the current NWID */
314    if (ioctl(sd, SIOCGWLCNWID, (caddr_t)&ifr))
315	err(1,"get NWID");
316    currnwid = (int)ifr.ifr_data;
317
318    /* just dump and exit? */
319    if (argc == 2) {
320	print_psa(psabuf, currnwid);
321	exit(0);
322    }
323
324    /* loop reading arg pairs */
325    for (argind = 2; argind < argc; argind += 2) {
326
327	param = argv[argind];
328	value = argv[argind+1];
329
330	/* What to do? */
331
332	if (!strcasecmp(param,"currnwid")) {		/* set current NWID */
333	    val = strtol(value,&cp,0);
334	    if ((val < 0) || (val > 0xffff) || (cp == value))
335		errx(1,"bad NWID '%s'",value);
336
337	    ifr.ifr_data = (caddr_t)val;
338	    if (ioctl(sd, SIOCSWLCNWID, (caddr_t)&ifr))
339		err(1,"set NWID (interface not up?)");
340	    continue ;
341	}
342
343	if (!strcasecmp(param,"irq")) {
344	    val = strtol(value,&cp,0);
345	    val = irqvals[val];
346	    if ((val == 0) || (cp == value))
347		errx(1,"bad IRQ '%s'",value);
348	    psabuf[WLPSA_IRQNO] = (u_char)val;
349	    work = 1;
350	    continue;
351	}
352
353	if (!strcasecmp(param,"mac")) {
354	    if ((ea = ether_aton(value)) == NULL)
355		errx(1,"bad ethernet address '%s'",value);
356	    for (i = 0; i < 6; i++)
357		psabuf[WLPSA_LOCALMAC + i] = ea->octet[i];
358	    work = 1;
359	    continue;
360	}
361
362	if (!strcasecmp(param,"macsel")) {
363	    if (!strcasecmp(value,"local")) {
364		psabuf[WLPSA_MACSEL] |= 0x1;
365		work = 1;
366		continue;
367	    }
368	    if (!strcasecmp(value,"universal")) {
369		psabuf[WLPSA_MACSEL] &= ~0x1;
370		work = 1;
371		continue;
372	    }
373	    errx(1,"bad macsel value '%s'",value);
374	}
375
376	if (!strcasecmp(param,"nwid")) {
377	    val = strtol(value,&cp,0);
378	    if ((val < 0) || (val > 0xffff) || (cp == value))
379		errx(1,"bad NWID '%s'",value);
380	    psabuf[WLPSA_NWID] = (val >> 8) & 0xff;
381	    psabuf[WLPSA_NWID+1] = val & 0xff;
382	    work = 1;
383	    continue;
384	}
385	if (!strcasecmp(param,"cache")) {
386
387            /* raw cache dump
388	    */
389	    if (!strcasecmp(value,"raw")) {
390	    	get_cache(sd, &ifr);
391		raw_cache();
392		continue;
393	    }
394            /* scaled cache dump
395	    */
396	    else if (!strcasecmp(value,"scale")) {
397	    	get_cache(sd, &ifr);
398		scale_cache();
399		continue;
400	    }
401	    /* zero out cache
402	    */
403	    else if (!strcasecmp(value,"zero")) {
404		if (ioctl(sd, SIOCDWLCACHE, (caddr_t)&ifr))
405		    err(1,"zero cache");
406		continue;
407	    }
408	    errx(1,"unknown value '%s'", value);
409 	}
410	errx(1,"unknown parameter '%s'",param);
411    }
412    if (work) {
413	ifr.ifr_data = (caddr_t)psabuf;
414	if (ioctl(sd, SIOCSWLPSA, (caddr_t)&ifr))
415	    err(1,"set PSA");
416    }
417    return(0);
418}
419