source: rtems-libbsd/freebsd/sbin/ifconfig/ifieee80211.c @ 2389539

55-freebsd-126-freebsd-12
Last change on this file since 2389539 was 2389539, checked in by Christian Mauderer <Christian.Mauderer@…>, on 11/16/16 at 14:02:20

ifconfig/wlan, expat, lib80211: Import from FreeBSD.

  • Property mode set to 100644
File size: 145.2 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*
4 * Copyright 2001 The Aerospace Corporation.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of The Aerospace Corporation may not be used to endorse or
15 *    promote products derived from this software.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD$
30 */
31
32/*-
33 * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
38 * NASA Ames Research Center.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 *    notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 *    notice, this list of conditions and the following disclaimer in the
47 *    documentation and/or other materials provided with the distribution.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
50 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
53 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 * POSSIBILITY OF SUCH DAMAGE.
60 */
61
62#include <rtems/bsd/sys/param.h>
63#include <sys/ioctl.h>
64#include <sys/socket.h>
65#include <sys/sysctl.h>
66#include <sys/time.h>
67
68#include <net/ethernet.h>
69#include <net/if.h>
70#include <net/if_dl.h>
71#include <net/if_types.h>
72#include <net/if_media.h>
73#include <net/route.h>
74
75#include <net80211/ieee80211_ioctl.h>
76#include <net80211/ieee80211_freebsd.h>
77#include <net80211/ieee80211_superg.h>
78#include <net80211/ieee80211_tdma.h>
79#include <net80211/ieee80211_mesh.h>
80
81#include <assert.h>
82#include <ctype.h>
83#include <err.h>
84#include <errno.h>
85#include <fcntl.h>
86#include <inttypes.h>
87#include <stdio.h>
88#include <stdlib.h>
89#include <string.h>
90#include <unistd.h>
91#include <stdarg.h>
92#include <stddef.h>             /* NB: for offsetof */
93
94#include "ifconfig.h"
95
96#include <lib80211/lib80211_regdomain.h>
97#include <lib80211/lib80211_ioctl.h>
98
99#ifndef IEEE80211_FIXED_RATE_NONE
100#define IEEE80211_FIXED_RATE_NONE       0xff
101#endif
102
103/* XXX need these publicly defined or similar */
104#ifndef IEEE80211_NODE_AUTH
105#define IEEE80211_NODE_AUTH     0x000001        /* authorized for data */
106#define IEEE80211_NODE_QOS      0x000002        /* QoS enabled */
107#define IEEE80211_NODE_ERP      0x000004        /* ERP enabled */
108#define IEEE80211_NODE_PWR_MGT  0x000010        /* power save mode enabled */
109#define IEEE80211_NODE_AREF     0x000020        /* authentication ref held */
110#define IEEE80211_NODE_HT       0x000040        /* HT enabled */
111#define IEEE80211_NODE_HTCOMPAT 0x000080        /* HT setup w/ vendor OUI's */
112#define IEEE80211_NODE_WPS      0x000100        /* WPS association */
113#define IEEE80211_NODE_TSN      0x000200        /* TSN association */
114#define IEEE80211_NODE_AMPDU_RX 0x000400        /* AMPDU rx enabled */
115#define IEEE80211_NODE_AMPDU_TX 0x000800        /* AMPDU tx enabled */
116#define IEEE80211_NODE_MIMO_PS  0x001000        /* MIMO power save enabled */
117#define IEEE80211_NODE_MIMO_RTS 0x002000        /* send RTS in MIMO PS */
118#define IEEE80211_NODE_RIFS     0x004000        /* RIFS enabled */
119#define IEEE80211_NODE_SGI20    0x008000        /* Short GI in HT20 enabled */
120#define IEEE80211_NODE_SGI40    0x010000        /* Short GI in HT40 enabled */
121#define IEEE80211_NODE_ASSOCID  0x020000        /* xmit requires associd */
122#define IEEE80211_NODE_AMSDU_RX 0x040000        /* AMSDU rx enabled */
123#define IEEE80211_NODE_AMSDU_TX 0x080000        /* AMSDU tx enabled */
124#define IEEE80211_NODE_VHT      0x100000        /* VHT enabled */
125#endif
126
127#define MAXCHAN 1536            /* max 1.5K channels */
128
129#define MAXCOL  78
130static  int col;
131static  char spacer;
132
133static void LINE_INIT(char c);
134static void LINE_BREAK(void);
135static void LINE_CHECK(const char *fmt, ...);
136
137static const char *modename[IEEE80211_MODE_MAX] = {
138        [IEEE80211_MODE_AUTO]     = "auto",
139        [IEEE80211_MODE_11A]      = "11a",
140        [IEEE80211_MODE_11B]      = "11b",
141        [IEEE80211_MODE_11G]      = "11g",
142        [IEEE80211_MODE_FH]       = "fh",
143        [IEEE80211_MODE_TURBO_A]  = "turboA",
144        [IEEE80211_MODE_TURBO_G]  = "turboG",
145        [IEEE80211_MODE_STURBO_A] = "sturbo",
146        [IEEE80211_MODE_11NA]     = "11na",
147        [IEEE80211_MODE_11NG]     = "11ng",
148        [IEEE80211_MODE_HALF]     = "half",
149        [IEEE80211_MODE_QUARTER]  = "quarter",
150        [IEEE80211_MODE_VHT_2GHZ] = "11acg",
151        [IEEE80211_MODE_VHT_5GHZ] = "11ac",
152};
153
154static void set80211(int s, int type, int val, int len, void *data);
155static int get80211(int s, int type, void *data, int len);
156static int get80211len(int s, int type, void *data, int len, int *plen);
157static int get80211val(int s, int type, int *val);
158static const char *get_string(const char *val, const char *sep,
159    u_int8_t *buf, int *lenp);
160static void print_string(const u_int8_t *buf, int len);
161static void print_regdomain(const struct ieee80211_regdomain *, int);
162static void print_channels(int, const struct ieee80211req_chaninfo *,
163    int allchans, int verbose);
164static void regdomain_makechannels(struct ieee80211_regdomain_req *,
165    const struct ieee80211_devcaps_req *);
166static const char *mesh_linkstate_string(uint8_t state);
167
168static struct ieee80211req_chaninfo *chaninfo;
169static struct ieee80211_regdomain regdomain;
170static int gotregdomain = 0;
171static struct ieee80211_roamparams_req roamparams;
172static int gotroam = 0;
173static struct ieee80211_txparams_req txparams;
174static int gottxparams = 0;
175static struct ieee80211_channel curchan;
176static int gotcurchan = 0;
177static struct ifmediareq *ifmr;
178static int htconf = 0;
179static  int gothtconf = 0;
180
181static void
182gethtconf(int s)
183{
184        if (gothtconf)
185                return;
186        if (get80211val(s, IEEE80211_IOC_HTCONF, &htconf) < 0)
187                warn("unable to get HT configuration information");
188        gothtconf = 1;
189}
190
191/* VHT */
192static int vhtconf = 0;
193static  int gotvhtconf = 0;
194
195static void
196getvhtconf(int s)
197{
198        if (gotvhtconf)
199                return;
200        if (get80211val(s, IEEE80211_IOC_VHTCONF, &vhtconf) < 0)
201                warn("unable to get VHT configuration information");
202        gotvhtconf = 1;
203}
204
205/*
206 * Collect channel info from the kernel.  We use this (mostly)
207 * to handle mapping between frequency and IEEE channel number.
208 */
209static void
210getchaninfo(int s)
211{
212        if (chaninfo != NULL)
213                return;
214        chaninfo = malloc(IEEE80211_CHANINFO_SIZE(MAXCHAN));
215        if (chaninfo == NULL)
216                errx(1, "no space for channel list");
217        if (get80211(s, IEEE80211_IOC_CHANINFO, chaninfo,
218            IEEE80211_CHANINFO_SIZE(MAXCHAN)) < 0)
219                err(1, "unable to get channel information");
220        ifmr = ifmedia_getstate(s);
221        gethtconf(s);
222        getvhtconf(s);
223}
224
225static struct regdata *
226getregdata(void)
227{
228        static struct regdata *rdp = NULL;
229        if (rdp == NULL) {
230                rdp = lib80211_alloc_regdata();
231                if (rdp == NULL)
232                        errx(-1, "missing or corrupted regdomain database");
233        }
234        return rdp;
235}
236
237/*
238 * Given the channel at index i with attributes from,
239 * check if there is a channel with attributes to in
240 * the channel table.  With suitable attributes this
241 * allows the caller to look for promotion; e.g. from
242 * 11b > 11g.
243 */
244static int
245canpromote(int i, int from, int to)
246{
247        const struct ieee80211_channel *fc = &chaninfo->ic_chans[i];
248        u_int j;
249
250        if ((fc->ic_flags & from) != from)
251                return i;
252        /* NB: quick check exploiting ordering of chans w/ same frequency */
253        if (i+1 < chaninfo->ic_nchans &&
254            chaninfo->ic_chans[i+1].ic_freq == fc->ic_freq &&
255            (chaninfo->ic_chans[i+1].ic_flags & to) == to)
256                return i+1;
257        /* brute force search in case channel list is not ordered */
258        for (j = 0; j < chaninfo->ic_nchans; j++) {
259                const struct ieee80211_channel *tc = &chaninfo->ic_chans[j];
260                if (j != i &&
261                    tc->ic_freq == fc->ic_freq && (tc->ic_flags & to) == to)
262                return j;
263        }
264        return i;
265}
266
267/*
268 * Handle channel promotion.  When a channel is specified with
269 * only a frequency we want to promote it to the ``best'' channel
270 * available.  The channel list has separate entries for 11b, 11g,
271 * 11a, and 11n[ga] channels so specifying a frequency w/o any
272 * attributes requires we upgrade, e.g. from 11b -> 11g.  This
273 * gets complicated when the channel is specified on the same
274 * command line with a media request that constrains the available
275 * channe list (e.g. mode 11a); we want to honor that to avoid
276 * confusing behaviour.
277 */
278/*
279 * XXX VHT
280 */
281static int
282promote(int i)
283{
284        /*
285         * Query the current mode of the interface in case it's
286         * constrained (e.g. to 11a).  We must do this carefully
287         * as there may be a pending ifmedia request in which case
288         * asking the kernel will give us the wrong answer.  This
289         * is an unfortunate side-effect of the way ifconfig is
290         * structure for modularity (yech).
291         *
292         * NB: ifmr is actually setup in getchaninfo (above); we
293         *     assume it's called coincident with to this call so
294         *     we have a ``current setting''; otherwise we must pass
295         *     the socket descriptor down to here so we can make
296         *     the ifmedia_getstate call ourselves.
297         */
298        int chanmode = ifmr != NULL ? IFM_MODE(ifmr->ifm_current) : IFM_AUTO;
299
300        /* when ambiguous promote to ``best'' */
301        /* NB: we abitrarily pick HT40+ over HT40- */
302        if (chanmode != IFM_IEEE80211_11B)
303                i = canpromote(i, IEEE80211_CHAN_B, IEEE80211_CHAN_G);
304        if (chanmode != IFM_IEEE80211_11G && (htconf & 1)) {
305                i = canpromote(i, IEEE80211_CHAN_G,
306                        IEEE80211_CHAN_G | IEEE80211_CHAN_HT20);
307                if (htconf & 2) {
308                        i = canpromote(i, IEEE80211_CHAN_G,
309                                IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D);
310                        i = canpromote(i, IEEE80211_CHAN_G,
311                                IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U);
312                }
313        }
314        if (chanmode != IFM_IEEE80211_11A && (htconf & 1)) {
315                i = canpromote(i, IEEE80211_CHAN_A,
316                        IEEE80211_CHAN_A | IEEE80211_CHAN_HT20);
317                if (htconf & 2) {
318                        i = canpromote(i, IEEE80211_CHAN_A,
319                                IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D);
320                        i = canpromote(i, IEEE80211_CHAN_A,
321                                IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U);
322                }
323        }
324        return i;
325}
326
327static void
328mapfreq(struct ieee80211_channel *chan, int freq, int flags)
329{
330        u_int i;
331
332        for (i = 0; i < chaninfo->ic_nchans; i++) {
333                const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
334
335                if (c->ic_freq == freq && (c->ic_flags & flags) == flags) {
336                        if (flags == 0) {
337                                /* when ambiguous promote to ``best'' */
338                                c = &chaninfo->ic_chans[promote(i)];
339                        }
340                        *chan = *c;
341                        return;
342                }
343        }
344        errx(1, "unknown/undefined frequency %u/0x%x", freq, flags);
345}
346
347static void
348mapchan(struct ieee80211_channel *chan, int ieee, int flags)
349{
350        u_int i;
351
352        for (i = 0; i < chaninfo->ic_nchans; i++) {
353                const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
354
355                if (c->ic_ieee == ieee && (c->ic_flags & flags) == flags) {
356                        if (flags == 0) {
357                                /* when ambiguous promote to ``best'' */
358                                c = &chaninfo->ic_chans[promote(i)];
359                        }
360                        *chan = *c;
361                        return;
362                }
363        }
364        errx(1, "unknown/undefined channel number %d flags 0x%x", ieee, flags);
365}
366
367static const struct ieee80211_channel *
368getcurchan(int s)
369{
370        if (gotcurchan)
371                return &curchan;
372        if (get80211(s, IEEE80211_IOC_CURCHAN, &curchan, sizeof(curchan)) < 0) {
373                int val;
374                /* fall back to legacy ioctl */
375                if (get80211val(s, IEEE80211_IOC_CHANNEL, &val) < 0)
376                        err(-1, "cannot figure out current channel");
377                getchaninfo(s);
378                mapchan(&curchan, val, 0);
379        }
380        gotcurchan = 1;
381        return &curchan;
382}
383
384static enum ieee80211_phymode
385chan2mode(const struct ieee80211_channel *c)
386{
387        if (IEEE80211_IS_CHAN_VHTA(c))
388                return IEEE80211_MODE_VHT_5GHZ;
389        if (IEEE80211_IS_CHAN_VHTG(c))
390                return IEEE80211_MODE_VHT_2GHZ;
391        if (IEEE80211_IS_CHAN_HTA(c))
392                return IEEE80211_MODE_11NA;
393        if (IEEE80211_IS_CHAN_HTG(c))
394                return IEEE80211_MODE_11NG;
395        if (IEEE80211_IS_CHAN_108A(c))
396                return IEEE80211_MODE_TURBO_A;
397        if (IEEE80211_IS_CHAN_108G(c))
398                return IEEE80211_MODE_TURBO_G;
399        if (IEEE80211_IS_CHAN_ST(c))
400                return IEEE80211_MODE_STURBO_A;
401        if (IEEE80211_IS_CHAN_FHSS(c))
402                return IEEE80211_MODE_FH;
403        if (IEEE80211_IS_CHAN_HALF(c))
404                return IEEE80211_MODE_HALF;
405        if (IEEE80211_IS_CHAN_QUARTER(c))
406                return IEEE80211_MODE_QUARTER;
407        if (IEEE80211_IS_CHAN_A(c))
408                return IEEE80211_MODE_11A;
409        if (IEEE80211_IS_CHAN_ANYG(c))
410                return IEEE80211_MODE_11G;
411        if (IEEE80211_IS_CHAN_B(c))
412                return IEEE80211_MODE_11B;
413        return IEEE80211_MODE_AUTO;
414}
415
416static void
417getroam(int s)
418{
419        if (gotroam)
420                return;
421        if (get80211(s, IEEE80211_IOC_ROAM,
422            &roamparams, sizeof(roamparams)) < 0)
423                err(1, "unable to get roaming parameters");
424        gotroam = 1;
425}
426
427static void
428setroam_cb(int s, void *arg)
429{
430        struct ieee80211_roamparams_req *roam = arg;
431        set80211(s, IEEE80211_IOC_ROAM, 0, sizeof(*roam), roam);
432}
433
434static void
435gettxparams(int s)
436{
437        if (gottxparams)
438                return;
439        if (get80211(s, IEEE80211_IOC_TXPARAMS,
440            &txparams, sizeof(txparams)) < 0)
441                err(1, "unable to get transmit parameters");
442        gottxparams = 1;
443}
444
445static void
446settxparams_cb(int s, void *arg)
447{
448        struct ieee80211_txparams_req *txp = arg;
449        set80211(s, IEEE80211_IOC_TXPARAMS, 0, sizeof(*txp), txp);
450}
451
452static void
453getregdomain(int s)
454{
455        if (gotregdomain)
456                return;
457        if (get80211(s, IEEE80211_IOC_REGDOMAIN,
458            &regdomain, sizeof(regdomain)) < 0)
459                err(1, "unable to get regulatory domain info");
460        gotregdomain = 1;
461}
462
463static void
464getdevcaps(int s, struct ieee80211_devcaps_req *dc)
465{
466        if (get80211(s, IEEE80211_IOC_DEVCAPS, dc,
467            IEEE80211_DEVCAPS_SPACE(dc)) < 0)
468                err(1, "unable to get device capabilities");
469}
470
471static void
472setregdomain_cb(int s, void *arg)
473{
474        struct ieee80211_regdomain_req *req;
475        struct ieee80211_regdomain *rd = arg;
476        struct ieee80211_devcaps_req *dc;
477        struct regdata *rdp = getregdata();
478
479        if (rd->country != NO_COUNTRY) {
480                const struct country *cc;
481                /*
482                 * Check current country seting to make sure it's
483                 * compatible with the new regdomain.  If not, then
484                 * override it with any default country for this
485                 * SKU.  If we cannot arrange a match, then abort.
486                 */
487                cc = lib80211_country_findbycc(rdp, rd->country);
488                if (cc == NULL)
489                        errx(1, "unknown ISO country code %d", rd->country);
490                if (cc->rd->sku != rd->regdomain) {
491                        const struct regdomain *rp;
492                        /*
493                         * Check if country is incompatible with regdomain.
494                         * To enable multiple regdomains for a country code
495                         * we permit a mismatch between the regdomain and
496                         * the country's associated regdomain when the
497                         * regdomain is setup w/o a default country.  For
498                         * example, US is bound to the FCC regdomain but
499                         * we allow US to be combined with FCC3 because FCC3
500                         * has not default country.  This allows bogus
501                         * combinations like FCC3+DK which are resolved when
502                         * constructing the channel list by deferring to the
503                         * regdomain to construct the channel list.
504                         */
505                        rp = lib80211_regdomain_findbysku(rdp, rd->regdomain);
506                        if (rp == NULL)
507                                errx(1, "country %s (%s) is not usable with "
508                                    "regdomain %d", cc->isoname, cc->name,
509                                    rd->regdomain);
510                        else if (rp->cc != NULL && rp->cc != cc)
511                                errx(1, "country %s (%s) is not usable with "
512                                   "regdomain %s", cc->isoname, cc->name,
513                                   rp->name);
514                }
515        }
516        /*
517         * Fetch the device capabilities and calculate the
518         * full set of netbands for which we request a new
519         * channel list be constructed.  Once that's done we
520         * push the regdomain info + channel list to the kernel.
521         */
522        dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
523        if (dc == NULL)
524                errx(1, "no space for device capabilities");
525        dc->dc_chaninfo.ic_nchans = MAXCHAN;
526        getdevcaps(s, dc);
527#if 0
528        if (verbose) {
529                printf("drivercaps: 0x%x\n", dc->dc_drivercaps);
530                printf("cryptocaps: 0x%x\n", dc->dc_cryptocaps);
531                printf("htcaps    : 0x%x\n", dc->dc_htcaps);
532                printf("vhtcaps   : 0x%x\n", dc->dc_vhtcaps);
533#if 0
534                memcpy(chaninfo, &dc->dc_chaninfo,
535                    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
536                print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, 1/*verbose*/);
537#endif
538        }
539#endif
540        req = malloc(IEEE80211_REGDOMAIN_SIZE(dc->dc_chaninfo.ic_nchans));
541        if (req == NULL)
542                errx(1, "no space for regdomain request");
543        req->rd = *rd;
544        regdomain_makechannels(req, dc);
545        if (verbose) {
546                LINE_INIT(':');
547                print_regdomain(rd, 1/*verbose*/);
548                LINE_BREAK();
549                /* blech, reallocate channel list for new data */
550                if (chaninfo != NULL)
551                        free(chaninfo);
552                chaninfo = malloc(IEEE80211_CHANINFO_SPACE(&req->chaninfo));
553                if (chaninfo == NULL)
554                        errx(1, "no space for channel list");
555                memcpy(chaninfo, &req->chaninfo,
556                    IEEE80211_CHANINFO_SPACE(&req->chaninfo));
557                print_channels(s, &req->chaninfo, 1/*allchans*/, 1/*verbose*/);
558        }
559        if (req->chaninfo.ic_nchans == 0)
560                errx(1, "no channels calculated");
561        set80211(s, IEEE80211_IOC_REGDOMAIN, 0,
562            IEEE80211_REGDOMAIN_SPACE(req), req);
563        free(req);
564        free(dc);
565}
566
567static int
568ieee80211_mhz2ieee(int freq, int flags)
569{
570        struct ieee80211_channel chan;
571        mapfreq(&chan, freq, flags);
572        return chan.ic_ieee;
573}
574
575static int
576isanyarg(const char *arg)
577{
578        return (strncmp(arg, "-", 1) == 0 ||
579            strncasecmp(arg, "any", 3) == 0 || strncasecmp(arg, "off", 3) == 0);
580}
581
582static void
583set80211ssid(const char *val, int d, int s, const struct afswtch *rafp)
584{
585        int             ssid;
586        int             len;
587        u_int8_t        data[IEEE80211_NWID_LEN];
588
589        ssid = 0;
590        len = strlen(val);
591        if (len > 2 && isdigit((int)val[0]) && val[1] == ':') {
592                ssid = atoi(val)-1;
593                val += 2;
594        }
595
596        bzero(data, sizeof(data));
597        len = sizeof(data);
598        if (get_string(val, NULL, data, &len) == NULL)
599                exit(1);
600
601        set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
602}
603
604static void
605set80211meshid(const char *val, int d, int s, const struct afswtch *rafp)
606{
607        int             len;
608        u_int8_t        data[IEEE80211_NWID_LEN];
609
610        memset(data, 0, sizeof(data));
611        len = sizeof(data);
612        if (get_string(val, NULL, data, &len) == NULL)
613                exit(1);
614
615        set80211(s, IEEE80211_IOC_MESH_ID, 0, len, data);
616}       
617
618static void
619set80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
620{
621        int                     len;
622        u_int8_t                data[33];
623
624        bzero(data, sizeof(data));
625        len = sizeof(data);
626        get_string(val, NULL, data, &len);
627
628        set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
629}
630
631/*
632 * Parse a channel specification for attributes/flags.
633 * The syntax is:
634 *      freq/xx         channel width (5,10,20,40,40+,40-)
635 *      freq:mode       channel mode (a,b,g,h,n,t,s,d)
636 *
637 * These can be combined in either order; e.g. 2437:ng/40.
638 * Modes are case insensitive.
639 *
640 * The result is not validated here; it's assumed to be
641 * checked against the channel table fetched from the kernel.
642 */
643static int
644getchannelflags(const char *val, int freq)
645{
646#define _CHAN_HT        0x80000000
647        const char *cp;
648        int flags;
649        int is_vht = 0;
650
651        flags = 0;
652
653        cp = strchr(val, ':');
654        if (cp != NULL) {
655                for (cp++; isalpha((int) *cp); cp++) {
656                        /* accept mixed case */
657                        int c = *cp;
658                        if (isupper(c))
659                                c = tolower(c);
660                        switch (c) {
661                        case 'a':               /* 802.11a */
662                                flags |= IEEE80211_CHAN_A;
663                                break;
664                        case 'b':               /* 802.11b */
665                                flags |= IEEE80211_CHAN_B;
666                                break;
667                        case 'g':               /* 802.11g */
668                                flags |= IEEE80211_CHAN_G;
669                                break;
670                        case 'v':               /* vht: 802.11ac */
671                                is_vht = 1;
672                                /* Fallthrough */
673                        case 'h':               /* ht = 802.11n */
674                        case 'n':               /* 802.11n */
675                                flags |= _CHAN_HT;      /* NB: private */
676                                break;
677                        case 'd':               /* dt = Atheros Dynamic Turbo */
678                                flags |= IEEE80211_CHAN_TURBO;
679                                break;
680                        case 't':               /* ht, dt, st, t */
681                                /* dt and unadorned t specify Dynamic Turbo */
682                                if ((flags & (IEEE80211_CHAN_STURBO|_CHAN_HT)) == 0)
683                                        flags |= IEEE80211_CHAN_TURBO;
684                                break;
685                        case 's':               /* st = Atheros Static Turbo */
686                                flags |= IEEE80211_CHAN_STURBO;
687                                break;
688                        default:
689                                errx(-1, "%s: Invalid channel attribute %c\n",
690                                    val, *cp);
691                        }
692                }
693        }
694        cp = strchr(val, '/');
695        if (cp != NULL) {
696                char *ep;
697                u_long cw = strtoul(cp+1, &ep, 10);
698
699                switch (cw) {
700                case 5:
701                        flags |= IEEE80211_CHAN_QUARTER;
702                        break;
703                case 10:
704                        flags |= IEEE80211_CHAN_HALF;
705                        break;
706                case 20:
707                        /* NB: this may be removed below */
708                        flags |= IEEE80211_CHAN_HT20;
709                        break;
710                case 40:
711                case 80:
712                case 160:
713                        /* Handle the 80/160 VHT flag */
714                        if (cw == 80)
715                                flags |= IEEE80211_CHAN_VHT80;
716                        else if (cw == 160)
717                                flags |= IEEE80211_CHAN_VHT160;
718
719                        /* Fallthrough */
720                        if (ep != NULL && *ep == '+')
721                                flags |= IEEE80211_CHAN_HT40U;
722                        else if (ep != NULL && *ep == '-')
723                                flags |= IEEE80211_CHAN_HT40D;
724                        break;
725                default:
726                        errx(-1, "%s: Invalid channel width\n", val);
727                }
728        }
729
730        /*
731         * Cleanup specifications.
732         */
733        if ((flags & _CHAN_HT) == 0) {
734                /*
735                 * If user specified freq/20 or freq/40 quietly remove
736                 * HT cw attributes depending on channel use.  To give
737                 * an explicit 20/40 width for an HT channel you must
738                 * indicate it is an HT channel since all HT channels
739                 * are also usable for legacy operation; e.g. freq:n/40.
740                 */
741                flags &= ~IEEE80211_CHAN_HT;
742                flags &= ~IEEE80211_CHAN_VHT;
743        } else {
744                /*
745                 * Remove private indicator that this is an HT channel
746                 * and if no explicit channel width has been given
747                 * provide the default settings.
748                 */
749                flags &= ~_CHAN_HT;
750                if ((flags & IEEE80211_CHAN_HT) == 0) {
751                        struct ieee80211_channel chan;
752                        /*
753                         * Consult the channel list to see if we can use
754                         * HT40+ or HT40- (if both the map routines choose).
755                         */
756                        if (freq > 255)
757                                mapfreq(&chan, freq, 0);
758                        else
759                                mapchan(&chan, freq, 0);
760                        flags |= (chan.ic_flags & IEEE80211_CHAN_HT);
761                }
762
763                /*
764                 * If VHT is enabled, then also set the VHT flag and the
765                 * relevant channel up/down.
766                 */
767                if (is_vht && (flags & IEEE80211_CHAN_HT)) {
768                        /*
769                         * XXX yes, maybe we should just have VHT, and reuse
770                         * HT20/HT40U/HT40D
771                         */
772                        if (flags & IEEE80211_CHAN_VHT80)
773                                ;
774                        else if (flags & IEEE80211_CHAN_HT20)
775                                flags |= IEEE80211_CHAN_VHT20;
776                        else if (flags & IEEE80211_CHAN_HT40U)
777                                flags |= IEEE80211_CHAN_VHT40U;
778                        else if (flags & IEEE80211_CHAN_HT40D)
779                                flags |= IEEE80211_CHAN_VHT40D;
780                }
781        }
782        return flags;
783#undef _CHAN_HT
784}
785
786static void
787getchannel(int s, struct ieee80211_channel *chan, const char *val)
788{
789        int v, flags;
790        char *eptr;
791
792        memset(chan, 0, sizeof(*chan));
793        if (isanyarg(val)) {
794                chan->ic_freq = IEEE80211_CHAN_ANY;
795                return;
796        }
797        getchaninfo(s);
798        errno = 0;
799        v = strtol(val, &eptr, 10);
800        if (val[0] == '\0' || val == eptr || errno == ERANGE ||
801            /* channel may be suffixed with nothing, :flag, or /width */
802            (eptr[0] != '\0' && eptr[0] != ':' && eptr[0] != '/'))
803                errx(1, "invalid channel specification%s",
804                    errno == ERANGE ? " (out of range)" : "");
805        flags = getchannelflags(val, v);
806        if (v > 255) {          /* treat as frequency */
807                mapfreq(chan, v, flags);
808        } else {
809                mapchan(chan, v, flags);
810        }
811}
812
813static void
814set80211channel(const char *val, int d, int s, const struct afswtch *rafp)
815{
816        struct ieee80211_channel chan;
817
818        getchannel(s, &chan, val);
819        set80211(s, IEEE80211_IOC_CURCHAN, 0, sizeof(chan), &chan);
820}
821
822static void
823set80211chanswitch(const char *val, int d, int s, const struct afswtch *rafp)
824{
825        struct ieee80211_chanswitch_req csr;
826
827        getchannel(s, &csr.csa_chan, val);
828        csr.csa_mode = 1;
829        csr.csa_count = 5;
830        set80211(s, IEEE80211_IOC_CHANSWITCH, 0, sizeof(csr), &csr);
831}
832
833static void
834set80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
835{
836        int     mode;
837
838        if (strcasecmp(val, "none") == 0) {
839                mode = IEEE80211_AUTH_NONE;
840        } else if (strcasecmp(val, "open") == 0) {
841                mode = IEEE80211_AUTH_OPEN;
842        } else if (strcasecmp(val, "shared") == 0) {
843                mode = IEEE80211_AUTH_SHARED;
844        } else if (strcasecmp(val, "8021x") == 0) {
845                mode = IEEE80211_AUTH_8021X;
846        } else if (strcasecmp(val, "wpa") == 0) {
847                mode = IEEE80211_AUTH_WPA;
848        } else {
849                errx(1, "unknown authmode");
850        }
851
852        set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
853}
854
855static void
856set80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
857{
858        int     mode;
859
860        if (strcasecmp(val, "off") == 0) {
861                mode = IEEE80211_POWERSAVE_OFF;
862        } else if (strcasecmp(val, "on") == 0) {
863                mode = IEEE80211_POWERSAVE_ON;
864        } else if (strcasecmp(val, "cam") == 0) {
865                mode = IEEE80211_POWERSAVE_CAM;
866        } else if (strcasecmp(val, "psp") == 0) {
867                mode = IEEE80211_POWERSAVE_PSP;
868        } else if (strcasecmp(val, "psp-cam") == 0) {
869                mode = IEEE80211_POWERSAVE_PSP_CAM;
870        } else {
871                errx(1, "unknown powersavemode");
872        }
873
874        set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
875}
876
877static void
878set80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
879{
880        if (d == 0)
881                set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
882                    0, NULL);
883        else
884                set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
885                    0, NULL);
886}
887
888static void
889set80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
890{
891        set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
892}
893
894static void
895set80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
896{
897        int     mode;
898
899        if (strcasecmp(val, "off") == 0) {
900                mode = IEEE80211_WEP_OFF;
901        } else if (strcasecmp(val, "on") == 0) {
902                mode = IEEE80211_WEP_ON;
903        } else if (strcasecmp(val, "mixed") == 0) {
904                mode = IEEE80211_WEP_MIXED;
905        } else {
906                errx(1, "unknown wep mode");
907        }
908
909        set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
910}
911
912static void
913set80211wep(const char *val, int d, int s, const struct afswtch *rafp)
914{
915        set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
916}
917
918static int
919isundefarg(const char *arg)
920{
921        return (strcmp(arg, "-") == 0 || strncasecmp(arg, "undef", 5) == 0);
922}
923
924static void
925set80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
926{
927        if (isundefarg(val))
928                set80211(s, IEEE80211_IOC_WEPTXKEY, IEEE80211_KEYIX_NONE, 0, NULL);
929        else
930                set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
931}
932
933static void
934set80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
935{
936        int             key = 0;
937        int             len;
938        u_int8_t        data[IEEE80211_KEYBUF_SIZE];
939
940        if (isdigit((int)val[0]) && val[1] == ':') {
941                key = atoi(val)-1;
942                val += 2;
943        }
944
945        bzero(data, sizeof(data));
946        len = sizeof(data);
947        get_string(val, NULL, data, &len);
948
949        set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
950}
951
952/*
953 * This function is purely a NetBSD compatibility interface.  The NetBSD
954 * interface is too inflexible, but it's there so we'll support it since
955 * it's not all that hard.
956 */
957static void
958set80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
959{
960        int             txkey;
961        int             i, len;
962        u_int8_t        data[IEEE80211_KEYBUF_SIZE];
963
964        set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
965
966        if (isdigit((int)val[0]) && val[1] == ':') {
967                txkey = val[0]-'0'-1;
968                val += 2;
969
970                for (i = 0; i < 4; i++) {
971                        bzero(data, sizeof(data));
972                        len = sizeof(data);
973                        val = get_string(val, ",", data, &len);
974                        if (val == NULL)
975                                exit(1);
976
977                        set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
978                }
979        } else {
980                bzero(data, sizeof(data));
981                len = sizeof(data);
982                get_string(val, NULL, data, &len);
983                txkey = 0;
984
985                set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
986
987                bzero(data, sizeof(data));
988                for (i = 1; i < 4; i++)
989                        set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
990        }
991
992        set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
993}
994
995static void
996set80211rtsthreshold(const char *val, int d, int s, const struct afswtch *rafp)
997{
998        set80211(s, IEEE80211_IOC_RTSTHRESHOLD,
999                isundefarg(val) ? IEEE80211_RTS_MAX : atoi(val), 0, NULL);
1000}
1001
1002static void
1003set80211protmode(const char *val, int d, int s, const struct afswtch *rafp)
1004{
1005        int     mode;
1006
1007        if (strcasecmp(val, "off") == 0) {
1008                mode = IEEE80211_PROTMODE_OFF;
1009        } else if (strcasecmp(val, "cts") == 0) {
1010                mode = IEEE80211_PROTMODE_CTS;
1011        } else if (strncasecmp(val, "rtscts", 3) == 0) {
1012                mode = IEEE80211_PROTMODE_RTSCTS;
1013        } else {
1014                errx(1, "unknown protection mode");
1015        }
1016
1017        set80211(s, IEEE80211_IOC_PROTMODE, mode, 0, NULL);
1018}
1019
1020static void
1021set80211htprotmode(const char *val, int d, int s, const struct afswtch *rafp)
1022{
1023        int     mode;
1024
1025        if (strcasecmp(val, "off") == 0) {
1026                mode = IEEE80211_PROTMODE_OFF;
1027        } else if (strncasecmp(val, "rts", 3) == 0) {
1028                mode = IEEE80211_PROTMODE_RTSCTS;
1029        } else {
1030                errx(1, "unknown protection mode");
1031        }
1032
1033        set80211(s, IEEE80211_IOC_HTPROTMODE, mode, 0, NULL);
1034}
1035
1036static void
1037set80211txpower(const char *val, int d, int s, const struct afswtch *rafp)
1038{
1039        double v = atof(val);
1040        int txpow;
1041
1042        txpow = (int) (2*v);
1043        if (txpow != 2*v)
1044                errx(-1, "invalid tx power (must be .5 dBm units)");
1045        set80211(s, IEEE80211_IOC_TXPOWER, txpow, 0, NULL);
1046}
1047
1048#define IEEE80211_ROAMING_DEVICE        0
1049#define IEEE80211_ROAMING_AUTO          1
1050#define IEEE80211_ROAMING_MANUAL        2
1051
1052static void
1053set80211roaming(const char *val, int d, int s, const struct afswtch *rafp)
1054{
1055        int mode;
1056
1057        if (strcasecmp(val, "device") == 0) {
1058                mode = IEEE80211_ROAMING_DEVICE;
1059        } else if (strcasecmp(val, "auto") == 0) {
1060                mode = IEEE80211_ROAMING_AUTO;
1061        } else if (strcasecmp(val, "manual") == 0) {
1062                mode = IEEE80211_ROAMING_MANUAL;
1063        } else {
1064                errx(1, "unknown roaming mode");
1065        }
1066        set80211(s, IEEE80211_IOC_ROAMING, mode, 0, NULL);
1067}
1068
1069static void
1070set80211wme(const char *val, int d, int s, const struct afswtch *rafp)
1071{
1072        set80211(s, IEEE80211_IOC_WME, d, 0, NULL);
1073}
1074
1075static void
1076set80211hidessid(const char *val, int d, int s, const struct afswtch *rafp)
1077{
1078        set80211(s, IEEE80211_IOC_HIDESSID, d, 0, NULL);
1079}
1080
1081static void
1082set80211apbridge(const char *val, int d, int s, const struct afswtch *rafp)
1083{
1084        set80211(s, IEEE80211_IOC_APBRIDGE, d, 0, NULL);
1085}
1086
1087static void
1088set80211fastframes(const char *val, int d, int s, const struct afswtch *rafp)
1089{
1090        set80211(s, IEEE80211_IOC_FF, d, 0, NULL);
1091}
1092
1093static void
1094set80211dturbo(const char *val, int d, int s, const struct afswtch *rafp)
1095{
1096        set80211(s, IEEE80211_IOC_TURBOP, d, 0, NULL);
1097}
1098
1099static void
1100set80211chanlist(const char *val, int d, int s, const struct afswtch *rafp)
1101{
1102        struct ieee80211req_chanlist chanlist;
1103        char *temp, *cp, *tp;
1104
1105        temp = malloc(strlen(val) + 1);
1106        if (temp == NULL)
1107                errx(1, "malloc failed");
1108        strcpy(temp, val);
1109        memset(&chanlist, 0, sizeof(chanlist));
1110        cp = temp;
1111        for (;;) {
1112                int first, last, f, c;
1113
1114                tp = strchr(cp, ',');
1115                if (tp != NULL)
1116                        *tp++ = '\0';
1117                switch (sscanf(cp, "%u-%u", &first, &last)) {
1118                case 1:
1119                        if (first > IEEE80211_CHAN_MAX)
1120                                errx(-1, "channel %u out of range, max %u",
1121                                        first, IEEE80211_CHAN_MAX);
1122                        setbit(chanlist.ic_channels, first);
1123                        break;
1124                case 2:
1125                        if (first > IEEE80211_CHAN_MAX)
1126                                errx(-1, "channel %u out of range, max %u",
1127                                        first, IEEE80211_CHAN_MAX);
1128                        if (last > IEEE80211_CHAN_MAX)
1129                                errx(-1, "channel %u out of range, max %u",
1130                                        last, IEEE80211_CHAN_MAX);
1131                        if (first > last)
1132                                errx(-1, "void channel range, %u > %u",
1133                                        first, last);
1134                        for (f = first; f <= last; f++)
1135                                setbit(chanlist.ic_channels, f);
1136                        break;
1137                }
1138                if (tp == NULL)
1139                        break;
1140                c = *tp;
1141                while (isspace(c))
1142                        tp++;
1143                if (!isdigit(c))
1144                        break;
1145                cp = tp;
1146        }
1147        set80211(s, IEEE80211_IOC_CHANLIST, 0, sizeof(chanlist), &chanlist);
1148}
1149
1150static void
1151set80211bssid(const char *val, int d, int s, const struct afswtch *rafp)
1152{
1153
1154        if (!isanyarg(val)) {
1155                char *temp;
1156                struct sockaddr_dl sdl;
1157
1158                temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1159                if (temp == NULL)
1160                        errx(1, "malloc failed");
1161                temp[0] = ':';
1162                strcpy(temp + 1, val);
1163                sdl.sdl_len = sizeof(sdl);
1164                link_addr(temp, &sdl);
1165                free(temp);
1166                if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1167                        errx(1, "malformed link-level address");
1168                set80211(s, IEEE80211_IOC_BSSID, 0,
1169                        IEEE80211_ADDR_LEN, LLADDR(&sdl));
1170        } else {
1171                uint8_t zerobssid[IEEE80211_ADDR_LEN];
1172                memset(zerobssid, 0, sizeof(zerobssid));
1173                set80211(s, IEEE80211_IOC_BSSID, 0,
1174                        IEEE80211_ADDR_LEN, zerobssid);
1175        }
1176}
1177
1178static int
1179getac(const char *ac)
1180{
1181        if (strcasecmp(ac, "ac_be") == 0 || strcasecmp(ac, "be") == 0)
1182                return WME_AC_BE;
1183        if (strcasecmp(ac, "ac_bk") == 0 || strcasecmp(ac, "bk") == 0)
1184                return WME_AC_BK;
1185        if (strcasecmp(ac, "ac_vi") == 0 || strcasecmp(ac, "vi") == 0)
1186                return WME_AC_VI;
1187        if (strcasecmp(ac, "ac_vo") == 0 || strcasecmp(ac, "vo") == 0)
1188                return WME_AC_VO;
1189        errx(1, "unknown wme access class %s", ac);
1190}
1191
1192static
1193DECL_CMD_FUNC2(set80211cwmin, ac, val)
1194{
1195        set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val), getac(ac), NULL);
1196}
1197
1198static
1199DECL_CMD_FUNC2(set80211cwmax, ac, val)
1200{
1201        set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val), getac(ac), NULL);
1202}
1203
1204static
1205DECL_CMD_FUNC2(set80211aifs, ac, val)
1206{
1207        set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val), getac(ac), NULL);
1208}
1209
1210static
1211DECL_CMD_FUNC2(set80211txoplimit, ac, val)
1212{
1213        set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), getac(ac), NULL);
1214}
1215
1216static
1217DECL_CMD_FUNC(set80211acm, ac, d)
1218{
1219        set80211(s, IEEE80211_IOC_WME_ACM, 1, getac(ac), NULL);
1220}
1221static
1222DECL_CMD_FUNC(set80211noacm, ac, d)
1223{
1224        set80211(s, IEEE80211_IOC_WME_ACM, 0, getac(ac), NULL);
1225}
1226
1227static
1228DECL_CMD_FUNC(set80211ackpolicy, ac, d)
1229{
1230        set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 1, getac(ac), NULL);
1231}
1232static
1233DECL_CMD_FUNC(set80211noackpolicy, ac, d)
1234{
1235        set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 0, getac(ac), NULL);
1236}
1237
1238static
1239DECL_CMD_FUNC2(set80211bsscwmin, ac, val)
1240{
1241        set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val),
1242                getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1243}
1244
1245static
1246DECL_CMD_FUNC2(set80211bsscwmax, ac, val)
1247{
1248        set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val),
1249                getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1250}
1251
1252static
1253DECL_CMD_FUNC2(set80211bssaifs, ac, val)
1254{
1255        set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val),
1256                getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1257}
1258
1259static
1260DECL_CMD_FUNC2(set80211bsstxoplimit, ac, val)
1261{
1262        set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val),
1263                getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1264}
1265
1266static
1267DECL_CMD_FUNC(set80211dtimperiod, val, d)
1268{
1269        set80211(s, IEEE80211_IOC_DTIM_PERIOD, atoi(val), 0, NULL);
1270}
1271
1272static
1273DECL_CMD_FUNC(set80211bintval, val, d)
1274{
1275        set80211(s, IEEE80211_IOC_BEACON_INTERVAL, atoi(val), 0, NULL);
1276}
1277
1278static void
1279set80211macmac(int s, int op, const char *val)
1280{
1281        char *temp;
1282        struct sockaddr_dl sdl;
1283
1284        temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1285        if (temp == NULL)
1286                errx(1, "malloc failed");
1287        temp[0] = ':';
1288        strcpy(temp + 1, val);
1289        sdl.sdl_len = sizeof(sdl);
1290        link_addr(temp, &sdl);
1291        free(temp);
1292        if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1293                errx(1, "malformed link-level address");
1294        set80211(s, op, 0, IEEE80211_ADDR_LEN, LLADDR(&sdl));
1295}
1296
1297static
1298DECL_CMD_FUNC(set80211addmac, val, d)
1299{
1300        set80211macmac(s, IEEE80211_IOC_ADDMAC, val);
1301}
1302
1303static
1304DECL_CMD_FUNC(set80211delmac, val, d)
1305{
1306        set80211macmac(s, IEEE80211_IOC_DELMAC, val);
1307}
1308
1309static
1310DECL_CMD_FUNC(set80211kickmac, val, d)
1311{
1312        char *temp;
1313        struct sockaddr_dl sdl;
1314        struct ieee80211req_mlme mlme;
1315
1316        temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1317        if (temp == NULL)
1318                errx(1, "malloc failed");
1319        temp[0] = ':';
1320        strcpy(temp + 1, val);
1321        sdl.sdl_len = sizeof(sdl);
1322        link_addr(temp, &sdl);
1323        free(temp);
1324        if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1325                errx(1, "malformed link-level address");
1326        memset(&mlme, 0, sizeof(mlme));
1327        mlme.im_op = IEEE80211_MLME_DEAUTH;
1328        mlme.im_reason = IEEE80211_REASON_AUTH_EXPIRE;
1329        memcpy(mlme.im_macaddr, LLADDR(&sdl), IEEE80211_ADDR_LEN);
1330        set80211(s, IEEE80211_IOC_MLME, 0, sizeof(mlme), &mlme);
1331}
1332
1333static
1334DECL_CMD_FUNC(set80211maccmd, val, d)
1335{
1336        set80211(s, IEEE80211_IOC_MACCMD, d, 0, NULL);
1337}
1338
1339static void
1340set80211meshrtmac(int s, int req, const char *val)
1341{
1342        char *temp;
1343        struct sockaddr_dl sdl;
1344
1345        temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1346        if (temp == NULL)
1347                errx(1, "malloc failed");
1348        temp[0] = ':';
1349        strcpy(temp + 1, val);
1350        sdl.sdl_len = sizeof(sdl);
1351        link_addr(temp, &sdl);
1352        free(temp);
1353        if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1354                errx(1, "malformed link-level address");
1355        set80211(s, IEEE80211_IOC_MESH_RTCMD, req,
1356            IEEE80211_ADDR_LEN, LLADDR(&sdl));
1357}
1358
1359static
1360DECL_CMD_FUNC(set80211addmeshrt, val, d)
1361{
1362        set80211meshrtmac(s, IEEE80211_MESH_RTCMD_ADD, val);
1363}
1364
1365static
1366DECL_CMD_FUNC(set80211delmeshrt, val, d)
1367{
1368        set80211meshrtmac(s, IEEE80211_MESH_RTCMD_DELETE, val);
1369}
1370
1371static
1372DECL_CMD_FUNC(set80211meshrtcmd, val, d)
1373{
1374        set80211(s, IEEE80211_IOC_MESH_RTCMD, d, 0, NULL);
1375}
1376
1377static
1378DECL_CMD_FUNC(set80211hwmprootmode, val, d)
1379{
1380        int mode;
1381
1382        if (strcasecmp(val, "normal") == 0)
1383                mode = IEEE80211_HWMP_ROOTMODE_NORMAL;
1384        else if (strcasecmp(val, "proactive") == 0)
1385                mode = IEEE80211_HWMP_ROOTMODE_PROACTIVE;
1386        else if (strcasecmp(val, "rann") == 0)
1387                mode = IEEE80211_HWMP_ROOTMODE_RANN;
1388        else
1389                mode = IEEE80211_HWMP_ROOTMODE_DISABLED;
1390        set80211(s, IEEE80211_IOC_HWMP_ROOTMODE, mode, 0, NULL);
1391}
1392
1393static
1394DECL_CMD_FUNC(set80211hwmpmaxhops, val, d)
1395{
1396        set80211(s, IEEE80211_IOC_HWMP_MAXHOPS, atoi(val), 0, NULL);
1397}
1398
1399static void
1400set80211pureg(const char *val, int d, int s, const struct afswtch *rafp)
1401{
1402        set80211(s, IEEE80211_IOC_PUREG, d, 0, NULL);
1403}
1404
1405static void
1406set80211quiet(const char *val, int d, int s, const struct afswtch *rafp)
1407{
1408        set80211(s, IEEE80211_IOC_QUIET, d, 0, NULL);
1409}
1410
1411static
1412DECL_CMD_FUNC(set80211quietperiod, val, d)
1413{
1414        set80211(s, IEEE80211_IOC_QUIET_PERIOD, atoi(val), 0, NULL);
1415}
1416
1417static
1418DECL_CMD_FUNC(set80211quietcount, val, d)
1419{
1420        set80211(s, IEEE80211_IOC_QUIET_COUNT, atoi(val), 0, NULL);
1421}
1422
1423static
1424DECL_CMD_FUNC(set80211quietduration, val, d)
1425{
1426        set80211(s, IEEE80211_IOC_QUIET_DUR, atoi(val), 0, NULL);
1427}
1428
1429static
1430DECL_CMD_FUNC(set80211quietoffset, val, d)
1431{
1432        set80211(s, IEEE80211_IOC_QUIET_OFFSET, atoi(val), 0, NULL);
1433}
1434
1435static void
1436set80211bgscan(const char *val, int d, int s, const struct afswtch *rafp)
1437{
1438        set80211(s, IEEE80211_IOC_BGSCAN, d, 0, NULL);
1439}
1440
1441static
1442DECL_CMD_FUNC(set80211bgscanidle, val, d)
1443{
1444        set80211(s, IEEE80211_IOC_BGSCAN_IDLE, atoi(val), 0, NULL);
1445}
1446
1447static
1448DECL_CMD_FUNC(set80211bgscanintvl, val, d)
1449{
1450        set80211(s, IEEE80211_IOC_BGSCAN_INTERVAL, atoi(val), 0, NULL);
1451}
1452
1453static
1454DECL_CMD_FUNC(set80211scanvalid, val, d)
1455{
1456        set80211(s, IEEE80211_IOC_SCANVALID, atoi(val), 0, NULL);
1457}
1458
1459/*
1460 * Parse an optional trailing specification of which netbands
1461 * to apply a parameter to.  This is basically the same syntax
1462 * as used for channels but you can concatenate to specify
1463 * multiple.  For example:
1464 *      14:abg          apply to 11a, 11b, and 11g
1465 *      6:ht            apply to 11na and 11ng
1466 * We don't make a big effort to catch silly things; this is
1467 * really a convenience mechanism.
1468 */
1469static int
1470getmodeflags(const char *val)
1471{
1472        const char *cp;
1473        int flags;
1474
1475        flags = 0;
1476
1477        cp = strchr(val, ':');
1478        if (cp != NULL) {
1479                for (cp++; isalpha((int) *cp); cp++) {
1480                        /* accept mixed case */
1481                        int c = *cp;
1482                        if (isupper(c))
1483                                c = tolower(c);
1484                        switch (c) {
1485                        case 'a':               /* 802.11a */
1486                                flags |= IEEE80211_CHAN_A;
1487                                break;
1488                        case 'b':               /* 802.11b */
1489                                flags |= IEEE80211_CHAN_B;
1490                                break;
1491                        case 'g':               /* 802.11g */
1492                                flags |= IEEE80211_CHAN_G;
1493                                break;
1494                        case 'n':               /* 802.11n */
1495                                flags |= IEEE80211_CHAN_HT;
1496                                break;
1497                        case 'd':               /* dt = Atheros Dynamic Turbo */
1498                                flags |= IEEE80211_CHAN_TURBO;
1499                                break;
1500                        case 't':               /* ht, dt, st, t */
1501                                /* dt and unadorned t specify Dynamic Turbo */
1502                                if ((flags & (IEEE80211_CHAN_STURBO|IEEE80211_CHAN_HT)) == 0)
1503                                        flags |= IEEE80211_CHAN_TURBO;
1504                                break;
1505                        case 's':               /* st = Atheros Static Turbo */
1506                                flags |= IEEE80211_CHAN_STURBO;
1507                                break;
1508                        case 'h':               /* 1/2-width channels */
1509                                flags |= IEEE80211_CHAN_HALF;
1510                                break;
1511                        case 'q':               /* 1/4-width channels */
1512                                flags |= IEEE80211_CHAN_QUARTER;
1513                                break;
1514                        case 'v':
1515                                /* XXX set HT too? */
1516                                flags |= IEEE80211_CHAN_VHT;
1517                                break;
1518                        default:
1519                                errx(-1, "%s: Invalid mode attribute %c\n",
1520                                    val, *cp);
1521                        }
1522                }
1523        }
1524        return flags;
1525}
1526
1527#define IEEE80211_CHAN_HTA      (IEEE80211_CHAN_HT|IEEE80211_CHAN_5GHZ)
1528#define IEEE80211_CHAN_HTG      (IEEE80211_CHAN_HT|IEEE80211_CHAN_2GHZ)
1529
1530#define _APPLY(_flags, _base, _param, _v) do {                          \
1531    if (_flags & IEEE80211_CHAN_HT) {                                   \
1532            if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1533                    _base.params[IEEE80211_MODE_11NA]._param = _v;      \
1534                    _base.params[IEEE80211_MODE_11NG]._param = _v;      \
1535            } else if (_flags & IEEE80211_CHAN_5GHZ)                    \
1536                    _base.params[IEEE80211_MODE_11NA]._param = _v;      \
1537            else                                                        \
1538                    _base.params[IEEE80211_MODE_11NG]._param = _v;      \
1539    }                                                                   \
1540    if (_flags & IEEE80211_CHAN_TURBO) {                                \
1541            if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1542                    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;   \
1543                    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;   \
1544            } else if (_flags & IEEE80211_CHAN_5GHZ)                    \
1545                    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;   \
1546            else                                                        \
1547                    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;   \
1548    }                                                                   \
1549    if (_flags & IEEE80211_CHAN_STURBO)                                 \
1550            _base.params[IEEE80211_MODE_STURBO_A]._param = _v;          \
1551    if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)                \
1552            _base.params[IEEE80211_MODE_11A]._param = _v;               \
1553    if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)                \
1554            _base.params[IEEE80211_MODE_11G]._param = _v;               \
1555    if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)                \
1556            _base.params[IEEE80211_MODE_11B]._param = _v;               \
1557    if (_flags & IEEE80211_CHAN_HALF)                                   \
1558            _base.params[IEEE80211_MODE_HALF]._param = _v;              \
1559    if (_flags & IEEE80211_CHAN_QUARTER)                                \
1560            _base.params[IEEE80211_MODE_QUARTER]._param = _v;           \
1561} while (0)
1562#define _APPLY1(_flags, _base, _param, _v) do {                         \
1563    if (_flags & IEEE80211_CHAN_HT) {                                   \
1564            if (_flags & IEEE80211_CHAN_5GHZ)                           \
1565                    _base.params[IEEE80211_MODE_11NA]._param = _v;      \
1566            else                                                        \
1567                    _base.params[IEEE80211_MODE_11NG]._param = _v;      \
1568    } else if ((_flags & IEEE80211_CHAN_108A) == IEEE80211_CHAN_108A)   \
1569            _base.params[IEEE80211_MODE_TURBO_A]._param = _v;           \
1570    else if ((_flags & IEEE80211_CHAN_108G) == IEEE80211_CHAN_108G)     \
1571            _base.params[IEEE80211_MODE_TURBO_G]._param = _v;           \
1572    else if ((_flags & IEEE80211_CHAN_ST) == IEEE80211_CHAN_ST)         \
1573            _base.params[IEEE80211_MODE_STURBO_A]._param = _v;          \
1574    else if (_flags & IEEE80211_CHAN_HALF)                              \
1575            _base.params[IEEE80211_MODE_HALF]._param = _v;              \
1576    else if (_flags & IEEE80211_CHAN_QUARTER)                           \
1577            _base.params[IEEE80211_MODE_QUARTER]._param = _v;           \
1578    else if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)           \
1579            _base.params[IEEE80211_MODE_11A]._param = _v;               \
1580    else if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)           \
1581            _base.params[IEEE80211_MODE_11G]._param = _v;               \
1582    else if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)           \
1583            _base.params[IEEE80211_MODE_11B]._param = _v;               \
1584} while (0)
1585#define _APPLY_RATE(_flags, _base, _param, _v) do {                     \
1586    if (_flags & IEEE80211_CHAN_HT) {                                   \
1587        (_v) = (_v / 2) | IEEE80211_RATE_MCS;                           \
1588    }                                                                   \
1589    _APPLY(_flags, _base, _param, _v);                                  \
1590} while (0)
1591#define _APPLY_RATE1(_flags, _base, _param, _v) do {                    \
1592    if (_flags & IEEE80211_CHAN_HT) {                                   \
1593        (_v) = (_v / 2) | IEEE80211_RATE_MCS;                           \
1594    }                                                                   \
1595    _APPLY1(_flags, _base, _param, _v);                                 \
1596} while (0)
1597
1598static
1599DECL_CMD_FUNC(set80211roamrssi, val, d)
1600{
1601        double v = atof(val);
1602        int rssi, flags;
1603
1604        rssi = (int) (2*v);
1605        if (rssi != 2*v)
1606                errx(-1, "invalid rssi (must be .5 dBm units)");
1607        flags = getmodeflags(val);
1608        getroam(s);
1609        if (flags == 0) {               /* NB: no flags => current channel */
1610                flags = getcurchan(s)->ic_flags;
1611                _APPLY1(flags, roamparams, rssi, rssi);
1612        } else
1613                _APPLY(flags, roamparams, rssi, rssi);
1614        callback_register(setroam_cb, &roamparams);
1615}
1616
1617static int
1618getrate(const char *val, const char *tag)
1619{
1620        double v = atof(val);
1621        int rate;
1622
1623        rate = (int) (2*v);
1624        if (rate != 2*v)
1625                errx(-1, "invalid %s rate (must be .5 Mb/s units)", tag);
1626        return rate;            /* NB: returns 2x the specified value */
1627}
1628
1629static
1630DECL_CMD_FUNC(set80211roamrate, val, d)
1631{
1632        int rate, flags;
1633
1634        rate = getrate(val, "roam");
1635        flags = getmodeflags(val);
1636        getroam(s);
1637        if (flags == 0) {               /* NB: no flags => current channel */
1638                flags = getcurchan(s)->ic_flags;
1639                _APPLY_RATE1(flags, roamparams, rate, rate);
1640        } else
1641                _APPLY_RATE(flags, roamparams, rate, rate);
1642        callback_register(setroam_cb, &roamparams);
1643}
1644
1645static
1646DECL_CMD_FUNC(set80211mcastrate, val, d)
1647{
1648        int rate, flags;
1649
1650        rate = getrate(val, "mcast");
1651        flags = getmodeflags(val);
1652        gettxparams(s);
1653        if (flags == 0) {               /* NB: no flags => current channel */
1654                flags = getcurchan(s)->ic_flags;
1655                _APPLY_RATE1(flags, txparams, mcastrate, rate);
1656        } else
1657                _APPLY_RATE(flags, txparams, mcastrate, rate);
1658        callback_register(settxparams_cb, &txparams);
1659}
1660
1661static
1662DECL_CMD_FUNC(set80211mgtrate, val, d)
1663{
1664        int rate, flags;
1665
1666        rate = getrate(val, "mgmt");
1667        flags = getmodeflags(val);
1668        gettxparams(s);
1669        if (flags == 0) {               /* NB: no flags => current channel */
1670                flags = getcurchan(s)->ic_flags;
1671                _APPLY_RATE1(flags, txparams, mgmtrate, rate);
1672        } else
1673                _APPLY_RATE(flags, txparams, mgmtrate, rate);
1674        callback_register(settxparams_cb, &txparams);
1675}
1676
1677static
1678DECL_CMD_FUNC(set80211ucastrate, val, d)
1679{
1680        int flags;
1681
1682        gettxparams(s);
1683        flags = getmodeflags(val);
1684        if (isanyarg(val)) {
1685                if (flags == 0) {       /* NB: no flags => current channel */
1686                        flags = getcurchan(s)->ic_flags;
1687                        _APPLY1(flags, txparams, ucastrate,
1688                            IEEE80211_FIXED_RATE_NONE);
1689                } else
1690                        _APPLY(flags, txparams, ucastrate,
1691                            IEEE80211_FIXED_RATE_NONE);
1692        } else {
1693                int rate = getrate(val, "ucast");
1694                if (flags == 0) {       /* NB: no flags => current channel */
1695                        flags = getcurchan(s)->ic_flags;
1696                        _APPLY_RATE1(flags, txparams, ucastrate, rate);
1697                } else
1698                        _APPLY_RATE(flags, txparams, ucastrate, rate);
1699        }
1700        callback_register(settxparams_cb, &txparams);
1701}
1702
1703static
1704DECL_CMD_FUNC(set80211maxretry, val, d)
1705{
1706        int v = atoi(val), flags;
1707
1708        flags = getmodeflags(val);
1709        gettxparams(s);
1710        if (flags == 0) {               /* NB: no flags => current channel */
1711                flags = getcurchan(s)->ic_flags;
1712                _APPLY1(flags, txparams, maxretry, v);
1713        } else
1714                _APPLY(flags, txparams, maxretry, v);
1715        callback_register(settxparams_cb, &txparams);
1716}
1717#undef _APPLY_RATE
1718#undef _APPLY
1719#undef IEEE80211_CHAN_HTA
1720#undef IEEE80211_CHAN_HTG
1721
1722static
1723DECL_CMD_FUNC(set80211fragthreshold, val, d)
1724{
1725        set80211(s, IEEE80211_IOC_FRAGTHRESHOLD,
1726                isundefarg(val) ? IEEE80211_FRAG_MAX : atoi(val), 0, NULL);
1727}
1728
1729static
1730DECL_CMD_FUNC(set80211bmissthreshold, val, d)
1731{
1732        set80211(s, IEEE80211_IOC_BMISSTHRESHOLD,
1733                isundefarg(val) ? IEEE80211_HWBMISS_MAX : atoi(val), 0, NULL);
1734}
1735
1736static void
1737set80211burst(const char *val, int d, int s, const struct afswtch *rafp)
1738{
1739        set80211(s, IEEE80211_IOC_BURST, d, 0, NULL);
1740}
1741
1742static void
1743set80211doth(const char *val, int d, int s, const struct afswtch *rafp)
1744{
1745        set80211(s, IEEE80211_IOC_DOTH, d, 0, NULL);
1746}
1747
1748static void
1749set80211dfs(const char *val, int d, int s, const struct afswtch *rafp)
1750{
1751        set80211(s, IEEE80211_IOC_DFS, d, 0, NULL);
1752}
1753
1754static void
1755set80211shortgi(const char *val, int d, int s, const struct afswtch *rafp)
1756{
1757        set80211(s, IEEE80211_IOC_SHORTGI,
1758                d ? (IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40) : 0,
1759                0, NULL);
1760}
1761
1762static void
1763set80211ampdu(const char *val, int d, int s, const struct afswtch *rafp)
1764{
1765        int ampdu;
1766
1767        if (get80211val(s, IEEE80211_IOC_AMPDU, &ampdu) < 0)
1768                errx(-1, "cannot set AMPDU setting");
1769        if (d < 0) {
1770                d = -d;
1771                ampdu &= ~d;
1772        } else
1773                ampdu |= d;
1774        set80211(s, IEEE80211_IOC_AMPDU, ampdu, 0, NULL);
1775}
1776
1777static void
1778set80211stbc(const char *val, int d, int s, const struct afswtch *rafp)
1779{
1780        int stbc;
1781
1782        if (get80211val(s, IEEE80211_IOC_STBC, &stbc) < 0)
1783                errx(-1, "cannot set STBC setting");
1784        if (d < 0) {
1785                d = -d;
1786                stbc &= ~d;
1787        } else
1788                stbc |= d;
1789        set80211(s, IEEE80211_IOC_STBC, stbc, 0, NULL);
1790}
1791
1792static
1793DECL_CMD_FUNC(set80211ampdulimit, val, d)
1794{
1795        int v;
1796
1797        switch (atoi(val)) {
1798        case 8:
1799        case 8*1024:
1800                v = IEEE80211_HTCAP_MAXRXAMPDU_8K;
1801                break;
1802        case 16:
1803        case 16*1024:
1804                v = IEEE80211_HTCAP_MAXRXAMPDU_16K;
1805                break;
1806        case 32:
1807        case 32*1024:
1808                v = IEEE80211_HTCAP_MAXRXAMPDU_32K;
1809                break;
1810        case 64:
1811        case 64*1024:
1812                v = IEEE80211_HTCAP_MAXRXAMPDU_64K;
1813                break;
1814        default:
1815                errx(-1, "invalid A-MPDU limit %s", val);
1816        }
1817        set80211(s, IEEE80211_IOC_AMPDU_LIMIT, v, 0, NULL);
1818}
1819
1820static
1821DECL_CMD_FUNC(set80211ampdudensity, val, d)
1822{
1823        int v;
1824
1825        if (isanyarg(val) || strcasecmp(val, "na") == 0)
1826                v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1827        else switch ((int)(atof(val)*4)) {
1828        case 0:
1829                v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1830                break;
1831        case 1:
1832                v = IEEE80211_HTCAP_MPDUDENSITY_025;
1833                break;
1834        case 2:
1835                v = IEEE80211_HTCAP_MPDUDENSITY_05;
1836                break;
1837        case 4:
1838                v = IEEE80211_HTCAP_MPDUDENSITY_1;
1839                break;
1840        case 8:
1841                v = IEEE80211_HTCAP_MPDUDENSITY_2;
1842                break;
1843        case 16:
1844                v = IEEE80211_HTCAP_MPDUDENSITY_4;
1845                break;
1846        case 32:
1847                v = IEEE80211_HTCAP_MPDUDENSITY_8;
1848                break;
1849        case 64:
1850                v = IEEE80211_HTCAP_MPDUDENSITY_16;
1851                break;
1852        default:
1853                errx(-1, "invalid A-MPDU density %s", val);
1854        }
1855        set80211(s, IEEE80211_IOC_AMPDU_DENSITY, v, 0, NULL);
1856}
1857
1858static void
1859set80211amsdu(const char *val, int d, int s, const struct afswtch *rafp)
1860{
1861        int amsdu;
1862
1863        if (get80211val(s, IEEE80211_IOC_AMSDU, &amsdu) < 0)
1864                err(-1, "cannot get AMSDU setting");
1865        if (d < 0) {
1866                d = -d;
1867                amsdu &= ~d;
1868        } else
1869                amsdu |= d;
1870        set80211(s, IEEE80211_IOC_AMSDU, amsdu, 0, NULL);
1871}
1872
1873static
1874DECL_CMD_FUNC(set80211amsdulimit, val, d)
1875{
1876        set80211(s, IEEE80211_IOC_AMSDU_LIMIT, atoi(val), 0, NULL);
1877}
1878
1879static void
1880set80211puren(const char *val, int d, int s, const struct afswtch *rafp)
1881{
1882        set80211(s, IEEE80211_IOC_PUREN, d, 0, NULL);
1883}
1884
1885static void
1886set80211htcompat(const char *val, int d, int s, const struct afswtch *rafp)
1887{
1888        set80211(s, IEEE80211_IOC_HTCOMPAT, d, 0, NULL);
1889}
1890
1891static void
1892set80211htconf(const char *val, int d, int s, const struct afswtch *rafp)
1893{
1894        set80211(s, IEEE80211_IOC_HTCONF, d, 0, NULL);
1895        htconf = d;
1896}
1897
1898static void
1899set80211dwds(const char *val, int d, int s, const struct afswtch *rafp)
1900{
1901        set80211(s, IEEE80211_IOC_DWDS, d, 0, NULL);
1902}
1903
1904static void
1905set80211inact(const char *val, int d, int s, const struct afswtch *rafp)
1906{
1907        set80211(s, IEEE80211_IOC_INACTIVITY, d, 0, NULL);
1908}
1909
1910static void
1911set80211tsn(const char *val, int d, int s, const struct afswtch *rafp)
1912{
1913        set80211(s, IEEE80211_IOC_TSN, d, 0, NULL);
1914}
1915
1916static void
1917set80211dotd(const char *val, int d, int s, const struct afswtch *rafp)
1918{
1919        set80211(s, IEEE80211_IOC_DOTD, d, 0, NULL);
1920}
1921
1922static void
1923set80211smps(const char *val, int d, int s, const struct afswtch *rafp)
1924{
1925        set80211(s, IEEE80211_IOC_SMPS, d, 0, NULL);
1926}
1927
1928static void
1929set80211rifs(const char *val, int d, int s, const struct afswtch *rafp)
1930{
1931        set80211(s, IEEE80211_IOC_RIFS, d, 0, NULL);
1932}
1933
1934static void
1935set80211vhtconf(const char *val, int d, int s, const struct afswtch *rafp)
1936{
1937        if (get80211val(s, IEEE80211_IOC_VHTCONF, &vhtconf) < 0)
1938                errx(-1, "cannot set VHT setting");
1939        printf("%s: vhtconf=0x%08x, d=%d\n", __func__, vhtconf, d);
1940        if (d < 0) {
1941                d = -d;
1942                vhtconf &= ~d;
1943        } else
1944                vhtconf |= d;
1945        printf("%s: vhtconf is now 0x%08x\n", __func__, vhtconf);
1946        set80211(s, IEEE80211_IOC_VHTCONF, vhtconf, 0, NULL);
1947}
1948
1949static
1950DECL_CMD_FUNC(set80211tdmaslot, val, d)
1951{
1952        set80211(s, IEEE80211_IOC_TDMA_SLOT, atoi(val), 0, NULL);
1953}
1954
1955static
1956DECL_CMD_FUNC(set80211tdmaslotcnt, val, d)
1957{
1958        set80211(s, IEEE80211_IOC_TDMA_SLOTCNT, atoi(val), 0, NULL);
1959}
1960
1961static
1962DECL_CMD_FUNC(set80211tdmaslotlen, val, d)
1963{
1964        set80211(s, IEEE80211_IOC_TDMA_SLOTLEN, atoi(val), 0, NULL);
1965}
1966
1967static
1968DECL_CMD_FUNC(set80211tdmabintval, val, d)
1969{
1970        set80211(s, IEEE80211_IOC_TDMA_BINTERVAL, atoi(val), 0, NULL);
1971}
1972
1973static
1974DECL_CMD_FUNC(set80211meshttl, val, d)
1975{
1976        set80211(s, IEEE80211_IOC_MESH_TTL, atoi(val), 0, NULL);
1977}
1978
1979static
1980DECL_CMD_FUNC(set80211meshforward, val, d)
1981{
1982        set80211(s, IEEE80211_IOC_MESH_FWRD, d, 0, NULL);
1983}
1984
1985static
1986DECL_CMD_FUNC(set80211meshgate, val, d)
1987{
1988        set80211(s, IEEE80211_IOC_MESH_GATE, d, 0, NULL);
1989}
1990
1991static
1992DECL_CMD_FUNC(set80211meshpeering, val, d)
1993{
1994        set80211(s, IEEE80211_IOC_MESH_AP, d, 0, NULL);
1995}
1996
1997static
1998DECL_CMD_FUNC(set80211meshmetric, val, d)
1999{
2000        char v[12];
2001       
2002        memcpy(v, val, sizeof(v));
2003        set80211(s, IEEE80211_IOC_MESH_PR_METRIC, 0, 0, v);
2004}
2005
2006static
2007DECL_CMD_FUNC(set80211meshpath, val, d)
2008{
2009        char v[12];
2010       
2011        memcpy(v, val, sizeof(v));
2012        set80211(s, IEEE80211_IOC_MESH_PR_PATH, 0, 0, v);
2013}
2014
2015static int
2016regdomain_sort(const void *a, const void *b)
2017{
2018#define CHAN_ALL \
2019        (IEEE80211_CHAN_ALLTURBO|IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)
2020        const struct ieee80211_channel *ca = a;
2021        const struct ieee80211_channel *cb = b;
2022
2023        return ca->ic_freq == cb->ic_freq ?
2024            (ca->ic_flags & CHAN_ALL) - (cb->ic_flags & CHAN_ALL) :
2025            ca->ic_freq - cb->ic_freq;
2026#undef CHAN_ALL
2027}
2028
2029static const struct ieee80211_channel *
2030chanlookup(const struct ieee80211_channel chans[], int nchans,
2031        int freq, int flags)
2032{
2033        int i;
2034
2035        flags &= IEEE80211_CHAN_ALLTURBO;
2036        for (i = 0; i < nchans; i++) {
2037                const struct ieee80211_channel *c = &chans[i];
2038                if (c->ic_freq == freq &&
2039                    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
2040                        return c;
2041        }
2042        return NULL;
2043}
2044
2045static int
2046chanfind(const struct ieee80211_channel chans[], int nchans, int flags)
2047{
2048        int i;
2049
2050        for (i = 0; i < nchans; i++) {
2051                const struct ieee80211_channel *c = &chans[i];
2052                if ((c->ic_flags & flags) == flags)
2053                        return 1;
2054        }
2055        return 0;
2056}
2057
2058/*
2059 * Check channel compatibility.
2060 */
2061static int
2062checkchan(const struct ieee80211req_chaninfo *avail, int freq, int flags)
2063{
2064        flags &= ~REQ_FLAGS;
2065        /*
2066         * Check if exact channel is in the calibration table;
2067         * everything below is to deal with channels that we
2068         * want to include but that are not explicitly listed.
2069         */
2070        if (chanlookup(avail->ic_chans, avail->ic_nchans, freq, flags) != NULL)
2071                return 1;
2072        if (flags & IEEE80211_CHAN_GSM) {
2073                /*
2074                 * XXX GSM frequency mapping is handled in the kernel
2075                 * so we cannot find them in the calibration table;
2076                 * just accept the channel and the kernel will reject
2077                 * the channel list if it's wrong.
2078                 */
2079                return 1;
2080        }
2081        /*
2082         * If this is a 1/2 or 1/4 width channel allow it if a full
2083         * width channel is present for this frequency, and the device
2084         * supports fractional channels on this band.  This is a hack
2085         * that avoids bloating the calibration table; it may be better
2086         * by per-band attributes though (we are effectively calculating
2087         * this attribute by scanning the channel list ourself).
2088         */
2089        if ((flags & (IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)) == 0)
2090                return 0;
2091        if (chanlookup(avail->ic_chans, avail->ic_nchans, freq,
2092            flags &~ (IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)) == NULL)
2093                return 0;
2094        if (flags & IEEE80211_CHAN_HALF) {
2095                return chanfind(avail->ic_chans, avail->ic_nchans,
2096                    IEEE80211_CHAN_HALF |
2097                       (flags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ)));
2098        } else {
2099                return chanfind(avail->ic_chans, avail->ic_nchans,
2100                    IEEE80211_CHAN_QUARTER |
2101                        (flags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ)));
2102        }
2103}
2104
2105static void
2106regdomain_addchans(struct ieee80211req_chaninfo *ci,
2107        const netband_head *bands,
2108        const struct ieee80211_regdomain *reg,
2109        uint32_t chanFlags,
2110        const struct ieee80211req_chaninfo *avail)
2111{
2112        const struct netband *nb;
2113        const struct freqband *b;
2114        struct ieee80211_channel *c, *prev;
2115        int freq, hi_adj, lo_adj, channelSep;
2116        uint32_t flags;
2117
2118        hi_adj = (chanFlags & IEEE80211_CHAN_HT40U) ? -20 : 0;
2119        lo_adj = (chanFlags & IEEE80211_CHAN_HT40D) ? 20 : 0;
2120        channelSep = (chanFlags & IEEE80211_CHAN_2GHZ) ? 0 : 40;
2121
2122        LIST_FOREACH(nb, bands, next) {
2123                b = nb->band;
2124                if (verbose) {
2125                        printf("%s:", __func__);
2126                        printb(" chanFlags", chanFlags, IEEE80211_CHAN_BITS);
2127                        printb(" bandFlags", nb->flags | b->flags,
2128                            IEEE80211_CHAN_BITS);
2129                        putchar('\n');
2130                }
2131                prev = NULL;
2132
2133                for (freq = b->freqStart + lo_adj;
2134                     freq <= b->freqEnd + hi_adj; freq += b->chanSep) {
2135                        /*
2136                         * Construct flags for the new channel.  We take
2137                         * the attributes from the band descriptions except
2138                         * for HT40 which is enabled generically (i.e. +/-
2139                         * extension channel) in the band description and
2140                         * then constrained according by channel separation.
2141                         */
2142                        flags = nb->flags | b->flags;
2143
2144                        /*
2145                         * VHT first - HT is a subset.
2146                         *
2147                         * XXX TODO: VHT80p80, VHT160 is not yet done.
2148                         */
2149                        if (flags & IEEE80211_CHAN_VHT) {
2150                                if ((chanFlags & IEEE80211_CHAN_VHT20) &&
2151                                    (flags & IEEE80211_CHAN_VHT20) == 0) {
2152                                        if (verbose)
2153                                                printf("%u: skip, not a "
2154                                                    "VHT20 channel\n", freq);
2155                                        continue;
2156                                }
2157                                if ((chanFlags & IEEE80211_CHAN_VHT40) &&
2158                                    (flags & IEEE80211_CHAN_VHT40) == 0) {
2159                                        if (verbose)
2160                                                printf("%u: skip, not a "
2161                                                    "VHT40 channel\n", freq);
2162                                        continue;
2163                                }
2164                                if ((chanFlags & IEEE80211_CHAN_VHT80) &&
2165                                    (flags & IEEE80211_CHAN_VHT80) == 0) {
2166                                        if (verbose)
2167                                                printf("%u: skip, not a "
2168                                                    "VHT80 channel\n", freq);
2169                                        continue;
2170                                }
2171
2172                                flags &= ~IEEE80211_CHAN_VHT;
2173                                flags |= chanFlags & IEEE80211_CHAN_VHT;
2174                        }
2175
2176                        /* Now, constrain HT */
2177                        if (flags & IEEE80211_CHAN_HT) {
2178                                /*
2179                                 * HT channels are generated specially; we're
2180                                 * called to add HT20, HT40+, and HT40- chan's
2181                                 * so we need to expand only band specs for
2182                                 * the HT channel type being added.
2183                                 */
2184                                if ((chanFlags & IEEE80211_CHAN_HT20) &&
2185                                    (flags & IEEE80211_CHAN_HT20) == 0) {
2186                                        if (verbose)
2187                                                printf("%u: skip, not an "
2188                                                    "HT20 channel\n", freq);
2189                                        continue;
2190                                }
2191                                if ((chanFlags & IEEE80211_CHAN_HT40) &&
2192                                    (flags & IEEE80211_CHAN_HT40) == 0) {
2193                                        if (verbose)
2194                                                printf("%u: skip, not an "
2195                                                    "HT40 channel\n", freq);
2196                                        continue;
2197                                }
2198                                /* NB: HT attribute comes from caller */
2199                                flags &= ~IEEE80211_CHAN_HT;
2200                                flags |= chanFlags & IEEE80211_CHAN_HT;
2201                        }
2202                        /*
2203                         * Check if device can operate on this frequency.
2204                         */
2205                        if (!checkchan(avail, freq, flags)) {
2206                                if (verbose) {
2207                                        printf("%u: skip, ", freq);
2208                                        printb("flags", flags,
2209                                            IEEE80211_CHAN_BITS);
2210                                        printf(" not available\n");
2211                                }
2212                                continue;
2213                        }
2214                        if ((flags & REQ_ECM) && !reg->ecm) {
2215                                if (verbose)
2216                                        printf("%u: skip, ECM channel\n", freq);
2217                                continue;
2218                        }
2219                        if ((flags & REQ_INDOOR) && reg->location == 'O') {
2220                                if (verbose)
2221                                        printf("%u: skip, indoor channel\n",
2222                                            freq);
2223                                continue;
2224                        }
2225                        if ((flags & REQ_OUTDOOR) && reg->location == 'I') {
2226                                if (verbose)
2227                                        printf("%u: skip, outdoor channel\n",
2228                                            freq);
2229                                continue;
2230                        }
2231                        if ((flags & IEEE80211_CHAN_HT40) &&
2232                            prev != NULL && (freq - prev->ic_freq) < channelSep) {
2233                                if (verbose)
2234                                        printf("%u: skip, only %u channel "
2235                                            "separation, need %d\n", freq,
2236                                            freq - prev->ic_freq, channelSep);
2237                                continue;
2238                        }
2239                        if (ci->ic_nchans == IEEE80211_CHAN_MAX) {
2240                                if (verbose)
2241                                        printf("%u: skip, channel table full\n",
2242                                            freq);
2243                                break;
2244                        }
2245                        c = &ci->ic_chans[ci->ic_nchans++];
2246                        memset(c, 0, sizeof(*c));
2247                        c->ic_freq = freq;
2248                        c->ic_flags = flags;
2249                if (c->ic_flags & IEEE80211_CHAN_DFS)
2250                                c->ic_maxregpower = nb->maxPowerDFS;
2251                        else
2252                                c->ic_maxregpower = nb->maxPower;
2253                        if (verbose) {
2254                                printf("[%3d] add freq %u ",
2255                                    ci->ic_nchans-1, c->ic_freq);
2256                                printb("flags", c->ic_flags, IEEE80211_CHAN_BITS);
2257                                printf(" power %u\n", c->ic_maxregpower);
2258                        }
2259                        /* NB: kernel fills in other fields */
2260                        prev = c;
2261                }
2262        }
2263}
2264
2265static void
2266regdomain_makechannels(
2267        struct ieee80211_regdomain_req *req,
2268        const struct ieee80211_devcaps_req *dc)
2269{
2270        struct regdata *rdp = getregdata();
2271        const struct country *cc;
2272        const struct ieee80211_regdomain *reg = &req->rd;
2273        struct ieee80211req_chaninfo *ci = &req->chaninfo;
2274        const struct regdomain *rd;
2275
2276        /*
2277         * Locate construction table for new channel list.  We treat
2278         * the regdomain/SKU as definitive so a country can be in
2279         * multiple with different properties (e.g. US in FCC+FCC3).
2280         * If no regdomain is specified then we fallback on the country
2281         * code to find the associated regdomain since countries always
2282         * belong to at least one regdomain.
2283         */
2284        if (reg->regdomain == 0) {
2285                cc = lib80211_country_findbycc(rdp, reg->country);
2286                if (cc == NULL)
2287                        errx(1, "internal error, country %d not found",
2288                            reg->country);
2289                rd = cc->rd;
2290        } else
2291                rd = lib80211_regdomain_findbysku(rdp, reg->regdomain);
2292        if (rd == NULL)
2293                errx(1, "internal error, regdomain %d not found",
2294                            reg->regdomain);
2295        if (rd->sku != SKU_DEBUG) {
2296                /*
2297                 * regdomain_addchans incrememnts the channel count for
2298                 * each channel it adds so initialize ic_nchans to zero.
2299                 * Note that we know we have enough space to hold all possible
2300                 * channels because the devcaps list size was used to
2301                 * allocate our request.
2302                 */
2303                ci->ic_nchans = 0;
2304                if (!LIST_EMPTY(&rd->bands_11b))
2305                        regdomain_addchans(ci, &rd->bands_11b, reg,
2306                            IEEE80211_CHAN_B, &dc->dc_chaninfo);
2307                if (!LIST_EMPTY(&rd->bands_11g))
2308                        regdomain_addchans(ci, &rd->bands_11g, reg,
2309                            IEEE80211_CHAN_G, &dc->dc_chaninfo);
2310                if (!LIST_EMPTY(&rd->bands_11a))
2311                        regdomain_addchans(ci, &rd->bands_11a, reg,
2312                            IEEE80211_CHAN_A, &dc->dc_chaninfo);
2313                if (!LIST_EMPTY(&rd->bands_11na) && dc->dc_htcaps != 0) {
2314                        regdomain_addchans(ci, &rd->bands_11na, reg,
2315                            IEEE80211_CHAN_A | IEEE80211_CHAN_HT20,
2316                            &dc->dc_chaninfo);
2317                        if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2318                                regdomain_addchans(ci, &rd->bands_11na, reg,
2319                                    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U,
2320                                    &dc->dc_chaninfo);
2321                                regdomain_addchans(ci, &rd->bands_11na, reg,
2322                                    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D,
2323                                    &dc->dc_chaninfo);
2324                        }
2325                }
2326                if (!LIST_EMPTY(&rd->bands_11ac) && dc->dc_vhtcaps != 0) {
2327                        regdomain_addchans(ci, &rd->bands_11ac, reg,
2328                            IEEE80211_CHAN_A | IEEE80211_CHAN_HT20 |
2329                            IEEE80211_CHAN_VHT20,
2330                            &dc->dc_chaninfo);
2331
2332                        /* VHT40 is a function of HT40.. */
2333                        if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2334                                regdomain_addchans(ci, &rd->bands_11ac, reg,
2335                                    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
2336                                    IEEE80211_CHAN_VHT40U,
2337                                    &dc->dc_chaninfo);
2338                                regdomain_addchans(ci, &rd->bands_11ac, reg,
2339                                    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
2340                                    IEEE80211_CHAN_VHT40D,
2341                                    &dc->dc_chaninfo);
2342                        }
2343
2344                        /* VHT80 */
2345                        /* XXX dc_vhtcap? */
2346                        if (1) {
2347                                regdomain_addchans(ci, &rd->bands_11ac, reg,
2348                                    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
2349                                    IEEE80211_CHAN_VHT80,
2350                                    &dc->dc_chaninfo);
2351                                regdomain_addchans(ci, &rd->bands_11ac, reg,
2352                                    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
2353                                    IEEE80211_CHAN_VHT80,
2354                                    &dc->dc_chaninfo);
2355                        }
2356
2357                        /* XXX TODO: VHT80_80, VHT160 */
2358                }
2359
2360                if (!LIST_EMPTY(&rd->bands_11ng) && dc->dc_htcaps != 0) {
2361                        regdomain_addchans(ci, &rd->bands_11ng, reg,
2362                            IEEE80211_CHAN_G | IEEE80211_CHAN_HT20,
2363                            &dc->dc_chaninfo);
2364                        if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2365                                regdomain_addchans(ci, &rd->bands_11ng, reg,
2366                                    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U,
2367                                    &dc->dc_chaninfo);
2368                                regdomain_addchans(ci, &rd->bands_11ng, reg,
2369                                    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D,
2370                                    &dc->dc_chaninfo);
2371                        }
2372                }
2373                qsort(ci->ic_chans, ci->ic_nchans, sizeof(ci->ic_chans[0]),
2374                    regdomain_sort);
2375        } else
2376                memcpy(ci, &dc->dc_chaninfo,
2377                    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
2378}
2379
2380static void
2381list_countries(void)
2382{
2383        struct regdata *rdp = getregdata();
2384        const struct country *cp;
2385        const struct regdomain *dp;
2386        int i;
2387
2388        i = 0;
2389        printf("\nCountry codes:\n");
2390        LIST_FOREACH(cp, &rdp->countries, next) {
2391                printf("%2s %-15.15s%s", cp->isoname,
2392                    cp->name, ((i+1)%4) == 0 ? "\n" : " ");
2393                i++;
2394        }
2395        i = 0;
2396        printf("\nRegulatory domains:\n");
2397        LIST_FOREACH(dp, &rdp->domains, next) {
2398                printf("%-15.15s%s", dp->name, ((i+1)%4) == 0 ? "\n" : " ");
2399                i++;
2400        }
2401        printf("\n");
2402}
2403
2404static void
2405defaultcountry(const struct regdomain *rd)
2406{
2407        struct regdata *rdp = getregdata();
2408        const struct country *cc;
2409
2410        cc = lib80211_country_findbycc(rdp, rd->cc->code);
2411        if (cc == NULL)
2412                errx(1, "internal error, ISO country code %d not "
2413                    "defined for regdomain %s", rd->cc->code, rd->name);
2414        regdomain.country = cc->code;
2415        regdomain.isocc[0] = cc->isoname[0];
2416        regdomain.isocc[1] = cc->isoname[1];
2417}
2418
2419static
2420DECL_CMD_FUNC(set80211regdomain, val, d)
2421{
2422        struct regdata *rdp = getregdata();
2423        const struct regdomain *rd;
2424
2425        rd = lib80211_regdomain_findbyname(rdp, val);
2426        if (rd == NULL) {
2427                char *eptr;
2428                long sku = strtol(val, &eptr, 0);
2429
2430                if (eptr != val)
2431                        rd = lib80211_regdomain_findbysku(rdp, sku);
2432                if (eptr == val || rd == NULL)
2433                        errx(1, "unknown regdomain %s", val);
2434        }
2435        getregdomain(s);
2436        regdomain.regdomain = rd->sku;
2437        if (regdomain.country == 0 && rd->cc != NULL) {
2438                /*
2439                 * No country code setup and there's a default
2440                 * one for this regdomain fill it in.
2441                 */
2442                defaultcountry(rd);
2443        }
2444        callback_register(setregdomain_cb, &regdomain);
2445}
2446
2447static
2448DECL_CMD_FUNC(set80211country, val, d)
2449{
2450        struct regdata *rdp = getregdata();
2451        const struct country *cc;
2452
2453        cc = lib80211_country_findbyname(rdp, val);
2454        if (cc == NULL) {
2455                char *eptr;
2456                long code = strtol(val, &eptr, 0);
2457
2458                if (eptr != val)
2459                        cc = lib80211_country_findbycc(rdp, code);
2460                if (eptr == val || cc == NULL)
2461                        errx(1, "unknown ISO country code %s", val);
2462        }
2463        getregdomain(s);
2464        regdomain.regdomain = cc->rd->sku;
2465        regdomain.country = cc->code;
2466        regdomain.isocc[0] = cc->isoname[0];
2467        regdomain.isocc[1] = cc->isoname[1];
2468        callback_register(setregdomain_cb, &regdomain);
2469}
2470
2471static void
2472set80211location(const char *val, int d, int s, const struct afswtch *rafp)
2473{
2474        getregdomain(s);
2475        regdomain.location = d;
2476        callback_register(setregdomain_cb, &regdomain);
2477}
2478
2479static void
2480set80211ecm(const char *val, int d, int s, const struct afswtch *rafp)
2481{
2482        getregdomain(s);
2483        regdomain.ecm = d;
2484        callback_register(setregdomain_cb, &regdomain);
2485}
2486
2487static void
2488LINE_INIT(char c)
2489{
2490        spacer = c;
2491        if (c == '\t')
2492                col = 8;
2493        else
2494                col = 1;
2495}
2496
2497static void
2498LINE_BREAK(void)
2499{
2500        if (spacer != '\t') {
2501                printf("\n");
2502                spacer = '\t';
2503        }
2504        col = 8;                /* 8-col tab */
2505}
2506
2507static void
2508LINE_CHECK(const char *fmt, ...)
2509{
2510        char buf[80];
2511        va_list ap;
2512        int n;
2513
2514        va_start(ap, fmt);
2515        n = vsnprintf(buf+1, sizeof(buf)-1, fmt, ap);
2516        va_end(ap);
2517        col += 1+n;
2518        if (col > MAXCOL) {
2519                LINE_BREAK();
2520                col += n;
2521        }
2522        buf[0] = spacer;
2523        printf("%s", buf);
2524        spacer = ' ';
2525}
2526
2527static int
2528getmaxrate(const uint8_t rates[15], uint8_t nrates)
2529{
2530        int i, maxrate = -1;
2531
2532        for (i = 0; i < nrates; i++) {
2533                int rate = rates[i] & IEEE80211_RATE_VAL;
2534                if (rate > maxrate)
2535                        maxrate = rate;
2536        }
2537        return maxrate / 2;
2538}
2539
2540static const char *
2541getcaps(int capinfo)
2542{
2543        static char capstring[32];
2544        char *cp = capstring;
2545
2546        if (capinfo & IEEE80211_CAPINFO_ESS)
2547                *cp++ = 'E';
2548        if (capinfo & IEEE80211_CAPINFO_IBSS)
2549                *cp++ = 'I';
2550        if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
2551                *cp++ = 'c';
2552        if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
2553                *cp++ = 'C';
2554        if (capinfo & IEEE80211_CAPINFO_PRIVACY)
2555                *cp++ = 'P';
2556        if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
2557                *cp++ = 'S';
2558        if (capinfo & IEEE80211_CAPINFO_PBCC)
2559                *cp++ = 'B';
2560        if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
2561                *cp++ = 'A';
2562        if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
2563                *cp++ = 's';
2564        if (capinfo & IEEE80211_CAPINFO_RSN)
2565                *cp++ = 'R';
2566        if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
2567                *cp++ = 'D';
2568        *cp = '\0';
2569        return capstring;
2570}
2571
2572static const char *
2573getflags(int flags)
2574{
2575        static char flagstring[32];
2576        char *cp = flagstring;
2577
2578        if (flags & IEEE80211_NODE_AUTH)
2579                *cp++ = 'A';
2580        if (flags & IEEE80211_NODE_QOS)
2581                *cp++ = 'Q';
2582        if (flags & IEEE80211_NODE_ERP)
2583                *cp++ = 'E';
2584        if (flags & IEEE80211_NODE_PWR_MGT)
2585                *cp++ = 'P';
2586        if (flags & IEEE80211_NODE_HT) {
2587                *cp++ = 'H';
2588                if (flags & IEEE80211_NODE_HTCOMPAT)
2589                        *cp++ = '+';
2590        }
2591        if (flags & IEEE80211_NODE_VHT)
2592                *cp++ = 'V';
2593        if (flags & IEEE80211_NODE_WPS)
2594                *cp++ = 'W';
2595        if (flags & IEEE80211_NODE_TSN)
2596                *cp++ = 'N';
2597        if (flags & IEEE80211_NODE_AMPDU_TX)
2598                *cp++ = 'T';
2599        if (flags & IEEE80211_NODE_AMPDU_RX)
2600                *cp++ = 'R';
2601        if (flags & IEEE80211_NODE_MIMO_PS) {
2602                *cp++ = 'M';
2603                if (flags & IEEE80211_NODE_MIMO_RTS)
2604                        *cp++ = '+';
2605        }
2606        if (flags & IEEE80211_NODE_RIFS)
2607                *cp++ = 'I';
2608        if (flags & IEEE80211_NODE_SGI40) {
2609                *cp++ = 'S';
2610                if (flags & IEEE80211_NODE_SGI20)
2611                        *cp++ = '+';
2612        } else if (flags & IEEE80211_NODE_SGI20)
2613                *cp++ = 's';
2614        if (flags & IEEE80211_NODE_AMSDU_TX)
2615                *cp++ = 't';
2616        if (flags & IEEE80211_NODE_AMSDU_RX)
2617                *cp++ = 'r';
2618        *cp = '\0';
2619        return flagstring;
2620}
2621
2622static void
2623printie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
2624{
2625        printf("%s", tag);
2626        if (verbose) {
2627                maxlen -= strlen(tag)+2;
2628                if (2*ielen > maxlen)
2629                        maxlen--;
2630                printf("<");
2631                for (; ielen > 0; ie++, ielen--) {
2632                        if (maxlen-- <= 0)
2633                                break;
2634                        printf("%02x", *ie);
2635                }
2636                if (ielen != 0)
2637                        printf("-");
2638                printf(">");
2639        }
2640}
2641
2642#define LE_READ_2(p)                                    \
2643        ((u_int16_t)                                    \
2644         ((((const u_int8_t *)(p))[0]      ) |          \
2645          (((const u_int8_t *)(p))[1] <<  8)))
2646#define LE_READ_4(p)                                    \
2647        ((u_int32_t)                                    \
2648         ((((const u_int8_t *)(p))[0]      ) |          \
2649          (((const u_int8_t *)(p))[1] <<  8) |          \
2650          (((const u_int8_t *)(p))[2] << 16) |          \
2651          (((const u_int8_t *)(p))[3] << 24)))
2652
2653/*
2654 * NB: The decoding routines assume a properly formatted ie
2655 *     which should be safe as the kernel only retains them
2656 *     if they parse ok.
2657 */
2658
2659static void
2660printwmeparam(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2661{
2662#define MS(_v, _f)      (((_v) & _f) >> _f##_S)
2663        static const char *acnames[] = { "BE", "BK", "VO", "VI" };
2664        const struct ieee80211_wme_param *wme =
2665            (const struct ieee80211_wme_param *) ie;
2666        int i;
2667
2668        printf("%s", tag);
2669        if (!verbose)
2670                return;
2671        printf("<qosinfo 0x%x", wme->param_qosInfo);
2672        ie += offsetof(struct ieee80211_wme_param, params_acParams);
2673        for (i = 0; i < WME_NUM_AC; i++) {
2674                const struct ieee80211_wme_acparams *ac =
2675                    &wme->params_acParams[i];
2676
2677                printf(" %s[%saifsn %u cwmin %u cwmax %u txop %u]"
2678                        , acnames[i]
2679                        , MS(ac->acp_aci_aifsn, WME_PARAM_ACM) ? "acm " : ""
2680                        , MS(ac->acp_aci_aifsn, WME_PARAM_AIFSN)
2681                        , MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMIN)
2682                        , MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMAX)
2683                        , LE_READ_2(&ac->acp_txop)
2684                );
2685        }
2686        printf(">");
2687#undef MS
2688}
2689
2690static void
2691printwmeinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2692{
2693        printf("%s", tag);
2694        if (verbose) {
2695                const struct ieee80211_wme_info *wme =
2696                    (const struct ieee80211_wme_info *) ie;
2697                printf("<version 0x%x info 0x%x>",
2698                    wme->wme_version, wme->wme_info);
2699        }
2700}
2701
2702static void
2703printvhtcap(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2704{
2705        printf("%s", tag);
2706        if (verbose) {
2707                const struct ieee80211_ie_vhtcap *vhtcap =
2708                    (const struct ieee80211_ie_vhtcap *) ie;
2709                uint32_t vhtcap_info = LE_READ_4(&vhtcap->vht_cap_info);
2710
2711                printf("<cap 0x%08x", vhtcap_info);
2712                printf(" rx_mcs_map 0x%x",
2713                    LE_READ_2(&vhtcap->supp_mcs.rx_mcs_map));
2714                printf(" rx_highest %d",
2715                    LE_READ_2(&vhtcap->supp_mcs.rx_highest) & 0x1fff);
2716                printf(" tx_mcs_map 0x%x",
2717                    LE_READ_2(&vhtcap->supp_mcs.tx_mcs_map));
2718                printf(" tx_highest %d",
2719                    LE_READ_2(&vhtcap->supp_mcs.tx_highest) & 0x1fff);
2720
2721                printf(">");
2722        }
2723}
2724
2725static void
2726printvhtinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2727{
2728        printf("%s", tag);
2729        if (verbose) {
2730                const struct ieee80211_ie_vht_operation *vhtinfo =
2731                    (const struct ieee80211_ie_vht_operation *) ie;
2732
2733                printf("<chw %d freq1_idx %d freq2_idx %d basic_mcs_set 0x%04x>",
2734                    vhtinfo->chan_width,
2735                    vhtinfo->center_freq_seg1_idx,
2736                    vhtinfo->center_freq_seg2_idx,
2737                    LE_READ_2(&vhtinfo->basic_mcs_set));
2738        }
2739}
2740
2741static void
2742printvhtpwrenv(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2743{
2744        printf("%s", tag);
2745        static const char *txpwrmap[] = {
2746                "20",
2747                "40",
2748                "80",
2749                "160",
2750        };
2751        if (verbose) {
2752                const struct ieee80211_ie_vht_txpwrenv *vhtpwr =
2753                    (const struct ieee80211_ie_vht_txpwrenv *) ie;
2754                int i, n;
2755                const char *sep = "";
2756
2757                /* Get count; trim at ielen */
2758                n = (vhtpwr->tx_info &
2759                    IEEE80211_VHT_TXPWRENV_INFO_COUNT_MASK) + 1;
2760                /* Trim at ielen */
2761                if (n > ielen - 3)
2762                        n = ielen - 3;
2763                printf("<tx_info 0x%02x pwr:[", vhtpwr->tx_info);
2764                for (i = 0; i < n; i++) {
2765                        printf("%s%s:%.2f", sep, txpwrmap[i],
2766                            ((float) ((int8_t) ie[i+3])) / 2.0);
2767                        sep = " ";
2768                }
2769
2770                printf("]>");
2771        }
2772}
2773
2774static void
2775printhtcap(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2776{
2777        printf("%s", tag);
2778        if (verbose) {
2779                const struct ieee80211_ie_htcap *htcap =
2780                    (const struct ieee80211_ie_htcap *) ie;
2781                const char *sep;
2782                int i, j;
2783
2784                printf("<cap 0x%x param 0x%x",
2785                    LE_READ_2(&htcap->hc_cap), htcap->hc_param);
2786                printf(" mcsset[");
2787                sep = "";
2788                for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2789                        if (isset(htcap->hc_mcsset, i)) {
2790                                for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2791                                        if (isclr(htcap->hc_mcsset, j))
2792                                                break;
2793                                j--;
2794                                if (i == j)
2795                                        printf("%s%u", sep, i);
2796                                else
2797                                        printf("%s%u-%u", sep, i, j);
2798                                i += j-i;
2799                                sep = ",";
2800                        }
2801                printf("] extcap 0x%x txbf 0x%x antenna 0x%x>",
2802                    LE_READ_2(&htcap->hc_extcap),
2803                    LE_READ_4(&htcap->hc_txbf),
2804                    htcap->hc_antenna);
2805        }
2806}
2807
2808static void
2809printhtinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2810{
2811        printf("%s", tag);
2812        if (verbose) {
2813                const struct ieee80211_ie_htinfo *htinfo =
2814                    (const struct ieee80211_ie_htinfo *) ie;
2815                const char *sep;
2816                int i, j;
2817
2818                printf("<ctl %u, %x,%x,%x,%x", htinfo->hi_ctrlchannel,
2819                    htinfo->hi_byte1, htinfo->hi_byte2, htinfo->hi_byte3,
2820                    LE_READ_2(&htinfo->hi_byte45));
2821                printf(" basicmcs[");
2822                sep = "";
2823                for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2824                        if (isset(htinfo->hi_basicmcsset, i)) {
2825                                for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2826                                        if (isclr(htinfo->hi_basicmcsset, j))
2827                                                break;
2828                                j--;
2829                                if (i == j)
2830                                        printf("%s%u", sep, i);
2831                                else
2832                                        printf("%s%u-%u", sep, i, j);
2833                                i += j-i;
2834                                sep = ",";
2835                        }
2836                printf("]>");
2837        }
2838}
2839
2840static void
2841printathie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2842{
2843
2844        printf("%s", tag);
2845        if (verbose) {
2846                const struct ieee80211_ath_ie *ath =
2847                        (const struct ieee80211_ath_ie *)ie;
2848
2849                printf("<");
2850                if (ath->ath_capability & ATHEROS_CAP_TURBO_PRIME)
2851                        printf("DTURBO,");
2852                if (ath->ath_capability & ATHEROS_CAP_COMPRESSION)
2853                        printf("COMP,");
2854                if (ath->ath_capability & ATHEROS_CAP_FAST_FRAME)
2855                        printf("FF,");
2856                if (ath->ath_capability & ATHEROS_CAP_XR)
2857                        printf("XR,");
2858                if (ath->ath_capability & ATHEROS_CAP_AR)
2859                        printf("AR,");
2860                if (ath->ath_capability & ATHEROS_CAP_BURST)
2861                        printf("BURST,");
2862                if (ath->ath_capability & ATHEROS_CAP_WME)
2863                        printf("WME,");
2864                if (ath->ath_capability & ATHEROS_CAP_BOOST)
2865                        printf("BOOST,");
2866                printf("0x%x>", LE_READ_2(ath->ath_defkeyix));
2867        }
2868}
2869
2870
2871static void
2872printmeshconf(const char *tag, const uint8_t *ie, size_t ielen, int maxlen)
2873{
2874
2875        printf("%s", tag);
2876        if (verbose) {
2877                const struct ieee80211_meshconf_ie *mconf =
2878                        (const struct ieee80211_meshconf_ie *)ie;
2879                printf("<PATH:");
2880                if (mconf->conf_pselid == IEEE80211_MESHCONF_PATH_HWMP)
2881                        printf("HWMP");
2882                else
2883                        printf("UNKNOWN");
2884                printf(" LINK:");
2885                if (mconf->conf_pmetid == IEEE80211_MESHCONF_METRIC_AIRTIME)
2886                        printf("AIRTIME");
2887                else
2888                        printf("UNKNOWN");
2889                printf(" CONGESTION:");
2890                if (mconf->conf_ccid == IEEE80211_MESHCONF_CC_DISABLED)
2891                        printf("DISABLED");
2892                else
2893                        printf("UNKNOWN");
2894                printf(" SYNC:");
2895                if (mconf->conf_syncid == IEEE80211_MESHCONF_SYNC_NEIGHOFF)
2896                        printf("NEIGHOFF");
2897                else
2898                        printf("UNKNOWN");
2899                printf(" AUTH:");
2900                if (mconf->conf_authid == IEEE80211_MESHCONF_AUTH_DISABLED)
2901                        printf("DISABLED");
2902                else
2903                        printf("UNKNOWN");
2904                printf(" FORM:0x%x CAPS:0x%x>", mconf->conf_form,
2905                    mconf->conf_cap);
2906        }
2907}
2908
2909static void
2910printbssload(const char *tag, const uint8_t *ie, size_t ielen, int maxlen)
2911{
2912        printf("%s", tag);
2913        if (verbose) {
2914                const struct ieee80211_bss_load_ie *bssload =
2915                    (const struct ieee80211_bss_load_ie *) ie;
2916                printf("<sta count %d, chan load %d, aac %d>",
2917                    LE_READ_2(&bssload->sta_count),
2918                    bssload->chan_load,
2919                    bssload->aac);
2920        }
2921}
2922
2923static void
2924printapchanrep(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2925{
2926        printf("%s", tag);
2927        if (verbose) {
2928                const struct ieee80211_ap_chan_report_ie *ap =
2929                    (const struct ieee80211_ap_chan_report_ie *) ie;
2930                const char *sep = "";
2931                int i;
2932
2933                printf("<class %u, chan:[", ap->i_class);
2934
2935                for (i = 3; i < ielen; i++) {
2936                        printf("%s%u", sep, ie[i]);
2937                        sep = ",";
2938                }
2939                printf("]>");
2940        }
2941}
2942
2943static const char *
2944wpa_cipher(const u_int8_t *sel)
2945{
2946#define WPA_SEL(x)      (((x)<<24)|WPA_OUI)
2947        u_int32_t w = LE_READ_4(sel);
2948
2949        switch (w) {
2950        case WPA_SEL(WPA_CSE_NULL):
2951                return "NONE";
2952        case WPA_SEL(WPA_CSE_WEP40):
2953                return "WEP40";
2954        case WPA_SEL(WPA_CSE_WEP104):
2955                return "WEP104";
2956        case WPA_SEL(WPA_CSE_TKIP):
2957                return "TKIP";
2958        case WPA_SEL(WPA_CSE_CCMP):
2959                return "AES-CCMP";
2960        }
2961        return "?";             /* NB: so 1<< is discarded */
2962#undef WPA_SEL
2963}
2964
2965static const char *
2966wpa_keymgmt(const u_int8_t *sel)
2967{
2968#define WPA_SEL(x)      (((x)<<24)|WPA_OUI)
2969        u_int32_t w = LE_READ_4(sel);
2970
2971        switch (w) {
2972        case WPA_SEL(WPA_ASE_8021X_UNSPEC):
2973                return "8021X-UNSPEC";
2974        case WPA_SEL(WPA_ASE_8021X_PSK):
2975                return "8021X-PSK";
2976        case WPA_SEL(WPA_ASE_NONE):
2977                return "NONE";
2978        }
2979        return "?";
2980#undef WPA_SEL
2981}
2982
2983static void
2984printwpaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2985{
2986        u_int8_t len = ie[1];
2987
2988        printf("%s", tag);
2989        if (verbose) {
2990                const char *sep;
2991                int n;
2992
2993                ie += 6, len -= 4;              /* NB: len is payload only */
2994
2995                printf("<v%u", LE_READ_2(ie));
2996                ie += 2, len -= 2;
2997
2998                printf(" mc:%s", wpa_cipher(ie));
2999                ie += 4, len -= 4;
3000
3001                /* unicast ciphers */
3002                n = LE_READ_2(ie);
3003                ie += 2, len -= 2;
3004                sep = " uc:";
3005                for (; n > 0; n--) {
3006                        printf("%s%s", sep, wpa_cipher(ie));
3007                        ie += 4, len -= 4;
3008                        sep = "+";
3009                }
3010
3011                /* key management algorithms */
3012                n = LE_READ_2(ie);
3013                ie += 2, len -= 2;
3014                sep = " km:";
3015                for (; n > 0; n--) {
3016                        printf("%s%s", sep, wpa_keymgmt(ie));
3017                        ie += 4, len -= 4;
3018                        sep = "+";
3019                }
3020
3021                if (len > 2)            /* optional capabilities */
3022                        printf(", caps 0x%x", LE_READ_2(ie));
3023                printf(">");
3024        }
3025}
3026
3027static const char *
3028rsn_cipher(const u_int8_t *sel)
3029{
3030#define RSN_SEL(x)      (((x)<<24)|RSN_OUI)
3031        u_int32_t w = LE_READ_4(sel);
3032
3033        switch (w) {
3034        case RSN_SEL(RSN_CSE_NULL):
3035                return "NONE";
3036        case RSN_SEL(RSN_CSE_WEP40):
3037                return "WEP40";
3038        case RSN_SEL(RSN_CSE_WEP104):
3039                return "WEP104";
3040        case RSN_SEL(RSN_CSE_TKIP):
3041                return "TKIP";
3042        case RSN_SEL(RSN_CSE_CCMP):
3043                return "AES-CCMP";
3044        case RSN_SEL(RSN_CSE_WRAP):
3045                return "AES-OCB";
3046        }
3047        return "?";
3048#undef WPA_SEL
3049}
3050
3051static const char *
3052rsn_keymgmt(const u_int8_t *sel)
3053{
3054#define RSN_SEL(x)      (((x)<<24)|RSN_OUI)
3055        u_int32_t w = LE_READ_4(sel);
3056
3057        switch (w) {
3058        case RSN_SEL(RSN_ASE_8021X_UNSPEC):
3059                return "8021X-UNSPEC";
3060        case RSN_SEL(RSN_ASE_8021X_PSK):
3061                return "8021X-PSK";
3062        case RSN_SEL(RSN_ASE_NONE):
3063                return "NONE";
3064        }
3065        return "?";
3066#undef RSN_SEL
3067}
3068
3069static void
3070printrsnie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
3071{
3072        printf("%s", tag);
3073        if (verbose) {
3074                const char *sep;
3075                int n;
3076
3077                ie += 2, ielen -= 2;
3078
3079                printf("<v%u", LE_READ_2(ie));
3080                ie += 2, ielen -= 2;
3081
3082                printf(" mc:%s", rsn_cipher(ie));
3083                ie += 4, ielen -= 4;
3084
3085                /* unicast ciphers */
3086                n = LE_READ_2(ie);
3087                ie += 2, ielen -= 2;
3088                sep = " uc:";
3089                for (; n > 0; n--) {
3090                        printf("%s%s", sep, rsn_cipher(ie));
3091                        ie += 4, ielen -= 4;
3092                        sep = "+";
3093                }
3094
3095                /* key management algorithms */
3096                n = LE_READ_2(ie);
3097                ie += 2, ielen -= 2;
3098                sep = " km:";
3099                for (; n > 0; n--) {
3100                        printf("%s%s", sep, rsn_keymgmt(ie));
3101                        ie += 4, ielen -= 4;
3102                        sep = "+";
3103                }
3104
3105                if (ielen > 2)          /* optional capabilities */
3106                        printf(", caps 0x%x", LE_READ_2(ie));
3107                /* XXXPMKID */
3108                printf(">");
3109        }
3110}
3111
3112/* XXX move to a public include file */
3113#define IEEE80211_WPS_DEV_PASS_ID       0x1012
3114#define IEEE80211_WPS_SELECTED_REG      0x1041
3115#define IEEE80211_WPS_SETUP_STATE       0x1044
3116#define IEEE80211_WPS_UUID_E            0x1047
3117#define IEEE80211_WPS_VERSION           0x104a
3118
3119#define BE_READ_2(p)                                    \
3120        ((u_int16_t)                                    \
3121         ((((const u_int8_t *)(p))[1]      ) |          \
3122          (((const u_int8_t *)(p))[0] <<  8)))
3123
3124static void
3125printwpsie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
3126{
3127        u_int8_t len = ie[1];
3128
3129        printf("%s", tag);
3130        if (verbose) {
3131                static const char *dev_pass_id[] = {
3132                        "D",    /* Default (PIN) */
3133                        "U",    /* User-specified */
3134                        "M",    /* Machine-specified */
3135                        "K",    /* Rekey */
3136                        "P",    /* PushButton */
3137                        "R"     /* Registrar-specified */
3138                };
3139                int n;
3140
3141                ie +=6, len -= 4;               /* NB: len is payload only */
3142
3143                /* WPS IE in Beacon and Probe Resp frames have different fields */
3144                printf("<");
3145                while (len) {
3146                        uint16_t tlv_type = BE_READ_2(ie);
3147                        uint16_t tlv_len  = BE_READ_2(ie + 2);
3148
3149                        ie += 4, len -= 4;
3150
3151                        switch (tlv_type) {
3152                        case IEEE80211_WPS_VERSION:
3153                                printf("v:%d.%d", *ie >> 4, *ie & 0xf);
3154                                break;
3155                        case IEEE80211_WPS_SETUP_STATE:
3156                                /* Only 1 and 2 are valid */
3157                                if (*ie == 0 || *ie >= 3)
3158                                        printf(" state:B");
3159                                else
3160                                        printf(" st:%s", *ie == 1 ? "N" : "C");
3161                                break;
3162                        case IEEE80211_WPS_SELECTED_REG:
3163                                printf(" sel:%s", *ie ? "T" : "F");
3164                                break;
3165                        case IEEE80211_WPS_DEV_PASS_ID:
3166                                n = LE_READ_2(ie);
3167                                if (n < nitems(dev_pass_id))
3168                                        printf(" dpi:%s", dev_pass_id[n]);
3169                                break;
3170                        case IEEE80211_WPS_UUID_E:
3171                                printf(" uuid-e:");
3172                                for (n = 0; n < (tlv_len - 1); n++)
3173                                        printf("%02x-", ie[n]);
3174                                printf("%02x", ie[n]);
3175                                break;
3176                        }
3177                        ie += tlv_len, len -= tlv_len;
3178                }
3179                printf(">");
3180        }
3181}
3182
3183static void
3184printtdmaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
3185{
3186        printf("%s", tag);
3187        if (verbose && ielen >= sizeof(struct ieee80211_tdma_param)) {
3188                const struct ieee80211_tdma_param *tdma =
3189                   (const struct ieee80211_tdma_param *) ie;
3190
3191                /* XXX tstamp */
3192                printf("<v%u slot:%u slotcnt:%u slotlen:%u bintval:%u inuse:0x%x>",
3193                    tdma->tdma_version, tdma->tdma_slot, tdma->tdma_slotcnt,
3194                    LE_READ_2(&tdma->tdma_slotlen), tdma->tdma_bintval,
3195                    tdma->tdma_inuse[0]);
3196        }
3197}
3198
3199/*
3200 * Copy the ssid string contents into buf, truncating to fit.  If the
3201 * ssid is entirely printable then just copy intact.  Otherwise convert
3202 * to hexadecimal.  If the result is truncated then replace the last
3203 * three characters with "...".
3204 */
3205static int
3206copy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
3207{
3208        const u_int8_t *p;
3209        size_t maxlen;
3210        u_int i;
3211
3212        if (essid_len > bufsize)
3213                maxlen = bufsize;
3214        else
3215                maxlen = essid_len;
3216        /* determine printable or not */
3217        for (i = 0, p = essid; i < maxlen; i++, p++) {
3218                if (*p < ' ' || *p > 0x7e)
3219                        break;
3220        }
3221        if (i != maxlen) {              /* not printable, print as hex */
3222                if (bufsize < 3)
3223                        return 0;
3224                strlcpy(buf, "0x", bufsize);
3225                bufsize -= 2;
3226                p = essid;
3227                for (i = 0; i < maxlen && bufsize >= 2; i++) {
3228                        sprintf(&buf[2+2*i], "%02x", p[i]);
3229                        bufsize -= 2;
3230                }
3231                if (i != essid_len)
3232                        memcpy(&buf[2+2*i-3], "...", 3);
3233        } else {                        /* printable, truncate as needed */
3234                memcpy(buf, essid, maxlen);
3235                if (maxlen != essid_len)
3236                        memcpy(&buf[maxlen-3], "...", 3);
3237        }
3238        return maxlen;
3239}
3240
3241static void
3242printssid(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
3243{
3244        char ssid[2*IEEE80211_NWID_LEN+1];
3245
3246        printf("%s<%.*s>", tag, copy_essid(ssid, maxlen, ie+2, ie[1]), ssid);
3247}
3248
3249static void
3250printrates(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
3251{
3252        const char *sep;
3253        int i;
3254
3255        printf("%s", tag);
3256        sep = "<";
3257        for (i = 2; i < ielen; i++) {
3258                printf("%s%s%d", sep,
3259                    ie[i] & IEEE80211_RATE_BASIC ? "B" : "",
3260                    ie[i] & IEEE80211_RATE_VAL);
3261                sep = ",";
3262        }
3263        printf(">");
3264}
3265
3266static void
3267printcountry(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
3268{
3269        const struct ieee80211_country_ie *cie =
3270           (const struct ieee80211_country_ie *) ie;
3271        int i, nbands, schan, nchan;
3272
3273        printf("%s<%c%c%c", tag, cie->cc[0], cie->cc[1], cie->cc[2]);
3274        nbands = (cie->len - 3) / sizeof(cie->band[0]);
3275        for (i = 0; i < nbands; i++) {
3276                schan = cie->band[i].schan;
3277                nchan = cie->band[i].nchan;
3278                if (nchan != 1)
3279                        printf(" %u-%u,%u", schan, schan + nchan-1,
3280                            cie->band[i].maxtxpwr);
3281                else
3282                        printf(" %u,%u", schan, cie->band[i].maxtxpwr);
3283        }
3284        printf(">");
3285}
3286
3287static __inline int
3288iswpaoui(const u_int8_t *frm)
3289{
3290        return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
3291}
3292
3293static __inline int
3294iswmeinfo(const u_int8_t *frm)
3295{
3296        return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3297                frm[6] == WME_INFO_OUI_SUBTYPE;
3298}
3299
3300static __inline int
3301iswmeparam(const u_int8_t *frm)
3302{
3303        return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3304                frm[6] == WME_PARAM_OUI_SUBTYPE;
3305}
3306
3307static __inline int
3308isatherosoui(const u_int8_t *frm)
3309{
3310        return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
3311}
3312
3313static __inline int
3314istdmaoui(const uint8_t *frm)
3315{
3316        return frm[1] > 3 && LE_READ_4(frm+2) == ((TDMA_OUI_TYPE<<24)|TDMA_OUI);
3317}
3318
3319static __inline int
3320iswpsoui(const uint8_t *frm)
3321{
3322        return frm[1] > 3 && LE_READ_4(frm+2) == ((WPS_OUI_TYPE<<24)|WPA_OUI);
3323}
3324
3325static const char *
3326iename(int elemid)
3327{
3328        switch (elemid) {
3329        case IEEE80211_ELEMID_FHPARMS:  return " FHPARMS";
3330        case IEEE80211_ELEMID_CFPARMS:  return " CFPARMS";
3331        case IEEE80211_ELEMID_TIM:      return " TIM";
3332        case IEEE80211_ELEMID_IBSSPARMS:return " IBSSPARMS";
3333        case IEEE80211_ELEMID_BSSLOAD:  return " BSSLOAD";
3334        case IEEE80211_ELEMID_CHALLENGE:return " CHALLENGE";
3335        case IEEE80211_ELEMID_PWRCNSTR: return " PWRCNSTR";
3336        case IEEE80211_ELEMID_PWRCAP:   return " PWRCAP";
3337        case IEEE80211_ELEMID_TPCREQ:   return " TPCREQ";
3338        case IEEE80211_ELEMID_TPCREP:   return " TPCREP";
3339        case IEEE80211_ELEMID_SUPPCHAN: return " SUPPCHAN";
3340        case IEEE80211_ELEMID_CSA:      return " CSA";
3341        case IEEE80211_ELEMID_MEASREQ:  return " MEASREQ";
3342        case IEEE80211_ELEMID_MEASREP:  return " MEASREP";
3343        case IEEE80211_ELEMID_QUIET:    return " QUIET";
3344        case IEEE80211_ELEMID_IBSSDFS:  return " IBSSDFS";
3345        case IEEE80211_ELEMID_TPC:      return " TPC";
3346        case IEEE80211_ELEMID_CCKM:     return " CCKM";
3347        }
3348        return " ???";
3349}
3350
3351static void
3352printies(const u_int8_t *vp, int ielen, int maxcols)
3353{
3354        while (ielen > 0) {
3355                switch (vp[0]) {
3356                case IEEE80211_ELEMID_SSID:
3357                        if (verbose)
3358                                printssid(" SSID", vp, 2+vp[1], maxcols);
3359                        break;
3360                case IEEE80211_ELEMID_RATES:
3361                case IEEE80211_ELEMID_XRATES:
3362                        if (verbose)
3363                                printrates(vp[0] == IEEE80211_ELEMID_RATES ?
3364                                    " RATES" : " XRATES", vp, 2+vp[1], maxcols);
3365                        break;
3366                case IEEE80211_ELEMID_DSPARMS:
3367                        if (verbose)
3368                                printf(" DSPARMS<%u>", vp[2]);
3369                        break;
3370                case IEEE80211_ELEMID_COUNTRY:
3371                        if (verbose)
3372                                printcountry(" COUNTRY", vp, 2+vp[1], maxcols);
3373                        break;
3374                case IEEE80211_ELEMID_ERP:
3375                        if (verbose)
3376                                printf(" ERP<0x%x>", vp[2]);
3377                        break;
3378                case IEEE80211_ELEMID_VENDOR:
3379                        if (iswpaoui(vp))
3380                                printwpaie(" WPA", vp, 2+vp[1], maxcols);
3381                        else if (iswmeinfo(vp))
3382                                printwmeinfo(" WME", vp, 2+vp[1], maxcols);
3383                        else if (iswmeparam(vp))
3384                                printwmeparam(" WME", vp, 2+vp[1], maxcols);
3385                        else if (isatherosoui(vp))
3386                                printathie(" ATH", vp, 2+vp[1], maxcols);
3387                        else if (iswpsoui(vp))
3388                                printwpsie(" WPS", vp, 2+vp[1], maxcols);
3389                        else if (istdmaoui(vp))
3390                                printtdmaie(" TDMA", vp, 2+vp[1], maxcols);
3391                        else if (verbose)
3392                                printie(" VEN", vp, 2+vp[1], maxcols);
3393                        break;
3394                case IEEE80211_ELEMID_RSN:
3395                        printrsnie(" RSN", vp, 2+vp[1], maxcols);
3396                        break;
3397                case IEEE80211_ELEMID_HTCAP:
3398                        printhtcap(" HTCAP", vp, 2+vp[1], maxcols);
3399                        break;
3400                case IEEE80211_ELEMID_HTINFO:
3401                        if (verbose)
3402                                printhtinfo(" HTINFO", vp, 2+vp[1], maxcols);
3403                        break;
3404                case IEEE80211_ELEMID_MESHID:
3405                        if (verbose)
3406                                printssid(" MESHID", vp, 2+vp[1], maxcols);
3407                        break;
3408                case IEEE80211_ELEMID_MESHCONF:
3409                        printmeshconf(" MESHCONF", vp, 2+vp[1], maxcols);
3410                        break;
3411                case IEEE80211_ELEMID_VHT_CAP:
3412                        printvhtcap(" VHTCAP", vp, 2+vp[1], maxcols);
3413                        break;
3414                case IEEE80211_ELEMID_VHT_OPMODE:
3415                        printvhtinfo(" VHTOPMODE", vp, 2+vp[1], maxcols);
3416                        break;
3417                case IEEE80211_ELEMID_VHT_PWR_ENV:
3418                        printvhtpwrenv(" VHTPWRENV", vp, 2+vp[1], maxcols);
3419                        break;
3420                case IEEE80211_ELEMID_BSSLOAD:
3421                        printbssload(" BSSLOAD", vp, 2+vp[1], maxcols);
3422                        break;
3423                case IEEE80211_ELEMID_APCHANREP:
3424                        printapchanrep(" APCHANREP", vp, 2+vp[1], maxcols);
3425                        break;
3426                default:
3427                        if (verbose)
3428                                printie(iename(vp[0]), vp, 2+vp[1], maxcols);
3429                        break;
3430                }
3431                ielen -= 2+vp[1];
3432                vp += 2+vp[1];
3433        }
3434}
3435
3436static void
3437printmimo(const struct ieee80211_mimo_info *mi)
3438{
3439        /* NB: don't muddy display unless there's something to show */
3440        if (mi->rssi[0] != 0 || mi->rssi[1] != 0 || mi->rssi[2] != 0) {
3441                /* XXX ignore EVM for now */
3442                printf(" (rssi %.1f:%.1f:%.1f nf %d:%d:%d)",
3443                    mi->rssi[0] / 2.0, mi->rssi[1] / 2.0, mi->rssi[2] / 2.0,
3444                    mi->noise[0], mi->noise[1], mi->noise[2]);
3445        }
3446}
3447
3448static void
3449list_scan(int s)
3450{
3451        uint8_t buf[24*1024];
3452        char ssid[IEEE80211_NWID_LEN+1];
3453        const uint8_t *cp;
3454        int len, ssidmax, idlen;
3455
3456        if (get80211len(s, IEEE80211_IOC_SCAN_RESULTS, buf, sizeof(buf), &len) < 0)
3457                errx(1, "unable to get scan results");
3458        if (len < sizeof(struct ieee80211req_scan_result))
3459                return;
3460
3461        getchaninfo(s);
3462
3463        ssidmax = verbose ? IEEE80211_NWID_LEN : 14;
3464        printf("%-*.*s  %-17.17s  %4s %4s   %-7s  %3s %4s\n"
3465                , ssidmax, ssidmax, "SSID/MESH ID"
3466                , "BSSID"
3467                , "CHAN"
3468                , "RATE"
3469                , " S:N"
3470                , "INT"
3471                , "CAPS"
3472        );
3473        cp = buf;
3474        do {
3475                const struct ieee80211req_scan_result *sr;
3476                const uint8_t *vp, *idp;
3477
3478                sr = (const struct ieee80211req_scan_result *) cp;
3479                vp = cp + sr->isr_ie_off;
3480                if (sr->isr_meshid_len) {
3481                        idp = vp + sr->isr_ssid_len;
3482                        idlen = sr->isr_meshid_len;
3483                } else {
3484                        idp = vp;
3485                        idlen = sr->isr_ssid_len;
3486                }
3487                printf("%-*.*s  %s  %3d  %3dM %4d:%-4d %4d %-4.4s"
3488                        , ssidmax
3489                          , copy_essid(ssid, ssidmax, idp, idlen)
3490                          , ssid
3491                        , ether_ntoa((const struct ether_addr *) sr->isr_bssid)
3492                        , ieee80211_mhz2ieee(sr->isr_freq, sr->isr_flags)
3493                        , getmaxrate(sr->isr_rates, sr->isr_nrates)
3494                        , (sr->isr_rssi/2)+sr->isr_noise, sr->isr_noise
3495                        , sr->isr_intval
3496                        , getcaps(sr->isr_capinfo)
3497                );
3498                printies(vp + sr->isr_ssid_len + sr->isr_meshid_len,
3499                    sr->isr_ie_len, 24);
3500                printf("\n");
3501                cp += sr->isr_len, len -= sr->isr_len;
3502        } while (len >= sizeof(struct ieee80211req_scan_result));
3503}
3504
3505static void
3506scan_and_wait(int s)
3507{
3508        struct ieee80211_scan_req sr;
3509        struct ieee80211req ireq;
3510        int sroute;
3511
3512        sroute = socket(PF_ROUTE, SOCK_RAW, 0);
3513        if (sroute < 0) {
3514                perror("socket(PF_ROUTE,SOCK_RAW)");
3515                return;
3516        }
3517        (void) memset(&ireq, 0, sizeof(ireq));
3518        (void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
3519        ireq.i_type = IEEE80211_IOC_SCAN_REQ;
3520
3521        memset(&sr, 0, sizeof(sr));
3522        sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE
3523                    | IEEE80211_IOC_SCAN_BGSCAN
3524                    | IEEE80211_IOC_SCAN_NOPICK
3525                    | IEEE80211_IOC_SCAN_ONCE;
3526        sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
3527        sr.sr_nssid = 0;
3528
3529        ireq.i_data = &sr;
3530        ireq.i_len = sizeof(sr);
3531        /*
3532         * NB: only root can trigger a scan so ignore errors. Also ignore
3533         * possible errors from net80211, even if no new scan could be
3534         * started there might still be a valid scan cache.
3535         */
3536        if (ioctl(s, SIOCS80211, &ireq) == 0) {
3537                char buf[2048];
3538                struct if_announcemsghdr *ifan;
3539                struct rt_msghdr *rtm;
3540
3541                do {
3542                        if (read(sroute, buf, sizeof(buf)) < 0) {
3543                                perror("read(PF_ROUTE)");
3544                                break;
3545                        }
3546                        rtm = (struct rt_msghdr *) buf;
3547                        if (rtm->rtm_version != RTM_VERSION)
3548                                break;
3549                        ifan = (struct if_announcemsghdr *) rtm;
3550                } while (rtm->rtm_type != RTM_IEEE80211 ||
3551                    ifan->ifan_what != RTM_IEEE80211_SCAN);
3552        }
3553        close(sroute);
3554}
3555
3556static
3557DECL_CMD_FUNC(set80211scan, val, d)
3558{
3559        scan_and_wait(s);
3560        list_scan(s);
3561}
3562
3563static enum ieee80211_opmode get80211opmode(int s);
3564
3565static int
3566gettxseq(const struct ieee80211req_sta_info *si)
3567{
3568        int i, txseq;
3569
3570        if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3571                return si->isi_txseqs[0];
3572        /* XXX not right but usually what folks want */
3573        txseq = 0;
3574        for (i = 0; i < IEEE80211_TID_SIZE; i++)
3575                if (si->isi_txseqs[i] > txseq)
3576                        txseq = si->isi_txseqs[i];
3577        return txseq;
3578}
3579
3580static int
3581getrxseq(const struct ieee80211req_sta_info *si)
3582{
3583        int i, rxseq;
3584
3585        if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3586                return si->isi_rxseqs[0];
3587        /* XXX not right but usually what folks want */
3588        rxseq = 0;
3589        for (i = 0; i < IEEE80211_TID_SIZE; i++)
3590                if (si->isi_rxseqs[i] > rxseq)
3591                        rxseq = si->isi_rxseqs[i];
3592        return rxseq;
3593}
3594
3595static void
3596list_stations(int s)
3597{
3598        union {
3599                struct ieee80211req_sta_req req;
3600                uint8_t buf[24*1024];
3601        } u;
3602        enum ieee80211_opmode opmode = get80211opmode(s);
3603        const uint8_t *cp;
3604        int len;
3605
3606        /* broadcast address =>'s get all stations */
3607        (void) memset(u.req.is_u.macaddr, 0xff, IEEE80211_ADDR_LEN);
3608        if (opmode == IEEE80211_M_STA) {
3609                /*
3610                 * Get information about the associated AP.
3611                 */
3612                (void) get80211(s, IEEE80211_IOC_BSSID,
3613                    u.req.is_u.macaddr, IEEE80211_ADDR_LEN);
3614        }
3615        if (get80211len(s, IEEE80211_IOC_STA_INFO, &u, sizeof(u), &len) < 0)
3616                errx(1, "unable to get station information");
3617        if (len < sizeof(struct ieee80211req_sta_info))
3618                return;
3619
3620        getchaninfo(s);
3621
3622        if (opmode == IEEE80211_M_MBSS)
3623                printf("%-17.17s %4s %5s %5s %7s %4s %4s %4s %6s %6s\n"
3624                        , "ADDR"
3625                        , "CHAN"
3626                        , "LOCAL"
3627                        , "PEER"
3628                        , "STATE"
3629                        , "RATE"
3630                        , "RSSI"
3631                        , "IDLE"
3632                        , "TXSEQ"
3633                        , "RXSEQ"
3634                );
3635        else
3636                printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %-7s\n"
3637                        , "ADDR"
3638                        , "AID"
3639                        , "CHAN"
3640                        , "RATE"
3641                        , "RSSI"
3642                        , "IDLE"
3643                        , "TXSEQ"
3644                        , "RXSEQ"
3645                        , "CAPS"
3646                        , "FLAG"
3647                );
3648        cp = (const uint8_t *) u.req.info;
3649        do {
3650                const struct ieee80211req_sta_info *si;
3651
3652                si = (const struct ieee80211req_sta_info *) cp;
3653                if (si->isi_len < sizeof(*si))
3654                        break;
3655                if (opmode == IEEE80211_M_MBSS)
3656                        printf("%s %4d %5x %5x %7.7s %3dM %4.1f %4d %6d %6d"
3657                                , ether_ntoa((const struct ether_addr*)
3658                                    si->isi_macaddr)
3659                                , ieee80211_mhz2ieee(si->isi_freq,
3660                                    si->isi_flags)
3661                                , si->isi_localid
3662                                , si->isi_peerid
3663                                , mesh_linkstate_string(si->isi_peerstate)
3664                                , si->isi_txmbps/2
3665                                , si->isi_rssi/2.
3666                                , si->isi_inact
3667                                , gettxseq(si)
3668                                , getrxseq(si)
3669                        );
3670                else
3671                        printf("%s %4u %4d %3dM %4.1f %4d %6d %6d %-4.4s %-7.7s"
3672                                , ether_ntoa((const struct ether_addr*)
3673                                    si->isi_macaddr)
3674                                , IEEE80211_AID(si->isi_associd)
3675                                , ieee80211_mhz2ieee(si->isi_freq,
3676                                    si->isi_flags)
3677                                , si->isi_txmbps/2
3678                                , si->isi_rssi/2.
3679                                , si->isi_inact
3680                                , gettxseq(si)
3681                                , getrxseq(si)
3682                                , getcaps(si->isi_capinfo)
3683                                , getflags(si->isi_state)
3684                        );
3685                printies(cp + si->isi_ie_off, si->isi_ie_len, 24);
3686                printmimo(&si->isi_mimo);
3687                printf("\n");
3688                cp += si->isi_len, len -= si->isi_len;
3689        } while (len >= sizeof(struct ieee80211req_sta_info));
3690}
3691
3692static const char *
3693mesh_linkstate_string(uint8_t state)
3694{
3695        static const char *state_names[] = {
3696            [0] = "IDLE",
3697            [1] = "OPEN-TX",
3698            [2] = "OPEN-RX",
3699            [3] = "CONF-RX",
3700            [4] = "ESTAB",
3701            [5] = "HOLDING",
3702        };
3703
3704        if (state >= nitems(state_names)) {
3705                static char buf[10];
3706                snprintf(buf, sizeof(buf), "#%u", state);
3707                return buf;
3708        } else
3709                return state_names[state];
3710}
3711
3712static const char *
3713get_chaninfo(const struct ieee80211_channel *c, int precise,
3714        char buf[], size_t bsize)
3715{
3716        buf[0] = '\0';
3717        if (IEEE80211_IS_CHAN_FHSS(c))
3718                strlcat(buf, " FHSS", bsize);
3719        if (IEEE80211_IS_CHAN_A(c))
3720                strlcat(buf, " 11a", bsize);
3721        else if (IEEE80211_IS_CHAN_ANYG(c))
3722                strlcat(buf, " 11g", bsize);
3723        else if (IEEE80211_IS_CHAN_B(c))
3724                strlcat(buf, " 11b", bsize);
3725        if (IEEE80211_IS_CHAN_HALF(c))
3726                strlcat(buf, "/10MHz", bsize);
3727        if (IEEE80211_IS_CHAN_QUARTER(c))
3728                strlcat(buf, "/5MHz", bsize);
3729        if (IEEE80211_IS_CHAN_TURBO(c))
3730                strlcat(buf, " Turbo", bsize);
3731        if (precise) {
3732                /* XXX should make VHT80U, VHT80D */
3733                if (IEEE80211_IS_CHAN_VHT80(c) &&
3734                    IEEE80211_IS_CHAN_HT40D(c))
3735                        strlcat(buf, " vht/80-", bsize);
3736                else if (IEEE80211_IS_CHAN_VHT80(c) &&
3737                    IEEE80211_IS_CHAN_HT40U(c))
3738                        strlcat(buf, " vht/80+", bsize);
3739                else if (IEEE80211_IS_CHAN_VHT80(c))
3740                        strlcat(buf, " vht/80", bsize);
3741                else if (IEEE80211_IS_CHAN_VHT40D(c))
3742                        strlcat(buf, " vht/40-", bsize);
3743                else if (IEEE80211_IS_CHAN_VHT40U(c))
3744                        strlcat(buf, " vht/40+", bsize);
3745                else if (IEEE80211_IS_CHAN_VHT20(c))
3746                        strlcat(buf, " vht/20", bsize);
3747                else if (IEEE80211_IS_CHAN_HT20(c))
3748                        strlcat(buf, " ht/20", bsize);
3749                else if (IEEE80211_IS_CHAN_HT40D(c))
3750                        strlcat(buf, " ht/40-", bsize);
3751                else if (IEEE80211_IS_CHAN_HT40U(c))
3752                        strlcat(buf, " ht/40+", bsize);
3753        } else {
3754                if (IEEE80211_IS_CHAN_VHT(c))
3755                        strlcat(buf, " vht", bsize);
3756                else if (IEEE80211_IS_CHAN_HT(c))
3757                        strlcat(buf, " ht", bsize);
3758        }
3759        return buf;
3760}
3761
3762static void
3763print_chaninfo(const struct ieee80211_channel *c, int verb)
3764{
3765        char buf[14];
3766
3767        if (verb)
3768                printf("Channel %3u : %u%c%c%c%c%c MHz%-14.14s",
3769                    ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
3770                    IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
3771                    IEEE80211_IS_CHAN_DFS(c) ? 'D' : ' ',
3772                    IEEE80211_IS_CHAN_RADAR(c) ? 'R' : ' ',
3773                    IEEE80211_IS_CHAN_CWINT(c) ? 'I' : ' ',
3774                    IEEE80211_IS_CHAN_CACDONE(c) ? 'C' : ' ',
3775                    get_chaninfo(c, verb, buf, sizeof(buf)));
3776        else
3777        printf("Channel %3u : %u%c MHz%-14.14s",
3778            ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
3779            IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
3780            get_chaninfo(c, verb, buf, sizeof(buf)));
3781
3782}
3783
3784static int
3785chanpref(const struct ieee80211_channel *c)
3786{
3787        if (IEEE80211_IS_CHAN_VHT160(c))
3788                return 80;
3789        if (IEEE80211_IS_CHAN_VHT80_80(c))
3790                return 75;
3791        if (IEEE80211_IS_CHAN_VHT80(c))
3792                return 70;
3793        if (IEEE80211_IS_CHAN_VHT40(c))
3794                return 60;
3795        if (IEEE80211_IS_CHAN_VHT20(c))
3796                return 50;
3797        if (IEEE80211_IS_CHAN_HT40(c))
3798                return 40;
3799        if (IEEE80211_IS_CHAN_HT20(c))
3800                return 30;
3801        if (IEEE80211_IS_CHAN_HALF(c))
3802                return 10;
3803        if (IEEE80211_IS_CHAN_QUARTER(c))
3804                return 5;
3805        if (IEEE80211_IS_CHAN_TURBO(c))
3806                return 25;
3807        if (IEEE80211_IS_CHAN_A(c))
3808                return 20;
3809        if (IEEE80211_IS_CHAN_G(c))
3810                return 20;
3811        if (IEEE80211_IS_CHAN_B(c))
3812                return 15;
3813        if (IEEE80211_IS_CHAN_PUREG(c))
3814                return 15;
3815        return 0;
3816}
3817
3818static void
3819print_channels(int s, const struct ieee80211req_chaninfo *chans,
3820        int allchans, int verb)
3821{
3822        struct ieee80211req_chaninfo *achans;
3823        uint8_t reported[IEEE80211_CHAN_BYTES];
3824        const struct ieee80211_channel *c;
3825        int i, half;
3826
3827        achans = malloc(IEEE80211_CHANINFO_SPACE(chans));
3828        if (achans == NULL)
3829                errx(1, "no space for active channel list");
3830        achans->ic_nchans = 0;
3831        memset(reported, 0, sizeof(reported));
3832        if (!allchans) {
3833                struct ieee80211req_chanlist active;
3834
3835                if (get80211(s, IEEE80211_IOC_CHANLIST, &active, sizeof(active)) < 0)
3836                        errx(1, "unable to get active channel list");
3837                for (i = 0; i < chans->ic_nchans; i++) {
3838                        c = &chans->ic_chans[i];
3839                        if (!isset(active.ic_channels, c->ic_ieee))
3840                                continue;
3841                        /*
3842                         * Suppress compatible duplicates unless
3843                         * verbose.  The kernel gives us it's
3844                         * complete channel list which has separate
3845                         * entries for 11g/11b and 11a/turbo.
3846                         */
3847                        if (isset(reported, c->ic_ieee) && !verb) {
3848                                /* XXX we assume duplicates are adjacent */
3849                                achans->ic_chans[achans->ic_nchans-1] = *c;
3850                        } else {
3851                                achans->ic_chans[achans->ic_nchans++] = *c;
3852                                setbit(reported, c->ic_ieee);
3853                        }
3854                }
3855        } else {
3856                for (i = 0; i < chans->ic_nchans; i++) {
3857                        c = &chans->ic_chans[i];
3858                        /* suppress duplicates as above */
3859                        if (isset(reported, c->ic_ieee) && !verb) {
3860                                /* XXX we assume duplicates are adjacent */
3861                                struct ieee80211_channel *a =
3862                                    &achans->ic_chans[achans->ic_nchans-1];
3863                                if (chanpref(c) > chanpref(a))
3864                                        *a = *c;
3865                        } else {
3866                                achans->ic_chans[achans->ic_nchans++] = *c;
3867                                setbit(reported, c->ic_ieee);
3868                        }
3869                }
3870        }
3871        half = achans->ic_nchans / 2;
3872        if (achans->ic_nchans % 2)
3873                half++;
3874
3875        for (i = 0; i < achans->ic_nchans / 2; i++) {
3876                print_chaninfo(&achans->ic_chans[i], verb);
3877                print_chaninfo(&achans->ic_chans[half+i], verb);
3878                printf("\n");
3879        }
3880        if (achans->ic_nchans % 2) {
3881                print_chaninfo(&achans->ic_chans[i], verb);
3882                printf("\n");
3883        }
3884        free(achans);
3885}
3886
3887static void
3888list_channels(int s, int allchans)
3889{
3890        getchaninfo(s);
3891        print_channels(s, chaninfo, allchans, verbose);
3892}
3893
3894static void
3895print_txpow(const struct ieee80211_channel *c)
3896{
3897        printf("Channel %3u : %u MHz %3.1f reg %2d  ",
3898            c->ic_ieee, c->ic_freq,
3899            c->ic_maxpower/2., c->ic_maxregpower);
3900}
3901
3902static void
3903print_txpow_verbose(const struct ieee80211_channel *c)
3904{
3905        print_chaninfo(c, 1);
3906        printf("min %4.1f dBm  max %3.1f dBm  reg %2d dBm",
3907            c->ic_minpower/2., c->ic_maxpower/2., c->ic_maxregpower);
3908        /* indicate where regulatory cap limits power use */
3909        if (c->ic_maxpower > 2*c->ic_maxregpower)
3910                printf(" <");
3911}
3912
3913static void
3914list_txpow(int s)
3915{
3916        struct ieee80211req_chaninfo *achans;
3917        uint8_t reported[IEEE80211_CHAN_BYTES];
3918        struct ieee80211_channel *c, *prev;
3919        int i, half;
3920
3921        getchaninfo(s);
3922        achans = malloc(IEEE80211_CHANINFO_SPACE(chaninfo));
3923        if (achans == NULL)
3924                errx(1, "no space for active channel list");
3925        achans->ic_nchans = 0;
3926        memset(reported, 0, sizeof(reported));
3927        for (i = 0; i < chaninfo->ic_nchans; i++) {
3928                c = &chaninfo->ic_chans[i];
3929                /* suppress duplicates as above */
3930                if (isset(reported, c->ic_ieee) && !verbose) {
3931                        /* XXX we assume duplicates are adjacent */
3932                        assert(achans->ic_nchans > 0);
3933                        prev = &achans->ic_chans[achans->ic_nchans-1];
3934                        /* display highest power on channel */
3935                        if (c->ic_maxpower > prev->ic_maxpower)
3936                                *prev = *c;
3937                } else {
3938                        achans->ic_chans[achans->ic_nchans++] = *c;
3939                        setbit(reported, c->ic_ieee);
3940                }
3941        }
3942        if (!verbose) {
3943                half = achans->ic_nchans / 2;
3944                if (achans->ic_nchans % 2)
3945                        half++;
3946
3947                for (i = 0; i < achans->ic_nchans / 2; i++) {
3948                        print_txpow(&achans->ic_chans[i]);
3949                        print_txpow(&achans->ic_chans[half+i]);
3950                        printf("\n");
3951                }
3952                if (achans->ic_nchans % 2) {
3953                        print_txpow(&achans->ic_chans[i]);
3954                        printf("\n");
3955                }
3956        } else {
3957                for (i = 0; i < achans->ic_nchans; i++) {
3958                        print_txpow_verbose(&achans->ic_chans[i]);
3959                        printf("\n");
3960                }
3961        }
3962        free(achans);
3963}
3964
3965static void
3966list_keys(int s)
3967{
3968}
3969
3970static void
3971list_capabilities(int s)
3972{
3973        struct ieee80211_devcaps_req *dc;
3974
3975        if (verbose)
3976                dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
3977        else
3978                dc = malloc(IEEE80211_DEVCAPS_SIZE(1));
3979        if (dc == NULL)
3980                errx(1, "no space for device capabilities");
3981        dc->dc_chaninfo.ic_nchans = verbose ? MAXCHAN : 1;
3982        getdevcaps(s, dc);
3983        printb("drivercaps", dc->dc_drivercaps, IEEE80211_C_BITS);
3984        if (dc->dc_cryptocaps != 0 || verbose) {
3985                putchar('\n');
3986                printb("cryptocaps", dc->dc_cryptocaps, IEEE80211_CRYPTO_BITS);
3987        }
3988        if (dc->dc_htcaps != 0 || verbose) {
3989                putchar('\n');
3990                printb("htcaps", dc->dc_htcaps, IEEE80211_HTCAP_BITS);
3991        }
3992        if (dc->dc_vhtcaps != 0 || verbose) {
3993                putchar('\n');
3994                printb("vhtcaps", dc->dc_vhtcaps, IEEE80211_VHTCAP_BITS);
3995        }
3996
3997        putchar('\n');
3998        if (verbose) {
3999                chaninfo = &dc->dc_chaninfo;    /* XXX */
4000                print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, verbose);
4001        }
4002        free(dc);
4003}
4004
4005static int
4006get80211wme(int s, int param, int ac, int *val)
4007{
4008        struct ieee80211req ireq;
4009
4010        (void) memset(&ireq, 0, sizeof(ireq));
4011        (void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
4012        ireq.i_type = param;
4013        ireq.i_len = ac;
4014        if (ioctl(s, SIOCG80211, &ireq) < 0) {
4015                warn("cannot get WME parameter %d, ac %d%s",
4016                    param, ac & IEEE80211_WMEPARAM_VAL,
4017                    ac & IEEE80211_WMEPARAM_BSS ? " (BSS)" : "");
4018                return -1;
4019        }
4020        *val = ireq.i_val;
4021        return 0;
4022}
4023
4024static void
4025list_wme_aci(int s, const char *tag, int ac)
4026{
4027        int val;
4028
4029        printf("\t%s", tag);
4030
4031        /* show WME BSS parameters */
4032        if (get80211wme(s, IEEE80211_IOC_WME_CWMIN, ac, &val) != -1)
4033                printf(" cwmin %2u", val);
4034        if (get80211wme(s, IEEE80211_IOC_WME_CWMAX, ac, &val) != -1)
4035                printf(" cwmax %2u", val);
4036        if (get80211wme(s, IEEE80211_IOC_WME_AIFS, ac, &val) != -1)
4037                printf(" aifs %2u", val);
4038        if (get80211wme(s, IEEE80211_IOC_WME_TXOPLIMIT, ac, &val) != -1)
4039                printf(" txopLimit %3u", val);
4040        if (get80211wme(s, IEEE80211_IOC_WME_ACM, ac, &val) != -1) {
4041                if (val)
4042                        printf(" acm");
4043                else if (verbose)
4044                        printf(" -acm");
4045        }
4046        /* !BSS only */
4047        if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
4048                if (get80211wme(s, IEEE80211_IOC_WME_ACKPOLICY, ac, &val) != -1) {
4049                        if (!val)
4050                                printf(" -ack");
4051                        else if (verbose)
4052                                printf(" ack");
4053                }
4054        }
4055        printf("\n");
4056}
4057
4058static void
4059list_wme(int s)
4060{
4061        static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
4062        int ac;
4063
4064        if (verbose) {
4065                /* display both BSS and local settings */
4066                for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
4067        again:
4068                        if (ac & IEEE80211_WMEPARAM_BSS)
4069                                list_wme_aci(s, "     ", ac);
4070                        else
4071                                list_wme_aci(s, acnames[ac], ac);
4072                        if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
4073                                ac |= IEEE80211_WMEPARAM_BSS;
4074                                goto again;
4075                        } else
4076                                ac &= ~IEEE80211_WMEPARAM_BSS;
4077                }
4078        } else {
4079                /* display only channel settings */
4080                for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++)
4081                        list_wme_aci(s, acnames[ac], ac);
4082        }
4083}
4084
4085static void
4086list_roam(int s)
4087{
4088        const struct ieee80211_roamparam *rp;
4089        int mode;
4090
4091        getroam(s);
4092        for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
4093                rp = &roamparams.params[mode];
4094                if (rp->rssi == 0 && rp->rate == 0)
4095                        continue;
4096                if (mode == IEEE80211_MODE_11NA || mode == IEEE80211_MODE_11NG) {
4097                        if (rp->rssi & 1)
4098                                LINE_CHECK("roam:%-7.7s rssi %2u.5dBm  MCS %2u    ",
4099                                    modename[mode], rp->rssi/2,
4100                                    rp->rate &~ IEEE80211_RATE_MCS);
4101                        else
4102                                LINE_CHECK("roam:%-7.7s rssi %4udBm  MCS %2u    ",
4103                                    modename[mode], rp->rssi/2,
4104                                    rp->rate &~ IEEE80211_RATE_MCS);
4105                } else {
4106                        if (rp->rssi & 1)
4107                                LINE_CHECK("roam:%-7.7s rssi %2u.5dBm rate %2u Mb/s",
4108                                    modename[mode], rp->rssi/2, rp->rate/2);
4109                        else
4110                                LINE_CHECK("roam:%-7.7s rssi %4udBm rate %2u Mb/s",
4111                                    modename[mode], rp->rssi/2, rp->rate/2);
4112                }
4113        }
4114}
4115
4116static void
4117list_txparams(int s)
4118{
4119        const struct ieee80211_txparam *tp;
4120        int mode;
4121
4122        gettxparams(s);
4123        for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
4124                tp = &txparams.params[mode];
4125                if (tp->mgmtrate == 0 && tp->mcastrate == 0)
4126                        continue;
4127                if (mode == IEEE80211_MODE_11NA || mode == IEEE80211_MODE_11NG) {
4128                        if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
4129                                LINE_CHECK("%-7.7s ucast NONE    mgmt %2u MCS  "
4130                                    "mcast %2u MCS  maxretry %u",
4131                                    modename[mode],
4132                                    tp->mgmtrate &~ IEEE80211_RATE_MCS,
4133                                    tp->mcastrate &~ IEEE80211_RATE_MCS,
4134                                    tp->maxretry);
4135                        else
4136                                LINE_CHECK("%-7.7s ucast %2u MCS  mgmt %2u MCS  "
4137                                    "mcast %2u MCS  maxretry %u",
4138                                    modename[mode],
4139                                    tp->ucastrate &~ IEEE80211_RATE_MCS,
4140                                    tp->mgmtrate &~ IEEE80211_RATE_MCS,
4141                                    tp->mcastrate &~ IEEE80211_RATE_MCS,
4142                                    tp->maxretry);
4143                } else {
4144                        if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
4145                                LINE_CHECK("%-7.7s ucast NONE    mgmt %2u Mb/s "
4146                                    "mcast %2u Mb/s maxretry %u",
4147                                    modename[mode],
4148                                    tp->mgmtrate/2,
4149                                    tp->mcastrate/2, tp->maxretry);
4150                        else
4151                                LINE_CHECK("%-7.7s ucast %2u Mb/s mgmt %2u Mb/s "
4152                                    "mcast %2u Mb/s maxretry %u",
4153                                    modename[mode],
4154                                    tp->ucastrate/2, tp->mgmtrate/2,
4155                                    tp->mcastrate/2, tp->maxretry);
4156                }
4157        }
4158}
4159
4160static void
4161printpolicy(int policy)
4162{
4163        switch (policy) {
4164        case IEEE80211_MACCMD_POLICY_OPEN:
4165                printf("policy: open\n");
4166                break;
4167        case IEEE80211_MACCMD_POLICY_ALLOW:
4168                printf("policy: allow\n");
4169                break;
4170        case IEEE80211_MACCMD_POLICY_DENY:
4171                printf("policy: deny\n");
4172                break;
4173        case IEEE80211_MACCMD_POLICY_RADIUS:
4174                printf("policy: radius\n");
4175                break;
4176        default:
4177                printf("policy: unknown (%u)\n", policy);
4178                break;
4179        }
4180}
4181
4182static void
4183list_mac(int s)
4184{
4185        struct ieee80211req ireq;
4186        struct ieee80211req_maclist *acllist;
4187        int i, nacls, policy, len;
4188        uint8_t *data;
4189        char c;
4190
4191        (void) memset(&ireq, 0, sizeof(ireq));
4192        (void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
4193        ireq.i_type = IEEE80211_IOC_MACCMD;
4194        ireq.i_val = IEEE80211_MACCMD_POLICY;
4195        if (ioctl(s, SIOCG80211, &ireq) < 0) {
4196                if (errno == EINVAL) {
4197                        printf("No acl policy loaded\n");
4198                        return;
4199                }
4200                err(1, "unable to get mac policy");
4201        }
4202        policy = ireq.i_val;
4203        if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
4204                c = '*';
4205        } else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
4206                c = '+';
4207        } else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
4208                c = '-';
4209        } else if (policy == IEEE80211_MACCMD_POLICY_RADIUS) {
4210                c = 'r';                /* NB: should never have entries */
4211        } else {
4212                printf("policy: unknown (%u)\n", policy);
4213                c = '?';
4214        }
4215        if (verbose || c == '?')
4216                printpolicy(policy);
4217
4218        ireq.i_val = IEEE80211_MACCMD_LIST;
4219        ireq.i_len = 0;
4220        if (ioctl(s, SIOCG80211, &ireq) < 0)
4221                err(1, "unable to get mac acl list size");
4222        if (ireq.i_len == 0) {          /* NB: no acls */
4223                if (!(verbose || c == '?'))
4224                        printpolicy(policy);
4225                return;
4226        }
4227        len = ireq.i_len;
4228
4229        data = malloc(len);
4230        if (data == NULL)
4231                err(1, "out of memory for acl list");
4232
4233        ireq.i_data = data;
4234        if (ioctl(s, SIOCG80211, &ireq) < 0)
4235                err(1, "unable to get mac acl list");
4236        nacls = len / sizeof(*acllist);
4237        acllist = (struct ieee80211req_maclist *) data;
4238        for (i = 0; i < nacls; i++)
4239                printf("%c%s\n", c, ether_ntoa(
4240                        (const struct ether_addr *) acllist[i].ml_macaddr));
4241        free(data);
4242}
4243
4244static void
4245print_regdomain(const struct ieee80211_regdomain *reg, int verb)
4246{
4247        if ((reg->regdomain != 0 &&
4248            reg->regdomain != reg->country) || verb) {
4249                const struct regdomain *rd =
4250                    lib80211_regdomain_findbysku(getregdata(), reg->regdomain);
4251                if (rd == NULL)
4252                        LINE_CHECK("regdomain %d", reg->regdomain);
4253                else
4254                        LINE_CHECK("regdomain %s", rd->name);
4255        }
4256        if (reg->country != 0 || verb) {
4257                const struct country *cc =
4258                    lib80211_country_findbycc(getregdata(), reg->country);
4259                if (cc == NULL)
4260                        LINE_CHECK("country %d", reg->country);
4261                else
4262                        LINE_CHECK("country %s", cc->isoname);
4263        }
4264        if (reg->location == 'I')
4265                LINE_CHECK("indoor");
4266        else if (reg->location == 'O')
4267                LINE_CHECK("outdoor");
4268        else if (verb)
4269                LINE_CHECK("anywhere");
4270        if (reg->ecm)
4271                LINE_CHECK("ecm");
4272        else if (verb)
4273                LINE_CHECK("-ecm");
4274}
4275
4276static void
4277list_regdomain(int s, int channelsalso)
4278{
4279        getregdomain(s);
4280        if (channelsalso) {
4281                getchaninfo(s);
4282                spacer = ':';
4283                print_regdomain(&regdomain, 1);
4284                LINE_BREAK();
4285                print_channels(s, chaninfo, 1/*allchans*/, 1/*verbose*/);
4286        } else
4287                print_regdomain(&regdomain, verbose);
4288}
4289
4290static void
4291list_mesh(int s)
4292{
4293        struct ieee80211req ireq;
4294        struct ieee80211req_mesh_route routes[128];
4295        struct ieee80211req_mesh_route *rt;
4296
4297        (void) memset(&ireq, 0, sizeof(ireq));
4298        (void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
4299        ireq.i_type = IEEE80211_IOC_MESH_RTCMD;
4300        ireq.i_val = IEEE80211_MESH_RTCMD_LIST;
4301        ireq.i_data = &routes;
4302        ireq.i_len = sizeof(routes);
4303        if (ioctl(s, SIOCG80211, &ireq) < 0)
4304                err(1, "unable to get the Mesh routing table");
4305
4306        printf("%-17.17s %-17.17s %4s %4s %4s %6s %s\n"
4307                , "DEST"
4308                , "NEXT HOP"
4309                , "HOPS"
4310                , "METRIC"
4311                , "LIFETIME"
4312                , "MSEQ"
4313                , "FLAGS");
4314
4315        for (rt = &routes[0]; rt - &routes[0] < ireq.i_len / sizeof(*rt); rt++){
4316                printf("%s ",
4317                    ether_ntoa((const struct ether_addr *)rt->imr_dest));
4318                printf("%s %4u   %4u   %6u %6u    %c%c\n",
4319                        ether_ntoa((const struct ether_addr *)rt->imr_nexthop),
4320                        rt->imr_nhops, rt->imr_metric, rt->imr_lifetime,
4321                        rt->imr_lastmseq,
4322                        (rt->imr_flags & IEEE80211_MESHRT_FLAGS_DISCOVER) ?
4323                            'D' :
4324                        (rt->imr_flags & IEEE80211_MESHRT_FLAGS_VALID) ?
4325                            'V' : '!',
4326                        (rt->imr_flags & IEEE80211_MESHRT_FLAGS_PROXY) ?
4327                            'P' :
4328                        (rt->imr_flags & IEEE80211_MESHRT_FLAGS_GATE) ?
4329                            'G' :' ');
4330        }
4331}
4332
4333static
4334DECL_CMD_FUNC(set80211list, arg, d)
4335{
4336#define iseq(a,b)       (strncasecmp(a,b,sizeof(b)-1) == 0)
4337
4338        LINE_INIT('\t');
4339
4340        if (iseq(arg, "sta"))
4341                list_stations(s);
4342        else if (iseq(arg, "scan") || iseq(arg, "ap"))
4343                list_scan(s);
4344        else if (iseq(arg, "chan") || iseq(arg, "freq"))
4345                list_channels(s, 1);
4346        else if (iseq(arg, "active"))
4347                list_channels(s, 0);
4348        else if (iseq(arg, "keys"))
4349                list_keys(s);
4350        else if (iseq(arg, "caps"))
4351                list_capabilities(s);
4352        else if (iseq(arg, "wme") || iseq(arg, "wmm"))
4353                list_wme(s);
4354        else if (iseq(arg, "mac"))
4355                list_mac(s);
4356        else if (iseq(arg, "txpow"))
4357                list_txpow(s);
4358        else if (iseq(arg, "roam"))
4359                list_roam(s);
4360        else if (iseq(arg, "txparam") || iseq(arg, "txparm"))
4361                list_txparams(s);
4362        else if (iseq(arg, "regdomain"))
4363                list_regdomain(s, 1);
4364        else if (iseq(arg, "countries"))
4365                list_countries();
4366        else if (iseq(arg, "mesh"))
4367                list_mesh(s);
4368        else
4369                errx(1, "Don't know how to list %s for %s", arg, name);
4370        LINE_BREAK();
4371#undef iseq
4372}
4373
4374static enum ieee80211_opmode
4375get80211opmode(int s)
4376{
4377        struct ifmediareq ifmr;
4378
4379        (void) memset(&ifmr, 0, sizeof(ifmr));
4380        (void) strlcpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
4381
4382        if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
4383                if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) {
4384                        if (ifmr.ifm_current & IFM_FLAG0)
4385                                return IEEE80211_M_AHDEMO;
4386                        else
4387                                return IEEE80211_M_IBSS;
4388                }
4389                if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
4390                        return IEEE80211_M_HOSTAP;
4391                if (ifmr.ifm_current & IFM_IEEE80211_IBSS)
4392                        return IEEE80211_M_IBSS;
4393                if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
4394                        return IEEE80211_M_MONITOR;
4395                if (ifmr.ifm_current & IFM_IEEE80211_MBSS)
4396                        return IEEE80211_M_MBSS;
4397        }
4398        return IEEE80211_M_STA;
4399}
4400
4401#if 0
4402static void
4403printcipher(int s, struct ieee80211req *ireq, int keylenop)
4404{
4405        switch (ireq->i_val) {
4406        case IEEE80211_CIPHER_WEP:
4407                ireq->i_type = keylenop;
4408                if (ioctl(s, SIOCG80211, ireq) != -1)
4409                        printf("WEP-%s",
4410                            ireq->i_len <= 5 ? "40" :
4411                            ireq->i_len <= 13 ? "104" : "128");
4412                else
4413                        printf("WEP");
4414                break;
4415        case IEEE80211_CIPHER_TKIP:
4416                printf("TKIP");
4417                break;
4418        case IEEE80211_CIPHER_AES_OCB:
4419                printf("AES-OCB");
4420                break;
4421        case IEEE80211_CIPHER_AES_CCM:
4422                printf("AES-CCM");
4423                break;
4424        case IEEE80211_CIPHER_CKIP:
4425                printf("CKIP");
4426                break;
4427        case IEEE80211_CIPHER_NONE:
4428                printf("NONE");
4429                break;
4430        default:
4431                printf("UNKNOWN (0x%x)", ireq->i_val);
4432                break;
4433        }
4434}
4435#endif
4436
4437static void
4438printkey(const struct ieee80211req_key *ik)
4439{
4440        static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
4441        u_int keylen = ik->ik_keylen;
4442        int printcontents;
4443
4444        printcontents = printkeys &&
4445                (memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
4446        if (printcontents)
4447                LINE_BREAK();
4448        switch (ik->ik_type) {
4449        case IEEE80211_CIPHER_WEP:
4450                /* compatibility */
4451                LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
4452                    keylen <= 5 ? "40-bit" :
4453                    keylen <= 13 ? "104-bit" : "128-bit");
4454                break;
4455        case IEEE80211_CIPHER_TKIP:
4456                if (keylen > 128/8)
4457                        keylen -= 128/8;        /* ignore MIC for now */
4458                LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4459                break;
4460        case IEEE80211_CIPHER_AES_OCB:
4461                LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4462                break;
4463        case IEEE80211_CIPHER_AES_CCM:
4464                LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4465                break;
4466        case IEEE80211_CIPHER_CKIP:
4467                LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4468                break;
4469        case IEEE80211_CIPHER_NONE:
4470                LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4471                break;
4472        default:
4473                LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
4474                        ik->ik_type, ik->ik_keyix+1, 8*keylen);
4475                break;
4476        }
4477        if (printcontents) {
4478                u_int i;
4479
4480                printf(" <");
4481                for (i = 0; i < keylen; i++)
4482                        printf("%02x", ik->ik_keydata[i]);
4483                printf(">");
4484                if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4485                    (ik->ik_keyrsc != 0 || verbose))
4486                        printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
4487                if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4488                    (ik->ik_keytsc != 0 || verbose))
4489                        printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
4490                if (ik->ik_flags != 0 && verbose) {
4491                        const char *sep = " ";
4492
4493                        if (ik->ik_flags & IEEE80211_KEY_XMIT)
4494                                printf("%stx", sep), sep = "+";
4495                        if (ik->ik_flags & IEEE80211_KEY_RECV)
4496                                printf("%srx", sep), sep = "+";
4497                        if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
4498                                printf("%sdef", sep), sep = "+";
4499                }
4500                LINE_BREAK();
4501        }
4502}
4503
4504static void
4505printrate(const char *tag, int v, int defrate, int defmcs)
4506{
4507        if ((v & IEEE80211_RATE_MCS) == 0) {
4508                if (v != defrate) {
4509                        if (v & 1)
4510                                LINE_CHECK("%s %d.5", tag, v/2);
4511                        else
4512                                LINE_CHECK("%s %d", tag, v/2);
4513                }
4514        } else {
4515                if (v != defmcs)
4516                        LINE_CHECK("%s %d", tag, v &~ 0x80);
4517        }
4518}
4519
4520static int
4521getid(int s, int ix, void *data, size_t len, int *plen, int mesh)
4522{
4523        struct ieee80211req ireq;
4524
4525        (void) memset(&ireq, 0, sizeof(ireq));
4526        (void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
4527        ireq.i_type = (!mesh) ? IEEE80211_IOC_SSID : IEEE80211_IOC_MESH_ID;
4528        ireq.i_val = ix;
4529        ireq.i_data = data;
4530        ireq.i_len = len;
4531        if (ioctl(s, SIOCG80211, &ireq) < 0)
4532                return -1;
4533        *plen = ireq.i_len;
4534        return 0;
4535}
4536
4537static void
4538ieee80211_status(int s)
4539{
4540        static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
4541        enum ieee80211_opmode opmode = get80211opmode(s);
4542        int i, num, wpa, wme, bgscan, bgscaninterval, val, len, wepmode;
4543        uint8_t data[32];
4544        const struct ieee80211_channel *c;
4545        const struct ieee80211_roamparam *rp;
4546        const struct ieee80211_txparam *tp;
4547
4548        if (getid(s, -1, data, sizeof(data), &len, 0) < 0) {
4549                /* If we can't get the SSID, this isn't an 802.11 device. */
4550                return;
4551        }
4552
4553        /*
4554         * Invalidate cached state so printing status for multiple
4555         * if's doesn't reuse the first interfaces' cached state.
4556         */
4557        gotcurchan = 0;
4558        gotroam = 0;
4559        gottxparams = 0;
4560        gothtconf = 0;
4561        gotregdomain = 0;
4562
4563        printf("\t");
4564        if (opmode == IEEE80211_M_MBSS) {
4565                printf("meshid ");
4566                getid(s, 0, data, sizeof(data), &len, 1);
4567                print_string(data, len);
4568        } else {
4569                if (get80211val(s, IEEE80211_IOC_NUMSSIDS, &num) < 0)
4570                        num = 0;
4571                printf("ssid ");
4572                if (num > 1) {
4573                        for (i = 0; i < num; i++) {
4574                                if (getid(s, i, data, sizeof(data), &len, 0) >= 0 && len > 0) {
4575                                        printf(" %d:", i + 1);
4576                                        print_string(data, len);
4577                                }
4578                        }
4579                } else
4580                        print_string(data, len);
4581        }
4582        c = getcurchan(s);
4583        if (c->ic_freq != IEEE80211_CHAN_ANY) {
4584                char buf[14];
4585                printf(" channel %d (%u MHz%s)", c->ic_ieee, c->ic_freq,
4586                        get_chaninfo(c, 1, buf, sizeof(buf)));
4587        } else if (verbose)
4588                printf(" channel UNDEF");
4589
4590        if (get80211(s, IEEE80211_IOC_BSSID, data, IEEE80211_ADDR_LEN) >= 0 &&
4591            (memcmp(data, zerobssid, sizeof(zerobssid)) != 0 || verbose))
4592                printf(" bssid %s", ether_ntoa((struct ether_addr *)data));
4593
4594        if (get80211len(s, IEEE80211_IOC_STATIONNAME, data, sizeof(data), &len) != -1) {
4595                printf("\n\tstationname ");
4596                print_string(data, len);
4597        }
4598
4599        spacer = ' ';           /* force first break */
4600        LINE_BREAK();
4601
4602        list_regdomain(s, 0);
4603
4604        wpa = 0;
4605        if (get80211val(s, IEEE80211_IOC_AUTHMODE, &val) != -1) {
4606                switch (val) {
4607                case IEEE80211_AUTH_NONE:
4608                        LINE_CHECK("authmode NONE");
4609                        break;
4610                case IEEE80211_AUTH_OPEN:
4611                        LINE_CHECK("authmode OPEN");
4612                        break;
4613                case IEEE80211_AUTH_SHARED:
4614                        LINE_CHECK("authmode SHARED");
4615                        break;
4616                case IEEE80211_AUTH_8021X:
4617                        LINE_CHECK("authmode 802.1x");
4618                        break;
4619                case IEEE80211_AUTH_WPA:
4620                        if (get80211val(s, IEEE80211_IOC_WPA, &wpa) < 0)
4621                                wpa = 1;        /* default to WPA1 */
4622                        switch (wpa) {
4623                        case 2:
4624                                LINE_CHECK("authmode WPA2/802.11i");
4625                                break;
4626                        case 3:
4627                                LINE_CHECK("authmode WPA1+WPA2/802.11i");
4628                                break;
4629                        default:
4630                                LINE_CHECK("authmode WPA");
4631                                break;
4632                        }
4633                        break;
4634                case IEEE80211_AUTH_AUTO:
4635                        LINE_CHECK("authmode AUTO");
4636                        break;
4637                default:
4638                        LINE_CHECK("authmode UNKNOWN (0x%x)", val);
4639                        break;
4640                }
4641        }
4642
4643        if (wpa || verbose) {
4644                if (get80211val(s, IEEE80211_IOC_WPS, &val) != -1) {
4645                        if (val)
4646                                LINE_CHECK("wps");
4647                        else if (verbose)
4648                                LINE_CHECK("-wps");
4649                }
4650                if (get80211val(s, IEEE80211_IOC_TSN, &val) != -1) {
4651                        if (val)
4652                                LINE_CHECK("tsn");
4653                        else if (verbose)
4654                                LINE_CHECK("-tsn");
4655                }
4656                if (ioctl(s, IEEE80211_IOC_COUNTERMEASURES, &val) != -1) {
4657                        if (val)
4658                                LINE_CHECK("countermeasures");
4659                        else if (verbose)
4660                                LINE_CHECK("-countermeasures");
4661                }
4662#if 0
4663                /* XXX not interesting with WPA done in user space */
4664                ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
4665                if (ioctl(s, SIOCG80211, &ireq) != -1) {
4666                }
4667
4668                ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
4669                if (ioctl(s, SIOCG80211, &ireq) != -1) {
4670                        LINE_CHECK("mcastcipher ");
4671                        printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
4672                        spacer = ' ';
4673                }
4674
4675                ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
4676                if (ioctl(s, SIOCG80211, &ireq) != -1) {
4677                        LINE_CHECK("ucastcipher ");
4678                        printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
4679                }
4680
4681                if (wpa & 2) {
4682                        ireq.i_type = IEEE80211_IOC_RSNCAPS;
4683                        if (ioctl(s, SIOCG80211, &ireq) != -1) {
4684                                LINE_CHECK("RSN caps 0x%x", ireq.i_val);
4685                                spacer = ' ';
4686                        }
4687                }
4688
4689                ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
4690                if (ioctl(s, SIOCG80211, &ireq) != -1) {
4691                }
4692#endif
4693        }
4694
4695        if (get80211val(s, IEEE80211_IOC_WEP, &wepmode) != -1 &&
4696            wepmode != IEEE80211_WEP_NOSUP) {
4697
4698                switch (wepmode) {
4699                case IEEE80211_WEP_OFF:
4700                        LINE_CHECK("privacy OFF");
4701                        break;
4702                case IEEE80211_WEP_ON:
4703                        LINE_CHECK("privacy ON");
4704                        break;
4705                case IEEE80211_WEP_MIXED:
4706                        LINE_CHECK("privacy MIXED");
4707                        break;
4708                default:
4709                        LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
4710                        break;
4711                }
4712
4713                /*
4714                 * If we get here then we've got WEP support so we need
4715                 * to print WEP status.
4716                 */
4717
4718                if (get80211val(s, IEEE80211_IOC_WEPTXKEY, &val) < 0) {
4719                        warn("WEP support, but no tx key!");
4720                        goto end;
4721                }
4722                if (val != -1)
4723                        LINE_CHECK("deftxkey %d", val+1);
4724                else if (wepmode != IEEE80211_WEP_OFF || verbose)
4725                        LINE_CHECK("deftxkey UNDEF");
4726
4727                if (get80211val(s, IEEE80211_IOC_NUMWEPKEYS, &num) < 0) {
4728                        warn("WEP support, but no NUMWEPKEYS support!");
4729                        goto end;
4730                }
4731
4732                for (i = 0; i < num; i++) {
4733                        struct ieee80211req_key ik;
4734
4735                        memset(&ik, 0, sizeof(ik));
4736                        ik.ik_keyix = i;
4737                        if (get80211(s, IEEE80211_IOC_WPAKEY, &ik, sizeof(ik)) < 0) {
4738                                warn("WEP support, but can get keys!");
4739                                goto end;
4740                        }
4741                        if (ik.ik_keylen != 0) {
4742                                if (verbose)
4743                                        LINE_BREAK();
4744                                printkey(&ik);
4745                        }
4746                }
4747end:
4748                ;
4749        }
4750
4751        if (get80211val(s, IEEE80211_IOC_POWERSAVE, &val) != -1 &&
4752            val != IEEE80211_POWERSAVE_NOSUP ) {
4753                if (val != IEEE80211_POWERSAVE_OFF || verbose) {
4754                        switch (val) {
4755                        case IEEE80211_POWERSAVE_OFF:
4756                                LINE_CHECK("powersavemode OFF");
4757                                break;
4758                        case IEEE80211_POWERSAVE_CAM:
4759                                LINE_CHECK("powersavemode CAM");
4760                                break;
4761                        case IEEE80211_POWERSAVE_PSP:
4762                                LINE_CHECK("powersavemode PSP");
4763                                break;
4764                        case IEEE80211_POWERSAVE_PSP_CAM:
4765                                LINE_CHECK("powersavemode PSP-CAM");
4766                                break;
4767                        }
4768                        if (get80211val(s, IEEE80211_IOC_POWERSAVESLEEP, &val) != -1)
4769                                LINE_CHECK("powersavesleep %d", val);
4770                }
4771        }
4772
4773        if (get80211val(s, IEEE80211_IOC_TXPOWER, &val) != -1) {
4774                if (val & 1)
4775                        LINE_CHECK("txpower %d.5", val/2);
4776                else
4777                        LINE_CHECK("txpower %d", val/2);
4778        }
4779        if (verbose) {
4780                if (get80211val(s, IEEE80211_IOC_TXPOWMAX, &val) != -1)
4781                        LINE_CHECK("txpowmax %.1f", val/2.);
4782        }
4783
4784        if (get80211val(s, IEEE80211_IOC_DOTD, &val) != -1) {
4785                if (val)
4786                        LINE_CHECK("dotd");
4787                else if (verbose)
4788                        LINE_CHECK("-dotd");
4789        }
4790
4791        if (get80211val(s, IEEE80211_IOC_RTSTHRESHOLD, &val) != -1) {
4792                if (val != IEEE80211_RTS_MAX || verbose)
4793                        LINE_CHECK("rtsthreshold %d", val);
4794        }
4795
4796        if (get80211val(s, IEEE80211_IOC_FRAGTHRESHOLD, &val) != -1) {
4797                if (val != IEEE80211_FRAG_MAX || verbose)
4798                        LINE_CHECK("fragthreshold %d", val);
4799        }
4800        if (opmode == IEEE80211_M_STA || verbose) {
4801                if (get80211val(s, IEEE80211_IOC_BMISSTHRESHOLD, &val) != -1) {
4802                        if (val != IEEE80211_HWBMISS_MAX || verbose)
4803                                LINE_CHECK("bmiss %d", val);
4804                }
4805        }
4806
4807        if (!verbose) {
4808                gettxparams(s);
4809                tp = &txparams.params[chan2mode(c)];
4810                printrate("ucastrate", tp->ucastrate,
4811                    IEEE80211_FIXED_RATE_NONE, IEEE80211_FIXED_RATE_NONE);
4812                printrate("mcastrate", tp->mcastrate, 2*1,
4813                    IEEE80211_RATE_MCS|0);
4814                printrate("mgmtrate", tp->mgmtrate, 2*1,
4815                    IEEE80211_RATE_MCS|0);
4816                if (tp->maxretry != 6)          /* XXX */
4817                        LINE_CHECK("maxretry %d", tp->maxretry);
4818        } else {
4819                LINE_BREAK();
4820                list_txparams(s);
4821        }
4822
4823        bgscaninterval = -1;
4824        (void) get80211val(s, IEEE80211_IOC_BGSCAN_INTERVAL, &bgscaninterval);
4825
4826        if (get80211val(s, IEEE80211_IOC_SCANVALID, &val) != -1) {
4827                if (val != bgscaninterval || verbose)
4828                        LINE_CHECK("scanvalid %u", val);
4829        }
4830
4831        bgscan = 0;
4832        if (get80211val(s, IEEE80211_IOC_BGSCAN, &bgscan) != -1) {
4833                if (bgscan)
4834                        LINE_CHECK("bgscan");
4835                else if (verbose)
4836                        LINE_CHECK("-bgscan");
4837        }
4838        if (bgscan || verbose) {
4839                if (bgscaninterval != -1)
4840                        LINE_CHECK("bgscanintvl %u", bgscaninterval);
4841                if (get80211val(s, IEEE80211_IOC_BGSCAN_IDLE, &val) != -1)
4842                        LINE_CHECK("bgscanidle %u", val);
4843                if (!verbose) {
4844                        getroam(s);
4845                        rp = &roamparams.params[chan2mode(c)];
4846                        if (rp->rssi & 1)
4847                                LINE_CHECK("roam:rssi %u.5", rp->rssi/2);
4848                        else
4849                                LINE_CHECK("roam:rssi %u", rp->rssi/2);
4850                        LINE_CHECK("roam:rate %u", rp->rate/2);
4851                } else {
4852                        LINE_BREAK();
4853                        list_roam(s);
4854                        LINE_BREAK();
4855                }
4856        }
4857
4858        if (IEEE80211_IS_CHAN_ANYG(c) || verbose) {
4859                if (get80211val(s, IEEE80211_IOC_PUREG, &val) != -1) {
4860                        if (val)
4861                                LINE_CHECK("pureg");
4862                        else if (verbose)
4863                                LINE_CHECK("-pureg");
4864                }
4865                if (get80211val(s, IEEE80211_IOC_PROTMODE, &val) != -1) {
4866                        switch (val) {
4867                        case IEEE80211_PROTMODE_OFF:
4868                                LINE_CHECK("protmode OFF");
4869                                break;
4870                        case IEEE80211_PROTMODE_CTS:
4871                                LINE_CHECK("protmode CTS");
4872                                break;
4873                        case IEEE80211_PROTMODE_RTSCTS:
4874                                LINE_CHECK("protmode RTSCTS");
4875                                break;
4876                        default:
4877                                LINE_CHECK("protmode UNKNOWN (0x%x)", val);
4878                                break;
4879                        }
4880                }
4881        }
4882
4883        if (IEEE80211_IS_CHAN_HT(c) || verbose) {
4884                gethtconf(s);
4885                switch (htconf & 3) {
4886                case 0:
4887                case 2:
4888                        LINE_CHECK("-ht");
4889                        break;
4890                case 1:
4891                        LINE_CHECK("ht20");
4892                        break;
4893                case 3:
4894                        if (verbose)
4895                                LINE_CHECK("ht");
4896                        break;
4897                }
4898                if (get80211val(s, IEEE80211_IOC_HTCOMPAT, &val) != -1) {
4899                        if (!val)
4900                                LINE_CHECK("-htcompat");
4901                        else if (verbose)
4902                                LINE_CHECK("htcompat");
4903                }
4904                if (get80211val(s, IEEE80211_IOC_AMPDU, &val) != -1) {
4905                        switch (val) {
4906                        case 0:
4907                                LINE_CHECK("-ampdu");
4908                                break;
4909                        case 1:
4910                                LINE_CHECK("ampdutx -ampdurx");
4911                                break;
4912                        case 2:
4913                                LINE_CHECK("-ampdutx ampdurx");
4914                                break;
4915                        case 3:
4916                                if (verbose)
4917                                        LINE_CHECK("ampdu");
4918                                break;
4919                        }
4920                }
4921                if (get80211val(s, IEEE80211_IOC_AMPDU_LIMIT, &val) != -1) {
4922                        switch (val) {
4923                        case IEEE80211_HTCAP_MAXRXAMPDU_8K:
4924                                LINE_CHECK("ampdulimit 8k");
4925                                break;
4926                        case IEEE80211_HTCAP_MAXRXAMPDU_16K:
4927                                LINE_CHECK("ampdulimit 16k");
4928                                break;
4929                        case IEEE80211_HTCAP_MAXRXAMPDU_32K:
4930                                LINE_CHECK("ampdulimit 32k");
4931                                break;
4932                        case IEEE80211_HTCAP_MAXRXAMPDU_64K:
4933                                LINE_CHECK("ampdulimit 64k");
4934                                break;
4935                        }
4936                }
4937                if (get80211val(s, IEEE80211_IOC_AMPDU_DENSITY, &val) != -1) {
4938                        switch (val) {
4939                        case IEEE80211_HTCAP_MPDUDENSITY_NA:
4940                                if (verbose)
4941                                        LINE_CHECK("ampdudensity NA");
4942                                break;
4943                        case IEEE80211_HTCAP_MPDUDENSITY_025:
4944                                LINE_CHECK("ampdudensity .25");
4945                                break;
4946                        case IEEE80211_HTCAP_MPDUDENSITY_05:
4947                                LINE_CHECK("ampdudensity .5");
4948                                break;
4949                        case IEEE80211_HTCAP_MPDUDENSITY_1:
4950                                LINE_CHECK("ampdudensity 1");
4951                                break;
4952                        case IEEE80211_HTCAP_MPDUDENSITY_2:
4953                                LINE_CHECK("ampdudensity 2");
4954                                break;
4955                        case IEEE80211_HTCAP_MPDUDENSITY_4:
4956                                LINE_CHECK("ampdudensity 4");
4957                                break;
4958                        case IEEE80211_HTCAP_MPDUDENSITY_8:
4959                                LINE_CHECK("ampdudensity 8");
4960                                break;
4961                        case IEEE80211_HTCAP_MPDUDENSITY_16:
4962                                LINE_CHECK("ampdudensity 16");
4963                                break;
4964                        }
4965                }
4966                if (get80211val(s, IEEE80211_IOC_AMSDU, &val) != -1) {
4967                        switch (val) {
4968                        case 0:
4969                                LINE_CHECK("-amsdu");
4970                                break;
4971                        case 1:
4972                                LINE_CHECK("amsdutx -amsdurx");
4973                                break;
4974                        case 2:
4975                                LINE_CHECK("-amsdutx amsdurx");
4976                                break;
4977                        case 3:
4978                                if (verbose)
4979                                        LINE_CHECK("amsdu");
4980                                break;
4981                        }
4982                }
4983                /* XXX amsdu limit */
4984                if (get80211val(s, IEEE80211_IOC_SHORTGI, &val) != -1) {
4985                        if (val)
4986                                LINE_CHECK("shortgi");
4987                        else if (verbose)
4988                                LINE_CHECK("-shortgi");
4989                }
4990                if (get80211val(s, IEEE80211_IOC_HTPROTMODE, &val) != -1) {
4991                        if (val == IEEE80211_PROTMODE_OFF)
4992                                LINE_CHECK("htprotmode OFF");
4993                        else if (val != IEEE80211_PROTMODE_RTSCTS)
4994                                LINE_CHECK("htprotmode UNKNOWN (0x%x)", val);
4995                        else if (verbose)
4996                                LINE_CHECK("htprotmode RTSCTS");
4997                }
4998                if (get80211val(s, IEEE80211_IOC_PUREN, &val) != -1) {
4999                        if (val)
5000                                LINE_CHECK("puren");
5001                        else if (verbose)
5002                                LINE_CHECK("-puren");
5003                }
5004                if (get80211val(s, IEEE80211_IOC_SMPS, &val) != -1) {
5005                        if (val == IEEE80211_HTCAP_SMPS_DYNAMIC)
5006                                LINE_CHECK("smpsdyn");
5007                        else if (val == IEEE80211_HTCAP_SMPS_ENA)
5008                                LINE_CHECK("smps");
5009                        else if (verbose)
5010                                LINE_CHECK("-smps");
5011                }
5012                if (get80211val(s, IEEE80211_IOC_RIFS, &val) != -1) {
5013                        if (val)
5014                                LINE_CHECK("rifs");
5015                        else if (verbose)
5016                                LINE_CHECK("-rifs");
5017                }
5018                if (get80211val(s, IEEE80211_IOC_STBC, &val) != -1) {
5019                        switch (val) {
5020                        case 0:
5021                                LINE_CHECK("-stbc");
5022                                break;
5023                        case 1:
5024                                LINE_CHECK("stbctx -stbcrx");
5025                                break;
5026                        case 2:
5027                                LINE_CHECK("-stbctx stbcrx");
5028                                break;
5029                        case 3:
5030                                if (verbose)
5031                                        LINE_CHECK("stbc");
5032                                break;
5033                        }
5034                }
5035        }
5036
5037        if (IEEE80211_IS_CHAN_VHT(c) || verbose) {
5038                getvhtconf(s);
5039                if (vhtconf & 0x1)
5040                        LINE_CHECK("vht");
5041                else
5042                        LINE_CHECK("-vht");
5043                if (vhtconf & 0x2)
5044                        LINE_CHECK("vht40");
5045                else
5046                        LINE_CHECK("-vht40");
5047                if (vhtconf & 0x4)
5048                        LINE_CHECK("vht80");
5049                else
5050                        LINE_CHECK("-vht80");
5051                if (vhtconf & 0x8)
5052                        LINE_CHECK("vht80p80");
5053                else
5054                        LINE_CHECK("-vht80p80");
5055                if (vhtconf & 0x10)
5056                        LINE_CHECK("vht160");
5057                else
5058                        LINE_CHECK("-vht160");
5059        }
5060
5061        if (get80211val(s, IEEE80211_IOC_WME, &wme) != -1) {
5062                if (wme)
5063                        LINE_CHECK("wme");
5064                else if (verbose)
5065                        LINE_CHECK("-wme");
5066        } else
5067                wme = 0;
5068
5069        if (get80211val(s, IEEE80211_IOC_BURST, &val) != -1) {
5070                if (val)
5071                        LINE_CHECK("burst");
5072                else if (verbose)
5073                        LINE_CHECK("-burst");
5074        }
5075
5076        if (get80211val(s, IEEE80211_IOC_FF, &val) != -1) {
5077                if (val)
5078                        LINE_CHECK("ff");
5079                else if (verbose)
5080                        LINE_CHECK("-ff");
5081        }
5082        if (get80211val(s, IEEE80211_IOC_TURBOP, &val) != -1) {
5083                if (val)
5084                        LINE_CHECK("dturbo");
5085                else if (verbose)
5086                        LINE_CHECK("-dturbo");
5087        }
5088        if (get80211val(s, IEEE80211_IOC_DWDS, &val) != -1) {
5089                if (val)
5090                        LINE_CHECK("dwds");
5091                else if (verbose)
5092                        LINE_CHECK("-dwds");
5093        }
5094
5095        if (opmode == IEEE80211_M_HOSTAP) {
5096                if (get80211val(s, IEEE80211_IOC_HIDESSID, &val) != -1) {
5097                        if (val)
5098                                LINE_CHECK("hidessid");
5099                        else if (verbose)
5100                                LINE_CHECK("-hidessid");
5101                }
5102                if (get80211val(s, IEEE80211_IOC_APBRIDGE, &val) != -1) {
5103                        if (!val)
5104                                LINE_CHECK("-apbridge");
5105                        else if (verbose)
5106                                LINE_CHECK("apbridge");
5107                }
5108                if (get80211val(s, IEEE80211_IOC_DTIM_PERIOD, &val) != -1)
5109                        LINE_CHECK("dtimperiod %u", val);
5110
5111                if (get80211val(s, IEEE80211_IOC_DOTH, &val) != -1) {
5112                        if (!val)
5113                                LINE_CHECK("-doth");
5114                        else if (verbose)
5115                                LINE_CHECK("doth");
5116                }
5117                if (get80211val(s, IEEE80211_IOC_DFS, &val) != -1) {
5118                        if (!val)
5119                                LINE_CHECK("-dfs");
5120                        else if (verbose)
5121                                LINE_CHECK("dfs");
5122                }
5123                if (get80211val(s, IEEE80211_IOC_INACTIVITY, &val) != -1) {
5124                        if (!val)
5125                                LINE_CHECK("-inact");
5126                        else if (verbose)
5127                                LINE_CHECK("inact");
5128                }
5129        } else {
5130                if (get80211val(s, IEEE80211_IOC_ROAMING, &val) != -1) {
5131                        if (val != IEEE80211_ROAMING_AUTO || verbose) {
5132                                switch (val) {
5133                                case IEEE80211_ROAMING_DEVICE:
5134                                        LINE_CHECK("roaming DEVICE");
5135                                        break;
5136                                case IEEE80211_ROAMING_AUTO:
5137                                        LINE_CHECK("roaming AUTO");
5138                                        break;
5139                                case IEEE80211_ROAMING_MANUAL:
5140                                        LINE_CHECK("roaming MANUAL");
5141                                        break;
5142                                default:
5143                                        LINE_CHECK("roaming UNKNOWN (0x%x)",
5144                                                val);
5145                                        break;
5146                                }
5147                        }
5148                }
5149        }
5150
5151        if (opmode == IEEE80211_M_AHDEMO) {
5152                if (get80211val(s, IEEE80211_IOC_TDMA_SLOT, &val) != -1)
5153                        LINE_CHECK("tdmaslot %u", val);
5154                if (get80211val(s, IEEE80211_IOC_TDMA_SLOTCNT, &val) != -1)
5155                        LINE_CHECK("tdmaslotcnt %u", val);
5156                if (get80211val(s, IEEE80211_IOC_TDMA_SLOTLEN, &val) != -1)
5157                        LINE_CHECK("tdmaslotlen %u", val);
5158                if (get80211val(s, IEEE80211_IOC_TDMA_BINTERVAL, &val) != -1)
5159                        LINE_CHECK("tdmabintval %u", val);
5160        } else if (get80211val(s, IEEE80211_IOC_BEACON_INTERVAL, &val) != -1) {
5161                /* XXX default define not visible */
5162                if (val != 100 || verbose)
5163                        LINE_CHECK("bintval %u", val);
5164        }
5165
5166        if (wme && verbose) {
5167                LINE_BREAK();
5168                list_wme(s);
5169        }
5170
5171        if (opmode == IEEE80211_M_MBSS) {
5172                if (get80211val(s, IEEE80211_IOC_MESH_TTL, &val) != -1) {
5173                        LINE_CHECK("meshttl %u", val);
5174                }
5175                if (get80211val(s, IEEE80211_IOC_MESH_AP, &val) != -1) {
5176                        if (val)
5177                                LINE_CHECK("meshpeering");
5178                        else
5179                                LINE_CHECK("-meshpeering");
5180                }
5181                if (get80211val(s, IEEE80211_IOC_MESH_FWRD, &val) != -1) {
5182                        if (val)
5183                                LINE_CHECK("meshforward");
5184                        else
5185                                LINE_CHECK("-meshforward");
5186                }
5187                if (get80211val(s, IEEE80211_IOC_MESH_GATE, &val) != -1) {
5188                        if (val)
5189                                LINE_CHECK("meshgate");
5190                        else
5191                                LINE_CHECK("-meshgate");
5192                }
5193                if (get80211len(s, IEEE80211_IOC_MESH_PR_METRIC, data, 12,
5194                    &len) != -1) {
5195                        data[len] = '\0';
5196                        LINE_CHECK("meshmetric %s", data);
5197                }
5198                if (get80211len(s, IEEE80211_IOC_MESH_PR_PATH, data, 12,
5199                    &len) != -1) {
5200                        data[len] = '\0';
5201                        LINE_CHECK("meshpath %s", data);
5202                }
5203                if (get80211val(s, IEEE80211_IOC_HWMP_ROOTMODE, &val) != -1) {
5204                        switch (val) {
5205                        case IEEE80211_HWMP_ROOTMODE_DISABLED:
5206                                LINE_CHECK("hwmprootmode DISABLED");
5207                                break;
5208                        case IEEE80211_HWMP_ROOTMODE_NORMAL:
5209                                LINE_CHECK("hwmprootmode NORMAL");
5210                                break;
5211                        case IEEE80211_HWMP_ROOTMODE_PROACTIVE:
5212                                LINE_CHECK("hwmprootmode PROACTIVE");
5213                                break;
5214                        case IEEE80211_HWMP_ROOTMODE_RANN:
5215                                LINE_CHECK("hwmprootmode RANN");
5216                                break;
5217                        default:
5218                                LINE_CHECK("hwmprootmode UNKNOWN(%d)", val);
5219                                break;
5220                        }
5221                }
5222                if (get80211val(s, IEEE80211_IOC_HWMP_MAXHOPS, &val) != -1) {
5223                        LINE_CHECK("hwmpmaxhops %u", val);
5224                }
5225        }
5226
5227        LINE_BREAK();
5228}
5229
5230static int
5231get80211(int s, int type, void *data, int len)
5232{
5233
5234        return (lib80211_get80211(s, name, type, data, len));
5235}
5236
5237static int
5238get80211len(int s, int type, void *data, int len, int *plen)
5239{
5240
5241        return (lib80211_get80211len(s, name, type, data, len, plen));
5242}
5243
5244static int
5245get80211val(int s, int type, int *val)
5246{
5247
5248        return (lib80211_get80211val(s, name, type, val));
5249}
5250
5251static void
5252set80211(int s, int type, int val, int len, void *data)
5253{
5254        int ret;
5255
5256        ret = lib80211_set80211(s, name, type, val, len, data);
5257        if (ret < 0)
5258                err(1, "SIOCS80211");
5259}
5260
5261static const char *
5262get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
5263{
5264        int len;
5265        int hexstr;
5266        u_int8_t *p;
5267
5268        len = *lenp;
5269        p = buf;
5270        hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
5271        if (hexstr)
5272                val += 2;
5273        for (;;) {
5274                if (*val == '\0')
5275                        break;
5276                if (sep != NULL && strchr(sep, *val) != NULL) {
5277                        val++;
5278                        break;
5279                }
5280                if (hexstr) {
5281                        if (!isxdigit((u_char)val[0])) {
5282                                warnx("bad hexadecimal digits");
5283                                return NULL;
5284                        }
5285                        if (!isxdigit((u_char)val[1])) {
5286                                warnx("odd count hexadecimal digits");
5287                                return NULL;
5288                        }
5289                }
5290                if (p >= buf + len) {
5291                        if (hexstr)
5292                                warnx("hexadecimal digits too long");
5293                        else
5294                                warnx("string too long");
5295                        return NULL;
5296                }
5297                if (hexstr) {
5298#define tohex(x)        (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
5299                        *p++ = (tohex((u_char)val[0]) << 4) |
5300                            tohex((u_char)val[1]);
5301#undef tohex
5302                        val += 2;
5303                } else
5304                        *p++ = *val++;
5305        }
5306        len = p - buf;
5307        /* The string "-" is treated as the empty string. */
5308        if (!hexstr && len == 1 && buf[0] == '-') {
5309                len = 0;
5310                memset(buf, 0, *lenp);
5311        } else if (len < *lenp)
5312                memset(p, 0, *lenp - len);
5313        *lenp = len;
5314        return val;
5315}
5316
5317static void
5318print_string(const u_int8_t *buf, int len)
5319{
5320        int i;
5321        int hasspc;
5322
5323        i = 0;
5324        hasspc = 0;
5325        for (; i < len; i++) {
5326                if (!isprint(buf[i]) && buf[i] != '\0')
5327                        break;
5328                if (isspace(buf[i]))
5329                        hasspc++;
5330        }
5331        if (i == len) {
5332                if (hasspc || len == 0 || buf[0] == '\0')
5333                        printf("\"%.*s\"", len, buf);
5334                else
5335                        printf("%.*s", len, buf);
5336        } else {
5337                printf("0x");
5338                for (i = 0; i < len; i++)
5339                        printf("%02x", buf[i]);
5340        }
5341}
5342
5343static void
5344setdefregdomain(int s)
5345{
5346        struct regdata *rdp = getregdata();
5347        const struct regdomain *rd;
5348
5349        /* Check if regdomain/country was already set by a previous call. */
5350        /* XXX is it possible? */
5351        if (regdomain.regdomain != 0 ||
5352            regdomain.country != CTRY_DEFAULT)
5353                return;
5354
5355        getregdomain(s);
5356
5357        /* Check if it was already set by the driver. */
5358        if (regdomain.regdomain != 0 ||
5359            regdomain.country != CTRY_DEFAULT)
5360                return;
5361
5362        /* Set FCC/US as default. */
5363        rd = lib80211_regdomain_findbysku(rdp, SKU_FCC);
5364        if (rd == NULL)
5365                errx(1, "FCC regdomain was not found");
5366
5367        regdomain.regdomain = rd->sku;
5368        if (rd->cc != NULL)
5369                defaultcountry(rd);
5370
5371        /* Send changes to net80211. */
5372        setregdomain_cb(s, &regdomain);
5373
5374        /* Cleanup (so it can be overriden by subsequent parameters). */
5375        regdomain.regdomain = 0;
5376        regdomain.country = CTRY_DEFAULT;
5377        regdomain.isocc[0] = 0;
5378        regdomain.isocc[1] = 0;
5379}
5380
5381/*
5382 * Virtual AP cloning support.
5383 */
5384static struct ieee80211_clone_params params = {
5385        .icp_opmode     = IEEE80211_M_STA,      /* default to station mode */
5386};
5387
5388static void
5389wlan_create(int s, struct ifreq *ifr)
5390{
5391        static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
5392        char orig_name[IFNAMSIZ];
5393
5394        if (params.icp_parent[0] == '\0')
5395                errx(1, "must specify a parent device (wlandev) when creating "
5396                    "a wlan device");
5397        if (params.icp_opmode == IEEE80211_M_WDS &&
5398            memcmp(params.icp_bssid, zerobssid, sizeof(zerobssid)) == 0)
5399                errx(1, "no bssid specified for WDS (use wlanbssid)");
5400        ifr->ifr_data = (caddr_t) &params;
5401        if (ioctl(s, SIOCIFCREATE2, ifr) < 0)
5402                err(1, "SIOCIFCREATE2");
5403
5404        /* XXX preserve original name for ifclonecreate(). */
5405        strlcpy(orig_name, name, sizeof(orig_name));
5406        strlcpy(name, ifr->ifr_name, sizeof(name));
5407
5408        setdefregdomain(s);
5409
5410        strlcpy(name, orig_name, sizeof(name));
5411}
5412
5413static
5414DECL_CMD_FUNC(set80211clone_wlandev, arg, d)
5415{
5416        strlcpy(params.icp_parent, arg, IFNAMSIZ);
5417}
5418
5419static
5420DECL_CMD_FUNC(set80211clone_wlanbssid, arg, d)
5421{
5422        const struct ether_addr *ea;
5423
5424        ea = ether_aton(arg);
5425        if (ea == NULL)
5426                errx(1, "%s: cannot parse bssid", arg);
5427        memcpy(params.icp_bssid, ea->octet, IEEE80211_ADDR_LEN);
5428}
5429
5430static
5431DECL_CMD_FUNC(set80211clone_wlanaddr, arg, d)
5432{
5433        const struct ether_addr *ea;
5434
5435        ea = ether_aton(arg);
5436        if (ea == NULL)
5437                errx(1, "%s: cannot parse address", arg);
5438        memcpy(params.icp_macaddr, ea->octet, IEEE80211_ADDR_LEN);
5439        params.icp_flags |= IEEE80211_CLONE_MACADDR;
5440}
5441
5442static
5443DECL_CMD_FUNC(set80211clone_wlanmode, arg, d)
5444{
5445#define iseq(a,b)       (strncasecmp(a,b,sizeof(b)-1) == 0)
5446        if (iseq(arg, "sta"))
5447                params.icp_opmode = IEEE80211_M_STA;
5448        else if (iseq(arg, "ahdemo") || iseq(arg, "adhoc-demo"))
5449                params.icp_opmode = IEEE80211_M_AHDEMO;
5450        else if (iseq(arg, "ibss") || iseq(arg, "adhoc"))
5451                params.icp_opmode = IEEE80211_M_IBSS;
5452        else if (iseq(arg, "ap") || iseq(arg, "host"))
5453                params.icp_opmode = IEEE80211_M_HOSTAP;
5454        else if (iseq(arg, "wds"))
5455                params.icp_opmode = IEEE80211_M_WDS;
5456        else if (iseq(arg, "monitor"))
5457                params.icp_opmode = IEEE80211_M_MONITOR;
5458        else if (iseq(arg, "tdma")) {
5459                params.icp_opmode = IEEE80211_M_AHDEMO;
5460                params.icp_flags |= IEEE80211_CLONE_TDMA;
5461        } else if (iseq(arg, "mesh") || iseq(arg, "mp")) /* mesh point */
5462                params.icp_opmode = IEEE80211_M_MBSS;
5463        else
5464                errx(1, "Don't know to create %s for %s", arg, name);
5465#undef iseq
5466}
5467
5468static void
5469set80211clone_beacons(const char *val, int d, int s, const struct afswtch *rafp)
5470{
5471        /* NB: inverted sense */
5472        if (d)
5473                params.icp_flags &= ~IEEE80211_CLONE_NOBEACONS;
5474        else
5475                params.icp_flags |= IEEE80211_CLONE_NOBEACONS;
5476}
5477
5478static void
5479set80211clone_bssid(const char *val, int d, int s, const struct afswtch *rafp)
5480{
5481        if (d)
5482                params.icp_flags |= IEEE80211_CLONE_BSSID;
5483        else
5484                params.icp_flags &= ~IEEE80211_CLONE_BSSID;
5485}
5486
5487static void
5488set80211clone_wdslegacy(const char *val, int d, int s, const struct afswtch *rafp)
5489{
5490        if (d)
5491                params.icp_flags |= IEEE80211_CLONE_WDSLEGACY;
5492        else
5493                params.icp_flags &= ~IEEE80211_CLONE_WDSLEGACY;
5494}
5495
5496static struct cmd ieee80211_cmds[] = {
5497        DEF_CMD_ARG("ssid",             set80211ssid),
5498        DEF_CMD_ARG("nwid",             set80211ssid),
5499        DEF_CMD_ARG("meshid",           set80211meshid),
5500        DEF_CMD_ARG("stationname",      set80211stationname),
5501        DEF_CMD_ARG("station",          set80211stationname),   /* BSD/OS */
5502        DEF_CMD_ARG("channel",          set80211channel),
5503        DEF_CMD_ARG("authmode",         set80211authmode),
5504        DEF_CMD_ARG("powersavemode",    set80211powersavemode),
5505        DEF_CMD("powersave",    1,      set80211powersave),
5506        DEF_CMD("-powersave",   0,      set80211powersave),
5507        DEF_CMD_ARG("powersavesleep",   set80211powersavesleep),
5508        DEF_CMD_ARG("wepmode",          set80211wepmode),
5509        DEF_CMD("wep",          1,      set80211wep),
5510        DEF_CMD("-wep",         0,      set80211wep),
5511        DEF_CMD_ARG("deftxkey",         set80211weptxkey),
5512        DEF_CMD_ARG("weptxkey",         set80211weptxkey),
5513        DEF_CMD_ARG("wepkey",           set80211wepkey),
5514        DEF_CMD_ARG("nwkey",            set80211nwkey),         /* NetBSD */
5515        DEF_CMD("-nwkey",       0,      set80211wep),           /* NetBSD */
5516        DEF_CMD_ARG("rtsthreshold",     set80211rtsthreshold),
5517        DEF_CMD_ARG("protmode",         set80211protmode),
5518        DEF_CMD_ARG("txpower",          set80211txpower),
5519        DEF_CMD_ARG("roaming",          set80211roaming),
5520        DEF_CMD("wme",          1,      set80211wme),
5521        DEF_CMD("-wme",         0,      set80211wme),
5522        DEF_CMD("wmm",          1,      set80211wme),
5523        DEF_CMD("-wmm",         0,      set80211wme),
5524        DEF_CMD("hidessid",     1,      set80211hidessid),
5525        DEF_CMD("-hidessid",    0,      set80211hidessid),
5526        DEF_CMD("apbridge",     1,      set80211apbridge),
5527        DEF_CMD("-apbridge",    0,      set80211apbridge),
5528        DEF_CMD_ARG("chanlist",         set80211chanlist),
5529        DEF_CMD_ARG("bssid",            set80211bssid),
5530        DEF_CMD_ARG("ap",               set80211bssid),
5531        DEF_CMD("scan", 0,              set80211scan),
5532        DEF_CMD_ARG("list",             set80211list),
5533        DEF_CMD_ARG2("cwmin",           set80211cwmin),
5534        DEF_CMD_ARG2("cwmax",           set80211cwmax),
5535        DEF_CMD_ARG2("aifs",            set80211aifs),
5536        DEF_CMD_ARG2("txoplimit",       set80211txoplimit),
5537        DEF_CMD_ARG("acm",              set80211acm),
5538        DEF_CMD_ARG("-acm",             set80211noacm),
5539        DEF_CMD_ARG("ack",              set80211ackpolicy),
5540        DEF_CMD_ARG("-ack",             set80211noackpolicy),
5541        DEF_CMD_ARG2("bss:cwmin",       set80211bsscwmin),
5542        DEF_CMD_ARG2("bss:cwmax",       set80211bsscwmax),
5543        DEF_CMD_ARG2("bss:aifs",        set80211bssaifs),
5544        DEF_CMD_ARG2("bss:txoplimit",   set80211bsstxoplimit),
5545        DEF_CMD_ARG("dtimperiod",       set80211dtimperiod),
5546        DEF_CMD_ARG("bintval",          set80211bintval),
5547        DEF_CMD("mac:open",     IEEE80211_MACCMD_POLICY_OPEN,   set80211maccmd),
5548        DEF_CMD("mac:allow",    IEEE80211_MACCMD_POLICY_ALLOW,  set80211maccmd),
5549        DEF_CMD("mac:deny",     IEEE80211_MACCMD_POLICY_DENY,   set80211maccmd),
5550        DEF_CMD("mac:radius",   IEEE80211_MACCMD_POLICY_RADIUS, set80211maccmd),
5551        DEF_CMD("mac:flush",    IEEE80211_MACCMD_FLUSH,         set80211maccmd),
5552        DEF_CMD("mac:detach",   IEEE80211_MACCMD_DETACH,        set80211maccmd),
5553        DEF_CMD_ARG("mac:add",          set80211addmac),
5554        DEF_CMD_ARG("mac:del",          set80211delmac),
5555        DEF_CMD_ARG("mac:kick",         set80211kickmac),
5556        DEF_CMD("pureg",        1,      set80211pureg),
5557        DEF_CMD("-pureg",       0,      set80211pureg),
5558        DEF_CMD("ff",           1,      set80211fastframes),
5559        DEF_CMD("-ff",          0,      set80211fastframes),
5560        DEF_CMD("dturbo",       1,      set80211dturbo),
5561        DEF_CMD("-dturbo",      0,      set80211dturbo),
5562        DEF_CMD("bgscan",       1,      set80211bgscan),
5563        DEF_CMD("-bgscan",      0,      set80211bgscan),
5564        DEF_CMD_ARG("bgscanidle",       set80211bgscanidle),
5565        DEF_CMD_ARG("bgscanintvl",      set80211bgscanintvl),
5566        DEF_CMD_ARG("scanvalid",        set80211scanvalid),
5567        DEF_CMD("quiet",        1,      set80211quiet),
5568        DEF_CMD("-quiet",       0,      set80211quiet),
5569        DEF_CMD_ARG("quiet_count",      set80211quietcount),
5570        DEF_CMD_ARG("quiet_period",     set80211quietperiod),
5571        DEF_CMD_ARG("quiet_dur",        set80211quietduration),
5572        DEF_CMD_ARG("quiet_offset",     set80211quietoffset),
5573        DEF_CMD_ARG("roam:rssi",        set80211roamrssi),
5574        DEF_CMD_ARG("roam:rate",        set80211roamrate),
5575        DEF_CMD_ARG("mcastrate",        set80211mcastrate),
5576        DEF_CMD_ARG("ucastrate",        set80211ucastrate),
5577        DEF_CMD_ARG("mgtrate",          set80211mgtrate),
5578        DEF_CMD_ARG("mgmtrate",         set80211mgtrate),
5579        DEF_CMD_ARG("maxretry",         set80211maxretry),
5580        DEF_CMD_ARG("fragthreshold",    set80211fragthreshold),
5581        DEF_CMD("burst",        1,      set80211burst),
5582        DEF_CMD("-burst",       0,      set80211burst),
5583        DEF_CMD_ARG("bmiss",            set80211bmissthreshold),
5584        DEF_CMD_ARG("bmissthreshold",   set80211bmissthreshold),
5585        DEF_CMD("shortgi",      1,      set80211shortgi),
5586        DEF_CMD("-shortgi",     0,      set80211shortgi),
5587        DEF_CMD("ampdurx",      2,      set80211ampdu),
5588        DEF_CMD("-ampdurx",     -2,     set80211ampdu),
5589        DEF_CMD("ampdutx",      1,      set80211ampdu),
5590        DEF_CMD("-ampdutx",     -1,     set80211ampdu),
5591        DEF_CMD("ampdu",        3,      set80211ampdu),         /* NB: tx+rx */
5592        DEF_CMD("-ampdu",       -3,     set80211ampdu),
5593        DEF_CMD_ARG("ampdulimit",       set80211ampdulimit),
5594        DEF_CMD_ARG("ampdudensity",     set80211ampdudensity),
5595        DEF_CMD("amsdurx",      2,      set80211amsdu),
5596        DEF_CMD("-amsdurx",     -2,     set80211amsdu),
5597        DEF_CMD("amsdutx",      1,      set80211amsdu),
5598        DEF_CMD("-amsdutx",     -1,     set80211amsdu),
5599        DEF_CMD("amsdu",        3,      set80211amsdu),         /* NB: tx+rx */
5600        DEF_CMD("-amsdu",       -3,     set80211amsdu),
5601        DEF_CMD_ARG("amsdulimit",       set80211amsdulimit),
5602        DEF_CMD("stbcrx",       2,      set80211stbc),
5603        DEF_CMD("-stbcrx",      -2,     set80211stbc),
5604        DEF_CMD("stbctx",       1,      set80211stbc),
5605        DEF_CMD("-stbctx",      -1,     set80211stbc),
5606        DEF_CMD("stbc",         3,      set80211stbc),          /* NB: tx+rx */
5607        DEF_CMD("-ampdu",       -3,     set80211stbc),
5608        DEF_CMD("puren",        1,      set80211puren),
5609        DEF_CMD("-puren",       0,      set80211puren),
5610        DEF_CMD("doth",         1,      set80211doth),
5611        DEF_CMD("-doth",        0,      set80211doth),
5612        DEF_CMD("dfs",          1,      set80211dfs),
5613        DEF_CMD("-dfs",         0,      set80211dfs),
5614        DEF_CMD("htcompat",     1,      set80211htcompat),
5615        DEF_CMD("-htcompat",    0,      set80211htcompat),
5616        DEF_CMD("dwds",         1,      set80211dwds),
5617        DEF_CMD("-dwds",        0,      set80211dwds),
5618        DEF_CMD("inact",        1,      set80211inact),
5619        DEF_CMD("-inact",       0,      set80211inact),
5620        DEF_CMD("tsn",          1,      set80211tsn),
5621        DEF_CMD("-tsn",         0,      set80211tsn),
5622        DEF_CMD_ARG("regdomain",        set80211regdomain),
5623        DEF_CMD_ARG("country",          set80211country),
5624        DEF_CMD("indoor",       'I',    set80211location),
5625        DEF_CMD("-indoor",      'O',    set80211location),
5626        DEF_CMD("outdoor",      'O',    set80211location),
5627        DEF_CMD("-outdoor",     'I',    set80211location),
5628        DEF_CMD("anywhere",     ' ',    set80211location),
5629        DEF_CMD("ecm",          1,      set80211ecm),
5630        DEF_CMD("-ecm",         0,      set80211ecm),
5631        DEF_CMD("dotd",         1,      set80211dotd),
5632        DEF_CMD("-dotd",        0,      set80211dotd),
5633        DEF_CMD_ARG("htprotmode",       set80211htprotmode),
5634        DEF_CMD("ht20",         1,      set80211htconf),
5635        DEF_CMD("-ht20",        0,      set80211htconf),
5636        DEF_CMD("ht40",         3,      set80211htconf),        /* NB: 20+40 */
5637        DEF_CMD("-ht40",        0,      set80211htconf),
5638        DEF_CMD("ht",           3,      set80211htconf),        /* NB: 20+40 */
5639        DEF_CMD("-ht",          0,      set80211htconf),
5640        DEF_CMD("vht",          1,      set80211vhtconf),
5641        DEF_CMD("-vht",         0,      set80211vhtconf),
5642        DEF_CMD("vht40",                2,      set80211vhtconf),
5643        DEF_CMD("-vht40",               -2,     set80211vhtconf),
5644        DEF_CMD("vht80",                4,      set80211vhtconf),
5645        DEF_CMD("-vht80",               -4,     set80211vhtconf),
5646        DEF_CMD("vht80p80",             8,      set80211vhtconf),
5647        DEF_CMD("-vht80p80",            -8,     set80211vhtconf),
5648        DEF_CMD("vht160",               16,     set80211vhtconf),
5649        DEF_CMD("-vht160",              -16,    set80211vhtconf),
5650        DEF_CMD("rifs",         1,      set80211rifs),
5651        DEF_CMD("-rifs",        0,      set80211rifs),
5652        DEF_CMD("smps",         IEEE80211_HTCAP_SMPS_ENA,       set80211smps),
5653        DEF_CMD("smpsdyn",      IEEE80211_HTCAP_SMPS_DYNAMIC,   set80211smps),
5654        DEF_CMD("-smps",        IEEE80211_HTCAP_SMPS_OFF,       set80211smps),
5655        /* XXX for testing */
5656        DEF_CMD_ARG("chanswitch",       set80211chanswitch),
5657
5658        DEF_CMD_ARG("tdmaslot",         set80211tdmaslot),
5659        DEF_CMD_ARG("tdmaslotcnt",      set80211tdmaslotcnt),
5660        DEF_CMD_ARG("tdmaslotlen",      set80211tdmaslotlen),
5661        DEF_CMD_ARG("tdmabintval",      set80211tdmabintval),
5662
5663        DEF_CMD_ARG("meshttl",          set80211meshttl),
5664        DEF_CMD("meshforward",  1,      set80211meshforward),
5665        DEF_CMD("-meshforward", 0,      set80211meshforward),
5666        DEF_CMD("meshgate",     1,      set80211meshgate),
5667        DEF_CMD("-meshgate",    0,      set80211meshgate),
5668        DEF_CMD("meshpeering",  1,      set80211meshpeering),
5669        DEF_CMD("-meshpeering", 0,      set80211meshpeering),
5670        DEF_CMD_ARG("meshmetric",       set80211meshmetric),
5671        DEF_CMD_ARG("meshpath",         set80211meshpath),
5672        DEF_CMD("meshrt:flush", IEEE80211_MESH_RTCMD_FLUSH,     set80211meshrtcmd),
5673        DEF_CMD_ARG("meshrt:add",       set80211addmeshrt),
5674        DEF_CMD_ARG("meshrt:del",       set80211delmeshrt),
5675        DEF_CMD_ARG("hwmprootmode",     set80211hwmprootmode),
5676        DEF_CMD_ARG("hwmpmaxhops",      set80211hwmpmaxhops),
5677
5678        /* vap cloning support */
5679        DEF_CLONE_CMD_ARG("wlanaddr",   set80211clone_wlanaddr),
5680        DEF_CLONE_CMD_ARG("wlanbssid",  set80211clone_wlanbssid),
5681        DEF_CLONE_CMD_ARG("wlandev",    set80211clone_wlandev),
5682        DEF_CLONE_CMD_ARG("wlanmode",   set80211clone_wlanmode),
5683        DEF_CLONE_CMD("beacons", 1,     set80211clone_beacons),
5684        DEF_CLONE_CMD("-beacons", 0,    set80211clone_beacons),
5685        DEF_CLONE_CMD("bssid",  1,      set80211clone_bssid),
5686        DEF_CLONE_CMD("-bssid", 0,      set80211clone_bssid),
5687        DEF_CLONE_CMD("wdslegacy", 1,   set80211clone_wdslegacy),
5688        DEF_CLONE_CMD("-wdslegacy", 0,  set80211clone_wdslegacy),
5689};
5690static struct afswtch af_ieee80211 = {
5691        .af_name        = "af_ieee80211",
5692        .af_af          = AF_UNSPEC,
5693        .af_other_status = ieee80211_status,
5694};
5695
5696static __constructor void
5697ieee80211_ctor(void)
5698{
5699        int i;
5700
5701        for (i = 0; i < nitems(ieee80211_cmds);  i++)
5702                cmd_register(&ieee80211_cmds[i]);
5703        af_register(&af_ieee80211);
5704        clone_setdefcallback("wlan", wlan_create);
5705}
Note: See TracBrowser for help on using the repository browser.