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.
47 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
48 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
49 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
50 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
51 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57 * POSSIBILITY OF SUCH DAMAGE.
60 #include <sys/param.h>
61 #include <sys/ioctl.h>
62 #include <sys/socket.h>
63 #include <sys/sysctl.h>
66 #include <net/ethernet.h>
68 #include <net/if_dl.h>
69 #include <net/if_types.h>
70 #include <net/if_media.h>
71 #include <net/route.h>
73 #include <net80211/ieee80211_ioctl.h>
74 #include <net80211/ieee80211_freebsd.h>
75 #include <net80211/ieee80211_superg.h>
76 #include <net80211/ieee80211_tdma.h>
77 #include <net80211/ieee80211_mesh.h>
90 #include <stddef.h> /* NB: for offsetof */
93 #include "regdomain.h"
95 #ifndef IEEE80211_FIXED_RATE_NONE
96 #define IEEE80211_FIXED_RATE_NONE 0xff
99 /* XXX need these publicly defined or similar */
100 #ifndef IEEE80211_NODE_AUTH
101 #define IEEE80211_NODE_AUTH 0x000001 /* authorized for data */
102 #define IEEE80211_NODE_QOS 0x000002 /* QoS enabled */
103 #define IEEE80211_NODE_ERP 0x000004 /* ERP enabled */
104 #define IEEE80211_NODE_PWR_MGT 0x000010 /* power save mode enabled */
105 #define IEEE80211_NODE_AREF 0x000020 /* authentication ref held */
106 #define IEEE80211_NODE_HT 0x000040 /* HT enabled */
107 #define IEEE80211_NODE_HTCOMPAT 0x000080 /* HT setup w/ vendor OUI's */
108 #define IEEE80211_NODE_WPS 0x000100 /* WPS association */
109 #define IEEE80211_NODE_TSN 0x000200 /* TSN association */
110 #define IEEE80211_NODE_AMPDU_RX 0x000400 /* AMPDU rx enabled */
111 #define IEEE80211_NODE_AMPDU_TX 0x000800 /* AMPDU tx enabled */
112 #define IEEE80211_NODE_MIMO_PS 0x001000 /* MIMO power save enabled */
113 #define IEEE80211_NODE_MIMO_RTS 0x002000 /* send RTS in MIMO PS */
114 #define IEEE80211_NODE_RIFS 0x004000 /* RIFS enabled */
115 #define IEEE80211_NODE_SGI20 0x008000 /* Short GI in HT20 enabled */
116 #define IEEE80211_NODE_SGI40 0x010000 /* Short GI in HT40 enabled */
117 #define IEEE80211_NODE_ASSOCID 0x020000 /* xmit requires associd */
118 #define IEEE80211_NODE_AMSDU_RX 0x040000 /* AMSDU rx enabled */
119 #define IEEE80211_NODE_AMSDU_TX 0x080000 /* AMSDU tx enabled */
122 #define MAXCHAN 1536 /* max 1.5K channels */
128 static void LINE_INIT(char c);
129 static void LINE_BREAK(void);
130 static void LINE_CHECK(const char *fmt, ...);
132 static const char *modename[IEEE80211_MODE_MAX] = {
133 [IEEE80211_MODE_AUTO] = "auto",
134 [IEEE80211_MODE_11A] = "11a",
135 [IEEE80211_MODE_11B] = "11b",
136 [IEEE80211_MODE_11G] = "11g",
137 [IEEE80211_MODE_FH] = "fh",
138 [IEEE80211_MODE_TURBO_A] = "turboA",
139 [IEEE80211_MODE_TURBO_G] = "turboG",
140 [IEEE80211_MODE_STURBO_A] = "sturbo",
141 [IEEE80211_MODE_11NA] = "11na",
142 [IEEE80211_MODE_11NG] = "11ng",
143 [IEEE80211_MODE_HALF] = "half",
144 [IEEE80211_MODE_QUARTER] = "quarter"
147 static void set80211(int s, int type, int val, int len, void *data);
148 static int get80211(int s, int type, void *data, int len);
149 static int get80211len(int s, int type, void *data, int len, int *plen);
150 static int get80211val(int s, int type, int *val);
151 static const char *get_string(const char *val, const char *sep,
152 u_int8_t *buf, int *lenp);
153 static void print_string(const u_int8_t *buf, int len);
154 static void print_regdomain(const struct ieee80211_regdomain *, int);
155 static void print_channels(int, const struct ieee80211req_chaninfo *,
156 int allchans, int verbose);
157 static void regdomain_makechannels(struct ieee80211_regdomain_req *,
158 const struct ieee80211_devcaps_req *);
159 static const char *mesh_linkstate_string(uint8_t state);
161 static struct ieee80211req_chaninfo *chaninfo;
162 static struct ieee80211_regdomain regdomain;
163 static int gotregdomain = 0;
164 static struct ieee80211_roamparams_req roamparams;
165 static int gotroam = 0;
166 static struct ieee80211_txparams_req txparams;
167 static int gottxparams = 0;
168 static struct ieee80211_channel curchan;
169 static int gotcurchan = 0;
170 static struct ifmediareq *ifmr;
171 static int htconf = 0;
172 static int gothtconf = 0;
179 if (get80211val(s, IEEE80211_IOC_HTCONF, &htconf) < 0)
180 warn("unable to get HT configuration information");
185 * Collect channel info from the kernel. We use this (mostly)
186 * to handle mapping between frequency and IEEE channel number.
191 if (chaninfo != NULL)
193 chaninfo = malloc(IEEE80211_CHANINFO_SIZE(MAXCHAN));
194 if (chaninfo == NULL)
195 errx(1, "no space for channel list");
196 if (get80211(s, IEEE80211_IOC_CHANINFO, chaninfo,
197 IEEE80211_CHANINFO_SIZE(MAXCHAN)) < 0)
198 err(1, "unable to get channel information");
199 ifmr = ifmedia_getstate(s);
203 static struct regdata *
206 static struct regdata *rdp = NULL;
208 rdp = lib80211_alloc_regdata();
210 errx(-1, "missing or corrupted regdomain database");
216 * Given the channel at index i with attributes from,
217 * check if there is a channel with attributes to in
218 * the channel table. With suitable attributes this
219 * allows the caller to look for promotion; e.g. from
223 canpromote(int i, int from, int to)
225 const struct ieee80211_channel *fc = &chaninfo->ic_chans[i];
228 if ((fc->ic_flags & from) != from)
230 /* NB: quick check exploiting ordering of chans w/ same frequency */
231 if (i+1 < chaninfo->ic_nchans &&
232 chaninfo->ic_chans[i+1].ic_freq == fc->ic_freq &&
233 (chaninfo->ic_chans[i+1].ic_flags & to) == to)
235 /* brute force search in case channel list is not ordered */
236 for (j = 0; j < chaninfo->ic_nchans; j++) {
237 const struct ieee80211_channel *tc = &chaninfo->ic_chans[j];
239 tc->ic_freq == fc->ic_freq && (tc->ic_flags & to) == to)
246 * Handle channel promotion. When a channel is specified with
247 * only a frequency we want to promote it to the ``best'' channel
248 * available. The channel list has separate entries for 11b, 11g,
249 * 11a, and 11n[ga] channels so specifying a frequency w/o any
250 * attributes requires we upgrade, e.g. from 11b -> 11g. This
251 * gets complicated when the channel is specified on the same
252 * command line with a media request that constrains the available
253 * channe list (e.g. mode 11a); we want to honor that to avoid
254 * confusing behaviour.
260 * Query the current mode of the interface in case it's
261 * constrained (e.g. to 11a). We must do this carefully
262 * as there may be a pending ifmedia request in which case
263 * asking the kernel will give us the wrong answer. This
264 * is an unfortunate side-effect of the way ifconfig is
265 * structure for modularity (yech).
267 * NB: ifmr is actually setup in getchaninfo (above); we
268 * assume it's called coincident with to this call so
269 * we have a ``current setting''; otherwise we must pass
270 * the socket descriptor down to here so we can make
271 * the ifmedia_getstate call ourselves.
273 int chanmode = ifmr != NULL ? IFM_MODE(ifmr->ifm_current) : IFM_AUTO;
275 /* when ambiguous promote to ``best'' */
276 /* NB: we abitrarily pick HT40+ over HT40- */
277 if (chanmode != IFM_IEEE80211_11B)
278 i = canpromote(i, IEEE80211_CHAN_B, IEEE80211_CHAN_G);
279 if (chanmode != IFM_IEEE80211_11G && (htconf & 1)) {
280 i = canpromote(i, IEEE80211_CHAN_G,
281 IEEE80211_CHAN_G | IEEE80211_CHAN_HT20);
283 i = canpromote(i, IEEE80211_CHAN_G,
284 IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D);
285 i = canpromote(i, IEEE80211_CHAN_G,
286 IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U);
289 if (chanmode != IFM_IEEE80211_11A && (htconf & 1)) {
290 i = canpromote(i, IEEE80211_CHAN_A,
291 IEEE80211_CHAN_A | IEEE80211_CHAN_HT20);
293 i = canpromote(i, IEEE80211_CHAN_A,
294 IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D);
295 i = canpromote(i, IEEE80211_CHAN_A,
296 IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U);
303 mapfreq(struct ieee80211_channel *chan, int freq, int flags)
307 for (i = 0; i < chaninfo->ic_nchans; i++) {
308 const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
310 if (c->ic_freq == freq && (c->ic_flags & flags) == flags) {
312 /* when ambiguous promote to ``best'' */
313 c = &chaninfo->ic_chans[promote(i)];
319 errx(1, "unknown/undefined frequency %u/0x%x", freq, flags);
323 mapchan(struct ieee80211_channel *chan, int ieee, int flags)
327 for (i = 0; i < chaninfo->ic_nchans; i++) {
328 const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
330 if (c->ic_ieee == ieee && (c->ic_flags & flags) == flags) {
332 /* when ambiguous promote to ``best'' */
333 c = &chaninfo->ic_chans[promote(i)];
339 errx(1, "unknown/undefined channel number %d flags 0x%x", ieee, flags);
342 static const struct ieee80211_channel *
347 if (get80211(s, IEEE80211_IOC_CURCHAN, &curchan, sizeof(curchan)) < 0) {
349 /* fall back to legacy ioctl */
350 if (get80211val(s, IEEE80211_IOC_CHANNEL, &val) < 0)
351 err(-1, "cannot figure out current channel");
353 mapchan(&curchan, val, 0);
359 static enum ieee80211_phymode
360 chan2mode(const struct ieee80211_channel *c)
362 if (IEEE80211_IS_CHAN_HTA(c))
363 return IEEE80211_MODE_11NA;
364 if (IEEE80211_IS_CHAN_HTG(c))
365 return IEEE80211_MODE_11NG;
366 if (IEEE80211_IS_CHAN_108A(c))
367 return IEEE80211_MODE_TURBO_A;
368 if (IEEE80211_IS_CHAN_108G(c))
369 return IEEE80211_MODE_TURBO_G;
370 if (IEEE80211_IS_CHAN_ST(c))
371 return IEEE80211_MODE_STURBO_A;
372 if (IEEE80211_IS_CHAN_FHSS(c))
373 return IEEE80211_MODE_FH;
374 if (IEEE80211_IS_CHAN_HALF(c))
375 return IEEE80211_MODE_HALF;
376 if (IEEE80211_IS_CHAN_QUARTER(c))
377 return IEEE80211_MODE_QUARTER;
378 if (IEEE80211_IS_CHAN_A(c))
379 return IEEE80211_MODE_11A;
380 if (IEEE80211_IS_CHAN_ANYG(c))
381 return IEEE80211_MODE_11G;
382 if (IEEE80211_IS_CHAN_B(c))
383 return IEEE80211_MODE_11B;
384 return IEEE80211_MODE_AUTO;
392 if (get80211(s, IEEE80211_IOC_ROAM,
393 &roamparams, sizeof(roamparams)) < 0)
394 err(1, "unable to get roaming parameters");
399 setroam_cb(int s, void *arg)
401 struct ieee80211_roamparams_req *roam = arg;
402 set80211(s, IEEE80211_IOC_ROAM, 0, sizeof(*roam), roam);
410 if (get80211(s, IEEE80211_IOC_TXPARAMS,
411 &txparams, sizeof(txparams)) < 0)
412 err(1, "unable to get transmit parameters");
417 settxparams_cb(int s, void *arg)
419 struct ieee80211_txparams_req *txp = arg;
420 set80211(s, IEEE80211_IOC_TXPARAMS, 0, sizeof(*txp), txp);
428 if (get80211(s, IEEE80211_IOC_REGDOMAIN,
429 ®domain, sizeof(regdomain)) < 0)
430 err(1, "unable to get regulatory domain info");
435 getdevcaps(int s, struct ieee80211_devcaps_req *dc)
437 if (get80211(s, IEEE80211_IOC_DEVCAPS, dc,
438 IEEE80211_DEVCAPS_SPACE(dc)) < 0)
439 err(1, "unable to get device capabilities");
443 setregdomain_cb(int s, void *arg)
445 struct ieee80211_regdomain_req *req;
446 struct ieee80211_regdomain *rd = arg;
447 struct ieee80211_devcaps_req *dc;
448 struct regdata *rdp = getregdata();
450 if (rd->country != NO_COUNTRY) {
451 const struct country *cc;
453 * Check current country seting to make sure it's
454 * compatible with the new regdomain. If not, then
455 * override it with any default country for this
456 * SKU. If we cannot arrange a match, then abort.
458 cc = lib80211_country_findbycc(rdp, rd->country);
460 errx(1, "unknown ISO country code %d", rd->country);
461 if (cc->rd->sku != rd->regdomain) {
462 const struct regdomain *rp;
464 * Check if country is incompatible with regdomain.
465 * To enable multiple regdomains for a country code
466 * we permit a mismatch between the regdomain and
467 * the country's associated regdomain when the
468 * regdomain is setup w/o a default country. For
469 * example, US is bound to the FCC regdomain but
470 * we allow US to be combined with FCC3 because FCC3
471 * has not default country. This allows bogus
472 * combinations like FCC3+DK which are resolved when
473 * constructing the channel list by deferring to the
474 * regdomain to construct the channel list.
476 rp = lib80211_regdomain_findbysku(rdp, rd->regdomain);
478 errx(1, "country %s (%s) is not usable with "
479 "regdomain %d", cc->isoname, cc->name,
481 else if (rp->cc != NULL && rp->cc != cc)
482 errx(1, "country %s (%s) is not usable with "
483 "regdomain %s", cc->isoname, cc->name,
488 * Fetch the device capabilities and calculate the
489 * full set of netbands for which we request a new
490 * channel list be constructed. Once that's done we
491 * push the regdomain info + channel list to the kernel.
493 dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
495 errx(1, "no space for device capabilities");
496 dc->dc_chaninfo.ic_nchans = MAXCHAN;
500 printf("drivercaps: 0x%x\n", dc->dc_drivercaps);
501 printf("cryptocaps: 0x%x\n", dc->dc_cryptocaps);
502 printf("htcaps : 0x%x\n", dc->dc_htcaps);
503 memcpy(chaninfo, &dc->dc_chaninfo,
504 IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
505 print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, 1/*verbose*/);
508 req = malloc(IEEE80211_REGDOMAIN_SIZE(dc->dc_chaninfo.ic_nchans));
510 errx(1, "no space for regdomain request");
512 regdomain_makechannels(req, dc);
515 print_regdomain(rd, 1/*verbose*/);
517 /* blech, reallocate channel list for new data */
518 if (chaninfo != NULL)
520 chaninfo = malloc(IEEE80211_CHANINFO_SPACE(&req->chaninfo));
521 if (chaninfo == NULL)
522 errx(1, "no space for channel list");
523 memcpy(chaninfo, &req->chaninfo,
524 IEEE80211_CHANINFO_SPACE(&req->chaninfo));
525 print_channels(s, &req->chaninfo, 1/*allchans*/, 1/*verbose*/);
527 if (req->chaninfo.ic_nchans == 0)
528 errx(1, "no channels calculated");
529 set80211(s, IEEE80211_IOC_REGDOMAIN, 0,
530 IEEE80211_REGDOMAIN_SPACE(req), req);
536 ieee80211_mhz2ieee(int freq, int flags)
538 struct ieee80211_channel chan;
539 mapfreq(&chan, freq, flags);
544 isanyarg(const char *arg)
546 return (strncmp(arg, "-", 1) == 0 ||
547 strncasecmp(arg, "any", 3) == 0 || strncasecmp(arg, "off", 3) == 0);
551 set80211ssid(const char *val, int d, int s, const struct afswtch *rafp)
555 u_int8_t data[IEEE80211_NWID_LEN];
559 if (len > 2 && isdigit((int)val[0]) && val[1] == ':') {
564 bzero(data, sizeof(data));
566 if (get_string(val, NULL, data, &len) == NULL)
569 set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
573 set80211meshid(const char *val, int d, int s, const struct afswtch *rafp)
576 u_int8_t data[IEEE80211_NWID_LEN];
578 memset(data, 0, sizeof(data));
580 if (get_string(val, NULL, data, &len) == NULL)
583 set80211(s, IEEE80211_IOC_MESH_ID, 0, len, data);
587 set80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
592 bzero(data, sizeof(data));
594 get_string(val, NULL, data, &len);
596 set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
600 * Parse a channel specification for attributes/flags.
602 * freq/xx channel width (5,10,20,40,40+,40-)
603 * freq:mode channel mode (a,b,g,h,n,t,s,d)
605 * These can be combined in either order; e.g. 2437:ng/40.
606 * Modes are case insensitive.
608 * The result is not validated here; it's assumed to be
609 * checked against the channel table fetched from the kernel.
612 getchannelflags(const char *val, int freq)
614 #define _CHAN_HT 0x80000000
620 cp = strchr(val, ':');
622 for (cp++; isalpha((int) *cp); cp++) {
623 /* accept mixed case */
628 case 'a': /* 802.11a */
629 flags |= IEEE80211_CHAN_A;
631 case 'b': /* 802.11b */
632 flags |= IEEE80211_CHAN_B;
634 case 'g': /* 802.11g */
635 flags |= IEEE80211_CHAN_G;
637 case 'h': /* ht = 802.11n */
638 case 'n': /* 802.11n */
639 flags |= _CHAN_HT; /* NB: private */
641 case 'd': /* dt = Atheros Dynamic Turbo */
642 flags |= IEEE80211_CHAN_TURBO;
644 case 't': /* ht, dt, st, t */
645 /* dt and unadorned t specify Dynamic Turbo */
646 if ((flags & (IEEE80211_CHAN_STURBO|_CHAN_HT)) == 0)
647 flags |= IEEE80211_CHAN_TURBO;
649 case 's': /* st = Atheros Static Turbo */
650 flags |= IEEE80211_CHAN_STURBO;
653 errx(-1, "%s: Invalid channel attribute %c\n",
658 cp = strchr(val, '/');
661 u_long cw = strtoul(cp+1, &ep, 10);
665 flags |= IEEE80211_CHAN_QUARTER;
668 flags |= IEEE80211_CHAN_HALF;
671 /* NB: this may be removed below */
672 flags |= IEEE80211_CHAN_HT20;
675 if (ep != NULL && *ep == '+')
676 flags |= IEEE80211_CHAN_HT40U;
677 else if (ep != NULL && *ep == '-')
678 flags |= IEEE80211_CHAN_HT40D;
681 errx(-1, "%s: Invalid channel width\n", val);
685 * Cleanup specifications.
687 if ((flags & _CHAN_HT) == 0) {
689 * If user specified freq/20 or freq/40 quietly remove
690 * HT cw attributes depending on channel use. To give
691 * an explicit 20/40 width for an HT channel you must
692 * indicate it is an HT channel since all HT channels
693 * are also usable for legacy operation; e.g. freq:n/40.
695 flags &= ~IEEE80211_CHAN_HT;
698 * Remove private indicator that this is an HT channel
699 * and if no explicit channel width has been given
700 * provide the default settings.
703 if ((flags & IEEE80211_CHAN_HT) == 0) {
704 struct ieee80211_channel chan;
706 * Consult the channel list to see if we can use
707 * HT40+ or HT40- (if both the map routines choose).
710 mapfreq(&chan, freq, 0);
712 mapchan(&chan, freq, 0);
713 flags |= (chan.ic_flags & IEEE80211_CHAN_HT);
721 getchannel(int s, struct ieee80211_channel *chan, const char *val)
726 memset(chan, 0, sizeof(*chan));
728 chan->ic_freq = IEEE80211_CHAN_ANY;
733 v = strtol(val, &eptr, 10);
734 if (val[0] == '\0' || val == eptr || errno == ERANGE ||
735 /* channel may be suffixed with nothing, :flag, or /width */
736 (eptr[0] != '\0' && eptr[0] != ':' && eptr[0] != '/'))
737 errx(1, "invalid channel specification%s",
738 errno == ERANGE ? " (out of range)" : "");
739 flags = getchannelflags(val, v);
740 if (v > 255) { /* treat as frequency */
741 mapfreq(chan, v, flags);
743 mapchan(chan, v, flags);
748 set80211channel(const char *val, int d, int s, const struct afswtch *rafp)
750 struct ieee80211_channel chan;
752 getchannel(s, &chan, val);
753 set80211(s, IEEE80211_IOC_CURCHAN, 0, sizeof(chan), &chan);
757 set80211chanswitch(const char *val, int d, int s, const struct afswtch *rafp)
759 struct ieee80211_chanswitch_req csr;
761 getchannel(s, &csr.csa_chan, val);
764 set80211(s, IEEE80211_IOC_CHANSWITCH, 0, sizeof(csr), &csr);
768 set80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
772 if (strcasecmp(val, "none") == 0) {
773 mode = IEEE80211_AUTH_NONE;
774 } else if (strcasecmp(val, "open") == 0) {
775 mode = IEEE80211_AUTH_OPEN;
776 } else if (strcasecmp(val, "shared") == 0) {
777 mode = IEEE80211_AUTH_SHARED;
778 } else if (strcasecmp(val, "8021x") == 0) {
779 mode = IEEE80211_AUTH_8021X;
780 } else if (strcasecmp(val, "wpa") == 0) {
781 mode = IEEE80211_AUTH_WPA;
783 errx(1, "unknown authmode");
786 set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
790 set80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
794 if (strcasecmp(val, "off") == 0) {
795 mode = IEEE80211_POWERSAVE_OFF;
796 } else if (strcasecmp(val, "on") == 0) {
797 mode = IEEE80211_POWERSAVE_ON;
798 } else if (strcasecmp(val, "cam") == 0) {
799 mode = IEEE80211_POWERSAVE_CAM;
800 } else if (strcasecmp(val, "psp") == 0) {
801 mode = IEEE80211_POWERSAVE_PSP;
802 } else if (strcasecmp(val, "psp-cam") == 0) {
803 mode = IEEE80211_POWERSAVE_PSP_CAM;
805 errx(1, "unknown powersavemode");
808 set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
812 set80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
815 set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
818 set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
823 set80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
825 set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
829 set80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
833 if (strcasecmp(val, "off") == 0) {
834 mode = IEEE80211_WEP_OFF;
835 } else if (strcasecmp(val, "on") == 0) {
836 mode = IEEE80211_WEP_ON;
837 } else if (strcasecmp(val, "mixed") == 0) {
838 mode = IEEE80211_WEP_MIXED;
840 errx(1, "unknown wep mode");
843 set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
847 set80211wep(const char *val, int d, int s, const struct afswtch *rafp)
849 set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
853 isundefarg(const char *arg)
855 return (strcmp(arg, "-") == 0 || strncasecmp(arg, "undef", 5) == 0);
859 set80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
862 set80211(s, IEEE80211_IOC_WEPTXKEY, IEEE80211_KEYIX_NONE, 0, NULL);
864 set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
868 set80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
872 u_int8_t data[IEEE80211_KEYBUF_SIZE];
874 if (isdigit((int)val[0]) && val[1] == ':') {
879 bzero(data, sizeof(data));
881 get_string(val, NULL, data, &len);
883 set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
887 * This function is purely a NetBSD compatibility interface. The NetBSD
888 * interface is too inflexible, but it's there so we'll support it since
889 * it's not all that hard.
892 set80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
896 u_int8_t data[IEEE80211_KEYBUF_SIZE];
898 set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
900 if (isdigit((int)val[0]) && val[1] == ':') {
901 txkey = val[0]-'0'-1;
904 for (i = 0; i < 4; i++) {
905 bzero(data, sizeof(data));
907 val = get_string(val, ",", data, &len);
911 set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
914 bzero(data, sizeof(data));
916 get_string(val, NULL, data, &len);
919 set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
921 bzero(data, sizeof(data));
922 for (i = 1; i < 4; i++)
923 set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
926 set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
930 set80211rtsthreshold(const char *val, int d, int s, const struct afswtch *rafp)
932 set80211(s, IEEE80211_IOC_RTSTHRESHOLD,
933 isundefarg(val) ? IEEE80211_RTS_MAX : atoi(val), 0, NULL);
937 set80211protmode(const char *val, int d, int s, const struct afswtch *rafp)
941 if (strcasecmp(val, "off") == 0) {
942 mode = IEEE80211_PROTMODE_OFF;
943 } else if (strcasecmp(val, "cts") == 0) {
944 mode = IEEE80211_PROTMODE_CTS;
945 } else if (strncasecmp(val, "rtscts", 3) == 0) {
946 mode = IEEE80211_PROTMODE_RTSCTS;
948 errx(1, "unknown protection mode");
951 set80211(s, IEEE80211_IOC_PROTMODE, mode, 0, NULL);
955 set80211htprotmode(const char *val, int d, int s, const struct afswtch *rafp)
959 if (strcasecmp(val, "off") == 0) {
960 mode = IEEE80211_PROTMODE_OFF;
961 } else if (strncasecmp(val, "rts", 3) == 0) {
962 mode = IEEE80211_PROTMODE_RTSCTS;
964 errx(1, "unknown protection mode");
967 set80211(s, IEEE80211_IOC_HTPROTMODE, mode, 0, NULL);
971 set80211txpower(const char *val, int d, int s, const struct afswtch *rafp)
973 double v = atof(val);
978 errx(-1, "invalid tx power (must be .5 dBm units)");
979 set80211(s, IEEE80211_IOC_TXPOWER, txpow, 0, NULL);
982 #define IEEE80211_ROAMING_DEVICE 0
983 #define IEEE80211_ROAMING_AUTO 1
984 #define IEEE80211_ROAMING_MANUAL 2
987 set80211roaming(const char *val, int d, int s, const struct afswtch *rafp)
991 if (strcasecmp(val, "device") == 0) {
992 mode = IEEE80211_ROAMING_DEVICE;
993 } else if (strcasecmp(val, "auto") == 0) {
994 mode = IEEE80211_ROAMING_AUTO;
995 } else if (strcasecmp(val, "manual") == 0) {
996 mode = IEEE80211_ROAMING_MANUAL;
998 errx(1, "unknown roaming mode");
1000 set80211(s, IEEE80211_IOC_ROAMING, mode, 0, NULL);
1004 set80211wme(const char *val, int d, int s, const struct afswtch *rafp)
1006 set80211(s, IEEE80211_IOC_WME, d, 0, NULL);
1010 set80211hidessid(const char *val, int d, int s, const struct afswtch *rafp)
1012 set80211(s, IEEE80211_IOC_HIDESSID, d, 0, NULL);
1016 set80211apbridge(const char *val, int d, int s, const struct afswtch *rafp)
1018 set80211(s, IEEE80211_IOC_APBRIDGE, d, 0, NULL);
1022 set80211fastframes(const char *val, int d, int s, const struct afswtch *rafp)
1024 set80211(s, IEEE80211_IOC_FF, d, 0, NULL);
1028 set80211dturbo(const char *val, int d, int s, const struct afswtch *rafp)
1030 set80211(s, IEEE80211_IOC_TURBOP, d, 0, NULL);
1034 set80211chanlist(const char *val, int d, int s, const struct afswtch *rafp)
1036 struct ieee80211req_chanlist chanlist;
1037 char *temp, *cp, *tp;
1039 temp = malloc(strlen(val) + 1);
1041 errx(1, "malloc failed");
1043 memset(&chanlist, 0, sizeof(chanlist));
1046 int first, last, f, c;
1048 tp = strchr(cp, ',');
1051 switch (sscanf(cp, "%u-%u", &first, &last)) {
1053 if (first > IEEE80211_CHAN_MAX)
1054 errx(-1, "channel %u out of range, max %u",
1055 first, IEEE80211_CHAN_MAX);
1056 setbit(chanlist.ic_channels, first);
1059 if (first > IEEE80211_CHAN_MAX)
1060 errx(-1, "channel %u out of range, max %u",
1061 first, IEEE80211_CHAN_MAX);
1062 if (last > IEEE80211_CHAN_MAX)
1063 errx(-1, "channel %u out of range, max %u",
1064 last, IEEE80211_CHAN_MAX);
1066 errx(-1, "void channel range, %u > %u",
1068 for (f = first; f <= last; f++)
1069 setbit(chanlist.ic_channels, f);
1081 set80211(s, IEEE80211_IOC_CHANLIST, 0, sizeof(chanlist), &chanlist);
1085 set80211bssid(const char *val, int d, int s, const struct afswtch *rafp)
1088 if (!isanyarg(val)) {
1090 struct sockaddr_dl sdl;
1092 temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1094 errx(1, "malloc failed");
1096 strcpy(temp + 1, val);
1097 sdl.sdl_len = sizeof(sdl);
1098 link_addr(temp, &sdl);
1100 if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1101 errx(1, "malformed link-level address");
1102 set80211(s, IEEE80211_IOC_BSSID, 0,
1103 IEEE80211_ADDR_LEN, LLADDR(&sdl));
1105 uint8_t zerobssid[IEEE80211_ADDR_LEN];
1106 memset(zerobssid, 0, sizeof(zerobssid));
1107 set80211(s, IEEE80211_IOC_BSSID, 0,
1108 IEEE80211_ADDR_LEN, zerobssid);
1113 getac(const char *ac)
1115 if (strcasecmp(ac, "ac_be") == 0 || strcasecmp(ac, "be") == 0)
1117 if (strcasecmp(ac, "ac_bk") == 0 || strcasecmp(ac, "bk") == 0)
1119 if (strcasecmp(ac, "ac_vi") == 0 || strcasecmp(ac, "vi") == 0)
1121 if (strcasecmp(ac, "ac_vo") == 0 || strcasecmp(ac, "vo") == 0)
1123 errx(1, "unknown wme access class %s", ac);
1127 DECL_CMD_FUNC2(set80211cwmin, ac, val)
1129 set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val), getac(ac), NULL);
1133 DECL_CMD_FUNC2(set80211cwmax, ac, val)
1135 set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val), getac(ac), NULL);
1139 DECL_CMD_FUNC2(set80211aifs, ac, val)
1141 set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val), getac(ac), NULL);
1145 DECL_CMD_FUNC2(set80211txoplimit, ac, val)
1147 set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), getac(ac), NULL);
1151 DECL_CMD_FUNC(set80211acm, ac, d)
1153 set80211(s, IEEE80211_IOC_WME_ACM, 1, getac(ac), NULL);
1156 DECL_CMD_FUNC(set80211noacm, ac, d)
1158 set80211(s, IEEE80211_IOC_WME_ACM, 0, getac(ac), NULL);
1162 DECL_CMD_FUNC(set80211ackpolicy, ac, d)
1164 set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 1, getac(ac), NULL);
1167 DECL_CMD_FUNC(set80211noackpolicy, ac, d)
1169 set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 0, getac(ac), NULL);
1173 DECL_CMD_FUNC2(set80211bsscwmin, ac, val)
1175 set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val),
1176 getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1180 DECL_CMD_FUNC2(set80211bsscwmax, ac, val)
1182 set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val),
1183 getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1187 DECL_CMD_FUNC2(set80211bssaifs, ac, val)
1189 set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val),
1190 getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1194 DECL_CMD_FUNC2(set80211bsstxoplimit, ac, val)
1196 set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val),
1197 getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1201 DECL_CMD_FUNC(set80211dtimperiod, val, d)
1203 set80211(s, IEEE80211_IOC_DTIM_PERIOD, atoi(val), 0, NULL);
1207 DECL_CMD_FUNC(set80211bintval, val, d)
1209 set80211(s, IEEE80211_IOC_BEACON_INTERVAL, atoi(val), 0, NULL);
1213 set80211macmac(int s, int op, const char *val)
1216 struct sockaddr_dl sdl;
1218 temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1220 errx(1, "malloc failed");
1222 strcpy(temp + 1, val);
1223 sdl.sdl_len = sizeof(sdl);
1224 link_addr(temp, &sdl);
1226 if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1227 errx(1, "malformed link-level address");
1228 set80211(s, op, 0, IEEE80211_ADDR_LEN, LLADDR(&sdl));
1232 DECL_CMD_FUNC(set80211addmac, val, d)
1234 set80211macmac(s, IEEE80211_IOC_ADDMAC, val);
1238 DECL_CMD_FUNC(set80211delmac, val, d)
1240 set80211macmac(s, IEEE80211_IOC_DELMAC, val);
1244 DECL_CMD_FUNC(set80211kickmac, val, d)
1247 struct sockaddr_dl sdl;
1248 struct ieee80211req_mlme mlme;
1250 temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1252 errx(1, "malloc failed");
1254 strcpy(temp + 1, val);
1255 sdl.sdl_len = sizeof(sdl);
1256 link_addr(temp, &sdl);
1258 if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1259 errx(1, "malformed link-level address");
1260 memset(&mlme, 0, sizeof(mlme));
1261 mlme.im_op = IEEE80211_MLME_DEAUTH;
1262 mlme.im_reason = IEEE80211_REASON_AUTH_EXPIRE;
1263 memcpy(mlme.im_macaddr, LLADDR(&sdl), IEEE80211_ADDR_LEN);
1264 set80211(s, IEEE80211_IOC_MLME, 0, sizeof(mlme), &mlme);
1268 DECL_CMD_FUNC(set80211maccmd, val, d)
1270 set80211(s, IEEE80211_IOC_MACCMD, d, 0, NULL);
1274 set80211meshrtmac(int s, int req, const char *val)
1277 struct sockaddr_dl sdl;
1279 temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1281 errx(1, "malloc failed");
1283 strcpy(temp + 1, val);
1284 sdl.sdl_len = sizeof(sdl);
1285 link_addr(temp, &sdl);
1287 if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1288 errx(1, "malformed link-level address");
1289 set80211(s, IEEE80211_IOC_MESH_RTCMD, req,
1290 IEEE80211_ADDR_LEN, LLADDR(&sdl));
1294 DECL_CMD_FUNC(set80211addmeshrt, val, d)
1296 set80211meshrtmac(s, IEEE80211_MESH_RTCMD_ADD, val);
1300 DECL_CMD_FUNC(set80211delmeshrt, val, d)
1302 set80211meshrtmac(s, IEEE80211_MESH_RTCMD_DELETE, val);
1306 DECL_CMD_FUNC(set80211meshrtcmd, val, d)
1308 set80211(s, IEEE80211_IOC_MESH_RTCMD, d, 0, NULL);
1312 DECL_CMD_FUNC(set80211hwmprootmode, val, d)
1316 if (strcasecmp(val, "normal") == 0)
1317 mode = IEEE80211_HWMP_ROOTMODE_NORMAL;
1318 else if (strcasecmp(val, "proactive") == 0)
1319 mode = IEEE80211_HWMP_ROOTMODE_PROACTIVE;
1320 else if (strcasecmp(val, "rann") == 0)
1321 mode = IEEE80211_HWMP_ROOTMODE_RANN;
1323 mode = IEEE80211_HWMP_ROOTMODE_DISABLED;
1324 set80211(s, IEEE80211_IOC_HWMP_ROOTMODE, mode, 0, NULL);
1328 DECL_CMD_FUNC(set80211hwmpmaxhops, val, d)
1330 set80211(s, IEEE80211_IOC_HWMP_MAXHOPS, atoi(val), 0, NULL);
1334 set80211pureg(const char *val, int d, int s, const struct afswtch *rafp)
1336 set80211(s, IEEE80211_IOC_PUREG, d, 0, NULL);
1340 set80211quiet(const char *val, int d, int s, const struct afswtch *rafp)
1342 set80211(s, IEEE80211_IOC_QUIET, d, 0, NULL);
1346 DECL_CMD_FUNC(set80211quietperiod, val, d)
1348 set80211(s, IEEE80211_IOC_QUIET_PERIOD, atoi(val), 0, NULL);
1352 DECL_CMD_FUNC(set80211quietcount, val, d)
1354 set80211(s, IEEE80211_IOC_QUIET_COUNT, atoi(val), 0, NULL);
1358 DECL_CMD_FUNC(set80211quietduration, val, d)
1360 set80211(s, IEEE80211_IOC_QUIET_DUR, atoi(val), 0, NULL);
1364 DECL_CMD_FUNC(set80211quietoffset, val, d)
1366 set80211(s, IEEE80211_IOC_QUIET_OFFSET, atoi(val), 0, NULL);
1370 set80211bgscan(const char *val, int d, int s, const struct afswtch *rafp)
1372 set80211(s, IEEE80211_IOC_BGSCAN, d, 0, NULL);
1376 DECL_CMD_FUNC(set80211bgscanidle, val, d)
1378 set80211(s, IEEE80211_IOC_BGSCAN_IDLE, atoi(val), 0, NULL);
1382 DECL_CMD_FUNC(set80211bgscanintvl, val, d)
1384 set80211(s, IEEE80211_IOC_BGSCAN_INTERVAL, atoi(val), 0, NULL);
1388 DECL_CMD_FUNC(set80211scanvalid, val, d)
1390 set80211(s, IEEE80211_IOC_SCANVALID, atoi(val), 0, NULL);
1394 * Parse an optional trailing specification of which netbands
1395 * to apply a parameter to. This is basically the same syntax
1396 * as used for channels but you can concatenate to specify
1397 * multiple. For example:
1398 * 14:abg apply to 11a, 11b, and 11g
1399 * 6:ht apply to 11na and 11ng
1400 * We don't make a big effort to catch silly things; this is
1401 * really a convenience mechanism.
1404 getmodeflags(const char *val)
1411 cp = strchr(val, ':');
1413 for (cp++; isalpha((int) *cp); cp++) {
1414 /* accept mixed case */
1419 case 'a': /* 802.11a */
1420 flags |= IEEE80211_CHAN_A;
1422 case 'b': /* 802.11b */
1423 flags |= IEEE80211_CHAN_B;
1425 case 'g': /* 802.11g */
1426 flags |= IEEE80211_CHAN_G;
1428 case 'n': /* 802.11n */
1429 flags |= IEEE80211_CHAN_HT;
1431 case 'd': /* dt = Atheros Dynamic Turbo */
1432 flags |= IEEE80211_CHAN_TURBO;
1434 case 't': /* ht, dt, st, t */
1435 /* dt and unadorned t specify Dynamic Turbo */
1436 if ((flags & (IEEE80211_CHAN_STURBO|IEEE80211_CHAN_HT)) == 0)
1437 flags |= IEEE80211_CHAN_TURBO;
1439 case 's': /* st = Atheros Static Turbo */
1440 flags |= IEEE80211_CHAN_STURBO;
1442 case 'h': /* 1/2-width channels */
1443 flags |= IEEE80211_CHAN_HALF;
1445 case 'q': /* 1/4-width channels */
1446 flags |= IEEE80211_CHAN_QUARTER;
1449 errx(-1, "%s: Invalid mode attribute %c\n",
1457 #define _APPLY(_flags, _base, _param, _v) do { \
1458 if (_flags & IEEE80211_CHAN_HT) { \
1459 if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1460 _base.params[IEEE80211_MODE_11NA]._param = _v; \
1461 _base.params[IEEE80211_MODE_11NG]._param = _v; \
1462 } else if (_flags & IEEE80211_CHAN_5GHZ) \
1463 _base.params[IEEE80211_MODE_11NA]._param = _v; \
1465 _base.params[IEEE80211_MODE_11NG]._param = _v; \
1467 if (_flags & IEEE80211_CHAN_TURBO) { \
1468 if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1469 _base.params[IEEE80211_MODE_TURBO_A]._param = _v; \
1470 _base.params[IEEE80211_MODE_TURBO_G]._param = _v; \
1471 } else if (_flags & IEEE80211_CHAN_5GHZ) \
1472 _base.params[IEEE80211_MODE_TURBO_A]._param = _v; \
1474 _base.params[IEEE80211_MODE_TURBO_G]._param = _v; \
1476 if (_flags & IEEE80211_CHAN_STURBO) \
1477 _base.params[IEEE80211_MODE_STURBO_A]._param = _v; \
1478 if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A) \
1479 _base.params[IEEE80211_MODE_11A]._param = _v; \
1480 if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G) \
1481 _base.params[IEEE80211_MODE_11G]._param = _v; \
1482 if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B) \
1483 _base.params[IEEE80211_MODE_11B]._param = _v; \
1484 if (_flags & IEEE80211_CHAN_HALF) \
1485 _base.params[IEEE80211_MODE_HALF]._param = _v; \
1486 if (_flags & IEEE80211_CHAN_QUARTER) \
1487 _base.params[IEEE80211_MODE_QUARTER]._param = _v; \
1489 #define _APPLY1(_flags, _base, _param, _v) do { \
1490 if (_flags & IEEE80211_CHAN_HT) { \
1491 if (_flags & IEEE80211_CHAN_5GHZ) \
1492 _base.params[IEEE80211_MODE_11NA]._param = _v; \
1494 _base.params[IEEE80211_MODE_11NG]._param = _v; \
1495 } else if ((_flags & IEEE80211_CHAN_108A) == IEEE80211_CHAN_108A) \
1496 _base.params[IEEE80211_MODE_TURBO_A]._param = _v; \
1497 else if ((_flags & IEEE80211_CHAN_108G) == IEEE80211_CHAN_108G) \
1498 _base.params[IEEE80211_MODE_TURBO_G]._param = _v; \
1499 else if ((_flags & IEEE80211_CHAN_ST) == IEEE80211_CHAN_ST) \
1500 _base.params[IEEE80211_MODE_STURBO_A]._param = _v; \
1501 else if (_flags & IEEE80211_CHAN_HALF) \
1502 _base.params[IEEE80211_MODE_HALF]._param = _v; \
1503 else if (_flags & IEEE80211_CHAN_QUARTER) \
1504 _base.params[IEEE80211_MODE_QUARTER]._param = _v; \
1505 else if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A) \
1506 _base.params[IEEE80211_MODE_11A]._param = _v; \
1507 else if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G) \
1508 _base.params[IEEE80211_MODE_11G]._param = _v; \
1509 else if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B) \
1510 _base.params[IEEE80211_MODE_11B]._param = _v; \
1512 #define _APPLY_RATE(_flags, _base, _param, _v) do { \
1513 if (_flags & IEEE80211_CHAN_HT) { \
1514 (_v) = (_v / 2) | IEEE80211_RATE_MCS; \
1516 _APPLY(_flags, _base, _param, _v); \
1518 #define _APPLY_RATE1(_flags, _base, _param, _v) do { \
1519 if (_flags & IEEE80211_CHAN_HT) { \
1520 (_v) = (_v / 2) | IEEE80211_RATE_MCS; \
1522 _APPLY1(_flags, _base, _param, _v); \
1526 DECL_CMD_FUNC(set80211roamrssi, val, d)
1528 double v = atof(val);
1533 errx(-1, "invalid rssi (must be .5 dBm units)");
1534 flags = getmodeflags(val);
1536 if (flags == 0) { /* NB: no flags => current channel */
1537 flags = getcurchan(s)->ic_flags;
1538 _APPLY1(flags, roamparams, rssi, rssi);
1540 _APPLY(flags, roamparams, rssi, rssi);
1541 callback_register(setroam_cb, &roamparams);
1545 getrate(const char *val, const char *tag)
1547 double v = atof(val);
1552 errx(-1, "invalid %s rate (must be .5 Mb/s units)", tag);
1553 return rate; /* NB: returns 2x the specified value */
1557 DECL_CMD_FUNC(set80211roamrate, val, d)
1561 rate = getrate(val, "roam");
1562 flags = getmodeflags(val);
1564 if (flags == 0) { /* NB: no flags => current channel */
1565 flags = getcurchan(s)->ic_flags;
1566 _APPLY_RATE1(flags, roamparams, rate, rate);
1568 _APPLY_RATE(flags, roamparams, rate, rate);
1569 callback_register(setroam_cb, &roamparams);
1573 DECL_CMD_FUNC(set80211mcastrate, val, d)
1577 rate = getrate(val, "mcast");
1578 flags = getmodeflags(val);
1580 if (flags == 0) { /* NB: no flags => current channel */
1581 flags = getcurchan(s)->ic_flags;
1582 _APPLY_RATE1(flags, txparams, mcastrate, rate);
1584 _APPLY_RATE(flags, txparams, mcastrate, rate);
1585 callback_register(settxparams_cb, &txparams);
1589 DECL_CMD_FUNC(set80211mgtrate, val, d)
1593 rate = getrate(val, "mgmt");
1594 flags = getmodeflags(val);
1596 if (flags == 0) { /* NB: no flags => current channel */
1597 flags = getcurchan(s)->ic_flags;
1598 _APPLY_RATE1(flags, txparams, mgmtrate, rate);
1600 _APPLY_RATE(flags, txparams, mgmtrate, rate);
1601 callback_register(settxparams_cb, &txparams);
1605 DECL_CMD_FUNC(set80211ucastrate, val, d)
1610 flags = getmodeflags(val);
1611 if (isanyarg(val)) {
1612 if (flags == 0) { /* NB: no flags => current channel */
1613 flags = getcurchan(s)->ic_flags;
1614 _APPLY1(flags, txparams, ucastrate,
1615 IEEE80211_FIXED_RATE_NONE);
1617 _APPLY(flags, txparams, ucastrate,
1618 IEEE80211_FIXED_RATE_NONE);
1620 int rate = getrate(val, "ucast");
1621 if (flags == 0) { /* NB: no flags => current channel */
1622 flags = getcurchan(s)->ic_flags;
1623 _APPLY_RATE1(flags, txparams, ucastrate, rate);
1625 _APPLY_RATE(flags, txparams, ucastrate, rate);
1627 callback_register(settxparams_cb, &txparams);
1631 DECL_CMD_FUNC(set80211maxretry, val, d)
1633 int v = atoi(val), flags;
1635 flags = getmodeflags(val);
1637 if (flags == 0) { /* NB: no flags => current channel */
1638 flags = getcurchan(s)->ic_flags;
1639 _APPLY1(flags, txparams, maxretry, v);
1641 _APPLY(flags, txparams, maxretry, v);
1642 callback_register(settxparams_cb, &txparams);
1648 DECL_CMD_FUNC(set80211fragthreshold, val, d)
1650 set80211(s, IEEE80211_IOC_FRAGTHRESHOLD,
1651 isundefarg(val) ? IEEE80211_FRAG_MAX : atoi(val), 0, NULL);
1655 DECL_CMD_FUNC(set80211bmissthreshold, val, d)
1657 set80211(s, IEEE80211_IOC_BMISSTHRESHOLD,
1658 isundefarg(val) ? IEEE80211_HWBMISS_MAX : atoi(val), 0, NULL);
1662 set80211burst(const char *val, int d, int s, const struct afswtch *rafp)
1664 set80211(s, IEEE80211_IOC_BURST, d, 0, NULL);
1668 set80211doth(const char *val, int d, int s, const struct afswtch *rafp)
1670 set80211(s, IEEE80211_IOC_DOTH, d, 0, NULL);
1674 set80211dfs(const char *val, int d, int s, const struct afswtch *rafp)
1676 set80211(s, IEEE80211_IOC_DFS, d, 0, NULL);
1680 set80211shortgi(const char *val, int d, int s, const struct afswtch *rafp)
1682 set80211(s, IEEE80211_IOC_SHORTGI,
1683 d ? (IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40) : 0,
1688 set80211ampdu(const char *val, int d, int s, const struct afswtch *rafp)
1692 if (get80211val(s, IEEE80211_IOC_AMPDU, &du) < 0)
1693 errx(-1, "cannot get AMPDU setting");
1699 set80211(s, IEEE80211_IOC_AMPDU, ampdu, 0, NULL);
1703 DECL_CMD_FUNC(set80211ampdulimit, val, d)
1707 switch (atoi(val)) {
1710 v = IEEE80211_HTCAP_MAXRXAMPDU_8K;
1714 v = IEEE80211_HTCAP_MAXRXAMPDU_16K;
1718 v = IEEE80211_HTCAP_MAXRXAMPDU_32K;
1722 v = IEEE80211_HTCAP_MAXRXAMPDU_64K;
1725 errx(-1, "invalid A-MPDU limit %s", val);
1727 set80211(s, IEEE80211_IOC_AMPDU_LIMIT, v, 0, NULL);
1731 DECL_CMD_FUNC(set80211ampdudensity, val, d)
1735 if (isanyarg(val) || strcasecmp(val, "na") == 0)
1736 v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1737 else switch ((int)(atof(val)*4)) {
1739 v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1742 v = IEEE80211_HTCAP_MPDUDENSITY_025;
1745 v = IEEE80211_HTCAP_MPDUDENSITY_05;
1748 v = IEEE80211_HTCAP_MPDUDENSITY_1;
1751 v = IEEE80211_HTCAP_MPDUDENSITY_2;
1754 v = IEEE80211_HTCAP_MPDUDENSITY_4;
1757 v = IEEE80211_HTCAP_MPDUDENSITY_8;
1760 v = IEEE80211_HTCAP_MPDUDENSITY_16;
1763 errx(-1, "invalid A-MPDU density %s", val);
1765 set80211(s, IEEE80211_IOC_AMPDU_DENSITY, v, 0, NULL);
1769 set80211amsdu(const char *val, int d, int s, const struct afswtch *rafp)
1773 if (get80211val(s, IEEE80211_IOC_AMSDU, &amsdu) < 0)
1774 err(-1, "cannot get AMSDU setting");
1780 set80211(s, IEEE80211_IOC_AMSDU, amsdu, 0, NULL);
1784 DECL_CMD_FUNC(set80211amsdulimit, val, d)
1786 set80211(s, IEEE80211_IOC_AMSDU_LIMIT, atoi(val), 0, NULL);
1790 set80211puren(const char *val, int d, int s, const struct afswtch *rafp)
1792 set80211(s, IEEE80211_IOC_PUREN, d, 0, NULL);
1796 set80211htcompat(const char *val, int d, int s, const struct afswtch *rafp)
1798 set80211(s, IEEE80211_IOC_HTCOMPAT, d, 0, NULL);
1802 set80211htconf(const char *val, int d, int s, const struct afswtch *rafp)
1804 set80211(s, IEEE80211_IOC_HTCONF, d, 0, NULL);
1809 set80211dwds(const char *val, int d, int s, const struct afswtch *rafp)
1811 set80211(s, IEEE80211_IOC_DWDS, d, 0, NULL);
1815 set80211inact(const char *val, int d, int s, const struct afswtch *rafp)
1817 set80211(s, IEEE80211_IOC_INACTIVITY, d, 0, NULL);
1821 set80211tsn(const char *val, int d, int s, const struct afswtch *rafp)
1823 set80211(s, IEEE80211_IOC_TSN, d, 0, NULL);
1827 set80211dotd(const char *val, int d, int s, const struct afswtch *rafp)
1829 set80211(s, IEEE80211_IOC_DOTD, d, 0, NULL);
1833 set80211smps(const char *val, int d, int s, const struct afswtch *rafp)
1835 set80211(s, IEEE80211_IOC_SMPS, d, 0, NULL);
1839 set80211rifs(const char *val, int d, int s, const struct afswtch *rafp)
1841 set80211(s, IEEE80211_IOC_RIFS, d, 0, NULL);
1845 DECL_CMD_FUNC(set80211tdmaslot, val, d)
1847 set80211(s, IEEE80211_IOC_TDMA_SLOT, atoi(val), 0, NULL);
1851 DECL_CMD_FUNC(set80211tdmaslotcnt, val, d)
1853 set80211(s, IEEE80211_IOC_TDMA_SLOTCNT, atoi(val), 0, NULL);
1857 DECL_CMD_FUNC(set80211tdmaslotlen, val, d)
1859 set80211(s, IEEE80211_IOC_TDMA_SLOTLEN, atoi(val), 0, NULL);
1863 DECL_CMD_FUNC(set80211tdmabintval, val, d)
1865 set80211(s, IEEE80211_IOC_TDMA_BINTERVAL, atoi(val), 0, NULL);
1869 DECL_CMD_FUNC(set80211meshttl, val, d)
1871 set80211(s, IEEE80211_IOC_MESH_TTL, atoi(val), 0, NULL);
1875 DECL_CMD_FUNC(set80211meshforward, val, d)
1877 set80211(s, IEEE80211_IOC_MESH_FWRD, d, 0, NULL);
1881 DECL_CMD_FUNC(set80211meshgate, val, d)
1883 set80211(s, IEEE80211_IOC_MESH_GATE, d, 0, NULL);
1887 DECL_CMD_FUNC(set80211meshpeering, val, d)
1889 set80211(s, IEEE80211_IOC_MESH_AP, d, 0, NULL);
1893 DECL_CMD_FUNC(set80211meshmetric, val, d)
1897 memcpy(v, val, sizeof(v));
1898 set80211(s, IEEE80211_IOC_MESH_PR_METRIC, 0, 0, v);
1902 DECL_CMD_FUNC(set80211meshpath, val, d)
1906 memcpy(v, val, sizeof(v));
1907 set80211(s, IEEE80211_IOC_MESH_PR_PATH, 0, 0, v);
1911 regdomain_sort(const void *a, const void *b)
1914 (IEEE80211_CHAN_ALLTURBO|IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)
1915 const struct ieee80211_channel *ca = a;
1916 const struct ieee80211_channel *cb = b;
1918 return ca->ic_freq == cb->ic_freq ?
1919 (ca->ic_flags & CHAN_ALL) - (cb->ic_flags & CHAN_ALL) :
1920 ca->ic_freq - cb->ic_freq;
1924 static const struct ieee80211_channel *
1925 chanlookup(const struct ieee80211_channel chans[], int nchans,
1926 int freq, int flags)
1930 flags &= IEEE80211_CHAN_ALLTURBO;
1931 for (i = 0; i < nchans; i++) {
1932 const struct ieee80211_channel *c = &chans[i];
1933 if (c->ic_freq == freq &&
1934 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1941 chanfind(const struct ieee80211_channel chans[], int nchans, int flags)
1945 for (i = 0; i < nchans; i++) {
1946 const struct ieee80211_channel *c = &chans[i];
1947 if ((c->ic_flags & flags) == flags)
1954 * Check channel compatibility.
1957 checkchan(const struct ieee80211req_chaninfo *avail, int freq, int flags)
1959 flags &= ~REQ_FLAGS;
1961 * Check if exact channel is in the calibration table;
1962 * everything below is to deal with channels that we
1963 * want to include but that are not explicitly listed.
1965 if (flags & IEEE80211_CHAN_HT40) {
1966 /* NB: we use an HT40 channel center that matches HT20 */
1967 flags = (flags &~ IEEE80211_CHAN_HT40) | IEEE80211_CHAN_HT20;
1969 if (chanlookup(avail->ic_chans, avail->ic_nchans, freq, flags) != NULL)
1971 if (flags & IEEE80211_CHAN_GSM) {
1973 * XXX GSM frequency mapping is handled in the kernel
1974 * so we cannot find them in the calibration table;
1975 * just accept the channel and the kernel will reject
1976 * the channel list if it's wrong.
1981 * If this is a 1/2 or 1/4 width channel allow it if a full
1982 * width channel is present for this frequency, and the device
1983 * supports fractional channels on this band. This is a hack
1984 * that avoids bloating the calibration table; it may be better
1985 * by per-band attributes though (we are effectively calculating
1986 * this attribute by scanning the channel list ourself).
1988 if ((flags & (IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)) == 0)
1990 if (chanlookup(avail->ic_chans, avail->ic_nchans, freq,
1991 flags &~ (IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)) == NULL)
1993 if (flags & IEEE80211_CHAN_HALF) {
1994 return chanfind(avail->ic_chans, avail->ic_nchans,
1995 IEEE80211_CHAN_HALF |
1996 (flags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ)));
1998 return chanfind(avail->ic_chans, avail->ic_nchans,
1999 IEEE80211_CHAN_QUARTER |
2000 (flags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ)));
2005 regdomain_addchans(struct ieee80211req_chaninfo *ci,
2006 const netband_head *bands,
2007 const struct ieee80211_regdomain *reg,
2009 const struct ieee80211req_chaninfo *avail)
2011 const struct netband *nb;
2012 const struct freqband *b;
2013 struct ieee80211_channel *c, *prev;
2014 int freq, hi_adj, lo_adj, channelSep;
2017 hi_adj = (chanFlags & IEEE80211_CHAN_HT40U) ? -20 : 0;
2018 lo_adj = (chanFlags & IEEE80211_CHAN_HT40D) ? 20 : 0;
2019 channelSep = (chanFlags & IEEE80211_CHAN_2GHZ) ? 0 : 40;
2020 LIST_FOREACH(nb, bands, next) {
2023 printf("%s:", __func__);
2024 printb(" chanFlags", chanFlags, IEEE80211_CHAN_BITS);
2025 printb(" bandFlags", nb->flags | b->flags,
2026 IEEE80211_CHAN_BITS);
2030 for (freq = b->freqStart + lo_adj;
2031 freq <= b->freqEnd + hi_adj; freq += b->chanSep) {
2033 * Construct flags for the new channel. We take
2034 * the attributes from the band descriptions except
2035 * for HT40 which is enabled generically (i.e. +/-
2036 * extension channel) in the band description and
2037 * then constrained according by channel separation.
2039 flags = nb->flags | b->flags;
2040 if (flags & IEEE80211_CHAN_HT) {
2042 * HT channels are generated specially; we're
2043 * called to add HT20, HT40+, and HT40- chan's
2044 * so we need to expand only band specs for
2045 * the HT channel type being added.
2047 if ((chanFlags & IEEE80211_CHAN_HT20) &&
2048 (flags & IEEE80211_CHAN_HT20) == 0) {
2050 printf("%u: skip, not an "
2051 "HT20 channel\n", freq);
2054 if ((chanFlags & IEEE80211_CHAN_HT40) &&
2055 (flags & IEEE80211_CHAN_HT40) == 0) {
2057 printf("%u: skip, not an "
2058 "HT40 channel\n", freq);
2061 /* NB: HT attribute comes from caller */
2062 flags &= ~IEEE80211_CHAN_HT;
2063 flags |= chanFlags & IEEE80211_CHAN_HT;
2066 * Check if device can operate on this frequency.
2068 if (!checkchan(avail, freq, flags)) {
2070 printf("%u: skip, ", freq);
2071 printb("flags", flags,
2072 IEEE80211_CHAN_BITS);
2073 printf(" not available\n");
2077 if ((flags & REQ_ECM) && !reg->ecm) {
2079 printf("%u: skip, ECM channel\n", freq);
2082 if ((flags & REQ_INDOOR) && reg->location == 'O') {
2084 printf("%u: skip, indoor channel\n",
2088 if ((flags & REQ_OUTDOOR) && reg->location == 'I') {
2090 printf("%u: skip, outdoor channel\n",
2094 if ((flags & IEEE80211_CHAN_HT40) &&
2095 prev != NULL && (freq - prev->ic_freq) < channelSep) {
2097 printf("%u: skip, only %u channel "
2098 "separation, need %d\n", freq,
2099 freq - prev->ic_freq, channelSep);
2102 if (ci->ic_nchans == IEEE80211_CHAN_MAX) {
2104 printf("%u: skip, channel table full\n",
2108 c = &ci->ic_chans[ci->ic_nchans++];
2109 memset(c, 0, sizeof(*c));
2111 c->ic_flags = flags;
2112 if (c->ic_flags & IEEE80211_CHAN_DFS)
2113 c->ic_maxregpower = nb->maxPowerDFS;
2115 c->ic_maxregpower = nb->maxPower;
2117 printf("[%3d] add freq %u ",
2118 ci->ic_nchans-1, c->ic_freq);
2119 printb("flags", c->ic_flags, IEEE80211_CHAN_BITS);
2120 printf(" power %u\n", c->ic_maxregpower);
2122 /* NB: kernel fills in other fields */
2129 regdomain_makechannels(
2130 struct ieee80211_regdomain_req *req,
2131 const struct ieee80211_devcaps_req *dc)
2133 struct regdata *rdp = getregdata();
2134 const struct country *cc;
2135 const struct ieee80211_regdomain *reg = &req->rd;
2136 struct ieee80211req_chaninfo *ci = &req->chaninfo;
2137 const struct regdomain *rd;
2140 * Locate construction table for new channel list. We treat
2141 * the regdomain/SKU as definitive so a country can be in
2142 * multiple with different properties (e.g. US in FCC+FCC3).
2143 * If no regdomain is specified then we fallback on the country
2144 * code to find the associated regdomain since countries always
2145 * belong to at least one regdomain.
2147 if (reg->regdomain == 0) {
2148 cc = lib80211_country_findbycc(rdp, reg->country);
2150 errx(1, "internal error, country %d not found",
2154 rd = lib80211_regdomain_findbysku(rdp, reg->regdomain);
2156 errx(1, "internal error, regdomain %d not found",
2158 if (rd->sku != SKU_DEBUG) {
2160 * regdomain_addchans incrememnts the channel count for
2161 * each channel it adds so initialize ic_nchans to zero.
2162 * Note that we know we have enough space to hold all possible
2163 * channels because the devcaps list size was used to
2164 * allocate our request.
2167 if (!LIST_EMPTY(&rd->bands_11b))
2168 regdomain_addchans(ci, &rd->bands_11b, reg,
2169 IEEE80211_CHAN_B, &dc->dc_chaninfo);
2170 if (!LIST_EMPTY(&rd->bands_11g))
2171 regdomain_addchans(ci, &rd->bands_11g, reg,
2172 IEEE80211_CHAN_G, &dc->dc_chaninfo);
2173 if (!LIST_EMPTY(&rd->bands_11a))
2174 regdomain_addchans(ci, &rd->bands_11a, reg,
2175 IEEE80211_CHAN_A, &dc->dc_chaninfo);
2176 if (!LIST_EMPTY(&rd->bands_11na) && dc->dc_htcaps != 0) {
2177 regdomain_addchans(ci, &rd->bands_11na, reg,
2178 IEEE80211_CHAN_A | IEEE80211_CHAN_HT20,
2180 if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2181 regdomain_addchans(ci, &rd->bands_11na, reg,
2182 IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U,
2184 regdomain_addchans(ci, &rd->bands_11na, reg,
2185 IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D,
2189 if (!LIST_EMPTY(&rd->bands_11ng) && dc->dc_htcaps != 0) {
2190 regdomain_addchans(ci, &rd->bands_11ng, reg,
2191 IEEE80211_CHAN_G | IEEE80211_CHAN_HT20,
2193 if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2194 regdomain_addchans(ci, &rd->bands_11ng, reg,
2195 IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U,
2197 regdomain_addchans(ci, &rd->bands_11ng, reg,
2198 IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D,
2202 qsort(ci->ic_chans, ci->ic_nchans, sizeof(ci->ic_chans[0]),
2205 memcpy(ci, &dc->dc_chaninfo,
2206 IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
2210 list_countries(void)
2212 struct regdata *rdp = getregdata();
2213 const struct country *cp;
2214 const struct regdomain *dp;
2218 printf("\nCountry codes:\n");
2219 LIST_FOREACH(cp, &rdp->countries, next) {
2220 printf("%2s %-15.15s%s", cp->isoname,
2221 cp->name, ((i+1)%4) == 0 ? "\n" : " ");
2225 printf("\nRegulatory domains:\n");
2226 LIST_FOREACH(dp, &rdp->domains, next) {
2227 printf("%-15.15s%s", dp->name, ((i+1)%4) == 0 ? "\n" : " ");
2234 defaultcountry(const struct regdomain *rd)
2236 struct regdata *rdp = getregdata();
2237 const struct country *cc;
2239 cc = lib80211_country_findbycc(rdp, rd->cc->code);
2241 errx(1, "internal error, ISO country code %d not "
2242 "defined for regdomain %s", rd->cc->code, rd->name);
2243 regdomain.country = cc->code;
2244 regdomain.isocc[0] = cc->isoname[0];
2245 regdomain.isocc[1] = cc->isoname[1];
2249 DECL_CMD_FUNC(set80211regdomain, val, d)
2251 struct regdata *rdp = getregdata();
2252 const struct regdomain *rd;
2254 rd = lib80211_regdomain_findbyname(rdp, val);
2257 long sku = strtol(val, &eptr, 0);
2260 rd = lib80211_regdomain_findbysku(rdp, sku);
2261 if (eptr == val || rd == NULL)
2262 errx(1, "unknown regdomain %s", val);
2265 regdomain.regdomain = rd->sku;
2266 if (regdomain.country == 0 && rd->cc != NULL) {
2268 * No country code setup and there's a default
2269 * one for this regdomain fill it in.
2273 callback_register(setregdomain_cb, ®domain);
2277 DECL_CMD_FUNC(set80211country, val, d)
2279 struct regdata *rdp = getregdata();
2280 const struct country *cc;
2282 cc = lib80211_country_findbyname(rdp, val);
2285 long code = strtol(val, &eptr, 0);
2288 cc = lib80211_country_findbycc(rdp, code);
2289 if (eptr == val || cc == NULL)
2290 errx(1, "unknown ISO country code %s", val);
2293 regdomain.regdomain = cc->rd->sku;
2294 regdomain.country = cc->code;
2295 regdomain.isocc[0] = cc->isoname[0];
2296 regdomain.isocc[1] = cc->isoname[1];
2297 callback_register(setregdomain_cb, ®domain);
2301 set80211location(const char *val, int d, int s, const struct afswtch *rafp)
2304 regdomain.location = d;
2305 callback_register(setregdomain_cb, ®domain);
2309 set80211ecm(const char *val, int d, int s, const struct afswtch *rafp)
2313 callback_register(setregdomain_cb, ®domain);
2329 if (spacer != '\t') {
2333 col = 8; /* 8-col tab */
2337 LINE_CHECK(const char *fmt, ...)
2344 n = vsnprintf(buf+1, sizeof(buf)-1, fmt, ap);
2357 getmaxrate(const uint8_t rates[15], uint8_t nrates)
2359 int i, maxrate = -1;
2361 for (i = 0; i < nrates; i++) {
2362 int rate = rates[i] & IEEE80211_RATE_VAL;
2370 getcaps(int capinfo)
2372 static char capstring[32];
2373 char *cp = capstring;
2375 if (capinfo & IEEE80211_CAPINFO_ESS)
2377 if (capinfo & IEEE80211_CAPINFO_IBSS)
2379 if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
2381 if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
2383 if (capinfo & IEEE80211_CAPINFO_PRIVACY)
2385 if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
2387 if (capinfo & IEEE80211_CAPINFO_PBCC)
2389 if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
2391 if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
2393 if (capinfo & IEEE80211_CAPINFO_RSN)
2395 if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
2404 static char flagstring[32];
2405 char *cp = flagstring;
2407 if (flags & IEEE80211_NODE_AUTH)
2409 if (flags & IEEE80211_NODE_QOS)
2411 if (flags & IEEE80211_NODE_ERP)
2413 if (flags & IEEE80211_NODE_PWR_MGT)
2415 if (flags & IEEE80211_NODE_HT) {
2417 if (flags & IEEE80211_NODE_HTCOMPAT)
2420 if (flags & IEEE80211_NODE_WPS)
2422 if (flags & IEEE80211_NODE_TSN)
2424 if (flags & IEEE80211_NODE_AMPDU_TX)
2426 if (flags & IEEE80211_NODE_AMPDU_RX)
2428 if (flags & IEEE80211_NODE_MIMO_PS) {
2430 if (flags & IEEE80211_NODE_MIMO_RTS)
2433 if (flags & IEEE80211_NODE_RIFS)
2435 if (flags & IEEE80211_NODE_SGI40) {
2437 if (flags & IEEE80211_NODE_SGI20)
2439 } else if (flags & IEEE80211_NODE_SGI20)
2441 if (flags & IEEE80211_NODE_AMSDU_TX)
2443 if (flags & IEEE80211_NODE_AMSDU_RX)
2450 printie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
2454 maxlen -= strlen(tag)+2;
2455 if (2*ielen > maxlen)
2458 for (; ielen > 0; ie++, ielen--) {
2461 printf("%02x", *ie);
2469 #define LE_READ_2(p) \
2471 ((((const u_int8_t *)(p))[0] ) | \
2472 (((const u_int8_t *)(p))[1] << 8)))
2473 #define LE_READ_4(p) \
2475 ((((const u_int8_t *)(p))[0] ) | \
2476 (((const u_int8_t *)(p))[1] << 8) | \
2477 (((const u_int8_t *)(p))[2] << 16) | \
2478 (((const u_int8_t *)(p))[3] << 24)))
2481 * NB: The decoding routines assume a properly formatted ie
2482 * which should be safe as the kernel only retains them
2487 printwmeparam(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2489 #define MS(_v, _f) (((_v) & _f) >> _f##_S)
2490 static const char *acnames[] = { "BE", "BK", "VO", "VI" };
2491 const struct ieee80211_wme_param *wme =
2492 (const struct ieee80211_wme_param *) ie;
2498 printf("<qosinfo 0x%x", wme->param_qosInfo);
2499 ie += offsetof(struct ieee80211_wme_param, params_acParams);
2500 for (i = 0; i < WME_NUM_AC; i++) {
2501 const struct ieee80211_wme_acparams *ac =
2502 &wme->params_acParams[i];
2504 printf(" %s[%saifsn %u cwmin %u cwmax %u txop %u]"
2506 , MS(ac->acp_aci_aifsn, WME_PARAM_ACM) ? "acm " : ""
2507 , MS(ac->acp_aci_aifsn, WME_PARAM_AIFSN)
2508 , MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMIN)
2509 , MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMAX)
2510 , LE_READ_2(&ac->acp_txop)
2518 printwmeinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2522 const struct ieee80211_wme_info *wme =
2523 (const struct ieee80211_wme_info *) ie;
2524 printf("<version 0x%x info 0x%x>",
2525 wme->wme_version, wme->wme_info);
2530 printhtcap(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2534 const struct ieee80211_ie_htcap *htcap =
2535 (const struct ieee80211_ie_htcap *) ie;
2539 printf("<cap 0x%x param 0x%x",
2540 LE_READ_2(&htcap->hc_cap), htcap->hc_param);
2543 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2544 if (isset(htcap->hc_mcsset, i)) {
2545 for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2546 if (isclr(htcap->hc_mcsset, j))
2550 printf("%s%u", sep, i);
2552 printf("%s%u-%u", sep, i, j);
2556 printf("] extcap 0x%x txbf 0x%x antenna 0x%x>",
2557 LE_READ_2(&htcap->hc_extcap),
2558 LE_READ_4(&htcap->hc_txbf),
2564 printhtinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2568 const struct ieee80211_ie_htinfo *htinfo =
2569 (const struct ieee80211_ie_htinfo *) ie;
2573 printf("<ctl %u, %x,%x,%x,%x", htinfo->hi_ctrlchannel,
2574 htinfo->hi_byte1, htinfo->hi_byte2, htinfo->hi_byte3,
2575 LE_READ_2(&htinfo->hi_byte45));
2576 printf(" basicmcs[");
2578 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2579 if (isset(htinfo->hi_basicmcsset, i)) {
2580 for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2581 if (isclr(htinfo->hi_basicmcsset, j))
2585 printf("%s%u", sep, i);
2587 printf("%s%u-%u", sep, i, j);
2596 printathie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2601 const struct ieee80211_ath_ie *ath =
2602 (const struct ieee80211_ath_ie *)ie;
2605 if (ath->ath_capability & ATHEROS_CAP_TURBO_PRIME)
2607 if (ath->ath_capability & ATHEROS_CAP_COMPRESSION)
2609 if (ath->ath_capability & ATHEROS_CAP_FAST_FRAME)
2611 if (ath->ath_capability & ATHEROS_CAP_XR)
2613 if (ath->ath_capability & ATHEROS_CAP_AR)
2615 if (ath->ath_capability & ATHEROS_CAP_BURST)
2617 if (ath->ath_capability & ATHEROS_CAP_WME)
2619 if (ath->ath_capability & ATHEROS_CAP_BOOST)
2621 printf("0x%x>", LE_READ_2(ath->ath_defkeyix));
2627 printmeshconf(const char *tag, const uint8_t *ie, size_t ielen, int maxlen)
2629 #define MATCHOUI(field, oui, string) \
2631 if (memcmp(field, oui, 4) == 0) \
2632 printf("%s", string); \
2637 const struct ieee80211_meshconf_ie *mconf =
2638 (const struct ieee80211_meshconf_ie *)ie;
2640 if (mconf->conf_pselid == IEEE80211_MESHCONF_PATH_HWMP)
2645 if (mconf->conf_pmetid == IEEE80211_MESHCONF_METRIC_AIRTIME)
2649 printf(" CONGESTION:");
2650 if (mconf->conf_ccid == IEEE80211_MESHCONF_CC_DISABLED)
2655 if (mconf->conf_syncid == IEEE80211_MESHCONF_SYNC_NEIGHOFF)
2660 if (mconf->conf_authid == IEEE80211_MESHCONF_AUTH_DISABLED)
2664 printf(" FORM:0x%x CAPS:0x%x>", mconf->conf_form,
2671 wpa_cipher(const u_int8_t *sel)
2673 #define WPA_SEL(x) (((x)<<24)|WPA_OUI)
2674 u_int32_t w = LE_READ_4(sel);
2677 case WPA_SEL(WPA_CSE_NULL):
2679 case WPA_SEL(WPA_CSE_WEP40):
2681 case WPA_SEL(WPA_CSE_WEP104):
2683 case WPA_SEL(WPA_CSE_TKIP):
2685 case WPA_SEL(WPA_CSE_CCMP):
2688 return "?"; /* NB: so 1<< is discarded */
2693 wpa_keymgmt(const u_int8_t *sel)
2695 #define WPA_SEL(x) (((x)<<24)|WPA_OUI)
2696 u_int32_t w = LE_READ_4(sel);
2699 case WPA_SEL(WPA_ASE_8021X_UNSPEC):
2700 return "8021X-UNSPEC";
2701 case WPA_SEL(WPA_ASE_8021X_PSK):
2703 case WPA_SEL(WPA_ASE_NONE):
2711 printwpaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2713 u_int8_t len = ie[1];
2720 ie += 6, len -= 4; /* NB: len is payload only */
2722 printf("<v%u", LE_READ_2(ie));
2725 printf(" mc:%s", wpa_cipher(ie));
2728 /* unicast ciphers */
2732 for (; n > 0; n--) {
2733 printf("%s%s", sep, wpa_cipher(ie));
2738 /* key management algorithms */
2742 for (; n > 0; n--) {
2743 printf("%s%s", sep, wpa_keymgmt(ie));
2748 if (len > 2) /* optional capabilities */
2749 printf(", caps 0x%x", LE_READ_2(ie));
2755 rsn_cipher(const u_int8_t *sel)
2757 #define RSN_SEL(x) (((x)<<24)|RSN_OUI)
2758 u_int32_t w = LE_READ_4(sel);
2761 case RSN_SEL(RSN_CSE_NULL):
2763 case RSN_SEL(RSN_CSE_WEP40):
2765 case RSN_SEL(RSN_CSE_WEP104):
2767 case RSN_SEL(RSN_CSE_TKIP):
2769 case RSN_SEL(RSN_CSE_CCMP):
2771 case RSN_SEL(RSN_CSE_WRAP):
2779 rsn_keymgmt(const u_int8_t *sel)
2781 #define RSN_SEL(x) (((x)<<24)|RSN_OUI)
2782 u_int32_t w = LE_READ_4(sel);
2785 case RSN_SEL(RSN_ASE_8021X_UNSPEC):
2786 return "8021X-UNSPEC";
2787 case RSN_SEL(RSN_ASE_8021X_PSK):
2789 case RSN_SEL(RSN_ASE_NONE):
2797 printrsnie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2804 ie += 2, ielen -= 2;
2806 printf("<v%u", LE_READ_2(ie));
2807 ie += 2, ielen -= 2;
2809 printf(" mc:%s", rsn_cipher(ie));
2810 ie += 4, ielen -= 4;
2812 /* unicast ciphers */
2814 ie += 2, ielen -= 2;
2816 for (; n > 0; n--) {
2817 printf("%s%s", sep, rsn_cipher(ie));
2818 ie += 4, ielen -= 4;
2822 /* key management algorithms */
2824 ie += 2, ielen -= 2;
2826 for (; n > 0; n--) {
2827 printf("%s%s", sep, rsn_keymgmt(ie));
2828 ie += 4, ielen -= 4;
2832 if (ielen > 2) /* optional capabilities */
2833 printf(", caps 0x%x", LE_READ_2(ie));
2839 /* XXX move to a public include file */
2840 #define IEEE80211_WPS_DEV_PASS_ID 0x1012
2841 #define IEEE80211_WPS_SELECTED_REG 0x1041
2842 #define IEEE80211_WPS_SETUP_STATE 0x1044
2843 #define IEEE80211_WPS_UUID_E 0x1047
2844 #define IEEE80211_WPS_VERSION 0x104a
2846 #define BE_READ_2(p) \
2848 ((((const u_int8_t *)(p))[1] ) | \
2849 (((const u_int8_t *)(p))[0] << 8)))
2852 printwpsie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2854 u_int8_t len = ie[1];
2858 static const char *dev_pass_id[] = {
2859 "D", /* Default (PIN) */
2860 "U", /* User-specified */
2861 "M", /* Machine-specified */
2863 "P", /* PushButton */
2864 "R" /* Registrar-specified */
2868 ie +=6, len -= 4; /* NB: len is payload only */
2870 /* WPS IE in Beacon and Probe Resp frames have different fields */
2873 uint16_t tlv_type = BE_READ_2(ie);
2874 uint16_t tlv_len = BE_READ_2(ie + 2);
2879 case IEEE80211_WPS_VERSION:
2880 printf("v:%d.%d", *ie >> 4, *ie & 0xf);
2882 case IEEE80211_WPS_SETUP_STATE:
2883 /* Only 1 and 2 are valid */
2884 if (*ie == 0 || *ie >= 3)
2887 printf(" st:%s", *ie == 1 ? "N" : "C");
2889 case IEEE80211_WPS_SELECTED_REG:
2890 printf(" sel:%s", *ie ? "T" : "F");
2892 case IEEE80211_WPS_DEV_PASS_ID:
2894 if (n < nitems(dev_pass_id))
2895 printf(" dpi:%s", dev_pass_id[n]);
2897 case IEEE80211_WPS_UUID_E:
2899 for (n = 0; n < (tlv_len - 1); n++)
2900 printf("%02x-", ie[n]);
2901 printf("%02x", ie[n]);
2904 ie += tlv_len, len -= tlv_len;
2911 printtdmaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2914 if (verbose && ielen >= sizeof(struct ieee80211_tdma_param)) {
2915 const struct ieee80211_tdma_param *tdma =
2916 (const struct ieee80211_tdma_param *) ie;
2919 printf("<v%u slot:%u slotcnt:%u slotlen:%u bintval:%u inuse:0x%x>",
2920 tdma->tdma_version, tdma->tdma_slot, tdma->tdma_slotcnt,
2921 LE_READ_2(&tdma->tdma_slotlen), tdma->tdma_bintval,
2922 tdma->tdma_inuse[0]);
2927 * Copy the ssid string contents into buf, truncating to fit. If the
2928 * ssid is entirely printable then just copy intact. Otherwise convert
2929 * to hexadecimal. If the result is truncated then replace the last
2930 * three characters with "...".
2933 copy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
2939 if (essid_len > bufsize)
2943 /* determine printable or not */
2944 for (i = 0, p = essid; i < maxlen; i++, p++) {
2945 if (*p < ' ' || *p > 0x7e)
2948 if (i != maxlen) { /* not printable, print as hex */
2951 strlcpy(buf, "0x", bufsize);
2954 for (i = 0; i < maxlen && bufsize >= 2; i++) {
2955 sprintf(&buf[2+2*i], "%02x", p[i]);
2959 memcpy(&buf[2+2*i-3], "...", 3);
2960 } else { /* printable, truncate as needed */
2961 memcpy(buf, essid, maxlen);
2962 if (maxlen != essid_len)
2963 memcpy(&buf[maxlen-3], "...", 3);
2969 printssid(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2971 char ssid[2*IEEE80211_NWID_LEN+1];
2973 printf("%s<%.*s>", tag, copy_essid(ssid, maxlen, ie+2, ie[1]), ssid);
2977 printrates(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2984 for (i = 2; i < ielen; i++) {
2985 printf("%s%s%d", sep,
2986 ie[i] & IEEE80211_RATE_BASIC ? "B" : "",
2987 ie[i] & IEEE80211_RATE_VAL);
2994 printcountry(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2996 const struct ieee80211_country_ie *cie =
2997 (const struct ieee80211_country_ie *) ie;
2998 int i, nbands, schan, nchan;
3000 printf("%s<%c%c%c", tag, cie->cc[0], cie->cc[1], cie->cc[2]);
3001 nbands = (cie->len - 3) / sizeof(cie->band[0]);
3002 for (i = 0; i < nbands; i++) {
3003 schan = cie->band[i].schan;
3004 nchan = cie->band[i].nchan;
3006 printf(" %u-%u,%u", schan, schan + nchan-1,
3007 cie->band[i].maxtxpwr);
3009 printf(" %u,%u", schan, cie->band[i].maxtxpwr);
3014 /* unaligned little endian access */
3015 #define LE_READ_4(p) \
3017 ((((const u_int8_t *)(p))[0] ) | \
3018 (((const u_int8_t *)(p))[1] << 8) | \
3019 (((const u_int8_t *)(p))[2] << 16) | \
3020 (((const u_int8_t *)(p))[3] << 24)))
3023 iswpaoui(const u_int8_t *frm)
3025 return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
3029 iswmeinfo(const u_int8_t *frm)
3031 return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3032 frm[6] == WME_INFO_OUI_SUBTYPE;
3036 iswmeparam(const u_int8_t *frm)
3038 return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3039 frm[6] == WME_PARAM_OUI_SUBTYPE;
3043 isatherosoui(const u_int8_t *frm)
3045 return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
3049 istdmaoui(const uint8_t *frm)
3051 return frm[1] > 3 && LE_READ_4(frm+2) == ((TDMA_OUI_TYPE<<24)|TDMA_OUI);
3055 iswpsoui(const uint8_t *frm)
3057 return frm[1] > 3 && LE_READ_4(frm+2) == ((WPS_OUI_TYPE<<24)|WPA_OUI);
3064 case IEEE80211_ELEMID_FHPARMS: return " FHPARMS";
3065 case IEEE80211_ELEMID_CFPARMS: return " CFPARMS";
3066 case IEEE80211_ELEMID_TIM: return " TIM";
3067 case IEEE80211_ELEMID_IBSSPARMS:return " IBSSPARMS";
3068 case IEEE80211_ELEMID_CHALLENGE:return " CHALLENGE";
3069 case IEEE80211_ELEMID_PWRCNSTR: return " PWRCNSTR";
3070 case IEEE80211_ELEMID_PWRCAP: return " PWRCAP";
3071 case IEEE80211_ELEMID_TPCREQ: return " TPCREQ";
3072 case IEEE80211_ELEMID_TPCREP: return " TPCREP";
3073 case IEEE80211_ELEMID_SUPPCHAN: return " SUPPCHAN";
3074 case IEEE80211_ELEMID_CSA: return " CSA";
3075 case IEEE80211_ELEMID_MEASREQ: return " MEASREQ";
3076 case IEEE80211_ELEMID_MEASREP: return " MEASREP";
3077 case IEEE80211_ELEMID_QUIET: return " QUIET";
3078 case IEEE80211_ELEMID_IBSSDFS: return " IBSSDFS";
3079 case IEEE80211_ELEMID_TPC: return " TPC";
3080 case IEEE80211_ELEMID_CCKM: return " CCKM";
3086 printies(const u_int8_t *vp, int ielen, int maxcols)
3090 case IEEE80211_ELEMID_SSID:
3092 printssid(" SSID", vp, 2+vp[1], maxcols);
3094 case IEEE80211_ELEMID_RATES:
3095 case IEEE80211_ELEMID_XRATES:
3097 printrates(vp[0] == IEEE80211_ELEMID_RATES ?
3098 " RATES" : " XRATES", vp, 2+vp[1], maxcols);
3100 case IEEE80211_ELEMID_DSPARMS:
3102 printf(" DSPARMS<%u>", vp[2]);
3104 case IEEE80211_ELEMID_COUNTRY:
3106 printcountry(" COUNTRY", vp, 2+vp[1], maxcols);
3108 case IEEE80211_ELEMID_ERP:
3110 printf(" ERP<0x%x>", vp[2]);
3112 case IEEE80211_ELEMID_VENDOR:
3114 printwpaie(" WPA", vp, 2+vp[1], maxcols);
3115 else if (iswmeinfo(vp))
3116 printwmeinfo(" WME", vp, 2+vp[1], maxcols);
3117 else if (iswmeparam(vp))
3118 printwmeparam(" WME", vp, 2+vp[1], maxcols);
3119 else if (isatherosoui(vp))
3120 printathie(" ATH", vp, 2+vp[1], maxcols);
3121 else if (iswpsoui(vp))
3122 printwpsie(" WPS", vp, 2+vp[1], maxcols);
3123 else if (istdmaoui(vp))
3124 printtdmaie(" TDMA", vp, 2+vp[1], maxcols);
3126 printie(" VEN", vp, 2+vp[1], maxcols);
3128 case IEEE80211_ELEMID_RSN:
3129 printrsnie(" RSN", vp, 2+vp[1], maxcols);
3131 case IEEE80211_ELEMID_HTCAP:
3132 printhtcap(" HTCAP", vp, 2+vp[1], maxcols);
3134 case IEEE80211_ELEMID_HTINFO:
3136 printhtinfo(" HTINFO", vp, 2+vp[1], maxcols);
3138 case IEEE80211_ELEMID_MESHID:
3140 printssid(" MESHID", vp, 2+vp[1], maxcols);
3142 case IEEE80211_ELEMID_MESHCONF:
3143 printmeshconf(" MESHCONF", vp, 2+vp[1], maxcols);
3147 printie(iename(vp[0]), vp, 2+vp[1], maxcols);
3156 printmimo(const struct ieee80211_mimo_info *mi)
3158 /* NB: don't muddy display unless there's something to show */
3159 if (mi->rssi[0] != 0 || mi->rssi[1] != 0 || mi->rssi[2] != 0) {
3160 /* XXX ignore EVM for now */
3161 printf(" (rssi %d:%d:%d nf %d:%d:%d)",
3162 mi->rssi[0], mi->rssi[1], mi->rssi[2],
3163 mi->noise[0], mi->noise[1], mi->noise[2]);
3170 uint8_t buf[24*1024];
3171 char ssid[IEEE80211_NWID_LEN+1];
3173 int len, ssidmax, idlen;
3175 if (get80211len(s, IEEE80211_IOC_SCAN_RESULTS, buf, sizeof(buf), &len) < 0)
3176 errx(1, "unable to get scan results");
3177 if (len < sizeof(struct ieee80211req_scan_result))
3182 ssidmax = verbose ? IEEE80211_NWID_LEN - 1 : 14;
3183 printf("%-*.*s %-17.17s %4s %4s %-7s %3s %4s\n"
3184 , ssidmax, ssidmax, "SSID/MESH ID"
3194 const struct ieee80211req_scan_result *sr;
3195 const uint8_t *vp, *idp;
3197 sr = (const struct ieee80211req_scan_result *) cp;
3198 vp = cp + sr->isr_ie_off;
3199 if (sr->isr_meshid_len) {
3200 idp = vp + sr->isr_ssid_len;
3201 idlen = sr->isr_meshid_len;
3204 idlen = sr->isr_ssid_len;
3206 printf("%-*.*s %s %3d %3dM %3d:%-3d %3d %-4.4s"
3208 , copy_essid(ssid, ssidmax, idp, idlen)
3210 , ether_ntoa((const struct ether_addr *) sr->isr_bssid)
3211 , ieee80211_mhz2ieee(sr->isr_freq, sr->isr_flags)
3212 , getmaxrate(sr->isr_rates, sr->isr_nrates)
3213 , (sr->isr_rssi/2)+sr->isr_noise, sr->isr_noise
3215 , getcaps(sr->isr_capinfo)
3217 printies(vp + sr->isr_ssid_len + sr->isr_meshid_len,
3218 sr->isr_ie_len, 24);
3220 cp += sr->isr_len, len -= sr->isr_len;
3221 } while (len >= sizeof(struct ieee80211req_scan_result));
3225 scan_and_wait(int s)
3227 struct ieee80211_scan_req sr;
3228 struct ieee80211req ireq;
3231 sroute = socket(PF_ROUTE, SOCK_RAW, 0);
3233 perror("socket(PF_ROUTE,SOCK_RAW)");
3236 (void) memset(&ireq, 0, sizeof(ireq));
3237 (void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
3238 ireq.i_type = IEEE80211_IOC_SCAN_REQ;
3240 memset(&sr, 0, sizeof(sr));
3241 sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE
3242 | IEEE80211_IOC_SCAN_BGSCAN
3243 | IEEE80211_IOC_SCAN_NOPICK
3244 | IEEE80211_IOC_SCAN_ONCE;
3245 sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
3249 ireq.i_len = sizeof(sr);
3251 * NB: only root can trigger a scan so ignore errors. Also ignore
3252 * possible errors from net80211, even if no new scan could be
3253 * started there might still be a valid scan cache.
3255 if (ioctl(s, SIOCS80211, &ireq) == 0) {
3257 struct if_announcemsghdr *ifan;
3258 struct rt_msghdr *rtm;
3261 if (read(sroute, buf, sizeof(buf)) < 0) {
3262 perror("read(PF_ROUTE)");
3265 rtm = (struct rt_msghdr *) buf;
3266 if (rtm->rtm_version != RTM_VERSION)
3268 ifan = (struct if_announcemsghdr *) rtm;
3269 } while (rtm->rtm_type != RTM_IEEE80211 ||
3270 ifan->ifan_what != RTM_IEEE80211_SCAN);
3276 DECL_CMD_FUNC(set80211scan, val, d)
3282 static enum ieee80211_opmode get80211opmode(int s);
3285 gettxseq(const struct ieee80211req_sta_info *si)
3289 if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3290 return si->isi_txseqs[0];
3291 /* XXX not right but usually what folks want */
3293 for (i = 0; i < IEEE80211_TID_SIZE; i++)
3294 if (si->isi_txseqs[i] > txseq)
3295 txseq = si->isi_txseqs[i];
3300 getrxseq(const struct ieee80211req_sta_info *si)
3304 if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3305 return si->isi_rxseqs[0];
3306 /* XXX not right but usually what folks want */
3308 for (i = 0; i < IEEE80211_TID_SIZE; i++)
3309 if (si->isi_rxseqs[i] > rxseq)
3310 rxseq = si->isi_rxseqs[i];
3315 list_stations(int s)
3318 struct ieee80211req_sta_req req;
3319 uint8_t buf[24*1024];
3321 enum ieee80211_opmode opmode = get80211opmode(s);
3325 /* broadcast address =>'s get all stations */
3326 (void) memset(u.req.is_u.macaddr, 0xff, IEEE80211_ADDR_LEN);
3327 if (opmode == IEEE80211_M_STA) {
3329 * Get information about the associated AP.
3331 (void) get80211(s, IEEE80211_IOC_BSSID,
3332 u.req.is_u.macaddr, IEEE80211_ADDR_LEN);
3334 if (get80211len(s, IEEE80211_IOC_STA_INFO, &u, sizeof(u), &len) < 0)
3335 errx(1, "unable to get station information");
3336 if (len < sizeof(struct ieee80211req_sta_info))
3341 if (opmode == IEEE80211_M_MBSS)
3342 printf("%-17.17s %4s %5s %5s %7s %4s %4s %4s %6s %6s\n"
3355 printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %-7s\n"
3367 cp = (const uint8_t *) u.req.info;
3369 const struct ieee80211req_sta_info *si;
3371 si = (const struct ieee80211req_sta_info *) cp;
3372 if (si->isi_len < sizeof(*si))
3374 if (opmode == IEEE80211_M_MBSS)
3375 printf("%s %4d %5x %5x %7.7s %3dM %4.1f %4d %6d %6d"
3376 , ether_ntoa((const struct ether_addr*)
3378 , ieee80211_mhz2ieee(si->isi_freq,
3382 , mesh_linkstate_string(si->isi_peerstate)
3390 printf("%s %4u %4d %3dM %4.1f %4d %6d %6d %-4.4s %-7.7s"
3391 , ether_ntoa((const struct ether_addr*)
3393 , IEEE80211_AID(si->isi_associd)
3394 , ieee80211_mhz2ieee(si->isi_freq,
3401 , getcaps(si->isi_capinfo)
3402 , getflags(si->isi_state)
3404 printies(cp + si->isi_ie_off, si->isi_ie_len, 24);
3405 printmimo(&si->isi_mimo);
3407 cp += si->isi_len, len -= si->isi_len;
3408 } while (len >= sizeof(struct ieee80211req_sta_info));
3412 mesh_linkstate_string(uint8_t state)
3414 static const char *state_names[] = {
3423 if (state >= nitems(state_names)) {
3424 static char buf[10];
3425 snprintf(buf, sizeof(buf), "#%u", state);
3428 return state_names[state];
3432 get_chaninfo(const struct ieee80211_channel *c, int precise,
3433 char buf[], size_t bsize)
3436 if (IEEE80211_IS_CHAN_FHSS(c))
3437 strlcat(buf, " FHSS", bsize);
3438 if (IEEE80211_IS_CHAN_A(c))
3439 strlcat(buf, " 11a", bsize);
3440 else if (IEEE80211_IS_CHAN_ANYG(c))
3441 strlcat(buf, " 11g", bsize);
3442 else if (IEEE80211_IS_CHAN_B(c))
3443 strlcat(buf, " 11b", bsize);
3444 if (IEEE80211_IS_CHAN_HALF(c))
3445 strlcat(buf, "/10MHz", bsize);
3446 if (IEEE80211_IS_CHAN_QUARTER(c))
3447 strlcat(buf, "/5MHz", bsize);
3448 if (IEEE80211_IS_CHAN_TURBO(c))
3449 strlcat(buf, " Turbo", bsize);
3451 if (IEEE80211_IS_CHAN_HT20(c))
3452 strlcat(buf, " ht/20", bsize);
3453 else if (IEEE80211_IS_CHAN_HT40D(c))
3454 strlcat(buf, " ht/40-", bsize);
3455 else if (IEEE80211_IS_CHAN_HT40U(c))
3456 strlcat(buf, " ht/40+", bsize);
3458 if (IEEE80211_IS_CHAN_HT(c))
3459 strlcat(buf, " ht", bsize);
3465 print_chaninfo(const struct ieee80211_channel *c, int verb)
3470 printf("Channel %3u : %u%c%c%c%c%c MHz%-14.14s",
3471 ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
3472 IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
3473 IEEE80211_IS_CHAN_DFS(c) ? 'D' : ' ',
3474 IEEE80211_IS_CHAN_RADAR(c) ? 'R' : ' ',
3475 IEEE80211_IS_CHAN_CWINT(c) ? 'I' : ' ',
3476 IEEE80211_IS_CHAN_CACDONE(c) ? 'C' : ' ',
3477 get_chaninfo(c, verb, buf, sizeof(buf)));
3479 printf("Channel %3u : %u%c MHz%-14.14s",
3480 ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
3481 IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
3482 get_chaninfo(c, verb, buf, sizeof(buf)));
3487 chanpref(const struct ieee80211_channel *c)
3489 if (IEEE80211_IS_CHAN_HT40(c))
3491 if (IEEE80211_IS_CHAN_HT20(c))
3493 if (IEEE80211_IS_CHAN_HALF(c))
3495 if (IEEE80211_IS_CHAN_QUARTER(c))
3497 if (IEEE80211_IS_CHAN_TURBO(c))
3499 if (IEEE80211_IS_CHAN_A(c))
3501 if (IEEE80211_IS_CHAN_G(c))
3503 if (IEEE80211_IS_CHAN_B(c))
3505 if (IEEE80211_IS_CHAN_PUREG(c))
3511 print_channels(int s, const struct ieee80211req_chaninfo *chans,
3512 int allchans, int verb)
3514 struct ieee80211req_chaninfo *achans;
3515 uint8_t reported[IEEE80211_CHAN_BYTES];
3516 const struct ieee80211_channel *c;
3519 achans = malloc(IEEE80211_CHANINFO_SPACE(chans));
3521 errx(1, "no space for active channel list");
3522 achans->ic_nchans = 0;
3523 memset(reported, 0, sizeof(reported));
3525 struct ieee80211req_chanlist active;
3527 if (get80211(s, IEEE80211_IOC_CHANLIST, &active, sizeof(active)) < 0)
3528 errx(1, "unable to get active channel list");
3529 for (i = 0; i < chans->ic_nchans; i++) {
3530 c = &chans->ic_chans[i];
3531 if (!isset(active.ic_channels, c->ic_ieee))
3534 * Suppress compatible duplicates unless
3535 * verbose. The kernel gives us it's
3536 * complete channel list which has separate
3537 * entries for 11g/11b and 11a/turbo.
3539 if (isset(reported, c->ic_ieee) && !verb) {
3540 /* XXX we assume duplicates are adjacent */
3541 achans->ic_chans[achans->ic_nchans-1] = *c;
3543 achans->ic_chans[achans->ic_nchans++] = *c;
3544 setbit(reported, c->ic_ieee);
3548 for (i = 0; i < chans->ic_nchans; i++) {
3549 c = &chans->ic_chans[i];
3550 /* suppress duplicates as above */
3551 if (isset(reported, c->ic_ieee) && !verb) {
3552 /* XXX we assume duplicates are adjacent */
3553 struct ieee80211_channel *a =
3554 &achans->ic_chans[achans->ic_nchans-1];
3555 if (chanpref(c) > chanpref(a))
3558 achans->ic_chans[achans->ic_nchans++] = *c;
3559 setbit(reported, c->ic_ieee);
3563 half = achans->ic_nchans / 2;
3564 if (achans->ic_nchans % 2)
3567 for (i = 0; i < achans->ic_nchans / 2; i++) {
3568 print_chaninfo(&achans->ic_chans[i], verb);
3569 print_chaninfo(&achans->ic_chans[half+i], verb);
3572 if (achans->ic_nchans % 2) {
3573 print_chaninfo(&achans->ic_chans[i], verb);
3580 list_channels(int s, int allchans)
3583 print_channels(s, chaninfo, allchans, verbose);
3587 print_txpow(const struct ieee80211_channel *c)
3589 printf("Channel %3u : %u MHz %3.1f reg %2d ",
3590 c->ic_ieee, c->ic_freq,
3591 c->ic_maxpower/2., c->ic_maxregpower);
3595 print_txpow_verbose(const struct ieee80211_channel *c)
3597 print_chaninfo(c, 1);
3598 printf("min %4.1f dBm max %3.1f dBm reg %2d dBm",
3599 c->ic_minpower/2., c->ic_maxpower/2., c->ic_maxregpower);
3600 /* indicate where regulatory cap limits power use */
3601 if (c->ic_maxpower > 2*c->ic_maxregpower)
3608 struct ieee80211req_chaninfo *achans;
3609 uint8_t reported[IEEE80211_CHAN_BYTES];
3610 struct ieee80211_channel *c, *prev;
3614 achans = malloc(IEEE80211_CHANINFO_SPACE(chaninfo));
3616 errx(1, "no space for active channel list");
3617 achans->ic_nchans = 0;
3618 memset(reported, 0, sizeof(reported));
3619 for (i = 0; i < chaninfo->ic_nchans; i++) {
3620 c = &chaninfo->ic_chans[i];
3621 /* suppress duplicates as above */
3622 if (isset(reported, c->ic_ieee) && !verbose) {
3623 /* XXX we assume duplicates are adjacent */
3624 assert(achans->ic_nchans > 0);
3625 prev = &achans->ic_chans[achans->ic_nchans-1];
3626 /* display highest power on channel */
3627 if (c->ic_maxpower > prev->ic_maxpower)
3630 achans->ic_chans[achans->ic_nchans++] = *c;
3631 setbit(reported, c->ic_ieee);
3635 half = achans->ic_nchans / 2;
3636 if (achans->ic_nchans % 2)
3639 for (i = 0; i < achans->ic_nchans / 2; i++) {
3640 print_txpow(&achans->ic_chans[i]);
3641 print_txpow(&achans->ic_chans[half+i]);
3644 if (achans->ic_nchans % 2) {
3645 print_txpow(&achans->ic_chans[i]);
3649 for (i = 0; i < achans->ic_nchans; i++) {
3650 print_txpow_verbose(&achans->ic_chans[i]);
3662 #define IEEE80211_C_BITS \
3663 "\20\1STA\002803ENCAP\7FF\10TURBOP\11IBSS\12PMGT" \
3664 "\13HOSTAP\14AHDEMO\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE" \
3665 "\21MONITOR\22DFS\23MBSS\30WPA1\31WPA2\32BURST\33WME\34WDS\36BGSCAN" \
3669 list_capabilities(int s)
3671 struct ieee80211_devcaps_req *dc;
3674 dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
3676 dc = malloc(IEEE80211_DEVCAPS_SIZE(1));
3678 errx(1, "no space for device capabilities");
3679 dc->dc_chaninfo.ic_nchans = verbose ? MAXCHAN : 1;
3681 printb("drivercaps", dc->dc_drivercaps, IEEE80211_C_BITS);
3682 if (dc->dc_cryptocaps != 0 || verbose) {
3684 printb("cryptocaps", dc->dc_cryptocaps, IEEE80211_CRYPTO_BITS);
3686 if (dc->dc_htcaps != 0 || verbose) {
3688 printb("htcaps", dc->dc_htcaps, IEEE80211_HTCAP_BITS);
3692 chaninfo = &dc->dc_chaninfo; /* XXX */
3693 print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, verbose);
3699 get80211wme(int s, int param, int ac, int *val)
3701 struct ieee80211req ireq;
3703 (void) memset(&ireq, 0, sizeof(ireq));
3704 (void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
3705 ireq.i_type = param;
3707 if (ioctl(s, SIOCG80211, &ireq) < 0) {
3708 warn("cannot get WME parameter %d, ac %d%s",
3709 param, ac & IEEE80211_WMEPARAM_VAL,
3710 ac & IEEE80211_WMEPARAM_BSS ? " (BSS)" : "");
3718 list_wme_aci(int s, const char *tag, int ac)
3722 printf("\t%s", tag);
3724 /* show WME BSS parameters */
3725 if (get80211wme(s, IEEE80211_IOC_WME_CWMIN, ac, &val) != -1)
3726 printf(" cwmin %2u", val);
3727 if (get80211wme(s, IEEE80211_IOC_WME_CWMAX, ac, &val) != -1)
3728 printf(" cwmax %2u", val);
3729 if (get80211wme(s, IEEE80211_IOC_WME_AIFS, ac, &val) != -1)
3730 printf(" aifs %2u", val);
3731 if (get80211wme(s, IEEE80211_IOC_WME_TXOPLIMIT, ac, &val) != -1)
3732 printf(" txopLimit %3u", val);
3733 if (get80211wme(s, IEEE80211_IOC_WME_ACM, ac, &val) != -1) {
3740 if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3741 if (get80211wme(s, IEEE80211_IOC_WME_ACKPOLICY, ac, &val) != -1) {
3754 static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
3758 /* display both BSS and local settings */
3759 for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
3761 if (ac & IEEE80211_WMEPARAM_BSS)
3762 list_wme_aci(s, " ", ac);
3764 list_wme_aci(s, acnames[ac], ac);
3765 if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3766 ac |= IEEE80211_WMEPARAM_BSS;
3769 ac &= ~IEEE80211_WMEPARAM_BSS;
3772 /* display only channel settings */
3773 for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++)
3774 list_wme_aci(s, acnames[ac], ac);
3781 const struct ieee80211_roamparam *rp;
3785 for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
3786 rp = &roamparams.params[mode];
3787 if (rp->rssi == 0 && rp->rate == 0)
3789 if (mode == IEEE80211_MODE_11NA || mode == IEEE80211_MODE_11NG) {
3791 LINE_CHECK("roam:%-7.7s rssi %2u.5dBm MCS %2u ",
3792 modename[mode], rp->rssi/2,
3793 rp->rate &~ IEEE80211_RATE_MCS);
3795 LINE_CHECK("roam:%-7.7s rssi %4udBm MCS %2u ",
3796 modename[mode], rp->rssi/2,
3797 rp->rate &~ IEEE80211_RATE_MCS);
3800 LINE_CHECK("roam:%-7.7s rssi %2u.5dBm rate %2u Mb/s",
3801 modename[mode], rp->rssi/2, rp->rate/2);
3803 LINE_CHECK("roam:%-7.7s rssi %4udBm rate %2u Mb/s",
3804 modename[mode], rp->rssi/2, rp->rate/2);
3809 /* XXX TODO: rate-to-string method... */
3811 get_mcs_mbs_rate_str(uint8_t rate)
3813 return (rate & IEEE80211_RATE_MCS) ? "MCS " : "Mb/s";
3817 get_rate_value(uint8_t rate)
3819 if (rate & IEEE80211_RATE_MCS)
3820 return (rate &~ IEEE80211_RATE_MCS);
3825 list_txparams(int s)
3827 const struct ieee80211_txparam *tp;
3831 for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
3832 tp = &txparams.params[mode];
3833 if (tp->mgmtrate == 0 && tp->mcastrate == 0)
3835 if (mode == IEEE80211_MODE_11NA || mode == IEEE80211_MODE_11NG) {
3836 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3837 LINE_CHECK("%-7.7s ucast NONE mgmt %2u %s "
3838 "mcast %2u %s maxretry %u",
3840 get_rate_value(tp->mgmtrate),
3841 get_mcs_mbs_rate_str(tp->mgmtrate),
3842 get_rate_value(tp->mcastrate),
3843 get_mcs_mbs_rate_str(tp->mcastrate),
3846 LINE_CHECK("%-7.7s ucast %2u MCS mgmt %2u %s "
3847 "mcast %2u %s maxretry %u",
3849 tp->ucastrate &~ IEEE80211_RATE_MCS,
3850 get_rate_value(tp->mgmtrate),
3851 get_mcs_mbs_rate_str(tp->mgmtrate),
3852 get_rate_value(tp->mcastrate),
3853 get_mcs_mbs_rate_str(tp->mcastrate),
3856 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3857 LINE_CHECK("%-7.7s ucast NONE mgmt %2u Mb/s "
3858 "mcast %2u Mb/s maxretry %u",
3861 tp->mcastrate/2, tp->maxretry);
3863 LINE_CHECK("%-7.7s ucast %2u Mb/s mgmt %2u Mb/s "
3864 "mcast %2u Mb/s maxretry %u",
3866 tp->ucastrate/2, tp->mgmtrate/2,
3867 tp->mcastrate/2, tp->maxretry);
3873 printpolicy(int policy)
3876 case IEEE80211_MACCMD_POLICY_OPEN:
3877 printf("policy: open\n");
3879 case IEEE80211_MACCMD_POLICY_ALLOW:
3880 printf("policy: allow\n");
3882 case IEEE80211_MACCMD_POLICY_DENY:
3883 printf("policy: deny\n");
3885 case IEEE80211_MACCMD_POLICY_RADIUS:
3886 printf("policy: radius\n");
3889 printf("policy: unknown (%u)\n", policy);
3897 struct ieee80211req ireq;
3898 struct ieee80211req_maclist *acllist;
3899 int i, nacls, policy, len;
3903 (void) memset(&ireq, 0, sizeof(ireq));
3904 (void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
3905 ireq.i_type = IEEE80211_IOC_MACCMD;
3906 ireq.i_val = IEEE80211_MACCMD_POLICY;
3907 if (ioctl(s, SIOCG80211, &ireq) < 0) {
3908 if (errno == EINVAL) {
3909 printf("No acl policy loaded\n");
3912 err(1, "unable to get mac policy");
3914 policy = ireq.i_val;
3915 if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
3917 } else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
3919 } else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
3921 } else if (policy == IEEE80211_MACCMD_POLICY_RADIUS) {
3922 c = 'r'; /* NB: should never have entries */
3924 printf("policy: unknown (%u)\n", policy);
3927 if (verbose || c == '?')
3928 printpolicy(policy);
3930 ireq.i_val = IEEE80211_MACCMD_LIST;
3932 if (ioctl(s, SIOCG80211, &ireq) < 0)
3933 err(1, "unable to get mac acl list size");
3934 if (ireq.i_len == 0) { /* NB: no acls */
3935 if (!(verbose || c == '?'))
3936 printpolicy(policy);
3943 err(1, "out of memory for acl list");
3946 if (ioctl(s, SIOCG80211, &ireq) < 0)
3947 err(1, "unable to get mac acl list");
3948 nacls = len / sizeof(*acllist);
3949 acllist = (struct ieee80211req_maclist *) data;
3950 for (i = 0; i < nacls; i++)
3951 printf("%c%s\n", c, ether_ntoa(
3952 (const struct ether_addr *) acllist[i].ml_macaddr));
3957 print_regdomain(const struct ieee80211_regdomain *reg, int verb)
3959 if ((reg->regdomain != 0 &&
3960 reg->regdomain != reg->country) || verb) {
3961 const struct regdomain *rd =
3962 lib80211_regdomain_findbysku(getregdata(), reg->regdomain);
3964 LINE_CHECK("regdomain %d", reg->regdomain);
3966 LINE_CHECK("regdomain %s", rd->name);
3968 if (reg->country != 0 || verb) {
3969 const struct country *cc =
3970 lib80211_country_findbycc(getregdata(), reg->country);
3972 LINE_CHECK("country %d", reg->country);
3974 LINE_CHECK("country %s", cc->isoname);
3976 if (reg->location == 'I')
3977 LINE_CHECK("indoor");
3978 else if (reg->location == 'O')
3979 LINE_CHECK("outdoor");
3981 LINE_CHECK("anywhere");
3989 list_regdomain(int s, int channelsalso)
3995 print_regdomain(®domain, 1);
3997 print_channels(s, chaninfo, 1/*allchans*/, 1/*verbose*/);
3999 print_regdomain(®domain, verbose);
4005 struct ieee80211req ireq;
4006 struct ieee80211req_mesh_route routes[128];
4007 struct ieee80211req_mesh_route *rt;
4009 (void) memset(&ireq, 0, sizeof(ireq));
4010 (void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
4011 ireq.i_type = IEEE80211_IOC_MESH_RTCMD;
4012 ireq.i_val = IEEE80211_MESH_RTCMD_LIST;
4013 ireq.i_data = &routes;
4014 ireq.i_len = sizeof(routes);
4015 if (ioctl(s, SIOCG80211, &ireq) < 0)
4016 err(1, "unable to get the Mesh routing table");
4018 printf("%-17.17s %-17.17s %4s %4s %4s %6s %s\n"
4027 for (rt = &routes[0]; rt - &routes[0] < ireq.i_len / sizeof(*rt); rt++){
4029 ether_ntoa((const struct ether_addr *)rt->imr_dest));
4030 printf("%s %4u %4u %6u %6u %c%c\n",
4031 ether_ntoa((const struct ether_addr *)rt->imr_nexthop),
4032 rt->imr_nhops, rt->imr_metric, rt->imr_lifetime,
4034 (rt->imr_flags & IEEE80211_MESHRT_FLAGS_DISCOVER) ?
4036 (rt->imr_flags & IEEE80211_MESHRT_FLAGS_VALID) ?
4038 (rt->imr_flags & IEEE80211_MESHRT_FLAGS_PROXY) ?
4040 (rt->imr_flags & IEEE80211_MESHRT_FLAGS_GATE) ?
4046 DECL_CMD_FUNC(set80211list, arg, d)
4048 #define iseq(a,b) (strncasecmp(a,b,sizeof(b)-1) == 0)
4052 if (iseq(arg, "sta"))
4054 else if (iseq(arg, "scan") || iseq(arg, "ap"))
4056 else if (iseq(arg, "chan") || iseq(arg, "freq"))
4057 list_channels(s, 1);
4058 else if (iseq(arg, "active"))
4059 list_channels(s, 0);
4060 else if (iseq(arg, "keys"))
4062 else if (iseq(arg, "caps"))
4063 list_capabilities(s);
4064 else if (iseq(arg, "wme") || iseq(arg, "wmm"))
4066 else if (iseq(arg, "mac"))
4068 else if (iseq(arg, "txpow"))
4070 else if (iseq(arg, "roam"))
4072 else if (iseq(arg, "txparam") || iseq(arg, "txparm"))
4074 else if (iseq(arg, "regdomain"))
4075 list_regdomain(s, 1);
4076 else if (iseq(arg, "countries"))
4078 else if (iseq(arg, "mesh"))
4081 errx(1, "Don't know how to list %s for %s", arg, name);
4086 static enum ieee80211_opmode
4087 get80211opmode(int s)
4089 struct ifmediareq ifmr;
4091 (void) memset(&ifmr, 0, sizeof(ifmr));
4092 (void) strlcpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
4094 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
4095 if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) {
4096 if (ifmr.ifm_current & IFM_FLAG0)
4097 return IEEE80211_M_AHDEMO;
4099 return IEEE80211_M_IBSS;
4101 if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
4102 return IEEE80211_M_HOSTAP;
4103 if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
4104 return IEEE80211_M_MONITOR;
4105 if (ifmr.ifm_current & IFM_IEEE80211_MBSS)
4106 return IEEE80211_M_MBSS;
4108 return IEEE80211_M_STA;
4113 printcipher(int s, struct ieee80211req *ireq, int keylenop)
4115 switch (ireq->i_val) {
4116 case IEEE80211_CIPHER_WEP:
4117 ireq->i_type = keylenop;
4118 if (ioctl(s, SIOCG80211, ireq) != -1)
4120 ireq->i_len <= 5 ? "40" :
4121 ireq->i_len <= 13 ? "104" : "128");
4125 case IEEE80211_CIPHER_TKIP:
4128 case IEEE80211_CIPHER_AES_OCB:
4131 case IEEE80211_CIPHER_AES_CCM:
4134 case IEEE80211_CIPHER_CKIP:
4137 case IEEE80211_CIPHER_NONE:
4141 printf("UNKNOWN (0x%x)", ireq->i_val);
4148 printkey(const struct ieee80211req_key *ik)
4150 static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
4151 u_int keylen = ik->ik_keylen;
4154 printcontents = printkeys &&
4155 (memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
4158 switch (ik->ik_type) {
4159 case IEEE80211_CIPHER_WEP:
4161 LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
4162 keylen <= 5 ? "40-bit" :
4163 keylen <= 13 ? "104-bit" : "128-bit");
4165 case IEEE80211_CIPHER_TKIP:
4167 keylen -= 128/8; /* ignore MIC for now */
4168 LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4170 case IEEE80211_CIPHER_AES_OCB:
4171 LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4173 case IEEE80211_CIPHER_AES_CCM:
4174 LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4176 case IEEE80211_CIPHER_CKIP:
4177 LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4179 case IEEE80211_CIPHER_NONE:
4180 LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4183 LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
4184 ik->ik_type, ik->ik_keyix+1, 8*keylen);
4187 if (printcontents) {
4191 for (i = 0; i < keylen; i++)
4192 printf("%02x", ik->ik_keydata[i]);
4194 if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4195 (ik->ik_keyrsc != 0 || verbose))
4196 printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
4197 if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4198 (ik->ik_keytsc != 0 || verbose))
4199 printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
4200 if (ik->ik_flags != 0 && verbose) {
4201 const char *sep = " ";
4203 if (ik->ik_flags & IEEE80211_KEY_XMIT)
4204 printf("%stx", sep), sep = "+";
4205 if (ik->ik_flags & IEEE80211_KEY_RECV)
4206 printf("%srx", sep), sep = "+";
4207 if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
4208 printf("%sdef", sep), sep = "+";
4215 printrate(const char *tag, int v, int defrate, int defmcs)
4217 if ((v & IEEE80211_RATE_MCS) == 0) {
4220 LINE_CHECK("%s %d.5", tag, v/2);
4222 LINE_CHECK("%s %d", tag, v/2);
4226 LINE_CHECK("%s %d", tag, v &~ 0x80);
4231 getid(int s, int ix, void *data, size_t len, int *plen, int mesh)
4233 struct ieee80211req ireq;
4235 (void) memset(&ireq, 0, sizeof(ireq));
4236 (void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
4237 ireq.i_type = (!mesh) ? IEEE80211_IOC_SSID : IEEE80211_IOC_MESH_ID;
4241 if (ioctl(s, SIOCG80211, &ireq) < 0)
4248 ieee80211_status(int s)
4250 static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
4251 enum ieee80211_opmode opmode = get80211opmode(s);
4252 int i, num, wpa, wme, bgscan, bgscaninterval, val, len, wepmode;
4254 const struct ieee80211_channel *c;
4255 const struct ieee80211_roamparam *rp;
4256 const struct ieee80211_txparam *tp;
4258 if (getid(s, -1, data, sizeof(data), &len, 0) < 0) {
4259 /* If we can't get the SSID, this isn't an 802.11 device. */
4264 * Invalidate cached state so printing status for multiple
4265 * if's doesn't reuse the first interfaces' cached state.
4274 if (opmode == IEEE80211_M_MBSS) {
4276 getid(s, 0, data, sizeof(data), &len, 1);
4277 print_string(data, len);
4279 if (get80211val(s, IEEE80211_IOC_NUMSSIDS, &num) < 0)
4283 for (i = 0; i < num; i++) {
4284 if (getid(s, i, data, sizeof(data), &len, 0) >= 0 && len > 0) {
4285 printf(" %d:", i + 1);
4286 print_string(data, len);
4290 print_string(data, len);
4293 if (c->ic_freq != IEEE80211_CHAN_ANY) {
4295 printf(" channel %d (%u MHz%s)", c->ic_ieee, c->ic_freq,
4296 get_chaninfo(c, 1, buf, sizeof(buf)));
4298 printf(" channel UNDEF");
4300 if (get80211(s, IEEE80211_IOC_BSSID, data, IEEE80211_ADDR_LEN) >= 0 &&
4301 (memcmp(data, zerobssid, sizeof(zerobssid)) != 0 || verbose))
4302 printf(" bssid %s", ether_ntoa((struct ether_addr *)data));
4304 if (get80211len(s, IEEE80211_IOC_STATIONNAME, data, sizeof(data), &len) != -1) {
4305 printf("\n\tstationname ");
4306 print_string(data, len);
4309 spacer = ' '; /* force first break */
4312 list_regdomain(s, 0);
4315 if (get80211val(s, IEEE80211_IOC_AUTHMODE, &val) != -1) {
4317 case IEEE80211_AUTH_NONE:
4318 LINE_CHECK("authmode NONE");
4320 case IEEE80211_AUTH_OPEN:
4321 LINE_CHECK("authmode OPEN");
4323 case IEEE80211_AUTH_SHARED:
4324 LINE_CHECK("authmode SHARED");
4326 case IEEE80211_AUTH_8021X:
4327 LINE_CHECK("authmode 802.1x");
4329 case IEEE80211_AUTH_WPA:
4330 if (get80211val(s, IEEE80211_IOC_WPA, &wpa) < 0)
4331 wpa = 1; /* default to WPA1 */
4334 LINE_CHECK("authmode WPA2/802.11i");
4337 LINE_CHECK("authmode WPA1+WPA2/802.11i");
4340 LINE_CHECK("authmode WPA");
4344 case IEEE80211_AUTH_AUTO:
4345 LINE_CHECK("authmode AUTO");
4348 LINE_CHECK("authmode UNKNOWN (0x%x)", val);
4353 if (wpa || verbose) {
4354 if (get80211val(s, IEEE80211_IOC_WPS, &val) != -1) {
4360 if (get80211val(s, IEEE80211_IOC_TSN, &val) != -1) {
4366 if (ioctl(s, IEEE80211_IOC_COUNTERMEASURES, &val) != -1) {
4368 LINE_CHECK("countermeasures");
4370 LINE_CHECK("-countermeasures");
4373 /* XXX not interesting with WPA done in user space */
4374 ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
4375 if (ioctl(s, SIOCG80211, &ireq) != -1) {
4378 ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
4379 if (ioctl(s, SIOCG80211, &ireq) != -1) {
4380 LINE_CHECK("mcastcipher ");
4381 printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
4385 ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
4386 if (ioctl(s, SIOCG80211, &ireq) != -1) {
4387 LINE_CHECK("ucastcipher ");
4388 printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
4392 ireq.i_type = IEEE80211_IOC_RSNCAPS;
4393 if (ioctl(s, SIOCG80211, &ireq) != -1) {
4394 LINE_CHECK("RSN caps 0x%x", ireq.i_val);
4399 ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
4400 if (ioctl(s, SIOCG80211, &ireq) != -1) {
4405 if (get80211val(s, IEEE80211_IOC_WEP, &wepmode) != -1 &&
4406 wepmode != IEEE80211_WEP_NOSUP) {
4409 case IEEE80211_WEP_OFF:
4410 LINE_CHECK("privacy OFF");
4412 case IEEE80211_WEP_ON:
4413 LINE_CHECK("privacy ON");
4415 case IEEE80211_WEP_MIXED:
4416 LINE_CHECK("privacy MIXED");
4419 LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
4424 * If we get here then we've got WEP support so we need
4425 * to print WEP status.
4428 if (get80211val(s, IEEE80211_IOC_WEPTXKEY, &val) < 0) {
4429 warn("WEP support, but no tx key!");
4433 LINE_CHECK("deftxkey %d", val+1);
4434 else if (wepmode != IEEE80211_WEP_OFF || verbose)
4435 LINE_CHECK("deftxkey UNDEF");
4437 if (get80211val(s, IEEE80211_IOC_NUMWEPKEYS, &num) < 0) {
4438 warn("WEP support, but no NUMWEPKEYS support!");
4442 for (i = 0; i < num; i++) {
4443 struct ieee80211req_key ik;
4445 memset(&ik, 0, sizeof(ik));
4447 if (get80211(s, IEEE80211_IOC_WPAKEY, &ik, sizeof(ik)) < 0) {
4448 warn("WEP support, but can get keys!");
4451 if (ik.ik_keylen != 0) {
4461 if (get80211val(s, IEEE80211_IOC_POWERSAVE, &val) != -1 &&
4462 val != IEEE80211_POWERSAVE_NOSUP ) {
4463 if (val != IEEE80211_POWERSAVE_OFF || verbose) {
4465 case IEEE80211_POWERSAVE_OFF:
4466 LINE_CHECK("powersavemode OFF");
4468 case IEEE80211_POWERSAVE_CAM:
4469 LINE_CHECK("powersavemode CAM");
4471 case IEEE80211_POWERSAVE_PSP:
4472 LINE_CHECK("powersavemode PSP");
4474 case IEEE80211_POWERSAVE_PSP_CAM:
4475 LINE_CHECK("powersavemode PSP-CAM");
4478 if (get80211val(s, IEEE80211_IOC_POWERSAVESLEEP, &val) != -1)
4479 LINE_CHECK("powersavesleep %d", val);
4483 if (get80211val(s, IEEE80211_IOC_TXPOWER, &val) != -1) {
4485 LINE_CHECK("txpower %d.5", val/2);
4487 LINE_CHECK("txpower %d", val/2);
4490 if (get80211val(s, IEEE80211_IOC_TXPOWMAX, &val) != -1)
4491 LINE_CHECK("txpowmax %.1f", val/2.);
4494 if (get80211val(s, IEEE80211_IOC_DOTD, &val) != -1) {
4498 LINE_CHECK("-dotd");
4501 if (get80211val(s, IEEE80211_IOC_RTSTHRESHOLD, &val) != -1) {
4502 if (val != IEEE80211_RTS_MAX || verbose)
4503 LINE_CHECK("rtsthreshold %d", val);
4506 if (get80211val(s, IEEE80211_IOC_FRAGTHRESHOLD, &val) != -1) {
4507 if (val != IEEE80211_FRAG_MAX || verbose)
4508 LINE_CHECK("fragthreshold %d", val);
4510 if (opmode == IEEE80211_M_STA || verbose) {
4511 if (get80211val(s, IEEE80211_IOC_BMISSTHRESHOLD, &val) != -1) {
4512 if (val != IEEE80211_HWBMISS_MAX || verbose)
4513 LINE_CHECK("bmiss %d", val);
4519 tp = &txparams.params[chan2mode(c)];
4520 printrate("ucastrate", tp->ucastrate,
4521 IEEE80211_FIXED_RATE_NONE, IEEE80211_FIXED_RATE_NONE);
4522 printrate("mcastrate", tp->mcastrate, 2*1,
4523 IEEE80211_RATE_MCS|0);
4524 printrate("mgmtrate", tp->mgmtrate, 2*1,
4525 IEEE80211_RATE_MCS|0);
4526 if (tp->maxretry != 6) /* XXX */
4527 LINE_CHECK("maxretry %d", tp->maxretry);
4533 bgscaninterval = -1;
4534 (void) get80211val(s, IEEE80211_IOC_BGSCAN_INTERVAL, &bgscaninterval);
4536 if (get80211val(s, IEEE80211_IOC_SCANVALID, &val) != -1) {
4537 if (val != bgscaninterval || verbose)
4538 LINE_CHECK("scanvalid %u", val);
4542 if (get80211val(s, IEEE80211_IOC_BGSCAN, &bgscan) != -1) {
4544 LINE_CHECK("bgscan");
4546 LINE_CHECK("-bgscan");
4548 if (bgscan || verbose) {
4549 if (bgscaninterval != -1)
4550 LINE_CHECK("bgscanintvl %u", bgscaninterval);
4551 if (get80211val(s, IEEE80211_IOC_BGSCAN_IDLE, &val) != -1)
4552 LINE_CHECK("bgscanidle %u", val);
4555 rp = &roamparams.params[chan2mode(c)];
4557 LINE_CHECK("roam:rssi %u.5", rp->rssi/2);
4559 LINE_CHECK("roam:rssi %u", rp->rssi/2);
4560 LINE_CHECK("roam:rate %s%u",
4561 (rp->rate & IEEE80211_RATE_MCS) ? "MCS " : "",
4562 get_rate_value(rp->rate));
4570 if (IEEE80211_IS_CHAN_ANYG(c) || verbose) {
4571 if (get80211val(s, IEEE80211_IOC_PUREG, &val) != -1) {
4573 LINE_CHECK("pureg");
4575 LINE_CHECK("-pureg");
4577 if (get80211val(s, IEEE80211_IOC_PROTMODE, &val) != -1) {
4579 case IEEE80211_PROTMODE_OFF:
4580 LINE_CHECK("protmode OFF");
4582 case IEEE80211_PROTMODE_CTS:
4583 LINE_CHECK("protmode CTS");
4585 case IEEE80211_PROTMODE_RTSCTS:
4586 LINE_CHECK("protmode RTSCTS");
4589 LINE_CHECK("protmode UNKNOWN (0x%x)", val);
4595 if (IEEE80211_IS_CHAN_HT(c) || verbose) {
4597 switch (htconf & 3) {
4610 if (get80211val(s, IEEE80211_IOC_HTCOMPAT, &val) != -1) {
4612 LINE_CHECK("-htcompat");
4614 LINE_CHECK("htcompat");
4616 if (get80211val(s, IEEE80211_IOC_AMPDU, &val) != -1) {
4619 LINE_CHECK("-ampdu");
4622 LINE_CHECK("ampdutx -ampdurx");
4625 LINE_CHECK("-ampdutx ampdurx");
4629 LINE_CHECK("ampdu");
4633 if (get80211val(s, IEEE80211_IOC_AMPDU_LIMIT, &val) != -1) {
4635 case IEEE80211_HTCAP_MAXRXAMPDU_8K:
4636 LINE_CHECK("ampdulimit 8k");
4638 case IEEE80211_HTCAP_MAXRXAMPDU_16K:
4639 LINE_CHECK("ampdulimit 16k");
4641 case IEEE80211_HTCAP_MAXRXAMPDU_32K:
4642 LINE_CHECK("ampdulimit 32k");
4644 case IEEE80211_HTCAP_MAXRXAMPDU_64K:
4645 LINE_CHECK("ampdulimit 64k");
4649 if (get80211val(s, IEEE80211_IOC_AMPDU_DENSITY, &val) != -1) {
4651 case IEEE80211_HTCAP_MPDUDENSITY_NA:
4653 LINE_CHECK("ampdudensity NA");
4655 case IEEE80211_HTCAP_MPDUDENSITY_025:
4656 LINE_CHECK("ampdudensity .25");
4658 case IEEE80211_HTCAP_MPDUDENSITY_05:
4659 LINE_CHECK("ampdudensity .5");
4661 case IEEE80211_HTCAP_MPDUDENSITY_1:
4662 LINE_CHECK("ampdudensity 1");
4664 case IEEE80211_HTCAP_MPDUDENSITY_2:
4665 LINE_CHECK("ampdudensity 2");
4667 case IEEE80211_HTCAP_MPDUDENSITY_4:
4668 LINE_CHECK("ampdudensity 4");
4670 case IEEE80211_HTCAP_MPDUDENSITY_8:
4671 LINE_CHECK("ampdudensity 8");
4673 case IEEE80211_HTCAP_MPDUDENSITY_16:
4674 LINE_CHECK("ampdudensity 16");
4678 if (get80211val(s, IEEE80211_IOC_AMSDU, &val) != -1) {
4681 LINE_CHECK("-amsdu");
4684 LINE_CHECK("amsdutx -amsdurx");
4687 LINE_CHECK("-amsdutx amsdurx");
4691 LINE_CHECK("amsdu");
4695 /* XXX amsdu limit */
4696 if (get80211val(s, IEEE80211_IOC_SHORTGI, &val) != -1) {
4698 LINE_CHECK("shortgi");
4700 LINE_CHECK("-shortgi");
4702 if (get80211val(s, IEEE80211_IOC_HTPROTMODE, &val) != -1) {
4703 if (val == IEEE80211_PROTMODE_OFF)
4704 LINE_CHECK("htprotmode OFF");
4705 else if (val != IEEE80211_PROTMODE_RTSCTS)
4706 LINE_CHECK("htprotmode UNKNOWN (0x%x)", val);
4708 LINE_CHECK("htprotmode RTSCTS");
4710 if (get80211val(s, IEEE80211_IOC_PUREN, &val) != -1) {
4712 LINE_CHECK("puren");
4714 LINE_CHECK("-puren");
4716 if (get80211val(s, IEEE80211_IOC_SMPS, &val) != -1) {
4717 if (val == IEEE80211_HTCAP_SMPS_DYNAMIC)
4718 LINE_CHECK("smpsdyn");
4719 else if (val == IEEE80211_HTCAP_SMPS_ENA)
4722 LINE_CHECK("-smps");
4724 if (get80211val(s, IEEE80211_IOC_RIFS, &val) != -1) {
4728 LINE_CHECK("-rifs");
4732 if (get80211val(s, IEEE80211_IOC_WME, &wme) != -1) {
4740 if (get80211val(s, IEEE80211_IOC_BURST, &val) != -1) {
4742 LINE_CHECK("burst");
4744 LINE_CHECK("-burst");
4747 if (get80211val(s, IEEE80211_IOC_FF, &val) != -1) {
4753 if (get80211val(s, IEEE80211_IOC_TURBOP, &val) != -1) {
4755 LINE_CHECK("dturbo");
4757 LINE_CHECK("-dturbo");
4759 if (get80211val(s, IEEE80211_IOC_DWDS, &val) != -1) {
4763 LINE_CHECK("-dwds");
4766 if (opmode == IEEE80211_M_HOSTAP) {
4767 if (get80211val(s, IEEE80211_IOC_HIDESSID, &val) != -1) {
4769 LINE_CHECK("hidessid");
4771 LINE_CHECK("-hidessid");
4773 if (get80211val(s, IEEE80211_IOC_APBRIDGE, &val) != -1) {
4775 LINE_CHECK("-apbridge");
4777 LINE_CHECK("apbridge");
4779 if (get80211val(s, IEEE80211_IOC_DTIM_PERIOD, &val) != -1)
4780 LINE_CHECK("dtimperiod %u", val);
4782 if (get80211val(s, IEEE80211_IOC_DOTH, &val) != -1) {
4784 LINE_CHECK("-doth");
4788 if (get80211val(s, IEEE80211_IOC_DFS, &val) != -1) {
4794 if (get80211val(s, IEEE80211_IOC_INACTIVITY, &val) != -1) {
4796 LINE_CHECK("-inact");
4798 LINE_CHECK("inact");
4801 if (get80211val(s, IEEE80211_IOC_ROAMING, &val) != -1) {
4802 if (val != IEEE80211_ROAMING_AUTO || verbose) {
4804 case IEEE80211_ROAMING_DEVICE:
4805 LINE_CHECK("roaming DEVICE");
4807 case IEEE80211_ROAMING_AUTO:
4808 LINE_CHECK("roaming AUTO");
4810 case IEEE80211_ROAMING_MANUAL:
4811 LINE_CHECK("roaming MANUAL");
4814 LINE_CHECK("roaming UNKNOWN (0x%x)",
4822 if (opmode == IEEE80211_M_AHDEMO) {
4823 if (get80211val(s, IEEE80211_IOC_TDMA_SLOT, &val) != -1)
4824 LINE_CHECK("tdmaslot %u", val);
4825 if (get80211val(s, IEEE80211_IOC_TDMA_SLOTCNT, &val) != -1)
4826 LINE_CHECK("tdmaslotcnt %u", val);
4827 if (get80211val(s, IEEE80211_IOC_TDMA_SLOTLEN, &val) != -1)
4828 LINE_CHECK("tdmaslotlen %u", val);
4829 if (get80211val(s, IEEE80211_IOC_TDMA_BINTERVAL, &val) != -1)
4830 LINE_CHECK("tdmabintval %u", val);
4831 } else if (get80211val(s, IEEE80211_IOC_BEACON_INTERVAL, &val) != -1) {
4832 /* XXX default define not visible */
4833 if (val != 100 || verbose)
4834 LINE_CHECK("bintval %u", val);
4837 if (wme && verbose) {
4842 if (opmode == IEEE80211_M_MBSS) {
4843 if (get80211val(s, IEEE80211_IOC_MESH_TTL, &val) != -1) {
4844 LINE_CHECK("meshttl %u", val);
4846 if (get80211val(s, IEEE80211_IOC_MESH_AP, &val) != -1) {
4848 LINE_CHECK("meshpeering");
4850 LINE_CHECK("-meshpeering");
4852 if (get80211val(s, IEEE80211_IOC_MESH_FWRD, &val) != -1) {
4854 LINE_CHECK("meshforward");
4856 LINE_CHECK("-meshforward");
4858 if (get80211val(s, IEEE80211_IOC_MESH_GATE, &val) != -1) {
4860 LINE_CHECK("meshgate");
4862 LINE_CHECK("-meshgate");
4864 if (get80211len(s, IEEE80211_IOC_MESH_PR_METRIC, data, 12,
4867 LINE_CHECK("meshmetric %s", data);
4869 if (get80211len(s, IEEE80211_IOC_MESH_PR_PATH, data, 12,
4872 LINE_CHECK("meshpath %s", data);
4874 if (get80211val(s, IEEE80211_IOC_HWMP_ROOTMODE, &val) != -1) {
4876 case IEEE80211_HWMP_ROOTMODE_DISABLED:
4877 LINE_CHECK("hwmprootmode DISABLED");
4879 case IEEE80211_HWMP_ROOTMODE_NORMAL:
4880 LINE_CHECK("hwmprootmode NORMAL");
4882 case IEEE80211_HWMP_ROOTMODE_PROACTIVE:
4883 LINE_CHECK("hwmprootmode PROACTIVE");
4885 case IEEE80211_HWMP_ROOTMODE_RANN:
4886 LINE_CHECK("hwmprootmode RANN");
4889 LINE_CHECK("hwmprootmode UNKNOWN(%d)", val);
4893 if (get80211val(s, IEEE80211_IOC_HWMP_MAXHOPS, &val) != -1) {
4894 LINE_CHECK("hwmpmaxhops %u", val);
4902 get80211(int s, int type, void *data, int len)
4904 struct ieee80211req ireq;
4906 (void) memset(&ireq, 0, sizeof(ireq));
4907 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4911 return ioctl(s, SIOCG80211, &ireq);
4915 get80211len(int s, int type, void *data, int len, int *plen)
4917 struct ieee80211req ireq;
4919 (void) memset(&ireq, 0, sizeof(ireq));
4920 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4923 assert(ireq.i_len == len); /* NB: check for 16-bit truncation */
4925 if (ioctl(s, SIOCG80211, &ireq) < 0)
4932 get80211val(int s, int type, int *val)
4934 struct ieee80211req ireq;
4936 (void) memset(&ireq, 0, sizeof(ireq));
4937 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4939 if (ioctl(s, SIOCG80211, &ireq) < 0)
4946 set80211(int s, int type, int val, int len, void *data)
4948 struct ieee80211req ireq;
4950 (void) memset(&ireq, 0, sizeof(ireq));
4951 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4955 assert(ireq.i_len == len); /* NB: check for 16-bit truncation */
4957 if (ioctl(s, SIOCS80211, &ireq) < 0)
4958 err(1, "SIOCS80211");
4962 get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
4970 hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
4976 if (sep != NULL && strchr(sep, *val) != NULL) {
4981 if (!isxdigit((u_char)val[0])) {
4982 warnx("bad hexadecimal digits");
4985 if (!isxdigit((u_char)val[1])) {
4986 warnx("odd count hexadecimal digits");
4990 if (p >= buf + len) {
4992 warnx("hexadecimal digits too long");
4994 warnx("string too long");
4998 #define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
4999 *p++ = (tohex((u_char)val[0]) << 4) |
5000 tohex((u_char)val[1]);
5007 /* The string "-" is treated as the empty string. */
5008 if (!hexstr && len == 1 && buf[0] == '-') {
5010 memset(buf, 0, *lenp);
5011 } else if (len < *lenp)
5012 memset(p, 0, *lenp - len);
5018 print_string(const u_int8_t *buf, int len)
5025 for (; i < len; i++) {
5026 if (!isprint(buf[i]) && buf[i] != '\0')
5028 if (isspace(buf[i]))
5032 if (hasspc || len == 0 || buf[0] == '\0')
5033 printf("\"%.*s\"", len, buf);
5035 printf("%.*s", len, buf);
5038 for (i = 0; i < len; i++)
5039 printf("%02x", buf[i]);
5044 * Virtual AP cloning support.
5046 static struct ieee80211_clone_params params = {
5047 .icp_opmode = IEEE80211_M_STA, /* default to station mode */
5051 wlan_create(int s, struct ifreq *ifr)
5053 static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
5055 if (params.icp_parent[0] == '\0')
5056 errx(1, "must specify a parent device (wlandev) when creating "
5058 if (params.icp_opmode == IEEE80211_M_WDS &&
5059 memcmp(params.icp_bssid, zerobssid, sizeof(zerobssid)) == 0)
5060 errx(1, "no bssid specified for WDS (use wlanbssid)");
5061 ifr->ifr_data = (caddr_t) ¶ms;
5062 if (ioctl(s, SIOCIFCREATE2, ifr) < 0)
5063 err(1, "SIOCIFCREATE2");
5067 DECL_CMD_FUNC(set80211clone_wlandev, arg, d)
5069 strlcpy(params.icp_parent, arg, IFNAMSIZ);
5073 DECL_CMD_FUNC(set80211clone_wlanbssid, arg, d)
5075 const struct ether_addr *ea;
5077 ea = ether_aton(arg);
5079 errx(1, "%s: cannot parse bssid", arg);
5080 memcpy(params.icp_bssid, ea->octet, IEEE80211_ADDR_LEN);
5084 DECL_CMD_FUNC(set80211clone_wlanaddr, arg, d)
5086 const struct ether_addr *ea;
5088 ea = ether_aton(arg);
5090 errx(1, "%s: cannot parse address", arg);
5091 memcpy(params.icp_macaddr, ea->octet, IEEE80211_ADDR_LEN);
5092 params.icp_flags |= IEEE80211_CLONE_MACADDR;
5096 DECL_CMD_FUNC(set80211clone_wlanmode, arg, d)
5098 #define iseq(a,b) (strncasecmp(a,b,sizeof(b)-1) == 0)
5099 if (iseq(arg, "sta"))
5100 params.icp_opmode = IEEE80211_M_STA;
5101 else if (iseq(arg, "ahdemo") || iseq(arg, "adhoc-demo"))
5102 params.icp_opmode = IEEE80211_M_AHDEMO;
5103 else if (iseq(arg, "ibss") || iseq(arg, "adhoc"))
5104 params.icp_opmode = IEEE80211_M_IBSS;
5105 else if (iseq(arg, "ap") || iseq(arg, "host"))
5106 params.icp_opmode = IEEE80211_M_HOSTAP;
5107 else if (iseq(arg, "wds"))
5108 params.icp_opmode = IEEE80211_M_WDS;
5109 else if (iseq(arg, "monitor"))
5110 params.icp_opmode = IEEE80211_M_MONITOR;
5111 else if (iseq(arg, "tdma")) {
5112 params.icp_opmode = IEEE80211_M_AHDEMO;
5113 params.icp_flags |= IEEE80211_CLONE_TDMA;
5114 } else if (iseq(arg, "mesh") || iseq(arg, "mp")) /* mesh point */
5115 params.icp_opmode = IEEE80211_M_MBSS;
5117 errx(1, "Don't know to create %s for %s", arg, name);
5122 set80211clone_beacons(const char *val, int d, int s, const struct afswtch *rafp)
5124 /* NB: inverted sense */
5126 params.icp_flags &= ~IEEE80211_CLONE_NOBEACONS;
5128 params.icp_flags |= IEEE80211_CLONE_NOBEACONS;
5132 set80211clone_bssid(const char *val, int d, int s, const struct afswtch *rafp)
5135 params.icp_flags |= IEEE80211_CLONE_BSSID;
5137 params.icp_flags &= ~IEEE80211_CLONE_BSSID;
5141 set80211clone_wdslegacy(const char *val, int d, int s, const struct afswtch *rafp)
5144 params.icp_flags |= IEEE80211_CLONE_WDSLEGACY;
5146 params.icp_flags &= ~IEEE80211_CLONE_WDSLEGACY;
5149 static struct cmd ieee80211_cmds[] = {
5150 DEF_CMD_ARG("ssid", set80211ssid),
5151 DEF_CMD_ARG("nwid", set80211ssid),
5152 DEF_CMD_ARG("meshid", set80211meshid),
5153 DEF_CMD_ARG("stationname", set80211stationname),
5154 DEF_CMD_ARG("station", set80211stationname), /* BSD/OS */
5155 DEF_CMD_ARG("channel", set80211channel),
5156 DEF_CMD_ARG("authmode", set80211authmode),
5157 DEF_CMD_ARG("powersavemode", set80211powersavemode),
5158 DEF_CMD("powersave", 1, set80211powersave),
5159 DEF_CMD("-powersave", 0, set80211powersave),
5160 DEF_CMD_ARG("powersavesleep", set80211powersavesleep),
5161 DEF_CMD_ARG("wepmode", set80211wepmode),
5162 DEF_CMD("wep", 1, set80211wep),
5163 DEF_CMD("-wep", 0, set80211wep),
5164 DEF_CMD_ARG("deftxkey", set80211weptxkey),
5165 DEF_CMD_ARG("weptxkey", set80211weptxkey),
5166 DEF_CMD_ARG("wepkey", set80211wepkey),
5167 DEF_CMD_ARG("nwkey", set80211nwkey), /* NetBSD */
5168 DEF_CMD("-nwkey", 0, set80211wep), /* NetBSD */
5169 DEF_CMD_ARG("rtsthreshold", set80211rtsthreshold),
5170 DEF_CMD_ARG("protmode", set80211protmode),
5171 DEF_CMD_ARG("txpower", set80211txpower),
5172 DEF_CMD_ARG("roaming", set80211roaming),
5173 DEF_CMD("wme", 1, set80211wme),
5174 DEF_CMD("-wme", 0, set80211wme),
5175 DEF_CMD("wmm", 1, set80211wme),
5176 DEF_CMD("-wmm", 0, set80211wme),
5177 DEF_CMD("hidessid", 1, set80211hidessid),
5178 DEF_CMD("-hidessid", 0, set80211hidessid),
5179 DEF_CMD("apbridge", 1, set80211apbridge),
5180 DEF_CMD("-apbridge", 0, set80211apbridge),
5181 DEF_CMD_ARG("chanlist", set80211chanlist),
5182 DEF_CMD_ARG("bssid", set80211bssid),
5183 DEF_CMD_ARG("ap", set80211bssid),
5184 DEF_CMD("scan", 0, set80211scan),
5185 DEF_CMD_ARG("list", set80211list),
5186 DEF_CMD_ARG2("cwmin", set80211cwmin),
5187 DEF_CMD_ARG2("cwmax", set80211cwmax),
5188 DEF_CMD_ARG2("aifs", set80211aifs),
5189 DEF_CMD_ARG2("txoplimit", set80211txoplimit),
5190 DEF_CMD_ARG("acm", set80211acm),
5191 DEF_CMD_ARG("-acm", set80211noacm),
5192 DEF_CMD_ARG("ack", set80211ackpolicy),
5193 DEF_CMD_ARG("-ack", set80211noackpolicy),
5194 DEF_CMD_ARG2("bss:cwmin", set80211bsscwmin),
5195 DEF_CMD_ARG2("bss:cwmax", set80211bsscwmax),
5196 DEF_CMD_ARG2("bss:aifs", set80211bssaifs),
5197 DEF_CMD_ARG2("bss:txoplimit", set80211bsstxoplimit),
5198 DEF_CMD_ARG("dtimperiod", set80211dtimperiod),
5199 DEF_CMD_ARG("bintval", set80211bintval),
5200 DEF_CMD("mac:open", IEEE80211_MACCMD_POLICY_OPEN, set80211maccmd),
5201 DEF_CMD("mac:allow", IEEE80211_MACCMD_POLICY_ALLOW, set80211maccmd),
5202 DEF_CMD("mac:deny", IEEE80211_MACCMD_POLICY_DENY, set80211maccmd),
5203 DEF_CMD("mac:radius", IEEE80211_MACCMD_POLICY_RADIUS, set80211maccmd),
5204 DEF_CMD("mac:flush", IEEE80211_MACCMD_FLUSH, set80211maccmd),
5205 DEF_CMD("mac:detach", IEEE80211_MACCMD_DETACH, set80211maccmd),
5206 DEF_CMD_ARG("mac:add", set80211addmac),
5207 DEF_CMD_ARG("mac:del", set80211delmac),
5208 DEF_CMD_ARG("mac:kick", set80211kickmac),
5209 DEF_CMD("pureg", 1, set80211pureg),
5210 DEF_CMD("-pureg", 0, set80211pureg),
5211 DEF_CMD("ff", 1, set80211fastframes),
5212 DEF_CMD("-ff", 0, set80211fastframes),
5213 DEF_CMD("dturbo", 1, set80211dturbo),
5214 DEF_CMD("-dturbo", 0, set80211dturbo),
5215 DEF_CMD("bgscan", 1, set80211bgscan),
5216 DEF_CMD("-bgscan", 0, set80211bgscan),
5217 DEF_CMD_ARG("bgscanidle", set80211bgscanidle),
5218 DEF_CMD_ARG("bgscanintvl", set80211bgscanintvl),
5219 DEF_CMD_ARG("scanvalid", set80211scanvalid),
5220 DEF_CMD("quiet", 1, set80211quiet),
5221 DEF_CMD("-quiet", 0, set80211quiet),
5222 DEF_CMD_ARG("quiet_count", set80211quietcount),
5223 DEF_CMD_ARG("quiet_period", set80211quietperiod),
5224 DEF_CMD_ARG("quiet_dur", set80211quietduration),
5225 DEF_CMD_ARG("quiet_offset", set80211quietoffset),
5226 DEF_CMD_ARG("roam:rssi", set80211roamrssi),
5227 DEF_CMD_ARG("roam:rate", set80211roamrate),
5228 DEF_CMD_ARG("mcastrate", set80211mcastrate),
5229 DEF_CMD_ARG("ucastrate", set80211ucastrate),
5230 DEF_CMD_ARG("mgtrate", set80211mgtrate),
5231 DEF_CMD_ARG("mgmtrate", set80211mgtrate),
5232 DEF_CMD_ARG("maxretry", set80211maxretry),
5233 DEF_CMD_ARG("fragthreshold", set80211fragthreshold),
5234 DEF_CMD("burst", 1, set80211burst),
5235 DEF_CMD("-burst", 0, set80211burst),
5236 DEF_CMD_ARG("bmiss", set80211bmissthreshold),
5237 DEF_CMD_ARG("bmissthreshold", set80211bmissthreshold),
5238 DEF_CMD("shortgi", 1, set80211shortgi),
5239 DEF_CMD("-shortgi", 0, set80211shortgi),
5240 DEF_CMD("ampdurx", 2, set80211ampdu),
5241 DEF_CMD("-ampdurx", -2, set80211ampdu),
5242 DEF_CMD("ampdutx", 1, set80211ampdu),
5243 DEF_CMD("-ampdutx", -1, set80211ampdu),
5244 DEF_CMD("ampdu", 3, set80211ampdu), /* NB: tx+rx */
5245 DEF_CMD("-ampdu", -3, set80211ampdu),
5246 DEF_CMD_ARG("ampdulimit", set80211ampdulimit),
5247 DEF_CMD_ARG("ampdudensity", set80211ampdudensity),
5248 DEF_CMD("amsdurx", 2, set80211amsdu),
5249 DEF_CMD("-amsdurx", -2, set80211amsdu),
5250 DEF_CMD("amsdutx", 1, set80211amsdu),
5251 DEF_CMD("-amsdutx", -1, set80211amsdu),
5252 DEF_CMD("amsdu", 3, set80211amsdu), /* NB: tx+rx */
5253 DEF_CMD("-amsdu", -3, set80211amsdu),
5254 DEF_CMD_ARG("amsdulimit", set80211amsdulimit),
5255 DEF_CMD("puren", 1, set80211puren),
5256 DEF_CMD("-puren", 0, set80211puren),
5257 DEF_CMD("doth", 1, set80211doth),
5258 DEF_CMD("-doth", 0, set80211doth),
5259 DEF_CMD("dfs", 1, set80211dfs),
5260 DEF_CMD("-dfs", 0, set80211dfs),
5261 DEF_CMD("htcompat", 1, set80211htcompat),
5262 DEF_CMD("-htcompat", 0, set80211htcompat),
5263 DEF_CMD("dwds", 1, set80211dwds),
5264 DEF_CMD("-dwds", 0, set80211dwds),
5265 DEF_CMD("inact", 1, set80211inact),
5266 DEF_CMD("-inact", 0, set80211inact),
5267 DEF_CMD("tsn", 1, set80211tsn),
5268 DEF_CMD("-tsn", 0, set80211tsn),
5269 DEF_CMD_ARG("regdomain", set80211regdomain),
5270 DEF_CMD_ARG("country", set80211country),
5271 DEF_CMD("indoor", 'I', set80211location),
5272 DEF_CMD("-indoor", 'O', set80211location),
5273 DEF_CMD("outdoor", 'O', set80211location),
5274 DEF_CMD("-outdoor", 'I', set80211location),
5275 DEF_CMD("anywhere", ' ', set80211location),
5276 DEF_CMD("ecm", 1, set80211ecm),
5277 DEF_CMD("-ecm", 0, set80211ecm),
5278 DEF_CMD("dotd", 1, set80211dotd),
5279 DEF_CMD("-dotd", 0, set80211dotd),
5280 DEF_CMD_ARG("htprotmode", set80211htprotmode),
5281 DEF_CMD("ht20", 1, set80211htconf),
5282 DEF_CMD("-ht20", 0, set80211htconf),
5283 DEF_CMD("ht40", 3, set80211htconf), /* NB: 20+40 */
5284 DEF_CMD("-ht40", 0, set80211htconf),
5285 DEF_CMD("ht", 3, set80211htconf), /* NB: 20+40 */
5286 DEF_CMD("-ht", 0, set80211htconf),
5287 DEF_CMD("rifs", 1, set80211rifs),
5288 DEF_CMD("-rifs", 0, set80211rifs),
5289 DEF_CMD("smps", IEEE80211_HTCAP_SMPS_ENA, set80211smps),
5290 DEF_CMD("smpsdyn", IEEE80211_HTCAP_SMPS_DYNAMIC, set80211smps),
5291 DEF_CMD("-smps", IEEE80211_HTCAP_SMPS_OFF, set80211smps),
5292 /* XXX for testing */
5293 DEF_CMD_ARG("chanswitch", set80211chanswitch),
5295 DEF_CMD_ARG("tdmaslot", set80211tdmaslot),
5296 DEF_CMD_ARG("tdmaslotcnt", set80211tdmaslotcnt),
5297 DEF_CMD_ARG("tdmaslotlen", set80211tdmaslotlen),
5298 DEF_CMD_ARG("tdmabintval", set80211tdmabintval),
5300 DEF_CMD_ARG("meshttl", set80211meshttl),
5301 DEF_CMD("meshforward", 1, set80211meshforward),
5302 DEF_CMD("-meshforward", 0, set80211meshforward),
5303 DEF_CMD("meshgate", 1, set80211meshgate),
5304 DEF_CMD("-meshgate", 0, set80211meshgate),
5305 DEF_CMD("meshpeering", 1, set80211meshpeering),
5306 DEF_CMD("-meshpeering", 0, set80211meshpeering),
5307 DEF_CMD_ARG("meshmetric", set80211meshmetric),
5308 DEF_CMD_ARG("meshpath", set80211meshpath),
5309 DEF_CMD("meshrt:flush", IEEE80211_MESH_RTCMD_FLUSH, set80211meshrtcmd),
5310 DEF_CMD_ARG("meshrt:add", set80211addmeshrt),
5311 DEF_CMD_ARG("meshrt:del", set80211delmeshrt),
5312 DEF_CMD_ARG("hwmprootmode", set80211hwmprootmode),
5313 DEF_CMD_ARG("hwmpmaxhops", set80211hwmpmaxhops),
5315 /* vap cloning support */
5316 DEF_CLONE_CMD_ARG("wlanaddr", set80211clone_wlanaddr),
5317 DEF_CLONE_CMD_ARG("wlanbssid", set80211clone_wlanbssid),
5318 DEF_CLONE_CMD_ARG("wlandev", set80211clone_wlandev),
5319 DEF_CLONE_CMD_ARG("wlanmode", set80211clone_wlanmode),
5320 DEF_CLONE_CMD("beacons", 1, set80211clone_beacons),
5321 DEF_CLONE_CMD("-beacons", 0, set80211clone_beacons),
5322 DEF_CLONE_CMD("bssid", 1, set80211clone_bssid),
5323 DEF_CLONE_CMD("-bssid", 0, set80211clone_bssid),
5324 DEF_CLONE_CMD("wdslegacy", 1, set80211clone_wdslegacy),
5325 DEF_CLONE_CMD("-wdslegacy", 0, set80211clone_wdslegacy),
5327 static struct afswtch af_ieee80211 = {
5328 .af_name = "af_ieee80211",
5330 .af_other_status = ieee80211_status,
5333 static __constructor void
5334 ieee80211_ctor(void)
5338 for (i = 0; i < nitems(ieee80211_cmds); i++)
5339 cmd_register(&ieee80211_cmds[i]);
5340 af_register(&af_ieee80211);
5341 clone_setdefcallback("wlan", wlan_create);