ifieee80211.c revision 299873
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 299873 2016-05-16 00:25:24Z truckman $
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)
1700298606Sadrian		errx(-1, "cannot set 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
1709298606Sadrianstatic void
1710298606Sadrianset80211stbc(const char *val, int d, int s, const struct afswtch *rafp)
1711298606Sadrian{
1712298606Sadrian	int stbc;
1713298606Sadrian
1714298606Sadrian	if (get80211val(s, IEEE80211_IOC_STBC, &stbc) < 0)
1715298606Sadrian		errx(-1, "cannot set STBC setting");
1716298606Sadrian	if (d < 0) {
1717298606Sadrian		d = -d;
1718298606Sadrian		stbc &= ~d;
1719298606Sadrian	} else
1720298606Sadrian		stbc |= d;
1721298606Sadrian	set80211(s, IEEE80211_IOC_STBC, stbc, 0, NULL);
1722298606Sadrian}
1723298606Sadrian
1724173275Ssamstatic
1725173275SsamDECL_CMD_FUNC(set80211ampdulimit, val, d)
1726173275Ssam{
1727173275Ssam	int v;
1728173275Ssam
1729173275Ssam	switch (atoi(val)) {
1730173275Ssam	case 8:
1731173275Ssam	case 8*1024:
1732173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_8K;
1733173275Ssam		break;
1734173275Ssam	case 16:
1735173275Ssam	case 16*1024:
1736173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_16K;
1737173275Ssam		break;
1738173275Ssam	case 32:
1739173275Ssam	case 32*1024:
1740173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_32K;
1741173275Ssam		break;
1742173275Ssam	case 64:
1743173275Ssam	case 64*1024:
1744173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_64K;
1745173275Ssam		break;
1746173275Ssam	default:
1747173275Ssam		errx(-1, "invalid A-MPDU limit %s", val);
1748173275Ssam	}
1749173275Ssam	set80211(s, IEEE80211_IOC_AMPDU_LIMIT, v, 0, NULL);
1750173275Ssam}
1751173275Ssam
1752173275Ssamstatic
1753173275SsamDECL_CMD_FUNC(set80211ampdudensity, val, d)
1754173275Ssam{
1755173275Ssam	int v;
1756173275Ssam
1757183260Ssam	if (isanyarg(val) || strcasecmp(val, "na") == 0)
1758173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1759173275Ssam	else switch ((int)(atof(val)*4)) {
1760173275Ssam	case 0:
1761173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1762173275Ssam		break;
1763173275Ssam	case 1:
1764173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_025;
1765173275Ssam		break;
1766173275Ssam	case 2:
1767173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_05;
1768173275Ssam		break;
1769173275Ssam	case 4:
1770173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_1;
1771173275Ssam		break;
1772173275Ssam	case 8:
1773173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_2;
1774173275Ssam		break;
1775173275Ssam	case 16:
1776173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_4;
1777173275Ssam		break;
1778173275Ssam	case 32:
1779173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_8;
1780173275Ssam		break;
1781173275Ssam	case 64:
1782173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_16;
1783173275Ssam		break;
1784173275Ssam	default:
1785173275Ssam		errx(-1, "invalid A-MPDU density %s", val);
1786173275Ssam	}
1787173275Ssam	set80211(s, IEEE80211_IOC_AMPDU_DENSITY, v, 0, NULL);
1788173275Ssam}
1789173275Ssam
1790173275Ssamstatic void
1791173275Ssamset80211amsdu(const char *val, int d, int s, const struct afswtch *rafp)
1792173275Ssam{
1793173275Ssam	int amsdu;
1794173275Ssam
1795173275Ssam	if (get80211val(s, IEEE80211_IOC_AMSDU, &amsdu) < 0)
1796187801Ssam		err(-1, "cannot get AMSDU setting");
1797173275Ssam	if (d < 0) {
1798173275Ssam		d = -d;
1799173275Ssam		amsdu &= ~d;
1800173275Ssam	} else
1801173275Ssam		amsdu |= d;
1802173275Ssam	set80211(s, IEEE80211_IOC_AMSDU, amsdu, 0, NULL);
1803173275Ssam}
1804173275Ssam
1805173275Ssamstatic
1806173275SsamDECL_CMD_FUNC(set80211amsdulimit, val, d)
1807173275Ssam{
1808173275Ssam	set80211(s, IEEE80211_IOC_AMSDU_LIMIT, atoi(val), 0, NULL);
1809173275Ssam}
1810173275Ssam
1811173275Ssamstatic void
1812173275Ssamset80211puren(const char *val, int d, int s, const struct afswtch *rafp)
1813173275Ssam{
1814173275Ssam	set80211(s, IEEE80211_IOC_PUREN, d, 0, NULL);
1815173275Ssam}
1816173275Ssam
1817173275Ssamstatic void
1818173275Ssamset80211htcompat(const char *val, int d, int s, const struct afswtch *rafp)
1819173275Ssam{
1820173275Ssam	set80211(s, IEEE80211_IOC_HTCOMPAT, d, 0, NULL);
1821173275Ssam}
1822173275Ssam
1823173275Ssamstatic void
1824173275Ssamset80211htconf(const char *val, int d, int s, const struct afswtch *rafp)
1825173275Ssam{
1826173275Ssam	set80211(s, IEEE80211_IOC_HTCONF, d, 0, NULL);
1827173275Ssam	htconf = d;
1828173275Ssam}
1829173275Ssam
1830173275Ssamstatic void
1831178354Ssamset80211dwds(const char *val, int d, int s, const struct afswtch *rafp)
1832178354Ssam{
1833178354Ssam	set80211(s, IEEE80211_IOC_DWDS, d, 0, NULL);
1834178354Ssam}
1835178354Ssam
1836178354Ssamstatic void
1837173275Ssamset80211inact(const char *val, int d, int s, const struct afswtch *rafp)
1838173275Ssam{
1839173275Ssam	set80211(s, IEEE80211_IOC_INACTIVITY, d, 0, NULL);
1840173275Ssam}
1841173275Ssam
1842173275Ssamstatic void
1843178354Ssamset80211tsn(const char *val, int d, int s, const struct afswtch *rafp)
1844178354Ssam{
1845178354Ssam	set80211(s, IEEE80211_IOC_TSN, d, 0, NULL);
1846178354Ssam}
1847178354Ssam
1848178354Ssamstatic void
1849178354Ssamset80211dotd(const char *val, int d, int s, const struct afswtch *rafp)
1850178354Ssam{
1851178354Ssam	set80211(s, IEEE80211_IOC_DOTD, d, 0, NULL);
1852178354Ssam}
1853178354Ssam
1854183261Ssamstatic void
1855183261Ssamset80211smps(const char *val, int d, int s, const struct afswtch *rafp)
1856183261Ssam{
1857183261Ssam	set80211(s, IEEE80211_IOC_SMPS, d, 0, NULL);
1858183261Ssam}
1859183261Ssam
1860183261Ssamstatic void
1861183261Ssamset80211rifs(const char *val, int d, int s, const struct afswtch *rafp)
1862183261Ssam{
1863183261Ssam	set80211(s, IEEE80211_IOC_RIFS, d, 0, NULL);
1864183261Ssam}
1865183261Ssam
1866186904Ssamstatic
1867186904SsamDECL_CMD_FUNC(set80211tdmaslot, val, d)
1868186904Ssam{
1869186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOT, atoi(val), 0, NULL);
1870186904Ssam}
1871186904Ssam
1872186904Ssamstatic
1873186904SsamDECL_CMD_FUNC(set80211tdmaslotcnt, val, d)
1874186904Ssam{
1875186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOTCNT, atoi(val), 0, NULL);
1876186904Ssam}
1877186904Ssam
1878186904Ssamstatic
1879186904SsamDECL_CMD_FUNC(set80211tdmaslotlen, val, d)
1880186904Ssam{
1881186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOTLEN, atoi(val), 0, NULL);
1882186904Ssam}
1883186904Ssam
1884186904Ssamstatic
1885186904SsamDECL_CMD_FUNC(set80211tdmabintval, val, d)
1886186904Ssam{
1887186904Ssam	set80211(s, IEEE80211_IOC_TDMA_BINTERVAL, atoi(val), 0, NULL);
1888186904Ssam}
1889186904Ssam
1890195618Srpaulostatic
1891195618SrpauloDECL_CMD_FUNC(set80211meshttl, val, d)
1892195618Srpaulo{
1893195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_TTL, atoi(val), 0, NULL);
1894195618Srpaulo}
1895195618Srpaulo
1896195618Srpaulostatic
1897195618SrpauloDECL_CMD_FUNC(set80211meshforward, val, d)
1898195618Srpaulo{
1899233328Sadrian	set80211(s, IEEE80211_IOC_MESH_FWRD, d, 0, NULL);
1900195618Srpaulo}
1901195618Srpaulo
1902195618Srpaulostatic
1903234893SmonthadarDECL_CMD_FUNC(set80211meshgate, val, d)
1904234893Smonthadar{
1905234893Smonthadar	set80211(s, IEEE80211_IOC_MESH_GATE, d, 0, NULL);
1906234893Smonthadar}
1907234893Smonthadar
1908234893Smonthadarstatic
1909195618SrpauloDECL_CMD_FUNC(set80211meshpeering, val, d)
1910195618Srpaulo{
1911233382Sbschmidt	set80211(s, IEEE80211_IOC_MESH_AP, d, 0, NULL);
1912195618Srpaulo}
1913195618Srpaulo
1914195618Srpaulostatic
1915195618SrpauloDECL_CMD_FUNC(set80211meshmetric, val, d)
1916195618Srpaulo{
1917195618Srpaulo	char v[12];
1918195618Srpaulo
1919195618Srpaulo	memcpy(v, val, sizeof(v));
1920195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_PR_METRIC, 0, 0, v);
1921195618Srpaulo}
1922195618Srpaulo
1923195618Srpaulostatic
1924195618SrpauloDECL_CMD_FUNC(set80211meshpath, val, d)
1925195618Srpaulo{
1926195618Srpaulo	char v[12];
1927195618Srpaulo
1928195618Srpaulo	memcpy(v, val, sizeof(v));
1929195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_PR_PATH, 0, 0, v);
1930195618Srpaulo}
1931195618Srpaulo
1932178354Ssamstatic int
1933178354Ssamregdomain_sort(const void *a, const void *b)
1934178354Ssam{
1935178354Ssam#define	CHAN_ALL \
1936178354Ssam	(IEEE80211_CHAN_ALLTURBO|IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)
1937178354Ssam	const struct ieee80211_channel *ca = a;
1938178354Ssam	const struct ieee80211_channel *cb = b;
1939178354Ssam
1940178354Ssam	return ca->ic_freq == cb->ic_freq ?
1941178354Ssam	    (ca->ic_flags & CHAN_ALL) - (cb->ic_flags & CHAN_ALL) :
1942178354Ssam	    ca->ic_freq - cb->ic_freq;
1943178354Ssam#undef CHAN_ALL
1944178354Ssam}
1945178354Ssam
1946178354Ssamstatic const struct ieee80211_channel *
1947178354Ssamchanlookup(const struct ieee80211_channel chans[], int nchans,
1948178354Ssam	int freq, int flags)
1949178354Ssam{
1950178354Ssam	int i;
1951178354Ssam
1952178354Ssam	flags &= IEEE80211_CHAN_ALLTURBO;
1953178354Ssam	for (i = 0; i < nchans; i++) {
1954178354Ssam		const struct ieee80211_channel *c = &chans[i];
1955178354Ssam		if (c->ic_freq == freq &&
1956178354Ssam		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1957178354Ssam			return c;
1958178354Ssam	}
1959178354Ssam	return NULL;
1960178354Ssam}
1961178354Ssam
1962187841Ssamstatic int
1963187841Ssamchanfind(const struct ieee80211_channel chans[], int nchans, int flags)
1964187841Ssam{
1965187841Ssam	int i;
1966187841Ssam
1967187841Ssam	for (i = 0; i < nchans; i++) {
1968187841Ssam		const struct ieee80211_channel *c = &chans[i];
1969187841Ssam		if ((c->ic_flags & flags) == flags)
1970187841Ssam			return 1;
1971187841Ssam	}
1972187841Ssam	return 0;
1973187841Ssam}
1974187841Ssam
1975188258Ssam/*
1976188258Ssam * Check channel compatibility.
1977188258Ssam */
1978188258Ssamstatic int
1979188258Ssamcheckchan(const struct ieee80211req_chaninfo *avail, int freq, int flags)
1980188258Ssam{
1981188258Ssam	flags &= ~REQ_FLAGS;
1982188258Ssam	/*
1983188258Ssam	 * Check if exact channel is in the calibration table;
1984188258Ssam	 * everything below is to deal with channels that we
1985188258Ssam	 * want to include but that are not explicitly listed.
1986188258Ssam	 */
1987188258Ssam	if (chanlookup(avail->ic_chans, avail->ic_nchans, freq, flags) != NULL)
1988188258Ssam		return 1;
1989188258Ssam	if (flags & IEEE80211_CHAN_GSM) {
1990188258Ssam		/*
1991188258Ssam		 * XXX GSM frequency mapping is handled in the kernel
1992188258Ssam		 * so we cannot find them in the calibration table;
1993188258Ssam		 * just accept the channel and the kernel will reject
1994188258Ssam		 * the channel list if it's wrong.
1995188258Ssam		 */
1996188258Ssam		return 1;
1997188258Ssam	}
1998188258Ssam	/*
1999188258Ssam	 * If this is a 1/2 or 1/4 width channel allow it if a full
2000188258Ssam	 * width channel is present for this frequency, and the device
2001188258Ssam	 * supports fractional channels on this band.  This is a hack
2002188258Ssam	 * that avoids bloating the calibration table; it may be better
2003188258Ssam	 * by per-band attributes though (we are effectively calculating
2004188258Ssam	 * this attribute by scanning the channel list ourself).
2005188258Ssam	 */
2006188258Ssam	if ((flags & (IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)) == 0)
2007188258Ssam		return 0;
2008188258Ssam	if (chanlookup(avail->ic_chans, avail->ic_nchans, freq,
2009188258Ssam	    flags &~ (IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)) == NULL)
2010188258Ssam		return 0;
2011188258Ssam	if (flags & IEEE80211_CHAN_HALF) {
2012188258Ssam		return chanfind(avail->ic_chans, avail->ic_nchans,
2013188258Ssam		    IEEE80211_CHAN_HALF |
2014188258Ssam		       (flags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ)));
2015188258Ssam	} else {
2016188258Ssam		return chanfind(avail->ic_chans, avail->ic_nchans,
2017188258Ssam		    IEEE80211_CHAN_QUARTER |
2018188258Ssam			(flags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ)));
2019188258Ssam	}
2020188258Ssam}
2021188258Ssam
2022178354Ssamstatic void
2023178354Ssamregdomain_addchans(struct ieee80211req_chaninfo *ci,
2024178354Ssam	const netband_head *bands,
2025178354Ssam	const struct ieee80211_regdomain *reg,
2026178354Ssam	uint32_t chanFlags,
2027178354Ssam	const struct ieee80211req_chaninfo *avail)
2028178354Ssam{
2029178354Ssam	const struct netband *nb;
2030178354Ssam	const struct freqband *b;
2031178354Ssam	struct ieee80211_channel *c, *prev;
2032188258Ssam	int freq, hi_adj, lo_adj, channelSep;
2033188258Ssam	uint32_t flags;
2034178354Ssam
2035188258Ssam	hi_adj = (chanFlags & IEEE80211_CHAN_HT40U) ? -20 : 0;
2036188258Ssam	lo_adj = (chanFlags & IEEE80211_CHAN_HT40D) ? 20 : 0;
2037178354Ssam	channelSep = (chanFlags & IEEE80211_CHAN_2GHZ) ? 0 : 40;
2038178354Ssam	LIST_FOREACH(nb, bands, next) {
2039178354Ssam		b = nb->band;
2040187842Ssam		if (verbose) {
2041187842Ssam			printf("%s:", __func__);
2042187842Ssam			printb(" chanFlags", chanFlags, IEEE80211_CHAN_BITS);
2043187842Ssam			printb(" bandFlags", nb->flags | b->flags,
2044187842Ssam			    IEEE80211_CHAN_BITS);
2045187842Ssam			putchar('\n');
2046187842Ssam		}
2047178354Ssam		prev = NULL;
2048188258Ssam		for (freq = b->freqStart + lo_adj;
2049188258Ssam		     freq <= b->freqEnd + hi_adj; freq += b->chanSep) {
2050187841Ssam			/*
2051188258Ssam			 * Construct flags for the new channel.  We take
2052188258Ssam			 * the attributes from the band descriptions except
2053188258Ssam			 * for HT40 which is enabled generically (i.e. +/-
2054188258Ssam			 * extension channel) in the band description and
2055188258Ssam			 * then constrained according by channel separation.
2056187841Ssam			 */
2057188258Ssam			flags = nb->flags | b->flags;
2058188258Ssam			if (flags & IEEE80211_CHAN_HT) {
2059188258Ssam				/*
2060188258Ssam				 * HT channels are generated specially; we're
2061188258Ssam				 * called to add HT20, HT40+, and HT40- chan's
2062188258Ssam				 * so we need to expand only band specs for
2063188258Ssam				 * the HT channel type being added.
2064188258Ssam				 */
2065188258Ssam				if ((chanFlags & IEEE80211_CHAN_HT20) &&
2066188258Ssam				    (flags & IEEE80211_CHAN_HT20) == 0) {
2067188258Ssam					if (verbose)
2068188258Ssam						printf("%u: skip, not an "
2069188258Ssam						    "HT20 channel\n", freq);
2070188258Ssam					continue;
2071188258Ssam				}
2072188258Ssam				if ((chanFlags & IEEE80211_CHAN_HT40) &&
2073188258Ssam				    (flags & IEEE80211_CHAN_HT40) == 0) {
2074188258Ssam					if (verbose)
2075188258Ssam						printf("%u: skip, not an "
2076188258Ssam						    "HT40 channel\n", freq);
2077188258Ssam					continue;
2078188258Ssam				}
2079188258Ssam				/* NB: HT attribute comes from caller */
2080188258Ssam				flags &= ~IEEE80211_CHAN_HT;
2081188258Ssam				flags |= chanFlags & IEEE80211_CHAN_HT;
2082188258Ssam			}
2083188258Ssam			/*
2084188258Ssam			 * Check if device can operate on this frequency.
2085188258Ssam			 */
2086188258Ssam			if (!checkchan(avail, freq, flags)) {
2087187842Ssam				if (verbose) {
2088187842Ssam					printf("%u: skip, ", freq);
2089188258Ssam					printb("flags", flags,
2090187842Ssam					    IEEE80211_CHAN_BITS);
2091187842Ssam					printf(" not available\n");
2092187842Ssam				}
2093178354Ssam				continue;
2094178354Ssam			}
2095188258Ssam			if ((flags & REQ_ECM) && !reg->ecm) {
2096178354Ssam				if (verbose)
2097188258Ssam					printf("%u: skip, ECM channel\n", freq);
2098178354Ssam				continue;
2099178354Ssam			}
2100188258Ssam			if ((flags & REQ_INDOOR) && reg->location == 'O') {
2101178354Ssam				if (verbose)
2102188258Ssam					printf("%u: skip, indoor channel\n",
2103187841Ssam					    freq);
2104178354Ssam				continue;
2105178354Ssam			}
2106178354Ssam			if ((flags & REQ_OUTDOOR) && reg->location == 'I') {
2107178354Ssam				if (verbose)
2108188258Ssam					printf("%u: skip, outdoor channel\n",
2109188258Ssam					    freq);
2110178354Ssam				continue;
2111178354Ssam			}
2112178354Ssam			if ((flags & IEEE80211_CHAN_HT40) &&
2113178354Ssam			    prev != NULL && (freq - prev->ic_freq) < channelSep) {
2114178354Ssam				if (verbose)
2115178354Ssam					printf("%u: skip, only %u channel "
2116178354Ssam					    "separation, need %d\n", freq,
2117178354Ssam					    freq - prev->ic_freq, channelSep);
2118178354Ssam				continue;
2119178354Ssam			}
2120178354Ssam			if (ci->ic_nchans == IEEE80211_CHAN_MAX) {
2121178354Ssam				if (verbose)
2122187842Ssam					printf("%u: skip, channel table full\n",
2123187842Ssam					    freq);
2124178354Ssam				break;
2125178354Ssam			}
2126178354Ssam			c = &ci->ic_chans[ci->ic_nchans++];
2127187801Ssam			memset(c, 0, sizeof(*c));
2128178354Ssam			c->ic_freq = freq;
2129188258Ssam			c->ic_flags = flags;
2130178354Ssam			if (c->ic_flags & IEEE80211_CHAN_DFS)
2131178354Ssam				c->ic_maxregpower = nb->maxPowerDFS;
2132178354Ssam			else
2133178354Ssam				c->ic_maxregpower = nb->maxPower;
2134187842Ssam			if (verbose) {
2135187842Ssam				printf("[%3d] add freq %u ",
2136187842Ssam				    ci->ic_nchans-1, c->ic_freq);
2137187842Ssam				printb("flags", c->ic_flags, IEEE80211_CHAN_BITS);
2138187842Ssam				printf(" power %u\n", c->ic_maxregpower);
2139187842Ssam			}
2140178354Ssam			/* NB: kernel fills in other fields */
2141178354Ssam			prev = c;
2142178354Ssam		}
2143178354Ssam	}
2144178354Ssam}
2145178354Ssam
2146178354Ssamstatic void
2147178354Ssamregdomain_makechannels(
2148178354Ssam	struct ieee80211_regdomain_req *req,
2149178354Ssam	const struct ieee80211_devcaps_req *dc)
2150178354Ssam{
2151178354Ssam	struct regdata *rdp = getregdata();
2152178354Ssam	const struct country *cc;
2153178354Ssam	const struct ieee80211_regdomain *reg = &req->rd;
2154178354Ssam	struct ieee80211req_chaninfo *ci = &req->chaninfo;
2155178354Ssam	const struct regdomain *rd;
2156178354Ssam
2157178354Ssam	/*
2158178354Ssam	 * Locate construction table for new channel list.  We treat
2159178354Ssam	 * the regdomain/SKU as definitive so a country can be in
2160178354Ssam	 * multiple with different properties (e.g. US in FCC+FCC3).
2161178354Ssam	 * If no regdomain is specified then we fallback on the country
2162178354Ssam	 * code to find the associated regdomain since countries always
2163178354Ssam	 * belong to at least one regdomain.
2164178354Ssam	 */
2165178354Ssam	if (reg->regdomain == 0) {
2166178354Ssam		cc = lib80211_country_findbycc(rdp, reg->country);
2167178354Ssam		if (cc == NULL)
2168178354Ssam			errx(1, "internal error, country %d not found",
2169178354Ssam			    reg->country);
2170178354Ssam		rd = cc->rd;
2171178354Ssam	} else
2172178354Ssam		rd = lib80211_regdomain_findbysku(rdp, reg->regdomain);
2173178354Ssam	if (rd == NULL)
2174178354Ssam		errx(1, "internal error, regdomain %d not found",
2175178354Ssam			    reg->regdomain);
2176178354Ssam	if (rd->sku != SKU_DEBUG) {
2177187801Ssam		/*
2178187801Ssam		 * regdomain_addchans incrememnts the channel count for
2179187801Ssam		 * each channel it adds so initialize ic_nchans to zero.
2180187801Ssam		 * Note that we know we have enough space to hold all possible
2181187801Ssam		 * channels because the devcaps list size was used to
2182187801Ssam		 * allocate our request.
2183187801Ssam		 */
2184187801Ssam		ci->ic_nchans = 0;
2185178354Ssam		if (!LIST_EMPTY(&rd->bands_11b))
2186178354Ssam			regdomain_addchans(ci, &rd->bands_11b, reg,
2187178354Ssam			    IEEE80211_CHAN_B, &dc->dc_chaninfo);
2188187841Ssam		if (!LIST_EMPTY(&rd->bands_11g))
2189178354Ssam			regdomain_addchans(ci, &rd->bands_11g, reg,
2190178354Ssam			    IEEE80211_CHAN_G, &dc->dc_chaninfo);
2191187841Ssam		if (!LIST_EMPTY(&rd->bands_11a))
2192178354Ssam			regdomain_addchans(ci, &rd->bands_11a, reg,
2193178354Ssam			    IEEE80211_CHAN_A, &dc->dc_chaninfo);
2194188258Ssam		if (!LIST_EMPTY(&rd->bands_11na) && dc->dc_htcaps != 0) {
2195178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
2196178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT20,
2197178354Ssam			    &dc->dc_chaninfo);
2198188258Ssam			if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2199188258Ssam				regdomain_addchans(ci, &rd->bands_11na, reg,
2200188258Ssam				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U,
2201188258Ssam				    &dc->dc_chaninfo);
2202188258Ssam				regdomain_addchans(ci, &rd->bands_11na, reg,
2203188258Ssam				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D,
2204188258Ssam				    &dc->dc_chaninfo);
2205188258Ssam			}
2206178354Ssam		}
2207188258Ssam		if (!LIST_EMPTY(&rd->bands_11ng) && dc->dc_htcaps != 0) {
2208178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
2209178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT20,
2210178354Ssam			    &dc->dc_chaninfo);
2211188258Ssam			if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2212188258Ssam				regdomain_addchans(ci, &rd->bands_11ng, reg,
2213188258Ssam				    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U,
2214188258Ssam				    &dc->dc_chaninfo);
2215188258Ssam				regdomain_addchans(ci, &rd->bands_11ng, reg,
2216188258Ssam				    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D,
2217188258Ssam				    &dc->dc_chaninfo);
2218188258Ssam			}
2219178354Ssam		}
2220178354Ssam		qsort(ci->ic_chans, ci->ic_nchans, sizeof(ci->ic_chans[0]),
2221178354Ssam		    regdomain_sort);
2222178354Ssam	} else
2223187801Ssam		memcpy(ci, &dc->dc_chaninfo,
2224187801Ssam		    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
2225178354Ssam}
2226178354Ssam
2227178354Ssamstatic void
2228178354Ssamlist_countries(void)
2229178354Ssam{
2230178354Ssam	struct regdata *rdp = getregdata();
2231178354Ssam	const struct country *cp;
2232178354Ssam	const struct regdomain *dp;
2233178354Ssam	int i;
2234178354Ssam
2235178354Ssam	i = 0;
2236178354Ssam	printf("\nCountry codes:\n");
2237178354Ssam	LIST_FOREACH(cp, &rdp->countries, next) {
2238178354Ssam		printf("%2s %-15.15s%s", cp->isoname,
2239178354Ssam		    cp->name, ((i+1)%4) == 0 ? "\n" : " ");
2240178354Ssam		i++;
2241178354Ssam	}
2242178354Ssam	i = 0;
2243178354Ssam	printf("\nRegulatory domains:\n");
2244178354Ssam	LIST_FOREACH(dp, &rdp->domains, next) {
2245178354Ssam		printf("%-15.15s%s", dp->name, ((i+1)%4) == 0 ? "\n" : " ");
2246178354Ssam		i++;
2247178354Ssam	}
2248178354Ssam	printf("\n");
2249178354Ssam}
2250178354Ssam
2251178354Ssamstatic void
2252178354Ssamdefaultcountry(const struct regdomain *rd)
2253178354Ssam{
2254178354Ssam	struct regdata *rdp = getregdata();
2255178354Ssam	const struct country *cc;
2256178354Ssam
2257178354Ssam	cc = lib80211_country_findbycc(rdp, rd->cc->code);
2258178354Ssam	if (cc == NULL)
2259178354Ssam		errx(1, "internal error, ISO country code %d not "
2260178354Ssam		    "defined for regdomain %s", rd->cc->code, rd->name);
2261178354Ssam	regdomain.country = cc->code;
2262178354Ssam	regdomain.isocc[0] = cc->isoname[0];
2263178354Ssam	regdomain.isocc[1] = cc->isoname[1];
2264178354Ssam}
2265178354Ssam
2266178354Ssamstatic
2267178354SsamDECL_CMD_FUNC(set80211regdomain, val, d)
2268178354Ssam{
2269178354Ssam	struct regdata *rdp = getregdata();
2270178354Ssam	const struct regdomain *rd;
2271178354Ssam
2272178354Ssam	rd = lib80211_regdomain_findbyname(rdp, val);
2273178354Ssam	if (rd == NULL) {
2274186104Ssam		char *eptr;
2275186104Ssam		long sku = strtol(val, &eptr, 0);
2276186104Ssam
2277186104Ssam		if (eptr != val)
2278186104Ssam			rd = lib80211_regdomain_findbysku(rdp, sku);
2279186104Ssam		if (eptr == val || rd == NULL)
2280178354Ssam			errx(1, "unknown regdomain %s", val);
2281178354Ssam	}
2282178354Ssam	getregdomain(s);
2283178354Ssam	regdomain.regdomain = rd->sku;
2284178354Ssam	if (regdomain.country == 0 && rd->cc != NULL) {
2285178354Ssam		/*
2286178354Ssam		 * No country code setup and there's a default
2287178354Ssam		 * one for this regdomain fill it in.
2288178354Ssam		 */
2289178354Ssam		defaultcountry(rd);
2290178354Ssam	}
2291178354Ssam	callback_register(setregdomain_cb, &regdomain);
2292178354Ssam}
2293178354Ssam
2294178354Ssamstatic
2295178354SsamDECL_CMD_FUNC(set80211country, val, d)
2296178354Ssam{
2297178354Ssam	struct regdata *rdp = getregdata();
2298178354Ssam	const struct country *cc;
2299178354Ssam
2300178354Ssam	cc = lib80211_country_findbyname(rdp, val);
2301178354Ssam	if (cc == NULL) {
2302186103Ssam		char *eptr;
2303186103Ssam		long code = strtol(val, &eptr, 0);
2304186103Ssam
2305186103Ssam		if (eptr != val)
2306186103Ssam			cc = lib80211_country_findbycc(rdp, code);
2307186103Ssam		if (eptr == val || cc == NULL)
2308178354Ssam			errx(1, "unknown ISO country code %s", val);
2309178354Ssam	}
2310178354Ssam	getregdomain(s);
2311178354Ssam	regdomain.regdomain = cc->rd->sku;
2312178354Ssam	regdomain.country = cc->code;
2313178354Ssam	regdomain.isocc[0] = cc->isoname[0];
2314178354Ssam	regdomain.isocc[1] = cc->isoname[1];
2315178354Ssam	callback_register(setregdomain_cb, &regdomain);
2316178354Ssam}
2317178354Ssam
2318178354Ssamstatic void
2319178354Ssamset80211location(const char *val, int d, int s, const struct afswtch *rafp)
2320178354Ssam{
2321178354Ssam	getregdomain(s);
2322178354Ssam	regdomain.location = d;
2323178354Ssam	callback_register(setregdomain_cb, &regdomain);
2324178354Ssam}
2325178354Ssam
2326178354Ssamstatic void
2327178354Ssamset80211ecm(const char *val, int d, int s, const struct afswtch *rafp)
2328178354Ssam{
2329178354Ssam	getregdomain(s);
2330178354Ssam	regdomain.ecm = d;
2331178354Ssam	callback_register(setregdomain_cb, &regdomain);
2332178354Ssam}
2333178354Ssam
2334178354Ssamstatic void
2335173275SsamLINE_INIT(char c)
2336173275Ssam{
2337173275Ssam	spacer = c;
2338173275Ssam	if (c == '\t')
2339173275Ssam		col = 8;
2340173275Ssam	else
2341173275Ssam		col = 1;
2342173275Ssam}
2343173275Ssam
2344173275Ssamstatic void
2345173275SsamLINE_BREAK(void)
2346173275Ssam{
2347173275Ssam	if (spacer != '\t') {
2348173275Ssam		printf("\n");
2349173275Ssam		spacer = '\t';
2350173275Ssam	}
2351173275Ssam	col = 8;		/* 8-col tab */
2352173275Ssam}
2353173275Ssam
2354173275Ssamstatic void
2355173275SsamLINE_CHECK(const char *fmt, ...)
2356173275Ssam{
2357173275Ssam	char buf[80];
2358173275Ssam	va_list ap;
2359173275Ssam	int n;
2360173275Ssam
2361173275Ssam	va_start(ap, fmt);
2362173275Ssam	n = vsnprintf(buf+1, sizeof(buf)-1, fmt, ap);
2363173275Ssam	va_end(ap);
2364173275Ssam	col += 1+n;
2365173275Ssam	if (col > MAXCOL) {
2366173275Ssam		LINE_BREAK();
2367173275Ssam		col += n;
2368173275Ssam	}
2369173275Ssam	buf[0] = spacer;
2370173275Ssam	printf("%s", buf);
2371173275Ssam	spacer = ' ';
2372173275Ssam}
2373173275Ssam
2374138593Ssamstatic int
2375170531Ssamgetmaxrate(const uint8_t rates[15], uint8_t nrates)
2376138593Ssam{
2377138593Ssam	int i, maxrate = -1;
2378138593Ssam
2379138593Ssam	for (i = 0; i < nrates; i++) {
2380138593Ssam		int rate = rates[i] & IEEE80211_RATE_VAL;
2381138593Ssam		if (rate > maxrate)
2382138593Ssam			maxrate = rate;
2383138593Ssam	}
2384138593Ssam	return maxrate / 2;
2385138593Ssam}
2386138593Ssam
2387138593Ssamstatic const char *
2388138593Ssamgetcaps(int capinfo)
2389138593Ssam{
2390138593Ssam	static char capstring[32];
2391138593Ssam	char *cp = capstring;
2392138593Ssam
2393138593Ssam	if (capinfo & IEEE80211_CAPINFO_ESS)
2394138593Ssam		*cp++ = 'E';
2395138593Ssam	if (capinfo & IEEE80211_CAPINFO_IBSS)
2396138593Ssam		*cp++ = 'I';
2397138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
2398138593Ssam		*cp++ = 'c';
2399138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
2400138593Ssam		*cp++ = 'C';
2401138593Ssam	if (capinfo & IEEE80211_CAPINFO_PRIVACY)
2402138593Ssam		*cp++ = 'P';
2403138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
2404138593Ssam		*cp++ = 'S';
2405138593Ssam	if (capinfo & IEEE80211_CAPINFO_PBCC)
2406138593Ssam		*cp++ = 'B';
2407138593Ssam	if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
2408138593Ssam		*cp++ = 'A';
2409138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
2410138593Ssam		*cp++ = 's';
2411138593Ssam	if (capinfo & IEEE80211_CAPINFO_RSN)
2412138593Ssam		*cp++ = 'R';
2413138593Ssam	if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
2414138593Ssam		*cp++ = 'D';
2415138593Ssam	*cp = '\0';
2416138593Ssam	return capstring;
2417138593Ssam}
2418138593Ssam
2419159885Ssamstatic const char *
2420159885Ssamgetflags(int flags)
2421159885Ssam{
2422159885Ssam	static char flagstring[32];
2423159885Ssam	char *cp = flagstring;
2424159885Ssam
2425159885Ssam	if (flags & IEEE80211_NODE_AUTH)
2426159885Ssam		*cp++ = 'A';
2427159885Ssam	if (flags & IEEE80211_NODE_QOS)
2428159885Ssam		*cp++ = 'Q';
2429159885Ssam	if (flags & IEEE80211_NODE_ERP)
2430159885Ssam		*cp++ = 'E';
2431159885Ssam	if (flags & IEEE80211_NODE_PWR_MGT)
2432159885Ssam		*cp++ = 'P';
2433173275Ssam	if (flags & IEEE80211_NODE_HT) {
2434170531Ssam		*cp++ = 'H';
2435173275Ssam		if (flags & IEEE80211_NODE_HTCOMPAT)
2436173275Ssam			*cp++ = '+';
2437173275Ssam	}
2438173275Ssam	if (flags & IEEE80211_NODE_WPS)
2439173275Ssam		*cp++ = 'W';
2440173275Ssam	if (flags & IEEE80211_NODE_TSN)
2441183261Ssam		*cp++ = 'N';
2442183261Ssam	if (flags & IEEE80211_NODE_AMPDU_TX)
2443173275Ssam		*cp++ = 'T';
2444183261Ssam	if (flags & IEEE80211_NODE_AMPDU_RX)
2445183261Ssam		*cp++ = 'R';
2446183261Ssam	if (flags & IEEE80211_NODE_MIMO_PS) {
2447183261Ssam		*cp++ = 'M';
2448183261Ssam		if (flags & IEEE80211_NODE_MIMO_RTS)
2449183261Ssam			*cp++ = '+';
2450183261Ssam	}
2451183261Ssam	if (flags & IEEE80211_NODE_RIFS)
2452183261Ssam		*cp++ = 'I';
2453194136Ssam	if (flags & IEEE80211_NODE_SGI40) {
2454194136Ssam		*cp++ = 'S';
2455194136Ssam		if (flags & IEEE80211_NODE_SGI20)
2456194136Ssam			*cp++ = '+';
2457194136Ssam	} else if (flags & IEEE80211_NODE_SGI20)
2458194136Ssam		*cp++ = 's';
2459194136Ssam	if (flags & IEEE80211_NODE_AMSDU_TX)
2460194136Ssam		*cp++ = 't';
2461194136Ssam	if (flags & IEEE80211_NODE_AMSDU_RX)
2462194136Ssam		*cp++ = 'r';
2463159885Ssam	*cp = '\0';
2464159885Ssam	return flagstring;
2465159885Ssam}
2466159885Ssam
2467138593Ssamstatic void
2468138593Ssamprintie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
2469138593Ssam{
2470138593Ssam	printf("%s", tag);
2471138593Ssam	if (verbose) {
2472138593Ssam		maxlen -= strlen(tag)+2;
2473138593Ssam		if (2*ielen > maxlen)
2474138593Ssam			maxlen--;
2475138593Ssam		printf("<");
2476138593Ssam		for (; ielen > 0; ie++, ielen--) {
2477138593Ssam			if (maxlen-- <= 0)
2478138593Ssam				break;
2479138593Ssam			printf("%02x", *ie);
2480138593Ssam		}
2481138593Ssam		if (ielen != 0)
2482138593Ssam			printf("-");
2483138593Ssam		printf(">");
2484138593Ssam	}
2485138593Ssam}
2486138593Ssam
2487170531Ssam#define LE_READ_2(p)					\
2488170531Ssam	((u_int16_t)					\
2489170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2490170531Ssam	  (((const u_int8_t *)(p))[1] <<  8)))
2491170531Ssam#define LE_READ_4(p)					\
2492170531Ssam	((u_int32_t)					\
2493170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2494170531Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
2495170531Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
2496170531Ssam	  (((const u_int8_t *)(p))[3] << 24)))
2497170531Ssam
2498138593Ssam/*
2499170531Ssam * NB: The decoding routines assume a properly formatted ie
2500170531Ssam *     which should be safe as the kernel only retains them
2501170531Ssam *     if they parse ok.
2502170531Ssam */
2503170531Ssam
2504170531Ssamstatic void
2505173275Ssamprintwmeparam(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2506170531Ssam{
2507170531Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
2508170531Ssam	static const char *acnames[] = { "BE", "BK", "VO", "VI" };
2509173275Ssam	const struct ieee80211_wme_param *wme =
2510173275Ssam	    (const struct ieee80211_wme_param *) ie;
2511170531Ssam	int i;
2512170531Ssam
2513170531Ssam	printf("%s", tag);
2514173275Ssam	if (!verbose)
2515173275Ssam		return;
2516173275Ssam	printf("<qosinfo 0x%x", wme->param_qosInfo);
2517173275Ssam	ie += offsetof(struct ieee80211_wme_param, params_acParams);
2518173275Ssam	for (i = 0; i < WME_NUM_AC; i++) {
2519173275Ssam		const struct ieee80211_wme_acparams *ac =
2520173275Ssam		    &wme->params_acParams[i];
2521173275Ssam
2522173275Ssam		printf(" %s[%saifsn %u cwmin %u cwmax %u txop %u]"
2523173275Ssam			, acnames[i]
2524173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_ACM) ? "acm " : ""
2525173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_AIFSN)
2526173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMIN)
2527173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMAX)
2528173275Ssam			, LE_READ_2(&ac->acp_txop)
2529173275Ssam		);
2530170531Ssam	}
2531173275Ssam	printf(">");
2532170531Ssam#undef MS
2533170531Ssam}
2534170531Ssam
2535170531Ssamstatic void
2536173275Ssamprintwmeinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2537173275Ssam{
2538173275Ssam	printf("%s", tag);
2539173275Ssam	if (verbose) {
2540173275Ssam		const struct ieee80211_wme_info *wme =
2541173275Ssam		    (const struct ieee80211_wme_info *) ie;
2542173275Ssam		printf("<version 0x%x info 0x%x>",
2543173275Ssam		    wme->wme_version, wme->wme_info);
2544173275Ssam	}
2545173275Ssam}
2546173275Ssam
2547173275Ssamstatic void
2548297007Sadrianprintvhtcap(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2549297007Sadrian{
2550297007Sadrian	printf("%s", tag);
2551297007Sadrian	if (verbose) {
2552297007Sadrian		const struct ieee80211_ie_vhtcap *vhtcap =
2553297007Sadrian		    (const struct ieee80211_ie_vhtcap *) ie;
2554297007Sadrian		uint32_t vhtcap_info = LE_READ_4(&vhtcap->vht_cap_info);
2555297007Sadrian
2556297007Sadrian		printf("<cap 0x%08x", vhtcap_info);
2557297007Sadrian		printf(" rx_mcs_map 0x%x",
2558297007Sadrian		    LE_READ_2(&vhtcap->supp_mcs.rx_mcs_map));
2559297007Sadrian		printf(" rx_highest %d",
2560297007Sadrian		    LE_READ_2(&vhtcap->supp_mcs.rx_highest) & 0x1fff);
2561297007Sadrian		printf(" tx_mcs_map 0x%x",
2562297007Sadrian		    LE_READ_2(&vhtcap->supp_mcs.tx_mcs_map));
2563297007Sadrian		printf(" tx_highest %d",
2564297007Sadrian		    LE_READ_2(&vhtcap->supp_mcs.tx_highest) & 0x1fff);
2565297007Sadrian
2566297007Sadrian		printf(">");
2567297007Sadrian	}
2568297007Sadrian}
2569297007Sadrian
2570297007Sadrianstatic void
2571297007Sadrianprintvhtinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2572297007Sadrian{
2573297007Sadrian	printf("%s", tag);
2574297007Sadrian	if (verbose) {
2575297007Sadrian		const struct ieee80211_ie_vht_operation *vhtinfo =
2576297007Sadrian		    (const struct ieee80211_ie_vht_operation *) ie;
2577297007Sadrian
2578297007Sadrian		printf("<chw %d freq1_idx %d freq2_idx %d basic_mcs_set 0x%04x>",
2579297007Sadrian		    vhtinfo->chan_width,
2580297007Sadrian		    vhtinfo->center_freq_seg1_idx,
2581297007Sadrian		    vhtinfo->center_freq_seg2_idx,
2582297007Sadrian		    LE_READ_2(&vhtinfo->basic_mcs_set));
2583297007Sadrian	}
2584297007Sadrian}
2585297007Sadrian
2586297007Sadrianstatic void
2587298252Sadrianprintvhtpwrenv(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2588298252Sadrian{
2589298252Sadrian	printf("%s", tag);
2590298252Sadrian	static const char *txpwrmap[] = {
2591298252Sadrian		"20",
2592298252Sadrian		"40",
2593298252Sadrian		"80",
2594298252Sadrian		"160",
2595298252Sadrian	};
2596298252Sadrian	if (verbose) {
2597298252Sadrian		const struct ieee80211_ie_vht_txpwrenv *vhtpwr =
2598298252Sadrian		    (const struct ieee80211_ie_vht_txpwrenv *) ie;
2599298252Sadrian		int i, n;
2600298252Sadrian		const char *sep = "";
2601298252Sadrian
2602298252Sadrian		/* Get count; trim at ielen */
2603298252Sadrian		n = (vhtpwr->tx_info &
2604298252Sadrian		    IEEE80211_VHT_TXPWRENV_INFO_COUNT_MASK) + 1;
2605298252Sadrian		/* Trim at ielen */
2606298252Sadrian		if (n > ielen - 3)
2607298252Sadrian			n = ielen - 3;
2608298252Sadrian		printf("<tx_info 0x%02x pwr:[", vhtpwr->tx_info);
2609298252Sadrian		for (i = 0; i < n; i++) {
2610298252Sadrian			printf("%s%s:%.2f", sep, txpwrmap[i],
2611298252Sadrian			    ((float) ((int8_t) ie[i+3])) / 2.0);
2612298252Sadrian			sep = " ";
2613298252Sadrian		}
2614298252Sadrian
2615298252Sadrian		printf("]>");
2616298252Sadrian	}
2617298252Sadrian}
2618298252Sadrian
2619298252Sadrianstatic void
2620173275Ssamprinthtcap(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2621173275Ssam{
2622173275Ssam	printf("%s", tag);
2623173275Ssam	if (verbose) {
2624173275Ssam		const struct ieee80211_ie_htcap *htcap =
2625173275Ssam		    (const struct ieee80211_ie_htcap *) ie;
2626173275Ssam		const char *sep;
2627173275Ssam		int i, j;
2628173275Ssam
2629173275Ssam		printf("<cap 0x%x param 0x%x",
2630173275Ssam		    LE_READ_2(&htcap->hc_cap), htcap->hc_param);
2631173275Ssam		printf(" mcsset[");
2632173275Ssam		sep = "";
2633173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2634173275Ssam			if (isset(htcap->hc_mcsset, i)) {
2635173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2636173275Ssam					if (isclr(htcap->hc_mcsset, j))
2637173275Ssam						break;
2638173275Ssam				j--;
2639173275Ssam				if (i == j)
2640173275Ssam					printf("%s%u", sep, i);
2641173275Ssam				else
2642173275Ssam					printf("%s%u-%u", sep, i, j);
2643173275Ssam				i += j-i;
2644173275Ssam				sep = ",";
2645173275Ssam			}
2646173275Ssam		printf("] extcap 0x%x txbf 0x%x antenna 0x%x>",
2647173275Ssam		    LE_READ_2(&htcap->hc_extcap),
2648173275Ssam		    LE_READ_4(&htcap->hc_txbf),
2649173275Ssam		    htcap->hc_antenna);
2650173275Ssam	}
2651173275Ssam}
2652173275Ssam
2653173275Ssamstatic void
2654173275Ssamprinthtinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2655173275Ssam{
2656173275Ssam	printf("%s", tag);
2657173275Ssam	if (verbose) {
2658173275Ssam		const struct ieee80211_ie_htinfo *htinfo =
2659173275Ssam		    (const struct ieee80211_ie_htinfo *) ie;
2660173275Ssam		const char *sep;
2661173275Ssam		int i, j;
2662173275Ssam
2663173275Ssam		printf("<ctl %u, %x,%x,%x,%x", htinfo->hi_ctrlchannel,
2664173275Ssam		    htinfo->hi_byte1, htinfo->hi_byte2, htinfo->hi_byte3,
2665173275Ssam		    LE_READ_2(&htinfo->hi_byte45));
2666173275Ssam		printf(" basicmcs[");
2667173275Ssam		sep = "";
2668173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2669173275Ssam			if (isset(htinfo->hi_basicmcsset, i)) {
2670173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2671173275Ssam					if (isclr(htinfo->hi_basicmcsset, j))
2672173275Ssam						break;
2673173275Ssam				j--;
2674173275Ssam				if (i == j)
2675173275Ssam					printf("%s%u", sep, i);
2676173275Ssam				else
2677173275Ssam					printf("%s%u-%u", sep, i, j);
2678173275Ssam				i += j-i;
2679173275Ssam				sep = ",";
2680173275Ssam			}
2681173275Ssam		printf("]>");
2682173275Ssam	}
2683173275Ssam}
2684173275Ssam
2685173275Ssamstatic void
2686170531Ssamprintathie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2687170531Ssam{
2688170531Ssam
2689170531Ssam	printf("%s", tag);
2690170531Ssam	if (verbose) {
2691170531Ssam		const struct ieee80211_ath_ie *ath =
2692170531Ssam			(const struct ieee80211_ath_ie *)ie;
2693170531Ssam
2694170531Ssam		printf("<");
2695170531Ssam		if (ath->ath_capability & ATHEROS_CAP_TURBO_PRIME)
2696170531Ssam			printf("DTURBO,");
2697170531Ssam		if (ath->ath_capability & ATHEROS_CAP_COMPRESSION)
2698170531Ssam			printf("COMP,");
2699170531Ssam		if (ath->ath_capability & ATHEROS_CAP_FAST_FRAME)
2700170531Ssam			printf("FF,");
2701170531Ssam		if (ath->ath_capability & ATHEROS_CAP_XR)
2702170531Ssam			printf("XR,");
2703170531Ssam		if (ath->ath_capability & ATHEROS_CAP_AR)
2704170531Ssam			printf("AR,");
2705170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BURST)
2706170531Ssam			printf("BURST,");
2707170531Ssam		if (ath->ath_capability & ATHEROS_CAP_WME)
2708170531Ssam			printf("WME,");
2709170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BOOST)
2710170531Ssam			printf("BOOST,");
2711170531Ssam		printf("0x%x>", LE_READ_2(ath->ath_defkeyix));
2712170531Ssam	}
2713170531Ssam}
2714170531Ssam
2715195618Srpaulo
2716195618Srpaulostatic void
2717195618Srpauloprintmeshconf(const char *tag, const uint8_t *ie, size_t ielen, int maxlen)
2718195618Srpaulo{
2719195618Srpaulo#define MATCHOUI(field, oui, string)					\
2720195618Srpaulodo {									\
2721195618Srpaulo	if (memcmp(field, oui, 4) == 0)					\
2722195618Srpaulo		printf("%s", string);					\
2723195618Srpaulo} while (0)
2724195618Srpaulo
2725195618Srpaulo	printf("%s", tag);
2726195618Srpaulo	if (verbose) {
2727195618Srpaulo		const struct ieee80211_meshconf_ie *mconf =
2728195618Srpaulo			(const struct ieee80211_meshconf_ie *)ie;
2729197980Srpaulo		printf("<PATH:");
2730197980Srpaulo		if (mconf->conf_pselid == IEEE80211_MESHCONF_PATH_HWMP)
2731197980Srpaulo			printf("HWMP");
2732197980Srpaulo		else
2733197980Srpaulo			printf("UNKNOWN");
2734195618Srpaulo		printf(" LINK:");
2735197980Srpaulo		if (mconf->conf_pmetid == IEEE80211_MESHCONF_METRIC_AIRTIME)
2736197980Srpaulo			printf("AIRTIME");
2737197980Srpaulo		else
2738197980Srpaulo			printf("UNKNOWN");
2739195618Srpaulo		printf(" CONGESTION:");
2740197980Srpaulo		if (mconf->conf_ccid == IEEE80211_MESHCONF_CC_DISABLED)
2741197980Srpaulo			printf("DISABLED");
2742197980Srpaulo		else
2743197980Srpaulo			printf("UNKNOWN");
2744195618Srpaulo		printf(" SYNC:");
2745197980Srpaulo		if (mconf->conf_syncid == IEEE80211_MESHCONF_SYNC_NEIGHOFF)
2746197980Srpaulo			printf("NEIGHOFF");
2747197980Srpaulo		else
2748197980Srpaulo			printf("UNKNOWN");
2749195618Srpaulo		printf(" AUTH:");
2750197980Srpaulo		if (mconf->conf_authid == IEEE80211_MESHCONF_AUTH_DISABLED)
2751197980Srpaulo			printf("DISABLED");
2752197980Srpaulo		else
2753197980Srpaulo			printf("UNKNOWN");
2754195618Srpaulo		printf(" FORM:0x%x CAPS:0x%x>", mconf->conf_form,
2755195618Srpaulo		    mconf->conf_cap);
2756195618Srpaulo	}
2757195618Srpaulo#undef MATCHOUI
2758195618Srpaulo}
2759195618Srpaulo
2760297007Sadrianstatic void
2761297007Sadrianprintbssload(const char *tag, const uint8_t *ie, size_t ielen, int maxlen)
2762297007Sadrian{
2763297007Sadrian	printf("%s", tag);
2764297007Sadrian	if (verbose) {
2765297007Sadrian		const struct ieee80211_bss_load_ie *bssload =
2766297007Sadrian		    (const struct ieee80211_bss_load_ie *) ie;
2767297007Sadrian		printf("<sta count %d, chan load %d, aac %d>",
2768297007Sadrian		    LE_READ_2(&bssload->sta_count),
2769297007Sadrian		    bssload->chan_load,
2770297007Sadrian		    bssload->aac);
2771297007Sadrian	}
2772297007Sadrian}
2773297007Sadrian
2774297010Sadrianstatic void
2775297010Sadrianprintapchanrep(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2776297010Sadrian{
2777297010Sadrian	printf("%s", tag);
2778297010Sadrian	if (verbose) {
2779297010Sadrian		const struct ieee80211_ap_chan_report_ie *ap =
2780297010Sadrian		    (const struct ieee80211_ap_chan_report_ie *) ie;
2781297010Sadrian		const char *sep = "";
2782297010Sadrian		int i;
2783297010Sadrian
2784297010Sadrian		printf("<class %u, chan:[", ap->i_class);
2785297010Sadrian
2786297010Sadrian		for (i = 3; i < ielen; i++) {
2787297010Sadrian			printf("%s%u", sep, ie[i]);
2788297010Sadrian			sep = ",";
2789297010Sadrian		}
2790297010Sadrian		printf("]>");
2791297010Sadrian	}
2792297010Sadrian}
2793297010Sadrian
2794170531Ssamstatic const char *
2795170531Ssamwpa_cipher(const u_int8_t *sel)
2796170531Ssam{
2797170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2798170531Ssam	u_int32_t w = LE_READ_4(sel);
2799170531Ssam
2800170531Ssam	switch (w) {
2801170531Ssam	case WPA_SEL(WPA_CSE_NULL):
2802170531Ssam		return "NONE";
2803170531Ssam	case WPA_SEL(WPA_CSE_WEP40):
2804170531Ssam		return "WEP40";
2805170531Ssam	case WPA_SEL(WPA_CSE_WEP104):
2806170531Ssam		return "WEP104";
2807170531Ssam	case WPA_SEL(WPA_CSE_TKIP):
2808170531Ssam		return "TKIP";
2809170531Ssam	case WPA_SEL(WPA_CSE_CCMP):
2810170531Ssam		return "AES-CCMP";
2811170531Ssam	}
2812170531Ssam	return "?";		/* NB: so 1<< is discarded */
2813170531Ssam#undef WPA_SEL
2814170531Ssam}
2815170531Ssam
2816170531Ssamstatic const char *
2817170531Ssamwpa_keymgmt(const u_int8_t *sel)
2818170531Ssam{
2819170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2820170531Ssam	u_int32_t w = LE_READ_4(sel);
2821170531Ssam
2822170531Ssam	switch (w) {
2823170531Ssam	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
2824170531Ssam		return "8021X-UNSPEC";
2825170531Ssam	case WPA_SEL(WPA_ASE_8021X_PSK):
2826170531Ssam		return "8021X-PSK";
2827170531Ssam	case WPA_SEL(WPA_ASE_NONE):
2828170531Ssam		return "NONE";
2829170531Ssam	}
2830170531Ssam	return "?";
2831170531Ssam#undef WPA_SEL
2832170531Ssam}
2833170531Ssam
2834170531Ssamstatic void
2835170531Ssamprintwpaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2836170531Ssam{
2837170531Ssam	u_int8_t len = ie[1];
2838170531Ssam
2839170531Ssam	printf("%s", tag);
2840170531Ssam	if (verbose) {
2841170531Ssam		const char *sep;
2842170531Ssam		int n;
2843170531Ssam
2844170531Ssam		ie += 6, len -= 4;		/* NB: len is payload only */
2845170531Ssam
2846170531Ssam		printf("<v%u", LE_READ_2(ie));
2847170531Ssam		ie += 2, len -= 2;
2848170531Ssam
2849170531Ssam		printf(" mc:%s", wpa_cipher(ie));
2850170531Ssam		ie += 4, len -= 4;
2851170531Ssam
2852170531Ssam		/* unicast ciphers */
2853170531Ssam		n = LE_READ_2(ie);
2854170531Ssam		ie += 2, len -= 2;
2855170531Ssam		sep = " uc:";
2856170531Ssam		for (; n > 0; n--) {
2857170531Ssam			printf("%s%s", sep, wpa_cipher(ie));
2858170531Ssam			ie += 4, len -= 4;
2859170531Ssam			sep = "+";
2860170531Ssam		}
2861170531Ssam
2862170531Ssam		/* key management algorithms */
2863170531Ssam		n = LE_READ_2(ie);
2864170531Ssam		ie += 2, len -= 2;
2865170531Ssam		sep = " km:";
2866170531Ssam		for (; n > 0; n--) {
2867170531Ssam			printf("%s%s", sep, wpa_keymgmt(ie));
2868170531Ssam			ie += 4, len -= 4;
2869170531Ssam			sep = "+";
2870170531Ssam		}
2871170531Ssam
2872170531Ssam		if (len > 2)		/* optional capabilities */
2873170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2874170531Ssam		printf(">");
2875170531Ssam	}
2876170531Ssam}
2877170531Ssam
2878170531Ssamstatic const char *
2879170531Ssamrsn_cipher(const u_int8_t *sel)
2880170531Ssam{
2881170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2882170531Ssam	u_int32_t w = LE_READ_4(sel);
2883170531Ssam
2884170531Ssam	switch (w) {
2885170531Ssam	case RSN_SEL(RSN_CSE_NULL):
2886170531Ssam		return "NONE";
2887170531Ssam	case RSN_SEL(RSN_CSE_WEP40):
2888170531Ssam		return "WEP40";
2889170531Ssam	case RSN_SEL(RSN_CSE_WEP104):
2890170531Ssam		return "WEP104";
2891170531Ssam	case RSN_SEL(RSN_CSE_TKIP):
2892170531Ssam		return "TKIP";
2893170531Ssam	case RSN_SEL(RSN_CSE_CCMP):
2894170531Ssam		return "AES-CCMP";
2895170531Ssam	case RSN_SEL(RSN_CSE_WRAP):
2896170531Ssam		return "AES-OCB";
2897170531Ssam	}
2898170531Ssam	return "?";
2899170531Ssam#undef WPA_SEL
2900170531Ssam}
2901170531Ssam
2902170531Ssamstatic const char *
2903170531Ssamrsn_keymgmt(const u_int8_t *sel)
2904170531Ssam{
2905170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2906170531Ssam	u_int32_t w = LE_READ_4(sel);
2907170531Ssam
2908170531Ssam	switch (w) {
2909170531Ssam	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
2910170531Ssam		return "8021X-UNSPEC";
2911170531Ssam	case RSN_SEL(RSN_ASE_8021X_PSK):
2912170531Ssam		return "8021X-PSK";
2913170531Ssam	case RSN_SEL(RSN_ASE_NONE):
2914170531Ssam		return "NONE";
2915170531Ssam	}
2916170531Ssam	return "?";
2917170531Ssam#undef RSN_SEL
2918170531Ssam}
2919170531Ssam
2920170531Ssamstatic void
2921170531Ssamprintrsnie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2922170531Ssam{
2923170531Ssam	printf("%s", tag);
2924170531Ssam	if (verbose) {
2925170531Ssam		const char *sep;
2926170531Ssam		int n;
2927170531Ssam
2928173275Ssam		ie += 2, ielen -= 2;
2929170531Ssam
2930170531Ssam		printf("<v%u", LE_READ_2(ie));
2931173275Ssam		ie += 2, ielen -= 2;
2932170531Ssam
2933170531Ssam		printf(" mc:%s", rsn_cipher(ie));
2934173275Ssam		ie += 4, ielen -= 4;
2935170531Ssam
2936170531Ssam		/* unicast ciphers */
2937170531Ssam		n = LE_READ_2(ie);
2938173275Ssam		ie += 2, ielen -= 2;
2939170531Ssam		sep = " uc:";
2940170531Ssam		for (; n > 0; n--) {
2941170531Ssam			printf("%s%s", sep, rsn_cipher(ie));
2942173275Ssam			ie += 4, ielen -= 4;
2943170531Ssam			sep = "+";
2944170531Ssam		}
2945170531Ssam
2946170531Ssam		/* key management algorithms */
2947170531Ssam		n = LE_READ_2(ie);
2948173275Ssam		ie += 2, ielen -= 2;
2949170531Ssam		sep = " km:";
2950170531Ssam		for (; n > 0; n--) {
2951170531Ssam			printf("%s%s", sep, rsn_keymgmt(ie));
2952173275Ssam			ie += 4, ielen -= 4;
2953170531Ssam			sep = "+";
2954170531Ssam		}
2955170531Ssam
2956173275Ssam		if (ielen > 2)		/* optional capabilities */
2957170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2958170531Ssam		/* XXXPMKID */
2959170531Ssam		printf(">");
2960170531Ssam	}
2961170531Ssam}
2962170531Ssam
2963181454Ssam/* XXX move to a public include file */
2964181454Ssam#define IEEE80211_WPS_DEV_PASS_ID	0x1012
2965181454Ssam#define IEEE80211_WPS_SELECTED_REG	0x1041
2966181454Ssam#define IEEE80211_WPS_SETUP_STATE	0x1044
2967181454Ssam#define IEEE80211_WPS_UUID_E		0x1047
2968181454Ssam#define IEEE80211_WPS_VERSION		0x104a
2969181454Ssam
2970181454Ssam#define BE_READ_2(p)					\
2971181454Ssam	((u_int16_t)					\
2972181454Ssam	 ((((const u_int8_t *)(p))[1]      ) |		\
2973181454Ssam	  (((const u_int8_t *)(p))[0] <<  8)))
2974181454Ssam
2975181454Ssamstatic void
2976181454Ssamprintwpsie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2977181454Ssam{
2978181454Ssam	u_int8_t len = ie[1];
2979181454Ssam
2980181454Ssam	printf("%s", tag);
2981181454Ssam	if (verbose) {
2982181454Ssam		static const char *dev_pass_id[] = {
2983181454Ssam			"D",	/* Default (PIN) */
2984181454Ssam			"U",	/* User-specified */
2985181454Ssam			"M",	/* Machine-specified */
2986181454Ssam			"K",	/* Rekey */
2987181454Ssam			"P",	/* PushButton */
2988181454Ssam			"R"	/* Registrar-specified */
2989181454Ssam		};
2990181454Ssam		int n;
2991181454Ssam
2992181454Ssam		ie +=6, len -= 4;		/* NB: len is payload only */
2993181454Ssam
2994181454Ssam		/* WPS IE in Beacon and Probe Resp frames have different fields */
2995181454Ssam		printf("<");
2996181454Ssam		while (len) {
2997181454Ssam			uint16_t tlv_type = BE_READ_2(ie);
2998181454Ssam			uint16_t tlv_len  = BE_READ_2(ie + 2);
2999181454Ssam
3000181454Ssam			ie += 4, len -= 4;
3001181454Ssam
3002181454Ssam			switch (tlv_type) {
3003181454Ssam			case IEEE80211_WPS_VERSION:
3004181454Ssam				printf("v:%d.%d", *ie >> 4, *ie & 0xf);
3005181454Ssam				break;
3006181454Ssam			case IEEE80211_WPS_SETUP_STATE:
3007181454Ssam				/* Only 1 and 2 are valid */
3008181454Ssam				if (*ie == 0 || *ie >= 3)
3009181454Ssam					printf(" state:B");
3010181454Ssam				else
3011181454Ssam					printf(" st:%s", *ie == 1 ? "N" : "C");
3012181454Ssam				break;
3013181454Ssam			case IEEE80211_WPS_SELECTED_REG:
3014181454Ssam				printf(" sel:%s", *ie ? "T" : "F");
3015181454Ssam				break;
3016181454Ssam			case IEEE80211_WPS_DEV_PASS_ID:
3017181454Ssam				n = LE_READ_2(ie);
3018288305Sngie				if (n < nitems(dev_pass_id))
3019181454Ssam					printf(" dpi:%s", dev_pass_id[n]);
3020181454Ssam				break;
3021181454Ssam			case IEEE80211_WPS_UUID_E:
3022181454Ssam				printf(" uuid-e:");
3023181454Ssam				for (n = 0; n < (tlv_len - 1); n++)
3024181454Ssam					printf("%02x-", ie[n]);
3025181454Ssam				printf("%02x", ie[n]);
3026181454Ssam				break;
3027181454Ssam			}
3028181454Ssam			ie += tlv_len, len -= tlv_len;
3029181454Ssam		}
3030181454Ssam		printf(">");
3031181454Ssam	}
3032181454Ssam}
3033181454Ssam
3034186904Ssamstatic void
3035186904Ssamprinttdmaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
3036186904Ssam{
3037186904Ssam	printf("%s", tag);
3038186904Ssam	if (verbose && ielen >= sizeof(struct ieee80211_tdma_param)) {
3039186904Ssam		const struct ieee80211_tdma_param *tdma =
3040186904Ssam		   (const struct ieee80211_tdma_param *) ie;
3041186904Ssam
3042186904Ssam		/* XXX tstamp */
3043186904Ssam		printf("<v%u slot:%u slotcnt:%u slotlen:%u bintval:%u inuse:0x%x>",
3044186904Ssam		    tdma->tdma_version, tdma->tdma_slot, tdma->tdma_slotcnt,
3045186904Ssam		    LE_READ_2(&tdma->tdma_slotlen), tdma->tdma_bintval,
3046186904Ssam		    tdma->tdma_inuse[0]);
3047186904Ssam	}
3048186904Ssam}
3049186904Ssam
3050170531Ssam/*
3051138593Ssam * Copy the ssid string contents into buf, truncating to fit.  If the
3052138593Ssam * ssid is entirely printable then just copy intact.  Otherwise convert
3053138593Ssam * to hexadecimal.  If the result is truncated then replace the last
3054138593Ssam * three characters with "...".
3055138593Ssam */
3056146873Sjhbstatic int
3057138593Ssamcopy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
3058138593Ssam{
3059138593Ssam	const u_int8_t *p;
3060138593Ssam	size_t maxlen;
3061298556Spfg	u_int i;
3062138593Ssam
3063138593Ssam	if (essid_len > bufsize)
3064138593Ssam		maxlen = bufsize;
3065138593Ssam	else
3066138593Ssam		maxlen = essid_len;
3067138593Ssam	/* determine printable or not */
3068138593Ssam	for (i = 0, p = essid; i < maxlen; i++, p++) {
3069138593Ssam		if (*p < ' ' || *p > 0x7e)
3070138593Ssam			break;
3071138593Ssam	}
3072138593Ssam	if (i != maxlen) {		/* not printable, print as hex */
3073138593Ssam		if (bufsize < 3)
3074138593Ssam			return 0;
3075138593Ssam		strlcpy(buf, "0x", bufsize);
3076138593Ssam		bufsize -= 2;
3077138593Ssam		p = essid;
3078138593Ssam		for (i = 0; i < maxlen && bufsize >= 2; i++) {
3079147489Savatar			sprintf(&buf[2+2*i], "%02x", p[i]);
3080138593Ssam			bufsize -= 2;
3081138593Ssam		}
3082147489Savatar		if (i != essid_len)
3083147489Savatar			memcpy(&buf[2+2*i-3], "...", 3);
3084138593Ssam	} else {			/* printable, truncate as needed */
3085138593Ssam		memcpy(buf, essid, maxlen);
3086147489Savatar		if (maxlen != essid_len)
3087147489Savatar			memcpy(&buf[maxlen-3], "...", 3);
3088138593Ssam	}
3089138593Ssam	return maxlen;
3090138593Ssam}
3091138593Ssam
3092173275Ssamstatic void
3093173275Ssamprintssid(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
3094173275Ssam{
3095173275Ssam	char ssid[2*IEEE80211_NWID_LEN+1];
3096173275Ssam
3097173275Ssam	printf("%s<%.*s>", tag, copy_essid(ssid, maxlen, ie+2, ie[1]), ssid);
3098173275Ssam}
3099173275Ssam
3100173275Ssamstatic void
3101173275Ssamprintrates(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
3102173275Ssam{
3103173275Ssam	const char *sep;
3104173275Ssam	int i;
3105173275Ssam
3106173275Ssam	printf("%s", tag);
3107173275Ssam	sep = "<";
3108173275Ssam	for (i = 2; i < ielen; i++) {
3109173275Ssam		printf("%s%s%d", sep,
3110173275Ssam		    ie[i] & IEEE80211_RATE_BASIC ? "B" : "",
3111173275Ssam		    ie[i] & IEEE80211_RATE_VAL);
3112173275Ssam		sep = ",";
3113173275Ssam	}
3114173275Ssam	printf(">");
3115173275Ssam}
3116173275Ssam
3117173275Ssamstatic void
3118173275Ssamprintcountry(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
3119173275Ssam{
3120173275Ssam	const struct ieee80211_country_ie *cie =
3121173275Ssam	   (const struct ieee80211_country_ie *) ie;
3122173275Ssam	int i, nbands, schan, nchan;
3123173275Ssam
3124173275Ssam	printf("%s<%c%c%c", tag, cie->cc[0], cie->cc[1], cie->cc[2]);
3125173275Ssam	nbands = (cie->len - 3) / sizeof(cie->band[0]);
3126173275Ssam	for (i = 0; i < nbands; i++) {
3127173275Ssam		schan = cie->band[i].schan;
3128173275Ssam		nchan = cie->band[i].nchan;
3129173275Ssam		if (nchan != 1)
3130173275Ssam			printf(" %u-%u,%u", schan, schan + nchan-1,
3131173275Ssam			    cie->band[i].maxtxpwr);
3132173275Ssam		else
3133173275Ssam			printf(" %u,%u", schan, cie->band[i].maxtxpwr);
3134173275Ssam	}
3135173275Ssam	printf(">");
3136173275Ssam}
3137173275Ssam
3138178354Ssamstatic __inline int
3139138593Ssamiswpaoui(const u_int8_t *frm)
3140138593Ssam{
3141138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
3142138593Ssam}
3143138593Ssam
3144178354Ssamstatic __inline int
3145173275Ssamiswmeinfo(const u_int8_t *frm)
3146138593Ssam{
3147173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3148173275Ssam		frm[6] == WME_INFO_OUI_SUBTYPE;
3149138593Ssam}
3150138593Ssam
3151178354Ssamstatic __inline int
3152173275Ssamiswmeparam(const u_int8_t *frm)
3153173275Ssam{
3154173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3155173275Ssam		frm[6] == WME_PARAM_OUI_SUBTYPE;
3156173275Ssam}
3157173275Ssam
3158178354Ssamstatic __inline int
3159138593Ssamisatherosoui(const u_int8_t *frm)
3160138593Ssam{
3161138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
3162138593Ssam}
3163138593Ssam
3164181454Ssamstatic __inline int
3165186904Ssamistdmaoui(const uint8_t *frm)
3166186904Ssam{
3167186904Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((TDMA_OUI_TYPE<<24)|TDMA_OUI);
3168186904Ssam}
3169186904Ssam
3170186904Ssamstatic __inline int
3171181454Ssamiswpsoui(const uint8_t *frm)
3172181454Ssam{
3173181454Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPS_OUI_TYPE<<24)|WPA_OUI);
3174181454Ssam}
3175181454Ssam
3176173275Ssamstatic const char *
3177173275Ssamiename(int elemid)
3178173275Ssam{
3179173275Ssam	switch (elemid) {
3180173275Ssam	case IEEE80211_ELEMID_FHPARMS:	return " FHPARMS";
3181173275Ssam	case IEEE80211_ELEMID_CFPARMS:	return " CFPARMS";
3182173275Ssam	case IEEE80211_ELEMID_TIM:	return " TIM";
3183173275Ssam	case IEEE80211_ELEMID_IBSSPARMS:return " IBSSPARMS";
3184296823Sadrian	case IEEE80211_ELEMID_BSSLOAD:	return " BSSLOAD";
3185173275Ssam	case IEEE80211_ELEMID_CHALLENGE:return " CHALLENGE";
3186173275Ssam	case IEEE80211_ELEMID_PWRCNSTR:	return " PWRCNSTR";
3187173275Ssam	case IEEE80211_ELEMID_PWRCAP:	return " PWRCAP";
3188173275Ssam	case IEEE80211_ELEMID_TPCREQ:	return " TPCREQ";
3189173275Ssam	case IEEE80211_ELEMID_TPCREP:	return " TPCREP";
3190173275Ssam	case IEEE80211_ELEMID_SUPPCHAN:	return " SUPPCHAN";
3191193447Ssam	case IEEE80211_ELEMID_CSA:	return " CSA";
3192173275Ssam	case IEEE80211_ELEMID_MEASREQ:	return " MEASREQ";
3193173275Ssam	case IEEE80211_ELEMID_MEASREP:	return " MEASREP";
3194173275Ssam	case IEEE80211_ELEMID_QUIET:	return " QUIET";
3195173275Ssam	case IEEE80211_ELEMID_IBSSDFS:	return " IBSSDFS";
3196173275Ssam	case IEEE80211_ELEMID_TPC:	return " TPC";
3197173275Ssam	case IEEE80211_ELEMID_CCKM:	return " CCKM";
3198173275Ssam	}
3199173275Ssam	return " ???";
3200173275Ssam}
3201173275Ssam
3202138593Ssamstatic void
3203138593Ssamprinties(const u_int8_t *vp, int ielen, int maxcols)
3204138593Ssam{
3205138593Ssam	while (ielen > 0) {
3206138593Ssam		switch (vp[0]) {
3207173275Ssam		case IEEE80211_ELEMID_SSID:
3208173275Ssam			if (verbose)
3209173275Ssam				printssid(" SSID", vp, 2+vp[1], maxcols);
3210173275Ssam			break;
3211173275Ssam		case IEEE80211_ELEMID_RATES:
3212173275Ssam		case IEEE80211_ELEMID_XRATES:
3213173275Ssam			if (verbose)
3214173275Ssam				printrates(vp[0] == IEEE80211_ELEMID_RATES ?
3215173275Ssam				    " RATES" : " XRATES", vp, 2+vp[1], maxcols);
3216173275Ssam			break;
3217173275Ssam		case IEEE80211_ELEMID_DSPARMS:
3218173275Ssam			if (verbose)
3219173275Ssam				printf(" DSPARMS<%u>", vp[2]);
3220173275Ssam			break;
3221173275Ssam		case IEEE80211_ELEMID_COUNTRY:
3222173275Ssam			if (verbose)
3223173275Ssam				printcountry(" COUNTRY", vp, 2+vp[1], maxcols);
3224173275Ssam			break;
3225173275Ssam		case IEEE80211_ELEMID_ERP:
3226173275Ssam			if (verbose)
3227173275Ssam				printf(" ERP<0x%x>", vp[2]);
3228173275Ssam			break;
3229138593Ssam		case IEEE80211_ELEMID_VENDOR:
3230138593Ssam			if (iswpaoui(vp))
3231170531Ssam				printwpaie(" WPA", vp, 2+vp[1], maxcols);
3232173275Ssam			else if (iswmeinfo(vp))
3233173275Ssam				printwmeinfo(" WME", vp, 2+vp[1], maxcols);
3234173275Ssam			else if (iswmeparam(vp))
3235173275Ssam				printwmeparam(" WME", vp, 2+vp[1], maxcols);
3236139492Ssam			else if (isatherosoui(vp))
3237170531Ssam				printathie(" ATH", vp, 2+vp[1], maxcols);
3238181454Ssam			else if (iswpsoui(vp))
3239181454Ssam				printwpsie(" WPS", vp, 2+vp[1], maxcols);
3240186904Ssam			else if (istdmaoui(vp))
3241186904Ssam				printtdmaie(" TDMA", vp, 2+vp[1], maxcols);
3242173275Ssam			else if (verbose)
3243138593Ssam				printie(" VEN", vp, 2+vp[1], maxcols);
3244138593Ssam			break;
3245138593Ssam		case IEEE80211_ELEMID_RSN:
3246170531Ssam			printrsnie(" RSN", vp, 2+vp[1], maxcols);
3247138593Ssam			break;
3248173275Ssam		case IEEE80211_ELEMID_HTCAP:
3249173275Ssam			printhtcap(" HTCAP", vp, 2+vp[1], maxcols);
3250173275Ssam			break;
3251173275Ssam		case IEEE80211_ELEMID_HTINFO:
3252173275Ssam			if (verbose)
3253173275Ssam				printhtinfo(" HTINFO", vp, 2+vp[1], maxcols);
3254173275Ssam			break;
3255195618Srpaulo		case IEEE80211_ELEMID_MESHID:
3256195618Srpaulo			if (verbose)
3257195618Srpaulo				printssid(" MESHID", vp, 2+vp[1], maxcols);
3258195618Srpaulo			break;
3259195618Srpaulo		case IEEE80211_ELEMID_MESHCONF:
3260195618Srpaulo			printmeshconf(" MESHCONF", vp, 2+vp[1], maxcols);
3261195618Srpaulo			break;
3262297007Sadrian		case IEEE80211_ELEMID_VHT_CAP:
3263297007Sadrian			printvhtcap(" VHTCAP", vp, 2+vp[1], maxcols);
3264297007Sadrian			break;
3265297007Sadrian		case IEEE80211_ELEMID_VHT_OPMODE:
3266297007Sadrian			printvhtinfo(" VHTOPMODE", vp, 2+vp[1], maxcols);
3267297007Sadrian			break;
3268298252Sadrian		case IEEE80211_ELEMID_VHT_PWR_ENV:
3269298252Sadrian			printvhtpwrenv(" VHTPWRENV", vp, 2+vp[1], maxcols);
3270298252Sadrian			break;
3271297007Sadrian		case IEEE80211_ELEMID_BSSLOAD:
3272297007Sadrian			printbssload(" BSSLOAD", vp, 2+vp[1], maxcols);
3273297007Sadrian			break;
3274297010Sadrian		case IEEE80211_ELEMID_APCHANREP:
3275297010Sadrian			printapchanrep(" APCHANREP", vp, 2+vp[1], maxcols);
3276297010Sadrian			break;
3277138593Ssam		default:
3278173275Ssam			if (verbose)
3279173275Ssam				printie(iename(vp[0]), vp, 2+vp[1], maxcols);
3280138593Ssam			break;
3281138593Ssam		}
3282138593Ssam		ielen -= 2+vp[1];
3283138593Ssam		vp += 2+vp[1];
3284138593Ssam	}
3285138593Ssam}
3286138593Ssam
3287138593Ssamstatic void
3288178354Ssamprintmimo(const struct ieee80211_mimo_info *mi)
3289178354Ssam{
3290178354Ssam	/* NB: don't muddy display unless there's something to show */
3291220935Sadrian	if (mi->rssi[0] != 0 || mi->rssi[1] != 0 || mi->rssi[2] != 0) {
3292178354Ssam		/* XXX ignore EVM for now */
3293178354Ssam		printf(" (rssi %d:%d:%d nf %d:%d:%d)",
3294220935Sadrian		    mi->rssi[0], mi->rssi[1], mi->rssi[2],
3295220935Sadrian		    mi->noise[0], mi->noise[1], mi->noise[2]);
3296178354Ssam	}
3297178354Ssam}
3298178354Ssam
3299178354Ssamstatic void
3300138593Ssamlist_scan(int s)
3301138593Ssam{
3302138593Ssam	uint8_t buf[24*1024];
3303153892Srwatson	char ssid[IEEE80211_NWID_LEN+1];
3304173275Ssam	const uint8_t *cp;
3305195618Srpaulo	int len, ssidmax, idlen;
3306138593Ssam
3307173275Ssam	if (get80211len(s, IEEE80211_IOC_SCAN_RESULTS, buf, sizeof(buf), &len) < 0)
3308138593Ssam		errx(1, "unable to get scan results");
3309138593Ssam	if (len < sizeof(struct ieee80211req_scan_result))
3310138593Ssam		return;
3311138593Ssam
3312170531Ssam	getchaninfo(s);
3313170531Ssam
3314195618Srpaulo	ssidmax = verbose ? IEEE80211_NWID_LEN - 1 : 14;
3315290438Savos	printf("%-*.*s  %-17.17s  %4s %4s   %-7s  %3s %4s\n"
3316195618Srpaulo		, ssidmax, ssidmax, "SSID/MESH ID"
3317138593Ssam		, "BSSID"
3318138593Ssam		, "CHAN"
3319138593Ssam		, "RATE"
3320170531Ssam		, " S:N"
3321138593Ssam		, "INT"
3322138593Ssam		, "CAPS"
3323138593Ssam	);
3324138593Ssam	cp = buf;
3325138593Ssam	do {
3326170531Ssam		const struct ieee80211req_scan_result *sr;
3327195618Srpaulo		const uint8_t *vp, *idp;
3328138593Ssam
3329170531Ssam		sr = (const struct ieee80211req_scan_result *) cp;
3330173275Ssam		vp = cp + sr->isr_ie_off;
3331195618Srpaulo		if (sr->isr_meshid_len) {
3332195618Srpaulo			idp = vp + sr->isr_ssid_len;
3333195618Srpaulo			idlen = sr->isr_meshid_len;
3334195618Srpaulo		} else {
3335195618Srpaulo			idp = vp;
3336195618Srpaulo			idlen = sr->isr_ssid_len;
3337195618Srpaulo		}
3338290438Savos		printf("%-*.*s  %s  %3d  %3dM %4d:%-4d %4d %-4.4s"
3339154522Ssam			, ssidmax
3340195618Srpaulo			  , copy_essid(ssid, ssidmax, idp, idlen)
3341154522Ssam			  , ssid
3342138593Ssam			, ether_ntoa((const struct ether_addr *) sr->isr_bssid)
3343165570Ssam			, ieee80211_mhz2ieee(sr->isr_freq, sr->isr_flags)
3344138593Ssam			, getmaxrate(sr->isr_rates, sr->isr_nrates)
3345170531Ssam			, (sr->isr_rssi/2)+sr->isr_noise, sr->isr_noise
3346138593Ssam			, sr->isr_intval
3347138593Ssam			, getcaps(sr->isr_capinfo)
3348138593Ssam		);
3349195618Srpaulo		printies(vp + sr->isr_ssid_len + sr->isr_meshid_len,
3350195618Srpaulo		    sr->isr_ie_len, 24);
3351138593Ssam		printf("\n");
3352138593Ssam		cp += sr->isr_len, len -= sr->isr_len;
3353138593Ssam	} while (len >= sizeof(struct ieee80211req_scan_result));
3354138593Ssam}
3355138593Ssam
3356138593Ssamstatic void
3357138593Ssamscan_and_wait(int s)
3358138593Ssam{
3359178354Ssam	struct ieee80211_scan_req sr;
3360138593Ssam	struct ieee80211req ireq;
3361138593Ssam	int sroute;
3362138593Ssam
3363138593Ssam	sroute = socket(PF_ROUTE, SOCK_RAW, 0);
3364138593Ssam	if (sroute < 0) {
3365138593Ssam		perror("socket(PF_ROUTE,SOCK_RAW)");
3366138593Ssam		return;
3367138593Ssam	}
3368138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3369299873Struckman	(void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
3370138593Ssam	ireq.i_type = IEEE80211_IOC_SCAN_REQ;
3371178354Ssam
3372178354Ssam	memset(&sr, 0, sizeof(sr));
3373178354Ssam	sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE
3374218090Sbschmidt		    | IEEE80211_IOC_SCAN_BGSCAN
3375178354Ssam		    | IEEE80211_IOC_SCAN_NOPICK
3376178354Ssam		    | IEEE80211_IOC_SCAN_ONCE;
3377178354Ssam	sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
3378178354Ssam	sr.sr_nssid = 0;
3379178354Ssam
3380178354Ssam	ireq.i_data = &sr;
3381178354Ssam	ireq.i_len = sizeof(sr);
3382218093Sbschmidt	/*
3383218093Sbschmidt	 * NB: only root can trigger a scan so ignore errors. Also ignore
3384218093Sbschmidt	 * possible errors from net80211, even if no new scan could be
3385218093Sbschmidt	 * started there might still be a valid scan cache.
3386218093Sbschmidt	 */
3387218093Sbschmidt	if (ioctl(s, SIOCS80211, &ireq) == 0) {
3388138593Ssam		char buf[2048];
3389138593Ssam		struct if_announcemsghdr *ifan;
3390138593Ssam		struct rt_msghdr *rtm;
3391138593Ssam
3392138593Ssam		do {
3393138593Ssam			if (read(sroute, buf, sizeof(buf)) < 0) {
3394138593Ssam				perror("read(PF_ROUTE)");
3395138593Ssam				break;
3396138593Ssam			}
3397138593Ssam			rtm = (struct rt_msghdr *) buf;
3398138593Ssam			if (rtm->rtm_version != RTM_VERSION)
3399138593Ssam				break;
3400138593Ssam			ifan = (struct if_announcemsghdr *) rtm;
3401138593Ssam		} while (rtm->rtm_type != RTM_IEEE80211 ||
3402138593Ssam		    ifan->ifan_what != RTM_IEEE80211_SCAN);
3403138593Ssam	}
3404138593Ssam	close(sroute);
3405138593Ssam}
3406138593Ssam
3407138593Ssamstatic
3408138593SsamDECL_CMD_FUNC(set80211scan, val, d)
3409138593Ssam{
3410138593Ssam	scan_and_wait(s);
3411138593Ssam	list_scan(s);
3412138593Ssam}
3413138593Ssam
3414161147Ssamstatic enum ieee80211_opmode get80211opmode(int s);
3415161147Ssam
3416173275Ssamstatic int
3417173275Ssamgettxseq(const struct ieee80211req_sta_info *si)
3418173275Ssam{
3419173275Ssam	int i, txseq;
3420173275Ssam
3421173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3422173275Ssam		return si->isi_txseqs[0];
3423173275Ssam	/* XXX not right but usually what folks want */
3424173275Ssam	txseq = 0;
3425173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3426173275Ssam		if (si->isi_txseqs[i] > txseq)
3427173275Ssam			txseq = si->isi_txseqs[i];
3428173275Ssam	return txseq;
3429173275Ssam}
3430173275Ssam
3431173275Ssamstatic int
3432173275Ssamgetrxseq(const struct ieee80211req_sta_info *si)
3433173275Ssam{
3434173275Ssam	int i, rxseq;
3435173275Ssam
3436173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3437173275Ssam		return si->isi_rxseqs[0];
3438173275Ssam	/* XXX not right but usually what folks want */
3439173275Ssam	rxseq = 0;
3440173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3441173275Ssam		if (si->isi_rxseqs[i] > rxseq)
3442173275Ssam			rxseq = si->isi_rxseqs[i];
3443173275Ssam	return rxseq;
3444173275Ssam}
3445173275Ssam
3446138593Ssamstatic void
3447138593Ssamlist_stations(int s)
3448138593Ssam{
3449161147Ssam	union {
3450161147Ssam		struct ieee80211req_sta_req req;
3451161147Ssam		uint8_t buf[24*1024];
3452161147Ssam	} u;
3453161147Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
3454170531Ssam	const uint8_t *cp;
3455138593Ssam	int len;
3456138593Ssam
3457161147Ssam	/* broadcast address =>'s get all stations */
3458161147Ssam	(void) memset(u.req.is_u.macaddr, 0xff, IEEE80211_ADDR_LEN);
3459161147Ssam	if (opmode == IEEE80211_M_STA) {
3460161147Ssam		/*
3461161147Ssam		 * Get information about the associated AP.
3462161147Ssam		 */
3463173275Ssam		(void) get80211(s, IEEE80211_IOC_BSSID,
3464173275Ssam		    u.req.is_u.macaddr, IEEE80211_ADDR_LEN);
3465161147Ssam	}
3466173275Ssam	if (get80211len(s, IEEE80211_IOC_STA_INFO, &u, sizeof(u), &len) < 0)
3467138593Ssam		errx(1, "unable to get station information");
3468138593Ssam	if (len < sizeof(struct ieee80211req_sta_info))
3469138593Ssam		return;
3470138593Ssam
3471170531Ssam	getchaninfo(s);
3472170531Ssam
3473195618Srpaulo	if (opmode == IEEE80211_M_MBSS)
3474195618Srpaulo		printf("%-17.17s %4s %5s %5s %7s %4s %4s %4s %6s %6s\n"
3475195618Srpaulo			, "ADDR"
3476195618Srpaulo			, "CHAN"
3477195618Srpaulo			, "LOCAL"
3478195618Srpaulo			, "PEER"
3479195618Srpaulo			, "STATE"
3480195618Srpaulo			, "RATE"
3481195618Srpaulo			, "RSSI"
3482195618Srpaulo			, "IDLE"
3483195618Srpaulo			, "TXSEQ"
3484195618Srpaulo			, "RXSEQ"
3485195618Srpaulo		);
3486195618Srpaulo	else
3487195618Srpaulo		printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %-7s\n"
3488195618Srpaulo			, "ADDR"
3489195618Srpaulo			, "AID"
3490195618Srpaulo			, "CHAN"
3491195618Srpaulo			, "RATE"
3492195618Srpaulo			, "RSSI"
3493195618Srpaulo			, "IDLE"
3494195618Srpaulo			, "TXSEQ"
3495195618Srpaulo			, "RXSEQ"
3496195618Srpaulo			, "CAPS"
3497195618Srpaulo			, "FLAG"
3498195618Srpaulo		);
3499170531Ssam	cp = (const uint8_t *) u.req.info;
3500138593Ssam	do {
3501170531Ssam		const struct ieee80211req_sta_info *si;
3502138593Ssam
3503170531Ssam		si = (const struct ieee80211req_sta_info *) cp;
3504161147Ssam		if (si->isi_len < sizeof(*si))
3505161147Ssam			break;
3506195618Srpaulo		if (opmode == IEEE80211_M_MBSS)
3507195618Srpaulo			printf("%s %4d %5x %5x %7.7s %3dM %4.1f %4d %6d %6d"
3508195618Srpaulo				, ether_ntoa((const struct ether_addr*)
3509195618Srpaulo				    si->isi_macaddr)
3510195618Srpaulo				, ieee80211_mhz2ieee(si->isi_freq,
3511195618Srpaulo				    si->isi_flags)
3512195618Srpaulo				, si->isi_localid
3513195618Srpaulo				, si->isi_peerid
3514195618Srpaulo				, mesh_linkstate_string(si->isi_peerstate)
3515195618Srpaulo				, si->isi_txmbps/2
3516195618Srpaulo				, si->isi_rssi/2.
3517195618Srpaulo				, si->isi_inact
3518195618Srpaulo				, gettxseq(si)
3519195618Srpaulo				, getrxseq(si)
3520195618Srpaulo			);
3521195618Srpaulo		else
3522195618Srpaulo			printf("%s %4u %4d %3dM %4.1f %4d %6d %6d %-4.4s %-7.7s"
3523195618Srpaulo				, ether_ntoa((const struct ether_addr*)
3524195618Srpaulo				    si->isi_macaddr)
3525195618Srpaulo				, IEEE80211_AID(si->isi_associd)
3526195618Srpaulo				, ieee80211_mhz2ieee(si->isi_freq,
3527195618Srpaulo				    si->isi_flags)
3528195618Srpaulo				, si->isi_txmbps/2
3529195618Srpaulo				, si->isi_rssi/2.
3530195618Srpaulo				, si->isi_inact
3531195618Srpaulo				, gettxseq(si)
3532195618Srpaulo				, getrxseq(si)
3533195618Srpaulo				, getcaps(si->isi_capinfo)
3534195618Srpaulo				, getflags(si->isi_state)
3535195618Srpaulo			);
3536170531Ssam		printies(cp + si->isi_ie_off, si->isi_ie_len, 24);
3537178354Ssam		printmimo(&si->isi_mimo);
3538138593Ssam		printf("\n");
3539138593Ssam		cp += si->isi_len, len -= si->isi_len;
3540138593Ssam	} while (len >= sizeof(struct ieee80211req_sta_info));
3541138593Ssam}
3542138593Ssam
3543170531Ssamstatic const char *
3544195618Srpaulomesh_linkstate_string(uint8_t state)
3545195618Srpaulo{
3546195618Srpaulo	static const char *state_names[] = {
3547195618Srpaulo	    [0] = "IDLE",
3548195618Srpaulo	    [1] = "OPEN-TX",
3549195618Srpaulo	    [2] = "OPEN-RX",
3550195618Srpaulo	    [3] = "CONF-RX",
3551195618Srpaulo	    [4] = "ESTAB",
3552195618Srpaulo	    [5] = "HOLDING",
3553195618Srpaulo	};
3554195618Srpaulo
3555288305Sngie	if (state >= nitems(state_names)) {
3556195618Srpaulo		static char buf[10];
3557195618Srpaulo		snprintf(buf, sizeof(buf), "#%u", state);
3558195618Srpaulo		return buf;
3559195618Srpaulo	} else
3560195618Srpaulo		return state_names[state];
3561195618Srpaulo}
3562195618Srpaulo
3563195618Srpaulostatic const char *
3564170531Ssamget_chaninfo(const struct ieee80211_channel *c, int precise,
3565170531Ssam	char buf[], size_t bsize)
3566138593Ssam{
3567138593Ssam	buf[0] = '\0';
3568138593Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
3569170531Ssam		strlcat(buf, " FHSS", bsize);
3570187843Ssam	if (IEEE80211_IS_CHAN_A(c))
3571187843Ssam		strlcat(buf, " 11a", bsize);
3572187843Ssam	else if (IEEE80211_IS_CHAN_ANYG(c))
3573187843Ssam		strlcat(buf, " 11g", bsize);
3574187843Ssam	else if (IEEE80211_IS_CHAN_B(c))
3575170531Ssam		strlcat(buf, " 11b", bsize);
3576187843Ssam	if (IEEE80211_IS_CHAN_HALF(c))
3577202161Sgavin		strlcat(buf, "/10MHz", bsize);
3578187843Ssam	if (IEEE80211_IS_CHAN_QUARTER(c))
3579202161Sgavin		strlcat(buf, "/5MHz", bsize);
3580170531Ssam	if (IEEE80211_IS_CHAN_TURBO(c))
3581170531Ssam		strlcat(buf, " Turbo", bsize);
3582170531Ssam	if (precise) {
3583170531Ssam		if (IEEE80211_IS_CHAN_HT20(c))
3584170531Ssam			strlcat(buf, " ht/20", bsize);
3585170531Ssam		else if (IEEE80211_IS_CHAN_HT40D(c))
3586170531Ssam			strlcat(buf, " ht/40-", bsize);
3587170531Ssam		else if (IEEE80211_IS_CHAN_HT40U(c))
3588170531Ssam			strlcat(buf, " ht/40+", bsize);
3589170531Ssam	} else {
3590170531Ssam		if (IEEE80211_IS_CHAN_HT(c))
3591170531Ssam			strlcat(buf, " ht", bsize);
3592170531Ssam	}
3593170531Ssam	return buf;
3594170531Ssam}
3595170531Ssam
3596170531Ssamstatic void
3597173275Ssamprint_chaninfo(const struct ieee80211_channel *c, int verb)
3598170531Ssam{
3599170531Ssam	char buf[14];
3600170531Ssam
3601224219Sadrian	if (verb)
3602224219Sadrian		printf("Channel %3u : %u%c%c%c%c%c MHz%-14.14s",
3603224219Sadrian		    ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
3604224219Sadrian		    IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
3605224219Sadrian		    IEEE80211_IS_CHAN_DFS(c) ? 'D' : ' ',
3606224219Sadrian		    IEEE80211_IS_CHAN_RADAR(c) ? 'R' : ' ',
3607224219Sadrian		    IEEE80211_IS_CHAN_CWINT(c) ? 'I' : ' ',
3608224219Sadrian		    IEEE80211_IS_CHAN_CACDONE(c) ? 'C' : ' ',
3609224219Sadrian		    get_chaninfo(c, verb, buf, sizeof(buf)));
3610224219Sadrian	else
3611202161Sgavin	printf("Channel %3u : %u%c MHz%-14.14s",
3612224219Sadrian	    ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
3613224219Sadrian	    IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
3614224219Sadrian	    get_chaninfo(c, verb, buf, sizeof(buf)));
3615224219Sadrian
3616138593Ssam}
3617138593Ssam
3618187844Ssamstatic int
3619187844Ssamchanpref(const struct ieee80211_channel *c)
3620187844Ssam{
3621187844Ssam	if (IEEE80211_IS_CHAN_HT40(c))
3622187844Ssam		return 40;
3623187844Ssam	if (IEEE80211_IS_CHAN_HT20(c))
3624187844Ssam		return 30;
3625187844Ssam	if (IEEE80211_IS_CHAN_HALF(c))
3626187844Ssam		return 10;
3627187844Ssam	if (IEEE80211_IS_CHAN_QUARTER(c))
3628187844Ssam		return 5;
3629187844Ssam	if (IEEE80211_IS_CHAN_TURBO(c))
3630187844Ssam		return 25;
3631187844Ssam	if (IEEE80211_IS_CHAN_A(c))
3632187844Ssam		return 20;
3633187844Ssam	if (IEEE80211_IS_CHAN_G(c))
3634187844Ssam		return 20;
3635187844Ssam	if (IEEE80211_IS_CHAN_B(c))
3636187844Ssam		return 15;
3637187844Ssam	if (IEEE80211_IS_CHAN_PUREG(c))
3638187844Ssam		return 15;
3639187844Ssam	return 0;
3640187844Ssam}
3641187844Ssam
3642138593Ssamstatic void
3643173275Ssamprint_channels(int s, const struct ieee80211req_chaninfo *chans,
3644173275Ssam	int allchans, int verb)
3645138593Ssam{
3646187801Ssam	struct ieee80211req_chaninfo *achans;
3647170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3648138593Ssam	const struct ieee80211_channel *c;
3649170531Ssam	int i, half;
3650138593Ssam
3651187801Ssam	achans = malloc(IEEE80211_CHANINFO_SPACE(chans));
3652187801Ssam	if (achans == NULL)
3653187801Ssam		errx(1, "no space for active channel list");
3654187801Ssam	achans->ic_nchans = 0;
3655170531Ssam	memset(reported, 0, sizeof(reported));
3656138593Ssam	if (!allchans) {
3657138593Ssam		struct ieee80211req_chanlist active;
3658138593Ssam
3659173275Ssam		if (get80211(s, IEEE80211_IOC_CHANLIST, &active, sizeof(active)) < 0)
3660138593Ssam			errx(1, "unable to get active channel list");
3661173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3662173275Ssam			c = &chans->ic_chans[i];
3663170531Ssam			if (!isset(active.ic_channels, c->ic_ieee))
3664170531Ssam				continue;
3665170531Ssam			/*
3666170531Ssam			 * Suppress compatible duplicates unless
3667170531Ssam			 * verbose.  The kernel gives us it's
3668170531Ssam			 * complete channel list which has separate
3669170531Ssam			 * entries for 11g/11b and 11a/turbo.
3670170531Ssam			 */
3671173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3672170531Ssam				/* XXX we assume duplicates are adjacent */
3673187801Ssam				achans->ic_chans[achans->ic_nchans-1] = *c;
3674170531Ssam			} else {
3675187801Ssam				achans->ic_chans[achans->ic_nchans++] = *c;
3676170531Ssam				setbit(reported, c->ic_ieee);
3677170531Ssam			}
3678138593Ssam		}
3679170531Ssam	} else {
3680173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3681173275Ssam			c = &chans->ic_chans[i];
3682170531Ssam			/* suppress duplicates as above */
3683173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3684170531Ssam				/* XXX we assume duplicates are adjacent */
3685187844Ssam				struct ieee80211_channel *a =
3686187844Ssam				    &achans->ic_chans[achans->ic_nchans-1];
3687187844Ssam				if (chanpref(c) > chanpref(a))
3688187844Ssam					*a = *c;
3689170531Ssam			} else {
3690187801Ssam				achans->ic_chans[achans->ic_nchans++] = *c;
3691170531Ssam				setbit(reported, c->ic_ieee);
3692170531Ssam			}
3693170531Ssam		}
3694170531Ssam	}
3695187801Ssam	half = achans->ic_nchans / 2;
3696187801Ssam	if (achans->ic_nchans % 2)
3697138593Ssam		half++;
3698170531Ssam
3699187801Ssam	for (i = 0; i < achans->ic_nchans / 2; i++) {
3700187801Ssam		print_chaninfo(&achans->ic_chans[i], verb);
3701187801Ssam		print_chaninfo(&achans->ic_chans[half+i], verb);
3702138593Ssam		printf("\n");
3703138593Ssam	}
3704187801Ssam	if (achans->ic_nchans % 2) {
3705187801Ssam		print_chaninfo(&achans->ic_chans[i], verb);
3706138593Ssam		printf("\n");
3707138593Ssam	}
3708187801Ssam	free(achans);
3709138593Ssam}
3710138593Ssam
3711138593Ssamstatic void
3712173275Ssamlist_channels(int s, int allchans)
3713173275Ssam{
3714173275Ssam	getchaninfo(s);
3715187801Ssam	print_channels(s, chaninfo, allchans, verbose);
3716173275Ssam}
3717173275Ssam
3718173275Ssamstatic void
3719170531Ssamprint_txpow(const struct ieee80211_channel *c)
3720170531Ssam{
3721202161Sgavin	printf("Channel %3u : %u MHz %3.1f reg %2d  ",
3722170531Ssam	    c->ic_ieee, c->ic_freq,
3723170531Ssam	    c->ic_maxpower/2., c->ic_maxregpower);
3724170531Ssam}
3725170531Ssam
3726170531Ssamstatic void
3727170531Ssamprint_txpow_verbose(const struct ieee80211_channel *c)
3728170531Ssam{
3729173275Ssam	print_chaninfo(c, 1);
3730170531Ssam	printf("min %4.1f dBm  max %3.1f dBm  reg %2d dBm",
3731170531Ssam	    c->ic_minpower/2., c->ic_maxpower/2., c->ic_maxregpower);
3732170531Ssam	/* indicate where regulatory cap limits power use */
3733170531Ssam	if (c->ic_maxpower > 2*c->ic_maxregpower)
3734170531Ssam		printf(" <");
3735170531Ssam}
3736170531Ssam
3737170531Ssamstatic void
3738170531Ssamlist_txpow(int s)
3739170531Ssam{
3740187801Ssam	struct ieee80211req_chaninfo *achans;
3741170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3742170531Ssam	struct ieee80211_channel *c, *prev;
3743170531Ssam	int i, half;
3744170531Ssam
3745170531Ssam	getchaninfo(s);
3746187801Ssam	achans = malloc(IEEE80211_CHANINFO_SPACE(chaninfo));
3747187801Ssam	if (achans == NULL)
3748187801Ssam		errx(1, "no space for active channel list");
3749187801Ssam	achans->ic_nchans = 0;
3750170531Ssam	memset(reported, 0, sizeof(reported));
3751187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
3752187801Ssam		c = &chaninfo->ic_chans[i];
3753170531Ssam		/* suppress duplicates as above */
3754170531Ssam		if (isset(reported, c->ic_ieee) && !verbose) {
3755170531Ssam			/* XXX we assume duplicates are adjacent */
3756187801Ssam			prev = &achans->ic_chans[achans->ic_nchans-1];
3757170531Ssam			/* display highest power on channel */
3758170531Ssam			if (c->ic_maxpower > prev->ic_maxpower)
3759170531Ssam				*prev = *c;
3760170531Ssam		} else {
3761187801Ssam			achans->ic_chans[achans->ic_nchans++] = *c;
3762170531Ssam			setbit(reported, c->ic_ieee);
3763170531Ssam		}
3764170531Ssam	}
3765170531Ssam	if (!verbose) {
3766187801Ssam		half = achans->ic_nchans / 2;
3767187801Ssam		if (achans->ic_nchans % 2)
3768170531Ssam			half++;
3769170531Ssam
3770187801Ssam		for (i = 0; i < achans->ic_nchans / 2; i++) {
3771187801Ssam			print_txpow(&achans->ic_chans[i]);
3772187801Ssam			print_txpow(&achans->ic_chans[half+i]);
3773170531Ssam			printf("\n");
3774170531Ssam		}
3775187801Ssam		if (achans->ic_nchans % 2) {
3776187801Ssam			print_txpow(&achans->ic_chans[i]);
3777170531Ssam			printf("\n");
3778170531Ssam		}
3779170531Ssam	} else {
3780187801Ssam		for (i = 0; i < achans->ic_nchans; i++) {
3781187801Ssam			print_txpow_verbose(&achans->ic_chans[i]);
3782170531Ssam			printf("\n");
3783170531Ssam		}
3784170531Ssam	}
3785187801Ssam	free(achans);
3786170531Ssam}
3787170531Ssam
3788170531Ssamstatic void
3789138593Ssamlist_keys(int s)
3790138593Ssam{
3791138593Ssam}
3792138593Ssam
3793138593Ssam#define	IEEE80211_C_BITS \
3794195618Srpaulo	"\20\1STA\002803ENCAP\7FF\10TURBOP\11IBSS\12PMGT" \
3795178354Ssam	"\13HOSTAP\14AHDEMO\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE" \
3796195618Srpaulo	"\21MONITOR\22DFS\23MBSS\30WPA1\31WPA2\32BURST\33WME\34WDS\36BGSCAN" \
3797186904Ssam	"\37TXFRAG\40TDMA"
3798138593Ssam
3799138593Ssamstatic void
3800138593Ssamlist_capabilities(int s)
3801138593Ssam{
3802187801Ssam	struct ieee80211_devcaps_req *dc;
3803138593Ssam
3804187845Ssam	if (verbose)
3805187845Ssam		dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
3806187845Ssam	else
3807187845Ssam		dc = malloc(IEEE80211_DEVCAPS_SIZE(1));
3808187801Ssam	if (dc == NULL)
3809187801Ssam		errx(1, "no space for device capabilities");
3810187845Ssam	dc->dc_chaninfo.ic_nchans = verbose ? MAXCHAN : 1;
3811187801Ssam	getdevcaps(s, dc);
3812187801Ssam	printb("drivercaps", dc->dc_drivercaps, IEEE80211_C_BITS);
3813187801Ssam	if (dc->dc_cryptocaps != 0 || verbose) {
3814178354Ssam		putchar('\n');
3815187801Ssam		printb("cryptocaps", dc->dc_cryptocaps, IEEE80211_CRYPTO_BITS);
3816178354Ssam	}
3817187801Ssam	if (dc->dc_htcaps != 0 || verbose) {
3818178354Ssam		putchar('\n');
3819187801Ssam		printb("htcaps", dc->dc_htcaps, IEEE80211_HTCAP_BITS);
3820178354Ssam	}
3821138593Ssam	putchar('\n');
3822187845Ssam	if (verbose) {
3823187845Ssam		chaninfo = &dc->dc_chaninfo;	/* XXX */
3824187845Ssam		print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, verbose);
3825187845Ssam	}
3826187801Ssam	free(dc);
3827138593Ssam}
3828138593Ssam
3829173275Ssamstatic int
3830173275Ssamget80211wme(int s, int param, int ac, int *val)
3831173275Ssam{
3832173275Ssam	struct ieee80211req ireq;
3833173275Ssam
3834173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3835299873Struckman	(void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
3836173275Ssam	ireq.i_type = param;
3837173275Ssam	ireq.i_len = ac;
3838173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3839173275Ssam		warn("cannot get WME parameter %d, ac %d%s",
3840173275Ssam		    param, ac & IEEE80211_WMEPARAM_VAL,
3841173275Ssam		    ac & IEEE80211_WMEPARAM_BSS ? " (BSS)" : "");
3842173275Ssam		return -1;
3843173275Ssam	}
3844173275Ssam	*val = ireq.i_val;
3845173275Ssam	return 0;
3846173275Ssam}
3847173275Ssam
3848138593Ssamstatic void
3849181199Ssamlist_wme_aci(int s, const char *tag, int ac)
3850138593Ssam{
3851181199Ssam	int val;
3852138593Ssam
3853181199Ssam	printf("\t%s", tag);
3854138593Ssam
3855181199Ssam	/* show WME BSS parameters */
3856181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMIN, ac, &val) != -1)
3857181199Ssam		printf(" cwmin %2u", val);
3858181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMAX, ac, &val) != -1)
3859181199Ssam		printf(" cwmax %2u", val);
3860181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_AIFS, ac, &val) != -1)
3861181199Ssam		printf(" aifs %2u", val);
3862181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_TXOPLIMIT, ac, &val) != -1)
3863181199Ssam		printf(" txopLimit %3u", val);
3864181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_ACM, ac, &val) != -1) {
3865181199Ssam		if (val)
3866181199Ssam			printf(" acm");
3867181199Ssam		else if (verbose)
3868181199Ssam			printf(" -acm");
3869181199Ssam	}
3870181199Ssam	/* !BSS only */
3871181199Ssam	if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3872181199Ssam		if (get80211wme(s, IEEE80211_IOC_WME_ACKPOLICY, ac, &val) != -1) {
3873181199Ssam			if (!val)
3874181199Ssam				printf(" -ack");
3875138593Ssam			else if (verbose)
3876181199Ssam				printf(" ack");
3877138593Ssam		}
3878181199Ssam	}
3879181199Ssam	printf("\n");
3880181199Ssam}
3881181199Ssam
3882181199Ssamstatic void
3883181199Ssamlist_wme(int s)
3884181199Ssam{
3885181199Ssam	static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
3886181199Ssam	int ac;
3887181199Ssam
3888181199Ssam	if (verbose) {
3889181199Ssam		/* display both BSS and local settings */
3890181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
3891181199Ssam	again:
3892181199Ssam			if (ac & IEEE80211_WMEPARAM_BSS)
3893181199Ssam				list_wme_aci(s, "     ", ac);
3894181199Ssam			else
3895181199Ssam				list_wme_aci(s, acnames[ac], ac);
3896181199Ssam			if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3897181199Ssam				ac |= IEEE80211_WMEPARAM_BSS;
3898181199Ssam				goto again;
3899181199Ssam			} else
3900181199Ssam				ac &= ~IEEE80211_WMEPARAM_BSS;
3901138593Ssam		}
3902181199Ssam	} else {
3903181199Ssam		/* display only channel settings */
3904181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++)
3905181199Ssam			list_wme_aci(s, acnames[ac], ac);
3906138593Ssam	}
3907138593Ssam}
3908138593Ssam
3909149029Ssamstatic void
3910178354Ssamlist_roam(int s)
3911178354Ssam{
3912178354Ssam	const struct ieee80211_roamparam *rp;
3913178354Ssam	int mode;
3914178354Ssam
3915178354Ssam	getroam(s);
3916188784Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
3917178354Ssam		rp = &roamparams.params[mode];
3918178354Ssam		if (rp->rssi == 0 && rp->rate == 0)
3919178354Ssam			continue;
3920188784Ssam		if (mode == IEEE80211_MODE_11NA || mode == IEEE80211_MODE_11NG) {
3921188784Ssam			if (rp->rssi & 1)
3922188784Ssam				LINE_CHECK("roam:%-7.7s rssi %2u.5dBm  MCS %2u    ",
3923188784Ssam				    modename[mode], rp->rssi/2,
3924188784Ssam				    rp->rate &~ IEEE80211_RATE_MCS);
3925188784Ssam			else
3926188784Ssam				LINE_CHECK("roam:%-7.7s rssi %4udBm  MCS %2u    ",
3927188784Ssam				    modename[mode], rp->rssi/2,
3928188784Ssam				    rp->rate &~ IEEE80211_RATE_MCS);
3929188784Ssam		} else {
3930188784Ssam			if (rp->rssi & 1)
3931188784Ssam				LINE_CHECK("roam:%-7.7s rssi %2u.5dBm rate %2u Mb/s",
3932188784Ssam				    modename[mode], rp->rssi/2, rp->rate/2);
3933188784Ssam			else
3934188784Ssam				LINE_CHECK("roam:%-7.7s rssi %4udBm rate %2u Mb/s",
3935188784Ssam				    modename[mode], rp->rssi/2, rp->rate/2);
3936188784Ssam		}
3937178354Ssam	}
3938178354Ssam}
3939178354Ssam
3940178354Ssamstatic void
3941178354Ssamlist_txparams(int s)
3942178354Ssam{
3943178354Ssam	const struct ieee80211_txparam *tp;
3944178354Ssam	int mode;
3945178354Ssam
3946178354Ssam	gettxparams(s);
3947188784Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
3948178354Ssam		tp = &txparams.params[mode];
3949178354Ssam		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
3950178354Ssam			continue;
3951188784Ssam		if (mode == IEEE80211_MODE_11NA || mode == IEEE80211_MODE_11NG) {
3952188784Ssam			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3953188784Ssam				LINE_CHECK("%-7.7s ucast NONE    mgmt %2u MCS  "
3954188784Ssam				    "mcast %2u MCS  maxretry %u",
3955188784Ssam				    modename[mode],
3956188784Ssam				    tp->mgmtrate &~ IEEE80211_RATE_MCS,
3957188784Ssam				    tp->mcastrate &~ IEEE80211_RATE_MCS,
3958188784Ssam				    tp->maxretry);
3959188784Ssam			else
3960188784Ssam				LINE_CHECK("%-7.7s ucast %2u MCS  mgmt %2u MCS  "
3961188784Ssam				    "mcast %2u MCS  maxretry %u",
3962188784Ssam				    modename[mode],
3963188784Ssam				    tp->ucastrate &~ IEEE80211_RATE_MCS,
3964188784Ssam				    tp->mgmtrate &~ IEEE80211_RATE_MCS,
3965188784Ssam				    tp->mcastrate &~ IEEE80211_RATE_MCS,
3966188784Ssam				    tp->maxretry);
3967188784Ssam		} else {
3968188784Ssam			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3969188784Ssam				LINE_CHECK("%-7.7s ucast NONE    mgmt %2u Mb/s "
3970188784Ssam				    "mcast %2u Mb/s maxretry %u",
3971188784Ssam				    modename[mode],
3972188784Ssam				    tp->mgmtrate/2,
3973188784Ssam				    tp->mcastrate/2, tp->maxretry);
3974188784Ssam			else
3975188784Ssam				LINE_CHECK("%-7.7s ucast %2u Mb/s mgmt %2u Mb/s "
3976188784Ssam				    "mcast %2u Mb/s maxretry %u",
3977188784Ssam				    modename[mode],
3978188784Ssam				    tp->ucastrate/2, tp->mgmtrate/2,
3979188784Ssam				    tp->mcastrate/2, tp->maxretry);
3980188784Ssam		}
3981178354Ssam	}
3982178354Ssam}
3983178354Ssam
3984178354Ssamstatic void
3985173275Ssamprintpolicy(int policy)
3986173275Ssam{
3987173275Ssam	switch (policy) {
3988173275Ssam	case IEEE80211_MACCMD_POLICY_OPEN:
3989173275Ssam		printf("policy: open\n");
3990173275Ssam		break;
3991173275Ssam	case IEEE80211_MACCMD_POLICY_ALLOW:
3992173275Ssam		printf("policy: allow\n");
3993173275Ssam		break;
3994173275Ssam	case IEEE80211_MACCMD_POLICY_DENY:
3995173275Ssam		printf("policy: deny\n");
3996173275Ssam		break;
3997178354Ssam	case IEEE80211_MACCMD_POLICY_RADIUS:
3998178354Ssam		printf("policy: radius\n");
3999178354Ssam		break;
4000173275Ssam	default:
4001173275Ssam		printf("policy: unknown (%u)\n", policy);
4002173275Ssam		break;
4003173275Ssam	}
4004173275Ssam}
4005173275Ssam
4006173275Ssamstatic void
4007149029Ssamlist_mac(int s)
4008149029Ssam{
4009149029Ssam	struct ieee80211req ireq;
4010149029Ssam	struct ieee80211req_maclist *acllist;
4011173275Ssam	int i, nacls, policy, len;
4012173275Ssam	uint8_t *data;
4013149029Ssam	char c;
4014149029Ssam
4015149029Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4016299873Struckman	(void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
4017149029Ssam	ireq.i_type = IEEE80211_IOC_MACCMD;
4018149029Ssam	ireq.i_val = IEEE80211_MACCMD_POLICY;
4019149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
4020149029Ssam		if (errno == EINVAL) {
4021149029Ssam			printf("No acl policy loaded\n");
4022149029Ssam			return;
4023149029Ssam		}
4024149029Ssam		err(1, "unable to get mac policy");
4025149029Ssam	}
4026149029Ssam	policy = ireq.i_val;
4027149029Ssam	if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
4028149029Ssam		c = '*';
4029149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
4030149029Ssam		c = '+';
4031149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
4032149029Ssam		c = '-';
4033178354Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_RADIUS) {
4034178354Ssam		c = 'r';		/* NB: should never have entries */
4035149029Ssam	} else {
4036149029Ssam		printf("policy: unknown (%u)\n", policy);
4037149029Ssam		c = '?';
4038149029Ssam	}
4039173275Ssam	if (verbose || c == '?')
4040173275Ssam		printpolicy(policy);
4041173275Ssam
4042175952Ssam	ireq.i_val = IEEE80211_MACCMD_LIST;
4043175952Ssam	ireq.i_len = 0;
4044175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4045173275Ssam		err(1, "unable to get mac acl list size");
4046175952Ssam	if (ireq.i_len == 0) {		/* NB: no acls */
4047173275Ssam		if (!(verbose || c == '?'))
4048173275Ssam			printpolicy(policy);
4049173275Ssam		return;
4050173275Ssam	}
4051175952Ssam	len = ireq.i_len;
4052173275Ssam
4053173275Ssam	data = malloc(len);
4054173275Ssam	if (data == NULL)
4055173275Ssam		err(1, "out of memory for acl list");
4056173275Ssam
4057175952Ssam	ireq.i_data = data;
4058175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4059173275Ssam		err(1, "unable to get mac acl list");
4060173275Ssam	nacls = len / sizeof(*acllist);
4061173275Ssam	acllist = (struct ieee80211req_maclist *) data;
4062149029Ssam	for (i = 0; i < nacls; i++)
4063149029Ssam		printf("%c%s\n", c, ether_ntoa(
4064149029Ssam			(const struct ether_addr *) acllist[i].ml_macaddr));
4065173275Ssam	free(data);
4066149029Ssam}
4067149029Ssam
4068178354Ssamstatic void
4069178354Ssamprint_regdomain(const struct ieee80211_regdomain *reg, int verb)
4070178354Ssam{
4071178354Ssam	if ((reg->regdomain != 0 &&
4072178354Ssam	    reg->regdomain != reg->country) || verb) {
4073178354Ssam		const struct regdomain *rd =
4074178354Ssam		    lib80211_regdomain_findbysku(getregdata(), reg->regdomain);
4075178354Ssam		if (rd == NULL)
4076178354Ssam			LINE_CHECK("regdomain %d", reg->regdomain);
4077178354Ssam		else
4078178354Ssam			LINE_CHECK("regdomain %s", rd->name);
4079178354Ssam	}
4080178354Ssam	if (reg->country != 0 || verb) {
4081178354Ssam		const struct country *cc =
4082178354Ssam		    lib80211_country_findbycc(getregdata(), reg->country);
4083178354Ssam		if (cc == NULL)
4084178354Ssam			LINE_CHECK("country %d", reg->country);
4085178354Ssam		else
4086178354Ssam			LINE_CHECK("country %s", cc->isoname);
4087178354Ssam	}
4088178354Ssam	if (reg->location == 'I')
4089178354Ssam		LINE_CHECK("indoor");
4090178354Ssam	else if (reg->location == 'O')
4091178354Ssam		LINE_CHECK("outdoor");
4092178354Ssam	else if (verb)
4093178354Ssam		LINE_CHECK("anywhere");
4094178354Ssam	if (reg->ecm)
4095178354Ssam		LINE_CHECK("ecm");
4096178354Ssam	else if (verb)
4097178354Ssam		LINE_CHECK("-ecm");
4098178354Ssam}
4099178354Ssam
4100178354Ssamstatic void
4101178354Ssamlist_regdomain(int s, int channelsalso)
4102178354Ssam{
4103178354Ssam	getregdomain(s);
4104178354Ssam	if (channelsalso) {
4105178354Ssam		getchaninfo(s);
4106178354Ssam		spacer = ':';
4107178354Ssam		print_regdomain(&regdomain, 1);
4108178354Ssam		LINE_BREAK();
4109187801Ssam		print_channels(s, chaninfo, 1/*allchans*/, 1/*verbose*/);
4110178354Ssam	} else
4111178354Ssam		print_regdomain(&regdomain, verbose);
4112178354Ssam}
4113178354Ssam
4114195618Srpaulostatic void
4115195618Srpaulolist_mesh(int s)
4116195618Srpaulo{
4117195618Srpaulo	struct ieee80211req ireq;
4118195618Srpaulo	struct ieee80211req_mesh_route routes[128];
4119195908Srpaulo	struct ieee80211req_mesh_route *rt;
4120195618Srpaulo
4121195618Srpaulo	(void) memset(&ireq, 0, sizeof(ireq));
4122299873Struckman	(void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
4123195618Srpaulo	ireq.i_type = IEEE80211_IOC_MESH_RTCMD;
4124195618Srpaulo	ireq.i_val = IEEE80211_MESH_RTCMD_LIST;
4125195618Srpaulo	ireq.i_data = &routes;
4126195618Srpaulo	ireq.i_len = sizeof(routes);
4127195618Srpaulo	if (ioctl(s, SIOCG80211, &ireq) < 0)
4128195618Srpaulo	 	err(1, "unable to get the Mesh routing table");
4129195618Srpaulo
4130195908Srpaulo	printf("%-17.17s %-17.17s %4s %4s %4s %6s %s\n"
4131195618Srpaulo		, "DEST"
4132195618Srpaulo		, "NEXT HOP"
4133195618Srpaulo		, "HOPS"
4134195618Srpaulo		, "METRIC"
4135195784Srpaulo		, "LIFETIME"
4136195908Srpaulo		, "MSEQ"
4137195908Srpaulo		, "FLAGS");
4138195618Srpaulo
4139195908Srpaulo	for (rt = &routes[0]; rt - &routes[0] < ireq.i_len / sizeof(*rt); rt++){
4140195618Srpaulo		printf("%s ",
4141195908Srpaulo		    ether_ntoa((const struct ether_addr *)rt->imr_dest));
4142195908Srpaulo		printf("%s %4u   %4u   %6u %6u    %c%c\n",
4143195908Srpaulo			ether_ntoa((const struct ether_addr *)rt->imr_nexthop),
4144195908Srpaulo			rt->imr_nhops, rt->imr_metric, rt->imr_lifetime,
4145195908Srpaulo			rt->imr_lastmseq,
4146234895Smonthadar			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_DISCOVER) ?
4147234895Smonthadar			    'D' :
4148195908Srpaulo			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_VALID) ?
4149195908Srpaulo			    'V' : '!',
4150195908Srpaulo			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_PROXY) ?
4151246505Smonthadar			    'P' :
4152246505Smonthadar			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_GATE) ?
4153246505Smonthadar			    'G' :' ');
4154195618Srpaulo	}
4155195618Srpaulo}
4156195618Srpaulo
4157138593Ssamstatic
4158138593SsamDECL_CMD_FUNC(set80211list, arg, d)
4159138593Ssam{
4160138593Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
4161138593Ssam
4162173275Ssam	LINE_INIT('\t');
4163173275Ssam
4164138593Ssam	if (iseq(arg, "sta"))
4165138593Ssam		list_stations(s);
4166138593Ssam	else if (iseq(arg, "scan") || iseq(arg, "ap"))
4167138593Ssam		list_scan(s);
4168138593Ssam	else if (iseq(arg, "chan") || iseq(arg, "freq"))
4169138593Ssam		list_channels(s, 1);
4170138593Ssam	else if (iseq(arg, "active"))
4171138593Ssam		list_channels(s, 0);
4172138593Ssam	else if (iseq(arg, "keys"))
4173138593Ssam		list_keys(s);
4174138593Ssam	else if (iseq(arg, "caps"))
4175138593Ssam		list_capabilities(s);
4176178354Ssam	else if (iseq(arg, "wme") || iseq(arg, "wmm"))
4177138593Ssam		list_wme(s);
4178149029Ssam	else if (iseq(arg, "mac"))
4179149029Ssam		list_mac(s);
4180170531Ssam	else if (iseq(arg, "txpow"))
4181170531Ssam		list_txpow(s);
4182178354Ssam	else if (iseq(arg, "roam"))
4183178354Ssam		list_roam(s);
4184178354Ssam	else if (iseq(arg, "txparam") || iseq(arg, "txparm"))
4185178354Ssam		list_txparams(s);
4186178354Ssam	else if (iseq(arg, "regdomain"))
4187178354Ssam		list_regdomain(s, 1);
4188178354Ssam	else if (iseq(arg, "countries"))
4189178354Ssam		list_countries();
4190195618Srpaulo	else if (iseq(arg, "mesh"))
4191195618Srpaulo		list_mesh(s);
4192138593Ssam	else
4193138593Ssam		errx(1, "Don't know how to list %s for %s", arg, name);
4194178354Ssam	LINE_BREAK();
4195138593Ssam#undef iseq
4196138593Ssam}
4197138593Ssam
4198138593Ssamstatic enum ieee80211_opmode
4199138593Ssamget80211opmode(int s)
4200138593Ssam{
4201138593Ssam	struct ifmediareq ifmr;
4202138593Ssam
4203138593Ssam	(void) memset(&ifmr, 0, sizeof(ifmr));
4204299873Struckman	(void) strlcpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
4205138593Ssam
4206138593Ssam	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
4207186101Ssam		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) {
4208186101Ssam			if (ifmr.ifm_current & IFM_FLAG0)
4209186101Ssam				return IEEE80211_M_AHDEMO;
4210186101Ssam			else
4211186101Ssam				return IEEE80211_M_IBSS;
4212186101Ssam		}
4213138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
4214138593Ssam			return IEEE80211_M_HOSTAP;
4215291352Sadrian		if (ifmr.ifm_current & IFM_IEEE80211_IBSS)
4216291352Sadrian			return IEEE80211_M_IBSS;
4217138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
4218138593Ssam			return IEEE80211_M_MONITOR;
4219195618Srpaulo		if (ifmr.ifm_current & IFM_IEEE80211_MBSS)
4220195618Srpaulo			return IEEE80211_M_MBSS;
4221138593Ssam	}
4222138593Ssam	return IEEE80211_M_STA;
4223138593Ssam}
4224138593Ssam
4225138593Ssam#if 0
4226138593Ssamstatic void
4227138593Ssamprintcipher(int s, struct ieee80211req *ireq, int keylenop)
4228138593Ssam{
4229138593Ssam	switch (ireq->i_val) {
4230138593Ssam	case IEEE80211_CIPHER_WEP:
4231138593Ssam		ireq->i_type = keylenop;
4232138593Ssam		if (ioctl(s, SIOCG80211, ireq) != -1)
4233138593Ssam			printf("WEP-%s",
4234138593Ssam			    ireq->i_len <= 5 ? "40" :
4235138593Ssam			    ireq->i_len <= 13 ? "104" : "128");
4236138593Ssam		else
4237138593Ssam			printf("WEP");
4238138593Ssam		break;
4239138593Ssam	case IEEE80211_CIPHER_TKIP:
4240138593Ssam		printf("TKIP");
4241138593Ssam		break;
4242138593Ssam	case IEEE80211_CIPHER_AES_OCB:
4243138593Ssam		printf("AES-OCB");
4244138593Ssam		break;
4245138593Ssam	case IEEE80211_CIPHER_AES_CCM:
4246138593Ssam		printf("AES-CCM");
4247138593Ssam		break;
4248138593Ssam	case IEEE80211_CIPHER_CKIP:
4249138593Ssam		printf("CKIP");
4250138593Ssam		break;
4251138593Ssam	case IEEE80211_CIPHER_NONE:
4252138593Ssam		printf("NONE");
4253138593Ssam		break;
4254138593Ssam	default:
4255138593Ssam		printf("UNKNOWN (0x%x)", ireq->i_val);
4256138593Ssam		break;
4257138593Ssam	}
4258138593Ssam}
4259138593Ssam#endif
4260138593Ssam
4261155931Ssamstatic void
4262138593Ssamprintkey(const struct ieee80211req_key *ik)
4263138593Ssam{
4264138593Ssam	static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
4265298556Spfg	u_int keylen = ik->ik_keylen;
4266138593Ssam	int printcontents;
4267138593Ssam
4268148001Srwatson	printcontents = printkeys &&
4269138593Ssam		(memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
4270138593Ssam	if (printcontents)
4271138593Ssam		LINE_BREAK();
4272138593Ssam	switch (ik->ik_type) {
4273138593Ssam	case IEEE80211_CIPHER_WEP:
4274138593Ssam		/* compatibility */
4275155931Ssam		LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
4276138593Ssam		    keylen <= 5 ? "40-bit" :
4277138593Ssam		    keylen <= 13 ? "104-bit" : "128-bit");
4278138593Ssam		break;
4279138593Ssam	case IEEE80211_CIPHER_TKIP:
4280138593Ssam		if (keylen > 128/8)
4281138593Ssam			keylen -= 128/8;	/* ignore MIC for now */
4282155931Ssam		LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4283138593Ssam		break;
4284138593Ssam	case IEEE80211_CIPHER_AES_OCB:
4285155931Ssam		LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4286138593Ssam		break;
4287138593Ssam	case IEEE80211_CIPHER_AES_CCM:
4288155931Ssam		LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4289138593Ssam		break;
4290138593Ssam	case IEEE80211_CIPHER_CKIP:
4291155931Ssam		LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4292138593Ssam		break;
4293138593Ssam	case IEEE80211_CIPHER_NONE:
4294155931Ssam		LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4295138593Ssam		break;
4296138593Ssam	default:
4297155931Ssam		LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
4298138593Ssam			ik->ik_type, ik->ik_keyix+1, 8*keylen);
4299138593Ssam		break;
4300138593Ssam	}
4301138593Ssam	if (printcontents) {
4302298556Spfg		u_int i;
4303138593Ssam
4304138593Ssam		printf(" <");
4305138593Ssam		for (i = 0; i < keylen; i++)
4306138593Ssam			printf("%02x", ik->ik_keydata[i]);
4307138593Ssam		printf(">");
4308138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4309138593Ssam		    (ik->ik_keyrsc != 0 || verbose))
4310146873Sjhb			printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
4311138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4312138593Ssam		    (ik->ik_keytsc != 0 || verbose))
4313146873Sjhb			printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
4314138593Ssam		if (ik->ik_flags != 0 && verbose) {
4315138593Ssam			const char *sep = " ";
4316138593Ssam
4317138593Ssam			if (ik->ik_flags & IEEE80211_KEY_XMIT)
4318138593Ssam				printf("%stx", sep), sep = "+";
4319138593Ssam			if (ik->ik_flags & IEEE80211_KEY_RECV)
4320138593Ssam				printf("%srx", sep), sep = "+";
4321138593Ssam			if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
4322138593Ssam				printf("%sdef", sep), sep = "+";
4323138593Ssam		}
4324138593Ssam		LINE_BREAK();
4325138593Ssam	}
4326138593Ssam}
4327138593Ssam
4328138593Ssamstatic void
4329173275Ssamprintrate(const char *tag, int v, int defrate, int defmcs)
4330138593Ssam{
4331188784Ssam	if ((v & IEEE80211_RATE_MCS) == 0) {
4332188784Ssam		if (v != defrate) {
4333188784Ssam			if (v & 1)
4334188784Ssam				LINE_CHECK("%s %d.5", tag, v/2);
4335188784Ssam			else
4336188784Ssam				LINE_CHECK("%s %d", tag, v/2);
4337188784Ssam		}
4338188784Ssam	} else {
4339173275Ssam		if (v != defmcs)
4340173275Ssam			LINE_CHECK("%s %d", tag, v &~ 0x80);
4341173275Ssam	}
4342173275Ssam}
4343173275Ssam
4344173275Ssamstatic int
4345195618Srpaulogetid(int s, int ix, void *data, size_t len, int *plen, int mesh)
4346173275Ssam{
4347138593Ssam	struct ieee80211req ireq;
4348138593Ssam
4349138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4350299873Struckman	(void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
4351195618Srpaulo	ireq.i_type = (!mesh) ? IEEE80211_IOC_SSID : IEEE80211_IOC_MESH_ID;
4352173275Ssam	ireq.i_val = ix;
4353173275Ssam	ireq.i_data = data;
4354173275Ssam	ireq.i_len = len;
4355173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4356173275Ssam		return -1;
4357173275Ssam	*plen = ireq.i_len;
4358173275Ssam	return 0;
4359173275Ssam}
436077218Sphk
4361173275Ssamstatic void
4362173275Ssamieee80211_status(int s)
4363173275Ssam{
4364173275Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
4365173275Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
4366173275Ssam	int i, num, wpa, wme, bgscan, bgscaninterval, val, len, wepmode;
4367173275Ssam	uint8_t data[32];
4368173275Ssam	const struct ieee80211_channel *c;
4369178354Ssam	const struct ieee80211_roamparam *rp;
4370178354Ssam	const struct ieee80211_txparam *tp;
4371173275Ssam
4372195618Srpaulo	if (getid(s, -1, data, sizeof(data), &len, 0) < 0) {
4373148686Sstefanf		/* If we can't get the SSID, this isn't an 802.11 device. */
437477218Sphk		return;
437577218Sphk	}
4376173275Ssam
4377173275Ssam	/*
4378173275Ssam	 * Invalidate cached state so printing status for multiple
4379173275Ssam	 * if's doesn't reuse the first interfaces' cached state.
4380173275Ssam	 */
4381173275Ssam	gotcurchan = 0;
4382178354Ssam	gotroam = 0;
4383178354Ssam	gottxparams = 0;
4384173275Ssam	gothtconf = 0;
4385178354Ssam	gotregdomain = 0;
4386173275Ssam
4387195618Srpaulo	printf("\t");
4388195618Srpaulo	if (opmode == IEEE80211_M_MBSS) {
4389195618Srpaulo		printf("meshid ");
4390195618Srpaulo		getid(s, 0, data, sizeof(data), &len, 1);
4391195618Srpaulo		print_string(data, len);
4392195618Srpaulo	} else {
4393195618Srpaulo		if (get80211val(s, IEEE80211_IOC_NUMSSIDS, &num) < 0)
4394195618Srpaulo			num = 0;
4395195618Srpaulo		printf("ssid ");
4396195618Srpaulo		if (num > 1) {
4397195618Srpaulo			for (i = 0; i < num; i++) {
4398195618Srpaulo				if (getid(s, i, data, sizeof(data), &len, 0) >= 0 && len > 0) {
4399195618Srpaulo					printf(" %d:", i + 1);
4400195618Srpaulo					print_string(data, len);
4401195618Srpaulo				}
4402138593Ssam			}
4403195618Srpaulo		} else
4404195618Srpaulo			print_string(data, len);
4405195618Srpaulo	}
4406173275Ssam	c = getcurchan(s);
4407170531Ssam	if (c->ic_freq != IEEE80211_CHAN_ANY) {
4408170531Ssam		char buf[14];
4409202161Sgavin		printf(" channel %d (%u MHz%s)", c->ic_ieee, c->ic_freq,
4410170531Ssam			get_chaninfo(c, 1, buf, sizeof(buf)));
4411138593Ssam	} else if (verbose)
4412138593Ssam		printf(" channel UNDEF");
4413138593Ssam
4414173275Ssam	if (get80211(s, IEEE80211_IOC_BSSID, data, IEEE80211_ADDR_LEN) >= 0 &&
4415173275Ssam	    (memcmp(data, zerobssid, sizeof(zerobssid)) != 0 || verbose))
4416173275Ssam		printf(" bssid %s", ether_ntoa((struct ether_addr *)data));
4417138593Ssam
4418173275Ssam	if (get80211len(s, IEEE80211_IOC_STATIONNAME, data, sizeof(data), &len) != -1) {
4419138593Ssam		printf("\n\tstationname ");
4420173275Ssam		print_string(data, len);
442177218Sphk	}
442277218Sphk
4423138593Ssam	spacer = ' ';		/* force first break */
4424138593Ssam	LINE_BREAK();
442577218Sphk
4426178354Ssam	list_regdomain(s, 0);
4427178354Ssam
4428173275Ssam	wpa = 0;
4429173275Ssam	if (get80211val(s, IEEE80211_IOC_AUTHMODE, &val) != -1) {
4430173275Ssam		switch (val) {
4431173275Ssam		case IEEE80211_AUTH_NONE:
4432173275Ssam			LINE_CHECK("authmode NONE");
4433173275Ssam			break;
4434173275Ssam		case IEEE80211_AUTH_OPEN:
4435173275Ssam			LINE_CHECK("authmode OPEN");
4436173275Ssam			break;
4437173275Ssam		case IEEE80211_AUTH_SHARED:
4438173275Ssam			LINE_CHECK("authmode SHARED");
4439173275Ssam			break;
4440173275Ssam		case IEEE80211_AUTH_8021X:
4441173275Ssam			LINE_CHECK("authmode 802.1x");
4442173275Ssam			break;
4443173275Ssam		case IEEE80211_AUTH_WPA:
4444173275Ssam			if (get80211val(s, IEEE80211_IOC_WPA, &wpa) < 0)
4445173275Ssam				wpa = 1;	/* default to WPA1 */
4446173275Ssam			switch (wpa) {
4447173275Ssam			case 2:
4448173275Ssam				LINE_CHECK("authmode WPA2/802.11i");
444977218Sphk				break;
4450173275Ssam			case 3:
4451173275Ssam				LINE_CHECK("authmode WPA1+WPA2/802.11i");
445277218Sphk				break;
4453127649Ssam			default:
4454173275Ssam				LINE_CHECK("authmode WPA");
4455127649Ssam				break;
4456173275Ssam			}
4457173275Ssam			break;
4458173275Ssam		case IEEE80211_AUTH_AUTO:
4459173275Ssam			LINE_CHECK("authmode AUTO");
4460173275Ssam			break;
4461173275Ssam		default:
4462173275Ssam			LINE_CHECK("authmode UNKNOWN (0x%x)", val);
4463173275Ssam			break;
4464127649Ssam		}
4465127649Ssam	}
4466127649Ssam
4467173275Ssam	if (wpa || verbose) {
4468178354Ssam		if (get80211val(s, IEEE80211_IOC_WPS, &val) != -1) {
4469178354Ssam			if (val)
4470178354Ssam				LINE_CHECK("wps");
4471178354Ssam			else if (verbose)
4472178354Ssam				LINE_CHECK("-wps");
4473178354Ssam		}
4474178354Ssam		if (get80211val(s, IEEE80211_IOC_TSN, &val) != -1) {
4475178354Ssam			if (val)
4476178354Ssam				LINE_CHECK("tsn");
4477178354Ssam			else if (verbose)
4478178354Ssam				LINE_CHECK("-tsn");
4479178354Ssam		}
4480173275Ssam		if (ioctl(s, IEEE80211_IOC_COUNTERMEASURES, &val) != -1) {
4481173275Ssam			if (val)
4482173275Ssam				LINE_CHECK("countermeasures");
4483173275Ssam			else if (verbose)
4484173275Ssam				LINE_CHECK("-countermeasures");
4485173275Ssam		}
4486178354Ssam#if 0
4487178354Ssam		/* XXX not interesting with WPA done in user space */
4488178354Ssam		ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
4489178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4490178354Ssam		}
4491178354Ssam
4492178354Ssam		ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
4493178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4494178354Ssam			LINE_CHECK("mcastcipher ");
4495178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
4496178354Ssam			spacer = ' ';
4497178354Ssam		}
4498178354Ssam
4499178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
4500178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4501178354Ssam			LINE_CHECK("ucastcipher ");
4502178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
4503178354Ssam		}
4504178354Ssam
4505178354Ssam		if (wpa & 2) {
4506178354Ssam			ireq.i_type = IEEE80211_IOC_RSNCAPS;
4507178354Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1) {
4508178354Ssam				LINE_CHECK("RSN caps 0x%x", ireq.i_val);
4509178354Ssam				spacer = ' ';
4510178354Ssam			}
4511178354Ssam		}
4512178354Ssam
4513178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
4514178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4515178354Ssam		}
4516178354Ssam#endif
4517173275Ssam	}
4518138593Ssam
4519173275Ssam	if (get80211val(s, IEEE80211_IOC_WEP, &wepmode) != -1 &&
4520173275Ssam	    wepmode != IEEE80211_WEP_NOSUP) {
4521173275Ssam
4522138718Ssam		switch (wepmode) {
4523173275Ssam		case IEEE80211_WEP_OFF:
4524173275Ssam			LINE_CHECK("privacy OFF");
4525173275Ssam			break;
4526173275Ssam		case IEEE80211_WEP_ON:
4527173275Ssam			LINE_CHECK("privacy ON");
4528173275Ssam			break;
4529173275Ssam		case IEEE80211_WEP_MIXED:
4530173275Ssam			LINE_CHECK("privacy MIXED");
4531173275Ssam			break;
4532173275Ssam		default:
4533173275Ssam			LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
4534173275Ssam			break;
453577218Sphk		}
453677218Sphk
453777218Sphk		/*
453877218Sphk		 * If we get here then we've got WEP support so we need
453977218Sphk		 * to print WEP status.
454091454Sbrooks		 */
454177218Sphk
4542173275Ssam		if (get80211val(s, IEEE80211_IOC_WEPTXKEY, &val) < 0) {
454377218Sphk			warn("WEP support, but no tx key!");
454477218Sphk			goto end;
454577218Sphk		}
4546173275Ssam		if (val != -1)
4547173275Ssam			LINE_CHECK("deftxkey %d", val+1);
4548138718Ssam		else if (wepmode != IEEE80211_WEP_OFF || verbose)
4549155931Ssam			LINE_CHECK("deftxkey UNDEF");
455077218Sphk
4551173275Ssam		if (get80211val(s, IEEE80211_IOC_NUMWEPKEYS, &num) < 0) {
455277218Sphk			warn("WEP support, but no NUMWEPKEYS support!");
455377218Sphk			goto end;
455477218Sphk		}
455577218Sphk
4556138593Ssam		for (i = 0; i < num; i++) {
4557138593Ssam			struct ieee80211req_key ik;
455877218Sphk
4559138593Ssam			memset(&ik, 0, sizeof(ik));
4560138593Ssam			ik.ik_keyix = i;
4561173275Ssam			if (get80211(s, IEEE80211_IOC_WPAKEY, &ik, sizeof(ik)) < 0) {
456277218Sphk				warn("WEP support, but can get keys!");
456377218Sphk				goto end;
456477218Sphk			}
4565138593Ssam			if (ik.ik_keylen != 0) {
4566138593Ssam				if (verbose)
4567138593Ssam					LINE_BREAK();
4568138593Ssam				printkey(&ik);
4569138593Ssam			}
4570138593Ssam		}
4571173275Ssamend:
4572173275Ssam		;
4573138593Ssam	}
4574138593Ssam
4575173275Ssam	if (get80211val(s, IEEE80211_IOC_POWERSAVE, &val) != -1 &&
4576173275Ssam	    val != IEEE80211_POWERSAVE_NOSUP ) {
4577173275Ssam		if (val != IEEE80211_POWERSAVE_OFF || verbose) {
4578173275Ssam			switch (val) {
4579173275Ssam			case IEEE80211_POWERSAVE_OFF:
4580173275Ssam				LINE_CHECK("powersavemode OFF");
4581173275Ssam				break;
4582173275Ssam			case IEEE80211_POWERSAVE_CAM:
4583173275Ssam				LINE_CHECK("powersavemode CAM");
4584173275Ssam				break;
4585173275Ssam			case IEEE80211_POWERSAVE_PSP:
4586173275Ssam				LINE_CHECK("powersavemode PSP");
4587173275Ssam				break;
4588173275Ssam			case IEEE80211_POWERSAVE_PSP_CAM:
4589173275Ssam				LINE_CHECK("powersavemode PSP-CAM");
4590173275Ssam				break;
4591138593Ssam			}
4592173275Ssam			if (get80211val(s, IEEE80211_IOC_POWERSAVESLEEP, &val) != -1)
4593173275Ssam				LINE_CHECK("powersavesleep %d", val);
4594138593Ssam		}
4595138593Ssam	}
4596138593Ssam
4597173275Ssam	if (get80211val(s, IEEE80211_IOC_TXPOWER, &val) != -1) {
4598173275Ssam		if (val & 1)
4599173275Ssam			LINE_CHECK("txpower %d.5", val/2);
4600173275Ssam		else
4601173275Ssam			LINE_CHECK("txpower %d", val/2);
4602173275Ssam	}
4603138593Ssam	if (verbose) {
4604173275Ssam		if (get80211val(s, IEEE80211_IOC_TXPOWMAX, &val) != -1)
4605173275Ssam			LINE_CHECK("txpowmax %.1f", val/2.);
4606138593Ssam	}
4607138593Ssam
4608178354Ssam	if (get80211val(s, IEEE80211_IOC_DOTD, &val) != -1) {
4609178354Ssam		if (val)
4610178354Ssam			LINE_CHECK("dotd");
4611178354Ssam		else if (verbose)
4612178354Ssam			LINE_CHECK("-dotd");
4613178354Ssam	}
4614178354Ssam
4615173275Ssam	if (get80211val(s, IEEE80211_IOC_RTSTHRESHOLD, &val) != -1) {
4616173275Ssam		if (val != IEEE80211_RTS_MAX || verbose)
4617173275Ssam			LINE_CHECK("rtsthreshold %d", val);
4618138593Ssam	}
4619138593Ssam
4620173275Ssam	if (get80211val(s, IEEE80211_IOC_FRAGTHRESHOLD, &val) != -1) {
4621173275Ssam		if (val != IEEE80211_FRAG_MAX || verbose)
4622173275Ssam			LINE_CHECK("fragthreshold %d", val);
4623170531Ssam	}
4624173275Ssam	if (opmode == IEEE80211_M_STA || verbose) {
4625173275Ssam		if (get80211val(s, IEEE80211_IOC_BMISSTHRESHOLD, &val) != -1) {
4626173275Ssam			if (val != IEEE80211_HWBMISS_MAX || verbose)
4627173275Ssam				LINE_CHECK("bmiss %d", val);
4628153354Ssam		}
4629153354Ssam	}
4630153354Ssam
4631178354Ssam	if (!verbose) {
4632178354Ssam		gettxparams(s);
4633178354Ssam		tp = &txparams.params[chan2mode(c)];
4634178354Ssam		printrate("ucastrate", tp->ucastrate,
4635178354Ssam		    IEEE80211_FIXED_RATE_NONE, IEEE80211_FIXED_RATE_NONE);
4636188784Ssam		printrate("mcastrate", tp->mcastrate, 2*1,
4637188784Ssam		    IEEE80211_RATE_MCS|0);
4638188784Ssam		printrate("mgmtrate", tp->mgmtrate, 2*1,
4639188784Ssam		    IEEE80211_RATE_MCS|0);
4640178354Ssam		if (tp->maxretry != 6)		/* XXX */
4641178354Ssam			LINE_CHECK("maxretry %d", tp->maxretry);
4642178354Ssam	} else {
4643178354Ssam		LINE_BREAK();
4644178354Ssam		list_txparams(s);
4645178354Ssam	}
4646170531Ssam
4647173275Ssam	bgscaninterval = -1;
4648173275Ssam	(void) get80211val(s, IEEE80211_IOC_BGSCAN_INTERVAL, &bgscaninterval);
4649173275Ssam
4650173275Ssam	if (get80211val(s, IEEE80211_IOC_SCANVALID, &val) != -1) {
4651173275Ssam		if (val != bgscaninterval || verbose)
4652173275Ssam			LINE_CHECK("scanvalid %u", val);
4653148416Ssam	}
4654148416Ssam
4655173275Ssam	bgscan = 0;
4656173275Ssam	if (get80211val(s, IEEE80211_IOC_BGSCAN, &bgscan) != -1) {
4657173275Ssam		if (bgscan)
4658170531Ssam			LINE_CHECK("bgscan");
4659170531Ssam		else if (verbose)
4660170531Ssam			LINE_CHECK("-bgscan");
4661160687Ssam	}
4662170531Ssam	if (bgscan || verbose) {
4663170531Ssam		if (bgscaninterval != -1)
4664170531Ssam			LINE_CHECK("bgscanintvl %u", bgscaninterval);
4665173275Ssam		if (get80211val(s, IEEE80211_IOC_BGSCAN_IDLE, &val) != -1)
4666173275Ssam			LINE_CHECK("bgscanidle %u", val);
4667178354Ssam		if (!verbose) {
4668178354Ssam			getroam(s);
4669178354Ssam			rp = &roamparams.params[chan2mode(c)];
4670178354Ssam			if (rp->rssi & 1)
4671178354Ssam				LINE_CHECK("roam:rssi %u.5", rp->rssi/2);
4672178354Ssam			else
4673178354Ssam				LINE_CHECK("roam:rssi %u", rp->rssi/2);
4674178354Ssam			LINE_CHECK("roam:rate %u", rp->rate/2);
4675178354Ssam		} else {
4676178354Ssam			LINE_BREAK();
4677178354Ssam			list_roam(s);
4678205514Srpaulo			LINE_BREAK();
4679170531Ssam		}
4680170531Ssam	}
4681160687Ssam
4682165570Ssam	if (IEEE80211_IS_CHAN_ANYG(c) || verbose) {
4683173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREG, &val) != -1) {
4684173275Ssam			if (val)
4685155931Ssam				LINE_CHECK("pureg");
4686147795Ssam			else if (verbose)
4687155931Ssam				LINE_CHECK("-pureg");
4688147795Ssam		}
4689173275Ssam		if (get80211val(s, IEEE80211_IOC_PROTMODE, &val) != -1) {
4690173275Ssam			switch (val) {
4691173275Ssam			case IEEE80211_PROTMODE_OFF:
4692173275Ssam				LINE_CHECK("protmode OFF");
4693173275Ssam				break;
4694173275Ssam			case IEEE80211_PROTMODE_CTS:
4695173275Ssam				LINE_CHECK("protmode CTS");
4696173275Ssam				break;
4697173275Ssam			case IEEE80211_PROTMODE_RTSCTS:
4698173275Ssam				LINE_CHECK("protmode RTSCTS");
4699173275Ssam				break;
4700173275Ssam			default:
4701173275Ssam				LINE_CHECK("protmode UNKNOWN (0x%x)", val);
4702173275Ssam				break;
4703138593Ssam			}
4704138593Ssam		}
4705138593Ssam	}
4706138593Ssam
4707173275Ssam	if (IEEE80211_IS_CHAN_HT(c) || verbose) {
4708173275Ssam		gethtconf(s);
4709173275Ssam		switch (htconf & 3) {
4710173275Ssam		case 0:
4711173275Ssam		case 2:
4712173275Ssam			LINE_CHECK("-ht");
4713173275Ssam			break;
4714173275Ssam		case 1:
4715173275Ssam			LINE_CHECK("ht20");
4716173275Ssam			break;
4717173275Ssam		case 3:
4718173275Ssam			if (verbose)
4719173275Ssam				LINE_CHECK("ht");
4720173275Ssam			break;
4721173275Ssam		}
4722173275Ssam		if (get80211val(s, IEEE80211_IOC_HTCOMPAT, &val) != -1) {
4723173275Ssam			if (!val)
4724173275Ssam				LINE_CHECK("-htcompat");
4725173275Ssam			else if (verbose)
4726173275Ssam				LINE_CHECK("htcompat");
4727173275Ssam		}
4728173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU, &val) != -1) {
4729173275Ssam			switch (val) {
4730173275Ssam			case 0:
4731173275Ssam				LINE_CHECK("-ampdu");
4732173275Ssam				break;
4733173275Ssam			case 1:
4734173275Ssam				LINE_CHECK("ampdutx -ampdurx");
4735173275Ssam				break;
4736173275Ssam			case 2:
4737173275Ssam				LINE_CHECK("-ampdutx ampdurx");
4738173275Ssam				break;
4739173275Ssam			case 3:
4740173275Ssam				if (verbose)
4741173275Ssam					LINE_CHECK("ampdu");
4742173275Ssam				break;
4743173275Ssam			}
4744173275Ssam		}
4745173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_LIMIT, &val) != -1) {
4746173275Ssam			switch (val) {
4747173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_8K:
4748173275Ssam				LINE_CHECK("ampdulimit 8k");
4749173275Ssam				break;
4750173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_16K:
4751173275Ssam				LINE_CHECK("ampdulimit 16k");
4752173275Ssam				break;
4753173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_32K:
4754173275Ssam				LINE_CHECK("ampdulimit 32k");
4755173275Ssam				break;
4756173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_64K:
4757173275Ssam				LINE_CHECK("ampdulimit 64k");
4758173275Ssam				break;
4759173275Ssam			}
4760173275Ssam		}
4761173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_DENSITY, &val) != -1) {
4762173275Ssam			switch (val) {
4763173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_NA:
4764173275Ssam				if (verbose)
4765183260Ssam					LINE_CHECK("ampdudensity NA");
4766173275Ssam				break;
4767173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_025:
4768173275Ssam				LINE_CHECK("ampdudensity .25");
4769173275Ssam				break;
4770173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_05:
4771173275Ssam				LINE_CHECK("ampdudensity .5");
4772173275Ssam				break;
4773173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_1:
4774173275Ssam				LINE_CHECK("ampdudensity 1");
4775173275Ssam				break;
4776173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_2:
4777173275Ssam				LINE_CHECK("ampdudensity 2");
4778173275Ssam				break;
4779173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_4:
4780173275Ssam				LINE_CHECK("ampdudensity 4");
4781173275Ssam				break;
4782173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_8:
4783173275Ssam				LINE_CHECK("ampdudensity 8");
4784173275Ssam				break;
4785173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_16:
4786173275Ssam				LINE_CHECK("ampdudensity 16");
4787173275Ssam				break;
4788173275Ssam			}
4789173275Ssam		}
4790173275Ssam		if (get80211val(s, IEEE80211_IOC_AMSDU, &val) != -1) {
4791173275Ssam			switch (val) {
4792173275Ssam			case 0:
4793173275Ssam				LINE_CHECK("-amsdu");
4794173275Ssam				break;
4795173275Ssam			case 1:
4796173275Ssam				LINE_CHECK("amsdutx -amsdurx");
4797173275Ssam				break;
4798173275Ssam			case 2:
4799173275Ssam				LINE_CHECK("-amsdutx amsdurx");
4800173275Ssam				break;
4801173275Ssam			case 3:
4802173275Ssam				if (verbose)
4803173275Ssam					LINE_CHECK("amsdu");
4804173275Ssam				break;
4805173275Ssam			}
4806173275Ssam		}
4807173275Ssam		/* XXX amsdu limit */
4808173275Ssam		if (get80211val(s, IEEE80211_IOC_SHORTGI, &val) != -1) {
4809173275Ssam			if (val)
4810173275Ssam				LINE_CHECK("shortgi");
4811173275Ssam			else if (verbose)
4812173275Ssam				LINE_CHECK("-shortgi");
4813173275Ssam		}
4814173275Ssam		if (get80211val(s, IEEE80211_IOC_HTPROTMODE, &val) != -1) {
4815173275Ssam			if (val == IEEE80211_PROTMODE_OFF)
4816173275Ssam				LINE_CHECK("htprotmode OFF");
4817173275Ssam			else if (val != IEEE80211_PROTMODE_RTSCTS)
4818173275Ssam				LINE_CHECK("htprotmode UNKNOWN (0x%x)", val);
4819173275Ssam			else if (verbose)
4820173275Ssam				LINE_CHECK("htprotmode RTSCTS");
4821173275Ssam		}
4822173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREN, &val) != -1) {
4823173275Ssam			if (val)
4824173275Ssam				LINE_CHECK("puren");
4825173275Ssam			else if (verbose)
4826173275Ssam				LINE_CHECK("-puren");
4827173275Ssam		}
4828183261Ssam		if (get80211val(s, IEEE80211_IOC_SMPS, &val) != -1) {
4829183261Ssam			if (val == IEEE80211_HTCAP_SMPS_DYNAMIC)
4830183261Ssam				LINE_CHECK("smpsdyn");
4831183261Ssam			else if (val == IEEE80211_HTCAP_SMPS_ENA)
4832183261Ssam				LINE_CHECK("smps");
4833183261Ssam			else if (verbose)
4834183261Ssam				LINE_CHECK("-smps");
4835183261Ssam		}
4836183261Ssam		if (get80211val(s, IEEE80211_IOC_RIFS, &val) != -1) {
4837183261Ssam			if (val)
4838183261Ssam				LINE_CHECK("rifs");
4839183261Ssam			else if (verbose)
4840183261Ssam				LINE_CHECK("-rifs");
4841183261Ssam		}
4842298606Sadrian		if (get80211val(s, IEEE80211_IOC_STBC, &val) != -1) {
4843298606Sadrian			switch (val) {
4844298606Sadrian			case 0:
4845298606Sadrian				LINE_CHECK("-stbc");
4846298606Sadrian				break;
4847298606Sadrian			case 1:
4848298606Sadrian				LINE_CHECK("stbctx -stbcrx");
4849298606Sadrian				break;
4850298606Sadrian			case 2:
4851298606Sadrian				LINE_CHECK("-stbctx stbcrx");
4852298606Sadrian				break;
4853298606Sadrian			case 3:
4854298606Sadrian				if (verbose)
4855298606Sadrian					LINE_CHECK("stbc");
4856298606Sadrian				break;
4857298606Sadrian			}
4858298606Sadrian		}
4859173275Ssam	}
4860173275Ssam
4861173275Ssam	if (get80211val(s, IEEE80211_IOC_WME, &wme) != -1) {
4862138593Ssam		if (wme)
4863155931Ssam			LINE_CHECK("wme");
4864138593Ssam		else if (verbose)
4865155931Ssam			LINE_CHECK("-wme");
4866138593Ssam	} else
4867138593Ssam		wme = 0;
4868138593Ssam
4869173275Ssam	if (get80211val(s, IEEE80211_IOC_BURST, &val) != -1) {
4870173275Ssam		if (val)
4871155931Ssam			LINE_CHECK("burst");
4872153422Ssam		else if (verbose)
4873155931Ssam			LINE_CHECK("-burst");
4874153422Ssam	}
4875153422Ssam
4876173275Ssam	if (get80211val(s, IEEE80211_IOC_FF, &val) != -1) {
4877173275Ssam		if (val)
4878170531Ssam			LINE_CHECK("ff");
4879170531Ssam		else if (verbose)
4880170531Ssam			LINE_CHECK("-ff");
4881170531Ssam	}
4882173275Ssam	if (get80211val(s, IEEE80211_IOC_TURBOP, &val) != -1) {
4883173275Ssam		if (val)
4884170531Ssam			LINE_CHECK("dturbo");
4885170531Ssam		else if (verbose)
4886170531Ssam			LINE_CHECK("-dturbo");
4887170531Ssam	}
4888178354Ssam	if (get80211val(s, IEEE80211_IOC_DWDS, &val) != -1) {
4889178354Ssam		if (val)
4890178354Ssam			LINE_CHECK("dwds");
4891178354Ssam		else if (verbose)
4892178354Ssam			LINE_CHECK("-dwds");
4893178354Ssam	}
4894170531Ssam
4895138593Ssam	if (opmode == IEEE80211_M_HOSTAP) {
4896173275Ssam		if (get80211val(s, IEEE80211_IOC_HIDESSID, &val) != -1) {
4897173275Ssam			if (val)
4898168075Ssam				LINE_CHECK("hidessid");
4899138593Ssam			else if (verbose)
4900168075Ssam				LINE_CHECK("-hidessid");
4901138593Ssam		}
4902173275Ssam		if (get80211val(s, IEEE80211_IOC_APBRIDGE, &val) != -1) {
4903173275Ssam			if (!val)
4904155931Ssam				LINE_CHECK("-apbridge");
4905138593Ssam			else if (verbose)
4906155931Ssam				LINE_CHECK("apbridge");
4907138593Ssam		}
4908173275Ssam		if (get80211val(s, IEEE80211_IOC_DTIM_PERIOD, &val) != -1)
4909173275Ssam			LINE_CHECK("dtimperiod %u", val);
4910138593Ssam
4911173275Ssam		if (get80211val(s, IEEE80211_IOC_DOTH, &val) != -1) {
4912173275Ssam			if (!val)
4913170531Ssam				LINE_CHECK("-doth");
4914170531Ssam			else if (verbose)
4915170531Ssam				LINE_CHECK("doth");
4916170531Ssam		}
4917178354Ssam		if (get80211val(s, IEEE80211_IOC_DFS, &val) != -1) {
4918178354Ssam			if (!val)
4919178354Ssam				LINE_CHECK("-dfs");
4920178354Ssam			else if (verbose)
4921178354Ssam				LINE_CHECK("dfs");
4922178354Ssam		}
4923173275Ssam		if (get80211val(s, IEEE80211_IOC_INACTIVITY, &val) != -1) {
4924173275Ssam			if (!val)
4925173275Ssam				LINE_CHECK("-inact");
4926173275Ssam			else if (verbose)
4927173275Ssam				LINE_CHECK("inact");
4928173275Ssam		}
4929138593Ssam	} else {
4930173275Ssam		if (get80211val(s, IEEE80211_IOC_ROAMING, &val) != -1) {
4931173275Ssam			if (val != IEEE80211_ROAMING_AUTO || verbose) {
4932173275Ssam				switch (val) {
4933138593Ssam				case IEEE80211_ROAMING_DEVICE:
4934155931Ssam					LINE_CHECK("roaming DEVICE");
4935138593Ssam					break;
4936138593Ssam				case IEEE80211_ROAMING_AUTO:
4937155931Ssam					LINE_CHECK("roaming AUTO");
4938138593Ssam					break;
4939138593Ssam				case IEEE80211_ROAMING_MANUAL:
4940155931Ssam					LINE_CHECK("roaming MANUAL");
4941138593Ssam					break;
4942138593Ssam				default:
4943155931Ssam					LINE_CHECK("roaming UNKNOWN (0x%x)",
4944173275Ssam						val);
4945138593Ssam					break;
4946138593Ssam				}
4947138593Ssam			}
4948138593Ssam		}
4949138593Ssam	}
4950186101Ssam
4951186904Ssam	if (opmode == IEEE80211_M_AHDEMO) {
4952186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOT, &val) != -1)
4953186904Ssam			LINE_CHECK("tdmaslot %u", val);
4954186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTCNT, &val) != -1)
4955186904Ssam			LINE_CHECK("tdmaslotcnt %u", val);
4956186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTLEN, &val) != -1)
4957186904Ssam			LINE_CHECK("tdmaslotlen %u", val);
4958186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_BINTERVAL, &val) != -1)
4959186904Ssam			LINE_CHECK("tdmabintval %u", val);
4960186904Ssam	} else if (get80211val(s, IEEE80211_IOC_BEACON_INTERVAL, &val) != -1) {
4961173275Ssam		/* XXX default define not visible */
4962173275Ssam		if (val != 100 || verbose)
4963173275Ssam			LINE_CHECK("bintval %u", val);
4964138593Ssam	}
4965138593Ssam
4966138593Ssam	if (wme && verbose) {
4967138593Ssam		LINE_BREAK();
4968138593Ssam		list_wme(s);
4969138593Ssam	}
4970195618Srpaulo
4971195618Srpaulo	if (opmode == IEEE80211_M_MBSS) {
4972195618Srpaulo		if (get80211val(s, IEEE80211_IOC_MESH_TTL, &val) != -1) {
4973195618Srpaulo			LINE_CHECK("meshttl %u", val);
4974195618Srpaulo		}
4975195618Srpaulo		if (get80211val(s, IEEE80211_IOC_MESH_AP, &val) != -1) {
4976195618Srpaulo			if (val)
4977195618Srpaulo				LINE_CHECK("meshpeering");
4978195618Srpaulo			else
4979195618Srpaulo				LINE_CHECK("-meshpeering");
4980195618Srpaulo		}
4981195618Srpaulo		if (get80211val(s, IEEE80211_IOC_MESH_FWRD, &val) != -1) {
4982195618Srpaulo			if (val)
4983195618Srpaulo				LINE_CHECK("meshforward");
4984195618Srpaulo			else
4985195618Srpaulo				LINE_CHECK("-meshforward");
4986195618Srpaulo		}
4987234893Smonthadar		if (get80211val(s, IEEE80211_IOC_MESH_GATE, &val) != -1) {
4988234893Smonthadar			if (val)
4989234893Smonthadar				LINE_CHECK("meshgate");
4990234893Smonthadar			else
4991234893Smonthadar				LINE_CHECK("-meshgate");
4992234893Smonthadar		}
4993195618Srpaulo		if (get80211len(s, IEEE80211_IOC_MESH_PR_METRIC, data, 12,
4994195618Srpaulo		    &len) != -1) {
4995195618Srpaulo			data[len] = '\0';
4996195618Srpaulo			LINE_CHECK("meshmetric %s", data);
4997195618Srpaulo		}
4998195618Srpaulo		if (get80211len(s, IEEE80211_IOC_MESH_PR_PATH, data, 12,
4999195618Srpaulo		    &len) != -1) {
5000195618Srpaulo			data[len] = '\0';
5001195618Srpaulo			LINE_CHECK("meshpath %s", data);
5002195618Srpaulo		}
5003195618Srpaulo		if (get80211val(s, IEEE80211_IOC_HWMP_ROOTMODE, &val) != -1) {
5004195618Srpaulo			switch (val) {
5005195618Srpaulo			case IEEE80211_HWMP_ROOTMODE_DISABLED:
5006195618Srpaulo				LINE_CHECK("hwmprootmode DISABLED");
5007195618Srpaulo				break;
5008195618Srpaulo			case IEEE80211_HWMP_ROOTMODE_NORMAL:
5009195618Srpaulo				LINE_CHECK("hwmprootmode NORMAL");
5010195618Srpaulo				break;
5011195618Srpaulo			case IEEE80211_HWMP_ROOTMODE_PROACTIVE:
5012195618Srpaulo				LINE_CHECK("hwmprootmode PROACTIVE");
5013195618Srpaulo				break;
5014195618Srpaulo			case IEEE80211_HWMP_ROOTMODE_RANN:
5015195618Srpaulo				LINE_CHECK("hwmprootmode RANN");
5016195618Srpaulo				break;
5017195618Srpaulo			default:
5018195618Srpaulo				LINE_CHECK("hwmprootmode UNKNOWN(%d)", val);
5019195618Srpaulo				break;
5020195618Srpaulo			}
5021195618Srpaulo		}
5022195618Srpaulo		if (get80211val(s, IEEE80211_IOC_HWMP_MAXHOPS, &val) != -1) {
5023195618Srpaulo			LINE_CHECK("hwmpmaxhops %u", val);
5024195618Srpaulo		}
5025195618Srpaulo	}
5026195618Srpaulo
5027173275Ssam	LINE_BREAK();
5028173275Ssam}
5029138593Ssam
5030173275Ssamstatic int
5031173275Ssamget80211(int s, int type, void *data, int len)
5032173275Ssam{
5033138593Ssam
5034291470Sadrian	return (lib80211_get80211(s, name, type, data, len));
5035173275Ssam}
5036138593Ssam
5037173275Ssamstatic int
5038173275Ssamget80211len(int s, int type, void *data, int len, int *plen)
5039173275Ssam{
5040138593Ssam
5041291470Sadrian	return (lib80211_get80211len(s, name, type, data, len, plen));
5042173275Ssam}
5043138593Ssam
5044173275Ssamstatic int
5045173275Ssamget80211val(int s, int type, int *val)
5046173275Ssam{
504777218Sphk
5048291470Sadrian	return (lib80211_get80211val(s, name, type, val));
504977218Sphk}
505077218Sphk
505177218Sphkstatic void
5052170531Ssamset80211(int s, int type, int val, int len, void *data)
505377218Sphk{
5054291470Sadrian	int ret;
505577218Sphk
5056291470Sadrian	ret = lib80211_set80211(s, name, type, val, len, data);
5057291470Sadrian	if (ret < 0)
505877218Sphk		err(1, "SIOCS80211");
505977218Sphk}
506077218Sphk
506177218Sphkstatic const char *
506277218Sphkget_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
506377218Sphk{
506477218Sphk	int len;
506577218Sphk	int hexstr;
506677218Sphk	u_int8_t *p;
506777218Sphk
506877218Sphk	len = *lenp;
506977218Sphk	p = buf;
507077218Sphk	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
507177218Sphk	if (hexstr)
507277218Sphk		val += 2;
507377218Sphk	for (;;) {
507477218Sphk		if (*val == '\0')
507577218Sphk			break;
507677218Sphk		if (sep != NULL && strchr(sep, *val) != NULL) {
507777218Sphk			val++;
507877218Sphk			break;
507977218Sphk		}
508077218Sphk		if (hexstr) {
5081127831Sphk			if (!isxdigit((u_char)val[0])) {
508277218Sphk				warnx("bad hexadecimal digits");
508377218Sphk				return NULL;
508477218Sphk			}
5085127831Sphk			if (!isxdigit((u_char)val[1])) {
5086127831Sphk				warnx("odd count hexadecimal digits");
5087127831Sphk				return NULL;
5088127831Sphk			}
508977218Sphk		}
5090127831Sphk		if (p >= buf + len) {
509177218Sphk			if (hexstr)
509277218Sphk				warnx("hexadecimal digits too long");
509377218Sphk			else
5094127831Sphk				warnx("string too long");
509577218Sphk			return NULL;
509677218Sphk		}
509777218Sphk		if (hexstr) {
509877218Sphk#define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
509977218Sphk			*p++ = (tohex((u_char)val[0]) << 4) |
510077218Sphk			    tohex((u_char)val[1]);
510177218Sphk#undef tohex
510277218Sphk			val += 2;
510377218Sphk		} else
510477218Sphk			*p++ = *val++;
510577218Sphk	}
510677218Sphk	len = p - buf;
510777218Sphk	/* The string "-" is treated as the empty string. */
5108165045Ssam	if (!hexstr && len == 1 && buf[0] == '-') {
510977218Sphk		len = 0;
5110165045Ssam		memset(buf, 0, *lenp);
5111165045Ssam	} else if (len < *lenp)
511277218Sphk		memset(p, 0, *lenp - len);
511377218Sphk	*lenp = len;
511477218Sphk	return val;
511577218Sphk}
511677218Sphk
511777218Sphkstatic void
511877218Sphkprint_string(const u_int8_t *buf, int len)
511977218Sphk{
512077218Sphk	int i;
512177218Sphk	int hasspc;
512277218Sphk
512377218Sphk	i = 0;
512477218Sphk	hasspc = 0;
512591454Sbrooks	for (; i < len; i++) {
512677218Sphk		if (!isprint(buf[i]) && buf[i] != '\0')
512777218Sphk			break;
512877218Sphk		if (isspace(buf[i]))
512977218Sphk			hasspc++;
513077218Sphk	}
513177218Sphk	if (i == len) {
513277218Sphk		if (hasspc || len == 0 || buf[0] == '\0')
513377218Sphk			printf("\"%.*s\"", len, buf);
513477218Sphk		else
513577218Sphk			printf("%.*s", len, buf);
513677218Sphk	} else {
513777218Sphk		printf("0x");
513877218Sphk		for (i = 0; i < len; i++)
513977218Sphk			printf("%02x", buf[i]);
514077218Sphk	}
514177218Sphk}
514277218Sphk
5143178354Ssam/*
5144178354Ssam * Virtual AP cloning support.
5145178354Ssam */
5146178354Ssamstatic struct ieee80211_clone_params params = {
5147178354Ssam	.icp_opmode	= IEEE80211_M_STA,	/* default to station mode */
5148178354Ssam};
5149178354Ssam
5150178354Ssamstatic void
5151178354Ssamwlan_create(int s, struct ifreq *ifr)
5152178354Ssam{
5153178354Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
5154178354Ssam
5155178354Ssam	if (params.icp_parent[0] == '\0')
5156189096Srpaulo		errx(1, "must specify a parent device (wlandev) when creating "
5157189096Srpaulo		    "a wlan device");
5158178354Ssam	if (params.icp_opmode == IEEE80211_M_WDS &&
5159178354Ssam	    memcmp(params.icp_bssid, zerobssid, sizeof(zerobssid)) == 0)
5160178354Ssam		errx(1, "no bssid specified for WDS (use wlanbssid)");
5161178354Ssam	ifr->ifr_data = (caddr_t) &params;
5162178354Ssam	if (ioctl(s, SIOCIFCREATE2, ifr) < 0)
5163178354Ssam		err(1, "SIOCIFCREATE2");
5164178354Ssam}
5165178354Ssam
5166178354Ssamstatic
5167178354SsamDECL_CMD_FUNC(set80211clone_wlandev, arg, d)
5168178354Ssam{
5169178354Ssam	strlcpy(params.icp_parent, arg, IFNAMSIZ);
5170178354Ssam}
5171178354Ssam
5172178354Ssamstatic
5173178354SsamDECL_CMD_FUNC(set80211clone_wlanbssid, arg, d)
5174178354Ssam{
5175178354Ssam	const struct ether_addr *ea;
5176178354Ssam
5177178354Ssam	ea = ether_aton(arg);
5178178354Ssam	if (ea == NULL)
5179178354Ssam		errx(1, "%s: cannot parse bssid", arg);
5180178354Ssam	memcpy(params.icp_bssid, ea->octet, IEEE80211_ADDR_LEN);
5181178354Ssam}
5182178354Ssam
5183178354Ssamstatic
5184178354SsamDECL_CMD_FUNC(set80211clone_wlanaddr, arg, d)
5185178354Ssam{
5186178354Ssam	const struct ether_addr *ea;
5187178354Ssam
5188178354Ssam	ea = ether_aton(arg);
5189178354Ssam	if (ea == NULL)
5190207320Srpaulo		errx(1, "%s: cannot parse address", arg);
5191178354Ssam	memcpy(params.icp_macaddr, ea->octet, IEEE80211_ADDR_LEN);
5192178354Ssam	params.icp_flags |= IEEE80211_CLONE_MACADDR;
5193178354Ssam}
5194178354Ssam
5195178354Ssamstatic
5196178354SsamDECL_CMD_FUNC(set80211clone_wlanmode, arg, d)
5197178354Ssam{
5198178354Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
5199178354Ssam	if (iseq(arg, "sta"))
5200178354Ssam		params.icp_opmode = IEEE80211_M_STA;
5201178354Ssam	else if (iseq(arg, "ahdemo") || iseq(arg, "adhoc-demo"))
5202178354Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
5203178354Ssam	else if (iseq(arg, "ibss") || iseq(arg, "adhoc"))
5204178354Ssam		params.icp_opmode = IEEE80211_M_IBSS;
5205178354Ssam	else if (iseq(arg, "ap") || iseq(arg, "host"))
5206178354Ssam		params.icp_opmode = IEEE80211_M_HOSTAP;
5207178354Ssam	else if (iseq(arg, "wds"))
5208178354Ssam		params.icp_opmode = IEEE80211_M_WDS;
5209178354Ssam	else if (iseq(arg, "monitor"))
5210178354Ssam		params.icp_opmode = IEEE80211_M_MONITOR;
5211186904Ssam	else if (iseq(arg, "tdma")) {
5212186904Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
5213186904Ssam		params.icp_flags |= IEEE80211_CLONE_TDMA;
5214195618Srpaulo	} else if (iseq(arg, "mesh") || iseq(arg, "mp")) /* mesh point */
5215195618Srpaulo		params.icp_opmode = IEEE80211_M_MBSS;
5216195618Srpaulo	else
5217178354Ssam		errx(1, "Don't know to create %s for %s", arg, name);
5218178354Ssam#undef iseq
5219178354Ssam}
5220178354Ssam
5221178354Ssamstatic void
5222178354Ssamset80211clone_beacons(const char *val, int d, int s, const struct afswtch *rafp)
5223178354Ssam{
5224178354Ssam	/* NB: inverted sense */
5225178354Ssam	if (d)
5226178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_NOBEACONS;
5227178354Ssam	else
5228178354Ssam		params.icp_flags |= IEEE80211_CLONE_NOBEACONS;
5229178354Ssam}
5230178354Ssam
5231178354Ssamstatic void
5232178354Ssamset80211clone_bssid(const char *val, int d, int s, const struct afswtch *rafp)
5233178354Ssam{
5234178354Ssam	if (d)
5235178354Ssam		params.icp_flags |= IEEE80211_CLONE_BSSID;
5236178354Ssam	else
5237178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_BSSID;
5238178354Ssam}
5239178354Ssam
5240178354Ssamstatic void
5241178354Ssamset80211clone_wdslegacy(const char *val, int d, int s, const struct afswtch *rafp)
5242178354Ssam{
5243178354Ssam	if (d)
5244178354Ssam		params.icp_flags |= IEEE80211_CLONE_WDSLEGACY;
5245178354Ssam	else
5246178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_WDSLEGACY;
5247178354Ssam}
5248178354Ssam
5249138593Ssamstatic struct cmd ieee80211_cmds[] = {
5250138593Ssam	DEF_CMD_ARG("ssid",		set80211ssid),
5251138593Ssam	DEF_CMD_ARG("nwid",		set80211ssid),
5252195618Srpaulo	DEF_CMD_ARG("meshid",		set80211meshid),
5253138593Ssam	DEF_CMD_ARG("stationname",	set80211stationname),
5254138593Ssam	DEF_CMD_ARG("station",		set80211stationname),	/* BSD/OS */
5255138593Ssam	DEF_CMD_ARG("channel",		set80211channel),
5256138593Ssam	DEF_CMD_ARG("authmode",		set80211authmode),
5257138593Ssam	DEF_CMD_ARG("powersavemode",	set80211powersavemode),
5258138593Ssam	DEF_CMD("powersave",	1,	set80211powersave),
5259138593Ssam	DEF_CMD("-powersave",	0,	set80211powersave),
5260138593Ssam	DEF_CMD_ARG("powersavesleep", 	set80211powersavesleep),
5261138593Ssam	DEF_CMD_ARG("wepmode",		set80211wepmode),
5262138593Ssam	DEF_CMD("wep",		1,	set80211wep),
5263138593Ssam	DEF_CMD("-wep",		0,	set80211wep),
5264139493Ssam	DEF_CMD_ARG("deftxkey",		set80211weptxkey),
5265138593Ssam	DEF_CMD_ARG("weptxkey",		set80211weptxkey),
5266138593Ssam	DEF_CMD_ARG("wepkey",		set80211wepkey),
5267138593Ssam	DEF_CMD_ARG("nwkey",		set80211nwkey),		/* NetBSD */
5268138593Ssam	DEF_CMD("-nwkey",	0,	set80211wep),		/* NetBSD */
5269138593Ssam	DEF_CMD_ARG("rtsthreshold",	set80211rtsthreshold),
5270138593Ssam	DEF_CMD_ARG("protmode",		set80211protmode),
5271138593Ssam	DEF_CMD_ARG("txpower",		set80211txpower),
5272138593Ssam	DEF_CMD_ARG("roaming",		set80211roaming),
5273138593Ssam	DEF_CMD("wme",		1,	set80211wme),
5274138593Ssam	DEF_CMD("-wme",		0,	set80211wme),
5275178354Ssam	DEF_CMD("wmm",		1,	set80211wme),
5276178354Ssam	DEF_CMD("-wmm",		0,	set80211wme),
5277138593Ssam	DEF_CMD("hidessid",	1,	set80211hidessid),
5278138593Ssam	DEF_CMD("-hidessid",	0,	set80211hidessid),
5279138593Ssam	DEF_CMD("apbridge",	1,	set80211apbridge),
5280138593Ssam	DEF_CMD("-apbridge",	0,	set80211apbridge),
5281138593Ssam	DEF_CMD_ARG("chanlist",		set80211chanlist),
5282138593Ssam	DEF_CMD_ARG("bssid",		set80211bssid),
5283138593Ssam	DEF_CMD_ARG("ap",		set80211bssid),
5284138593Ssam	DEF_CMD("scan",	0,		set80211scan),
5285138593Ssam	DEF_CMD_ARG("list",		set80211list),
5286138593Ssam	DEF_CMD_ARG2("cwmin",		set80211cwmin),
5287138593Ssam	DEF_CMD_ARG2("cwmax",		set80211cwmax),
5288138593Ssam	DEF_CMD_ARG2("aifs",		set80211aifs),
5289138593Ssam	DEF_CMD_ARG2("txoplimit",	set80211txoplimit),
5290148621Ssam	DEF_CMD_ARG("acm",		set80211acm),
5291148621Ssam	DEF_CMD_ARG("-acm",		set80211noacm),
5292148621Ssam	DEF_CMD_ARG("ack",		set80211ackpolicy),
5293148621Ssam	DEF_CMD_ARG("-ack",		set80211noackpolicy),
5294138593Ssam	DEF_CMD_ARG2("bss:cwmin",	set80211bsscwmin),
5295138593Ssam	DEF_CMD_ARG2("bss:cwmax",	set80211bsscwmax),
5296138593Ssam	DEF_CMD_ARG2("bss:aifs",	set80211bssaifs),
5297138593Ssam	DEF_CMD_ARG2("bss:txoplimit",	set80211bsstxoplimit),
5298138593Ssam	DEF_CMD_ARG("dtimperiod",	set80211dtimperiod),
5299138593Ssam	DEF_CMD_ARG("bintval",		set80211bintval),
5300138593Ssam	DEF_CMD("mac:open",	IEEE80211_MACCMD_POLICY_OPEN,	set80211maccmd),
5301138593Ssam	DEF_CMD("mac:allow",	IEEE80211_MACCMD_POLICY_ALLOW,	set80211maccmd),
5302138593Ssam	DEF_CMD("mac:deny",	IEEE80211_MACCMD_POLICY_DENY,	set80211maccmd),
5303178354Ssam	DEF_CMD("mac:radius",	IEEE80211_MACCMD_POLICY_RADIUS,	set80211maccmd),
5304138593Ssam	DEF_CMD("mac:flush",	IEEE80211_MACCMD_FLUSH,		set80211maccmd),
5305138593Ssam	DEF_CMD("mac:detach",	IEEE80211_MACCMD_DETACH,	set80211maccmd),
5306138593Ssam	DEF_CMD_ARG("mac:add",		set80211addmac),
5307138593Ssam	DEF_CMD_ARG("mac:del",		set80211delmac),
5308138593Ssam	DEF_CMD_ARG("mac:kick",		set80211kickmac),
5309147795Ssam	DEF_CMD("pureg",	1,	set80211pureg),
5310147795Ssam	DEF_CMD("-pureg",	0,	set80211pureg),
5311170531Ssam	DEF_CMD("ff",		1,	set80211fastframes),
5312170531Ssam	DEF_CMD("-ff",		0,	set80211fastframes),
5313170531Ssam	DEF_CMD("dturbo",	1,	set80211dturbo),
5314170531Ssam	DEF_CMD("-dturbo",	0,	set80211dturbo),
5315170531Ssam	DEF_CMD("bgscan",	1,	set80211bgscan),
5316170531Ssam	DEF_CMD("-bgscan",	0,	set80211bgscan),
5317170531Ssam	DEF_CMD_ARG("bgscanidle",	set80211bgscanidle),
5318170531Ssam	DEF_CMD_ARG("bgscanintvl",	set80211bgscanintvl),
5319170531Ssam	DEF_CMD_ARG("scanvalid",	set80211scanvalid),
5320227336Sadrian	DEF_CMD("quiet",        1,      set80211quiet),
5321227336Sadrian	DEF_CMD("-quiet",       0,      set80211quiet),
5322227336Sadrian	DEF_CMD_ARG("quiet_count",      set80211quietcount),
5323227336Sadrian	DEF_CMD_ARG("quiet_period",     set80211quietperiod),
5324227336Sadrian	DEF_CMD_ARG("quiet_dur",        set80211quietduration),
5325227336Sadrian	DEF_CMD_ARG("quiet_offset",     set80211quietoffset),
5326178354Ssam	DEF_CMD_ARG("roam:rssi",	set80211roamrssi),
5327178354Ssam	DEF_CMD_ARG("roam:rate",	set80211roamrate),
5328153354Ssam	DEF_CMD_ARG("mcastrate",	set80211mcastrate),
5329178354Ssam	DEF_CMD_ARG("ucastrate",	set80211ucastrate),
5330178354Ssam	DEF_CMD_ARG("mgtrate",		set80211mgtrate),
5331178354Ssam	DEF_CMD_ARG("mgmtrate",		set80211mgtrate),
5332178354Ssam	DEF_CMD_ARG("maxretry",		set80211maxretry),
5333148416Ssam	DEF_CMD_ARG("fragthreshold",	set80211fragthreshold),
5334153422Ssam	DEF_CMD("burst",	1,	set80211burst),
5335153422Ssam	DEF_CMD("-burst",	0,	set80211burst),
5336160687Ssam	DEF_CMD_ARG("bmiss",		set80211bmissthreshold),
5337160687Ssam	DEF_CMD_ARG("bmissthreshold",	set80211bmissthreshold),
5338173275Ssam	DEF_CMD("shortgi",	1,	set80211shortgi),
5339173275Ssam	DEF_CMD("-shortgi",	0,	set80211shortgi),
5340173275Ssam	DEF_CMD("ampdurx",	2,	set80211ampdu),
5341173275Ssam	DEF_CMD("-ampdurx",	-2,	set80211ampdu),
5342173275Ssam	DEF_CMD("ampdutx",	1,	set80211ampdu),
5343173275Ssam	DEF_CMD("-ampdutx",	-1,	set80211ampdu),
5344173275Ssam	DEF_CMD("ampdu",	3,	set80211ampdu),		/* NB: tx+rx */
5345173275Ssam	DEF_CMD("-ampdu",	-3,	set80211ampdu),
5346173275Ssam	DEF_CMD_ARG("ampdulimit",	set80211ampdulimit),
5347173275Ssam	DEF_CMD_ARG("ampdudensity",	set80211ampdudensity),
5348173275Ssam	DEF_CMD("amsdurx",	2,	set80211amsdu),
5349173275Ssam	DEF_CMD("-amsdurx",	-2,	set80211amsdu),
5350173275Ssam	DEF_CMD("amsdutx",	1,	set80211amsdu),
5351173275Ssam	DEF_CMD("-amsdutx",	-1,	set80211amsdu),
5352173275Ssam	DEF_CMD("amsdu",	3,	set80211amsdu),		/* NB: tx+rx */
5353173275Ssam	DEF_CMD("-amsdu",	-3,	set80211amsdu),
5354173275Ssam	DEF_CMD_ARG("amsdulimit",	set80211amsdulimit),
5355298606Sadrian	DEF_CMD("stbcrx",	2,	set80211stbc),
5356298606Sadrian	DEF_CMD("-stbcrx",	-2,	set80211stbc),
5357298606Sadrian	DEF_CMD("stbctx",	1,	set80211stbc),
5358298606Sadrian	DEF_CMD("-stbctx",	-1,	set80211stbc),
5359298606Sadrian	DEF_CMD("stbc",		3,	set80211stbc),		/* NB: tx+rx */
5360298606Sadrian	DEF_CMD("-ampdu",	-3,	set80211stbc),
5361173275Ssam	DEF_CMD("puren",	1,	set80211puren),
5362173275Ssam	DEF_CMD("-puren",	0,	set80211puren),
5363170531Ssam	DEF_CMD("doth",		1,	set80211doth),
5364170531Ssam	DEF_CMD("-doth",	0,	set80211doth),
5365178354Ssam	DEF_CMD("dfs",		1,	set80211dfs),
5366178354Ssam	DEF_CMD("-dfs",		0,	set80211dfs),
5367173275Ssam	DEF_CMD("htcompat",	1,	set80211htcompat),
5368173275Ssam	DEF_CMD("-htcompat",	0,	set80211htcompat),
5369178354Ssam	DEF_CMD("dwds",		1,	set80211dwds),
5370178354Ssam	DEF_CMD("-dwds",	0,	set80211dwds),
5371173275Ssam	DEF_CMD("inact",	1,	set80211inact),
5372173275Ssam	DEF_CMD("-inact",	0,	set80211inact),
5373178354Ssam	DEF_CMD("tsn",		1,	set80211tsn),
5374178354Ssam	DEF_CMD("-tsn",		0,	set80211tsn),
5375178354Ssam	DEF_CMD_ARG("regdomain",	set80211regdomain),
5376178354Ssam	DEF_CMD_ARG("country",		set80211country),
5377178354Ssam	DEF_CMD("indoor",	'I',	set80211location),
5378178354Ssam	DEF_CMD("-indoor",	'O',	set80211location),
5379178354Ssam	DEF_CMD("outdoor",	'O',	set80211location),
5380178354Ssam	DEF_CMD("-outdoor",	'I',	set80211location),
5381178354Ssam	DEF_CMD("anywhere",	' ',	set80211location),
5382178354Ssam	DEF_CMD("ecm",		1,	set80211ecm),
5383178354Ssam	DEF_CMD("-ecm",		0,	set80211ecm),
5384178354Ssam	DEF_CMD("dotd",		1,	set80211dotd),
5385178354Ssam	DEF_CMD("-dotd",	0,	set80211dotd),
5386173275Ssam	DEF_CMD_ARG("htprotmode",	set80211htprotmode),
5387173275Ssam	DEF_CMD("ht20",		1,	set80211htconf),
5388173275Ssam	DEF_CMD("-ht20",	0,	set80211htconf),
5389173275Ssam	DEF_CMD("ht40",		3,	set80211htconf),	/* NB: 20+40 */
5390173275Ssam	DEF_CMD("-ht40",	0,	set80211htconf),
5391173275Ssam	DEF_CMD("ht",		3,	set80211htconf),	/* NB: 20+40 */
5392173275Ssam	DEF_CMD("-ht",		0,	set80211htconf),
5393183261Ssam	DEF_CMD("rifs",		1,	set80211rifs),
5394183261Ssam	DEF_CMD("-rifs",	0,	set80211rifs),
5395183261Ssam	DEF_CMD("smps",		IEEE80211_HTCAP_SMPS_ENA,	set80211smps),
5396183261Ssam	DEF_CMD("smpsdyn",	IEEE80211_HTCAP_SMPS_DYNAMIC,	set80211smps),
5397183261Ssam	DEF_CMD("-smps",	IEEE80211_HTCAP_SMPS_OFF,	set80211smps),
5398178354Ssam	/* XXX for testing */
5399178354Ssam	DEF_CMD_ARG("chanswitch",	set80211chanswitch),
5400178354Ssam
5401186904Ssam	DEF_CMD_ARG("tdmaslot",		set80211tdmaslot),
5402186904Ssam	DEF_CMD_ARG("tdmaslotcnt",	set80211tdmaslotcnt),
5403186904Ssam	DEF_CMD_ARG("tdmaslotlen",	set80211tdmaslotlen),
5404186904Ssam	DEF_CMD_ARG("tdmabintval",	set80211tdmabintval),
5405186904Ssam
5406195618Srpaulo	DEF_CMD_ARG("meshttl",		set80211meshttl),
5407195618Srpaulo	DEF_CMD("meshforward",	1,	set80211meshforward),
5408195618Srpaulo	DEF_CMD("-meshforward",	0,	set80211meshforward),
5409234893Smonthadar	DEF_CMD("meshgate",	1,	set80211meshgate),
5410234893Smonthadar	DEF_CMD("-meshgate",	0,	set80211meshgate),
5411195618Srpaulo	DEF_CMD("meshpeering",	1,	set80211meshpeering),
5412195618Srpaulo	DEF_CMD("-meshpeering",	0,	set80211meshpeering),
5413195618Srpaulo	DEF_CMD_ARG("meshmetric",	set80211meshmetric),
5414195618Srpaulo	DEF_CMD_ARG("meshpath",		set80211meshpath),
5415195618Srpaulo	DEF_CMD("meshrt:flush",	IEEE80211_MESH_RTCMD_FLUSH,	set80211meshrtcmd),
5416195618Srpaulo	DEF_CMD_ARG("meshrt:add",	set80211addmeshrt),
5417195618Srpaulo	DEF_CMD_ARG("meshrt:del",	set80211delmeshrt),
5418195618Srpaulo	DEF_CMD_ARG("hwmprootmode",	set80211hwmprootmode),
5419195618Srpaulo	DEF_CMD_ARG("hwmpmaxhops",	set80211hwmpmaxhops),
5420195618Srpaulo
5421178354Ssam	/* vap cloning support */
5422178354Ssam	DEF_CLONE_CMD_ARG("wlanaddr",	set80211clone_wlanaddr),
5423178354Ssam	DEF_CLONE_CMD_ARG("wlanbssid",	set80211clone_wlanbssid),
5424178354Ssam	DEF_CLONE_CMD_ARG("wlandev",	set80211clone_wlandev),
5425178354Ssam	DEF_CLONE_CMD_ARG("wlanmode",	set80211clone_wlanmode),
5426178354Ssam	DEF_CLONE_CMD("beacons", 1,	set80211clone_beacons),
5427178354Ssam	DEF_CLONE_CMD("-beacons", 0,	set80211clone_beacons),
5428178354Ssam	DEF_CLONE_CMD("bssid",	1,	set80211clone_bssid),
5429178354Ssam	DEF_CLONE_CMD("-bssid",	0,	set80211clone_bssid),
5430178354Ssam	DEF_CLONE_CMD("wdslegacy", 1,	set80211clone_wdslegacy),
5431178354Ssam	DEF_CLONE_CMD("-wdslegacy", 0,	set80211clone_wdslegacy),
5432138593Ssam};
5433138593Ssamstatic struct afswtch af_ieee80211 = {
5434138593Ssam	.af_name	= "af_ieee80211",
5435138593Ssam	.af_af		= AF_UNSPEC,
5436139494Ssam	.af_other_status = ieee80211_status,
5437138593Ssam};
5438138593Ssam
5439138593Ssamstatic __constructor void
5440138593Ssamieee80211_ctor(void)
5441138593Ssam{
5442138593Ssam	int i;
5443138593Ssam
5444288305Sngie	for (i = 0; i < nitems(ieee80211_cmds);  i++)
5445138593Ssam		cmd_register(&ieee80211_cmds[i]);
5446138593Ssam	af_register(&af_ieee80211);
5447189096Srpaulo	clone_setdefcallback("wlan", wlan_create);
5448138593Ssam}
5449