ifieee80211.c revision 187841
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 187841 2009-01-28 19:15:52Z 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
82187801Ssam#include <assert.h>
8377218Sphk#include <ctype.h>
8477218Sphk#include <err.h>
8577218Sphk#include <errno.h>
8677218Sphk#include <fcntl.h>
87146873Sjhb#include <inttypes.h>
8877218Sphk#include <stdio.h>
8977218Sphk#include <stdlib.h>
9077218Sphk#include <string.h>
9177218Sphk#include <unistd.h>
92155931Ssam#include <stdarg.h>
93173275Ssam#include <stddef.h>		/* NB: for offsetof */
9477218Sphk
9577218Sphk#include "ifconfig.h"
96178354Ssam#include "regdomain.h"
9777218Sphk
98178354Ssam#ifndef IEEE80211_FIXED_RATE_NONE
99178354Ssam#define	IEEE80211_FIXED_RATE_NONE	0xff
100178354Ssam#endif
101178354Ssam
102178354Ssam#define	REQ_ECM		0x01000000	/* enable if ECM set */
103178354Ssam#define	REQ_OUTDOOR	0x02000000	/* enable for outdoor operation */
104178354Ssam#define	REQ_FLAGS	0xff000000	/* private flags, don't pass to os */
105178354Ssam
106178354Ssam/* XXX need these publicly defined or similar */
107178354Ssam#ifndef IEEE80211_NODE_AUTH
108178354Ssam#define	IEEE80211_NODE_AUTH	0x0001		/* authorized for data */
109178354Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
110178354Ssam#define	IEEE80211_NODE_ERP	0x0004		/* ERP enabled */
111178354Ssam#define	IEEE80211_NODE_PWR_MGT	0x0010		/* power save mode enabled */
112178354Ssam#define	IEEE80211_NODE_HT	0x0040		/* HT enabled */
113178354Ssam#define	IEEE80211_NODE_HTCOMPAT	0x0080		/* HT setup w/ vendor OUI's */
114178354Ssam#define	IEEE80211_NODE_WPS	0x0100		/* WPS association */
115178354Ssam#define	IEEE80211_NODE_TSN	0x0200		/* TSN association */
116183261Ssam#define	IEEE80211_NODE_AMPDU_RX	0x0400		/* AMPDU rx enabled */
117183261Ssam#define	IEEE80211_NODE_AMPDU_TX	0x0800		/* AMPDU tx enabled */
118183261Ssam#define	IEEE80211_NODE_MIMO_PS	0x1000		/* MIMO power save enabled */
119183261Ssam#define	IEEE80211_NODE_MIMO_RTS	0x2000		/* send RTS in MIMO PS */
120183261Ssam#define	IEEE80211_NODE_RIFS	0x4000		/* RIFS enabled */
121178354Ssam#endif
122178354Ssam
123187801Ssam#define	MAXCHAN	1536		/* max 1.5K channels */
124187801Ssam
125173275Ssam#define	MAXCOL	78
126173275Ssamstatic	int col;
127173275Ssamstatic	char spacer;
128173275Ssam
129173275Ssamstatic void LINE_INIT(char c);
130173275Ssamstatic void LINE_BREAK(void);
131173275Ssamstatic void LINE_CHECK(const char *fmt, ...);
132173275Ssam
133178354Ssamstatic const char *modename[] = {
134178354Ssam	"auto", "11a", "11b", "11g", "fh", "turboA", "turboG",
135178354Ssam	"sturbo", "11na", "11ng"
136173275Ssam};
137173275Ssam
138178354Ssamstatic void set80211(int s, int type, int val, int len, void *data);
139173275Ssamstatic int get80211(int s, int type, void *data, int len);
140173275Ssamstatic int get80211len(int s, int type, void *data, int len, int *plen);
141173275Ssamstatic int get80211val(int s, int type, int *val);
14277218Sphkstatic const char *get_string(const char *val, const char *sep,
14377218Sphk    u_int8_t *buf, int *lenp);
14477218Sphkstatic void print_string(const u_int8_t *buf, int len);
145178354Ssamstatic void print_regdomain(const struct ieee80211_regdomain *, int);
146178354Ssamstatic void print_channels(int, const struct ieee80211req_chaninfo *,
147178354Ssam    int allchans, int verbose);
148178354Ssamstatic void regdomain_makechannels(struct ieee80211_regdomain_req *,
149178354Ssam    const struct ieee80211_devcaps_req *);
15077218Sphk
151187801Ssamstatic struct ieee80211req_chaninfo *chaninfo;
152178354Ssamstatic struct ieee80211_regdomain regdomain;
153178354Ssamstatic int gotregdomain = 0;
154178354Ssamstatic struct ieee80211_roamparams_req roamparams;
155178354Ssamstatic int gotroam = 0;
156178354Ssamstatic struct ieee80211_txparams_req txparams;
157178354Ssamstatic int gottxparams = 0;
158173275Ssamstatic struct ieee80211_channel curchan;
159173275Ssamstatic int gotcurchan = 0;
160178354Ssamstatic struct ifmediareq *ifmr;
161173275Ssamstatic int htconf = 0;
162173275Ssamstatic	int gothtconf = 0;
163170531Ssam
164173275Ssamstatic void
165173275Ssamgethtconf(int s)
166173275Ssam{
167173275Ssam	if (gothtconf)
168173275Ssam		return;
169173275Ssam	if (get80211val(s, IEEE80211_IOC_HTCONF, &htconf) < 0)
170173275Ssam		warn("unable to get HT configuration information");
171173275Ssam	gothtconf = 1;
172173275Ssam}
173173275Ssam
174170531Ssam/*
175170531Ssam * Collect channel info from the kernel.  We use this (mostly)
176170531Ssam * to handle mapping between frequency and IEEE channel number.
177170531Ssam */
178170531Ssamstatic void
179170531Ssamgetchaninfo(int s)
180170531Ssam{
181187801Ssam	if (chaninfo != NULL)
182170531Ssam		return;
183187801Ssam	chaninfo = malloc(IEEE80211_CHANINFO_SIZE(MAXCHAN));
184187801Ssam	if (chaninfo == NULL)
185187801Ssam		errx(1, "no space for channel list");
186187801Ssam	if (get80211(s, IEEE80211_IOC_CHANINFO, chaninfo,
187187801Ssam	    IEEE80211_CHANINFO_SIZE(MAXCHAN)) < 0)
188187801Ssam		err(1, "unable to get channel information");
189170531Ssam	ifmr = ifmedia_getstate(s);
190173275Ssam	gethtconf(s);
191170531Ssam}
192170531Ssam
193178354Ssamstatic struct regdata *
194178354Ssamgetregdata(void)
195178354Ssam{
196178354Ssam	static struct regdata *rdp = NULL;
197178354Ssam	if (rdp == NULL) {
198178354Ssam		rdp = lib80211_alloc_regdata();
199178354Ssam		if (rdp == NULL)
200181198Ssam			errx(-1, "missing or corrupted regdomain database");
201178354Ssam	}
202178354Ssam	return rdp;
203178354Ssam}
204178354Ssam
205170531Ssam/*
206170531Ssam * Given the channel at index i with attributes from,
207170531Ssam * check if there is a channel with attributes to in
208170531Ssam * the channel table.  With suitable attributes this
209170531Ssam * allows the caller to look for promotion; e.g. from
210170531Ssam * 11b > 11g.
211170531Ssam */
212138593Ssamstatic int
213170531Ssamcanpromote(int i, int from, int to)
214170531Ssam{
215187801Ssam	const struct ieee80211_channel *fc = &chaninfo->ic_chans[i];
216170531Ssam	int j;
217170531Ssam
218170531Ssam	if ((fc->ic_flags & from) != from)
219170531Ssam		return i;
220170531Ssam	/* NB: quick check exploiting ordering of chans w/ same frequency */
221187801Ssam	if (i+1 < chaninfo->ic_nchans &&
222187801Ssam	    chaninfo->ic_chans[i+1].ic_freq == fc->ic_freq &&
223187801Ssam	    (chaninfo->ic_chans[i+1].ic_flags & to) == to)
224170531Ssam		return i+1;
225170531Ssam	/* brute force search in case channel list is not ordered */
226187801Ssam	for (j = 0; j < chaninfo->ic_nchans; j++) {
227187801Ssam		const struct ieee80211_channel *tc = &chaninfo->ic_chans[j];
228170531Ssam		if (j != i &&
229170531Ssam		    tc->ic_freq == fc->ic_freq && (tc->ic_flags & to) == to)
230170531Ssam		return j;
231170531Ssam	}
232170531Ssam	return i;
233170531Ssam}
234170531Ssam
235170531Ssam/*
236170531Ssam * Handle channel promotion.  When a channel is specified with
237170531Ssam * only a frequency we want to promote it to the ``best'' channel
238170531Ssam * available.  The channel list has separate entries for 11b, 11g,
239170531Ssam * 11a, and 11n[ga] channels so specifying a frequency w/o any
240170531Ssam * attributes requires we upgrade, e.g. from 11b -> 11g.  This
241170531Ssam * gets complicated when the channel is specified on the same
242170531Ssam * command line with a media request that constrains the available
243170531Ssam * channe list (e.g. mode 11a); we want to honor that to avoid
244170531Ssam * confusing behaviour.
245170531Ssam */
246170531Ssamstatic int
247170531Ssampromote(int i)
248170531Ssam{
249170531Ssam	/*
250170531Ssam	 * Query the current mode of the interface in case it's
251170531Ssam	 * constrained (e.g. to 11a).  We must do this carefully
252170531Ssam	 * as there may be a pending ifmedia request in which case
253170531Ssam	 * asking the kernel will give us the wrong answer.  This
254170531Ssam	 * is an unfortunate side-effect of the way ifconfig is
255170531Ssam	 * structure for modularity (yech).
256170531Ssam	 *
257170531Ssam	 * NB: ifmr is actually setup in getchaninfo (above); we
258170531Ssam	 *     assume it's called coincident with to this call so
259170531Ssam	 *     we have a ``current setting''; otherwise we must pass
260170531Ssam	 *     the socket descriptor down to here so we can make
261170531Ssam	 *     the ifmedia_getstate call ourselves.
262170531Ssam	 */
263170531Ssam	int chanmode = ifmr != NULL ? IFM_MODE(ifmr->ifm_current) : IFM_AUTO;
264170531Ssam
265170531Ssam	/* when ambiguous promote to ``best'' */
266170531Ssam	/* NB: we abitrarily pick HT40+ over HT40- */
267170531Ssam	if (chanmode != IFM_IEEE80211_11B)
268170531Ssam		i = canpromote(i, IEEE80211_CHAN_B, IEEE80211_CHAN_G);
269173275Ssam	if (chanmode != IFM_IEEE80211_11G && (htconf & 1)) {
270170531Ssam		i = canpromote(i, IEEE80211_CHAN_G,
271170531Ssam			IEEE80211_CHAN_G | IEEE80211_CHAN_HT20);
272173275Ssam		if (htconf & 2) {
273173275Ssam			i = canpromote(i, IEEE80211_CHAN_G,
274173275Ssam				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D);
275173275Ssam			i = canpromote(i, IEEE80211_CHAN_G,
276173275Ssam				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U);
277173275Ssam		}
278170531Ssam	}
279173275Ssam	if (chanmode != IFM_IEEE80211_11A && (htconf & 1)) {
280170531Ssam		i = canpromote(i, IEEE80211_CHAN_A,
281170531Ssam			IEEE80211_CHAN_A | IEEE80211_CHAN_HT20);
282173275Ssam		if (htconf & 2) {
283173275Ssam			i = canpromote(i, IEEE80211_CHAN_A,
284173275Ssam				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D);
285173275Ssam			i = canpromote(i, IEEE80211_CHAN_A,
286173275Ssam				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U);
287173275Ssam		}
288170531Ssam	}
289170531Ssam	return i;
290170531Ssam}
291170531Ssam
292170531Ssamstatic void
293170531Ssammapfreq(struct ieee80211_channel *chan, int freq, int flags)
294170531Ssam{
295170531Ssam	int i;
296170531Ssam
297187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
298187801Ssam		const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
299170531Ssam
300170531Ssam		if (c->ic_freq == freq && (c->ic_flags & flags) == flags) {
301170531Ssam			if (flags == 0) {
302170531Ssam				/* when ambiguous promote to ``best'' */
303187801Ssam				c = &chaninfo->ic_chans[promote(i)];
304170531Ssam			}
305170531Ssam			*chan = *c;
306170531Ssam			return;
307170531Ssam		}
308170531Ssam	}
309170531Ssam	errx(1, "unknown/undefined frequency %u/0x%x", freq, flags);
310170531Ssam}
311170531Ssam
312170531Ssamstatic void
313170531Ssammapchan(struct ieee80211_channel *chan, int ieee, int flags)
314170531Ssam{
315170531Ssam	int i;
316170531Ssam
317187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
318187801Ssam		const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
319170531Ssam
320170531Ssam		if (c->ic_ieee == ieee && (c->ic_flags & flags) == flags) {
321170531Ssam			if (flags == 0) {
322170531Ssam				/* when ambiguous promote to ``best'' */
323187801Ssam				c = &chaninfo->ic_chans[promote(i)];
324170531Ssam			}
325170531Ssam			*chan = *c;
326170531Ssam			return;
327170531Ssam		}
328170531Ssam	}
329173275Ssam	errx(1, "unknown/undefined channel number %d flags 0x%x", ieee, flags);
330170531Ssam}
331170531Ssam
332173275Ssamstatic const struct ieee80211_channel *
333173275Ssamgetcurchan(int s)
334173275Ssam{
335173275Ssam	if (gotcurchan)
336173275Ssam		return &curchan;
337173275Ssam	if (get80211(s, IEEE80211_IOC_CURCHAN, &curchan, sizeof(curchan)) < 0) {
338173275Ssam		int val;
339173275Ssam		/* fall back to legacy ioctl */
340173275Ssam		if (get80211val(s, IEEE80211_IOC_CHANNEL, &val) < 0)
341187801Ssam			err(-1, "cannot figure out current channel");
342173275Ssam		getchaninfo(s);
343173275Ssam		mapchan(&curchan, val, 0);
344173275Ssam	}
345173275Ssam	gotcurchan = 1;
346173275Ssam	return &curchan;
347173275Ssam}
348173275Ssam
349178354Ssamstatic enum ieee80211_phymode
350178354Ssamchan2mode(const struct ieee80211_channel *c)
351178354Ssam{
352178354Ssam	if (IEEE80211_IS_CHAN_HTA(c))
353178354Ssam		return IEEE80211_MODE_11NA;
354178354Ssam	if (IEEE80211_IS_CHAN_HTG(c))
355178354Ssam		return IEEE80211_MODE_11NG;
356178354Ssam	if (IEEE80211_IS_CHAN_108A(c))
357178354Ssam		return IEEE80211_MODE_TURBO_A;
358178354Ssam	if (IEEE80211_IS_CHAN_108G(c))
359178354Ssam		return IEEE80211_MODE_TURBO_G;
360178354Ssam	if (IEEE80211_IS_CHAN_ST(c))
361178354Ssam		return IEEE80211_MODE_STURBO_A;
362178354Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
363178354Ssam		return IEEE80211_MODE_FH;
364178354Ssam	if (IEEE80211_IS_CHAN_A(c))
365178354Ssam		return IEEE80211_MODE_11A;
366178354Ssam	if (IEEE80211_IS_CHAN_ANYG(c))
367178354Ssam		return IEEE80211_MODE_11G;
368178354Ssam	if (IEEE80211_IS_CHAN_B(c))
369178354Ssam		return IEEE80211_MODE_11B;
370178354Ssam	return IEEE80211_MODE_AUTO;
371178354Ssam}
372178354Ssam
373178354Ssamstatic void
374178354Ssamgetroam(int s)
375178354Ssam{
376178354Ssam	if (gotroam)
377178354Ssam		return;
378178354Ssam	if (get80211(s, IEEE80211_IOC_ROAM,
379178354Ssam	    &roamparams, sizeof(roamparams)) < 0)
380187801Ssam		err(1, "unable to get roaming parameters");
381178354Ssam	gotroam = 1;
382178354Ssam}
383178354Ssam
384178354Ssamstatic void
385178354Ssamsetroam_cb(int s, void *arg)
386178354Ssam{
387178354Ssam	struct ieee80211_roamparams_req *roam = arg;
388178354Ssam	set80211(s, IEEE80211_IOC_ROAM, 0, sizeof(*roam), roam);
389178354Ssam}
390178354Ssam
391178354Ssamstatic void
392178354Ssamgettxparams(int s)
393178354Ssam{
394178354Ssam	if (gottxparams)
395178354Ssam		return;
396178354Ssam	if (get80211(s, IEEE80211_IOC_TXPARAMS,
397178354Ssam	    &txparams, sizeof(txparams)) < 0)
398187801Ssam		err(1, "unable to get transmit parameters");
399178354Ssam	gottxparams = 1;
400178354Ssam}
401178354Ssam
402178354Ssamstatic void
403178354Ssamsettxparams_cb(int s, void *arg)
404178354Ssam{
405178354Ssam	struct ieee80211_txparams_req *txp = arg;
406178354Ssam	set80211(s, IEEE80211_IOC_TXPARAMS, 0, sizeof(*txp), txp);
407178354Ssam}
408178354Ssam
409178354Ssamstatic void
410178354Ssamgetregdomain(int s)
411178354Ssam{
412178354Ssam	if (gotregdomain)
413178354Ssam		return;
414178354Ssam	if (get80211(s, IEEE80211_IOC_REGDOMAIN,
415178354Ssam	    &regdomain, sizeof(regdomain)) < 0)
416187801Ssam		err(1, "unable to get regulatory domain info");
417178354Ssam	gotregdomain = 1;
418178354Ssam}
419178354Ssam
420178354Ssamstatic void
421178354Ssamgetdevcaps(int s, struct ieee80211_devcaps_req *dc)
422178354Ssam{
423187801Ssam	if (get80211(s, IEEE80211_IOC_DEVCAPS, dc,
424187801Ssam	    IEEE80211_DEVCAPS_SPACE(dc)) < 0)
425187801Ssam		err(1, "unable to get device capabilities");
426178354Ssam}
427178354Ssam
428178354Ssamstatic void
429178354Ssamsetregdomain_cb(int s, void *arg)
430178354Ssam{
431187801Ssam	struct ieee80211_regdomain_req *req;
432178354Ssam	struct ieee80211_regdomain *rd = arg;
433187801Ssam	struct ieee80211_devcaps_req *dc;
434178354Ssam	struct regdata *rdp = getregdata();
435178354Ssam
436186102Ssam	if (rd->country != NO_COUNTRY) {
437178354Ssam		const struct country *cc;
438178354Ssam		/*
439178354Ssam		 * Check current country seting to make sure it's
440178354Ssam		 * compatible with the new regdomain.  If not, then
441178354Ssam		 * override it with any default country for this
442178354Ssam		 * SKU.  If we cannot arrange a match, then abort.
443178354Ssam		 */
444178354Ssam		cc = lib80211_country_findbycc(rdp, rd->country);
445178354Ssam		if (cc == NULL)
446178354Ssam			errx(1, "unknown ISO country code %d", rd->country);
447178354Ssam		if (cc->rd->sku != rd->regdomain) {
448178354Ssam			const struct regdomain *rp;
449178354Ssam			/*
450178354Ssam			 * Check if country is incompatible with regdomain.
451178354Ssam			 * To enable multiple regdomains for a country code
452178354Ssam			 * we permit a mismatch between the regdomain and
453178354Ssam			 * the country's associated regdomain when the
454178354Ssam			 * regdomain is setup w/o a default country.  For
455178354Ssam			 * example, US is bound to the FCC regdomain but
456178354Ssam			 * we allow US to be combined with FCC3 because FCC3
457178354Ssam			 * has not default country.  This allows bogus
458178354Ssam			 * combinations like FCC3+DK which are resolved when
459178354Ssam			 * constructing the channel list by deferring to the
460178354Ssam			 * regdomain to construct the channel list.
461178354Ssam			 */
462178354Ssam			rp = lib80211_regdomain_findbysku(rdp, rd->regdomain);
463178354Ssam			if (rp == NULL)
464178354Ssam				errx(1, "country %s (%s) is not usable with "
465178354Ssam				    "regdomain %d", cc->isoname, cc->name,
466178354Ssam				    rd->regdomain);
467186102Ssam			else if (rp->cc != NULL && rp->cc != cc)
468178354Ssam				errx(1, "country %s (%s) is not usable with "
469178354Ssam				   "regdomain %s", cc->isoname, cc->name,
470178354Ssam				   rp->name);
471178354Ssam		}
472178354Ssam	}
473178354Ssam	/*
474178354Ssam	 * Fetch the device capabilities and calculate the
475178354Ssam	 * full set of netbands for which we request a new
476178354Ssam	 * channel list be constructed.  Once that's done we
477178354Ssam	 * push the regdomain info + channel list to the kernel.
478178354Ssam	 */
479187801Ssam	dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
480187801Ssam	if (dc == NULL)
481187801Ssam		errx(1, "no space for device capabilities");
482187801Ssam	dc->dc_chaninfo.ic_nchans = MAXCHAN;
483187801Ssam	getdevcaps(s, dc);
484178354Ssam#if 0
485178354Ssam	if (verbose) {
486187801Ssam		printf("drivercaps: 0x%x\n", dc->dc_drivercaps);
487187801Ssam		printf("cryptocaps: 0x%x\n", dc->dc_cryptocaps);
488187801Ssam		printf("htcaps    : 0x%x\n", dc->dc_htcaps);
489187801Ssam		memcpy(chaninfo, &dc->dc_chaninfo,
490187801Ssam		    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
491187801Ssam		print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, 1/*verbose*/);
492178354Ssam	}
493178354Ssam#endif
494187801Ssam	req = malloc(IEEE80211_REGDOMAIN_SIZE(dc->dc_chaninfo.ic_nchans));
495187801Ssam	if (req == NULL)
496187801Ssam		errx(1, "no space for regdomain request");
497187801Ssam	req->rd = *rd;
498187801Ssam	regdomain_makechannels(req, dc);
499178354Ssam	if (verbose) {
500178354Ssam		LINE_INIT(':');
501178354Ssam		print_regdomain(rd, 1/*verbose*/);
502178354Ssam		LINE_BREAK();
503187801Ssam		/* blech, reallocate channel list for new data */
504187801Ssam		if (chaninfo != NULL)
505187801Ssam			free(chaninfo);
506187801Ssam		chaninfo = malloc(IEEE80211_CHANINFO_SPACE(&req->chaninfo));
507187801Ssam		if (chaninfo == NULL)
508187801Ssam			errx(1, "no space for channel list");
509187801Ssam		memcpy(chaninfo, &req->chaninfo,
510187801Ssam		    IEEE80211_CHANINFO_SPACE(&req->chaninfo));
511187801Ssam		print_channels(s, &req->chaninfo, 1/*allchans*/, 1/*verbose*/);
512178354Ssam	}
513187801Ssam	if (req->chaninfo.ic_nchans == 0)
514178354Ssam		errx(1, "no channels calculated");
515187801Ssam	set80211(s, IEEE80211_IOC_REGDOMAIN, 0,
516187801Ssam	    IEEE80211_REGDOMAIN_SPACE(req), req);
517187801Ssam	free(req);
518187801Ssam	free(dc);
519178354Ssam}
520178354Ssam
521170531Ssamstatic int
522170531Ssamieee80211_mhz2ieee(int freq, int flags)
523170531Ssam{
524170531Ssam	struct ieee80211_channel chan;
525170531Ssam	mapfreq(&chan, freq, flags);
526170531Ssam	return chan.ic_ieee;
527170531Ssam}
528170531Ssam
529170531Ssamstatic int
530138593Ssamisanyarg(const char *arg)
531138593Ssam{
532173275Ssam	return (strncmp(arg, "-", 1) == 0 ||
533173275Ssam	    strncasecmp(arg, "any", 3) == 0 || strncasecmp(arg, "off", 3) == 0);
534138593Ssam}
535138593Ssam
536138593Ssamstatic void
53777218Sphkset80211ssid(const char *val, int d, int s, const struct afswtch *rafp)
53877218Sphk{
53977218Sphk	int		ssid;
54077218Sphk	int		len;
541151883Sbrooks	u_int8_t	data[IEEE80211_NWID_LEN];
54277218Sphk
54377218Sphk	ssid = 0;
544121827Sbrooks	len = strlen(val);
545178354Ssam	if (len > 2 && isdigit((int)val[0]) && val[1] == ':') {
54688748Sambrisko		ssid = atoi(val)-1;
54788748Sambrisko		val += 2;
54888748Sambrisko	}
54977218Sphk
55077218Sphk	bzero(data, sizeof(data));
55177218Sphk	len = sizeof(data);
552151883Sbrooks	if (get_string(val, NULL, data, &len) == NULL)
553151883Sbrooks		exit(1);
55477218Sphk
55577218Sphk	set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
55677218Sphk}
55777218Sphk
558138593Ssamstatic void
55977218Sphkset80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
56077218Sphk{
56177218Sphk	int			len;
56277218Sphk	u_int8_t		data[33];
56377218Sphk
56477218Sphk	bzero(data, sizeof(data));
56577218Sphk	len = sizeof(data);
56677218Sphk	get_string(val, NULL, data, &len);
56777218Sphk
56877218Sphk	set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
56977218Sphk}
57077218Sphk
571138593Ssam/*
572170531Ssam * Parse a channel specification for attributes/flags.
573170531Ssam * The syntax is:
574170531Ssam *	freq/xx		channel width (5,10,20,40,40+,40-)
575170531Ssam *	freq:mode	channel mode (a,b,g,h,n,t,s,d)
576170531Ssam *
577170531Ssam * These can be combined in either order; e.g. 2437:ng/40.
578170531Ssam * Modes are case insensitive.
579170531Ssam *
580170531Ssam * The result is not validated here; it's assumed to be
581170531Ssam * checked against the channel table fetched from the kernel.
582170531Ssam */
583170531Ssamstatic int
584173275Ssamgetchannelflags(const char *val, int freq)
585138593Ssam{
586170531Ssam#define	_CHAN_HT	0x80000000
587170531Ssam	const char *cp;
588170531Ssam	int flags;
589138593Ssam
590170531Ssam	flags = 0;
591166015Ssam
592170531Ssam	cp = strchr(val, ':');
593170531Ssam	if (cp != NULL) {
594170531Ssam		for (cp++; isalpha((int) *cp); cp++) {
595170531Ssam			/* accept mixed case */
596170531Ssam			int c = *cp;
597170531Ssam			if (isupper(c))
598170531Ssam				c = tolower(c);
599170531Ssam			switch (c) {
600170531Ssam			case 'a':		/* 802.11a */
601170531Ssam				flags |= IEEE80211_CHAN_A;
602170531Ssam				break;
603170531Ssam			case 'b':		/* 802.11b */
604170531Ssam				flags |= IEEE80211_CHAN_B;
605170531Ssam				break;
606170531Ssam			case 'g':		/* 802.11g */
607170531Ssam				flags |= IEEE80211_CHAN_G;
608170531Ssam				break;
609170531Ssam			case 'h':		/* ht = 802.11n */
610170531Ssam			case 'n':		/* 802.11n */
611170531Ssam				flags |= _CHAN_HT;	/* NB: private */
612170531Ssam				break;
613170531Ssam			case 'd':		/* dt = Atheros Dynamic Turbo */
614170531Ssam				flags |= IEEE80211_CHAN_TURBO;
615170531Ssam				break;
616170531Ssam			case 't':		/* ht, dt, st, t */
617170531Ssam				/* dt and unadorned t specify Dynamic Turbo */
618170531Ssam				if ((flags & (IEEE80211_CHAN_STURBO|_CHAN_HT)) == 0)
619170531Ssam					flags |= IEEE80211_CHAN_TURBO;
620170531Ssam				break;
621170531Ssam			case 's':		/* st = Atheros Static Turbo */
622170531Ssam				flags |= IEEE80211_CHAN_STURBO;
623170531Ssam				break;
624170531Ssam			default:
625173275Ssam				errx(-1, "%s: Invalid channel attribute %c\n",
626170531Ssam				    val, *cp);
627170531Ssam			}
628170531Ssam		}
629170531Ssam	}
630170531Ssam	cp = strchr(val, '/');
631170531Ssam	if (cp != NULL) {
632170531Ssam		char *ep;
633170531Ssam		u_long cw = strtoul(cp+1, &ep, 10);
634166015Ssam
635170531Ssam		switch (cw) {
636170531Ssam		case 5:
637170531Ssam			flags |= IEEE80211_CHAN_QUARTER;
638170531Ssam			break;
639170531Ssam		case 10:
640170531Ssam			flags |= IEEE80211_CHAN_HALF;
641170531Ssam			break;
642170531Ssam		case 20:
643170531Ssam			/* NB: this may be removed below */
644170531Ssam			flags |= IEEE80211_CHAN_HT20;
645170531Ssam			break;
646170531Ssam		case 40:
647170531Ssam			if (ep != NULL && *ep == '+')
648170531Ssam				flags |= IEEE80211_CHAN_HT40U;
649170531Ssam			else if (ep != NULL && *ep == '-')
650170531Ssam				flags |= IEEE80211_CHAN_HT40D;
651170531Ssam			break;
652170531Ssam		default:
653173275Ssam			errx(-1, "%s: Invalid channel width\n", val);
654170531Ssam		}
655165570Ssam	}
656170531Ssam	/*
657170531Ssam	 * Cleanup specifications.
658170531Ssam	 */
659170531Ssam	if ((flags & _CHAN_HT) == 0) {
660170531Ssam		/*
661170531Ssam		 * If user specified freq/20 or freq/40 quietly remove
662170531Ssam		 * HT cw attributes depending on channel use.  To give
663170531Ssam		 * an explicit 20/40 width for an HT channel you must
664170531Ssam		 * indicate it is an HT channel since all HT channels
665170531Ssam		 * are also usable for legacy operation; e.g. freq:n/40.
666170531Ssam		 */
667170531Ssam		flags &= ~IEEE80211_CHAN_HT;
668170531Ssam	} else {
669170531Ssam		/*
670170531Ssam		 * Remove private indicator that this is an HT channel
671170531Ssam		 * and if no explicit channel width has been given
672170531Ssam		 * provide the default settings.
673170531Ssam		 */
674170531Ssam		flags &= ~_CHAN_HT;
675173275Ssam		if ((flags & IEEE80211_CHAN_HT) == 0) {
676173275Ssam			struct ieee80211_channel chan;
677173275Ssam			/*
678173275Ssam			 * Consult the channel list to see if we can use
679173275Ssam			 * HT40+ or HT40- (if both the map routines choose).
680173275Ssam			 */
681173275Ssam			if (freq > 255)
682173275Ssam				mapfreq(&chan, freq, 0);
683173275Ssam			else
684173275Ssam				mapchan(&chan, freq, 0);
685173275Ssam			flags |= (chan.ic_flags & IEEE80211_CHAN_HT);
686173275Ssam		}
687170531Ssam	}
688170531Ssam	return flags;
689170531Ssam#undef _CHAN_HT
690138593Ssam}
691138593Ssam
692138593Ssamstatic void
693186105Ssamgetchannel(int s, struct ieee80211_channel *chan, const char *val)
694186105Ssam{
695186105Ssam	int v, flags;
696186105Ssam	char *eptr;
697186105Ssam
698186105Ssam	memset(chan, 0, sizeof(*chan));
699186105Ssam	if (isanyarg(val)) {
700186105Ssam		chan->ic_freq = IEEE80211_CHAN_ANY;
701186105Ssam		return;
702186105Ssam	}
703186105Ssam	getchaninfo(s);
704186105Ssam	errno = 0;
705186105Ssam	v = strtol(val, &eptr, 10);
706186105Ssam	if (val[0] == '\0' || val == eptr || errno == ERANGE ||
707186105Ssam	    /* channel may be suffixed with nothing, :flag, or /width */
708186105Ssam	    (eptr[0] != '\0' && eptr[0] != ':' && eptr[0] != '/'))
709186105Ssam		errx(1, "invalid channel specification%s",
710186105Ssam		    errno == ERANGE ? " (out of range)" : "");
711186105Ssam	flags = getchannelflags(val, v);
712186105Ssam	if (v > 255) {		/* treat as frequency */
713186105Ssam		mapfreq(chan, v, flags);
714186105Ssam	} else {
715186105Ssam		mapchan(chan, v, flags);
716186105Ssam	}
717186105Ssam}
718186105Ssam
719186105Ssamstatic void
72077218Sphkset80211channel(const char *val, int d, int s, const struct afswtch *rafp)
72177218Sphk{
722170531Ssam	struct ieee80211_channel chan;
723170531Ssam
724186105Ssam	getchannel(s, &chan, val);
725170531Ssam	set80211(s, IEEE80211_IOC_CURCHAN, 0, sizeof(chan), &chan);
72677218Sphk}
72777218Sphk
728138593Ssamstatic void
729178354Ssamset80211chanswitch(const char *val, int d, int s, const struct afswtch *rafp)
730178354Ssam{
731178354Ssam	struct ieee80211_chanswitch_req csr;
732178354Ssam
733186105Ssam	getchannel(s, &csr.csa_chan, val);
734178354Ssam	csr.csa_mode = 1;
735178354Ssam	csr.csa_count = 5;
736178354Ssam	set80211(s, IEEE80211_IOC_CHANSWITCH, 0, sizeof(csr), &csr);
737178354Ssam}
738178354Ssam
739178354Ssamstatic void
74077218Sphkset80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
74177218Sphk{
74277218Sphk	int	mode;
74377218Sphk
74491454Sbrooks	if (strcasecmp(val, "none") == 0) {
74577218Sphk		mode = IEEE80211_AUTH_NONE;
74691454Sbrooks	} else if (strcasecmp(val, "open") == 0) {
74777218Sphk		mode = IEEE80211_AUTH_OPEN;
74891454Sbrooks	} else if (strcasecmp(val, "shared") == 0) {
74977218Sphk		mode = IEEE80211_AUTH_SHARED;
750138593Ssam	} else if (strcasecmp(val, "8021x") == 0) {
751138593Ssam		mode = IEEE80211_AUTH_8021X;
752138593Ssam	} else if (strcasecmp(val, "wpa") == 0) {
753138593Ssam		mode = IEEE80211_AUTH_WPA;
75477218Sphk	} else {
755150708Sru		errx(1, "unknown authmode");
75677218Sphk	}
75777218Sphk
75877218Sphk	set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
75977218Sphk}
76077218Sphk
761138593Ssamstatic void
76277218Sphkset80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
76377218Sphk{
76477218Sphk	int	mode;
76577218Sphk
76691454Sbrooks	if (strcasecmp(val, "off") == 0) {
76777218Sphk		mode = IEEE80211_POWERSAVE_OFF;
76891454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
76977218Sphk		mode = IEEE80211_POWERSAVE_ON;
77091454Sbrooks	} else if (strcasecmp(val, "cam") == 0) {
77177218Sphk		mode = IEEE80211_POWERSAVE_CAM;
77291454Sbrooks	} else if (strcasecmp(val, "psp") == 0) {
77377218Sphk		mode = IEEE80211_POWERSAVE_PSP;
77491454Sbrooks	} else if (strcasecmp(val, "psp-cam") == 0) {
77577218Sphk		mode = IEEE80211_POWERSAVE_PSP_CAM;
77677218Sphk	} else {
777150708Sru		errx(1, "unknown powersavemode");
77877218Sphk	}
77977218Sphk
78077218Sphk	set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
78177218Sphk}
78277218Sphk
783138593Ssamstatic void
78477218Sphkset80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
78577218Sphk{
78677218Sphk	if (d == 0)
78777218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
78877218Sphk		    0, NULL);
78977218Sphk	else
79077218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
79177218Sphk		    0, NULL);
79277218Sphk}
79377218Sphk
794138593Ssamstatic void
79577218Sphkset80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
79677218Sphk{
79777218Sphk	set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
79877218Sphk}
79977218Sphk
800138593Ssamstatic void
80177218Sphkset80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
80277218Sphk{
80377218Sphk	int	mode;
80477218Sphk
80591454Sbrooks	if (strcasecmp(val, "off") == 0) {
80677218Sphk		mode = IEEE80211_WEP_OFF;
80791454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
80877218Sphk		mode = IEEE80211_WEP_ON;
80991454Sbrooks	} else if (strcasecmp(val, "mixed") == 0) {
81077218Sphk		mode = IEEE80211_WEP_MIXED;
81177218Sphk	} else {
812150708Sru		errx(1, "unknown wep mode");
81377218Sphk	}
81477218Sphk
81577218Sphk	set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
81677218Sphk}
81777218Sphk
818138593Ssamstatic void
81977218Sphkset80211wep(const char *val, int d, int s, const struct afswtch *rafp)
82077218Sphk{
82177218Sphk	set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
82277218Sphk}
82377218Sphk
824139493Ssamstatic int
825139493Ssamisundefarg(const char *arg)
826139493Ssam{
827139493Ssam	return (strcmp(arg, "-") == 0 || strncasecmp(arg, "undef", 5) == 0);
828139493Ssam}
829139493Ssam
830138593Ssamstatic void
83177218Sphkset80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
83277218Sphk{
833139493Ssam	if (isundefarg(val))
834139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, IEEE80211_KEYIX_NONE, 0, NULL);
835139493Ssam	else
836139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
83777218Sphk}
83877218Sphk
839138593Ssamstatic void
84077218Sphkset80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
84177218Sphk{
84277218Sphk	int		key = 0;
84377218Sphk	int		len;
844120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
84577218Sphk
846178354Ssam	if (isdigit((int)val[0]) && val[1] == ':') {
84777218Sphk		key = atoi(val)-1;
84877218Sphk		val += 2;
84977218Sphk	}
85077218Sphk
85177218Sphk	bzero(data, sizeof(data));
85277218Sphk	len = sizeof(data);
85377218Sphk	get_string(val, NULL, data, &len);
85477218Sphk
85577218Sphk	set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
85677218Sphk}
85777218Sphk
85877218Sphk/*
859148686Sstefanf * This function is purely a NetBSD compatability interface.  The NetBSD
860148686Sstefanf * interface is too inflexible, but it's there so we'll support it since
86177218Sphk * it's not all that hard.
86277218Sphk */
863138593Ssamstatic void
86477218Sphkset80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
86577218Sphk{
86677218Sphk	int		txkey;
86777218Sphk	int		i, len;
868120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
86977218Sphk
87077218Sphk	set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
87177218Sphk
872178354Ssam	if (isdigit((int)val[0]) && val[1] == ':') {
87377218Sphk		txkey = val[0]-'0'-1;
87477218Sphk		val += 2;
87577218Sphk
87691454Sbrooks		for (i = 0; i < 4; i++) {
87777218Sphk			bzero(data, sizeof(data));
87877218Sphk			len = sizeof(data);
87977218Sphk			val = get_string(val, ",", data, &len);
880151827Sbrooks			if (val == NULL)
881151827Sbrooks				exit(1);
88277218Sphk
88377218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
88477218Sphk		}
88577218Sphk	} else {
88677218Sphk		bzero(data, sizeof(data));
88777218Sphk		len = sizeof(data);
88877218Sphk		get_string(val, NULL, data, &len);
88977218Sphk		txkey = 0;
89077218Sphk
89177218Sphk		set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
89277218Sphk
89377218Sphk		bzero(data, sizeof(data));
89491454Sbrooks		for (i = 1; i < 4; i++)
89577218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
89677218Sphk	}
89777218Sphk
89877218Sphk	set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
89977218Sphk}
90077218Sphk
901138593Ssamstatic void
902127649Ssamset80211rtsthreshold(const char *val, int d, int s, const struct afswtch *rafp)
903127649Ssam{
904148416Ssam	set80211(s, IEEE80211_IOC_RTSTHRESHOLD,
905148416Ssam		isundefarg(val) ? IEEE80211_RTS_MAX : atoi(val), 0, NULL);
906127649Ssam}
907127649Ssam
908138593Ssamstatic void
909127649Ssamset80211protmode(const char *val, int d, int s, const struct afswtch *rafp)
910127649Ssam{
911127649Ssam	int	mode;
912127649Ssam
913127649Ssam	if (strcasecmp(val, "off") == 0) {
914127649Ssam		mode = IEEE80211_PROTMODE_OFF;
915127649Ssam	} else if (strcasecmp(val, "cts") == 0) {
916127649Ssam		mode = IEEE80211_PROTMODE_CTS;
917173275Ssam	} else if (strncasecmp(val, "rtscts", 3) == 0) {
918127649Ssam		mode = IEEE80211_PROTMODE_RTSCTS;
919127649Ssam	} else {
920150708Sru		errx(1, "unknown protection mode");
921127649Ssam	}
922127649Ssam
923127649Ssam	set80211(s, IEEE80211_IOC_PROTMODE, mode, 0, NULL);
924127649Ssam}
925127649Ssam
926138593Ssamstatic void
927173275Ssamset80211htprotmode(const char *val, int d, int s, const struct afswtch *rafp)
928173275Ssam{
929173275Ssam	int	mode;
930173275Ssam
931173275Ssam	if (strcasecmp(val, "off") == 0) {
932173275Ssam		mode = IEEE80211_PROTMODE_OFF;
933173275Ssam	} else if (strncasecmp(val, "rts", 3) == 0) {
934173275Ssam		mode = IEEE80211_PROTMODE_RTSCTS;
935173275Ssam	} else {
936173275Ssam		errx(1, "unknown protection mode");
937173275Ssam	}
938173275Ssam
939173275Ssam	set80211(s, IEEE80211_IOC_HTPROTMODE, mode, 0, NULL);
940173275Ssam}
941173275Ssam
942173275Ssamstatic void
943127649Ssamset80211txpower(const char *val, int d, int s, const struct afswtch *rafp)
944127649Ssam{
945173275Ssam	double v = atof(val);
946173275Ssam	int txpow;
947173275Ssam
948173275Ssam	txpow = (int) (2*v);
949173275Ssam	if (txpow != 2*v)
950173275Ssam		errx(-1, "invalid tx power (must be .5 dBm units)");
951173275Ssam	set80211(s, IEEE80211_IOC_TXPOWER, txpow, 0, NULL);
952127649Ssam}
953127649Ssam
954138593Ssam#define	IEEE80211_ROAMING_DEVICE	0
955138593Ssam#define	IEEE80211_ROAMING_AUTO		1
956138593Ssam#define	IEEE80211_ROAMING_MANUAL	2
957138593Ssam
958138593Ssamstatic void
959138593Ssamset80211roaming(const char *val, int d, int s, const struct afswtch *rafp)
96077218Sphk{
961138593Ssam	int mode;
96277218Sphk
963138593Ssam	if (strcasecmp(val, "device") == 0) {
964138593Ssam		mode = IEEE80211_ROAMING_DEVICE;
965138593Ssam	} else if (strcasecmp(val, "auto") == 0) {
966138593Ssam		mode = IEEE80211_ROAMING_AUTO;
967138593Ssam	} else if (strcasecmp(val, "manual") == 0) {
968138593Ssam		mode = IEEE80211_ROAMING_MANUAL;
969138593Ssam	} else {
970150708Sru		errx(1, "unknown roaming mode");
971138593Ssam	}
972138593Ssam	set80211(s, IEEE80211_IOC_ROAMING, mode, 0, NULL);
973138593Ssam}
974138593Ssam
975138593Ssamstatic void
976138593Ssamset80211wme(const char *val, int d, int s, const struct afswtch *rafp)
977138593Ssam{
978138593Ssam	set80211(s, IEEE80211_IOC_WME, d, 0, NULL);
979138593Ssam}
980138593Ssam
981138593Ssamstatic void
982138593Ssamset80211hidessid(const char *val, int d, int s, const struct afswtch *rafp)
983138593Ssam{
984138593Ssam	set80211(s, IEEE80211_IOC_HIDESSID, d, 0, NULL);
985138593Ssam}
986138593Ssam
987138593Ssamstatic void
988138593Ssamset80211apbridge(const char *val, int d, int s, const struct afswtch *rafp)
989138593Ssam{
990138593Ssam	set80211(s, IEEE80211_IOC_APBRIDGE, d, 0, NULL);
991138593Ssam}
992138593Ssam
993138593Ssamstatic void
994170531Ssamset80211fastframes(const char *val, int d, int s, const struct afswtch *rafp)
995170531Ssam{
996170531Ssam	set80211(s, IEEE80211_IOC_FF, d, 0, NULL);
997170531Ssam}
998170531Ssam
999170531Ssamstatic void
1000170531Ssamset80211dturbo(const char *val, int d, int s, const struct afswtch *rafp)
1001170531Ssam{
1002170531Ssam	set80211(s, IEEE80211_IOC_TURBOP, d, 0, NULL);
1003170531Ssam}
1004170531Ssam
1005170531Ssamstatic void
1006138593Ssamset80211chanlist(const char *val, int d, int s, const struct afswtch *rafp)
1007138593Ssam{
1008138593Ssam	struct ieee80211req_chanlist chanlist;
1009138593Ssam	char *temp, *cp, *tp;
1010138593Ssam
1011138593Ssam	temp = malloc(strlen(val) + 1);
1012138593Ssam	if (temp == NULL)
1013138593Ssam		errx(1, "malloc failed");
1014138593Ssam	strcpy(temp, val);
1015138593Ssam	memset(&chanlist, 0, sizeof(chanlist));
1016138593Ssam	cp = temp;
1017138593Ssam	for (;;) {
1018173275Ssam		int first, last, f, c;
1019138593Ssam
1020138593Ssam		tp = strchr(cp, ',');
1021138593Ssam		if (tp != NULL)
1022138593Ssam			*tp++ = '\0';
1023138593Ssam		switch (sscanf(cp, "%u-%u", &first, &last)) {
1024138593Ssam		case 1:
1025187801Ssam			if (first > IEEE80211_CHAN_MAX)
1026146873Sjhb				errx(-1, "channel %u out of range, max %zu",
1027187801Ssam					first, IEEE80211_CHAN_MAX);
1028138593Ssam			setbit(chanlist.ic_channels, first);
1029138593Ssam			break;
1030138593Ssam		case 2:
1031187801Ssam			if (first > IEEE80211_CHAN_MAX)
1032146873Sjhb				errx(-1, "channel %u out of range, max %zu",
1033187801Ssam					first, IEEE80211_CHAN_MAX);
1034187801Ssam			if (last > IEEE80211_CHAN_MAX)
1035146873Sjhb				errx(-1, "channel %u out of range, max %zu",
1036187801Ssam					last, IEEE80211_CHAN_MAX);
1037138593Ssam			if (first > last)
1038138593Ssam				errx(-1, "void channel range, %u > %u",
1039138593Ssam					first, last);
1040138593Ssam			for (f = first; f <= last; f++)
1041138593Ssam				setbit(chanlist.ic_channels, f);
1042138593Ssam			break;
1043138593Ssam		}
1044138593Ssam		if (tp == NULL)
1045138593Ssam			break;
1046173275Ssam		c = *tp;
1047173275Ssam		while (isspace(c))
1048138593Ssam			tp++;
1049173275Ssam		if (!isdigit(c))
1050138593Ssam			break;
1051138593Ssam		cp = tp;
1052138593Ssam	}
1053170531Ssam	set80211(s, IEEE80211_IOC_CHANLIST, 0, sizeof(chanlist), &chanlist);
1054138593Ssam}
1055138593Ssam
1056138593Ssamstatic void
1057138593Ssamset80211bssid(const char *val, int d, int s, const struct afswtch *rafp)
1058138593Ssam{
1059138593Ssam
1060138593Ssam	if (!isanyarg(val)) {
1061138593Ssam		char *temp;
1062138593Ssam		struct sockaddr_dl sdl;
1063138593Ssam
1064155702Ssam		temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1065138593Ssam		if (temp == NULL)
1066138593Ssam			errx(1, "malloc failed");
1067138593Ssam		temp[0] = ':';
1068138593Ssam		strcpy(temp + 1, val);
1069138593Ssam		sdl.sdl_len = sizeof(sdl);
1070138593Ssam		link_addr(temp, &sdl);
1071138593Ssam		free(temp);
1072138593Ssam		if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1073138593Ssam			errx(1, "malformed link-level address");
1074138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
1075138593Ssam			IEEE80211_ADDR_LEN, LLADDR(&sdl));
1076138593Ssam	} else {
1077138593Ssam		uint8_t zerobssid[IEEE80211_ADDR_LEN];
1078138593Ssam		memset(zerobssid, 0, sizeof(zerobssid));
1079138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
1080138593Ssam			IEEE80211_ADDR_LEN, zerobssid);
1081138593Ssam	}
1082138593Ssam}
1083138593Ssam
1084138593Ssamstatic int
1085138593Ssamgetac(const char *ac)
1086138593Ssam{
1087138593Ssam	if (strcasecmp(ac, "ac_be") == 0 || strcasecmp(ac, "be") == 0)
1088138593Ssam		return WME_AC_BE;
1089138593Ssam	if (strcasecmp(ac, "ac_bk") == 0 || strcasecmp(ac, "bk") == 0)
1090138593Ssam		return WME_AC_BK;
1091138593Ssam	if (strcasecmp(ac, "ac_vi") == 0 || strcasecmp(ac, "vi") == 0)
1092138593Ssam		return WME_AC_VI;
1093138593Ssam	if (strcasecmp(ac, "ac_vo") == 0 || strcasecmp(ac, "vo") == 0)
1094138593Ssam		return WME_AC_VO;
1095138593Ssam	errx(1, "unknown wme access class %s", ac);
1096138593Ssam}
1097138593Ssam
1098138593Ssamstatic
1099138593SsamDECL_CMD_FUNC2(set80211cwmin, ac, val)
1100138593Ssam{
1101138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val), getac(ac), NULL);
1102138593Ssam}
1103138593Ssam
1104138593Ssamstatic
1105138593SsamDECL_CMD_FUNC2(set80211cwmax, ac, val)
1106138593Ssam{
1107138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val), getac(ac), NULL);
1108138593Ssam}
1109138593Ssam
1110138593Ssamstatic
1111138593SsamDECL_CMD_FUNC2(set80211aifs, ac, val)
1112138593Ssam{
1113138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val), getac(ac), NULL);
1114138593Ssam}
1115138593Ssam
1116138593Ssamstatic
1117138593SsamDECL_CMD_FUNC2(set80211txoplimit, ac, val)
1118138593Ssam{
1119138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), getac(ac), NULL);
1120138593Ssam}
1121138593Ssam
1122138593Ssamstatic
1123148621SsamDECL_CMD_FUNC(set80211acm, ac, d)
1124138593Ssam{
1125148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 1, getac(ac), NULL);
1126138593Ssam}
1127148621Ssamstatic
1128148621SsamDECL_CMD_FUNC(set80211noacm, ac, d)
1129148621Ssam{
1130148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 0, getac(ac), NULL);
1131148621Ssam}
1132138593Ssam
1133138593Ssamstatic
1134148621SsamDECL_CMD_FUNC(set80211ackpolicy, ac, d)
1135138593Ssam{
1136148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 1, getac(ac), NULL);
1137138593Ssam}
1138148621Ssamstatic
1139148621SsamDECL_CMD_FUNC(set80211noackpolicy, ac, d)
1140148621Ssam{
1141148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 0, getac(ac), NULL);
1142148621Ssam}
1143138593Ssam
1144138593Ssamstatic
1145138593SsamDECL_CMD_FUNC2(set80211bsscwmin, ac, val)
1146138593Ssam{
1147138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val),
1148138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1149138593Ssam}
1150138593Ssam
1151138593Ssamstatic
1152138593SsamDECL_CMD_FUNC2(set80211bsscwmax, ac, val)
1153138593Ssam{
1154138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val),
1155138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1156138593Ssam}
1157138593Ssam
1158138593Ssamstatic
1159138593SsamDECL_CMD_FUNC2(set80211bssaifs, ac, val)
1160138593Ssam{
1161138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val),
1162138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1163138593Ssam}
1164138593Ssam
1165138593Ssamstatic
1166138593SsamDECL_CMD_FUNC2(set80211bsstxoplimit, ac, val)
1167138593Ssam{
1168138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val),
1169138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1170138593Ssam}
1171138593Ssam
1172138593Ssamstatic
1173138593SsamDECL_CMD_FUNC(set80211dtimperiod, val, d)
1174138593Ssam{
1175138593Ssam	set80211(s, IEEE80211_IOC_DTIM_PERIOD, atoi(val), 0, NULL);
1176138593Ssam}
1177138593Ssam
1178138593Ssamstatic
1179138593SsamDECL_CMD_FUNC(set80211bintval, val, d)
1180138593Ssam{
1181138593Ssam	set80211(s, IEEE80211_IOC_BEACON_INTERVAL, atoi(val), 0, NULL);
1182138593Ssam}
1183138593Ssam
1184138593Ssamstatic void
1185138593Ssamset80211macmac(int s, int op, const char *val)
1186138593Ssam{
1187138593Ssam	char *temp;
1188138593Ssam	struct sockaddr_dl sdl;
1189138593Ssam
1190155702Ssam	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1191138593Ssam	if (temp == NULL)
1192138593Ssam		errx(1, "malloc failed");
1193138593Ssam	temp[0] = ':';
1194138593Ssam	strcpy(temp + 1, val);
1195138593Ssam	sdl.sdl_len = sizeof(sdl);
1196138593Ssam	link_addr(temp, &sdl);
1197138593Ssam	free(temp);
1198138593Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1199138593Ssam		errx(1, "malformed link-level address");
1200138593Ssam	set80211(s, op, 0, IEEE80211_ADDR_LEN, LLADDR(&sdl));
1201138593Ssam}
1202138593Ssam
1203138593Ssamstatic
1204138593SsamDECL_CMD_FUNC(set80211addmac, val, d)
1205138593Ssam{
1206138593Ssam	set80211macmac(s, IEEE80211_IOC_ADDMAC, val);
1207138593Ssam}
1208138593Ssam
1209138593Ssamstatic
1210138593SsamDECL_CMD_FUNC(set80211delmac, val, d)
1211138593Ssam{
1212138593Ssam	set80211macmac(s, IEEE80211_IOC_DELMAC, val);
1213138593Ssam}
1214138593Ssam
1215138593Ssamstatic
1216149029SsamDECL_CMD_FUNC(set80211kickmac, val, d)
1217149029Ssam{
1218149029Ssam	char *temp;
1219149029Ssam	struct sockaddr_dl sdl;
1220149029Ssam	struct ieee80211req_mlme mlme;
1221149029Ssam
1222155702Ssam	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1223149029Ssam	if (temp == NULL)
1224149029Ssam		errx(1, "malloc failed");
1225149029Ssam	temp[0] = ':';
1226149029Ssam	strcpy(temp + 1, val);
1227149029Ssam	sdl.sdl_len = sizeof(sdl);
1228149029Ssam	link_addr(temp, &sdl);
1229149029Ssam	free(temp);
1230149029Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1231149029Ssam		errx(1, "malformed link-level address");
1232149029Ssam	memset(&mlme, 0, sizeof(mlme));
1233149029Ssam	mlme.im_op = IEEE80211_MLME_DEAUTH;
1234149029Ssam	mlme.im_reason = IEEE80211_REASON_AUTH_EXPIRE;
1235149029Ssam	memcpy(mlme.im_macaddr, LLADDR(&sdl), IEEE80211_ADDR_LEN);
1236170531Ssam	set80211(s, IEEE80211_IOC_MLME, 0, sizeof(mlme), &mlme);
1237149029Ssam}
1238149029Ssam
1239149029Ssamstatic
1240138593SsamDECL_CMD_FUNC(set80211maccmd, val, d)
1241138593Ssam{
1242138593Ssam	set80211(s, IEEE80211_IOC_MACCMD, d, 0, NULL);
1243138593Ssam}
1244138593Ssam
1245147795Ssamstatic void
1246147795Ssamset80211pureg(const char *val, int d, int s, const struct afswtch *rafp)
1247147795Ssam{
1248147795Ssam	set80211(s, IEEE80211_IOC_PUREG, d, 0, NULL);
1249147795Ssam}
1250147795Ssam
1251153422Ssamstatic void
1252170531Ssamset80211bgscan(const char *val, int d, int s, const struct afswtch *rafp)
1253153422Ssam{
1254170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN, d, 0, NULL);
1255153422Ssam}
1256153422Ssam
1257148416Ssamstatic
1258170531SsamDECL_CMD_FUNC(set80211bgscanidle, val, d)
1259170531Ssam{
1260170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN_IDLE, atoi(val), 0, NULL);
1261170531Ssam}
1262170531Ssam
1263170531Ssamstatic
1264170531SsamDECL_CMD_FUNC(set80211bgscanintvl, val, d)
1265170531Ssam{
1266170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN_INTERVAL, atoi(val), 0, NULL);
1267170531Ssam}
1268170531Ssam
1269170531Ssamstatic
1270170531SsamDECL_CMD_FUNC(set80211scanvalid, val, d)
1271170531Ssam{
1272170531Ssam	set80211(s, IEEE80211_IOC_SCANVALID, atoi(val), 0, NULL);
1273170531Ssam}
1274170531Ssam
1275178354Ssam/*
1276178354Ssam * Parse an optional trailing specification of which netbands
1277178354Ssam * to apply a parameter to.  This is basically the same syntax
1278178354Ssam * as used for channels but you can concatenate to specify
1279178354Ssam * multiple.  For example:
1280178354Ssam *	14:abg		apply to 11a, 11b, and 11g
1281178354Ssam *	6:ht		apply to 11na and 11ng
1282178354Ssam * We don't make a big effort to catch silly things; this is
1283178354Ssam * really a convenience mechanism.
1284178354Ssam */
1285178354Ssamstatic int
1286178354Ssamgetmodeflags(const char *val)
1287170531Ssam{
1288178354Ssam	const char *cp;
1289178354Ssam	int flags;
1290178354Ssam
1291178354Ssam	flags = 0;
1292178354Ssam
1293178354Ssam	cp = strchr(val, ':');
1294178354Ssam	if (cp != NULL) {
1295178354Ssam		for (cp++; isalpha((int) *cp); cp++) {
1296178354Ssam			/* accept mixed case */
1297178354Ssam			int c = *cp;
1298178354Ssam			if (isupper(c))
1299178354Ssam				c = tolower(c);
1300178354Ssam			switch (c) {
1301178354Ssam			case 'a':		/* 802.11a */
1302178354Ssam				flags |= IEEE80211_CHAN_A;
1303178354Ssam				break;
1304178354Ssam			case 'b':		/* 802.11b */
1305178354Ssam				flags |= IEEE80211_CHAN_B;
1306178354Ssam				break;
1307178354Ssam			case 'g':		/* 802.11g */
1308178354Ssam				flags |= IEEE80211_CHAN_G;
1309178354Ssam				break;
1310178354Ssam			case 'h':		/* ht = 802.11n */
1311178354Ssam			case 'n':		/* 802.11n */
1312178354Ssam				flags |= IEEE80211_CHAN_HT;
1313178354Ssam				break;
1314178354Ssam			case 'd':		/* dt = Atheros Dynamic Turbo */
1315178354Ssam				flags |= IEEE80211_CHAN_TURBO;
1316178354Ssam				break;
1317178354Ssam			case 't':		/* ht, dt, st, t */
1318178354Ssam				/* dt and unadorned t specify Dynamic Turbo */
1319178354Ssam				if ((flags & (IEEE80211_CHAN_STURBO|IEEE80211_CHAN_HT)) == 0)
1320178354Ssam					flags |= IEEE80211_CHAN_TURBO;
1321178354Ssam				break;
1322178354Ssam			case 's':		/* st = Atheros Static Turbo */
1323178354Ssam				flags |= IEEE80211_CHAN_STURBO;
1324178354Ssam				break;
1325178354Ssam			default:
1326178354Ssam				errx(-1, "%s: Invalid mode attribute %c\n",
1327178354Ssam				    val, *cp);
1328178354Ssam			}
1329178354Ssam		}
1330178354Ssam	}
1331178354Ssam	return flags;
1332170531Ssam}
1333170531Ssam
1334178354Ssam#define	IEEE80211_CHAN_HTA	(IEEE80211_CHAN_HT|IEEE80211_CHAN_5GHZ)
1335178354Ssam#define	IEEE80211_CHAN_HTG	(IEEE80211_CHAN_HT|IEEE80211_CHAN_2GHZ)
1336178354Ssam
1337178354Ssam#define	_APPLY(_flags, _base, _param, _v) do {				\
1338178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1339178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1340178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1341178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1342178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1343178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1344178354Ssam	    else							\
1345178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1346178354Ssam    }									\
1347178354Ssam    if (_flags & IEEE80211_CHAN_TURBO) {				\
1348178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1349178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;	\
1350178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;	\
1351178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1352178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;	\
1353178354Ssam	    else							\
1354178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;	\
1355178354Ssam    }									\
1356178354Ssam    if (_flags & IEEE80211_CHAN_STURBO)					\
1357178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = _v;		\
1358178354Ssam    if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1359178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = _v;		\
1360178354Ssam    if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1361178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = _v;		\
1362178354Ssam    if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1363178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = _v;		\
1364178354Ssam} while (0)
1365178354Ssam#define	_APPLY1(_flags, _base, _param, _v) do {				\
1366178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1367178354Ssam	    if (_flags & IEEE80211_CHAN_5GHZ)				\
1368178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1369178354Ssam	    else							\
1370178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1371178354Ssam    } else if ((_flags & IEEE80211_CHAN_108A) == IEEE80211_CHAN_108A)	\
1372178354Ssam	    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;		\
1373178354Ssam    else if ((_flags & IEEE80211_CHAN_108G) == IEEE80211_CHAN_108G)	\
1374178354Ssam	    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;		\
1375178354Ssam    else if ((_flags & IEEE80211_CHAN_ST) == IEEE80211_CHAN_ST)		\
1376178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = _v;		\
1377178354Ssam    else if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1378178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = _v;		\
1379178354Ssam    else if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1380178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = _v;		\
1381178354Ssam    else if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1382178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = _v;		\
1383178354Ssam} while (0)
1384178354Ssam#define	_APPLY_RATE(_flags, _base, _param, _v) do {			\
1385178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1386178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1387178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v|0x80;	\
1388178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v|0x80;	\
1389178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1390178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v|0x80;	\
1391178354Ssam	    else							\
1392178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v|0x80;	\
1393178354Ssam    }									\
1394178354Ssam    if (_flags & IEEE80211_CHAN_TURBO) {				\
1395178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1396178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = 2*_v;	\
1397178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = 2*_v;	\
1398178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1399178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = 2*_v;	\
1400178354Ssam	    else							\
1401178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = 2*_v;	\
1402178354Ssam    }									\
1403178354Ssam    if (_flags & IEEE80211_CHAN_STURBO)					\
1404178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = 2*_v;	\
1405178354Ssam    if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1406178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = 2*_v;		\
1407178354Ssam    if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1408178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = (_v == 5 ? 11 : 2*_v);\
1409178354Ssam    if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1410178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = (_v == 5 ? 11 : 2*_v);\
1411178354Ssam} while (0)
1412178354Ssam#define	_APPLY_RATE1(_flags, _base, _param, _v) do {			\
1413178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1414178354Ssam	    if (_flags & IEEE80211_CHAN_5GHZ)				\
1415178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v|0x80;	\
1416178354Ssam	    else							\
1417178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v|0x80;	\
1418178354Ssam    } else if ((_flags & IEEE80211_CHAN_108A) == IEEE80211_CHAN_108A)	\
1419178354Ssam	    _base.params[IEEE80211_MODE_TURBO_A]._param = 2*_v;		\
1420178354Ssam    else if ((_flags & IEEE80211_CHAN_108G) == IEEE80211_CHAN_108G)	\
1421178354Ssam	    _base.params[IEEE80211_MODE_TURBO_G]._param = 2*_v;		\
1422178354Ssam    else if ((_flags & IEEE80211_CHAN_ST) == IEEE80211_CHAN_ST)		\
1423178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = 2*_v;	\
1424178354Ssam    else if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1425178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = 2*_v;		\
1426178354Ssam    else if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1427178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = (_v == 5 ? 11 : 2*_v);\
1428178354Ssam    else if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1429178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = (_v == 5 ? 11 : 2*_v);\
1430178354Ssam} while (0)
1431178354Ssam
1432170531Ssamstatic
1433178354SsamDECL_CMD_FUNC(set80211roamrssi, val, d)
1434170531Ssam{
1435178354Ssam	double v = atof(val);
1436178354Ssam	int rssi, flags;
1437178354Ssam
1438178354Ssam	rssi = (int) (2*v);
1439178354Ssam	if (rssi != 2*v)
1440178354Ssam		errx(-1, "invalid rssi (must be .5 dBm units)");
1441178354Ssam	flags = getmodeflags(val);
1442178354Ssam	getroam(s);
1443178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1444178354Ssam		flags = getcurchan(s)->ic_flags;
1445178354Ssam		_APPLY1(flags, roamparams, rssi, rssi);
1446178354Ssam	} else
1447178354Ssam		_APPLY(flags, roamparams, rssi, rssi);
1448178354Ssam	callback_register(setroam_cb, &roamparams);
1449170531Ssam}
1450170531Ssam
1451170531Ssamstatic
1452178354SsamDECL_CMD_FUNC(set80211roamrate, val, d)
1453170531Ssam{
1454178354Ssam	int v = atoi(val), flags;
1455178354Ssam
1456178354Ssam	flags = getmodeflags(val);
1457178354Ssam	getroam(s);
1458178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1459178354Ssam		flags = getcurchan(s)->ic_flags;
1460178354Ssam		_APPLY_RATE1(flags, roamparams, rate, v);
1461178354Ssam	} else
1462178354Ssam		_APPLY_RATE(flags, roamparams, rate, v);
1463178354Ssam	callback_register(setroam_cb, &roamparams);
1464170531Ssam}
1465170531Ssam
1466170531Ssamstatic
1467178354SsamDECL_CMD_FUNC(set80211mcastrate, val, d)
1468170531Ssam{
1469178354Ssam	int v = atoi(val), flags;
1470178354Ssam
1471178354Ssam	flags = getmodeflags(val);
1472178354Ssam	gettxparams(s);
1473178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1474178354Ssam		flags = getcurchan(s)->ic_flags;
1475178354Ssam		_APPLY_RATE1(flags, txparams, mcastrate, v);
1476178354Ssam	} else
1477178354Ssam		_APPLY_RATE(flags, txparams, mcastrate, v);
1478178354Ssam	callback_register(settxparams_cb, &txparams);
1479170531Ssam}
1480170531Ssam
1481170531Ssamstatic
1482178354SsamDECL_CMD_FUNC(set80211mgtrate, val, d)
1483170531Ssam{
1484178354Ssam	int v = atoi(val), flags;
1485178354Ssam
1486178354Ssam	flags = getmodeflags(val);
1487178354Ssam	gettxparams(s);
1488178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1489178354Ssam		flags = getcurchan(s)->ic_flags;
1490178354Ssam		_APPLY_RATE1(flags, txparams, mgmtrate, v);
1491178354Ssam	} else
1492178354Ssam		_APPLY_RATE(flags, txparams, mgmtrate, v);
1493178354Ssam	callback_register(settxparams_cb, &txparams);
1494170531Ssam}
1495170531Ssam
1496170531Ssamstatic
1497178354SsamDECL_CMD_FUNC(set80211ucastrate, val, d)
1498170531Ssam{
1499178354Ssam	int v, flags;
1500178354Ssam
1501178354Ssam	gettxparams(s);
1502178354Ssam	flags = getmodeflags(val);
1503178354Ssam	if (isanyarg(val)) {
1504178354Ssam		if (flags == 0) {	/* NB: no flags => current channel */
1505178354Ssam			flags = getcurchan(s)->ic_flags;
1506178354Ssam			_APPLY1(flags, txparams, ucastrate,
1507178354Ssam			    IEEE80211_FIXED_RATE_NONE);
1508178354Ssam		} else
1509178354Ssam			_APPLY(flags, txparams, ucastrate,
1510178354Ssam			    IEEE80211_FIXED_RATE_NONE);
1511178354Ssam	} else {
1512178354Ssam		v = atoi(val);
1513178354Ssam		if (flags == 0) {	/* NB: no flags => current channel */
1514178354Ssam			flags = getcurchan(s)->ic_flags;
1515178354Ssam			_APPLY_RATE1(flags, txparams, ucastrate, v);
1516178354Ssam		} else
1517178354Ssam			_APPLY_RATE(flags, txparams, ucastrate, v);
1518178354Ssam	}
1519178354Ssam	callback_register(settxparams_cb, &txparams);
1520170531Ssam}
1521170531Ssam
1522170531Ssamstatic
1523178354SsamDECL_CMD_FUNC(set80211maxretry, val, d)
1524153354Ssam{
1525178354Ssam	int v = atoi(val), flags;
1526178354Ssam
1527178354Ssam	flags = getmodeflags(val);
1528178354Ssam	gettxparams(s);
1529178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1530178354Ssam		flags = getcurchan(s)->ic_flags;
1531178354Ssam		_APPLY1(flags, txparams, maxretry, v);
1532178354Ssam	} else
1533178354Ssam		_APPLY(flags, txparams, maxretry, v);
1534178354Ssam	callback_register(settxparams_cb, &txparams);
1535153354Ssam}
1536178354Ssam#undef _APPLY_RATE
1537178354Ssam#undef _APPLY
1538178354Ssam#undef IEEE80211_CHAN_HTA
1539178354Ssam#undef IEEE80211_CHAN_HTG
1540153354Ssam
1541153354Ssamstatic
1542148416SsamDECL_CMD_FUNC(set80211fragthreshold, val, d)
1543148416Ssam{
1544148416Ssam	set80211(s, IEEE80211_IOC_FRAGTHRESHOLD,
1545148416Ssam		isundefarg(val) ? IEEE80211_FRAG_MAX : atoi(val), 0, NULL);
1546148416Ssam}
1547148416Ssam
1548160687Ssamstatic
1549160687SsamDECL_CMD_FUNC(set80211bmissthreshold, val, d)
1550160687Ssam{
1551160687Ssam	set80211(s, IEEE80211_IOC_BMISSTHRESHOLD,
1552160687Ssam		isundefarg(val) ? IEEE80211_HWBMISS_MAX : atoi(val), 0, NULL);
1553160687Ssam}
1554160687Ssam
1555170531Ssamstatic void
1556170531Ssamset80211burst(const char *val, int d, int s, const struct afswtch *rafp)
1557170531Ssam{
1558170531Ssam	set80211(s, IEEE80211_IOC_BURST, d, 0, NULL);
1559170531Ssam}
1560170531Ssam
1561170531Ssamstatic void
1562170531Ssamset80211doth(const char *val, int d, int s, const struct afswtch *rafp)
1563170531Ssam{
1564170531Ssam	set80211(s, IEEE80211_IOC_DOTH, d, 0, NULL);
1565170531Ssam}
1566170531Ssam
1567173275Ssamstatic void
1568178354Ssamset80211dfs(const char *val, int d, int s, const struct afswtch *rafp)
1569178354Ssam{
1570178354Ssam	set80211(s, IEEE80211_IOC_DFS, d, 0, NULL);
1571178354Ssam}
1572178354Ssam
1573178354Ssamstatic void
1574173275Ssamset80211shortgi(const char *val, int d, int s, const struct afswtch *rafp)
1575173275Ssam{
1576173275Ssam	set80211(s, IEEE80211_IOC_SHORTGI,
1577173275Ssam		d ? (IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40) : 0,
1578173275Ssam		0, NULL);
1579173275Ssam}
1580173275Ssam
1581173275Ssamstatic void
1582173275Ssamset80211ampdu(const char *val, int d, int s, const struct afswtch *rafp)
1583173275Ssam{
1584173275Ssam	int ampdu;
1585173275Ssam
1586173275Ssam	if (get80211val(s, IEEE80211_IOC_AMPDU, &ampdu) < 0)
1587173275Ssam		errx(-1, "cannot get AMPDU setting");
1588173275Ssam	if (d < 0) {
1589173275Ssam		d = -d;
1590173275Ssam		ampdu &= ~d;
1591173275Ssam	} else
1592173275Ssam		ampdu |= d;
1593173275Ssam	set80211(s, IEEE80211_IOC_AMPDU, ampdu, 0, NULL);
1594173275Ssam}
1595173275Ssam
1596173275Ssamstatic
1597173275SsamDECL_CMD_FUNC(set80211ampdulimit, val, d)
1598173275Ssam{
1599173275Ssam	int v;
1600173275Ssam
1601173275Ssam	switch (atoi(val)) {
1602173275Ssam	case 8:
1603173275Ssam	case 8*1024:
1604173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_8K;
1605173275Ssam		break;
1606173275Ssam	case 16:
1607173275Ssam	case 16*1024:
1608173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_16K;
1609173275Ssam		break;
1610173275Ssam	case 32:
1611173275Ssam	case 32*1024:
1612173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_32K;
1613173275Ssam		break;
1614173275Ssam	case 64:
1615173275Ssam	case 64*1024:
1616173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_64K;
1617173275Ssam		break;
1618173275Ssam	default:
1619173275Ssam		errx(-1, "invalid A-MPDU limit %s", val);
1620173275Ssam	}
1621173275Ssam	set80211(s, IEEE80211_IOC_AMPDU_LIMIT, v, 0, NULL);
1622173275Ssam}
1623173275Ssam
1624173275Ssamstatic
1625173275SsamDECL_CMD_FUNC(set80211ampdudensity, val, d)
1626173275Ssam{
1627173275Ssam	int v;
1628173275Ssam
1629183260Ssam	if (isanyarg(val) || strcasecmp(val, "na") == 0)
1630173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1631173275Ssam	else switch ((int)(atof(val)*4)) {
1632173275Ssam	case 0:
1633173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1634173275Ssam		break;
1635173275Ssam	case 1:
1636173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_025;
1637173275Ssam		break;
1638173275Ssam	case 2:
1639173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_05;
1640173275Ssam		break;
1641173275Ssam	case 4:
1642173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_1;
1643173275Ssam		break;
1644173275Ssam	case 8:
1645173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_2;
1646173275Ssam		break;
1647173275Ssam	case 16:
1648173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_4;
1649173275Ssam		break;
1650173275Ssam	case 32:
1651173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_8;
1652173275Ssam		break;
1653173275Ssam	case 64:
1654173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_16;
1655173275Ssam		break;
1656173275Ssam	default:
1657173275Ssam		errx(-1, "invalid A-MPDU density %s", val);
1658173275Ssam	}
1659173275Ssam	set80211(s, IEEE80211_IOC_AMPDU_DENSITY, v, 0, NULL);
1660173275Ssam}
1661173275Ssam
1662173275Ssamstatic void
1663173275Ssamset80211amsdu(const char *val, int d, int s, const struct afswtch *rafp)
1664173275Ssam{
1665173275Ssam	int amsdu;
1666173275Ssam
1667173275Ssam	if (get80211val(s, IEEE80211_IOC_AMSDU, &amsdu) < 0)
1668187801Ssam		err(-1, "cannot get AMSDU setting");
1669173275Ssam	if (d < 0) {
1670173275Ssam		d = -d;
1671173275Ssam		amsdu &= ~d;
1672173275Ssam	} else
1673173275Ssam		amsdu |= d;
1674173275Ssam	set80211(s, IEEE80211_IOC_AMSDU, amsdu, 0, NULL);
1675173275Ssam}
1676173275Ssam
1677173275Ssamstatic
1678173275SsamDECL_CMD_FUNC(set80211amsdulimit, val, d)
1679173275Ssam{
1680173275Ssam	set80211(s, IEEE80211_IOC_AMSDU_LIMIT, atoi(val), 0, NULL);
1681173275Ssam}
1682173275Ssam
1683173275Ssamstatic void
1684173275Ssamset80211puren(const char *val, int d, int s, const struct afswtch *rafp)
1685173275Ssam{
1686173275Ssam	set80211(s, IEEE80211_IOC_PUREN, d, 0, NULL);
1687173275Ssam}
1688173275Ssam
1689173275Ssamstatic void
1690173275Ssamset80211htcompat(const char *val, int d, int s, const struct afswtch *rafp)
1691173275Ssam{
1692173275Ssam	set80211(s, IEEE80211_IOC_HTCOMPAT, d, 0, NULL);
1693173275Ssam}
1694173275Ssam
1695173275Ssamstatic void
1696173275Ssamset80211htconf(const char *val, int d, int s, const struct afswtch *rafp)
1697173275Ssam{
1698173275Ssam	set80211(s, IEEE80211_IOC_HTCONF, d, 0, NULL);
1699173275Ssam	htconf = d;
1700173275Ssam}
1701173275Ssam
1702173275Ssamstatic void
1703178354Ssamset80211dwds(const char *val, int d, int s, const struct afswtch *rafp)
1704178354Ssam{
1705178354Ssam	set80211(s, IEEE80211_IOC_DWDS, d, 0, NULL);
1706178354Ssam}
1707178354Ssam
1708178354Ssamstatic void
1709173275Ssamset80211inact(const char *val, int d, int s, const struct afswtch *rafp)
1710173275Ssam{
1711173275Ssam	set80211(s, IEEE80211_IOC_INACTIVITY, d, 0, NULL);
1712173275Ssam}
1713173275Ssam
1714173275Ssamstatic void
1715178354Ssamset80211tsn(const char *val, int d, int s, const struct afswtch *rafp)
1716178354Ssam{
1717178354Ssam	set80211(s, IEEE80211_IOC_TSN, d, 0, NULL);
1718178354Ssam}
1719178354Ssam
1720178354Ssamstatic void
1721178354Ssamset80211dotd(const char *val, int d, int s, const struct afswtch *rafp)
1722178354Ssam{
1723178354Ssam	set80211(s, IEEE80211_IOC_DOTD, d, 0, NULL);
1724178354Ssam}
1725178354Ssam
1726183261Ssamstatic void
1727183261Ssamset80211smps(const char *val, int d, int s, const struct afswtch *rafp)
1728183261Ssam{
1729183261Ssam	set80211(s, IEEE80211_IOC_SMPS, d, 0, NULL);
1730183261Ssam}
1731183261Ssam
1732183261Ssamstatic void
1733183261Ssamset80211rifs(const char *val, int d, int s, const struct afswtch *rafp)
1734183261Ssam{
1735183261Ssam	set80211(s, IEEE80211_IOC_RIFS, d, 0, NULL);
1736183261Ssam}
1737183261Ssam
1738186904Ssamstatic
1739186904SsamDECL_CMD_FUNC(set80211tdmaslot, val, d)
1740186904Ssam{
1741186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOT, atoi(val), 0, NULL);
1742186904Ssam}
1743186904Ssam
1744186904Ssamstatic
1745186904SsamDECL_CMD_FUNC(set80211tdmaslotcnt, val, d)
1746186904Ssam{
1747186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOTCNT, atoi(val), 0, NULL);
1748186904Ssam}
1749186904Ssam
1750186904Ssamstatic
1751186904SsamDECL_CMD_FUNC(set80211tdmaslotlen, val, d)
1752186904Ssam{
1753186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOTLEN, atoi(val), 0, NULL);
1754186904Ssam}
1755186904Ssam
1756186904Ssamstatic
1757186904SsamDECL_CMD_FUNC(set80211tdmabintval, val, d)
1758186904Ssam{
1759186904Ssam	set80211(s, IEEE80211_IOC_TDMA_BINTERVAL, atoi(val), 0, NULL);
1760186904Ssam}
1761186904Ssam
1762178354Ssamstatic int
1763178354Ssamregdomain_sort(const void *a, const void *b)
1764178354Ssam{
1765178354Ssam#define	CHAN_ALL \
1766178354Ssam	(IEEE80211_CHAN_ALLTURBO|IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)
1767178354Ssam	const struct ieee80211_channel *ca = a;
1768178354Ssam	const struct ieee80211_channel *cb = b;
1769178354Ssam
1770178354Ssam	return ca->ic_freq == cb->ic_freq ?
1771178354Ssam	    (ca->ic_flags & CHAN_ALL) - (cb->ic_flags & CHAN_ALL) :
1772178354Ssam	    ca->ic_freq - cb->ic_freq;
1773178354Ssam#undef CHAN_ALL
1774178354Ssam}
1775178354Ssam
1776178354Ssamstatic const struct ieee80211_channel *
1777178354Ssamchanlookup(const struct ieee80211_channel chans[], int nchans,
1778178354Ssam	int freq, int flags)
1779178354Ssam{
1780178354Ssam	int i;
1781178354Ssam
1782178354Ssam	flags &= IEEE80211_CHAN_ALLTURBO;
1783178354Ssam	for (i = 0; i < nchans; i++) {
1784178354Ssam		const struct ieee80211_channel *c = &chans[i];
1785178354Ssam		if (c->ic_freq == freq &&
1786178354Ssam		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1787178354Ssam			return c;
1788178354Ssam	}
1789178354Ssam	return NULL;
1790178354Ssam}
1791178354Ssam
1792187841Ssamstatic int
1793187841Ssamchanfind(const struct ieee80211_channel chans[], int nchans, int flags)
1794187841Ssam{
1795187841Ssam	int i;
1796187841Ssam
1797187841Ssam	for (i = 0; i < nchans; i++) {
1798187841Ssam		const struct ieee80211_channel *c = &chans[i];
1799187841Ssam		if ((c->ic_flags & flags) == flags)
1800187841Ssam			return 1;
1801187841Ssam	}
1802187841Ssam	return 0;
1803187841Ssam}
1804187841Ssam
1805178354Ssamstatic void
1806178354Ssamregdomain_addchans(struct ieee80211req_chaninfo *ci,
1807178354Ssam	const netband_head *bands,
1808178354Ssam	const struct ieee80211_regdomain *reg,
1809178354Ssam	uint32_t chanFlags,
1810178354Ssam	const struct ieee80211req_chaninfo *avail)
1811178354Ssam{
1812178354Ssam	const struct netband *nb;
1813178354Ssam	const struct freqband *b;
1814178354Ssam	struct ieee80211_channel *c, *prev;
1815187841Ssam	int freq, channelSep, hasHalfChans, hasQuarterChans;
1816178354Ssam
1817178354Ssam	channelSep = (chanFlags & IEEE80211_CHAN_2GHZ) ? 0 : 40;
1818187841Ssam	hasHalfChans = chanfind(avail->ic_chans, avail->ic_nchans,
1819187841Ssam	    IEEE80211_CHAN_HALF |
1820187841Ssam	       (chanFlags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ)));
1821187841Ssam	hasQuarterChans = chanfind(avail->ic_chans, avail->ic_nchans,
1822187841Ssam	    IEEE80211_CHAN_QUARTER |
1823187841Ssam	        (chanFlags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ)));
1824178354Ssam	LIST_FOREACH(nb, bands, next) {
1825178354Ssam		b = nb->band;
1826178354Ssam		if (verbose)
1827178354Ssam			printf("%s: chanFlags 0x%x b %p\n",
1828178354Ssam			    __func__, chanFlags, b);
1829178354Ssam		prev = NULL;
1830178354Ssam		for (freq = b->freqStart; freq <= b->freqEnd; freq += b->chanSep) {
1831178354Ssam			uint32_t flags = nb->flags | b->flags;
1832178354Ssam
1833178354Ssam			/* check if device can operate on this frequency */
1834187841Ssam			/*
1835187841Ssam			 * XXX GSM frequency mapping is handled in the kernel
1836187841Ssam			 * so we cannot find them in the calibration table;
1837187841Ssam			 * just construct the list and the kernel will reject
1838187841Ssam			 * if it's wrong.
1839187841Ssam			 */
1840187841Ssam			if (chanlookup(avail->ic_chans, avail->ic_nchans, freq, chanFlags) == NULL &&
1841187841Ssam			    (flags & IEEE80211_CHAN_GSM) == 0) {
1842178354Ssam				if (verbose)
1843178354Ssam					printf("%u: skip, flags 0x%x not available\n", freq, chanFlags);
1844178354Ssam				continue;
1845178354Ssam			}
1846187841Ssam			if ((flags & IEEE80211_CHAN_HALF) && !hasHalfChans) {
1847178354Ssam				if (verbose)
1848187841Ssam					printf("%u: skip, device does not "
1849187841Ssam					    "support half-rate channel\n",
1850187841Ssam					    freq);
1851178354Ssam				continue;
1852178354Ssam			}
1853178354Ssam			if ((flags & IEEE80211_CHAN_QUARTER) &&
1854187841Ssam			    !hasQuarterChans) {
1855178354Ssam				if (verbose)
1856187841Ssam					printf("%u: skip, device does not "
1857187841Ssam					    "support quarter-rate channel\n",
1858187841Ssam					    freq);
1859178354Ssam				continue;
1860178354Ssam			}
1861178354Ssam			if ((flags & IEEE80211_CHAN_HT20) &&
1862178354Ssam			    (chanFlags & IEEE80211_CHAN_HT20) == 0) {
1863178354Ssam				if (verbose)
1864178354Ssam					printf("%u: skip, device does not support HT20 operation\n", freq);
1865178354Ssam				continue;
1866178354Ssam			}
1867178354Ssam			if ((flags & IEEE80211_CHAN_HT40) &&
1868178354Ssam			    (chanFlags & IEEE80211_CHAN_HT40) == 0) {
1869178354Ssam				if (verbose)
1870178354Ssam					printf("%u: skip, device does not support HT40 operation\n", freq);
1871178354Ssam				continue;
1872178354Ssam			}
1873178354Ssam			if ((flags & REQ_ECM) && !reg->ecm) {
1874178354Ssam				if (verbose)
1875178354Ssam					printf("%u: skip, ECM channel\n", freq);
1876178354Ssam				continue;
1877178354Ssam			}
1878178354Ssam			if ((flags & REQ_OUTDOOR) && reg->location == 'I') {
1879178354Ssam				if (verbose)
1880178354Ssam					printf("%u: skip, outdoor channel\n", freq);
1881178354Ssam				continue;
1882178354Ssam			}
1883178354Ssam			if ((flags & IEEE80211_CHAN_HT40) &&
1884178354Ssam			    prev != NULL && (freq - prev->ic_freq) < channelSep) {
1885178354Ssam				if (verbose)
1886178354Ssam					printf("%u: skip, only %u channel "
1887178354Ssam					    "separation, need %d\n", freq,
1888178354Ssam					    freq - prev->ic_freq, channelSep);
1889178354Ssam				continue;
1890178354Ssam			}
1891178354Ssam			if (ci->ic_nchans == IEEE80211_CHAN_MAX) {
1892178354Ssam				if (verbose)
1893178354Ssam					printf("%u: skip, channel table full\n", freq);
1894178354Ssam				break;
1895178354Ssam			}
1896178354Ssam			c = &ci->ic_chans[ci->ic_nchans++];
1897187801Ssam			memset(c, 0, sizeof(*c));
1898178354Ssam			c->ic_freq = freq;
1899178354Ssam			c->ic_flags = chanFlags |
1900178354Ssam			    (flags &~ (REQ_FLAGS | IEEE80211_CHAN_HT40));
1901178354Ssam			if (c->ic_flags & IEEE80211_CHAN_DFS)
1902178354Ssam				c->ic_maxregpower = nb->maxPowerDFS;
1903178354Ssam			else
1904178354Ssam				c->ic_maxregpower = nb->maxPower;
1905178354Ssam			if (verbose)
1906178354Ssam				printf("[%3d] add freq %u flags 0x%x power %u\n",
1907178354Ssam				    ci->ic_nchans-1, c->ic_freq, c->ic_flags,
1908178354Ssam				    c->ic_maxregpower);
1909178354Ssam			/* NB: kernel fills in other fields */
1910178354Ssam			prev = c;
1911178354Ssam		}
1912178354Ssam	}
1913178354Ssam}
1914178354Ssam
1915178354Ssamstatic void
1916178354Ssamregdomain_makechannels(
1917178354Ssam	struct ieee80211_regdomain_req *req,
1918178354Ssam	const struct ieee80211_devcaps_req *dc)
1919178354Ssam{
1920178354Ssam	struct regdata *rdp = getregdata();
1921178354Ssam	const struct country *cc;
1922178354Ssam	const struct ieee80211_regdomain *reg = &req->rd;
1923178354Ssam	struct ieee80211req_chaninfo *ci = &req->chaninfo;
1924178354Ssam	const struct regdomain *rd;
1925178354Ssam
1926178354Ssam	/*
1927178354Ssam	 * Locate construction table for new channel list.  We treat
1928178354Ssam	 * the regdomain/SKU as definitive so a country can be in
1929178354Ssam	 * multiple with different properties (e.g. US in FCC+FCC3).
1930178354Ssam	 * If no regdomain is specified then we fallback on the country
1931178354Ssam	 * code to find the associated regdomain since countries always
1932178354Ssam	 * belong to at least one regdomain.
1933178354Ssam	 */
1934178354Ssam	if (reg->regdomain == 0) {
1935178354Ssam		cc = lib80211_country_findbycc(rdp, reg->country);
1936178354Ssam		if (cc == NULL)
1937178354Ssam			errx(1, "internal error, country %d not found",
1938178354Ssam			    reg->country);
1939178354Ssam		rd = cc->rd;
1940178354Ssam	} else
1941178354Ssam		rd = lib80211_regdomain_findbysku(rdp, reg->regdomain);
1942178354Ssam	if (rd == NULL)
1943178354Ssam		errx(1, "internal error, regdomain %d not found",
1944178354Ssam			    reg->regdomain);
1945178354Ssam	if (rd->sku != SKU_DEBUG) {
1946187801Ssam		/*
1947187801Ssam		 * regdomain_addchans incrememnts the channel count for
1948187801Ssam		 * each channel it adds so initialize ic_nchans to zero.
1949187801Ssam		 * Note that we know we have enough space to hold all possible
1950187801Ssam		 * channels because the devcaps list size was used to
1951187801Ssam		 * allocate our request.
1952187801Ssam		 */
1953187801Ssam		ci->ic_nchans = 0;
1954178354Ssam		if (!LIST_EMPTY(&rd->bands_11b))
1955178354Ssam			regdomain_addchans(ci, &rd->bands_11b, reg,
1956178354Ssam			    IEEE80211_CHAN_B, &dc->dc_chaninfo);
1957187841Ssam		if (!LIST_EMPTY(&rd->bands_11g))
1958178354Ssam			regdomain_addchans(ci, &rd->bands_11g, reg,
1959178354Ssam			    IEEE80211_CHAN_G, &dc->dc_chaninfo);
1960187841Ssam		if (!LIST_EMPTY(&rd->bands_11a))
1961178354Ssam			regdomain_addchans(ci, &rd->bands_11a, reg,
1962178354Ssam			    IEEE80211_CHAN_A, &dc->dc_chaninfo);
1963178354Ssam		if (!LIST_EMPTY(&rd->bands_11na)) {
1964178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
1965178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT20,
1966178354Ssam			    &dc->dc_chaninfo);
1967178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
1968178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U,
1969178354Ssam			    &dc->dc_chaninfo);
1970178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
1971178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D,
1972178354Ssam			    &dc->dc_chaninfo);
1973178354Ssam		}
1974178354Ssam		if (!LIST_EMPTY(&rd->bands_11ng)) {
1975178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
1976178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT20,
1977178354Ssam			    &dc->dc_chaninfo);
1978178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
1979178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U,
1980178354Ssam			    &dc->dc_chaninfo);
1981178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
1982178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D,
1983178354Ssam			    &dc->dc_chaninfo);
1984178354Ssam		}
1985178354Ssam		qsort(ci->ic_chans, ci->ic_nchans, sizeof(ci->ic_chans[0]),
1986178354Ssam		    regdomain_sort);
1987178354Ssam	} else
1988187801Ssam		memcpy(ci, &dc->dc_chaninfo,
1989187801Ssam		    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
1990178354Ssam}
1991178354Ssam
1992178354Ssamstatic void
1993178354Ssamlist_countries(void)
1994178354Ssam{
1995178354Ssam	struct regdata *rdp = getregdata();
1996178354Ssam	const struct country *cp;
1997178354Ssam	const struct regdomain *dp;
1998178354Ssam	int i;
1999178354Ssam
2000178354Ssam	i = 0;
2001178354Ssam	printf("\nCountry codes:\n");
2002178354Ssam	LIST_FOREACH(cp, &rdp->countries, next) {
2003178354Ssam		printf("%2s %-15.15s%s", cp->isoname,
2004178354Ssam		    cp->name, ((i+1)%4) == 0 ? "\n" : " ");
2005178354Ssam		i++;
2006178354Ssam	}
2007178354Ssam	i = 0;
2008178354Ssam	printf("\nRegulatory domains:\n");
2009178354Ssam	LIST_FOREACH(dp, &rdp->domains, next) {
2010178354Ssam		printf("%-15.15s%s", dp->name, ((i+1)%4) == 0 ? "\n" : " ");
2011178354Ssam		i++;
2012178354Ssam	}
2013178354Ssam	printf("\n");
2014178354Ssam}
2015178354Ssam
2016178354Ssamstatic void
2017178354Ssamdefaultcountry(const struct regdomain *rd)
2018178354Ssam{
2019178354Ssam	struct regdata *rdp = getregdata();
2020178354Ssam	const struct country *cc;
2021178354Ssam
2022178354Ssam	cc = lib80211_country_findbycc(rdp, rd->cc->code);
2023178354Ssam	if (cc == NULL)
2024178354Ssam		errx(1, "internal error, ISO country code %d not "
2025178354Ssam		    "defined for regdomain %s", rd->cc->code, rd->name);
2026178354Ssam	regdomain.country = cc->code;
2027178354Ssam	regdomain.isocc[0] = cc->isoname[0];
2028178354Ssam	regdomain.isocc[1] = cc->isoname[1];
2029178354Ssam}
2030178354Ssam
2031178354Ssamstatic
2032178354SsamDECL_CMD_FUNC(set80211regdomain, val, d)
2033178354Ssam{
2034178354Ssam	struct regdata *rdp = getregdata();
2035178354Ssam	const struct regdomain *rd;
2036178354Ssam
2037178354Ssam	rd = lib80211_regdomain_findbyname(rdp, val);
2038178354Ssam	if (rd == NULL) {
2039186104Ssam		char *eptr;
2040186104Ssam		long sku = strtol(val, &eptr, 0);
2041186104Ssam
2042186104Ssam		if (eptr != val)
2043186104Ssam			rd = lib80211_regdomain_findbysku(rdp, sku);
2044186104Ssam		if (eptr == val || rd == NULL)
2045178354Ssam			errx(1, "unknown regdomain %s", val);
2046178354Ssam	}
2047178354Ssam	getregdomain(s);
2048178354Ssam	regdomain.regdomain = rd->sku;
2049178354Ssam	if (regdomain.country == 0 && rd->cc != NULL) {
2050178354Ssam		/*
2051178354Ssam		 * No country code setup and there's a default
2052178354Ssam		 * one for this regdomain fill it in.
2053178354Ssam		 */
2054178354Ssam		defaultcountry(rd);
2055178354Ssam	}
2056178354Ssam	callback_register(setregdomain_cb, &regdomain);
2057178354Ssam}
2058178354Ssam
2059178354Ssamstatic
2060178354SsamDECL_CMD_FUNC(set80211country, val, d)
2061178354Ssam{
2062178354Ssam	struct regdata *rdp = getregdata();
2063178354Ssam	const struct country *cc;
2064178354Ssam
2065178354Ssam	cc = lib80211_country_findbyname(rdp, val);
2066178354Ssam	if (cc == NULL) {
2067186103Ssam		char *eptr;
2068186103Ssam		long code = strtol(val, &eptr, 0);
2069186103Ssam
2070186103Ssam		if (eptr != val)
2071186103Ssam			cc = lib80211_country_findbycc(rdp, code);
2072186103Ssam		if (eptr == val || cc == NULL)
2073178354Ssam			errx(1, "unknown ISO country code %s", val);
2074178354Ssam	}
2075178354Ssam	getregdomain(s);
2076178354Ssam	regdomain.regdomain = cc->rd->sku;
2077178354Ssam	regdomain.country = cc->code;
2078178354Ssam	regdomain.isocc[0] = cc->isoname[0];
2079178354Ssam	regdomain.isocc[1] = cc->isoname[1];
2080178354Ssam	callback_register(setregdomain_cb, &regdomain);
2081178354Ssam}
2082178354Ssam
2083178354Ssamstatic void
2084178354Ssamset80211location(const char *val, int d, int s, const struct afswtch *rafp)
2085178354Ssam{
2086178354Ssam	getregdomain(s);
2087178354Ssam	regdomain.location = d;
2088178354Ssam	callback_register(setregdomain_cb, &regdomain);
2089178354Ssam}
2090178354Ssam
2091178354Ssamstatic void
2092178354Ssamset80211ecm(const char *val, int d, int s, const struct afswtch *rafp)
2093178354Ssam{
2094178354Ssam	getregdomain(s);
2095178354Ssam	regdomain.ecm = d;
2096178354Ssam	callback_register(setregdomain_cb, &regdomain);
2097178354Ssam}
2098178354Ssam
2099178354Ssamstatic void
2100173275SsamLINE_INIT(char c)
2101173275Ssam{
2102173275Ssam	spacer = c;
2103173275Ssam	if (c == '\t')
2104173275Ssam		col = 8;
2105173275Ssam	else
2106173275Ssam		col = 1;
2107173275Ssam}
2108173275Ssam
2109173275Ssamstatic void
2110173275SsamLINE_BREAK(void)
2111173275Ssam{
2112173275Ssam	if (spacer != '\t') {
2113173275Ssam		printf("\n");
2114173275Ssam		spacer = '\t';
2115173275Ssam	}
2116173275Ssam	col = 8;		/* 8-col tab */
2117173275Ssam}
2118173275Ssam
2119173275Ssamstatic void
2120173275SsamLINE_CHECK(const char *fmt, ...)
2121173275Ssam{
2122173275Ssam	char buf[80];
2123173275Ssam	va_list ap;
2124173275Ssam	int n;
2125173275Ssam
2126173275Ssam	va_start(ap, fmt);
2127173275Ssam	n = vsnprintf(buf+1, sizeof(buf)-1, fmt, ap);
2128173275Ssam	va_end(ap);
2129173275Ssam	col += 1+n;
2130173275Ssam	if (col > MAXCOL) {
2131173275Ssam		LINE_BREAK();
2132173275Ssam		col += n;
2133173275Ssam	}
2134173275Ssam	buf[0] = spacer;
2135173275Ssam	printf("%s", buf);
2136173275Ssam	spacer = ' ';
2137173275Ssam}
2138173275Ssam
2139138593Ssamstatic int
2140170531Ssamgetmaxrate(const uint8_t rates[15], uint8_t nrates)
2141138593Ssam{
2142138593Ssam	int i, maxrate = -1;
2143138593Ssam
2144138593Ssam	for (i = 0; i < nrates; i++) {
2145138593Ssam		int rate = rates[i] & IEEE80211_RATE_VAL;
2146138593Ssam		if (rate > maxrate)
2147138593Ssam			maxrate = rate;
2148138593Ssam	}
2149138593Ssam	return maxrate / 2;
2150138593Ssam}
2151138593Ssam
2152138593Ssamstatic const char *
2153138593Ssamgetcaps(int capinfo)
2154138593Ssam{
2155138593Ssam	static char capstring[32];
2156138593Ssam	char *cp = capstring;
2157138593Ssam
2158138593Ssam	if (capinfo & IEEE80211_CAPINFO_ESS)
2159138593Ssam		*cp++ = 'E';
2160138593Ssam	if (capinfo & IEEE80211_CAPINFO_IBSS)
2161138593Ssam		*cp++ = 'I';
2162138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
2163138593Ssam		*cp++ = 'c';
2164138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
2165138593Ssam		*cp++ = 'C';
2166138593Ssam	if (capinfo & IEEE80211_CAPINFO_PRIVACY)
2167138593Ssam		*cp++ = 'P';
2168138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
2169138593Ssam		*cp++ = 'S';
2170138593Ssam	if (capinfo & IEEE80211_CAPINFO_PBCC)
2171138593Ssam		*cp++ = 'B';
2172138593Ssam	if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
2173138593Ssam		*cp++ = 'A';
2174138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
2175138593Ssam		*cp++ = 's';
2176138593Ssam	if (capinfo & IEEE80211_CAPINFO_RSN)
2177138593Ssam		*cp++ = 'R';
2178138593Ssam	if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
2179138593Ssam		*cp++ = 'D';
2180138593Ssam	*cp = '\0';
2181138593Ssam	return capstring;
2182138593Ssam}
2183138593Ssam
2184159885Ssamstatic const char *
2185159885Ssamgetflags(int flags)
2186159885Ssam{
2187159885Ssam	static char flagstring[32];
2188159885Ssam	char *cp = flagstring;
2189159885Ssam
2190159885Ssam	if (flags & IEEE80211_NODE_AUTH)
2191159885Ssam		*cp++ = 'A';
2192159885Ssam	if (flags & IEEE80211_NODE_QOS)
2193159885Ssam		*cp++ = 'Q';
2194159885Ssam	if (flags & IEEE80211_NODE_ERP)
2195159885Ssam		*cp++ = 'E';
2196159885Ssam	if (flags & IEEE80211_NODE_PWR_MGT)
2197159885Ssam		*cp++ = 'P';
2198173275Ssam	if (flags & IEEE80211_NODE_HT) {
2199170531Ssam		*cp++ = 'H';
2200173275Ssam		if (flags & IEEE80211_NODE_HTCOMPAT)
2201173275Ssam			*cp++ = '+';
2202173275Ssam	}
2203173275Ssam	if (flags & IEEE80211_NODE_WPS)
2204173275Ssam		*cp++ = 'W';
2205173275Ssam	if (flags & IEEE80211_NODE_TSN)
2206183261Ssam		*cp++ = 'N';
2207183261Ssam	if (flags & IEEE80211_NODE_AMPDU_TX)
2208173275Ssam		*cp++ = 'T';
2209183261Ssam	if (flags & IEEE80211_NODE_AMPDU_RX)
2210183261Ssam		*cp++ = 'R';
2211183261Ssam	if (flags & IEEE80211_NODE_MIMO_PS) {
2212183261Ssam		*cp++ = 'M';
2213183261Ssam		if (flags & IEEE80211_NODE_MIMO_RTS)
2214183261Ssam			*cp++ = '+';
2215183261Ssam	}
2216183261Ssam	if (flags & IEEE80211_NODE_RIFS)
2217183261Ssam		*cp++ = 'I';
2218159885Ssam	*cp = '\0';
2219159885Ssam	return flagstring;
2220159885Ssam}
2221159885Ssam
2222138593Ssamstatic void
2223138593Ssamprintie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
2224138593Ssam{
2225138593Ssam	printf("%s", tag);
2226138593Ssam	if (verbose) {
2227138593Ssam		maxlen -= strlen(tag)+2;
2228138593Ssam		if (2*ielen > maxlen)
2229138593Ssam			maxlen--;
2230138593Ssam		printf("<");
2231138593Ssam		for (; ielen > 0; ie++, ielen--) {
2232138593Ssam			if (maxlen-- <= 0)
2233138593Ssam				break;
2234138593Ssam			printf("%02x", *ie);
2235138593Ssam		}
2236138593Ssam		if (ielen != 0)
2237138593Ssam			printf("-");
2238138593Ssam		printf(">");
2239138593Ssam	}
2240138593Ssam}
2241138593Ssam
2242170531Ssam#define LE_READ_2(p)					\
2243170531Ssam	((u_int16_t)					\
2244170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2245170531Ssam	  (((const u_int8_t *)(p))[1] <<  8)))
2246170531Ssam#define LE_READ_4(p)					\
2247170531Ssam	((u_int32_t)					\
2248170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2249170531Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
2250170531Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
2251170531Ssam	  (((const u_int8_t *)(p))[3] << 24)))
2252170531Ssam
2253138593Ssam/*
2254170531Ssam * NB: The decoding routines assume a properly formatted ie
2255170531Ssam *     which should be safe as the kernel only retains them
2256170531Ssam *     if they parse ok.
2257170531Ssam */
2258170531Ssam
2259170531Ssamstatic void
2260173275Ssamprintwmeparam(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2261170531Ssam{
2262170531Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
2263170531Ssam	static const char *acnames[] = { "BE", "BK", "VO", "VI" };
2264173275Ssam	const struct ieee80211_wme_param *wme =
2265173275Ssam	    (const struct ieee80211_wme_param *) ie;
2266170531Ssam	int i;
2267170531Ssam
2268170531Ssam	printf("%s", tag);
2269173275Ssam	if (!verbose)
2270173275Ssam		return;
2271173275Ssam	printf("<qosinfo 0x%x", wme->param_qosInfo);
2272173275Ssam	ie += offsetof(struct ieee80211_wme_param, params_acParams);
2273173275Ssam	for (i = 0; i < WME_NUM_AC; i++) {
2274173275Ssam		const struct ieee80211_wme_acparams *ac =
2275173275Ssam		    &wme->params_acParams[i];
2276173275Ssam
2277173275Ssam		printf(" %s[%saifsn %u cwmin %u cwmax %u txop %u]"
2278173275Ssam			, acnames[i]
2279173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_ACM) ? "acm " : ""
2280173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_AIFSN)
2281173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMIN)
2282173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMAX)
2283173275Ssam			, LE_READ_2(&ac->acp_txop)
2284173275Ssam		);
2285170531Ssam	}
2286173275Ssam	printf(">");
2287170531Ssam#undef MS
2288170531Ssam}
2289170531Ssam
2290170531Ssamstatic void
2291173275Ssamprintwmeinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2292173275Ssam{
2293173275Ssam	printf("%s", tag);
2294173275Ssam	if (verbose) {
2295173275Ssam		const struct ieee80211_wme_info *wme =
2296173275Ssam		    (const struct ieee80211_wme_info *) ie;
2297173275Ssam		printf("<version 0x%x info 0x%x>",
2298173275Ssam		    wme->wme_version, wme->wme_info);
2299173275Ssam	}
2300173275Ssam}
2301173275Ssam
2302173275Ssamstatic void
2303173275Ssamprinthtcap(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2304173275Ssam{
2305173275Ssam	printf("%s", tag);
2306173275Ssam	if (verbose) {
2307173275Ssam		const struct ieee80211_ie_htcap *htcap =
2308173275Ssam		    (const struct ieee80211_ie_htcap *) ie;
2309173275Ssam		const char *sep;
2310173275Ssam		int i, j;
2311173275Ssam
2312173275Ssam		printf("<cap 0x%x param 0x%x",
2313173275Ssam		    LE_READ_2(&htcap->hc_cap), htcap->hc_param);
2314173275Ssam		printf(" mcsset[");
2315173275Ssam		sep = "";
2316173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2317173275Ssam			if (isset(htcap->hc_mcsset, i)) {
2318173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2319173275Ssam					if (isclr(htcap->hc_mcsset, j))
2320173275Ssam						break;
2321173275Ssam				j--;
2322173275Ssam				if (i == j)
2323173275Ssam					printf("%s%u", sep, i);
2324173275Ssam				else
2325173275Ssam					printf("%s%u-%u", sep, i, j);
2326173275Ssam				i += j-i;
2327173275Ssam				sep = ",";
2328173275Ssam			}
2329173275Ssam		printf("] extcap 0x%x txbf 0x%x antenna 0x%x>",
2330173275Ssam		    LE_READ_2(&htcap->hc_extcap),
2331173275Ssam		    LE_READ_4(&htcap->hc_txbf),
2332173275Ssam		    htcap->hc_antenna);
2333173275Ssam	}
2334173275Ssam}
2335173275Ssam
2336173275Ssamstatic void
2337173275Ssamprinthtinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2338173275Ssam{
2339173275Ssam	printf("%s", tag);
2340173275Ssam	if (verbose) {
2341173275Ssam		const struct ieee80211_ie_htinfo *htinfo =
2342173275Ssam		    (const struct ieee80211_ie_htinfo *) ie;
2343173275Ssam		const char *sep;
2344173275Ssam		int i, j;
2345173275Ssam
2346173275Ssam		printf("<ctl %u, %x,%x,%x,%x", htinfo->hi_ctrlchannel,
2347173275Ssam		    htinfo->hi_byte1, htinfo->hi_byte2, htinfo->hi_byte3,
2348173275Ssam		    LE_READ_2(&htinfo->hi_byte45));
2349173275Ssam		printf(" basicmcs[");
2350173275Ssam		sep = "";
2351173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2352173275Ssam			if (isset(htinfo->hi_basicmcsset, i)) {
2353173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2354173275Ssam					if (isclr(htinfo->hi_basicmcsset, j))
2355173275Ssam						break;
2356173275Ssam				j--;
2357173275Ssam				if (i == j)
2358173275Ssam					printf("%s%u", sep, i);
2359173275Ssam				else
2360173275Ssam					printf("%s%u-%u", sep, i, j);
2361173275Ssam				i += j-i;
2362173275Ssam				sep = ",";
2363173275Ssam			}
2364173275Ssam		printf("]>");
2365173275Ssam	}
2366173275Ssam}
2367173275Ssam
2368173275Ssamstatic void
2369170531Ssamprintathie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2370170531Ssam{
2371170531Ssam
2372170531Ssam	printf("%s", tag);
2373170531Ssam	if (verbose) {
2374170531Ssam		const struct ieee80211_ath_ie *ath =
2375170531Ssam			(const struct ieee80211_ath_ie *)ie;
2376170531Ssam
2377170531Ssam		printf("<");
2378170531Ssam		if (ath->ath_capability & ATHEROS_CAP_TURBO_PRIME)
2379170531Ssam			printf("DTURBO,");
2380170531Ssam		if (ath->ath_capability & ATHEROS_CAP_COMPRESSION)
2381170531Ssam			printf("COMP,");
2382170531Ssam		if (ath->ath_capability & ATHEROS_CAP_FAST_FRAME)
2383170531Ssam			printf("FF,");
2384170531Ssam		if (ath->ath_capability & ATHEROS_CAP_XR)
2385170531Ssam			printf("XR,");
2386170531Ssam		if (ath->ath_capability & ATHEROS_CAP_AR)
2387170531Ssam			printf("AR,");
2388170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BURST)
2389170531Ssam			printf("BURST,");
2390170531Ssam		if (ath->ath_capability & ATHEROS_CAP_WME)
2391170531Ssam			printf("WME,");
2392170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BOOST)
2393170531Ssam			printf("BOOST,");
2394170531Ssam		printf("0x%x>", LE_READ_2(ath->ath_defkeyix));
2395170531Ssam	}
2396170531Ssam}
2397170531Ssam
2398170531Ssamstatic const char *
2399170531Ssamwpa_cipher(const u_int8_t *sel)
2400170531Ssam{
2401170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2402170531Ssam	u_int32_t w = LE_READ_4(sel);
2403170531Ssam
2404170531Ssam	switch (w) {
2405170531Ssam	case WPA_SEL(WPA_CSE_NULL):
2406170531Ssam		return "NONE";
2407170531Ssam	case WPA_SEL(WPA_CSE_WEP40):
2408170531Ssam		return "WEP40";
2409170531Ssam	case WPA_SEL(WPA_CSE_WEP104):
2410170531Ssam		return "WEP104";
2411170531Ssam	case WPA_SEL(WPA_CSE_TKIP):
2412170531Ssam		return "TKIP";
2413170531Ssam	case WPA_SEL(WPA_CSE_CCMP):
2414170531Ssam		return "AES-CCMP";
2415170531Ssam	}
2416170531Ssam	return "?";		/* NB: so 1<< is discarded */
2417170531Ssam#undef WPA_SEL
2418170531Ssam}
2419170531Ssam
2420170531Ssamstatic const char *
2421170531Ssamwpa_keymgmt(const u_int8_t *sel)
2422170531Ssam{
2423170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2424170531Ssam	u_int32_t w = LE_READ_4(sel);
2425170531Ssam
2426170531Ssam	switch (w) {
2427170531Ssam	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
2428170531Ssam		return "8021X-UNSPEC";
2429170531Ssam	case WPA_SEL(WPA_ASE_8021X_PSK):
2430170531Ssam		return "8021X-PSK";
2431170531Ssam	case WPA_SEL(WPA_ASE_NONE):
2432170531Ssam		return "NONE";
2433170531Ssam	}
2434170531Ssam	return "?";
2435170531Ssam#undef WPA_SEL
2436170531Ssam}
2437170531Ssam
2438170531Ssamstatic void
2439170531Ssamprintwpaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2440170531Ssam{
2441170531Ssam	u_int8_t len = ie[1];
2442170531Ssam
2443170531Ssam	printf("%s", tag);
2444170531Ssam	if (verbose) {
2445170531Ssam		const char *sep;
2446170531Ssam		int n;
2447170531Ssam
2448170531Ssam		ie += 6, len -= 4;		/* NB: len is payload only */
2449170531Ssam
2450170531Ssam		printf("<v%u", LE_READ_2(ie));
2451170531Ssam		ie += 2, len -= 2;
2452170531Ssam
2453170531Ssam		printf(" mc:%s", wpa_cipher(ie));
2454170531Ssam		ie += 4, len -= 4;
2455170531Ssam
2456170531Ssam		/* unicast ciphers */
2457170531Ssam		n = LE_READ_2(ie);
2458170531Ssam		ie += 2, len -= 2;
2459170531Ssam		sep = " uc:";
2460170531Ssam		for (; n > 0; n--) {
2461170531Ssam			printf("%s%s", sep, wpa_cipher(ie));
2462170531Ssam			ie += 4, len -= 4;
2463170531Ssam			sep = "+";
2464170531Ssam		}
2465170531Ssam
2466170531Ssam		/* key management algorithms */
2467170531Ssam		n = LE_READ_2(ie);
2468170531Ssam		ie += 2, len -= 2;
2469170531Ssam		sep = " km:";
2470170531Ssam		for (; n > 0; n--) {
2471170531Ssam			printf("%s%s", sep, wpa_keymgmt(ie));
2472170531Ssam			ie += 4, len -= 4;
2473170531Ssam			sep = "+";
2474170531Ssam		}
2475170531Ssam
2476170531Ssam		if (len > 2)		/* optional capabilities */
2477170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2478170531Ssam		printf(">");
2479170531Ssam	}
2480170531Ssam}
2481170531Ssam
2482170531Ssamstatic const char *
2483170531Ssamrsn_cipher(const u_int8_t *sel)
2484170531Ssam{
2485170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2486170531Ssam	u_int32_t w = LE_READ_4(sel);
2487170531Ssam
2488170531Ssam	switch (w) {
2489170531Ssam	case RSN_SEL(RSN_CSE_NULL):
2490170531Ssam		return "NONE";
2491170531Ssam	case RSN_SEL(RSN_CSE_WEP40):
2492170531Ssam		return "WEP40";
2493170531Ssam	case RSN_SEL(RSN_CSE_WEP104):
2494170531Ssam		return "WEP104";
2495170531Ssam	case RSN_SEL(RSN_CSE_TKIP):
2496170531Ssam		return "TKIP";
2497170531Ssam	case RSN_SEL(RSN_CSE_CCMP):
2498170531Ssam		return "AES-CCMP";
2499170531Ssam	case RSN_SEL(RSN_CSE_WRAP):
2500170531Ssam		return "AES-OCB";
2501170531Ssam	}
2502170531Ssam	return "?";
2503170531Ssam#undef WPA_SEL
2504170531Ssam}
2505170531Ssam
2506170531Ssamstatic const char *
2507170531Ssamrsn_keymgmt(const u_int8_t *sel)
2508170531Ssam{
2509170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2510170531Ssam	u_int32_t w = LE_READ_4(sel);
2511170531Ssam
2512170531Ssam	switch (w) {
2513170531Ssam	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
2514170531Ssam		return "8021X-UNSPEC";
2515170531Ssam	case RSN_SEL(RSN_ASE_8021X_PSK):
2516170531Ssam		return "8021X-PSK";
2517170531Ssam	case RSN_SEL(RSN_ASE_NONE):
2518170531Ssam		return "NONE";
2519170531Ssam	}
2520170531Ssam	return "?";
2521170531Ssam#undef RSN_SEL
2522170531Ssam}
2523170531Ssam
2524170531Ssamstatic void
2525170531Ssamprintrsnie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2526170531Ssam{
2527170531Ssam	printf("%s", tag);
2528170531Ssam	if (verbose) {
2529170531Ssam		const char *sep;
2530170531Ssam		int n;
2531170531Ssam
2532173275Ssam		ie += 2, ielen -= 2;
2533170531Ssam
2534170531Ssam		printf("<v%u", LE_READ_2(ie));
2535173275Ssam		ie += 2, ielen -= 2;
2536170531Ssam
2537170531Ssam		printf(" mc:%s", rsn_cipher(ie));
2538173275Ssam		ie += 4, ielen -= 4;
2539170531Ssam
2540170531Ssam		/* unicast ciphers */
2541170531Ssam		n = LE_READ_2(ie);
2542173275Ssam		ie += 2, ielen -= 2;
2543170531Ssam		sep = " uc:";
2544170531Ssam		for (; n > 0; n--) {
2545170531Ssam			printf("%s%s", sep, rsn_cipher(ie));
2546173275Ssam			ie += 4, ielen -= 4;
2547170531Ssam			sep = "+";
2548170531Ssam		}
2549170531Ssam
2550170531Ssam		/* key management algorithms */
2551170531Ssam		n = LE_READ_2(ie);
2552173275Ssam		ie += 2, ielen -= 2;
2553170531Ssam		sep = " km:";
2554170531Ssam		for (; n > 0; n--) {
2555170531Ssam			printf("%s%s", sep, rsn_keymgmt(ie));
2556173275Ssam			ie += 4, ielen -= 4;
2557170531Ssam			sep = "+";
2558170531Ssam		}
2559170531Ssam
2560173275Ssam		if (ielen > 2)		/* optional capabilities */
2561170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2562170531Ssam		/* XXXPMKID */
2563170531Ssam		printf(">");
2564170531Ssam	}
2565170531Ssam}
2566170531Ssam
2567181454Ssam/* XXX move to a public include file */
2568181454Ssam#define IEEE80211_WPS_DEV_PASS_ID	0x1012
2569181454Ssam#define IEEE80211_WPS_SELECTED_REG	0x1041
2570181454Ssam#define IEEE80211_WPS_SETUP_STATE	0x1044
2571181454Ssam#define IEEE80211_WPS_UUID_E		0x1047
2572181454Ssam#define IEEE80211_WPS_VERSION		0x104a
2573181454Ssam
2574181454Ssam#define BE_READ_2(p)					\
2575181454Ssam	((u_int16_t)					\
2576181454Ssam	 ((((const u_int8_t *)(p))[1]      ) |		\
2577181454Ssam	  (((const u_int8_t *)(p))[0] <<  8)))
2578181454Ssam
2579181454Ssamstatic void
2580181454Ssamprintwpsie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2581181454Ssam{
2582181454Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
2583181454Ssam	u_int8_t len = ie[1];
2584181454Ssam
2585181454Ssam	printf("%s", tag);
2586181454Ssam	if (verbose) {
2587181454Ssam		static const char *dev_pass_id[] = {
2588181454Ssam			"D",	/* Default (PIN) */
2589181454Ssam			"U",	/* User-specified */
2590181454Ssam			"M",	/* Machine-specified */
2591181454Ssam			"K",	/* Rekey */
2592181454Ssam			"P",	/* PushButton */
2593181454Ssam			"R"	/* Registrar-specified */
2594181454Ssam		};
2595181454Ssam		int n;
2596181454Ssam
2597181454Ssam		ie +=6, len -= 4;		/* NB: len is payload only */
2598181454Ssam
2599181454Ssam		/* WPS IE in Beacon and Probe Resp frames have different fields */
2600181454Ssam		printf("<");
2601181454Ssam		while (len) {
2602181454Ssam			uint16_t tlv_type = BE_READ_2(ie);
2603181454Ssam			uint16_t tlv_len  = BE_READ_2(ie + 2);
2604181454Ssam
2605181454Ssam			ie += 4, len -= 4;
2606181454Ssam
2607181454Ssam			switch (tlv_type) {
2608181454Ssam			case IEEE80211_WPS_VERSION:
2609181454Ssam				printf("v:%d.%d", *ie >> 4, *ie & 0xf);
2610181454Ssam				break;
2611181454Ssam			case IEEE80211_WPS_SETUP_STATE:
2612181454Ssam				/* Only 1 and 2 are valid */
2613181454Ssam				if (*ie == 0 || *ie >= 3)
2614181454Ssam					printf(" state:B");
2615181454Ssam				else
2616181454Ssam					printf(" st:%s", *ie == 1 ? "N" : "C");
2617181454Ssam				break;
2618181454Ssam			case IEEE80211_WPS_SELECTED_REG:
2619181454Ssam				printf(" sel:%s", *ie ? "T" : "F");
2620181454Ssam				break;
2621181454Ssam			case IEEE80211_WPS_DEV_PASS_ID:
2622181454Ssam				n = LE_READ_2(ie);
2623181454Ssam				if (n < N(dev_pass_id))
2624181454Ssam					printf(" dpi:%s", dev_pass_id[n]);
2625181454Ssam				break;
2626181454Ssam			case IEEE80211_WPS_UUID_E:
2627181454Ssam				printf(" uuid-e:");
2628181454Ssam				for (n = 0; n < (tlv_len - 1); n++)
2629181454Ssam					printf("%02x-", ie[n]);
2630181454Ssam				printf("%02x", ie[n]);
2631181454Ssam				break;
2632181454Ssam			}
2633181454Ssam			ie += tlv_len, len -= tlv_len;
2634181454Ssam		}
2635181454Ssam		printf(">");
2636181454Ssam	}
2637181454Ssam#undef N
2638181454Ssam}
2639181454Ssam
2640186904Ssamstatic void
2641186904Ssamprinttdmaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2642186904Ssam{
2643186904Ssam	printf("%s", tag);
2644186904Ssam	if (verbose && ielen >= sizeof(struct ieee80211_tdma_param)) {
2645186904Ssam		const struct ieee80211_tdma_param *tdma =
2646186904Ssam		   (const struct ieee80211_tdma_param *) ie;
2647186904Ssam
2648186904Ssam		/* XXX tstamp */
2649186904Ssam		printf("<v%u slot:%u slotcnt:%u slotlen:%u bintval:%u inuse:0x%x>",
2650186904Ssam		    tdma->tdma_version, tdma->tdma_slot, tdma->tdma_slotcnt,
2651186904Ssam		    LE_READ_2(&tdma->tdma_slotlen), tdma->tdma_bintval,
2652186904Ssam		    tdma->tdma_inuse[0]);
2653186904Ssam	}
2654186904Ssam}
2655186904Ssam
2656170531Ssam/*
2657138593Ssam * Copy the ssid string contents into buf, truncating to fit.  If the
2658138593Ssam * ssid is entirely printable then just copy intact.  Otherwise convert
2659138593Ssam * to hexadecimal.  If the result is truncated then replace the last
2660138593Ssam * three characters with "...".
2661138593Ssam */
2662146873Sjhbstatic int
2663138593Ssamcopy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
2664138593Ssam{
2665138593Ssam	const u_int8_t *p;
2666138593Ssam	size_t maxlen;
2667138593Ssam	int i;
2668138593Ssam
2669138593Ssam	if (essid_len > bufsize)
2670138593Ssam		maxlen = bufsize;
2671138593Ssam	else
2672138593Ssam		maxlen = essid_len;
2673138593Ssam	/* determine printable or not */
2674138593Ssam	for (i = 0, p = essid; i < maxlen; i++, p++) {
2675138593Ssam		if (*p < ' ' || *p > 0x7e)
2676138593Ssam			break;
2677138593Ssam	}
2678138593Ssam	if (i != maxlen) {		/* not printable, print as hex */
2679138593Ssam		if (bufsize < 3)
2680138593Ssam			return 0;
2681138593Ssam		strlcpy(buf, "0x", bufsize);
2682138593Ssam		bufsize -= 2;
2683138593Ssam		p = essid;
2684138593Ssam		for (i = 0; i < maxlen && bufsize >= 2; i++) {
2685147489Savatar			sprintf(&buf[2+2*i], "%02x", p[i]);
2686138593Ssam			bufsize -= 2;
2687138593Ssam		}
2688147489Savatar		if (i != essid_len)
2689147489Savatar			memcpy(&buf[2+2*i-3], "...", 3);
2690138593Ssam	} else {			/* printable, truncate as needed */
2691138593Ssam		memcpy(buf, essid, maxlen);
2692147489Savatar		if (maxlen != essid_len)
2693147489Savatar			memcpy(&buf[maxlen-3], "...", 3);
2694138593Ssam	}
2695138593Ssam	return maxlen;
2696138593Ssam}
2697138593Ssam
2698173275Ssamstatic void
2699173275Ssamprintssid(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2700173275Ssam{
2701173275Ssam	char ssid[2*IEEE80211_NWID_LEN+1];
2702173275Ssam
2703173275Ssam	printf("%s<%.*s>", tag, copy_essid(ssid, maxlen, ie+2, ie[1]), ssid);
2704173275Ssam}
2705173275Ssam
2706173275Ssamstatic void
2707173275Ssamprintrates(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2708173275Ssam{
2709173275Ssam	const char *sep;
2710173275Ssam	int i;
2711173275Ssam
2712173275Ssam	printf("%s", tag);
2713173275Ssam	sep = "<";
2714173275Ssam	for (i = 2; i < ielen; i++) {
2715173275Ssam		printf("%s%s%d", sep,
2716173275Ssam		    ie[i] & IEEE80211_RATE_BASIC ? "B" : "",
2717173275Ssam		    ie[i] & IEEE80211_RATE_VAL);
2718173275Ssam		sep = ",";
2719173275Ssam	}
2720173275Ssam	printf(">");
2721173275Ssam}
2722173275Ssam
2723173275Ssamstatic void
2724173275Ssamprintcountry(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2725173275Ssam{
2726173275Ssam	const struct ieee80211_country_ie *cie =
2727173275Ssam	   (const struct ieee80211_country_ie *) ie;
2728173275Ssam	int i, nbands, schan, nchan;
2729173275Ssam
2730173275Ssam	printf("%s<%c%c%c", tag, cie->cc[0], cie->cc[1], cie->cc[2]);
2731173275Ssam	nbands = (cie->len - 3) / sizeof(cie->band[0]);
2732173275Ssam	for (i = 0; i < nbands; i++) {
2733173275Ssam		schan = cie->band[i].schan;
2734173275Ssam		nchan = cie->band[i].nchan;
2735173275Ssam		if (nchan != 1)
2736173275Ssam			printf(" %u-%u,%u", schan, schan + nchan-1,
2737173275Ssam			    cie->band[i].maxtxpwr);
2738173275Ssam		else
2739173275Ssam			printf(" %u,%u", schan, cie->band[i].maxtxpwr);
2740173275Ssam	}
2741173275Ssam	printf(">");
2742173275Ssam}
2743173275Ssam
2744148686Sstefanf/* unaligned little endian access */
2745138593Ssam#define LE_READ_4(p)					\
2746138593Ssam	((u_int32_t)					\
2747138593Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2748138593Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
2749138593Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
2750138593Ssam	  (((const u_int8_t *)(p))[3] << 24)))
2751138593Ssam
2752178354Ssamstatic __inline int
2753138593Ssamiswpaoui(const u_int8_t *frm)
2754138593Ssam{
2755138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
2756138593Ssam}
2757138593Ssam
2758178354Ssamstatic __inline int
2759173275Ssamiswmeinfo(const u_int8_t *frm)
2760138593Ssam{
2761173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
2762173275Ssam		frm[6] == WME_INFO_OUI_SUBTYPE;
2763138593Ssam}
2764138593Ssam
2765178354Ssamstatic __inline int
2766173275Ssamiswmeparam(const u_int8_t *frm)
2767173275Ssam{
2768173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
2769173275Ssam		frm[6] == WME_PARAM_OUI_SUBTYPE;
2770173275Ssam}
2771173275Ssam
2772178354Ssamstatic __inline int
2773138593Ssamisatherosoui(const u_int8_t *frm)
2774138593Ssam{
2775138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
2776138593Ssam}
2777138593Ssam
2778181454Ssamstatic __inline int
2779186904Ssamistdmaoui(const uint8_t *frm)
2780186904Ssam{
2781186904Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((TDMA_OUI_TYPE<<24)|TDMA_OUI);
2782186904Ssam}
2783186904Ssam
2784186904Ssamstatic __inline int
2785181454Ssamiswpsoui(const uint8_t *frm)
2786181454Ssam{
2787181454Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPS_OUI_TYPE<<24)|WPA_OUI);
2788181454Ssam}
2789181454Ssam
2790173275Ssamstatic const char *
2791173275Ssamiename(int elemid)
2792173275Ssam{
2793173275Ssam	switch (elemid) {
2794173275Ssam	case IEEE80211_ELEMID_FHPARMS:	return " FHPARMS";
2795173275Ssam	case IEEE80211_ELEMID_CFPARMS:	return " CFPARMS";
2796173275Ssam	case IEEE80211_ELEMID_TIM:	return " TIM";
2797173275Ssam	case IEEE80211_ELEMID_IBSSPARMS:return " IBSSPARMS";
2798173275Ssam	case IEEE80211_ELEMID_CHALLENGE:return " CHALLENGE";
2799173275Ssam	case IEEE80211_ELEMID_PWRCNSTR:	return " PWRCNSTR";
2800173275Ssam	case IEEE80211_ELEMID_PWRCAP:	return " PWRCAP";
2801173275Ssam	case IEEE80211_ELEMID_TPCREQ:	return " TPCREQ";
2802173275Ssam	case IEEE80211_ELEMID_TPCREP:	return " TPCREP";
2803173275Ssam	case IEEE80211_ELEMID_SUPPCHAN:	return " SUPPCHAN";
2804173275Ssam	case IEEE80211_ELEMID_CHANSWITCHANN:return " CSA";
2805173275Ssam	case IEEE80211_ELEMID_MEASREQ:	return " MEASREQ";
2806173275Ssam	case IEEE80211_ELEMID_MEASREP:	return " MEASREP";
2807173275Ssam	case IEEE80211_ELEMID_QUIET:	return " QUIET";
2808173275Ssam	case IEEE80211_ELEMID_IBSSDFS:	return " IBSSDFS";
2809173275Ssam	case IEEE80211_ELEMID_TPC:	return " TPC";
2810173275Ssam	case IEEE80211_ELEMID_CCKM:	return " CCKM";
2811173275Ssam	}
2812173275Ssam	return " ???";
2813173275Ssam}
2814173275Ssam
2815138593Ssamstatic void
2816138593Ssamprinties(const u_int8_t *vp, int ielen, int maxcols)
2817138593Ssam{
2818138593Ssam	while (ielen > 0) {
2819138593Ssam		switch (vp[0]) {
2820173275Ssam		case IEEE80211_ELEMID_SSID:
2821173275Ssam			if (verbose)
2822173275Ssam				printssid(" SSID", vp, 2+vp[1], maxcols);
2823173275Ssam			break;
2824173275Ssam		case IEEE80211_ELEMID_RATES:
2825173275Ssam		case IEEE80211_ELEMID_XRATES:
2826173275Ssam			if (verbose)
2827173275Ssam				printrates(vp[0] == IEEE80211_ELEMID_RATES ?
2828173275Ssam				    " RATES" : " XRATES", vp, 2+vp[1], maxcols);
2829173275Ssam			break;
2830173275Ssam		case IEEE80211_ELEMID_DSPARMS:
2831173275Ssam			if (verbose)
2832173275Ssam				printf(" DSPARMS<%u>", vp[2]);
2833173275Ssam			break;
2834173275Ssam		case IEEE80211_ELEMID_COUNTRY:
2835173275Ssam			if (verbose)
2836173275Ssam				printcountry(" COUNTRY", vp, 2+vp[1], maxcols);
2837173275Ssam			break;
2838173275Ssam		case IEEE80211_ELEMID_ERP:
2839173275Ssam			if (verbose)
2840173275Ssam				printf(" ERP<0x%x>", vp[2]);
2841173275Ssam			break;
2842138593Ssam		case IEEE80211_ELEMID_VENDOR:
2843138593Ssam			if (iswpaoui(vp))
2844170531Ssam				printwpaie(" WPA", vp, 2+vp[1], maxcols);
2845173275Ssam			else if (iswmeinfo(vp))
2846173275Ssam				printwmeinfo(" WME", vp, 2+vp[1], maxcols);
2847173275Ssam			else if (iswmeparam(vp))
2848173275Ssam				printwmeparam(" WME", vp, 2+vp[1], maxcols);
2849139492Ssam			else if (isatherosoui(vp))
2850170531Ssam				printathie(" ATH", vp, 2+vp[1], maxcols);
2851181454Ssam			else if (iswpsoui(vp))
2852181454Ssam				printwpsie(" WPS", vp, 2+vp[1], maxcols);
2853186904Ssam			else if (istdmaoui(vp))
2854186904Ssam				printtdmaie(" TDMA", vp, 2+vp[1], maxcols);
2855173275Ssam			else if (verbose)
2856138593Ssam				printie(" VEN", vp, 2+vp[1], maxcols);
2857138593Ssam			break;
2858138593Ssam		case IEEE80211_ELEMID_RSN:
2859170531Ssam			printrsnie(" RSN", vp, 2+vp[1], maxcols);
2860138593Ssam			break;
2861173275Ssam		case IEEE80211_ELEMID_HTCAP:
2862173275Ssam			printhtcap(" HTCAP", vp, 2+vp[1], maxcols);
2863173275Ssam			break;
2864173275Ssam		case IEEE80211_ELEMID_HTINFO:
2865173275Ssam			if (verbose)
2866173275Ssam				printhtinfo(" HTINFO", vp, 2+vp[1], maxcols);
2867173275Ssam			break;
2868138593Ssam		default:
2869173275Ssam			if (verbose)
2870173275Ssam				printie(iename(vp[0]), vp, 2+vp[1], maxcols);
2871138593Ssam			break;
2872138593Ssam		}
2873138593Ssam		ielen -= 2+vp[1];
2874138593Ssam		vp += 2+vp[1];
2875138593Ssam	}
2876138593Ssam}
2877138593Ssam
2878138593Ssamstatic void
2879178354Ssamprintmimo(const struct ieee80211_mimo_info *mi)
2880178354Ssam{
2881178354Ssam	/* NB: don't muddy display unless there's something to show */
2882178354Ssam	if (mi->rssi[0] != 0 || mi->rssi[1] != 0 || mi->rssi[2] != 0) {
2883178354Ssam		/* XXX ignore EVM for now */
2884178354Ssam		printf(" (rssi %d:%d:%d nf %d:%d:%d)",
2885178354Ssam		    mi->rssi[0], mi->rssi[1], mi->rssi[2],
2886178354Ssam		    mi->noise[0], mi->noise[1], mi->noise[2]);
2887178354Ssam	}
2888178354Ssam}
2889178354Ssam
2890178354Ssamstatic void
2891138593Ssamlist_scan(int s)
2892138593Ssam{
2893138593Ssam	uint8_t buf[24*1024];
2894153892Srwatson	char ssid[IEEE80211_NWID_LEN+1];
2895173275Ssam	const uint8_t *cp;
2896154522Ssam	int len, ssidmax;
2897138593Ssam
2898173275Ssam	if (get80211len(s, IEEE80211_IOC_SCAN_RESULTS, buf, sizeof(buf), &len) < 0)
2899138593Ssam		errx(1, "unable to get scan results");
2900138593Ssam	if (len < sizeof(struct ieee80211req_scan_result))
2901138593Ssam		return;
2902138593Ssam
2903170531Ssam	getchaninfo(s);
2904170531Ssam
2905154522Ssam	ssidmax = verbose ? IEEE80211_NWID_LEN : 14;
2906170531Ssam	printf("%-*.*s  %-17.17s  %4s %4s  %-7s  %3s %4s\n"
2907154522Ssam		, ssidmax, ssidmax, "SSID"
2908138593Ssam		, "BSSID"
2909138593Ssam		, "CHAN"
2910138593Ssam		, "RATE"
2911170531Ssam		, " S:N"
2912138593Ssam		, "INT"
2913138593Ssam		, "CAPS"
2914138593Ssam	);
2915138593Ssam	cp = buf;
2916138593Ssam	do {
2917170531Ssam		const struct ieee80211req_scan_result *sr;
2918170531Ssam		const uint8_t *vp;
2919138593Ssam
2920170531Ssam		sr = (const struct ieee80211req_scan_result *) cp;
2921173275Ssam		vp = cp + sr->isr_ie_off;
2922170531Ssam		printf("%-*.*s  %s  %3d  %3dM %3d:%-3d  %3d %-4.4s"
2923154522Ssam			, ssidmax
2924155461Ssam			  , copy_essid(ssid, ssidmax, vp, sr->isr_ssid_len)
2925154522Ssam			  , ssid
2926138593Ssam			, ether_ntoa((const struct ether_addr *) sr->isr_bssid)
2927165570Ssam			, ieee80211_mhz2ieee(sr->isr_freq, sr->isr_flags)
2928138593Ssam			, getmaxrate(sr->isr_rates, sr->isr_nrates)
2929170531Ssam			, (sr->isr_rssi/2)+sr->isr_noise, sr->isr_noise
2930138593Ssam			, sr->isr_intval
2931138593Ssam			, getcaps(sr->isr_capinfo)
2932138593Ssam		);
2933170531Ssam		printies(vp + sr->isr_ssid_len, sr->isr_ie_len, 24);
2934138593Ssam		printf("\n");
2935138593Ssam		cp += sr->isr_len, len -= sr->isr_len;
2936138593Ssam	} while (len >= sizeof(struct ieee80211req_scan_result));
2937138593Ssam}
2938138593Ssam
2939178354Ssam#ifdef __FreeBSD__
2940138593Ssam#include <net80211/ieee80211_freebsd.h>
2941178354Ssam#endif
2942178354Ssam#ifdef __NetBSD__
2943178354Ssam#include <net80211/ieee80211_netbsd.h>
2944178354Ssam#endif
2945138593Ssam
2946138593Ssamstatic void
2947138593Ssamscan_and_wait(int s)
2948138593Ssam{
2949178354Ssam	struct ieee80211_scan_req sr;
2950138593Ssam	struct ieee80211req ireq;
2951138593Ssam	int sroute;
2952138593Ssam
2953138593Ssam	sroute = socket(PF_ROUTE, SOCK_RAW, 0);
2954138593Ssam	if (sroute < 0) {
2955138593Ssam		perror("socket(PF_ROUTE,SOCK_RAW)");
2956138593Ssam		return;
2957138593Ssam	}
2958138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
2959138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
2960138593Ssam	ireq.i_type = IEEE80211_IOC_SCAN_REQ;
2961178354Ssam
2962178354Ssam	memset(&sr, 0, sizeof(sr));
2963178354Ssam	sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE
2964178354Ssam		    | IEEE80211_IOC_SCAN_NOPICK
2965178354Ssam		    | IEEE80211_IOC_SCAN_ONCE;
2966178354Ssam	sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
2967178354Ssam	sr.sr_nssid = 0;
2968178354Ssam
2969178354Ssam	ireq.i_data = &sr;
2970178354Ssam	ireq.i_len = sizeof(sr);
2971138593Ssam	/* NB: only root can trigger a scan so ignore errors */
2972138593Ssam	if (ioctl(s, SIOCS80211, &ireq) >= 0) {
2973138593Ssam		char buf[2048];
2974138593Ssam		struct if_announcemsghdr *ifan;
2975138593Ssam		struct rt_msghdr *rtm;
2976138593Ssam
2977138593Ssam		do {
2978138593Ssam			if (read(sroute, buf, sizeof(buf)) < 0) {
2979138593Ssam				perror("read(PF_ROUTE)");
2980138593Ssam				break;
2981138593Ssam			}
2982138593Ssam			rtm = (struct rt_msghdr *) buf;
2983138593Ssam			if (rtm->rtm_version != RTM_VERSION)
2984138593Ssam				break;
2985138593Ssam			ifan = (struct if_announcemsghdr *) rtm;
2986138593Ssam		} while (rtm->rtm_type != RTM_IEEE80211 ||
2987138593Ssam		    ifan->ifan_what != RTM_IEEE80211_SCAN);
2988138593Ssam	}
2989138593Ssam	close(sroute);
2990138593Ssam}
2991138593Ssam
2992138593Ssamstatic
2993138593SsamDECL_CMD_FUNC(set80211scan, val, d)
2994138593Ssam{
2995138593Ssam	scan_and_wait(s);
2996138593Ssam	list_scan(s);
2997138593Ssam}
2998138593Ssam
2999161147Ssamstatic enum ieee80211_opmode get80211opmode(int s);
3000161147Ssam
3001173275Ssamstatic int
3002173275Ssamgettxseq(const struct ieee80211req_sta_info *si)
3003173275Ssam{
3004173275Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
3005173275Ssam
3006173275Ssam	int i, txseq;
3007173275Ssam
3008173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3009173275Ssam		return si->isi_txseqs[0];
3010173275Ssam	/* XXX not right but usually what folks want */
3011173275Ssam	txseq = 0;
3012173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3013173275Ssam		if (si->isi_txseqs[i] > txseq)
3014173275Ssam			txseq = si->isi_txseqs[i];
3015173275Ssam	return txseq;
3016173275Ssam#undef IEEE80211_NODE_QOS
3017173275Ssam}
3018173275Ssam
3019173275Ssamstatic int
3020173275Ssamgetrxseq(const struct ieee80211req_sta_info *si)
3021173275Ssam{
3022173275Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
3023173275Ssam
3024173275Ssam	int i, rxseq;
3025173275Ssam
3026173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3027173275Ssam		return si->isi_rxseqs[0];
3028173275Ssam	/* XXX not right but usually what folks want */
3029173275Ssam	rxseq = 0;
3030173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3031173275Ssam		if (si->isi_rxseqs[i] > rxseq)
3032173275Ssam			rxseq = si->isi_rxseqs[i];
3033173275Ssam	return rxseq;
3034173275Ssam#undef IEEE80211_NODE_QOS
3035173275Ssam}
3036173275Ssam
3037138593Ssamstatic void
3038138593Ssamlist_stations(int s)
3039138593Ssam{
3040161147Ssam	union {
3041161147Ssam		struct ieee80211req_sta_req req;
3042161147Ssam		uint8_t buf[24*1024];
3043161147Ssam	} u;
3044161147Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
3045170531Ssam	const uint8_t *cp;
3046138593Ssam	int len;
3047138593Ssam
3048161147Ssam	/* broadcast address =>'s get all stations */
3049161147Ssam	(void) memset(u.req.is_u.macaddr, 0xff, IEEE80211_ADDR_LEN);
3050161147Ssam	if (opmode == IEEE80211_M_STA) {
3051161147Ssam		/*
3052161147Ssam		 * Get information about the associated AP.
3053161147Ssam		 */
3054173275Ssam		(void) get80211(s, IEEE80211_IOC_BSSID,
3055173275Ssam		    u.req.is_u.macaddr, IEEE80211_ADDR_LEN);
3056161147Ssam	}
3057173275Ssam	if (get80211len(s, IEEE80211_IOC_STA_INFO, &u, sizeof(u), &len) < 0)
3058138593Ssam		errx(1, "unable to get station information");
3059138593Ssam	if (len < sizeof(struct ieee80211req_sta_info))
3060138593Ssam		return;
3061138593Ssam
3062170531Ssam	getchaninfo(s);
3063170531Ssam
3064159885Ssam	printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %4s\n"
3065138593Ssam		, "ADDR"
3066138593Ssam		, "AID"
3067138593Ssam		, "CHAN"
3068138593Ssam		, "RATE"
3069138593Ssam		, "RSSI"
3070138593Ssam		, "IDLE"
3071138593Ssam		, "TXSEQ"
3072138593Ssam		, "RXSEQ"
3073138593Ssam		, "CAPS"
3074159885Ssam		, "FLAG"
3075138593Ssam	);
3076170531Ssam	cp = (const uint8_t *) u.req.info;
3077138593Ssam	do {
3078170531Ssam		const struct ieee80211req_sta_info *si;
3079138593Ssam
3080170531Ssam		si = (const struct ieee80211req_sta_info *) cp;
3081161147Ssam		if (si->isi_len < sizeof(*si))
3082161147Ssam			break;
3083170531Ssam		printf("%s %4u %4d %3dM %3.1f %4d %6d %6d %-4.4s %-4.4s"
3084138593Ssam			, ether_ntoa((const struct ether_addr*) si->isi_macaddr)
3085138593Ssam			, IEEE80211_AID(si->isi_associd)
3086166015Ssam			, ieee80211_mhz2ieee(si->isi_freq, si->isi_flags)
3087178354Ssam			, si->isi_txmbps/2
3088170531Ssam			, si->isi_rssi/2.
3089138593Ssam			, si->isi_inact
3090173275Ssam			, gettxseq(si)
3091173275Ssam			, getrxseq(si)
3092138593Ssam			, getcaps(si->isi_capinfo)
3093159885Ssam			, getflags(si->isi_state)
3094138593Ssam		);
3095170531Ssam		printies(cp + si->isi_ie_off, si->isi_ie_len, 24);
3096178354Ssam		printmimo(&si->isi_mimo);
3097138593Ssam		printf("\n");
3098138593Ssam		cp += si->isi_len, len -= si->isi_len;
3099138593Ssam	} while (len >= sizeof(struct ieee80211req_sta_info));
3100138593Ssam}
3101138593Ssam
3102170531Ssamstatic const char *
3103170531Ssamget_chaninfo(const struct ieee80211_channel *c, int precise,
3104170531Ssam	char buf[], size_t bsize)
3105138593Ssam{
3106138593Ssam	buf[0] = '\0';
3107138593Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
3108170531Ssam		strlcat(buf, " FHSS", bsize);
3109165570Ssam	if (IEEE80211_IS_CHAN_A(c)) {
3110165570Ssam		if (IEEE80211_IS_CHAN_HALF(c))
3111170531Ssam			strlcat(buf, " 11a/10Mhz", bsize);
3112165570Ssam		else if (IEEE80211_IS_CHAN_QUARTER(c))
3113170531Ssam			strlcat(buf, " 11a/5Mhz", bsize);
3114165570Ssam		else
3115170531Ssam			strlcat(buf, " 11a", bsize);
3116165570Ssam	}
3117166015Ssam	if (IEEE80211_IS_CHAN_ANYG(c)) {
3118166015Ssam		if (IEEE80211_IS_CHAN_HALF(c))
3119170531Ssam			strlcat(buf, " 11g/10Mhz", bsize);
3120166015Ssam		else if (IEEE80211_IS_CHAN_QUARTER(c))
3121170531Ssam			strlcat(buf, " 11g/5Mhz", bsize);
3122166015Ssam		else
3123170531Ssam			strlcat(buf, " 11g", bsize);
3124166015Ssam	} else if (IEEE80211_IS_CHAN_B(c))
3125170531Ssam		strlcat(buf, " 11b", bsize);
3126170531Ssam	if (IEEE80211_IS_CHAN_TURBO(c))
3127170531Ssam		strlcat(buf, " Turbo", bsize);
3128170531Ssam	if (precise) {
3129170531Ssam		if (IEEE80211_IS_CHAN_HT20(c))
3130170531Ssam			strlcat(buf, " ht/20", bsize);
3131170531Ssam		else if (IEEE80211_IS_CHAN_HT40D(c))
3132170531Ssam			strlcat(buf, " ht/40-", bsize);
3133170531Ssam		else if (IEEE80211_IS_CHAN_HT40U(c))
3134170531Ssam			strlcat(buf, " ht/40+", bsize);
3135170531Ssam	} else {
3136170531Ssam		if (IEEE80211_IS_CHAN_HT(c))
3137170531Ssam			strlcat(buf, " ht", bsize);
3138170531Ssam	}
3139170531Ssam	return buf;
3140170531Ssam}
3141170531Ssam
3142170531Ssamstatic void
3143173275Ssamprint_chaninfo(const struct ieee80211_channel *c, int verb)
3144170531Ssam{
3145170531Ssam	char buf[14];
3146170531Ssam
3147138593Ssam	printf("Channel %3u : %u%c Mhz%-14.14s",
3148165570Ssam		ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
3149170531Ssam		IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
3150173275Ssam		get_chaninfo(c, verb, buf, sizeof(buf)));
3151138593Ssam}
3152138593Ssam
3153138593Ssamstatic void
3154173275Ssamprint_channels(int s, const struct ieee80211req_chaninfo *chans,
3155173275Ssam	int allchans, int verb)
3156138593Ssam{
3157187801Ssam	struct ieee80211req_chaninfo *achans;
3158170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3159138593Ssam	const struct ieee80211_channel *c;
3160170531Ssam	int i, half;
3161138593Ssam
3162187801Ssam	achans = malloc(IEEE80211_CHANINFO_SPACE(chans));
3163187801Ssam	if (achans == NULL)
3164187801Ssam		errx(1, "no space for active channel list");
3165187801Ssam	achans->ic_nchans = 0;
3166170531Ssam	memset(reported, 0, sizeof(reported));
3167138593Ssam	if (!allchans) {
3168138593Ssam		struct ieee80211req_chanlist active;
3169138593Ssam
3170173275Ssam		if (get80211(s, IEEE80211_IOC_CHANLIST, &active, sizeof(active)) < 0)
3171138593Ssam			errx(1, "unable to get active channel list");
3172173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3173173275Ssam			c = &chans->ic_chans[i];
3174170531Ssam			if (!isset(active.ic_channels, c->ic_ieee))
3175170531Ssam				continue;
3176170531Ssam			/*
3177170531Ssam			 * Suppress compatible duplicates unless
3178170531Ssam			 * verbose.  The kernel gives us it's
3179170531Ssam			 * complete channel list which has separate
3180170531Ssam			 * entries for 11g/11b and 11a/turbo.
3181170531Ssam			 */
3182173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3183170531Ssam				/* XXX we assume duplicates are adjacent */
3184187801Ssam				achans->ic_chans[achans->ic_nchans-1] = *c;
3185170531Ssam			} else {
3186187801Ssam				achans->ic_chans[achans->ic_nchans++] = *c;
3187170531Ssam				setbit(reported, c->ic_ieee);
3188170531Ssam			}
3189138593Ssam		}
3190170531Ssam	} else {
3191173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3192173275Ssam			c = &chans->ic_chans[i];
3193170531Ssam			/* suppress duplicates as above */
3194173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3195170531Ssam				/* XXX we assume duplicates are adjacent */
3196187801Ssam				achans->ic_chans[achans->ic_nchans-1] = *c;
3197170531Ssam			} else {
3198187801Ssam				achans->ic_chans[achans->ic_nchans++] = *c;
3199170531Ssam				setbit(reported, c->ic_ieee);
3200170531Ssam			}
3201170531Ssam		}
3202170531Ssam	}
3203187801Ssam	half = achans->ic_nchans / 2;
3204187801Ssam	if (achans->ic_nchans % 2)
3205138593Ssam		half++;
3206170531Ssam
3207187801Ssam	for (i = 0; i < achans->ic_nchans / 2; i++) {
3208187801Ssam		print_chaninfo(&achans->ic_chans[i], verb);
3209187801Ssam		print_chaninfo(&achans->ic_chans[half+i], verb);
3210138593Ssam		printf("\n");
3211138593Ssam	}
3212187801Ssam	if (achans->ic_nchans % 2) {
3213187801Ssam		print_chaninfo(&achans->ic_chans[i], verb);
3214138593Ssam		printf("\n");
3215138593Ssam	}
3216187801Ssam	free(achans);
3217138593Ssam}
3218138593Ssam
3219138593Ssamstatic void
3220173275Ssamlist_channels(int s, int allchans)
3221173275Ssam{
3222173275Ssam	getchaninfo(s);
3223187801Ssam	print_channels(s, chaninfo, allchans, verbose);
3224173275Ssam}
3225173275Ssam
3226173275Ssamstatic void
3227170531Ssamprint_txpow(const struct ieee80211_channel *c)
3228170531Ssam{
3229170531Ssam	printf("Channel %3u : %u Mhz %3.1f reg %2d  ",
3230170531Ssam	    c->ic_ieee, c->ic_freq,
3231170531Ssam	    c->ic_maxpower/2., c->ic_maxregpower);
3232170531Ssam}
3233170531Ssam
3234170531Ssamstatic void
3235170531Ssamprint_txpow_verbose(const struct ieee80211_channel *c)
3236170531Ssam{
3237173275Ssam	print_chaninfo(c, 1);
3238170531Ssam	printf("min %4.1f dBm  max %3.1f dBm  reg %2d dBm",
3239170531Ssam	    c->ic_minpower/2., c->ic_maxpower/2., c->ic_maxregpower);
3240170531Ssam	/* indicate where regulatory cap limits power use */
3241170531Ssam	if (c->ic_maxpower > 2*c->ic_maxregpower)
3242170531Ssam		printf(" <");
3243170531Ssam}
3244170531Ssam
3245170531Ssamstatic void
3246170531Ssamlist_txpow(int s)
3247170531Ssam{
3248187801Ssam	struct ieee80211req_chaninfo *achans;
3249170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3250170531Ssam	struct ieee80211_channel *c, *prev;
3251170531Ssam	int i, half;
3252170531Ssam
3253170531Ssam	getchaninfo(s);
3254187801Ssam	achans = malloc(IEEE80211_CHANINFO_SPACE(chaninfo));
3255187801Ssam	if (achans == NULL)
3256187801Ssam		errx(1, "no space for active channel list");
3257187801Ssam	achans->ic_nchans = 0;
3258170531Ssam	memset(reported, 0, sizeof(reported));
3259187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
3260187801Ssam		c = &chaninfo->ic_chans[i];
3261170531Ssam		/* suppress duplicates as above */
3262170531Ssam		if (isset(reported, c->ic_ieee) && !verbose) {
3263170531Ssam			/* XXX we assume duplicates are adjacent */
3264187801Ssam			prev = &achans->ic_chans[achans->ic_nchans-1];
3265170531Ssam			/* display highest power on channel */
3266170531Ssam			if (c->ic_maxpower > prev->ic_maxpower)
3267170531Ssam				*prev = *c;
3268170531Ssam		} else {
3269187801Ssam			achans->ic_chans[achans->ic_nchans++] = *c;
3270170531Ssam			setbit(reported, c->ic_ieee);
3271170531Ssam		}
3272170531Ssam	}
3273170531Ssam	if (!verbose) {
3274187801Ssam		half = achans->ic_nchans / 2;
3275187801Ssam		if (achans->ic_nchans % 2)
3276170531Ssam			half++;
3277170531Ssam
3278187801Ssam		for (i = 0; i < achans->ic_nchans / 2; i++) {
3279187801Ssam			print_txpow(&achans->ic_chans[i]);
3280187801Ssam			print_txpow(&achans->ic_chans[half+i]);
3281170531Ssam			printf("\n");
3282170531Ssam		}
3283187801Ssam		if (achans->ic_nchans % 2) {
3284187801Ssam			print_txpow(&achans->ic_chans[i]);
3285170531Ssam			printf("\n");
3286170531Ssam		}
3287170531Ssam	} else {
3288187801Ssam		for (i = 0; i < achans->ic_nchans; i++) {
3289187801Ssam			print_txpow_verbose(&achans->ic_chans[i]);
3290170531Ssam			printf("\n");
3291170531Ssam		}
3292170531Ssam	}
3293187801Ssam	free(achans);
3294170531Ssam}
3295170531Ssam
3296170531Ssamstatic void
3297138593Ssamlist_keys(int s)
3298138593Ssam{
3299138593Ssam}
3300138593Ssam
3301138593Ssam#define	IEEE80211_C_BITS \
3302181102Ssam	"\20\1STA\7FF\10TURBOP\11IBSS\12PMGT" \
3303178354Ssam	"\13HOSTAP\14AHDEMO\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE" \
3304181102Ssam	"\21MONITOR\22DFS\30WPA1\31WPA2\32BURST\33WME\34WDS\36BGSCAN" \
3305186904Ssam	"\37TXFRAG\40TDMA"
3306138593Ssam
3307138593Ssamstatic void
3308138593Ssamlist_capabilities(int s)
3309138593Ssam{
3310187801Ssam	struct ieee80211_devcaps_req *dc;
3311138593Ssam
3312187801Ssam	dc = malloc(IEEE80211_DEVCAPS_SIZE(1));
3313187801Ssam	if (dc == NULL)
3314187801Ssam		errx(1, "no space for device capabilities");
3315187801Ssam	dc->dc_chaninfo.ic_nchans = 1;
3316187801Ssam	getdevcaps(s, dc);
3317187801Ssam	printb("drivercaps", dc->dc_drivercaps, IEEE80211_C_BITS);
3318187801Ssam	if (dc->dc_cryptocaps != 0 || verbose) {
3319178354Ssam		putchar('\n');
3320187801Ssam		printb("cryptocaps", dc->dc_cryptocaps, IEEE80211_CRYPTO_BITS);
3321178354Ssam	}
3322187801Ssam	if (dc->dc_htcaps != 0 || verbose) {
3323178354Ssam		putchar('\n');
3324187801Ssam		printb("htcaps", dc->dc_htcaps, IEEE80211_HTCAP_BITS);
3325178354Ssam	}
3326138593Ssam	putchar('\n');
3327187801Ssam	free(dc);
3328138593Ssam}
3329138593Ssam
3330173275Ssamstatic int
3331173275Ssamget80211wme(int s, int param, int ac, int *val)
3332173275Ssam{
3333173275Ssam	struct ieee80211req ireq;
3334173275Ssam
3335173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3336173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3337173275Ssam	ireq.i_type = param;
3338173275Ssam	ireq.i_len = ac;
3339173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3340173275Ssam		warn("cannot get WME parameter %d, ac %d%s",
3341173275Ssam		    param, ac & IEEE80211_WMEPARAM_VAL,
3342173275Ssam		    ac & IEEE80211_WMEPARAM_BSS ? " (BSS)" : "");
3343173275Ssam		return -1;
3344173275Ssam	}
3345173275Ssam	*val = ireq.i_val;
3346173275Ssam	return 0;
3347173275Ssam}
3348173275Ssam
3349138593Ssamstatic void
3350181199Ssamlist_wme_aci(int s, const char *tag, int ac)
3351138593Ssam{
3352181199Ssam	int val;
3353138593Ssam
3354181199Ssam	printf("\t%s", tag);
3355138593Ssam
3356181199Ssam	/* show WME BSS parameters */
3357181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMIN, ac, &val) != -1)
3358181199Ssam		printf(" cwmin %2u", val);
3359181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMAX, ac, &val) != -1)
3360181199Ssam		printf(" cwmax %2u", val);
3361181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_AIFS, ac, &val) != -1)
3362181199Ssam		printf(" aifs %2u", val);
3363181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_TXOPLIMIT, ac, &val) != -1)
3364181199Ssam		printf(" txopLimit %3u", val);
3365181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_ACM, ac, &val) != -1) {
3366181199Ssam		if (val)
3367181199Ssam			printf(" acm");
3368181199Ssam		else if (verbose)
3369181199Ssam			printf(" -acm");
3370181199Ssam	}
3371181199Ssam	/* !BSS only */
3372181199Ssam	if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3373181199Ssam		if (get80211wme(s, IEEE80211_IOC_WME_ACKPOLICY, ac, &val) != -1) {
3374181199Ssam			if (!val)
3375181199Ssam				printf(" -ack");
3376138593Ssam			else if (verbose)
3377181199Ssam				printf(" ack");
3378138593Ssam		}
3379181199Ssam	}
3380181199Ssam	printf("\n");
3381181199Ssam}
3382181199Ssam
3383181199Ssamstatic void
3384181199Ssamlist_wme(int s)
3385181199Ssam{
3386181199Ssam	static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
3387181199Ssam	int ac;
3388181199Ssam
3389181199Ssam	if (verbose) {
3390181199Ssam		/* display both BSS and local settings */
3391181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
3392181199Ssam	again:
3393181199Ssam			if (ac & IEEE80211_WMEPARAM_BSS)
3394181199Ssam				list_wme_aci(s, "     ", ac);
3395181199Ssam			else
3396181199Ssam				list_wme_aci(s, acnames[ac], ac);
3397181199Ssam			if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3398181199Ssam				ac |= IEEE80211_WMEPARAM_BSS;
3399181199Ssam				goto again;
3400181199Ssam			} else
3401181199Ssam				ac &= ~IEEE80211_WMEPARAM_BSS;
3402138593Ssam		}
3403181199Ssam	} else {
3404181199Ssam		/* display only channel settings */
3405181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++)
3406181199Ssam			list_wme_aci(s, acnames[ac], ac);
3407138593Ssam	}
3408138593Ssam}
3409138593Ssam
3410149029Ssamstatic void
3411178354Ssamlist_roam(int s)
3412178354Ssam{
3413178354Ssam	const struct ieee80211_roamparam *rp;
3414178354Ssam	int mode;
3415178354Ssam
3416178354Ssam	getroam(s);
3417178354Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_11NA; mode++) {
3418178354Ssam		rp = &roamparams.params[mode];
3419178354Ssam		if (rp->rssi == 0 && rp->rate == 0)
3420178354Ssam			continue;
3421178354Ssam		if (rp->rssi & 1)
3422178354Ssam			LINE_CHECK("roam:%-6.6s rssi %2u.5dBm rate %2u Mb/s",
3423178354Ssam			    modename[mode], rp->rssi/2, rp->rate/2);
3424178354Ssam		else
3425178354Ssam			LINE_CHECK("roam:%-6.6s rssi %4udBm rate %2u Mb/s",
3426178354Ssam			    modename[mode], rp->rssi/2, rp->rate/2);
3427178354Ssam	}
3428178354Ssam	for (; mode < IEEE80211_MODE_MAX; mode++) {
3429178354Ssam		rp = &roamparams.params[mode];
3430178354Ssam		if (rp->rssi == 0 && rp->rate == 0)
3431178354Ssam			continue;
3432178354Ssam		if (rp->rssi & 1)
3433178354Ssam			LINE_CHECK("roam:%-6.6s rssi %2u.5dBm  MCS %2u    ",
3434178354Ssam			    modename[mode], rp->rssi/2, rp->rate &~ 0x80);
3435178354Ssam		else
3436178354Ssam			LINE_CHECK("roam:%-6.6s rssi %4udBm  MCS %2u    ",
3437178354Ssam			    modename[mode], rp->rssi/2, rp->rate &~ 0x80);
3438178354Ssam	}
3439178354Ssam}
3440178354Ssam
3441178354Ssamstatic void
3442178354Ssamlist_txparams(int s)
3443178354Ssam{
3444178354Ssam	const struct ieee80211_txparam *tp;
3445178354Ssam	int mode;
3446178354Ssam
3447178354Ssam	gettxparams(s);
3448178354Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_11NA; mode++) {
3449178354Ssam		tp = &txparams.params[mode];
3450178354Ssam		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
3451178354Ssam			continue;
3452178354Ssam		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3453178354Ssam			LINE_CHECK("%-6.6s ucast NONE    mgmt %2u Mb/s "
3454178354Ssam			    "mcast %2u Mb/s maxretry %u",
3455178354Ssam			    modename[mode], tp->mgmtrate/2,
3456178354Ssam			    tp->mcastrate/2, tp->maxretry);
3457178354Ssam		else
3458178354Ssam			LINE_CHECK("%-6.6s ucast %2u Mb/s mgmt %2u Mb/s "
3459178354Ssam			    "mcast %2u Mb/s maxretry %u",
3460178354Ssam			    modename[mode], tp->ucastrate/2, tp->mgmtrate/2,
3461178354Ssam			    tp->mcastrate/2, tp->maxretry);
3462178354Ssam	}
3463178354Ssam	for (; mode < IEEE80211_MODE_MAX; mode++) {
3464178354Ssam		tp = &txparams.params[mode];
3465178354Ssam		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
3466178354Ssam			continue;
3467178354Ssam		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3468178354Ssam			LINE_CHECK("%-6.6s ucast NONE    mgmt %2u MCS  "
3469178354Ssam			    "mcast %2u MCS  maxretry %u",
3470178354Ssam			    modename[mode], tp->mgmtrate &~ 0x80,
3471178354Ssam			    tp->mcastrate &~ 0x80, tp->maxretry);
3472178354Ssam		else
3473178354Ssam			LINE_CHECK("%-6.6s ucast %2u MCS  mgmt %2u MCS  "
3474178354Ssam			    "mcast %2u MCS  maxretry %u",
3475178354Ssam			    modename[mode], tp->ucastrate &~ 0x80,
3476178354Ssam			    tp->mgmtrate &~ 0x80,
3477178354Ssam			    tp->mcastrate &~ 0x80, tp->maxretry);
3478178354Ssam	}
3479178354Ssam}
3480178354Ssam
3481178354Ssamstatic void
3482173275Ssamprintpolicy(int policy)
3483173275Ssam{
3484173275Ssam	switch (policy) {
3485173275Ssam	case IEEE80211_MACCMD_POLICY_OPEN:
3486173275Ssam		printf("policy: open\n");
3487173275Ssam		break;
3488173275Ssam	case IEEE80211_MACCMD_POLICY_ALLOW:
3489173275Ssam		printf("policy: allow\n");
3490173275Ssam		break;
3491173275Ssam	case IEEE80211_MACCMD_POLICY_DENY:
3492173275Ssam		printf("policy: deny\n");
3493173275Ssam		break;
3494178354Ssam	case IEEE80211_MACCMD_POLICY_RADIUS:
3495178354Ssam		printf("policy: radius\n");
3496178354Ssam		break;
3497173275Ssam	default:
3498173275Ssam		printf("policy: unknown (%u)\n", policy);
3499173275Ssam		break;
3500173275Ssam	}
3501173275Ssam}
3502173275Ssam
3503173275Ssamstatic void
3504149029Ssamlist_mac(int s)
3505149029Ssam{
3506149029Ssam	struct ieee80211req ireq;
3507149029Ssam	struct ieee80211req_maclist *acllist;
3508173275Ssam	int i, nacls, policy, len;
3509173275Ssam	uint8_t *data;
3510149029Ssam	char c;
3511149029Ssam
3512149029Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3513149029Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
3514149029Ssam	ireq.i_type = IEEE80211_IOC_MACCMD;
3515149029Ssam	ireq.i_val = IEEE80211_MACCMD_POLICY;
3516149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3517149029Ssam		if (errno == EINVAL) {
3518149029Ssam			printf("No acl policy loaded\n");
3519149029Ssam			return;
3520149029Ssam		}
3521149029Ssam		err(1, "unable to get mac policy");
3522149029Ssam	}
3523149029Ssam	policy = ireq.i_val;
3524149029Ssam	if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
3525149029Ssam		c = '*';
3526149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
3527149029Ssam		c = '+';
3528149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
3529149029Ssam		c = '-';
3530178354Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_RADIUS) {
3531178354Ssam		c = 'r';		/* NB: should never have entries */
3532149029Ssam	} else {
3533149029Ssam		printf("policy: unknown (%u)\n", policy);
3534149029Ssam		c = '?';
3535149029Ssam	}
3536173275Ssam	if (verbose || c == '?')
3537173275Ssam		printpolicy(policy);
3538173275Ssam
3539175952Ssam	ireq.i_val = IEEE80211_MACCMD_LIST;
3540175952Ssam	ireq.i_len = 0;
3541175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3542173275Ssam		err(1, "unable to get mac acl list size");
3543175952Ssam	if (ireq.i_len == 0) {		/* NB: no acls */
3544173275Ssam		if (!(verbose || c == '?'))
3545173275Ssam			printpolicy(policy);
3546173275Ssam		return;
3547173275Ssam	}
3548175952Ssam	len = ireq.i_len;
3549173275Ssam
3550173275Ssam	data = malloc(len);
3551173275Ssam	if (data == NULL)
3552173275Ssam		err(1, "out of memory for acl list");
3553173275Ssam
3554175952Ssam	ireq.i_data = data;
3555175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3556173275Ssam		err(1, "unable to get mac acl list");
3557173275Ssam	nacls = len / sizeof(*acllist);
3558173275Ssam	acllist = (struct ieee80211req_maclist *) data;
3559149029Ssam	for (i = 0; i < nacls; i++)
3560149029Ssam		printf("%c%s\n", c, ether_ntoa(
3561149029Ssam			(const struct ether_addr *) acllist[i].ml_macaddr));
3562173275Ssam	free(data);
3563149029Ssam}
3564149029Ssam
3565178354Ssamstatic void
3566178354Ssamprint_regdomain(const struct ieee80211_regdomain *reg, int verb)
3567178354Ssam{
3568178354Ssam	if ((reg->regdomain != 0 &&
3569178354Ssam	    reg->regdomain != reg->country) || verb) {
3570178354Ssam		const struct regdomain *rd =
3571178354Ssam		    lib80211_regdomain_findbysku(getregdata(), reg->regdomain);
3572178354Ssam		if (rd == NULL)
3573178354Ssam			LINE_CHECK("regdomain %d", reg->regdomain);
3574178354Ssam		else
3575178354Ssam			LINE_CHECK("regdomain %s", rd->name);
3576178354Ssam	}
3577178354Ssam	if (reg->country != 0 || verb) {
3578178354Ssam		const struct country *cc =
3579178354Ssam		    lib80211_country_findbycc(getregdata(), reg->country);
3580178354Ssam		if (cc == NULL)
3581178354Ssam			LINE_CHECK("country %d", reg->country);
3582178354Ssam		else
3583178354Ssam			LINE_CHECK("country %s", cc->isoname);
3584178354Ssam	}
3585178354Ssam	if (reg->location == 'I')
3586178354Ssam		LINE_CHECK("indoor");
3587178354Ssam	else if (reg->location == 'O')
3588178354Ssam		LINE_CHECK("outdoor");
3589178354Ssam	else if (verb)
3590178354Ssam		LINE_CHECK("anywhere");
3591178354Ssam	if (reg->ecm)
3592178354Ssam		LINE_CHECK("ecm");
3593178354Ssam	else if (verb)
3594178354Ssam		LINE_CHECK("-ecm");
3595178354Ssam}
3596178354Ssam
3597178354Ssamstatic void
3598178354Ssamlist_regdomain(int s, int channelsalso)
3599178354Ssam{
3600178354Ssam	getregdomain(s);
3601178354Ssam	if (channelsalso) {
3602178354Ssam		getchaninfo(s);
3603178354Ssam		spacer = ':';
3604178354Ssam		print_regdomain(&regdomain, 1);
3605178354Ssam		LINE_BREAK();
3606187801Ssam		print_channels(s, chaninfo, 1/*allchans*/, 1/*verbose*/);
3607178354Ssam	} else
3608178354Ssam		print_regdomain(&regdomain, verbose);
3609178354Ssam}
3610178354Ssam
3611138593Ssamstatic
3612138593SsamDECL_CMD_FUNC(set80211list, arg, d)
3613138593Ssam{
3614138593Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
3615138593Ssam
3616173275Ssam	LINE_INIT('\t');
3617173275Ssam
3618138593Ssam	if (iseq(arg, "sta"))
3619138593Ssam		list_stations(s);
3620138593Ssam	else if (iseq(arg, "scan") || iseq(arg, "ap"))
3621138593Ssam		list_scan(s);
3622138593Ssam	else if (iseq(arg, "chan") || iseq(arg, "freq"))
3623138593Ssam		list_channels(s, 1);
3624138593Ssam	else if (iseq(arg, "active"))
3625138593Ssam		list_channels(s, 0);
3626138593Ssam	else if (iseq(arg, "keys"))
3627138593Ssam		list_keys(s);
3628138593Ssam	else if (iseq(arg, "caps"))
3629138593Ssam		list_capabilities(s);
3630178354Ssam	else if (iseq(arg, "wme") || iseq(arg, "wmm"))
3631138593Ssam		list_wme(s);
3632149029Ssam	else if (iseq(arg, "mac"))
3633149029Ssam		list_mac(s);
3634170531Ssam	else if (iseq(arg, "txpow"))
3635170531Ssam		list_txpow(s);
3636178354Ssam	else if (iseq(arg, "roam"))
3637178354Ssam		list_roam(s);
3638178354Ssam	else if (iseq(arg, "txparam") || iseq(arg, "txparm"))
3639178354Ssam		list_txparams(s);
3640178354Ssam	else if (iseq(arg, "regdomain"))
3641178354Ssam		list_regdomain(s, 1);
3642178354Ssam	else if (iseq(arg, "countries"))
3643178354Ssam		list_countries();
3644138593Ssam	else
3645138593Ssam		errx(1, "Don't know how to list %s for %s", arg, name);
3646178354Ssam	LINE_BREAK();
3647138593Ssam#undef iseq
3648138593Ssam}
3649138593Ssam
3650138593Ssamstatic enum ieee80211_opmode
3651138593Ssamget80211opmode(int s)
3652138593Ssam{
3653138593Ssam	struct ifmediareq ifmr;
3654138593Ssam
3655138593Ssam	(void) memset(&ifmr, 0, sizeof(ifmr));
3656138593Ssam	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
3657138593Ssam
3658138593Ssam	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
3659186101Ssam		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) {
3660186101Ssam			if (ifmr.ifm_current & IFM_FLAG0)
3661186101Ssam				return IEEE80211_M_AHDEMO;
3662186101Ssam			else
3663186101Ssam				return IEEE80211_M_IBSS;
3664186101Ssam		}
3665138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
3666138593Ssam			return IEEE80211_M_HOSTAP;
3667138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
3668138593Ssam			return IEEE80211_M_MONITOR;
3669138593Ssam	}
3670138593Ssam	return IEEE80211_M_STA;
3671138593Ssam}
3672138593Ssam
3673138593Ssam#if 0
3674138593Ssamstatic void
3675138593Ssamprintcipher(int s, struct ieee80211req *ireq, int keylenop)
3676138593Ssam{
3677138593Ssam	switch (ireq->i_val) {
3678138593Ssam	case IEEE80211_CIPHER_WEP:
3679138593Ssam		ireq->i_type = keylenop;
3680138593Ssam		if (ioctl(s, SIOCG80211, ireq) != -1)
3681138593Ssam			printf("WEP-%s",
3682138593Ssam			    ireq->i_len <= 5 ? "40" :
3683138593Ssam			    ireq->i_len <= 13 ? "104" : "128");
3684138593Ssam		else
3685138593Ssam			printf("WEP");
3686138593Ssam		break;
3687138593Ssam	case IEEE80211_CIPHER_TKIP:
3688138593Ssam		printf("TKIP");
3689138593Ssam		break;
3690138593Ssam	case IEEE80211_CIPHER_AES_OCB:
3691138593Ssam		printf("AES-OCB");
3692138593Ssam		break;
3693138593Ssam	case IEEE80211_CIPHER_AES_CCM:
3694138593Ssam		printf("AES-CCM");
3695138593Ssam		break;
3696138593Ssam	case IEEE80211_CIPHER_CKIP:
3697138593Ssam		printf("CKIP");
3698138593Ssam		break;
3699138593Ssam	case IEEE80211_CIPHER_NONE:
3700138593Ssam		printf("NONE");
3701138593Ssam		break;
3702138593Ssam	default:
3703138593Ssam		printf("UNKNOWN (0x%x)", ireq->i_val);
3704138593Ssam		break;
3705138593Ssam	}
3706138593Ssam}
3707138593Ssam#endif
3708138593Ssam
3709155931Ssamstatic void
3710138593Ssamprintkey(const struct ieee80211req_key *ik)
3711138593Ssam{
3712138593Ssam	static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
3713138593Ssam	int keylen = ik->ik_keylen;
3714138593Ssam	int printcontents;
3715138593Ssam
3716148001Srwatson	printcontents = printkeys &&
3717138593Ssam		(memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
3718138593Ssam	if (printcontents)
3719138593Ssam		LINE_BREAK();
3720138593Ssam	switch (ik->ik_type) {
3721138593Ssam	case IEEE80211_CIPHER_WEP:
3722138593Ssam		/* compatibility */
3723155931Ssam		LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
3724138593Ssam		    keylen <= 5 ? "40-bit" :
3725138593Ssam		    keylen <= 13 ? "104-bit" : "128-bit");
3726138593Ssam		break;
3727138593Ssam	case IEEE80211_CIPHER_TKIP:
3728138593Ssam		if (keylen > 128/8)
3729138593Ssam			keylen -= 128/8;	/* ignore MIC for now */
3730155931Ssam		LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3731138593Ssam		break;
3732138593Ssam	case IEEE80211_CIPHER_AES_OCB:
3733155931Ssam		LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3734138593Ssam		break;
3735138593Ssam	case IEEE80211_CIPHER_AES_CCM:
3736155931Ssam		LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3737138593Ssam		break;
3738138593Ssam	case IEEE80211_CIPHER_CKIP:
3739155931Ssam		LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3740138593Ssam		break;
3741138593Ssam	case IEEE80211_CIPHER_NONE:
3742155931Ssam		LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3743138593Ssam		break;
3744138593Ssam	default:
3745155931Ssam		LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
3746138593Ssam			ik->ik_type, ik->ik_keyix+1, 8*keylen);
3747138593Ssam		break;
3748138593Ssam	}
3749138593Ssam	if (printcontents) {
3750138593Ssam		int i;
3751138593Ssam
3752138593Ssam		printf(" <");
3753138593Ssam		for (i = 0; i < keylen; i++)
3754138593Ssam			printf("%02x", ik->ik_keydata[i]);
3755138593Ssam		printf(">");
3756138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
3757138593Ssam		    (ik->ik_keyrsc != 0 || verbose))
3758146873Sjhb			printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
3759138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
3760138593Ssam		    (ik->ik_keytsc != 0 || verbose))
3761146873Sjhb			printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
3762138593Ssam		if (ik->ik_flags != 0 && verbose) {
3763138593Ssam			const char *sep = " ";
3764138593Ssam
3765138593Ssam			if (ik->ik_flags & IEEE80211_KEY_XMIT)
3766138593Ssam				printf("%stx", sep), sep = "+";
3767138593Ssam			if (ik->ik_flags & IEEE80211_KEY_RECV)
3768138593Ssam				printf("%srx", sep), sep = "+";
3769138593Ssam			if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
3770138593Ssam				printf("%sdef", sep), sep = "+";
3771138593Ssam		}
3772138593Ssam		LINE_BREAK();
3773138593Ssam	}
3774138593Ssam}
3775138593Ssam
3776138593Ssamstatic void
3777173275Ssamprintrate(const char *tag, int v, int defrate, int defmcs)
3778138593Ssam{
3779173275Ssam	if (v == 11)
3780173275Ssam		LINE_CHECK("%s 5.5", tag);
3781173275Ssam	else if (v & 0x80) {
3782173275Ssam		if (v != defmcs)
3783173275Ssam			LINE_CHECK("%s %d", tag, v &~ 0x80);
3784173275Ssam	} else {
3785173275Ssam		if (v != defrate)
3786173275Ssam			LINE_CHECK("%s %d", tag, v/2);
3787173275Ssam	}
3788173275Ssam}
3789173275Ssam
3790173275Ssamstatic int
3791173275Ssamgetssid(int s, int ix, void *data, size_t len, int *plen)
3792173275Ssam{
3793138593Ssam	struct ieee80211req ireq;
3794138593Ssam
3795138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3796138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3797173275Ssam	ireq.i_type = IEEE80211_IOC_SSID;
3798173275Ssam	ireq.i_val = ix;
3799173275Ssam	ireq.i_data = data;
3800173275Ssam	ireq.i_len = len;
3801173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3802173275Ssam		return -1;
3803173275Ssam	*plen = ireq.i_len;
3804173275Ssam	return 0;
3805173275Ssam}
380677218Sphk
3807173275Ssamstatic void
3808173275Ssamieee80211_status(int s)
3809173275Ssam{
3810173275Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
3811173275Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
3812173275Ssam	int i, num, wpa, wme, bgscan, bgscaninterval, val, len, wepmode;
3813173275Ssam	uint8_t data[32];
3814173275Ssam	const struct ieee80211_channel *c;
3815178354Ssam	const struct ieee80211_roamparam *rp;
3816178354Ssam	const struct ieee80211_txparam *tp;
3817173275Ssam
3818173275Ssam	if (getssid(s, -1, data, sizeof(data), &len) < 0) {
3819148686Sstefanf		/* If we can't get the SSID, this isn't an 802.11 device. */
382077218Sphk		return;
382177218Sphk	}
3822173275Ssam
3823173275Ssam	/*
3824173275Ssam	 * Invalidate cached state so printing status for multiple
3825173275Ssam	 * if's doesn't reuse the first interfaces' cached state.
3826173275Ssam	 */
3827173275Ssam	gotcurchan = 0;
3828178354Ssam	gotroam = 0;
3829178354Ssam	gottxparams = 0;
3830173275Ssam	gothtconf = 0;
3831178354Ssam	gotregdomain = 0;
3832173275Ssam
3833173275Ssam	if (get80211val(s, IEEE80211_IOC_NUMSSIDS, &num) < 0)
3834173275Ssam		num = 0;
3835138593Ssam	printf("\tssid ");
3836138593Ssam	if (num > 1) {
3837173275Ssam		for (i = 0; i < num; i++) {
3838173275Ssam			if (getssid(s, i, data, sizeof(data), &len) >= 0 && len > 0) {
3839173275Ssam				printf(" %d:", i + 1);
3840173275Ssam				print_string(data, len);
3841138593Ssam			}
384288748Sambrisko		}
3843138593Ssam	} else
3844173275Ssam		print_string(data, len);
384577218Sphk
3846173275Ssam	c = getcurchan(s);
3847170531Ssam	if (c->ic_freq != IEEE80211_CHAN_ANY) {
3848170531Ssam		char buf[14];
3849170531Ssam		printf(" channel %d (%u Mhz%s)", c->ic_ieee, c->ic_freq,
3850170531Ssam			get_chaninfo(c, 1, buf, sizeof(buf)));
3851138593Ssam	} else if (verbose)
3852138593Ssam		printf(" channel UNDEF");
3853138593Ssam
3854173275Ssam	if (get80211(s, IEEE80211_IOC_BSSID, data, IEEE80211_ADDR_LEN) >= 0 &&
3855173275Ssam	    (memcmp(data, zerobssid, sizeof(zerobssid)) != 0 || verbose))
3856173275Ssam		printf(" bssid %s", ether_ntoa((struct ether_addr *)data));
3857138593Ssam
3858173275Ssam	if (get80211len(s, IEEE80211_IOC_STATIONNAME, data, sizeof(data), &len) != -1) {
3859138593Ssam		printf("\n\tstationname ");
3860173275Ssam		print_string(data, len);
386177218Sphk	}
386277218Sphk
3863138593Ssam	spacer = ' ';		/* force first break */
3864138593Ssam	LINE_BREAK();
386577218Sphk
3866178354Ssam	list_regdomain(s, 0);
3867178354Ssam
3868173275Ssam	wpa = 0;
3869173275Ssam	if (get80211val(s, IEEE80211_IOC_AUTHMODE, &val) != -1) {
3870173275Ssam		switch (val) {
3871173275Ssam		case IEEE80211_AUTH_NONE:
3872173275Ssam			LINE_CHECK("authmode NONE");
3873173275Ssam			break;
3874173275Ssam		case IEEE80211_AUTH_OPEN:
3875173275Ssam			LINE_CHECK("authmode OPEN");
3876173275Ssam			break;
3877173275Ssam		case IEEE80211_AUTH_SHARED:
3878173275Ssam			LINE_CHECK("authmode SHARED");
3879173275Ssam			break;
3880173275Ssam		case IEEE80211_AUTH_8021X:
3881173275Ssam			LINE_CHECK("authmode 802.1x");
3882173275Ssam			break;
3883173275Ssam		case IEEE80211_AUTH_WPA:
3884173275Ssam			if (get80211val(s, IEEE80211_IOC_WPA, &wpa) < 0)
3885173275Ssam				wpa = 1;	/* default to WPA1 */
3886173275Ssam			switch (wpa) {
3887173275Ssam			case 2:
3888173275Ssam				LINE_CHECK("authmode WPA2/802.11i");
388977218Sphk				break;
3890173275Ssam			case 3:
3891173275Ssam				LINE_CHECK("authmode WPA1+WPA2/802.11i");
389277218Sphk				break;
3893127649Ssam			default:
3894173275Ssam				LINE_CHECK("authmode WPA");
3895127649Ssam				break;
3896173275Ssam			}
3897173275Ssam			break;
3898173275Ssam		case IEEE80211_AUTH_AUTO:
3899173275Ssam			LINE_CHECK("authmode AUTO");
3900173275Ssam			break;
3901173275Ssam		default:
3902173275Ssam			LINE_CHECK("authmode UNKNOWN (0x%x)", val);
3903173275Ssam			break;
3904127649Ssam		}
3905127649Ssam	}
3906127649Ssam
3907173275Ssam	if (wpa || verbose) {
3908178354Ssam		if (get80211val(s, IEEE80211_IOC_WPS, &val) != -1) {
3909178354Ssam			if (val)
3910178354Ssam				LINE_CHECK("wps");
3911178354Ssam			else if (verbose)
3912178354Ssam				LINE_CHECK("-wps");
3913178354Ssam		}
3914178354Ssam		if (get80211val(s, IEEE80211_IOC_TSN, &val) != -1) {
3915178354Ssam			if (val)
3916178354Ssam				LINE_CHECK("tsn");
3917178354Ssam			else if (verbose)
3918178354Ssam				LINE_CHECK("-tsn");
3919178354Ssam		}
3920173275Ssam		if (ioctl(s, IEEE80211_IOC_COUNTERMEASURES, &val) != -1) {
3921173275Ssam			if (val)
3922173275Ssam				LINE_CHECK("countermeasures");
3923173275Ssam			else if (verbose)
3924173275Ssam				LINE_CHECK("-countermeasures");
3925173275Ssam		}
3926178354Ssam#if 0
3927178354Ssam		/* XXX not interesting with WPA done in user space */
3928178354Ssam		ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
3929178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3930178354Ssam		}
3931178354Ssam
3932178354Ssam		ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
3933178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3934178354Ssam			LINE_CHECK("mcastcipher ");
3935178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
3936178354Ssam			spacer = ' ';
3937178354Ssam		}
3938178354Ssam
3939178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
3940178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3941178354Ssam			LINE_CHECK("ucastcipher ");
3942178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
3943178354Ssam		}
3944178354Ssam
3945178354Ssam		if (wpa & 2) {
3946178354Ssam			ireq.i_type = IEEE80211_IOC_RSNCAPS;
3947178354Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1) {
3948178354Ssam				LINE_CHECK("RSN caps 0x%x", ireq.i_val);
3949178354Ssam				spacer = ' ';
3950178354Ssam			}
3951178354Ssam		}
3952178354Ssam
3953178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
3954178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3955178354Ssam		}
3956178354Ssam#endif
3957173275Ssam	}
3958138593Ssam
3959173275Ssam	if (get80211val(s, IEEE80211_IOC_WEP, &wepmode) != -1 &&
3960173275Ssam	    wepmode != IEEE80211_WEP_NOSUP) {
3961173275Ssam		int firstkey;
3962173275Ssam
3963138718Ssam		switch (wepmode) {
3964173275Ssam		case IEEE80211_WEP_OFF:
3965173275Ssam			LINE_CHECK("privacy OFF");
3966173275Ssam			break;
3967173275Ssam		case IEEE80211_WEP_ON:
3968173275Ssam			LINE_CHECK("privacy ON");
3969173275Ssam			break;
3970173275Ssam		case IEEE80211_WEP_MIXED:
3971173275Ssam			LINE_CHECK("privacy MIXED");
3972173275Ssam			break;
3973173275Ssam		default:
3974173275Ssam			LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
3975173275Ssam			break;
397677218Sphk		}
397777218Sphk
397877218Sphk		/*
397977218Sphk		 * If we get here then we've got WEP support so we need
398077218Sphk		 * to print WEP status.
398191454Sbrooks		 */
398277218Sphk
3983173275Ssam		if (get80211val(s, IEEE80211_IOC_WEPTXKEY, &val) < 0) {
398477218Sphk			warn("WEP support, but no tx key!");
398577218Sphk			goto end;
398677218Sphk		}
3987173275Ssam		if (val != -1)
3988173275Ssam			LINE_CHECK("deftxkey %d", val+1);
3989138718Ssam		else if (wepmode != IEEE80211_WEP_OFF || verbose)
3990155931Ssam			LINE_CHECK("deftxkey UNDEF");
399177218Sphk
3992173275Ssam		if (get80211val(s, IEEE80211_IOC_NUMWEPKEYS, &num) < 0) {
399377218Sphk			warn("WEP support, but no NUMWEPKEYS support!");
399477218Sphk			goto end;
399577218Sphk		}
399677218Sphk
3997138593Ssam		firstkey = 1;
3998138593Ssam		for (i = 0; i < num; i++) {
3999138593Ssam			struct ieee80211req_key ik;
400077218Sphk
4001138593Ssam			memset(&ik, 0, sizeof(ik));
4002138593Ssam			ik.ik_keyix = i;
4003173275Ssam			if (get80211(s, IEEE80211_IOC_WPAKEY, &ik, sizeof(ik)) < 0) {
400477218Sphk				warn("WEP support, but can get keys!");
400577218Sphk				goto end;
400677218Sphk			}
4007138593Ssam			if (ik.ik_keylen != 0) {
4008138593Ssam				if (verbose)
4009138593Ssam					LINE_BREAK();
4010138593Ssam				printkey(&ik);
4011138593Ssam				firstkey = 0;
4012138593Ssam			}
4013138593Ssam		}
4014173275Ssamend:
4015173275Ssam		;
4016138593Ssam	}
4017138593Ssam
4018173275Ssam	if (get80211val(s, IEEE80211_IOC_POWERSAVE, &val) != -1 &&
4019173275Ssam	    val != IEEE80211_POWERSAVE_NOSUP ) {
4020173275Ssam		if (val != IEEE80211_POWERSAVE_OFF || verbose) {
4021173275Ssam			switch (val) {
4022173275Ssam			case IEEE80211_POWERSAVE_OFF:
4023173275Ssam				LINE_CHECK("powersavemode OFF");
4024173275Ssam				break;
4025173275Ssam			case IEEE80211_POWERSAVE_CAM:
4026173275Ssam				LINE_CHECK("powersavemode CAM");
4027173275Ssam				break;
4028173275Ssam			case IEEE80211_POWERSAVE_PSP:
4029173275Ssam				LINE_CHECK("powersavemode PSP");
4030173275Ssam				break;
4031173275Ssam			case IEEE80211_POWERSAVE_PSP_CAM:
4032173275Ssam				LINE_CHECK("powersavemode PSP-CAM");
4033173275Ssam				break;
4034138593Ssam			}
4035173275Ssam			if (get80211val(s, IEEE80211_IOC_POWERSAVESLEEP, &val) != -1)
4036173275Ssam				LINE_CHECK("powersavesleep %d", val);
4037138593Ssam		}
4038138593Ssam	}
4039138593Ssam
4040173275Ssam	if (get80211val(s, IEEE80211_IOC_TXPOWER, &val) != -1) {
4041173275Ssam		if (val & 1)
4042173275Ssam			LINE_CHECK("txpower %d.5", val/2);
4043173275Ssam		else
4044173275Ssam			LINE_CHECK("txpower %d", val/2);
4045173275Ssam	}
4046138593Ssam	if (verbose) {
4047173275Ssam		if (get80211val(s, IEEE80211_IOC_TXPOWMAX, &val) != -1)
4048173275Ssam			LINE_CHECK("txpowmax %.1f", val/2.);
4049138593Ssam	}
4050138593Ssam
4051178354Ssam	if (get80211val(s, IEEE80211_IOC_DOTD, &val) != -1) {
4052178354Ssam		if (val)
4053178354Ssam			LINE_CHECK("dotd");
4054178354Ssam		else if (verbose)
4055178354Ssam			LINE_CHECK("-dotd");
4056178354Ssam	}
4057178354Ssam
4058173275Ssam	if (get80211val(s, IEEE80211_IOC_RTSTHRESHOLD, &val) != -1) {
4059173275Ssam		if (val != IEEE80211_RTS_MAX || verbose)
4060173275Ssam			LINE_CHECK("rtsthreshold %d", val);
4061138593Ssam	}
4062138593Ssam
4063173275Ssam	if (get80211val(s, IEEE80211_IOC_FRAGTHRESHOLD, &val) != -1) {
4064173275Ssam		if (val != IEEE80211_FRAG_MAX || verbose)
4065173275Ssam			LINE_CHECK("fragthreshold %d", val);
4066170531Ssam	}
4067173275Ssam	if (opmode == IEEE80211_M_STA || verbose) {
4068173275Ssam		if (get80211val(s, IEEE80211_IOC_BMISSTHRESHOLD, &val) != -1) {
4069173275Ssam			if (val != IEEE80211_HWBMISS_MAX || verbose)
4070173275Ssam				LINE_CHECK("bmiss %d", val);
4071153354Ssam		}
4072153354Ssam	}
4073153354Ssam
4074178354Ssam	if (!verbose) {
4075178354Ssam		gettxparams(s);
4076178354Ssam		tp = &txparams.params[chan2mode(c)];
4077178354Ssam		printrate("ucastrate", tp->ucastrate,
4078178354Ssam		    IEEE80211_FIXED_RATE_NONE, IEEE80211_FIXED_RATE_NONE);
4079178354Ssam		printrate("mcastrate", tp->mcastrate, 2*1, 0x80|0);
4080178354Ssam		printrate("mgmtrate", tp->mgmtrate, 2*1, 0x80|0);
4081178354Ssam		if (tp->maxretry != 6)		/* XXX */
4082178354Ssam			LINE_CHECK("maxretry %d", tp->maxretry);
4083178354Ssam	} else {
4084178354Ssam		LINE_BREAK();
4085178354Ssam		list_txparams(s);
4086178354Ssam	}
4087170531Ssam
4088173275Ssam	bgscaninterval = -1;
4089173275Ssam	(void) get80211val(s, IEEE80211_IOC_BGSCAN_INTERVAL, &bgscaninterval);
4090173275Ssam
4091173275Ssam	if (get80211val(s, IEEE80211_IOC_SCANVALID, &val) != -1) {
4092173275Ssam		if (val != bgscaninterval || verbose)
4093173275Ssam			LINE_CHECK("scanvalid %u", val);
4094148416Ssam	}
4095148416Ssam
4096173275Ssam	bgscan = 0;
4097173275Ssam	if (get80211val(s, IEEE80211_IOC_BGSCAN, &bgscan) != -1) {
4098173275Ssam		if (bgscan)
4099170531Ssam			LINE_CHECK("bgscan");
4100170531Ssam		else if (verbose)
4101170531Ssam			LINE_CHECK("-bgscan");
4102160687Ssam	}
4103170531Ssam	if (bgscan || verbose) {
4104170531Ssam		if (bgscaninterval != -1)
4105170531Ssam			LINE_CHECK("bgscanintvl %u", bgscaninterval);
4106173275Ssam		if (get80211val(s, IEEE80211_IOC_BGSCAN_IDLE, &val) != -1)
4107173275Ssam			LINE_CHECK("bgscanidle %u", val);
4108178354Ssam		if (!verbose) {
4109178354Ssam			getroam(s);
4110178354Ssam			rp = &roamparams.params[chan2mode(c)];
4111178354Ssam			if (rp->rssi & 1)
4112178354Ssam				LINE_CHECK("roam:rssi %u.5", rp->rssi/2);
4113178354Ssam			else
4114178354Ssam				LINE_CHECK("roam:rssi %u", rp->rssi/2);
4115178354Ssam			LINE_CHECK("roam:rate %u", rp->rate/2);
4116178354Ssam		} else {
4117178354Ssam			LINE_BREAK();
4118178354Ssam			list_roam(s);
4119170531Ssam		}
4120170531Ssam	}
4121160687Ssam
4122165570Ssam	if (IEEE80211_IS_CHAN_ANYG(c) || verbose) {
4123173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREG, &val) != -1) {
4124173275Ssam			if (val)
4125155931Ssam				LINE_CHECK("pureg");
4126147795Ssam			else if (verbose)
4127155931Ssam				LINE_CHECK("-pureg");
4128147795Ssam		}
4129173275Ssam		if (get80211val(s, IEEE80211_IOC_PROTMODE, &val) != -1) {
4130173275Ssam			switch (val) {
4131173275Ssam			case IEEE80211_PROTMODE_OFF:
4132173275Ssam				LINE_CHECK("protmode OFF");
4133173275Ssam				break;
4134173275Ssam			case IEEE80211_PROTMODE_CTS:
4135173275Ssam				LINE_CHECK("protmode CTS");
4136173275Ssam				break;
4137173275Ssam			case IEEE80211_PROTMODE_RTSCTS:
4138173275Ssam				LINE_CHECK("protmode RTSCTS");
4139173275Ssam				break;
4140173275Ssam			default:
4141173275Ssam				LINE_CHECK("protmode UNKNOWN (0x%x)", val);
4142173275Ssam				break;
4143138593Ssam			}
4144138593Ssam		}
4145138593Ssam	}
4146138593Ssam
4147173275Ssam	if (IEEE80211_IS_CHAN_HT(c) || verbose) {
4148173275Ssam		gethtconf(s);
4149173275Ssam		switch (htconf & 3) {
4150173275Ssam		case 0:
4151173275Ssam		case 2:
4152173275Ssam			LINE_CHECK("-ht");
4153173275Ssam			break;
4154173275Ssam		case 1:
4155173275Ssam			LINE_CHECK("ht20");
4156173275Ssam			break;
4157173275Ssam		case 3:
4158173275Ssam			if (verbose)
4159173275Ssam				LINE_CHECK("ht");
4160173275Ssam			break;
4161173275Ssam		}
4162173275Ssam		if (get80211val(s, IEEE80211_IOC_HTCOMPAT, &val) != -1) {
4163173275Ssam			if (!val)
4164173275Ssam				LINE_CHECK("-htcompat");
4165173275Ssam			else if (verbose)
4166173275Ssam				LINE_CHECK("htcompat");
4167173275Ssam		}
4168173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU, &val) != -1) {
4169173275Ssam			switch (val) {
4170173275Ssam			case 0:
4171173275Ssam				LINE_CHECK("-ampdu");
4172173275Ssam				break;
4173173275Ssam			case 1:
4174173275Ssam				LINE_CHECK("ampdutx -ampdurx");
4175173275Ssam				break;
4176173275Ssam			case 2:
4177173275Ssam				LINE_CHECK("-ampdutx ampdurx");
4178173275Ssam				break;
4179173275Ssam			case 3:
4180173275Ssam				if (verbose)
4181173275Ssam					LINE_CHECK("ampdu");
4182173275Ssam				break;
4183173275Ssam			}
4184173275Ssam		}
4185173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_LIMIT, &val) != -1) {
4186173275Ssam			switch (val) {
4187173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_8K:
4188173275Ssam				LINE_CHECK("ampdulimit 8k");
4189173275Ssam				break;
4190173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_16K:
4191173275Ssam				LINE_CHECK("ampdulimit 16k");
4192173275Ssam				break;
4193173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_32K:
4194173275Ssam				LINE_CHECK("ampdulimit 32k");
4195173275Ssam				break;
4196173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_64K:
4197173275Ssam				LINE_CHECK("ampdulimit 64k");
4198173275Ssam				break;
4199173275Ssam			}
4200173275Ssam		}
4201173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_DENSITY, &val) != -1) {
4202173275Ssam			switch (val) {
4203173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_NA:
4204173275Ssam				if (verbose)
4205183260Ssam					LINE_CHECK("ampdudensity NA");
4206173275Ssam				break;
4207173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_025:
4208173275Ssam				LINE_CHECK("ampdudensity .25");
4209173275Ssam				break;
4210173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_05:
4211173275Ssam				LINE_CHECK("ampdudensity .5");
4212173275Ssam				break;
4213173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_1:
4214173275Ssam				LINE_CHECK("ampdudensity 1");
4215173275Ssam				break;
4216173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_2:
4217173275Ssam				LINE_CHECK("ampdudensity 2");
4218173275Ssam				break;
4219173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_4:
4220173275Ssam				LINE_CHECK("ampdudensity 4");
4221173275Ssam				break;
4222173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_8:
4223173275Ssam				LINE_CHECK("ampdudensity 8");
4224173275Ssam				break;
4225173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_16:
4226173275Ssam				LINE_CHECK("ampdudensity 16");
4227173275Ssam				break;
4228173275Ssam			}
4229173275Ssam		}
4230173275Ssam		if (get80211val(s, IEEE80211_IOC_AMSDU, &val) != -1) {
4231173275Ssam			switch (val) {
4232173275Ssam			case 0:
4233173275Ssam				LINE_CHECK("-amsdu");
4234173275Ssam				break;
4235173275Ssam			case 1:
4236173275Ssam				LINE_CHECK("amsdutx -amsdurx");
4237173275Ssam				break;
4238173275Ssam			case 2:
4239173275Ssam				LINE_CHECK("-amsdutx amsdurx");
4240173275Ssam				break;
4241173275Ssam			case 3:
4242173275Ssam				if (verbose)
4243173275Ssam					LINE_CHECK("amsdu");
4244173275Ssam				break;
4245173275Ssam			}
4246173275Ssam		}
4247173275Ssam		/* XXX amsdu limit */
4248173275Ssam		if (get80211val(s, IEEE80211_IOC_SHORTGI, &val) != -1) {
4249173275Ssam			if (val)
4250173275Ssam				LINE_CHECK("shortgi");
4251173275Ssam			else if (verbose)
4252173275Ssam				LINE_CHECK("-shortgi");
4253173275Ssam		}
4254173275Ssam		if (get80211val(s, IEEE80211_IOC_HTPROTMODE, &val) != -1) {
4255173275Ssam			if (val == IEEE80211_PROTMODE_OFF)
4256173275Ssam				LINE_CHECK("htprotmode OFF");
4257173275Ssam			else if (val != IEEE80211_PROTMODE_RTSCTS)
4258173275Ssam				LINE_CHECK("htprotmode UNKNOWN (0x%x)", val);
4259173275Ssam			else if (verbose)
4260173275Ssam				LINE_CHECK("htprotmode RTSCTS");
4261173275Ssam		}
4262173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREN, &val) != -1) {
4263173275Ssam			if (val)
4264173275Ssam				LINE_CHECK("puren");
4265173275Ssam			else if (verbose)
4266173275Ssam				LINE_CHECK("-puren");
4267173275Ssam		}
4268183261Ssam		if (get80211val(s, IEEE80211_IOC_SMPS, &val) != -1) {
4269183261Ssam			if (val == IEEE80211_HTCAP_SMPS_DYNAMIC)
4270183261Ssam				LINE_CHECK("smpsdyn");
4271183261Ssam			else if (val == IEEE80211_HTCAP_SMPS_ENA)
4272183261Ssam				LINE_CHECK("smps");
4273183261Ssam			else if (verbose)
4274183261Ssam				LINE_CHECK("-smps");
4275183261Ssam		}
4276183261Ssam		if (get80211val(s, IEEE80211_IOC_RIFS, &val) != -1) {
4277183261Ssam			if (val)
4278183261Ssam				LINE_CHECK("rifs");
4279183261Ssam			else if (verbose)
4280183261Ssam				LINE_CHECK("-rifs");
4281183261Ssam		}
4282173275Ssam	}
4283173275Ssam
4284173275Ssam	if (get80211val(s, IEEE80211_IOC_WME, &wme) != -1) {
4285138593Ssam		if (wme)
4286155931Ssam			LINE_CHECK("wme");
4287138593Ssam		else if (verbose)
4288155931Ssam			LINE_CHECK("-wme");
4289138593Ssam	} else
4290138593Ssam		wme = 0;
4291138593Ssam
4292173275Ssam	if (get80211val(s, IEEE80211_IOC_BURST, &val) != -1) {
4293173275Ssam		if (val)
4294155931Ssam			LINE_CHECK("burst");
4295153422Ssam		else if (verbose)
4296155931Ssam			LINE_CHECK("-burst");
4297153422Ssam	}
4298153422Ssam
4299173275Ssam	if (get80211val(s, IEEE80211_IOC_FF, &val) != -1) {
4300173275Ssam		if (val)
4301170531Ssam			LINE_CHECK("ff");
4302170531Ssam		else if (verbose)
4303170531Ssam			LINE_CHECK("-ff");
4304170531Ssam	}
4305173275Ssam	if (get80211val(s, IEEE80211_IOC_TURBOP, &val) != -1) {
4306173275Ssam		if (val)
4307170531Ssam			LINE_CHECK("dturbo");
4308170531Ssam		else if (verbose)
4309170531Ssam			LINE_CHECK("-dturbo");
4310170531Ssam	}
4311178354Ssam	if (get80211val(s, IEEE80211_IOC_DWDS, &val) != -1) {
4312178354Ssam		if (val)
4313178354Ssam			LINE_CHECK("dwds");
4314178354Ssam		else if (verbose)
4315178354Ssam			LINE_CHECK("-dwds");
4316178354Ssam	}
4317170531Ssam
4318138593Ssam	if (opmode == IEEE80211_M_HOSTAP) {
4319173275Ssam		if (get80211val(s, IEEE80211_IOC_HIDESSID, &val) != -1) {
4320173275Ssam			if (val)
4321168075Ssam				LINE_CHECK("hidessid");
4322138593Ssam			else if (verbose)
4323168075Ssam				LINE_CHECK("-hidessid");
4324138593Ssam		}
4325173275Ssam		if (get80211val(s, IEEE80211_IOC_APBRIDGE, &val) != -1) {
4326173275Ssam			if (!val)
4327155931Ssam				LINE_CHECK("-apbridge");
4328138593Ssam			else if (verbose)
4329155931Ssam				LINE_CHECK("apbridge");
4330138593Ssam		}
4331173275Ssam		if (get80211val(s, IEEE80211_IOC_DTIM_PERIOD, &val) != -1)
4332173275Ssam			LINE_CHECK("dtimperiod %u", val);
4333138593Ssam
4334173275Ssam		if (get80211val(s, IEEE80211_IOC_DOTH, &val) != -1) {
4335173275Ssam			if (!val)
4336170531Ssam				LINE_CHECK("-doth");
4337170531Ssam			else if (verbose)
4338170531Ssam				LINE_CHECK("doth");
4339170531Ssam		}
4340178354Ssam		if (get80211val(s, IEEE80211_IOC_DFS, &val) != -1) {
4341178354Ssam			if (!val)
4342178354Ssam				LINE_CHECK("-dfs");
4343178354Ssam			else if (verbose)
4344178354Ssam				LINE_CHECK("dfs");
4345178354Ssam		}
4346173275Ssam		if (get80211val(s, IEEE80211_IOC_INACTIVITY, &val) != -1) {
4347173275Ssam			if (!val)
4348173275Ssam				LINE_CHECK("-inact");
4349173275Ssam			else if (verbose)
4350173275Ssam				LINE_CHECK("inact");
4351173275Ssam		}
4352138593Ssam	} else {
4353173275Ssam		if (get80211val(s, IEEE80211_IOC_ROAMING, &val) != -1) {
4354173275Ssam			if (val != IEEE80211_ROAMING_AUTO || verbose) {
4355173275Ssam				switch (val) {
4356138593Ssam				case IEEE80211_ROAMING_DEVICE:
4357155931Ssam					LINE_CHECK("roaming DEVICE");
4358138593Ssam					break;
4359138593Ssam				case IEEE80211_ROAMING_AUTO:
4360155931Ssam					LINE_CHECK("roaming AUTO");
4361138593Ssam					break;
4362138593Ssam				case IEEE80211_ROAMING_MANUAL:
4363155931Ssam					LINE_CHECK("roaming MANUAL");
4364138593Ssam					break;
4365138593Ssam				default:
4366155931Ssam					LINE_CHECK("roaming UNKNOWN (0x%x)",
4367173275Ssam						val);
4368138593Ssam					break;
4369138593Ssam				}
4370138593Ssam			}
4371138593Ssam		}
4372138593Ssam	}
4373186101Ssam
4374186904Ssam	if (opmode == IEEE80211_M_AHDEMO) {
4375186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOT, &val) != -1)
4376186904Ssam			LINE_CHECK("tdmaslot %u", val);
4377186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTCNT, &val) != -1)
4378186904Ssam			LINE_CHECK("tdmaslotcnt %u", val);
4379186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTLEN, &val) != -1)
4380186904Ssam			LINE_CHECK("tdmaslotlen %u", val);
4381186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_BINTERVAL, &val) != -1)
4382186904Ssam			LINE_CHECK("tdmabintval %u", val);
4383186904Ssam	} else if (get80211val(s, IEEE80211_IOC_BEACON_INTERVAL, &val) != -1) {
4384173275Ssam		/* XXX default define not visible */
4385173275Ssam		if (val != 100 || verbose)
4386173275Ssam			LINE_CHECK("bintval %u", val);
4387138593Ssam	}
4388138593Ssam
4389138593Ssam	if (wme && verbose) {
4390138593Ssam		LINE_BREAK();
4391138593Ssam		list_wme(s);
4392138593Ssam	}
4393173275Ssam	LINE_BREAK();
4394173275Ssam}
4395138593Ssam
4396173275Ssamstatic int
4397173275Ssamget80211(int s, int type, void *data, int len)
4398173275Ssam{
4399173275Ssam	struct ieee80211req ireq;
4400138593Ssam
4401173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4402173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4403173275Ssam	ireq.i_type = type;
4404173275Ssam	ireq.i_data = data;
4405173275Ssam	ireq.i_len = len;
4406173275Ssam	return ioctl(s, SIOCG80211, &ireq);
4407173275Ssam}
4408138593Ssam
4409173275Ssamstatic int
4410173275Ssamget80211len(int s, int type, void *data, int len, int *plen)
4411173275Ssam{
4412173275Ssam	struct ieee80211req ireq;
4413138593Ssam
4414173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4415173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4416173275Ssam	ireq.i_type = type;
4417173275Ssam	ireq.i_len = len;
4418187801Ssam	assert(ireq.i_len == len);	/* NB: check for 16-bit truncation */
4419173275Ssam	ireq.i_data = data;
4420173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4421173275Ssam		return -1;
4422173275Ssam	*plen = ireq.i_len;
4423173275Ssam	return 0;
4424173275Ssam}
4425138593Ssam
4426173275Ssamstatic int
4427173275Ssamget80211val(int s, int type, int *val)
4428173275Ssam{
4429173275Ssam	struct ieee80211req ireq;
443077218Sphk
4431173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4432173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4433173275Ssam	ireq.i_type = type;
4434173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4435173275Ssam		return -1;
4436173275Ssam	*val = ireq.i_val;
4437173275Ssam	return 0;
443877218Sphk}
443977218Sphk
444077218Sphkstatic void
4441170531Ssamset80211(int s, int type, int val, int len, void *data)
444277218Sphk{
444377218Sphk	struct ieee80211req	ireq;
444477218Sphk
444577218Sphk	(void) memset(&ireq, 0, sizeof(ireq));
444677218Sphk	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
444777218Sphk	ireq.i_type = type;
444877218Sphk	ireq.i_val = val;
444977218Sphk	ireq.i_len = len;
4450187801Ssam	assert(ireq.i_len == len);	/* NB: check for 16-bit truncation */
445177218Sphk	ireq.i_data = data;
445291454Sbrooks	if (ioctl(s, SIOCS80211, &ireq) < 0)
445377218Sphk		err(1, "SIOCS80211");
445477218Sphk}
445577218Sphk
445677218Sphkstatic const char *
445777218Sphkget_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
445877218Sphk{
445977218Sphk	int len;
446077218Sphk	int hexstr;
446177218Sphk	u_int8_t *p;
446277218Sphk
446377218Sphk	len = *lenp;
446477218Sphk	p = buf;
446577218Sphk	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
446677218Sphk	if (hexstr)
446777218Sphk		val += 2;
446877218Sphk	for (;;) {
446977218Sphk		if (*val == '\0')
447077218Sphk			break;
447177218Sphk		if (sep != NULL && strchr(sep, *val) != NULL) {
447277218Sphk			val++;
447377218Sphk			break;
447477218Sphk		}
447577218Sphk		if (hexstr) {
4476127831Sphk			if (!isxdigit((u_char)val[0])) {
447777218Sphk				warnx("bad hexadecimal digits");
447877218Sphk				return NULL;
447977218Sphk			}
4480127831Sphk			if (!isxdigit((u_char)val[1])) {
4481127831Sphk				warnx("odd count hexadecimal digits");
4482127831Sphk				return NULL;
4483127831Sphk			}
448477218Sphk		}
4485127831Sphk		if (p >= buf + len) {
448677218Sphk			if (hexstr)
448777218Sphk				warnx("hexadecimal digits too long");
448877218Sphk			else
4489127831Sphk				warnx("string too long");
449077218Sphk			return NULL;
449177218Sphk		}
449277218Sphk		if (hexstr) {
449377218Sphk#define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
449477218Sphk			*p++ = (tohex((u_char)val[0]) << 4) |
449577218Sphk			    tohex((u_char)val[1]);
449677218Sphk#undef tohex
449777218Sphk			val += 2;
449877218Sphk		} else
449977218Sphk			*p++ = *val++;
450077218Sphk	}
450177218Sphk	len = p - buf;
450277218Sphk	/* The string "-" is treated as the empty string. */
4503165045Ssam	if (!hexstr && len == 1 && buf[0] == '-') {
450477218Sphk		len = 0;
4505165045Ssam		memset(buf, 0, *lenp);
4506165045Ssam	} else if (len < *lenp)
450777218Sphk		memset(p, 0, *lenp - len);
450877218Sphk	*lenp = len;
450977218Sphk	return val;
451077218Sphk}
451177218Sphk
451277218Sphkstatic void
451377218Sphkprint_string(const u_int8_t *buf, int len)
451477218Sphk{
451577218Sphk	int i;
451677218Sphk	int hasspc;
451777218Sphk
451877218Sphk	i = 0;
451977218Sphk	hasspc = 0;
452091454Sbrooks	for (; i < len; i++) {
452177218Sphk		if (!isprint(buf[i]) && buf[i] != '\0')
452277218Sphk			break;
452377218Sphk		if (isspace(buf[i]))
452477218Sphk			hasspc++;
452577218Sphk	}
452677218Sphk	if (i == len) {
452777218Sphk		if (hasspc || len == 0 || buf[0] == '\0')
452877218Sphk			printf("\"%.*s\"", len, buf);
452977218Sphk		else
453077218Sphk			printf("%.*s", len, buf);
453177218Sphk	} else {
453277218Sphk		printf("0x");
453377218Sphk		for (i = 0; i < len; i++)
453477218Sphk			printf("%02x", buf[i]);
453577218Sphk	}
453677218Sphk}
453777218Sphk
4538178354Ssam/*
4539178354Ssam * Virtual AP cloning support.
4540178354Ssam */
4541178354Ssamstatic struct ieee80211_clone_params params = {
4542178354Ssam	.icp_opmode	= IEEE80211_M_STA,	/* default to station mode */
4543178354Ssam};
4544178354Ssam
4545178354Ssamstatic void
4546178354Ssamwlan_create(int s, struct ifreq *ifr)
4547178354Ssam{
4548178354Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
4549178354Ssam
4550178354Ssam	if (params.icp_parent[0] == '\0')
4551178354Ssam		errx(1, "must specify a parent when creating a wlan device");
4552178354Ssam	if (params.icp_opmode == IEEE80211_M_WDS &&
4553178354Ssam	    memcmp(params.icp_bssid, zerobssid, sizeof(zerobssid)) == 0)
4554178354Ssam		errx(1, "no bssid specified for WDS (use wlanbssid)");
4555178354Ssam	ifr->ifr_data = (caddr_t) &params;
4556178354Ssam	if (ioctl(s, SIOCIFCREATE2, ifr) < 0)
4557178354Ssam		err(1, "SIOCIFCREATE2");
4558178354Ssam}
4559178354Ssam
4560178354Ssamstatic
4561178354SsamDECL_CMD_FUNC(set80211clone_wlandev, arg, d)
4562178354Ssam{
4563178354Ssam	strlcpy(params.icp_parent, arg, IFNAMSIZ);
4564178354Ssam	clone_setcallback(wlan_create);
4565178354Ssam}
4566178354Ssam
4567178354Ssamstatic
4568178354SsamDECL_CMD_FUNC(set80211clone_wlanbssid, arg, d)
4569178354Ssam{
4570178354Ssam	const struct ether_addr *ea;
4571178354Ssam
4572178354Ssam	ea = ether_aton(arg);
4573178354Ssam	if (ea == NULL)
4574178354Ssam		errx(1, "%s: cannot parse bssid", arg);
4575178354Ssam	memcpy(params.icp_bssid, ea->octet, IEEE80211_ADDR_LEN);
4576178354Ssam	clone_setcallback(wlan_create);
4577178354Ssam}
4578178354Ssam
4579178354Ssamstatic
4580178354SsamDECL_CMD_FUNC(set80211clone_wlanaddr, arg, d)
4581178354Ssam{
4582178354Ssam	const struct ether_addr *ea;
4583178354Ssam
4584178354Ssam	ea = ether_aton(arg);
4585178354Ssam	if (ea == NULL)
4586178354Ssam		errx(1, "%s: cannot parse addres", arg);
4587178354Ssam	memcpy(params.icp_macaddr, ea->octet, IEEE80211_ADDR_LEN);
4588178354Ssam	params.icp_flags |= IEEE80211_CLONE_MACADDR;
4589178354Ssam	clone_setcallback(wlan_create);
4590178354Ssam}
4591178354Ssam
4592178354Ssamstatic
4593178354SsamDECL_CMD_FUNC(set80211clone_wlanmode, arg, d)
4594178354Ssam{
4595178354Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
4596178354Ssam	if (iseq(arg, "sta"))
4597178354Ssam		params.icp_opmode = IEEE80211_M_STA;
4598178354Ssam	else if (iseq(arg, "ahdemo") || iseq(arg, "adhoc-demo"))
4599178354Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
4600178354Ssam	else if (iseq(arg, "ibss") || iseq(arg, "adhoc"))
4601178354Ssam		params.icp_opmode = IEEE80211_M_IBSS;
4602178354Ssam	else if (iseq(arg, "ap") || iseq(arg, "host"))
4603178354Ssam		params.icp_opmode = IEEE80211_M_HOSTAP;
4604178354Ssam	else if (iseq(arg, "wds"))
4605178354Ssam		params.icp_opmode = IEEE80211_M_WDS;
4606178354Ssam	else if (iseq(arg, "monitor"))
4607178354Ssam		params.icp_opmode = IEEE80211_M_MONITOR;
4608186904Ssam	else if (iseq(arg, "tdma")) {
4609186904Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
4610186904Ssam		params.icp_flags |= IEEE80211_CLONE_TDMA;
4611186904Ssam	} else
4612178354Ssam		errx(1, "Don't know to create %s for %s", arg, name);
4613178354Ssam	clone_setcallback(wlan_create);
4614178354Ssam#undef iseq
4615178354Ssam}
4616178354Ssam
4617178354Ssamstatic void
4618178354Ssamset80211clone_beacons(const char *val, int d, int s, const struct afswtch *rafp)
4619178354Ssam{
4620178354Ssam	/* NB: inverted sense */
4621178354Ssam	if (d)
4622178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_NOBEACONS;
4623178354Ssam	else
4624178354Ssam		params.icp_flags |= IEEE80211_CLONE_NOBEACONS;
4625178354Ssam	clone_setcallback(wlan_create);
4626178354Ssam}
4627178354Ssam
4628178354Ssamstatic void
4629178354Ssamset80211clone_bssid(const char *val, int d, int s, const struct afswtch *rafp)
4630178354Ssam{
4631178354Ssam	if (d)
4632178354Ssam		params.icp_flags |= IEEE80211_CLONE_BSSID;
4633178354Ssam	else
4634178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_BSSID;
4635178354Ssam	clone_setcallback(wlan_create);
4636178354Ssam}
4637178354Ssam
4638178354Ssamstatic void
4639178354Ssamset80211clone_wdslegacy(const char *val, int d, int s, const struct afswtch *rafp)
4640178354Ssam{
4641178354Ssam	if (d)
4642178354Ssam		params.icp_flags |= IEEE80211_CLONE_WDSLEGACY;
4643178354Ssam	else
4644178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_WDSLEGACY;
4645178354Ssam	clone_setcallback(wlan_create);
4646178354Ssam}
4647178354Ssam
4648138593Ssamstatic struct cmd ieee80211_cmds[] = {
4649138593Ssam	DEF_CMD_ARG("ssid",		set80211ssid),
4650138593Ssam	DEF_CMD_ARG("nwid",		set80211ssid),
4651138593Ssam	DEF_CMD_ARG("stationname",	set80211stationname),
4652138593Ssam	DEF_CMD_ARG("station",		set80211stationname),	/* BSD/OS */
4653138593Ssam	DEF_CMD_ARG("channel",		set80211channel),
4654138593Ssam	DEF_CMD_ARG("authmode",		set80211authmode),
4655138593Ssam	DEF_CMD_ARG("powersavemode",	set80211powersavemode),
4656138593Ssam	DEF_CMD("powersave",	1,	set80211powersave),
4657138593Ssam	DEF_CMD("-powersave",	0,	set80211powersave),
4658138593Ssam	DEF_CMD_ARG("powersavesleep", 	set80211powersavesleep),
4659138593Ssam	DEF_CMD_ARG("wepmode",		set80211wepmode),
4660138593Ssam	DEF_CMD("wep",		1,	set80211wep),
4661138593Ssam	DEF_CMD("-wep",		0,	set80211wep),
4662139493Ssam	DEF_CMD_ARG("deftxkey",		set80211weptxkey),
4663138593Ssam	DEF_CMD_ARG("weptxkey",		set80211weptxkey),
4664138593Ssam	DEF_CMD_ARG("wepkey",		set80211wepkey),
4665138593Ssam	DEF_CMD_ARG("nwkey",		set80211nwkey),		/* NetBSD */
4666138593Ssam	DEF_CMD("-nwkey",	0,	set80211wep),		/* NetBSD */
4667138593Ssam	DEF_CMD_ARG("rtsthreshold",	set80211rtsthreshold),
4668138593Ssam	DEF_CMD_ARG("protmode",		set80211protmode),
4669138593Ssam	DEF_CMD_ARG("txpower",		set80211txpower),
4670138593Ssam	DEF_CMD_ARG("roaming",		set80211roaming),
4671138593Ssam	DEF_CMD("wme",		1,	set80211wme),
4672138593Ssam	DEF_CMD("-wme",		0,	set80211wme),
4673178354Ssam	DEF_CMD("wmm",		1,	set80211wme),
4674178354Ssam	DEF_CMD("-wmm",		0,	set80211wme),
4675138593Ssam	DEF_CMD("hidessid",	1,	set80211hidessid),
4676138593Ssam	DEF_CMD("-hidessid",	0,	set80211hidessid),
4677138593Ssam	DEF_CMD("apbridge",	1,	set80211apbridge),
4678138593Ssam	DEF_CMD("-apbridge",	0,	set80211apbridge),
4679138593Ssam	DEF_CMD_ARG("chanlist",		set80211chanlist),
4680138593Ssam	DEF_CMD_ARG("bssid",		set80211bssid),
4681138593Ssam	DEF_CMD_ARG("ap",		set80211bssid),
4682138593Ssam	DEF_CMD("scan",	0,		set80211scan),
4683138593Ssam	DEF_CMD_ARG("list",		set80211list),
4684138593Ssam	DEF_CMD_ARG2("cwmin",		set80211cwmin),
4685138593Ssam	DEF_CMD_ARG2("cwmax",		set80211cwmax),
4686138593Ssam	DEF_CMD_ARG2("aifs",		set80211aifs),
4687138593Ssam	DEF_CMD_ARG2("txoplimit",	set80211txoplimit),
4688148621Ssam	DEF_CMD_ARG("acm",		set80211acm),
4689148621Ssam	DEF_CMD_ARG("-acm",		set80211noacm),
4690148621Ssam	DEF_CMD_ARG("ack",		set80211ackpolicy),
4691148621Ssam	DEF_CMD_ARG("-ack",		set80211noackpolicy),
4692138593Ssam	DEF_CMD_ARG2("bss:cwmin",	set80211bsscwmin),
4693138593Ssam	DEF_CMD_ARG2("bss:cwmax",	set80211bsscwmax),
4694138593Ssam	DEF_CMD_ARG2("bss:aifs",	set80211bssaifs),
4695138593Ssam	DEF_CMD_ARG2("bss:txoplimit",	set80211bsstxoplimit),
4696138593Ssam	DEF_CMD_ARG("dtimperiod",	set80211dtimperiod),
4697138593Ssam	DEF_CMD_ARG("bintval",		set80211bintval),
4698138593Ssam	DEF_CMD("mac:open",	IEEE80211_MACCMD_POLICY_OPEN,	set80211maccmd),
4699138593Ssam	DEF_CMD("mac:allow",	IEEE80211_MACCMD_POLICY_ALLOW,	set80211maccmd),
4700138593Ssam	DEF_CMD("mac:deny",	IEEE80211_MACCMD_POLICY_DENY,	set80211maccmd),
4701178354Ssam	DEF_CMD("mac:radius",	IEEE80211_MACCMD_POLICY_RADIUS,	set80211maccmd),
4702138593Ssam	DEF_CMD("mac:flush",	IEEE80211_MACCMD_FLUSH,		set80211maccmd),
4703138593Ssam	DEF_CMD("mac:detach",	IEEE80211_MACCMD_DETACH,	set80211maccmd),
4704138593Ssam	DEF_CMD_ARG("mac:add",		set80211addmac),
4705138593Ssam	DEF_CMD_ARG("mac:del",		set80211delmac),
4706138593Ssam	DEF_CMD_ARG("mac:kick",		set80211kickmac),
4707147795Ssam	DEF_CMD("pureg",	1,	set80211pureg),
4708147795Ssam	DEF_CMD("-pureg",	0,	set80211pureg),
4709170531Ssam	DEF_CMD("ff",		1,	set80211fastframes),
4710170531Ssam	DEF_CMD("-ff",		0,	set80211fastframes),
4711170531Ssam	DEF_CMD("dturbo",	1,	set80211dturbo),
4712170531Ssam	DEF_CMD("-dturbo",	0,	set80211dturbo),
4713170531Ssam	DEF_CMD("bgscan",	1,	set80211bgscan),
4714170531Ssam	DEF_CMD("-bgscan",	0,	set80211bgscan),
4715170531Ssam	DEF_CMD_ARG("bgscanidle",	set80211bgscanidle),
4716170531Ssam	DEF_CMD_ARG("bgscanintvl",	set80211bgscanintvl),
4717170531Ssam	DEF_CMD_ARG("scanvalid",	set80211scanvalid),
4718178354Ssam	DEF_CMD_ARG("roam:rssi",	set80211roamrssi),
4719178354Ssam	DEF_CMD_ARG("roam:rate",	set80211roamrate),
4720153354Ssam	DEF_CMD_ARG("mcastrate",	set80211mcastrate),
4721178354Ssam	DEF_CMD_ARG("ucastrate",	set80211ucastrate),
4722178354Ssam	DEF_CMD_ARG("mgtrate",		set80211mgtrate),
4723178354Ssam	DEF_CMD_ARG("mgmtrate",		set80211mgtrate),
4724178354Ssam	DEF_CMD_ARG("maxretry",		set80211maxretry),
4725148416Ssam	DEF_CMD_ARG("fragthreshold",	set80211fragthreshold),
4726153422Ssam	DEF_CMD("burst",	1,	set80211burst),
4727153422Ssam	DEF_CMD("-burst",	0,	set80211burst),
4728160687Ssam	DEF_CMD_ARG("bmiss",		set80211bmissthreshold),
4729160687Ssam	DEF_CMD_ARG("bmissthreshold",	set80211bmissthreshold),
4730173275Ssam	DEF_CMD("shortgi",	1,	set80211shortgi),
4731173275Ssam	DEF_CMD("-shortgi",	0,	set80211shortgi),
4732173275Ssam	DEF_CMD("ampdurx",	2,	set80211ampdu),
4733173275Ssam	DEF_CMD("-ampdurx",	-2,	set80211ampdu),
4734173275Ssam	DEF_CMD("ampdutx",	1,	set80211ampdu),
4735173275Ssam	DEF_CMD("-ampdutx",	-1,	set80211ampdu),
4736173275Ssam	DEF_CMD("ampdu",	3,	set80211ampdu),		/* NB: tx+rx */
4737173275Ssam	DEF_CMD("-ampdu",	-3,	set80211ampdu),
4738173275Ssam	DEF_CMD_ARG("ampdulimit",	set80211ampdulimit),
4739173275Ssam	DEF_CMD_ARG("ampdudensity",	set80211ampdudensity),
4740173275Ssam	DEF_CMD("amsdurx",	2,	set80211amsdu),
4741173275Ssam	DEF_CMD("-amsdurx",	-2,	set80211amsdu),
4742173275Ssam	DEF_CMD("amsdutx",	1,	set80211amsdu),
4743173275Ssam	DEF_CMD("-amsdutx",	-1,	set80211amsdu),
4744173275Ssam	DEF_CMD("amsdu",	3,	set80211amsdu),		/* NB: tx+rx */
4745173275Ssam	DEF_CMD("-amsdu",	-3,	set80211amsdu),
4746173275Ssam	DEF_CMD_ARG("amsdulimit",	set80211amsdulimit),
4747173275Ssam	DEF_CMD("puren",	1,	set80211puren),
4748173275Ssam	DEF_CMD("-puren",	0,	set80211puren),
4749170531Ssam	DEF_CMD("doth",		1,	set80211doth),
4750170531Ssam	DEF_CMD("-doth",	0,	set80211doth),
4751178354Ssam	DEF_CMD("dfs",		1,	set80211dfs),
4752178354Ssam	DEF_CMD("-dfs",		0,	set80211dfs),
4753173275Ssam	DEF_CMD("htcompat",	1,	set80211htcompat),
4754173275Ssam	DEF_CMD("-htcompat",	0,	set80211htcompat),
4755178354Ssam	DEF_CMD("dwds",		1,	set80211dwds),
4756178354Ssam	DEF_CMD("-dwds",	0,	set80211dwds),
4757173275Ssam	DEF_CMD("inact",	1,	set80211inact),
4758173275Ssam	DEF_CMD("-inact",	0,	set80211inact),
4759178354Ssam	DEF_CMD("tsn",		1,	set80211tsn),
4760178354Ssam	DEF_CMD("-tsn",		0,	set80211tsn),
4761178354Ssam	DEF_CMD_ARG("regdomain",	set80211regdomain),
4762178354Ssam	DEF_CMD_ARG("country",		set80211country),
4763178354Ssam	DEF_CMD("indoor",	'I',	set80211location),
4764178354Ssam	DEF_CMD("-indoor",	'O',	set80211location),
4765178354Ssam	DEF_CMD("outdoor",	'O',	set80211location),
4766178354Ssam	DEF_CMD("-outdoor",	'I',	set80211location),
4767178354Ssam	DEF_CMD("anywhere",	' ',	set80211location),
4768178354Ssam	DEF_CMD("ecm",		1,	set80211ecm),
4769178354Ssam	DEF_CMD("-ecm",		0,	set80211ecm),
4770178354Ssam	DEF_CMD("dotd",		1,	set80211dotd),
4771178354Ssam	DEF_CMD("-dotd",	0,	set80211dotd),
4772173275Ssam	DEF_CMD_ARG("htprotmode",	set80211htprotmode),
4773173275Ssam	DEF_CMD("ht20",		1,	set80211htconf),
4774173275Ssam	DEF_CMD("-ht20",	0,	set80211htconf),
4775173275Ssam	DEF_CMD("ht40",		3,	set80211htconf),	/* NB: 20+40 */
4776173275Ssam	DEF_CMD("-ht40",	0,	set80211htconf),
4777173275Ssam	DEF_CMD("ht",		3,	set80211htconf),	/* NB: 20+40 */
4778173275Ssam	DEF_CMD("-ht",		0,	set80211htconf),
4779183261Ssam	DEF_CMD("rifs",		1,	set80211rifs),
4780183261Ssam	DEF_CMD("-rifs",	0,	set80211rifs),
4781183261Ssam	DEF_CMD("smps",		IEEE80211_HTCAP_SMPS_ENA,	set80211smps),
4782183261Ssam	DEF_CMD("smpsdyn",	IEEE80211_HTCAP_SMPS_DYNAMIC,	set80211smps),
4783183261Ssam	DEF_CMD("-smps",	IEEE80211_HTCAP_SMPS_OFF,	set80211smps),
4784178354Ssam	/* XXX for testing */
4785178354Ssam	DEF_CMD_ARG("chanswitch",	set80211chanswitch),
4786178354Ssam
4787186904Ssam	DEF_CMD_ARG("tdmaslot",		set80211tdmaslot),
4788186904Ssam	DEF_CMD_ARG("tdmaslotcnt",	set80211tdmaslotcnt),
4789186904Ssam	DEF_CMD_ARG("tdmaslotlen",	set80211tdmaslotlen),
4790186904Ssam	DEF_CMD_ARG("tdmabintval",	set80211tdmabintval),
4791186904Ssam
4792178354Ssam	/* vap cloning support */
4793178354Ssam	DEF_CLONE_CMD_ARG("wlanaddr",	set80211clone_wlanaddr),
4794178354Ssam	DEF_CLONE_CMD_ARG("wlanbssid",	set80211clone_wlanbssid),
4795178354Ssam	DEF_CLONE_CMD_ARG("wlandev",	set80211clone_wlandev),
4796178354Ssam	DEF_CLONE_CMD_ARG("wlanmode",	set80211clone_wlanmode),
4797178354Ssam	DEF_CLONE_CMD("beacons", 1,	set80211clone_beacons),
4798178354Ssam	DEF_CLONE_CMD("-beacons", 0,	set80211clone_beacons),
4799178354Ssam	DEF_CLONE_CMD("bssid",	1,	set80211clone_bssid),
4800178354Ssam	DEF_CLONE_CMD("-bssid",	0,	set80211clone_bssid),
4801178354Ssam	DEF_CLONE_CMD("wdslegacy", 1,	set80211clone_wdslegacy),
4802178354Ssam	DEF_CLONE_CMD("-wdslegacy", 0,	set80211clone_wdslegacy),
4803138593Ssam};
4804138593Ssamstatic struct afswtch af_ieee80211 = {
4805138593Ssam	.af_name	= "af_ieee80211",
4806138593Ssam	.af_af		= AF_UNSPEC,
4807139494Ssam	.af_other_status = ieee80211_status,
4808138593Ssam};
4809138593Ssam
4810138593Ssamstatic __constructor void
4811138593Ssamieee80211_ctor(void)
4812138593Ssam{
4813138593Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
4814138593Ssam	int i;
4815138593Ssam
4816138593Ssam	for (i = 0; i < N(ieee80211_cmds);  i++)
4817138593Ssam		cmd_register(&ieee80211_cmds[i]);
4818138593Ssam	af_register(&af_ieee80211);
4819138593Ssam#undef N
4820138593Ssam}
4821