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