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