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