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