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