ifieee80211.c revision 183261
177218Sphk/*
277218Sphk * Copyright 2001 The Aerospace Corporation.  All rights reserved.
377218Sphk *
477218Sphk * Redistribution and use in source and binary forms, with or without
577218Sphk * modification, are permitted provided that the following conditions
677218Sphk * are met:
777218Sphk * 1. Redistributions of source code must retain the above copyright
877218Sphk *    notice, this list of conditions and the following disclaimer.
977218Sphk * 2. Redistributions in binary form must reproduce the above copyright
1077218Sphk *    notice, this list of conditions and the following disclaimer in the
1177218Sphk *    documentation and/or other materials provided with the distribution.
1291454Sbrooks * 3. The name of The Aerospace Corporation may not be used to endorse or
1391454Sbrooks *    promote products derived from this software.
1477218Sphk *
1577218Sphk * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION ``AS IS'' AND
1677218Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1777218Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1877218Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE
1977218Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2077218Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2177218Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2277218Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2377218Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2477218Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2577218Sphk * SUCH DAMAGE.
2677218Sphk *
2777218Sphk * $FreeBSD: head/sbin/ifconfig/ifieee80211.c 183261 2008-09-22 00:30:26Z sam $
2877218Sphk */
2977218Sphk
3077218Sphk/*-
3177218Sphk * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
3277218Sphk * All rights reserved.
3377218Sphk *
3477218Sphk * This code is derived from software contributed to The NetBSD Foundation
3577218Sphk * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
3677218Sphk * NASA Ames Research Center.
3777218Sphk *
3877218Sphk * Redistribution and use in source and binary forms, with or without
3977218Sphk * modification, are permitted provided that the following conditions
4077218Sphk * are met:
4177218Sphk * 1. Redistributions of source code must retain the above copyright
4277218Sphk *    notice, this list of conditions and the following disclaimer.
4377218Sphk * 2. Redistributions in binary form must reproduce the above copyright
4477218Sphk *    notice, this list of conditions and the following disclaimer in the
4577218Sphk *    documentation and/or other materials provided with the distribution.
4677218Sphk * 3. All advertising materials mentioning features or use of this software
4777218Sphk *    must display the following acknowledgement:
4877218Sphk *	This product includes software developed by the NetBSD
4977218Sphk *	Foundation, Inc. and its contributors.
5077218Sphk * 4. Neither the name of The NetBSD Foundation nor the names of its
5177218Sphk *    contributors may be used to endorse or promote products derived
5277218Sphk *    from this software without specific prior written permission.
5377218Sphk *
5477218Sphk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
5577218Sphk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
5677218Sphk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5777218Sphk * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
5877218Sphk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5977218Sphk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
6077218Sphk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
6177218Sphk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
6277218Sphk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
6377218Sphk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
6477218Sphk * POSSIBILITY OF SUCH DAMAGE.
6577218Sphk */
6677218Sphk
6777218Sphk#include <sys/param.h>
6877218Sphk#include <sys/ioctl.h>
6977218Sphk#include <sys/socket.h>
7077218Sphk#include <sys/sysctl.h>
7177218Sphk#include <sys/time.h>
7277218Sphk
7377218Sphk#include <net/ethernet.h>
7477218Sphk#include <net/if.h>
7577218Sphk#include <net/if_dl.h>
7677218Sphk#include <net/if_types.h>
77138593Ssam#include <net/if_media.h>
7877218Sphk#include <net/route.h>
79138593Ssam
80116957Ssam#include <net80211/ieee80211_ioctl.h>
8177218Sphk
8277218Sphk#include <ctype.h>
8377218Sphk#include <err.h>
8477218Sphk#include <errno.h>
8577218Sphk#include <fcntl.h>
86146873Sjhb#include <inttypes.h>
8777218Sphk#include <stdio.h>
8877218Sphk#include <stdlib.h>
8977218Sphk#include <string.h>
9077218Sphk#include <unistd.h>
91155931Ssam#include <stdarg.h>
92173275Ssam#include <stddef.h>		/* NB: for offsetof */
9377218Sphk
9477218Sphk#include "ifconfig.h"
95178354Ssam#include "regdomain.h"
9677218Sphk
97178354Ssam#ifndef IEEE80211_FIXED_RATE_NONE
98178354Ssam#define	IEEE80211_FIXED_RATE_NONE	0xff
99178354Ssam#endif
100178354Ssam
101178354Ssam#define	REQ_ECM		0x01000000	/* enable if ECM set */
102178354Ssam#define	REQ_OUTDOOR	0x02000000	/* enable for outdoor operation */
103178354Ssam#define	REQ_FLAGS	0xff000000	/* private flags, don't pass to os */
104178354Ssam
105178354Ssam/* XXX need these publicly defined or similar */
106178354Ssam#ifndef IEEE80211_NODE_AUTH
107178354Ssam#define	IEEE80211_NODE_AUTH	0x0001		/* authorized for data */
108178354Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
109178354Ssam#define	IEEE80211_NODE_ERP	0x0004		/* ERP enabled */
110178354Ssam#define	IEEE80211_NODE_PWR_MGT	0x0010		/* power save mode enabled */
111178354Ssam#define	IEEE80211_NODE_HT	0x0040		/* HT enabled */
112178354Ssam#define	IEEE80211_NODE_HTCOMPAT	0x0080		/* HT setup w/ vendor OUI's */
113178354Ssam#define	IEEE80211_NODE_WPS	0x0100		/* WPS association */
114178354Ssam#define	IEEE80211_NODE_TSN	0x0200		/* TSN association */
115183261Ssam#define	IEEE80211_NODE_AMPDU_RX	0x0400		/* AMPDU rx enabled */
116183261Ssam#define	IEEE80211_NODE_AMPDU_TX	0x0800		/* AMPDU tx enabled */
117183261Ssam#define	IEEE80211_NODE_MIMO_PS	0x1000		/* MIMO power save enabled */
118183261Ssam#define	IEEE80211_NODE_MIMO_RTS	0x2000		/* send RTS in MIMO PS */
119183261Ssam#define	IEEE80211_NODE_RIFS	0x4000		/* RIFS enabled */
120178354Ssam#endif
121178354Ssam
122173275Ssam#define	MAXCOL	78
123173275Ssamstatic	int col;
124173275Ssamstatic	char spacer;
125173275Ssam
126173275Ssamstatic void LINE_INIT(char c);
127173275Ssamstatic void LINE_BREAK(void);
128173275Ssamstatic void LINE_CHECK(const char *fmt, ...);
129173275Ssam
130178354Ssamstatic const char *modename[] = {
131178354Ssam	"auto", "11a", "11b", "11g", "fh", "turboA", "turboG",
132178354Ssam	"sturbo", "11na", "11ng"
133173275Ssam};
134173275Ssam
135178354Ssamstatic void set80211(int s, int type, int val, int len, void *data);
136173275Ssamstatic int get80211(int s, int type, void *data, int len);
137173275Ssamstatic int get80211len(int s, int type, void *data, int len, int *plen);
138173275Ssamstatic int get80211val(int s, int type, int *val);
13977218Sphkstatic const char *get_string(const char *val, const char *sep,
14077218Sphk    u_int8_t *buf, int *lenp);
14177218Sphkstatic void print_string(const u_int8_t *buf, int len);
142178354Ssamstatic void print_regdomain(const struct ieee80211_regdomain *, int);
143178354Ssamstatic void print_channels(int, const struct ieee80211req_chaninfo *,
144178354Ssam    int allchans, int verbose);
145178354Ssamstatic void regdomain_makechannels(struct ieee80211_regdomain_req *,
146178354Ssam    const struct ieee80211_devcaps_req *);
14777218Sphk
148170531Ssamstatic struct ieee80211req_chaninfo chaninfo;
149178354Ssamstatic struct ieee80211_regdomain regdomain;
150178354Ssamstatic int gotregdomain = 0;
151178354Ssamstatic struct ieee80211_roamparams_req roamparams;
152178354Ssamstatic int gotroam = 0;
153178354Ssamstatic struct ieee80211_txparams_req txparams;
154178354Ssamstatic int gottxparams = 0;
155173275Ssamstatic struct ieee80211_channel curchan;
156173275Ssamstatic int gotcurchan = 0;
157178354Ssamstatic struct ifmediareq *ifmr;
158173275Ssamstatic int htconf = 0;
159173275Ssamstatic	int gothtconf = 0;
160170531Ssam
161173275Ssamstatic void
162173275Ssamgethtconf(int s)
163173275Ssam{
164173275Ssam	if (gothtconf)
165173275Ssam		return;
166173275Ssam	if (get80211val(s, IEEE80211_IOC_HTCONF, &htconf) < 0)
167173275Ssam		warn("unable to get HT configuration information");
168173275Ssam	gothtconf = 1;
169173275Ssam}
170173275Ssam
171170531Ssam/*
172170531Ssam * Collect channel info from the kernel.  We use this (mostly)
173170531Ssam * to handle mapping between frequency and IEEE channel number.
174170531Ssam */
175170531Ssamstatic void
176170531Ssamgetchaninfo(int s)
177170531Ssam{
178170531Ssam	if (chaninfo.ic_nchans != 0)
179170531Ssam		return;
180173275Ssam	if (get80211(s, IEEE80211_IOC_CHANINFO, &chaninfo, sizeof(chaninfo)) < 0)
181170531Ssam		errx(1, "unable to get channel information");
182170531Ssam	ifmr = ifmedia_getstate(s);
183173275Ssam	gethtconf(s);
184170531Ssam}
185170531Ssam
186178354Ssamstatic struct regdata *
187178354Ssamgetregdata(void)
188178354Ssam{
189178354Ssam	static struct regdata *rdp = NULL;
190178354Ssam	if (rdp == NULL) {
191178354Ssam		rdp = lib80211_alloc_regdata();
192178354Ssam		if (rdp == NULL)
193181198Ssam			errx(-1, "missing or corrupted regdomain database");
194178354Ssam	}
195178354Ssam	return rdp;
196178354Ssam}
197178354Ssam
198170531Ssam/*
199170531Ssam * Given the channel at index i with attributes from,
200170531Ssam * check if there is a channel with attributes to in
201170531Ssam * the channel table.  With suitable attributes this
202170531Ssam * allows the caller to look for promotion; e.g. from
203170531Ssam * 11b > 11g.
204170531Ssam */
205138593Ssamstatic int
206170531Ssamcanpromote(int i, int from, int to)
207170531Ssam{
208170531Ssam	const struct ieee80211_channel *fc = &chaninfo.ic_chans[i];
209170531Ssam	int j;
210170531Ssam
211170531Ssam	if ((fc->ic_flags & from) != from)
212170531Ssam		return i;
213170531Ssam	/* NB: quick check exploiting ordering of chans w/ same frequency */
214170531Ssam	if (i+1 < chaninfo.ic_nchans &&
215170531Ssam	    chaninfo.ic_chans[i+1].ic_freq == fc->ic_freq &&
216170531Ssam	    (chaninfo.ic_chans[i+1].ic_flags & to) == to)
217170531Ssam		return i+1;
218170531Ssam	/* brute force search in case channel list is not ordered */
219170531Ssam	for (j = 0; j < chaninfo.ic_nchans; j++) {
220170531Ssam		const struct ieee80211_channel *tc = &chaninfo.ic_chans[j];
221170531Ssam		if (j != i &&
222170531Ssam		    tc->ic_freq == fc->ic_freq && (tc->ic_flags & to) == to)
223170531Ssam		return j;
224170531Ssam	}
225170531Ssam	return i;
226170531Ssam}
227170531Ssam
228170531Ssam/*
229170531Ssam * Handle channel promotion.  When a channel is specified with
230170531Ssam * only a frequency we want to promote it to the ``best'' channel
231170531Ssam * available.  The channel list has separate entries for 11b, 11g,
232170531Ssam * 11a, and 11n[ga] channels so specifying a frequency w/o any
233170531Ssam * attributes requires we upgrade, e.g. from 11b -> 11g.  This
234170531Ssam * gets complicated when the channel is specified on the same
235170531Ssam * command line with a media request that constrains the available
236170531Ssam * channe list (e.g. mode 11a); we want to honor that to avoid
237170531Ssam * confusing behaviour.
238170531Ssam */
239170531Ssamstatic int
240170531Ssampromote(int i)
241170531Ssam{
242170531Ssam	/*
243170531Ssam	 * Query the current mode of the interface in case it's
244170531Ssam	 * constrained (e.g. to 11a).  We must do this carefully
245170531Ssam	 * as there may be a pending ifmedia request in which case
246170531Ssam	 * asking the kernel will give us the wrong answer.  This
247170531Ssam	 * is an unfortunate side-effect of the way ifconfig is
248170531Ssam	 * structure for modularity (yech).
249170531Ssam	 *
250170531Ssam	 * NB: ifmr is actually setup in getchaninfo (above); we
251170531Ssam	 *     assume it's called coincident with to this call so
252170531Ssam	 *     we have a ``current setting''; otherwise we must pass
253170531Ssam	 *     the socket descriptor down to here so we can make
254170531Ssam	 *     the ifmedia_getstate call ourselves.
255170531Ssam	 */
256170531Ssam	int chanmode = ifmr != NULL ? IFM_MODE(ifmr->ifm_current) : IFM_AUTO;
257170531Ssam
258170531Ssam	/* when ambiguous promote to ``best'' */
259170531Ssam	/* NB: we abitrarily pick HT40+ over HT40- */
260170531Ssam	if (chanmode != IFM_IEEE80211_11B)
261170531Ssam		i = canpromote(i, IEEE80211_CHAN_B, IEEE80211_CHAN_G);
262173275Ssam	if (chanmode != IFM_IEEE80211_11G && (htconf & 1)) {
263170531Ssam		i = canpromote(i, IEEE80211_CHAN_G,
264170531Ssam			IEEE80211_CHAN_G | IEEE80211_CHAN_HT20);
265173275Ssam		if (htconf & 2) {
266173275Ssam			i = canpromote(i, IEEE80211_CHAN_G,
267173275Ssam				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D);
268173275Ssam			i = canpromote(i, IEEE80211_CHAN_G,
269173275Ssam				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U);
270173275Ssam		}
271170531Ssam	}
272173275Ssam	if (chanmode != IFM_IEEE80211_11A && (htconf & 1)) {
273170531Ssam		i = canpromote(i, IEEE80211_CHAN_A,
274170531Ssam			IEEE80211_CHAN_A | IEEE80211_CHAN_HT20);
275173275Ssam		if (htconf & 2) {
276173275Ssam			i = canpromote(i, IEEE80211_CHAN_A,
277173275Ssam				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D);
278173275Ssam			i = canpromote(i, IEEE80211_CHAN_A,
279173275Ssam				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U);
280173275Ssam		}
281170531Ssam	}
282170531Ssam	return i;
283170531Ssam}
284170531Ssam
285170531Ssamstatic void
286170531Ssammapfreq(struct ieee80211_channel *chan, int freq, int flags)
287170531Ssam{
288170531Ssam	int i;
289170531Ssam
290170531Ssam	for (i = 0; i < chaninfo.ic_nchans; i++) {
291170531Ssam		const struct ieee80211_channel *c = &chaninfo.ic_chans[i];
292170531Ssam
293170531Ssam		if (c->ic_freq == freq && (c->ic_flags & flags) == flags) {
294170531Ssam			if (flags == 0) {
295170531Ssam				/* when ambiguous promote to ``best'' */
296170531Ssam				c = &chaninfo.ic_chans[promote(i)];
297170531Ssam			}
298170531Ssam			*chan = *c;
299170531Ssam			return;
300170531Ssam		}
301170531Ssam	}
302170531Ssam	errx(1, "unknown/undefined frequency %u/0x%x", freq, flags);
303170531Ssam}
304170531Ssam
305170531Ssamstatic void
306170531Ssammapchan(struct ieee80211_channel *chan, int ieee, int flags)
307170531Ssam{
308170531Ssam	int i;
309170531Ssam
310170531Ssam	for (i = 0; i < chaninfo.ic_nchans; i++) {
311170531Ssam		const struct ieee80211_channel *c = &chaninfo.ic_chans[i];
312170531Ssam
313170531Ssam		if (c->ic_ieee == ieee && (c->ic_flags & flags) == flags) {
314170531Ssam			if (flags == 0) {
315170531Ssam				/* when ambiguous promote to ``best'' */
316170531Ssam				c = &chaninfo.ic_chans[promote(i)];
317170531Ssam			}
318170531Ssam			*chan = *c;
319170531Ssam			return;
320170531Ssam		}
321170531Ssam	}
322173275Ssam	errx(1, "unknown/undefined channel number %d flags 0x%x", ieee, flags);
323170531Ssam}
324170531Ssam
325173275Ssamstatic const struct ieee80211_channel *
326173275Ssamgetcurchan(int s)
327173275Ssam{
328173275Ssam	if (gotcurchan)
329173275Ssam		return &curchan;
330173275Ssam	if (get80211(s, IEEE80211_IOC_CURCHAN, &curchan, sizeof(curchan)) < 0) {
331173275Ssam		int val;
332173275Ssam		/* fall back to legacy ioctl */
333173275Ssam		if (get80211val(s, IEEE80211_IOC_CHANNEL, &val) < 0)
334173275Ssam			errx(-1, "cannot figure out current channel");
335173275Ssam		getchaninfo(s);
336173275Ssam		mapchan(&curchan, val, 0);
337173275Ssam	}
338173275Ssam	gotcurchan = 1;
339173275Ssam	return &curchan;
340173275Ssam}
341173275Ssam
342178354Ssamstatic enum ieee80211_phymode
343178354Ssamchan2mode(const struct ieee80211_channel *c)
344178354Ssam{
345178354Ssam	if (IEEE80211_IS_CHAN_HTA(c))
346178354Ssam		return IEEE80211_MODE_11NA;
347178354Ssam	if (IEEE80211_IS_CHAN_HTG(c))
348178354Ssam		return IEEE80211_MODE_11NG;
349178354Ssam	if (IEEE80211_IS_CHAN_108A(c))
350178354Ssam		return IEEE80211_MODE_TURBO_A;
351178354Ssam	if (IEEE80211_IS_CHAN_108G(c))
352178354Ssam		return IEEE80211_MODE_TURBO_G;
353178354Ssam	if (IEEE80211_IS_CHAN_ST(c))
354178354Ssam		return IEEE80211_MODE_STURBO_A;
355178354Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
356178354Ssam		return IEEE80211_MODE_FH;
357178354Ssam	if (IEEE80211_IS_CHAN_A(c))
358178354Ssam		return IEEE80211_MODE_11A;
359178354Ssam	if (IEEE80211_IS_CHAN_ANYG(c))
360178354Ssam		return IEEE80211_MODE_11G;
361178354Ssam	if (IEEE80211_IS_CHAN_B(c))
362178354Ssam		return IEEE80211_MODE_11B;
363178354Ssam	return IEEE80211_MODE_AUTO;
364178354Ssam}
365178354Ssam
366178354Ssamstatic void
367178354Ssamgetroam(int s)
368178354Ssam{
369178354Ssam	if (gotroam)
370178354Ssam		return;
371178354Ssam	if (get80211(s, IEEE80211_IOC_ROAM,
372178354Ssam	    &roamparams, sizeof(roamparams)) < 0)
373178354Ssam		errx(1, "unable to get roaming parameters");
374178354Ssam	gotroam = 1;
375178354Ssam}
376178354Ssam
377178354Ssamstatic void
378178354Ssamsetroam_cb(int s, void *arg)
379178354Ssam{
380178354Ssam	struct ieee80211_roamparams_req *roam = arg;
381178354Ssam	set80211(s, IEEE80211_IOC_ROAM, 0, sizeof(*roam), roam);
382178354Ssam}
383178354Ssam
384178354Ssamstatic void
385178354Ssamgettxparams(int s)
386178354Ssam{
387178354Ssam	if (gottxparams)
388178354Ssam		return;
389178354Ssam	if (get80211(s, IEEE80211_IOC_TXPARAMS,
390178354Ssam	    &txparams, sizeof(txparams)) < 0)
391178354Ssam		errx(1, "unable to get transmit parameters");
392178354Ssam	gottxparams = 1;
393178354Ssam}
394178354Ssam
395178354Ssamstatic void
396178354Ssamsettxparams_cb(int s, void *arg)
397178354Ssam{
398178354Ssam	struct ieee80211_txparams_req *txp = arg;
399178354Ssam	set80211(s, IEEE80211_IOC_TXPARAMS, 0, sizeof(*txp), txp);
400178354Ssam}
401178354Ssam
402178354Ssamstatic void
403178354Ssamgetregdomain(int s)
404178354Ssam{
405178354Ssam	if (gotregdomain)
406178354Ssam		return;
407178354Ssam	if (get80211(s, IEEE80211_IOC_REGDOMAIN,
408178354Ssam	    &regdomain, sizeof(regdomain)) < 0)
409178354Ssam		errx(1, "unable to get regulatory domain info");
410178354Ssam	gotregdomain = 1;
411178354Ssam}
412178354Ssam
413178354Ssamstatic void
414178354Ssamgetdevcaps(int s, struct ieee80211_devcaps_req *dc)
415178354Ssam{
416178354Ssam	if (get80211(s, IEEE80211_IOC_DEVCAPS, dc, sizeof(*dc)) < 0)
417178354Ssam		errx(1, "unable to get device capabilities");
418178354Ssam}
419178354Ssam
420178354Ssamstatic void
421178354Ssamsetregdomain_cb(int s, void *arg)
422178354Ssam{
423178354Ssam	struct ieee80211_regdomain_req req;
424178354Ssam	struct ieee80211_regdomain *rd = arg;
425178354Ssam	struct ieee80211_devcaps_req dc;
426178354Ssam	struct regdata *rdp = getregdata();
427178354Ssam
428178354Ssam	if (rd->country != 0) {
429178354Ssam		const struct country *cc;
430178354Ssam		/*
431178354Ssam		 * Check current country seting to make sure it's
432178354Ssam		 * compatible with the new regdomain.  If not, then
433178354Ssam		 * override it with any default country for this
434178354Ssam		 * SKU.  If we cannot arrange a match, then abort.
435178354Ssam		 */
436178354Ssam		cc = lib80211_country_findbycc(rdp, rd->country);
437178354Ssam		if (cc == NULL)
438178354Ssam			errx(1, "unknown ISO country code %d", rd->country);
439178354Ssam		if (cc->rd->sku != rd->regdomain) {
440178354Ssam			const struct regdomain *rp;
441178354Ssam			/*
442178354Ssam			 * Check if country is incompatible with regdomain.
443178354Ssam			 * To enable multiple regdomains for a country code
444178354Ssam			 * we permit a mismatch between the regdomain and
445178354Ssam			 * the country's associated regdomain when the
446178354Ssam			 * regdomain is setup w/o a default country.  For
447178354Ssam			 * example, US is bound to the FCC regdomain but
448178354Ssam			 * we allow US to be combined with FCC3 because FCC3
449178354Ssam			 * has not default country.  This allows bogus
450178354Ssam			 * combinations like FCC3+DK which are resolved when
451178354Ssam			 * constructing the channel list by deferring to the
452178354Ssam			 * regdomain to construct the channel list.
453178354Ssam			 */
454178354Ssam			rp = lib80211_regdomain_findbysku(rdp, rd->regdomain);
455178354Ssam			if (rp == NULL)
456178354Ssam				errx(1, "country %s (%s) is not usable with "
457178354Ssam				    "regdomain %d", cc->isoname, cc->name,
458178354Ssam				    rd->regdomain);
459178354Ssam			else if (rp->cc != 0 && rp->cc != cc)
460178354Ssam				errx(1, "country %s (%s) is not usable with "
461178354Ssam				   "regdomain %s", cc->isoname, cc->name,
462178354Ssam				   rp->name);
463178354Ssam		}
464178354Ssam	}
465178354Ssam	req.rd = *rd;
466178354Ssam	/*
467178354Ssam	 * Fetch the device capabilities and calculate the
468178354Ssam	 * full set of netbands for which we request a new
469178354Ssam	 * channel list be constructed.  Once that's done we
470178354Ssam	 * push the regdomain info + channel list to the kernel.
471178354Ssam	 */
472178354Ssam	getdevcaps(s, &dc);
473178354Ssam#if 0
474178354Ssam	if (verbose) {
475178354Ssam		printf("drivercaps: 0x%x\n", dc.dc_drivercaps);
476178354Ssam		printf("cryptocaps: 0x%x\n", dc.dc_cryptocaps);
477178354Ssam		printf("htcaps    : 0x%x\n", dc.dc_htcaps);
478178354Ssam		memcpy(&chaninfo, &dc.dc_chaninfo, sizeof(chaninfo));
479178354Ssam		print_channels(s, &dc.dc_chaninfo, 1/*allchans*/, 1/*verbose*/);
480178354Ssam	}
481178354Ssam#endif
482178354Ssam	regdomain_makechannels(&req, &dc);
483178354Ssam	if (verbose) {
484178354Ssam		LINE_INIT(':');
485178354Ssam		print_regdomain(rd, 1/*verbose*/);
486178354Ssam		LINE_BREAK();
487178354Ssam		memcpy(&chaninfo, &req.chaninfo, sizeof(chaninfo));
488178354Ssam		print_channels(s, &req.chaninfo, 1/*allchans*/, 1/*verbose*/);
489178354Ssam	}
490178354Ssam	if (req.chaninfo.ic_nchans == 0)
491178354Ssam		errx(1, "no channels calculated");
492178354Ssam	set80211(s, IEEE80211_IOC_REGDOMAIN, 0, sizeof(req), &req);
493178354Ssam}
494178354Ssam
495170531Ssamstatic int
496170531Ssamieee80211_mhz2ieee(int freq, int flags)
497170531Ssam{
498170531Ssam	struct ieee80211_channel chan;
499170531Ssam	mapfreq(&chan, freq, flags);
500170531Ssam	return chan.ic_ieee;
501170531Ssam}
502170531Ssam
503170531Ssamstatic int
504138593Ssamisanyarg(const char *arg)
505138593Ssam{
506173275Ssam	return (strncmp(arg, "-", 1) == 0 ||
507173275Ssam	    strncasecmp(arg, "any", 3) == 0 || strncasecmp(arg, "off", 3) == 0);
508138593Ssam}
509138593Ssam
510138593Ssamstatic void
51177218Sphkset80211ssid(const char *val, int d, int s, const struct afswtch *rafp)
51277218Sphk{
51377218Sphk	int		ssid;
51477218Sphk	int		len;
515151883Sbrooks	u_int8_t	data[IEEE80211_NWID_LEN];
51677218Sphk
51777218Sphk	ssid = 0;
518121827Sbrooks	len = strlen(val);
519178354Ssam	if (len > 2 && isdigit((int)val[0]) && val[1] == ':') {
52088748Sambrisko		ssid = atoi(val)-1;
52188748Sambrisko		val += 2;
52288748Sambrisko	}
52377218Sphk
52477218Sphk	bzero(data, sizeof(data));
52577218Sphk	len = sizeof(data);
526151883Sbrooks	if (get_string(val, NULL, data, &len) == NULL)
527151883Sbrooks		exit(1);
52877218Sphk
52977218Sphk	set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
53077218Sphk}
53177218Sphk
532138593Ssamstatic void
53377218Sphkset80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
53477218Sphk{
53577218Sphk	int			len;
53677218Sphk	u_int8_t		data[33];
53777218Sphk
53877218Sphk	bzero(data, sizeof(data));
53977218Sphk	len = sizeof(data);
54077218Sphk	get_string(val, NULL, data, &len);
54177218Sphk
54277218Sphk	set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
54377218Sphk}
54477218Sphk
545138593Ssam/*
546170531Ssam * Parse a channel specification for attributes/flags.
547170531Ssam * The syntax is:
548170531Ssam *	freq/xx		channel width (5,10,20,40,40+,40-)
549170531Ssam *	freq:mode	channel mode (a,b,g,h,n,t,s,d)
550170531Ssam *
551170531Ssam * These can be combined in either order; e.g. 2437:ng/40.
552170531Ssam * Modes are case insensitive.
553170531Ssam *
554170531Ssam * The result is not validated here; it's assumed to be
555170531Ssam * checked against the channel table fetched from the kernel.
556170531Ssam */
557170531Ssamstatic int
558173275Ssamgetchannelflags(const char *val, int freq)
559138593Ssam{
560170531Ssam#define	_CHAN_HT	0x80000000
561170531Ssam	const char *cp;
562170531Ssam	int flags;
563138593Ssam
564170531Ssam	flags = 0;
565166015Ssam
566170531Ssam	cp = strchr(val, ':');
567170531Ssam	if (cp != NULL) {
568170531Ssam		for (cp++; isalpha((int) *cp); cp++) {
569170531Ssam			/* accept mixed case */
570170531Ssam			int c = *cp;
571170531Ssam			if (isupper(c))
572170531Ssam				c = tolower(c);
573170531Ssam			switch (c) {
574170531Ssam			case 'a':		/* 802.11a */
575170531Ssam				flags |= IEEE80211_CHAN_A;
576170531Ssam				break;
577170531Ssam			case 'b':		/* 802.11b */
578170531Ssam				flags |= IEEE80211_CHAN_B;
579170531Ssam				break;
580170531Ssam			case 'g':		/* 802.11g */
581170531Ssam				flags |= IEEE80211_CHAN_G;
582170531Ssam				break;
583170531Ssam			case 'h':		/* ht = 802.11n */
584170531Ssam			case 'n':		/* 802.11n */
585170531Ssam				flags |= _CHAN_HT;	/* NB: private */
586170531Ssam				break;
587170531Ssam			case 'd':		/* dt = Atheros Dynamic Turbo */
588170531Ssam				flags |= IEEE80211_CHAN_TURBO;
589170531Ssam				break;
590170531Ssam			case 't':		/* ht, dt, st, t */
591170531Ssam				/* dt and unadorned t specify Dynamic Turbo */
592170531Ssam				if ((flags & (IEEE80211_CHAN_STURBO|_CHAN_HT)) == 0)
593170531Ssam					flags |= IEEE80211_CHAN_TURBO;
594170531Ssam				break;
595170531Ssam			case 's':		/* st = Atheros Static Turbo */
596170531Ssam				flags |= IEEE80211_CHAN_STURBO;
597170531Ssam				break;
598170531Ssam			default:
599173275Ssam				errx(-1, "%s: Invalid channel attribute %c\n",
600170531Ssam				    val, *cp);
601170531Ssam			}
602170531Ssam		}
603170531Ssam	}
604170531Ssam	cp = strchr(val, '/');
605170531Ssam	if (cp != NULL) {
606170531Ssam		char *ep;
607170531Ssam		u_long cw = strtoul(cp+1, &ep, 10);
608166015Ssam
609170531Ssam		switch (cw) {
610170531Ssam		case 5:
611170531Ssam			flags |= IEEE80211_CHAN_QUARTER;
612170531Ssam			break;
613170531Ssam		case 10:
614170531Ssam			flags |= IEEE80211_CHAN_HALF;
615170531Ssam			break;
616170531Ssam		case 20:
617170531Ssam			/* NB: this may be removed below */
618170531Ssam			flags |= IEEE80211_CHAN_HT20;
619170531Ssam			break;
620170531Ssam		case 40:
621170531Ssam			if (ep != NULL && *ep == '+')
622170531Ssam				flags |= IEEE80211_CHAN_HT40U;
623170531Ssam			else if (ep != NULL && *ep == '-')
624170531Ssam				flags |= IEEE80211_CHAN_HT40D;
625170531Ssam			break;
626170531Ssam		default:
627173275Ssam			errx(-1, "%s: Invalid channel width\n", val);
628170531Ssam		}
629165570Ssam	}
630170531Ssam	/*
631170531Ssam	 * Cleanup specifications.
632170531Ssam	 */
633170531Ssam	if ((flags & _CHAN_HT) == 0) {
634170531Ssam		/*
635170531Ssam		 * If user specified freq/20 or freq/40 quietly remove
636170531Ssam		 * HT cw attributes depending on channel use.  To give
637170531Ssam		 * an explicit 20/40 width for an HT channel you must
638170531Ssam		 * indicate it is an HT channel since all HT channels
639170531Ssam		 * are also usable for legacy operation; e.g. freq:n/40.
640170531Ssam		 */
641170531Ssam		flags &= ~IEEE80211_CHAN_HT;
642170531Ssam	} else {
643170531Ssam		/*
644170531Ssam		 * Remove private indicator that this is an HT channel
645170531Ssam		 * and if no explicit channel width has been given
646170531Ssam		 * provide the default settings.
647170531Ssam		 */
648170531Ssam		flags &= ~_CHAN_HT;
649173275Ssam		if ((flags & IEEE80211_CHAN_HT) == 0) {
650173275Ssam			struct ieee80211_channel chan;
651173275Ssam			/*
652173275Ssam			 * Consult the channel list to see if we can use
653173275Ssam			 * HT40+ or HT40- (if both the map routines choose).
654173275Ssam			 */
655173275Ssam			if (freq > 255)
656173275Ssam				mapfreq(&chan, freq, 0);
657173275Ssam			else
658173275Ssam				mapchan(&chan, freq, 0);
659173275Ssam			flags |= (chan.ic_flags & IEEE80211_CHAN_HT);
660173275Ssam		}
661170531Ssam	}
662170531Ssam	return flags;
663170531Ssam#undef _CHAN_HT
664138593Ssam}
665138593Ssam
666138593Ssamstatic void
66777218Sphkset80211channel(const char *val, int d, int s, const struct afswtch *rafp)
66877218Sphk{
669170531Ssam	struct ieee80211_channel chan;
670170531Ssam
671170531Ssam	memset(&chan, 0, sizeof(chan));
672138593Ssam	if (!isanyarg(val)) {
673173275Ssam		int v, flags;
674179958Sthompsa		char *ep;
675170531Ssam
676170531Ssam		getchaninfo(s);
677179958Sthompsa		v = strtol(val, &ep, 10);
678181722Sthompsa		if (val[0] == '\0' || val == ep || errno == ERANGE ||
679181722Sthompsa		    /* channel may be suffixed with nothing, :flag, or /width */
680181722Sthompsa		    (ep[0] != '\0' && ep[0] != ':' && ep[0] != '/'))
681181722Sthompsa			errx(1, "invalid channel specification");
682173275Ssam		flags = getchannelflags(val, v);
683170531Ssam		if (v > 255) {		/* treat as frequency */
684170531Ssam			mapfreq(&chan, v, flags);
685170531Ssam		} else {
686170531Ssam			mapchan(&chan, v, flags);
687170531Ssam		}
688170531Ssam	} else {
689170531Ssam		chan.ic_freq = IEEE80211_CHAN_ANY;
690170531Ssam	}
691170531Ssam	set80211(s, IEEE80211_IOC_CURCHAN, 0, sizeof(chan), &chan);
69277218Sphk}
69377218Sphk
694138593Ssamstatic void
695178354Ssamset80211chanswitch(const char *val, int d, int s, const struct afswtch *rafp)
696178354Ssam{
697178354Ssam	struct ieee80211_chanswitch_req csr;
698178354Ssam	int v, flags;
699178354Ssam
700178354Ssam	memset(&csr, 0, sizeof(csr));
701178354Ssam	getchaninfo(s);
702178354Ssam	v = atoi(val);
703178354Ssam	flags = getchannelflags(val, v);
704178354Ssam	if (v > 255) {		/* treat as frequency */
705178354Ssam		mapfreq(&csr.csa_chan, v, flags);
706178354Ssam	} else {
707178354Ssam		mapchan(&csr.csa_chan, v, flags);
708178354Ssam	}
709178354Ssam	csr.csa_mode = 1;
710178354Ssam	csr.csa_count = 5;
711178354Ssam	set80211(s, IEEE80211_IOC_CHANSWITCH, 0, sizeof(csr), &csr);
712178354Ssam}
713178354Ssam
714178354Ssamstatic void
71577218Sphkset80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
71677218Sphk{
71777218Sphk	int	mode;
71877218Sphk
71991454Sbrooks	if (strcasecmp(val, "none") == 0) {
72077218Sphk		mode = IEEE80211_AUTH_NONE;
72191454Sbrooks	} else if (strcasecmp(val, "open") == 0) {
72277218Sphk		mode = IEEE80211_AUTH_OPEN;
72391454Sbrooks	} else if (strcasecmp(val, "shared") == 0) {
72477218Sphk		mode = IEEE80211_AUTH_SHARED;
725138593Ssam	} else if (strcasecmp(val, "8021x") == 0) {
726138593Ssam		mode = IEEE80211_AUTH_8021X;
727138593Ssam	} else if (strcasecmp(val, "wpa") == 0) {
728138593Ssam		mode = IEEE80211_AUTH_WPA;
72977218Sphk	} else {
730150708Sru		errx(1, "unknown authmode");
73177218Sphk	}
73277218Sphk
73377218Sphk	set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
73477218Sphk}
73577218Sphk
736138593Ssamstatic void
73777218Sphkset80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
73877218Sphk{
73977218Sphk	int	mode;
74077218Sphk
74191454Sbrooks	if (strcasecmp(val, "off") == 0) {
74277218Sphk		mode = IEEE80211_POWERSAVE_OFF;
74391454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
74477218Sphk		mode = IEEE80211_POWERSAVE_ON;
74591454Sbrooks	} else if (strcasecmp(val, "cam") == 0) {
74677218Sphk		mode = IEEE80211_POWERSAVE_CAM;
74791454Sbrooks	} else if (strcasecmp(val, "psp") == 0) {
74877218Sphk		mode = IEEE80211_POWERSAVE_PSP;
74991454Sbrooks	} else if (strcasecmp(val, "psp-cam") == 0) {
75077218Sphk		mode = IEEE80211_POWERSAVE_PSP_CAM;
75177218Sphk	} else {
752150708Sru		errx(1, "unknown powersavemode");
75377218Sphk	}
75477218Sphk
75577218Sphk	set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
75677218Sphk}
75777218Sphk
758138593Ssamstatic void
75977218Sphkset80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
76077218Sphk{
76177218Sphk	if (d == 0)
76277218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
76377218Sphk		    0, NULL);
76477218Sphk	else
76577218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
76677218Sphk		    0, NULL);
76777218Sphk}
76877218Sphk
769138593Ssamstatic void
77077218Sphkset80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
77177218Sphk{
77277218Sphk	set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
77377218Sphk}
77477218Sphk
775138593Ssamstatic void
77677218Sphkset80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
77777218Sphk{
77877218Sphk	int	mode;
77977218Sphk
78091454Sbrooks	if (strcasecmp(val, "off") == 0) {
78177218Sphk		mode = IEEE80211_WEP_OFF;
78291454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
78377218Sphk		mode = IEEE80211_WEP_ON;
78491454Sbrooks	} else if (strcasecmp(val, "mixed") == 0) {
78577218Sphk		mode = IEEE80211_WEP_MIXED;
78677218Sphk	} else {
787150708Sru		errx(1, "unknown wep mode");
78877218Sphk	}
78977218Sphk
79077218Sphk	set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
79177218Sphk}
79277218Sphk
793138593Ssamstatic void
79477218Sphkset80211wep(const char *val, int d, int s, const struct afswtch *rafp)
79577218Sphk{
79677218Sphk	set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
79777218Sphk}
79877218Sphk
799139493Ssamstatic int
800139493Ssamisundefarg(const char *arg)
801139493Ssam{
802139493Ssam	return (strcmp(arg, "-") == 0 || strncasecmp(arg, "undef", 5) == 0);
803139493Ssam}
804139493Ssam
805138593Ssamstatic void
80677218Sphkset80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
80777218Sphk{
808139493Ssam	if (isundefarg(val))
809139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, IEEE80211_KEYIX_NONE, 0, NULL);
810139493Ssam	else
811139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
81277218Sphk}
81377218Sphk
814138593Ssamstatic void
81577218Sphkset80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
81677218Sphk{
81777218Sphk	int		key = 0;
81877218Sphk	int		len;
819120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
82077218Sphk
821178354Ssam	if (isdigit((int)val[0]) && val[1] == ':') {
82277218Sphk		key = atoi(val)-1;
82377218Sphk		val += 2;
82477218Sphk	}
82577218Sphk
82677218Sphk	bzero(data, sizeof(data));
82777218Sphk	len = sizeof(data);
82877218Sphk	get_string(val, NULL, data, &len);
82977218Sphk
83077218Sphk	set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
83177218Sphk}
83277218Sphk
83377218Sphk/*
834148686Sstefanf * This function is purely a NetBSD compatability interface.  The NetBSD
835148686Sstefanf * interface is too inflexible, but it's there so we'll support it since
83677218Sphk * it's not all that hard.
83777218Sphk */
838138593Ssamstatic void
83977218Sphkset80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
84077218Sphk{
84177218Sphk	int		txkey;
84277218Sphk	int		i, len;
843120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
84477218Sphk
84577218Sphk	set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
84677218Sphk
847178354Ssam	if (isdigit((int)val[0]) && val[1] == ':') {
84877218Sphk		txkey = val[0]-'0'-1;
84977218Sphk		val += 2;
85077218Sphk
85191454Sbrooks		for (i = 0; i < 4; i++) {
85277218Sphk			bzero(data, sizeof(data));
85377218Sphk			len = sizeof(data);
85477218Sphk			val = get_string(val, ",", data, &len);
855151827Sbrooks			if (val == NULL)
856151827Sbrooks				exit(1);
85777218Sphk
85877218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
85977218Sphk		}
86077218Sphk	} else {
86177218Sphk		bzero(data, sizeof(data));
86277218Sphk		len = sizeof(data);
86377218Sphk		get_string(val, NULL, data, &len);
86477218Sphk		txkey = 0;
86577218Sphk
86677218Sphk		set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
86777218Sphk
86877218Sphk		bzero(data, sizeof(data));
86991454Sbrooks		for (i = 1; i < 4; i++)
87077218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
87177218Sphk	}
87277218Sphk
87377218Sphk	set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
87477218Sphk}
87577218Sphk
876138593Ssamstatic void
877127649Ssamset80211rtsthreshold(const char *val, int d, int s, const struct afswtch *rafp)
878127649Ssam{
879148416Ssam	set80211(s, IEEE80211_IOC_RTSTHRESHOLD,
880148416Ssam		isundefarg(val) ? IEEE80211_RTS_MAX : atoi(val), 0, NULL);
881127649Ssam}
882127649Ssam
883138593Ssamstatic void
884127649Ssamset80211protmode(const char *val, int d, int s, const struct afswtch *rafp)
885127649Ssam{
886127649Ssam	int	mode;
887127649Ssam
888127649Ssam	if (strcasecmp(val, "off") == 0) {
889127649Ssam		mode = IEEE80211_PROTMODE_OFF;
890127649Ssam	} else if (strcasecmp(val, "cts") == 0) {
891127649Ssam		mode = IEEE80211_PROTMODE_CTS;
892173275Ssam	} else if (strncasecmp(val, "rtscts", 3) == 0) {
893127649Ssam		mode = IEEE80211_PROTMODE_RTSCTS;
894127649Ssam	} else {
895150708Sru		errx(1, "unknown protection mode");
896127649Ssam	}
897127649Ssam
898127649Ssam	set80211(s, IEEE80211_IOC_PROTMODE, mode, 0, NULL);
899127649Ssam}
900127649Ssam
901138593Ssamstatic void
902173275Ssamset80211htprotmode(const char *val, int d, int s, const struct afswtch *rafp)
903173275Ssam{
904173275Ssam	int	mode;
905173275Ssam
906173275Ssam	if (strcasecmp(val, "off") == 0) {
907173275Ssam		mode = IEEE80211_PROTMODE_OFF;
908173275Ssam	} else if (strncasecmp(val, "rts", 3) == 0) {
909173275Ssam		mode = IEEE80211_PROTMODE_RTSCTS;
910173275Ssam	} else {
911173275Ssam		errx(1, "unknown protection mode");
912173275Ssam	}
913173275Ssam
914173275Ssam	set80211(s, IEEE80211_IOC_HTPROTMODE, mode, 0, NULL);
915173275Ssam}
916173275Ssam
917173275Ssamstatic void
918127649Ssamset80211txpower(const char *val, int d, int s, const struct afswtch *rafp)
919127649Ssam{
920173275Ssam	double v = atof(val);
921173275Ssam	int txpow;
922173275Ssam
923173275Ssam	txpow = (int) (2*v);
924173275Ssam	if (txpow != 2*v)
925173275Ssam		errx(-1, "invalid tx power (must be .5 dBm units)");
926173275Ssam	set80211(s, IEEE80211_IOC_TXPOWER, txpow, 0, NULL);
927127649Ssam}
928127649Ssam
929138593Ssam#define	IEEE80211_ROAMING_DEVICE	0
930138593Ssam#define	IEEE80211_ROAMING_AUTO		1
931138593Ssam#define	IEEE80211_ROAMING_MANUAL	2
932138593Ssam
933138593Ssamstatic void
934138593Ssamset80211roaming(const char *val, int d, int s, const struct afswtch *rafp)
93577218Sphk{
936138593Ssam	int mode;
93777218Sphk
938138593Ssam	if (strcasecmp(val, "device") == 0) {
939138593Ssam		mode = IEEE80211_ROAMING_DEVICE;
940138593Ssam	} else if (strcasecmp(val, "auto") == 0) {
941138593Ssam		mode = IEEE80211_ROAMING_AUTO;
942138593Ssam	} else if (strcasecmp(val, "manual") == 0) {
943138593Ssam		mode = IEEE80211_ROAMING_MANUAL;
944138593Ssam	} else {
945150708Sru		errx(1, "unknown roaming mode");
946138593Ssam	}
947138593Ssam	set80211(s, IEEE80211_IOC_ROAMING, mode, 0, NULL);
948138593Ssam}
949138593Ssam
950138593Ssamstatic void
951138593Ssamset80211wme(const char *val, int d, int s, const struct afswtch *rafp)
952138593Ssam{
953138593Ssam	set80211(s, IEEE80211_IOC_WME, d, 0, NULL);
954138593Ssam}
955138593Ssam
956138593Ssamstatic void
957138593Ssamset80211hidessid(const char *val, int d, int s, const struct afswtch *rafp)
958138593Ssam{
959138593Ssam	set80211(s, IEEE80211_IOC_HIDESSID, d, 0, NULL);
960138593Ssam}
961138593Ssam
962138593Ssamstatic void
963138593Ssamset80211apbridge(const char *val, int d, int s, const struct afswtch *rafp)
964138593Ssam{
965138593Ssam	set80211(s, IEEE80211_IOC_APBRIDGE, d, 0, NULL);
966138593Ssam}
967138593Ssam
968138593Ssamstatic void
969170531Ssamset80211fastframes(const char *val, int d, int s, const struct afswtch *rafp)
970170531Ssam{
971170531Ssam	set80211(s, IEEE80211_IOC_FF, d, 0, NULL);
972170531Ssam}
973170531Ssam
974170531Ssamstatic void
975170531Ssamset80211dturbo(const char *val, int d, int s, const struct afswtch *rafp)
976170531Ssam{
977170531Ssam	set80211(s, IEEE80211_IOC_TURBOP, d, 0, NULL);
978170531Ssam}
979170531Ssam
980170531Ssamstatic void
981138593Ssamset80211chanlist(const char *val, int d, int s, const struct afswtch *rafp)
982138593Ssam{
983138593Ssam	struct ieee80211req_chanlist chanlist;
984138593Ssam#define	MAXCHAN	(sizeof(chanlist.ic_channels)*NBBY)
985138593Ssam	char *temp, *cp, *tp;
986138593Ssam
987138593Ssam	temp = malloc(strlen(val) + 1);
988138593Ssam	if (temp == NULL)
989138593Ssam		errx(1, "malloc failed");
990138593Ssam	strcpy(temp, val);
991138593Ssam	memset(&chanlist, 0, sizeof(chanlist));
992138593Ssam	cp = temp;
993138593Ssam	for (;;) {
994173275Ssam		int first, last, f, c;
995138593Ssam
996138593Ssam		tp = strchr(cp, ',');
997138593Ssam		if (tp != NULL)
998138593Ssam			*tp++ = '\0';
999138593Ssam		switch (sscanf(cp, "%u-%u", &first, &last)) {
1000138593Ssam		case 1:
1001138593Ssam			if (first > MAXCHAN)
1002146873Sjhb				errx(-1, "channel %u out of range, max %zu",
1003138593Ssam					first, MAXCHAN);
1004138593Ssam			setbit(chanlist.ic_channels, first);
1005138593Ssam			break;
1006138593Ssam		case 2:
1007138593Ssam			if (first > MAXCHAN)
1008146873Sjhb				errx(-1, "channel %u out of range, max %zu",
1009138593Ssam					first, MAXCHAN);
1010138593Ssam			if (last > MAXCHAN)
1011146873Sjhb				errx(-1, "channel %u out of range, max %zu",
1012138593Ssam					last, MAXCHAN);
1013138593Ssam			if (first > last)
1014138593Ssam				errx(-1, "void channel range, %u > %u",
1015138593Ssam					first, last);
1016138593Ssam			for (f = first; f <= last; f++)
1017138593Ssam				setbit(chanlist.ic_channels, f);
1018138593Ssam			break;
1019138593Ssam		}
1020138593Ssam		if (tp == NULL)
1021138593Ssam			break;
1022173275Ssam		c = *tp;
1023173275Ssam		while (isspace(c))
1024138593Ssam			tp++;
1025173275Ssam		if (!isdigit(c))
1026138593Ssam			break;
1027138593Ssam		cp = tp;
1028138593Ssam	}
1029170531Ssam	set80211(s, IEEE80211_IOC_CHANLIST, 0, sizeof(chanlist), &chanlist);
1030138593Ssam#undef MAXCHAN
1031138593Ssam}
1032138593Ssam
1033138593Ssamstatic void
1034138593Ssamset80211bssid(const char *val, int d, int s, const struct afswtch *rafp)
1035138593Ssam{
1036138593Ssam
1037138593Ssam	if (!isanyarg(val)) {
1038138593Ssam		char *temp;
1039138593Ssam		struct sockaddr_dl sdl;
1040138593Ssam
1041155702Ssam		temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1042138593Ssam		if (temp == NULL)
1043138593Ssam			errx(1, "malloc failed");
1044138593Ssam		temp[0] = ':';
1045138593Ssam		strcpy(temp + 1, val);
1046138593Ssam		sdl.sdl_len = sizeof(sdl);
1047138593Ssam		link_addr(temp, &sdl);
1048138593Ssam		free(temp);
1049138593Ssam		if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1050138593Ssam			errx(1, "malformed link-level address");
1051138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
1052138593Ssam			IEEE80211_ADDR_LEN, LLADDR(&sdl));
1053138593Ssam	} else {
1054138593Ssam		uint8_t zerobssid[IEEE80211_ADDR_LEN];
1055138593Ssam		memset(zerobssid, 0, sizeof(zerobssid));
1056138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
1057138593Ssam			IEEE80211_ADDR_LEN, zerobssid);
1058138593Ssam	}
1059138593Ssam}
1060138593Ssam
1061138593Ssamstatic int
1062138593Ssamgetac(const char *ac)
1063138593Ssam{
1064138593Ssam	if (strcasecmp(ac, "ac_be") == 0 || strcasecmp(ac, "be") == 0)
1065138593Ssam		return WME_AC_BE;
1066138593Ssam	if (strcasecmp(ac, "ac_bk") == 0 || strcasecmp(ac, "bk") == 0)
1067138593Ssam		return WME_AC_BK;
1068138593Ssam	if (strcasecmp(ac, "ac_vi") == 0 || strcasecmp(ac, "vi") == 0)
1069138593Ssam		return WME_AC_VI;
1070138593Ssam	if (strcasecmp(ac, "ac_vo") == 0 || strcasecmp(ac, "vo") == 0)
1071138593Ssam		return WME_AC_VO;
1072138593Ssam	errx(1, "unknown wme access class %s", ac);
1073138593Ssam}
1074138593Ssam
1075138593Ssamstatic
1076138593SsamDECL_CMD_FUNC2(set80211cwmin, ac, val)
1077138593Ssam{
1078138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val), getac(ac), NULL);
1079138593Ssam}
1080138593Ssam
1081138593Ssamstatic
1082138593SsamDECL_CMD_FUNC2(set80211cwmax, ac, val)
1083138593Ssam{
1084138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val), getac(ac), NULL);
1085138593Ssam}
1086138593Ssam
1087138593Ssamstatic
1088138593SsamDECL_CMD_FUNC2(set80211aifs, ac, val)
1089138593Ssam{
1090138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val), getac(ac), NULL);
1091138593Ssam}
1092138593Ssam
1093138593Ssamstatic
1094138593SsamDECL_CMD_FUNC2(set80211txoplimit, ac, val)
1095138593Ssam{
1096138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), getac(ac), NULL);
1097138593Ssam}
1098138593Ssam
1099138593Ssamstatic
1100148621SsamDECL_CMD_FUNC(set80211acm, ac, d)
1101138593Ssam{
1102148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 1, getac(ac), NULL);
1103138593Ssam}
1104148621Ssamstatic
1105148621SsamDECL_CMD_FUNC(set80211noacm, ac, d)
1106148621Ssam{
1107148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 0, getac(ac), NULL);
1108148621Ssam}
1109138593Ssam
1110138593Ssamstatic
1111148621SsamDECL_CMD_FUNC(set80211ackpolicy, ac, d)
1112138593Ssam{
1113148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 1, getac(ac), NULL);
1114138593Ssam}
1115148621Ssamstatic
1116148621SsamDECL_CMD_FUNC(set80211noackpolicy, ac, d)
1117148621Ssam{
1118148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 0, getac(ac), NULL);
1119148621Ssam}
1120138593Ssam
1121138593Ssamstatic
1122138593SsamDECL_CMD_FUNC2(set80211bsscwmin, ac, val)
1123138593Ssam{
1124138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val),
1125138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1126138593Ssam}
1127138593Ssam
1128138593Ssamstatic
1129138593SsamDECL_CMD_FUNC2(set80211bsscwmax, ac, val)
1130138593Ssam{
1131138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val),
1132138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1133138593Ssam}
1134138593Ssam
1135138593Ssamstatic
1136138593SsamDECL_CMD_FUNC2(set80211bssaifs, ac, val)
1137138593Ssam{
1138138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val),
1139138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1140138593Ssam}
1141138593Ssam
1142138593Ssamstatic
1143138593SsamDECL_CMD_FUNC2(set80211bsstxoplimit, ac, val)
1144138593Ssam{
1145138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val),
1146138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1147138593Ssam}
1148138593Ssam
1149138593Ssamstatic
1150138593SsamDECL_CMD_FUNC(set80211dtimperiod, val, d)
1151138593Ssam{
1152138593Ssam	set80211(s, IEEE80211_IOC_DTIM_PERIOD, atoi(val), 0, NULL);
1153138593Ssam}
1154138593Ssam
1155138593Ssamstatic
1156138593SsamDECL_CMD_FUNC(set80211bintval, val, d)
1157138593Ssam{
1158138593Ssam	set80211(s, IEEE80211_IOC_BEACON_INTERVAL, atoi(val), 0, NULL);
1159138593Ssam}
1160138593Ssam
1161138593Ssamstatic void
1162138593Ssamset80211macmac(int s, int op, const char *val)
1163138593Ssam{
1164138593Ssam	char *temp;
1165138593Ssam	struct sockaddr_dl sdl;
1166138593Ssam
1167155702Ssam	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1168138593Ssam	if (temp == NULL)
1169138593Ssam		errx(1, "malloc failed");
1170138593Ssam	temp[0] = ':';
1171138593Ssam	strcpy(temp + 1, val);
1172138593Ssam	sdl.sdl_len = sizeof(sdl);
1173138593Ssam	link_addr(temp, &sdl);
1174138593Ssam	free(temp);
1175138593Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1176138593Ssam		errx(1, "malformed link-level address");
1177138593Ssam	set80211(s, op, 0, IEEE80211_ADDR_LEN, LLADDR(&sdl));
1178138593Ssam}
1179138593Ssam
1180138593Ssamstatic
1181138593SsamDECL_CMD_FUNC(set80211addmac, val, d)
1182138593Ssam{
1183138593Ssam	set80211macmac(s, IEEE80211_IOC_ADDMAC, val);
1184138593Ssam}
1185138593Ssam
1186138593Ssamstatic
1187138593SsamDECL_CMD_FUNC(set80211delmac, val, d)
1188138593Ssam{
1189138593Ssam	set80211macmac(s, IEEE80211_IOC_DELMAC, val);
1190138593Ssam}
1191138593Ssam
1192138593Ssamstatic
1193149029SsamDECL_CMD_FUNC(set80211kickmac, val, d)
1194149029Ssam{
1195149029Ssam	char *temp;
1196149029Ssam	struct sockaddr_dl sdl;
1197149029Ssam	struct ieee80211req_mlme mlme;
1198149029Ssam
1199155702Ssam	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1200149029Ssam	if (temp == NULL)
1201149029Ssam		errx(1, "malloc failed");
1202149029Ssam	temp[0] = ':';
1203149029Ssam	strcpy(temp + 1, val);
1204149029Ssam	sdl.sdl_len = sizeof(sdl);
1205149029Ssam	link_addr(temp, &sdl);
1206149029Ssam	free(temp);
1207149029Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1208149029Ssam		errx(1, "malformed link-level address");
1209149029Ssam	memset(&mlme, 0, sizeof(mlme));
1210149029Ssam	mlme.im_op = IEEE80211_MLME_DEAUTH;
1211149029Ssam	mlme.im_reason = IEEE80211_REASON_AUTH_EXPIRE;
1212149029Ssam	memcpy(mlme.im_macaddr, LLADDR(&sdl), IEEE80211_ADDR_LEN);
1213170531Ssam	set80211(s, IEEE80211_IOC_MLME, 0, sizeof(mlme), &mlme);
1214149029Ssam}
1215149029Ssam
1216149029Ssamstatic
1217138593SsamDECL_CMD_FUNC(set80211maccmd, val, d)
1218138593Ssam{
1219138593Ssam	set80211(s, IEEE80211_IOC_MACCMD, d, 0, NULL);
1220138593Ssam}
1221138593Ssam
1222147795Ssamstatic void
1223147795Ssamset80211pureg(const char *val, int d, int s, const struct afswtch *rafp)
1224147795Ssam{
1225147795Ssam	set80211(s, IEEE80211_IOC_PUREG, d, 0, NULL);
1226147795Ssam}
1227147795Ssam
1228153422Ssamstatic void
1229170531Ssamset80211bgscan(const char *val, int d, int s, const struct afswtch *rafp)
1230153422Ssam{
1231170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN, d, 0, NULL);
1232153422Ssam}
1233153422Ssam
1234148416Ssamstatic
1235170531SsamDECL_CMD_FUNC(set80211bgscanidle, val, d)
1236170531Ssam{
1237170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN_IDLE, atoi(val), 0, NULL);
1238170531Ssam}
1239170531Ssam
1240170531Ssamstatic
1241170531SsamDECL_CMD_FUNC(set80211bgscanintvl, val, d)
1242170531Ssam{
1243170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN_INTERVAL, atoi(val), 0, NULL);
1244170531Ssam}
1245170531Ssam
1246170531Ssamstatic
1247170531SsamDECL_CMD_FUNC(set80211scanvalid, val, d)
1248170531Ssam{
1249170531Ssam	set80211(s, IEEE80211_IOC_SCANVALID, atoi(val), 0, NULL);
1250170531Ssam}
1251170531Ssam
1252178354Ssam/*
1253178354Ssam * Parse an optional trailing specification of which netbands
1254178354Ssam * to apply a parameter to.  This is basically the same syntax
1255178354Ssam * as used for channels but you can concatenate to specify
1256178354Ssam * multiple.  For example:
1257178354Ssam *	14:abg		apply to 11a, 11b, and 11g
1258178354Ssam *	6:ht		apply to 11na and 11ng
1259178354Ssam * We don't make a big effort to catch silly things; this is
1260178354Ssam * really a convenience mechanism.
1261178354Ssam */
1262178354Ssamstatic int
1263178354Ssamgetmodeflags(const char *val)
1264170531Ssam{
1265178354Ssam	const char *cp;
1266178354Ssam	int flags;
1267178354Ssam
1268178354Ssam	flags = 0;
1269178354Ssam
1270178354Ssam	cp = strchr(val, ':');
1271178354Ssam	if (cp != NULL) {
1272178354Ssam		for (cp++; isalpha((int) *cp); cp++) {
1273178354Ssam			/* accept mixed case */
1274178354Ssam			int c = *cp;
1275178354Ssam			if (isupper(c))
1276178354Ssam				c = tolower(c);
1277178354Ssam			switch (c) {
1278178354Ssam			case 'a':		/* 802.11a */
1279178354Ssam				flags |= IEEE80211_CHAN_A;
1280178354Ssam				break;
1281178354Ssam			case 'b':		/* 802.11b */
1282178354Ssam				flags |= IEEE80211_CHAN_B;
1283178354Ssam				break;
1284178354Ssam			case 'g':		/* 802.11g */
1285178354Ssam				flags |= IEEE80211_CHAN_G;
1286178354Ssam				break;
1287178354Ssam			case 'h':		/* ht = 802.11n */
1288178354Ssam			case 'n':		/* 802.11n */
1289178354Ssam				flags |= IEEE80211_CHAN_HT;
1290178354Ssam				break;
1291178354Ssam			case 'd':		/* dt = Atheros Dynamic Turbo */
1292178354Ssam				flags |= IEEE80211_CHAN_TURBO;
1293178354Ssam				break;
1294178354Ssam			case 't':		/* ht, dt, st, t */
1295178354Ssam				/* dt and unadorned t specify Dynamic Turbo */
1296178354Ssam				if ((flags & (IEEE80211_CHAN_STURBO|IEEE80211_CHAN_HT)) == 0)
1297178354Ssam					flags |= IEEE80211_CHAN_TURBO;
1298178354Ssam				break;
1299178354Ssam			case 's':		/* st = Atheros Static Turbo */
1300178354Ssam				flags |= IEEE80211_CHAN_STURBO;
1301178354Ssam				break;
1302178354Ssam			default:
1303178354Ssam				errx(-1, "%s: Invalid mode attribute %c\n",
1304178354Ssam				    val, *cp);
1305178354Ssam			}
1306178354Ssam		}
1307178354Ssam	}
1308178354Ssam	return flags;
1309170531Ssam}
1310170531Ssam
1311178354Ssam#define	IEEE80211_CHAN_HTA	(IEEE80211_CHAN_HT|IEEE80211_CHAN_5GHZ)
1312178354Ssam#define	IEEE80211_CHAN_HTG	(IEEE80211_CHAN_HT|IEEE80211_CHAN_2GHZ)
1313178354Ssam
1314178354Ssam#define	_APPLY(_flags, _base, _param, _v) do {				\
1315178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1316178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1317178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1318178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1319178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1320178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1321178354Ssam	    else							\
1322178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1323178354Ssam    }									\
1324178354Ssam    if (_flags & IEEE80211_CHAN_TURBO) {				\
1325178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1326178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;	\
1327178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;	\
1328178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1329178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;	\
1330178354Ssam	    else							\
1331178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;	\
1332178354Ssam    }									\
1333178354Ssam    if (_flags & IEEE80211_CHAN_STURBO)					\
1334178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = _v;		\
1335178354Ssam    if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1336178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = _v;		\
1337178354Ssam    if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1338178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = _v;		\
1339178354Ssam    if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1340178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = _v;		\
1341178354Ssam} while (0)
1342178354Ssam#define	_APPLY1(_flags, _base, _param, _v) do {				\
1343178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1344178354Ssam	    if (_flags & IEEE80211_CHAN_5GHZ)				\
1345178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1346178354Ssam	    else							\
1347178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1348178354Ssam    } else if ((_flags & IEEE80211_CHAN_108A) == IEEE80211_CHAN_108A)	\
1349178354Ssam	    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;		\
1350178354Ssam    else if ((_flags & IEEE80211_CHAN_108G) == IEEE80211_CHAN_108G)	\
1351178354Ssam	    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;		\
1352178354Ssam    else if ((_flags & IEEE80211_CHAN_ST) == IEEE80211_CHAN_ST)		\
1353178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = _v;		\
1354178354Ssam    else if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1355178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = _v;		\
1356178354Ssam    else if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1357178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = _v;		\
1358178354Ssam    else if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1359178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = _v;		\
1360178354Ssam} while (0)
1361178354Ssam#define	_APPLY_RATE(_flags, _base, _param, _v) do {			\
1362178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1363178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1364178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v|0x80;	\
1365178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v|0x80;	\
1366178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1367178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v|0x80;	\
1368178354Ssam	    else							\
1369178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v|0x80;	\
1370178354Ssam    }									\
1371178354Ssam    if (_flags & IEEE80211_CHAN_TURBO) {				\
1372178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1373178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = 2*_v;	\
1374178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = 2*_v;	\
1375178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1376178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = 2*_v;	\
1377178354Ssam	    else							\
1378178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = 2*_v;	\
1379178354Ssam    }									\
1380178354Ssam    if (_flags & IEEE80211_CHAN_STURBO)					\
1381178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = 2*_v;	\
1382178354Ssam    if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1383178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = 2*_v;		\
1384178354Ssam    if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1385178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = (_v == 5 ? 11 : 2*_v);\
1386178354Ssam    if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1387178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = (_v == 5 ? 11 : 2*_v);\
1388178354Ssam} while (0)
1389178354Ssam#define	_APPLY_RATE1(_flags, _base, _param, _v) do {			\
1390178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1391178354Ssam	    if (_flags & IEEE80211_CHAN_5GHZ)				\
1392178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v|0x80;	\
1393178354Ssam	    else							\
1394178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v|0x80;	\
1395178354Ssam    } else if ((_flags & IEEE80211_CHAN_108A) == IEEE80211_CHAN_108A)	\
1396178354Ssam	    _base.params[IEEE80211_MODE_TURBO_A]._param = 2*_v;		\
1397178354Ssam    else if ((_flags & IEEE80211_CHAN_108G) == IEEE80211_CHAN_108G)	\
1398178354Ssam	    _base.params[IEEE80211_MODE_TURBO_G]._param = 2*_v;		\
1399178354Ssam    else if ((_flags & IEEE80211_CHAN_ST) == IEEE80211_CHAN_ST)		\
1400178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = 2*_v;	\
1401178354Ssam    else if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1402178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = 2*_v;		\
1403178354Ssam    else if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1404178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = (_v == 5 ? 11 : 2*_v);\
1405178354Ssam    else if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1406178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = (_v == 5 ? 11 : 2*_v);\
1407178354Ssam} while (0)
1408178354Ssam
1409170531Ssamstatic
1410178354SsamDECL_CMD_FUNC(set80211roamrssi, val, d)
1411170531Ssam{
1412178354Ssam	double v = atof(val);
1413178354Ssam	int rssi, flags;
1414178354Ssam
1415178354Ssam	rssi = (int) (2*v);
1416178354Ssam	if (rssi != 2*v)
1417178354Ssam		errx(-1, "invalid rssi (must be .5 dBm units)");
1418178354Ssam	flags = getmodeflags(val);
1419178354Ssam	getroam(s);
1420178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1421178354Ssam		flags = getcurchan(s)->ic_flags;
1422178354Ssam		_APPLY1(flags, roamparams, rssi, rssi);
1423178354Ssam	} else
1424178354Ssam		_APPLY(flags, roamparams, rssi, rssi);
1425178354Ssam	callback_register(setroam_cb, &roamparams);
1426170531Ssam}
1427170531Ssam
1428170531Ssamstatic
1429178354SsamDECL_CMD_FUNC(set80211roamrate, val, d)
1430170531Ssam{
1431178354Ssam	int v = atoi(val), flags;
1432178354Ssam
1433178354Ssam	flags = getmodeflags(val);
1434178354Ssam	getroam(s);
1435178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1436178354Ssam		flags = getcurchan(s)->ic_flags;
1437178354Ssam		_APPLY_RATE1(flags, roamparams, rate, v);
1438178354Ssam	} else
1439178354Ssam		_APPLY_RATE(flags, roamparams, rate, v);
1440178354Ssam	callback_register(setroam_cb, &roamparams);
1441170531Ssam}
1442170531Ssam
1443170531Ssamstatic
1444178354SsamDECL_CMD_FUNC(set80211mcastrate, val, d)
1445170531Ssam{
1446178354Ssam	int v = atoi(val), flags;
1447178354Ssam
1448178354Ssam	flags = getmodeflags(val);
1449178354Ssam	gettxparams(s);
1450178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1451178354Ssam		flags = getcurchan(s)->ic_flags;
1452178354Ssam		_APPLY_RATE1(flags, txparams, mcastrate, v);
1453178354Ssam	} else
1454178354Ssam		_APPLY_RATE(flags, txparams, mcastrate, v);
1455178354Ssam	callback_register(settxparams_cb, &txparams);
1456170531Ssam}
1457170531Ssam
1458170531Ssamstatic
1459178354SsamDECL_CMD_FUNC(set80211mgtrate, val, d)
1460170531Ssam{
1461178354Ssam	int v = atoi(val), flags;
1462178354Ssam
1463178354Ssam	flags = getmodeflags(val);
1464178354Ssam	gettxparams(s);
1465178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1466178354Ssam		flags = getcurchan(s)->ic_flags;
1467178354Ssam		_APPLY_RATE1(flags, txparams, mgmtrate, v);
1468178354Ssam	} else
1469178354Ssam		_APPLY_RATE(flags, txparams, mgmtrate, v);
1470178354Ssam	callback_register(settxparams_cb, &txparams);
1471170531Ssam}
1472170531Ssam
1473170531Ssamstatic
1474178354SsamDECL_CMD_FUNC(set80211ucastrate, val, d)
1475170531Ssam{
1476178354Ssam	int v, flags;
1477178354Ssam
1478178354Ssam	gettxparams(s);
1479178354Ssam	flags = getmodeflags(val);
1480178354Ssam	if (isanyarg(val)) {
1481178354Ssam		if (flags == 0) {	/* NB: no flags => current channel */
1482178354Ssam			flags = getcurchan(s)->ic_flags;
1483178354Ssam			_APPLY1(flags, txparams, ucastrate,
1484178354Ssam			    IEEE80211_FIXED_RATE_NONE);
1485178354Ssam		} else
1486178354Ssam			_APPLY(flags, txparams, ucastrate,
1487178354Ssam			    IEEE80211_FIXED_RATE_NONE);
1488178354Ssam	} else {
1489178354Ssam		v = atoi(val);
1490178354Ssam		if (flags == 0) {	/* NB: no flags => current channel */
1491178354Ssam			flags = getcurchan(s)->ic_flags;
1492178354Ssam			_APPLY_RATE1(flags, txparams, ucastrate, v);
1493178354Ssam		} else
1494178354Ssam			_APPLY_RATE(flags, txparams, ucastrate, v);
1495178354Ssam	}
1496178354Ssam	callback_register(settxparams_cb, &txparams);
1497170531Ssam}
1498170531Ssam
1499170531Ssamstatic
1500178354SsamDECL_CMD_FUNC(set80211maxretry, val, d)
1501153354Ssam{
1502178354Ssam	int v = atoi(val), flags;
1503178354Ssam
1504178354Ssam	flags = getmodeflags(val);
1505178354Ssam	gettxparams(s);
1506178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1507178354Ssam		flags = getcurchan(s)->ic_flags;
1508178354Ssam		_APPLY1(flags, txparams, maxretry, v);
1509178354Ssam	} else
1510178354Ssam		_APPLY(flags, txparams, maxretry, v);
1511178354Ssam	callback_register(settxparams_cb, &txparams);
1512153354Ssam}
1513178354Ssam#undef _APPLY_RATE
1514178354Ssam#undef _APPLY
1515178354Ssam#undef IEEE80211_CHAN_HTA
1516178354Ssam#undef IEEE80211_CHAN_HTG
1517153354Ssam
1518153354Ssamstatic
1519148416SsamDECL_CMD_FUNC(set80211fragthreshold, val, d)
1520148416Ssam{
1521148416Ssam	set80211(s, IEEE80211_IOC_FRAGTHRESHOLD,
1522148416Ssam		isundefarg(val) ? IEEE80211_FRAG_MAX : atoi(val), 0, NULL);
1523148416Ssam}
1524148416Ssam
1525160687Ssamstatic
1526160687SsamDECL_CMD_FUNC(set80211bmissthreshold, val, d)
1527160687Ssam{
1528160687Ssam	set80211(s, IEEE80211_IOC_BMISSTHRESHOLD,
1529160687Ssam		isundefarg(val) ? IEEE80211_HWBMISS_MAX : atoi(val), 0, NULL);
1530160687Ssam}
1531160687Ssam
1532170531Ssamstatic void
1533170531Ssamset80211burst(const char *val, int d, int s, const struct afswtch *rafp)
1534170531Ssam{
1535170531Ssam	set80211(s, IEEE80211_IOC_BURST, d, 0, NULL);
1536170531Ssam}
1537170531Ssam
1538170531Ssamstatic void
1539170531Ssamset80211doth(const char *val, int d, int s, const struct afswtch *rafp)
1540170531Ssam{
1541170531Ssam	set80211(s, IEEE80211_IOC_DOTH, d, 0, NULL);
1542170531Ssam}
1543170531Ssam
1544173275Ssamstatic void
1545178354Ssamset80211dfs(const char *val, int d, int s, const struct afswtch *rafp)
1546178354Ssam{
1547178354Ssam	set80211(s, IEEE80211_IOC_DFS, d, 0, NULL);
1548178354Ssam}
1549178354Ssam
1550178354Ssamstatic void
1551173275Ssamset80211shortgi(const char *val, int d, int s, const struct afswtch *rafp)
1552173275Ssam{
1553173275Ssam	set80211(s, IEEE80211_IOC_SHORTGI,
1554173275Ssam		d ? (IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40) : 0,
1555173275Ssam		0, NULL);
1556173275Ssam}
1557173275Ssam
1558173275Ssamstatic void
1559173275Ssamset80211ampdu(const char *val, int d, int s, const struct afswtch *rafp)
1560173275Ssam{
1561173275Ssam	int ampdu;
1562173275Ssam
1563173275Ssam	if (get80211val(s, IEEE80211_IOC_AMPDU, &ampdu) < 0)
1564173275Ssam		errx(-1, "cannot get AMPDU setting");
1565173275Ssam	if (d < 0) {
1566173275Ssam		d = -d;
1567173275Ssam		ampdu &= ~d;
1568173275Ssam	} else
1569173275Ssam		ampdu |= d;
1570173275Ssam	set80211(s, IEEE80211_IOC_AMPDU, ampdu, 0, NULL);
1571173275Ssam}
1572173275Ssam
1573173275Ssamstatic
1574173275SsamDECL_CMD_FUNC(set80211ampdulimit, val, d)
1575173275Ssam{
1576173275Ssam	int v;
1577173275Ssam
1578173275Ssam	switch (atoi(val)) {
1579173275Ssam	case 8:
1580173275Ssam	case 8*1024:
1581173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_8K;
1582173275Ssam		break;
1583173275Ssam	case 16:
1584173275Ssam	case 16*1024:
1585173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_16K;
1586173275Ssam		break;
1587173275Ssam	case 32:
1588173275Ssam	case 32*1024:
1589173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_32K;
1590173275Ssam		break;
1591173275Ssam	case 64:
1592173275Ssam	case 64*1024:
1593173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_64K;
1594173275Ssam		break;
1595173275Ssam	default:
1596173275Ssam		errx(-1, "invalid A-MPDU limit %s", val);
1597173275Ssam	}
1598173275Ssam	set80211(s, IEEE80211_IOC_AMPDU_LIMIT, v, 0, NULL);
1599173275Ssam}
1600173275Ssam
1601173275Ssamstatic
1602173275SsamDECL_CMD_FUNC(set80211ampdudensity, val, d)
1603173275Ssam{
1604173275Ssam	int v;
1605173275Ssam
1606183260Ssam	if (isanyarg(val) || strcasecmp(val, "na") == 0)
1607173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1608173275Ssam	else switch ((int)(atof(val)*4)) {
1609173275Ssam	case 0:
1610173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1611173275Ssam		break;
1612173275Ssam	case 1:
1613173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_025;
1614173275Ssam		break;
1615173275Ssam	case 2:
1616173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_05;
1617173275Ssam		break;
1618173275Ssam	case 4:
1619173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_1;
1620173275Ssam		break;
1621173275Ssam	case 8:
1622173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_2;
1623173275Ssam		break;
1624173275Ssam	case 16:
1625173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_4;
1626173275Ssam		break;
1627173275Ssam	case 32:
1628173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_8;
1629173275Ssam		break;
1630173275Ssam	case 64:
1631173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_16;
1632173275Ssam		break;
1633173275Ssam	default:
1634173275Ssam		errx(-1, "invalid A-MPDU density %s", val);
1635173275Ssam	}
1636173275Ssam	set80211(s, IEEE80211_IOC_AMPDU_DENSITY, v, 0, NULL);
1637173275Ssam}
1638173275Ssam
1639173275Ssamstatic void
1640173275Ssamset80211amsdu(const char *val, int d, int s, const struct afswtch *rafp)
1641173275Ssam{
1642173275Ssam	int amsdu;
1643173275Ssam
1644173275Ssam	if (get80211val(s, IEEE80211_IOC_AMSDU, &amsdu) < 0)
1645173275Ssam		errx(-1, "cannot get AMSDU setting");
1646173275Ssam	if (d < 0) {
1647173275Ssam		d = -d;
1648173275Ssam		amsdu &= ~d;
1649173275Ssam	} else
1650173275Ssam		amsdu |= d;
1651173275Ssam	set80211(s, IEEE80211_IOC_AMSDU, amsdu, 0, NULL);
1652173275Ssam}
1653173275Ssam
1654173275Ssamstatic
1655173275SsamDECL_CMD_FUNC(set80211amsdulimit, val, d)
1656173275Ssam{
1657173275Ssam	set80211(s, IEEE80211_IOC_AMSDU_LIMIT, atoi(val), 0, NULL);
1658173275Ssam}
1659173275Ssam
1660173275Ssamstatic void
1661173275Ssamset80211puren(const char *val, int d, int s, const struct afswtch *rafp)
1662173275Ssam{
1663173275Ssam	set80211(s, IEEE80211_IOC_PUREN, d, 0, NULL);
1664173275Ssam}
1665173275Ssam
1666173275Ssamstatic void
1667173275Ssamset80211htcompat(const char *val, int d, int s, const struct afswtch *rafp)
1668173275Ssam{
1669173275Ssam	set80211(s, IEEE80211_IOC_HTCOMPAT, d, 0, NULL);
1670173275Ssam}
1671173275Ssam
1672173275Ssamstatic void
1673173275Ssamset80211htconf(const char *val, int d, int s, const struct afswtch *rafp)
1674173275Ssam{
1675173275Ssam	set80211(s, IEEE80211_IOC_HTCONF, d, 0, NULL);
1676173275Ssam	htconf = d;
1677173275Ssam}
1678173275Ssam
1679173275Ssamstatic void
1680178354Ssamset80211dwds(const char *val, int d, int s, const struct afswtch *rafp)
1681178354Ssam{
1682178354Ssam	set80211(s, IEEE80211_IOC_DWDS, d, 0, NULL);
1683178354Ssam}
1684178354Ssam
1685178354Ssamstatic void
1686173275Ssamset80211inact(const char *val, int d, int s, const struct afswtch *rafp)
1687173275Ssam{
1688173275Ssam	set80211(s, IEEE80211_IOC_INACTIVITY, d, 0, NULL);
1689173275Ssam}
1690173275Ssam
1691173275Ssamstatic void
1692178354Ssamset80211tsn(const char *val, int d, int s, const struct afswtch *rafp)
1693178354Ssam{
1694178354Ssam	set80211(s, IEEE80211_IOC_TSN, d, 0, NULL);
1695178354Ssam}
1696178354Ssam
1697178354Ssamstatic void
1698178354Ssamset80211dotd(const char *val, int d, int s, const struct afswtch *rafp)
1699178354Ssam{
1700178354Ssam	set80211(s, IEEE80211_IOC_DOTD, d, 0, NULL);
1701178354Ssam}
1702178354Ssam
1703183261Ssamstatic void
1704183261Ssamset80211smps(const char *val, int d, int s, const struct afswtch *rafp)
1705183261Ssam{
1706183261Ssam	set80211(s, IEEE80211_IOC_SMPS, d, 0, NULL);
1707183261Ssam}
1708183261Ssam
1709183261Ssamstatic void
1710183261Ssamset80211rifs(const char *val, int d, int s, const struct afswtch *rafp)
1711183261Ssam{
1712183261Ssam	set80211(s, IEEE80211_IOC_RIFS, d, 0, NULL);
1713183261Ssam}
1714183261Ssam
1715178354Ssamstatic int
1716178354Ssamregdomain_sort(const void *a, const void *b)
1717178354Ssam{
1718178354Ssam#define	CHAN_ALL \
1719178354Ssam	(IEEE80211_CHAN_ALLTURBO|IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)
1720178354Ssam	const struct ieee80211_channel *ca = a;
1721178354Ssam	const struct ieee80211_channel *cb = b;
1722178354Ssam
1723178354Ssam	return ca->ic_freq == cb->ic_freq ?
1724178354Ssam	    (ca->ic_flags & CHAN_ALL) - (cb->ic_flags & CHAN_ALL) :
1725178354Ssam	    ca->ic_freq - cb->ic_freq;
1726178354Ssam#undef CHAN_ALL
1727178354Ssam}
1728178354Ssam
1729178354Ssamstatic const struct ieee80211_channel *
1730178354Ssamchanlookup(const struct ieee80211_channel chans[], int nchans,
1731178354Ssam	int freq, int flags)
1732178354Ssam{
1733178354Ssam	int i;
1734178354Ssam
1735178354Ssam	flags &= IEEE80211_CHAN_ALLTURBO;
1736178354Ssam	for (i = 0; i < nchans; i++) {
1737178354Ssam		const struct ieee80211_channel *c = &chans[i];
1738178354Ssam		if (c->ic_freq == freq &&
1739178354Ssam		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1740178354Ssam			return c;
1741178354Ssam	}
1742178354Ssam	return NULL;
1743178354Ssam}
1744178354Ssam
1745178354Ssamstatic void
1746178354Ssamregdomain_addchans(struct ieee80211req_chaninfo *ci,
1747178354Ssam	const netband_head *bands,
1748178354Ssam	const struct ieee80211_regdomain *reg,
1749178354Ssam	uint32_t chanFlags,
1750178354Ssam	const struct ieee80211req_chaninfo *avail)
1751178354Ssam{
1752178354Ssam	const struct netband *nb;
1753178354Ssam	const struct freqband *b;
1754178354Ssam	struct ieee80211_channel *c, *prev;
1755178354Ssam	int freq, channelSep;
1756178354Ssam
1757178354Ssam	channelSep = (chanFlags & IEEE80211_CHAN_2GHZ) ? 0 : 40;
1758178354Ssam	LIST_FOREACH(nb, bands, next) {
1759178354Ssam		b = nb->band;
1760178354Ssam		if (verbose)
1761178354Ssam			printf("%s: chanFlags 0x%x b %p\n",
1762178354Ssam			    __func__, chanFlags, b);
1763178354Ssam		prev = NULL;
1764178354Ssam		for (freq = b->freqStart; freq <= b->freqEnd; freq += b->chanSep) {
1765178354Ssam			uint32_t flags = nb->flags | b->flags;
1766178354Ssam
1767178354Ssam			/* check if device can operate on this frequency */
1768178354Ssam			if (chanlookup(avail->ic_chans, avail->ic_nchans, freq, chanFlags) == NULL) {
1769178354Ssam				if (verbose)
1770178354Ssam					printf("%u: skip, flags 0x%x not available\n", freq, chanFlags);
1771178354Ssam				continue;
1772178354Ssam			}
1773178354Ssam			if ((flags & IEEE80211_CHAN_HALF) &&
1774178354Ssam			    (chanFlags & IEEE80211_CHAN_HALF) == 0) {
1775178354Ssam				if (verbose)
1776178354Ssam					printf("%u: skip, device does not support half-rate channels\n", freq);
1777178354Ssam				continue;
1778178354Ssam			}
1779178354Ssam			if ((flags & IEEE80211_CHAN_QUARTER) &&
1780178354Ssam			    (chanFlags & IEEE80211_CHAN_QUARTER) == 0) {
1781178354Ssam				if (verbose)
1782178354Ssam					printf("%u: skip, device does not support quarter-rate channels\n", freq);
1783178354Ssam				continue;
1784178354Ssam			}
1785178354Ssam			if ((flags & IEEE80211_CHAN_HT20) &&
1786178354Ssam			    (chanFlags & IEEE80211_CHAN_HT20) == 0) {
1787178354Ssam				if (verbose)
1788178354Ssam					printf("%u: skip, device does not support HT20 operation\n", freq);
1789178354Ssam				continue;
1790178354Ssam			}
1791178354Ssam			if ((flags & IEEE80211_CHAN_HT40) &&
1792178354Ssam			    (chanFlags & IEEE80211_CHAN_HT40) == 0) {
1793178354Ssam				if (verbose)
1794178354Ssam					printf("%u: skip, device does not support HT40 operation\n", freq);
1795178354Ssam				continue;
1796178354Ssam			}
1797178354Ssam			if ((flags & REQ_ECM) && !reg->ecm) {
1798178354Ssam				if (verbose)
1799178354Ssam					printf("%u: skip, ECM channel\n", freq);
1800178354Ssam				continue;
1801178354Ssam			}
1802178354Ssam			if ((flags & REQ_OUTDOOR) && reg->location == 'I') {
1803178354Ssam				if (verbose)
1804178354Ssam					printf("%u: skip, outdoor channel\n", freq);
1805178354Ssam				continue;
1806178354Ssam			}
1807178354Ssam			if ((flags & IEEE80211_CHAN_HT40) &&
1808178354Ssam			    prev != NULL && (freq - prev->ic_freq) < channelSep) {
1809178354Ssam				if (verbose)
1810178354Ssam					printf("%u: skip, only %u channel "
1811178354Ssam					    "separation, need %d\n", freq,
1812178354Ssam					    freq - prev->ic_freq, channelSep);
1813178354Ssam				continue;
1814178354Ssam			}
1815178354Ssam			if (ci->ic_nchans == IEEE80211_CHAN_MAX) {
1816178354Ssam				if (verbose)
1817178354Ssam					printf("%u: skip, channel table full\n", freq);
1818178354Ssam				break;
1819178354Ssam			}
1820178354Ssam			c = &ci->ic_chans[ci->ic_nchans++];
1821178354Ssam			c->ic_freq = freq;
1822178354Ssam			c->ic_flags = chanFlags |
1823178354Ssam			    (flags &~ (REQ_FLAGS | IEEE80211_CHAN_HT40));
1824178354Ssam			if (c->ic_flags & IEEE80211_CHAN_DFS)
1825178354Ssam				c->ic_maxregpower = nb->maxPowerDFS;
1826178354Ssam			else
1827178354Ssam				c->ic_maxregpower = nb->maxPower;
1828178354Ssam			if (verbose)
1829178354Ssam				printf("[%3d] add freq %u flags 0x%x power %u\n",
1830178354Ssam				    ci->ic_nchans-1, c->ic_freq, c->ic_flags,
1831178354Ssam				    c->ic_maxregpower);
1832178354Ssam			/* NB: kernel fills in other fields */
1833178354Ssam			prev = c;
1834178354Ssam		}
1835178354Ssam	}
1836178354Ssam}
1837178354Ssam
1838178354Ssamstatic void
1839178354Ssamregdomain_makechannels(
1840178354Ssam	struct ieee80211_regdomain_req *req,
1841178354Ssam	const struct ieee80211_devcaps_req *dc)
1842178354Ssam{
1843178354Ssam	struct regdata *rdp = getregdata();
1844178354Ssam	const struct country *cc;
1845178354Ssam	const struct ieee80211_regdomain *reg = &req->rd;
1846178354Ssam	struct ieee80211req_chaninfo *ci = &req->chaninfo;
1847178354Ssam	const struct regdomain *rd;
1848178354Ssam
1849178354Ssam	/*
1850178354Ssam	 * Locate construction table for new channel list.  We treat
1851178354Ssam	 * the regdomain/SKU as definitive so a country can be in
1852178354Ssam	 * multiple with different properties (e.g. US in FCC+FCC3).
1853178354Ssam	 * If no regdomain is specified then we fallback on the country
1854178354Ssam	 * code to find the associated regdomain since countries always
1855178354Ssam	 * belong to at least one regdomain.
1856178354Ssam	 */
1857178354Ssam	if (reg->regdomain == 0) {
1858178354Ssam		cc = lib80211_country_findbycc(rdp, reg->country);
1859178354Ssam		if (cc == NULL)
1860178354Ssam			errx(1, "internal error, country %d not found",
1861178354Ssam			    reg->country);
1862178354Ssam		rd = cc->rd;
1863178354Ssam	} else
1864178354Ssam		rd = lib80211_regdomain_findbysku(rdp, reg->regdomain);
1865178354Ssam	if (rd == NULL)
1866178354Ssam		errx(1, "internal error, regdomain %d not found",
1867178354Ssam			    reg->regdomain);
1868178354Ssam	if (rd->sku != SKU_DEBUG) {
1869178354Ssam		memset(ci, 0, sizeof(*ci));
1870178354Ssam		if (!LIST_EMPTY(&rd->bands_11b))
1871178354Ssam			regdomain_addchans(ci, &rd->bands_11b, reg,
1872178354Ssam			    IEEE80211_CHAN_B, &dc->dc_chaninfo);
1873178354Ssam		if (!LIST_EMPTY(&rd->bands_11g))
1874178354Ssam			regdomain_addchans(ci, &rd->bands_11g, reg,
1875178354Ssam			    IEEE80211_CHAN_G, &dc->dc_chaninfo);
1876178354Ssam		if (!LIST_EMPTY(&rd->bands_11a))
1877178354Ssam			regdomain_addchans(ci, &rd->bands_11a, reg,
1878178354Ssam			    IEEE80211_CHAN_A, &dc->dc_chaninfo);
1879178354Ssam		if (!LIST_EMPTY(&rd->bands_11na)) {
1880178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
1881178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT20,
1882178354Ssam			    &dc->dc_chaninfo);
1883178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
1884178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U,
1885178354Ssam			    &dc->dc_chaninfo);
1886178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
1887178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D,
1888178354Ssam			    &dc->dc_chaninfo);
1889178354Ssam		}
1890178354Ssam		if (!LIST_EMPTY(&rd->bands_11ng)) {
1891178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
1892178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT20,
1893178354Ssam			    &dc->dc_chaninfo);
1894178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
1895178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U,
1896178354Ssam			    &dc->dc_chaninfo);
1897178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
1898178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D,
1899178354Ssam			    &dc->dc_chaninfo);
1900178354Ssam		}
1901178354Ssam		qsort(ci->ic_chans, ci->ic_nchans, sizeof(ci->ic_chans[0]),
1902178354Ssam		    regdomain_sort);
1903178354Ssam	} else
1904178354Ssam		*ci = dc->dc_chaninfo;
1905178354Ssam}
1906178354Ssam
1907178354Ssamstatic void
1908178354Ssamlist_countries(void)
1909178354Ssam{
1910178354Ssam	struct regdata *rdp = getregdata();
1911178354Ssam	const struct country *cp;
1912178354Ssam	const struct regdomain *dp;
1913178354Ssam	int i;
1914178354Ssam
1915178354Ssam	i = 0;
1916178354Ssam	printf("\nCountry codes:\n");
1917178354Ssam	LIST_FOREACH(cp, &rdp->countries, next) {
1918178354Ssam		printf("%2s %-15.15s%s", cp->isoname,
1919178354Ssam		    cp->name, ((i+1)%4) == 0 ? "\n" : " ");
1920178354Ssam		i++;
1921178354Ssam	}
1922178354Ssam	i = 0;
1923178354Ssam	printf("\nRegulatory domains:\n");
1924178354Ssam	LIST_FOREACH(dp, &rdp->domains, next) {
1925178354Ssam		printf("%-15.15s%s", dp->name, ((i+1)%4) == 0 ? "\n" : " ");
1926178354Ssam		i++;
1927178354Ssam	}
1928178354Ssam	printf("\n");
1929178354Ssam}
1930178354Ssam
1931178354Ssamstatic void
1932178354Ssamdefaultcountry(const struct regdomain *rd)
1933178354Ssam{
1934178354Ssam	struct regdata *rdp = getregdata();
1935178354Ssam	const struct country *cc;
1936178354Ssam
1937178354Ssam	cc = lib80211_country_findbycc(rdp, rd->cc->code);
1938178354Ssam	if (cc == NULL)
1939178354Ssam		errx(1, "internal error, ISO country code %d not "
1940178354Ssam		    "defined for regdomain %s", rd->cc->code, rd->name);
1941178354Ssam	regdomain.country = cc->code;
1942178354Ssam	regdomain.isocc[0] = cc->isoname[0];
1943178354Ssam	regdomain.isocc[1] = cc->isoname[1];
1944178354Ssam}
1945178354Ssam
1946178354Ssamstatic
1947178354SsamDECL_CMD_FUNC(set80211regdomain, val, d)
1948178354Ssam{
1949178354Ssam	struct regdata *rdp = getregdata();
1950178354Ssam	const struct regdomain *rd;
1951178354Ssam
1952178354Ssam	rd = lib80211_regdomain_findbyname(rdp, val);
1953178354Ssam	if (rd == NULL) {
1954178354Ssam		rd = lib80211_regdomain_findbysku(rdp, atoi(val));
1955178354Ssam		if (rd == NULL)
1956178354Ssam			errx(1, "unknown regdomain %s", val);
1957178354Ssam	}
1958178354Ssam	getregdomain(s);
1959178354Ssam	regdomain.regdomain = rd->sku;
1960178354Ssam	if (regdomain.country == 0 && rd->cc != NULL) {
1961178354Ssam		/*
1962178354Ssam		 * No country code setup and there's a default
1963178354Ssam		 * one for this regdomain fill it in.
1964178354Ssam		 */
1965178354Ssam		defaultcountry(rd);
1966178354Ssam	}
1967178354Ssam	callback_register(setregdomain_cb, &regdomain);
1968178354Ssam}
1969178354Ssam
1970178354Ssamstatic
1971178354SsamDECL_CMD_FUNC(set80211country, val, d)
1972178354Ssam{
1973178354Ssam	struct regdata *rdp = getregdata();
1974178354Ssam	const struct country *cc;
1975178354Ssam
1976178354Ssam	cc = lib80211_country_findbyname(rdp, val);
1977178354Ssam	if (cc == NULL) {
1978178354Ssam		cc = lib80211_country_findbycc(rdp, atoi(val));
1979178354Ssam		if (cc == NULL)
1980178354Ssam			errx(1, "unknown ISO country code %s", val);
1981178354Ssam	}
1982178354Ssam	getregdomain(s);
1983178354Ssam	regdomain.regdomain = cc->rd->sku;
1984178354Ssam	regdomain.country = cc->code;
1985178354Ssam	regdomain.isocc[0] = cc->isoname[0];
1986178354Ssam	regdomain.isocc[1] = cc->isoname[1];
1987178354Ssam	callback_register(setregdomain_cb, &regdomain);
1988178354Ssam}
1989178354Ssam
1990178354Ssamstatic void
1991178354Ssamset80211location(const char *val, int d, int s, const struct afswtch *rafp)
1992178354Ssam{
1993178354Ssam	getregdomain(s);
1994178354Ssam	regdomain.location = d;
1995178354Ssam	callback_register(setregdomain_cb, &regdomain);
1996178354Ssam}
1997178354Ssam
1998178354Ssamstatic void
1999178354Ssamset80211ecm(const char *val, int d, int s, const struct afswtch *rafp)
2000178354Ssam{
2001178354Ssam	getregdomain(s);
2002178354Ssam	regdomain.ecm = d;
2003178354Ssam	callback_register(setregdomain_cb, &regdomain);
2004178354Ssam}
2005178354Ssam
2006178354Ssamstatic void
2007173275SsamLINE_INIT(char c)
2008173275Ssam{
2009173275Ssam	spacer = c;
2010173275Ssam	if (c == '\t')
2011173275Ssam		col = 8;
2012173275Ssam	else
2013173275Ssam		col = 1;
2014173275Ssam}
2015173275Ssam
2016173275Ssamstatic void
2017173275SsamLINE_BREAK(void)
2018173275Ssam{
2019173275Ssam	if (spacer != '\t') {
2020173275Ssam		printf("\n");
2021173275Ssam		spacer = '\t';
2022173275Ssam	}
2023173275Ssam	col = 8;		/* 8-col tab */
2024173275Ssam}
2025173275Ssam
2026173275Ssamstatic void
2027173275SsamLINE_CHECK(const char *fmt, ...)
2028173275Ssam{
2029173275Ssam	char buf[80];
2030173275Ssam	va_list ap;
2031173275Ssam	int n;
2032173275Ssam
2033173275Ssam	va_start(ap, fmt);
2034173275Ssam	n = vsnprintf(buf+1, sizeof(buf)-1, fmt, ap);
2035173275Ssam	va_end(ap);
2036173275Ssam	col += 1+n;
2037173275Ssam	if (col > MAXCOL) {
2038173275Ssam		LINE_BREAK();
2039173275Ssam		col += n;
2040173275Ssam	}
2041173275Ssam	buf[0] = spacer;
2042173275Ssam	printf("%s", buf);
2043173275Ssam	spacer = ' ';
2044173275Ssam}
2045173275Ssam
2046138593Ssamstatic int
2047170531Ssamgetmaxrate(const uint8_t rates[15], uint8_t nrates)
2048138593Ssam{
2049138593Ssam	int i, maxrate = -1;
2050138593Ssam
2051138593Ssam	for (i = 0; i < nrates; i++) {
2052138593Ssam		int rate = rates[i] & IEEE80211_RATE_VAL;
2053138593Ssam		if (rate > maxrate)
2054138593Ssam			maxrate = rate;
2055138593Ssam	}
2056138593Ssam	return maxrate / 2;
2057138593Ssam}
2058138593Ssam
2059138593Ssamstatic const char *
2060138593Ssamgetcaps(int capinfo)
2061138593Ssam{
2062138593Ssam	static char capstring[32];
2063138593Ssam	char *cp = capstring;
2064138593Ssam
2065138593Ssam	if (capinfo & IEEE80211_CAPINFO_ESS)
2066138593Ssam		*cp++ = 'E';
2067138593Ssam	if (capinfo & IEEE80211_CAPINFO_IBSS)
2068138593Ssam		*cp++ = 'I';
2069138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
2070138593Ssam		*cp++ = 'c';
2071138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
2072138593Ssam		*cp++ = 'C';
2073138593Ssam	if (capinfo & IEEE80211_CAPINFO_PRIVACY)
2074138593Ssam		*cp++ = 'P';
2075138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
2076138593Ssam		*cp++ = 'S';
2077138593Ssam	if (capinfo & IEEE80211_CAPINFO_PBCC)
2078138593Ssam		*cp++ = 'B';
2079138593Ssam	if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
2080138593Ssam		*cp++ = 'A';
2081138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
2082138593Ssam		*cp++ = 's';
2083138593Ssam	if (capinfo & IEEE80211_CAPINFO_RSN)
2084138593Ssam		*cp++ = 'R';
2085138593Ssam	if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
2086138593Ssam		*cp++ = 'D';
2087138593Ssam	*cp = '\0';
2088138593Ssam	return capstring;
2089138593Ssam}
2090138593Ssam
2091159885Ssamstatic const char *
2092159885Ssamgetflags(int flags)
2093159885Ssam{
2094159885Ssam	static char flagstring[32];
2095159885Ssam	char *cp = flagstring;
2096159885Ssam
2097159885Ssam	if (flags & IEEE80211_NODE_AUTH)
2098159885Ssam		*cp++ = 'A';
2099159885Ssam	if (flags & IEEE80211_NODE_QOS)
2100159885Ssam		*cp++ = 'Q';
2101159885Ssam	if (flags & IEEE80211_NODE_ERP)
2102159885Ssam		*cp++ = 'E';
2103159885Ssam	if (flags & IEEE80211_NODE_PWR_MGT)
2104159885Ssam		*cp++ = 'P';
2105173275Ssam	if (flags & IEEE80211_NODE_HT) {
2106170531Ssam		*cp++ = 'H';
2107173275Ssam		if (flags & IEEE80211_NODE_HTCOMPAT)
2108173275Ssam			*cp++ = '+';
2109173275Ssam	}
2110173275Ssam	if (flags & IEEE80211_NODE_WPS)
2111173275Ssam		*cp++ = 'W';
2112173275Ssam	if (flags & IEEE80211_NODE_TSN)
2113183261Ssam		*cp++ = 'N';
2114183261Ssam	if (flags & IEEE80211_NODE_AMPDU_TX)
2115173275Ssam		*cp++ = 'T';
2116183261Ssam	if (flags & IEEE80211_NODE_AMPDU_RX)
2117183261Ssam		*cp++ = 'R';
2118183261Ssam	if (flags & IEEE80211_NODE_MIMO_PS) {
2119183261Ssam		*cp++ = 'M';
2120183261Ssam		if (flags & IEEE80211_NODE_MIMO_RTS)
2121183261Ssam			*cp++ = '+';
2122183261Ssam	}
2123183261Ssam	if (flags & IEEE80211_NODE_RIFS)
2124183261Ssam		*cp++ = 'I';
2125159885Ssam	*cp = '\0';
2126159885Ssam	return flagstring;
2127159885Ssam}
2128159885Ssam
2129138593Ssamstatic void
2130138593Ssamprintie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
2131138593Ssam{
2132138593Ssam	printf("%s", tag);
2133138593Ssam	if (verbose) {
2134138593Ssam		maxlen -= strlen(tag)+2;
2135138593Ssam		if (2*ielen > maxlen)
2136138593Ssam			maxlen--;
2137138593Ssam		printf("<");
2138138593Ssam		for (; ielen > 0; ie++, ielen--) {
2139138593Ssam			if (maxlen-- <= 0)
2140138593Ssam				break;
2141138593Ssam			printf("%02x", *ie);
2142138593Ssam		}
2143138593Ssam		if (ielen != 0)
2144138593Ssam			printf("-");
2145138593Ssam		printf(">");
2146138593Ssam	}
2147138593Ssam}
2148138593Ssam
2149170531Ssam#define LE_READ_2(p)					\
2150170531Ssam	((u_int16_t)					\
2151170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2152170531Ssam	  (((const u_int8_t *)(p))[1] <<  8)))
2153170531Ssam#define LE_READ_4(p)					\
2154170531Ssam	((u_int32_t)					\
2155170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2156170531Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
2157170531Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
2158170531Ssam	  (((const u_int8_t *)(p))[3] << 24)))
2159170531Ssam
2160138593Ssam/*
2161170531Ssam * NB: The decoding routines assume a properly formatted ie
2162170531Ssam *     which should be safe as the kernel only retains them
2163170531Ssam *     if they parse ok.
2164170531Ssam */
2165170531Ssam
2166170531Ssamstatic void
2167173275Ssamprintwmeparam(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2168170531Ssam{
2169170531Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
2170170531Ssam	static const char *acnames[] = { "BE", "BK", "VO", "VI" };
2171173275Ssam	const struct ieee80211_wme_param *wme =
2172173275Ssam	    (const struct ieee80211_wme_param *) ie;
2173170531Ssam	int i;
2174170531Ssam
2175170531Ssam	printf("%s", tag);
2176173275Ssam	if (!verbose)
2177173275Ssam		return;
2178173275Ssam	printf("<qosinfo 0x%x", wme->param_qosInfo);
2179173275Ssam	ie += offsetof(struct ieee80211_wme_param, params_acParams);
2180173275Ssam	for (i = 0; i < WME_NUM_AC; i++) {
2181173275Ssam		const struct ieee80211_wme_acparams *ac =
2182173275Ssam		    &wme->params_acParams[i];
2183173275Ssam
2184173275Ssam		printf(" %s[%saifsn %u cwmin %u cwmax %u txop %u]"
2185173275Ssam			, acnames[i]
2186173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_ACM) ? "acm " : ""
2187173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_AIFSN)
2188173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMIN)
2189173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMAX)
2190173275Ssam			, LE_READ_2(&ac->acp_txop)
2191173275Ssam		);
2192170531Ssam	}
2193173275Ssam	printf(">");
2194170531Ssam#undef MS
2195170531Ssam}
2196170531Ssam
2197170531Ssamstatic void
2198173275Ssamprintwmeinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2199173275Ssam{
2200173275Ssam	printf("%s", tag);
2201173275Ssam	if (verbose) {
2202173275Ssam		const struct ieee80211_wme_info *wme =
2203173275Ssam		    (const struct ieee80211_wme_info *) ie;
2204173275Ssam		printf("<version 0x%x info 0x%x>",
2205173275Ssam		    wme->wme_version, wme->wme_info);
2206173275Ssam	}
2207173275Ssam}
2208173275Ssam
2209173275Ssamstatic void
2210173275Ssamprinthtcap(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2211173275Ssam{
2212173275Ssam	printf("%s", tag);
2213173275Ssam	if (verbose) {
2214173275Ssam		const struct ieee80211_ie_htcap *htcap =
2215173275Ssam		    (const struct ieee80211_ie_htcap *) ie;
2216173275Ssam		const char *sep;
2217173275Ssam		int i, j;
2218173275Ssam
2219173275Ssam		printf("<cap 0x%x param 0x%x",
2220173275Ssam		    LE_READ_2(&htcap->hc_cap), htcap->hc_param);
2221173275Ssam		printf(" mcsset[");
2222173275Ssam		sep = "";
2223173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2224173275Ssam			if (isset(htcap->hc_mcsset, i)) {
2225173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2226173275Ssam					if (isclr(htcap->hc_mcsset, j))
2227173275Ssam						break;
2228173275Ssam				j--;
2229173275Ssam				if (i == j)
2230173275Ssam					printf("%s%u", sep, i);
2231173275Ssam				else
2232173275Ssam					printf("%s%u-%u", sep, i, j);
2233173275Ssam				i += j-i;
2234173275Ssam				sep = ",";
2235173275Ssam			}
2236173275Ssam		printf("] extcap 0x%x txbf 0x%x antenna 0x%x>",
2237173275Ssam		    LE_READ_2(&htcap->hc_extcap),
2238173275Ssam		    LE_READ_4(&htcap->hc_txbf),
2239173275Ssam		    htcap->hc_antenna);
2240173275Ssam	}
2241173275Ssam}
2242173275Ssam
2243173275Ssamstatic void
2244173275Ssamprinthtinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2245173275Ssam{
2246173275Ssam	printf("%s", tag);
2247173275Ssam	if (verbose) {
2248173275Ssam		const struct ieee80211_ie_htinfo *htinfo =
2249173275Ssam		    (const struct ieee80211_ie_htinfo *) ie;
2250173275Ssam		const char *sep;
2251173275Ssam		int i, j;
2252173275Ssam
2253173275Ssam		printf("<ctl %u, %x,%x,%x,%x", htinfo->hi_ctrlchannel,
2254173275Ssam		    htinfo->hi_byte1, htinfo->hi_byte2, htinfo->hi_byte3,
2255173275Ssam		    LE_READ_2(&htinfo->hi_byte45));
2256173275Ssam		printf(" basicmcs[");
2257173275Ssam		sep = "";
2258173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2259173275Ssam			if (isset(htinfo->hi_basicmcsset, i)) {
2260173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2261173275Ssam					if (isclr(htinfo->hi_basicmcsset, j))
2262173275Ssam						break;
2263173275Ssam				j--;
2264173275Ssam				if (i == j)
2265173275Ssam					printf("%s%u", sep, i);
2266173275Ssam				else
2267173275Ssam					printf("%s%u-%u", sep, i, j);
2268173275Ssam				i += j-i;
2269173275Ssam				sep = ",";
2270173275Ssam			}
2271173275Ssam		printf("]>");
2272173275Ssam	}
2273173275Ssam}
2274173275Ssam
2275173275Ssamstatic void
2276170531Ssamprintathie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2277170531Ssam{
2278170531Ssam
2279170531Ssam	printf("%s", tag);
2280170531Ssam	if (verbose) {
2281170531Ssam		const struct ieee80211_ath_ie *ath =
2282170531Ssam			(const struct ieee80211_ath_ie *)ie;
2283170531Ssam
2284170531Ssam		printf("<");
2285170531Ssam		if (ath->ath_capability & ATHEROS_CAP_TURBO_PRIME)
2286170531Ssam			printf("DTURBO,");
2287170531Ssam		if (ath->ath_capability & ATHEROS_CAP_COMPRESSION)
2288170531Ssam			printf("COMP,");
2289170531Ssam		if (ath->ath_capability & ATHEROS_CAP_FAST_FRAME)
2290170531Ssam			printf("FF,");
2291170531Ssam		if (ath->ath_capability & ATHEROS_CAP_XR)
2292170531Ssam			printf("XR,");
2293170531Ssam		if (ath->ath_capability & ATHEROS_CAP_AR)
2294170531Ssam			printf("AR,");
2295170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BURST)
2296170531Ssam			printf("BURST,");
2297170531Ssam		if (ath->ath_capability & ATHEROS_CAP_WME)
2298170531Ssam			printf("WME,");
2299170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BOOST)
2300170531Ssam			printf("BOOST,");
2301170531Ssam		printf("0x%x>", LE_READ_2(ath->ath_defkeyix));
2302170531Ssam	}
2303170531Ssam}
2304170531Ssam
2305170531Ssamstatic const char *
2306170531Ssamwpa_cipher(const u_int8_t *sel)
2307170531Ssam{
2308170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2309170531Ssam	u_int32_t w = LE_READ_4(sel);
2310170531Ssam
2311170531Ssam	switch (w) {
2312170531Ssam	case WPA_SEL(WPA_CSE_NULL):
2313170531Ssam		return "NONE";
2314170531Ssam	case WPA_SEL(WPA_CSE_WEP40):
2315170531Ssam		return "WEP40";
2316170531Ssam	case WPA_SEL(WPA_CSE_WEP104):
2317170531Ssam		return "WEP104";
2318170531Ssam	case WPA_SEL(WPA_CSE_TKIP):
2319170531Ssam		return "TKIP";
2320170531Ssam	case WPA_SEL(WPA_CSE_CCMP):
2321170531Ssam		return "AES-CCMP";
2322170531Ssam	}
2323170531Ssam	return "?";		/* NB: so 1<< is discarded */
2324170531Ssam#undef WPA_SEL
2325170531Ssam}
2326170531Ssam
2327170531Ssamstatic const char *
2328170531Ssamwpa_keymgmt(const u_int8_t *sel)
2329170531Ssam{
2330170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2331170531Ssam	u_int32_t w = LE_READ_4(sel);
2332170531Ssam
2333170531Ssam	switch (w) {
2334170531Ssam	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
2335170531Ssam		return "8021X-UNSPEC";
2336170531Ssam	case WPA_SEL(WPA_ASE_8021X_PSK):
2337170531Ssam		return "8021X-PSK";
2338170531Ssam	case WPA_SEL(WPA_ASE_NONE):
2339170531Ssam		return "NONE";
2340170531Ssam	}
2341170531Ssam	return "?";
2342170531Ssam#undef WPA_SEL
2343170531Ssam}
2344170531Ssam
2345170531Ssamstatic void
2346170531Ssamprintwpaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2347170531Ssam{
2348170531Ssam	u_int8_t len = ie[1];
2349170531Ssam
2350170531Ssam	printf("%s", tag);
2351170531Ssam	if (verbose) {
2352170531Ssam		const char *sep;
2353170531Ssam		int n;
2354170531Ssam
2355170531Ssam		ie += 6, len -= 4;		/* NB: len is payload only */
2356170531Ssam
2357170531Ssam		printf("<v%u", LE_READ_2(ie));
2358170531Ssam		ie += 2, len -= 2;
2359170531Ssam
2360170531Ssam		printf(" mc:%s", wpa_cipher(ie));
2361170531Ssam		ie += 4, len -= 4;
2362170531Ssam
2363170531Ssam		/* unicast ciphers */
2364170531Ssam		n = LE_READ_2(ie);
2365170531Ssam		ie += 2, len -= 2;
2366170531Ssam		sep = " uc:";
2367170531Ssam		for (; n > 0; n--) {
2368170531Ssam			printf("%s%s", sep, wpa_cipher(ie));
2369170531Ssam			ie += 4, len -= 4;
2370170531Ssam			sep = "+";
2371170531Ssam		}
2372170531Ssam
2373170531Ssam		/* key management algorithms */
2374170531Ssam		n = LE_READ_2(ie);
2375170531Ssam		ie += 2, len -= 2;
2376170531Ssam		sep = " km:";
2377170531Ssam		for (; n > 0; n--) {
2378170531Ssam			printf("%s%s", sep, wpa_keymgmt(ie));
2379170531Ssam			ie += 4, len -= 4;
2380170531Ssam			sep = "+";
2381170531Ssam		}
2382170531Ssam
2383170531Ssam		if (len > 2)		/* optional capabilities */
2384170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2385170531Ssam		printf(">");
2386170531Ssam	}
2387170531Ssam}
2388170531Ssam
2389170531Ssamstatic const char *
2390170531Ssamrsn_cipher(const u_int8_t *sel)
2391170531Ssam{
2392170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2393170531Ssam	u_int32_t w = LE_READ_4(sel);
2394170531Ssam
2395170531Ssam	switch (w) {
2396170531Ssam	case RSN_SEL(RSN_CSE_NULL):
2397170531Ssam		return "NONE";
2398170531Ssam	case RSN_SEL(RSN_CSE_WEP40):
2399170531Ssam		return "WEP40";
2400170531Ssam	case RSN_SEL(RSN_CSE_WEP104):
2401170531Ssam		return "WEP104";
2402170531Ssam	case RSN_SEL(RSN_CSE_TKIP):
2403170531Ssam		return "TKIP";
2404170531Ssam	case RSN_SEL(RSN_CSE_CCMP):
2405170531Ssam		return "AES-CCMP";
2406170531Ssam	case RSN_SEL(RSN_CSE_WRAP):
2407170531Ssam		return "AES-OCB";
2408170531Ssam	}
2409170531Ssam	return "?";
2410170531Ssam#undef WPA_SEL
2411170531Ssam}
2412170531Ssam
2413170531Ssamstatic const char *
2414170531Ssamrsn_keymgmt(const u_int8_t *sel)
2415170531Ssam{
2416170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2417170531Ssam	u_int32_t w = LE_READ_4(sel);
2418170531Ssam
2419170531Ssam	switch (w) {
2420170531Ssam	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
2421170531Ssam		return "8021X-UNSPEC";
2422170531Ssam	case RSN_SEL(RSN_ASE_8021X_PSK):
2423170531Ssam		return "8021X-PSK";
2424170531Ssam	case RSN_SEL(RSN_ASE_NONE):
2425170531Ssam		return "NONE";
2426170531Ssam	}
2427170531Ssam	return "?";
2428170531Ssam#undef RSN_SEL
2429170531Ssam}
2430170531Ssam
2431170531Ssamstatic void
2432170531Ssamprintrsnie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2433170531Ssam{
2434170531Ssam	printf("%s", tag);
2435170531Ssam	if (verbose) {
2436170531Ssam		const char *sep;
2437170531Ssam		int n;
2438170531Ssam
2439173275Ssam		ie += 2, ielen -= 2;
2440170531Ssam
2441170531Ssam		printf("<v%u", LE_READ_2(ie));
2442173275Ssam		ie += 2, ielen -= 2;
2443170531Ssam
2444170531Ssam		printf(" mc:%s", rsn_cipher(ie));
2445173275Ssam		ie += 4, ielen -= 4;
2446170531Ssam
2447170531Ssam		/* unicast ciphers */
2448170531Ssam		n = LE_READ_2(ie);
2449173275Ssam		ie += 2, ielen -= 2;
2450170531Ssam		sep = " uc:";
2451170531Ssam		for (; n > 0; n--) {
2452170531Ssam			printf("%s%s", sep, rsn_cipher(ie));
2453173275Ssam			ie += 4, ielen -= 4;
2454170531Ssam			sep = "+";
2455170531Ssam		}
2456170531Ssam
2457170531Ssam		/* key management algorithms */
2458170531Ssam		n = LE_READ_2(ie);
2459173275Ssam		ie += 2, ielen -= 2;
2460170531Ssam		sep = " km:";
2461170531Ssam		for (; n > 0; n--) {
2462170531Ssam			printf("%s%s", sep, rsn_keymgmt(ie));
2463173275Ssam			ie += 4, ielen -= 4;
2464170531Ssam			sep = "+";
2465170531Ssam		}
2466170531Ssam
2467173275Ssam		if (ielen > 2)		/* optional capabilities */
2468170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2469170531Ssam		/* XXXPMKID */
2470170531Ssam		printf(">");
2471170531Ssam	}
2472170531Ssam}
2473170531Ssam
2474181454Ssam/* XXX move to a public include file */
2475181454Ssam#define IEEE80211_WPS_DEV_PASS_ID	0x1012
2476181454Ssam#define IEEE80211_WPS_SELECTED_REG	0x1041
2477181454Ssam#define IEEE80211_WPS_SETUP_STATE	0x1044
2478181454Ssam#define IEEE80211_WPS_UUID_E		0x1047
2479181454Ssam#define IEEE80211_WPS_VERSION		0x104a
2480181454Ssam
2481181454Ssam#define BE_READ_2(p)					\
2482181454Ssam	((u_int16_t)					\
2483181454Ssam	 ((((const u_int8_t *)(p))[1]      ) |		\
2484181454Ssam	  (((const u_int8_t *)(p))[0] <<  8)))
2485181454Ssam
2486181454Ssamstatic void
2487181454Ssamprintwpsie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2488181454Ssam{
2489181454Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
2490181454Ssam	u_int8_t len = ie[1];
2491181454Ssam
2492181454Ssam	printf("%s", tag);
2493181454Ssam	if (verbose) {
2494181454Ssam		static const char *dev_pass_id[] = {
2495181454Ssam			"D",	/* Default (PIN) */
2496181454Ssam			"U",	/* User-specified */
2497181454Ssam			"M",	/* Machine-specified */
2498181454Ssam			"K",	/* Rekey */
2499181454Ssam			"P",	/* PushButton */
2500181454Ssam			"R"	/* Registrar-specified */
2501181454Ssam		};
2502181454Ssam		int n;
2503181454Ssam
2504181454Ssam		ie +=6, len -= 4;		/* NB: len is payload only */
2505181454Ssam
2506181454Ssam		/* WPS IE in Beacon and Probe Resp frames have different fields */
2507181454Ssam		printf("<");
2508181454Ssam		while (len) {
2509181454Ssam			uint16_t tlv_type = BE_READ_2(ie);
2510181454Ssam			uint16_t tlv_len  = BE_READ_2(ie + 2);
2511181454Ssam
2512181454Ssam			ie += 4, len -= 4;
2513181454Ssam
2514181454Ssam			switch (tlv_type) {
2515181454Ssam			case IEEE80211_WPS_VERSION:
2516181454Ssam				printf("v:%d.%d", *ie >> 4, *ie & 0xf);
2517181454Ssam				break;
2518181454Ssam			case IEEE80211_WPS_SETUP_STATE:
2519181454Ssam				/* Only 1 and 2 are valid */
2520181454Ssam				if (*ie == 0 || *ie >= 3)
2521181454Ssam					printf(" state:B");
2522181454Ssam				else
2523181454Ssam					printf(" st:%s", *ie == 1 ? "N" : "C");
2524181454Ssam				break;
2525181454Ssam			case IEEE80211_WPS_SELECTED_REG:
2526181454Ssam				printf(" sel:%s", *ie ? "T" : "F");
2527181454Ssam				break;
2528181454Ssam			case IEEE80211_WPS_DEV_PASS_ID:
2529181454Ssam				n = LE_READ_2(ie);
2530181454Ssam				if (n < N(dev_pass_id))
2531181454Ssam					printf(" dpi:%s", dev_pass_id[n]);
2532181454Ssam				break;
2533181454Ssam			case IEEE80211_WPS_UUID_E:
2534181454Ssam				printf(" uuid-e:");
2535181454Ssam				for (n = 0; n < (tlv_len - 1); n++)
2536181454Ssam					printf("%02x-", ie[n]);
2537181454Ssam				printf("%02x", ie[n]);
2538181454Ssam				break;
2539181454Ssam			}
2540181454Ssam			ie += tlv_len, len -= tlv_len;
2541181454Ssam		}
2542181454Ssam		printf(">");
2543181454Ssam	}
2544181454Ssam#undef N
2545181454Ssam}
2546181454Ssam
2547170531Ssam/*
2548138593Ssam * Copy the ssid string contents into buf, truncating to fit.  If the
2549138593Ssam * ssid is entirely printable then just copy intact.  Otherwise convert
2550138593Ssam * to hexadecimal.  If the result is truncated then replace the last
2551138593Ssam * three characters with "...".
2552138593Ssam */
2553146873Sjhbstatic int
2554138593Ssamcopy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
2555138593Ssam{
2556138593Ssam	const u_int8_t *p;
2557138593Ssam	size_t maxlen;
2558138593Ssam	int i;
2559138593Ssam
2560138593Ssam	if (essid_len > bufsize)
2561138593Ssam		maxlen = bufsize;
2562138593Ssam	else
2563138593Ssam		maxlen = essid_len;
2564138593Ssam	/* determine printable or not */
2565138593Ssam	for (i = 0, p = essid; i < maxlen; i++, p++) {
2566138593Ssam		if (*p < ' ' || *p > 0x7e)
2567138593Ssam			break;
2568138593Ssam	}
2569138593Ssam	if (i != maxlen) {		/* not printable, print as hex */
2570138593Ssam		if (bufsize < 3)
2571138593Ssam			return 0;
2572138593Ssam		strlcpy(buf, "0x", bufsize);
2573138593Ssam		bufsize -= 2;
2574138593Ssam		p = essid;
2575138593Ssam		for (i = 0; i < maxlen && bufsize >= 2; i++) {
2576147489Savatar			sprintf(&buf[2+2*i], "%02x", p[i]);
2577138593Ssam			bufsize -= 2;
2578138593Ssam		}
2579147489Savatar		if (i != essid_len)
2580147489Savatar			memcpy(&buf[2+2*i-3], "...", 3);
2581138593Ssam	} else {			/* printable, truncate as needed */
2582138593Ssam		memcpy(buf, essid, maxlen);
2583147489Savatar		if (maxlen != essid_len)
2584147489Savatar			memcpy(&buf[maxlen-3], "...", 3);
2585138593Ssam	}
2586138593Ssam	return maxlen;
2587138593Ssam}
2588138593Ssam
2589173275Ssamstatic void
2590173275Ssamprintssid(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2591173275Ssam{
2592173275Ssam	char ssid[2*IEEE80211_NWID_LEN+1];
2593173275Ssam
2594173275Ssam	printf("%s<%.*s>", tag, copy_essid(ssid, maxlen, ie+2, ie[1]), ssid);
2595173275Ssam}
2596173275Ssam
2597173275Ssamstatic void
2598173275Ssamprintrates(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2599173275Ssam{
2600173275Ssam	const char *sep;
2601173275Ssam	int i;
2602173275Ssam
2603173275Ssam	printf("%s", tag);
2604173275Ssam	sep = "<";
2605173275Ssam	for (i = 2; i < ielen; i++) {
2606173275Ssam		printf("%s%s%d", sep,
2607173275Ssam		    ie[i] & IEEE80211_RATE_BASIC ? "B" : "",
2608173275Ssam		    ie[i] & IEEE80211_RATE_VAL);
2609173275Ssam		sep = ",";
2610173275Ssam	}
2611173275Ssam	printf(">");
2612173275Ssam}
2613173275Ssam
2614173275Ssamstatic void
2615173275Ssamprintcountry(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2616173275Ssam{
2617173275Ssam	const struct ieee80211_country_ie *cie =
2618173275Ssam	   (const struct ieee80211_country_ie *) ie;
2619173275Ssam	int i, nbands, schan, nchan;
2620173275Ssam
2621173275Ssam	printf("%s<%c%c%c", tag, cie->cc[0], cie->cc[1], cie->cc[2]);
2622173275Ssam	nbands = (cie->len - 3) / sizeof(cie->band[0]);
2623173275Ssam	for (i = 0; i < nbands; i++) {
2624173275Ssam		schan = cie->band[i].schan;
2625173275Ssam		nchan = cie->band[i].nchan;
2626173275Ssam		if (nchan != 1)
2627173275Ssam			printf(" %u-%u,%u", schan, schan + nchan-1,
2628173275Ssam			    cie->band[i].maxtxpwr);
2629173275Ssam		else
2630173275Ssam			printf(" %u,%u", schan, cie->band[i].maxtxpwr);
2631173275Ssam	}
2632173275Ssam	printf(">");
2633173275Ssam}
2634173275Ssam
2635148686Sstefanf/* unaligned little endian access */
2636138593Ssam#define LE_READ_4(p)					\
2637138593Ssam	((u_int32_t)					\
2638138593Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2639138593Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
2640138593Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
2641138593Ssam	  (((const u_int8_t *)(p))[3] << 24)))
2642138593Ssam
2643178354Ssamstatic __inline int
2644138593Ssamiswpaoui(const u_int8_t *frm)
2645138593Ssam{
2646138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
2647138593Ssam}
2648138593Ssam
2649178354Ssamstatic __inline int
2650173275Ssamiswmeinfo(const u_int8_t *frm)
2651138593Ssam{
2652173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
2653173275Ssam		frm[6] == WME_INFO_OUI_SUBTYPE;
2654138593Ssam}
2655138593Ssam
2656178354Ssamstatic __inline int
2657173275Ssamiswmeparam(const u_int8_t *frm)
2658173275Ssam{
2659173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
2660173275Ssam		frm[6] == WME_PARAM_OUI_SUBTYPE;
2661173275Ssam}
2662173275Ssam
2663178354Ssamstatic __inline int
2664138593Ssamisatherosoui(const u_int8_t *frm)
2665138593Ssam{
2666138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
2667138593Ssam}
2668138593Ssam
2669181454Ssamstatic __inline int
2670181454Ssamiswpsoui(const uint8_t *frm)
2671181454Ssam{
2672181454Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPS_OUI_TYPE<<24)|WPA_OUI);
2673181454Ssam}
2674181454Ssam
2675173275Ssamstatic const char *
2676173275Ssamiename(int elemid)
2677173275Ssam{
2678173275Ssam	switch (elemid) {
2679173275Ssam	case IEEE80211_ELEMID_FHPARMS:	return " FHPARMS";
2680173275Ssam	case IEEE80211_ELEMID_CFPARMS:	return " CFPARMS";
2681173275Ssam	case IEEE80211_ELEMID_TIM:	return " TIM";
2682173275Ssam	case IEEE80211_ELEMID_IBSSPARMS:return " IBSSPARMS";
2683173275Ssam	case IEEE80211_ELEMID_CHALLENGE:return " CHALLENGE";
2684173275Ssam	case IEEE80211_ELEMID_PWRCNSTR:	return " PWRCNSTR";
2685173275Ssam	case IEEE80211_ELEMID_PWRCAP:	return " PWRCAP";
2686173275Ssam	case IEEE80211_ELEMID_TPCREQ:	return " TPCREQ";
2687173275Ssam	case IEEE80211_ELEMID_TPCREP:	return " TPCREP";
2688173275Ssam	case IEEE80211_ELEMID_SUPPCHAN:	return " SUPPCHAN";
2689173275Ssam	case IEEE80211_ELEMID_CHANSWITCHANN:return " CSA";
2690173275Ssam	case IEEE80211_ELEMID_MEASREQ:	return " MEASREQ";
2691173275Ssam	case IEEE80211_ELEMID_MEASREP:	return " MEASREP";
2692173275Ssam	case IEEE80211_ELEMID_QUIET:	return " QUIET";
2693173275Ssam	case IEEE80211_ELEMID_IBSSDFS:	return " IBSSDFS";
2694173275Ssam	case IEEE80211_ELEMID_TPC:	return " TPC";
2695173275Ssam	case IEEE80211_ELEMID_CCKM:	return " CCKM";
2696173275Ssam	}
2697173275Ssam	return " ???";
2698173275Ssam}
2699173275Ssam
2700138593Ssamstatic void
2701138593Ssamprinties(const u_int8_t *vp, int ielen, int maxcols)
2702138593Ssam{
2703138593Ssam	while (ielen > 0) {
2704138593Ssam		switch (vp[0]) {
2705173275Ssam		case IEEE80211_ELEMID_SSID:
2706173275Ssam			if (verbose)
2707173275Ssam				printssid(" SSID", vp, 2+vp[1], maxcols);
2708173275Ssam			break;
2709173275Ssam		case IEEE80211_ELEMID_RATES:
2710173275Ssam		case IEEE80211_ELEMID_XRATES:
2711173275Ssam			if (verbose)
2712173275Ssam				printrates(vp[0] == IEEE80211_ELEMID_RATES ?
2713173275Ssam				    " RATES" : " XRATES", vp, 2+vp[1], maxcols);
2714173275Ssam			break;
2715173275Ssam		case IEEE80211_ELEMID_DSPARMS:
2716173275Ssam			if (verbose)
2717173275Ssam				printf(" DSPARMS<%u>", vp[2]);
2718173275Ssam			break;
2719173275Ssam		case IEEE80211_ELEMID_COUNTRY:
2720173275Ssam			if (verbose)
2721173275Ssam				printcountry(" COUNTRY", vp, 2+vp[1], maxcols);
2722173275Ssam			break;
2723173275Ssam		case IEEE80211_ELEMID_ERP:
2724173275Ssam			if (verbose)
2725173275Ssam				printf(" ERP<0x%x>", vp[2]);
2726173275Ssam			break;
2727138593Ssam		case IEEE80211_ELEMID_VENDOR:
2728138593Ssam			if (iswpaoui(vp))
2729170531Ssam				printwpaie(" WPA", vp, 2+vp[1], maxcols);
2730173275Ssam			else if (iswmeinfo(vp))
2731173275Ssam				printwmeinfo(" WME", vp, 2+vp[1], maxcols);
2732173275Ssam			else if (iswmeparam(vp))
2733173275Ssam				printwmeparam(" WME", vp, 2+vp[1], maxcols);
2734139492Ssam			else if (isatherosoui(vp))
2735170531Ssam				printathie(" ATH", vp, 2+vp[1], maxcols);
2736181454Ssam			else if (iswpsoui(vp))
2737181454Ssam				printwpsie(" WPS", vp, 2+vp[1], maxcols);
2738173275Ssam			else if (verbose)
2739138593Ssam				printie(" VEN", vp, 2+vp[1], maxcols);
2740138593Ssam			break;
2741138593Ssam		case IEEE80211_ELEMID_RSN:
2742170531Ssam			printrsnie(" RSN", vp, 2+vp[1], maxcols);
2743138593Ssam			break;
2744173275Ssam		case IEEE80211_ELEMID_HTCAP:
2745173275Ssam			printhtcap(" HTCAP", vp, 2+vp[1], maxcols);
2746173275Ssam			break;
2747173275Ssam		case IEEE80211_ELEMID_HTINFO:
2748173275Ssam			if (verbose)
2749173275Ssam				printhtinfo(" HTINFO", vp, 2+vp[1], maxcols);
2750173275Ssam			break;
2751138593Ssam		default:
2752173275Ssam			if (verbose)
2753173275Ssam				printie(iename(vp[0]), vp, 2+vp[1], maxcols);
2754138593Ssam			break;
2755138593Ssam		}
2756138593Ssam		ielen -= 2+vp[1];
2757138593Ssam		vp += 2+vp[1];
2758138593Ssam	}
2759138593Ssam}
2760138593Ssam
2761138593Ssamstatic void
2762178354Ssamprintmimo(const struct ieee80211_mimo_info *mi)
2763178354Ssam{
2764178354Ssam	/* NB: don't muddy display unless there's something to show */
2765178354Ssam	if (mi->rssi[0] != 0 || mi->rssi[1] != 0 || mi->rssi[2] != 0) {
2766178354Ssam		/* XXX ignore EVM for now */
2767178354Ssam		printf(" (rssi %d:%d:%d nf %d:%d:%d)",
2768178354Ssam		    mi->rssi[0], mi->rssi[1], mi->rssi[2],
2769178354Ssam		    mi->noise[0], mi->noise[1], mi->noise[2]);
2770178354Ssam	}
2771178354Ssam}
2772178354Ssam
2773178354Ssamstatic void
2774138593Ssamlist_scan(int s)
2775138593Ssam{
2776138593Ssam	uint8_t buf[24*1024];
2777153892Srwatson	char ssid[IEEE80211_NWID_LEN+1];
2778173275Ssam	const uint8_t *cp;
2779154522Ssam	int len, ssidmax;
2780138593Ssam
2781173275Ssam	if (get80211len(s, IEEE80211_IOC_SCAN_RESULTS, buf, sizeof(buf), &len) < 0)
2782138593Ssam		errx(1, "unable to get scan results");
2783138593Ssam	if (len < sizeof(struct ieee80211req_scan_result))
2784138593Ssam		return;
2785138593Ssam
2786170531Ssam	getchaninfo(s);
2787170531Ssam
2788154522Ssam	ssidmax = verbose ? IEEE80211_NWID_LEN : 14;
2789170531Ssam	printf("%-*.*s  %-17.17s  %4s %4s  %-7s  %3s %4s\n"
2790154522Ssam		, ssidmax, ssidmax, "SSID"
2791138593Ssam		, "BSSID"
2792138593Ssam		, "CHAN"
2793138593Ssam		, "RATE"
2794170531Ssam		, " S:N"
2795138593Ssam		, "INT"
2796138593Ssam		, "CAPS"
2797138593Ssam	);
2798138593Ssam	cp = buf;
2799138593Ssam	do {
2800170531Ssam		const struct ieee80211req_scan_result *sr;
2801170531Ssam		const uint8_t *vp;
2802138593Ssam
2803170531Ssam		sr = (const struct ieee80211req_scan_result *) cp;
2804173275Ssam		vp = cp + sr->isr_ie_off;
2805170531Ssam		printf("%-*.*s  %s  %3d  %3dM %3d:%-3d  %3d %-4.4s"
2806154522Ssam			, ssidmax
2807155461Ssam			  , copy_essid(ssid, ssidmax, vp, sr->isr_ssid_len)
2808154522Ssam			  , ssid
2809138593Ssam			, ether_ntoa((const struct ether_addr *) sr->isr_bssid)
2810165570Ssam			, ieee80211_mhz2ieee(sr->isr_freq, sr->isr_flags)
2811138593Ssam			, getmaxrate(sr->isr_rates, sr->isr_nrates)
2812170531Ssam			, (sr->isr_rssi/2)+sr->isr_noise, sr->isr_noise
2813138593Ssam			, sr->isr_intval
2814138593Ssam			, getcaps(sr->isr_capinfo)
2815138593Ssam		);
2816170531Ssam		printies(vp + sr->isr_ssid_len, sr->isr_ie_len, 24);
2817138593Ssam		printf("\n");
2818138593Ssam		cp += sr->isr_len, len -= sr->isr_len;
2819138593Ssam	} while (len >= sizeof(struct ieee80211req_scan_result));
2820138593Ssam}
2821138593Ssam
2822178354Ssam#ifdef __FreeBSD__
2823138593Ssam#include <net80211/ieee80211_freebsd.h>
2824178354Ssam#endif
2825178354Ssam#ifdef __NetBSD__
2826178354Ssam#include <net80211/ieee80211_netbsd.h>
2827178354Ssam#endif
2828138593Ssam
2829138593Ssamstatic void
2830138593Ssamscan_and_wait(int s)
2831138593Ssam{
2832178354Ssam	struct ieee80211_scan_req sr;
2833138593Ssam	struct ieee80211req ireq;
2834138593Ssam	int sroute;
2835138593Ssam
2836138593Ssam	sroute = socket(PF_ROUTE, SOCK_RAW, 0);
2837138593Ssam	if (sroute < 0) {
2838138593Ssam		perror("socket(PF_ROUTE,SOCK_RAW)");
2839138593Ssam		return;
2840138593Ssam	}
2841138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
2842138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
2843138593Ssam	ireq.i_type = IEEE80211_IOC_SCAN_REQ;
2844178354Ssam
2845178354Ssam	memset(&sr, 0, sizeof(sr));
2846178354Ssam	sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE
2847178354Ssam		    | IEEE80211_IOC_SCAN_NOPICK
2848178354Ssam		    | IEEE80211_IOC_SCAN_ONCE;
2849178354Ssam	sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
2850178354Ssam	sr.sr_nssid = 0;
2851178354Ssam
2852178354Ssam	ireq.i_data = &sr;
2853178354Ssam	ireq.i_len = sizeof(sr);
2854138593Ssam	/* NB: only root can trigger a scan so ignore errors */
2855138593Ssam	if (ioctl(s, SIOCS80211, &ireq) >= 0) {
2856138593Ssam		char buf[2048];
2857138593Ssam		struct if_announcemsghdr *ifan;
2858138593Ssam		struct rt_msghdr *rtm;
2859138593Ssam
2860138593Ssam		do {
2861138593Ssam			if (read(sroute, buf, sizeof(buf)) < 0) {
2862138593Ssam				perror("read(PF_ROUTE)");
2863138593Ssam				break;
2864138593Ssam			}
2865138593Ssam			rtm = (struct rt_msghdr *) buf;
2866138593Ssam			if (rtm->rtm_version != RTM_VERSION)
2867138593Ssam				break;
2868138593Ssam			ifan = (struct if_announcemsghdr *) rtm;
2869138593Ssam		} while (rtm->rtm_type != RTM_IEEE80211 ||
2870138593Ssam		    ifan->ifan_what != RTM_IEEE80211_SCAN);
2871138593Ssam	}
2872138593Ssam	close(sroute);
2873138593Ssam}
2874138593Ssam
2875138593Ssamstatic
2876138593SsamDECL_CMD_FUNC(set80211scan, val, d)
2877138593Ssam{
2878138593Ssam	scan_and_wait(s);
2879138593Ssam	list_scan(s);
2880138593Ssam}
2881138593Ssam
2882161147Ssamstatic enum ieee80211_opmode get80211opmode(int s);
2883161147Ssam
2884173275Ssamstatic int
2885173275Ssamgettxseq(const struct ieee80211req_sta_info *si)
2886173275Ssam{
2887173275Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
2888173275Ssam
2889173275Ssam	int i, txseq;
2890173275Ssam
2891173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
2892173275Ssam		return si->isi_txseqs[0];
2893173275Ssam	/* XXX not right but usually what folks want */
2894173275Ssam	txseq = 0;
2895173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
2896173275Ssam		if (si->isi_txseqs[i] > txseq)
2897173275Ssam			txseq = si->isi_txseqs[i];
2898173275Ssam	return txseq;
2899173275Ssam#undef IEEE80211_NODE_QOS
2900173275Ssam}
2901173275Ssam
2902173275Ssamstatic int
2903173275Ssamgetrxseq(const struct ieee80211req_sta_info *si)
2904173275Ssam{
2905173275Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
2906173275Ssam
2907173275Ssam	int i, rxseq;
2908173275Ssam
2909173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
2910173275Ssam		return si->isi_rxseqs[0];
2911173275Ssam	/* XXX not right but usually what folks want */
2912173275Ssam	rxseq = 0;
2913173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
2914173275Ssam		if (si->isi_rxseqs[i] > rxseq)
2915173275Ssam			rxseq = si->isi_rxseqs[i];
2916173275Ssam	return rxseq;
2917173275Ssam#undef IEEE80211_NODE_QOS
2918173275Ssam}
2919173275Ssam
2920138593Ssamstatic void
2921138593Ssamlist_stations(int s)
2922138593Ssam{
2923161147Ssam	union {
2924161147Ssam		struct ieee80211req_sta_req req;
2925161147Ssam		uint8_t buf[24*1024];
2926161147Ssam	} u;
2927161147Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
2928170531Ssam	const uint8_t *cp;
2929138593Ssam	int len;
2930138593Ssam
2931161147Ssam	/* broadcast address =>'s get all stations */
2932161147Ssam	(void) memset(u.req.is_u.macaddr, 0xff, IEEE80211_ADDR_LEN);
2933161147Ssam	if (opmode == IEEE80211_M_STA) {
2934161147Ssam		/*
2935161147Ssam		 * Get information about the associated AP.
2936161147Ssam		 */
2937173275Ssam		(void) get80211(s, IEEE80211_IOC_BSSID,
2938173275Ssam		    u.req.is_u.macaddr, IEEE80211_ADDR_LEN);
2939161147Ssam	}
2940173275Ssam	if (get80211len(s, IEEE80211_IOC_STA_INFO, &u, sizeof(u), &len) < 0)
2941138593Ssam		errx(1, "unable to get station information");
2942138593Ssam	if (len < sizeof(struct ieee80211req_sta_info))
2943138593Ssam		return;
2944138593Ssam
2945170531Ssam	getchaninfo(s);
2946170531Ssam
2947159885Ssam	printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %4s\n"
2948138593Ssam		, "ADDR"
2949138593Ssam		, "AID"
2950138593Ssam		, "CHAN"
2951138593Ssam		, "RATE"
2952138593Ssam		, "RSSI"
2953138593Ssam		, "IDLE"
2954138593Ssam		, "TXSEQ"
2955138593Ssam		, "RXSEQ"
2956138593Ssam		, "CAPS"
2957159885Ssam		, "FLAG"
2958138593Ssam	);
2959170531Ssam	cp = (const uint8_t *) u.req.info;
2960138593Ssam	do {
2961170531Ssam		const struct ieee80211req_sta_info *si;
2962138593Ssam
2963170531Ssam		si = (const struct ieee80211req_sta_info *) cp;
2964161147Ssam		if (si->isi_len < sizeof(*si))
2965161147Ssam			break;
2966170531Ssam		printf("%s %4u %4d %3dM %3.1f %4d %6d %6d %-4.4s %-4.4s"
2967138593Ssam			, ether_ntoa((const struct ether_addr*) si->isi_macaddr)
2968138593Ssam			, IEEE80211_AID(si->isi_associd)
2969166015Ssam			, ieee80211_mhz2ieee(si->isi_freq, si->isi_flags)
2970178354Ssam			, si->isi_txmbps/2
2971170531Ssam			, si->isi_rssi/2.
2972138593Ssam			, si->isi_inact
2973173275Ssam			, gettxseq(si)
2974173275Ssam			, getrxseq(si)
2975138593Ssam			, getcaps(si->isi_capinfo)
2976159885Ssam			, getflags(si->isi_state)
2977138593Ssam		);
2978170531Ssam		printies(cp + si->isi_ie_off, si->isi_ie_len, 24);
2979178354Ssam		printmimo(&si->isi_mimo);
2980138593Ssam		printf("\n");
2981138593Ssam		cp += si->isi_len, len -= si->isi_len;
2982138593Ssam	} while (len >= sizeof(struct ieee80211req_sta_info));
2983138593Ssam}
2984138593Ssam
2985170531Ssamstatic const char *
2986170531Ssamget_chaninfo(const struct ieee80211_channel *c, int precise,
2987170531Ssam	char buf[], size_t bsize)
2988138593Ssam{
2989138593Ssam	buf[0] = '\0';
2990138593Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
2991170531Ssam		strlcat(buf, " FHSS", bsize);
2992165570Ssam	if (IEEE80211_IS_CHAN_A(c)) {
2993165570Ssam		if (IEEE80211_IS_CHAN_HALF(c))
2994170531Ssam			strlcat(buf, " 11a/10Mhz", bsize);
2995165570Ssam		else if (IEEE80211_IS_CHAN_QUARTER(c))
2996170531Ssam			strlcat(buf, " 11a/5Mhz", bsize);
2997165570Ssam		else
2998170531Ssam			strlcat(buf, " 11a", bsize);
2999165570Ssam	}
3000166015Ssam	if (IEEE80211_IS_CHAN_ANYG(c)) {
3001166015Ssam		if (IEEE80211_IS_CHAN_HALF(c))
3002170531Ssam			strlcat(buf, " 11g/10Mhz", bsize);
3003166015Ssam		else if (IEEE80211_IS_CHAN_QUARTER(c))
3004170531Ssam			strlcat(buf, " 11g/5Mhz", bsize);
3005166015Ssam		else
3006170531Ssam			strlcat(buf, " 11g", bsize);
3007166015Ssam	} else if (IEEE80211_IS_CHAN_B(c))
3008170531Ssam		strlcat(buf, " 11b", bsize);
3009170531Ssam	if (IEEE80211_IS_CHAN_TURBO(c))
3010170531Ssam		strlcat(buf, " Turbo", bsize);
3011170531Ssam	if (precise) {
3012170531Ssam		if (IEEE80211_IS_CHAN_HT20(c))
3013170531Ssam			strlcat(buf, " ht/20", bsize);
3014170531Ssam		else if (IEEE80211_IS_CHAN_HT40D(c))
3015170531Ssam			strlcat(buf, " ht/40-", bsize);
3016170531Ssam		else if (IEEE80211_IS_CHAN_HT40U(c))
3017170531Ssam			strlcat(buf, " ht/40+", bsize);
3018170531Ssam	} else {
3019170531Ssam		if (IEEE80211_IS_CHAN_HT(c))
3020170531Ssam			strlcat(buf, " ht", bsize);
3021170531Ssam	}
3022170531Ssam	return buf;
3023170531Ssam}
3024170531Ssam
3025170531Ssamstatic void
3026173275Ssamprint_chaninfo(const struct ieee80211_channel *c, int verb)
3027170531Ssam{
3028170531Ssam	char buf[14];
3029170531Ssam
3030138593Ssam	printf("Channel %3u : %u%c Mhz%-14.14s",
3031165570Ssam		ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
3032170531Ssam		IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
3033173275Ssam		get_chaninfo(c, verb, buf, sizeof(buf)));
3034138593Ssam}
3035138593Ssam
3036138593Ssamstatic void
3037173275Ssamprint_channels(int s, const struct ieee80211req_chaninfo *chans,
3038173275Ssam	int allchans, int verb)
3039138593Ssam{
3040138593Ssam	struct ieee80211req_chaninfo achans;
3041170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3042138593Ssam	const struct ieee80211_channel *c;
3043170531Ssam	int i, half;
3044138593Ssam
3045170531Ssam	memset(&achans, 0, sizeof(achans));
3046170531Ssam	memset(reported, 0, sizeof(reported));
3047138593Ssam	if (!allchans) {
3048138593Ssam		struct ieee80211req_chanlist active;
3049138593Ssam
3050173275Ssam		if (get80211(s, IEEE80211_IOC_CHANLIST, &active, sizeof(active)) < 0)
3051138593Ssam			errx(1, "unable to get active channel list");
3052138593Ssam		memset(&achans, 0, sizeof(achans));
3053173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3054173275Ssam			c = &chans->ic_chans[i];
3055170531Ssam			if (!isset(active.ic_channels, c->ic_ieee))
3056170531Ssam				continue;
3057170531Ssam			/*
3058170531Ssam			 * Suppress compatible duplicates unless
3059170531Ssam			 * verbose.  The kernel gives us it's
3060170531Ssam			 * complete channel list which has separate
3061170531Ssam			 * entries for 11g/11b and 11a/turbo.
3062170531Ssam			 */
3063173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3064170531Ssam				/* XXX we assume duplicates are adjacent */
3065170531Ssam				achans.ic_chans[achans.ic_nchans-1] = *c;
3066170531Ssam			} else {
3067138593Ssam				achans.ic_chans[achans.ic_nchans++] = *c;
3068170531Ssam				setbit(reported, c->ic_ieee);
3069170531Ssam			}
3070138593Ssam		}
3071170531Ssam	} else {
3072173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3073173275Ssam			c = &chans->ic_chans[i];
3074170531Ssam			/* suppress duplicates as above */
3075173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3076170531Ssam				/* XXX we assume duplicates are adjacent */
3077170531Ssam				achans.ic_chans[achans.ic_nchans-1] = *c;
3078170531Ssam			} else {
3079170531Ssam				achans.ic_chans[achans.ic_nchans++] = *c;
3080170531Ssam				setbit(reported, c->ic_ieee);
3081170531Ssam			}
3082170531Ssam		}
3083170531Ssam	}
3084138593Ssam	half = achans.ic_nchans / 2;
3085138593Ssam	if (achans.ic_nchans % 2)
3086138593Ssam		half++;
3087170531Ssam
3088138593Ssam	for (i = 0; i < achans.ic_nchans / 2; i++) {
3089173275Ssam		print_chaninfo(&achans.ic_chans[i], verb);
3090173275Ssam		print_chaninfo(&achans.ic_chans[half+i], verb);
3091138593Ssam		printf("\n");
3092138593Ssam	}
3093138593Ssam	if (achans.ic_nchans % 2) {
3094173275Ssam		print_chaninfo(&achans.ic_chans[i], verb);
3095138593Ssam		printf("\n");
3096138593Ssam	}
3097138593Ssam}
3098138593Ssam
3099138593Ssamstatic void
3100173275Ssamlist_channels(int s, int allchans)
3101173275Ssam{
3102173275Ssam	getchaninfo(s);
3103173275Ssam	print_channels(s, &chaninfo, allchans, verbose);
3104173275Ssam}
3105173275Ssam
3106173275Ssamstatic void
3107170531Ssamprint_txpow(const struct ieee80211_channel *c)
3108170531Ssam{
3109170531Ssam	printf("Channel %3u : %u Mhz %3.1f reg %2d  ",
3110170531Ssam	    c->ic_ieee, c->ic_freq,
3111170531Ssam	    c->ic_maxpower/2., c->ic_maxregpower);
3112170531Ssam}
3113170531Ssam
3114170531Ssamstatic void
3115170531Ssamprint_txpow_verbose(const struct ieee80211_channel *c)
3116170531Ssam{
3117173275Ssam	print_chaninfo(c, 1);
3118170531Ssam	printf("min %4.1f dBm  max %3.1f dBm  reg %2d dBm",
3119170531Ssam	    c->ic_minpower/2., c->ic_maxpower/2., c->ic_maxregpower);
3120170531Ssam	/* indicate where regulatory cap limits power use */
3121170531Ssam	if (c->ic_maxpower > 2*c->ic_maxregpower)
3122170531Ssam		printf(" <");
3123170531Ssam}
3124170531Ssam
3125170531Ssamstatic void
3126170531Ssamlist_txpow(int s)
3127170531Ssam{
3128170531Ssam	struct ieee80211req_chaninfo achans;
3129170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3130170531Ssam	struct ieee80211_channel *c, *prev;
3131170531Ssam	int i, half;
3132170531Ssam
3133170531Ssam	getchaninfo(s);
3134170531Ssam	memset(&achans, 0, sizeof(achans));
3135170531Ssam	memset(reported, 0, sizeof(reported));
3136170531Ssam	for (i = 0; i < chaninfo.ic_nchans; i++) {
3137170531Ssam		c = &chaninfo.ic_chans[i];
3138170531Ssam		/* suppress duplicates as above */
3139170531Ssam		if (isset(reported, c->ic_ieee) && !verbose) {
3140170531Ssam			/* XXX we assume duplicates are adjacent */
3141170531Ssam			prev = &achans.ic_chans[achans.ic_nchans-1];
3142170531Ssam			/* display highest power on channel */
3143170531Ssam			if (c->ic_maxpower > prev->ic_maxpower)
3144170531Ssam				*prev = *c;
3145170531Ssam		} else {
3146170531Ssam			achans.ic_chans[achans.ic_nchans++] = *c;
3147170531Ssam			setbit(reported, c->ic_ieee);
3148170531Ssam		}
3149170531Ssam	}
3150170531Ssam	if (!verbose) {
3151170531Ssam		half = achans.ic_nchans / 2;
3152170531Ssam		if (achans.ic_nchans % 2)
3153170531Ssam			half++;
3154170531Ssam
3155170531Ssam		for (i = 0; i < achans.ic_nchans / 2; i++) {
3156170531Ssam			print_txpow(&achans.ic_chans[i]);
3157170531Ssam			print_txpow(&achans.ic_chans[half+i]);
3158170531Ssam			printf("\n");
3159170531Ssam		}
3160170531Ssam		if (achans.ic_nchans % 2) {
3161170531Ssam			print_txpow(&achans.ic_chans[i]);
3162170531Ssam			printf("\n");
3163170531Ssam		}
3164170531Ssam	} else {
3165170531Ssam		for (i = 0; i < achans.ic_nchans; i++) {
3166170531Ssam			print_txpow_verbose(&achans.ic_chans[i]);
3167170531Ssam			printf("\n");
3168170531Ssam		}
3169170531Ssam	}
3170170531Ssam}
3171170531Ssam
3172170531Ssamstatic void
3173138593Ssamlist_keys(int s)
3174138593Ssam{
3175138593Ssam}
3176138593Ssam
3177138593Ssam#define	IEEE80211_C_BITS \
3178181102Ssam	"\20\1STA\7FF\10TURBOP\11IBSS\12PMGT" \
3179178354Ssam	"\13HOSTAP\14AHDEMO\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE" \
3180181102Ssam	"\21MONITOR\22DFS\30WPA1\31WPA2\32BURST\33WME\34WDS\36BGSCAN" \
3181178354Ssam	"\37TXFRAG"
3182138593Ssam
3183178354Ssam#define	IEEE80211_CRYPTO_BITS \
3184178354Ssam	"\20\1WEP\2TKIP\3AES\4AES_CCM\5TKIPMIC\6CKIP\12PMGT"
3185178354Ssam
3186178354Ssam#define	IEEE80211_HTCAP_BITS \
3187178354Ssam	"\20\1LDPC\2CHWIDTH40\5GREENFIELD\6SHORTGI20\7SHORTGI40\10TXSTBC" \
3188178354Ssam	"\21AMPDU\22AMSDU\23HT"
3189178354Ssam
3190138593Ssamstatic void
3191138593Ssamlist_capabilities(int s)
3192138593Ssam{
3193178354Ssam	struct ieee80211_devcaps_req dc;
3194138593Ssam
3195178354Ssam	getdevcaps(s, &dc);
3196178354Ssam	printb("drivercaps", dc.dc_drivercaps, IEEE80211_C_BITS);
3197178354Ssam	if (dc.dc_cryptocaps != 0 || verbose) {
3198178354Ssam		putchar('\n');
3199178354Ssam		printb("cryptocaps", dc.dc_cryptocaps, IEEE80211_CRYPTO_BITS);
3200178354Ssam	}
3201178354Ssam	if (dc.dc_htcaps != 0 || verbose) {
3202178354Ssam		putchar('\n');
3203178354Ssam		printb("htcaps", dc.dc_htcaps, IEEE80211_HTCAP_BITS);
3204178354Ssam	}
3205138593Ssam	putchar('\n');
3206138593Ssam}
3207138593Ssam
3208173275Ssamstatic int
3209173275Ssamget80211wme(int s, int param, int ac, int *val)
3210173275Ssam{
3211173275Ssam	struct ieee80211req ireq;
3212173275Ssam
3213173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3214173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3215173275Ssam	ireq.i_type = param;
3216173275Ssam	ireq.i_len = ac;
3217173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3218173275Ssam		warn("cannot get WME parameter %d, ac %d%s",
3219173275Ssam		    param, ac & IEEE80211_WMEPARAM_VAL,
3220173275Ssam		    ac & IEEE80211_WMEPARAM_BSS ? " (BSS)" : "");
3221173275Ssam		return -1;
3222173275Ssam	}
3223173275Ssam	*val = ireq.i_val;
3224173275Ssam	return 0;
3225173275Ssam}
3226173275Ssam
3227138593Ssamstatic void
3228181199Ssamlist_wme_aci(int s, const char *tag, int ac)
3229138593Ssam{
3230181199Ssam	int val;
3231138593Ssam
3232181199Ssam	printf("\t%s", tag);
3233138593Ssam
3234181199Ssam	/* show WME BSS parameters */
3235181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMIN, ac, &val) != -1)
3236181199Ssam		printf(" cwmin %2u", val);
3237181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMAX, ac, &val) != -1)
3238181199Ssam		printf(" cwmax %2u", val);
3239181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_AIFS, ac, &val) != -1)
3240181199Ssam		printf(" aifs %2u", val);
3241181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_TXOPLIMIT, ac, &val) != -1)
3242181199Ssam		printf(" txopLimit %3u", val);
3243181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_ACM, ac, &val) != -1) {
3244181199Ssam		if (val)
3245181199Ssam			printf(" acm");
3246181199Ssam		else if (verbose)
3247181199Ssam			printf(" -acm");
3248181199Ssam	}
3249181199Ssam	/* !BSS only */
3250181199Ssam	if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3251181199Ssam		if (get80211wme(s, IEEE80211_IOC_WME_ACKPOLICY, ac, &val) != -1) {
3252181199Ssam			if (!val)
3253181199Ssam				printf(" -ack");
3254138593Ssam			else if (verbose)
3255181199Ssam				printf(" ack");
3256138593Ssam		}
3257181199Ssam	}
3258181199Ssam	printf("\n");
3259181199Ssam}
3260181199Ssam
3261181199Ssamstatic void
3262181199Ssamlist_wme(int s)
3263181199Ssam{
3264181199Ssam	static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
3265181199Ssam	int ac;
3266181199Ssam
3267181199Ssam	if (verbose) {
3268181199Ssam		/* display both BSS and local settings */
3269181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
3270181199Ssam	again:
3271181199Ssam			if (ac & IEEE80211_WMEPARAM_BSS)
3272181199Ssam				list_wme_aci(s, "     ", ac);
3273181199Ssam			else
3274181199Ssam				list_wme_aci(s, acnames[ac], ac);
3275181199Ssam			if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3276181199Ssam				ac |= IEEE80211_WMEPARAM_BSS;
3277181199Ssam				goto again;
3278181199Ssam			} else
3279181199Ssam				ac &= ~IEEE80211_WMEPARAM_BSS;
3280138593Ssam		}
3281181199Ssam	} else {
3282181199Ssam		/* display only channel settings */
3283181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++)
3284181199Ssam			list_wme_aci(s, acnames[ac], ac);
3285138593Ssam	}
3286138593Ssam}
3287138593Ssam
3288149029Ssamstatic void
3289178354Ssamlist_roam(int s)
3290178354Ssam{
3291178354Ssam	const struct ieee80211_roamparam *rp;
3292178354Ssam	int mode;
3293178354Ssam
3294178354Ssam	getroam(s);
3295178354Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_11NA; mode++) {
3296178354Ssam		rp = &roamparams.params[mode];
3297178354Ssam		if (rp->rssi == 0 && rp->rate == 0)
3298178354Ssam			continue;
3299178354Ssam		if (rp->rssi & 1)
3300178354Ssam			LINE_CHECK("roam:%-6.6s rssi %2u.5dBm rate %2u Mb/s",
3301178354Ssam			    modename[mode], rp->rssi/2, rp->rate/2);
3302178354Ssam		else
3303178354Ssam			LINE_CHECK("roam:%-6.6s rssi %4udBm rate %2u Mb/s",
3304178354Ssam			    modename[mode], rp->rssi/2, rp->rate/2);
3305178354Ssam	}
3306178354Ssam	for (; mode < IEEE80211_MODE_MAX; mode++) {
3307178354Ssam		rp = &roamparams.params[mode];
3308178354Ssam		if (rp->rssi == 0 && rp->rate == 0)
3309178354Ssam			continue;
3310178354Ssam		if (rp->rssi & 1)
3311178354Ssam			LINE_CHECK("roam:%-6.6s rssi %2u.5dBm  MCS %2u    ",
3312178354Ssam			    modename[mode], rp->rssi/2, rp->rate &~ 0x80);
3313178354Ssam		else
3314178354Ssam			LINE_CHECK("roam:%-6.6s rssi %4udBm  MCS %2u    ",
3315178354Ssam			    modename[mode], rp->rssi/2, rp->rate &~ 0x80);
3316178354Ssam	}
3317178354Ssam}
3318178354Ssam
3319178354Ssamstatic void
3320178354Ssamlist_txparams(int s)
3321178354Ssam{
3322178354Ssam	const struct ieee80211_txparam *tp;
3323178354Ssam	int mode;
3324178354Ssam
3325178354Ssam	gettxparams(s);
3326178354Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_11NA; mode++) {
3327178354Ssam		tp = &txparams.params[mode];
3328178354Ssam		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
3329178354Ssam			continue;
3330178354Ssam		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3331178354Ssam			LINE_CHECK("%-6.6s ucast NONE    mgmt %2u Mb/s "
3332178354Ssam			    "mcast %2u Mb/s maxretry %u",
3333178354Ssam			    modename[mode], tp->mgmtrate/2,
3334178354Ssam			    tp->mcastrate/2, tp->maxretry);
3335178354Ssam		else
3336178354Ssam			LINE_CHECK("%-6.6s ucast %2u Mb/s mgmt %2u Mb/s "
3337178354Ssam			    "mcast %2u Mb/s maxretry %u",
3338178354Ssam			    modename[mode], tp->ucastrate/2, tp->mgmtrate/2,
3339178354Ssam			    tp->mcastrate/2, tp->maxretry);
3340178354Ssam	}
3341178354Ssam	for (; mode < IEEE80211_MODE_MAX; mode++) {
3342178354Ssam		tp = &txparams.params[mode];
3343178354Ssam		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
3344178354Ssam			continue;
3345178354Ssam		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3346178354Ssam			LINE_CHECK("%-6.6s ucast NONE    mgmt %2u MCS  "
3347178354Ssam			    "mcast %2u MCS  maxretry %u",
3348178354Ssam			    modename[mode], tp->mgmtrate &~ 0x80,
3349178354Ssam			    tp->mcastrate &~ 0x80, tp->maxretry);
3350178354Ssam		else
3351178354Ssam			LINE_CHECK("%-6.6s ucast %2u MCS  mgmt %2u MCS  "
3352178354Ssam			    "mcast %2u MCS  maxretry %u",
3353178354Ssam			    modename[mode], tp->ucastrate &~ 0x80,
3354178354Ssam			    tp->mgmtrate &~ 0x80,
3355178354Ssam			    tp->mcastrate &~ 0x80, tp->maxretry);
3356178354Ssam	}
3357178354Ssam}
3358178354Ssam
3359178354Ssamstatic void
3360173275Ssamprintpolicy(int policy)
3361173275Ssam{
3362173275Ssam	switch (policy) {
3363173275Ssam	case IEEE80211_MACCMD_POLICY_OPEN:
3364173275Ssam		printf("policy: open\n");
3365173275Ssam		break;
3366173275Ssam	case IEEE80211_MACCMD_POLICY_ALLOW:
3367173275Ssam		printf("policy: allow\n");
3368173275Ssam		break;
3369173275Ssam	case IEEE80211_MACCMD_POLICY_DENY:
3370173275Ssam		printf("policy: deny\n");
3371173275Ssam		break;
3372178354Ssam	case IEEE80211_MACCMD_POLICY_RADIUS:
3373178354Ssam		printf("policy: radius\n");
3374178354Ssam		break;
3375173275Ssam	default:
3376173275Ssam		printf("policy: unknown (%u)\n", policy);
3377173275Ssam		break;
3378173275Ssam	}
3379173275Ssam}
3380173275Ssam
3381173275Ssamstatic void
3382149029Ssamlist_mac(int s)
3383149029Ssam{
3384149029Ssam	struct ieee80211req ireq;
3385149029Ssam	struct ieee80211req_maclist *acllist;
3386173275Ssam	int i, nacls, policy, len;
3387173275Ssam	uint8_t *data;
3388149029Ssam	char c;
3389149029Ssam
3390149029Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3391149029Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
3392149029Ssam	ireq.i_type = IEEE80211_IOC_MACCMD;
3393149029Ssam	ireq.i_val = IEEE80211_MACCMD_POLICY;
3394149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3395149029Ssam		if (errno == EINVAL) {
3396149029Ssam			printf("No acl policy loaded\n");
3397149029Ssam			return;
3398149029Ssam		}
3399149029Ssam		err(1, "unable to get mac policy");
3400149029Ssam	}
3401149029Ssam	policy = ireq.i_val;
3402149029Ssam	if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
3403149029Ssam		c = '*';
3404149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
3405149029Ssam		c = '+';
3406149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
3407149029Ssam		c = '-';
3408178354Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_RADIUS) {
3409178354Ssam		c = 'r';		/* NB: should never have entries */
3410149029Ssam	} else {
3411149029Ssam		printf("policy: unknown (%u)\n", policy);
3412149029Ssam		c = '?';
3413149029Ssam	}
3414173275Ssam	if (verbose || c == '?')
3415173275Ssam		printpolicy(policy);
3416173275Ssam
3417175952Ssam	ireq.i_val = IEEE80211_MACCMD_LIST;
3418175952Ssam	ireq.i_len = 0;
3419175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3420173275Ssam		err(1, "unable to get mac acl list size");
3421175952Ssam	if (ireq.i_len == 0) {		/* NB: no acls */
3422173275Ssam		if (!(verbose || c == '?'))
3423173275Ssam			printpolicy(policy);
3424173275Ssam		return;
3425173275Ssam	}
3426175952Ssam	len = ireq.i_len;
3427173275Ssam
3428173275Ssam	data = malloc(len);
3429173275Ssam	if (data == NULL)
3430173275Ssam		err(1, "out of memory for acl list");
3431173275Ssam
3432175952Ssam	ireq.i_data = data;
3433175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3434173275Ssam		err(1, "unable to get mac acl list");
3435173275Ssam	nacls = len / sizeof(*acllist);
3436173275Ssam	acllist = (struct ieee80211req_maclist *) data;
3437149029Ssam	for (i = 0; i < nacls; i++)
3438149029Ssam		printf("%c%s\n", c, ether_ntoa(
3439149029Ssam			(const struct ether_addr *) acllist[i].ml_macaddr));
3440173275Ssam	free(data);
3441149029Ssam}
3442149029Ssam
3443178354Ssamstatic void
3444178354Ssamprint_regdomain(const struct ieee80211_regdomain *reg, int verb)
3445178354Ssam{
3446178354Ssam	if ((reg->regdomain != 0 &&
3447178354Ssam	    reg->regdomain != reg->country) || verb) {
3448178354Ssam		const struct regdomain *rd =
3449178354Ssam		    lib80211_regdomain_findbysku(getregdata(), reg->regdomain);
3450178354Ssam		if (rd == NULL)
3451178354Ssam			LINE_CHECK("regdomain %d", reg->regdomain);
3452178354Ssam		else
3453178354Ssam			LINE_CHECK("regdomain %s", rd->name);
3454178354Ssam	}
3455178354Ssam	if (reg->country != 0 || verb) {
3456178354Ssam		const struct country *cc =
3457178354Ssam		    lib80211_country_findbycc(getregdata(), reg->country);
3458178354Ssam		if (cc == NULL)
3459178354Ssam			LINE_CHECK("country %d", reg->country);
3460178354Ssam		else
3461178354Ssam			LINE_CHECK("country %s", cc->isoname);
3462178354Ssam	}
3463178354Ssam	if (reg->location == 'I')
3464178354Ssam		LINE_CHECK("indoor");
3465178354Ssam	else if (reg->location == 'O')
3466178354Ssam		LINE_CHECK("outdoor");
3467178354Ssam	else if (verb)
3468178354Ssam		LINE_CHECK("anywhere");
3469178354Ssam	if (reg->ecm)
3470178354Ssam		LINE_CHECK("ecm");
3471178354Ssam	else if (verb)
3472178354Ssam		LINE_CHECK("-ecm");
3473178354Ssam}
3474178354Ssam
3475178354Ssamstatic void
3476178354Ssamlist_regdomain(int s, int channelsalso)
3477178354Ssam{
3478178354Ssam	getregdomain(s);
3479178354Ssam	if (channelsalso) {
3480178354Ssam		getchaninfo(s);
3481178354Ssam		spacer = ':';
3482178354Ssam		print_regdomain(&regdomain, 1);
3483178354Ssam		LINE_BREAK();
3484178354Ssam		print_channels(s, &chaninfo, 1/*allchans*/, 1/*verbose*/);
3485178354Ssam	} else
3486178354Ssam		print_regdomain(&regdomain, verbose);
3487178354Ssam}
3488178354Ssam
3489138593Ssamstatic
3490138593SsamDECL_CMD_FUNC(set80211list, arg, d)
3491138593Ssam{
3492138593Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
3493138593Ssam
3494173275Ssam	LINE_INIT('\t');
3495173275Ssam
3496138593Ssam	if (iseq(arg, "sta"))
3497138593Ssam		list_stations(s);
3498138593Ssam	else if (iseq(arg, "scan") || iseq(arg, "ap"))
3499138593Ssam		list_scan(s);
3500138593Ssam	else if (iseq(arg, "chan") || iseq(arg, "freq"))
3501138593Ssam		list_channels(s, 1);
3502138593Ssam	else if (iseq(arg, "active"))
3503138593Ssam		list_channels(s, 0);
3504138593Ssam	else if (iseq(arg, "keys"))
3505138593Ssam		list_keys(s);
3506138593Ssam	else if (iseq(arg, "caps"))
3507138593Ssam		list_capabilities(s);
3508178354Ssam	else if (iseq(arg, "wme") || iseq(arg, "wmm"))
3509138593Ssam		list_wme(s);
3510149029Ssam	else if (iseq(arg, "mac"))
3511149029Ssam		list_mac(s);
3512170531Ssam	else if (iseq(arg, "txpow"))
3513170531Ssam		list_txpow(s);
3514178354Ssam	else if (iseq(arg, "roam"))
3515178354Ssam		list_roam(s);
3516178354Ssam	else if (iseq(arg, "txparam") || iseq(arg, "txparm"))
3517178354Ssam		list_txparams(s);
3518178354Ssam	else if (iseq(arg, "regdomain"))
3519178354Ssam		list_regdomain(s, 1);
3520178354Ssam	else if (iseq(arg, "countries"))
3521178354Ssam		list_countries();
3522138593Ssam	else
3523138593Ssam		errx(1, "Don't know how to list %s for %s", arg, name);
3524178354Ssam	LINE_BREAK();
3525138593Ssam#undef iseq
3526138593Ssam}
3527138593Ssam
3528138593Ssamstatic enum ieee80211_opmode
3529138593Ssamget80211opmode(int s)
3530138593Ssam{
3531138593Ssam	struct ifmediareq ifmr;
3532138593Ssam
3533138593Ssam	(void) memset(&ifmr, 0, sizeof(ifmr));
3534138593Ssam	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
3535138593Ssam
3536138593Ssam	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
3537138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC)
3538138593Ssam			return IEEE80211_M_IBSS;	/* XXX ahdemo */
3539138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
3540138593Ssam			return IEEE80211_M_HOSTAP;
3541138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
3542138593Ssam			return IEEE80211_M_MONITOR;
3543138593Ssam	}
3544138593Ssam	return IEEE80211_M_STA;
3545138593Ssam}
3546138593Ssam
3547138593Ssam#if 0
3548138593Ssamstatic void
3549138593Ssamprintcipher(int s, struct ieee80211req *ireq, int keylenop)
3550138593Ssam{
3551138593Ssam	switch (ireq->i_val) {
3552138593Ssam	case IEEE80211_CIPHER_WEP:
3553138593Ssam		ireq->i_type = keylenop;
3554138593Ssam		if (ioctl(s, SIOCG80211, ireq) != -1)
3555138593Ssam			printf("WEP-%s",
3556138593Ssam			    ireq->i_len <= 5 ? "40" :
3557138593Ssam			    ireq->i_len <= 13 ? "104" : "128");
3558138593Ssam		else
3559138593Ssam			printf("WEP");
3560138593Ssam		break;
3561138593Ssam	case IEEE80211_CIPHER_TKIP:
3562138593Ssam		printf("TKIP");
3563138593Ssam		break;
3564138593Ssam	case IEEE80211_CIPHER_AES_OCB:
3565138593Ssam		printf("AES-OCB");
3566138593Ssam		break;
3567138593Ssam	case IEEE80211_CIPHER_AES_CCM:
3568138593Ssam		printf("AES-CCM");
3569138593Ssam		break;
3570138593Ssam	case IEEE80211_CIPHER_CKIP:
3571138593Ssam		printf("CKIP");
3572138593Ssam		break;
3573138593Ssam	case IEEE80211_CIPHER_NONE:
3574138593Ssam		printf("NONE");
3575138593Ssam		break;
3576138593Ssam	default:
3577138593Ssam		printf("UNKNOWN (0x%x)", ireq->i_val);
3578138593Ssam		break;
3579138593Ssam	}
3580138593Ssam}
3581138593Ssam#endif
3582138593Ssam
3583155931Ssamstatic void
3584138593Ssamprintkey(const struct ieee80211req_key *ik)
3585138593Ssam{
3586138593Ssam	static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
3587138593Ssam	int keylen = ik->ik_keylen;
3588138593Ssam	int printcontents;
3589138593Ssam
3590148001Srwatson	printcontents = printkeys &&
3591138593Ssam		(memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
3592138593Ssam	if (printcontents)
3593138593Ssam		LINE_BREAK();
3594138593Ssam	switch (ik->ik_type) {
3595138593Ssam	case IEEE80211_CIPHER_WEP:
3596138593Ssam		/* compatibility */
3597155931Ssam		LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
3598138593Ssam		    keylen <= 5 ? "40-bit" :
3599138593Ssam		    keylen <= 13 ? "104-bit" : "128-bit");
3600138593Ssam		break;
3601138593Ssam	case IEEE80211_CIPHER_TKIP:
3602138593Ssam		if (keylen > 128/8)
3603138593Ssam			keylen -= 128/8;	/* ignore MIC for now */
3604155931Ssam		LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3605138593Ssam		break;
3606138593Ssam	case IEEE80211_CIPHER_AES_OCB:
3607155931Ssam		LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3608138593Ssam		break;
3609138593Ssam	case IEEE80211_CIPHER_AES_CCM:
3610155931Ssam		LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3611138593Ssam		break;
3612138593Ssam	case IEEE80211_CIPHER_CKIP:
3613155931Ssam		LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3614138593Ssam		break;
3615138593Ssam	case IEEE80211_CIPHER_NONE:
3616155931Ssam		LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3617138593Ssam		break;
3618138593Ssam	default:
3619155931Ssam		LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
3620138593Ssam			ik->ik_type, ik->ik_keyix+1, 8*keylen);
3621138593Ssam		break;
3622138593Ssam	}
3623138593Ssam	if (printcontents) {
3624138593Ssam		int i;
3625138593Ssam
3626138593Ssam		printf(" <");
3627138593Ssam		for (i = 0; i < keylen; i++)
3628138593Ssam			printf("%02x", ik->ik_keydata[i]);
3629138593Ssam		printf(">");
3630138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
3631138593Ssam		    (ik->ik_keyrsc != 0 || verbose))
3632146873Sjhb			printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
3633138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
3634138593Ssam		    (ik->ik_keytsc != 0 || verbose))
3635146873Sjhb			printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
3636138593Ssam		if (ik->ik_flags != 0 && verbose) {
3637138593Ssam			const char *sep = " ";
3638138593Ssam
3639138593Ssam			if (ik->ik_flags & IEEE80211_KEY_XMIT)
3640138593Ssam				printf("%stx", sep), sep = "+";
3641138593Ssam			if (ik->ik_flags & IEEE80211_KEY_RECV)
3642138593Ssam				printf("%srx", sep), sep = "+";
3643138593Ssam			if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
3644138593Ssam				printf("%sdef", sep), sep = "+";
3645138593Ssam		}
3646138593Ssam		LINE_BREAK();
3647138593Ssam	}
3648138593Ssam}
3649138593Ssam
3650138593Ssamstatic void
3651173275Ssamprintrate(const char *tag, int v, int defrate, int defmcs)
3652138593Ssam{
3653173275Ssam	if (v == 11)
3654173275Ssam		LINE_CHECK("%s 5.5", tag);
3655173275Ssam	else if (v & 0x80) {
3656173275Ssam		if (v != defmcs)
3657173275Ssam			LINE_CHECK("%s %d", tag, v &~ 0x80);
3658173275Ssam	} else {
3659173275Ssam		if (v != defrate)
3660173275Ssam			LINE_CHECK("%s %d", tag, v/2);
3661173275Ssam	}
3662173275Ssam}
3663173275Ssam
3664173275Ssamstatic int
3665173275Ssamgetssid(int s, int ix, void *data, size_t len, int *plen)
3666173275Ssam{
3667138593Ssam	struct ieee80211req ireq;
3668138593Ssam
3669138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3670138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3671173275Ssam	ireq.i_type = IEEE80211_IOC_SSID;
3672173275Ssam	ireq.i_val = ix;
3673173275Ssam	ireq.i_data = data;
3674173275Ssam	ireq.i_len = len;
3675173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3676173275Ssam		return -1;
3677173275Ssam	*plen = ireq.i_len;
3678173275Ssam	return 0;
3679173275Ssam}
368077218Sphk
3681173275Ssamstatic void
3682173275Ssamieee80211_status(int s)
3683173275Ssam{
3684173275Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
3685173275Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
3686173275Ssam	int i, num, wpa, wme, bgscan, bgscaninterval, val, len, wepmode;
3687173275Ssam	uint8_t data[32];
3688173275Ssam	const struct ieee80211_channel *c;
3689178354Ssam	const struct ieee80211_roamparam *rp;
3690178354Ssam	const struct ieee80211_txparam *tp;
3691173275Ssam
3692173275Ssam	if (getssid(s, -1, data, sizeof(data), &len) < 0) {
3693148686Sstefanf		/* If we can't get the SSID, this isn't an 802.11 device. */
369477218Sphk		return;
369577218Sphk	}
3696173275Ssam
3697173275Ssam	/*
3698173275Ssam	 * Invalidate cached state so printing status for multiple
3699173275Ssam	 * if's doesn't reuse the first interfaces' cached state.
3700173275Ssam	 */
3701173275Ssam	gotcurchan = 0;
3702178354Ssam	gotroam = 0;
3703178354Ssam	gottxparams = 0;
3704173275Ssam	gothtconf = 0;
3705178354Ssam	gotregdomain = 0;
3706173275Ssam
3707173275Ssam	if (get80211val(s, IEEE80211_IOC_NUMSSIDS, &num) < 0)
3708173275Ssam		num = 0;
3709138593Ssam	printf("\tssid ");
3710138593Ssam	if (num > 1) {
3711173275Ssam		for (i = 0; i < num; i++) {
3712173275Ssam			if (getssid(s, i, data, sizeof(data), &len) >= 0 && len > 0) {
3713173275Ssam				printf(" %d:", i + 1);
3714173275Ssam				print_string(data, len);
3715138593Ssam			}
371688748Sambrisko		}
3717138593Ssam	} else
3718173275Ssam		print_string(data, len);
371977218Sphk
3720173275Ssam	c = getcurchan(s);
3721170531Ssam	if (c->ic_freq != IEEE80211_CHAN_ANY) {
3722170531Ssam		char buf[14];
3723170531Ssam		printf(" channel %d (%u Mhz%s)", c->ic_ieee, c->ic_freq,
3724170531Ssam			get_chaninfo(c, 1, buf, sizeof(buf)));
3725138593Ssam	} else if (verbose)
3726138593Ssam		printf(" channel UNDEF");
3727138593Ssam
3728173275Ssam	if (get80211(s, IEEE80211_IOC_BSSID, data, IEEE80211_ADDR_LEN) >= 0 &&
3729173275Ssam	    (memcmp(data, zerobssid, sizeof(zerobssid)) != 0 || verbose))
3730173275Ssam		printf(" bssid %s", ether_ntoa((struct ether_addr *)data));
3731138593Ssam
3732173275Ssam	if (get80211len(s, IEEE80211_IOC_STATIONNAME, data, sizeof(data), &len) != -1) {
3733138593Ssam		printf("\n\tstationname ");
3734173275Ssam		print_string(data, len);
373577218Sphk	}
373677218Sphk
3737138593Ssam	spacer = ' ';		/* force first break */
3738138593Ssam	LINE_BREAK();
373977218Sphk
3740178354Ssam	list_regdomain(s, 0);
3741178354Ssam
3742173275Ssam	wpa = 0;
3743173275Ssam	if (get80211val(s, IEEE80211_IOC_AUTHMODE, &val) != -1) {
3744173275Ssam		switch (val) {
3745173275Ssam		case IEEE80211_AUTH_NONE:
3746173275Ssam			LINE_CHECK("authmode NONE");
3747173275Ssam			break;
3748173275Ssam		case IEEE80211_AUTH_OPEN:
3749173275Ssam			LINE_CHECK("authmode OPEN");
3750173275Ssam			break;
3751173275Ssam		case IEEE80211_AUTH_SHARED:
3752173275Ssam			LINE_CHECK("authmode SHARED");
3753173275Ssam			break;
3754173275Ssam		case IEEE80211_AUTH_8021X:
3755173275Ssam			LINE_CHECK("authmode 802.1x");
3756173275Ssam			break;
3757173275Ssam		case IEEE80211_AUTH_WPA:
3758173275Ssam			if (get80211val(s, IEEE80211_IOC_WPA, &wpa) < 0)
3759173275Ssam				wpa = 1;	/* default to WPA1 */
3760173275Ssam			switch (wpa) {
3761173275Ssam			case 2:
3762173275Ssam				LINE_CHECK("authmode WPA2/802.11i");
376377218Sphk				break;
3764173275Ssam			case 3:
3765173275Ssam				LINE_CHECK("authmode WPA1+WPA2/802.11i");
376677218Sphk				break;
3767127649Ssam			default:
3768173275Ssam				LINE_CHECK("authmode WPA");
3769127649Ssam				break;
3770173275Ssam			}
3771173275Ssam			break;
3772173275Ssam		case IEEE80211_AUTH_AUTO:
3773173275Ssam			LINE_CHECK("authmode AUTO");
3774173275Ssam			break;
3775173275Ssam		default:
3776173275Ssam			LINE_CHECK("authmode UNKNOWN (0x%x)", val);
3777173275Ssam			break;
3778127649Ssam		}
3779127649Ssam	}
3780127649Ssam
3781173275Ssam	if (wpa || verbose) {
3782178354Ssam		if (get80211val(s, IEEE80211_IOC_WPS, &val) != -1) {
3783178354Ssam			if (val)
3784178354Ssam				LINE_CHECK("wps");
3785178354Ssam			else if (verbose)
3786178354Ssam				LINE_CHECK("-wps");
3787178354Ssam		}
3788178354Ssam		if (get80211val(s, IEEE80211_IOC_TSN, &val) != -1) {
3789178354Ssam			if (val)
3790178354Ssam				LINE_CHECK("tsn");
3791178354Ssam			else if (verbose)
3792178354Ssam				LINE_CHECK("-tsn");
3793178354Ssam		}
3794173275Ssam		if (ioctl(s, IEEE80211_IOC_COUNTERMEASURES, &val) != -1) {
3795173275Ssam			if (val)
3796173275Ssam				LINE_CHECK("countermeasures");
3797173275Ssam			else if (verbose)
3798173275Ssam				LINE_CHECK("-countermeasures");
3799173275Ssam		}
3800178354Ssam#if 0
3801178354Ssam		/* XXX not interesting with WPA done in user space */
3802178354Ssam		ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
3803178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3804178354Ssam		}
3805178354Ssam
3806178354Ssam		ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
3807178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3808178354Ssam			LINE_CHECK("mcastcipher ");
3809178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
3810178354Ssam			spacer = ' ';
3811178354Ssam		}
3812178354Ssam
3813178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
3814178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3815178354Ssam			LINE_CHECK("ucastcipher ");
3816178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
3817178354Ssam		}
3818178354Ssam
3819178354Ssam		if (wpa & 2) {
3820178354Ssam			ireq.i_type = IEEE80211_IOC_RSNCAPS;
3821178354Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1) {
3822178354Ssam				LINE_CHECK("RSN caps 0x%x", ireq.i_val);
3823178354Ssam				spacer = ' ';
3824178354Ssam			}
3825178354Ssam		}
3826178354Ssam
3827178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
3828178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3829178354Ssam		}
3830178354Ssam#endif
3831173275Ssam	}
3832138593Ssam
3833173275Ssam	if (get80211val(s, IEEE80211_IOC_WEP, &wepmode) != -1 &&
3834173275Ssam	    wepmode != IEEE80211_WEP_NOSUP) {
3835173275Ssam		int firstkey;
3836173275Ssam
3837138718Ssam		switch (wepmode) {
3838173275Ssam		case IEEE80211_WEP_OFF:
3839173275Ssam			LINE_CHECK("privacy OFF");
3840173275Ssam			break;
3841173275Ssam		case IEEE80211_WEP_ON:
3842173275Ssam			LINE_CHECK("privacy ON");
3843173275Ssam			break;
3844173275Ssam		case IEEE80211_WEP_MIXED:
3845173275Ssam			LINE_CHECK("privacy MIXED");
3846173275Ssam			break;
3847173275Ssam		default:
3848173275Ssam			LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
3849173275Ssam			break;
385077218Sphk		}
385177218Sphk
385277218Sphk		/*
385377218Sphk		 * If we get here then we've got WEP support so we need
385477218Sphk		 * to print WEP status.
385591454Sbrooks		 */
385677218Sphk
3857173275Ssam		if (get80211val(s, IEEE80211_IOC_WEPTXKEY, &val) < 0) {
385877218Sphk			warn("WEP support, but no tx key!");
385977218Sphk			goto end;
386077218Sphk		}
3861173275Ssam		if (val != -1)
3862173275Ssam			LINE_CHECK("deftxkey %d", val+1);
3863138718Ssam		else if (wepmode != IEEE80211_WEP_OFF || verbose)
3864155931Ssam			LINE_CHECK("deftxkey UNDEF");
386577218Sphk
3866173275Ssam		if (get80211val(s, IEEE80211_IOC_NUMWEPKEYS, &num) < 0) {
386777218Sphk			warn("WEP support, but no NUMWEPKEYS support!");
386877218Sphk			goto end;
386977218Sphk		}
387077218Sphk
3871138593Ssam		firstkey = 1;
3872138593Ssam		for (i = 0; i < num; i++) {
3873138593Ssam			struct ieee80211req_key ik;
387477218Sphk
3875138593Ssam			memset(&ik, 0, sizeof(ik));
3876138593Ssam			ik.ik_keyix = i;
3877173275Ssam			if (get80211(s, IEEE80211_IOC_WPAKEY, &ik, sizeof(ik)) < 0) {
387877218Sphk				warn("WEP support, but can get keys!");
387977218Sphk				goto end;
388077218Sphk			}
3881138593Ssam			if (ik.ik_keylen != 0) {
3882138593Ssam				if (verbose)
3883138593Ssam					LINE_BREAK();
3884138593Ssam				printkey(&ik);
3885138593Ssam				firstkey = 0;
3886138593Ssam			}
3887138593Ssam		}
3888173275Ssamend:
3889173275Ssam		;
3890138593Ssam	}
3891138593Ssam
3892173275Ssam	if (get80211val(s, IEEE80211_IOC_POWERSAVE, &val) != -1 &&
3893173275Ssam	    val != IEEE80211_POWERSAVE_NOSUP ) {
3894173275Ssam		if (val != IEEE80211_POWERSAVE_OFF || verbose) {
3895173275Ssam			switch (val) {
3896173275Ssam			case IEEE80211_POWERSAVE_OFF:
3897173275Ssam				LINE_CHECK("powersavemode OFF");
3898173275Ssam				break;
3899173275Ssam			case IEEE80211_POWERSAVE_CAM:
3900173275Ssam				LINE_CHECK("powersavemode CAM");
3901173275Ssam				break;
3902173275Ssam			case IEEE80211_POWERSAVE_PSP:
3903173275Ssam				LINE_CHECK("powersavemode PSP");
3904173275Ssam				break;
3905173275Ssam			case IEEE80211_POWERSAVE_PSP_CAM:
3906173275Ssam				LINE_CHECK("powersavemode PSP-CAM");
3907173275Ssam				break;
3908138593Ssam			}
3909173275Ssam			if (get80211val(s, IEEE80211_IOC_POWERSAVESLEEP, &val) != -1)
3910173275Ssam				LINE_CHECK("powersavesleep %d", val);
3911138593Ssam		}
3912138593Ssam	}
3913138593Ssam
3914173275Ssam	if (get80211val(s, IEEE80211_IOC_TXPOWER, &val) != -1) {
3915173275Ssam		if (val & 1)
3916173275Ssam			LINE_CHECK("txpower %d.5", val/2);
3917173275Ssam		else
3918173275Ssam			LINE_CHECK("txpower %d", val/2);
3919173275Ssam	}
3920138593Ssam	if (verbose) {
3921173275Ssam		if (get80211val(s, IEEE80211_IOC_TXPOWMAX, &val) != -1)
3922173275Ssam			LINE_CHECK("txpowmax %.1f", val/2.);
3923138593Ssam	}
3924138593Ssam
3925178354Ssam	if (get80211val(s, IEEE80211_IOC_DOTD, &val) != -1) {
3926178354Ssam		if (val)
3927178354Ssam			LINE_CHECK("dotd");
3928178354Ssam		else if (verbose)
3929178354Ssam			LINE_CHECK("-dotd");
3930178354Ssam	}
3931178354Ssam
3932173275Ssam	if (get80211val(s, IEEE80211_IOC_RTSTHRESHOLD, &val) != -1) {
3933173275Ssam		if (val != IEEE80211_RTS_MAX || verbose)
3934173275Ssam			LINE_CHECK("rtsthreshold %d", val);
3935138593Ssam	}
3936138593Ssam
3937173275Ssam	if (get80211val(s, IEEE80211_IOC_FRAGTHRESHOLD, &val) != -1) {
3938173275Ssam		if (val != IEEE80211_FRAG_MAX || verbose)
3939173275Ssam			LINE_CHECK("fragthreshold %d", val);
3940170531Ssam	}
3941173275Ssam	if (opmode == IEEE80211_M_STA || verbose) {
3942173275Ssam		if (get80211val(s, IEEE80211_IOC_BMISSTHRESHOLD, &val) != -1) {
3943173275Ssam			if (val != IEEE80211_HWBMISS_MAX || verbose)
3944173275Ssam				LINE_CHECK("bmiss %d", val);
3945153354Ssam		}
3946153354Ssam	}
3947153354Ssam
3948178354Ssam	if (!verbose) {
3949178354Ssam		gettxparams(s);
3950178354Ssam		tp = &txparams.params[chan2mode(c)];
3951178354Ssam		printrate("ucastrate", tp->ucastrate,
3952178354Ssam		    IEEE80211_FIXED_RATE_NONE, IEEE80211_FIXED_RATE_NONE);
3953178354Ssam		printrate("mcastrate", tp->mcastrate, 2*1, 0x80|0);
3954178354Ssam		printrate("mgmtrate", tp->mgmtrate, 2*1, 0x80|0);
3955178354Ssam		if (tp->maxretry != 6)		/* XXX */
3956178354Ssam			LINE_CHECK("maxretry %d", tp->maxretry);
3957178354Ssam	} else {
3958178354Ssam		LINE_BREAK();
3959178354Ssam		list_txparams(s);
3960178354Ssam	}
3961170531Ssam
3962173275Ssam	bgscaninterval = -1;
3963173275Ssam	(void) get80211val(s, IEEE80211_IOC_BGSCAN_INTERVAL, &bgscaninterval);
3964173275Ssam
3965173275Ssam	if (get80211val(s, IEEE80211_IOC_SCANVALID, &val) != -1) {
3966173275Ssam		if (val != bgscaninterval || verbose)
3967173275Ssam			LINE_CHECK("scanvalid %u", val);
3968148416Ssam	}
3969148416Ssam
3970173275Ssam	bgscan = 0;
3971173275Ssam	if (get80211val(s, IEEE80211_IOC_BGSCAN, &bgscan) != -1) {
3972173275Ssam		if (bgscan)
3973170531Ssam			LINE_CHECK("bgscan");
3974170531Ssam		else if (verbose)
3975170531Ssam			LINE_CHECK("-bgscan");
3976160687Ssam	}
3977170531Ssam	if (bgscan || verbose) {
3978170531Ssam		if (bgscaninterval != -1)
3979170531Ssam			LINE_CHECK("bgscanintvl %u", bgscaninterval);
3980173275Ssam		if (get80211val(s, IEEE80211_IOC_BGSCAN_IDLE, &val) != -1)
3981173275Ssam			LINE_CHECK("bgscanidle %u", val);
3982178354Ssam		if (!verbose) {
3983178354Ssam			getroam(s);
3984178354Ssam			rp = &roamparams.params[chan2mode(c)];
3985178354Ssam			if (rp->rssi & 1)
3986178354Ssam				LINE_CHECK("roam:rssi %u.5", rp->rssi/2);
3987178354Ssam			else
3988178354Ssam				LINE_CHECK("roam:rssi %u", rp->rssi/2);
3989178354Ssam			LINE_CHECK("roam:rate %u", rp->rate/2);
3990178354Ssam		} else {
3991178354Ssam			LINE_BREAK();
3992178354Ssam			list_roam(s);
3993170531Ssam		}
3994170531Ssam	}
3995160687Ssam
3996165570Ssam	if (IEEE80211_IS_CHAN_ANYG(c) || verbose) {
3997173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREG, &val) != -1) {
3998173275Ssam			if (val)
3999155931Ssam				LINE_CHECK("pureg");
4000147795Ssam			else if (verbose)
4001155931Ssam				LINE_CHECK("-pureg");
4002147795Ssam		}
4003173275Ssam		if (get80211val(s, IEEE80211_IOC_PROTMODE, &val) != -1) {
4004173275Ssam			switch (val) {
4005173275Ssam			case IEEE80211_PROTMODE_OFF:
4006173275Ssam				LINE_CHECK("protmode OFF");
4007173275Ssam				break;
4008173275Ssam			case IEEE80211_PROTMODE_CTS:
4009173275Ssam				LINE_CHECK("protmode CTS");
4010173275Ssam				break;
4011173275Ssam			case IEEE80211_PROTMODE_RTSCTS:
4012173275Ssam				LINE_CHECK("protmode RTSCTS");
4013173275Ssam				break;
4014173275Ssam			default:
4015173275Ssam				LINE_CHECK("protmode UNKNOWN (0x%x)", val);
4016173275Ssam				break;
4017138593Ssam			}
4018138593Ssam		}
4019138593Ssam	}
4020138593Ssam
4021173275Ssam	if (IEEE80211_IS_CHAN_HT(c) || verbose) {
4022173275Ssam		gethtconf(s);
4023173275Ssam		switch (htconf & 3) {
4024173275Ssam		case 0:
4025173275Ssam		case 2:
4026173275Ssam			LINE_CHECK("-ht");
4027173275Ssam			break;
4028173275Ssam		case 1:
4029173275Ssam			LINE_CHECK("ht20");
4030173275Ssam			break;
4031173275Ssam		case 3:
4032173275Ssam			if (verbose)
4033173275Ssam				LINE_CHECK("ht");
4034173275Ssam			break;
4035173275Ssam		}
4036173275Ssam		if (get80211val(s, IEEE80211_IOC_HTCOMPAT, &val) != -1) {
4037173275Ssam			if (!val)
4038173275Ssam				LINE_CHECK("-htcompat");
4039173275Ssam			else if (verbose)
4040173275Ssam				LINE_CHECK("htcompat");
4041173275Ssam		}
4042173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU, &val) != -1) {
4043173275Ssam			switch (val) {
4044173275Ssam			case 0:
4045173275Ssam				LINE_CHECK("-ampdu");
4046173275Ssam				break;
4047173275Ssam			case 1:
4048173275Ssam				LINE_CHECK("ampdutx -ampdurx");
4049173275Ssam				break;
4050173275Ssam			case 2:
4051173275Ssam				LINE_CHECK("-ampdutx ampdurx");
4052173275Ssam				break;
4053173275Ssam			case 3:
4054173275Ssam				if (verbose)
4055173275Ssam					LINE_CHECK("ampdu");
4056173275Ssam				break;
4057173275Ssam			}
4058173275Ssam		}
4059173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_LIMIT, &val) != -1) {
4060173275Ssam			switch (val) {
4061173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_8K:
4062173275Ssam				LINE_CHECK("ampdulimit 8k");
4063173275Ssam				break;
4064173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_16K:
4065173275Ssam				LINE_CHECK("ampdulimit 16k");
4066173275Ssam				break;
4067173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_32K:
4068173275Ssam				LINE_CHECK("ampdulimit 32k");
4069173275Ssam				break;
4070173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_64K:
4071173275Ssam				LINE_CHECK("ampdulimit 64k");
4072173275Ssam				break;
4073173275Ssam			}
4074173275Ssam		}
4075173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_DENSITY, &val) != -1) {
4076173275Ssam			switch (val) {
4077173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_NA:
4078173275Ssam				if (verbose)
4079183260Ssam					LINE_CHECK("ampdudensity NA");
4080173275Ssam				break;
4081173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_025:
4082173275Ssam				LINE_CHECK("ampdudensity .25");
4083173275Ssam				break;
4084173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_05:
4085173275Ssam				LINE_CHECK("ampdudensity .5");
4086173275Ssam				break;
4087173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_1:
4088173275Ssam				LINE_CHECK("ampdudensity 1");
4089173275Ssam				break;
4090173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_2:
4091173275Ssam				LINE_CHECK("ampdudensity 2");
4092173275Ssam				break;
4093173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_4:
4094173275Ssam				LINE_CHECK("ampdudensity 4");
4095173275Ssam				break;
4096173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_8:
4097173275Ssam				LINE_CHECK("ampdudensity 8");
4098173275Ssam				break;
4099173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_16:
4100173275Ssam				LINE_CHECK("ampdudensity 16");
4101173275Ssam				break;
4102173275Ssam			}
4103173275Ssam		}
4104173275Ssam		if (get80211val(s, IEEE80211_IOC_AMSDU, &val) != -1) {
4105173275Ssam			switch (val) {
4106173275Ssam			case 0:
4107173275Ssam				LINE_CHECK("-amsdu");
4108173275Ssam				break;
4109173275Ssam			case 1:
4110173275Ssam				LINE_CHECK("amsdutx -amsdurx");
4111173275Ssam				break;
4112173275Ssam			case 2:
4113173275Ssam				LINE_CHECK("-amsdutx amsdurx");
4114173275Ssam				break;
4115173275Ssam			case 3:
4116173275Ssam				if (verbose)
4117173275Ssam					LINE_CHECK("amsdu");
4118173275Ssam				break;
4119173275Ssam			}
4120173275Ssam		}
4121173275Ssam		/* XXX amsdu limit */
4122173275Ssam		if (get80211val(s, IEEE80211_IOC_SHORTGI, &val) != -1) {
4123173275Ssam			if (val)
4124173275Ssam				LINE_CHECK("shortgi");
4125173275Ssam			else if (verbose)
4126173275Ssam				LINE_CHECK("-shortgi");
4127173275Ssam		}
4128173275Ssam		if (get80211val(s, IEEE80211_IOC_HTPROTMODE, &val) != -1) {
4129173275Ssam			if (val == IEEE80211_PROTMODE_OFF)
4130173275Ssam				LINE_CHECK("htprotmode OFF");
4131173275Ssam			else if (val != IEEE80211_PROTMODE_RTSCTS)
4132173275Ssam				LINE_CHECK("htprotmode UNKNOWN (0x%x)", val);
4133173275Ssam			else if (verbose)
4134173275Ssam				LINE_CHECK("htprotmode RTSCTS");
4135173275Ssam		}
4136173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREN, &val) != -1) {
4137173275Ssam			if (val)
4138173275Ssam				LINE_CHECK("puren");
4139173275Ssam			else if (verbose)
4140173275Ssam				LINE_CHECK("-puren");
4141173275Ssam		}
4142183261Ssam		if (get80211val(s, IEEE80211_IOC_SMPS, &val) != -1) {
4143183261Ssam			if (val == IEEE80211_HTCAP_SMPS_DYNAMIC)
4144183261Ssam				LINE_CHECK("smpsdyn");
4145183261Ssam			else if (val == IEEE80211_HTCAP_SMPS_ENA)
4146183261Ssam				LINE_CHECK("smps");
4147183261Ssam			else if (verbose)
4148183261Ssam				LINE_CHECK("-smps");
4149183261Ssam		}
4150183261Ssam		if (get80211val(s, IEEE80211_IOC_RIFS, &val) != -1) {
4151183261Ssam			if (val)
4152183261Ssam				LINE_CHECK("rifs");
4153183261Ssam			else if (verbose)
4154183261Ssam				LINE_CHECK("-rifs");
4155183261Ssam		}
4156173275Ssam	}
4157173275Ssam
4158173275Ssam	if (get80211val(s, IEEE80211_IOC_WME, &wme) != -1) {
4159138593Ssam		if (wme)
4160155931Ssam			LINE_CHECK("wme");
4161138593Ssam		else if (verbose)
4162155931Ssam			LINE_CHECK("-wme");
4163138593Ssam	} else
4164138593Ssam		wme = 0;
4165138593Ssam
4166173275Ssam	if (get80211val(s, IEEE80211_IOC_BURST, &val) != -1) {
4167173275Ssam		if (val)
4168155931Ssam			LINE_CHECK("burst");
4169153422Ssam		else if (verbose)
4170155931Ssam			LINE_CHECK("-burst");
4171153422Ssam	}
4172153422Ssam
4173173275Ssam	if (get80211val(s, IEEE80211_IOC_FF, &val) != -1) {
4174173275Ssam		if (val)
4175170531Ssam			LINE_CHECK("ff");
4176170531Ssam		else if (verbose)
4177170531Ssam			LINE_CHECK("-ff");
4178170531Ssam	}
4179173275Ssam	if (get80211val(s, IEEE80211_IOC_TURBOP, &val) != -1) {
4180173275Ssam		if (val)
4181170531Ssam			LINE_CHECK("dturbo");
4182170531Ssam		else if (verbose)
4183170531Ssam			LINE_CHECK("-dturbo");
4184170531Ssam	}
4185178354Ssam	if (get80211val(s, IEEE80211_IOC_DWDS, &val) != -1) {
4186178354Ssam		if (val)
4187178354Ssam			LINE_CHECK("dwds");
4188178354Ssam		else if (verbose)
4189178354Ssam			LINE_CHECK("-dwds");
4190178354Ssam	}
4191170531Ssam
4192138593Ssam	if (opmode == IEEE80211_M_HOSTAP) {
4193173275Ssam		if (get80211val(s, IEEE80211_IOC_HIDESSID, &val) != -1) {
4194173275Ssam			if (val)
4195168075Ssam				LINE_CHECK("hidessid");
4196138593Ssam			else if (verbose)
4197168075Ssam				LINE_CHECK("-hidessid");
4198138593Ssam		}
4199173275Ssam		if (get80211val(s, IEEE80211_IOC_APBRIDGE, &val) != -1) {
4200173275Ssam			if (!val)
4201155931Ssam				LINE_CHECK("-apbridge");
4202138593Ssam			else if (verbose)
4203155931Ssam				LINE_CHECK("apbridge");
4204138593Ssam		}
4205173275Ssam		if (get80211val(s, IEEE80211_IOC_DTIM_PERIOD, &val) != -1)
4206173275Ssam			LINE_CHECK("dtimperiod %u", val);
4207138593Ssam
4208173275Ssam		if (get80211val(s, IEEE80211_IOC_DOTH, &val) != -1) {
4209173275Ssam			if (!val)
4210170531Ssam				LINE_CHECK("-doth");
4211170531Ssam			else if (verbose)
4212170531Ssam				LINE_CHECK("doth");
4213170531Ssam		}
4214178354Ssam		if (get80211val(s, IEEE80211_IOC_DFS, &val) != -1) {
4215178354Ssam			if (!val)
4216178354Ssam				LINE_CHECK("-dfs");
4217178354Ssam			else if (verbose)
4218178354Ssam				LINE_CHECK("dfs");
4219178354Ssam		}
4220173275Ssam		if (get80211val(s, IEEE80211_IOC_INACTIVITY, &val) != -1) {
4221173275Ssam			if (!val)
4222173275Ssam				LINE_CHECK("-inact");
4223173275Ssam			else if (verbose)
4224173275Ssam				LINE_CHECK("inact");
4225173275Ssam		}
4226138593Ssam	} else {
4227173275Ssam		if (get80211val(s, IEEE80211_IOC_ROAMING, &val) != -1) {
4228173275Ssam			if (val != IEEE80211_ROAMING_AUTO || verbose) {
4229173275Ssam				switch (val) {
4230138593Ssam				case IEEE80211_ROAMING_DEVICE:
4231155931Ssam					LINE_CHECK("roaming DEVICE");
4232138593Ssam					break;
4233138593Ssam				case IEEE80211_ROAMING_AUTO:
4234155931Ssam					LINE_CHECK("roaming AUTO");
4235138593Ssam					break;
4236138593Ssam				case IEEE80211_ROAMING_MANUAL:
4237155931Ssam					LINE_CHECK("roaming MANUAL");
4238138593Ssam					break;
4239138593Ssam				default:
4240155931Ssam					LINE_CHECK("roaming UNKNOWN (0x%x)",
4241173275Ssam						val);
4242138593Ssam					break;
4243138593Ssam				}
4244138593Ssam			}
4245138593Ssam		}
4246138593Ssam	}
4247173275Ssam	if (get80211val(s, IEEE80211_IOC_BEACON_INTERVAL, &val) != -1) {
4248173275Ssam		/* XXX default define not visible */
4249173275Ssam		if (val != 100 || verbose)
4250173275Ssam			LINE_CHECK("bintval %u", val);
4251138593Ssam	}
4252138593Ssam
4253138593Ssam	if (wme && verbose) {
4254138593Ssam		LINE_BREAK();
4255138593Ssam		list_wme(s);
4256138593Ssam	}
4257173275Ssam	LINE_BREAK();
4258173275Ssam}
4259138593Ssam
4260173275Ssamstatic int
4261173275Ssamget80211(int s, int type, void *data, int len)
4262173275Ssam{
4263173275Ssam	struct ieee80211req ireq;
4264138593Ssam
4265173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4266173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4267173275Ssam	ireq.i_type = type;
4268173275Ssam	ireq.i_data = data;
4269173275Ssam	ireq.i_len = len;
4270173275Ssam	return ioctl(s, SIOCG80211, &ireq);
4271173275Ssam}
4272138593Ssam
4273173275Ssamstatic int
4274173275Ssamget80211len(int s, int type, void *data, int len, int *plen)
4275173275Ssam{
4276173275Ssam	struct ieee80211req ireq;
4277138593Ssam
4278173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4279173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4280173275Ssam	ireq.i_type = type;
4281173275Ssam	ireq.i_len = len;
4282173275Ssam	ireq.i_data = data;
4283173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4284173275Ssam		return -1;
4285173275Ssam	*plen = ireq.i_len;
4286173275Ssam	return 0;
4287173275Ssam}
4288138593Ssam
4289173275Ssamstatic int
4290173275Ssamget80211val(int s, int type, int *val)
4291173275Ssam{
4292173275Ssam	struct ieee80211req ireq;
429377218Sphk
4294173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4295173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4296173275Ssam	ireq.i_type = type;
4297173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4298173275Ssam		return -1;
4299173275Ssam	*val = ireq.i_val;
4300173275Ssam	return 0;
430177218Sphk}
430277218Sphk
430377218Sphkstatic void
4304170531Ssamset80211(int s, int type, int val, int len, void *data)
430577218Sphk{
430677218Sphk	struct ieee80211req	ireq;
430777218Sphk
430877218Sphk	(void) memset(&ireq, 0, sizeof(ireq));
430977218Sphk	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
431077218Sphk	ireq.i_type = type;
431177218Sphk	ireq.i_val = val;
431277218Sphk	ireq.i_len = len;
431377218Sphk	ireq.i_data = data;
431491454Sbrooks	if (ioctl(s, SIOCS80211, &ireq) < 0)
431577218Sphk		err(1, "SIOCS80211");
431677218Sphk}
431777218Sphk
431877218Sphkstatic const char *
431977218Sphkget_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
432077218Sphk{
432177218Sphk	int len;
432277218Sphk	int hexstr;
432377218Sphk	u_int8_t *p;
432477218Sphk
432577218Sphk	len = *lenp;
432677218Sphk	p = buf;
432777218Sphk	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
432877218Sphk	if (hexstr)
432977218Sphk		val += 2;
433077218Sphk	for (;;) {
433177218Sphk		if (*val == '\0')
433277218Sphk			break;
433377218Sphk		if (sep != NULL && strchr(sep, *val) != NULL) {
433477218Sphk			val++;
433577218Sphk			break;
433677218Sphk		}
433777218Sphk		if (hexstr) {
4338127831Sphk			if (!isxdigit((u_char)val[0])) {
433977218Sphk				warnx("bad hexadecimal digits");
434077218Sphk				return NULL;
434177218Sphk			}
4342127831Sphk			if (!isxdigit((u_char)val[1])) {
4343127831Sphk				warnx("odd count hexadecimal digits");
4344127831Sphk				return NULL;
4345127831Sphk			}
434677218Sphk		}
4347127831Sphk		if (p >= buf + len) {
434877218Sphk			if (hexstr)
434977218Sphk				warnx("hexadecimal digits too long");
435077218Sphk			else
4351127831Sphk				warnx("string too long");
435277218Sphk			return NULL;
435377218Sphk		}
435477218Sphk		if (hexstr) {
435577218Sphk#define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
435677218Sphk			*p++ = (tohex((u_char)val[0]) << 4) |
435777218Sphk			    tohex((u_char)val[1]);
435877218Sphk#undef tohex
435977218Sphk			val += 2;
436077218Sphk		} else
436177218Sphk			*p++ = *val++;
436277218Sphk	}
436377218Sphk	len = p - buf;
436477218Sphk	/* The string "-" is treated as the empty string. */
4365165045Ssam	if (!hexstr && len == 1 && buf[0] == '-') {
436677218Sphk		len = 0;
4367165045Ssam		memset(buf, 0, *lenp);
4368165045Ssam	} else if (len < *lenp)
436977218Sphk		memset(p, 0, *lenp - len);
437077218Sphk	*lenp = len;
437177218Sphk	return val;
437277218Sphk}
437377218Sphk
437477218Sphkstatic void
437577218Sphkprint_string(const u_int8_t *buf, int len)
437677218Sphk{
437777218Sphk	int i;
437877218Sphk	int hasspc;
437977218Sphk
438077218Sphk	i = 0;
438177218Sphk	hasspc = 0;
438291454Sbrooks	for (; i < len; i++) {
438377218Sphk		if (!isprint(buf[i]) && buf[i] != '\0')
438477218Sphk			break;
438577218Sphk		if (isspace(buf[i]))
438677218Sphk			hasspc++;
438777218Sphk	}
438877218Sphk	if (i == len) {
438977218Sphk		if (hasspc || len == 0 || buf[0] == '\0')
439077218Sphk			printf("\"%.*s\"", len, buf);
439177218Sphk		else
439277218Sphk			printf("%.*s", len, buf);
439377218Sphk	} else {
439477218Sphk		printf("0x");
439577218Sphk		for (i = 0; i < len; i++)
439677218Sphk			printf("%02x", buf[i]);
439777218Sphk	}
439877218Sphk}
439977218Sphk
4400178354Ssam/*
4401178354Ssam * Virtual AP cloning support.
4402178354Ssam */
4403178354Ssamstatic struct ieee80211_clone_params params = {
4404178354Ssam	.icp_opmode	= IEEE80211_M_STA,	/* default to station mode */
4405178354Ssam};
4406178354Ssam
4407178354Ssamstatic void
4408178354Ssamwlan_create(int s, struct ifreq *ifr)
4409178354Ssam{
4410178354Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
4411178354Ssam
4412178354Ssam	if (params.icp_parent[0] == '\0')
4413178354Ssam		errx(1, "must specify a parent when creating a wlan device");
4414178354Ssam	if (params.icp_opmode == IEEE80211_M_WDS &&
4415178354Ssam	    memcmp(params.icp_bssid, zerobssid, sizeof(zerobssid)) == 0)
4416178354Ssam		errx(1, "no bssid specified for WDS (use wlanbssid)");
4417178354Ssam	ifr->ifr_data = (caddr_t) &params;
4418178354Ssam	if (ioctl(s, SIOCIFCREATE2, ifr) < 0)
4419178354Ssam		err(1, "SIOCIFCREATE2");
4420178354Ssam}
4421178354Ssam
4422178354Ssamstatic
4423178354SsamDECL_CMD_FUNC(set80211clone_wlandev, arg, d)
4424178354Ssam{
4425178354Ssam	strlcpy(params.icp_parent, arg, IFNAMSIZ);
4426178354Ssam	clone_setcallback(wlan_create);
4427178354Ssam}
4428178354Ssam
4429178354Ssamstatic
4430178354SsamDECL_CMD_FUNC(set80211clone_wlanbssid, arg, d)
4431178354Ssam{
4432178354Ssam	const struct ether_addr *ea;
4433178354Ssam
4434178354Ssam	ea = ether_aton(arg);
4435178354Ssam	if (ea == NULL)
4436178354Ssam		errx(1, "%s: cannot parse bssid", arg);
4437178354Ssam	memcpy(params.icp_bssid, ea->octet, IEEE80211_ADDR_LEN);
4438178354Ssam	clone_setcallback(wlan_create);
4439178354Ssam}
4440178354Ssam
4441178354Ssamstatic
4442178354SsamDECL_CMD_FUNC(set80211clone_wlanaddr, arg, d)
4443178354Ssam{
4444178354Ssam	const struct ether_addr *ea;
4445178354Ssam
4446178354Ssam	ea = ether_aton(arg);
4447178354Ssam	if (ea == NULL)
4448178354Ssam		errx(1, "%s: cannot parse addres", arg);
4449178354Ssam	memcpy(params.icp_macaddr, ea->octet, IEEE80211_ADDR_LEN);
4450178354Ssam	params.icp_flags |= IEEE80211_CLONE_MACADDR;
4451178354Ssam	clone_setcallback(wlan_create);
4452178354Ssam}
4453178354Ssam
4454178354Ssamstatic
4455178354SsamDECL_CMD_FUNC(set80211clone_wlanmode, arg, d)
4456178354Ssam{
4457178354Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
4458178354Ssam	if (iseq(arg, "sta"))
4459178354Ssam		params.icp_opmode = IEEE80211_M_STA;
4460178354Ssam	else if (iseq(arg, "ahdemo") || iseq(arg, "adhoc-demo"))
4461178354Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
4462178354Ssam	else if (iseq(arg, "ibss") || iseq(arg, "adhoc"))
4463178354Ssam		params.icp_opmode = IEEE80211_M_IBSS;
4464178354Ssam	else if (iseq(arg, "ap") || iseq(arg, "host"))
4465178354Ssam		params.icp_opmode = IEEE80211_M_HOSTAP;
4466178354Ssam	else if (iseq(arg, "wds"))
4467178354Ssam		params.icp_opmode = IEEE80211_M_WDS;
4468178354Ssam	else if (iseq(arg, "monitor"))
4469178354Ssam		params.icp_opmode = IEEE80211_M_MONITOR;
4470178354Ssam	else
4471178354Ssam		errx(1, "Don't know to create %s for %s", arg, name);
4472178354Ssam	clone_setcallback(wlan_create);
4473178354Ssam#undef iseq
4474178354Ssam}
4475178354Ssam
4476178354Ssamstatic void
4477178354Ssamset80211clone_beacons(const char *val, int d, int s, const struct afswtch *rafp)
4478178354Ssam{
4479178354Ssam	/* NB: inverted sense */
4480178354Ssam	if (d)
4481178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_NOBEACONS;
4482178354Ssam	else
4483178354Ssam		params.icp_flags |= IEEE80211_CLONE_NOBEACONS;
4484178354Ssam	clone_setcallback(wlan_create);
4485178354Ssam}
4486178354Ssam
4487178354Ssamstatic void
4488178354Ssamset80211clone_bssid(const char *val, int d, int s, const struct afswtch *rafp)
4489178354Ssam{
4490178354Ssam	if (d)
4491178354Ssam		params.icp_flags |= IEEE80211_CLONE_BSSID;
4492178354Ssam	else
4493178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_BSSID;
4494178354Ssam	clone_setcallback(wlan_create);
4495178354Ssam}
4496178354Ssam
4497178354Ssamstatic void
4498178354Ssamset80211clone_wdslegacy(const char *val, int d, int s, const struct afswtch *rafp)
4499178354Ssam{
4500178354Ssam	if (d)
4501178354Ssam		params.icp_flags |= IEEE80211_CLONE_WDSLEGACY;
4502178354Ssam	else
4503178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_WDSLEGACY;
4504178354Ssam	clone_setcallback(wlan_create);
4505178354Ssam}
4506178354Ssam
4507138593Ssamstatic struct cmd ieee80211_cmds[] = {
4508138593Ssam	DEF_CMD_ARG("ssid",		set80211ssid),
4509138593Ssam	DEF_CMD_ARG("nwid",		set80211ssid),
4510138593Ssam	DEF_CMD_ARG("stationname",	set80211stationname),
4511138593Ssam	DEF_CMD_ARG("station",		set80211stationname),	/* BSD/OS */
4512138593Ssam	DEF_CMD_ARG("channel",		set80211channel),
4513138593Ssam	DEF_CMD_ARG("authmode",		set80211authmode),
4514138593Ssam	DEF_CMD_ARG("powersavemode",	set80211powersavemode),
4515138593Ssam	DEF_CMD("powersave",	1,	set80211powersave),
4516138593Ssam	DEF_CMD("-powersave",	0,	set80211powersave),
4517138593Ssam	DEF_CMD_ARG("powersavesleep", 	set80211powersavesleep),
4518138593Ssam	DEF_CMD_ARG("wepmode",		set80211wepmode),
4519138593Ssam	DEF_CMD("wep",		1,	set80211wep),
4520138593Ssam	DEF_CMD("-wep",		0,	set80211wep),
4521139493Ssam	DEF_CMD_ARG("deftxkey",		set80211weptxkey),
4522138593Ssam	DEF_CMD_ARG("weptxkey",		set80211weptxkey),
4523138593Ssam	DEF_CMD_ARG("wepkey",		set80211wepkey),
4524138593Ssam	DEF_CMD_ARG("nwkey",		set80211nwkey),		/* NetBSD */
4525138593Ssam	DEF_CMD("-nwkey",	0,	set80211wep),		/* NetBSD */
4526138593Ssam	DEF_CMD_ARG("rtsthreshold",	set80211rtsthreshold),
4527138593Ssam	DEF_CMD_ARG("protmode",		set80211protmode),
4528138593Ssam	DEF_CMD_ARG("txpower",		set80211txpower),
4529138593Ssam	DEF_CMD_ARG("roaming",		set80211roaming),
4530138593Ssam	DEF_CMD("wme",		1,	set80211wme),
4531138593Ssam	DEF_CMD("-wme",		0,	set80211wme),
4532178354Ssam	DEF_CMD("wmm",		1,	set80211wme),
4533178354Ssam	DEF_CMD("-wmm",		0,	set80211wme),
4534138593Ssam	DEF_CMD("hidessid",	1,	set80211hidessid),
4535138593Ssam	DEF_CMD("-hidessid",	0,	set80211hidessid),
4536138593Ssam	DEF_CMD("apbridge",	1,	set80211apbridge),
4537138593Ssam	DEF_CMD("-apbridge",	0,	set80211apbridge),
4538138593Ssam	DEF_CMD_ARG("chanlist",		set80211chanlist),
4539138593Ssam	DEF_CMD_ARG("bssid",		set80211bssid),
4540138593Ssam	DEF_CMD_ARG("ap",		set80211bssid),
4541138593Ssam	DEF_CMD("scan",	0,		set80211scan),
4542138593Ssam	DEF_CMD_ARG("list",		set80211list),
4543138593Ssam	DEF_CMD_ARG2("cwmin",		set80211cwmin),
4544138593Ssam	DEF_CMD_ARG2("cwmax",		set80211cwmax),
4545138593Ssam	DEF_CMD_ARG2("aifs",		set80211aifs),
4546138593Ssam	DEF_CMD_ARG2("txoplimit",	set80211txoplimit),
4547148621Ssam	DEF_CMD_ARG("acm",		set80211acm),
4548148621Ssam	DEF_CMD_ARG("-acm",		set80211noacm),
4549148621Ssam	DEF_CMD_ARG("ack",		set80211ackpolicy),
4550148621Ssam	DEF_CMD_ARG("-ack",		set80211noackpolicy),
4551138593Ssam	DEF_CMD_ARG2("bss:cwmin",	set80211bsscwmin),
4552138593Ssam	DEF_CMD_ARG2("bss:cwmax",	set80211bsscwmax),
4553138593Ssam	DEF_CMD_ARG2("bss:aifs",	set80211bssaifs),
4554138593Ssam	DEF_CMD_ARG2("bss:txoplimit",	set80211bsstxoplimit),
4555138593Ssam	DEF_CMD_ARG("dtimperiod",	set80211dtimperiod),
4556138593Ssam	DEF_CMD_ARG("bintval",		set80211bintval),
4557138593Ssam	DEF_CMD("mac:open",	IEEE80211_MACCMD_POLICY_OPEN,	set80211maccmd),
4558138593Ssam	DEF_CMD("mac:allow",	IEEE80211_MACCMD_POLICY_ALLOW,	set80211maccmd),
4559138593Ssam	DEF_CMD("mac:deny",	IEEE80211_MACCMD_POLICY_DENY,	set80211maccmd),
4560178354Ssam	DEF_CMD("mac:radius",	IEEE80211_MACCMD_POLICY_RADIUS,	set80211maccmd),
4561138593Ssam	DEF_CMD("mac:flush",	IEEE80211_MACCMD_FLUSH,		set80211maccmd),
4562138593Ssam	DEF_CMD("mac:detach",	IEEE80211_MACCMD_DETACH,	set80211maccmd),
4563138593Ssam	DEF_CMD_ARG("mac:add",		set80211addmac),
4564138593Ssam	DEF_CMD_ARG("mac:del",		set80211delmac),
4565138593Ssam	DEF_CMD_ARG("mac:kick",		set80211kickmac),
4566147795Ssam	DEF_CMD("pureg",	1,	set80211pureg),
4567147795Ssam	DEF_CMD("-pureg",	0,	set80211pureg),
4568170531Ssam	DEF_CMD("ff",		1,	set80211fastframes),
4569170531Ssam	DEF_CMD("-ff",		0,	set80211fastframes),
4570170531Ssam	DEF_CMD("dturbo",	1,	set80211dturbo),
4571170531Ssam	DEF_CMD("-dturbo",	0,	set80211dturbo),
4572170531Ssam	DEF_CMD("bgscan",	1,	set80211bgscan),
4573170531Ssam	DEF_CMD("-bgscan",	0,	set80211bgscan),
4574170531Ssam	DEF_CMD_ARG("bgscanidle",	set80211bgscanidle),
4575170531Ssam	DEF_CMD_ARG("bgscanintvl",	set80211bgscanintvl),
4576170531Ssam	DEF_CMD_ARG("scanvalid",	set80211scanvalid),
4577178354Ssam	DEF_CMD_ARG("roam:rssi",	set80211roamrssi),
4578178354Ssam	DEF_CMD_ARG("roam:rate",	set80211roamrate),
4579153354Ssam	DEF_CMD_ARG("mcastrate",	set80211mcastrate),
4580178354Ssam	DEF_CMD_ARG("ucastrate",	set80211ucastrate),
4581178354Ssam	DEF_CMD_ARG("mgtrate",		set80211mgtrate),
4582178354Ssam	DEF_CMD_ARG("mgmtrate",		set80211mgtrate),
4583178354Ssam	DEF_CMD_ARG("maxretry",		set80211maxretry),
4584148416Ssam	DEF_CMD_ARG("fragthreshold",	set80211fragthreshold),
4585153422Ssam	DEF_CMD("burst",	1,	set80211burst),
4586153422Ssam	DEF_CMD("-burst",	0,	set80211burst),
4587160687Ssam	DEF_CMD_ARG("bmiss",		set80211bmissthreshold),
4588160687Ssam	DEF_CMD_ARG("bmissthreshold",	set80211bmissthreshold),
4589173275Ssam	DEF_CMD("shortgi",	1,	set80211shortgi),
4590173275Ssam	DEF_CMD("-shortgi",	0,	set80211shortgi),
4591173275Ssam	DEF_CMD("ampdurx",	2,	set80211ampdu),
4592173275Ssam	DEF_CMD("-ampdurx",	-2,	set80211ampdu),
4593173275Ssam	DEF_CMD("ampdutx",	1,	set80211ampdu),
4594173275Ssam	DEF_CMD("-ampdutx",	-1,	set80211ampdu),
4595173275Ssam	DEF_CMD("ampdu",	3,	set80211ampdu),		/* NB: tx+rx */
4596173275Ssam	DEF_CMD("-ampdu",	-3,	set80211ampdu),
4597173275Ssam	DEF_CMD_ARG("ampdulimit",	set80211ampdulimit),
4598173275Ssam	DEF_CMD_ARG("ampdudensity",	set80211ampdudensity),
4599173275Ssam	DEF_CMD("amsdurx",	2,	set80211amsdu),
4600173275Ssam	DEF_CMD("-amsdurx",	-2,	set80211amsdu),
4601173275Ssam	DEF_CMD("amsdutx",	1,	set80211amsdu),
4602173275Ssam	DEF_CMD("-amsdutx",	-1,	set80211amsdu),
4603173275Ssam	DEF_CMD("amsdu",	3,	set80211amsdu),		/* NB: tx+rx */
4604173275Ssam	DEF_CMD("-amsdu",	-3,	set80211amsdu),
4605173275Ssam	DEF_CMD_ARG("amsdulimit",	set80211amsdulimit),
4606173275Ssam	DEF_CMD("puren",	1,	set80211puren),
4607173275Ssam	DEF_CMD("-puren",	0,	set80211puren),
4608170531Ssam	DEF_CMD("doth",		1,	set80211doth),
4609170531Ssam	DEF_CMD("-doth",	0,	set80211doth),
4610178354Ssam	DEF_CMD("dfs",		1,	set80211dfs),
4611178354Ssam	DEF_CMD("-dfs",		0,	set80211dfs),
4612173275Ssam	DEF_CMD("htcompat",	1,	set80211htcompat),
4613173275Ssam	DEF_CMD("-htcompat",	0,	set80211htcompat),
4614178354Ssam	DEF_CMD("dwds",		1,	set80211dwds),
4615178354Ssam	DEF_CMD("-dwds",	0,	set80211dwds),
4616173275Ssam	DEF_CMD("inact",	1,	set80211inact),
4617173275Ssam	DEF_CMD("-inact",	0,	set80211inact),
4618178354Ssam	DEF_CMD("tsn",		1,	set80211tsn),
4619178354Ssam	DEF_CMD("-tsn",		0,	set80211tsn),
4620178354Ssam	DEF_CMD_ARG("regdomain",	set80211regdomain),
4621178354Ssam	DEF_CMD_ARG("country",		set80211country),
4622178354Ssam	DEF_CMD("indoor",	'I',	set80211location),
4623178354Ssam	DEF_CMD("-indoor",	'O',	set80211location),
4624178354Ssam	DEF_CMD("outdoor",	'O',	set80211location),
4625178354Ssam	DEF_CMD("-outdoor",	'I',	set80211location),
4626178354Ssam	DEF_CMD("anywhere",	' ',	set80211location),
4627178354Ssam	DEF_CMD("ecm",		1,	set80211ecm),
4628178354Ssam	DEF_CMD("-ecm",		0,	set80211ecm),
4629178354Ssam	DEF_CMD("dotd",		1,	set80211dotd),
4630178354Ssam	DEF_CMD("-dotd",	0,	set80211dotd),
4631173275Ssam	DEF_CMD_ARG("htprotmode",	set80211htprotmode),
4632173275Ssam	DEF_CMD("ht20",		1,	set80211htconf),
4633173275Ssam	DEF_CMD("-ht20",	0,	set80211htconf),
4634173275Ssam	DEF_CMD("ht40",		3,	set80211htconf),	/* NB: 20+40 */
4635173275Ssam	DEF_CMD("-ht40",	0,	set80211htconf),
4636173275Ssam	DEF_CMD("ht",		3,	set80211htconf),	/* NB: 20+40 */
4637173275Ssam	DEF_CMD("-ht",		0,	set80211htconf),
4638183261Ssam	DEF_CMD("rifs",		1,	set80211rifs),
4639183261Ssam	DEF_CMD("-rifs",	0,	set80211rifs),
4640183261Ssam	DEF_CMD("smps",		IEEE80211_HTCAP_SMPS_ENA,	set80211smps),
4641183261Ssam	DEF_CMD("smpsdyn",	IEEE80211_HTCAP_SMPS_DYNAMIC,	set80211smps),
4642183261Ssam	DEF_CMD("-smps",	IEEE80211_HTCAP_SMPS_OFF,	set80211smps),
4643178354Ssam	/* XXX for testing */
4644178354Ssam	DEF_CMD_ARG("chanswitch",	set80211chanswitch),
4645178354Ssam
4646178354Ssam	/* vap cloning support */
4647178354Ssam	DEF_CLONE_CMD_ARG("wlanaddr",	set80211clone_wlanaddr),
4648178354Ssam	DEF_CLONE_CMD_ARG("wlanbssid",	set80211clone_wlanbssid),
4649178354Ssam	DEF_CLONE_CMD_ARG("wlandev",	set80211clone_wlandev),
4650178354Ssam	DEF_CLONE_CMD_ARG("wlanmode",	set80211clone_wlanmode),
4651178354Ssam	DEF_CLONE_CMD("beacons", 1,	set80211clone_beacons),
4652178354Ssam	DEF_CLONE_CMD("-beacons", 0,	set80211clone_beacons),
4653178354Ssam	DEF_CLONE_CMD("bssid",	1,	set80211clone_bssid),
4654178354Ssam	DEF_CLONE_CMD("-bssid",	0,	set80211clone_bssid),
4655178354Ssam	DEF_CLONE_CMD("wdslegacy", 1,	set80211clone_wdslegacy),
4656178354Ssam	DEF_CLONE_CMD("-wdslegacy", 0,	set80211clone_wdslegacy),
4657138593Ssam};
4658138593Ssamstatic struct afswtch af_ieee80211 = {
4659138593Ssam	.af_name	= "af_ieee80211",
4660138593Ssam	.af_af		= AF_UNSPEC,
4661139494Ssam	.af_other_status = ieee80211_status,
4662138593Ssam};
4663138593Ssam
4664138593Ssamstatic __constructor void
4665138593Ssamieee80211_ctor(void)
4666138593Ssam{
4667138593Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
4668138593Ssam	int i;
4669138593Ssam
4670138593Ssam	for (i = 0; i < N(ieee80211_cmds);  i++)
4671138593Ssam		cmd_register(&ieee80211_cmds[i]);
4672138593Ssam	af_register(&af_ieee80211);
4673138593Ssam#undef N
4674138593Ssam}
4675