2 * Copyright 2001 The Aerospace Corporation. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
31 * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
32 * All rights reserved.
34 * This code is derived from software contributed to The NetBSD Foundation
35 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
36 * NASA Ames Research Center.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the NetBSD
49 * Foundation, Inc. and its contributors.
50 * 4. Neither the name of The NetBSD Foundation nor the names of its
51 * contributors may be used to endorse or promote products derived
52 * from this software without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
55 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
58 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 * POSSIBILITY OF SUCH DAMAGE.
67 #include <sys/param.h>
68 #include <sys/ioctl.h>
69 #include <sys/socket.h>
70 #include <sys/sysctl.h>
73 #include <net/ethernet.h>
75 #include <net/if_dl.h>
76 #include <net/if_types.h>
77 #include <net/if_media.h>
78 #include <net/route.h>
80 #include <net80211/ieee80211.h>
81 #include <net80211/ieee80211_crypto.h>
82 #include <net80211/ieee80211_ioctl.h>
94 #include <stddef.h> /* NB: for offsetof */
102 static void LINE_INIT(char c);
103 static void LINE_BREAK(void);
104 static void LINE_CHECK(const char *fmt, ...);
106 /* XXX need max array size */
107 static const int htrates[16] = {
108 13, /* IFM_IEEE80211_MCS0 */
109 26, /* IFM_IEEE80211_MCS1 */
110 39, /* IFM_IEEE80211_MCS2 */
111 52, /* IFM_IEEE80211_MCS3 */
112 78, /* IFM_IEEE80211_MCS4 */
113 104, /* IFM_IEEE80211_MCS5 */
114 117, /* IFM_IEEE80211_MCS6 */
115 130, /* IFM_IEEE80211_MCS7 */
116 26, /* IFM_IEEE80211_MCS8 */
117 52, /* IFM_IEEE80211_MCS9 */
118 78, /* IFM_IEEE80211_MCS10 */
119 104, /* IFM_IEEE80211_MCS11 */
120 156, /* IFM_IEEE80211_MCS12 */
121 208, /* IFM_IEEE80211_MCS13 */
122 234, /* IFM_IEEE80211_MCS14 */
123 260, /* IFM_IEEE80211_MCS15 */
126 static int get80211(int s, int type, void *data, int len);
127 static int get80211len(int s, int type, void *data, int len, int *plen);
128 static int get80211val(int s, int type, int *val);
129 static void set80211(int s, int type, int val, int len, void *data);
130 static const char *get_string(const char *val, const char *sep,
131 u_int8_t *buf, int *lenp);
132 static void print_string(const u_int8_t *buf, int len);
134 static struct ieee80211req_chaninfo chaninfo;
135 static struct ifmediareq *ifmr;
136 static struct ieee80211_channel curchan;
137 static int gotcurchan = 0;
138 static int htconf = 0;
139 static int gothtconf = 0;
146 if (get80211val(s, IEEE80211_IOC_HTCONF, &htconf) < 0)
147 warn("unable to get HT configuration information");
152 * Collect channel info from the kernel. We use this (mostly)
153 * to handle mapping between frequency and IEEE channel number.
158 if (chaninfo.ic_nchans != 0)
160 if (get80211(s, IEEE80211_IOC_CHANINFO, &chaninfo, sizeof(chaninfo)) < 0)
161 errx(1, "unable to get channel information");
163 ifmr = ifmedia_getstate(s);
168 * Given the channel at index i with attributes from,
169 * check if there is a channel with attributes to in
170 * the channel table. With suitable attributes this
171 * allows the caller to look for promotion; e.g. from
175 canpromote(int i, int from, int to)
177 const struct ieee80211_channel *fc = &chaninfo.ic_chans[i];
180 if ((fc->ic_flags & from) != from)
182 /* NB: quick check exploiting ordering of chans w/ same frequency */
183 if (i+1 < chaninfo.ic_nchans &&
184 chaninfo.ic_chans[i+1].ic_freq == fc->ic_freq &&
185 (chaninfo.ic_chans[i+1].ic_flags & to) == to)
187 /* brute force search in case channel list is not ordered */
188 for (j = 0; j < chaninfo.ic_nchans; j++) {
189 const struct ieee80211_channel *tc = &chaninfo.ic_chans[j];
191 tc->ic_freq == fc->ic_freq && (tc->ic_flags & to) == to)
198 * Handle channel promotion. When a channel is specified with
199 * only a frequency we want to promote it to the ``best'' channel
200 * available. The channel list has separate entries for 11b, 11g,
201 * 11a, and 11n[ga] channels so specifying a frequency w/o any
202 * attributes requires we upgrade, e.g. from 11b -> 11g. This
203 * gets complicated when the channel is specified on the same
204 * command line with a media request that constrains the available
205 * channe list (e.g. mode 11a); we want to honor that to avoid
206 * confusing behaviour.
212 * Query the current mode of the interface in case it's
213 * constrained (e.g. to 11a). We must do this carefully
214 * as there may be a pending ifmedia request in which case
215 * asking the kernel will give us the wrong answer. This
216 * is an unfortunate side-effect of the way ifconfig is
217 * structure for modularity (yech).
219 * NB: ifmr is actually setup in getchaninfo (above); we
220 * assume it's called coincident with to this call so
221 * we have a ``current setting''; otherwise we must pass
222 * the socket descriptor down to here so we can make
223 * the ifmedia_getstate call ourselves.
225 int chanmode = ifmr != NULL ? IFM_MODE(ifmr->ifm_current) : IFM_AUTO;
227 /* when ambiguous promote to ``best'' */
228 /* NB: we abitrarily pick HT40+ over HT40- */
229 if (chanmode != IFM_IEEE80211_11B)
230 i = canpromote(i, IEEE80211_CHAN_B, IEEE80211_CHAN_G);
231 if (chanmode != IFM_IEEE80211_11G && (htconf & 1)) {
232 i = canpromote(i, IEEE80211_CHAN_G,
233 IEEE80211_CHAN_G | IEEE80211_CHAN_HT20);
235 i = canpromote(i, IEEE80211_CHAN_G,
236 IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D);
237 i = canpromote(i, IEEE80211_CHAN_G,
238 IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U);
241 if (chanmode != IFM_IEEE80211_11A && (htconf & 1)) {
242 i = canpromote(i, IEEE80211_CHAN_A,
243 IEEE80211_CHAN_A | IEEE80211_CHAN_HT20);
245 i = canpromote(i, IEEE80211_CHAN_A,
246 IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D);
247 i = canpromote(i, IEEE80211_CHAN_A,
248 IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U);
255 mapfreq(struct ieee80211_channel *chan, int freq, int flags)
259 for (i = 0; i < chaninfo.ic_nchans; i++) {
260 const struct ieee80211_channel *c = &chaninfo.ic_chans[i];
262 if (c->ic_freq == freq && (c->ic_flags & flags) == flags) {
264 /* when ambiguous promote to ``best'' */
265 c = &chaninfo.ic_chans[promote(i)];
271 errx(1, "unknown/undefined frequency %u/0x%x", freq, flags);
275 mapchan(struct ieee80211_channel *chan, int ieee, int flags)
279 for (i = 0; i < chaninfo.ic_nchans; i++) {
280 const struct ieee80211_channel *c = &chaninfo.ic_chans[i];
282 if (c->ic_ieee == ieee && (c->ic_flags & flags) == flags) {
284 /* when ambiguous promote to ``best'' */
285 c = &chaninfo.ic_chans[promote(i)];
291 errx(1, "unknown/undefined channel number %d flags 0x%x", ieee, flags);
294 static const struct ieee80211_channel *
299 if (get80211(s, IEEE80211_IOC_CURCHAN, &curchan, sizeof(curchan)) < 0) {
301 /* fall back to legacy ioctl */
302 if (get80211val(s, IEEE80211_IOC_CHANNEL, &val) < 0)
303 errx(-1, "cannot figure out current channel");
305 mapchan(&curchan, val, 0);
312 ieee80211_mhz2ieee(int freq, int flags)
314 struct ieee80211_channel chan;
315 mapfreq(&chan, freq, flags);
320 isanyarg(const char *arg)
322 return (strncmp(arg, "-", 1) == 0 ||
323 strncasecmp(arg, "any", 3) == 0 || strncasecmp(arg, "off", 3) == 0);
327 set80211ssid(const char *val, int d, int s, const struct afswtch *rafp)
331 u_int8_t data[IEEE80211_NWID_LEN];
335 if (len > 2 && isdigit(val[0]) && val[1] == ':') {
340 bzero(data, sizeof(data));
342 if (get_string(val, NULL, data, &len) == NULL)
345 set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
349 set80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
354 bzero(data, sizeof(data));
356 get_string(val, NULL, data, &len);
358 set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
362 * Parse a channel specification for attributes/flags.
364 * freq/xx channel width (5,10,20,40,40+,40-)
365 * freq:mode channel mode (a,b,g,h,n,t,s,d)
367 * These can be combined in either order; e.g. 2437:ng/40.
368 * Modes are case insensitive.
370 * The result is not validated here; it's assumed to be
371 * checked against the channel table fetched from the kernel.
374 getchannelflags(const char *val, int freq)
376 #define _CHAN_HT 0x80000000
382 cp = strchr(val, ':');
384 for (cp++; isalpha((int) *cp); cp++) {
385 /* accept mixed case */
390 case 'a': /* 802.11a */
391 flags |= IEEE80211_CHAN_A;
393 case 'b': /* 802.11b */
394 flags |= IEEE80211_CHAN_B;
396 case 'g': /* 802.11g */
397 flags |= IEEE80211_CHAN_G;
399 case 'h': /* ht = 802.11n */
400 case 'n': /* 802.11n */
401 flags |= _CHAN_HT; /* NB: private */
403 case 'd': /* dt = Atheros Dynamic Turbo */
404 flags |= IEEE80211_CHAN_TURBO;
406 case 't': /* ht, dt, st, t */
407 /* dt and unadorned t specify Dynamic Turbo */
408 if ((flags & (IEEE80211_CHAN_STURBO|_CHAN_HT)) == 0)
409 flags |= IEEE80211_CHAN_TURBO;
411 case 's': /* st = Atheros Static Turbo */
412 flags |= IEEE80211_CHAN_STURBO;
415 errx(-1, "%s: Invalid channel attribute %c\n",
420 cp = strchr(val, '/');
423 u_long cw = strtoul(cp+1, &ep, 10);
427 flags |= IEEE80211_CHAN_QUARTER;
430 flags |= IEEE80211_CHAN_HALF;
433 /* NB: this may be removed below */
434 flags |= IEEE80211_CHAN_HT20;
437 if (ep != NULL && *ep == '+')
438 flags |= IEEE80211_CHAN_HT40U;
439 else if (ep != NULL && *ep == '-')
440 flags |= IEEE80211_CHAN_HT40D;
443 errx(-1, "%s: Invalid channel width\n", val);
447 * Cleanup specifications.
449 if ((flags & _CHAN_HT) == 0) {
451 * If user specified freq/20 or freq/40 quietly remove
452 * HT cw attributes depending on channel use. To give
453 * an explicit 20/40 width for an HT channel you must
454 * indicate it is an HT channel since all HT channels
455 * are also usable for legacy operation; e.g. freq:n/40.
457 flags &= ~IEEE80211_CHAN_HT;
460 * Remove private indicator that this is an HT channel
461 * and if no explicit channel width has been given
462 * provide the default settings.
465 if ((flags & IEEE80211_CHAN_HT) == 0) {
466 struct ieee80211_channel chan;
468 * Consult the channel list to see if we can use
469 * HT40+ or HT40- (if both the map routines choose).
472 mapfreq(&chan, freq, 0);
474 mapchan(&chan, freq, 0);
475 flags |= (chan.ic_flags & IEEE80211_CHAN_HT);
483 set80211channel(const char *val, int d, int s, const struct afswtch *rafp)
485 struct ieee80211_channel chan;
487 memset(&chan, 0, sizeof(chan));
488 if (!isanyarg(val)) {
493 flags = getchannelflags(val, v);
494 if (v > 255) { /* treat as frequency */
495 mapfreq(&chan, v, flags);
497 mapchan(&chan, v, flags);
500 chan.ic_freq = IEEE80211_CHAN_ANY;
502 set80211(s, IEEE80211_IOC_CURCHAN, 0, sizeof(chan), &chan);
506 set80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
510 if (strcasecmp(val, "none") == 0) {
511 mode = IEEE80211_AUTH_NONE;
512 } else if (strcasecmp(val, "open") == 0) {
513 mode = IEEE80211_AUTH_OPEN;
514 } else if (strcasecmp(val, "shared") == 0) {
515 mode = IEEE80211_AUTH_SHARED;
516 } else if (strcasecmp(val, "8021x") == 0) {
517 mode = IEEE80211_AUTH_8021X;
518 } else if (strcasecmp(val, "wpa") == 0) {
519 mode = IEEE80211_AUTH_WPA;
521 errx(1, "unknown authmode");
524 set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
528 set80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
532 if (strcasecmp(val, "off") == 0) {
533 mode = IEEE80211_POWERSAVE_OFF;
534 } else if (strcasecmp(val, "on") == 0) {
535 mode = IEEE80211_POWERSAVE_ON;
536 } else if (strcasecmp(val, "cam") == 0) {
537 mode = IEEE80211_POWERSAVE_CAM;
538 } else if (strcasecmp(val, "psp") == 0) {
539 mode = IEEE80211_POWERSAVE_PSP;
540 } else if (strcasecmp(val, "psp-cam") == 0) {
541 mode = IEEE80211_POWERSAVE_PSP_CAM;
543 errx(1, "unknown powersavemode");
546 set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
550 set80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
553 set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
556 set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
561 set80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
563 set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
567 set80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
571 if (strcasecmp(val, "off") == 0) {
572 mode = IEEE80211_WEP_OFF;
573 } else if (strcasecmp(val, "on") == 0) {
574 mode = IEEE80211_WEP_ON;
575 } else if (strcasecmp(val, "mixed") == 0) {
576 mode = IEEE80211_WEP_MIXED;
578 errx(1, "unknown wep mode");
581 set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
585 set80211wep(const char *val, int d, int s, const struct afswtch *rafp)
587 set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
591 isundefarg(const char *arg)
593 return (strcmp(arg, "-") == 0 || strncasecmp(arg, "undef", 5) == 0);
597 set80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
600 set80211(s, IEEE80211_IOC_WEPTXKEY, IEEE80211_KEYIX_NONE, 0, NULL);
602 set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
606 set80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
610 u_int8_t data[IEEE80211_KEYBUF_SIZE];
612 if (isdigit(val[0]) && val[1] == ':') {
617 bzero(data, sizeof(data));
619 get_string(val, NULL, data, &len);
621 set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
625 * This function is purely a NetBSD compatability interface. The NetBSD
626 * interface is too inflexible, but it's there so we'll support it since
627 * it's not all that hard.
630 set80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
634 u_int8_t data[IEEE80211_KEYBUF_SIZE];
636 set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
638 if (isdigit(val[0]) && val[1] == ':') {
639 txkey = val[0]-'0'-1;
642 for (i = 0; i < 4; i++) {
643 bzero(data, sizeof(data));
645 val = get_string(val, ",", data, &len);
649 set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
652 bzero(data, sizeof(data));
654 get_string(val, NULL, data, &len);
657 set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
659 bzero(data, sizeof(data));
660 for (i = 1; i < 4; i++)
661 set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
664 set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
668 set80211rtsthreshold(const char *val, int d, int s, const struct afswtch *rafp)
670 set80211(s, IEEE80211_IOC_RTSTHRESHOLD,
671 isundefarg(val) ? IEEE80211_RTS_MAX : atoi(val), 0, NULL);
675 set80211protmode(const char *val, int d, int s, const struct afswtch *rafp)
679 if (strcasecmp(val, "off") == 0) {
680 mode = IEEE80211_PROTMODE_OFF;
681 } else if (strcasecmp(val, "cts") == 0) {
682 mode = IEEE80211_PROTMODE_CTS;
683 } else if (strncasecmp(val, "rtscts", 3) == 0) {
684 mode = IEEE80211_PROTMODE_RTSCTS;
686 errx(1, "unknown protection mode");
689 set80211(s, IEEE80211_IOC_PROTMODE, mode, 0, NULL);
693 set80211htprotmode(const char *val, int d, int s, const struct afswtch *rafp)
697 if (strcasecmp(val, "off") == 0) {
698 mode = IEEE80211_PROTMODE_OFF;
699 } else if (strncasecmp(val, "rts", 3) == 0) {
700 mode = IEEE80211_PROTMODE_RTSCTS;
702 errx(1, "unknown protection mode");
705 set80211(s, IEEE80211_IOC_HTPROTMODE, mode, 0, NULL);
709 set80211txpower(const char *val, int d, int s, const struct afswtch *rafp)
711 double v = atof(val);
716 errx(-1, "invalid tx power (must be .5 dBm units)");
717 set80211(s, IEEE80211_IOC_TXPOWER, txpow, 0, NULL);
720 #define IEEE80211_ROAMING_DEVICE 0
721 #define IEEE80211_ROAMING_AUTO 1
722 #define IEEE80211_ROAMING_MANUAL 2
725 set80211roaming(const char *val, int d, int s, const struct afswtch *rafp)
729 if (strcasecmp(val, "device") == 0) {
730 mode = IEEE80211_ROAMING_DEVICE;
731 } else if (strcasecmp(val, "auto") == 0) {
732 mode = IEEE80211_ROAMING_AUTO;
733 } else if (strcasecmp(val, "manual") == 0) {
734 mode = IEEE80211_ROAMING_MANUAL;
736 errx(1, "unknown roaming mode");
738 set80211(s, IEEE80211_IOC_ROAMING, mode, 0, NULL);
742 set80211wme(const char *val, int d, int s, const struct afswtch *rafp)
744 set80211(s, IEEE80211_IOC_WME, d, 0, NULL);
748 set80211hidessid(const char *val, int d, int s, const struct afswtch *rafp)
750 set80211(s, IEEE80211_IOC_HIDESSID, d, 0, NULL);
754 set80211apbridge(const char *val, int d, int s, const struct afswtch *rafp)
756 set80211(s, IEEE80211_IOC_APBRIDGE, d, 0, NULL);
760 set80211fastframes(const char *val, int d, int s, const struct afswtch *rafp)
762 set80211(s, IEEE80211_IOC_FF, d, 0, NULL);
766 set80211dturbo(const char *val, int d, int s, const struct afswtch *rafp)
768 set80211(s, IEEE80211_IOC_TURBOP, d, 0, NULL);
772 set80211chanlist(const char *val, int d, int s, const struct afswtch *rafp)
774 struct ieee80211req_chanlist chanlist;
775 #define MAXCHAN (sizeof(chanlist.ic_channels)*NBBY)
776 char *temp, *cp, *tp;
778 temp = malloc(strlen(val) + 1);
780 errx(1, "malloc failed");
782 memset(&chanlist, 0, sizeof(chanlist));
785 int first, last, f, c;
787 tp = strchr(cp, ',');
790 switch (sscanf(cp, "%u-%u", &first, &last)) {
793 errx(-1, "channel %u out of range, max %zu",
795 setbit(chanlist.ic_channels, first);
799 errx(-1, "channel %u out of range, max %zu",
802 errx(-1, "channel %u out of range, max %zu",
805 errx(-1, "void channel range, %u > %u",
807 for (f = first; f <= last; f++)
808 setbit(chanlist.ic_channels, f);
820 set80211(s, IEEE80211_IOC_CHANLIST, 0, sizeof(chanlist), &chanlist);
825 set80211bssid(const char *val, int d, int s, const struct afswtch *rafp)
828 if (!isanyarg(val)) {
830 struct sockaddr_dl sdl;
832 temp = malloc(strlen(val) + 2); /* ':' and '\0' */
834 errx(1, "malloc failed");
836 strcpy(temp + 1, val);
837 sdl.sdl_len = sizeof(sdl);
838 link_addr(temp, &sdl);
840 if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
841 errx(1, "malformed link-level address");
842 set80211(s, IEEE80211_IOC_BSSID, 0,
843 IEEE80211_ADDR_LEN, LLADDR(&sdl));
845 uint8_t zerobssid[IEEE80211_ADDR_LEN];
846 memset(zerobssid, 0, sizeof(zerobssid));
847 set80211(s, IEEE80211_IOC_BSSID, 0,
848 IEEE80211_ADDR_LEN, zerobssid);
853 getac(const char *ac)
855 if (strcasecmp(ac, "ac_be") == 0 || strcasecmp(ac, "be") == 0)
857 if (strcasecmp(ac, "ac_bk") == 0 || strcasecmp(ac, "bk") == 0)
859 if (strcasecmp(ac, "ac_vi") == 0 || strcasecmp(ac, "vi") == 0)
861 if (strcasecmp(ac, "ac_vo") == 0 || strcasecmp(ac, "vo") == 0)
863 errx(1, "unknown wme access class %s", ac);
867 DECL_CMD_FUNC2(set80211cwmin, ac, val)
869 set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val), getac(ac), NULL);
873 DECL_CMD_FUNC2(set80211cwmax, ac, val)
875 set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val), getac(ac), NULL);
879 DECL_CMD_FUNC2(set80211aifs, ac, val)
881 set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val), getac(ac), NULL);
885 DECL_CMD_FUNC2(set80211txoplimit, ac, val)
887 set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), getac(ac), NULL);
891 DECL_CMD_FUNC(set80211acm, ac, d)
893 set80211(s, IEEE80211_IOC_WME_ACM, 1, getac(ac), NULL);
896 DECL_CMD_FUNC(set80211noacm, ac, d)
898 set80211(s, IEEE80211_IOC_WME_ACM, 0, getac(ac), NULL);
902 DECL_CMD_FUNC(set80211ackpolicy, ac, d)
904 set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 1, getac(ac), NULL);
907 DECL_CMD_FUNC(set80211noackpolicy, ac, d)
909 set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 0, getac(ac), NULL);
913 DECL_CMD_FUNC2(set80211bsscwmin, ac, val)
915 set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val),
916 getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
920 DECL_CMD_FUNC2(set80211bsscwmax, ac, val)
922 set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val),
923 getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
927 DECL_CMD_FUNC2(set80211bssaifs, ac, val)
929 set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val),
930 getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
934 DECL_CMD_FUNC2(set80211bsstxoplimit, ac, val)
936 set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val),
937 getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
941 DECL_CMD_FUNC(set80211dtimperiod, val, d)
943 set80211(s, IEEE80211_IOC_DTIM_PERIOD, atoi(val), 0, NULL);
947 DECL_CMD_FUNC(set80211bintval, val, d)
949 set80211(s, IEEE80211_IOC_BEACON_INTERVAL, atoi(val), 0, NULL);
953 set80211macmac(int s, int op, const char *val)
956 struct sockaddr_dl sdl;
958 temp = malloc(strlen(val) + 2); /* ':' and '\0' */
960 errx(1, "malloc failed");
962 strcpy(temp + 1, val);
963 sdl.sdl_len = sizeof(sdl);
964 link_addr(temp, &sdl);
966 if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
967 errx(1, "malformed link-level address");
968 set80211(s, op, 0, IEEE80211_ADDR_LEN, LLADDR(&sdl));
972 DECL_CMD_FUNC(set80211addmac, val, d)
974 set80211macmac(s, IEEE80211_IOC_ADDMAC, val);
978 DECL_CMD_FUNC(set80211delmac, val, d)
980 set80211macmac(s, IEEE80211_IOC_DELMAC, val);
984 DECL_CMD_FUNC(set80211kickmac, val, d)
987 struct sockaddr_dl sdl;
988 struct ieee80211req_mlme mlme;
990 temp = malloc(strlen(val) + 2); /* ':' and '\0' */
992 errx(1, "malloc failed");
994 strcpy(temp + 1, val);
995 sdl.sdl_len = sizeof(sdl);
996 link_addr(temp, &sdl);
998 if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
999 errx(1, "malformed link-level address");
1000 memset(&mlme, 0, sizeof(mlme));
1001 mlme.im_op = IEEE80211_MLME_DEAUTH;
1002 mlme.im_reason = IEEE80211_REASON_AUTH_EXPIRE;
1003 memcpy(mlme.im_macaddr, LLADDR(&sdl), IEEE80211_ADDR_LEN);
1004 set80211(s, IEEE80211_IOC_MLME, 0, sizeof(mlme), &mlme);
1008 DECL_CMD_FUNC(set80211maccmd, val, d)
1010 set80211(s, IEEE80211_IOC_MACCMD, d, 0, NULL);
1014 set80211pureg(const char *val, int d, int s, const struct afswtch *rafp)
1016 set80211(s, IEEE80211_IOC_PUREG, d, 0, NULL);
1020 set80211bgscan(const char *val, int d, int s, const struct afswtch *rafp)
1022 set80211(s, IEEE80211_IOC_BGSCAN, d, 0, NULL);
1026 DECL_CMD_FUNC(set80211bgscanidle, val, d)
1028 set80211(s, IEEE80211_IOC_BGSCAN_IDLE, atoi(val), 0, NULL);
1032 DECL_CMD_FUNC(set80211bgscanintvl, val, d)
1034 set80211(s, IEEE80211_IOC_BGSCAN_INTERVAL, atoi(val), 0, NULL);
1038 DECL_CMD_FUNC(set80211scanvalid, val, d)
1040 set80211(s, IEEE80211_IOC_SCANVALID, atoi(val), 0, NULL);
1044 DECL_CMD_FUNC(set80211roamrssi11a, val, d)
1046 set80211(s, IEEE80211_IOC_ROAM_RSSI_11A, atoi(val), 0, NULL);
1050 DECL_CMD_FUNC(set80211roamrssi11b, val, d)
1052 set80211(s, IEEE80211_IOC_ROAM_RSSI_11B, atoi(val), 0, NULL);
1056 DECL_CMD_FUNC(set80211roamrssi11g, val, d)
1058 set80211(s, IEEE80211_IOC_ROAM_RSSI_11G, atoi(val), 0, NULL);
1062 DECL_CMD_FUNC(set80211roamrate11a, val, d)
1064 set80211(s, IEEE80211_IOC_ROAM_RATE_11A, 2*atoi(val), 0, NULL);
1068 DECL_CMD_FUNC(set80211roamrate11b, val, d)
1070 set80211(s, IEEE80211_IOC_ROAM_RATE_11B, 2*atoi(val), 0, NULL);
1074 DECL_CMD_FUNC(set80211roamrate11g, val, d)
1076 set80211(s, IEEE80211_IOC_ROAM_RATE_11G, 2*atoi(val), 0, NULL);
1080 DECL_CMD_FUNC(set80211mcastrate, val, d)
1082 set80211(s, IEEE80211_IOC_MCAST_RATE, 2*atoi(val), 0, NULL);
1086 DECL_CMD_FUNC(set80211fragthreshold, val, d)
1088 set80211(s, IEEE80211_IOC_FRAGTHRESHOLD,
1089 isundefarg(val) ? IEEE80211_FRAG_MAX : atoi(val), 0, NULL);
1093 DECL_CMD_FUNC(set80211bmissthreshold, val, d)
1095 set80211(s, IEEE80211_IOC_BMISSTHRESHOLD,
1096 isundefarg(val) ? IEEE80211_HWBMISS_MAX : atoi(val), 0, NULL);
1100 set80211burst(const char *val, int d, int s, const struct afswtch *rafp)
1102 set80211(s, IEEE80211_IOC_BURST, d, 0, NULL);
1106 set80211doth(const char *val, int d, int s, const struct afswtch *rafp)
1108 set80211(s, IEEE80211_IOC_DOTH, d, 0, NULL);
1112 set80211shortgi(const char *val, int d, int s, const struct afswtch *rafp)
1114 set80211(s, IEEE80211_IOC_SHORTGI,
1115 d ? (IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40) : 0,
1120 set80211ampdu(const char *val, int d, int s, const struct afswtch *rafp)
1124 if (get80211val(s, IEEE80211_IOC_AMPDU, &du) < 0)
1125 errx(-1, "cannot get AMPDU setting");
1131 set80211(s, IEEE80211_IOC_AMPDU, ampdu, 0, NULL);
1135 DECL_CMD_FUNC(set80211ampdulimit, val, d)
1139 switch (atoi(val)) {
1142 v = IEEE80211_HTCAP_MAXRXAMPDU_8K;
1146 v = IEEE80211_HTCAP_MAXRXAMPDU_16K;
1150 v = IEEE80211_HTCAP_MAXRXAMPDU_32K;
1154 v = IEEE80211_HTCAP_MAXRXAMPDU_64K;
1157 errx(-1, "invalid A-MPDU limit %s", val);
1159 set80211(s, IEEE80211_IOC_AMPDU_LIMIT, v, 0, NULL);
1163 DECL_CMD_FUNC(set80211ampdudensity, val, d)
1168 v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1169 else switch ((int)(atof(val)*4)) {
1171 v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1174 v = IEEE80211_HTCAP_MPDUDENSITY_025;
1177 v = IEEE80211_HTCAP_MPDUDENSITY_05;
1180 v = IEEE80211_HTCAP_MPDUDENSITY_1;
1183 v = IEEE80211_HTCAP_MPDUDENSITY_2;
1186 v = IEEE80211_HTCAP_MPDUDENSITY_4;
1189 v = IEEE80211_HTCAP_MPDUDENSITY_8;
1192 v = IEEE80211_HTCAP_MPDUDENSITY_16;
1195 errx(-1, "invalid A-MPDU density %s", val);
1197 set80211(s, IEEE80211_IOC_AMPDU_DENSITY, v, 0, NULL);
1201 set80211amsdu(const char *val, int d, int s, const struct afswtch *rafp)
1205 if (get80211val(s, IEEE80211_IOC_AMSDU, &amsdu) < 0)
1206 errx(-1, "cannot get AMSDU setting");
1212 set80211(s, IEEE80211_IOC_AMSDU, amsdu, 0, NULL);
1216 DECL_CMD_FUNC(set80211amsdulimit, val, d)
1218 set80211(s, IEEE80211_IOC_AMSDU_LIMIT, atoi(val), 0, NULL);
1222 set80211puren(const char *val, int d, int s, const struct afswtch *rafp)
1224 set80211(s, IEEE80211_IOC_PUREN, d, 0, NULL);
1228 set80211htcompat(const char *val, int d, int s, const struct afswtch *rafp)
1230 set80211(s, IEEE80211_IOC_HTCOMPAT, d, 0, NULL);
1234 set80211htconf(const char *val, int d, int s, const struct afswtch *rafp)
1236 set80211(s, IEEE80211_IOC_HTCONF, d, 0, NULL);
1241 set80211inact(const char *val, int d, int s, const struct afswtch *rafp)
1243 set80211(s, IEEE80211_IOC_INACTIVITY, d, 0, NULL);
1259 if (spacer != '\t') {
1263 col = 8; /* 8-col tab */
1267 LINE_CHECK(const char *fmt, ...)
1274 n = vsnprintf(buf+1, sizeof(buf)-1, fmt, ap);
1287 getmaxrate(const uint8_t rates[15], uint8_t nrates)
1289 int i, maxrate = -1;
1291 for (i = 0; i < nrates; i++) {
1292 int rate = rates[i] & IEEE80211_RATE_VAL;
1300 getcaps(int capinfo)
1302 static char capstring[32];
1303 char *cp = capstring;
1305 if (capinfo & IEEE80211_CAPINFO_ESS)
1307 if (capinfo & IEEE80211_CAPINFO_IBSS)
1309 if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
1311 if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
1313 if (capinfo & IEEE80211_CAPINFO_PRIVACY)
1315 if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
1317 if (capinfo & IEEE80211_CAPINFO_PBCC)
1319 if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
1321 if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
1323 if (capinfo & IEEE80211_CAPINFO_RSN)
1325 if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
1334 /* XXX need these publicly defined or similar */
1335 #define IEEE80211_NODE_AUTH 0x0001 /* authorized for data */
1336 #define IEEE80211_NODE_QOS 0x0002 /* QoS enabled */
1337 #define IEEE80211_NODE_ERP 0x0004 /* ERP enabled */
1338 #define IEEE80211_NODE_PWR_MGT 0x0010 /* power save mode enabled */
1339 #define IEEE80211_NODE_HT 0x0040 /* HT enabled */
1340 #define IEEE80211_NODE_HTCOMPAT 0x0080 /* HT setup w/ vendor OUI's */
1341 #define IEEE80211_NODE_WPS 0x0100 /* WPS association */
1342 #define IEEE80211_NODE_TSN 0x0200 /* TSN association */
1344 static char flagstring[32];
1345 char *cp = flagstring;
1347 if (flags & IEEE80211_NODE_AUTH)
1349 if (flags & IEEE80211_NODE_QOS)
1351 if (flags & IEEE80211_NODE_ERP)
1353 if (flags & IEEE80211_NODE_PWR_MGT)
1355 if (flags & IEEE80211_NODE_HT) {
1357 if (flags & IEEE80211_NODE_HTCOMPAT)
1360 if (flags & IEEE80211_NODE_WPS)
1362 if (flags & IEEE80211_NODE_TSN)
1366 #undef IEEE80211_NODE_TSN
1367 #undef IEEE80211_NODE_WPS
1368 #undef IEEE80211_NODE_HTCOMPAT
1369 #undef IEEE80211_NODE_HT
1370 #undef IEEE80211_NODE_AUTH
1371 #undef IEEE80211_NODE_QOS
1372 #undef IEEE80211_NODE_ERP
1373 #undef IEEE80211_NODE_PWR_MGT
1377 printie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
1381 maxlen -= strlen(tag)+2;
1382 if (2*ielen > maxlen)
1385 for (; ielen > 0; ie++, ielen--) {
1388 printf("%02x", *ie);
1396 #define LE_READ_2(p) \
1398 ((((const u_int8_t *)(p))[0] ) | \
1399 (((const u_int8_t *)(p))[1] << 8)))
1400 #define LE_READ_4(p) \
1402 ((((const u_int8_t *)(p))[0] ) | \
1403 (((const u_int8_t *)(p))[1] << 8) | \
1404 (((const u_int8_t *)(p))[2] << 16) | \
1405 (((const u_int8_t *)(p))[3] << 24)))
1408 * NB: The decoding routines assume a properly formatted ie
1409 * which should be safe as the kernel only retains them
1414 printwmeparam(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
1416 #define MS(_v, _f) (((_v) & _f) >> _f##_S)
1417 static const char *acnames[] = { "BE", "BK", "VO", "VI" };
1418 const struct ieee80211_wme_param *wme =
1419 (const struct ieee80211_wme_param *) ie;
1425 printf("<qosinfo 0x%x", wme->param_qosInfo);
1426 ie += offsetof(struct ieee80211_wme_param, params_acParams);
1427 for (i = 0; i < WME_NUM_AC; i++) {
1428 const struct ieee80211_wme_acparams *ac =
1429 &wme->params_acParams[i];
1431 printf(" %s[%saifsn %u cwmin %u cwmax %u txop %u]"
1433 , MS(ac->acp_aci_aifsn, WME_PARAM_ACM) ? "acm " : ""
1434 , MS(ac->acp_aci_aifsn, WME_PARAM_AIFSN)
1435 , MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMIN)
1436 , MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMAX)
1437 , LE_READ_2(&ac->acp_txop)
1445 printwmeinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
1449 const struct ieee80211_wme_info *wme =
1450 (const struct ieee80211_wme_info *) ie;
1451 printf("<version 0x%x info 0x%x>",
1452 wme->wme_version, wme->wme_info);
1457 printhtcap(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
1461 const struct ieee80211_ie_htcap *htcap =
1462 (const struct ieee80211_ie_htcap *) ie;
1466 printf("<cap 0x%x param 0x%x",
1467 LE_READ_2(&htcap->hc_cap), htcap->hc_param);
1470 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
1471 if (isset(htcap->hc_mcsset, i)) {
1472 for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
1473 if (isclr(htcap->hc_mcsset, j))
1477 printf("%s%u", sep, i);
1479 printf("%s%u-%u", sep, i, j);
1483 printf("] extcap 0x%x txbf 0x%x antenna 0x%x>",
1484 LE_READ_2(&htcap->hc_extcap),
1485 LE_READ_4(&htcap->hc_txbf),
1491 printhtinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
1495 const struct ieee80211_ie_htinfo *htinfo =
1496 (const struct ieee80211_ie_htinfo *) ie;
1500 printf("<ctl %u, %x,%x,%x,%x", htinfo->hi_ctrlchannel,
1501 htinfo->hi_byte1, htinfo->hi_byte2, htinfo->hi_byte3,
1502 LE_READ_2(&htinfo->hi_byte45));
1503 printf(" basicmcs[");
1505 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
1506 if (isset(htinfo->hi_basicmcsset, i)) {
1507 for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
1508 if (isclr(htinfo->hi_basicmcsset, j))
1512 printf("%s%u", sep, i);
1514 printf("%s%u-%u", sep, i, j);
1523 printathie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
1528 const struct ieee80211_ath_ie *ath =
1529 (const struct ieee80211_ath_ie *)ie;
1532 if (ath->ath_capability & ATHEROS_CAP_TURBO_PRIME)
1534 if (ath->ath_capability & ATHEROS_CAP_COMPRESSION)
1536 if (ath->ath_capability & ATHEROS_CAP_FAST_FRAME)
1538 if (ath->ath_capability & ATHEROS_CAP_XR)
1540 if (ath->ath_capability & ATHEROS_CAP_AR)
1542 if (ath->ath_capability & ATHEROS_CAP_BURST)
1544 if (ath->ath_capability & ATHEROS_CAP_WME)
1546 if (ath->ath_capability & ATHEROS_CAP_BOOST)
1548 printf("0x%x>", LE_READ_2(ath->ath_defkeyix));
1553 wpa_cipher(const u_int8_t *sel)
1555 #define WPA_SEL(x) (((x)<<24)|WPA_OUI)
1556 u_int32_t w = LE_READ_4(sel);
1559 case WPA_SEL(WPA_CSE_NULL):
1561 case WPA_SEL(WPA_CSE_WEP40):
1563 case WPA_SEL(WPA_CSE_WEP104):
1565 case WPA_SEL(WPA_CSE_TKIP):
1567 case WPA_SEL(WPA_CSE_CCMP):
1570 return "?"; /* NB: so 1<< is discarded */
1575 wpa_keymgmt(const u_int8_t *sel)
1577 #define WPA_SEL(x) (((x)<<24)|WPA_OUI)
1578 u_int32_t w = LE_READ_4(sel);
1581 case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1582 return "8021X-UNSPEC";
1583 case WPA_SEL(WPA_ASE_8021X_PSK):
1585 case WPA_SEL(WPA_ASE_NONE):
1593 printwpaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
1595 u_int8_t len = ie[1];
1602 ie += 6, len -= 4; /* NB: len is payload only */
1604 printf("<v%u", LE_READ_2(ie));
1607 printf(" mc:%s", wpa_cipher(ie));
1610 /* unicast ciphers */
1614 for (; n > 0; n--) {
1615 printf("%s%s", sep, wpa_cipher(ie));
1620 /* key management algorithms */
1624 for (; n > 0; n--) {
1625 printf("%s%s", sep, wpa_keymgmt(ie));
1630 if (len > 2) /* optional capabilities */
1631 printf(", caps 0x%x", LE_READ_2(ie));
1637 rsn_cipher(const u_int8_t *sel)
1639 #define RSN_SEL(x) (((x)<<24)|RSN_OUI)
1640 u_int32_t w = LE_READ_4(sel);
1643 case RSN_SEL(RSN_CSE_NULL):
1645 case RSN_SEL(RSN_CSE_WEP40):
1647 case RSN_SEL(RSN_CSE_WEP104):
1649 case RSN_SEL(RSN_CSE_TKIP):
1651 case RSN_SEL(RSN_CSE_CCMP):
1653 case RSN_SEL(RSN_CSE_WRAP):
1661 rsn_keymgmt(const u_int8_t *sel)
1663 #define RSN_SEL(x) (((x)<<24)|RSN_OUI)
1664 u_int32_t w = LE_READ_4(sel);
1667 case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1668 return "8021X-UNSPEC";
1669 case RSN_SEL(RSN_ASE_8021X_PSK):
1671 case RSN_SEL(RSN_ASE_NONE):
1679 printrsnie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
1686 ie += 2, ielen -= 2;
1688 printf("<v%u", LE_READ_2(ie));
1689 ie += 2, ielen -= 2;
1691 printf(" mc:%s", rsn_cipher(ie));
1692 ie += 4, ielen -= 4;
1694 /* unicast ciphers */
1696 ie += 2, ielen -= 2;
1698 for (; n > 0; n--) {
1699 printf("%s%s", sep, rsn_cipher(ie));
1700 ie += 4, ielen -= 4;
1704 /* key management algorithms */
1706 ie += 2, ielen -= 2;
1708 for (; n > 0; n--) {
1709 printf("%s%s", sep, rsn_keymgmt(ie));
1710 ie += 4, ielen -= 4;
1714 if (ielen > 2) /* optional capabilities */
1715 printf(", caps 0x%x", LE_READ_2(ie));
1721 /* XXX move to a public include file */
1722 #define IEEE80211_WPS_DEV_PASS_ID 0x1012
1723 #define IEEE80211_WPS_SELECTED_REG 0x1041
1724 #define IEEE80211_WPS_SETUP_STATE 0x1044
1725 #define IEEE80211_WPS_UUID_E 0x1047
1726 #define IEEE80211_WPS_VERSION 0x104a
1728 #define BE_READ_2(p) \
1730 ((((const u_int8_t *)(p))[1] ) | \
1731 (((const u_int8_t *)(p))[0] << 8)))
1734 printwpsie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
1736 #define N(a) (sizeof(a) / sizeof(a[0]))
1737 u_int8_t len = ie[1];
1741 static const char *dev_pass_id[] = {
1742 "D", /* Default (PIN) */
1743 "U", /* User-specified */
1744 "M", /* Machine-specified */
1746 "P", /* PushButton */
1747 "R" /* Registrar-specified */
1751 ie +=6, len -= 4; /* NB: len is payload only */
1753 /* WPS IE in Beacon and Probe Resp frames have different fields */
1756 uint16_t tlv_type = BE_READ_2(ie);
1757 uint16_t tlv_len = BE_READ_2(ie + 2);
1762 case IEEE80211_WPS_VERSION:
1763 printf("v:%d.%d", *ie >> 4, *ie & 0xf);
1765 case IEEE80211_WPS_SETUP_STATE:
1766 /* Only 1 and 2 are valid */
1767 if (*ie == 0 || *ie >= 3)
1770 printf(" st:%s", *ie == 1 ? "N" : "C");
1772 case IEEE80211_WPS_SELECTED_REG:
1773 printf(" sel:%s", *ie ? "T" : "F");
1775 case IEEE80211_WPS_DEV_PASS_ID:
1777 if (n < N(dev_pass_id))
1778 printf(" dpi:%s", dev_pass_id[n]);
1780 case IEEE80211_WPS_UUID_E:
1782 for (n = 0; n < (tlv_len - 1); n++)
1783 printf("%02x-", ie[n]);
1784 printf("%02x", ie[n]);
1787 ie += tlv_len, len -= tlv_len;
1795 * Copy the ssid string contents into buf, truncating to fit. If the
1796 * ssid is entirely printable then just copy intact. Otherwise convert
1797 * to hexadecimal. If the result is truncated then replace the last
1798 * three characters with "...".
1801 copy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
1807 if (essid_len > bufsize)
1811 /* determine printable or not */
1812 for (i = 0, p = essid; i < maxlen; i++, p++) {
1813 if (*p < ' ' || *p > 0x7e)
1816 if (i != maxlen) { /* not printable, print as hex */
1819 strlcpy(buf, "0x", bufsize);
1822 for (i = 0; i < maxlen && bufsize >= 2; i++) {
1823 sprintf(&buf[2+2*i], "%02x", p[i]);
1827 memcpy(&buf[2+2*i-3], "...", 3);
1828 } else { /* printable, truncate as needed */
1829 memcpy(buf, essid, maxlen);
1830 if (maxlen != essid_len)
1831 memcpy(&buf[maxlen-3], "...", 3);
1837 printssid(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
1839 char ssid[2*IEEE80211_NWID_LEN+1];
1841 printf("%s<%.*s>", tag, copy_essid(ssid, maxlen, ie+2, ie[1]), ssid);
1845 printrates(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
1852 for (i = 2; i < ielen; i++) {
1853 printf("%s%s%d", sep,
1854 ie[i] & IEEE80211_RATE_BASIC ? "B" : "",
1855 ie[i] & IEEE80211_RATE_VAL);
1862 printcountry(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
1864 const struct ieee80211_country_ie *cie =
1865 (const struct ieee80211_country_ie *) ie;
1866 int i, nbands, schan, nchan;
1868 printf("%s<%c%c%c", tag, cie->cc[0], cie->cc[1], cie->cc[2]);
1869 nbands = (cie->len - 3) / sizeof(cie->band[0]);
1870 for (i = 0; i < nbands; i++) {
1871 schan = cie->band[i].schan;
1872 nchan = cie->band[i].nchan;
1874 printf(" %u-%u,%u", schan, schan + nchan-1,
1875 cie->band[i].maxtxpwr);
1877 printf(" %u,%u", schan, cie->band[i].maxtxpwr);
1882 /* unaligned little endian access */
1883 #define LE_READ_4(p) \
1885 ((((const u_int8_t *)(p))[0] ) | \
1886 (((const u_int8_t *)(p))[1] << 8) | \
1887 (((const u_int8_t *)(p))[2] << 16) | \
1888 (((const u_int8_t *)(p))[3] << 24)))
1891 iswpaoui(const u_int8_t *frm)
1893 return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
1897 iswmeinfo(const u_int8_t *frm)
1899 return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
1900 frm[6] == WME_INFO_OUI_SUBTYPE;
1904 iswmeparam(const u_int8_t *frm)
1906 return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
1907 frm[6] == WME_PARAM_OUI_SUBTYPE;
1911 isatherosoui(const u_int8_t *frm)
1913 return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
1917 iswpsoui(const uint8_t *frm)
1919 return frm[1] > 3 && LE_READ_4(frm+2) == ((WPS_OUI_TYPE<<24)|WPA_OUI);
1926 case IEEE80211_ELEMID_FHPARMS: return " FHPARMS";
1927 case IEEE80211_ELEMID_CFPARMS: return " CFPARMS";
1928 case IEEE80211_ELEMID_TIM: return " TIM";
1929 case IEEE80211_ELEMID_IBSSPARMS:return " IBSSPARMS";
1930 case IEEE80211_ELEMID_CHALLENGE:return " CHALLENGE";
1931 case IEEE80211_ELEMID_PWRCNSTR: return " PWRCNSTR";
1932 case IEEE80211_ELEMID_PWRCAP: return " PWRCAP";
1933 case IEEE80211_ELEMID_TPCREQ: return " TPCREQ";
1934 case IEEE80211_ELEMID_TPCREP: return " TPCREP";
1935 case IEEE80211_ELEMID_SUPPCHAN: return " SUPPCHAN";
1936 case IEEE80211_ELEMID_CHANSWITCHANN:return " CSA";
1937 case IEEE80211_ELEMID_MEASREQ: return " MEASREQ";
1938 case IEEE80211_ELEMID_MEASREP: return " MEASREP";
1939 case IEEE80211_ELEMID_QUIET: return " QUIET";
1940 case IEEE80211_ELEMID_IBSSDFS: return " IBSSDFS";
1941 case IEEE80211_ELEMID_TPC: return " TPC";
1942 case IEEE80211_ELEMID_CCKM: return " CCKM";
1948 printies(const u_int8_t *vp, int ielen, int maxcols)
1952 case IEEE80211_ELEMID_SSID:
1954 printssid(" SSID", vp, 2+vp[1], maxcols);
1956 case IEEE80211_ELEMID_RATES:
1957 case IEEE80211_ELEMID_XRATES:
1959 printrates(vp[0] == IEEE80211_ELEMID_RATES ?
1960 " RATES" : " XRATES", vp, 2+vp[1], maxcols);
1962 case IEEE80211_ELEMID_DSPARMS:
1964 printf(" DSPARMS<%u>", vp[2]);
1966 case IEEE80211_ELEMID_COUNTRY:
1968 printcountry(" COUNTRY", vp, 2+vp[1], maxcols);
1970 case IEEE80211_ELEMID_ERP:
1972 printf(" ERP<0x%x>", vp[2]);
1974 case IEEE80211_ELEMID_VENDOR:
1976 printwpaie(" WPA", vp, 2+vp[1], maxcols);
1977 else if (iswmeinfo(vp))
1978 printwmeinfo(" WME", vp, 2+vp[1], maxcols);
1979 else if (iswmeparam(vp))
1980 printwmeparam(" WME", vp, 2+vp[1], maxcols);
1981 else if (isatherosoui(vp))
1982 printathie(" ATH", vp, 2+vp[1], maxcols);
1983 else if (iswpsoui(vp))
1984 printwpsie(" WPS", vp, 2+vp[1], maxcols);
1986 printie(" VEN", vp, 2+vp[1], maxcols);
1988 case IEEE80211_ELEMID_RSN:
1989 printrsnie(" RSN", vp, 2+vp[1], maxcols);
1991 case IEEE80211_ELEMID_HTCAP:
1992 printhtcap(" HTCAP", vp, 2+vp[1], maxcols);
1994 case IEEE80211_ELEMID_HTINFO:
1996 printhtinfo(" HTINFO", vp, 2+vp[1], maxcols);
2000 printie(iename(vp[0]), vp, 2+vp[1], maxcols);
2011 uint8_t buf[24*1024];
2012 char ssid[IEEE80211_NWID_LEN+1];
2016 if (get80211len(s, IEEE80211_IOC_SCAN_RESULTS, buf, sizeof(buf), &len) < 0)
2017 errx(1, "unable to get scan results");
2018 if (len < sizeof(struct ieee80211req_scan_result))
2023 ssidmax = verbose ? IEEE80211_NWID_LEN : 14;
2024 printf("%-*.*s %-17.17s %4s %4s %-7s %3s %4s\n"
2025 , ssidmax, ssidmax, "SSID"
2035 const struct ieee80211req_scan_result *sr;
2038 sr = (const struct ieee80211req_scan_result *) cp;
2039 vp = cp + sr->isr_ie_off;
2040 printf("%-*.*s %s %3d %3dM %3d:%-3d %3d %-4.4s"
2042 , copy_essid(ssid, ssidmax, vp, sr->isr_ssid_len)
2044 , ether_ntoa((const struct ether_addr *) sr->isr_bssid)
2045 , ieee80211_mhz2ieee(sr->isr_freq, sr->isr_flags)
2046 , getmaxrate(sr->isr_rates, sr->isr_nrates)
2047 , (sr->isr_rssi/2)+sr->isr_noise, sr->isr_noise
2049 , getcaps(sr->isr_capinfo)
2051 printies(vp + sr->isr_ssid_len, sr->isr_ie_len, 24);
2053 cp += sr->isr_len, len -= sr->isr_len;
2054 } while (len >= sizeof(struct ieee80211req_scan_result));
2057 #include <net80211/ieee80211_freebsd.h>
2060 scan_and_wait(int s)
2062 struct ieee80211req ireq;
2065 sroute = socket(PF_ROUTE, SOCK_RAW, 0);
2067 perror("socket(PF_ROUTE,SOCK_RAW)");
2070 (void) memset(&ireq, 0, sizeof(ireq));
2071 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
2072 ireq.i_type = IEEE80211_IOC_SCAN_REQ;
2073 /* NB: only root can trigger a scan so ignore errors */
2074 if (ioctl(s, SIOCS80211, &ireq) >= 0) {
2076 struct if_announcemsghdr *ifan;
2077 struct rt_msghdr *rtm;
2080 if (read(sroute, buf, sizeof(buf)) < 0) {
2081 perror("read(PF_ROUTE)");
2084 rtm = (struct rt_msghdr *) buf;
2085 if (rtm->rtm_version != RTM_VERSION)
2087 ifan = (struct if_announcemsghdr *) rtm;
2088 } while (rtm->rtm_type != RTM_IEEE80211 ||
2089 ifan->ifan_what != RTM_IEEE80211_SCAN);
2095 DECL_CMD_FUNC(set80211scan, val, d)
2101 static enum ieee80211_opmode get80211opmode(int s);
2104 gettxseq(const struct ieee80211req_sta_info *si)
2106 #define IEEE80211_NODE_QOS 0x0002 /* QoS enabled */
2110 if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
2111 return si->isi_txseqs[0];
2112 /* XXX not right but usually what folks want */
2114 for (i = 0; i < IEEE80211_TID_SIZE; i++)
2115 if (si->isi_txseqs[i] > txseq)
2116 txseq = si->isi_txseqs[i];
2118 #undef IEEE80211_NODE_QOS
2122 getrxseq(const struct ieee80211req_sta_info *si)
2124 #define IEEE80211_NODE_QOS 0x0002 /* QoS enabled */
2128 if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
2129 return si->isi_rxseqs[0];
2130 /* XXX not right but usually what folks want */
2132 for (i = 0; i < IEEE80211_TID_SIZE; i++)
2133 if (si->isi_rxseqs[i] > rxseq)
2134 rxseq = si->isi_rxseqs[i];
2136 #undef IEEE80211_NODE_QOS
2140 gettxrate(const struct ieee80211req_sta_info *si)
2142 int txrate = si->isi_txrate;
2144 if (txrate & 0x80) {
2145 txrate = htrates[txrate & 0xf];
2146 /* NB: could bump this more based on short gi */
2147 return si->isi_flags & IEEE80211_CHAN_HT40 ?
2148 txrate : txrate / 2;
2150 return (si->isi_rates[txrate] & IEEE80211_RATE_VAL) / 2;
2154 list_stations(int s)
2157 struct ieee80211req_sta_req req;
2158 uint8_t buf[24*1024];
2160 enum ieee80211_opmode opmode = get80211opmode(s);
2164 /* broadcast address =>'s get all stations */
2165 (void) memset(u.req.is_u.macaddr, 0xff, IEEE80211_ADDR_LEN);
2166 if (opmode == IEEE80211_M_STA) {
2168 * Get information about the associated AP.
2170 (void) get80211(s, IEEE80211_IOC_BSSID,
2171 u.req.is_u.macaddr, IEEE80211_ADDR_LEN);
2173 if (get80211len(s, IEEE80211_IOC_STA_INFO, &u, sizeof(u), &len) < 0)
2174 errx(1, "unable to get station information");
2175 if (len < sizeof(struct ieee80211req_sta_info))
2180 printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %4s\n"
2192 cp = (const uint8_t *) u.req.info;
2194 const struct ieee80211req_sta_info *si;
2196 si = (const struct ieee80211req_sta_info *) cp;
2197 if (si->isi_len < sizeof(*si))
2199 printf("%s %4u %4d %3dM %3.1f %4d %6d %6d %-4.4s %-4.4s"
2200 , ether_ntoa((const struct ether_addr*) si->isi_macaddr)
2201 , IEEE80211_AID(si->isi_associd)
2202 , ieee80211_mhz2ieee(si->isi_freq, si->isi_flags)
2208 , getcaps(si->isi_capinfo)
2209 , getflags(si->isi_state)
2211 printies(cp + si->isi_ie_off, si->isi_ie_len, 24);
2213 cp += si->isi_len, len -= si->isi_len;
2214 } while (len >= sizeof(struct ieee80211req_sta_info));
2218 get_chaninfo(const struct ieee80211_channel *c, int precise,
2219 char buf[], size_t bsize)
2222 if (IEEE80211_IS_CHAN_FHSS(c))
2223 strlcat(buf, " FHSS", bsize);
2224 if (IEEE80211_IS_CHAN_A(c)) {
2225 if (IEEE80211_IS_CHAN_HALF(c))
2226 strlcat(buf, " 11a/10Mhz", bsize);
2227 else if (IEEE80211_IS_CHAN_QUARTER(c))
2228 strlcat(buf, " 11a/5Mhz", bsize);
2230 strlcat(buf, " 11a", bsize);
2232 if (IEEE80211_IS_CHAN_ANYG(c)) {
2233 if (IEEE80211_IS_CHAN_HALF(c))
2234 strlcat(buf, " 11g/10Mhz", bsize);
2235 else if (IEEE80211_IS_CHAN_QUARTER(c))
2236 strlcat(buf, " 11g/5Mhz", bsize);
2238 strlcat(buf, " 11g", bsize);
2239 } else if (IEEE80211_IS_CHAN_B(c))
2240 strlcat(buf, " 11b", bsize);
2241 if (IEEE80211_IS_CHAN_TURBO(c))
2242 strlcat(buf, " Turbo", bsize);
2244 if (IEEE80211_IS_CHAN_HT20(c))
2245 strlcat(buf, " ht/20", bsize);
2246 else if (IEEE80211_IS_CHAN_HT40D(c))
2247 strlcat(buf, " ht/40-", bsize);
2248 else if (IEEE80211_IS_CHAN_HT40U(c))
2249 strlcat(buf, " ht/40+", bsize);
2251 if (IEEE80211_IS_CHAN_HT(c))
2252 strlcat(buf, " ht", bsize);
2258 print_chaninfo(const struct ieee80211_channel *c, int verb)
2262 printf("Channel %3u : %u%c Mhz%-14.14s",
2263 ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
2264 IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
2265 get_chaninfo(c, verb, buf, sizeof(buf)));
2269 print_channels(int s, const struct ieee80211req_chaninfo *chans,
2270 int allchans, int verb)
2272 struct ieee80211req_chaninfo achans;
2273 uint8_t reported[IEEE80211_CHAN_BYTES];
2274 const struct ieee80211_channel *c;
2277 memset(&achans, 0, sizeof(achans));
2278 memset(reported, 0, sizeof(reported));
2280 struct ieee80211req_chanlist active;
2282 if (get80211(s, IEEE80211_IOC_CHANLIST, &active, sizeof(active)) < 0)
2283 errx(1, "unable to get active channel list");
2284 memset(&achans, 0, sizeof(achans));
2285 for (i = 0; i < chans->ic_nchans; i++) {
2286 c = &chans->ic_chans[i];
2287 if (!isset(active.ic_channels, c->ic_ieee))
2290 * Suppress compatible duplicates unless
2291 * verbose. The kernel gives us it's
2292 * complete channel list which has separate
2293 * entries for 11g/11b and 11a/turbo.
2295 if (isset(reported, c->ic_ieee) && !verb) {
2296 /* XXX we assume duplicates are adjacent */
2297 achans.ic_chans[achans.ic_nchans-1] = *c;
2299 achans.ic_chans[achans.ic_nchans++] = *c;
2300 setbit(reported, c->ic_ieee);
2304 for (i = 0; i < chans->ic_nchans; i++) {
2305 c = &chans->ic_chans[i];
2306 /* suppress duplicates as above */
2307 if (isset(reported, c->ic_ieee) && !verb) {
2308 /* XXX we assume duplicates are adjacent */
2309 achans.ic_chans[achans.ic_nchans-1] = *c;
2311 achans.ic_chans[achans.ic_nchans++] = *c;
2312 setbit(reported, c->ic_ieee);
2316 half = achans.ic_nchans / 2;
2317 if (achans.ic_nchans % 2)
2320 for (i = 0; i < achans.ic_nchans / 2; i++) {
2321 print_chaninfo(&achans.ic_chans[i], verb);
2322 print_chaninfo(&achans.ic_chans[half+i], verb);
2325 if (achans.ic_nchans % 2) {
2326 print_chaninfo(&achans.ic_chans[i], verb);
2332 list_channels(int s, int allchans)
2335 print_channels(s, &chaninfo, allchans, verbose);
2339 print_txpow(const struct ieee80211_channel *c)
2341 printf("Channel %3u : %u Mhz %3.1f reg %2d ",
2342 c->ic_ieee, c->ic_freq,
2343 c->ic_maxpower/2., c->ic_maxregpower);
2347 print_txpow_verbose(const struct ieee80211_channel *c)
2349 print_chaninfo(c, 1);
2350 printf("min %4.1f dBm max %3.1f dBm reg %2d dBm",
2351 c->ic_minpower/2., c->ic_maxpower/2., c->ic_maxregpower);
2352 /* indicate where regulatory cap limits power use */
2353 if (c->ic_maxpower > 2*c->ic_maxregpower)
2360 struct ieee80211req_chaninfo achans;
2361 uint8_t reported[IEEE80211_CHAN_BYTES];
2362 struct ieee80211_channel *c, *prev;
2366 memset(&achans, 0, sizeof(achans));
2367 memset(reported, 0, sizeof(reported));
2368 for (i = 0; i < chaninfo.ic_nchans; i++) {
2369 c = &chaninfo.ic_chans[i];
2370 /* suppress duplicates as above */
2371 if (isset(reported, c->ic_ieee) && !verbose) {
2372 /* XXX we assume duplicates are adjacent */
2373 prev = &achans.ic_chans[achans.ic_nchans-1];
2374 /* display highest power on channel */
2375 if (c->ic_maxpower > prev->ic_maxpower)
2378 achans.ic_chans[achans.ic_nchans++] = *c;
2379 setbit(reported, c->ic_ieee);
2383 half = achans.ic_nchans / 2;
2384 if (achans.ic_nchans % 2)
2387 for (i = 0; i < achans.ic_nchans / 2; i++) {
2388 print_txpow(&achans.ic_chans[i]);
2389 print_txpow(&achans.ic_chans[half+i]);
2392 if (achans.ic_nchans % 2) {
2393 print_txpow(&achans.ic_chans[i]);
2397 for (i = 0; i < achans.ic_nchans; i++) {
2398 print_txpow_verbose(&achans.ic_chans[i]);
2409 #define IEEE80211_C_BITS \
2410 "\020\1WEP\2TKIP\3AES\4AES_CCM\6CKIP\7FF\10TURBOP\11IBSS\12PMGT\13HOSTAP\14AHDEMO" \
2411 "\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE\21MONITOR\22TKIPMIC\30WPA1" \
2412 "\31WPA2\32BURST\33WME\34WDS\36BGSCAN\37TXFRAG"
2415 list_capabilities(int s)
2417 struct ieee80211req ireq;
2420 (void) memset(&ireq, 0, sizeof(ireq));
2421 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
2422 ireq.i_type = IEEE80211_IOC_DRIVER_CAPS;
2423 if (ioctl(s, SIOCG80211, &ireq) < 0)
2424 errx(1, "unable to get driver capabilities");
2425 caps = (((u_int16_t) ireq.i_val) << 16) | ((u_int16_t) ireq.i_len);
2426 printb(name, caps, IEEE80211_C_BITS);
2431 get80211wme(int s, int param, int ac, int *val)
2433 struct ieee80211req ireq;
2435 (void) memset(&ireq, 0, sizeof(ireq));
2436 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
2437 ireq.i_type = param;
2439 if (ioctl(s, SIOCG80211, &ireq) < 0) {
2440 warn("cannot get WME parameter %d, ac %d%s",
2441 param, ac & IEEE80211_WMEPARAM_VAL,
2442 ac & IEEE80211_WMEPARAM_BSS ? " (BSS)" : "");
2452 static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
2455 for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
2457 if (ac & IEEE80211_WMEPARAM_BSS)
2458 printf("\t%s", " ");
2460 printf("\t%s", acnames[ac]);
2462 /* show WME BSS parameters */
2463 if (get80211wme(s, IEEE80211_IOC_WME_CWMIN, ac, &val) != -1)
2464 printf(" cwmin %2u", val);
2465 if (get80211wme(s, IEEE80211_IOC_WME_CWMAX, ac, &val) != -1)
2466 printf(" cwmax %2u", val);
2467 if (get80211wme(s, IEEE80211_IOC_WME_AIFS, ac, &val) != -1)
2468 printf(" aifs %2u", val);
2469 if (get80211wme(s, IEEE80211_IOC_WME_TXOPLIMIT, ac, &val) != -1)
2470 printf(" txopLimit %3u", val);
2471 if (get80211wme(s, IEEE80211_IOC_WME_ACM, ac, &val) != -1) {
2478 if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
2479 if (get80211wme(s, IEEE80211_IOC_WME_ACKPOLICY, ac, &val) != -1) {
2487 if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
2488 ac |= IEEE80211_WMEPARAM_BSS;
2491 ac &= ~IEEE80211_WMEPARAM_BSS;
2496 printpolicy(int policy)
2499 case IEEE80211_MACCMD_POLICY_OPEN:
2500 printf("policy: open\n");
2502 case IEEE80211_MACCMD_POLICY_ALLOW:
2503 printf("policy: allow\n");
2505 case IEEE80211_MACCMD_POLICY_DENY:
2506 printf("policy: deny\n");
2509 printf("policy: unknown (%u)\n", policy);
2517 struct ieee80211req ireq;
2518 struct ieee80211req_maclist *acllist;
2519 int i, nacls, policy, len;
2523 (void) memset(&ireq, 0, sizeof(ireq));
2524 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
2525 ireq.i_type = IEEE80211_IOC_MACCMD;
2526 ireq.i_val = IEEE80211_MACCMD_POLICY;
2527 if (ioctl(s, SIOCG80211, &ireq) < 0) {
2528 if (errno == EINVAL) {
2529 printf("No acl policy loaded\n");
2532 err(1, "unable to get mac policy");
2534 policy = ireq.i_val;
2535 if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
2537 } else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
2539 } else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
2542 printf("policy: unknown (%u)\n", policy);
2545 if (verbose || c == '?')
2546 printpolicy(policy);
2548 ireq.i_val = IEEE80211_MACCMD_LIST;
2550 if (ioctl(s, SIOCG80211, &ireq) < 0)
2551 err(1, "unable to get mac acl list size");
2552 if (ireq.i_len == 0) { /* NB: no acls */
2553 if (!(verbose || c == '?'))
2554 printpolicy(policy);
2561 err(1, "out of memory for acl list");
2564 if (ioctl(s, SIOCG80211, &ireq) < 0)
2565 err(1, "unable to get mac acl list");
2566 nacls = len / sizeof(*acllist);
2567 acllist = (struct ieee80211req_maclist *) data;
2568 for (i = 0; i < nacls; i++)
2569 printf("%c%s\n", c, ether_ntoa(
2570 (const struct ether_addr *) acllist[i].ml_macaddr));
2575 DECL_CMD_FUNC(set80211list, arg, d)
2577 #define iseq(a,b) (strncasecmp(a,b,sizeof(b)-1) == 0)
2581 if (iseq(arg, "sta"))
2583 else if (iseq(arg, "scan") || iseq(arg, "ap"))
2585 else if (iseq(arg, "chan") || iseq(arg, "freq"))
2586 list_channels(s, 1);
2587 else if (iseq(arg, "active"))
2588 list_channels(s, 0);
2589 else if (iseq(arg, "keys"))
2591 else if (iseq(arg, "caps"))
2592 list_capabilities(s);
2593 else if (iseq(arg, "wme"))
2595 else if (iseq(arg, "mac"))
2597 else if (iseq(arg, "txpow"))
2600 errx(1, "Don't know how to list %s for %s", arg, name);
2604 static enum ieee80211_opmode
2605 get80211opmode(int s)
2607 struct ifmediareq ifmr;
2609 (void) memset(&ifmr, 0, sizeof(ifmr));
2610 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
2612 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
2613 if (ifmr.ifm_current & IFM_IEEE80211_ADHOC)
2614 return IEEE80211_M_IBSS; /* XXX ahdemo */
2615 if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
2616 return IEEE80211_M_HOSTAP;
2617 if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
2618 return IEEE80211_M_MONITOR;
2620 return IEEE80211_M_STA;
2625 printcipher(int s, struct ieee80211req *ireq, int keylenop)
2627 switch (ireq->i_val) {
2628 case IEEE80211_CIPHER_WEP:
2629 ireq->i_type = keylenop;
2630 if (ioctl(s, SIOCG80211, ireq) != -1)
2632 ireq->i_len <= 5 ? "40" :
2633 ireq->i_len <= 13 ? "104" : "128");
2637 case IEEE80211_CIPHER_TKIP:
2640 case IEEE80211_CIPHER_AES_OCB:
2643 case IEEE80211_CIPHER_AES_CCM:
2646 case IEEE80211_CIPHER_CKIP:
2649 case IEEE80211_CIPHER_NONE:
2653 printf("UNKNOWN (0x%x)", ireq->i_val);
2660 printkey(const struct ieee80211req_key *ik)
2662 static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
2663 int keylen = ik->ik_keylen;
2666 printcontents = printkeys &&
2667 (memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
2670 switch (ik->ik_type) {
2671 case IEEE80211_CIPHER_WEP:
2673 LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
2674 keylen <= 5 ? "40-bit" :
2675 keylen <= 13 ? "104-bit" : "128-bit");
2677 case IEEE80211_CIPHER_TKIP:
2679 keylen -= 128/8; /* ignore MIC for now */
2680 LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
2682 case IEEE80211_CIPHER_AES_OCB:
2683 LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
2685 case IEEE80211_CIPHER_AES_CCM:
2686 LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
2688 case IEEE80211_CIPHER_CKIP:
2689 LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
2691 case IEEE80211_CIPHER_NONE:
2692 LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
2695 LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
2696 ik->ik_type, ik->ik_keyix+1, 8*keylen);
2699 if (printcontents) {
2703 for (i = 0; i < keylen; i++)
2704 printf("%02x", ik->ik_keydata[i]);
2706 if (ik->ik_type != IEEE80211_CIPHER_WEP &&
2707 (ik->ik_keyrsc != 0 || verbose))
2708 printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
2709 if (ik->ik_type != IEEE80211_CIPHER_WEP &&
2710 (ik->ik_keytsc != 0 || verbose))
2711 printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
2712 if (ik->ik_flags != 0 && verbose) {
2713 const char *sep = " ";
2715 if (ik->ik_flags & IEEE80211_KEY_XMIT)
2716 printf("%stx", sep), sep = "+";
2717 if (ik->ik_flags & IEEE80211_KEY_RECV)
2718 printf("%srx", sep), sep = "+";
2719 if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
2720 printf("%sdef", sep), sep = "+";
2727 printrate(const char *tag, int v, int defrate, int defmcs)
2730 LINE_CHECK("%s 5.5", tag);
2731 else if (v & 0x80) {
2733 LINE_CHECK("%s %d", tag, v &~ 0x80);
2736 LINE_CHECK("%s %d", tag, v/2);
2741 getssid(int s, int ix, void *data, size_t len, int *plen)
2743 struct ieee80211req ireq;
2745 (void) memset(&ireq, 0, sizeof(ireq));
2746 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
2747 ireq.i_type = IEEE80211_IOC_SSID;
2751 if (ioctl(s, SIOCG80211, &ireq) < 0)
2758 printrssi(const char *tag, int rssi)
2761 LINE_CHECK("%s %u.5", tag, rssi/2);
2763 LINE_CHECK("%s %u", tag, rssi/2);
2767 ieee80211_status(int s)
2769 static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
2770 enum ieee80211_opmode opmode = get80211opmode(s);
2771 int i, num, wpa, wme, bgscan, bgscaninterval, val, len, wepmode;
2773 const struct ieee80211_channel *c;
2775 if (getssid(s, -1, data, sizeof(data), &len) < 0) {
2776 /* If we can't get the SSID, this isn't an 802.11 device. */
2781 * Invalidate cached state so printing status for multiple
2782 * if's doesn't reuse the first interfaces' cached state.
2787 if (get80211val(s, IEEE80211_IOC_NUMSSIDS, &num) < 0)
2791 for (i = 0; i < num; i++) {
2792 if (getssid(s, i, data, sizeof(data), &len) >= 0 && len > 0) {
2793 printf(" %d:", i + 1);
2794 print_string(data, len);
2798 print_string(data, len);
2801 if (c->ic_freq != IEEE80211_CHAN_ANY) {
2803 printf(" channel %d (%u Mhz%s)", c->ic_ieee, c->ic_freq,
2804 get_chaninfo(c, 1, buf, sizeof(buf)));
2806 printf(" channel UNDEF");
2808 if (get80211(s, IEEE80211_IOC_BSSID, data, IEEE80211_ADDR_LEN) >= 0 &&
2809 (memcmp(data, zerobssid, sizeof(zerobssid)) != 0 || verbose))
2810 printf(" bssid %s", ether_ntoa((struct ether_addr *)data));
2812 if (get80211len(s, IEEE80211_IOC_STATIONNAME, data, sizeof(data), &len) != -1) {
2813 printf("\n\tstationname ");
2814 print_string(data, len);
2817 spacer = ' '; /* force first break */
2821 if (get80211val(s, IEEE80211_IOC_AUTHMODE, &val) != -1) {
2823 case IEEE80211_AUTH_NONE:
2824 LINE_CHECK("authmode NONE");
2826 case IEEE80211_AUTH_OPEN:
2827 LINE_CHECK("authmode OPEN");
2829 case IEEE80211_AUTH_SHARED:
2830 LINE_CHECK("authmode SHARED");
2832 case IEEE80211_AUTH_8021X:
2833 LINE_CHECK("authmode 802.1x");
2835 case IEEE80211_AUTH_WPA:
2836 if (get80211val(s, IEEE80211_IOC_WPA, &wpa) < 0)
2837 wpa = 1; /* default to WPA1 */
2840 LINE_CHECK("authmode WPA2/802.11i");
2843 LINE_CHECK("authmode WPA1+WPA2/802.11i");
2846 LINE_CHECK("authmode WPA");
2850 case IEEE80211_AUTH_AUTO:
2851 LINE_CHECK("authmode AUTO");
2854 LINE_CHECK("authmode UNKNOWN (0x%x)", val);
2859 if (wpa || verbose) {
2860 if (ioctl(s, IEEE80211_IOC_COUNTERMEASURES, &val) != -1) {
2862 LINE_CHECK("countermeasures");
2864 LINE_CHECK("-countermeasures");
2868 if (get80211val(s, IEEE80211_IOC_WEP, &wepmode) != -1 &&
2869 wepmode != IEEE80211_WEP_NOSUP) {
2873 case IEEE80211_WEP_OFF:
2874 LINE_CHECK("privacy OFF");
2876 case IEEE80211_WEP_ON:
2877 LINE_CHECK("privacy ON");
2879 case IEEE80211_WEP_MIXED:
2880 LINE_CHECK("privacy MIXED");
2883 LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
2888 * If we get here then we've got WEP support so we need
2889 * to print WEP status.
2892 if (get80211val(s, IEEE80211_IOC_WEPTXKEY, &val) < 0) {
2893 warn("WEP support, but no tx key!");
2897 LINE_CHECK("deftxkey %d", val+1);
2898 else if (wepmode != IEEE80211_WEP_OFF || verbose)
2899 LINE_CHECK("deftxkey UNDEF");
2901 if (get80211val(s, IEEE80211_IOC_NUMWEPKEYS, &num) < 0) {
2902 warn("WEP support, but no NUMWEPKEYS support!");
2907 for (i = 0; i < num; i++) {
2908 struct ieee80211req_key ik;
2910 memset(&ik, 0, sizeof(ik));
2912 if (get80211(s, IEEE80211_IOC_WPAKEY, &ik, sizeof(ik)) < 0) {
2913 warn("WEP support, but can get keys!");
2916 if (ik.ik_keylen != 0) {
2927 if (get80211val(s, IEEE80211_IOC_POWERSAVE, &val) != -1 &&
2928 val != IEEE80211_POWERSAVE_NOSUP ) {
2929 if (val != IEEE80211_POWERSAVE_OFF || verbose) {
2931 case IEEE80211_POWERSAVE_OFF:
2932 LINE_CHECK("powersavemode OFF");
2934 case IEEE80211_POWERSAVE_CAM:
2935 LINE_CHECK("powersavemode CAM");
2937 case IEEE80211_POWERSAVE_PSP:
2938 LINE_CHECK("powersavemode PSP");
2940 case IEEE80211_POWERSAVE_PSP_CAM:
2941 LINE_CHECK("powersavemode PSP-CAM");
2944 if (get80211val(s, IEEE80211_IOC_POWERSAVESLEEP, &val) != -1)
2945 LINE_CHECK("powersavesleep %d", val);
2949 if (get80211val(s, IEEE80211_IOC_TXPOWER, &val) != -1) {
2951 LINE_CHECK("txpower %d.5", val/2);
2953 LINE_CHECK("txpower %d", val/2);
2956 if (get80211val(s, IEEE80211_IOC_TXPOWMAX, &val) != -1)
2957 LINE_CHECK("txpowmax %.1f", val/2.);
2960 if (get80211val(s, IEEE80211_IOC_RTSTHRESHOLD, &val) != -1) {
2961 if (val != IEEE80211_RTS_MAX || verbose)
2962 LINE_CHECK("rtsthreshold %d", val);
2965 if (get80211val(s, IEEE80211_IOC_FRAGTHRESHOLD, &val) != -1) {
2966 if (val != IEEE80211_FRAG_MAX || verbose)
2967 LINE_CHECK("fragthreshold %d", val);
2969 if (opmode == IEEE80211_M_STA || verbose) {
2970 if (get80211val(s, IEEE80211_IOC_BMISSTHRESHOLD, &val) != -1) {
2971 if (val != IEEE80211_HWBMISS_MAX || verbose)
2972 LINE_CHECK("bmiss %d", val);
2976 if (get80211val(s, IEEE80211_IOC_MCAST_RATE, &val) != -1)
2977 printrate("mcastrate", val, 2*1, 0/*XXX*/);
2979 bgscaninterval = -1;
2980 (void) get80211val(s, IEEE80211_IOC_BGSCAN_INTERVAL, &bgscaninterval);
2982 if (get80211val(s, IEEE80211_IOC_SCANVALID, &val) != -1) {
2983 if (val != bgscaninterval || verbose)
2984 LINE_CHECK("scanvalid %u", val);
2988 if (get80211val(s, IEEE80211_IOC_BGSCAN, &bgscan) != -1) {
2990 LINE_CHECK("bgscan");
2992 LINE_CHECK("-bgscan");
2994 if (bgscan || verbose) {
2995 if (bgscaninterval != -1)
2996 LINE_CHECK("bgscanintvl %u", bgscaninterval);
2997 if (get80211val(s, IEEE80211_IOC_BGSCAN_IDLE, &val) != -1)
2998 LINE_CHECK("bgscanidle %u", val);
2999 if (IEEE80211_IS_CHAN_A(c) || verbose) {
3000 if (get80211val(s, IEEE80211_IOC_ROAM_RSSI_11A, &val) != -1)
3001 printrssi("roam:rssi11a", val);
3002 if (get80211val(s, IEEE80211_IOC_ROAM_RATE_11A, &val) != -1)
3003 printrate("roam:rate11a", val, -1, -1);
3005 if (IEEE80211_IS_CHAN_B(c) || verbose) {
3006 if (get80211val(s, IEEE80211_IOC_ROAM_RSSI_11B, &val) != -1)
3007 printrssi("roam:rssi11b", val);
3008 if (get80211val(s, IEEE80211_IOC_ROAM_RATE_11B, &val) != -1)
3009 printrate("roam:rate11b", val, -1, -1);
3011 if (IEEE80211_IS_CHAN_ANYG(c) || verbose) {
3012 if (get80211val(s, IEEE80211_IOC_ROAM_RSSI_11G, &val) != -1)
3013 printrssi("roam:rssi11g", val);
3014 if (get80211val(s, IEEE80211_IOC_ROAM_RATE_11G, &val) != -1)
3015 printrate("roam:rate11g", val, -1, -1);
3019 if (IEEE80211_IS_CHAN_ANYG(c) || verbose) {
3020 if (get80211val(s, IEEE80211_IOC_PUREG, &val) != -1) {
3022 LINE_CHECK("pureg");
3024 LINE_CHECK("-pureg");
3026 if (get80211val(s, IEEE80211_IOC_PROTMODE, &val) != -1) {
3028 case IEEE80211_PROTMODE_OFF:
3029 LINE_CHECK("protmode OFF");
3031 case IEEE80211_PROTMODE_CTS:
3032 LINE_CHECK("protmode CTS");
3034 case IEEE80211_PROTMODE_RTSCTS:
3035 LINE_CHECK("protmode RTSCTS");
3038 LINE_CHECK("protmode UNKNOWN (0x%x)", val);
3044 if (IEEE80211_IS_CHAN_HT(c) || verbose) {
3046 switch (htconf & 3) {
3059 if (get80211val(s, IEEE80211_IOC_HTCOMPAT, &val) != -1) {
3061 LINE_CHECK("-htcompat");
3063 LINE_CHECK("htcompat");
3065 if (get80211val(s, IEEE80211_IOC_AMPDU, &val) != -1) {
3068 LINE_CHECK("-ampdu");
3071 LINE_CHECK("ampdutx -ampdurx");
3074 LINE_CHECK("-ampdutx ampdurx");
3078 LINE_CHECK("ampdu");
3082 if (get80211val(s, IEEE80211_IOC_AMPDU_LIMIT, &val) != -1) {
3084 case IEEE80211_HTCAP_MAXRXAMPDU_8K:
3085 LINE_CHECK("ampdulimit 8k");
3087 case IEEE80211_HTCAP_MAXRXAMPDU_16K:
3088 LINE_CHECK("ampdulimit 16k");
3090 case IEEE80211_HTCAP_MAXRXAMPDU_32K:
3091 LINE_CHECK("ampdulimit 32k");
3093 case IEEE80211_HTCAP_MAXRXAMPDU_64K:
3094 LINE_CHECK("ampdulimit 64k");
3098 if (get80211val(s, IEEE80211_IOC_AMPDU_DENSITY, &val) != -1) {
3100 case IEEE80211_HTCAP_MPDUDENSITY_NA:
3102 LINE_CHECK("ampdudensity -");
3104 case IEEE80211_HTCAP_MPDUDENSITY_025:
3105 LINE_CHECK("ampdudensity .25");
3107 case IEEE80211_HTCAP_MPDUDENSITY_05:
3108 LINE_CHECK("ampdudensity .5");
3110 case IEEE80211_HTCAP_MPDUDENSITY_1:
3111 LINE_CHECK("ampdudensity 1");
3113 case IEEE80211_HTCAP_MPDUDENSITY_2:
3114 LINE_CHECK("ampdudensity 2");
3116 case IEEE80211_HTCAP_MPDUDENSITY_4:
3117 LINE_CHECK("ampdudensity 4");
3119 case IEEE80211_HTCAP_MPDUDENSITY_8:
3120 LINE_CHECK("ampdudensity 8");
3122 case IEEE80211_HTCAP_MPDUDENSITY_16:
3123 LINE_CHECK("ampdudensity 16");
3127 if (get80211val(s, IEEE80211_IOC_AMSDU, &val) != -1) {
3130 LINE_CHECK("-amsdu");
3133 LINE_CHECK("amsdutx -amsdurx");
3136 LINE_CHECK("-amsdutx amsdurx");
3140 LINE_CHECK("amsdu");
3144 /* XXX amsdu limit */
3146 if (get80211val(s, IEEE80211_IOC_SHORTGI, &val) != -1) {
3148 LINE_CHECK("shortgi");
3150 LINE_CHECK("-shortgi");
3152 if (get80211val(s, IEEE80211_IOC_HTPROTMODE, &val) != -1) {
3153 if (val == IEEE80211_PROTMODE_OFF)
3154 LINE_CHECK("htprotmode OFF");
3155 else if (val != IEEE80211_PROTMODE_RTSCTS)
3156 LINE_CHECK("htprotmode UNKNOWN (0x%x)", val);
3158 LINE_CHECK("htprotmode RTSCTS");
3160 if (get80211val(s, IEEE80211_IOC_PUREN, &val) != -1) {
3162 LINE_CHECK("puren");
3164 LINE_CHECK("-puren");
3168 if (get80211val(s, IEEE80211_IOC_WME, &wme) != -1) {
3176 if (get80211val(s, IEEE80211_IOC_BURST, &val) != -1) {
3178 LINE_CHECK("burst");
3180 LINE_CHECK("-burst");
3183 if (get80211val(s, IEEE80211_IOC_FF, &val) != -1) {
3189 if (get80211val(s, IEEE80211_IOC_TURBOP, &val) != -1) {
3191 LINE_CHECK("dturbo");
3193 LINE_CHECK("-dturbo");
3196 if (opmode == IEEE80211_M_HOSTAP) {
3197 if (get80211val(s, IEEE80211_IOC_HIDESSID, &val) != -1) {
3199 LINE_CHECK("hidessid");
3201 LINE_CHECK("-hidessid");
3203 if (get80211val(s, IEEE80211_IOC_APBRIDGE, &val) != -1) {
3205 LINE_CHECK("-apbridge");
3207 LINE_CHECK("apbridge");
3209 if (get80211val(s, IEEE80211_IOC_DTIM_PERIOD, &val) != -1)
3210 LINE_CHECK("dtimperiod %u", val);
3212 if (get80211val(s, IEEE80211_IOC_DOTH, &val) != -1) {
3214 LINE_CHECK("-doth");
3218 if (get80211val(s, IEEE80211_IOC_INACTIVITY, &val) != -1) {
3220 LINE_CHECK("-inact");
3222 LINE_CHECK("inact");
3225 if (get80211val(s, IEEE80211_IOC_ROAMING, &val) != -1) {
3226 if (val != IEEE80211_ROAMING_AUTO || verbose) {
3228 case IEEE80211_ROAMING_DEVICE:
3229 LINE_CHECK("roaming DEVICE");
3231 case IEEE80211_ROAMING_AUTO:
3232 LINE_CHECK("roaming AUTO");
3234 case IEEE80211_ROAMING_MANUAL:
3235 LINE_CHECK("roaming MANUAL");
3238 LINE_CHECK("roaming UNKNOWN (0x%x)",
3245 if (get80211val(s, IEEE80211_IOC_BEACON_INTERVAL, &val) != -1) {
3246 /* XXX default define not visible */
3247 if (val != 100 || verbose)
3248 LINE_CHECK("bintval %u", val);
3251 if (wme && verbose) {
3259 get80211(int s, int type, void *data, int len)
3261 struct ieee80211req ireq;
3263 (void) memset(&ireq, 0, sizeof(ireq));
3264 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3268 return ioctl(s, SIOCG80211, &ireq);
3272 get80211len(int s, int type, void *data, int len, int *plen)
3274 struct ieee80211req ireq;
3276 (void) memset(&ireq, 0, sizeof(ireq));
3277 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3281 if (ioctl(s, SIOCG80211, &ireq) < 0)
3288 get80211val(int s, int type, int *val)
3290 struct ieee80211req ireq;
3292 (void) memset(&ireq, 0, sizeof(ireq));
3293 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3295 if (ioctl(s, SIOCG80211, &ireq) < 0)
3302 set80211(int s, int type, int val, int len, void *data)
3304 struct ieee80211req ireq;
3306 (void) memset(&ireq, 0, sizeof(ireq));
3307 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3312 if (ioctl(s, SIOCS80211, &ireq) < 0)
3313 err(1, "SIOCS80211");
3317 get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
3325 hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
3331 if (sep != NULL && strchr(sep, *val) != NULL) {
3336 if (!isxdigit((u_char)val[0])) {
3337 warnx("bad hexadecimal digits");
3340 if (!isxdigit((u_char)val[1])) {
3341 warnx("odd count hexadecimal digits");
3345 if (p >= buf + len) {
3347 warnx("hexadecimal digits too long");
3349 warnx("string too long");
3353 #define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
3354 *p++ = (tohex((u_char)val[0]) << 4) |
3355 tohex((u_char)val[1]);
3362 /* The string "-" is treated as the empty string. */
3363 if (!hexstr && len == 1 && buf[0] == '-') {
3365 memset(buf, 0, *lenp);
3366 } else if (len < *lenp)
3367 memset(p, 0, *lenp - len);
3373 print_string(const u_int8_t *buf, int len)
3380 for (; i < len; i++) {
3381 if (!isprint(buf[i]) && buf[i] != '\0')
3383 if (isspace(buf[i]))
3387 if (hasspc || len == 0 || buf[0] == '\0')
3388 printf("\"%.*s\"", len, buf);
3390 printf("%.*s", len, buf);
3393 for (i = 0; i < len; i++)
3394 printf("%02x", buf[i]);
3398 static struct cmd ieee80211_cmds[] = {
3399 DEF_CMD_ARG("ssid", set80211ssid),
3400 DEF_CMD_ARG("nwid", set80211ssid),
3401 DEF_CMD_ARG("stationname", set80211stationname),
3402 DEF_CMD_ARG("station", set80211stationname), /* BSD/OS */
3403 DEF_CMD_ARG("channel", set80211channel),
3404 DEF_CMD_ARG("authmode", set80211authmode),
3405 DEF_CMD_ARG("powersavemode", set80211powersavemode),
3406 DEF_CMD("powersave", 1, set80211powersave),
3407 DEF_CMD("-powersave", 0, set80211powersave),
3408 DEF_CMD_ARG("powersavesleep", set80211powersavesleep),
3409 DEF_CMD_ARG("wepmode", set80211wepmode),
3410 DEF_CMD("wep", 1, set80211wep),
3411 DEF_CMD("-wep", 0, set80211wep),
3412 DEF_CMD_ARG("deftxkey", set80211weptxkey),
3413 DEF_CMD_ARG("weptxkey", set80211weptxkey),
3414 DEF_CMD_ARG("wepkey", set80211wepkey),
3415 DEF_CMD_ARG("nwkey", set80211nwkey), /* NetBSD */
3416 DEF_CMD("-nwkey", 0, set80211wep), /* NetBSD */
3417 DEF_CMD_ARG("rtsthreshold", set80211rtsthreshold),
3418 DEF_CMD_ARG("protmode", set80211protmode),
3419 DEF_CMD_ARG("txpower", set80211txpower),
3420 DEF_CMD_ARG("roaming", set80211roaming),
3421 DEF_CMD("wme", 1, set80211wme),
3422 DEF_CMD("-wme", 0, set80211wme),
3423 DEF_CMD("hidessid", 1, set80211hidessid),
3424 DEF_CMD("-hidessid", 0, set80211hidessid),
3425 DEF_CMD("apbridge", 1, set80211apbridge),
3426 DEF_CMD("-apbridge", 0, set80211apbridge),
3427 DEF_CMD_ARG("chanlist", set80211chanlist),
3428 DEF_CMD_ARG("bssid", set80211bssid),
3429 DEF_CMD_ARG("ap", set80211bssid),
3430 DEF_CMD("scan", 0, set80211scan),
3431 DEF_CMD_ARG("list", set80211list),
3432 DEF_CMD_ARG2("cwmin", set80211cwmin),
3433 DEF_CMD_ARG2("cwmax", set80211cwmax),
3434 DEF_CMD_ARG2("aifs", set80211aifs),
3435 DEF_CMD_ARG2("txoplimit", set80211txoplimit),
3436 DEF_CMD_ARG("acm", set80211acm),
3437 DEF_CMD_ARG("-acm", set80211noacm),
3438 DEF_CMD_ARG("ack", set80211ackpolicy),
3439 DEF_CMD_ARG("-ack", set80211noackpolicy),
3440 DEF_CMD_ARG2("bss:cwmin", set80211bsscwmin),
3441 DEF_CMD_ARG2("bss:cwmax", set80211bsscwmax),
3442 DEF_CMD_ARG2("bss:aifs", set80211bssaifs),
3443 DEF_CMD_ARG2("bss:txoplimit", set80211bsstxoplimit),
3444 DEF_CMD_ARG("dtimperiod", set80211dtimperiod),
3445 DEF_CMD_ARG("bintval", set80211bintval),
3446 DEF_CMD("mac:open", IEEE80211_MACCMD_POLICY_OPEN, set80211maccmd),
3447 DEF_CMD("mac:allow", IEEE80211_MACCMD_POLICY_ALLOW, set80211maccmd),
3448 DEF_CMD("mac:deny", IEEE80211_MACCMD_POLICY_DENY, set80211maccmd),
3449 DEF_CMD("mac:flush", IEEE80211_MACCMD_FLUSH, set80211maccmd),
3450 DEF_CMD("mac:detach", IEEE80211_MACCMD_DETACH, set80211maccmd),
3451 DEF_CMD_ARG("mac:add", set80211addmac),
3452 DEF_CMD_ARG("mac:del", set80211delmac),
3453 DEF_CMD_ARG("mac:kick", set80211kickmac),
3454 DEF_CMD("pureg", 1, set80211pureg),
3455 DEF_CMD("-pureg", 0, set80211pureg),
3456 DEF_CMD("ff", 1, set80211fastframes),
3457 DEF_CMD("-ff", 0, set80211fastframes),
3458 DEF_CMD("dturbo", 1, set80211dturbo),
3459 DEF_CMD("-dturbo", 0, set80211dturbo),
3460 DEF_CMD("bgscan", 1, set80211bgscan),
3461 DEF_CMD("-bgscan", 0, set80211bgscan),
3462 DEF_CMD_ARG("bgscanidle", set80211bgscanidle),
3463 DEF_CMD_ARG("bgscanintvl", set80211bgscanintvl),
3464 DEF_CMD_ARG("scanvalid", set80211scanvalid),
3465 DEF_CMD_ARG("roam:rssi11a", set80211roamrssi11a),
3466 DEF_CMD_ARG("roam:rssi11b", set80211roamrssi11b),
3467 DEF_CMD_ARG("roam:rssi11g", set80211roamrssi11g),
3468 DEF_CMD_ARG("roam:rate11a", set80211roamrate11a),
3469 DEF_CMD_ARG("roam:rate11b", set80211roamrate11b),
3470 DEF_CMD_ARG("roam:rate11g", set80211roamrate11g),
3471 DEF_CMD_ARG("mcastrate", set80211mcastrate),
3472 DEF_CMD_ARG("fragthreshold", set80211fragthreshold),
3473 DEF_CMD("burst", 1, set80211burst),
3474 DEF_CMD("-burst", 0, set80211burst),
3475 DEF_CMD_ARG("bmiss", set80211bmissthreshold),
3476 DEF_CMD_ARG("bmissthreshold", set80211bmissthreshold),
3477 DEF_CMD("shortgi", 1, set80211shortgi),
3478 DEF_CMD("-shortgi", 0, set80211shortgi),
3479 DEF_CMD("ampdurx", 2, set80211ampdu),
3480 DEF_CMD("-ampdurx", -2, set80211ampdu),
3481 DEF_CMD("ampdutx", 1, set80211ampdu),
3482 DEF_CMD("-ampdutx", -1, set80211ampdu),
3483 DEF_CMD("ampdu", 3, set80211ampdu), /* NB: tx+rx */
3484 DEF_CMD("-ampdu", -3, set80211ampdu),
3485 DEF_CMD_ARG("ampdulimit", set80211ampdulimit),
3486 DEF_CMD_ARG("ampdudensity", set80211ampdudensity),
3487 DEF_CMD("amsdurx", 2, set80211amsdu),
3488 DEF_CMD("-amsdurx", -2, set80211amsdu),
3489 DEF_CMD("amsdutx", 1, set80211amsdu),
3490 DEF_CMD("-amsdutx", -1, set80211amsdu),
3491 DEF_CMD("amsdu", 3, set80211amsdu), /* NB: tx+rx */
3492 DEF_CMD("-amsdu", -3, set80211amsdu),
3493 DEF_CMD_ARG("amsdulimit", set80211amsdulimit),
3494 DEF_CMD("puren", 1, set80211puren),
3495 DEF_CMD("-puren", 0, set80211puren),
3496 DEF_CMD("doth", 1, set80211doth),
3497 DEF_CMD("-doth", 0, set80211doth),
3498 DEF_CMD("htcompat", 1, set80211htcompat),
3499 DEF_CMD("-htcompat", 0, set80211htcompat),
3500 DEF_CMD("inact", 1, set80211inact),
3501 DEF_CMD("-inact", 0, set80211inact),
3502 DEF_CMD_ARG("htprotmode", set80211htprotmode),
3503 DEF_CMD("ht20", 1, set80211htconf),
3504 DEF_CMD("-ht20", 0, set80211htconf),
3505 DEF_CMD("ht40", 3, set80211htconf), /* NB: 20+40 */
3506 DEF_CMD("-ht40", 0, set80211htconf),
3507 DEF_CMD("ht", 3, set80211htconf), /* NB: 20+40 */
3508 DEF_CMD("-ht", 0, set80211htconf),
3510 static struct afswtch af_ieee80211 = {
3511 .af_name = "af_ieee80211",
3513 .af_other_status = ieee80211_status,
3516 static __constructor void
3517 ieee80211_ctor(void)
3519 #define N(a) (sizeof(a) / sizeof(a[0]))
3522 for (i = 0; i < N(ieee80211_cmds); i++)
3523 cmd_register(&ieee80211_cmds[i]);
3524 af_register(&af_ieee80211);