Merge from vendor branch FILE:
[dragonfly.git] / sbin / ifconfig / ifieee80211.c
1 /*
2  * Copyright 2001 The Aerospace Corporation.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of The Aerospace Corporation may not be used to endorse or
13  *    promote products derived from this software.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sbin/ifconfig/ifieee80211.c,v 1.18.2.9 2006/03/07 17:50:23 sam Exp $
28  * $DragonFly: src/sbin/ifconfig/ifieee80211.c,v 1.14 2006/09/03 02:17:54 sephe Exp $
29  */
30
31 /*-
32  * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
33  * All rights reserved.
34  *
35  * This code is derived from software contributed to The NetBSD Foundation
36  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
37  * NASA Ames Research Center.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgement:
49  *      This product includes software developed by the NetBSD
50  *      Foundation, Inc. and its contributors.
51  * 4. Neither the name of The NetBSD Foundation nor the names of its
52  *    contributors may be used to endorse or promote products derived
53  *    from this software without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
56  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
59  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
61  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
62  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
64  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
65  * POSSIBILITY OF SUCH DAMAGE.
66  */
67
68 #include <sys/param.h>
69 #include <sys/ioctl.h>
70 #include <sys/socket.h>
71 #include <sys/sysctl.h>
72 #include <sys/time.h>
73
74 #include <net/ethernet.h>
75 #include <net/if.h>
76 #include <net/if_dl.h>
77 #include <net/if_types.h>
78 #include <net/if_media.h>
79 #include <net/route.h>
80
81 #include <netproto/802_11/ieee80211.h>
82 #include <netproto/802_11/ieee80211_crypto.h>
83 #include <netproto/802_11/ieee80211_ioctl.h>
84 #include <netproto/802_11/ieee80211_ratectl.h>
85
86 #include <ctype.h>
87 #include <err.h>
88 #include <errno.h>
89 #include <fcntl.h>
90 #include <inttypes.h>
91 #include <stdio.h>
92 #include <stdlib.h>
93 #include <string.h>
94 #include <unistd.h>
95 #include <stdarg.h>
96
97 #include "ifconfig.h"
98
99 static void set80211(int s, int type, int val, int len, u_int8_t *data);
100 static const char *get_string(const char *val, const char *sep,
101     u_int8_t *buf, int *lenp);
102 static void print_string(const u_int8_t *buf, int len);
103
104 static int
105 isanyarg(const char *arg)
106 {
107         return (strcmp(arg, "-") == 0 ||
108             strcasecmp(arg, "any") == 0 || strcasecmp(arg, "off") == 0);
109 }
110
111 static void
112 set80211ssid(const char *val, int d, int s, const struct afswtch *rafp)
113 {
114         int             ssid;
115         int             len;
116         u_int8_t        data[IEEE80211_NWID_LEN];
117
118         ssid = 0;
119         len = strlen(val);
120         if (len > 2 && isdigit(val[0]) && val[1] == ':') {
121                 ssid = atoi(val)-1;
122                 val += 2;
123         }
124
125         bzero(data, sizeof(data));
126         len = sizeof(data);
127         if (get_string(val, NULL, data, &len) == NULL)
128                 exit(1);
129
130         set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
131 }
132
133 static void
134 set80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
135 {
136         int                     len;
137         u_int8_t                data[33];
138
139         bzero(data, sizeof(data));
140         len = sizeof(data);
141         get_string(val, NULL, data, &len);
142
143         set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
144 }
145
146 /*
147  * Convert IEEE channel number to MHz frequency.
148  */
149 static u_int
150 ieee80211_ieee2mhz(u_int chan)
151 {
152         if (chan == 14)
153                 return 2484;
154         if (chan < 14)                  /* 0-13 */
155                 return 2407 + chan*5;
156         if (chan < 27)                  /* 15-26 */
157                 return 2512 + ((chan-15)*20);
158         return 5000 + (chan*5);
159 }
160
161 /*
162  * Convert MHz frequency to IEEE channel number.
163  */
164 static u_int
165 ieee80211_mhz2ieee(u_int freq)
166 {
167         if (freq == 2484)
168                 return 14;
169         if (freq < 2484)
170                 return (freq - 2407) / 5;
171         if (freq < 5000)
172                 return 15 + ((freq - 2512) / 20);
173         return (freq - 5000) / 5;
174 }
175
176 static void
177 set80211channel(const char *val, int d, int s, const struct afswtch *rafp)
178 {
179         if (!isanyarg(val)) {
180                 int v = atoi(val);
181                 if (v > 255)            /* treat as frequency */
182                         v = ieee80211_mhz2ieee(v);
183                 set80211(s, IEEE80211_IOC_CHANNEL, v, 0, NULL);
184         } else
185                 set80211(s, IEEE80211_IOC_CHANNEL, IEEE80211_CHAN_ANY, 0, NULL);
186 }
187
188 static void
189 set80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
190 {
191         int     mode;
192
193         if (strcasecmp(val, "none") == 0) {
194                 mode = IEEE80211_AUTH_NONE;
195         } else if (strcasecmp(val, "open") == 0) {
196                 mode = IEEE80211_AUTH_OPEN;
197         } else if (strcasecmp(val, "shared") == 0) {
198                 mode = IEEE80211_AUTH_SHARED;
199         } else if (strcasecmp(val, "8021x") == 0) {
200                 mode = IEEE80211_AUTH_8021X;
201         } else if (strcasecmp(val, "wpa") == 0) {
202                 mode = IEEE80211_AUTH_WPA;
203         } else {
204                 errx(1, "unknown authmode");
205         }
206
207         set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
208 }
209
210 static void
211 set80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
212 {
213         int     mode;
214
215         if (strcasecmp(val, "off") == 0) {
216                 mode = IEEE80211_POWERSAVE_OFF;
217         } else if (strcasecmp(val, "on") == 0) {
218                 mode = IEEE80211_POWERSAVE_ON;
219         } else if (strcasecmp(val, "cam") == 0) {
220                 mode = IEEE80211_POWERSAVE_CAM;
221         } else if (strcasecmp(val, "psp") == 0) {
222                 mode = IEEE80211_POWERSAVE_PSP;
223         } else if (strcasecmp(val, "psp-cam") == 0) {
224                 mode = IEEE80211_POWERSAVE_PSP_CAM;
225         } else {
226                 errx(1, "unknown powersavemode");
227         }
228
229         set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
230 }
231
232 static void
233 set80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
234 {
235         if (d == 0)
236                 set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
237                     0, NULL);
238         else
239                 set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
240                     0, NULL);
241 }
242
243 static void
244 set80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
245 {
246         set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
247 }
248
249 static void
250 set80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
251 {
252         int     mode;
253
254         if (strcasecmp(val, "off") == 0) {
255                 mode = IEEE80211_WEP_OFF;
256         } else if (strcasecmp(val, "on") == 0) {
257                 mode = IEEE80211_WEP_ON;
258         } else if (strcasecmp(val, "mixed") == 0) {
259                 mode = IEEE80211_WEP_MIXED;
260         } else {
261                 errx(1, "unknown wep mode");
262         }
263
264         set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
265 }
266
267 static void
268 set80211wep(const char *val, int d, int s, const struct afswtch *rafp)
269 {
270         set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
271 }
272
273 static int
274 isundefarg(const char *arg)
275 {
276         return (strcmp(arg, "-") == 0 || strncasecmp(arg, "undef", 5) == 0);
277 }
278
279 static void
280 set80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
281 {
282         if (isundefarg(val))
283                 set80211(s, IEEE80211_IOC_WEPTXKEY, IEEE80211_KEYIX_NONE, 0, NULL);
284         else
285                 set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
286 }
287
288 static void
289 set80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
290 {
291         int             key = 0;
292         int             len;
293         u_int8_t        data[IEEE80211_KEYBUF_SIZE];
294
295         if (isdigit(val[0]) && val[1] == ':') {
296                 key = atoi(val)-1;
297                 val += 2;
298         }
299
300         bzero(data, sizeof(data));
301         len = sizeof(data);
302         get_string(val, NULL, data, &len);
303
304         set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
305 }
306
307 /*
308  * This function is purely a NetBSD compatability interface.  The NetBSD
309  * interface is too inflexible, but it's there so we'll support it since
310  * it's not all that hard.
311  */
312 static void
313 set80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
314 {
315         int             txkey;
316         int             i, len;
317         u_int8_t        data[IEEE80211_KEYBUF_SIZE];
318
319         set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
320
321         if (isdigit(val[0]) && val[1] == ':') {
322                 txkey = val[0]-'0'-1;
323                 val += 2;
324
325                 for (i = 0; i < 4; i++) {
326                         bzero(data, sizeof(data));
327                         len = sizeof(data);
328                         val = get_string(val, ",", data, &len);
329                         if (val == NULL)
330                                 exit(1);
331
332                         set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
333                 }
334         } else {
335                 bzero(data, sizeof(data));
336                 len = sizeof(data);
337                 get_string(val, NULL, data, &len);
338                 txkey = 0;
339
340                 set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
341
342                 bzero(data, sizeof(data));
343                 for (i = 1; i < 4; i++)
344                         set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
345         }
346
347         set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
348 }
349
350 static void
351 set80211rtsthreshold(const char *val, int d, int s, const struct afswtch *rafp)
352 {
353         set80211(s, IEEE80211_IOC_RTSTHRESHOLD,
354                 isundefarg(val) ? IEEE80211_RTS_MAX : atoi(val), 0, NULL);
355 }
356
357 static void
358 set80211protmode(const char *val, int d, int s, const struct afswtch *rafp)
359 {
360         int     mode;
361
362         if (strcasecmp(val, "off") == 0) {
363                 mode = IEEE80211_PROTMODE_OFF;
364         } else if (strcasecmp(val, "cts") == 0) {
365                 mode = IEEE80211_PROTMODE_CTS;
366         } else if (strcasecmp(val, "rtscts") == 0) {
367                 mode = IEEE80211_PROTMODE_RTSCTS;
368         } else {
369                 errx(1, "unknown protection mode");
370         }
371
372         set80211(s, IEEE80211_IOC_PROTMODE, mode, 0, NULL);
373 }
374
375 static void
376 set80211txpower(const char *val, int d, int s, const struct afswtch *rafp)
377 {
378         set80211(s, IEEE80211_IOC_TXPOWER, atoi(val), 0, NULL);
379 }
380
381 #define IEEE80211_ROAMING_DEVICE        0
382 #define IEEE80211_ROAMING_AUTO          1
383 #define IEEE80211_ROAMING_MANUAL        2
384
385 static void
386 set80211roaming(const char *val, int d, int s, const struct afswtch *rafp)
387 {
388         int mode;
389
390         if (strcasecmp(val, "device") == 0) {
391                 mode = IEEE80211_ROAMING_DEVICE;
392         } else if (strcasecmp(val, "auto") == 0) {
393                 mode = IEEE80211_ROAMING_AUTO;
394         } else if (strcasecmp(val, "manual") == 0) {
395                 mode = IEEE80211_ROAMING_MANUAL;
396         } else {
397                 errx(1, "unknown roaming mode");
398         }
399         set80211(s, IEEE80211_IOC_ROAMING, mode, 0, NULL);
400 }
401
402 static void
403 set80211wme(const char *val, int d, int s, const struct afswtch *rafp)
404 {
405         set80211(s, IEEE80211_IOC_WME, d, 0, NULL);
406 }
407
408 static void
409 set80211hidessid(const char *val, int d, int s, const struct afswtch *rafp)
410 {
411         set80211(s, IEEE80211_IOC_HIDESSID, d, 0, NULL);
412 }
413
414 static void
415 set80211apbridge(const char *val, int d, int s, const struct afswtch *rafp)
416 {
417         set80211(s, IEEE80211_IOC_APBRIDGE, d, 0, NULL);
418 }
419
420 static void
421 set80211chanlist(const char *val, int d, int s, const struct afswtch *rafp)
422 {
423         struct ieee80211req_chanlist chanlist;
424 #define MAXCHAN (sizeof(chanlist.ic_channels)*NBBY)
425         char *temp, *cp, *tp;
426
427         temp = malloc(strlen(val) + 1);
428         if (temp == NULL)
429                 errx(1, "malloc failed");
430         strcpy(temp, val);
431         memset(&chanlist, 0, sizeof(chanlist));
432         cp = temp;
433         for (;;) {
434                 int first, last, f;
435
436                 tp = strchr(cp, ',');
437                 if (tp != NULL)
438                         *tp++ = '\0';
439                 switch (sscanf(cp, "%u-%u", &first, &last)) {
440                 case 1:
441                         if (first > MAXCHAN)
442                                 errx(-1, "channel %u out of range, max %zu",
443                                         first, MAXCHAN);
444                         setbit(chanlist.ic_channels, first);
445                         break;
446                 case 2:
447                         if (first > MAXCHAN)
448                                 errx(-1, "channel %u out of range, max %zu",
449                                         first, MAXCHAN);
450                         if (last > MAXCHAN)
451                                 errx(-1, "channel %u out of range, max %zu",
452                                         last, MAXCHAN);
453                         if (first > last)
454                                 errx(-1, "void channel range, %u > %u",
455                                         first, last);
456                         for (f = first; f <= last; f++)
457                                 setbit(chanlist.ic_channels, f);
458                         break;
459                 }
460                 if (tp == NULL)
461                         break;
462                 while (isspace(*tp))
463                         tp++;
464                 if (!isdigit(*tp))
465                         break;
466                 cp = tp;
467         }
468         set80211(s, IEEE80211_IOC_CHANLIST, 0,
469                 sizeof(chanlist), (uint8_t *) &chanlist);
470 #undef MAXCHAN
471 }
472
473 static void
474 set80211bssid(const char *val, int d, int s, const struct afswtch *rafp)
475 {
476
477         if (!isanyarg(val)) {
478                 char *temp;
479                 struct sockaddr_dl sdl;
480
481                 temp = malloc(strlen(val) + 2); /* ':' and '\0' */
482                 if (temp == NULL)
483                         errx(1, "malloc failed");
484                 temp[0] = ':';
485                 strcpy(temp + 1, val);
486                 sdl.sdl_len = sizeof(sdl);
487                 link_addr(temp, &sdl);
488                 free(temp);
489                 if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
490                         errx(1, "malformed link-level address");
491                 set80211(s, IEEE80211_IOC_BSSID, 0,
492                         IEEE80211_ADDR_LEN, LLADDR(&sdl));
493         } else {
494                 uint8_t zerobssid[IEEE80211_ADDR_LEN];
495                 memset(zerobssid, 0, sizeof(zerobssid));
496                 set80211(s, IEEE80211_IOC_BSSID, 0,
497                         IEEE80211_ADDR_LEN, zerobssid);
498         }
499 }
500
501 static int
502 getac(const char *ac)
503 {
504         if (strcasecmp(ac, "ac_be") == 0 || strcasecmp(ac, "be") == 0)
505                 return WME_AC_BE;
506         if (strcasecmp(ac, "ac_bk") == 0 || strcasecmp(ac, "bk") == 0)
507                 return WME_AC_BK;
508         if (strcasecmp(ac, "ac_vi") == 0 || strcasecmp(ac, "vi") == 0)
509                 return WME_AC_VI;
510         if (strcasecmp(ac, "ac_vo") == 0 || strcasecmp(ac, "vo") == 0)
511                 return WME_AC_VO;
512         errx(1, "unknown wme access class %s", ac);
513 }
514
515 static
516 DECL_CMD_FUNC2(set80211cwmin, ac, val)
517 {
518         set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val), getac(ac), NULL);
519 }
520
521 static
522 DECL_CMD_FUNC2(set80211cwmax, ac, val)
523 {
524         set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val), getac(ac), NULL);
525 }
526
527 static
528 DECL_CMD_FUNC2(set80211aifs, ac, val)
529 {
530         set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val), getac(ac), NULL);
531 }
532
533 static
534 DECL_CMD_FUNC2(set80211txoplimit, ac, val)
535 {
536         set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), getac(ac), NULL);
537 }
538
539 static
540 DECL_CMD_FUNC(set80211acm, ac, d)
541 {
542         set80211(s, IEEE80211_IOC_WME_ACM, 1, getac(ac), NULL);
543 }
544 static
545 DECL_CMD_FUNC(set80211noacm, ac, d)
546 {
547         set80211(s, IEEE80211_IOC_WME_ACM, 0, getac(ac), NULL);
548 }
549
550 static
551 DECL_CMD_FUNC(set80211ackpolicy, ac, d)
552 {
553         set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 1, getac(ac), NULL);
554 }
555 static
556 DECL_CMD_FUNC(set80211noackpolicy, ac, d)
557 {
558         set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 0, getac(ac), NULL);
559 }
560
561 static
562 DECL_CMD_FUNC2(set80211bsscwmin, ac, val)
563 {
564         set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val),
565                 getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
566 }
567
568 static
569 DECL_CMD_FUNC2(set80211bsscwmax, ac, val)
570 {
571         set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val),
572                 getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
573 }
574
575 static
576 DECL_CMD_FUNC2(set80211bssaifs, ac, val)
577 {
578         set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val),
579                 getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
580 }
581
582 static
583 DECL_CMD_FUNC2(set80211bsstxoplimit, ac, val)
584 {
585         set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val),
586                 getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
587 }
588
589 static
590 DECL_CMD_FUNC(set80211dtimperiod, val, d)
591 {
592         set80211(s, IEEE80211_IOC_DTIM_PERIOD, atoi(val), 0, NULL);
593 }
594
595 static
596 DECL_CMD_FUNC(set80211bintval, val, d)
597 {
598         set80211(s, IEEE80211_IOC_BEACON_INTERVAL, atoi(val), 0, NULL);
599 }
600
601 static void
602 set80211macmac(int s, int op, const char *val)
603 {
604         char *temp;
605         struct sockaddr_dl sdl;
606
607         temp = malloc(strlen(val) + 2); /* ':' and '\0' */
608         if (temp == NULL)
609                 errx(1, "malloc failed");
610         temp[0] = ':';
611         strcpy(temp + 1, val);
612         sdl.sdl_len = sizeof(sdl);
613         link_addr(temp, &sdl);
614         free(temp);
615         if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
616                 errx(1, "malformed link-level address");
617         set80211(s, op, 0, IEEE80211_ADDR_LEN, LLADDR(&sdl));
618 }
619
620 static
621 DECL_CMD_FUNC(set80211addmac, val, d)
622 {
623         set80211macmac(s, IEEE80211_IOC_ADDMAC, val);
624 }
625
626 static
627 DECL_CMD_FUNC(set80211delmac, val, d)
628 {
629         set80211macmac(s, IEEE80211_IOC_DELMAC, val);
630 }
631
632 static
633 DECL_CMD_FUNC(set80211kickmac, val, d)
634 {
635         char *temp;
636         struct sockaddr_dl sdl;
637         struct ieee80211req_mlme mlme;
638
639         temp = malloc(strlen(val) + 2); /* ':' and '\0' */
640         if (temp == NULL)
641                 errx(1, "malloc failed");
642         temp[0] = ':';
643         strcpy(temp + 1, val);
644         sdl.sdl_len = sizeof(sdl);
645         link_addr(temp, &sdl);
646         free(temp);
647         if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
648                 errx(1, "malformed link-level address");
649         memset(&mlme, 0, sizeof(mlme));
650         mlme.im_op = IEEE80211_MLME_DEAUTH;
651         mlme.im_reason = IEEE80211_REASON_AUTH_EXPIRE;
652         memcpy(mlme.im_macaddr, LLADDR(&sdl), IEEE80211_ADDR_LEN);
653         set80211(s, IEEE80211_IOC_MLME, 0, sizeof(mlme), (u_int8_t *) &mlme);
654 }
655
656 static
657 DECL_CMD_FUNC(set80211maccmd, val, d)
658 {
659         set80211(s, IEEE80211_IOC_MACCMD, d, 0, NULL);
660 }
661
662 static void
663 set80211pureg(const char *val, int d, int s, const struct afswtch *rafp)
664 {
665         set80211(s, IEEE80211_IOC_PUREG, d, 0, NULL);
666 }
667
668 static void
669 set80211burst(const char *val, int d, int s, const struct afswtch *rafp)
670 {
671         set80211(s, IEEE80211_IOC_BURST, d, 0, NULL);
672 }
673
674 static void
675 set80211ratectl(const char *val, int d, int s, const struct afswtch *rafp)
676 {
677         int ratectl = 0;
678
679         if (strcmp("onoe", val) == 0)
680                 ratectl = IEEE80211_RATECTL_ONOE;
681         else if (strcmp("amrr", val) == 0)
682                 ratectl = IEEE80211_RATECTL_AMRR;
683         else
684                 errx(1, "unknown ratectl");
685
686         set80211(s, IEEE80211_IOC_RATECTL, ratectl, 0, NULL);
687 }
688
689 static
690 DECL_CMD_FUNC(set80211mcastrate, val, d)
691 {
692         set80211(s, IEEE80211_IOC_MCAST_RATE, (int) 2*atof(val), 0, NULL);
693 }
694
695 static
696 DECL_CMD_FUNC(set80211fragthreshold, val, d)
697 {
698         set80211(s, IEEE80211_IOC_FRAGTHRESHOLD,
699                 isundefarg(val) ? IEEE80211_FRAG_MAX : atoi(val), 0, NULL);
700 }
701
702 static int
703 getmaxrate(uint8_t rates[15], uint8_t nrates)
704 {
705         int i, maxrate = -1;
706
707         for (i = 0; i < nrates; i++) {
708                 int rate = rates[i] & IEEE80211_RATE_VAL;
709                 if (rate > maxrate)
710                         maxrate = rate;
711         }
712         return maxrate / 2;
713 }
714
715 static const char *
716 getcaps(int capinfo)
717 {
718         static char capstring[32];
719         char *cp = capstring;
720
721         if (capinfo & IEEE80211_CAPINFO_ESS)
722                 *cp++ = 'E';
723         if (capinfo & IEEE80211_CAPINFO_IBSS)
724                 *cp++ = 'I';
725         if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
726                 *cp++ = 'c';
727         if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
728                 *cp++ = 'C';
729         if (capinfo & IEEE80211_CAPINFO_PRIVACY)
730                 *cp++ = 'P';
731         if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
732                 *cp++ = 'S';
733         if (capinfo & IEEE80211_CAPINFO_PBCC)
734                 *cp++ = 'B';
735         if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
736                 *cp++ = 'A';
737         if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
738                 *cp++ = 's';
739         if (capinfo & IEEE80211_CAPINFO_RSN)
740                 *cp++ = 'R';
741         if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
742                 *cp++ = 'D';
743         *cp = '\0';
744         return capstring;
745 }
746
747 static void
748 printie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
749 {
750         printf("%s", tag);
751         if (verbose) {
752                 maxlen -= strlen(tag)+2;
753                 if (2*ielen > maxlen)
754                         maxlen--;
755                 printf("<");
756                 for (; ielen > 0; ie++, ielen--) {
757                         if (maxlen-- <= 0)
758                                 break;
759                         printf("%02x", *ie);
760                 }
761                 if (ielen != 0)
762                         printf("-");
763                 printf(">");
764         }
765 }
766
767 /*
768  * Copy the ssid string contents into buf, truncating to fit.  If the
769  * ssid is entirely printable then just copy intact.  Otherwise convert
770  * to hexadecimal.  If the result is truncated then replace the last
771  * three characters with "...".
772  */
773 static int
774 copy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
775 {
776         const u_int8_t *p; 
777         size_t maxlen;
778         int i;
779
780         if (essid_len > bufsize)
781                 maxlen = bufsize;
782         else
783                 maxlen = essid_len;
784         /* determine printable or not */
785         for (i = 0, p = essid; i < maxlen; i++, p++) {
786                 if (*p < ' ' || *p > 0x7e)
787                         break;
788         }
789         if (i != maxlen) {              /* not printable, print as hex */
790                 if (bufsize < 3)
791                         return 0;
792                 strlcpy(buf, "0x", bufsize);
793                 bufsize -= 2;
794                 p = essid;
795                 for (i = 0; i < maxlen && bufsize >= 2; i++) {
796                         sprintf(&buf[2+2*i], "%02x", p[i]);
797                         bufsize -= 2;
798                 }
799                 if (i != essid_len)
800                         memcpy(&buf[2+2*i-3], "...", 3);
801         } else {                        /* printable, truncate as needed */
802                 memcpy(buf, essid, maxlen);
803                 if (maxlen != essid_len)
804                         memcpy(&buf[maxlen-3], "...", 3);
805         }
806         return maxlen;
807 }
808
809 /* unaligned little endian access */     
810 #define LE_READ_4(p)                                    \
811         ((u_int32_t)                                    \
812          ((((const u_int8_t *)(p))[0]      ) |          \
813           (((const u_int8_t *)(p))[1] <<  8) |          \
814           (((const u_int8_t *)(p))[2] << 16) |          \
815           (((const u_int8_t *)(p))[3] << 24)))
816
817 static int __inline
818 iswpaoui(const u_int8_t *frm)
819 {
820         return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
821 }
822
823 static int __inline
824 iswmeoui(const u_int8_t *frm)
825 {
826         return frm[1] > 3 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI);
827 }
828
829 static int __inline
830 isatherosoui(const u_int8_t *frm)
831 {
832         return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
833 }
834
835 static void
836 printies(const u_int8_t *vp, int ielen, int maxcols)
837 {
838         while (ielen > 0) {
839                 switch (vp[0]) {
840                 case IEEE80211_ELEMID_VENDOR:
841                         if (iswpaoui(vp))
842                                 printie(" WPA", vp, 2+vp[1], maxcols);
843                         else if (iswmeoui(vp))
844                                 printie(" WME", vp, 2+vp[1], maxcols);
845                         else if (isatherosoui(vp))
846                                 printie(" ATH", vp, 2+vp[1], maxcols);
847                         else
848                                 printie(" VEN", vp, 2+vp[1], maxcols);
849                         break;
850                 case IEEE80211_ELEMID_RSN:
851                         printie(" RSN", vp, 2+vp[1], maxcols);
852                         break;
853                 default:
854                         printie(" ???", vp, 2+vp[1], maxcols);
855                         break;
856                 }
857                 ielen -= 2+vp[1];
858                 vp += 2+vp[1];
859         }
860 }
861
862 static void
863 list_scan(int s)
864 {
865         uint8_t buf[24*1024];
866         struct ieee80211req ireq;
867         char ssid[IEEE80211_NWID_LEN+1];
868         uint8_t *cp;
869         int len, ssidmax;
870
871         (void) memset(&ireq, 0, sizeof(ireq));
872         (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
873         ireq.i_type = IEEE80211_IOC_SCAN_RESULTS;
874         ireq.i_data = buf;
875         ireq.i_len = sizeof(buf);
876         if (ioctl(s, SIOCG80211, &ireq) < 0)
877                 errx(1, "unable to get scan results");
878         len = ireq.i_len;
879         if (len < sizeof(struct ieee80211req_scan_result))
880                 return;
881
882         ssidmax = verbose ? IEEE80211_NWID_LEN : 14;
883         printf("%-*.*s  %-17.17s  %4s %4s  %-5s %3s %4s\n"
884                 , ssidmax, ssidmax, "SSID"
885                 , "BSSID"
886                 , "CHAN"
887                 , "RATE"
888                 , "S:N"
889                 , "INT"
890                 , "CAPS"
891         );
892         cp = buf;
893         do {
894                 struct ieee80211req_scan_result *sr;
895                 uint8_t *vp;
896
897                 sr = (struct ieee80211req_scan_result *) cp;
898                 vp = (u_int8_t *)(sr+1);
899                 printf("%-*.*s  %s  %3d  %3dM %2d:%-2d  %3d %-4.4s"
900                         , ssidmax
901                           , copy_essid(ssid, ssidmax, vp, sr->isr_ssid_len)
902                           , ssid
903                         , ether_ntoa((const struct ether_addr *) sr->isr_bssid)
904                         , ieee80211_mhz2ieee(sr->isr_freq)
905                         , getmaxrate(sr->isr_rates, sr->isr_nrates)
906                         , sr->isr_rssi, sr->isr_noise
907                         , sr->isr_intval
908                         , getcaps(sr->isr_capinfo)
909                 );
910                 printies(vp + sr->isr_ssid_len, sr->isr_ie_len, 24);
911                 printf("\n");
912                 cp += sr->isr_len, len -= sr->isr_len;
913         } while (len >= sizeof(struct ieee80211req_scan_result));
914 }
915
916 #include <netproto/802_11/ieee80211_dragonfly.h>
917
918 static void
919 scan_and_wait(int s)
920 {
921         struct ieee80211req ireq;
922         int sroute;
923
924         sroute = socket(PF_ROUTE, SOCK_RAW, 0);
925         if (sroute < 0) {
926                 perror("socket(PF_ROUTE,SOCK_RAW)");
927                 return;
928         }
929         (void) memset(&ireq, 0, sizeof(ireq));
930         (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
931         ireq.i_type = IEEE80211_IOC_SCAN_REQ;
932         /* NB: only root can trigger a scan so ignore errors */
933         if (ioctl(s, SIOCS80211, &ireq) >= 0) {
934                 char buf[2048];
935                 struct if_announcemsghdr *ifan;
936                 struct rt_msghdr *rtm;
937
938                 do {
939                         if (read(sroute, buf, sizeof(buf)) < 0) {
940                                 perror("read(PF_ROUTE)");
941                                 break;
942                         }
943                         rtm = (struct rt_msghdr *) buf;
944                         if (rtm->rtm_version != RTM_VERSION)
945                                 break;
946                         ifan = (struct if_announcemsghdr *) rtm;
947                 } while (rtm->rtm_type != RTM_IEEE80211 ||
948                     ifan->ifan_what != RTM_IEEE80211_SCAN);
949         }
950         close(sroute);
951 }
952
953 static
954 DECL_CMD_FUNC(set80211scan, val, d)
955 {
956         scan_and_wait(s);
957         list_scan(s);
958 }
959
960 static enum ieee80211_opmode get80211opmode(int s);
961
962 static void
963 list_stations(int s)
964 {
965         union {
966                 struct ieee80211req_sta_req req;
967                 uint8_t buf[24*1024];
968         } u;
969         enum ieee80211_opmode opmode = get80211opmode(s);
970         struct ieee80211req ireq;
971         uint8_t *cp;
972         int len;
973
974         (void) memset(&ireq, 0, sizeof(ireq));
975         (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
976         /* broadcast address =>'s get all stations */
977         (void) memset(u.req.is_u.macaddr, 0xff, IEEE80211_ADDR_LEN);
978         if (opmode == IEEE80211_M_STA) {
979                 /*
980                  * Get information about the associated AP.
981                  */
982                 ireq.i_type = IEEE80211_IOC_BSSID;
983                 ireq.i_data = u.req.is_u.macaddr;
984                 ireq.i_len = IEEE80211_ADDR_LEN;
985                 (void) ioctl(s, SIOCG80211, &ireq);
986         }
987         ireq.i_type = IEEE80211_IOC_STA_INFO;
988         ireq.i_data = &u;
989         ireq.i_len = sizeof(u);
990         if (ioctl(s, SIOCG80211, &ireq) < 0)
991                 errx(1, "unable to get station information");
992         len = ireq.i_len;
993         if (len < sizeof(struct ieee80211req_sta_info))
994                 return;
995
996         printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %3s\n"
997                 , "ADDR"
998                 , "AID"
999                 , "CHAN"
1000                 , "RATE"
1001                 , "RSSI"
1002                 , "IDLE"
1003                 , "TXSEQ"
1004                 , "RXSEQ"
1005                 , "CAPS"
1006                 , "ERP"
1007         );
1008         cp = (uint8_t *) u.req.info;
1009         do {
1010                 struct ieee80211req_sta_info *si;
1011                 uint8_t *vp;
1012
1013                 si = (struct ieee80211req_sta_info *) cp;
1014                 if (si->isi_len < sizeof(*si))
1015                         break;
1016                 vp = (u_int8_t *)(si+1);
1017                 printf("%s %4u %4d %3dM %4d %4d %6d %6d %-4.4s %3x"
1018                         , ether_ntoa((const struct ether_addr*) si->isi_macaddr)
1019                         , IEEE80211_AID(si->isi_associd)
1020                         , ieee80211_mhz2ieee(si->isi_freq)
1021                         , (si->isi_rates[si->isi_txrate] & IEEE80211_RATE_VAL)/2
1022                         , si->isi_rssi
1023                         , si->isi_inact
1024                         , si->isi_txseqs[0]
1025                         , si->isi_rxseqs[0]
1026                         , getcaps(si->isi_capinfo)
1027                         , si->isi_erp
1028                 );
1029                 printies(vp, si->isi_ie_len, 24);
1030                 printf("\n");
1031                 cp += si->isi_len, len -= si->isi_len;
1032         } while (len >= sizeof(struct ieee80211req_sta_info));
1033 }
1034
1035 static void
1036 print_chaninfo(const struct ieee80211_channel *c)
1037 {
1038 #define IEEE80211_IS_CHAN_PASSIVE(_c) \
1039         (((_c)->ic_flags & IEEE80211_CHAN_PASSIVE))
1040         char buf[14];
1041
1042         buf[0] = '\0';
1043         if (IEEE80211_IS_CHAN_FHSS(c))
1044                 strlcat(buf, " FHSS", sizeof(buf));
1045         if (IEEE80211_IS_CHAN_A(c))
1046                 strlcat(buf, " 11a", sizeof(buf));
1047         /* XXX 11g schizophrenia */
1048         if (IEEE80211_IS_CHAN_G(c) ||
1049             IEEE80211_IS_CHAN_PUREG(c))
1050                 strlcat(buf, " 11g", sizeof(buf));
1051         else if (IEEE80211_IS_CHAN_B(c))
1052                 strlcat(buf, " 11b", sizeof(buf));
1053         if (IEEE80211_IS_CHAN_T(c))
1054                 strlcat(buf, " Turbo", sizeof(buf));
1055         printf("Channel %3u : %u%c Mhz%-14.14s",
1056                 ieee80211_mhz2ieee(c->ic_freq), c->ic_freq,
1057                 IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ', buf);
1058 #undef IEEE80211_IS_CHAN_PASSIVE
1059 }
1060
1061 static void
1062 list_channels(int s, int allchans)
1063 {
1064         struct ieee80211req ireq;
1065         struct ieee80211req_chaninfo chans;
1066         struct ieee80211req_chaninfo achans;
1067         const struct ieee80211_channel *c;
1068         int i, half;
1069
1070         (void) memset(&ireq, 0, sizeof(ireq));
1071         (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1072         ireq.i_type = IEEE80211_IOC_CHANINFO;
1073         ireq.i_data = &chans;
1074         ireq.i_len = sizeof(chans);
1075         if (ioctl(s, SIOCG80211, &ireq) < 0)
1076                 errx(1, "unable to get channel information");
1077         if (!allchans) {
1078                 struct ieee80211req_chanlist active;
1079
1080                 ireq.i_type = IEEE80211_IOC_CHANLIST;
1081                 ireq.i_data = &active;
1082                 ireq.i_len = sizeof(active);
1083                 if (ioctl(s, SIOCG80211, &ireq) < 0)
1084                         errx(1, "unable to get active channel list");
1085                 memset(&achans, 0, sizeof(achans));
1086                 for (i = 0; i < chans.ic_nchans; i++) {
1087                         c = &chans.ic_chans[i];
1088                         if (isset(active.ic_channels, ieee80211_mhz2ieee(c->ic_freq)) || allchans)
1089                                 achans.ic_chans[achans.ic_nchans++] = *c;
1090                 }
1091         } else
1092                 achans = chans;
1093         half = achans.ic_nchans / 2;
1094         if (achans.ic_nchans % 2)
1095                 half++;
1096         for (i = 0; i < achans.ic_nchans / 2; i++) {
1097                 print_chaninfo(&achans.ic_chans[i]);
1098                 print_chaninfo(&achans.ic_chans[half+i]);
1099                 printf("\n");
1100         }
1101         if (achans.ic_nchans % 2) {
1102                 print_chaninfo(&achans.ic_chans[i]);
1103                 printf("\n");
1104         }
1105 }
1106
1107 static void
1108 list_keys(int s)
1109 {
1110 }
1111
1112 #define IEEE80211_C_BITS \
1113 "\020\1WEP\2TKIP\3AES\4AES_CCM\6CKIP\11IBSS\12PMGT\13HOSTAP\14AHDEMO" \
1114 "\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE\21MONITOR\22TKIPMIC\30WPA1" \
1115 "\31WPA2\32BURST\33WME"
1116
1117 static void
1118 list_capabilities(int s)
1119 {
1120         struct ieee80211req ireq;
1121         u_int32_t caps;
1122
1123         (void) memset(&ireq, 0, sizeof(ireq));
1124         (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1125         ireq.i_type = IEEE80211_IOC_DRIVER_CAPS;
1126         if (ioctl(s, SIOCG80211, &ireq) < 0)
1127                 errx(1, "unable to get driver capabilities");
1128         caps = (((u_int16_t) ireq.i_val) << 16) | ((u_int16_t) ireq.i_len);
1129         printb(name, caps, IEEE80211_C_BITS);
1130         putchar('\n');
1131 }
1132
1133 static void
1134 list_wme(int s)
1135 {
1136         static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
1137         struct ieee80211req ireq;
1138         int ac;
1139
1140         (void) memset(&ireq, 0, sizeof(ireq));
1141         (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1142         ireq.i_len = 0;
1143         for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
1144 again:
1145                 if (ireq.i_len & IEEE80211_WMEPARAM_BSS)
1146                         printf("\t%s", "     ");
1147                 else
1148                         printf("\t%s", acnames[ac]);
1149
1150                 ireq.i_len = (ireq.i_len & IEEE80211_WMEPARAM_BSS) | ac;
1151
1152                 /* show WME BSS parameters */
1153                 ireq.i_type = IEEE80211_IOC_WME_CWMIN;
1154                 if (ioctl(s, SIOCG80211, &ireq) != -1)
1155                         printf(" cwmin %2u", ireq.i_val);
1156                 ireq.i_type = IEEE80211_IOC_WME_CWMAX;
1157                 if (ioctl(s, SIOCG80211, &ireq) != -1)
1158                         printf(" cwmax %2u", ireq.i_val);
1159                 ireq.i_type = IEEE80211_IOC_WME_AIFS;
1160                 if (ioctl(s, SIOCG80211, &ireq) != -1)
1161                         printf(" aifs %2u", ireq.i_val);
1162                 ireq.i_type = IEEE80211_IOC_WME_TXOPLIMIT;
1163                 if (ioctl(s, SIOCG80211, &ireq) != -1)
1164                         printf(" txopLimit %3u", ireq.i_val);
1165                 ireq.i_type = IEEE80211_IOC_WME_ACM;
1166                 if (ioctl(s, SIOCG80211, &ireq) != -1) {
1167                         if (ireq.i_val)
1168                                 printf(" acm");
1169                         else if (verbose)
1170                                 printf(" -acm");
1171                 }
1172                 /* !BSS only */
1173                 if ((ireq.i_len & IEEE80211_WMEPARAM_BSS) == 0) {
1174                         ireq.i_type = IEEE80211_IOC_WME_ACKPOLICY;
1175                         if (ioctl(s, SIOCG80211, &ireq) != -1) {
1176                                 if (!ireq.i_val)
1177                                         printf(" -ack");
1178                                 else if (verbose)
1179                                         printf(" ack");
1180                         }
1181                 }
1182                 printf("\n");
1183                 if ((ireq.i_len & IEEE80211_WMEPARAM_BSS) == 0) {
1184                         ireq.i_len |= IEEE80211_WMEPARAM_BSS;
1185                         goto again;
1186                 } else
1187                         ireq.i_len &= ~IEEE80211_WMEPARAM_BSS;
1188         }
1189 }
1190
1191 static void
1192 list_mac(int s)
1193 {
1194         struct ieee80211req ireq;
1195         struct ieee80211req_maclist *acllist;
1196         int i, nacls, policy;
1197         char c;
1198
1199         (void) memset(&ireq, 0, sizeof(ireq));
1200         (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
1201         ireq.i_type = IEEE80211_IOC_MACCMD;
1202         ireq.i_val = IEEE80211_MACCMD_POLICY;
1203         if (ioctl(s, SIOCG80211, &ireq) < 0) {
1204                 if (errno == EINVAL) {
1205                         printf("No acl policy loaded\n");
1206                         return;
1207                 }
1208                 err(1, "unable to get mac policy");
1209         }
1210         policy = ireq.i_val;
1211
1212         ireq.i_val = IEEE80211_MACCMD_LIST;
1213         ireq.i_len = 0;
1214         if (ioctl(s, SIOCG80211, &ireq) < 0)
1215                 err(1, "unable to get mac acl list size");
1216         if (ireq.i_len == 0)            /* NB: no acls */
1217                 return;
1218
1219         ireq.i_data = malloc(ireq.i_len);
1220         if (ireq.i_data == NULL)
1221                 err(1, "out of memory for acl list");
1222
1223         if (ioctl(s, SIOCG80211, &ireq) < 0)
1224                 err(1, "unable to get mac acl list");
1225         if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
1226                 if (verbose)
1227                         printf("policy: open\n");
1228                 c = '*';
1229         } else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
1230                 if (verbose)
1231                         printf("policy: allow\n");
1232                 c = '+';
1233         } else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
1234                 if (verbose)
1235                         printf("policy: deny\n");
1236                 c = '-';
1237         } else {
1238                 printf("policy: unknown (%u)\n", policy);
1239                 c = '?';
1240         }
1241         nacls = ireq.i_len / sizeof(*acllist);
1242         acllist = (struct ieee80211req_maclist *) ireq.i_data;
1243         for (i = 0; i < nacls; i++)
1244                 printf("%c%s\n", c, ether_ntoa(
1245                         (const struct ether_addr *) acllist[i].ml_macaddr));
1246 }
1247
1248 static
1249 DECL_CMD_FUNC(set80211list, arg, d)
1250 {
1251 #define iseq(a,b)       (strncasecmp(a,b,sizeof(b)-1) == 0)
1252
1253         if (iseq(arg, "sta"))
1254                 list_stations(s);
1255         else if (iseq(arg, "scan") || iseq(arg, "ap"))
1256                 list_scan(s);
1257         else if (iseq(arg, "chan") || iseq(arg, "freq"))
1258                 list_channels(s, 1);
1259         else if (iseq(arg, "active"))
1260                 list_channels(s, 0);
1261         else if (iseq(arg, "keys"))
1262                 list_keys(s);
1263         else if (iseq(arg, "caps"))
1264                 list_capabilities(s);
1265         else if (iseq(arg, "wme"))
1266                 list_wme(s);
1267         else if (iseq(arg, "mac"))
1268                 list_mac(s);
1269         else
1270                 errx(1, "Don't know how to list %s for %s", arg, name);
1271 #undef iseq
1272 }
1273
1274 static enum ieee80211_opmode
1275 get80211opmode(int s)
1276 {
1277         struct ifmediareq ifmr;
1278
1279         (void) memset(&ifmr, 0, sizeof(ifmr));
1280         (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1281
1282         if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
1283                 if (ifmr.ifm_current & IFM_IEEE80211_ADHOC)
1284                         return IEEE80211_M_IBSS;        /* XXX ahdemo */
1285                 if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
1286                         return IEEE80211_M_HOSTAP;
1287                 if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
1288                         return IEEE80211_M_MONITOR;
1289         }
1290         return IEEE80211_M_STA;
1291 }
1292
1293 static const struct ieee80211_channel *
1294 getchaninfo(int s, int chan)
1295 {
1296         struct ieee80211req ireq;
1297         static struct ieee80211req_chaninfo chans;
1298         static struct ieee80211_channel undef;
1299         const struct ieee80211_channel *c;
1300         int i, freq;
1301
1302         (void) memset(&ireq, 0, sizeof(ireq));
1303         (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1304         ireq.i_type = IEEE80211_IOC_CHANINFO;
1305         ireq.i_data = &chans;
1306         ireq.i_len = sizeof(chans);
1307         if (ioctl(s, SIOCG80211, &ireq) < 0)
1308                 errx(1, "unable to get channel information");
1309         freq = ieee80211_ieee2mhz(chan);
1310         for (i = 0; i < chans.ic_nchans; i++) {
1311                 c = &chans.ic_chans[i];
1312                 if (c->ic_freq == freq)
1313                         return c;
1314         }
1315         return &undef;
1316 }
1317
1318 #if 0
1319 static void
1320 printcipher(int s, struct ieee80211req *ireq, int keylenop)
1321 {
1322         switch (ireq->i_val) {
1323         case IEEE80211_CIPHER_WEP:
1324                 ireq->i_type = keylenop;
1325                 if (ioctl(s, SIOCG80211, ireq) != -1)
1326                         printf("WEP-%s", 
1327                             ireq->i_len <= 5 ? "40" :
1328                             ireq->i_len <= 13 ? "104" : "128");
1329                 else
1330                         printf("WEP");
1331                 break;
1332         case IEEE80211_CIPHER_TKIP:
1333                 printf("TKIP");
1334                 break;
1335         case IEEE80211_CIPHER_AES_OCB:
1336                 printf("AES-OCB");
1337                 break;
1338         case IEEE80211_CIPHER_AES_CCM:
1339                 printf("AES-CCM");
1340                 break;
1341         case IEEE80211_CIPHER_CKIP:
1342                 printf("CKIP");
1343                 break;
1344         case IEEE80211_CIPHER_NONE:
1345                 printf("NONE");
1346                 break;
1347         default:
1348                 printf("UNKNOWN (0x%x)", ireq->i_val);
1349                 break;
1350         }
1351 }
1352 #endif
1353
1354 #define MAXCOL  78
1355 static  int col;
1356 static  char spacer;
1357
1358 static void
1359 LINE_BREAK(void)
1360 {
1361         if (spacer != '\t') {
1362                 printf("\n");
1363                 spacer = '\t';
1364         }
1365         col = 8;        /* 8-col tab */
1366 }
1367
1368 static void
1369 LINE_CHECK(const char *fmt, ...)
1370 {
1371         char buf[80];
1372         va_list ap;
1373         int n;
1374
1375         va_start(ap, fmt);
1376         n = vsnprintf(buf+1, sizeof(buf)-1, fmt, ap);
1377         va_end(ap);
1378         col += 1+n;
1379         if (col > MAXCOL) {
1380                 LINE_BREAK();
1381                 col += n;
1382         }
1383         buf[0] = spacer;
1384         printf("%s", buf);
1385         spacer = ' ';
1386 }
1387
1388 static void
1389 printkey(const struct ieee80211req_key *ik)
1390 {
1391         static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
1392         int keylen = ik->ik_keylen;
1393         int printcontents;
1394
1395         printcontents = printkeys &&
1396                 (memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
1397         if (printcontents)
1398                 LINE_BREAK();
1399         switch (ik->ik_type) {
1400         case IEEE80211_CIPHER_WEP:
1401                 /* compatibility */
1402                 LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
1403                     keylen <= 5 ? "40-bit" :
1404                     keylen <= 13 ? "104-bit" : "128-bit");
1405                 break;
1406         case IEEE80211_CIPHER_TKIP:
1407                 if (keylen > 128/8)
1408                         keylen -= 128/8;        /* ignore MIC for now */
1409                 LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
1410                 break;
1411         case IEEE80211_CIPHER_AES_OCB:
1412                 LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
1413                 break;
1414         case IEEE80211_CIPHER_AES_CCM:
1415                 LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
1416                 break;
1417         case IEEE80211_CIPHER_CKIP:
1418                 LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
1419                 break;
1420         case IEEE80211_CIPHER_NONE:
1421                 LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
1422                 break;
1423         default:
1424                 LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
1425                         ik->ik_type, ik->ik_keyix+1, 8*keylen);
1426                 break;
1427         }
1428         if (printcontents) {
1429                 int i;
1430
1431                 printf(" <");
1432                 for (i = 0; i < keylen; i++)
1433                         printf("%02x", ik->ik_keydata[i]);
1434                 printf(">");
1435                 if (ik->ik_type != IEEE80211_CIPHER_WEP &&
1436                     (ik->ik_keyrsc != 0 || verbose))
1437                         printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
1438                 if (ik->ik_type != IEEE80211_CIPHER_WEP &&
1439                     (ik->ik_keytsc != 0 || verbose))
1440                         printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
1441                 if (ik->ik_flags != 0 && verbose) {
1442                         const char *sep = " ";
1443
1444                         if (ik->ik_flags & IEEE80211_KEY_XMIT)
1445                                 printf("%stx", sep), sep = "+";
1446                         if (ik->ik_flags & IEEE80211_KEY_RECV)
1447                                 printf("%srx", sep), sep = "+";
1448                         if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
1449                                 printf("%sdef", sep), sep = "+";
1450                 }
1451                 LINE_BREAK();
1452         }
1453 }
1454
1455 static void
1456 ieee80211_status(int s)
1457 {
1458         static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
1459         enum ieee80211_opmode opmode = get80211opmode(s);
1460         int i, num, wpa, wme;
1461         struct ieee80211req ireq;
1462         u_int8_t data[32];
1463         const struct ieee80211_channel *c;
1464
1465         (void) memset(&ireq, 0, sizeof(ireq));
1466         (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1467         ireq.i_data = &data;
1468
1469         wpa = 0;                /* unknown/not set */
1470
1471         ireq.i_type = IEEE80211_IOC_SSID;
1472         ireq.i_val = -1;
1473         if (ioctl(s, SIOCG80211, &ireq) < 0) {
1474                 /* If we can't get the SSID, this isn't an 802.11 device. */
1475                 return;
1476         }
1477
1478         ireq.i_type = IEEE80211_IOC_RATECTL;
1479         if (ioctl(s, SIOCG80211, &ireq) != -1) {
1480                 int lineb = 1;
1481
1482                 switch (ireq.i_val) {
1483                 case IEEE80211_RATECTL_ONOE:
1484                         printf("\tratectl: onoe");
1485                         break;
1486                 case IEEE80211_RATECTL_AMRR:
1487                         printf("\tratectl: amrr");
1488                         break;
1489                 default:
1490                         if (verbose)
1491                                 printf("\tratectl: none");
1492                         else
1493                                 lineb = 0;
1494                         break;
1495                 }
1496                 if (lineb)
1497                         LINE_BREAK();
1498         }
1499
1500         num = 0;
1501         ireq.i_type = IEEE80211_IOC_NUMSSIDS;
1502         if (ioctl(s, SIOCG80211, &ireq) >= 0)
1503                 num = ireq.i_val;
1504         printf("\tssid ");
1505         if (num > 1) {
1506                 ireq.i_type = IEEE80211_IOC_SSID;
1507                 for (ireq.i_val = 0; ireq.i_val < num; ireq.i_val++) {
1508                         if (ioctl(s, SIOCG80211, &ireq) >= 0 && ireq.i_len > 0) {
1509                                 printf(" %d:", ireq.i_val + 1);
1510                                 print_string(data, ireq.i_len);
1511                         }
1512                 }
1513         } else
1514                 print_string(data, ireq.i_len);
1515
1516         ireq.i_type = IEEE80211_IOC_CHANNEL;
1517         if (ioctl(s, SIOCG80211, &ireq) < 0)
1518                 goto end;
1519         c = getchaninfo(s, ireq.i_val);
1520         if (ireq.i_val != -1) {
1521                 printf(" channel %d", ireq.i_val);
1522                 if (verbose)
1523                         printf(" (%u)", c->ic_freq);
1524         } else if (verbose)
1525                 printf(" channel UNDEF");
1526
1527         ireq.i_type = IEEE80211_IOC_BSSID;
1528         ireq.i_len = IEEE80211_ADDR_LEN;
1529         if (ioctl(s, SIOCG80211, &ireq) >= 0 &&
1530             (memcmp(ireq.i_data, zerobssid, sizeof(zerobssid)) != 0 || verbose))
1531                 printf(" bssid %s", ether_ntoa(ireq.i_data));
1532
1533         ireq.i_type = IEEE80211_IOC_STATIONNAME;
1534         if (ioctl(s, SIOCG80211, &ireq) != -1) {
1535                 printf("\n\tstationname ");
1536                 print_string(data, ireq.i_len);
1537         }
1538
1539         spacer = ' ';           /* force first break */
1540         LINE_BREAK();
1541
1542         ireq.i_type = IEEE80211_IOC_AUTHMODE;
1543         if (ioctl(s, SIOCG80211, &ireq) != -1) {
1544                 switch (ireq.i_val) {
1545                         case IEEE80211_AUTH_NONE:
1546                                 LINE_CHECK("authmode NONE");
1547                                 break;
1548                         case IEEE80211_AUTH_OPEN:
1549                                 LINE_CHECK("authmode OPEN");
1550                                 break;
1551                         case IEEE80211_AUTH_SHARED:
1552                                 LINE_CHECK("authmode SHARED");
1553                                 break;
1554                         case IEEE80211_AUTH_8021X:
1555                                 LINE_CHECK("authmode 802.1x");
1556                                 break;
1557                         case IEEE80211_AUTH_WPA:
1558                                 ireq.i_type = IEEE80211_IOC_WPA;
1559                                 if (ioctl(s, SIOCG80211, &ireq) != -1)
1560                                         wpa = ireq.i_val;
1561                                 if (!wpa)
1562                                         wpa = 1;        /* default to WPA1 */
1563                                 switch (wpa) {
1564                                 case 2:
1565                                         LINE_CHECK("authmode WPA2/802.11i");
1566                                         break;
1567                                 case 3:
1568                                         LINE_CHECK("authmode WPA1+WPA2/802.11i");
1569                                         break;
1570                                 default:
1571                                         LINE_CHECK("authmode WPA");
1572                                         break;
1573                                 }
1574                                 break;
1575                         case IEEE80211_AUTH_AUTO:
1576                                 LINE_CHECK("authmode AUTO");
1577                                 break;
1578                         default:
1579                                 LINE_CHECK("authmode UNKNOWN (0x%x)",
1580                                         ireq.i_val);
1581                                 break;
1582                 }
1583         }
1584
1585         ireq.i_type = IEEE80211_IOC_WEP;
1586         if (ioctl(s, SIOCG80211, &ireq) != -1 &&
1587             ireq.i_val != IEEE80211_WEP_NOSUP) {
1588                 int firstkey, wepmode;
1589
1590                 wepmode = ireq.i_val;
1591                 switch (wepmode) {
1592                         case IEEE80211_WEP_OFF:
1593                                 LINE_CHECK("privacy OFF");
1594                                 break;
1595                         case IEEE80211_WEP_ON:
1596                                 LINE_CHECK("privacy ON");
1597                                 break;
1598                         case IEEE80211_WEP_MIXED:
1599                                 LINE_CHECK("privacy MIXED");
1600                                 break;
1601                         default:
1602                                 LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
1603                                 break;
1604                 }
1605
1606                 /*
1607                  * If we get here then we've got WEP support so we need
1608                  * to print WEP status.
1609                  */
1610
1611                 ireq.i_type = IEEE80211_IOC_WEPTXKEY;
1612                 if (ioctl(s, SIOCG80211, &ireq) < 0) {
1613                         warn("WEP support, but no tx key!");
1614                         goto end;
1615                 }
1616                 if (ireq.i_val != -1)
1617                         LINE_CHECK("deftxkey %d", ireq.i_val+1);
1618                 else if (wepmode != IEEE80211_WEP_OFF || verbose)
1619                         LINE_CHECK("deftxkey UNDEF");
1620
1621                 ireq.i_type = IEEE80211_IOC_NUMWEPKEYS;
1622                 if (ioctl(s, SIOCG80211, &ireq) < 0) {
1623                         warn("WEP support, but no NUMWEPKEYS support!");
1624                         goto end;
1625                 }
1626                 num = ireq.i_val;
1627
1628                 firstkey = 1;
1629                 for (i = 0; i < num; i++) {
1630                         struct ieee80211req_key ik;
1631
1632                         memset(&ik, 0, sizeof(ik));
1633                         ik.ik_keyix = i;
1634                         ireq.i_type = IEEE80211_IOC_WPAKEY;
1635                         ireq.i_data = &ik;
1636                         ireq.i_len = sizeof(ik);
1637                         if (ioctl(s, SIOCG80211, &ireq) < 0) {
1638                                 warn("WEP support, but can get keys!");
1639                                 goto end;
1640                         }
1641                         if (ik.ik_keylen != 0) {
1642                                 if (verbose)
1643                                         LINE_BREAK();
1644                                 printkey(&ik);
1645                                 firstkey = 0;
1646                         }
1647                 }
1648         }
1649
1650         ireq.i_type = IEEE80211_IOC_POWERSAVE;
1651         if (ioctl(s, SIOCG80211, &ireq) != -1 &&
1652             ireq.i_val != IEEE80211_POWERSAVE_NOSUP ) {
1653                 if (ireq.i_val != IEEE80211_POWERSAVE_OFF || verbose) {
1654                         switch (ireq.i_val) {
1655                                 case IEEE80211_POWERSAVE_OFF:
1656                                         LINE_CHECK("powersavemode OFF");
1657                                         break;
1658                                 case IEEE80211_POWERSAVE_CAM:
1659                                         LINE_CHECK("powersavemode CAM");
1660                                         break;
1661                                 case IEEE80211_POWERSAVE_PSP:
1662                                         LINE_CHECK("powersavemode PSP");
1663                                         break;
1664                                 case IEEE80211_POWERSAVE_PSP_CAM:
1665                                         LINE_CHECK("powersavemode PSP-CAM");
1666                                         break;
1667                         }
1668                         ireq.i_type = IEEE80211_IOC_POWERSAVESLEEP;
1669                         if (ioctl(s, SIOCG80211, &ireq) != -1)
1670                                 LINE_CHECK("powersavesleep %d", ireq.i_val);
1671                 }
1672         }
1673
1674         ireq.i_type = IEEE80211_IOC_TXPOWMAX;
1675         if (ioctl(s, SIOCG80211, &ireq) != -1)
1676                 LINE_CHECK("txpowmax %d", ireq.i_val);
1677
1678         if (verbose) {
1679                 ireq.i_type = IEEE80211_IOC_TXPOWER;
1680                 if (ioctl(s, SIOCG80211, &ireq) != -1)
1681                         LINE_CHECK("txpower %d", ireq.i_val);
1682         }
1683
1684         ireq.i_type = IEEE80211_IOC_RTSTHRESHOLD;
1685         if (ioctl(s, SIOCG80211, &ireq) != -1) {
1686                 if (ireq.i_val != IEEE80211_RTS_MAX || verbose)
1687                         LINE_CHECK("rtsthreshold %d", ireq.i_val);
1688         }
1689
1690         ireq.i_type = IEEE80211_IOC_MCAST_RATE;
1691         if (ioctl(s, SIOCG80211, &ireq) != -1) {
1692                 if (ireq.i_val != 2*1 || verbose) {
1693                         if (ireq.i_val == 11)
1694                                 LINE_CHECK("mcastrate 5.5");
1695                         else
1696                                 LINE_CHECK("mcastrate %d", ireq.i_val/2);
1697                 }
1698         }
1699
1700         ireq.i_type = IEEE80211_IOC_FRAGTHRESHOLD;
1701         if (ioctl(s, SIOCG80211, &ireq) != -1) {
1702                 if (ireq.i_val != IEEE80211_FRAG_MAX || verbose)
1703                         LINE_CHECK("fragthreshold %d", ireq.i_val);
1704         }
1705
1706         if (IEEE80211_IS_CHAN_G(c) || IEEE80211_IS_CHAN_PUREG(c) || verbose) {
1707                 ireq.i_type = IEEE80211_IOC_PUREG;
1708                 if (ioctl(s, SIOCG80211, &ireq) != -1) {
1709                         if (ireq.i_val)
1710                                 LINE_CHECK("pureg");
1711                         else if (verbose)
1712                                 LINE_CHECK("-pureg");
1713                 }
1714                 ireq.i_type = IEEE80211_IOC_PROTMODE;
1715                 if (ioctl(s, SIOCG80211, &ireq) != -1) {
1716                         switch (ireq.i_val) {
1717                                 case IEEE80211_PROTMODE_OFF:
1718                                         LINE_CHECK("protmode OFF");
1719                                         break;
1720                                 case IEEE80211_PROTMODE_CTS:
1721                                         LINE_CHECK("protmode CTS");
1722                                         break;
1723                                 case IEEE80211_PROTMODE_RTSCTS:
1724                                         LINE_CHECK("protmode RTSCTS");
1725                                         break;
1726                                 default:
1727                                         LINE_CHECK("protmode UNKNOWN (0x%x)",
1728                                                 ireq.i_val);
1729                                         break;
1730                         }
1731                 }
1732         }
1733
1734         ireq.i_type = IEEE80211_IOC_WME;
1735         if (ioctl(s, SIOCG80211, &ireq) != -1) {
1736                 wme = ireq.i_val;
1737                 if (wme)
1738                         LINE_CHECK("wme");
1739                 else if (verbose)
1740                         LINE_CHECK("-wme");
1741         } else
1742                 wme = 0;
1743
1744         ireq.i_type = IEEE80211_IOC_BURST;
1745         if (ioctl(s, SIOCG80211, &ireq) != -1) {
1746                 if (ireq.i_val)
1747                         LINE_CHECK("burst");
1748                 else if (verbose)
1749                         LINE_CHECK("-burst");
1750         }
1751
1752         if (opmode == IEEE80211_M_HOSTAP) {
1753                 ireq.i_type = IEEE80211_IOC_HIDESSID;
1754                 if (ioctl(s, SIOCG80211, &ireq) != -1) {
1755                         if (ireq.i_val)
1756                                 LINE_CHECK("ssid HIDE");
1757                         else if (verbose)
1758                                 LINE_CHECK("ssid SHOW");
1759                 }
1760
1761                 ireq.i_type = IEEE80211_IOC_APBRIDGE;
1762                 if (ioctl(s, SIOCG80211, &ireq) != -1) {
1763                         if (!ireq.i_val)
1764                                 LINE_CHECK("-apbridge");
1765                         else if (verbose)
1766                                 LINE_CHECK("apbridge");
1767                 }
1768
1769                 ireq.i_type = IEEE80211_IOC_DTIM_PERIOD;
1770                 if (ioctl(s, SIOCG80211, &ireq) != -1)
1771                         LINE_CHECK("dtimperiod %u", ireq.i_val);
1772         } else {
1773                 ireq.i_type = IEEE80211_IOC_ROAMING;
1774                 if (ioctl(s, SIOCG80211, &ireq) != -1) {
1775                         if (ireq.i_val != IEEE80211_ROAMING_AUTO || verbose) {
1776                                 switch (ireq.i_val) {
1777                                 case IEEE80211_ROAMING_DEVICE:
1778                                         LINE_CHECK("roaming DEVICE");
1779                                         break;
1780                                 case IEEE80211_ROAMING_AUTO:
1781                                         LINE_CHECK("roaming AUTO");
1782                                         break;
1783                                 case IEEE80211_ROAMING_MANUAL:
1784                                         LINE_CHECK("roaming MANUAL");
1785                                         break;
1786                                 default:
1787                                         LINE_CHECK("roaming UNKNOWN (0x%x)",
1788                                                 ireq.i_val);
1789                                         break;
1790                                 }
1791                         }
1792                 }
1793         }
1794         ireq.i_type = IEEE80211_IOC_BEACON_INTERVAL;
1795         if (ioctl(s, SIOCG80211, &ireq) != -1) {
1796                 if (ireq.i_val)
1797                         LINE_CHECK("bintval %u", ireq.i_val);
1798                 else if (verbose)
1799                         LINE_CHECK("bintval %u", ireq.i_val);
1800         }
1801
1802         if (wme && verbose) {
1803                 LINE_BREAK();
1804                 list_wme(s);
1805         }
1806
1807         if (wpa) {
1808                 ireq.i_type = IEEE80211_IOC_COUNTERMEASURES;
1809                 if (ioctl(s, SIOCG80211, &ireq) != -1) {
1810                         if (ireq.i_val)
1811                                 LINE_CHECK("countermeasures");
1812                         else if (verbose)
1813                                 LINE_CHECK("-countermeasures");
1814                 }
1815 #if 0
1816                 /* XXX not interesting with WPA done in user space */
1817                 ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
1818                 if (ioctl(s, SIOCG80211, &ireq) != -1) {
1819                 }
1820
1821                 ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
1822                 if (ioctl(s, SIOCG80211, &ireq) != -1) {
1823                         LINE_CHECK("mcastcipher ");
1824                         printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
1825                         spacer = ' ';
1826                 }
1827
1828                 ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
1829                 if (ioctl(s, SIOCG80211, &ireq) != -1) {
1830                         LINE_CHECK("ucastcipher ");
1831                         printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
1832                 }
1833
1834                 if (wpa & 2) {
1835                         ireq.i_type = IEEE80211_IOC_RSNCAPS;
1836                         if (ioctl(s, SIOCG80211, &ireq) != -1) {
1837                                 LINE_CHECK("RSN caps 0x%x", ireq.i_val);
1838                                 spacer = ' ';
1839                         }
1840                 }
1841
1842                 ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
1843                 if (ioctl(s, SIOCG80211, &ireq) != -1) {
1844                 }
1845 #endif
1846                 LINE_BREAK();
1847         }
1848         LINE_BREAK();
1849
1850 end:
1851         return;
1852 }
1853
1854 static void
1855 set80211(int s, int type, int val, int len, u_int8_t *data)
1856 {
1857         struct ieee80211req     ireq;
1858
1859         (void) memset(&ireq, 0, sizeof(ireq));
1860         (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1861         ireq.i_type = type;
1862         ireq.i_val = val;
1863         ireq.i_len = len;
1864         ireq.i_data = data;
1865         if (ioctl(s, SIOCS80211, &ireq) < 0)
1866                 err(1, "SIOCS80211");
1867 }
1868
1869 static const char *
1870 get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
1871 {
1872         int len;
1873         int hexstr;
1874         u_int8_t *p;
1875
1876         len = *lenp;
1877         p = buf;
1878         hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
1879         if (hexstr)
1880                 val += 2;
1881         for (;;) {
1882                 if (*val == '\0')
1883                         break;
1884                 if (sep != NULL && strchr(sep, *val) != NULL) {
1885                         val++;
1886                         break;
1887                 }
1888                 if (hexstr) {
1889                         if (!isxdigit((u_char)val[0])) {
1890                                 warnx("bad hexadecimal digits");
1891                                 return NULL;
1892                         }
1893                         if (!isxdigit((u_char)val[1])) {
1894                                 warnx("odd count hexadecimal digits");
1895                                 return NULL;
1896                         }
1897                 }
1898                 if (p >= buf + len) {
1899                         if (hexstr)
1900                                 warnx("hexadecimal digits too long");
1901                         else
1902                                 warnx("string too long");
1903                         return NULL;
1904                 }
1905                 if (hexstr) {
1906 #define tohex(x)        (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
1907                         *p++ = (tohex((u_char)val[0]) << 4) |
1908                             tohex((u_char)val[1]);
1909 #undef tohex
1910                         val += 2;
1911                 } else
1912                         *p++ = *val++;
1913         }
1914         len = p - buf;
1915         /* The string "-" is treated as the empty string. */
1916         if (!hexstr && len == 1 && buf[0] == '-')
1917                 len = 0;
1918         if (len < *lenp)
1919                 memset(p, 0, *lenp - len);
1920         *lenp = len;
1921         return val;
1922 }
1923
1924 static void
1925 print_string(const u_int8_t *buf, int len)
1926 {
1927         int i;
1928         int hasspc;
1929
1930         i = 0;
1931         hasspc = 0;
1932         for (; i < len; i++) {
1933                 if (!isprint(buf[i]) && buf[i] != '\0')
1934                         break;
1935                 if (isspace(buf[i]))
1936                         hasspc++;
1937         }
1938         if (i == len) {
1939                 if (hasspc || len == 0 || buf[0] == '\0')
1940                         printf("\"%.*s\"", len, buf);
1941                 else
1942                         printf("%.*s", len, buf);
1943         } else {
1944                 printf("0x");
1945                 for (i = 0; i < len; i++)
1946                         printf("%02x", buf[i]);
1947         }
1948 }
1949
1950 static struct cmd ieee80211_cmds[] = {
1951         DEF_CMD_ARG("ssid",             set80211ssid),
1952         DEF_CMD_ARG("nwid",             set80211ssid),
1953         DEF_CMD_ARG("stationname",      set80211stationname),
1954         DEF_CMD_ARG("station",          set80211stationname),   /* BSD/OS */
1955         DEF_CMD_ARG("channel",          set80211channel),
1956         DEF_CMD_ARG("authmode",         set80211authmode),
1957         DEF_CMD_ARG("powersavemode",    set80211powersavemode),
1958         DEF_CMD("powersave",    1,      set80211powersave),
1959         DEF_CMD("-powersave",   0,      set80211powersave),
1960         DEF_CMD_ARG("powersavesleep",   set80211powersavesleep),
1961         DEF_CMD_ARG("wepmode",          set80211wepmode),
1962         DEF_CMD("wep",          1,      set80211wep),
1963         DEF_CMD("-wep",         0,      set80211wep),
1964         DEF_CMD_ARG("deftxkey",         set80211weptxkey),
1965         DEF_CMD_ARG("weptxkey",         set80211weptxkey),
1966         DEF_CMD_ARG("wepkey",           set80211wepkey),
1967         DEF_CMD_ARG("nwkey",            set80211nwkey),         /* NetBSD */
1968         DEF_CMD("-nwkey",       0,      set80211wep),           /* NetBSD */
1969         DEF_CMD_ARG("rtsthreshold",     set80211rtsthreshold),
1970         DEF_CMD_ARG("protmode",         set80211protmode),
1971         DEF_CMD_ARG("txpower",          set80211txpower),
1972         DEF_CMD_ARG("roaming",          set80211roaming),
1973         DEF_CMD("wme",          1,      set80211wme),
1974         DEF_CMD("-wme",         0,      set80211wme),
1975         DEF_CMD("hidessid",     1,      set80211hidessid),
1976         DEF_CMD("-hidessid",    0,      set80211hidessid),
1977         DEF_CMD("apbridge",     1,      set80211apbridge),
1978         DEF_CMD("-apbridge",    0,      set80211apbridge),
1979         DEF_CMD_ARG("chanlist",         set80211chanlist),
1980         DEF_CMD_ARG("bssid",            set80211bssid),
1981         DEF_CMD_ARG("ap",               set80211bssid),
1982         DEF_CMD("scan", 0,              set80211scan),
1983         DEF_CMD_ARG("list",             set80211list),
1984         DEF_CMD_ARG2("cwmin",           set80211cwmin),
1985         DEF_CMD_ARG2("cwmax",           set80211cwmax),
1986         DEF_CMD_ARG2("aifs",            set80211aifs),
1987         DEF_CMD_ARG2("txoplimit",       set80211txoplimit),
1988         DEF_CMD_ARG("acm",              set80211acm),
1989         DEF_CMD_ARG("-acm",             set80211noacm),
1990         DEF_CMD_ARG("ack",              set80211ackpolicy),
1991         DEF_CMD_ARG("-ack",             set80211noackpolicy),
1992         DEF_CMD_ARG2("bss:cwmin",       set80211bsscwmin),
1993         DEF_CMD_ARG2("bss:cwmax",       set80211bsscwmax),
1994         DEF_CMD_ARG2("bss:aifs",        set80211bssaifs),
1995         DEF_CMD_ARG2("bss:txoplimit",   set80211bsstxoplimit),
1996         DEF_CMD_ARG("dtimperiod",       set80211dtimperiod),
1997         DEF_CMD_ARG("bintval",          set80211bintval),
1998         DEF_CMD("mac:open",     IEEE80211_MACCMD_POLICY_OPEN,   set80211maccmd),
1999         DEF_CMD("mac:allow",    IEEE80211_MACCMD_POLICY_ALLOW,  set80211maccmd),
2000         DEF_CMD("mac:deny",     IEEE80211_MACCMD_POLICY_DENY,   set80211maccmd),
2001         DEF_CMD("mac:flush",    IEEE80211_MACCMD_FLUSH,         set80211maccmd),
2002         DEF_CMD("mac:detach",   IEEE80211_MACCMD_DETACH,        set80211maccmd),
2003         DEF_CMD_ARG("mac:add",          set80211addmac),
2004         DEF_CMD_ARG("mac:del",          set80211delmac),
2005         DEF_CMD_ARG("mac:kick",         set80211kickmac),
2006         DEF_CMD("pureg",        1,      set80211pureg),
2007         DEF_CMD("-pureg",       0,      set80211pureg),
2008         DEF_CMD_ARG("mcastrate",        set80211mcastrate),
2009         DEF_CMD_ARG("fragthreshold",    set80211fragthreshold),
2010         DEF_CMD("burst",        1,      set80211burst),
2011         DEF_CMD("-burst",       0,      set80211burst),
2012         DEF_CMD_ARG("ratectl",          set80211ratectl)
2013 };
2014 static struct afswtch af_ieee80211 = {
2015         .af_name        = "af_ieee80211",
2016         .af_af          = AF_UNSPEC,
2017         .af_other_status = ieee80211_status,
2018 };
2019
2020 static __constructor void
2021 ieee80211_ctor(void)
2022 {
2023 #define N(a)    (sizeof(a) / sizeof(a[0]))
2024         int i;
2025
2026         for (i = 0; i < N(ieee80211_cmds);  i++)
2027                 cmd_register(&ieee80211_cmds[i]);
2028         af_register(&af_ieee80211);
2029 #undef N
2030 }