xref: /freebsd/sbin/ifconfig/ifieee80211.c (revision b17b639832e707aab0e9514cf94727498e2d67bd)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright 2001 The Aerospace Corporation.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of The Aerospace Corporation may not be used to endorse or
15  *    promote products derived from this software.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 /*-
33  * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
34  * All rights reserved.
35  *
36  * This code is derived from software contributed to The NetBSD Foundation
37  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
38  * NASA Ames Research Center.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
50  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
53  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59  * POSSIBILITY OF SUCH DAMAGE.
60  */
61 
62 #include <sys/param.h>
63 #include <sys/ioctl.h>
64 #include <sys/socket.h>
65 #include <sys/sysctl.h>
66 #include <sys/time.h>
67 
68 #include <net/ethernet.h>
69 #include <net/if.h>
70 #include <net/if_dl.h>
71 #include <net/if_types.h>
72 #include <net/if_media.h>
73 #include <net/route.h>
74 
75 #include <net80211/ieee80211_ioctl.h>
76 #include <net80211/ieee80211_freebsd.h>
77 #include <net80211/ieee80211_superg.h>
78 #include <net80211/ieee80211_tdma.h>
79 #include <net80211/ieee80211_mesh.h>
80 #include <net80211/ieee80211_wps.h>
81 
82 #include <assert.h>
83 #include <ctype.h>
84 #include <err.h>
85 #include <errno.h>
86 #include <fcntl.h>
87 #include <inttypes.h>
88 #include <stdio.h>
89 #include <stdlib.h>
90 #include <string.h>
91 #include <unistd.h>
92 #include <stdarg.h>
93 #include <stddef.h>		/* NB: for offsetof */
94 #include <locale.h>
95 #include <langinfo.h>
96 
97 #include "ifconfig.h"
98 
99 #include <lib80211/lib80211_regdomain.h>
100 #include <lib80211/lib80211_ioctl.h>
101 
102 #ifndef IEEE80211_FIXED_RATE_NONE
103 #define	IEEE80211_FIXED_RATE_NONE	0xff
104 #endif
105 
106 /* XXX need these publicly defined or similar */
107 #ifndef IEEE80211_NODE_AUTH
108 #define	IEEE80211_NODE_AUTH	0x000001	/* authorized for data */
109 #define	IEEE80211_NODE_QOS	0x000002	/* QoS enabled */
110 #define	IEEE80211_NODE_ERP	0x000004	/* ERP enabled */
111 #define	IEEE80211_NODE_PWR_MGT	0x000010	/* power save mode enabled */
112 #define	IEEE80211_NODE_AREF	0x000020	/* authentication ref held */
113 #define	IEEE80211_NODE_HT	0x000040	/* HT enabled */
114 #define	IEEE80211_NODE_HTCOMPAT	0x000080	/* HT setup w/ vendor OUI's */
115 #define	IEEE80211_NODE_WPS	0x000100	/* WPS association */
116 #define	IEEE80211_NODE_TSN	0x000200	/* TSN association */
117 #define	IEEE80211_NODE_AMPDU_RX	0x000400	/* AMPDU rx enabled */
118 #define	IEEE80211_NODE_AMPDU_TX	0x000800	/* AMPDU tx enabled */
119 #define	IEEE80211_NODE_MIMO_PS	0x001000	/* MIMO power save enabled */
120 #define	IEEE80211_NODE_MIMO_RTS	0x002000	/* send RTS in MIMO PS */
121 #define	IEEE80211_NODE_RIFS	0x004000	/* RIFS enabled */
122 #define	IEEE80211_NODE_SGI20	0x008000	/* Short GI in HT20 enabled */
123 #define	IEEE80211_NODE_SGI40	0x010000	/* Short GI in HT40 enabled */
124 #define	IEEE80211_NODE_ASSOCID	0x020000	/* xmit requires associd */
125 #define	IEEE80211_NODE_AMSDU_RX	0x040000	/* AMSDU rx enabled */
126 #define	IEEE80211_NODE_AMSDU_TX	0x080000	/* AMSDU tx enabled */
127 #define	IEEE80211_NODE_VHT	0x100000	/* VHT enabled */
128 #define	IEEE80211_NODE_LDPC	0x200000	/* LDPC enabled */
129 #define	IEEE80211_NODE_UAPSD	0x400000	/* UAPSD enabled */
130 #endif
131 
132 /* XXX should also figure out where to put these for k/u-space sharing. */
133 #ifndef IEEE80211_FVHT_VHT
134 #define	IEEE80211_FVHT_VHT	0x000000001	/* CONF: VHT supported */
135 #define	IEEE80211_FVHT_USEVHT40	0x000000002	/* CONF: Use VHT40 */
136 #define	IEEE80211_FVHT_USEVHT80	0x000000004	/* CONF: Use VHT80 */
137 #define	IEEE80211_FVHT_USEVHT160 0x000000008	/* CONF: Use VHT160 */
138 #define	IEEE80211_FVHT_USEVHT80P80 0x000000010	/* CONF: Use VHT 80+80 */
139 #endif
140 
141 /* Helper macros unified. */
142 #ifndef	_IEEE80211_MASKSHIFT
143 #define	_IEEE80211_MASKSHIFT(_v, _f)	(((_v) & _f) >> _f##_S)
144 #endif
145 #ifndef	_IEEE80211_SHIFTMASK
146 #define	_IEEE80211_SHIFTMASK(_v, _f)	(((_v) << _f##_S) & _f)
147 #endif
148 
149 #define	MAXCHAN	1536		/* max 1.5K channels */
150 
151 #define	MAXCOL	78
152 static	int col;
153 static	char spacer;
154 
155 static void LINE_INIT(char c);
156 static void LINE_BREAK(void);
157 static void LINE_CHECK(const char *fmt, ...);
158 
159 static const char *modename[IEEE80211_MODE_MAX] = {
160 	[IEEE80211_MODE_AUTO]	  = "auto",
161 	[IEEE80211_MODE_11A]	  = "11a",
162 	[IEEE80211_MODE_11B]	  = "11b",
163 	[IEEE80211_MODE_11G]	  = "11g",
164 	[IEEE80211_MODE_FH]	  = "fh",
165 	[IEEE80211_MODE_TURBO_A]  = "turboA",
166 	[IEEE80211_MODE_TURBO_G]  = "turboG",
167 	[IEEE80211_MODE_STURBO_A] = "sturbo",
168 	[IEEE80211_MODE_11NA]	  = "11na",
169 	[IEEE80211_MODE_11NG]	  = "11ng",
170 	[IEEE80211_MODE_HALF]	  = "half",
171 	[IEEE80211_MODE_QUARTER]  = "quarter",
172 	[IEEE80211_MODE_VHT_2GHZ] = "11acg",
173 	[IEEE80211_MODE_VHT_5GHZ] = "11ac",
174 };
175 
176 static void set80211(int s, int type, int val, int len, void *data);
177 static int get80211(int s, int type, void *data, int len);
178 static int get80211len(int s, int type, void *data, int len, int *plen);
179 static int get80211val(int s, int type, int *val);
180 static const char *get_string(const char *val, const char *sep,
181     u_int8_t *buf, int *lenp);
182 static void print_string(const u_int8_t *buf, int len);
183 static void print_regdomain(const struct ieee80211_regdomain *, int);
184 static void print_channels(int, const struct ieee80211req_chaninfo *,
185     int allchans, int verbose);
186 static void regdomain_makechannels(struct ieee80211_regdomain_req *,
187     const struct ieee80211_devcaps_req *);
188 static const char *mesh_linkstate_string(uint8_t state);
189 
190 static struct ieee80211req_chaninfo *chaninfo;
191 static struct ieee80211_regdomain regdomain;
192 static int gotregdomain = 0;
193 static struct ieee80211_roamparams_req roamparams;
194 static int gotroam = 0;
195 static struct ieee80211_txparams_req txparams;
196 static int gottxparams = 0;
197 static struct ieee80211_channel curchan;
198 static int gotcurchan = 0;
199 static struct ifmediareq *ifmr;
200 static int htconf = 0;
201 static	int gothtconf = 0;
202 
203 static void
204 gethtconf(int s)
205 {
206 	if (gothtconf)
207 		return;
208 	if (get80211val(s, IEEE80211_IOC_HTCONF, &htconf) < 0)
209 		warn("unable to get HT configuration information");
210 	gothtconf = 1;
211 }
212 
213 /* VHT */
214 static int vhtconf = 0;
215 static	int gotvhtconf = 0;
216 
217 static void
218 getvhtconf(int s)
219 {
220 	if (gotvhtconf)
221 		return;
222 	if (get80211val(s, IEEE80211_IOC_VHTCONF, &vhtconf) < 0)
223 		warn("unable to get VHT configuration information");
224 	gotvhtconf = 1;
225 }
226 
227 /*
228  * Collect channel info from the kernel.  We use this (mostly)
229  * to handle mapping between frequency and IEEE channel number.
230  */
231 static void
232 getchaninfo(int s)
233 {
234 	if (chaninfo != NULL)
235 		return;
236 	chaninfo = malloc(IEEE80211_CHANINFO_SIZE(MAXCHAN));
237 	if (chaninfo == NULL)
238 		errx(1, "no space for channel list");
239 	if (get80211(s, IEEE80211_IOC_CHANINFO, chaninfo,
240 	    IEEE80211_CHANINFO_SIZE(MAXCHAN)) < 0)
241 		err(1, "unable to get channel information");
242 	ifmr = ifmedia_getstate();
243 	gethtconf(s);
244 	getvhtconf(s);
245 }
246 
247 static struct regdata *
248 getregdata(void)
249 {
250 	static struct regdata *rdp = NULL;
251 	if (rdp == NULL) {
252 		rdp = lib80211_alloc_regdata();
253 		if (rdp == NULL)
254 			errx(-1, "missing or corrupted regdomain database");
255 	}
256 	return rdp;
257 }
258 
259 /*
260  * Given the channel at index i with attributes from,
261  * check if there is a channel with attributes to in
262  * the channel table.  With suitable attributes this
263  * allows the caller to look for promotion; e.g. from
264  * 11b > 11g.
265  */
266 static int
267 canpromote(unsigned int i, uint32_t from, uint32_t to)
268 {
269 	const struct ieee80211_channel *fc = &chaninfo->ic_chans[i];
270 	u_int j;
271 
272 	if ((fc->ic_flags & from) != from)
273 		return i;
274 	/* NB: quick check exploiting ordering of chans w/ same frequency */
275 	if (i+1 < chaninfo->ic_nchans &&
276 	    chaninfo->ic_chans[i+1].ic_freq == fc->ic_freq &&
277 	    (chaninfo->ic_chans[i+1].ic_flags & to) == to)
278 		return i+1;
279 	/* brute force search in case channel list is not ordered */
280 	for (j = 0; j < chaninfo->ic_nchans; j++) {
281 		const struct ieee80211_channel *tc = &chaninfo->ic_chans[j];
282 		if (j != i &&
283 		    tc->ic_freq == fc->ic_freq && (tc->ic_flags & to) == to)
284 		return j;
285 	}
286 	return i;
287 }
288 
289 /*
290  * Handle channel promotion.  When a channel is specified with
291  * only a frequency we want to promote it to the ``best'' channel
292  * available.  The channel list has separate entries for 11b, 11g,
293  * 11a, and 11n[ga] channels so specifying a frequency w/o any
294  * attributes requires we upgrade, e.g. from 11b -> 11g.  This
295  * gets complicated when the channel is specified on the same
296  * command line with a media request that constrains the available
297  * channe list (e.g. mode 11a); we want to honor that to avoid
298  * confusing behaviour.
299  */
300 /*
301  * XXX VHT
302  */
303 static int
304 promote(unsigned int i)
305 {
306 	/*
307 	 * Query the current mode of the interface in case it's
308 	 * constrained (e.g. to 11a).  We must do this carefully
309 	 * as there may be a pending ifmedia request in which case
310 	 * asking the kernel will give us the wrong answer.  This
311 	 * is an unfortunate side-effect of the way ifconfig is
312 	 * structure for modularity (yech).
313 	 *
314 	 * NB: ifmr is actually setup in getchaninfo (above); we
315 	 *     assume it's called coincident with to this call so
316 	 *     we have a ``current setting''; otherwise we must pass
317 	 *     the socket descriptor down to here so we can make
318 	 *     the ifmedia_getstate call ourselves.
319 	 */
320 	int chanmode = ifmr != NULL ? IFM_MODE(ifmr->ifm_current) : IFM_AUTO;
321 
322 	/* when ambiguous promote to ``best'' */
323 	/* NB: we abitrarily pick HT40+ over HT40- */
324 	if (chanmode != IFM_IEEE80211_11B)
325 		i = canpromote(i, IEEE80211_CHAN_B, IEEE80211_CHAN_G);
326 	if (chanmode != IFM_IEEE80211_11G && (htconf & 1)) {
327 		i = canpromote(i, IEEE80211_CHAN_G,
328 			IEEE80211_CHAN_G | IEEE80211_CHAN_HT20);
329 		if (htconf & 2) {
330 			i = canpromote(i, IEEE80211_CHAN_G,
331 				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D);
332 			i = canpromote(i, IEEE80211_CHAN_G,
333 				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U);
334 		}
335 	}
336 	if (chanmode != IFM_IEEE80211_11A && (htconf & 1)) {
337 		i = canpromote(i, IEEE80211_CHAN_A,
338 			IEEE80211_CHAN_A | IEEE80211_CHAN_HT20);
339 		if (htconf & 2) {
340 			i = canpromote(i, IEEE80211_CHAN_A,
341 				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D);
342 			i = canpromote(i, IEEE80211_CHAN_A,
343 				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U);
344 		}
345 	}
346 	return i;
347 }
348 
349 static void
350 mapfreq(struct ieee80211_channel *chan, uint16_t freq, unsigned int flags)
351 {
352 	u_int i;
353 
354 	for (i = 0; i < chaninfo->ic_nchans; i++) {
355 		const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
356 
357 		if (c->ic_freq == freq && (c->ic_flags & flags) == flags) {
358 			if (flags == 0) {
359 				/* when ambiguous promote to ``best'' */
360 				c = &chaninfo->ic_chans[promote(i)];
361 			}
362 			*chan = *c;
363 			return;
364 		}
365 	}
366 	errx(1, "unknown/undefined frequency %u/0x%x", freq, flags);
367 }
368 
369 static void
370 mapchan(struct ieee80211_channel *chan, uint8_t ieee, unsigned int flags)
371 {
372 	u_int i;
373 
374 	for (i = 0; i < chaninfo->ic_nchans; i++) {
375 		const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
376 
377 		if (c->ic_ieee == ieee && (c->ic_flags & flags) == flags) {
378 			if (flags == 0) {
379 				/* when ambiguous promote to ``best'' */
380 				c = &chaninfo->ic_chans[promote(i)];
381 			}
382 			*chan = *c;
383 			return;
384 		}
385 	}
386 	errx(1, "unknown/undefined channel number %d flags 0x%x", ieee, flags);
387 }
388 
389 static const struct ieee80211_channel *
390 getcurchan(int s)
391 {
392 	if (gotcurchan)
393 		return &curchan;
394 	if (get80211(s, IEEE80211_IOC_CURCHAN, &curchan, sizeof(curchan)) < 0) {
395 		int val;
396 		/* fall back to legacy ioctl */
397 		if (get80211val(s, IEEE80211_IOC_CHANNEL, &val) < 0)
398 			err(-1, "cannot figure out current channel");
399 		getchaninfo(s);
400 		mapchan(&curchan, val, 0);
401 	}
402 	gotcurchan = 1;
403 	return &curchan;
404 }
405 
406 static enum ieee80211_phymode
407 chan2mode(const struct ieee80211_channel *c)
408 {
409 	if (IEEE80211_IS_CHAN_VHTA(c))
410 		return IEEE80211_MODE_VHT_5GHZ;
411 	if (IEEE80211_IS_CHAN_VHTG(c))
412 		return IEEE80211_MODE_VHT_2GHZ;
413 	if (IEEE80211_IS_CHAN_HTA(c))
414 		return IEEE80211_MODE_11NA;
415 	if (IEEE80211_IS_CHAN_HTG(c))
416 		return IEEE80211_MODE_11NG;
417 	if (IEEE80211_IS_CHAN_108A(c))
418 		return IEEE80211_MODE_TURBO_A;
419 	if (IEEE80211_IS_CHAN_108G(c))
420 		return IEEE80211_MODE_TURBO_G;
421 	if (IEEE80211_IS_CHAN_ST(c))
422 		return IEEE80211_MODE_STURBO_A;
423 	if (IEEE80211_IS_CHAN_FHSS(c))
424 		return IEEE80211_MODE_FH;
425 	if (IEEE80211_IS_CHAN_HALF(c))
426 		return IEEE80211_MODE_HALF;
427 	if (IEEE80211_IS_CHAN_QUARTER(c))
428 		return IEEE80211_MODE_QUARTER;
429 	if (IEEE80211_IS_CHAN_A(c))
430 		return IEEE80211_MODE_11A;
431 	if (IEEE80211_IS_CHAN_ANYG(c))
432 		return IEEE80211_MODE_11G;
433 	if (IEEE80211_IS_CHAN_B(c))
434 		return IEEE80211_MODE_11B;
435 	return IEEE80211_MODE_AUTO;
436 }
437 
438 static void
439 getroam(int s)
440 {
441 	if (gotroam)
442 		return;
443 	if (get80211(s, IEEE80211_IOC_ROAM,
444 	    &roamparams, sizeof(roamparams)) < 0)
445 		err(1, "unable to get roaming parameters");
446 	gotroam = 1;
447 }
448 
449 static void
450 setroam_cb(int s, void *arg)
451 {
452 	struct ieee80211_roamparams_req *roam = arg;
453 	set80211(s, IEEE80211_IOC_ROAM, 0, sizeof(*roam), roam);
454 }
455 
456 static void
457 gettxparams(int s)
458 {
459 	if (gottxparams)
460 		return;
461 	if (get80211(s, IEEE80211_IOC_TXPARAMS,
462 	    &txparams, sizeof(txparams)) < 0)
463 		err(1, "unable to get transmit parameters");
464 	gottxparams = 1;
465 }
466 
467 static void
468 settxparams_cb(int s, void *arg)
469 {
470 	struct ieee80211_txparams_req *txp = arg;
471 	set80211(s, IEEE80211_IOC_TXPARAMS, 0, sizeof(*txp), txp);
472 }
473 
474 static void
475 getregdomain(int s)
476 {
477 	if (gotregdomain)
478 		return;
479 	if (get80211(s, IEEE80211_IOC_REGDOMAIN,
480 	    &regdomain, sizeof(regdomain)) < 0)
481 		err(1, "unable to get regulatory domain info");
482 	gotregdomain = 1;
483 }
484 
485 static void
486 getdevcaps(int s, struct ieee80211_devcaps_req *dc)
487 {
488 	if (get80211(s, IEEE80211_IOC_DEVCAPS, dc,
489 	    IEEE80211_DEVCAPS_SPACE(dc)) < 0)
490 		err(1, "unable to get device capabilities");
491 }
492 
493 static void
494 setregdomain_cb(int s, void *arg)
495 {
496 	struct ieee80211_regdomain_req *req;
497 	struct ieee80211_regdomain *rd = arg;
498 	struct ieee80211_devcaps_req *dc;
499 	struct regdata *rdp = getregdata();
500 
501 	if (rd->country != NO_COUNTRY) {
502 		const struct country *cc;
503 		/*
504 		 * Check current country seting to make sure it's
505 		 * compatible with the new regdomain.  If not, then
506 		 * override it with any default country for this
507 		 * SKU.  If we cannot arrange a match, then abort.
508 		 */
509 		cc = lib80211_country_findbycc(rdp, rd->country);
510 		if (cc == NULL)
511 			errx(1, "unknown ISO country code %d", rd->country);
512 		if (cc->rd->sku != rd->regdomain) {
513 			const struct regdomain *rp;
514 			/*
515 			 * Check if country is incompatible with regdomain.
516 			 * To enable multiple regdomains for a country code
517 			 * we permit a mismatch between the regdomain and
518 			 * the country's associated regdomain when the
519 			 * regdomain is setup w/o a default country.  For
520 			 * example, US is bound to the FCC regdomain but
521 			 * we allow US to be combined with FCC3 because FCC3
522 			 * has not default country.  This allows bogus
523 			 * combinations like FCC3+DK which are resolved when
524 			 * constructing the channel list by deferring to the
525 			 * regdomain to construct the channel list.
526 			 */
527 			rp = lib80211_regdomain_findbysku(rdp, rd->regdomain);
528 			if (rp == NULL)
529 				errx(1, "country %s (%s) is not usable with "
530 				    "regdomain %d", cc->isoname, cc->name,
531 				    rd->regdomain);
532 			else if (rp->cc != NULL && rp->cc != cc)
533 				errx(1, "country %s (%s) is not usable with "
534 				   "regdomain %s", cc->isoname, cc->name,
535 				   rp->name);
536 		}
537 	}
538 	/*
539 	 * Fetch the device capabilities and calculate the
540 	 * full set of netbands for which we request a new
541 	 * channel list be constructed.  Once that's done we
542 	 * push the regdomain info + channel list to the kernel.
543 	 */
544 	dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
545 	if (dc == NULL)
546 		errx(1, "no space for device capabilities");
547 	dc->dc_chaninfo.ic_nchans = MAXCHAN;
548 	getdevcaps(s, dc);
549 #if 0
550 	if (verbose) {
551 		printf("drivercaps: 0x%x\n", dc->dc_drivercaps);
552 		printf("cryptocaps: 0x%x\n", dc->dc_cryptocaps);
553 		printf("htcaps    : 0x%x\n", dc->dc_htcaps);
554 		printf("vhtcaps   : 0x%x\n", dc->dc_vhtcaps);
555 #if 0
556 		memcpy(chaninfo, &dc->dc_chaninfo,
557 		    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
558 		print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, 1/*verbose*/);
559 #endif
560 	}
561 #endif
562 	req = malloc(IEEE80211_REGDOMAIN_SIZE(dc->dc_chaninfo.ic_nchans));
563 	if (req == NULL)
564 		errx(1, "no space for regdomain request");
565 	req->rd = *rd;
566 	regdomain_makechannels(req, dc);
567 	if (verbose) {
568 		LINE_INIT(':');
569 		print_regdomain(rd, 1/*verbose*/);
570 		LINE_BREAK();
571 		/* blech, reallocate channel list for new data */
572 		if (chaninfo != NULL)
573 			free(chaninfo);
574 		chaninfo = malloc(IEEE80211_CHANINFO_SPACE(&req->chaninfo));
575 		if (chaninfo == NULL)
576 			errx(1, "no space for channel list");
577 		memcpy(chaninfo, &req->chaninfo,
578 		    IEEE80211_CHANINFO_SPACE(&req->chaninfo));
579 		print_channels(s, &req->chaninfo, 1/*allchans*/, 1/*verbose*/);
580 	}
581 	if (req->chaninfo.ic_nchans == 0)
582 		errx(1, "no channels calculated");
583 	set80211(s, IEEE80211_IOC_REGDOMAIN, 0,
584 	    IEEE80211_REGDOMAIN_SPACE(req), req);
585 	free(req);
586 	free(dc);
587 }
588 
589 static int
590 ieee80211_mhz2ieee(int freq, int flags)
591 {
592 	struct ieee80211_channel chan;
593 	mapfreq(&chan, freq, flags);
594 	return chan.ic_ieee;
595 }
596 
597 static int
598 isanyarg(const char *arg)
599 {
600 	return (strncmp(arg, "-", 1) == 0 ||
601 	    strncasecmp(arg, "any", 3) == 0 || strncasecmp(arg, "off", 3) == 0);
602 }
603 
604 static void
605 set80211ssid(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(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 
2180 	hi_adj = (chanFlags & IEEE80211_CHAN_HT40U) ? -20 : 0;
2181 	lo_adj = (chanFlags & IEEE80211_CHAN_HT40D) ? 20 : 0;
2182 	channelSep = (chanFlags & IEEE80211_CHAN_2GHZ) ? 0 : 40;
2183 
2184 	LIST_FOREACH(nb, bands, next) {
2185 		b = nb->band;
2186 		if (verbose) {
2187 			printf("%s:", __func__);
2188 			printb(" chanFlags", chanFlags, IEEE80211_CHAN_BITS);
2189 			printb(" bandFlags", nb->flags | b->flags,
2190 			    IEEE80211_CHAN_BITS);
2191 			putchar('\n');
2192 		}
2193 		prev = NULL;
2194 
2195 		for (freq = b->freqStart + lo_adj;
2196 		     freq <= b->freqEnd + hi_adj; freq += b->chanSep) {
2197 			/*
2198 			 * Construct flags for the new channel.  We take
2199 			 * the attributes from the band descriptions except
2200 			 * for HT40 which is enabled generically (i.e. +/-
2201 			 * extension channel) in the band description and
2202 			 * then constrained according by channel separation.
2203 			 */
2204 			flags = nb->flags | b->flags;
2205 
2206 			/*
2207 			 * VHT first - HT is a subset.
2208 			 */
2209 			if (flags & IEEE80211_CHAN_VHT) {
2210 				if ((chanFlags & IEEE80211_CHAN_VHT20) &&
2211 				    (flags & IEEE80211_CHAN_VHT20) == 0) {
2212 					if (verbose)
2213 						printf("%u: skip, not a "
2214 						    "VHT20 channel\n", freq);
2215 					continue;
2216 				}
2217 				if ((chanFlags & IEEE80211_CHAN_VHT40) &&
2218 				    (flags & IEEE80211_CHAN_VHT40) == 0) {
2219 					if (verbose)
2220 						printf("%u: skip, not a "
2221 						    "VHT40 channel\n", freq);
2222 					continue;
2223 				}
2224 				if ((chanFlags & IEEE80211_CHAN_VHT80) &&
2225 				    (flags & IEEE80211_CHAN_VHT80) == 0) {
2226 					if (verbose)
2227 						printf("%u: skip, not a "
2228 						    "VHT80 channel\n", freq);
2229 					continue;
2230 				}
2231 				if ((chanFlags & IEEE80211_CHAN_VHT160) &&
2232 				    (flags & IEEE80211_CHAN_VHT160) == 0) {
2233 					if (verbose)
2234 						printf("%u: skip, not a "
2235 						    "VHT160 channel\n", freq);
2236 					continue;
2237 				}
2238 				if ((chanFlags & IEEE80211_CHAN_VHT80P80) &&
2239 				    (flags & IEEE80211_CHAN_VHT80P80) == 0) {
2240 					if (verbose)
2241 						printf("%u: skip, not a "
2242 						    "VHT80+80 channel\n", freq);
2243 					continue;
2244 				}
2245 				flags &= ~IEEE80211_CHAN_VHT;
2246 				flags |= chanFlags & IEEE80211_CHAN_VHT;
2247 			}
2248 
2249 			/* Now, constrain HT */
2250 			if (flags & IEEE80211_CHAN_HT) {
2251 				/*
2252 				 * HT channels are generated specially; we're
2253 				 * called to add HT20, HT40+, and HT40- chan's
2254 				 * so we need to expand only band specs for
2255 				 * the HT channel type being added.
2256 				 */
2257 				if ((chanFlags & IEEE80211_CHAN_HT20) &&
2258 				    (flags & IEEE80211_CHAN_HT20) == 0) {
2259 					if (verbose)
2260 						printf("%u: skip, not an "
2261 						    "HT20 channel\n", freq);
2262 					continue;
2263 				}
2264 				if ((chanFlags & IEEE80211_CHAN_HT40) &&
2265 				    (flags & IEEE80211_CHAN_HT40) == 0) {
2266 					if (verbose)
2267 						printf("%u: skip, not an "
2268 						    "HT40 channel\n", freq);
2269 					continue;
2270 				}
2271 				/* NB: HT attribute comes from caller */
2272 				flags &= ~IEEE80211_CHAN_HT;
2273 				flags |= chanFlags & IEEE80211_CHAN_HT;
2274 			}
2275 			/*
2276 			 * Check if device can operate on this frequency.
2277 			 */
2278 			if (!checkchan(avail, freq, flags)) {
2279 				if (verbose) {
2280 					printf("%u: skip, ", freq);
2281 					printb("flags", flags,
2282 					    IEEE80211_CHAN_BITS);
2283 					printf(" not available\n");
2284 				}
2285 				continue;
2286 			}
2287 			if ((flags & REQ_ECM) && !reg->ecm) {
2288 				if (verbose)
2289 					printf("%u: skip, ECM channel\n", freq);
2290 				continue;
2291 			}
2292 			if ((flags & REQ_INDOOR) && reg->location == 'O') {
2293 				if (verbose)
2294 					printf("%u: skip, indoor channel\n",
2295 					    freq);
2296 				continue;
2297 			}
2298 			if ((flags & REQ_OUTDOOR) && reg->location == 'I') {
2299 				if (verbose)
2300 					printf("%u: skip, outdoor channel\n",
2301 					    freq);
2302 				continue;
2303 			}
2304 			if ((flags & IEEE80211_CHAN_HT40) &&
2305 			    prev != NULL && (freq - prev->ic_freq) < channelSep) {
2306 				if (verbose)
2307 					printf("%u: skip, only %u channel "
2308 					    "separation, need %d\n", freq,
2309 					    freq - prev->ic_freq, channelSep);
2310 				continue;
2311 			}
2312 			if (ci->ic_nchans == IEEE80211_CHAN_MAX) {
2313 				if (verbose)
2314 					printf("%u: skip, channel table full\n",
2315 					    freq);
2316 				break;
2317 			}
2318 			c = &ci->ic_chans[ci->ic_nchans++];
2319 			memset(c, 0, sizeof(*c));
2320 			c->ic_freq = freq;
2321 			c->ic_flags = flags;
2322 		if (c->ic_flags & IEEE80211_CHAN_DFS)
2323 				c->ic_maxregpower = nb->maxPowerDFS;
2324 			else
2325 				c->ic_maxregpower = nb->maxPower;
2326 			if (verbose) {
2327 				printf("[%3d] add freq %u ",
2328 				    ci->ic_nchans-1, c->ic_freq);
2329 				printb("flags", c->ic_flags, IEEE80211_CHAN_BITS);
2330 				printf(" power %u\n", c->ic_maxregpower);
2331 			}
2332 			/* NB: kernel fills in other fields */
2333 			prev = c;
2334 		}
2335 	}
2336 }
2337 
2338 static void
2339 regdomain_makechannels(
2340 	struct ieee80211_regdomain_req *req,
2341 	const struct ieee80211_devcaps_req *dc)
2342 {
2343 	struct regdata *rdp = getregdata();
2344 	const struct country *cc;
2345 	const struct ieee80211_regdomain *reg = &req->rd;
2346 	struct ieee80211req_chaninfo *ci = &req->chaninfo;
2347 	const struct regdomain *rd;
2348 
2349 	/*
2350 	 * Locate construction table for new channel list.  We treat
2351 	 * the regdomain/SKU as definitive so a country can be in
2352 	 * multiple with different properties (e.g. US in FCC+FCC3).
2353 	 * If no regdomain is specified then we fallback on the country
2354 	 * code to find the associated regdomain since countries always
2355 	 * belong to at least one regdomain.
2356 	 */
2357 	if (reg->regdomain == 0) {
2358 		cc = lib80211_country_findbycc(rdp, reg->country);
2359 		if (cc == NULL)
2360 			errx(1, "internal error, country %d not found",
2361 			    reg->country);
2362 		rd = cc->rd;
2363 	} else
2364 		rd = lib80211_regdomain_findbysku(rdp, reg->regdomain);
2365 	if (rd == NULL)
2366 		errx(1, "internal error, regdomain %d not found",
2367 			    reg->regdomain);
2368 	if (rd->sku != SKU_DEBUG) {
2369 		/*
2370 		 * regdomain_addchans incrememnts the channel count for
2371 		 * each channel it adds so initialize ic_nchans to zero.
2372 		 * Note that we know we have enough space to hold all possible
2373 		 * channels because the devcaps list size was used to
2374 		 * allocate our request.
2375 		 */
2376 		ci->ic_nchans = 0;
2377 		if (!LIST_EMPTY(&rd->bands_11b))
2378 			regdomain_addchans(ci, &rd->bands_11b, reg,
2379 			    IEEE80211_CHAN_B, &dc->dc_chaninfo);
2380 		if (!LIST_EMPTY(&rd->bands_11g))
2381 			regdomain_addchans(ci, &rd->bands_11g, reg,
2382 			    IEEE80211_CHAN_G, &dc->dc_chaninfo);
2383 		if (!LIST_EMPTY(&rd->bands_11a))
2384 			regdomain_addchans(ci, &rd->bands_11a, reg,
2385 			    IEEE80211_CHAN_A, &dc->dc_chaninfo);
2386 		if (!LIST_EMPTY(&rd->bands_11na) && dc->dc_htcaps != 0) {
2387 			regdomain_addchans(ci, &rd->bands_11na, reg,
2388 			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT20,
2389 			    &dc->dc_chaninfo);
2390 			if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2391 				regdomain_addchans(ci, &rd->bands_11na, reg,
2392 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U,
2393 				    &dc->dc_chaninfo);
2394 				regdomain_addchans(ci, &rd->bands_11na, reg,
2395 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D,
2396 				    &dc->dc_chaninfo);
2397 			}
2398 		}
2399 		if (!LIST_EMPTY(&rd->bands_11ac) && dc->dc_vhtcaps != 0) {
2400 			regdomain_addchans(ci, &rd->bands_11ac, reg,
2401 			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT20 |
2402 			    IEEE80211_CHAN_VHT20,
2403 			    &dc->dc_chaninfo);
2404 
2405 			/* VHT40 is a function of HT40.. */
2406 			if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2407 				regdomain_addchans(ci, &rd->bands_11ac, reg,
2408 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
2409 				    IEEE80211_CHAN_VHT40U,
2410 				    &dc->dc_chaninfo);
2411 				regdomain_addchans(ci, &rd->bands_11ac, reg,
2412 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
2413 				    IEEE80211_CHAN_VHT40D,
2414 				    &dc->dc_chaninfo);
2415 			}
2416 
2417 			/* VHT80 is mandatory (and so should be VHT40 above). */
2418 			if (1) {
2419 				regdomain_addchans(ci, &rd->bands_11ac, reg,
2420 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
2421 				    IEEE80211_CHAN_VHT80,
2422 				    &dc->dc_chaninfo);
2423 				regdomain_addchans(ci, &rd->bands_11ac, reg,
2424 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
2425 				    IEEE80211_CHAN_VHT80,
2426 				    &dc->dc_chaninfo);
2427 			}
2428 
2429 			/* VHT160 */
2430 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
2431 			    dc->dc_vhtcaps)) {
2432 				regdomain_addchans(ci, &rd->bands_11ac, reg,
2433 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
2434 				    IEEE80211_CHAN_VHT160,
2435 				    &dc->dc_chaninfo);
2436 				regdomain_addchans(ci, &rd->bands_11ac, reg,
2437 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
2438 				    IEEE80211_CHAN_VHT160,
2439 				    &dc->dc_chaninfo);
2440 			}
2441 
2442 			/* VHT80P80 */
2443 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
2444 			    dc->dc_vhtcaps)) {
2445 				regdomain_addchans(ci, &rd->bands_11ac, reg,
2446 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
2447 				    IEEE80211_CHAN_VHT80P80,
2448 				    &dc->dc_chaninfo);
2449 				regdomain_addchans(ci, &rd->bands_11ac, reg,
2450 				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
2451 				    IEEE80211_CHAN_VHT80P80,
2452 				    &dc->dc_chaninfo);
2453 			}
2454 		}
2455 
2456 		if (!LIST_EMPTY(&rd->bands_11ng) && dc->dc_htcaps != 0) {
2457 			regdomain_addchans(ci, &rd->bands_11ng, reg,
2458 			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT20,
2459 			    &dc->dc_chaninfo);
2460 			if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2461 				regdomain_addchans(ci, &rd->bands_11ng, reg,
2462 				    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U,
2463 				    &dc->dc_chaninfo);
2464 				regdomain_addchans(ci, &rd->bands_11ng, reg,
2465 				    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D,
2466 				    &dc->dc_chaninfo);
2467 			}
2468 		}
2469 		qsort(ci->ic_chans, ci->ic_nchans, sizeof(ci->ic_chans[0]),
2470 		    regdomain_sort);
2471 	} else
2472 		memcpy(ci, &dc->dc_chaninfo,
2473 		    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
2474 }
2475 
2476 static void
2477 list_countries(void)
2478 {
2479 	struct regdata *rdp = getregdata();
2480 	const struct country *cp;
2481 	const struct regdomain *dp;
2482 	int i;
2483 
2484 	i = 0;
2485 	printf("\nCountry codes:\n");
2486 	LIST_FOREACH(cp, &rdp->countries, next) {
2487 		printf("%2s %-15.15s%s", cp->isoname,
2488 		    cp->name, ((i+1)%4) == 0 ? "\n" : " ");
2489 		i++;
2490 	}
2491 	i = 0;
2492 	printf("\nRegulatory domains:\n");
2493 	LIST_FOREACH(dp, &rdp->domains, next) {
2494 		printf("%-15.15s%s", dp->name, ((i+1)%4) == 0 ? "\n" : " ");
2495 		i++;
2496 	}
2497 	printf("\n");
2498 }
2499 
2500 static void
2501 defaultcountry(const struct regdomain *rd)
2502 {
2503 	struct regdata *rdp = getregdata();
2504 	const struct country *cc;
2505 
2506 	cc = lib80211_country_findbycc(rdp, rd->cc->code);
2507 	if (cc == NULL)
2508 		errx(1, "internal error, ISO country code %d not "
2509 		    "defined for regdomain %s", rd->cc->code, rd->name);
2510 	regdomain.country = cc->code;
2511 	regdomain.isocc[0] = cc->isoname[0];
2512 	regdomain.isocc[1] = cc->isoname[1];
2513 }
2514 
2515 static void
2516 set80211regdomain(if_ctx *ctx, const char *val, int dummy __unused)
2517 {
2518 	struct regdata *rdp = getregdata();
2519 	const struct regdomain *rd;
2520 
2521 	rd = lib80211_regdomain_findbyname(rdp, val);
2522 	if (rd == NULL) {
2523 		char *eptr;
2524 		long sku = strtol(val, &eptr, 0);
2525 
2526 		if (eptr != val)
2527 			rd = lib80211_regdomain_findbysku(rdp, sku);
2528 		if (eptr == val || rd == NULL)
2529 			errx(1, "unknown regdomain %s", val);
2530 	}
2531 	getregdomain(ctx->io_s);
2532 	regdomain.regdomain = rd->sku;
2533 	if (regdomain.country == 0 && rd->cc != NULL) {
2534 		/*
2535 		 * No country code setup and there's a default
2536 		 * one for this regdomain fill it in.
2537 		 */
2538 		defaultcountry(rd);
2539 	}
2540 	callback_register(setregdomain_cb, &regdomain);
2541 }
2542 
2543 static void
2544 set80211country(if_ctx *ctx, const char *val, int dummy __unused)
2545 {
2546 	struct regdata *rdp = getregdata();
2547 	const struct country *cc;
2548 
2549 	cc = lib80211_country_findbyname(rdp, val);
2550 	if (cc == NULL) {
2551 		char *eptr;
2552 		long code = strtol(val, &eptr, 0);
2553 
2554 		if (eptr != val)
2555 			cc = lib80211_country_findbycc(rdp, code);
2556 		if (eptr == val || cc == NULL)
2557 			errx(1, "unknown ISO country code %s", val);
2558 	}
2559 	getregdomain(ctx->io_s);
2560 	regdomain.regdomain = cc->rd->sku;
2561 	regdomain.country = cc->code;
2562 	regdomain.isocc[0] = cc->isoname[0];
2563 	regdomain.isocc[1] = cc->isoname[1];
2564 	callback_register(setregdomain_cb, &regdomain);
2565 }
2566 
2567 static void
2568 set80211location(if_ctx *ctx, const char *val __unused, int d)
2569 {
2570 	getregdomain(ctx->io_s);
2571 	regdomain.location = d;
2572 	callback_register(setregdomain_cb, &regdomain);
2573 }
2574 
2575 static void
2576 set80211ecm(if_ctx *ctx, const char *val __unused, int d)
2577 {
2578 	getregdomain(ctx->io_s);
2579 	regdomain.ecm = d;
2580 	callback_register(setregdomain_cb, &regdomain);
2581 }
2582 
2583 static void
2584 LINE_INIT(char c)
2585 {
2586 	spacer = c;
2587 	if (c == '\t')
2588 		col = 8;
2589 	else
2590 		col = 1;
2591 }
2592 
2593 static void
2594 LINE_BREAK(void)
2595 {
2596 	if (spacer != '\t') {
2597 		printf("\n");
2598 		spacer = '\t';
2599 	}
2600 	col = 8;		/* 8-col tab */
2601 }
2602 
2603 static void
2604 LINE_CHECK(const char *fmt, ...)
2605 {
2606 	char buf[80];
2607 	va_list ap;
2608 	int n;
2609 
2610 	va_start(ap, fmt);
2611 	n = vsnprintf(buf+1, sizeof(buf)-1, fmt, ap);
2612 	va_end(ap);
2613 	col += 1+n;
2614 	if (col > MAXCOL) {
2615 		LINE_BREAK();
2616 		col += n;
2617 	}
2618 	buf[0] = spacer;
2619 	printf("%s", buf);
2620 	spacer = ' ';
2621 }
2622 
2623 static int
2624 getmaxrate(const uint8_t rates[15], uint8_t nrates)
2625 {
2626 	int i, maxrate = -1;
2627 
2628 	for (i = 0; i < nrates; i++) {
2629 		int rate = rates[i] & IEEE80211_RATE_VAL;
2630 		if (rate > maxrate)
2631 			maxrate = rate;
2632 	}
2633 	return maxrate / 2;
2634 }
2635 
2636 static const char *
2637 getcaps(int capinfo)
2638 {
2639 	static char capstring[32];
2640 	char *cp = capstring;
2641 
2642 	if (capinfo & IEEE80211_CAPINFO_ESS)
2643 		*cp++ = 'E';
2644 	if (capinfo & IEEE80211_CAPINFO_IBSS)
2645 		*cp++ = 'I';
2646 	if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
2647 		*cp++ = 'c';
2648 	if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
2649 		*cp++ = 'C';
2650 	if (capinfo & IEEE80211_CAPINFO_PRIVACY)
2651 		*cp++ = 'P';
2652 	if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
2653 		*cp++ = 'S';
2654 	if (capinfo & IEEE80211_CAPINFO_PBCC)
2655 		*cp++ = 'B';
2656 	if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
2657 		*cp++ = 'A';
2658 	if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
2659 		*cp++ = 's';
2660 	if (capinfo & IEEE80211_CAPINFO_RSN)
2661 		*cp++ = 'R';
2662 	if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
2663 		*cp++ = 'D';
2664 	*cp = '\0';
2665 	return capstring;
2666 }
2667 
2668 static const char *
2669 getflags(int flags)
2670 {
2671 	static char flagstring[32];
2672 	char *cp = flagstring;
2673 
2674 	if (flags & IEEE80211_NODE_AUTH)
2675 		*cp++ = 'A';
2676 	if (flags & IEEE80211_NODE_QOS)
2677 		*cp++ = 'Q';
2678 	if (flags & IEEE80211_NODE_ERP)
2679 		*cp++ = 'E';
2680 	if (flags & IEEE80211_NODE_PWR_MGT)
2681 		*cp++ = 'P';
2682 	if (flags & IEEE80211_NODE_HT) {
2683 		*cp++ = 'H';
2684 		if (flags & IEEE80211_NODE_HTCOMPAT)
2685 			*cp++ = '+';
2686 	}
2687 	if (flags & IEEE80211_NODE_VHT)
2688 		*cp++ = 'V';
2689 	if (flags & IEEE80211_NODE_WPS)
2690 		*cp++ = 'W';
2691 	if (flags & IEEE80211_NODE_TSN)
2692 		*cp++ = 'N';
2693 	if (flags & IEEE80211_NODE_AMPDU_TX)
2694 		*cp++ = 'T';
2695 	if (flags & IEEE80211_NODE_AMPDU_RX)
2696 		*cp++ = 'R';
2697 	if (flags & IEEE80211_NODE_MIMO_PS) {
2698 		*cp++ = 'M';
2699 		if (flags & IEEE80211_NODE_MIMO_RTS)
2700 			*cp++ = '+';
2701 	}
2702 	if (flags & IEEE80211_NODE_RIFS)
2703 		*cp++ = 'I';
2704 	if (flags & IEEE80211_NODE_SGI40) {
2705 		*cp++ = 'S';
2706 		if (flags & IEEE80211_NODE_SGI20)
2707 			*cp++ = '+';
2708 	} else if (flags & IEEE80211_NODE_SGI20)
2709 		*cp++ = 's';
2710 	if (flags & IEEE80211_NODE_AMSDU_TX)
2711 		*cp++ = 't';
2712 	if (flags & IEEE80211_NODE_AMSDU_RX)
2713 		*cp++ = 'r';
2714 	if (flags & IEEE80211_NODE_UAPSD)
2715 		*cp++ = 'U';
2716 	if (flags & IEEE80211_NODE_LDPC)
2717 		*cp++ = 'L';
2718 	*cp = '\0';
2719 	return flagstring;
2720 }
2721 
2722 static void
2723 printie(const char* tag, const uint8_t *ie, size_t ielen, unsigned int maxlen)
2724 {
2725 	printf("%s", tag);
2726 	if (verbose) {
2727 		maxlen -= strlen(tag)+2;
2728 		if (2*ielen > maxlen)
2729 			maxlen--;
2730 		printf("<");
2731 		for (; ielen > 0; ie++, ielen--) {
2732 			if (maxlen-- <= 0)
2733 				break;
2734 			printf("%02x", *ie);
2735 		}
2736 		if (ielen != 0)
2737 			printf("-");
2738 		printf(">");
2739 	}
2740 }
2741 
2742 #define LE_READ_2(p)					\
2743 	((u_int16_t)					\
2744 	 ((((const u_int8_t *)(p))[0]      ) |		\
2745 	  (((const u_int8_t *)(p))[1] <<  8)))
2746 #define LE_READ_4(p)					\
2747 	((u_int32_t)					\
2748 	 ((((const u_int8_t *)(p))[0]      ) |		\
2749 	  (((const u_int8_t *)(p))[1] <<  8) |		\
2750 	  (((const u_int8_t *)(p))[2] << 16) |		\
2751 	  (((const u_int8_t *)(p))[3] << 24)))
2752 
2753 /*
2754  * NB: The decoding routines assume a properly formatted ie
2755  *     which should be safe as the kernel only retains them
2756  *     if they parse ok.
2757  */
2758 
2759 static void
2760 printwmeparam(const char *tag, const u_int8_t *ie)
2761 {
2762 	static const char *acnames[] = { "BE", "BK", "VO", "VI" };
2763 	const struct ieee80211_wme_param *wme =
2764 	    (const struct ieee80211_wme_param *) ie;
2765 	int i;
2766 
2767 	printf("%s", tag);
2768 	if (!verbose)
2769 		return;
2770 	printf("<qosinfo 0x%x", wme->param_qosInfo);
2771 	ie += offsetof(struct ieee80211_wme_param, params_acParams);
2772 	for (i = 0; i < WME_NUM_AC; i++) {
2773 		const struct ieee80211_wme_acparams *ac =
2774 		    &wme->params_acParams[i];
2775 
2776 		printf(" %s[%saifsn %u cwmin %u cwmax %u txop %u]", acnames[i],
2777 		    _IEEE80211_MASKSHIFT(ac->acp_aci_aifsn, WME_PARAM_ACM) ?
2778 			"acm " : "",
2779 		    _IEEE80211_MASKSHIFT(ac->acp_aci_aifsn, WME_PARAM_AIFSN),
2780 		    _IEEE80211_MASKSHIFT(ac->acp_logcwminmax,
2781 			WME_PARAM_LOGCWMIN),
2782 		    _IEEE80211_MASKSHIFT(ac->acp_logcwminmax,
2783 			WME_PARAM_LOGCWMAX),
2784 		    LE_READ_2(&ac->acp_txop));
2785 	}
2786 	printf(">");
2787 }
2788 
2789 static void
2790 printwmeinfo(const char *tag, const u_int8_t *ie)
2791 {
2792 	printf("%s", tag);
2793 	if (verbose) {
2794 		const struct ieee80211_wme_info *wme =
2795 		    (const struct ieee80211_wme_info *) ie;
2796 		printf("<version 0x%x info 0x%x>",
2797 		    wme->wme_version, wme->wme_info);
2798 	}
2799 }
2800 
2801 static void
2802 printvhtcap(const char *tag, const u_int8_t *ie)
2803 {
2804 	printf("%s", tag);
2805 	if (verbose) {
2806 		const struct ieee80211_ie_vhtcap *vhtcap =
2807 		    (const struct ieee80211_ie_vhtcap *) ie;
2808 		uint32_t vhtcap_info = LE_READ_4(&vhtcap->vht_cap_info);
2809 
2810 		printf("<cap 0x%08x", vhtcap_info);
2811 		printf(" rx_mcs_map 0x%x",
2812 		    LE_READ_2(&vhtcap->supp_mcs.rx_mcs_map));
2813 		printf(" rx_highest %d",
2814 		    LE_READ_2(&vhtcap->supp_mcs.rx_highest) & 0x1fff);
2815 		printf(" tx_mcs_map 0x%x",
2816 		    LE_READ_2(&vhtcap->supp_mcs.tx_mcs_map));
2817 		printf(" tx_highest %d",
2818 		    LE_READ_2(&vhtcap->supp_mcs.tx_highest) & 0x1fff);
2819 
2820 		printf(">");
2821 	}
2822 }
2823 
2824 static void
2825 printvhtinfo(const char *tag, const u_int8_t *ie)
2826 {
2827 	printf("%s", tag);
2828 	if (verbose) {
2829 		const struct ieee80211_ie_vht_operation *vhtinfo =
2830 		    (const struct ieee80211_ie_vht_operation *) ie;
2831 
2832 		printf("<chw %d freq1_idx %d freq2_idx %d basic_mcs_set 0x%04x>",
2833 		    vhtinfo->chan_width,
2834 		    vhtinfo->center_freq_seg1_idx,
2835 		    vhtinfo->center_freq_seg2_idx,
2836 		    LE_READ_2(&vhtinfo->basic_mcs_set));
2837 	}
2838 }
2839 
2840 static void
2841 printvhtpwrenv(const char *tag, const u_int8_t *ie, size_t ielen)
2842 {
2843 	printf("%s", tag);
2844 	static const char *txpwrmap[] = {
2845 		"20",
2846 		"40",
2847 		"80",
2848 		"160",
2849 	};
2850 	if (verbose) {
2851 		const struct ieee80211_ie_vht_txpwrenv *vhtpwr =
2852 		    (const struct ieee80211_ie_vht_txpwrenv *) ie;
2853 		size_t i, n;
2854 		const char *sep = "";
2855 
2856 		/* Get count; trim at ielen */
2857 		n = (vhtpwr->tx_info &
2858 		    IEEE80211_VHT_TXPWRENV_INFO_COUNT_MASK) + 1;
2859 		/* Trim at ielen */
2860 		if (n + 3 > ielen)
2861 			n = ielen - 3;
2862 		printf("<tx_info 0x%02x pwr:[", vhtpwr->tx_info);
2863 		for (i = 0; i < n; i++) {
2864 			printf("%s%s:%.2f", sep, txpwrmap[i],
2865 			    ((float) ((int8_t) ie[i+3])) / 2.0);
2866 			sep = " ";
2867 		}
2868 
2869 		printf("]>");
2870 	}
2871 }
2872 
2873 static void
2874 printhtcap(const char *tag, const u_int8_t *ie)
2875 {
2876 	printf("%s", tag);
2877 	if (verbose) {
2878 		const struct ieee80211_ie_htcap *htcap =
2879 		    (const struct ieee80211_ie_htcap *) ie;
2880 		const char *sep;
2881 		int i, j;
2882 
2883 		printf("<cap 0x%x param 0x%x",
2884 		    LE_READ_2(&htcap->hc_cap), htcap->hc_param);
2885 		printf(" mcsset[");
2886 		sep = "";
2887 		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2888 			if (isset(htcap->hc_mcsset, i)) {
2889 				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2890 					if (isclr(htcap->hc_mcsset, j))
2891 						break;
2892 				j--;
2893 				if (i == j)
2894 					printf("%s%u", sep, i);
2895 				else
2896 					printf("%s%u-%u", sep, i, j);
2897 				i += j-i;
2898 				sep = ",";
2899 			}
2900 		printf("] extcap 0x%x txbf 0x%x antenna 0x%x>",
2901 		    LE_READ_2(&htcap->hc_extcap),
2902 		    LE_READ_4(&htcap->hc_txbf),
2903 		    htcap->hc_antenna);
2904 	}
2905 }
2906 
2907 static void
2908 printhtinfo(const char *tag, const u_int8_t *ie)
2909 {
2910 	printf("%s", tag);
2911 	if (verbose) {
2912 		const struct ieee80211_ie_htinfo *htinfo =
2913 		    (const struct ieee80211_ie_htinfo *) ie;
2914 		const char *sep;
2915 		int i, j;
2916 
2917 		printf("<ctl %u, %x,%x,%x,%x", htinfo->hi_ctrlchannel,
2918 		    htinfo->hi_byte1, htinfo->hi_byte2, htinfo->hi_byte3,
2919 		    LE_READ_2(&htinfo->hi_byte45));
2920 		printf(" basicmcs[");
2921 		sep = "";
2922 		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2923 			if (isset(htinfo->hi_basicmcsset, i)) {
2924 				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2925 					if (isclr(htinfo->hi_basicmcsset, j))
2926 						break;
2927 				j--;
2928 				if (i == j)
2929 					printf("%s%u", sep, i);
2930 				else
2931 					printf("%s%u-%u", sep, i, j);
2932 				i += j-i;
2933 				sep = ",";
2934 			}
2935 		printf("]>");
2936 	}
2937 }
2938 
2939 static void
2940 printathie(const char *tag, const u_int8_t *ie)
2941 {
2942 
2943 	printf("%s", tag);
2944 	if (verbose) {
2945 		const struct ieee80211_ath_ie *ath =
2946 			(const struct ieee80211_ath_ie *)ie;
2947 
2948 		printf("<");
2949 		if (ath->ath_capability & ATHEROS_CAP_TURBO_PRIME)
2950 			printf("DTURBO,");
2951 		if (ath->ath_capability & ATHEROS_CAP_COMPRESSION)
2952 			printf("COMP,");
2953 		if (ath->ath_capability & ATHEROS_CAP_FAST_FRAME)
2954 			printf("FF,");
2955 		if (ath->ath_capability & ATHEROS_CAP_XR)
2956 			printf("XR,");
2957 		if (ath->ath_capability & ATHEROS_CAP_AR)
2958 			printf("AR,");
2959 		if (ath->ath_capability & ATHEROS_CAP_BURST)
2960 			printf("BURST,");
2961 		if (ath->ath_capability & ATHEROS_CAP_WME)
2962 			printf("WME,");
2963 		if (ath->ath_capability & ATHEROS_CAP_BOOST)
2964 			printf("BOOST,");
2965 		printf("0x%x>", LE_READ_2(ath->ath_defkeyix));
2966 	}
2967 }
2968 
2969 
2970 static void
2971 printmeshconf(const char *tag, const uint8_t *ie)
2972 {
2973 
2974 	printf("%s", tag);
2975 	if (verbose) {
2976 		const struct ieee80211_meshconf_ie *mconf =
2977 			(const struct ieee80211_meshconf_ie *)ie;
2978 		printf("<PATH:");
2979 		if (mconf->conf_pselid == IEEE80211_MESHCONF_PATH_HWMP)
2980 			printf("HWMP");
2981 		else
2982 			printf("UNKNOWN");
2983 		printf(" LINK:");
2984 		if (mconf->conf_pmetid == IEEE80211_MESHCONF_METRIC_AIRTIME)
2985 			printf("AIRTIME");
2986 		else
2987 			printf("UNKNOWN");
2988 		printf(" CONGESTION:");
2989 		if (mconf->conf_ccid == IEEE80211_MESHCONF_CC_DISABLED)
2990 			printf("DISABLED");
2991 		else
2992 			printf("UNKNOWN");
2993 		printf(" SYNC:");
2994 		if (mconf->conf_syncid == IEEE80211_MESHCONF_SYNC_NEIGHOFF)
2995 			printf("NEIGHOFF");
2996 		else
2997 			printf("UNKNOWN");
2998 		printf(" AUTH:");
2999 		if (mconf->conf_authid == IEEE80211_MESHCONF_AUTH_DISABLED)
3000 			printf("DISABLED");
3001 		else
3002 			printf("UNKNOWN");
3003 		printf(" FORM:0x%x CAPS:0x%x>", mconf->conf_form,
3004 		    mconf->conf_cap);
3005 	}
3006 }
3007 
3008 static void
3009 printbssload(const char *tag, const uint8_t *ie)
3010 {
3011 	printf("%s", tag);
3012 	if (verbose) {
3013 		const struct ieee80211_bss_load_ie *bssload =
3014 		    (const struct ieee80211_bss_load_ie *) ie;
3015 		printf("<sta count %d, chan load %d, aac %d>",
3016 		    LE_READ_2(&bssload->sta_count),
3017 		    bssload->chan_load,
3018 		    bssload->aac);
3019 	}
3020 }
3021 
3022 static void
3023 printapchanrep(const char *tag, const u_int8_t *ie, size_t ielen)
3024 {
3025 	printf("%s", tag);
3026 	if (verbose) {
3027 		const struct ieee80211_ap_chan_report_ie *ap =
3028 		    (const struct ieee80211_ap_chan_report_ie *) ie;
3029 		const char *sep = "";
3030 
3031 		printf("<class %u, chan:[", ap->i_class);
3032 
3033 		for (size_t i = 3; i < ielen; i++) {
3034 			printf("%s%u", sep, ie[i]);
3035 			sep = ",";
3036 		}
3037 		printf("]>");
3038 	}
3039 }
3040 
3041 static const char *
3042 wpa_cipher(const u_int8_t *sel)
3043 {
3044 #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
3045 	u_int32_t w = LE_READ_4(sel);
3046 
3047 	switch (w) {
3048 	case WPA_SEL(WPA_CSE_NULL):
3049 		return "NONE";
3050 	case WPA_SEL(WPA_CSE_WEP40):
3051 		return "WEP40";
3052 	case WPA_SEL(WPA_CSE_WEP104):
3053 		return "WEP104";
3054 	case WPA_SEL(WPA_CSE_TKIP):
3055 		return "TKIP";
3056 	case WPA_SEL(WPA_CSE_CCMP):
3057 		return "AES-CCMP";
3058 	}
3059 	return "?";		/* NB: so 1<< is discarded */
3060 #undef WPA_SEL
3061 }
3062 
3063 static const char *
3064 wpa_keymgmt(const u_int8_t *sel)
3065 {
3066 #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
3067 	u_int32_t w = LE_READ_4(sel);
3068 
3069 	switch (w) {
3070 	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
3071 		return "8021X-UNSPEC";
3072 	case WPA_SEL(WPA_ASE_8021X_PSK):
3073 		return "8021X-PSK";
3074 	case WPA_SEL(WPA_ASE_NONE):
3075 		return "NONE";
3076 	}
3077 	return "?";
3078 #undef WPA_SEL
3079 }
3080 
3081 static void
3082 printwpaie(const char *tag, const u_int8_t *ie)
3083 {
3084 	u_int8_t len = ie[1];
3085 
3086 	printf("%s", tag);
3087 	if (verbose) {
3088 		const char *sep;
3089 		int n;
3090 
3091 		ie += 6, len -= 4;		/* NB: len is payload only */
3092 
3093 		printf("<v%u", LE_READ_2(ie));
3094 		ie += 2, len -= 2;
3095 
3096 		printf(" mc:%s", wpa_cipher(ie));
3097 		ie += 4, len -= 4;
3098 
3099 		/* unicast ciphers */
3100 		n = LE_READ_2(ie);
3101 		ie += 2, len -= 2;
3102 		sep = " uc:";
3103 		for (; n > 0; n--) {
3104 			printf("%s%s", sep, wpa_cipher(ie));
3105 			ie += 4, len -= 4;
3106 			sep = "+";
3107 		}
3108 
3109 		/* key management algorithms */
3110 		n = LE_READ_2(ie);
3111 		ie += 2, len -= 2;
3112 		sep = " km:";
3113 		for (; n > 0; n--) {
3114 			printf("%s%s", sep, wpa_keymgmt(ie));
3115 			ie += 4, len -= 4;
3116 			sep = "+";
3117 		}
3118 
3119 		if (len > 2)		/* optional capabilities */
3120 			printf(", caps 0x%x", LE_READ_2(ie));
3121 		printf(">");
3122 	}
3123 }
3124 
3125 static const char *
3126 rsn_cipher(const u_int8_t *sel)
3127 {
3128 #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
3129 	u_int32_t w = LE_READ_4(sel);
3130 
3131 	switch (w) {
3132 	case RSN_SEL(RSN_CSE_NULL):
3133 		return "NONE";
3134 	case RSN_SEL(RSN_CSE_WEP40):
3135 		return "WEP40";
3136 	case RSN_SEL(RSN_CSE_WEP104):
3137 		return "WEP104";
3138 	case RSN_SEL(RSN_CSE_TKIP):
3139 		return "TKIP";
3140 	case RSN_SEL(RSN_CSE_CCMP):
3141 		return "AES-CCMP";
3142 	case RSN_SEL(RSN_CSE_WRAP):
3143 		return "AES-OCB";
3144 	}
3145 	return "?";
3146 #undef WPA_SEL
3147 }
3148 
3149 static const char *
3150 rsn_keymgmt(const u_int8_t *sel)
3151 {
3152 #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
3153 	u_int32_t w = LE_READ_4(sel);
3154 
3155 	switch (w) {
3156 	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
3157 		return "8021X-UNSPEC";
3158 	case RSN_SEL(RSN_ASE_8021X_PSK):
3159 		return "8021X-PSK";
3160 	case RSN_SEL(RSN_ASE_NONE):
3161 		return "NONE";
3162 	}
3163 	return "?";
3164 #undef RSN_SEL
3165 }
3166 
3167 static void
3168 printrsnie(const char *tag, const u_int8_t *ie, size_t ielen)
3169 {
3170 	printf("%s", tag);
3171 	if (verbose) {
3172 		const char *sep;
3173 		int n;
3174 
3175 		ie += 2, ielen -= 2;
3176 
3177 		printf("<v%u", LE_READ_2(ie));
3178 		ie += 2, ielen -= 2;
3179 
3180 		printf(" mc:%s", rsn_cipher(ie));
3181 		ie += 4, ielen -= 4;
3182 
3183 		/* unicast ciphers */
3184 		n = LE_READ_2(ie);
3185 		ie += 2, ielen -= 2;
3186 		sep = " uc:";
3187 		for (; n > 0; n--) {
3188 			printf("%s%s", sep, rsn_cipher(ie));
3189 			ie += 4, ielen -= 4;
3190 			sep = "+";
3191 		}
3192 
3193 		/* key management algorithms */
3194 		n = LE_READ_2(ie);
3195 		ie += 2, ielen -= 2;
3196 		sep = " km:";
3197 		for (; n > 0; n--) {
3198 			printf("%s%s", sep, rsn_keymgmt(ie));
3199 			ie += 4, ielen -= 4;
3200 			sep = "+";
3201 		}
3202 
3203 		if (ielen > 2)		/* optional capabilities */
3204 			printf(", caps 0x%x", LE_READ_2(ie));
3205 		/* XXXPMKID */
3206 		printf(">");
3207 	}
3208 }
3209 
3210 #define BE_READ_2(p)					\
3211 	((u_int16_t)					\
3212 	 ((((const u_int8_t *)(p))[1]      ) |		\
3213 	  (((const u_int8_t *)(p))[0] <<  8)))
3214 
3215 static void
3216 printwpsie(const char *tag, const u_int8_t *ie)
3217 {
3218 	u_int8_t len = ie[1];
3219 
3220 	printf("%s", tag);
3221 	if (verbose) {
3222 		static const char *dev_pass_id[] = {
3223 			"D",	/* Default (PIN) */
3224 			"U",	/* User-specified */
3225 			"M",	/* Machine-specified */
3226 			"K",	/* Rekey */
3227 			"P",	/* PushButton */
3228 			"R"	/* Registrar-specified */
3229 		};
3230 		int n;
3231 		int f;
3232 
3233 		ie +=6, len -= 4;		/* NB: len is payload only */
3234 
3235 		/* WPS IE in Beacon and Probe Resp frames have different fields */
3236 		printf("<");
3237 		while (len) {
3238 			uint16_t tlv_type = BE_READ_2(ie);
3239 			uint16_t tlv_len  = BE_READ_2(ie + 2);
3240 			uint16_t cfg_mthd;
3241 
3242 			/* some devices broadcast invalid WPS frames */
3243 			if (tlv_len > len) {
3244 				printf("bad frame length tlv_type=0x%02x "
3245 				    "tlv_len=%d len=%d", tlv_type, tlv_len,
3246 				    len);
3247 				break;
3248 			}
3249 
3250 			ie += 4, len -= 4;
3251 
3252 			switch (tlv_type) {
3253 			case IEEE80211_WPS_ATTR_VERSION:
3254 				printf("v:%d.%d", *ie >> 4, *ie & 0xf);
3255 				break;
3256 			case IEEE80211_WPS_ATTR_AP_SETUP_LOCKED:
3257 				printf(" ap_setup:%s", *ie ? "locked" :
3258 				    "unlocked");
3259 				break;
3260 			case IEEE80211_WPS_ATTR_CONFIG_METHODS:
3261 			case IEEE80211_WPS_ATTR_SELECTED_REGISTRAR_CONFIG_METHODS:
3262 				if (tlv_type == IEEE80211_WPS_ATTR_SELECTED_REGISTRAR_CONFIG_METHODS)
3263 					printf(" sel_reg_cfg_mthd:");
3264 				else
3265 					printf(" cfg_mthd:" );
3266 				cfg_mthd = BE_READ_2(ie);
3267 				f = 0;
3268 				for (n = 15; n >= 0; n--) {
3269 					if (f) {
3270 						printf(",");
3271 						f = 0;
3272 					}
3273 					switch (cfg_mthd & (1 << n)) {
3274 					case 0:
3275 						break;
3276 					case IEEE80211_WPS_CONFIG_USBA:
3277 						printf("usba");
3278 						f++;
3279 						break;
3280 					case IEEE80211_WPS_CONFIG_ETHERNET:
3281 						printf("ethernet");
3282 						f++;
3283 						break;
3284 					case IEEE80211_WPS_CONFIG_LABEL:
3285 						printf("label");
3286 						f++;
3287 						break;
3288 					case IEEE80211_WPS_CONFIG_DISPLAY:
3289 						if (!(cfg_mthd &
3290 						    (IEEE80211_WPS_CONFIG_VIRT_DISPLAY |
3291 						    IEEE80211_WPS_CONFIG_PHY_DISPLAY)))
3292 						    {
3293 							printf("display");
3294 							f++;
3295 						}
3296 						break;
3297 					case IEEE80211_WPS_CONFIG_EXT_NFC_TOKEN:
3298 						printf("ext_nfc_tokenk");
3299 						f++;
3300 						break;
3301 					case IEEE80211_WPS_CONFIG_INT_NFC_TOKEN:
3302 						printf("int_nfc_token");
3303 						f++;
3304 						break;
3305 					case IEEE80211_WPS_CONFIG_NFC_INTERFACE:
3306 						printf("nfc_interface");
3307 						f++;
3308 						break;
3309 					case IEEE80211_WPS_CONFIG_PUSHBUTTON:
3310 						if (!(cfg_mthd &
3311 						    (IEEE80211_WPS_CONFIG_VIRT_PUSHBUTTON |
3312 						    IEEE80211_WPS_CONFIG_PHY_PUSHBUTTON))) {
3313 							printf("push_button");
3314 							f++;
3315 						}
3316 						break;
3317 					case IEEE80211_WPS_CONFIG_KEYPAD:
3318 						printf("keypad");
3319 						f++;
3320 						break;
3321 					case IEEE80211_WPS_CONFIG_VIRT_PUSHBUTTON:
3322 						printf("virtual_push_button");
3323 						f++;
3324 						break;
3325 					case IEEE80211_WPS_CONFIG_PHY_PUSHBUTTON:
3326 						printf("physical_push_button");
3327 						f++;
3328 						break;
3329 					case IEEE80211_WPS_CONFIG_P2PS:
3330 						printf("p2ps");
3331 						f++;
3332 						break;
3333 					case IEEE80211_WPS_CONFIG_VIRT_DISPLAY:
3334 						printf("virtual_display");
3335 						f++;
3336 						break;
3337 					case IEEE80211_WPS_CONFIG_PHY_DISPLAY:
3338 						printf("physical_display");
3339 						f++;
3340 						break;
3341 					default:
3342 						printf("unknown_wps_config<%04x>",
3343 						    cfg_mthd & (1 << n));
3344 						f++;
3345 						break;
3346 					}
3347 				}
3348 				break;
3349 			case IEEE80211_WPS_ATTR_DEV_NAME:
3350 				printf(" device_name:<%.*s>", tlv_len, ie);
3351 				break;
3352 			case IEEE80211_WPS_ATTR_DEV_PASSWORD_ID:
3353 				n = LE_READ_2(ie);
3354 				if (n < (int)nitems(dev_pass_id))
3355 					printf(" dpi:%s", dev_pass_id[n]);
3356 				break;
3357 			case IEEE80211_WPS_ATTR_MANUFACTURER:
3358 				printf(" manufacturer:<%.*s>", tlv_len, ie);
3359 				break;
3360 			case IEEE80211_WPS_ATTR_MODEL_NAME:
3361 				printf(" model_name:<%.*s>", tlv_len, ie);
3362 				break;
3363 			case IEEE80211_WPS_ATTR_MODEL_NUMBER:
3364 				printf(" model_number:<%.*s>", tlv_len, ie);
3365 				break;
3366 			case IEEE80211_WPS_ATTR_PRIMARY_DEV_TYPE:
3367 				printf(" prim_dev:");
3368 				for (n = 0; n < tlv_len; n++)
3369 					printf("%02x", ie[n]);
3370 				break;
3371 			case IEEE80211_WPS_ATTR_RF_BANDS:
3372 				printf(" rf:");
3373 				f = 0;
3374 				for (n = 7; n >= 0; n--) {
3375 					if (f) {
3376 						printf(",");
3377 						f = 0;
3378 					}
3379 					switch (*ie & (1 << n)) {
3380 					case 0:
3381 						break;
3382 					case IEEE80211_WPS_RF_BAND_24GHZ:
3383 						printf("2.4Ghz");
3384 						f++;
3385 						break;
3386 					case IEEE80211_WPS_RF_BAND_50GHZ:
3387 						printf("5Ghz");
3388 						f++;
3389 						break;
3390 					case IEEE80211_WPS_RF_BAND_600GHZ:
3391 						printf("60Ghz");
3392 						f++;
3393 						break;
3394 					default:
3395 						printf("unknown<%02x>",
3396 						    *ie & (1 << n));
3397 						f++;
3398 						break;
3399 					}
3400 				}
3401 				break;
3402 			case IEEE80211_WPS_ATTR_RESPONSE_TYPE:
3403 				printf(" resp_type:0x%02x", *ie);
3404 				break;
3405 			case IEEE80211_WPS_ATTR_SELECTED_REGISTRAR:
3406 				printf(" sel:%s", *ie ? "T" : "F");
3407 				break;
3408 			case IEEE80211_WPS_ATTR_SERIAL_NUMBER:
3409 				printf(" serial_number:<%.*s>", tlv_len, ie);
3410 				break;
3411 			case IEEE80211_WPS_ATTR_UUID_E:
3412 				printf(" uuid-e:");
3413 				for (n = 0; n < (tlv_len - 1); n++)
3414 					printf("%02x-", ie[n]);
3415 				printf("%02x", ie[n]);
3416 				break;
3417 			case IEEE80211_WPS_ATTR_VENDOR_EXT:
3418 				printf(" vendor:");
3419 				for (n = 0; n < tlv_len; n++)
3420 					printf("%02x", ie[n]);
3421 				break;
3422 			case IEEE80211_WPS_ATTR_WPS_STATE:
3423 				switch (*ie) {
3424 				case IEEE80211_WPS_STATE_NOT_CONFIGURED:
3425 					printf(" state:N");
3426 					break;
3427 				case IEEE80211_WPS_STATE_CONFIGURED:
3428 					printf(" state:C");
3429 					break;
3430 				default:
3431 					printf(" state:B<%02x>", *ie);
3432 					break;
3433 				}
3434 				break;
3435 			default:
3436 				printf(" unknown_wps_attr:0x%x", tlv_type);
3437 				break;
3438 			}
3439 			ie += tlv_len, len -= tlv_len;
3440 		}
3441 		printf(">");
3442 	}
3443 }
3444 
3445 static void
3446 printtdmaie(const char *tag, const u_int8_t *ie, size_t ielen)
3447 {
3448 	printf("%s", tag);
3449 	if (verbose && ielen >= sizeof(struct ieee80211_tdma_param)) {
3450 		const struct ieee80211_tdma_param *tdma =
3451 		   (const struct ieee80211_tdma_param *) ie;
3452 
3453 		/* XXX tstamp */
3454 		printf("<v%u slot:%u slotcnt:%u slotlen:%u bintval:%u inuse:0x%x>",
3455 		    tdma->tdma_version, tdma->tdma_slot, tdma->tdma_slotcnt,
3456 		    LE_READ_2(&tdma->tdma_slotlen), tdma->tdma_bintval,
3457 		    tdma->tdma_inuse[0]);
3458 	}
3459 }
3460 
3461 /*
3462  * Copy the ssid string contents into buf, truncating to fit.  If the
3463  * ssid is entirely printable then just copy intact.  Otherwise convert
3464  * to hexadecimal.  If the result is truncated then replace the last
3465  * three characters with "...".
3466  */
3467 static int
3468 copy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
3469 {
3470 	const u_int8_t *p;
3471 	size_t maxlen;
3472 	u_int i;
3473 
3474 	if (essid_len > bufsize)
3475 		maxlen = bufsize;
3476 	else
3477 		maxlen = essid_len;
3478 	/* determine printable or not */
3479 	for (i = 0, p = essid; i < maxlen; i++, p++) {
3480 		if (*p < ' ' || *p > 0x7e)
3481 			break;
3482 	}
3483 	if (i != maxlen) {		/* not printable, print as hex */
3484 		if (bufsize < 3)
3485 			return 0;
3486 		strlcpy(buf, "0x", bufsize);
3487 		bufsize -= 2;
3488 		p = essid;
3489 		for (i = 0; i < maxlen && bufsize >= 2; i++) {
3490 			sprintf(&buf[2+2*i], "%02x", p[i]);
3491 			bufsize -= 2;
3492 		}
3493 		if (i != essid_len)
3494 			memcpy(&buf[2+2*i-3], "...", 3);
3495 	} else {			/* printable, truncate as needed */
3496 		memcpy(buf, essid, maxlen);
3497 		if (maxlen != essid_len)
3498 			memcpy(&buf[maxlen-3], "...", 3);
3499 	}
3500 	return maxlen;
3501 }
3502 
3503 static void
3504 printssid(const char *tag, const u_int8_t *ie, int maxlen)
3505 {
3506 	char ssid[2*IEEE80211_NWID_LEN+1];
3507 
3508 	printf("%s<%.*s>", tag, copy_essid(ssid, maxlen, ie+2, ie[1]), ssid);
3509 }
3510 
3511 static void
3512 printrates(const char *tag, const u_int8_t *ie, size_t ielen)
3513 {
3514 	const char *sep;
3515 
3516 	printf("%s", tag);
3517 	sep = "<";
3518 	for (size_t i = 2; i < ielen; i++) {
3519 		printf("%s%s%d", sep,
3520 		    ie[i] & IEEE80211_RATE_BASIC ? "B" : "",
3521 		    ie[i] & IEEE80211_RATE_VAL);
3522 		sep = ",";
3523 	}
3524 	printf(">");
3525 }
3526 
3527 static void
3528 printcountry(const char *tag, const u_int8_t *ie)
3529 {
3530 	const struct ieee80211_country_ie *cie =
3531 	   (const struct ieee80211_country_ie *) ie;
3532 	int i, nbands, schan, nchan;
3533 
3534 	printf("%s<%c%c%c", tag, cie->cc[0], cie->cc[1], cie->cc[2]);
3535 	nbands = (cie->len - 3) / sizeof(cie->band[0]);
3536 	for (i = 0; i < nbands; i++) {
3537 		schan = cie->band[i].schan;
3538 		nchan = cie->band[i].nchan;
3539 		if (nchan != 1)
3540 			printf(" %u-%u,%u", schan, schan + nchan-1,
3541 			    cie->band[i].maxtxpwr);
3542 		else
3543 			printf(" %u,%u", schan, cie->band[i].maxtxpwr);
3544 	}
3545 	printf(">");
3546 }
3547 
3548 static __inline int
3549 iswpaoui(const u_int8_t *frm)
3550 {
3551 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
3552 }
3553 
3554 static __inline int
3555 iswmeinfo(const u_int8_t *frm)
3556 {
3557 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3558 		frm[6] == WME_INFO_OUI_SUBTYPE;
3559 }
3560 
3561 static __inline int
3562 iswmeparam(const u_int8_t *frm)
3563 {
3564 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3565 		frm[6] == WME_PARAM_OUI_SUBTYPE;
3566 }
3567 
3568 static __inline int
3569 isatherosoui(const u_int8_t *frm)
3570 {
3571 	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
3572 }
3573 
3574 static __inline int
3575 istdmaoui(const uint8_t *frm)
3576 {
3577 	return frm[1] > 3 && LE_READ_4(frm+2) == ((TDMA_OUI_TYPE<<24)|TDMA_OUI);
3578 }
3579 
3580 static __inline int
3581 iswpsoui(const uint8_t *frm)
3582 {
3583 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPS_OUI_TYPE<<24)|WPA_OUI);
3584 }
3585 
3586 static const char *
3587 iename(int elemid)
3588 {
3589 	static char iename_buf[64];
3590 	switch (elemid) {
3591 	case IEEE80211_ELEMID_FHPARMS:	return " FHPARMS";
3592 	case IEEE80211_ELEMID_CFPARMS:	return " CFPARMS";
3593 	case IEEE80211_ELEMID_TIM:	return " TIM";
3594 	case IEEE80211_ELEMID_IBSSPARMS:return " IBSSPARMS";
3595 	case IEEE80211_ELEMID_BSSLOAD:	return " BSSLOAD";
3596 	case IEEE80211_ELEMID_CHALLENGE:return " CHALLENGE";
3597 	case IEEE80211_ELEMID_PWRCNSTR:	return " PWRCNSTR";
3598 	case IEEE80211_ELEMID_PWRCAP:	return " PWRCAP";
3599 	case IEEE80211_ELEMID_TPCREQ:	return " TPCREQ";
3600 	case IEEE80211_ELEMID_TPCREP:	return " TPCREP";
3601 	case IEEE80211_ELEMID_SUPPCHAN:	return " SUPPCHAN";
3602 	case IEEE80211_ELEMID_CSA:	return " CSA";
3603 	case IEEE80211_ELEMID_MEASREQ:	return " MEASREQ";
3604 	case IEEE80211_ELEMID_MEASREP:	return " MEASREP";
3605 	case IEEE80211_ELEMID_QUIET:	return " QUIET";
3606 	case IEEE80211_ELEMID_IBSSDFS:	return " IBSSDFS";
3607 	case IEEE80211_ELEMID_RESERVED_47:
3608 					return " RESERVED_47";
3609 	case IEEE80211_ELEMID_MOBILITY_DOMAIN:
3610 					return " MOBILITY_DOMAIN";
3611 	case IEEE80211_ELEMID_RRM_ENACAPS:
3612 					return " RRM_ENCAPS";
3613 	case IEEE80211_ELEMID_OVERLAP_BSS_SCAN_PARAM:
3614 					return " OVERLAP_BSS";
3615 	case IEEE80211_ELEMID_TPC:	return " TPC";
3616 	case IEEE80211_ELEMID_CCKM:	return " CCKM";
3617 	case IEEE80211_ELEMID_EXTCAP:	return " EXTCAP";
3618 	}
3619 	snprintf(iename_buf, sizeof(iename_buf), " UNKNOWN_ELEMID_%d",
3620 	    elemid);
3621 	return (const char *) iename_buf;
3622 }
3623 
3624 static void
3625 printies(const u_int8_t *vp, int ielen, unsigned int maxcols)
3626 {
3627 	while (ielen > 0) {
3628 		switch (vp[0]) {
3629 		case IEEE80211_ELEMID_SSID:
3630 			if (verbose)
3631 				printssid(" SSID", vp, maxcols);
3632 			break;
3633 		case IEEE80211_ELEMID_RATES:
3634 		case IEEE80211_ELEMID_XRATES:
3635 			if (verbose)
3636 				printrates(vp[0] == IEEE80211_ELEMID_RATES ?
3637 				    " RATES" : " XRATES", vp, 2+vp[1]);
3638 			break;
3639 		case IEEE80211_ELEMID_DSPARMS:
3640 			if (verbose)
3641 				printf(" DSPARMS<%u>", vp[2]);
3642 			break;
3643 		case IEEE80211_ELEMID_COUNTRY:
3644 			if (verbose)
3645 				printcountry(" COUNTRY", vp);
3646 			break;
3647 		case IEEE80211_ELEMID_ERP:
3648 			if (verbose)
3649 				printf(" ERP<0x%x>", vp[2]);
3650 			break;
3651 		case IEEE80211_ELEMID_VENDOR:
3652 			if (iswpaoui(vp))
3653 				printwpaie(" WPA", vp);
3654 			else if (iswmeinfo(vp))
3655 				printwmeinfo(" WME", vp);
3656 			else if (iswmeparam(vp))
3657 				printwmeparam(" WME", vp);
3658 			else if (isatherosoui(vp))
3659 				printathie(" ATH", vp);
3660 			else if (iswpsoui(vp))
3661 				printwpsie(" WPS", vp);
3662 			else if (istdmaoui(vp))
3663 				printtdmaie(" TDMA", vp, 2+vp[1]);
3664 			else if (verbose)
3665 				printie(" VEN", vp, 2+vp[1], maxcols);
3666 			break;
3667 		case IEEE80211_ELEMID_RSN:
3668 			printrsnie(" RSN", vp, 2+vp[1]);
3669 			break;
3670 		case IEEE80211_ELEMID_HTCAP:
3671 			printhtcap(" HTCAP", vp);
3672 			break;
3673 		case IEEE80211_ELEMID_HTINFO:
3674 			if (verbose)
3675 				printhtinfo(" HTINFO", vp);
3676 			break;
3677 		case IEEE80211_ELEMID_MESHID:
3678 			if (verbose)
3679 				printssid(" MESHID", vp, maxcols);
3680 			break;
3681 		case IEEE80211_ELEMID_MESHCONF:
3682 			printmeshconf(" MESHCONF", vp);
3683 			break;
3684 		case IEEE80211_ELEMID_VHT_CAP:
3685 			printvhtcap(" VHTCAP", vp);
3686 			break;
3687 		case IEEE80211_ELEMID_VHT_OPMODE:
3688 			printvhtinfo(" VHTOPMODE", vp);
3689 			break;
3690 		case IEEE80211_ELEMID_VHT_PWR_ENV:
3691 			printvhtpwrenv(" VHTPWRENV", vp, 2+vp[1]);
3692 			break;
3693 		case IEEE80211_ELEMID_BSSLOAD:
3694 			printbssload(" BSSLOAD", vp);
3695 			break;
3696 		case IEEE80211_ELEMID_APCHANREP:
3697 			printapchanrep(" APCHANREP", vp, 2+vp[1]);
3698 			break;
3699 		default:
3700 			if (verbose)
3701 				printie(iename(vp[0]), vp, 2+vp[1], maxcols);
3702 			break;
3703 		}
3704 		ielen -= 2+vp[1];
3705 		vp += 2+vp[1];
3706 	}
3707 }
3708 
3709 static void
3710 printmimo(const struct ieee80211_mimo_info *mi)
3711 {
3712 	int i;
3713 	int r = 0;
3714 
3715 	for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3716 		if (mi->ch[i].rssi != 0) {
3717 			r = 1;
3718 			break;
3719 		}
3720 	}
3721 
3722 	/* NB: don't muddy display unless there's something to show */
3723 	if (r == 0)
3724 		return;
3725 
3726 	/* XXX TODO: ignore EVM; secondary channels for now */
3727 	printf(" (rssi %.1f:%.1f:%.1f:%.1f nf %d:%d:%d:%d)",
3728 	    mi->ch[0].rssi[0] / 2.0,
3729 	    mi->ch[1].rssi[0] / 2.0,
3730 	    mi->ch[2].rssi[0] / 2.0,
3731 	    mi->ch[3].rssi[0] / 2.0,
3732 	    mi->ch[0].noise[0],
3733 	    mi->ch[1].noise[0],
3734 	    mi->ch[2].noise[0],
3735 	    mi->ch[3].noise[0]);
3736 }
3737 
3738 static void
3739 printbssidname(const struct ether_addr *n)
3740 {
3741 	char name[MAXHOSTNAMELEN + 1];
3742 
3743 	if (ether_ntohost(name, n) != 0)
3744 		return;
3745 
3746 	printf(" (%s)", name);
3747 }
3748 
3749 static void
3750 list_scan(int s)
3751 {
3752 	uint8_t buf[24*1024];
3753 	char ssid[IEEE80211_NWID_LEN+1];
3754 	const uint8_t *cp;
3755 	int len, idlen;
3756 
3757 	if (get80211len(s, IEEE80211_IOC_SCAN_RESULTS, buf, sizeof(buf), &len) < 0)
3758 		errx(1, "unable to get scan results");
3759 	if (len < (int)sizeof(struct ieee80211req_scan_result))
3760 		return;
3761 
3762 	getchaninfo(s);
3763 
3764 	printf("%-*.*s  %-17.17s  %4s %4s   %-7s  %3s %4s\n"
3765 		, IEEE80211_NWID_LEN, IEEE80211_NWID_LEN, "SSID/MESH ID"
3766 		, "BSSID"
3767 		, "CHAN"
3768 		, "RATE"
3769 		, " S:N"
3770 		, "INT"
3771 		, "CAPS"
3772 	);
3773 	cp = buf;
3774 	do {
3775 		const struct ieee80211req_scan_result *sr;
3776 		const uint8_t *vp, *idp;
3777 
3778 		sr = (const struct ieee80211req_scan_result *)(const void *) cp;
3779 		vp = cp + sr->isr_ie_off;
3780 		if (sr->isr_meshid_len) {
3781 			idp = vp + sr->isr_ssid_len;
3782 			idlen = sr->isr_meshid_len;
3783 		} else {
3784 			idp = vp;
3785 			idlen = sr->isr_ssid_len;
3786 		}
3787 		printf("%-*.*s  %s  %3d  %3dM %4d:%-4d %4d %-4.4s"
3788 			, IEEE80211_NWID_LEN
3789 			  , copy_essid(ssid, IEEE80211_NWID_LEN, idp, idlen)
3790 			  , ssid
3791 			, ether_ntoa((const struct ether_addr *) sr->isr_bssid)
3792 			, ieee80211_mhz2ieee(sr->isr_freq, sr->isr_flags)
3793 			, getmaxrate(sr->isr_rates, sr->isr_nrates)
3794 			, (sr->isr_rssi/2)+sr->isr_noise, sr->isr_noise
3795 			, sr->isr_intval
3796 			, getcaps(sr->isr_capinfo)
3797 		);
3798 		printies(vp + sr->isr_ssid_len + sr->isr_meshid_len,
3799 		    sr->isr_ie_len, 24);
3800 		printbssidname((const struct ether_addr *)sr->isr_bssid);
3801 		printf("\n");
3802 		cp += sr->isr_len, len -= sr->isr_len;
3803 	} while (len >= (int)sizeof(struct ieee80211req_scan_result));
3804 }
3805 
3806 static void
3807 scan_and_wait(int s)
3808 {
3809 	struct ieee80211_scan_req sr;
3810 	struct ieee80211req ireq;
3811 	int sroute;
3812 
3813 	sroute = socket(PF_ROUTE, SOCK_RAW, 0);
3814 	if (sroute < 0) {
3815 		perror("socket(PF_ROUTE,SOCK_RAW)");
3816 		return;
3817 	}
3818 	(void) memset(&ireq, 0, sizeof(ireq));
3819 	(void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
3820 	ireq.i_type = IEEE80211_IOC_SCAN_REQ;
3821 
3822 	memset(&sr, 0, sizeof(sr));
3823 	sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE
3824 		    | IEEE80211_IOC_SCAN_BGSCAN
3825 		    | IEEE80211_IOC_SCAN_NOPICK
3826 		    | IEEE80211_IOC_SCAN_ONCE;
3827 	sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
3828 	sr.sr_nssid = 0;
3829 
3830 	ireq.i_data = &sr;
3831 	ireq.i_len = sizeof(sr);
3832 	/*
3833 	 * NB: only root can trigger a scan so ignore errors. Also ignore
3834 	 * possible errors from net80211, even if no new scan could be
3835 	 * started there might still be a valid scan cache.
3836 	 */
3837 	if (ioctl(s, SIOCS80211, &ireq) == 0) {
3838 		char buf[2048];
3839 		struct if_announcemsghdr *ifan;
3840 		struct rt_msghdr *rtm;
3841 
3842 		do {
3843 			if (read(sroute, buf, sizeof(buf)) < 0) {
3844 				perror("read(PF_ROUTE)");
3845 				break;
3846 			}
3847 			rtm = (struct rt_msghdr *) buf;
3848 			if (rtm->rtm_version != RTM_VERSION)
3849 				break;
3850 			ifan = (struct if_announcemsghdr *) rtm;
3851 		} while (rtm->rtm_type != RTM_IEEE80211 ||
3852 		    ifan->ifan_what != RTM_IEEE80211_SCAN);
3853 	}
3854 	close(sroute);
3855 }
3856 
3857 static void
3858 set80211scan(if_ctx *ctx, const char *val __unused, int dummy __unused)
3859 {
3860 	scan_and_wait(ctx->io_s);
3861 	list_scan(ctx->io_s);
3862 }
3863 
3864 static enum ieee80211_opmode get80211opmode(int s);
3865 
3866 static int
3867 gettxseq(const struct ieee80211req_sta_info *si)
3868 {
3869 	int i, txseq;
3870 
3871 	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3872 		return si->isi_txseqs[0];
3873 	/* XXX not right but usually what folks want */
3874 	txseq = 0;
3875 	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3876 		if (si->isi_txseqs[i] > txseq)
3877 			txseq = si->isi_txseqs[i];
3878 	return txseq;
3879 }
3880 
3881 static int
3882 getrxseq(const struct ieee80211req_sta_info *si)
3883 {
3884 	int rxseq;
3885 
3886 	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3887 		return si->isi_rxseqs[0];
3888 	/* XXX not right but usually what folks want */
3889 	rxseq = 0;
3890 	for (unsigned int i = 0; i < IEEE80211_TID_SIZE; i++)
3891 		if (si->isi_rxseqs[i] > rxseq)
3892 			rxseq = si->isi_rxseqs[i];
3893 	return rxseq;
3894 }
3895 
3896 static void
3897 list_stations(int s)
3898 {
3899 	union {
3900 		struct ieee80211req_sta_req req;
3901 		uint8_t buf[24*1024];
3902 	} u;
3903 	enum ieee80211_opmode opmode = get80211opmode(s);
3904 	const uint8_t *cp;
3905 	int len;
3906 
3907 	/* broadcast address =>'s get all stations */
3908 	(void) memset(u.req.is_u.macaddr, 0xff, IEEE80211_ADDR_LEN);
3909 	if (opmode == IEEE80211_M_STA) {
3910 		/*
3911 		 * Get information about the associated AP.
3912 		 */
3913 		(void) get80211(s, IEEE80211_IOC_BSSID,
3914 		    u.req.is_u.macaddr, IEEE80211_ADDR_LEN);
3915 	}
3916 	if (get80211len(s, IEEE80211_IOC_STA_INFO, &u, sizeof(u), &len) < 0)
3917 		errx(1, "unable to get station information");
3918 	if (len < (int)sizeof(struct ieee80211req_sta_info))
3919 		return;
3920 
3921 	getchaninfo(s);
3922 
3923 	if (opmode == IEEE80211_M_MBSS)
3924 		printf("%-17.17s %4s %5s %5s %7s %4s %4s %4s %6s %6s\n"
3925 			, "ADDR"
3926 			, "CHAN"
3927 			, "LOCAL"
3928 			, "PEER"
3929 			, "STATE"
3930 			, "RATE"
3931 			, "RSSI"
3932 			, "IDLE"
3933 			, "TXSEQ"
3934 			, "RXSEQ"
3935 		);
3936 	else
3937 		printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %-12s\n"
3938 			, "ADDR"
3939 			, "AID"
3940 			, "CHAN"
3941 			, "RATE"
3942 			, "RSSI"
3943 			, "IDLE"
3944 			, "TXSEQ"
3945 			, "RXSEQ"
3946 			, "CAPS"
3947 			, "FLAG"
3948 		);
3949 	cp = (const uint8_t *) u.req.info;
3950 	do {
3951 		const struct ieee80211req_sta_info *si;
3952 
3953 		si = (const struct ieee80211req_sta_info *)(const void *)cp;
3954 		if (si->isi_len < sizeof(*si))
3955 			break;
3956 		if (opmode == IEEE80211_M_MBSS)
3957 			printf("%s %4d %5x %5x %7.7s %3dM %4.1f %4d %6d %6d"
3958 				, ether_ntoa((const struct ether_addr*)
3959 				    si->isi_macaddr)
3960 				, ieee80211_mhz2ieee(si->isi_freq,
3961 				    si->isi_flags)
3962 				, si->isi_localid
3963 				, si->isi_peerid
3964 				, mesh_linkstate_string(si->isi_peerstate)
3965 				, si->isi_txmbps/2
3966 				, si->isi_rssi/2.
3967 				, si->isi_inact
3968 				, gettxseq(si)
3969 				, getrxseq(si)
3970 			);
3971 		else
3972 			printf("%s %4u %4d %3dM %4.1f %4d %6d %6d %-4.4s %-12.12s"
3973 				, ether_ntoa((const struct ether_addr*)
3974 				    si->isi_macaddr)
3975 				, IEEE80211_AID(si->isi_associd)
3976 				, ieee80211_mhz2ieee(si->isi_freq,
3977 				    si->isi_flags)
3978 				, si->isi_txmbps/2
3979 				, si->isi_rssi/2.
3980 				, si->isi_inact
3981 				, gettxseq(si)
3982 				, getrxseq(si)
3983 				, getcaps(si->isi_capinfo)
3984 				, getflags(si->isi_state)
3985 			);
3986 		printies(cp + si->isi_ie_off, si->isi_ie_len, 24);
3987 		printmimo(&si->isi_mimo);
3988 		printf("\n");
3989 		cp += si->isi_len, len -= si->isi_len;
3990 	} while (len >= (int)sizeof(struct ieee80211req_sta_info));
3991 }
3992 
3993 static const char *
3994 mesh_linkstate_string(uint8_t state)
3995 {
3996 	static const char *state_names[] = {
3997 	    [0] = "IDLE",
3998 	    [1] = "OPEN-TX",
3999 	    [2] = "OPEN-RX",
4000 	    [3] = "CONF-RX",
4001 	    [4] = "ESTAB",
4002 	    [5] = "HOLDING",
4003 	};
4004 
4005 	if (state >= nitems(state_names)) {
4006 		static char buf[10];
4007 		snprintf(buf, sizeof(buf), "#%u", state);
4008 		return buf;
4009 	} else
4010 		return state_names[state];
4011 }
4012 
4013 static const char *
4014 get_chaninfo(const struct ieee80211_channel *c, int precise,
4015 	char buf[], size_t bsize)
4016 {
4017 	buf[0] = '\0';
4018 	if (IEEE80211_IS_CHAN_FHSS(c))
4019 		strlcat(buf, " FHSS", bsize);
4020 	if (IEEE80211_IS_CHAN_A(c))
4021 		strlcat(buf, " 11a", bsize);
4022 	else if (IEEE80211_IS_CHAN_ANYG(c))
4023 		strlcat(buf, " 11g", bsize);
4024 	else if (IEEE80211_IS_CHAN_B(c))
4025 		strlcat(buf, " 11b", bsize);
4026 	if (IEEE80211_IS_CHAN_HALF(c))
4027 		strlcat(buf, "/10MHz", bsize);
4028 	if (IEEE80211_IS_CHAN_QUARTER(c))
4029 		strlcat(buf, "/5MHz", bsize);
4030 	if (IEEE80211_IS_CHAN_TURBO(c))
4031 		strlcat(buf, " Turbo", bsize);
4032 	if (precise) {
4033 		if (IEEE80211_IS_CHAN_VHT80P80(c))
4034 			strlcat(buf, " vht/80p80", bsize);
4035 		else if (IEEE80211_IS_CHAN_VHT160(c))
4036 			strlcat(buf, " vht/160", bsize);
4037 		else if (IEEE80211_IS_CHAN_VHT80(c) &&
4038 		    IEEE80211_IS_CHAN_HT40D(c))
4039 			strlcat(buf, " vht/80-", bsize);
4040 		else if (IEEE80211_IS_CHAN_VHT80(c) &&
4041 		    IEEE80211_IS_CHAN_HT40U(c))
4042 			strlcat(buf, " vht/80+", bsize);
4043 		else if (IEEE80211_IS_CHAN_VHT80(c))
4044 			strlcat(buf, " vht/80", bsize);
4045 		else if (IEEE80211_IS_CHAN_VHT40D(c))
4046 			strlcat(buf, " vht/40-", bsize);
4047 		else if (IEEE80211_IS_CHAN_VHT40U(c))
4048 			strlcat(buf, " vht/40+", bsize);
4049 		else if (IEEE80211_IS_CHAN_VHT20(c))
4050 			strlcat(buf, " vht/20", bsize);
4051 		else if (IEEE80211_IS_CHAN_HT20(c))
4052 			strlcat(buf, " ht/20", bsize);
4053 		else if (IEEE80211_IS_CHAN_HT40D(c))
4054 			strlcat(buf, " ht/40-", bsize);
4055 		else if (IEEE80211_IS_CHAN_HT40U(c))
4056 			strlcat(buf, " ht/40+", bsize);
4057 	} else {
4058 		if (IEEE80211_IS_CHAN_VHT(c))
4059 			strlcat(buf, " vht", bsize);
4060 		else if (IEEE80211_IS_CHAN_HT(c))
4061 			strlcat(buf, " ht", bsize);
4062 	}
4063 	return buf;
4064 }
4065 
4066 static void
4067 print_chaninfo(const struct ieee80211_channel *c, int verb)
4068 {
4069 	char buf[14];
4070 
4071 	if (verb)
4072 		printf("Channel %3u : %u%c%c%c%c%c MHz%-14.14s",
4073 		    ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
4074 		    IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
4075 		    IEEE80211_IS_CHAN_DFS(c) ? 'D' : ' ',
4076 		    IEEE80211_IS_CHAN_RADAR(c) ? 'R' : ' ',
4077 		    IEEE80211_IS_CHAN_CWINT(c) ? 'I' : ' ',
4078 		    IEEE80211_IS_CHAN_CACDONE(c) ? 'C' : ' ',
4079 		    get_chaninfo(c, verb, buf, sizeof(buf)));
4080 	else
4081 	printf("Channel %3u : %u%c MHz%-14.14s",
4082 	    ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
4083 	    IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
4084 	    get_chaninfo(c, verb, buf, sizeof(buf)));
4085 
4086 }
4087 
4088 static int
4089 chanpref(const struct ieee80211_channel *c)
4090 {
4091 
4092 	if (IEEE80211_IS_CHAN_VHT80P80(c))
4093 		return 90;
4094 	if (IEEE80211_IS_CHAN_VHT160(c))
4095 		return 80;
4096 	if (IEEE80211_IS_CHAN_VHT80(c))
4097 		return 70;
4098 	if (IEEE80211_IS_CHAN_VHT40(c))
4099 		return 60;
4100 	if (IEEE80211_IS_CHAN_VHT20(c))
4101 		return 50;
4102 	if (IEEE80211_IS_CHAN_HT40(c))
4103 		return 40;
4104 	if (IEEE80211_IS_CHAN_HT20(c))
4105 		return 30;
4106 	if (IEEE80211_IS_CHAN_HALF(c))
4107 		return 10;
4108 	if (IEEE80211_IS_CHAN_QUARTER(c))
4109 		return 5;
4110 	if (IEEE80211_IS_CHAN_TURBO(c))
4111 		return 25;
4112 	if (IEEE80211_IS_CHAN_A(c))
4113 		return 20;
4114 	if (IEEE80211_IS_CHAN_G(c))
4115 		return 20;
4116 	if (IEEE80211_IS_CHAN_B(c))
4117 		return 15;
4118 	if (IEEE80211_IS_CHAN_PUREG(c))
4119 		return 15;
4120 	return 0;
4121 }
4122 
4123 static void
4124 print_channels(int s, const struct ieee80211req_chaninfo *chans,
4125 	int allchans, int verb)
4126 {
4127 	struct ieee80211req_chaninfo *achans;
4128 	uint8_t reported[IEEE80211_CHAN_BYTES];
4129 	const struct ieee80211_channel *c;
4130 	unsigned int i, half;
4131 
4132 	achans = malloc(IEEE80211_CHANINFO_SPACE(chans));
4133 	if (achans == NULL)
4134 		errx(1, "no space for active channel list");
4135 	achans->ic_nchans = 0;
4136 	memset(reported, 0, sizeof(reported));
4137 	if (!allchans) {
4138 		struct ieee80211req_chanlist active;
4139 
4140 		if (get80211(s, IEEE80211_IOC_CHANLIST, &active, sizeof(active)) < 0)
4141 			errx(1, "unable to get active channel list");
4142 		for (i = 0; i < chans->ic_nchans; i++) {
4143 			c = &chans->ic_chans[i];
4144 			if (!isset(active.ic_channels, c->ic_ieee))
4145 				continue;
4146 			/*
4147 			 * Suppress compatible duplicates unless
4148 			 * verbose.  The kernel gives us it's
4149 			 * complete channel list which has separate
4150 			 * entries for 11g/11b and 11a/turbo.
4151 			 */
4152 			if (isset(reported, c->ic_ieee) && !verb) {
4153 				/* XXX we assume duplicates are adjacent */
4154 				achans->ic_chans[achans->ic_nchans-1] = *c;
4155 			} else {
4156 				achans->ic_chans[achans->ic_nchans++] = *c;
4157 				setbit(reported, c->ic_ieee);
4158 			}
4159 		}
4160 	} else {
4161 		for (i = 0; i < chans->ic_nchans; i++) {
4162 			c = &chans->ic_chans[i];
4163 			/* suppress duplicates as above */
4164 			if (isset(reported, c->ic_ieee) && !verb) {
4165 				/* XXX we assume duplicates are adjacent */
4166 				struct ieee80211_channel *a =
4167 				    &achans->ic_chans[achans->ic_nchans-1];
4168 				if (chanpref(c) > chanpref(a))
4169 					*a = *c;
4170 			} else {
4171 				achans->ic_chans[achans->ic_nchans++] = *c;
4172 				setbit(reported, c->ic_ieee);
4173 			}
4174 		}
4175 	}
4176 	half = achans->ic_nchans / 2;
4177 	if (achans->ic_nchans % 2)
4178 		half++;
4179 
4180 	for (i = 0; i < achans->ic_nchans / 2; i++) {
4181 		print_chaninfo(&achans->ic_chans[i], verb);
4182 		print_chaninfo(&achans->ic_chans[half+i], verb);
4183 		printf("\n");
4184 	}
4185 	if (achans->ic_nchans % 2) {
4186 		print_chaninfo(&achans->ic_chans[i], verb);
4187 		printf("\n");
4188 	}
4189 	free(achans);
4190 }
4191 
4192 static void
4193 list_channels(int s, int allchans)
4194 {
4195 	getchaninfo(s);
4196 	print_channels(s, chaninfo, allchans, verbose);
4197 }
4198 
4199 static void
4200 print_txpow(const struct ieee80211_channel *c)
4201 {
4202 	printf("Channel %3u : %u MHz %3.1f reg %2d  ",
4203 	    c->ic_ieee, c->ic_freq,
4204 	    c->ic_maxpower/2., c->ic_maxregpower);
4205 }
4206 
4207 static void
4208 print_txpow_verbose(const struct ieee80211_channel *c)
4209 {
4210 	print_chaninfo(c, 1);
4211 	printf("min %4.1f dBm  max %3.1f dBm  reg %2d dBm",
4212 	    c->ic_minpower/2., c->ic_maxpower/2., c->ic_maxregpower);
4213 	/* indicate where regulatory cap limits power use */
4214 	if (c->ic_maxpower > 2*c->ic_maxregpower)
4215 		printf(" <");
4216 }
4217 
4218 static void
4219 list_txpow(int s)
4220 {
4221 	struct ieee80211req_chaninfo *achans;
4222 	uint8_t reported[IEEE80211_CHAN_BYTES];
4223 	struct ieee80211_channel *c, *prev;
4224 	unsigned int i, half;
4225 
4226 	getchaninfo(s);
4227 	achans = malloc(IEEE80211_CHANINFO_SPACE(chaninfo));
4228 	if (achans == NULL)
4229 		errx(1, "no space for active channel list");
4230 	achans->ic_nchans = 0;
4231 	memset(reported, 0, sizeof(reported));
4232 	for (i = 0; i < chaninfo->ic_nchans; i++) {
4233 		c = &chaninfo->ic_chans[i];
4234 		/* suppress duplicates as above */
4235 		if (isset(reported, c->ic_ieee) && !verbose) {
4236 			/* XXX we assume duplicates are adjacent */
4237 			assert(achans->ic_nchans > 0);
4238 			prev = &achans->ic_chans[achans->ic_nchans-1];
4239 			/* display highest power on channel */
4240 			if (c->ic_maxpower > prev->ic_maxpower)
4241 				*prev = *c;
4242 		} else {
4243 			achans->ic_chans[achans->ic_nchans++] = *c;
4244 			setbit(reported, c->ic_ieee);
4245 		}
4246 	}
4247 	if (!verbose) {
4248 		half = achans->ic_nchans / 2;
4249 		if (achans->ic_nchans % 2)
4250 			half++;
4251 
4252 		for (i = 0; i < achans->ic_nchans / 2; i++) {
4253 			print_txpow(&achans->ic_chans[i]);
4254 			print_txpow(&achans->ic_chans[half+i]);
4255 			printf("\n");
4256 		}
4257 		if (achans->ic_nchans % 2) {
4258 			print_txpow(&achans->ic_chans[i]);
4259 			printf("\n");
4260 		}
4261 	} else {
4262 		for (i = 0; i < achans->ic_nchans; i++) {
4263 			print_txpow_verbose(&achans->ic_chans[i]);
4264 			printf("\n");
4265 		}
4266 	}
4267 	free(achans);
4268 }
4269 
4270 static void
4271 list_keys(int s __unused)
4272 {
4273 }
4274 
4275 static void
4276 list_capabilities(int s)
4277 {
4278 	struct ieee80211_devcaps_req *dc;
4279 
4280 	if (verbose)
4281 		dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
4282 	else
4283 		dc = malloc(IEEE80211_DEVCAPS_SIZE(1));
4284 	if (dc == NULL)
4285 		errx(1, "no space for device capabilities");
4286 	dc->dc_chaninfo.ic_nchans = verbose ? MAXCHAN : 1;
4287 	getdevcaps(s, dc);
4288 	printb("drivercaps", dc->dc_drivercaps, IEEE80211_C_BITS);
4289 	if (dc->dc_cryptocaps != 0 || verbose) {
4290 		putchar('\n');
4291 		printb("cryptocaps", dc->dc_cryptocaps, IEEE80211_CRYPTO_BITS);
4292 	}
4293 	if (dc->dc_htcaps != 0 || verbose) {
4294 		putchar('\n');
4295 		printb("htcaps", dc->dc_htcaps, IEEE80211_HTCAP_BITS);
4296 	}
4297 	if (dc->dc_vhtcaps != 0 || verbose) {
4298 		putchar('\n');
4299 		printb("vhtcaps", dc->dc_vhtcaps, IEEE80211_VHTCAP_BITS);
4300 	}
4301 
4302 	putchar('\n');
4303 	if (verbose) {
4304 		chaninfo = &dc->dc_chaninfo;	/* XXX */
4305 		print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, verbose);
4306 	}
4307 	free(dc);
4308 }
4309 
4310 static int
4311 get80211wme(int s, int param, int ac, int *val)
4312 {
4313 	struct ieee80211req ireq;
4314 
4315 	(void) memset(&ireq, 0, sizeof(ireq));
4316 	(void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
4317 	ireq.i_type = param;
4318 	ireq.i_len = ac;
4319 	if (ioctl(s, SIOCG80211, &ireq) < 0) {
4320 		warn("cannot get WME parameter %d, ac %d%s",
4321 		    param, ac & IEEE80211_WMEPARAM_VAL,
4322 		    ac & IEEE80211_WMEPARAM_BSS ? " (BSS)" : "");
4323 		return -1;
4324 	}
4325 	*val = ireq.i_val;
4326 	return 0;
4327 }
4328 
4329 static void
4330 list_wme_aci(int s, const char *tag, int ac)
4331 {
4332 	int val;
4333 
4334 	printf("\t%s", tag);
4335 
4336 	/* show WME BSS parameters */
4337 	if (get80211wme(s, IEEE80211_IOC_WME_CWMIN, ac, &val) != -1)
4338 		printf(" cwmin %2u", val);
4339 	if (get80211wme(s, IEEE80211_IOC_WME_CWMAX, ac, &val) != -1)
4340 		printf(" cwmax %2u", val);
4341 	if (get80211wme(s, IEEE80211_IOC_WME_AIFS, ac, &val) != -1)
4342 		printf(" aifs %2u", val);
4343 	if (get80211wme(s, IEEE80211_IOC_WME_TXOPLIMIT, ac, &val) != -1)
4344 		printf(" txopLimit %3u", val);
4345 	if (get80211wme(s, IEEE80211_IOC_WME_ACM, ac, &val) != -1) {
4346 		if (val)
4347 			printf(" acm");
4348 		else if (verbose)
4349 			printf(" -acm");
4350 	}
4351 	/* !BSS only */
4352 	if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
4353 		if (get80211wme(s, IEEE80211_IOC_WME_ACKPOLICY, ac, &val) != -1) {
4354 			if (!val)
4355 				printf(" -ack");
4356 			else if (verbose)
4357 				printf(" ack");
4358 		}
4359 	}
4360 	printf("\n");
4361 }
4362 
4363 static void
4364 list_wme(int s)
4365 {
4366 	static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
4367 	int ac;
4368 
4369 	if (verbose) {
4370 		/* display both BSS and local settings */
4371 		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
4372 	again:
4373 			if (ac & IEEE80211_WMEPARAM_BSS)
4374 				list_wme_aci(s, "     ", ac);
4375 			else
4376 				list_wme_aci(s, acnames[ac], ac);
4377 			if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
4378 				ac |= IEEE80211_WMEPARAM_BSS;
4379 				goto again;
4380 			} else
4381 				ac &= ~IEEE80211_WMEPARAM_BSS;
4382 		}
4383 	} else {
4384 		/* display only channel settings */
4385 		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++)
4386 			list_wme_aci(s, acnames[ac], ac);
4387 	}
4388 }
4389 
4390 static void
4391 list_roam(int s)
4392 {
4393 	const struct ieee80211_roamparam *rp;
4394 	int mode;
4395 
4396 	getroam(s);
4397 	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
4398 		rp = &roamparams.params[mode];
4399 		if (rp->rssi == 0 && rp->rate == 0)
4400 			continue;
4401 		if (mode == IEEE80211_MODE_11NA ||
4402 		    mode == IEEE80211_MODE_11NG ||
4403 		    mode == IEEE80211_MODE_VHT_2GHZ ||
4404 		    mode == IEEE80211_MODE_VHT_5GHZ) {
4405 			if (rp->rssi & 1)
4406 				LINE_CHECK("roam:%-7.7s rssi %2u.5dBm  MCS %2u    ",
4407 				    modename[mode], rp->rssi/2,
4408 				    rp->rate &~ IEEE80211_RATE_MCS);
4409 			else
4410 				LINE_CHECK("roam:%-7.7s rssi %4udBm  MCS %2u    ",
4411 				    modename[mode], rp->rssi/2,
4412 				    rp->rate &~ IEEE80211_RATE_MCS);
4413 		} else {
4414 			if (rp->rssi & 1)
4415 				LINE_CHECK("roam:%-7.7s rssi %2u.5dBm rate %2u Mb/s",
4416 				    modename[mode], rp->rssi/2, rp->rate/2);
4417 			else
4418 				LINE_CHECK("roam:%-7.7s rssi %4udBm rate %2u Mb/s",
4419 				    modename[mode], rp->rssi/2, rp->rate/2);
4420 		}
4421 	}
4422 }
4423 
4424 /* XXX TODO: rate-to-string method... */
4425 static const char*
4426 get_mcs_mbs_rate_str(uint8_t rate)
4427 {
4428 	return (rate & IEEE80211_RATE_MCS) ? "MCS " : "Mb/s";
4429 }
4430 
4431 static uint8_t
4432 get_rate_value(uint8_t rate)
4433 {
4434 	if (rate & IEEE80211_RATE_MCS)
4435 		return (rate &~ IEEE80211_RATE_MCS);
4436 	return (rate / 2);
4437 }
4438 
4439 static void
4440 list_txparams(int s)
4441 {
4442 	const struct ieee80211_txparam *tp;
4443 	int mode;
4444 
4445 	gettxparams(s);
4446 	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
4447 		tp = &txparams.params[mode];
4448 		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
4449 			continue;
4450 		if (mode == IEEE80211_MODE_11NA ||
4451 		    mode == IEEE80211_MODE_11NG ||
4452 		    mode == IEEE80211_MODE_VHT_2GHZ ||
4453 		    mode == IEEE80211_MODE_VHT_5GHZ) {
4454 			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
4455 				LINE_CHECK("%-7.7s ucast NONE    mgmt %2u %s "
4456 				    "mcast %2u %s maxretry %u",
4457 				    modename[mode],
4458 				    get_rate_value(tp->mgmtrate),
4459 				    get_mcs_mbs_rate_str(tp->mgmtrate),
4460 				    get_rate_value(tp->mcastrate),
4461 				    get_mcs_mbs_rate_str(tp->mcastrate),
4462 				    tp->maxretry);
4463 			else
4464 				LINE_CHECK("%-7.7s ucast %2u MCS  mgmt %2u %s "
4465 				    "mcast %2u %s maxretry %u",
4466 				    modename[mode],
4467 				    tp->ucastrate &~ IEEE80211_RATE_MCS,
4468 				    get_rate_value(tp->mgmtrate),
4469 				    get_mcs_mbs_rate_str(tp->mgmtrate),
4470 				    get_rate_value(tp->mcastrate),
4471 				    get_mcs_mbs_rate_str(tp->mcastrate),
4472 				    tp->maxretry);
4473 		} else {
4474 			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
4475 				LINE_CHECK("%-7.7s ucast NONE    mgmt %2u Mb/s "
4476 				    "mcast %2u Mb/s maxretry %u",
4477 				    modename[mode],
4478 				    tp->mgmtrate/2,
4479 				    tp->mcastrate/2, tp->maxretry);
4480 			else
4481 				LINE_CHECK("%-7.7s ucast %2u Mb/s mgmt %2u Mb/s "
4482 				    "mcast %2u Mb/s maxretry %u",
4483 				    modename[mode],
4484 				    tp->ucastrate/2, tp->mgmtrate/2,
4485 				    tp->mcastrate/2, tp->maxretry);
4486 		}
4487 	}
4488 }
4489 
4490 static void
4491 printpolicy(int policy)
4492 {
4493 	switch (policy) {
4494 	case IEEE80211_MACCMD_POLICY_OPEN:
4495 		printf("policy: open\n");
4496 		break;
4497 	case IEEE80211_MACCMD_POLICY_ALLOW:
4498 		printf("policy: allow\n");
4499 		break;
4500 	case IEEE80211_MACCMD_POLICY_DENY:
4501 		printf("policy: deny\n");
4502 		break;
4503 	case IEEE80211_MACCMD_POLICY_RADIUS:
4504 		printf("policy: radius\n");
4505 		break;
4506 	default:
4507 		printf("policy: unknown (%u)\n", policy);
4508 		break;
4509 	}
4510 }
4511 
4512 static void
4513 list_mac(int s)
4514 {
4515 	struct ieee80211req ireq;
4516 	struct ieee80211req_maclist *acllist;
4517 	int i, nacls, policy, len;
4518 	uint8_t *data;
4519 	char c;
4520 
4521 	(void) memset(&ireq, 0, sizeof(ireq));
4522 	(void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
4523 	ireq.i_type = IEEE80211_IOC_MACCMD;
4524 	ireq.i_val = IEEE80211_MACCMD_POLICY;
4525 	if (ioctl(s, SIOCG80211, &ireq) < 0) {
4526 		if (errno == EINVAL) {
4527 			printf("No acl policy loaded\n");
4528 			return;
4529 		}
4530 		err(1, "unable to get mac policy");
4531 	}
4532 	policy = ireq.i_val;
4533 	if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
4534 		c = '*';
4535 	} else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
4536 		c = '+';
4537 	} else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
4538 		c = '-';
4539 	} else if (policy == IEEE80211_MACCMD_POLICY_RADIUS) {
4540 		c = 'r';		/* NB: should never have entries */
4541 	} else {
4542 		printf("policy: unknown (%u)\n", policy);
4543 		c = '?';
4544 	}
4545 	if (verbose || c == '?')
4546 		printpolicy(policy);
4547 
4548 	ireq.i_val = IEEE80211_MACCMD_LIST;
4549 	ireq.i_len = 0;
4550 	if (ioctl(s, SIOCG80211, &ireq) < 0)
4551 		err(1, "unable to get mac acl list size");
4552 	if (ireq.i_len == 0) {		/* NB: no acls */
4553 		if (!(verbose || c == '?'))
4554 			printpolicy(policy);
4555 		return;
4556 	}
4557 	len = ireq.i_len;
4558 
4559 	data = malloc(len);
4560 	if (data == NULL)
4561 		err(1, "out of memory for acl list");
4562 
4563 	ireq.i_data = data;
4564 	if (ioctl(s, SIOCG80211, &ireq) < 0)
4565 		err(1, "unable to get mac acl list");
4566 	nacls = len / sizeof(*acllist);
4567 	acllist = (struct ieee80211req_maclist *) data;
4568 	for (i = 0; i < nacls; i++)
4569 		printf("%c%s\n", c, ether_ntoa(
4570 			(const struct ether_addr *) acllist[i].ml_macaddr));
4571 	free(data);
4572 }
4573 
4574 static void
4575 print_regdomain(const struct ieee80211_regdomain *reg, int verb)
4576 {
4577 	if ((reg->regdomain != 0 &&
4578 	    reg->regdomain != reg->country) || verb) {
4579 		const struct regdomain *rd =
4580 		    lib80211_regdomain_findbysku(getregdata(), reg->regdomain);
4581 		if (rd == NULL)
4582 			LINE_CHECK("regdomain %d", reg->regdomain);
4583 		else
4584 			LINE_CHECK("regdomain %s", rd->name);
4585 	}
4586 	if (reg->country != 0 || verb) {
4587 		const struct country *cc =
4588 		    lib80211_country_findbycc(getregdata(), reg->country);
4589 		if (cc == NULL)
4590 			LINE_CHECK("country %d", reg->country);
4591 		else
4592 			LINE_CHECK("country %s", cc->isoname);
4593 	}
4594 	if (reg->location == 'I')
4595 		LINE_CHECK("indoor");
4596 	else if (reg->location == 'O')
4597 		LINE_CHECK("outdoor");
4598 	else if (verb)
4599 		LINE_CHECK("anywhere");
4600 	if (reg->ecm)
4601 		LINE_CHECK("ecm");
4602 	else if (verb)
4603 		LINE_CHECK("-ecm");
4604 }
4605 
4606 static void
4607 list_regdomain(int s, int channelsalso)
4608 {
4609 	getregdomain(s);
4610 	if (channelsalso) {
4611 		getchaninfo(s);
4612 		spacer = ':';
4613 		print_regdomain(&regdomain, 1);
4614 		LINE_BREAK();
4615 		print_channels(s, chaninfo, 1/*allchans*/, 1/*verbose*/);
4616 	} else
4617 		print_regdomain(&regdomain, verbose);
4618 }
4619 
4620 static void
4621 list_mesh(int s)
4622 {
4623 	struct ieee80211req ireq;
4624 	struct ieee80211req_mesh_route routes[128];
4625 	struct ieee80211req_mesh_route *rt;
4626 
4627 	(void) memset(&ireq, 0, sizeof(ireq));
4628 	(void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
4629 	ireq.i_type = IEEE80211_IOC_MESH_RTCMD;
4630 	ireq.i_val = IEEE80211_MESH_RTCMD_LIST;
4631 	ireq.i_data = &routes;
4632 	ireq.i_len = sizeof(routes);
4633 	if (ioctl(s, SIOCG80211, &ireq) < 0)
4634 	 	err(1, "unable to get the Mesh routing table");
4635 
4636 	printf("%-17.17s %-17.17s %4s %4s %4s %6s %s\n"
4637 		, "DEST"
4638 		, "NEXT HOP"
4639 		, "HOPS"
4640 		, "METRIC"
4641 		, "LIFETIME"
4642 		, "MSEQ"
4643 		, "FLAGS");
4644 
4645 	for (unsigned int i = 0; i < ireq.i_len / sizeof(*rt); i++) {
4646 		rt = &routes[i];
4647 		printf("%s ",
4648 		    ether_ntoa((const struct ether_addr *)rt->imr_dest));
4649 		printf("%s %4u   %4u   %6u %6u    %c%c\n",
4650 			ether_ntoa((const struct ether_addr *)rt->imr_nexthop),
4651 			rt->imr_nhops, rt->imr_metric, rt->imr_lifetime,
4652 			rt->imr_lastmseq,
4653 			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_DISCOVER) ?
4654 			    'D' :
4655 			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_VALID) ?
4656 			    'V' : '!',
4657 			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_PROXY) ?
4658 			    'P' :
4659 			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_GATE) ?
4660 			    'G' :' ');
4661 	}
4662 }
4663 
4664 static void
4665 set80211list(if_ctx *ctx, const char *arg, int dummy __unused)
4666 {
4667 	int s = ctx->io_s;
4668 #define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
4669 
4670 	LINE_INIT('\t');
4671 
4672 	if (iseq(arg, "sta"))
4673 		list_stations(s);
4674 	else if (iseq(arg, "scan") || iseq(arg, "ap"))
4675 		list_scan(s);
4676 	else if (iseq(arg, "chan") || iseq(arg, "freq"))
4677 		list_channels(s, 1);
4678 	else if (iseq(arg, "active"))
4679 		list_channels(s, 0);
4680 	else if (iseq(arg, "keys"))
4681 		list_keys(s);
4682 	else if (iseq(arg, "caps"))
4683 		list_capabilities(s);
4684 	else if (iseq(arg, "wme") || iseq(arg, "wmm"))
4685 		list_wme(s);
4686 	else if (iseq(arg, "mac"))
4687 		list_mac(s);
4688 	else if (iseq(arg, "txpow"))
4689 		list_txpow(s);
4690 	else if (iseq(arg, "roam"))
4691 		list_roam(s);
4692 	else if (iseq(arg, "txparam") || iseq(arg, "txparm"))
4693 		list_txparams(s);
4694 	else if (iseq(arg, "regdomain"))
4695 		list_regdomain(s, 1);
4696 	else if (iseq(arg, "countries"))
4697 		list_countries();
4698 	else if (iseq(arg, "mesh"))
4699 		list_mesh(s);
4700 	else
4701 		errx(1, "Don't know how to list %s for %s", arg, name);
4702 	LINE_BREAK();
4703 #undef iseq
4704 }
4705 
4706 static enum ieee80211_opmode
4707 get80211opmode(int s)
4708 {
4709 	struct ifmediareq ifmr;
4710 
4711 	(void) memset(&ifmr, 0, sizeof(ifmr));
4712 	(void) strlcpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
4713 
4714 	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
4715 		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) {
4716 			if (ifmr.ifm_current & IFM_FLAG0)
4717 				return IEEE80211_M_AHDEMO;
4718 			else
4719 				return IEEE80211_M_IBSS;
4720 		}
4721 		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
4722 			return IEEE80211_M_HOSTAP;
4723 		if (ifmr.ifm_current & IFM_IEEE80211_IBSS)
4724 			return IEEE80211_M_IBSS;
4725 		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
4726 			return IEEE80211_M_MONITOR;
4727 		if (ifmr.ifm_current & IFM_IEEE80211_MBSS)
4728 			return IEEE80211_M_MBSS;
4729 	}
4730 	return IEEE80211_M_STA;
4731 }
4732 
4733 #if 0
4734 static void
4735 printcipher(int s, struct ieee80211req *ireq, int keylenop)
4736 {
4737 	switch (ireq->i_val) {
4738 	case IEEE80211_CIPHER_WEP:
4739 		ireq->i_type = keylenop;
4740 		if (ioctl(s, SIOCG80211, ireq) != -1)
4741 			printf("WEP-%s",
4742 			    ireq->i_len <= 5 ? "40" :
4743 			    ireq->i_len <= 13 ? "104" : "128");
4744 		else
4745 			printf("WEP");
4746 		break;
4747 	case IEEE80211_CIPHER_TKIP:
4748 		printf("TKIP");
4749 		break;
4750 	case IEEE80211_CIPHER_AES_OCB:
4751 		printf("AES-OCB");
4752 		break;
4753 	case IEEE80211_CIPHER_AES_CCM:
4754 		printf("AES-CCM");
4755 		break;
4756 	case IEEE80211_CIPHER_CKIP:
4757 		printf("CKIP");
4758 		break;
4759 	case IEEE80211_CIPHER_NONE:
4760 		printf("NONE");
4761 		break;
4762 	default:
4763 		printf("UNKNOWN (0x%x)", ireq->i_val);
4764 		break;
4765 	}
4766 }
4767 #endif
4768 
4769 static void
4770 printkey(const struct ieee80211req_key *ik)
4771 {
4772 	static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
4773 	u_int keylen = ik->ik_keylen;
4774 	int printcontents;
4775 
4776 	printcontents = printkeys &&
4777 		(memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
4778 	if (printcontents)
4779 		LINE_BREAK();
4780 	switch (ik->ik_type) {
4781 	case IEEE80211_CIPHER_WEP:
4782 		/* compatibility */
4783 		LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
4784 		    keylen <= 5 ? "40-bit" :
4785 		    keylen <= 13 ? "104-bit" : "128-bit");
4786 		break;
4787 	case IEEE80211_CIPHER_TKIP:
4788 		if (keylen > 128/8)
4789 			keylen -= 128/8;	/* ignore MIC for now */
4790 		LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4791 		break;
4792 	case IEEE80211_CIPHER_AES_OCB:
4793 		LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4794 		break;
4795 	case IEEE80211_CIPHER_AES_CCM:
4796 		LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4797 		break;
4798 	case IEEE80211_CIPHER_CKIP:
4799 		LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4800 		break;
4801 	case IEEE80211_CIPHER_NONE:
4802 		LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4803 		break;
4804 	default:
4805 		LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
4806 			ik->ik_type, ik->ik_keyix+1, 8*keylen);
4807 		break;
4808 	}
4809 	if (printcontents) {
4810 		u_int i;
4811 
4812 		printf(" <");
4813 		for (i = 0; i < keylen; i++)
4814 			printf("%02x", ik->ik_keydata[i]);
4815 		printf(">");
4816 		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4817 		    (ik->ik_keyrsc != 0 || verbose))
4818 			printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
4819 		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4820 		    (ik->ik_keytsc != 0 || verbose))
4821 			printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
4822 		if (ik->ik_flags != 0 && verbose) {
4823 			const char *sep = " ";
4824 
4825 			if (ik->ik_flags & IEEE80211_KEY_XMIT)
4826 				printf("%stx", sep), sep = "+";
4827 			if (ik->ik_flags & IEEE80211_KEY_RECV)
4828 				printf("%srx", sep), sep = "+";
4829 			if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
4830 				printf("%sdef", sep), sep = "+";
4831 		}
4832 		LINE_BREAK();
4833 	}
4834 }
4835 
4836 static void
4837 printrate(const char *tag, int v, int defrate, int defmcs)
4838 {
4839 	if ((v & IEEE80211_RATE_MCS) == 0) {
4840 		if (v != defrate) {
4841 			if (v & 1)
4842 				LINE_CHECK("%s %d.5", tag, v/2);
4843 			else
4844 				LINE_CHECK("%s %d", tag, v/2);
4845 		}
4846 	} else {
4847 		if (v != defmcs)
4848 			LINE_CHECK("%s %d", tag, v &~ 0x80);
4849 	}
4850 }
4851 
4852 static int
4853 getid(int s, int ix, void *data, size_t len, int *plen, int mesh)
4854 {
4855 	struct ieee80211req ireq;
4856 
4857 	(void) memset(&ireq, 0, sizeof(ireq));
4858 	(void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
4859 	ireq.i_type = (!mesh) ? IEEE80211_IOC_SSID : IEEE80211_IOC_MESH_ID;
4860 	ireq.i_val = ix;
4861 	ireq.i_data = data;
4862 	ireq.i_len = len;
4863 	if (ioctl(s, SIOCG80211, &ireq) < 0)
4864 		return -1;
4865 	*plen = ireq.i_len;
4866 	return 0;
4867 }
4868 
4869 static int
4870 getdevicename(int s, void *data, size_t len, int *plen)
4871 {
4872 	struct ieee80211req ireq;
4873 
4874 	(void) memset(&ireq, 0, sizeof(ireq));
4875 	(void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
4876 	ireq.i_type = IEEE80211_IOC_IC_NAME;
4877 	ireq.i_val = -1;
4878 	ireq.i_data = data;
4879 	ireq.i_len = len;
4880 	if (ioctl(s, SIOCG80211, &ireq) < 0)
4881 		return (-1);
4882 	*plen = ireq.i_len;
4883 	return (0);
4884 }
4885 
4886 static void
4887 ieee80211_status(if_ctx *ctx)
4888 {
4889 	int s = ctx->io_s;
4890 	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
4891 	enum ieee80211_opmode opmode = get80211opmode(s);
4892 	int i, num, wpa, wme, bgscan, bgscaninterval, val, len, wepmode;
4893 	uint8_t data[32];
4894 	const struct ieee80211_channel *c;
4895 	const struct ieee80211_roamparam *rp;
4896 	const struct ieee80211_txparam *tp;
4897 
4898 	if (getid(s, -1, data, sizeof(data), &len, 0) < 0) {
4899 		/* If we can't get the SSID, this isn't an 802.11 device. */
4900 		return;
4901 	}
4902 
4903 	/*
4904 	 * Invalidate cached state so printing status for multiple
4905 	 * if's doesn't reuse the first interfaces' cached state.
4906 	 */
4907 	gotcurchan = 0;
4908 	gotroam = 0;
4909 	gottxparams = 0;
4910 	gothtconf = 0;
4911 	gotregdomain = 0;
4912 
4913 	printf("\t");
4914 	if (opmode == IEEE80211_M_MBSS) {
4915 		printf("meshid ");
4916 		getid(s, 0, data, sizeof(data), &len, 1);
4917 		print_string(data, len);
4918 	} else {
4919 		if (get80211val(s, IEEE80211_IOC_NUMSSIDS, &num) < 0)
4920 			num = 0;
4921 		printf("ssid ");
4922 		if (num > 1) {
4923 			for (i = 0; i < num; i++) {
4924 				if (getid(s, i, data, sizeof(data), &len, 0) >= 0 && len > 0) {
4925 					printf(" %d:", i + 1);
4926 					print_string(data, len);
4927 				}
4928 			}
4929 		} else
4930 			print_string(data, len);
4931 	}
4932 	c = getcurchan(s);
4933 	if (c->ic_freq != IEEE80211_CHAN_ANY) {
4934 		char buf[14];
4935 		printf(" channel %d (%u MHz%s)", c->ic_ieee, c->ic_freq,
4936 			get_chaninfo(c, 1, buf, sizeof(buf)));
4937 	} else if (verbose)
4938 		printf(" channel UNDEF");
4939 
4940 	if (get80211(s, IEEE80211_IOC_BSSID, data, IEEE80211_ADDR_LEN) >= 0 &&
4941 	    (memcmp(data, zerobssid, sizeof(zerobssid)) != 0 || verbose)) {
4942 		printf(" bssid %s", ether_ntoa((struct ether_addr *)data));
4943 		printbssidname((struct ether_addr *)data);
4944 	}
4945 
4946 	if (get80211len(s, IEEE80211_IOC_STATIONNAME, data, sizeof(data), &len) != -1) {
4947 		printf("\n\tstationname ");
4948 		print_string(data, len);
4949 	}
4950 
4951 	spacer = ' ';		/* force first break */
4952 	LINE_BREAK();
4953 
4954 	list_regdomain(s, 0);
4955 
4956 	wpa = 0;
4957 	if (get80211val(s, IEEE80211_IOC_AUTHMODE, &val) != -1) {
4958 		switch (val) {
4959 		case IEEE80211_AUTH_NONE:
4960 			LINE_CHECK("authmode NONE");
4961 			break;
4962 		case IEEE80211_AUTH_OPEN:
4963 			LINE_CHECK("authmode OPEN");
4964 			break;
4965 		case IEEE80211_AUTH_SHARED:
4966 			LINE_CHECK("authmode SHARED");
4967 			break;
4968 		case IEEE80211_AUTH_8021X:
4969 			LINE_CHECK("authmode 802.1x");
4970 			break;
4971 		case IEEE80211_AUTH_WPA:
4972 			if (get80211val(s, IEEE80211_IOC_WPA, &wpa) < 0)
4973 				wpa = 1;	/* default to WPA1 */
4974 			switch (wpa) {
4975 			case 2:
4976 				LINE_CHECK("authmode WPA2/802.11i");
4977 				break;
4978 			case 3:
4979 				LINE_CHECK("authmode WPA1+WPA2/802.11i");
4980 				break;
4981 			default:
4982 				LINE_CHECK("authmode WPA");
4983 				break;
4984 			}
4985 			break;
4986 		case IEEE80211_AUTH_AUTO:
4987 			LINE_CHECK("authmode AUTO");
4988 			break;
4989 		default:
4990 			LINE_CHECK("authmode UNKNOWN (0x%x)", val);
4991 			break;
4992 		}
4993 	}
4994 
4995 	if (wpa || verbose) {
4996 		if (get80211val(s, IEEE80211_IOC_WPS, &val) != -1) {
4997 			if (val)
4998 				LINE_CHECK("wps");
4999 			else if (verbose)
5000 				LINE_CHECK("-wps");
5001 		}
5002 		if (get80211val(s, IEEE80211_IOC_TSN, &val) != -1) {
5003 			if (val)
5004 				LINE_CHECK("tsn");
5005 			else if (verbose)
5006 				LINE_CHECK("-tsn");
5007 		}
5008 		if (ioctl(s, IEEE80211_IOC_COUNTERMEASURES, &val) != -1) {
5009 			if (val)
5010 				LINE_CHECK("countermeasures");
5011 			else if (verbose)
5012 				LINE_CHECK("-countermeasures");
5013 		}
5014 #if 0
5015 		/* XXX not interesting with WPA done in user space */
5016 		ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
5017 		if (ioctl(s, SIOCG80211, &ireq) != -1) {
5018 		}
5019 
5020 		ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
5021 		if (ioctl(s, SIOCG80211, &ireq) != -1) {
5022 			LINE_CHECK("mcastcipher ");
5023 			printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
5024 			spacer = ' ';
5025 		}
5026 
5027 		ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
5028 		if (ioctl(s, SIOCG80211, &ireq) != -1) {
5029 			LINE_CHECK("ucastcipher ");
5030 			printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
5031 		}
5032 
5033 		if (wpa & 2) {
5034 			ireq.i_type = IEEE80211_IOC_RSNCAPS;
5035 			if (ioctl(s, SIOCG80211, &ireq) != -1) {
5036 				LINE_CHECK("RSN caps 0x%x", ireq.i_val);
5037 				spacer = ' ';
5038 			}
5039 		}
5040 
5041 		ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
5042 		if (ioctl(s, SIOCG80211, &ireq) != -1) {
5043 		}
5044 #endif
5045 	}
5046 
5047 	if (get80211val(s, IEEE80211_IOC_WEP, &wepmode) != -1 &&
5048 	    wepmode != IEEE80211_WEP_NOSUP) {
5049 
5050 		switch (wepmode) {
5051 		case IEEE80211_WEP_OFF:
5052 			LINE_CHECK("privacy OFF");
5053 			break;
5054 		case IEEE80211_WEP_ON:
5055 			LINE_CHECK("privacy ON");
5056 			break;
5057 		case IEEE80211_WEP_MIXED:
5058 			LINE_CHECK("privacy MIXED");
5059 			break;
5060 		default:
5061 			LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
5062 			break;
5063 		}
5064 
5065 		/*
5066 		 * If we get here then we've got WEP support so we need
5067 		 * to print WEP status.
5068 		 */
5069 
5070 		if (get80211val(s, IEEE80211_IOC_WEPTXKEY, &val) < 0) {
5071 			warn("WEP support, but no tx key!");
5072 			goto end;
5073 		}
5074 		if (val != -1)
5075 			LINE_CHECK("deftxkey %d", val+1);
5076 		else if (wepmode != IEEE80211_WEP_OFF || verbose)
5077 			LINE_CHECK("deftxkey UNDEF");
5078 
5079 		if (get80211val(s, IEEE80211_IOC_NUMWEPKEYS, &num) < 0) {
5080 			warn("WEP support, but no NUMWEPKEYS support!");
5081 			goto end;
5082 		}
5083 
5084 		for (i = 0; i < num; i++) {
5085 			struct ieee80211req_key ik;
5086 
5087 			memset(&ik, 0, sizeof(ik));
5088 			ik.ik_keyix = i;
5089 			if (get80211(s, IEEE80211_IOC_WPAKEY, &ik, sizeof(ik)) < 0) {
5090 				warn("WEP support, but can get keys!");
5091 				goto end;
5092 			}
5093 			if (ik.ik_keylen != 0) {
5094 				if (verbose)
5095 					LINE_BREAK();
5096 				printkey(&ik);
5097 			}
5098 		}
5099 		if (i > 0 && verbose)
5100 			LINE_BREAK();
5101 end:
5102 		;
5103 	}
5104 
5105 	if (get80211val(s, IEEE80211_IOC_POWERSAVE, &val) != -1 &&
5106 	    val != IEEE80211_POWERSAVE_NOSUP ) {
5107 		if (val != IEEE80211_POWERSAVE_OFF || verbose) {
5108 			switch (val) {
5109 			case IEEE80211_POWERSAVE_OFF:
5110 				LINE_CHECK("powersavemode OFF");
5111 				break;
5112 			case IEEE80211_POWERSAVE_CAM:
5113 				LINE_CHECK("powersavemode CAM");
5114 				break;
5115 			case IEEE80211_POWERSAVE_PSP:
5116 				LINE_CHECK("powersavemode PSP");
5117 				break;
5118 			case IEEE80211_POWERSAVE_PSP_CAM:
5119 				LINE_CHECK("powersavemode PSP-CAM");
5120 				break;
5121 			}
5122 			if (get80211val(s, IEEE80211_IOC_POWERSAVESLEEP, &val) != -1)
5123 				LINE_CHECK("powersavesleep %d", val);
5124 		}
5125 	}
5126 
5127 	if (get80211val(s, IEEE80211_IOC_TXPOWER, &val) != -1) {
5128 		if (val & 1)
5129 			LINE_CHECK("txpower %d.5", val/2);
5130 		else
5131 			LINE_CHECK("txpower %d", val/2);
5132 	}
5133 	if (verbose) {
5134 		if (get80211val(s, IEEE80211_IOC_TXPOWMAX, &val) != -1)
5135 			LINE_CHECK("txpowmax %.1f", val/2.);
5136 	}
5137 
5138 	if (get80211val(s, IEEE80211_IOC_DOTD, &val) != -1) {
5139 		if (val)
5140 			LINE_CHECK("dotd");
5141 		else if (verbose)
5142 			LINE_CHECK("-dotd");
5143 	}
5144 
5145 	if (get80211val(s, IEEE80211_IOC_RTSTHRESHOLD, &val) != -1) {
5146 		if (val != IEEE80211_RTS_MAX || verbose)
5147 			LINE_CHECK("rtsthreshold %d", val);
5148 	}
5149 
5150 	if (get80211val(s, IEEE80211_IOC_FRAGTHRESHOLD, &val) != -1) {
5151 		if (val != IEEE80211_FRAG_MAX || verbose)
5152 			LINE_CHECK("fragthreshold %d", val);
5153 	}
5154 	if (opmode == IEEE80211_M_STA || verbose) {
5155 		if (get80211val(s, IEEE80211_IOC_BMISSTHRESHOLD, &val) != -1) {
5156 			if (val != IEEE80211_HWBMISS_MAX || verbose)
5157 				LINE_CHECK("bmiss %d", val);
5158 		}
5159 	}
5160 
5161 	if (!verbose) {
5162 		gettxparams(s);
5163 		tp = &txparams.params[chan2mode(c)];
5164 		printrate("ucastrate", tp->ucastrate,
5165 		    IEEE80211_FIXED_RATE_NONE, IEEE80211_FIXED_RATE_NONE);
5166 		printrate("mcastrate", tp->mcastrate, 2*1,
5167 		    IEEE80211_RATE_MCS|0);
5168 		printrate("mgmtrate", tp->mgmtrate, 2*1,
5169 		    IEEE80211_RATE_MCS|0);
5170 		if (tp->maxretry != 6)		/* XXX */
5171 			LINE_CHECK("maxretry %d", tp->maxretry);
5172 	} else {
5173 		LINE_BREAK();
5174 		list_txparams(s);
5175 	}
5176 
5177 	bgscaninterval = -1;
5178 	(void) get80211val(s, IEEE80211_IOC_BGSCAN_INTERVAL, &bgscaninterval);
5179 
5180 	if (get80211val(s, IEEE80211_IOC_SCANVALID, &val) != -1) {
5181 		if (val != bgscaninterval || verbose)
5182 			LINE_CHECK("scanvalid %u", val);
5183 	}
5184 
5185 	bgscan = 0;
5186 	if (get80211val(s, IEEE80211_IOC_BGSCAN, &bgscan) != -1) {
5187 		if (bgscan)
5188 			LINE_CHECK("bgscan");
5189 		else if (verbose)
5190 			LINE_CHECK("-bgscan");
5191 	}
5192 	if (bgscan || verbose) {
5193 		if (bgscaninterval != -1)
5194 			LINE_CHECK("bgscanintvl %u", bgscaninterval);
5195 		if (get80211val(s, IEEE80211_IOC_BGSCAN_IDLE, &val) != -1)
5196 			LINE_CHECK("bgscanidle %u", val);
5197 		if (!verbose) {
5198 			getroam(s);
5199 			rp = &roamparams.params[chan2mode(c)];
5200 			if (rp->rssi & 1)
5201 				LINE_CHECK("roam:rssi %u.5", rp->rssi/2);
5202 			else
5203 				LINE_CHECK("roam:rssi %u", rp->rssi/2);
5204 			LINE_CHECK("roam:rate %s%u",
5205 			    (rp->rate & IEEE80211_RATE_MCS) ? "MCS " : "",
5206 			    get_rate_value(rp->rate));
5207 		} else {
5208 			LINE_BREAK();
5209 			list_roam(s);
5210 			LINE_BREAK();
5211 		}
5212 	}
5213 
5214 	if (IEEE80211_IS_CHAN_ANYG(c) || verbose) {
5215 		if (get80211val(s, IEEE80211_IOC_PUREG, &val) != -1) {
5216 			if (val)
5217 				LINE_CHECK("pureg");
5218 			else if (verbose)
5219 				LINE_CHECK("-pureg");
5220 		}
5221 		if (get80211val(s, IEEE80211_IOC_PROTMODE, &val) != -1) {
5222 			switch (val) {
5223 			case IEEE80211_PROTMODE_OFF:
5224 				LINE_CHECK("protmode OFF");
5225 				break;
5226 			case IEEE80211_PROTMODE_CTS:
5227 				LINE_CHECK("protmode CTS");
5228 				break;
5229 			case IEEE80211_PROTMODE_RTSCTS:
5230 				LINE_CHECK("protmode RTSCTS");
5231 				break;
5232 			default:
5233 				LINE_CHECK("protmode UNKNOWN (0x%x)", val);
5234 				break;
5235 			}
5236 		}
5237 	}
5238 
5239 	if (IEEE80211_IS_CHAN_HT(c) || verbose) {
5240 		gethtconf(s);
5241 		switch (htconf & 3) {
5242 		case 0:
5243 		case 2:
5244 			LINE_CHECK("-ht");
5245 			break;
5246 		case 1:
5247 			LINE_CHECK("ht20");
5248 			break;
5249 		case 3:
5250 			if (verbose)
5251 				LINE_CHECK("ht");
5252 			break;
5253 		}
5254 		if (get80211val(s, IEEE80211_IOC_HTCOMPAT, &val) != -1) {
5255 			if (!val)
5256 				LINE_CHECK("-htcompat");
5257 			else if (verbose)
5258 				LINE_CHECK("htcompat");
5259 		}
5260 		if (get80211val(s, IEEE80211_IOC_AMPDU, &val) != -1) {
5261 			switch (val) {
5262 			case 0:
5263 				LINE_CHECK("-ampdu");
5264 				break;
5265 			case 1:
5266 				LINE_CHECK("ampdutx -ampdurx");
5267 				break;
5268 			case 2:
5269 				LINE_CHECK("-ampdutx ampdurx");
5270 				break;
5271 			case 3:
5272 				if (verbose)
5273 					LINE_CHECK("ampdu");
5274 				break;
5275 			}
5276 		}
5277 		/* XXX 11ac density/size is different */
5278 		if (get80211val(s, IEEE80211_IOC_AMPDU_LIMIT, &val) != -1) {
5279 			switch (val) {
5280 			case IEEE80211_HTCAP_MAXRXAMPDU_8K:
5281 				LINE_CHECK("ampdulimit 8k");
5282 				break;
5283 			case IEEE80211_HTCAP_MAXRXAMPDU_16K:
5284 				LINE_CHECK("ampdulimit 16k");
5285 				break;
5286 			case IEEE80211_HTCAP_MAXRXAMPDU_32K:
5287 				LINE_CHECK("ampdulimit 32k");
5288 				break;
5289 			case IEEE80211_HTCAP_MAXRXAMPDU_64K:
5290 				LINE_CHECK("ampdulimit 64k");
5291 				break;
5292 			}
5293 		}
5294 		/* XXX 11ac density/size is different */
5295 		if (get80211val(s, IEEE80211_IOC_AMPDU_DENSITY, &val) != -1) {
5296 			switch (val) {
5297 			case IEEE80211_HTCAP_MPDUDENSITY_NA:
5298 				if (verbose)
5299 					LINE_CHECK("ampdudensity NA");
5300 				break;
5301 			case IEEE80211_HTCAP_MPDUDENSITY_025:
5302 				LINE_CHECK("ampdudensity .25");
5303 				break;
5304 			case IEEE80211_HTCAP_MPDUDENSITY_05:
5305 				LINE_CHECK("ampdudensity .5");
5306 				break;
5307 			case IEEE80211_HTCAP_MPDUDENSITY_1:
5308 				LINE_CHECK("ampdudensity 1");
5309 				break;
5310 			case IEEE80211_HTCAP_MPDUDENSITY_2:
5311 				LINE_CHECK("ampdudensity 2");
5312 				break;
5313 			case IEEE80211_HTCAP_MPDUDENSITY_4:
5314 				LINE_CHECK("ampdudensity 4");
5315 				break;
5316 			case IEEE80211_HTCAP_MPDUDENSITY_8:
5317 				LINE_CHECK("ampdudensity 8");
5318 				break;
5319 			case IEEE80211_HTCAP_MPDUDENSITY_16:
5320 				LINE_CHECK("ampdudensity 16");
5321 				break;
5322 			}
5323 		}
5324 		if (get80211val(s, IEEE80211_IOC_AMSDU, &val) != -1) {
5325 			switch (val) {
5326 			case 0:
5327 				LINE_CHECK("-amsdu");
5328 				break;
5329 			case 1:
5330 				LINE_CHECK("amsdutx -amsdurx");
5331 				break;
5332 			case 2:
5333 				LINE_CHECK("-amsdutx amsdurx");
5334 				break;
5335 			case 3:
5336 				if (verbose)
5337 					LINE_CHECK("amsdu");
5338 				break;
5339 			}
5340 		}
5341 		/* XXX amsdu limit */
5342 		if (get80211val(s, IEEE80211_IOC_SHORTGI, &val) != -1) {
5343 			if (val)
5344 				LINE_CHECK("shortgi");
5345 			else if (verbose)
5346 				LINE_CHECK("-shortgi");
5347 		}
5348 		if (get80211val(s, IEEE80211_IOC_HTPROTMODE, &val) != -1) {
5349 			if (val == IEEE80211_PROTMODE_OFF)
5350 				LINE_CHECK("htprotmode OFF");
5351 			else if (val != IEEE80211_PROTMODE_RTSCTS)
5352 				LINE_CHECK("htprotmode UNKNOWN (0x%x)", val);
5353 			else if (verbose)
5354 				LINE_CHECK("htprotmode RTSCTS");
5355 		}
5356 		if (get80211val(s, IEEE80211_IOC_PUREN, &val) != -1) {
5357 			if (val)
5358 				LINE_CHECK("puren");
5359 			else if (verbose)
5360 				LINE_CHECK("-puren");
5361 		}
5362 		if (get80211val(s, IEEE80211_IOC_SMPS, &val) != -1) {
5363 			if (val == IEEE80211_HTCAP_SMPS_DYNAMIC)
5364 				LINE_CHECK("smpsdyn");
5365 			else if (val == IEEE80211_HTCAP_SMPS_ENA)
5366 				LINE_CHECK("smps");
5367 			else if (verbose)
5368 				LINE_CHECK("-smps");
5369 		}
5370 		if (get80211val(s, IEEE80211_IOC_RIFS, &val) != -1) {
5371 			if (val)
5372 				LINE_CHECK("rifs");
5373 			else if (verbose)
5374 				LINE_CHECK("-rifs");
5375 		}
5376 
5377 		/* XXX VHT STBC? */
5378 		if (get80211val(s, IEEE80211_IOC_STBC, &val) != -1) {
5379 			switch (val) {
5380 			case 0:
5381 				LINE_CHECK("-stbc");
5382 				break;
5383 			case 1:
5384 				LINE_CHECK("stbctx -stbcrx");
5385 				break;
5386 			case 2:
5387 				LINE_CHECK("-stbctx stbcrx");
5388 				break;
5389 			case 3:
5390 				if (verbose)
5391 					LINE_CHECK("stbc");
5392 				break;
5393 			}
5394 		}
5395 		if (get80211val(s, IEEE80211_IOC_LDPC, &val) != -1) {
5396 			switch (val) {
5397 			case 0:
5398 				LINE_CHECK("-ldpc");
5399 				break;
5400 			case 1:
5401 				LINE_CHECK("ldpctx -ldpcrx");
5402 				break;
5403 			case 2:
5404 				LINE_CHECK("-ldpctx ldpcrx");
5405 				break;
5406 			case 3:
5407 				if (verbose)
5408 					LINE_CHECK("ldpc");
5409 				break;
5410 			}
5411 		}
5412 		if (get80211val(s, IEEE80211_IOC_UAPSD, &val) != -1) {
5413 			switch (val) {
5414 			case 0:
5415 				LINE_CHECK("-uapsd");
5416 				break;
5417 			case 1:
5418 				LINE_CHECK("uapsd");
5419 				break;
5420 			}
5421 		}
5422 	}
5423 
5424 	if (IEEE80211_IS_CHAN_VHT(c) || verbose) {
5425 		getvhtconf(s);
5426 		if (vhtconf & IEEE80211_FVHT_VHT)
5427 			LINE_CHECK("vht");
5428 		else
5429 			LINE_CHECK("-vht");
5430 		if (vhtconf & IEEE80211_FVHT_USEVHT40)
5431 			LINE_CHECK("vht40");
5432 		else
5433 			LINE_CHECK("-vht40");
5434 		if (vhtconf & IEEE80211_FVHT_USEVHT80)
5435 			LINE_CHECK("vht80");
5436 		else
5437 			LINE_CHECK("-vht80");
5438 		if (vhtconf & IEEE80211_FVHT_USEVHT160)
5439 			LINE_CHECK("vht160");
5440 		else
5441 			LINE_CHECK("-vht160");
5442 		if (vhtconf & IEEE80211_FVHT_USEVHT80P80)
5443 			LINE_CHECK("vht80p80");
5444 		else
5445 			LINE_CHECK("-vht80p80");
5446 	}
5447 
5448 	if (get80211val(s, IEEE80211_IOC_WME, &wme) != -1) {
5449 		if (wme)
5450 			LINE_CHECK("wme");
5451 		else if (verbose)
5452 			LINE_CHECK("-wme");
5453 	} else
5454 		wme = 0;
5455 
5456 	if (get80211val(s, IEEE80211_IOC_BURST, &val) != -1) {
5457 		if (val)
5458 			LINE_CHECK("burst");
5459 		else if (verbose)
5460 			LINE_CHECK("-burst");
5461 	}
5462 
5463 	if (get80211val(s, IEEE80211_IOC_FF, &val) != -1) {
5464 		if (val)
5465 			LINE_CHECK("ff");
5466 		else if (verbose)
5467 			LINE_CHECK("-ff");
5468 	}
5469 	if (get80211val(s, IEEE80211_IOC_TURBOP, &val) != -1) {
5470 		if (val)
5471 			LINE_CHECK("dturbo");
5472 		else if (verbose)
5473 			LINE_CHECK("-dturbo");
5474 	}
5475 	if (get80211val(s, IEEE80211_IOC_DWDS, &val) != -1) {
5476 		if (val)
5477 			LINE_CHECK("dwds");
5478 		else if (verbose)
5479 			LINE_CHECK("-dwds");
5480 	}
5481 
5482 	if (opmode == IEEE80211_M_HOSTAP) {
5483 		if (get80211val(s, IEEE80211_IOC_HIDESSID, &val) != -1) {
5484 			if (val)
5485 				LINE_CHECK("hidessid");
5486 			else if (verbose)
5487 				LINE_CHECK("-hidessid");
5488 		}
5489 		if (get80211val(s, IEEE80211_IOC_APBRIDGE, &val) != -1) {
5490 			if (!val)
5491 				LINE_CHECK("-apbridge");
5492 			else if (verbose)
5493 				LINE_CHECK("apbridge");
5494 		}
5495 		if (get80211val(s, IEEE80211_IOC_DTIM_PERIOD, &val) != -1)
5496 			LINE_CHECK("dtimperiod %u", val);
5497 
5498 		if (get80211val(s, IEEE80211_IOC_DOTH, &val) != -1) {
5499 			if (!val)
5500 				LINE_CHECK("-doth");
5501 			else if (verbose)
5502 				LINE_CHECK("doth");
5503 		}
5504 		if (get80211val(s, IEEE80211_IOC_DFS, &val) != -1) {
5505 			if (!val)
5506 				LINE_CHECK("-dfs");
5507 			else if (verbose)
5508 				LINE_CHECK("dfs");
5509 		}
5510 		if (get80211val(s, IEEE80211_IOC_INACTIVITY, &val) != -1) {
5511 			if (!val)
5512 				LINE_CHECK("-inact");
5513 			else if (verbose)
5514 				LINE_CHECK("inact");
5515 		}
5516 	} else {
5517 		if (get80211val(s, IEEE80211_IOC_ROAMING, &val) != -1) {
5518 			if (val != IEEE80211_ROAMING_AUTO || verbose) {
5519 				switch (val) {
5520 				case IEEE80211_ROAMING_DEVICE:
5521 					LINE_CHECK("roaming DEVICE");
5522 					break;
5523 				case IEEE80211_ROAMING_AUTO:
5524 					LINE_CHECK("roaming AUTO");
5525 					break;
5526 				case IEEE80211_ROAMING_MANUAL:
5527 					LINE_CHECK("roaming MANUAL");
5528 					break;
5529 				default:
5530 					LINE_CHECK("roaming UNKNOWN (0x%x)",
5531 						val);
5532 					break;
5533 				}
5534 			}
5535 		}
5536 	}
5537 
5538 	if (opmode == IEEE80211_M_AHDEMO) {
5539 		if (get80211val(s, IEEE80211_IOC_TDMA_SLOT, &val) != -1)
5540 			LINE_CHECK("tdmaslot %u", val);
5541 		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTCNT, &val) != -1)
5542 			LINE_CHECK("tdmaslotcnt %u", val);
5543 		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTLEN, &val) != -1)
5544 			LINE_CHECK("tdmaslotlen %u", val);
5545 		if (get80211val(s, IEEE80211_IOC_TDMA_BINTERVAL, &val) != -1)
5546 			LINE_CHECK("tdmabintval %u", val);
5547 	} else if (get80211val(s, IEEE80211_IOC_BEACON_INTERVAL, &val) != -1) {
5548 		/* XXX default define not visible */
5549 		if (val != 100 || verbose)
5550 			LINE_CHECK("bintval %u", val);
5551 	}
5552 
5553 	if (wme && verbose) {
5554 		LINE_BREAK();
5555 		list_wme(s);
5556 	}
5557 
5558 	if (opmode == IEEE80211_M_MBSS) {
5559 		if (get80211val(s, IEEE80211_IOC_MESH_TTL, &val) != -1) {
5560 			LINE_CHECK("meshttl %u", val);
5561 		}
5562 		if (get80211val(s, IEEE80211_IOC_MESH_AP, &val) != -1) {
5563 			if (val)
5564 				LINE_CHECK("meshpeering");
5565 			else
5566 				LINE_CHECK("-meshpeering");
5567 		}
5568 		if (get80211val(s, IEEE80211_IOC_MESH_FWRD, &val) != -1) {
5569 			if (val)
5570 				LINE_CHECK("meshforward");
5571 			else
5572 				LINE_CHECK("-meshforward");
5573 		}
5574 		if (get80211val(s, IEEE80211_IOC_MESH_GATE, &val) != -1) {
5575 			if (val)
5576 				LINE_CHECK("meshgate");
5577 			else
5578 				LINE_CHECK("-meshgate");
5579 		}
5580 		if (get80211len(s, IEEE80211_IOC_MESH_PR_METRIC, data, 12,
5581 		    &len) != -1) {
5582 			data[len] = '\0';
5583 			LINE_CHECK("meshmetric %s", data);
5584 		}
5585 		if (get80211len(s, IEEE80211_IOC_MESH_PR_PATH, data, 12,
5586 		    &len) != -1) {
5587 			data[len] = '\0';
5588 			LINE_CHECK("meshpath %s", data);
5589 		}
5590 		if (get80211val(s, IEEE80211_IOC_HWMP_ROOTMODE, &val) != -1) {
5591 			switch (val) {
5592 			case IEEE80211_HWMP_ROOTMODE_DISABLED:
5593 				LINE_CHECK("hwmprootmode DISABLED");
5594 				break;
5595 			case IEEE80211_HWMP_ROOTMODE_NORMAL:
5596 				LINE_CHECK("hwmprootmode NORMAL");
5597 				break;
5598 			case IEEE80211_HWMP_ROOTMODE_PROACTIVE:
5599 				LINE_CHECK("hwmprootmode PROACTIVE");
5600 				break;
5601 			case IEEE80211_HWMP_ROOTMODE_RANN:
5602 				LINE_CHECK("hwmprootmode RANN");
5603 				break;
5604 			default:
5605 				LINE_CHECK("hwmprootmode UNKNOWN(%d)", val);
5606 				break;
5607 			}
5608 		}
5609 		if (get80211val(s, IEEE80211_IOC_HWMP_MAXHOPS, &val) != -1) {
5610 			LINE_CHECK("hwmpmaxhops %u", val);
5611 		}
5612 	}
5613 
5614 	LINE_BREAK();
5615 
5616 	if (getdevicename(s, data, sizeof(data), &len) < 0)
5617 		return;
5618 	LINE_CHECK("parent interface: %s", data);
5619 
5620 	LINE_BREAK();
5621 }
5622 
5623 static int
5624 get80211(int s, int type, void *data, int len)
5625 {
5626 
5627 	return (lib80211_get80211(s, name, type, data, len));
5628 }
5629 
5630 static int
5631 get80211len(int s, int type, void *data, int len, int *plen)
5632 {
5633 
5634 	return (lib80211_get80211len(s, name, type, data, len, plen));
5635 }
5636 
5637 static int
5638 get80211val(int s, int type, int *val)
5639 {
5640 
5641 	return (lib80211_get80211val(s, name, type, val));
5642 }
5643 
5644 static void
5645 set80211(int s, int type, int val, int len, void *data)
5646 {
5647 	int ret;
5648 
5649 	ret = lib80211_set80211(s, name, type, val, len, data);
5650 	if (ret < 0)
5651 		err(1, "SIOCS80211");
5652 }
5653 
5654 static const char *
5655 get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
5656 {
5657 	int len;
5658 	int hexstr;
5659 	u_int8_t *p;
5660 
5661 	len = *lenp;
5662 	p = buf;
5663 	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
5664 	if (hexstr)
5665 		val += 2;
5666 	for (;;) {
5667 		if (*val == '\0')
5668 			break;
5669 		if (sep != NULL && strchr(sep, *val) != NULL) {
5670 			val++;
5671 			break;
5672 		}
5673 		if (hexstr) {
5674 			if (!isxdigit((u_char)val[0])) {
5675 				warnx("bad hexadecimal digits");
5676 				return NULL;
5677 			}
5678 			if (!isxdigit((u_char)val[1])) {
5679 				warnx("odd count hexadecimal digits");
5680 				return NULL;
5681 			}
5682 		}
5683 		if (p >= buf + len) {
5684 			if (hexstr)
5685 				warnx("hexadecimal digits too long");
5686 			else
5687 				warnx("string too long");
5688 			return NULL;
5689 		}
5690 		if (hexstr) {
5691 #define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
5692 			*p++ = (tohex((u_char)val[0]) << 4) |
5693 			    tohex((u_char)val[1]);
5694 #undef tohex
5695 			val += 2;
5696 		} else
5697 			*p++ = *val++;
5698 	}
5699 	len = p - buf;
5700 	/* The string "-" is treated as the empty string. */
5701 	if (!hexstr && len == 1 && buf[0] == '-') {
5702 		len = 0;
5703 		memset(buf, 0, *lenp);
5704 	} else if (len < *lenp)
5705 		memset(p, 0, *lenp - len);
5706 	*lenp = len;
5707 	return val;
5708 }
5709 
5710 static void
5711 print_string(const u_int8_t *buf, int len)
5712 {
5713 	int i;
5714 	int hasspc;
5715 	int utf8;
5716 
5717 	i = 0;
5718 	hasspc = 0;
5719 
5720 	setlocale(LC_CTYPE, "");
5721 	utf8 = strncmp("UTF-8", nl_langinfo(CODESET), 5) == 0;
5722 
5723 	for (; i < len; i++) {
5724 		if (!isprint(buf[i]) && buf[i] != '\0' && !utf8)
5725 			break;
5726 		if (isspace(buf[i]))
5727 			hasspc++;
5728 	}
5729 	if (i == len || utf8) {
5730 		if (hasspc || len == 0 || buf[0] == '\0')
5731 			printf("\"%.*s\"", len, buf);
5732 		else
5733 			printf("%.*s", len, buf);
5734 	} else {
5735 		printf("0x");
5736 		for (i = 0; i < len; i++)
5737 			printf("%02x", buf[i]);
5738 	}
5739 }
5740 
5741 static void
5742 setdefregdomain(int s)
5743 {
5744 	struct regdata *rdp = getregdata();
5745 	const struct regdomain *rd;
5746 
5747 	/* Check if regdomain/country was already set by a previous call. */
5748 	/* XXX is it possible? */
5749 	if (regdomain.regdomain != 0 ||
5750 	    regdomain.country != CTRY_DEFAULT)
5751 		return;
5752 
5753 	getregdomain(s);
5754 
5755 	/* Check if it was already set by the driver. */
5756 	if (regdomain.regdomain != 0 ||
5757 	    regdomain.country != CTRY_DEFAULT)
5758 		return;
5759 
5760 	/* Set FCC/US as default. */
5761 	rd = lib80211_regdomain_findbysku(rdp, SKU_FCC);
5762 	if (rd == NULL)
5763 		errx(1, "FCC regdomain was not found");
5764 
5765 	regdomain.regdomain = rd->sku;
5766 	if (rd->cc != NULL)
5767 		defaultcountry(rd);
5768 
5769 	/* Send changes to net80211. */
5770 	setregdomain_cb(s, &regdomain);
5771 
5772 	/* Cleanup (so it can be overridden by subsequent parameters). */
5773 	regdomain.regdomain = 0;
5774 	regdomain.country = CTRY_DEFAULT;
5775 	regdomain.isocc[0] = 0;
5776 	regdomain.isocc[1] = 0;
5777 }
5778 
5779 /*
5780  * Virtual AP cloning support.
5781  */
5782 static struct ieee80211_clone_params params = {
5783 	.icp_opmode	= IEEE80211_M_STA,	/* default to station mode */
5784 };
5785 
5786 static void
5787 wlan_create(int s, struct ifreq *ifr)
5788 {
5789 	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
5790 	char orig_name[IFNAMSIZ];
5791 
5792 	if (params.icp_parent[0] == '\0')
5793 		errx(1, "must specify a parent device (wlandev) when creating "
5794 		    "a wlan device");
5795 	if (params.icp_opmode == IEEE80211_M_WDS &&
5796 	    memcmp(params.icp_bssid, zerobssid, sizeof(zerobssid)) == 0)
5797 		errx(1, "no bssid specified for WDS (use wlanbssid)");
5798 	ifr->ifr_data = (caddr_t) &params;
5799 	ioctl_ifcreate(s, ifr);
5800 
5801 	/* XXX preserve original name for ifclonecreate(). */
5802 	strlcpy(orig_name, name, sizeof(orig_name));
5803 	strlcpy(name, ifr->ifr_name, sizeof(name));
5804 
5805 	setdefregdomain(s);
5806 
5807 	strlcpy(name, orig_name, sizeof(name));
5808 }
5809 
5810 static void
5811 set80211clone_wlandev(if_ctx *ctx __unused, const char *arg, int dummy __unused)
5812 {
5813 	strlcpy(params.icp_parent, arg, IFNAMSIZ);
5814 }
5815 
5816 static void
5817 set80211clone_wlanbssid(if_ctx *ctx __unused, const char *arg, int dummy __unused)
5818 {
5819 	const struct ether_addr *ea;
5820 
5821 	ea = ether_aton(arg);
5822 	if (ea == NULL)
5823 		errx(1, "%s: cannot parse bssid", arg);
5824 	memcpy(params.icp_bssid, ea->octet, IEEE80211_ADDR_LEN);
5825 }
5826 
5827 static void
5828 set80211clone_wlanaddr(if_ctx *ctx __unused, const char *arg, int dummy __unused)
5829 {
5830 	const struct ether_addr *ea;
5831 
5832 	ea = ether_aton(arg);
5833 	if (ea == NULL)
5834 		errx(1, "%s: cannot parse address", arg);
5835 	memcpy(params.icp_macaddr, ea->octet, IEEE80211_ADDR_LEN);
5836 	params.icp_flags |= IEEE80211_CLONE_MACADDR;
5837 }
5838 
5839 static void
5840 set80211clone_wlanmode(if_ctx *ctx __unused, const char *arg, int dummy __unused)
5841 {
5842 #define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
5843 	if (iseq(arg, "sta"))
5844 		params.icp_opmode = IEEE80211_M_STA;
5845 	else if (iseq(arg, "ahdemo") || iseq(arg, "adhoc-demo"))
5846 		params.icp_opmode = IEEE80211_M_AHDEMO;
5847 	else if (iseq(arg, "ibss") || iseq(arg, "adhoc"))
5848 		params.icp_opmode = IEEE80211_M_IBSS;
5849 	else if (iseq(arg, "ap") || iseq(arg, "host"))
5850 		params.icp_opmode = IEEE80211_M_HOSTAP;
5851 	else if (iseq(arg, "wds"))
5852 		params.icp_opmode = IEEE80211_M_WDS;
5853 	else if (iseq(arg, "monitor"))
5854 		params.icp_opmode = IEEE80211_M_MONITOR;
5855 	else if (iseq(arg, "tdma")) {
5856 		params.icp_opmode = IEEE80211_M_AHDEMO;
5857 		params.icp_flags |= IEEE80211_CLONE_TDMA;
5858 	} else if (iseq(arg, "mesh") || iseq(arg, "mp")) /* mesh point */
5859 		params.icp_opmode = IEEE80211_M_MBSS;
5860 	else
5861 		errx(1, "Don't know to create %s for %s", arg, name);
5862 #undef iseq
5863 }
5864 
5865 static void
5866 set80211clone_beacons(if_ctx *ctx __unused, const char *val __unused, int d)
5867 {
5868 	/* NB: inverted sense */
5869 	if (d)
5870 		params.icp_flags &= ~IEEE80211_CLONE_NOBEACONS;
5871 	else
5872 		params.icp_flags |= IEEE80211_CLONE_NOBEACONS;
5873 }
5874 
5875 static void
5876 set80211clone_bssid(if_ctx *ctx __unused, const char *val __unused, int d)
5877 {
5878 	if (d)
5879 		params.icp_flags |= IEEE80211_CLONE_BSSID;
5880 	else
5881 		params.icp_flags &= ~IEEE80211_CLONE_BSSID;
5882 }
5883 
5884 static void
5885 set80211clone_wdslegacy(if_ctx *ctx __unused, const char *val __unused, int d)
5886 {
5887 	if (d)
5888 		params.icp_flags |= IEEE80211_CLONE_WDSLEGACY;
5889 	else
5890 		params.icp_flags &= ~IEEE80211_CLONE_WDSLEGACY;
5891 }
5892 
5893 static struct cmd ieee80211_cmds[] = {
5894 	DEF_CMD_ARG("ssid",		set80211ssid),
5895 	DEF_CMD_ARG("nwid",		set80211ssid),
5896 	DEF_CMD_ARG("meshid",		set80211meshid),
5897 	DEF_CMD_ARG("stationname",	set80211stationname),
5898 	DEF_CMD_ARG("station",		set80211stationname),	/* BSD/OS */
5899 	DEF_CMD_ARG("channel",		set80211channel),
5900 	DEF_CMD_ARG("authmode",		set80211authmode),
5901 	DEF_CMD_ARG("powersavemode",	set80211powersavemode),
5902 	DEF_CMD("powersave",	1,	set80211powersave),
5903 	DEF_CMD("-powersave",	0,	set80211powersave),
5904 	DEF_CMD_ARG("powersavesleep", 	set80211powersavesleep),
5905 	DEF_CMD_ARG("wepmode",		set80211wepmode),
5906 	DEF_CMD("wep",		1,	set80211wep),
5907 	DEF_CMD("-wep",		0,	set80211wep),
5908 	DEF_CMD_ARG("deftxkey",		set80211weptxkey),
5909 	DEF_CMD_ARG("weptxkey",		set80211weptxkey),
5910 	DEF_CMD_ARG("wepkey",		set80211wepkey),
5911 	DEF_CMD_ARG("nwkey",		set80211nwkey),		/* NetBSD */
5912 	DEF_CMD("-nwkey",	0,	set80211wep),		/* NetBSD */
5913 	DEF_CMD_ARG("rtsthreshold",	set80211rtsthreshold),
5914 	DEF_CMD_ARG("protmode",		set80211protmode),
5915 	DEF_CMD_ARG("txpower",		set80211txpower),
5916 	DEF_CMD_ARG("roaming",		set80211roaming),
5917 	DEF_CMD("wme",		1,	set80211wme),
5918 	DEF_CMD("-wme",		0,	set80211wme),
5919 	DEF_CMD("wmm",		1,	set80211wme),
5920 	DEF_CMD("-wmm",		0,	set80211wme),
5921 	DEF_CMD("hidessid",	1,	set80211hidessid),
5922 	DEF_CMD("-hidessid",	0,	set80211hidessid),
5923 	DEF_CMD("apbridge",	1,	set80211apbridge),
5924 	DEF_CMD("-apbridge",	0,	set80211apbridge),
5925 	DEF_CMD_ARG("chanlist",		set80211chanlist),
5926 	DEF_CMD_ARG("bssid",		set80211bssid),
5927 	DEF_CMD_ARG("ap",		set80211bssid),
5928 	DEF_CMD("scan",	0,		set80211scan),
5929 	DEF_CMD_ARG("list",		set80211list),
5930 	DEF_CMD_ARG2("cwmin",		set80211cwmin),
5931 	DEF_CMD_ARG2("cwmax",		set80211cwmax),
5932 	DEF_CMD_ARG2("aifs",		set80211aifs),
5933 	DEF_CMD_ARG2("txoplimit",	set80211txoplimit),
5934 	DEF_CMD_ARG("acm",		set80211acm),
5935 	DEF_CMD_ARG("-acm",		set80211noacm),
5936 	DEF_CMD_ARG("ack",		set80211ackpolicy),
5937 	DEF_CMD_ARG("-ack",		set80211noackpolicy),
5938 	DEF_CMD_ARG2("bss:cwmin",	set80211bsscwmin),
5939 	DEF_CMD_ARG2("bss:cwmax",	set80211bsscwmax),
5940 	DEF_CMD_ARG2("bss:aifs",	set80211bssaifs),
5941 	DEF_CMD_ARG2("bss:txoplimit",	set80211bsstxoplimit),
5942 	DEF_CMD_ARG("dtimperiod",	set80211dtimperiod),
5943 	DEF_CMD_ARG("bintval",		set80211bintval),
5944 	DEF_CMD("mac:open",	IEEE80211_MACCMD_POLICY_OPEN,	set80211maccmd),
5945 	DEF_CMD("mac:allow",	IEEE80211_MACCMD_POLICY_ALLOW,	set80211maccmd),
5946 	DEF_CMD("mac:deny",	IEEE80211_MACCMD_POLICY_DENY,	set80211maccmd),
5947 	DEF_CMD("mac:radius",	IEEE80211_MACCMD_POLICY_RADIUS,	set80211maccmd),
5948 	DEF_CMD("mac:flush",	IEEE80211_MACCMD_FLUSH,		set80211maccmd),
5949 	DEF_CMD("mac:detach",	IEEE80211_MACCMD_DETACH,	set80211maccmd),
5950 	DEF_CMD_ARG("mac:add",		set80211addmac),
5951 	DEF_CMD_ARG("mac:del",		set80211delmac),
5952 	DEF_CMD_ARG("mac:kick",		set80211kickmac),
5953 	DEF_CMD("pureg",	1,	set80211pureg),
5954 	DEF_CMD("-pureg",	0,	set80211pureg),
5955 	DEF_CMD("ff",		1,	set80211fastframes),
5956 	DEF_CMD("-ff",		0,	set80211fastframes),
5957 	DEF_CMD("dturbo",	1,	set80211dturbo),
5958 	DEF_CMD("-dturbo",	0,	set80211dturbo),
5959 	DEF_CMD("bgscan",	1,	set80211bgscan),
5960 	DEF_CMD("-bgscan",	0,	set80211bgscan),
5961 	DEF_CMD_ARG("bgscanidle",	set80211bgscanidle),
5962 	DEF_CMD_ARG("bgscanintvl",	set80211bgscanintvl),
5963 	DEF_CMD_ARG("scanvalid",	set80211scanvalid),
5964 	DEF_CMD("quiet",	1,	set80211quiet),
5965 	DEF_CMD("-quiet",	0,	set80211quiet),
5966 	DEF_CMD_ARG("quiet_count",	set80211quietcount),
5967 	DEF_CMD_ARG("quiet_period",	set80211quietperiod),
5968 	DEF_CMD_ARG("quiet_duration",	set80211quietduration),
5969 	DEF_CMD_ARG("quiet_offset",	set80211quietoffset),
5970 	DEF_CMD_ARG("roam:rssi",	set80211roamrssi),
5971 	DEF_CMD_ARG("roam:rate",	set80211roamrate),
5972 	DEF_CMD_ARG("mcastrate",	set80211mcastrate),
5973 	DEF_CMD_ARG("ucastrate",	set80211ucastrate),
5974 	DEF_CMD_ARG("mgtrate",		set80211mgtrate),
5975 	DEF_CMD_ARG("mgmtrate",		set80211mgtrate),
5976 	DEF_CMD_ARG("maxretry",		set80211maxretry),
5977 	DEF_CMD_ARG("fragthreshold",	set80211fragthreshold),
5978 	DEF_CMD("burst",	1,	set80211burst),
5979 	DEF_CMD("-burst",	0,	set80211burst),
5980 	DEF_CMD_ARG("bmiss",		set80211bmissthreshold),
5981 	DEF_CMD_ARG("bmissthreshold",	set80211bmissthreshold),
5982 	DEF_CMD("shortgi",	1,	set80211shortgi),
5983 	DEF_CMD("-shortgi",	0,	set80211shortgi),
5984 	DEF_CMD("ampdurx",	2,	set80211ampdu),
5985 	DEF_CMD("-ampdurx",	-2,	set80211ampdu),
5986 	DEF_CMD("ampdutx",	1,	set80211ampdu),
5987 	DEF_CMD("-ampdutx",	-1,	set80211ampdu),
5988 	DEF_CMD("ampdu",	3,	set80211ampdu),		/* NB: tx+rx */
5989 	DEF_CMD("-ampdu",	-3,	set80211ampdu),
5990 	DEF_CMD_ARG("ampdulimit",	set80211ampdulimit),
5991 	DEF_CMD_ARG("ampdudensity",	set80211ampdudensity),
5992 	DEF_CMD("amsdurx",	2,	set80211amsdu),
5993 	DEF_CMD("-amsdurx",	-2,	set80211amsdu),
5994 	DEF_CMD("amsdutx",	1,	set80211amsdu),
5995 	DEF_CMD("-amsdutx",	-1,	set80211amsdu),
5996 	DEF_CMD("amsdu",	3,	set80211amsdu),		/* NB: tx+rx */
5997 	DEF_CMD("-amsdu",	-3,	set80211amsdu),
5998 	DEF_CMD_ARG("amsdulimit",	set80211amsdulimit),
5999 	DEF_CMD("stbcrx",	2,	set80211stbc),
6000 	DEF_CMD("-stbcrx",	-2,	set80211stbc),
6001 	DEF_CMD("stbctx",	1,	set80211stbc),
6002 	DEF_CMD("-stbctx",	-1,	set80211stbc),
6003 	DEF_CMD("stbc",		3,	set80211stbc),		/* NB: tx+rx */
6004 	DEF_CMD("-stbc",	-3,	set80211stbc),
6005 	DEF_CMD("ldpcrx",	2,	set80211ldpc),
6006 	DEF_CMD("-ldpcrx",	-2,	set80211ldpc),
6007 	DEF_CMD("ldpctx",	1,	set80211ldpc),
6008 	DEF_CMD("-ldpctx",	-1,	set80211ldpc),
6009 	DEF_CMD("ldpc",		3,	set80211ldpc),		/* NB: tx+rx */
6010 	DEF_CMD("-ldpc",	-3,	set80211ldpc),
6011 	DEF_CMD("uapsd",	1,	set80211uapsd),
6012 	DEF_CMD("-uapsd",	0,	set80211uapsd),
6013 	DEF_CMD("puren",	1,	set80211puren),
6014 	DEF_CMD("-puren",	0,	set80211puren),
6015 	DEF_CMD("doth",		1,	set80211doth),
6016 	DEF_CMD("-doth",	0,	set80211doth),
6017 	DEF_CMD("dfs",		1,	set80211dfs),
6018 	DEF_CMD("-dfs",		0,	set80211dfs),
6019 	DEF_CMD("htcompat",	1,	set80211htcompat),
6020 	DEF_CMD("-htcompat",	0,	set80211htcompat),
6021 	DEF_CMD("dwds",		1,	set80211dwds),
6022 	DEF_CMD("-dwds",	0,	set80211dwds),
6023 	DEF_CMD("inact",	1,	set80211inact),
6024 	DEF_CMD("-inact",	0,	set80211inact),
6025 	DEF_CMD("tsn",		1,	set80211tsn),
6026 	DEF_CMD("-tsn",		0,	set80211tsn),
6027 	DEF_CMD_ARG("regdomain",	set80211regdomain),
6028 	DEF_CMD_ARG("country",		set80211country),
6029 	DEF_CMD("indoor",	'I',	set80211location),
6030 	DEF_CMD("-indoor",	'O',	set80211location),
6031 	DEF_CMD("outdoor",	'O',	set80211location),
6032 	DEF_CMD("-outdoor",	'I',	set80211location),
6033 	DEF_CMD("anywhere",	' ',	set80211location),
6034 	DEF_CMD("ecm",		1,	set80211ecm),
6035 	DEF_CMD("-ecm",		0,	set80211ecm),
6036 	DEF_CMD("dotd",		1,	set80211dotd),
6037 	DEF_CMD("-dotd",	0,	set80211dotd),
6038 	DEF_CMD_ARG("htprotmode",	set80211htprotmode),
6039 	DEF_CMD("ht20",		1,	set80211htconf),
6040 	DEF_CMD("-ht20",	0,	set80211htconf),
6041 	DEF_CMD("ht40",		3,	set80211htconf),	/* NB: 20+40 */
6042 	DEF_CMD("-ht40",	0,	set80211htconf),
6043 	DEF_CMD("ht",		3,	set80211htconf),	/* NB: 20+40 */
6044 	DEF_CMD("-ht",		0,	set80211htconf),
6045 	DEF_CMD("vht",		IEEE80211_FVHT_VHT,		set80211vhtconf),
6046 	DEF_CMD("-vht",		0,				set80211vhtconf),
6047 	DEF_CMD("vht40",	IEEE80211_FVHT_USEVHT40,	set80211vhtconf),
6048 	DEF_CMD("-vht40",	-IEEE80211_FVHT_USEVHT40,	set80211vhtconf),
6049 	DEF_CMD("vht80",	IEEE80211_FVHT_USEVHT80,	set80211vhtconf),
6050 	DEF_CMD("-vht80",	-IEEE80211_FVHT_USEVHT80,	set80211vhtconf),
6051 	DEF_CMD("vht160",	IEEE80211_FVHT_USEVHT160,	set80211vhtconf),
6052 	DEF_CMD("-vht160",	-IEEE80211_FVHT_USEVHT160,	set80211vhtconf),
6053 	DEF_CMD("vht80p80",	IEEE80211_FVHT_USEVHT80P80,	set80211vhtconf),
6054 	DEF_CMD("-vht80p80",	-IEEE80211_FVHT_USEVHT80P80,	set80211vhtconf),
6055 	DEF_CMD("rifs",		1,	set80211rifs),
6056 	DEF_CMD("-rifs",	0,	set80211rifs),
6057 	DEF_CMD("smps",		IEEE80211_HTCAP_SMPS_ENA,	set80211smps),
6058 	DEF_CMD("smpsdyn",	IEEE80211_HTCAP_SMPS_DYNAMIC,	set80211smps),
6059 	DEF_CMD("-smps",	IEEE80211_HTCAP_SMPS_OFF,	set80211smps),
6060 	/* XXX for testing */
6061 	DEF_CMD_ARG("chanswitch",	set80211chanswitch),
6062 
6063 	DEF_CMD_ARG("tdmaslot",		set80211tdmaslot),
6064 	DEF_CMD_ARG("tdmaslotcnt",	set80211tdmaslotcnt),
6065 	DEF_CMD_ARG("tdmaslotlen",	set80211tdmaslotlen),
6066 	DEF_CMD_ARG("tdmabintval",	set80211tdmabintval),
6067 
6068 	DEF_CMD_ARG("meshttl",		set80211meshttl),
6069 	DEF_CMD("meshforward",	1,	set80211meshforward),
6070 	DEF_CMD("-meshforward",	0,	set80211meshforward),
6071 	DEF_CMD("meshgate",	1,	set80211meshgate),
6072 	DEF_CMD("-meshgate",	0,	set80211meshgate),
6073 	DEF_CMD("meshpeering",	1,	set80211meshpeering),
6074 	DEF_CMD("-meshpeering",	0,	set80211meshpeering),
6075 	DEF_CMD_ARG("meshmetric",	set80211meshmetric),
6076 	DEF_CMD_ARG("meshpath",		set80211meshpath),
6077 	DEF_CMD("meshrt:flush",	IEEE80211_MESH_RTCMD_FLUSH,	set80211meshrtcmd),
6078 	DEF_CMD_ARG("meshrt:add",	set80211addmeshrt),
6079 	DEF_CMD_ARG("meshrt:del",	set80211delmeshrt),
6080 	DEF_CMD_ARG("hwmprootmode",	set80211hwmprootmode),
6081 	DEF_CMD_ARG("hwmpmaxhops",	set80211hwmpmaxhops),
6082 
6083 	/* vap cloning support */
6084 	DEF_CLONE_CMD_ARG("wlanaddr",	set80211clone_wlanaddr),
6085 	DEF_CLONE_CMD_ARG("wlanbssid",	set80211clone_wlanbssid),
6086 	DEF_CLONE_CMD_ARG("wlandev",	set80211clone_wlandev),
6087 	DEF_CLONE_CMD_ARG("wlanmode",	set80211clone_wlanmode),
6088 	DEF_CLONE_CMD("beacons", 1,	set80211clone_beacons),
6089 	DEF_CLONE_CMD("-beacons", 0,	set80211clone_beacons),
6090 	DEF_CLONE_CMD("bssid",	1,	set80211clone_bssid),
6091 	DEF_CLONE_CMD("-bssid",	0,	set80211clone_bssid),
6092 	DEF_CLONE_CMD("wdslegacy", 1,	set80211clone_wdslegacy),
6093 	DEF_CLONE_CMD("-wdslegacy", 0,	set80211clone_wdslegacy),
6094 };
6095 static struct afswtch af_ieee80211 = {
6096 	.af_name	= "af_ieee80211",
6097 	.af_af		= AF_UNSPEC,
6098 	.af_other_status = ieee80211_status,
6099 };
6100 
6101 static __constructor void
6102 ieee80211_ctor(void)
6103 {
6104 	for (size_t i = 0; i < nitems(ieee80211_cmds);  i++)
6105 		cmd_register(&ieee80211_cmds[i]);
6106 	af_register(&af_ieee80211);
6107 	clone_setdefcallback_prefix("wlan", wlan_create);
6108 }
6109