xref: /freebsd/sbin/ifconfig/ifieee80211.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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(if_ctx *ctx, int type, int val, int len, void *data);
177 static int get80211(if_ctx *ctx, int type, void *data, int len);
178 static int get80211len(if_ctx *ctx, int type, void *data, int len, int *plen);
179 static int get80211val(if_ctx *ctx, 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(if_ctx *, const struct ieee80211req_chaninfo *,
185     int allchans, int verbose);
186 static void regdomain_makechannels(if_ctx *, 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 *global_ifmr;
200 static int htconf = 0;
201 static	int gothtconf = 0;
202 
203 static void
204 gethtconf(if_ctx *ctx)
205 {
206 	if (gothtconf)
207 		return;
208 	if (get80211val(ctx, 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(if_ctx *ctx)
219 {
220 	if (gotvhtconf)
221 		return;
222 	if (get80211val(ctx, 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(if_ctx *ctx)
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(ctx, IEEE80211_IOC_CHANINFO, chaninfo,
240 	    IEEE80211_CHANINFO_SIZE(MAXCHAN)) < 0)
241 		err(1, "unable to get channel information");
242 	global_ifmr = ifmedia_getstate(ctx);
243 	gethtconf(ctx);
244 	getvhtconf(ctx);
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 = global_ifmr != NULL ? IFM_MODE(global_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(if_ctx *ctx)
391 {
392 	if (gotcurchan)
393 		return &curchan;
394 	if (get80211(ctx, IEEE80211_IOC_CURCHAN, &curchan, sizeof(curchan)) < 0) {
395 		int val;
396 		/* fall back to legacy ioctl */
397 		if (get80211val(ctx, IEEE80211_IOC_CHANNEL, &val) < 0)
398 			err(-1, "cannot figure out current channel");
399 		getchaninfo(ctx);
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(if_ctx *ctx)
440 {
441 	if (gotroam)
442 		return;
443 	if (get80211(ctx, 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(if_ctx *ctx, void *arg)
451 {
452 	struct ieee80211_roamparams_req *roam = arg;
453 	set80211(ctx, IEEE80211_IOC_ROAM, 0, sizeof(*roam), roam);
454 }
455 
456 static void
457 gettxparams(if_ctx *ctx)
458 {
459 	if (gottxparams)
460 		return;
461 	if (get80211(ctx, 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(if_ctx *ctx, void *arg)
469 {
470 	struct ieee80211_txparams_req *txp = arg;
471 	set80211(ctx, IEEE80211_IOC_TXPARAMS, 0, sizeof(*txp), txp);
472 }
473 
474 static void
475 getregdomain(if_ctx *ctx)
476 {
477 	if (gotregdomain)
478 		return;
479 	if (get80211(ctx, 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(if_ctx *ctx, struct ieee80211_devcaps_req *dc)
487 {
488 	if (get80211(ctx, 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(if_ctx *ctx, 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(ctx, 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(ctx, req, dc);
567 	if (ctx->args->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(ctx, &req->chaninfo, 1/*allchans*/, 1/*verbose*/);
580 	}
581 	if (req->chaninfo.ic_nchans == 0)
582 		errx(1, "no channels calculated");
583 	set80211(ctx, 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(if_ctx *ctx, const char *val, int dummy __unused)
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(ctx, IEEE80211_IOC_SSID, ssid, len, data);
624 }
625 
626 static void
627 set80211meshid(if_ctx *ctx, const char *val, int dummy __unused)
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(ctx, IEEE80211_IOC_MESH_ID, 0, len, data);
638 }
639 
640 static void
641 set80211stationname(if_ctx *ctx, const char *val, int dummy __unused)
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(ctx, 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(if_ctx *ctx, 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(ctx);
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(if_ctx *ctx, const char *val, int dummy __unused)
837 {
838 	struct ieee80211_channel chan;
839 
840 	getchannel(ctx, &chan, val);
841 	set80211(ctx, IEEE80211_IOC_CURCHAN, 0, sizeof(chan), &chan);
842 }
843 
844 static void
845 set80211chanswitch(if_ctx *ctx, const char *val, int dummy __unused)
846 {
847 	struct ieee80211_chanswitch_req csr;
848 
849 	getchannel(ctx, &csr.csa_chan, val);
850 	csr.csa_mode = 1;
851 	csr.csa_count = 5;
852 	set80211(ctx, IEEE80211_IOC_CHANSWITCH, 0, sizeof(csr), &csr);
853 }
854 
855 static void
856 set80211authmode(if_ctx *ctx, const char *val, int dummy __unused)
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(ctx, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
875 }
876 
877 static void
878 set80211powersavemode(if_ctx *ctx, const char *val, int dummy __unused)
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(ctx, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
897 }
898 
899 static void
900 set80211powersave(if_ctx *ctx, const char *val __unused, int d)
901 {
902 	if (d == 0)
903 		set80211(ctx, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
904 		    0, NULL);
905 	else
906 		set80211(ctx, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
907 		    0, NULL);
908 }
909 
910 static void
911 set80211powersavesleep(if_ctx *ctx, const char *val, int dummy __unused)
912 {
913 	set80211(ctx, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
914 }
915 
916 static void
917 set80211wepmode(if_ctx *ctx, const char *val, int dummy __unused)
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(ctx, IEEE80211_IOC_WEP, mode, 0, NULL);
932 }
933 
934 static void
935 set80211wep(if_ctx *ctx, const char *val __unused, int d)
936 {
937 	set80211(ctx, 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(if_ctx *ctx, const char *val, int dummy __unused)
948 {
949 	if (isundefarg(val))
950 		set80211(ctx, IEEE80211_IOC_WEPTXKEY, IEEE80211_KEYIX_NONE, 0, NULL);
951 	else
952 		set80211(ctx, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
953 }
954 
955 static void
956 set80211wepkey(if_ctx *ctx, const char *val, int dummy __unused)
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(ctx, 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(if_ctx *ctx, const char *val, int dummy __unused)
981 {
982 	int		txkey;
983 	int		i, len;
984 	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
985 
986 	set80211(ctx, 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(ctx, 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(ctx, IEEE80211_IOC_WEPKEY, 0, len, data);
1008 
1009 		bzero(data, sizeof(data));
1010 		for (i = 1; i < 4; i++)
1011 			set80211(ctx, IEEE80211_IOC_WEPKEY, i, 0, data);
1012 	}
1013 
1014 	set80211(ctx, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
1015 }
1016 
1017 static void
1018 set80211rtsthreshold(if_ctx *ctx, const char *val, int dummy __unused)
1019 {
1020 	set80211(ctx, IEEE80211_IOC_RTSTHRESHOLD,
1021 		isundefarg(val) ? IEEE80211_RTS_MAX : atoi(val), 0, NULL);
1022 }
1023 
1024 static void
1025 set80211protmode(if_ctx *ctx, const char *val, int dummy __unused)
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(ctx, IEEE80211_IOC_PROTMODE, mode, 0, NULL);
1040 }
1041 
1042 static void
1043 set80211htprotmode(if_ctx *ctx, const char *val, int dummy __unused)
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(ctx, IEEE80211_IOC_HTPROTMODE, mode, 0, NULL);
1056 }
1057 
1058 static void
1059 set80211txpower(if_ctx *ctx, const char *val, int dummy __unused)
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(ctx, 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(if_ctx *ctx, const char *val, int dummy __unused)
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(ctx, IEEE80211_IOC_ROAMING, mode, 0, NULL);
1089 }
1090 
1091 static void
1092 set80211wme(if_ctx *ctx, const char *val __unused, int d)
1093 {
1094 	set80211(ctx, IEEE80211_IOC_WME, d, 0, NULL);
1095 }
1096 
1097 static void
1098 set80211hidessid(if_ctx *ctx, const char *val __unused, int d)
1099 {
1100 	set80211(ctx, IEEE80211_IOC_HIDESSID, d, 0, NULL);
1101 }
1102 
1103 static void
1104 set80211apbridge(if_ctx *ctx, const char *val __unused, int d)
1105 {
1106 	set80211(ctx, IEEE80211_IOC_APBRIDGE, d, 0, NULL);
1107 }
1108 
1109 static void
1110 set80211fastframes(if_ctx *ctx, const char *val __unused, int d)
1111 {
1112 	set80211(ctx, IEEE80211_IOC_FF, d, 0, NULL);
1113 }
1114 
1115 static void
1116 set80211dturbo(if_ctx *ctx, const char *val __unused, int d)
1117 {
1118 	set80211(ctx, IEEE80211_IOC_TURBOP, d, 0, NULL);
1119 }
1120 
1121 static void
1122 set80211chanlist(if_ctx *ctx, const char *val, int dummy __unused)
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(ctx, IEEE80211_IOC_CHANLIST, 0, sizeof(chanlist), &chanlist);
1170 	free(temp);
1171 }
1172 
1173 static void
1174 set80211bssid(if_ctx *ctx, const char *val, int dummy __unused)
1175 {
1176 	if (!isanyarg(val)) {
1177 		char *temp;
1178 		struct sockaddr_dl sdl;
1179 
1180 		temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1181 		if (temp == NULL)
1182 			errx(1, "malloc failed");
1183 		temp[0] = ':';
1184 		strcpy(temp + 1, val);
1185 		sdl.sdl_len = sizeof(sdl);
1186 		link_addr(temp, &sdl);
1187 		free(temp);
1188 		if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1189 			errx(1, "malformed link-level address");
1190 		set80211(ctx, IEEE80211_IOC_BSSID, 0,
1191 			IEEE80211_ADDR_LEN, LLADDR(&sdl));
1192 	} else {
1193 		uint8_t zerobssid[IEEE80211_ADDR_LEN];
1194 		memset(zerobssid, 0, sizeof(zerobssid));
1195 		set80211(ctx, IEEE80211_IOC_BSSID, 0,
1196 			IEEE80211_ADDR_LEN, zerobssid);
1197 	}
1198 }
1199 
1200 static int
1201 getac(const char *ac)
1202 {
1203 	if (strcasecmp(ac, "ac_be") == 0 || strcasecmp(ac, "be") == 0)
1204 		return WME_AC_BE;
1205 	if (strcasecmp(ac, "ac_bk") == 0 || strcasecmp(ac, "bk") == 0)
1206 		return WME_AC_BK;
1207 	if (strcasecmp(ac, "ac_vi") == 0 || strcasecmp(ac, "vi") == 0)
1208 		return WME_AC_VI;
1209 	if (strcasecmp(ac, "ac_vo") == 0 || strcasecmp(ac, "vo") == 0)
1210 		return WME_AC_VO;
1211 	errx(1, "unknown wme access class %s", ac);
1212 }
1213 
1214 static void
1215 set80211cwmin(if_ctx *ctx, const char *ac, const char *val)
1216 {
1217 	set80211(ctx, IEEE80211_IOC_WME_CWMIN, atoi(val), getac(ac), NULL);
1218 }
1219 
1220 static void
1221 set80211cwmax(if_ctx *ctx, const char *ac, const char *val)
1222 {
1223 	set80211(ctx, IEEE80211_IOC_WME_CWMAX, atoi(val), getac(ac), NULL);
1224 }
1225 
1226 static void
1227 set80211aifs(if_ctx *ctx, const char *ac, const char *val)
1228 {
1229 	set80211(ctx, IEEE80211_IOC_WME_AIFS, atoi(val), getac(ac), NULL);
1230 }
1231 
1232 static void
1233 set80211txoplimit(if_ctx *ctx, const char *ac, const char *val)
1234 {
1235 	set80211(ctx, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), getac(ac), NULL);
1236 }
1237 
1238 static void
1239 set80211acm(if_ctx *ctx, const char *ac, int dummy __unused)
1240 {
1241 	set80211(ctx, IEEE80211_IOC_WME_ACM, 1, getac(ac), NULL);
1242 }
1243 
1244 static void
1245 set80211noacm(if_ctx *ctx, const char *ac, int dummy __unused)
1246 {
1247 	set80211(ctx, IEEE80211_IOC_WME_ACM, 0, getac(ac), NULL);
1248 }
1249 
1250 static void
1251 set80211ackpolicy(if_ctx *ctx, const char *ac, int dummy __unused)
1252 {
1253 	set80211(ctx, IEEE80211_IOC_WME_ACKPOLICY, 1, getac(ac), NULL);
1254 }
1255 static void
1256 set80211noackpolicy(if_ctx *ctx, const char *ac, int dummy __unused)
1257 {
1258 	set80211(ctx, IEEE80211_IOC_WME_ACKPOLICY, 0, getac(ac), NULL);
1259 }
1260 
1261 static void
1262 set80211bsscwmin(if_ctx *ctx, const char *ac, const char *val)
1263 {
1264 	set80211(ctx, IEEE80211_IOC_WME_CWMIN, atoi(val),
1265 		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1266 }
1267 
1268 static void
1269 set80211bsscwmax(if_ctx *ctx, const char *ac, const char *val)
1270 {
1271 	set80211(ctx, IEEE80211_IOC_WME_CWMAX, atoi(val),
1272 		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1273 }
1274 
1275 static void
1276 set80211bssaifs(if_ctx *ctx, const char *ac, const char *val)
1277 {
1278 	set80211(ctx, IEEE80211_IOC_WME_AIFS, atoi(val),
1279 		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1280 }
1281 
1282 static void
1283 set80211bsstxoplimit(if_ctx *ctx, const char *ac, const char *val)
1284 {
1285 	set80211(ctx, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val),
1286 		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1287 }
1288 
1289 static void
1290 set80211dtimperiod(if_ctx *ctx, const char *val, int dummy __unused)
1291 {
1292 	set80211(ctx, IEEE80211_IOC_DTIM_PERIOD, atoi(val), 0, NULL);
1293 }
1294 
1295 static void
1296 set80211bintval(if_ctx *ctx, const char *val, int dummy __unused)
1297 {
1298 	set80211(ctx, IEEE80211_IOC_BEACON_INTERVAL, atoi(val), 0, NULL);
1299 }
1300 
1301 static void
1302 set80211macmac(if_ctx *ctx, 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(ctx, op, 0, IEEE80211_ADDR_LEN, LLADDR(&sdl));
1318 }
1319 
1320 static void
1321 set80211addmac(if_ctx *ctx, const char *val, int dummy __unused)
1322 {
1323 	set80211macmac(ctx, IEEE80211_IOC_ADDMAC, val);
1324 }
1325 
1326 static void
1327 set80211delmac(if_ctx *ctx, const char *val, int dummy __unused)
1328 {
1329 	set80211macmac(ctx, IEEE80211_IOC_DELMAC, val);
1330 }
1331 
1332 static void
1333 set80211kickmac(if_ctx *ctx, const char *val, int dummy __unused)
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(ctx, IEEE80211_IOC_MLME, 0, sizeof(mlme), &mlme);
1354 }
1355 
1356 static void
1357 set80211maccmd(if_ctx *ctx, const char *val __unused, int d)
1358 {
1359 	set80211(ctx, IEEE80211_IOC_MACCMD, d, 0, NULL);
1360 }
1361 
1362 static void
1363 set80211meshrtmac(if_ctx *ctx, 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(ctx, IEEE80211_IOC_MESH_RTCMD, req,
1379 	    IEEE80211_ADDR_LEN, LLADDR(&sdl));
1380 }
1381 
1382 static void
1383 set80211addmeshrt(if_ctx *ctx, const char *val, int dummy __unused)
1384 {
1385 	set80211meshrtmac(ctx, IEEE80211_MESH_RTCMD_ADD, val);
1386 }
1387 
1388 static void
1389 set80211delmeshrt(if_ctx *ctx, const char *val, int dummy __unused)
1390 {
1391 	set80211meshrtmac(ctx, IEEE80211_MESH_RTCMD_DELETE, val);
1392 }
1393 
1394 static void
1395 set80211meshrtcmd(if_ctx *ctx, const char *val __unused, int d)
1396 {
1397 	set80211(ctx, IEEE80211_IOC_MESH_RTCMD, d, 0, NULL);
1398 }
1399 
1400 static void
1401 set80211hwmprootmode(if_ctx *ctx, const char *val, int dummy __unused)
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(ctx, IEEE80211_IOC_HWMP_ROOTMODE, mode, 0, NULL);
1414 }
1415 
1416 static void
1417 set80211hwmpmaxhops(if_ctx *ctx, const char *val, int dummy __unused)
1418 {
1419 	set80211(ctx, IEEE80211_IOC_HWMP_MAXHOPS, atoi(val), 0, NULL);
1420 }
1421 
1422 static void
1423 set80211pureg(if_ctx *ctx, const char *val __unused, int d)
1424 {
1425 	set80211(ctx, IEEE80211_IOC_PUREG, d, 0, NULL);
1426 }
1427 
1428 static void
1429 set80211quiet(if_ctx *ctx, const char *val __unused, int d)
1430 {
1431 	set80211(ctx, IEEE80211_IOC_QUIET, d, 0, NULL);
1432 }
1433 
1434 static void
1435 set80211quietperiod(if_ctx *ctx, const char *val, int dummy __unused)
1436 {
1437 	set80211(ctx, IEEE80211_IOC_QUIET_PERIOD, atoi(val), 0, NULL);
1438 }
1439 
1440 static void
1441 set80211quietcount(if_ctx *ctx, const char *val, int dummy __unused)
1442 {
1443 	set80211(ctx, IEEE80211_IOC_QUIET_COUNT, atoi(val), 0, NULL);
1444 }
1445 
1446 static void
1447 set80211quietduration(if_ctx *ctx, const char *val, int dummy __unused)
1448 {
1449 	set80211(ctx, IEEE80211_IOC_QUIET_DUR, atoi(val), 0, NULL);
1450 }
1451 
1452 static void
1453 set80211quietoffset(if_ctx *ctx, const char *val, int dummy __unused)
1454 {
1455 	set80211(ctx, IEEE80211_IOC_QUIET_OFFSET, atoi(val), 0, NULL);
1456 }
1457 
1458 static void
1459 set80211bgscan(if_ctx *ctx, const char *val __unused, int d)
1460 {
1461 	set80211(ctx, IEEE80211_IOC_BGSCAN, d, 0, NULL);
1462 }
1463 
1464 static void
1465 set80211bgscanidle(if_ctx *ctx, const char *val, int dummy __unused)
1466 {
1467 	set80211(ctx, IEEE80211_IOC_BGSCAN_IDLE, atoi(val), 0, NULL);
1468 }
1469 
1470 static void
1471 set80211bgscanintvl(if_ctx *ctx, const char *val, int dummy __unused)
1472 {
1473 	set80211(ctx, IEEE80211_IOC_BGSCAN_INTERVAL, atoi(val), 0, NULL);
1474 }
1475 
1476 static void
1477 set80211scanvalid(if_ctx *ctx, const char *val, int dummy __unused)
1478 {
1479 	set80211(ctx, 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 void
1619 set80211roamrssi(if_ctx *ctx, const char *val, int dummy __unused)
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(ctx);
1629 	if (flags == 0) {		/* NB: no flags => current channel */
1630 		flags = getcurchan(ctx)->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 void
1650 set80211roamrate(if_ctx *ctx, const char *val, int dummy __unused)
1651 {
1652 	int rate, flags;
1653 
1654 	rate = getrate(val, "roam");
1655 	flags = getmodeflags(val);
1656 	getroam(ctx);
1657 	if (flags == 0) {		/* NB: no flags => current channel */
1658 		flags = getcurchan(ctx)->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 void
1666 set80211mcastrate(if_ctx *ctx, const char *val, int dummy __unused)
1667 {
1668 	int rate, flags;
1669 
1670 	rate = getrate(val, "mcast");
1671 	flags = getmodeflags(val);
1672 	gettxparams(ctx);
1673 	if (flags == 0) {		/* NB: no flags => current channel */
1674 		flags = getcurchan(ctx)->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 void
1682 set80211mgtrate(if_ctx *ctx, const char *val, int dummy __unused)
1683 {
1684 	int rate, flags;
1685 
1686 	rate = getrate(val, "mgmt");
1687 	flags = getmodeflags(val);
1688 	gettxparams(ctx);
1689 	if (flags == 0) {		/* NB: no flags => current channel */
1690 		flags = getcurchan(ctx)->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 void
1698 set80211ucastrate(if_ctx *ctx, const char *val, int dummy __unused)
1699 {
1700 	int flags;
1701 
1702 	gettxparams(ctx);
1703 	flags = getmodeflags(val);
1704 	if (isanyarg(val)) {
1705 		if (flags == 0) {	/* NB: no flags => current channel */
1706 			flags = getcurchan(ctx)->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(ctx)->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 void
1724 set80211maxretry(if_ctx *ctx, const char *val, int dummy __unused)
1725 {
1726 	int v = atoi(val), flags;
1727 
1728 	flags = getmodeflags(val);
1729 	gettxparams(ctx);
1730 	if (flags == 0) {		/* NB: no flags => current channel */
1731 		flags = getcurchan(ctx)->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 void
1741 set80211fragthreshold(if_ctx *ctx, const char *val, int dummy __unused)
1742 {
1743 	set80211(ctx, IEEE80211_IOC_FRAGTHRESHOLD,
1744 		isundefarg(val) ? IEEE80211_FRAG_MAX : atoi(val), 0, NULL);
1745 }
1746 
1747 static void
1748 set80211bmissthreshold(if_ctx *ctx, const char *val, int dummy __unused)
1749 {
1750 	set80211(ctx, IEEE80211_IOC_BMISSTHRESHOLD,
1751 		isundefarg(val) ? IEEE80211_HWBMISS_MAX : atoi(val), 0, NULL);
1752 }
1753 
1754 static void
1755 set80211burst(if_ctx *ctx, const char *val __unused, int d)
1756 {
1757 	set80211(ctx, IEEE80211_IOC_BURST, d, 0, NULL);
1758 }
1759 
1760 static void
1761 set80211doth(if_ctx *ctx, const char *val __unused, int d)
1762 {
1763 	set80211(ctx, IEEE80211_IOC_DOTH, d, 0, NULL);
1764 }
1765 
1766 static void
1767 set80211dfs(if_ctx *ctx, const char *val __unused, int d)
1768 {
1769 	set80211(ctx, IEEE80211_IOC_DFS, d, 0, NULL);
1770 }
1771 
1772 static void
1773 set80211shortgi(if_ctx *ctx, const char *val __unused, int d)
1774 {
1775 	set80211(ctx, 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(if_ctx *ctx, const char *val __unused, int d)
1783 {
1784 	int ampdu;
1785 
1786 	if (get80211val(ctx, 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(ctx, IEEE80211_IOC_AMPDU, ampdu, 0, NULL);
1794 }
1795 
1796 static void
1797 set80211stbc(if_ctx *ctx, const char *val __unused, int d)
1798 {
1799 	int stbc;
1800 
1801 	if (get80211val(ctx, 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(ctx, IEEE80211_IOC_STBC, stbc, 0, NULL);
1809 }
1810 
1811 static void
1812 set80211ldpc(if_ctx *ctx, const char *val __unused, int d)
1813 {
1814         int ldpc;
1815 
1816         if (get80211val(ctx, 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(ctx, IEEE80211_IOC_LDPC, ldpc, 0, NULL);
1824 }
1825 
1826 static void
1827 set80211uapsd(if_ctx *ctx, const char *val __unused, int d)
1828 {
1829 	set80211(ctx, IEEE80211_IOC_UAPSD, d, 0, NULL);
1830 }
1831 
1832 static void
1833 set80211ampdulimit(if_ctx *ctx, const char *val, int dummy __unused)
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(ctx, IEEE80211_IOC_AMPDU_LIMIT, v, 0, NULL);
1858 }
1859 
1860 /* XXX 11ac density/size is different */
1861 static void
1862 set80211ampdudensity(if_ctx *ctx, const char *val, int dummy __unused)
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(ctx, IEEE80211_IOC_AMPDU_DENSITY, v, 0, NULL);
1897 }
1898 
1899 static void
1900 set80211amsdu(if_ctx *ctx, const char *val __unused, int d)
1901 {
1902 	int amsdu;
1903 
1904 	if (get80211val(ctx, 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(ctx, IEEE80211_IOC_AMSDU, amsdu, 0, NULL);
1912 }
1913 
1914 static void
1915 set80211amsdulimit(if_ctx *ctx, const char *val, int dummy __unused)
1916 {
1917 	set80211(ctx, IEEE80211_IOC_AMSDU_LIMIT, atoi(val), 0, NULL);
1918 }
1919 
1920 static void
1921 set80211puren(if_ctx *ctx, const char *val __unused, int d)
1922 {
1923 	set80211(ctx, IEEE80211_IOC_PUREN, d, 0, NULL);
1924 }
1925 
1926 static void
1927 set80211htcompat(if_ctx *ctx, const char *val __unused, int d)
1928 {
1929 	set80211(ctx, IEEE80211_IOC_HTCOMPAT, d, 0, NULL);
1930 }
1931 
1932 static void
1933 set80211htconf(if_ctx *ctx, const char *val __unused, int d)
1934 {
1935 	set80211(ctx, IEEE80211_IOC_HTCONF, d, 0, NULL);
1936 	htconf = d;
1937 }
1938 
1939 static void
1940 set80211dwds(if_ctx *ctx, const char *val __unused, int d)
1941 {
1942 	set80211(ctx, IEEE80211_IOC_DWDS, d, 0, NULL);
1943 }
1944 
1945 static void
1946 set80211inact(if_ctx *ctx, const char *val __unused, int d)
1947 {
1948 	set80211(ctx, IEEE80211_IOC_INACTIVITY, d, 0, NULL);
1949 }
1950 
1951 static void
1952 set80211tsn(if_ctx *ctx, const char *val __unused, int d)
1953 {
1954 	set80211(ctx, IEEE80211_IOC_TSN, d, 0, NULL);
1955 }
1956 
1957 static void
1958 set80211dotd(if_ctx *ctx, const char *val __unused, int d)
1959 {
1960 	set80211(ctx, IEEE80211_IOC_DOTD, d, 0, NULL);
1961 }
1962 
1963 static void
1964 set80211smps(if_ctx *ctx, const char *val __unused, int d)
1965 {
1966 	set80211(ctx, IEEE80211_IOC_SMPS, d, 0, NULL);
1967 }
1968 
1969 static void
1970 set80211rifs(if_ctx *ctx, const char *val __unused, int d)
1971 {
1972 	set80211(ctx, IEEE80211_IOC_RIFS, d, 0, NULL);
1973 }
1974 
1975 static void
1976 set80211vhtconf(if_ctx *ctx, const char *val __unused, int d)
1977 {
1978 	if (get80211val(ctx, 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(ctx, IEEE80211_IOC_VHTCONF, vhtconf, 0, NULL);
1988 }
1989 
1990 static void
1991 set80211tdmaslot(if_ctx *ctx, const char *val, int dummy __unused)
1992 {
1993 	set80211(ctx, IEEE80211_IOC_TDMA_SLOT, atoi(val), 0, NULL);
1994 }
1995 
1996 static void
1997 set80211tdmaslotcnt(if_ctx *ctx, const char *val, int dummy __unused)
1998 {
1999 	set80211(ctx, IEEE80211_IOC_TDMA_SLOTCNT, atoi(val), 0, NULL);
2000 }
2001 
2002 static void
2003 set80211tdmaslotlen(if_ctx *ctx, const char *val, int dummy __unused)
2004 {
2005 	set80211(ctx, IEEE80211_IOC_TDMA_SLOTLEN, atoi(val), 0, NULL);
2006 }
2007 
2008 static void
2009 set80211tdmabintval(if_ctx *ctx, const char *val, int dummy __unused)
2010 {
2011 	set80211(ctx, IEEE80211_IOC_TDMA_BINTERVAL, atoi(val), 0, NULL);
2012 }
2013 
2014 static void
2015 set80211meshttl(if_ctx *ctx, const char *val, int dummy __unused)
2016 {
2017 	set80211(ctx, IEEE80211_IOC_MESH_TTL, atoi(val), 0, NULL);
2018 }
2019 
2020 static void
2021 set80211meshforward(if_ctx *ctx, const char *val __unused, int d)
2022 {
2023 	set80211(ctx, IEEE80211_IOC_MESH_FWRD, d, 0, NULL);
2024 }
2025 
2026 static void
2027 set80211meshgate(if_ctx *ctx, const char *val __unused, int d)
2028 {
2029 	set80211(ctx, IEEE80211_IOC_MESH_GATE, d, 0, NULL);
2030 }
2031 
2032 static void
2033 set80211meshpeering(if_ctx *ctx, const char *val __unused, int d)
2034 {
2035 	set80211(ctx, IEEE80211_IOC_MESH_AP, d, 0, NULL);
2036 }
2037 
2038 static void
2039 set80211meshmetric(if_ctx *ctx, const char *val, int dummy __unused)
2040 {
2041 	char v[12];
2042 
2043 	memcpy(v, val, sizeof(v));
2044 	set80211(ctx, IEEE80211_IOC_MESH_PR_METRIC, 0, 0, v);
2045 }
2046 
2047 static void
2048 set80211meshpath(if_ctx *ctx, const char *val, int dummy __unused)
2049 {
2050 	char v[12];
2051 
2052 	memcpy(v, val, sizeof(v));
2053 	set80211(ctx, 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 	    (int)(ca->ic_flags & CHAN_ALL) - (int)(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, uint32_t 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, uint32_t 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, uint32_t 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(if_ctx *ctx, 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 	const int verbose = ctx->args->verbose;
2157 
2158 	hi_adj = (chanFlags & IEEE80211_CHAN_HT40U) ? -20 : 0;
2159 	lo_adj = (chanFlags & IEEE80211_CHAN_HT40D) ? 20 : 0;
2160 	channelSep = (chanFlags & IEEE80211_CHAN_2GHZ) ? 0 : 40;
2161 
2162 	LIST_FOREACH(nb, bands, next) {
2163 		b = nb->band;
2164 		if (verbose) {
2165 			printf("%s:", __func__);
2166 			printb(" chanFlags", chanFlags, IEEE80211_CHAN_BITS);
2167 			printb(" bandFlags", nb->flags | b->flags,
2168 			    IEEE80211_CHAN_BITS);
2169 			putchar('\n');
2170 		}
2171 		prev = NULL;
2172 
2173 		for (freq = b->freqStart + lo_adj;
2174 		     freq <= b->freqEnd + hi_adj; freq += b->chanSep) {
2175 			/*
2176 			 * Construct flags for the new channel.  We take
2177 			 * the attributes from the band descriptions except
2178 			 * for HT40 which is enabled generically (i.e. +/-
2179 			 * extension channel) in the band description and
2180 			 * then constrained according by channel separation.
2181 			 */
2182 			flags = nb->flags | b->flags;
2183 
2184 			/*
2185 			 * VHT first - HT is a subset.
2186 			 */
2187 			if (flags & IEEE80211_CHAN_VHT) {
2188 				if ((chanFlags & IEEE80211_CHAN_VHT20) &&
2189 				    (flags & IEEE80211_CHAN_VHT20) == 0) {
2190 					if (verbose)
2191 						printf("%u: skip, not a "
2192 						    "VHT20 channel\n", freq);
2193 					continue;
2194 				}
2195 				if ((chanFlags & IEEE80211_CHAN_VHT40) &&
2196 				    (flags & IEEE80211_CHAN_VHT40) == 0) {
2197 					if (verbose)
2198 						printf("%u: skip, not a "
2199 						    "VHT40 channel\n", freq);
2200 					continue;
2201 				}
2202 				if ((chanFlags & IEEE80211_CHAN_VHT80) &&
2203 				    (flags & IEEE80211_CHAN_VHT80) == 0) {
2204 					if (verbose)
2205 						printf("%u: skip, not a "
2206 						    "VHT80 channel\n", freq);
2207 					continue;
2208 				}
2209 				if ((chanFlags & IEEE80211_CHAN_VHT160) &&
2210 				    (flags & IEEE80211_CHAN_VHT160) == 0) {
2211 					if (verbose)
2212 						printf("%u: skip, not a "
2213 						    "VHT160 channel\n", freq);
2214 					continue;
2215 				}
2216 				if ((chanFlags & IEEE80211_CHAN_VHT80P80) &&
2217 				    (flags & IEEE80211_CHAN_VHT80P80) == 0) {
2218 					if (verbose)
2219 						printf("%u: skip, not a "
2220 						    "VHT80+80 channel\n", freq);
2221 					continue;
2222 				}
2223 				flags &= ~IEEE80211_CHAN_VHT;
2224 				flags |= chanFlags & IEEE80211_CHAN_VHT;
2225 			}
2226 
2227 			/* Now, constrain HT */
2228 			if (flags & IEEE80211_CHAN_HT) {
2229 				/*
2230 				 * HT channels are generated specially; we're
2231 				 * called to add HT20, HT40+, and HT40- chan's
2232 				 * so we need to expand only band specs for
2233 				 * the HT channel type being added.
2234 				 */
2235 				if ((chanFlags & IEEE80211_CHAN_HT20) &&
2236 				    (flags & IEEE80211_CHAN_HT20) == 0) {
2237 					if (verbose)
2238 						printf("%u: skip, not an "
2239 						    "HT20 channel\n", freq);
2240 					continue;
2241 				}
2242 				if ((chanFlags & IEEE80211_CHAN_HT40) &&
2243 				    (flags & IEEE80211_CHAN_HT40) == 0) {
2244 					if (verbose)
2245 						printf("%u: skip, not an "
2246 						    "HT40 channel\n", freq);
2247 					continue;
2248 				}
2249 				/* NB: HT attribute comes from caller */
2250 				flags &= ~IEEE80211_CHAN_HT;
2251 				flags |= chanFlags & IEEE80211_CHAN_HT;
2252 			}
2253 			/*
2254 			 * Check if device can operate on this frequency.
2255 			 */
2256 			if (!checkchan(avail, freq, flags)) {
2257 				if (verbose) {
2258 					printf("%u: skip, ", freq);
2259 					printb("flags", flags,
2260 					    IEEE80211_CHAN_BITS);
2261 					printf(" not available\n");
2262 				}
2263 				continue;
2264 			}
2265 			if ((flags & REQ_ECM) && !reg->ecm) {
2266 				if (verbose)
2267 					printf("%u: skip, ECM channel\n", freq);
2268 				continue;
2269 			}
2270 			if ((flags & REQ_INDOOR) && reg->location == 'O') {
2271 				if (verbose)
2272 					printf("%u: skip, indoor channel\n",
2273 					    freq);
2274 				continue;
2275 			}
2276 			if ((flags & REQ_OUTDOOR) && reg->location == 'I') {
2277 				if (verbose)
2278 					printf("%u: skip, outdoor channel\n",
2279 					    freq);
2280 				continue;
2281 			}
2282 			if ((flags & IEEE80211_CHAN_HT40) &&
2283 			    prev != NULL && (freq - prev->ic_freq) < channelSep) {
2284 				if (verbose)
2285 					printf("%u: skip, only %u channel "
2286 					    "separation, need %d\n", freq,
2287 					    freq - prev->ic_freq, channelSep);
2288 				continue;
2289 			}
2290 			if (ci->ic_nchans == IEEE80211_CHAN_MAX) {
2291 				if (verbose)
2292 					printf("%u: skip, channel table full\n",
2293 					    freq);
2294 				break;
2295 			}
2296 			c = &ci->ic_chans[ci->ic_nchans++];
2297 			memset(c, 0, sizeof(*c));
2298 			c->ic_freq = freq;
2299 			c->ic_flags = flags;
2300 		if (c->ic_flags & IEEE80211_CHAN_DFS)
2301 				c->ic_maxregpower = nb->maxPowerDFS;
2302 			else
2303 				c->ic_maxregpower = nb->maxPower;
2304 			if (verbose) {
2305 				printf("[%3d] add freq %u ",
2306 				    ci->ic_nchans-1, c->ic_freq);
2307 				printb("flags", c->ic_flags, IEEE80211_CHAN_BITS);
2308 				printf(" power %u\n", c->ic_maxregpower);
2309 			}
2310 			/* NB: kernel fills in other fields */
2311 			prev = c;
2312 		}
2313 	}
2314 }
2315 
2316 static void
2317 regdomain_makechannels(
2318 	if_ctx *ctx,
2319 	struct ieee80211_regdomain_req *req,
2320 	const struct ieee80211_devcaps_req *dc)
2321 {
2322 	struct regdata *rdp = getregdata();
2323 	const struct country *cc;
2324 	const struct ieee80211_regdomain *reg = &req->rd;
2325 	struct ieee80211req_chaninfo *ci = &req->chaninfo;
2326 	const struct regdomain *rd;
2327 
2328 	/*
2329 	 * Locate construction table for new channel list.  We treat
2330 	 * the regdomain/SKU as definitive so a country can be in
2331 	 * multiple with different properties (e.g. US in FCC+FCC3).
2332 	 * If no regdomain is specified then we fallback on the country
2333 	 * code to find the associated regdomain since countries always
2334 	 * belong to at least one regdomain.
2335 	 */
2336 	if (reg->regdomain == 0) {
2337 		cc = lib80211_country_findbycc(rdp, reg->country);
2338 		if (cc == NULL)
2339 			errx(1, "internal error, country %d not found",
2340 			    reg->country);
2341 		rd = cc->rd;
2342 	} else
2343 		rd = lib80211_regdomain_findbysku(rdp, reg->regdomain);
2344 	if (rd == NULL)
2345 		errx(1, "internal error, regdomain %d not found",
2346 			    reg->regdomain);
2347 	if (rd->sku != SKU_DEBUG) {
2348 		/*
2349 		 * regdomain_addchans incrememnts the channel count for
2350 		 * each channel it adds so initialize ic_nchans to zero.
2351 		 * Note that we know we have enough space to hold all possible
2352 		 * channels because the devcaps list size was used to
2353 		 * allocate our request.
2354 		 */
2355 		ci->ic_nchans = 0;
2356 		if (!LIST_EMPTY(&rd->bands_11b))
2357 			regdomain_addchans(ctx, ci, &rd->bands_11b, reg,
2358 			    IEEE80211_CHAN_B, &dc->dc_chaninfo);
2359 		if (!LIST_EMPTY(&rd->bands_11g))
2360 			regdomain_addchans(ctx, ci, &rd->bands_11g, reg,
2361 			    IEEE80211_CHAN_G, &dc->dc_chaninfo);
2362 		if (!LIST_EMPTY(&rd->bands_11a))
2363 			regdomain_addchans(ctx, ci, &rd->bands_11a, reg,
2364 			    IEEE80211_CHAN_A, &dc->dc_chaninfo);
2365 		if (!LIST_EMPTY(&rd->bands_11na) && dc->dc_htcaps != 0) {
2366 			regdomain_addchans(ctx, ci, &rd->bands_11na, reg,
2367 			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT20,
2368 			    &dc->dc_chaninfo);
2369 			if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2370 				regdomain_addchans(ctx, ci, &rd->bands_11na, reg,
2371 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U,
2372 				    &dc->dc_chaninfo);
2373 				regdomain_addchans(ctx, ci, &rd->bands_11na, reg,
2374 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D,
2375 				    &dc->dc_chaninfo);
2376 			}
2377 		}
2378 		if (!LIST_EMPTY(&rd->bands_11ac) && dc->dc_vhtcaps != 0) {
2379 			regdomain_addchans(ctx, ci, &rd->bands_11ac, reg,
2380 			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT20 |
2381 			    IEEE80211_CHAN_VHT20,
2382 			    &dc->dc_chaninfo);
2383 
2384 			/* VHT40 is a function of HT40.. */
2385 			if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2386 				regdomain_addchans(ctx, ci, &rd->bands_11ac, reg,
2387 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
2388 				    IEEE80211_CHAN_VHT40U,
2389 				    &dc->dc_chaninfo);
2390 				regdomain_addchans(ctx, ci, &rd->bands_11ac, reg,
2391 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
2392 				    IEEE80211_CHAN_VHT40D,
2393 				    &dc->dc_chaninfo);
2394 			}
2395 
2396 			/* VHT80 is mandatory (and so should be VHT40 above). */
2397 			if (1) {
2398 				regdomain_addchans(ctx, ci, &rd->bands_11ac, reg,
2399 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
2400 				    IEEE80211_CHAN_VHT80,
2401 				    &dc->dc_chaninfo);
2402 				regdomain_addchans(ctx, ci, &rd->bands_11ac, reg,
2403 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
2404 				    IEEE80211_CHAN_VHT80,
2405 				    &dc->dc_chaninfo);
2406 			}
2407 
2408 			/* VHT160 */
2409 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
2410 			    dc->dc_vhtcaps)) {
2411 				regdomain_addchans(ctx, ci, &rd->bands_11ac, reg,
2412 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
2413 				    IEEE80211_CHAN_VHT160,
2414 				    &dc->dc_chaninfo);
2415 				regdomain_addchans(ctx, ci, &rd->bands_11ac, reg,
2416 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
2417 				    IEEE80211_CHAN_VHT160,
2418 				    &dc->dc_chaninfo);
2419 			}
2420 
2421 			/* VHT80P80 */
2422 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
2423 			    dc->dc_vhtcaps)) {
2424 				regdomain_addchans(ctx, ci, &rd->bands_11ac, reg,
2425 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
2426 				    IEEE80211_CHAN_VHT80P80,
2427 				    &dc->dc_chaninfo);
2428 				regdomain_addchans(ctx, ci, &rd->bands_11ac, reg,
2429 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
2430 				    IEEE80211_CHAN_VHT80P80,
2431 				    &dc->dc_chaninfo);
2432 			}
2433 		}
2434 
2435 		if (!LIST_EMPTY(&rd->bands_11ng) && dc->dc_htcaps != 0) {
2436 			regdomain_addchans(ctx, ci, &rd->bands_11ng, reg,
2437 			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT20,
2438 			    &dc->dc_chaninfo);
2439 			if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2440 				regdomain_addchans(ctx, ci, &rd->bands_11ng, reg,
2441 				    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U,
2442 				    &dc->dc_chaninfo);
2443 				regdomain_addchans(ctx, ci, &rd->bands_11ng, reg,
2444 				    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D,
2445 				    &dc->dc_chaninfo);
2446 			}
2447 		}
2448 		qsort(ci->ic_chans, ci->ic_nchans, sizeof(ci->ic_chans[0]),
2449 		    regdomain_sort);
2450 	} else
2451 		memcpy(ci, &dc->dc_chaninfo,
2452 		    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
2453 }
2454 
2455 static void
2456 list_countries(void)
2457 {
2458 	struct regdata *rdp = getregdata();
2459 	const struct country *cp;
2460 	const struct regdomain *dp;
2461 	int i;
2462 
2463 	i = 0;
2464 	printf("\nCountry codes:\n");
2465 	LIST_FOREACH(cp, &rdp->countries, next) {
2466 		printf("%2s %-15.15s%s", cp->isoname,
2467 		    cp->name, ((i+1)%4) == 0 ? "\n" : " ");
2468 		i++;
2469 	}
2470 	i = 0;
2471 	printf("\nRegulatory domains:\n");
2472 	LIST_FOREACH(dp, &rdp->domains, next) {
2473 		printf("%-15.15s%s", dp->name, ((i+1)%4) == 0 ? "\n" : " ");
2474 		i++;
2475 	}
2476 	printf("\n");
2477 }
2478 
2479 static void
2480 defaultcountry(const struct regdomain *rd)
2481 {
2482 	struct regdata *rdp = getregdata();
2483 	const struct country *cc;
2484 
2485 	cc = lib80211_country_findbycc(rdp, rd->cc->code);
2486 	if (cc == NULL)
2487 		errx(1, "internal error, ISO country code %d not "
2488 		    "defined for regdomain %s", rd->cc->code, rd->name);
2489 	regdomain.country = cc->code;
2490 	regdomain.isocc[0] = cc->isoname[0];
2491 	regdomain.isocc[1] = cc->isoname[1];
2492 }
2493 
2494 static void
2495 set80211regdomain(if_ctx *ctx, const char *val, int dummy __unused)
2496 {
2497 	struct regdata *rdp = getregdata();
2498 	const struct regdomain *rd;
2499 
2500 	rd = lib80211_regdomain_findbyname(rdp, val);
2501 	if (rd == NULL) {
2502 		char *eptr;
2503 		long sku = strtol(val, &eptr, 0);
2504 
2505 		if (eptr != val)
2506 			rd = lib80211_regdomain_findbysku(rdp, sku);
2507 		if (eptr == val || rd == NULL)
2508 			errx(1, "unknown regdomain %s", val);
2509 	}
2510 	getregdomain(ctx);
2511 	regdomain.regdomain = rd->sku;
2512 	if (regdomain.country == 0 && rd->cc != NULL) {
2513 		/*
2514 		 * No country code setup and there's a default
2515 		 * one for this regdomain fill it in.
2516 		 */
2517 		defaultcountry(rd);
2518 	}
2519 	callback_register(setregdomain_cb, &regdomain);
2520 }
2521 
2522 static void
2523 set80211country(if_ctx *ctx, const char *val, int dummy __unused)
2524 {
2525 	struct regdata *rdp = getregdata();
2526 	const struct country *cc;
2527 
2528 	cc = lib80211_country_findbyname(rdp, val);
2529 	if (cc == NULL) {
2530 		char *eptr;
2531 		long code = strtol(val, &eptr, 0);
2532 
2533 		if (eptr != val)
2534 			cc = lib80211_country_findbycc(rdp, code);
2535 		if (eptr == val || cc == NULL)
2536 			errx(1, "unknown ISO country code %s", val);
2537 	}
2538 	getregdomain(ctx);
2539 	regdomain.regdomain = cc->rd->sku;
2540 	regdomain.country = cc->code;
2541 	regdomain.isocc[0] = cc->isoname[0];
2542 	regdomain.isocc[1] = cc->isoname[1];
2543 	callback_register(setregdomain_cb, &regdomain);
2544 }
2545 
2546 static void
2547 set80211location(if_ctx *ctx, const char *val __unused, int d)
2548 {
2549 	getregdomain(ctx);
2550 	regdomain.location = d;
2551 	callback_register(setregdomain_cb, &regdomain);
2552 }
2553 
2554 static void
2555 set80211ecm(if_ctx *ctx, const char *val __unused, int d)
2556 {
2557 	getregdomain(ctx);
2558 	regdomain.ecm = d;
2559 	callback_register(setregdomain_cb, &regdomain);
2560 }
2561 
2562 static void
2563 LINE_INIT(char c)
2564 {
2565 	spacer = c;
2566 	if (c == '\t')
2567 		col = 8;
2568 	else
2569 		col = 1;
2570 }
2571 
2572 static void
2573 LINE_BREAK(void)
2574 {
2575 	if (spacer != '\t') {
2576 		printf("\n");
2577 		spacer = '\t';
2578 	}
2579 	col = 8;		/* 8-col tab */
2580 }
2581 
2582 static void
2583 LINE_CHECK(const char *fmt, ...)
2584 {
2585 	char buf[80];
2586 	va_list ap;
2587 	int n;
2588 
2589 	va_start(ap, fmt);
2590 	n = vsnprintf(buf+1, sizeof(buf)-1, fmt, ap);
2591 	va_end(ap);
2592 	col += 1+n;
2593 	if (col > MAXCOL) {
2594 		LINE_BREAK();
2595 		col += n;
2596 	}
2597 	buf[0] = spacer;
2598 	printf("%s", buf);
2599 	spacer = ' ';
2600 }
2601 
2602 static int
2603 getmaxrate(const uint8_t rates[15], uint8_t nrates)
2604 {
2605 	int i, maxrate = -1;
2606 
2607 	for (i = 0; i < nrates; i++) {
2608 		int rate = rates[i] & IEEE80211_RATE_VAL;
2609 		if (rate > maxrate)
2610 			maxrate = rate;
2611 	}
2612 	return maxrate / 2;
2613 }
2614 
2615 static const char *
2616 getcaps(int capinfo)
2617 {
2618 	static char capstring[32];
2619 	char *cp = capstring;
2620 
2621 	if (capinfo & IEEE80211_CAPINFO_ESS)
2622 		*cp++ = 'E';
2623 	if (capinfo & IEEE80211_CAPINFO_IBSS)
2624 		*cp++ = 'I';
2625 	if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
2626 		*cp++ = 'c';
2627 	if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
2628 		*cp++ = 'C';
2629 	if (capinfo & IEEE80211_CAPINFO_PRIVACY)
2630 		*cp++ = 'P';
2631 	if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
2632 		*cp++ = 'S';
2633 	if (capinfo & IEEE80211_CAPINFO_PBCC)
2634 		*cp++ = 'B';
2635 	if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
2636 		*cp++ = 'A';
2637 	if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
2638 		*cp++ = 's';
2639 	if (capinfo & IEEE80211_CAPINFO_RSN)
2640 		*cp++ = 'R';
2641 	if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
2642 		*cp++ = 'D';
2643 	*cp = '\0';
2644 	return capstring;
2645 }
2646 
2647 static const char *
2648 getflags(int flags)
2649 {
2650 	static char flagstring[32];
2651 	char *cp = flagstring;
2652 
2653 	if (flags & IEEE80211_NODE_AUTH)
2654 		*cp++ = 'A';
2655 	if (flags & IEEE80211_NODE_QOS)
2656 		*cp++ = 'Q';
2657 	if (flags & IEEE80211_NODE_ERP)
2658 		*cp++ = 'E';
2659 	if (flags & IEEE80211_NODE_PWR_MGT)
2660 		*cp++ = 'P';
2661 	if (flags & IEEE80211_NODE_HT) {
2662 		*cp++ = 'H';
2663 		if (flags & IEEE80211_NODE_HTCOMPAT)
2664 			*cp++ = '+';
2665 	}
2666 	if (flags & IEEE80211_NODE_VHT)
2667 		*cp++ = 'V';
2668 	if (flags & IEEE80211_NODE_WPS)
2669 		*cp++ = 'W';
2670 	if (flags & IEEE80211_NODE_TSN)
2671 		*cp++ = 'N';
2672 	if (flags & IEEE80211_NODE_AMPDU_TX)
2673 		*cp++ = 'T';
2674 	if (flags & IEEE80211_NODE_AMPDU_RX)
2675 		*cp++ = 'R';
2676 	if (flags & IEEE80211_NODE_MIMO_PS) {
2677 		*cp++ = 'M';
2678 		if (flags & IEEE80211_NODE_MIMO_RTS)
2679 			*cp++ = '+';
2680 	}
2681 	if (flags & IEEE80211_NODE_RIFS)
2682 		*cp++ = 'I';
2683 	if (flags & IEEE80211_NODE_SGI40) {
2684 		*cp++ = 'S';
2685 		if (flags & IEEE80211_NODE_SGI20)
2686 			*cp++ = '+';
2687 	} else if (flags & IEEE80211_NODE_SGI20)
2688 		*cp++ = 's';
2689 	if (flags & IEEE80211_NODE_AMSDU_TX)
2690 		*cp++ = 't';
2691 	if (flags & IEEE80211_NODE_AMSDU_RX)
2692 		*cp++ = 'r';
2693 	if (flags & IEEE80211_NODE_UAPSD)
2694 		*cp++ = 'U';
2695 	if (flags & IEEE80211_NODE_LDPC)
2696 		*cp++ = 'L';
2697 	*cp = '\0';
2698 	return flagstring;
2699 }
2700 
2701 static void
2702 printie(if_ctx *ctx, const char* tag, const uint8_t *ie, size_t ielen, unsigned int maxlen)
2703 {
2704 	printf("%s", tag);
2705 	if (ctx->args->verbose) {
2706 		maxlen -= strlen(tag)+2;
2707 		if (2*ielen > maxlen)
2708 			maxlen--;
2709 		printf("<");
2710 		for (; ielen > 0; ie++, ielen--) {
2711 			if (maxlen-- <= 0)
2712 				break;
2713 			printf("%02x", *ie);
2714 		}
2715 		if (ielen != 0)
2716 			printf("-");
2717 		printf(">");
2718 	}
2719 }
2720 
2721 #define LE_READ_2(p)					\
2722 	((u_int16_t)					\
2723 	 ((((const u_int8_t *)(p))[0]      ) |		\
2724 	  (((const u_int8_t *)(p))[1] <<  8)))
2725 #define LE_READ_4(p)					\
2726 	((u_int32_t)					\
2727 	 ((((const u_int8_t *)(p))[0]      ) |		\
2728 	  (((const u_int8_t *)(p))[1] <<  8) |		\
2729 	  (((const u_int8_t *)(p))[2] << 16) |		\
2730 	  (((const u_int8_t *)(p))[3] << 24)))
2731 
2732 /*
2733  * NB: The decoding routines assume a properly formatted ie
2734  *     which should be safe as the kernel only retains them
2735  *     if they parse ok.
2736  */
2737 
2738 static void
2739 printwmeparam(if_ctx *ctx, const char *tag, const u_int8_t *ie)
2740 {
2741 	static const char *acnames[] = { "BE", "BK", "VO", "VI" };
2742 	const struct ieee80211_wme_param *wme =
2743 	    (const struct ieee80211_wme_param *) ie;
2744 	int i;
2745 
2746 	printf("%s", tag);
2747 	if (!ctx->args->verbose)
2748 		return;
2749 	printf("<qosinfo 0x%x", wme->param_qosInfo);
2750 	ie += offsetof(struct ieee80211_wme_param, params_acParams);
2751 	for (i = 0; i < WME_NUM_AC; i++) {
2752 		const struct ieee80211_wme_acparams *ac =
2753 		    &wme->params_acParams[i];
2754 
2755 		printf(" %s[%saifsn %u cwmin %u cwmax %u txop %u]", acnames[i],
2756 		    _IEEE80211_MASKSHIFT(ac->acp_aci_aifsn, WME_PARAM_ACM) ?
2757 			"acm " : "",
2758 		    _IEEE80211_MASKSHIFT(ac->acp_aci_aifsn, WME_PARAM_AIFSN),
2759 		    _IEEE80211_MASKSHIFT(ac->acp_logcwminmax,
2760 			WME_PARAM_LOGCWMIN),
2761 		    _IEEE80211_MASKSHIFT(ac->acp_logcwminmax,
2762 			WME_PARAM_LOGCWMAX),
2763 		    LE_READ_2(&ac->acp_txop));
2764 	}
2765 	printf(">");
2766 }
2767 
2768 static void
2769 printwmeinfo(if_ctx *ctx, const char *tag, const u_int8_t *ie)
2770 {
2771 	printf("%s", tag);
2772 	if (ctx->args->verbose) {
2773 		const struct ieee80211_wme_info *wme =
2774 		    (const struct ieee80211_wme_info *) ie;
2775 		printf("<version 0x%x info 0x%x>",
2776 		    wme->wme_version, wme->wme_info);
2777 	}
2778 }
2779 
2780 static void
2781 printvhtcap(if_ctx *ctx, const char *tag, const u_int8_t *ie)
2782 {
2783 	printf("%s", tag);
2784 	if (ctx->args->verbose) {
2785 		const struct ieee80211_ie_vhtcap *vhtcap =
2786 		    (const struct ieee80211_ie_vhtcap *) ie;
2787 		uint32_t vhtcap_info = LE_READ_4(&vhtcap->vht_cap_info);
2788 
2789 		printf("<cap 0x%08x", vhtcap_info);
2790 		printf(" rx_mcs_map 0x%x",
2791 		    LE_READ_2(&vhtcap->supp_mcs.rx_mcs_map));
2792 		printf(" rx_highest %d",
2793 		    LE_READ_2(&vhtcap->supp_mcs.rx_highest) & 0x1fff);
2794 		printf(" tx_mcs_map 0x%x",
2795 		    LE_READ_2(&vhtcap->supp_mcs.tx_mcs_map));
2796 		printf(" tx_highest %d",
2797 		    LE_READ_2(&vhtcap->supp_mcs.tx_highest) & 0x1fff);
2798 
2799 		printf(">");
2800 	}
2801 }
2802 
2803 static void
2804 printvhtinfo(if_ctx *ctx, const char *tag, const u_int8_t *ie)
2805 {
2806 	printf("%s", tag);
2807 	if (ctx->args->verbose) {
2808 		const struct ieee80211_ie_vht_operation *vhtinfo =
2809 		    (const struct ieee80211_ie_vht_operation *) ie;
2810 
2811 		printf("<chw %d freq1_idx %d freq2_idx %d basic_mcs_set 0x%04x>",
2812 		    vhtinfo->chan_width,
2813 		    vhtinfo->center_freq_seg1_idx,
2814 		    vhtinfo->center_freq_seg2_idx,
2815 		    LE_READ_2(&vhtinfo->basic_mcs_set));
2816 	}
2817 }
2818 
2819 static void
2820 printvhtpwrenv(if_ctx *ctx, const char *tag, const u_int8_t *ie, size_t ielen)
2821 {
2822 	printf("%s", tag);
2823 	static const char *txpwrmap[] = {
2824 		"20",
2825 		"40",
2826 		"80",
2827 		"160",
2828 	};
2829 	if (ctx->args->verbose) {
2830 		const struct ieee80211_ie_vht_txpwrenv *vhtpwr =
2831 		    (const struct ieee80211_ie_vht_txpwrenv *) ie;
2832 		size_t i, n;
2833 		const char *sep = "";
2834 
2835 		/* Get count; trim at ielen */
2836 		n = (vhtpwr->tx_info &
2837 		    IEEE80211_VHT_TXPWRENV_INFO_COUNT_MASK) + 1;
2838 		/* Trim at ielen */
2839 		if (n + 3 > ielen)
2840 			n = ielen - 3;
2841 		printf("<tx_info 0x%02x pwr:[", vhtpwr->tx_info);
2842 		for (i = 0; i < n; i++) {
2843 			printf("%s%s:%.2f", sep, txpwrmap[i],
2844 			    ((float) ((int8_t) ie[i+3])) / 2.0);
2845 			sep = " ";
2846 		}
2847 
2848 		printf("]>");
2849 	}
2850 }
2851 
2852 static void
2853 printhtcap(if_ctx *ctx, const char *tag, const u_int8_t *ie)
2854 {
2855 	printf("%s", tag);
2856 	if (ctx->args->verbose) {
2857 		const struct ieee80211_ie_htcap *htcap =
2858 		    (const struct ieee80211_ie_htcap *) ie;
2859 		const char *sep;
2860 		int i, j;
2861 
2862 		printf("<cap 0x%x param 0x%x",
2863 		    LE_READ_2(&htcap->hc_cap), htcap->hc_param);
2864 		printf(" mcsset[");
2865 		sep = "";
2866 		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2867 			if (isset(htcap->hc_mcsset, i)) {
2868 				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2869 					if (isclr(htcap->hc_mcsset, j))
2870 						break;
2871 				j--;
2872 				if (i == j)
2873 					printf("%s%u", sep, i);
2874 				else
2875 					printf("%s%u-%u", sep, i, j);
2876 				i += j-i;
2877 				sep = ",";
2878 			}
2879 		printf("] extcap 0x%x txbf 0x%x antenna 0x%x>",
2880 		    LE_READ_2(&htcap->hc_extcap),
2881 		    LE_READ_4(&htcap->hc_txbf),
2882 		    htcap->hc_antenna);
2883 	}
2884 }
2885 
2886 static void
2887 printhtinfo(if_ctx *ctx, const char *tag, const u_int8_t *ie)
2888 {
2889 	printf("%s", tag);
2890 	if (ctx->args->verbose) {
2891 		const struct ieee80211_ie_htinfo *htinfo =
2892 		    (const struct ieee80211_ie_htinfo *) ie;
2893 		const char *sep;
2894 		int i, j;
2895 
2896 		printf("<ctl %u, %x,%x,%x,%x", htinfo->hi_ctrlchannel,
2897 		    htinfo->hi_byte1, htinfo->hi_byte2, htinfo->hi_byte3,
2898 		    LE_READ_2(&htinfo->hi_byte45));
2899 		printf(" basicmcs[");
2900 		sep = "";
2901 		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2902 			if (isset(htinfo->hi_basicmcsset, i)) {
2903 				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2904 					if (isclr(htinfo->hi_basicmcsset, j))
2905 						break;
2906 				j--;
2907 				if (i == j)
2908 					printf("%s%u", sep, i);
2909 				else
2910 					printf("%s%u-%u", sep, i, j);
2911 				i += j-i;
2912 				sep = ",";
2913 			}
2914 		printf("]>");
2915 	}
2916 }
2917 
2918 static void
2919 printathie(if_ctx *ctx, const char *tag, const u_int8_t *ie)
2920 {
2921 
2922 	printf("%s", tag);
2923 	if (ctx->args->verbose) {
2924 		const struct ieee80211_ath_ie *ath =
2925 			(const struct ieee80211_ath_ie *)ie;
2926 
2927 		printf("<");
2928 		if (ath->ath_capability & ATHEROS_CAP_TURBO_PRIME)
2929 			printf("DTURBO,");
2930 		if (ath->ath_capability & ATHEROS_CAP_COMPRESSION)
2931 			printf("COMP,");
2932 		if (ath->ath_capability & ATHEROS_CAP_FAST_FRAME)
2933 			printf("FF,");
2934 		if (ath->ath_capability & ATHEROS_CAP_XR)
2935 			printf("XR,");
2936 		if (ath->ath_capability & ATHEROS_CAP_AR)
2937 			printf("AR,");
2938 		if (ath->ath_capability & ATHEROS_CAP_BURST)
2939 			printf("BURST,");
2940 		if (ath->ath_capability & ATHEROS_CAP_WME)
2941 			printf("WME,");
2942 		if (ath->ath_capability & ATHEROS_CAP_BOOST)
2943 			printf("BOOST,");
2944 		printf("0x%x>", LE_READ_2(ath->ath_defkeyix));
2945 	}
2946 }
2947 
2948 
2949 static void
2950 printmeshconf(if_ctx *ctx, const char *tag, const uint8_t *ie)
2951 {
2952 
2953 	printf("%s", tag);
2954 	if (ctx->args->verbose) {
2955 		const struct ieee80211_meshconf_ie *mconf =
2956 			(const struct ieee80211_meshconf_ie *)ie;
2957 		printf("<PATH:");
2958 		if (mconf->conf_pselid == IEEE80211_MESHCONF_PATH_HWMP)
2959 			printf("HWMP");
2960 		else
2961 			printf("UNKNOWN");
2962 		printf(" LINK:");
2963 		if (mconf->conf_pmetid == IEEE80211_MESHCONF_METRIC_AIRTIME)
2964 			printf("AIRTIME");
2965 		else
2966 			printf("UNKNOWN");
2967 		printf(" CONGESTION:");
2968 		if (mconf->conf_ccid == IEEE80211_MESHCONF_CC_DISABLED)
2969 			printf("DISABLED");
2970 		else
2971 			printf("UNKNOWN");
2972 		printf(" SYNC:");
2973 		if (mconf->conf_syncid == IEEE80211_MESHCONF_SYNC_NEIGHOFF)
2974 			printf("NEIGHOFF");
2975 		else
2976 			printf("UNKNOWN");
2977 		printf(" AUTH:");
2978 		if (mconf->conf_authid == IEEE80211_MESHCONF_AUTH_DISABLED)
2979 			printf("DISABLED");
2980 		else
2981 			printf("UNKNOWN");
2982 		printf(" FORM:0x%x CAPS:0x%x>", mconf->conf_form,
2983 		    mconf->conf_cap);
2984 	}
2985 }
2986 
2987 static void
2988 printbssload(if_ctx *ctx, const char *tag, const uint8_t *ie)
2989 {
2990 	printf("%s", tag);
2991 	if (ctx->args->verbose) {
2992 		const struct ieee80211_bss_load_ie *bssload =
2993 		    (const struct ieee80211_bss_load_ie *) ie;
2994 		printf("<sta count %d, chan load %d, aac %d>",
2995 		    LE_READ_2(&bssload->sta_count),
2996 		    bssload->chan_load,
2997 		    bssload->aac);
2998 	}
2999 }
3000 
3001 static void
3002 printapchanrep(if_ctx *ctx, const char *tag, const u_int8_t *ie, size_t ielen)
3003 {
3004 	printf("%s", tag);
3005 	if (ctx->args->verbose) {
3006 		const struct ieee80211_ap_chan_report_ie *ap =
3007 		    (const struct ieee80211_ap_chan_report_ie *) ie;
3008 		const char *sep = "";
3009 
3010 		printf("<class %u, chan:[", ap->i_class);
3011 
3012 		for (size_t i = 3; i < ielen; i++) {
3013 			printf("%s%u", sep, ie[i]);
3014 			sep = ",";
3015 		}
3016 		printf("]>");
3017 	}
3018 }
3019 
3020 static const char *
3021 wpa_cipher(const u_int8_t *sel)
3022 {
3023 #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
3024 	u_int32_t w = LE_READ_4(sel);
3025 
3026 	switch (w) {
3027 	case WPA_SEL(WPA_CSE_NULL):
3028 		return "NONE";
3029 	case WPA_SEL(WPA_CSE_WEP40):
3030 		return "WEP40";
3031 	case WPA_SEL(WPA_CSE_WEP104):
3032 		return "WEP104";
3033 	case WPA_SEL(WPA_CSE_TKIP):
3034 		return "TKIP";
3035 	case WPA_SEL(WPA_CSE_CCMP):
3036 		return "AES-CCMP";
3037 	}
3038 	return "?";		/* NB: so 1<< is discarded */
3039 #undef WPA_SEL
3040 }
3041 
3042 static const char *
3043 wpa_keymgmt(const u_int8_t *sel)
3044 {
3045 #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
3046 	u_int32_t w = LE_READ_4(sel);
3047 
3048 	switch (w) {
3049 	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
3050 		return "8021X-UNSPEC";
3051 	case WPA_SEL(WPA_ASE_8021X_PSK):
3052 		return "8021X-PSK";
3053 	case WPA_SEL(WPA_ASE_NONE):
3054 		return "NONE";
3055 	}
3056 	return "?";
3057 #undef WPA_SEL
3058 }
3059 
3060 static void
3061 printwpaie(if_ctx *ctx, const char *tag, const u_int8_t *ie)
3062 {
3063 	u_int8_t len = ie[1];
3064 
3065 	printf("%s", tag);
3066 	if (ctx->args->verbose) {
3067 		const char *sep;
3068 		int n;
3069 
3070 		ie += 6, len -= 4;		/* NB: len is payload only */
3071 
3072 		printf("<v%u", LE_READ_2(ie));
3073 		ie += 2, len -= 2;
3074 
3075 		printf(" mc:%s", wpa_cipher(ie));
3076 		ie += 4, len -= 4;
3077 
3078 		/* unicast ciphers */
3079 		n = LE_READ_2(ie);
3080 		ie += 2, len -= 2;
3081 		sep = " uc:";
3082 		for (; n > 0; n--) {
3083 			printf("%s%s", sep, wpa_cipher(ie));
3084 			ie += 4, len -= 4;
3085 			sep = "+";
3086 		}
3087 
3088 		/* key management algorithms */
3089 		n = LE_READ_2(ie);
3090 		ie += 2, len -= 2;
3091 		sep = " km:";
3092 		for (; n > 0; n--) {
3093 			printf("%s%s", sep, wpa_keymgmt(ie));
3094 			ie += 4, len -= 4;
3095 			sep = "+";
3096 		}
3097 
3098 		if (len > 2)		/* optional capabilities */
3099 			printf(", caps 0x%x", LE_READ_2(ie));
3100 		printf(">");
3101 	}
3102 }
3103 
3104 static const char *
3105 rsn_cipher(const u_int8_t *sel)
3106 {
3107 #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
3108 	u_int32_t w = LE_READ_4(sel);
3109 
3110 	switch (w) {
3111 	case RSN_SEL(RSN_CSE_NULL):
3112 		return "NONE";
3113 	case RSN_SEL(RSN_CSE_WEP40):
3114 		return "WEP40";
3115 	case RSN_SEL(RSN_CSE_WEP104):
3116 		return "WEP104";
3117 	case RSN_SEL(RSN_CSE_TKIP):
3118 		return "TKIP";
3119 	case RSN_SEL(RSN_CSE_CCMP):
3120 		return "AES-CCMP";
3121 	case RSN_SEL(RSN_CSE_WRAP):
3122 		return "AES-OCB";
3123 	}
3124 	return "?";
3125 #undef WPA_SEL
3126 }
3127 
3128 static const char *
3129 rsn_keymgmt(const u_int8_t *sel)
3130 {
3131 #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
3132 	u_int32_t w = LE_READ_4(sel);
3133 
3134 	switch (w) {
3135 	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
3136 		return "8021X-UNSPEC";
3137 	case RSN_SEL(RSN_ASE_8021X_PSK):
3138 		return "8021X-PSK";
3139 	case RSN_SEL(RSN_ASE_NONE):
3140 		return "NONE";
3141 	}
3142 	return "?";
3143 #undef RSN_SEL
3144 }
3145 
3146 static void
3147 printrsnie(if_ctx *ctx, const char *tag, const u_int8_t *ie, size_t ielen)
3148 {
3149 	printf("%s", tag);
3150 	if (ctx->args->verbose) {
3151 		const char *sep;
3152 		int n;
3153 
3154 		ie += 2, ielen -= 2;
3155 
3156 		printf("<v%u", LE_READ_2(ie));
3157 		ie += 2, ielen -= 2;
3158 
3159 		printf(" mc:%s", rsn_cipher(ie));
3160 		ie += 4, ielen -= 4;
3161 
3162 		/* unicast ciphers */
3163 		n = LE_READ_2(ie);
3164 		ie += 2, ielen -= 2;
3165 		sep = " uc:";
3166 		for (; n > 0; n--) {
3167 			printf("%s%s", sep, rsn_cipher(ie));
3168 			ie += 4, ielen -= 4;
3169 			sep = "+";
3170 		}
3171 
3172 		/* key management algorithms */
3173 		n = LE_READ_2(ie);
3174 		ie += 2, ielen -= 2;
3175 		sep = " km:";
3176 		for (; n > 0; n--) {
3177 			printf("%s%s", sep, rsn_keymgmt(ie));
3178 			ie += 4, ielen -= 4;
3179 			sep = "+";
3180 		}
3181 
3182 		if (ielen > 2)		/* optional capabilities */
3183 			printf(", caps 0x%x", LE_READ_2(ie));
3184 		/* XXXPMKID */
3185 		printf(">");
3186 	}
3187 }
3188 
3189 #define BE_READ_2(p)					\
3190 	((u_int16_t)					\
3191 	 ((((const u_int8_t *)(p))[1]      ) |		\
3192 	  (((const u_int8_t *)(p))[0] <<  8)))
3193 
3194 static void
3195 printwpsie(if_ctx *ctx, const char *tag, const u_int8_t *ie)
3196 {
3197 	u_int8_t len = ie[1];
3198 
3199 	printf("%s", tag);
3200 	if (ctx->args->verbose) {
3201 		static const char *dev_pass_id[] = {
3202 			"D",	/* Default (PIN) */
3203 			"U",	/* User-specified */
3204 			"M",	/* Machine-specified */
3205 			"K",	/* Rekey */
3206 			"P",	/* PushButton */
3207 			"R"	/* Registrar-specified */
3208 		};
3209 		int n;
3210 		int f;
3211 
3212 		ie +=6, len -= 4;		/* NB: len is payload only */
3213 
3214 		/* WPS IE in Beacon and Probe Resp frames have different fields */
3215 		printf("<");
3216 		while (len) {
3217 			uint16_t tlv_type = BE_READ_2(ie);
3218 			uint16_t tlv_len  = BE_READ_2(ie + 2);
3219 			uint16_t cfg_mthd;
3220 
3221 			/* some devices broadcast invalid WPS frames */
3222 			if (tlv_len > len) {
3223 				printf("bad frame length tlv_type=0x%02x "
3224 				    "tlv_len=%d len=%d", tlv_type, tlv_len,
3225 				    len);
3226 				break;
3227 			}
3228 
3229 			ie += 4, len -= 4;
3230 
3231 			switch (tlv_type) {
3232 			case IEEE80211_WPS_ATTR_VERSION:
3233 				printf("v:%d.%d", *ie >> 4, *ie & 0xf);
3234 				break;
3235 			case IEEE80211_WPS_ATTR_AP_SETUP_LOCKED:
3236 				printf(" ap_setup:%s", *ie ? "locked" :
3237 				    "unlocked");
3238 				break;
3239 			case IEEE80211_WPS_ATTR_CONFIG_METHODS:
3240 			case IEEE80211_WPS_ATTR_SELECTED_REGISTRAR_CONFIG_METHODS:
3241 				if (tlv_type == IEEE80211_WPS_ATTR_SELECTED_REGISTRAR_CONFIG_METHODS)
3242 					printf(" sel_reg_cfg_mthd:");
3243 				else
3244 					printf(" cfg_mthd:" );
3245 				cfg_mthd = BE_READ_2(ie);
3246 				f = 0;
3247 				for (n = 15; n >= 0; n--) {
3248 					if (f) {
3249 						printf(",");
3250 						f = 0;
3251 					}
3252 					switch (cfg_mthd & (1 << n)) {
3253 					case 0:
3254 						break;
3255 					case IEEE80211_WPS_CONFIG_USBA:
3256 						printf("usba");
3257 						f++;
3258 						break;
3259 					case IEEE80211_WPS_CONFIG_ETHERNET:
3260 						printf("ethernet");
3261 						f++;
3262 						break;
3263 					case IEEE80211_WPS_CONFIG_LABEL:
3264 						printf("label");
3265 						f++;
3266 						break;
3267 					case IEEE80211_WPS_CONFIG_DISPLAY:
3268 						if (!(cfg_mthd &
3269 						    (IEEE80211_WPS_CONFIG_VIRT_DISPLAY |
3270 						    IEEE80211_WPS_CONFIG_PHY_DISPLAY)))
3271 						    {
3272 							printf("display");
3273 							f++;
3274 						}
3275 						break;
3276 					case IEEE80211_WPS_CONFIG_EXT_NFC_TOKEN:
3277 						printf("ext_nfc_tokenk");
3278 						f++;
3279 						break;
3280 					case IEEE80211_WPS_CONFIG_INT_NFC_TOKEN:
3281 						printf("int_nfc_token");
3282 						f++;
3283 						break;
3284 					case IEEE80211_WPS_CONFIG_NFC_INTERFACE:
3285 						printf("nfc_interface");
3286 						f++;
3287 						break;
3288 					case IEEE80211_WPS_CONFIG_PUSHBUTTON:
3289 						if (!(cfg_mthd &
3290 						    (IEEE80211_WPS_CONFIG_VIRT_PUSHBUTTON |
3291 						    IEEE80211_WPS_CONFIG_PHY_PUSHBUTTON))) {
3292 							printf("push_button");
3293 							f++;
3294 						}
3295 						break;
3296 					case IEEE80211_WPS_CONFIG_KEYPAD:
3297 						printf("keypad");
3298 						f++;
3299 						break;
3300 					case IEEE80211_WPS_CONFIG_VIRT_PUSHBUTTON:
3301 						printf("virtual_push_button");
3302 						f++;
3303 						break;
3304 					case IEEE80211_WPS_CONFIG_PHY_PUSHBUTTON:
3305 						printf("physical_push_button");
3306 						f++;
3307 						break;
3308 					case IEEE80211_WPS_CONFIG_P2PS:
3309 						printf("p2ps");
3310 						f++;
3311 						break;
3312 					case IEEE80211_WPS_CONFIG_VIRT_DISPLAY:
3313 						printf("virtual_display");
3314 						f++;
3315 						break;
3316 					case IEEE80211_WPS_CONFIG_PHY_DISPLAY:
3317 						printf("physical_display");
3318 						f++;
3319 						break;
3320 					default:
3321 						printf("unknown_wps_config<%04x>",
3322 						    cfg_mthd & (1 << n));
3323 						f++;
3324 						break;
3325 					}
3326 				}
3327 				break;
3328 			case IEEE80211_WPS_ATTR_DEV_NAME:
3329 				printf(" device_name:<%.*s>", tlv_len, ie);
3330 				break;
3331 			case IEEE80211_WPS_ATTR_DEV_PASSWORD_ID:
3332 				n = LE_READ_2(ie);
3333 				if (n < (int)nitems(dev_pass_id))
3334 					printf(" dpi:%s", dev_pass_id[n]);
3335 				break;
3336 			case IEEE80211_WPS_ATTR_MANUFACTURER:
3337 				printf(" manufacturer:<%.*s>", tlv_len, ie);
3338 				break;
3339 			case IEEE80211_WPS_ATTR_MODEL_NAME:
3340 				printf(" model_name:<%.*s>", tlv_len, ie);
3341 				break;
3342 			case IEEE80211_WPS_ATTR_MODEL_NUMBER:
3343 				printf(" model_number:<%.*s>", tlv_len, ie);
3344 				break;
3345 			case IEEE80211_WPS_ATTR_PRIMARY_DEV_TYPE:
3346 				printf(" prim_dev:");
3347 				for (n = 0; n < tlv_len; n++)
3348 					printf("%02x", ie[n]);
3349 				break;
3350 			case IEEE80211_WPS_ATTR_RF_BANDS:
3351 				printf(" rf:");
3352 				f = 0;
3353 				for (n = 7; n >= 0; n--) {
3354 					if (f) {
3355 						printf(",");
3356 						f = 0;
3357 					}
3358 					switch (*ie & (1 << n)) {
3359 					case 0:
3360 						break;
3361 					case IEEE80211_WPS_RF_BAND_24GHZ:
3362 						printf("2.4Ghz");
3363 						f++;
3364 						break;
3365 					case IEEE80211_WPS_RF_BAND_50GHZ:
3366 						printf("5Ghz");
3367 						f++;
3368 						break;
3369 					case IEEE80211_WPS_RF_BAND_600GHZ:
3370 						printf("60Ghz");
3371 						f++;
3372 						break;
3373 					default:
3374 						printf("unknown<%02x>",
3375 						    *ie & (1 << n));
3376 						f++;
3377 						break;
3378 					}
3379 				}
3380 				break;
3381 			case IEEE80211_WPS_ATTR_RESPONSE_TYPE:
3382 				printf(" resp_type:0x%02x", *ie);
3383 				break;
3384 			case IEEE80211_WPS_ATTR_SELECTED_REGISTRAR:
3385 				printf(" sel:%s", *ie ? "T" : "F");
3386 				break;
3387 			case IEEE80211_WPS_ATTR_SERIAL_NUMBER:
3388 				printf(" serial_number:<%.*s>", tlv_len, ie);
3389 				break;
3390 			case IEEE80211_WPS_ATTR_UUID_E:
3391 				printf(" uuid-e:");
3392 				for (n = 0; n < (tlv_len - 1); n++)
3393 					printf("%02x-", ie[n]);
3394 				printf("%02x", ie[n]);
3395 				break;
3396 			case IEEE80211_WPS_ATTR_VENDOR_EXT:
3397 				printf(" vendor:");
3398 				for (n = 0; n < tlv_len; n++)
3399 					printf("%02x", ie[n]);
3400 				break;
3401 			case IEEE80211_WPS_ATTR_WPS_STATE:
3402 				switch (*ie) {
3403 				case IEEE80211_WPS_STATE_NOT_CONFIGURED:
3404 					printf(" state:N");
3405 					break;
3406 				case IEEE80211_WPS_STATE_CONFIGURED:
3407 					printf(" state:C");
3408 					break;
3409 				default:
3410 					printf(" state:B<%02x>", *ie);
3411 					break;
3412 				}
3413 				break;
3414 			default:
3415 				printf(" unknown_wps_attr:0x%x", tlv_type);
3416 				break;
3417 			}
3418 			ie += tlv_len, len -= tlv_len;
3419 		}
3420 		printf(">");
3421 	}
3422 }
3423 
3424 static void
3425 printtdmaie(if_ctx *ctx, const char *tag, const u_int8_t *ie, size_t ielen)
3426 {
3427 	printf("%s", tag);
3428 	if (ctx->args->verbose && ielen >= sizeof(struct ieee80211_tdma_param)) {
3429 		const struct ieee80211_tdma_param *tdma =
3430 		   (const struct ieee80211_tdma_param *) ie;
3431 
3432 		/* XXX tstamp */
3433 		printf("<v%u slot:%u slotcnt:%u slotlen:%u bintval:%u inuse:0x%x>",
3434 		    tdma->tdma_version, tdma->tdma_slot, tdma->tdma_slotcnt,
3435 		    LE_READ_2(&tdma->tdma_slotlen), tdma->tdma_bintval,
3436 		    tdma->tdma_inuse[0]);
3437 	}
3438 }
3439 
3440 /*
3441  * Copy the ssid string contents into buf, truncating to fit.  If the
3442  * ssid is entirely printable then just copy intact.  Otherwise convert
3443  * to hexadecimal.  If the result is truncated then replace the last
3444  * three characters with "...".
3445  */
3446 static int
3447 copy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
3448 {
3449 	const u_int8_t *p;
3450 	size_t maxlen;
3451 	u_int i;
3452 
3453 	if (essid_len > bufsize)
3454 		maxlen = bufsize;
3455 	else
3456 		maxlen = essid_len;
3457 	/* determine printable or not */
3458 	for (i = 0, p = essid; i < maxlen; i++, p++) {
3459 		if (*p < ' ' || *p > 0x7e)
3460 			break;
3461 	}
3462 	if (i != maxlen) {		/* not printable, print as hex */
3463 		if (bufsize < 3)
3464 			return 0;
3465 		strlcpy(buf, "0x", bufsize);
3466 		bufsize -= 2;
3467 		p = essid;
3468 		for (i = 0; i < maxlen && bufsize >= 2; i++) {
3469 			sprintf(&buf[2+2*i], "%02x", p[i]);
3470 			bufsize -= 2;
3471 		}
3472 		if (i != essid_len)
3473 			memcpy(&buf[2+2*i-3], "...", 3);
3474 	} else {			/* printable, truncate as needed */
3475 		memcpy(buf, essid, maxlen);
3476 		if (maxlen != essid_len)
3477 			memcpy(&buf[maxlen-3], "...", 3);
3478 	}
3479 	return maxlen;
3480 }
3481 
3482 static void
3483 printssid(const char *tag, const u_int8_t *ie, int maxlen)
3484 {
3485 	char ssid[2*IEEE80211_NWID_LEN+1];
3486 
3487 	printf("%s<%.*s>", tag, copy_essid(ssid, maxlen, ie+2, ie[1]), ssid);
3488 }
3489 
3490 static void
3491 printrates(const char *tag, const u_int8_t *ie, size_t ielen)
3492 {
3493 	const char *sep;
3494 
3495 	printf("%s", tag);
3496 	sep = "<";
3497 	for (size_t i = 2; i < ielen; i++) {
3498 		printf("%s%s%d", sep,
3499 		    ie[i] & IEEE80211_RATE_BASIC ? "B" : "",
3500 		    ie[i] & IEEE80211_RATE_VAL);
3501 		sep = ",";
3502 	}
3503 	printf(">");
3504 }
3505 
3506 static void
3507 printcountry(const char *tag, const u_int8_t *ie)
3508 {
3509 	const struct ieee80211_country_ie *cie =
3510 	   (const struct ieee80211_country_ie *) ie;
3511 	int i, nbands, schan, nchan;
3512 
3513 	printf("%s<%c%c%c", tag, cie->cc[0], cie->cc[1], cie->cc[2]);
3514 	nbands = (cie->len - 3) / sizeof(cie->band[0]);
3515 	for (i = 0; i < nbands; i++) {
3516 		schan = cie->band[i].schan;
3517 		nchan = cie->band[i].nchan;
3518 		if (nchan != 1)
3519 			printf(" %u-%u,%u", schan, schan + nchan-1,
3520 			    cie->band[i].maxtxpwr);
3521 		else
3522 			printf(" %u,%u", schan, cie->band[i].maxtxpwr);
3523 	}
3524 	printf(">");
3525 }
3526 
3527 static __inline int
3528 iswpaoui(const u_int8_t *frm)
3529 {
3530 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
3531 }
3532 
3533 static __inline int
3534 iswmeinfo(const u_int8_t *frm)
3535 {
3536 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3537 		frm[6] == WME_INFO_OUI_SUBTYPE;
3538 }
3539 
3540 static __inline int
3541 iswmeparam(const u_int8_t *frm)
3542 {
3543 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3544 		frm[6] == WME_PARAM_OUI_SUBTYPE;
3545 }
3546 
3547 static __inline int
3548 isatherosoui(const u_int8_t *frm)
3549 {
3550 	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
3551 }
3552 
3553 static __inline int
3554 istdmaoui(const uint8_t *frm)
3555 {
3556 	return frm[1] > 3 && LE_READ_4(frm+2) == ((TDMA_OUI_TYPE<<24)|TDMA_OUI);
3557 }
3558 
3559 static __inline int
3560 iswpsoui(const uint8_t *frm)
3561 {
3562 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPS_OUI_TYPE<<24)|WPA_OUI);
3563 }
3564 
3565 static const char *
3566 iename(int elemid)
3567 {
3568 	static char iename_buf[64];
3569 	switch (elemid) {
3570 	case IEEE80211_ELEMID_FHPARMS:	return " FHPARMS";
3571 	case IEEE80211_ELEMID_CFPARMS:	return " CFPARMS";
3572 	case IEEE80211_ELEMID_TIM:	return " TIM";
3573 	case IEEE80211_ELEMID_IBSSPARMS:return " IBSSPARMS";
3574 	case IEEE80211_ELEMID_BSSLOAD:	return " BSSLOAD";
3575 	case IEEE80211_ELEMID_CHALLENGE:return " CHALLENGE";
3576 	case IEEE80211_ELEMID_PWRCNSTR:	return " PWRCNSTR";
3577 	case IEEE80211_ELEMID_PWRCAP:	return " PWRCAP";
3578 	case IEEE80211_ELEMID_TPCREQ:	return " TPCREQ";
3579 	case IEEE80211_ELEMID_TPCREP:	return " TPCREP";
3580 	case IEEE80211_ELEMID_SUPPCHAN:	return " SUPPCHAN";
3581 	case IEEE80211_ELEMID_CSA:	return " CSA";
3582 	case IEEE80211_ELEMID_MEASREQ:	return " MEASREQ";
3583 	case IEEE80211_ELEMID_MEASREP:	return " MEASREP";
3584 	case IEEE80211_ELEMID_QUIET:	return " QUIET";
3585 	case IEEE80211_ELEMID_IBSSDFS:	return " IBSSDFS";
3586 	case IEEE80211_ELEMID_RESERVED_47:
3587 					return " RESERVED_47";
3588 	case IEEE80211_ELEMID_MOBILITY_DOMAIN:
3589 					return " MOBILITY_DOMAIN";
3590 	case IEEE80211_ELEMID_RRM_ENACAPS:
3591 					return " RRM_ENCAPS";
3592 	case IEEE80211_ELEMID_OVERLAP_BSS_SCAN_PARAM:
3593 					return " OVERLAP_BSS";
3594 	case IEEE80211_ELEMID_TPC:	return " TPC";
3595 	case IEEE80211_ELEMID_CCKM:	return " CCKM";
3596 	case IEEE80211_ELEMID_EXTCAP:	return " EXTCAP";
3597 	}
3598 	snprintf(iename_buf, sizeof(iename_buf), " UNKNOWN_ELEMID_%d",
3599 	    elemid);
3600 	return (const char *) iename_buf;
3601 }
3602 
3603 static void
3604 printies(if_ctx *ctx, const u_int8_t *vp, int ielen, unsigned int maxcols)
3605 {
3606 	const int verbose = ctx->args->verbose;
3607 
3608 	while (ielen > 0) {
3609 		switch (vp[0]) {
3610 		case IEEE80211_ELEMID_SSID:
3611 			if (verbose)
3612 				printssid(" SSID", vp, maxcols);
3613 			break;
3614 		case IEEE80211_ELEMID_RATES:
3615 		case IEEE80211_ELEMID_XRATES:
3616 			if (verbose)
3617 				printrates(vp[0] == IEEE80211_ELEMID_RATES ?
3618 				    " RATES" : " XRATES", vp, 2+vp[1]);
3619 			break;
3620 		case IEEE80211_ELEMID_DSPARMS:
3621 			if (verbose)
3622 				printf(" DSPARMS<%u>", vp[2]);
3623 			break;
3624 		case IEEE80211_ELEMID_COUNTRY:
3625 			if (verbose)
3626 				printcountry(" COUNTRY", vp);
3627 			break;
3628 		case IEEE80211_ELEMID_ERP:
3629 			if (verbose)
3630 				printf(" ERP<0x%x>", vp[2]);
3631 			break;
3632 		case IEEE80211_ELEMID_VENDOR:
3633 			if (iswpaoui(vp))
3634 				printwpaie(ctx, " WPA", vp);
3635 			else if (iswmeinfo(vp))
3636 				printwmeinfo(ctx, " WME", vp);
3637 			else if (iswmeparam(vp))
3638 				printwmeparam(ctx, " WME", vp);
3639 			else if (isatherosoui(vp))
3640 				printathie(ctx, " ATH", vp);
3641 			else if (iswpsoui(vp))
3642 				printwpsie(ctx, " WPS", vp);
3643 			else if (istdmaoui(vp))
3644 				printtdmaie(ctx, " TDMA", vp, 2+vp[1]);
3645 			else if (verbose)
3646 					printie(ctx, " VEN", vp, 2+vp[1], maxcols);
3647 				break;
3648 		case IEEE80211_ELEMID_RSN:
3649 			printrsnie(ctx, " RSN", vp, 2+vp[1]);
3650 			break;
3651 		case IEEE80211_ELEMID_HTCAP:
3652 			printhtcap(ctx, " HTCAP", vp);
3653 			break;
3654 		case IEEE80211_ELEMID_HTINFO:
3655 			if (verbose)
3656 				printhtinfo(ctx, " HTINFO", vp);
3657 			break;
3658 		case IEEE80211_ELEMID_MESHID:
3659 			if (verbose)
3660 				printssid(" MESHID", vp, maxcols);
3661 			break;
3662 		case IEEE80211_ELEMID_MESHCONF:
3663 			printmeshconf(ctx, " MESHCONF", vp);
3664 			break;
3665 		case IEEE80211_ELEMID_VHT_CAP:
3666 			printvhtcap(ctx, " VHTCAP", vp);
3667 			break;
3668 		case IEEE80211_ELEMID_VHT_OPMODE:
3669 			printvhtinfo(ctx, " VHTOPMODE", vp);
3670 			break;
3671 		case IEEE80211_ELEMID_VHT_PWR_ENV:
3672 			printvhtpwrenv(ctx, " VHTPWRENV", vp, 2+vp[1]);
3673 			break;
3674 		case IEEE80211_ELEMID_BSSLOAD:
3675 			printbssload(ctx, " BSSLOAD", vp);
3676 			break;
3677 		case IEEE80211_ELEMID_APCHANREP:
3678 			printapchanrep(ctx, " APCHANREP", vp, 2+vp[1]);
3679 			break;
3680 		default:
3681 			if (verbose)
3682 				printie(ctx, iename(vp[0]), vp, 2+vp[1], maxcols);
3683 			break;
3684 		}
3685 		ielen -= 2+vp[1];
3686 		vp += 2+vp[1];
3687 	}
3688 }
3689 
3690 static void
3691 printmimo(const struct ieee80211_mimo_info *mi)
3692 {
3693 	int i;
3694 	int r = 0;
3695 
3696 	for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3697 		if (mi->ch[i].rssi[0] != 0) {
3698 			r = 1;
3699 			break;
3700 		}
3701 	}
3702 
3703 	/* NB: don't muddy display unless there's something to show */
3704 	if (r == 0)
3705 		return;
3706 
3707 	/* XXX TODO: ignore EVM; secondary channels for now */
3708 	printf(" (rssi %.1f:%.1f:%.1f:%.1f nf %d:%d:%d:%d)",
3709 	    mi->ch[0].rssi[0] / 2.0,
3710 	    mi->ch[1].rssi[0] / 2.0,
3711 	    mi->ch[2].rssi[0] / 2.0,
3712 	    mi->ch[3].rssi[0] / 2.0,
3713 	    mi->ch[0].noise[0],
3714 	    mi->ch[1].noise[0],
3715 	    mi->ch[2].noise[0],
3716 	    mi->ch[3].noise[0]);
3717 }
3718 
3719 static void
3720 printbssidname(const struct ether_addr *n)
3721 {
3722 	char name[MAXHOSTNAMELEN + 1];
3723 
3724 	if (ether_ntohost(name, n) != 0)
3725 		return;
3726 
3727 	printf(" (%s)", name);
3728 }
3729 
3730 static void
3731 list_scan(if_ctx *ctx)
3732 {
3733 	uint8_t buf[24*1024];
3734 	char ssid[IEEE80211_NWID_LEN+1];
3735 	const uint8_t *cp;
3736 	int len, idlen;
3737 
3738 	if (get80211len(ctx, IEEE80211_IOC_SCAN_RESULTS, buf, sizeof(buf), &len) < 0)
3739 		errx(1, "unable to get scan results");
3740 	if (len < (int)sizeof(struct ieee80211req_scan_result))
3741 		return;
3742 
3743 	getchaninfo(ctx);
3744 
3745 	printf("%-*.*s  %-17.17s  %4s %4s   %-7s  %3s %4s\n"
3746 		, IEEE80211_NWID_LEN, IEEE80211_NWID_LEN, "SSID/MESH ID"
3747 		, "BSSID"
3748 		, "CHAN"
3749 		, "RATE"
3750 		, " S:N"
3751 		, "INT"
3752 		, "CAPS"
3753 	);
3754 	cp = buf;
3755 	do {
3756 		const struct ieee80211req_scan_result *sr;
3757 		const uint8_t *vp, *idp;
3758 
3759 		sr = (const struct ieee80211req_scan_result *)(const void *) cp;
3760 		vp = cp + sr->isr_ie_off;
3761 		if (sr->isr_meshid_len) {
3762 			idp = vp + sr->isr_ssid_len;
3763 			idlen = sr->isr_meshid_len;
3764 		} else {
3765 			idp = vp;
3766 			idlen = sr->isr_ssid_len;
3767 		}
3768 		printf("%-*.*s  %s  %3d  %3dM %4d:%-4d %4d %-4.4s"
3769 			, IEEE80211_NWID_LEN
3770 			  , copy_essid(ssid, IEEE80211_NWID_LEN, idp, idlen)
3771 			  , ssid
3772 			, ether_ntoa((const struct ether_addr *) sr->isr_bssid)
3773 			, ieee80211_mhz2ieee(sr->isr_freq, sr->isr_flags)
3774 			, getmaxrate(sr->isr_rates, sr->isr_nrates)
3775 			, (sr->isr_rssi/2)+sr->isr_noise, sr->isr_noise
3776 			, sr->isr_intval
3777 			, getcaps(sr->isr_capinfo)
3778 		);
3779 		printies(ctx, vp + sr->isr_ssid_len + sr->isr_meshid_len,
3780 		    sr->isr_ie_len, 24);
3781 		printbssidname((const struct ether_addr *)sr->isr_bssid);
3782 		printf("\n");
3783 		cp += sr->isr_len, len -= sr->isr_len;
3784 	} while (len >= (int)sizeof(struct ieee80211req_scan_result));
3785 }
3786 
3787 static void
3788 scan_and_wait(if_ctx *ctx)
3789 {
3790 	struct ieee80211_scan_req sr;
3791 	struct ieee80211req ireq;
3792 	int sroute;
3793 
3794 	sroute = socket(PF_ROUTE, SOCK_RAW, 0);
3795 	if (sroute < 0) {
3796 		perror("socket(PF_ROUTE,SOCK_RAW)");
3797 		return;
3798 	}
3799 	memset(&ireq, 0, sizeof(ireq));
3800 	strlcpy(ireq.i_name, ctx->ifname, sizeof(ireq.i_name));
3801 	ireq.i_type = IEEE80211_IOC_SCAN_REQ;
3802 
3803 	memset(&sr, 0, sizeof(sr));
3804 	sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE
3805 		    | IEEE80211_IOC_SCAN_BGSCAN
3806 		    | IEEE80211_IOC_SCAN_NOPICK
3807 		    | IEEE80211_IOC_SCAN_ONCE;
3808 	sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
3809 	sr.sr_nssid = 0;
3810 
3811 	ireq.i_data = &sr;
3812 	ireq.i_len = sizeof(sr);
3813 	/*
3814 	 * NB: only root can trigger a scan so ignore errors. Also ignore
3815 	 * possible errors from net80211, even if no new scan could be
3816 	 * started there might still be a valid scan cache.
3817 	 */
3818 	if (ioctl_ctx(ctx, SIOCS80211, &ireq) == 0) {
3819 		char buf[2048];
3820 		struct if_announcemsghdr *ifan;
3821 		struct rt_msghdr *rtm;
3822 
3823 		do {
3824 			if (read(sroute, buf, sizeof(buf)) < 0) {
3825 				perror("read(PF_ROUTE)");
3826 				break;
3827 			}
3828 			rtm = (struct rt_msghdr *)(void *)buf;
3829 			if (rtm->rtm_version != RTM_VERSION)
3830 				break;
3831 			ifan = (struct if_announcemsghdr *) rtm;
3832 		} while (rtm->rtm_type != RTM_IEEE80211 ||
3833 		    ifan->ifan_what != RTM_IEEE80211_SCAN);
3834 	}
3835 	close(sroute);
3836 }
3837 
3838 static void
3839 set80211scan(if_ctx *ctx, const char *val __unused, int dummy __unused)
3840 {
3841 	scan_and_wait(ctx);
3842 	list_scan(ctx);
3843 }
3844 
3845 static enum ieee80211_opmode get80211opmode(if_ctx *ctx);
3846 
3847 static int
3848 gettxseq(const struct ieee80211req_sta_info *si)
3849 {
3850 	int i, txseq;
3851 
3852 	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3853 		return si->isi_txseqs[0];
3854 	/* XXX not right but usually what folks want */
3855 	txseq = 0;
3856 	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3857 		if (si->isi_txseqs[i] > txseq)
3858 			txseq = si->isi_txseqs[i];
3859 	return txseq;
3860 }
3861 
3862 static int
3863 getrxseq(const struct ieee80211req_sta_info *si)
3864 {
3865 	int rxseq;
3866 
3867 	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3868 		return si->isi_rxseqs[0];
3869 	/* XXX not right but usually what folks want */
3870 	rxseq = 0;
3871 	for (unsigned int i = 0; i < IEEE80211_TID_SIZE; i++)
3872 		if (si->isi_rxseqs[i] > rxseq)
3873 			rxseq = si->isi_rxseqs[i];
3874 	return rxseq;
3875 }
3876 
3877 static void
3878 list_stations(if_ctx *ctx)
3879 {
3880 	union {
3881 		struct ieee80211req_sta_req req;
3882 		uint8_t buf[24*1024];
3883 	} u;
3884 	enum ieee80211_opmode opmode = get80211opmode(ctx);
3885 	const uint8_t *cp;
3886 	int len;
3887 
3888 	/* broadcast address =>'s get all stations */
3889 	(void) memset(u.req.is_u.macaddr, 0xff, IEEE80211_ADDR_LEN);
3890 	if (opmode == IEEE80211_M_STA) {
3891 		/*
3892 		 * Get information about the associated AP.
3893 		 */
3894 		(void) get80211(ctx, IEEE80211_IOC_BSSID,
3895 		    u.req.is_u.macaddr, IEEE80211_ADDR_LEN);
3896 	}
3897 	if (get80211len(ctx, IEEE80211_IOC_STA_INFO, &u, sizeof(u), &len) < 0)
3898 		errx(1, "unable to get station information");
3899 	if (len < (int)sizeof(struct ieee80211req_sta_info))
3900 		return;
3901 
3902 	getchaninfo(ctx);
3903 
3904 	if (opmode == IEEE80211_M_MBSS)
3905 		printf("%-17.17s %4s %5s %5s %7s %4s %4s %4s %6s %6s\n"
3906 			, "ADDR"
3907 			, "CHAN"
3908 			, "LOCAL"
3909 			, "PEER"
3910 			, "STATE"
3911 			, "RATE"
3912 			, "RSSI"
3913 			, "IDLE"
3914 			, "TXSEQ"
3915 			, "RXSEQ"
3916 		);
3917 	else
3918 		printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %-12s\n"
3919 			, "ADDR"
3920 			, "AID"
3921 			, "CHAN"
3922 			, "RATE"
3923 			, "RSSI"
3924 			, "IDLE"
3925 			, "TXSEQ"
3926 			, "RXSEQ"
3927 			, "CAPS"
3928 			, "FLAG"
3929 		);
3930 	cp = (const uint8_t *) u.req.info;
3931 	do {
3932 		const struct ieee80211req_sta_info *si;
3933 
3934 		si = (const struct ieee80211req_sta_info *)(const void *)cp;
3935 		if (si->isi_len < sizeof(*si))
3936 			break;
3937 		if (opmode == IEEE80211_M_MBSS)
3938 			printf("%s %4d %5x %5x %7.7s %3dM %4.1f %4d %6d %6d"
3939 				, ether_ntoa((const struct ether_addr*)
3940 				    si->isi_macaddr)
3941 				, ieee80211_mhz2ieee(si->isi_freq,
3942 				    si->isi_flags)
3943 				, si->isi_localid
3944 				, si->isi_peerid
3945 				, mesh_linkstate_string(si->isi_peerstate)
3946 				, si->isi_txmbps/2
3947 				, si->isi_rssi/2.
3948 				, si->isi_inact
3949 				, gettxseq(si)
3950 				, getrxseq(si)
3951 			);
3952 		else
3953 			printf("%s %4u %4d %3dM %4.1f %4d %6d %6d %-4.4s %-12.12s"
3954 				, ether_ntoa((const struct ether_addr*)
3955 				    si->isi_macaddr)
3956 				, IEEE80211_AID(si->isi_associd)
3957 				, ieee80211_mhz2ieee(si->isi_freq,
3958 				    si->isi_flags)
3959 				, si->isi_txmbps/2
3960 				, si->isi_rssi/2.
3961 				, si->isi_inact
3962 				, gettxseq(si)
3963 				, getrxseq(si)
3964 				, getcaps(si->isi_capinfo)
3965 				, getflags(si->isi_state)
3966 			);
3967 		printies(ctx, cp + si->isi_ie_off, si->isi_ie_len, 24);
3968 		printmimo(&si->isi_mimo);
3969 		printf("\n");
3970 		cp += si->isi_len, len -= si->isi_len;
3971 	} while (len >= (int)sizeof(struct ieee80211req_sta_info));
3972 }
3973 
3974 static const char *
3975 mesh_linkstate_string(uint8_t state)
3976 {
3977 	static const char *state_names[] = {
3978 	    [0] = "IDLE",
3979 	    [1] = "OPEN-TX",
3980 	    [2] = "OPEN-RX",
3981 	    [3] = "CONF-RX",
3982 	    [4] = "ESTAB",
3983 	    [5] = "HOLDING",
3984 	};
3985 
3986 	if (state >= nitems(state_names)) {
3987 		static char buf[10];
3988 		snprintf(buf, sizeof(buf), "#%u", state);
3989 		return buf;
3990 	} else
3991 		return state_names[state];
3992 }
3993 
3994 static const char *
3995 get_chaninfo(const struct ieee80211_channel *c, int precise,
3996 	char buf[], size_t bsize)
3997 {
3998 	buf[0] = '\0';
3999 	if (IEEE80211_IS_CHAN_FHSS(c))
4000 		strlcat(buf, " FHSS", bsize);
4001 	if (IEEE80211_IS_CHAN_A(c))
4002 		strlcat(buf, " 11a", bsize);
4003 	else if (IEEE80211_IS_CHAN_ANYG(c))
4004 		strlcat(buf, " 11g", bsize);
4005 	else if (IEEE80211_IS_CHAN_B(c))
4006 		strlcat(buf, " 11b", bsize);
4007 	if (IEEE80211_IS_CHAN_HALF(c))
4008 		strlcat(buf, "/10MHz", bsize);
4009 	if (IEEE80211_IS_CHAN_QUARTER(c))
4010 		strlcat(buf, "/5MHz", bsize);
4011 	if (IEEE80211_IS_CHAN_TURBO(c))
4012 		strlcat(buf, " Turbo", bsize);
4013 	if (precise) {
4014 		if (IEEE80211_IS_CHAN_VHT80P80(c))
4015 			strlcat(buf, " vht/80p80", bsize);
4016 		else if (IEEE80211_IS_CHAN_VHT160(c))
4017 			strlcat(buf, " vht/160", bsize);
4018 		else if (IEEE80211_IS_CHAN_VHT80(c) &&
4019 		    IEEE80211_IS_CHAN_HT40D(c))
4020 			strlcat(buf, " vht/80-", bsize);
4021 		else if (IEEE80211_IS_CHAN_VHT80(c) &&
4022 		    IEEE80211_IS_CHAN_HT40U(c))
4023 			strlcat(buf, " vht/80+", bsize);
4024 		else if (IEEE80211_IS_CHAN_VHT80(c))
4025 			strlcat(buf, " vht/80", bsize);
4026 		else if (IEEE80211_IS_CHAN_VHT40D(c))
4027 			strlcat(buf, " vht/40-", bsize);
4028 		else if (IEEE80211_IS_CHAN_VHT40U(c))
4029 			strlcat(buf, " vht/40+", bsize);
4030 		else if (IEEE80211_IS_CHAN_VHT20(c))
4031 			strlcat(buf, " vht/20", bsize);
4032 		else if (IEEE80211_IS_CHAN_HT20(c))
4033 			strlcat(buf, " ht/20", bsize);
4034 		else if (IEEE80211_IS_CHAN_HT40D(c))
4035 			strlcat(buf, " ht/40-", bsize);
4036 		else if (IEEE80211_IS_CHAN_HT40U(c))
4037 			strlcat(buf, " ht/40+", bsize);
4038 	} else {
4039 		if (IEEE80211_IS_CHAN_VHT(c))
4040 			strlcat(buf, " vht", bsize);
4041 		else if (IEEE80211_IS_CHAN_HT(c))
4042 			strlcat(buf, " ht", bsize);
4043 	}
4044 	return buf;
4045 }
4046 
4047 static void
4048 print_chaninfo(const struct ieee80211_channel *c, int verb)
4049 {
4050 	char buf[14];
4051 
4052 	if (verb)
4053 		printf("Channel %3u : %u%c%c%c%c%c MHz%-14.14s",
4054 		    ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
4055 		    IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
4056 		    IEEE80211_IS_CHAN_DFS(c) ? 'D' : ' ',
4057 		    IEEE80211_IS_CHAN_RADAR(c) ? 'R' : ' ',
4058 		    IEEE80211_IS_CHAN_CWINT(c) ? 'I' : ' ',
4059 		    IEEE80211_IS_CHAN_CACDONE(c) ? 'C' : ' ',
4060 		    get_chaninfo(c, verb, buf, sizeof(buf)));
4061 	else
4062 	printf("Channel %3u : %u%c MHz%-14.14s",
4063 	    ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
4064 	    IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
4065 	    get_chaninfo(c, verb, buf, sizeof(buf)));
4066 
4067 }
4068 
4069 static int
4070 chanpref(const struct ieee80211_channel *c)
4071 {
4072 
4073 	if (IEEE80211_IS_CHAN_VHT80P80(c))
4074 		return 90;
4075 	if (IEEE80211_IS_CHAN_VHT160(c))
4076 		return 80;
4077 	if (IEEE80211_IS_CHAN_VHT80(c))
4078 		return 70;
4079 	if (IEEE80211_IS_CHAN_VHT40(c))
4080 		return 60;
4081 	if (IEEE80211_IS_CHAN_VHT20(c))
4082 		return 50;
4083 	if (IEEE80211_IS_CHAN_HT40(c))
4084 		return 40;
4085 	if (IEEE80211_IS_CHAN_HT20(c))
4086 		return 30;
4087 	if (IEEE80211_IS_CHAN_HALF(c))
4088 		return 10;
4089 	if (IEEE80211_IS_CHAN_QUARTER(c))
4090 		return 5;
4091 	if (IEEE80211_IS_CHAN_TURBO(c))
4092 		return 25;
4093 	if (IEEE80211_IS_CHAN_A(c))
4094 		return 20;
4095 	if (IEEE80211_IS_CHAN_G(c))
4096 		return 20;
4097 	if (IEEE80211_IS_CHAN_B(c))
4098 		return 15;
4099 	if (IEEE80211_IS_CHAN_PUREG(c))
4100 		return 15;
4101 	return 0;
4102 }
4103 
4104 static void
4105 print_channels(if_ctx *ctx, const struct ieee80211req_chaninfo *chans,
4106 	int allchans, int verb)
4107 {
4108 	struct ieee80211req_chaninfo *achans;
4109 	uint8_t reported[IEEE80211_CHAN_BYTES];
4110 	const struct ieee80211_channel *c;
4111 	unsigned int i, half;
4112 
4113 	achans = malloc(IEEE80211_CHANINFO_SPACE(chans));
4114 	if (achans == NULL)
4115 		errx(1, "no space for active channel list");
4116 	achans->ic_nchans = 0;
4117 	memset(reported, 0, sizeof(reported));
4118 	if (!allchans) {
4119 		struct ieee80211req_chanlist active;
4120 
4121 		if (get80211(ctx, IEEE80211_IOC_CHANLIST, &active, sizeof(active)) < 0)
4122 			errx(1, "unable to get active channel list");
4123 		for (i = 0; i < chans->ic_nchans; i++) {
4124 			c = &chans->ic_chans[i];
4125 			if (!isset(active.ic_channels, c->ic_ieee))
4126 				continue;
4127 			/*
4128 			 * Suppress compatible duplicates unless
4129 			 * verbose.  The kernel gives us it's
4130 			 * complete channel list which has separate
4131 			 * entries for 11g/11b and 11a/turbo.
4132 			 */
4133 			if (isset(reported, c->ic_ieee) && !verb) {
4134 				/* XXX we assume duplicates are adjacent */
4135 				achans->ic_chans[achans->ic_nchans-1] = *c;
4136 			} else {
4137 				achans->ic_chans[achans->ic_nchans++] = *c;
4138 				setbit(reported, c->ic_ieee);
4139 			}
4140 		}
4141 	} else {
4142 		for (i = 0; i < chans->ic_nchans; i++) {
4143 			c = &chans->ic_chans[i];
4144 			/* suppress duplicates as above */
4145 			if (isset(reported, c->ic_ieee) && !verb) {
4146 				/* XXX we assume duplicates are adjacent */
4147 				struct ieee80211_channel *a =
4148 				    &achans->ic_chans[achans->ic_nchans-1];
4149 				if (chanpref(c) > chanpref(a))
4150 					*a = *c;
4151 			} else {
4152 				achans->ic_chans[achans->ic_nchans++] = *c;
4153 				setbit(reported, c->ic_ieee);
4154 			}
4155 		}
4156 	}
4157 	half = achans->ic_nchans / 2;
4158 	if (achans->ic_nchans % 2)
4159 		half++;
4160 
4161 	for (i = 0; i < achans->ic_nchans / 2; i++) {
4162 		print_chaninfo(&achans->ic_chans[i], verb);
4163 		print_chaninfo(&achans->ic_chans[half+i], verb);
4164 		printf("\n");
4165 	}
4166 	if (achans->ic_nchans % 2) {
4167 		print_chaninfo(&achans->ic_chans[i], verb);
4168 		printf("\n");
4169 	}
4170 	free(achans);
4171 }
4172 
4173 static void
4174 list_channels(if_ctx *ctx, int allchans)
4175 {
4176 	getchaninfo(ctx);
4177 	print_channels(ctx, chaninfo, allchans, ctx->args->verbose);
4178 }
4179 
4180 static void
4181 print_txpow(const struct ieee80211_channel *c)
4182 {
4183 	printf("Channel %3u : %u MHz %3.1f reg %2d  ",
4184 	    c->ic_ieee, c->ic_freq,
4185 	    c->ic_maxpower/2., c->ic_maxregpower);
4186 }
4187 
4188 static void
4189 print_txpow_verbose(const struct ieee80211_channel *c)
4190 {
4191 	print_chaninfo(c, 1);
4192 	printf("min %4.1f dBm  max %3.1f dBm  reg %2d dBm",
4193 	    c->ic_minpower/2., c->ic_maxpower/2., c->ic_maxregpower);
4194 	/* indicate where regulatory cap limits power use */
4195 	if (c->ic_maxpower > 2*c->ic_maxregpower)
4196 		printf(" <");
4197 }
4198 
4199 static void
4200 list_txpow(if_ctx *ctx)
4201 {
4202 	struct ieee80211req_chaninfo *achans;
4203 	uint8_t reported[IEEE80211_CHAN_BYTES];
4204 	struct ieee80211_channel *c, *prev;
4205 	unsigned int i, half;
4206 
4207 	getchaninfo(ctx);
4208 	achans = malloc(IEEE80211_CHANINFO_SPACE(chaninfo));
4209 	if (achans == NULL)
4210 		errx(1, "no space for active channel list");
4211 	achans->ic_nchans = 0;
4212 	memset(reported, 0, sizeof(reported));
4213 	for (i = 0; i < chaninfo->ic_nchans; i++) {
4214 		c = &chaninfo->ic_chans[i];
4215 		/* suppress duplicates as above */
4216 		if (isset(reported, c->ic_ieee) && !ctx->args->verbose) {
4217 			/* XXX we assume duplicates are adjacent */
4218 			assert(achans->ic_nchans > 0);
4219 			prev = &achans->ic_chans[achans->ic_nchans-1];
4220 			/* display highest power on channel */
4221 			if (c->ic_maxpower > prev->ic_maxpower)
4222 				*prev = *c;
4223 		} else {
4224 			achans->ic_chans[achans->ic_nchans++] = *c;
4225 			setbit(reported, c->ic_ieee);
4226 		}
4227 	}
4228 	if (!ctx->args->verbose) {
4229 		half = achans->ic_nchans / 2;
4230 		if (achans->ic_nchans % 2)
4231 			half++;
4232 
4233 		for (i = 0; i < achans->ic_nchans / 2; i++) {
4234 			print_txpow(&achans->ic_chans[i]);
4235 			print_txpow(&achans->ic_chans[half+i]);
4236 			printf("\n");
4237 		}
4238 		if (achans->ic_nchans % 2) {
4239 			print_txpow(&achans->ic_chans[i]);
4240 			printf("\n");
4241 		}
4242 	} else {
4243 		for (i = 0; i < achans->ic_nchans; i++) {
4244 			print_txpow_verbose(&achans->ic_chans[i]);
4245 			printf("\n");
4246 		}
4247 	}
4248 	free(achans);
4249 }
4250 
4251 static void
4252 list_keys(int s __unused)
4253 {
4254 }
4255 
4256 static void
4257 list_capabilities(if_ctx *ctx)
4258 {
4259 	struct ieee80211_devcaps_req *dc;
4260 	const int verbose = ctx->args->verbose;
4261 
4262 	if (verbose)
4263 		dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
4264 	else
4265 		dc = malloc(IEEE80211_DEVCAPS_SIZE(1));
4266 	if (dc == NULL)
4267 		errx(1, "no space for device capabilities");
4268 	dc->dc_chaninfo.ic_nchans = verbose ? MAXCHAN : 1;
4269 	getdevcaps(ctx, dc);
4270 	printb("drivercaps", dc->dc_drivercaps, IEEE80211_C_BITS);
4271 	if (dc->dc_cryptocaps != 0 || verbose) {
4272 		putchar('\n');
4273 		printb("cryptocaps", dc->dc_cryptocaps, IEEE80211_CRYPTO_BITS);
4274 	}
4275 	if (dc->dc_htcaps != 0 || verbose) {
4276 		putchar('\n');
4277 		printb("htcaps", dc->dc_htcaps, IEEE80211_HTCAP_BITS);
4278 	}
4279 	if (dc->dc_vhtcaps != 0 || verbose) {
4280 		putchar('\n');
4281 		printb("vhtcaps", dc->dc_vhtcaps, IEEE80211_VHTCAP_BITS);
4282 	}
4283 
4284 	putchar('\n');
4285 	if (verbose) {
4286 		chaninfo = &dc->dc_chaninfo;	/* XXX */
4287 		print_channels(ctx, &dc->dc_chaninfo, 1/*allchans*/, verbose);
4288 	}
4289 	free(dc);
4290 }
4291 
4292 static int
4293 get80211wme(if_ctx *ctx, int param, int ac, int *val)
4294 {
4295 	struct ieee80211req ireq = {};
4296 
4297 	strlcpy(ireq.i_name, ctx->ifname, sizeof(ireq.i_name));
4298 	ireq.i_type = param;
4299 	ireq.i_len = ac;
4300 	if (ioctl_ctx(ctx, SIOCG80211, &ireq) < 0) {
4301 		warn("cannot get WME parameter %d, ac %d%s",
4302 		    param, ac & IEEE80211_WMEPARAM_VAL,
4303 		    ac & IEEE80211_WMEPARAM_BSS ? " (BSS)" : "");
4304 		return -1;
4305 	}
4306 	*val = ireq.i_val;
4307 	return 0;
4308 }
4309 
4310 static void
4311 list_wme_aci(if_ctx *ctx, const char *tag, int ac)
4312 {
4313 	int val;
4314 
4315 	printf("\t%s", tag);
4316 
4317 	/* show WME BSS parameters */
4318 	if (get80211wme(ctx, IEEE80211_IOC_WME_CWMIN, ac, &val) != -1)
4319 		printf(" cwmin %2u", val);
4320 	if (get80211wme(ctx, IEEE80211_IOC_WME_CWMAX, ac, &val) != -1)
4321 		printf(" cwmax %2u", val);
4322 	if (get80211wme(ctx, IEEE80211_IOC_WME_AIFS, ac, &val) != -1)
4323 		printf(" aifs %2u", val);
4324 	if (get80211wme(ctx, IEEE80211_IOC_WME_TXOPLIMIT, ac, &val) != -1)
4325 		printf(" txopLimit %3u", val);
4326 	if (get80211wme(ctx, IEEE80211_IOC_WME_ACM, ac, &val) != -1) {
4327 		if (val)
4328 			printf(" acm");
4329 		else if (ctx->args->verbose)
4330 			printf(" -acm");
4331 	}
4332 	/* !BSS only */
4333 	if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
4334 		if (get80211wme(ctx, IEEE80211_IOC_WME_ACKPOLICY, ac, &val) != -1) {
4335 			if (!val)
4336 				printf(" -ack");
4337 			else if (ctx->args->verbose)
4338 				printf(" ack");
4339 		}
4340 	}
4341 	printf("\n");
4342 }
4343 
4344 static void
4345 list_wme(if_ctx *ctx)
4346 {
4347 	static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
4348 	int ac;
4349 
4350 	if (ctx->args->verbose) {
4351 		/* display both BSS and local settings */
4352 		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
4353 	again:
4354 			if (ac & IEEE80211_WMEPARAM_BSS)
4355 				list_wme_aci(ctx, "     ", ac);
4356 			else
4357 				list_wme_aci(ctx, acnames[ac], ac);
4358 			if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
4359 				ac |= IEEE80211_WMEPARAM_BSS;
4360 				goto again;
4361 			} else
4362 				ac &= ~IEEE80211_WMEPARAM_BSS;
4363 		}
4364 	} else {
4365 		/* display only channel settings */
4366 		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++)
4367 			list_wme_aci(ctx, acnames[ac], ac);
4368 	}
4369 }
4370 
4371 static void
4372 list_roam(if_ctx *ctx)
4373 {
4374 	const struct ieee80211_roamparam *rp;
4375 	int mode;
4376 
4377 	getroam(ctx);
4378 	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
4379 		rp = &roamparams.params[mode];
4380 		if (rp->rssi == 0 && rp->rate == 0)
4381 			continue;
4382 		if (mode == IEEE80211_MODE_11NA ||
4383 		    mode == IEEE80211_MODE_11NG ||
4384 		    mode == IEEE80211_MODE_VHT_2GHZ ||
4385 		    mode == IEEE80211_MODE_VHT_5GHZ) {
4386 			if (rp->rssi & 1)
4387 				LINE_CHECK("roam:%-7.7s rssi %2u.5dBm  MCS %2u    ",
4388 				    modename[mode], rp->rssi/2,
4389 				    rp->rate &~ IEEE80211_RATE_MCS);
4390 			else
4391 				LINE_CHECK("roam:%-7.7s rssi %4udBm  MCS %2u    ",
4392 				    modename[mode], rp->rssi/2,
4393 				    rp->rate &~ IEEE80211_RATE_MCS);
4394 		} else {
4395 			if (rp->rssi & 1)
4396 				LINE_CHECK("roam:%-7.7s rssi %2u.5dBm rate %2u Mb/s",
4397 				    modename[mode], rp->rssi/2, rp->rate/2);
4398 			else
4399 				LINE_CHECK("roam:%-7.7s rssi %4udBm rate %2u Mb/s",
4400 				    modename[mode], rp->rssi/2, rp->rate/2);
4401 		}
4402 	}
4403 }
4404 
4405 /* XXX TODO: rate-to-string method... */
4406 static const char*
4407 get_mcs_mbs_rate_str(uint8_t rate)
4408 {
4409 	return (rate & IEEE80211_RATE_MCS) ? "MCS " : "Mb/s";
4410 }
4411 
4412 static uint8_t
4413 get_rate_value(uint8_t rate)
4414 {
4415 	if (rate & IEEE80211_RATE_MCS)
4416 		return (rate &~ IEEE80211_RATE_MCS);
4417 	return (rate / 2);
4418 }
4419 
4420 static void
4421 list_txparams(if_ctx *ctx)
4422 {
4423 	const struct ieee80211_txparam *tp;
4424 	int mode;
4425 
4426 	gettxparams(ctx);
4427 	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
4428 		tp = &txparams.params[mode];
4429 		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
4430 			continue;
4431 		if (mode == IEEE80211_MODE_11NA ||
4432 		    mode == IEEE80211_MODE_11NG ||
4433 		    mode == IEEE80211_MODE_VHT_2GHZ ||
4434 		    mode == IEEE80211_MODE_VHT_5GHZ) {
4435 			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
4436 				LINE_CHECK("%-7.7s ucast NONE    mgmt %2u %s "
4437 				    "mcast %2u %s maxretry %u",
4438 				    modename[mode],
4439 				    get_rate_value(tp->mgmtrate),
4440 				    get_mcs_mbs_rate_str(tp->mgmtrate),
4441 				    get_rate_value(tp->mcastrate),
4442 				    get_mcs_mbs_rate_str(tp->mcastrate),
4443 				    tp->maxretry);
4444 			else
4445 				LINE_CHECK("%-7.7s ucast %2u MCS  mgmt %2u %s "
4446 				    "mcast %2u %s maxretry %u",
4447 				    modename[mode],
4448 				    tp->ucastrate &~ IEEE80211_RATE_MCS,
4449 				    get_rate_value(tp->mgmtrate),
4450 				    get_mcs_mbs_rate_str(tp->mgmtrate),
4451 				    get_rate_value(tp->mcastrate),
4452 				    get_mcs_mbs_rate_str(tp->mcastrate),
4453 				    tp->maxretry);
4454 		} else {
4455 			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
4456 				LINE_CHECK("%-7.7s ucast NONE    mgmt %2u Mb/s "
4457 				    "mcast %2u Mb/s maxretry %u",
4458 				    modename[mode],
4459 				    tp->mgmtrate/2,
4460 				    tp->mcastrate/2, tp->maxretry);
4461 			else
4462 				LINE_CHECK("%-7.7s ucast %2u Mb/s mgmt %2u Mb/s "
4463 				    "mcast %2u Mb/s maxretry %u",
4464 				    modename[mode],
4465 				    tp->ucastrate/2, tp->mgmtrate/2,
4466 				    tp->mcastrate/2, tp->maxretry);
4467 		}
4468 	}
4469 }
4470 
4471 static void
4472 printpolicy(int policy)
4473 {
4474 	switch (policy) {
4475 	case IEEE80211_MACCMD_POLICY_OPEN:
4476 		printf("policy: open\n");
4477 		break;
4478 	case IEEE80211_MACCMD_POLICY_ALLOW:
4479 		printf("policy: allow\n");
4480 		break;
4481 	case IEEE80211_MACCMD_POLICY_DENY:
4482 		printf("policy: deny\n");
4483 		break;
4484 	case IEEE80211_MACCMD_POLICY_RADIUS:
4485 		printf("policy: radius\n");
4486 		break;
4487 	default:
4488 		printf("policy: unknown (%u)\n", policy);
4489 		break;
4490 	}
4491 }
4492 
4493 static void
4494 list_mac(if_ctx *ctx)
4495 {
4496 	struct ieee80211req ireq = {};
4497 	struct ieee80211req_maclist *acllist;
4498 	int i, nacls, policy, len;
4499 	uint8_t *data;
4500 	char c;
4501 
4502 	strlcpy(ireq.i_name, ctx->ifname, sizeof(ireq.i_name)); /* XXX ?? */
4503 	ireq.i_type = IEEE80211_IOC_MACCMD;
4504 	ireq.i_val = IEEE80211_MACCMD_POLICY;
4505 	if (ioctl_ctx(ctx, SIOCG80211, &ireq) < 0) {
4506 		if (errno == EINVAL) {
4507 			printf("No acl policy loaded\n");
4508 			return;
4509 		}
4510 		err(1, "unable to get mac policy");
4511 	}
4512 	policy = ireq.i_val;
4513 	if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
4514 		c = '*';
4515 	} else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
4516 		c = '+';
4517 	} else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
4518 		c = '-';
4519 	} else if (policy == IEEE80211_MACCMD_POLICY_RADIUS) {
4520 		c = 'r';		/* NB: should never have entries */
4521 	} else {
4522 		printf("policy: unknown (%u)\n", policy);
4523 		c = '?';
4524 	}
4525 	if (ctx->args->verbose || c == '?')
4526 		printpolicy(policy);
4527 
4528 	ireq.i_val = IEEE80211_MACCMD_LIST;
4529 	ireq.i_len = 0;
4530 	if (ioctl_ctx(ctx, SIOCG80211, &ireq) < 0)
4531 		err(1, "unable to get mac acl list size");
4532 	if (ireq.i_len == 0) {		/* NB: no acls */
4533 		if (!(ctx->args->verbose || c == '?'))
4534 			printpolicy(policy);
4535 		return;
4536 	}
4537 	len = ireq.i_len;
4538 
4539 	data = malloc(len);
4540 	if (data == NULL)
4541 		err(1, "out of memory for acl list");
4542 
4543 	ireq.i_data = data;
4544 	if (ioctl_ctx(ctx, SIOCG80211, &ireq) < 0)
4545 		err(1, "unable to get mac acl list");
4546 	nacls = len / sizeof(*acllist);
4547 	acllist = (struct ieee80211req_maclist *) data;
4548 	for (i = 0; i < nacls; i++)
4549 		printf("%c%s\n", c, ether_ntoa(
4550 			(const struct ether_addr *) acllist[i].ml_macaddr));
4551 	free(data);
4552 }
4553 
4554 static void
4555 print_regdomain(const struct ieee80211_regdomain *reg, int verb)
4556 {
4557 	if ((reg->regdomain != 0 &&
4558 	    reg->regdomain != reg->country) || verb) {
4559 		const struct regdomain *rd =
4560 		    lib80211_regdomain_findbysku(getregdata(), reg->regdomain);
4561 		if (rd == NULL)
4562 			LINE_CHECK("regdomain %d", reg->regdomain);
4563 		else
4564 			LINE_CHECK("regdomain %s", rd->name);
4565 	}
4566 	if (reg->country != 0 || verb) {
4567 		const struct country *cc =
4568 		    lib80211_country_findbycc(getregdata(), reg->country);
4569 		if (cc == NULL)
4570 			LINE_CHECK("country %d", reg->country);
4571 		else
4572 			LINE_CHECK("country %s", cc->isoname);
4573 	}
4574 	if (reg->location == 'I')
4575 		LINE_CHECK("indoor");
4576 	else if (reg->location == 'O')
4577 		LINE_CHECK("outdoor");
4578 	else if (verb)
4579 		LINE_CHECK("anywhere");
4580 	if (reg->ecm)
4581 		LINE_CHECK("ecm");
4582 	else if (verb)
4583 		LINE_CHECK("-ecm");
4584 }
4585 
4586 static void
4587 list_regdomain(if_ctx *ctx, int channelsalso)
4588 {
4589 	getregdomain(ctx);
4590 	if (channelsalso) {
4591 		getchaninfo(ctx);
4592 		spacer = ':';
4593 		print_regdomain(&regdomain, 1);
4594 		LINE_BREAK();
4595 		print_channels(ctx, chaninfo, 1/*allchans*/, 1/*verbose*/);
4596 	} else
4597 		print_regdomain(&regdomain, ctx->args->verbose);
4598 }
4599 
4600 static void
4601 list_mesh(if_ctx *ctx)
4602 {
4603 	struct ieee80211req ireq = {};
4604 	struct ieee80211req_mesh_route routes[128];
4605 	struct ieee80211req_mesh_route *rt;
4606 
4607 	strlcpy(ireq.i_name, ctx->ifname, sizeof(ireq.i_name));
4608 	ireq.i_type = IEEE80211_IOC_MESH_RTCMD;
4609 	ireq.i_val = IEEE80211_MESH_RTCMD_LIST;
4610 	ireq.i_data = &routes;
4611 	ireq.i_len = sizeof(routes);
4612 	if (ioctl_ctx(ctx, SIOCG80211, &ireq) < 0)
4613 	 	err(1, "unable to get the Mesh routing table");
4614 
4615 	printf("%-17.17s %-17.17s %4s %4s %4s %6s %s\n"
4616 		, "DEST"
4617 		, "NEXT HOP"
4618 		, "HOPS"
4619 		, "METRIC"
4620 		, "LIFETIME"
4621 		, "MSEQ"
4622 		, "FLAGS");
4623 
4624 	for (unsigned int i = 0; i < ireq.i_len / sizeof(*rt); i++) {
4625 		rt = &routes[i];
4626 		printf("%s ",
4627 		    ether_ntoa((const struct ether_addr *)rt->imr_dest));
4628 		printf("%s %4u   %4u   %6u %6u    %c%c\n",
4629 			ether_ntoa((const struct ether_addr *)rt->imr_nexthop),
4630 			rt->imr_nhops, rt->imr_metric, rt->imr_lifetime,
4631 			rt->imr_lastmseq,
4632 			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_DISCOVER) ?
4633 			    'D' :
4634 			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_VALID) ?
4635 			    'V' : '!',
4636 			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_PROXY) ?
4637 			    'P' :
4638 			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_GATE) ?
4639 			    'G' :' ');
4640 	}
4641 }
4642 
4643 static void
4644 set80211list(if_ctx *ctx, const char *arg, int dummy __unused)
4645 {
4646 	int s = ctx->io_s;
4647 #define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
4648 
4649 	LINE_INIT('\t');
4650 
4651 	if (iseq(arg, "sta"))
4652 		list_stations(ctx);
4653 	else if (iseq(arg, "scan") || iseq(arg, "ap"))
4654 		list_scan(ctx);
4655 	else if (iseq(arg, "chan") || iseq(arg, "freq"))
4656 		list_channels(ctx, 1);
4657 	else if (iseq(arg, "active"))
4658 		list_channels(ctx, 0);
4659 	else if (iseq(arg, "keys"))
4660 		list_keys(s);
4661 	else if (iseq(arg, "caps"))
4662 		list_capabilities(ctx);
4663 	else if (iseq(arg, "wme") || iseq(arg, "wmm"))
4664 		list_wme(ctx);
4665 	else if (iseq(arg, "mac"))
4666 		list_mac(ctx);
4667 	else if (iseq(arg, "txpow"))
4668 		list_txpow(ctx);
4669 	else if (iseq(arg, "roam"))
4670 		list_roam(ctx);
4671 	else if (iseq(arg, "txparam") || iseq(arg, "txparm"))
4672 		list_txparams(ctx);
4673 	else if (iseq(arg, "regdomain"))
4674 		list_regdomain(ctx, 1);
4675 	else if (iseq(arg, "countries"))
4676 		list_countries();
4677 	else if (iseq(arg, "mesh"))
4678 		list_mesh(ctx);
4679 	else
4680 		errx(1, "Don't know how to list %s for %s", arg, ctx->ifname);
4681 	LINE_BREAK();
4682 #undef iseq
4683 }
4684 
4685 static enum ieee80211_opmode
4686 get80211opmode(if_ctx *ctx)
4687 {
4688 	struct ifmediareq ifmr = {};
4689 
4690 	strlcpy(ifmr.ifm_name, ctx->ifname, sizeof(ifmr.ifm_name));
4691 
4692 	if (ioctl_ctx(ctx, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
4693 		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) {
4694 			if (ifmr.ifm_current & IFM_FLAG0)
4695 				return IEEE80211_M_AHDEMO;
4696 			else
4697 				return IEEE80211_M_IBSS;
4698 		}
4699 		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
4700 			return IEEE80211_M_HOSTAP;
4701 		if (ifmr.ifm_current & IFM_IEEE80211_IBSS)
4702 			return IEEE80211_M_IBSS;
4703 		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
4704 			return IEEE80211_M_MONITOR;
4705 		if (ifmr.ifm_current & IFM_IEEE80211_MBSS)
4706 			return IEEE80211_M_MBSS;
4707 	}
4708 	return IEEE80211_M_STA;
4709 }
4710 
4711 #if 0
4712 static void
4713 printcipher(int s, struct ieee80211req *ireq, int keylenop)
4714 {
4715 	switch (ireq->i_val) {
4716 	case IEEE80211_CIPHER_WEP:
4717 		ireq->i_type = keylenop;
4718 		if (ioctl(s, SIOCG80211, ireq) != -1)
4719 			printf("WEP-%s",
4720 			    ireq->i_len <= 5 ? "40" :
4721 			    ireq->i_len <= 13 ? "104" : "128");
4722 		else
4723 			printf("WEP");
4724 		break;
4725 	case IEEE80211_CIPHER_TKIP:
4726 		printf("TKIP");
4727 		break;
4728 	case IEEE80211_CIPHER_AES_OCB:
4729 		printf("AES-OCB");
4730 		break;
4731 	case IEEE80211_CIPHER_AES_CCM:
4732 		printf("AES-CCM");
4733 		break;
4734 	case IEEE80211_CIPHER_CKIP:
4735 		printf("CKIP");
4736 		break;
4737 	case IEEE80211_CIPHER_NONE:
4738 		printf("NONE");
4739 		break;
4740 	default:
4741 		printf("UNKNOWN (0x%x)", ireq->i_val);
4742 		break;
4743 	}
4744 }
4745 #endif
4746 
4747 static void
4748 printkey(if_ctx *ctx, const struct ieee80211req_key *ik)
4749 {
4750 	static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
4751 	u_int keylen = ik->ik_keylen;
4752 	int printcontents;
4753 	const int verbose = ctx->args->verbose;
4754 	const bool printkeys = ctx->args->printkeys;
4755 
4756 	printcontents = printkeys &&
4757 		(memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
4758 	if (printcontents)
4759 		LINE_BREAK();
4760 	switch (ik->ik_type) {
4761 	case IEEE80211_CIPHER_WEP:
4762 		/* compatibility */
4763 		LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
4764 		    keylen <= 5 ? "40-bit" :
4765 		    keylen <= 13 ? "104-bit" : "128-bit");
4766 		break;
4767 	case IEEE80211_CIPHER_TKIP:
4768 		if (keylen > 128/8)
4769 			keylen -= 128/8;	/* ignore MIC for now */
4770 		LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4771 		break;
4772 	case IEEE80211_CIPHER_AES_OCB:
4773 		LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4774 		break;
4775 	case IEEE80211_CIPHER_AES_CCM:
4776 		LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4777 		break;
4778 	case IEEE80211_CIPHER_CKIP:
4779 		LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4780 		break;
4781 	case IEEE80211_CIPHER_NONE:
4782 		LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4783 		break;
4784 	default:
4785 		LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
4786 			ik->ik_type, ik->ik_keyix+1, 8*keylen);
4787 		break;
4788 	}
4789 	if (printcontents) {
4790 		u_int i;
4791 
4792 		printf(" <");
4793 		for (i = 0; i < keylen; i++)
4794 			printf("%02x", ik->ik_keydata[i]);
4795 		printf(">");
4796 		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4797 		    (ik->ik_keyrsc != 0 || verbose))
4798 			printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
4799 		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4800 		    (ik->ik_keytsc != 0 || verbose))
4801 			printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
4802 		if (ik->ik_flags != 0 && verbose) {
4803 			const char *sep = " ";
4804 
4805 			if (ik->ik_flags & IEEE80211_KEY_XMIT)
4806 				printf("%stx", sep), sep = "+";
4807 			if (ik->ik_flags & IEEE80211_KEY_RECV)
4808 				printf("%srx", sep), sep = "+";
4809 			if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
4810 				printf("%sdef", sep), sep = "+";
4811 		}
4812 		LINE_BREAK();
4813 	}
4814 }
4815 
4816 static void
4817 printrate(const char *tag, int v, int defrate, int defmcs)
4818 {
4819 	if ((v & IEEE80211_RATE_MCS) == 0) {
4820 		if (v != defrate) {
4821 			if (v & 1)
4822 				LINE_CHECK("%s %d.5", tag, v/2);
4823 			else
4824 				LINE_CHECK("%s %d", tag, v/2);
4825 		}
4826 	} else {
4827 		if (v != defmcs)
4828 			LINE_CHECK("%s %d", tag, v &~ 0x80);
4829 	}
4830 }
4831 
4832 static int
4833 getid(if_ctx *ctx, int ix, void *data, size_t len, int *plen, int mesh)
4834 {
4835 	struct ieee80211req ireq = {};
4836 
4837 	strlcpy(ireq.i_name, ctx->ifname, sizeof(ireq.i_name));
4838 	ireq.i_type = (!mesh) ? IEEE80211_IOC_SSID : IEEE80211_IOC_MESH_ID;
4839 	ireq.i_val = ix;
4840 	ireq.i_data = data;
4841 	ireq.i_len = len;
4842 	if (ioctl_ctx(ctx, SIOCG80211, &ireq) < 0)
4843 		return -1;
4844 	*plen = ireq.i_len;
4845 	return 0;
4846 }
4847 
4848 static int
4849 getdevicename(if_ctx *ctx, void *data, size_t len, int *plen)
4850 {
4851 	struct ieee80211req ireq = {};
4852 
4853 	strlcpy(ireq.i_name, ctx->ifname, sizeof(ireq.i_name));
4854 	ireq.i_type = IEEE80211_IOC_IC_NAME;
4855 	ireq.i_val = -1;
4856 	ireq.i_data = data;
4857 	ireq.i_len = len;
4858 	if (ioctl_ctx(ctx, SIOCG80211, &ireq) < 0)
4859 		return (-1);
4860 	*plen = ireq.i_len;
4861 	return (0);
4862 }
4863 
4864 static void
4865 ieee80211_status(if_ctx *ctx)
4866 {
4867 	int s = ctx->io_s;
4868 	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
4869 	enum ieee80211_opmode opmode = get80211opmode(ctx);
4870 	int i, num, wpa, wme, bgscan, bgscaninterval, val, len, wepmode;
4871 	uint8_t data[32];
4872 	const struct ieee80211_channel *c;
4873 	const struct ieee80211_roamparam *rp;
4874 	const struct ieee80211_txparam *tp;
4875 	const int verbose = ctx->args->verbose;
4876 
4877 	if (getid(ctx, -1, data, sizeof(data), &len, 0) < 0) {
4878 		/* If we can't get the SSID, this isn't an 802.11 device. */
4879 		return;
4880 	}
4881 
4882 	/*
4883 	 * Invalidate cached state so printing status for multiple
4884 	 * if's doesn't reuse the first interfaces' cached state.
4885 	 */
4886 	gotcurchan = 0;
4887 	gotroam = 0;
4888 	gottxparams = 0;
4889 	gothtconf = 0;
4890 	gotregdomain = 0;
4891 
4892 	printf("\t");
4893 	if (opmode == IEEE80211_M_MBSS) {
4894 		printf("meshid ");
4895 		getid(ctx, 0, data, sizeof(data), &len, 1);
4896 		print_string(data, len);
4897 	} else {
4898 		if (get80211val(ctx, IEEE80211_IOC_NUMSSIDS, &num) < 0)
4899 			num = 0;
4900 		printf("ssid ");
4901 		if (num > 1) {
4902 			for (i = 0; i < num; i++) {
4903 				if (getid(ctx, i, data, sizeof(data), &len, 0) >= 0 && len > 0) {
4904 					printf(" %d:", i + 1);
4905 					print_string(data, len);
4906 				}
4907 			}
4908 		} else
4909 			print_string(data, len);
4910 	}
4911 	c = getcurchan(ctx);
4912 	if (c->ic_freq != IEEE80211_CHAN_ANY) {
4913 		char buf[14];
4914 		printf(" channel %d (%u MHz%s)", c->ic_ieee, c->ic_freq,
4915 			get_chaninfo(c, 1, buf, sizeof(buf)));
4916 	} else if (verbose)
4917 		printf(" channel UNDEF");
4918 
4919 	if (get80211(ctx, IEEE80211_IOC_BSSID, data, IEEE80211_ADDR_LEN) >= 0 &&
4920 	    (memcmp(data, zerobssid, sizeof(zerobssid)) != 0 || verbose)) {
4921 		printf(" bssid %s", ether_ntoa((struct ether_addr *)data));
4922 		printbssidname((struct ether_addr *)data);
4923 	}
4924 
4925 	if (get80211len(ctx, IEEE80211_IOC_STATIONNAME, data, sizeof(data), &len) != -1) {
4926 		printf("\n\tstationname ");
4927 		print_string(data, len);
4928 	}
4929 
4930 	spacer = ' ';		/* force first break */
4931 	LINE_BREAK();
4932 
4933 	list_regdomain(ctx, 0);
4934 
4935 	wpa = 0;
4936 	if (get80211val(ctx, IEEE80211_IOC_AUTHMODE, &val) != -1) {
4937 		switch (val) {
4938 		case IEEE80211_AUTH_NONE:
4939 			LINE_CHECK("authmode NONE");
4940 			break;
4941 		case IEEE80211_AUTH_OPEN:
4942 			LINE_CHECK("authmode OPEN");
4943 			break;
4944 		case IEEE80211_AUTH_SHARED:
4945 			LINE_CHECK("authmode SHARED");
4946 			break;
4947 		case IEEE80211_AUTH_8021X:
4948 			LINE_CHECK("authmode 802.1x");
4949 			break;
4950 		case IEEE80211_AUTH_WPA:
4951 			if (get80211val(ctx, IEEE80211_IOC_WPA, &wpa) < 0)
4952 				wpa = 1;	/* default to WPA1 */
4953 			switch (wpa) {
4954 			case 2:
4955 				LINE_CHECK("authmode WPA2/802.11i");
4956 				break;
4957 			case 3:
4958 				LINE_CHECK("authmode WPA1+WPA2/802.11i");
4959 				break;
4960 			default:
4961 				LINE_CHECK("authmode WPA");
4962 				break;
4963 			}
4964 			break;
4965 		case IEEE80211_AUTH_AUTO:
4966 			LINE_CHECK("authmode AUTO");
4967 			break;
4968 		default:
4969 			LINE_CHECK("authmode UNKNOWN (0x%x)", val);
4970 			break;
4971 		}
4972 	}
4973 
4974 	if (wpa || verbose) {
4975 		if (get80211val(ctx, IEEE80211_IOC_WPS, &val) != -1) {
4976 			if (val)
4977 				LINE_CHECK("wps");
4978 			else if (verbose)
4979 				LINE_CHECK("-wps");
4980 		}
4981 		if (get80211val(ctx, IEEE80211_IOC_TSN, &val) != -1) {
4982 			if (val)
4983 				LINE_CHECK("tsn");
4984 			else if (verbose)
4985 				LINE_CHECK("-tsn");
4986 		}
4987 		if (ioctl(s, IEEE80211_IOC_COUNTERMEASURES, &val) != -1) {
4988 			if (val)
4989 				LINE_CHECK("countermeasures");
4990 			else if (verbose)
4991 				LINE_CHECK("-countermeasures");
4992 		}
4993 #if 0
4994 		/* XXX not interesting with WPA done in user space */
4995 		ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
4996 		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4997 		}
4998 
4999 		ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
5000 		if (ioctl(s, SIOCG80211, &ireq) != -1) {
5001 			LINE_CHECK("mcastcipher ");
5002 			printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
5003 			spacer = ' ';
5004 		}
5005 
5006 		ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
5007 		if (ioctl(s, SIOCG80211, &ireq) != -1) {
5008 			LINE_CHECK("ucastcipher ");
5009 			printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
5010 		}
5011 
5012 		if (wpa & 2) {
5013 			ireq.i_type = IEEE80211_IOC_RSNCAPS;
5014 			if (ioctl(s, SIOCG80211, &ireq) != -1) {
5015 				LINE_CHECK("RSN caps 0x%x", ireq.i_val);
5016 				spacer = ' ';
5017 			}
5018 		}
5019 
5020 		ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
5021 		if (ioctl(s, SIOCG80211, &ireq) != -1) {
5022 		}
5023 #endif
5024 	}
5025 
5026 	if (get80211val(ctx, IEEE80211_IOC_WEP, &wepmode) != -1 &&
5027 	    wepmode != IEEE80211_WEP_NOSUP) {
5028 
5029 		switch (wepmode) {
5030 		case IEEE80211_WEP_OFF:
5031 			LINE_CHECK("privacy OFF");
5032 			break;
5033 		case IEEE80211_WEP_ON:
5034 			LINE_CHECK("privacy ON");
5035 			break;
5036 		case IEEE80211_WEP_MIXED:
5037 			LINE_CHECK("privacy MIXED");
5038 			break;
5039 		default:
5040 			LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
5041 			break;
5042 		}
5043 
5044 		/*
5045 		 * If we get here then we've got WEP support so we need
5046 		 * to print WEP status.
5047 		 */
5048 
5049 		if (get80211val(ctx, IEEE80211_IOC_WEPTXKEY, &val) < 0) {
5050 			warn("WEP support, but no tx key!");
5051 			goto end;
5052 		}
5053 		if (val != -1)
5054 			LINE_CHECK("deftxkey %d", val+1);
5055 		else if (wepmode != IEEE80211_WEP_OFF || verbose)
5056 			LINE_CHECK("deftxkey UNDEF");
5057 
5058 		if (get80211val(ctx, IEEE80211_IOC_NUMWEPKEYS, &num) < 0) {
5059 			warn("WEP support, but no NUMWEPKEYS support!");
5060 			goto end;
5061 		}
5062 
5063 		for (i = 0; i < num; i++) {
5064 			struct ieee80211req_key ik;
5065 
5066 			memset(&ik, 0, sizeof(ik));
5067 			ik.ik_keyix = i;
5068 			if (get80211(ctx, IEEE80211_IOC_WPAKEY, &ik, sizeof(ik)) < 0) {
5069 				warn("WEP support, but can get keys!");
5070 				goto end;
5071 			}
5072 			if (ik.ik_keylen != 0) {
5073 				if (verbose)
5074 					LINE_BREAK();
5075 				printkey(ctx, &ik);
5076 			}
5077 		}
5078 		if (i > 0 && verbose)
5079 			LINE_BREAK();
5080 end:
5081 		;
5082 	}
5083 
5084 	if (get80211val(ctx, IEEE80211_IOC_POWERSAVE, &val) != -1 &&
5085 	    val != IEEE80211_POWERSAVE_NOSUP ) {
5086 		if (val != IEEE80211_POWERSAVE_OFF || verbose) {
5087 			switch (val) {
5088 			case IEEE80211_POWERSAVE_OFF:
5089 				LINE_CHECK("powersavemode OFF");
5090 				break;
5091 			case IEEE80211_POWERSAVE_CAM:
5092 				LINE_CHECK("powersavemode CAM");
5093 				break;
5094 			case IEEE80211_POWERSAVE_PSP:
5095 				LINE_CHECK("powersavemode PSP");
5096 				break;
5097 			case IEEE80211_POWERSAVE_PSP_CAM:
5098 				LINE_CHECK("powersavemode PSP-CAM");
5099 				break;
5100 			}
5101 			if (get80211val(ctx, IEEE80211_IOC_POWERSAVESLEEP, &val) != -1)
5102 				LINE_CHECK("powersavesleep %d", val);
5103 		}
5104 	}
5105 
5106 	if (get80211val(ctx, IEEE80211_IOC_TXPOWER, &val) != -1) {
5107 		if (val & 1)
5108 			LINE_CHECK("txpower %d.5", val/2);
5109 		else
5110 			LINE_CHECK("txpower %d", val/2);
5111 	}
5112 	if (verbose) {
5113 		if (get80211val(ctx, IEEE80211_IOC_TXPOWMAX, &val) != -1)
5114 			LINE_CHECK("txpowmax %.1f", val/2.);
5115 	}
5116 
5117 	if (get80211val(ctx, IEEE80211_IOC_DOTD, &val) != -1) {
5118 		if (val)
5119 			LINE_CHECK("dotd");
5120 		else if (verbose)
5121 			LINE_CHECK("-dotd");
5122 	}
5123 
5124 	if (get80211val(ctx, IEEE80211_IOC_RTSTHRESHOLD, &val) != -1) {
5125 		if (val != IEEE80211_RTS_MAX || verbose)
5126 			LINE_CHECK("rtsthreshold %d", val);
5127 	}
5128 
5129 	if (get80211val(ctx, IEEE80211_IOC_FRAGTHRESHOLD, &val) != -1) {
5130 		if (val != IEEE80211_FRAG_MAX || verbose)
5131 			LINE_CHECK("fragthreshold %d", val);
5132 	}
5133 	if (opmode == IEEE80211_M_STA || verbose) {
5134 		if (get80211val(ctx, IEEE80211_IOC_BMISSTHRESHOLD, &val) != -1) {
5135 			if (val != IEEE80211_HWBMISS_MAX || verbose)
5136 				LINE_CHECK("bmiss %d", val);
5137 		}
5138 	}
5139 
5140 	if (!verbose) {
5141 		gettxparams(ctx);
5142 		tp = &txparams.params[chan2mode(c)];
5143 		printrate("ucastrate", tp->ucastrate,
5144 		    IEEE80211_FIXED_RATE_NONE, IEEE80211_FIXED_RATE_NONE);
5145 		printrate("mcastrate", tp->mcastrate, 2*1,
5146 		    IEEE80211_RATE_MCS|0);
5147 		printrate("mgmtrate", tp->mgmtrate, 2*1,
5148 		    IEEE80211_RATE_MCS|0);
5149 		if (tp->maxretry != 6)		/* XXX */
5150 			LINE_CHECK("maxretry %d", tp->maxretry);
5151 	} else {
5152 		LINE_BREAK();
5153 		list_txparams(ctx);
5154 	}
5155 
5156 	bgscaninterval = -1;
5157 	(void) get80211val(ctx, IEEE80211_IOC_BGSCAN_INTERVAL, &bgscaninterval);
5158 
5159 	if (get80211val(ctx, IEEE80211_IOC_SCANVALID, &val) != -1) {
5160 		if (val != bgscaninterval || verbose)
5161 			LINE_CHECK("scanvalid %u", val);
5162 	}
5163 
5164 	bgscan = 0;
5165 	if (get80211val(ctx, IEEE80211_IOC_BGSCAN, &bgscan) != -1) {
5166 		if (bgscan)
5167 			LINE_CHECK("bgscan");
5168 		else if (verbose)
5169 			LINE_CHECK("-bgscan");
5170 	}
5171 	if (bgscan || verbose) {
5172 		if (bgscaninterval != -1)
5173 			LINE_CHECK("bgscanintvl %u", bgscaninterval);
5174 		if (get80211val(ctx, IEEE80211_IOC_BGSCAN_IDLE, &val) != -1)
5175 			LINE_CHECK("bgscanidle %u", val);
5176 		if (!verbose) {
5177 			getroam(ctx);
5178 			rp = &roamparams.params[chan2mode(c)];
5179 			if (rp->rssi & 1)
5180 				LINE_CHECK("roam:rssi %u.5", rp->rssi/2);
5181 			else
5182 				LINE_CHECK("roam:rssi %u", rp->rssi/2);
5183 			LINE_CHECK("roam:rate %s%u",
5184 			    (rp->rate & IEEE80211_RATE_MCS) ? "MCS " : "",
5185 			    get_rate_value(rp->rate));
5186 		} else {
5187 			LINE_BREAK();
5188 			list_roam(ctx);
5189 			LINE_BREAK();
5190 		}
5191 	}
5192 
5193 	if (IEEE80211_IS_CHAN_ANYG(c) || verbose) {
5194 		if (get80211val(ctx, IEEE80211_IOC_PUREG, &val) != -1) {
5195 			if (val)
5196 				LINE_CHECK("pureg");
5197 			else if (verbose)
5198 				LINE_CHECK("-pureg");
5199 		}
5200 		if (get80211val(ctx, IEEE80211_IOC_PROTMODE, &val) != -1) {
5201 			switch (val) {
5202 			case IEEE80211_PROTMODE_OFF:
5203 				LINE_CHECK("protmode OFF");
5204 				break;
5205 			case IEEE80211_PROTMODE_CTS:
5206 				LINE_CHECK("protmode CTS");
5207 				break;
5208 			case IEEE80211_PROTMODE_RTSCTS:
5209 				LINE_CHECK("protmode RTSCTS");
5210 				break;
5211 			default:
5212 				LINE_CHECK("protmode UNKNOWN (0x%x)", val);
5213 				break;
5214 			}
5215 		}
5216 	}
5217 
5218 	if (IEEE80211_IS_CHAN_HT(c) || verbose) {
5219 		gethtconf(ctx);
5220 		switch (htconf & 3) {
5221 		case 0:
5222 		case 2:
5223 			LINE_CHECK("-ht");
5224 			break;
5225 		case 1:
5226 			LINE_CHECK("ht20");
5227 			break;
5228 		case 3:
5229 			if (verbose)
5230 				LINE_CHECK("ht");
5231 			break;
5232 		}
5233 		if (get80211val(ctx, IEEE80211_IOC_HTCOMPAT, &val) != -1) {
5234 			if (!val)
5235 				LINE_CHECK("-htcompat");
5236 			else if (verbose)
5237 				LINE_CHECK("htcompat");
5238 		}
5239 		if (get80211val(ctx, IEEE80211_IOC_AMPDU, &val) != -1) {
5240 			switch (val) {
5241 			case 0:
5242 				LINE_CHECK("-ampdu");
5243 				break;
5244 			case 1:
5245 				LINE_CHECK("ampdutx -ampdurx");
5246 				break;
5247 			case 2:
5248 				LINE_CHECK("-ampdutx ampdurx");
5249 				break;
5250 			case 3:
5251 				if (verbose)
5252 					LINE_CHECK("ampdu");
5253 				break;
5254 			}
5255 		}
5256 		/* XXX 11ac density/size is different */
5257 		if (get80211val(ctx, IEEE80211_IOC_AMPDU_LIMIT, &val) != -1) {
5258 			switch (val) {
5259 			case IEEE80211_HTCAP_MAXRXAMPDU_8K:
5260 				LINE_CHECK("ampdulimit 8k");
5261 				break;
5262 			case IEEE80211_HTCAP_MAXRXAMPDU_16K:
5263 				LINE_CHECK("ampdulimit 16k");
5264 				break;
5265 			case IEEE80211_HTCAP_MAXRXAMPDU_32K:
5266 				LINE_CHECK("ampdulimit 32k");
5267 				break;
5268 			case IEEE80211_HTCAP_MAXRXAMPDU_64K:
5269 				LINE_CHECK("ampdulimit 64k");
5270 				break;
5271 			}
5272 		}
5273 		/* XXX 11ac density/size is different */
5274 		if (get80211val(ctx, IEEE80211_IOC_AMPDU_DENSITY, &val) != -1) {
5275 			switch (val) {
5276 			case IEEE80211_HTCAP_MPDUDENSITY_NA:
5277 				if (verbose)
5278 					LINE_CHECK("ampdudensity NA");
5279 				break;
5280 			case IEEE80211_HTCAP_MPDUDENSITY_025:
5281 				LINE_CHECK("ampdudensity .25");
5282 				break;
5283 			case IEEE80211_HTCAP_MPDUDENSITY_05:
5284 				LINE_CHECK("ampdudensity .5");
5285 				break;
5286 			case IEEE80211_HTCAP_MPDUDENSITY_1:
5287 				LINE_CHECK("ampdudensity 1");
5288 				break;
5289 			case IEEE80211_HTCAP_MPDUDENSITY_2:
5290 				LINE_CHECK("ampdudensity 2");
5291 				break;
5292 			case IEEE80211_HTCAP_MPDUDENSITY_4:
5293 				LINE_CHECK("ampdudensity 4");
5294 				break;
5295 			case IEEE80211_HTCAP_MPDUDENSITY_8:
5296 				LINE_CHECK("ampdudensity 8");
5297 				break;
5298 			case IEEE80211_HTCAP_MPDUDENSITY_16:
5299 				LINE_CHECK("ampdudensity 16");
5300 				break;
5301 			}
5302 		}
5303 		if (get80211val(ctx, IEEE80211_IOC_AMSDU, &val) != -1) {
5304 			switch (val) {
5305 			case 0:
5306 				LINE_CHECK("-amsdu");
5307 				break;
5308 			case 1:
5309 				LINE_CHECK("amsdutx -amsdurx");
5310 				break;
5311 			case 2:
5312 				LINE_CHECK("-amsdutx amsdurx");
5313 				break;
5314 			case 3:
5315 				if (verbose)
5316 					LINE_CHECK("amsdu");
5317 				break;
5318 			}
5319 		}
5320 		/* XXX amsdu limit */
5321 		if (get80211val(ctx, IEEE80211_IOC_SHORTGI, &val) != -1) {
5322 			if (val)
5323 				LINE_CHECK("shortgi");
5324 			else if (verbose)
5325 				LINE_CHECK("-shortgi");
5326 		}
5327 		if (get80211val(ctx, IEEE80211_IOC_HTPROTMODE, &val) != -1) {
5328 			if (val == IEEE80211_PROTMODE_OFF)
5329 				LINE_CHECK("htprotmode OFF");
5330 			else if (val != IEEE80211_PROTMODE_RTSCTS)
5331 				LINE_CHECK("htprotmode UNKNOWN (0x%x)", val);
5332 			else if (verbose)
5333 				LINE_CHECK("htprotmode RTSCTS");
5334 		}
5335 		if (get80211val(ctx, IEEE80211_IOC_PUREN, &val) != -1) {
5336 			if (val)
5337 				LINE_CHECK("puren");
5338 			else if (verbose)
5339 				LINE_CHECK("-puren");
5340 		}
5341 		if (get80211val(ctx, IEEE80211_IOC_SMPS, &val) != -1) {
5342 			if (val == IEEE80211_HTCAP_SMPS_DYNAMIC)
5343 				LINE_CHECK("smpsdyn");
5344 			else if (val == IEEE80211_HTCAP_SMPS_ENA)
5345 				LINE_CHECK("smps");
5346 			else if (verbose)
5347 				LINE_CHECK("-smps");
5348 		}
5349 		if (get80211val(ctx, IEEE80211_IOC_RIFS, &val) != -1) {
5350 			if (val)
5351 				LINE_CHECK("rifs");
5352 			else if (verbose)
5353 				LINE_CHECK("-rifs");
5354 		}
5355 
5356 		/* XXX VHT STBC? */
5357 		if (get80211val(ctx, IEEE80211_IOC_STBC, &val) != -1) {
5358 			switch (val) {
5359 			case 0:
5360 				LINE_CHECK("-stbc");
5361 				break;
5362 			case 1:
5363 				LINE_CHECK("stbctx -stbcrx");
5364 				break;
5365 			case 2:
5366 				LINE_CHECK("-stbctx stbcrx");
5367 				break;
5368 			case 3:
5369 				if (verbose)
5370 					LINE_CHECK("stbc");
5371 				break;
5372 			}
5373 		}
5374 		if (get80211val(ctx, IEEE80211_IOC_LDPC, &val) != -1) {
5375 			switch (val) {
5376 			case 0:
5377 				LINE_CHECK("-ldpc");
5378 				break;
5379 			case 1:
5380 				LINE_CHECK("ldpctx -ldpcrx");
5381 				break;
5382 			case 2:
5383 				LINE_CHECK("-ldpctx ldpcrx");
5384 				break;
5385 			case 3:
5386 				if (verbose)
5387 					LINE_CHECK("ldpc");
5388 				break;
5389 			}
5390 		}
5391 		if (get80211val(ctx, IEEE80211_IOC_UAPSD, &val) != -1) {
5392 			switch (val) {
5393 			case 0:
5394 				LINE_CHECK("-uapsd");
5395 				break;
5396 			case 1:
5397 				LINE_CHECK("uapsd");
5398 				break;
5399 			}
5400 		}
5401 	}
5402 
5403 	if (IEEE80211_IS_CHAN_VHT(c) || verbose) {
5404 		getvhtconf(ctx);
5405 		if (vhtconf & IEEE80211_FVHT_VHT)
5406 			LINE_CHECK("vht");
5407 		else
5408 			LINE_CHECK("-vht");
5409 		if (vhtconf & IEEE80211_FVHT_USEVHT40)
5410 			LINE_CHECK("vht40");
5411 		else
5412 			LINE_CHECK("-vht40");
5413 		if (vhtconf & IEEE80211_FVHT_USEVHT80)
5414 			LINE_CHECK("vht80");
5415 		else
5416 			LINE_CHECK("-vht80");
5417 		if (vhtconf & IEEE80211_FVHT_USEVHT160)
5418 			LINE_CHECK("vht160");
5419 		else
5420 			LINE_CHECK("-vht160");
5421 		if (vhtconf & IEEE80211_FVHT_USEVHT80P80)
5422 			LINE_CHECK("vht80p80");
5423 		else
5424 			LINE_CHECK("-vht80p80");
5425 	}
5426 
5427 	if (get80211val(ctx, IEEE80211_IOC_WME, &wme) != -1) {
5428 		if (wme)
5429 			LINE_CHECK("wme");
5430 		else if (verbose)
5431 			LINE_CHECK("-wme");
5432 	} else
5433 		wme = 0;
5434 
5435 	if (get80211val(ctx, IEEE80211_IOC_BURST, &val) != -1) {
5436 		if (val)
5437 			LINE_CHECK("burst");
5438 		else if (verbose)
5439 			LINE_CHECK("-burst");
5440 	}
5441 
5442 	if (get80211val(ctx, IEEE80211_IOC_FF, &val) != -1) {
5443 		if (val)
5444 			LINE_CHECK("ff");
5445 		else if (verbose)
5446 			LINE_CHECK("-ff");
5447 	}
5448 	if (get80211val(ctx, IEEE80211_IOC_TURBOP, &val) != -1) {
5449 		if (val)
5450 			LINE_CHECK("dturbo");
5451 		else if (verbose)
5452 			LINE_CHECK("-dturbo");
5453 	}
5454 	if (get80211val(ctx, IEEE80211_IOC_DWDS, &val) != -1) {
5455 		if (val)
5456 			LINE_CHECK("dwds");
5457 		else if (verbose)
5458 			LINE_CHECK("-dwds");
5459 	}
5460 
5461 	if (opmode == IEEE80211_M_HOSTAP) {
5462 		if (get80211val(ctx, IEEE80211_IOC_HIDESSID, &val) != -1) {
5463 			if (val)
5464 				LINE_CHECK("hidessid");
5465 			else if (verbose)
5466 				LINE_CHECK("-hidessid");
5467 		}
5468 		if (get80211val(ctx, IEEE80211_IOC_APBRIDGE, &val) != -1) {
5469 			if (!val)
5470 				LINE_CHECK("-apbridge");
5471 			else if (verbose)
5472 				LINE_CHECK("apbridge");
5473 		}
5474 		if (get80211val(ctx, IEEE80211_IOC_DTIM_PERIOD, &val) != -1)
5475 			LINE_CHECK("dtimperiod %u", val);
5476 
5477 		if (get80211val(ctx, IEEE80211_IOC_DOTH, &val) != -1) {
5478 			if (!val)
5479 				LINE_CHECK("-doth");
5480 			else if (verbose)
5481 				LINE_CHECK("doth");
5482 		}
5483 		if (get80211val(ctx, IEEE80211_IOC_DFS, &val) != -1) {
5484 			if (!val)
5485 				LINE_CHECK("-dfs");
5486 			else if (verbose)
5487 				LINE_CHECK("dfs");
5488 		}
5489 		if (get80211val(ctx, IEEE80211_IOC_INACTIVITY, &val) != -1) {
5490 			if (!val)
5491 				LINE_CHECK("-inact");
5492 			else if (verbose)
5493 				LINE_CHECK("inact");
5494 		}
5495 	} else {
5496 		if (get80211val(ctx, IEEE80211_IOC_ROAMING, &val) != -1) {
5497 			if (val != IEEE80211_ROAMING_AUTO || verbose) {
5498 				switch (val) {
5499 				case IEEE80211_ROAMING_DEVICE:
5500 					LINE_CHECK("roaming DEVICE");
5501 					break;
5502 				case IEEE80211_ROAMING_AUTO:
5503 					LINE_CHECK("roaming AUTO");
5504 					break;
5505 				case IEEE80211_ROAMING_MANUAL:
5506 					LINE_CHECK("roaming MANUAL");
5507 					break;
5508 				default:
5509 					LINE_CHECK("roaming UNKNOWN (0x%x)",
5510 						val);
5511 					break;
5512 				}
5513 			}
5514 		}
5515 	}
5516 
5517 	if (opmode == IEEE80211_M_AHDEMO) {
5518 		if (get80211val(ctx, IEEE80211_IOC_TDMA_SLOT, &val) != -1)
5519 			LINE_CHECK("tdmaslot %u", val);
5520 		if (get80211val(ctx, IEEE80211_IOC_TDMA_SLOTCNT, &val) != -1)
5521 			LINE_CHECK("tdmaslotcnt %u", val);
5522 		if (get80211val(ctx, IEEE80211_IOC_TDMA_SLOTLEN, &val) != -1)
5523 			LINE_CHECK("tdmaslotlen %u", val);
5524 		if (get80211val(ctx, IEEE80211_IOC_TDMA_BINTERVAL, &val) != -1)
5525 			LINE_CHECK("tdmabintval %u", val);
5526 	} else if (get80211val(ctx, IEEE80211_IOC_BEACON_INTERVAL, &val) != -1) {
5527 		/* XXX default define not visible */
5528 		if (val != 100 || verbose)
5529 			LINE_CHECK("bintval %u", val);
5530 	}
5531 
5532 	if (wme && verbose) {
5533 		LINE_BREAK();
5534 		list_wme(ctx);
5535 	}
5536 
5537 	if (opmode == IEEE80211_M_MBSS) {
5538 		if (get80211val(ctx, IEEE80211_IOC_MESH_TTL, &val) != -1) {
5539 			LINE_CHECK("meshttl %u", val);
5540 		}
5541 		if (get80211val(ctx, IEEE80211_IOC_MESH_AP, &val) != -1) {
5542 			if (val)
5543 				LINE_CHECK("meshpeering");
5544 			else
5545 				LINE_CHECK("-meshpeering");
5546 		}
5547 		if (get80211val(ctx, IEEE80211_IOC_MESH_FWRD, &val) != -1) {
5548 			if (val)
5549 				LINE_CHECK("meshforward");
5550 			else
5551 				LINE_CHECK("-meshforward");
5552 		}
5553 		if (get80211val(ctx, IEEE80211_IOC_MESH_GATE, &val) != -1) {
5554 			if (val)
5555 				LINE_CHECK("meshgate");
5556 			else
5557 				LINE_CHECK("-meshgate");
5558 		}
5559 		if (get80211len(ctx, IEEE80211_IOC_MESH_PR_METRIC, data, 12,
5560 		    &len) != -1) {
5561 			data[len] = '\0';
5562 			LINE_CHECK("meshmetric %s", data);
5563 		}
5564 		if (get80211len(ctx, IEEE80211_IOC_MESH_PR_PATH, data, 12,
5565 		    &len) != -1) {
5566 			data[len] = '\0';
5567 			LINE_CHECK("meshpath %s", data);
5568 		}
5569 		if (get80211val(ctx, IEEE80211_IOC_HWMP_ROOTMODE, &val) != -1) {
5570 			switch (val) {
5571 			case IEEE80211_HWMP_ROOTMODE_DISABLED:
5572 				LINE_CHECK("hwmprootmode DISABLED");
5573 				break;
5574 			case IEEE80211_HWMP_ROOTMODE_NORMAL:
5575 				LINE_CHECK("hwmprootmode NORMAL");
5576 				break;
5577 			case IEEE80211_HWMP_ROOTMODE_PROACTIVE:
5578 				LINE_CHECK("hwmprootmode PROACTIVE");
5579 				break;
5580 			case IEEE80211_HWMP_ROOTMODE_RANN:
5581 				LINE_CHECK("hwmprootmode RANN");
5582 				break;
5583 			default:
5584 				LINE_CHECK("hwmprootmode UNKNOWN(%d)", val);
5585 				break;
5586 			}
5587 		}
5588 		if (get80211val(ctx, IEEE80211_IOC_HWMP_MAXHOPS, &val) != -1) {
5589 			LINE_CHECK("hwmpmaxhops %u", val);
5590 		}
5591 	}
5592 
5593 	LINE_BREAK();
5594 
5595 	if (getdevicename(ctx, data, sizeof(data), &len) < 0)
5596 		return;
5597 	LINE_CHECK("parent interface: %s", data);
5598 
5599 	LINE_BREAK();
5600 }
5601 
5602 static int
5603 get80211(if_ctx *ctx, int type, void *data, int len)
5604 {
5605 
5606 	return (lib80211_get80211(ctx->io_s, ctx->ifname, type, data, len));
5607 }
5608 
5609 static int
5610 get80211len(if_ctx *ctx, int type, void *data, int len, int *plen)
5611 {
5612 
5613 	return (lib80211_get80211len(ctx->io_s, ctx->ifname, type, data, len, plen));
5614 }
5615 
5616 static int
5617 get80211val(if_ctx *ctx, int type, int *val)
5618 {
5619 
5620 	return (lib80211_get80211val(ctx->io_s, ctx->ifname, type, val));
5621 }
5622 
5623 static void
5624 set80211(if_ctx *ctx, int type, int val, int len, void *data)
5625 {
5626 	int ret;
5627 
5628 	ret = lib80211_set80211(ctx->io_s, ctx->ifname, type, val, len, data);
5629 	if (ret < 0)
5630 		err(1, "SIOCS80211");
5631 }
5632 
5633 static const char *
5634 get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
5635 {
5636 	int len;
5637 	int hexstr;
5638 	u_int8_t *p;
5639 
5640 	len = *lenp;
5641 	p = buf;
5642 	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
5643 	if (hexstr)
5644 		val += 2;
5645 	for (;;) {
5646 		if (*val == '\0')
5647 			break;
5648 		if (sep != NULL && strchr(sep, *val) != NULL) {
5649 			val++;
5650 			break;
5651 		}
5652 		if (hexstr) {
5653 			if (!isxdigit((u_char)val[0])) {
5654 				warnx("bad hexadecimal digits");
5655 				return NULL;
5656 			}
5657 			if (!isxdigit((u_char)val[1])) {
5658 				warnx("odd count hexadecimal digits");
5659 				return NULL;
5660 			}
5661 		}
5662 		if (p >= buf + len) {
5663 			if (hexstr)
5664 				warnx("hexadecimal digits too long");
5665 			else
5666 				warnx("string too long");
5667 			return NULL;
5668 		}
5669 		if (hexstr) {
5670 #define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
5671 			*p++ = (tohex((u_char)val[0]) << 4) |
5672 			    tohex((u_char)val[1]);
5673 #undef tohex
5674 			val += 2;
5675 		} else
5676 			*p++ = *val++;
5677 	}
5678 	len = p - buf;
5679 	/* The string "-" is treated as the empty string. */
5680 	if (!hexstr && len == 1 && buf[0] == '-') {
5681 		len = 0;
5682 		memset(buf, 0, *lenp);
5683 	} else if (len < *lenp)
5684 		memset(p, 0, *lenp - len);
5685 	*lenp = len;
5686 	return val;
5687 }
5688 
5689 static void
5690 print_string(const u_int8_t *buf, int len)
5691 {
5692 	int i;
5693 	int hasspc;
5694 	int utf8;
5695 
5696 	i = 0;
5697 	hasspc = 0;
5698 
5699 	setlocale(LC_CTYPE, "");
5700 	utf8 = strncmp("UTF-8", nl_langinfo(CODESET), 5) == 0;
5701 
5702 	for (; i < len; i++) {
5703 		if (!isprint(buf[i]) && buf[i] != '\0' && !utf8)
5704 			break;
5705 		if (isspace(buf[i]))
5706 			hasspc++;
5707 	}
5708 	if (i == len || utf8) {
5709 		if (hasspc || len == 0 || buf[0] == '\0')
5710 			printf("\"%.*s\"", len, buf);
5711 		else
5712 			printf("%.*s", len, buf);
5713 	} else {
5714 		printf("0x");
5715 		for (i = 0; i < len; i++)
5716 			printf("%02x", buf[i]);
5717 	}
5718 }
5719 
5720 static void
5721 setdefregdomain(if_ctx *ctx)
5722 {
5723 	struct regdata *rdp = getregdata();
5724 	const struct regdomain *rd;
5725 
5726 	/* Check if regdomain/country was already set by a previous call. */
5727 	/* XXX is it possible? */
5728 	if (regdomain.regdomain != 0 ||
5729 	    regdomain.country != CTRY_DEFAULT)
5730 		return;
5731 
5732 	getregdomain(ctx);
5733 
5734 	/* Check if it was already set by the driver. */
5735 	if (regdomain.regdomain != 0 ||
5736 	    regdomain.country != CTRY_DEFAULT)
5737 		return;
5738 
5739 	/* Set FCC/US as default. */
5740 	rd = lib80211_regdomain_findbysku(rdp, SKU_FCC);
5741 	if (rd == NULL)
5742 		errx(1, "FCC regdomain was not found");
5743 
5744 	regdomain.regdomain = rd->sku;
5745 	if (rd->cc != NULL)
5746 		defaultcountry(rd);
5747 
5748 	/* Send changes to net80211. */
5749 	setregdomain_cb(ctx, &regdomain);
5750 
5751 	/* Cleanup (so it can be overridden by subsequent parameters). */
5752 	regdomain.regdomain = 0;
5753 	regdomain.country = CTRY_DEFAULT;
5754 	regdomain.isocc[0] = 0;
5755 	regdomain.isocc[1] = 0;
5756 }
5757 
5758 /*
5759  * Virtual AP cloning support.
5760  */
5761 static struct ieee80211_clone_params params = {
5762 	.icp_opmode	= IEEE80211_M_STA,	/* default to station mode */
5763 };
5764 
5765 static void
5766 wlan_create(if_ctx *ctx, struct ifreq *ifr)
5767 {
5768 	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
5769 
5770 	if (params.icp_parent[0] == '\0')
5771 		errx(1, "must specify a parent device (wlandev) when creating "
5772 		    "a wlan device");
5773 	if (params.icp_opmode == IEEE80211_M_WDS &&
5774 	    memcmp(params.icp_bssid, zerobssid, sizeof(zerobssid)) == 0)
5775 		errx(1, "no bssid specified for WDS (use wlanbssid)");
5776 	ifr->ifr_data = (caddr_t) &params;
5777 	ifcreate_ioctl(ctx, ifr);
5778 
5779 	setdefregdomain(ctx);
5780 }
5781 
5782 static void
5783 set80211clone_wlandev(if_ctx *ctx __unused, const char *arg, int dummy __unused)
5784 {
5785 	strlcpy(params.icp_parent, arg, IFNAMSIZ);
5786 }
5787 
5788 static void
5789 set80211clone_wlanbssid(if_ctx *ctx __unused, const char *arg, int dummy __unused)
5790 {
5791 	const struct ether_addr *ea;
5792 
5793 	ea = ether_aton(arg);
5794 	if (ea == NULL)
5795 		errx(1, "%s: cannot parse bssid", arg);
5796 	memcpy(params.icp_bssid, ea->octet, IEEE80211_ADDR_LEN);
5797 }
5798 
5799 static void
5800 set80211clone_wlanaddr(if_ctx *ctx __unused, const char *arg, int dummy __unused)
5801 {
5802 	const struct ether_addr *ea;
5803 
5804 	ea = ether_aton(arg);
5805 	if (ea == NULL)
5806 		errx(1, "%s: cannot parse address", arg);
5807 	memcpy(params.icp_macaddr, ea->octet, IEEE80211_ADDR_LEN);
5808 	params.icp_flags |= IEEE80211_CLONE_MACADDR;
5809 }
5810 
5811 static void
5812 set80211clone_wlanmode(if_ctx *ctx, const char *arg, int dummy __unused)
5813 {
5814 #define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
5815 	if (iseq(arg, "sta"))
5816 		params.icp_opmode = IEEE80211_M_STA;
5817 	else if (iseq(arg, "ahdemo") || iseq(arg, "adhoc-demo"))
5818 		params.icp_opmode = IEEE80211_M_AHDEMO;
5819 	else if (iseq(arg, "ibss") || iseq(arg, "adhoc"))
5820 		params.icp_opmode = IEEE80211_M_IBSS;
5821 	else if (iseq(arg, "ap") || iseq(arg, "host"))
5822 		params.icp_opmode = IEEE80211_M_HOSTAP;
5823 	else if (iseq(arg, "wds"))
5824 		params.icp_opmode = IEEE80211_M_WDS;
5825 	else if (iseq(arg, "monitor"))
5826 		params.icp_opmode = IEEE80211_M_MONITOR;
5827 	else if (iseq(arg, "tdma")) {
5828 		params.icp_opmode = IEEE80211_M_AHDEMO;
5829 		params.icp_flags |= IEEE80211_CLONE_TDMA;
5830 	} else if (iseq(arg, "mesh") || iseq(arg, "mp")) /* mesh point */
5831 		params.icp_opmode = IEEE80211_M_MBSS;
5832 	else
5833 		errx(1, "Don't know to create %s for %s", arg, ctx->ifname);
5834 #undef iseq
5835 }
5836 
5837 static void
5838 set80211clone_beacons(if_ctx *ctx __unused, const char *val __unused, int d)
5839 {
5840 	/* NB: inverted sense */
5841 	if (d)
5842 		params.icp_flags &= ~IEEE80211_CLONE_NOBEACONS;
5843 	else
5844 		params.icp_flags |= IEEE80211_CLONE_NOBEACONS;
5845 }
5846 
5847 static void
5848 set80211clone_bssid(if_ctx *ctx __unused, const char *val __unused, int d)
5849 {
5850 	if (d)
5851 		params.icp_flags |= IEEE80211_CLONE_BSSID;
5852 	else
5853 		params.icp_flags &= ~IEEE80211_CLONE_BSSID;
5854 }
5855 
5856 static void
5857 set80211clone_wdslegacy(if_ctx *ctx __unused, const char *val __unused, int d)
5858 {
5859 	if (d)
5860 		params.icp_flags |= IEEE80211_CLONE_WDSLEGACY;
5861 	else
5862 		params.icp_flags &= ~IEEE80211_CLONE_WDSLEGACY;
5863 }
5864 
5865 static struct cmd ieee80211_cmds[] = {
5866 	DEF_CMD_ARG("ssid",		set80211ssid),
5867 	DEF_CMD_ARG("nwid",		set80211ssid),
5868 	DEF_CMD_ARG("meshid",		set80211meshid),
5869 	DEF_CMD_ARG("stationname",	set80211stationname),
5870 	DEF_CMD_ARG("station",		set80211stationname),	/* BSD/OS */
5871 	DEF_CMD_ARG("channel",		set80211channel),
5872 	DEF_CMD_ARG("authmode",		set80211authmode),
5873 	DEF_CMD_ARG("powersavemode",	set80211powersavemode),
5874 	DEF_CMD("powersave",	1,	set80211powersave),
5875 	DEF_CMD("-powersave",	0,	set80211powersave),
5876 	DEF_CMD_ARG("powersavesleep", 	set80211powersavesleep),
5877 	DEF_CMD_ARG("wepmode",		set80211wepmode),
5878 	DEF_CMD("wep",		1,	set80211wep),
5879 	DEF_CMD("-wep",		0,	set80211wep),
5880 	DEF_CMD_ARG("deftxkey",		set80211weptxkey),
5881 	DEF_CMD_ARG("weptxkey",		set80211weptxkey),
5882 	DEF_CMD_ARG("wepkey",		set80211wepkey),
5883 	DEF_CMD_ARG("nwkey",		set80211nwkey),		/* NetBSD */
5884 	DEF_CMD("-nwkey",	0,	set80211wep),		/* NetBSD */
5885 	DEF_CMD_ARG("rtsthreshold",	set80211rtsthreshold),
5886 	DEF_CMD_ARG("protmode",		set80211protmode),
5887 	DEF_CMD_ARG("txpower",		set80211txpower),
5888 	DEF_CMD_ARG("roaming",		set80211roaming),
5889 	DEF_CMD("wme",		1,	set80211wme),
5890 	DEF_CMD("-wme",		0,	set80211wme),
5891 	DEF_CMD("wmm",		1,	set80211wme),
5892 	DEF_CMD("-wmm",		0,	set80211wme),
5893 	DEF_CMD("hidessid",	1,	set80211hidessid),
5894 	DEF_CMD("-hidessid",	0,	set80211hidessid),
5895 	DEF_CMD("apbridge",	1,	set80211apbridge),
5896 	DEF_CMD("-apbridge",	0,	set80211apbridge),
5897 	DEF_CMD_ARG("chanlist",		set80211chanlist),
5898 	DEF_CMD_ARG("bssid",		set80211bssid),
5899 	DEF_CMD_ARG("ap",		set80211bssid),
5900 	DEF_CMD("scan",	0,		set80211scan),
5901 	DEF_CMD_ARG("list",		set80211list),
5902 	DEF_CMD_ARG2("cwmin",		set80211cwmin),
5903 	DEF_CMD_ARG2("cwmax",		set80211cwmax),
5904 	DEF_CMD_ARG2("aifs",		set80211aifs),
5905 	DEF_CMD_ARG2("txoplimit",	set80211txoplimit),
5906 	DEF_CMD_ARG("acm",		set80211acm),
5907 	DEF_CMD_ARG("-acm",		set80211noacm),
5908 	DEF_CMD_ARG("ack",		set80211ackpolicy),
5909 	DEF_CMD_ARG("-ack",		set80211noackpolicy),
5910 	DEF_CMD_ARG2("bss:cwmin",	set80211bsscwmin),
5911 	DEF_CMD_ARG2("bss:cwmax",	set80211bsscwmax),
5912 	DEF_CMD_ARG2("bss:aifs",	set80211bssaifs),
5913 	DEF_CMD_ARG2("bss:txoplimit",	set80211bsstxoplimit),
5914 	DEF_CMD_ARG("dtimperiod",	set80211dtimperiod),
5915 	DEF_CMD_ARG("bintval",		set80211bintval),
5916 	DEF_CMD("mac:open",	IEEE80211_MACCMD_POLICY_OPEN,	set80211maccmd),
5917 	DEF_CMD("mac:allow",	IEEE80211_MACCMD_POLICY_ALLOW,	set80211maccmd),
5918 	DEF_CMD("mac:deny",	IEEE80211_MACCMD_POLICY_DENY,	set80211maccmd),
5919 	DEF_CMD("mac:radius",	IEEE80211_MACCMD_POLICY_RADIUS,	set80211maccmd),
5920 	DEF_CMD("mac:flush",	IEEE80211_MACCMD_FLUSH,		set80211maccmd),
5921 	DEF_CMD("mac:detach",	IEEE80211_MACCMD_DETACH,	set80211maccmd),
5922 	DEF_CMD_ARG("mac:add",		set80211addmac),
5923 	DEF_CMD_ARG("mac:del",		set80211delmac),
5924 	DEF_CMD_ARG("mac:kick",		set80211kickmac),
5925 	DEF_CMD("pureg",	1,	set80211pureg),
5926 	DEF_CMD("-pureg",	0,	set80211pureg),
5927 	DEF_CMD("ff",		1,	set80211fastframes),
5928 	DEF_CMD("-ff",		0,	set80211fastframes),
5929 	DEF_CMD("dturbo",	1,	set80211dturbo),
5930 	DEF_CMD("-dturbo",	0,	set80211dturbo),
5931 	DEF_CMD("bgscan",	1,	set80211bgscan),
5932 	DEF_CMD("-bgscan",	0,	set80211bgscan),
5933 	DEF_CMD_ARG("bgscanidle",	set80211bgscanidle),
5934 	DEF_CMD_ARG("bgscanintvl",	set80211bgscanintvl),
5935 	DEF_CMD_ARG("scanvalid",	set80211scanvalid),
5936 	DEF_CMD("quiet",	1,	set80211quiet),
5937 	DEF_CMD("-quiet",	0,	set80211quiet),
5938 	DEF_CMD_ARG("quiet_count",	set80211quietcount),
5939 	DEF_CMD_ARG("quiet_period",	set80211quietperiod),
5940 	DEF_CMD_ARG("quiet_duration",	set80211quietduration),
5941 	DEF_CMD_ARG("quiet_offset",	set80211quietoffset),
5942 	DEF_CMD_ARG("roam:rssi",	set80211roamrssi),
5943 	DEF_CMD_ARG("roam:rate",	set80211roamrate),
5944 	DEF_CMD_ARG("mcastrate",	set80211mcastrate),
5945 	DEF_CMD_ARG("ucastrate",	set80211ucastrate),
5946 	DEF_CMD_ARG("mgtrate",		set80211mgtrate),
5947 	DEF_CMD_ARG("mgmtrate",		set80211mgtrate),
5948 	DEF_CMD_ARG("maxretry",		set80211maxretry),
5949 	DEF_CMD_ARG("fragthreshold",	set80211fragthreshold),
5950 	DEF_CMD("burst",	1,	set80211burst),
5951 	DEF_CMD("-burst",	0,	set80211burst),
5952 	DEF_CMD_ARG("bmiss",		set80211bmissthreshold),
5953 	DEF_CMD_ARG("bmissthreshold",	set80211bmissthreshold),
5954 	DEF_CMD("shortgi",	1,	set80211shortgi),
5955 	DEF_CMD("-shortgi",	0,	set80211shortgi),
5956 	DEF_CMD("ampdurx",	2,	set80211ampdu),
5957 	DEF_CMD("-ampdurx",	-2,	set80211ampdu),
5958 	DEF_CMD("ampdutx",	1,	set80211ampdu),
5959 	DEF_CMD("-ampdutx",	-1,	set80211ampdu),
5960 	DEF_CMD("ampdu",	3,	set80211ampdu),		/* NB: tx+rx */
5961 	DEF_CMD("-ampdu",	-3,	set80211ampdu),
5962 	DEF_CMD_ARG("ampdulimit",	set80211ampdulimit),
5963 	DEF_CMD_ARG("ampdudensity",	set80211ampdudensity),
5964 	DEF_CMD("amsdurx",	2,	set80211amsdu),
5965 	DEF_CMD("-amsdurx",	-2,	set80211amsdu),
5966 	DEF_CMD("amsdutx",	1,	set80211amsdu),
5967 	DEF_CMD("-amsdutx",	-1,	set80211amsdu),
5968 	DEF_CMD("amsdu",	3,	set80211amsdu),		/* NB: tx+rx */
5969 	DEF_CMD("-amsdu",	-3,	set80211amsdu),
5970 	DEF_CMD_ARG("amsdulimit",	set80211amsdulimit),
5971 	DEF_CMD("stbcrx",	2,	set80211stbc),
5972 	DEF_CMD("-stbcrx",	-2,	set80211stbc),
5973 	DEF_CMD("stbctx",	1,	set80211stbc),
5974 	DEF_CMD("-stbctx",	-1,	set80211stbc),
5975 	DEF_CMD("stbc",		3,	set80211stbc),		/* NB: tx+rx */
5976 	DEF_CMD("-stbc",	-3,	set80211stbc),
5977 	DEF_CMD("ldpcrx",	2,	set80211ldpc),
5978 	DEF_CMD("-ldpcrx",	-2,	set80211ldpc),
5979 	DEF_CMD("ldpctx",	1,	set80211ldpc),
5980 	DEF_CMD("-ldpctx",	-1,	set80211ldpc),
5981 	DEF_CMD("ldpc",		3,	set80211ldpc),		/* NB: tx+rx */
5982 	DEF_CMD("-ldpc",	-3,	set80211ldpc),
5983 	DEF_CMD("uapsd",	1,	set80211uapsd),
5984 	DEF_CMD("-uapsd",	0,	set80211uapsd),
5985 	DEF_CMD("puren",	1,	set80211puren),
5986 	DEF_CMD("-puren",	0,	set80211puren),
5987 	DEF_CMD("doth",		1,	set80211doth),
5988 	DEF_CMD("-doth",	0,	set80211doth),
5989 	DEF_CMD("dfs",		1,	set80211dfs),
5990 	DEF_CMD("-dfs",		0,	set80211dfs),
5991 	DEF_CMD("htcompat",	1,	set80211htcompat),
5992 	DEF_CMD("-htcompat",	0,	set80211htcompat),
5993 	DEF_CMD("dwds",		1,	set80211dwds),
5994 	DEF_CMD("-dwds",	0,	set80211dwds),
5995 	DEF_CMD("inact",	1,	set80211inact),
5996 	DEF_CMD("-inact",	0,	set80211inact),
5997 	DEF_CMD("tsn",		1,	set80211tsn),
5998 	DEF_CMD("-tsn",		0,	set80211tsn),
5999 	DEF_CMD_ARG("regdomain",	set80211regdomain),
6000 	DEF_CMD_ARG("country",		set80211country),
6001 	DEF_CMD("indoor",	'I',	set80211location),
6002 	DEF_CMD("-indoor",	'O',	set80211location),
6003 	DEF_CMD("outdoor",	'O',	set80211location),
6004 	DEF_CMD("-outdoor",	'I',	set80211location),
6005 	DEF_CMD("anywhere",	' ',	set80211location),
6006 	DEF_CMD("ecm",		1,	set80211ecm),
6007 	DEF_CMD("-ecm",		0,	set80211ecm),
6008 	DEF_CMD("dotd",		1,	set80211dotd),
6009 	DEF_CMD("-dotd",	0,	set80211dotd),
6010 	DEF_CMD_ARG("htprotmode",	set80211htprotmode),
6011 	DEF_CMD("ht20",		1,	set80211htconf),
6012 	DEF_CMD("-ht20",	0,	set80211htconf),
6013 	DEF_CMD("ht40",		3,	set80211htconf),	/* NB: 20+40 */
6014 	DEF_CMD("-ht40",	0,	set80211htconf),
6015 	DEF_CMD("ht",		3,	set80211htconf),	/* NB: 20+40 */
6016 	DEF_CMD("-ht",		0,	set80211htconf),
6017 	DEF_CMD("vht",		IEEE80211_FVHT_VHT,		set80211vhtconf),
6018 	DEF_CMD("-vht",		0,				set80211vhtconf),
6019 	DEF_CMD("vht40",	IEEE80211_FVHT_USEVHT40,	set80211vhtconf),
6020 	DEF_CMD("-vht40",	-IEEE80211_FVHT_USEVHT40,	set80211vhtconf),
6021 	DEF_CMD("vht80",	IEEE80211_FVHT_USEVHT80,	set80211vhtconf),
6022 	DEF_CMD("-vht80",	-IEEE80211_FVHT_USEVHT80,	set80211vhtconf),
6023 	DEF_CMD("vht160",	IEEE80211_FVHT_USEVHT160,	set80211vhtconf),
6024 	DEF_CMD("-vht160",	-IEEE80211_FVHT_USEVHT160,	set80211vhtconf),
6025 	DEF_CMD("vht80p80",	IEEE80211_FVHT_USEVHT80P80,	set80211vhtconf),
6026 	DEF_CMD("-vht80p80",	-IEEE80211_FVHT_USEVHT80P80,	set80211vhtconf),
6027 	DEF_CMD("rifs",		1,	set80211rifs),
6028 	DEF_CMD("-rifs",	0,	set80211rifs),
6029 	DEF_CMD("smps",		IEEE80211_HTCAP_SMPS_ENA,	set80211smps),
6030 	DEF_CMD("smpsdyn",	IEEE80211_HTCAP_SMPS_DYNAMIC,	set80211smps),
6031 	DEF_CMD("-smps",	IEEE80211_HTCAP_SMPS_OFF,	set80211smps),
6032 	/* XXX for testing */
6033 	DEF_CMD_ARG("chanswitch",	set80211chanswitch),
6034 
6035 	DEF_CMD_ARG("tdmaslot",		set80211tdmaslot),
6036 	DEF_CMD_ARG("tdmaslotcnt",	set80211tdmaslotcnt),
6037 	DEF_CMD_ARG("tdmaslotlen",	set80211tdmaslotlen),
6038 	DEF_CMD_ARG("tdmabintval",	set80211tdmabintval),
6039 
6040 	DEF_CMD_ARG("meshttl",		set80211meshttl),
6041 	DEF_CMD("meshforward",	1,	set80211meshforward),
6042 	DEF_CMD("-meshforward",	0,	set80211meshforward),
6043 	DEF_CMD("meshgate",	1,	set80211meshgate),
6044 	DEF_CMD("-meshgate",	0,	set80211meshgate),
6045 	DEF_CMD("meshpeering",	1,	set80211meshpeering),
6046 	DEF_CMD("-meshpeering",	0,	set80211meshpeering),
6047 	DEF_CMD_ARG("meshmetric",	set80211meshmetric),
6048 	DEF_CMD_ARG("meshpath",		set80211meshpath),
6049 	DEF_CMD("meshrt:flush",	IEEE80211_MESH_RTCMD_FLUSH,	set80211meshrtcmd),
6050 	DEF_CMD_ARG("meshrt:add",	set80211addmeshrt),
6051 	DEF_CMD_ARG("meshrt:del",	set80211delmeshrt),
6052 	DEF_CMD_ARG("hwmprootmode",	set80211hwmprootmode),
6053 	DEF_CMD_ARG("hwmpmaxhops",	set80211hwmpmaxhops),
6054 
6055 	/* vap cloning support */
6056 	DEF_CLONE_CMD_ARG("wlanaddr",	set80211clone_wlanaddr),
6057 	DEF_CLONE_CMD_ARG("wlanbssid",	set80211clone_wlanbssid),
6058 	DEF_CLONE_CMD_ARG("wlandev",	set80211clone_wlandev),
6059 	DEF_CLONE_CMD_ARG("wlanmode",	set80211clone_wlanmode),
6060 	DEF_CLONE_CMD("beacons", 1,	set80211clone_beacons),
6061 	DEF_CLONE_CMD("-beacons", 0,	set80211clone_beacons),
6062 	DEF_CLONE_CMD("bssid",	1,	set80211clone_bssid),
6063 	DEF_CLONE_CMD("-bssid",	0,	set80211clone_bssid),
6064 	DEF_CLONE_CMD("wdslegacy", 1,	set80211clone_wdslegacy),
6065 	DEF_CLONE_CMD("-wdslegacy", 0,	set80211clone_wdslegacy),
6066 };
6067 static struct afswtch af_ieee80211 = {
6068 	.af_name	= "af_ieee80211",
6069 	.af_af		= AF_UNSPEC,
6070 	.af_other_status = ieee80211_status,
6071 };
6072 
6073 static __constructor void
6074 ieee80211_ctor(void)
6075 {
6076 	for (size_t i = 0; i < nitems(ieee80211_cmds);  i++)
6077 		cmd_register(&ieee80211_cmds[i]);
6078 	af_register(&af_ieee80211);
6079 	clone_setdefcallback_prefix("wlan", wlan_create);
6080 }
6081