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