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