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