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