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