ifieee80211.c revision 188038
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 188038 2009-02-03 00:15:19Z delphij $
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)
1026188038Sdelphij				errx(-1, "channel %u out of range, max %u",
1027187801Ssam					first, IEEE80211_CHAN_MAX);
1028138593Ssam			setbit(chanlist.ic_channels, first);
1029138593Ssam			break;
1030138593Ssam		case 2:
1031187801Ssam			if (first > IEEE80211_CHAN_MAX)
1032188038Sdelphij				errx(-1, "channel %u out of range, max %u",
1033187801Ssam					first, IEEE80211_CHAN_MAX);
1034187801Ssam			if (last > IEEE80211_CHAN_MAX)
1035188038Sdelphij				errx(-1, "channel %u out of range, max %u",
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;
1826187842Ssam		if (verbose) {
1827187842Ssam			printf("%s:", __func__);
1828187842Ssam			printb(" chanFlags", chanFlags, IEEE80211_CHAN_BITS);
1829187842Ssam			printb(" bandFlags", nb->flags | b->flags,
1830187842Ssam			    IEEE80211_CHAN_BITS);
1831187842Ssam			putchar('\n');
1832187842Ssam		}
1833178354Ssam		prev = NULL;
1834178354Ssam		for (freq = b->freqStart; freq <= b->freqEnd; freq += b->chanSep) {
1835178354Ssam			uint32_t flags = nb->flags | b->flags;
1836178354Ssam
1837178354Ssam			/* check if device can operate on this frequency */
1838187841Ssam			/*
1839187841Ssam			 * XXX GSM frequency mapping is handled in the kernel
1840187841Ssam			 * so we cannot find them in the calibration table;
1841187841Ssam			 * just construct the list and the kernel will reject
1842187841Ssam			 * if it's wrong.
1843187841Ssam			 */
1844187841Ssam			if (chanlookup(avail->ic_chans, avail->ic_nchans, freq, chanFlags) == NULL &&
1845187841Ssam			    (flags & IEEE80211_CHAN_GSM) == 0) {
1846187842Ssam				if (verbose) {
1847187842Ssam					printf("%u: skip, ", freq);
1848187842Ssam					printb("flags", chanFlags,
1849187842Ssam					    IEEE80211_CHAN_BITS);
1850187842Ssam					printf(" not available\n");
1851187842Ssam				}
1852178354Ssam				continue;
1853178354Ssam			}
1854187841Ssam			if ((flags & IEEE80211_CHAN_HALF) && !hasHalfChans) {
1855178354Ssam				if (verbose)
1856187841Ssam					printf("%u: skip, device does not "
1857187841Ssam					    "support half-rate channel\n",
1858187841Ssam					    freq);
1859178354Ssam				continue;
1860178354Ssam			}
1861178354Ssam			if ((flags & IEEE80211_CHAN_QUARTER) &&
1862187841Ssam			    !hasQuarterChans) {
1863178354Ssam				if (verbose)
1864187841Ssam					printf("%u: skip, device does not "
1865187841Ssam					    "support quarter-rate channel\n",
1866187841Ssam					    freq);
1867178354Ssam				continue;
1868178354Ssam			}
1869178354Ssam			if ((flags & IEEE80211_CHAN_HT20) &&
1870178354Ssam			    (chanFlags & IEEE80211_CHAN_HT20) == 0) {
1871178354Ssam				if (verbose)
1872187842Ssam					printf("%u: skip, device does not "
1873187842Ssam					    "support HT20 operation\n", freq);
1874178354Ssam				continue;
1875178354Ssam			}
1876178354Ssam			if ((flags & IEEE80211_CHAN_HT40) &&
1877178354Ssam			    (chanFlags & IEEE80211_CHAN_HT40) == 0) {
1878178354Ssam				if (verbose)
1879187842Ssam					printf("%u: skip, device does not "
1880187842Ssam					    "support HT40 operation\n", freq);
1881178354Ssam				continue;
1882178354Ssam			}
1883178354Ssam			if ((flags & REQ_ECM) && !reg->ecm) {
1884178354Ssam				if (verbose)
1885178354Ssam					printf("%u: skip, ECM channel\n", freq);
1886178354Ssam				continue;
1887178354Ssam			}
1888178354Ssam			if ((flags & REQ_OUTDOOR) && reg->location == 'I') {
1889178354Ssam				if (verbose)
1890178354Ssam					printf("%u: skip, outdoor channel\n", freq);
1891178354Ssam				continue;
1892178354Ssam			}
1893178354Ssam			if ((flags & IEEE80211_CHAN_HT40) &&
1894178354Ssam			    prev != NULL && (freq - prev->ic_freq) < channelSep) {
1895178354Ssam				if (verbose)
1896178354Ssam					printf("%u: skip, only %u channel "
1897178354Ssam					    "separation, need %d\n", freq,
1898178354Ssam					    freq - prev->ic_freq, channelSep);
1899178354Ssam				continue;
1900178354Ssam			}
1901178354Ssam			if (ci->ic_nchans == IEEE80211_CHAN_MAX) {
1902178354Ssam				if (verbose)
1903187842Ssam					printf("%u: skip, channel table full\n",
1904187842Ssam					    freq);
1905178354Ssam				break;
1906178354Ssam			}
1907178354Ssam			c = &ci->ic_chans[ci->ic_nchans++];
1908187801Ssam			memset(c, 0, sizeof(*c));
1909178354Ssam			c->ic_freq = freq;
1910178354Ssam			c->ic_flags = chanFlags |
1911178354Ssam			    (flags &~ (REQ_FLAGS | IEEE80211_CHAN_HT40));
1912178354Ssam			if (c->ic_flags & IEEE80211_CHAN_DFS)
1913178354Ssam				c->ic_maxregpower = nb->maxPowerDFS;
1914178354Ssam			else
1915178354Ssam				c->ic_maxregpower = nb->maxPower;
1916187842Ssam			if (verbose) {
1917187842Ssam				printf("[%3d] add freq %u ",
1918187842Ssam				    ci->ic_nchans-1, c->ic_freq);
1919187842Ssam				printb("flags", c->ic_flags, IEEE80211_CHAN_BITS);
1920187842Ssam				printf(" power %u\n", c->ic_maxregpower);
1921187842Ssam			}
1922178354Ssam			/* NB: kernel fills in other fields */
1923178354Ssam			prev = c;
1924178354Ssam		}
1925178354Ssam	}
1926178354Ssam}
1927178354Ssam
1928178354Ssamstatic void
1929178354Ssamregdomain_makechannels(
1930178354Ssam	struct ieee80211_regdomain_req *req,
1931178354Ssam	const struct ieee80211_devcaps_req *dc)
1932178354Ssam{
1933178354Ssam	struct regdata *rdp = getregdata();
1934178354Ssam	const struct country *cc;
1935178354Ssam	const struct ieee80211_regdomain *reg = &req->rd;
1936178354Ssam	struct ieee80211req_chaninfo *ci = &req->chaninfo;
1937178354Ssam	const struct regdomain *rd;
1938178354Ssam
1939178354Ssam	/*
1940178354Ssam	 * Locate construction table for new channel list.  We treat
1941178354Ssam	 * the regdomain/SKU as definitive so a country can be in
1942178354Ssam	 * multiple with different properties (e.g. US in FCC+FCC3).
1943178354Ssam	 * If no regdomain is specified then we fallback on the country
1944178354Ssam	 * code to find the associated regdomain since countries always
1945178354Ssam	 * belong to at least one regdomain.
1946178354Ssam	 */
1947178354Ssam	if (reg->regdomain == 0) {
1948178354Ssam		cc = lib80211_country_findbycc(rdp, reg->country);
1949178354Ssam		if (cc == NULL)
1950178354Ssam			errx(1, "internal error, country %d not found",
1951178354Ssam			    reg->country);
1952178354Ssam		rd = cc->rd;
1953178354Ssam	} else
1954178354Ssam		rd = lib80211_regdomain_findbysku(rdp, reg->regdomain);
1955178354Ssam	if (rd == NULL)
1956178354Ssam		errx(1, "internal error, regdomain %d not found",
1957178354Ssam			    reg->regdomain);
1958178354Ssam	if (rd->sku != SKU_DEBUG) {
1959187801Ssam		/*
1960187801Ssam		 * regdomain_addchans incrememnts the channel count for
1961187801Ssam		 * each channel it adds so initialize ic_nchans to zero.
1962187801Ssam		 * Note that we know we have enough space to hold all possible
1963187801Ssam		 * channels because the devcaps list size was used to
1964187801Ssam		 * allocate our request.
1965187801Ssam		 */
1966187801Ssam		ci->ic_nchans = 0;
1967178354Ssam		if (!LIST_EMPTY(&rd->bands_11b))
1968178354Ssam			regdomain_addchans(ci, &rd->bands_11b, reg,
1969178354Ssam			    IEEE80211_CHAN_B, &dc->dc_chaninfo);
1970187841Ssam		if (!LIST_EMPTY(&rd->bands_11g))
1971178354Ssam			regdomain_addchans(ci, &rd->bands_11g, reg,
1972178354Ssam			    IEEE80211_CHAN_G, &dc->dc_chaninfo);
1973187841Ssam		if (!LIST_EMPTY(&rd->bands_11a))
1974178354Ssam			regdomain_addchans(ci, &rd->bands_11a, reg,
1975178354Ssam			    IEEE80211_CHAN_A, &dc->dc_chaninfo);
1976178354Ssam		if (!LIST_EMPTY(&rd->bands_11na)) {
1977178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
1978178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT20,
1979178354Ssam			    &dc->dc_chaninfo);
1980178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
1981178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U,
1982178354Ssam			    &dc->dc_chaninfo);
1983178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
1984178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D,
1985178354Ssam			    &dc->dc_chaninfo);
1986178354Ssam		}
1987178354Ssam		if (!LIST_EMPTY(&rd->bands_11ng)) {
1988178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
1989178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT20,
1990178354Ssam			    &dc->dc_chaninfo);
1991178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
1992178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U,
1993178354Ssam			    &dc->dc_chaninfo);
1994178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
1995178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D,
1996178354Ssam			    &dc->dc_chaninfo);
1997178354Ssam		}
1998178354Ssam		qsort(ci->ic_chans, ci->ic_nchans, sizeof(ci->ic_chans[0]),
1999178354Ssam		    regdomain_sort);
2000178354Ssam	} else
2001187801Ssam		memcpy(ci, &dc->dc_chaninfo,
2002187801Ssam		    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
2003178354Ssam}
2004178354Ssam
2005178354Ssamstatic void
2006178354Ssamlist_countries(void)
2007178354Ssam{
2008178354Ssam	struct regdata *rdp = getregdata();
2009178354Ssam	const struct country *cp;
2010178354Ssam	const struct regdomain *dp;
2011178354Ssam	int i;
2012178354Ssam
2013178354Ssam	i = 0;
2014178354Ssam	printf("\nCountry codes:\n");
2015178354Ssam	LIST_FOREACH(cp, &rdp->countries, next) {
2016178354Ssam		printf("%2s %-15.15s%s", cp->isoname,
2017178354Ssam		    cp->name, ((i+1)%4) == 0 ? "\n" : " ");
2018178354Ssam		i++;
2019178354Ssam	}
2020178354Ssam	i = 0;
2021178354Ssam	printf("\nRegulatory domains:\n");
2022178354Ssam	LIST_FOREACH(dp, &rdp->domains, next) {
2023178354Ssam		printf("%-15.15s%s", dp->name, ((i+1)%4) == 0 ? "\n" : " ");
2024178354Ssam		i++;
2025178354Ssam	}
2026178354Ssam	printf("\n");
2027178354Ssam}
2028178354Ssam
2029178354Ssamstatic void
2030178354Ssamdefaultcountry(const struct regdomain *rd)
2031178354Ssam{
2032178354Ssam	struct regdata *rdp = getregdata();
2033178354Ssam	const struct country *cc;
2034178354Ssam
2035178354Ssam	cc = lib80211_country_findbycc(rdp, rd->cc->code);
2036178354Ssam	if (cc == NULL)
2037178354Ssam		errx(1, "internal error, ISO country code %d not "
2038178354Ssam		    "defined for regdomain %s", rd->cc->code, rd->name);
2039178354Ssam	regdomain.country = cc->code;
2040178354Ssam	regdomain.isocc[0] = cc->isoname[0];
2041178354Ssam	regdomain.isocc[1] = cc->isoname[1];
2042178354Ssam}
2043178354Ssam
2044178354Ssamstatic
2045178354SsamDECL_CMD_FUNC(set80211regdomain, val, d)
2046178354Ssam{
2047178354Ssam	struct regdata *rdp = getregdata();
2048178354Ssam	const struct regdomain *rd;
2049178354Ssam
2050178354Ssam	rd = lib80211_regdomain_findbyname(rdp, val);
2051178354Ssam	if (rd == NULL) {
2052186104Ssam		char *eptr;
2053186104Ssam		long sku = strtol(val, &eptr, 0);
2054186104Ssam
2055186104Ssam		if (eptr != val)
2056186104Ssam			rd = lib80211_regdomain_findbysku(rdp, sku);
2057186104Ssam		if (eptr == val || rd == NULL)
2058178354Ssam			errx(1, "unknown regdomain %s", val);
2059178354Ssam	}
2060178354Ssam	getregdomain(s);
2061178354Ssam	regdomain.regdomain = rd->sku;
2062178354Ssam	if (regdomain.country == 0 && rd->cc != NULL) {
2063178354Ssam		/*
2064178354Ssam		 * No country code setup and there's a default
2065178354Ssam		 * one for this regdomain fill it in.
2066178354Ssam		 */
2067178354Ssam		defaultcountry(rd);
2068178354Ssam	}
2069178354Ssam	callback_register(setregdomain_cb, &regdomain);
2070178354Ssam}
2071178354Ssam
2072178354Ssamstatic
2073178354SsamDECL_CMD_FUNC(set80211country, val, d)
2074178354Ssam{
2075178354Ssam	struct regdata *rdp = getregdata();
2076178354Ssam	const struct country *cc;
2077178354Ssam
2078178354Ssam	cc = lib80211_country_findbyname(rdp, val);
2079178354Ssam	if (cc == NULL) {
2080186103Ssam		char *eptr;
2081186103Ssam		long code = strtol(val, &eptr, 0);
2082186103Ssam
2083186103Ssam		if (eptr != val)
2084186103Ssam			cc = lib80211_country_findbycc(rdp, code);
2085186103Ssam		if (eptr == val || cc == NULL)
2086178354Ssam			errx(1, "unknown ISO country code %s", val);
2087178354Ssam	}
2088178354Ssam	getregdomain(s);
2089178354Ssam	regdomain.regdomain = cc->rd->sku;
2090178354Ssam	regdomain.country = cc->code;
2091178354Ssam	regdomain.isocc[0] = cc->isoname[0];
2092178354Ssam	regdomain.isocc[1] = cc->isoname[1];
2093178354Ssam	callback_register(setregdomain_cb, &regdomain);
2094178354Ssam}
2095178354Ssam
2096178354Ssamstatic void
2097178354Ssamset80211location(const char *val, int d, int s, const struct afswtch *rafp)
2098178354Ssam{
2099178354Ssam	getregdomain(s);
2100178354Ssam	regdomain.location = d;
2101178354Ssam	callback_register(setregdomain_cb, &regdomain);
2102178354Ssam}
2103178354Ssam
2104178354Ssamstatic void
2105178354Ssamset80211ecm(const char *val, int d, int s, const struct afswtch *rafp)
2106178354Ssam{
2107178354Ssam	getregdomain(s);
2108178354Ssam	regdomain.ecm = d;
2109178354Ssam	callback_register(setregdomain_cb, &regdomain);
2110178354Ssam}
2111178354Ssam
2112178354Ssamstatic void
2113173275SsamLINE_INIT(char c)
2114173275Ssam{
2115173275Ssam	spacer = c;
2116173275Ssam	if (c == '\t')
2117173275Ssam		col = 8;
2118173275Ssam	else
2119173275Ssam		col = 1;
2120173275Ssam}
2121173275Ssam
2122173275Ssamstatic void
2123173275SsamLINE_BREAK(void)
2124173275Ssam{
2125173275Ssam	if (spacer != '\t') {
2126173275Ssam		printf("\n");
2127173275Ssam		spacer = '\t';
2128173275Ssam	}
2129173275Ssam	col = 8;		/* 8-col tab */
2130173275Ssam}
2131173275Ssam
2132173275Ssamstatic void
2133173275SsamLINE_CHECK(const char *fmt, ...)
2134173275Ssam{
2135173275Ssam	char buf[80];
2136173275Ssam	va_list ap;
2137173275Ssam	int n;
2138173275Ssam
2139173275Ssam	va_start(ap, fmt);
2140173275Ssam	n = vsnprintf(buf+1, sizeof(buf)-1, fmt, ap);
2141173275Ssam	va_end(ap);
2142173275Ssam	col += 1+n;
2143173275Ssam	if (col > MAXCOL) {
2144173275Ssam		LINE_BREAK();
2145173275Ssam		col += n;
2146173275Ssam	}
2147173275Ssam	buf[0] = spacer;
2148173275Ssam	printf("%s", buf);
2149173275Ssam	spacer = ' ';
2150173275Ssam}
2151173275Ssam
2152138593Ssamstatic int
2153170531Ssamgetmaxrate(const uint8_t rates[15], uint8_t nrates)
2154138593Ssam{
2155138593Ssam	int i, maxrate = -1;
2156138593Ssam
2157138593Ssam	for (i = 0; i < nrates; i++) {
2158138593Ssam		int rate = rates[i] & IEEE80211_RATE_VAL;
2159138593Ssam		if (rate > maxrate)
2160138593Ssam			maxrate = rate;
2161138593Ssam	}
2162138593Ssam	return maxrate / 2;
2163138593Ssam}
2164138593Ssam
2165138593Ssamstatic const char *
2166138593Ssamgetcaps(int capinfo)
2167138593Ssam{
2168138593Ssam	static char capstring[32];
2169138593Ssam	char *cp = capstring;
2170138593Ssam
2171138593Ssam	if (capinfo & IEEE80211_CAPINFO_ESS)
2172138593Ssam		*cp++ = 'E';
2173138593Ssam	if (capinfo & IEEE80211_CAPINFO_IBSS)
2174138593Ssam		*cp++ = 'I';
2175138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
2176138593Ssam		*cp++ = 'c';
2177138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
2178138593Ssam		*cp++ = 'C';
2179138593Ssam	if (capinfo & IEEE80211_CAPINFO_PRIVACY)
2180138593Ssam		*cp++ = 'P';
2181138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
2182138593Ssam		*cp++ = 'S';
2183138593Ssam	if (capinfo & IEEE80211_CAPINFO_PBCC)
2184138593Ssam		*cp++ = 'B';
2185138593Ssam	if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
2186138593Ssam		*cp++ = 'A';
2187138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
2188138593Ssam		*cp++ = 's';
2189138593Ssam	if (capinfo & IEEE80211_CAPINFO_RSN)
2190138593Ssam		*cp++ = 'R';
2191138593Ssam	if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
2192138593Ssam		*cp++ = 'D';
2193138593Ssam	*cp = '\0';
2194138593Ssam	return capstring;
2195138593Ssam}
2196138593Ssam
2197159885Ssamstatic const char *
2198159885Ssamgetflags(int flags)
2199159885Ssam{
2200159885Ssam	static char flagstring[32];
2201159885Ssam	char *cp = flagstring;
2202159885Ssam
2203159885Ssam	if (flags & IEEE80211_NODE_AUTH)
2204159885Ssam		*cp++ = 'A';
2205159885Ssam	if (flags & IEEE80211_NODE_QOS)
2206159885Ssam		*cp++ = 'Q';
2207159885Ssam	if (flags & IEEE80211_NODE_ERP)
2208159885Ssam		*cp++ = 'E';
2209159885Ssam	if (flags & IEEE80211_NODE_PWR_MGT)
2210159885Ssam		*cp++ = 'P';
2211173275Ssam	if (flags & IEEE80211_NODE_HT) {
2212170531Ssam		*cp++ = 'H';
2213173275Ssam		if (flags & IEEE80211_NODE_HTCOMPAT)
2214173275Ssam			*cp++ = '+';
2215173275Ssam	}
2216173275Ssam	if (flags & IEEE80211_NODE_WPS)
2217173275Ssam		*cp++ = 'W';
2218173275Ssam	if (flags & IEEE80211_NODE_TSN)
2219183261Ssam		*cp++ = 'N';
2220183261Ssam	if (flags & IEEE80211_NODE_AMPDU_TX)
2221173275Ssam		*cp++ = 'T';
2222183261Ssam	if (flags & IEEE80211_NODE_AMPDU_RX)
2223183261Ssam		*cp++ = 'R';
2224183261Ssam	if (flags & IEEE80211_NODE_MIMO_PS) {
2225183261Ssam		*cp++ = 'M';
2226183261Ssam		if (flags & IEEE80211_NODE_MIMO_RTS)
2227183261Ssam			*cp++ = '+';
2228183261Ssam	}
2229183261Ssam	if (flags & IEEE80211_NODE_RIFS)
2230183261Ssam		*cp++ = 'I';
2231159885Ssam	*cp = '\0';
2232159885Ssam	return flagstring;
2233159885Ssam}
2234159885Ssam
2235138593Ssamstatic void
2236138593Ssamprintie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
2237138593Ssam{
2238138593Ssam	printf("%s", tag);
2239138593Ssam	if (verbose) {
2240138593Ssam		maxlen -= strlen(tag)+2;
2241138593Ssam		if (2*ielen > maxlen)
2242138593Ssam			maxlen--;
2243138593Ssam		printf("<");
2244138593Ssam		for (; ielen > 0; ie++, ielen--) {
2245138593Ssam			if (maxlen-- <= 0)
2246138593Ssam				break;
2247138593Ssam			printf("%02x", *ie);
2248138593Ssam		}
2249138593Ssam		if (ielen != 0)
2250138593Ssam			printf("-");
2251138593Ssam		printf(">");
2252138593Ssam	}
2253138593Ssam}
2254138593Ssam
2255170531Ssam#define LE_READ_2(p)					\
2256170531Ssam	((u_int16_t)					\
2257170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2258170531Ssam	  (((const u_int8_t *)(p))[1] <<  8)))
2259170531Ssam#define LE_READ_4(p)					\
2260170531Ssam	((u_int32_t)					\
2261170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2262170531Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
2263170531Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
2264170531Ssam	  (((const u_int8_t *)(p))[3] << 24)))
2265170531Ssam
2266138593Ssam/*
2267170531Ssam * NB: The decoding routines assume a properly formatted ie
2268170531Ssam *     which should be safe as the kernel only retains them
2269170531Ssam *     if they parse ok.
2270170531Ssam */
2271170531Ssam
2272170531Ssamstatic void
2273173275Ssamprintwmeparam(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2274170531Ssam{
2275170531Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
2276170531Ssam	static const char *acnames[] = { "BE", "BK", "VO", "VI" };
2277173275Ssam	const struct ieee80211_wme_param *wme =
2278173275Ssam	    (const struct ieee80211_wme_param *) ie;
2279170531Ssam	int i;
2280170531Ssam
2281170531Ssam	printf("%s", tag);
2282173275Ssam	if (!verbose)
2283173275Ssam		return;
2284173275Ssam	printf("<qosinfo 0x%x", wme->param_qosInfo);
2285173275Ssam	ie += offsetof(struct ieee80211_wme_param, params_acParams);
2286173275Ssam	for (i = 0; i < WME_NUM_AC; i++) {
2287173275Ssam		const struct ieee80211_wme_acparams *ac =
2288173275Ssam		    &wme->params_acParams[i];
2289173275Ssam
2290173275Ssam		printf(" %s[%saifsn %u cwmin %u cwmax %u txop %u]"
2291173275Ssam			, acnames[i]
2292173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_ACM) ? "acm " : ""
2293173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_AIFSN)
2294173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMIN)
2295173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMAX)
2296173275Ssam			, LE_READ_2(&ac->acp_txop)
2297173275Ssam		);
2298170531Ssam	}
2299173275Ssam	printf(">");
2300170531Ssam#undef MS
2301170531Ssam}
2302170531Ssam
2303170531Ssamstatic void
2304173275Ssamprintwmeinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2305173275Ssam{
2306173275Ssam	printf("%s", tag);
2307173275Ssam	if (verbose) {
2308173275Ssam		const struct ieee80211_wme_info *wme =
2309173275Ssam		    (const struct ieee80211_wme_info *) ie;
2310173275Ssam		printf("<version 0x%x info 0x%x>",
2311173275Ssam		    wme->wme_version, wme->wme_info);
2312173275Ssam	}
2313173275Ssam}
2314173275Ssam
2315173275Ssamstatic void
2316173275Ssamprinthtcap(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2317173275Ssam{
2318173275Ssam	printf("%s", tag);
2319173275Ssam	if (verbose) {
2320173275Ssam		const struct ieee80211_ie_htcap *htcap =
2321173275Ssam		    (const struct ieee80211_ie_htcap *) ie;
2322173275Ssam		const char *sep;
2323173275Ssam		int i, j;
2324173275Ssam
2325173275Ssam		printf("<cap 0x%x param 0x%x",
2326173275Ssam		    LE_READ_2(&htcap->hc_cap), htcap->hc_param);
2327173275Ssam		printf(" mcsset[");
2328173275Ssam		sep = "";
2329173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2330173275Ssam			if (isset(htcap->hc_mcsset, i)) {
2331173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2332173275Ssam					if (isclr(htcap->hc_mcsset, j))
2333173275Ssam						break;
2334173275Ssam				j--;
2335173275Ssam				if (i == j)
2336173275Ssam					printf("%s%u", sep, i);
2337173275Ssam				else
2338173275Ssam					printf("%s%u-%u", sep, i, j);
2339173275Ssam				i += j-i;
2340173275Ssam				sep = ",";
2341173275Ssam			}
2342173275Ssam		printf("] extcap 0x%x txbf 0x%x antenna 0x%x>",
2343173275Ssam		    LE_READ_2(&htcap->hc_extcap),
2344173275Ssam		    LE_READ_4(&htcap->hc_txbf),
2345173275Ssam		    htcap->hc_antenna);
2346173275Ssam	}
2347173275Ssam}
2348173275Ssam
2349173275Ssamstatic void
2350173275Ssamprinthtinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2351173275Ssam{
2352173275Ssam	printf("%s", tag);
2353173275Ssam	if (verbose) {
2354173275Ssam		const struct ieee80211_ie_htinfo *htinfo =
2355173275Ssam		    (const struct ieee80211_ie_htinfo *) ie;
2356173275Ssam		const char *sep;
2357173275Ssam		int i, j;
2358173275Ssam
2359173275Ssam		printf("<ctl %u, %x,%x,%x,%x", htinfo->hi_ctrlchannel,
2360173275Ssam		    htinfo->hi_byte1, htinfo->hi_byte2, htinfo->hi_byte3,
2361173275Ssam		    LE_READ_2(&htinfo->hi_byte45));
2362173275Ssam		printf(" basicmcs[");
2363173275Ssam		sep = "";
2364173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2365173275Ssam			if (isset(htinfo->hi_basicmcsset, i)) {
2366173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2367173275Ssam					if (isclr(htinfo->hi_basicmcsset, j))
2368173275Ssam						break;
2369173275Ssam				j--;
2370173275Ssam				if (i == j)
2371173275Ssam					printf("%s%u", sep, i);
2372173275Ssam				else
2373173275Ssam					printf("%s%u-%u", sep, i, j);
2374173275Ssam				i += j-i;
2375173275Ssam				sep = ",";
2376173275Ssam			}
2377173275Ssam		printf("]>");
2378173275Ssam	}
2379173275Ssam}
2380173275Ssam
2381173275Ssamstatic void
2382170531Ssamprintathie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2383170531Ssam{
2384170531Ssam
2385170531Ssam	printf("%s", tag);
2386170531Ssam	if (verbose) {
2387170531Ssam		const struct ieee80211_ath_ie *ath =
2388170531Ssam			(const struct ieee80211_ath_ie *)ie;
2389170531Ssam
2390170531Ssam		printf("<");
2391170531Ssam		if (ath->ath_capability & ATHEROS_CAP_TURBO_PRIME)
2392170531Ssam			printf("DTURBO,");
2393170531Ssam		if (ath->ath_capability & ATHEROS_CAP_COMPRESSION)
2394170531Ssam			printf("COMP,");
2395170531Ssam		if (ath->ath_capability & ATHEROS_CAP_FAST_FRAME)
2396170531Ssam			printf("FF,");
2397170531Ssam		if (ath->ath_capability & ATHEROS_CAP_XR)
2398170531Ssam			printf("XR,");
2399170531Ssam		if (ath->ath_capability & ATHEROS_CAP_AR)
2400170531Ssam			printf("AR,");
2401170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BURST)
2402170531Ssam			printf("BURST,");
2403170531Ssam		if (ath->ath_capability & ATHEROS_CAP_WME)
2404170531Ssam			printf("WME,");
2405170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BOOST)
2406170531Ssam			printf("BOOST,");
2407170531Ssam		printf("0x%x>", LE_READ_2(ath->ath_defkeyix));
2408170531Ssam	}
2409170531Ssam}
2410170531Ssam
2411170531Ssamstatic const char *
2412170531Ssamwpa_cipher(const u_int8_t *sel)
2413170531Ssam{
2414170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2415170531Ssam	u_int32_t w = LE_READ_4(sel);
2416170531Ssam
2417170531Ssam	switch (w) {
2418170531Ssam	case WPA_SEL(WPA_CSE_NULL):
2419170531Ssam		return "NONE";
2420170531Ssam	case WPA_SEL(WPA_CSE_WEP40):
2421170531Ssam		return "WEP40";
2422170531Ssam	case WPA_SEL(WPA_CSE_WEP104):
2423170531Ssam		return "WEP104";
2424170531Ssam	case WPA_SEL(WPA_CSE_TKIP):
2425170531Ssam		return "TKIP";
2426170531Ssam	case WPA_SEL(WPA_CSE_CCMP):
2427170531Ssam		return "AES-CCMP";
2428170531Ssam	}
2429170531Ssam	return "?";		/* NB: so 1<< is discarded */
2430170531Ssam#undef WPA_SEL
2431170531Ssam}
2432170531Ssam
2433170531Ssamstatic const char *
2434170531Ssamwpa_keymgmt(const u_int8_t *sel)
2435170531Ssam{
2436170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2437170531Ssam	u_int32_t w = LE_READ_4(sel);
2438170531Ssam
2439170531Ssam	switch (w) {
2440170531Ssam	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
2441170531Ssam		return "8021X-UNSPEC";
2442170531Ssam	case WPA_SEL(WPA_ASE_8021X_PSK):
2443170531Ssam		return "8021X-PSK";
2444170531Ssam	case WPA_SEL(WPA_ASE_NONE):
2445170531Ssam		return "NONE";
2446170531Ssam	}
2447170531Ssam	return "?";
2448170531Ssam#undef WPA_SEL
2449170531Ssam}
2450170531Ssam
2451170531Ssamstatic void
2452170531Ssamprintwpaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2453170531Ssam{
2454170531Ssam	u_int8_t len = ie[1];
2455170531Ssam
2456170531Ssam	printf("%s", tag);
2457170531Ssam	if (verbose) {
2458170531Ssam		const char *sep;
2459170531Ssam		int n;
2460170531Ssam
2461170531Ssam		ie += 6, len -= 4;		/* NB: len is payload only */
2462170531Ssam
2463170531Ssam		printf("<v%u", LE_READ_2(ie));
2464170531Ssam		ie += 2, len -= 2;
2465170531Ssam
2466170531Ssam		printf(" mc:%s", wpa_cipher(ie));
2467170531Ssam		ie += 4, len -= 4;
2468170531Ssam
2469170531Ssam		/* unicast ciphers */
2470170531Ssam		n = LE_READ_2(ie);
2471170531Ssam		ie += 2, len -= 2;
2472170531Ssam		sep = " uc:";
2473170531Ssam		for (; n > 0; n--) {
2474170531Ssam			printf("%s%s", sep, wpa_cipher(ie));
2475170531Ssam			ie += 4, len -= 4;
2476170531Ssam			sep = "+";
2477170531Ssam		}
2478170531Ssam
2479170531Ssam		/* key management algorithms */
2480170531Ssam		n = LE_READ_2(ie);
2481170531Ssam		ie += 2, len -= 2;
2482170531Ssam		sep = " km:";
2483170531Ssam		for (; n > 0; n--) {
2484170531Ssam			printf("%s%s", sep, wpa_keymgmt(ie));
2485170531Ssam			ie += 4, len -= 4;
2486170531Ssam			sep = "+";
2487170531Ssam		}
2488170531Ssam
2489170531Ssam		if (len > 2)		/* optional capabilities */
2490170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2491170531Ssam		printf(">");
2492170531Ssam	}
2493170531Ssam}
2494170531Ssam
2495170531Ssamstatic const char *
2496170531Ssamrsn_cipher(const u_int8_t *sel)
2497170531Ssam{
2498170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2499170531Ssam	u_int32_t w = LE_READ_4(sel);
2500170531Ssam
2501170531Ssam	switch (w) {
2502170531Ssam	case RSN_SEL(RSN_CSE_NULL):
2503170531Ssam		return "NONE";
2504170531Ssam	case RSN_SEL(RSN_CSE_WEP40):
2505170531Ssam		return "WEP40";
2506170531Ssam	case RSN_SEL(RSN_CSE_WEP104):
2507170531Ssam		return "WEP104";
2508170531Ssam	case RSN_SEL(RSN_CSE_TKIP):
2509170531Ssam		return "TKIP";
2510170531Ssam	case RSN_SEL(RSN_CSE_CCMP):
2511170531Ssam		return "AES-CCMP";
2512170531Ssam	case RSN_SEL(RSN_CSE_WRAP):
2513170531Ssam		return "AES-OCB";
2514170531Ssam	}
2515170531Ssam	return "?";
2516170531Ssam#undef WPA_SEL
2517170531Ssam}
2518170531Ssam
2519170531Ssamstatic const char *
2520170531Ssamrsn_keymgmt(const u_int8_t *sel)
2521170531Ssam{
2522170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2523170531Ssam	u_int32_t w = LE_READ_4(sel);
2524170531Ssam
2525170531Ssam	switch (w) {
2526170531Ssam	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
2527170531Ssam		return "8021X-UNSPEC";
2528170531Ssam	case RSN_SEL(RSN_ASE_8021X_PSK):
2529170531Ssam		return "8021X-PSK";
2530170531Ssam	case RSN_SEL(RSN_ASE_NONE):
2531170531Ssam		return "NONE";
2532170531Ssam	}
2533170531Ssam	return "?";
2534170531Ssam#undef RSN_SEL
2535170531Ssam}
2536170531Ssam
2537170531Ssamstatic void
2538170531Ssamprintrsnie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2539170531Ssam{
2540170531Ssam	printf("%s", tag);
2541170531Ssam	if (verbose) {
2542170531Ssam		const char *sep;
2543170531Ssam		int n;
2544170531Ssam
2545173275Ssam		ie += 2, ielen -= 2;
2546170531Ssam
2547170531Ssam		printf("<v%u", LE_READ_2(ie));
2548173275Ssam		ie += 2, ielen -= 2;
2549170531Ssam
2550170531Ssam		printf(" mc:%s", rsn_cipher(ie));
2551173275Ssam		ie += 4, ielen -= 4;
2552170531Ssam
2553170531Ssam		/* unicast ciphers */
2554170531Ssam		n = LE_READ_2(ie);
2555173275Ssam		ie += 2, ielen -= 2;
2556170531Ssam		sep = " uc:";
2557170531Ssam		for (; n > 0; n--) {
2558170531Ssam			printf("%s%s", sep, rsn_cipher(ie));
2559173275Ssam			ie += 4, ielen -= 4;
2560170531Ssam			sep = "+";
2561170531Ssam		}
2562170531Ssam
2563170531Ssam		/* key management algorithms */
2564170531Ssam		n = LE_READ_2(ie);
2565173275Ssam		ie += 2, ielen -= 2;
2566170531Ssam		sep = " km:";
2567170531Ssam		for (; n > 0; n--) {
2568170531Ssam			printf("%s%s", sep, rsn_keymgmt(ie));
2569173275Ssam			ie += 4, ielen -= 4;
2570170531Ssam			sep = "+";
2571170531Ssam		}
2572170531Ssam
2573173275Ssam		if (ielen > 2)		/* optional capabilities */
2574170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2575170531Ssam		/* XXXPMKID */
2576170531Ssam		printf(">");
2577170531Ssam	}
2578170531Ssam}
2579170531Ssam
2580181454Ssam/* XXX move to a public include file */
2581181454Ssam#define IEEE80211_WPS_DEV_PASS_ID	0x1012
2582181454Ssam#define IEEE80211_WPS_SELECTED_REG	0x1041
2583181454Ssam#define IEEE80211_WPS_SETUP_STATE	0x1044
2584181454Ssam#define IEEE80211_WPS_UUID_E		0x1047
2585181454Ssam#define IEEE80211_WPS_VERSION		0x104a
2586181454Ssam
2587181454Ssam#define BE_READ_2(p)					\
2588181454Ssam	((u_int16_t)					\
2589181454Ssam	 ((((const u_int8_t *)(p))[1]      ) |		\
2590181454Ssam	  (((const u_int8_t *)(p))[0] <<  8)))
2591181454Ssam
2592181454Ssamstatic void
2593181454Ssamprintwpsie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2594181454Ssam{
2595181454Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
2596181454Ssam	u_int8_t len = ie[1];
2597181454Ssam
2598181454Ssam	printf("%s", tag);
2599181454Ssam	if (verbose) {
2600181454Ssam		static const char *dev_pass_id[] = {
2601181454Ssam			"D",	/* Default (PIN) */
2602181454Ssam			"U",	/* User-specified */
2603181454Ssam			"M",	/* Machine-specified */
2604181454Ssam			"K",	/* Rekey */
2605181454Ssam			"P",	/* PushButton */
2606181454Ssam			"R"	/* Registrar-specified */
2607181454Ssam		};
2608181454Ssam		int n;
2609181454Ssam
2610181454Ssam		ie +=6, len -= 4;		/* NB: len is payload only */
2611181454Ssam
2612181454Ssam		/* WPS IE in Beacon and Probe Resp frames have different fields */
2613181454Ssam		printf("<");
2614181454Ssam		while (len) {
2615181454Ssam			uint16_t tlv_type = BE_READ_2(ie);
2616181454Ssam			uint16_t tlv_len  = BE_READ_2(ie + 2);
2617181454Ssam
2618181454Ssam			ie += 4, len -= 4;
2619181454Ssam
2620181454Ssam			switch (tlv_type) {
2621181454Ssam			case IEEE80211_WPS_VERSION:
2622181454Ssam				printf("v:%d.%d", *ie >> 4, *ie & 0xf);
2623181454Ssam				break;
2624181454Ssam			case IEEE80211_WPS_SETUP_STATE:
2625181454Ssam				/* Only 1 and 2 are valid */
2626181454Ssam				if (*ie == 0 || *ie >= 3)
2627181454Ssam					printf(" state:B");
2628181454Ssam				else
2629181454Ssam					printf(" st:%s", *ie == 1 ? "N" : "C");
2630181454Ssam				break;
2631181454Ssam			case IEEE80211_WPS_SELECTED_REG:
2632181454Ssam				printf(" sel:%s", *ie ? "T" : "F");
2633181454Ssam				break;
2634181454Ssam			case IEEE80211_WPS_DEV_PASS_ID:
2635181454Ssam				n = LE_READ_2(ie);
2636181454Ssam				if (n < N(dev_pass_id))
2637181454Ssam					printf(" dpi:%s", dev_pass_id[n]);
2638181454Ssam				break;
2639181454Ssam			case IEEE80211_WPS_UUID_E:
2640181454Ssam				printf(" uuid-e:");
2641181454Ssam				for (n = 0; n < (tlv_len - 1); n++)
2642181454Ssam					printf("%02x-", ie[n]);
2643181454Ssam				printf("%02x", ie[n]);
2644181454Ssam				break;
2645181454Ssam			}
2646181454Ssam			ie += tlv_len, len -= tlv_len;
2647181454Ssam		}
2648181454Ssam		printf(">");
2649181454Ssam	}
2650181454Ssam#undef N
2651181454Ssam}
2652181454Ssam
2653186904Ssamstatic void
2654186904Ssamprinttdmaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2655186904Ssam{
2656186904Ssam	printf("%s", tag);
2657186904Ssam	if (verbose && ielen >= sizeof(struct ieee80211_tdma_param)) {
2658186904Ssam		const struct ieee80211_tdma_param *tdma =
2659186904Ssam		   (const struct ieee80211_tdma_param *) ie;
2660186904Ssam
2661186904Ssam		/* XXX tstamp */
2662186904Ssam		printf("<v%u slot:%u slotcnt:%u slotlen:%u bintval:%u inuse:0x%x>",
2663186904Ssam		    tdma->tdma_version, tdma->tdma_slot, tdma->tdma_slotcnt,
2664186904Ssam		    LE_READ_2(&tdma->tdma_slotlen), tdma->tdma_bintval,
2665186904Ssam		    tdma->tdma_inuse[0]);
2666186904Ssam	}
2667186904Ssam}
2668186904Ssam
2669170531Ssam/*
2670138593Ssam * Copy the ssid string contents into buf, truncating to fit.  If the
2671138593Ssam * ssid is entirely printable then just copy intact.  Otherwise convert
2672138593Ssam * to hexadecimal.  If the result is truncated then replace the last
2673138593Ssam * three characters with "...".
2674138593Ssam */
2675146873Sjhbstatic int
2676138593Ssamcopy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
2677138593Ssam{
2678138593Ssam	const u_int8_t *p;
2679138593Ssam	size_t maxlen;
2680138593Ssam	int i;
2681138593Ssam
2682138593Ssam	if (essid_len > bufsize)
2683138593Ssam		maxlen = bufsize;
2684138593Ssam	else
2685138593Ssam		maxlen = essid_len;
2686138593Ssam	/* determine printable or not */
2687138593Ssam	for (i = 0, p = essid; i < maxlen; i++, p++) {
2688138593Ssam		if (*p < ' ' || *p > 0x7e)
2689138593Ssam			break;
2690138593Ssam	}
2691138593Ssam	if (i != maxlen) {		/* not printable, print as hex */
2692138593Ssam		if (bufsize < 3)
2693138593Ssam			return 0;
2694138593Ssam		strlcpy(buf, "0x", bufsize);
2695138593Ssam		bufsize -= 2;
2696138593Ssam		p = essid;
2697138593Ssam		for (i = 0; i < maxlen && bufsize >= 2; i++) {
2698147489Savatar			sprintf(&buf[2+2*i], "%02x", p[i]);
2699138593Ssam			bufsize -= 2;
2700138593Ssam		}
2701147489Savatar		if (i != essid_len)
2702147489Savatar			memcpy(&buf[2+2*i-3], "...", 3);
2703138593Ssam	} else {			/* printable, truncate as needed */
2704138593Ssam		memcpy(buf, essid, maxlen);
2705147489Savatar		if (maxlen != essid_len)
2706147489Savatar			memcpy(&buf[maxlen-3], "...", 3);
2707138593Ssam	}
2708138593Ssam	return maxlen;
2709138593Ssam}
2710138593Ssam
2711173275Ssamstatic void
2712173275Ssamprintssid(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2713173275Ssam{
2714173275Ssam	char ssid[2*IEEE80211_NWID_LEN+1];
2715173275Ssam
2716173275Ssam	printf("%s<%.*s>", tag, copy_essid(ssid, maxlen, ie+2, ie[1]), ssid);
2717173275Ssam}
2718173275Ssam
2719173275Ssamstatic void
2720173275Ssamprintrates(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2721173275Ssam{
2722173275Ssam	const char *sep;
2723173275Ssam	int i;
2724173275Ssam
2725173275Ssam	printf("%s", tag);
2726173275Ssam	sep = "<";
2727173275Ssam	for (i = 2; i < ielen; i++) {
2728173275Ssam		printf("%s%s%d", sep,
2729173275Ssam		    ie[i] & IEEE80211_RATE_BASIC ? "B" : "",
2730173275Ssam		    ie[i] & IEEE80211_RATE_VAL);
2731173275Ssam		sep = ",";
2732173275Ssam	}
2733173275Ssam	printf(">");
2734173275Ssam}
2735173275Ssam
2736173275Ssamstatic void
2737173275Ssamprintcountry(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2738173275Ssam{
2739173275Ssam	const struct ieee80211_country_ie *cie =
2740173275Ssam	   (const struct ieee80211_country_ie *) ie;
2741173275Ssam	int i, nbands, schan, nchan;
2742173275Ssam
2743173275Ssam	printf("%s<%c%c%c", tag, cie->cc[0], cie->cc[1], cie->cc[2]);
2744173275Ssam	nbands = (cie->len - 3) / sizeof(cie->band[0]);
2745173275Ssam	for (i = 0; i < nbands; i++) {
2746173275Ssam		schan = cie->band[i].schan;
2747173275Ssam		nchan = cie->band[i].nchan;
2748173275Ssam		if (nchan != 1)
2749173275Ssam			printf(" %u-%u,%u", schan, schan + nchan-1,
2750173275Ssam			    cie->band[i].maxtxpwr);
2751173275Ssam		else
2752173275Ssam			printf(" %u,%u", schan, cie->band[i].maxtxpwr);
2753173275Ssam	}
2754173275Ssam	printf(">");
2755173275Ssam}
2756173275Ssam
2757148686Sstefanf/* unaligned little endian access */
2758138593Ssam#define LE_READ_4(p)					\
2759138593Ssam	((u_int32_t)					\
2760138593Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2761138593Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
2762138593Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
2763138593Ssam	  (((const u_int8_t *)(p))[3] << 24)))
2764138593Ssam
2765178354Ssamstatic __inline int
2766138593Ssamiswpaoui(const u_int8_t *frm)
2767138593Ssam{
2768138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
2769138593Ssam}
2770138593Ssam
2771178354Ssamstatic __inline int
2772173275Ssamiswmeinfo(const u_int8_t *frm)
2773138593Ssam{
2774173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
2775173275Ssam		frm[6] == WME_INFO_OUI_SUBTYPE;
2776138593Ssam}
2777138593Ssam
2778178354Ssamstatic __inline int
2779173275Ssamiswmeparam(const u_int8_t *frm)
2780173275Ssam{
2781173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
2782173275Ssam		frm[6] == WME_PARAM_OUI_SUBTYPE;
2783173275Ssam}
2784173275Ssam
2785178354Ssamstatic __inline int
2786138593Ssamisatherosoui(const u_int8_t *frm)
2787138593Ssam{
2788138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
2789138593Ssam}
2790138593Ssam
2791181454Ssamstatic __inline int
2792186904Ssamistdmaoui(const uint8_t *frm)
2793186904Ssam{
2794186904Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((TDMA_OUI_TYPE<<24)|TDMA_OUI);
2795186904Ssam}
2796186904Ssam
2797186904Ssamstatic __inline int
2798181454Ssamiswpsoui(const uint8_t *frm)
2799181454Ssam{
2800181454Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPS_OUI_TYPE<<24)|WPA_OUI);
2801181454Ssam}
2802181454Ssam
2803173275Ssamstatic const char *
2804173275Ssamiename(int elemid)
2805173275Ssam{
2806173275Ssam	switch (elemid) {
2807173275Ssam	case IEEE80211_ELEMID_FHPARMS:	return " FHPARMS";
2808173275Ssam	case IEEE80211_ELEMID_CFPARMS:	return " CFPARMS";
2809173275Ssam	case IEEE80211_ELEMID_TIM:	return " TIM";
2810173275Ssam	case IEEE80211_ELEMID_IBSSPARMS:return " IBSSPARMS";
2811173275Ssam	case IEEE80211_ELEMID_CHALLENGE:return " CHALLENGE";
2812173275Ssam	case IEEE80211_ELEMID_PWRCNSTR:	return " PWRCNSTR";
2813173275Ssam	case IEEE80211_ELEMID_PWRCAP:	return " PWRCAP";
2814173275Ssam	case IEEE80211_ELEMID_TPCREQ:	return " TPCREQ";
2815173275Ssam	case IEEE80211_ELEMID_TPCREP:	return " TPCREP";
2816173275Ssam	case IEEE80211_ELEMID_SUPPCHAN:	return " SUPPCHAN";
2817173275Ssam	case IEEE80211_ELEMID_CHANSWITCHANN:return " CSA";
2818173275Ssam	case IEEE80211_ELEMID_MEASREQ:	return " MEASREQ";
2819173275Ssam	case IEEE80211_ELEMID_MEASREP:	return " MEASREP";
2820173275Ssam	case IEEE80211_ELEMID_QUIET:	return " QUIET";
2821173275Ssam	case IEEE80211_ELEMID_IBSSDFS:	return " IBSSDFS";
2822173275Ssam	case IEEE80211_ELEMID_TPC:	return " TPC";
2823173275Ssam	case IEEE80211_ELEMID_CCKM:	return " CCKM";
2824173275Ssam	}
2825173275Ssam	return " ???";
2826173275Ssam}
2827173275Ssam
2828138593Ssamstatic void
2829138593Ssamprinties(const u_int8_t *vp, int ielen, int maxcols)
2830138593Ssam{
2831138593Ssam	while (ielen > 0) {
2832138593Ssam		switch (vp[0]) {
2833173275Ssam		case IEEE80211_ELEMID_SSID:
2834173275Ssam			if (verbose)
2835173275Ssam				printssid(" SSID", vp, 2+vp[1], maxcols);
2836173275Ssam			break;
2837173275Ssam		case IEEE80211_ELEMID_RATES:
2838173275Ssam		case IEEE80211_ELEMID_XRATES:
2839173275Ssam			if (verbose)
2840173275Ssam				printrates(vp[0] == IEEE80211_ELEMID_RATES ?
2841173275Ssam				    " RATES" : " XRATES", vp, 2+vp[1], maxcols);
2842173275Ssam			break;
2843173275Ssam		case IEEE80211_ELEMID_DSPARMS:
2844173275Ssam			if (verbose)
2845173275Ssam				printf(" DSPARMS<%u>", vp[2]);
2846173275Ssam			break;
2847173275Ssam		case IEEE80211_ELEMID_COUNTRY:
2848173275Ssam			if (verbose)
2849173275Ssam				printcountry(" COUNTRY", vp, 2+vp[1], maxcols);
2850173275Ssam			break;
2851173275Ssam		case IEEE80211_ELEMID_ERP:
2852173275Ssam			if (verbose)
2853173275Ssam				printf(" ERP<0x%x>", vp[2]);
2854173275Ssam			break;
2855138593Ssam		case IEEE80211_ELEMID_VENDOR:
2856138593Ssam			if (iswpaoui(vp))
2857170531Ssam				printwpaie(" WPA", vp, 2+vp[1], maxcols);
2858173275Ssam			else if (iswmeinfo(vp))
2859173275Ssam				printwmeinfo(" WME", vp, 2+vp[1], maxcols);
2860173275Ssam			else if (iswmeparam(vp))
2861173275Ssam				printwmeparam(" WME", vp, 2+vp[1], maxcols);
2862139492Ssam			else if (isatherosoui(vp))
2863170531Ssam				printathie(" ATH", vp, 2+vp[1], maxcols);
2864181454Ssam			else if (iswpsoui(vp))
2865181454Ssam				printwpsie(" WPS", vp, 2+vp[1], maxcols);
2866186904Ssam			else if (istdmaoui(vp))
2867186904Ssam				printtdmaie(" TDMA", vp, 2+vp[1], maxcols);
2868173275Ssam			else if (verbose)
2869138593Ssam				printie(" VEN", vp, 2+vp[1], maxcols);
2870138593Ssam			break;
2871138593Ssam		case IEEE80211_ELEMID_RSN:
2872170531Ssam			printrsnie(" RSN", vp, 2+vp[1], maxcols);
2873138593Ssam			break;
2874173275Ssam		case IEEE80211_ELEMID_HTCAP:
2875173275Ssam			printhtcap(" HTCAP", vp, 2+vp[1], maxcols);
2876173275Ssam			break;
2877173275Ssam		case IEEE80211_ELEMID_HTINFO:
2878173275Ssam			if (verbose)
2879173275Ssam				printhtinfo(" HTINFO", vp, 2+vp[1], maxcols);
2880173275Ssam			break;
2881138593Ssam		default:
2882173275Ssam			if (verbose)
2883173275Ssam				printie(iename(vp[0]), vp, 2+vp[1], maxcols);
2884138593Ssam			break;
2885138593Ssam		}
2886138593Ssam		ielen -= 2+vp[1];
2887138593Ssam		vp += 2+vp[1];
2888138593Ssam	}
2889138593Ssam}
2890138593Ssam
2891138593Ssamstatic void
2892178354Ssamprintmimo(const struct ieee80211_mimo_info *mi)
2893178354Ssam{
2894178354Ssam	/* NB: don't muddy display unless there's something to show */
2895178354Ssam	if (mi->rssi[0] != 0 || mi->rssi[1] != 0 || mi->rssi[2] != 0) {
2896178354Ssam		/* XXX ignore EVM for now */
2897178354Ssam		printf(" (rssi %d:%d:%d nf %d:%d:%d)",
2898178354Ssam		    mi->rssi[0], mi->rssi[1], mi->rssi[2],
2899178354Ssam		    mi->noise[0], mi->noise[1], mi->noise[2]);
2900178354Ssam	}
2901178354Ssam}
2902178354Ssam
2903178354Ssamstatic void
2904138593Ssamlist_scan(int s)
2905138593Ssam{
2906138593Ssam	uint8_t buf[24*1024];
2907153892Srwatson	char ssid[IEEE80211_NWID_LEN+1];
2908173275Ssam	const uint8_t *cp;
2909154522Ssam	int len, ssidmax;
2910138593Ssam
2911173275Ssam	if (get80211len(s, IEEE80211_IOC_SCAN_RESULTS, buf, sizeof(buf), &len) < 0)
2912138593Ssam		errx(1, "unable to get scan results");
2913138593Ssam	if (len < sizeof(struct ieee80211req_scan_result))
2914138593Ssam		return;
2915138593Ssam
2916170531Ssam	getchaninfo(s);
2917170531Ssam
2918154522Ssam	ssidmax = verbose ? IEEE80211_NWID_LEN : 14;
2919170531Ssam	printf("%-*.*s  %-17.17s  %4s %4s  %-7s  %3s %4s\n"
2920154522Ssam		, ssidmax, ssidmax, "SSID"
2921138593Ssam		, "BSSID"
2922138593Ssam		, "CHAN"
2923138593Ssam		, "RATE"
2924170531Ssam		, " S:N"
2925138593Ssam		, "INT"
2926138593Ssam		, "CAPS"
2927138593Ssam	);
2928138593Ssam	cp = buf;
2929138593Ssam	do {
2930170531Ssam		const struct ieee80211req_scan_result *sr;
2931170531Ssam		const uint8_t *vp;
2932138593Ssam
2933170531Ssam		sr = (const struct ieee80211req_scan_result *) cp;
2934173275Ssam		vp = cp + sr->isr_ie_off;
2935170531Ssam		printf("%-*.*s  %s  %3d  %3dM %3d:%-3d  %3d %-4.4s"
2936154522Ssam			, ssidmax
2937155461Ssam			  , copy_essid(ssid, ssidmax, vp, sr->isr_ssid_len)
2938154522Ssam			  , ssid
2939138593Ssam			, ether_ntoa((const struct ether_addr *) sr->isr_bssid)
2940165570Ssam			, ieee80211_mhz2ieee(sr->isr_freq, sr->isr_flags)
2941138593Ssam			, getmaxrate(sr->isr_rates, sr->isr_nrates)
2942170531Ssam			, (sr->isr_rssi/2)+sr->isr_noise, sr->isr_noise
2943138593Ssam			, sr->isr_intval
2944138593Ssam			, getcaps(sr->isr_capinfo)
2945138593Ssam		);
2946170531Ssam		printies(vp + sr->isr_ssid_len, sr->isr_ie_len, 24);
2947138593Ssam		printf("\n");
2948138593Ssam		cp += sr->isr_len, len -= sr->isr_len;
2949138593Ssam	} while (len >= sizeof(struct ieee80211req_scan_result));
2950138593Ssam}
2951138593Ssam
2952178354Ssam#ifdef __FreeBSD__
2953138593Ssam#include <net80211/ieee80211_freebsd.h>
2954178354Ssam#endif
2955178354Ssam#ifdef __NetBSD__
2956178354Ssam#include <net80211/ieee80211_netbsd.h>
2957178354Ssam#endif
2958138593Ssam
2959138593Ssamstatic void
2960138593Ssamscan_and_wait(int s)
2961138593Ssam{
2962178354Ssam	struct ieee80211_scan_req sr;
2963138593Ssam	struct ieee80211req ireq;
2964138593Ssam	int sroute;
2965138593Ssam
2966138593Ssam	sroute = socket(PF_ROUTE, SOCK_RAW, 0);
2967138593Ssam	if (sroute < 0) {
2968138593Ssam		perror("socket(PF_ROUTE,SOCK_RAW)");
2969138593Ssam		return;
2970138593Ssam	}
2971138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
2972138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
2973138593Ssam	ireq.i_type = IEEE80211_IOC_SCAN_REQ;
2974178354Ssam
2975178354Ssam	memset(&sr, 0, sizeof(sr));
2976178354Ssam	sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE
2977178354Ssam		    | IEEE80211_IOC_SCAN_NOPICK
2978178354Ssam		    | IEEE80211_IOC_SCAN_ONCE;
2979178354Ssam	sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
2980178354Ssam	sr.sr_nssid = 0;
2981178354Ssam
2982178354Ssam	ireq.i_data = &sr;
2983178354Ssam	ireq.i_len = sizeof(sr);
2984138593Ssam	/* NB: only root can trigger a scan so ignore errors */
2985138593Ssam	if (ioctl(s, SIOCS80211, &ireq) >= 0) {
2986138593Ssam		char buf[2048];
2987138593Ssam		struct if_announcemsghdr *ifan;
2988138593Ssam		struct rt_msghdr *rtm;
2989138593Ssam
2990138593Ssam		do {
2991138593Ssam			if (read(sroute, buf, sizeof(buf)) < 0) {
2992138593Ssam				perror("read(PF_ROUTE)");
2993138593Ssam				break;
2994138593Ssam			}
2995138593Ssam			rtm = (struct rt_msghdr *) buf;
2996138593Ssam			if (rtm->rtm_version != RTM_VERSION)
2997138593Ssam				break;
2998138593Ssam			ifan = (struct if_announcemsghdr *) rtm;
2999138593Ssam		} while (rtm->rtm_type != RTM_IEEE80211 ||
3000138593Ssam		    ifan->ifan_what != RTM_IEEE80211_SCAN);
3001138593Ssam	}
3002138593Ssam	close(sroute);
3003138593Ssam}
3004138593Ssam
3005138593Ssamstatic
3006138593SsamDECL_CMD_FUNC(set80211scan, val, d)
3007138593Ssam{
3008138593Ssam	scan_and_wait(s);
3009138593Ssam	list_scan(s);
3010138593Ssam}
3011138593Ssam
3012161147Ssamstatic enum ieee80211_opmode get80211opmode(int s);
3013161147Ssam
3014173275Ssamstatic int
3015173275Ssamgettxseq(const struct ieee80211req_sta_info *si)
3016173275Ssam{
3017173275Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
3018173275Ssam
3019173275Ssam	int i, txseq;
3020173275Ssam
3021173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3022173275Ssam		return si->isi_txseqs[0];
3023173275Ssam	/* XXX not right but usually what folks want */
3024173275Ssam	txseq = 0;
3025173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3026173275Ssam		if (si->isi_txseqs[i] > txseq)
3027173275Ssam			txseq = si->isi_txseqs[i];
3028173275Ssam	return txseq;
3029173275Ssam#undef IEEE80211_NODE_QOS
3030173275Ssam}
3031173275Ssam
3032173275Ssamstatic int
3033173275Ssamgetrxseq(const struct ieee80211req_sta_info *si)
3034173275Ssam{
3035173275Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
3036173275Ssam
3037173275Ssam	int i, rxseq;
3038173275Ssam
3039173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3040173275Ssam		return si->isi_rxseqs[0];
3041173275Ssam	/* XXX not right but usually what folks want */
3042173275Ssam	rxseq = 0;
3043173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3044173275Ssam		if (si->isi_rxseqs[i] > rxseq)
3045173275Ssam			rxseq = si->isi_rxseqs[i];
3046173275Ssam	return rxseq;
3047173275Ssam#undef IEEE80211_NODE_QOS
3048173275Ssam}
3049173275Ssam
3050138593Ssamstatic void
3051138593Ssamlist_stations(int s)
3052138593Ssam{
3053161147Ssam	union {
3054161147Ssam		struct ieee80211req_sta_req req;
3055161147Ssam		uint8_t buf[24*1024];
3056161147Ssam	} u;
3057161147Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
3058170531Ssam	const uint8_t *cp;
3059138593Ssam	int len;
3060138593Ssam
3061161147Ssam	/* broadcast address =>'s get all stations */
3062161147Ssam	(void) memset(u.req.is_u.macaddr, 0xff, IEEE80211_ADDR_LEN);
3063161147Ssam	if (opmode == IEEE80211_M_STA) {
3064161147Ssam		/*
3065161147Ssam		 * Get information about the associated AP.
3066161147Ssam		 */
3067173275Ssam		(void) get80211(s, IEEE80211_IOC_BSSID,
3068173275Ssam		    u.req.is_u.macaddr, IEEE80211_ADDR_LEN);
3069161147Ssam	}
3070173275Ssam	if (get80211len(s, IEEE80211_IOC_STA_INFO, &u, sizeof(u), &len) < 0)
3071138593Ssam		errx(1, "unable to get station information");
3072138593Ssam	if (len < sizeof(struct ieee80211req_sta_info))
3073138593Ssam		return;
3074138593Ssam
3075170531Ssam	getchaninfo(s);
3076170531Ssam
3077159885Ssam	printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %4s\n"
3078138593Ssam		, "ADDR"
3079138593Ssam		, "AID"
3080138593Ssam		, "CHAN"
3081138593Ssam		, "RATE"
3082138593Ssam		, "RSSI"
3083138593Ssam		, "IDLE"
3084138593Ssam		, "TXSEQ"
3085138593Ssam		, "RXSEQ"
3086138593Ssam		, "CAPS"
3087159885Ssam		, "FLAG"
3088138593Ssam	);
3089170531Ssam	cp = (const uint8_t *) u.req.info;
3090138593Ssam	do {
3091170531Ssam		const struct ieee80211req_sta_info *si;
3092138593Ssam
3093170531Ssam		si = (const struct ieee80211req_sta_info *) cp;
3094161147Ssam		if (si->isi_len < sizeof(*si))
3095161147Ssam			break;
3096170531Ssam		printf("%s %4u %4d %3dM %3.1f %4d %6d %6d %-4.4s %-4.4s"
3097138593Ssam			, ether_ntoa((const struct ether_addr*) si->isi_macaddr)
3098138593Ssam			, IEEE80211_AID(si->isi_associd)
3099166015Ssam			, ieee80211_mhz2ieee(si->isi_freq, si->isi_flags)
3100178354Ssam			, si->isi_txmbps/2
3101170531Ssam			, si->isi_rssi/2.
3102138593Ssam			, si->isi_inact
3103173275Ssam			, gettxseq(si)
3104173275Ssam			, getrxseq(si)
3105138593Ssam			, getcaps(si->isi_capinfo)
3106159885Ssam			, getflags(si->isi_state)
3107138593Ssam		);
3108170531Ssam		printies(cp + si->isi_ie_off, si->isi_ie_len, 24);
3109178354Ssam		printmimo(&si->isi_mimo);
3110138593Ssam		printf("\n");
3111138593Ssam		cp += si->isi_len, len -= si->isi_len;
3112138593Ssam	} while (len >= sizeof(struct ieee80211req_sta_info));
3113138593Ssam}
3114138593Ssam
3115170531Ssamstatic const char *
3116170531Ssamget_chaninfo(const struct ieee80211_channel *c, int precise,
3117170531Ssam	char buf[], size_t bsize)
3118138593Ssam{
3119138593Ssam	buf[0] = '\0';
3120138593Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
3121170531Ssam		strlcat(buf, " FHSS", bsize);
3122187843Ssam	if (IEEE80211_IS_CHAN_A(c))
3123187843Ssam		strlcat(buf, " 11a", bsize);
3124187843Ssam	else if (IEEE80211_IS_CHAN_ANYG(c))
3125187843Ssam		strlcat(buf, " 11g", bsize);
3126187843Ssam	else if (IEEE80211_IS_CHAN_B(c))
3127170531Ssam		strlcat(buf, " 11b", bsize);
3128187843Ssam	if (IEEE80211_IS_CHAN_HALF(c))
3129187843Ssam		strlcat(buf, "/10Mhz", bsize);
3130187843Ssam	if (IEEE80211_IS_CHAN_QUARTER(c))
3131187843Ssam		strlcat(buf, "/5Mhz", bsize);
3132170531Ssam	if (IEEE80211_IS_CHAN_TURBO(c))
3133170531Ssam		strlcat(buf, " Turbo", bsize);
3134170531Ssam	if (precise) {
3135170531Ssam		if (IEEE80211_IS_CHAN_HT20(c))
3136170531Ssam			strlcat(buf, " ht/20", bsize);
3137170531Ssam		else if (IEEE80211_IS_CHAN_HT40D(c))
3138170531Ssam			strlcat(buf, " ht/40-", bsize);
3139170531Ssam		else if (IEEE80211_IS_CHAN_HT40U(c))
3140170531Ssam			strlcat(buf, " ht/40+", bsize);
3141170531Ssam	} else {
3142170531Ssam		if (IEEE80211_IS_CHAN_HT(c))
3143170531Ssam			strlcat(buf, " ht", bsize);
3144170531Ssam	}
3145170531Ssam	return buf;
3146170531Ssam}
3147170531Ssam
3148170531Ssamstatic void
3149173275Ssamprint_chaninfo(const struct ieee80211_channel *c, int verb)
3150170531Ssam{
3151170531Ssam	char buf[14];
3152170531Ssam
3153138593Ssam	printf("Channel %3u : %u%c Mhz%-14.14s",
3154165570Ssam		ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
3155170531Ssam		IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
3156173275Ssam		get_chaninfo(c, verb, buf, sizeof(buf)));
3157138593Ssam}
3158138593Ssam
3159187844Ssamstatic int
3160187844Ssamchanpref(const struct ieee80211_channel *c)
3161187844Ssam{
3162187844Ssam	if (IEEE80211_IS_CHAN_HT40(c))
3163187844Ssam		return 40;
3164187844Ssam	if (IEEE80211_IS_CHAN_HT20(c))
3165187844Ssam		return 30;
3166187844Ssam	if (IEEE80211_IS_CHAN_HALF(c))
3167187844Ssam		return 10;
3168187844Ssam	if (IEEE80211_IS_CHAN_QUARTER(c))
3169187844Ssam		return 5;
3170187844Ssam	if (IEEE80211_IS_CHAN_TURBO(c))
3171187844Ssam		return 25;
3172187844Ssam	if (IEEE80211_IS_CHAN_A(c))
3173187844Ssam		return 20;
3174187844Ssam	if (IEEE80211_IS_CHAN_G(c))
3175187844Ssam		return 20;
3176187844Ssam	if (IEEE80211_IS_CHAN_B(c))
3177187844Ssam		return 15;
3178187844Ssam	if (IEEE80211_IS_CHAN_PUREG(c))
3179187844Ssam		return 15;
3180187844Ssam	return 0;
3181187844Ssam}
3182187844Ssam
3183138593Ssamstatic void
3184173275Ssamprint_channels(int s, const struct ieee80211req_chaninfo *chans,
3185173275Ssam	int allchans, int verb)
3186138593Ssam{
3187187801Ssam	struct ieee80211req_chaninfo *achans;
3188170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3189138593Ssam	const struct ieee80211_channel *c;
3190170531Ssam	int i, half;
3191138593Ssam
3192187801Ssam	achans = malloc(IEEE80211_CHANINFO_SPACE(chans));
3193187801Ssam	if (achans == NULL)
3194187801Ssam		errx(1, "no space for active channel list");
3195187801Ssam	achans->ic_nchans = 0;
3196170531Ssam	memset(reported, 0, sizeof(reported));
3197138593Ssam	if (!allchans) {
3198138593Ssam		struct ieee80211req_chanlist active;
3199138593Ssam
3200173275Ssam		if (get80211(s, IEEE80211_IOC_CHANLIST, &active, sizeof(active)) < 0)
3201138593Ssam			errx(1, "unable to get active channel list");
3202173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3203173275Ssam			c = &chans->ic_chans[i];
3204170531Ssam			if (!isset(active.ic_channels, c->ic_ieee))
3205170531Ssam				continue;
3206170531Ssam			/*
3207170531Ssam			 * Suppress compatible duplicates unless
3208170531Ssam			 * verbose.  The kernel gives us it's
3209170531Ssam			 * complete channel list which has separate
3210170531Ssam			 * entries for 11g/11b and 11a/turbo.
3211170531Ssam			 */
3212173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3213170531Ssam				/* XXX we assume duplicates are adjacent */
3214187801Ssam				achans->ic_chans[achans->ic_nchans-1] = *c;
3215170531Ssam			} else {
3216187801Ssam				achans->ic_chans[achans->ic_nchans++] = *c;
3217170531Ssam				setbit(reported, c->ic_ieee);
3218170531Ssam			}
3219138593Ssam		}
3220170531Ssam	} else {
3221173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3222173275Ssam			c = &chans->ic_chans[i];
3223170531Ssam			/* suppress duplicates as above */
3224173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3225170531Ssam				/* XXX we assume duplicates are adjacent */
3226187844Ssam				struct ieee80211_channel *a =
3227187844Ssam				    &achans->ic_chans[achans->ic_nchans-1];
3228187844Ssam				if (chanpref(c) > chanpref(a))
3229187844Ssam					*a = *c;
3230170531Ssam			} else {
3231187801Ssam				achans->ic_chans[achans->ic_nchans++] = *c;
3232170531Ssam				setbit(reported, c->ic_ieee);
3233170531Ssam			}
3234170531Ssam		}
3235170531Ssam	}
3236187801Ssam	half = achans->ic_nchans / 2;
3237187801Ssam	if (achans->ic_nchans % 2)
3238138593Ssam		half++;
3239170531Ssam
3240187801Ssam	for (i = 0; i < achans->ic_nchans / 2; i++) {
3241187801Ssam		print_chaninfo(&achans->ic_chans[i], verb);
3242187801Ssam		print_chaninfo(&achans->ic_chans[half+i], verb);
3243138593Ssam		printf("\n");
3244138593Ssam	}
3245187801Ssam	if (achans->ic_nchans % 2) {
3246187801Ssam		print_chaninfo(&achans->ic_chans[i], verb);
3247138593Ssam		printf("\n");
3248138593Ssam	}
3249187801Ssam	free(achans);
3250138593Ssam}
3251138593Ssam
3252138593Ssamstatic void
3253173275Ssamlist_channels(int s, int allchans)
3254173275Ssam{
3255173275Ssam	getchaninfo(s);
3256187801Ssam	print_channels(s, chaninfo, allchans, verbose);
3257173275Ssam}
3258173275Ssam
3259173275Ssamstatic void
3260170531Ssamprint_txpow(const struct ieee80211_channel *c)
3261170531Ssam{
3262170531Ssam	printf("Channel %3u : %u Mhz %3.1f reg %2d  ",
3263170531Ssam	    c->ic_ieee, c->ic_freq,
3264170531Ssam	    c->ic_maxpower/2., c->ic_maxregpower);
3265170531Ssam}
3266170531Ssam
3267170531Ssamstatic void
3268170531Ssamprint_txpow_verbose(const struct ieee80211_channel *c)
3269170531Ssam{
3270173275Ssam	print_chaninfo(c, 1);
3271170531Ssam	printf("min %4.1f dBm  max %3.1f dBm  reg %2d dBm",
3272170531Ssam	    c->ic_minpower/2., c->ic_maxpower/2., c->ic_maxregpower);
3273170531Ssam	/* indicate where regulatory cap limits power use */
3274170531Ssam	if (c->ic_maxpower > 2*c->ic_maxregpower)
3275170531Ssam		printf(" <");
3276170531Ssam}
3277170531Ssam
3278170531Ssamstatic void
3279170531Ssamlist_txpow(int s)
3280170531Ssam{
3281187801Ssam	struct ieee80211req_chaninfo *achans;
3282170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3283170531Ssam	struct ieee80211_channel *c, *prev;
3284170531Ssam	int i, half;
3285170531Ssam
3286170531Ssam	getchaninfo(s);
3287187801Ssam	achans = malloc(IEEE80211_CHANINFO_SPACE(chaninfo));
3288187801Ssam	if (achans == NULL)
3289187801Ssam		errx(1, "no space for active channel list");
3290187801Ssam	achans->ic_nchans = 0;
3291170531Ssam	memset(reported, 0, sizeof(reported));
3292187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
3293187801Ssam		c = &chaninfo->ic_chans[i];
3294170531Ssam		/* suppress duplicates as above */
3295170531Ssam		if (isset(reported, c->ic_ieee) && !verbose) {
3296170531Ssam			/* XXX we assume duplicates are adjacent */
3297187801Ssam			prev = &achans->ic_chans[achans->ic_nchans-1];
3298170531Ssam			/* display highest power on channel */
3299170531Ssam			if (c->ic_maxpower > prev->ic_maxpower)
3300170531Ssam				*prev = *c;
3301170531Ssam		} else {
3302187801Ssam			achans->ic_chans[achans->ic_nchans++] = *c;
3303170531Ssam			setbit(reported, c->ic_ieee);
3304170531Ssam		}
3305170531Ssam	}
3306170531Ssam	if (!verbose) {
3307187801Ssam		half = achans->ic_nchans / 2;
3308187801Ssam		if (achans->ic_nchans % 2)
3309170531Ssam			half++;
3310170531Ssam
3311187801Ssam		for (i = 0; i < achans->ic_nchans / 2; i++) {
3312187801Ssam			print_txpow(&achans->ic_chans[i]);
3313187801Ssam			print_txpow(&achans->ic_chans[half+i]);
3314170531Ssam			printf("\n");
3315170531Ssam		}
3316187801Ssam		if (achans->ic_nchans % 2) {
3317187801Ssam			print_txpow(&achans->ic_chans[i]);
3318170531Ssam			printf("\n");
3319170531Ssam		}
3320170531Ssam	} else {
3321187801Ssam		for (i = 0; i < achans->ic_nchans; i++) {
3322187801Ssam			print_txpow_verbose(&achans->ic_chans[i]);
3323170531Ssam			printf("\n");
3324170531Ssam		}
3325170531Ssam	}
3326187801Ssam	free(achans);
3327170531Ssam}
3328170531Ssam
3329170531Ssamstatic void
3330138593Ssamlist_keys(int s)
3331138593Ssam{
3332138593Ssam}
3333138593Ssam
3334138593Ssam#define	IEEE80211_C_BITS \
3335181102Ssam	"\20\1STA\7FF\10TURBOP\11IBSS\12PMGT" \
3336178354Ssam	"\13HOSTAP\14AHDEMO\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE" \
3337181102Ssam	"\21MONITOR\22DFS\30WPA1\31WPA2\32BURST\33WME\34WDS\36BGSCAN" \
3338186904Ssam	"\37TXFRAG\40TDMA"
3339138593Ssam
3340138593Ssamstatic void
3341138593Ssamlist_capabilities(int s)
3342138593Ssam{
3343187801Ssam	struct ieee80211_devcaps_req *dc;
3344138593Ssam
3345187845Ssam	if (verbose)
3346187845Ssam		dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
3347187845Ssam	else
3348187845Ssam		dc = malloc(IEEE80211_DEVCAPS_SIZE(1));
3349187801Ssam	if (dc == NULL)
3350187801Ssam		errx(1, "no space for device capabilities");
3351187845Ssam	dc->dc_chaninfo.ic_nchans = verbose ? MAXCHAN : 1;
3352187801Ssam	getdevcaps(s, dc);
3353187801Ssam	printb("drivercaps", dc->dc_drivercaps, IEEE80211_C_BITS);
3354187801Ssam	if (dc->dc_cryptocaps != 0 || verbose) {
3355178354Ssam		putchar('\n');
3356187801Ssam		printb("cryptocaps", dc->dc_cryptocaps, IEEE80211_CRYPTO_BITS);
3357178354Ssam	}
3358187801Ssam	if (dc->dc_htcaps != 0 || verbose) {
3359178354Ssam		putchar('\n');
3360187801Ssam		printb("htcaps", dc->dc_htcaps, IEEE80211_HTCAP_BITS);
3361178354Ssam	}
3362138593Ssam	putchar('\n');
3363187845Ssam	if (verbose) {
3364187845Ssam		chaninfo = &dc->dc_chaninfo;	/* XXX */
3365187845Ssam		print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, verbose);
3366187845Ssam	}
3367187801Ssam	free(dc);
3368138593Ssam}
3369138593Ssam
3370173275Ssamstatic int
3371173275Ssamget80211wme(int s, int param, int ac, int *val)
3372173275Ssam{
3373173275Ssam	struct ieee80211req ireq;
3374173275Ssam
3375173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3376173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3377173275Ssam	ireq.i_type = param;
3378173275Ssam	ireq.i_len = ac;
3379173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3380173275Ssam		warn("cannot get WME parameter %d, ac %d%s",
3381173275Ssam		    param, ac & IEEE80211_WMEPARAM_VAL,
3382173275Ssam		    ac & IEEE80211_WMEPARAM_BSS ? " (BSS)" : "");
3383173275Ssam		return -1;
3384173275Ssam	}
3385173275Ssam	*val = ireq.i_val;
3386173275Ssam	return 0;
3387173275Ssam}
3388173275Ssam
3389138593Ssamstatic void
3390181199Ssamlist_wme_aci(int s, const char *tag, int ac)
3391138593Ssam{
3392181199Ssam	int val;
3393138593Ssam
3394181199Ssam	printf("\t%s", tag);
3395138593Ssam
3396181199Ssam	/* show WME BSS parameters */
3397181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMIN, ac, &val) != -1)
3398181199Ssam		printf(" cwmin %2u", val);
3399181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMAX, ac, &val) != -1)
3400181199Ssam		printf(" cwmax %2u", val);
3401181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_AIFS, ac, &val) != -1)
3402181199Ssam		printf(" aifs %2u", val);
3403181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_TXOPLIMIT, ac, &val) != -1)
3404181199Ssam		printf(" txopLimit %3u", val);
3405181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_ACM, ac, &val) != -1) {
3406181199Ssam		if (val)
3407181199Ssam			printf(" acm");
3408181199Ssam		else if (verbose)
3409181199Ssam			printf(" -acm");
3410181199Ssam	}
3411181199Ssam	/* !BSS only */
3412181199Ssam	if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3413181199Ssam		if (get80211wme(s, IEEE80211_IOC_WME_ACKPOLICY, ac, &val) != -1) {
3414181199Ssam			if (!val)
3415181199Ssam				printf(" -ack");
3416138593Ssam			else if (verbose)
3417181199Ssam				printf(" ack");
3418138593Ssam		}
3419181199Ssam	}
3420181199Ssam	printf("\n");
3421181199Ssam}
3422181199Ssam
3423181199Ssamstatic void
3424181199Ssamlist_wme(int s)
3425181199Ssam{
3426181199Ssam	static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
3427181199Ssam	int ac;
3428181199Ssam
3429181199Ssam	if (verbose) {
3430181199Ssam		/* display both BSS and local settings */
3431181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
3432181199Ssam	again:
3433181199Ssam			if (ac & IEEE80211_WMEPARAM_BSS)
3434181199Ssam				list_wme_aci(s, "     ", ac);
3435181199Ssam			else
3436181199Ssam				list_wme_aci(s, acnames[ac], ac);
3437181199Ssam			if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3438181199Ssam				ac |= IEEE80211_WMEPARAM_BSS;
3439181199Ssam				goto again;
3440181199Ssam			} else
3441181199Ssam				ac &= ~IEEE80211_WMEPARAM_BSS;
3442138593Ssam		}
3443181199Ssam	} else {
3444181199Ssam		/* display only channel settings */
3445181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++)
3446181199Ssam			list_wme_aci(s, acnames[ac], ac);
3447138593Ssam	}
3448138593Ssam}
3449138593Ssam
3450149029Ssamstatic void
3451178354Ssamlist_roam(int s)
3452178354Ssam{
3453178354Ssam	const struct ieee80211_roamparam *rp;
3454178354Ssam	int mode;
3455178354Ssam
3456178354Ssam	getroam(s);
3457178354Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_11NA; mode++) {
3458178354Ssam		rp = &roamparams.params[mode];
3459178354Ssam		if (rp->rssi == 0 && rp->rate == 0)
3460178354Ssam			continue;
3461178354Ssam		if (rp->rssi & 1)
3462178354Ssam			LINE_CHECK("roam:%-6.6s rssi %2u.5dBm rate %2u Mb/s",
3463178354Ssam			    modename[mode], rp->rssi/2, rp->rate/2);
3464178354Ssam		else
3465178354Ssam			LINE_CHECK("roam:%-6.6s rssi %4udBm rate %2u Mb/s",
3466178354Ssam			    modename[mode], rp->rssi/2, rp->rate/2);
3467178354Ssam	}
3468178354Ssam	for (; mode < IEEE80211_MODE_MAX; mode++) {
3469178354Ssam		rp = &roamparams.params[mode];
3470178354Ssam		if (rp->rssi == 0 && rp->rate == 0)
3471178354Ssam			continue;
3472178354Ssam		if (rp->rssi & 1)
3473178354Ssam			LINE_CHECK("roam:%-6.6s rssi %2u.5dBm  MCS %2u    ",
3474178354Ssam			    modename[mode], rp->rssi/2, rp->rate &~ 0x80);
3475178354Ssam		else
3476178354Ssam			LINE_CHECK("roam:%-6.6s rssi %4udBm  MCS %2u    ",
3477178354Ssam			    modename[mode], rp->rssi/2, rp->rate &~ 0x80);
3478178354Ssam	}
3479178354Ssam}
3480178354Ssam
3481178354Ssamstatic void
3482178354Ssamlist_txparams(int s)
3483178354Ssam{
3484178354Ssam	const struct ieee80211_txparam *tp;
3485178354Ssam	int mode;
3486178354Ssam
3487178354Ssam	gettxparams(s);
3488178354Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_11NA; mode++) {
3489178354Ssam		tp = &txparams.params[mode];
3490178354Ssam		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
3491178354Ssam			continue;
3492178354Ssam		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3493178354Ssam			LINE_CHECK("%-6.6s ucast NONE    mgmt %2u Mb/s "
3494178354Ssam			    "mcast %2u Mb/s maxretry %u",
3495178354Ssam			    modename[mode], tp->mgmtrate/2,
3496178354Ssam			    tp->mcastrate/2, tp->maxretry);
3497178354Ssam		else
3498178354Ssam			LINE_CHECK("%-6.6s ucast %2u Mb/s mgmt %2u Mb/s "
3499178354Ssam			    "mcast %2u Mb/s maxretry %u",
3500178354Ssam			    modename[mode], tp->ucastrate/2, tp->mgmtrate/2,
3501178354Ssam			    tp->mcastrate/2, tp->maxretry);
3502178354Ssam	}
3503178354Ssam	for (; mode < IEEE80211_MODE_MAX; mode++) {
3504178354Ssam		tp = &txparams.params[mode];
3505178354Ssam		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
3506178354Ssam			continue;
3507178354Ssam		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3508178354Ssam			LINE_CHECK("%-6.6s ucast NONE    mgmt %2u MCS  "
3509178354Ssam			    "mcast %2u MCS  maxretry %u",
3510178354Ssam			    modename[mode], tp->mgmtrate &~ 0x80,
3511178354Ssam			    tp->mcastrate &~ 0x80, tp->maxretry);
3512178354Ssam		else
3513178354Ssam			LINE_CHECK("%-6.6s ucast %2u MCS  mgmt %2u MCS  "
3514178354Ssam			    "mcast %2u MCS  maxretry %u",
3515178354Ssam			    modename[mode], tp->ucastrate &~ 0x80,
3516178354Ssam			    tp->mgmtrate &~ 0x80,
3517178354Ssam			    tp->mcastrate &~ 0x80, tp->maxretry);
3518178354Ssam	}
3519178354Ssam}
3520178354Ssam
3521178354Ssamstatic void
3522173275Ssamprintpolicy(int policy)
3523173275Ssam{
3524173275Ssam	switch (policy) {
3525173275Ssam	case IEEE80211_MACCMD_POLICY_OPEN:
3526173275Ssam		printf("policy: open\n");
3527173275Ssam		break;
3528173275Ssam	case IEEE80211_MACCMD_POLICY_ALLOW:
3529173275Ssam		printf("policy: allow\n");
3530173275Ssam		break;
3531173275Ssam	case IEEE80211_MACCMD_POLICY_DENY:
3532173275Ssam		printf("policy: deny\n");
3533173275Ssam		break;
3534178354Ssam	case IEEE80211_MACCMD_POLICY_RADIUS:
3535178354Ssam		printf("policy: radius\n");
3536178354Ssam		break;
3537173275Ssam	default:
3538173275Ssam		printf("policy: unknown (%u)\n", policy);
3539173275Ssam		break;
3540173275Ssam	}
3541173275Ssam}
3542173275Ssam
3543173275Ssamstatic void
3544149029Ssamlist_mac(int s)
3545149029Ssam{
3546149029Ssam	struct ieee80211req ireq;
3547149029Ssam	struct ieee80211req_maclist *acllist;
3548173275Ssam	int i, nacls, policy, len;
3549173275Ssam	uint8_t *data;
3550149029Ssam	char c;
3551149029Ssam
3552149029Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3553149029Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
3554149029Ssam	ireq.i_type = IEEE80211_IOC_MACCMD;
3555149029Ssam	ireq.i_val = IEEE80211_MACCMD_POLICY;
3556149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3557149029Ssam		if (errno == EINVAL) {
3558149029Ssam			printf("No acl policy loaded\n");
3559149029Ssam			return;
3560149029Ssam		}
3561149029Ssam		err(1, "unable to get mac policy");
3562149029Ssam	}
3563149029Ssam	policy = ireq.i_val;
3564149029Ssam	if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
3565149029Ssam		c = '*';
3566149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
3567149029Ssam		c = '+';
3568149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
3569149029Ssam		c = '-';
3570178354Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_RADIUS) {
3571178354Ssam		c = 'r';		/* NB: should never have entries */
3572149029Ssam	} else {
3573149029Ssam		printf("policy: unknown (%u)\n", policy);
3574149029Ssam		c = '?';
3575149029Ssam	}
3576173275Ssam	if (verbose || c == '?')
3577173275Ssam		printpolicy(policy);
3578173275Ssam
3579175952Ssam	ireq.i_val = IEEE80211_MACCMD_LIST;
3580175952Ssam	ireq.i_len = 0;
3581175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3582173275Ssam		err(1, "unable to get mac acl list size");
3583175952Ssam	if (ireq.i_len == 0) {		/* NB: no acls */
3584173275Ssam		if (!(verbose || c == '?'))
3585173275Ssam			printpolicy(policy);
3586173275Ssam		return;
3587173275Ssam	}
3588175952Ssam	len = ireq.i_len;
3589173275Ssam
3590173275Ssam	data = malloc(len);
3591173275Ssam	if (data == NULL)
3592173275Ssam		err(1, "out of memory for acl list");
3593173275Ssam
3594175952Ssam	ireq.i_data = data;
3595175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3596173275Ssam		err(1, "unable to get mac acl list");
3597173275Ssam	nacls = len / sizeof(*acllist);
3598173275Ssam	acllist = (struct ieee80211req_maclist *) data;
3599149029Ssam	for (i = 0; i < nacls; i++)
3600149029Ssam		printf("%c%s\n", c, ether_ntoa(
3601149029Ssam			(const struct ether_addr *) acllist[i].ml_macaddr));
3602173275Ssam	free(data);
3603149029Ssam}
3604149029Ssam
3605178354Ssamstatic void
3606178354Ssamprint_regdomain(const struct ieee80211_regdomain *reg, int verb)
3607178354Ssam{
3608178354Ssam	if ((reg->regdomain != 0 &&
3609178354Ssam	    reg->regdomain != reg->country) || verb) {
3610178354Ssam		const struct regdomain *rd =
3611178354Ssam		    lib80211_regdomain_findbysku(getregdata(), reg->regdomain);
3612178354Ssam		if (rd == NULL)
3613178354Ssam			LINE_CHECK("regdomain %d", reg->regdomain);
3614178354Ssam		else
3615178354Ssam			LINE_CHECK("regdomain %s", rd->name);
3616178354Ssam	}
3617178354Ssam	if (reg->country != 0 || verb) {
3618178354Ssam		const struct country *cc =
3619178354Ssam		    lib80211_country_findbycc(getregdata(), reg->country);
3620178354Ssam		if (cc == NULL)
3621178354Ssam			LINE_CHECK("country %d", reg->country);
3622178354Ssam		else
3623178354Ssam			LINE_CHECK("country %s", cc->isoname);
3624178354Ssam	}
3625178354Ssam	if (reg->location == 'I')
3626178354Ssam		LINE_CHECK("indoor");
3627178354Ssam	else if (reg->location == 'O')
3628178354Ssam		LINE_CHECK("outdoor");
3629178354Ssam	else if (verb)
3630178354Ssam		LINE_CHECK("anywhere");
3631178354Ssam	if (reg->ecm)
3632178354Ssam		LINE_CHECK("ecm");
3633178354Ssam	else if (verb)
3634178354Ssam		LINE_CHECK("-ecm");
3635178354Ssam}
3636178354Ssam
3637178354Ssamstatic void
3638178354Ssamlist_regdomain(int s, int channelsalso)
3639178354Ssam{
3640178354Ssam	getregdomain(s);
3641178354Ssam	if (channelsalso) {
3642178354Ssam		getchaninfo(s);
3643178354Ssam		spacer = ':';
3644178354Ssam		print_regdomain(&regdomain, 1);
3645178354Ssam		LINE_BREAK();
3646187801Ssam		print_channels(s, chaninfo, 1/*allchans*/, 1/*verbose*/);
3647178354Ssam	} else
3648178354Ssam		print_regdomain(&regdomain, verbose);
3649178354Ssam}
3650178354Ssam
3651138593Ssamstatic
3652138593SsamDECL_CMD_FUNC(set80211list, arg, d)
3653138593Ssam{
3654138593Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
3655138593Ssam
3656173275Ssam	LINE_INIT('\t');
3657173275Ssam
3658138593Ssam	if (iseq(arg, "sta"))
3659138593Ssam		list_stations(s);
3660138593Ssam	else if (iseq(arg, "scan") || iseq(arg, "ap"))
3661138593Ssam		list_scan(s);
3662138593Ssam	else if (iseq(arg, "chan") || iseq(arg, "freq"))
3663138593Ssam		list_channels(s, 1);
3664138593Ssam	else if (iseq(arg, "active"))
3665138593Ssam		list_channels(s, 0);
3666138593Ssam	else if (iseq(arg, "keys"))
3667138593Ssam		list_keys(s);
3668138593Ssam	else if (iseq(arg, "caps"))
3669138593Ssam		list_capabilities(s);
3670178354Ssam	else if (iseq(arg, "wme") || iseq(arg, "wmm"))
3671138593Ssam		list_wme(s);
3672149029Ssam	else if (iseq(arg, "mac"))
3673149029Ssam		list_mac(s);
3674170531Ssam	else if (iseq(arg, "txpow"))
3675170531Ssam		list_txpow(s);
3676178354Ssam	else if (iseq(arg, "roam"))
3677178354Ssam		list_roam(s);
3678178354Ssam	else if (iseq(arg, "txparam") || iseq(arg, "txparm"))
3679178354Ssam		list_txparams(s);
3680178354Ssam	else if (iseq(arg, "regdomain"))
3681178354Ssam		list_regdomain(s, 1);
3682178354Ssam	else if (iseq(arg, "countries"))
3683178354Ssam		list_countries();
3684138593Ssam	else
3685138593Ssam		errx(1, "Don't know how to list %s for %s", arg, name);
3686178354Ssam	LINE_BREAK();
3687138593Ssam#undef iseq
3688138593Ssam}
3689138593Ssam
3690138593Ssamstatic enum ieee80211_opmode
3691138593Ssamget80211opmode(int s)
3692138593Ssam{
3693138593Ssam	struct ifmediareq ifmr;
3694138593Ssam
3695138593Ssam	(void) memset(&ifmr, 0, sizeof(ifmr));
3696138593Ssam	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
3697138593Ssam
3698138593Ssam	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
3699186101Ssam		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) {
3700186101Ssam			if (ifmr.ifm_current & IFM_FLAG0)
3701186101Ssam				return IEEE80211_M_AHDEMO;
3702186101Ssam			else
3703186101Ssam				return IEEE80211_M_IBSS;
3704186101Ssam		}
3705138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
3706138593Ssam			return IEEE80211_M_HOSTAP;
3707138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
3708138593Ssam			return IEEE80211_M_MONITOR;
3709138593Ssam	}
3710138593Ssam	return IEEE80211_M_STA;
3711138593Ssam}
3712138593Ssam
3713138593Ssam#if 0
3714138593Ssamstatic void
3715138593Ssamprintcipher(int s, struct ieee80211req *ireq, int keylenop)
3716138593Ssam{
3717138593Ssam	switch (ireq->i_val) {
3718138593Ssam	case IEEE80211_CIPHER_WEP:
3719138593Ssam		ireq->i_type = keylenop;
3720138593Ssam		if (ioctl(s, SIOCG80211, ireq) != -1)
3721138593Ssam			printf("WEP-%s",
3722138593Ssam			    ireq->i_len <= 5 ? "40" :
3723138593Ssam			    ireq->i_len <= 13 ? "104" : "128");
3724138593Ssam		else
3725138593Ssam			printf("WEP");
3726138593Ssam		break;
3727138593Ssam	case IEEE80211_CIPHER_TKIP:
3728138593Ssam		printf("TKIP");
3729138593Ssam		break;
3730138593Ssam	case IEEE80211_CIPHER_AES_OCB:
3731138593Ssam		printf("AES-OCB");
3732138593Ssam		break;
3733138593Ssam	case IEEE80211_CIPHER_AES_CCM:
3734138593Ssam		printf("AES-CCM");
3735138593Ssam		break;
3736138593Ssam	case IEEE80211_CIPHER_CKIP:
3737138593Ssam		printf("CKIP");
3738138593Ssam		break;
3739138593Ssam	case IEEE80211_CIPHER_NONE:
3740138593Ssam		printf("NONE");
3741138593Ssam		break;
3742138593Ssam	default:
3743138593Ssam		printf("UNKNOWN (0x%x)", ireq->i_val);
3744138593Ssam		break;
3745138593Ssam	}
3746138593Ssam}
3747138593Ssam#endif
3748138593Ssam
3749155931Ssamstatic void
3750138593Ssamprintkey(const struct ieee80211req_key *ik)
3751138593Ssam{
3752138593Ssam	static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
3753138593Ssam	int keylen = ik->ik_keylen;
3754138593Ssam	int printcontents;
3755138593Ssam
3756148001Srwatson	printcontents = printkeys &&
3757138593Ssam		(memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
3758138593Ssam	if (printcontents)
3759138593Ssam		LINE_BREAK();
3760138593Ssam	switch (ik->ik_type) {
3761138593Ssam	case IEEE80211_CIPHER_WEP:
3762138593Ssam		/* compatibility */
3763155931Ssam		LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
3764138593Ssam		    keylen <= 5 ? "40-bit" :
3765138593Ssam		    keylen <= 13 ? "104-bit" : "128-bit");
3766138593Ssam		break;
3767138593Ssam	case IEEE80211_CIPHER_TKIP:
3768138593Ssam		if (keylen > 128/8)
3769138593Ssam			keylen -= 128/8;	/* ignore MIC for now */
3770155931Ssam		LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3771138593Ssam		break;
3772138593Ssam	case IEEE80211_CIPHER_AES_OCB:
3773155931Ssam		LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3774138593Ssam		break;
3775138593Ssam	case IEEE80211_CIPHER_AES_CCM:
3776155931Ssam		LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3777138593Ssam		break;
3778138593Ssam	case IEEE80211_CIPHER_CKIP:
3779155931Ssam		LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3780138593Ssam		break;
3781138593Ssam	case IEEE80211_CIPHER_NONE:
3782155931Ssam		LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3783138593Ssam		break;
3784138593Ssam	default:
3785155931Ssam		LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
3786138593Ssam			ik->ik_type, ik->ik_keyix+1, 8*keylen);
3787138593Ssam		break;
3788138593Ssam	}
3789138593Ssam	if (printcontents) {
3790138593Ssam		int i;
3791138593Ssam
3792138593Ssam		printf(" <");
3793138593Ssam		for (i = 0; i < keylen; i++)
3794138593Ssam			printf("%02x", ik->ik_keydata[i]);
3795138593Ssam		printf(">");
3796138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
3797138593Ssam		    (ik->ik_keyrsc != 0 || verbose))
3798146873Sjhb			printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
3799138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
3800138593Ssam		    (ik->ik_keytsc != 0 || verbose))
3801146873Sjhb			printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
3802138593Ssam		if (ik->ik_flags != 0 && verbose) {
3803138593Ssam			const char *sep = " ";
3804138593Ssam
3805138593Ssam			if (ik->ik_flags & IEEE80211_KEY_XMIT)
3806138593Ssam				printf("%stx", sep), sep = "+";
3807138593Ssam			if (ik->ik_flags & IEEE80211_KEY_RECV)
3808138593Ssam				printf("%srx", sep), sep = "+";
3809138593Ssam			if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
3810138593Ssam				printf("%sdef", sep), sep = "+";
3811138593Ssam		}
3812138593Ssam		LINE_BREAK();
3813138593Ssam	}
3814138593Ssam}
3815138593Ssam
3816138593Ssamstatic void
3817173275Ssamprintrate(const char *tag, int v, int defrate, int defmcs)
3818138593Ssam{
3819173275Ssam	if (v == 11)
3820173275Ssam		LINE_CHECK("%s 5.5", tag);
3821173275Ssam	else if (v & 0x80) {
3822173275Ssam		if (v != defmcs)
3823173275Ssam			LINE_CHECK("%s %d", tag, v &~ 0x80);
3824173275Ssam	} else {
3825173275Ssam		if (v != defrate)
3826173275Ssam			LINE_CHECK("%s %d", tag, v/2);
3827173275Ssam	}
3828173275Ssam}
3829173275Ssam
3830173275Ssamstatic int
3831173275Ssamgetssid(int s, int ix, void *data, size_t len, int *plen)
3832173275Ssam{
3833138593Ssam	struct ieee80211req ireq;
3834138593Ssam
3835138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3836138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3837173275Ssam	ireq.i_type = IEEE80211_IOC_SSID;
3838173275Ssam	ireq.i_val = ix;
3839173275Ssam	ireq.i_data = data;
3840173275Ssam	ireq.i_len = len;
3841173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3842173275Ssam		return -1;
3843173275Ssam	*plen = ireq.i_len;
3844173275Ssam	return 0;
3845173275Ssam}
384677218Sphk
3847173275Ssamstatic void
3848173275Ssamieee80211_status(int s)
3849173275Ssam{
3850173275Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
3851173275Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
3852173275Ssam	int i, num, wpa, wme, bgscan, bgscaninterval, val, len, wepmode;
3853173275Ssam	uint8_t data[32];
3854173275Ssam	const struct ieee80211_channel *c;
3855178354Ssam	const struct ieee80211_roamparam *rp;
3856178354Ssam	const struct ieee80211_txparam *tp;
3857173275Ssam
3858173275Ssam	if (getssid(s, -1, data, sizeof(data), &len) < 0) {
3859148686Sstefanf		/* If we can't get the SSID, this isn't an 802.11 device. */
386077218Sphk		return;
386177218Sphk	}
3862173275Ssam
3863173275Ssam	/*
3864173275Ssam	 * Invalidate cached state so printing status for multiple
3865173275Ssam	 * if's doesn't reuse the first interfaces' cached state.
3866173275Ssam	 */
3867173275Ssam	gotcurchan = 0;
3868178354Ssam	gotroam = 0;
3869178354Ssam	gottxparams = 0;
3870173275Ssam	gothtconf = 0;
3871178354Ssam	gotregdomain = 0;
3872173275Ssam
3873173275Ssam	if (get80211val(s, IEEE80211_IOC_NUMSSIDS, &num) < 0)
3874173275Ssam		num = 0;
3875138593Ssam	printf("\tssid ");
3876138593Ssam	if (num > 1) {
3877173275Ssam		for (i = 0; i < num; i++) {
3878173275Ssam			if (getssid(s, i, data, sizeof(data), &len) >= 0 && len > 0) {
3879173275Ssam				printf(" %d:", i + 1);
3880173275Ssam				print_string(data, len);
3881138593Ssam			}
388288748Sambrisko		}
3883138593Ssam	} else
3884173275Ssam		print_string(data, len);
388577218Sphk
3886173275Ssam	c = getcurchan(s);
3887170531Ssam	if (c->ic_freq != IEEE80211_CHAN_ANY) {
3888170531Ssam		char buf[14];
3889170531Ssam		printf(" channel %d (%u Mhz%s)", c->ic_ieee, c->ic_freq,
3890170531Ssam			get_chaninfo(c, 1, buf, sizeof(buf)));
3891138593Ssam	} else if (verbose)
3892138593Ssam		printf(" channel UNDEF");
3893138593Ssam
3894173275Ssam	if (get80211(s, IEEE80211_IOC_BSSID, data, IEEE80211_ADDR_LEN) >= 0 &&
3895173275Ssam	    (memcmp(data, zerobssid, sizeof(zerobssid)) != 0 || verbose))
3896173275Ssam		printf(" bssid %s", ether_ntoa((struct ether_addr *)data));
3897138593Ssam
3898173275Ssam	if (get80211len(s, IEEE80211_IOC_STATIONNAME, data, sizeof(data), &len) != -1) {
3899138593Ssam		printf("\n\tstationname ");
3900173275Ssam		print_string(data, len);
390177218Sphk	}
390277218Sphk
3903138593Ssam	spacer = ' ';		/* force first break */
3904138593Ssam	LINE_BREAK();
390577218Sphk
3906178354Ssam	list_regdomain(s, 0);
3907178354Ssam
3908173275Ssam	wpa = 0;
3909173275Ssam	if (get80211val(s, IEEE80211_IOC_AUTHMODE, &val) != -1) {
3910173275Ssam		switch (val) {
3911173275Ssam		case IEEE80211_AUTH_NONE:
3912173275Ssam			LINE_CHECK("authmode NONE");
3913173275Ssam			break;
3914173275Ssam		case IEEE80211_AUTH_OPEN:
3915173275Ssam			LINE_CHECK("authmode OPEN");
3916173275Ssam			break;
3917173275Ssam		case IEEE80211_AUTH_SHARED:
3918173275Ssam			LINE_CHECK("authmode SHARED");
3919173275Ssam			break;
3920173275Ssam		case IEEE80211_AUTH_8021X:
3921173275Ssam			LINE_CHECK("authmode 802.1x");
3922173275Ssam			break;
3923173275Ssam		case IEEE80211_AUTH_WPA:
3924173275Ssam			if (get80211val(s, IEEE80211_IOC_WPA, &wpa) < 0)
3925173275Ssam				wpa = 1;	/* default to WPA1 */
3926173275Ssam			switch (wpa) {
3927173275Ssam			case 2:
3928173275Ssam				LINE_CHECK("authmode WPA2/802.11i");
392977218Sphk				break;
3930173275Ssam			case 3:
3931173275Ssam				LINE_CHECK("authmode WPA1+WPA2/802.11i");
393277218Sphk				break;
3933127649Ssam			default:
3934173275Ssam				LINE_CHECK("authmode WPA");
3935127649Ssam				break;
3936173275Ssam			}
3937173275Ssam			break;
3938173275Ssam		case IEEE80211_AUTH_AUTO:
3939173275Ssam			LINE_CHECK("authmode AUTO");
3940173275Ssam			break;
3941173275Ssam		default:
3942173275Ssam			LINE_CHECK("authmode UNKNOWN (0x%x)", val);
3943173275Ssam			break;
3944127649Ssam		}
3945127649Ssam	}
3946127649Ssam
3947173275Ssam	if (wpa || verbose) {
3948178354Ssam		if (get80211val(s, IEEE80211_IOC_WPS, &val) != -1) {
3949178354Ssam			if (val)
3950178354Ssam				LINE_CHECK("wps");
3951178354Ssam			else if (verbose)
3952178354Ssam				LINE_CHECK("-wps");
3953178354Ssam		}
3954178354Ssam		if (get80211val(s, IEEE80211_IOC_TSN, &val) != -1) {
3955178354Ssam			if (val)
3956178354Ssam				LINE_CHECK("tsn");
3957178354Ssam			else if (verbose)
3958178354Ssam				LINE_CHECK("-tsn");
3959178354Ssam		}
3960173275Ssam		if (ioctl(s, IEEE80211_IOC_COUNTERMEASURES, &val) != -1) {
3961173275Ssam			if (val)
3962173275Ssam				LINE_CHECK("countermeasures");
3963173275Ssam			else if (verbose)
3964173275Ssam				LINE_CHECK("-countermeasures");
3965173275Ssam		}
3966178354Ssam#if 0
3967178354Ssam		/* XXX not interesting with WPA done in user space */
3968178354Ssam		ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
3969178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3970178354Ssam		}
3971178354Ssam
3972178354Ssam		ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
3973178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3974178354Ssam			LINE_CHECK("mcastcipher ");
3975178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
3976178354Ssam			spacer = ' ';
3977178354Ssam		}
3978178354Ssam
3979178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
3980178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3981178354Ssam			LINE_CHECK("ucastcipher ");
3982178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
3983178354Ssam		}
3984178354Ssam
3985178354Ssam		if (wpa & 2) {
3986178354Ssam			ireq.i_type = IEEE80211_IOC_RSNCAPS;
3987178354Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1) {
3988178354Ssam				LINE_CHECK("RSN caps 0x%x", ireq.i_val);
3989178354Ssam				spacer = ' ';
3990178354Ssam			}
3991178354Ssam		}
3992178354Ssam
3993178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
3994178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3995178354Ssam		}
3996178354Ssam#endif
3997173275Ssam	}
3998138593Ssam
3999173275Ssam	if (get80211val(s, IEEE80211_IOC_WEP, &wepmode) != -1 &&
4000173275Ssam	    wepmode != IEEE80211_WEP_NOSUP) {
4001173275Ssam		int firstkey;
4002173275Ssam
4003138718Ssam		switch (wepmode) {
4004173275Ssam		case IEEE80211_WEP_OFF:
4005173275Ssam			LINE_CHECK("privacy OFF");
4006173275Ssam			break;
4007173275Ssam		case IEEE80211_WEP_ON:
4008173275Ssam			LINE_CHECK("privacy ON");
4009173275Ssam			break;
4010173275Ssam		case IEEE80211_WEP_MIXED:
4011173275Ssam			LINE_CHECK("privacy MIXED");
4012173275Ssam			break;
4013173275Ssam		default:
4014173275Ssam			LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
4015173275Ssam			break;
401677218Sphk		}
401777218Sphk
401877218Sphk		/*
401977218Sphk		 * If we get here then we've got WEP support so we need
402077218Sphk		 * to print WEP status.
402191454Sbrooks		 */
402277218Sphk
4023173275Ssam		if (get80211val(s, IEEE80211_IOC_WEPTXKEY, &val) < 0) {
402477218Sphk			warn("WEP support, but no tx key!");
402577218Sphk			goto end;
402677218Sphk		}
4027173275Ssam		if (val != -1)
4028173275Ssam			LINE_CHECK("deftxkey %d", val+1);
4029138718Ssam		else if (wepmode != IEEE80211_WEP_OFF || verbose)
4030155931Ssam			LINE_CHECK("deftxkey UNDEF");
403177218Sphk
4032173275Ssam		if (get80211val(s, IEEE80211_IOC_NUMWEPKEYS, &num) < 0) {
403377218Sphk			warn("WEP support, but no NUMWEPKEYS support!");
403477218Sphk			goto end;
403577218Sphk		}
403677218Sphk
4037138593Ssam		firstkey = 1;
4038138593Ssam		for (i = 0; i < num; i++) {
4039138593Ssam			struct ieee80211req_key ik;
404077218Sphk
4041138593Ssam			memset(&ik, 0, sizeof(ik));
4042138593Ssam			ik.ik_keyix = i;
4043173275Ssam			if (get80211(s, IEEE80211_IOC_WPAKEY, &ik, sizeof(ik)) < 0) {
404477218Sphk				warn("WEP support, but can get keys!");
404577218Sphk				goto end;
404677218Sphk			}
4047138593Ssam			if (ik.ik_keylen != 0) {
4048138593Ssam				if (verbose)
4049138593Ssam					LINE_BREAK();
4050138593Ssam				printkey(&ik);
4051138593Ssam				firstkey = 0;
4052138593Ssam			}
4053138593Ssam		}
4054173275Ssamend:
4055173275Ssam		;
4056138593Ssam	}
4057138593Ssam
4058173275Ssam	if (get80211val(s, IEEE80211_IOC_POWERSAVE, &val) != -1 &&
4059173275Ssam	    val != IEEE80211_POWERSAVE_NOSUP ) {
4060173275Ssam		if (val != IEEE80211_POWERSAVE_OFF || verbose) {
4061173275Ssam			switch (val) {
4062173275Ssam			case IEEE80211_POWERSAVE_OFF:
4063173275Ssam				LINE_CHECK("powersavemode OFF");
4064173275Ssam				break;
4065173275Ssam			case IEEE80211_POWERSAVE_CAM:
4066173275Ssam				LINE_CHECK("powersavemode CAM");
4067173275Ssam				break;
4068173275Ssam			case IEEE80211_POWERSAVE_PSP:
4069173275Ssam				LINE_CHECK("powersavemode PSP");
4070173275Ssam				break;
4071173275Ssam			case IEEE80211_POWERSAVE_PSP_CAM:
4072173275Ssam				LINE_CHECK("powersavemode PSP-CAM");
4073173275Ssam				break;
4074138593Ssam			}
4075173275Ssam			if (get80211val(s, IEEE80211_IOC_POWERSAVESLEEP, &val) != -1)
4076173275Ssam				LINE_CHECK("powersavesleep %d", val);
4077138593Ssam		}
4078138593Ssam	}
4079138593Ssam
4080173275Ssam	if (get80211val(s, IEEE80211_IOC_TXPOWER, &val) != -1) {
4081173275Ssam		if (val & 1)
4082173275Ssam			LINE_CHECK("txpower %d.5", val/2);
4083173275Ssam		else
4084173275Ssam			LINE_CHECK("txpower %d", val/2);
4085173275Ssam	}
4086138593Ssam	if (verbose) {
4087173275Ssam		if (get80211val(s, IEEE80211_IOC_TXPOWMAX, &val) != -1)
4088173275Ssam			LINE_CHECK("txpowmax %.1f", val/2.);
4089138593Ssam	}
4090138593Ssam
4091178354Ssam	if (get80211val(s, IEEE80211_IOC_DOTD, &val) != -1) {
4092178354Ssam		if (val)
4093178354Ssam			LINE_CHECK("dotd");
4094178354Ssam		else if (verbose)
4095178354Ssam			LINE_CHECK("-dotd");
4096178354Ssam	}
4097178354Ssam
4098173275Ssam	if (get80211val(s, IEEE80211_IOC_RTSTHRESHOLD, &val) != -1) {
4099173275Ssam		if (val != IEEE80211_RTS_MAX || verbose)
4100173275Ssam			LINE_CHECK("rtsthreshold %d", val);
4101138593Ssam	}
4102138593Ssam
4103173275Ssam	if (get80211val(s, IEEE80211_IOC_FRAGTHRESHOLD, &val) != -1) {
4104173275Ssam		if (val != IEEE80211_FRAG_MAX || verbose)
4105173275Ssam			LINE_CHECK("fragthreshold %d", val);
4106170531Ssam	}
4107173275Ssam	if (opmode == IEEE80211_M_STA || verbose) {
4108173275Ssam		if (get80211val(s, IEEE80211_IOC_BMISSTHRESHOLD, &val) != -1) {
4109173275Ssam			if (val != IEEE80211_HWBMISS_MAX || verbose)
4110173275Ssam				LINE_CHECK("bmiss %d", val);
4111153354Ssam		}
4112153354Ssam	}
4113153354Ssam
4114178354Ssam	if (!verbose) {
4115178354Ssam		gettxparams(s);
4116178354Ssam		tp = &txparams.params[chan2mode(c)];
4117178354Ssam		printrate("ucastrate", tp->ucastrate,
4118178354Ssam		    IEEE80211_FIXED_RATE_NONE, IEEE80211_FIXED_RATE_NONE);
4119178354Ssam		printrate("mcastrate", tp->mcastrate, 2*1, 0x80|0);
4120178354Ssam		printrate("mgmtrate", tp->mgmtrate, 2*1, 0x80|0);
4121178354Ssam		if (tp->maxretry != 6)		/* XXX */
4122178354Ssam			LINE_CHECK("maxretry %d", tp->maxretry);
4123178354Ssam	} else {
4124178354Ssam		LINE_BREAK();
4125178354Ssam		list_txparams(s);
4126178354Ssam	}
4127170531Ssam
4128173275Ssam	bgscaninterval = -1;
4129173275Ssam	(void) get80211val(s, IEEE80211_IOC_BGSCAN_INTERVAL, &bgscaninterval);
4130173275Ssam
4131173275Ssam	if (get80211val(s, IEEE80211_IOC_SCANVALID, &val) != -1) {
4132173275Ssam		if (val != bgscaninterval || verbose)
4133173275Ssam			LINE_CHECK("scanvalid %u", val);
4134148416Ssam	}
4135148416Ssam
4136173275Ssam	bgscan = 0;
4137173275Ssam	if (get80211val(s, IEEE80211_IOC_BGSCAN, &bgscan) != -1) {
4138173275Ssam		if (bgscan)
4139170531Ssam			LINE_CHECK("bgscan");
4140170531Ssam		else if (verbose)
4141170531Ssam			LINE_CHECK("-bgscan");
4142160687Ssam	}
4143170531Ssam	if (bgscan || verbose) {
4144170531Ssam		if (bgscaninterval != -1)
4145170531Ssam			LINE_CHECK("bgscanintvl %u", bgscaninterval);
4146173275Ssam		if (get80211val(s, IEEE80211_IOC_BGSCAN_IDLE, &val) != -1)
4147173275Ssam			LINE_CHECK("bgscanidle %u", val);
4148178354Ssam		if (!verbose) {
4149178354Ssam			getroam(s);
4150178354Ssam			rp = &roamparams.params[chan2mode(c)];
4151178354Ssam			if (rp->rssi & 1)
4152178354Ssam				LINE_CHECK("roam:rssi %u.5", rp->rssi/2);
4153178354Ssam			else
4154178354Ssam				LINE_CHECK("roam:rssi %u", rp->rssi/2);
4155178354Ssam			LINE_CHECK("roam:rate %u", rp->rate/2);
4156178354Ssam		} else {
4157178354Ssam			LINE_BREAK();
4158178354Ssam			list_roam(s);
4159170531Ssam		}
4160170531Ssam	}
4161160687Ssam
4162165570Ssam	if (IEEE80211_IS_CHAN_ANYG(c) || verbose) {
4163173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREG, &val) != -1) {
4164173275Ssam			if (val)
4165155931Ssam				LINE_CHECK("pureg");
4166147795Ssam			else if (verbose)
4167155931Ssam				LINE_CHECK("-pureg");
4168147795Ssam		}
4169173275Ssam		if (get80211val(s, IEEE80211_IOC_PROTMODE, &val) != -1) {
4170173275Ssam			switch (val) {
4171173275Ssam			case IEEE80211_PROTMODE_OFF:
4172173275Ssam				LINE_CHECK("protmode OFF");
4173173275Ssam				break;
4174173275Ssam			case IEEE80211_PROTMODE_CTS:
4175173275Ssam				LINE_CHECK("protmode CTS");
4176173275Ssam				break;
4177173275Ssam			case IEEE80211_PROTMODE_RTSCTS:
4178173275Ssam				LINE_CHECK("protmode RTSCTS");
4179173275Ssam				break;
4180173275Ssam			default:
4181173275Ssam				LINE_CHECK("protmode UNKNOWN (0x%x)", val);
4182173275Ssam				break;
4183138593Ssam			}
4184138593Ssam		}
4185138593Ssam	}
4186138593Ssam
4187173275Ssam	if (IEEE80211_IS_CHAN_HT(c) || verbose) {
4188173275Ssam		gethtconf(s);
4189173275Ssam		switch (htconf & 3) {
4190173275Ssam		case 0:
4191173275Ssam		case 2:
4192173275Ssam			LINE_CHECK("-ht");
4193173275Ssam			break;
4194173275Ssam		case 1:
4195173275Ssam			LINE_CHECK("ht20");
4196173275Ssam			break;
4197173275Ssam		case 3:
4198173275Ssam			if (verbose)
4199173275Ssam				LINE_CHECK("ht");
4200173275Ssam			break;
4201173275Ssam		}
4202173275Ssam		if (get80211val(s, IEEE80211_IOC_HTCOMPAT, &val) != -1) {
4203173275Ssam			if (!val)
4204173275Ssam				LINE_CHECK("-htcompat");
4205173275Ssam			else if (verbose)
4206173275Ssam				LINE_CHECK("htcompat");
4207173275Ssam		}
4208173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU, &val) != -1) {
4209173275Ssam			switch (val) {
4210173275Ssam			case 0:
4211173275Ssam				LINE_CHECK("-ampdu");
4212173275Ssam				break;
4213173275Ssam			case 1:
4214173275Ssam				LINE_CHECK("ampdutx -ampdurx");
4215173275Ssam				break;
4216173275Ssam			case 2:
4217173275Ssam				LINE_CHECK("-ampdutx ampdurx");
4218173275Ssam				break;
4219173275Ssam			case 3:
4220173275Ssam				if (verbose)
4221173275Ssam					LINE_CHECK("ampdu");
4222173275Ssam				break;
4223173275Ssam			}
4224173275Ssam		}
4225173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_LIMIT, &val) != -1) {
4226173275Ssam			switch (val) {
4227173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_8K:
4228173275Ssam				LINE_CHECK("ampdulimit 8k");
4229173275Ssam				break;
4230173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_16K:
4231173275Ssam				LINE_CHECK("ampdulimit 16k");
4232173275Ssam				break;
4233173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_32K:
4234173275Ssam				LINE_CHECK("ampdulimit 32k");
4235173275Ssam				break;
4236173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_64K:
4237173275Ssam				LINE_CHECK("ampdulimit 64k");
4238173275Ssam				break;
4239173275Ssam			}
4240173275Ssam		}
4241173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_DENSITY, &val) != -1) {
4242173275Ssam			switch (val) {
4243173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_NA:
4244173275Ssam				if (verbose)
4245183260Ssam					LINE_CHECK("ampdudensity NA");
4246173275Ssam				break;
4247173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_025:
4248173275Ssam				LINE_CHECK("ampdudensity .25");
4249173275Ssam				break;
4250173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_05:
4251173275Ssam				LINE_CHECK("ampdudensity .5");
4252173275Ssam				break;
4253173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_1:
4254173275Ssam				LINE_CHECK("ampdudensity 1");
4255173275Ssam				break;
4256173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_2:
4257173275Ssam				LINE_CHECK("ampdudensity 2");
4258173275Ssam				break;
4259173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_4:
4260173275Ssam				LINE_CHECK("ampdudensity 4");
4261173275Ssam				break;
4262173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_8:
4263173275Ssam				LINE_CHECK("ampdudensity 8");
4264173275Ssam				break;
4265173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_16:
4266173275Ssam				LINE_CHECK("ampdudensity 16");
4267173275Ssam				break;
4268173275Ssam			}
4269173275Ssam		}
4270173275Ssam		if (get80211val(s, IEEE80211_IOC_AMSDU, &val) != -1) {
4271173275Ssam			switch (val) {
4272173275Ssam			case 0:
4273173275Ssam				LINE_CHECK("-amsdu");
4274173275Ssam				break;
4275173275Ssam			case 1:
4276173275Ssam				LINE_CHECK("amsdutx -amsdurx");
4277173275Ssam				break;
4278173275Ssam			case 2:
4279173275Ssam				LINE_CHECK("-amsdutx amsdurx");
4280173275Ssam				break;
4281173275Ssam			case 3:
4282173275Ssam				if (verbose)
4283173275Ssam					LINE_CHECK("amsdu");
4284173275Ssam				break;
4285173275Ssam			}
4286173275Ssam		}
4287173275Ssam		/* XXX amsdu limit */
4288173275Ssam		if (get80211val(s, IEEE80211_IOC_SHORTGI, &val) != -1) {
4289173275Ssam			if (val)
4290173275Ssam				LINE_CHECK("shortgi");
4291173275Ssam			else if (verbose)
4292173275Ssam				LINE_CHECK("-shortgi");
4293173275Ssam		}
4294173275Ssam		if (get80211val(s, IEEE80211_IOC_HTPROTMODE, &val) != -1) {
4295173275Ssam			if (val == IEEE80211_PROTMODE_OFF)
4296173275Ssam				LINE_CHECK("htprotmode OFF");
4297173275Ssam			else if (val != IEEE80211_PROTMODE_RTSCTS)
4298173275Ssam				LINE_CHECK("htprotmode UNKNOWN (0x%x)", val);
4299173275Ssam			else if (verbose)
4300173275Ssam				LINE_CHECK("htprotmode RTSCTS");
4301173275Ssam		}
4302173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREN, &val) != -1) {
4303173275Ssam			if (val)
4304173275Ssam				LINE_CHECK("puren");
4305173275Ssam			else if (verbose)
4306173275Ssam				LINE_CHECK("-puren");
4307173275Ssam		}
4308183261Ssam		if (get80211val(s, IEEE80211_IOC_SMPS, &val) != -1) {
4309183261Ssam			if (val == IEEE80211_HTCAP_SMPS_DYNAMIC)
4310183261Ssam				LINE_CHECK("smpsdyn");
4311183261Ssam			else if (val == IEEE80211_HTCAP_SMPS_ENA)
4312183261Ssam				LINE_CHECK("smps");
4313183261Ssam			else if (verbose)
4314183261Ssam				LINE_CHECK("-smps");
4315183261Ssam		}
4316183261Ssam		if (get80211val(s, IEEE80211_IOC_RIFS, &val) != -1) {
4317183261Ssam			if (val)
4318183261Ssam				LINE_CHECK("rifs");
4319183261Ssam			else if (verbose)
4320183261Ssam				LINE_CHECK("-rifs");
4321183261Ssam		}
4322173275Ssam	}
4323173275Ssam
4324173275Ssam	if (get80211val(s, IEEE80211_IOC_WME, &wme) != -1) {
4325138593Ssam		if (wme)
4326155931Ssam			LINE_CHECK("wme");
4327138593Ssam		else if (verbose)
4328155931Ssam			LINE_CHECK("-wme");
4329138593Ssam	} else
4330138593Ssam		wme = 0;
4331138593Ssam
4332173275Ssam	if (get80211val(s, IEEE80211_IOC_BURST, &val) != -1) {
4333173275Ssam		if (val)
4334155931Ssam			LINE_CHECK("burst");
4335153422Ssam		else if (verbose)
4336155931Ssam			LINE_CHECK("-burst");
4337153422Ssam	}
4338153422Ssam
4339173275Ssam	if (get80211val(s, IEEE80211_IOC_FF, &val) != -1) {
4340173275Ssam		if (val)
4341170531Ssam			LINE_CHECK("ff");
4342170531Ssam		else if (verbose)
4343170531Ssam			LINE_CHECK("-ff");
4344170531Ssam	}
4345173275Ssam	if (get80211val(s, IEEE80211_IOC_TURBOP, &val) != -1) {
4346173275Ssam		if (val)
4347170531Ssam			LINE_CHECK("dturbo");
4348170531Ssam		else if (verbose)
4349170531Ssam			LINE_CHECK("-dturbo");
4350170531Ssam	}
4351178354Ssam	if (get80211val(s, IEEE80211_IOC_DWDS, &val) != -1) {
4352178354Ssam		if (val)
4353178354Ssam			LINE_CHECK("dwds");
4354178354Ssam		else if (verbose)
4355178354Ssam			LINE_CHECK("-dwds");
4356178354Ssam	}
4357170531Ssam
4358138593Ssam	if (opmode == IEEE80211_M_HOSTAP) {
4359173275Ssam		if (get80211val(s, IEEE80211_IOC_HIDESSID, &val) != -1) {
4360173275Ssam			if (val)
4361168075Ssam				LINE_CHECK("hidessid");
4362138593Ssam			else if (verbose)
4363168075Ssam				LINE_CHECK("-hidessid");
4364138593Ssam		}
4365173275Ssam		if (get80211val(s, IEEE80211_IOC_APBRIDGE, &val) != -1) {
4366173275Ssam			if (!val)
4367155931Ssam				LINE_CHECK("-apbridge");
4368138593Ssam			else if (verbose)
4369155931Ssam				LINE_CHECK("apbridge");
4370138593Ssam		}
4371173275Ssam		if (get80211val(s, IEEE80211_IOC_DTIM_PERIOD, &val) != -1)
4372173275Ssam			LINE_CHECK("dtimperiod %u", val);
4373138593Ssam
4374173275Ssam		if (get80211val(s, IEEE80211_IOC_DOTH, &val) != -1) {
4375173275Ssam			if (!val)
4376170531Ssam				LINE_CHECK("-doth");
4377170531Ssam			else if (verbose)
4378170531Ssam				LINE_CHECK("doth");
4379170531Ssam		}
4380178354Ssam		if (get80211val(s, IEEE80211_IOC_DFS, &val) != -1) {
4381178354Ssam			if (!val)
4382178354Ssam				LINE_CHECK("-dfs");
4383178354Ssam			else if (verbose)
4384178354Ssam				LINE_CHECK("dfs");
4385178354Ssam		}
4386173275Ssam		if (get80211val(s, IEEE80211_IOC_INACTIVITY, &val) != -1) {
4387173275Ssam			if (!val)
4388173275Ssam				LINE_CHECK("-inact");
4389173275Ssam			else if (verbose)
4390173275Ssam				LINE_CHECK("inact");
4391173275Ssam		}
4392138593Ssam	} else {
4393173275Ssam		if (get80211val(s, IEEE80211_IOC_ROAMING, &val) != -1) {
4394173275Ssam			if (val != IEEE80211_ROAMING_AUTO || verbose) {
4395173275Ssam				switch (val) {
4396138593Ssam				case IEEE80211_ROAMING_DEVICE:
4397155931Ssam					LINE_CHECK("roaming DEVICE");
4398138593Ssam					break;
4399138593Ssam				case IEEE80211_ROAMING_AUTO:
4400155931Ssam					LINE_CHECK("roaming AUTO");
4401138593Ssam					break;
4402138593Ssam				case IEEE80211_ROAMING_MANUAL:
4403155931Ssam					LINE_CHECK("roaming MANUAL");
4404138593Ssam					break;
4405138593Ssam				default:
4406155931Ssam					LINE_CHECK("roaming UNKNOWN (0x%x)",
4407173275Ssam						val);
4408138593Ssam					break;
4409138593Ssam				}
4410138593Ssam			}
4411138593Ssam		}
4412138593Ssam	}
4413186101Ssam
4414186904Ssam	if (opmode == IEEE80211_M_AHDEMO) {
4415186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOT, &val) != -1)
4416186904Ssam			LINE_CHECK("tdmaslot %u", val);
4417186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTCNT, &val) != -1)
4418186904Ssam			LINE_CHECK("tdmaslotcnt %u", val);
4419186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTLEN, &val) != -1)
4420186904Ssam			LINE_CHECK("tdmaslotlen %u", val);
4421186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_BINTERVAL, &val) != -1)
4422186904Ssam			LINE_CHECK("tdmabintval %u", val);
4423186904Ssam	} else if (get80211val(s, IEEE80211_IOC_BEACON_INTERVAL, &val) != -1) {
4424173275Ssam		/* XXX default define not visible */
4425173275Ssam		if (val != 100 || verbose)
4426173275Ssam			LINE_CHECK("bintval %u", val);
4427138593Ssam	}
4428138593Ssam
4429138593Ssam	if (wme && verbose) {
4430138593Ssam		LINE_BREAK();
4431138593Ssam		list_wme(s);
4432138593Ssam	}
4433173275Ssam	LINE_BREAK();
4434173275Ssam}
4435138593Ssam
4436173275Ssamstatic int
4437173275Ssamget80211(int s, int type, void *data, int len)
4438173275Ssam{
4439173275Ssam	struct ieee80211req ireq;
4440138593Ssam
4441173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4442173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4443173275Ssam	ireq.i_type = type;
4444173275Ssam	ireq.i_data = data;
4445173275Ssam	ireq.i_len = len;
4446173275Ssam	return ioctl(s, SIOCG80211, &ireq);
4447173275Ssam}
4448138593Ssam
4449173275Ssamstatic int
4450173275Ssamget80211len(int s, int type, void *data, int len, int *plen)
4451173275Ssam{
4452173275Ssam	struct ieee80211req ireq;
4453138593Ssam
4454173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4455173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4456173275Ssam	ireq.i_type = type;
4457173275Ssam	ireq.i_len = len;
4458187801Ssam	assert(ireq.i_len == len);	/* NB: check for 16-bit truncation */
4459173275Ssam	ireq.i_data = data;
4460173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4461173275Ssam		return -1;
4462173275Ssam	*plen = ireq.i_len;
4463173275Ssam	return 0;
4464173275Ssam}
4465138593Ssam
4466173275Ssamstatic int
4467173275Ssamget80211val(int s, int type, int *val)
4468173275Ssam{
4469173275Ssam	struct ieee80211req ireq;
447077218Sphk
4471173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4472173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4473173275Ssam	ireq.i_type = type;
4474173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4475173275Ssam		return -1;
4476173275Ssam	*val = ireq.i_val;
4477173275Ssam	return 0;
447877218Sphk}
447977218Sphk
448077218Sphkstatic void
4481170531Ssamset80211(int s, int type, int val, int len, void *data)
448277218Sphk{
448377218Sphk	struct ieee80211req	ireq;
448477218Sphk
448577218Sphk	(void) memset(&ireq, 0, sizeof(ireq));
448677218Sphk	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
448777218Sphk	ireq.i_type = type;
448877218Sphk	ireq.i_val = val;
448977218Sphk	ireq.i_len = len;
4490187801Ssam	assert(ireq.i_len == len);	/* NB: check for 16-bit truncation */
449177218Sphk	ireq.i_data = data;
449291454Sbrooks	if (ioctl(s, SIOCS80211, &ireq) < 0)
449377218Sphk		err(1, "SIOCS80211");
449477218Sphk}
449577218Sphk
449677218Sphkstatic const char *
449777218Sphkget_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
449877218Sphk{
449977218Sphk	int len;
450077218Sphk	int hexstr;
450177218Sphk	u_int8_t *p;
450277218Sphk
450377218Sphk	len = *lenp;
450477218Sphk	p = buf;
450577218Sphk	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
450677218Sphk	if (hexstr)
450777218Sphk		val += 2;
450877218Sphk	for (;;) {
450977218Sphk		if (*val == '\0')
451077218Sphk			break;
451177218Sphk		if (sep != NULL && strchr(sep, *val) != NULL) {
451277218Sphk			val++;
451377218Sphk			break;
451477218Sphk		}
451577218Sphk		if (hexstr) {
4516127831Sphk			if (!isxdigit((u_char)val[0])) {
451777218Sphk				warnx("bad hexadecimal digits");
451877218Sphk				return NULL;
451977218Sphk			}
4520127831Sphk			if (!isxdigit((u_char)val[1])) {
4521127831Sphk				warnx("odd count hexadecimal digits");
4522127831Sphk				return NULL;
4523127831Sphk			}
452477218Sphk		}
4525127831Sphk		if (p >= buf + len) {
452677218Sphk			if (hexstr)
452777218Sphk				warnx("hexadecimal digits too long");
452877218Sphk			else
4529127831Sphk				warnx("string too long");
453077218Sphk			return NULL;
453177218Sphk		}
453277218Sphk		if (hexstr) {
453377218Sphk#define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
453477218Sphk			*p++ = (tohex((u_char)val[0]) << 4) |
453577218Sphk			    tohex((u_char)val[1]);
453677218Sphk#undef tohex
453777218Sphk			val += 2;
453877218Sphk		} else
453977218Sphk			*p++ = *val++;
454077218Sphk	}
454177218Sphk	len = p - buf;
454277218Sphk	/* The string "-" is treated as the empty string. */
4543165045Ssam	if (!hexstr && len == 1 && buf[0] == '-') {
454477218Sphk		len = 0;
4545165045Ssam		memset(buf, 0, *lenp);
4546165045Ssam	} else if (len < *lenp)
454777218Sphk		memset(p, 0, *lenp - len);
454877218Sphk	*lenp = len;
454977218Sphk	return val;
455077218Sphk}
455177218Sphk
455277218Sphkstatic void
455377218Sphkprint_string(const u_int8_t *buf, int len)
455477218Sphk{
455577218Sphk	int i;
455677218Sphk	int hasspc;
455777218Sphk
455877218Sphk	i = 0;
455977218Sphk	hasspc = 0;
456091454Sbrooks	for (; i < len; i++) {
456177218Sphk		if (!isprint(buf[i]) && buf[i] != '\0')
456277218Sphk			break;
456377218Sphk		if (isspace(buf[i]))
456477218Sphk			hasspc++;
456577218Sphk	}
456677218Sphk	if (i == len) {
456777218Sphk		if (hasspc || len == 0 || buf[0] == '\0')
456877218Sphk			printf("\"%.*s\"", len, buf);
456977218Sphk		else
457077218Sphk			printf("%.*s", len, buf);
457177218Sphk	} else {
457277218Sphk		printf("0x");
457377218Sphk		for (i = 0; i < len; i++)
457477218Sphk			printf("%02x", buf[i]);
457577218Sphk	}
457677218Sphk}
457777218Sphk
4578178354Ssam/*
4579178354Ssam * Virtual AP cloning support.
4580178354Ssam */
4581178354Ssamstatic struct ieee80211_clone_params params = {
4582178354Ssam	.icp_opmode	= IEEE80211_M_STA,	/* default to station mode */
4583178354Ssam};
4584178354Ssam
4585178354Ssamstatic void
4586178354Ssamwlan_create(int s, struct ifreq *ifr)
4587178354Ssam{
4588178354Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
4589178354Ssam
4590178354Ssam	if (params.icp_parent[0] == '\0')
4591178354Ssam		errx(1, "must specify a parent when creating a wlan device");
4592178354Ssam	if (params.icp_opmode == IEEE80211_M_WDS &&
4593178354Ssam	    memcmp(params.icp_bssid, zerobssid, sizeof(zerobssid)) == 0)
4594178354Ssam		errx(1, "no bssid specified for WDS (use wlanbssid)");
4595178354Ssam	ifr->ifr_data = (caddr_t) &params;
4596178354Ssam	if (ioctl(s, SIOCIFCREATE2, ifr) < 0)
4597178354Ssam		err(1, "SIOCIFCREATE2");
4598178354Ssam}
4599178354Ssam
4600178354Ssamstatic
4601178354SsamDECL_CMD_FUNC(set80211clone_wlandev, arg, d)
4602178354Ssam{
4603178354Ssam	strlcpy(params.icp_parent, arg, IFNAMSIZ);
4604178354Ssam	clone_setcallback(wlan_create);
4605178354Ssam}
4606178354Ssam
4607178354Ssamstatic
4608178354SsamDECL_CMD_FUNC(set80211clone_wlanbssid, arg, d)
4609178354Ssam{
4610178354Ssam	const struct ether_addr *ea;
4611178354Ssam
4612178354Ssam	ea = ether_aton(arg);
4613178354Ssam	if (ea == NULL)
4614178354Ssam		errx(1, "%s: cannot parse bssid", arg);
4615178354Ssam	memcpy(params.icp_bssid, ea->octet, IEEE80211_ADDR_LEN);
4616178354Ssam	clone_setcallback(wlan_create);
4617178354Ssam}
4618178354Ssam
4619178354Ssamstatic
4620178354SsamDECL_CMD_FUNC(set80211clone_wlanaddr, arg, d)
4621178354Ssam{
4622178354Ssam	const struct ether_addr *ea;
4623178354Ssam
4624178354Ssam	ea = ether_aton(arg);
4625178354Ssam	if (ea == NULL)
4626178354Ssam		errx(1, "%s: cannot parse addres", arg);
4627178354Ssam	memcpy(params.icp_macaddr, ea->octet, IEEE80211_ADDR_LEN);
4628178354Ssam	params.icp_flags |= IEEE80211_CLONE_MACADDR;
4629178354Ssam	clone_setcallback(wlan_create);
4630178354Ssam}
4631178354Ssam
4632178354Ssamstatic
4633178354SsamDECL_CMD_FUNC(set80211clone_wlanmode, arg, d)
4634178354Ssam{
4635178354Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
4636178354Ssam	if (iseq(arg, "sta"))
4637178354Ssam		params.icp_opmode = IEEE80211_M_STA;
4638178354Ssam	else if (iseq(arg, "ahdemo") || iseq(arg, "adhoc-demo"))
4639178354Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
4640178354Ssam	else if (iseq(arg, "ibss") || iseq(arg, "adhoc"))
4641178354Ssam		params.icp_opmode = IEEE80211_M_IBSS;
4642178354Ssam	else if (iseq(arg, "ap") || iseq(arg, "host"))
4643178354Ssam		params.icp_opmode = IEEE80211_M_HOSTAP;
4644178354Ssam	else if (iseq(arg, "wds"))
4645178354Ssam		params.icp_opmode = IEEE80211_M_WDS;
4646178354Ssam	else if (iseq(arg, "monitor"))
4647178354Ssam		params.icp_opmode = IEEE80211_M_MONITOR;
4648186904Ssam	else if (iseq(arg, "tdma")) {
4649186904Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
4650186904Ssam		params.icp_flags |= IEEE80211_CLONE_TDMA;
4651186904Ssam	} else
4652178354Ssam		errx(1, "Don't know to create %s for %s", arg, name);
4653178354Ssam	clone_setcallback(wlan_create);
4654178354Ssam#undef iseq
4655178354Ssam}
4656178354Ssam
4657178354Ssamstatic void
4658178354Ssamset80211clone_beacons(const char *val, int d, int s, const struct afswtch *rafp)
4659178354Ssam{
4660178354Ssam	/* NB: inverted sense */
4661178354Ssam	if (d)
4662178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_NOBEACONS;
4663178354Ssam	else
4664178354Ssam		params.icp_flags |= IEEE80211_CLONE_NOBEACONS;
4665178354Ssam	clone_setcallback(wlan_create);
4666178354Ssam}
4667178354Ssam
4668178354Ssamstatic void
4669178354Ssamset80211clone_bssid(const char *val, int d, int s, const struct afswtch *rafp)
4670178354Ssam{
4671178354Ssam	if (d)
4672178354Ssam		params.icp_flags |= IEEE80211_CLONE_BSSID;
4673178354Ssam	else
4674178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_BSSID;
4675178354Ssam	clone_setcallback(wlan_create);
4676178354Ssam}
4677178354Ssam
4678178354Ssamstatic void
4679178354Ssamset80211clone_wdslegacy(const char *val, int d, int s, const struct afswtch *rafp)
4680178354Ssam{
4681178354Ssam	if (d)
4682178354Ssam		params.icp_flags |= IEEE80211_CLONE_WDSLEGACY;
4683178354Ssam	else
4684178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_WDSLEGACY;
4685178354Ssam	clone_setcallback(wlan_create);
4686178354Ssam}
4687178354Ssam
4688138593Ssamstatic struct cmd ieee80211_cmds[] = {
4689138593Ssam	DEF_CMD_ARG("ssid",		set80211ssid),
4690138593Ssam	DEF_CMD_ARG("nwid",		set80211ssid),
4691138593Ssam	DEF_CMD_ARG("stationname",	set80211stationname),
4692138593Ssam	DEF_CMD_ARG("station",		set80211stationname),	/* BSD/OS */
4693138593Ssam	DEF_CMD_ARG("channel",		set80211channel),
4694138593Ssam	DEF_CMD_ARG("authmode",		set80211authmode),
4695138593Ssam	DEF_CMD_ARG("powersavemode",	set80211powersavemode),
4696138593Ssam	DEF_CMD("powersave",	1,	set80211powersave),
4697138593Ssam	DEF_CMD("-powersave",	0,	set80211powersave),
4698138593Ssam	DEF_CMD_ARG("powersavesleep", 	set80211powersavesleep),
4699138593Ssam	DEF_CMD_ARG("wepmode",		set80211wepmode),
4700138593Ssam	DEF_CMD("wep",		1,	set80211wep),
4701138593Ssam	DEF_CMD("-wep",		0,	set80211wep),
4702139493Ssam	DEF_CMD_ARG("deftxkey",		set80211weptxkey),
4703138593Ssam	DEF_CMD_ARG("weptxkey",		set80211weptxkey),
4704138593Ssam	DEF_CMD_ARG("wepkey",		set80211wepkey),
4705138593Ssam	DEF_CMD_ARG("nwkey",		set80211nwkey),		/* NetBSD */
4706138593Ssam	DEF_CMD("-nwkey",	0,	set80211wep),		/* NetBSD */
4707138593Ssam	DEF_CMD_ARG("rtsthreshold",	set80211rtsthreshold),
4708138593Ssam	DEF_CMD_ARG("protmode",		set80211protmode),
4709138593Ssam	DEF_CMD_ARG("txpower",		set80211txpower),
4710138593Ssam	DEF_CMD_ARG("roaming",		set80211roaming),
4711138593Ssam	DEF_CMD("wme",		1,	set80211wme),
4712138593Ssam	DEF_CMD("-wme",		0,	set80211wme),
4713178354Ssam	DEF_CMD("wmm",		1,	set80211wme),
4714178354Ssam	DEF_CMD("-wmm",		0,	set80211wme),
4715138593Ssam	DEF_CMD("hidessid",	1,	set80211hidessid),
4716138593Ssam	DEF_CMD("-hidessid",	0,	set80211hidessid),
4717138593Ssam	DEF_CMD("apbridge",	1,	set80211apbridge),
4718138593Ssam	DEF_CMD("-apbridge",	0,	set80211apbridge),
4719138593Ssam	DEF_CMD_ARG("chanlist",		set80211chanlist),
4720138593Ssam	DEF_CMD_ARG("bssid",		set80211bssid),
4721138593Ssam	DEF_CMD_ARG("ap",		set80211bssid),
4722138593Ssam	DEF_CMD("scan",	0,		set80211scan),
4723138593Ssam	DEF_CMD_ARG("list",		set80211list),
4724138593Ssam	DEF_CMD_ARG2("cwmin",		set80211cwmin),
4725138593Ssam	DEF_CMD_ARG2("cwmax",		set80211cwmax),
4726138593Ssam	DEF_CMD_ARG2("aifs",		set80211aifs),
4727138593Ssam	DEF_CMD_ARG2("txoplimit",	set80211txoplimit),
4728148621Ssam	DEF_CMD_ARG("acm",		set80211acm),
4729148621Ssam	DEF_CMD_ARG("-acm",		set80211noacm),
4730148621Ssam	DEF_CMD_ARG("ack",		set80211ackpolicy),
4731148621Ssam	DEF_CMD_ARG("-ack",		set80211noackpolicy),
4732138593Ssam	DEF_CMD_ARG2("bss:cwmin",	set80211bsscwmin),
4733138593Ssam	DEF_CMD_ARG2("bss:cwmax",	set80211bsscwmax),
4734138593Ssam	DEF_CMD_ARG2("bss:aifs",	set80211bssaifs),
4735138593Ssam	DEF_CMD_ARG2("bss:txoplimit",	set80211bsstxoplimit),
4736138593Ssam	DEF_CMD_ARG("dtimperiod",	set80211dtimperiod),
4737138593Ssam	DEF_CMD_ARG("bintval",		set80211bintval),
4738138593Ssam	DEF_CMD("mac:open",	IEEE80211_MACCMD_POLICY_OPEN,	set80211maccmd),
4739138593Ssam	DEF_CMD("mac:allow",	IEEE80211_MACCMD_POLICY_ALLOW,	set80211maccmd),
4740138593Ssam	DEF_CMD("mac:deny",	IEEE80211_MACCMD_POLICY_DENY,	set80211maccmd),
4741178354Ssam	DEF_CMD("mac:radius",	IEEE80211_MACCMD_POLICY_RADIUS,	set80211maccmd),
4742138593Ssam	DEF_CMD("mac:flush",	IEEE80211_MACCMD_FLUSH,		set80211maccmd),
4743138593Ssam	DEF_CMD("mac:detach",	IEEE80211_MACCMD_DETACH,	set80211maccmd),
4744138593Ssam	DEF_CMD_ARG("mac:add",		set80211addmac),
4745138593Ssam	DEF_CMD_ARG("mac:del",		set80211delmac),
4746138593Ssam	DEF_CMD_ARG("mac:kick",		set80211kickmac),
4747147795Ssam	DEF_CMD("pureg",	1,	set80211pureg),
4748147795Ssam	DEF_CMD("-pureg",	0,	set80211pureg),
4749170531Ssam	DEF_CMD("ff",		1,	set80211fastframes),
4750170531Ssam	DEF_CMD("-ff",		0,	set80211fastframes),
4751170531Ssam	DEF_CMD("dturbo",	1,	set80211dturbo),
4752170531Ssam	DEF_CMD("-dturbo",	0,	set80211dturbo),
4753170531Ssam	DEF_CMD("bgscan",	1,	set80211bgscan),
4754170531Ssam	DEF_CMD("-bgscan",	0,	set80211bgscan),
4755170531Ssam	DEF_CMD_ARG("bgscanidle",	set80211bgscanidle),
4756170531Ssam	DEF_CMD_ARG("bgscanintvl",	set80211bgscanintvl),
4757170531Ssam	DEF_CMD_ARG("scanvalid",	set80211scanvalid),
4758178354Ssam	DEF_CMD_ARG("roam:rssi",	set80211roamrssi),
4759178354Ssam	DEF_CMD_ARG("roam:rate",	set80211roamrate),
4760153354Ssam	DEF_CMD_ARG("mcastrate",	set80211mcastrate),
4761178354Ssam	DEF_CMD_ARG("ucastrate",	set80211ucastrate),
4762178354Ssam	DEF_CMD_ARG("mgtrate",		set80211mgtrate),
4763178354Ssam	DEF_CMD_ARG("mgmtrate",		set80211mgtrate),
4764178354Ssam	DEF_CMD_ARG("maxretry",		set80211maxretry),
4765148416Ssam	DEF_CMD_ARG("fragthreshold",	set80211fragthreshold),
4766153422Ssam	DEF_CMD("burst",	1,	set80211burst),
4767153422Ssam	DEF_CMD("-burst",	0,	set80211burst),
4768160687Ssam	DEF_CMD_ARG("bmiss",		set80211bmissthreshold),
4769160687Ssam	DEF_CMD_ARG("bmissthreshold",	set80211bmissthreshold),
4770173275Ssam	DEF_CMD("shortgi",	1,	set80211shortgi),
4771173275Ssam	DEF_CMD("-shortgi",	0,	set80211shortgi),
4772173275Ssam	DEF_CMD("ampdurx",	2,	set80211ampdu),
4773173275Ssam	DEF_CMD("-ampdurx",	-2,	set80211ampdu),
4774173275Ssam	DEF_CMD("ampdutx",	1,	set80211ampdu),
4775173275Ssam	DEF_CMD("-ampdutx",	-1,	set80211ampdu),
4776173275Ssam	DEF_CMD("ampdu",	3,	set80211ampdu),		/* NB: tx+rx */
4777173275Ssam	DEF_CMD("-ampdu",	-3,	set80211ampdu),
4778173275Ssam	DEF_CMD_ARG("ampdulimit",	set80211ampdulimit),
4779173275Ssam	DEF_CMD_ARG("ampdudensity",	set80211ampdudensity),
4780173275Ssam	DEF_CMD("amsdurx",	2,	set80211amsdu),
4781173275Ssam	DEF_CMD("-amsdurx",	-2,	set80211amsdu),
4782173275Ssam	DEF_CMD("amsdutx",	1,	set80211amsdu),
4783173275Ssam	DEF_CMD("-amsdutx",	-1,	set80211amsdu),
4784173275Ssam	DEF_CMD("amsdu",	3,	set80211amsdu),		/* NB: tx+rx */
4785173275Ssam	DEF_CMD("-amsdu",	-3,	set80211amsdu),
4786173275Ssam	DEF_CMD_ARG("amsdulimit",	set80211amsdulimit),
4787173275Ssam	DEF_CMD("puren",	1,	set80211puren),
4788173275Ssam	DEF_CMD("-puren",	0,	set80211puren),
4789170531Ssam	DEF_CMD("doth",		1,	set80211doth),
4790170531Ssam	DEF_CMD("-doth",	0,	set80211doth),
4791178354Ssam	DEF_CMD("dfs",		1,	set80211dfs),
4792178354Ssam	DEF_CMD("-dfs",		0,	set80211dfs),
4793173275Ssam	DEF_CMD("htcompat",	1,	set80211htcompat),
4794173275Ssam	DEF_CMD("-htcompat",	0,	set80211htcompat),
4795178354Ssam	DEF_CMD("dwds",		1,	set80211dwds),
4796178354Ssam	DEF_CMD("-dwds",	0,	set80211dwds),
4797173275Ssam	DEF_CMD("inact",	1,	set80211inact),
4798173275Ssam	DEF_CMD("-inact",	0,	set80211inact),
4799178354Ssam	DEF_CMD("tsn",		1,	set80211tsn),
4800178354Ssam	DEF_CMD("-tsn",		0,	set80211tsn),
4801178354Ssam	DEF_CMD_ARG("regdomain",	set80211regdomain),
4802178354Ssam	DEF_CMD_ARG("country",		set80211country),
4803178354Ssam	DEF_CMD("indoor",	'I',	set80211location),
4804178354Ssam	DEF_CMD("-indoor",	'O',	set80211location),
4805178354Ssam	DEF_CMD("outdoor",	'O',	set80211location),
4806178354Ssam	DEF_CMD("-outdoor",	'I',	set80211location),
4807178354Ssam	DEF_CMD("anywhere",	' ',	set80211location),
4808178354Ssam	DEF_CMD("ecm",		1,	set80211ecm),
4809178354Ssam	DEF_CMD("-ecm",		0,	set80211ecm),
4810178354Ssam	DEF_CMD("dotd",		1,	set80211dotd),
4811178354Ssam	DEF_CMD("-dotd",	0,	set80211dotd),
4812173275Ssam	DEF_CMD_ARG("htprotmode",	set80211htprotmode),
4813173275Ssam	DEF_CMD("ht20",		1,	set80211htconf),
4814173275Ssam	DEF_CMD("-ht20",	0,	set80211htconf),
4815173275Ssam	DEF_CMD("ht40",		3,	set80211htconf),	/* NB: 20+40 */
4816173275Ssam	DEF_CMD("-ht40",	0,	set80211htconf),
4817173275Ssam	DEF_CMD("ht",		3,	set80211htconf),	/* NB: 20+40 */
4818173275Ssam	DEF_CMD("-ht",		0,	set80211htconf),
4819183261Ssam	DEF_CMD("rifs",		1,	set80211rifs),
4820183261Ssam	DEF_CMD("-rifs",	0,	set80211rifs),
4821183261Ssam	DEF_CMD("smps",		IEEE80211_HTCAP_SMPS_ENA,	set80211smps),
4822183261Ssam	DEF_CMD("smpsdyn",	IEEE80211_HTCAP_SMPS_DYNAMIC,	set80211smps),
4823183261Ssam	DEF_CMD("-smps",	IEEE80211_HTCAP_SMPS_OFF,	set80211smps),
4824178354Ssam	/* XXX for testing */
4825178354Ssam	DEF_CMD_ARG("chanswitch",	set80211chanswitch),
4826178354Ssam
4827186904Ssam	DEF_CMD_ARG("tdmaslot",		set80211tdmaslot),
4828186904Ssam	DEF_CMD_ARG("tdmaslotcnt",	set80211tdmaslotcnt),
4829186904Ssam	DEF_CMD_ARG("tdmaslotlen",	set80211tdmaslotlen),
4830186904Ssam	DEF_CMD_ARG("tdmabintval",	set80211tdmabintval),
4831186904Ssam
4832178354Ssam	/* vap cloning support */
4833178354Ssam	DEF_CLONE_CMD_ARG("wlanaddr",	set80211clone_wlanaddr),
4834178354Ssam	DEF_CLONE_CMD_ARG("wlanbssid",	set80211clone_wlanbssid),
4835178354Ssam	DEF_CLONE_CMD_ARG("wlandev",	set80211clone_wlandev),
4836178354Ssam	DEF_CLONE_CMD_ARG("wlanmode",	set80211clone_wlanmode),
4837178354Ssam	DEF_CLONE_CMD("beacons", 1,	set80211clone_beacons),
4838178354Ssam	DEF_CLONE_CMD("-beacons", 0,	set80211clone_beacons),
4839178354Ssam	DEF_CLONE_CMD("bssid",	1,	set80211clone_bssid),
4840178354Ssam	DEF_CLONE_CMD("-bssid",	0,	set80211clone_bssid),
4841178354Ssam	DEF_CLONE_CMD("wdslegacy", 1,	set80211clone_wdslegacy),
4842178354Ssam	DEF_CLONE_CMD("-wdslegacy", 0,	set80211clone_wdslegacy),
4843138593Ssam};
4844138593Ssamstatic struct afswtch af_ieee80211 = {
4845138593Ssam	.af_name	= "af_ieee80211",
4846138593Ssam	.af_af		= AF_UNSPEC,
4847139494Ssam	.af_other_status = ieee80211_status,
4848138593Ssam};
4849138593Ssam
4850138593Ssamstatic __constructor void
4851138593Ssamieee80211_ctor(void)
4852138593Ssam{
4853138593Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
4854138593Ssam	int i;
4855138593Ssam
4856138593Ssam	for (i = 0; i < N(ieee80211_cmds);  i++)
4857138593Ssam		cmd_register(&ieee80211_cmds[i]);
4858138593Ssam	af_register(&af_ieee80211);
4859138593Ssam#undef N
4860138593Ssam}
4861