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  "$FreeBSD$";
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>
72
73#include <err.h>
74#include <stdio.h>
75#include <stdlib.h>
76#include <string.h>
77#include <unistd.h>
78#include <limits.h>
79
80/* translate IRQ bit to number */
81/* array for maping irq numbers to values for the irq parameter register */
82static int irqvals[16] = {
83    0, 0, 0, 0x01, 0x02, 0x04, 0, 0x08, 0, 0, 0x10, 0x20, 0x40, 0, 0, 0x80
84};
85
86/* cache */
87static int w_sigitems;	/* count of valid items */
88static struct w_sigcache wsc[MAXCACHEITEMS];
89
90int
91wlirq(int irqval)
92{
93    int irq;
94
95    for(irq = 0; irq < 16; irq++)
96	if(irqvals[irq] == irqval)
97	    return(irq);
98    return 0;
99}
100
101char *compat_type[] = {
102    "PC-AT 915MHz",
103    "PC-MC 915MHz",
104    "PC-AT 2.4GHz",
105    "PC-MC 2.4GHz",
106    "PCCARD or 1/2 size AT, 915MHz or 2.4GHz"
107};
108
109char *subband[] = {
110    "915MHz/see WaveModem",
111    "2425MHz",
112    "2460MHz",
113    "2484MHz",
114    "2430.5MHz"
115};
116
117
118/*
119** print_psa
120**
121** Given a pointer to a PSA structure, print it out
122*/
123void
124print_psa(u_char *psa, int currnwid)
125{
126    int		nwid;
127
128    /*
129    ** Work out what sort of board we have
130    */
131    if (psa[0] == 0x14) {
132	printf("Board type            : Microchannel\n");
133    } else {
134	if (psa[1] == 0) {
135	    printf("Board type            : PCCARD\n");
136	} else {
137	    printf("Board type            : ISA");
138	    if ((psa[4] == 0) &&
139		(psa[5] == 0) &&
140		(psa[6] == 0))
141		printf(" (DEC OEM)");
142	    printf("\n");
143	    printf("Base address options  : 0x300, 0x%02x0, 0x%02x0, 0x%02x0\n",
144		   (int)psa[1], (int)psa[2], (int)psa[3]);
145	    printf("Waitstates            : %d\n",psa[7] & 0xf);
146	    printf("Bus mode              : %s\n",(psa[7] & 0x10) ? "EISA" : "ISA");
147	    printf("IRQ                   : %d\n",wlirq(psa[8]));
148	}
149    }
150    printf("Default MAC address   : %02x:%02x:%02x:%02x:%02x:%02x\n",
151	   psa[0x10],psa[0x11],psa[0x12],psa[0x13],psa[0x14],psa[0x15]);
152    printf("Soft MAC address      : %02x:%02x:%02x:%02x:%02x:%02x\n",
153	   psa[0x16],psa[0x17],psa[0x18],psa[0x19],psa[0x1a],psa[0x1b]);
154    printf("Current MAC address   : %s\n",(psa[0x1c] & 0x1) ? "Soft" : "Default");
155    printf("Adapter compatibility : ");
156    if (psa[0x1d] < 5) {
157	printf("%s\n",compat_type[psa[0x1d]]);
158    } else {
159	printf("unknown\n");
160    }
161    printf("Threshold preset      : %d\n",psa[0x1e]);
162    printf("Call code required    : %s\n",(psa[0x1f] & 0x1) ? "YES" : "NO");
163    if (psa[0x1f] & 0x1)
164	printf("Call code             : 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
165	       psa[0x30],psa[0x31],psa[0x32],psa[0x33],psa[0x34],psa[0x35],psa[0x36],psa[0x37]);
166    printf("Subband               : %s\n",subband[psa[0x20] & 0xf]);
167    printf("Quality threshold     : %d\n",psa[0x21]);
168    printf("Hardware version      : %d (%s)\n",psa[0x22],psa[0x22] ? "Rel3" : "Rel1/Rel2");
169    printf("Network ID enable     : %s\n",(psa[0x25] & 0x1) ? "YES" : "NO");
170    if (psa[0x25] & 0x1) {
171	nwid = (psa[0x23] << 8) + psa[0x24];
172	printf("NWID                  : 0x%04x\n",nwid);
173	if (nwid != currnwid) {
174	    printf("Current NWID          : 0x%04x\n",currnwid);
175	}
176    }
177    printf("Datalink security     : %s\n",(psa[0x26] & 0x1) ? "YES" : "NO");
178    if (psa[0x26] & 0x1) {
179	printf("Encryption key        : ");
180	if (psa[0x27] == 0) {
181	    printf("DENIED\n");
182	} else {
183	    printf("0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
184		   psa[0x27],psa[0x28],psa[0x29],psa[0x2a],psa[0x2b],psa[0x2c],psa[0x2d],psa[0x2e]);
185	}
186    }
187    printf("Databus width         : %d (%s)\n",
188	   (psa[0x2f] & 0x1) ? 16 : 8, (psa[0x2f] & 0x80) ? "fixed" : "variable");
189    printf("Configuration state   : %sconfigured\n",(psa[0x38] & 0x1) ? "" : "un");
190    printf("CRC-16                : 0x%02x%02x\n",psa[0x3e],psa[0x3d]);
191    printf("CRC status            : ");
192    switch(psa[0x3f]) {
193    case 0xaa:
194	printf("OK\n");
195	break;
196    case 0x55:
197	printf("BAD\n");
198	break;
199    default:
200	printf("Error\n");
201	break;
202    }
203}
204
205
206static void
207usage()
208{
209    fprintf(stderr,"usage: wlconfig ifname [param value ...]\n");
210    exit(1);
211}
212
213
214void
215get_cache(int sd, struct ifreq *ifr)
216{
217    /* get the cache count */
218    if (ioctl(sd, SIOCGWLCITEM, (caddr_t)ifr))
219	err(1, "SIOCGWLCITEM - get cache count");
220    w_sigitems = (int) ifr->ifr_data;
221
222    ifr->ifr_data = (caddr_t) &wsc;
223    /* get the cache */
224    if (ioctl(sd, SIOCGWLCACHE, (caddr_t)ifr))
225	err(1, "SIOCGWLCACHE - get cache count");
226}
227
228static int
229scale_value(int value, int max)
230{
231	double dmax = (double) max;
232	if (value > max)
233		return(100);
234	return((value/dmax) * 100);
235}
236
237static void
238dump_cache(int rawFlag)
239{
240	int i;
241	int signal, silence, quality;
242
243	if (rawFlag)
244		printf("signal range 0..63: silence 0..63: quality 0..15\n");
245	else
246		printf("signal range 0..100: silence 0..100: quality 0..100\n");
247
248	/* after you read it, loop through structure,i.e. wsc
249         * print each item:
250	 */
251	for(i = 0; i < w_sigitems; i++) {
252		printf("[%d:%d]>\n", i+1, w_sigitems);
253        	printf("\tip: %d.%d.%d.%d,",((wsc[i].ipsrc >> 0) & 0xff),
254				        ((wsc[i].ipsrc >> 8) & 0xff),
255				        ((wsc[i].ipsrc >> 16) & 0xff),
256				        ((wsc[i].ipsrc >> 24) & 0xff));
257		printf(" mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
258		  		    	wsc[i].macsrc[0]&0xff,
259		  		    	wsc[i].macsrc[1]&0xff,
260		   		    	wsc[i].macsrc[2]&0xff,
261		   			wsc[i].macsrc[3]&0xff,
262		   			wsc[i].macsrc[4]&0xff,
263		   			wsc[i].macsrc[5]&0xff);
264		if (rawFlag) {
265			signal = wsc[i].signal;
266			silence = wsc[i].silence;
267			quality = wsc[i].quality;
268		}
269		else {
270			signal = scale_value(wsc[i].signal, 63);
271			silence = scale_value(wsc[i].silence, 63);
272			quality = scale_value(wsc[i].quality, 15);
273		}
274		printf("\tsignal: %d, silence: %d, quality: %d, ",
275		   			signal,
276		   			silence,
277		   			quality);
278		printf("snr: %d\n", signal - silence);
279	}
280}
281
282#define raw_cache()	dump_cache(1)
283#define scale_cache()	dump_cache(0)
284
285int
286main(int argc, char *argv[])
287{
288    int 		sd;
289    struct ifreq	ifr;
290    u_char		psabuf[0x40];
291    int			val, argind, i;
292    char		*cp, *param, *value;
293    struct ether_addr	*ea;
294    int			work = 0;
295    int			currnwid;
296
297    if ((argc < 2) || (argc % 2))
298	usage();
299
300    /* get a socket */
301    sd = socket(AF_INET, SOCK_DGRAM, 0);
302    if (sd < 0)
303	err(1,"socket");
304    strncpy(ifr.ifr_name, argv[1], sizeof(ifr.ifr_name));
305    ifr.ifr_addr.sa_family = AF_INET;
306
307    /* get the PSA */
308    ifr.ifr_data = (caddr_t)psabuf;
309    if (ioctl(sd, SIOCGWLPSA, (caddr_t)&ifr))
310	err(1,"get PSA");
311
312    /* get the current NWID */
313    if (ioctl(sd, SIOCGWLCNWID, (caddr_t)&ifr))
314	err(1,"get NWID");
315    currnwid = (int)ifr.ifr_data;
316
317    /* just dump and exit? */
318    if (argc == 2) {
319	print_psa(psabuf, currnwid);
320	exit(0);
321    }
322
323    /* loop reading arg pairs */
324    for (argind = 2; argind < argc; argind += 2) {
325
326	param = argv[argind];
327	value = argv[argind+1];
328
329	/* What to do? */
330
331	if (!strcasecmp(param,"currnwid")) {		/* set current NWID */
332	    val = strtol(value,&cp,0);
333	    if ((val < 0) || (val > 0xffff) || (cp == value))
334		errx(1,"bad NWID '%s'",value);
335
336	    ifr.ifr_data = (caddr_t)val;
337	    if (ioctl(sd, SIOCSWLCNWID, (caddr_t)&ifr))
338		err(1,"set NWID (interface not up?)");
339	    continue ;
340	}
341
342	if (!strcasecmp(param,"irq")) {
343	    val = strtol(value,&cp,0);
344	    val = irqvals[val];
345	    if ((val == 0) || (cp == value))
346		errx(1,"bad IRQ '%s'",value);
347	    psabuf[WLPSA_IRQNO] = (u_char)val;
348	    work = 1;
349	    continue;
350	}
351
352	if (!strcasecmp(param,"mac")) {
353	    if ((ea = ether_aton(value)) == NULL)
354		errx(1,"bad ethernet address '%s'",value);
355	    for (i = 0; i < 6; i++)
356		psabuf[WLPSA_LOCALMAC + i] = ea->octet[i];
357	    work = 1;
358	    continue;
359	}
360
361	if (!strcasecmp(param,"macsel")) {
362	    if (!strcasecmp(value,"local")) {
363		psabuf[WLPSA_MACSEL] |= 0x1;
364		work = 1;
365		continue;
366	    }
367	    if (!strcasecmp(value,"universal")) {
368		psabuf[WLPSA_MACSEL] &= ~0x1;
369		work = 1;
370		continue;
371	    }
372	    errx(1,"bad macsel value '%s'",value);
373	}
374
375	if (!strcasecmp(param,"nwid")) {
376	    val = strtol(value,&cp,0);
377	    if ((val < 0) || (val > 0xffff) || (cp == value))
378		errx(1,"bad NWID '%s'",value);
379	    psabuf[WLPSA_NWID] = (val >> 8) & 0xff;
380	    psabuf[WLPSA_NWID+1] = val & 0xff;
381	    work = 1;
382	    continue;
383	}
384	if (!strcasecmp(param,"cache")) {
385
386            /* raw cache dump
387	    */
388	    if (!strcasecmp(value,"raw")) {
389	    	get_cache(sd, &ifr);
390		raw_cache();
391		continue;
392	    }
393            /* scaled cache dump
394	    */
395	    else if (!strcasecmp(value,"scale")) {
396	    	get_cache(sd, &ifr);
397		scale_cache();
398		continue;
399	    }
400	    /* zero out cache
401	    */
402	    else if (!strcasecmp(value,"zero")) {
403		if (ioctl(sd, SIOCDWLCACHE, (caddr_t)&ifr))
404		    err(1,"zero cache");
405		continue;
406	    }
407	    errx(1,"unknown value '%s'", value);
408 	}
409	errx(1,"unknown parameter '%s'",param);
410    }
411    if (work) {
412	ifr.ifr_data = (caddr_t)psabuf;
413	if (ioctl(sd, SIOCSWLPSA, (caddr_t)&ifr))
414	    err(1,"set PSA");
415    }
416    return(0);
417}
418