ifieee80211.c revision 188258
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 188258 2009-02-07 01:12:51Z sam $
2877218Sphk */
2977218Sphk
3077218Sphk/*-
3177218Sphk * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
3277218Sphk * All rights reserved.
3377218Sphk *
3477218Sphk * This code is derived from software contributed to The NetBSD Foundation
3577218Sphk * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
3677218Sphk * NASA Ames Research Center.
3777218Sphk *
3877218Sphk * Redistribution and use in source and binary forms, with or without
3977218Sphk * modification, are permitted provided that the following conditions
4077218Sphk * are met:
4177218Sphk * 1. Redistributions of source code must retain the above copyright
4277218Sphk *    notice, this list of conditions and the following disclaimer.
4377218Sphk * 2. Redistributions in binary form must reproduce the above copyright
4477218Sphk *    notice, this list of conditions and the following disclaimer in the
4577218Sphk *    documentation and/or other materials provided with the distribution.
4677218Sphk * 3. All advertising materials mentioning features or use of this software
4777218Sphk *    must display the following acknowledgement:
4877218Sphk *	This product includes software developed by the NetBSD
4977218Sphk *	Foundation, Inc. and its contributors.
5077218Sphk * 4. Neither the name of The NetBSD Foundation nor the names of its
5177218Sphk *    contributors may be used to endorse or promote products derived
5277218Sphk *    from this software without specific prior written permission.
5377218Sphk *
5477218Sphk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
5577218Sphk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
5677218Sphk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5777218Sphk * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
5877218Sphk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5977218Sphk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
6077218Sphk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
6177218Sphk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
6277218Sphk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
6377218Sphk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
6477218Sphk * POSSIBILITY OF SUCH DAMAGE.
6577218Sphk */
6677218Sphk
6777218Sphk#include <sys/param.h>
6877218Sphk#include <sys/ioctl.h>
6977218Sphk#include <sys/socket.h>
7077218Sphk#include <sys/sysctl.h>
7177218Sphk#include <sys/time.h>
7277218Sphk
7377218Sphk#include <net/ethernet.h>
7477218Sphk#include <net/if.h>
7577218Sphk#include <net/if_dl.h>
7677218Sphk#include <net/if_types.h>
77138593Ssam#include <net/if_media.h>
7877218Sphk#include <net/route.h>
79138593Ssam
80116957Ssam#include <net80211/ieee80211_ioctl.h>
8177218Sphk
82187801Ssam#include <assert.h>
8377218Sphk#include <ctype.h>
8477218Sphk#include <err.h>
8577218Sphk#include <errno.h>
8677218Sphk#include <fcntl.h>
87146873Sjhb#include <inttypes.h>
8877218Sphk#include <stdio.h>
8977218Sphk#include <stdlib.h>
9077218Sphk#include <string.h>
9177218Sphk#include <unistd.h>
92155931Ssam#include <stdarg.h>
93173275Ssam#include <stddef.h>		/* NB: for offsetof */
9477218Sphk
9577218Sphk#include "ifconfig.h"
96178354Ssam#include "regdomain.h"
9777218Sphk
98178354Ssam#ifndef IEEE80211_FIXED_RATE_NONE
99178354Ssam#define	IEEE80211_FIXED_RATE_NONE	0xff
100178354Ssam#endif
101178354Ssam
102178354Ssam/* XXX need these publicly defined or similar */
103178354Ssam#ifndef IEEE80211_NODE_AUTH
104178354Ssam#define	IEEE80211_NODE_AUTH	0x0001		/* authorized for data */
105178354Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
106178354Ssam#define	IEEE80211_NODE_ERP	0x0004		/* ERP enabled */
107178354Ssam#define	IEEE80211_NODE_PWR_MGT	0x0010		/* power save mode enabled */
108178354Ssam#define	IEEE80211_NODE_HT	0x0040		/* HT enabled */
109178354Ssam#define	IEEE80211_NODE_HTCOMPAT	0x0080		/* HT setup w/ vendor OUI's */
110178354Ssam#define	IEEE80211_NODE_WPS	0x0100		/* WPS association */
111178354Ssam#define	IEEE80211_NODE_TSN	0x0200		/* TSN association */
112183261Ssam#define	IEEE80211_NODE_AMPDU_RX	0x0400		/* AMPDU rx enabled */
113183261Ssam#define	IEEE80211_NODE_AMPDU_TX	0x0800		/* AMPDU tx enabled */
114183261Ssam#define	IEEE80211_NODE_MIMO_PS	0x1000		/* MIMO power save enabled */
115183261Ssam#define	IEEE80211_NODE_MIMO_RTS	0x2000		/* send RTS in MIMO PS */
116183261Ssam#define	IEEE80211_NODE_RIFS	0x4000		/* RIFS enabled */
117178354Ssam#endif
118178354Ssam
119187801Ssam#define	MAXCHAN	1536		/* max 1.5K channels */
120187801Ssam
121173275Ssam#define	MAXCOL	78
122173275Ssamstatic	int col;
123173275Ssamstatic	char spacer;
124173275Ssam
125173275Ssamstatic void LINE_INIT(char c);
126173275Ssamstatic void LINE_BREAK(void);
127173275Ssamstatic void LINE_CHECK(const char *fmt, ...);
128173275Ssam
129178354Ssamstatic const char *modename[] = {
130178354Ssam	"auto", "11a", "11b", "11g", "fh", "turboA", "turboG",
131178354Ssam	"sturbo", "11na", "11ng"
132173275Ssam};
133173275Ssam
134178354Ssamstatic void set80211(int s, int type, int val, int len, void *data);
135173275Ssamstatic int get80211(int s, int type, void *data, int len);
136173275Ssamstatic int get80211len(int s, int type, void *data, int len, int *plen);
137173275Ssamstatic int get80211val(int s, int type, int *val);
13877218Sphkstatic const char *get_string(const char *val, const char *sep,
13977218Sphk    u_int8_t *buf, int *lenp);
14077218Sphkstatic void print_string(const u_int8_t *buf, int len);
141178354Ssamstatic void print_regdomain(const struct ieee80211_regdomain *, int);
142178354Ssamstatic void print_channels(int, const struct ieee80211req_chaninfo *,
143178354Ssam    int allchans, int verbose);
144178354Ssamstatic void regdomain_makechannels(struct ieee80211_regdomain_req *,
145178354Ssam    const struct ieee80211_devcaps_req *);
14677218Sphk
147187801Ssamstatic struct ieee80211req_chaninfo *chaninfo;
148178354Ssamstatic struct ieee80211_regdomain regdomain;
149178354Ssamstatic int gotregdomain = 0;
150178354Ssamstatic struct ieee80211_roamparams_req roamparams;
151178354Ssamstatic int gotroam = 0;
152178354Ssamstatic struct ieee80211_txparams_req txparams;
153178354Ssamstatic int gottxparams = 0;
154173275Ssamstatic struct ieee80211_channel curchan;
155173275Ssamstatic int gotcurchan = 0;
156178354Ssamstatic struct ifmediareq *ifmr;
157173275Ssamstatic int htconf = 0;
158173275Ssamstatic	int gothtconf = 0;
159170531Ssam
160173275Ssamstatic void
161173275Ssamgethtconf(int s)
162173275Ssam{
163173275Ssam	if (gothtconf)
164173275Ssam		return;
165173275Ssam	if (get80211val(s, IEEE80211_IOC_HTCONF, &htconf) < 0)
166173275Ssam		warn("unable to get HT configuration information");
167173275Ssam	gothtconf = 1;
168173275Ssam}
169173275Ssam
170170531Ssam/*
171170531Ssam * Collect channel info from the kernel.  We use this (mostly)
172170531Ssam * to handle mapping between frequency and IEEE channel number.
173170531Ssam */
174170531Ssamstatic void
175170531Ssamgetchaninfo(int s)
176170531Ssam{
177187801Ssam	if (chaninfo != NULL)
178170531Ssam		return;
179187801Ssam	chaninfo = malloc(IEEE80211_CHANINFO_SIZE(MAXCHAN));
180187801Ssam	if (chaninfo == NULL)
181187801Ssam		errx(1, "no space for channel list");
182187801Ssam	if (get80211(s, IEEE80211_IOC_CHANINFO, chaninfo,
183187801Ssam	    IEEE80211_CHANINFO_SIZE(MAXCHAN)) < 0)
184187801Ssam		err(1, "unable to get channel information");
185170531Ssam	ifmr = ifmedia_getstate(s);
186173275Ssam	gethtconf(s);
187170531Ssam}
188170531Ssam
189178354Ssamstatic struct regdata *
190178354Ssamgetregdata(void)
191178354Ssam{
192178354Ssam	static struct regdata *rdp = NULL;
193178354Ssam	if (rdp == NULL) {
194178354Ssam		rdp = lib80211_alloc_regdata();
195178354Ssam		if (rdp == NULL)
196181198Ssam			errx(-1, "missing or corrupted regdomain database");
197178354Ssam	}
198178354Ssam	return rdp;
199178354Ssam}
200178354Ssam
201170531Ssam/*
202170531Ssam * Given the channel at index i with attributes from,
203170531Ssam * check if there is a channel with attributes to in
204170531Ssam * the channel table.  With suitable attributes this
205170531Ssam * allows the caller to look for promotion; e.g. from
206170531Ssam * 11b > 11g.
207170531Ssam */
208138593Ssamstatic int
209170531Ssamcanpromote(int i, int from, int to)
210170531Ssam{
211187801Ssam	const struct ieee80211_channel *fc = &chaninfo->ic_chans[i];
212170531Ssam	int j;
213170531Ssam
214170531Ssam	if ((fc->ic_flags & from) != from)
215170531Ssam		return i;
216170531Ssam	/* NB: quick check exploiting ordering of chans w/ same frequency */
217187801Ssam	if (i+1 < chaninfo->ic_nchans &&
218187801Ssam	    chaninfo->ic_chans[i+1].ic_freq == fc->ic_freq &&
219187801Ssam	    (chaninfo->ic_chans[i+1].ic_flags & to) == to)
220170531Ssam		return i+1;
221170531Ssam	/* brute force search in case channel list is not ordered */
222187801Ssam	for (j = 0; j < chaninfo->ic_nchans; j++) {
223187801Ssam		const struct ieee80211_channel *tc = &chaninfo->ic_chans[j];
224170531Ssam		if (j != i &&
225170531Ssam		    tc->ic_freq == fc->ic_freq && (tc->ic_flags & to) == to)
226170531Ssam		return j;
227170531Ssam	}
228170531Ssam	return i;
229170531Ssam}
230170531Ssam
231170531Ssam/*
232170531Ssam * Handle channel promotion.  When a channel is specified with
233170531Ssam * only a frequency we want to promote it to the ``best'' channel
234170531Ssam * available.  The channel list has separate entries for 11b, 11g,
235170531Ssam * 11a, and 11n[ga] channels so specifying a frequency w/o any
236170531Ssam * attributes requires we upgrade, e.g. from 11b -> 11g.  This
237170531Ssam * gets complicated when the channel is specified on the same
238170531Ssam * command line with a media request that constrains the available
239170531Ssam * channe list (e.g. mode 11a); we want to honor that to avoid
240170531Ssam * confusing behaviour.
241170531Ssam */
242170531Ssamstatic int
243170531Ssampromote(int i)
244170531Ssam{
245170531Ssam	/*
246170531Ssam	 * Query the current mode of the interface in case it's
247170531Ssam	 * constrained (e.g. to 11a).  We must do this carefully
248170531Ssam	 * as there may be a pending ifmedia request in which case
249170531Ssam	 * asking the kernel will give us the wrong answer.  This
250170531Ssam	 * is an unfortunate side-effect of the way ifconfig is
251170531Ssam	 * structure for modularity (yech).
252170531Ssam	 *
253170531Ssam	 * NB: ifmr is actually setup in getchaninfo (above); we
254170531Ssam	 *     assume it's called coincident with to this call so
255170531Ssam	 *     we have a ``current setting''; otherwise we must pass
256170531Ssam	 *     the socket descriptor down to here so we can make
257170531Ssam	 *     the ifmedia_getstate call ourselves.
258170531Ssam	 */
259170531Ssam	int chanmode = ifmr != NULL ? IFM_MODE(ifmr->ifm_current) : IFM_AUTO;
260170531Ssam
261170531Ssam	/* when ambiguous promote to ``best'' */
262170531Ssam	/* NB: we abitrarily pick HT40+ over HT40- */
263170531Ssam	if (chanmode != IFM_IEEE80211_11B)
264170531Ssam		i = canpromote(i, IEEE80211_CHAN_B, IEEE80211_CHAN_G);
265173275Ssam	if (chanmode != IFM_IEEE80211_11G && (htconf & 1)) {
266170531Ssam		i = canpromote(i, IEEE80211_CHAN_G,
267170531Ssam			IEEE80211_CHAN_G | IEEE80211_CHAN_HT20);
268173275Ssam		if (htconf & 2) {
269173275Ssam			i = canpromote(i, IEEE80211_CHAN_G,
270173275Ssam				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D);
271173275Ssam			i = canpromote(i, IEEE80211_CHAN_G,
272173275Ssam				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U);
273173275Ssam		}
274170531Ssam	}
275173275Ssam	if (chanmode != IFM_IEEE80211_11A && (htconf & 1)) {
276170531Ssam		i = canpromote(i, IEEE80211_CHAN_A,
277170531Ssam			IEEE80211_CHAN_A | IEEE80211_CHAN_HT20);
278173275Ssam		if (htconf & 2) {
279173275Ssam			i = canpromote(i, IEEE80211_CHAN_A,
280173275Ssam				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D);
281173275Ssam			i = canpromote(i, IEEE80211_CHAN_A,
282173275Ssam				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U);
283173275Ssam		}
284170531Ssam	}
285170531Ssam	return i;
286170531Ssam}
287170531Ssam
288170531Ssamstatic void
289170531Ssammapfreq(struct ieee80211_channel *chan, int freq, int flags)
290170531Ssam{
291170531Ssam	int i;
292170531Ssam
293187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
294187801Ssam		const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
295170531Ssam
296170531Ssam		if (c->ic_freq == freq && (c->ic_flags & flags) == flags) {
297170531Ssam			if (flags == 0) {
298170531Ssam				/* when ambiguous promote to ``best'' */
299187801Ssam				c = &chaninfo->ic_chans[promote(i)];
300170531Ssam			}
301170531Ssam			*chan = *c;
302170531Ssam			return;
303170531Ssam		}
304170531Ssam	}
305170531Ssam	errx(1, "unknown/undefined frequency %u/0x%x", freq, flags);
306170531Ssam}
307170531Ssam
308170531Ssamstatic void
309170531Ssammapchan(struct ieee80211_channel *chan, int ieee, int flags)
310170531Ssam{
311170531Ssam	int i;
312170531Ssam
313187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
314187801Ssam		const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
315170531Ssam
316170531Ssam		if (c->ic_ieee == ieee && (c->ic_flags & flags) == flags) {
317170531Ssam			if (flags == 0) {
318170531Ssam				/* when ambiguous promote to ``best'' */
319187801Ssam				c = &chaninfo->ic_chans[promote(i)];
320170531Ssam			}
321170531Ssam			*chan = *c;
322170531Ssam			return;
323170531Ssam		}
324170531Ssam	}
325173275Ssam	errx(1, "unknown/undefined channel number %d flags 0x%x", ieee, flags);
326170531Ssam}
327170531Ssam
328173275Ssamstatic const struct ieee80211_channel *
329173275Ssamgetcurchan(int s)
330173275Ssam{
331173275Ssam	if (gotcurchan)
332173275Ssam		return &curchan;
333173275Ssam	if (get80211(s, IEEE80211_IOC_CURCHAN, &curchan, sizeof(curchan)) < 0) {
334173275Ssam		int val;
335173275Ssam		/* fall back to legacy ioctl */
336173275Ssam		if (get80211val(s, IEEE80211_IOC_CHANNEL, &val) < 0)
337187801Ssam			err(-1, "cannot figure out current channel");
338173275Ssam		getchaninfo(s);
339173275Ssam		mapchan(&curchan, val, 0);
340173275Ssam	}
341173275Ssam	gotcurchan = 1;
342173275Ssam	return &curchan;
343173275Ssam}
344173275Ssam
345178354Ssamstatic enum ieee80211_phymode
346178354Ssamchan2mode(const struct ieee80211_channel *c)
347178354Ssam{
348178354Ssam	if (IEEE80211_IS_CHAN_HTA(c))
349178354Ssam		return IEEE80211_MODE_11NA;
350178354Ssam	if (IEEE80211_IS_CHAN_HTG(c))
351178354Ssam		return IEEE80211_MODE_11NG;
352178354Ssam	if (IEEE80211_IS_CHAN_108A(c))
353178354Ssam		return IEEE80211_MODE_TURBO_A;
354178354Ssam	if (IEEE80211_IS_CHAN_108G(c))
355178354Ssam		return IEEE80211_MODE_TURBO_G;
356178354Ssam	if (IEEE80211_IS_CHAN_ST(c))
357178354Ssam		return IEEE80211_MODE_STURBO_A;
358178354Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
359178354Ssam		return IEEE80211_MODE_FH;
360178354Ssam	if (IEEE80211_IS_CHAN_A(c))
361178354Ssam		return IEEE80211_MODE_11A;
362178354Ssam	if (IEEE80211_IS_CHAN_ANYG(c))
363178354Ssam		return IEEE80211_MODE_11G;
364178354Ssam	if (IEEE80211_IS_CHAN_B(c))
365178354Ssam		return IEEE80211_MODE_11B;
366178354Ssam	return IEEE80211_MODE_AUTO;
367178354Ssam}
368178354Ssam
369178354Ssamstatic void
370178354Ssamgetroam(int s)
371178354Ssam{
372178354Ssam	if (gotroam)
373178354Ssam		return;
374178354Ssam	if (get80211(s, IEEE80211_IOC_ROAM,
375178354Ssam	    &roamparams, sizeof(roamparams)) < 0)
376187801Ssam		err(1, "unable to get roaming parameters");
377178354Ssam	gotroam = 1;
378178354Ssam}
379178354Ssam
380178354Ssamstatic void
381178354Ssamsetroam_cb(int s, void *arg)
382178354Ssam{
383178354Ssam	struct ieee80211_roamparams_req *roam = arg;
384178354Ssam	set80211(s, IEEE80211_IOC_ROAM, 0, sizeof(*roam), roam);
385178354Ssam}
386178354Ssam
387178354Ssamstatic void
388178354Ssamgettxparams(int s)
389178354Ssam{
390178354Ssam	if (gottxparams)
391178354Ssam		return;
392178354Ssam	if (get80211(s, IEEE80211_IOC_TXPARAMS,
393178354Ssam	    &txparams, sizeof(txparams)) < 0)
394187801Ssam		err(1, "unable to get transmit parameters");
395178354Ssam	gottxparams = 1;
396178354Ssam}
397178354Ssam
398178354Ssamstatic void
399178354Ssamsettxparams_cb(int s, void *arg)
400178354Ssam{
401178354Ssam	struct ieee80211_txparams_req *txp = arg;
402178354Ssam	set80211(s, IEEE80211_IOC_TXPARAMS, 0, sizeof(*txp), txp);
403178354Ssam}
404178354Ssam
405178354Ssamstatic void
406178354Ssamgetregdomain(int s)
407178354Ssam{
408178354Ssam	if (gotregdomain)
409178354Ssam		return;
410178354Ssam	if (get80211(s, IEEE80211_IOC_REGDOMAIN,
411178354Ssam	    &regdomain, sizeof(regdomain)) < 0)
412187801Ssam		err(1, "unable to get regulatory domain info");
413178354Ssam	gotregdomain = 1;
414178354Ssam}
415178354Ssam
416178354Ssamstatic void
417178354Ssamgetdevcaps(int s, struct ieee80211_devcaps_req *dc)
418178354Ssam{
419187801Ssam	if (get80211(s, IEEE80211_IOC_DEVCAPS, dc,
420187801Ssam	    IEEE80211_DEVCAPS_SPACE(dc)) < 0)
421187801Ssam		err(1, "unable to get device capabilities");
422178354Ssam}
423178354Ssam
424178354Ssamstatic void
425178354Ssamsetregdomain_cb(int s, void *arg)
426178354Ssam{
427187801Ssam	struct ieee80211_regdomain_req *req;
428178354Ssam	struct ieee80211_regdomain *rd = arg;
429187801Ssam	struct ieee80211_devcaps_req *dc;
430178354Ssam	struct regdata *rdp = getregdata();
431178354Ssam
432186102Ssam	if (rd->country != NO_COUNTRY) {
433178354Ssam		const struct country *cc;
434178354Ssam		/*
435178354Ssam		 * Check current country seting to make sure it's
436178354Ssam		 * compatible with the new regdomain.  If not, then
437178354Ssam		 * override it with any default country for this
438178354Ssam		 * SKU.  If we cannot arrange a match, then abort.
439178354Ssam		 */
440178354Ssam		cc = lib80211_country_findbycc(rdp, rd->country);
441178354Ssam		if (cc == NULL)
442178354Ssam			errx(1, "unknown ISO country code %d", rd->country);
443178354Ssam		if (cc->rd->sku != rd->regdomain) {
444178354Ssam			const struct regdomain *rp;
445178354Ssam			/*
446178354Ssam			 * Check if country is incompatible with regdomain.
447178354Ssam			 * To enable multiple regdomains for a country code
448178354Ssam			 * we permit a mismatch between the regdomain and
449178354Ssam			 * the country's associated regdomain when the
450178354Ssam			 * regdomain is setup w/o a default country.  For
451178354Ssam			 * example, US is bound to the FCC regdomain but
452178354Ssam			 * we allow US to be combined with FCC3 because FCC3
453178354Ssam			 * has not default country.  This allows bogus
454178354Ssam			 * combinations like FCC3+DK which are resolved when
455178354Ssam			 * constructing the channel list by deferring to the
456178354Ssam			 * regdomain to construct the channel list.
457178354Ssam			 */
458178354Ssam			rp = lib80211_regdomain_findbysku(rdp, rd->regdomain);
459178354Ssam			if (rp == NULL)
460178354Ssam				errx(1, "country %s (%s) is not usable with "
461178354Ssam				    "regdomain %d", cc->isoname, cc->name,
462178354Ssam				    rd->regdomain);
463186102Ssam			else if (rp->cc != NULL && rp->cc != cc)
464178354Ssam				errx(1, "country %s (%s) is not usable with "
465178354Ssam				   "regdomain %s", cc->isoname, cc->name,
466178354Ssam				   rp->name);
467178354Ssam		}
468178354Ssam	}
469178354Ssam	/*
470178354Ssam	 * Fetch the device capabilities and calculate the
471178354Ssam	 * full set of netbands for which we request a new
472178354Ssam	 * channel list be constructed.  Once that's done we
473178354Ssam	 * push the regdomain info + channel list to the kernel.
474178354Ssam	 */
475187801Ssam	dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
476187801Ssam	if (dc == NULL)
477187801Ssam		errx(1, "no space for device capabilities");
478187801Ssam	dc->dc_chaninfo.ic_nchans = MAXCHAN;
479187801Ssam	getdevcaps(s, dc);
480178354Ssam#if 0
481178354Ssam	if (verbose) {
482187801Ssam		printf("drivercaps: 0x%x\n", dc->dc_drivercaps);
483187801Ssam		printf("cryptocaps: 0x%x\n", dc->dc_cryptocaps);
484187801Ssam		printf("htcaps    : 0x%x\n", dc->dc_htcaps);
485187801Ssam		memcpy(chaninfo, &dc->dc_chaninfo,
486187801Ssam		    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
487187801Ssam		print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, 1/*verbose*/);
488178354Ssam	}
489178354Ssam#endif
490187801Ssam	req = malloc(IEEE80211_REGDOMAIN_SIZE(dc->dc_chaninfo.ic_nchans));
491187801Ssam	if (req == NULL)
492187801Ssam		errx(1, "no space for regdomain request");
493187801Ssam	req->rd = *rd;
494187801Ssam	regdomain_makechannels(req, dc);
495178354Ssam	if (verbose) {
496178354Ssam		LINE_INIT(':');
497178354Ssam		print_regdomain(rd, 1/*verbose*/);
498178354Ssam		LINE_BREAK();
499187801Ssam		/* blech, reallocate channel list for new data */
500187801Ssam		if (chaninfo != NULL)
501187801Ssam			free(chaninfo);
502187801Ssam		chaninfo = malloc(IEEE80211_CHANINFO_SPACE(&req->chaninfo));
503187801Ssam		if (chaninfo == NULL)
504187801Ssam			errx(1, "no space for channel list");
505187801Ssam		memcpy(chaninfo, &req->chaninfo,
506187801Ssam		    IEEE80211_CHANINFO_SPACE(&req->chaninfo));
507187801Ssam		print_channels(s, &req->chaninfo, 1/*allchans*/, 1/*verbose*/);
508178354Ssam	}
509187801Ssam	if (req->chaninfo.ic_nchans == 0)
510178354Ssam		errx(1, "no channels calculated");
511187801Ssam	set80211(s, IEEE80211_IOC_REGDOMAIN, 0,
512187801Ssam	    IEEE80211_REGDOMAIN_SPACE(req), req);
513187801Ssam	free(req);
514187801Ssam	free(dc);
515178354Ssam}
516178354Ssam
517170531Ssamstatic int
518170531Ssamieee80211_mhz2ieee(int freq, int flags)
519170531Ssam{
520170531Ssam	struct ieee80211_channel chan;
521170531Ssam	mapfreq(&chan, freq, flags);
522170531Ssam	return chan.ic_ieee;
523170531Ssam}
524170531Ssam
525170531Ssamstatic int
526138593Ssamisanyarg(const char *arg)
527138593Ssam{
528173275Ssam	return (strncmp(arg, "-", 1) == 0 ||
529173275Ssam	    strncasecmp(arg, "any", 3) == 0 || strncasecmp(arg, "off", 3) == 0);
530138593Ssam}
531138593Ssam
532138593Ssamstatic void
53377218Sphkset80211ssid(const char *val, int d, int s, const struct afswtch *rafp)
53477218Sphk{
53577218Sphk	int		ssid;
53677218Sphk	int		len;
537151883Sbrooks	u_int8_t	data[IEEE80211_NWID_LEN];
53877218Sphk
53977218Sphk	ssid = 0;
540121827Sbrooks	len = strlen(val);
541178354Ssam	if (len > 2 && isdigit((int)val[0]) && val[1] == ':') {
54288748Sambrisko		ssid = atoi(val)-1;
54388748Sambrisko		val += 2;
54488748Sambrisko	}
54577218Sphk
54677218Sphk	bzero(data, sizeof(data));
54777218Sphk	len = sizeof(data);
548151883Sbrooks	if (get_string(val, NULL, data, &len) == NULL)
549151883Sbrooks		exit(1);
55077218Sphk
55177218Sphk	set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
55277218Sphk}
55377218Sphk
554138593Ssamstatic void
55577218Sphkset80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
55677218Sphk{
55777218Sphk	int			len;
55877218Sphk	u_int8_t		data[33];
55977218Sphk
56077218Sphk	bzero(data, sizeof(data));
56177218Sphk	len = sizeof(data);
56277218Sphk	get_string(val, NULL, data, &len);
56377218Sphk
56477218Sphk	set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
56577218Sphk}
56677218Sphk
567138593Ssam/*
568170531Ssam * Parse a channel specification for attributes/flags.
569170531Ssam * The syntax is:
570170531Ssam *	freq/xx		channel width (5,10,20,40,40+,40-)
571170531Ssam *	freq:mode	channel mode (a,b,g,h,n,t,s,d)
572170531Ssam *
573170531Ssam * These can be combined in either order; e.g. 2437:ng/40.
574170531Ssam * Modes are case insensitive.
575170531Ssam *
576170531Ssam * The result is not validated here; it's assumed to be
577170531Ssam * checked against the channel table fetched from the kernel.
578170531Ssam */
579170531Ssamstatic int
580173275Ssamgetchannelflags(const char *val, int freq)
581138593Ssam{
582170531Ssam#define	_CHAN_HT	0x80000000
583170531Ssam	const char *cp;
584170531Ssam	int flags;
585138593Ssam
586170531Ssam	flags = 0;
587166015Ssam
588170531Ssam	cp = strchr(val, ':');
589170531Ssam	if (cp != NULL) {
590170531Ssam		for (cp++; isalpha((int) *cp); cp++) {
591170531Ssam			/* accept mixed case */
592170531Ssam			int c = *cp;
593170531Ssam			if (isupper(c))
594170531Ssam				c = tolower(c);
595170531Ssam			switch (c) {
596170531Ssam			case 'a':		/* 802.11a */
597170531Ssam				flags |= IEEE80211_CHAN_A;
598170531Ssam				break;
599170531Ssam			case 'b':		/* 802.11b */
600170531Ssam				flags |= IEEE80211_CHAN_B;
601170531Ssam				break;
602170531Ssam			case 'g':		/* 802.11g */
603170531Ssam				flags |= IEEE80211_CHAN_G;
604170531Ssam				break;
605170531Ssam			case 'h':		/* ht = 802.11n */
606170531Ssam			case 'n':		/* 802.11n */
607170531Ssam				flags |= _CHAN_HT;	/* NB: private */
608170531Ssam				break;
609170531Ssam			case 'd':		/* dt = Atheros Dynamic Turbo */
610170531Ssam				flags |= IEEE80211_CHAN_TURBO;
611170531Ssam				break;
612170531Ssam			case 't':		/* ht, dt, st, t */
613170531Ssam				/* dt and unadorned t specify Dynamic Turbo */
614170531Ssam				if ((flags & (IEEE80211_CHAN_STURBO|_CHAN_HT)) == 0)
615170531Ssam					flags |= IEEE80211_CHAN_TURBO;
616170531Ssam				break;
617170531Ssam			case 's':		/* st = Atheros Static Turbo */
618170531Ssam				flags |= IEEE80211_CHAN_STURBO;
619170531Ssam				break;
620170531Ssam			default:
621173275Ssam				errx(-1, "%s: Invalid channel attribute %c\n",
622170531Ssam				    val, *cp);
623170531Ssam			}
624170531Ssam		}
625170531Ssam	}
626170531Ssam	cp = strchr(val, '/');
627170531Ssam	if (cp != NULL) {
628170531Ssam		char *ep;
629170531Ssam		u_long cw = strtoul(cp+1, &ep, 10);
630166015Ssam
631170531Ssam		switch (cw) {
632170531Ssam		case 5:
633170531Ssam			flags |= IEEE80211_CHAN_QUARTER;
634170531Ssam			break;
635170531Ssam		case 10:
636170531Ssam			flags |= IEEE80211_CHAN_HALF;
637170531Ssam			break;
638170531Ssam		case 20:
639170531Ssam			/* NB: this may be removed below */
640170531Ssam			flags |= IEEE80211_CHAN_HT20;
641170531Ssam			break;
642170531Ssam		case 40:
643170531Ssam			if (ep != NULL && *ep == '+')
644170531Ssam				flags |= IEEE80211_CHAN_HT40U;
645170531Ssam			else if (ep != NULL && *ep == '-')
646170531Ssam				flags |= IEEE80211_CHAN_HT40D;
647170531Ssam			break;
648170531Ssam		default:
649173275Ssam			errx(-1, "%s: Invalid channel width\n", val);
650170531Ssam		}
651165570Ssam	}
652170531Ssam	/*
653170531Ssam	 * Cleanup specifications.
654170531Ssam	 */
655170531Ssam	if ((flags & _CHAN_HT) == 0) {
656170531Ssam		/*
657170531Ssam		 * If user specified freq/20 or freq/40 quietly remove
658170531Ssam		 * HT cw attributes depending on channel use.  To give
659170531Ssam		 * an explicit 20/40 width for an HT channel you must
660170531Ssam		 * indicate it is an HT channel since all HT channels
661170531Ssam		 * are also usable for legacy operation; e.g. freq:n/40.
662170531Ssam		 */
663170531Ssam		flags &= ~IEEE80211_CHAN_HT;
664170531Ssam	} else {
665170531Ssam		/*
666170531Ssam		 * Remove private indicator that this is an HT channel
667170531Ssam		 * and if no explicit channel width has been given
668170531Ssam		 * provide the default settings.
669170531Ssam		 */
670170531Ssam		flags &= ~_CHAN_HT;
671173275Ssam		if ((flags & IEEE80211_CHAN_HT) == 0) {
672173275Ssam			struct ieee80211_channel chan;
673173275Ssam			/*
674173275Ssam			 * Consult the channel list to see if we can use
675173275Ssam			 * HT40+ or HT40- (if both the map routines choose).
676173275Ssam			 */
677173275Ssam			if (freq > 255)
678173275Ssam				mapfreq(&chan, freq, 0);
679173275Ssam			else
680173275Ssam				mapchan(&chan, freq, 0);
681173275Ssam			flags |= (chan.ic_flags & IEEE80211_CHAN_HT);
682173275Ssam		}
683170531Ssam	}
684170531Ssam	return flags;
685170531Ssam#undef _CHAN_HT
686138593Ssam}
687138593Ssam
688138593Ssamstatic void
689186105Ssamgetchannel(int s, struct ieee80211_channel *chan, const char *val)
690186105Ssam{
691186105Ssam	int v, flags;
692186105Ssam	char *eptr;
693186105Ssam
694186105Ssam	memset(chan, 0, sizeof(*chan));
695186105Ssam	if (isanyarg(val)) {
696186105Ssam		chan->ic_freq = IEEE80211_CHAN_ANY;
697186105Ssam		return;
698186105Ssam	}
699186105Ssam	getchaninfo(s);
700186105Ssam	errno = 0;
701186105Ssam	v = strtol(val, &eptr, 10);
702186105Ssam	if (val[0] == '\0' || val == eptr || errno == ERANGE ||
703186105Ssam	    /* channel may be suffixed with nothing, :flag, or /width */
704186105Ssam	    (eptr[0] != '\0' && eptr[0] != ':' && eptr[0] != '/'))
705186105Ssam		errx(1, "invalid channel specification%s",
706186105Ssam		    errno == ERANGE ? " (out of range)" : "");
707186105Ssam	flags = getchannelflags(val, v);
708186105Ssam	if (v > 255) {		/* treat as frequency */
709186105Ssam		mapfreq(chan, v, flags);
710186105Ssam	} else {
711186105Ssam		mapchan(chan, v, flags);
712186105Ssam	}
713186105Ssam}
714186105Ssam
715186105Ssamstatic void
71677218Sphkset80211channel(const char *val, int d, int s, const struct afswtch *rafp)
71777218Sphk{
718170531Ssam	struct ieee80211_channel chan;
719170531Ssam
720186105Ssam	getchannel(s, &chan, val);
721170531Ssam	set80211(s, IEEE80211_IOC_CURCHAN, 0, sizeof(chan), &chan);
72277218Sphk}
72377218Sphk
724138593Ssamstatic void
725178354Ssamset80211chanswitch(const char *val, int d, int s, const struct afswtch *rafp)
726178354Ssam{
727178354Ssam	struct ieee80211_chanswitch_req csr;
728178354Ssam
729186105Ssam	getchannel(s, &csr.csa_chan, val);
730178354Ssam	csr.csa_mode = 1;
731178354Ssam	csr.csa_count = 5;
732178354Ssam	set80211(s, IEEE80211_IOC_CHANSWITCH, 0, sizeof(csr), &csr);
733178354Ssam}
734178354Ssam
735178354Ssamstatic void
73677218Sphkset80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
73777218Sphk{
73877218Sphk	int	mode;
73977218Sphk
74091454Sbrooks	if (strcasecmp(val, "none") == 0) {
74177218Sphk		mode = IEEE80211_AUTH_NONE;
74291454Sbrooks	} else if (strcasecmp(val, "open") == 0) {
74377218Sphk		mode = IEEE80211_AUTH_OPEN;
74491454Sbrooks	} else if (strcasecmp(val, "shared") == 0) {
74577218Sphk		mode = IEEE80211_AUTH_SHARED;
746138593Ssam	} else if (strcasecmp(val, "8021x") == 0) {
747138593Ssam		mode = IEEE80211_AUTH_8021X;
748138593Ssam	} else if (strcasecmp(val, "wpa") == 0) {
749138593Ssam		mode = IEEE80211_AUTH_WPA;
75077218Sphk	} else {
751150708Sru		errx(1, "unknown authmode");
75277218Sphk	}
75377218Sphk
75477218Sphk	set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
75577218Sphk}
75677218Sphk
757138593Ssamstatic void
75877218Sphkset80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
75977218Sphk{
76077218Sphk	int	mode;
76177218Sphk
76291454Sbrooks	if (strcasecmp(val, "off") == 0) {
76377218Sphk		mode = IEEE80211_POWERSAVE_OFF;
76491454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
76577218Sphk		mode = IEEE80211_POWERSAVE_ON;
76691454Sbrooks	} else if (strcasecmp(val, "cam") == 0) {
76777218Sphk		mode = IEEE80211_POWERSAVE_CAM;
76891454Sbrooks	} else if (strcasecmp(val, "psp") == 0) {
76977218Sphk		mode = IEEE80211_POWERSAVE_PSP;
77091454Sbrooks	} else if (strcasecmp(val, "psp-cam") == 0) {
77177218Sphk		mode = IEEE80211_POWERSAVE_PSP_CAM;
77277218Sphk	} else {
773150708Sru		errx(1, "unknown powersavemode");
77477218Sphk	}
77577218Sphk
77677218Sphk	set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
77777218Sphk}
77877218Sphk
779138593Ssamstatic void
78077218Sphkset80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
78177218Sphk{
78277218Sphk	if (d == 0)
78377218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
78477218Sphk		    0, NULL);
78577218Sphk	else
78677218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
78777218Sphk		    0, NULL);
78877218Sphk}
78977218Sphk
790138593Ssamstatic void
79177218Sphkset80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
79277218Sphk{
79377218Sphk	set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
79477218Sphk}
79577218Sphk
796138593Ssamstatic void
79777218Sphkset80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
79877218Sphk{
79977218Sphk	int	mode;
80077218Sphk
80191454Sbrooks	if (strcasecmp(val, "off") == 0) {
80277218Sphk		mode = IEEE80211_WEP_OFF;
80391454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
80477218Sphk		mode = IEEE80211_WEP_ON;
80591454Sbrooks	} else if (strcasecmp(val, "mixed") == 0) {
80677218Sphk		mode = IEEE80211_WEP_MIXED;
80777218Sphk	} else {
808150708Sru		errx(1, "unknown wep mode");
80977218Sphk	}
81077218Sphk
81177218Sphk	set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
81277218Sphk}
81377218Sphk
814138593Ssamstatic void
81577218Sphkset80211wep(const char *val, int d, int s, const struct afswtch *rafp)
81677218Sphk{
81777218Sphk	set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
81877218Sphk}
81977218Sphk
820139493Ssamstatic int
821139493Ssamisundefarg(const char *arg)
822139493Ssam{
823139493Ssam	return (strcmp(arg, "-") == 0 || strncasecmp(arg, "undef", 5) == 0);
824139493Ssam}
825139493Ssam
826138593Ssamstatic void
82777218Sphkset80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
82877218Sphk{
829139493Ssam	if (isundefarg(val))
830139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, IEEE80211_KEYIX_NONE, 0, NULL);
831139493Ssam	else
832139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
83377218Sphk}
83477218Sphk
835138593Ssamstatic void
83677218Sphkset80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
83777218Sphk{
83877218Sphk	int		key = 0;
83977218Sphk	int		len;
840120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
84177218Sphk
842178354Ssam	if (isdigit((int)val[0]) && val[1] == ':') {
84377218Sphk		key = atoi(val)-1;
84477218Sphk		val += 2;
84577218Sphk	}
84677218Sphk
84777218Sphk	bzero(data, sizeof(data));
84877218Sphk	len = sizeof(data);
84977218Sphk	get_string(val, NULL, data, &len);
85077218Sphk
85177218Sphk	set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
85277218Sphk}
85377218Sphk
85477218Sphk/*
855148686Sstefanf * This function is purely a NetBSD compatability interface.  The NetBSD
856148686Sstefanf * interface is too inflexible, but it's there so we'll support it since
85777218Sphk * it's not all that hard.
85877218Sphk */
859138593Ssamstatic void
86077218Sphkset80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
86177218Sphk{
86277218Sphk	int		txkey;
86377218Sphk	int		i, len;
864120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
86577218Sphk
86677218Sphk	set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
86777218Sphk
868178354Ssam	if (isdigit((int)val[0]) && val[1] == ':') {
86977218Sphk		txkey = val[0]-'0'-1;
87077218Sphk		val += 2;
87177218Sphk
87291454Sbrooks		for (i = 0; i < 4; i++) {
87377218Sphk			bzero(data, sizeof(data));
87477218Sphk			len = sizeof(data);
87577218Sphk			val = get_string(val, ",", data, &len);
876151827Sbrooks			if (val == NULL)
877151827Sbrooks				exit(1);
87877218Sphk
87977218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
88077218Sphk		}
88177218Sphk	} else {
88277218Sphk		bzero(data, sizeof(data));
88377218Sphk		len = sizeof(data);
88477218Sphk		get_string(val, NULL, data, &len);
88577218Sphk		txkey = 0;
88677218Sphk
88777218Sphk		set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
88877218Sphk
88977218Sphk		bzero(data, sizeof(data));
89091454Sbrooks		for (i = 1; i < 4; i++)
89177218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
89277218Sphk	}
89377218Sphk
89477218Sphk	set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
89577218Sphk}
89677218Sphk
897138593Ssamstatic void
898127649Ssamset80211rtsthreshold(const char *val, int d, int s, const struct afswtch *rafp)
899127649Ssam{
900148416Ssam	set80211(s, IEEE80211_IOC_RTSTHRESHOLD,
901148416Ssam		isundefarg(val) ? IEEE80211_RTS_MAX : atoi(val), 0, NULL);
902127649Ssam}
903127649Ssam
904138593Ssamstatic void
905127649Ssamset80211protmode(const char *val, int d, int s, const struct afswtch *rafp)
906127649Ssam{
907127649Ssam	int	mode;
908127649Ssam
909127649Ssam	if (strcasecmp(val, "off") == 0) {
910127649Ssam		mode = IEEE80211_PROTMODE_OFF;
911127649Ssam	} else if (strcasecmp(val, "cts") == 0) {
912127649Ssam		mode = IEEE80211_PROTMODE_CTS;
913173275Ssam	} else if (strncasecmp(val, "rtscts", 3) == 0) {
914127649Ssam		mode = IEEE80211_PROTMODE_RTSCTS;
915127649Ssam	} else {
916150708Sru		errx(1, "unknown protection mode");
917127649Ssam	}
918127649Ssam
919127649Ssam	set80211(s, IEEE80211_IOC_PROTMODE, mode, 0, NULL);
920127649Ssam}
921127649Ssam
922138593Ssamstatic void
923173275Ssamset80211htprotmode(const char *val, int d, int s, const struct afswtch *rafp)
924173275Ssam{
925173275Ssam	int	mode;
926173275Ssam
927173275Ssam	if (strcasecmp(val, "off") == 0) {
928173275Ssam		mode = IEEE80211_PROTMODE_OFF;
929173275Ssam	} else if (strncasecmp(val, "rts", 3) == 0) {
930173275Ssam		mode = IEEE80211_PROTMODE_RTSCTS;
931173275Ssam	} else {
932173275Ssam		errx(1, "unknown protection mode");
933173275Ssam	}
934173275Ssam
935173275Ssam	set80211(s, IEEE80211_IOC_HTPROTMODE, mode, 0, NULL);
936173275Ssam}
937173275Ssam
938173275Ssamstatic void
939127649Ssamset80211txpower(const char *val, int d, int s, const struct afswtch *rafp)
940127649Ssam{
941173275Ssam	double v = atof(val);
942173275Ssam	int txpow;
943173275Ssam
944173275Ssam	txpow = (int) (2*v);
945173275Ssam	if (txpow != 2*v)
946173275Ssam		errx(-1, "invalid tx power (must be .5 dBm units)");
947173275Ssam	set80211(s, IEEE80211_IOC_TXPOWER, txpow, 0, NULL);
948127649Ssam}
949127649Ssam
950138593Ssam#define	IEEE80211_ROAMING_DEVICE	0
951138593Ssam#define	IEEE80211_ROAMING_AUTO		1
952138593Ssam#define	IEEE80211_ROAMING_MANUAL	2
953138593Ssam
954138593Ssamstatic void
955138593Ssamset80211roaming(const char *val, int d, int s, const struct afswtch *rafp)
95677218Sphk{
957138593Ssam	int mode;
95877218Sphk
959138593Ssam	if (strcasecmp(val, "device") == 0) {
960138593Ssam		mode = IEEE80211_ROAMING_DEVICE;
961138593Ssam	} else if (strcasecmp(val, "auto") == 0) {
962138593Ssam		mode = IEEE80211_ROAMING_AUTO;
963138593Ssam	} else if (strcasecmp(val, "manual") == 0) {
964138593Ssam		mode = IEEE80211_ROAMING_MANUAL;
965138593Ssam	} else {
966150708Sru		errx(1, "unknown roaming mode");
967138593Ssam	}
968138593Ssam	set80211(s, IEEE80211_IOC_ROAMING, mode, 0, NULL);
969138593Ssam}
970138593Ssam
971138593Ssamstatic void
972138593Ssamset80211wme(const char *val, int d, int s, const struct afswtch *rafp)
973138593Ssam{
974138593Ssam	set80211(s, IEEE80211_IOC_WME, d, 0, NULL);
975138593Ssam}
976138593Ssam
977138593Ssamstatic void
978138593Ssamset80211hidessid(const char *val, int d, int s, const struct afswtch *rafp)
979138593Ssam{
980138593Ssam	set80211(s, IEEE80211_IOC_HIDESSID, d, 0, NULL);
981138593Ssam}
982138593Ssam
983138593Ssamstatic void
984138593Ssamset80211apbridge(const char *val, int d, int s, const struct afswtch *rafp)
985138593Ssam{
986138593Ssam	set80211(s, IEEE80211_IOC_APBRIDGE, d, 0, NULL);
987138593Ssam}
988138593Ssam
989138593Ssamstatic void
990170531Ssamset80211fastframes(const char *val, int d, int s, const struct afswtch *rafp)
991170531Ssam{
992170531Ssam	set80211(s, IEEE80211_IOC_FF, d, 0, NULL);
993170531Ssam}
994170531Ssam
995170531Ssamstatic void
996170531Ssamset80211dturbo(const char *val, int d, int s, const struct afswtch *rafp)
997170531Ssam{
998170531Ssam	set80211(s, IEEE80211_IOC_TURBOP, d, 0, NULL);
999170531Ssam}
1000170531Ssam
1001170531Ssamstatic void
1002138593Ssamset80211chanlist(const char *val, int d, int s, const struct afswtch *rafp)
1003138593Ssam{
1004138593Ssam	struct ieee80211req_chanlist chanlist;
1005138593Ssam	char *temp, *cp, *tp;
1006138593Ssam
1007138593Ssam	temp = malloc(strlen(val) + 1);
1008138593Ssam	if (temp == NULL)
1009138593Ssam		errx(1, "malloc failed");
1010138593Ssam	strcpy(temp, val);
1011138593Ssam	memset(&chanlist, 0, sizeof(chanlist));
1012138593Ssam	cp = temp;
1013138593Ssam	for (;;) {
1014173275Ssam		int first, last, f, c;
1015138593Ssam
1016138593Ssam		tp = strchr(cp, ',');
1017138593Ssam		if (tp != NULL)
1018138593Ssam			*tp++ = '\0';
1019138593Ssam		switch (sscanf(cp, "%u-%u", &first, &last)) {
1020138593Ssam		case 1:
1021187801Ssam			if (first > IEEE80211_CHAN_MAX)
1022188038Sdelphij				errx(-1, "channel %u out of range, max %u",
1023187801Ssam					first, IEEE80211_CHAN_MAX);
1024138593Ssam			setbit(chanlist.ic_channels, first);
1025138593Ssam			break;
1026138593Ssam		case 2:
1027187801Ssam			if (first > IEEE80211_CHAN_MAX)
1028188038Sdelphij				errx(-1, "channel %u out of range, max %u",
1029187801Ssam					first, IEEE80211_CHAN_MAX);
1030187801Ssam			if (last > IEEE80211_CHAN_MAX)
1031188038Sdelphij				errx(-1, "channel %u out of range, max %u",
1032187801Ssam					last, IEEE80211_CHAN_MAX);
1033138593Ssam			if (first > last)
1034138593Ssam				errx(-1, "void channel range, %u > %u",
1035138593Ssam					first, last);
1036138593Ssam			for (f = first; f <= last; f++)
1037138593Ssam				setbit(chanlist.ic_channels, f);
1038138593Ssam			break;
1039138593Ssam		}
1040138593Ssam		if (tp == NULL)
1041138593Ssam			break;
1042173275Ssam		c = *tp;
1043173275Ssam		while (isspace(c))
1044138593Ssam			tp++;
1045173275Ssam		if (!isdigit(c))
1046138593Ssam			break;
1047138593Ssam		cp = tp;
1048138593Ssam	}
1049170531Ssam	set80211(s, IEEE80211_IOC_CHANLIST, 0, sizeof(chanlist), &chanlist);
1050138593Ssam}
1051138593Ssam
1052138593Ssamstatic void
1053138593Ssamset80211bssid(const char *val, int d, int s, const struct afswtch *rafp)
1054138593Ssam{
1055138593Ssam
1056138593Ssam	if (!isanyarg(val)) {
1057138593Ssam		char *temp;
1058138593Ssam		struct sockaddr_dl sdl;
1059138593Ssam
1060155702Ssam		temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1061138593Ssam		if (temp == NULL)
1062138593Ssam			errx(1, "malloc failed");
1063138593Ssam		temp[0] = ':';
1064138593Ssam		strcpy(temp + 1, val);
1065138593Ssam		sdl.sdl_len = sizeof(sdl);
1066138593Ssam		link_addr(temp, &sdl);
1067138593Ssam		free(temp);
1068138593Ssam		if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1069138593Ssam			errx(1, "malformed link-level address");
1070138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
1071138593Ssam			IEEE80211_ADDR_LEN, LLADDR(&sdl));
1072138593Ssam	} else {
1073138593Ssam		uint8_t zerobssid[IEEE80211_ADDR_LEN];
1074138593Ssam		memset(zerobssid, 0, sizeof(zerobssid));
1075138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
1076138593Ssam			IEEE80211_ADDR_LEN, zerobssid);
1077138593Ssam	}
1078138593Ssam}
1079138593Ssam
1080138593Ssamstatic int
1081138593Ssamgetac(const char *ac)
1082138593Ssam{
1083138593Ssam	if (strcasecmp(ac, "ac_be") == 0 || strcasecmp(ac, "be") == 0)
1084138593Ssam		return WME_AC_BE;
1085138593Ssam	if (strcasecmp(ac, "ac_bk") == 0 || strcasecmp(ac, "bk") == 0)
1086138593Ssam		return WME_AC_BK;
1087138593Ssam	if (strcasecmp(ac, "ac_vi") == 0 || strcasecmp(ac, "vi") == 0)
1088138593Ssam		return WME_AC_VI;
1089138593Ssam	if (strcasecmp(ac, "ac_vo") == 0 || strcasecmp(ac, "vo") == 0)
1090138593Ssam		return WME_AC_VO;
1091138593Ssam	errx(1, "unknown wme access class %s", ac);
1092138593Ssam}
1093138593Ssam
1094138593Ssamstatic
1095138593SsamDECL_CMD_FUNC2(set80211cwmin, ac, val)
1096138593Ssam{
1097138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val), getac(ac), NULL);
1098138593Ssam}
1099138593Ssam
1100138593Ssamstatic
1101138593SsamDECL_CMD_FUNC2(set80211cwmax, ac, val)
1102138593Ssam{
1103138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val), getac(ac), NULL);
1104138593Ssam}
1105138593Ssam
1106138593Ssamstatic
1107138593SsamDECL_CMD_FUNC2(set80211aifs, ac, val)
1108138593Ssam{
1109138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val), getac(ac), NULL);
1110138593Ssam}
1111138593Ssam
1112138593Ssamstatic
1113138593SsamDECL_CMD_FUNC2(set80211txoplimit, ac, val)
1114138593Ssam{
1115138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), getac(ac), NULL);
1116138593Ssam}
1117138593Ssam
1118138593Ssamstatic
1119148621SsamDECL_CMD_FUNC(set80211acm, ac, d)
1120138593Ssam{
1121148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 1, getac(ac), NULL);
1122138593Ssam}
1123148621Ssamstatic
1124148621SsamDECL_CMD_FUNC(set80211noacm, ac, d)
1125148621Ssam{
1126148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 0, getac(ac), NULL);
1127148621Ssam}
1128138593Ssam
1129138593Ssamstatic
1130148621SsamDECL_CMD_FUNC(set80211ackpolicy, ac, d)
1131138593Ssam{
1132148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 1, getac(ac), NULL);
1133138593Ssam}
1134148621Ssamstatic
1135148621SsamDECL_CMD_FUNC(set80211noackpolicy, ac, d)
1136148621Ssam{
1137148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 0, getac(ac), NULL);
1138148621Ssam}
1139138593Ssam
1140138593Ssamstatic
1141138593SsamDECL_CMD_FUNC2(set80211bsscwmin, ac, val)
1142138593Ssam{
1143138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val),
1144138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1145138593Ssam}
1146138593Ssam
1147138593Ssamstatic
1148138593SsamDECL_CMD_FUNC2(set80211bsscwmax, ac, val)
1149138593Ssam{
1150138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val),
1151138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1152138593Ssam}
1153138593Ssam
1154138593Ssamstatic
1155138593SsamDECL_CMD_FUNC2(set80211bssaifs, ac, val)
1156138593Ssam{
1157138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val),
1158138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1159138593Ssam}
1160138593Ssam
1161138593Ssamstatic
1162138593SsamDECL_CMD_FUNC2(set80211bsstxoplimit, ac, val)
1163138593Ssam{
1164138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val),
1165138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1166138593Ssam}
1167138593Ssam
1168138593Ssamstatic
1169138593SsamDECL_CMD_FUNC(set80211dtimperiod, val, d)
1170138593Ssam{
1171138593Ssam	set80211(s, IEEE80211_IOC_DTIM_PERIOD, atoi(val), 0, NULL);
1172138593Ssam}
1173138593Ssam
1174138593Ssamstatic
1175138593SsamDECL_CMD_FUNC(set80211bintval, val, d)
1176138593Ssam{
1177138593Ssam	set80211(s, IEEE80211_IOC_BEACON_INTERVAL, atoi(val), 0, NULL);
1178138593Ssam}
1179138593Ssam
1180138593Ssamstatic void
1181138593Ssamset80211macmac(int s, int op, const char *val)
1182138593Ssam{
1183138593Ssam	char *temp;
1184138593Ssam	struct sockaddr_dl sdl;
1185138593Ssam
1186155702Ssam	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1187138593Ssam	if (temp == NULL)
1188138593Ssam		errx(1, "malloc failed");
1189138593Ssam	temp[0] = ':';
1190138593Ssam	strcpy(temp + 1, val);
1191138593Ssam	sdl.sdl_len = sizeof(sdl);
1192138593Ssam	link_addr(temp, &sdl);
1193138593Ssam	free(temp);
1194138593Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1195138593Ssam		errx(1, "malformed link-level address");
1196138593Ssam	set80211(s, op, 0, IEEE80211_ADDR_LEN, LLADDR(&sdl));
1197138593Ssam}
1198138593Ssam
1199138593Ssamstatic
1200138593SsamDECL_CMD_FUNC(set80211addmac, val, d)
1201138593Ssam{
1202138593Ssam	set80211macmac(s, IEEE80211_IOC_ADDMAC, val);
1203138593Ssam}
1204138593Ssam
1205138593Ssamstatic
1206138593SsamDECL_CMD_FUNC(set80211delmac, val, d)
1207138593Ssam{
1208138593Ssam	set80211macmac(s, IEEE80211_IOC_DELMAC, val);
1209138593Ssam}
1210138593Ssam
1211138593Ssamstatic
1212149029SsamDECL_CMD_FUNC(set80211kickmac, val, d)
1213149029Ssam{
1214149029Ssam	char *temp;
1215149029Ssam	struct sockaddr_dl sdl;
1216149029Ssam	struct ieee80211req_mlme mlme;
1217149029Ssam
1218155702Ssam	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1219149029Ssam	if (temp == NULL)
1220149029Ssam		errx(1, "malloc failed");
1221149029Ssam	temp[0] = ':';
1222149029Ssam	strcpy(temp + 1, val);
1223149029Ssam	sdl.sdl_len = sizeof(sdl);
1224149029Ssam	link_addr(temp, &sdl);
1225149029Ssam	free(temp);
1226149029Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1227149029Ssam		errx(1, "malformed link-level address");
1228149029Ssam	memset(&mlme, 0, sizeof(mlme));
1229149029Ssam	mlme.im_op = IEEE80211_MLME_DEAUTH;
1230149029Ssam	mlme.im_reason = IEEE80211_REASON_AUTH_EXPIRE;
1231149029Ssam	memcpy(mlme.im_macaddr, LLADDR(&sdl), IEEE80211_ADDR_LEN);
1232170531Ssam	set80211(s, IEEE80211_IOC_MLME, 0, sizeof(mlme), &mlme);
1233149029Ssam}
1234149029Ssam
1235149029Ssamstatic
1236138593SsamDECL_CMD_FUNC(set80211maccmd, val, d)
1237138593Ssam{
1238138593Ssam	set80211(s, IEEE80211_IOC_MACCMD, d, 0, NULL);
1239138593Ssam}
1240138593Ssam
1241147795Ssamstatic void
1242147795Ssamset80211pureg(const char *val, int d, int s, const struct afswtch *rafp)
1243147795Ssam{
1244147795Ssam	set80211(s, IEEE80211_IOC_PUREG, d, 0, NULL);
1245147795Ssam}
1246147795Ssam
1247153422Ssamstatic void
1248170531Ssamset80211bgscan(const char *val, int d, int s, const struct afswtch *rafp)
1249153422Ssam{
1250170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN, d, 0, NULL);
1251153422Ssam}
1252153422Ssam
1253148416Ssamstatic
1254170531SsamDECL_CMD_FUNC(set80211bgscanidle, val, d)
1255170531Ssam{
1256170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN_IDLE, atoi(val), 0, NULL);
1257170531Ssam}
1258170531Ssam
1259170531Ssamstatic
1260170531SsamDECL_CMD_FUNC(set80211bgscanintvl, val, d)
1261170531Ssam{
1262170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN_INTERVAL, atoi(val), 0, NULL);
1263170531Ssam}
1264170531Ssam
1265170531Ssamstatic
1266170531SsamDECL_CMD_FUNC(set80211scanvalid, val, d)
1267170531Ssam{
1268170531Ssam	set80211(s, IEEE80211_IOC_SCANVALID, atoi(val), 0, NULL);
1269170531Ssam}
1270170531Ssam
1271178354Ssam/*
1272178354Ssam * Parse an optional trailing specification of which netbands
1273178354Ssam * to apply a parameter to.  This is basically the same syntax
1274178354Ssam * as used for channels but you can concatenate to specify
1275178354Ssam * multiple.  For example:
1276178354Ssam *	14:abg		apply to 11a, 11b, and 11g
1277178354Ssam *	6:ht		apply to 11na and 11ng
1278178354Ssam * We don't make a big effort to catch silly things; this is
1279178354Ssam * really a convenience mechanism.
1280178354Ssam */
1281178354Ssamstatic int
1282178354Ssamgetmodeflags(const char *val)
1283170531Ssam{
1284178354Ssam	const char *cp;
1285178354Ssam	int flags;
1286178354Ssam
1287178354Ssam	flags = 0;
1288178354Ssam
1289178354Ssam	cp = strchr(val, ':');
1290178354Ssam	if (cp != NULL) {
1291178354Ssam		for (cp++; isalpha((int) *cp); cp++) {
1292178354Ssam			/* accept mixed case */
1293178354Ssam			int c = *cp;
1294178354Ssam			if (isupper(c))
1295178354Ssam				c = tolower(c);
1296178354Ssam			switch (c) {
1297178354Ssam			case 'a':		/* 802.11a */
1298178354Ssam				flags |= IEEE80211_CHAN_A;
1299178354Ssam				break;
1300178354Ssam			case 'b':		/* 802.11b */
1301178354Ssam				flags |= IEEE80211_CHAN_B;
1302178354Ssam				break;
1303178354Ssam			case 'g':		/* 802.11g */
1304178354Ssam				flags |= IEEE80211_CHAN_G;
1305178354Ssam				break;
1306178354Ssam			case 'h':		/* ht = 802.11n */
1307178354Ssam			case 'n':		/* 802.11n */
1308178354Ssam				flags |= IEEE80211_CHAN_HT;
1309178354Ssam				break;
1310178354Ssam			case 'd':		/* dt = Atheros Dynamic Turbo */
1311178354Ssam				flags |= IEEE80211_CHAN_TURBO;
1312178354Ssam				break;
1313178354Ssam			case 't':		/* ht, dt, st, t */
1314178354Ssam				/* dt and unadorned t specify Dynamic Turbo */
1315178354Ssam				if ((flags & (IEEE80211_CHAN_STURBO|IEEE80211_CHAN_HT)) == 0)
1316178354Ssam					flags |= IEEE80211_CHAN_TURBO;
1317178354Ssam				break;
1318178354Ssam			case 's':		/* st = Atheros Static Turbo */
1319178354Ssam				flags |= IEEE80211_CHAN_STURBO;
1320178354Ssam				break;
1321178354Ssam			default:
1322178354Ssam				errx(-1, "%s: Invalid mode attribute %c\n",
1323178354Ssam				    val, *cp);
1324178354Ssam			}
1325178354Ssam		}
1326178354Ssam	}
1327178354Ssam	return flags;
1328170531Ssam}
1329170531Ssam
1330178354Ssam#define	IEEE80211_CHAN_HTA	(IEEE80211_CHAN_HT|IEEE80211_CHAN_5GHZ)
1331178354Ssam#define	IEEE80211_CHAN_HTG	(IEEE80211_CHAN_HT|IEEE80211_CHAN_2GHZ)
1332178354Ssam
1333178354Ssam#define	_APPLY(_flags, _base, _param, _v) do {				\
1334178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1335178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1336178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1337178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1338178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1339178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1340178354Ssam	    else							\
1341178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1342178354Ssam    }									\
1343178354Ssam    if (_flags & IEEE80211_CHAN_TURBO) {				\
1344178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1345178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;	\
1346178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;	\
1347178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1348178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;	\
1349178354Ssam	    else							\
1350178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;	\
1351178354Ssam    }									\
1352178354Ssam    if (_flags & IEEE80211_CHAN_STURBO)					\
1353178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = _v;		\
1354178354Ssam    if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1355178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = _v;		\
1356178354Ssam    if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1357178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = _v;		\
1358178354Ssam    if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1359178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = _v;		\
1360178354Ssam} while (0)
1361178354Ssam#define	_APPLY1(_flags, _base, _param, _v) do {				\
1362178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1363178354Ssam	    if (_flags & IEEE80211_CHAN_5GHZ)				\
1364178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1365178354Ssam	    else							\
1366178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1367178354Ssam    } else if ((_flags & IEEE80211_CHAN_108A) == IEEE80211_CHAN_108A)	\
1368178354Ssam	    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;		\
1369178354Ssam    else if ((_flags & IEEE80211_CHAN_108G) == IEEE80211_CHAN_108G)	\
1370178354Ssam	    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;		\
1371178354Ssam    else if ((_flags & IEEE80211_CHAN_ST) == IEEE80211_CHAN_ST)		\
1372178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = _v;		\
1373178354Ssam    else if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1374178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = _v;		\
1375178354Ssam    else if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1376178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = _v;		\
1377178354Ssam    else if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1378178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = _v;		\
1379178354Ssam} while (0)
1380178354Ssam#define	_APPLY_RATE(_flags, _base, _param, _v) do {			\
1381178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1382178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1383178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v|0x80;	\
1384178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v|0x80;	\
1385178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1386178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v|0x80;	\
1387178354Ssam	    else							\
1388178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v|0x80;	\
1389178354Ssam    }									\
1390178354Ssam    if (_flags & IEEE80211_CHAN_TURBO) {				\
1391178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1392178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = 2*_v;	\
1393178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = 2*_v;	\
1394178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1395178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = 2*_v;	\
1396178354Ssam	    else							\
1397178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = 2*_v;	\
1398178354Ssam    }									\
1399178354Ssam    if (_flags & IEEE80211_CHAN_STURBO)					\
1400178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = 2*_v;	\
1401178354Ssam    if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1402178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = 2*_v;		\
1403178354Ssam    if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1404178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = (_v == 5 ? 11 : 2*_v);\
1405178354Ssam    if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1406178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = (_v == 5 ? 11 : 2*_v);\
1407178354Ssam} while (0)
1408178354Ssam#define	_APPLY_RATE1(_flags, _base, _param, _v) do {			\
1409178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1410178354Ssam	    if (_flags & IEEE80211_CHAN_5GHZ)				\
1411178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v|0x80;	\
1412178354Ssam	    else							\
1413178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v|0x80;	\
1414178354Ssam    } else if ((_flags & IEEE80211_CHAN_108A) == IEEE80211_CHAN_108A)	\
1415178354Ssam	    _base.params[IEEE80211_MODE_TURBO_A]._param = 2*_v;		\
1416178354Ssam    else if ((_flags & IEEE80211_CHAN_108G) == IEEE80211_CHAN_108G)	\
1417178354Ssam	    _base.params[IEEE80211_MODE_TURBO_G]._param = 2*_v;		\
1418178354Ssam    else if ((_flags & IEEE80211_CHAN_ST) == IEEE80211_CHAN_ST)		\
1419178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = 2*_v;	\
1420178354Ssam    else if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1421178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = 2*_v;		\
1422178354Ssam    else if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1423178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = (_v == 5 ? 11 : 2*_v);\
1424178354Ssam    else if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1425178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = (_v == 5 ? 11 : 2*_v);\
1426178354Ssam} while (0)
1427178354Ssam
1428170531Ssamstatic
1429178354SsamDECL_CMD_FUNC(set80211roamrssi, val, d)
1430170531Ssam{
1431178354Ssam	double v = atof(val);
1432178354Ssam	int rssi, flags;
1433178354Ssam
1434178354Ssam	rssi = (int) (2*v);
1435178354Ssam	if (rssi != 2*v)
1436178354Ssam		errx(-1, "invalid rssi (must be .5 dBm units)");
1437178354Ssam	flags = getmodeflags(val);
1438178354Ssam	getroam(s);
1439178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1440178354Ssam		flags = getcurchan(s)->ic_flags;
1441178354Ssam		_APPLY1(flags, roamparams, rssi, rssi);
1442178354Ssam	} else
1443178354Ssam		_APPLY(flags, roamparams, rssi, rssi);
1444178354Ssam	callback_register(setroam_cb, &roamparams);
1445170531Ssam}
1446170531Ssam
1447170531Ssamstatic
1448178354SsamDECL_CMD_FUNC(set80211roamrate, val, d)
1449170531Ssam{
1450178354Ssam	int v = atoi(val), flags;
1451178354Ssam
1452178354Ssam	flags = getmodeflags(val);
1453178354Ssam	getroam(s);
1454178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1455178354Ssam		flags = getcurchan(s)->ic_flags;
1456178354Ssam		_APPLY_RATE1(flags, roamparams, rate, v);
1457178354Ssam	} else
1458178354Ssam		_APPLY_RATE(flags, roamparams, rate, v);
1459178354Ssam	callback_register(setroam_cb, &roamparams);
1460170531Ssam}
1461170531Ssam
1462170531Ssamstatic
1463178354SsamDECL_CMD_FUNC(set80211mcastrate, val, d)
1464170531Ssam{
1465178354Ssam	int v = atoi(val), flags;
1466178354Ssam
1467178354Ssam	flags = getmodeflags(val);
1468178354Ssam	gettxparams(s);
1469178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1470178354Ssam		flags = getcurchan(s)->ic_flags;
1471178354Ssam		_APPLY_RATE1(flags, txparams, mcastrate, v);
1472178354Ssam	} else
1473178354Ssam		_APPLY_RATE(flags, txparams, mcastrate, v);
1474178354Ssam	callback_register(settxparams_cb, &txparams);
1475170531Ssam}
1476170531Ssam
1477170531Ssamstatic
1478178354SsamDECL_CMD_FUNC(set80211mgtrate, val, d)
1479170531Ssam{
1480178354Ssam	int v = atoi(val), flags;
1481178354Ssam
1482178354Ssam	flags = getmodeflags(val);
1483178354Ssam	gettxparams(s);
1484178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1485178354Ssam		flags = getcurchan(s)->ic_flags;
1486178354Ssam		_APPLY_RATE1(flags, txparams, mgmtrate, v);
1487178354Ssam	} else
1488178354Ssam		_APPLY_RATE(flags, txparams, mgmtrate, v);
1489178354Ssam	callback_register(settxparams_cb, &txparams);
1490170531Ssam}
1491170531Ssam
1492170531Ssamstatic
1493178354SsamDECL_CMD_FUNC(set80211ucastrate, val, d)
1494170531Ssam{
1495178354Ssam	int v, flags;
1496178354Ssam
1497178354Ssam	gettxparams(s);
1498178354Ssam	flags = getmodeflags(val);
1499178354Ssam	if (isanyarg(val)) {
1500178354Ssam		if (flags == 0) {	/* NB: no flags => current channel */
1501178354Ssam			flags = getcurchan(s)->ic_flags;
1502178354Ssam			_APPLY1(flags, txparams, ucastrate,
1503178354Ssam			    IEEE80211_FIXED_RATE_NONE);
1504178354Ssam		} else
1505178354Ssam			_APPLY(flags, txparams, ucastrate,
1506178354Ssam			    IEEE80211_FIXED_RATE_NONE);
1507178354Ssam	} else {
1508178354Ssam		v = atoi(val);
1509178354Ssam		if (flags == 0) {	/* NB: no flags => current channel */
1510178354Ssam			flags = getcurchan(s)->ic_flags;
1511178354Ssam			_APPLY_RATE1(flags, txparams, ucastrate, v);
1512178354Ssam		} else
1513178354Ssam			_APPLY_RATE(flags, txparams, ucastrate, v);
1514178354Ssam	}
1515178354Ssam	callback_register(settxparams_cb, &txparams);
1516170531Ssam}
1517170531Ssam
1518170531Ssamstatic
1519178354SsamDECL_CMD_FUNC(set80211maxretry, val, d)
1520153354Ssam{
1521178354Ssam	int v = atoi(val), flags;
1522178354Ssam
1523178354Ssam	flags = getmodeflags(val);
1524178354Ssam	gettxparams(s);
1525178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1526178354Ssam		flags = getcurchan(s)->ic_flags;
1527178354Ssam		_APPLY1(flags, txparams, maxretry, v);
1528178354Ssam	} else
1529178354Ssam		_APPLY(flags, txparams, maxretry, v);
1530178354Ssam	callback_register(settxparams_cb, &txparams);
1531153354Ssam}
1532178354Ssam#undef _APPLY_RATE
1533178354Ssam#undef _APPLY
1534178354Ssam#undef IEEE80211_CHAN_HTA
1535178354Ssam#undef IEEE80211_CHAN_HTG
1536153354Ssam
1537153354Ssamstatic
1538148416SsamDECL_CMD_FUNC(set80211fragthreshold, val, d)
1539148416Ssam{
1540148416Ssam	set80211(s, IEEE80211_IOC_FRAGTHRESHOLD,
1541148416Ssam		isundefarg(val) ? IEEE80211_FRAG_MAX : atoi(val), 0, NULL);
1542148416Ssam}
1543148416Ssam
1544160687Ssamstatic
1545160687SsamDECL_CMD_FUNC(set80211bmissthreshold, val, d)
1546160687Ssam{
1547160687Ssam	set80211(s, IEEE80211_IOC_BMISSTHRESHOLD,
1548160687Ssam		isundefarg(val) ? IEEE80211_HWBMISS_MAX : atoi(val), 0, NULL);
1549160687Ssam}
1550160687Ssam
1551170531Ssamstatic void
1552170531Ssamset80211burst(const char *val, int d, int s, const struct afswtch *rafp)
1553170531Ssam{
1554170531Ssam	set80211(s, IEEE80211_IOC_BURST, d, 0, NULL);
1555170531Ssam}
1556170531Ssam
1557170531Ssamstatic void
1558170531Ssamset80211doth(const char *val, int d, int s, const struct afswtch *rafp)
1559170531Ssam{
1560170531Ssam	set80211(s, IEEE80211_IOC_DOTH, d, 0, NULL);
1561170531Ssam}
1562170531Ssam
1563173275Ssamstatic void
1564178354Ssamset80211dfs(const char *val, int d, int s, const struct afswtch *rafp)
1565178354Ssam{
1566178354Ssam	set80211(s, IEEE80211_IOC_DFS, d, 0, NULL);
1567178354Ssam}
1568178354Ssam
1569178354Ssamstatic void
1570173275Ssamset80211shortgi(const char *val, int d, int s, const struct afswtch *rafp)
1571173275Ssam{
1572173275Ssam	set80211(s, IEEE80211_IOC_SHORTGI,
1573173275Ssam		d ? (IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40) : 0,
1574173275Ssam		0, NULL);
1575173275Ssam}
1576173275Ssam
1577173275Ssamstatic void
1578173275Ssamset80211ampdu(const char *val, int d, int s, const struct afswtch *rafp)
1579173275Ssam{
1580173275Ssam	int ampdu;
1581173275Ssam
1582173275Ssam	if (get80211val(s, IEEE80211_IOC_AMPDU, &ampdu) < 0)
1583173275Ssam		errx(-1, "cannot get AMPDU setting");
1584173275Ssam	if (d < 0) {
1585173275Ssam		d = -d;
1586173275Ssam		ampdu &= ~d;
1587173275Ssam	} else
1588173275Ssam		ampdu |= d;
1589173275Ssam	set80211(s, IEEE80211_IOC_AMPDU, ampdu, 0, NULL);
1590173275Ssam}
1591173275Ssam
1592173275Ssamstatic
1593173275SsamDECL_CMD_FUNC(set80211ampdulimit, val, d)
1594173275Ssam{
1595173275Ssam	int v;
1596173275Ssam
1597173275Ssam	switch (atoi(val)) {
1598173275Ssam	case 8:
1599173275Ssam	case 8*1024:
1600173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_8K;
1601173275Ssam		break;
1602173275Ssam	case 16:
1603173275Ssam	case 16*1024:
1604173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_16K;
1605173275Ssam		break;
1606173275Ssam	case 32:
1607173275Ssam	case 32*1024:
1608173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_32K;
1609173275Ssam		break;
1610173275Ssam	case 64:
1611173275Ssam	case 64*1024:
1612173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_64K;
1613173275Ssam		break;
1614173275Ssam	default:
1615173275Ssam		errx(-1, "invalid A-MPDU limit %s", val);
1616173275Ssam	}
1617173275Ssam	set80211(s, IEEE80211_IOC_AMPDU_LIMIT, v, 0, NULL);
1618173275Ssam}
1619173275Ssam
1620173275Ssamstatic
1621173275SsamDECL_CMD_FUNC(set80211ampdudensity, val, d)
1622173275Ssam{
1623173275Ssam	int v;
1624173275Ssam
1625183260Ssam	if (isanyarg(val) || strcasecmp(val, "na") == 0)
1626173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1627173275Ssam	else switch ((int)(atof(val)*4)) {
1628173275Ssam	case 0:
1629173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1630173275Ssam		break;
1631173275Ssam	case 1:
1632173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_025;
1633173275Ssam		break;
1634173275Ssam	case 2:
1635173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_05;
1636173275Ssam		break;
1637173275Ssam	case 4:
1638173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_1;
1639173275Ssam		break;
1640173275Ssam	case 8:
1641173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_2;
1642173275Ssam		break;
1643173275Ssam	case 16:
1644173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_4;
1645173275Ssam		break;
1646173275Ssam	case 32:
1647173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_8;
1648173275Ssam		break;
1649173275Ssam	case 64:
1650173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_16;
1651173275Ssam		break;
1652173275Ssam	default:
1653173275Ssam		errx(-1, "invalid A-MPDU density %s", val);
1654173275Ssam	}
1655173275Ssam	set80211(s, IEEE80211_IOC_AMPDU_DENSITY, v, 0, NULL);
1656173275Ssam}
1657173275Ssam
1658173275Ssamstatic void
1659173275Ssamset80211amsdu(const char *val, int d, int s, const struct afswtch *rafp)
1660173275Ssam{
1661173275Ssam	int amsdu;
1662173275Ssam
1663173275Ssam	if (get80211val(s, IEEE80211_IOC_AMSDU, &amsdu) < 0)
1664187801Ssam		err(-1, "cannot get AMSDU setting");
1665173275Ssam	if (d < 0) {
1666173275Ssam		d = -d;
1667173275Ssam		amsdu &= ~d;
1668173275Ssam	} else
1669173275Ssam		amsdu |= d;
1670173275Ssam	set80211(s, IEEE80211_IOC_AMSDU, amsdu, 0, NULL);
1671173275Ssam}
1672173275Ssam
1673173275Ssamstatic
1674173275SsamDECL_CMD_FUNC(set80211amsdulimit, val, d)
1675173275Ssam{
1676173275Ssam	set80211(s, IEEE80211_IOC_AMSDU_LIMIT, atoi(val), 0, NULL);
1677173275Ssam}
1678173275Ssam
1679173275Ssamstatic void
1680173275Ssamset80211puren(const char *val, int d, int s, const struct afswtch *rafp)
1681173275Ssam{
1682173275Ssam	set80211(s, IEEE80211_IOC_PUREN, d, 0, NULL);
1683173275Ssam}
1684173275Ssam
1685173275Ssamstatic void
1686173275Ssamset80211htcompat(const char *val, int d, int s, const struct afswtch *rafp)
1687173275Ssam{
1688173275Ssam	set80211(s, IEEE80211_IOC_HTCOMPAT, d, 0, NULL);
1689173275Ssam}
1690173275Ssam
1691173275Ssamstatic void
1692173275Ssamset80211htconf(const char *val, int d, int s, const struct afswtch *rafp)
1693173275Ssam{
1694173275Ssam	set80211(s, IEEE80211_IOC_HTCONF, d, 0, NULL);
1695173275Ssam	htconf = d;
1696173275Ssam}
1697173275Ssam
1698173275Ssamstatic void
1699178354Ssamset80211dwds(const char *val, int d, int s, const struct afswtch *rafp)
1700178354Ssam{
1701178354Ssam	set80211(s, IEEE80211_IOC_DWDS, d, 0, NULL);
1702178354Ssam}
1703178354Ssam
1704178354Ssamstatic void
1705173275Ssamset80211inact(const char *val, int d, int s, const struct afswtch *rafp)
1706173275Ssam{
1707173275Ssam	set80211(s, IEEE80211_IOC_INACTIVITY, d, 0, NULL);
1708173275Ssam}
1709173275Ssam
1710173275Ssamstatic void
1711178354Ssamset80211tsn(const char *val, int d, int s, const struct afswtch *rafp)
1712178354Ssam{
1713178354Ssam	set80211(s, IEEE80211_IOC_TSN, d, 0, NULL);
1714178354Ssam}
1715178354Ssam
1716178354Ssamstatic void
1717178354Ssamset80211dotd(const char *val, int d, int s, const struct afswtch *rafp)
1718178354Ssam{
1719178354Ssam	set80211(s, IEEE80211_IOC_DOTD, d, 0, NULL);
1720178354Ssam}
1721178354Ssam
1722183261Ssamstatic void
1723183261Ssamset80211smps(const char *val, int d, int s, const struct afswtch *rafp)
1724183261Ssam{
1725183261Ssam	set80211(s, IEEE80211_IOC_SMPS, d, 0, NULL);
1726183261Ssam}
1727183261Ssam
1728183261Ssamstatic void
1729183261Ssamset80211rifs(const char *val, int d, int s, const struct afswtch *rafp)
1730183261Ssam{
1731183261Ssam	set80211(s, IEEE80211_IOC_RIFS, d, 0, NULL);
1732183261Ssam}
1733183261Ssam
1734186904Ssamstatic
1735186904SsamDECL_CMD_FUNC(set80211tdmaslot, val, d)
1736186904Ssam{
1737186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOT, atoi(val), 0, NULL);
1738186904Ssam}
1739186904Ssam
1740186904Ssamstatic
1741186904SsamDECL_CMD_FUNC(set80211tdmaslotcnt, val, d)
1742186904Ssam{
1743186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOTCNT, atoi(val), 0, NULL);
1744186904Ssam}
1745186904Ssam
1746186904Ssamstatic
1747186904SsamDECL_CMD_FUNC(set80211tdmaslotlen, val, d)
1748186904Ssam{
1749186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOTLEN, atoi(val), 0, NULL);
1750186904Ssam}
1751186904Ssam
1752186904Ssamstatic
1753186904SsamDECL_CMD_FUNC(set80211tdmabintval, val, d)
1754186904Ssam{
1755186904Ssam	set80211(s, IEEE80211_IOC_TDMA_BINTERVAL, atoi(val), 0, NULL);
1756186904Ssam}
1757186904Ssam
1758178354Ssamstatic int
1759178354Ssamregdomain_sort(const void *a, const void *b)
1760178354Ssam{
1761178354Ssam#define	CHAN_ALL \
1762178354Ssam	(IEEE80211_CHAN_ALLTURBO|IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)
1763178354Ssam	const struct ieee80211_channel *ca = a;
1764178354Ssam	const struct ieee80211_channel *cb = b;
1765178354Ssam
1766178354Ssam	return ca->ic_freq == cb->ic_freq ?
1767178354Ssam	    (ca->ic_flags & CHAN_ALL) - (cb->ic_flags & CHAN_ALL) :
1768178354Ssam	    ca->ic_freq - cb->ic_freq;
1769178354Ssam#undef CHAN_ALL
1770178354Ssam}
1771178354Ssam
1772178354Ssamstatic const struct ieee80211_channel *
1773178354Ssamchanlookup(const struct ieee80211_channel chans[], int nchans,
1774178354Ssam	int freq, int flags)
1775178354Ssam{
1776178354Ssam	int i;
1777178354Ssam
1778178354Ssam	flags &= IEEE80211_CHAN_ALLTURBO;
1779178354Ssam	for (i = 0; i < nchans; i++) {
1780178354Ssam		const struct ieee80211_channel *c = &chans[i];
1781178354Ssam		if (c->ic_freq == freq &&
1782178354Ssam		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1783178354Ssam			return c;
1784178354Ssam	}
1785178354Ssam	return NULL;
1786178354Ssam}
1787178354Ssam
1788187841Ssamstatic int
1789187841Ssamchanfind(const struct ieee80211_channel chans[], int nchans, int flags)
1790187841Ssam{
1791187841Ssam	int i;
1792187841Ssam
1793187841Ssam	for (i = 0; i < nchans; i++) {
1794187841Ssam		const struct ieee80211_channel *c = &chans[i];
1795187841Ssam		if ((c->ic_flags & flags) == flags)
1796187841Ssam			return 1;
1797187841Ssam	}
1798187841Ssam	return 0;
1799187841Ssam}
1800187841Ssam
1801188258Ssam/*
1802188258Ssam * Check channel compatibility.
1803188258Ssam */
1804188258Ssamstatic int
1805188258Ssamcheckchan(const struct ieee80211req_chaninfo *avail, int freq, int flags)
1806188258Ssam{
1807188258Ssam	flags &= ~REQ_FLAGS;
1808188258Ssam	/*
1809188258Ssam	 * Check if exact channel is in the calibration table;
1810188258Ssam	 * everything below is to deal with channels that we
1811188258Ssam	 * want to include but that are not explicitly listed.
1812188258Ssam	 */
1813188258Ssam	if (flags & IEEE80211_CHAN_HT40) {
1814188258Ssam		/* NB: we use an HT40 channel center that matches HT20 */
1815188258Ssam		flags = (flags &~ IEEE80211_CHAN_HT40) | IEEE80211_CHAN_HT20;
1816188258Ssam	}
1817188258Ssam	if (chanlookup(avail->ic_chans, avail->ic_nchans, freq, flags) != NULL)
1818188258Ssam		return 1;
1819188258Ssam	if (flags & IEEE80211_CHAN_GSM) {
1820188258Ssam		/*
1821188258Ssam		 * XXX GSM frequency mapping is handled in the kernel
1822188258Ssam		 * so we cannot find them in the calibration table;
1823188258Ssam		 * just accept the channel and the kernel will reject
1824188258Ssam		 * the channel list if it's wrong.
1825188258Ssam		 */
1826188258Ssam		return 1;
1827188258Ssam	}
1828188258Ssam	/*
1829188258Ssam	 * If this is a 1/2 or 1/4 width channel allow it if a full
1830188258Ssam	 * width channel is present for this frequency, and the device
1831188258Ssam	 * supports fractional channels on this band.  This is a hack
1832188258Ssam	 * that avoids bloating the calibration table; it may be better
1833188258Ssam	 * by per-band attributes though (we are effectively calculating
1834188258Ssam	 * this attribute by scanning the channel list ourself).
1835188258Ssam	 */
1836188258Ssam	if ((flags & (IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)) == 0)
1837188258Ssam		return 0;
1838188258Ssam	if (chanlookup(avail->ic_chans, avail->ic_nchans, freq,
1839188258Ssam	    flags &~ (IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)) == NULL)
1840188258Ssam		return 0;
1841188258Ssam	if (flags & IEEE80211_CHAN_HALF) {
1842188258Ssam		return chanfind(avail->ic_chans, avail->ic_nchans,
1843188258Ssam		    IEEE80211_CHAN_HALF |
1844188258Ssam		       (flags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ)));
1845188258Ssam	} else {
1846188258Ssam		return chanfind(avail->ic_chans, avail->ic_nchans,
1847188258Ssam		    IEEE80211_CHAN_QUARTER |
1848188258Ssam			(flags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ)));
1849188258Ssam	}
1850188258Ssam}
1851188258Ssam
1852178354Ssamstatic void
1853178354Ssamregdomain_addchans(struct ieee80211req_chaninfo *ci,
1854178354Ssam	const netband_head *bands,
1855178354Ssam	const struct ieee80211_regdomain *reg,
1856178354Ssam	uint32_t chanFlags,
1857178354Ssam	const struct ieee80211req_chaninfo *avail)
1858178354Ssam{
1859178354Ssam	const struct netband *nb;
1860178354Ssam	const struct freqband *b;
1861178354Ssam	struct ieee80211_channel *c, *prev;
1862188258Ssam	int freq, hi_adj, lo_adj, channelSep;
1863188258Ssam	uint32_t flags;
1864178354Ssam
1865188258Ssam	hi_adj = (chanFlags & IEEE80211_CHAN_HT40U) ? -20 : 0;
1866188258Ssam	lo_adj = (chanFlags & IEEE80211_CHAN_HT40D) ? 20 : 0;
1867178354Ssam	channelSep = (chanFlags & IEEE80211_CHAN_2GHZ) ? 0 : 40;
1868178354Ssam	LIST_FOREACH(nb, bands, next) {
1869178354Ssam		b = nb->band;
1870187842Ssam		if (verbose) {
1871187842Ssam			printf("%s:", __func__);
1872187842Ssam			printb(" chanFlags", chanFlags, IEEE80211_CHAN_BITS);
1873187842Ssam			printb(" bandFlags", nb->flags | b->flags,
1874187842Ssam			    IEEE80211_CHAN_BITS);
1875187842Ssam			putchar('\n');
1876187842Ssam		}
1877178354Ssam		prev = NULL;
1878188258Ssam		for (freq = b->freqStart + lo_adj;
1879188258Ssam		     freq <= b->freqEnd + hi_adj; freq += b->chanSep) {
1880187841Ssam			/*
1881188258Ssam			 * Construct flags for the new channel.  We take
1882188258Ssam			 * the attributes from the band descriptions except
1883188258Ssam			 * for HT40 which is enabled generically (i.e. +/-
1884188258Ssam			 * extension channel) in the band description and
1885188258Ssam			 * then constrained according by channel separation.
1886187841Ssam			 */
1887188258Ssam			flags = nb->flags | b->flags;
1888188258Ssam			if (flags & IEEE80211_CHAN_HT) {
1889188258Ssam				/*
1890188258Ssam				 * HT channels are generated specially; we're
1891188258Ssam				 * called to add HT20, HT40+, and HT40- chan's
1892188258Ssam				 * so we need to expand only band specs for
1893188258Ssam				 * the HT channel type being added.
1894188258Ssam				 */
1895188258Ssam				if ((chanFlags & IEEE80211_CHAN_HT20) &&
1896188258Ssam				    (flags & IEEE80211_CHAN_HT20) == 0) {
1897188258Ssam					if (verbose)
1898188258Ssam						printf("%u: skip, not an "
1899188258Ssam						    "HT20 channel\n", freq);
1900188258Ssam					continue;
1901188258Ssam				}
1902188258Ssam				if ((chanFlags & IEEE80211_CHAN_HT40) &&
1903188258Ssam				    (flags & IEEE80211_CHAN_HT40) == 0) {
1904188258Ssam					if (verbose)
1905188258Ssam						printf("%u: skip, not an "
1906188258Ssam						    "HT40 channel\n", freq);
1907188258Ssam					continue;
1908188258Ssam				}
1909188258Ssam				/*
1910188258Ssam				 * DFS and HT40 don't mix.  This should be
1911188258Ssam				 * expressed in the regdomain database but
1912188258Ssam				 * just in case enforce it here.
1913188258Ssam				 */
1914188258Ssam				if ((chanFlags & IEEE80211_CHAN_HT40) &&
1915188258Ssam				    (flags & IEEE80211_CHAN_DFS)) {
1916188258Ssam					if (verbose)
1917188258Ssam						printf("%u: skip, HT40+DFS "
1918188258Ssam						    "not permitted\n", freq);
1919188258Ssam					continue;
1920188258Ssam				}
1921188258Ssam				/* NB: HT attribute comes from caller */
1922188258Ssam				flags &= ~IEEE80211_CHAN_HT;
1923188258Ssam				flags |= chanFlags & IEEE80211_CHAN_HT;
1924188258Ssam			}
1925188258Ssam			/*
1926188258Ssam			 * Check if device can operate on this frequency.
1927188258Ssam			 */
1928188258Ssam			if (!checkchan(avail, freq, flags)) {
1929187842Ssam				if (verbose) {
1930187842Ssam					printf("%u: skip, ", freq);
1931188258Ssam					printb("flags", flags,
1932187842Ssam					    IEEE80211_CHAN_BITS);
1933187842Ssam					printf(" not available\n");
1934187842Ssam				}
1935178354Ssam				continue;
1936178354Ssam			}
1937188258Ssam			if ((flags & REQ_ECM) && !reg->ecm) {
1938178354Ssam				if (verbose)
1939188258Ssam					printf("%u: skip, ECM channel\n", freq);
1940178354Ssam				continue;
1941178354Ssam			}
1942188258Ssam			if ((flags & REQ_INDOOR) && reg->location == 'O') {
1943178354Ssam				if (verbose)
1944188258Ssam					printf("%u: skip, indoor channel\n",
1945187841Ssam					    freq);
1946178354Ssam				continue;
1947178354Ssam			}
1948178354Ssam			if ((flags & REQ_OUTDOOR) && reg->location == 'I') {
1949178354Ssam				if (verbose)
1950188258Ssam					printf("%u: skip, outdoor channel\n",
1951188258Ssam					    freq);
1952178354Ssam				continue;
1953178354Ssam			}
1954178354Ssam			if ((flags & IEEE80211_CHAN_HT40) &&
1955178354Ssam			    prev != NULL && (freq - prev->ic_freq) < channelSep) {
1956178354Ssam				if (verbose)
1957178354Ssam					printf("%u: skip, only %u channel "
1958178354Ssam					    "separation, need %d\n", freq,
1959178354Ssam					    freq - prev->ic_freq, channelSep);
1960178354Ssam				continue;
1961178354Ssam			}
1962178354Ssam			if (ci->ic_nchans == IEEE80211_CHAN_MAX) {
1963178354Ssam				if (verbose)
1964187842Ssam					printf("%u: skip, channel table full\n",
1965187842Ssam					    freq);
1966178354Ssam				break;
1967178354Ssam			}
1968178354Ssam			c = &ci->ic_chans[ci->ic_nchans++];
1969187801Ssam			memset(c, 0, sizeof(*c));
1970178354Ssam			c->ic_freq = freq;
1971188258Ssam			c->ic_flags = flags;
1972178354Ssam			if (c->ic_flags & IEEE80211_CHAN_DFS)
1973178354Ssam				c->ic_maxregpower = nb->maxPowerDFS;
1974178354Ssam			else
1975178354Ssam				c->ic_maxregpower = nb->maxPower;
1976187842Ssam			if (verbose) {
1977187842Ssam				printf("[%3d] add freq %u ",
1978187842Ssam				    ci->ic_nchans-1, c->ic_freq);
1979187842Ssam				printb("flags", c->ic_flags, IEEE80211_CHAN_BITS);
1980187842Ssam				printf(" power %u\n", c->ic_maxregpower);
1981187842Ssam			}
1982178354Ssam			/* NB: kernel fills in other fields */
1983178354Ssam			prev = c;
1984178354Ssam		}
1985178354Ssam	}
1986178354Ssam}
1987178354Ssam
1988178354Ssamstatic void
1989178354Ssamregdomain_makechannels(
1990178354Ssam	struct ieee80211_regdomain_req *req,
1991178354Ssam	const struct ieee80211_devcaps_req *dc)
1992178354Ssam{
1993178354Ssam	struct regdata *rdp = getregdata();
1994178354Ssam	const struct country *cc;
1995178354Ssam	const struct ieee80211_regdomain *reg = &req->rd;
1996178354Ssam	struct ieee80211req_chaninfo *ci = &req->chaninfo;
1997178354Ssam	const struct regdomain *rd;
1998178354Ssam
1999178354Ssam	/*
2000178354Ssam	 * Locate construction table for new channel list.  We treat
2001178354Ssam	 * the regdomain/SKU as definitive so a country can be in
2002178354Ssam	 * multiple with different properties (e.g. US in FCC+FCC3).
2003178354Ssam	 * If no regdomain is specified then we fallback on the country
2004178354Ssam	 * code to find the associated regdomain since countries always
2005178354Ssam	 * belong to at least one regdomain.
2006178354Ssam	 */
2007178354Ssam	if (reg->regdomain == 0) {
2008178354Ssam		cc = lib80211_country_findbycc(rdp, reg->country);
2009178354Ssam		if (cc == NULL)
2010178354Ssam			errx(1, "internal error, country %d not found",
2011178354Ssam			    reg->country);
2012178354Ssam		rd = cc->rd;
2013178354Ssam	} else
2014178354Ssam		rd = lib80211_regdomain_findbysku(rdp, reg->regdomain);
2015178354Ssam	if (rd == NULL)
2016178354Ssam		errx(1, "internal error, regdomain %d not found",
2017178354Ssam			    reg->regdomain);
2018178354Ssam	if (rd->sku != SKU_DEBUG) {
2019187801Ssam		/*
2020187801Ssam		 * regdomain_addchans incrememnts the channel count for
2021187801Ssam		 * each channel it adds so initialize ic_nchans to zero.
2022187801Ssam		 * Note that we know we have enough space to hold all possible
2023187801Ssam		 * channels because the devcaps list size was used to
2024187801Ssam		 * allocate our request.
2025187801Ssam		 */
2026187801Ssam		ci->ic_nchans = 0;
2027178354Ssam		if (!LIST_EMPTY(&rd->bands_11b))
2028178354Ssam			regdomain_addchans(ci, &rd->bands_11b, reg,
2029178354Ssam			    IEEE80211_CHAN_B, &dc->dc_chaninfo);
2030187841Ssam		if (!LIST_EMPTY(&rd->bands_11g))
2031178354Ssam			regdomain_addchans(ci, &rd->bands_11g, reg,
2032178354Ssam			    IEEE80211_CHAN_G, &dc->dc_chaninfo);
2033187841Ssam		if (!LIST_EMPTY(&rd->bands_11a))
2034178354Ssam			regdomain_addchans(ci, &rd->bands_11a, reg,
2035178354Ssam			    IEEE80211_CHAN_A, &dc->dc_chaninfo);
2036188258Ssam		if (!LIST_EMPTY(&rd->bands_11na) && dc->dc_htcaps != 0) {
2037178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
2038178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT20,
2039178354Ssam			    &dc->dc_chaninfo);
2040188258Ssam			if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2041188258Ssam				regdomain_addchans(ci, &rd->bands_11na, reg,
2042188258Ssam				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U,
2043188258Ssam				    &dc->dc_chaninfo);
2044188258Ssam				regdomain_addchans(ci, &rd->bands_11na, reg,
2045188258Ssam				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D,
2046188258Ssam				    &dc->dc_chaninfo);
2047188258Ssam			}
2048178354Ssam		}
2049188258Ssam		if (!LIST_EMPTY(&rd->bands_11ng) && dc->dc_htcaps != 0) {
2050178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
2051178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT20,
2052178354Ssam			    &dc->dc_chaninfo);
2053188258Ssam			if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2054188258Ssam				regdomain_addchans(ci, &rd->bands_11ng, reg,
2055188258Ssam				    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U,
2056188258Ssam				    &dc->dc_chaninfo);
2057188258Ssam				regdomain_addchans(ci, &rd->bands_11ng, reg,
2058188258Ssam				    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D,
2059188258Ssam				    &dc->dc_chaninfo);
2060188258Ssam			}
2061178354Ssam		}
2062178354Ssam		qsort(ci->ic_chans, ci->ic_nchans, sizeof(ci->ic_chans[0]),
2063178354Ssam		    regdomain_sort);
2064178354Ssam	} else
2065187801Ssam		memcpy(ci, &dc->dc_chaninfo,
2066187801Ssam		    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
2067178354Ssam}
2068178354Ssam
2069178354Ssamstatic void
2070178354Ssamlist_countries(void)
2071178354Ssam{
2072178354Ssam	struct regdata *rdp = getregdata();
2073178354Ssam	const struct country *cp;
2074178354Ssam	const struct regdomain *dp;
2075178354Ssam	int i;
2076178354Ssam
2077178354Ssam	i = 0;
2078178354Ssam	printf("\nCountry codes:\n");
2079178354Ssam	LIST_FOREACH(cp, &rdp->countries, next) {
2080178354Ssam		printf("%2s %-15.15s%s", cp->isoname,
2081178354Ssam		    cp->name, ((i+1)%4) == 0 ? "\n" : " ");
2082178354Ssam		i++;
2083178354Ssam	}
2084178354Ssam	i = 0;
2085178354Ssam	printf("\nRegulatory domains:\n");
2086178354Ssam	LIST_FOREACH(dp, &rdp->domains, next) {
2087178354Ssam		printf("%-15.15s%s", dp->name, ((i+1)%4) == 0 ? "\n" : " ");
2088178354Ssam		i++;
2089178354Ssam	}
2090178354Ssam	printf("\n");
2091178354Ssam}
2092178354Ssam
2093178354Ssamstatic void
2094178354Ssamdefaultcountry(const struct regdomain *rd)
2095178354Ssam{
2096178354Ssam	struct regdata *rdp = getregdata();
2097178354Ssam	const struct country *cc;
2098178354Ssam
2099178354Ssam	cc = lib80211_country_findbycc(rdp, rd->cc->code);
2100178354Ssam	if (cc == NULL)
2101178354Ssam		errx(1, "internal error, ISO country code %d not "
2102178354Ssam		    "defined for regdomain %s", rd->cc->code, rd->name);
2103178354Ssam	regdomain.country = cc->code;
2104178354Ssam	regdomain.isocc[0] = cc->isoname[0];
2105178354Ssam	regdomain.isocc[1] = cc->isoname[1];
2106178354Ssam}
2107178354Ssam
2108178354Ssamstatic
2109178354SsamDECL_CMD_FUNC(set80211regdomain, val, d)
2110178354Ssam{
2111178354Ssam	struct regdata *rdp = getregdata();
2112178354Ssam	const struct regdomain *rd;
2113178354Ssam
2114178354Ssam	rd = lib80211_regdomain_findbyname(rdp, val);
2115178354Ssam	if (rd == NULL) {
2116186104Ssam		char *eptr;
2117186104Ssam		long sku = strtol(val, &eptr, 0);
2118186104Ssam
2119186104Ssam		if (eptr != val)
2120186104Ssam			rd = lib80211_regdomain_findbysku(rdp, sku);
2121186104Ssam		if (eptr == val || rd == NULL)
2122178354Ssam			errx(1, "unknown regdomain %s", val);
2123178354Ssam	}
2124178354Ssam	getregdomain(s);
2125178354Ssam	regdomain.regdomain = rd->sku;
2126178354Ssam	if (regdomain.country == 0 && rd->cc != NULL) {
2127178354Ssam		/*
2128178354Ssam		 * No country code setup and there's a default
2129178354Ssam		 * one for this regdomain fill it in.
2130178354Ssam		 */
2131178354Ssam		defaultcountry(rd);
2132178354Ssam	}
2133178354Ssam	callback_register(setregdomain_cb, &regdomain);
2134178354Ssam}
2135178354Ssam
2136178354Ssamstatic
2137178354SsamDECL_CMD_FUNC(set80211country, val, d)
2138178354Ssam{
2139178354Ssam	struct regdata *rdp = getregdata();
2140178354Ssam	const struct country *cc;
2141178354Ssam
2142178354Ssam	cc = lib80211_country_findbyname(rdp, val);
2143178354Ssam	if (cc == NULL) {
2144186103Ssam		char *eptr;
2145186103Ssam		long code = strtol(val, &eptr, 0);
2146186103Ssam
2147186103Ssam		if (eptr != val)
2148186103Ssam			cc = lib80211_country_findbycc(rdp, code);
2149186103Ssam		if (eptr == val || cc == NULL)
2150178354Ssam			errx(1, "unknown ISO country code %s", val);
2151178354Ssam	}
2152178354Ssam	getregdomain(s);
2153178354Ssam	regdomain.regdomain = cc->rd->sku;
2154178354Ssam	regdomain.country = cc->code;
2155178354Ssam	regdomain.isocc[0] = cc->isoname[0];
2156178354Ssam	regdomain.isocc[1] = cc->isoname[1];
2157178354Ssam	callback_register(setregdomain_cb, &regdomain);
2158178354Ssam}
2159178354Ssam
2160178354Ssamstatic void
2161178354Ssamset80211location(const char *val, int d, int s, const struct afswtch *rafp)
2162178354Ssam{
2163178354Ssam	getregdomain(s);
2164178354Ssam	regdomain.location = d;
2165178354Ssam	callback_register(setregdomain_cb, &regdomain);
2166178354Ssam}
2167178354Ssam
2168178354Ssamstatic void
2169178354Ssamset80211ecm(const char *val, int d, int s, const struct afswtch *rafp)
2170178354Ssam{
2171178354Ssam	getregdomain(s);
2172178354Ssam	regdomain.ecm = d;
2173178354Ssam	callback_register(setregdomain_cb, &regdomain);
2174178354Ssam}
2175178354Ssam
2176178354Ssamstatic void
2177173275SsamLINE_INIT(char c)
2178173275Ssam{
2179173275Ssam	spacer = c;
2180173275Ssam	if (c == '\t')
2181173275Ssam		col = 8;
2182173275Ssam	else
2183173275Ssam		col = 1;
2184173275Ssam}
2185173275Ssam
2186173275Ssamstatic void
2187173275SsamLINE_BREAK(void)
2188173275Ssam{
2189173275Ssam	if (spacer != '\t') {
2190173275Ssam		printf("\n");
2191173275Ssam		spacer = '\t';
2192173275Ssam	}
2193173275Ssam	col = 8;		/* 8-col tab */
2194173275Ssam}
2195173275Ssam
2196173275Ssamstatic void
2197173275SsamLINE_CHECK(const char *fmt, ...)
2198173275Ssam{
2199173275Ssam	char buf[80];
2200173275Ssam	va_list ap;
2201173275Ssam	int n;
2202173275Ssam
2203173275Ssam	va_start(ap, fmt);
2204173275Ssam	n = vsnprintf(buf+1, sizeof(buf)-1, fmt, ap);
2205173275Ssam	va_end(ap);
2206173275Ssam	col += 1+n;
2207173275Ssam	if (col > MAXCOL) {
2208173275Ssam		LINE_BREAK();
2209173275Ssam		col += n;
2210173275Ssam	}
2211173275Ssam	buf[0] = spacer;
2212173275Ssam	printf("%s", buf);
2213173275Ssam	spacer = ' ';
2214173275Ssam}
2215173275Ssam
2216138593Ssamstatic int
2217170531Ssamgetmaxrate(const uint8_t rates[15], uint8_t nrates)
2218138593Ssam{
2219138593Ssam	int i, maxrate = -1;
2220138593Ssam
2221138593Ssam	for (i = 0; i < nrates; i++) {
2222138593Ssam		int rate = rates[i] & IEEE80211_RATE_VAL;
2223138593Ssam		if (rate > maxrate)
2224138593Ssam			maxrate = rate;
2225138593Ssam	}
2226138593Ssam	return maxrate / 2;
2227138593Ssam}
2228138593Ssam
2229138593Ssamstatic const char *
2230138593Ssamgetcaps(int capinfo)
2231138593Ssam{
2232138593Ssam	static char capstring[32];
2233138593Ssam	char *cp = capstring;
2234138593Ssam
2235138593Ssam	if (capinfo & IEEE80211_CAPINFO_ESS)
2236138593Ssam		*cp++ = 'E';
2237138593Ssam	if (capinfo & IEEE80211_CAPINFO_IBSS)
2238138593Ssam		*cp++ = 'I';
2239138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
2240138593Ssam		*cp++ = 'c';
2241138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
2242138593Ssam		*cp++ = 'C';
2243138593Ssam	if (capinfo & IEEE80211_CAPINFO_PRIVACY)
2244138593Ssam		*cp++ = 'P';
2245138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
2246138593Ssam		*cp++ = 'S';
2247138593Ssam	if (capinfo & IEEE80211_CAPINFO_PBCC)
2248138593Ssam		*cp++ = 'B';
2249138593Ssam	if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
2250138593Ssam		*cp++ = 'A';
2251138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
2252138593Ssam		*cp++ = 's';
2253138593Ssam	if (capinfo & IEEE80211_CAPINFO_RSN)
2254138593Ssam		*cp++ = 'R';
2255138593Ssam	if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
2256138593Ssam		*cp++ = 'D';
2257138593Ssam	*cp = '\0';
2258138593Ssam	return capstring;
2259138593Ssam}
2260138593Ssam
2261159885Ssamstatic const char *
2262159885Ssamgetflags(int flags)
2263159885Ssam{
2264159885Ssam	static char flagstring[32];
2265159885Ssam	char *cp = flagstring;
2266159885Ssam
2267159885Ssam	if (flags & IEEE80211_NODE_AUTH)
2268159885Ssam		*cp++ = 'A';
2269159885Ssam	if (flags & IEEE80211_NODE_QOS)
2270159885Ssam		*cp++ = 'Q';
2271159885Ssam	if (flags & IEEE80211_NODE_ERP)
2272159885Ssam		*cp++ = 'E';
2273159885Ssam	if (flags & IEEE80211_NODE_PWR_MGT)
2274159885Ssam		*cp++ = 'P';
2275173275Ssam	if (flags & IEEE80211_NODE_HT) {
2276170531Ssam		*cp++ = 'H';
2277173275Ssam		if (flags & IEEE80211_NODE_HTCOMPAT)
2278173275Ssam			*cp++ = '+';
2279173275Ssam	}
2280173275Ssam	if (flags & IEEE80211_NODE_WPS)
2281173275Ssam		*cp++ = 'W';
2282173275Ssam	if (flags & IEEE80211_NODE_TSN)
2283183261Ssam		*cp++ = 'N';
2284183261Ssam	if (flags & IEEE80211_NODE_AMPDU_TX)
2285173275Ssam		*cp++ = 'T';
2286183261Ssam	if (flags & IEEE80211_NODE_AMPDU_RX)
2287183261Ssam		*cp++ = 'R';
2288183261Ssam	if (flags & IEEE80211_NODE_MIMO_PS) {
2289183261Ssam		*cp++ = 'M';
2290183261Ssam		if (flags & IEEE80211_NODE_MIMO_RTS)
2291183261Ssam			*cp++ = '+';
2292183261Ssam	}
2293183261Ssam	if (flags & IEEE80211_NODE_RIFS)
2294183261Ssam		*cp++ = 'I';
2295159885Ssam	*cp = '\0';
2296159885Ssam	return flagstring;
2297159885Ssam}
2298159885Ssam
2299138593Ssamstatic void
2300138593Ssamprintie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
2301138593Ssam{
2302138593Ssam	printf("%s", tag);
2303138593Ssam	if (verbose) {
2304138593Ssam		maxlen -= strlen(tag)+2;
2305138593Ssam		if (2*ielen > maxlen)
2306138593Ssam			maxlen--;
2307138593Ssam		printf("<");
2308138593Ssam		for (; ielen > 0; ie++, ielen--) {
2309138593Ssam			if (maxlen-- <= 0)
2310138593Ssam				break;
2311138593Ssam			printf("%02x", *ie);
2312138593Ssam		}
2313138593Ssam		if (ielen != 0)
2314138593Ssam			printf("-");
2315138593Ssam		printf(">");
2316138593Ssam	}
2317138593Ssam}
2318138593Ssam
2319170531Ssam#define LE_READ_2(p)					\
2320170531Ssam	((u_int16_t)					\
2321170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2322170531Ssam	  (((const u_int8_t *)(p))[1] <<  8)))
2323170531Ssam#define LE_READ_4(p)					\
2324170531Ssam	((u_int32_t)					\
2325170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2326170531Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
2327170531Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
2328170531Ssam	  (((const u_int8_t *)(p))[3] << 24)))
2329170531Ssam
2330138593Ssam/*
2331170531Ssam * NB: The decoding routines assume a properly formatted ie
2332170531Ssam *     which should be safe as the kernel only retains them
2333170531Ssam *     if they parse ok.
2334170531Ssam */
2335170531Ssam
2336170531Ssamstatic void
2337173275Ssamprintwmeparam(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2338170531Ssam{
2339170531Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
2340170531Ssam	static const char *acnames[] = { "BE", "BK", "VO", "VI" };
2341173275Ssam	const struct ieee80211_wme_param *wme =
2342173275Ssam	    (const struct ieee80211_wme_param *) ie;
2343170531Ssam	int i;
2344170531Ssam
2345170531Ssam	printf("%s", tag);
2346173275Ssam	if (!verbose)
2347173275Ssam		return;
2348173275Ssam	printf("<qosinfo 0x%x", wme->param_qosInfo);
2349173275Ssam	ie += offsetof(struct ieee80211_wme_param, params_acParams);
2350173275Ssam	for (i = 0; i < WME_NUM_AC; i++) {
2351173275Ssam		const struct ieee80211_wme_acparams *ac =
2352173275Ssam		    &wme->params_acParams[i];
2353173275Ssam
2354173275Ssam		printf(" %s[%saifsn %u cwmin %u cwmax %u txop %u]"
2355173275Ssam			, acnames[i]
2356173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_ACM) ? "acm " : ""
2357173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_AIFSN)
2358173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMIN)
2359173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMAX)
2360173275Ssam			, LE_READ_2(&ac->acp_txop)
2361173275Ssam		);
2362170531Ssam	}
2363173275Ssam	printf(">");
2364170531Ssam#undef MS
2365170531Ssam}
2366170531Ssam
2367170531Ssamstatic void
2368173275Ssamprintwmeinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2369173275Ssam{
2370173275Ssam	printf("%s", tag);
2371173275Ssam	if (verbose) {
2372173275Ssam		const struct ieee80211_wme_info *wme =
2373173275Ssam		    (const struct ieee80211_wme_info *) ie;
2374173275Ssam		printf("<version 0x%x info 0x%x>",
2375173275Ssam		    wme->wme_version, wme->wme_info);
2376173275Ssam	}
2377173275Ssam}
2378173275Ssam
2379173275Ssamstatic void
2380173275Ssamprinthtcap(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2381173275Ssam{
2382173275Ssam	printf("%s", tag);
2383173275Ssam	if (verbose) {
2384173275Ssam		const struct ieee80211_ie_htcap *htcap =
2385173275Ssam		    (const struct ieee80211_ie_htcap *) ie;
2386173275Ssam		const char *sep;
2387173275Ssam		int i, j;
2388173275Ssam
2389173275Ssam		printf("<cap 0x%x param 0x%x",
2390173275Ssam		    LE_READ_2(&htcap->hc_cap), htcap->hc_param);
2391173275Ssam		printf(" mcsset[");
2392173275Ssam		sep = "";
2393173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2394173275Ssam			if (isset(htcap->hc_mcsset, i)) {
2395173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2396173275Ssam					if (isclr(htcap->hc_mcsset, j))
2397173275Ssam						break;
2398173275Ssam				j--;
2399173275Ssam				if (i == j)
2400173275Ssam					printf("%s%u", sep, i);
2401173275Ssam				else
2402173275Ssam					printf("%s%u-%u", sep, i, j);
2403173275Ssam				i += j-i;
2404173275Ssam				sep = ",";
2405173275Ssam			}
2406173275Ssam		printf("] extcap 0x%x txbf 0x%x antenna 0x%x>",
2407173275Ssam		    LE_READ_2(&htcap->hc_extcap),
2408173275Ssam		    LE_READ_4(&htcap->hc_txbf),
2409173275Ssam		    htcap->hc_antenna);
2410173275Ssam	}
2411173275Ssam}
2412173275Ssam
2413173275Ssamstatic void
2414173275Ssamprinthtinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2415173275Ssam{
2416173275Ssam	printf("%s", tag);
2417173275Ssam	if (verbose) {
2418173275Ssam		const struct ieee80211_ie_htinfo *htinfo =
2419173275Ssam		    (const struct ieee80211_ie_htinfo *) ie;
2420173275Ssam		const char *sep;
2421173275Ssam		int i, j;
2422173275Ssam
2423173275Ssam		printf("<ctl %u, %x,%x,%x,%x", htinfo->hi_ctrlchannel,
2424173275Ssam		    htinfo->hi_byte1, htinfo->hi_byte2, htinfo->hi_byte3,
2425173275Ssam		    LE_READ_2(&htinfo->hi_byte45));
2426173275Ssam		printf(" basicmcs[");
2427173275Ssam		sep = "";
2428173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2429173275Ssam			if (isset(htinfo->hi_basicmcsset, i)) {
2430173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2431173275Ssam					if (isclr(htinfo->hi_basicmcsset, j))
2432173275Ssam						break;
2433173275Ssam				j--;
2434173275Ssam				if (i == j)
2435173275Ssam					printf("%s%u", sep, i);
2436173275Ssam				else
2437173275Ssam					printf("%s%u-%u", sep, i, j);
2438173275Ssam				i += j-i;
2439173275Ssam				sep = ",";
2440173275Ssam			}
2441173275Ssam		printf("]>");
2442173275Ssam	}
2443173275Ssam}
2444173275Ssam
2445173275Ssamstatic void
2446170531Ssamprintathie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2447170531Ssam{
2448170531Ssam
2449170531Ssam	printf("%s", tag);
2450170531Ssam	if (verbose) {
2451170531Ssam		const struct ieee80211_ath_ie *ath =
2452170531Ssam			(const struct ieee80211_ath_ie *)ie;
2453170531Ssam
2454170531Ssam		printf("<");
2455170531Ssam		if (ath->ath_capability & ATHEROS_CAP_TURBO_PRIME)
2456170531Ssam			printf("DTURBO,");
2457170531Ssam		if (ath->ath_capability & ATHEROS_CAP_COMPRESSION)
2458170531Ssam			printf("COMP,");
2459170531Ssam		if (ath->ath_capability & ATHEROS_CAP_FAST_FRAME)
2460170531Ssam			printf("FF,");
2461170531Ssam		if (ath->ath_capability & ATHEROS_CAP_XR)
2462170531Ssam			printf("XR,");
2463170531Ssam		if (ath->ath_capability & ATHEROS_CAP_AR)
2464170531Ssam			printf("AR,");
2465170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BURST)
2466170531Ssam			printf("BURST,");
2467170531Ssam		if (ath->ath_capability & ATHEROS_CAP_WME)
2468170531Ssam			printf("WME,");
2469170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BOOST)
2470170531Ssam			printf("BOOST,");
2471170531Ssam		printf("0x%x>", LE_READ_2(ath->ath_defkeyix));
2472170531Ssam	}
2473170531Ssam}
2474170531Ssam
2475170531Ssamstatic const char *
2476170531Ssamwpa_cipher(const u_int8_t *sel)
2477170531Ssam{
2478170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2479170531Ssam	u_int32_t w = LE_READ_4(sel);
2480170531Ssam
2481170531Ssam	switch (w) {
2482170531Ssam	case WPA_SEL(WPA_CSE_NULL):
2483170531Ssam		return "NONE";
2484170531Ssam	case WPA_SEL(WPA_CSE_WEP40):
2485170531Ssam		return "WEP40";
2486170531Ssam	case WPA_SEL(WPA_CSE_WEP104):
2487170531Ssam		return "WEP104";
2488170531Ssam	case WPA_SEL(WPA_CSE_TKIP):
2489170531Ssam		return "TKIP";
2490170531Ssam	case WPA_SEL(WPA_CSE_CCMP):
2491170531Ssam		return "AES-CCMP";
2492170531Ssam	}
2493170531Ssam	return "?";		/* NB: so 1<< is discarded */
2494170531Ssam#undef WPA_SEL
2495170531Ssam}
2496170531Ssam
2497170531Ssamstatic const char *
2498170531Ssamwpa_keymgmt(const u_int8_t *sel)
2499170531Ssam{
2500170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2501170531Ssam	u_int32_t w = LE_READ_4(sel);
2502170531Ssam
2503170531Ssam	switch (w) {
2504170531Ssam	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
2505170531Ssam		return "8021X-UNSPEC";
2506170531Ssam	case WPA_SEL(WPA_ASE_8021X_PSK):
2507170531Ssam		return "8021X-PSK";
2508170531Ssam	case WPA_SEL(WPA_ASE_NONE):
2509170531Ssam		return "NONE";
2510170531Ssam	}
2511170531Ssam	return "?";
2512170531Ssam#undef WPA_SEL
2513170531Ssam}
2514170531Ssam
2515170531Ssamstatic void
2516170531Ssamprintwpaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2517170531Ssam{
2518170531Ssam	u_int8_t len = ie[1];
2519170531Ssam
2520170531Ssam	printf("%s", tag);
2521170531Ssam	if (verbose) {
2522170531Ssam		const char *sep;
2523170531Ssam		int n;
2524170531Ssam
2525170531Ssam		ie += 6, len -= 4;		/* NB: len is payload only */
2526170531Ssam
2527170531Ssam		printf("<v%u", LE_READ_2(ie));
2528170531Ssam		ie += 2, len -= 2;
2529170531Ssam
2530170531Ssam		printf(" mc:%s", wpa_cipher(ie));
2531170531Ssam		ie += 4, len -= 4;
2532170531Ssam
2533170531Ssam		/* unicast ciphers */
2534170531Ssam		n = LE_READ_2(ie);
2535170531Ssam		ie += 2, len -= 2;
2536170531Ssam		sep = " uc:";
2537170531Ssam		for (; n > 0; n--) {
2538170531Ssam			printf("%s%s", sep, wpa_cipher(ie));
2539170531Ssam			ie += 4, len -= 4;
2540170531Ssam			sep = "+";
2541170531Ssam		}
2542170531Ssam
2543170531Ssam		/* key management algorithms */
2544170531Ssam		n = LE_READ_2(ie);
2545170531Ssam		ie += 2, len -= 2;
2546170531Ssam		sep = " km:";
2547170531Ssam		for (; n > 0; n--) {
2548170531Ssam			printf("%s%s", sep, wpa_keymgmt(ie));
2549170531Ssam			ie += 4, len -= 4;
2550170531Ssam			sep = "+";
2551170531Ssam		}
2552170531Ssam
2553170531Ssam		if (len > 2)		/* optional capabilities */
2554170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2555170531Ssam		printf(">");
2556170531Ssam	}
2557170531Ssam}
2558170531Ssam
2559170531Ssamstatic const char *
2560170531Ssamrsn_cipher(const u_int8_t *sel)
2561170531Ssam{
2562170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2563170531Ssam	u_int32_t w = LE_READ_4(sel);
2564170531Ssam
2565170531Ssam	switch (w) {
2566170531Ssam	case RSN_SEL(RSN_CSE_NULL):
2567170531Ssam		return "NONE";
2568170531Ssam	case RSN_SEL(RSN_CSE_WEP40):
2569170531Ssam		return "WEP40";
2570170531Ssam	case RSN_SEL(RSN_CSE_WEP104):
2571170531Ssam		return "WEP104";
2572170531Ssam	case RSN_SEL(RSN_CSE_TKIP):
2573170531Ssam		return "TKIP";
2574170531Ssam	case RSN_SEL(RSN_CSE_CCMP):
2575170531Ssam		return "AES-CCMP";
2576170531Ssam	case RSN_SEL(RSN_CSE_WRAP):
2577170531Ssam		return "AES-OCB";
2578170531Ssam	}
2579170531Ssam	return "?";
2580170531Ssam#undef WPA_SEL
2581170531Ssam}
2582170531Ssam
2583170531Ssamstatic const char *
2584170531Ssamrsn_keymgmt(const u_int8_t *sel)
2585170531Ssam{
2586170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2587170531Ssam	u_int32_t w = LE_READ_4(sel);
2588170531Ssam
2589170531Ssam	switch (w) {
2590170531Ssam	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
2591170531Ssam		return "8021X-UNSPEC";
2592170531Ssam	case RSN_SEL(RSN_ASE_8021X_PSK):
2593170531Ssam		return "8021X-PSK";
2594170531Ssam	case RSN_SEL(RSN_ASE_NONE):
2595170531Ssam		return "NONE";
2596170531Ssam	}
2597170531Ssam	return "?";
2598170531Ssam#undef RSN_SEL
2599170531Ssam}
2600170531Ssam
2601170531Ssamstatic void
2602170531Ssamprintrsnie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2603170531Ssam{
2604170531Ssam	printf("%s", tag);
2605170531Ssam	if (verbose) {
2606170531Ssam		const char *sep;
2607170531Ssam		int n;
2608170531Ssam
2609173275Ssam		ie += 2, ielen -= 2;
2610170531Ssam
2611170531Ssam		printf("<v%u", LE_READ_2(ie));
2612173275Ssam		ie += 2, ielen -= 2;
2613170531Ssam
2614170531Ssam		printf(" mc:%s", rsn_cipher(ie));
2615173275Ssam		ie += 4, ielen -= 4;
2616170531Ssam
2617170531Ssam		/* unicast ciphers */
2618170531Ssam		n = LE_READ_2(ie);
2619173275Ssam		ie += 2, ielen -= 2;
2620170531Ssam		sep = " uc:";
2621170531Ssam		for (; n > 0; n--) {
2622170531Ssam			printf("%s%s", sep, rsn_cipher(ie));
2623173275Ssam			ie += 4, ielen -= 4;
2624170531Ssam			sep = "+";
2625170531Ssam		}
2626170531Ssam
2627170531Ssam		/* key management algorithms */
2628170531Ssam		n = LE_READ_2(ie);
2629173275Ssam		ie += 2, ielen -= 2;
2630170531Ssam		sep = " km:";
2631170531Ssam		for (; n > 0; n--) {
2632170531Ssam			printf("%s%s", sep, rsn_keymgmt(ie));
2633173275Ssam			ie += 4, ielen -= 4;
2634170531Ssam			sep = "+";
2635170531Ssam		}
2636170531Ssam
2637173275Ssam		if (ielen > 2)		/* optional capabilities */
2638170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2639170531Ssam		/* XXXPMKID */
2640170531Ssam		printf(">");
2641170531Ssam	}
2642170531Ssam}
2643170531Ssam
2644181454Ssam/* XXX move to a public include file */
2645181454Ssam#define IEEE80211_WPS_DEV_PASS_ID	0x1012
2646181454Ssam#define IEEE80211_WPS_SELECTED_REG	0x1041
2647181454Ssam#define IEEE80211_WPS_SETUP_STATE	0x1044
2648181454Ssam#define IEEE80211_WPS_UUID_E		0x1047
2649181454Ssam#define IEEE80211_WPS_VERSION		0x104a
2650181454Ssam
2651181454Ssam#define BE_READ_2(p)					\
2652181454Ssam	((u_int16_t)					\
2653181454Ssam	 ((((const u_int8_t *)(p))[1]      ) |		\
2654181454Ssam	  (((const u_int8_t *)(p))[0] <<  8)))
2655181454Ssam
2656181454Ssamstatic void
2657181454Ssamprintwpsie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2658181454Ssam{
2659181454Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
2660181454Ssam	u_int8_t len = ie[1];
2661181454Ssam
2662181454Ssam	printf("%s", tag);
2663181454Ssam	if (verbose) {
2664181454Ssam		static const char *dev_pass_id[] = {
2665181454Ssam			"D",	/* Default (PIN) */
2666181454Ssam			"U",	/* User-specified */
2667181454Ssam			"M",	/* Machine-specified */
2668181454Ssam			"K",	/* Rekey */
2669181454Ssam			"P",	/* PushButton */
2670181454Ssam			"R"	/* Registrar-specified */
2671181454Ssam		};
2672181454Ssam		int n;
2673181454Ssam
2674181454Ssam		ie +=6, len -= 4;		/* NB: len is payload only */
2675181454Ssam
2676181454Ssam		/* WPS IE in Beacon and Probe Resp frames have different fields */
2677181454Ssam		printf("<");
2678181454Ssam		while (len) {
2679181454Ssam			uint16_t tlv_type = BE_READ_2(ie);
2680181454Ssam			uint16_t tlv_len  = BE_READ_2(ie + 2);
2681181454Ssam
2682181454Ssam			ie += 4, len -= 4;
2683181454Ssam
2684181454Ssam			switch (tlv_type) {
2685181454Ssam			case IEEE80211_WPS_VERSION:
2686181454Ssam				printf("v:%d.%d", *ie >> 4, *ie & 0xf);
2687181454Ssam				break;
2688181454Ssam			case IEEE80211_WPS_SETUP_STATE:
2689181454Ssam				/* Only 1 and 2 are valid */
2690181454Ssam				if (*ie == 0 || *ie >= 3)
2691181454Ssam					printf(" state:B");
2692181454Ssam				else
2693181454Ssam					printf(" st:%s", *ie == 1 ? "N" : "C");
2694181454Ssam				break;
2695181454Ssam			case IEEE80211_WPS_SELECTED_REG:
2696181454Ssam				printf(" sel:%s", *ie ? "T" : "F");
2697181454Ssam				break;
2698181454Ssam			case IEEE80211_WPS_DEV_PASS_ID:
2699181454Ssam				n = LE_READ_2(ie);
2700181454Ssam				if (n < N(dev_pass_id))
2701181454Ssam					printf(" dpi:%s", dev_pass_id[n]);
2702181454Ssam				break;
2703181454Ssam			case IEEE80211_WPS_UUID_E:
2704181454Ssam				printf(" uuid-e:");
2705181454Ssam				for (n = 0; n < (tlv_len - 1); n++)
2706181454Ssam					printf("%02x-", ie[n]);
2707181454Ssam				printf("%02x", ie[n]);
2708181454Ssam				break;
2709181454Ssam			}
2710181454Ssam			ie += tlv_len, len -= tlv_len;
2711181454Ssam		}
2712181454Ssam		printf(">");
2713181454Ssam	}
2714181454Ssam#undef N
2715181454Ssam}
2716181454Ssam
2717186904Ssamstatic void
2718186904Ssamprinttdmaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2719186904Ssam{
2720186904Ssam	printf("%s", tag);
2721186904Ssam	if (verbose && ielen >= sizeof(struct ieee80211_tdma_param)) {
2722186904Ssam		const struct ieee80211_tdma_param *tdma =
2723186904Ssam		   (const struct ieee80211_tdma_param *) ie;
2724186904Ssam
2725186904Ssam		/* XXX tstamp */
2726186904Ssam		printf("<v%u slot:%u slotcnt:%u slotlen:%u bintval:%u inuse:0x%x>",
2727186904Ssam		    tdma->tdma_version, tdma->tdma_slot, tdma->tdma_slotcnt,
2728186904Ssam		    LE_READ_2(&tdma->tdma_slotlen), tdma->tdma_bintval,
2729186904Ssam		    tdma->tdma_inuse[0]);
2730186904Ssam	}
2731186904Ssam}
2732186904Ssam
2733170531Ssam/*
2734138593Ssam * Copy the ssid string contents into buf, truncating to fit.  If the
2735138593Ssam * ssid is entirely printable then just copy intact.  Otherwise convert
2736138593Ssam * to hexadecimal.  If the result is truncated then replace the last
2737138593Ssam * three characters with "...".
2738138593Ssam */
2739146873Sjhbstatic int
2740138593Ssamcopy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
2741138593Ssam{
2742138593Ssam	const u_int8_t *p;
2743138593Ssam	size_t maxlen;
2744138593Ssam	int i;
2745138593Ssam
2746138593Ssam	if (essid_len > bufsize)
2747138593Ssam		maxlen = bufsize;
2748138593Ssam	else
2749138593Ssam		maxlen = essid_len;
2750138593Ssam	/* determine printable or not */
2751138593Ssam	for (i = 0, p = essid; i < maxlen; i++, p++) {
2752138593Ssam		if (*p < ' ' || *p > 0x7e)
2753138593Ssam			break;
2754138593Ssam	}
2755138593Ssam	if (i != maxlen) {		/* not printable, print as hex */
2756138593Ssam		if (bufsize < 3)
2757138593Ssam			return 0;
2758138593Ssam		strlcpy(buf, "0x", bufsize);
2759138593Ssam		bufsize -= 2;
2760138593Ssam		p = essid;
2761138593Ssam		for (i = 0; i < maxlen && bufsize >= 2; i++) {
2762147489Savatar			sprintf(&buf[2+2*i], "%02x", p[i]);
2763138593Ssam			bufsize -= 2;
2764138593Ssam		}
2765147489Savatar		if (i != essid_len)
2766147489Savatar			memcpy(&buf[2+2*i-3], "...", 3);
2767138593Ssam	} else {			/* printable, truncate as needed */
2768138593Ssam		memcpy(buf, essid, maxlen);
2769147489Savatar		if (maxlen != essid_len)
2770147489Savatar			memcpy(&buf[maxlen-3], "...", 3);
2771138593Ssam	}
2772138593Ssam	return maxlen;
2773138593Ssam}
2774138593Ssam
2775173275Ssamstatic void
2776173275Ssamprintssid(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2777173275Ssam{
2778173275Ssam	char ssid[2*IEEE80211_NWID_LEN+1];
2779173275Ssam
2780173275Ssam	printf("%s<%.*s>", tag, copy_essid(ssid, maxlen, ie+2, ie[1]), ssid);
2781173275Ssam}
2782173275Ssam
2783173275Ssamstatic void
2784173275Ssamprintrates(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2785173275Ssam{
2786173275Ssam	const char *sep;
2787173275Ssam	int i;
2788173275Ssam
2789173275Ssam	printf("%s", tag);
2790173275Ssam	sep = "<";
2791173275Ssam	for (i = 2; i < ielen; i++) {
2792173275Ssam		printf("%s%s%d", sep,
2793173275Ssam		    ie[i] & IEEE80211_RATE_BASIC ? "B" : "",
2794173275Ssam		    ie[i] & IEEE80211_RATE_VAL);
2795173275Ssam		sep = ",";
2796173275Ssam	}
2797173275Ssam	printf(">");
2798173275Ssam}
2799173275Ssam
2800173275Ssamstatic void
2801173275Ssamprintcountry(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2802173275Ssam{
2803173275Ssam	const struct ieee80211_country_ie *cie =
2804173275Ssam	   (const struct ieee80211_country_ie *) ie;
2805173275Ssam	int i, nbands, schan, nchan;
2806173275Ssam
2807173275Ssam	printf("%s<%c%c%c", tag, cie->cc[0], cie->cc[1], cie->cc[2]);
2808173275Ssam	nbands = (cie->len - 3) / sizeof(cie->band[0]);
2809173275Ssam	for (i = 0; i < nbands; i++) {
2810173275Ssam		schan = cie->band[i].schan;
2811173275Ssam		nchan = cie->band[i].nchan;
2812173275Ssam		if (nchan != 1)
2813173275Ssam			printf(" %u-%u,%u", schan, schan + nchan-1,
2814173275Ssam			    cie->band[i].maxtxpwr);
2815173275Ssam		else
2816173275Ssam			printf(" %u,%u", schan, cie->band[i].maxtxpwr);
2817173275Ssam	}
2818173275Ssam	printf(">");
2819173275Ssam}
2820173275Ssam
2821148686Sstefanf/* unaligned little endian access */
2822138593Ssam#define LE_READ_4(p)					\
2823138593Ssam	((u_int32_t)					\
2824138593Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2825138593Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
2826138593Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
2827138593Ssam	  (((const u_int8_t *)(p))[3] << 24)))
2828138593Ssam
2829178354Ssamstatic __inline int
2830138593Ssamiswpaoui(const u_int8_t *frm)
2831138593Ssam{
2832138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
2833138593Ssam}
2834138593Ssam
2835178354Ssamstatic __inline int
2836173275Ssamiswmeinfo(const u_int8_t *frm)
2837138593Ssam{
2838173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
2839173275Ssam		frm[6] == WME_INFO_OUI_SUBTYPE;
2840138593Ssam}
2841138593Ssam
2842178354Ssamstatic __inline int
2843173275Ssamiswmeparam(const u_int8_t *frm)
2844173275Ssam{
2845173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
2846173275Ssam		frm[6] == WME_PARAM_OUI_SUBTYPE;
2847173275Ssam}
2848173275Ssam
2849178354Ssamstatic __inline int
2850138593Ssamisatherosoui(const u_int8_t *frm)
2851138593Ssam{
2852138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
2853138593Ssam}
2854138593Ssam
2855181454Ssamstatic __inline int
2856186904Ssamistdmaoui(const uint8_t *frm)
2857186904Ssam{
2858186904Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((TDMA_OUI_TYPE<<24)|TDMA_OUI);
2859186904Ssam}
2860186904Ssam
2861186904Ssamstatic __inline int
2862181454Ssamiswpsoui(const uint8_t *frm)
2863181454Ssam{
2864181454Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPS_OUI_TYPE<<24)|WPA_OUI);
2865181454Ssam}
2866181454Ssam
2867173275Ssamstatic const char *
2868173275Ssamiename(int elemid)
2869173275Ssam{
2870173275Ssam	switch (elemid) {
2871173275Ssam	case IEEE80211_ELEMID_FHPARMS:	return " FHPARMS";
2872173275Ssam	case IEEE80211_ELEMID_CFPARMS:	return " CFPARMS";
2873173275Ssam	case IEEE80211_ELEMID_TIM:	return " TIM";
2874173275Ssam	case IEEE80211_ELEMID_IBSSPARMS:return " IBSSPARMS";
2875173275Ssam	case IEEE80211_ELEMID_CHALLENGE:return " CHALLENGE";
2876173275Ssam	case IEEE80211_ELEMID_PWRCNSTR:	return " PWRCNSTR";
2877173275Ssam	case IEEE80211_ELEMID_PWRCAP:	return " PWRCAP";
2878173275Ssam	case IEEE80211_ELEMID_TPCREQ:	return " TPCREQ";
2879173275Ssam	case IEEE80211_ELEMID_TPCREP:	return " TPCREP";
2880173275Ssam	case IEEE80211_ELEMID_SUPPCHAN:	return " SUPPCHAN";
2881173275Ssam	case IEEE80211_ELEMID_CHANSWITCHANN:return " CSA";
2882173275Ssam	case IEEE80211_ELEMID_MEASREQ:	return " MEASREQ";
2883173275Ssam	case IEEE80211_ELEMID_MEASREP:	return " MEASREP";
2884173275Ssam	case IEEE80211_ELEMID_QUIET:	return " QUIET";
2885173275Ssam	case IEEE80211_ELEMID_IBSSDFS:	return " IBSSDFS";
2886173275Ssam	case IEEE80211_ELEMID_TPC:	return " TPC";
2887173275Ssam	case IEEE80211_ELEMID_CCKM:	return " CCKM";
2888173275Ssam	}
2889173275Ssam	return " ???";
2890173275Ssam}
2891173275Ssam
2892138593Ssamstatic void
2893138593Ssamprinties(const u_int8_t *vp, int ielen, int maxcols)
2894138593Ssam{
2895138593Ssam	while (ielen > 0) {
2896138593Ssam		switch (vp[0]) {
2897173275Ssam		case IEEE80211_ELEMID_SSID:
2898173275Ssam			if (verbose)
2899173275Ssam				printssid(" SSID", vp, 2+vp[1], maxcols);
2900173275Ssam			break;
2901173275Ssam		case IEEE80211_ELEMID_RATES:
2902173275Ssam		case IEEE80211_ELEMID_XRATES:
2903173275Ssam			if (verbose)
2904173275Ssam				printrates(vp[0] == IEEE80211_ELEMID_RATES ?
2905173275Ssam				    " RATES" : " XRATES", vp, 2+vp[1], maxcols);
2906173275Ssam			break;
2907173275Ssam		case IEEE80211_ELEMID_DSPARMS:
2908173275Ssam			if (verbose)
2909173275Ssam				printf(" DSPARMS<%u>", vp[2]);
2910173275Ssam			break;
2911173275Ssam		case IEEE80211_ELEMID_COUNTRY:
2912173275Ssam			if (verbose)
2913173275Ssam				printcountry(" COUNTRY", vp, 2+vp[1], maxcols);
2914173275Ssam			break;
2915173275Ssam		case IEEE80211_ELEMID_ERP:
2916173275Ssam			if (verbose)
2917173275Ssam				printf(" ERP<0x%x>", vp[2]);
2918173275Ssam			break;
2919138593Ssam		case IEEE80211_ELEMID_VENDOR:
2920138593Ssam			if (iswpaoui(vp))
2921170531Ssam				printwpaie(" WPA", vp, 2+vp[1], maxcols);
2922173275Ssam			else if (iswmeinfo(vp))
2923173275Ssam				printwmeinfo(" WME", vp, 2+vp[1], maxcols);
2924173275Ssam			else if (iswmeparam(vp))
2925173275Ssam				printwmeparam(" WME", vp, 2+vp[1], maxcols);
2926139492Ssam			else if (isatherosoui(vp))
2927170531Ssam				printathie(" ATH", vp, 2+vp[1], maxcols);
2928181454Ssam			else if (iswpsoui(vp))
2929181454Ssam				printwpsie(" WPS", vp, 2+vp[1], maxcols);
2930186904Ssam			else if (istdmaoui(vp))
2931186904Ssam				printtdmaie(" TDMA", vp, 2+vp[1], maxcols);
2932173275Ssam			else if (verbose)
2933138593Ssam				printie(" VEN", vp, 2+vp[1], maxcols);
2934138593Ssam			break;
2935138593Ssam		case IEEE80211_ELEMID_RSN:
2936170531Ssam			printrsnie(" RSN", vp, 2+vp[1], maxcols);
2937138593Ssam			break;
2938173275Ssam		case IEEE80211_ELEMID_HTCAP:
2939173275Ssam			printhtcap(" HTCAP", vp, 2+vp[1], maxcols);
2940173275Ssam			break;
2941173275Ssam		case IEEE80211_ELEMID_HTINFO:
2942173275Ssam			if (verbose)
2943173275Ssam				printhtinfo(" HTINFO", vp, 2+vp[1], maxcols);
2944173275Ssam			break;
2945138593Ssam		default:
2946173275Ssam			if (verbose)
2947173275Ssam				printie(iename(vp[0]), vp, 2+vp[1], maxcols);
2948138593Ssam			break;
2949138593Ssam		}
2950138593Ssam		ielen -= 2+vp[1];
2951138593Ssam		vp += 2+vp[1];
2952138593Ssam	}
2953138593Ssam}
2954138593Ssam
2955138593Ssamstatic void
2956178354Ssamprintmimo(const struct ieee80211_mimo_info *mi)
2957178354Ssam{
2958178354Ssam	/* NB: don't muddy display unless there's something to show */
2959178354Ssam	if (mi->rssi[0] != 0 || mi->rssi[1] != 0 || mi->rssi[2] != 0) {
2960178354Ssam		/* XXX ignore EVM for now */
2961178354Ssam		printf(" (rssi %d:%d:%d nf %d:%d:%d)",
2962178354Ssam		    mi->rssi[0], mi->rssi[1], mi->rssi[2],
2963178354Ssam		    mi->noise[0], mi->noise[1], mi->noise[2]);
2964178354Ssam	}
2965178354Ssam}
2966178354Ssam
2967178354Ssamstatic void
2968138593Ssamlist_scan(int s)
2969138593Ssam{
2970138593Ssam	uint8_t buf[24*1024];
2971153892Srwatson	char ssid[IEEE80211_NWID_LEN+1];
2972173275Ssam	const uint8_t *cp;
2973154522Ssam	int len, ssidmax;
2974138593Ssam
2975173275Ssam	if (get80211len(s, IEEE80211_IOC_SCAN_RESULTS, buf, sizeof(buf), &len) < 0)
2976138593Ssam		errx(1, "unable to get scan results");
2977138593Ssam	if (len < sizeof(struct ieee80211req_scan_result))
2978138593Ssam		return;
2979138593Ssam
2980170531Ssam	getchaninfo(s);
2981170531Ssam
2982154522Ssam	ssidmax = verbose ? IEEE80211_NWID_LEN : 14;
2983170531Ssam	printf("%-*.*s  %-17.17s  %4s %4s  %-7s  %3s %4s\n"
2984154522Ssam		, ssidmax, ssidmax, "SSID"
2985138593Ssam		, "BSSID"
2986138593Ssam		, "CHAN"
2987138593Ssam		, "RATE"
2988170531Ssam		, " S:N"
2989138593Ssam		, "INT"
2990138593Ssam		, "CAPS"
2991138593Ssam	);
2992138593Ssam	cp = buf;
2993138593Ssam	do {
2994170531Ssam		const struct ieee80211req_scan_result *sr;
2995170531Ssam		const uint8_t *vp;
2996138593Ssam
2997170531Ssam		sr = (const struct ieee80211req_scan_result *) cp;
2998173275Ssam		vp = cp + sr->isr_ie_off;
2999170531Ssam		printf("%-*.*s  %s  %3d  %3dM %3d:%-3d  %3d %-4.4s"
3000154522Ssam			, ssidmax
3001155461Ssam			  , copy_essid(ssid, ssidmax, vp, sr->isr_ssid_len)
3002154522Ssam			  , ssid
3003138593Ssam			, ether_ntoa((const struct ether_addr *) sr->isr_bssid)
3004165570Ssam			, ieee80211_mhz2ieee(sr->isr_freq, sr->isr_flags)
3005138593Ssam			, getmaxrate(sr->isr_rates, sr->isr_nrates)
3006170531Ssam			, (sr->isr_rssi/2)+sr->isr_noise, sr->isr_noise
3007138593Ssam			, sr->isr_intval
3008138593Ssam			, getcaps(sr->isr_capinfo)
3009138593Ssam		);
3010170531Ssam		printies(vp + sr->isr_ssid_len, sr->isr_ie_len, 24);
3011138593Ssam		printf("\n");
3012138593Ssam		cp += sr->isr_len, len -= sr->isr_len;
3013138593Ssam	} while (len >= sizeof(struct ieee80211req_scan_result));
3014138593Ssam}
3015138593Ssam
3016178354Ssam#ifdef __FreeBSD__
3017138593Ssam#include <net80211/ieee80211_freebsd.h>
3018178354Ssam#endif
3019178354Ssam#ifdef __NetBSD__
3020178354Ssam#include <net80211/ieee80211_netbsd.h>
3021178354Ssam#endif
3022138593Ssam
3023138593Ssamstatic void
3024138593Ssamscan_and_wait(int s)
3025138593Ssam{
3026178354Ssam	struct ieee80211_scan_req sr;
3027138593Ssam	struct ieee80211req ireq;
3028138593Ssam	int sroute;
3029138593Ssam
3030138593Ssam	sroute = socket(PF_ROUTE, SOCK_RAW, 0);
3031138593Ssam	if (sroute < 0) {
3032138593Ssam		perror("socket(PF_ROUTE,SOCK_RAW)");
3033138593Ssam		return;
3034138593Ssam	}
3035138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3036138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3037138593Ssam	ireq.i_type = IEEE80211_IOC_SCAN_REQ;
3038178354Ssam
3039178354Ssam	memset(&sr, 0, sizeof(sr));
3040178354Ssam	sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE
3041178354Ssam		    | IEEE80211_IOC_SCAN_NOPICK
3042178354Ssam		    | IEEE80211_IOC_SCAN_ONCE;
3043178354Ssam	sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
3044178354Ssam	sr.sr_nssid = 0;
3045178354Ssam
3046178354Ssam	ireq.i_data = &sr;
3047178354Ssam	ireq.i_len = sizeof(sr);
3048138593Ssam	/* NB: only root can trigger a scan so ignore errors */
3049138593Ssam	if (ioctl(s, SIOCS80211, &ireq) >= 0) {
3050138593Ssam		char buf[2048];
3051138593Ssam		struct if_announcemsghdr *ifan;
3052138593Ssam		struct rt_msghdr *rtm;
3053138593Ssam
3054138593Ssam		do {
3055138593Ssam			if (read(sroute, buf, sizeof(buf)) < 0) {
3056138593Ssam				perror("read(PF_ROUTE)");
3057138593Ssam				break;
3058138593Ssam			}
3059138593Ssam			rtm = (struct rt_msghdr *) buf;
3060138593Ssam			if (rtm->rtm_version != RTM_VERSION)
3061138593Ssam				break;
3062138593Ssam			ifan = (struct if_announcemsghdr *) rtm;
3063138593Ssam		} while (rtm->rtm_type != RTM_IEEE80211 ||
3064138593Ssam		    ifan->ifan_what != RTM_IEEE80211_SCAN);
3065138593Ssam	}
3066138593Ssam	close(sroute);
3067138593Ssam}
3068138593Ssam
3069138593Ssamstatic
3070138593SsamDECL_CMD_FUNC(set80211scan, val, d)
3071138593Ssam{
3072138593Ssam	scan_and_wait(s);
3073138593Ssam	list_scan(s);
3074138593Ssam}
3075138593Ssam
3076161147Ssamstatic enum ieee80211_opmode get80211opmode(int s);
3077161147Ssam
3078173275Ssamstatic int
3079173275Ssamgettxseq(const struct ieee80211req_sta_info *si)
3080173275Ssam{
3081173275Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
3082173275Ssam
3083173275Ssam	int i, txseq;
3084173275Ssam
3085173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3086173275Ssam		return si->isi_txseqs[0];
3087173275Ssam	/* XXX not right but usually what folks want */
3088173275Ssam	txseq = 0;
3089173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3090173275Ssam		if (si->isi_txseqs[i] > txseq)
3091173275Ssam			txseq = si->isi_txseqs[i];
3092173275Ssam	return txseq;
3093173275Ssam#undef IEEE80211_NODE_QOS
3094173275Ssam}
3095173275Ssam
3096173275Ssamstatic int
3097173275Ssamgetrxseq(const struct ieee80211req_sta_info *si)
3098173275Ssam{
3099173275Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
3100173275Ssam
3101173275Ssam	int i, rxseq;
3102173275Ssam
3103173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3104173275Ssam		return si->isi_rxseqs[0];
3105173275Ssam	/* XXX not right but usually what folks want */
3106173275Ssam	rxseq = 0;
3107173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3108173275Ssam		if (si->isi_rxseqs[i] > rxseq)
3109173275Ssam			rxseq = si->isi_rxseqs[i];
3110173275Ssam	return rxseq;
3111173275Ssam#undef IEEE80211_NODE_QOS
3112173275Ssam}
3113173275Ssam
3114138593Ssamstatic void
3115138593Ssamlist_stations(int s)
3116138593Ssam{
3117161147Ssam	union {
3118161147Ssam		struct ieee80211req_sta_req req;
3119161147Ssam		uint8_t buf[24*1024];
3120161147Ssam	} u;
3121161147Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
3122170531Ssam	const uint8_t *cp;
3123138593Ssam	int len;
3124138593Ssam
3125161147Ssam	/* broadcast address =>'s get all stations */
3126161147Ssam	(void) memset(u.req.is_u.macaddr, 0xff, IEEE80211_ADDR_LEN);
3127161147Ssam	if (opmode == IEEE80211_M_STA) {
3128161147Ssam		/*
3129161147Ssam		 * Get information about the associated AP.
3130161147Ssam		 */
3131173275Ssam		(void) get80211(s, IEEE80211_IOC_BSSID,
3132173275Ssam		    u.req.is_u.macaddr, IEEE80211_ADDR_LEN);
3133161147Ssam	}
3134173275Ssam	if (get80211len(s, IEEE80211_IOC_STA_INFO, &u, sizeof(u), &len) < 0)
3135138593Ssam		errx(1, "unable to get station information");
3136138593Ssam	if (len < sizeof(struct ieee80211req_sta_info))
3137138593Ssam		return;
3138138593Ssam
3139170531Ssam	getchaninfo(s);
3140170531Ssam
3141159885Ssam	printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %4s\n"
3142138593Ssam		, "ADDR"
3143138593Ssam		, "AID"
3144138593Ssam		, "CHAN"
3145138593Ssam		, "RATE"
3146138593Ssam		, "RSSI"
3147138593Ssam		, "IDLE"
3148138593Ssam		, "TXSEQ"
3149138593Ssam		, "RXSEQ"
3150138593Ssam		, "CAPS"
3151159885Ssam		, "FLAG"
3152138593Ssam	);
3153170531Ssam	cp = (const uint8_t *) u.req.info;
3154138593Ssam	do {
3155170531Ssam		const struct ieee80211req_sta_info *si;
3156138593Ssam
3157170531Ssam		si = (const struct ieee80211req_sta_info *) cp;
3158161147Ssam		if (si->isi_len < sizeof(*si))
3159161147Ssam			break;
3160170531Ssam		printf("%s %4u %4d %3dM %3.1f %4d %6d %6d %-4.4s %-4.4s"
3161138593Ssam			, ether_ntoa((const struct ether_addr*) si->isi_macaddr)
3162138593Ssam			, IEEE80211_AID(si->isi_associd)
3163166015Ssam			, ieee80211_mhz2ieee(si->isi_freq, si->isi_flags)
3164178354Ssam			, si->isi_txmbps/2
3165170531Ssam			, si->isi_rssi/2.
3166138593Ssam			, si->isi_inact
3167173275Ssam			, gettxseq(si)
3168173275Ssam			, getrxseq(si)
3169138593Ssam			, getcaps(si->isi_capinfo)
3170159885Ssam			, getflags(si->isi_state)
3171138593Ssam		);
3172170531Ssam		printies(cp + si->isi_ie_off, si->isi_ie_len, 24);
3173178354Ssam		printmimo(&si->isi_mimo);
3174138593Ssam		printf("\n");
3175138593Ssam		cp += si->isi_len, len -= si->isi_len;
3176138593Ssam	} while (len >= sizeof(struct ieee80211req_sta_info));
3177138593Ssam}
3178138593Ssam
3179170531Ssamstatic const char *
3180170531Ssamget_chaninfo(const struct ieee80211_channel *c, int precise,
3181170531Ssam	char buf[], size_t bsize)
3182138593Ssam{
3183138593Ssam	buf[0] = '\0';
3184138593Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
3185170531Ssam		strlcat(buf, " FHSS", bsize);
3186187843Ssam	if (IEEE80211_IS_CHAN_A(c))
3187187843Ssam		strlcat(buf, " 11a", bsize);
3188187843Ssam	else if (IEEE80211_IS_CHAN_ANYG(c))
3189187843Ssam		strlcat(buf, " 11g", bsize);
3190187843Ssam	else if (IEEE80211_IS_CHAN_B(c))
3191170531Ssam		strlcat(buf, " 11b", bsize);
3192187843Ssam	if (IEEE80211_IS_CHAN_HALF(c))
3193187843Ssam		strlcat(buf, "/10Mhz", bsize);
3194187843Ssam	if (IEEE80211_IS_CHAN_QUARTER(c))
3195187843Ssam		strlcat(buf, "/5Mhz", bsize);
3196170531Ssam	if (IEEE80211_IS_CHAN_TURBO(c))
3197170531Ssam		strlcat(buf, " Turbo", bsize);
3198170531Ssam	if (precise) {
3199170531Ssam		if (IEEE80211_IS_CHAN_HT20(c))
3200170531Ssam			strlcat(buf, " ht/20", bsize);
3201170531Ssam		else if (IEEE80211_IS_CHAN_HT40D(c))
3202170531Ssam			strlcat(buf, " ht/40-", bsize);
3203170531Ssam		else if (IEEE80211_IS_CHAN_HT40U(c))
3204170531Ssam			strlcat(buf, " ht/40+", bsize);
3205170531Ssam	} else {
3206170531Ssam		if (IEEE80211_IS_CHAN_HT(c))
3207170531Ssam			strlcat(buf, " ht", bsize);
3208170531Ssam	}
3209170531Ssam	return buf;
3210170531Ssam}
3211170531Ssam
3212170531Ssamstatic void
3213173275Ssamprint_chaninfo(const struct ieee80211_channel *c, int verb)
3214170531Ssam{
3215170531Ssam	char buf[14];
3216170531Ssam
3217138593Ssam	printf("Channel %3u : %u%c Mhz%-14.14s",
3218165570Ssam		ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
3219170531Ssam		IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
3220173275Ssam		get_chaninfo(c, verb, buf, sizeof(buf)));
3221138593Ssam}
3222138593Ssam
3223187844Ssamstatic int
3224187844Ssamchanpref(const struct ieee80211_channel *c)
3225187844Ssam{
3226187844Ssam	if (IEEE80211_IS_CHAN_HT40(c))
3227187844Ssam		return 40;
3228187844Ssam	if (IEEE80211_IS_CHAN_HT20(c))
3229187844Ssam		return 30;
3230187844Ssam	if (IEEE80211_IS_CHAN_HALF(c))
3231187844Ssam		return 10;
3232187844Ssam	if (IEEE80211_IS_CHAN_QUARTER(c))
3233187844Ssam		return 5;
3234187844Ssam	if (IEEE80211_IS_CHAN_TURBO(c))
3235187844Ssam		return 25;
3236187844Ssam	if (IEEE80211_IS_CHAN_A(c))
3237187844Ssam		return 20;
3238187844Ssam	if (IEEE80211_IS_CHAN_G(c))
3239187844Ssam		return 20;
3240187844Ssam	if (IEEE80211_IS_CHAN_B(c))
3241187844Ssam		return 15;
3242187844Ssam	if (IEEE80211_IS_CHAN_PUREG(c))
3243187844Ssam		return 15;
3244187844Ssam	return 0;
3245187844Ssam}
3246187844Ssam
3247138593Ssamstatic void
3248173275Ssamprint_channels(int s, const struct ieee80211req_chaninfo *chans,
3249173275Ssam	int allchans, int verb)
3250138593Ssam{
3251187801Ssam	struct ieee80211req_chaninfo *achans;
3252170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3253138593Ssam	const struct ieee80211_channel *c;
3254170531Ssam	int i, half;
3255138593Ssam
3256187801Ssam	achans = malloc(IEEE80211_CHANINFO_SPACE(chans));
3257187801Ssam	if (achans == NULL)
3258187801Ssam		errx(1, "no space for active channel list");
3259187801Ssam	achans->ic_nchans = 0;
3260170531Ssam	memset(reported, 0, sizeof(reported));
3261138593Ssam	if (!allchans) {
3262138593Ssam		struct ieee80211req_chanlist active;
3263138593Ssam
3264173275Ssam		if (get80211(s, IEEE80211_IOC_CHANLIST, &active, sizeof(active)) < 0)
3265138593Ssam			errx(1, "unable to get active channel list");
3266173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3267173275Ssam			c = &chans->ic_chans[i];
3268170531Ssam			if (!isset(active.ic_channels, c->ic_ieee))
3269170531Ssam				continue;
3270170531Ssam			/*
3271170531Ssam			 * Suppress compatible duplicates unless
3272170531Ssam			 * verbose.  The kernel gives us it's
3273170531Ssam			 * complete channel list which has separate
3274170531Ssam			 * entries for 11g/11b and 11a/turbo.
3275170531Ssam			 */
3276173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3277170531Ssam				/* XXX we assume duplicates are adjacent */
3278187801Ssam				achans->ic_chans[achans->ic_nchans-1] = *c;
3279170531Ssam			} else {
3280187801Ssam				achans->ic_chans[achans->ic_nchans++] = *c;
3281170531Ssam				setbit(reported, c->ic_ieee);
3282170531Ssam			}
3283138593Ssam		}
3284170531Ssam	} else {
3285173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3286173275Ssam			c = &chans->ic_chans[i];
3287170531Ssam			/* suppress duplicates as above */
3288173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3289170531Ssam				/* XXX we assume duplicates are adjacent */
3290187844Ssam				struct ieee80211_channel *a =
3291187844Ssam				    &achans->ic_chans[achans->ic_nchans-1];
3292187844Ssam				if (chanpref(c) > chanpref(a))
3293187844Ssam					*a = *c;
3294170531Ssam			} else {
3295187801Ssam				achans->ic_chans[achans->ic_nchans++] = *c;
3296170531Ssam				setbit(reported, c->ic_ieee);
3297170531Ssam			}
3298170531Ssam		}
3299170531Ssam	}
3300187801Ssam	half = achans->ic_nchans / 2;
3301187801Ssam	if (achans->ic_nchans % 2)
3302138593Ssam		half++;
3303170531Ssam
3304187801Ssam	for (i = 0; i < achans->ic_nchans / 2; i++) {
3305187801Ssam		print_chaninfo(&achans->ic_chans[i], verb);
3306187801Ssam		print_chaninfo(&achans->ic_chans[half+i], verb);
3307138593Ssam		printf("\n");
3308138593Ssam	}
3309187801Ssam	if (achans->ic_nchans % 2) {
3310187801Ssam		print_chaninfo(&achans->ic_chans[i], verb);
3311138593Ssam		printf("\n");
3312138593Ssam	}
3313187801Ssam	free(achans);
3314138593Ssam}
3315138593Ssam
3316138593Ssamstatic void
3317173275Ssamlist_channels(int s, int allchans)
3318173275Ssam{
3319173275Ssam	getchaninfo(s);
3320187801Ssam	print_channels(s, chaninfo, allchans, verbose);
3321173275Ssam}
3322173275Ssam
3323173275Ssamstatic void
3324170531Ssamprint_txpow(const struct ieee80211_channel *c)
3325170531Ssam{
3326170531Ssam	printf("Channel %3u : %u Mhz %3.1f reg %2d  ",
3327170531Ssam	    c->ic_ieee, c->ic_freq,
3328170531Ssam	    c->ic_maxpower/2., c->ic_maxregpower);
3329170531Ssam}
3330170531Ssam
3331170531Ssamstatic void
3332170531Ssamprint_txpow_verbose(const struct ieee80211_channel *c)
3333170531Ssam{
3334173275Ssam	print_chaninfo(c, 1);
3335170531Ssam	printf("min %4.1f dBm  max %3.1f dBm  reg %2d dBm",
3336170531Ssam	    c->ic_minpower/2., c->ic_maxpower/2., c->ic_maxregpower);
3337170531Ssam	/* indicate where regulatory cap limits power use */
3338170531Ssam	if (c->ic_maxpower > 2*c->ic_maxregpower)
3339170531Ssam		printf(" <");
3340170531Ssam}
3341170531Ssam
3342170531Ssamstatic void
3343170531Ssamlist_txpow(int s)
3344170531Ssam{
3345187801Ssam	struct ieee80211req_chaninfo *achans;
3346170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3347170531Ssam	struct ieee80211_channel *c, *prev;
3348170531Ssam	int i, half;
3349170531Ssam
3350170531Ssam	getchaninfo(s);
3351187801Ssam	achans = malloc(IEEE80211_CHANINFO_SPACE(chaninfo));
3352187801Ssam	if (achans == NULL)
3353187801Ssam		errx(1, "no space for active channel list");
3354187801Ssam	achans->ic_nchans = 0;
3355170531Ssam	memset(reported, 0, sizeof(reported));
3356187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
3357187801Ssam		c = &chaninfo->ic_chans[i];
3358170531Ssam		/* suppress duplicates as above */
3359170531Ssam		if (isset(reported, c->ic_ieee) && !verbose) {
3360170531Ssam			/* XXX we assume duplicates are adjacent */
3361187801Ssam			prev = &achans->ic_chans[achans->ic_nchans-1];
3362170531Ssam			/* display highest power on channel */
3363170531Ssam			if (c->ic_maxpower > prev->ic_maxpower)
3364170531Ssam				*prev = *c;
3365170531Ssam		} else {
3366187801Ssam			achans->ic_chans[achans->ic_nchans++] = *c;
3367170531Ssam			setbit(reported, c->ic_ieee);
3368170531Ssam		}
3369170531Ssam	}
3370170531Ssam	if (!verbose) {
3371187801Ssam		half = achans->ic_nchans / 2;
3372187801Ssam		if (achans->ic_nchans % 2)
3373170531Ssam			half++;
3374170531Ssam
3375187801Ssam		for (i = 0; i < achans->ic_nchans / 2; i++) {
3376187801Ssam			print_txpow(&achans->ic_chans[i]);
3377187801Ssam			print_txpow(&achans->ic_chans[half+i]);
3378170531Ssam			printf("\n");
3379170531Ssam		}
3380187801Ssam		if (achans->ic_nchans % 2) {
3381187801Ssam			print_txpow(&achans->ic_chans[i]);
3382170531Ssam			printf("\n");
3383170531Ssam		}
3384170531Ssam	} else {
3385187801Ssam		for (i = 0; i < achans->ic_nchans; i++) {
3386187801Ssam			print_txpow_verbose(&achans->ic_chans[i]);
3387170531Ssam			printf("\n");
3388170531Ssam		}
3389170531Ssam	}
3390187801Ssam	free(achans);
3391170531Ssam}
3392170531Ssam
3393170531Ssamstatic void
3394138593Ssamlist_keys(int s)
3395138593Ssam{
3396138593Ssam}
3397138593Ssam
3398138593Ssam#define	IEEE80211_C_BITS \
3399181102Ssam	"\20\1STA\7FF\10TURBOP\11IBSS\12PMGT" \
3400178354Ssam	"\13HOSTAP\14AHDEMO\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE" \
3401181102Ssam	"\21MONITOR\22DFS\30WPA1\31WPA2\32BURST\33WME\34WDS\36BGSCAN" \
3402186904Ssam	"\37TXFRAG\40TDMA"
3403138593Ssam
3404138593Ssamstatic void
3405138593Ssamlist_capabilities(int s)
3406138593Ssam{
3407187801Ssam	struct ieee80211_devcaps_req *dc;
3408138593Ssam
3409187845Ssam	if (verbose)
3410187845Ssam		dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
3411187845Ssam	else
3412187845Ssam		dc = malloc(IEEE80211_DEVCAPS_SIZE(1));
3413187801Ssam	if (dc == NULL)
3414187801Ssam		errx(1, "no space for device capabilities");
3415187845Ssam	dc->dc_chaninfo.ic_nchans = verbose ? MAXCHAN : 1;
3416187801Ssam	getdevcaps(s, dc);
3417187801Ssam	printb("drivercaps", dc->dc_drivercaps, IEEE80211_C_BITS);
3418187801Ssam	if (dc->dc_cryptocaps != 0 || verbose) {
3419178354Ssam		putchar('\n');
3420187801Ssam		printb("cryptocaps", dc->dc_cryptocaps, IEEE80211_CRYPTO_BITS);
3421178354Ssam	}
3422187801Ssam	if (dc->dc_htcaps != 0 || verbose) {
3423178354Ssam		putchar('\n');
3424187801Ssam		printb("htcaps", dc->dc_htcaps, IEEE80211_HTCAP_BITS);
3425178354Ssam	}
3426138593Ssam	putchar('\n');
3427187845Ssam	if (verbose) {
3428187845Ssam		chaninfo = &dc->dc_chaninfo;	/* XXX */
3429187845Ssam		print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, verbose);
3430187845Ssam	}
3431187801Ssam	free(dc);
3432138593Ssam}
3433138593Ssam
3434173275Ssamstatic int
3435173275Ssamget80211wme(int s, int param, int ac, int *val)
3436173275Ssam{
3437173275Ssam	struct ieee80211req ireq;
3438173275Ssam
3439173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3440173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3441173275Ssam	ireq.i_type = param;
3442173275Ssam	ireq.i_len = ac;
3443173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3444173275Ssam		warn("cannot get WME parameter %d, ac %d%s",
3445173275Ssam		    param, ac & IEEE80211_WMEPARAM_VAL,
3446173275Ssam		    ac & IEEE80211_WMEPARAM_BSS ? " (BSS)" : "");
3447173275Ssam		return -1;
3448173275Ssam	}
3449173275Ssam	*val = ireq.i_val;
3450173275Ssam	return 0;
3451173275Ssam}
3452173275Ssam
3453138593Ssamstatic void
3454181199Ssamlist_wme_aci(int s, const char *tag, int ac)
3455138593Ssam{
3456181199Ssam	int val;
3457138593Ssam
3458181199Ssam	printf("\t%s", tag);
3459138593Ssam
3460181199Ssam	/* show WME BSS parameters */
3461181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMIN, ac, &val) != -1)
3462181199Ssam		printf(" cwmin %2u", val);
3463181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMAX, ac, &val) != -1)
3464181199Ssam		printf(" cwmax %2u", val);
3465181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_AIFS, ac, &val) != -1)
3466181199Ssam		printf(" aifs %2u", val);
3467181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_TXOPLIMIT, ac, &val) != -1)
3468181199Ssam		printf(" txopLimit %3u", val);
3469181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_ACM, ac, &val) != -1) {
3470181199Ssam		if (val)
3471181199Ssam			printf(" acm");
3472181199Ssam		else if (verbose)
3473181199Ssam			printf(" -acm");
3474181199Ssam	}
3475181199Ssam	/* !BSS only */
3476181199Ssam	if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3477181199Ssam		if (get80211wme(s, IEEE80211_IOC_WME_ACKPOLICY, ac, &val) != -1) {
3478181199Ssam			if (!val)
3479181199Ssam				printf(" -ack");
3480138593Ssam			else if (verbose)
3481181199Ssam				printf(" ack");
3482138593Ssam		}
3483181199Ssam	}
3484181199Ssam	printf("\n");
3485181199Ssam}
3486181199Ssam
3487181199Ssamstatic void
3488181199Ssamlist_wme(int s)
3489181199Ssam{
3490181199Ssam	static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
3491181199Ssam	int ac;
3492181199Ssam
3493181199Ssam	if (verbose) {
3494181199Ssam		/* display both BSS and local settings */
3495181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
3496181199Ssam	again:
3497181199Ssam			if (ac & IEEE80211_WMEPARAM_BSS)
3498181199Ssam				list_wme_aci(s, "     ", ac);
3499181199Ssam			else
3500181199Ssam				list_wme_aci(s, acnames[ac], ac);
3501181199Ssam			if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3502181199Ssam				ac |= IEEE80211_WMEPARAM_BSS;
3503181199Ssam				goto again;
3504181199Ssam			} else
3505181199Ssam				ac &= ~IEEE80211_WMEPARAM_BSS;
3506138593Ssam		}
3507181199Ssam	} else {
3508181199Ssam		/* display only channel settings */
3509181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++)
3510181199Ssam			list_wme_aci(s, acnames[ac], ac);
3511138593Ssam	}
3512138593Ssam}
3513138593Ssam
3514149029Ssamstatic void
3515178354Ssamlist_roam(int s)
3516178354Ssam{
3517178354Ssam	const struct ieee80211_roamparam *rp;
3518178354Ssam	int mode;
3519178354Ssam
3520178354Ssam	getroam(s);
3521178354Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_11NA; mode++) {
3522178354Ssam		rp = &roamparams.params[mode];
3523178354Ssam		if (rp->rssi == 0 && rp->rate == 0)
3524178354Ssam			continue;
3525178354Ssam		if (rp->rssi & 1)
3526178354Ssam			LINE_CHECK("roam:%-6.6s rssi %2u.5dBm rate %2u Mb/s",
3527178354Ssam			    modename[mode], rp->rssi/2, rp->rate/2);
3528178354Ssam		else
3529178354Ssam			LINE_CHECK("roam:%-6.6s rssi %4udBm rate %2u Mb/s",
3530178354Ssam			    modename[mode], rp->rssi/2, rp->rate/2);
3531178354Ssam	}
3532178354Ssam	for (; mode < IEEE80211_MODE_MAX; mode++) {
3533178354Ssam		rp = &roamparams.params[mode];
3534178354Ssam		if (rp->rssi == 0 && rp->rate == 0)
3535178354Ssam			continue;
3536178354Ssam		if (rp->rssi & 1)
3537178354Ssam			LINE_CHECK("roam:%-6.6s rssi %2u.5dBm  MCS %2u    ",
3538178354Ssam			    modename[mode], rp->rssi/2, rp->rate &~ 0x80);
3539178354Ssam		else
3540178354Ssam			LINE_CHECK("roam:%-6.6s rssi %4udBm  MCS %2u    ",
3541178354Ssam			    modename[mode], rp->rssi/2, rp->rate &~ 0x80);
3542178354Ssam	}
3543178354Ssam}
3544178354Ssam
3545178354Ssamstatic void
3546178354Ssamlist_txparams(int s)
3547178354Ssam{
3548178354Ssam	const struct ieee80211_txparam *tp;
3549178354Ssam	int mode;
3550178354Ssam
3551178354Ssam	gettxparams(s);
3552178354Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_11NA; mode++) {
3553178354Ssam		tp = &txparams.params[mode];
3554178354Ssam		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
3555178354Ssam			continue;
3556178354Ssam		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3557178354Ssam			LINE_CHECK("%-6.6s ucast NONE    mgmt %2u Mb/s "
3558178354Ssam			    "mcast %2u Mb/s maxretry %u",
3559178354Ssam			    modename[mode], tp->mgmtrate/2,
3560178354Ssam			    tp->mcastrate/2, tp->maxretry);
3561178354Ssam		else
3562178354Ssam			LINE_CHECK("%-6.6s ucast %2u Mb/s mgmt %2u Mb/s "
3563178354Ssam			    "mcast %2u Mb/s maxretry %u",
3564178354Ssam			    modename[mode], tp->ucastrate/2, tp->mgmtrate/2,
3565178354Ssam			    tp->mcastrate/2, tp->maxretry);
3566178354Ssam	}
3567178354Ssam	for (; mode < IEEE80211_MODE_MAX; mode++) {
3568178354Ssam		tp = &txparams.params[mode];
3569178354Ssam		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
3570178354Ssam			continue;
3571178354Ssam		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3572178354Ssam			LINE_CHECK("%-6.6s ucast NONE    mgmt %2u MCS  "
3573178354Ssam			    "mcast %2u MCS  maxretry %u",
3574178354Ssam			    modename[mode], tp->mgmtrate &~ 0x80,
3575178354Ssam			    tp->mcastrate &~ 0x80, tp->maxretry);
3576178354Ssam		else
3577178354Ssam			LINE_CHECK("%-6.6s ucast %2u MCS  mgmt %2u MCS  "
3578178354Ssam			    "mcast %2u MCS  maxretry %u",
3579178354Ssam			    modename[mode], tp->ucastrate &~ 0x80,
3580178354Ssam			    tp->mgmtrate &~ 0x80,
3581178354Ssam			    tp->mcastrate &~ 0x80, tp->maxretry);
3582178354Ssam	}
3583178354Ssam}
3584178354Ssam
3585178354Ssamstatic void
3586173275Ssamprintpolicy(int policy)
3587173275Ssam{
3588173275Ssam	switch (policy) {
3589173275Ssam	case IEEE80211_MACCMD_POLICY_OPEN:
3590173275Ssam		printf("policy: open\n");
3591173275Ssam		break;
3592173275Ssam	case IEEE80211_MACCMD_POLICY_ALLOW:
3593173275Ssam		printf("policy: allow\n");
3594173275Ssam		break;
3595173275Ssam	case IEEE80211_MACCMD_POLICY_DENY:
3596173275Ssam		printf("policy: deny\n");
3597173275Ssam		break;
3598178354Ssam	case IEEE80211_MACCMD_POLICY_RADIUS:
3599178354Ssam		printf("policy: radius\n");
3600178354Ssam		break;
3601173275Ssam	default:
3602173275Ssam		printf("policy: unknown (%u)\n", policy);
3603173275Ssam		break;
3604173275Ssam	}
3605173275Ssam}
3606173275Ssam
3607173275Ssamstatic void
3608149029Ssamlist_mac(int s)
3609149029Ssam{
3610149029Ssam	struct ieee80211req ireq;
3611149029Ssam	struct ieee80211req_maclist *acllist;
3612173275Ssam	int i, nacls, policy, len;
3613173275Ssam	uint8_t *data;
3614149029Ssam	char c;
3615149029Ssam
3616149029Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3617149029Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
3618149029Ssam	ireq.i_type = IEEE80211_IOC_MACCMD;
3619149029Ssam	ireq.i_val = IEEE80211_MACCMD_POLICY;
3620149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3621149029Ssam		if (errno == EINVAL) {
3622149029Ssam			printf("No acl policy loaded\n");
3623149029Ssam			return;
3624149029Ssam		}
3625149029Ssam		err(1, "unable to get mac policy");
3626149029Ssam	}
3627149029Ssam	policy = ireq.i_val;
3628149029Ssam	if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
3629149029Ssam		c = '*';
3630149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
3631149029Ssam		c = '+';
3632149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
3633149029Ssam		c = '-';
3634178354Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_RADIUS) {
3635178354Ssam		c = 'r';		/* NB: should never have entries */
3636149029Ssam	} else {
3637149029Ssam		printf("policy: unknown (%u)\n", policy);
3638149029Ssam		c = '?';
3639149029Ssam	}
3640173275Ssam	if (verbose || c == '?')
3641173275Ssam		printpolicy(policy);
3642173275Ssam
3643175952Ssam	ireq.i_val = IEEE80211_MACCMD_LIST;
3644175952Ssam	ireq.i_len = 0;
3645175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3646173275Ssam		err(1, "unable to get mac acl list size");
3647175952Ssam	if (ireq.i_len == 0) {		/* NB: no acls */
3648173275Ssam		if (!(verbose || c == '?'))
3649173275Ssam			printpolicy(policy);
3650173275Ssam		return;
3651173275Ssam	}
3652175952Ssam	len = ireq.i_len;
3653173275Ssam
3654173275Ssam	data = malloc(len);
3655173275Ssam	if (data == NULL)
3656173275Ssam		err(1, "out of memory for acl list");
3657173275Ssam
3658175952Ssam	ireq.i_data = data;
3659175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3660173275Ssam		err(1, "unable to get mac acl list");
3661173275Ssam	nacls = len / sizeof(*acllist);
3662173275Ssam	acllist = (struct ieee80211req_maclist *) data;
3663149029Ssam	for (i = 0; i < nacls; i++)
3664149029Ssam		printf("%c%s\n", c, ether_ntoa(
3665149029Ssam			(const struct ether_addr *) acllist[i].ml_macaddr));
3666173275Ssam	free(data);
3667149029Ssam}
3668149029Ssam
3669178354Ssamstatic void
3670178354Ssamprint_regdomain(const struct ieee80211_regdomain *reg, int verb)
3671178354Ssam{
3672178354Ssam	if ((reg->regdomain != 0 &&
3673178354Ssam	    reg->regdomain != reg->country) || verb) {
3674178354Ssam		const struct regdomain *rd =
3675178354Ssam		    lib80211_regdomain_findbysku(getregdata(), reg->regdomain);
3676178354Ssam		if (rd == NULL)
3677178354Ssam			LINE_CHECK("regdomain %d", reg->regdomain);
3678178354Ssam		else
3679178354Ssam			LINE_CHECK("regdomain %s", rd->name);
3680178354Ssam	}
3681178354Ssam	if (reg->country != 0 || verb) {
3682178354Ssam		const struct country *cc =
3683178354Ssam		    lib80211_country_findbycc(getregdata(), reg->country);
3684178354Ssam		if (cc == NULL)
3685178354Ssam			LINE_CHECK("country %d", reg->country);
3686178354Ssam		else
3687178354Ssam			LINE_CHECK("country %s", cc->isoname);
3688178354Ssam	}
3689178354Ssam	if (reg->location == 'I')
3690178354Ssam		LINE_CHECK("indoor");
3691178354Ssam	else if (reg->location == 'O')
3692178354Ssam		LINE_CHECK("outdoor");
3693178354Ssam	else if (verb)
3694178354Ssam		LINE_CHECK("anywhere");
3695178354Ssam	if (reg->ecm)
3696178354Ssam		LINE_CHECK("ecm");
3697178354Ssam	else if (verb)
3698178354Ssam		LINE_CHECK("-ecm");
3699178354Ssam}
3700178354Ssam
3701178354Ssamstatic void
3702178354Ssamlist_regdomain(int s, int channelsalso)
3703178354Ssam{
3704178354Ssam	getregdomain(s);
3705178354Ssam	if (channelsalso) {
3706178354Ssam		getchaninfo(s);
3707178354Ssam		spacer = ':';
3708178354Ssam		print_regdomain(&regdomain, 1);
3709178354Ssam		LINE_BREAK();
3710187801Ssam		print_channels(s, chaninfo, 1/*allchans*/, 1/*verbose*/);
3711178354Ssam	} else
3712178354Ssam		print_regdomain(&regdomain, verbose);
3713178354Ssam}
3714178354Ssam
3715138593Ssamstatic
3716138593SsamDECL_CMD_FUNC(set80211list, arg, d)
3717138593Ssam{
3718138593Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
3719138593Ssam
3720173275Ssam	LINE_INIT('\t');
3721173275Ssam
3722138593Ssam	if (iseq(arg, "sta"))
3723138593Ssam		list_stations(s);
3724138593Ssam	else if (iseq(arg, "scan") || iseq(arg, "ap"))
3725138593Ssam		list_scan(s);
3726138593Ssam	else if (iseq(arg, "chan") || iseq(arg, "freq"))
3727138593Ssam		list_channels(s, 1);
3728138593Ssam	else if (iseq(arg, "active"))
3729138593Ssam		list_channels(s, 0);
3730138593Ssam	else if (iseq(arg, "keys"))
3731138593Ssam		list_keys(s);
3732138593Ssam	else if (iseq(arg, "caps"))
3733138593Ssam		list_capabilities(s);
3734178354Ssam	else if (iseq(arg, "wme") || iseq(arg, "wmm"))
3735138593Ssam		list_wme(s);
3736149029Ssam	else if (iseq(arg, "mac"))
3737149029Ssam		list_mac(s);
3738170531Ssam	else if (iseq(arg, "txpow"))
3739170531Ssam		list_txpow(s);
3740178354Ssam	else if (iseq(arg, "roam"))
3741178354Ssam		list_roam(s);
3742178354Ssam	else if (iseq(arg, "txparam") || iseq(arg, "txparm"))
3743178354Ssam		list_txparams(s);
3744178354Ssam	else if (iseq(arg, "regdomain"))
3745178354Ssam		list_regdomain(s, 1);
3746178354Ssam	else if (iseq(arg, "countries"))
3747178354Ssam		list_countries();
3748138593Ssam	else
3749138593Ssam		errx(1, "Don't know how to list %s for %s", arg, name);
3750178354Ssam	LINE_BREAK();
3751138593Ssam#undef iseq
3752138593Ssam}
3753138593Ssam
3754138593Ssamstatic enum ieee80211_opmode
3755138593Ssamget80211opmode(int s)
3756138593Ssam{
3757138593Ssam	struct ifmediareq ifmr;
3758138593Ssam
3759138593Ssam	(void) memset(&ifmr, 0, sizeof(ifmr));
3760138593Ssam	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
3761138593Ssam
3762138593Ssam	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
3763186101Ssam		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) {
3764186101Ssam			if (ifmr.ifm_current & IFM_FLAG0)
3765186101Ssam				return IEEE80211_M_AHDEMO;
3766186101Ssam			else
3767186101Ssam				return IEEE80211_M_IBSS;
3768186101Ssam		}
3769138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
3770138593Ssam			return IEEE80211_M_HOSTAP;
3771138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
3772138593Ssam			return IEEE80211_M_MONITOR;
3773138593Ssam	}
3774138593Ssam	return IEEE80211_M_STA;
3775138593Ssam}
3776138593Ssam
3777138593Ssam#if 0
3778138593Ssamstatic void
3779138593Ssamprintcipher(int s, struct ieee80211req *ireq, int keylenop)
3780138593Ssam{
3781138593Ssam	switch (ireq->i_val) {
3782138593Ssam	case IEEE80211_CIPHER_WEP:
3783138593Ssam		ireq->i_type = keylenop;
3784138593Ssam		if (ioctl(s, SIOCG80211, ireq) != -1)
3785138593Ssam			printf("WEP-%s",
3786138593Ssam			    ireq->i_len <= 5 ? "40" :
3787138593Ssam			    ireq->i_len <= 13 ? "104" : "128");
3788138593Ssam		else
3789138593Ssam			printf("WEP");
3790138593Ssam		break;
3791138593Ssam	case IEEE80211_CIPHER_TKIP:
3792138593Ssam		printf("TKIP");
3793138593Ssam		break;
3794138593Ssam	case IEEE80211_CIPHER_AES_OCB:
3795138593Ssam		printf("AES-OCB");
3796138593Ssam		break;
3797138593Ssam	case IEEE80211_CIPHER_AES_CCM:
3798138593Ssam		printf("AES-CCM");
3799138593Ssam		break;
3800138593Ssam	case IEEE80211_CIPHER_CKIP:
3801138593Ssam		printf("CKIP");
3802138593Ssam		break;
3803138593Ssam	case IEEE80211_CIPHER_NONE:
3804138593Ssam		printf("NONE");
3805138593Ssam		break;
3806138593Ssam	default:
3807138593Ssam		printf("UNKNOWN (0x%x)", ireq->i_val);
3808138593Ssam		break;
3809138593Ssam	}
3810138593Ssam}
3811138593Ssam#endif
3812138593Ssam
3813155931Ssamstatic void
3814138593Ssamprintkey(const struct ieee80211req_key *ik)
3815138593Ssam{
3816138593Ssam	static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
3817138593Ssam	int keylen = ik->ik_keylen;
3818138593Ssam	int printcontents;
3819138593Ssam
3820148001Srwatson	printcontents = printkeys &&
3821138593Ssam		(memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
3822138593Ssam	if (printcontents)
3823138593Ssam		LINE_BREAK();
3824138593Ssam	switch (ik->ik_type) {
3825138593Ssam	case IEEE80211_CIPHER_WEP:
3826138593Ssam		/* compatibility */
3827155931Ssam		LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
3828138593Ssam		    keylen <= 5 ? "40-bit" :
3829138593Ssam		    keylen <= 13 ? "104-bit" : "128-bit");
3830138593Ssam		break;
3831138593Ssam	case IEEE80211_CIPHER_TKIP:
3832138593Ssam		if (keylen > 128/8)
3833138593Ssam			keylen -= 128/8;	/* ignore MIC for now */
3834155931Ssam		LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3835138593Ssam		break;
3836138593Ssam	case IEEE80211_CIPHER_AES_OCB:
3837155931Ssam		LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3838138593Ssam		break;
3839138593Ssam	case IEEE80211_CIPHER_AES_CCM:
3840155931Ssam		LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3841138593Ssam		break;
3842138593Ssam	case IEEE80211_CIPHER_CKIP:
3843155931Ssam		LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3844138593Ssam		break;
3845138593Ssam	case IEEE80211_CIPHER_NONE:
3846155931Ssam		LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3847138593Ssam		break;
3848138593Ssam	default:
3849155931Ssam		LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
3850138593Ssam			ik->ik_type, ik->ik_keyix+1, 8*keylen);
3851138593Ssam		break;
3852138593Ssam	}
3853138593Ssam	if (printcontents) {
3854138593Ssam		int i;
3855138593Ssam
3856138593Ssam		printf(" <");
3857138593Ssam		for (i = 0; i < keylen; i++)
3858138593Ssam			printf("%02x", ik->ik_keydata[i]);
3859138593Ssam		printf(">");
3860138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
3861138593Ssam		    (ik->ik_keyrsc != 0 || verbose))
3862146873Sjhb			printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
3863138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
3864138593Ssam		    (ik->ik_keytsc != 0 || verbose))
3865146873Sjhb			printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
3866138593Ssam		if (ik->ik_flags != 0 && verbose) {
3867138593Ssam			const char *sep = " ";
3868138593Ssam
3869138593Ssam			if (ik->ik_flags & IEEE80211_KEY_XMIT)
3870138593Ssam				printf("%stx", sep), sep = "+";
3871138593Ssam			if (ik->ik_flags & IEEE80211_KEY_RECV)
3872138593Ssam				printf("%srx", sep), sep = "+";
3873138593Ssam			if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
3874138593Ssam				printf("%sdef", sep), sep = "+";
3875138593Ssam		}
3876138593Ssam		LINE_BREAK();
3877138593Ssam	}
3878138593Ssam}
3879138593Ssam
3880138593Ssamstatic void
3881173275Ssamprintrate(const char *tag, int v, int defrate, int defmcs)
3882138593Ssam{
3883173275Ssam	if (v == 11)
3884173275Ssam		LINE_CHECK("%s 5.5", tag);
3885173275Ssam	else if (v & 0x80) {
3886173275Ssam		if (v != defmcs)
3887173275Ssam			LINE_CHECK("%s %d", tag, v &~ 0x80);
3888173275Ssam	} else {
3889173275Ssam		if (v != defrate)
3890173275Ssam			LINE_CHECK("%s %d", tag, v/2);
3891173275Ssam	}
3892173275Ssam}
3893173275Ssam
3894173275Ssamstatic int
3895173275Ssamgetssid(int s, int ix, void *data, size_t len, int *plen)
3896173275Ssam{
3897138593Ssam	struct ieee80211req ireq;
3898138593Ssam
3899138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3900138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3901173275Ssam	ireq.i_type = IEEE80211_IOC_SSID;
3902173275Ssam	ireq.i_val = ix;
3903173275Ssam	ireq.i_data = data;
3904173275Ssam	ireq.i_len = len;
3905173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3906173275Ssam		return -1;
3907173275Ssam	*plen = ireq.i_len;
3908173275Ssam	return 0;
3909173275Ssam}
391077218Sphk
3911173275Ssamstatic void
3912173275Ssamieee80211_status(int s)
3913173275Ssam{
3914173275Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
3915173275Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
3916173275Ssam	int i, num, wpa, wme, bgscan, bgscaninterval, val, len, wepmode;
3917173275Ssam	uint8_t data[32];
3918173275Ssam	const struct ieee80211_channel *c;
3919178354Ssam	const struct ieee80211_roamparam *rp;
3920178354Ssam	const struct ieee80211_txparam *tp;
3921173275Ssam
3922173275Ssam	if (getssid(s, -1, data, sizeof(data), &len) < 0) {
3923148686Sstefanf		/* If we can't get the SSID, this isn't an 802.11 device. */
392477218Sphk		return;
392577218Sphk	}
3926173275Ssam
3927173275Ssam	/*
3928173275Ssam	 * Invalidate cached state so printing status for multiple
3929173275Ssam	 * if's doesn't reuse the first interfaces' cached state.
3930173275Ssam	 */
3931173275Ssam	gotcurchan = 0;
3932178354Ssam	gotroam = 0;
3933178354Ssam	gottxparams = 0;
3934173275Ssam	gothtconf = 0;
3935178354Ssam	gotregdomain = 0;
3936173275Ssam
3937173275Ssam	if (get80211val(s, IEEE80211_IOC_NUMSSIDS, &num) < 0)
3938173275Ssam		num = 0;
3939138593Ssam	printf("\tssid ");
3940138593Ssam	if (num > 1) {
3941173275Ssam		for (i = 0; i < num; i++) {
3942173275Ssam			if (getssid(s, i, data, sizeof(data), &len) >= 0 && len > 0) {
3943173275Ssam				printf(" %d:", i + 1);
3944173275Ssam				print_string(data, len);
3945138593Ssam			}
394688748Sambrisko		}
3947138593Ssam	} else
3948173275Ssam		print_string(data, len);
394977218Sphk
3950173275Ssam	c = getcurchan(s);
3951170531Ssam	if (c->ic_freq != IEEE80211_CHAN_ANY) {
3952170531Ssam		char buf[14];
3953170531Ssam		printf(" channel %d (%u Mhz%s)", c->ic_ieee, c->ic_freq,
3954170531Ssam			get_chaninfo(c, 1, buf, sizeof(buf)));
3955138593Ssam	} else if (verbose)
3956138593Ssam		printf(" channel UNDEF");
3957138593Ssam
3958173275Ssam	if (get80211(s, IEEE80211_IOC_BSSID, data, IEEE80211_ADDR_LEN) >= 0 &&
3959173275Ssam	    (memcmp(data, zerobssid, sizeof(zerobssid)) != 0 || verbose))
3960173275Ssam		printf(" bssid %s", ether_ntoa((struct ether_addr *)data));
3961138593Ssam
3962173275Ssam	if (get80211len(s, IEEE80211_IOC_STATIONNAME, data, sizeof(data), &len) != -1) {
3963138593Ssam		printf("\n\tstationname ");
3964173275Ssam		print_string(data, len);
396577218Sphk	}
396677218Sphk
3967138593Ssam	spacer = ' ';		/* force first break */
3968138593Ssam	LINE_BREAK();
396977218Sphk
3970178354Ssam	list_regdomain(s, 0);
3971178354Ssam
3972173275Ssam	wpa = 0;
3973173275Ssam	if (get80211val(s, IEEE80211_IOC_AUTHMODE, &val) != -1) {
3974173275Ssam		switch (val) {
3975173275Ssam		case IEEE80211_AUTH_NONE:
3976173275Ssam			LINE_CHECK("authmode NONE");
3977173275Ssam			break;
3978173275Ssam		case IEEE80211_AUTH_OPEN:
3979173275Ssam			LINE_CHECK("authmode OPEN");
3980173275Ssam			break;
3981173275Ssam		case IEEE80211_AUTH_SHARED:
3982173275Ssam			LINE_CHECK("authmode SHARED");
3983173275Ssam			break;
3984173275Ssam		case IEEE80211_AUTH_8021X:
3985173275Ssam			LINE_CHECK("authmode 802.1x");
3986173275Ssam			break;
3987173275Ssam		case IEEE80211_AUTH_WPA:
3988173275Ssam			if (get80211val(s, IEEE80211_IOC_WPA, &wpa) < 0)
3989173275Ssam				wpa = 1;	/* default to WPA1 */
3990173275Ssam			switch (wpa) {
3991173275Ssam			case 2:
3992173275Ssam				LINE_CHECK("authmode WPA2/802.11i");
399377218Sphk				break;
3994173275Ssam			case 3:
3995173275Ssam				LINE_CHECK("authmode WPA1+WPA2/802.11i");
399677218Sphk				break;
3997127649Ssam			default:
3998173275Ssam				LINE_CHECK("authmode WPA");
3999127649Ssam				break;
4000173275Ssam			}
4001173275Ssam			break;
4002173275Ssam		case IEEE80211_AUTH_AUTO:
4003173275Ssam			LINE_CHECK("authmode AUTO");
4004173275Ssam			break;
4005173275Ssam		default:
4006173275Ssam			LINE_CHECK("authmode UNKNOWN (0x%x)", val);
4007173275Ssam			break;
4008127649Ssam		}
4009127649Ssam	}
4010127649Ssam
4011173275Ssam	if (wpa || verbose) {
4012178354Ssam		if (get80211val(s, IEEE80211_IOC_WPS, &val) != -1) {
4013178354Ssam			if (val)
4014178354Ssam				LINE_CHECK("wps");
4015178354Ssam			else if (verbose)
4016178354Ssam				LINE_CHECK("-wps");
4017178354Ssam		}
4018178354Ssam		if (get80211val(s, IEEE80211_IOC_TSN, &val) != -1) {
4019178354Ssam			if (val)
4020178354Ssam				LINE_CHECK("tsn");
4021178354Ssam			else if (verbose)
4022178354Ssam				LINE_CHECK("-tsn");
4023178354Ssam		}
4024173275Ssam		if (ioctl(s, IEEE80211_IOC_COUNTERMEASURES, &val) != -1) {
4025173275Ssam			if (val)
4026173275Ssam				LINE_CHECK("countermeasures");
4027173275Ssam			else if (verbose)
4028173275Ssam				LINE_CHECK("-countermeasures");
4029173275Ssam		}
4030178354Ssam#if 0
4031178354Ssam		/* XXX not interesting with WPA done in user space */
4032178354Ssam		ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
4033178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4034178354Ssam		}
4035178354Ssam
4036178354Ssam		ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
4037178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4038178354Ssam			LINE_CHECK("mcastcipher ");
4039178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
4040178354Ssam			spacer = ' ';
4041178354Ssam		}
4042178354Ssam
4043178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
4044178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4045178354Ssam			LINE_CHECK("ucastcipher ");
4046178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
4047178354Ssam		}
4048178354Ssam
4049178354Ssam		if (wpa & 2) {
4050178354Ssam			ireq.i_type = IEEE80211_IOC_RSNCAPS;
4051178354Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1) {
4052178354Ssam				LINE_CHECK("RSN caps 0x%x", ireq.i_val);
4053178354Ssam				spacer = ' ';
4054178354Ssam			}
4055178354Ssam		}
4056178354Ssam
4057178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
4058178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4059178354Ssam		}
4060178354Ssam#endif
4061173275Ssam	}
4062138593Ssam
4063173275Ssam	if (get80211val(s, IEEE80211_IOC_WEP, &wepmode) != -1 &&
4064173275Ssam	    wepmode != IEEE80211_WEP_NOSUP) {
4065173275Ssam		int firstkey;
4066173275Ssam
4067138718Ssam		switch (wepmode) {
4068173275Ssam		case IEEE80211_WEP_OFF:
4069173275Ssam			LINE_CHECK("privacy OFF");
4070173275Ssam			break;
4071173275Ssam		case IEEE80211_WEP_ON:
4072173275Ssam			LINE_CHECK("privacy ON");
4073173275Ssam			break;
4074173275Ssam		case IEEE80211_WEP_MIXED:
4075173275Ssam			LINE_CHECK("privacy MIXED");
4076173275Ssam			break;
4077173275Ssam		default:
4078173275Ssam			LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
4079173275Ssam			break;
408077218Sphk		}
408177218Sphk
408277218Sphk		/*
408377218Sphk		 * If we get here then we've got WEP support so we need
408477218Sphk		 * to print WEP status.
408591454Sbrooks		 */
408677218Sphk
4087173275Ssam		if (get80211val(s, IEEE80211_IOC_WEPTXKEY, &val) < 0) {
408877218Sphk			warn("WEP support, but no tx key!");
408977218Sphk			goto end;
409077218Sphk		}
4091173275Ssam		if (val != -1)
4092173275Ssam			LINE_CHECK("deftxkey %d", val+1);
4093138718Ssam		else if (wepmode != IEEE80211_WEP_OFF || verbose)
4094155931Ssam			LINE_CHECK("deftxkey UNDEF");
409577218Sphk
4096173275Ssam		if (get80211val(s, IEEE80211_IOC_NUMWEPKEYS, &num) < 0) {
409777218Sphk			warn("WEP support, but no NUMWEPKEYS support!");
409877218Sphk			goto end;
409977218Sphk		}
410077218Sphk
4101138593Ssam		firstkey = 1;
4102138593Ssam		for (i = 0; i < num; i++) {
4103138593Ssam			struct ieee80211req_key ik;
410477218Sphk
4105138593Ssam			memset(&ik, 0, sizeof(ik));
4106138593Ssam			ik.ik_keyix = i;
4107173275Ssam			if (get80211(s, IEEE80211_IOC_WPAKEY, &ik, sizeof(ik)) < 0) {
410877218Sphk				warn("WEP support, but can get keys!");
410977218Sphk				goto end;
411077218Sphk			}
4111138593Ssam			if (ik.ik_keylen != 0) {
4112138593Ssam				if (verbose)
4113138593Ssam					LINE_BREAK();
4114138593Ssam				printkey(&ik);
4115138593Ssam				firstkey = 0;
4116138593Ssam			}
4117138593Ssam		}
4118173275Ssamend:
4119173275Ssam		;
4120138593Ssam	}
4121138593Ssam
4122173275Ssam	if (get80211val(s, IEEE80211_IOC_POWERSAVE, &val) != -1 &&
4123173275Ssam	    val != IEEE80211_POWERSAVE_NOSUP ) {
4124173275Ssam		if (val != IEEE80211_POWERSAVE_OFF || verbose) {
4125173275Ssam			switch (val) {
4126173275Ssam			case IEEE80211_POWERSAVE_OFF:
4127173275Ssam				LINE_CHECK("powersavemode OFF");
4128173275Ssam				break;
4129173275Ssam			case IEEE80211_POWERSAVE_CAM:
4130173275Ssam				LINE_CHECK("powersavemode CAM");
4131173275Ssam				break;
4132173275Ssam			case IEEE80211_POWERSAVE_PSP:
4133173275Ssam				LINE_CHECK("powersavemode PSP");
4134173275Ssam				break;
4135173275Ssam			case IEEE80211_POWERSAVE_PSP_CAM:
4136173275Ssam				LINE_CHECK("powersavemode PSP-CAM");
4137173275Ssam				break;
4138138593Ssam			}
4139173275Ssam			if (get80211val(s, IEEE80211_IOC_POWERSAVESLEEP, &val) != -1)
4140173275Ssam				LINE_CHECK("powersavesleep %d", val);
4141138593Ssam		}
4142138593Ssam	}
4143138593Ssam
4144173275Ssam	if (get80211val(s, IEEE80211_IOC_TXPOWER, &val) != -1) {
4145173275Ssam		if (val & 1)
4146173275Ssam			LINE_CHECK("txpower %d.5", val/2);
4147173275Ssam		else
4148173275Ssam			LINE_CHECK("txpower %d", val/2);
4149173275Ssam	}
4150138593Ssam	if (verbose) {
4151173275Ssam		if (get80211val(s, IEEE80211_IOC_TXPOWMAX, &val) != -1)
4152173275Ssam			LINE_CHECK("txpowmax %.1f", val/2.);
4153138593Ssam	}
4154138593Ssam
4155178354Ssam	if (get80211val(s, IEEE80211_IOC_DOTD, &val) != -1) {
4156178354Ssam		if (val)
4157178354Ssam			LINE_CHECK("dotd");
4158178354Ssam		else if (verbose)
4159178354Ssam			LINE_CHECK("-dotd");
4160178354Ssam	}
4161178354Ssam
4162173275Ssam	if (get80211val(s, IEEE80211_IOC_RTSTHRESHOLD, &val) != -1) {
4163173275Ssam		if (val != IEEE80211_RTS_MAX || verbose)
4164173275Ssam			LINE_CHECK("rtsthreshold %d", val);
4165138593Ssam	}
4166138593Ssam
4167173275Ssam	if (get80211val(s, IEEE80211_IOC_FRAGTHRESHOLD, &val) != -1) {
4168173275Ssam		if (val != IEEE80211_FRAG_MAX || verbose)
4169173275Ssam			LINE_CHECK("fragthreshold %d", val);
4170170531Ssam	}
4171173275Ssam	if (opmode == IEEE80211_M_STA || verbose) {
4172173275Ssam		if (get80211val(s, IEEE80211_IOC_BMISSTHRESHOLD, &val) != -1) {
4173173275Ssam			if (val != IEEE80211_HWBMISS_MAX || verbose)
4174173275Ssam				LINE_CHECK("bmiss %d", val);
4175153354Ssam		}
4176153354Ssam	}
4177153354Ssam
4178178354Ssam	if (!verbose) {
4179178354Ssam		gettxparams(s);
4180178354Ssam		tp = &txparams.params[chan2mode(c)];
4181178354Ssam		printrate("ucastrate", tp->ucastrate,
4182178354Ssam		    IEEE80211_FIXED_RATE_NONE, IEEE80211_FIXED_RATE_NONE);
4183178354Ssam		printrate("mcastrate", tp->mcastrate, 2*1, 0x80|0);
4184178354Ssam		printrate("mgmtrate", tp->mgmtrate, 2*1, 0x80|0);
4185178354Ssam		if (tp->maxretry != 6)		/* XXX */
4186178354Ssam			LINE_CHECK("maxretry %d", tp->maxretry);
4187178354Ssam	} else {
4188178354Ssam		LINE_BREAK();
4189178354Ssam		list_txparams(s);
4190178354Ssam	}
4191170531Ssam
4192173275Ssam	bgscaninterval = -1;
4193173275Ssam	(void) get80211val(s, IEEE80211_IOC_BGSCAN_INTERVAL, &bgscaninterval);
4194173275Ssam
4195173275Ssam	if (get80211val(s, IEEE80211_IOC_SCANVALID, &val) != -1) {
4196173275Ssam		if (val != bgscaninterval || verbose)
4197173275Ssam			LINE_CHECK("scanvalid %u", val);
4198148416Ssam	}
4199148416Ssam
4200173275Ssam	bgscan = 0;
4201173275Ssam	if (get80211val(s, IEEE80211_IOC_BGSCAN, &bgscan) != -1) {
4202173275Ssam		if (bgscan)
4203170531Ssam			LINE_CHECK("bgscan");
4204170531Ssam		else if (verbose)
4205170531Ssam			LINE_CHECK("-bgscan");
4206160687Ssam	}
4207170531Ssam	if (bgscan || verbose) {
4208170531Ssam		if (bgscaninterval != -1)
4209170531Ssam			LINE_CHECK("bgscanintvl %u", bgscaninterval);
4210173275Ssam		if (get80211val(s, IEEE80211_IOC_BGSCAN_IDLE, &val) != -1)
4211173275Ssam			LINE_CHECK("bgscanidle %u", val);
4212178354Ssam		if (!verbose) {
4213178354Ssam			getroam(s);
4214178354Ssam			rp = &roamparams.params[chan2mode(c)];
4215178354Ssam			if (rp->rssi & 1)
4216178354Ssam				LINE_CHECK("roam:rssi %u.5", rp->rssi/2);
4217178354Ssam			else
4218178354Ssam				LINE_CHECK("roam:rssi %u", rp->rssi/2);
4219178354Ssam			LINE_CHECK("roam:rate %u", rp->rate/2);
4220178354Ssam		} else {
4221178354Ssam			LINE_BREAK();
4222178354Ssam			list_roam(s);
4223170531Ssam		}
4224170531Ssam	}
4225160687Ssam
4226165570Ssam	if (IEEE80211_IS_CHAN_ANYG(c) || verbose) {
4227173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREG, &val) != -1) {
4228173275Ssam			if (val)
4229155931Ssam				LINE_CHECK("pureg");
4230147795Ssam			else if (verbose)
4231155931Ssam				LINE_CHECK("-pureg");
4232147795Ssam		}
4233173275Ssam		if (get80211val(s, IEEE80211_IOC_PROTMODE, &val) != -1) {
4234173275Ssam			switch (val) {
4235173275Ssam			case IEEE80211_PROTMODE_OFF:
4236173275Ssam				LINE_CHECK("protmode OFF");
4237173275Ssam				break;
4238173275Ssam			case IEEE80211_PROTMODE_CTS:
4239173275Ssam				LINE_CHECK("protmode CTS");
4240173275Ssam				break;
4241173275Ssam			case IEEE80211_PROTMODE_RTSCTS:
4242173275Ssam				LINE_CHECK("protmode RTSCTS");
4243173275Ssam				break;
4244173275Ssam			default:
4245173275Ssam				LINE_CHECK("protmode UNKNOWN (0x%x)", val);
4246173275Ssam				break;
4247138593Ssam			}
4248138593Ssam		}
4249138593Ssam	}
4250138593Ssam
4251173275Ssam	if (IEEE80211_IS_CHAN_HT(c) || verbose) {
4252173275Ssam		gethtconf(s);
4253173275Ssam		switch (htconf & 3) {
4254173275Ssam		case 0:
4255173275Ssam		case 2:
4256173275Ssam			LINE_CHECK("-ht");
4257173275Ssam			break;
4258173275Ssam		case 1:
4259173275Ssam			LINE_CHECK("ht20");
4260173275Ssam			break;
4261173275Ssam		case 3:
4262173275Ssam			if (verbose)
4263173275Ssam				LINE_CHECK("ht");
4264173275Ssam			break;
4265173275Ssam		}
4266173275Ssam		if (get80211val(s, IEEE80211_IOC_HTCOMPAT, &val) != -1) {
4267173275Ssam			if (!val)
4268173275Ssam				LINE_CHECK("-htcompat");
4269173275Ssam			else if (verbose)
4270173275Ssam				LINE_CHECK("htcompat");
4271173275Ssam		}
4272173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU, &val) != -1) {
4273173275Ssam			switch (val) {
4274173275Ssam			case 0:
4275173275Ssam				LINE_CHECK("-ampdu");
4276173275Ssam				break;
4277173275Ssam			case 1:
4278173275Ssam				LINE_CHECK("ampdutx -ampdurx");
4279173275Ssam				break;
4280173275Ssam			case 2:
4281173275Ssam				LINE_CHECK("-ampdutx ampdurx");
4282173275Ssam				break;
4283173275Ssam			case 3:
4284173275Ssam				if (verbose)
4285173275Ssam					LINE_CHECK("ampdu");
4286173275Ssam				break;
4287173275Ssam			}
4288173275Ssam		}
4289173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_LIMIT, &val) != -1) {
4290173275Ssam			switch (val) {
4291173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_8K:
4292173275Ssam				LINE_CHECK("ampdulimit 8k");
4293173275Ssam				break;
4294173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_16K:
4295173275Ssam				LINE_CHECK("ampdulimit 16k");
4296173275Ssam				break;
4297173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_32K:
4298173275Ssam				LINE_CHECK("ampdulimit 32k");
4299173275Ssam				break;
4300173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_64K:
4301173275Ssam				LINE_CHECK("ampdulimit 64k");
4302173275Ssam				break;
4303173275Ssam			}
4304173275Ssam		}
4305173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_DENSITY, &val) != -1) {
4306173275Ssam			switch (val) {
4307173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_NA:
4308173275Ssam				if (verbose)
4309183260Ssam					LINE_CHECK("ampdudensity NA");
4310173275Ssam				break;
4311173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_025:
4312173275Ssam				LINE_CHECK("ampdudensity .25");
4313173275Ssam				break;
4314173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_05:
4315173275Ssam				LINE_CHECK("ampdudensity .5");
4316173275Ssam				break;
4317173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_1:
4318173275Ssam				LINE_CHECK("ampdudensity 1");
4319173275Ssam				break;
4320173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_2:
4321173275Ssam				LINE_CHECK("ampdudensity 2");
4322173275Ssam				break;
4323173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_4:
4324173275Ssam				LINE_CHECK("ampdudensity 4");
4325173275Ssam				break;
4326173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_8:
4327173275Ssam				LINE_CHECK("ampdudensity 8");
4328173275Ssam				break;
4329173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_16:
4330173275Ssam				LINE_CHECK("ampdudensity 16");
4331173275Ssam				break;
4332173275Ssam			}
4333173275Ssam		}
4334173275Ssam		if (get80211val(s, IEEE80211_IOC_AMSDU, &val) != -1) {
4335173275Ssam			switch (val) {
4336173275Ssam			case 0:
4337173275Ssam				LINE_CHECK("-amsdu");
4338173275Ssam				break;
4339173275Ssam			case 1:
4340173275Ssam				LINE_CHECK("amsdutx -amsdurx");
4341173275Ssam				break;
4342173275Ssam			case 2:
4343173275Ssam				LINE_CHECK("-amsdutx amsdurx");
4344173275Ssam				break;
4345173275Ssam			case 3:
4346173275Ssam				if (verbose)
4347173275Ssam					LINE_CHECK("amsdu");
4348173275Ssam				break;
4349173275Ssam			}
4350173275Ssam		}
4351173275Ssam		/* XXX amsdu limit */
4352173275Ssam		if (get80211val(s, IEEE80211_IOC_SHORTGI, &val) != -1) {
4353173275Ssam			if (val)
4354173275Ssam				LINE_CHECK("shortgi");
4355173275Ssam			else if (verbose)
4356173275Ssam				LINE_CHECK("-shortgi");
4357173275Ssam		}
4358173275Ssam		if (get80211val(s, IEEE80211_IOC_HTPROTMODE, &val) != -1) {
4359173275Ssam			if (val == IEEE80211_PROTMODE_OFF)
4360173275Ssam				LINE_CHECK("htprotmode OFF");
4361173275Ssam			else if (val != IEEE80211_PROTMODE_RTSCTS)
4362173275Ssam				LINE_CHECK("htprotmode UNKNOWN (0x%x)", val);
4363173275Ssam			else if (verbose)
4364173275Ssam				LINE_CHECK("htprotmode RTSCTS");
4365173275Ssam		}
4366173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREN, &val) != -1) {
4367173275Ssam			if (val)
4368173275Ssam				LINE_CHECK("puren");
4369173275Ssam			else if (verbose)
4370173275Ssam				LINE_CHECK("-puren");
4371173275Ssam		}
4372183261Ssam		if (get80211val(s, IEEE80211_IOC_SMPS, &val) != -1) {
4373183261Ssam			if (val == IEEE80211_HTCAP_SMPS_DYNAMIC)
4374183261Ssam				LINE_CHECK("smpsdyn");
4375183261Ssam			else if (val == IEEE80211_HTCAP_SMPS_ENA)
4376183261Ssam				LINE_CHECK("smps");
4377183261Ssam			else if (verbose)
4378183261Ssam				LINE_CHECK("-smps");
4379183261Ssam		}
4380183261Ssam		if (get80211val(s, IEEE80211_IOC_RIFS, &val) != -1) {
4381183261Ssam			if (val)
4382183261Ssam				LINE_CHECK("rifs");
4383183261Ssam			else if (verbose)
4384183261Ssam				LINE_CHECK("-rifs");
4385183261Ssam		}
4386173275Ssam	}
4387173275Ssam
4388173275Ssam	if (get80211val(s, IEEE80211_IOC_WME, &wme) != -1) {
4389138593Ssam		if (wme)
4390155931Ssam			LINE_CHECK("wme");
4391138593Ssam		else if (verbose)
4392155931Ssam			LINE_CHECK("-wme");
4393138593Ssam	} else
4394138593Ssam		wme = 0;
4395138593Ssam
4396173275Ssam	if (get80211val(s, IEEE80211_IOC_BURST, &val) != -1) {
4397173275Ssam		if (val)
4398155931Ssam			LINE_CHECK("burst");
4399153422Ssam		else if (verbose)
4400155931Ssam			LINE_CHECK("-burst");
4401153422Ssam	}
4402153422Ssam
4403173275Ssam	if (get80211val(s, IEEE80211_IOC_FF, &val) != -1) {
4404173275Ssam		if (val)
4405170531Ssam			LINE_CHECK("ff");
4406170531Ssam		else if (verbose)
4407170531Ssam			LINE_CHECK("-ff");
4408170531Ssam	}
4409173275Ssam	if (get80211val(s, IEEE80211_IOC_TURBOP, &val) != -1) {
4410173275Ssam		if (val)
4411170531Ssam			LINE_CHECK("dturbo");
4412170531Ssam		else if (verbose)
4413170531Ssam			LINE_CHECK("-dturbo");
4414170531Ssam	}
4415178354Ssam	if (get80211val(s, IEEE80211_IOC_DWDS, &val) != -1) {
4416178354Ssam		if (val)
4417178354Ssam			LINE_CHECK("dwds");
4418178354Ssam		else if (verbose)
4419178354Ssam			LINE_CHECK("-dwds");
4420178354Ssam	}
4421170531Ssam
4422138593Ssam	if (opmode == IEEE80211_M_HOSTAP) {
4423173275Ssam		if (get80211val(s, IEEE80211_IOC_HIDESSID, &val) != -1) {
4424173275Ssam			if (val)
4425168075Ssam				LINE_CHECK("hidessid");
4426138593Ssam			else if (verbose)
4427168075Ssam				LINE_CHECK("-hidessid");
4428138593Ssam		}
4429173275Ssam		if (get80211val(s, IEEE80211_IOC_APBRIDGE, &val) != -1) {
4430173275Ssam			if (!val)
4431155931Ssam				LINE_CHECK("-apbridge");
4432138593Ssam			else if (verbose)
4433155931Ssam				LINE_CHECK("apbridge");
4434138593Ssam		}
4435173275Ssam		if (get80211val(s, IEEE80211_IOC_DTIM_PERIOD, &val) != -1)
4436173275Ssam			LINE_CHECK("dtimperiod %u", val);
4437138593Ssam
4438173275Ssam		if (get80211val(s, IEEE80211_IOC_DOTH, &val) != -1) {
4439173275Ssam			if (!val)
4440170531Ssam				LINE_CHECK("-doth");
4441170531Ssam			else if (verbose)
4442170531Ssam				LINE_CHECK("doth");
4443170531Ssam		}
4444178354Ssam		if (get80211val(s, IEEE80211_IOC_DFS, &val) != -1) {
4445178354Ssam			if (!val)
4446178354Ssam				LINE_CHECK("-dfs");
4447178354Ssam			else if (verbose)
4448178354Ssam				LINE_CHECK("dfs");
4449178354Ssam		}
4450173275Ssam		if (get80211val(s, IEEE80211_IOC_INACTIVITY, &val) != -1) {
4451173275Ssam			if (!val)
4452173275Ssam				LINE_CHECK("-inact");
4453173275Ssam			else if (verbose)
4454173275Ssam				LINE_CHECK("inact");
4455173275Ssam		}
4456138593Ssam	} else {
4457173275Ssam		if (get80211val(s, IEEE80211_IOC_ROAMING, &val) != -1) {
4458173275Ssam			if (val != IEEE80211_ROAMING_AUTO || verbose) {
4459173275Ssam				switch (val) {
4460138593Ssam				case IEEE80211_ROAMING_DEVICE:
4461155931Ssam					LINE_CHECK("roaming DEVICE");
4462138593Ssam					break;
4463138593Ssam				case IEEE80211_ROAMING_AUTO:
4464155931Ssam					LINE_CHECK("roaming AUTO");
4465138593Ssam					break;
4466138593Ssam				case IEEE80211_ROAMING_MANUAL:
4467155931Ssam					LINE_CHECK("roaming MANUAL");
4468138593Ssam					break;
4469138593Ssam				default:
4470155931Ssam					LINE_CHECK("roaming UNKNOWN (0x%x)",
4471173275Ssam						val);
4472138593Ssam					break;
4473138593Ssam				}
4474138593Ssam			}
4475138593Ssam		}
4476138593Ssam	}
4477186101Ssam
4478186904Ssam	if (opmode == IEEE80211_M_AHDEMO) {
4479186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOT, &val) != -1)
4480186904Ssam			LINE_CHECK("tdmaslot %u", val);
4481186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTCNT, &val) != -1)
4482186904Ssam			LINE_CHECK("tdmaslotcnt %u", val);
4483186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTLEN, &val) != -1)
4484186904Ssam			LINE_CHECK("tdmaslotlen %u", val);
4485186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_BINTERVAL, &val) != -1)
4486186904Ssam			LINE_CHECK("tdmabintval %u", val);
4487186904Ssam	} else if (get80211val(s, IEEE80211_IOC_BEACON_INTERVAL, &val) != -1) {
4488173275Ssam		/* XXX default define not visible */
4489173275Ssam		if (val != 100 || verbose)
4490173275Ssam			LINE_CHECK("bintval %u", val);
4491138593Ssam	}
4492138593Ssam
4493138593Ssam	if (wme && verbose) {
4494138593Ssam		LINE_BREAK();
4495138593Ssam		list_wme(s);
4496138593Ssam	}
4497173275Ssam	LINE_BREAK();
4498173275Ssam}
4499138593Ssam
4500173275Ssamstatic int
4501173275Ssamget80211(int s, int type, void *data, int len)
4502173275Ssam{
4503173275Ssam	struct ieee80211req ireq;
4504138593Ssam
4505173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4506173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4507173275Ssam	ireq.i_type = type;
4508173275Ssam	ireq.i_data = data;
4509173275Ssam	ireq.i_len = len;
4510173275Ssam	return ioctl(s, SIOCG80211, &ireq);
4511173275Ssam}
4512138593Ssam
4513173275Ssamstatic int
4514173275Ssamget80211len(int s, int type, void *data, int len, int *plen)
4515173275Ssam{
4516173275Ssam	struct ieee80211req ireq;
4517138593Ssam
4518173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4519173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4520173275Ssam	ireq.i_type = type;
4521173275Ssam	ireq.i_len = len;
4522187801Ssam	assert(ireq.i_len == len);	/* NB: check for 16-bit truncation */
4523173275Ssam	ireq.i_data = data;
4524173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4525173275Ssam		return -1;
4526173275Ssam	*plen = ireq.i_len;
4527173275Ssam	return 0;
4528173275Ssam}
4529138593Ssam
4530173275Ssamstatic int
4531173275Ssamget80211val(int s, int type, int *val)
4532173275Ssam{
4533173275Ssam	struct ieee80211req ireq;
453477218Sphk
4535173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4536173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4537173275Ssam	ireq.i_type = type;
4538173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4539173275Ssam		return -1;
4540173275Ssam	*val = ireq.i_val;
4541173275Ssam	return 0;
454277218Sphk}
454377218Sphk
454477218Sphkstatic void
4545170531Ssamset80211(int s, int type, int val, int len, void *data)
454677218Sphk{
454777218Sphk	struct ieee80211req	ireq;
454877218Sphk
454977218Sphk	(void) memset(&ireq, 0, sizeof(ireq));
455077218Sphk	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
455177218Sphk	ireq.i_type = type;
455277218Sphk	ireq.i_val = val;
455377218Sphk	ireq.i_len = len;
4554187801Ssam	assert(ireq.i_len == len);	/* NB: check for 16-bit truncation */
455577218Sphk	ireq.i_data = data;
455691454Sbrooks	if (ioctl(s, SIOCS80211, &ireq) < 0)
455777218Sphk		err(1, "SIOCS80211");
455877218Sphk}
455977218Sphk
456077218Sphkstatic const char *
456177218Sphkget_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
456277218Sphk{
456377218Sphk	int len;
456477218Sphk	int hexstr;
456577218Sphk	u_int8_t *p;
456677218Sphk
456777218Sphk	len = *lenp;
456877218Sphk	p = buf;
456977218Sphk	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
457077218Sphk	if (hexstr)
457177218Sphk		val += 2;
457277218Sphk	for (;;) {
457377218Sphk		if (*val == '\0')
457477218Sphk			break;
457577218Sphk		if (sep != NULL && strchr(sep, *val) != NULL) {
457677218Sphk			val++;
457777218Sphk			break;
457877218Sphk		}
457977218Sphk		if (hexstr) {
4580127831Sphk			if (!isxdigit((u_char)val[0])) {
458177218Sphk				warnx("bad hexadecimal digits");
458277218Sphk				return NULL;
458377218Sphk			}
4584127831Sphk			if (!isxdigit((u_char)val[1])) {
4585127831Sphk				warnx("odd count hexadecimal digits");
4586127831Sphk				return NULL;
4587127831Sphk			}
458877218Sphk		}
4589127831Sphk		if (p >= buf + len) {
459077218Sphk			if (hexstr)
459177218Sphk				warnx("hexadecimal digits too long");
459277218Sphk			else
4593127831Sphk				warnx("string too long");
459477218Sphk			return NULL;
459577218Sphk		}
459677218Sphk		if (hexstr) {
459777218Sphk#define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
459877218Sphk			*p++ = (tohex((u_char)val[0]) << 4) |
459977218Sphk			    tohex((u_char)val[1]);
460077218Sphk#undef tohex
460177218Sphk			val += 2;
460277218Sphk		} else
460377218Sphk			*p++ = *val++;
460477218Sphk	}
460577218Sphk	len = p - buf;
460677218Sphk	/* The string "-" is treated as the empty string. */
4607165045Ssam	if (!hexstr && len == 1 && buf[0] == '-') {
460877218Sphk		len = 0;
4609165045Ssam		memset(buf, 0, *lenp);
4610165045Ssam	} else if (len < *lenp)
461177218Sphk		memset(p, 0, *lenp - len);
461277218Sphk	*lenp = len;
461377218Sphk	return val;
461477218Sphk}
461577218Sphk
461677218Sphkstatic void
461777218Sphkprint_string(const u_int8_t *buf, int len)
461877218Sphk{
461977218Sphk	int i;
462077218Sphk	int hasspc;
462177218Sphk
462277218Sphk	i = 0;
462377218Sphk	hasspc = 0;
462491454Sbrooks	for (; i < len; i++) {
462577218Sphk		if (!isprint(buf[i]) && buf[i] != '\0')
462677218Sphk			break;
462777218Sphk		if (isspace(buf[i]))
462877218Sphk			hasspc++;
462977218Sphk	}
463077218Sphk	if (i == len) {
463177218Sphk		if (hasspc || len == 0 || buf[0] == '\0')
463277218Sphk			printf("\"%.*s\"", len, buf);
463377218Sphk		else
463477218Sphk			printf("%.*s", len, buf);
463577218Sphk	} else {
463677218Sphk		printf("0x");
463777218Sphk		for (i = 0; i < len; i++)
463877218Sphk			printf("%02x", buf[i]);
463977218Sphk	}
464077218Sphk}
464177218Sphk
4642178354Ssam/*
4643178354Ssam * Virtual AP cloning support.
4644178354Ssam */
4645178354Ssamstatic struct ieee80211_clone_params params = {
4646178354Ssam	.icp_opmode	= IEEE80211_M_STA,	/* default to station mode */
4647178354Ssam};
4648178354Ssam
4649178354Ssamstatic void
4650178354Ssamwlan_create(int s, struct ifreq *ifr)
4651178354Ssam{
4652178354Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
4653178354Ssam
4654178354Ssam	if (params.icp_parent[0] == '\0')
4655178354Ssam		errx(1, "must specify a parent when creating a wlan device");
4656178354Ssam	if (params.icp_opmode == IEEE80211_M_WDS &&
4657178354Ssam	    memcmp(params.icp_bssid, zerobssid, sizeof(zerobssid)) == 0)
4658178354Ssam		errx(1, "no bssid specified for WDS (use wlanbssid)");
4659178354Ssam	ifr->ifr_data = (caddr_t) &params;
4660178354Ssam	if (ioctl(s, SIOCIFCREATE2, ifr) < 0)
4661178354Ssam		err(1, "SIOCIFCREATE2");
4662178354Ssam}
4663178354Ssam
4664178354Ssamstatic
4665178354SsamDECL_CMD_FUNC(set80211clone_wlandev, arg, d)
4666178354Ssam{
4667178354Ssam	strlcpy(params.icp_parent, arg, IFNAMSIZ);
4668178354Ssam	clone_setcallback(wlan_create);
4669178354Ssam}
4670178354Ssam
4671178354Ssamstatic
4672178354SsamDECL_CMD_FUNC(set80211clone_wlanbssid, arg, d)
4673178354Ssam{
4674178354Ssam	const struct ether_addr *ea;
4675178354Ssam
4676178354Ssam	ea = ether_aton(arg);
4677178354Ssam	if (ea == NULL)
4678178354Ssam		errx(1, "%s: cannot parse bssid", arg);
4679178354Ssam	memcpy(params.icp_bssid, ea->octet, IEEE80211_ADDR_LEN);
4680178354Ssam	clone_setcallback(wlan_create);
4681178354Ssam}
4682178354Ssam
4683178354Ssamstatic
4684178354SsamDECL_CMD_FUNC(set80211clone_wlanaddr, arg, d)
4685178354Ssam{
4686178354Ssam	const struct ether_addr *ea;
4687178354Ssam
4688178354Ssam	ea = ether_aton(arg);
4689178354Ssam	if (ea == NULL)
4690178354Ssam		errx(1, "%s: cannot parse addres", arg);
4691178354Ssam	memcpy(params.icp_macaddr, ea->octet, IEEE80211_ADDR_LEN);
4692178354Ssam	params.icp_flags |= IEEE80211_CLONE_MACADDR;
4693178354Ssam	clone_setcallback(wlan_create);
4694178354Ssam}
4695178354Ssam
4696178354Ssamstatic
4697178354SsamDECL_CMD_FUNC(set80211clone_wlanmode, arg, d)
4698178354Ssam{
4699178354Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
4700178354Ssam	if (iseq(arg, "sta"))
4701178354Ssam		params.icp_opmode = IEEE80211_M_STA;
4702178354Ssam	else if (iseq(arg, "ahdemo") || iseq(arg, "adhoc-demo"))
4703178354Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
4704178354Ssam	else if (iseq(arg, "ibss") || iseq(arg, "adhoc"))
4705178354Ssam		params.icp_opmode = IEEE80211_M_IBSS;
4706178354Ssam	else if (iseq(arg, "ap") || iseq(arg, "host"))
4707178354Ssam		params.icp_opmode = IEEE80211_M_HOSTAP;
4708178354Ssam	else if (iseq(arg, "wds"))
4709178354Ssam		params.icp_opmode = IEEE80211_M_WDS;
4710178354Ssam	else if (iseq(arg, "monitor"))
4711178354Ssam		params.icp_opmode = IEEE80211_M_MONITOR;
4712186904Ssam	else if (iseq(arg, "tdma")) {
4713186904Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
4714186904Ssam		params.icp_flags |= IEEE80211_CLONE_TDMA;
4715186904Ssam	} else
4716178354Ssam		errx(1, "Don't know to create %s for %s", arg, name);
4717178354Ssam	clone_setcallback(wlan_create);
4718178354Ssam#undef iseq
4719178354Ssam}
4720178354Ssam
4721178354Ssamstatic void
4722178354Ssamset80211clone_beacons(const char *val, int d, int s, const struct afswtch *rafp)
4723178354Ssam{
4724178354Ssam	/* NB: inverted sense */
4725178354Ssam	if (d)
4726178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_NOBEACONS;
4727178354Ssam	else
4728178354Ssam		params.icp_flags |= IEEE80211_CLONE_NOBEACONS;
4729178354Ssam	clone_setcallback(wlan_create);
4730178354Ssam}
4731178354Ssam
4732178354Ssamstatic void
4733178354Ssamset80211clone_bssid(const char *val, int d, int s, const struct afswtch *rafp)
4734178354Ssam{
4735178354Ssam	if (d)
4736178354Ssam		params.icp_flags |= IEEE80211_CLONE_BSSID;
4737178354Ssam	else
4738178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_BSSID;
4739178354Ssam	clone_setcallback(wlan_create);
4740178354Ssam}
4741178354Ssam
4742178354Ssamstatic void
4743178354Ssamset80211clone_wdslegacy(const char *val, int d, int s, const struct afswtch *rafp)
4744178354Ssam{
4745178354Ssam	if (d)
4746178354Ssam		params.icp_flags |= IEEE80211_CLONE_WDSLEGACY;
4747178354Ssam	else
4748178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_WDSLEGACY;
4749178354Ssam	clone_setcallback(wlan_create);
4750178354Ssam}
4751178354Ssam
4752138593Ssamstatic struct cmd ieee80211_cmds[] = {
4753138593Ssam	DEF_CMD_ARG("ssid",		set80211ssid),
4754138593Ssam	DEF_CMD_ARG("nwid",		set80211ssid),
4755138593Ssam	DEF_CMD_ARG("stationname",	set80211stationname),
4756138593Ssam	DEF_CMD_ARG("station",		set80211stationname),	/* BSD/OS */
4757138593Ssam	DEF_CMD_ARG("channel",		set80211channel),
4758138593Ssam	DEF_CMD_ARG("authmode",		set80211authmode),
4759138593Ssam	DEF_CMD_ARG("powersavemode",	set80211powersavemode),
4760138593Ssam	DEF_CMD("powersave",	1,	set80211powersave),
4761138593Ssam	DEF_CMD("-powersave",	0,	set80211powersave),
4762138593Ssam	DEF_CMD_ARG("powersavesleep", 	set80211powersavesleep),
4763138593Ssam	DEF_CMD_ARG("wepmode",		set80211wepmode),
4764138593Ssam	DEF_CMD("wep",		1,	set80211wep),
4765138593Ssam	DEF_CMD("-wep",		0,	set80211wep),
4766139493Ssam	DEF_CMD_ARG("deftxkey",		set80211weptxkey),
4767138593Ssam	DEF_CMD_ARG("weptxkey",		set80211weptxkey),
4768138593Ssam	DEF_CMD_ARG("wepkey",		set80211wepkey),
4769138593Ssam	DEF_CMD_ARG("nwkey",		set80211nwkey),		/* NetBSD */
4770138593Ssam	DEF_CMD("-nwkey",	0,	set80211wep),		/* NetBSD */
4771138593Ssam	DEF_CMD_ARG("rtsthreshold",	set80211rtsthreshold),
4772138593Ssam	DEF_CMD_ARG("protmode",		set80211protmode),
4773138593Ssam	DEF_CMD_ARG("txpower",		set80211txpower),
4774138593Ssam	DEF_CMD_ARG("roaming",		set80211roaming),
4775138593Ssam	DEF_CMD("wme",		1,	set80211wme),
4776138593Ssam	DEF_CMD("-wme",		0,	set80211wme),
4777178354Ssam	DEF_CMD("wmm",		1,	set80211wme),
4778178354Ssam	DEF_CMD("-wmm",		0,	set80211wme),
4779138593Ssam	DEF_CMD("hidessid",	1,	set80211hidessid),
4780138593Ssam	DEF_CMD("-hidessid",	0,	set80211hidessid),
4781138593Ssam	DEF_CMD("apbridge",	1,	set80211apbridge),
4782138593Ssam	DEF_CMD("-apbridge",	0,	set80211apbridge),
4783138593Ssam	DEF_CMD_ARG("chanlist",		set80211chanlist),
4784138593Ssam	DEF_CMD_ARG("bssid",		set80211bssid),
4785138593Ssam	DEF_CMD_ARG("ap",		set80211bssid),
4786138593Ssam	DEF_CMD("scan",	0,		set80211scan),
4787138593Ssam	DEF_CMD_ARG("list",		set80211list),
4788138593Ssam	DEF_CMD_ARG2("cwmin",		set80211cwmin),
4789138593Ssam	DEF_CMD_ARG2("cwmax",		set80211cwmax),
4790138593Ssam	DEF_CMD_ARG2("aifs",		set80211aifs),
4791138593Ssam	DEF_CMD_ARG2("txoplimit",	set80211txoplimit),
4792148621Ssam	DEF_CMD_ARG("acm",		set80211acm),
4793148621Ssam	DEF_CMD_ARG("-acm",		set80211noacm),
4794148621Ssam	DEF_CMD_ARG("ack",		set80211ackpolicy),
4795148621Ssam	DEF_CMD_ARG("-ack",		set80211noackpolicy),
4796138593Ssam	DEF_CMD_ARG2("bss:cwmin",	set80211bsscwmin),
4797138593Ssam	DEF_CMD_ARG2("bss:cwmax",	set80211bsscwmax),
4798138593Ssam	DEF_CMD_ARG2("bss:aifs",	set80211bssaifs),
4799138593Ssam	DEF_CMD_ARG2("bss:txoplimit",	set80211bsstxoplimit),
4800138593Ssam	DEF_CMD_ARG("dtimperiod",	set80211dtimperiod),
4801138593Ssam	DEF_CMD_ARG("bintval",		set80211bintval),
4802138593Ssam	DEF_CMD("mac:open",	IEEE80211_MACCMD_POLICY_OPEN,	set80211maccmd),
4803138593Ssam	DEF_CMD("mac:allow",	IEEE80211_MACCMD_POLICY_ALLOW,	set80211maccmd),
4804138593Ssam	DEF_CMD("mac:deny",	IEEE80211_MACCMD_POLICY_DENY,	set80211maccmd),
4805178354Ssam	DEF_CMD("mac:radius",	IEEE80211_MACCMD_POLICY_RADIUS,	set80211maccmd),
4806138593Ssam	DEF_CMD("mac:flush",	IEEE80211_MACCMD_FLUSH,		set80211maccmd),
4807138593Ssam	DEF_CMD("mac:detach",	IEEE80211_MACCMD_DETACH,	set80211maccmd),
4808138593Ssam	DEF_CMD_ARG("mac:add",		set80211addmac),
4809138593Ssam	DEF_CMD_ARG("mac:del",		set80211delmac),
4810138593Ssam	DEF_CMD_ARG("mac:kick",		set80211kickmac),
4811147795Ssam	DEF_CMD("pureg",	1,	set80211pureg),
4812147795Ssam	DEF_CMD("-pureg",	0,	set80211pureg),
4813170531Ssam	DEF_CMD("ff",		1,	set80211fastframes),
4814170531Ssam	DEF_CMD("-ff",		0,	set80211fastframes),
4815170531Ssam	DEF_CMD("dturbo",	1,	set80211dturbo),
4816170531Ssam	DEF_CMD("-dturbo",	0,	set80211dturbo),
4817170531Ssam	DEF_CMD("bgscan",	1,	set80211bgscan),
4818170531Ssam	DEF_CMD("-bgscan",	0,	set80211bgscan),
4819170531Ssam	DEF_CMD_ARG("bgscanidle",	set80211bgscanidle),
4820170531Ssam	DEF_CMD_ARG("bgscanintvl",	set80211bgscanintvl),
4821170531Ssam	DEF_CMD_ARG("scanvalid",	set80211scanvalid),
4822178354Ssam	DEF_CMD_ARG("roam:rssi",	set80211roamrssi),
4823178354Ssam	DEF_CMD_ARG("roam:rate",	set80211roamrate),
4824153354Ssam	DEF_CMD_ARG("mcastrate",	set80211mcastrate),
4825178354Ssam	DEF_CMD_ARG("ucastrate",	set80211ucastrate),
4826178354Ssam	DEF_CMD_ARG("mgtrate",		set80211mgtrate),
4827178354Ssam	DEF_CMD_ARG("mgmtrate",		set80211mgtrate),
4828178354Ssam	DEF_CMD_ARG("maxretry",		set80211maxretry),
4829148416Ssam	DEF_CMD_ARG("fragthreshold",	set80211fragthreshold),
4830153422Ssam	DEF_CMD("burst",	1,	set80211burst),
4831153422Ssam	DEF_CMD("-burst",	0,	set80211burst),
4832160687Ssam	DEF_CMD_ARG("bmiss",		set80211bmissthreshold),
4833160687Ssam	DEF_CMD_ARG("bmissthreshold",	set80211bmissthreshold),
4834173275Ssam	DEF_CMD("shortgi",	1,	set80211shortgi),
4835173275Ssam	DEF_CMD("-shortgi",	0,	set80211shortgi),
4836173275Ssam	DEF_CMD("ampdurx",	2,	set80211ampdu),
4837173275Ssam	DEF_CMD("-ampdurx",	-2,	set80211ampdu),
4838173275Ssam	DEF_CMD("ampdutx",	1,	set80211ampdu),
4839173275Ssam	DEF_CMD("-ampdutx",	-1,	set80211ampdu),
4840173275Ssam	DEF_CMD("ampdu",	3,	set80211ampdu),		/* NB: tx+rx */
4841173275Ssam	DEF_CMD("-ampdu",	-3,	set80211ampdu),
4842173275Ssam	DEF_CMD_ARG("ampdulimit",	set80211ampdulimit),
4843173275Ssam	DEF_CMD_ARG("ampdudensity",	set80211ampdudensity),
4844173275Ssam	DEF_CMD("amsdurx",	2,	set80211amsdu),
4845173275Ssam	DEF_CMD("-amsdurx",	-2,	set80211amsdu),
4846173275Ssam	DEF_CMD("amsdutx",	1,	set80211amsdu),
4847173275Ssam	DEF_CMD("-amsdutx",	-1,	set80211amsdu),
4848173275Ssam	DEF_CMD("amsdu",	3,	set80211amsdu),		/* NB: tx+rx */
4849173275Ssam	DEF_CMD("-amsdu",	-3,	set80211amsdu),
4850173275Ssam	DEF_CMD_ARG("amsdulimit",	set80211amsdulimit),
4851173275Ssam	DEF_CMD("puren",	1,	set80211puren),
4852173275Ssam	DEF_CMD("-puren",	0,	set80211puren),
4853170531Ssam	DEF_CMD("doth",		1,	set80211doth),
4854170531Ssam	DEF_CMD("-doth",	0,	set80211doth),
4855178354Ssam	DEF_CMD("dfs",		1,	set80211dfs),
4856178354Ssam	DEF_CMD("-dfs",		0,	set80211dfs),
4857173275Ssam	DEF_CMD("htcompat",	1,	set80211htcompat),
4858173275Ssam	DEF_CMD("-htcompat",	0,	set80211htcompat),
4859178354Ssam	DEF_CMD("dwds",		1,	set80211dwds),
4860178354Ssam	DEF_CMD("-dwds",	0,	set80211dwds),
4861173275Ssam	DEF_CMD("inact",	1,	set80211inact),
4862173275Ssam	DEF_CMD("-inact",	0,	set80211inact),
4863178354Ssam	DEF_CMD("tsn",		1,	set80211tsn),
4864178354Ssam	DEF_CMD("-tsn",		0,	set80211tsn),
4865178354Ssam	DEF_CMD_ARG("regdomain",	set80211regdomain),
4866178354Ssam	DEF_CMD_ARG("country",		set80211country),
4867178354Ssam	DEF_CMD("indoor",	'I',	set80211location),
4868178354Ssam	DEF_CMD("-indoor",	'O',	set80211location),
4869178354Ssam	DEF_CMD("outdoor",	'O',	set80211location),
4870178354Ssam	DEF_CMD("-outdoor",	'I',	set80211location),
4871178354Ssam	DEF_CMD("anywhere",	' ',	set80211location),
4872178354Ssam	DEF_CMD("ecm",		1,	set80211ecm),
4873178354Ssam	DEF_CMD("-ecm",		0,	set80211ecm),
4874178354Ssam	DEF_CMD("dotd",		1,	set80211dotd),
4875178354Ssam	DEF_CMD("-dotd",	0,	set80211dotd),
4876173275Ssam	DEF_CMD_ARG("htprotmode",	set80211htprotmode),
4877173275Ssam	DEF_CMD("ht20",		1,	set80211htconf),
4878173275Ssam	DEF_CMD("-ht20",	0,	set80211htconf),
4879173275Ssam	DEF_CMD("ht40",		3,	set80211htconf),	/* NB: 20+40 */
4880173275Ssam	DEF_CMD("-ht40",	0,	set80211htconf),
4881173275Ssam	DEF_CMD("ht",		3,	set80211htconf),	/* NB: 20+40 */
4882173275Ssam	DEF_CMD("-ht",		0,	set80211htconf),
4883183261Ssam	DEF_CMD("rifs",		1,	set80211rifs),
4884183261Ssam	DEF_CMD("-rifs",	0,	set80211rifs),
4885183261Ssam	DEF_CMD("smps",		IEEE80211_HTCAP_SMPS_ENA,	set80211smps),
4886183261Ssam	DEF_CMD("smpsdyn",	IEEE80211_HTCAP_SMPS_DYNAMIC,	set80211smps),
4887183261Ssam	DEF_CMD("-smps",	IEEE80211_HTCAP_SMPS_OFF,	set80211smps),
4888178354Ssam	/* XXX for testing */
4889178354Ssam	DEF_CMD_ARG("chanswitch",	set80211chanswitch),
4890178354Ssam
4891186904Ssam	DEF_CMD_ARG("tdmaslot",		set80211tdmaslot),
4892186904Ssam	DEF_CMD_ARG("tdmaslotcnt",	set80211tdmaslotcnt),
4893186904Ssam	DEF_CMD_ARG("tdmaslotlen",	set80211tdmaslotlen),
4894186904Ssam	DEF_CMD_ARG("tdmabintval",	set80211tdmabintval),
4895186904Ssam
4896178354Ssam	/* vap cloning support */
4897178354Ssam	DEF_CLONE_CMD_ARG("wlanaddr",	set80211clone_wlanaddr),
4898178354Ssam	DEF_CLONE_CMD_ARG("wlanbssid",	set80211clone_wlanbssid),
4899178354Ssam	DEF_CLONE_CMD_ARG("wlandev",	set80211clone_wlandev),
4900178354Ssam	DEF_CLONE_CMD_ARG("wlanmode",	set80211clone_wlanmode),
4901178354Ssam	DEF_CLONE_CMD("beacons", 1,	set80211clone_beacons),
4902178354Ssam	DEF_CLONE_CMD("-beacons", 0,	set80211clone_beacons),
4903178354Ssam	DEF_CLONE_CMD("bssid",	1,	set80211clone_bssid),
4904178354Ssam	DEF_CLONE_CMD("-bssid",	0,	set80211clone_bssid),
4905178354Ssam	DEF_CLONE_CMD("wdslegacy", 1,	set80211clone_wdslegacy),
4906178354Ssam	DEF_CLONE_CMD("-wdslegacy", 0,	set80211clone_wdslegacy),
4907138593Ssam};
4908138593Ssamstatic struct afswtch af_ieee80211 = {
4909138593Ssam	.af_name	= "af_ieee80211",
4910138593Ssam	.af_af		= AF_UNSPEC,
4911139494Ssam	.af_other_status = ieee80211_status,
4912138593Ssam};
4913138593Ssam
4914138593Ssamstatic __constructor void
4915138593Ssamieee80211_ctor(void)
4916138593Ssam{
4917138593Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
4918138593Ssam	int i;
4919138593Ssam
4920138593Ssam	for (i = 0; i < N(ieee80211_cmds);  i++)
4921138593Ssam		cmd_register(&ieee80211_cmds[i]);
4922138593Ssam	af_register(&af_ieee80211);
4923138593Ssam#undef N
4924138593Ssam}
4925