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