ifieee80211.c revision 186101
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 186101 2008-12-15 01:06:49Z 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			}
1773186101Ssam			/*
1774186101Ssam			 * NB: don't enforce 1/2 and 1/4 rate channels being
1775186101Ssam			 * specified in the device's calibration list for
1776186101Ssam			 * 900MHz cards because most are not self-identifying.
1777186101Ssam			 */
1778178354Ssam			if ((flags & IEEE80211_CHAN_HALF) &&
1779186101Ssam			    ((chanFlags & IEEE80211_CHAN_HALF) == 0 &&
1780186101Ssam			     (flags & IEEE80211_CHAN_GSM) == 0)) {
1781178354Ssam				if (verbose)
1782178354Ssam					printf("%u: skip, device does not support half-rate channels\n", freq);
1783178354Ssam				continue;
1784178354Ssam			}
1785178354Ssam			if ((flags & IEEE80211_CHAN_QUARTER) &&
1786186101Ssam			    ((chanFlags & IEEE80211_CHAN_HALF) == 0 &&
1787186101Ssam			     (flags & IEEE80211_CHAN_GSM) == 0)) {
1788178354Ssam				if (verbose)
1789178354Ssam					printf("%u: skip, device does not support quarter-rate channels\n", freq);
1790178354Ssam				continue;
1791178354Ssam			}
1792178354Ssam			if ((flags & IEEE80211_CHAN_HT20) &&
1793178354Ssam			    (chanFlags & IEEE80211_CHAN_HT20) == 0) {
1794178354Ssam				if (verbose)
1795178354Ssam					printf("%u: skip, device does not support HT20 operation\n", freq);
1796178354Ssam				continue;
1797178354Ssam			}
1798178354Ssam			if ((flags & IEEE80211_CHAN_HT40) &&
1799178354Ssam			    (chanFlags & IEEE80211_CHAN_HT40) == 0) {
1800178354Ssam				if (verbose)
1801178354Ssam					printf("%u: skip, device does not support HT40 operation\n", freq);
1802178354Ssam				continue;
1803178354Ssam			}
1804178354Ssam			if ((flags & REQ_ECM) && !reg->ecm) {
1805178354Ssam				if (verbose)
1806178354Ssam					printf("%u: skip, ECM channel\n", freq);
1807178354Ssam				continue;
1808178354Ssam			}
1809178354Ssam			if ((flags & REQ_OUTDOOR) && reg->location == 'I') {
1810178354Ssam				if (verbose)
1811178354Ssam					printf("%u: skip, outdoor channel\n", freq);
1812178354Ssam				continue;
1813178354Ssam			}
1814178354Ssam			if ((flags & IEEE80211_CHAN_HT40) &&
1815178354Ssam			    prev != NULL && (freq - prev->ic_freq) < channelSep) {
1816178354Ssam				if (verbose)
1817178354Ssam					printf("%u: skip, only %u channel "
1818178354Ssam					    "separation, need %d\n", freq,
1819178354Ssam					    freq - prev->ic_freq, channelSep);
1820178354Ssam				continue;
1821178354Ssam			}
1822178354Ssam			if (ci->ic_nchans == IEEE80211_CHAN_MAX) {
1823178354Ssam				if (verbose)
1824178354Ssam					printf("%u: skip, channel table full\n", freq);
1825178354Ssam				break;
1826178354Ssam			}
1827178354Ssam			c = &ci->ic_chans[ci->ic_nchans++];
1828178354Ssam			c->ic_freq = freq;
1829178354Ssam			c->ic_flags = chanFlags |
1830178354Ssam			    (flags &~ (REQ_FLAGS | IEEE80211_CHAN_HT40));
1831178354Ssam			if (c->ic_flags & IEEE80211_CHAN_DFS)
1832178354Ssam				c->ic_maxregpower = nb->maxPowerDFS;
1833178354Ssam			else
1834178354Ssam				c->ic_maxregpower = nb->maxPower;
1835178354Ssam			if (verbose)
1836178354Ssam				printf("[%3d] add freq %u flags 0x%x power %u\n",
1837178354Ssam				    ci->ic_nchans-1, c->ic_freq, c->ic_flags,
1838178354Ssam				    c->ic_maxregpower);
1839178354Ssam			/* NB: kernel fills in other fields */
1840178354Ssam			prev = c;
1841178354Ssam		}
1842178354Ssam	}
1843178354Ssam}
1844178354Ssam
1845178354Ssamstatic void
1846178354Ssamregdomain_makechannels(
1847178354Ssam	struct ieee80211_regdomain_req *req,
1848178354Ssam	const struct ieee80211_devcaps_req *dc)
1849178354Ssam{
1850178354Ssam	struct regdata *rdp = getregdata();
1851178354Ssam	const struct country *cc;
1852178354Ssam	const struct ieee80211_regdomain *reg = &req->rd;
1853178354Ssam	struct ieee80211req_chaninfo *ci = &req->chaninfo;
1854178354Ssam	const struct regdomain *rd;
1855178354Ssam
1856178354Ssam	/*
1857178354Ssam	 * Locate construction table for new channel list.  We treat
1858178354Ssam	 * the regdomain/SKU as definitive so a country can be in
1859178354Ssam	 * multiple with different properties (e.g. US in FCC+FCC3).
1860178354Ssam	 * If no regdomain is specified then we fallback on the country
1861178354Ssam	 * code to find the associated regdomain since countries always
1862178354Ssam	 * belong to at least one regdomain.
1863178354Ssam	 */
1864178354Ssam	if (reg->regdomain == 0) {
1865178354Ssam		cc = lib80211_country_findbycc(rdp, reg->country);
1866178354Ssam		if (cc == NULL)
1867178354Ssam			errx(1, "internal error, country %d not found",
1868178354Ssam			    reg->country);
1869178354Ssam		rd = cc->rd;
1870178354Ssam	} else
1871178354Ssam		rd = lib80211_regdomain_findbysku(rdp, reg->regdomain);
1872178354Ssam	if (rd == NULL)
1873178354Ssam		errx(1, "internal error, regdomain %d not found",
1874178354Ssam			    reg->regdomain);
1875178354Ssam	if (rd->sku != SKU_DEBUG) {
1876178354Ssam		memset(ci, 0, sizeof(*ci));
1877178354Ssam		if (!LIST_EMPTY(&rd->bands_11b))
1878178354Ssam			regdomain_addchans(ci, &rd->bands_11b, reg,
1879178354Ssam			    IEEE80211_CHAN_B, &dc->dc_chaninfo);
1880178354Ssam		if (!LIST_EMPTY(&rd->bands_11g))
1881178354Ssam			regdomain_addchans(ci, &rd->bands_11g, reg,
1882178354Ssam			    IEEE80211_CHAN_G, &dc->dc_chaninfo);
1883178354Ssam		if (!LIST_EMPTY(&rd->bands_11a))
1884178354Ssam			regdomain_addchans(ci, &rd->bands_11a, reg,
1885178354Ssam			    IEEE80211_CHAN_A, &dc->dc_chaninfo);
1886178354Ssam		if (!LIST_EMPTY(&rd->bands_11na)) {
1887178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
1888178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT20,
1889178354Ssam			    &dc->dc_chaninfo);
1890178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
1891178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U,
1892178354Ssam			    &dc->dc_chaninfo);
1893178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
1894178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D,
1895178354Ssam			    &dc->dc_chaninfo);
1896178354Ssam		}
1897178354Ssam		if (!LIST_EMPTY(&rd->bands_11ng)) {
1898178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
1899178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT20,
1900178354Ssam			    &dc->dc_chaninfo);
1901178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
1902178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U,
1903178354Ssam			    &dc->dc_chaninfo);
1904178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
1905178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D,
1906178354Ssam			    &dc->dc_chaninfo);
1907178354Ssam		}
1908178354Ssam		qsort(ci->ic_chans, ci->ic_nchans, sizeof(ci->ic_chans[0]),
1909178354Ssam		    regdomain_sort);
1910178354Ssam	} else
1911178354Ssam		*ci = dc->dc_chaninfo;
1912178354Ssam}
1913178354Ssam
1914178354Ssamstatic void
1915178354Ssamlist_countries(void)
1916178354Ssam{
1917178354Ssam	struct regdata *rdp = getregdata();
1918178354Ssam	const struct country *cp;
1919178354Ssam	const struct regdomain *dp;
1920178354Ssam	int i;
1921178354Ssam
1922178354Ssam	i = 0;
1923178354Ssam	printf("\nCountry codes:\n");
1924178354Ssam	LIST_FOREACH(cp, &rdp->countries, next) {
1925178354Ssam		printf("%2s %-15.15s%s", cp->isoname,
1926178354Ssam		    cp->name, ((i+1)%4) == 0 ? "\n" : " ");
1927178354Ssam		i++;
1928178354Ssam	}
1929178354Ssam	i = 0;
1930178354Ssam	printf("\nRegulatory domains:\n");
1931178354Ssam	LIST_FOREACH(dp, &rdp->domains, next) {
1932178354Ssam		printf("%-15.15s%s", dp->name, ((i+1)%4) == 0 ? "\n" : " ");
1933178354Ssam		i++;
1934178354Ssam	}
1935178354Ssam	printf("\n");
1936178354Ssam}
1937178354Ssam
1938178354Ssamstatic void
1939178354Ssamdefaultcountry(const struct regdomain *rd)
1940178354Ssam{
1941178354Ssam	struct regdata *rdp = getregdata();
1942178354Ssam	const struct country *cc;
1943178354Ssam
1944178354Ssam	cc = lib80211_country_findbycc(rdp, rd->cc->code);
1945178354Ssam	if (cc == NULL)
1946178354Ssam		errx(1, "internal error, ISO country code %d not "
1947178354Ssam		    "defined for regdomain %s", rd->cc->code, rd->name);
1948178354Ssam	regdomain.country = cc->code;
1949178354Ssam	regdomain.isocc[0] = cc->isoname[0];
1950178354Ssam	regdomain.isocc[1] = cc->isoname[1];
1951178354Ssam}
1952178354Ssam
1953178354Ssamstatic
1954178354SsamDECL_CMD_FUNC(set80211regdomain, val, d)
1955178354Ssam{
1956178354Ssam	struct regdata *rdp = getregdata();
1957178354Ssam	const struct regdomain *rd;
1958178354Ssam
1959178354Ssam	rd = lib80211_regdomain_findbyname(rdp, val);
1960178354Ssam	if (rd == NULL) {
1961178354Ssam		rd = lib80211_regdomain_findbysku(rdp, atoi(val));
1962178354Ssam		if (rd == NULL)
1963178354Ssam			errx(1, "unknown regdomain %s", val);
1964178354Ssam	}
1965178354Ssam	getregdomain(s);
1966178354Ssam	regdomain.regdomain = rd->sku;
1967178354Ssam	if (regdomain.country == 0 && rd->cc != NULL) {
1968178354Ssam		/*
1969178354Ssam		 * No country code setup and there's a default
1970178354Ssam		 * one for this regdomain fill it in.
1971178354Ssam		 */
1972178354Ssam		defaultcountry(rd);
1973178354Ssam	}
1974178354Ssam	callback_register(setregdomain_cb, &regdomain);
1975178354Ssam}
1976178354Ssam
1977178354Ssamstatic
1978178354SsamDECL_CMD_FUNC(set80211country, val, d)
1979178354Ssam{
1980178354Ssam	struct regdata *rdp = getregdata();
1981178354Ssam	const struct country *cc;
1982178354Ssam
1983178354Ssam	cc = lib80211_country_findbyname(rdp, val);
1984178354Ssam	if (cc == NULL) {
1985178354Ssam		cc = lib80211_country_findbycc(rdp, atoi(val));
1986178354Ssam		if (cc == NULL)
1987178354Ssam			errx(1, "unknown ISO country code %s", val);
1988178354Ssam	}
1989178354Ssam	getregdomain(s);
1990178354Ssam	regdomain.regdomain = cc->rd->sku;
1991178354Ssam	regdomain.country = cc->code;
1992178354Ssam	regdomain.isocc[0] = cc->isoname[0];
1993178354Ssam	regdomain.isocc[1] = cc->isoname[1];
1994178354Ssam	callback_register(setregdomain_cb, &regdomain);
1995178354Ssam}
1996178354Ssam
1997178354Ssamstatic void
1998178354Ssamset80211location(const char *val, int d, int s, const struct afswtch *rafp)
1999178354Ssam{
2000178354Ssam	getregdomain(s);
2001178354Ssam	regdomain.location = d;
2002178354Ssam	callback_register(setregdomain_cb, &regdomain);
2003178354Ssam}
2004178354Ssam
2005178354Ssamstatic void
2006178354Ssamset80211ecm(const char *val, int d, int s, const struct afswtch *rafp)
2007178354Ssam{
2008178354Ssam	getregdomain(s);
2009178354Ssam	regdomain.ecm = d;
2010178354Ssam	callback_register(setregdomain_cb, &regdomain);
2011178354Ssam}
2012178354Ssam
2013178354Ssamstatic void
2014173275SsamLINE_INIT(char c)
2015173275Ssam{
2016173275Ssam	spacer = c;
2017173275Ssam	if (c == '\t')
2018173275Ssam		col = 8;
2019173275Ssam	else
2020173275Ssam		col = 1;
2021173275Ssam}
2022173275Ssam
2023173275Ssamstatic void
2024173275SsamLINE_BREAK(void)
2025173275Ssam{
2026173275Ssam	if (spacer != '\t') {
2027173275Ssam		printf("\n");
2028173275Ssam		spacer = '\t';
2029173275Ssam	}
2030173275Ssam	col = 8;		/* 8-col tab */
2031173275Ssam}
2032173275Ssam
2033173275Ssamstatic void
2034173275SsamLINE_CHECK(const char *fmt, ...)
2035173275Ssam{
2036173275Ssam	char buf[80];
2037173275Ssam	va_list ap;
2038173275Ssam	int n;
2039173275Ssam
2040173275Ssam	va_start(ap, fmt);
2041173275Ssam	n = vsnprintf(buf+1, sizeof(buf)-1, fmt, ap);
2042173275Ssam	va_end(ap);
2043173275Ssam	col += 1+n;
2044173275Ssam	if (col > MAXCOL) {
2045173275Ssam		LINE_BREAK();
2046173275Ssam		col += n;
2047173275Ssam	}
2048173275Ssam	buf[0] = spacer;
2049173275Ssam	printf("%s", buf);
2050173275Ssam	spacer = ' ';
2051173275Ssam}
2052173275Ssam
2053138593Ssamstatic int
2054170531Ssamgetmaxrate(const uint8_t rates[15], uint8_t nrates)
2055138593Ssam{
2056138593Ssam	int i, maxrate = -1;
2057138593Ssam
2058138593Ssam	for (i = 0; i < nrates; i++) {
2059138593Ssam		int rate = rates[i] & IEEE80211_RATE_VAL;
2060138593Ssam		if (rate > maxrate)
2061138593Ssam			maxrate = rate;
2062138593Ssam	}
2063138593Ssam	return maxrate / 2;
2064138593Ssam}
2065138593Ssam
2066138593Ssamstatic const char *
2067138593Ssamgetcaps(int capinfo)
2068138593Ssam{
2069138593Ssam	static char capstring[32];
2070138593Ssam	char *cp = capstring;
2071138593Ssam
2072138593Ssam	if (capinfo & IEEE80211_CAPINFO_ESS)
2073138593Ssam		*cp++ = 'E';
2074138593Ssam	if (capinfo & IEEE80211_CAPINFO_IBSS)
2075138593Ssam		*cp++ = 'I';
2076138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
2077138593Ssam		*cp++ = 'c';
2078138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
2079138593Ssam		*cp++ = 'C';
2080138593Ssam	if (capinfo & IEEE80211_CAPINFO_PRIVACY)
2081138593Ssam		*cp++ = 'P';
2082138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
2083138593Ssam		*cp++ = 'S';
2084138593Ssam	if (capinfo & IEEE80211_CAPINFO_PBCC)
2085138593Ssam		*cp++ = 'B';
2086138593Ssam	if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
2087138593Ssam		*cp++ = 'A';
2088138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
2089138593Ssam		*cp++ = 's';
2090138593Ssam	if (capinfo & IEEE80211_CAPINFO_RSN)
2091138593Ssam		*cp++ = 'R';
2092138593Ssam	if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
2093138593Ssam		*cp++ = 'D';
2094138593Ssam	*cp = '\0';
2095138593Ssam	return capstring;
2096138593Ssam}
2097138593Ssam
2098159885Ssamstatic const char *
2099159885Ssamgetflags(int flags)
2100159885Ssam{
2101159885Ssam	static char flagstring[32];
2102159885Ssam	char *cp = flagstring;
2103159885Ssam
2104159885Ssam	if (flags & IEEE80211_NODE_AUTH)
2105159885Ssam		*cp++ = 'A';
2106159885Ssam	if (flags & IEEE80211_NODE_QOS)
2107159885Ssam		*cp++ = 'Q';
2108159885Ssam	if (flags & IEEE80211_NODE_ERP)
2109159885Ssam		*cp++ = 'E';
2110159885Ssam	if (flags & IEEE80211_NODE_PWR_MGT)
2111159885Ssam		*cp++ = 'P';
2112173275Ssam	if (flags & IEEE80211_NODE_HT) {
2113170531Ssam		*cp++ = 'H';
2114173275Ssam		if (flags & IEEE80211_NODE_HTCOMPAT)
2115173275Ssam			*cp++ = '+';
2116173275Ssam	}
2117173275Ssam	if (flags & IEEE80211_NODE_WPS)
2118173275Ssam		*cp++ = 'W';
2119173275Ssam	if (flags & IEEE80211_NODE_TSN)
2120183261Ssam		*cp++ = 'N';
2121183261Ssam	if (flags & IEEE80211_NODE_AMPDU_TX)
2122173275Ssam		*cp++ = 'T';
2123183261Ssam	if (flags & IEEE80211_NODE_AMPDU_RX)
2124183261Ssam		*cp++ = 'R';
2125183261Ssam	if (flags & IEEE80211_NODE_MIMO_PS) {
2126183261Ssam		*cp++ = 'M';
2127183261Ssam		if (flags & IEEE80211_NODE_MIMO_RTS)
2128183261Ssam			*cp++ = '+';
2129183261Ssam	}
2130183261Ssam	if (flags & IEEE80211_NODE_RIFS)
2131183261Ssam		*cp++ = 'I';
2132159885Ssam	*cp = '\0';
2133159885Ssam	return flagstring;
2134159885Ssam}
2135159885Ssam
2136138593Ssamstatic void
2137138593Ssamprintie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
2138138593Ssam{
2139138593Ssam	printf("%s", tag);
2140138593Ssam	if (verbose) {
2141138593Ssam		maxlen -= strlen(tag)+2;
2142138593Ssam		if (2*ielen > maxlen)
2143138593Ssam			maxlen--;
2144138593Ssam		printf("<");
2145138593Ssam		for (; ielen > 0; ie++, ielen--) {
2146138593Ssam			if (maxlen-- <= 0)
2147138593Ssam				break;
2148138593Ssam			printf("%02x", *ie);
2149138593Ssam		}
2150138593Ssam		if (ielen != 0)
2151138593Ssam			printf("-");
2152138593Ssam		printf(">");
2153138593Ssam	}
2154138593Ssam}
2155138593Ssam
2156170531Ssam#define LE_READ_2(p)					\
2157170531Ssam	((u_int16_t)					\
2158170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2159170531Ssam	  (((const u_int8_t *)(p))[1] <<  8)))
2160170531Ssam#define LE_READ_4(p)					\
2161170531Ssam	((u_int32_t)					\
2162170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2163170531Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
2164170531Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
2165170531Ssam	  (((const u_int8_t *)(p))[3] << 24)))
2166170531Ssam
2167138593Ssam/*
2168170531Ssam * NB: The decoding routines assume a properly formatted ie
2169170531Ssam *     which should be safe as the kernel only retains them
2170170531Ssam *     if they parse ok.
2171170531Ssam */
2172170531Ssam
2173170531Ssamstatic void
2174173275Ssamprintwmeparam(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2175170531Ssam{
2176170531Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
2177170531Ssam	static const char *acnames[] = { "BE", "BK", "VO", "VI" };
2178173275Ssam	const struct ieee80211_wme_param *wme =
2179173275Ssam	    (const struct ieee80211_wme_param *) ie;
2180170531Ssam	int i;
2181170531Ssam
2182170531Ssam	printf("%s", tag);
2183173275Ssam	if (!verbose)
2184173275Ssam		return;
2185173275Ssam	printf("<qosinfo 0x%x", wme->param_qosInfo);
2186173275Ssam	ie += offsetof(struct ieee80211_wme_param, params_acParams);
2187173275Ssam	for (i = 0; i < WME_NUM_AC; i++) {
2188173275Ssam		const struct ieee80211_wme_acparams *ac =
2189173275Ssam		    &wme->params_acParams[i];
2190173275Ssam
2191173275Ssam		printf(" %s[%saifsn %u cwmin %u cwmax %u txop %u]"
2192173275Ssam			, acnames[i]
2193173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_ACM) ? "acm " : ""
2194173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_AIFSN)
2195173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMIN)
2196173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMAX)
2197173275Ssam			, LE_READ_2(&ac->acp_txop)
2198173275Ssam		);
2199170531Ssam	}
2200173275Ssam	printf(">");
2201170531Ssam#undef MS
2202170531Ssam}
2203170531Ssam
2204170531Ssamstatic void
2205173275Ssamprintwmeinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2206173275Ssam{
2207173275Ssam	printf("%s", tag);
2208173275Ssam	if (verbose) {
2209173275Ssam		const struct ieee80211_wme_info *wme =
2210173275Ssam		    (const struct ieee80211_wme_info *) ie;
2211173275Ssam		printf("<version 0x%x info 0x%x>",
2212173275Ssam		    wme->wme_version, wme->wme_info);
2213173275Ssam	}
2214173275Ssam}
2215173275Ssam
2216173275Ssamstatic void
2217173275Ssamprinthtcap(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2218173275Ssam{
2219173275Ssam	printf("%s", tag);
2220173275Ssam	if (verbose) {
2221173275Ssam		const struct ieee80211_ie_htcap *htcap =
2222173275Ssam		    (const struct ieee80211_ie_htcap *) ie;
2223173275Ssam		const char *sep;
2224173275Ssam		int i, j;
2225173275Ssam
2226173275Ssam		printf("<cap 0x%x param 0x%x",
2227173275Ssam		    LE_READ_2(&htcap->hc_cap), htcap->hc_param);
2228173275Ssam		printf(" mcsset[");
2229173275Ssam		sep = "";
2230173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2231173275Ssam			if (isset(htcap->hc_mcsset, i)) {
2232173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2233173275Ssam					if (isclr(htcap->hc_mcsset, j))
2234173275Ssam						break;
2235173275Ssam				j--;
2236173275Ssam				if (i == j)
2237173275Ssam					printf("%s%u", sep, i);
2238173275Ssam				else
2239173275Ssam					printf("%s%u-%u", sep, i, j);
2240173275Ssam				i += j-i;
2241173275Ssam				sep = ",";
2242173275Ssam			}
2243173275Ssam		printf("] extcap 0x%x txbf 0x%x antenna 0x%x>",
2244173275Ssam		    LE_READ_2(&htcap->hc_extcap),
2245173275Ssam		    LE_READ_4(&htcap->hc_txbf),
2246173275Ssam		    htcap->hc_antenna);
2247173275Ssam	}
2248173275Ssam}
2249173275Ssam
2250173275Ssamstatic void
2251173275Ssamprinthtinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2252173275Ssam{
2253173275Ssam	printf("%s", tag);
2254173275Ssam	if (verbose) {
2255173275Ssam		const struct ieee80211_ie_htinfo *htinfo =
2256173275Ssam		    (const struct ieee80211_ie_htinfo *) ie;
2257173275Ssam		const char *sep;
2258173275Ssam		int i, j;
2259173275Ssam
2260173275Ssam		printf("<ctl %u, %x,%x,%x,%x", htinfo->hi_ctrlchannel,
2261173275Ssam		    htinfo->hi_byte1, htinfo->hi_byte2, htinfo->hi_byte3,
2262173275Ssam		    LE_READ_2(&htinfo->hi_byte45));
2263173275Ssam		printf(" basicmcs[");
2264173275Ssam		sep = "";
2265173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2266173275Ssam			if (isset(htinfo->hi_basicmcsset, i)) {
2267173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2268173275Ssam					if (isclr(htinfo->hi_basicmcsset, j))
2269173275Ssam						break;
2270173275Ssam				j--;
2271173275Ssam				if (i == j)
2272173275Ssam					printf("%s%u", sep, i);
2273173275Ssam				else
2274173275Ssam					printf("%s%u-%u", sep, i, j);
2275173275Ssam				i += j-i;
2276173275Ssam				sep = ",";
2277173275Ssam			}
2278173275Ssam		printf("]>");
2279173275Ssam	}
2280173275Ssam}
2281173275Ssam
2282173275Ssamstatic void
2283170531Ssamprintathie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2284170531Ssam{
2285170531Ssam
2286170531Ssam	printf("%s", tag);
2287170531Ssam	if (verbose) {
2288170531Ssam		const struct ieee80211_ath_ie *ath =
2289170531Ssam			(const struct ieee80211_ath_ie *)ie;
2290170531Ssam
2291170531Ssam		printf("<");
2292170531Ssam		if (ath->ath_capability & ATHEROS_CAP_TURBO_PRIME)
2293170531Ssam			printf("DTURBO,");
2294170531Ssam		if (ath->ath_capability & ATHEROS_CAP_COMPRESSION)
2295170531Ssam			printf("COMP,");
2296170531Ssam		if (ath->ath_capability & ATHEROS_CAP_FAST_FRAME)
2297170531Ssam			printf("FF,");
2298170531Ssam		if (ath->ath_capability & ATHEROS_CAP_XR)
2299170531Ssam			printf("XR,");
2300170531Ssam		if (ath->ath_capability & ATHEROS_CAP_AR)
2301170531Ssam			printf("AR,");
2302170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BURST)
2303170531Ssam			printf("BURST,");
2304170531Ssam		if (ath->ath_capability & ATHEROS_CAP_WME)
2305170531Ssam			printf("WME,");
2306170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BOOST)
2307170531Ssam			printf("BOOST,");
2308170531Ssam		printf("0x%x>", LE_READ_2(ath->ath_defkeyix));
2309170531Ssam	}
2310170531Ssam}
2311170531Ssam
2312170531Ssamstatic const char *
2313170531Ssamwpa_cipher(const u_int8_t *sel)
2314170531Ssam{
2315170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2316170531Ssam	u_int32_t w = LE_READ_4(sel);
2317170531Ssam
2318170531Ssam	switch (w) {
2319170531Ssam	case WPA_SEL(WPA_CSE_NULL):
2320170531Ssam		return "NONE";
2321170531Ssam	case WPA_SEL(WPA_CSE_WEP40):
2322170531Ssam		return "WEP40";
2323170531Ssam	case WPA_SEL(WPA_CSE_WEP104):
2324170531Ssam		return "WEP104";
2325170531Ssam	case WPA_SEL(WPA_CSE_TKIP):
2326170531Ssam		return "TKIP";
2327170531Ssam	case WPA_SEL(WPA_CSE_CCMP):
2328170531Ssam		return "AES-CCMP";
2329170531Ssam	}
2330170531Ssam	return "?";		/* NB: so 1<< is discarded */
2331170531Ssam#undef WPA_SEL
2332170531Ssam}
2333170531Ssam
2334170531Ssamstatic const char *
2335170531Ssamwpa_keymgmt(const u_int8_t *sel)
2336170531Ssam{
2337170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2338170531Ssam	u_int32_t w = LE_READ_4(sel);
2339170531Ssam
2340170531Ssam	switch (w) {
2341170531Ssam	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
2342170531Ssam		return "8021X-UNSPEC";
2343170531Ssam	case WPA_SEL(WPA_ASE_8021X_PSK):
2344170531Ssam		return "8021X-PSK";
2345170531Ssam	case WPA_SEL(WPA_ASE_NONE):
2346170531Ssam		return "NONE";
2347170531Ssam	}
2348170531Ssam	return "?";
2349170531Ssam#undef WPA_SEL
2350170531Ssam}
2351170531Ssam
2352170531Ssamstatic void
2353170531Ssamprintwpaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2354170531Ssam{
2355170531Ssam	u_int8_t len = ie[1];
2356170531Ssam
2357170531Ssam	printf("%s", tag);
2358170531Ssam	if (verbose) {
2359170531Ssam		const char *sep;
2360170531Ssam		int n;
2361170531Ssam
2362170531Ssam		ie += 6, len -= 4;		/* NB: len is payload only */
2363170531Ssam
2364170531Ssam		printf("<v%u", LE_READ_2(ie));
2365170531Ssam		ie += 2, len -= 2;
2366170531Ssam
2367170531Ssam		printf(" mc:%s", wpa_cipher(ie));
2368170531Ssam		ie += 4, len -= 4;
2369170531Ssam
2370170531Ssam		/* unicast ciphers */
2371170531Ssam		n = LE_READ_2(ie);
2372170531Ssam		ie += 2, len -= 2;
2373170531Ssam		sep = " uc:";
2374170531Ssam		for (; n > 0; n--) {
2375170531Ssam			printf("%s%s", sep, wpa_cipher(ie));
2376170531Ssam			ie += 4, len -= 4;
2377170531Ssam			sep = "+";
2378170531Ssam		}
2379170531Ssam
2380170531Ssam		/* key management algorithms */
2381170531Ssam		n = LE_READ_2(ie);
2382170531Ssam		ie += 2, len -= 2;
2383170531Ssam		sep = " km:";
2384170531Ssam		for (; n > 0; n--) {
2385170531Ssam			printf("%s%s", sep, wpa_keymgmt(ie));
2386170531Ssam			ie += 4, len -= 4;
2387170531Ssam			sep = "+";
2388170531Ssam		}
2389170531Ssam
2390170531Ssam		if (len > 2)		/* optional capabilities */
2391170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2392170531Ssam		printf(">");
2393170531Ssam	}
2394170531Ssam}
2395170531Ssam
2396170531Ssamstatic const char *
2397170531Ssamrsn_cipher(const u_int8_t *sel)
2398170531Ssam{
2399170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2400170531Ssam	u_int32_t w = LE_READ_4(sel);
2401170531Ssam
2402170531Ssam	switch (w) {
2403170531Ssam	case RSN_SEL(RSN_CSE_NULL):
2404170531Ssam		return "NONE";
2405170531Ssam	case RSN_SEL(RSN_CSE_WEP40):
2406170531Ssam		return "WEP40";
2407170531Ssam	case RSN_SEL(RSN_CSE_WEP104):
2408170531Ssam		return "WEP104";
2409170531Ssam	case RSN_SEL(RSN_CSE_TKIP):
2410170531Ssam		return "TKIP";
2411170531Ssam	case RSN_SEL(RSN_CSE_CCMP):
2412170531Ssam		return "AES-CCMP";
2413170531Ssam	case RSN_SEL(RSN_CSE_WRAP):
2414170531Ssam		return "AES-OCB";
2415170531Ssam	}
2416170531Ssam	return "?";
2417170531Ssam#undef WPA_SEL
2418170531Ssam}
2419170531Ssam
2420170531Ssamstatic const char *
2421170531Ssamrsn_keymgmt(const u_int8_t *sel)
2422170531Ssam{
2423170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2424170531Ssam	u_int32_t w = LE_READ_4(sel);
2425170531Ssam
2426170531Ssam	switch (w) {
2427170531Ssam	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
2428170531Ssam		return "8021X-UNSPEC";
2429170531Ssam	case RSN_SEL(RSN_ASE_8021X_PSK):
2430170531Ssam		return "8021X-PSK";
2431170531Ssam	case RSN_SEL(RSN_ASE_NONE):
2432170531Ssam		return "NONE";
2433170531Ssam	}
2434170531Ssam	return "?";
2435170531Ssam#undef RSN_SEL
2436170531Ssam}
2437170531Ssam
2438170531Ssamstatic void
2439170531Ssamprintrsnie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2440170531Ssam{
2441170531Ssam	printf("%s", tag);
2442170531Ssam	if (verbose) {
2443170531Ssam		const char *sep;
2444170531Ssam		int n;
2445170531Ssam
2446173275Ssam		ie += 2, ielen -= 2;
2447170531Ssam
2448170531Ssam		printf("<v%u", LE_READ_2(ie));
2449173275Ssam		ie += 2, ielen -= 2;
2450170531Ssam
2451170531Ssam		printf(" mc:%s", rsn_cipher(ie));
2452173275Ssam		ie += 4, ielen -= 4;
2453170531Ssam
2454170531Ssam		/* unicast ciphers */
2455170531Ssam		n = LE_READ_2(ie);
2456173275Ssam		ie += 2, ielen -= 2;
2457170531Ssam		sep = " uc:";
2458170531Ssam		for (; n > 0; n--) {
2459170531Ssam			printf("%s%s", sep, rsn_cipher(ie));
2460173275Ssam			ie += 4, ielen -= 4;
2461170531Ssam			sep = "+";
2462170531Ssam		}
2463170531Ssam
2464170531Ssam		/* key management algorithms */
2465170531Ssam		n = LE_READ_2(ie);
2466173275Ssam		ie += 2, ielen -= 2;
2467170531Ssam		sep = " km:";
2468170531Ssam		for (; n > 0; n--) {
2469170531Ssam			printf("%s%s", sep, rsn_keymgmt(ie));
2470173275Ssam			ie += 4, ielen -= 4;
2471170531Ssam			sep = "+";
2472170531Ssam		}
2473170531Ssam
2474173275Ssam		if (ielen > 2)		/* optional capabilities */
2475170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2476170531Ssam		/* XXXPMKID */
2477170531Ssam		printf(">");
2478170531Ssam	}
2479170531Ssam}
2480170531Ssam
2481181454Ssam/* XXX move to a public include file */
2482181454Ssam#define IEEE80211_WPS_DEV_PASS_ID	0x1012
2483181454Ssam#define IEEE80211_WPS_SELECTED_REG	0x1041
2484181454Ssam#define IEEE80211_WPS_SETUP_STATE	0x1044
2485181454Ssam#define IEEE80211_WPS_UUID_E		0x1047
2486181454Ssam#define IEEE80211_WPS_VERSION		0x104a
2487181454Ssam
2488181454Ssam#define BE_READ_2(p)					\
2489181454Ssam	((u_int16_t)					\
2490181454Ssam	 ((((const u_int8_t *)(p))[1]      ) |		\
2491181454Ssam	  (((const u_int8_t *)(p))[0] <<  8)))
2492181454Ssam
2493181454Ssamstatic void
2494181454Ssamprintwpsie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2495181454Ssam{
2496181454Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
2497181454Ssam	u_int8_t len = ie[1];
2498181454Ssam
2499181454Ssam	printf("%s", tag);
2500181454Ssam	if (verbose) {
2501181454Ssam		static const char *dev_pass_id[] = {
2502181454Ssam			"D",	/* Default (PIN) */
2503181454Ssam			"U",	/* User-specified */
2504181454Ssam			"M",	/* Machine-specified */
2505181454Ssam			"K",	/* Rekey */
2506181454Ssam			"P",	/* PushButton */
2507181454Ssam			"R"	/* Registrar-specified */
2508181454Ssam		};
2509181454Ssam		int n;
2510181454Ssam
2511181454Ssam		ie +=6, len -= 4;		/* NB: len is payload only */
2512181454Ssam
2513181454Ssam		/* WPS IE in Beacon and Probe Resp frames have different fields */
2514181454Ssam		printf("<");
2515181454Ssam		while (len) {
2516181454Ssam			uint16_t tlv_type = BE_READ_2(ie);
2517181454Ssam			uint16_t tlv_len  = BE_READ_2(ie + 2);
2518181454Ssam
2519181454Ssam			ie += 4, len -= 4;
2520181454Ssam
2521181454Ssam			switch (tlv_type) {
2522181454Ssam			case IEEE80211_WPS_VERSION:
2523181454Ssam				printf("v:%d.%d", *ie >> 4, *ie & 0xf);
2524181454Ssam				break;
2525181454Ssam			case IEEE80211_WPS_SETUP_STATE:
2526181454Ssam				/* Only 1 and 2 are valid */
2527181454Ssam				if (*ie == 0 || *ie >= 3)
2528181454Ssam					printf(" state:B");
2529181454Ssam				else
2530181454Ssam					printf(" st:%s", *ie == 1 ? "N" : "C");
2531181454Ssam				break;
2532181454Ssam			case IEEE80211_WPS_SELECTED_REG:
2533181454Ssam				printf(" sel:%s", *ie ? "T" : "F");
2534181454Ssam				break;
2535181454Ssam			case IEEE80211_WPS_DEV_PASS_ID:
2536181454Ssam				n = LE_READ_2(ie);
2537181454Ssam				if (n < N(dev_pass_id))
2538181454Ssam					printf(" dpi:%s", dev_pass_id[n]);
2539181454Ssam				break;
2540181454Ssam			case IEEE80211_WPS_UUID_E:
2541181454Ssam				printf(" uuid-e:");
2542181454Ssam				for (n = 0; n < (tlv_len - 1); n++)
2543181454Ssam					printf("%02x-", ie[n]);
2544181454Ssam				printf("%02x", ie[n]);
2545181454Ssam				break;
2546181454Ssam			}
2547181454Ssam			ie += tlv_len, len -= tlv_len;
2548181454Ssam		}
2549181454Ssam		printf(">");
2550181454Ssam	}
2551181454Ssam#undef N
2552181454Ssam}
2553181454Ssam
2554170531Ssam/*
2555138593Ssam * Copy the ssid string contents into buf, truncating to fit.  If the
2556138593Ssam * ssid is entirely printable then just copy intact.  Otherwise convert
2557138593Ssam * to hexadecimal.  If the result is truncated then replace the last
2558138593Ssam * three characters with "...".
2559138593Ssam */
2560146873Sjhbstatic int
2561138593Ssamcopy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
2562138593Ssam{
2563138593Ssam	const u_int8_t *p;
2564138593Ssam	size_t maxlen;
2565138593Ssam	int i;
2566138593Ssam
2567138593Ssam	if (essid_len > bufsize)
2568138593Ssam		maxlen = bufsize;
2569138593Ssam	else
2570138593Ssam		maxlen = essid_len;
2571138593Ssam	/* determine printable or not */
2572138593Ssam	for (i = 0, p = essid; i < maxlen; i++, p++) {
2573138593Ssam		if (*p < ' ' || *p > 0x7e)
2574138593Ssam			break;
2575138593Ssam	}
2576138593Ssam	if (i != maxlen) {		/* not printable, print as hex */
2577138593Ssam		if (bufsize < 3)
2578138593Ssam			return 0;
2579138593Ssam		strlcpy(buf, "0x", bufsize);
2580138593Ssam		bufsize -= 2;
2581138593Ssam		p = essid;
2582138593Ssam		for (i = 0; i < maxlen && bufsize >= 2; i++) {
2583147489Savatar			sprintf(&buf[2+2*i], "%02x", p[i]);
2584138593Ssam			bufsize -= 2;
2585138593Ssam		}
2586147489Savatar		if (i != essid_len)
2587147489Savatar			memcpy(&buf[2+2*i-3], "...", 3);
2588138593Ssam	} else {			/* printable, truncate as needed */
2589138593Ssam		memcpy(buf, essid, maxlen);
2590147489Savatar		if (maxlen != essid_len)
2591147489Savatar			memcpy(&buf[maxlen-3], "...", 3);
2592138593Ssam	}
2593138593Ssam	return maxlen;
2594138593Ssam}
2595138593Ssam
2596173275Ssamstatic void
2597173275Ssamprintssid(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2598173275Ssam{
2599173275Ssam	char ssid[2*IEEE80211_NWID_LEN+1];
2600173275Ssam
2601173275Ssam	printf("%s<%.*s>", tag, copy_essid(ssid, maxlen, ie+2, ie[1]), ssid);
2602173275Ssam}
2603173275Ssam
2604173275Ssamstatic void
2605173275Ssamprintrates(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2606173275Ssam{
2607173275Ssam	const char *sep;
2608173275Ssam	int i;
2609173275Ssam
2610173275Ssam	printf("%s", tag);
2611173275Ssam	sep = "<";
2612173275Ssam	for (i = 2; i < ielen; i++) {
2613173275Ssam		printf("%s%s%d", sep,
2614173275Ssam		    ie[i] & IEEE80211_RATE_BASIC ? "B" : "",
2615173275Ssam		    ie[i] & IEEE80211_RATE_VAL);
2616173275Ssam		sep = ",";
2617173275Ssam	}
2618173275Ssam	printf(">");
2619173275Ssam}
2620173275Ssam
2621173275Ssamstatic void
2622173275Ssamprintcountry(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2623173275Ssam{
2624173275Ssam	const struct ieee80211_country_ie *cie =
2625173275Ssam	   (const struct ieee80211_country_ie *) ie;
2626173275Ssam	int i, nbands, schan, nchan;
2627173275Ssam
2628173275Ssam	printf("%s<%c%c%c", tag, cie->cc[0], cie->cc[1], cie->cc[2]);
2629173275Ssam	nbands = (cie->len - 3) / sizeof(cie->band[0]);
2630173275Ssam	for (i = 0; i < nbands; i++) {
2631173275Ssam		schan = cie->band[i].schan;
2632173275Ssam		nchan = cie->band[i].nchan;
2633173275Ssam		if (nchan != 1)
2634173275Ssam			printf(" %u-%u,%u", schan, schan + nchan-1,
2635173275Ssam			    cie->band[i].maxtxpwr);
2636173275Ssam		else
2637173275Ssam			printf(" %u,%u", schan, cie->band[i].maxtxpwr);
2638173275Ssam	}
2639173275Ssam	printf(">");
2640173275Ssam}
2641173275Ssam
2642148686Sstefanf/* unaligned little endian access */
2643138593Ssam#define LE_READ_4(p)					\
2644138593Ssam	((u_int32_t)					\
2645138593Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2646138593Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
2647138593Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
2648138593Ssam	  (((const u_int8_t *)(p))[3] << 24)))
2649138593Ssam
2650178354Ssamstatic __inline int
2651138593Ssamiswpaoui(const u_int8_t *frm)
2652138593Ssam{
2653138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
2654138593Ssam}
2655138593Ssam
2656178354Ssamstatic __inline int
2657173275Ssamiswmeinfo(const u_int8_t *frm)
2658138593Ssam{
2659173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
2660173275Ssam		frm[6] == WME_INFO_OUI_SUBTYPE;
2661138593Ssam}
2662138593Ssam
2663178354Ssamstatic __inline int
2664173275Ssamiswmeparam(const u_int8_t *frm)
2665173275Ssam{
2666173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
2667173275Ssam		frm[6] == WME_PARAM_OUI_SUBTYPE;
2668173275Ssam}
2669173275Ssam
2670178354Ssamstatic __inline int
2671138593Ssamisatherosoui(const u_int8_t *frm)
2672138593Ssam{
2673138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
2674138593Ssam}
2675138593Ssam
2676181454Ssamstatic __inline int
2677181454Ssamiswpsoui(const uint8_t *frm)
2678181454Ssam{
2679181454Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPS_OUI_TYPE<<24)|WPA_OUI);
2680181454Ssam}
2681181454Ssam
2682173275Ssamstatic const char *
2683173275Ssamiename(int elemid)
2684173275Ssam{
2685173275Ssam	switch (elemid) {
2686173275Ssam	case IEEE80211_ELEMID_FHPARMS:	return " FHPARMS";
2687173275Ssam	case IEEE80211_ELEMID_CFPARMS:	return " CFPARMS";
2688173275Ssam	case IEEE80211_ELEMID_TIM:	return " TIM";
2689173275Ssam	case IEEE80211_ELEMID_IBSSPARMS:return " IBSSPARMS";
2690173275Ssam	case IEEE80211_ELEMID_CHALLENGE:return " CHALLENGE";
2691173275Ssam	case IEEE80211_ELEMID_PWRCNSTR:	return " PWRCNSTR";
2692173275Ssam	case IEEE80211_ELEMID_PWRCAP:	return " PWRCAP";
2693173275Ssam	case IEEE80211_ELEMID_TPCREQ:	return " TPCREQ";
2694173275Ssam	case IEEE80211_ELEMID_TPCREP:	return " TPCREP";
2695173275Ssam	case IEEE80211_ELEMID_SUPPCHAN:	return " SUPPCHAN";
2696173275Ssam	case IEEE80211_ELEMID_CHANSWITCHANN:return " CSA";
2697173275Ssam	case IEEE80211_ELEMID_MEASREQ:	return " MEASREQ";
2698173275Ssam	case IEEE80211_ELEMID_MEASREP:	return " MEASREP";
2699173275Ssam	case IEEE80211_ELEMID_QUIET:	return " QUIET";
2700173275Ssam	case IEEE80211_ELEMID_IBSSDFS:	return " IBSSDFS";
2701173275Ssam	case IEEE80211_ELEMID_TPC:	return " TPC";
2702173275Ssam	case IEEE80211_ELEMID_CCKM:	return " CCKM";
2703173275Ssam	}
2704173275Ssam	return " ???";
2705173275Ssam}
2706173275Ssam
2707138593Ssamstatic void
2708138593Ssamprinties(const u_int8_t *vp, int ielen, int maxcols)
2709138593Ssam{
2710138593Ssam	while (ielen > 0) {
2711138593Ssam		switch (vp[0]) {
2712173275Ssam		case IEEE80211_ELEMID_SSID:
2713173275Ssam			if (verbose)
2714173275Ssam				printssid(" SSID", vp, 2+vp[1], maxcols);
2715173275Ssam			break;
2716173275Ssam		case IEEE80211_ELEMID_RATES:
2717173275Ssam		case IEEE80211_ELEMID_XRATES:
2718173275Ssam			if (verbose)
2719173275Ssam				printrates(vp[0] == IEEE80211_ELEMID_RATES ?
2720173275Ssam				    " RATES" : " XRATES", vp, 2+vp[1], maxcols);
2721173275Ssam			break;
2722173275Ssam		case IEEE80211_ELEMID_DSPARMS:
2723173275Ssam			if (verbose)
2724173275Ssam				printf(" DSPARMS<%u>", vp[2]);
2725173275Ssam			break;
2726173275Ssam		case IEEE80211_ELEMID_COUNTRY:
2727173275Ssam			if (verbose)
2728173275Ssam				printcountry(" COUNTRY", vp, 2+vp[1], maxcols);
2729173275Ssam			break;
2730173275Ssam		case IEEE80211_ELEMID_ERP:
2731173275Ssam			if (verbose)
2732173275Ssam				printf(" ERP<0x%x>", vp[2]);
2733173275Ssam			break;
2734138593Ssam		case IEEE80211_ELEMID_VENDOR:
2735138593Ssam			if (iswpaoui(vp))
2736170531Ssam				printwpaie(" WPA", vp, 2+vp[1], maxcols);
2737173275Ssam			else if (iswmeinfo(vp))
2738173275Ssam				printwmeinfo(" WME", vp, 2+vp[1], maxcols);
2739173275Ssam			else if (iswmeparam(vp))
2740173275Ssam				printwmeparam(" WME", vp, 2+vp[1], maxcols);
2741139492Ssam			else if (isatherosoui(vp))
2742170531Ssam				printathie(" ATH", vp, 2+vp[1], maxcols);
2743181454Ssam			else if (iswpsoui(vp))
2744181454Ssam				printwpsie(" WPS", vp, 2+vp[1], maxcols);
2745173275Ssam			else if (verbose)
2746138593Ssam				printie(" VEN", vp, 2+vp[1], maxcols);
2747138593Ssam			break;
2748138593Ssam		case IEEE80211_ELEMID_RSN:
2749170531Ssam			printrsnie(" RSN", vp, 2+vp[1], maxcols);
2750138593Ssam			break;
2751173275Ssam		case IEEE80211_ELEMID_HTCAP:
2752173275Ssam			printhtcap(" HTCAP", vp, 2+vp[1], maxcols);
2753173275Ssam			break;
2754173275Ssam		case IEEE80211_ELEMID_HTINFO:
2755173275Ssam			if (verbose)
2756173275Ssam				printhtinfo(" HTINFO", vp, 2+vp[1], maxcols);
2757173275Ssam			break;
2758138593Ssam		default:
2759173275Ssam			if (verbose)
2760173275Ssam				printie(iename(vp[0]), vp, 2+vp[1], maxcols);
2761138593Ssam			break;
2762138593Ssam		}
2763138593Ssam		ielen -= 2+vp[1];
2764138593Ssam		vp += 2+vp[1];
2765138593Ssam	}
2766138593Ssam}
2767138593Ssam
2768138593Ssamstatic void
2769178354Ssamprintmimo(const struct ieee80211_mimo_info *mi)
2770178354Ssam{
2771178354Ssam	/* NB: don't muddy display unless there's something to show */
2772178354Ssam	if (mi->rssi[0] != 0 || mi->rssi[1] != 0 || mi->rssi[2] != 0) {
2773178354Ssam		/* XXX ignore EVM for now */
2774178354Ssam		printf(" (rssi %d:%d:%d nf %d:%d:%d)",
2775178354Ssam		    mi->rssi[0], mi->rssi[1], mi->rssi[2],
2776178354Ssam		    mi->noise[0], mi->noise[1], mi->noise[2]);
2777178354Ssam	}
2778178354Ssam}
2779178354Ssam
2780178354Ssamstatic void
2781138593Ssamlist_scan(int s)
2782138593Ssam{
2783138593Ssam	uint8_t buf[24*1024];
2784153892Srwatson	char ssid[IEEE80211_NWID_LEN+1];
2785173275Ssam	const uint8_t *cp;
2786154522Ssam	int len, ssidmax;
2787138593Ssam
2788173275Ssam	if (get80211len(s, IEEE80211_IOC_SCAN_RESULTS, buf, sizeof(buf), &len) < 0)
2789138593Ssam		errx(1, "unable to get scan results");
2790138593Ssam	if (len < sizeof(struct ieee80211req_scan_result))
2791138593Ssam		return;
2792138593Ssam
2793170531Ssam	getchaninfo(s);
2794170531Ssam
2795154522Ssam	ssidmax = verbose ? IEEE80211_NWID_LEN : 14;
2796170531Ssam	printf("%-*.*s  %-17.17s  %4s %4s  %-7s  %3s %4s\n"
2797154522Ssam		, ssidmax, ssidmax, "SSID"
2798138593Ssam		, "BSSID"
2799138593Ssam		, "CHAN"
2800138593Ssam		, "RATE"
2801170531Ssam		, " S:N"
2802138593Ssam		, "INT"
2803138593Ssam		, "CAPS"
2804138593Ssam	);
2805138593Ssam	cp = buf;
2806138593Ssam	do {
2807170531Ssam		const struct ieee80211req_scan_result *sr;
2808170531Ssam		const uint8_t *vp;
2809138593Ssam
2810170531Ssam		sr = (const struct ieee80211req_scan_result *) cp;
2811173275Ssam		vp = cp + sr->isr_ie_off;
2812170531Ssam		printf("%-*.*s  %s  %3d  %3dM %3d:%-3d  %3d %-4.4s"
2813154522Ssam			, ssidmax
2814155461Ssam			  , copy_essid(ssid, ssidmax, vp, sr->isr_ssid_len)
2815154522Ssam			  , ssid
2816138593Ssam			, ether_ntoa((const struct ether_addr *) sr->isr_bssid)
2817165570Ssam			, ieee80211_mhz2ieee(sr->isr_freq, sr->isr_flags)
2818138593Ssam			, getmaxrate(sr->isr_rates, sr->isr_nrates)
2819170531Ssam			, (sr->isr_rssi/2)+sr->isr_noise, sr->isr_noise
2820138593Ssam			, sr->isr_intval
2821138593Ssam			, getcaps(sr->isr_capinfo)
2822138593Ssam		);
2823170531Ssam		printies(vp + sr->isr_ssid_len, sr->isr_ie_len, 24);
2824138593Ssam		printf("\n");
2825138593Ssam		cp += sr->isr_len, len -= sr->isr_len;
2826138593Ssam	} while (len >= sizeof(struct ieee80211req_scan_result));
2827138593Ssam}
2828138593Ssam
2829178354Ssam#ifdef __FreeBSD__
2830138593Ssam#include <net80211/ieee80211_freebsd.h>
2831178354Ssam#endif
2832178354Ssam#ifdef __NetBSD__
2833178354Ssam#include <net80211/ieee80211_netbsd.h>
2834178354Ssam#endif
2835138593Ssam
2836138593Ssamstatic void
2837138593Ssamscan_and_wait(int s)
2838138593Ssam{
2839178354Ssam	struct ieee80211_scan_req sr;
2840138593Ssam	struct ieee80211req ireq;
2841138593Ssam	int sroute;
2842138593Ssam
2843138593Ssam	sroute = socket(PF_ROUTE, SOCK_RAW, 0);
2844138593Ssam	if (sroute < 0) {
2845138593Ssam		perror("socket(PF_ROUTE,SOCK_RAW)");
2846138593Ssam		return;
2847138593Ssam	}
2848138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
2849138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
2850138593Ssam	ireq.i_type = IEEE80211_IOC_SCAN_REQ;
2851178354Ssam
2852178354Ssam	memset(&sr, 0, sizeof(sr));
2853178354Ssam	sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE
2854178354Ssam		    | IEEE80211_IOC_SCAN_NOPICK
2855178354Ssam		    | IEEE80211_IOC_SCAN_ONCE;
2856178354Ssam	sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
2857178354Ssam	sr.sr_nssid = 0;
2858178354Ssam
2859178354Ssam	ireq.i_data = &sr;
2860178354Ssam	ireq.i_len = sizeof(sr);
2861138593Ssam	/* NB: only root can trigger a scan so ignore errors */
2862138593Ssam	if (ioctl(s, SIOCS80211, &ireq) >= 0) {
2863138593Ssam		char buf[2048];
2864138593Ssam		struct if_announcemsghdr *ifan;
2865138593Ssam		struct rt_msghdr *rtm;
2866138593Ssam
2867138593Ssam		do {
2868138593Ssam			if (read(sroute, buf, sizeof(buf)) < 0) {
2869138593Ssam				perror("read(PF_ROUTE)");
2870138593Ssam				break;
2871138593Ssam			}
2872138593Ssam			rtm = (struct rt_msghdr *) buf;
2873138593Ssam			if (rtm->rtm_version != RTM_VERSION)
2874138593Ssam				break;
2875138593Ssam			ifan = (struct if_announcemsghdr *) rtm;
2876138593Ssam		} while (rtm->rtm_type != RTM_IEEE80211 ||
2877138593Ssam		    ifan->ifan_what != RTM_IEEE80211_SCAN);
2878138593Ssam	}
2879138593Ssam	close(sroute);
2880138593Ssam}
2881138593Ssam
2882138593Ssamstatic
2883138593SsamDECL_CMD_FUNC(set80211scan, val, d)
2884138593Ssam{
2885138593Ssam	scan_and_wait(s);
2886138593Ssam	list_scan(s);
2887138593Ssam}
2888138593Ssam
2889161147Ssamstatic enum ieee80211_opmode get80211opmode(int s);
2890161147Ssam
2891173275Ssamstatic int
2892173275Ssamgettxseq(const struct ieee80211req_sta_info *si)
2893173275Ssam{
2894173275Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
2895173275Ssam
2896173275Ssam	int i, txseq;
2897173275Ssam
2898173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
2899173275Ssam		return si->isi_txseqs[0];
2900173275Ssam	/* XXX not right but usually what folks want */
2901173275Ssam	txseq = 0;
2902173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
2903173275Ssam		if (si->isi_txseqs[i] > txseq)
2904173275Ssam			txseq = si->isi_txseqs[i];
2905173275Ssam	return txseq;
2906173275Ssam#undef IEEE80211_NODE_QOS
2907173275Ssam}
2908173275Ssam
2909173275Ssamstatic int
2910173275Ssamgetrxseq(const struct ieee80211req_sta_info *si)
2911173275Ssam{
2912173275Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
2913173275Ssam
2914173275Ssam	int i, rxseq;
2915173275Ssam
2916173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
2917173275Ssam		return si->isi_rxseqs[0];
2918173275Ssam	/* XXX not right but usually what folks want */
2919173275Ssam	rxseq = 0;
2920173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
2921173275Ssam		if (si->isi_rxseqs[i] > rxseq)
2922173275Ssam			rxseq = si->isi_rxseqs[i];
2923173275Ssam	return rxseq;
2924173275Ssam#undef IEEE80211_NODE_QOS
2925173275Ssam}
2926173275Ssam
2927138593Ssamstatic void
2928138593Ssamlist_stations(int s)
2929138593Ssam{
2930161147Ssam	union {
2931161147Ssam		struct ieee80211req_sta_req req;
2932161147Ssam		uint8_t buf[24*1024];
2933161147Ssam	} u;
2934161147Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
2935170531Ssam	const uint8_t *cp;
2936138593Ssam	int len;
2937138593Ssam
2938161147Ssam	/* broadcast address =>'s get all stations */
2939161147Ssam	(void) memset(u.req.is_u.macaddr, 0xff, IEEE80211_ADDR_LEN);
2940161147Ssam	if (opmode == IEEE80211_M_STA) {
2941161147Ssam		/*
2942161147Ssam		 * Get information about the associated AP.
2943161147Ssam		 */
2944173275Ssam		(void) get80211(s, IEEE80211_IOC_BSSID,
2945173275Ssam		    u.req.is_u.macaddr, IEEE80211_ADDR_LEN);
2946161147Ssam	}
2947173275Ssam	if (get80211len(s, IEEE80211_IOC_STA_INFO, &u, sizeof(u), &len) < 0)
2948138593Ssam		errx(1, "unable to get station information");
2949138593Ssam	if (len < sizeof(struct ieee80211req_sta_info))
2950138593Ssam		return;
2951138593Ssam
2952170531Ssam	getchaninfo(s);
2953170531Ssam
2954159885Ssam	printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %4s\n"
2955138593Ssam		, "ADDR"
2956138593Ssam		, "AID"
2957138593Ssam		, "CHAN"
2958138593Ssam		, "RATE"
2959138593Ssam		, "RSSI"
2960138593Ssam		, "IDLE"
2961138593Ssam		, "TXSEQ"
2962138593Ssam		, "RXSEQ"
2963138593Ssam		, "CAPS"
2964159885Ssam		, "FLAG"
2965138593Ssam	);
2966170531Ssam	cp = (const uint8_t *) u.req.info;
2967138593Ssam	do {
2968170531Ssam		const struct ieee80211req_sta_info *si;
2969138593Ssam
2970170531Ssam		si = (const struct ieee80211req_sta_info *) cp;
2971161147Ssam		if (si->isi_len < sizeof(*si))
2972161147Ssam			break;
2973170531Ssam		printf("%s %4u %4d %3dM %3.1f %4d %6d %6d %-4.4s %-4.4s"
2974138593Ssam			, ether_ntoa((const struct ether_addr*) si->isi_macaddr)
2975138593Ssam			, IEEE80211_AID(si->isi_associd)
2976166015Ssam			, ieee80211_mhz2ieee(si->isi_freq, si->isi_flags)
2977178354Ssam			, si->isi_txmbps/2
2978170531Ssam			, si->isi_rssi/2.
2979138593Ssam			, si->isi_inact
2980173275Ssam			, gettxseq(si)
2981173275Ssam			, getrxseq(si)
2982138593Ssam			, getcaps(si->isi_capinfo)
2983159885Ssam			, getflags(si->isi_state)
2984138593Ssam		);
2985170531Ssam		printies(cp + si->isi_ie_off, si->isi_ie_len, 24);
2986178354Ssam		printmimo(&si->isi_mimo);
2987138593Ssam		printf("\n");
2988138593Ssam		cp += si->isi_len, len -= si->isi_len;
2989138593Ssam	} while (len >= sizeof(struct ieee80211req_sta_info));
2990138593Ssam}
2991138593Ssam
2992170531Ssamstatic const char *
2993170531Ssamget_chaninfo(const struct ieee80211_channel *c, int precise,
2994170531Ssam	char buf[], size_t bsize)
2995138593Ssam{
2996138593Ssam	buf[0] = '\0';
2997138593Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
2998170531Ssam		strlcat(buf, " FHSS", bsize);
2999165570Ssam	if (IEEE80211_IS_CHAN_A(c)) {
3000165570Ssam		if (IEEE80211_IS_CHAN_HALF(c))
3001170531Ssam			strlcat(buf, " 11a/10Mhz", bsize);
3002165570Ssam		else if (IEEE80211_IS_CHAN_QUARTER(c))
3003170531Ssam			strlcat(buf, " 11a/5Mhz", bsize);
3004165570Ssam		else
3005170531Ssam			strlcat(buf, " 11a", bsize);
3006165570Ssam	}
3007166015Ssam	if (IEEE80211_IS_CHAN_ANYG(c)) {
3008166015Ssam		if (IEEE80211_IS_CHAN_HALF(c))
3009170531Ssam			strlcat(buf, " 11g/10Mhz", bsize);
3010166015Ssam		else if (IEEE80211_IS_CHAN_QUARTER(c))
3011170531Ssam			strlcat(buf, " 11g/5Mhz", bsize);
3012166015Ssam		else
3013170531Ssam			strlcat(buf, " 11g", bsize);
3014166015Ssam	} else if (IEEE80211_IS_CHAN_B(c))
3015170531Ssam		strlcat(buf, " 11b", bsize);
3016170531Ssam	if (IEEE80211_IS_CHAN_TURBO(c))
3017170531Ssam		strlcat(buf, " Turbo", bsize);
3018170531Ssam	if (precise) {
3019170531Ssam		if (IEEE80211_IS_CHAN_HT20(c))
3020170531Ssam			strlcat(buf, " ht/20", bsize);
3021170531Ssam		else if (IEEE80211_IS_CHAN_HT40D(c))
3022170531Ssam			strlcat(buf, " ht/40-", bsize);
3023170531Ssam		else if (IEEE80211_IS_CHAN_HT40U(c))
3024170531Ssam			strlcat(buf, " ht/40+", bsize);
3025170531Ssam	} else {
3026170531Ssam		if (IEEE80211_IS_CHAN_HT(c))
3027170531Ssam			strlcat(buf, " ht", bsize);
3028170531Ssam	}
3029170531Ssam	return buf;
3030170531Ssam}
3031170531Ssam
3032170531Ssamstatic void
3033173275Ssamprint_chaninfo(const struct ieee80211_channel *c, int verb)
3034170531Ssam{
3035170531Ssam	char buf[14];
3036170531Ssam
3037138593Ssam	printf("Channel %3u : %u%c Mhz%-14.14s",
3038165570Ssam		ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
3039170531Ssam		IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
3040173275Ssam		get_chaninfo(c, verb, buf, sizeof(buf)));
3041138593Ssam}
3042138593Ssam
3043138593Ssamstatic void
3044173275Ssamprint_channels(int s, const struct ieee80211req_chaninfo *chans,
3045173275Ssam	int allchans, int verb)
3046138593Ssam{
3047138593Ssam	struct ieee80211req_chaninfo achans;
3048170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3049138593Ssam	const struct ieee80211_channel *c;
3050170531Ssam	int i, half;
3051138593Ssam
3052170531Ssam	memset(&achans, 0, sizeof(achans));
3053170531Ssam	memset(reported, 0, sizeof(reported));
3054138593Ssam	if (!allchans) {
3055138593Ssam		struct ieee80211req_chanlist active;
3056138593Ssam
3057173275Ssam		if (get80211(s, IEEE80211_IOC_CHANLIST, &active, sizeof(active)) < 0)
3058138593Ssam			errx(1, "unable to get active channel list");
3059138593Ssam		memset(&achans, 0, sizeof(achans));
3060173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3061173275Ssam			c = &chans->ic_chans[i];
3062170531Ssam			if (!isset(active.ic_channels, c->ic_ieee))
3063170531Ssam				continue;
3064170531Ssam			/*
3065170531Ssam			 * Suppress compatible duplicates unless
3066170531Ssam			 * verbose.  The kernel gives us it's
3067170531Ssam			 * complete channel list which has separate
3068170531Ssam			 * entries for 11g/11b and 11a/turbo.
3069170531Ssam			 */
3070173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3071170531Ssam				/* XXX we assume duplicates are adjacent */
3072170531Ssam				achans.ic_chans[achans.ic_nchans-1] = *c;
3073170531Ssam			} else {
3074138593Ssam				achans.ic_chans[achans.ic_nchans++] = *c;
3075170531Ssam				setbit(reported, c->ic_ieee);
3076170531Ssam			}
3077138593Ssam		}
3078170531Ssam	} else {
3079173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3080173275Ssam			c = &chans->ic_chans[i];
3081170531Ssam			/* suppress duplicates as above */
3082173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3083170531Ssam				/* XXX we assume duplicates are adjacent */
3084170531Ssam				achans.ic_chans[achans.ic_nchans-1] = *c;
3085170531Ssam			} else {
3086170531Ssam				achans.ic_chans[achans.ic_nchans++] = *c;
3087170531Ssam				setbit(reported, c->ic_ieee);
3088170531Ssam			}
3089170531Ssam		}
3090170531Ssam	}
3091138593Ssam	half = achans.ic_nchans / 2;
3092138593Ssam	if (achans.ic_nchans % 2)
3093138593Ssam		half++;
3094170531Ssam
3095138593Ssam	for (i = 0; i < achans.ic_nchans / 2; i++) {
3096173275Ssam		print_chaninfo(&achans.ic_chans[i], verb);
3097173275Ssam		print_chaninfo(&achans.ic_chans[half+i], verb);
3098138593Ssam		printf("\n");
3099138593Ssam	}
3100138593Ssam	if (achans.ic_nchans % 2) {
3101173275Ssam		print_chaninfo(&achans.ic_chans[i], verb);
3102138593Ssam		printf("\n");
3103138593Ssam	}
3104138593Ssam}
3105138593Ssam
3106138593Ssamstatic void
3107173275Ssamlist_channels(int s, int allchans)
3108173275Ssam{
3109173275Ssam	getchaninfo(s);
3110173275Ssam	print_channels(s, &chaninfo, allchans, verbose);
3111173275Ssam}
3112173275Ssam
3113173275Ssamstatic void
3114170531Ssamprint_txpow(const struct ieee80211_channel *c)
3115170531Ssam{
3116170531Ssam	printf("Channel %3u : %u Mhz %3.1f reg %2d  ",
3117170531Ssam	    c->ic_ieee, c->ic_freq,
3118170531Ssam	    c->ic_maxpower/2., c->ic_maxregpower);
3119170531Ssam}
3120170531Ssam
3121170531Ssamstatic void
3122170531Ssamprint_txpow_verbose(const struct ieee80211_channel *c)
3123170531Ssam{
3124173275Ssam	print_chaninfo(c, 1);
3125170531Ssam	printf("min %4.1f dBm  max %3.1f dBm  reg %2d dBm",
3126170531Ssam	    c->ic_minpower/2., c->ic_maxpower/2., c->ic_maxregpower);
3127170531Ssam	/* indicate where regulatory cap limits power use */
3128170531Ssam	if (c->ic_maxpower > 2*c->ic_maxregpower)
3129170531Ssam		printf(" <");
3130170531Ssam}
3131170531Ssam
3132170531Ssamstatic void
3133170531Ssamlist_txpow(int s)
3134170531Ssam{
3135170531Ssam	struct ieee80211req_chaninfo achans;
3136170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3137170531Ssam	struct ieee80211_channel *c, *prev;
3138170531Ssam	int i, half;
3139170531Ssam
3140170531Ssam	getchaninfo(s);
3141170531Ssam	memset(&achans, 0, sizeof(achans));
3142170531Ssam	memset(reported, 0, sizeof(reported));
3143170531Ssam	for (i = 0; i < chaninfo.ic_nchans; i++) {
3144170531Ssam		c = &chaninfo.ic_chans[i];
3145170531Ssam		/* suppress duplicates as above */
3146170531Ssam		if (isset(reported, c->ic_ieee) && !verbose) {
3147170531Ssam			/* XXX we assume duplicates are adjacent */
3148170531Ssam			prev = &achans.ic_chans[achans.ic_nchans-1];
3149170531Ssam			/* display highest power on channel */
3150170531Ssam			if (c->ic_maxpower > prev->ic_maxpower)
3151170531Ssam				*prev = *c;
3152170531Ssam		} else {
3153170531Ssam			achans.ic_chans[achans.ic_nchans++] = *c;
3154170531Ssam			setbit(reported, c->ic_ieee);
3155170531Ssam		}
3156170531Ssam	}
3157170531Ssam	if (!verbose) {
3158170531Ssam		half = achans.ic_nchans / 2;
3159170531Ssam		if (achans.ic_nchans % 2)
3160170531Ssam			half++;
3161170531Ssam
3162170531Ssam		for (i = 0; i < achans.ic_nchans / 2; i++) {
3163170531Ssam			print_txpow(&achans.ic_chans[i]);
3164170531Ssam			print_txpow(&achans.ic_chans[half+i]);
3165170531Ssam			printf("\n");
3166170531Ssam		}
3167170531Ssam		if (achans.ic_nchans % 2) {
3168170531Ssam			print_txpow(&achans.ic_chans[i]);
3169170531Ssam			printf("\n");
3170170531Ssam		}
3171170531Ssam	} else {
3172170531Ssam		for (i = 0; i < achans.ic_nchans; i++) {
3173170531Ssam			print_txpow_verbose(&achans.ic_chans[i]);
3174170531Ssam			printf("\n");
3175170531Ssam		}
3176170531Ssam	}
3177170531Ssam}
3178170531Ssam
3179170531Ssamstatic void
3180138593Ssamlist_keys(int s)
3181138593Ssam{
3182138593Ssam}
3183138593Ssam
3184138593Ssam#define	IEEE80211_C_BITS \
3185181102Ssam	"\20\1STA\7FF\10TURBOP\11IBSS\12PMGT" \
3186178354Ssam	"\13HOSTAP\14AHDEMO\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE" \
3187181102Ssam	"\21MONITOR\22DFS\30WPA1\31WPA2\32BURST\33WME\34WDS\36BGSCAN" \
3188178354Ssam	"\37TXFRAG"
3189138593Ssam
3190178354Ssam#define	IEEE80211_CRYPTO_BITS \
3191178354Ssam	"\20\1WEP\2TKIP\3AES\4AES_CCM\5TKIPMIC\6CKIP\12PMGT"
3192178354Ssam
3193178354Ssam#define	IEEE80211_HTCAP_BITS \
3194178354Ssam	"\20\1LDPC\2CHWIDTH40\5GREENFIELD\6SHORTGI20\7SHORTGI40\10TXSTBC" \
3195178354Ssam	"\21AMPDU\22AMSDU\23HT"
3196178354Ssam
3197138593Ssamstatic void
3198138593Ssamlist_capabilities(int s)
3199138593Ssam{
3200178354Ssam	struct ieee80211_devcaps_req dc;
3201138593Ssam
3202178354Ssam	getdevcaps(s, &dc);
3203178354Ssam	printb("drivercaps", dc.dc_drivercaps, IEEE80211_C_BITS);
3204178354Ssam	if (dc.dc_cryptocaps != 0 || verbose) {
3205178354Ssam		putchar('\n');
3206178354Ssam		printb("cryptocaps", dc.dc_cryptocaps, IEEE80211_CRYPTO_BITS);
3207178354Ssam	}
3208178354Ssam	if (dc.dc_htcaps != 0 || verbose) {
3209178354Ssam		putchar('\n');
3210178354Ssam		printb("htcaps", dc.dc_htcaps, IEEE80211_HTCAP_BITS);
3211178354Ssam	}
3212138593Ssam	putchar('\n');
3213138593Ssam}
3214138593Ssam
3215173275Ssamstatic int
3216173275Ssamget80211wme(int s, int param, int ac, int *val)
3217173275Ssam{
3218173275Ssam	struct ieee80211req ireq;
3219173275Ssam
3220173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3221173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3222173275Ssam	ireq.i_type = param;
3223173275Ssam	ireq.i_len = ac;
3224173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3225173275Ssam		warn("cannot get WME parameter %d, ac %d%s",
3226173275Ssam		    param, ac & IEEE80211_WMEPARAM_VAL,
3227173275Ssam		    ac & IEEE80211_WMEPARAM_BSS ? " (BSS)" : "");
3228173275Ssam		return -1;
3229173275Ssam	}
3230173275Ssam	*val = ireq.i_val;
3231173275Ssam	return 0;
3232173275Ssam}
3233173275Ssam
3234138593Ssamstatic void
3235181199Ssamlist_wme_aci(int s, const char *tag, int ac)
3236138593Ssam{
3237181199Ssam	int val;
3238138593Ssam
3239181199Ssam	printf("\t%s", tag);
3240138593Ssam
3241181199Ssam	/* show WME BSS parameters */
3242181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMIN, ac, &val) != -1)
3243181199Ssam		printf(" cwmin %2u", val);
3244181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMAX, ac, &val) != -1)
3245181199Ssam		printf(" cwmax %2u", val);
3246181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_AIFS, ac, &val) != -1)
3247181199Ssam		printf(" aifs %2u", val);
3248181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_TXOPLIMIT, ac, &val) != -1)
3249181199Ssam		printf(" txopLimit %3u", val);
3250181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_ACM, ac, &val) != -1) {
3251181199Ssam		if (val)
3252181199Ssam			printf(" acm");
3253181199Ssam		else if (verbose)
3254181199Ssam			printf(" -acm");
3255181199Ssam	}
3256181199Ssam	/* !BSS only */
3257181199Ssam	if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3258181199Ssam		if (get80211wme(s, IEEE80211_IOC_WME_ACKPOLICY, ac, &val) != -1) {
3259181199Ssam			if (!val)
3260181199Ssam				printf(" -ack");
3261138593Ssam			else if (verbose)
3262181199Ssam				printf(" ack");
3263138593Ssam		}
3264181199Ssam	}
3265181199Ssam	printf("\n");
3266181199Ssam}
3267181199Ssam
3268181199Ssamstatic void
3269181199Ssamlist_wme(int s)
3270181199Ssam{
3271181199Ssam	static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
3272181199Ssam	int ac;
3273181199Ssam
3274181199Ssam	if (verbose) {
3275181199Ssam		/* display both BSS and local settings */
3276181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
3277181199Ssam	again:
3278181199Ssam			if (ac & IEEE80211_WMEPARAM_BSS)
3279181199Ssam				list_wme_aci(s, "     ", ac);
3280181199Ssam			else
3281181199Ssam				list_wme_aci(s, acnames[ac], ac);
3282181199Ssam			if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3283181199Ssam				ac |= IEEE80211_WMEPARAM_BSS;
3284181199Ssam				goto again;
3285181199Ssam			} else
3286181199Ssam				ac &= ~IEEE80211_WMEPARAM_BSS;
3287138593Ssam		}
3288181199Ssam	} else {
3289181199Ssam		/* display only channel settings */
3290181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++)
3291181199Ssam			list_wme_aci(s, acnames[ac], ac);
3292138593Ssam	}
3293138593Ssam}
3294138593Ssam
3295149029Ssamstatic void
3296178354Ssamlist_roam(int s)
3297178354Ssam{
3298178354Ssam	const struct ieee80211_roamparam *rp;
3299178354Ssam	int mode;
3300178354Ssam
3301178354Ssam	getroam(s);
3302178354Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_11NA; mode++) {
3303178354Ssam		rp = &roamparams.params[mode];
3304178354Ssam		if (rp->rssi == 0 && rp->rate == 0)
3305178354Ssam			continue;
3306178354Ssam		if (rp->rssi & 1)
3307178354Ssam			LINE_CHECK("roam:%-6.6s rssi %2u.5dBm rate %2u Mb/s",
3308178354Ssam			    modename[mode], rp->rssi/2, rp->rate/2);
3309178354Ssam		else
3310178354Ssam			LINE_CHECK("roam:%-6.6s rssi %4udBm rate %2u Mb/s",
3311178354Ssam			    modename[mode], rp->rssi/2, rp->rate/2);
3312178354Ssam	}
3313178354Ssam	for (; mode < IEEE80211_MODE_MAX; mode++) {
3314178354Ssam		rp = &roamparams.params[mode];
3315178354Ssam		if (rp->rssi == 0 && rp->rate == 0)
3316178354Ssam			continue;
3317178354Ssam		if (rp->rssi & 1)
3318178354Ssam			LINE_CHECK("roam:%-6.6s rssi %2u.5dBm  MCS %2u    ",
3319178354Ssam			    modename[mode], rp->rssi/2, rp->rate &~ 0x80);
3320178354Ssam		else
3321178354Ssam			LINE_CHECK("roam:%-6.6s rssi %4udBm  MCS %2u    ",
3322178354Ssam			    modename[mode], rp->rssi/2, rp->rate &~ 0x80);
3323178354Ssam	}
3324178354Ssam}
3325178354Ssam
3326178354Ssamstatic void
3327178354Ssamlist_txparams(int s)
3328178354Ssam{
3329178354Ssam	const struct ieee80211_txparam *tp;
3330178354Ssam	int mode;
3331178354Ssam
3332178354Ssam	gettxparams(s);
3333178354Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_11NA; mode++) {
3334178354Ssam		tp = &txparams.params[mode];
3335178354Ssam		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
3336178354Ssam			continue;
3337178354Ssam		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3338178354Ssam			LINE_CHECK("%-6.6s ucast NONE    mgmt %2u Mb/s "
3339178354Ssam			    "mcast %2u Mb/s maxretry %u",
3340178354Ssam			    modename[mode], tp->mgmtrate/2,
3341178354Ssam			    tp->mcastrate/2, tp->maxretry);
3342178354Ssam		else
3343178354Ssam			LINE_CHECK("%-6.6s ucast %2u Mb/s mgmt %2u Mb/s "
3344178354Ssam			    "mcast %2u Mb/s maxretry %u",
3345178354Ssam			    modename[mode], tp->ucastrate/2, tp->mgmtrate/2,
3346178354Ssam			    tp->mcastrate/2, tp->maxretry);
3347178354Ssam	}
3348178354Ssam	for (; mode < IEEE80211_MODE_MAX; mode++) {
3349178354Ssam		tp = &txparams.params[mode];
3350178354Ssam		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
3351178354Ssam			continue;
3352178354Ssam		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3353178354Ssam			LINE_CHECK("%-6.6s ucast NONE    mgmt %2u MCS  "
3354178354Ssam			    "mcast %2u MCS  maxretry %u",
3355178354Ssam			    modename[mode], tp->mgmtrate &~ 0x80,
3356178354Ssam			    tp->mcastrate &~ 0x80, tp->maxretry);
3357178354Ssam		else
3358178354Ssam			LINE_CHECK("%-6.6s ucast %2u MCS  mgmt %2u MCS  "
3359178354Ssam			    "mcast %2u MCS  maxretry %u",
3360178354Ssam			    modename[mode], tp->ucastrate &~ 0x80,
3361178354Ssam			    tp->mgmtrate &~ 0x80,
3362178354Ssam			    tp->mcastrate &~ 0x80, tp->maxretry);
3363178354Ssam	}
3364178354Ssam}
3365178354Ssam
3366178354Ssamstatic void
3367173275Ssamprintpolicy(int policy)
3368173275Ssam{
3369173275Ssam	switch (policy) {
3370173275Ssam	case IEEE80211_MACCMD_POLICY_OPEN:
3371173275Ssam		printf("policy: open\n");
3372173275Ssam		break;
3373173275Ssam	case IEEE80211_MACCMD_POLICY_ALLOW:
3374173275Ssam		printf("policy: allow\n");
3375173275Ssam		break;
3376173275Ssam	case IEEE80211_MACCMD_POLICY_DENY:
3377173275Ssam		printf("policy: deny\n");
3378173275Ssam		break;
3379178354Ssam	case IEEE80211_MACCMD_POLICY_RADIUS:
3380178354Ssam		printf("policy: radius\n");
3381178354Ssam		break;
3382173275Ssam	default:
3383173275Ssam		printf("policy: unknown (%u)\n", policy);
3384173275Ssam		break;
3385173275Ssam	}
3386173275Ssam}
3387173275Ssam
3388173275Ssamstatic void
3389149029Ssamlist_mac(int s)
3390149029Ssam{
3391149029Ssam	struct ieee80211req ireq;
3392149029Ssam	struct ieee80211req_maclist *acllist;
3393173275Ssam	int i, nacls, policy, len;
3394173275Ssam	uint8_t *data;
3395149029Ssam	char c;
3396149029Ssam
3397149029Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3398149029Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
3399149029Ssam	ireq.i_type = IEEE80211_IOC_MACCMD;
3400149029Ssam	ireq.i_val = IEEE80211_MACCMD_POLICY;
3401149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3402149029Ssam		if (errno == EINVAL) {
3403149029Ssam			printf("No acl policy loaded\n");
3404149029Ssam			return;
3405149029Ssam		}
3406149029Ssam		err(1, "unable to get mac policy");
3407149029Ssam	}
3408149029Ssam	policy = ireq.i_val;
3409149029Ssam	if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
3410149029Ssam		c = '*';
3411149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
3412149029Ssam		c = '+';
3413149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
3414149029Ssam		c = '-';
3415178354Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_RADIUS) {
3416178354Ssam		c = 'r';		/* NB: should never have entries */
3417149029Ssam	} else {
3418149029Ssam		printf("policy: unknown (%u)\n", policy);
3419149029Ssam		c = '?';
3420149029Ssam	}
3421173275Ssam	if (verbose || c == '?')
3422173275Ssam		printpolicy(policy);
3423173275Ssam
3424175952Ssam	ireq.i_val = IEEE80211_MACCMD_LIST;
3425175952Ssam	ireq.i_len = 0;
3426175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3427173275Ssam		err(1, "unable to get mac acl list size");
3428175952Ssam	if (ireq.i_len == 0) {		/* NB: no acls */
3429173275Ssam		if (!(verbose || c == '?'))
3430173275Ssam			printpolicy(policy);
3431173275Ssam		return;
3432173275Ssam	}
3433175952Ssam	len = ireq.i_len;
3434173275Ssam
3435173275Ssam	data = malloc(len);
3436173275Ssam	if (data == NULL)
3437173275Ssam		err(1, "out of memory for acl list");
3438173275Ssam
3439175952Ssam	ireq.i_data = data;
3440175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3441173275Ssam		err(1, "unable to get mac acl list");
3442173275Ssam	nacls = len / sizeof(*acllist);
3443173275Ssam	acllist = (struct ieee80211req_maclist *) data;
3444149029Ssam	for (i = 0; i < nacls; i++)
3445149029Ssam		printf("%c%s\n", c, ether_ntoa(
3446149029Ssam			(const struct ether_addr *) acllist[i].ml_macaddr));
3447173275Ssam	free(data);
3448149029Ssam}
3449149029Ssam
3450178354Ssamstatic void
3451178354Ssamprint_regdomain(const struct ieee80211_regdomain *reg, int verb)
3452178354Ssam{
3453178354Ssam	if ((reg->regdomain != 0 &&
3454178354Ssam	    reg->regdomain != reg->country) || verb) {
3455178354Ssam		const struct regdomain *rd =
3456178354Ssam		    lib80211_regdomain_findbysku(getregdata(), reg->regdomain);
3457178354Ssam		if (rd == NULL)
3458178354Ssam			LINE_CHECK("regdomain %d", reg->regdomain);
3459178354Ssam		else
3460178354Ssam			LINE_CHECK("regdomain %s", rd->name);
3461178354Ssam	}
3462178354Ssam	if (reg->country != 0 || verb) {
3463178354Ssam		const struct country *cc =
3464178354Ssam		    lib80211_country_findbycc(getregdata(), reg->country);
3465178354Ssam		if (cc == NULL)
3466178354Ssam			LINE_CHECK("country %d", reg->country);
3467178354Ssam		else
3468178354Ssam			LINE_CHECK("country %s", cc->isoname);
3469178354Ssam	}
3470178354Ssam	if (reg->location == 'I')
3471178354Ssam		LINE_CHECK("indoor");
3472178354Ssam	else if (reg->location == 'O')
3473178354Ssam		LINE_CHECK("outdoor");
3474178354Ssam	else if (verb)
3475178354Ssam		LINE_CHECK("anywhere");
3476178354Ssam	if (reg->ecm)
3477178354Ssam		LINE_CHECK("ecm");
3478178354Ssam	else if (verb)
3479178354Ssam		LINE_CHECK("-ecm");
3480178354Ssam}
3481178354Ssam
3482178354Ssamstatic void
3483178354Ssamlist_regdomain(int s, int channelsalso)
3484178354Ssam{
3485178354Ssam	getregdomain(s);
3486178354Ssam	if (channelsalso) {
3487178354Ssam		getchaninfo(s);
3488178354Ssam		spacer = ':';
3489178354Ssam		print_regdomain(&regdomain, 1);
3490178354Ssam		LINE_BREAK();
3491178354Ssam		print_channels(s, &chaninfo, 1/*allchans*/, 1/*verbose*/);
3492178354Ssam	} else
3493178354Ssam		print_regdomain(&regdomain, verbose);
3494178354Ssam}
3495178354Ssam
3496138593Ssamstatic
3497138593SsamDECL_CMD_FUNC(set80211list, arg, d)
3498138593Ssam{
3499138593Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
3500138593Ssam
3501173275Ssam	LINE_INIT('\t');
3502173275Ssam
3503138593Ssam	if (iseq(arg, "sta"))
3504138593Ssam		list_stations(s);
3505138593Ssam	else if (iseq(arg, "scan") || iseq(arg, "ap"))
3506138593Ssam		list_scan(s);
3507138593Ssam	else if (iseq(arg, "chan") || iseq(arg, "freq"))
3508138593Ssam		list_channels(s, 1);
3509138593Ssam	else if (iseq(arg, "active"))
3510138593Ssam		list_channels(s, 0);
3511138593Ssam	else if (iseq(arg, "keys"))
3512138593Ssam		list_keys(s);
3513138593Ssam	else if (iseq(arg, "caps"))
3514138593Ssam		list_capabilities(s);
3515178354Ssam	else if (iseq(arg, "wme") || iseq(arg, "wmm"))
3516138593Ssam		list_wme(s);
3517149029Ssam	else if (iseq(arg, "mac"))
3518149029Ssam		list_mac(s);
3519170531Ssam	else if (iseq(arg, "txpow"))
3520170531Ssam		list_txpow(s);
3521178354Ssam	else if (iseq(arg, "roam"))
3522178354Ssam		list_roam(s);
3523178354Ssam	else if (iseq(arg, "txparam") || iseq(arg, "txparm"))
3524178354Ssam		list_txparams(s);
3525178354Ssam	else if (iseq(arg, "regdomain"))
3526178354Ssam		list_regdomain(s, 1);
3527178354Ssam	else if (iseq(arg, "countries"))
3528178354Ssam		list_countries();
3529138593Ssam	else
3530138593Ssam		errx(1, "Don't know how to list %s for %s", arg, name);
3531178354Ssam	LINE_BREAK();
3532138593Ssam#undef iseq
3533138593Ssam}
3534138593Ssam
3535138593Ssamstatic enum ieee80211_opmode
3536138593Ssamget80211opmode(int s)
3537138593Ssam{
3538138593Ssam	struct ifmediareq ifmr;
3539138593Ssam
3540138593Ssam	(void) memset(&ifmr, 0, sizeof(ifmr));
3541138593Ssam	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
3542138593Ssam
3543138593Ssam	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
3544186101Ssam		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) {
3545186101Ssam			if (ifmr.ifm_current & IFM_FLAG0)
3546186101Ssam				return IEEE80211_M_AHDEMO;
3547186101Ssam			else
3548186101Ssam				return IEEE80211_M_IBSS;
3549186101Ssam		}
3550138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
3551138593Ssam			return IEEE80211_M_HOSTAP;
3552138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
3553138593Ssam			return IEEE80211_M_MONITOR;
3554138593Ssam	}
3555138593Ssam	return IEEE80211_M_STA;
3556138593Ssam}
3557138593Ssam
3558138593Ssam#if 0
3559138593Ssamstatic void
3560138593Ssamprintcipher(int s, struct ieee80211req *ireq, int keylenop)
3561138593Ssam{
3562138593Ssam	switch (ireq->i_val) {
3563138593Ssam	case IEEE80211_CIPHER_WEP:
3564138593Ssam		ireq->i_type = keylenop;
3565138593Ssam		if (ioctl(s, SIOCG80211, ireq) != -1)
3566138593Ssam			printf("WEP-%s",
3567138593Ssam			    ireq->i_len <= 5 ? "40" :
3568138593Ssam			    ireq->i_len <= 13 ? "104" : "128");
3569138593Ssam		else
3570138593Ssam			printf("WEP");
3571138593Ssam		break;
3572138593Ssam	case IEEE80211_CIPHER_TKIP:
3573138593Ssam		printf("TKIP");
3574138593Ssam		break;
3575138593Ssam	case IEEE80211_CIPHER_AES_OCB:
3576138593Ssam		printf("AES-OCB");
3577138593Ssam		break;
3578138593Ssam	case IEEE80211_CIPHER_AES_CCM:
3579138593Ssam		printf("AES-CCM");
3580138593Ssam		break;
3581138593Ssam	case IEEE80211_CIPHER_CKIP:
3582138593Ssam		printf("CKIP");
3583138593Ssam		break;
3584138593Ssam	case IEEE80211_CIPHER_NONE:
3585138593Ssam		printf("NONE");
3586138593Ssam		break;
3587138593Ssam	default:
3588138593Ssam		printf("UNKNOWN (0x%x)", ireq->i_val);
3589138593Ssam		break;
3590138593Ssam	}
3591138593Ssam}
3592138593Ssam#endif
3593138593Ssam
3594155931Ssamstatic void
3595138593Ssamprintkey(const struct ieee80211req_key *ik)
3596138593Ssam{
3597138593Ssam	static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
3598138593Ssam	int keylen = ik->ik_keylen;
3599138593Ssam	int printcontents;
3600138593Ssam
3601148001Srwatson	printcontents = printkeys &&
3602138593Ssam		(memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
3603138593Ssam	if (printcontents)
3604138593Ssam		LINE_BREAK();
3605138593Ssam	switch (ik->ik_type) {
3606138593Ssam	case IEEE80211_CIPHER_WEP:
3607138593Ssam		/* compatibility */
3608155931Ssam		LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
3609138593Ssam		    keylen <= 5 ? "40-bit" :
3610138593Ssam		    keylen <= 13 ? "104-bit" : "128-bit");
3611138593Ssam		break;
3612138593Ssam	case IEEE80211_CIPHER_TKIP:
3613138593Ssam		if (keylen > 128/8)
3614138593Ssam			keylen -= 128/8;	/* ignore MIC for now */
3615155931Ssam		LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3616138593Ssam		break;
3617138593Ssam	case IEEE80211_CIPHER_AES_OCB:
3618155931Ssam		LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3619138593Ssam		break;
3620138593Ssam	case IEEE80211_CIPHER_AES_CCM:
3621155931Ssam		LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3622138593Ssam		break;
3623138593Ssam	case IEEE80211_CIPHER_CKIP:
3624155931Ssam		LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3625138593Ssam		break;
3626138593Ssam	case IEEE80211_CIPHER_NONE:
3627155931Ssam		LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3628138593Ssam		break;
3629138593Ssam	default:
3630155931Ssam		LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
3631138593Ssam			ik->ik_type, ik->ik_keyix+1, 8*keylen);
3632138593Ssam		break;
3633138593Ssam	}
3634138593Ssam	if (printcontents) {
3635138593Ssam		int i;
3636138593Ssam
3637138593Ssam		printf(" <");
3638138593Ssam		for (i = 0; i < keylen; i++)
3639138593Ssam			printf("%02x", ik->ik_keydata[i]);
3640138593Ssam		printf(">");
3641138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
3642138593Ssam		    (ik->ik_keyrsc != 0 || verbose))
3643146873Sjhb			printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
3644138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
3645138593Ssam		    (ik->ik_keytsc != 0 || verbose))
3646146873Sjhb			printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
3647138593Ssam		if (ik->ik_flags != 0 && verbose) {
3648138593Ssam			const char *sep = " ";
3649138593Ssam
3650138593Ssam			if (ik->ik_flags & IEEE80211_KEY_XMIT)
3651138593Ssam				printf("%stx", sep), sep = "+";
3652138593Ssam			if (ik->ik_flags & IEEE80211_KEY_RECV)
3653138593Ssam				printf("%srx", sep), sep = "+";
3654138593Ssam			if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
3655138593Ssam				printf("%sdef", sep), sep = "+";
3656138593Ssam		}
3657138593Ssam		LINE_BREAK();
3658138593Ssam	}
3659138593Ssam}
3660138593Ssam
3661138593Ssamstatic void
3662173275Ssamprintrate(const char *tag, int v, int defrate, int defmcs)
3663138593Ssam{
3664173275Ssam	if (v == 11)
3665173275Ssam		LINE_CHECK("%s 5.5", tag);
3666173275Ssam	else if (v & 0x80) {
3667173275Ssam		if (v != defmcs)
3668173275Ssam			LINE_CHECK("%s %d", tag, v &~ 0x80);
3669173275Ssam	} else {
3670173275Ssam		if (v != defrate)
3671173275Ssam			LINE_CHECK("%s %d", tag, v/2);
3672173275Ssam	}
3673173275Ssam}
3674173275Ssam
3675173275Ssamstatic int
3676173275Ssamgetssid(int s, int ix, void *data, size_t len, int *plen)
3677173275Ssam{
3678138593Ssam	struct ieee80211req ireq;
3679138593Ssam
3680138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3681138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3682173275Ssam	ireq.i_type = IEEE80211_IOC_SSID;
3683173275Ssam	ireq.i_val = ix;
3684173275Ssam	ireq.i_data = data;
3685173275Ssam	ireq.i_len = len;
3686173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3687173275Ssam		return -1;
3688173275Ssam	*plen = ireq.i_len;
3689173275Ssam	return 0;
3690173275Ssam}
369177218Sphk
3692173275Ssamstatic void
3693173275Ssamieee80211_status(int s)
3694173275Ssam{
3695173275Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
3696173275Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
3697173275Ssam	int i, num, wpa, wme, bgscan, bgscaninterval, val, len, wepmode;
3698173275Ssam	uint8_t data[32];
3699173275Ssam	const struct ieee80211_channel *c;
3700178354Ssam	const struct ieee80211_roamparam *rp;
3701178354Ssam	const struct ieee80211_txparam *tp;
3702173275Ssam
3703173275Ssam	if (getssid(s, -1, data, sizeof(data), &len) < 0) {
3704148686Sstefanf		/* If we can't get the SSID, this isn't an 802.11 device. */
370577218Sphk		return;
370677218Sphk	}
3707173275Ssam
3708173275Ssam	/*
3709173275Ssam	 * Invalidate cached state so printing status for multiple
3710173275Ssam	 * if's doesn't reuse the first interfaces' cached state.
3711173275Ssam	 */
3712173275Ssam	gotcurchan = 0;
3713178354Ssam	gotroam = 0;
3714178354Ssam	gottxparams = 0;
3715173275Ssam	gothtconf = 0;
3716178354Ssam	gotregdomain = 0;
3717173275Ssam
3718173275Ssam	if (get80211val(s, IEEE80211_IOC_NUMSSIDS, &num) < 0)
3719173275Ssam		num = 0;
3720138593Ssam	printf("\tssid ");
3721138593Ssam	if (num > 1) {
3722173275Ssam		for (i = 0; i < num; i++) {
3723173275Ssam			if (getssid(s, i, data, sizeof(data), &len) >= 0 && len > 0) {
3724173275Ssam				printf(" %d:", i + 1);
3725173275Ssam				print_string(data, len);
3726138593Ssam			}
372788748Sambrisko		}
3728138593Ssam	} else
3729173275Ssam		print_string(data, len);
373077218Sphk
3731173275Ssam	c = getcurchan(s);
3732170531Ssam	if (c->ic_freq != IEEE80211_CHAN_ANY) {
3733170531Ssam		char buf[14];
3734170531Ssam		printf(" channel %d (%u Mhz%s)", c->ic_ieee, c->ic_freq,
3735170531Ssam			get_chaninfo(c, 1, buf, sizeof(buf)));
3736138593Ssam	} else if (verbose)
3737138593Ssam		printf(" channel UNDEF");
3738138593Ssam
3739173275Ssam	if (get80211(s, IEEE80211_IOC_BSSID, data, IEEE80211_ADDR_LEN) >= 0 &&
3740173275Ssam	    (memcmp(data, zerobssid, sizeof(zerobssid)) != 0 || verbose))
3741173275Ssam		printf(" bssid %s", ether_ntoa((struct ether_addr *)data));
3742138593Ssam
3743173275Ssam	if (get80211len(s, IEEE80211_IOC_STATIONNAME, data, sizeof(data), &len) != -1) {
3744138593Ssam		printf("\n\tstationname ");
3745173275Ssam		print_string(data, len);
374677218Sphk	}
374777218Sphk
3748138593Ssam	spacer = ' ';		/* force first break */
3749138593Ssam	LINE_BREAK();
375077218Sphk
3751178354Ssam	list_regdomain(s, 0);
3752178354Ssam
3753173275Ssam	wpa = 0;
3754173275Ssam	if (get80211val(s, IEEE80211_IOC_AUTHMODE, &val) != -1) {
3755173275Ssam		switch (val) {
3756173275Ssam		case IEEE80211_AUTH_NONE:
3757173275Ssam			LINE_CHECK("authmode NONE");
3758173275Ssam			break;
3759173275Ssam		case IEEE80211_AUTH_OPEN:
3760173275Ssam			LINE_CHECK("authmode OPEN");
3761173275Ssam			break;
3762173275Ssam		case IEEE80211_AUTH_SHARED:
3763173275Ssam			LINE_CHECK("authmode SHARED");
3764173275Ssam			break;
3765173275Ssam		case IEEE80211_AUTH_8021X:
3766173275Ssam			LINE_CHECK("authmode 802.1x");
3767173275Ssam			break;
3768173275Ssam		case IEEE80211_AUTH_WPA:
3769173275Ssam			if (get80211val(s, IEEE80211_IOC_WPA, &wpa) < 0)
3770173275Ssam				wpa = 1;	/* default to WPA1 */
3771173275Ssam			switch (wpa) {
3772173275Ssam			case 2:
3773173275Ssam				LINE_CHECK("authmode WPA2/802.11i");
377477218Sphk				break;
3775173275Ssam			case 3:
3776173275Ssam				LINE_CHECK("authmode WPA1+WPA2/802.11i");
377777218Sphk				break;
3778127649Ssam			default:
3779173275Ssam				LINE_CHECK("authmode WPA");
3780127649Ssam				break;
3781173275Ssam			}
3782173275Ssam			break;
3783173275Ssam		case IEEE80211_AUTH_AUTO:
3784173275Ssam			LINE_CHECK("authmode AUTO");
3785173275Ssam			break;
3786173275Ssam		default:
3787173275Ssam			LINE_CHECK("authmode UNKNOWN (0x%x)", val);
3788173275Ssam			break;
3789127649Ssam		}
3790127649Ssam	}
3791127649Ssam
3792173275Ssam	if (wpa || verbose) {
3793178354Ssam		if (get80211val(s, IEEE80211_IOC_WPS, &val) != -1) {
3794178354Ssam			if (val)
3795178354Ssam				LINE_CHECK("wps");
3796178354Ssam			else if (verbose)
3797178354Ssam				LINE_CHECK("-wps");
3798178354Ssam		}
3799178354Ssam		if (get80211val(s, IEEE80211_IOC_TSN, &val) != -1) {
3800178354Ssam			if (val)
3801178354Ssam				LINE_CHECK("tsn");
3802178354Ssam			else if (verbose)
3803178354Ssam				LINE_CHECK("-tsn");
3804178354Ssam		}
3805173275Ssam		if (ioctl(s, IEEE80211_IOC_COUNTERMEASURES, &val) != -1) {
3806173275Ssam			if (val)
3807173275Ssam				LINE_CHECK("countermeasures");
3808173275Ssam			else if (verbose)
3809173275Ssam				LINE_CHECK("-countermeasures");
3810173275Ssam		}
3811178354Ssam#if 0
3812178354Ssam		/* XXX not interesting with WPA done in user space */
3813178354Ssam		ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
3814178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3815178354Ssam		}
3816178354Ssam
3817178354Ssam		ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
3818178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3819178354Ssam			LINE_CHECK("mcastcipher ");
3820178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
3821178354Ssam			spacer = ' ';
3822178354Ssam		}
3823178354Ssam
3824178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
3825178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3826178354Ssam			LINE_CHECK("ucastcipher ");
3827178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
3828178354Ssam		}
3829178354Ssam
3830178354Ssam		if (wpa & 2) {
3831178354Ssam			ireq.i_type = IEEE80211_IOC_RSNCAPS;
3832178354Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1) {
3833178354Ssam				LINE_CHECK("RSN caps 0x%x", ireq.i_val);
3834178354Ssam				spacer = ' ';
3835178354Ssam			}
3836178354Ssam		}
3837178354Ssam
3838178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
3839178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3840178354Ssam		}
3841178354Ssam#endif
3842173275Ssam	}
3843138593Ssam
3844173275Ssam	if (get80211val(s, IEEE80211_IOC_WEP, &wepmode) != -1 &&
3845173275Ssam	    wepmode != IEEE80211_WEP_NOSUP) {
3846173275Ssam		int firstkey;
3847173275Ssam
3848138718Ssam		switch (wepmode) {
3849173275Ssam		case IEEE80211_WEP_OFF:
3850173275Ssam			LINE_CHECK("privacy OFF");
3851173275Ssam			break;
3852173275Ssam		case IEEE80211_WEP_ON:
3853173275Ssam			LINE_CHECK("privacy ON");
3854173275Ssam			break;
3855173275Ssam		case IEEE80211_WEP_MIXED:
3856173275Ssam			LINE_CHECK("privacy MIXED");
3857173275Ssam			break;
3858173275Ssam		default:
3859173275Ssam			LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
3860173275Ssam			break;
386177218Sphk		}
386277218Sphk
386377218Sphk		/*
386477218Sphk		 * If we get here then we've got WEP support so we need
386577218Sphk		 * to print WEP status.
386691454Sbrooks		 */
386777218Sphk
3868173275Ssam		if (get80211val(s, IEEE80211_IOC_WEPTXKEY, &val) < 0) {
386977218Sphk			warn("WEP support, but no tx key!");
387077218Sphk			goto end;
387177218Sphk		}
3872173275Ssam		if (val != -1)
3873173275Ssam			LINE_CHECK("deftxkey %d", val+1);
3874138718Ssam		else if (wepmode != IEEE80211_WEP_OFF || verbose)
3875155931Ssam			LINE_CHECK("deftxkey UNDEF");
387677218Sphk
3877173275Ssam		if (get80211val(s, IEEE80211_IOC_NUMWEPKEYS, &num) < 0) {
387877218Sphk			warn("WEP support, but no NUMWEPKEYS support!");
387977218Sphk			goto end;
388077218Sphk		}
388177218Sphk
3882138593Ssam		firstkey = 1;
3883138593Ssam		for (i = 0; i < num; i++) {
3884138593Ssam			struct ieee80211req_key ik;
388577218Sphk
3886138593Ssam			memset(&ik, 0, sizeof(ik));
3887138593Ssam			ik.ik_keyix = i;
3888173275Ssam			if (get80211(s, IEEE80211_IOC_WPAKEY, &ik, sizeof(ik)) < 0) {
388977218Sphk				warn("WEP support, but can get keys!");
389077218Sphk				goto end;
389177218Sphk			}
3892138593Ssam			if (ik.ik_keylen != 0) {
3893138593Ssam				if (verbose)
3894138593Ssam					LINE_BREAK();
3895138593Ssam				printkey(&ik);
3896138593Ssam				firstkey = 0;
3897138593Ssam			}
3898138593Ssam		}
3899173275Ssamend:
3900173275Ssam		;
3901138593Ssam	}
3902138593Ssam
3903173275Ssam	if (get80211val(s, IEEE80211_IOC_POWERSAVE, &val) != -1 &&
3904173275Ssam	    val != IEEE80211_POWERSAVE_NOSUP ) {
3905173275Ssam		if (val != IEEE80211_POWERSAVE_OFF || verbose) {
3906173275Ssam			switch (val) {
3907173275Ssam			case IEEE80211_POWERSAVE_OFF:
3908173275Ssam				LINE_CHECK("powersavemode OFF");
3909173275Ssam				break;
3910173275Ssam			case IEEE80211_POWERSAVE_CAM:
3911173275Ssam				LINE_CHECK("powersavemode CAM");
3912173275Ssam				break;
3913173275Ssam			case IEEE80211_POWERSAVE_PSP:
3914173275Ssam				LINE_CHECK("powersavemode PSP");
3915173275Ssam				break;
3916173275Ssam			case IEEE80211_POWERSAVE_PSP_CAM:
3917173275Ssam				LINE_CHECK("powersavemode PSP-CAM");
3918173275Ssam				break;
3919138593Ssam			}
3920173275Ssam			if (get80211val(s, IEEE80211_IOC_POWERSAVESLEEP, &val) != -1)
3921173275Ssam				LINE_CHECK("powersavesleep %d", val);
3922138593Ssam		}
3923138593Ssam	}
3924138593Ssam
3925173275Ssam	if (get80211val(s, IEEE80211_IOC_TXPOWER, &val) != -1) {
3926173275Ssam		if (val & 1)
3927173275Ssam			LINE_CHECK("txpower %d.5", val/2);
3928173275Ssam		else
3929173275Ssam			LINE_CHECK("txpower %d", val/2);
3930173275Ssam	}
3931138593Ssam	if (verbose) {
3932173275Ssam		if (get80211val(s, IEEE80211_IOC_TXPOWMAX, &val) != -1)
3933173275Ssam			LINE_CHECK("txpowmax %.1f", val/2.);
3934138593Ssam	}
3935138593Ssam
3936178354Ssam	if (get80211val(s, IEEE80211_IOC_DOTD, &val) != -1) {
3937178354Ssam		if (val)
3938178354Ssam			LINE_CHECK("dotd");
3939178354Ssam		else if (verbose)
3940178354Ssam			LINE_CHECK("-dotd");
3941178354Ssam	}
3942178354Ssam
3943173275Ssam	if (get80211val(s, IEEE80211_IOC_RTSTHRESHOLD, &val) != -1) {
3944173275Ssam		if (val != IEEE80211_RTS_MAX || verbose)
3945173275Ssam			LINE_CHECK("rtsthreshold %d", val);
3946138593Ssam	}
3947138593Ssam
3948173275Ssam	if (get80211val(s, IEEE80211_IOC_FRAGTHRESHOLD, &val) != -1) {
3949173275Ssam		if (val != IEEE80211_FRAG_MAX || verbose)
3950173275Ssam			LINE_CHECK("fragthreshold %d", val);
3951170531Ssam	}
3952173275Ssam	if (opmode == IEEE80211_M_STA || verbose) {
3953173275Ssam		if (get80211val(s, IEEE80211_IOC_BMISSTHRESHOLD, &val) != -1) {
3954173275Ssam			if (val != IEEE80211_HWBMISS_MAX || verbose)
3955173275Ssam				LINE_CHECK("bmiss %d", val);
3956153354Ssam		}
3957153354Ssam	}
3958153354Ssam
3959178354Ssam	if (!verbose) {
3960178354Ssam		gettxparams(s);
3961178354Ssam		tp = &txparams.params[chan2mode(c)];
3962178354Ssam		printrate("ucastrate", tp->ucastrate,
3963178354Ssam		    IEEE80211_FIXED_RATE_NONE, IEEE80211_FIXED_RATE_NONE);
3964178354Ssam		printrate("mcastrate", tp->mcastrate, 2*1, 0x80|0);
3965178354Ssam		printrate("mgmtrate", tp->mgmtrate, 2*1, 0x80|0);
3966178354Ssam		if (tp->maxretry != 6)		/* XXX */
3967178354Ssam			LINE_CHECK("maxretry %d", tp->maxretry);
3968178354Ssam	} else {
3969178354Ssam		LINE_BREAK();
3970178354Ssam		list_txparams(s);
3971178354Ssam	}
3972170531Ssam
3973173275Ssam	bgscaninterval = -1;
3974173275Ssam	(void) get80211val(s, IEEE80211_IOC_BGSCAN_INTERVAL, &bgscaninterval);
3975173275Ssam
3976173275Ssam	if (get80211val(s, IEEE80211_IOC_SCANVALID, &val) != -1) {
3977173275Ssam		if (val != bgscaninterval || verbose)
3978173275Ssam			LINE_CHECK("scanvalid %u", val);
3979148416Ssam	}
3980148416Ssam
3981173275Ssam	bgscan = 0;
3982173275Ssam	if (get80211val(s, IEEE80211_IOC_BGSCAN, &bgscan) != -1) {
3983173275Ssam		if (bgscan)
3984170531Ssam			LINE_CHECK("bgscan");
3985170531Ssam		else if (verbose)
3986170531Ssam			LINE_CHECK("-bgscan");
3987160687Ssam	}
3988170531Ssam	if (bgscan || verbose) {
3989170531Ssam		if (bgscaninterval != -1)
3990170531Ssam			LINE_CHECK("bgscanintvl %u", bgscaninterval);
3991173275Ssam		if (get80211val(s, IEEE80211_IOC_BGSCAN_IDLE, &val) != -1)
3992173275Ssam			LINE_CHECK("bgscanidle %u", val);
3993178354Ssam		if (!verbose) {
3994178354Ssam			getroam(s);
3995178354Ssam			rp = &roamparams.params[chan2mode(c)];
3996178354Ssam			if (rp->rssi & 1)
3997178354Ssam				LINE_CHECK("roam:rssi %u.5", rp->rssi/2);
3998178354Ssam			else
3999178354Ssam				LINE_CHECK("roam:rssi %u", rp->rssi/2);
4000178354Ssam			LINE_CHECK("roam:rate %u", rp->rate/2);
4001178354Ssam		} else {
4002178354Ssam			LINE_BREAK();
4003178354Ssam			list_roam(s);
4004170531Ssam		}
4005170531Ssam	}
4006160687Ssam
4007165570Ssam	if (IEEE80211_IS_CHAN_ANYG(c) || verbose) {
4008173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREG, &val) != -1) {
4009173275Ssam			if (val)
4010155931Ssam				LINE_CHECK("pureg");
4011147795Ssam			else if (verbose)
4012155931Ssam				LINE_CHECK("-pureg");
4013147795Ssam		}
4014173275Ssam		if (get80211val(s, IEEE80211_IOC_PROTMODE, &val) != -1) {
4015173275Ssam			switch (val) {
4016173275Ssam			case IEEE80211_PROTMODE_OFF:
4017173275Ssam				LINE_CHECK("protmode OFF");
4018173275Ssam				break;
4019173275Ssam			case IEEE80211_PROTMODE_CTS:
4020173275Ssam				LINE_CHECK("protmode CTS");
4021173275Ssam				break;
4022173275Ssam			case IEEE80211_PROTMODE_RTSCTS:
4023173275Ssam				LINE_CHECK("protmode RTSCTS");
4024173275Ssam				break;
4025173275Ssam			default:
4026173275Ssam				LINE_CHECK("protmode UNKNOWN (0x%x)", val);
4027173275Ssam				break;
4028138593Ssam			}
4029138593Ssam		}
4030138593Ssam	}
4031138593Ssam
4032173275Ssam	if (IEEE80211_IS_CHAN_HT(c) || verbose) {
4033173275Ssam		gethtconf(s);
4034173275Ssam		switch (htconf & 3) {
4035173275Ssam		case 0:
4036173275Ssam		case 2:
4037173275Ssam			LINE_CHECK("-ht");
4038173275Ssam			break;
4039173275Ssam		case 1:
4040173275Ssam			LINE_CHECK("ht20");
4041173275Ssam			break;
4042173275Ssam		case 3:
4043173275Ssam			if (verbose)
4044173275Ssam				LINE_CHECK("ht");
4045173275Ssam			break;
4046173275Ssam		}
4047173275Ssam		if (get80211val(s, IEEE80211_IOC_HTCOMPAT, &val) != -1) {
4048173275Ssam			if (!val)
4049173275Ssam				LINE_CHECK("-htcompat");
4050173275Ssam			else if (verbose)
4051173275Ssam				LINE_CHECK("htcompat");
4052173275Ssam		}
4053173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU, &val) != -1) {
4054173275Ssam			switch (val) {
4055173275Ssam			case 0:
4056173275Ssam				LINE_CHECK("-ampdu");
4057173275Ssam				break;
4058173275Ssam			case 1:
4059173275Ssam				LINE_CHECK("ampdutx -ampdurx");
4060173275Ssam				break;
4061173275Ssam			case 2:
4062173275Ssam				LINE_CHECK("-ampdutx ampdurx");
4063173275Ssam				break;
4064173275Ssam			case 3:
4065173275Ssam				if (verbose)
4066173275Ssam					LINE_CHECK("ampdu");
4067173275Ssam				break;
4068173275Ssam			}
4069173275Ssam		}
4070173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_LIMIT, &val) != -1) {
4071173275Ssam			switch (val) {
4072173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_8K:
4073173275Ssam				LINE_CHECK("ampdulimit 8k");
4074173275Ssam				break;
4075173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_16K:
4076173275Ssam				LINE_CHECK("ampdulimit 16k");
4077173275Ssam				break;
4078173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_32K:
4079173275Ssam				LINE_CHECK("ampdulimit 32k");
4080173275Ssam				break;
4081173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_64K:
4082173275Ssam				LINE_CHECK("ampdulimit 64k");
4083173275Ssam				break;
4084173275Ssam			}
4085173275Ssam		}
4086173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_DENSITY, &val) != -1) {
4087173275Ssam			switch (val) {
4088173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_NA:
4089173275Ssam				if (verbose)
4090183260Ssam					LINE_CHECK("ampdudensity NA");
4091173275Ssam				break;
4092173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_025:
4093173275Ssam				LINE_CHECK("ampdudensity .25");
4094173275Ssam				break;
4095173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_05:
4096173275Ssam				LINE_CHECK("ampdudensity .5");
4097173275Ssam				break;
4098173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_1:
4099173275Ssam				LINE_CHECK("ampdudensity 1");
4100173275Ssam				break;
4101173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_2:
4102173275Ssam				LINE_CHECK("ampdudensity 2");
4103173275Ssam				break;
4104173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_4:
4105173275Ssam				LINE_CHECK("ampdudensity 4");
4106173275Ssam				break;
4107173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_8:
4108173275Ssam				LINE_CHECK("ampdudensity 8");
4109173275Ssam				break;
4110173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_16:
4111173275Ssam				LINE_CHECK("ampdudensity 16");
4112173275Ssam				break;
4113173275Ssam			}
4114173275Ssam		}
4115173275Ssam		if (get80211val(s, IEEE80211_IOC_AMSDU, &val) != -1) {
4116173275Ssam			switch (val) {
4117173275Ssam			case 0:
4118173275Ssam				LINE_CHECK("-amsdu");
4119173275Ssam				break;
4120173275Ssam			case 1:
4121173275Ssam				LINE_CHECK("amsdutx -amsdurx");
4122173275Ssam				break;
4123173275Ssam			case 2:
4124173275Ssam				LINE_CHECK("-amsdutx amsdurx");
4125173275Ssam				break;
4126173275Ssam			case 3:
4127173275Ssam				if (verbose)
4128173275Ssam					LINE_CHECK("amsdu");
4129173275Ssam				break;
4130173275Ssam			}
4131173275Ssam		}
4132173275Ssam		/* XXX amsdu limit */
4133173275Ssam		if (get80211val(s, IEEE80211_IOC_SHORTGI, &val) != -1) {
4134173275Ssam			if (val)
4135173275Ssam				LINE_CHECK("shortgi");
4136173275Ssam			else if (verbose)
4137173275Ssam				LINE_CHECK("-shortgi");
4138173275Ssam		}
4139173275Ssam		if (get80211val(s, IEEE80211_IOC_HTPROTMODE, &val) != -1) {
4140173275Ssam			if (val == IEEE80211_PROTMODE_OFF)
4141173275Ssam				LINE_CHECK("htprotmode OFF");
4142173275Ssam			else if (val != IEEE80211_PROTMODE_RTSCTS)
4143173275Ssam				LINE_CHECK("htprotmode UNKNOWN (0x%x)", val);
4144173275Ssam			else if (verbose)
4145173275Ssam				LINE_CHECK("htprotmode RTSCTS");
4146173275Ssam		}
4147173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREN, &val) != -1) {
4148173275Ssam			if (val)
4149173275Ssam				LINE_CHECK("puren");
4150173275Ssam			else if (verbose)
4151173275Ssam				LINE_CHECK("-puren");
4152173275Ssam		}
4153183261Ssam		if (get80211val(s, IEEE80211_IOC_SMPS, &val) != -1) {
4154183261Ssam			if (val == IEEE80211_HTCAP_SMPS_DYNAMIC)
4155183261Ssam				LINE_CHECK("smpsdyn");
4156183261Ssam			else if (val == IEEE80211_HTCAP_SMPS_ENA)
4157183261Ssam				LINE_CHECK("smps");
4158183261Ssam			else if (verbose)
4159183261Ssam				LINE_CHECK("-smps");
4160183261Ssam		}
4161183261Ssam		if (get80211val(s, IEEE80211_IOC_RIFS, &val) != -1) {
4162183261Ssam			if (val)
4163183261Ssam				LINE_CHECK("rifs");
4164183261Ssam			else if (verbose)
4165183261Ssam				LINE_CHECK("-rifs");
4166183261Ssam		}
4167173275Ssam	}
4168173275Ssam
4169173275Ssam	if (get80211val(s, IEEE80211_IOC_WME, &wme) != -1) {
4170138593Ssam		if (wme)
4171155931Ssam			LINE_CHECK("wme");
4172138593Ssam		else if (verbose)
4173155931Ssam			LINE_CHECK("-wme");
4174138593Ssam	} else
4175138593Ssam		wme = 0;
4176138593Ssam
4177173275Ssam	if (get80211val(s, IEEE80211_IOC_BURST, &val) != -1) {
4178173275Ssam		if (val)
4179155931Ssam			LINE_CHECK("burst");
4180153422Ssam		else if (verbose)
4181155931Ssam			LINE_CHECK("-burst");
4182153422Ssam	}
4183153422Ssam
4184173275Ssam	if (get80211val(s, IEEE80211_IOC_FF, &val) != -1) {
4185173275Ssam		if (val)
4186170531Ssam			LINE_CHECK("ff");
4187170531Ssam		else if (verbose)
4188170531Ssam			LINE_CHECK("-ff");
4189170531Ssam	}
4190173275Ssam	if (get80211val(s, IEEE80211_IOC_TURBOP, &val) != -1) {
4191173275Ssam		if (val)
4192170531Ssam			LINE_CHECK("dturbo");
4193170531Ssam		else if (verbose)
4194170531Ssam			LINE_CHECK("-dturbo");
4195170531Ssam	}
4196178354Ssam	if (get80211val(s, IEEE80211_IOC_DWDS, &val) != -1) {
4197178354Ssam		if (val)
4198178354Ssam			LINE_CHECK("dwds");
4199178354Ssam		else if (verbose)
4200178354Ssam			LINE_CHECK("-dwds");
4201178354Ssam	}
4202170531Ssam
4203138593Ssam	if (opmode == IEEE80211_M_HOSTAP) {
4204173275Ssam		if (get80211val(s, IEEE80211_IOC_HIDESSID, &val) != -1) {
4205173275Ssam			if (val)
4206168075Ssam				LINE_CHECK("hidessid");
4207138593Ssam			else if (verbose)
4208168075Ssam				LINE_CHECK("-hidessid");
4209138593Ssam		}
4210173275Ssam		if (get80211val(s, IEEE80211_IOC_APBRIDGE, &val) != -1) {
4211173275Ssam			if (!val)
4212155931Ssam				LINE_CHECK("-apbridge");
4213138593Ssam			else if (verbose)
4214155931Ssam				LINE_CHECK("apbridge");
4215138593Ssam		}
4216173275Ssam		if (get80211val(s, IEEE80211_IOC_DTIM_PERIOD, &val) != -1)
4217173275Ssam			LINE_CHECK("dtimperiod %u", val);
4218138593Ssam
4219173275Ssam		if (get80211val(s, IEEE80211_IOC_DOTH, &val) != -1) {
4220173275Ssam			if (!val)
4221170531Ssam				LINE_CHECK("-doth");
4222170531Ssam			else if (verbose)
4223170531Ssam				LINE_CHECK("doth");
4224170531Ssam		}
4225178354Ssam		if (get80211val(s, IEEE80211_IOC_DFS, &val) != -1) {
4226178354Ssam			if (!val)
4227178354Ssam				LINE_CHECK("-dfs");
4228178354Ssam			else if (verbose)
4229178354Ssam				LINE_CHECK("dfs");
4230178354Ssam		}
4231173275Ssam		if (get80211val(s, IEEE80211_IOC_INACTIVITY, &val) != -1) {
4232173275Ssam			if (!val)
4233173275Ssam				LINE_CHECK("-inact");
4234173275Ssam			else if (verbose)
4235173275Ssam				LINE_CHECK("inact");
4236173275Ssam		}
4237138593Ssam	} else {
4238173275Ssam		if (get80211val(s, IEEE80211_IOC_ROAMING, &val) != -1) {
4239173275Ssam			if (val != IEEE80211_ROAMING_AUTO || verbose) {
4240173275Ssam				switch (val) {
4241138593Ssam				case IEEE80211_ROAMING_DEVICE:
4242155931Ssam					LINE_CHECK("roaming DEVICE");
4243138593Ssam					break;
4244138593Ssam				case IEEE80211_ROAMING_AUTO:
4245155931Ssam					LINE_CHECK("roaming AUTO");
4246138593Ssam					break;
4247138593Ssam				case IEEE80211_ROAMING_MANUAL:
4248155931Ssam					LINE_CHECK("roaming MANUAL");
4249138593Ssam					break;
4250138593Ssam				default:
4251155931Ssam					LINE_CHECK("roaming UNKNOWN (0x%x)",
4252173275Ssam						val);
4253138593Ssam					break;
4254138593Ssam				}
4255138593Ssam			}
4256138593Ssam		}
4257138593Ssam	}
4258186101Ssam
4259173275Ssam	if (get80211val(s, IEEE80211_IOC_BEACON_INTERVAL, &val) != -1) {
4260173275Ssam		/* XXX default define not visible */
4261173275Ssam		if (val != 100 || verbose)
4262173275Ssam			LINE_CHECK("bintval %u", val);
4263138593Ssam	}
4264138593Ssam
4265138593Ssam	if (wme && verbose) {
4266138593Ssam		LINE_BREAK();
4267138593Ssam		list_wme(s);
4268138593Ssam	}
4269173275Ssam	LINE_BREAK();
4270173275Ssam}
4271138593Ssam
4272173275Ssamstatic int
4273173275Ssamget80211(int s, int type, void *data, int len)
4274173275Ssam{
4275173275Ssam	struct ieee80211req ireq;
4276138593Ssam
4277173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4278173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4279173275Ssam	ireq.i_type = type;
4280173275Ssam	ireq.i_data = data;
4281173275Ssam	ireq.i_len = len;
4282173275Ssam	return ioctl(s, SIOCG80211, &ireq);
4283173275Ssam}
4284138593Ssam
4285173275Ssamstatic int
4286173275Ssamget80211len(int s, int type, void *data, int len, int *plen)
4287173275Ssam{
4288173275Ssam	struct ieee80211req ireq;
4289138593Ssam
4290173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4291173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4292173275Ssam	ireq.i_type = type;
4293173275Ssam	ireq.i_len = len;
4294173275Ssam	ireq.i_data = data;
4295173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4296173275Ssam		return -1;
4297173275Ssam	*plen = ireq.i_len;
4298173275Ssam	return 0;
4299173275Ssam}
4300138593Ssam
4301173275Ssamstatic int
4302173275Ssamget80211val(int s, int type, int *val)
4303173275Ssam{
4304173275Ssam	struct ieee80211req ireq;
430577218Sphk
4306173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4307173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4308173275Ssam	ireq.i_type = type;
4309173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4310173275Ssam		return -1;
4311173275Ssam	*val = ireq.i_val;
4312173275Ssam	return 0;
431377218Sphk}
431477218Sphk
431577218Sphkstatic void
4316170531Ssamset80211(int s, int type, int val, int len, void *data)
431777218Sphk{
431877218Sphk	struct ieee80211req	ireq;
431977218Sphk
432077218Sphk	(void) memset(&ireq, 0, sizeof(ireq));
432177218Sphk	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
432277218Sphk	ireq.i_type = type;
432377218Sphk	ireq.i_val = val;
432477218Sphk	ireq.i_len = len;
432577218Sphk	ireq.i_data = data;
432691454Sbrooks	if (ioctl(s, SIOCS80211, &ireq) < 0)
432777218Sphk		err(1, "SIOCS80211");
432877218Sphk}
432977218Sphk
433077218Sphkstatic const char *
433177218Sphkget_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
433277218Sphk{
433377218Sphk	int len;
433477218Sphk	int hexstr;
433577218Sphk	u_int8_t *p;
433677218Sphk
433777218Sphk	len = *lenp;
433877218Sphk	p = buf;
433977218Sphk	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
434077218Sphk	if (hexstr)
434177218Sphk		val += 2;
434277218Sphk	for (;;) {
434377218Sphk		if (*val == '\0')
434477218Sphk			break;
434577218Sphk		if (sep != NULL && strchr(sep, *val) != NULL) {
434677218Sphk			val++;
434777218Sphk			break;
434877218Sphk		}
434977218Sphk		if (hexstr) {
4350127831Sphk			if (!isxdigit((u_char)val[0])) {
435177218Sphk				warnx("bad hexadecimal digits");
435277218Sphk				return NULL;
435377218Sphk			}
4354127831Sphk			if (!isxdigit((u_char)val[1])) {
4355127831Sphk				warnx("odd count hexadecimal digits");
4356127831Sphk				return NULL;
4357127831Sphk			}
435877218Sphk		}
4359127831Sphk		if (p >= buf + len) {
436077218Sphk			if (hexstr)
436177218Sphk				warnx("hexadecimal digits too long");
436277218Sphk			else
4363127831Sphk				warnx("string too long");
436477218Sphk			return NULL;
436577218Sphk		}
436677218Sphk		if (hexstr) {
436777218Sphk#define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
436877218Sphk			*p++ = (tohex((u_char)val[0]) << 4) |
436977218Sphk			    tohex((u_char)val[1]);
437077218Sphk#undef tohex
437177218Sphk			val += 2;
437277218Sphk		} else
437377218Sphk			*p++ = *val++;
437477218Sphk	}
437577218Sphk	len = p - buf;
437677218Sphk	/* The string "-" is treated as the empty string. */
4377165045Ssam	if (!hexstr && len == 1 && buf[0] == '-') {
437877218Sphk		len = 0;
4379165045Ssam		memset(buf, 0, *lenp);
4380165045Ssam	} else if (len < *lenp)
438177218Sphk		memset(p, 0, *lenp - len);
438277218Sphk	*lenp = len;
438377218Sphk	return val;
438477218Sphk}
438577218Sphk
438677218Sphkstatic void
438777218Sphkprint_string(const u_int8_t *buf, int len)
438877218Sphk{
438977218Sphk	int i;
439077218Sphk	int hasspc;
439177218Sphk
439277218Sphk	i = 0;
439377218Sphk	hasspc = 0;
439491454Sbrooks	for (; i < len; i++) {
439577218Sphk		if (!isprint(buf[i]) && buf[i] != '\0')
439677218Sphk			break;
439777218Sphk		if (isspace(buf[i]))
439877218Sphk			hasspc++;
439977218Sphk	}
440077218Sphk	if (i == len) {
440177218Sphk		if (hasspc || len == 0 || buf[0] == '\0')
440277218Sphk			printf("\"%.*s\"", len, buf);
440377218Sphk		else
440477218Sphk			printf("%.*s", len, buf);
440577218Sphk	} else {
440677218Sphk		printf("0x");
440777218Sphk		for (i = 0; i < len; i++)
440877218Sphk			printf("%02x", buf[i]);
440977218Sphk	}
441077218Sphk}
441177218Sphk
4412178354Ssam/*
4413178354Ssam * Virtual AP cloning support.
4414178354Ssam */
4415178354Ssamstatic struct ieee80211_clone_params params = {
4416178354Ssam	.icp_opmode	= IEEE80211_M_STA,	/* default to station mode */
4417178354Ssam};
4418178354Ssam
4419178354Ssamstatic void
4420178354Ssamwlan_create(int s, struct ifreq *ifr)
4421178354Ssam{
4422178354Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
4423178354Ssam
4424178354Ssam	if (params.icp_parent[0] == '\0')
4425178354Ssam		errx(1, "must specify a parent when creating a wlan device");
4426178354Ssam	if (params.icp_opmode == IEEE80211_M_WDS &&
4427178354Ssam	    memcmp(params.icp_bssid, zerobssid, sizeof(zerobssid)) == 0)
4428178354Ssam		errx(1, "no bssid specified for WDS (use wlanbssid)");
4429178354Ssam	ifr->ifr_data = (caddr_t) &params;
4430178354Ssam	if (ioctl(s, SIOCIFCREATE2, ifr) < 0)
4431178354Ssam		err(1, "SIOCIFCREATE2");
4432178354Ssam}
4433178354Ssam
4434178354Ssamstatic
4435178354SsamDECL_CMD_FUNC(set80211clone_wlandev, arg, d)
4436178354Ssam{
4437178354Ssam	strlcpy(params.icp_parent, arg, IFNAMSIZ);
4438178354Ssam	clone_setcallback(wlan_create);
4439178354Ssam}
4440178354Ssam
4441178354Ssamstatic
4442178354SsamDECL_CMD_FUNC(set80211clone_wlanbssid, 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 bssid", arg);
4449178354Ssam	memcpy(params.icp_bssid, ea->octet, IEEE80211_ADDR_LEN);
4450178354Ssam	clone_setcallback(wlan_create);
4451178354Ssam}
4452178354Ssam
4453178354Ssamstatic
4454178354SsamDECL_CMD_FUNC(set80211clone_wlanaddr, arg, d)
4455178354Ssam{
4456178354Ssam	const struct ether_addr *ea;
4457178354Ssam
4458178354Ssam	ea = ether_aton(arg);
4459178354Ssam	if (ea == NULL)
4460178354Ssam		errx(1, "%s: cannot parse addres", arg);
4461178354Ssam	memcpy(params.icp_macaddr, ea->octet, IEEE80211_ADDR_LEN);
4462178354Ssam	params.icp_flags |= IEEE80211_CLONE_MACADDR;
4463178354Ssam	clone_setcallback(wlan_create);
4464178354Ssam}
4465178354Ssam
4466178354Ssamstatic
4467178354SsamDECL_CMD_FUNC(set80211clone_wlanmode, arg, d)
4468178354Ssam{
4469178354Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
4470178354Ssam	if (iseq(arg, "sta"))
4471178354Ssam		params.icp_opmode = IEEE80211_M_STA;
4472178354Ssam	else if (iseq(arg, "ahdemo") || iseq(arg, "adhoc-demo"))
4473178354Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
4474178354Ssam	else if (iseq(arg, "ibss") || iseq(arg, "adhoc"))
4475178354Ssam		params.icp_opmode = IEEE80211_M_IBSS;
4476178354Ssam	else if (iseq(arg, "ap") || iseq(arg, "host"))
4477178354Ssam		params.icp_opmode = IEEE80211_M_HOSTAP;
4478178354Ssam	else if (iseq(arg, "wds"))
4479178354Ssam		params.icp_opmode = IEEE80211_M_WDS;
4480178354Ssam	else if (iseq(arg, "monitor"))
4481178354Ssam		params.icp_opmode = IEEE80211_M_MONITOR;
4482178354Ssam	else
4483178354Ssam		errx(1, "Don't know to create %s for %s", arg, name);
4484178354Ssam	clone_setcallback(wlan_create);
4485178354Ssam#undef iseq
4486178354Ssam}
4487178354Ssam
4488178354Ssamstatic void
4489178354Ssamset80211clone_beacons(const char *val, int d, int s, const struct afswtch *rafp)
4490178354Ssam{
4491178354Ssam	/* NB: inverted sense */
4492178354Ssam	if (d)
4493178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_NOBEACONS;
4494178354Ssam	else
4495178354Ssam		params.icp_flags |= IEEE80211_CLONE_NOBEACONS;
4496178354Ssam	clone_setcallback(wlan_create);
4497178354Ssam}
4498178354Ssam
4499178354Ssamstatic void
4500178354Ssamset80211clone_bssid(const char *val, int d, int s, const struct afswtch *rafp)
4501178354Ssam{
4502178354Ssam	if (d)
4503178354Ssam		params.icp_flags |= IEEE80211_CLONE_BSSID;
4504178354Ssam	else
4505178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_BSSID;
4506178354Ssam	clone_setcallback(wlan_create);
4507178354Ssam}
4508178354Ssam
4509178354Ssamstatic void
4510178354Ssamset80211clone_wdslegacy(const char *val, int d, int s, const struct afswtch *rafp)
4511178354Ssam{
4512178354Ssam	if (d)
4513178354Ssam		params.icp_flags |= IEEE80211_CLONE_WDSLEGACY;
4514178354Ssam	else
4515178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_WDSLEGACY;
4516178354Ssam	clone_setcallback(wlan_create);
4517178354Ssam}
4518178354Ssam
4519138593Ssamstatic struct cmd ieee80211_cmds[] = {
4520138593Ssam	DEF_CMD_ARG("ssid",		set80211ssid),
4521138593Ssam	DEF_CMD_ARG("nwid",		set80211ssid),
4522138593Ssam	DEF_CMD_ARG("stationname",	set80211stationname),
4523138593Ssam	DEF_CMD_ARG("station",		set80211stationname),	/* BSD/OS */
4524138593Ssam	DEF_CMD_ARG("channel",		set80211channel),
4525138593Ssam	DEF_CMD_ARG("authmode",		set80211authmode),
4526138593Ssam	DEF_CMD_ARG("powersavemode",	set80211powersavemode),
4527138593Ssam	DEF_CMD("powersave",	1,	set80211powersave),
4528138593Ssam	DEF_CMD("-powersave",	0,	set80211powersave),
4529138593Ssam	DEF_CMD_ARG("powersavesleep", 	set80211powersavesleep),
4530138593Ssam	DEF_CMD_ARG("wepmode",		set80211wepmode),
4531138593Ssam	DEF_CMD("wep",		1,	set80211wep),
4532138593Ssam	DEF_CMD("-wep",		0,	set80211wep),
4533139493Ssam	DEF_CMD_ARG("deftxkey",		set80211weptxkey),
4534138593Ssam	DEF_CMD_ARG("weptxkey",		set80211weptxkey),
4535138593Ssam	DEF_CMD_ARG("wepkey",		set80211wepkey),
4536138593Ssam	DEF_CMD_ARG("nwkey",		set80211nwkey),		/* NetBSD */
4537138593Ssam	DEF_CMD("-nwkey",	0,	set80211wep),		/* NetBSD */
4538138593Ssam	DEF_CMD_ARG("rtsthreshold",	set80211rtsthreshold),
4539138593Ssam	DEF_CMD_ARG("protmode",		set80211protmode),
4540138593Ssam	DEF_CMD_ARG("txpower",		set80211txpower),
4541138593Ssam	DEF_CMD_ARG("roaming",		set80211roaming),
4542138593Ssam	DEF_CMD("wme",		1,	set80211wme),
4543138593Ssam	DEF_CMD("-wme",		0,	set80211wme),
4544178354Ssam	DEF_CMD("wmm",		1,	set80211wme),
4545178354Ssam	DEF_CMD("-wmm",		0,	set80211wme),
4546138593Ssam	DEF_CMD("hidessid",	1,	set80211hidessid),
4547138593Ssam	DEF_CMD("-hidessid",	0,	set80211hidessid),
4548138593Ssam	DEF_CMD("apbridge",	1,	set80211apbridge),
4549138593Ssam	DEF_CMD("-apbridge",	0,	set80211apbridge),
4550138593Ssam	DEF_CMD_ARG("chanlist",		set80211chanlist),
4551138593Ssam	DEF_CMD_ARG("bssid",		set80211bssid),
4552138593Ssam	DEF_CMD_ARG("ap",		set80211bssid),
4553138593Ssam	DEF_CMD("scan",	0,		set80211scan),
4554138593Ssam	DEF_CMD_ARG("list",		set80211list),
4555138593Ssam	DEF_CMD_ARG2("cwmin",		set80211cwmin),
4556138593Ssam	DEF_CMD_ARG2("cwmax",		set80211cwmax),
4557138593Ssam	DEF_CMD_ARG2("aifs",		set80211aifs),
4558138593Ssam	DEF_CMD_ARG2("txoplimit",	set80211txoplimit),
4559148621Ssam	DEF_CMD_ARG("acm",		set80211acm),
4560148621Ssam	DEF_CMD_ARG("-acm",		set80211noacm),
4561148621Ssam	DEF_CMD_ARG("ack",		set80211ackpolicy),
4562148621Ssam	DEF_CMD_ARG("-ack",		set80211noackpolicy),
4563138593Ssam	DEF_CMD_ARG2("bss:cwmin",	set80211bsscwmin),
4564138593Ssam	DEF_CMD_ARG2("bss:cwmax",	set80211bsscwmax),
4565138593Ssam	DEF_CMD_ARG2("bss:aifs",	set80211bssaifs),
4566138593Ssam	DEF_CMD_ARG2("bss:txoplimit",	set80211bsstxoplimit),
4567138593Ssam	DEF_CMD_ARG("dtimperiod",	set80211dtimperiod),
4568138593Ssam	DEF_CMD_ARG("bintval",		set80211bintval),
4569138593Ssam	DEF_CMD("mac:open",	IEEE80211_MACCMD_POLICY_OPEN,	set80211maccmd),
4570138593Ssam	DEF_CMD("mac:allow",	IEEE80211_MACCMD_POLICY_ALLOW,	set80211maccmd),
4571138593Ssam	DEF_CMD("mac:deny",	IEEE80211_MACCMD_POLICY_DENY,	set80211maccmd),
4572178354Ssam	DEF_CMD("mac:radius",	IEEE80211_MACCMD_POLICY_RADIUS,	set80211maccmd),
4573138593Ssam	DEF_CMD("mac:flush",	IEEE80211_MACCMD_FLUSH,		set80211maccmd),
4574138593Ssam	DEF_CMD("mac:detach",	IEEE80211_MACCMD_DETACH,	set80211maccmd),
4575138593Ssam	DEF_CMD_ARG("mac:add",		set80211addmac),
4576138593Ssam	DEF_CMD_ARG("mac:del",		set80211delmac),
4577138593Ssam	DEF_CMD_ARG("mac:kick",		set80211kickmac),
4578147795Ssam	DEF_CMD("pureg",	1,	set80211pureg),
4579147795Ssam	DEF_CMD("-pureg",	0,	set80211pureg),
4580170531Ssam	DEF_CMD("ff",		1,	set80211fastframes),
4581170531Ssam	DEF_CMD("-ff",		0,	set80211fastframes),
4582170531Ssam	DEF_CMD("dturbo",	1,	set80211dturbo),
4583170531Ssam	DEF_CMD("-dturbo",	0,	set80211dturbo),
4584170531Ssam	DEF_CMD("bgscan",	1,	set80211bgscan),
4585170531Ssam	DEF_CMD("-bgscan",	0,	set80211bgscan),
4586170531Ssam	DEF_CMD_ARG("bgscanidle",	set80211bgscanidle),
4587170531Ssam	DEF_CMD_ARG("bgscanintvl",	set80211bgscanintvl),
4588170531Ssam	DEF_CMD_ARG("scanvalid",	set80211scanvalid),
4589178354Ssam	DEF_CMD_ARG("roam:rssi",	set80211roamrssi),
4590178354Ssam	DEF_CMD_ARG("roam:rate",	set80211roamrate),
4591153354Ssam	DEF_CMD_ARG("mcastrate",	set80211mcastrate),
4592178354Ssam	DEF_CMD_ARG("ucastrate",	set80211ucastrate),
4593178354Ssam	DEF_CMD_ARG("mgtrate",		set80211mgtrate),
4594178354Ssam	DEF_CMD_ARG("mgmtrate",		set80211mgtrate),
4595178354Ssam	DEF_CMD_ARG("maxretry",		set80211maxretry),
4596148416Ssam	DEF_CMD_ARG("fragthreshold",	set80211fragthreshold),
4597153422Ssam	DEF_CMD("burst",	1,	set80211burst),
4598153422Ssam	DEF_CMD("-burst",	0,	set80211burst),
4599160687Ssam	DEF_CMD_ARG("bmiss",		set80211bmissthreshold),
4600160687Ssam	DEF_CMD_ARG("bmissthreshold",	set80211bmissthreshold),
4601173275Ssam	DEF_CMD("shortgi",	1,	set80211shortgi),
4602173275Ssam	DEF_CMD("-shortgi",	0,	set80211shortgi),
4603173275Ssam	DEF_CMD("ampdurx",	2,	set80211ampdu),
4604173275Ssam	DEF_CMD("-ampdurx",	-2,	set80211ampdu),
4605173275Ssam	DEF_CMD("ampdutx",	1,	set80211ampdu),
4606173275Ssam	DEF_CMD("-ampdutx",	-1,	set80211ampdu),
4607173275Ssam	DEF_CMD("ampdu",	3,	set80211ampdu),		/* NB: tx+rx */
4608173275Ssam	DEF_CMD("-ampdu",	-3,	set80211ampdu),
4609173275Ssam	DEF_CMD_ARG("ampdulimit",	set80211ampdulimit),
4610173275Ssam	DEF_CMD_ARG("ampdudensity",	set80211ampdudensity),
4611173275Ssam	DEF_CMD("amsdurx",	2,	set80211amsdu),
4612173275Ssam	DEF_CMD("-amsdurx",	-2,	set80211amsdu),
4613173275Ssam	DEF_CMD("amsdutx",	1,	set80211amsdu),
4614173275Ssam	DEF_CMD("-amsdutx",	-1,	set80211amsdu),
4615173275Ssam	DEF_CMD("amsdu",	3,	set80211amsdu),		/* NB: tx+rx */
4616173275Ssam	DEF_CMD("-amsdu",	-3,	set80211amsdu),
4617173275Ssam	DEF_CMD_ARG("amsdulimit",	set80211amsdulimit),
4618173275Ssam	DEF_CMD("puren",	1,	set80211puren),
4619173275Ssam	DEF_CMD("-puren",	0,	set80211puren),
4620170531Ssam	DEF_CMD("doth",		1,	set80211doth),
4621170531Ssam	DEF_CMD("-doth",	0,	set80211doth),
4622178354Ssam	DEF_CMD("dfs",		1,	set80211dfs),
4623178354Ssam	DEF_CMD("-dfs",		0,	set80211dfs),
4624173275Ssam	DEF_CMD("htcompat",	1,	set80211htcompat),
4625173275Ssam	DEF_CMD("-htcompat",	0,	set80211htcompat),
4626178354Ssam	DEF_CMD("dwds",		1,	set80211dwds),
4627178354Ssam	DEF_CMD("-dwds",	0,	set80211dwds),
4628173275Ssam	DEF_CMD("inact",	1,	set80211inact),
4629173275Ssam	DEF_CMD("-inact",	0,	set80211inact),
4630178354Ssam	DEF_CMD("tsn",		1,	set80211tsn),
4631178354Ssam	DEF_CMD("-tsn",		0,	set80211tsn),
4632178354Ssam	DEF_CMD_ARG("regdomain",	set80211regdomain),
4633178354Ssam	DEF_CMD_ARG("country",		set80211country),
4634178354Ssam	DEF_CMD("indoor",	'I',	set80211location),
4635178354Ssam	DEF_CMD("-indoor",	'O',	set80211location),
4636178354Ssam	DEF_CMD("outdoor",	'O',	set80211location),
4637178354Ssam	DEF_CMD("-outdoor",	'I',	set80211location),
4638178354Ssam	DEF_CMD("anywhere",	' ',	set80211location),
4639178354Ssam	DEF_CMD("ecm",		1,	set80211ecm),
4640178354Ssam	DEF_CMD("-ecm",		0,	set80211ecm),
4641178354Ssam	DEF_CMD("dotd",		1,	set80211dotd),
4642178354Ssam	DEF_CMD("-dotd",	0,	set80211dotd),
4643173275Ssam	DEF_CMD_ARG("htprotmode",	set80211htprotmode),
4644173275Ssam	DEF_CMD("ht20",		1,	set80211htconf),
4645173275Ssam	DEF_CMD("-ht20",	0,	set80211htconf),
4646173275Ssam	DEF_CMD("ht40",		3,	set80211htconf),	/* NB: 20+40 */
4647173275Ssam	DEF_CMD("-ht40",	0,	set80211htconf),
4648173275Ssam	DEF_CMD("ht",		3,	set80211htconf),	/* NB: 20+40 */
4649173275Ssam	DEF_CMD("-ht",		0,	set80211htconf),
4650183261Ssam	DEF_CMD("rifs",		1,	set80211rifs),
4651183261Ssam	DEF_CMD("-rifs",	0,	set80211rifs),
4652183261Ssam	DEF_CMD("smps",		IEEE80211_HTCAP_SMPS_ENA,	set80211smps),
4653183261Ssam	DEF_CMD("smpsdyn",	IEEE80211_HTCAP_SMPS_DYNAMIC,	set80211smps),
4654183261Ssam	DEF_CMD("-smps",	IEEE80211_HTCAP_SMPS_OFF,	set80211smps),
4655178354Ssam	/* XXX for testing */
4656178354Ssam	DEF_CMD_ARG("chanswitch",	set80211chanswitch),
4657178354Ssam
4658178354Ssam	/* vap cloning support */
4659178354Ssam	DEF_CLONE_CMD_ARG("wlanaddr",	set80211clone_wlanaddr),
4660178354Ssam	DEF_CLONE_CMD_ARG("wlanbssid",	set80211clone_wlanbssid),
4661178354Ssam	DEF_CLONE_CMD_ARG("wlandev",	set80211clone_wlandev),
4662178354Ssam	DEF_CLONE_CMD_ARG("wlanmode",	set80211clone_wlanmode),
4663178354Ssam	DEF_CLONE_CMD("beacons", 1,	set80211clone_beacons),
4664178354Ssam	DEF_CLONE_CMD("-beacons", 0,	set80211clone_beacons),
4665178354Ssam	DEF_CLONE_CMD("bssid",	1,	set80211clone_bssid),
4666178354Ssam	DEF_CLONE_CMD("-bssid",	0,	set80211clone_bssid),
4667178354Ssam	DEF_CLONE_CMD("wdslegacy", 1,	set80211clone_wdslegacy),
4668178354Ssam	DEF_CLONE_CMD("-wdslegacy", 0,	set80211clone_wdslegacy),
4669138593Ssam};
4670138593Ssamstatic struct afswtch af_ieee80211 = {
4671138593Ssam	.af_name	= "af_ieee80211",
4672138593Ssam	.af_af		= AF_UNSPEC,
4673139494Ssam	.af_other_status = ieee80211_status,
4674138593Ssam};
4675138593Ssam
4676138593Ssamstatic __constructor void
4677138593Ssamieee80211_ctor(void)
4678138593Ssam{
4679138593Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
4680138593Ssam	int i;
4681138593Ssam
4682138593Ssam	for (i = 0; i < N(ieee80211_cmds);  i++)
4683138593Ssam		cmd_register(&ieee80211_cmds[i]);
4684138593Ssam	af_register(&af_ieee80211);
4685138593Ssam#undef N
4686138593Ssam}
4687