ancontrol.c revision 81586
1/*
2 * Copyright 1997, 1998, 1999
3 *	Bill Paul <wpaul@ee.columbia.edu>.  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 Bill Paul 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 Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef lint
34static const char copyright[] = "@(#) Copyright (c) 1997, 1998, 1999\
35	Bill Paul. All rights reserved.";
36static const char rcsid[] =
37  "$FreeBSD: head/usr.sbin/ancontrol/ancontrol.c 81586 2001-08-13 14:06:34Z ru $";
38#endif
39
40#include <sys/types.h>
41#include <sys/cdefs.h>
42#include <sys/param.h>
43#include <sys/socket.h>
44#include <sys/ioctl.h>
45#include <sys/socket.h>
46
47#include <net/if.h>
48#include <net/if_var.h>
49#include <net/ethernet.h>
50
51#include <dev/an/if_aironet_ieee.h>
52
53#include <stdio.h>
54#include <string.h>
55#include <stdlib.h>
56#include <unistd.h>
57#include <errno.h>
58#include <err.h>
59
60static void an_getval		__P((const char *, struct an_req *));
61static void an_setval		__P((const char *, struct an_req *));
62static void an_printwords	__P((u_int16_t *, int));
63static void an_printspeeds	__P((u_int8_t*, int));
64static void an_printbool	__P((int));
65static void an_printhex		__P((char *, int));
66static void an_printstr		__P((char *, int));
67static void an_dumpstatus	__P((const char *));
68static void an_dumpstats	__P((const char *));
69static void an_dumpconfig	__P((const char *));
70static void an_dumpcaps		__P((const char *));
71static void an_dumpssid		__P((const char *));
72static void an_dumpap		__P((const char *));
73static void an_setconfig	__P((const char *, int, void *));
74static void an_setssid		__P((const char *, int, void *));
75static void an_setap		__P((const char *, int, void *));
76static void an_setspeed		__P((const char *, int, void *));
77static void an_readkeyinfo	__P((const char *));
78#ifdef ANCACHE
79static void an_zerocache	__P((const char *));
80static void an_readcache	__P((const char *));
81#endif
82static int an_hex2int		__P((char));
83static void an_str2key		__P((char *, struct an_ltv_key *));
84static void an_setkeys		__P((const char *, char *, int));
85static void an_enable_tx_key	__P((const char *, char *));
86static void usage		__P((char *));
87int main			__P((int, char **));
88
89#define ACT_DUMPSTATS 1
90#define ACT_DUMPCONFIG 2
91#define ACT_DUMPSTATUS 3
92#define ACT_DUMPCAPS 4
93#define ACT_DUMPSSID 5
94#define ACT_DUMPAP 6
95
96#define ACT_SET_OPMODE 7
97#define ACT_SET_SSID1 8
98#define ACT_SET_SSID2 9
99#define ACT_SET_SSID3 10
100#define ACT_SET_FREQ 11
101#define ACT_SET_AP1 12
102#define ACT_SET_AP2 13
103#define ACT_SET_AP3 14
104#define ACT_SET_AP4 15
105#define ACT_SET_DRIVERNAME 16
106#define ACT_SET_SCANMODE 17
107#define ACT_SET_TXRATE 18
108#define ACT_SET_RTS_THRESH 19
109#define ACT_SET_PWRSAVE 20
110#define ACT_SET_DIVERSITY_RX 21
111#define ACT_SET_DIVERSITY_TX 22
112#define ACT_SET_RTS_RETRYLIM 23
113#define ACT_SET_WAKE_DURATION 24
114#define ACT_SET_BEACON_PERIOD 25
115#define ACT_SET_TXPWR 26
116#define ACT_SET_FRAG_THRESH 27
117#define ACT_SET_NETJOIN 28
118#define ACT_SET_MYNAME 29
119#define ACT_SET_MAC 30
120
121#define ACT_DUMPCACHE 31
122#define ACT_ZEROCACHE 32
123
124#define ACT_ENABLE_WEP 33
125#define ACT_SET_KEY_TYPE 34
126#define ACT_SET_KEYS 35
127#define ACT_ENABLE_TX_KEY 36
128
129static void an_getval(iface, areq)
130	const char		*iface;
131	struct an_req		*areq;
132{
133	struct ifreq		ifr;
134	int			s;
135
136	bzero((char *)&ifr, sizeof(ifr));
137
138	strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
139	ifr.ifr_data = (caddr_t)areq;
140
141	s = socket(AF_INET, SOCK_DGRAM, 0);
142
143	if (s == -1)
144		err(1, "socket");
145
146	if (ioctl(s, SIOCGAIRONET, &ifr) == -1)
147		err(1, "SIOCGAIRONET");
148
149	close(s);
150
151	return;
152}
153
154static void an_setval(iface, areq)
155	const char		*iface;
156	struct an_req		*areq;
157{
158	struct ifreq		ifr;
159	int			s;
160
161	bzero((char *)&ifr, sizeof(ifr));
162
163	strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
164	ifr.ifr_data = (caddr_t)areq;
165
166	s = socket(AF_INET, SOCK_DGRAM, 0);
167
168	if (s == -1)
169		err(1, "socket");
170
171	if (ioctl(s, SIOCSAIRONET, &ifr) == -1)
172		err(1, "SIOCSAIRONET");
173
174	close(s);
175
176	return;
177}
178
179static void an_printstr(str, len)
180	char			*str;
181	int			len;
182{
183	int			i;
184
185	for (i = 0; i < len - 1; i++) {
186		if (str[i] == '\0')
187			str[i] = ' ';
188	}
189
190	printf("[ %.*s ]", len, str);
191
192	return;
193}
194
195static void an_printwords(w, len)
196	u_int16_t		*w;
197	int			len;
198{
199	int			i;
200
201	printf("[ ");
202	for (i = 0; i < len; i++)
203		printf("%d ", w[i]);
204	printf("]");
205
206	return;
207}
208
209static void an_printspeeds(w, len)
210	u_int8_t		*w;
211	int			len;
212{
213	int			i;
214
215	printf("[ ");
216	for (i = 0; i < len && w[i]; i++)
217		printf("%2.1fMbps ", w[i] * 0.500);
218	printf("]");
219
220	return;
221}
222
223static void an_printbool(val)
224	int			val;
225{
226	if (val)
227		printf("[ On ]");
228	else
229		printf("[ Off ]");
230
231	return;
232}
233
234static void an_printhex(ptr, len)
235	char			*ptr;
236	int			len;
237{
238	int			i;
239
240	printf("[ ");
241	for (i = 0; i < len; i++) {
242		printf("%02x", ptr[i] & 0xFF);
243		if (i < (len - 1))
244			printf(":");
245	}
246
247	printf(" ]");
248	return;
249}
250
251
252
253static void an_dumpstatus(iface)
254	const char		*iface;
255{
256	struct an_ltv_status	*sts;
257	struct an_req		areq;
258
259	areq.an_len = sizeof(areq);
260	areq.an_type = AN_RID_STATUS;
261
262	an_getval(iface, &areq);
263
264	sts = (struct an_ltv_status *)&areq;
265
266	printf("MAC address:\t\t");
267	an_printhex((char *)&sts->an_macaddr, ETHER_ADDR_LEN);
268	printf("\nOperating mode:\t\t[ ");
269	if (sts->an_opmode & AN_STATUS_OPMODE_CONFIGURED)
270		printf("configured ");
271	if (sts->an_opmode & AN_STATUS_OPMODE_MAC_ENABLED)
272		printf("MAC ON ");
273	if (sts->an_opmode & AN_STATUS_OPMODE_RX_ENABLED)
274		printf("RX ON ");
275	if (sts->an_opmode & AN_STATUS_OPMODE_IN_SYNC)
276		printf("synced ");
277	if (sts->an_opmode & AN_STATUS_OPMODE_ASSOCIATED)
278		printf("associated ");
279	if (sts->an_opmode & AN_STATUS_OPMODE_ERROR)
280		printf("error ");
281	printf("]\n");
282	printf("Error code:\t\t");
283	an_printhex((char *)&sts->an_errcode, 1);
284	printf("\nSignal quality:\t\t");
285	an_printhex((char *)&sts->an_cur_signal_quality, 1);
286	/*
287	 * XXX: This uses the old definition of the rate field (units of
288	 * 500kbps).  Technically the new definition is that this field
289	 * contains arbitrary values, but no devices which need this
290	 * support exist and the IEEE seems to intend to use the old
291	 * definition until they get something big so we'll keep using
292	 * it as well because this will work with new cards with
293	 * rate <= 63.5Mbps.
294	 */
295	printf("\nCurrent TX rate:\t[ %d%s ]", sts->an_current_tx_rate / 2,
296	    (sts->an_current_tx_rate % 2) ? ".5" : "");
297	printf("\nCurrent SSID:\t\t");
298	an_printstr((char *)&sts->an_ssid, sts->an_ssidlen);
299	printf("\nCurrent AP name:\t");
300	an_printstr((char *)&sts->an_ap_name, 16);
301	printf("\nCurrent BSSID:\t\t");
302	an_printhex((char *)&sts->an_cur_bssid, ETHER_ADDR_LEN);
303	printf("\nBeacon period:\t\t");
304	an_printwords(&sts->an_beacon_period, 1);
305	printf("\nDTIM period:\t\t");
306	an_printwords(&sts->an_dtim_period, 1);
307	printf("\nATIM duration:\t\t");
308	an_printwords(&sts->an_atim_duration, 1);
309	printf("\nHOP period:\t\t");
310	an_printwords(&sts->an_hop_period, 1);
311	printf("\nChannel set:\t\t");
312	an_printwords(&sts->an_channel_set, 1);
313	printf("\nCurrent channel:\t");
314	an_printwords(&sts->an_cur_channel, 1);
315	printf("\nHops to backbone:\t");
316	an_printwords(&sts->an_hops_to_backbone, 1);
317	printf("\nTotal AP load:\t\t");
318	an_printwords(&sts->an_ap_total_load, 1);
319	printf("\nOur generated load:\t");
320	an_printwords(&sts->an_our_generated_load, 1);
321	printf("\nAccumulated ARL:\t");
322	an_printwords(&sts->an_accumulated_arl, 1);
323	printf("\n");
324	return;
325}
326
327static void an_dumpcaps(iface)
328	const char		*iface;
329{
330	struct an_ltv_caps	*caps;
331	struct an_req		areq;
332	u_int16_t		tmp;
333
334	areq.an_len = sizeof(areq);
335	areq.an_type = AN_RID_CAPABILITIES;
336
337	an_getval(iface, &areq);
338
339	caps = (struct an_ltv_caps *)&areq;
340
341	printf("OUI:\t\t\t");
342	an_printhex((char *)&caps->an_oui, 3);
343	printf("\nProduct number:\t\t");
344	an_printwords(&caps->an_prodnum, 1);
345	printf("\nManufacturer name:\t");
346	an_printstr((char *)&caps->an_manufname, 32);
347	printf("\nProduce name:\t\t");
348	an_printstr((char *)&caps->an_prodname, 16);
349	printf("\nFirmware version:\t");
350	an_printstr((char *)&caps->an_prodvers, 1);
351	printf("\nOEM MAC address:\t");
352	an_printhex((char *)&caps->an_oemaddr, ETHER_ADDR_LEN);
353	printf("\nAironet MAC address:\t");
354	an_printhex((char *)&caps->an_aironetaddr, ETHER_ADDR_LEN);
355	printf("\nRadio type:\t\t[ ");
356	if (caps->an_radiotype & AN_RADIOTYPE_80211_FH)
357		printf("802.11 FH");
358	else if (caps->an_radiotype & AN_RADIOTYPE_80211_DS)
359		printf("802.11 DS");
360	else if (caps->an_radiotype & AN_RADIOTYPE_LM2000_DS)
361		printf("LM2000 DS");
362	else
363		printf("unknown (%x)", caps->an_radiotype);
364	printf(" ]");
365	printf("\nRegulatory domain:\t");
366	an_printwords(&caps->an_regdomain, 1);
367	printf("\nAssigned CallID:\t");
368	an_printhex((char *)&caps->an_callid, 6);
369	printf("\nSupported speeds:\t");
370	an_printspeeds(caps->an_rates, 8);
371	printf("\nRX Diversity:\t\t[ ");
372	if (caps->an_rx_diversity == AN_DIVERSITY_ANTENNA_1_ONLY)
373		printf("antenna 1 only");
374	else if (caps->an_rx_diversity == AN_DIVERSITY_ANTENNA_2_ONLY)
375		printf("antenna 2 only");
376	else if (caps->an_rx_diversity == AN_DIVERSITY_ANTENNA_1_AND_2)
377		printf("antenna 1 and 2");
378	printf(" ]");
379	printf("\nTX Diversity:\t\t[ ");
380	if (caps->an_rx_diversity == AN_DIVERSITY_ANTENNA_1_ONLY)
381		printf("antenna 1 only");
382	else if (caps->an_rx_diversity == AN_DIVERSITY_ANTENNA_2_ONLY)
383		printf("antenna 2 only");
384	else if (caps->an_rx_diversity == AN_DIVERSITY_ANTENNA_1_AND_2)
385		printf("antenna 1 and 2");
386	printf(" ]");
387	printf("\nSupported power levels:\t");
388	an_printwords(caps->an_tx_powerlevels, 8);
389	printf("\nHardware revision:\t");
390	tmp = ntohs(caps->an_hwrev);
391	an_printhex((char *)&tmp, 2);
392	printf("\nSoftware revision:\t");
393	tmp = ntohs(caps->an_fwrev);
394	an_printhex((char *)&tmp, 2);
395	printf("\nSoftware subrevision:\t");
396	tmp = ntohs(caps->an_fwsubrev);
397	an_printhex((char *)&tmp, 2);
398	printf("\nInterface revision:\t");
399	tmp = ntohs(caps->an_ifacerev);
400	an_printhex((char *)&tmp, 2);
401	printf("\nBootblock revision:\t");
402	tmp = ntohs(caps->an_bootblockrev);
403	an_printhex((char *)&tmp, 2);
404	printf("\n");
405	return;
406}
407
408static void an_dumpstats(iface)
409	const char		*iface;
410{
411	struct an_ltv_stats	*stats;
412	struct an_req		areq;
413	caddr_t			ptr;
414
415	areq.an_len = sizeof(areq);
416	areq.an_type = AN_RID_32BITS_CUM;
417
418	an_getval(iface, &areq);
419
420	ptr = (caddr_t)&areq;
421	ptr -= 2;
422	stats = (struct an_ltv_stats *)ptr;
423
424	printf("RX overruns:\t\t\t\t\t[ %d ]\n", stats->an_rx_overruns);
425	printf("RX PLCP CSUM errors:\t\t\t\t[ %d ]\n",
426	    stats->an_rx_plcp_csum_errs);
427	printf("RX PLCP format errors:\t\t\t\t[ %d ]\n",
428	    stats->an_rx_plcp_format_errs);
429	printf("RX PLCP length errors:\t\t\t\t[ %d ]\n",
430	    stats->an_rx_plcp_len_errs);
431	printf("RX MAC CRC errors:\t\t\t\t[ %d ]\n",
432	    stats->an_rx_mac_crc_errs);
433	printf("RX MAC CRC OK:\t\t\t\t\t[ %d ]\n",
434	    stats->an_rx_mac_crc_ok);
435	printf("RX WEP errors:\t\t\t\t\t[ %d ]\n",
436	    stats->an_rx_wep_errs);
437	printf("RX WEP OK:\t\t\t\t\t[ %d ]\n",
438	    stats->an_rx_wep_ok);
439	printf("Long retries:\t\t\t\t\t[ %d ]\n",
440	    stats->an_retry_long);
441	printf("Short retries:\t\t\t\t\t[ %d ]\n",
442	    stats->an_retry_short);
443	printf("Retries exhausted:\t\t\t\t[ %d ]\n",
444	    stats->an_retry_max);
445	printf("Bad ACK:\t\t\t\t\t[ %d ]\n",
446	    stats->an_no_ack);
447	printf("Bad CTS:\t\t\t\t\t[ %d ]\n",
448	    stats->an_no_cts);
449	printf("RX good ACKs:\t\t\t\t\t[ %d ]\n",
450	    stats->an_rx_ack_ok);
451	printf("RX good CTSs:\t\t\t\t\t[ %d ]\n",
452	    stats->an_rx_cts_ok);
453	printf("TX good ACKs:\t\t\t\t\t[ %d ]\n",
454	    stats->an_tx_ack_ok);
455	printf("TX good RTSs:\t\t\t\t\t[ %d ]\n",
456	    stats->an_tx_rts_ok);
457	printf("TX good CTSs:\t\t\t\t\t[ %d ]\n",
458	    stats->an_tx_cts_ok);
459	printf("LMAC multicasts transmitted:\t\t\t[ %d ]\n",
460	    stats->an_tx_lmac_mcasts);
461	printf("LMAC broadcasts transmitted:\t\t\t[ %d ]\n",
462	    stats->an_tx_lmac_bcasts);
463	printf("LMAC unicast frags transmitted:\t\t\t[ %d ]\n",
464	    stats->an_tx_lmac_ucast_frags);
465	printf("LMAC unicasts transmitted:\t\t\t[ %d ]\n",
466	    stats->an_tx_lmac_ucasts);
467	printf("Beacons transmitted:\t\t\t\t[ %d ]\n",
468	    stats->an_tx_beacons);
469	printf("Beacons received:\t\t\t\t[ %d ]\n",
470	    stats->an_rx_beacons);
471	printf("Single transmit collisions:\t\t\t[ %d ]\n",
472	    stats->an_tx_single_cols);
473	printf("Multiple transmit collisions:\t\t\t[ %d ]\n",
474	    stats->an_tx_multi_cols);
475	printf("Transmits without deferrals:\t\t\t[ %d ]\n",
476	    stats->an_tx_defers_no);
477	printf("Transmits deferred due to protocol:\t\t[ %d ]\n",
478	    stats->an_tx_defers_prot);
479	printf("Transmits deferred due to energy detect:\t\t[ %d ]\n",
480	    stats->an_tx_defers_energy);
481	printf("RX duplicate frames/frags:\t\t\t[ %d ]\n",
482	    stats->an_rx_dups);
483	printf("RX partial frames:\t\t\t\t[ %d ]\n",
484	    stats->an_rx_partial);
485	printf("TX max lifetime exceeded:\t\t\t[ %d ]\n",
486	    stats->an_tx_too_old);
487	printf("RX max lifetime exceeded:\t\t\t[ %d ]\n",
488	    stats->an_tx_too_old);
489	printf("Sync lost due to too many missed beacons:\t[ %d ]\n",
490	    stats->an_lostsync_missed_beacons);
491	printf("Sync lost due to ARL exceeded:\t\t\t[ %d ]\n",
492	    stats->an_lostsync_arl_exceeded);
493	printf("Sync lost due to deauthentication:\t\t[ %d ]\n",
494	    stats->an_lostsync_deauthed);
495	printf("Sync lost due to disassociation:\t\t[ %d ]\n",
496	    stats->an_lostsync_disassociated);
497	printf("Sync lost due to excess change in TSF timing:\t[ %d ]\n",
498	    stats->an_lostsync_tsf_timing);
499	printf("Host transmitted multicasts:\t\t\t[ %d ]\n",
500	    stats->an_tx_host_mcasts);
501	printf("Host transmitted broadcasts:\t\t\t[ %d ]\n",
502	    stats->an_tx_host_bcasts);
503	printf("Host transmitted unicasts:\t\t\t[ %d ]\n",
504	    stats->an_tx_host_ucasts);
505	printf("Host transmission failures:\t\t\t[ %d ]\n",
506	    stats->an_tx_host_failed);
507	printf("Host received multicasts:\t\t\t[ %d ]\n",
508	    stats->an_rx_host_mcasts);
509	printf("Host received broadcasts:\t\t\t[ %d ]\n",
510	    stats->an_rx_host_bcasts);
511	printf("Host received unicasts:\t\t\t\t[ %d ]\n",
512	    stats->an_rx_host_ucasts);
513	printf("Host receive discards:\t\t\t\t[ %d ]\n",
514	    stats->an_rx_host_discarded);
515	printf("HMAC transmitted multicasts:\t\t\t[ %d ]\n",
516	    stats->an_tx_hmac_mcasts);
517	printf("HMAC transmitted broadcasts:\t\t\t[ %d ]\n",
518	    stats->an_tx_hmac_bcasts);
519	printf("HMAC transmitted unicasts:\t\t\t[ %d ]\n",
520	    stats->an_tx_hmac_ucasts);
521	printf("HMAC transmissions failed:\t\t\t[ %d ]\n",
522	    stats->an_tx_hmac_failed);
523	printf("HMAC received multicasts:\t\t\t[ %d ]\n",
524	    stats->an_rx_hmac_mcasts);
525	printf("HMAC received broadcasts:\t\t\t[ %d ]\n",
526	    stats->an_rx_hmac_bcasts);
527	printf("HMAC received unicasts:\t\t\t\t[ %d ]\n",
528	    stats->an_rx_hmac_ucasts);
529	printf("HMAC receive discards:\t\t\t\t[ %d ]\n",
530	    stats->an_rx_hmac_discarded);
531	printf("HMAC transmits accepted:\t\t\t[ %d ]\n",
532	    stats->an_tx_hmac_accepted);
533	printf("SSID mismatches:\t\t\t\t[ %d ]\n",
534	    stats->an_ssid_mismatches);
535	printf("Access point mismatches:\t\t\t[ %d ]\n",
536	    stats->an_ap_mismatches);
537	printf("Speed mismatches:\t\t\t\t[ %d ]\n",
538	    stats->an_rates_mismatches);
539	printf("Authentication rejects:\t\t\t\t[ %d ]\n",
540	    stats->an_auth_rejects);
541	printf("Authentication timeouts:\t\t\t[ %d ]\n",
542	    stats->an_auth_timeouts);
543	printf("Association rejects:\t\t\t\t[ %d ]\n",
544	    stats->an_assoc_rejects);
545	printf("Association timeouts:\t\t\t\t[ %d ]\n",
546	    stats->an_assoc_timeouts);
547	printf("Management frames received:\t\t\t[ %d ]\n",
548	    stats->an_rx_mgmt_pkts);
549	printf("Management frames transmitted:\t\t\t[ %d ]\n",
550	    stats->an_tx_mgmt_pkts);
551	printf("Refresh frames received:\t\t\t[ %d ]\n",
552	    stats->an_rx_refresh_pkts),
553	printf("Refresh frames transmitted:\t\t\t[ %d ]\n",
554	    stats->an_tx_refresh_pkts),
555	printf("Poll frames received:\t\t\t\t[ %d ]\n",
556	    stats->an_rx_poll_pkts);
557	printf("Poll frames transmitted:\t\t\t[ %d ]\n",
558	    stats->an_tx_poll_pkts);
559	printf("Host requested sync losses:\t\t\t[ %d ]\n",
560	    stats->an_lostsync_hostreq);
561	printf("Host transmitted bytes:\t\t\t\t[ %d ]\n",
562	    stats->an_host_tx_bytes);
563	printf("Host received bytes:\t\t\t\t[ %d ]\n",
564	    stats->an_host_rx_bytes);
565	printf("Uptime in microseconds:\t\t\t\t[ %d ]\n",
566	    stats->an_uptime_usecs);
567	printf("Uptime in seconds:\t\t\t\t[ %d ]\n",
568	    stats->an_uptime_secs);
569	printf("Sync lost due to better AP:\t\t\t[ %d ]\n",
570	    stats->an_lostsync_better_ap);
571
572	return;
573}
574
575static void an_dumpap(iface)
576	const char		*iface;
577{
578	struct an_ltv_aplist	*ap;
579	struct an_req		areq;
580
581	areq.an_len = sizeof(areq);
582	areq.an_type = AN_RID_APLIST;
583
584	an_getval(iface, &areq);
585
586	ap = (struct an_ltv_aplist *)&areq;
587	printf("Access point 1:\t\t\t");
588	an_printhex((char *)&ap->an_ap1, ETHER_ADDR_LEN);
589	printf("\nAccess point 2:\t\t\t");
590	an_printhex((char *)&ap->an_ap2, ETHER_ADDR_LEN);
591	printf("\nAccess point 3:\t\t\t");
592	an_printhex((char *)&ap->an_ap3, ETHER_ADDR_LEN);
593	printf("\nAccess point 4:\t\t\t");
594	an_printhex((char *)&ap->an_ap4, ETHER_ADDR_LEN);
595	printf("\n");
596
597	return;
598}
599
600static void an_dumpssid(iface)
601	const char		*iface;
602{
603	struct an_ltv_ssidlist	*ssid;
604	struct an_req		areq;
605
606	areq.an_len = sizeof(areq);
607	areq.an_type = AN_RID_SSIDLIST;
608
609	an_getval(iface, &areq);
610
611	ssid = (struct an_ltv_ssidlist *)&areq;
612	printf("SSID 1:\t\t\t[ %.*s ]\n", ssid->an_ssid1_len, ssid->an_ssid1);
613	printf("SSID 2:\t\t\t[ %.*s ]\n", ssid->an_ssid2_len, ssid->an_ssid2);
614	printf("SSID 3:\t\t\t[ %.*s ]\n", ssid->an_ssid3_len, ssid->an_ssid3);
615
616	return;
617}
618
619static void an_dumpconfig(iface)
620	const char		*iface;
621{
622	struct an_ltv_genconfig	*cfg;
623	struct an_req		areq;
624	unsigned char		diversity;
625
626	areq.an_len = sizeof(areq);
627	areq.an_type = AN_RID_ACTUALCFG;
628
629	an_getval(iface, &areq);
630
631	cfg = (struct an_ltv_genconfig *)&areq;
632
633	printf("Operating mode:\t\t\t\t[ ");
634	if ((cfg->an_opmode & 0x7) == AN_OPMODE_IBSS_ADHOC)
635		printf("ad-hoc");
636	if ((cfg->an_opmode & 0x7) == AN_OPMODE_INFRASTRUCTURE_STATION)
637		printf("infrastructure");
638	if ((cfg->an_opmode & 0x7) == AN_OPMODE_AP)
639		printf("access point");
640	if ((cfg->an_opmode & 0x7) == AN_OPMODE_AP_REPEATER)
641		printf("access point repeater");
642	printf(" ]");
643	printf("\nReceive mode:\t\t\t\t[ ");
644	if ((cfg->an_rxmode & 0x7) == AN_RXMODE_BC_MC_ADDR)
645		printf("broadcast/multicast/unicast");
646	if ((cfg->an_rxmode & 0x7) == AN_RXMODE_BC_ADDR)
647		printf("broadcast/unicast");
648	if ((cfg->an_rxmode & 0x7) == AN_RXMODE_ADDR)
649		printf("unicast");
650	if ((cfg->an_rxmode & 0x7) == AN_RXMODE_80211_MONITOR_CURBSS)
651		printf("802.11 monitor, current BSSID");
652	if ((cfg->an_rxmode & 0x7) == AN_RXMODE_80211_MONITOR_ANYBSS)
653		printf("802.11 monitor, any BSSID");
654	if ((cfg->an_rxmode & 0x7) == AN_RXMODE_LAN_MONITOR_CURBSS)
655		printf("LAN monitor, current BSSID");
656	printf(" ]");
657	printf("\nFragment threshold:\t\t\t");
658	an_printwords(&cfg->an_fragthresh, 1);
659	printf("\nRTS threshold:\t\t\t\t");
660	an_printwords(&cfg->an_rtsthresh, 1);
661	printf("\nMAC address:\t\t\t\t");
662	an_printhex((char *)&cfg->an_macaddr, ETHER_ADDR_LEN);
663	printf("\nSupported rates:\t\t\t");
664	an_printspeeds(cfg->an_rates, 8);
665	printf("\nShort retry limit:\t\t\t");
666	an_printwords(&cfg->an_shortretry_limit, 1);
667	printf("\nLong retry limit:\t\t\t");
668	an_printwords(&cfg->an_longretry_limit, 1);
669	printf("\nTX MSDU lifetime:\t\t\t");
670	an_printwords(&cfg->an_tx_msdu_lifetime, 1);
671	printf("\nRX MSDU lifetime:\t\t\t");
672	an_printwords(&cfg->an_rx_msdu_lifetime, 1);
673	printf("\nStationary:\t\t\t\t");
674	an_printbool(cfg->an_stationary);
675	printf("\nOrdering:\t\t\t\t");
676	an_printbool(cfg->an_ordering);
677	printf("\nDevice type:\t\t\t\t[ ");
678	if (cfg->an_devtype == AN_DEVTYPE_PC4500)
679		printf("PC4500");
680	else if (cfg->an_devtype == AN_DEVTYPE_PC4800)
681		printf("PC4800");
682	else
683		printf("unknown (%x)", cfg->an_devtype);
684	printf(" ]");
685	printf("\nScanning mode:\t\t\t\t[ ");
686	if (cfg->an_scanmode == AN_SCANMODE_ACTIVE)
687		printf("active");
688	if (cfg->an_scanmode == AN_SCANMODE_PASSIVE)
689		printf("passive");
690	if (cfg->an_scanmode == AN_SCANMODE_AIRONET_ACTIVE)
691		printf("Aironet active");
692	printf(" ]");
693	printf("\nProbe delay:\t\t\t\t");
694	an_printwords(&cfg->an_probedelay, 1);
695	printf("\nProbe energy timeout:\t\t\t");
696	an_printwords(&cfg->an_probe_energy_timeout, 1);
697	printf("\nProbe response timeout:\t\t\t");
698	an_printwords(&cfg->an_probe_response_timeout, 1);
699	printf("\nBeacon listen timeout:\t\t\t");
700	an_printwords(&cfg->an_beacon_listen_timeout, 1);
701	printf("\nIBSS join network timeout:\t\t");
702	an_printwords(&cfg->an_ibss_join_net_timeout, 1);
703	printf("\nAuthentication timeout:\t\t\t");
704	an_printwords(&cfg->an_auth_timeout, 1);
705	printf("\nWEP enabled:\t\t\t\t[ ");
706	if (cfg->an_authtype & AN_AUTHTYPE_PRIVACY_IN_USE)
707	{
708		if (cfg->an_authtype & AN_AUTHTYPE_ALLOW_UNENCRYPTED)
709			 printf("mixed cell");
710		else
711			 printf("full");
712	}
713	else
714		printf("no");
715	printf(" ]");
716	printf("\nAuthentication type:\t\t\t[ ");
717	if ((cfg->an_authtype & AN_AUTHTYPE_MASK) == AN_AUTHTYPE_NONE)
718		printf("none");
719	if ((cfg->an_authtype & AN_AUTHTYPE_MASK) == AN_AUTHTYPE_OPEN)
720		printf("open");
721	if ((cfg->an_authtype & AN_AUTHTYPE_MASK) == AN_AUTHTYPE_SHAREDKEY)
722		printf("shared key");
723	printf(" ]");
724	printf("\nAssociation timeout:\t\t\t");
725	an_printwords(&cfg->an_assoc_timeout, 1);
726	printf("\nSpecified AP association timeout:\t");
727	an_printwords(&cfg->an_specified_ap_timeout, 1);
728	printf("\nOffline scan interval:\t\t\t");
729	an_printwords(&cfg->an_offline_scan_interval, 1);
730	printf("\nOffline scan duration:\t\t\t");
731	an_printwords(&cfg->an_offline_scan_duration, 1);
732	printf("\nLink loss delay:\t\t\t");
733	an_printwords(&cfg->an_link_loss_delay, 1);
734	printf("\nMax beacon loss time:\t\t\t");
735	an_printwords(&cfg->an_max_beacon_lost_time, 1);
736	printf("\nRefresh interval:\t\t\t");
737	an_printwords(&cfg->an_refresh_interval, 1);
738	printf("\nPower save mode:\t\t\t[ ");
739	if (cfg->an_psave_mode == AN_PSAVE_NONE)
740		printf("none");
741	if (cfg->an_psave_mode == AN_PSAVE_CAM)
742		printf("constantly awake mode");
743	if (cfg->an_psave_mode == AN_PSAVE_PSP)
744		printf("PSP");
745	if (cfg->an_psave_mode == AN_PSAVE_PSP_CAM)
746		printf("PSP-CAM (fast PSP)");
747	printf(" ]");
748	printf("\nSleep through DTIMs:\t\t\t");
749	an_printbool(cfg->an_sleep_for_dtims);
750	printf("\nPower save listen interval:\t\t");
751	an_printwords(&cfg->an_listen_interval, 1);
752	printf("\nPower save fast listen interval:\t");
753	an_printwords(&cfg->an_fast_listen_interval, 1);
754	printf("\nPower save listen decay:\t\t");
755	an_printwords(&cfg->an_listen_decay, 1);
756	printf("\nPower save fast listen decay:\t\t");
757	an_printwords(&cfg->an_fast_listen_decay, 1);
758	printf("\nAP/ad-hoc Beacon period:\t\t");
759	an_printwords(&cfg->an_beacon_period, 1);
760	printf("\nAP/ad-hoc ATIM duration:\t\t");
761	an_printwords(&cfg->an_atim_duration, 1);
762	printf("\nAP/ad-hoc current channel:\t\t");
763	an_printwords(&cfg->an_ds_channel, 1);
764	printf("\nAP/ad-hoc DTIM period:\t\t\t");
765	an_printwords(&cfg->an_dtim_period, 1);
766	printf("\nRadio type:\t\t\t\t[ ");
767	if (cfg->an_radiotype & AN_RADIOTYPE_80211_FH)
768		printf("802.11 FH");
769	else if (cfg->an_radiotype & AN_RADIOTYPE_80211_DS)
770		printf("802.11 DS");
771	else if (cfg->an_radiotype & AN_RADIOTYPE_LM2000_DS)
772		printf("LM2000 DS");
773	else
774		printf("unknown (%x)", cfg->an_radiotype);
775	printf(" ]");
776	printf("\nRX Diversity:\t\t\t\t[ ");
777	diversity = cfg->an_diversity & 0xFF;
778	if (diversity == AN_DIVERSITY_ANTENNA_1_ONLY)
779		printf("antenna 1 only");
780	else if (diversity == AN_DIVERSITY_ANTENNA_2_ONLY)
781		printf("antenna 2 only");
782	else if (diversity == AN_DIVERSITY_ANTENNA_1_AND_2)
783		printf("antenna 1 and 2");
784	printf(" ]");
785	printf("\nTX Diversity:\t\t\t\t[ ");
786	diversity = (cfg->an_diversity >> 8) & 0xFF;
787	if (diversity == AN_DIVERSITY_ANTENNA_1_ONLY)
788		printf("antenna 1 only");
789	else if (diversity == AN_DIVERSITY_ANTENNA_2_ONLY)
790		printf("antenna 2 only");
791	else if (diversity == AN_DIVERSITY_ANTENNA_1_AND_2)
792		printf("antenna 1 and 2");
793	printf(" ]");
794	printf("\nTransmit power level:\t\t\t");
795	an_printwords(&cfg->an_tx_power, 1);
796	printf("\nRSS threshold:\t\t\t\t");
797	an_printwords(&cfg->an_rss_thresh, 1);
798	printf("\nNode name:\t\t\t\t");
799	an_printstr((char *)&cfg->an_nodename, 16);
800	printf("\nARL threshold:\t\t\t\t");
801	an_printwords(&cfg->an_arl_thresh, 1);
802	printf("\nARL decay:\t\t\t\t");
803	an_printwords(&cfg->an_arl_decay, 1);
804	printf("\nARL delay:\t\t\t\t");
805	an_printwords(&cfg->an_arl_delay, 1);
806
807	printf("\n");
808	printf("\n");
809	an_readkeyinfo(iface);
810
811	return;
812}
813
814
815static void usage(p)
816	char			*p;
817{
818	fprintf(stderr, "usage:  %s -i iface -A (show specified APs)\n", p);
819	fprintf(stderr, "\t%s -i iface -N (show specified SSIDss)\n", p);
820	fprintf(stderr, "\t%s -i iface -S (show NIC status)\n", p);
821	fprintf(stderr, "\t%s -i iface -I (show NIC capabilities)\n", p);
822	fprintf(stderr, "\t%s -i iface -T (show stats counters)\n", p);
823	fprintf(stderr, "\t%s -i iface -C (show current config)\n", p);
824	fprintf(stderr, "\t%s -i iface -t 0|1|2|3|4 (set TX speed)\n", p);
825	fprintf(stderr, "\t%s -i iface -s 0|1|2|3 (set power same mode)\n", p);
826	fprintf(stderr, "\t%s -i iface [-v 1|2|3|4] -a AP (specify AP)\n", p);
827	fprintf(stderr, "\t%s -i iface -b val (set beacon period)\n", p);
828	fprintf(stderr, "\t%s -i iface [-v 0|1] -d val (set diversity)\n", p);
829	fprintf(stderr, "\t%s -i iface -j val (set netjoin timeout)\n", p);
830	fprintf(stderr, "\t%s -i iface -e 0|1|2|3 (enable transmit key)\n", p);
831	fprintf(stderr, "\t%s -i iface [-v 0|1|2|3|4|5|6|7] -k key (set key)\n", p);
832	fprintf(stderr, "\t%s -i iface -K 0|1|2 (no auth/open/shared secret)\n", p);
833	fprintf(stderr, "\t%s -i iface -W 0|1|2 (no WEP/full WEP/mixed cell)\n", p);
834	fprintf(stderr, "\t%s -i iface -l val (set station name)\n", p);
835	fprintf(stderr, "\t%s -i iface -m val (set MAC address)\n", p);
836	fprintf(stderr, "\t%s -i iface [-v 1|2|3] -n SSID "
837	    "(specify SSID)\n", p);
838	fprintf(stderr, "\t%s -i iface -o 0|1 (set operating mode)\n", p);
839	fprintf(stderr, "\t%s -i iface -c val (set ad-hoc channel)\n", p);
840	fprintf(stderr, "\t%s -i iface -f val (set frag threshold)\n", p);
841	fprintf(stderr, "\t%s -i iface -r val (set RTS threshold)\n", p);
842#ifdef ANCACHE
843	fprintf(stderr, "\t%s -i iface -Q print signal quality cache\n", p);
844	fprintf(stderr, "\t%s -i iface -Z zero out signal cache\n", p);
845#endif
846
847	fprintf(stderr, "\t%s -h (display this message)\n", p);
848
849
850	exit(1);
851}
852
853static void an_setconfig(iface, act, arg)
854	const char		*iface;
855	int			act;
856	void			*arg;
857{
858	struct an_ltv_genconfig	*cfg;
859	struct an_ltv_caps	*caps;
860	struct an_req		areq;
861	struct an_req		areq_caps;
862	u_int16_t		diversity = 0;
863	struct ether_addr	*addr;
864	int			i;
865
866	areq.an_len = sizeof(areq);
867	areq.an_type = AN_RID_GENCONFIG;
868	an_getval(iface, &areq);
869	cfg = (struct an_ltv_genconfig *)&areq;
870
871	areq_caps.an_len = sizeof(areq);
872	areq_caps.an_type = AN_RID_CAPABILITIES;
873	an_getval(iface, &areq_caps);
874	caps = (struct an_ltv_caps *)&areq_caps;
875
876	switch(act) {
877	case ACT_SET_OPMODE:
878		cfg->an_opmode = atoi(arg);
879		break;
880	case ACT_SET_FREQ:
881		cfg->an_ds_channel = atoi(arg);
882		break;
883	case ACT_SET_PWRSAVE:
884		cfg->an_psave_mode = atoi(arg);
885		break;
886	case ACT_SET_SCANMODE:
887		cfg->an_scanmode = atoi(arg);
888		break;
889	case ACT_SET_DIVERSITY_RX:
890	case ACT_SET_DIVERSITY_TX:
891		switch(atoi(arg)) {
892		case 0:
893			diversity = AN_DIVERSITY_FACTORY_DEFAULT;
894			break;
895		case 1:
896			diversity = AN_DIVERSITY_ANTENNA_1_ONLY;
897			break;
898		case 2:
899			diversity = AN_DIVERSITY_ANTENNA_2_ONLY;
900			break;
901		case 3:
902			diversity = AN_DIVERSITY_ANTENNA_1_AND_2;
903			break;
904		default:
905			errx(1, "bad diversity setting: %d", diversity);
906			break;
907		}
908		if (atoi(arg) == ACT_SET_DIVERSITY_RX) {
909			cfg->an_diversity &= 0x00FF;
910			cfg->an_diversity |= (diversity << 8);
911		} else {
912			cfg->an_diversity &= 0xFF00;
913			cfg->an_diversity |= diversity;
914		}
915		break;
916	case ACT_SET_TXPWR:
917		for (i = 0; i < 8; i++) {
918			if (caps->an_tx_powerlevels[i] == atoi(arg))
919				break;
920		}
921		if (i == 8)
922			errx(1, "unsupported power level: %dmW", atoi(arg));
923
924		cfg->an_tx_power = atoi(arg);
925		break;
926	case ACT_SET_RTS_THRESH:
927		cfg->an_rtsthresh = atoi(arg);
928		break;
929	case ACT_SET_RTS_RETRYLIM:
930		cfg->an_shortretry_limit =
931		   cfg->an_longretry_limit = atoi(arg);
932		break;
933	case ACT_SET_BEACON_PERIOD:
934		cfg->an_beacon_period = atoi(arg);
935		break;
936	case ACT_SET_WAKE_DURATION:
937		cfg->an_atim_duration = atoi(arg);
938		break;
939	case ACT_SET_FRAG_THRESH:
940		cfg->an_fragthresh = atoi(arg);
941		break;
942	case ACT_SET_NETJOIN:
943		cfg->an_ibss_join_net_timeout = atoi(arg);
944		break;
945	case ACT_SET_MYNAME:
946		bzero(cfg->an_nodename, 16);
947		strncpy((char *)&cfg->an_nodename, optarg, 16);
948		break;
949	case ACT_SET_MAC:
950		addr = ether_aton((char *)arg);
951
952		if (addr == NULL)
953			errx(1, "badly formatted address");
954		bzero(cfg->an_macaddr, ETHER_ADDR_LEN);
955		bcopy((char *)addr, (char *)&cfg->an_macaddr, ETHER_ADDR_LEN);
956		break;
957	case ACT_ENABLE_WEP:
958		switch (atoi (arg)) {
959		case 0:
960			/* no WEP */
961			cfg->an_authtype &= ~(AN_AUTHTYPE_PRIVACY_IN_USE
962					| AN_AUTHTYPE_ALLOW_UNENCRYPTED);
963			break;
964		case 1:
965			/* full WEP */
966			cfg->an_authtype |= AN_AUTHTYPE_PRIVACY_IN_USE;
967			cfg->an_authtype &= ~AN_AUTHTYPE_ALLOW_UNENCRYPTED;
968			break;
969		case 2:
970			/* mixed cell */
971			cfg->an_authtype = AN_AUTHTYPE_PRIVACY_IN_USE
972					| AN_AUTHTYPE_ALLOW_UNENCRYPTED;
973			break;
974		}
975		break;
976	case ACT_SET_KEY_TYPE:
977		cfg->an_authtype = (cfg->an_authtype & ~AN_AUTHTYPE_MASK)
978		        | atoi(arg);
979		break;
980	default:
981		errx(1, "unknown action");
982		break;
983	}
984
985	an_setval(iface, &areq);
986	exit(0);
987}
988
989static void an_setspeed(iface, act, arg)
990	const char		*iface;
991	int			act __unused;
992	void			*arg;
993{
994	struct an_req		areq;
995	struct an_ltv_caps	*caps;
996	u_int16_t		speed;
997
998	areq.an_len = sizeof(areq);
999	areq.an_type = AN_RID_CAPABILITIES;
1000
1001	an_getval(iface, &areq);
1002	caps = (struct an_ltv_caps *)&areq;
1003
1004	switch(atoi(arg)) {
1005	case 0:
1006		speed = 0;
1007		break;
1008	case 1:
1009		speed = AN_RATE_1MBPS;
1010		break;
1011	case 2:
1012		speed = AN_RATE_2MBPS;
1013		break;
1014	case 3:
1015		if (caps->an_rates[2] != AN_RATE_5_5MBPS)
1016			errx(1, "5.5Mbps not supported on this card");
1017		speed = AN_RATE_5_5MBPS;
1018		break;
1019	case 4:
1020		if (caps->an_rates[3] != AN_RATE_11MBPS)
1021			errx(1, "11Mbps not supported on this card");
1022		speed = AN_RATE_11MBPS;
1023		break;
1024	default:
1025		errx(1, "unsupported speed");
1026		break;
1027	}
1028
1029	areq.an_len = 6;
1030	areq.an_type = AN_RID_TX_SPEED;
1031	areq.an_val[0] = speed;
1032
1033	an_setval(iface, &areq);
1034	exit(0);
1035}
1036
1037static void an_setap(iface, act, arg)
1038	const char		*iface;
1039	int			act;
1040	void			*arg;
1041{
1042	struct an_ltv_aplist	*ap;
1043	struct an_req		areq;
1044	struct ether_addr	*addr;
1045
1046	areq.an_len = sizeof(areq);
1047	areq.an_type = AN_RID_APLIST;
1048
1049	an_getval(iface, &areq);
1050	ap = (struct an_ltv_aplist *)&areq;
1051
1052	addr = ether_aton((char *)arg);
1053
1054	if (addr == NULL)
1055		errx(1, "badly formatted address");
1056
1057	switch(act) {
1058	case ACT_SET_AP1:
1059		bzero(ap->an_ap1, ETHER_ADDR_LEN);
1060		bcopy((char *)addr, (char *)&ap->an_ap1, ETHER_ADDR_LEN);
1061		break;
1062	case ACT_SET_AP2:
1063		bzero(ap->an_ap2, ETHER_ADDR_LEN);
1064		bcopy((char *)addr, (char *)&ap->an_ap2, ETHER_ADDR_LEN);
1065		break;
1066	case ACT_SET_AP3:
1067		bzero(ap->an_ap3, ETHER_ADDR_LEN);
1068		bcopy((char *)addr, (char *)&ap->an_ap3, ETHER_ADDR_LEN);
1069		break;
1070	case ACT_SET_AP4:
1071		bzero(ap->an_ap4, ETHER_ADDR_LEN);
1072		bcopy((char *)addr, (char *)&ap->an_ap4, ETHER_ADDR_LEN);
1073		break;
1074	default:
1075		errx(1, "unknown action");
1076		break;
1077	}
1078
1079	an_setval(iface, &areq);
1080	exit(0);
1081}
1082
1083static void an_setssid(iface, act, arg)
1084	const char		*iface;
1085	int			act;
1086	void			*arg;
1087{
1088	struct an_ltv_ssidlist	*ssid;
1089	struct an_req		areq;
1090
1091	areq.an_len = sizeof(areq);
1092	areq.an_type = AN_RID_SSIDLIST;
1093
1094	an_getval(iface, &areq);
1095	ssid = (struct an_ltv_ssidlist *)&areq;
1096
1097	switch (act) {
1098	case ACT_SET_SSID1:
1099		bzero(ssid->an_ssid1, sizeof(ssid->an_ssid1));
1100		strlcpy(ssid->an_ssid1, (char *)arg, sizeof(ssid->an_ssid1));
1101		ssid->an_ssid1_len = strlen(ssid->an_ssid1);
1102		break;
1103	case ACT_SET_SSID2:
1104		bzero(ssid->an_ssid2, sizeof(ssid->an_ssid2));
1105		strlcpy(ssid->an_ssid2, (char *)arg, sizeof(ssid->an_ssid2));
1106		ssid->an_ssid2_len = strlen(ssid->an_ssid2);
1107		break;
1108	case ACT_SET_SSID3:
1109		bzero(ssid->an_ssid3, sizeof(ssid->an_ssid3));
1110		strlcpy(ssid->an_ssid3, (char *)arg, sizeof(ssid->an_ssid3));
1111		ssid->an_ssid3_len = strlen(ssid->an_ssid3);
1112		break;
1113	default:
1114		errx(1, "unknown action");
1115		break;
1116	}
1117
1118	an_setval(iface, &areq);
1119	exit(0);
1120}
1121
1122#ifdef ANCACHE
1123static void an_zerocache(iface)
1124	const char		*iface;
1125{
1126	struct an_req		areq;
1127
1128	bzero((char *)&areq, sizeof(areq));
1129	areq.an_len = 0;
1130	areq.an_type = AN_RID_ZERO_CACHE;
1131
1132	an_getval(iface, &areq);
1133
1134	return;
1135}
1136
1137static void an_readcache(iface)
1138	const char		*iface;
1139{
1140	struct an_req		areq;
1141	int 			*an_sigitems;
1142	struct an_sigcache 	*sc;
1143	char *			pt;
1144	int 			i;
1145
1146	if (iface == NULL)
1147		errx(1, "must specify interface name");
1148
1149	bzero((char *)&areq, sizeof(areq));
1150	areq.an_len = AN_MAX_DATALEN;
1151	areq.an_type = AN_RID_READ_CACHE;
1152
1153	an_getval(iface, &areq);
1154
1155	an_sigitems = (int *) &areq.an_val;
1156	pt = ((char *) &areq.an_val);
1157	pt += sizeof(int);
1158	sc = (struct an_sigcache *) pt;
1159
1160	for (i = 0; i < *an_sigitems; i++) {
1161		printf("[%d/%d]:", i+1, *an_sigitems);
1162		printf(" %02x:%02x:%02x:%02x:%02x:%02x,",
1163		  		    	sc->macsrc[0]&0xff,
1164		  		    	sc->macsrc[1]&0xff,
1165		   		    	sc->macsrc[2]&0xff,
1166		   			sc->macsrc[3]&0xff,
1167		   			sc->macsrc[4]&0xff,
1168		   			sc->macsrc[5]&0xff);
1169        	printf(" %d.%d.%d.%d,",((sc->ipsrc >> 0) & 0xff),
1170				        ((sc->ipsrc >> 8) & 0xff),
1171				        ((sc->ipsrc >> 16) & 0xff),
1172				        ((sc->ipsrc >> 24) & 0xff));
1173		printf(" sig: %d, noise: %d, qual: %d\n",
1174		   			sc->signal,
1175		   			sc->noise,
1176		   			sc->quality);
1177		sc++;
1178	}
1179
1180	return;
1181}
1182#endif
1183
1184static int an_hex2int(c)
1185	char			c;
1186{
1187	if (c >= '0' && c <= '9')
1188		return (c - '0');
1189	if (c >= 'A' && c <= 'F')
1190		return (c - 'A' + 10);
1191	if (c >= 'a' && c <= 'f')
1192		return (c - 'a' + 10);
1193
1194	return (0);
1195}
1196
1197static void an_str2key(s, k)
1198	char			*s;
1199	struct an_ltv_key	*k;
1200{
1201	int			n, i;
1202	char			*p;
1203
1204	/* Is this a hex string? */
1205	if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
1206		/* Yes, convert to int. */
1207		n = 0;
1208		p = (char *)&k->key[0];
1209		for (i = 2; s[i] != '\0' && s[i + 1] != '\0'; i+= 2) {
1210			*p++ = (an_hex2int(s[i]) << 4) + an_hex2int(s[i + 1]);
1211			n++;
1212		}
1213		if (s[i] != '\0')
1214			errx(1, "hex strings must be of even length");
1215		k->klen = n;
1216	} else {
1217		/* No, just copy it in. */
1218		bcopy(s, k->key, strlen(s));
1219		k->klen = strlen(s);
1220	}
1221
1222	return;
1223}
1224
1225static void an_setkeys(iface, key, keytype)
1226	const char		*iface;
1227	char			*key;
1228	int			keytype;
1229{
1230	struct an_req		areq;
1231	struct an_ltv_key	*k;
1232
1233	bzero((char *)&areq, sizeof(areq));
1234	k = (struct an_ltv_key *)&areq;
1235
1236	if (strlen(key) > 28) {
1237		err(1, "encryption key must be no "
1238		    "more than 18 characters long");
1239	}
1240
1241	an_str2key(key, k);
1242
1243	k->kindex=keytype/2;
1244
1245	if (!(k->klen==0 || k->klen==5 || k->klen==13)) {
1246		err(1, "encryption key must be 0, 5 or 13 bytes long");
1247	}
1248
1249	/* default mac and only valid one (from manual) 1.0.0.0.0.0 */
1250	k->mac[0]=1;
1251	k->mac[1]=0;
1252	k->mac[2]=0;
1253	k->mac[3]=0;
1254	k->mac[4]=0;
1255	k->mac[5]=0;
1256
1257	switch(keytype & 1){
1258	case 0:
1259	  areq.an_len = sizeof(struct an_ltv_key);
1260	  areq.an_type = AN_RID_WEP_PERM;
1261	  an_setval(iface, &areq);
1262	  break;
1263	case 1:
1264	  areq.an_len = sizeof(struct an_ltv_key);
1265	  areq.an_type = AN_RID_WEP_TEMP;
1266	  an_setval(iface, &areq);
1267	  break;
1268	}
1269
1270	return;
1271}
1272
1273static void an_readkeyinfo(iface)
1274	const char		*iface;
1275{
1276	struct an_req		areq;
1277	struct an_ltv_key	*k;
1278	int i;
1279
1280	bzero((char *)&areq, sizeof(areq));
1281	k = (struct an_ltv_key *)&areq;
1282
1283	printf("WEP Key status:\n");
1284	areq.an_type = AN_RID_WEP_TEMP;  	/* read first key */
1285	for(i=0; i<4; i++){
1286		areq.an_len = sizeof(struct an_ltv_key);
1287		an_getval(iface, &areq);
1288		switch (k->klen){
1289		case 0:
1290			printf("\tKey %d is unset\n",i);
1291			break;
1292		case 5:
1293			printf("\tKey %d is set  40 bits\n",i);
1294			break;
1295		case 13:
1296			printf("\tKey %d is set 128 bits\n",i);
1297			break;
1298		default:
1299			printf("\tWEP Key %d has an unknown size %d\n",
1300			    i, k->klen);
1301		}
1302
1303		areq.an_type = AN_RID_WEP_PERM;	/* read next key */
1304	}
1305	k->kindex = 0xffff;
1306	areq.an_len = sizeof(struct an_ltv_key);
1307      	an_getval(iface, &areq);
1308	printf("\tThe active transmit key is %d\n", k->mac[0]);
1309
1310	return;
1311}
1312
1313static void an_enable_tx_key(iface, arg)
1314	const char		*iface;
1315	char			*arg;
1316{
1317	struct an_req		areq;
1318	struct an_ltv_key	*k;
1319
1320	bzero((char *)&areq, sizeof(areq));
1321	k = (struct an_ltv_key *)&areq;
1322
1323	/* From a Cisco engineer write the transmit key to use in the
1324	   first MAC, index is FFFF*/
1325	k->kindex=0xffff;
1326	k->klen=0;
1327
1328	k->mac[0]=atoi(arg);
1329	k->mac[1]=0;
1330	k->mac[2]=0;
1331	k->mac[3]=0;
1332	k->mac[4]=0;
1333	k->mac[5]=0;
1334
1335	areq.an_len = sizeof(struct an_ltv_key);
1336	areq.an_type = AN_RID_WEP_PERM;
1337	an_setval(iface, &areq);
1338
1339	return;
1340}
1341
1342int main(argc, argv)
1343	int			argc;
1344	char			*argv[];
1345{
1346	int			ch;
1347	int			act = 0;
1348	const char		*iface = NULL;
1349	int			modifier = 0;
1350	char			*key = NULL;
1351	void			*arg = NULL;
1352	char			*p = argv[0];
1353
1354	/* Get the interface name */
1355	opterr = 0;
1356	ch = getopt(argc, argv, "i:");
1357	if (ch == 'i') {
1358		iface = optarg;
1359	} else {
1360		if (argc > 1 && *argv[1] != '-') {
1361			iface = argv[1];
1362			optind = 2;
1363		} else {
1364			iface = "an0";
1365			optind = 1;
1366		}
1367		optreset = 1;
1368	}
1369	opterr = 1;
1370
1371	while ((ch = getopt(argc, argv,
1372	    "ANISCTht:a:e:o:s:n:v:d:j:b:c:r:p:w:m:l:k:K:W:QZ")) != -1) {
1373		switch(ch) {
1374		case 'Z':
1375#ifdef ANCACHE
1376			act = ACT_ZEROCACHE;
1377#else
1378			errx(1, "ANCACHE not available");
1379#endif
1380			break;
1381		case 'Q':
1382#ifdef ANCACHE
1383			act = ACT_DUMPCACHE;
1384#else
1385			errx(1, "ANCACHE not available");
1386#endif
1387			break;
1388		case 'A':
1389			act = ACT_DUMPAP;
1390			break;
1391		case 'N':
1392			act = ACT_DUMPSSID;
1393			break;
1394		case 'S':
1395			act = ACT_DUMPSTATUS;
1396			break;
1397		case 'I':
1398			act = ACT_DUMPCAPS;
1399			break;
1400		case 'T':
1401			act = ACT_DUMPSTATS;
1402			break;
1403		case 'C':
1404			act = ACT_DUMPCONFIG;
1405			break;
1406		case 't':
1407			act = ACT_SET_TXRATE;
1408			arg = optarg;
1409			break;
1410		case 's':
1411			act = ACT_SET_PWRSAVE;
1412			arg = optarg;
1413			break;
1414		case 'p':
1415			act = ACT_SET_TXPWR;
1416			arg = optarg;
1417			break;
1418		case 'v':
1419			modifier = atoi(optarg);
1420			break;
1421		case 'a':
1422			switch(modifier) {
1423			case 0:
1424			case 1:
1425				act = ACT_SET_AP1;
1426				break;
1427			case 2:
1428				act = ACT_SET_AP2;
1429				break;
1430			case 3:
1431				act = ACT_SET_AP3;
1432				break;
1433			case 4:
1434				act = ACT_SET_AP4;
1435				break;
1436			default:
1437				errx(1, "bad modifier %d: there "
1438				    "are only 4 access point settings",
1439				    modifier);
1440				usage(p);
1441				break;
1442			}
1443			arg = optarg;
1444			break;
1445		case 'b':
1446			act = ACT_SET_BEACON_PERIOD;
1447			arg = optarg;
1448			break;
1449		case 'd':
1450			switch(modifier) {
1451			case 0:
1452				act = ACT_SET_DIVERSITY_RX;
1453				break;
1454			case 1:
1455				act = ACT_SET_DIVERSITY_TX;
1456				break;
1457			default:
1458				errx(1, "must specift RX or TX diversity");
1459				break;
1460			}
1461			arg = optarg;
1462			break;
1463		case 'j':
1464			act = ACT_SET_NETJOIN;
1465			arg = optarg;
1466			break;
1467		case 'l':
1468			act = ACT_SET_MYNAME;
1469			arg = optarg;
1470			break;
1471		case 'm':
1472			act = ACT_SET_MAC;
1473			arg = optarg;
1474			break;
1475		case 'n':
1476			switch(modifier) {
1477			case 0:
1478			case 1:
1479				act = ACT_SET_SSID1;
1480				break;
1481			case 2:
1482				act = ACT_SET_SSID2;
1483				break;
1484			case 3:
1485				act = ACT_SET_SSID3;
1486				break;
1487			default:
1488				errx(1, "bad modifier %d: there"
1489				    "are only 3 SSID settings", modifier);
1490				usage(p);
1491				break;
1492			}
1493			arg = optarg;
1494			break;
1495		case 'o':
1496			act = ACT_SET_OPMODE;
1497			arg = optarg;
1498			break;
1499		case 'c':
1500			act = ACT_SET_FREQ;
1501			arg = optarg;
1502			break;
1503		case 'f':
1504			act = ACT_SET_FRAG_THRESH;
1505			arg = optarg;
1506			break;
1507		case 'W':
1508			act = ACT_ENABLE_WEP;
1509			arg = optarg;
1510			break;
1511		case 'K':
1512			act = ACT_SET_KEY_TYPE;
1513			arg = optarg;
1514			break;
1515		case 'k':
1516			act = ACT_SET_KEYS;
1517			key = optarg;
1518			break;
1519		case 'e':
1520			act = ACT_ENABLE_TX_KEY;
1521			arg = optarg;
1522			break;
1523		case 'q':
1524			act = ACT_SET_RTS_RETRYLIM;
1525			arg = optarg;
1526			break;
1527		case 'r':
1528			act = ACT_SET_RTS_THRESH;
1529			arg = optarg;
1530			break;
1531		case 'w':
1532			act = ACT_SET_WAKE_DURATION;
1533			arg = optarg;
1534			break;
1535		case 'h':
1536		default:
1537			usage(p);
1538		}
1539	}
1540
1541	if (iface == NULL || (!act && !key))
1542		usage(p);
1543
1544	switch(act) {
1545	case ACT_DUMPSTATUS:
1546		an_dumpstatus(iface);
1547		break;
1548	case ACT_DUMPCAPS:
1549		an_dumpcaps(iface);
1550		break;
1551	case ACT_DUMPSTATS:
1552		an_dumpstats(iface);
1553		break;
1554	case ACT_DUMPCONFIG:
1555		an_dumpconfig(iface);
1556		break;
1557	case ACT_DUMPSSID:
1558		an_dumpssid(iface);
1559		break;
1560	case ACT_DUMPAP:
1561		an_dumpap(iface);
1562		break;
1563	case ACT_SET_SSID1:
1564	case ACT_SET_SSID2:
1565	case ACT_SET_SSID3:
1566		an_setssid(iface, act, arg);
1567		break;
1568	case ACT_SET_AP1:
1569	case ACT_SET_AP2:
1570	case ACT_SET_AP3:
1571	case ACT_SET_AP4:
1572		an_setap(iface, act, arg);
1573		break;
1574	case ACT_SET_TXRATE:
1575		an_setspeed(iface, act, arg);
1576		break;
1577#ifdef ANCACHE
1578	case ACT_ZEROCACHE:
1579		an_zerocache(iface);
1580		break;
1581	case ACT_DUMPCACHE:
1582		an_readcache(iface);
1583		break;
1584
1585#endif
1586	case ACT_SET_KEYS:
1587		an_setkeys(iface, key, modifier);
1588		break;
1589	case ACT_ENABLE_TX_KEY:
1590		an_enable_tx_key(iface, arg);
1591		break;
1592	default:
1593		an_setconfig(iface, act, arg);
1594		break;
1595	}
1596
1597	exit(0);
1598}
1599
1600