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