2 * Copyright (c) 1997, 1998, 1999
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
34 static const char copyright[] = "@(#) Copyright (c) 1997, 1998, 1999\
35 Bill Paul. All rights reserved.";
36 static const char rcsid[] =
40 #include <sys/types.h>
41 #include <sys/cdefs.h>
42 #include <sys/param.h>
43 #include <sys/socket.h>
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
48 #include <net/if_var.h>
49 #include <net/ethernet.h>
51 #include <dev/wi/if_wavelan_ieee.h>
61 static void wi_getval(const char *, struct wi_req *);
62 static void wi_setval(const char *, struct wi_req *);
63 static void wi_printstr(struct wi_req *);
64 static void wi_setstr(const char *, int, char *);
65 static void wi_setbytes(const char *, int, char *, int);
66 static void wi_setword(const char *, int, int);
67 static void wi_sethex(const char *, int, char *);
68 static void wi_printwords(struct wi_req *);
69 static void wi_printbool(struct wi_req *);
70 static void wi_printhex(struct wi_req *);
71 static void wi_dumpinfo(const char *);
72 static void wi_dumpstats(const char *);
73 static void wi_setkeys(const char *, char *, int);
74 static void wi_printkeys(struct wi_req *);
75 static void wi_printaplist(const char *);
76 static int wi_hex2int(char);
77 static void wi_str2key(char *, struct wi_key *);
79 static void wi_zerocache(const char *);
80 static void wi_readcache(const char *);
82 static void usage(const char *);
87 wi_getval(const char *iface, struct wi_req *wreq)
92 bzero((char *)&ifr, sizeof(ifr));
94 strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
95 ifr.ifr_data = (caddr_t)wreq;
97 s = socket(AF_INET, SOCK_DGRAM, 0);
102 if (ioctl(s, SIOCGWAVELAN, &ifr) == -1)
103 err(1, "SIOCGWAVELAN");
111 wi_setval(const char *iface, struct wi_req *wreq)
116 bzero((char *)&ifr, sizeof(ifr));
118 strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
119 ifr.ifr_data = (caddr_t)wreq;
121 s = socket(AF_INET, SOCK_DGRAM, 0);
126 if (ioctl(s, SIOCSWAVELAN, &ifr) == -1)
127 err(1, "SIOCSWAVELAN");
135 wi_printstr(struct wi_req *wreq)
140 if (wreq->wi_type == WI_RID_SERIALNO) {
141 ptr = (char *)&wreq->wi_val;
142 for (i = 0; i < (wreq->wi_len - 1) * 2; i++) {
147 ptr = (char *)&wreq->wi_val[1];
148 for (i = 0; i < wreq->wi_val[0]; i++) {
155 printf("[ %s ]", ptr);
161 wi_setstr(const char *iface, int code, char *str)
166 errx(1, "must specify interface name");
169 errx(1, "must specify string");
171 bzero((char *)&wreq, sizeof(wreq));
173 if (strlen(str) > 30)
174 errx(1, "string too long");
178 wreq.wi_val[0] = strlen(str);
179 bcopy(str, (char *)&wreq.wi_val[1], strlen(str));
181 wi_setval(iface, &wreq);
187 wi_setbytes(const char *iface, int code, char *bytes, int len)
192 errx(1, "must specify interface name");
194 bzero((char *)&wreq, sizeof(wreq));
197 wreq.wi_len = (len / 2) + 1;
198 bcopy(bytes, (char *)&wreq.wi_val[0], len);
200 wi_setval(iface, &wreq);
206 wi_setword(const char *iface, int code, int word)
210 bzero((char *)&wreq, sizeof(wreq));
214 wreq.wi_val[0] = word;
216 wi_setval(iface, &wreq);
222 wi_sethex(const char *iface, int code, char *str)
224 struct ether_addr *addr;
227 errx(1, "must specify address");
229 addr = ether_aton(str);
232 errx(1, "badly formatted address");
234 wi_setbytes(iface, code, (char *)addr, ETHER_ADDR_LEN);
242 if (c >= '0' && c <= '9')
244 if (c >= 'A' && c <= 'F')
245 return (c - 'A' + 10);
246 if (c >= 'a' && c <= 'f')
247 return (c - 'a' + 10);
253 wi_str2key(char *s, struct wi_key *k)
258 /* Is this a hex string? */
259 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
260 /* Yes, convert to int. */
262 p = (char *)&k->wi_keydat[0];
263 for (i = 2; s[i] != '\0' && s[i + 1] != '\0'; i+= 2) {
264 *p++ = (wi_hex2int(s[i]) << 4) + wi_hex2int(s[i + 1]);
268 errx(1, "hex strings must be of even length");
271 /* No, just copy it in. */
272 bcopy(s, k->wi_keydat, strlen(s));
273 k->wi_keylen = strlen(s);
280 wi_setkeys(const char *iface, char *key, int idx)
284 struct wi_ltv_keys *keys;
287 bzero((char *)&wreq, sizeof(wreq));
288 wreq.wi_len = WI_MAX_DATALEN;
289 wreq.wi_type = WI_RID_WEP_AVAIL;
291 wi_getval(iface, &wreq);
292 if (wreq.wi_val[0] == 0)
293 errx(1, "no WEP option available on this card");
295 bzero((char *)&wreq, sizeof(wreq));
296 wreq.wi_len = WI_MAX_DATALEN;
297 wreq.wi_type = WI_RID_DEFLT_CRYPT_KEYS;
299 wi_getval(iface, &wreq);
300 keys = (struct wi_ltv_keys *)&wreq;
302 keylen = strlen(key);
303 if (key[0] == '0' && (key[1] == 'x' || key[1] == 'X')) {
304 if (keylen != 2 && keylen != 12 && keylen != 28) {
305 errx(1, "encryption key must be 0, 10, or 26 "
309 if (keylen != 0 && keylen != 5 && keylen != 13) {
310 errx(1, "encryption key must be 0, 5, or 13 "
316 errx(1, "only 4 encryption keys available");
318 k = &keys->wi_keys[idx];
321 wreq.wi_len = (sizeof(struct wi_ltv_keys) / 2) + 1;
322 wreq.wi_type = WI_RID_DEFLT_CRYPT_KEYS;
323 wi_setval(iface, &wreq);
329 wi_printkeys(struct wi_req *wreq)
334 struct wi_ltv_keys *keys;
337 keys = (struct wi_ltv_keys *)wreq;
339 for (i = 0; i < 4; i++) {
340 k = &keys->wi_keys[i];
341 ptr = (char *)k->wi_keydat;
343 for (j = 0; j < k->wi_keylen; j++) {
344 if (!isprint(ptr[j])) {
351 printf("[ %s ]", ptr);
354 for (j = 0; j < k->wi_keylen; j++) {
355 printf("%02x", ptr[j] & 0xFF);
366 wi_printwords(struct wi_req *wreq)
371 for (i = 0; i < wreq->wi_len - 1; i++)
372 printf("%d ", wreq->wi_val[i]);
379 wi_printbool(struct wi_req *wreq)
390 wi_printhex(struct wi_req *wreq)
395 c = (unsigned char *)&wreq->wi_val;
398 for (i = 0; i < (wreq->wi_len - 1) * 2; i++) {
399 printf("%02x", c[i]);
400 if (i < ((wreq->wi_len - 1) * 2) - 1)
409 wi_printaplist(const char *iface)
411 int prism2, len, i = 0, j;
413 struct wi_scan_p2_hdr *wi_p2_h;
414 struct wi_scan_res *res;
416 printf("Available APs:\n");
418 /* first determine if this is a prism2 card or not */
419 wreq.wi_len = WI_MAX_DATALEN;
420 wreq.wi_type = WI_RID_PRISM2;
422 wi_getval(iface, &wreq);
423 prism2 = wreq.wi_val[0];
425 /* send out a scan request */
426 wreq.wi_len = prism2 ? 3 : 1;
427 wreq.wi_type = WI_RID_SCAN_REQ;
430 wreq.wi_val[0] = 0x3FFF;
431 wreq.wi_val[1] = 0x000F;
434 wi_setval(iface, &wreq);
437 * sleep for 100 milliseconds so there's enough time for the card to
438 * respond... prism2's take a little longer.
440 usleep(prism2 ? 500000 : 100000);
442 /* get the scan results */
443 wreq.wi_len = WI_MAX_DATALEN;
444 wreq.wi_type = WI_RID_SCAN_RES;
446 wi_getval(iface, &wreq);
449 wi_p2_h = (struct wi_scan_p2_hdr *)wreq.wi_val;
451 /* if the reason is 0, this info is invalid */
452 if (wi_p2_h->wi_reason == 0)
458 len = prism2 ? WI_PRISM2_RES_SIZE : WI_WAVELAN_RES_SIZE;
460 for (; i < (wreq.wi_len * 2) - len; i += len) {
461 res = (struct wi_scan_res *)((char *)wreq.wi_val + i);
463 res->wi_ssid[res->wi_ssid_len] = '\0';
465 printf(" %-8s [ %02x:%02x:%02x:%02x:%02x:%02x ] [ %-2d ] "
466 "[ %d %d %d ] %-3d ", res->wi_ssid,
467 res->wi_bssid[0], res->wi_bssid[1], res->wi_bssid[2],
468 res->wi_bssid[3], res->wi_bssid[4], res->wi_bssid[5],
469 res->wi_chan, res->wi_signal - res->wi_noise,
470 res->wi_signal, res->wi_noise, res->wi_interval);
472 if (res->wi_capinfo) {
474 if (res->wi_capinfo & WI_CAPINFO_ESS)
476 if (res->wi_capinfo & WI_CAPINFO_IBSS)
478 if (res->wi_capinfo & WI_CAPINFO_PRIV)
485 for (j = 0; res->wi_srates[j] != 0; j++) {
486 res->wi_srates[j] = res->wi_srates[j] &
488 printf("%d.%d ", res->wi_srates[j] / 2,
489 (res->wi_srates[j] % 2) * 5);
493 printf("* %2.1f *", res->wi_rate == 0xa ? 1 :
494 (res->wi_rate == 0x14 ? 2 :
495 (res->wi_rate == 0x37 ? 5.5 :
496 (res->wi_rate == 0x6e ? 11 : 0))));
505 #define WI_STRING 0x01
507 #define WI_WORDS 0x03
508 #define WI_HEXBYTES 0x04
509 #define WI_KEYSTRUCT 0x05
517 static struct wi_table wi_table[] = {
518 { WI_RID_SERIALNO, WI_STRING, "NIC serial number:\t\t\t" },
519 { WI_RID_NODENAME, WI_STRING, "Station name:\t\t\t\t" },
520 { WI_RID_OWN_SSID, WI_STRING, "SSID for IBSS creation:\t\t\t" },
521 { WI_RID_CURRENT_SSID, WI_STRING, "Current netname (SSID):\t\t\t" },
522 { WI_RID_DESIRED_SSID, WI_STRING, "Desired netname (SSID):\t\t\t" },
523 { WI_RID_CURRENT_BSSID, WI_HEXBYTES, "Current BSSID:\t\t\t\t" },
524 { WI_RID_CHANNEL_LIST, WI_WORDS, "Channel list:\t\t\t\t" },
525 { WI_RID_OWN_CHNL, WI_WORDS, "IBSS channel:\t\t\t\t" },
526 { WI_RID_CURRENT_CHAN, WI_WORDS, "Current channel:\t\t\t" },
527 { WI_RID_COMMS_QUALITY, WI_WORDS, "Comms quality/signal/noise:\t\t" },
528 { WI_RID_PROMISC, WI_BOOL, "Promiscuous mode:\t\t\t" },
529 { WI_RID_PROCFRAME, WI_BOOL, "Process 802.11b Frame:\t\t\t" },
530 { WI_RID_PRISM2, WI_WORDS, "Intersil-Prism2 based card:\t\t" },
531 { WI_RID_PORTTYPE, WI_WORDS, "Port type (1=BSS, 3=ad-hoc):\t\t"},
532 { WI_RID_MAC_NODE, WI_HEXBYTES, "MAC address:\t\t\t\t"},
533 { WI_RID_TX_RATE, WI_WORDS, "TX rate (selection):\t\t\t"},
534 { WI_RID_CUR_TX_RATE, WI_WORDS, "TX rate (actual speed):\t\t\t"},
535 { WI_RID_RTS_THRESH, WI_WORDS, "RTS/CTS handshake threshold:\t\t"},
536 { WI_RID_CREATE_IBSS, WI_BOOL, "Create IBSS:\t\t\t\t" },
537 { WI_RID_SYSTEM_SCALE, WI_WORDS, "Access point density:\t\t\t" },
538 { WI_RID_PM_ENABLED, WI_WORDS, "Power Mgmt (1=on, 0=off):\t\t" },
539 { WI_RID_MAX_SLEEP, WI_WORDS, "Max sleep time:\t\t\t\t" },
543 static struct wi_table wi_crypt_table[] = {
544 { WI_RID_ENCRYPTION, WI_BOOL, "WEP encryption:\t\t\t\t" },
545 { WI_RID_TX_CRYPT_KEY, WI_WORDS, "TX encryption key:\t\t\t" },
546 { WI_RID_DEFLT_CRYPT_KEYS, WI_KEYSTRUCT, "Encryption keys:\t\t\t" },
551 wi_dumpinfo(const char *iface)
557 bzero((char *)&wreq, sizeof(wreq));
559 wreq.wi_len = WI_MAX_DATALEN;
560 wreq.wi_type = WI_RID_WEP_AVAIL;
562 wi_getval(iface, &wreq);
563 has_wep = wreq.wi_val[0];
567 for (i = 0; w[i].wi_type; i++) {
568 bzero((char *)&wreq, sizeof(wreq));
570 wreq.wi_len = WI_MAX_DATALEN;
571 wreq.wi_type = w[i].wi_code;
573 wi_getval(iface, &wreq);
574 printf("%s", w[i].wi_str);
575 switch(w[i].wi_type) {
580 wi_printwords(&wreq);
596 for (i = 0; w[i].wi_type; i++) {
597 bzero((char *)&wreq, sizeof(wreq));
599 wreq.wi_len = WI_MAX_DATALEN;
600 wreq.wi_type = w[i].wi_code;
602 wi_getval(iface, &wreq);
603 printf("%s", w[i].wi_str);
604 switch(w[i].wi_type) {
609 if (wreq.wi_type == WI_RID_TX_CRYPT_KEY)
611 wi_printwords(&wreq);
630 wi_printaplist(iface);
636 wi_dumpstats(const char *iface)
639 struct wi_counters *c;
642 errx(1, "must specify interface name");
644 bzero((char *)&wreq, sizeof(wreq));
645 wreq.wi_len = WI_MAX_DATALEN;
646 wreq.wi_type = WI_RID_IFACE_STATS;
648 wi_getval(iface, &wreq);
650 c = (struct wi_counters *)&wreq.wi_val;
652 printf("Transmitted unicast frames:\t\t%d\n",
653 c->wi_tx_unicast_frames);
654 printf("Transmitted multicast frames:\t\t%d\n",
655 c->wi_tx_multicast_frames);
656 printf("Transmitted fragments:\t\t\t%d\n",
658 printf("Transmitted unicast octets:\t\t%d\n",
659 c->wi_tx_unicast_octets);
660 printf("Transmitted multicast octets:\t\t%d\n",
661 c->wi_tx_multicast_octets);
662 printf("Single transmit retries:\t\t%d\n",
663 c->wi_tx_single_retries);
664 printf("Multiple transmit retries:\t\t%d\n",
665 c->wi_tx_multi_retries);
666 printf("Transmit retry limit exceeded:\t\t%d\n",
667 c->wi_tx_retry_limit);
668 printf("Transmit discards:\t\t\t%d\n",
670 printf("Transmit discards due to wrong SA:\t%d\n",
671 c->wi_tx_discards_wrong_sa);
672 printf("Received unicast frames:\t\t%d\n",
673 c->wi_rx_unicast_frames);
674 printf("Received multicast frames:\t\t%d\n",
675 c->wi_rx_multicast_frames);
676 printf("Received fragments:\t\t\t%d\n",
678 printf("Received unicast octets:\t\t%d\n",
679 c->wi_rx_unicast_octets);
680 printf("Received multicast octets:\t\t%d\n",
681 c->wi_rx_multicast_octets);
682 printf("Receive FCS errors:\t\t\t%d\n",
683 c->wi_rx_fcs_errors);
684 printf("Receive discards due to no buffer:\t%d\n",
685 c->wi_rx_discards_nobuf);
686 printf("Can't decrypt WEP frame:\t\t%d\n",
687 c->wi_rx_WEP_cant_decrypt);
688 printf("Received message fragments:\t\t%d\n",
689 c->wi_rx_msg_in_msg_frags);
690 printf("Received message bad fragments:\t\t%d\n",
691 c->wi_rx_msg_in_bad_msg_frags);
699 fprintf(stderr, "usage: %s -i iface\n", p);
700 fprintf(stderr, "\t%s -i iface -o\n", p);
701 fprintf(stderr, "\t%s -i iface -l\n", p);
702 fprintf(stderr, "\t%s -i iface -t tx rate\n", p);
703 fprintf(stderr, "\t%s -i iface -n network name\n", p);
704 fprintf(stderr, "\t%s -i iface -s station name\n", p);
705 fprintf(stderr, "\t%s -i iface -c 0|1\n", p);
706 fprintf(stderr, "\t%s -i iface -q SSID\n", p);
707 fprintf(stderr, "\t%s -i iface -p port type\n", p);
708 fprintf(stderr, "\t%s -i iface -a access point density\n", p);
709 fprintf(stderr, "\t%s -i iface -m mac address\n", p);
710 fprintf(stderr, "\t%s -i iface -d max data length\n", p);
711 fprintf(stderr, "\t%s -i iface -e 0|1\n", p);
712 fprintf(stderr, "\t%s -i iface -k encryption key [-v 1|2|3|4]\n", p);
713 fprintf(stderr, "\t%s -i iface -r RTS threshold\n", p);
714 fprintf(stderr, "\t%s -i iface -f frequency\n", p);
715 fprintf(stderr, "\t%s -i iface -F 0|1\n", p);
716 fprintf(stderr, "\t%s -i iface -P 0|1\n", p);
717 fprintf(stderr, "\t%s -i iface -S max sleep duration\n", p);
718 fprintf(stderr, "\t%s -i iface -T 1|2|3|4\n", p);
720 fprintf(stderr, "\t%s -i iface -Z zero out signal cache\n", p);
721 fprintf(stderr, "\t%s -i iface -C print signal cache\n", p);
729 wi_zerocache(const char *iface)
734 errx(1, "must specify interface name");
736 bzero((char *)&wreq, sizeof(wreq));
738 wreq.wi_type = WI_RID_ZERO_CACHE;
740 wi_getval(iface, &wreq);
744 wi_readcache(const char *iface)
748 struct wi_sigcache *sc;
753 errx(1, "must specify interface name");
755 bzero((char *)&wreq, sizeof(wreq));
756 wreq.wi_len = WI_MAX_DATALEN;
757 wreq.wi_type = WI_RID_READ_CACHE;
759 wi_getval(iface, &wreq);
761 wi_sigitems = (int *) &wreq.wi_val;
762 pt = ((char *) &wreq.wi_val);
764 sc = (struct wi_sigcache *) pt;
766 for (i = 0; i < *wi_sigitems; i++) {
767 printf("[%d/%d]:", i+1, *wi_sigitems);
768 printf(" %02x:%02x:%02x:%02x:%02x:%02x,",
775 printf(" %d.%d.%d.%d,",((sc->ipsrc >> 0) & 0xff),
776 ((sc->ipsrc >> 8) & 0xff),
777 ((sc->ipsrc >> 16) & 0xff),
778 ((sc->ipsrc >> 24) & 0xff));
779 printf(" sig: %d, noise: %d, qual: %d\n",
791 main(int argc, char *argv[])
794 const char *iface = NULL;
799 /* Get the interface name */
801 ch = getopt(argc, argv, "i:");
805 if (argc > 1 && *argv[1] != '-') {
816 while((ch = getopt(argc, argv,
817 "a:hoc:d:e:f:i:k:lp:r:q:t:n:s:m:v:F:P:S:T:ZC")) != -1) {
824 printf("WICACHE not available\n");
831 printf("WICACHE not available\n");
840 wi_setword(iface, WI_RID_CREATE_IBSS, atoi(optarg));
844 wi_setword(iface, WI_RID_MAX_DATALEN, atoi(optarg));
848 wi_setword(iface, WI_RID_ENCRYPTION, atoi(optarg));
852 wi_setword(iface, WI_RID_OWN_CHNL, atoi(optarg));
856 wi_setword(iface, WI_RID_PROCFRAME, atoi(optarg));
866 wi_setword(iface, WI_RID_PORTTYPE, atoi(optarg));
870 wi_setword(iface, WI_RID_RTS_THRESH, atoi(optarg));
874 wi_setword(iface, WI_RID_TX_RATE, atoi(optarg));
878 wi_setstr(iface, WI_RID_DESIRED_SSID, optarg);
882 wi_setstr(iface, WI_RID_NODENAME, optarg);
886 wi_sethex(iface, WI_RID_MAC_NODE, optarg);
890 wi_setstr(iface, WI_RID_OWN_SSID, optarg);
894 wi_setword(iface, WI_RID_MAX_SLEEP, atoi(optarg));
899 WI_RID_TX_CRYPT_KEY, atoi(optarg) - 1);
903 wi_setword(iface, WI_RID_PM_ENABLED, atoi(optarg));
907 wi_setword(iface, WI_RID_SYSTEM_SCALE, atoi(optarg));
911 modifier = atoi(optarg);
925 wi_setkeys(iface, key, modifier);