ifieee80211.c revision 298556
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 298556 2016-04-25 00:41:23Z pfg $
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 *
4777218Sphk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
4877218Sphk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
4977218Sphk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5077218Sphk * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
5177218Sphk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5277218Sphk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5377218Sphk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5477218Sphk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
5577218Sphk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5677218Sphk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
5777218Sphk * POSSIBILITY OF SUCH DAMAGE.
5877218Sphk */
5977218Sphk
6077218Sphk#include <sys/param.h>
6177218Sphk#include <sys/ioctl.h>
6277218Sphk#include <sys/socket.h>
6377218Sphk#include <sys/sysctl.h>
6477218Sphk#include <sys/time.h>
6577218Sphk
6677218Sphk#include <net/ethernet.h>
6777218Sphk#include <net/if.h>
6877218Sphk#include <net/if_dl.h>
6977218Sphk#include <net/if_types.h>
70138593Ssam#include <net/if_media.h>
7177218Sphk#include <net/route.h>
72138593Ssam
73116957Ssam#include <net80211/ieee80211_ioctl.h>
74190456Ssam#include <net80211/ieee80211_freebsd.h>
75190456Ssam#include <net80211/ieee80211_superg.h>
76190456Ssam#include <net80211/ieee80211_tdma.h>
77195618Srpaulo#include <net80211/ieee80211_mesh.h>
7877218Sphk
79187801Ssam#include <assert.h>
8077218Sphk#include <ctype.h>
8177218Sphk#include <err.h>
8277218Sphk#include <errno.h>
8377218Sphk#include <fcntl.h>
84146873Sjhb#include <inttypes.h>
8577218Sphk#include <stdio.h>
8677218Sphk#include <stdlib.h>
8777218Sphk#include <string.h>
8877218Sphk#include <unistd.h>
89155931Ssam#include <stdarg.h>
90173275Ssam#include <stddef.h>		/* NB: for offsetof */
9177218Sphk
9277218Sphk#include "ifconfig.h"
9377218Sphk
94291470Sadrian#include <lib80211/lib80211_regdomain.h>
95291470Sadrian#include <lib80211/lib80211_ioctl.h>
96291470Sadrian
97178354Ssam#ifndef IEEE80211_FIXED_RATE_NONE
98178354Ssam#define	IEEE80211_FIXED_RATE_NONE	0xff
99178354Ssam#endif
100178354Ssam
101178354Ssam/* XXX need these publicly defined or similar */
102178354Ssam#ifndef IEEE80211_NODE_AUTH
103194136Ssam#define	IEEE80211_NODE_AUTH	0x000001	/* authorized for data */
104194136Ssam#define	IEEE80211_NODE_QOS	0x000002	/* QoS enabled */
105194136Ssam#define	IEEE80211_NODE_ERP	0x000004	/* ERP enabled */
106194136Ssam#define	IEEE80211_NODE_PWR_MGT	0x000010	/* power save mode enabled */
107194136Ssam#define	IEEE80211_NODE_AREF	0x000020	/* authentication ref held */
108194136Ssam#define	IEEE80211_NODE_HT	0x000040	/* HT enabled */
109194136Ssam#define	IEEE80211_NODE_HTCOMPAT	0x000080	/* HT setup w/ vendor OUI's */
110194136Ssam#define	IEEE80211_NODE_WPS	0x000100	/* WPS association */
111194136Ssam#define	IEEE80211_NODE_TSN	0x000200	/* TSN association */
112194136Ssam#define	IEEE80211_NODE_AMPDU_RX	0x000400	/* AMPDU rx enabled */
113194136Ssam#define	IEEE80211_NODE_AMPDU_TX	0x000800	/* AMPDU tx enabled */
114194136Ssam#define	IEEE80211_NODE_MIMO_PS	0x001000	/* MIMO power save enabled */
115194136Ssam#define	IEEE80211_NODE_MIMO_RTS	0x002000	/* send RTS in MIMO PS */
116194136Ssam#define	IEEE80211_NODE_RIFS	0x004000	/* RIFS enabled */
117194136Ssam#define	IEEE80211_NODE_SGI20	0x008000	/* Short GI in HT20 enabled */
118194136Ssam#define	IEEE80211_NODE_SGI40	0x010000	/* Short GI in HT40 enabled */
119194136Ssam#define	IEEE80211_NODE_ASSOCID	0x020000	/* xmit requires associd */
120194136Ssam#define	IEEE80211_NODE_AMSDU_RX	0x040000	/* AMSDU rx enabled */
121194136Ssam#define	IEEE80211_NODE_AMSDU_TX	0x080000	/* AMSDU tx enabled */
122178354Ssam#endif
123178354Ssam
124187801Ssam#define	MAXCHAN	1536		/* max 1.5K channels */
125187801Ssam
126173275Ssam#define	MAXCOL	78
127173275Ssamstatic	int col;
128173275Ssamstatic	char spacer;
129173275Ssam
130173275Ssamstatic void LINE_INIT(char c);
131173275Ssamstatic void LINE_BREAK(void);
132173275Ssamstatic void LINE_CHECK(const char *fmt, ...);
133173275Ssam
134188784Ssamstatic const char *modename[IEEE80211_MODE_MAX] = {
135188784Ssam	[IEEE80211_MODE_AUTO]	  = "auto",
136188784Ssam	[IEEE80211_MODE_11A]	  = "11a",
137188784Ssam	[IEEE80211_MODE_11B]	  = "11b",
138188784Ssam	[IEEE80211_MODE_11G]	  = "11g",
139188784Ssam	[IEEE80211_MODE_FH]	  = "fh",
140188784Ssam	[IEEE80211_MODE_TURBO_A]  = "turboA",
141188784Ssam	[IEEE80211_MODE_TURBO_G]  = "turboG",
142188784Ssam	[IEEE80211_MODE_STURBO_A] = "sturbo",
143188784Ssam	[IEEE80211_MODE_11NA]	  = "11na",
144188784Ssam	[IEEE80211_MODE_11NG]	  = "11ng",
145188784Ssam	[IEEE80211_MODE_HALF]	  = "half",
146188784Ssam	[IEEE80211_MODE_QUARTER]  = "quarter"
147173275Ssam};
148173275Ssam
149178354Ssamstatic void set80211(int s, int type, int val, int len, void *data);
150173275Ssamstatic int get80211(int s, int type, void *data, int len);
151173275Ssamstatic int get80211len(int s, int type, void *data, int len, int *plen);
152173275Ssamstatic int get80211val(int s, int type, int *val);
15377218Sphkstatic const char *get_string(const char *val, const char *sep,
15477218Sphk    u_int8_t *buf, int *lenp);
15577218Sphkstatic void print_string(const u_int8_t *buf, int len);
156178354Ssamstatic void print_regdomain(const struct ieee80211_regdomain *, int);
157178354Ssamstatic void print_channels(int, const struct ieee80211req_chaninfo *,
158178354Ssam    int allchans, int verbose);
159178354Ssamstatic void regdomain_makechannels(struct ieee80211_regdomain_req *,
160178354Ssam    const struct ieee80211_devcaps_req *);
161195618Srpaulostatic const char *mesh_linkstate_string(uint8_t state);
16277218Sphk
163187801Ssamstatic struct ieee80211req_chaninfo *chaninfo;
164178354Ssamstatic struct ieee80211_regdomain regdomain;
165178354Ssamstatic int gotregdomain = 0;
166178354Ssamstatic struct ieee80211_roamparams_req roamparams;
167178354Ssamstatic int gotroam = 0;
168178354Ssamstatic struct ieee80211_txparams_req txparams;
169178354Ssamstatic int gottxparams = 0;
170173275Ssamstatic struct ieee80211_channel curchan;
171173275Ssamstatic int gotcurchan = 0;
172178354Ssamstatic struct ifmediareq *ifmr;
173173275Ssamstatic int htconf = 0;
174173275Ssamstatic	int gothtconf = 0;
175170531Ssam
176173275Ssamstatic void
177173275Ssamgethtconf(int s)
178173275Ssam{
179173275Ssam	if (gothtconf)
180173275Ssam		return;
181173275Ssam	if (get80211val(s, IEEE80211_IOC_HTCONF, &htconf) < 0)
182173275Ssam		warn("unable to get HT configuration information");
183173275Ssam	gothtconf = 1;
184173275Ssam}
185173275Ssam
186170531Ssam/*
187170531Ssam * Collect channel info from the kernel.  We use this (mostly)
188170531Ssam * to handle mapping between frequency and IEEE channel number.
189170531Ssam */
190170531Ssamstatic void
191170531Ssamgetchaninfo(int s)
192170531Ssam{
193187801Ssam	if (chaninfo != NULL)
194170531Ssam		return;
195187801Ssam	chaninfo = malloc(IEEE80211_CHANINFO_SIZE(MAXCHAN));
196187801Ssam	if (chaninfo == NULL)
197187801Ssam		errx(1, "no space for channel list");
198187801Ssam	if (get80211(s, IEEE80211_IOC_CHANINFO, chaninfo,
199187801Ssam	    IEEE80211_CHANINFO_SIZE(MAXCHAN)) < 0)
200187801Ssam		err(1, "unable to get channel information");
201170531Ssam	ifmr = ifmedia_getstate(s);
202173275Ssam	gethtconf(s);
203170531Ssam}
204170531Ssam
205178354Ssamstatic struct regdata *
206178354Ssamgetregdata(void)
207178354Ssam{
208178354Ssam	static struct regdata *rdp = NULL;
209178354Ssam	if (rdp == NULL) {
210178354Ssam		rdp = lib80211_alloc_regdata();
211178354Ssam		if (rdp == NULL)
212181198Ssam			errx(-1, "missing or corrupted regdomain database");
213178354Ssam	}
214178354Ssam	return rdp;
215178354Ssam}
216178354Ssam
217170531Ssam/*
218170531Ssam * Given the channel at index i with attributes from,
219170531Ssam * check if there is a channel with attributes to in
220170531Ssam * the channel table.  With suitable attributes this
221170531Ssam * allows the caller to look for promotion; e.g. from
222170531Ssam * 11b > 11g.
223170531Ssam */
224138593Ssamstatic int
225170531Ssamcanpromote(int i, int from, int to)
226170531Ssam{
227187801Ssam	const struct ieee80211_channel *fc = &chaninfo->ic_chans[i];
228298556Spfg	u_int j;
229170531Ssam
230170531Ssam	if ((fc->ic_flags & from) != from)
231170531Ssam		return i;
232170531Ssam	/* NB: quick check exploiting ordering of chans w/ same frequency */
233187801Ssam	if (i+1 < chaninfo->ic_nchans &&
234187801Ssam	    chaninfo->ic_chans[i+1].ic_freq == fc->ic_freq &&
235187801Ssam	    (chaninfo->ic_chans[i+1].ic_flags & to) == to)
236170531Ssam		return i+1;
237170531Ssam	/* brute force search in case channel list is not ordered */
238187801Ssam	for (j = 0; j < chaninfo->ic_nchans; j++) {
239187801Ssam		const struct ieee80211_channel *tc = &chaninfo->ic_chans[j];
240170531Ssam		if (j != i &&
241170531Ssam		    tc->ic_freq == fc->ic_freq && (tc->ic_flags & to) == to)
242170531Ssam		return j;
243170531Ssam	}
244170531Ssam	return i;
245170531Ssam}
246170531Ssam
247170531Ssam/*
248170531Ssam * Handle channel promotion.  When a channel is specified with
249170531Ssam * only a frequency we want to promote it to the ``best'' channel
250170531Ssam * available.  The channel list has separate entries for 11b, 11g,
251170531Ssam * 11a, and 11n[ga] channels so specifying a frequency w/o any
252170531Ssam * attributes requires we upgrade, e.g. from 11b -> 11g.  This
253170531Ssam * gets complicated when the channel is specified on the same
254170531Ssam * command line with a media request that constrains the available
255170531Ssam * channe list (e.g. mode 11a); we want to honor that to avoid
256170531Ssam * confusing behaviour.
257170531Ssam */
258170531Ssamstatic int
259170531Ssampromote(int i)
260170531Ssam{
261170531Ssam	/*
262170531Ssam	 * Query the current mode of the interface in case it's
263170531Ssam	 * constrained (e.g. to 11a).  We must do this carefully
264170531Ssam	 * as there may be a pending ifmedia request in which case
265170531Ssam	 * asking the kernel will give us the wrong answer.  This
266170531Ssam	 * is an unfortunate side-effect of the way ifconfig is
267170531Ssam	 * structure for modularity (yech).
268170531Ssam	 *
269170531Ssam	 * NB: ifmr is actually setup in getchaninfo (above); we
270170531Ssam	 *     assume it's called coincident with to this call so
271170531Ssam	 *     we have a ``current setting''; otherwise we must pass
272170531Ssam	 *     the socket descriptor down to here so we can make
273170531Ssam	 *     the ifmedia_getstate call ourselves.
274170531Ssam	 */
275170531Ssam	int chanmode = ifmr != NULL ? IFM_MODE(ifmr->ifm_current) : IFM_AUTO;
276170531Ssam
277170531Ssam	/* when ambiguous promote to ``best'' */
278170531Ssam	/* NB: we abitrarily pick HT40+ over HT40- */
279170531Ssam	if (chanmode != IFM_IEEE80211_11B)
280170531Ssam		i = canpromote(i, IEEE80211_CHAN_B, IEEE80211_CHAN_G);
281173275Ssam	if (chanmode != IFM_IEEE80211_11G && (htconf & 1)) {
282170531Ssam		i = canpromote(i, IEEE80211_CHAN_G,
283170531Ssam			IEEE80211_CHAN_G | IEEE80211_CHAN_HT20);
284173275Ssam		if (htconf & 2) {
285173275Ssam			i = canpromote(i, IEEE80211_CHAN_G,
286173275Ssam				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D);
287173275Ssam			i = canpromote(i, IEEE80211_CHAN_G,
288173275Ssam				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U);
289173275Ssam		}
290170531Ssam	}
291173275Ssam	if (chanmode != IFM_IEEE80211_11A && (htconf & 1)) {
292170531Ssam		i = canpromote(i, IEEE80211_CHAN_A,
293170531Ssam			IEEE80211_CHAN_A | IEEE80211_CHAN_HT20);
294173275Ssam		if (htconf & 2) {
295173275Ssam			i = canpromote(i, IEEE80211_CHAN_A,
296173275Ssam				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D);
297173275Ssam			i = canpromote(i, IEEE80211_CHAN_A,
298173275Ssam				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U);
299173275Ssam		}
300170531Ssam	}
301170531Ssam	return i;
302170531Ssam}
303170531Ssam
304170531Ssamstatic void
305170531Ssammapfreq(struct ieee80211_channel *chan, int freq, int flags)
306170531Ssam{
307298556Spfg	u_int i;
308170531Ssam
309187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
310187801Ssam		const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
311170531Ssam
312170531Ssam		if (c->ic_freq == freq && (c->ic_flags & flags) == flags) {
313170531Ssam			if (flags == 0) {
314170531Ssam				/* when ambiguous promote to ``best'' */
315187801Ssam				c = &chaninfo->ic_chans[promote(i)];
316170531Ssam			}
317170531Ssam			*chan = *c;
318170531Ssam			return;
319170531Ssam		}
320170531Ssam	}
321170531Ssam	errx(1, "unknown/undefined frequency %u/0x%x", freq, flags);
322170531Ssam}
323170531Ssam
324170531Ssamstatic void
325170531Ssammapchan(struct ieee80211_channel *chan, int ieee, int flags)
326170531Ssam{
327298556Spfg	u_int i;
328170531Ssam
329187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
330187801Ssam		const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
331170531Ssam
332170531Ssam		if (c->ic_ieee == ieee && (c->ic_flags & flags) == flags) {
333170531Ssam			if (flags == 0) {
334170531Ssam				/* when ambiguous promote to ``best'' */
335187801Ssam				c = &chaninfo->ic_chans[promote(i)];
336170531Ssam			}
337170531Ssam			*chan = *c;
338170531Ssam			return;
339170531Ssam		}
340170531Ssam	}
341173275Ssam	errx(1, "unknown/undefined channel number %d flags 0x%x", ieee, flags);
342170531Ssam}
343170531Ssam
344173275Ssamstatic const struct ieee80211_channel *
345173275Ssamgetcurchan(int s)
346173275Ssam{
347173275Ssam	if (gotcurchan)
348173275Ssam		return &curchan;
349173275Ssam	if (get80211(s, IEEE80211_IOC_CURCHAN, &curchan, sizeof(curchan)) < 0) {
350173275Ssam		int val;
351173275Ssam		/* fall back to legacy ioctl */
352173275Ssam		if (get80211val(s, IEEE80211_IOC_CHANNEL, &val) < 0)
353187801Ssam			err(-1, "cannot figure out current channel");
354173275Ssam		getchaninfo(s);
355173275Ssam		mapchan(&curchan, val, 0);
356173275Ssam	}
357173275Ssam	gotcurchan = 1;
358173275Ssam	return &curchan;
359173275Ssam}
360173275Ssam
361178354Ssamstatic enum ieee80211_phymode
362178354Ssamchan2mode(const struct ieee80211_channel *c)
363178354Ssam{
364178354Ssam	if (IEEE80211_IS_CHAN_HTA(c))
365178354Ssam		return IEEE80211_MODE_11NA;
366178354Ssam	if (IEEE80211_IS_CHAN_HTG(c))
367178354Ssam		return IEEE80211_MODE_11NG;
368178354Ssam	if (IEEE80211_IS_CHAN_108A(c))
369178354Ssam		return IEEE80211_MODE_TURBO_A;
370178354Ssam	if (IEEE80211_IS_CHAN_108G(c))
371178354Ssam		return IEEE80211_MODE_TURBO_G;
372178354Ssam	if (IEEE80211_IS_CHAN_ST(c))
373178354Ssam		return IEEE80211_MODE_STURBO_A;
374178354Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
375178354Ssam		return IEEE80211_MODE_FH;
376188784Ssam	if (IEEE80211_IS_CHAN_HALF(c))
377188784Ssam		return IEEE80211_MODE_HALF;
378188784Ssam	if (IEEE80211_IS_CHAN_QUARTER(c))
379188784Ssam		return IEEE80211_MODE_QUARTER;
380178354Ssam	if (IEEE80211_IS_CHAN_A(c))
381178354Ssam		return IEEE80211_MODE_11A;
382178354Ssam	if (IEEE80211_IS_CHAN_ANYG(c))
383178354Ssam		return IEEE80211_MODE_11G;
384178354Ssam	if (IEEE80211_IS_CHAN_B(c))
385178354Ssam		return IEEE80211_MODE_11B;
386178354Ssam	return IEEE80211_MODE_AUTO;
387178354Ssam}
388178354Ssam
389178354Ssamstatic void
390178354Ssamgetroam(int s)
391178354Ssam{
392178354Ssam	if (gotroam)
393178354Ssam		return;
394178354Ssam	if (get80211(s, IEEE80211_IOC_ROAM,
395178354Ssam	    &roamparams, sizeof(roamparams)) < 0)
396187801Ssam		err(1, "unable to get roaming parameters");
397178354Ssam	gotroam = 1;
398178354Ssam}
399178354Ssam
400178354Ssamstatic void
401178354Ssamsetroam_cb(int s, void *arg)
402178354Ssam{
403178354Ssam	struct ieee80211_roamparams_req *roam = arg;
404178354Ssam	set80211(s, IEEE80211_IOC_ROAM, 0, sizeof(*roam), roam);
405178354Ssam}
406178354Ssam
407178354Ssamstatic void
408178354Ssamgettxparams(int s)
409178354Ssam{
410178354Ssam	if (gottxparams)
411178354Ssam		return;
412178354Ssam	if (get80211(s, IEEE80211_IOC_TXPARAMS,
413178354Ssam	    &txparams, sizeof(txparams)) < 0)
414187801Ssam		err(1, "unable to get transmit parameters");
415178354Ssam	gottxparams = 1;
416178354Ssam}
417178354Ssam
418178354Ssamstatic void
419178354Ssamsettxparams_cb(int s, void *arg)
420178354Ssam{
421178354Ssam	struct ieee80211_txparams_req *txp = arg;
422178354Ssam	set80211(s, IEEE80211_IOC_TXPARAMS, 0, sizeof(*txp), txp);
423178354Ssam}
424178354Ssam
425178354Ssamstatic void
426178354Ssamgetregdomain(int s)
427178354Ssam{
428178354Ssam	if (gotregdomain)
429178354Ssam		return;
430178354Ssam	if (get80211(s, IEEE80211_IOC_REGDOMAIN,
431178354Ssam	    &regdomain, sizeof(regdomain)) < 0)
432187801Ssam		err(1, "unable to get regulatory domain info");
433178354Ssam	gotregdomain = 1;
434178354Ssam}
435178354Ssam
436178354Ssamstatic void
437178354Ssamgetdevcaps(int s, struct ieee80211_devcaps_req *dc)
438178354Ssam{
439187801Ssam	if (get80211(s, IEEE80211_IOC_DEVCAPS, dc,
440187801Ssam	    IEEE80211_DEVCAPS_SPACE(dc)) < 0)
441187801Ssam		err(1, "unable to get device capabilities");
442178354Ssam}
443178354Ssam
444178354Ssamstatic void
445178354Ssamsetregdomain_cb(int s, void *arg)
446178354Ssam{
447187801Ssam	struct ieee80211_regdomain_req *req;
448178354Ssam	struct ieee80211_regdomain *rd = arg;
449187801Ssam	struct ieee80211_devcaps_req *dc;
450178354Ssam	struct regdata *rdp = getregdata();
451178354Ssam
452186102Ssam	if (rd->country != NO_COUNTRY) {
453178354Ssam		const struct country *cc;
454178354Ssam		/*
455178354Ssam		 * Check current country seting to make sure it's
456178354Ssam		 * compatible with the new regdomain.  If not, then
457178354Ssam		 * override it with any default country for this
458178354Ssam		 * SKU.  If we cannot arrange a match, then abort.
459178354Ssam		 */
460178354Ssam		cc = lib80211_country_findbycc(rdp, rd->country);
461178354Ssam		if (cc == NULL)
462178354Ssam			errx(1, "unknown ISO country code %d", rd->country);
463178354Ssam		if (cc->rd->sku != rd->regdomain) {
464178354Ssam			const struct regdomain *rp;
465178354Ssam			/*
466178354Ssam			 * Check if country is incompatible with regdomain.
467178354Ssam			 * To enable multiple regdomains for a country code
468178354Ssam			 * we permit a mismatch between the regdomain and
469178354Ssam			 * the country's associated regdomain when the
470178354Ssam			 * regdomain is setup w/o a default country.  For
471178354Ssam			 * example, US is bound to the FCC regdomain but
472178354Ssam			 * we allow US to be combined with FCC3 because FCC3
473178354Ssam			 * has not default country.  This allows bogus
474178354Ssam			 * combinations like FCC3+DK which are resolved when
475178354Ssam			 * constructing the channel list by deferring to the
476178354Ssam			 * regdomain to construct the channel list.
477178354Ssam			 */
478178354Ssam			rp = lib80211_regdomain_findbysku(rdp, rd->regdomain);
479178354Ssam			if (rp == NULL)
480178354Ssam				errx(1, "country %s (%s) is not usable with "
481178354Ssam				    "regdomain %d", cc->isoname, cc->name,
482178354Ssam				    rd->regdomain);
483186102Ssam			else if (rp->cc != NULL && rp->cc != cc)
484178354Ssam				errx(1, "country %s (%s) is not usable with "
485178354Ssam				   "regdomain %s", cc->isoname, cc->name,
486178354Ssam				   rp->name);
487178354Ssam		}
488178354Ssam	}
489178354Ssam	/*
490178354Ssam	 * Fetch the device capabilities and calculate the
491178354Ssam	 * full set of netbands for which we request a new
492178354Ssam	 * channel list be constructed.  Once that's done we
493178354Ssam	 * push the regdomain info + channel list to the kernel.
494178354Ssam	 */
495187801Ssam	dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
496187801Ssam	if (dc == NULL)
497187801Ssam		errx(1, "no space for device capabilities");
498187801Ssam	dc->dc_chaninfo.ic_nchans = MAXCHAN;
499187801Ssam	getdevcaps(s, dc);
500178354Ssam#if 0
501178354Ssam	if (verbose) {
502187801Ssam		printf("drivercaps: 0x%x\n", dc->dc_drivercaps);
503187801Ssam		printf("cryptocaps: 0x%x\n", dc->dc_cryptocaps);
504187801Ssam		printf("htcaps    : 0x%x\n", dc->dc_htcaps);
505187801Ssam		memcpy(chaninfo, &dc->dc_chaninfo,
506187801Ssam		    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
507187801Ssam		print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, 1/*verbose*/);
508178354Ssam	}
509178354Ssam#endif
510187801Ssam	req = malloc(IEEE80211_REGDOMAIN_SIZE(dc->dc_chaninfo.ic_nchans));
511187801Ssam	if (req == NULL)
512187801Ssam		errx(1, "no space for regdomain request");
513187801Ssam	req->rd = *rd;
514187801Ssam	regdomain_makechannels(req, dc);
515178354Ssam	if (verbose) {
516178354Ssam		LINE_INIT(':');
517178354Ssam		print_regdomain(rd, 1/*verbose*/);
518178354Ssam		LINE_BREAK();
519187801Ssam		/* blech, reallocate channel list for new data */
520187801Ssam		if (chaninfo != NULL)
521187801Ssam			free(chaninfo);
522187801Ssam		chaninfo = malloc(IEEE80211_CHANINFO_SPACE(&req->chaninfo));
523187801Ssam		if (chaninfo == NULL)
524187801Ssam			errx(1, "no space for channel list");
525187801Ssam		memcpy(chaninfo, &req->chaninfo,
526187801Ssam		    IEEE80211_CHANINFO_SPACE(&req->chaninfo));
527187801Ssam		print_channels(s, &req->chaninfo, 1/*allchans*/, 1/*verbose*/);
528178354Ssam	}
529187801Ssam	if (req->chaninfo.ic_nchans == 0)
530178354Ssam		errx(1, "no channels calculated");
531187801Ssam	set80211(s, IEEE80211_IOC_REGDOMAIN, 0,
532187801Ssam	    IEEE80211_REGDOMAIN_SPACE(req), req);
533187801Ssam	free(req);
534187801Ssam	free(dc);
535178354Ssam}
536178354Ssam
537170531Ssamstatic int
538170531Ssamieee80211_mhz2ieee(int freq, int flags)
539170531Ssam{
540170531Ssam	struct ieee80211_channel chan;
541170531Ssam	mapfreq(&chan, freq, flags);
542170531Ssam	return chan.ic_ieee;
543170531Ssam}
544170531Ssam
545170531Ssamstatic int
546138593Ssamisanyarg(const char *arg)
547138593Ssam{
548173275Ssam	return (strncmp(arg, "-", 1) == 0 ||
549173275Ssam	    strncasecmp(arg, "any", 3) == 0 || strncasecmp(arg, "off", 3) == 0);
550138593Ssam}
551138593Ssam
552138593Ssamstatic void
55377218Sphkset80211ssid(const char *val, int d, int s, const struct afswtch *rafp)
55477218Sphk{
55577218Sphk	int		ssid;
55677218Sphk	int		len;
557151883Sbrooks	u_int8_t	data[IEEE80211_NWID_LEN];
55877218Sphk
55977218Sphk	ssid = 0;
560121827Sbrooks	len = strlen(val);
561178354Ssam	if (len > 2 && isdigit((int)val[0]) && val[1] == ':') {
56288748Sambrisko		ssid = atoi(val)-1;
56388748Sambrisko		val += 2;
56488748Sambrisko	}
56577218Sphk
56677218Sphk	bzero(data, sizeof(data));
56777218Sphk	len = sizeof(data);
568151883Sbrooks	if (get_string(val, NULL, data, &len) == NULL)
569151883Sbrooks		exit(1);
57077218Sphk
57177218Sphk	set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
57277218Sphk}
57377218Sphk
574138593Ssamstatic void
575195618Srpauloset80211meshid(const char *val, int d, int s, const struct afswtch *rafp)
576195618Srpaulo{
577195618Srpaulo	int		len;
578195618Srpaulo	u_int8_t	data[IEEE80211_NWID_LEN];
579195618Srpaulo
580195618Srpaulo	memset(data, 0, sizeof(data));
581195618Srpaulo	len = sizeof(data);
582195618Srpaulo	if (get_string(val, NULL, data, &len) == NULL)
583195618Srpaulo		exit(1);
584195618Srpaulo
585195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_ID, 0, len, data);
586195618Srpaulo}
587195618Srpaulo
588195618Srpaulostatic void
58977218Sphkset80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
59077218Sphk{
59177218Sphk	int			len;
59277218Sphk	u_int8_t		data[33];
59377218Sphk
59477218Sphk	bzero(data, sizeof(data));
59577218Sphk	len = sizeof(data);
59677218Sphk	get_string(val, NULL, data, &len);
59777218Sphk
59877218Sphk	set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
59977218Sphk}
60077218Sphk
601138593Ssam/*
602170531Ssam * Parse a channel specification for attributes/flags.
603170531Ssam * The syntax is:
604170531Ssam *	freq/xx		channel width (5,10,20,40,40+,40-)
605170531Ssam *	freq:mode	channel mode (a,b,g,h,n,t,s,d)
606170531Ssam *
607170531Ssam * These can be combined in either order; e.g. 2437:ng/40.
608170531Ssam * Modes are case insensitive.
609170531Ssam *
610170531Ssam * The result is not validated here; it's assumed to be
611170531Ssam * checked against the channel table fetched from the kernel.
612170531Ssam */
613170531Ssamstatic int
614173275Ssamgetchannelflags(const char *val, int freq)
615138593Ssam{
616170531Ssam#define	_CHAN_HT	0x80000000
617170531Ssam	const char *cp;
618170531Ssam	int flags;
619138593Ssam
620170531Ssam	flags = 0;
621166015Ssam
622170531Ssam	cp = strchr(val, ':');
623170531Ssam	if (cp != NULL) {
624170531Ssam		for (cp++; isalpha((int) *cp); cp++) {
625170531Ssam			/* accept mixed case */
626170531Ssam			int c = *cp;
627170531Ssam			if (isupper(c))
628170531Ssam				c = tolower(c);
629170531Ssam			switch (c) {
630170531Ssam			case 'a':		/* 802.11a */
631170531Ssam				flags |= IEEE80211_CHAN_A;
632170531Ssam				break;
633170531Ssam			case 'b':		/* 802.11b */
634170531Ssam				flags |= IEEE80211_CHAN_B;
635170531Ssam				break;
636170531Ssam			case 'g':		/* 802.11g */
637170531Ssam				flags |= IEEE80211_CHAN_G;
638170531Ssam				break;
639170531Ssam			case 'h':		/* ht = 802.11n */
640170531Ssam			case 'n':		/* 802.11n */
641170531Ssam				flags |= _CHAN_HT;	/* NB: private */
642170531Ssam				break;
643170531Ssam			case 'd':		/* dt = Atheros Dynamic Turbo */
644170531Ssam				flags |= IEEE80211_CHAN_TURBO;
645170531Ssam				break;
646170531Ssam			case 't':		/* ht, dt, st, t */
647170531Ssam				/* dt and unadorned t specify Dynamic Turbo */
648170531Ssam				if ((flags & (IEEE80211_CHAN_STURBO|_CHAN_HT)) == 0)
649170531Ssam					flags |= IEEE80211_CHAN_TURBO;
650170531Ssam				break;
651170531Ssam			case 's':		/* st = Atheros Static Turbo */
652170531Ssam				flags |= IEEE80211_CHAN_STURBO;
653170531Ssam				break;
654170531Ssam			default:
655173275Ssam				errx(-1, "%s: Invalid channel attribute %c\n",
656170531Ssam				    val, *cp);
657170531Ssam			}
658170531Ssam		}
659170531Ssam	}
660170531Ssam	cp = strchr(val, '/');
661170531Ssam	if (cp != NULL) {
662170531Ssam		char *ep;
663170531Ssam		u_long cw = strtoul(cp+1, &ep, 10);
664166015Ssam
665170531Ssam		switch (cw) {
666170531Ssam		case 5:
667170531Ssam			flags |= IEEE80211_CHAN_QUARTER;
668170531Ssam			break;
669170531Ssam		case 10:
670170531Ssam			flags |= IEEE80211_CHAN_HALF;
671170531Ssam			break;
672170531Ssam		case 20:
673170531Ssam			/* NB: this may be removed below */
674170531Ssam			flags |= IEEE80211_CHAN_HT20;
675170531Ssam			break;
676170531Ssam		case 40:
677170531Ssam			if (ep != NULL && *ep == '+')
678170531Ssam				flags |= IEEE80211_CHAN_HT40U;
679170531Ssam			else if (ep != NULL && *ep == '-')
680170531Ssam				flags |= IEEE80211_CHAN_HT40D;
681170531Ssam			break;
682170531Ssam		default:
683173275Ssam			errx(-1, "%s: Invalid channel width\n", val);
684170531Ssam		}
685165570Ssam	}
686170531Ssam	/*
687170531Ssam	 * Cleanup specifications.
688170531Ssam	 */
689170531Ssam	if ((flags & _CHAN_HT) == 0) {
690170531Ssam		/*
691170531Ssam		 * If user specified freq/20 or freq/40 quietly remove
692170531Ssam		 * HT cw attributes depending on channel use.  To give
693170531Ssam		 * an explicit 20/40 width for an HT channel you must
694170531Ssam		 * indicate it is an HT channel since all HT channels
695170531Ssam		 * are also usable for legacy operation; e.g. freq:n/40.
696170531Ssam		 */
697170531Ssam		flags &= ~IEEE80211_CHAN_HT;
698170531Ssam	} else {
699170531Ssam		/*
700170531Ssam		 * Remove private indicator that this is an HT channel
701170531Ssam		 * and if no explicit channel width has been given
702170531Ssam		 * provide the default settings.
703170531Ssam		 */
704170531Ssam		flags &= ~_CHAN_HT;
705173275Ssam		if ((flags & IEEE80211_CHAN_HT) == 0) {
706173275Ssam			struct ieee80211_channel chan;
707173275Ssam			/*
708173275Ssam			 * Consult the channel list to see if we can use
709173275Ssam			 * HT40+ or HT40- (if both the map routines choose).
710173275Ssam			 */
711173275Ssam			if (freq > 255)
712173275Ssam				mapfreq(&chan, freq, 0);
713173275Ssam			else
714173275Ssam				mapchan(&chan, freq, 0);
715173275Ssam			flags |= (chan.ic_flags & IEEE80211_CHAN_HT);
716173275Ssam		}
717170531Ssam	}
718170531Ssam	return flags;
719170531Ssam#undef _CHAN_HT
720138593Ssam}
721138593Ssam
722138593Ssamstatic void
723186105Ssamgetchannel(int s, struct ieee80211_channel *chan, const char *val)
724186105Ssam{
725186105Ssam	int v, flags;
726186105Ssam	char *eptr;
727186105Ssam
728186105Ssam	memset(chan, 0, sizeof(*chan));
729186105Ssam	if (isanyarg(val)) {
730186105Ssam		chan->ic_freq = IEEE80211_CHAN_ANY;
731186105Ssam		return;
732186105Ssam	}
733186105Ssam	getchaninfo(s);
734186105Ssam	errno = 0;
735186105Ssam	v = strtol(val, &eptr, 10);
736186105Ssam	if (val[0] == '\0' || val == eptr || errno == ERANGE ||
737186105Ssam	    /* channel may be suffixed with nothing, :flag, or /width */
738186105Ssam	    (eptr[0] != '\0' && eptr[0] != ':' && eptr[0] != '/'))
739186105Ssam		errx(1, "invalid channel specification%s",
740186105Ssam		    errno == ERANGE ? " (out of range)" : "");
741186105Ssam	flags = getchannelflags(val, v);
742186105Ssam	if (v > 255) {		/* treat as frequency */
743186105Ssam		mapfreq(chan, v, flags);
744186105Ssam	} else {
745186105Ssam		mapchan(chan, v, flags);
746186105Ssam	}
747186105Ssam}
748186105Ssam
749186105Ssamstatic void
75077218Sphkset80211channel(const char *val, int d, int s, const struct afswtch *rafp)
75177218Sphk{
752170531Ssam	struct ieee80211_channel chan;
753170531Ssam
754186105Ssam	getchannel(s, &chan, val);
755170531Ssam	set80211(s, IEEE80211_IOC_CURCHAN, 0, sizeof(chan), &chan);
75677218Sphk}
75777218Sphk
758138593Ssamstatic void
759178354Ssamset80211chanswitch(const char *val, int d, int s, const struct afswtch *rafp)
760178354Ssam{
761178354Ssam	struct ieee80211_chanswitch_req csr;
762178354Ssam
763186105Ssam	getchannel(s, &csr.csa_chan, val);
764178354Ssam	csr.csa_mode = 1;
765178354Ssam	csr.csa_count = 5;
766178354Ssam	set80211(s, IEEE80211_IOC_CHANSWITCH, 0, sizeof(csr), &csr);
767178354Ssam}
768178354Ssam
769178354Ssamstatic void
77077218Sphkset80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
77177218Sphk{
77277218Sphk	int	mode;
77377218Sphk
77491454Sbrooks	if (strcasecmp(val, "none") == 0) {
77577218Sphk		mode = IEEE80211_AUTH_NONE;
77691454Sbrooks	} else if (strcasecmp(val, "open") == 0) {
77777218Sphk		mode = IEEE80211_AUTH_OPEN;
77891454Sbrooks	} else if (strcasecmp(val, "shared") == 0) {
77977218Sphk		mode = IEEE80211_AUTH_SHARED;
780138593Ssam	} else if (strcasecmp(val, "8021x") == 0) {
781138593Ssam		mode = IEEE80211_AUTH_8021X;
782138593Ssam	} else if (strcasecmp(val, "wpa") == 0) {
783138593Ssam		mode = IEEE80211_AUTH_WPA;
78477218Sphk	} else {
785150708Sru		errx(1, "unknown authmode");
78677218Sphk	}
78777218Sphk
78877218Sphk	set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
78977218Sphk}
79077218Sphk
791138593Ssamstatic void
79277218Sphkset80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
79377218Sphk{
79477218Sphk	int	mode;
79577218Sphk
79691454Sbrooks	if (strcasecmp(val, "off") == 0) {
79777218Sphk		mode = IEEE80211_POWERSAVE_OFF;
79891454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
79977218Sphk		mode = IEEE80211_POWERSAVE_ON;
80091454Sbrooks	} else if (strcasecmp(val, "cam") == 0) {
80177218Sphk		mode = IEEE80211_POWERSAVE_CAM;
80291454Sbrooks	} else if (strcasecmp(val, "psp") == 0) {
80377218Sphk		mode = IEEE80211_POWERSAVE_PSP;
80491454Sbrooks	} else if (strcasecmp(val, "psp-cam") == 0) {
80577218Sphk		mode = IEEE80211_POWERSAVE_PSP_CAM;
80677218Sphk	} else {
807150708Sru		errx(1, "unknown powersavemode");
80877218Sphk	}
80977218Sphk
81077218Sphk	set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
81177218Sphk}
81277218Sphk
813138593Ssamstatic void
81477218Sphkset80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
81577218Sphk{
81677218Sphk	if (d == 0)
81777218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
81877218Sphk		    0, NULL);
81977218Sphk	else
82077218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
82177218Sphk		    0, NULL);
82277218Sphk}
82377218Sphk
824138593Ssamstatic void
82577218Sphkset80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
82677218Sphk{
82777218Sphk	set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
82877218Sphk}
82977218Sphk
830138593Ssamstatic void
83177218Sphkset80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
83277218Sphk{
83377218Sphk	int	mode;
83477218Sphk
83591454Sbrooks	if (strcasecmp(val, "off") == 0) {
83677218Sphk		mode = IEEE80211_WEP_OFF;
83791454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
83877218Sphk		mode = IEEE80211_WEP_ON;
83991454Sbrooks	} else if (strcasecmp(val, "mixed") == 0) {
84077218Sphk		mode = IEEE80211_WEP_MIXED;
84177218Sphk	} else {
842150708Sru		errx(1, "unknown wep mode");
84377218Sphk	}
84477218Sphk
84577218Sphk	set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
84677218Sphk}
84777218Sphk
848138593Ssamstatic void
84977218Sphkset80211wep(const char *val, int d, int s, const struct afswtch *rafp)
85077218Sphk{
85177218Sphk	set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
85277218Sphk}
85377218Sphk
854139493Ssamstatic int
855139493Ssamisundefarg(const char *arg)
856139493Ssam{
857139493Ssam	return (strcmp(arg, "-") == 0 || strncasecmp(arg, "undef", 5) == 0);
858139493Ssam}
859139493Ssam
860138593Ssamstatic void
86177218Sphkset80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
86277218Sphk{
863139493Ssam	if (isundefarg(val))
864139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, IEEE80211_KEYIX_NONE, 0, NULL);
865139493Ssam	else
866139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
86777218Sphk}
86877218Sphk
869138593Ssamstatic void
87077218Sphkset80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
87177218Sphk{
87277218Sphk	int		key = 0;
87377218Sphk	int		len;
874120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
87577218Sphk
876178354Ssam	if (isdigit((int)val[0]) && val[1] == ':') {
87777218Sphk		key = atoi(val)-1;
87877218Sphk		val += 2;
87977218Sphk	}
88077218Sphk
88177218Sphk	bzero(data, sizeof(data));
88277218Sphk	len = sizeof(data);
88377218Sphk	get_string(val, NULL, data, &len);
88477218Sphk
88577218Sphk	set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
88677218Sphk}
88777218Sphk
88877218Sphk/*
889229778Suqs * This function is purely a NetBSD compatibility interface.  The NetBSD
890148686Sstefanf * interface is too inflexible, but it's there so we'll support it since
89177218Sphk * it's not all that hard.
89277218Sphk */
893138593Ssamstatic void
89477218Sphkset80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
89577218Sphk{
89677218Sphk	int		txkey;
89777218Sphk	int		i, len;
898120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
89977218Sphk
90077218Sphk	set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
90177218Sphk
902178354Ssam	if (isdigit((int)val[0]) && val[1] == ':') {
90377218Sphk		txkey = val[0]-'0'-1;
90477218Sphk		val += 2;
90577218Sphk
90691454Sbrooks		for (i = 0; i < 4; i++) {
90777218Sphk			bzero(data, sizeof(data));
90877218Sphk			len = sizeof(data);
90977218Sphk			val = get_string(val, ",", data, &len);
910151827Sbrooks			if (val == NULL)
911151827Sbrooks				exit(1);
91277218Sphk
91377218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
91477218Sphk		}
91577218Sphk	} else {
91677218Sphk		bzero(data, sizeof(data));
91777218Sphk		len = sizeof(data);
91877218Sphk		get_string(val, NULL, data, &len);
91977218Sphk		txkey = 0;
92077218Sphk
92177218Sphk		set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
92277218Sphk
92377218Sphk		bzero(data, sizeof(data));
92491454Sbrooks		for (i = 1; i < 4; i++)
92577218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
92677218Sphk	}
92777218Sphk
92877218Sphk	set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
92977218Sphk}
93077218Sphk
931138593Ssamstatic void
932127649Ssamset80211rtsthreshold(const char *val, int d, int s, const struct afswtch *rafp)
933127649Ssam{
934148416Ssam	set80211(s, IEEE80211_IOC_RTSTHRESHOLD,
935148416Ssam		isundefarg(val) ? IEEE80211_RTS_MAX : atoi(val), 0, NULL);
936127649Ssam}
937127649Ssam
938138593Ssamstatic void
939127649Ssamset80211protmode(const char *val, int d, int s, const struct afswtch *rafp)
940127649Ssam{
941127649Ssam	int	mode;
942127649Ssam
943127649Ssam	if (strcasecmp(val, "off") == 0) {
944127649Ssam		mode = IEEE80211_PROTMODE_OFF;
945127649Ssam	} else if (strcasecmp(val, "cts") == 0) {
946127649Ssam		mode = IEEE80211_PROTMODE_CTS;
947173275Ssam	} else if (strncasecmp(val, "rtscts", 3) == 0) {
948127649Ssam		mode = IEEE80211_PROTMODE_RTSCTS;
949127649Ssam	} else {
950150708Sru		errx(1, "unknown protection mode");
951127649Ssam	}
952127649Ssam
953127649Ssam	set80211(s, IEEE80211_IOC_PROTMODE, mode, 0, NULL);
954127649Ssam}
955127649Ssam
956138593Ssamstatic void
957173275Ssamset80211htprotmode(const char *val, int d, int s, const struct afswtch *rafp)
958173275Ssam{
959173275Ssam	int	mode;
960173275Ssam
961173275Ssam	if (strcasecmp(val, "off") == 0) {
962173275Ssam		mode = IEEE80211_PROTMODE_OFF;
963173275Ssam	} else if (strncasecmp(val, "rts", 3) == 0) {
964173275Ssam		mode = IEEE80211_PROTMODE_RTSCTS;
965173275Ssam	} else {
966173275Ssam		errx(1, "unknown protection mode");
967173275Ssam	}
968173275Ssam
969173275Ssam	set80211(s, IEEE80211_IOC_HTPROTMODE, mode, 0, NULL);
970173275Ssam}
971173275Ssam
972173275Ssamstatic void
973127649Ssamset80211txpower(const char *val, int d, int s, const struct afswtch *rafp)
974127649Ssam{
975173275Ssam	double v = atof(val);
976173275Ssam	int txpow;
977173275Ssam
978173275Ssam	txpow = (int) (2*v);
979173275Ssam	if (txpow != 2*v)
980173275Ssam		errx(-1, "invalid tx power (must be .5 dBm units)");
981173275Ssam	set80211(s, IEEE80211_IOC_TXPOWER, txpow, 0, NULL);
982127649Ssam}
983127649Ssam
984138593Ssam#define	IEEE80211_ROAMING_DEVICE	0
985138593Ssam#define	IEEE80211_ROAMING_AUTO		1
986138593Ssam#define	IEEE80211_ROAMING_MANUAL	2
987138593Ssam
988138593Ssamstatic void
989138593Ssamset80211roaming(const char *val, int d, int s, const struct afswtch *rafp)
99077218Sphk{
991138593Ssam	int mode;
99277218Sphk
993138593Ssam	if (strcasecmp(val, "device") == 0) {
994138593Ssam		mode = IEEE80211_ROAMING_DEVICE;
995138593Ssam	} else if (strcasecmp(val, "auto") == 0) {
996138593Ssam		mode = IEEE80211_ROAMING_AUTO;
997138593Ssam	} else if (strcasecmp(val, "manual") == 0) {
998138593Ssam		mode = IEEE80211_ROAMING_MANUAL;
999138593Ssam	} else {
1000150708Sru		errx(1, "unknown roaming mode");
1001138593Ssam	}
1002138593Ssam	set80211(s, IEEE80211_IOC_ROAMING, mode, 0, NULL);
1003138593Ssam}
1004138593Ssam
1005138593Ssamstatic void
1006138593Ssamset80211wme(const char *val, int d, int s, const struct afswtch *rafp)
1007138593Ssam{
1008138593Ssam	set80211(s, IEEE80211_IOC_WME, d, 0, NULL);
1009138593Ssam}
1010138593Ssam
1011138593Ssamstatic void
1012138593Ssamset80211hidessid(const char *val, int d, int s, const struct afswtch *rafp)
1013138593Ssam{
1014138593Ssam	set80211(s, IEEE80211_IOC_HIDESSID, d, 0, NULL);
1015138593Ssam}
1016138593Ssam
1017138593Ssamstatic void
1018138593Ssamset80211apbridge(const char *val, int d, int s, const struct afswtch *rafp)
1019138593Ssam{
1020138593Ssam	set80211(s, IEEE80211_IOC_APBRIDGE, d, 0, NULL);
1021138593Ssam}
1022138593Ssam
1023138593Ssamstatic void
1024170531Ssamset80211fastframes(const char *val, int d, int s, const struct afswtch *rafp)
1025170531Ssam{
1026170531Ssam	set80211(s, IEEE80211_IOC_FF, d, 0, NULL);
1027170531Ssam}
1028170531Ssam
1029170531Ssamstatic void
1030170531Ssamset80211dturbo(const char *val, int d, int s, const struct afswtch *rafp)
1031170531Ssam{
1032170531Ssam	set80211(s, IEEE80211_IOC_TURBOP, d, 0, NULL);
1033170531Ssam}
1034170531Ssam
1035170531Ssamstatic void
1036138593Ssamset80211chanlist(const char *val, int d, int s, const struct afswtch *rafp)
1037138593Ssam{
1038138593Ssam	struct ieee80211req_chanlist chanlist;
1039138593Ssam	char *temp, *cp, *tp;
1040138593Ssam
1041138593Ssam	temp = malloc(strlen(val) + 1);
1042138593Ssam	if (temp == NULL)
1043138593Ssam		errx(1, "malloc failed");
1044138593Ssam	strcpy(temp, val);
1045138593Ssam	memset(&chanlist, 0, sizeof(chanlist));
1046138593Ssam	cp = temp;
1047138593Ssam	for (;;) {
1048173275Ssam		int first, last, f, c;
1049138593Ssam
1050138593Ssam		tp = strchr(cp, ',');
1051138593Ssam		if (tp != NULL)
1052138593Ssam			*tp++ = '\0';
1053138593Ssam		switch (sscanf(cp, "%u-%u", &first, &last)) {
1054138593Ssam		case 1:
1055187801Ssam			if (first > IEEE80211_CHAN_MAX)
1056188038Sdelphij				errx(-1, "channel %u out of range, max %u",
1057187801Ssam					first, IEEE80211_CHAN_MAX);
1058138593Ssam			setbit(chanlist.ic_channels, first);
1059138593Ssam			break;
1060138593Ssam		case 2:
1061187801Ssam			if (first > IEEE80211_CHAN_MAX)
1062188038Sdelphij				errx(-1, "channel %u out of range, max %u",
1063187801Ssam					first, IEEE80211_CHAN_MAX);
1064187801Ssam			if (last > IEEE80211_CHAN_MAX)
1065188038Sdelphij				errx(-1, "channel %u out of range, max %u",
1066187801Ssam					last, IEEE80211_CHAN_MAX);
1067138593Ssam			if (first > last)
1068138593Ssam				errx(-1, "void channel range, %u > %u",
1069138593Ssam					first, last);
1070138593Ssam			for (f = first; f <= last; f++)
1071138593Ssam				setbit(chanlist.ic_channels, f);
1072138593Ssam			break;
1073138593Ssam		}
1074138593Ssam		if (tp == NULL)
1075138593Ssam			break;
1076173275Ssam		c = *tp;
1077173275Ssam		while (isspace(c))
1078138593Ssam			tp++;
1079173275Ssam		if (!isdigit(c))
1080138593Ssam			break;
1081138593Ssam		cp = tp;
1082138593Ssam	}
1083170531Ssam	set80211(s, IEEE80211_IOC_CHANLIST, 0, sizeof(chanlist), &chanlist);
1084138593Ssam}
1085138593Ssam
1086138593Ssamstatic void
1087138593Ssamset80211bssid(const char *val, int d, int s, const struct afswtch *rafp)
1088138593Ssam{
1089138593Ssam
1090138593Ssam	if (!isanyarg(val)) {
1091138593Ssam		char *temp;
1092138593Ssam		struct sockaddr_dl sdl;
1093138593Ssam
1094155702Ssam		temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1095138593Ssam		if (temp == NULL)
1096138593Ssam			errx(1, "malloc failed");
1097138593Ssam		temp[0] = ':';
1098138593Ssam		strcpy(temp + 1, val);
1099138593Ssam		sdl.sdl_len = sizeof(sdl);
1100138593Ssam		link_addr(temp, &sdl);
1101138593Ssam		free(temp);
1102138593Ssam		if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1103138593Ssam			errx(1, "malformed link-level address");
1104138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
1105138593Ssam			IEEE80211_ADDR_LEN, LLADDR(&sdl));
1106138593Ssam	} else {
1107138593Ssam		uint8_t zerobssid[IEEE80211_ADDR_LEN];
1108138593Ssam		memset(zerobssid, 0, sizeof(zerobssid));
1109138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
1110138593Ssam			IEEE80211_ADDR_LEN, zerobssid);
1111138593Ssam	}
1112138593Ssam}
1113138593Ssam
1114138593Ssamstatic int
1115138593Ssamgetac(const char *ac)
1116138593Ssam{
1117138593Ssam	if (strcasecmp(ac, "ac_be") == 0 || strcasecmp(ac, "be") == 0)
1118138593Ssam		return WME_AC_BE;
1119138593Ssam	if (strcasecmp(ac, "ac_bk") == 0 || strcasecmp(ac, "bk") == 0)
1120138593Ssam		return WME_AC_BK;
1121138593Ssam	if (strcasecmp(ac, "ac_vi") == 0 || strcasecmp(ac, "vi") == 0)
1122138593Ssam		return WME_AC_VI;
1123138593Ssam	if (strcasecmp(ac, "ac_vo") == 0 || strcasecmp(ac, "vo") == 0)
1124138593Ssam		return WME_AC_VO;
1125138593Ssam	errx(1, "unknown wme access class %s", ac);
1126138593Ssam}
1127138593Ssam
1128138593Ssamstatic
1129138593SsamDECL_CMD_FUNC2(set80211cwmin, ac, val)
1130138593Ssam{
1131138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val), getac(ac), NULL);
1132138593Ssam}
1133138593Ssam
1134138593Ssamstatic
1135138593SsamDECL_CMD_FUNC2(set80211cwmax, ac, val)
1136138593Ssam{
1137138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val), getac(ac), NULL);
1138138593Ssam}
1139138593Ssam
1140138593Ssamstatic
1141138593SsamDECL_CMD_FUNC2(set80211aifs, ac, val)
1142138593Ssam{
1143138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val), getac(ac), NULL);
1144138593Ssam}
1145138593Ssam
1146138593Ssamstatic
1147138593SsamDECL_CMD_FUNC2(set80211txoplimit, ac, val)
1148138593Ssam{
1149138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), getac(ac), NULL);
1150138593Ssam}
1151138593Ssam
1152138593Ssamstatic
1153148621SsamDECL_CMD_FUNC(set80211acm, ac, d)
1154138593Ssam{
1155148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 1, getac(ac), NULL);
1156138593Ssam}
1157148621Ssamstatic
1158148621SsamDECL_CMD_FUNC(set80211noacm, ac, d)
1159148621Ssam{
1160148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 0, getac(ac), NULL);
1161148621Ssam}
1162138593Ssam
1163138593Ssamstatic
1164148621SsamDECL_CMD_FUNC(set80211ackpolicy, ac, d)
1165138593Ssam{
1166148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 1, getac(ac), NULL);
1167138593Ssam}
1168148621Ssamstatic
1169148621SsamDECL_CMD_FUNC(set80211noackpolicy, ac, d)
1170148621Ssam{
1171148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 0, getac(ac), NULL);
1172148621Ssam}
1173138593Ssam
1174138593Ssamstatic
1175138593SsamDECL_CMD_FUNC2(set80211bsscwmin, ac, val)
1176138593Ssam{
1177138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val),
1178138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1179138593Ssam}
1180138593Ssam
1181138593Ssamstatic
1182138593SsamDECL_CMD_FUNC2(set80211bsscwmax, ac, val)
1183138593Ssam{
1184138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val),
1185138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1186138593Ssam}
1187138593Ssam
1188138593Ssamstatic
1189138593SsamDECL_CMD_FUNC2(set80211bssaifs, ac, val)
1190138593Ssam{
1191138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val),
1192138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1193138593Ssam}
1194138593Ssam
1195138593Ssamstatic
1196138593SsamDECL_CMD_FUNC2(set80211bsstxoplimit, ac, val)
1197138593Ssam{
1198138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val),
1199138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1200138593Ssam}
1201138593Ssam
1202138593Ssamstatic
1203138593SsamDECL_CMD_FUNC(set80211dtimperiod, val, d)
1204138593Ssam{
1205138593Ssam	set80211(s, IEEE80211_IOC_DTIM_PERIOD, atoi(val), 0, NULL);
1206138593Ssam}
1207138593Ssam
1208138593Ssamstatic
1209138593SsamDECL_CMD_FUNC(set80211bintval, val, d)
1210138593Ssam{
1211138593Ssam	set80211(s, IEEE80211_IOC_BEACON_INTERVAL, atoi(val), 0, NULL);
1212138593Ssam}
1213138593Ssam
1214138593Ssamstatic void
1215138593Ssamset80211macmac(int s, int op, const char *val)
1216138593Ssam{
1217138593Ssam	char *temp;
1218138593Ssam	struct sockaddr_dl sdl;
1219138593Ssam
1220155702Ssam	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1221138593Ssam	if (temp == NULL)
1222138593Ssam		errx(1, "malloc failed");
1223138593Ssam	temp[0] = ':';
1224138593Ssam	strcpy(temp + 1, val);
1225138593Ssam	sdl.sdl_len = sizeof(sdl);
1226138593Ssam	link_addr(temp, &sdl);
1227138593Ssam	free(temp);
1228138593Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1229138593Ssam		errx(1, "malformed link-level address");
1230138593Ssam	set80211(s, op, 0, IEEE80211_ADDR_LEN, LLADDR(&sdl));
1231138593Ssam}
1232138593Ssam
1233138593Ssamstatic
1234138593SsamDECL_CMD_FUNC(set80211addmac, val, d)
1235138593Ssam{
1236138593Ssam	set80211macmac(s, IEEE80211_IOC_ADDMAC, val);
1237138593Ssam}
1238138593Ssam
1239138593Ssamstatic
1240138593SsamDECL_CMD_FUNC(set80211delmac, val, d)
1241138593Ssam{
1242138593Ssam	set80211macmac(s, IEEE80211_IOC_DELMAC, val);
1243138593Ssam}
1244138593Ssam
1245138593Ssamstatic
1246149029SsamDECL_CMD_FUNC(set80211kickmac, val, d)
1247149029Ssam{
1248149029Ssam	char *temp;
1249149029Ssam	struct sockaddr_dl sdl;
1250149029Ssam	struct ieee80211req_mlme mlme;
1251149029Ssam
1252155702Ssam	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1253149029Ssam	if (temp == NULL)
1254149029Ssam		errx(1, "malloc failed");
1255149029Ssam	temp[0] = ':';
1256149029Ssam	strcpy(temp + 1, val);
1257149029Ssam	sdl.sdl_len = sizeof(sdl);
1258149029Ssam	link_addr(temp, &sdl);
1259149029Ssam	free(temp);
1260149029Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1261149029Ssam		errx(1, "malformed link-level address");
1262149029Ssam	memset(&mlme, 0, sizeof(mlme));
1263149029Ssam	mlme.im_op = IEEE80211_MLME_DEAUTH;
1264149029Ssam	mlme.im_reason = IEEE80211_REASON_AUTH_EXPIRE;
1265149029Ssam	memcpy(mlme.im_macaddr, LLADDR(&sdl), IEEE80211_ADDR_LEN);
1266170531Ssam	set80211(s, IEEE80211_IOC_MLME, 0, sizeof(mlme), &mlme);
1267149029Ssam}
1268149029Ssam
1269149029Ssamstatic
1270138593SsamDECL_CMD_FUNC(set80211maccmd, val, d)
1271138593Ssam{
1272138593Ssam	set80211(s, IEEE80211_IOC_MACCMD, d, 0, NULL);
1273138593Ssam}
1274138593Ssam
1275147795Ssamstatic void
1276195618Srpauloset80211meshrtmac(int s, int req, const char *val)
1277195618Srpaulo{
1278195618Srpaulo	char *temp;
1279195618Srpaulo	struct sockaddr_dl sdl;
1280195618Srpaulo
1281195618Srpaulo	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1282195618Srpaulo	if (temp == NULL)
1283195618Srpaulo		errx(1, "malloc failed");
1284195618Srpaulo	temp[0] = ':';
1285195618Srpaulo	strcpy(temp + 1, val);
1286195618Srpaulo	sdl.sdl_len = sizeof(sdl);
1287195618Srpaulo	link_addr(temp, &sdl);
1288195618Srpaulo	free(temp);
1289195618Srpaulo	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1290195618Srpaulo		errx(1, "malformed link-level address");
1291195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_RTCMD, req,
1292195618Srpaulo	    IEEE80211_ADDR_LEN, LLADDR(&sdl));
1293195618Srpaulo}
1294195618Srpaulo
1295195618Srpaulostatic
1296195618SrpauloDECL_CMD_FUNC(set80211addmeshrt, val, d)
1297195618Srpaulo{
1298195618Srpaulo	set80211meshrtmac(s, IEEE80211_MESH_RTCMD_ADD, val);
1299195618Srpaulo}
1300195618Srpaulo
1301195618Srpaulostatic
1302195618SrpauloDECL_CMD_FUNC(set80211delmeshrt, val, d)
1303195618Srpaulo{
1304195618Srpaulo	set80211meshrtmac(s, IEEE80211_MESH_RTCMD_DELETE, val);
1305195618Srpaulo}
1306195618Srpaulo
1307195618Srpaulostatic
1308195618SrpauloDECL_CMD_FUNC(set80211meshrtcmd, val, d)
1309195618Srpaulo{
1310195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_RTCMD, d, 0, NULL);
1311195618Srpaulo}
1312195618Srpaulo
1313195618Srpaulostatic
1314195618SrpauloDECL_CMD_FUNC(set80211hwmprootmode, val, d)
1315195618Srpaulo{
1316195618Srpaulo	int mode;
1317195618Srpaulo
1318195618Srpaulo	if (strcasecmp(val, "normal") == 0)
1319195618Srpaulo		mode = IEEE80211_HWMP_ROOTMODE_NORMAL;
1320195618Srpaulo	else if (strcasecmp(val, "proactive") == 0)
1321195618Srpaulo		mode = IEEE80211_HWMP_ROOTMODE_PROACTIVE;
1322195618Srpaulo	else if (strcasecmp(val, "rann") == 0)
1323195618Srpaulo		mode = IEEE80211_HWMP_ROOTMODE_RANN;
1324195618Srpaulo	else
1325195618Srpaulo		mode = IEEE80211_HWMP_ROOTMODE_DISABLED;
1326195618Srpaulo	set80211(s, IEEE80211_IOC_HWMP_ROOTMODE, mode, 0, NULL);
1327195618Srpaulo}
1328195618Srpaulo
1329195618Srpaulostatic
1330195618SrpauloDECL_CMD_FUNC(set80211hwmpmaxhops, val, d)
1331195618Srpaulo{
1332195618Srpaulo	set80211(s, IEEE80211_IOC_HWMP_MAXHOPS, atoi(val), 0, NULL);
1333195618Srpaulo}
1334195618Srpaulo
1335195618Srpaulostatic void
1336147795Ssamset80211pureg(const char *val, int d, int s, const struct afswtch *rafp)
1337147795Ssam{
1338147795Ssam	set80211(s, IEEE80211_IOC_PUREG, d, 0, NULL);
1339147795Ssam}
1340147795Ssam
1341153422Ssamstatic void
1342227336Sadrianset80211quiet(const char *val, int d, int s, const struct afswtch *rafp)
1343227336Sadrian{
1344227336Sadrian	set80211(s, IEEE80211_IOC_QUIET, d, 0, NULL);
1345227336Sadrian}
1346227336Sadrian
1347227336Sadrianstatic
1348227336SadrianDECL_CMD_FUNC(set80211quietperiod, val, d)
1349227336Sadrian{
1350227336Sadrian	set80211(s, IEEE80211_IOC_QUIET_PERIOD, atoi(val), 0, NULL);
1351227336Sadrian}
1352227336Sadrian
1353227336Sadrianstatic
1354227336SadrianDECL_CMD_FUNC(set80211quietcount, val, d)
1355227336Sadrian{
1356227336Sadrian	set80211(s, IEEE80211_IOC_QUIET_COUNT, atoi(val), 0, NULL);
1357227336Sadrian}
1358227336Sadrian
1359227336Sadrianstatic
1360227336SadrianDECL_CMD_FUNC(set80211quietduration, val, d)
1361227336Sadrian{
1362227336Sadrian	set80211(s, IEEE80211_IOC_QUIET_DUR, atoi(val), 0, NULL);
1363227336Sadrian}
1364227336Sadrian
1365227336Sadrianstatic
1366227336SadrianDECL_CMD_FUNC(set80211quietoffset, val, d)
1367227336Sadrian{
1368227336Sadrian	set80211(s, IEEE80211_IOC_QUIET_OFFSET, atoi(val), 0, NULL);
1369227336Sadrian}
1370227336Sadrian
1371227336Sadrianstatic void
1372170531Ssamset80211bgscan(const char *val, int d, int s, const struct afswtch *rafp)
1373153422Ssam{
1374170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN, d, 0, NULL);
1375153422Ssam}
1376153422Ssam
1377148416Ssamstatic
1378170531SsamDECL_CMD_FUNC(set80211bgscanidle, val, d)
1379170531Ssam{
1380170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN_IDLE, atoi(val), 0, NULL);
1381170531Ssam}
1382170531Ssam
1383170531Ssamstatic
1384170531SsamDECL_CMD_FUNC(set80211bgscanintvl, val, d)
1385170531Ssam{
1386170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN_INTERVAL, atoi(val), 0, NULL);
1387170531Ssam}
1388170531Ssam
1389170531Ssamstatic
1390170531SsamDECL_CMD_FUNC(set80211scanvalid, val, d)
1391170531Ssam{
1392170531Ssam	set80211(s, IEEE80211_IOC_SCANVALID, atoi(val), 0, NULL);
1393170531Ssam}
1394170531Ssam
1395178354Ssam/*
1396178354Ssam * Parse an optional trailing specification of which netbands
1397178354Ssam * to apply a parameter to.  This is basically the same syntax
1398178354Ssam * as used for channels but you can concatenate to specify
1399178354Ssam * multiple.  For example:
1400178354Ssam *	14:abg		apply to 11a, 11b, and 11g
1401178354Ssam *	6:ht		apply to 11na and 11ng
1402178354Ssam * We don't make a big effort to catch silly things; this is
1403178354Ssam * really a convenience mechanism.
1404178354Ssam */
1405178354Ssamstatic int
1406178354Ssamgetmodeflags(const char *val)
1407170531Ssam{
1408178354Ssam	const char *cp;
1409178354Ssam	int flags;
1410178354Ssam
1411178354Ssam	flags = 0;
1412178354Ssam
1413178354Ssam	cp = strchr(val, ':');
1414178354Ssam	if (cp != NULL) {
1415178354Ssam		for (cp++; isalpha((int) *cp); cp++) {
1416178354Ssam			/* accept mixed case */
1417178354Ssam			int c = *cp;
1418178354Ssam			if (isupper(c))
1419178354Ssam				c = tolower(c);
1420178354Ssam			switch (c) {
1421178354Ssam			case 'a':		/* 802.11a */
1422178354Ssam				flags |= IEEE80211_CHAN_A;
1423178354Ssam				break;
1424178354Ssam			case 'b':		/* 802.11b */
1425178354Ssam				flags |= IEEE80211_CHAN_B;
1426178354Ssam				break;
1427178354Ssam			case 'g':		/* 802.11g */
1428178354Ssam				flags |= IEEE80211_CHAN_G;
1429178354Ssam				break;
1430178354Ssam			case 'n':		/* 802.11n */
1431178354Ssam				flags |= IEEE80211_CHAN_HT;
1432178354Ssam				break;
1433178354Ssam			case 'd':		/* dt = Atheros Dynamic Turbo */
1434178354Ssam				flags |= IEEE80211_CHAN_TURBO;
1435178354Ssam				break;
1436178354Ssam			case 't':		/* ht, dt, st, t */
1437178354Ssam				/* dt and unadorned t specify Dynamic Turbo */
1438178354Ssam				if ((flags & (IEEE80211_CHAN_STURBO|IEEE80211_CHAN_HT)) == 0)
1439178354Ssam					flags |= IEEE80211_CHAN_TURBO;
1440178354Ssam				break;
1441178354Ssam			case 's':		/* st = Atheros Static Turbo */
1442178354Ssam				flags |= IEEE80211_CHAN_STURBO;
1443178354Ssam				break;
1444188784Ssam			case 'h':		/* 1/2-width channels */
1445188784Ssam				flags |= IEEE80211_CHAN_HALF;
1446188784Ssam				break;
1447188784Ssam			case 'q':		/* 1/4-width channels */
1448188784Ssam				flags |= IEEE80211_CHAN_QUARTER;
1449188784Ssam				break;
1450178354Ssam			default:
1451178354Ssam				errx(-1, "%s: Invalid mode attribute %c\n",
1452178354Ssam				    val, *cp);
1453178354Ssam			}
1454178354Ssam		}
1455178354Ssam	}
1456178354Ssam	return flags;
1457170531Ssam}
1458170531Ssam
1459178354Ssam#define	IEEE80211_CHAN_HTA	(IEEE80211_CHAN_HT|IEEE80211_CHAN_5GHZ)
1460178354Ssam#define	IEEE80211_CHAN_HTG	(IEEE80211_CHAN_HT|IEEE80211_CHAN_2GHZ)
1461178354Ssam
1462178354Ssam#define	_APPLY(_flags, _base, _param, _v) do {				\
1463178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1464178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1465178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1466178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1467178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1468178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1469178354Ssam	    else							\
1470178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1471178354Ssam    }									\
1472178354Ssam    if (_flags & IEEE80211_CHAN_TURBO) {				\
1473178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1474178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;	\
1475178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;	\
1476178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1477178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;	\
1478178354Ssam	    else							\
1479178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;	\
1480178354Ssam    }									\
1481178354Ssam    if (_flags & IEEE80211_CHAN_STURBO)					\
1482178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = _v;		\
1483178354Ssam    if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1484178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = _v;		\
1485178354Ssam    if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1486178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = _v;		\
1487178354Ssam    if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1488178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = _v;		\
1489188784Ssam    if (_flags & IEEE80211_CHAN_HALF)					\
1490188784Ssam	    _base.params[IEEE80211_MODE_HALF]._param = _v;		\
1491188784Ssam    if (_flags & IEEE80211_CHAN_QUARTER)				\
1492188784Ssam	    _base.params[IEEE80211_MODE_QUARTER]._param = _v;		\
1493178354Ssam} while (0)
1494178354Ssam#define	_APPLY1(_flags, _base, _param, _v) do {				\
1495178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1496178354Ssam	    if (_flags & IEEE80211_CHAN_5GHZ)				\
1497178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1498178354Ssam	    else							\
1499178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1500178354Ssam    } else if ((_flags & IEEE80211_CHAN_108A) == IEEE80211_CHAN_108A)	\
1501178354Ssam	    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;		\
1502178354Ssam    else if ((_flags & IEEE80211_CHAN_108G) == IEEE80211_CHAN_108G)	\
1503178354Ssam	    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;		\
1504178354Ssam    else if ((_flags & IEEE80211_CHAN_ST) == IEEE80211_CHAN_ST)		\
1505178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = _v;		\
1506188784Ssam    else if (_flags & IEEE80211_CHAN_HALF)				\
1507188784Ssam	    _base.params[IEEE80211_MODE_HALF]._param = _v;		\
1508188784Ssam    else if (_flags & IEEE80211_CHAN_QUARTER)				\
1509188784Ssam	    _base.params[IEEE80211_MODE_QUARTER]._param = _v;		\
1510178354Ssam    else if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1511178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = _v;		\
1512178354Ssam    else if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1513178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = _v;		\
1514178354Ssam    else if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1515178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = _v;		\
1516178354Ssam} while (0)
1517178354Ssam#define	_APPLY_RATE(_flags, _base, _param, _v) do {			\
1518178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1519188784Ssam	(_v) = (_v / 2) | IEEE80211_RATE_MCS;				\
1520178354Ssam    }									\
1521188784Ssam    _APPLY(_flags, _base, _param, _v);					\
1522178354Ssam} while (0)
1523178354Ssam#define	_APPLY_RATE1(_flags, _base, _param, _v) do {			\
1524178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1525188784Ssam	(_v) = (_v / 2) | IEEE80211_RATE_MCS;				\
1526188784Ssam    }									\
1527188784Ssam    _APPLY1(_flags, _base, _param, _v);					\
1528178354Ssam} while (0)
1529178354Ssam
1530170531Ssamstatic
1531178354SsamDECL_CMD_FUNC(set80211roamrssi, val, d)
1532170531Ssam{
1533178354Ssam	double v = atof(val);
1534178354Ssam	int rssi, flags;
1535178354Ssam
1536178354Ssam	rssi = (int) (2*v);
1537178354Ssam	if (rssi != 2*v)
1538178354Ssam		errx(-1, "invalid rssi (must be .5 dBm units)");
1539178354Ssam	flags = getmodeflags(val);
1540178354Ssam	getroam(s);
1541178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1542178354Ssam		flags = getcurchan(s)->ic_flags;
1543178354Ssam		_APPLY1(flags, roamparams, rssi, rssi);
1544178354Ssam	} else
1545178354Ssam		_APPLY(flags, roamparams, rssi, rssi);
1546178354Ssam	callback_register(setroam_cb, &roamparams);
1547170531Ssam}
1548170531Ssam
1549188784Ssamstatic int
1550188784Ssamgetrate(const char *val, const char *tag)
1551188784Ssam{
1552188784Ssam	double v = atof(val);
1553188784Ssam	int rate;
1554188784Ssam
1555188784Ssam	rate = (int) (2*v);
1556188784Ssam	if (rate != 2*v)
1557188784Ssam		errx(-1, "invalid %s rate (must be .5 Mb/s units)", tag);
1558188784Ssam	return rate;		/* NB: returns 2x the specified value */
1559188784Ssam}
1560188784Ssam
1561170531Ssamstatic
1562178354SsamDECL_CMD_FUNC(set80211roamrate, val, d)
1563170531Ssam{
1564188784Ssam	int rate, flags;
1565178354Ssam
1566188784Ssam	rate = getrate(val, "roam");
1567178354Ssam	flags = getmodeflags(val);
1568178354Ssam	getroam(s);
1569178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1570178354Ssam		flags = getcurchan(s)->ic_flags;
1571188784Ssam		_APPLY_RATE1(flags, roamparams, rate, rate);
1572178354Ssam	} else
1573188784Ssam		_APPLY_RATE(flags, roamparams, rate, rate);
1574178354Ssam	callback_register(setroam_cb, &roamparams);
1575170531Ssam}
1576170531Ssam
1577170531Ssamstatic
1578178354SsamDECL_CMD_FUNC(set80211mcastrate, val, d)
1579170531Ssam{
1580188784Ssam	int rate, flags;
1581178354Ssam
1582188784Ssam	rate = getrate(val, "mcast");
1583178354Ssam	flags = getmodeflags(val);
1584178354Ssam	gettxparams(s);
1585178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1586178354Ssam		flags = getcurchan(s)->ic_flags;
1587188784Ssam		_APPLY_RATE1(flags, txparams, mcastrate, rate);
1588178354Ssam	} else
1589188784Ssam		_APPLY_RATE(flags, txparams, mcastrate, rate);
1590178354Ssam	callback_register(settxparams_cb, &txparams);
1591170531Ssam}
1592170531Ssam
1593170531Ssamstatic
1594178354SsamDECL_CMD_FUNC(set80211mgtrate, val, d)
1595170531Ssam{
1596188784Ssam	int rate, flags;
1597178354Ssam
1598188784Ssam	rate = getrate(val, "mgmt");
1599178354Ssam	flags = getmodeflags(val);
1600178354Ssam	gettxparams(s);
1601178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1602178354Ssam		flags = getcurchan(s)->ic_flags;
1603188784Ssam		_APPLY_RATE1(flags, txparams, mgmtrate, rate);
1604178354Ssam	} else
1605188784Ssam		_APPLY_RATE(flags, txparams, mgmtrate, rate);
1606178354Ssam	callback_register(settxparams_cb, &txparams);
1607170531Ssam}
1608170531Ssam
1609170531Ssamstatic
1610178354SsamDECL_CMD_FUNC(set80211ucastrate, val, d)
1611170531Ssam{
1612188784Ssam	int flags;
1613178354Ssam
1614178354Ssam	gettxparams(s);
1615178354Ssam	flags = getmodeflags(val);
1616178354Ssam	if (isanyarg(val)) {
1617178354Ssam		if (flags == 0) {	/* NB: no flags => current channel */
1618178354Ssam			flags = getcurchan(s)->ic_flags;
1619178354Ssam			_APPLY1(flags, txparams, ucastrate,
1620178354Ssam			    IEEE80211_FIXED_RATE_NONE);
1621178354Ssam		} else
1622178354Ssam			_APPLY(flags, txparams, ucastrate,
1623178354Ssam			    IEEE80211_FIXED_RATE_NONE);
1624178354Ssam	} else {
1625188784Ssam		int rate = getrate(val, "ucast");
1626178354Ssam		if (flags == 0) {	/* NB: no flags => current channel */
1627178354Ssam			flags = getcurchan(s)->ic_flags;
1628188784Ssam			_APPLY_RATE1(flags, txparams, ucastrate, rate);
1629178354Ssam		} else
1630188784Ssam			_APPLY_RATE(flags, txparams, ucastrate, rate);
1631178354Ssam	}
1632178354Ssam	callback_register(settxparams_cb, &txparams);
1633170531Ssam}
1634170531Ssam
1635170531Ssamstatic
1636178354SsamDECL_CMD_FUNC(set80211maxretry, val, d)
1637153354Ssam{
1638178354Ssam	int v = atoi(val), flags;
1639178354Ssam
1640178354Ssam	flags = getmodeflags(val);
1641178354Ssam	gettxparams(s);
1642178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1643178354Ssam		flags = getcurchan(s)->ic_flags;
1644178354Ssam		_APPLY1(flags, txparams, maxretry, v);
1645178354Ssam	} else
1646178354Ssam		_APPLY(flags, txparams, maxretry, v);
1647178354Ssam	callback_register(settxparams_cb, &txparams);
1648153354Ssam}
1649178354Ssam#undef _APPLY_RATE
1650178354Ssam#undef _APPLY
1651178354Ssam#undef IEEE80211_CHAN_HTA
1652178354Ssam#undef IEEE80211_CHAN_HTG
1653153354Ssam
1654153354Ssamstatic
1655148416SsamDECL_CMD_FUNC(set80211fragthreshold, val, d)
1656148416Ssam{
1657148416Ssam	set80211(s, IEEE80211_IOC_FRAGTHRESHOLD,
1658148416Ssam		isundefarg(val) ? IEEE80211_FRAG_MAX : atoi(val), 0, NULL);
1659148416Ssam}
1660148416Ssam
1661160687Ssamstatic
1662160687SsamDECL_CMD_FUNC(set80211bmissthreshold, val, d)
1663160687Ssam{
1664160687Ssam	set80211(s, IEEE80211_IOC_BMISSTHRESHOLD,
1665160687Ssam		isundefarg(val) ? IEEE80211_HWBMISS_MAX : atoi(val), 0, NULL);
1666160687Ssam}
1667160687Ssam
1668170531Ssamstatic void
1669170531Ssamset80211burst(const char *val, int d, int s, const struct afswtch *rafp)
1670170531Ssam{
1671170531Ssam	set80211(s, IEEE80211_IOC_BURST, d, 0, NULL);
1672170531Ssam}
1673170531Ssam
1674170531Ssamstatic void
1675170531Ssamset80211doth(const char *val, int d, int s, const struct afswtch *rafp)
1676170531Ssam{
1677170531Ssam	set80211(s, IEEE80211_IOC_DOTH, d, 0, NULL);
1678170531Ssam}
1679170531Ssam
1680173275Ssamstatic void
1681178354Ssamset80211dfs(const char *val, int d, int s, const struct afswtch *rafp)
1682178354Ssam{
1683178354Ssam	set80211(s, IEEE80211_IOC_DFS, d, 0, NULL);
1684178354Ssam}
1685178354Ssam
1686178354Ssamstatic void
1687173275Ssamset80211shortgi(const char *val, int d, int s, const struct afswtch *rafp)
1688173275Ssam{
1689173275Ssam	set80211(s, IEEE80211_IOC_SHORTGI,
1690173275Ssam		d ? (IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40) : 0,
1691173275Ssam		0, NULL);
1692173275Ssam}
1693173275Ssam
1694173275Ssamstatic void
1695173275Ssamset80211ampdu(const char *val, int d, int s, const struct afswtch *rafp)
1696173275Ssam{
1697173275Ssam	int ampdu;
1698173275Ssam
1699173275Ssam	if (get80211val(s, IEEE80211_IOC_AMPDU, &ampdu) < 0)
1700173275Ssam		errx(-1, "cannot get AMPDU setting");
1701173275Ssam	if (d < 0) {
1702173275Ssam		d = -d;
1703173275Ssam		ampdu &= ~d;
1704173275Ssam	} else
1705173275Ssam		ampdu |= d;
1706173275Ssam	set80211(s, IEEE80211_IOC_AMPDU, ampdu, 0, NULL);
1707173275Ssam}
1708173275Ssam
1709173275Ssamstatic
1710173275SsamDECL_CMD_FUNC(set80211ampdulimit, val, d)
1711173275Ssam{
1712173275Ssam	int v;
1713173275Ssam
1714173275Ssam	switch (atoi(val)) {
1715173275Ssam	case 8:
1716173275Ssam	case 8*1024:
1717173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_8K;
1718173275Ssam		break;
1719173275Ssam	case 16:
1720173275Ssam	case 16*1024:
1721173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_16K;
1722173275Ssam		break;
1723173275Ssam	case 32:
1724173275Ssam	case 32*1024:
1725173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_32K;
1726173275Ssam		break;
1727173275Ssam	case 64:
1728173275Ssam	case 64*1024:
1729173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_64K;
1730173275Ssam		break;
1731173275Ssam	default:
1732173275Ssam		errx(-1, "invalid A-MPDU limit %s", val);
1733173275Ssam	}
1734173275Ssam	set80211(s, IEEE80211_IOC_AMPDU_LIMIT, v, 0, NULL);
1735173275Ssam}
1736173275Ssam
1737173275Ssamstatic
1738173275SsamDECL_CMD_FUNC(set80211ampdudensity, val, d)
1739173275Ssam{
1740173275Ssam	int v;
1741173275Ssam
1742183260Ssam	if (isanyarg(val) || strcasecmp(val, "na") == 0)
1743173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1744173275Ssam	else switch ((int)(atof(val)*4)) {
1745173275Ssam	case 0:
1746173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1747173275Ssam		break;
1748173275Ssam	case 1:
1749173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_025;
1750173275Ssam		break;
1751173275Ssam	case 2:
1752173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_05;
1753173275Ssam		break;
1754173275Ssam	case 4:
1755173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_1;
1756173275Ssam		break;
1757173275Ssam	case 8:
1758173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_2;
1759173275Ssam		break;
1760173275Ssam	case 16:
1761173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_4;
1762173275Ssam		break;
1763173275Ssam	case 32:
1764173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_8;
1765173275Ssam		break;
1766173275Ssam	case 64:
1767173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_16;
1768173275Ssam		break;
1769173275Ssam	default:
1770173275Ssam		errx(-1, "invalid A-MPDU density %s", val);
1771173275Ssam	}
1772173275Ssam	set80211(s, IEEE80211_IOC_AMPDU_DENSITY, v, 0, NULL);
1773173275Ssam}
1774173275Ssam
1775173275Ssamstatic void
1776173275Ssamset80211amsdu(const char *val, int d, int s, const struct afswtch *rafp)
1777173275Ssam{
1778173275Ssam	int amsdu;
1779173275Ssam
1780173275Ssam	if (get80211val(s, IEEE80211_IOC_AMSDU, &amsdu) < 0)
1781187801Ssam		err(-1, "cannot get AMSDU setting");
1782173275Ssam	if (d < 0) {
1783173275Ssam		d = -d;
1784173275Ssam		amsdu &= ~d;
1785173275Ssam	} else
1786173275Ssam		amsdu |= d;
1787173275Ssam	set80211(s, IEEE80211_IOC_AMSDU, amsdu, 0, NULL);
1788173275Ssam}
1789173275Ssam
1790173275Ssamstatic
1791173275SsamDECL_CMD_FUNC(set80211amsdulimit, val, d)
1792173275Ssam{
1793173275Ssam	set80211(s, IEEE80211_IOC_AMSDU_LIMIT, atoi(val), 0, NULL);
1794173275Ssam}
1795173275Ssam
1796173275Ssamstatic void
1797173275Ssamset80211puren(const char *val, int d, int s, const struct afswtch *rafp)
1798173275Ssam{
1799173275Ssam	set80211(s, IEEE80211_IOC_PUREN, d, 0, NULL);
1800173275Ssam}
1801173275Ssam
1802173275Ssamstatic void
1803173275Ssamset80211htcompat(const char *val, int d, int s, const struct afswtch *rafp)
1804173275Ssam{
1805173275Ssam	set80211(s, IEEE80211_IOC_HTCOMPAT, d, 0, NULL);
1806173275Ssam}
1807173275Ssam
1808173275Ssamstatic void
1809173275Ssamset80211htconf(const char *val, int d, int s, const struct afswtch *rafp)
1810173275Ssam{
1811173275Ssam	set80211(s, IEEE80211_IOC_HTCONF, d, 0, NULL);
1812173275Ssam	htconf = d;
1813173275Ssam}
1814173275Ssam
1815173275Ssamstatic void
1816178354Ssamset80211dwds(const char *val, int d, int s, const struct afswtch *rafp)
1817178354Ssam{
1818178354Ssam	set80211(s, IEEE80211_IOC_DWDS, d, 0, NULL);
1819178354Ssam}
1820178354Ssam
1821178354Ssamstatic void
1822173275Ssamset80211inact(const char *val, int d, int s, const struct afswtch *rafp)
1823173275Ssam{
1824173275Ssam	set80211(s, IEEE80211_IOC_INACTIVITY, d, 0, NULL);
1825173275Ssam}
1826173275Ssam
1827173275Ssamstatic void
1828178354Ssamset80211tsn(const char *val, int d, int s, const struct afswtch *rafp)
1829178354Ssam{
1830178354Ssam	set80211(s, IEEE80211_IOC_TSN, d, 0, NULL);
1831178354Ssam}
1832178354Ssam
1833178354Ssamstatic void
1834178354Ssamset80211dotd(const char *val, int d, int s, const struct afswtch *rafp)
1835178354Ssam{
1836178354Ssam	set80211(s, IEEE80211_IOC_DOTD, d, 0, NULL);
1837178354Ssam}
1838178354Ssam
1839183261Ssamstatic void
1840183261Ssamset80211smps(const char *val, int d, int s, const struct afswtch *rafp)
1841183261Ssam{
1842183261Ssam	set80211(s, IEEE80211_IOC_SMPS, d, 0, NULL);
1843183261Ssam}
1844183261Ssam
1845183261Ssamstatic void
1846183261Ssamset80211rifs(const char *val, int d, int s, const struct afswtch *rafp)
1847183261Ssam{
1848183261Ssam	set80211(s, IEEE80211_IOC_RIFS, d, 0, NULL);
1849183261Ssam}
1850183261Ssam
1851186904Ssamstatic
1852186904SsamDECL_CMD_FUNC(set80211tdmaslot, val, d)
1853186904Ssam{
1854186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOT, atoi(val), 0, NULL);
1855186904Ssam}
1856186904Ssam
1857186904Ssamstatic
1858186904SsamDECL_CMD_FUNC(set80211tdmaslotcnt, val, d)
1859186904Ssam{
1860186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOTCNT, atoi(val), 0, NULL);
1861186904Ssam}
1862186904Ssam
1863186904Ssamstatic
1864186904SsamDECL_CMD_FUNC(set80211tdmaslotlen, val, d)
1865186904Ssam{
1866186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOTLEN, atoi(val), 0, NULL);
1867186904Ssam}
1868186904Ssam
1869186904Ssamstatic
1870186904SsamDECL_CMD_FUNC(set80211tdmabintval, val, d)
1871186904Ssam{
1872186904Ssam	set80211(s, IEEE80211_IOC_TDMA_BINTERVAL, atoi(val), 0, NULL);
1873186904Ssam}
1874186904Ssam
1875195618Srpaulostatic
1876195618SrpauloDECL_CMD_FUNC(set80211meshttl, val, d)
1877195618Srpaulo{
1878195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_TTL, atoi(val), 0, NULL);
1879195618Srpaulo}
1880195618Srpaulo
1881195618Srpaulostatic
1882195618SrpauloDECL_CMD_FUNC(set80211meshforward, val, d)
1883195618Srpaulo{
1884233328Sadrian	set80211(s, IEEE80211_IOC_MESH_FWRD, d, 0, NULL);
1885195618Srpaulo}
1886195618Srpaulo
1887195618Srpaulostatic
1888234893SmonthadarDECL_CMD_FUNC(set80211meshgate, val, d)
1889234893Smonthadar{
1890234893Smonthadar	set80211(s, IEEE80211_IOC_MESH_GATE, d, 0, NULL);
1891234893Smonthadar}
1892234893Smonthadar
1893234893Smonthadarstatic
1894195618SrpauloDECL_CMD_FUNC(set80211meshpeering, val, d)
1895195618Srpaulo{
1896233382Sbschmidt	set80211(s, IEEE80211_IOC_MESH_AP, d, 0, NULL);
1897195618Srpaulo}
1898195618Srpaulo
1899195618Srpaulostatic
1900195618SrpauloDECL_CMD_FUNC(set80211meshmetric, val, d)
1901195618Srpaulo{
1902195618Srpaulo	char v[12];
1903195618Srpaulo
1904195618Srpaulo	memcpy(v, val, sizeof(v));
1905195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_PR_METRIC, 0, 0, v);
1906195618Srpaulo}
1907195618Srpaulo
1908195618Srpaulostatic
1909195618SrpauloDECL_CMD_FUNC(set80211meshpath, val, d)
1910195618Srpaulo{
1911195618Srpaulo	char v[12];
1912195618Srpaulo
1913195618Srpaulo	memcpy(v, val, sizeof(v));
1914195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_PR_PATH, 0, 0, v);
1915195618Srpaulo}
1916195618Srpaulo
1917178354Ssamstatic int
1918178354Ssamregdomain_sort(const void *a, const void *b)
1919178354Ssam{
1920178354Ssam#define	CHAN_ALL \
1921178354Ssam	(IEEE80211_CHAN_ALLTURBO|IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)
1922178354Ssam	const struct ieee80211_channel *ca = a;
1923178354Ssam	const struct ieee80211_channel *cb = b;
1924178354Ssam
1925178354Ssam	return ca->ic_freq == cb->ic_freq ?
1926178354Ssam	    (ca->ic_flags & CHAN_ALL) - (cb->ic_flags & CHAN_ALL) :
1927178354Ssam	    ca->ic_freq - cb->ic_freq;
1928178354Ssam#undef CHAN_ALL
1929178354Ssam}
1930178354Ssam
1931178354Ssamstatic const struct ieee80211_channel *
1932178354Ssamchanlookup(const struct ieee80211_channel chans[], int nchans,
1933178354Ssam	int freq, int flags)
1934178354Ssam{
1935178354Ssam	int i;
1936178354Ssam
1937178354Ssam	flags &= IEEE80211_CHAN_ALLTURBO;
1938178354Ssam	for (i = 0; i < nchans; i++) {
1939178354Ssam		const struct ieee80211_channel *c = &chans[i];
1940178354Ssam		if (c->ic_freq == freq &&
1941178354Ssam		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1942178354Ssam			return c;
1943178354Ssam	}
1944178354Ssam	return NULL;
1945178354Ssam}
1946178354Ssam
1947187841Ssamstatic int
1948187841Ssamchanfind(const struct ieee80211_channel chans[], int nchans, int flags)
1949187841Ssam{
1950187841Ssam	int i;
1951187841Ssam
1952187841Ssam	for (i = 0; i < nchans; i++) {
1953187841Ssam		const struct ieee80211_channel *c = &chans[i];
1954187841Ssam		if ((c->ic_flags & flags) == flags)
1955187841Ssam			return 1;
1956187841Ssam	}
1957187841Ssam	return 0;
1958187841Ssam}
1959187841Ssam
1960188258Ssam/*
1961188258Ssam * Check channel compatibility.
1962188258Ssam */
1963188258Ssamstatic int
1964188258Ssamcheckchan(const struct ieee80211req_chaninfo *avail, int freq, int flags)
1965188258Ssam{
1966188258Ssam	flags &= ~REQ_FLAGS;
1967188258Ssam	/*
1968188258Ssam	 * Check if exact channel is in the calibration table;
1969188258Ssam	 * everything below is to deal with channels that we
1970188258Ssam	 * want to include but that are not explicitly listed.
1971188258Ssam	 */
1972188258Ssam	if (flags & IEEE80211_CHAN_HT40) {
1973188258Ssam		/* NB: we use an HT40 channel center that matches HT20 */
1974188258Ssam		flags = (flags &~ IEEE80211_CHAN_HT40) | IEEE80211_CHAN_HT20;
1975188258Ssam	}
1976188258Ssam	if (chanlookup(avail->ic_chans, avail->ic_nchans, freq, flags) != NULL)
1977188258Ssam		return 1;
1978188258Ssam	if (flags & IEEE80211_CHAN_GSM) {
1979188258Ssam		/*
1980188258Ssam		 * XXX GSM frequency mapping is handled in the kernel
1981188258Ssam		 * so we cannot find them in the calibration table;
1982188258Ssam		 * just accept the channel and the kernel will reject
1983188258Ssam		 * the channel list if it's wrong.
1984188258Ssam		 */
1985188258Ssam		return 1;
1986188258Ssam	}
1987188258Ssam	/*
1988188258Ssam	 * If this is a 1/2 or 1/4 width channel allow it if a full
1989188258Ssam	 * width channel is present for this frequency, and the device
1990188258Ssam	 * supports fractional channels on this band.  This is a hack
1991188258Ssam	 * that avoids bloating the calibration table; it may be better
1992188258Ssam	 * by per-band attributes though (we are effectively calculating
1993188258Ssam	 * this attribute by scanning the channel list ourself).
1994188258Ssam	 */
1995188258Ssam	if ((flags & (IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)) == 0)
1996188258Ssam		return 0;
1997188258Ssam	if (chanlookup(avail->ic_chans, avail->ic_nchans, freq,
1998188258Ssam	    flags &~ (IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)) == NULL)
1999188258Ssam		return 0;
2000188258Ssam	if (flags & IEEE80211_CHAN_HALF) {
2001188258Ssam		return chanfind(avail->ic_chans, avail->ic_nchans,
2002188258Ssam		    IEEE80211_CHAN_HALF |
2003188258Ssam		       (flags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ)));
2004188258Ssam	} else {
2005188258Ssam		return chanfind(avail->ic_chans, avail->ic_nchans,
2006188258Ssam		    IEEE80211_CHAN_QUARTER |
2007188258Ssam			(flags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ)));
2008188258Ssam	}
2009188258Ssam}
2010188258Ssam
2011178354Ssamstatic void
2012178354Ssamregdomain_addchans(struct ieee80211req_chaninfo *ci,
2013178354Ssam	const netband_head *bands,
2014178354Ssam	const struct ieee80211_regdomain *reg,
2015178354Ssam	uint32_t chanFlags,
2016178354Ssam	const struct ieee80211req_chaninfo *avail)
2017178354Ssam{
2018178354Ssam	const struct netband *nb;
2019178354Ssam	const struct freqband *b;
2020178354Ssam	struct ieee80211_channel *c, *prev;
2021188258Ssam	int freq, hi_adj, lo_adj, channelSep;
2022188258Ssam	uint32_t flags;
2023178354Ssam
2024188258Ssam	hi_adj = (chanFlags & IEEE80211_CHAN_HT40U) ? -20 : 0;
2025188258Ssam	lo_adj = (chanFlags & IEEE80211_CHAN_HT40D) ? 20 : 0;
2026178354Ssam	channelSep = (chanFlags & IEEE80211_CHAN_2GHZ) ? 0 : 40;
2027178354Ssam	LIST_FOREACH(nb, bands, next) {
2028178354Ssam		b = nb->band;
2029187842Ssam		if (verbose) {
2030187842Ssam			printf("%s:", __func__);
2031187842Ssam			printb(" chanFlags", chanFlags, IEEE80211_CHAN_BITS);
2032187842Ssam			printb(" bandFlags", nb->flags | b->flags,
2033187842Ssam			    IEEE80211_CHAN_BITS);
2034187842Ssam			putchar('\n');
2035187842Ssam		}
2036178354Ssam		prev = NULL;
2037188258Ssam		for (freq = b->freqStart + lo_adj;
2038188258Ssam		     freq <= b->freqEnd + hi_adj; freq += b->chanSep) {
2039187841Ssam			/*
2040188258Ssam			 * Construct flags for the new channel.  We take
2041188258Ssam			 * the attributes from the band descriptions except
2042188258Ssam			 * for HT40 which is enabled generically (i.e. +/-
2043188258Ssam			 * extension channel) in the band description and
2044188258Ssam			 * then constrained according by channel separation.
2045187841Ssam			 */
2046188258Ssam			flags = nb->flags | b->flags;
2047188258Ssam			if (flags & IEEE80211_CHAN_HT) {
2048188258Ssam				/*
2049188258Ssam				 * HT channels are generated specially; we're
2050188258Ssam				 * called to add HT20, HT40+, and HT40- chan's
2051188258Ssam				 * so we need to expand only band specs for
2052188258Ssam				 * the HT channel type being added.
2053188258Ssam				 */
2054188258Ssam				if ((chanFlags & IEEE80211_CHAN_HT20) &&
2055188258Ssam				    (flags & IEEE80211_CHAN_HT20) == 0) {
2056188258Ssam					if (verbose)
2057188258Ssam						printf("%u: skip, not an "
2058188258Ssam						    "HT20 channel\n", freq);
2059188258Ssam					continue;
2060188258Ssam				}
2061188258Ssam				if ((chanFlags & IEEE80211_CHAN_HT40) &&
2062188258Ssam				    (flags & IEEE80211_CHAN_HT40) == 0) {
2063188258Ssam					if (verbose)
2064188258Ssam						printf("%u: skip, not an "
2065188258Ssam						    "HT40 channel\n", freq);
2066188258Ssam					continue;
2067188258Ssam				}
2068188258Ssam				/* NB: HT attribute comes from caller */
2069188258Ssam				flags &= ~IEEE80211_CHAN_HT;
2070188258Ssam				flags |= chanFlags & IEEE80211_CHAN_HT;
2071188258Ssam			}
2072188258Ssam			/*
2073188258Ssam			 * Check if device can operate on this frequency.
2074188258Ssam			 */
2075188258Ssam			if (!checkchan(avail, freq, flags)) {
2076187842Ssam				if (verbose) {
2077187842Ssam					printf("%u: skip, ", freq);
2078188258Ssam					printb("flags", flags,
2079187842Ssam					    IEEE80211_CHAN_BITS);
2080187842Ssam					printf(" not available\n");
2081187842Ssam				}
2082178354Ssam				continue;
2083178354Ssam			}
2084188258Ssam			if ((flags & REQ_ECM) && !reg->ecm) {
2085178354Ssam				if (verbose)
2086188258Ssam					printf("%u: skip, ECM channel\n", freq);
2087178354Ssam				continue;
2088178354Ssam			}
2089188258Ssam			if ((flags & REQ_INDOOR) && reg->location == 'O') {
2090178354Ssam				if (verbose)
2091188258Ssam					printf("%u: skip, indoor channel\n",
2092187841Ssam					    freq);
2093178354Ssam				continue;
2094178354Ssam			}
2095178354Ssam			if ((flags & REQ_OUTDOOR) && reg->location == 'I') {
2096178354Ssam				if (verbose)
2097188258Ssam					printf("%u: skip, outdoor channel\n",
2098188258Ssam					    freq);
2099178354Ssam				continue;
2100178354Ssam			}
2101178354Ssam			if ((flags & IEEE80211_CHAN_HT40) &&
2102178354Ssam			    prev != NULL && (freq - prev->ic_freq) < channelSep) {
2103178354Ssam				if (verbose)
2104178354Ssam					printf("%u: skip, only %u channel "
2105178354Ssam					    "separation, need %d\n", freq,
2106178354Ssam					    freq - prev->ic_freq, channelSep);
2107178354Ssam				continue;
2108178354Ssam			}
2109178354Ssam			if (ci->ic_nchans == IEEE80211_CHAN_MAX) {
2110178354Ssam				if (verbose)
2111187842Ssam					printf("%u: skip, channel table full\n",
2112187842Ssam					    freq);
2113178354Ssam				break;
2114178354Ssam			}
2115178354Ssam			c = &ci->ic_chans[ci->ic_nchans++];
2116187801Ssam			memset(c, 0, sizeof(*c));
2117178354Ssam			c->ic_freq = freq;
2118188258Ssam			c->ic_flags = flags;
2119178354Ssam			if (c->ic_flags & IEEE80211_CHAN_DFS)
2120178354Ssam				c->ic_maxregpower = nb->maxPowerDFS;
2121178354Ssam			else
2122178354Ssam				c->ic_maxregpower = nb->maxPower;
2123187842Ssam			if (verbose) {
2124187842Ssam				printf("[%3d] add freq %u ",
2125187842Ssam				    ci->ic_nchans-1, c->ic_freq);
2126187842Ssam				printb("flags", c->ic_flags, IEEE80211_CHAN_BITS);
2127187842Ssam				printf(" power %u\n", c->ic_maxregpower);
2128187842Ssam			}
2129178354Ssam			/* NB: kernel fills in other fields */
2130178354Ssam			prev = c;
2131178354Ssam		}
2132178354Ssam	}
2133178354Ssam}
2134178354Ssam
2135178354Ssamstatic void
2136178354Ssamregdomain_makechannels(
2137178354Ssam	struct ieee80211_regdomain_req *req,
2138178354Ssam	const struct ieee80211_devcaps_req *dc)
2139178354Ssam{
2140178354Ssam	struct regdata *rdp = getregdata();
2141178354Ssam	const struct country *cc;
2142178354Ssam	const struct ieee80211_regdomain *reg = &req->rd;
2143178354Ssam	struct ieee80211req_chaninfo *ci = &req->chaninfo;
2144178354Ssam	const struct regdomain *rd;
2145178354Ssam
2146178354Ssam	/*
2147178354Ssam	 * Locate construction table for new channel list.  We treat
2148178354Ssam	 * the regdomain/SKU as definitive so a country can be in
2149178354Ssam	 * multiple with different properties (e.g. US in FCC+FCC3).
2150178354Ssam	 * If no regdomain is specified then we fallback on the country
2151178354Ssam	 * code to find the associated regdomain since countries always
2152178354Ssam	 * belong to at least one regdomain.
2153178354Ssam	 */
2154178354Ssam	if (reg->regdomain == 0) {
2155178354Ssam		cc = lib80211_country_findbycc(rdp, reg->country);
2156178354Ssam		if (cc == NULL)
2157178354Ssam			errx(1, "internal error, country %d not found",
2158178354Ssam			    reg->country);
2159178354Ssam		rd = cc->rd;
2160178354Ssam	} else
2161178354Ssam		rd = lib80211_regdomain_findbysku(rdp, reg->regdomain);
2162178354Ssam	if (rd == NULL)
2163178354Ssam		errx(1, "internal error, regdomain %d not found",
2164178354Ssam			    reg->regdomain);
2165178354Ssam	if (rd->sku != SKU_DEBUG) {
2166187801Ssam		/*
2167187801Ssam		 * regdomain_addchans incrememnts the channel count for
2168187801Ssam		 * each channel it adds so initialize ic_nchans to zero.
2169187801Ssam		 * Note that we know we have enough space to hold all possible
2170187801Ssam		 * channels because the devcaps list size was used to
2171187801Ssam		 * allocate our request.
2172187801Ssam		 */
2173187801Ssam		ci->ic_nchans = 0;
2174178354Ssam		if (!LIST_EMPTY(&rd->bands_11b))
2175178354Ssam			regdomain_addchans(ci, &rd->bands_11b, reg,
2176178354Ssam			    IEEE80211_CHAN_B, &dc->dc_chaninfo);
2177187841Ssam		if (!LIST_EMPTY(&rd->bands_11g))
2178178354Ssam			regdomain_addchans(ci, &rd->bands_11g, reg,
2179178354Ssam			    IEEE80211_CHAN_G, &dc->dc_chaninfo);
2180187841Ssam		if (!LIST_EMPTY(&rd->bands_11a))
2181178354Ssam			regdomain_addchans(ci, &rd->bands_11a, reg,
2182178354Ssam			    IEEE80211_CHAN_A, &dc->dc_chaninfo);
2183188258Ssam		if (!LIST_EMPTY(&rd->bands_11na) && dc->dc_htcaps != 0) {
2184178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
2185178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT20,
2186178354Ssam			    &dc->dc_chaninfo);
2187188258Ssam			if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2188188258Ssam				regdomain_addchans(ci, &rd->bands_11na, reg,
2189188258Ssam				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U,
2190188258Ssam				    &dc->dc_chaninfo);
2191188258Ssam				regdomain_addchans(ci, &rd->bands_11na, reg,
2192188258Ssam				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D,
2193188258Ssam				    &dc->dc_chaninfo);
2194188258Ssam			}
2195178354Ssam		}
2196188258Ssam		if (!LIST_EMPTY(&rd->bands_11ng) && dc->dc_htcaps != 0) {
2197178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
2198178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT20,
2199178354Ssam			    &dc->dc_chaninfo);
2200188258Ssam			if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2201188258Ssam				regdomain_addchans(ci, &rd->bands_11ng, reg,
2202188258Ssam				    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U,
2203188258Ssam				    &dc->dc_chaninfo);
2204188258Ssam				regdomain_addchans(ci, &rd->bands_11ng, reg,
2205188258Ssam				    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D,
2206188258Ssam				    &dc->dc_chaninfo);
2207188258Ssam			}
2208178354Ssam		}
2209178354Ssam		qsort(ci->ic_chans, ci->ic_nchans, sizeof(ci->ic_chans[0]),
2210178354Ssam		    regdomain_sort);
2211178354Ssam	} else
2212187801Ssam		memcpy(ci, &dc->dc_chaninfo,
2213187801Ssam		    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
2214178354Ssam}
2215178354Ssam
2216178354Ssamstatic void
2217178354Ssamlist_countries(void)
2218178354Ssam{
2219178354Ssam	struct regdata *rdp = getregdata();
2220178354Ssam	const struct country *cp;
2221178354Ssam	const struct regdomain *dp;
2222178354Ssam	int i;
2223178354Ssam
2224178354Ssam	i = 0;
2225178354Ssam	printf("\nCountry codes:\n");
2226178354Ssam	LIST_FOREACH(cp, &rdp->countries, next) {
2227178354Ssam		printf("%2s %-15.15s%s", cp->isoname,
2228178354Ssam		    cp->name, ((i+1)%4) == 0 ? "\n" : " ");
2229178354Ssam		i++;
2230178354Ssam	}
2231178354Ssam	i = 0;
2232178354Ssam	printf("\nRegulatory domains:\n");
2233178354Ssam	LIST_FOREACH(dp, &rdp->domains, next) {
2234178354Ssam		printf("%-15.15s%s", dp->name, ((i+1)%4) == 0 ? "\n" : " ");
2235178354Ssam		i++;
2236178354Ssam	}
2237178354Ssam	printf("\n");
2238178354Ssam}
2239178354Ssam
2240178354Ssamstatic void
2241178354Ssamdefaultcountry(const struct regdomain *rd)
2242178354Ssam{
2243178354Ssam	struct regdata *rdp = getregdata();
2244178354Ssam	const struct country *cc;
2245178354Ssam
2246178354Ssam	cc = lib80211_country_findbycc(rdp, rd->cc->code);
2247178354Ssam	if (cc == NULL)
2248178354Ssam		errx(1, "internal error, ISO country code %d not "
2249178354Ssam		    "defined for regdomain %s", rd->cc->code, rd->name);
2250178354Ssam	regdomain.country = cc->code;
2251178354Ssam	regdomain.isocc[0] = cc->isoname[0];
2252178354Ssam	regdomain.isocc[1] = cc->isoname[1];
2253178354Ssam}
2254178354Ssam
2255178354Ssamstatic
2256178354SsamDECL_CMD_FUNC(set80211regdomain, val, d)
2257178354Ssam{
2258178354Ssam	struct regdata *rdp = getregdata();
2259178354Ssam	const struct regdomain *rd;
2260178354Ssam
2261178354Ssam	rd = lib80211_regdomain_findbyname(rdp, val);
2262178354Ssam	if (rd == NULL) {
2263186104Ssam		char *eptr;
2264186104Ssam		long sku = strtol(val, &eptr, 0);
2265186104Ssam
2266186104Ssam		if (eptr != val)
2267186104Ssam			rd = lib80211_regdomain_findbysku(rdp, sku);
2268186104Ssam		if (eptr == val || rd == NULL)
2269178354Ssam			errx(1, "unknown regdomain %s", val);
2270178354Ssam	}
2271178354Ssam	getregdomain(s);
2272178354Ssam	regdomain.regdomain = rd->sku;
2273178354Ssam	if (regdomain.country == 0 && rd->cc != NULL) {
2274178354Ssam		/*
2275178354Ssam		 * No country code setup and there's a default
2276178354Ssam		 * one for this regdomain fill it in.
2277178354Ssam		 */
2278178354Ssam		defaultcountry(rd);
2279178354Ssam	}
2280178354Ssam	callback_register(setregdomain_cb, &regdomain);
2281178354Ssam}
2282178354Ssam
2283178354Ssamstatic
2284178354SsamDECL_CMD_FUNC(set80211country, val, d)
2285178354Ssam{
2286178354Ssam	struct regdata *rdp = getregdata();
2287178354Ssam	const struct country *cc;
2288178354Ssam
2289178354Ssam	cc = lib80211_country_findbyname(rdp, val);
2290178354Ssam	if (cc == NULL) {
2291186103Ssam		char *eptr;
2292186103Ssam		long code = strtol(val, &eptr, 0);
2293186103Ssam
2294186103Ssam		if (eptr != val)
2295186103Ssam			cc = lib80211_country_findbycc(rdp, code);
2296186103Ssam		if (eptr == val || cc == NULL)
2297178354Ssam			errx(1, "unknown ISO country code %s", val);
2298178354Ssam	}
2299178354Ssam	getregdomain(s);
2300178354Ssam	regdomain.regdomain = cc->rd->sku;
2301178354Ssam	regdomain.country = cc->code;
2302178354Ssam	regdomain.isocc[0] = cc->isoname[0];
2303178354Ssam	regdomain.isocc[1] = cc->isoname[1];
2304178354Ssam	callback_register(setregdomain_cb, &regdomain);
2305178354Ssam}
2306178354Ssam
2307178354Ssamstatic void
2308178354Ssamset80211location(const char *val, int d, int s, const struct afswtch *rafp)
2309178354Ssam{
2310178354Ssam	getregdomain(s);
2311178354Ssam	regdomain.location = d;
2312178354Ssam	callback_register(setregdomain_cb, &regdomain);
2313178354Ssam}
2314178354Ssam
2315178354Ssamstatic void
2316178354Ssamset80211ecm(const char *val, int d, int s, const struct afswtch *rafp)
2317178354Ssam{
2318178354Ssam	getregdomain(s);
2319178354Ssam	regdomain.ecm = d;
2320178354Ssam	callback_register(setregdomain_cb, &regdomain);
2321178354Ssam}
2322178354Ssam
2323178354Ssamstatic void
2324173275SsamLINE_INIT(char c)
2325173275Ssam{
2326173275Ssam	spacer = c;
2327173275Ssam	if (c == '\t')
2328173275Ssam		col = 8;
2329173275Ssam	else
2330173275Ssam		col = 1;
2331173275Ssam}
2332173275Ssam
2333173275Ssamstatic void
2334173275SsamLINE_BREAK(void)
2335173275Ssam{
2336173275Ssam	if (spacer != '\t') {
2337173275Ssam		printf("\n");
2338173275Ssam		spacer = '\t';
2339173275Ssam	}
2340173275Ssam	col = 8;		/* 8-col tab */
2341173275Ssam}
2342173275Ssam
2343173275Ssamstatic void
2344173275SsamLINE_CHECK(const char *fmt, ...)
2345173275Ssam{
2346173275Ssam	char buf[80];
2347173275Ssam	va_list ap;
2348173275Ssam	int n;
2349173275Ssam
2350173275Ssam	va_start(ap, fmt);
2351173275Ssam	n = vsnprintf(buf+1, sizeof(buf)-1, fmt, ap);
2352173275Ssam	va_end(ap);
2353173275Ssam	col += 1+n;
2354173275Ssam	if (col > MAXCOL) {
2355173275Ssam		LINE_BREAK();
2356173275Ssam		col += n;
2357173275Ssam	}
2358173275Ssam	buf[0] = spacer;
2359173275Ssam	printf("%s", buf);
2360173275Ssam	spacer = ' ';
2361173275Ssam}
2362173275Ssam
2363138593Ssamstatic int
2364170531Ssamgetmaxrate(const uint8_t rates[15], uint8_t nrates)
2365138593Ssam{
2366138593Ssam	int i, maxrate = -1;
2367138593Ssam
2368138593Ssam	for (i = 0; i < nrates; i++) {
2369138593Ssam		int rate = rates[i] & IEEE80211_RATE_VAL;
2370138593Ssam		if (rate > maxrate)
2371138593Ssam			maxrate = rate;
2372138593Ssam	}
2373138593Ssam	return maxrate / 2;
2374138593Ssam}
2375138593Ssam
2376138593Ssamstatic const char *
2377138593Ssamgetcaps(int capinfo)
2378138593Ssam{
2379138593Ssam	static char capstring[32];
2380138593Ssam	char *cp = capstring;
2381138593Ssam
2382138593Ssam	if (capinfo & IEEE80211_CAPINFO_ESS)
2383138593Ssam		*cp++ = 'E';
2384138593Ssam	if (capinfo & IEEE80211_CAPINFO_IBSS)
2385138593Ssam		*cp++ = 'I';
2386138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
2387138593Ssam		*cp++ = 'c';
2388138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
2389138593Ssam		*cp++ = 'C';
2390138593Ssam	if (capinfo & IEEE80211_CAPINFO_PRIVACY)
2391138593Ssam		*cp++ = 'P';
2392138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
2393138593Ssam		*cp++ = 'S';
2394138593Ssam	if (capinfo & IEEE80211_CAPINFO_PBCC)
2395138593Ssam		*cp++ = 'B';
2396138593Ssam	if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
2397138593Ssam		*cp++ = 'A';
2398138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
2399138593Ssam		*cp++ = 's';
2400138593Ssam	if (capinfo & IEEE80211_CAPINFO_RSN)
2401138593Ssam		*cp++ = 'R';
2402138593Ssam	if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
2403138593Ssam		*cp++ = 'D';
2404138593Ssam	*cp = '\0';
2405138593Ssam	return capstring;
2406138593Ssam}
2407138593Ssam
2408159885Ssamstatic const char *
2409159885Ssamgetflags(int flags)
2410159885Ssam{
2411159885Ssam	static char flagstring[32];
2412159885Ssam	char *cp = flagstring;
2413159885Ssam
2414159885Ssam	if (flags & IEEE80211_NODE_AUTH)
2415159885Ssam		*cp++ = 'A';
2416159885Ssam	if (flags & IEEE80211_NODE_QOS)
2417159885Ssam		*cp++ = 'Q';
2418159885Ssam	if (flags & IEEE80211_NODE_ERP)
2419159885Ssam		*cp++ = 'E';
2420159885Ssam	if (flags & IEEE80211_NODE_PWR_MGT)
2421159885Ssam		*cp++ = 'P';
2422173275Ssam	if (flags & IEEE80211_NODE_HT) {
2423170531Ssam		*cp++ = 'H';
2424173275Ssam		if (flags & IEEE80211_NODE_HTCOMPAT)
2425173275Ssam			*cp++ = '+';
2426173275Ssam	}
2427173275Ssam	if (flags & IEEE80211_NODE_WPS)
2428173275Ssam		*cp++ = 'W';
2429173275Ssam	if (flags & IEEE80211_NODE_TSN)
2430183261Ssam		*cp++ = 'N';
2431183261Ssam	if (flags & IEEE80211_NODE_AMPDU_TX)
2432173275Ssam		*cp++ = 'T';
2433183261Ssam	if (flags & IEEE80211_NODE_AMPDU_RX)
2434183261Ssam		*cp++ = 'R';
2435183261Ssam	if (flags & IEEE80211_NODE_MIMO_PS) {
2436183261Ssam		*cp++ = 'M';
2437183261Ssam		if (flags & IEEE80211_NODE_MIMO_RTS)
2438183261Ssam			*cp++ = '+';
2439183261Ssam	}
2440183261Ssam	if (flags & IEEE80211_NODE_RIFS)
2441183261Ssam		*cp++ = 'I';
2442194136Ssam	if (flags & IEEE80211_NODE_SGI40) {
2443194136Ssam		*cp++ = 'S';
2444194136Ssam		if (flags & IEEE80211_NODE_SGI20)
2445194136Ssam			*cp++ = '+';
2446194136Ssam	} else if (flags & IEEE80211_NODE_SGI20)
2447194136Ssam		*cp++ = 's';
2448194136Ssam	if (flags & IEEE80211_NODE_AMSDU_TX)
2449194136Ssam		*cp++ = 't';
2450194136Ssam	if (flags & IEEE80211_NODE_AMSDU_RX)
2451194136Ssam		*cp++ = 'r';
2452159885Ssam	*cp = '\0';
2453159885Ssam	return flagstring;
2454159885Ssam}
2455159885Ssam
2456138593Ssamstatic void
2457138593Ssamprintie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
2458138593Ssam{
2459138593Ssam	printf("%s", tag);
2460138593Ssam	if (verbose) {
2461138593Ssam		maxlen -= strlen(tag)+2;
2462138593Ssam		if (2*ielen > maxlen)
2463138593Ssam			maxlen--;
2464138593Ssam		printf("<");
2465138593Ssam		for (; ielen > 0; ie++, ielen--) {
2466138593Ssam			if (maxlen-- <= 0)
2467138593Ssam				break;
2468138593Ssam			printf("%02x", *ie);
2469138593Ssam		}
2470138593Ssam		if (ielen != 0)
2471138593Ssam			printf("-");
2472138593Ssam		printf(">");
2473138593Ssam	}
2474138593Ssam}
2475138593Ssam
2476170531Ssam#define LE_READ_2(p)					\
2477170531Ssam	((u_int16_t)					\
2478170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2479170531Ssam	  (((const u_int8_t *)(p))[1] <<  8)))
2480170531Ssam#define LE_READ_4(p)					\
2481170531Ssam	((u_int32_t)					\
2482170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2483170531Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
2484170531Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
2485170531Ssam	  (((const u_int8_t *)(p))[3] << 24)))
2486170531Ssam
2487138593Ssam/*
2488170531Ssam * NB: The decoding routines assume a properly formatted ie
2489170531Ssam *     which should be safe as the kernel only retains them
2490170531Ssam *     if they parse ok.
2491170531Ssam */
2492170531Ssam
2493170531Ssamstatic void
2494173275Ssamprintwmeparam(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2495170531Ssam{
2496170531Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
2497170531Ssam	static const char *acnames[] = { "BE", "BK", "VO", "VI" };
2498173275Ssam	const struct ieee80211_wme_param *wme =
2499173275Ssam	    (const struct ieee80211_wme_param *) ie;
2500170531Ssam	int i;
2501170531Ssam
2502170531Ssam	printf("%s", tag);
2503173275Ssam	if (!verbose)
2504173275Ssam		return;
2505173275Ssam	printf("<qosinfo 0x%x", wme->param_qosInfo);
2506173275Ssam	ie += offsetof(struct ieee80211_wme_param, params_acParams);
2507173275Ssam	for (i = 0; i < WME_NUM_AC; i++) {
2508173275Ssam		const struct ieee80211_wme_acparams *ac =
2509173275Ssam		    &wme->params_acParams[i];
2510173275Ssam
2511173275Ssam		printf(" %s[%saifsn %u cwmin %u cwmax %u txop %u]"
2512173275Ssam			, acnames[i]
2513173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_ACM) ? "acm " : ""
2514173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_AIFSN)
2515173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMIN)
2516173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMAX)
2517173275Ssam			, LE_READ_2(&ac->acp_txop)
2518173275Ssam		);
2519170531Ssam	}
2520173275Ssam	printf(">");
2521170531Ssam#undef MS
2522170531Ssam}
2523170531Ssam
2524170531Ssamstatic void
2525173275Ssamprintwmeinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2526173275Ssam{
2527173275Ssam	printf("%s", tag);
2528173275Ssam	if (verbose) {
2529173275Ssam		const struct ieee80211_wme_info *wme =
2530173275Ssam		    (const struct ieee80211_wme_info *) ie;
2531173275Ssam		printf("<version 0x%x info 0x%x>",
2532173275Ssam		    wme->wme_version, wme->wme_info);
2533173275Ssam	}
2534173275Ssam}
2535173275Ssam
2536173275Ssamstatic void
2537297007Sadrianprintvhtcap(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2538297007Sadrian{
2539297007Sadrian	printf("%s", tag);
2540297007Sadrian	if (verbose) {
2541297007Sadrian		const struct ieee80211_ie_vhtcap *vhtcap =
2542297007Sadrian		    (const struct ieee80211_ie_vhtcap *) ie;
2543297007Sadrian		uint32_t vhtcap_info = LE_READ_4(&vhtcap->vht_cap_info);
2544297007Sadrian
2545297007Sadrian		printf("<cap 0x%08x", vhtcap_info);
2546297007Sadrian		printf(" rx_mcs_map 0x%x",
2547297007Sadrian		    LE_READ_2(&vhtcap->supp_mcs.rx_mcs_map));
2548297007Sadrian		printf(" rx_highest %d",
2549297007Sadrian		    LE_READ_2(&vhtcap->supp_mcs.rx_highest) & 0x1fff);
2550297007Sadrian		printf(" tx_mcs_map 0x%x",
2551297007Sadrian		    LE_READ_2(&vhtcap->supp_mcs.tx_mcs_map));
2552297007Sadrian		printf(" tx_highest %d",
2553297007Sadrian		    LE_READ_2(&vhtcap->supp_mcs.tx_highest) & 0x1fff);
2554297007Sadrian
2555297007Sadrian		printf(">");
2556297007Sadrian	}
2557297007Sadrian}
2558297007Sadrian
2559297007Sadrianstatic void
2560297007Sadrianprintvhtinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2561297007Sadrian{
2562297007Sadrian	printf("%s", tag);
2563297007Sadrian	if (verbose) {
2564297007Sadrian		const struct ieee80211_ie_vht_operation *vhtinfo =
2565297007Sadrian		    (const struct ieee80211_ie_vht_operation *) ie;
2566297007Sadrian
2567297007Sadrian		printf("<chw %d freq1_idx %d freq2_idx %d basic_mcs_set 0x%04x>",
2568297007Sadrian		    vhtinfo->chan_width,
2569297007Sadrian		    vhtinfo->center_freq_seg1_idx,
2570297007Sadrian		    vhtinfo->center_freq_seg2_idx,
2571297007Sadrian		    LE_READ_2(&vhtinfo->basic_mcs_set));
2572297007Sadrian	}
2573297007Sadrian}
2574297007Sadrian
2575297007Sadrianstatic void
2576298252Sadrianprintvhtpwrenv(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2577298252Sadrian{
2578298252Sadrian	printf("%s", tag);
2579298252Sadrian	static const char *txpwrmap[] = {
2580298252Sadrian		"20",
2581298252Sadrian		"40",
2582298252Sadrian		"80",
2583298252Sadrian		"160",
2584298252Sadrian	};
2585298252Sadrian	if (verbose) {
2586298252Sadrian		const struct ieee80211_ie_vht_txpwrenv *vhtpwr =
2587298252Sadrian		    (const struct ieee80211_ie_vht_txpwrenv *) ie;
2588298252Sadrian		int i, n;
2589298252Sadrian		const char *sep = "";
2590298252Sadrian
2591298252Sadrian		/* Get count; trim at ielen */
2592298252Sadrian		n = (vhtpwr->tx_info &
2593298252Sadrian		    IEEE80211_VHT_TXPWRENV_INFO_COUNT_MASK) + 1;
2594298252Sadrian		/* Trim at ielen */
2595298252Sadrian		if (n > ielen - 3)
2596298252Sadrian			n = ielen - 3;
2597298252Sadrian		printf("<tx_info 0x%02x pwr:[", vhtpwr->tx_info);
2598298252Sadrian		for (i = 0; i < n; i++) {
2599298252Sadrian			printf("%s%s:%.2f", sep, txpwrmap[i],
2600298252Sadrian			    ((float) ((int8_t) ie[i+3])) / 2.0);
2601298252Sadrian			sep = " ";
2602298252Sadrian		}
2603298252Sadrian
2604298252Sadrian		printf("]>");
2605298252Sadrian	}
2606298252Sadrian}
2607298252Sadrian
2608298252Sadrianstatic void
2609173275Ssamprinthtcap(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2610173275Ssam{
2611173275Ssam	printf("%s", tag);
2612173275Ssam	if (verbose) {
2613173275Ssam		const struct ieee80211_ie_htcap *htcap =
2614173275Ssam		    (const struct ieee80211_ie_htcap *) ie;
2615173275Ssam		const char *sep;
2616173275Ssam		int i, j;
2617173275Ssam
2618173275Ssam		printf("<cap 0x%x param 0x%x",
2619173275Ssam		    LE_READ_2(&htcap->hc_cap), htcap->hc_param);
2620173275Ssam		printf(" mcsset[");
2621173275Ssam		sep = "";
2622173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2623173275Ssam			if (isset(htcap->hc_mcsset, i)) {
2624173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2625173275Ssam					if (isclr(htcap->hc_mcsset, j))
2626173275Ssam						break;
2627173275Ssam				j--;
2628173275Ssam				if (i == j)
2629173275Ssam					printf("%s%u", sep, i);
2630173275Ssam				else
2631173275Ssam					printf("%s%u-%u", sep, i, j);
2632173275Ssam				i += j-i;
2633173275Ssam				sep = ",";
2634173275Ssam			}
2635173275Ssam		printf("] extcap 0x%x txbf 0x%x antenna 0x%x>",
2636173275Ssam		    LE_READ_2(&htcap->hc_extcap),
2637173275Ssam		    LE_READ_4(&htcap->hc_txbf),
2638173275Ssam		    htcap->hc_antenna);
2639173275Ssam	}
2640173275Ssam}
2641173275Ssam
2642173275Ssamstatic void
2643173275Ssamprinthtinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2644173275Ssam{
2645173275Ssam	printf("%s", tag);
2646173275Ssam	if (verbose) {
2647173275Ssam		const struct ieee80211_ie_htinfo *htinfo =
2648173275Ssam		    (const struct ieee80211_ie_htinfo *) ie;
2649173275Ssam		const char *sep;
2650173275Ssam		int i, j;
2651173275Ssam
2652173275Ssam		printf("<ctl %u, %x,%x,%x,%x", htinfo->hi_ctrlchannel,
2653173275Ssam		    htinfo->hi_byte1, htinfo->hi_byte2, htinfo->hi_byte3,
2654173275Ssam		    LE_READ_2(&htinfo->hi_byte45));
2655173275Ssam		printf(" basicmcs[");
2656173275Ssam		sep = "";
2657173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2658173275Ssam			if (isset(htinfo->hi_basicmcsset, i)) {
2659173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2660173275Ssam					if (isclr(htinfo->hi_basicmcsset, j))
2661173275Ssam						break;
2662173275Ssam				j--;
2663173275Ssam				if (i == j)
2664173275Ssam					printf("%s%u", sep, i);
2665173275Ssam				else
2666173275Ssam					printf("%s%u-%u", sep, i, j);
2667173275Ssam				i += j-i;
2668173275Ssam				sep = ",";
2669173275Ssam			}
2670173275Ssam		printf("]>");
2671173275Ssam	}
2672173275Ssam}
2673173275Ssam
2674173275Ssamstatic void
2675170531Ssamprintathie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2676170531Ssam{
2677170531Ssam
2678170531Ssam	printf("%s", tag);
2679170531Ssam	if (verbose) {
2680170531Ssam		const struct ieee80211_ath_ie *ath =
2681170531Ssam			(const struct ieee80211_ath_ie *)ie;
2682170531Ssam
2683170531Ssam		printf("<");
2684170531Ssam		if (ath->ath_capability & ATHEROS_CAP_TURBO_PRIME)
2685170531Ssam			printf("DTURBO,");
2686170531Ssam		if (ath->ath_capability & ATHEROS_CAP_COMPRESSION)
2687170531Ssam			printf("COMP,");
2688170531Ssam		if (ath->ath_capability & ATHEROS_CAP_FAST_FRAME)
2689170531Ssam			printf("FF,");
2690170531Ssam		if (ath->ath_capability & ATHEROS_CAP_XR)
2691170531Ssam			printf("XR,");
2692170531Ssam		if (ath->ath_capability & ATHEROS_CAP_AR)
2693170531Ssam			printf("AR,");
2694170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BURST)
2695170531Ssam			printf("BURST,");
2696170531Ssam		if (ath->ath_capability & ATHEROS_CAP_WME)
2697170531Ssam			printf("WME,");
2698170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BOOST)
2699170531Ssam			printf("BOOST,");
2700170531Ssam		printf("0x%x>", LE_READ_2(ath->ath_defkeyix));
2701170531Ssam	}
2702170531Ssam}
2703170531Ssam
2704195618Srpaulo
2705195618Srpaulostatic void
2706195618Srpauloprintmeshconf(const char *tag, const uint8_t *ie, size_t ielen, int maxlen)
2707195618Srpaulo{
2708195618Srpaulo#define MATCHOUI(field, oui, string)					\
2709195618Srpaulodo {									\
2710195618Srpaulo	if (memcmp(field, oui, 4) == 0)					\
2711195618Srpaulo		printf("%s", string);					\
2712195618Srpaulo} while (0)
2713195618Srpaulo
2714195618Srpaulo	printf("%s", tag);
2715195618Srpaulo	if (verbose) {
2716195618Srpaulo		const struct ieee80211_meshconf_ie *mconf =
2717195618Srpaulo			(const struct ieee80211_meshconf_ie *)ie;
2718197980Srpaulo		printf("<PATH:");
2719197980Srpaulo		if (mconf->conf_pselid == IEEE80211_MESHCONF_PATH_HWMP)
2720197980Srpaulo			printf("HWMP");
2721197980Srpaulo		else
2722197980Srpaulo			printf("UNKNOWN");
2723195618Srpaulo		printf(" LINK:");
2724197980Srpaulo		if (mconf->conf_pmetid == IEEE80211_MESHCONF_METRIC_AIRTIME)
2725197980Srpaulo			printf("AIRTIME");
2726197980Srpaulo		else
2727197980Srpaulo			printf("UNKNOWN");
2728195618Srpaulo		printf(" CONGESTION:");
2729197980Srpaulo		if (mconf->conf_ccid == IEEE80211_MESHCONF_CC_DISABLED)
2730197980Srpaulo			printf("DISABLED");
2731197980Srpaulo		else
2732197980Srpaulo			printf("UNKNOWN");
2733195618Srpaulo		printf(" SYNC:");
2734197980Srpaulo		if (mconf->conf_syncid == IEEE80211_MESHCONF_SYNC_NEIGHOFF)
2735197980Srpaulo			printf("NEIGHOFF");
2736197980Srpaulo		else
2737197980Srpaulo			printf("UNKNOWN");
2738195618Srpaulo		printf(" AUTH:");
2739197980Srpaulo		if (mconf->conf_authid == IEEE80211_MESHCONF_AUTH_DISABLED)
2740197980Srpaulo			printf("DISABLED");
2741197980Srpaulo		else
2742197980Srpaulo			printf("UNKNOWN");
2743195618Srpaulo		printf(" FORM:0x%x CAPS:0x%x>", mconf->conf_form,
2744195618Srpaulo		    mconf->conf_cap);
2745195618Srpaulo	}
2746195618Srpaulo#undef MATCHOUI
2747195618Srpaulo}
2748195618Srpaulo
2749297007Sadrianstatic void
2750297007Sadrianprintbssload(const char *tag, const uint8_t *ie, size_t ielen, int maxlen)
2751297007Sadrian{
2752297007Sadrian	printf("%s", tag);
2753297007Sadrian	if (verbose) {
2754297007Sadrian		const struct ieee80211_bss_load_ie *bssload =
2755297007Sadrian		    (const struct ieee80211_bss_load_ie *) ie;
2756297007Sadrian		printf("<sta count %d, chan load %d, aac %d>",
2757297007Sadrian		    LE_READ_2(&bssload->sta_count),
2758297007Sadrian		    bssload->chan_load,
2759297007Sadrian		    bssload->aac);
2760297007Sadrian	}
2761297007Sadrian}
2762297007Sadrian
2763297010Sadrianstatic void
2764297010Sadrianprintapchanrep(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2765297010Sadrian{
2766297010Sadrian	printf("%s", tag);
2767297010Sadrian	if (verbose) {
2768297010Sadrian		const struct ieee80211_ap_chan_report_ie *ap =
2769297010Sadrian		    (const struct ieee80211_ap_chan_report_ie *) ie;
2770297010Sadrian		const char *sep = "";
2771297010Sadrian		int i;
2772297010Sadrian
2773297010Sadrian		printf("<class %u, chan:[", ap->i_class);
2774297010Sadrian
2775297010Sadrian		for (i = 3; i < ielen; i++) {
2776297010Sadrian			printf("%s%u", sep, ie[i]);
2777297010Sadrian			sep = ",";
2778297010Sadrian		}
2779297010Sadrian		printf("]>");
2780297010Sadrian	}
2781297010Sadrian}
2782297010Sadrian
2783170531Ssamstatic const char *
2784170531Ssamwpa_cipher(const u_int8_t *sel)
2785170531Ssam{
2786170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2787170531Ssam	u_int32_t w = LE_READ_4(sel);
2788170531Ssam
2789170531Ssam	switch (w) {
2790170531Ssam	case WPA_SEL(WPA_CSE_NULL):
2791170531Ssam		return "NONE";
2792170531Ssam	case WPA_SEL(WPA_CSE_WEP40):
2793170531Ssam		return "WEP40";
2794170531Ssam	case WPA_SEL(WPA_CSE_WEP104):
2795170531Ssam		return "WEP104";
2796170531Ssam	case WPA_SEL(WPA_CSE_TKIP):
2797170531Ssam		return "TKIP";
2798170531Ssam	case WPA_SEL(WPA_CSE_CCMP):
2799170531Ssam		return "AES-CCMP";
2800170531Ssam	}
2801170531Ssam	return "?";		/* NB: so 1<< is discarded */
2802170531Ssam#undef WPA_SEL
2803170531Ssam}
2804170531Ssam
2805170531Ssamstatic const char *
2806170531Ssamwpa_keymgmt(const u_int8_t *sel)
2807170531Ssam{
2808170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2809170531Ssam	u_int32_t w = LE_READ_4(sel);
2810170531Ssam
2811170531Ssam	switch (w) {
2812170531Ssam	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
2813170531Ssam		return "8021X-UNSPEC";
2814170531Ssam	case WPA_SEL(WPA_ASE_8021X_PSK):
2815170531Ssam		return "8021X-PSK";
2816170531Ssam	case WPA_SEL(WPA_ASE_NONE):
2817170531Ssam		return "NONE";
2818170531Ssam	}
2819170531Ssam	return "?";
2820170531Ssam#undef WPA_SEL
2821170531Ssam}
2822170531Ssam
2823170531Ssamstatic void
2824170531Ssamprintwpaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2825170531Ssam{
2826170531Ssam	u_int8_t len = ie[1];
2827170531Ssam
2828170531Ssam	printf("%s", tag);
2829170531Ssam	if (verbose) {
2830170531Ssam		const char *sep;
2831170531Ssam		int n;
2832170531Ssam
2833170531Ssam		ie += 6, len -= 4;		/* NB: len is payload only */
2834170531Ssam
2835170531Ssam		printf("<v%u", LE_READ_2(ie));
2836170531Ssam		ie += 2, len -= 2;
2837170531Ssam
2838170531Ssam		printf(" mc:%s", wpa_cipher(ie));
2839170531Ssam		ie += 4, len -= 4;
2840170531Ssam
2841170531Ssam		/* unicast ciphers */
2842170531Ssam		n = LE_READ_2(ie);
2843170531Ssam		ie += 2, len -= 2;
2844170531Ssam		sep = " uc:";
2845170531Ssam		for (; n > 0; n--) {
2846170531Ssam			printf("%s%s", sep, wpa_cipher(ie));
2847170531Ssam			ie += 4, len -= 4;
2848170531Ssam			sep = "+";
2849170531Ssam		}
2850170531Ssam
2851170531Ssam		/* key management algorithms */
2852170531Ssam		n = LE_READ_2(ie);
2853170531Ssam		ie += 2, len -= 2;
2854170531Ssam		sep = " km:";
2855170531Ssam		for (; n > 0; n--) {
2856170531Ssam			printf("%s%s", sep, wpa_keymgmt(ie));
2857170531Ssam			ie += 4, len -= 4;
2858170531Ssam			sep = "+";
2859170531Ssam		}
2860170531Ssam
2861170531Ssam		if (len > 2)		/* optional capabilities */
2862170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2863170531Ssam		printf(">");
2864170531Ssam	}
2865170531Ssam}
2866170531Ssam
2867170531Ssamstatic const char *
2868170531Ssamrsn_cipher(const u_int8_t *sel)
2869170531Ssam{
2870170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2871170531Ssam	u_int32_t w = LE_READ_4(sel);
2872170531Ssam
2873170531Ssam	switch (w) {
2874170531Ssam	case RSN_SEL(RSN_CSE_NULL):
2875170531Ssam		return "NONE";
2876170531Ssam	case RSN_SEL(RSN_CSE_WEP40):
2877170531Ssam		return "WEP40";
2878170531Ssam	case RSN_SEL(RSN_CSE_WEP104):
2879170531Ssam		return "WEP104";
2880170531Ssam	case RSN_SEL(RSN_CSE_TKIP):
2881170531Ssam		return "TKIP";
2882170531Ssam	case RSN_SEL(RSN_CSE_CCMP):
2883170531Ssam		return "AES-CCMP";
2884170531Ssam	case RSN_SEL(RSN_CSE_WRAP):
2885170531Ssam		return "AES-OCB";
2886170531Ssam	}
2887170531Ssam	return "?";
2888170531Ssam#undef WPA_SEL
2889170531Ssam}
2890170531Ssam
2891170531Ssamstatic const char *
2892170531Ssamrsn_keymgmt(const u_int8_t *sel)
2893170531Ssam{
2894170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2895170531Ssam	u_int32_t w = LE_READ_4(sel);
2896170531Ssam
2897170531Ssam	switch (w) {
2898170531Ssam	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
2899170531Ssam		return "8021X-UNSPEC";
2900170531Ssam	case RSN_SEL(RSN_ASE_8021X_PSK):
2901170531Ssam		return "8021X-PSK";
2902170531Ssam	case RSN_SEL(RSN_ASE_NONE):
2903170531Ssam		return "NONE";
2904170531Ssam	}
2905170531Ssam	return "?";
2906170531Ssam#undef RSN_SEL
2907170531Ssam}
2908170531Ssam
2909170531Ssamstatic void
2910170531Ssamprintrsnie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2911170531Ssam{
2912170531Ssam	printf("%s", tag);
2913170531Ssam	if (verbose) {
2914170531Ssam		const char *sep;
2915170531Ssam		int n;
2916170531Ssam
2917173275Ssam		ie += 2, ielen -= 2;
2918170531Ssam
2919170531Ssam		printf("<v%u", LE_READ_2(ie));
2920173275Ssam		ie += 2, ielen -= 2;
2921170531Ssam
2922170531Ssam		printf(" mc:%s", rsn_cipher(ie));
2923173275Ssam		ie += 4, ielen -= 4;
2924170531Ssam
2925170531Ssam		/* unicast ciphers */
2926170531Ssam		n = LE_READ_2(ie);
2927173275Ssam		ie += 2, ielen -= 2;
2928170531Ssam		sep = " uc:";
2929170531Ssam		for (; n > 0; n--) {
2930170531Ssam			printf("%s%s", sep, rsn_cipher(ie));
2931173275Ssam			ie += 4, ielen -= 4;
2932170531Ssam			sep = "+";
2933170531Ssam		}
2934170531Ssam
2935170531Ssam		/* key management algorithms */
2936170531Ssam		n = LE_READ_2(ie);
2937173275Ssam		ie += 2, ielen -= 2;
2938170531Ssam		sep = " km:";
2939170531Ssam		for (; n > 0; n--) {
2940170531Ssam			printf("%s%s", sep, rsn_keymgmt(ie));
2941173275Ssam			ie += 4, ielen -= 4;
2942170531Ssam			sep = "+";
2943170531Ssam		}
2944170531Ssam
2945173275Ssam		if (ielen > 2)		/* optional capabilities */
2946170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2947170531Ssam		/* XXXPMKID */
2948170531Ssam		printf(">");
2949170531Ssam	}
2950170531Ssam}
2951170531Ssam
2952181454Ssam/* XXX move to a public include file */
2953181454Ssam#define IEEE80211_WPS_DEV_PASS_ID	0x1012
2954181454Ssam#define IEEE80211_WPS_SELECTED_REG	0x1041
2955181454Ssam#define IEEE80211_WPS_SETUP_STATE	0x1044
2956181454Ssam#define IEEE80211_WPS_UUID_E		0x1047
2957181454Ssam#define IEEE80211_WPS_VERSION		0x104a
2958181454Ssam
2959181454Ssam#define BE_READ_2(p)					\
2960181454Ssam	((u_int16_t)					\
2961181454Ssam	 ((((const u_int8_t *)(p))[1]      ) |		\
2962181454Ssam	  (((const u_int8_t *)(p))[0] <<  8)))
2963181454Ssam
2964181454Ssamstatic void
2965181454Ssamprintwpsie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2966181454Ssam{
2967181454Ssam	u_int8_t len = ie[1];
2968181454Ssam
2969181454Ssam	printf("%s", tag);
2970181454Ssam	if (verbose) {
2971181454Ssam		static const char *dev_pass_id[] = {
2972181454Ssam			"D",	/* Default (PIN) */
2973181454Ssam			"U",	/* User-specified */
2974181454Ssam			"M",	/* Machine-specified */
2975181454Ssam			"K",	/* Rekey */
2976181454Ssam			"P",	/* PushButton */
2977181454Ssam			"R"	/* Registrar-specified */
2978181454Ssam		};
2979181454Ssam		int n;
2980181454Ssam
2981181454Ssam		ie +=6, len -= 4;		/* NB: len is payload only */
2982181454Ssam
2983181454Ssam		/* WPS IE in Beacon and Probe Resp frames have different fields */
2984181454Ssam		printf("<");
2985181454Ssam		while (len) {
2986181454Ssam			uint16_t tlv_type = BE_READ_2(ie);
2987181454Ssam			uint16_t tlv_len  = BE_READ_2(ie + 2);
2988181454Ssam
2989181454Ssam			ie += 4, len -= 4;
2990181454Ssam
2991181454Ssam			switch (tlv_type) {
2992181454Ssam			case IEEE80211_WPS_VERSION:
2993181454Ssam				printf("v:%d.%d", *ie >> 4, *ie & 0xf);
2994181454Ssam				break;
2995181454Ssam			case IEEE80211_WPS_SETUP_STATE:
2996181454Ssam				/* Only 1 and 2 are valid */
2997181454Ssam				if (*ie == 0 || *ie >= 3)
2998181454Ssam					printf(" state:B");
2999181454Ssam				else
3000181454Ssam					printf(" st:%s", *ie == 1 ? "N" : "C");
3001181454Ssam				break;
3002181454Ssam			case IEEE80211_WPS_SELECTED_REG:
3003181454Ssam				printf(" sel:%s", *ie ? "T" : "F");
3004181454Ssam				break;
3005181454Ssam			case IEEE80211_WPS_DEV_PASS_ID:
3006181454Ssam				n = LE_READ_2(ie);
3007288305Sngie				if (n < nitems(dev_pass_id))
3008181454Ssam					printf(" dpi:%s", dev_pass_id[n]);
3009181454Ssam				break;
3010181454Ssam			case IEEE80211_WPS_UUID_E:
3011181454Ssam				printf(" uuid-e:");
3012181454Ssam				for (n = 0; n < (tlv_len - 1); n++)
3013181454Ssam					printf("%02x-", ie[n]);
3014181454Ssam				printf("%02x", ie[n]);
3015181454Ssam				break;
3016181454Ssam			}
3017181454Ssam			ie += tlv_len, len -= tlv_len;
3018181454Ssam		}
3019181454Ssam		printf(">");
3020181454Ssam	}
3021181454Ssam}
3022181454Ssam
3023186904Ssamstatic void
3024186904Ssamprinttdmaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
3025186904Ssam{
3026186904Ssam	printf("%s", tag);
3027186904Ssam	if (verbose && ielen >= sizeof(struct ieee80211_tdma_param)) {
3028186904Ssam		const struct ieee80211_tdma_param *tdma =
3029186904Ssam		   (const struct ieee80211_tdma_param *) ie;
3030186904Ssam
3031186904Ssam		/* XXX tstamp */
3032186904Ssam		printf("<v%u slot:%u slotcnt:%u slotlen:%u bintval:%u inuse:0x%x>",
3033186904Ssam		    tdma->tdma_version, tdma->tdma_slot, tdma->tdma_slotcnt,
3034186904Ssam		    LE_READ_2(&tdma->tdma_slotlen), tdma->tdma_bintval,
3035186904Ssam		    tdma->tdma_inuse[0]);
3036186904Ssam	}
3037186904Ssam}
3038186904Ssam
3039170531Ssam/*
3040138593Ssam * Copy the ssid string contents into buf, truncating to fit.  If the
3041138593Ssam * ssid is entirely printable then just copy intact.  Otherwise convert
3042138593Ssam * to hexadecimal.  If the result is truncated then replace the last
3043138593Ssam * three characters with "...".
3044138593Ssam */
3045146873Sjhbstatic int
3046138593Ssamcopy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
3047138593Ssam{
3048138593Ssam	const u_int8_t *p;
3049138593Ssam	size_t maxlen;
3050298556Spfg	u_int i;
3051138593Ssam
3052138593Ssam	if (essid_len > bufsize)
3053138593Ssam		maxlen = bufsize;
3054138593Ssam	else
3055138593Ssam		maxlen = essid_len;
3056138593Ssam	/* determine printable or not */
3057138593Ssam	for (i = 0, p = essid; i < maxlen; i++, p++) {
3058138593Ssam		if (*p < ' ' || *p > 0x7e)
3059138593Ssam			break;
3060138593Ssam	}
3061138593Ssam	if (i != maxlen) {		/* not printable, print as hex */
3062138593Ssam		if (bufsize < 3)
3063138593Ssam			return 0;
3064138593Ssam		strlcpy(buf, "0x", bufsize);
3065138593Ssam		bufsize -= 2;
3066138593Ssam		p = essid;
3067138593Ssam		for (i = 0; i < maxlen && bufsize >= 2; i++) {
3068147489Savatar			sprintf(&buf[2+2*i], "%02x", p[i]);
3069138593Ssam			bufsize -= 2;
3070138593Ssam		}
3071147489Savatar		if (i != essid_len)
3072147489Savatar			memcpy(&buf[2+2*i-3], "...", 3);
3073138593Ssam	} else {			/* printable, truncate as needed */
3074138593Ssam		memcpy(buf, essid, maxlen);
3075147489Savatar		if (maxlen != essid_len)
3076147489Savatar			memcpy(&buf[maxlen-3], "...", 3);
3077138593Ssam	}
3078138593Ssam	return maxlen;
3079138593Ssam}
3080138593Ssam
3081173275Ssamstatic void
3082173275Ssamprintssid(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
3083173275Ssam{
3084173275Ssam	char ssid[2*IEEE80211_NWID_LEN+1];
3085173275Ssam
3086173275Ssam	printf("%s<%.*s>", tag, copy_essid(ssid, maxlen, ie+2, ie[1]), ssid);
3087173275Ssam}
3088173275Ssam
3089173275Ssamstatic void
3090173275Ssamprintrates(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
3091173275Ssam{
3092173275Ssam	const char *sep;
3093173275Ssam	int i;
3094173275Ssam
3095173275Ssam	printf("%s", tag);
3096173275Ssam	sep = "<";
3097173275Ssam	for (i = 2; i < ielen; i++) {
3098173275Ssam		printf("%s%s%d", sep,
3099173275Ssam		    ie[i] & IEEE80211_RATE_BASIC ? "B" : "",
3100173275Ssam		    ie[i] & IEEE80211_RATE_VAL);
3101173275Ssam		sep = ",";
3102173275Ssam	}
3103173275Ssam	printf(">");
3104173275Ssam}
3105173275Ssam
3106173275Ssamstatic void
3107173275Ssamprintcountry(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
3108173275Ssam{
3109173275Ssam	const struct ieee80211_country_ie *cie =
3110173275Ssam	   (const struct ieee80211_country_ie *) ie;
3111173275Ssam	int i, nbands, schan, nchan;
3112173275Ssam
3113173275Ssam	printf("%s<%c%c%c", tag, cie->cc[0], cie->cc[1], cie->cc[2]);
3114173275Ssam	nbands = (cie->len - 3) / sizeof(cie->band[0]);
3115173275Ssam	for (i = 0; i < nbands; i++) {
3116173275Ssam		schan = cie->band[i].schan;
3117173275Ssam		nchan = cie->band[i].nchan;
3118173275Ssam		if (nchan != 1)
3119173275Ssam			printf(" %u-%u,%u", schan, schan + nchan-1,
3120173275Ssam			    cie->band[i].maxtxpwr);
3121173275Ssam		else
3122173275Ssam			printf(" %u,%u", schan, cie->band[i].maxtxpwr);
3123173275Ssam	}
3124173275Ssam	printf(">");
3125173275Ssam}
3126173275Ssam
3127178354Ssamstatic __inline int
3128138593Ssamiswpaoui(const u_int8_t *frm)
3129138593Ssam{
3130138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
3131138593Ssam}
3132138593Ssam
3133178354Ssamstatic __inline int
3134173275Ssamiswmeinfo(const u_int8_t *frm)
3135138593Ssam{
3136173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3137173275Ssam		frm[6] == WME_INFO_OUI_SUBTYPE;
3138138593Ssam}
3139138593Ssam
3140178354Ssamstatic __inline int
3141173275Ssamiswmeparam(const u_int8_t *frm)
3142173275Ssam{
3143173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3144173275Ssam		frm[6] == WME_PARAM_OUI_SUBTYPE;
3145173275Ssam}
3146173275Ssam
3147178354Ssamstatic __inline int
3148138593Ssamisatherosoui(const u_int8_t *frm)
3149138593Ssam{
3150138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
3151138593Ssam}
3152138593Ssam
3153181454Ssamstatic __inline int
3154186904Ssamistdmaoui(const uint8_t *frm)
3155186904Ssam{
3156186904Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((TDMA_OUI_TYPE<<24)|TDMA_OUI);
3157186904Ssam}
3158186904Ssam
3159186904Ssamstatic __inline int
3160181454Ssamiswpsoui(const uint8_t *frm)
3161181454Ssam{
3162181454Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPS_OUI_TYPE<<24)|WPA_OUI);
3163181454Ssam}
3164181454Ssam
3165173275Ssamstatic const char *
3166173275Ssamiename(int elemid)
3167173275Ssam{
3168173275Ssam	switch (elemid) {
3169173275Ssam	case IEEE80211_ELEMID_FHPARMS:	return " FHPARMS";
3170173275Ssam	case IEEE80211_ELEMID_CFPARMS:	return " CFPARMS";
3171173275Ssam	case IEEE80211_ELEMID_TIM:	return " TIM";
3172173275Ssam	case IEEE80211_ELEMID_IBSSPARMS:return " IBSSPARMS";
3173296823Sadrian	case IEEE80211_ELEMID_BSSLOAD:	return " BSSLOAD";
3174173275Ssam	case IEEE80211_ELEMID_CHALLENGE:return " CHALLENGE";
3175173275Ssam	case IEEE80211_ELEMID_PWRCNSTR:	return " PWRCNSTR";
3176173275Ssam	case IEEE80211_ELEMID_PWRCAP:	return " PWRCAP";
3177173275Ssam	case IEEE80211_ELEMID_TPCREQ:	return " TPCREQ";
3178173275Ssam	case IEEE80211_ELEMID_TPCREP:	return " TPCREP";
3179173275Ssam	case IEEE80211_ELEMID_SUPPCHAN:	return " SUPPCHAN";
3180193447Ssam	case IEEE80211_ELEMID_CSA:	return " CSA";
3181173275Ssam	case IEEE80211_ELEMID_MEASREQ:	return " MEASREQ";
3182173275Ssam	case IEEE80211_ELEMID_MEASREP:	return " MEASREP";
3183173275Ssam	case IEEE80211_ELEMID_QUIET:	return " QUIET";
3184173275Ssam	case IEEE80211_ELEMID_IBSSDFS:	return " IBSSDFS";
3185173275Ssam	case IEEE80211_ELEMID_TPC:	return " TPC";
3186173275Ssam	case IEEE80211_ELEMID_CCKM:	return " CCKM";
3187173275Ssam	}
3188173275Ssam	return " ???";
3189173275Ssam}
3190173275Ssam
3191138593Ssamstatic void
3192138593Ssamprinties(const u_int8_t *vp, int ielen, int maxcols)
3193138593Ssam{
3194138593Ssam	while (ielen > 0) {
3195138593Ssam		switch (vp[0]) {
3196173275Ssam		case IEEE80211_ELEMID_SSID:
3197173275Ssam			if (verbose)
3198173275Ssam				printssid(" SSID", vp, 2+vp[1], maxcols);
3199173275Ssam			break;
3200173275Ssam		case IEEE80211_ELEMID_RATES:
3201173275Ssam		case IEEE80211_ELEMID_XRATES:
3202173275Ssam			if (verbose)
3203173275Ssam				printrates(vp[0] == IEEE80211_ELEMID_RATES ?
3204173275Ssam				    " RATES" : " XRATES", vp, 2+vp[1], maxcols);
3205173275Ssam			break;
3206173275Ssam		case IEEE80211_ELEMID_DSPARMS:
3207173275Ssam			if (verbose)
3208173275Ssam				printf(" DSPARMS<%u>", vp[2]);
3209173275Ssam			break;
3210173275Ssam		case IEEE80211_ELEMID_COUNTRY:
3211173275Ssam			if (verbose)
3212173275Ssam				printcountry(" COUNTRY", vp, 2+vp[1], maxcols);
3213173275Ssam			break;
3214173275Ssam		case IEEE80211_ELEMID_ERP:
3215173275Ssam			if (verbose)
3216173275Ssam				printf(" ERP<0x%x>", vp[2]);
3217173275Ssam			break;
3218138593Ssam		case IEEE80211_ELEMID_VENDOR:
3219138593Ssam			if (iswpaoui(vp))
3220170531Ssam				printwpaie(" WPA", vp, 2+vp[1], maxcols);
3221173275Ssam			else if (iswmeinfo(vp))
3222173275Ssam				printwmeinfo(" WME", vp, 2+vp[1], maxcols);
3223173275Ssam			else if (iswmeparam(vp))
3224173275Ssam				printwmeparam(" WME", vp, 2+vp[1], maxcols);
3225139492Ssam			else if (isatherosoui(vp))
3226170531Ssam				printathie(" ATH", vp, 2+vp[1], maxcols);
3227181454Ssam			else if (iswpsoui(vp))
3228181454Ssam				printwpsie(" WPS", vp, 2+vp[1], maxcols);
3229186904Ssam			else if (istdmaoui(vp))
3230186904Ssam				printtdmaie(" TDMA", vp, 2+vp[1], maxcols);
3231173275Ssam			else if (verbose)
3232138593Ssam				printie(" VEN", vp, 2+vp[1], maxcols);
3233138593Ssam			break;
3234138593Ssam		case IEEE80211_ELEMID_RSN:
3235170531Ssam			printrsnie(" RSN", vp, 2+vp[1], maxcols);
3236138593Ssam			break;
3237173275Ssam		case IEEE80211_ELEMID_HTCAP:
3238173275Ssam			printhtcap(" HTCAP", vp, 2+vp[1], maxcols);
3239173275Ssam			break;
3240173275Ssam		case IEEE80211_ELEMID_HTINFO:
3241173275Ssam			if (verbose)
3242173275Ssam				printhtinfo(" HTINFO", vp, 2+vp[1], maxcols);
3243173275Ssam			break;
3244195618Srpaulo		case IEEE80211_ELEMID_MESHID:
3245195618Srpaulo			if (verbose)
3246195618Srpaulo				printssid(" MESHID", vp, 2+vp[1], maxcols);
3247195618Srpaulo			break;
3248195618Srpaulo		case IEEE80211_ELEMID_MESHCONF:
3249195618Srpaulo			printmeshconf(" MESHCONF", vp, 2+vp[1], maxcols);
3250195618Srpaulo			break;
3251297007Sadrian		case IEEE80211_ELEMID_VHT_CAP:
3252297007Sadrian			printvhtcap(" VHTCAP", vp, 2+vp[1], maxcols);
3253297007Sadrian			break;
3254297007Sadrian		case IEEE80211_ELEMID_VHT_OPMODE:
3255297007Sadrian			printvhtinfo(" VHTOPMODE", vp, 2+vp[1], maxcols);
3256297007Sadrian			break;
3257298252Sadrian		case IEEE80211_ELEMID_VHT_PWR_ENV:
3258298252Sadrian			printvhtpwrenv(" VHTPWRENV", vp, 2+vp[1], maxcols);
3259298252Sadrian			break;
3260297007Sadrian		case IEEE80211_ELEMID_BSSLOAD:
3261297007Sadrian			printbssload(" BSSLOAD", vp, 2+vp[1], maxcols);
3262297007Sadrian			break;
3263297010Sadrian		case IEEE80211_ELEMID_APCHANREP:
3264297010Sadrian			printapchanrep(" APCHANREP", vp, 2+vp[1], maxcols);
3265297010Sadrian			break;
3266138593Ssam		default:
3267173275Ssam			if (verbose)
3268173275Ssam				printie(iename(vp[0]), vp, 2+vp[1], maxcols);
3269138593Ssam			break;
3270138593Ssam		}
3271138593Ssam		ielen -= 2+vp[1];
3272138593Ssam		vp += 2+vp[1];
3273138593Ssam	}
3274138593Ssam}
3275138593Ssam
3276138593Ssamstatic void
3277178354Ssamprintmimo(const struct ieee80211_mimo_info *mi)
3278178354Ssam{
3279178354Ssam	/* NB: don't muddy display unless there's something to show */
3280220935Sadrian	if (mi->rssi[0] != 0 || mi->rssi[1] != 0 || mi->rssi[2] != 0) {
3281178354Ssam		/* XXX ignore EVM for now */
3282178354Ssam		printf(" (rssi %d:%d:%d nf %d:%d:%d)",
3283220935Sadrian		    mi->rssi[0], mi->rssi[1], mi->rssi[2],
3284220935Sadrian		    mi->noise[0], mi->noise[1], mi->noise[2]);
3285178354Ssam	}
3286178354Ssam}
3287178354Ssam
3288178354Ssamstatic void
3289138593Ssamlist_scan(int s)
3290138593Ssam{
3291138593Ssam	uint8_t buf[24*1024];
3292153892Srwatson	char ssid[IEEE80211_NWID_LEN+1];
3293173275Ssam	const uint8_t *cp;
3294195618Srpaulo	int len, ssidmax, idlen;
3295138593Ssam
3296173275Ssam	if (get80211len(s, IEEE80211_IOC_SCAN_RESULTS, buf, sizeof(buf), &len) < 0)
3297138593Ssam		errx(1, "unable to get scan results");
3298138593Ssam	if (len < sizeof(struct ieee80211req_scan_result))
3299138593Ssam		return;
3300138593Ssam
3301170531Ssam	getchaninfo(s);
3302170531Ssam
3303195618Srpaulo	ssidmax = verbose ? IEEE80211_NWID_LEN - 1 : 14;
3304290438Savos	printf("%-*.*s  %-17.17s  %4s %4s   %-7s  %3s %4s\n"
3305195618Srpaulo		, ssidmax, ssidmax, "SSID/MESH ID"
3306138593Ssam		, "BSSID"
3307138593Ssam		, "CHAN"
3308138593Ssam		, "RATE"
3309170531Ssam		, " S:N"
3310138593Ssam		, "INT"
3311138593Ssam		, "CAPS"
3312138593Ssam	);
3313138593Ssam	cp = buf;
3314138593Ssam	do {
3315170531Ssam		const struct ieee80211req_scan_result *sr;
3316195618Srpaulo		const uint8_t *vp, *idp;
3317138593Ssam
3318170531Ssam		sr = (const struct ieee80211req_scan_result *) cp;
3319173275Ssam		vp = cp + sr->isr_ie_off;
3320195618Srpaulo		if (sr->isr_meshid_len) {
3321195618Srpaulo			idp = vp + sr->isr_ssid_len;
3322195618Srpaulo			idlen = sr->isr_meshid_len;
3323195618Srpaulo		} else {
3324195618Srpaulo			idp = vp;
3325195618Srpaulo			idlen = sr->isr_ssid_len;
3326195618Srpaulo		}
3327290438Savos		printf("%-*.*s  %s  %3d  %3dM %4d:%-4d %4d %-4.4s"
3328154522Ssam			, ssidmax
3329195618Srpaulo			  , copy_essid(ssid, ssidmax, idp, idlen)
3330154522Ssam			  , ssid
3331138593Ssam			, ether_ntoa((const struct ether_addr *) sr->isr_bssid)
3332165570Ssam			, ieee80211_mhz2ieee(sr->isr_freq, sr->isr_flags)
3333138593Ssam			, getmaxrate(sr->isr_rates, sr->isr_nrates)
3334170531Ssam			, (sr->isr_rssi/2)+sr->isr_noise, sr->isr_noise
3335138593Ssam			, sr->isr_intval
3336138593Ssam			, getcaps(sr->isr_capinfo)
3337138593Ssam		);
3338195618Srpaulo		printies(vp + sr->isr_ssid_len + sr->isr_meshid_len,
3339195618Srpaulo		    sr->isr_ie_len, 24);
3340138593Ssam		printf("\n");
3341138593Ssam		cp += sr->isr_len, len -= sr->isr_len;
3342138593Ssam	} while (len >= sizeof(struct ieee80211req_scan_result));
3343138593Ssam}
3344138593Ssam
3345138593Ssamstatic void
3346138593Ssamscan_and_wait(int s)
3347138593Ssam{
3348178354Ssam	struct ieee80211_scan_req sr;
3349138593Ssam	struct ieee80211req ireq;
3350138593Ssam	int sroute;
3351138593Ssam
3352138593Ssam	sroute = socket(PF_ROUTE, SOCK_RAW, 0);
3353138593Ssam	if (sroute < 0) {
3354138593Ssam		perror("socket(PF_ROUTE,SOCK_RAW)");
3355138593Ssam		return;
3356138593Ssam	}
3357138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3358138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3359138593Ssam	ireq.i_type = IEEE80211_IOC_SCAN_REQ;
3360178354Ssam
3361178354Ssam	memset(&sr, 0, sizeof(sr));
3362178354Ssam	sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE
3363218090Sbschmidt		    | IEEE80211_IOC_SCAN_BGSCAN
3364178354Ssam		    | IEEE80211_IOC_SCAN_NOPICK
3365178354Ssam		    | IEEE80211_IOC_SCAN_ONCE;
3366178354Ssam	sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
3367178354Ssam	sr.sr_nssid = 0;
3368178354Ssam
3369178354Ssam	ireq.i_data = &sr;
3370178354Ssam	ireq.i_len = sizeof(sr);
3371218093Sbschmidt	/*
3372218093Sbschmidt	 * NB: only root can trigger a scan so ignore errors. Also ignore
3373218093Sbschmidt	 * possible errors from net80211, even if no new scan could be
3374218093Sbschmidt	 * started there might still be a valid scan cache.
3375218093Sbschmidt	 */
3376218093Sbschmidt	if (ioctl(s, SIOCS80211, &ireq) == 0) {
3377138593Ssam		char buf[2048];
3378138593Ssam		struct if_announcemsghdr *ifan;
3379138593Ssam		struct rt_msghdr *rtm;
3380138593Ssam
3381138593Ssam		do {
3382138593Ssam			if (read(sroute, buf, sizeof(buf)) < 0) {
3383138593Ssam				perror("read(PF_ROUTE)");
3384138593Ssam				break;
3385138593Ssam			}
3386138593Ssam			rtm = (struct rt_msghdr *) buf;
3387138593Ssam			if (rtm->rtm_version != RTM_VERSION)
3388138593Ssam				break;
3389138593Ssam			ifan = (struct if_announcemsghdr *) rtm;
3390138593Ssam		} while (rtm->rtm_type != RTM_IEEE80211 ||
3391138593Ssam		    ifan->ifan_what != RTM_IEEE80211_SCAN);
3392138593Ssam	}
3393138593Ssam	close(sroute);
3394138593Ssam}
3395138593Ssam
3396138593Ssamstatic
3397138593SsamDECL_CMD_FUNC(set80211scan, val, d)
3398138593Ssam{
3399138593Ssam	scan_and_wait(s);
3400138593Ssam	list_scan(s);
3401138593Ssam}
3402138593Ssam
3403161147Ssamstatic enum ieee80211_opmode get80211opmode(int s);
3404161147Ssam
3405173275Ssamstatic int
3406173275Ssamgettxseq(const struct ieee80211req_sta_info *si)
3407173275Ssam{
3408173275Ssam	int i, txseq;
3409173275Ssam
3410173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3411173275Ssam		return si->isi_txseqs[0];
3412173275Ssam	/* XXX not right but usually what folks want */
3413173275Ssam	txseq = 0;
3414173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3415173275Ssam		if (si->isi_txseqs[i] > txseq)
3416173275Ssam			txseq = si->isi_txseqs[i];
3417173275Ssam	return txseq;
3418173275Ssam}
3419173275Ssam
3420173275Ssamstatic int
3421173275Ssamgetrxseq(const struct ieee80211req_sta_info *si)
3422173275Ssam{
3423173275Ssam	int i, rxseq;
3424173275Ssam
3425173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3426173275Ssam		return si->isi_rxseqs[0];
3427173275Ssam	/* XXX not right but usually what folks want */
3428173275Ssam	rxseq = 0;
3429173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3430173275Ssam		if (si->isi_rxseqs[i] > rxseq)
3431173275Ssam			rxseq = si->isi_rxseqs[i];
3432173275Ssam	return rxseq;
3433173275Ssam}
3434173275Ssam
3435138593Ssamstatic void
3436138593Ssamlist_stations(int s)
3437138593Ssam{
3438161147Ssam	union {
3439161147Ssam		struct ieee80211req_sta_req req;
3440161147Ssam		uint8_t buf[24*1024];
3441161147Ssam	} u;
3442161147Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
3443170531Ssam	const uint8_t *cp;
3444138593Ssam	int len;
3445138593Ssam
3446161147Ssam	/* broadcast address =>'s get all stations */
3447161147Ssam	(void) memset(u.req.is_u.macaddr, 0xff, IEEE80211_ADDR_LEN);
3448161147Ssam	if (opmode == IEEE80211_M_STA) {
3449161147Ssam		/*
3450161147Ssam		 * Get information about the associated AP.
3451161147Ssam		 */
3452173275Ssam		(void) get80211(s, IEEE80211_IOC_BSSID,
3453173275Ssam		    u.req.is_u.macaddr, IEEE80211_ADDR_LEN);
3454161147Ssam	}
3455173275Ssam	if (get80211len(s, IEEE80211_IOC_STA_INFO, &u, sizeof(u), &len) < 0)
3456138593Ssam		errx(1, "unable to get station information");
3457138593Ssam	if (len < sizeof(struct ieee80211req_sta_info))
3458138593Ssam		return;
3459138593Ssam
3460170531Ssam	getchaninfo(s);
3461170531Ssam
3462195618Srpaulo	if (opmode == IEEE80211_M_MBSS)
3463195618Srpaulo		printf("%-17.17s %4s %5s %5s %7s %4s %4s %4s %6s %6s\n"
3464195618Srpaulo			, "ADDR"
3465195618Srpaulo			, "CHAN"
3466195618Srpaulo			, "LOCAL"
3467195618Srpaulo			, "PEER"
3468195618Srpaulo			, "STATE"
3469195618Srpaulo			, "RATE"
3470195618Srpaulo			, "RSSI"
3471195618Srpaulo			, "IDLE"
3472195618Srpaulo			, "TXSEQ"
3473195618Srpaulo			, "RXSEQ"
3474195618Srpaulo		);
3475195618Srpaulo	else
3476195618Srpaulo		printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %-7s\n"
3477195618Srpaulo			, "ADDR"
3478195618Srpaulo			, "AID"
3479195618Srpaulo			, "CHAN"
3480195618Srpaulo			, "RATE"
3481195618Srpaulo			, "RSSI"
3482195618Srpaulo			, "IDLE"
3483195618Srpaulo			, "TXSEQ"
3484195618Srpaulo			, "RXSEQ"
3485195618Srpaulo			, "CAPS"
3486195618Srpaulo			, "FLAG"
3487195618Srpaulo		);
3488170531Ssam	cp = (const uint8_t *) u.req.info;
3489138593Ssam	do {
3490170531Ssam		const struct ieee80211req_sta_info *si;
3491138593Ssam
3492170531Ssam		si = (const struct ieee80211req_sta_info *) cp;
3493161147Ssam		if (si->isi_len < sizeof(*si))
3494161147Ssam			break;
3495195618Srpaulo		if (opmode == IEEE80211_M_MBSS)
3496195618Srpaulo			printf("%s %4d %5x %5x %7.7s %3dM %4.1f %4d %6d %6d"
3497195618Srpaulo				, ether_ntoa((const struct ether_addr*)
3498195618Srpaulo				    si->isi_macaddr)
3499195618Srpaulo				, ieee80211_mhz2ieee(si->isi_freq,
3500195618Srpaulo				    si->isi_flags)
3501195618Srpaulo				, si->isi_localid
3502195618Srpaulo				, si->isi_peerid
3503195618Srpaulo				, mesh_linkstate_string(si->isi_peerstate)
3504195618Srpaulo				, si->isi_txmbps/2
3505195618Srpaulo				, si->isi_rssi/2.
3506195618Srpaulo				, si->isi_inact
3507195618Srpaulo				, gettxseq(si)
3508195618Srpaulo				, getrxseq(si)
3509195618Srpaulo			);
3510195618Srpaulo		else
3511195618Srpaulo			printf("%s %4u %4d %3dM %4.1f %4d %6d %6d %-4.4s %-7.7s"
3512195618Srpaulo				, ether_ntoa((const struct ether_addr*)
3513195618Srpaulo				    si->isi_macaddr)
3514195618Srpaulo				, IEEE80211_AID(si->isi_associd)
3515195618Srpaulo				, ieee80211_mhz2ieee(si->isi_freq,
3516195618Srpaulo				    si->isi_flags)
3517195618Srpaulo				, si->isi_txmbps/2
3518195618Srpaulo				, si->isi_rssi/2.
3519195618Srpaulo				, si->isi_inact
3520195618Srpaulo				, gettxseq(si)
3521195618Srpaulo				, getrxseq(si)
3522195618Srpaulo				, getcaps(si->isi_capinfo)
3523195618Srpaulo				, getflags(si->isi_state)
3524195618Srpaulo			);
3525170531Ssam		printies(cp + si->isi_ie_off, si->isi_ie_len, 24);
3526178354Ssam		printmimo(&si->isi_mimo);
3527138593Ssam		printf("\n");
3528138593Ssam		cp += si->isi_len, len -= si->isi_len;
3529138593Ssam	} while (len >= sizeof(struct ieee80211req_sta_info));
3530138593Ssam}
3531138593Ssam
3532170531Ssamstatic const char *
3533195618Srpaulomesh_linkstate_string(uint8_t state)
3534195618Srpaulo{
3535195618Srpaulo	static const char *state_names[] = {
3536195618Srpaulo	    [0] = "IDLE",
3537195618Srpaulo	    [1] = "OPEN-TX",
3538195618Srpaulo	    [2] = "OPEN-RX",
3539195618Srpaulo	    [3] = "CONF-RX",
3540195618Srpaulo	    [4] = "ESTAB",
3541195618Srpaulo	    [5] = "HOLDING",
3542195618Srpaulo	};
3543195618Srpaulo
3544288305Sngie	if (state >= nitems(state_names)) {
3545195618Srpaulo		static char buf[10];
3546195618Srpaulo		snprintf(buf, sizeof(buf), "#%u", state);
3547195618Srpaulo		return buf;
3548195618Srpaulo	} else
3549195618Srpaulo		return state_names[state];
3550195618Srpaulo}
3551195618Srpaulo
3552195618Srpaulostatic const char *
3553170531Ssamget_chaninfo(const struct ieee80211_channel *c, int precise,
3554170531Ssam	char buf[], size_t bsize)
3555138593Ssam{
3556138593Ssam	buf[0] = '\0';
3557138593Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
3558170531Ssam		strlcat(buf, " FHSS", bsize);
3559187843Ssam	if (IEEE80211_IS_CHAN_A(c))
3560187843Ssam		strlcat(buf, " 11a", bsize);
3561187843Ssam	else if (IEEE80211_IS_CHAN_ANYG(c))
3562187843Ssam		strlcat(buf, " 11g", bsize);
3563187843Ssam	else if (IEEE80211_IS_CHAN_B(c))
3564170531Ssam		strlcat(buf, " 11b", bsize);
3565187843Ssam	if (IEEE80211_IS_CHAN_HALF(c))
3566202161Sgavin		strlcat(buf, "/10MHz", bsize);
3567187843Ssam	if (IEEE80211_IS_CHAN_QUARTER(c))
3568202161Sgavin		strlcat(buf, "/5MHz", bsize);
3569170531Ssam	if (IEEE80211_IS_CHAN_TURBO(c))
3570170531Ssam		strlcat(buf, " Turbo", bsize);
3571170531Ssam	if (precise) {
3572170531Ssam		if (IEEE80211_IS_CHAN_HT20(c))
3573170531Ssam			strlcat(buf, " ht/20", bsize);
3574170531Ssam		else if (IEEE80211_IS_CHAN_HT40D(c))
3575170531Ssam			strlcat(buf, " ht/40-", bsize);
3576170531Ssam		else if (IEEE80211_IS_CHAN_HT40U(c))
3577170531Ssam			strlcat(buf, " ht/40+", bsize);
3578170531Ssam	} else {
3579170531Ssam		if (IEEE80211_IS_CHAN_HT(c))
3580170531Ssam			strlcat(buf, " ht", bsize);
3581170531Ssam	}
3582170531Ssam	return buf;
3583170531Ssam}
3584170531Ssam
3585170531Ssamstatic void
3586173275Ssamprint_chaninfo(const struct ieee80211_channel *c, int verb)
3587170531Ssam{
3588170531Ssam	char buf[14];
3589170531Ssam
3590224219Sadrian	if (verb)
3591224219Sadrian		printf("Channel %3u : %u%c%c%c%c%c MHz%-14.14s",
3592224219Sadrian		    ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
3593224219Sadrian		    IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
3594224219Sadrian		    IEEE80211_IS_CHAN_DFS(c) ? 'D' : ' ',
3595224219Sadrian		    IEEE80211_IS_CHAN_RADAR(c) ? 'R' : ' ',
3596224219Sadrian		    IEEE80211_IS_CHAN_CWINT(c) ? 'I' : ' ',
3597224219Sadrian		    IEEE80211_IS_CHAN_CACDONE(c) ? 'C' : ' ',
3598224219Sadrian		    get_chaninfo(c, verb, buf, sizeof(buf)));
3599224219Sadrian	else
3600202161Sgavin	printf("Channel %3u : %u%c MHz%-14.14s",
3601224219Sadrian	    ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
3602224219Sadrian	    IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
3603224219Sadrian	    get_chaninfo(c, verb, buf, sizeof(buf)));
3604224219Sadrian
3605138593Ssam}
3606138593Ssam
3607187844Ssamstatic int
3608187844Ssamchanpref(const struct ieee80211_channel *c)
3609187844Ssam{
3610187844Ssam	if (IEEE80211_IS_CHAN_HT40(c))
3611187844Ssam		return 40;
3612187844Ssam	if (IEEE80211_IS_CHAN_HT20(c))
3613187844Ssam		return 30;
3614187844Ssam	if (IEEE80211_IS_CHAN_HALF(c))
3615187844Ssam		return 10;
3616187844Ssam	if (IEEE80211_IS_CHAN_QUARTER(c))
3617187844Ssam		return 5;
3618187844Ssam	if (IEEE80211_IS_CHAN_TURBO(c))
3619187844Ssam		return 25;
3620187844Ssam	if (IEEE80211_IS_CHAN_A(c))
3621187844Ssam		return 20;
3622187844Ssam	if (IEEE80211_IS_CHAN_G(c))
3623187844Ssam		return 20;
3624187844Ssam	if (IEEE80211_IS_CHAN_B(c))
3625187844Ssam		return 15;
3626187844Ssam	if (IEEE80211_IS_CHAN_PUREG(c))
3627187844Ssam		return 15;
3628187844Ssam	return 0;
3629187844Ssam}
3630187844Ssam
3631138593Ssamstatic void
3632173275Ssamprint_channels(int s, const struct ieee80211req_chaninfo *chans,
3633173275Ssam	int allchans, int verb)
3634138593Ssam{
3635187801Ssam	struct ieee80211req_chaninfo *achans;
3636170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3637138593Ssam	const struct ieee80211_channel *c;
3638170531Ssam	int i, half;
3639138593Ssam
3640187801Ssam	achans = malloc(IEEE80211_CHANINFO_SPACE(chans));
3641187801Ssam	if (achans == NULL)
3642187801Ssam		errx(1, "no space for active channel list");
3643187801Ssam	achans->ic_nchans = 0;
3644170531Ssam	memset(reported, 0, sizeof(reported));
3645138593Ssam	if (!allchans) {
3646138593Ssam		struct ieee80211req_chanlist active;
3647138593Ssam
3648173275Ssam		if (get80211(s, IEEE80211_IOC_CHANLIST, &active, sizeof(active)) < 0)
3649138593Ssam			errx(1, "unable to get active channel list");
3650173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3651173275Ssam			c = &chans->ic_chans[i];
3652170531Ssam			if (!isset(active.ic_channels, c->ic_ieee))
3653170531Ssam				continue;
3654170531Ssam			/*
3655170531Ssam			 * Suppress compatible duplicates unless
3656170531Ssam			 * verbose.  The kernel gives us it's
3657170531Ssam			 * complete channel list which has separate
3658170531Ssam			 * entries for 11g/11b and 11a/turbo.
3659170531Ssam			 */
3660173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3661170531Ssam				/* XXX we assume duplicates are adjacent */
3662187801Ssam				achans->ic_chans[achans->ic_nchans-1] = *c;
3663170531Ssam			} else {
3664187801Ssam				achans->ic_chans[achans->ic_nchans++] = *c;
3665170531Ssam				setbit(reported, c->ic_ieee);
3666170531Ssam			}
3667138593Ssam		}
3668170531Ssam	} else {
3669173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3670173275Ssam			c = &chans->ic_chans[i];
3671170531Ssam			/* suppress duplicates as above */
3672173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3673170531Ssam				/* XXX we assume duplicates are adjacent */
3674187844Ssam				struct ieee80211_channel *a =
3675187844Ssam				    &achans->ic_chans[achans->ic_nchans-1];
3676187844Ssam				if (chanpref(c) > chanpref(a))
3677187844Ssam					*a = *c;
3678170531Ssam			} else {
3679187801Ssam				achans->ic_chans[achans->ic_nchans++] = *c;
3680170531Ssam				setbit(reported, c->ic_ieee);
3681170531Ssam			}
3682170531Ssam		}
3683170531Ssam	}
3684187801Ssam	half = achans->ic_nchans / 2;
3685187801Ssam	if (achans->ic_nchans % 2)
3686138593Ssam		half++;
3687170531Ssam
3688187801Ssam	for (i = 0; i < achans->ic_nchans / 2; i++) {
3689187801Ssam		print_chaninfo(&achans->ic_chans[i], verb);
3690187801Ssam		print_chaninfo(&achans->ic_chans[half+i], verb);
3691138593Ssam		printf("\n");
3692138593Ssam	}
3693187801Ssam	if (achans->ic_nchans % 2) {
3694187801Ssam		print_chaninfo(&achans->ic_chans[i], verb);
3695138593Ssam		printf("\n");
3696138593Ssam	}
3697187801Ssam	free(achans);
3698138593Ssam}
3699138593Ssam
3700138593Ssamstatic void
3701173275Ssamlist_channels(int s, int allchans)
3702173275Ssam{
3703173275Ssam	getchaninfo(s);
3704187801Ssam	print_channels(s, chaninfo, allchans, verbose);
3705173275Ssam}
3706173275Ssam
3707173275Ssamstatic void
3708170531Ssamprint_txpow(const struct ieee80211_channel *c)
3709170531Ssam{
3710202161Sgavin	printf("Channel %3u : %u MHz %3.1f reg %2d  ",
3711170531Ssam	    c->ic_ieee, c->ic_freq,
3712170531Ssam	    c->ic_maxpower/2., c->ic_maxregpower);
3713170531Ssam}
3714170531Ssam
3715170531Ssamstatic void
3716170531Ssamprint_txpow_verbose(const struct ieee80211_channel *c)
3717170531Ssam{
3718173275Ssam	print_chaninfo(c, 1);
3719170531Ssam	printf("min %4.1f dBm  max %3.1f dBm  reg %2d dBm",
3720170531Ssam	    c->ic_minpower/2., c->ic_maxpower/2., c->ic_maxregpower);
3721170531Ssam	/* indicate where regulatory cap limits power use */
3722170531Ssam	if (c->ic_maxpower > 2*c->ic_maxregpower)
3723170531Ssam		printf(" <");
3724170531Ssam}
3725170531Ssam
3726170531Ssamstatic void
3727170531Ssamlist_txpow(int s)
3728170531Ssam{
3729187801Ssam	struct ieee80211req_chaninfo *achans;
3730170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3731170531Ssam	struct ieee80211_channel *c, *prev;
3732170531Ssam	int i, half;
3733170531Ssam
3734170531Ssam	getchaninfo(s);
3735187801Ssam	achans = malloc(IEEE80211_CHANINFO_SPACE(chaninfo));
3736187801Ssam	if (achans == NULL)
3737187801Ssam		errx(1, "no space for active channel list");
3738187801Ssam	achans->ic_nchans = 0;
3739170531Ssam	memset(reported, 0, sizeof(reported));
3740187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
3741187801Ssam		c = &chaninfo->ic_chans[i];
3742170531Ssam		/* suppress duplicates as above */
3743170531Ssam		if (isset(reported, c->ic_ieee) && !verbose) {
3744170531Ssam			/* XXX we assume duplicates are adjacent */
3745187801Ssam			prev = &achans->ic_chans[achans->ic_nchans-1];
3746170531Ssam			/* display highest power on channel */
3747170531Ssam			if (c->ic_maxpower > prev->ic_maxpower)
3748170531Ssam				*prev = *c;
3749170531Ssam		} else {
3750187801Ssam			achans->ic_chans[achans->ic_nchans++] = *c;
3751170531Ssam			setbit(reported, c->ic_ieee);
3752170531Ssam		}
3753170531Ssam	}
3754170531Ssam	if (!verbose) {
3755187801Ssam		half = achans->ic_nchans / 2;
3756187801Ssam		if (achans->ic_nchans % 2)
3757170531Ssam			half++;
3758170531Ssam
3759187801Ssam		for (i = 0; i < achans->ic_nchans / 2; i++) {
3760187801Ssam			print_txpow(&achans->ic_chans[i]);
3761187801Ssam			print_txpow(&achans->ic_chans[half+i]);
3762170531Ssam			printf("\n");
3763170531Ssam		}
3764187801Ssam		if (achans->ic_nchans % 2) {
3765187801Ssam			print_txpow(&achans->ic_chans[i]);
3766170531Ssam			printf("\n");
3767170531Ssam		}
3768170531Ssam	} else {
3769187801Ssam		for (i = 0; i < achans->ic_nchans; i++) {
3770187801Ssam			print_txpow_verbose(&achans->ic_chans[i]);
3771170531Ssam			printf("\n");
3772170531Ssam		}
3773170531Ssam	}
3774187801Ssam	free(achans);
3775170531Ssam}
3776170531Ssam
3777170531Ssamstatic void
3778138593Ssamlist_keys(int s)
3779138593Ssam{
3780138593Ssam}
3781138593Ssam
3782138593Ssam#define	IEEE80211_C_BITS \
3783195618Srpaulo	"\20\1STA\002803ENCAP\7FF\10TURBOP\11IBSS\12PMGT" \
3784178354Ssam	"\13HOSTAP\14AHDEMO\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE" \
3785195618Srpaulo	"\21MONITOR\22DFS\23MBSS\30WPA1\31WPA2\32BURST\33WME\34WDS\36BGSCAN" \
3786186904Ssam	"\37TXFRAG\40TDMA"
3787138593Ssam
3788138593Ssamstatic void
3789138593Ssamlist_capabilities(int s)
3790138593Ssam{
3791187801Ssam	struct ieee80211_devcaps_req *dc;
3792138593Ssam
3793187845Ssam	if (verbose)
3794187845Ssam		dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
3795187845Ssam	else
3796187845Ssam		dc = malloc(IEEE80211_DEVCAPS_SIZE(1));
3797187801Ssam	if (dc == NULL)
3798187801Ssam		errx(1, "no space for device capabilities");
3799187845Ssam	dc->dc_chaninfo.ic_nchans = verbose ? MAXCHAN : 1;
3800187801Ssam	getdevcaps(s, dc);
3801187801Ssam	printb("drivercaps", dc->dc_drivercaps, IEEE80211_C_BITS);
3802187801Ssam	if (dc->dc_cryptocaps != 0 || verbose) {
3803178354Ssam		putchar('\n');
3804187801Ssam		printb("cryptocaps", dc->dc_cryptocaps, IEEE80211_CRYPTO_BITS);
3805178354Ssam	}
3806187801Ssam	if (dc->dc_htcaps != 0 || verbose) {
3807178354Ssam		putchar('\n');
3808187801Ssam		printb("htcaps", dc->dc_htcaps, IEEE80211_HTCAP_BITS);
3809178354Ssam	}
3810138593Ssam	putchar('\n');
3811187845Ssam	if (verbose) {
3812187845Ssam		chaninfo = &dc->dc_chaninfo;	/* XXX */
3813187845Ssam		print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, verbose);
3814187845Ssam	}
3815187801Ssam	free(dc);
3816138593Ssam}
3817138593Ssam
3818173275Ssamstatic int
3819173275Ssamget80211wme(int s, int param, int ac, int *val)
3820173275Ssam{
3821173275Ssam	struct ieee80211req ireq;
3822173275Ssam
3823173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3824173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3825173275Ssam	ireq.i_type = param;
3826173275Ssam	ireq.i_len = ac;
3827173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3828173275Ssam		warn("cannot get WME parameter %d, ac %d%s",
3829173275Ssam		    param, ac & IEEE80211_WMEPARAM_VAL,
3830173275Ssam		    ac & IEEE80211_WMEPARAM_BSS ? " (BSS)" : "");
3831173275Ssam		return -1;
3832173275Ssam	}
3833173275Ssam	*val = ireq.i_val;
3834173275Ssam	return 0;
3835173275Ssam}
3836173275Ssam
3837138593Ssamstatic void
3838181199Ssamlist_wme_aci(int s, const char *tag, int ac)
3839138593Ssam{
3840181199Ssam	int val;
3841138593Ssam
3842181199Ssam	printf("\t%s", tag);
3843138593Ssam
3844181199Ssam	/* show WME BSS parameters */
3845181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMIN, ac, &val) != -1)
3846181199Ssam		printf(" cwmin %2u", val);
3847181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMAX, ac, &val) != -1)
3848181199Ssam		printf(" cwmax %2u", val);
3849181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_AIFS, ac, &val) != -1)
3850181199Ssam		printf(" aifs %2u", val);
3851181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_TXOPLIMIT, ac, &val) != -1)
3852181199Ssam		printf(" txopLimit %3u", val);
3853181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_ACM, ac, &val) != -1) {
3854181199Ssam		if (val)
3855181199Ssam			printf(" acm");
3856181199Ssam		else if (verbose)
3857181199Ssam			printf(" -acm");
3858181199Ssam	}
3859181199Ssam	/* !BSS only */
3860181199Ssam	if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3861181199Ssam		if (get80211wme(s, IEEE80211_IOC_WME_ACKPOLICY, ac, &val) != -1) {
3862181199Ssam			if (!val)
3863181199Ssam				printf(" -ack");
3864138593Ssam			else if (verbose)
3865181199Ssam				printf(" ack");
3866138593Ssam		}
3867181199Ssam	}
3868181199Ssam	printf("\n");
3869181199Ssam}
3870181199Ssam
3871181199Ssamstatic void
3872181199Ssamlist_wme(int s)
3873181199Ssam{
3874181199Ssam	static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
3875181199Ssam	int ac;
3876181199Ssam
3877181199Ssam	if (verbose) {
3878181199Ssam		/* display both BSS and local settings */
3879181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
3880181199Ssam	again:
3881181199Ssam			if (ac & IEEE80211_WMEPARAM_BSS)
3882181199Ssam				list_wme_aci(s, "     ", ac);
3883181199Ssam			else
3884181199Ssam				list_wme_aci(s, acnames[ac], ac);
3885181199Ssam			if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3886181199Ssam				ac |= IEEE80211_WMEPARAM_BSS;
3887181199Ssam				goto again;
3888181199Ssam			} else
3889181199Ssam				ac &= ~IEEE80211_WMEPARAM_BSS;
3890138593Ssam		}
3891181199Ssam	} else {
3892181199Ssam		/* display only channel settings */
3893181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++)
3894181199Ssam			list_wme_aci(s, acnames[ac], ac);
3895138593Ssam	}
3896138593Ssam}
3897138593Ssam
3898149029Ssamstatic void
3899178354Ssamlist_roam(int s)
3900178354Ssam{
3901178354Ssam	const struct ieee80211_roamparam *rp;
3902178354Ssam	int mode;
3903178354Ssam
3904178354Ssam	getroam(s);
3905188784Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
3906178354Ssam		rp = &roamparams.params[mode];
3907178354Ssam		if (rp->rssi == 0 && rp->rate == 0)
3908178354Ssam			continue;
3909188784Ssam		if (mode == IEEE80211_MODE_11NA || mode == IEEE80211_MODE_11NG) {
3910188784Ssam			if (rp->rssi & 1)
3911188784Ssam				LINE_CHECK("roam:%-7.7s rssi %2u.5dBm  MCS %2u    ",
3912188784Ssam				    modename[mode], rp->rssi/2,
3913188784Ssam				    rp->rate &~ IEEE80211_RATE_MCS);
3914188784Ssam			else
3915188784Ssam				LINE_CHECK("roam:%-7.7s rssi %4udBm  MCS %2u    ",
3916188784Ssam				    modename[mode], rp->rssi/2,
3917188784Ssam				    rp->rate &~ IEEE80211_RATE_MCS);
3918188784Ssam		} else {
3919188784Ssam			if (rp->rssi & 1)
3920188784Ssam				LINE_CHECK("roam:%-7.7s rssi %2u.5dBm rate %2u Mb/s",
3921188784Ssam				    modename[mode], rp->rssi/2, rp->rate/2);
3922188784Ssam			else
3923188784Ssam				LINE_CHECK("roam:%-7.7s rssi %4udBm rate %2u Mb/s",
3924188784Ssam				    modename[mode], rp->rssi/2, rp->rate/2);
3925188784Ssam		}
3926178354Ssam	}
3927178354Ssam}
3928178354Ssam
3929178354Ssamstatic void
3930178354Ssamlist_txparams(int s)
3931178354Ssam{
3932178354Ssam	const struct ieee80211_txparam *tp;
3933178354Ssam	int mode;
3934178354Ssam
3935178354Ssam	gettxparams(s);
3936188784Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
3937178354Ssam		tp = &txparams.params[mode];
3938178354Ssam		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
3939178354Ssam			continue;
3940188784Ssam		if (mode == IEEE80211_MODE_11NA || mode == IEEE80211_MODE_11NG) {
3941188784Ssam			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3942188784Ssam				LINE_CHECK("%-7.7s ucast NONE    mgmt %2u MCS  "
3943188784Ssam				    "mcast %2u MCS  maxretry %u",
3944188784Ssam				    modename[mode],
3945188784Ssam				    tp->mgmtrate &~ IEEE80211_RATE_MCS,
3946188784Ssam				    tp->mcastrate &~ IEEE80211_RATE_MCS,
3947188784Ssam				    tp->maxretry);
3948188784Ssam			else
3949188784Ssam				LINE_CHECK("%-7.7s ucast %2u MCS  mgmt %2u MCS  "
3950188784Ssam				    "mcast %2u MCS  maxretry %u",
3951188784Ssam				    modename[mode],
3952188784Ssam				    tp->ucastrate &~ IEEE80211_RATE_MCS,
3953188784Ssam				    tp->mgmtrate &~ IEEE80211_RATE_MCS,
3954188784Ssam				    tp->mcastrate &~ IEEE80211_RATE_MCS,
3955188784Ssam				    tp->maxretry);
3956188784Ssam		} else {
3957188784Ssam			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3958188784Ssam				LINE_CHECK("%-7.7s ucast NONE    mgmt %2u Mb/s "
3959188784Ssam				    "mcast %2u Mb/s maxretry %u",
3960188784Ssam				    modename[mode],
3961188784Ssam				    tp->mgmtrate/2,
3962188784Ssam				    tp->mcastrate/2, tp->maxretry);
3963188784Ssam			else
3964188784Ssam				LINE_CHECK("%-7.7s ucast %2u Mb/s mgmt %2u Mb/s "
3965188784Ssam				    "mcast %2u Mb/s maxretry %u",
3966188784Ssam				    modename[mode],
3967188784Ssam				    tp->ucastrate/2, tp->mgmtrate/2,
3968188784Ssam				    tp->mcastrate/2, tp->maxretry);
3969188784Ssam		}
3970178354Ssam	}
3971178354Ssam}
3972178354Ssam
3973178354Ssamstatic void
3974173275Ssamprintpolicy(int policy)
3975173275Ssam{
3976173275Ssam	switch (policy) {
3977173275Ssam	case IEEE80211_MACCMD_POLICY_OPEN:
3978173275Ssam		printf("policy: open\n");
3979173275Ssam		break;
3980173275Ssam	case IEEE80211_MACCMD_POLICY_ALLOW:
3981173275Ssam		printf("policy: allow\n");
3982173275Ssam		break;
3983173275Ssam	case IEEE80211_MACCMD_POLICY_DENY:
3984173275Ssam		printf("policy: deny\n");
3985173275Ssam		break;
3986178354Ssam	case IEEE80211_MACCMD_POLICY_RADIUS:
3987178354Ssam		printf("policy: radius\n");
3988178354Ssam		break;
3989173275Ssam	default:
3990173275Ssam		printf("policy: unknown (%u)\n", policy);
3991173275Ssam		break;
3992173275Ssam	}
3993173275Ssam}
3994173275Ssam
3995173275Ssamstatic void
3996149029Ssamlist_mac(int s)
3997149029Ssam{
3998149029Ssam	struct ieee80211req ireq;
3999149029Ssam	struct ieee80211req_maclist *acllist;
4000173275Ssam	int i, nacls, policy, len;
4001173275Ssam	uint8_t *data;
4002149029Ssam	char c;
4003149029Ssam
4004149029Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4005149029Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
4006149029Ssam	ireq.i_type = IEEE80211_IOC_MACCMD;
4007149029Ssam	ireq.i_val = IEEE80211_MACCMD_POLICY;
4008149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
4009149029Ssam		if (errno == EINVAL) {
4010149029Ssam			printf("No acl policy loaded\n");
4011149029Ssam			return;
4012149029Ssam		}
4013149029Ssam		err(1, "unable to get mac policy");
4014149029Ssam	}
4015149029Ssam	policy = ireq.i_val;
4016149029Ssam	if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
4017149029Ssam		c = '*';
4018149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
4019149029Ssam		c = '+';
4020149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
4021149029Ssam		c = '-';
4022178354Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_RADIUS) {
4023178354Ssam		c = 'r';		/* NB: should never have entries */
4024149029Ssam	} else {
4025149029Ssam		printf("policy: unknown (%u)\n", policy);
4026149029Ssam		c = '?';
4027149029Ssam	}
4028173275Ssam	if (verbose || c == '?')
4029173275Ssam		printpolicy(policy);
4030173275Ssam
4031175952Ssam	ireq.i_val = IEEE80211_MACCMD_LIST;
4032175952Ssam	ireq.i_len = 0;
4033175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4034173275Ssam		err(1, "unable to get mac acl list size");
4035175952Ssam	if (ireq.i_len == 0) {		/* NB: no acls */
4036173275Ssam		if (!(verbose || c == '?'))
4037173275Ssam			printpolicy(policy);
4038173275Ssam		return;
4039173275Ssam	}
4040175952Ssam	len = ireq.i_len;
4041173275Ssam
4042173275Ssam	data = malloc(len);
4043173275Ssam	if (data == NULL)
4044173275Ssam		err(1, "out of memory for acl list");
4045173275Ssam
4046175952Ssam	ireq.i_data = data;
4047175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4048173275Ssam		err(1, "unable to get mac acl list");
4049173275Ssam	nacls = len / sizeof(*acllist);
4050173275Ssam	acllist = (struct ieee80211req_maclist *) data;
4051149029Ssam	for (i = 0; i < nacls; i++)
4052149029Ssam		printf("%c%s\n", c, ether_ntoa(
4053149029Ssam			(const struct ether_addr *) acllist[i].ml_macaddr));
4054173275Ssam	free(data);
4055149029Ssam}
4056149029Ssam
4057178354Ssamstatic void
4058178354Ssamprint_regdomain(const struct ieee80211_regdomain *reg, int verb)
4059178354Ssam{
4060178354Ssam	if ((reg->regdomain != 0 &&
4061178354Ssam	    reg->regdomain != reg->country) || verb) {
4062178354Ssam		const struct regdomain *rd =
4063178354Ssam		    lib80211_regdomain_findbysku(getregdata(), reg->regdomain);
4064178354Ssam		if (rd == NULL)
4065178354Ssam			LINE_CHECK("regdomain %d", reg->regdomain);
4066178354Ssam		else
4067178354Ssam			LINE_CHECK("regdomain %s", rd->name);
4068178354Ssam	}
4069178354Ssam	if (reg->country != 0 || verb) {
4070178354Ssam		const struct country *cc =
4071178354Ssam		    lib80211_country_findbycc(getregdata(), reg->country);
4072178354Ssam		if (cc == NULL)
4073178354Ssam			LINE_CHECK("country %d", reg->country);
4074178354Ssam		else
4075178354Ssam			LINE_CHECK("country %s", cc->isoname);
4076178354Ssam	}
4077178354Ssam	if (reg->location == 'I')
4078178354Ssam		LINE_CHECK("indoor");
4079178354Ssam	else if (reg->location == 'O')
4080178354Ssam		LINE_CHECK("outdoor");
4081178354Ssam	else if (verb)
4082178354Ssam		LINE_CHECK("anywhere");
4083178354Ssam	if (reg->ecm)
4084178354Ssam		LINE_CHECK("ecm");
4085178354Ssam	else if (verb)
4086178354Ssam		LINE_CHECK("-ecm");
4087178354Ssam}
4088178354Ssam
4089178354Ssamstatic void
4090178354Ssamlist_regdomain(int s, int channelsalso)
4091178354Ssam{
4092178354Ssam	getregdomain(s);
4093178354Ssam	if (channelsalso) {
4094178354Ssam		getchaninfo(s);
4095178354Ssam		spacer = ':';
4096178354Ssam		print_regdomain(&regdomain, 1);
4097178354Ssam		LINE_BREAK();
4098187801Ssam		print_channels(s, chaninfo, 1/*allchans*/, 1/*verbose*/);
4099178354Ssam	} else
4100178354Ssam		print_regdomain(&regdomain, verbose);
4101178354Ssam}
4102178354Ssam
4103195618Srpaulostatic void
4104195618Srpaulolist_mesh(int s)
4105195618Srpaulo{
4106195618Srpaulo	struct ieee80211req ireq;
4107195618Srpaulo	struct ieee80211req_mesh_route routes[128];
4108195908Srpaulo	struct ieee80211req_mesh_route *rt;
4109195618Srpaulo
4110195618Srpaulo	(void) memset(&ireq, 0, sizeof(ireq));
4111195618Srpaulo	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4112195618Srpaulo	ireq.i_type = IEEE80211_IOC_MESH_RTCMD;
4113195618Srpaulo	ireq.i_val = IEEE80211_MESH_RTCMD_LIST;
4114195618Srpaulo	ireq.i_data = &routes;
4115195618Srpaulo	ireq.i_len = sizeof(routes);
4116195618Srpaulo	if (ioctl(s, SIOCG80211, &ireq) < 0)
4117195618Srpaulo	 	err(1, "unable to get the Mesh routing table");
4118195618Srpaulo
4119195908Srpaulo	printf("%-17.17s %-17.17s %4s %4s %4s %6s %s\n"
4120195618Srpaulo		, "DEST"
4121195618Srpaulo		, "NEXT HOP"
4122195618Srpaulo		, "HOPS"
4123195618Srpaulo		, "METRIC"
4124195784Srpaulo		, "LIFETIME"
4125195908Srpaulo		, "MSEQ"
4126195908Srpaulo		, "FLAGS");
4127195618Srpaulo
4128195908Srpaulo	for (rt = &routes[0]; rt - &routes[0] < ireq.i_len / sizeof(*rt); rt++){
4129195618Srpaulo		printf("%s ",
4130195908Srpaulo		    ether_ntoa((const struct ether_addr *)rt->imr_dest));
4131195908Srpaulo		printf("%s %4u   %4u   %6u %6u    %c%c\n",
4132195908Srpaulo			ether_ntoa((const struct ether_addr *)rt->imr_nexthop),
4133195908Srpaulo			rt->imr_nhops, rt->imr_metric, rt->imr_lifetime,
4134195908Srpaulo			rt->imr_lastmseq,
4135234895Smonthadar			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_DISCOVER) ?
4136234895Smonthadar			    'D' :
4137195908Srpaulo			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_VALID) ?
4138195908Srpaulo			    'V' : '!',
4139195908Srpaulo			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_PROXY) ?
4140246505Smonthadar			    'P' :
4141246505Smonthadar			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_GATE) ?
4142246505Smonthadar			    'G' :' ');
4143195618Srpaulo	}
4144195618Srpaulo}
4145195618Srpaulo
4146138593Ssamstatic
4147138593SsamDECL_CMD_FUNC(set80211list, arg, d)
4148138593Ssam{
4149138593Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
4150138593Ssam
4151173275Ssam	LINE_INIT('\t');
4152173275Ssam
4153138593Ssam	if (iseq(arg, "sta"))
4154138593Ssam		list_stations(s);
4155138593Ssam	else if (iseq(arg, "scan") || iseq(arg, "ap"))
4156138593Ssam		list_scan(s);
4157138593Ssam	else if (iseq(arg, "chan") || iseq(arg, "freq"))
4158138593Ssam		list_channels(s, 1);
4159138593Ssam	else if (iseq(arg, "active"))
4160138593Ssam		list_channels(s, 0);
4161138593Ssam	else if (iseq(arg, "keys"))
4162138593Ssam		list_keys(s);
4163138593Ssam	else if (iseq(arg, "caps"))
4164138593Ssam		list_capabilities(s);
4165178354Ssam	else if (iseq(arg, "wme") || iseq(arg, "wmm"))
4166138593Ssam		list_wme(s);
4167149029Ssam	else if (iseq(arg, "mac"))
4168149029Ssam		list_mac(s);
4169170531Ssam	else if (iseq(arg, "txpow"))
4170170531Ssam		list_txpow(s);
4171178354Ssam	else if (iseq(arg, "roam"))
4172178354Ssam		list_roam(s);
4173178354Ssam	else if (iseq(arg, "txparam") || iseq(arg, "txparm"))
4174178354Ssam		list_txparams(s);
4175178354Ssam	else if (iseq(arg, "regdomain"))
4176178354Ssam		list_regdomain(s, 1);
4177178354Ssam	else if (iseq(arg, "countries"))
4178178354Ssam		list_countries();
4179195618Srpaulo	else if (iseq(arg, "mesh"))
4180195618Srpaulo		list_mesh(s);
4181138593Ssam	else
4182138593Ssam		errx(1, "Don't know how to list %s for %s", arg, name);
4183178354Ssam	LINE_BREAK();
4184138593Ssam#undef iseq
4185138593Ssam}
4186138593Ssam
4187138593Ssamstatic enum ieee80211_opmode
4188138593Ssamget80211opmode(int s)
4189138593Ssam{
4190138593Ssam	struct ifmediareq ifmr;
4191138593Ssam
4192138593Ssam	(void) memset(&ifmr, 0, sizeof(ifmr));
4193138593Ssam	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
4194138593Ssam
4195138593Ssam	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
4196186101Ssam		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) {
4197186101Ssam			if (ifmr.ifm_current & IFM_FLAG0)
4198186101Ssam				return IEEE80211_M_AHDEMO;
4199186101Ssam			else
4200186101Ssam				return IEEE80211_M_IBSS;
4201186101Ssam		}
4202138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
4203138593Ssam			return IEEE80211_M_HOSTAP;
4204291352Sadrian		if (ifmr.ifm_current & IFM_IEEE80211_IBSS)
4205291352Sadrian			return IEEE80211_M_IBSS;
4206138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
4207138593Ssam			return IEEE80211_M_MONITOR;
4208195618Srpaulo		if (ifmr.ifm_current & IFM_IEEE80211_MBSS)
4209195618Srpaulo			return IEEE80211_M_MBSS;
4210138593Ssam	}
4211138593Ssam	return IEEE80211_M_STA;
4212138593Ssam}
4213138593Ssam
4214138593Ssam#if 0
4215138593Ssamstatic void
4216138593Ssamprintcipher(int s, struct ieee80211req *ireq, int keylenop)
4217138593Ssam{
4218138593Ssam	switch (ireq->i_val) {
4219138593Ssam	case IEEE80211_CIPHER_WEP:
4220138593Ssam		ireq->i_type = keylenop;
4221138593Ssam		if (ioctl(s, SIOCG80211, ireq) != -1)
4222138593Ssam			printf("WEP-%s",
4223138593Ssam			    ireq->i_len <= 5 ? "40" :
4224138593Ssam			    ireq->i_len <= 13 ? "104" : "128");
4225138593Ssam		else
4226138593Ssam			printf("WEP");
4227138593Ssam		break;
4228138593Ssam	case IEEE80211_CIPHER_TKIP:
4229138593Ssam		printf("TKIP");
4230138593Ssam		break;
4231138593Ssam	case IEEE80211_CIPHER_AES_OCB:
4232138593Ssam		printf("AES-OCB");
4233138593Ssam		break;
4234138593Ssam	case IEEE80211_CIPHER_AES_CCM:
4235138593Ssam		printf("AES-CCM");
4236138593Ssam		break;
4237138593Ssam	case IEEE80211_CIPHER_CKIP:
4238138593Ssam		printf("CKIP");
4239138593Ssam		break;
4240138593Ssam	case IEEE80211_CIPHER_NONE:
4241138593Ssam		printf("NONE");
4242138593Ssam		break;
4243138593Ssam	default:
4244138593Ssam		printf("UNKNOWN (0x%x)", ireq->i_val);
4245138593Ssam		break;
4246138593Ssam	}
4247138593Ssam}
4248138593Ssam#endif
4249138593Ssam
4250155931Ssamstatic void
4251138593Ssamprintkey(const struct ieee80211req_key *ik)
4252138593Ssam{
4253138593Ssam	static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
4254298556Spfg	u_int keylen = ik->ik_keylen;
4255138593Ssam	int printcontents;
4256138593Ssam
4257148001Srwatson	printcontents = printkeys &&
4258138593Ssam		(memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
4259138593Ssam	if (printcontents)
4260138593Ssam		LINE_BREAK();
4261138593Ssam	switch (ik->ik_type) {
4262138593Ssam	case IEEE80211_CIPHER_WEP:
4263138593Ssam		/* compatibility */
4264155931Ssam		LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
4265138593Ssam		    keylen <= 5 ? "40-bit" :
4266138593Ssam		    keylen <= 13 ? "104-bit" : "128-bit");
4267138593Ssam		break;
4268138593Ssam	case IEEE80211_CIPHER_TKIP:
4269138593Ssam		if (keylen > 128/8)
4270138593Ssam			keylen -= 128/8;	/* ignore MIC for now */
4271155931Ssam		LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4272138593Ssam		break;
4273138593Ssam	case IEEE80211_CIPHER_AES_OCB:
4274155931Ssam		LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4275138593Ssam		break;
4276138593Ssam	case IEEE80211_CIPHER_AES_CCM:
4277155931Ssam		LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4278138593Ssam		break;
4279138593Ssam	case IEEE80211_CIPHER_CKIP:
4280155931Ssam		LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4281138593Ssam		break;
4282138593Ssam	case IEEE80211_CIPHER_NONE:
4283155931Ssam		LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4284138593Ssam		break;
4285138593Ssam	default:
4286155931Ssam		LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
4287138593Ssam			ik->ik_type, ik->ik_keyix+1, 8*keylen);
4288138593Ssam		break;
4289138593Ssam	}
4290138593Ssam	if (printcontents) {
4291298556Spfg		u_int i;
4292138593Ssam
4293138593Ssam		printf(" <");
4294138593Ssam		for (i = 0; i < keylen; i++)
4295138593Ssam			printf("%02x", ik->ik_keydata[i]);
4296138593Ssam		printf(">");
4297138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4298138593Ssam		    (ik->ik_keyrsc != 0 || verbose))
4299146873Sjhb			printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
4300138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4301138593Ssam		    (ik->ik_keytsc != 0 || verbose))
4302146873Sjhb			printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
4303138593Ssam		if (ik->ik_flags != 0 && verbose) {
4304138593Ssam			const char *sep = " ";
4305138593Ssam
4306138593Ssam			if (ik->ik_flags & IEEE80211_KEY_XMIT)
4307138593Ssam				printf("%stx", sep), sep = "+";
4308138593Ssam			if (ik->ik_flags & IEEE80211_KEY_RECV)
4309138593Ssam				printf("%srx", sep), sep = "+";
4310138593Ssam			if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
4311138593Ssam				printf("%sdef", sep), sep = "+";
4312138593Ssam		}
4313138593Ssam		LINE_BREAK();
4314138593Ssam	}
4315138593Ssam}
4316138593Ssam
4317138593Ssamstatic void
4318173275Ssamprintrate(const char *tag, int v, int defrate, int defmcs)
4319138593Ssam{
4320188784Ssam	if ((v & IEEE80211_RATE_MCS) == 0) {
4321188784Ssam		if (v != defrate) {
4322188784Ssam			if (v & 1)
4323188784Ssam				LINE_CHECK("%s %d.5", tag, v/2);
4324188784Ssam			else
4325188784Ssam				LINE_CHECK("%s %d", tag, v/2);
4326188784Ssam		}
4327188784Ssam	} else {
4328173275Ssam		if (v != defmcs)
4329173275Ssam			LINE_CHECK("%s %d", tag, v &~ 0x80);
4330173275Ssam	}
4331173275Ssam}
4332173275Ssam
4333173275Ssamstatic int
4334195618Srpaulogetid(int s, int ix, void *data, size_t len, int *plen, int mesh)
4335173275Ssam{
4336138593Ssam	struct ieee80211req ireq;
4337138593Ssam
4338138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4339138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4340195618Srpaulo	ireq.i_type = (!mesh) ? IEEE80211_IOC_SSID : IEEE80211_IOC_MESH_ID;
4341173275Ssam	ireq.i_val = ix;
4342173275Ssam	ireq.i_data = data;
4343173275Ssam	ireq.i_len = len;
4344173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4345173275Ssam		return -1;
4346173275Ssam	*plen = ireq.i_len;
4347173275Ssam	return 0;
4348173275Ssam}
434977218Sphk
4350173275Ssamstatic void
4351173275Ssamieee80211_status(int s)
4352173275Ssam{
4353173275Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
4354173275Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
4355173275Ssam	int i, num, wpa, wme, bgscan, bgscaninterval, val, len, wepmode;
4356173275Ssam	uint8_t data[32];
4357173275Ssam	const struct ieee80211_channel *c;
4358178354Ssam	const struct ieee80211_roamparam *rp;
4359178354Ssam	const struct ieee80211_txparam *tp;
4360173275Ssam
4361195618Srpaulo	if (getid(s, -1, data, sizeof(data), &len, 0) < 0) {
4362148686Sstefanf		/* If we can't get the SSID, this isn't an 802.11 device. */
436377218Sphk		return;
436477218Sphk	}
4365173275Ssam
4366173275Ssam	/*
4367173275Ssam	 * Invalidate cached state so printing status for multiple
4368173275Ssam	 * if's doesn't reuse the first interfaces' cached state.
4369173275Ssam	 */
4370173275Ssam	gotcurchan = 0;
4371178354Ssam	gotroam = 0;
4372178354Ssam	gottxparams = 0;
4373173275Ssam	gothtconf = 0;
4374178354Ssam	gotregdomain = 0;
4375173275Ssam
4376195618Srpaulo	printf("\t");
4377195618Srpaulo	if (opmode == IEEE80211_M_MBSS) {
4378195618Srpaulo		printf("meshid ");
4379195618Srpaulo		getid(s, 0, data, sizeof(data), &len, 1);
4380195618Srpaulo		print_string(data, len);
4381195618Srpaulo	} else {
4382195618Srpaulo		if (get80211val(s, IEEE80211_IOC_NUMSSIDS, &num) < 0)
4383195618Srpaulo			num = 0;
4384195618Srpaulo		printf("ssid ");
4385195618Srpaulo		if (num > 1) {
4386195618Srpaulo			for (i = 0; i < num; i++) {
4387195618Srpaulo				if (getid(s, i, data, sizeof(data), &len, 0) >= 0 && len > 0) {
4388195618Srpaulo					printf(" %d:", i + 1);
4389195618Srpaulo					print_string(data, len);
4390195618Srpaulo				}
4391138593Ssam			}
4392195618Srpaulo		} else
4393195618Srpaulo			print_string(data, len);
4394195618Srpaulo	}
4395173275Ssam	c = getcurchan(s);
4396170531Ssam	if (c->ic_freq != IEEE80211_CHAN_ANY) {
4397170531Ssam		char buf[14];
4398202161Sgavin		printf(" channel %d (%u MHz%s)", c->ic_ieee, c->ic_freq,
4399170531Ssam			get_chaninfo(c, 1, buf, sizeof(buf)));
4400138593Ssam	} else if (verbose)
4401138593Ssam		printf(" channel UNDEF");
4402138593Ssam
4403173275Ssam	if (get80211(s, IEEE80211_IOC_BSSID, data, IEEE80211_ADDR_LEN) >= 0 &&
4404173275Ssam	    (memcmp(data, zerobssid, sizeof(zerobssid)) != 0 || verbose))
4405173275Ssam		printf(" bssid %s", ether_ntoa((struct ether_addr *)data));
4406138593Ssam
4407173275Ssam	if (get80211len(s, IEEE80211_IOC_STATIONNAME, data, sizeof(data), &len) != -1) {
4408138593Ssam		printf("\n\tstationname ");
4409173275Ssam		print_string(data, len);
441077218Sphk	}
441177218Sphk
4412138593Ssam	spacer = ' ';		/* force first break */
4413138593Ssam	LINE_BREAK();
441477218Sphk
4415178354Ssam	list_regdomain(s, 0);
4416178354Ssam
4417173275Ssam	wpa = 0;
4418173275Ssam	if (get80211val(s, IEEE80211_IOC_AUTHMODE, &val) != -1) {
4419173275Ssam		switch (val) {
4420173275Ssam		case IEEE80211_AUTH_NONE:
4421173275Ssam			LINE_CHECK("authmode NONE");
4422173275Ssam			break;
4423173275Ssam		case IEEE80211_AUTH_OPEN:
4424173275Ssam			LINE_CHECK("authmode OPEN");
4425173275Ssam			break;
4426173275Ssam		case IEEE80211_AUTH_SHARED:
4427173275Ssam			LINE_CHECK("authmode SHARED");
4428173275Ssam			break;
4429173275Ssam		case IEEE80211_AUTH_8021X:
4430173275Ssam			LINE_CHECK("authmode 802.1x");
4431173275Ssam			break;
4432173275Ssam		case IEEE80211_AUTH_WPA:
4433173275Ssam			if (get80211val(s, IEEE80211_IOC_WPA, &wpa) < 0)
4434173275Ssam				wpa = 1;	/* default to WPA1 */
4435173275Ssam			switch (wpa) {
4436173275Ssam			case 2:
4437173275Ssam				LINE_CHECK("authmode WPA2/802.11i");
443877218Sphk				break;
4439173275Ssam			case 3:
4440173275Ssam				LINE_CHECK("authmode WPA1+WPA2/802.11i");
444177218Sphk				break;
4442127649Ssam			default:
4443173275Ssam				LINE_CHECK("authmode WPA");
4444127649Ssam				break;
4445173275Ssam			}
4446173275Ssam			break;
4447173275Ssam		case IEEE80211_AUTH_AUTO:
4448173275Ssam			LINE_CHECK("authmode AUTO");
4449173275Ssam			break;
4450173275Ssam		default:
4451173275Ssam			LINE_CHECK("authmode UNKNOWN (0x%x)", val);
4452173275Ssam			break;
4453127649Ssam		}
4454127649Ssam	}
4455127649Ssam
4456173275Ssam	if (wpa || verbose) {
4457178354Ssam		if (get80211val(s, IEEE80211_IOC_WPS, &val) != -1) {
4458178354Ssam			if (val)
4459178354Ssam				LINE_CHECK("wps");
4460178354Ssam			else if (verbose)
4461178354Ssam				LINE_CHECK("-wps");
4462178354Ssam		}
4463178354Ssam		if (get80211val(s, IEEE80211_IOC_TSN, &val) != -1) {
4464178354Ssam			if (val)
4465178354Ssam				LINE_CHECK("tsn");
4466178354Ssam			else if (verbose)
4467178354Ssam				LINE_CHECK("-tsn");
4468178354Ssam		}
4469173275Ssam		if (ioctl(s, IEEE80211_IOC_COUNTERMEASURES, &val) != -1) {
4470173275Ssam			if (val)
4471173275Ssam				LINE_CHECK("countermeasures");
4472173275Ssam			else if (verbose)
4473173275Ssam				LINE_CHECK("-countermeasures");
4474173275Ssam		}
4475178354Ssam#if 0
4476178354Ssam		/* XXX not interesting with WPA done in user space */
4477178354Ssam		ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
4478178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4479178354Ssam		}
4480178354Ssam
4481178354Ssam		ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
4482178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4483178354Ssam			LINE_CHECK("mcastcipher ");
4484178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
4485178354Ssam			spacer = ' ';
4486178354Ssam		}
4487178354Ssam
4488178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
4489178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4490178354Ssam			LINE_CHECK("ucastcipher ");
4491178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
4492178354Ssam		}
4493178354Ssam
4494178354Ssam		if (wpa & 2) {
4495178354Ssam			ireq.i_type = IEEE80211_IOC_RSNCAPS;
4496178354Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1) {
4497178354Ssam				LINE_CHECK("RSN caps 0x%x", ireq.i_val);
4498178354Ssam				spacer = ' ';
4499178354Ssam			}
4500178354Ssam		}
4501178354Ssam
4502178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
4503178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4504178354Ssam		}
4505178354Ssam#endif
4506173275Ssam	}
4507138593Ssam
4508173275Ssam	if (get80211val(s, IEEE80211_IOC_WEP, &wepmode) != -1 &&
4509173275Ssam	    wepmode != IEEE80211_WEP_NOSUP) {
4510173275Ssam
4511138718Ssam		switch (wepmode) {
4512173275Ssam		case IEEE80211_WEP_OFF:
4513173275Ssam			LINE_CHECK("privacy OFF");
4514173275Ssam			break;
4515173275Ssam		case IEEE80211_WEP_ON:
4516173275Ssam			LINE_CHECK("privacy ON");
4517173275Ssam			break;
4518173275Ssam		case IEEE80211_WEP_MIXED:
4519173275Ssam			LINE_CHECK("privacy MIXED");
4520173275Ssam			break;
4521173275Ssam		default:
4522173275Ssam			LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
4523173275Ssam			break;
452477218Sphk		}
452577218Sphk
452677218Sphk		/*
452777218Sphk		 * If we get here then we've got WEP support so we need
452877218Sphk		 * to print WEP status.
452991454Sbrooks		 */
453077218Sphk
4531173275Ssam		if (get80211val(s, IEEE80211_IOC_WEPTXKEY, &val) < 0) {
453277218Sphk			warn("WEP support, but no tx key!");
453377218Sphk			goto end;
453477218Sphk		}
4535173275Ssam		if (val != -1)
4536173275Ssam			LINE_CHECK("deftxkey %d", val+1);
4537138718Ssam		else if (wepmode != IEEE80211_WEP_OFF || verbose)
4538155931Ssam			LINE_CHECK("deftxkey UNDEF");
453977218Sphk
4540173275Ssam		if (get80211val(s, IEEE80211_IOC_NUMWEPKEYS, &num) < 0) {
454177218Sphk			warn("WEP support, but no NUMWEPKEYS support!");
454277218Sphk			goto end;
454377218Sphk		}
454477218Sphk
4545138593Ssam		for (i = 0; i < num; i++) {
4546138593Ssam			struct ieee80211req_key ik;
454777218Sphk
4548138593Ssam			memset(&ik, 0, sizeof(ik));
4549138593Ssam			ik.ik_keyix = i;
4550173275Ssam			if (get80211(s, IEEE80211_IOC_WPAKEY, &ik, sizeof(ik)) < 0) {
455177218Sphk				warn("WEP support, but can get keys!");
455277218Sphk				goto end;
455377218Sphk			}
4554138593Ssam			if (ik.ik_keylen != 0) {
4555138593Ssam				if (verbose)
4556138593Ssam					LINE_BREAK();
4557138593Ssam				printkey(&ik);
4558138593Ssam			}
4559138593Ssam		}
4560173275Ssamend:
4561173275Ssam		;
4562138593Ssam	}
4563138593Ssam
4564173275Ssam	if (get80211val(s, IEEE80211_IOC_POWERSAVE, &val) != -1 &&
4565173275Ssam	    val != IEEE80211_POWERSAVE_NOSUP ) {
4566173275Ssam		if (val != IEEE80211_POWERSAVE_OFF || verbose) {
4567173275Ssam			switch (val) {
4568173275Ssam			case IEEE80211_POWERSAVE_OFF:
4569173275Ssam				LINE_CHECK("powersavemode OFF");
4570173275Ssam				break;
4571173275Ssam			case IEEE80211_POWERSAVE_CAM:
4572173275Ssam				LINE_CHECK("powersavemode CAM");
4573173275Ssam				break;
4574173275Ssam			case IEEE80211_POWERSAVE_PSP:
4575173275Ssam				LINE_CHECK("powersavemode PSP");
4576173275Ssam				break;
4577173275Ssam			case IEEE80211_POWERSAVE_PSP_CAM:
4578173275Ssam				LINE_CHECK("powersavemode PSP-CAM");
4579173275Ssam				break;
4580138593Ssam			}
4581173275Ssam			if (get80211val(s, IEEE80211_IOC_POWERSAVESLEEP, &val) != -1)
4582173275Ssam				LINE_CHECK("powersavesleep %d", val);
4583138593Ssam		}
4584138593Ssam	}
4585138593Ssam
4586173275Ssam	if (get80211val(s, IEEE80211_IOC_TXPOWER, &val) != -1) {
4587173275Ssam		if (val & 1)
4588173275Ssam			LINE_CHECK("txpower %d.5", val/2);
4589173275Ssam		else
4590173275Ssam			LINE_CHECK("txpower %d", val/2);
4591173275Ssam	}
4592138593Ssam	if (verbose) {
4593173275Ssam		if (get80211val(s, IEEE80211_IOC_TXPOWMAX, &val) != -1)
4594173275Ssam			LINE_CHECK("txpowmax %.1f", val/2.);
4595138593Ssam	}
4596138593Ssam
4597178354Ssam	if (get80211val(s, IEEE80211_IOC_DOTD, &val) != -1) {
4598178354Ssam		if (val)
4599178354Ssam			LINE_CHECK("dotd");
4600178354Ssam		else if (verbose)
4601178354Ssam			LINE_CHECK("-dotd");
4602178354Ssam	}
4603178354Ssam
4604173275Ssam	if (get80211val(s, IEEE80211_IOC_RTSTHRESHOLD, &val) != -1) {
4605173275Ssam		if (val != IEEE80211_RTS_MAX || verbose)
4606173275Ssam			LINE_CHECK("rtsthreshold %d", val);
4607138593Ssam	}
4608138593Ssam
4609173275Ssam	if (get80211val(s, IEEE80211_IOC_FRAGTHRESHOLD, &val) != -1) {
4610173275Ssam		if (val != IEEE80211_FRAG_MAX || verbose)
4611173275Ssam			LINE_CHECK("fragthreshold %d", val);
4612170531Ssam	}
4613173275Ssam	if (opmode == IEEE80211_M_STA || verbose) {
4614173275Ssam		if (get80211val(s, IEEE80211_IOC_BMISSTHRESHOLD, &val) != -1) {
4615173275Ssam			if (val != IEEE80211_HWBMISS_MAX || verbose)
4616173275Ssam				LINE_CHECK("bmiss %d", val);
4617153354Ssam		}
4618153354Ssam	}
4619153354Ssam
4620178354Ssam	if (!verbose) {
4621178354Ssam		gettxparams(s);
4622178354Ssam		tp = &txparams.params[chan2mode(c)];
4623178354Ssam		printrate("ucastrate", tp->ucastrate,
4624178354Ssam		    IEEE80211_FIXED_RATE_NONE, IEEE80211_FIXED_RATE_NONE);
4625188784Ssam		printrate("mcastrate", tp->mcastrate, 2*1,
4626188784Ssam		    IEEE80211_RATE_MCS|0);
4627188784Ssam		printrate("mgmtrate", tp->mgmtrate, 2*1,
4628188784Ssam		    IEEE80211_RATE_MCS|0);
4629178354Ssam		if (tp->maxretry != 6)		/* XXX */
4630178354Ssam			LINE_CHECK("maxretry %d", tp->maxretry);
4631178354Ssam	} else {
4632178354Ssam		LINE_BREAK();
4633178354Ssam		list_txparams(s);
4634178354Ssam	}
4635170531Ssam
4636173275Ssam	bgscaninterval = -1;
4637173275Ssam	(void) get80211val(s, IEEE80211_IOC_BGSCAN_INTERVAL, &bgscaninterval);
4638173275Ssam
4639173275Ssam	if (get80211val(s, IEEE80211_IOC_SCANVALID, &val) != -1) {
4640173275Ssam		if (val != bgscaninterval || verbose)
4641173275Ssam			LINE_CHECK("scanvalid %u", val);
4642148416Ssam	}
4643148416Ssam
4644173275Ssam	bgscan = 0;
4645173275Ssam	if (get80211val(s, IEEE80211_IOC_BGSCAN, &bgscan) != -1) {
4646173275Ssam		if (bgscan)
4647170531Ssam			LINE_CHECK("bgscan");
4648170531Ssam		else if (verbose)
4649170531Ssam			LINE_CHECK("-bgscan");
4650160687Ssam	}
4651170531Ssam	if (bgscan || verbose) {
4652170531Ssam		if (bgscaninterval != -1)
4653170531Ssam			LINE_CHECK("bgscanintvl %u", bgscaninterval);
4654173275Ssam		if (get80211val(s, IEEE80211_IOC_BGSCAN_IDLE, &val) != -1)
4655173275Ssam			LINE_CHECK("bgscanidle %u", val);
4656178354Ssam		if (!verbose) {
4657178354Ssam			getroam(s);
4658178354Ssam			rp = &roamparams.params[chan2mode(c)];
4659178354Ssam			if (rp->rssi & 1)
4660178354Ssam				LINE_CHECK("roam:rssi %u.5", rp->rssi/2);
4661178354Ssam			else
4662178354Ssam				LINE_CHECK("roam:rssi %u", rp->rssi/2);
4663178354Ssam			LINE_CHECK("roam:rate %u", rp->rate/2);
4664178354Ssam		} else {
4665178354Ssam			LINE_BREAK();
4666178354Ssam			list_roam(s);
4667205514Srpaulo			LINE_BREAK();
4668170531Ssam		}
4669170531Ssam	}
4670160687Ssam
4671165570Ssam	if (IEEE80211_IS_CHAN_ANYG(c) || verbose) {
4672173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREG, &val) != -1) {
4673173275Ssam			if (val)
4674155931Ssam				LINE_CHECK("pureg");
4675147795Ssam			else if (verbose)
4676155931Ssam				LINE_CHECK("-pureg");
4677147795Ssam		}
4678173275Ssam		if (get80211val(s, IEEE80211_IOC_PROTMODE, &val) != -1) {
4679173275Ssam			switch (val) {
4680173275Ssam			case IEEE80211_PROTMODE_OFF:
4681173275Ssam				LINE_CHECK("protmode OFF");
4682173275Ssam				break;
4683173275Ssam			case IEEE80211_PROTMODE_CTS:
4684173275Ssam				LINE_CHECK("protmode CTS");
4685173275Ssam				break;
4686173275Ssam			case IEEE80211_PROTMODE_RTSCTS:
4687173275Ssam				LINE_CHECK("protmode RTSCTS");
4688173275Ssam				break;
4689173275Ssam			default:
4690173275Ssam				LINE_CHECK("protmode UNKNOWN (0x%x)", val);
4691173275Ssam				break;
4692138593Ssam			}
4693138593Ssam		}
4694138593Ssam	}
4695138593Ssam
4696173275Ssam	if (IEEE80211_IS_CHAN_HT(c) || verbose) {
4697173275Ssam		gethtconf(s);
4698173275Ssam		switch (htconf & 3) {
4699173275Ssam		case 0:
4700173275Ssam		case 2:
4701173275Ssam			LINE_CHECK("-ht");
4702173275Ssam			break;
4703173275Ssam		case 1:
4704173275Ssam			LINE_CHECK("ht20");
4705173275Ssam			break;
4706173275Ssam		case 3:
4707173275Ssam			if (verbose)
4708173275Ssam				LINE_CHECK("ht");
4709173275Ssam			break;
4710173275Ssam		}
4711173275Ssam		if (get80211val(s, IEEE80211_IOC_HTCOMPAT, &val) != -1) {
4712173275Ssam			if (!val)
4713173275Ssam				LINE_CHECK("-htcompat");
4714173275Ssam			else if (verbose)
4715173275Ssam				LINE_CHECK("htcompat");
4716173275Ssam		}
4717173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU, &val) != -1) {
4718173275Ssam			switch (val) {
4719173275Ssam			case 0:
4720173275Ssam				LINE_CHECK("-ampdu");
4721173275Ssam				break;
4722173275Ssam			case 1:
4723173275Ssam				LINE_CHECK("ampdutx -ampdurx");
4724173275Ssam				break;
4725173275Ssam			case 2:
4726173275Ssam				LINE_CHECK("-ampdutx ampdurx");
4727173275Ssam				break;
4728173275Ssam			case 3:
4729173275Ssam				if (verbose)
4730173275Ssam					LINE_CHECK("ampdu");
4731173275Ssam				break;
4732173275Ssam			}
4733173275Ssam		}
4734173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_LIMIT, &val) != -1) {
4735173275Ssam			switch (val) {
4736173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_8K:
4737173275Ssam				LINE_CHECK("ampdulimit 8k");
4738173275Ssam				break;
4739173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_16K:
4740173275Ssam				LINE_CHECK("ampdulimit 16k");
4741173275Ssam				break;
4742173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_32K:
4743173275Ssam				LINE_CHECK("ampdulimit 32k");
4744173275Ssam				break;
4745173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_64K:
4746173275Ssam				LINE_CHECK("ampdulimit 64k");
4747173275Ssam				break;
4748173275Ssam			}
4749173275Ssam		}
4750173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_DENSITY, &val) != -1) {
4751173275Ssam			switch (val) {
4752173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_NA:
4753173275Ssam				if (verbose)
4754183260Ssam					LINE_CHECK("ampdudensity NA");
4755173275Ssam				break;
4756173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_025:
4757173275Ssam				LINE_CHECK("ampdudensity .25");
4758173275Ssam				break;
4759173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_05:
4760173275Ssam				LINE_CHECK("ampdudensity .5");
4761173275Ssam				break;
4762173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_1:
4763173275Ssam				LINE_CHECK("ampdudensity 1");
4764173275Ssam				break;
4765173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_2:
4766173275Ssam				LINE_CHECK("ampdudensity 2");
4767173275Ssam				break;
4768173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_4:
4769173275Ssam				LINE_CHECK("ampdudensity 4");
4770173275Ssam				break;
4771173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_8:
4772173275Ssam				LINE_CHECK("ampdudensity 8");
4773173275Ssam				break;
4774173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_16:
4775173275Ssam				LINE_CHECK("ampdudensity 16");
4776173275Ssam				break;
4777173275Ssam			}
4778173275Ssam		}
4779173275Ssam		if (get80211val(s, IEEE80211_IOC_AMSDU, &val) != -1) {
4780173275Ssam			switch (val) {
4781173275Ssam			case 0:
4782173275Ssam				LINE_CHECK("-amsdu");
4783173275Ssam				break;
4784173275Ssam			case 1:
4785173275Ssam				LINE_CHECK("amsdutx -amsdurx");
4786173275Ssam				break;
4787173275Ssam			case 2:
4788173275Ssam				LINE_CHECK("-amsdutx amsdurx");
4789173275Ssam				break;
4790173275Ssam			case 3:
4791173275Ssam				if (verbose)
4792173275Ssam					LINE_CHECK("amsdu");
4793173275Ssam				break;
4794173275Ssam			}
4795173275Ssam		}
4796173275Ssam		/* XXX amsdu limit */
4797173275Ssam		if (get80211val(s, IEEE80211_IOC_SHORTGI, &val) != -1) {
4798173275Ssam			if (val)
4799173275Ssam				LINE_CHECK("shortgi");
4800173275Ssam			else if (verbose)
4801173275Ssam				LINE_CHECK("-shortgi");
4802173275Ssam		}
4803173275Ssam		if (get80211val(s, IEEE80211_IOC_HTPROTMODE, &val) != -1) {
4804173275Ssam			if (val == IEEE80211_PROTMODE_OFF)
4805173275Ssam				LINE_CHECK("htprotmode OFF");
4806173275Ssam			else if (val != IEEE80211_PROTMODE_RTSCTS)
4807173275Ssam				LINE_CHECK("htprotmode UNKNOWN (0x%x)", val);
4808173275Ssam			else if (verbose)
4809173275Ssam				LINE_CHECK("htprotmode RTSCTS");
4810173275Ssam		}
4811173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREN, &val) != -1) {
4812173275Ssam			if (val)
4813173275Ssam				LINE_CHECK("puren");
4814173275Ssam			else if (verbose)
4815173275Ssam				LINE_CHECK("-puren");
4816173275Ssam		}
4817183261Ssam		if (get80211val(s, IEEE80211_IOC_SMPS, &val) != -1) {
4818183261Ssam			if (val == IEEE80211_HTCAP_SMPS_DYNAMIC)
4819183261Ssam				LINE_CHECK("smpsdyn");
4820183261Ssam			else if (val == IEEE80211_HTCAP_SMPS_ENA)
4821183261Ssam				LINE_CHECK("smps");
4822183261Ssam			else if (verbose)
4823183261Ssam				LINE_CHECK("-smps");
4824183261Ssam		}
4825183261Ssam		if (get80211val(s, IEEE80211_IOC_RIFS, &val) != -1) {
4826183261Ssam			if (val)
4827183261Ssam				LINE_CHECK("rifs");
4828183261Ssam			else if (verbose)
4829183261Ssam				LINE_CHECK("-rifs");
4830183261Ssam		}
4831173275Ssam	}
4832173275Ssam
4833173275Ssam	if (get80211val(s, IEEE80211_IOC_WME, &wme) != -1) {
4834138593Ssam		if (wme)
4835155931Ssam			LINE_CHECK("wme");
4836138593Ssam		else if (verbose)
4837155931Ssam			LINE_CHECK("-wme");
4838138593Ssam	} else
4839138593Ssam		wme = 0;
4840138593Ssam
4841173275Ssam	if (get80211val(s, IEEE80211_IOC_BURST, &val) != -1) {
4842173275Ssam		if (val)
4843155931Ssam			LINE_CHECK("burst");
4844153422Ssam		else if (verbose)
4845155931Ssam			LINE_CHECK("-burst");
4846153422Ssam	}
4847153422Ssam
4848173275Ssam	if (get80211val(s, IEEE80211_IOC_FF, &val) != -1) {
4849173275Ssam		if (val)
4850170531Ssam			LINE_CHECK("ff");
4851170531Ssam		else if (verbose)
4852170531Ssam			LINE_CHECK("-ff");
4853170531Ssam	}
4854173275Ssam	if (get80211val(s, IEEE80211_IOC_TURBOP, &val) != -1) {
4855173275Ssam		if (val)
4856170531Ssam			LINE_CHECK("dturbo");
4857170531Ssam		else if (verbose)
4858170531Ssam			LINE_CHECK("-dturbo");
4859170531Ssam	}
4860178354Ssam	if (get80211val(s, IEEE80211_IOC_DWDS, &val) != -1) {
4861178354Ssam		if (val)
4862178354Ssam			LINE_CHECK("dwds");
4863178354Ssam		else if (verbose)
4864178354Ssam			LINE_CHECK("-dwds");
4865178354Ssam	}
4866170531Ssam
4867138593Ssam	if (opmode == IEEE80211_M_HOSTAP) {
4868173275Ssam		if (get80211val(s, IEEE80211_IOC_HIDESSID, &val) != -1) {
4869173275Ssam			if (val)
4870168075Ssam				LINE_CHECK("hidessid");
4871138593Ssam			else if (verbose)
4872168075Ssam				LINE_CHECK("-hidessid");
4873138593Ssam		}
4874173275Ssam		if (get80211val(s, IEEE80211_IOC_APBRIDGE, &val) != -1) {
4875173275Ssam			if (!val)
4876155931Ssam				LINE_CHECK("-apbridge");
4877138593Ssam			else if (verbose)
4878155931Ssam				LINE_CHECK("apbridge");
4879138593Ssam		}
4880173275Ssam		if (get80211val(s, IEEE80211_IOC_DTIM_PERIOD, &val) != -1)
4881173275Ssam			LINE_CHECK("dtimperiod %u", val);
4882138593Ssam
4883173275Ssam		if (get80211val(s, IEEE80211_IOC_DOTH, &val) != -1) {
4884173275Ssam			if (!val)
4885170531Ssam				LINE_CHECK("-doth");
4886170531Ssam			else if (verbose)
4887170531Ssam				LINE_CHECK("doth");
4888170531Ssam		}
4889178354Ssam		if (get80211val(s, IEEE80211_IOC_DFS, &val) != -1) {
4890178354Ssam			if (!val)
4891178354Ssam				LINE_CHECK("-dfs");
4892178354Ssam			else if (verbose)
4893178354Ssam				LINE_CHECK("dfs");
4894178354Ssam		}
4895173275Ssam		if (get80211val(s, IEEE80211_IOC_INACTIVITY, &val) != -1) {
4896173275Ssam			if (!val)
4897173275Ssam				LINE_CHECK("-inact");
4898173275Ssam			else if (verbose)
4899173275Ssam				LINE_CHECK("inact");
4900173275Ssam		}
4901138593Ssam	} else {
4902173275Ssam		if (get80211val(s, IEEE80211_IOC_ROAMING, &val) != -1) {
4903173275Ssam			if (val != IEEE80211_ROAMING_AUTO || verbose) {
4904173275Ssam				switch (val) {
4905138593Ssam				case IEEE80211_ROAMING_DEVICE:
4906155931Ssam					LINE_CHECK("roaming DEVICE");
4907138593Ssam					break;
4908138593Ssam				case IEEE80211_ROAMING_AUTO:
4909155931Ssam					LINE_CHECK("roaming AUTO");
4910138593Ssam					break;
4911138593Ssam				case IEEE80211_ROAMING_MANUAL:
4912155931Ssam					LINE_CHECK("roaming MANUAL");
4913138593Ssam					break;
4914138593Ssam				default:
4915155931Ssam					LINE_CHECK("roaming UNKNOWN (0x%x)",
4916173275Ssam						val);
4917138593Ssam					break;
4918138593Ssam				}
4919138593Ssam			}
4920138593Ssam		}
4921138593Ssam	}
4922186101Ssam
4923186904Ssam	if (opmode == IEEE80211_M_AHDEMO) {
4924186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOT, &val) != -1)
4925186904Ssam			LINE_CHECK("tdmaslot %u", val);
4926186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTCNT, &val) != -1)
4927186904Ssam			LINE_CHECK("tdmaslotcnt %u", val);
4928186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTLEN, &val) != -1)
4929186904Ssam			LINE_CHECK("tdmaslotlen %u", val);
4930186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_BINTERVAL, &val) != -1)
4931186904Ssam			LINE_CHECK("tdmabintval %u", val);
4932186904Ssam	} else if (get80211val(s, IEEE80211_IOC_BEACON_INTERVAL, &val) != -1) {
4933173275Ssam		/* XXX default define not visible */
4934173275Ssam		if (val != 100 || verbose)
4935173275Ssam			LINE_CHECK("bintval %u", val);
4936138593Ssam	}
4937138593Ssam
4938138593Ssam	if (wme && verbose) {
4939138593Ssam		LINE_BREAK();
4940138593Ssam		list_wme(s);
4941138593Ssam	}
4942195618Srpaulo
4943195618Srpaulo	if (opmode == IEEE80211_M_MBSS) {
4944195618Srpaulo		if (get80211val(s, IEEE80211_IOC_MESH_TTL, &val) != -1) {
4945195618Srpaulo			LINE_CHECK("meshttl %u", val);
4946195618Srpaulo		}
4947195618Srpaulo		if (get80211val(s, IEEE80211_IOC_MESH_AP, &val) != -1) {
4948195618Srpaulo			if (val)
4949195618Srpaulo				LINE_CHECK("meshpeering");
4950195618Srpaulo			else
4951195618Srpaulo				LINE_CHECK("-meshpeering");
4952195618Srpaulo		}
4953195618Srpaulo		if (get80211val(s, IEEE80211_IOC_MESH_FWRD, &val) != -1) {
4954195618Srpaulo			if (val)
4955195618Srpaulo				LINE_CHECK("meshforward");
4956195618Srpaulo			else
4957195618Srpaulo				LINE_CHECK("-meshforward");
4958195618Srpaulo		}
4959234893Smonthadar		if (get80211val(s, IEEE80211_IOC_MESH_GATE, &val) != -1) {
4960234893Smonthadar			if (val)
4961234893Smonthadar				LINE_CHECK("meshgate");
4962234893Smonthadar			else
4963234893Smonthadar				LINE_CHECK("-meshgate");
4964234893Smonthadar		}
4965195618Srpaulo		if (get80211len(s, IEEE80211_IOC_MESH_PR_METRIC, data, 12,
4966195618Srpaulo		    &len) != -1) {
4967195618Srpaulo			data[len] = '\0';
4968195618Srpaulo			LINE_CHECK("meshmetric %s", data);
4969195618Srpaulo		}
4970195618Srpaulo		if (get80211len(s, IEEE80211_IOC_MESH_PR_PATH, data, 12,
4971195618Srpaulo		    &len) != -1) {
4972195618Srpaulo			data[len] = '\0';
4973195618Srpaulo			LINE_CHECK("meshpath %s", data);
4974195618Srpaulo		}
4975195618Srpaulo		if (get80211val(s, IEEE80211_IOC_HWMP_ROOTMODE, &val) != -1) {
4976195618Srpaulo			switch (val) {
4977195618Srpaulo			case IEEE80211_HWMP_ROOTMODE_DISABLED:
4978195618Srpaulo				LINE_CHECK("hwmprootmode DISABLED");
4979195618Srpaulo				break;
4980195618Srpaulo			case IEEE80211_HWMP_ROOTMODE_NORMAL:
4981195618Srpaulo				LINE_CHECK("hwmprootmode NORMAL");
4982195618Srpaulo				break;
4983195618Srpaulo			case IEEE80211_HWMP_ROOTMODE_PROACTIVE:
4984195618Srpaulo				LINE_CHECK("hwmprootmode PROACTIVE");
4985195618Srpaulo				break;
4986195618Srpaulo			case IEEE80211_HWMP_ROOTMODE_RANN:
4987195618Srpaulo				LINE_CHECK("hwmprootmode RANN");
4988195618Srpaulo				break;
4989195618Srpaulo			default:
4990195618Srpaulo				LINE_CHECK("hwmprootmode UNKNOWN(%d)", val);
4991195618Srpaulo				break;
4992195618Srpaulo			}
4993195618Srpaulo		}
4994195618Srpaulo		if (get80211val(s, IEEE80211_IOC_HWMP_MAXHOPS, &val) != -1) {
4995195618Srpaulo			LINE_CHECK("hwmpmaxhops %u", val);
4996195618Srpaulo		}
4997195618Srpaulo	}
4998195618Srpaulo
4999173275Ssam	LINE_BREAK();
5000173275Ssam}
5001138593Ssam
5002173275Ssamstatic int
5003173275Ssamget80211(int s, int type, void *data, int len)
5004173275Ssam{
5005138593Ssam
5006291470Sadrian	return (lib80211_get80211(s, name, type, data, len));
5007173275Ssam}
5008138593Ssam
5009173275Ssamstatic int
5010173275Ssamget80211len(int s, int type, void *data, int len, int *plen)
5011173275Ssam{
5012138593Ssam
5013291470Sadrian	return (lib80211_get80211len(s, name, type, data, len, plen));
5014173275Ssam}
5015138593Ssam
5016173275Ssamstatic int
5017173275Ssamget80211val(int s, int type, int *val)
5018173275Ssam{
501977218Sphk
5020291470Sadrian	return (lib80211_get80211val(s, name, type, val));
502177218Sphk}
502277218Sphk
502377218Sphkstatic void
5024170531Ssamset80211(int s, int type, int val, int len, void *data)
502577218Sphk{
5026291470Sadrian	int ret;
502777218Sphk
5028291470Sadrian	ret = lib80211_set80211(s, name, type, val, len, data);
5029291470Sadrian	if (ret < 0)
503077218Sphk		err(1, "SIOCS80211");
503177218Sphk}
503277218Sphk
503377218Sphkstatic const char *
503477218Sphkget_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
503577218Sphk{
503677218Sphk	int len;
503777218Sphk	int hexstr;
503877218Sphk	u_int8_t *p;
503977218Sphk
504077218Sphk	len = *lenp;
504177218Sphk	p = buf;
504277218Sphk	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
504377218Sphk	if (hexstr)
504477218Sphk		val += 2;
504577218Sphk	for (;;) {
504677218Sphk		if (*val == '\0')
504777218Sphk			break;
504877218Sphk		if (sep != NULL && strchr(sep, *val) != NULL) {
504977218Sphk			val++;
505077218Sphk			break;
505177218Sphk		}
505277218Sphk		if (hexstr) {
5053127831Sphk			if (!isxdigit((u_char)val[0])) {
505477218Sphk				warnx("bad hexadecimal digits");
505577218Sphk				return NULL;
505677218Sphk			}
5057127831Sphk			if (!isxdigit((u_char)val[1])) {
5058127831Sphk				warnx("odd count hexadecimal digits");
5059127831Sphk				return NULL;
5060127831Sphk			}
506177218Sphk		}
5062127831Sphk		if (p >= buf + len) {
506377218Sphk			if (hexstr)
506477218Sphk				warnx("hexadecimal digits too long");
506577218Sphk			else
5066127831Sphk				warnx("string too long");
506777218Sphk			return NULL;
506877218Sphk		}
506977218Sphk		if (hexstr) {
507077218Sphk#define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
507177218Sphk			*p++ = (tohex((u_char)val[0]) << 4) |
507277218Sphk			    tohex((u_char)val[1]);
507377218Sphk#undef tohex
507477218Sphk			val += 2;
507577218Sphk		} else
507677218Sphk			*p++ = *val++;
507777218Sphk	}
507877218Sphk	len = p - buf;
507977218Sphk	/* The string "-" is treated as the empty string. */
5080165045Ssam	if (!hexstr && len == 1 && buf[0] == '-') {
508177218Sphk		len = 0;
5082165045Ssam		memset(buf, 0, *lenp);
5083165045Ssam	} else if (len < *lenp)
508477218Sphk		memset(p, 0, *lenp - len);
508577218Sphk	*lenp = len;
508677218Sphk	return val;
508777218Sphk}
508877218Sphk
508977218Sphkstatic void
509077218Sphkprint_string(const u_int8_t *buf, int len)
509177218Sphk{
509277218Sphk	int i;
509377218Sphk	int hasspc;
509477218Sphk
509577218Sphk	i = 0;
509677218Sphk	hasspc = 0;
509791454Sbrooks	for (; i < len; i++) {
509877218Sphk		if (!isprint(buf[i]) && buf[i] != '\0')
509977218Sphk			break;
510077218Sphk		if (isspace(buf[i]))
510177218Sphk			hasspc++;
510277218Sphk	}
510377218Sphk	if (i == len) {
510477218Sphk		if (hasspc || len == 0 || buf[0] == '\0')
510577218Sphk			printf("\"%.*s\"", len, buf);
510677218Sphk		else
510777218Sphk			printf("%.*s", len, buf);
510877218Sphk	} else {
510977218Sphk		printf("0x");
511077218Sphk		for (i = 0; i < len; i++)
511177218Sphk			printf("%02x", buf[i]);
511277218Sphk	}
511377218Sphk}
511477218Sphk
5115178354Ssam/*
5116178354Ssam * Virtual AP cloning support.
5117178354Ssam */
5118178354Ssamstatic struct ieee80211_clone_params params = {
5119178354Ssam	.icp_opmode	= IEEE80211_M_STA,	/* default to station mode */
5120178354Ssam};
5121178354Ssam
5122178354Ssamstatic void
5123178354Ssamwlan_create(int s, struct ifreq *ifr)
5124178354Ssam{
5125178354Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
5126178354Ssam
5127178354Ssam	if (params.icp_parent[0] == '\0')
5128189096Srpaulo		errx(1, "must specify a parent device (wlandev) when creating "
5129189096Srpaulo		    "a wlan device");
5130178354Ssam	if (params.icp_opmode == IEEE80211_M_WDS &&
5131178354Ssam	    memcmp(params.icp_bssid, zerobssid, sizeof(zerobssid)) == 0)
5132178354Ssam		errx(1, "no bssid specified for WDS (use wlanbssid)");
5133178354Ssam	ifr->ifr_data = (caddr_t) &params;
5134178354Ssam	if (ioctl(s, SIOCIFCREATE2, ifr) < 0)
5135178354Ssam		err(1, "SIOCIFCREATE2");
5136178354Ssam}
5137178354Ssam
5138178354Ssamstatic
5139178354SsamDECL_CMD_FUNC(set80211clone_wlandev, arg, d)
5140178354Ssam{
5141178354Ssam	strlcpy(params.icp_parent, arg, IFNAMSIZ);
5142178354Ssam}
5143178354Ssam
5144178354Ssamstatic
5145178354SsamDECL_CMD_FUNC(set80211clone_wlanbssid, arg, d)
5146178354Ssam{
5147178354Ssam	const struct ether_addr *ea;
5148178354Ssam
5149178354Ssam	ea = ether_aton(arg);
5150178354Ssam	if (ea == NULL)
5151178354Ssam		errx(1, "%s: cannot parse bssid", arg);
5152178354Ssam	memcpy(params.icp_bssid, ea->octet, IEEE80211_ADDR_LEN);
5153178354Ssam}
5154178354Ssam
5155178354Ssamstatic
5156178354SsamDECL_CMD_FUNC(set80211clone_wlanaddr, arg, d)
5157178354Ssam{
5158178354Ssam	const struct ether_addr *ea;
5159178354Ssam
5160178354Ssam	ea = ether_aton(arg);
5161178354Ssam	if (ea == NULL)
5162207320Srpaulo		errx(1, "%s: cannot parse address", arg);
5163178354Ssam	memcpy(params.icp_macaddr, ea->octet, IEEE80211_ADDR_LEN);
5164178354Ssam	params.icp_flags |= IEEE80211_CLONE_MACADDR;
5165178354Ssam}
5166178354Ssam
5167178354Ssamstatic
5168178354SsamDECL_CMD_FUNC(set80211clone_wlanmode, arg, d)
5169178354Ssam{
5170178354Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
5171178354Ssam	if (iseq(arg, "sta"))
5172178354Ssam		params.icp_opmode = IEEE80211_M_STA;
5173178354Ssam	else if (iseq(arg, "ahdemo") || iseq(arg, "adhoc-demo"))
5174178354Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
5175178354Ssam	else if (iseq(arg, "ibss") || iseq(arg, "adhoc"))
5176178354Ssam		params.icp_opmode = IEEE80211_M_IBSS;
5177178354Ssam	else if (iseq(arg, "ap") || iseq(arg, "host"))
5178178354Ssam		params.icp_opmode = IEEE80211_M_HOSTAP;
5179178354Ssam	else if (iseq(arg, "wds"))
5180178354Ssam		params.icp_opmode = IEEE80211_M_WDS;
5181178354Ssam	else if (iseq(arg, "monitor"))
5182178354Ssam		params.icp_opmode = IEEE80211_M_MONITOR;
5183186904Ssam	else if (iseq(arg, "tdma")) {
5184186904Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
5185186904Ssam		params.icp_flags |= IEEE80211_CLONE_TDMA;
5186195618Srpaulo	} else if (iseq(arg, "mesh") || iseq(arg, "mp")) /* mesh point */
5187195618Srpaulo		params.icp_opmode = IEEE80211_M_MBSS;
5188195618Srpaulo	else
5189178354Ssam		errx(1, "Don't know to create %s for %s", arg, name);
5190178354Ssam#undef iseq
5191178354Ssam}
5192178354Ssam
5193178354Ssamstatic void
5194178354Ssamset80211clone_beacons(const char *val, int d, int s, const struct afswtch *rafp)
5195178354Ssam{
5196178354Ssam	/* NB: inverted sense */
5197178354Ssam	if (d)
5198178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_NOBEACONS;
5199178354Ssam	else
5200178354Ssam		params.icp_flags |= IEEE80211_CLONE_NOBEACONS;
5201178354Ssam}
5202178354Ssam
5203178354Ssamstatic void
5204178354Ssamset80211clone_bssid(const char *val, int d, int s, const struct afswtch *rafp)
5205178354Ssam{
5206178354Ssam	if (d)
5207178354Ssam		params.icp_flags |= IEEE80211_CLONE_BSSID;
5208178354Ssam	else
5209178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_BSSID;
5210178354Ssam}
5211178354Ssam
5212178354Ssamstatic void
5213178354Ssamset80211clone_wdslegacy(const char *val, int d, int s, const struct afswtch *rafp)
5214178354Ssam{
5215178354Ssam	if (d)
5216178354Ssam		params.icp_flags |= IEEE80211_CLONE_WDSLEGACY;
5217178354Ssam	else
5218178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_WDSLEGACY;
5219178354Ssam}
5220178354Ssam
5221138593Ssamstatic struct cmd ieee80211_cmds[] = {
5222138593Ssam	DEF_CMD_ARG("ssid",		set80211ssid),
5223138593Ssam	DEF_CMD_ARG("nwid",		set80211ssid),
5224195618Srpaulo	DEF_CMD_ARG("meshid",		set80211meshid),
5225138593Ssam	DEF_CMD_ARG("stationname",	set80211stationname),
5226138593Ssam	DEF_CMD_ARG("station",		set80211stationname),	/* BSD/OS */
5227138593Ssam	DEF_CMD_ARG("channel",		set80211channel),
5228138593Ssam	DEF_CMD_ARG("authmode",		set80211authmode),
5229138593Ssam	DEF_CMD_ARG("powersavemode",	set80211powersavemode),
5230138593Ssam	DEF_CMD("powersave",	1,	set80211powersave),
5231138593Ssam	DEF_CMD("-powersave",	0,	set80211powersave),
5232138593Ssam	DEF_CMD_ARG("powersavesleep", 	set80211powersavesleep),
5233138593Ssam	DEF_CMD_ARG("wepmode",		set80211wepmode),
5234138593Ssam	DEF_CMD("wep",		1,	set80211wep),
5235138593Ssam	DEF_CMD("-wep",		0,	set80211wep),
5236139493Ssam	DEF_CMD_ARG("deftxkey",		set80211weptxkey),
5237138593Ssam	DEF_CMD_ARG("weptxkey",		set80211weptxkey),
5238138593Ssam	DEF_CMD_ARG("wepkey",		set80211wepkey),
5239138593Ssam	DEF_CMD_ARG("nwkey",		set80211nwkey),		/* NetBSD */
5240138593Ssam	DEF_CMD("-nwkey",	0,	set80211wep),		/* NetBSD */
5241138593Ssam	DEF_CMD_ARG("rtsthreshold",	set80211rtsthreshold),
5242138593Ssam	DEF_CMD_ARG("protmode",		set80211protmode),
5243138593Ssam	DEF_CMD_ARG("txpower",		set80211txpower),
5244138593Ssam	DEF_CMD_ARG("roaming",		set80211roaming),
5245138593Ssam	DEF_CMD("wme",		1,	set80211wme),
5246138593Ssam	DEF_CMD("-wme",		0,	set80211wme),
5247178354Ssam	DEF_CMD("wmm",		1,	set80211wme),
5248178354Ssam	DEF_CMD("-wmm",		0,	set80211wme),
5249138593Ssam	DEF_CMD("hidessid",	1,	set80211hidessid),
5250138593Ssam	DEF_CMD("-hidessid",	0,	set80211hidessid),
5251138593Ssam	DEF_CMD("apbridge",	1,	set80211apbridge),
5252138593Ssam	DEF_CMD("-apbridge",	0,	set80211apbridge),
5253138593Ssam	DEF_CMD_ARG("chanlist",		set80211chanlist),
5254138593Ssam	DEF_CMD_ARG("bssid",		set80211bssid),
5255138593Ssam	DEF_CMD_ARG("ap",		set80211bssid),
5256138593Ssam	DEF_CMD("scan",	0,		set80211scan),
5257138593Ssam	DEF_CMD_ARG("list",		set80211list),
5258138593Ssam	DEF_CMD_ARG2("cwmin",		set80211cwmin),
5259138593Ssam	DEF_CMD_ARG2("cwmax",		set80211cwmax),
5260138593Ssam	DEF_CMD_ARG2("aifs",		set80211aifs),
5261138593Ssam	DEF_CMD_ARG2("txoplimit",	set80211txoplimit),
5262148621Ssam	DEF_CMD_ARG("acm",		set80211acm),
5263148621Ssam	DEF_CMD_ARG("-acm",		set80211noacm),
5264148621Ssam	DEF_CMD_ARG("ack",		set80211ackpolicy),
5265148621Ssam	DEF_CMD_ARG("-ack",		set80211noackpolicy),
5266138593Ssam	DEF_CMD_ARG2("bss:cwmin",	set80211bsscwmin),
5267138593Ssam	DEF_CMD_ARG2("bss:cwmax",	set80211bsscwmax),
5268138593Ssam	DEF_CMD_ARG2("bss:aifs",	set80211bssaifs),
5269138593Ssam	DEF_CMD_ARG2("bss:txoplimit",	set80211bsstxoplimit),
5270138593Ssam	DEF_CMD_ARG("dtimperiod",	set80211dtimperiod),
5271138593Ssam	DEF_CMD_ARG("bintval",		set80211bintval),
5272138593Ssam	DEF_CMD("mac:open",	IEEE80211_MACCMD_POLICY_OPEN,	set80211maccmd),
5273138593Ssam	DEF_CMD("mac:allow",	IEEE80211_MACCMD_POLICY_ALLOW,	set80211maccmd),
5274138593Ssam	DEF_CMD("mac:deny",	IEEE80211_MACCMD_POLICY_DENY,	set80211maccmd),
5275178354Ssam	DEF_CMD("mac:radius",	IEEE80211_MACCMD_POLICY_RADIUS,	set80211maccmd),
5276138593Ssam	DEF_CMD("mac:flush",	IEEE80211_MACCMD_FLUSH,		set80211maccmd),
5277138593Ssam	DEF_CMD("mac:detach",	IEEE80211_MACCMD_DETACH,	set80211maccmd),
5278138593Ssam	DEF_CMD_ARG("mac:add",		set80211addmac),
5279138593Ssam	DEF_CMD_ARG("mac:del",		set80211delmac),
5280138593Ssam	DEF_CMD_ARG("mac:kick",		set80211kickmac),
5281147795Ssam	DEF_CMD("pureg",	1,	set80211pureg),
5282147795Ssam	DEF_CMD("-pureg",	0,	set80211pureg),
5283170531Ssam	DEF_CMD("ff",		1,	set80211fastframes),
5284170531Ssam	DEF_CMD("-ff",		0,	set80211fastframes),
5285170531Ssam	DEF_CMD("dturbo",	1,	set80211dturbo),
5286170531Ssam	DEF_CMD("-dturbo",	0,	set80211dturbo),
5287170531Ssam	DEF_CMD("bgscan",	1,	set80211bgscan),
5288170531Ssam	DEF_CMD("-bgscan",	0,	set80211bgscan),
5289170531Ssam	DEF_CMD_ARG("bgscanidle",	set80211bgscanidle),
5290170531Ssam	DEF_CMD_ARG("bgscanintvl",	set80211bgscanintvl),
5291170531Ssam	DEF_CMD_ARG("scanvalid",	set80211scanvalid),
5292227336Sadrian	DEF_CMD("quiet",        1,      set80211quiet),
5293227336Sadrian	DEF_CMD("-quiet",       0,      set80211quiet),
5294227336Sadrian	DEF_CMD_ARG("quiet_count",      set80211quietcount),
5295227336Sadrian	DEF_CMD_ARG("quiet_period",     set80211quietperiod),
5296227336Sadrian	DEF_CMD_ARG("quiet_dur",        set80211quietduration),
5297227336Sadrian	DEF_CMD_ARG("quiet_offset",     set80211quietoffset),
5298178354Ssam	DEF_CMD_ARG("roam:rssi",	set80211roamrssi),
5299178354Ssam	DEF_CMD_ARG("roam:rate",	set80211roamrate),
5300153354Ssam	DEF_CMD_ARG("mcastrate",	set80211mcastrate),
5301178354Ssam	DEF_CMD_ARG("ucastrate",	set80211ucastrate),
5302178354Ssam	DEF_CMD_ARG("mgtrate",		set80211mgtrate),
5303178354Ssam	DEF_CMD_ARG("mgmtrate",		set80211mgtrate),
5304178354Ssam	DEF_CMD_ARG("maxretry",		set80211maxretry),
5305148416Ssam	DEF_CMD_ARG("fragthreshold",	set80211fragthreshold),
5306153422Ssam	DEF_CMD("burst",	1,	set80211burst),
5307153422Ssam	DEF_CMD("-burst",	0,	set80211burst),
5308160687Ssam	DEF_CMD_ARG("bmiss",		set80211bmissthreshold),
5309160687Ssam	DEF_CMD_ARG("bmissthreshold",	set80211bmissthreshold),
5310173275Ssam	DEF_CMD("shortgi",	1,	set80211shortgi),
5311173275Ssam	DEF_CMD("-shortgi",	0,	set80211shortgi),
5312173275Ssam	DEF_CMD("ampdurx",	2,	set80211ampdu),
5313173275Ssam	DEF_CMD("-ampdurx",	-2,	set80211ampdu),
5314173275Ssam	DEF_CMD("ampdutx",	1,	set80211ampdu),
5315173275Ssam	DEF_CMD("-ampdutx",	-1,	set80211ampdu),
5316173275Ssam	DEF_CMD("ampdu",	3,	set80211ampdu),		/* NB: tx+rx */
5317173275Ssam	DEF_CMD("-ampdu",	-3,	set80211ampdu),
5318173275Ssam	DEF_CMD_ARG("ampdulimit",	set80211ampdulimit),
5319173275Ssam	DEF_CMD_ARG("ampdudensity",	set80211ampdudensity),
5320173275Ssam	DEF_CMD("amsdurx",	2,	set80211amsdu),
5321173275Ssam	DEF_CMD("-amsdurx",	-2,	set80211amsdu),
5322173275Ssam	DEF_CMD("amsdutx",	1,	set80211amsdu),
5323173275Ssam	DEF_CMD("-amsdutx",	-1,	set80211amsdu),
5324173275Ssam	DEF_CMD("amsdu",	3,	set80211amsdu),		/* NB: tx+rx */
5325173275Ssam	DEF_CMD("-amsdu",	-3,	set80211amsdu),
5326173275Ssam	DEF_CMD_ARG("amsdulimit",	set80211amsdulimit),
5327173275Ssam	DEF_CMD("puren",	1,	set80211puren),
5328173275Ssam	DEF_CMD("-puren",	0,	set80211puren),
5329170531Ssam	DEF_CMD("doth",		1,	set80211doth),
5330170531Ssam	DEF_CMD("-doth",	0,	set80211doth),
5331178354Ssam	DEF_CMD("dfs",		1,	set80211dfs),
5332178354Ssam	DEF_CMD("-dfs",		0,	set80211dfs),
5333173275Ssam	DEF_CMD("htcompat",	1,	set80211htcompat),
5334173275Ssam	DEF_CMD("-htcompat",	0,	set80211htcompat),
5335178354Ssam	DEF_CMD("dwds",		1,	set80211dwds),
5336178354Ssam	DEF_CMD("-dwds",	0,	set80211dwds),
5337173275Ssam	DEF_CMD("inact",	1,	set80211inact),
5338173275Ssam	DEF_CMD("-inact",	0,	set80211inact),
5339178354Ssam	DEF_CMD("tsn",		1,	set80211tsn),
5340178354Ssam	DEF_CMD("-tsn",		0,	set80211tsn),
5341178354Ssam	DEF_CMD_ARG("regdomain",	set80211regdomain),
5342178354Ssam	DEF_CMD_ARG("country",		set80211country),
5343178354Ssam	DEF_CMD("indoor",	'I',	set80211location),
5344178354Ssam	DEF_CMD("-indoor",	'O',	set80211location),
5345178354Ssam	DEF_CMD("outdoor",	'O',	set80211location),
5346178354Ssam	DEF_CMD("-outdoor",	'I',	set80211location),
5347178354Ssam	DEF_CMD("anywhere",	' ',	set80211location),
5348178354Ssam	DEF_CMD("ecm",		1,	set80211ecm),
5349178354Ssam	DEF_CMD("-ecm",		0,	set80211ecm),
5350178354Ssam	DEF_CMD("dotd",		1,	set80211dotd),
5351178354Ssam	DEF_CMD("-dotd",	0,	set80211dotd),
5352173275Ssam	DEF_CMD_ARG("htprotmode",	set80211htprotmode),
5353173275Ssam	DEF_CMD("ht20",		1,	set80211htconf),
5354173275Ssam	DEF_CMD("-ht20",	0,	set80211htconf),
5355173275Ssam	DEF_CMD("ht40",		3,	set80211htconf),	/* NB: 20+40 */
5356173275Ssam	DEF_CMD("-ht40",	0,	set80211htconf),
5357173275Ssam	DEF_CMD("ht",		3,	set80211htconf),	/* NB: 20+40 */
5358173275Ssam	DEF_CMD("-ht",		0,	set80211htconf),
5359183261Ssam	DEF_CMD("rifs",		1,	set80211rifs),
5360183261Ssam	DEF_CMD("-rifs",	0,	set80211rifs),
5361183261Ssam	DEF_CMD("smps",		IEEE80211_HTCAP_SMPS_ENA,	set80211smps),
5362183261Ssam	DEF_CMD("smpsdyn",	IEEE80211_HTCAP_SMPS_DYNAMIC,	set80211smps),
5363183261Ssam	DEF_CMD("-smps",	IEEE80211_HTCAP_SMPS_OFF,	set80211smps),
5364178354Ssam	/* XXX for testing */
5365178354Ssam	DEF_CMD_ARG("chanswitch",	set80211chanswitch),
5366178354Ssam
5367186904Ssam	DEF_CMD_ARG("tdmaslot",		set80211tdmaslot),
5368186904Ssam	DEF_CMD_ARG("tdmaslotcnt",	set80211tdmaslotcnt),
5369186904Ssam	DEF_CMD_ARG("tdmaslotlen",	set80211tdmaslotlen),
5370186904Ssam	DEF_CMD_ARG("tdmabintval",	set80211tdmabintval),
5371186904Ssam
5372195618Srpaulo	DEF_CMD_ARG("meshttl",		set80211meshttl),
5373195618Srpaulo	DEF_CMD("meshforward",	1,	set80211meshforward),
5374195618Srpaulo	DEF_CMD("-meshforward",	0,	set80211meshforward),
5375234893Smonthadar	DEF_CMD("meshgate",	1,	set80211meshgate),
5376234893Smonthadar	DEF_CMD("-meshgate",	0,	set80211meshgate),
5377195618Srpaulo	DEF_CMD("meshpeering",	1,	set80211meshpeering),
5378195618Srpaulo	DEF_CMD("-meshpeering",	0,	set80211meshpeering),
5379195618Srpaulo	DEF_CMD_ARG("meshmetric",	set80211meshmetric),
5380195618Srpaulo	DEF_CMD_ARG("meshpath",		set80211meshpath),
5381195618Srpaulo	DEF_CMD("meshrt:flush",	IEEE80211_MESH_RTCMD_FLUSH,	set80211meshrtcmd),
5382195618Srpaulo	DEF_CMD_ARG("meshrt:add",	set80211addmeshrt),
5383195618Srpaulo	DEF_CMD_ARG("meshrt:del",	set80211delmeshrt),
5384195618Srpaulo	DEF_CMD_ARG("hwmprootmode",	set80211hwmprootmode),
5385195618Srpaulo	DEF_CMD_ARG("hwmpmaxhops",	set80211hwmpmaxhops),
5386195618Srpaulo
5387178354Ssam	/* vap cloning support */
5388178354Ssam	DEF_CLONE_CMD_ARG("wlanaddr",	set80211clone_wlanaddr),
5389178354Ssam	DEF_CLONE_CMD_ARG("wlanbssid",	set80211clone_wlanbssid),
5390178354Ssam	DEF_CLONE_CMD_ARG("wlandev",	set80211clone_wlandev),
5391178354Ssam	DEF_CLONE_CMD_ARG("wlanmode",	set80211clone_wlanmode),
5392178354Ssam	DEF_CLONE_CMD("beacons", 1,	set80211clone_beacons),
5393178354Ssam	DEF_CLONE_CMD("-beacons", 0,	set80211clone_beacons),
5394178354Ssam	DEF_CLONE_CMD("bssid",	1,	set80211clone_bssid),
5395178354Ssam	DEF_CLONE_CMD("-bssid",	0,	set80211clone_bssid),
5396178354Ssam	DEF_CLONE_CMD("wdslegacy", 1,	set80211clone_wdslegacy),
5397178354Ssam	DEF_CLONE_CMD("-wdslegacy", 0,	set80211clone_wdslegacy),
5398138593Ssam};
5399138593Ssamstatic struct afswtch af_ieee80211 = {
5400138593Ssam	.af_name	= "af_ieee80211",
5401138593Ssam	.af_af		= AF_UNSPEC,
5402139494Ssam	.af_other_status = ieee80211_status,
5403138593Ssam};
5404138593Ssam
5405138593Ssamstatic __constructor void
5406138593Ssamieee80211_ctor(void)
5407138593Ssam{
5408138593Ssam	int i;
5409138593Ssam
5410288305Sngie	for (i = 0; i < nitems(ieee80211_cmds);  i++)
5411138593Ssam		cmd_register(&ieee80211_cmds[i]);
5412138593Ssam	af_register(&af_ieee80211);
5413189096Srpaulo	clone_setdefcallback("wlan", wlan_create);
5414138593Ssam}
5415