ifieee80211.c revision 186904
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 186904 2009-01-08 17:12:47Z sam $
2877218Sphk */
2977218Sphk
3077218Sphk/*-
3177218Sphk * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
3277218Sphk * All rights reserved.
3377218Sphk *
3477218Sphk * This code is derived from software contributed to The NetBSD Foundation
3577218Sphk * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
3677218Sphk * NASA Ames Research Center.
3777218Sphk *
3877218Sphk * Redistribution and use in source and binary forms, with or without
3977218Sphk * modification, are permitted provided that the following conditions
4077218Sphk * are met:
4177218Sphk * 1. Redistributions of source code must retain the above copyright
4277218Sphk *    notice, this list of conditions and the following disclaimer.
4377218Sphk * 2. Redistributions in binary form must reproduce the above copyright
4477218Sphk *    notice, this list of conditions and the following disclaimer in the
4577218Sphk *    documentation and/or other materials provided with the distribution.
4677218Sphk * 3. All advertising materials mentioning features or use of this software
4777218Sphk *    must display the following acknowledgement:
4877218Sphk *	This product includes software developed by the NetBSD
4977218Sphk *	Foundation, Inc. and its contributors.
5077218Sphk * 4. Neither the name of The NetBSD Foundation nor the names of its
5177218Sphk *    contributors may be used to endorse or promote products derived
5277218Sphk *    from this software without specific prior written permission.
5377218Sphk *
5477218Sphk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
5577218Sphk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
5677218Sphk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5777218Sphk * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
5877218Sphk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5977218Sphk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
6077218Sphk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
6177218Sphk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
6277218Sphk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
6377218Sphk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
6477218Sphk * POSSIBILITY OF SUCH DAMAGE.
6577218Sphk */
6677218Sphk
6777218Sphk#include <sys/param.h>
6877218Sphk#include <sys/ioctl.h>
6977218Sphk#include <sys/socket.h>
7077218Sphk#include <sys/sysctl.h>
7177218Sphk#include <sys/time.h>
7277218Sphk
7377218Sphk#include <net/ethernet.h>
7477218Sphk#include <net/if.h>
7577218Sphk#include <net/if_dl.h>
7677218Sphk#include <net/if_types.h>
77138593Ssam#include <net/if_media.h>
7877218Sphk#include <net/route.h>
79138593Ssam
80116957Ssam#include <net80211/ieee80211_ioctl.h>
8177218Sphk
8277218Sphk#include <ctype.h>
8377218Sphk#include <err.h>
8477218Sphk#include <errno.h>
8577218Sphk#include <fcntl.h>
86146873Sjhb#include <inttypes.h>
8777218Sphk#include <stdio.h>
8877218Sphk#include <stdlib.h>
8977218Sphk#include <string.h>
9077218Sphk#include <unistd.h>
91155931Ssam#include <stdarg.h>
92173275Ssam#include <stddef.h>		/* NB: for offsetof */
9377218Sphk
9477218Sphk#include "ifconfig.h"
95178354Ssam#include "regdomain.h"
9677218Sphk
97178354Ssam#ifndef IEEE80211_FIXED_RATE_NONE
98178354Ssam#define	IEEE80211_FIXED_RATE_NONE	0xff
99178354Ssam#endif
100178354Ssam
101178354Ssam#define	REQ_ECM		0x01000000	/* enable if ECM set */
102178354Ssam#define	REQ_OUTDOOR	0x02000000	/* enable for outdoor operation */
103178354Ssam#define	REQ_FLAGS	0xff000000	/* private flags, don't pass to os */
104178354Ssam
105178354Ssam/* XXX need these publicly defined or similar */
106178354Ssam#ifndef IEEE80211_NODE_AUTH
107178354Ssam#define	IEEE80211_NODE_AUTH	0x0001		/* authorized for data */
108178354Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
109178354Ssam#define	IEEE80211_NODE_ERP	0x0004		/* ERP enabled */
110178354Ssam#define	IEEE80211_NODE_PWR_MGT	0x0010		/* power save mode enabled */
111178354Ssam#define	IEEE80211_NODE_HT	0x0040		/* HT enabled */
112178354Ssam#define	IEEE80211_NODE_HTCOMPAT	0x0080		/* HT setup w/ vendor OUI's */
113178354Ssam#define	IEEE80211_NODE_WPS	0x0100		/* WPS association */
114178354Ssam#define	IEEE80211_NODE_TSN	0x0200		/* TSN association */
115183261Ssam#define	IEEE80211_NODE_AMPDU_RX	0x0400		/* AMPDU rx enabled */
116183261Ssam#define	IEEE80211_NODE_AMPDU_TX	0x0800		/* AMPDU tx enabled */
117183261Ssam#define	IEEE80211_NODE_MIMO_PS	0x1000		/* MIMO power save enabled */
118183261Ssam#define	IEEE80211_NODE_MIMO_RTS	0x2000		/* send RTS in MIMO PS */
119183261Ssam#define	IEEE80211_NODE_RIFS	0x4000		/* RIFS enabled */
120178354Ssam#endif
121178354Ssam
122173275Ssam#define	MAXCOL	78
123173275Ssamstatic	int col;
124173275Ssamstatic	char spacer;
125173275Ssam
126173275Ssamstatic void LINE_INIT(char c);
127173275Ssamstatic void LINE_BREAK(void);
128173275Ssamstatic void LINE_CHECK(const char *fmt, ...);
129173275Ssam
130178354Ssamstatic const char *modename[] = {
131178354Ssam	"auto", "11a", "11b", "11g", "fh", "turboA", "turboG",
132178354Ssam	"sturbo", "11na", "11ng"
133173275Ssam};
134173275Ssam
135178354Ssamstatic void set80211(int s, int type, int val, int len, void *data);
136173275Ssamstatic int get80211(int s, int type, void *data, int len);
137173275Ssamstatic int get80211len(int s, int type, void *data, int len, int *plen);
138173275Ssamstatic int get80211val(int s, int type, int *val);
13977218Sphkstatic const char *get_string(const char *val, const char *sep,
14077218Sphk    u_int8_t *buf, int *lenp);
14177218Sphkstatic void print_string(const u_int8_t *buf, int len);
142178354Ssamstatic void print_regdomain(const struct ieee80211_regdomain *, int);
143178354Ssamstatic void print_channels(int, const struct ieee80211req_chaninfo *,
144178354Ssam    int allchans, int verbose);
145178354Ssamstatic void regdomain_makechannels(struct ieee80211_regdomain_req *,
146178354Ssam    const struct ieee80211_devcaps_req *);
14777218Sphk
148170531Ssamstatic struct ieee80211req_chaninfo chaninfo;
149178354Ssamstatic struct ieee80211_regdomain regdomain;
150178354Ssamstatic int gotregdomain = 0;
151178354Ssamstatic struct ieee80211_roamparams_req roamparams;
152178354Ssamstatic int gotroam = 0;
153178354Ssamstatic struct ieee80211_txparams_req txparams;
154178354Ssamstatic int gottxparams = 0;
155173275Ssamstatic struct ieee80211_channel curchan;
156173275Ssamstatic int gotcurchan = 0;
157178354Ssamstatic struct ifmediareq *ifmr;
158173275Ssamstatic int htconf = 0;
159173275Ssamstatic	int gothtconf = 0;
160170531Ssam
161173275Ssamstatic void
162173275Ssamgethtconf(int s)
163173275Ssam{
164173275Ssam	if (gothtconf)
165173275Ssam		return;
166173275Ssam	if (get80211val(s, IEEE80211_IOC_HTCONF, &htconf) < 0)
167173275Ssam		warn("unable to get HT configuration information");
168173275Ssam	gothtconf = 1;
169173275Ssam}
170173275Ssam
171170531Ssam/*
172170531Ssam * Collect channel info from the kernel.  We use this (mostly)
173170531Ssam * to handle mapping between frequency and IEEE channel number.
174170531Ssam */
175170531Ssamstatic void
176170531Ssamgetchaninfo(int s)
177170531Ssam{
178170531Ssam	if (chaninfo.ic_nchans != 0)
179170531Ssam		return;
180173275Ssam	if (get80211(s, IEEE80211_IOC_CHANINFO, &chaninfo, sizeof(chaninfo)) < 0)
181170531Ssam		errx(1, "unable to get channel information");
182170531Ssam	ifmr = ifmedia_getstate(s);
183173275Ssam	gethtconf(s);
184170531Ssam}
185170531Ssam
186178354Ssamstatic struct regdata *
187178354Ssamgetregdata(void)
188178354Ssam{
189178354Ssam	static struct regdata *rdp = NULL;
190178354Ssam	if (rdp == NULL) {
191178354Ssam		rdp = lib80211_alloc_regdata();
192178354Ssam		if (rdp == NULL)
193181198Ssam			errx(-1, "missing or corrupted regdomain database");
194178354Ssam	}
195178354Ssam	return rdp;
196178354Ssam}
197178354Ssam
198170531Ssam/*
199170531Ssam * Given the channel at index i with attributes from,
200170531Ssam * check if there is a channel with attributes to in
201170531Ssam * the channel table.  With suitable attributes this
202170531Ssam * allows the caller to look for promotion; e.g. from
203170531Ssam * 11b > 11g.
204170531Ssam */
205138593Ssamstatic int
206170531Ssamcanpromote(int i, int from, int to)
207170531Ssam{
208170531Ssam	const struct ieee80211_channel *fc = &chaninfo.ic_chans[i];
209170531Ssam	int j;
210170531Ssam
211170531Ssam	if ((fc->ic_flags & from) != from)
212170531Ssam		return i;
213170531Ssam	/* NB: quick check exploiting ordering of chans w/ same frequency */
214170531Ssam	if (i+1 < chaninfo.ic_nchans &&
215170531Ssam	    chaninfo.ic_chans[i+1].ic_freq == fc->ic_freq &&
216170531Ssam	    (chaninfo.ic_chans[i+1].ic_flags & to) == to)
217170531Ssam		return i+1;
218170531Ssam	/* brute force search in case channel list is not ordered */
219170531Ssam	for (j = 0; j < chaninfo.ic_nchans; j++) {
220170531Ssam		const struct ieee80211_channel *tc = &chaninfo.ic_chans[j];
221170531Ssam		if (j != i &&
222170531Ssam		    tc->ic_freq == fc->ic_freq && (tc->ic_flags & to) == to)
223170531Ssam		return j;
224170531Ssam	}
225170531Ssam	return i;
226170531Ssam}
227170531Ssam
228170531Ssam/*
229170531Ssam * Handle channel promotion.  When a channel is specified with
230170531Ssam * only a frequency we want to promote it to the ``best'' channel
231170531Ssam * available.  The channel list has separate entries for 11b, 11g,
232170531Ssam * 11a, and 11n[ga] channels so specifying a frequency w/o any
233170531Ssam * attributes requires we upgrade, e.g. from 11b -> 11g.  This
234170531Ssam * gets complicated when the channel is specified on the same
235170531Ssam * command line with a media request that constrains the available
236170531Ssam * channe list (e.g. mode 11a); we want to honor that to avoid
237170531Ssam * confusing behaviour.
238170531Ssam */
239170531Ssamstatic int
240170531Ssampromote(int i)
241170531Ssam{
242170531Ssam	/*
243170531Ssam	 * Query the current mode of the interface in case it's
244170531Ssam	 * constrained (e.g. to 11a).  We must do this carefully
245170531Ssam	 * as there may be a pending ifmedia request in which case
246170531Ssam	 * asking the kernel will give us the wrong answer.  This
247170531Ssam	 * is an unfortunate side-effect of the way ifconfig is
248170531Ssam	 * structure for modularity (yech).
249170531Ssam	 *
250170531Ssam	 * NB: ifmr is actually setup in getchaninfo (above); we
251170531Ssam	 *     assume it's called coincident with to this call so
252170531Ssam	 *     we have a ``current setting''; otherwise we must pass
253170531Ssam	 *     the socket descriptor down to here so we can make
254170531Ssam	 *     the ifmedia_getstate call ourselves.
255170531Ssam	 */
256170531Ssam	int chanmode = ifmr != NULL ? IFM_MODE(ifmr->ifm_current) : IFM_AUTO;
257170531Ssam
258170531Ssam	/* when ambiguous promote to ``best'' */
259170531Ssam	/* NB: we abitrarily pick HT40+ over HT40- */
260170531Ssam	if (chanmode != IFM_IEEE80211_11B)
261170531Ssam		i = canpromote(i, IEEE80211_CHAN_B, IEEE80211_CHAN_G);
262173275Ssam	if (chanmode != IFM_IEEE80211_11G && (htconf & 1)) {
263170531Ssam		i = canpromote(i, IEEE80211_CHAN_G,
264170531Ssam			IEEE80211_CHAN_G | IEEE80211_CHAN_HT20);
265173275Ssam		if (htconf & 2) {
266173275Ssam			i = canpromote(i, IEEE80211_CHAN_G,
267173275Ssam				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D);
268173275Ssam			i = canpromote(i, IEEE80211_CHAN_G,
269173275Ssam				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U);
270173275Ssam		}
271170531Ssam	}
272173275Ssam	if (chanmode != IFM_IEEE80211_11A && (htconf & 1)) {
273170531Ssam		i = canpromote(i, IEEE80211_CHAN_A,
274170531Ssam			IEEE80211_CHAN_A | IEEE80211_CHAN_HT20);
275173275Ssam		if (htconf & 2) {
276173275Ssam			i = canpromote(i, IEEE80211_CHAN_A,
277173275Ssam				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D);
278173275Ssam			i = canpromote(i, IEEE80211_CHAN_A,
279173275Ssam				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U);
280173275Ssam		}
281170531Ssam	}
282170531Ssam	return i;
283170531Ssam}
284170531Ssam
285170531Ssamstatic void
286170531Ssammapfreq(struct ieee80211_channel *chan, int freq, int flags)
287170531Ssam{
288170531Ssam	int i;
289170531Ssam
290170531Ssam	for (i = 0; i < chaninfo.ic_nchans; i++) {
291170531Ssam		const struct ieee80211_channel *c = &chaninfo.ic_chans[i];
292170531Ssam
293170531Ssam		if (c->ic_freq == freq && (c->ic_flags & flags) == flags) {
294170531Ssam			if (flags == 0) {
295170531Ssam				/* when ambiguous promote to ``best'' */
296170531Ssam				c = &chaninfo.ic_chans[promote(i)];
297170531Ssam			}
298170531Ssam			*chan = *c;
299170531Ssam			return;
300170531Ssam		}
301170531Ssam	}
302170531Ssam	errx(1, "unknown/undefined frequency %u/0x%x", freq, flags);
303170531Ssam}
304170531Ssam
305170531Ssamstatic void
306170531Ssammapchan(struct ieee80211_channel *chan, int ieee, int flags)
307170531Ssam{
308170531Ssam	int i;
309170531Ssam
310170531Ssam	for (i = 0; i < chaninfo.ic_nchans; i++) {
311170531Ssam		const struct ieee80211_channel *c = &chaninfo.ic_chans[i];
312170531Ssam
313170531Ssam		if (c->ic_ieee == ieee && (c->ic_flags & flags) == flags) {
314170531Ssam			if (flags == 0) {
315170531Ssam				/* when ambiguous promote to ``best'' */
316170531Ssam				c = &chaninfo.ic_chans[promote(i)];
317170531Ssam			}
318170531Ssam			*chan = *c;
319170531Ssam			return;
320170531Ssam		}
321170531Ssam	}
322173275Ssam	errx(1, "unknown/undefined channel number %d flags 0x%x", ieee, flags);
323170531Ssam}
324170531Ssam
325173275Ssamstatic const struct ieee80211_channel *
326173275Ssamgetcurchan(int s)
327173275Ssam{
328173275Ssam	if (gotcurchan)
329173275Ssam		return &curchan;
330173275Ssam	if (get80211(s, IEEE80211_IOC_CURCHAN, &curchan, sizeof(curchan)) < 0) {
331173275Ssam		int val;
332173275Ssam		/* fall back to legacy ioctl */
333173275Ssam		if (get80211val(s, IEEE80211_IOC_CHANNEL, &val) < 0)
334173275Ssam			errx(-1, "cannot figure out current channel");
335173275Ssam		getchaninfo(s);
336173275Ssam		mapchan(&curchan, val, 0);
337173275Ssam	}
338173275Ssam	gotcurchan = 1;
339173275Ssam	return &curchan;
340173275Ssam}
341173275Ssam
342178354Ssamstatic enum ieee80211_phymode
343178354Ssamchan2mode(const struct ieee80211_channel *c)
344178354Ssam{
345178354Ssam	if (IEEE80211_IS_CHAN_HTA(c))
346178354Ssam		return IEEE80211_MODE_11NA;
347178354Ssam	if (IEEE80211_IS_CHAN_HTG(c))
348178354Ssam		return IEEE80211_MODE_11NG;
349178354Ssam	if (IEEE80211_IS_CHAN_108A(c))
350178354Ssam		return IEEE80211_MODE_TURBO_A;
351178354Ssam	if (IEEE80211_IS_CHAN_108G(c))
352178354Ssam		return IEEE80211_MODE_TURBO_G;
353178354Ssam	if (IEEE80211_IS_CHAN_ST(c))
354178354Ssam		return IEEE80211_MODE_STURBO_A;
355178354Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
356178354Ssam		return IEEE80211_MODE_FH;
357178354Ssam	if (IEEE80211_IS_CHAN_A(c))
358178354Ssam		return IEEE80211_MODE_11A;
359178354Ssam	if (IEEE80211_IS_CHAN_ANYG(c))
360178354Ssam		return IEEE80211_MODE_11G;
361178354Ssam	if (IEEE80211_IS_CHAN_B(c))
362178354Ssam		return IEEE80211_MODE_11B;
363178354Ssam	return IEEE80211_MODE_AUTO;
364178354Ssam}
365178354Ssam
366178354Ssamstatic void
367178354Ssamgetroam(int s)
368178354Ssam{
369178354Ssam	if (gotroam)
370178354Ssam		return;
371178354Ssam	if (get80211(s, IEEE80211_IOC_ROAM,
372178354Ssam	    &roamparams, sizeof(roamparams)) < 0)
373178354Ssam		errx(1, "unable to get roaming parameters");
374178354Ssam	gotroam = 1;
375178354Ssam}
376178354Ssam
377178354Ssamstatic void
378178354Ssamsetroam_cb(int s, void *arg)
379178354Ssam{
380178354Ssam	struct ieee80211_roamparams_req *roam = arg;
381178354Ssam	set80211(s, IEEE80211_IOC_ROAM, 0, sizeof(*roam), roam);
382178354Ssam}
383178354Ssam
384178354Ssamstatic void
385178354Ssamgettxparams(int s)
386178354Ssam{
387178354Ssam	if (gottxparams)
388178354Ssam		return;
389178354Ssam	if (get80211(s, IEEE80211_IOC_TXPARAMS,
390178354Ssam	    &txparams, sizeof(txparams)) < 0)
391178354Ssam		errx(1, "unable to get transmit parameters");
392178354Ssam	gottxparams = 1;
393178354Ssam}
394178354Ssam
395178354Ssamstatic void
396178354Ssamsettxparams_cb(int s, void *arg)
397178354Ssam{
398178354Ssam	struct ieee80211_txparams_req *txp = arg;
399178354Ssam	set80211(s, IEEE80211_IOC_TXPARAMS, 0, sizeof(*txp), txp);
400178354Ssam}
401178354Ssam
402178354Ssamstatic void
403178354Ssamgetregdomain(int s)
404178354Ssam{
405178354Ssam	if (gotregdomain)
406178354Ssam		return;
407178354Ssam	if (get80211(s, IEEE80211_IOC_REGDOMAIN,
408178354Ssam	    &regdomain, sizeof(regdomain)) < 0)
409178354Ssam		errx(1, "unable to get regulatory domain info");
410178354Ssam	gotregdomain = 1;
411178354Ssam}
412178354Ssam
413178354Ssamstatic void
414178354Ssamgetdevcaps(int s, struct ieee80211_devcaps_req *dc)
415178354Ssam{
416178354Ssam	if (get80211(s, IEEE80211_IOC_DEVCAPS, dc, sizeof(*dc)) < 0)
417178354Ssam		errx(1, "unable to get device capabilities");
418178354Ssam}
419178354Ssam
420178354Ssamstatic void
421178354Ssamsetregdomain_cb(int s, void *arg)
422178354Ssam{
423178354Ssam	struct ieee80211_regdomain_req req;
424178354Ssam	struct ieee80211_regdomain *rd = arg;
425178354Ssam	struct ieee80211_devcaps_req dc;
426178354Ssam	struct regdata *rdp = getregdata();
427178354Ssam
428186102Ssam	if (rd->country != NO_COUNTRY) {
429178354Ssam		const struct country *cc;
430178354Ssam		/*
431178354Ssam		 * Check current country seting to make sure it's
432178354Ssam		 * compatible with the new regdomain.  If not, then
433178354Ssam		 * override it with any default country for this
434178354Ssam		 * SKU.  If we cannot arrange a match, then abort.
435178354Ssam		 */
436178354Ssam		cc = lib80211_country_findbycc(rdp, rd->country);
437178354Ssam		if (cc == NULL)
438178354Ssam			errx(1, "unknown ISO country code %d", rd->country);
439178354Ssam		if (cc->rd->sku != rd->regdomain) {
440178354Ssam			const struct regdomain *rp;
441178354Ssam			/*
442178354Ssam			 * Check if country is incompatible with regdomain.
443178354Ssam			 * To enable multiple regdomains for a country code
444178354Ssam			 * we permit a mismatch between the regdomain and
445178354Ssam			 * the country's associated regdomain when the
446178354Ssam			 * regdomain is setup w/o a default country.  For
447178354Ssam			 * example, US is bound to the FCC regdomain but
448178354Ssam			 * we allow US to be combined with FCC3 because FCC3
449178354Ssam			 * has not default country.  This allows bogus
450178354Ssam			 * combinations like FCC3+DK which are resolved when
451178354Ssam			 * constructing the channel list by deferring to the
452178354Ssam			 * regdomain to construct the channel list.
453178354Ssam			 */
454178354Ssam			rp = lib80211_regdomain_findbysku(rdp, rd->regdomain);
455178354Ssam			if (rp == NULL)
456178354Ssam				errx(1, "country %s (%s) is not usable with "
457178354Ssam				    "regdomain %d", cc->isoname, cc->name,
458178354Ssam				    rd->regdomain);
459186102Ssam			else if (rp->cc != NULL && rp->cc != cc)
460178354Ssam				errx(1, "country %s (%s) is not usable with "
461178354Ssam				   "regdomain %s", cc->isoname, cc->name,
462178354Ssam				   rp->name);
463178354Ssam		}
464178354Ssam	}
465178354Ssam	req.rd = *rd;
466178354Ssam	/*
467178354Ssam	 * Fetch the device capabilities and calculate the
468178354Ssam	 * full set of netbands for which we request a new
469178354Ssam	 * channel list be constructed.  Once that's done we
470178354Ssam	 * push the regdomain info + channel list to the kernel.
471178354Ssam	 */
472178354Ssam	getdevcaps(s, &dc);
473178354Ssam#if 0
474178354Ssam	if (verbose) {
475178354Ssam		printf("drivercaps: 0x%x\n", dc.dc_drivercaps);
476178354Ssam		printf("cryptocaps: 0x%x\n", dc.dc_cryptocaps);
477178354Ssam		printf("htcaps    : 0x%x\n", dc.dc_htcaps);
478178354Ssam		memcpy(&chaninfo, &dc.dc_chaninfo, sizeof(chaninfo));
479178354Ssam		print_channels(s, &dc.dc_chaninfo, 1/*allchans*/, 1/*verbose*/);
480178354Ssam	}
481178354Ssam#endif
482178354Ssam	regdomain_makechannels(&req, &dc);
483178354Ssam	if (verbose) {
484178354Ssam		LINE_INIT(':');
485178354Ssam		print_regdomain(rd, 1/*verbose*/);
486178354Ssam		LINE_BREAK();
487178354Ssam		memcpy(&chaninfo, &req.chaninfo, sizeof(chaninfo));
488178354Ssam		print_channels(s, &req.chaninfo, 1/*allchans*/, 1/*verbose*/);
489178354Ssam	}
490178354Ssam	if (req.chaninfo.ic_nchans == 0)
491178354Ssam		errx(1, "no channels calculated");
492178354Ssam	set80211(s, IEEE80211_IOC_REGDOMAIN, 0, sizeof(req), &req);
493178354Ssam}
494178354Ssam
495170531Ssamstatic int
496170531Ssamieee80211_mhz2ieee(int freq, int flags)
497170531Ssam{
498170531Ssam	struct ieee80211_channel chan;
499170531Ssam	mapfreq(&chan, freq, flags);
500170531Ssam	return chan.ic_ieee;
501170531Ssam}
502170531Ssam
503170531Ssamstatic int
504138593Ssamisanyarg(const char *arg)
505138593Ssam{
506173275Ssam	return (strncmp(arg, "-", 1) == 0 ||
507173275Ssam	    strncasecmp(arg, "any", 3) == 0 || strncasecmp(arg, "off", 3) == 0);
508138593Ssam}
509138593Ssam
510138593Ssamstatic void
51177218Sphkset80211ssid(const char *val, int d, int s, const struct afswtch *rafp)
51277218Sphk{
51377218Sphk	int		ssid;
51477218Sphk	int		len;
515151883Sbrooks	u_int8_t	data[IEEE80211_NWID_LEN];
51677218Sphk
51777218Sphk	ssid = 0;
518121827Sbrooks	len = strlen(val);
519178354Ssam	if (len > 2 && isdigit((int)val[0]) && val[1] == ':') {
52088748Sambrisko		ssid = atoi(val)-1;
52188748Sambrisko		val += 2;
52288748Sambrisko	}
52377218Sphk
52477218Sphk	bzero(data, sizeof(data));
52577218Sphk	len = sizeof(data);
526151883Sbrooks	if (get_string(val, NULL, data, &len) == NULL)
527151883Sbrooks		exit(1);
52877218Sphk
52977218Sphk	set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
53077218Sphk}
53177218Sphk
532138593Ssamstatic void
53377218Sphkset80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
53477218Sphk{
53577218Sphk	int			len;
53677218Sphk	u_int8_t		data[33];
53777218Sphk
53877218Sphk	bzero(data, sizeof(data));
53977218Sphk	len = sizeof(data);
54077218Sphk	get_string(val, NULL, data, &len);
54177218Sphk
54277218Sphk	set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
54377218Sphk}
54477218Sphk
545138593Ssam/*
546170531Ssam * Parse a channel specification for attributes/flags.
547170531Ssam * The syntax is:
548170531Ssam *	freq/xx		channel width (5,10,20,40,40+,40-)
549170531Ssam *	freq:mode	channel mode (a,b,g,h,n,t,s,d)
550170531Ssam *
551170531Ssam * These can be combined in either order; e.g. 2437:ng/40.
552170531Ssam * Modes are case insensitive.
553170531Ssam *
554170531Ssam * The result is not validated here; it's assumed to be
555170531Ssam * checked against the channel table fetched from the kernel.
556170531Ssam */
557170531Ssamstatic int
558173275Ssamgetchannelflags(const char *val, int freq)
559138593Ssam{
560170531Ssam#define	_CHAN_HT	0x80000000
561170531Ssam	const char *cp;
562170531Ssam	int flags;
563138593Ssam
564170531Ssam	flags = 0;
565166015Ssam
566170531Ssam	cp = strchr(val, ':');
567170531Ssam	if (cp != NULL) {
568170531Ssam		for (cp++; isalpha((int) *cp); cp++) {
569170531Ssam			/* accept mixed case */
570170531Ssam			int c = *cp;
571170531Ssam			if (isupper(c))
572170531Ssam				c = tolower(c);
573170531Ssam			switch (c) {
574170531Ssam			case 'a':		/* 802.11a */
575170531Ssam				flags |= IEEE80211_CHAN_A;
576170531Ssam				break;
577170531Ssam			case 'b':		/* 802.11b */
578170531Ssam				flags |= IEEE80211_CHAN_B;
579170531Ssam				break;
580170531Ssam			case 'g':		/* 802.11g */
581170531Ssam				flags |= IEEE80211_CHAN_G;
582170531Ssam				break;
583170531Ssam			case 'h':		/* ht = 802.11n */
584170531Ssam			case 'n':		/* 802.11n */
585170531Ssam				flags |= _CHAN_HT;	/* NB: private */
586170531Ssam				break;
587170531Ssam			case 'd':		/* dt = Atheros Dynamic Turbo */
588170531Ssam				flags |= IEEE80211_CHAN_TURBO;
589170531Ssam				break;
590170531Ssam			case 't':		/* ht, dt, st, t */
591170531Ssam				/* dt and unadorned t specify Dynamic Turbo */
592170531Ssam				if ((flags & (IEEE80211_CHAN_STURBO|_CHAN_HT)) == 0)
593170531Ssam					flags |= IEEE80211_CHAN_TURBO;
594170531Ssam				break;
595170531Ssam			case 's':		/* st = Atheros Static Turbo */
596170531Ssam				flags |= IEEE80211_CHAN_STURBO;
597170531Ssam				break;
598170531Ssam			default:
599173275Ssam				errx(-1, "%s: Invalid channel attribute %c\n",
600170531Ssam				    val, *cp);
601170531Ssam			}
602170531Ssam		}
603170531Ssam	}
604170531Ssam	cp = strchr(val, '/');
605170531Ssam	if (cp != NULL) {
606170531Ssam		char *ep;
607170531Ssam		u_long cw = strtoul(cp+1, &ep, 10);
608166015Ssam
609170531Ssam		switch (cw) {
610170531Ssam		case 5:
611170531Ssam			flags |= IEEE80211_CHAN_QUARTER;
612170531Ssam			break;
613170531Ssam		case 10:
614170531Ssam			flags |= IEEE80211_CHAN_HALF;
615170531Ssam			break;
616170531Ssam		case 20:
617170531Ssam			/* NB: this may be removed below */
618170531Ssam			flags |= IEEE80211_CHAN_HT20;
619170531Ssam			break;
620170531Ssam		case 40:
621170531Ssam			if (ep != NULL && *ep == '+')
622170531Ssam				flags |= IEEE80211_CHAN_HT40U;
623170531Ssam			else if (ep != NULL && *ep == '-')
624170531Ssam				flags |= IEEE80211_CHAN_HT40D;
625170531Ssam			break;
626170531Ssam		default:
627173275Ssam			errx(-1, "%s: Invalid channel width\n", val);
628170531Ssam		}
629165570Ssam	}
630170531Ssam	/*
631170531Ssam	 * Cleanup specifications.
632170531Ssam	 */
633170531Ssam	if ((flags & _CHAN_HT) == 0) {
634170531Ssam		/*
635170531Ssam		 * If user specified freq/20 or freq/40 quietly remove
636170531Ssam		 * HT cw attributes depending on channel use.  To give
637170531Ssam		 * an explicit 20/40 width for an HT channel you must
638170531Ssam		 * indicate it is an HT channel since all HT channels
639170531Ssam		 * are also usable for legacy operation; e.g. freq:n/40.
640170531Ssam		 */
641170531Ssam		flags &= ~IEEE80211_CHAN_HT;
642170531Ssam	} else {
643170531Ssam		/*
644170531Ssam		 * Remove private indicator that this is an HT channel
645170531Ssam		 * and if no explicit channel width has been given
646170531Ssam		 * provide the default settings.
647170531Ssam		 */
648170531Ssam		flags &= ~_CHAN_HT;
649173275Ssam		if ((flags & IEEE80211_CHAN_HT) == 0) {
650173275Ssam			struct ieee80211_channel chan;
651173275Ssam			/*
652173275Ssam			 * Consult the channel list to see if we can use
653173275Ssam			 * HT40+ or HT40- (if both the map routines choose).
654173275Ssam			 */
655173275Ssam			if (freq > 255)
656173275Ssam				mapfreq(&chan, freq, 0);
657173275Ssam			else
658173275Ssam				mapchan(&chan, freq, 0);
659173275Ssam			flags |= (chan.ic_flags & IEEE80211_CHAN_HT);
660173275Ssam		}
661170531Ssam	}
662170531Ssam	return flags;
663170531Ssam#undef _CHAN_HT
664138593Ssam}
665138593Ssam
666138593Ssamstatic void
667186105Ssamgetchannel(int s, struct ieee80211_channel *chan, const char *val)
668186105Ssam{
669186105Ssam	int v, flags;
670186105Ssam	char *eptr;
671186105Ssam
672186105Ssam	memset(chan, 0, sizeof(*chan));
673186105Ssam	if (isanyarg(val)) {
674186105Ssam		chan->ic_freq = IEEE80211_CHAN_ANY;
675186105Ssam		return;
676186105Ssam	}
677186105Ssam	getchaninfo(s);
678186105Ssam	errno = 0;
679186105Ssam	v = strtol(val, &eptr, 10);
680186105Ssam	if (val[0] == '\0' || val == eptr || errno == ERANGE ||
681186105Ssam	    /* channel may be suffixed with nothing, :flag, or /width */
682186105Ssam	    (eptr[0] != '\0' && eptr[0] != ':' && eptr[0] != '/'))
683186105Ssam		errx(1, "invalid channel specification%s",
684186105Ssam		    errno == ERANGE ? " (out of range)" : "");
685186105Ssam	flags = getchannelflags(val, v);
686186105Ssam	if (v > 255) {		/* treat as frequency */
687186105Ssam		mapfreq(chan, v, flags);
688186105Ssam	} else {
689186105Ssam		mapchan(chan, v, flags);
690186105Ssam	}
691186105Ssam}
692186105Ssam
693186105Ssamstatic void
69477218Sphkset80211channel(const char *val, int d, int s, const struct afswtch *rafp)
69577218Sphk{
696170531Ssam	struct ieee80211_channel chan;
697170531Ssam
698186105Ssam	getchannel(s, &chan, val);
699170531Ssam	set80211(s, IEEE80211_IOC_CURCHAN, 0, sizeof(chan), &chan);
70077218Sphk}
70177218Sphk
702138593Ssamstatic void
703178354Ssamset80211chanswitch(const char *val, int d, int s, const struct afswtch *rafp)
704178354Ssam{
705178354Ssam	struct ieee80211_chanswitch_req csr;
706178354Ssam
707186105Ssam	getchannel(s, &csr.csa_chan, val);
708178354Ssam	csr.csa_mode = 1;
709178354Ssam	csr.csa_count = 5;
710178354Ssam	set80211(s, IEEE80211_IOC_CHANSWITCH, 0, sizeof(csr), &csr);
711178354Ssam}
712178354Ssam
713178354Ssamstatic void
71477218Sphkset80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
71577218Sphk{
71677218Sphk	int	mode;
71777218Sphk
71891454Sbrooks	if (strcasecmp(val, "none") == 0) {
71977218Sphk		mode = IEEE80211_AUTH_NONE;
72091454Sbrooks	} else if (strcasecmp(val, "open") == 0) {
72177218Sphk		mode = IEEE80211_AUTH_OPEN;
72291454Sbrooks	} else if (strcasecmp(val, "shared") == 0) {
72377218Sphk		mode = IEEE80211_AUTH_SHARED;
724138593Ssam	} else if (strcasecmp(val, "8021x") == 0) {
725138593Ssam		mode = IEEE80211_AUTH_8021X;
726138593Ssam	} else if (strcasecmp(val, "wpa") == 0) {
727138593Ssam		mode = IEEE80211_AUTH_WPA;
72877218Sphk	} else {
729150708Sru		errx(1, "unknown authmode");
73077218Sphk	}
73177218Sphk
73277218Sphk	set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
73377218Sphk}
73477218Sphk
735138593Ssamstatic void
73677218Sphkset80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
73777218Sphk{
73877218Sphk	int	mode;
73977218Sphk
74091454Sbrooks	if (strcasecmp(val, "off") == 0) {
74177218Sphk		mode = IEEE80211_POWERSAVE_OFF;
74291454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
74377218Sphk		mode = IEEE80211_POWERSAVE_ON;
74491454Sbrooks	} else if (strcasecmp(val, "cam") == 0) {
74577218Sphk		mode = IEEE80211_POWERSAVE_CAM;
74691454Sbrooks	} else if (strcasecmp(val, "psp") == 0) {
74777218Sphk		mode = IEEE80211_POWERSAVE_PSP;
74891454Sbrooks	} else if (strcasecmp(val, "psp-cam") == 0) {
74977218Sphk		mode = IEEE80211_POWERSAVE_PSP_CAM;
75077218Sphk	} else {
751150708Sru		errx(1, "unknown powersavemode");
75277218Sphk	}
75377218Sphk
75477218Sphk	set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
75577218Sphk}
75677218Sphk
757138593Ssamstatic void
75877218Sphkset80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
75977218Sphk{
76077218Sphk	if (d == 0)
76177218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
76277218Sphk		    0, NULL);
76377218Sphk	else
76477218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
76577218Sphk		    0, NULL);
76677218Sphk}
76777218Sphk
768138593Ssamstatic void
76977218Sphkset80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
77077218Sphk{
77177218Sphk	set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
77277218Sphk}
77377218Sphk
774138593Ssamstatic void
77577218Sphkset80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
77677218Sphk{
77777218Sphk	int	mode;
77877218Sphk
77991454Sbrooks	if (strcasecmp(val, "off") == 0) {
78077218Sphk		mode = IEEE80211_WEP_OFF;
78191454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
78277218Sphk		mode = IEEE80211_WEP_ON;
78391454Sbrooks	} else if (strcasecmp(val, "mixed") == 0) {
78477218Sphk		mode = IEEE80211_WEP_MIXED;
78577218Sphk	} else {
786150708Sru		errx(1, "unknown wep mode");
78777218Sphk	}
78877218Sphk
78977218Sphk	set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
79077218Sphk}
79177218Sphk
792138593Ssamstatic void
79377218Sphkset80211wep(const char *val, int d, int s, const struct afswtch *rafp)
79477218Sphk{
79577218Sphk	set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
79677218Sphk}
79777218Sphk
798139493Ssamstatic int
799139493Ssamisundefarg(const char *arg)
800139493Ssam{
801139493Ssam	return (strcmp(arg, "-") == 0 || strncasecmp(arg, "undef", 5) == 0);
802139493Ssam}
803139493Ssam
804138593Ssamstatic void
80577218Sphkset80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
80677218Sphk{
807139493Ssam	if (isundefarg(val))
808139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, IEEE80211_KEYIX_NONE, 0, NULL);
809139493Ssam	else
810139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
81177218Sphk}
81277218Sphk
813138593Ssamstatic void
81477218Sphkset80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
81577218Sphk{
81677218Sphk	int		key = 0;
81777218Sphk	int		len;
818120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
81977218Sphk
820178354Ssam	if (isdigit((int)val[0]) && val[1] == ':') {
82177218Sphk		key = atoi(val)-1;
82277218Sphk		val += 2;
82377218Sphk	}
82477218Sphk
82577218Sphk	bzero(data, sizeof(data));
82677218Sphk	len = sizeof(data);
82777218Sphk	get_string(val, NULL, data, &len);
82877218Sphk
82977218Sphk	set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
83077218Sphk}
83177218Sphk
83277218Sphk/*
833148686Sstefanf * This function is purely a NetBSD compatability interface.  The NetBSD
834148686Sstefanf * interface is too inflexible, but it's there so we'll support it since
83577218Sphk * it's not all that hard.
83677218Sphk */
837138593Ssamstatic void
83877218Sphkset80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
83977218Sphk{
84077218Sphk	int		txkey;
84177218Sphk	int		i, len;
842120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
84377218Sphk
84477218Sphk	set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
84577218Sphk
846178354Ssam	if (isdigit((int)val[0]) && val[1] == ':') {
84777218Sphk		txkey = val[0]-'0'-1;
84877218Sphk		val += 2;
84977218Sphk
85091454Sbrooks		for (i = 0; i < 4; i++) {
85177218Sphk			bzero(data, sizeof(data));
85277218Sphk			len = sizeof(data);
85377218Sphk			val = get_string(val, ",", data, &len);
854151827Sbrooks			if (val == NULL)
855151827Sbrooks				exit(1);
85677218Sphk
85777218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
85877218Sphk		}
85977218Sphk	} else {
86077218Sphk		bzero(data, sizeof(data));
86177218Sphk		len = sizeof(data);
86277218Sphk		get_string(val, NULL, data, &len);
86377218Sphk		txkey = 0;
86477218Sphk
86577218Sphk		set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
86677218Sphk
86777218Sphk		bzero(data, sizeof(data));
86891454Sbrooks		for (i = 1; i < 4; i++)
86977218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
87077218Sphk	}
87177218Sphk
87277218Sphk	set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
87377218Sphk}
87477218Sphk
875138593Ssamstatic void
876127649Ssamset80211rtsthreshold(const char *val, int d, int s, const struct afswtch *rafp)
877127649Ssam{
878148416Ssam	set80211(s, IEEE80211_IOC_RTSTHRESHOLD,
879148416Ssam		isundefarg(val) ? IEEE80211_RTS_MAX : atoi(val), 0, NULL);
880127649Ssam}
881127649Ssam
882138593Ssamstatic void
883127649Ssamset80211protmode(const char *val, int d, int s, const struct afswtch *rafp)
884127649Ssam{
885127649Ssam	int	mode;
886127649Ssam
887127649Ssam	if (strcasecmp(val, "off") == 0) {
888127649Ssam		mode = IEEE80211_PROTMODE_OFF;
889127649Ssam	} else if (strcasecmp(val, "cts") == 0) {
890127649Ssam		mode = IEEE80211_PROTMODE_CTS;
891173275Ssam	} else if (strncasecmp(val, "rtscts", 3) == 0) {
892127649Ssam		mode = IEEE80211_PROTMODE_RTSCTS;
893127649Ssam	} else {
894150708Sru		errx(1, "unknown protection mode");
895127649Ssam	}
896127649Ssam
897127649Ssam	set80211(s, IEEE80211_IOC_PROTMODE, mode, 0, NULL);
898127649Ssam}
899127649Ssam
900138593Ssamstatic void
901173275Ssamset80211htprotmode(const char *val, int d, int s, const struct afswtch *rafp)
902173275Ssam{
903173275Ssam	int	mode;
904173275Ssam
905173275Ssam	if (strcasecmp(val, "off") == 0) {
906173275Ssam		mode = IEEE80211_PROTMODE_OFF;
907173275Ssam	} else if (strncasecmp(val, "rts", 3) == 0) {
908173275Ssam		mode = IEEE80211_PROTMODE_RTSCTS;
909173275Ssam	} else {
910173275Ssam		errx(1, "unknown protection mode");
911173275Ssam	}
912173275Ssam
913173275Ssam	set80211(s, IEEE80211_IOC_HTPROTMODE, mode, 0, NULL);
914173275Ssam}
915173275Ssam
916173275Ssamstatic void
917127649Ssamset80211txpower(const char *val, int d, int s, const struct afswtch *rafp)
918127649Ssam{
919173275Ssam	double v = atof(val);
920173275Ssam	int txpow;
921173275Ssam
922173275Ssam	txpow = (int) (2*v);
923173275Ssam	if (txpow != 2*v)
924173275Ssam		errx(-1, "invalid tx power (must be .5 dBm units)");
925173275Ssam	set80211(s, IEEE80211_IOC_TXPOWER, txpow, 0, NULL);
926127649Ssam}
927127649Ssam
928138593Ssam#define	IEEE80211_ROAMING_DEVICE	0
929138593Ssam#define	IEEE80211_ROAMING_AUTO		1
930138593Ssam#define	IEEE80211_ROAMING_MANUAL	2
931138593Ssam
932138593Ssamstatic void
933138593Ssamset80211roaming(const char *val, int d, int s, const struct afswtch *rafp)
93477218Sphk{
935138593Ssam	int mode;
93677218Sphk
937138593Ssam	if (strcasecmp(val, "device") == 0) {
938138593Ssam		mode = IEEE80211_ROAMING_DEVICE;
939138593Ssam	} else if (strcasecmp(val, "auto") == 0) {
940138593Ssam		mode = IEEE80211_ROAMING_AUTO;
941138593Ssam	} else if (strcasecmp(val, "manual") == 0) {
942138593Ssam		mode = IEEE80211_ROAMING_MANUAL;
943138593Ssam	} else {
944150708Sru		errx(1, "unknown roaming mode");
945138593Ssam	}
946138593Ssam	set80211(s, IEEE80211_IOC_ROAMING, mode, 0, NULL);
947138593Ssam}
948138593Ssam
949138593Ssamstatic void
950138593Ssamset80211wme(const char *val, int d, int s, const struct afswtch *rafp)
951138593Ssam{
952138593Ssam	set80211(s, IEEE80211_IOC_WME, d, 0, NULL);
953138593Ssam}
954138593Ssam
955138593Ssamstatic void
956138593Ssamset80211hidessid(const char *val, int d, int s, const struct afswtch *rafp)
957138593Ssam{
958138593Ssam	set80211(s, IEEE80211_IOC_HIDESSID, d, 0, NULL);
959138593Ssam}
960138593Ssam
961138593Ssamstatic void
962138593Ssamset80211apbridge(const char *val, int d, int s, const struct afswtch *rafp)
963138593Ssam{
964138593Ssam	set80211(s, IEEE80211_IOC_APBRIDGE, d, 0, NULL);
965138593Ssam}
966138593Ssam
967138593Ssamstatic void
968170531Ssamset80211fastframes(const char *val, int d, int s, const struct afswtch *rafp)
969170531Ssam{
970170531Ssam	set80211(s, IEEE80211_IOC_FF, d, 0, NULL);
971170531Ssam}
972170531Ssam
973170531Ssamstatic void
974170531Ssamset80211dturbo(const char *val, int d, int s, const struct afswtch *rafp)
975170531Ssam{
976170531Ssam	set80211(s, IEEE80211_IOC_TURBOP, d, 0, NULL);
977170531Ssam}
978170531Ssam
979170531Ssamstatic void
980138593Ssamset80211chanlist(const char *val, int d, int s, const struct afswtch *rafp)
981138593Ssam{
982138593Ssam	struct ieee80211req_chanlist chanlist;
983138593Ssam#define	MAXCHAN	(sizeof(chanlist.ic_channels)*NBBY)
984138593Ssam	char *temp, *cp, *tp;
985138593Ssam
986138593Ssam	temp = malloc(strlen(val) + 1);
987138593Ssam	if (temp == NULL)
988138593Ssam		errx(1, "malloc failed");
989138593Ssam	strcpy(temp, val);
990138593Ssam	memset(&chanlist, 0, sizeof(chanlist));
991138593Ssam	cp = temp;
992138593Ssam	for (;;) {
993173275Ssam		int first, last, f, c;
994138593Ssam
995138593Ssam		tp = strchr(cp, ',');
996138593Ssam		if (tp != NULL)
997138593Ssam			*tp++ = '\0';
998138593Ssam		switch (sscanf(cp, "%u-%u", &first, &last)) {
999138593Ssam		case 1:
1000138593Ssam			if (first > MAXCHAN)
1001146873Sjhb				errx(-1, "channel %u out of range, max %zu",
1002138593Ssam					first, MAXCHAN);
1003138593Ssam			setbit(chanlist.ic_channels, first);
1004138593Ssam			break;
1005138593Ssam		case 2:
1006138593Ssam			if (first > MAXCHAN)
1007146873Sjhb				errx(-1, "channel %u out of range, max %zu",
1008138593Ssam					first, MAXCHAN);
1009138593Ssam			if (last > MAXCHAN)
1010146873Sjhb				errx(-1, "channel %u out of range, max %zu",
1011138593Ssam					last, MAXCHAN);
1012138593Ssam			if (first > last)
1013138593Ssam				errx(-1, "void channel range, %u > %u",
1014138593Ssam					first, last);
1015138593Ssam			for (f = first; f <= last; f++)
1016138593Ssam				setbit(chanlist.ic_channels, f);
1017138593Ssam			break;
1018138593Ssam		}
1019138593Ssam		if (tp == NULL)
1020138593Ssam			break;
1021173275Ssam		c = *tp;
1022173275Ssam		while (isspace(c))
1023138593Ssam			tp++;
1024173275Ssam		if (!isdigit(c))
1025138593Ssam			break;
1026138593Ssam		cp = tp;
1027138593Ssam	}
1028170531Ssam	set80211(s, IEEE80211_IOC_CHANLIST, 0, sizeof(chanlist), &chanlist);
1029138593Ssam#undef MAXCHAN
1030138593Ssam}
1031138593Ssam
1032138593Ssamstatic void
1033138593Ssamset80211bssid(const char *val, int d, int s, const struct afswtch *rafp)
1034138593Ssam{
1035138593Ssam
1036138593Ssam	if (!isanyarg(val)) {
1037138593Ssam		char *temp;
1038138593Ssam		struct sockaddr_dl sdl;
1039138593Ssam
1040155702Ssam		temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1041138593Ssam		if (temp == NULL)
1042138593Ssam			errx(1, "malloc failed");
1043138593Ssam		temp[0] = ':';
1044138593Ssam		strcpy(temp + 1, val);
1045138593Ssam		sdl.sdl_len = sizeof(sdl);
1046138593Ssam		link_addr(temp, &sdl);
1047138593Ssam		free(temp);
1048138593Ssam		if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1049138593Ssam			errx(1, "malformed link-level address");
1050138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
1051138593Ssam			IEEE80211_ADDR_LEN, LLADDR(&sdl));
1052138593Ssam	} else {
1053138593Ssam		uint8_t zerobssid[IEEE80211_ADDR_LEN];
1054138593Ssam		memset(zerobssid, 0, sizeof(zerobssid));
1055138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
1056138593Ssam			IEEE80211_ADDR_LEN, zerobssid);
1057138593Ssam	}
1058138593Ssam}
1059138593Ssam
1060138593Ssamstatic int
1061138593Ssamgetac(const char *ac)
1062138593Ssam{
1063138593Ssam	if (strcasecmp(ac, "ac_be") == 0 || strcasecmp(ac, "be") == 0)
1064138593Ssam		return WME_AC_BE;
1065138593Ssam	if (strcasecmp(ac, "ac_bk") == 0 || strcasecmp(ac, "bk") == 0)
1066138593Ssam		return WME_AC_BK;
1067138593Ssam	if (strcasecmp(ac, "ac_vi") == 0 || strcasecmp(ac, "vi") == 0)
1068138593Ssam		return WME_AC_VI;
1069138593Ssam	if (strcasecmp(ac, "ac_vo") == 0 || strcasecmp(ac, "vo") == 0)
1070138593Ssam		return WME_AC_VO;
1071138593Ssam	errx(1, "unknown wme access class %s", ac);
1072138593Ssam}
1073138593Ssam
1074138593Ssamstatic
1075138593SsamDECL_CMD_FUNC2(set80211cwmin, ac, val)
1076138593Ssam{
1077138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val), getac(ac), NULL);
1078138593Ssam}
1079138593Ssam
1080138593Ssamstatic
1081138593SsamDECL_CMD_FUNC2(set80211cwmax, ac, val)
1082138593Ssam{
1083138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val), getac(ac), NULL);
1084138593Ssam}
1085138593Ssam
1086138593Ssamstatic
1087138593SsamDECL_CMD_FUNC2(set80211aifs, ac, val)
1088138593Ssam{
1089138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val), getac(ac), NULL);
1090138593Ssam}
1091138593Ssam
1092138593Ssamstatic
1093138593SsamDECL_CMD_FUNC2(set80211txoplimit, ac, val)
1094138593Ssam{
1095138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), getac(ac), NULL);
1096138593Ssam}
1097138593Ssam
1098138593Ssamstatic
1099148621SsamDECL_CMD_FUNC(set80211acm, ac, d)
1100138593Ssam{
1101148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 1, getac(ac), NULL);
1102138593Ssam}
1103148621Ssamstatic
1104148621SsamDECL_CMD_FUNC(set80211noacm, ac, d)
1105148621Ssam{
1106148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 0, getac(ac), NULL);
1107148621Ssam}
1108138593Ssam
1109138593Ssamstatic
1110148621SsamDECL_CMD_FUNC(set80211ackpolicy, ac, d)
1111138593Ssam{
1112148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 1, getac(ac), NULL);
1113138593Ssam}
1114148621Ssamstatic
1115148621SsamDECL_CMD_FUNC(set80211noackpolicy, ac, d)
1116148621Ssam{
1117148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 0, getac(ac), NULL);
1118148621Ssam}
1119138593Ssam
1120138593Ssamstatic
1121138593SsamDECL_CMD_FUNC2(set80211bsscwmin, ac, val)
1122138593Ssam{
1123138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val),
1124138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1125138593Ssam}
1126138593Ssam
1127138593Ssamstatic
1128138593SsamDECL_CMD_FUNC2(set80211bsscwmax, ac, val)
1129138593Ssam{
1130138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val),
1131138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1132138593Ssam}
1133138593Ssam
1134138593Ssamstatic
1135138593SsamDECL_CMD_FUNC2(set80211bssaifs, ac, val)
1136138593Ssam{
1137138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val),
1138138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1139138593Ssam}
1140138593Ssam
1141138593Ssamstatic
1142138593SsamDECL_CMD_FUNC2(set80211bsstxoplimit, ac, val)
1143138593Ssam{
1144138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val),
1145138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1146138593Ssam}
1147138593Ssam
1148138593Ssamstatic
1149138593SsamDECL_CMD_FUNC(set80211dtimperiod, val, d)
1150138593Ssam{
1151138593Ssam	set80211(s, IEEE80211_IOC_DTIM_PERIOD, atoi(val), 0, NULL);
1152138593Ssam}
1153138593Ssam
1154138593Ssamstatic
1155138593SsamDECL_CMD_FUNC(set80211bintval, val, d)
1156138593Ssam{
1157138593Ssam	set80211(s, IEEE80211_IOC_BEACON_INTERVAL, atoi(val), 0, NULL);
1158138593Ssam}
1159138593Ssam
1160138593Ssamstatic void
1161138593Ssamset80211macmac(int s, int op, const char *val)
1162138593Ssam{
1163138593Ssam	char *temp;
1164138593Ssam	struct sockaddr_dl sdl;
1165138593Ssam
1166155702Ssam	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1167138593Ssam	if (temp == NULL)
1168138593Ssam		errx(1, "malloc failed");
1169138593Ssam	temp[0] = ':';
1170138593Ssam	strcpy(temp + 1, val);
1171138593Ssam	sdl.sdl_len = sizeof(sdl);
1172138593Ssam	link_addr(temp, &sdl);
1173138593Ssam	free(temp);
1174138593Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1175138593Ssam		errx(1, "malformed link-level address");
1176138593Ssam	set80211(s, op, 0, IEEE80211_ADDR_LEN, LLADDR(&sdl));
1177138593Ssam}
1178138593Ssam
1179138593Ssamstatic
1180138593SsamDECL_CMD_FUNC(set80211addmac, val, d)
1181138593Ssam{
1182138593Ssam	set80211macmac(s, IEEE80211_IOC_ADDMAC, val);
1183138593Ssam}
1184138593Ssam
1185138593Ssamstatic
1186138593SsamDECL_CMD_FUNC(set80211delmac, val, d)
1187138593Ssam{
1188138593Ssam	set80211macmac(s, IEEE80211_IOC_DELMAC, val);
1189138593Ssam}
1190138593Ssam
1191138593Ssamstatic
1192149029SsamDECL_CMD_FUNC(set80211kickmac, val, d)
1193149029Ssam{
1194149029Ssam	char *temp;
1195149029Ssam	struct sockaddr_dl sdl;
1196149029Ssam	struct ieee80211req_mlme mlme;
1197149029Ssam
1198155702Ssam	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1199149029Ssam	if (temp == NULL)
1200149029Ssam		errx(1, "malloc failed");
1201149029Ssam	temp[0] = ':';
1202149029Ssam	strcpy(temp + 1, val);
1203149029Ssam	sdl.sdl_len = sizeof(sdl);
1204149029Ssam	link_addr(temp, &sdl);
1205149029Ssam	free(temp);
1206149029Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1207149029Ssam		errx(1, "malformed link-level address");
1208149029Ssam	memset(&mlme, 0, sizeof(mlme));
1209149029Ssam	mlme.im_op = IEEE80211_MLME_DEAUTH;
1210149029Ssam	mlme.im_reason = IEEE80211_REASON_AUTH_EXPIRE;
1211149029Ssam	memcpy(mlme.im_macaddr, LLADDR(&sdl), IEEE80211_ADDR_LEN);
1212170531Ssam	set80211(s, IEEE80211_IOC_MLME, 0, sizeof(mlme), &mlme);
1213149029Ssam}
1214149029Ssam
1215149029Ssamstatic
1216138593SsamDECL_CMD_FUNC(set80211maccmd, val, d)
1217138593Ssam{
1218138593Ssam	set80211(s, IEEE80211_IOC_MACCMD, d, 0, NULL);
1219138593Ssam}
1220138593Ssam
1221147795Ssamstatic void
1222147795Ssamset80211pureg(const char *val, int d, int s, const struct afswtch *rafp)
1223147795Ssam{
1224147795Ssam	set80211(s, IEEE80211_IOC_PUREG, d, 0, NULL);
1225147795Ssam}
1226147795Ssam
1227153422Ssamstatic void
1228170531Ssamset80211bgscan(const char *val, int d, int s, const struct afswtch *rafp)
1229153422Ssam{
1230170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN, d, 0, NULL);
1231153422Ssam}
1232153422Ssam
1233148416Ssamstatic
1234170531SsamDECL_CMD_FUNC(set80211bgscanidle, val, d)
1235170531Ssam{
1236170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN_IDLE, atoi(val), 0, NULL);
1237170531Ssam}
1238170531Ssam
1239170531Ssamstatic
1240170531SsamDECL_CMD_FUNC(set80211bgscanintvl, val, d)
1241170531Ssam{
1242170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN_INTERVAL, atoi(val), 0, NULL);
1243170531Ssam}
1244170531Ssam
1245170531Ssamstatic
1246170531SsamDECL_CMD_FUNC(set80211scanvalid, val, d)
1247170531Ssam{
1248170531Ssam	set80211(s, IEEE80211_IOC_SCANVALID, atoi(val), 0, NULL);
1249170531Ssam}
1250170531Ssam
1251178354Ssam/*
1252178354Ssam * Parse an optional trailing specification of which netbands
1253178354Ssam * to apply a parameter to.  This is basically the same syntax
1254178354Ssam * as used for channels but you can concatenate to specify
1255178354Ssam * multiple.  For example:
1256178354Ssam *	14:abg		apply to 11a, 11b, and 11g
1257178354Ssam *	6:ht		apply to 11na and 11ng
1258178354Ssam * We don't make a big effort to catch silly things; this is
1259178354Ssam * really a convenience mechanism.
1260178354Ssam */
1261178354Ssamstatic int
1262178354Ssamgetmodeflags(const char *val)
1263170531Ssam{
1264178354Ssam	const char *cp;
1265178354Ssam	int flags;
1266178354Ssam
1267178354Ssam	flags = 0;
1268178354Ssam
1269178354Ssam	cp = strchr(val, ':');
1270178354Ssam	if (cp != NULL) {
1271178354Ssam		for (cp++; isalpha((int) *cp); cp++) {
1272178354Ssam			/* accept mixed case */
1273178354Ssam			int c = *cp;
1274178354Ssam			if (isupper(c))
1275178354Ssam				c = tolower(c);
1276178354Ssam			switch (c) {
1277178354Ssam			case 'a':		/* 802.11a */
1278178354Ssam				flags |= IEEE80211_CHAN_A;
1279178354Ssam				break;
1280178354Ssam			case 'b':		/* 802.11b */
1281178354Ssam				flags |= IEEE80211_CHAN_B;
1282178354Ssam				break;
1283178354Ssam			case 'g':		/* 802.11g */
1284178354Ssam				flags |= IEEE80211_CHAN_G;
1285178354Ssam				break;
1286178354Ssam			case 'h':		/* ht = 802.11n */
1287178354Ssam			case 'n':		/* 802.11n */
1288178354Ssam				flags |= IEEE80211_CHAN_HT;
1289178354Ssam				break;
1290178354Ssam			case 'd':		/* dt = Atheros Dynamic Turbo */
1291178354Ssam				flags |= IEEE80211_CHAN_TURBO;
1292178354Ssam				break;
1293178354Ssam			case 't':		/* ht, dt, st, t */
1294178354Ssam				/* dt and unadorned t specify Dynamic Turbo */
1295178354Ssam				if ((flags & (IEEE80211_CHAN_STURBO|IEEE80211_CHAN_HT)) == 0)
1296178354Ssam					flags |= IEEE80211_CHAN_TURBO;
1297178354Ssam				break;
1298178354Ssam			case 's':		/* st = Atheros Static Turbo */
1299178354Ssam				flags |= IEEE80211_CHAN_STURBO;
1300178354Ssam				break;
1301178354Ssam			default:
1302178354Ssam				errx(-1, "%s: Invalid mode attribute %c\n",
1303178354Ssam				    val, *cp);
1304178354Ssam			}
1305178354Ssam		}
1306178354Ssam	}
1307178354Ssam	return flags;
1308170531Ssam}
1309170531Ssam
1310178354Ssam#define	IEEE80211_CHAN_HTA	(IEEE80211_CHAN_HT|IEEE80211_CHAN_5GHZ)
1311178354Ssam#define	IEEE80211_CHAN_HTG	(IEEE80211_CHAN_HT|IEEE80211_CHAN_2GHZ)
1312178354Ssam
1313178354Ssam#define	_APPLY(_flags, _base, _param, _v) do {				\
1314178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1315178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1316178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1317178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1318178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1319178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1320178354Ssam	    else							\
1321178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1322178354Ssam    }									\
1323178354Ssam    if (_flags & IEEE80211_CHAN_TURBO) {				\
1324178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1325178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;	\
1326178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;	\
1327178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1328178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;	\
1329178354Ssam	    else							\
1330178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;	\
1331178354Ssam    }									\
1332178354Ssam    if (_flags & IEEE80211_CHAN_STURBO)					\
1333178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = _v;		\
1334178354Ssam    if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1335178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = _v;		\
1336178354Ssam    if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1337178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = _v;		\
1338178354Ssam    if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1339178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = _v;		\
1340178354Ssam} while (0)
1341178354Ssam#define	_APPLY1(_flags, _base, _param, _v) do {				\
1342178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1343178354Ssam	    if (_flags & IEEE80211_CHAN_5GHZ)				\
1344178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1345178354Ssam	    else							\
1346178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1347178354Ssam    } else if ((_flags & IEEE80211_CHAN_108A) == IEEE80211_CHAN_108A)	\
1348178354Ssam	    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;		\
1349178354Ssam    else if ((_flags & IEEE80211_CHAN_108G) == IEEE80211_CHAN_108G)	\
1350178354Ssam	    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;		\
1351178354Ssam    else if ((_flags & IEEE80211_CHAN_ST) == IEEE80211_CHAN_ST)		\
1352178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = _v;		\
1353178354Ssam    else if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1354178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = _v;		\
1355178354Ssam    else if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1356178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = _v;		\
1357178354Ssam    else if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1358178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = _v;		\
1359178354Ssam} while (0)
1360178354Ssam#define	_APPLY_RATE(_flags, _base, _param, _v) do {			\
1361178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1362178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1363178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v|0x80;	\
1364178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v|0x80;	\
1365178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1366178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v|0x80;	\
1367178354Ssam	    else							\
1368178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v|0x80;	\
1369178354Ssam    }									\
1370178354Ssam    if (_flags & IEEE80211_CHAN_TURBO) {				\
1371178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1372178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = 2*_v;	\
1373178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = 2*_v;	\
1374178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1375178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = 2*_v;	\
1376178354Ssam	    else							\
1377178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = 2*_v;	\
1378178354Ssam    }									\
1379178354Ssam    if (_flags & IEEE80211_CHAN_STURBO)					\
1380178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = 2*_v;	\
1381178354Ssam    if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1382178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = 2*_v;		\
1383178354Ssam    if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1384178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = (_v == 5 ? 11 : 2*_v);\
1385178354Ssam    if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1386178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = (_v == 5 ? 11 : 2*_v);\
1387178354Ssam} while (0)
1388178354Ssam#define	_APPLY_RATE1(_flags, _base, _param, _v) do {			\
1389178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1390178354Ssam	    if (_flags & IEEE80211_CHAN_5GHZ)				\
1391178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v|0x80;	\
1392178354Ssam	    else							\
1393178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v|0x80;	\
1394178354Ssam    } else if ((_flags & IEEE80211_CHAN_108A) == IEEE80211_CHAN_108A)	\
1395178354Ssam	    _base.params[IEEE80211_MODE_TURBO_A]._param = 2*_v;		\
1396178354Ssam    else if ((_flags & IEEE80211_CHAN_108G) == IEEE80211_CHAN_108G)	\
1397178354Ssam	    _base.params[IEEE80211_MODE_TURBO_G]._param = 2*_v;		\
1398178354Ssam    else if ((_flags & IEEE80211_CHAN_ST) == IEEE80211_CHAN_ST)		\
1399178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = 2*_v;	\
1400178354Ssam    else if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1401178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = 2*_v;		\
1402178354Ssam    else if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1403178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = (_v == 5 ? 11 : 2*_v);\
1404178354Ssam    else if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1405178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = (_v == 5 ? 11 : 2*_v);\
1406178354Ssam} while (0)
1407178354Ssam
1408170531Ssamstatic
1409178354SsamDECL_CMD_FUNC(set80211roamrssi, val, d)
1410170531Ssam{
1411178354Ssam	double v = atof(val);
1412178354Ssam	int rssi, flags;
1413178354Ssam
1414178354Ssam	rssi = (int) (2*v);
1415178354Ssam	if (rssi != 2*v)
1416178354Ssam		errx(-1, "invalid rssi (must be .5 dBm units)");
1417178354Ssam	flags = getmodeflags(val);
1418178354Ssam	getroam(s);
1419178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1420178354Ssam		flags = getcurchan(s)->ic_flags;
1421178354Ssam		_APPLY1(flags, roamparams, rssi, rssi);
1422178354Ssam	} else
1423178354Ssam		_APPLY(flags, roamparams, rssi, rssi);
1424178354Ssam	callback_register(setroam_cb, &roamparams);
1425170531Ssam}
1426170531Ssam
1427170531Ssamstatic
1428178354SsamDECL_CMD_FUNC(set80211roamrate, val, d)
1429170531Ssam{
1430178354Ssam	int v = atoi(val), flags;
1431178354Ssam
1432178354Ssam	flags = getmodeflags(val);
1433178354Ssam	getroam(s);
1434178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1435178354Ssam		flags = getcurchan(s)->ic_flags;
1436178354Ssam		_APPLY_RATE1(flags, roamparams, rate, v);
1437178354Ssam	} else
1438178354Ssam		_APPLY_RATE(flags, roamparams, rate, v);
1439178354Ssam	callback_register(setroam_cb, &roamparams);
1440170531Ssam}
1441170531Ssam
1442170531Ssamstatic
1443178354SsamDECL_CMD_FUNC(set80211mcastrate, val, d)
1444170531Ssam{
1445178354Ssam	int v = atoi(val), flags;
1446178354Ssam
1447178354Ssam	flags = getmodeflags(val);
1448178354Ssam	gettxparams(s);
1449178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1450178354Ssam		flags = getcurchan(s)->ic_flags;
1451178354Ssam		_APPLY_RATE1(flags, txparams, mcastrate, v);
1452178354Ssam	} else
1453178354Ssam		_APPLY_RATE(flags, txparams, mcastrate, v);
1454178354Ssam	callback_register(settxparams_cb, &txparams);
1455170531Ssam}
1456170531Ssam
1457170531Ssamstatic
1458178354SsamDECL_CMD_FUNC(set80211mgtrate, val, d)
1459170531Ssam{
1460178354Ssam	int v = atoi(val), flags;
1461178354Ssam
1462178354Ssam	flags = getmodeflags(val);
1463178354Ssam	gettxparams(s);
1464178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1465178354Ssam		flags = getcurchan(s)->ic_flags;
1466178354Ssam		_APPLY_RATE1(flags, txparams, mgmtrate, v);
1467178354Ssam	} else
1468178354Ssam		_APPLY_RATE(flags, txparams, mgmtrate, v);
1469178354Ssam	callback_register(settxparams_cb, &txparams);
1470170531Ssam}
1471170531Ssam
1472170531Ssamstatic
1473178354SsamDECL_CMD_FUNC(set80211ucastrate, val, d)
1474170531Ssam{
1475178354Ssam	int v, flags;
1476178354Ssam
1477178354Ssam	gettxparams(s);
1478178354Ssam	flags = getmodeflags(val);
1479178354Ssam	if (isanyarg(val)) {
1480178354Ssam		if (flags == 0) {	/* NB: no flags => current channel */
1481178354Ssam			flags = getcurchan(s)->ic_flags;
1482178354Ssam			_APPLY1(flags, txparams, ucastrate,
1483178354Ssam			    IEEE80211_FIXED_RATE_NONE);
1484178354Ssam		} else
1485178354Ssam			_APPLY(flags, txparams, ucastrate,
1486178354Ssam			    IEEE80211_FIXED_RATE_NONE);
1487178354Ssam	} else {
1488178354Ssam		v = atoi(val);
1489178354Ssam		if (flags == 0) {	/* NB: no flags => current channel */
1490178354Ssam			flags = getcurchan(s)->ic_flags;
1491178354Ssam			_APPLY_RATE1(flags, txparams, ucastrate, v);
1492178354Ssam		} else
1493178354Ssam			_APPLY_RATE(flags, txparams, ucastrate, v);
1494178354Ssam	}
1495178354Ssam	callback_register(settxparams_cb, &txparams);
1496170531Ssam}
1497170531Ssam
1498170531Ssamstatic
1499178354SsamDECL_CMD_FUNC(set80211maxretry, val, d)
1500153354Ssam{
1501178354Ssam	int v = atoi(val), flags;
1502178354Ssam
1503178354Ssam	flags = getmodeflags(val);
1504178354Ssam	gettxparams(s);
1505178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1506178354Ssam		flags = getcurchan(s)->ic_flags;
1507178354Ssam		_APPLY1(flags, txparams, maxretry, v);
1508178354Ssam	} else
1509178354Ssam		_APPLY(flags, txparams, maxretry, v);
1510178354Ssam	callback_register(settxparams_cb, &txparams);
1511153354Ssam}
1512178354Ssam#undef _APPLY_RATE
1513178354Ssam#undef _APPLY
1514178354Ssam#undef IEEE80211_CHAN_HTA
1515178354Ssam#undef IEEE80211_CHAN_HTG
1516153354Ssam
1517153354Ssamstatic
1518148416SsamDECL_CMD_FUNC(set80211fragthreshold, val, d)
1519148416Ssam{
1520148416Ssam	set80211(s, IEEE80211_IOC_FRAGTHRESHOLD,
1521148416Ssam		isundefarg(val) ? IEEE80211_FRAG_MAX : atoi(val), 0, NULL);
1522148416Ssam}
1523148416Ssam
1524160687Ssamstatic
1525160687SsamDECL_CMD_FUNC(set80211bmissthreshold, val, d)
1526160687Ssam{
1527160687Ssam	set80211(s, IEEE80211_IOC_BMISSTHRESHOLD,
1528160687Ssam		isundefarg(val) ? IEEE80211_HWBMISS_MAX : atoi(val), 0, NULL);
1529160687Ssam}
1530160687Ssam
1531170531Ssamstatic void
1532170531Ssamset80211burst(const char *val, int d, int s, const struct afswtch *rafp)
1533170531Ssam{
1534170531Ssam	set80211(s, IEEE80211_IOC_BURST, d, 0, NULL);
1535170531Ssam}
1536170531Ssam
1537170531Ssamstatic void
1538170531Ssamset80211doth(const char *val, int d, int s, const struct afswtch *rafp)
1539170531Ssam{
1540170531Ssam	set80211(s, IEEE80211_IOC_DOTH, d, 0, NULL);
1541170531Ssam}
1542170531Ssam
1543173275Ssamstatic void
1544178354Ssamset80211dfs(const char *val, int d, int s, const struct afswtch *rafp)
1545178354Ssam{
1546178354Ssam	set80211(s, IEEE80211_IOC_DFS, d, 0, NULL);
1547178354Ssam}
1548178354Ssam
1549178354Ssamstatic void
1550173275Ssamset80211shortgi(const char *val, int d, int s, const struct afswtch *rafp)
1551173275Ssam{
1552173275Ssam	set80211(s, IEEE80211_IOC_SHORTGI,
1553173275Ssam		d ? (IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40) : 0,
1554173275Ssam		0, NULL);
1555173275Ssam}
1556173275Ssam
1557173275Ssamstatic void
1558173275Ssamset80211ampdu(const char *val, int d, int s, const struct afswtch *rafp)
1559173275Ssam{
1560173275Ssam	int ampdu;
1561173275Ssam
1562173275Ssam	if (get80211val(s, IEEE80211_IOC_AMPDU, &ampdu) < 0)
1563173275Ssam		errx(-1, "cannot get AMPDU setting");
1564173275Ssam	if (d < 0) {
1565173275Ssam		d = -d;
1566173275Ssam		ampdu &= ~d;
1567173275Ssam	} else
1568173275Ssam		ampdu |= d;
1569173275Ssam	set80211(s, IEEE80211_IOC_AMPDU, ampdu, 0, NULL);
1570173275Ssam}
1571173275Ssam
1572173275Ssamstatic
1573173275SsamDECL_CMD_FUNC(set80211ampdulimit, val, d)
1574173275Ssam{
1575173275Ssam	int v;
1576173275Ssam
1577173275Ssam	switch (atoi(val)) {
1578173275Ssam	case 8:
1579173275Ssam	case 8*1024:
1580173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_8K;
1581173275Ssam		break;
1582173275Ssam	case 16:
1583173275Ssam	case 16*1024:
1584173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_16K;
1585173275Ssam		break;
1586173275Ssam	case 32:
1587173275Ssam	case 32*1024:
1588173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_32K;
1589173275Ssam		break;
1590173275Ssam	case 64:
1591173275Ssam	case 64*1024:
1592173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_64K;
1593173275Ssam		break;
1594173275Ssam	default:
1595173275Ssam		errx(-1, "invalid A-MPDU limit %s", val);
1596173275Ssam	}
1597173275Ssam	set80211(s, IEEE80211_IOC_AMPDU_LIMIT, v, 0, NULL);
1598173275Ssam}
1599173275Ssam
1600173275Ssamstatic
1601173275SsamDECL_CMD_FUNC(set80211ampdudensity, val, d)
1602173275Ssam{
1603173275Ssam	int v;
1604173275Ssam
1605183260Ssam	if (isanyarg(val) || strcasecmp(val, "na") == 0)
1606173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1607173275Ssam	else switch ((int)(atof(val)*4)) {
1608173275Ssam	case 0:
1609173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1610173275Ssam		break;
1611173275Ssam	case 1:
1612173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_025;
1613173275Ssam		break;
1614173275Ssam	case 2:
1615173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_05;
1616173275Ssam		break;
1617173275Ssam	case 4:
1618173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_1;
1619173275Ssam		break;
1620173275Ssam	case 8:
1621173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_2;
1622173275Ssam		break;
1623173275Ssam	case 16:
1624173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_4;
1625173275Ssam		break;
1626173275Ssam	case 32:
1627173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_8;
1628173275Ssam		break;
1629173275Ssam	case 64:
1630173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_16;
1631173275Ssam		break;
1632173275Ssam	default:
1633173275Ssam		errx(-1, "invalid A-MPDU density %s", val);
1634173275Ssam	}
1635173275Ssam	set80211(s, IEEE80211_IOC_AMPDU_DENSITY, v, 0, NULL);
1636173275Ssam}
1637173275Ssam
1638173275Ssamstatic void
1639173275Ssamset80211amsdu(const char *val, int d, int s, const struct afswtch *rafp)
1640173275Ssam{
1641173275Ssam	int amsdu;
1642173275Ssam
1643173275Ssam	if (get80211val(s, IEEE80211_IOC_AMSDU, &amsdu) < 0)
1644173275Ssam		errx(-1, "cannot get AMSDU setting");
1645173275Ssam	if (d < 0) {
1646173275Ssam		d = -d;
1647173275Ssam		amsdu &= ~d;
1648173275Ssam	} else
1649173275Ssam		amsdu |= d;
1650173275Ssam	set80211(s, IEEE80211_IOC_AMSDU, amsdu, 0, NULL);
1651173275Ssam}
1652173275Ssam
1653173275Ssamstatic
1654173275SsamDECL_CMD_FUNC(set80211amsdulimit, val, d)
1655173275Ssam{
1656173275Ssam	set80211(s, IEEE80211_IOC_AMSDU_LIMIT, atoi(val), 0, NULL);
1657173275Ssam}
1658173275Ssam
1659173275Ssamstatic void
1660173275Ssamset80211puren(const char *val, int d, int s, const struct afswtch *rafp)
1661173275Ssam{
1662173275Ssam	set80211(s, IEEE80211_IOC_PUREN, d, 0, NULL);
1663173275Ssam}
1664173275Ssam
1665173275Ssamstatic void
1666173275Ssamset80211htcompat(const char *val, int d, int s, const struct afswtch *rafp)
1667173275Ssam{
1668173275Ssam	set80211(s, IEEE80211_IOC_HTCOMPAT, d, 0, NULL);
1669173275Ssam}
1670173275Ssam
1671173275Ssamstatic void
1672173275Ssamset80211htconf(const char *val, int d, int s, const struct afswtch *rafp)
1673173275Ssam{
1674173275Ssam	set80211(s, IEEE80211_IOC_HTCONF, d, 0, NULL);
1675173275Ssam	htconf = d;
1676173275Ssam}
1677173275Ssam
1678173275Ssamstatic void
1679178354Ssamset80211dwds(const char *val, int d, int s, const struct afswtch *rafp)
1680178354Ssam{
1681178354Ssam	set80211(s, IEEE80211_IOC_DWDS, d, 0, NULL);
1682178354Ssam}
1683178354Ssam
1684178354Ssamstatic void
1685173275Ssamset80211inact(const char *val, int d, int s, const struct afswtch *rafp)
1686173275Ssam{
1687173275Ssam	set80211(s, IEEE80211_IOC_INACTIVITY, d, 0, NULL);
1688173275Ssam}
1689173275Ssam
1690173275Ssamstatic void
1691178354Ssamset80211tsn(const char *val, int d, int s, const struct afswtch *rafp)
1692178354Ssam{
1693178354Ssam	set80211(s, IEEE80211_IOC_TSN, d, 0, NULL);
1694178354Ssam}
1695178354Ssam
1696178354Ssamstatic void
1697178354Ssamset80211dotd(const char *val, int d, int s, const struct afswtch *rafp)
1698178354Ssam{
1699178354Ssam	set80211(s, IEEE80211_IOC_DOTD, d, 0, NULL);
1700178354Ssam}
1701178354Ssam
1702183261Ssamstatic void
1703183261Ssamset80211smps(const char *val, int d, int s, const struct afswtch *rafp)
1704183261Ssam{
1705183261Ssam	set80211(s, IEEE80211_IOC_SMPS, d, 0, NULL);
1706183261Ssam}
1707183261Ssam
1708183261Ssamstatic void
1709183261Ssamset80211rifs(const char *val, int d, int s, const struct afswtch *rafp)
1710183261Ssam{
1711183261Ssam	set80211(s, IEEE80211_IOC_RIFS, d, 0, NULL);
1712183261Ssam}
1713183261Ssam
1714186904Ssamstatic
1715186904SsamDECL_CMD_FUNC(set80211tdmaslot, val, d)
1716186904Ssam{
1717186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOT, atoi(val), 0, NULL);
1718186904Ssam}
1719186904Ssam
1720186904Ssamstatic
1721186904SsamDECL_CMD_FUNC(set80211tdmaslotcnt, val, d)
1722186904Ssam{
1723186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOTCNT, atoi(val), 0, NULL);
1724186904Ssam}
1725186904Ssam
1726186904Ssamstatic
1727186904SsamDECL_CMD_FUNC(set80211tdmaslotlen, val, d)
1728186904Ssam{
1729186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOTLEN, atoi(val), 0, NULL);
1730186904Ssam}
1731186904Ssam
1732186904Ssamstatic
1733186904SsamDECL_CMD_FUNC(set80211tdmabintval, val, d)
1734186904Ssam{
1735186904Ssam	set80211(s, IEEE80211_IOC_TDMA_BINTERVAL, atoi(val), 0, NULL);
1736186904Ssam}
1737186904Ssam
1738178354Ssamstatic int
1739178354Ssamregdomain_sort(const void *a, const void *b)
1740178354Ssam{
1741178354Ssam#define	CHAN_ALL \
1742178354Ssam	(IEEE80211_CHAN_ALLTURBO|IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)
1743178354Ssam	const struct ieee80211_channel *ca = a;
1744178354Ssam	const struct ieee80211_channel *cb = b;
1745178354Ssam
1746178354Ssam	return ca->ic_freq == cb->ic_freq ?
1747178354Ssam	    (ca->ic_flags & CHAN_ALL) - (cb->ic_flags & CHAN_ALL) :
1748178354Ssam	    ca->ic_freq - cb->ic_freq;
1749178354Ssam#undef CHAN_ALL
1750178354Ssam}
1751178354Ssam
1752178354Ssamstatic const struct ieee80211_channel *
1753178354Ssamchanlookup(const struct ieee80211_channel chans[], int nchans,
1754178354Ssam	int freq, int flags)
1755178354Ssam{
1756178354Ssam	int i;
1757178354Ssam
1758178354Ssam	flags &= IEEE80211_CHAN_ALLTURBO;
1759178354Ssam	for (i = 0; i < nchans; i++) {
1760178354Ssam		const struct ieee80211_channel *c = &chans[i];
1761178354Ssam		if (c->ic_freq == freq &&
1762178354Ssam		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1763178354Ssam			return c;
1764178354Ssam	}
1765178354Ssam	return NULL;
1766178354Ssam}
1767178354Ssam
1768178354Ssamstatic void
1769178354Ssamregdomain_addchans(struct ieee80211req_chaninfo *ci,
1770178354Ssam	const netband_head *bands,
1771178354Ssam	const struct ieee80211_regdomain *reg,
1772178354Ssam	uint32_t chanFlags,
1773178354Ssam	const struct ieee80211req_chaninfo *avail)
1774178354Ssam{
1775178354Ssam	const struct netband *nb;
1776178354Ssam	const struct freqband *b;
1777178354Ssam	struct ieee80211_channel *c, *prev;
1778178354Ssam	int freq, channelSep;
1779178354Ssam
1780178354Ssam	channelSep = (chanFlags & IEEE80211_CHAN_2GHZ) ? 0 : 40;
1781178354Ssam	LIST_FOREACH(nb, bands, next) {
1782178354Ssam		b = nb->band;
1783178354Ssam		if (verbose)
1784178354Ssam			printf("%s: chanFlags 0x%x b %p\n",
1785178354Ssam			    __func__, chanFlags, b);
1786178354Ssam		prev = NULL;
1787178354Ssam		for (freq = b->freqStart; freq <= b->freqEnd; freq += b->chanSep) {
1788178354Ssam			uint32_t flags = nb->flags | b->flags;
1789178354Ssam
1790178354Ssam			/* check if device can operate on this frequency */
1791178354Ssam			if (chanlookup(avail->ic_chans, avail->ic_nchans, freq, chanFlags) == NULL) {
1792178354Ssam				if (verbose)
1793178354Ssam					printf("%u: skip, flags 0x%x not available\n", freq, chanFlags);
1794178354Ssam				continue;
1795178354Ssam			}
1796186101Ssam			/*
1797186101Ssam			 * NB: don't enforce 1/2 and 1/4 rate channels being
1798186101Ssam			 * specified in the device's calibration list for
1799186101Ssam			 * 900MHz cards because most are not self-identifying.
1800186101Ssam			 */
1801178354Ssam			if ((flags & IEEE80211_CHAN_HALF) &&
1802186101Ssam			    ((chanFlags & IEEE80211_CHAN_HALF) == 0 &&
1803186101Ssam			     (flags & IEEE80211_CHAN_GSM) == 0)) {
1804178354Ssam				if (verbose)
1805178354Ssam					printf("%u: skip, device does not support half-rate channels\n", freq);
1806178354Ssam				continue;
1807178354Ssam			}
1808178354Ssam			if ((flags & IEEE80211_CHAN_QUARTER) &&
1809186101Ssam			    ((chanFlags & IEEE80211_CHAN_HALF) == 0 &&
1810186101Ssam			     (flags & IEEE80211_CHAN_GSM) == 0)) {
1811178354Ssam				if (verbose)
1812178354Ssam					printf("%u: skip, device does not support quarter-rate channels\n", freq);
1813178354Ssam				continue;
1814178354Ssam			}
1815178354Ssam			if ((flags & IEEE80211_CHAN_HT20) &&
1816178354Ssam			    (chanFlags & IEEE80211_CHAN_HT20) == 0) {
1817178354Ssam				if (verbose)
1818178354Ssam					printf("%u: skip, device does not support HT20 operation\n", freq);
1819178354Ssam				continue;
1820178354Ssam			}
1821178354Ssam			if ((flags & IEEE80211_CHAN_HT40) &&
1822178354Ssam			    (chanFlags & IEEE80211_CHAN_HT40) == 0) {
1823178354Ssam				if (verbose)
1824178354Ssam					printf("%u: skip, device does not support HT40 operation\n", freq);
1825178354Ssam				continue;
1826178354Ssam			}
1827178354Ssam			if ((flags & REQ_ECM) && !reg->ecm) {
1828178354Ssam				if (verbose)
1829178354Ssam					printf("%u: skip, ECM channel\n", freq);
1830178354Ssam				continue;
1831178354Ssam			}
1832178354Ssam			if ((flags & REQ_OUTDOOR) && reg->location == 'I') {
1833178354Ssam				if (verbose)
1834178354Ssam					printf("%u: skip, outdoor channel\n", freq);
1835178354Ssam				continue;
1836178354Ssam			}
1837178354Ssam			if ((flags & IEEE80211_CHAN_HT40) &&
1838178354Ssam			    prev != NULL && (freq - prev->ic_freq) < channelSep) {
1839178354Ssam				if (verbose)
1840178354Ssam					printf("%u: skip, only %u channel "
1841178354Ssam					    "separation, need %d\n", freq,
1842178354Ssam					    freq - prev->ic_freq, channelSep);
1843178354Ssam				continue;
1844178354Ssam			}
1845178354Ssam			if (ci->ic_nchans == IEEE80211_CHAN_MAX) {
1846178354Ssam				if (verbose)
1847178354Ssam					printf("%u: skip, channel table full\n", freq);
1848178354Ssam				break;
1849178354Ssam			}
1850178354Ssam			c = &ci->ic_chans[ci->ic_nchans++];
1851178354Ssam			c->ic_freq = freq;
1852178354Ssam			c->ic_flags = chanFlags |
1853178354Ssam			    (flags &~ (REQ_FLAGS | IEEE80211_CHAN_HT40));
1854178354Ssam			if (c->ic_flags & IEEE80211_CHAN_DFS)
1855178354Ssam				c->ic_maxregpower = nb->maxPowerDFS;
1856178354Ssam			else
1857178354Ssam				c->ic_maxregpower = nb->maxPower;
1858178354Ssam			if (verbose)
1859178354Ssam				printf("[%3d] add freq %u flags 0x%x power %u\n",
1860178354Ssam				    ci->ic_nchans-1, c->ic_freq, c->ic_flags,
1861178354Ssam				    c->ic_maxregpower);
1862178354Ssam			/* NB: kernel fills in other fields */
1863178354Ssam			prev = c;
1864178354Ssam		}
1865178354Ssam	}
1866178354Ssam}
1867178354Ssam
1868178354Ssamstatic void
1869178354Ssamregdomain_makechannels(
1870178354Ssam	struct ieee80211_regdomain_req *req,
1871178354Ssam	const struct ieee80211_devcaps_req *dc)
1872178354Ssam{
1873178354Ssam	struct regdata *rdp = getregdata();
1874178354Ssam	const struct country *cc;
1875178354Ssam	const struct ieee80211_regdomain *reg = &req->rd;
1876178354Ssam	struct ieee80211req_chaninfo *ci = &req->chaninfo;
1877178354Ssam	const struct regdomain *rd;
1878178354Ssam
1879178354Ssam	/*
1880178354Ssam	 * Locate construction table for new channel list.  We treat
1881178354Ssam	 * the regdomain/SKU as definitive so a country can be in
1882178354Ssam	 * multiple with different properties (e.g. US in FCC+FCC3).
1883178354Ssam	 * If no regdomain is specified then we fallback on the country
1884178354Ssam	 * code to find the associated regdomain since countries always
1885178354Ssam	 * belong to at least one regdomain.
1886178354Ssam	 */
1887178354Ssam	if (reg->regdomain == 0) {
1888178354Ssam		cc = lib80211_country_findbycc(rdp, reg->country);
1889178354Ssam		if (cc == NULL)
1890178354Ssam			errx(1, "internal error, country %d not found",
1891178354Ssam			    reg->country);
1892178354Ssam		rd = cc->rd;
1893178354Ssam	} else
1894178354Ssam		rd = lib80211_regdomain_findbysku(rdp, reg->regdomain);
1895178354Ssam	if (rd == NULL)
1896178354Ssam		errx(1, "internal error, regdomain %d not found",
1897178354Ssam			    reg->regdomain);
1898178354Ssam	if (rd->sku != SKU_DEBUG) {
1899178354Ssam		memset(ci, 0, sizeof(*ci));
1900178354Ssam		if (!LIST_EMPTY(&rd->bands_11b))
1901178354Ssam			regdomain_addchans(ci, &rd->bands_11b, reg,
1902178354Ssam			    IEEE80211_CHAN_B, &dc->dc_chaninfo);
1903178354Ssam		if (!LIST_EMPTY(&rd->bands_11g))
1904178354Ssam			regdomain_addchans(ci, &rd->bands_11g, reg,
1905178354Ssam			    IEEE80211_CHAN_G, &dc->dc_chaninfo);
1906178354Ssam		if (!LIST_EMPTY(&rd->bands_11a))
1907178354Ssam			regdomain_addchans(ci, &rd->bands_11a, reg,
1908178354Ssam			    IEEE80211_CHAN_A, &dc->dc_chaninfo);
1909178354Ssam		if (!LIST_EMPTY(&rd->bands_11na)) {
1910178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
1911178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT20,
1912178354Ssam			    &dc->dc_chaninfo);
1913178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
1914178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U,
1915178354Ssam			    &dc->dc_chaninfo);
1916178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
1917178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D,
1918178354Ssam			    &dc->dc_chaninfo);
1919178354Ssam		}
1920178354Ssam		if (!LIST_EMPTY(&rd->bands_11ng)) {
1921178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
1922178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT20,
1923178354Ssam			    &dc->dc_chaninfo);
1924178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
1925178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U,
1926178354Ssam			    &dc->dc_chaninfo);
1927178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
1928178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D,
1929178354Ssam			    &dc->dc_chaninfo);
1930178354Ssam		}
1931178354Ssam		qsort(ci->ic_chans, ci->ic_nchans, sizeof(ci->ic_chans[0]),
1932178354Ssam		    regdomain_sort);
1933178354Ssam	} else
1934178354Ssam		*ci = dc->dc_chaninfo;
1935178354Ssam}
1936178354Ssam
1937178354Ssamstatic void
1938178354Ssamlist_countries(void)
1939178354Ssam{
1940178354Ssam	struct regdata *rdp = getregdata();
1941178354Ssam	const struct country *cp;
1942178354Ssam	const struct regdomain *dp;
1943178354Ssam	int i;
1944178354Ssam
1945178354Ssam	i = 0;
1946178354Ssam	printf("\nCountry codes:\n");
1947178354Ssam	LIST_FOREACH(cp, &rdp->countries, next) {
1948178354Ssam		printf("%2s %-15.15s%s", cp->isoname,
1949178354Ssam		    cp->name, ((i+1)%4) == 0 ? "\n" : " ");
1950178354Ssam		i++;
1951178354Ssam	}
1952178354Ssam	i = 0;
1953178354Ssam	printf("\nRegulatory domains:\n");
1954178354Ssam	LIST_FOREACH(dp, &rdp->domains, next) {
1955178354Ssam		printf("%-15.15s%s", dp->name, ((i+1)%4) == 0 ? "\n" : " ");
1956178354Ssam		i++;
1957178354Ssam	}
1958178354Ssam	printf("\n");
1959178354Ssam}
1960178354Ssam
1961178354Ssamstatic void
1962178354Ssamdefaultcountry(const struct regdomain *rd)
1963178354Ssam{
1964178354Ssam	struct regdata *rdp = getregdata();
1965178354Ssam	const struct country *cc;
1966178354Ssam
1967178354Ssam	cc = lib80211_country_findbycc(rdp, rd->cc->code);
1968178354Ssam	if (cc == NULL)
1969178354Ssam		errx(1, "internal error, ISO country code %d not "
1970178354Ssam		    "defined for regdomain %s", rd->cc->code, rd->name);
1971178354Ssam	regdomain.country = cc->code;
1972178354Ssam	regdomain.isocc[0] = cc->isoname[0];
1973178354Ssam	regdomain.isocc[1] = cc->isoname[1];
1974178354Ssam}
1975178354Ssam
1976178354Ssamstatic
1977178354SsamDECL_CMD_FUNC(set80211regdomain, val, d)
1978178354Ssam{
1979178354Ssam	struct regdata *rdp = getregdata();
1980178354Ssam	const struct regdomain *rd;
1981178354Ssam
1982178354Ssam	rd = lib80211_regdomain_findbyname(rdp, val);
1983178354Ssam	if (rd == NULL) {
1984186104Ssam		char *eptr;
1985186104Ssam		long sku = strtol(val, &eptr, 0);
1986186104Ssam
1987186104Ssam		if (eptr != val)
1988186104Ssam			rd = lib80211_regdomain_findbysku(rdp, sku);
1989186104Ssam		if (eptr == val || rd == NULL)
1990178354Ssam			errx(1, "unknown regdomain %s", val);
1991178354Ssam	}
1992178354Ssam	getregdomain(s);
1993178354Ssam	regdomain.regdomain = rd->sku;
1994178354Ssam	if (regdomain.country == 0 && rd->cc != NULL) {
1995178354Ssam		/*
1996178354Ssam		 * No country code setup and there's a default
1997178354Ssam		 * one for this regdomain fill it in.
1998178354Ssam		 */
1999178354Ssam		defaultcountry(rd);
2000178354Ssam	}
2001178354Ssam	callback_register(setregdomain_cb, &regdomain);
2002178354Ssam}
2003178354Ssam
2004178354Ssamstatic
2005178354SsamDECL_CMD_FUNC(set80211country, val, d)
2006178354Ssam{
2007178354Ssam	struct regdata *rdp = getregdata();
2008178354Ssam	const struct country *cc;
2009178354Ssam
2010178354Ssam	cc = lib80211_country_findbyname(rdp, val);
2011178354Ssam	if (cc == NULL) {
2012186103Ssam		char *eptr;
2013186103Ssam		long code = strtol(val, &eptr, 0);
2014186103Ssam
2015186103Ssam		if (eptr != val)
2016186103Ssam			cc = lib80211_country_findbycc(rdp, code);
2017186103Ssam		if (eptr == val || cc == NULL)
2018178354Ssam			errx(1, "unknown ISO country code %s", val);
2019178354Ssam	}
2020178354Ssam	getregdomain(s);
2021178354Ssam	regdomain.regdomain = cc->rd->sku;
2022178354Ssam	regdomain.country = cc->code;
2023178354Ssam	regdomain.isocc[0] = cc->isoname[0];
2024178354Ssam	regdomain.isocc[1] = cc->isoname[1];
2025178354Ssam	callback_register(setregdomain_cb, &regdomain);
2026178354Ssam}
2027178354Ssam
2028178354Ssamstatic void
2029178354Ssamset80211location(const char *val, int d, int s, const struct afswtch *rafp)
2030178354Ssam{
2031178354Ssam	getregdomain(s);
2032178354Ssam	regdomain.location = d;
2033178354Ssam	callback_register(setregdomain_cb, &regdomain);
2034178354Ssam}
2035178354Ssam
2036178354Ssamstatic void
2037178354Ssamset80211ecm(const char *val, int d, int s, const struct afswtch *rafp)
2038178354Ssam{
2039178354Ssam	getregdomain(s);
2040178354Ssam	regdomain.ecm = d;
2041178354Ssam	callback_register(setregdomain_cb, &regdomain);
2042178354Ssam}
2043178354Ssam
2044178354Ssamstatic void
2045173275SsamLINE_INIT(char c)
2046173275Ssam{
2047173275Ssam	spacer = c;
2048173275Ssam	if (c == '\t')
2049173275Ssam		col = 8;
2050173275Ssam	else
2051173275Ssam		col = 1;
2052173275Ssam}
2053173275Ssam
2054173275Ssamstatic void
2055173275SsamLINE_BREAK(void)
2056173275Ssam{
2057173275Ssam	if (spacer != '\t') {
2058173275Ssam		printf("\n");
2059173275Ssam		spacer = '\t';
2060173275Ssam	}
2061173275Ssam	col = 8;		/* 8-col tab */
2062173275Ssam}
2063173275Ssam
2064173275Ssamstatic void
2065173275SsamLINE_CHECK(const char *fmt, ...)
2066173275Ssam{
2067173275Ssam	char buf[80];
2068173275Ssam	va_list ap;
2069173275Ssam	int n;
2070173275Ssam
2071173275Ssam	va_start(ap, fmt);
2072173275Ssam	n = vsnprintf(buf+1, sizeof(buf)-1, fmt, ap);
2073173275Ssam	va_end(ap);
2074173275Ssam	col += 1+n;
2075173275Ssam	if (col > MAXCOL) {
2076173275Ssam		LINE_BREAK();
2077173275Ssam		col += n;
2078173275Ssam	}
2079173275Ssam	buf[0] = spacer;
2080173275Ssam	printf("%s", buf);
2081173275Ssam	spacer = ' ';
2082173275Ssam}
2083173275Ssam
2084138593Ssamstatic int
2085170531Ssamgetmaxrate(const uint8_t rates[15], uint8_t nrates)
2086138593Ssam{
2087138593Ssam	int i, maxrate = -1;
2088138593Ssam
2089138593Ssam	for (i = 0; i < nrates; i++) {
2090138593Ssam		int rate = rates[i] & IEEE80211_RATE_VAL;
2091138593Ssam		if (rate > maxrate)
2092138593Ssam			maxrate = rate;
2093138593Ssam	}
2094138593Ssam	return maxrate / 2;
2095138593Ssam}
2096138593Ssam
2097138593Ssamstatic const char *
2098138593Ssamgetcaps(int capinfo)
2099138593Ssam{
2100138593Ssam	static char capstring[32];
2101138593Ssam	char *cp = capstring;
2102138593Ssam
2103138593Ssam	if (capinfo & IEEE80211_CAPINFO_ESS)
2104138593Ssam		*cp++ = 'E';
2105138593Ssam	if (capinfo & IEEE80211_CAPINFO_IBSS)
2106138593Ssam		*cp++ = 'I';
2107138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
2108138593Ssam		*cp++ = 'c';
2109138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
2110138593Ssam		*cp++ = 'C';
2111138593Ssam	if (capinfo & IEEE80211_CAPINFO_PRIVACY)
2112138593Ssam		*cp++ = 'P';
2113138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
2114138593Ssam		*cp++ = 'S';
2115138593Ssam	if (capinfo & IEEE80211_CAPINFO_PBCC)
2116138593Ssam		*cp++ = 'B';
2117138593Ssam	if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
2118138593Ssam		*cp++ = 'A';
2119138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
2120138593Ssam		*cp++ = 's';
2121138593Ssam	if (capinfo & IEEE80211_CAPINFO_RSN)
2122138593Ssam		*cp++ = 'R';
2123138593Ssam	if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
2124138593Ssam		*cp++ = 'D';
2125138593Ssam	*cp = '\0';
2126138593Ssam	return capstring;
2127138593Ssam}
2128138593Ssam
2129159885Ssamstatic const char *
2130159885Ssamgetflags(int flags)
2131159885Ssam{
2132159885Ssam	static char flagstring[32];
2133159885Ssam	char *cp = flagstring;
2134159885Ssam
2135159885Ssam	if (flags & IEEE80211_NODE_AUTH)
2136159885Ssam		*cp++ = 'A';
2137159885Ssam	if (flags & IEEE80211_NODE_QOS)
2138159885Ssam		*cp++ = 'Q';
2139159885Ssam	if (flags & IEEE80211_NODE_ERP)
2140159885Ssam		*cp++ = 'E';
2141159885Ssam	if (flags & IEEE80211_NODE_PWR_MGT)
2142159885Ssam		*cp++ = 'P';
2143173275Ssam	if (flags & IEEE80211_NODE_HT) {
2144170531Ssam		*cp++ = 'H';
2145173275Ssam		if (flags & IEEE80211_NODE_HTCOMPAT)
2146173275Ssam			*cp++ = '+';
2147173275Ssam	}
2148173275Ssam	if (flags & IEEE80211_NODE_WPS)
2149173275Ssam		*cp++ = 'W';
2150173275Ssam	if (flags & IEEE80211_NODE_TSN)
2151183261Ssam		*cp++ = 'N';
2152183261Ssam	if (flags & IEEE80211_NODE_AMPDU_TX)
2153173275Ssam		*cp++ = 'T';
2154183261Ssam	if (flags & IEEE80211_NODE_AMPDU_RX)
2155183261Ssam		*cp++ = 'R';
2156183261Ssam	if (flags & IEEE80211_NODE_MIMO_PS) {
2157183261Ssam		*cp++ = 'M';
2158183261Ssam		if (flags & IEEE80211_NODE_MIMO_RTS)
2159183261Ssam			*cp++ = '+';
2160183261Ssam	}
2161183261Ssam	if (flags & IEEE80211_NODE_RIFS)
2162183261Ssam		*cp++ = 'I';
2163159885Ssam	*cp = '\0';
2164159885Ssam	return flagstring;
2165159885Ssam}
2166159885Ssam
2167138593Ssamstatic void
2168138593Ssamprintie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
2169138593Ssam{
2170138593Ssam	printf("%s", tag);
2171138593Ssam	if (verbose) {
2172138593Ssam		maxlen -= strlen(tag)+2;
2173138593Ssam		if (2*ielen > maxlen)
2174138593Ssam			maxlen--;
2175138593Ssam		printf("<");
2176138593Ssam		for (; ielen > 0; ie++, ielen--) {
2177138593Ssam			if (maxlen-- <= 0)
2178138593Ssam				break;
2179138593Ssam			printf("%02x", *ie);
2180138593Ssam		}
2181138593Ssam		if (ielen != 0)
2182138593Ssam			printf("-");
2183138593Ssam		printf(">");
2184138593Ssam	}
2185138593Ssam}
2186138593Ssam
2187170531Ssam#define LE_READ_2(p)					\
2188170531Ssam	((u_int16_t)					\
2189170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2190170531Ssam	  (((const u_int8_t *)(p))[1] <<  8)))
2191170531Ssam#define LE_READ_4(p)					\
2192170531Ssam	((u_int32_t)					\
2193170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2194170531Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
2195170531Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
2196170531Ssam	  (((const u_int8_t *)(p))[3] << 24)))
2197170531Ssam
2198138593Ssam/*
2199170531Ssam * NB: The decoding routines assume a properly formatted ie
2200170531Ssam *     which should be safe as the kernel only retains them
2201170531Ssam *     if they parse ok.
2202170531Ssam */
2203170531Ssam
2204170531Ssamstatic void
2205173275Ssamprintwmeparam(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2206170531Ssam{
2207170531Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
2208170531Ssam	static const char *acnames[] = { "BE", "BK", "VO", "VI" };
2209173275Ssam	const struct ieee80211_wme_param *wme =
2210173275Ssam	    (const struct ieee80211_wme_param *) ie;
2211170531Ssam	int i;
2212170531Ssam
2213170531Ssam	printf("%s", tag);
2214173275Ssam	if (!verbose)
2215173275Ssam		return;
2216173275Ssam	printf("<qosinfo 0x%x", wme->param_qosInfo);
2217173275Ssam	ie += offsetof(struct ieee80211_wme_param, params_acParams);
2218173275Ssam	for (i = 0; i < WME_NUM_AC; i++) {
2219173275Ssam		const struct ieee80211_wme_acparams *ac =
2220173275Ssam		    &wme->params_acParams[i];
2221173275Ssam
2222173275Ssam		printf(" %s[%saifsn %u cwmin %u cwmax %u txop %u]"
2223173275Ssam			, acnames[i]
2224173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_ACM) ? "acm " : ""
2225173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_AIFSN)
2226173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMIN)
2227173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMAX)
2228173275Ssam			, LE_READ_2(&ac->acp_txop)
2229173275Ssam		);
2230170531Ssam	}
2231173275Ssam	printf(">");
2232170531Ssam#undef MS
2233170531Ssam}
2234170531Ssam
2235170531Ssamstatic void
2236173275Ssamprintwmeinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2237173275Ssam{
2238173275Ssam	printf("%s", tag);
2239173275Ssam	if (verbose) {
2240173275Ssam		const struct ieee80211_wme_info *wme =
2241173275Ssam		    (const struct ieee80211_wme_info *) ie;
2242173275Ssam		printf("<version 0x%x info 0x%x>",
2243173275Ssam		    wme->wme_version, wme->wme_info);
2244173275Ssam	}
2245173275Ssam}
2246173275Ssam
2247173275Ssamstatic void
2248173275Ssamprinthtcap(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2249173275Ssam{
2250173275Ssam	printf("%s", tag);
2251173275Ssam	if (verbose) {
2252173275Ssam		const struct ieee80211_ie_htcap *htcap =
2253173275Ssam		    (const struct ieee80211_ie_htcap *) ie;
2254173275Ssam		const char *sep;
2255173275Ssam		int i, j;
2256173275Ssam
2257173275Ssam		printf("<cap 0x%x param 0x%x",
2258173275Ssam		    LE_READ_2(&htcap->hc_cap), htcap->hc_param);
2259173275Ssam		printf(" mcsset[");
2260173275Ssam		sep = "";
2261173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2262173275Ssam			if (isset(htcap->hc_mcsset, i)) {
2263173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2264173275Ssam					if (isclr(htcap->hc_mcsset, j))
2265173275Ssam						break;
2266173275Ssam				j--;
2267173275Ssam				if (i == j)
2268173275Ssam					printf("%s%u", sep, i);
2269173275Ssam				else
2270173275Ssam					printf("%s%u-%u", sep, i, j);
2271173275Ssam				i += j-i;
2272173275Ssam				sep = ",";
2273173275Ssam			}
2274173275Ssam		printf("] extcap 0x%x txbf 0x%x antenna 0x%x>",
2275173275Ssam		    LE_READ_2(&htcap->hc_extcap),
2276173275Ssam		    LE_READ_4(&htcap->hc_txbf),
2277173275Ssam		    htcap->hc_antenna);
2278173275Ssam	}
2279173275Ssam}
2280173275Ssam
2281173275Ssamstatic void
2282173275Ssamprinthtinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2283173275Ssam{
2284173275Ssam	printf("%s", tag);
2285173275Ssam	if (verbose) {
2286173275Ssam		const struct ieee80211_ie_htinfo *htinfo =
2287173275Ssam		    (const struct ieee80211_ie_htinfo *) ie;
2288173275Ssam		const char *sep;
2289173275Ssam		int i, j;
2290173275Ssam
2291173275Ssam		printf("<ctl %u, %x,%x,%x,%x", htinfo->hi_ctrlchannel,
2292173275Ssam		    htinfo->hi_byte1, htinfo->hi_byte2, htinfo->hi_byte3,
2293173275Ssam		    LE_READ_2(&htinfo->hi_byte45));
2294173275Ssam		printf(" basicmcs[");
2295173275Ssam		sep = "";
2296173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2297173275Ssam			if (isset(htinfo->hi_basicmcsset, i)) {
2298173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2299173275Ssam					if (isclr(htinfo->hi_basicmcsset, j))
2300173275Ssam						break;
2301173275Ssam				j--;
2302173275Ssam				if (i == j)
2303173275Ssam					printf("%s%u", sep, i);
2304173275Ssam				else
2305173275Ssam					printf("%s%u-%u", sep, i, j);
2306173275Ssam				i += j-i;
2307173275Ssam				sep = ",";
2308173275Ssam			}
2309173275Ssam		printf("]>");
2310173275Ssam	}
2311173275Ssam}
2312173275Ssam
2313173275Ssamstatic void
2314170531Ssamprintathie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2315170531Ssam{
2316170531Ssam
2317170531Ssam	printf("%s", tag);
2318170531Ssam	if (verbose) {
2319170531Ssam		const struct ieee80211_ath_ie *ath =
2320170531Ssam			(const struct ieee80211_ath_ie *)ie;
2321170531Ssam
2322170531Ssam		printf("<");
2323170531Ssam		if (ath->ath_capability & ATHEROS_CAP_TURBO_PRIME)
2324170531Ssam			printf("DTURBO,");
2325170531Ssam		if (ath->ath_capability & ATHEROS_CAP_COMPRESSION)
2326170531Ssam			printf("COMP,");
2327170531Ssam		if (ath->ath_capability & ATHEROS_CAP_FAST_FRAME)
2328170531Ssam			printf("FF,");
2329170531Ssam		if (ath->ath_capability & ATHEROS_CAP_XR)
2330170531Ssam			printf("XR,");
2331170531Ssam		if (ath->ath_capability & ATHEROS_CAP_AR)
2332170531Ssam			printf("AR,");
2333170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BURST)
2334170531Ssam			printf("BURST,");
2335170531Ssam		if (ath->ath_capability & ATHEROS_CAP_WME)
2336170531Ssam			printf("WME,");
2337170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BOOST)
2338170531Ssam			printf("BOOST,");
2339170531Ssam		printf("0x%x>", LE_READ_2(ath->ath_defkeyix));
2340170531Ssam	}
2341170531Ssam}
2342170531Ssam
2343170531Ssamstatic const char *
2344170531Ssamwpa_cipher(const u_int8_t *sel)
2345170531Ssam{
2346170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2347170531Ssam	u_int32_t w = LE_READ_4(sel);
2348170531Ssam
2349170531Ssam	switch (w) {
2350170531Ssam	case WPA_SEL(WPA_CSE_NULL):
2351170531Ssam		return "NONE";
2352170531Ssam	case WPA_SEL(WPA_CSE_WEP40):
2353170531Ssam		return "WEP40";
2354170531Ssam	case WPA_SEL(WPA_CSE_WEP104):
2355170531Ssam		return "WEP104";
2356170531Ssam	case WPA_SEL(WPA_CSE_TKIP):
2357170531Ssam		return "TKIP";
2358170531Ssam	case WPA_SEL(WPA_CSE_CCMP):
2359170531Ssam		return "AES-CCMP";
2360170531Ssam	}
2361170531Ssam	return "?";		/* NB: so 1<< is discarded */
2362170531Ssam#undef WPA_SEL
2363170531Ssam}
2364170531Ssam
2365170531Ssamstatic const char *
2366170531Ssamwpa_keymgmt(const u_int8_t *sel)
2367170531Ssam{
2368170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2369170531Ssam	u_int32_t w = LE_READ_4(sel);
2370170531Ssam
2371170531Ssam	switch (w) {
2372170531Ssam	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
2373170531Ssam		return "8021X-UNSPEC";
2374170531Ssam	case WPA_SEL(WPA_ASE_8021X_PSK):
2375170531Ssam		return "8021X-PSK";
2376170531Ssam	case WPA_SEL(WPA_ASE_NONE):
2377170531Ssam		return "NONE";
2378170531Ssam	}
2379170531Ssam	return "?";
2380170531Ssam#undef WPA_SEL
2381170531Ssam}
2382170531Ssam
2383170531Ssamstatic void
2384170531Ssamprintwpaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2385170531Ssam{
2386170531Ssam	u_int8_t len = ie[1];
2387170531Ssam
2388170531Ssam	printf("%s", tag);
2389170531Ssam	if (verbose) {
2390170531Ssam		const char *sep;
2391170531Ssam		int n;
2392170531Ssam
2393170531Ssam		ie += 6, len -= 4;		/* NB: len is payload only */
2394170531Ssam
2395170531Ssam		printf("<v%u", LE_READ_2(ie));
2396170531Ssam		ie += 2, len -= 2;
2397170531Ssam
2398170531Ssam		printf(" mc:%s", wpa_cipher(ie));
2399170531Ssam		ie += 4, len -= 4;
2400170531Ssam
2401170531Ssam		/* unicast ciphers */
2402170531Ssam		n = LE_READ_2(ie);
2403170531Ssam		ie += 2, len -= 2;
2404170531Ssam		sep = " uc:";
2405170531Ssam		for (; n > 0; n--) {
2406170531Ssam			printf("%s%s", sep, wpa_cipher(ie));
2407170531Ssam			ie += 4, len -= 4;
2408170531Ssam			sep = "+";
2409170531Ssam		}
2410170531Ssam
2411170531Ssam		/* key management algorithms */
2412170531Ssam		n = LE_READ_2(ie);
2413170531Ssam		ie += 2, len -= 2;
2414170531Ssam		sep = " km:";
2415170531Ssam		for (; n > 0; n--) {
2416170531Ssam			printf("%s%s", sep, wpa_keymgmt(ie));
2417170531Ssam			ie += 4, len -= 4;
2418170531Ssam			sep = "+";
2419170531Ssam		}
2420170531Ssam
2421170531Ssam		if (len > 2)		/* optional capabilities */
2422170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2423170531Ssam		printf(">");
2424170531Ssam	}
2425170531Ssam}
2426170531Ssam
2427170531Ssamstatic const char *
2428170531Ssamrsn_cipher(const u_int8_t *sel)
2429170531Ssam{
2430170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2431170531Ssam	u_int32_t w = LE_READ_4(sel);
2432170531Ssam
2433170531Ssam	switch (w) {
2434170531Ssam	case RSN_SEL(RSN_CSE_NULL):
2435170531Ssam		return "NONE";
2436170531Ssam	case RSN_SEL(RSN_CSE_WEP40):
2437170531Ssam		return "WEP40";
2438170531Ssam	case RSN_SEL(RSN_CSE_WEP104):
2439170531Ssam		return "WEP104";
2440170531Ssam	case RSN_SEL(RSN_CSE_TKIP):
2441170531Ssam		return "TKIP";
2442170531Ssam	case RSN_SEL(RSN_CSE_CCMP):
2443170531Ssam		return "AES-CCMP";
2444170531Ssam	case RSN_SEL(RSN_CSE_WRAP):
2445170531Ssam		return "AES-OCB";
2446170531Ssam	}
2447170531Ssam	return "?";
2448170531Ssam#undef WPA_SEL
2449170531Ssam}
2450170531Ssam
2451170531Ssamstatic const char *
2452170531Ssamrsn_keymgmt(const u_int8_t *sel)
2453170531Ssam{
2454170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2455170531Ssam	u_int32_t w = LE_READ_4(sel);
2456170531Ssam
2457170531Ssam	switch (w) {
2458170531Ssam	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
2459170531Ssam		return "8021X-UNSPEC";
2460170531Ssam	case RSN_SEL(RSN_ASE_8021X_PSK):
2461170531Ssam		return "8021X-PSK";
2462170531Ssam	case RSN_SEL(RSN_ASE_NONE):
2463170531Ssam		return "NONE";
2464170531Ssam	}
2465170531Ssam	return "?";
2466170531Ssam#undef RSN_SEL
2467170531Ssam}
2468170531Ssam
2469170531Ssamstatic void
2470170531Ssamprintrsnie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2471170531Ssam{
2472170531Ssam	printf("%s", tag);
2473170531Ssam	if (verbose) {
2474170531Ssam		const char *sep;
2475170531Ssam		int n;
2476170531Ssam
2477173275Ssam		ie += 2, ielen -= 2;
2478170531Ssam
2479170531Ssam		printf("<v%u", LE_READ_2(ie));
2480173275Ssam		ie += 2, ielen -= 2;
2481170531Ssam
2482170531Ssam		printf(" mc:%s", rsn_cipher(ie));
2483173275Ssam		ie += 4, ielen -= 4;
2484170531Ssam
2485170531Ssam		/* unicast ciphers */
2486170531Ssam		n = LE_READ_2(ie);
2487173275Ssam		ie += 2, ielen -= 2;
2488170531Ssam		sep = " uc:";
2489170531Ssam		for (; n > 0; n--) {
2490170531Ssam			printf("%s%s", sep, rsn_cipher(ie));
2491173275Ssam			ie += 4, ielen -= 4;
2492170531Ssam			sep = "+";
2493170531Ssam		}
2494170531Ssam
2495170531Ssam		/* key management algorithms */
2496170531Ssam		n = LE_READ_2(ie);
2497173275Ssam		ie += 2, ielen -= 2;
2498170531Ssam		sep = " km:";
2499170531Ssam		for (; n > 0; n--) {
2500170531Ssam			printf("%s%s", sep, rsn_keymgmt(ie));
2501173275Ssam			ie += 4, ielen -= 4;
2502170531Ssam			sep = "+";
2503170531Ssam		}
2504170531Ssam
2505173275Ssam		if (ielen > 2)		/* optional capabilities */
2506170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2507170531Ssam		/* XXXPMKID */
2508170531Ssam		printf(">");
2509170531Ssam	}
2510170531Ssam}
2511170531Ssam
2512181454Ssam/* XXX move to a public include file */
2513181454Ssam#define IEEE80211_WPS_DEV_PASS_ID	0x1012
2514181454Ssam#define IEEE80211_WPS_SELECTED_REG	0x1041
2515181454Ssam#define IEEE80211_WPS_SETUP_STATE	0x1044
2516181454Ssam#define IEEE80211_WPS_UUID_E		0x1047
2517181454Ssam#define IEEE80211_WPS_VERSION		0x104a
2518181454Ssam
2519181454Ssam#define BE_READ_2(p)					\
2520181454Ssam	((u_int16_t)					\
2521181454Ssam	 ((((const u_int8_t *)(p))[1]      ) |		\
2522181454Ssam	  (((const u_int8_t *)(p))[0] <<  8)))
2523181454Ssam
2524181454Ssamstatic void
2525181454Ssamprintwpsie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2526181454Ssam{
2527181454Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
2528181454Ssam	u_int8_t len = ie[1];
2529181454Ssam
2530181454Ssam	printf("%s", tag);
2531181454Ssam	if (verbose) {
2532181454Ssam		static const char *dev_pass_id[] = {
2533181454Ssam			"D",	/* Default (PIN) */
2534181454Ssam			"U",	/* User-specified */
2535181454Ssam			"M",	/* Machine-specified */
2536181454Ssam			"K",	/* Rekey */
2537181454Ssam			"P",	/* PushButton */
2538181454Ssam			"R"	/* Registrar-specified */
2539181454Ssam		};
2540181454Ssam		int n;
2541181454Ssam
2542181454Ssam		ie +=6, len -= 4;		/* NB: len is payload only */
2543181454Ssam
2544181454Ssam		/* WPS IE in Beacon and Probe Resp frames have different fields */
2545181454Ssam		printf("<");
2546181454Ssam		while (len) {
2547181454Ssam			uint16_t tlv_type = BE_READ_2(ie);
2548181454Ssam			uint16_t tlv_len  = BE_READ_2(ie + 2);
2549181454Ssam
2550181454Ssam			ie += 4, len -= 4;
2551181454Ssam
2552181454Ssam			switch (tlv_type) {
2553181454Ssam			case IEEE80211_WPS_VERSION:
2554181454Ssam				printf("v:%d.%d", *ie >> 4, *ie & 0xf);
2555181454Ssam				break;
2556181454Ssam			case IEEE80211_WPS_SETUP_STATE:
2557181454Ssam				/* Only 1 and 2 are valid */
2558181454Ssam				if (*ie == 0 || *ie >= 3)
2559181454Ssam					printf(" state:B");
2560181454Ssam				else
2561181454Ssam					printf(" st:%s", *ie == 1 ? "N" : "C");
2562181454Ssam				break;
2563181454Ssam			case IEEE80211_WPS_SELECTED_REG:
2564181454Ssam				printf(" sel:%s", *ie ? "T" : "F");
2565181454Ssam				break;
2566181454Ssam			case IEEE80211_WPS_DEV_PASS_ID:
2567181454Ssam				n = LE_READ_2(ie);
2568181454Ssam				if (n < N(dev_pass_id))
2569181454Ssam					printf(" dpi:%s", dev_pass_id[n]);
2570181454Ssam				break;
2571181454Ssam			case IEEE80211_WPS_UUID_E:
2572181454Ssam				printf(" uuid-e:");
2573181454Ssam				for (n = 0; n < (tlv_len - 1); n++)
2574181454Ssam					printf("%02x-", ie[n]);
2575181454Ssam				printf("%02x", ie[n]);
2576181454Ssam				break;
2577181454Ssam			}
2578181454Ssam			ie += tlv_len, len -= tlv_len;
2579181454Ssam		}
2580181454Ssam		printf(">");
2581181454Ssam	}
2582181454Ssam#undef N
2583181454Ssam}
2584181454Ssam
2585186904Ssamstatic void
2586186904Ssamprinttdmaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2587186904Ssam{
2588186904Ssam	printf("%s", tag);
2589186904Ssam	if (verbose && ielen >= sizeof(struct ieee80211_tdma_param)) {
2590186904Ssam		const struct ieee80211_tdma_param *tdma =
2591186904Ssam		   (const struct ieee80211_tdma_param *) ie;
2592186904Ssam
2593186904Ssam		/* XXX tstamp */
2594186904Ssam		printf("<v%u slot:%u slotcnt:%u slotlen:%u bintval:%u inuse:0x%x>",
2595186904Ssam		    tdma->tdma_version, tdma->tdma_slot, tdma->tdma_slotcnt,
2596186904Ssam		    LE_READ_2(&tdma->tdma_slotlen), tdma->tdma_bintval,
2597186904Ssam		    tdma->tdma_inuse[0]);
2598186904Ssam	}
2599186904Ssam}
2600186904Ssam
2601170531Ssam/*
2602138593Ssam * Copy the ssid string contents into buf, truncating to fit.  If the
2603138593Ssam * ssid is entirely printable then just copy intact.  Otherwise convert
2604138593Ssam * to hexadecimal.  If the result is truncated then replace the last
2605138593Ssam * three characters with "...".
2606138593Ssam */
2607146873Sjhbstatic int
2608138593Ssamcopy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
2609138593Ssam{
2610138593Ssam	const u_int8_t *p;
2611138593Ssam	size_t maxlen;
2612138593Ssam	int i;
2613138593Ssam
2614138593Ssam	if (essid_len > bufsize)
2615138593Ssam		maxlen = bufsize;
2616138593Ssam	else
2617138593Ssam		maxlen = essid_len;
2618138593Ssam	/* determine printable or not */
2619138593Ssam	for (i = 0, p = essid; i < maxlen; i++, p++) {
2620138593Ssam		if (*p < ' ' || *p > 0x7e)
2621138593Ssam			break;
2622138593Ssam	}
2623138593Ssam	if (i != maxlen) {		/* not printable, print as hex */
2624138593Ssam		if (bufsize < 3)
2625138593Ssam			return 0;
2626138593Ssam		strlcpy(buf, "0x", bufsize);
2627138593Ssam		bufsize -= 2;
2628138593Ssam		p = essid;
2629138593Ssam		for (i = 0; i < maxlen && bufsize >= 2; i++) {
2630147489Savatar			sprintf(&buf[2+2*i], "%02x", p[i]);
2631138593Ssam			bufsize -= 2;
2632138593Ssam		}
2633147489Savatar		if (i != essid_len)
2634147489Savatar			memcpy(&buf[2+2*i-3], "...", 3);
2635138593Ssam	} else {			/* printable, truncate as needed */
2636138593Ssam		memcpy(buf, essid, maxlen);
2637147489Savatar		if (maxlen != essid_len)
2638147489Savatar			memcpy(&buf[maxlen-3], "...", 3);
2639138593Ssam	}
2640138593Ssam	return maxlen;
2641138593Ssam}
2642138593Ssam
2643173275Ssamstatic void
2644173275Ssamprintssid(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2645173275Ssam{
2646173275Ssam	char ssid[2*IEEE80211_NWID_LEN+1];
2647173275Ssam
2648173275Ssam	printf("%s<%.*s>", tag, copy_essid(ssid, maxlen, ie+2, ie[1]), ssid);
2649173275Ssam}
2650173275Ssam
2651173275Ssamstatic void
2652173275Ssamprintrates(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2653173275Ssam{
2654173275Ssam	const char *sep;
2655173275Ssam	int i;
2656173275Ssam
2657173275Ssam	printf("%s", tag);
2658173275Ssam	sep = "<";
2659173275Ssam	for (i = 2; i < ielen; i++) {
2660173275Ssam		printf("%s%s%d", sep,
2661173275Ssam		    ie[i] & IEEE80211_RATE_BASIC ? "B" : "",
2662173275Ssam		    ie[i] & IEEE80211_RATE_VAL);
2663173275Ssam		sep = ",";
2664173275Ssam	}
2665173275Ssam	printf(">");
2666173275Ssam}
2667173275Ssam
2668173275Ssamstatic void
2669173275Ssamprintcountry(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2670173275Ssam{
2671173275Ssam	const struct ieee80211_country_ie *cie =
2672173275Ssam	   (const struct ieee80211_country_ie *) ie;
2673173275Ssam	int i, nbands, schan, nchan;
2674173275Ssam
2675173275Ssam	printf("%s<%c%c%c", tag, cie->cc[0], cie->cc[1], cie->cc[2]);
2676173275Ssam	nbands = (cie->len - 3) / sizeof(cie->band[0]);
2677173275Ssam	for (i = 0; i < nbands; i++) {
2678173275Ssam		schan = cie->band[i].schan;
2679173275Ssam		nchan = cie->band[i].nchan;
2680173275Ssam		if (nchan != 1)
2681173275Ssam			printf(" %u-%u,%u", schan, schan + nchan-1,
2682173275Ssam			    cie->band[i].maxtxpwr);
2683173275Ssam		else
2684173275Ssam			printf(" %u,%u", schan, cie->band[i].maxtxpwr);
2685173275Ssam	}
2686173275Ssam	printf(">");
2687173275Ssam}
2688173275Ssam
2689148686Sstefanf/* unaligned little endian access */
2690138593Ssam#define LE_READ_4(p)					\
2691138593Ssam	((u_int32_t)					\
2692138593Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2693138593Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
2694138593Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
2695138593Ssam	  (((const u_int8_t *)(p))[3] << 24)))
2696138593Ssam
2697178354Ssamstatic __inline int
2698138593Ssamiswpaoui(const u_int8_t *frm)
2699138593Ssam{
2700138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
2701138593Ssam}
2702138593Ssam
2703178354Ssamstatic __inline int
2704173275Ssamiswmeinfo(const u_int8_t *frm)
2705138593Ssam{
2706173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
2707173275Ssam		frm[6] == WME_INFO_OUI_SUBTYPE;
2708138593Ssam}
2709138593Ssam
2710178354Ssamstatic __inline int
2711173275Ssamiswmeparam(const u_int8_t *frm)
2712173275Ssam{
2713173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
2714173275Ssam		frm[6] == WME_PARAM_OUI_SUBTYPE;
2715173275Ssam}
2716173275Ssam
2717178354Ssamstatic __inline int
2718138593Ssamisatherosoui(const u_int8_t *frm)
2719138593Ssam{
2720138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
2721138593Ssam}
2722138593Ssam
2723181454Ssamstatic __inline int
2724186904Ssamistdmaoui(const uint8_t *frm)
2725186904Ssam{
2726186904Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((TDMA_OUI_TYPE<<24)|TDMA_OUI);
2727186904Ssam}
2728186904Ssam
2729186904Ssamstatic __inline int
2730181454Ssamiswpsoui(const uint8_t *frm)
2731181454Ssam{
2732181454Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPS_OUI_TYPE<<24)|WPA_OUI);
2733181454Ssam}
2734181454Ssam
2735173275Ssamstatic const char *
2736173275Ssamiename(int elemid)
2737173275Ssam{
2738173275Ssam	switch (elemid) {
2739173275Ssam	case IEEE80211_ELEMID_FHPARMS:	return " FHPARMS";
2740173275Ssam	case IEEE80211_ELEMID_CFPARMS:	return " CFPARMS";
2741173275Ssam	case IEEE80211_ELEMID_TIM:	return " TIM";
2742173275Ssam	case IEEE80211_ELEMID_IBSSPARMS:return " IBSSPARMS";
2743173275Ssam	case IEEE80211_ELEMID_CHALLENGE:return " CHALLENGE";
2744173275Ssam	case IEEE80211_ELEMID_PWRCNSTR:	return " PWRCNSTR";
2745173275Ssam	case IEEE80211_ELEMID_PWRCAP:	return " PWRCAP";
2746173275Ssam	case IEEE80211_ELEMID_TPCREQ:	return " TPCREQ";
2747173275Ssam	case IEEE80211_ELEMID_TPCREP:	return " TPCREP";
2748173275Ssam	case IEEE80211_ELEMID_SUPPCHAN:	return " SUPPCHAN";
2749173275Ssam	case IEEE80211_ELEMID_CHANSWITCHANN:return " CSA";
2750173275Ssam	case IEEE80211_ELEMID_MEASREQ:	return " MEASREQ";
2751173275Ssam	case IEEE80211_ELEMID_MEASREP:	return " MEASREP";
2752173275Ssam	case IEEE80211_ELEMID_QUIET:	return " QUIET";
2753173275Ssam	case IEEE80211_ELEMID_IBSSDFS:	return " IBSSDFS";
2754173275Ssam	case IEEE80211_ELEMID_TPC:	return " TPC";
2755173275Ssam	case IEEE80211_ELEMID_CCKM:	return " CCKM";
2756173275Ssam	}
2757173275Ssam	return " ???";
2758173275Ssam}
2759173275Ssam
2760138593Ssamstatic void
2761138593Ssamprinties(const u_int8_t *vp, int ielen, int maxcols)
2762138593Ssam{
2763138593Ssam	while (ielen > 0) {
2764138593Ssam		switch (vp[0]) {
2765173275Ssam		case IEEE80211_ELEMID_SSID:
2766173275Ssam			if (verbose)
2767173275Ssam				printssid(" SSID", vp, 2+vp[1], maxcols);
2768173275Ssam			break;
2769173275Ssam		case IEEE80211_ELEMID_RATES:
2770173275Ssam		case IEEE80211_ELEMID_XRATES:
2771173275Ssam			if (verbose)
2772173275Ssam				printrates(vp[0] == IEEE80211_ELEMID_RATES ?
2773173275Ssam				    " RATES" : " XRATES", vp, 2+vp[1], maxcols);
2774173275Ssam			break;
2775173275Ssam		case IEEE80211_ELEMID_DSPARMS:
2776173275Ssam			if (verbose)
2777173275Ssam				printf(" DSPARMS<%u>", vp[2]);
2778173275Ssam			break;
2779173275Ssam		case IEEE80211_ELEMID_COUNTRY:
2780173275Ssam			if (verbose)
2781173275Ssam				printcountry(" COUNTRY", vp, 2+vp[1], maxcols);
2782173275Ssam			break;
2783173275Ssam		case IEEE80211_ELEMID_ERP:
2784173275Ssam			if (verbose)
2785173275Ssam				printf(" ERP<0x%x>", vp[2]);
2786173275Ssam			break;
2787138593Ssam		case IEEE80211_ELEMID_VENDOR:
2788138593Ssam			if (iswpaoui(vp))
2789170531Ssam				printwpaie(" WPA", vp, 2+vp[1], maxcols);
2790173275Ssam			else if (iswmeinfo(vp))
2791173275Ssam				printwmeinfo(" WME", vp, 2+vp[1], maxcols);
2792173275Ssam			else if (iswmeparam(vp))
2793173275Ssam				printwmeparam(" WME", vp, 2+vp[1], maxcols);
2794139492Ssam			else if (isatherosoui(vp))
2795170531Ssam				printathie(" ATH", vp, 2+vp[1], maxcols);
2796181454Ssam			else if (iswpsoui(vp))
2797181454Ssam				printwpsie(" WPS", vp, 2+vp[1], maxcols);
2798186904Ssam			else if (istdmaoui(vp))
2799186904Ssam				printtdmaie(" TDMA", vp, 2+vp[1], maxcols);
2800173275Ssam			else if (verbose)
2801138593Ssam				printie(" VEN", vp, 2+vp[1], maxcols);
2802138593Ssam			break;
2803138593Ssam		case IEEE80211_ELEMID_RSN:
2804170531Ssam			printrsnie(" RSN", vp, 2+vp[1], maxcols);
2805138593Ssam			break;
2806173275Ssam		case IEEE80211_ELEMID_HTCAP:
2807173275Ssam			printhtcap(" HTCAP", vp, 2+vp[1], maxcols);
2808173275Ssam			break;
2809173275Ssam		case IEEE80211_ELEMID_HTINFO:
2810173275Ssam			if (verbose)
2811173275Ssam				printhtinfo(" HTINFO", vp, 2+vp[1], maxcols);
2812173275Ssam			break;
2813138593Ssam		default:
2814173275Ssam			if (verbose)
2815173275Ssam				printie(iename(vp[0]), vp, 2+vp[1], maxcols);
2816138593Ssam			break;
2817138593Ssam		}
2818138593Ssam		ielen -= 2+vp[1];
2819138593Ssam		vp += 2+vp[1];
2820138593Ssam	}
2821138593Ssam}
2822138593Ssam
2823138593Ssamstatic void
2824178354Ssamprintmimo(const struct ieee80211_mimo_info *mi)
2825178354Ssam{
2826178354Ssam	/* NB: don't muddy display unless there's something to show */
2827178354Ssam	if (mi->rssi[0] != 0 || mi->rssi[1] != 0 || mi->rssi[2] != 0) {
2828178354Ssam		/* XXX ignore EVM for now */
2829178354Ssam		printf(" (rssi %d:%d:%d nf %d:%d:%d)",
2830178354Ssam		    mi->rssi[0], mi->rssi[1], mi->rssi[2],
2831178354Ssam		    mi->noise[0], mi->noise[1], mi->noise[2]);
2832178354Ssam	}
2833178354Ssam}
2834178354Ssam
2835178354Ssamstatic void
2836138593Ssamlist_scan(int s)
2837138593Ssam{
2838138593Ssam	uint8_t buf[24*1024];
2839153892Srwatson	char ssid[IEEE80211_NWID_LEN+1];
2840173275Ssam	const uint8_t *cp;
2841154522Ssam	int len, ssidmax;
2842138593Ssam
2843173275Ssam	if (get80211len(s, IEEE80211_IOC_SCAN_RESULTS, buf, sizeof(buf), &len) < 0)
2844138593Ssam		errx(1, "unable to get scan results");
2845138593Ssam	if (len < sizeof(struct ieee80211req_scan_result))
2846138593Ssam		return;
2847138593Ssam
2848170531Ssam	getchaninfo(s);
2849170531Ssam
2850154522Ssam	ssidmax = verbose ? IEEE80211_NWID_LEN : 14;
2851170531Ssam	printf("%-*.*s  %-17.17s  %4s %4s  %-7s  %3s %4s\n"
2852154522Ssam		, ssidmax, ssidmax, "SSID"
2853138593Ssam		, "BSSID"
2854138593Ssam		, "CHAN"
2855138593Ssam		, "RATE"
2856170531Ssam		, " S:N"
2857138593Ssam		, "INT"
2858138593Ssam		, "CAPS"
2859138593Ssam	);
2860138593Ssam	cp = buf;
2861138593Ssam	do {
2862170531Ssam		const struct ieee80211req_scan_result *sr;
2863170531Ssam		const uint8_t *vp;
2864138593Ssam
2865170531Ssam		sr = (const struct ieee80211req_scan_result *) cp;
2866173275Ssam		vp = cp + sr->isr_ie_off;
2867170531Ssam		printf("%-*.*s  %s  %3d  %3dM %3d:%-3d  %3d %-4.4s"
2868154522Ssam			, ssidmax
2869155461Ssam			  , copy_essid(ssid, ssidmax, vp, sr->isr_ssid_len)
2870154522Ssam			  , ssid
2871138593Ssam			, ether_ntoa((const struct ether_addr *) sr->isr_bssid)
2872165570Ssam			, ieee80211_mhz2ieee(sr->isr_freq, sr->isr_flags)
2873138593Ssam			, getmaxrate(sr->isr_rates, sr->isr_nrates)
2874170531Ssam			, (sr->isr_rssi/2)+sr->isr_noise, sr->isr_noise
2875138593Ssam			, sr->isr_intval
2876138593Ssam			, getcaps(sr->isr_capinfo)
2877138593Ssam		);
2878170531Ssam		printies(vp + sr->isr_ssid_len, sr->isr_ie_len, 24);
2879138593Ssam		printf("\n");
2880138593Ssam		cp += sr->isr_len, len -= sr->isr_len;
2881138593Ssam	} while (len >= sizeof(struct ieee80211req_scan_result));
2882138593Ssam}
2883138593Ssam
2884178354Ssam#ifdef __FreeBSD__
2885138593Ssam#include <net80211/ieee80211_freebsd.h>
2886178354Ssam#endif
2887178354Ssam#ifdef __NetBSD__
2888178354Ssam#include <net80211/ieee80211_netbsd.h>
2889178354Ssam#endif
2890138593Ssam
2891138593Ssamstatic void
2892138593Ssamscan_and_wait(int s)
2893138593Ssam{
2894178354Ssam	struct ieee80211_scan_req sr;
2895138593Ssam	struct ieee80211req ireq;
2896138593Ssam	int sroute;
2897138593Ssam
2898138593Ssam	sroute = socket(PF_ROUTE, SOCK_RAW, 0);
2899138593Ssam	if (sroute < 0) {
2900138593Ssam		perror("socket(PF_ROUTE,SOCK_RAW)");
2901138593Ssam		return;
2902138593Ssam	}
2903138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
2904138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
2905138593Ssam	ireq.i_type = IEEE80211_IOC_SCAN_REQ;
2906178354Ssam
2907178354Ssam	memset(&sr, 0, sizeof(sr));
2908178354Ssam	sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE
2909178354Ssam		    | IEEE80211_IOC_SCAN_NOPICK
2910178354Ssam		    | IEEE80211_IOC_SCAN_ONCE;
2911178354Ssam	sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
2912178354Ssam	sr.sr_nssid = 0;
2913178354Ssam
2914178354Ssam	ireq.i_data = &sr;
2915178354Ssam	ireq.i_len = sizeof(sr);
2916138593Ssam	/* NB: only root can trigger a scan so ignore errors */
2917138593Ssam	if (ioctl(s, SIOCS80211, &ireq) >= 0) {
2918138593Ssam		char buf[2048];
2919138593Ssam		struct if_announcemsghdr *ifan;
2920138593Ssam		struct rt_msghdr *rtm;
2921138593Ssam
2922138593Ssam		do {
2923138593Ssam			if (read(sroute, buf, sizeof(buf)) < 0) {
2924138593Ssam				perror("read(PF_ROUTE)");
2925138593Ssam				break;
2926138593Ssam			}
2927138593Ssam			rtm = (struct rt_msghdr *) buf;
2928138593Ssam			if (rtm->rtm_version != RTM_VERSION)
2929138593Ssam				break;
2930138593Ssam			ifan = (struct if_announcemsghdr *) rtm;
2931138593Ssam		} while (rtm->rtm_type != RTM_IEEE80211 ||
2932138593Ssam		    ifan->ifan_what != RTM_IEEE80211_SCAN);
2933138593Ssam	}
2934138593Ssam	close(sroute);
2935138593Ssam}
2936138593Ssam
2937138593Ssamstatic
2938138593SsamDECL_CMD_FUNC(set80211scan, val, d)
2939138593Ssam{
2940138593Ssam	scan_and_wait(s);
2941138593Ssam	list_scan(s);
2942138593Ssam}
2943138593Ssam
2944161147Ssamstatic enum ieee80211_opmode get80211opmode(int s);
2945161147Ssam
2946173275Ssamstatic int
2947173275Ssamgettxseq(const struct ieee80211req_sta_info *si)
2948173275Ssam{
2949173275Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
2950173275Ssam
2951173275Ssam	int i, txseq;
2952173275Ssam
2953173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
2954173275Ssam		return si->isi_txseqs[0];
2955173275Ssam	/* XXX not right but usually what folks want */
2956173275Ssam	txseq = 0;
2957173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
2958173275Ssam		if (si->isi_txseqs[i] > txseq)
2959173275Ssam			txseq = si->isi_txseqs[i];
2960173275Ssam	return txseq;
2961173275Ssam#undef IEEE80211_NODE_QOS
2962173275Ssam}
2963173275Ssam
2964173275Ssamstatic int
2965173275Ssamgetrxseq(const struct ieee80211req_sta_info *si)
2966173275Ssam{
2967173275Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
2968173275Ssam
2969173275Ssam	int i, rxseq;
2970173275Ssam
2971173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
2972173275Ssam		return si->isi_rxseqs[0];
2973173275Ssam	/* XXX not right but usually what folks want */
2974173275Ssam	rxseq = 0;
2975173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
2976173275Ssam		if (si->isi_rxseqs[i] > rxseq)
2977173275Ssam			rxseq = si->isi_rxseqs[i];
2978173275Ssam	return rxseq;
2979173275Ssam#undef IEEE80211_NODE_QOS
2980173275Ssam}
2981173275Ssam
2982138593Ssamstatic void
2983138593Ssamlist_stations(int s)
2984138593Ssam{
2985161147Ssam	union {
2986161147Ssam		struct ieee80211req_sta_req req;
2987161147Ssam		uint8_t buf[24*1024];
2988161147Ssam	} u;
2989161147Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
2990170531Ssam	const uint8_t *cp;
2991138593Ssam	int len;
2992138593Ssam
2993161147Ssam	/* broadcast address =>'s get all stations */
2994161147Ssam	(void) memset(u.req.is_u.macaddr, 0xff, IEEE80211_ADDR_LEN);
2995161147Ssam	if (opmode == IEEE80211_M_STA) {
2996161147Ssam		/*
2997161147Ssam		 * Get information about the associated AP.
2998161147Ssam		 */
2999173275Ssam		(void) get80211(s, IEEE80211_IOC_BSSID,
3000173275Ssam		    u.req.is_u.macaddr, IEEE80211_ADDR_LEN);
3001161147Ssam	}
3002173275Ssam	if (get80211len(s, IEEE80211_IOC_STA_INFO, &u, sizeof(u), &len) < 0)
3003138593Ssam		errx(1, "unable to get station information");
3004138593Ssam	if (len < sizeof(struct ieee80211req_sta_info))
3005138593Ssam		return;
3006138593Ssam
3007170531Ssam	getchaninfo(s);
3008170531Ssam
3009159885Ssam	printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %4s\n"
3010138593Ssam		, "ADDR"
3011138593Ssam		, "AID"
3012138593Ssam		, "CHAN"
3013138593Ssam		, "RATE"
3014138593Ssam		, "RSSI"
3015138593Ssam		, "IDLE"
3016138593Ssam		, "TXSEQ"
3017138593Ssam		, "RXSEQ"
3018138593Ssam		, "CAPS"
3019159885Ssam		, "FLAG"
3020138593Ssam	);
3021170531Ssam	cp = (const uint8_t *) u.req.info;
3022138593Ssam	do {
3023170531Ssam		const struct ieee80211req_sta_info *si;
3024138593Ssam
3025170531Ssam		si = (const struct ieee80211req_sta_info *) cp;
3026161147Ssam		if (si->isi_len < sizeof(*si))
3027161147Ssam			break;
3028170531Ssam		printf("%s %4u %4d %3dM %3.1f %4d %6d %6d %-4.4s %-4.4s"
3029138593Ssam			, ether_ntoa((const struct ether_addr*) si->isi_macaddr)
3030138593Ssam			, IEEE80211_AID(si->isi_associd)
3031166015Ssam			, ieee80211_mhz2ieee(si->isi_freq, si->isi_flags)
3032178354Ssam			, si->isi_txmbps/2
3033170531Ssam			, si->isi_rssi/2.
3034138593Ssam			, si->isi_inact
3035173275Ssam			, gettxseq(si)
3036173275Ssam			, getrxseq(si)
3037138593Ssam			, getcaps(si->isi_capinfo)
3038159885Ssam			, getflags(si->isi_state)
3039138593Ssam		);
3040170531Ssam		printies(cp + si->isi_ie_off, si->isi_ie_len, 24);
3041178354Ssam		printmimo(&si->isi_mimo);
3042138593Ssam		printf("\n");
3043138593Ssam		cp += si->isi_len, len -= si->isi_len;
3044138593Ssam	} while (len >= sizeof(struct ieee80211req_sta_info));
3045138593Ssam}
3046138593Ssam
3047170531Ssamstatic const char *
3048170531Ssamget_chaninfo(const struct ieee80211_channel *c, int precise,
3049170531Ssam	char buf[], size_t bsize)
3050138593Ssam{
3051138593Ssam	buf[0] = '\0';
3052138593Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
3053170531Ssam		strlcat(buf, " FHSS", bsize);
3054165570Ssam	if (IEEE80211_IS_CHAN_A(c)) {
3055165570Ssam		if (IEEE80211_IS_CHAN_HALF(c))
3056170531Ssam			strlcat(buf, " 11a/10Mhz", bsize);
3057165570Ssam		else if (IEEE80211_IS_CHAN_QUARTER(c))
3058170531Ssam			strlcat(buf, " 11a/5Mhz", bsize);
3059165570Ssam		else
3060170531Ssam			strlcat(buf, " 11a", bsize);
3061165570Ssam	}
3062166015Ssam	if (IEEE80211_IS_CHAN_ANYG(c)) {
3063166015Ssam		if (IEEE80211_IS_CHAN_HALF(c))
3064170531Ssam			strlcat(buf, " 11g/10Mhz", bsize);
3065166015Ssam		else if (IEEE80211_IS_CHAN_QUARTER(c))
3066170531Ssam			strlcat(buf, " 11g/5Mhz", bsize);
3067166015Ssam		else
3068170531Ssam			strlcat(buf, " 11g", bsize);
3069166015Ssam	} else if (IEEE80211_IS_CHAN_B(c))
3070170531Ssam		strlcat(buf, " 11b", bsize);
3071170531Ssam	if (IEEE80211_IS_CHAN_TURBO(c))
3072170531Ssam		strlcat(buf, " Turbo", bsize);
3073170531Ssam	if (precise) {
3074170531Ssam		if (IEEE80211_IS_CHAN_HT20(c))
3075170531Ssam			strlcat(buf, " ht/20", bsize);
3076170531Ssam		else if (IEEE80211_IS_CHAN_HT40D(c))
3077170531Ssam			strlcat(buf, " ht/40-", bsize);
3078170531Ssam		else if (IEEE80211_IS_CHAN_HT40U(c))
3079170531Ssam			strlcat(buf, " ht/40+", bsize);
3080170531Ssam	} else {
3081170531Ssam		if (IEEE80211_IS_CHAN_HT(c))
3082170531Ssam			strlcat(buf, " ht", bsize);
3083170531Ssam	}
3084170531Ssam	return buf;
3085170531Ssam}
3086170531Ssam
3087170531Ssamstatic void
3088173275Ssamprint_chaninfo(const struct ieee80211_channel *c, int verb)
3089170531Ssam{
3090170531Ssam	char buf[14];
3091170531Ssam
3092138593Ssam	printf("Channel %3u : %u%c Mhz%-14.14s",
3093165570Ssam		ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
3094170531Ssam		IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
3095173275Ssam		get_chaninfo(c, verb, buf, sizeof(buf)));
3096138593Ssam}
3097138593Ssam
3098138593Ssamstatic void
3099173275Ssamprint_channels(int s, const struct ieee80211req_chaninfo *chans,
3100173275Ssam	int allchans, int verb)
3101138593Ssam{
3102138593Ssam	struct ieee80211req_chaninfo achans;
3103170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3104138593Ssam	const struct ieee80211_channel *c;
3105170531Ssam	int i, half;
3106138593Ssam
3107170531Ssam	memset(&achans, 0, sizeof(achans));
3108170531Ssam	memset(reported, 0, sizeof(reported));
3109138593Ssam	if (!allchans) {
3110138593Ssam		struct ieee80211req_chanlist active;
3111138593Ssam
3112173275Ssam		if (get80211(s, IEEE80211_IOC_CHANLIST, &active, sizeof(active)) < 0)
3113138593Ssam			errx(1, "unable to get active channel list");
3114138593Ssam		memset(&achans, 0, sizeof(achans));
3115173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3116173275Ssam			c = &chans->ic_chans[i];
3117170531Ssam			if (!isset(active.ic_channels, c->ic_ieee))
3118170531Ssam				continue;
3119170531Ssam			/*
3120170531Ssam			 * Suppress compatible duplicates unless
3121170531Ssam			 * verbose.  The kernel gives us it's
3122170531Ssam			 * complete channel list which has separate
3123170531Ssam			 * entries for 11g/11b and 11a/turbo.
3124170531Ssam			 */
3125173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3126170531Ssam				/* XXX we assume duplicates are adjacent */
3127170531Ssam				achans.ic_chans[achans.ic_nchans-1] = *c;
3128170531Ssam			} else {
3129138593Ssam				achans.ic_chans[achans.ic_nchans++] = *c;
3130170531Ssam				setbit(reported, c->ic_ieee);
3131170531Ssam			}
3132138593Ssam		}
3133170531Ssam	} else {
3134173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3135173275Ssam			c = &chans->ic_chans[i];
3136170531Ssam			/* suppress duplicates as above */
3137173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3138170531Ssam				/* XXX we assume duplicates are adjacent */
3139170531Ssam				achans.ic_chans[achans.ic_nchans-1] = *c;
3140170531Ssam			} else {
3141170531Ssam				achans.ic_chans[achans.ic_nchans++] = *c;
3142170531Ssam				setbit(reported, c->ic_ieee);
3143170531Ssam			}
3144170531Ssam		}
3145170531Ssam	}
3146138593Ssam	half = achans.ic_nchans / 2;
3147138593Ssam	if (achans.ic_nchans % 2)
3148138593Ssam		half++;
3149170531Ssam
3150138593Ssam	for (i = 0; i < achans.ic_nchans / 2; i++) {
3151173275Ssam		print_chaninfo(&achans.ic_chans[i], verb);
3152173275Ssam		print_chaninfo(&achans.ic_chans[half+i], verb);
3153138593Ssam		printf("\n");
3154138593Ssam	}
3155138593Ssam	if (achans.ic_nchans % 2) {
3156173275Ssam		print_chaninfo(&achans.ic_chans[i], verb);
3157138593Ssam		printf("\n");
3158138593Ssam	}
3159138593Ssam}
3160138593Ssam
3161138593Ssamstatic void
3162173275Ssamlist_channels(int s, int allchans)
3163173275Ssam{
3164173275Ssam	getchaninfo(s);
3165173275Ssam	print_channels(s, &chaninfo, allchans, verbose);
3166173275Ssam}
3167173275Ssam
3168173275Ssamstatic void
3169170531Ssamprint_txpow(const struct ieee80211_channel *c)
3170170531Ssam{
3171170531Ssam	printf("Channel %3u : %u Mhz %3.1f reg %2d  ",
3172170531Ssam	    c->ic_ieee, c->ic_freq,
3173170531Ssam	    c->ic_maxpower/2., c->ic_maxregpower);
3174170531Ssam}
3175170531Ssam
3176170531Ssamstatic void
3177170531Ssamprint_txpow_verbose(const struct ieee80211_channel *c)
3178170531Ssam{
3179173275Ssam	print_chaninfo(c, 1);
3180170531Ssam	printf("min %4.1f dBm  max %3.1f dBm  reg %2d dBm",
3181170531Ssam	    c->ic_minpower/2., c->ic_maxpower/2., c->ic_maxregpower);
3182170531Ssam	/* indicate where regulatory cap limits power use */
3183170531Ssam	if (c->ic_maxpower > 2*c->ic_maxregpower)
3184170531Ssam		printf(" <");
3185170531Ssam}
3186170531Ssam
3187170531Ssamstatic void
3188170531Ssamlist_txpow(int s)
3189170531Ssam{
3190170531Ssam	struct ieee80211req_chaninfo achans;
3191170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3192170531Ssam	struct ieee80211_channel *c, *prev;
3193170531Ssam	int i, half;
3194170531Ssam
3195170531Ssam	getchaninfo(s);
3196170531Ssam	memset(&achans, 0, sizeof(achans));
3197170531Ssam	memset(reported, 0, sizeof(reported));
3198170531Ssam	for (i = 0; i < chaninfo.ic_nchans; i++) {
3199170531Ssam		c = &chaninfo.ic_chans[i];
3200170531Ssam		/* suppress duplicates as above */
3201170531Ssam		if (isset(reported, c->ic_ieee) && !verbose) {
3202170531Ssam			/* XXX we assume duplicates are adjacent */
3203170531Ssam			prev = &achans.ic_chans[achans.ic_nchans-1];
3204170531Ssam			/* display highest power on channel */
3205170531Ssam			if (c->ic_maxpower > prev->ic_maxpower)
3206170531Ssam				*prev = *c;
3207170531Ssam		} else {
3208170531Ssam			achans.ic_chans[achans.ic_nchans++] = *c;
3209170531Ssam			setbit(reported, c->ic_ieee);
3210170531Ssam		}
3211170531Ssam	}
3212170531Ssam	if (!verbose) {
3213170531Ssam		half = achans.ic_nchans / 2;
3214170531Ssam		if (achans.ic_nchans % 2)
3215170531Ssam			half++;
3216170531Ssam
3217170531Ssam		for (i = 0; i < achans.ic_nchans / 2; i++) {
3218170531Ssam			print_txpow(&achans.ic_chans[i]);
3219170531Ssam			print_txpow(&achans.ic_chans[half+i]);
3220170531Ssam			printf("\n");
3221170531Ssam		}
3222170531Ssam		if (achans.ic_nchans % 2) {
3223170531Ssam			print_txpow(&achans.ic_chans[i]);
3224170531Ssam			printf("\n");
3225170531Ssam		}
3226170531Ssam	} else {
3227170531Ssam		for (i = 0; i < achans.ic_nchans; i++) {
3228170531Ssam			print_txpow_verbose(&achans.ic_chans[i]);
3229170531Ssam			printf("\n");
3230170531Ssam		}
3231170531Ssam	}
3232170531Ssam}
3233170531Ssam
3234170531Ssamstatic void
3235138593Ssamlist_keys(int s)
3236138593Ssam{
3237138593Ssam}
3238138593Ssam
3239138593Ssam#define	IEEE80211_C_BITS \
3240181102Ssam	"\20\1STA\7FF\10TURBOP\11IBSS\12PMGT" \
3241178354Ssam	"\13HOSTAP\14AHDEMO\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE" \
3242181102Ssam	"\21MONITOR\22DFS\30WPA1\31WPA2\32BURST\33WME\34WDS\36BGSCAN" \
3243186904Ssam	"\37TXFRAG\40TDMA"
3244138593Ssam
3245178354Ssam#define	IEEE80211_CRYPTO_BITS \
3246178354Ssam	"\20\1WEP\2TKIP\3AES\4AES_CCM\5TKIPMIC\6CKIP\12PMGT"
3247178354Ssam
3248178354Ssam#define	IEEE80211_HTCAP_BITS \
3249178354Ssam	"\20\1LDPC\2CHWIDTH40\5GREENFIELD\6SHORTGI20\7SHORTGI40\10TXSTBC" \
3250178354Ssam	"\21AMPDU\22AMSDU\23HT"
3251178354Ssam
3252138593Ssamstatic void
3253138593Ssamlist_capabilities(int s)
3254138593Ssam{
3255178354Ssam	struct ieee80211_devcaps_req dc;
3256138593Ssam
3257178354Ssam	getdevcaps(s, &dc);
3258178354Ssam	printb("drivercaps", dc.dc_drivercaps, IEEE80211_C_BITS);
3259178354Ssam	if (dc.dc_cryptocaps != 0 || verbose) {
3260178354Ssam		putchar('\n');
3261178354Ssam		printb("cryptocaps", dc.dc_cryptocaps, IEEE80211_CRYPTO_BITS);
3262178354Ssam	}
3263178354Ssam	if (dc.dc_htcaps != 0 || verbose) {
3264178354Ssam		putchar('\n');
3265178354Ssam		printb("htcaps", dc.dc_htcaps, IEEE80211_HTCAP_BITS);
3266178354Ssam	}
3267138593Ssam	putchar('\n');
3268138593Ssam}
3269138593Ssam
3270173275Ssamstatic int
3271173275Ssamget80211wme(int s, int param, int ac, int *val)
3272173275Ssam{
3273173275Ssam	struct ieee80211req ireq;
3274173275Ssam
3275173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3276173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3277173275Ssam	ireq.i_type = param;
3278173275Ssam	ireq.i_len = ac;
3279173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3280173275Ssam		warn("cannot get WME parameter %d, ac %d%s",
3281173275Ssam		    param, ac & IEEE80211_WMEPARAM_VAL,
3282173275Ssam		    ac & IEEE80211_WMEPARAM_BSS ? " (BSS)" : "");
3283173275Ssam		return -1;
3284173275Ssam	}
3285173275Ssam	*val = ireq.i_val;
3286173275Ssam	return 0;
3287173275Ssam}
3288173275Ssam
3289138593Ssamstatic void
3290181199Ssamlist_wme_aci(int s, const char *tag, int ac)
3291138593Ssam{
3292181199Ssam	int val;
3293138593Ssam
3294181199Ssam	printf("\t%s", tag);
3295138593Ssam
3296181199Ssam	/* show WME BSS parameters */
3297181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMIN, ac, &val) != -1)
3298181199Ssam		printf(" cwmin %2u", val);
3299181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMAX, ac, &val) != -1)
3300181199Ssam		printf(" cwmax %2u", val);
3301181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_AIFS, ac, &val) != -1)
3302181199Ssam		printf(" aifs %2u", val);
3303181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_TXOPLIMIT, ac, &val) != -1)
3304181199Ssam		printf(" txopLimit %3u", val);
3305181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_ACM, ac, &val) != -1) {
3306181199Ssam		if (val)
3307181199Ssam			printf(" acm");
3308181199Ssam		else if (verbose)
3309181199Ssam			printf(" -acm");
3310181199Ssam	}
3311181199Ssam	/* !BSS only */
3312181199Ssam	if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3313181199Ssam		if (get80211wme(s, IEEE80211_IOC_WME_ACKPOLICY, ac, &val) != -1) {
3314181199Ssam			if (!val)
3315181199Ssam				printf(" -ack");
3316138593Ssam			else if (verbose)
3317181199Ssam				printf(" ack");
3318138593Ssam		}
3319181199Ssam	}
3320181199Ssam	printf("\n");
3321181199Ssam}
3322181199Ssam
3323181199Ssamstatic void
3324181199Ssamlist_wme(int s)
3325181199Ssam{
3326181199Ssam	static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
3327181199Ssam	int ac;
3328181199Ssam
3329181199Ssam	if (verbose) {
3330181199Ssam		/* display both BSS and local settings */
3331181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
3332181199Ssam	again:
3333181199Ssam			if (ac & IEEE80211_WMEPARAM_BSS)
3334181199Ssam				list_wme_aci(s, "     ", ac);
3335181199Ssam			else
3336181199Ssam				list_wme_aci(s, acnames[ac], ac);
3337181199Ssam			if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3338181199Ssam				ac |= IEEE80211_WMEPARAM_BSS;
3339181199Ssam				goto again;
3340181199Ssam			} else
3341181199Ssam				ac &= ~IEEE80211_WMEPARAM_BSS;
3342138593Ssam		}
3343181199Ssam	} else {
3344181199Ssam		/* display only channel settings */
3345181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++)
3346181199Ssam			list_wme_aci(s, acnames[ac], ac);
3347138593Ssam	}
3348138593Ssam}
3349138593Ssam
3350149029Ssamstatic void
3351178354Ssamlist_roam(int s)
3352178354Ssam{
3353178354Ssam	const struct ieee80211_roamparam *rp;
3354178354Ssam	int mode;
3355178354Ssam
3356178354Ssam	getroam(s);
3357178354Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_11NA; mode++) {
3358178354Ssam		rp = &roamparams.params[mode];
3359178354Ssam		if (rp->rssi == 0 && rp->rate == 0)
3360178354Ssam			continue;
3361178354Ssam		if (rp->rssi & 1)
3362178354Ssam			LINE_CHECK("roam:%-6.6s rssi %2u.5dBm rate %2u Mb/s",
3363178354Ssam			    modename[mode], rp->rssi/2, rp->rate/2);
3364178354Ssam		else
3365178354Ssam			LINE_CHECK("roam:%-6.6s rssi %4udBm rate %2u Mb/s",
3366178354Ssam			    modename[mode], rp->rssi/2, rp->rate/2);
3367178354Ssam	}
3368178354Ssam	for (; mode < IEEE80211_MODE_MAX; mode++) {
3369178354Ssam		rp = &roamparams.params[mode];
3370178354Ssam		if (rp->rssi == 0 && rp->rate == 0)
3371178354Ssam			continue;
3372178354Ssam		if (rp->rssi & 1)
3373178354Ssam			LINE_CHECK("roam:%-6.6s rssi %2u.5dBm  MCS %2u    ",
3374178354Ssam			    modename[mode], rp->rssi/2, rp->rate &~ 0x80);
3375178354Ssam		else
3376178354Ssam			LINE_CHECK("roam:%-6.6s rssi %4udBm  MCS %2u    ",
3377178354Ssam			    modename[mode], rp->rssi/2, rp->rate &~ 0x80);
3378178354Ssam	}
3379178354Ssam}
3380178354Ssam
3381178354Ssamstatic void
3382178354Ssamlist_txparams(int s)
3383178354Ssam{
3384178354Ssam	const struct ieee80211_txparam *tp;
3385178354Ssam	int mode;
3386178354Ssam
3387178354Ssam	gettxparams(s);
3388178354Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_11NA; mode++) {
3389178354Ssam		tp = &txparams.params[mode];
3390178354Ssam		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
3391178354Ssam			continue;
3392178354Ssam		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3393178354Ssam			LINE_CHECK("%-6.6s ucast NONE    mgmt %2u Mb/s "
3394178354Ssam			    "mcast %2u Mb/s maxretry %u",
3395178354Ssam			    modename[mode], tp->mgmtrate/2,
3396178354Ssam			    tp->mcastrate/2, tp->maxretry);
3397178354Ssam		else
3398178354Ssam			LINE_CHECK("%-6.6s ucast %2u Mb/s mgmt %2u Mb/s "
3399178354Ssam			    "mcast %2u Mb/s maxretry %u",
3400178354Ssam			    modename[mode], tp->ucastrate/2, tp->mgmtrate/2,
3401178354Ssam			    tp->mcastrate/2, tp->maxretry);
3402178354Ssam	}
3403178354Ssam	for (; mode < IEEE80211_MODE_MAX; mode++) {
3404178354Ssam		tp = &txparams.params[mode];
3405178354Ssam		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
3406178354Ssam			continue;
3407178354Ssam		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3408178354Ssam			LINE_CHECK("%-6.6s ucast NONE    mgmt %2u MCS  "
3409178354Ssam			    "mcast %2u MCS  maxretry %u",
3410178354Ssam			    modename[mode], tp->mgmtrate &~ 0x80,
3411178354Ssam			    tp->mcastrate &~ 0x80, tp->maxretry);
3412178354Ssam		else
3413178354Ssam			LINE_CHECK("%-6.6s ucast %2u MCS  mgmt %2u MCS  "
3414178354Ssam			    "mcast %2u MCS  maxretry %u",
3415178354Ssam			    modename[mode], tp->ucastrate &~ 0x80,
3416178354Ssam			    tp->mgmtrate &~ 0x80,
3417178354Ssam			    tp->mcastrate &~ 0x80, tp->maxretry);
3418178354Ssam	}
3419178354Ssam}
3420178354Ssam
3421178354Ssamstatic void
3422173275Ssamprintpolicy(int policy)
3423173275Ssam{
3424173275Ssam	switch (policy) {
3425173275Ssam	case IEEE80211_MACCMD_POLICY_OPEN:
3426173275Ssam		printf("policy: open\n");
3427173275Ssam		break;
3428173275Ssam	case IEEE80211_MACCMD_POLICY_ALLOW:
3429173275Ssam		printf("policy: allow\n");
3430173275Ssam		break;
3431173275Ssam	case IEEE80211_MACCMD_POLICY_DENY:
3432173275Ssam		printf("policy: deny\n");
3433173275Ssam		break;
3434178354Ssam	case IEEE80211_MACCMD_POLICY_RADIUS:
3435178354Ssam		printf("policy: radius\n");
3436178354Ssam		break;
3437173275Ssam	default:
3438173275Ssam		printf("policy: unknown (%u)\n", policy);
3439173275Ssam		break;
3440173275Ssam	}
3441173275Ssam}
3442173275Ssam
3443173275Ssamstatic void
3444149029Ssamlist_mac(int s)
3445149029Ssam{
3446149029Ssam	struct ieee80211req ireq;
3447149029Ssam	struct ieee80211req_maclist *acllist;
3448173275Ssam	int i, nacls, policy, len;
3449173275Ssam	uint8_t *data;
3450149029Ssam	char c;
3451149029Ssam
3452149029Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3453149029Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
3454149029Ssam	ireq.i_type = IEEE80211_IOC_MACCMD;
3455149029Ssam	ireq.i_val = IEEE80211_MACCMD_POLICY;
3456149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3457149029Ssam		if (errno == EINVAL) {
3458149029Ssam			printf("No acl policy loaded\n");
3459149029Ssam			return;
3460149029Ssam		}
3461149029Ssam		err(1, "unable to get mac policy");
3462149029Ssam	}
3463149029Ssam	policy = ireq.i_val;
3464149029Ssam	if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
3465149029Ssam		c = '*';
3466149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
3467149029Ssam		c = '+';
3468149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
3469149029Ssam		c = '-';
3470178354Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_RADIUS) {
3471178354Ssam		c = 'r';		/* NB: should never have entries */
3472149029Ssam	} else {
3473149029Ssam		printf("policy: unknown (%u)\n", policy);
3474149029Ssam		c = '?';
3475149029Ssam	}
3476173275Ssam	if (verbose || c == '?')
3477173275Ssam		printpolicy(policy);
3478173275Ssam
3479175952Ssam	ireq.i_val = IEEE80211_MACCMD_LIST;
3480175952Ssam	ireq.i_len = 0;
3481175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3482173275Ssam		err(1, "unable to get mac acl list size");
3483175952Ssam	if (ireq.i_len == 0) {		/* NB: no acls */
3484173275Ssam		if (!(verbose || c == '?'))
3485173275Ssam			printpolicy(policy);
3486173275Ssam		return;
3487173275Ssam	}
3488175952Ssam	len = ireq.i_len;
3489173275Ssam
3490173275Ssam	data = malloc(len);
3491173275Ssam	if (data == NULL)
3492173275Ssam		err(1, "out of memory for acl list");
3493173275Ssam
3494175952Ssam	ireq.i_data = data;
3495175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3496173275Ssam		err(1, "unable to get mac acl list");
3497173275Ssam	nacls = len / sizeof(*acllist);
3498173275Ssam	acllist = (struct ieee80211req_maclist *) data;
3499149029Ssam	for (i = 0; i < nacls; i++)
3500149029Ssam		printf("%c%s\n", c, ether_ntoa(
3501149029Ssam			(const struct ether_addr *) acllist[i].ml_macaddr));
3502173275Ssam	free(data);
3503149029Ssam}
3504149029Ssam
3505178354Ssamstatic void
3506178354Ssamprint_regdomain(const struct ieee80211_regdomain *reg, int verb)
3507178354Ssam{
3508178354Ssam	if ((reg->regdomain != 0 &&
3509178354Ssam	    reg->regdomain != reg->country) || verb) {
3510178354Ssam		const struct regdomain *rd =
3511178354Ssam		    lib80211_regdomain_findbysku(getregdata(), reg->regdomain);
3512178354Ssam		if (rd == NULL)
3513178354Ssam			LINE_CHECK("regdomain %d", reg->regdomain);
3514178354Ssam		else
3515178354Ssam			LINE_CHECK("regdomain %s", rd->name);
3516178354Ssam	}
3517178354Ssam	if (reg->country != 0 || verb) {
3518178354Ssam		const struct country *cc =
3519178354Ssam		    lib80211_country_findbycc(getregdata(), reg->country);
3520178354Ssam		if (cc == NULL)
3521178354Ssam			LINE_CHECK("country %d", reg->country);
3522178354Ssam		else
3523178354Ssam			LINE_CHECK("country %s", cc->isoname);
3524178354Ssam	}
3525178354Ssam	if (reg->location == 'I')
3526178354Ssam		LINE_CHECK("indoor");
3527178354Ssam	else if (reg->location == 'O')
3528178354Ssam		LINE_CHECK("outdoor");
3529178354Ssam	else if (verb)
3530178354Ssam		LINE_CHECK("anywhere");
3531178354Ssam	if (reg->ecm)
3532178354Ssam		LINE_CHECK("ecm");
3533178354Ssam	else if (verb)
3534178354Ssam		LINE_CHECK("-ecm");
3535178354Ssam}
3536178354Ssam
3537178354Ssamstatic void
3538178354Ssamlist_regdomain(int s, int channelsalso)
3539178354Ssam{
3540178354Ssam	getregdomain(s);
3541178354Ssam	if (channelsalso) {
3542178354Ssam		getchaninfo(s);
3543178354Ssam		spacer = ':';
3544178354Ssam		print_regdomain(&regdomain, 1);
3545178354Ssam		LINE_BREAK();
3546178354Ssam		print_channels(s, &chaninfo, 1/*allchans*/, 1/*verbose*/);
3547178354Ssam	} else
3548178354Ssam		print_regdomain(&regdomain, verbose);
3549178354Ssam}
3550178354Ssam
3551138593Ssamstatic
3552138593SsamDECL_CMD_FUNC(set80211list, arg, d)
3553138593Ssam{
3554138593Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
3555138593Ssam
3556173275Ssam	LINE_INIT('\t');
3557173275Ssam
3558138593Ssam	if (iseq(arg, "sta"))
3559138593Ssam		list_stations(s);
3560138593Ssam	else if (iseq(arg, "scan") || iseq(arg, "ap"))
3561138593Ssam		list_scan(s);
3562138593Ssam	else if (iseq(arg, "chan") || iseq(arg, "freq"))
3563138593Ssam		list_channels(s, 1);
3564138593Ssam	else if (iseq(arg, "active"))
3565138593Ssam		list_channels(s, 0);
3566138593Ssam	else if (iseq(arg, "keys"))
3567138593Ssam		list_keys(s);
3568138593Ssam	else if (iseq(arg, "caps"))
3569138593Ssam		list_capabilities(s);
3570178354Ssam	else if (iseq(arg, "wme") || iseq(arg, "wmm"))
3571138593Ssam		list_wme(s);
3572149029Ssam	else if (iseq(arg, "mac"))
3573149029Ssam		list_mac(s);
3574170531Ssam	else if (iseq(arg, "txpow"))
3575170531Ssam		list_txpow(s);
3576178354Ssam	else if (iseq(arg, "roam"))
3577178354Ssam		list_roam(s);
3578178354Ssam	else if (iseq(arg, "txparam") || iseq(arg, "txparm"))
3579178354Ssam		list_txparams(s);
3580178354Ssam	else if (iseq(arg, "regdomain"))
3581178354Ssam		list_regdomain(s, 1);
3582178354Ssam	else if (iseq(arg, "countries"))
3583178354Ssam		list_countries();
3584138593Ssam	else
3585138593Ssam		errx(1, "Don't know how to list %s for %s", arg, name);
3586178354Ssam	LINE_BREAK();
3587138593Ssam#undef iseq
3588138593Ssam}
3589138593Ssam
3590138593Ssamstatic enum ieee80211_opmode
3591138593Ssamget80211opmode(int s)
3592138593Ssam{
3593138593Ssam	struct ifmediareq ifmr;
3594138593Ssam
3595138593Ssam	(void) memset(&ifmr, 0, sizeof(ifmr));
3596138593Ssam	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
3597138593Ssam
3598138593Ssam	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
3599186101Ssam		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) {
3600186101Ssam			if (ifmr.ifm_current & IFM_FLAG0)
3601186101Ssam				return IEEE80211_M_AHDEMO;
3602186101Ssam			else
3603186101Ssam				return IEEE80211_M_IBSS;
3604186101Ssam		}
3605138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
3606138593Ssam			return IEEE80211_M_HOSTAP;
3607138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
3608138593Ssam			return IEEE80211_M_MONITOR;
3609138593Ssam	}
3610138593Ssam	return IEEE80211_M_STA;
3611138593Ssam}
3612138593Ssam
3613138593Ssam#if 0
3614138593Ssamstatic void
3615138593Ssamprintcipher(int s, struct ieee80211req *ireq, int keylenop)
3616138593Ssam{
3617138593Ssam	switch (ireq->i_val) {
3618138593Ssam	case IEEE80211_CIPHER_WEP:
3619138593Ssam		ireq->i_type = keylenop;
3620138593Ssam		if (ioctl(s, SIOCG80211, ireq) != -1)
3621138593Ssam			printf("WEP-%s",
3622138593Ssam			    ireq->i_len <= 5 ? "40" :
3623138593Ssam			    ireq->i_len <= 13 ? "104" : "128");
3624138593Ssam		else
3625138593Ssam			printf("WEP");
3626138593Ssam		break;
3627138593Ssam	case IEEE80211_CIPHER_TKIP:
3628138593Ssam		printf("TKIP");
3629138593Ssam		break;
3630138593Ssam	case IEEE80211_CIPHER_AES_OCB:
3631138593Ssam		printf("AES-OCB");
3632138593Ssam		break;
3633138593Ssam	case IEEE80211_CIPHER_AES_CCM:
3634138593Ssam		printf("AES-CCM");
3635138593Ssam		break;
3636138593Ssam	case IEEE80211_CIPHER_CKIP:
3637138593Ssam		printf("CKIP");
3638138593Ssam		break;
3639138593Ssam	case IEEE80211_CIPHER_NONE:
3640138593Ssam		printf("NONE");
3641138593Ssam		break;
3642138593Ssam	default:
3643138593Ssam		printf("UNKNOWN (0x%x)", ireq->i_val);
3644138593Ssam		break;
3645138593Ssam	}
3646138593Ssam}
3647138593Ssam#endif
3648138593Ssam
3649155931Ssamstatic void
3650138593Ssamprintkey(const struct ieee80211req_key *ik)
3651138593Ssam{
3652138593Ssam	static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
3653138593Ssam	int keylen = ik->ik_keylen;
3654138593Ssam	int printcontents;
3655138593Ssam
3656148001Srwatson	printcontents = printkeys &&
3657138593Ssam		(memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
3658138593Ssam	if (printcontents)
3659138593Ssam		LINE_BREAK();
3660138593Ssam	switch (ik->ik_type) {
3661138593Ssam	case IEEE80211_CIPHER_WEP:
3662138593Ssam		/* compatibility */
3663155931Ssam		LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
3664138593Ssam		    keylen <= 5 ? "40-bit" :
3665138593Ssam		    keylen <= 13 ? "104-bit" : "128-bit");
3666138593Ssam		break;
3667138593Ssam	case IEEE80211_CIPHER_TKIP:
3668138593Ssam		if (keylen > 128/8)
3669138593Ssam			keylen -= 128/8;	/* ignore MIC for now */
3670155931Ssam		LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3671138593Ssam		break;
3672138593Ssam	case IEEE80211_CIPHER_AES_OCB:
3673155931Ssam		LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3674138593Ssam		break;
3675138593Ssam	case IEEE80211_CIPHER_AES_CCM:
3676155931Ssam		LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3677138593Ssam		break;
3678138593Ssam	case IEEE80211_CIPHER_CKIP:
3679155931Ssam		LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3680138593Ssam		break;
3681138593Ssam	case IEEE80211_CIPHER_NONE:
3682155931Ssam		LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3683138593Ssam		break;
3684138593Ssam	default:
3685155931Ssam		LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
3686138593Ssam			ik->ik_type, ik->ik_keyix+1, 8*keylen);
3687138593Ssam		break;
3688138593Ssam	}
3689138593Ssam	if (printcontents) {
3690138593Ssam		int i;
3691138593Ssam
3692138593Ssam		printf(" <");
3693138593Ssam		for (i = 0; i < keylen; i++)
3694138593Ssam			printf("%02x", ik->ik_keydata[i]);
3695138593Ssam		printf(">");
3696138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
3697138593Ssam		    (ik->ik_keyrsc != 0 || verbose))
3698146873Sjhb			printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
3699138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
3700138593Ssam		    (ik->ik_keytsc != 0 || verbose))
3701146873Sjhb			printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
3702138593Ssam		if (ik->ik_flags != 0 && verbose) {
3703138593Ssam			const char *sep = " ";
3704138593Ssam
3705138593Ssam			if (ik->ik_flags & IEEE80211_KEY_XMIT)
3706138593Ssam				printf("%stx", sep), sep = "+";
3707138593Ssam			if (ik->ik_flags & IEEE80211_KEY_RECV)
3708138593Ssam				printf("%srx", sep), sep = "+";
3709138593Ssam			if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
3710138593Ssam				printf("%sdef", sep), sep = "+";
3711138593Ssam		}
3712138593Ssam		LINE_BREAK();
3713138593Ssam	}
3714138593Ssam}
3715138593Ssam
3716138593Ssamstatic void
3717173275Ssamprintrate(const char *tag, int v, int defrate, int defmcs)
3718138593Ssam{
3719173275Ssam	if (v == 11)
3720173275Ssam		LINE_CHECK("%s 5.5", tag);
3721173275Ssam	else if (v & 0x80) {
3722173275Ssam		if (v != defmcs)
3723173275Ssam			LINE_CHECK("%s %d", tag, v &~ 0x80);
3724173275Ssam	} else {
3725173275Ssam		if (v != defrate)
3726173275Ssam			LINE_CHECK("%s %d", tag, v/2);
3727173275Ssam	}
3728173275Ssam}
3729173275Ssam
3730173275Ssamstatic int
3731173275Ssamgetssid(int s, int ix, void *data, size_t len, int *plen)
3732173275Ssam{
3733138593Ssam	struct ieee80211req ireq;
3734138593Ssam
3735138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3736138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3737173275Ssam	ireq.i_type = IEEE80211_IOC_SSID;
3738173275Ssam	ireq.i_val = ix;
3739173275Ssam	ireq.i_data = data;
3740173275Ssam	ireq.i_len = len;
3741173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3742173275Ssam		return -1;
3743173275Ssam	*plen = ireq.i_len;
3744173275Ssam	return 0;
3745173275Ssam}
374677218Sphk
3747173275Ssamstatic void
3748173275Ssamieee80211_status(int s)
3749173275Ssam{
3750173275Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
3751173275Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
3752173275Ssam	int i, num, wpa, wme, bgscan, bgscaninterval, val, len, wepmode;
3753173275Ssam	uint8_t data[32];
3754173275Ssam	const struct ieee80211_channel *c;
3755178354Ssam	const struct ieee80211_roamparam *rp;
3756178354Ssam	const struct ieee80211_txparam *tp;
3757173275Ssam
3758173275Ssam	if (getssid(s, -1, data, sizeof(data), &len) < 0) {
3759148686Sstefanf		/* If we can't get the SSID, this isn't an 802.11 device. */
376077218Sphk		return;
376177218Sphk	}
3762173275Ssam
3763173275Ssam	/*
3764173275Ssam	 * Invalidate cached state so printing status for multiple
3765173275Ssam	 * if's doesn't reuse the first interfaces' cached state.
3766173275Ssam	 */
3767173275Ssam	gotcurchan = 0;
3768178354Ssam	gotroam = 0;
3769178354Ssam	gottxparams = 0;
3770173275Ssam	gothtconf = 0;
3771178354Ssam	gotregdomain = 0;
3772173275Ssam
3773173275Ssam	if (get80211val(s, IEEE80211_IOC_NUMSSIDS, &num) < 0)
3774173275Ssam		num = 0;
3775138593Ssam	printf("\tssid ");
3776138593Ssam	if (num > 1) {
3777173275Ssam		for (i = 0; i < num; i++) {
3778173275Ssam			if (getssid(s, i, data, sizeof(data), &len) >= 0 && len > 0) {
3779173275Ssam				printf(" %d:", i + 1);
3780173275Ssam				print_string(data, len);
3781138593Ssam			}
378288748Sambrisko		}
3783138593Ssam	} else
3784173275Ssam		print_string(data, len);
378577218Sphk
3786173275Ssam	c = getcurchan(s);
3787170531Ssam	if (c->ic_freq != IEEE80211_CHAN_ANY) {
3788170531Ssam		char buf[14];
3789170531Ssam		printf(" channel %d (%u Mhz%s)", c->ic_ieee, c->ic_freq,
3790170531Ssam			get_chaninfo(c, 1, buf, sizeof(buf)));
3791138593Ssam	} else if (verbose)
3792138593Ssam		printf(" channel UNDEF");
3793138593Ssam
3794173275Ssam	if (get80211(s, IEEE80211_IOC_BSSID, data, IEEE80211_ADDR_LEN) >= 0 &&
3795173275Ssam	    (memcmp(data, zerobssid, sizeof(zerobssid)) != 0 || verbose))
3796173275Ssam		printf(" bssid %s", ether_ntoa((struct ether_addr *)data));
3797138593Ssam
3798173275Ssam	if (get80211len(s, IEEE80211_IOC_STATIONNAME, data, sizeof(data), &len) != -1) {
3799138593Ssam		printf("\n\tstationname ");
3800173275Ssam		print_string(data, len);
380177218Sphk	}
380277218Sphk
3803138593Ssam	spacer = ' ';		/* force first break */
3804138593Ssam	LINE_BREAK();
380577218Sphk
3806178354Ssam	list_regdomain(s, 0);
3807178354Ssam
3808173275Ssam	wpa = 0;
3809173275Ssam	if (get80211val(s, IEEE80211_IOC_AUTHMODE, &val) != -1) {
3810173275Ssam		switch (val) {
3811173275Ssam		case IEEE80211_AUTH_NONE:
3812173275Ssam			LINE_CHECK("authmode NONE");
3813173275Ssam			break;
3814173275Ssam		case IEEE80211_AUTH_OPEN:
3815173275Ssam			LINE_CHECK("authmode OPEN");
3816173275Ssam			break;
3817173275Ssam		case IEEE80211_AUTH_SHARED:
3818173275Ssam			LINE_CHECK("authmode SHARED");
3819173275Ssam			break;
3820173275Ssam		case IEEE80211_AUTH_8021X:
3821173275Ssam			LINE_CHECK("authmode 802.1x");
3822173275Ssam			break;
3823173275Ssam		case IEEE80211_AUTH_WPA:
3824173275Ssam			if (get80211val(s, IEEE80211_IOC_WPA, &wpa) < 0)
3825173275Ssam				wpa = 1;	/* default to WPA1 */
3826173275Ssam			switch (wpa) {
3827173275Ssam			case 2:
3828173275Ssam				LINE_CHECK("authmode WPA2/802.11i");
382977218Sphk				break;
3830173275Ssam			case 3:
3831173275Ssam				LINE_CHECK("authmode WPA1+WPA2/802.11i");
383277218Sphk				break;
3833127649Ssam			default:
3834173275Ssam				LINE_CHECK("authmode WPA");
3835127649Ssam				break;
3836173275Ssam			}
3837173275Ssam			break;
3838173275Ssam		case IEEE80211_AUTH_AUTO:
3839173275Ssam			LINE_CHECK("authmode AUTO");
3840173275Ssam			break;
3841173275Ssam		default:
3842173275Ssam			LINE_CHECK("authmode UNKNOWN (0x%x)", val);
3843173275Ssam			break;
3844127649Ssam		}
3845127649Ssam	}
3846127649Ssam
3847173275Ssam	if (wpa || verbose) {
3848178354Ssam		if (get80211val(s, IEEE80211_IOC_WPS, &val) != -1) {
3849178354Ssam			if (val)
3850178354Ssam				LINE_CHECK("wps");
3851178354Ssam			else if (verbose)
3852178354Ssam				LINE_CHECK("-wps");
3853178354Ssam		}
3854178354Ssam		if (get80211val(s, IEEE80211_IOC_TSN, &val) != -1) {
3855178354Ssam			if (val)
3856178354Ssam				LINE_CHECK("tsn");
3857178354Ssam			else if (verbose)
3858178354Ssam				LINE_CHECK("-tsn");
3859178354Ssam		}
3860173275Ssam		if (ioctl(s, IEEE80211_IOC_COUNTERMEASURES, &val) != -1) {
3861173275Ssam			if (val)
3862173275Ssam				LINE_CHECK("countermeasures");
3863173275Ssam			else if (verbose)
3864173275Ssam				LINE_CHECK("-countermeasures");
3865173275Ssam		}
3866178354Ssam#if 0
3867178354Ssam		/* XXX not interesting with WPA done in user space */
3868178354Ssam		ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
3869178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3870178354Ssam		}
3871178354Ssam
3872178354Ssam		ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
3873178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3874178354Ssam			LINE_CHECK("mcastcipher ");
3875178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
3876178354Ssam			spacer = ' ';
3877178354Ssam		}
3878178354Ssam
3879178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
3880178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3881178354Ssam			LINE_CHECK("ucastcipher ");
3882178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
3883178354Ssam		}
3884178354Ssam
3885178354Ssam		if (wpa & 2) {
3886178354Ssam			ireq.i_type = IEEE80211_IOC_RSNCAPS;
3887178354Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1) {
3888178354Ssam				LINE_CHECK("RSN caps 0x%x", ireq.i_val);
3889178354Ssam				spacer = ' ';
3890178354Ssam			}
3891178354Ssam		}
3892178354Ssam
3893178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
3894178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
3895178354Ssam		}
3896178354Ssam#endif
3897173275Ssam	}
3898138593Ssam
3899173275Ssam	if (get80211val(s, IEEE80211_IOC_WEP, &wepmode) != -1 &&
3900173275Ssam	    wepmode != IEEE80211_WEP_NOSUP) {
3901173275Ssam		int firstkey;
3902173275Ssam
3903138718Ssam		switch (wepmode) {
3904173275Ssam		case IEEE80211_WEP_OFF:
3905173275Ssam			LINE_CHECK("privacy OFF");
3906173275Ssam			break;
3907173275Ssam		case IEEE80211_WEP_ON:
3908173275Ssam			LINE_CHECK("privacy ON");
3909173275Ssam			break;
3910173275Ssam		case IEEE80211_WEP_MIXED:
3911173275Ssam			LINE_CHECK("privacy MIXED");
3912173275Ssam			break;
3913173275Ssam		default:
3914173275Ssam			LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
3915173275Ssam			break;
391677218Sphk		}
391777218Sphk
391877218Sphk		/*
391977218Sphk		 * If we get here then we've got WEP support so we need
392077218Sphk		 * to print WEP status.
392191454Sbrooks		 */
392277218Sphk
3923173275Ssam		if (get80211val(s, IEEE80211_IOC_WEPTXKEY, &val) < 0) {
392477218Sphk			warn("WEP support, but no tx key!");
392577218Sphk			goto end;
392677218Sphk		}
3927173275Ssam		if (val != -1)
3928173275Ssam			LINE_CHECK("deftxkey %d", val+1);
3929138718Ssam		else if (wepmode != IEEE80211_WEP_OFF || verbose)
3930155931Ssam			LINE_CHECK("deftxkey UNDEF");
393177218Sphk
3932173275Ssam		if (get80211val(s, IEEE80211_IOC_NUMWEPKEYS, &num) < 0) {
393377218Sphk			warn("WEP support, but no NUMWEPKEYS support!");
393477218Sphk			goto end;
393577218Sphk		}
393677218Sphk
3937138593Ssam		firstkey = 1;
3938138593Ssam		for (i = 0; i < num; i++) {
3939138593Ssam			struct ieee80211req_key ik;
394077218Sphk
3941138593Ssam			memset(&ik, 0, sizeof(ik));
3942138593Ssam			ik.ik_keyix = i;
3943173275Ssam			if (get80211(s, IEEE80211_IOC_WPAKEY, &ik, sizeof(ik)) < 0) {
394477218Sphk				warn("WEP support, but can get keys!");
394577218Sphk				goto end;
394677218Sphk			}
3947138593Ssam			if (ik.ik_keylen != 0) {
3948138593Ssam				if (verbose)
3949138593Ssam					LINE_BREAK();
3950138593Ssam				printkey(&ik);
3951138593Ssam				firstkey = 0;
3952138593Ssam			}
3953138593Ssam		}
3954173275Ssamend:
3955173275Ssam		;
3956138593Ssam	}
3957138593Ssam
3958173275Ssam	if (get80211val(s, IEEE80211_IOC_POWERSAVE, &val) != -1 &&
3959173275Ssam	    val != IEEE80211_POWERSAVE_NOSUP ) {
3960173275Ssam		if (val != IEEE80211_POWERSAVE_OFF || verbose) {
3961173275Ssam			switch (val) {
3962173275Ssam			case IEEE80211_POWERSAVE_OFF:
3963173275Ssam				LINE_CHECK("powersavemode OFF");
3964173275Ssam				break;
3965173275Ssam			case IEEE80211_POWERSAVE_CAM:
3966173275Ssam				LINE_CHECK("powersavemode CAM");
3967173275Ssam				break;
3968173275Ssam			case IEEE80211_POWERSAVE_PSP:
3969173275Ssam				LINE_CHECK("powersavemode PSP");
3970173275Ssam				break;
3971173275Ssam			case IEEE80211_POWERSAVE_PSP_CAM:
3972173275Ssam				LINE_CHECK("powersavemode PSP-CAM");
3973173275Ssam				break;
3974138593Ssam			}
3975173275Ssam			if (get80211val(s, IEEE80211_IOC_POWERSAVESLEEP, &val) != -1)
3976173275Ssam				LINE_CHECK("powersavesleep %d", val);
3977138593Ssam		}
3978138593Ssam	}
3979138593Ssam
3980173275Ssam	if (get80211val(s, IEEE80211_IOC_TXPOWER, &val) != -1) {
3981173275Ssam		if (val & 1)
3982173275Ssam			LINE_CHECK("txpower %d.5", val/2);
3983173275Ssam		else
3984173275Ssam			LINE_CHECK("txpower %d", val/2);
3985173275Ssam	}
3986138593Ssam	if (verbose) {
3987173275Ssam		if (get80211val(s, IEEE80211_IOC_TXPOWMAX, &val) != -1)
3988173275Ssam			LINE_CHECK("txpowmax %.1f", val/2.);
3989138593Ssam	}
3990138593Ssam
3991178354Ssam	if (get80211val(s, IEEE80211_IOC_DOTD, &val) != -1) {
3992178354Ssam		if (val)
3993178354Ssam			LINE_CHECK("dotd");
3994178354Ssam		else if (verbose)
3995178354Ssam			LINE_CHECK("-dotd");
3996178354Ssam	}
3997178354Ssam
3998173275Ssam	if (get80211val(s, IEEE80211_IOC_RTSTHRESHOLD, &val) != -1) {
3999173275Ssam		if (val != IEEE80211_RTS_MAX || verbose)
4000173275Ssam			LINE_CHECK("rtsthreshold %d", val);
4001138593Ssam	}
4002138593Ssam
4003173275Ssam	if (get80211val(s, IEEE80211_IOC_FRAGTHRESHOLD, &val) != -1) {
4004173275Ssam		if (val != IEEE80211_FRAG_MAX || verbose)
4005173275Ssam			LINE_CHECK("fragthreshold %d", val);
4006170531Ssam	}
4007173275Ssam	if (opmode == IEEE80211_M_STA || verbose) {
4008173275Ssam		if (get80211val(s, IEEE80211_IOC_BMISSTHRESHOLD, &val) != -1) {
4009173275Ssam			if (val != IEEE80211_HWBMISS_MAX || verbose)
4010173275Ssam				LINE_CHECK("bmiss %d", val);
4011153354Ssam		}
4012153354Ssam	}
4013153354Ssam
4014178354Ssam	if (!verbose) {
4015178354Ssam		gettxparams(s);
4016178354Ssam		tp = &txparams.params[chan2mode(c)];
4017178354Ssam		printrate("ucastrate", tp->ucastrate,
4018178354Ssam		    IEEE80211_FIXED_RATE_NONE, IEEE80211_FIXED_RATE_NONE);
4019178354Ssam		printrate("mcastrate", tp->mcastrate, 2*1, 0x80|0);
4020178354Ssam		printrate("mgmtrate", tp->mgmtrate, 2*1, 0x80|0);
4021178354Ssam		if (tp->maxretry != 6)		/* XXX */
4022178354Ssam			LINE_CHECK("maxretry %d", tp->maxretry);
4023178354Ssam	} else {
4024178354Ssam		LINE_BREAK();
4025178354Ssam		list_txparams(s);
4026178354Ssam	}
4027170531Ssam
4028173275Ssam	bgscaninterval = -1;
4029173275Ssam	(void) get80211val(s, IEEE80211_IOC_BGSCAN_INTERVAL, &bgscaninterval);
4030173275Ssam
4031173275Ssam	if (get80211val(s, IEEE80211_IOC_SCANVALID, &val) != -1) {
4032173275Ssam		if (val != bgscaninterval || verbose)
4033173275Ssam			LINE_CHECK("scanvalid %u", val);
4034148416Ssam	}
4035148416Ssam
4036173275Ssam	bgscan = 0;
4037173275Ssam	if (get80211val(s, IEEE80211_IOC_BGSCAN, &bgscan) != -1) {
4038173275Ssam		if (bgscan)
4039170531Ssam			LINE_CHECK("bgscan");
4040170531Ssam		else if (verbose)
4041170531Ssam			LINE_CHECK("-bgscan");
4042160687Ssam	}
4043170531Ssam	if (bgscan || verbose) {
4044170531Ssam		if (bgscaninterval != -1)
4045170531Ssam			LINE_CHECK("bgscanintvl %u", bgscaninterval);
4046173275Ssam		if (get80211val(s, IEEE80211_IOC_BGSCAN_IDLE, &val) != -1)
4047173275Ssam			LINE_CHECK("bgscanidle %u", val);
4048178354Ssam		if (!verbose) {
4049178354Ssam			getroam(s);
4050178354Ssam			rp = &roamparams.params[chan2mode(c)];
4051178354Ssam			if (rp->rssi & 1)
4052178354Ssam				LINE_CHECK("roam:rssi %u.5", rp->rssi/2);
4053178354Ssam			else
4054178354Ssam				LINE_CHECK("roam:rssi %u", rp->rssi/2);
4055178354Ssam			LINE_CHECK("roam:rate %u", rp->rate/2);
4056178354Ssam		} else {
4057178354Ssam			LINE_BREAK();
4058178354Ssam			list_roam(s);
4059170531Ssam		}
4060170531Ssam	}
4061160687Ssam
4062165570Ssam	if (IEEE80211_IS_CHAN_ANYG(c) || verbose) {
4063173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREG, &val) != -1) {
4064173275Ssam			if (val)
4065155931Ssam				LINE_CHECK("pureg");
4066147795Ssam			else if (verbose)
4067155931Ssam				LINE_CHECK("-pureg");
4068147795Ssam		}
4069173275Ssam		if (get80211val(s, IEEE80211_IOC_PROTMODE, &val) != -1) {
4070173275Ssam			switch (val) {
4071173275Ssam			case IEEE80211_PROTMODE_OFF:
4072173275Ssam				LINE_CHECK("protmode OFF");
4073173275Ssam				break;
4074173275Ssam			case IEEE80211_PROTMODE_CTS:
4075173275Ssam				LINE_CHECK("protmode CTS");
4076173275Ssam				break;
4077173275Ssam			case IEEE80211_PROTMODE_RTSCTS:
4078173275Ssam				LINE_CHECK("protmode RTSCTS");
4079173275Ssam				break;
4080173275Ssam			default:
4081173275Ssam				LINE_CHECK("protmode UNKNOWN (0x%x)", val);
4082173275Ssam				break;
4083138593Ssam			}
4084138593Ssam		}
4085138593Ssam	}
4086138593Ssam
4087173275Ssam	if (IEEE80211_IS_CHAN_HT(c) || verbose) {
4088173275Ssam		gethtconf(s);
4089173275Ssam		switch (htconf & 3) {
4090173275Ssam		case 0:
4091173275Ssam		case 2:
4092173275Ssam			LINE_CHECK("-ht");
4093173275Ssam			break;
4094173275Ssam		case 1:
4095173275Ssam			LINE_CHECK("ht20");
4096173275Ssam			break;
4097173275Ssam		case 3:
4098173275Ssam			if (verbose)
4099173275Ssam				LINE_CHECK("ht");
4100173275Ssam			break;
4101173275Ssam		}
4102173275Ssam		if (get80211val(s, IEEE80211_IOC_HTCOMPAT, &val) != -1) {
4103173275Ssam			if (!val)
4104173275Ssam				LINE_CHECK("-htcompat");
4105173275Ssam			else if (verbose)
4106173275Ssam				LINE_CHECK("htcompat");
4107173275Ssam		}
4108173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU, &val) != -1) {
4109173275Ssam			switch (val) {
4110173275Ssam			case 0:
4111173275Ssam				LINE_CHECK("-ampdu");
4112173275Ssam				break;
4113173275Ssam			case 1:
4114173275Ssam				LINE_CHECK("ampdutx -ampdurx");
4115173275Ssam				break;
4116173275Ssam			case 2:
4117173275Ssam				LINE_CHECK("-ampdutx ampdurx");
4118173275Ssam				break;
4119173275Ssam			case 3:
4120173275Ssam				if (verbose)
4121173275Ssam					LINE_CHECK("ampdu");
4122173275Ssam				break;
4123173275Ssam			}
4124173275Ssam		}
4125173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_LIMIT, &val) != -1) {
4126173275Ssam			switch (val) {
4127173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_8K:
4128173275Ssam				LINE_CHECK("ampdulimit 8k");
4129173275Ssam				break;
4130173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_16K:
4131173275Ssam				LINE_CHECK("ampdulimit 16k");
4132173275Ssam				break;
4133173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_32K:
4134173275Ssam				LINE_CHECK("ampdulimit 32k");
4135173275Ssam				break;
4136173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_64K:
4137173275Ssam				LINE_CHECK("ampdulimit 64k");
4138173275Ssam				break;
4139173275Ssam			}
4140173275Ssam		}
4141173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_DENSITY, &val) != -1) {
4142173275Ssam			switch (val) {
4143173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_NA:
4144173275Ssam				if (verbose)
4145183260Ssam					LINE_CHECK("ampdudensity NA");
4146173275Ssam				break;
4147173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_025:
4148173275Ssam				LINE_CHECK("ampdudensity .25");
4149173275Ssam				break;
4150173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_05:
4151173275Ssam				LINE_CHECK("ampdudensity .5");
4152173275Ssam				break;
4153173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_1:
4154173275Ssam				LINE_CHECK("ampdudensity 1");
4155173275Ssam				break;
4156173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_2:
4157173275Ssam				LINE_CHECK("ampdudensity 2");
4158173275Ssam				break;
4159173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_4:
4160173275Ssam				LINE_CHECK("ampdudensity 4");
4161173275Ssam				break;
4162173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_8:
4163173275Ssam				LINE_CHECK("ampdudensity 8");
4164173275Ssam				break;
4165173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_16:
4166173275Ssam				LINE_CHECK("ampdudensity 16");
4167173275Ssam				break;
4168173275Ssam			}
4169173275Ssam		}
4170173275Ssam		if (get80211val(s, IEEE80211_IOC_AMSDU, &val) != -1) {
4171173275Ssam			switch (val) {
4172173275Ssam			case 0:
4173173275Ssam				LINE_CHECK("-amsdu");
4174173275Ssam				break;
4175173275Ssam			case 1:
4176173275Ssam				LINE_CHECK("amsdutx -amsdurx");
4177173275Ssam				break;
4178173275Ssam			case 2:
4179173275Ssam				LINE_CHECK("-amsdutx amsdurx");
4180173275Ssam				break;
4181173275Ssam			case 3:
4182173275Ssam				if (verbose)
4183173275Ssam					LINE_CHECK("amsdu");
4184173275Ssam				break;
4185173275Ssam			}
4186173275Ssam		}
4187173275Ssam		/* XXX amsdu limit */
4188173275Ssam		if (get80211val(s, IEEE80211_IOC_SHORTGI, &val) != -1) {
4189173275Ssam			if (val)
4190173275Ssam				LINE_CHECK("shortgi");
4191173275Ssam			else if (verbose)
4192173275Ssam				LINE_CHECK("-shortgi");
4193173275Ssam		}
4194173275Ssam		if (get80211val(s, IEEE80211_IOC_HTPROTMODE, &val) != -1) {
4195173275Ssam			if (val == IEEE80211_PROTMODE_OFF)
4196173275Ssam				LINE_CHECK("htprotmode OFF");
4197173275Ssam			else if (val != IEEE80211_PROTMODE_RTSCTS)
4198173275Ssam				LINE_CHECK("htprotmode UNKNOWN (0x%x)", val);
4199173275Ssam			else if (verbose)
4200173275Ssam				LINE_CHECK("htprotmode RTSCTS");
4201173275Ssam		}
4202173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREN, &val) != -1) {
4203173275Ssam			if (val)
4204173275Ssam				LINE_CHECK("puren");
4205173275Ssam			else if (verbose)
4206173275Ssam				LINE_CHECK("-puren");
4207173275Ssam		}
4208183261Ssam		if (get80211val(s, IEEE80211_IOC_SMPS, &val) != -1) {
4209183261Ssam			if (val == IEEE80211_HTCAP_SMPS_DYNAMIC)
4210183261Ssam				LINE_CHECK("smpsdyn");
4211183261Ssam			else if (val == IEEE80211_HTCAP_SMPS_ENA)
4212183261Ssam				LINE_CHECK("smps");
4213183261Ssam			else if (verbose)
4214183261Ssam				LINE_CHECK("-smps");
4215183261Ssam		}
4216183261Ssam		if (get80211val(s, IEEE80211_IOC_RIFS, &val) != -1) {
4217183261Ssam			if (val)
4218183261Ssam				LINE_CHECK("rifs");
4219183261Ssam			else if (verbose)
4220183261Ssam				LINE_CHECK("-rifs");
4221183261Ssam		}
4222173275Ssam	}
4223173275Ssam
4224173275Ssam	if (get80211val(s, IEEE80211_IOC_WME, &wme) != -1) {
4225138593Ssam		if (wme)
4226155931Ssam			LINE_CHECK("wme");
4227138593Ssam		else if (verbose)
4228155931Ssam			LINE_CHECK("-wme");
4229138593Ssam	} else
4230138593Ssam		wme = 0;
4231138593Ssam
4232173275Ssam	if (get80211val(s, IEEE80211_IOC_BURST, &val) != -1) {
4233173275Ssam		if (val)
4234155931Ssam			LINE_CHECK("burst");
4235153422Ssam		else if (verbose)
4236155931Ssam			LINE_CHECK("-burst");
4237153422Ssam	}
4238153422Ssam
4239173275Ssam	if (get80211val(s, IEEE80211_IOC_FF, &val) != -1) {
4240173275Ssam		if (val)
4241170531Ssam			LINE_CHECK("ff");
4242170531Ssam		else if (verbose)
4243170531Ssam			LINE_CHECK("-ff");
4244170531Ssam	}
4245173275Ssam	if (get80211val(s, IEEE80211_IOC_TURBOP, &val) != -1) {
4246173275Ssam		if (val)
4247170531Ssam			LINE_CHECK("dturbo");
4248170531Ssam		else if (verbose)
4249170531Ssam			LINE_CHECK("-dturbo");
4250170531Ssam	}
4251178354Ssam	if (get80211val(s, IEEE80211_IOC_DWDS, &val) != -1) {
4252178354Ssam		if (val)
4253178354Ssam			LINE_CHECK("dwds");
4254178354Ssam		else if (verbose)
4255178354Ssam			LINE_CHECK("-dwds");
4256178354Ssam	}
4257170531Ssam
4258138593Ssam	if (opmode == IEEE80211_M_HOSTAP) {
4259173275Ssam		if (get80211val(s, IEEE80211_IOC_HIDESSID, &val) != -1) {
4260173275Ssam			if (val)
4261168075Ssam				LINE_CHECK("hidessid");
4262138593Ssam			else if (verbose)
4263168075Ssam				LINE_CHECK("-hidessid");
4264138593Ssam		}
4265173275Ssam		if (get80211val(s, IEEE80211_IOC_APBRIDGE, &val) != -1) {
4266173275Ssam			if (!val)
4267155931Ssam				LINE_CHECK("-apbridge");
4268138593Ssam			else if (verbose)
4269155931Ssam				LINE_CHECK("apbridge");
4270138593Ssam		}
4271173275Ssam		if (get80211val(s, IEEE80211_IOC_DTIM_PERIOD, &val) != -1)
4272173275Ssam			LINE_CHECK("dtimperiod %u", val);
4273138593Ssam
4274173275Ssam		if (get80211val(s, IEEE80211_IOC_DOTH, &val) != -1) {
4275173275Ssam			if (!val)
4276170531Ssam				LINE_CHECK("-doth");
4277170531Ssam			else if (verbose)
4278170531Ssam				LINE_CHECK("doth");
4279170531Ssam		}
4280178354Ssam		if (get80211val(s, IEEE80211_IOC_DFS, &val) != -1) {
4281178354Ssam			if (!val)
4282178354Ssam				LINE_CHECK("-dfs");
4283178354Ssam			else if (verbose)
4284178354Ssam				LINE_CHECK("dfs");
4285178354Ssam		}
4286173275Ssam		if (get80211val(s, IEEE80211_IOC_INACTIVITY, &val) != -1) {
4287173275Ssam			if (!val)
4288173275Ssam				LINE_CHECK("-inact");
4289173275Ssam			else if (verbose)
4290173275Ssam				LINE_CHECK("inact");
4291173275Ssam		}
4292138593Ssam	} else {
4293173275Ssam		if (get80211val(s, IEEE80211_IOC_ROAMING, &val) != -1) {
4294173275Ssam			if (val != IEEE80211_ROAMING_AUTO || verbose) {
4295173275Ssam				switch (val) {
4296138593Ssam				case IEEE80211_ROAMING_DEVICE:
4297155931Ssam					LINE_CHECK("roaming DEVICE");
4298138593Ssam					break;
4299138593Ssam				case IEEE80211_ROAMING_AUTO:
4300155931Ssam					LINE_CHECK("roaming AUTO");
4301138593Ssam					break;
4302138593Ssam				case IEEE80211_ROAMING_MANUAL:
4303155931Ssam					LINE_CHECK("roaming MANUAL");
4304138593Ssam					break;
4305138593Ssam				default:
4306155931Ssam					LINE_CHECK("roaming UNKNOWN (0x%x)",
4307173275Ssam						val);
4308138593Ssam					break;
4309138593Ssam				}
4310138593Ssam			}
4311138593Ssam		}
4312138593Ssam	}
4313186101Ssam
4314186904Ssam	if (opmode == IEEE80211_M_AHDEMO) {
4315186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOT, &val) != -1)
4316186904Ssam			LINE_CHECK("tdmaslot %u", val);
4317186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTCNT, &val) != -1)
4318186904Ssam			LINE_CHECK("tdmaslotcnt %u", val);
4319186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTLEN, &val) != -1)
4320186904Ssam			LINE_CHECK("tdmaslotlen %u", val);
4321186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_BINTERVAL, &val) != -1)
4322186904Ssam			LINE_CHECK("tdmabintval %u", val);
4323186904Ssam	} else if (get80211val(s, IEEE80211_IOC_BEACON_INTERVAL, &val) != -1) {
4324173275Ssam		/* XXX default define not visible */
4325173275Ssam		if (val != 100 || verbose)
4326173275Ssam			LINE_CHECK("bintval %u", val);
4327138593Ssam	}
4328138593Ssam
4329138593Ssam	if (wme && verbose) {
4330138593Ssam		LINE_BREAK();
4331138593Ssam		list_wme(s);
4332138593Ssam	}
4333173275Ssam	LINE_BREAK();
4334173275Ssam}
4335138593Ssam
4336173275Ssamstatic int
4337173275Ssamget80211(int s, int type, void *data, int len)
4338173275Ssam{
4339173275Ssam	struct ieee80211req ireq;
4340138593Ssam
4341173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4342173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4343173275Ssam	ireq.i_type = type;
4344173275Ssam	ireq.i_data = data;
4345173275Ssam	ireq.i_len = len;
4346173275Ssam	return ioctl(s, SIOCG80211, &ireq);
4347173275Ssam}
4348138593Ssam
4349173275Ssamstatic int
4350173275Ssamget80211len(int s, int type, void *data, int len, int *plen)
4351173275Ssam{
4352173275Ssam	struct ieee80211req ireq;
4353138593Ssam
4354173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4355173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4356173275Ssam	ireq.i_type = type;
4357173275Ssam	ireq.i_len = len;
4358173275Ssam	ireq.i_data = data;
4359173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4360173275Ssam		return -1;
4361173275Ssam	*plen = ireq.i_len;
4362173275Ssam	return 0;
4363173275Ssam}
4364138593Ssam
4365173275Ssamstatic int
4366173275Ssamget80211val(int s, int type, int *val)
4367173275Ssam{
4368173275Ssam	struct ieee80211req ireq;
436977218Sphk
4370173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4371173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4372173275Ssam	ireq.i_type = type;
4373173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4374173275Ssam		return -1;
4375173275Ssam	*val = ireq.i_val;
4376173275Ssam	return 0;
437777218Sphk}
437877218Sphk
437977218Sphkstatic void
4380170531Ssamset80211(int s, int type, int val, int len, void *data)
438177218Sphk{
438277218Sphk	struct ieee80211req	ireq;
438377218Sphk
438477218Sphk	(void) memset(&ireq, 0, sizeof(ireq));
438577218Sphk	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
438677218Sphk	ireq.i_type = type;
438777218Sphk	ireq.i_val = val;
438877218Sphk	ireq.i_len = len;
438977218Sphk	ireq.i_data = data;
439091454Sbrooks	if (ioctl(s, SIOCS80211, &ireq) < 0)
439177218Sphk		err(1, "SIOCS80211");
439277218Sphk}
439377218Sphk
439477218Sphkstatic const char *
439577218Sphkget_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
439677218Sphk{
439777218Sphk	int len;
439877218Sphk	int hexstr;
439977218Sphk	u_int8_t *p;
440077218Sphk
440177218Sphk	len = *lenp;
440277218Sphk	p = buf;
440377218Sphk	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
440477218Sphk	if (hexstr)
440577218Sphk		val += 2;
440677218Sphk	for (;;) {
440777218Sphk		if (*val == '\0')
440877218Sphk			break;
440977218Sphk		if (sep != NULL && strchr(sep, *val) != NULL) {
441077218Sphk			val++;
441177218Sphk			break;
441277218Sphk		}
441377218Sphk		if (hexstr) {
4414127831Sphk			if (!isxdigit((u_char)val[0])) {
441577218Sphk				warnx("bad hexadecimal digits");
441677218Sphk				return NULL;
441777218Sphk			}
4418127831Sphk			if (!isxdigit((u_char)val[1])) {
4419127831Sphk				warnx("odd count hexadecimal digits");
4420127831Sphk				return NULL;
4421127831Sphk			}
442277218Sphk		}
4423127831Sphk		if (p >= buf + len) {
442477218Sphk			if (hexstr)
442577218Sphk				warnx("hexadecimal digits too long");
442677218Sphk			else
4427127831Sphk				warnx("string too long");
442877218Sphk			return NULL;
442977218Sphk		}
443077218Sphk		if (hexstr) {
443177218Sphk#define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
443277218Sphk			*p++ = (tohex((u_char)val[0]) << 4) |
443377218Sphk			    tohex((u_char)val[1]);
443477218Sphk#undef tohex
443577218Sphk			val += 2;
443677218Sphk		} else
443777218Sphk			*p++ = *val++;
443877218Sphk	}
443977218Sphk	len = p - buf;
444077218Sphk	/* The string "-" is treated as the empty string. */
4441165045Ssam	if (!hexstr && len == 1 && buf[0] == '-') {
444277218Sphk		len = 0;
4443165045Ssam		memset(buf, 0, *lenp);
4444165045Ssam	} else if (len < *lenp)
444577218Sphk		memset(p, 0, *lenp - len);
444677218Sphk	*lenp = len;
444777218Sphk	return val;
444877218Sphk}
444977218Sphk
445077218Sphkstatic void
445177218Sphkprint_string(const u_int8_t *buf, int len)
445277218Sphk{
445377218Sphk	int i;
445477218Sphk	int hasspc;
445577218Sphk
445677218Sphk	i = 0;
445777218Sphk	hasspc = 0;
445891454Sbrooks	for (; i < len; i++) {
445977218Sphk		if (!isprint(buf[i]) && buf[i] != '\0')
446077218Sphk			break;
446177218Sphk		if (isspace(buf[i]))
446277218Sphk			hasspc++;
446377218Sphk	}
446477218Sphk	if (i == len) {
446577218Sphk		if (hasspc || len == 0 || buf[0] == '\0')
446677218Sphk			printf("\"%.*s\"", len, buf);
446777218Sphk		else
446877218Sphk			printf("%.*s", len, buf);
446977218Sphk	} else {
447077218Sphk		printf("0x");
447177218Sphk		for (i = 0; i < len; i++)
447277218Sphk			printf("%02x", buf[i]);
447377218Sphk	}
447477218Sphk}
447577218Sphk
4476178354Ssam/*
4477178354Ssam * Virtual AP cloning support.
4478178354Ssam */
4479178354Ssamstatic struct ieee80211_clone_params params = {
4480178354Ssam	.icp_opmode	= IEEE80211_M_STA,	/* default to station mode */
4481178354Ssam};
4482178354Ssam
4483178354Ssamstatic void
4484178354Ssamwlan_create(int s, struct ifreq *ifr)
4485178354Ssam{
4486178354Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
4487178354Ssam
4488178354Ssam	if (params.icp_parent[0] == '\0')
4489178354Ssam		errx(1, "must specify a parent when creating a wlan device");
4490178354Ssam	if (params.icp_opmode == IEEE80211_M_WDS &&
4491178354Ssam	    memcmp(params.icp_bssid, zerobssid, sizeof(zerobssid)) == 0)
4492178354Ssam		errx(1, "no bssid specified for WDS (use wlanbssid)");
4493178354Ssam	ifr->ifr_data = (caddr_t) &params;
4494178354Ssam	if (ioctl(s, SIOCIFCREATE2, ifr) < 0)
4495178354Ssam		err(1, "SIOCIFCREATE2");
4496178354Ssam}
4497178354Ssam
4498178354Ssamstatic
4499178354SsamDECL_CMD_FUNC(set80211clone_wlandev, arg, d)
4500178354Ssam{
4501178354Ssam	strlcpy(params.icp_parent, arg, IFNAMSIZ);
4502178354Ssam	clone_setcallback(wlan_create);
4503178354Ssam}
4504178354Ssam
4505178354Ssamstatic
4506178354SsamDECL_CMD_FUNC(set80211clone_wlanbssid, arg, d)
4507178354Ssam{
4508178354Ssam	const struct ether_addr *ea;
4509178354Ssam
4510178354Ssam	ea = ether_aton(arg);
4511178354Ssam	if (ea == NULL)
4512178354Ssam		errx(1, "%s: cannot parse bssid", arg);
4513178354Ssam	memcpy(params.icp_bssid, ea->octet, IEEE80211_ADDR_LEN);
4514178354Ssam	clone_setcallback(wlan_create);
4515178354Ssam}
4516178354Ssam
4517178354Ssamstatic
4518178354SsamDECL_CMD_FUNC(set80211clone_wlanaddr, arg, d)
4519178354Ssam{
4520178354Ssam	const struct ether_addr *ea;
4521178354Ssam
4522178354Ssam	ea = ether_aton(arg);
4523178354Ssam	if (ea == NULL)
4524178354Ssam		errx(1, "%s: cannot parse addres", arg);
4525178354Ssam	memcpy(params.icp_macaddr, ea->octet, IEEE80211_ADDR_LEN);
4526178354Ssam	params.icp_flags |= IEEE80211_CLONE_MACADDR;
4527178354Ssam	clone_setcallback(wlan_create);
4528178354Ssam}
4529178354Ssam
4530178354Ssamstatic
4531178354SsamDECL_CMD_FUNC(set80211clone_wlanmode, arg, d)
4532178354Ssam{
4533178354Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
4534178354Ssam	if (iseq(arg, "sta"))
4535178354Ssam		params.icp_opmode = IEEE80211_M_STA;
4536178354Ssam	else if (iseq(arg, "ahdemo") || iseq(arg, "adhoc-demo"))
4537178354Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
4538178354Ssam	else if (iseq(arg, "ibss") || iseq(arg, "adhoc"))
4539178354Ssam		params.icp_opmode = IEEE80211_M_IBSS;
4540178354Ssam	else if (iseq(arg, "ap") || iseq(arg, "host"))
4541178354Ssam		params.icp_opmode = IEEE80211_M_HOSTAP;
4542178354Ssam	else if (iseq(arg, "wds"))
4543178354Ssam		params.icp_opmode = IEEE80211_M_WDS;
4544178354Ssam	else if (iseq(arg, "monitor"))
4545178354Ssam		params.icp_opmode = IEEE80211_M_MONITOR;
4546186904Ssam	else if (iseq(arg, "tdma")) {
4547186904Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
4548186904Ssam		params.icp_flags |= IEEE80211_CLONE_TDMA;
4549186904Ssam	} else
4550178354Ssam		errx(1, "Don't know to create %s for %s", arg, name);
4551178354Ssam	clone_setcallback(wlan_create);
4552178354Ssam#undef iseq
4553178354Ssam}
4554178354Ssam
4555178354Ssamstatic void
4556178354Ssamset80211clone_beacons(const char *val, int d, int s, const struct afswtch *rafp)
4557178354Ssam{
4558178354Ssam	/* NB: inverted sense */
4559178354Ssam	if (d)
4560178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_NOBEACONS;
4561178354Ssam	else
4562178354Ssam		params.icp_flags |= IEEE80211_CLONE_NOBEACONS;
4563178354Ssam	clone_setcallback(wlan_create);
4564178354Ssam}
4565178354Ssam
4566178354Ssamstatic void
4567178354Ssamset80211clone_bssid(const char *val, int d, int s, const struct afswtch *rafp)
4568178354Ssam{
4569178354Ssam	if (d)
4570178354Ssam		params.icp_flags |= IEEE80211_CLONE_BSSID;
4571178354Ssam	else
4572178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_BSSID;
4573178354Ssam	clone_setcallback(wlan_create);
4574178354Ssam}
4575178354Ssam
4576178354Ssamstatic void
4577178354Ssamset80211clone_wdslegacy(const char *val, int d, int s, const struct afswtch *rafp)
4578178354Ssam{
4579178354Ssam	if (d)
4580178354Ssam		params.icp_flags |= IEEE80211_CLONE_WDSLEGACY;
4581178354Ssam	else
4582178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_WDSLEGACY;
4583178354Ssam	clone_setcallback(wlan_create);
4584178354Ssam}
4585178354Ssam
4586138593Ssamstatic struct cmd ieee80211_cmds[] = {
4587138593Ssam	DEF_CMD_ARG("ssid",		set80211ssid),
4588138593Ssam	DEF_CMD_ARG("nwid",		set80211ssid),
4589138593Ssam	DEF_CMD_ARG("stationname",	set80211stationname),
4590138593Ssam	DEF_CMD_ARG("station",		set80211stationname),	/* BSD/OS */
4591138593Ssam	DEF_CMD_ARG("channel",		set80211channel),
4592138593Ssam	DEF_CMD_ARG("authmode",		set80211authmode),
4593138593Ssam	DEF_CMD_ARG("powersavemode",	set80211powersavemode),
4594138593Ssam	DEF_CMD("powersave",	1,	set80211powersave),
4595138593Ssam	DEF_CMD("-powersave",	0,	set80211powersave),
4596138593Ssam	DEF_CMD_ARG("powersavesleep", 	set80211powersavesleep),
4597138593Ssam	DEF_CMD_ARG("wepmode",		set80211wepmode),
4598138593Ssam	DEF_CMD("wep",		1,	set80211wep),
4599138593Ssam	DEF_CMD("-wep",		0,	set80211wep),
4600139493Ssam	DEF_CMD_ARG("deftxkey",		set80211weptxkey),
4601138593Ssam	DEF_CMD_ARG("weptxkey",		set80211weptxkey),
4602138593Ssam	DEF_CMD_ARG("wepkey",		set80211wepkey),
4603138593Ssam	DEF_CMD_ARG("nwkey",		set80211nwkey),		/* NetBSD */
4604138593Ssam	DEF_CMD("-nwkey",	0,	set80211wep),		/* NetBSD */
4605138593Ssam	DEF_CMD_ARG("rtsthreshold",	set80211rtsthreshold),
4606138593Ssam	DEF_CMD_ARG("protmode",		set80211protmode),
4607138593Ssam	DEF_CMD_ARG("txpower",		set80211txpower),
4608138593Ssam	DEF_CMD_ARG("roaming",		set80211roaming),
4609138593Ssam	DEF_CMD("wme",		1,	set80211wme),
4610138593Ssam	DEF_CMD("-wme",		0,	set80211wme),
4611178354Ssam	DEF_CMD("wmm",		1,	set80211wme),
4612178354Ssam	DEF_CMD("-wmm",		0,	set80211wme),
4613138593Ssam	DEF_CMD("hidessid",	1,	set80211hidessid),
4614138593Ssam	DEF_CMD("-hidessid",	0,	set80211hidessid),
4615138593Ssam	DEF_CMD("apbridge",	1,	set80211apbridge),
4616138593Ssam	DEF_CMD("-apbridge",	0,	set80211apbridge),
4617138593Ssam	DEF_CMD_ARG("chanlist",		set80211chanlist),
4618138593Ssam	DEF_CMD_ARG("bssid",		set80211bssid),
4619138593Ssam	DEF_CMD_ARG("ap",		set80211bssid),
4620138593Ssam	DEF_CMD("scan",	0,		set80211scan),
4621138593Ssam	DEF_CMD_ARG("list",		set80211list),
4622138593Ssam	DEF_CMD_ARG2("cwmin",		set80211cwmin),
4623138593Ssam	DEF_CMD_ARG2("cwmax",		set80211cwmax),
4624138593Ssam	DEF_CMD_ARG2("aifs",		set80211aifs),
4625138593Ssam	DEF_CMD_ARG2("txoplimit",	set80211txoplimit),
4626148621Ssam	DEF_CMD_ARG("acm",		set80211acm),
4627148621Ssam	DEF_CMD_ARG("-acm",		set80211noacm),
4628148621Ssam	DEF_CMD_ARG("ack",		set80211ackpolicy),
4629148621Ssam	DEF_CMD_ARG("-ack",		set80211noackpolicy),
4630138593Ssam	DEF_CMD_ARG2("bss:cwmin",	set80211bsscwmin),
4631138593Ssam	DEF_CMD_ARG2("bss:cwmax",	set80211bsscwmax),
4632138593Ssam	DEF_CMD_ARG2("bss:aifs",	set80211bssaifs),
4633138593Ssam	DEF_CMD_ARG2("bss:txoplimit",	set80211bsstxoplimit),
4634138593Ssam	DEF_CMD_ARG("dtimperiod",	set80211dtimperiod),
4635138593Ssam	DEF_CMD_ARG("bintval",		set80211bintval),
4636138593Ssam	DEF_CMD("mac:open",	IEEE80211_MACCMD_POLICY_OPEN,	set80211maccmd),
4637138593Ssam	DEF_CMD("mac:allow",	IEEE80211_MACCMD_POLICY_ALLOW,	set80211maccmd),
4638138593Ssam	DEF_CMD("mac:deny",	IEEE80211_MACCMD_POLICY_DENY,	set80211maccmd),
4639178354Ssam	DEF_CMD("mac:radius",	IEEE80211_MACCMD_POLICY_RADIUS,	set80211maccmd),
4640138593Ssam	DEF_CMD("mac:flush",	IEEE80211_MACCMD_FLUSH,		set80211maccmd),
4641138593Ssam	DEF_CMD("mac:detach",	IEEE80211_MACCMD_DETACH,	set80211maccmd),
4642138593Ssam	DEF_CMD_ARG("mac:add",		set80211addmac),
4643138593Ssam	DEF_CMD_ARG("mac:del",		set80211delmac),
4644138593Ssam	DEF_CMD_ARG("mac:kick",		set80211kickmac),
4645147795Ssam	DEF_CMD("pureg",	1,	set80211pureg),
4646147795Ssam	DEF_CMD("-pureg",	0,	set80211pureg),
4647170531Ssam	DEF_CMD("ff",		1,	set80211fastframes),
4648170531Ssam	DEF_CMD("-ff",		0,	set80211fastframes),
4649170531Ssam	DEF_CMD("dturbo",	1,	set80211dturbo),
4650170531Ssam	DEF_CMD("-dturbo",	0,	set80211dturbo),
4651170531Ssam	DEF_CMD("bgscan",	1,	set80211bgscan),
4652170531Ssam	DEF_CMD("-bgscan",	0,	set80211bgscan),
4653170531Ssam	DEF_CMD_ARG("bgscanidle",	set80211bgscanidle),
4654170531Ssam	DEF_CMD_ARG("bgscanintvl",	set80211bgscanintvl),
4655170531Ssam	DEF_CMD_ARG("scanvalid",	set80211scanvalid),
4656178354Ssam	DEF_CMD_ARG("roam:rssi",	set80211roamrssi),
4657178354Ssam	DEF_CMD_ARG("roam:rate",	set80211roamrate),
4658153354Ssam	DEF_CMD_ARG("mcastrate",	set80211mcastrate),
4659178354Ssam	DEF_CMD_ARG("ucastrate",	set80211ucastrate),
4660178354Ssam	DEF_CMD_ARG("mgtrate",		set80211mgtrate),
4661178354Ssam	DEF_CMD_ARG("mgmtrate",		set80211mgtrate),
4662178354Ssam	DEF_CMD_ARG("maxretry",		set80211maxretry),
4663148416Ssam	DEF_CMD_ARG("fragthreshold",	set80211fragthreshold),
4664153422Ssam	DEF_CMD("burst",	1,	set80211burst),
4665153422Ssam	DEF_CMD("-burst",	0,	set80211burst),
4666160687Ssam	DEF_CMD_ARG("bmiss",		set80211bmissthreshold),
4667160687Ssam	DEF_CMD_ARG("bmissthreshold",	set80211bmissthreshold),
4668173275Ssam	DEF_CMD("shortgi",	1,	set80211shortgi),
4669173275Ssam	DEF_CMD("-shortgi",	0,	set80211shortgi),
4670173275Ssam	DEF_CMD("ampdurx",	2,	set80211ampdu),
4671173275Ssam	DEF_CMD("-ampdurx",	-2,	set80211ampdu),
4672173275Ssam	DEF_CMD("ampdutx",	1,	set80211ampdu),
4673173275Ssam	DEF_CMD("-ampdutx",	-1,	set80211ampdu),
4674173275Ssam	DEF_CMD("ampdu",	3,	set80211ampdu),		/* NB: tx+rx */
4675173275Ssam	DEF_CMD("-ampdu",	-3,	set80211ampdu),
4676173275Ssam	DEF_CMD_ARG("ampdulimit",	set80211ampdulimit),
4677173275Ssam	DEF_CMD_ARG("ampdudensity",	set80211ampdudensity),
4678173275Ssam	DEF_CMD("amsdurx",	2,	set80211amsdu),
4679173275Ssam	DEF_CMD("-amsdurx",	-2,	set80211amsdu),
4680173275Ssam	DEF_CMD("amsdutx",	1,	set80211amsdu),
4681173275Ssam	DEF_CMD("-amsdutx",	-1,	set80211amsdu),
4682173275Ssam	DEF_CMD("amsdu",	3,	set80211amsdu),		/* NB: tx+rx */
4683173275Ssam	DEF_CMD("-amsdu",	-3,	set80211amsdu),
4684173275Ssam	DEF_CMD_ARG("amsdulimit",	set80211amsdulimit),
4685173275Ssam	DEF_CMD("puren",	1,	set80211puren),
4686173275Ssam	DEF_CMD("-puren",	0,	set80211puren),
4687170531Ssam	DEF_CMD("doth",		1,	set80211doth),
4688170531Ssam	DEF_CMD("-doth",	0,	set80211doth),
4689178354Ssam	DEF_CMD("dfs",		1,	set80211dfs),
4690178354Ssam	DEF_CMD("-dfs",		0,	set80211dfs),
4691173275Ssam	DEF_CMD("htcompat",	1,	set80211htcompat),
4692173275Ssam	DEF_CMD("-htcompat",	0,	set80211htcompat),
4693178354Ssam	DEF_CMD("dwds",		1,	set80211dwds),
4694178354Ssam	DEF_CMD("-dwds",	0,	set80211dwds),
4695173275Ssam	DEF_CMD("inact",	1,	set80211inact),
4696173275Ssam	DEF_CMD("-inact",	0,	set80211inact),
4697178354Ssam	DEF_CMD("tsn",		1,	set80211tsn),
4698178354Ssam	DEF_CMD("-tsn",		0,	set80211tsn),
4699178354Ssam	DEF_CMD_ARG("regdomain",	set80211regdomain),
4700178354Ssam	DEF_CMD_ARG("country",		set80211country),
4701178354Ssam	DEF_CMD("indoor",	'I',	set80211location),
4702178354Ssam	DEF_CMD("-indoor",	'O',	set80211location),
4703178354Ssam	DEF_CMD("outdoor",	'O',	set80211location),
4704178354Ssam	DEF_CMD("-outdoor",	'I',	set80211location),
4705178354Ssam	DEF_CMD("anywhere",	' ',	set80211location),
4706178354Ssam	DEF_CMD("ecm",		1,	set80211ecm),
4707178354Ssam	DEF_CMD("-ecm",		0,	set80211ecm),
4708178354Ssam	DEF_CMD("dotd",		1,	set80211dotd),
4709178354Ssam	DEF_CMD("-dotd",	0,	set80211dotd),
4710173275Ssam	DEF_CMD_ARG("htprotmode",	set80211htprotmode),
4711173275Ssam	DEF_CMD("ht20",		1,	set80211htconf),
4712173275Ssam	DEF_CMD("-ht20",	0,	set80211htconf),
4713173275Ssam	DEF_CMD("ht40",		3,	set80211htconf),	/* NB: 20+40 */
4714173275Ssam	DEF_CMD("-ht40",	0,	set80211htconf),
4715173275Ssam	DEF_CMD("ht",		3,	set80211htconf),	/* NB: 20+40 */
4716173275Ssam	DEF_CMD("-ht",		0,	set80211htconf),
4717183261Ssam	DEF_CMD("rifs",		1,	set80211rifs),
4718183261Ssam	DEF_CMD("-rifs",	0,	set80211rifs),
4719183261Ssam	DEF_CMD("smps",		IEEE80211_HTCAP_SMPS_ENA,	set80211smps),
4720183261Ssam	DEF_CMD("smpsdyn",	IEEE80211_HTCAP_SMPS_DYNAMIC,	set80211smps),
4721183261Ssam	DEF_CMD("-smps",	IEEE80211_HTCAP_SMPS_OFF,	set80211smps),
4722178354Ssam	/* XXX for testing */
4723178354Ssam	DEF_CMD_ARG("chanswitch",	set80211chanswitch),
4724178354Ssam
4725186904Ssam	DEF_CMD_ARG("tdmaslot",		set80211tdmaslot),
4726186904Ssam	DEF_CMD_ARG("tdmaslotcnt",	set80211tdmaslotcnt),
4727186904Ssam	DEF_CMD_ARG("tdmaslotlen",	set80211tdmaslotlen),
4728186904Ssam	DEF_CMD_ARG("tdmabintval",	set80211tdmabintval),
4729186904Ssam
4730178354Ssam	/* vap cloning support */
4731178354Ssam	DEF_CLONE_CMD_ARG("wlanaddr",	set80211clone_wlanaddr),
4732178354Ssam	DEF_CLONE_CMD_ARG("wlanbssid",	set80211clone_wlanbssid),
4733178354Ssam	DEF_CLONE_CMD_ARG("wlandev",	set80211clone_wlandev),
4734178354Ssam	DEF_CLONE_CMD_ARG("wlanmode",	set80211clone_wlanmode),
4735178354Ssam	DEF_CLONE_CMD("beacons", 1,	set80211clone_beacons),
4736178354Ssam	DEF_CLONE_CMD("-beacons", 0,	set80211clone_beacons),
4737178354Ssam	DEF_CLONE_CMD("bssid",	1,	set80211clone_bssid),
4738178354Ssam	DEF_CLONE_CMD("-bssid",	0,	set80211clone_bssid),
4739178354Ssam	DEF_CLONE_CMD("wdslegacy", 1,	set80211clone_wdslegacy),
4740178354Ssam	DEF_CLONE_CMD("-wdslegacy", 0,	set80211clone_wdslegacy),
4741138593Ssam};
4742138593Ssamstatic struct afswtch af_ieee80211 = {
4743138593Ssam	.af_name	= "af_ieee80211",
4744138593Ssam	.af_af		= AF_UNSPEC,
4745139494Ssam	.af_other_status = ieee80211_status,
4746138593Ssam};
4747138593Ssam
4748138593Ssamstatic __constructor void
4749138593Ssamieee80211_ctor(void)
4750138593Ssam{
4751138593Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
4752138593Ssam	int i;
4753138593Ssam
4754138593Ssam	for (i = 0; i < N(ieee80211_cmds);  i++)
4755138593Ssam		cmd_register(&ieee80211_cmds[i]);
4756138593Ssam	af_register(&af_ieee80211);
4757138593Ssam#undef N
4758138593Ssam}
4759