wiconfig.c revision 1.15 1 1.15 ichiro /* $NetBSD: wiconfig.c,v 1.15 2002/01/21 11:35:06 ichiro Exp $ */
2 1.1 sommerfe /*
3 1.1 sommerfe * Copyright (c) 1997, 1998, 1999
4 1.1 sommerfe * Bill Paul <wpaul (at) ctr.columbia.edu>. All rights reserved.
5 1.1 sommerfe *
6 1.1 sommerfe * Redistribution and use in source and binary forms, with or without
7 1.1 sommerfe * modification, are permitted provided that the following conditions
8 1.1 sommerfe * are met:
9 1.1 sommerfe * 1. Redistributions of source code must retain the above copyright
10 1.1 sommerfe * notice, this list of conditions and the following disclaimer.
11 1.1 sommerfe * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 sommerfe * notice, this list of conditions and the following disclaimer in the
13 1.1 sommerfe * documentation and/or other materials provided with the distribution.
14 1.1 sommerfe * 3. All advertising materials mentioning features or use of this software
15 1.1 sommerfe * must display the following acknowledgement:
16 1.1 sommerfe * This product includes software developed by Bill Paul.
17 1.1 sommerfe * 4. Neither the name of the author nor the names of any co-contributors
18 1.1 sommerfe * may be used to endorse or promote products derived from this software
19 1.1 sommerfe * without specific prior written permission.
20 1.1 sommerfe *
21 1.1 sommerfe * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22 1.1 sommerfe * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 sommerfe * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 sommerfe * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25 1.1 sommerfe * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1 sommerfe * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1 sommerfe * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 sommerfe * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 sommerfe * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 sommerfe * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 1.1 sommerfe * THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 sommerfe *
33 1.8 enami * From: Id: wicontrol.c,v 1.6 1999/05/22 16:12:49 wpaul Exp $
34 1.1 sommerfe */
35 1.1 sommerfe
36 1.1 sommerfe #include <sys/types.h>
37 1.1 sommerfe #include <sys/cdefs.h>
38 1.1 sommerfe #include <sys/param.h>
39 1.1 sommerfe #include <sys/socket.h>
40 1.1 sommerfe #include <sys/ioctl.h>
41 1.1 sommerfe #include <sys/socket.h>
42 1.1 sommerfe
43 1.1 sommerfe #include <net/if.h>
44 1.2 explorer #ifdef __FreeBSD__
45 1.2 explorer #include <net/if_var.h>
46 1.2 explorer #include <net/ethernet.h>
47 1.2 explorer
48 1.2 explorer #include <machine/if_wavelan_ieee.h>
49 1.2 explorer #else
50 1.2 explorer #include <netinet/in.h>
51 1.2 explorer #include <netinet/if_ether.h>
52 1.2 explorer #ifdef __NetBSD__
53 1.15 ichiro #include <net/if_ieee80211.h>
54 1.12 ichiro #include <dev/ic/wi_ieee.h>
55 1.2 explorer #else
56 1.2 explorer #include <dev/pcmcia/if_wavelan_ieee.h>
57 1.2 explorer #endif
58 1.2 explorer #endif
59 1.1 sommerfe
60 1.1 sommerfe #include <stdio.h>
61 1.1 sommerfe #include <string.h>
62 1.2 explorer #include <ctype.h>
63 1.1 sommerfe #include <stdlib.h>
64 1.1 sommerfe #include <unistd.h>
65 1.1 sommerfe #include <errno.h>
66 1.1 sommerfe #include <err.h>
67 1.1 sommerfe
68 1.1 sommerfe #if !defined(lint)
69 1.1 sommerfe static const char copyright[] = "@(#) Copyright (c) 1997, 1998, 1999\
70 1.1 sommerfe Bill Paul. All rights reserved.";
71 1.1 sommerfe static const char rcsid[] =
72 1.15 ichiro "@(#) $Id: wiconfig.c,v 1.15 2002/01/21 11:35:06 ichiro Exp $";
73 1.1 sommerfe #endif
74 1.1 sommerfe
75 1.7 enami struct wi_table {
76 1.7 enami int wi_type;
77 1.7 enami int wi_code;
78 1.7 enami #define WI_NONE 0x00
79 1.7 enami #define WI_STRING 0x01
80 1.7 enami #define WI_BOOL 0x02
81 1.7 enami #define WI_WORDS 0x03
82 1.7 enami #define WI_HEXBYTES 0x04
83 1.7 enami #define WI_KEYSTRUCT 0x05
84 1.7 enami char *wi_label; /* label used to print info */
85 1.7 enami int wi_opt; /* option character to set this */
86 1.7 enami char *wi_desc;
87 1.7 enami char *wi_optval;
88 1.7 enami };
89 1.7 enami
90 1.15 ichiro /* already define in wireg.h XXX */
91 1.15 ichiro #define WI_APRATE_0 0x00 /* NONE */
92 1.15 ichiro #define WI_APRATE_1 0x0A /* 1 Mbps */
93 1.15 ichiro #define WI_APRATE_2 0x14 /* 2 Mbps */
94 1.15 ichiro #define WI_APRATE_5 0x37 /* 5.5 Mbps */
95 1.15 ichiro #define WI_APRATE_11 0x6E /* 11 Mbps */
96 1.15 ichiro
97 1.15 ichiro static void wi_apscan __P((char *));
98 1.1 sommerfe static void wi_getval __P((char *, struct wi_req *));
99 1.1 sommerfe static void wi_setval __P((char *, struct wi_req *));
100 1.1 sommerfe static void wi_printstr __P((struct wi_req *));
101 1.1 sommerfe static void wi_setstr __P((char *, int, char *));
102 1.1 sommerfe static void wi_setbytes __P((char *, int, char *, int));
103 1.1 sommerfe static void wi_setword __P((char *, int, int));
104 1.1 sommerfe static void wi_sethex __P((char *, int, char *));
105 1.1 sommerfe static void wi_printwords __P((struct wi_req *));
106 1.1 sommerfe static void wi_printbool __P((struct wi_req *));
107 1.1 sommerfe static void wi_printhex __P((struct wi_req *));
108 1.1 sommerfe static void wi_dumpinfo __P((char *));
109 1.2 explorer static void wi_setkeys __P((char *, char *, int));
110 1.2 explorer static void wi_printkeys __P((struct wi_req *));
111 1.1 sommerfe static void wi_dumpstats __P((char *));
112 1.7 enami static void usage __P((void));
113 1.7 enami static struct wi_table *
114 1.7 enami wi_optlookup __P((struct wi_table *, int));
115 1.2 explorer static int wi_hex2int(char c);
116 1.2 explorer static void wi_str2key __P((char *, struct wi_key *));
117 1.1 sommerfe int main __P((int argc, char **argv));
118 1.1 sommerfe
119 1.15 ichiro static void wi_apscan(iface)
120 1.15 ichiro char *iface;
121 1.15 ichiro {
122 1.15 ichiro struct wi_req wreq;
123 1.15 ichiro struct ifreq ifr;
124 1.15 ichiro int s;
125 1.15 ichiro int naps, rate;
126 1.15 ichiro int retries = 10;
127 1.15 ichiro struct wi_apinfo *w;
128 1.15 ichiro int i, j;
129 1.15 ichiro
130 1.15 ichiro if (iface == NULL)
131 1.15 ichiro errx(1, "must specify interface name");
132 1.15 ichiro memset((char *)&wreq, 0, sizeof(wreq));
133 1.15 ichiro
134 1.15 ichiro wreq.wi_type = WI_RID_SCAN_APS;
135 1.15 ichiro wreq.wi_len = 4;
136 1.15 ichiro /* note chan. 1 is the least significant bit */
137 1.15 ichiro wreq.wi_val[0] = 0x7ff; /* 1 bit per channel, 1-11 */
138 1.15 ichiro wreq.wi_val[1] = 3; /* tx rate */
139 1.15 ichiro
140 1.15 ichiro /* write the request */
141 1.15 ichiro wi_setval(iface, &wreq);
142 1.15 ichiro
143 1.15 ichiro /* now poll for a result */
144 1.15 ichiro memset((char *)&wreq, 0, sizeof(wreq));
145 1.15 ichiro
146 1.15 ichiro wreq.wi_type = WI_RID_READ_APS;
147 1.15 ichiro wreq.wi_len = WI_MAX_DATALEN;
148 1.15 ichiro
149 1.15 ichiro /* we have to do this ourself as opposed to
150 1.15 ichiro * using setval, because we cannot bail if
151 1.15 ichiro * the ioctl fails
152 1.15 ichiro */
153 1.15 ichiro memset((char *)&ifr, 0, sizeof(ifr));
154 1.15 ichiro strcpy(ifr.ifr_name, iface);
155 1.15 ichiro ifr.ifr_data = (caddr_t)&wreq;
156 1.15 ichiro
157 1.15 ichiro s = socket(AF_INET, SOCK_DGRAM, 0);
158 1.15 ichiro
159 1.15 ichiro if (s == -1)
160 1.15 ichiro err(1, "socket");
161 1.15 ichiro
162 1.15 ichiro printf("scanning ...");
163 1.15 ichiro while (ioctl(s, SIOCGWAVELAN, &ifr) == -1) {
164 1.15 ichiro retries--;
165 1.15 ichiro if (retries >= 0) {
166 1.15 ichiro printf(".");
167 1.15 ichiro sleep(1);
168 1.15 ichiro } else
169 1.15 ichiro break;
170 1.15 ichiro errno = 0;
171 1.15 ichiro }
172 1.15 ichiro
173 1.15 ichiro close(s);
174 1.15 ichiro if (errno) {
175 1.15 ichiro err(1, "ioctl");
176 1.15 ichiro exit(1);
177 1.15 ichiro }
178 1.15 ichiro printf("\nAP Information\n");
179 1.15 ichiro
180 1.15 ichiro naps = (int)wreq.wi_val[0];
181 1.15 ichiro w = (struct wi_apinfo *)(((char *)&wreq.wi_val) + sizeof(int));
182 1.15 ichiro for ( i = 0; i < naps; i++, w++) {
183 1.15 ichiro printf("ap[%d]:\n", i);
184 1.15 ichiro if (w->scanreason) {
185 1.15 ichiro static char *scanm[] = {
186 1.15 ichiro "Host initiated",
187 1.15 ichiro "Firmware initiated",
188 1.15 ichiro "Inquiry request from host"
189 1.15 ichiro };
190 1.15 ichiro printf("\tScanReason:\t\t\t[ %s ]\n",
191 1.15 ichiro scanm[w->scanreason - 1]);
192 1.15 ichiro }
193 1.15 ichiro printf("\tnetname (SSID):\t\t\t[ ");
194 1.15 ichiro for (j = 0; j < w->namelen; j++) {
195 1.15 ichiro printf("%c", w->name[j]);
196 1.15 ichiro }
197 1.15 ichiro printf(" ]\n");
198 1.15 ichiro printf("\tBSSID:\t\t\t\t[ %02x:%02x:%02x:%02x:%02x:%02x ]\n",
199 1.15 ichiro w->bssid[0]&0xff, w->bssid[1]&0xff,
200 1.15 ichiro w->bssid[2]&0xff, w->bssid[3]&0xff,
201 1.15 ichiro w->bssid[4]&0xff, w->bssid[5]&0xff);
202 1.15 ichiro printf("\tChannel:\t\t\t[ %d ]\n", w->channel);
203 1.15 ichiro printf("\tQuality/Signal/Noise [signal]:\t[ %d / %d / %d ]\n"
204 1.15 ichiro "\t [dBm]:\t[ %d / %d / %d ]\n",
205 1.15 ichiro w->quality, w->signal, w->noise,
206 1.15 ichiro w->quality, w->signal - 149, w->noise - 149);
207 1.15 ichiro printf("\tBSS Beacon Interval [Kusec]:\t[ %d ]\n", w->interval);
208 1.15 ichiro printf("\tCapinfo:\t\t\t[ ");
209 1.15 ichiro if (w->capinfo & IEEE80211_CAPINFO_ESS)
210 1.15 ichiro printf("ESS ");
211 1.15 ichiro if (w->capinfo & IEEE80211_CAPINFO_PRIVACY)
212 1.15 ichiro printf("WEP ");
213 1.15 ichiro printf("]\n");
214 1.15 ichiro
215 1.15 ichiro switch (w->rate) {
216 1.15 ichiro case WI_APRATE_1:
217 1.15 ichiro rate = 1;
218 1.15 ichiro break;
219 1.15 ichiro case WI_APRATE_2:
220 1.15 ichiro rate = 2;
221 1.15 ichiro break;
222 1.15 ichiro case WI_APRATE_5:
223 1.15 ichiro rate = 5.5;
224 1.15 ichiro break;
225 1.15 ichiro case WI_APRATE_11:
226 1.15 ichiro rate = 11;
227 1.15 ichiro break;
228 1.15 ichiro case WI_APRATE_0:
229 1.15 ichiro default:
230 1.15 ichiro rate = 0;
231 1.15 ichiro break;
232 1.15 ichiro }
233 1.15 ichiro if (rate) printf("\tDataRate [Mbps]:\t\t[ %d ]\n", rate);
234 1.15 ichiro }
235 1.15 ichiro }
236 1.15 ichiro
237 1.1 sommerfe static void wi_getval(iface, wreq)
238 1.1 sommerfe char *iface;
239 1.1 sommerfe struct wi_req *wreq;
240 1.1 sommerfe {
241 1.1 sommerfe struct ifreq ifr;
242 1.1 sommerfe int s;
243 1.1 sommerfe
244 1.1 sommerfe bzero((char *)&ifr, sizeof(ifr));
245 1.1 sommerfe
246 1.1 sommerfe strcpy(ifr.ifr_name, iface);
247 1.1 sommerfe ifr.ifr_data = (caddr_t)wreq;
248 1.1 sommerfe
249 1.1 sommerfe s = socket(AF_INET, SOCK_DGRAM, 0);
250 1.1 sommerfe
251 1.1 sommerfe if (s == -1)
252 1.1 sommerfe err(1, "socket");
253 1.1 sommerfe
254 1.1 sommerfe if (ioctl(s, SIOCGWAVELAN, &ifr) == -1)
255 1.1 sommerfe err(1, "SIOCGWAVELAN");
256 1.1 sommerfe
257 1.1 sommerfe close(s);
258 1.1 sommerfe
259 1.1 sommerfe return;
260 1.1 sommerfe }
261 1.1 sommerfe
262 1.1 sommerfe static void wi_setval(iface, wreq)
263 1.1 sommerfe char *iface;
264 1.1 sommerfe struct wi_req *wreq;
265 1.1 sommerfe {
266 1.1 sommerfe struct ifreq ifr;
267 1.1 sommerfe int s;
268 1.1 sommerfe
269 1.1 sommerfe bzero((char *)&ifr, sizeof(ifr));
270 1.1 sommerfe
271 1.1 sommerfe strcpy(ifr.ifr_name, iface);
272 1.1 sommerfe ifr.ifr_data = (caddr_t)wreq;
273 1.1 sommerfe
274 1.1 sommerfe s = socket(AF_INET, SOCK_DGRAM, 0);
275 1.1 sommerfe
276 1.1 sommerfe if (s == -1)
277 1.1 sommerfe err(1, "socket");
278 1.1 sommerfe
279 1.1 sommerfe if (ioctl(s, SIOCSWAVELAN, &ifr) == -1)
280 1.1 sommerfe err(1, "SIOCSWAVELAN");
281 1.1 sommerfe
282 1.1 sommerfe close(s);
283 1.1 sommerfe
284 1.1 sommerfe return;
285 1.1 sommerfe }
286 1.1 sommerfe
287 1.1 sommerfe void wi_printstr(wreq)
288 1.1 sommerfe struct wi_req *wreq;
289 1.1 sommerfe {
290 1.1 sommerfe char *ptr;
291 1.1 sommerfe int i;
292 1.1 sommerfe
293 1.1 sommerfe if (wreq->wi_type == WI_RID_SERIALNO) {
294 1.1 sommerfe ptr = (char *)&wreq->wi_val;
295 1.1 sommerfe for (i = 0; i < (wreq->wi_len - 1) * 2; i++) {
296 1.1 sommerfe if (ptr[i] == '\0')
297 1.1 sommerfe ptr[i] = ' ';
298 1.1 sommerfe }
299 1.1 sommerfe } else {
300 1.14 tsubai int len = le16toh(wreq->wi_val[0]);
301 1.14 tsubai
302 1.1 sommerfe ptr = (char *)&wreq->wi_val[1];
303 1.14 tsubai for (i = 0; i < len; i++) {
304 1.1 sommerfe if (ptr[i] == '\0')
305 1.1 sommerfe ptr[i] = ' ';
306 1.1 sommerfe }
307 1.1 sommerfe }
308 1.1 sommerfe
309 1.1 sommerfe ptr[i] = '\0';
310 1.1 sommerfe printf("[ %s ]", ptr);
311 1.1 sommerfe
312 1.1 sommerfe return;
313 1.1 sommerfe }
314 1.1 sommerfe
315 1.1 sommerfe void wi_setstr(iface, code, str)
316 1.1 sommerfe char *iface;
317 1.1 sommerfe int code;
318 1.1 sommerfe char *str;
319 1.1 sommerfe {
320 1.1 sommerfe struct wi_req wreq;
321 1.1 sommerfe
322 1.1 sommerfe bzero((char *)&wreq, sizeof(wreq));
323 1.1 sommerfe
324 1.1 sommerfe if (strlen(str) > 30)
325 1.1 sommerfe errx(1, "string too long");
326 1.1 sommerfe
327 1.1 sommerfe wreq.wi_type = code;
328 1.1 sommerfe wreq.wi_len = 18;
329 1.14 tsubai wreq.wi_val[0] = htole16(strlen(str));
330 1.1 sommerfe bcopy(str, (char *)&wreq.wi_val[1], strlen(str));
331 1.1 sommerfe
332 1.1 sommerfe wi_setval(iface, &wreq);
333 1.1 sommerfe
334 1.1 sommerfe return;
335 1.1 sommerfe }
336 1.1 sommerfe
337 1.1 sommerfe void wi_setbytes(iface, code, bytes, len)
338 1.1 sommerfe char *iface;
339 1.1 sommerfe int code;
340 1.1 sommerfe char *bytes;
341 1.1 sommerfe int len;
342 1.1 sommerfe {
343 1.1 sommerfe struct wi_req wreq;
344 1.1 sommerfe
345 1.1 sommerfe bzero((char *)&wreq, sizeof(wreq));
346 1.1 sommerfe
347 1.1 sommerfe wreq.wi_type = code;
348 1.1 sommerfe wreq.wi_len = (len / 2) + 1;
349 1.1 sommerfe bcopy(bytes, (char *)&wreq.wi_val[0], len);
350 1.1 sommerfe
351 1.1 sommerfe wi_setval(iface, &wreq);
352 1.1 sommerfe
353 1.1 sommerfe return;
354 1.1 sommerfe }
355 1.1 sommerfe
356 1.1 sommerfe void wi_setword(iface, code, word)
357 1.1 sommerfe char *iface;
358 1.1 sommerfe int code;
359 1.1 sommerfe int word;
360 1.1 sommerfe {
361 1.1 sommerfe struct wi_req wreq;
362 1.1 sommerfe
363 1.1 sommerfe bzero((char *)&wreq, sizeof(wreq));
364 1.1 sommerfe
365 1.1 sommerfe wreq.wi_type = code;
366 1.1 sommerfe wreq.wi_len = 2;
367 1.14 tsubai wreq.wi_val[0] = htole16(word);
368 1.1 sommerfe
369 1.1 sommerfe wi_setval(iface, &wreq);
370 1.1 sommerfe
371 1.1 sommerfe return;
372 1.1 sommerfe }
373 1.1 sommerfe
374 1.1 sommerfe void wi_sethex(iface, code, str)
375 1.1 sommerfe char *iface;
376 1.1 sommerfe int code;
377 1.1 sommerfe char *str;
378 1.1 sommerfe {
379 1.1 sommerfe struct ether_addr *addr;
380 1.1 sommerfe
381 1.1 sommerfe addr = ether_aton(str);
382 1.1 sommerfe if (addr == NULL)
383 1.1 sommerfe errx(1, "badly formatted address");
384 1.1 sommerfe
385 1.1 sommerfe wi_setbytes(iface, code, (char *)addr, ETHER_ADDR_LEN);
386 1.1 sommerfe
387 1.1 sommerfe return;
388 1.1 sommerfe }
389 1.1 sommerfe
390 1.2 explorer static int
391 1.2 explorer wi_hex2int(char c)
392 1.2 explorer {
393 1.2 explorer if (c >= '0' && c <= '9')
394 1.2 explorer return (c - '0');
395 1.2 explorer if (c >= 'A' && c <= 'F')
396 1.2 explorer return (c - 'A' + 10);
397 1.2 explorer if (c >= 'a' && c <= 'f')
398 1.2 explorer return (c - 'a' + 10);
399 1.2 explorer
400 1.2 explorer return (0);
401 1.2 explorer }
402 1.2 explorer
403 1.2 explorer static void wi_str2key(s, k)
404 1.2 explorer char *s;
405 1.2 explorer struct wi_key *k;
406 1.2 explorer {
407 1.2 explorer int n, i;
408 1.2 explorer char *p;
409 1.2 explorer
410 1.2 explorer /* Is this a hex string? */
411 1.2 explorer if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
412 1.2 explorer /* Yes, convert to int. */
413 1.2 explorer n = 0;
414 1.2 explorer p = (char *)&k->wi_keydat[0];
415 1.2 explorer for (i = 2; i < strlen(s); i+= 2) {
416 1.2 explorer *p++ = (wi_hex2int(s[i]) << 4) + wi_hex2int(s[i + 1]);
417 1.2 explorer n++;
418 1.2 explorer }
419 1.14 tsubai k->wi_keylen = htole16(n);
420 1.2 explorer } else {
421 1.2 explorer /* No, just copy it in. */
422 1.2 explorer bcopy(s, k->wi_keydat, strlen(s));
423 1.14 tsubai k->wi_keylen = htole16(strlen(s));
424 1.2 explorer }
425 1.2 explorer
426 1.2 explorer return;
427 1.2 explorer }
428 1.2 explorer
429 1.2 explorer static void wi_setkeys(iface, key, idx)
430 1.2 explorer char *iface;
431 1.2 explorer char *key;
432 1.2 explorer int idx;
433 1.2 explorer {
434 1.2 explorer struct wi_req wreq;
435 1.2 explorer struct wi_ltv_keys *keys;
436 1.2 explorer struct wi_key *k;
437 1.2 explorer
438 1.2 explorer bzero((char *)&wreq, sizeof(wreq));
439 1.2 explorer wreq.wi_len = WI_MAX_DATALEN;
440 1.2 explorer wreq.wi_type = WI_RID_WEP_AVAIL;
441 1.2 explorer
442 1.2 explorer wi_getval(iface, &wreq);
443 1.14 tsubai if (le16toh(wreq.wi_val[0]) == 0)
444 1.2 explorer err(1, "no WEP option available on this card");
445 1.2 explorer
446 1.2 explorer bzero((char *)&wreq, sizeof(wreq));
447 1.2 explorer wreq.wi_len = WI_MAX_DATALEN;
448 1.2 explorer wreq.wi_type = WI_RID_DEFLT_CRYPT_KEYS;
449 1.2 explorer
450 1.2 explorer wi_getval(iface, &wreq);
451 1.2 explorer keys = (struct wi_ltv_keys *)&wreq;
452 1.2 explorer
453 1.2 explorer if (key[0] == '0' && (key[1] == 'x' || key[1] == 'X')) {
454 1.2 explorer if (strlen(key) > 30)
455 1.2 explorer err(1, "encryption key must be no "
456 1.2 explorer "more than 28 hex digits long");
457 1.2 explorer } else {
458 1.2 explorer if (strlen(key) > 14)
459 1.2 explorer err(1, "encryption key must be no "
460 1.2 explorer "more than 14 characters long");
461 1.2 explorer }
462 1.2 explorer
463 1.2 explorer if (idx > 3)
464 1.2 explorer err(1, "only 4 encryption keys available");
465 1.2 explorer
466 1.2 explorer k = &keys->wi_keys[idx];
467 1.2 explorer wi_str2key(key, k);
468 1.2 explorer
469 1.2 explorer wreq.wi_len = (sizeof(struct wi_ltv_keys) / 2) + 1;
470 1.2 explorer wreq.wi_type = WI_RID_DEFLT_CRYPT_KEYS;
471 1.2 explorer wi_setval(iface, &wreq);
472 1.2 explorer
473 1.2 explorer return;
474 1.2 explorer }
475 1.2 explorer
476 1.2 explorer static void wi_printkeys(wreq)
477 1.2 explorer struct wi_req *wreq;
478 1.2 explorer {
479 1.2 explorer int i, j, bn;
480 1.2 explorer struct wi_key *k;
481 1.2 explorer struct wi_ltv_keys *keys;
482 1.2 explorer char *ptr;
483 1.2 explorer
484 1.2 explorer keys = (struct wi_ltv_keys *)wreq;
485 1.2 explorer
486 1.2 explorer for (i = 0, bn = 0; i < 4; i++, bn = 0) {
487 1.2 explorer k = &keys->wi_keys[i];
488 1.2 explorer ptr = (char *)k->wi_keydat;
489 1.14 tsubai for (j = 0; j < le16toh(k->wi_keylen); j++) {
490 1.10 jdolecek if (!isprint((unsigned char) ptr[j])) {
491 1.2 explorer bn = 1;
492 1.2 explorer break;
493 1.2 explorer }
494 1.2 explorer }
495 1.2 explorer
496 1.2 explorer if (bn) {
497 1.2 explorer printf("[ 0x");
498 1.14 tsubai for (j = 0; j < le16toh(k->wi_keylen); j++)
499 1.2 explorer printf("%02x", ((unsigned char *) ptr)[j]);
500 1.2 explorer printf(" ]");
501 1.2 explorer } else {
502 1.2 explorer ptr[j] = '\0';
503 1.2 explorer printf("[ %s ]", ptr);
504 1.2 explorer }
505 1.2 explorer }
506 1.2 explorer
507 1.2 explorer return;
508 1.2 explorer };
509 1.2 explorer
510 1.1 sommerfe void wi_printwords(wreq)
511 1.1 sommerfe struct wi_req *wreq;
512 1.1 sommerfe {
513 1.1 sommerfe int i;
514 1.1 sommerfe
515 1.1 sommerfe printf("[ ");
516 1.1 sommerfe for (i = 0; i < wreq->wi_len - 1; i++)
517 1.14 tsubai printf("%d ", le16toh(wreq->wi_val[i]));
518 1.1 sommerfe printf("]");
519 1.1 sommerfe
520 1.1 sommerfe return;
521 1.1 sommerfe }
522 1.1 sommerfe
523 1.1 sommerfe void wi_printbool(wreq)
524 1.1 sommerfe struct wi_req *wreq;
525 1.1 sommerfe {
526 1.14 tsubai if (le16toh(wreq->wi_val[0]))
527 1.1 sommerfe printf("[ On ]");
528 1.1 sommerfe else
529 1.1 sommerfe printf("[ Off ]");
530 1.1 sommerfe
531 1.1 sommerfe return;
532 1.1 sommerfe }
533 1.1 sommerfe
534 1.1 sommerfe void wi_printhex(wreq)
535 1.1 sommerfe struct wi_req *wreq;
536 1.1 sommerfe {
537 1.1 sommerfe int i;
538 1.1 sommerfe unsigned char *c;
539 1.1 sommerfe
540 1.1 sommerfe c = (unsigned char *)&wreq->wi_val;
541 1.1 sommerfe
542 1.1 sommerfe printf("[ ");
543 1.1 sommerfe for (i = 0; i < (wreq->wi_len - 1) * 2; i++) {
544 1.1 sommerfe printf("%02x", c[i]);
545 1.1 sommerfe if (i < ((wreq->wi_len - 1) * 2) - 1)
546 1.1 sommerfe printf(":");
547 1.1 sommerfe }
548 1.1 sommerfe
549 1.1 sommerfe printf(" ]");
550 1.1 sommerfe return;
551 1.1 sommerfe }
552 1.1 sommerfe
553 1.1 sommerfe static struct wi_table wi_table[] = {
554 1.1 sommerfe { WI_RID_SERIALNO, WI_STRING, "NIC serial number:\t\t\t" },
555 1.7 enami { WI_RID_NODENAME, WI_STRING, "Station name:\t\t\t\t",
556 1.7 enami 's', "station name" },
557 1.7 enami { WI_RID_OWN_SSID, WI_STRING, "SSID for IBSS creation:\t\t\t",
558 1.7 enami 'q', "own SSID" },
559 1.1 sommerfe { WI_RID_CURRENT_SSID, WI_STRING, "Current netname (SSID):\t\t\t" },
560 1.7 enami { WI_RID_DESIRED_SSID, WI_STRING, "Desired netname (SSID):\t\t\t",
561 1.7 enami 'n', "network name" },
562 1.1 sommerfe { WI_RID_CURRENT_BSSID, WI_HEXBYTES, "Current BSSID:\t\t\t\t" },
563 1.1 sommerfe { WI_RID_CHANNEL_LIST, WI_WORDS, "Channel list:\t\t\t\t" },
564 1.7 enami { WI_RID_OWN_CHNL, WI_WORDS, "IBSS channel:\t\t\t\t",
565 1.7 enami 'f', "frequency" },
566 1.1 sommerfe { WI_RID_CURRENT_CHAN, WI_WORDS, "Current channel:\t\t\t" },
567 1.1 sommerfe { WI_RID_COMMS_QUALITY, WI_WORDS, "Comms quality/signal/noise:\t\t" },
568 1.1 sommerfe { WI_RID_PROMISC, WI_BOOL, "Promiscuous mode:\t\t\t" },
569 1.7 enami { WI_RID_PORTTYPE, WI_WORDS, "Port type (1=BSS, 3=ad-hoc):\t\t",
570 1.7 enami 'p', "port type" },
571 1.7 enami { WI_RID_MAC_NODE, WI_HEXBYTES, "MAC address:\t\t\t\t",
572 1.7 enami 'm', "MAC address" },
573 1.7 enami { WI_RID_TX_RATE, WI_WORDS, "TX rate (selection):\t\t\t",
574 1.7 enami 't', "TX rate" },
575 1.2 explorer { WI_RID_CUR_TX_RATE, WI_WORDS, "TX rate (actual speed):\t\t\t"},
576 1.7 enami { WI_RID_MAX_DATALEN, WI_WORDS, "Maximum data length:\t\t\t",
577 1.7 enami 'd', "maximum data length" },
578 1.7 enami { WI_RID_RTS_THRESH, WI_WORDS, "RTS/CTS handshake threshold:\t\t",
579 1.7 enami 'r', "RTS threshold" },
580 1.7 enami { WI_RID_CREATE_IBSS, WI_BOOL, "Create IBSS:\t\t\t\t",
581 1.7 enami 'c', "create ibss" },
582 1.13 ichiro { WI_RID_MICROWAVE_OVEN, WI_WORDS, "Microwave oven robustness:\t\t",
583 1.15 ichiro 'M', "microwave oven robustness enabled" },
584 1.13 ichiro { WI_RID_ROAMING_MODE, WI_WORDS, "Roaming mode(1:firm,3:disable):\t\t",
585 1.15 ichiro 'R', "roaming mode" },
586 1.7 enami { WI_RID_SYSTEM_SCALE, WI_WORDS, "Access point density:\t\t\t",
587 1.7 enami 'a', "system scale" },
588 1.7 enami { WI_RID_PM_ENABLED, WI_WORDS, "Power Mgmt (1=on, 0=off):\t\t",
589 1.7 enami 'P', "power management enabled" },
590 1.13 ichiro { WI_RID_MAX_SLEEP, WI_WORDS, "Max sleep time (msec):\t\t\t",
591 1.7 enami 'S', "max sleep duration" },
592 1.7 enami { 0, WI_NONE }
593 1.1 sommerfe };
594 1.1 sommerfe
595 1.2 explorer static struct wi_table wi_crypt_table[] = {
596 1.7 enami { WI_RID_ENCRYPTION, WI_BOOL, "WEP encryption:\t\t\t\t",
597 1.7 enami 'e', "encryption" },
598 1.13 ichiro { WI_RID_AUTH_CNTL, WI_WORDS, "Authentication type \n(1=OpenSys, 2=Shared Key):\t\t",
599 1.13 ichiro 'A', "authentication type" },
600 1.2 explorer { WI_RID_TX_CRYPT_KEY, WI_WORDS, "TX encryption key:\t\t\t" },
601 1.2 explorer { WI_RID_DEFLT_CRYPT_KEYS, WI_KEYSTRUCT, "Encryption keys:\t\t\t" },
602 1.7 enami { 0, WI_NONE }
603 1.7 enami };
604 1.7 enami
605 1.7 enami static struct wi_table *wi_tables[] = {
606 1.7 enami wi_table,
607 1.7 enami wi_crypt_table,
608 1.7 enami NULL
609 1.2 explorer };
610 1.2 explorer
611 1.7 enami static struct wi_table *
612 1.7 enami wi_optlookup(table, opt)
613 1.7 enami struct wi_table *table;
614 1.7 enami int opt;
615 1.7 enami {
616 1.7 enami struct wi_table *wt;
617 1.7 enami
618 1.7 enami for (wt = table; wt->wi_type != 0; wt++)
619 1.7 enami if (wt->wi_opt == opt)
620 1.7 enami return (wt);
621 1.7 enami return (NULL);
622 1.7 enami }
623 1.7 enami
624 1.1 sommerfe static void wi_dumpinfo(iface)
625 1.1 sommerfe char *iface;
626 1.1 sommerfe {
627 1.1 sommerfe struct wi_req wreq;
628 1.2 explorer int i, has_wep;
629 1.1 sommerfe struct wi_table *w;
630 1.1 sommerfe
631 1.2 explorer bzero((char *)&wreq, sizeof(wreq));
632 1.2 explorer
633 1.2 explorer wreq.wi_len = WI_MAX_DATALEN;
634 1.2 explorer wreq.wi_type = WI_RID_WEP_AVAIL;
635 1.2 explorer
636 1.2 explorer wi_getval(iface, &wreq);
637 1.14 tsubai has_wep = le16toh(wreq.wi_val[0]);
638 1.2 explorer
639 1.1 sommerfe w = wi_table;
640 1.1 sommerfe
641 1.7 enami for (i = 0; w[i].wi_code != WI_NONE; i++) {
642 1.1 sommerfe bzero((char *)&wreq, sizeof(wreq));
643 1.1 sommerfe
644 1.1 sommerfe wreq.wi_len = WI_MAX_DATALEN;
645 1.7 enami wreq.wi_type = w[i].wi_type;
646 1.1 sommerfe
647 1.1 sommerfe wi_getval(iface, &wreq);
648 1.7 enami printf("%s", w[i].wi_label);
649 1.7 enami switch (w[i].wi_code) {
650 1.1 sommerfe case WI_STRING:
651 1.1 sommerfe wi_printstr(&wreq);
652 1.1 sommerfe break;
653 1.1 sommerfe case WI_WORDS:
654 1.1 sommerfe wi_printwords(&wreq);
655 1.1 sommerfe break;
656 1.1 sommerfe case WI_BOOL:
657 1.1 sommerfe wi_printbool(&wreq);
658 1.1 sommerfe break;
659 1.1 sommerfe case WI_HEXBYTES:
660 1.1 sommerfe wi_printhex(&wreq);
661 1.1 sommerfe break;
662 1.1 sommerfe default:
663 1.1 sommerfe break;
664 1.1 sommerfe }
665 1.1 sommerfe printf("\n");
666 1.1 sommerfe }
667 1.1 sommerfe
668 1.2 explorer if (has_wep) {
669 1.2 explorer w = wi_crypt_table;
670 1.7 enami for (i = 0; w[i].wi_code != WI_NONE; i++) {
671 1.2 explorer bzero((char *)&wreq, sizeof(wreq));
672 1.2 explorer
673 1.2 explorer wreq.wi_len = WI_MAX_DATALEN;
674 1.7 enami wreq.wi_type = w[i].wi_type;
675 1.2 explorer
676 1.2 explorer wi_getval(iface, &wreq);
677 1.7 enami printf("%s", w[i].wi_label);
678 1.7 enami switch (w[i].wi_code) {
679 1.2 explorer case WI_STRING:
680 1.2 explorer wi_printstr(&wreq);
681 1.2 explorer break;
682 1.2 explorer case WI_WORDS:
683 1.2 explorer if (wreq.wi_type == WI_RID_TX_CRYPT_KEY)
684 1.14 tsubai wreq.wi_val[0] =
685 1.14 tsubai htole16(le16toh(wreq.wi_val[0]) + 1);
686 1.2 explorer wi_printwords(&wreq);
687 1.2 explorer break;
688 1.2 explorer case WI_BOOL:
689 1.2 explorer wi_printbool(&wreq);
690 1.2 explorer break;
691 1.2 explorer case WI_HEXBYTES:
692 1.2 explorer wi_printhex(&wreq);
693 1.2 explorer break;
694 1.2 explorer case WI_KEYSTRUCT:
695 1.2 explorer wi_printkeys(&wreq);
696 1.2 explorer break;
697 1.2 explorer default:
698 1.2 explorer break;
699 1.2 explorer }
700 1.2 explorer printf("\n");
701 1.2 explorer }
702 1.2 explorer }
703 1.2 explorer
704 1.1 sommerfe return;
705 1.1 sommerfe }
706 1.1 sommerfe
707 1.1 sommerfe static void wi_dumpstats(iface)
708 1.1 sommerfe char *iface;
709 1.1 sommerfe {
710 1.1 sommerfe struct wi_req wreq;
711 1.1 sommerfe struct wi_counters *c;
712 1.1 sommerfe
713 1.1 sommerfe bzero((char *)&wreq, sizeof(wreq));
714 1.1 sommerfe wreq.wi_len = WI_MAX_DATALEN;
715 1.1 sommerfe wreq.wi_type = WI_RID_IFACE_STATS;
716 1.1 sommerfe
717 1.1 sommerfe wi_getval(iface, &wreq);
718 1.1 sommerfe
719 1.1 sommerfe c = (struct wi_counters *)&wreq.wi_val;
720 1.1 sommerfe
721 1.14 tsubai /* XXX native byte order */
722 1.1 sommerfe printf("Transmitted unicast frames:\t\t%d\n",
723 1.1 sommerfe c->wi_tx_unicast_frames);
724 1.1 sommerfe printf("Transmitted multicast frames:\t\t%d\n",
725 1.1 sommerfe c->wi_tx_multicast_frames);
726 1.1 sommerfe printf("Transmitted fragments:\t\t\t%d\n",
727 1.1 sommerfe c->wi_tx_fragments);
728 1.1 sommerfe printf("Transmitted unicast octets:\t\t%d\n",
729 1.1 sommerfe c->wi_tx_unicast_octets);
730 1.1 sommerfe printf("Transmitted multicast octets:\t\t%d\n",
731 1.1 sommerfe c->wi_tx_multicast_octets);
732 1.1 sommerfe printf("Single transmit retries:\t\t%d\n",
733 1.1 sommerfe c->wi_tx_single_retries);
734 1.1 sommerfe printf("Multiple transmit retries:\t\t%d\n",
735 1.1 sommerfe c->wi_tx_multi_retries);
736 1.1 sommerfe printf("Transmit retry limit exceeded:\t\t%d\n",
737 1.1 sommerfe c->wi_tx_retry_limit);
738 1.1 sommerfe printf("Transmit discards:\t\t\t%d\n",
739 1.1 sommerfe c->wi_tx_discards);
740 1.1 sommerfe printf("Transmit discards due to wrong SA:\t%d\n",
741 1.1 sommerfe c->wi_tx_discards_wrong_sa);
742 1.1 sommerfe printf("Received unicast frames:\t\t%d\n",
743 1.1 sommerfe c->wi_rx_unicast_frames);
744 1.1 sommerfe printf("Received multicast frames:\t\t%d\n",
745 1.1 sommerfe c->wi_rx_multicast_frames);
746 1.1 sommerfe printf("Received fragments:\t\t\t%d\n",
747 1.1 sommerfe c->wi_rx_fragments);
748 1.1 sommerfe printf("Received unicast octets:\t\t%d\n",
749 1.1 sommerfe c->wi_rx_unicast_octets);
750 1.1 sommerfe printf("Received multicast octets:\t\t%d\n",
751 1.1 sommerfe c->wi_rx_multicast_octets);
752 1.1 sommerfe printf("Receive FCS errors:\t\t\t%d\n",
753 1.1 sommerfe c->wi_rx_fcs_errors);
754 1.1 sommerfe printf("Receive discards due to no buffer:\t%d\n",
755 1.1 sommerfe c->wi_rx_discards_nobuf);
756 1.1 sommerfe printf("Can't decrypt WEP frame:\t\t%d\n",
757 1.1 sommerfe c->wi_rx_WEP_cant_decrypt);
758 1.1 sommerfe printf("Received message fragments:\t\t%d\n",
759 1.1 sommerfe c->wi_rx_msg_in_msg_frags);
760 1.1 sommerfe printf("Received message bad fragments:\t\t%d\n",
761 1.1 sommerfe c->wi_rx_msg_in_bad_msg_frags);
762 1.1 sommerfe
763 1.1 sommerfe return;
764 1.1 sommerfe }
765 1.1 sommerfe
766 1.7 enami static void
767 1.7 enami usage()
768 1.1 sommerfe {
769 1.7 enami
770 1.2 explorer fprintf(stderr,
771 1.7 enami "usage: %s interface "
772 1.15 ichiro "[-oD] [-t tx rate] [-n network name] [-s station name]\n"
773 1.2 explorer " [-e 0|1] [-k key [-v 1|2|3|4]] [-T 1|2|3|4]\n"
774 1.2 explorer " [-c 0|1] [-q SSID] [-p port type] [-a access point density]\n"
775 1.2 explorer " [-m MAC address] [-d max data length] [-r RTS threshold]\n"
776 1.9 jhawk " [-f frequency] [-M 0|1] [-P 0|1] [-S max sleep duration]\n"
777 1.13 ichiro " [-A 0|1 ] [-R 1|3]\n"
778 1.9 jhawk ,
779 1.11 cgd getprogname());
780 1.1 sommerfe exit(1);
781 1.1 sommerfe }
782 1.1 sommerfe
783 1.1 sommerfe int main(argc, argv)
784 1.1 sommerfe int argc;
785 1.1 sommerfe char *argv[];
786 1.1 sommerfe {
787 1.7 enami struct wi_table *wt, **table;
788 1.7 enami char *iface, *key, *keyv[4], *tx_crypt_key;
789 1.15 ichiro int ch, dumpinfo, dumpstats, modifier, oldind, apscan;
790 1.7 enami
791 1.7 enami #define SET_OPERAND(opr, desc) do { \
792 1.7 enami if ((opr) == NULL) \
793 1.7 enami (opr) = optarg; \
794 1.7 enami else \
795 1.7 enami warnx("%s is already specified to %s", \
796 1.7 enami desc, (opr)); \
797 1.7 enami } while (0)
798 1.7 enami
799 1.7 enami dumpinfo = 1;
800 1.7 enami dumpstats = 0;
801 1.15 ichiro apscan = 0;
802 1.7 enami iface = key = keyv[0] = keyv[1] = keyv[2] = keyv[3] =
803 1.7 enami tx_crypt_key = NULL;
804 1.2 explorer
805 1.2 explorer if (argc > 1 && argv[1][0] != '-') {
806 1.2 explorer iface = argv[1];
807 1.5 enami optind++;
808 1.2 explorer }
809 1.1 sommerfe
810 1.3 itojun while ((ch = getopt(argc, argv,
811 1.15 ichiro "a:c:d:e:f:hi:k:m:n:op:q:r:s:t:A:M:S:P:R:T:DZ:")) != -1) {
812 1.7 enami if (ch != 'i')
813 1.7 enami dumpinfo = 0;
814 1.7 enami /*
815 1.7 enami * Lookup generic options and remeber operand if found.
816 1.7 enami */
817 1.7 enami for (table = wi_tables; *table != NULL; table++)
818 1.7 enami if ((wt = wi_optlookup(*table, ch)) != NULL) {
819 1.7 enami SET_OPERAND(wt->wi_optval, wt->wi_desc);
820 1.7 enami break;
821 1.7 enami }
822 1.7 enami if (wt == NULL)
823 1.7 enami /*
824 1.7 enami * Handle special options.
825 1.7 enami */
826 1.7 enami switch (ch) {
827 1.7 enami case 'o':
828 1.7 enami dumpstats = 1;
829 1.7 enami break;
830 1.7 enami case 'i':
831 1.7 enami SET_OPERAND(iface, "interface");
832 1.7 enami break;
833 1.7 enami case 'k':
834 1.7 enami key = optarg;
835 1.7 enami oldind = optind;
836 1.7 enami opterr = 0;
837 1.7 enami ch = getopt(argc, argv, "v:");
838 1.7 enami opterr = 1;
839 1.7 enami switch (ch) {
840 1.7 enami case 'v':
841 1.7 enami modifier = atoi(optarg) - 1;
842 1.7 enami break;
843 1.7 enami default:
844 1.7 enami modifier = 0;
845 1.7 enami optind = oldind;
846 1.7 enami break;
847 1.7 enami }
848 1.7 enami keyv[modifier] = key;
849 1.7 enami break;
850 1.7 enami case 'T':
851 1.7 enami SET_OPERAND(tx_crypt_key, "TX encryption key");
852 1.7 enami break;
853 1.15 ichiro case 'D':
854 1.15 ichiro apscan = 1;
855 1.15 ichiro break;
856 1.7 enami case 'h':
857 1.7 enami default:
858 1.7 enami usage();
859 1.7 enami break;
860 1.7 enami }
861 1.1 sommerfe }
862 1.1 sommerfe
863 1.1 sommerfe if (iface == NULL)
864 1.7 enami usage();
865 1.2 explorer
866 1.7 enami for (table = wi_tables; *table != NULL; table++)
867 1.7 enami for (wt = *table; wt->wi_code != WI_NONE; wt++)
868 1.7 enami if (wt->wi_optval != NULL) {
869 1.7 enami switch (wt->wi_code) {
870 1.7 enami case WI_BOOL:
871 1.7 enami case WI_WORDS:
872 1.7 enami wi_setword(iface, wt->wi_type,
873 1.7 enami atoi(wt->wi_optval));
874 1.7 enami break;
875 1.7 enami case WI_STRING:
876 1.7 enami wi_setstr(iface, wt->wi_type,
877 1.7 enami wt->wi_optval);
878 1.7 enami break;
879 1.7 enami case WI_HEXBYTES:
880 1.7 enami wi_sethex(iface, wt->wi_type,
881 1.7 enami wt->wi_optval);
882 1.7 enami break;
883 1.7 enami }
884 1.7 enami }
885 1.7 enami
886 1.7 enami if (tx_crypt_key != NULL)
887 1.7 enami wi_setword(iface, WI_RID_TX_CRYPT_KEY, atoi(tx_crypt_key) - 1);
888 1.1 sommerfe
889 1.7 enami for (modifier = 0; modifier < sizeof(keyv) / sizeof(keyv[0]);
890 1.7 enami modifier++)
891 1.7 enami if (keyv[modifier] != NULL)
892 1.7 enami wi_setkeys(iface, keyv[modifier], modifier);
893 1.7 enami
894 1.7 enami if (dumpstats)
895 1.7 enami wi_dumpstats(iface);
896 1.7 enami if (dumpinfo)
897 1.7 enami wi_dumpinfo(iface);
898 1.15 ichiro if (apscan)
899 1.15 ichiro wi_apscan(iface);
900 1.1 sommerfe
901 1.1 sommerfe exit(0);
902 1.1 sommerfe }
903