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