1330449Seadler/*-
2330449Seadler * SPDX-License-Identifier: BSD-3-Clause
3330449Seadler *
477218Sphk * Copyright 2001 The Aerospace Corporation.  All rights reserved.
577218Sphk *
677218Sphk * Redistribution and use in source and binary forms, with or without
777218Sphk * modification, are permitted provided that the following conditions
877218Sphk * are met:
977218Sphk * 1. Redistributions of source code must retain the above copyright
1077218Sphk *    notice, this list of conditions and the following disclaimer.
1177218Sphk * 2. Redistributions in binary form must reproduce the above copyright
1277218Sphk *    notice, this list of conditions and the following disclaimer in the
1377218Sphk *    documentation and/or other materials provided with the distribution.
1491454Sbrooks * 3. The name of The Aerospace Corporation may not be used to endorse or
1591454Sbrooks *    promote products derived from this software.
1677218Sphk *
1777218Sphk * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION ``AS IS'' AND
1877218Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1977218Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2077218Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE
2177218Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2277218Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2377218Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2477218Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2577218Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2677218Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2777218Sphk * SUCH DAMAGE.
2877218Sphk *
2977218Sphk * $FreeBSD: stable/11/sbin/ifconfig/ifieee80211.c 368298 2020-12-03 10:41:06Z hselasky $
3077218Sphk */
3177218Sphk
3277218Sphk/*-
3377218Sphk * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
3477218Sphk * All rights reserved.
3577218Sphk *
3677218Sphk * This code is derived from software contributed to The NetBSD Foundation
3777218Sphk * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
3877218Sphk * NASA Ames Research Center.
3977218Sphk *
4077218Sphk * Redistribution and use in source and binary forms, with or without
4177218Sphk * modification, are permitted provided that the following conditions
4277218Sphk * are met:
4377218Sphk * 1. Redistributions of source code must retain the above copyright
4477218Sphk *    notice, this list of conditions and the following disclaimer.
4577218Sphk * 2. Redistributions in binary form must reproduce the above copyright
4677218Sphk *    notice, this list of conditions and the following disclaimer in the
4777218Sphk *    documentation and/or other materials provided with the distribution.
4877218Sphk *
4977218Sphk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
5077218Sphk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
5177218Sphk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5277218Sphk * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
5377218Sphk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5477218Sphk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5577218Sphk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5677218Sphk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
5777218Sphk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5877218Sphk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
5977218Sphk * POSSIBILITY OF SUCH DAMAGE.
6077218Sphk */
6177218Sphk
6277218Sphk#include <sys/param.h>
6377218Sphk#include <sys/ioctl.h>
6477218Sphk#include <sys/socket.h>
6577218Sphk#include <sys/sysctl.h>
6677218Sphk#include <sys/time.h>
6777218Sphk
6877218Sphk#include <net/ethernet.h>
6977218Sphk#include <net/if.h>
7077218Sphk#include <net/if_dl.h>
7177218Sphk#include <net/if_types.h>
72138593Ssam#include <net/if_media.h>
7377218Sphk#include <net/route.h>
74138593Ssam
75116957Ssam#include <net80211/ieee80211_ioctl.h>
76190456Ssam#include <net80211/ieee80211_freebsd.h>
77190456Ssam#include <net80211/ieee80211_superg.h>
78190456Ssam#include <net80211/ieee80211_tdma.h>
79195618Srpaulo#include <net80211/ieee80211_mesh.h>
8077218Sphk
81187801Ssam#include <assert.h>
8277218Sphk#include <ctype.h>
8377218Sphk#include <err.h>
8477218Sphk#include <errno.h>
8577218Sphk#include <fcntl.h>
86146873Sjhb#include <inttypes.h>
8777218Sphk#include <stdio.h>
8877218Sphk#include <stdlib.h>
8977218Sphk#include <string.h>
9077218Sphk#include <unistd.h>
91155931Ssam#include <stdarg.h>
92173275Ssam#include <stddef.h>		/* NB: for offsetof */
93336344Skevans#include <locale.h>
94336344Skevans#include <langinfo.h>
9577218Sphk
9677218Sphk#include "ifconfig.h"
9777218Sphk
98291470Sadrian#include <lib80211/lib80211_regdomain.h>
99291470Sadrian#include <lib80211/lib80211_ioctl.h>
100291470Sadrian
101178354Ssam#ifndef IEEE80211_FIXED_RATE_NONE
102178354Ssam#define	IEEE80211_FIXED_RATE_NONE	0xff
103178354Ssam#endif
104178354Ssam
105178354Ssam/* XXX need these publicly defined or similar */
106178354Ssam#ifndef IEEE80211_NODE_AUTH
107194136Ssam#define	IEEE80211_NODE_AUTH	0x000001	/* authorized for data */
108194136Ssam#define	IEEE80211_NODE_QOS	0x000002	/* QoS enabled */
109194136Ssam#define	IEEE80211_NODE_ERP	0x000004	/* ERP enabled */
110194136Ssam#define	IEEE80211_NODE_PWR_MGT	0x000010	/* power save mode enabled */
111194136Ssam#define	IEEE80211_NODE_AREF	0x000020	/* authentication ref held */
112194136Ssam#define	IEEE80211_NODE_HT	0x000040	/* HT enabled */
113194136Ssam#define	IEEE80211_NODE_HTCOMPAT	0x000080	/* HT setup w/ vendor OUI's */
114194136Ssam#define	IEEE80211_NODE_WPS	0x000100	/* WPS association */
115194136Ssam#define	IEEE80211_NODE_TSN	0x000200	/* TSN association */
116194136Ssam#define	IEEE80211_NODE_AMPDU_RX	0x000400	/* AMPDU rx enabled */
117194136Ssam#define	IEEE80211_NODE_AMPDU_TX	0x000800	/* AMPDU tx enabled */
118194136Ssam#define	IEEE80211_NODE_MIMO_PS	0x001000	/* MIMO power save enabled */
119194136Ssam#define	IEEE80211_NODE_MIMO_RTS	0x002000	/* send RTS in MIMO PS */
120194136Ssam#define	IEEE80211_NODE_RIFS	0x004000	/* RIFS enabled */
121194136Ssam#define	IEEE80211_NODE_SGI20	0x008000	/* Short GI in HT20 enabled */
122194136Ssam#define	IEEE80211_NODE_SGI40	0x010000	/* Short GI in HT40 enabled */
123194136Ssam#define	IEEE80211_NODE_ASSOCID	0x020000	/* xmit requires associd */
124194136Ssam#define	IEEE80211_NODE_AMSDU_RX	0x040000	/* AMSDU rx enabled */
125194136Ssam#define	IEEE80211_NODE_AMSDU_TX	0x080000	/* AMSDU tx enabled */
126178354Ssam#endif
127178354Ssam
128187801Ssam#define	MAXCHAN	1536		/* max 1.5K channels */
129187801Ssam
130173275Ssam#define	MAXCOL	78
131173275Ssamstatic	int col;
132173275Ssamstatic	char spacer;
133173275Ssam
134173275Ssamstatic void LINE_INIT(char c);
135173275Ssamstatic void LINE_BREAK(void);
136173275Ssamstatic void LINE_CHECK(const char *fmt, ...);
137173275Ssam
138188784Ssamstatic const char *modename[IEEE80211_MODE_MAX] = {
139188784Ssam	[IEEE80211_MODE_AUTO]	  = "auto",
140188784Ssam	[IEEE80211_MODE_11A]	  = "11a",
141188784Ssam	[IEEE80211_MODE_11B]	  = "11b",
142188784Ssam	[IEEE80211_MODE_11G]	  = "11g",
143188784Ssam	[IEEE80211_MODE_FH]	  = "fh",
144188784Ssam	[IEEE80211_MODE_TURBO_A]  = "turboA",
145188784Ssam	[IEEE80211_MODE_TURBO_G]  = "turboG",
146188784Ssam	[IEEE80211_MODE_STURBO_A] = "sturbo",
147188784Ssam	[IEEE80211_MODE_11NA]	  = "11na",
148188784Ssam	[IEEE80211_MODE_11NG]	  = "11ng",
149188784Ssam	[IEEE80211_MODE_HALF]	  = "half",
150188784Ssam	[IEEE80211_MODE_QUARTER]  = "quarter"
151173275Ssam};
152173275Ssam
153178354Ssamstatic void set80211(int s, int type, int val, int len, void *data);
154173275Ssamstatic int get80211(int s, int type, void *data, int len);
155173275Ssamstatic int get80211len(int s, int type, void *data, int len, int *plen);
156173275Ssamstatic int get80211val(int s, int type, int *val);
15777218Sphkstatic const char *get_string(const char *val, const char *sep,
15877218Sphk    u_int8_t *buf, int *lenp);
15977218Sphkstatic void print_string(const u_int8_t *buf, int len);
160178354Ssamstatic void print_regdomain(const struct ieee80211_regdomain *, int);
161178354Ssamstatic void print_channels(int, const struct ieee80211req_chaninfo *,
162178354Ssam    int allchans, int verbose);
163178354Ssamstatic void regdomain_makechannels(struct ieee80211_regdomain_req *,
164178354Ssam    const struct ieee80211_devcaps_req *);
165195618Srpaulostatic const char *mesh_linkstate_string(uint8_t state);
16677218Sphk
167187801Ssamstatic struct ieee80211req_chaninfo *chaninfo;
168178354Ssamstatic struct ieee80211_regdomain regdomain;
169178354Ssamstatic int gotregdomain = 0;
170178354Ssamstatic struct ieee80211_roamparams_req roamparams;
171178354Ssamstatic int gotroam = 0;
172178354Ssamstatic struct ieee80211_txparams_req txparams;
173178354Ssamstatic int gottxparams = 0;
174173275Ssamstatic struct ieee80211_channel curchan;
175173275Ssamstatic int gotcurchan = 0;
176178354Ssamstatic struct ifmediareq *ifmr;
177173275Ssamstatic int htconf = 0;
178173275Ssamstatic	int gothtconf = 0;
179170531Ssam
180173275Ssamstatic void
181173275Ssamgethtconf(int s)
182173275Ssam{
183173275Ssam	if (gothtconf)
184173275Ssam		return;
185173275Ssam	if (get80211val(s, IEEE80211_IOC_HTCONF, &htconf) < 0)
186173275Ssam		warn("unable to get HT configuration information");
187173275Ssam	gothtconf = 1;
188173275Ssam}
189173275Ssam
190170531Ssam/*
191170531Ssam * Collect channel info from the kernel.  We use this (mostly)
192170531Ssam * to handle mapping between frequency and IEEE channel number.
193170531Ssam */
194170531Ssamstatic void
195170531Ssamgetchaninfo(int s)
196170531Ssam{
197187801Ssam	if (chaninfo != NULL)
198170531Ssam		return;
199187801Ssam	chaninfo = malloc(IEEE80211_CHANINFO_SIZE(MAXCHAN));
200187801Ssam	if (chaninfo == NULL)
201187801Ssam		errx(1, "no space for channel list");
202187801Ssam	if (get80211(s, IEEE80211_IOC_CHANINFO, chaninfo,
203187801Ssam	    IEEE80211_CHANINFO_SIZE(MAXCHAN)) < 0)
204187801Ssam		err(1, "unable to get channel information");
205170531Ssam	ifmr = ifmedia_getstate(s);
206173275Ssam	gethtconf(s);
207170531Ssam}
208170531Ssam
209178354Ssamstatic struct regdata *
210178354Ssamgetregdata(void)
211178354Ssam{
212178354Ssam	static struct regdata *rdp = NULL;
213178354Ssam	if (rdp == NULL) {
214178354Ssam		rdp = lib80211_alloc_regdata();
215178354Ssam		if (rdp == NULL)
216181198Ssam			errx(-1, "missing or corrupted regdomain database");
217178354Ssam	}
218178354Ssam	return rdp;
219178354Ssam}
220178354Ssam
221170531Ssam/*
222170531Ssam * Given the channel at index i with attributes from,
223170531Ssam * check if there is a channel with attributes to in
224170531Ssam * the channel table.  With suitable attributes this
225170531Ssam * allows the caller to look for promotion; e.g. from
226170531Ssam * 11b > 11g.
227170531Ssam */
228138593Ssamstatic int
229170531Ssamcanpromote(int i, int from, int to)
230170531Ssam{
231187801Ssam	const struct ieee80211_channel *fc = &chaninfo->ic_chans[i];
232298556Spfg	u_int j;
233170531Ssam
234170531Ssam	if ((fc->ic_flags & from) != from)
235170531Ssam		return i;
236170531Ssam	/* NB: quick check exploiting ordering of chans w/ same frequency */
237187801Ssam	if (i+1 < chaninfo->ic_nchans &&
238187801Ssam	    chaninfo->ic_chans[i+1].ic_freq == fc->ic_freq &&
239187801Ssam	    (chaninfo->ic_chans[i+1].ic_flags & to) == to)
240170531Ssam		return i+1;
241170531Ssam	/* brute force search in case channel list is not ordered */
242187801Ssam	for (j = 0; j < chaninfo->ic_nchans; j++) {
243187801Ssam		const struct ieee80211_channel *tc = &chaninfo->ic_chans[j];
244170531Ssam		if (j != i &&
245170531Ssam		    tc->ic_freq == fc->ic_freq && (tc->ic_flags & to) == to)
246170531Ssam		return j;
247170531Ssam	}
248170531Ssam	return i;
249170531Ssam}
250170531Ssam
251170531Ssam/*
252170531Ssam * Handle channel promotion.  When a channel is specified with
253170531Ssam * only a frequency we want to promote it to the ``best'' channel
254170531Ssam * available.  The channel list has separate entries for 11b, 11g,
255170531Ssam * 11a, and 11n[ga] channels so specifying a frequency w/o any
256170531Ssam * attributes requires we upgrade, e.g. from 11b -> 11g.  This
257170531Ssam * gets complicated when the channel is specified on the same
258170531Ssam * command line with a media request that constrains the available
259170531Ssam * channe list (e.g. mode 11a); we want to honor that to avoid
260170531Ssam * confusing behaviour.
261170531Ssam */
262170531Ssamstatic int
263170531Ssampromote(int i)
264170531Ssam{
265170531Ssam	/*
266170531Ssam	 * Query the current mode of the interface in case it's
267170531Ssam	 * constrained (e.g. to 11a).  We must do this carefully
268170531Ssam	 * as there may be a pending ifmedia request in which case
269170531Ssam	 * asking the kernel will give us the wrong answer.  This
270170531Ssam	 * is an unfortunate side-effect of the way ifconfig is
271170531Ssam	 * structure for modularity (yech).
272170531Ssam	 *
273170531Ssam	 * NB: ifmr is actually setup in getchaninfo (above); we
274170531Ssam	 *     assume it's called coincident with to this call so
275170531Ssam	 *     we have a ``current setting''; otherwise we must pass
276170531Ssam	 *     the socket descriptor down to here so we can make
277170531Ssam	 *     the ifmedia_getstate call ourselves.
278170531Ssam	 */
279170531Ssam	int chanmode = ifmr != NULL ? IFM_MODE(ifmr->ifm_current) : IFM_AUTO;
280170531Ssam
281170531Ssam	/* when ambiguous promote to ``best'' */
282170531Ssam	/* NB: we abitrarily pick HT40+ over HT40- */
283170531Ssam	if (chanmode != IFM_IEEE80211_11B)
284170531Ssam		i = canpromote(i, IEEE80211_CHAN_B, IEEE80211_CHAN_G);
285173275Ssam	if (chanmode != IFM_IEEE80211_11G && (htconf & 1)) {
286170531Ssam		i = canpromote(i, IEEE80211_CHAN_G,
287170531Ssam			IEEE80211_CHAN_G | IEEE80211_CHAN_HT20);
288173275Ssam		if (htconf & 2) {
289173275Ssam			i = canpromote(i, IEEE80211_CHAN_G,
290173275Ssam				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D);
291173275Ssam			i = canpromote(i, IEEE80211_CHAN_G,
292173275Ssam				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U);
293173275Ssam		}
294170531Ssam	}
295173275Ssam	if (chanmode != IFM_IEEE80211_11A && (htconf & 1)) {
296170531Ssam		i = canpromote(i, IEEE80211_CHAN_A,
297170531Ssam			IEEE80211_CHAN_A | IEEE80211_CHAN_HT20);
298173275Ssam		if (htconf & 2) {
299173275Ssam			i = canpromote(i, IEEE80211_CHAN_A,
300173275Ssam				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D);
301173275Ssam			i = canpromote(i, IEEE80211_CHAN_A,
302173275Ssam				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U);
303173275Ssam		}
304170531Ssam	}
305170531Ssam	return i;
306170531Ssam}
307170531Ssam
308170531Ssamstatic void
309170531Ssammapfreq(struct ieee80211_channel *chan, int freq, int flags)
310170531Ssam{
311298556Spfg	u_int i;
312170531Ssam
313187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
314187801Ssam		const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
315170531Ssam
316170531Ssam		if (c->ic_freq == freq && (c->ic_flags & flags) == flags) {
317170531Ssam			if (flags == 0) {
318170531Ssam				/* when ambiguous promote to ``best'' */
319187801Ssam				c = &chaninfo->ic_chans[promote(i)];
320170531Ssam			}
321170531Ssam			*chan = *c;
322170531Ssam			return;
323170531Ssam		}
324170531Ssam	}
325170531Ssam	errx(1, "unknown/undefined frequency %u/0x%x", freq, flags);
326170531Ssam}
327170531Ssam
328170531Ssamstatic void
329170531Ssammapchan(struct ieee80211_channel *chan, int ieee, int flags)
330170531Ssam{
331298556Spfg	u_int i;
332170531Ssam
333187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
334187801Ssam		const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
335170531Ssam
336170531Ssam		if (c->ic_ieee == ieee && (c->ic_flags & flags) == flags) {
337170531Ssam			if (flags == 0) {
338170531Ssam				/* when ambiguous promote to ``best'' */
339187801Ssam				c = &chaninfo->ic_chans[promote(i)];
340170531Ssam			}
341170531Ssam			*chan = *c;
342170531Ssam			return;
343170531Ssam		}
344170531Ssam	}
345173275Ssam	errx(1, "unknown/undefined channel number %d flags 0x%x", ieee, flags);
346170531Ssam}
347170531Ssam
348173275Ssamstatic const struct ieee80211_channel *
349173275Ssamgetcurchan(int s)
350173275Ssam{
351173275Ssam	if (gotcurchan)
352173275Ssam		return &curchan;
353173275Ssam	if (get80211(s, IEEE80211_IOC_CURCHAN, &curchan, sizeof(curchan)) < 0) {
354173275Ssam		int val;
355173275Ssam		/* fall back to legacy ioctl */
356173275Ssam		if (get80211val(s, IEEE80211_IOC_CHANNEL, &val) < 0)
357187801Ssam			err(-1, "cannot figure out current channel");
358173275Ssam		getchaninfo(s);
359173275Ssam		mapchan(&curchan, val, 0);
360173275Ssam	}
361173275Ssam	gotcurchan = 1;
362173275Ssam	return &curchan;
363173275Ssam}
364173275Ssam
365178354Ssamstatic enum ieee80211_phymode
366178354Ssamchan2mode(const struct ieee80211_channel *c)
367178354Ssam{
368178354Ssam	if (IEEE80211_IS_CHAN_HTA(c))
369178354Ssam		return IEEE80211_MODE_11NA;
370178354Ssam	if (IEEE80211_IS_CHAN_HTG(c))
371178354Ssam		return IEEE80211_MODE_11NG;
372178354Ssam	if (IEEE80211_IS_CHAN_108A(c))
373178354Ssam		return IEEE80211_MODE_TURBO_A;
374178354Ssam	if (IEEE80211_IS_CHAN_108G(c))
375178354Ssam		return IEEE80211_MODE_TURBO_G;
376178354Ssam	if (IEEE80211_IS_CHAN_ST(c))
377178354Ssam		return IEEE80211_MODE_STURBO_A;
378178354Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
379178354Ssam		return IEEE80211_MODE_FH;
380188784Ssam	if (IEEE80211_IS_CHAN_HALF(c))
381188784Ssam		return IEEE80211_MODE_HALF;
382188784Ssam	if (IEEE80211_IS_CHAN_QUARTER(c))
383188784Ssam		return IEEE80211_MODE_QUARTER;
384178354Ssam	if (IEEE80211_IS_CHAN_A(c))
385178354Ssam		return IEEE80211_MODE_11A;
386178354Ssam	if (IEEE80211_IS_CHAN_ANYG(c))
387178354Ssam		return IEEE80211_MODE_11G;
388178354Ssam	if (IEEE80211_IS_CHAN_B(c))
389178354Ssam		return IEEE80211_MODE_11B;
390178354Ssam	return IEEE80211_MODE_AUTO;
391178354Ssam}
392178354Ssam
393178354Ssamstatic void
394178354Ssamgetroam(int s)
395178354Ssam{
396178354Ssam	if (gotroam)
397178354Ssam		return;
398178354Ssam	if (get80211(s, IEEE80211_IOC_ROAM,
399178354Ssam	    &roamparams, sizeof(roamparams)) < 0)
400187801Ssam		err(1, "unable to get roaming parameters");
401178354Ssam	gotroam = 1;
402178354Ssam}
403178354Ssam
404178354Ssamstatic void
405178354Ssamsetroam_cb(int s, void *arg)
406178354Ssam{
407178354Ssam	struct ieee80211_roamparams_req *roam = arg;
408178354Ssam	set80211(s, IEEE80211_IOC_ROAM, 0, sizeof(*roam), roam);
409178354Ssam}
410178354Ssam
411178354Ssamstatic void
412178354Ssamgettxparams(int s)
413178354Ssam{
414178354Ssam	if (gottxparams)
415178354Ssam		return;
416178354Ssam	if (get80211(s, IEEE80211_IOC_TXPARAMS,
417178354Ssam	    &txparams, sizeof(txparams)) < 0)
418187801Ssam		err(1, "unable to get transmit parameters");
419178354Ssam	gottxparams = 1;
420178354Ssam}
421178354Ssam
422178354Ssamstatic void
423178354Ssamsettxparams_cb(int s, void *arg)
424178354Ssam{
425178354Ssam	struct ieee80211_txparams_req *txp = arg;
426178354Ssam	set80211(s, IEEE80211_IOC_TXPARAMS, 0, sizeof(*txp), txp);
427178354Ssam}
428178354Ssam
429178354Ssamstatic void
430178354Ssamgetregdomain(int s)
431178354Ssam{
432178354Ssam	if (gotregdomain)
433178354Ssam		return;
434178354Ssam	if (get80211(s, IEEE80211_IOC_REGDOMAIN,
435178354Ssam	    &regdomain, sizeof(regdomain)) < 0)
436187801Ssam		err(1, "unable to get regulatory domain info");
437178354Ssam	gotregdomain = 1;
438178354Ssam}
439178354Ssam
440178354Ssamstatic void
441178354Ssamgetdevcaps(int s, struct ieee80211_devcaps_req *dc)
442178354Ssam{
443187801Ssam	if (get80211(s, IEEE80211_IOC_DEVCAPS, dc,
444187801Ssam	    IEEE80211_DEVCAPS_SPACE(dc)) < 0)
445187801Ssam		err(1, "unable to get device capabilities");
446178354Ssam}
447178354Ssam
448178354Ssamstatic void
449178354Ssamsetregdomain_cb(int s, void *arg)
450178354Ssam{
451187801Ssam	struct ieee80211_regdomain_req *req;
452178354Ssam	struct ieee80211_regdomain *rd = arg;
453187801Ssam	struct ieee80211_devcaps_req *dc;
454178354Ssam	struct regdata *rdp = getregdata();
455178354Ssam
456186102Ssam	if (rd->country != NO_COUNTRY) {
457178354Ssam		const struct country *cc;
458178354Ssam		/*
459178354Ssam		 * Check current country seting to make sure it's
460178354Ssam		 * compatible with the new regdomain.  If not, then
461178354Ssam		 * override it with any default country for this
462178354Ssam		 * SKU.  If we cannot arrange a match, then abort.
463178354Ssam		 */
464178354Ssam		cc = lib80211_country_findbycc(rdp, rd->country);
465178354Ssam		if (cc == NULL)
466178354Ssam			errx(1, "unknown ISO country code %d", rd->country);
467178354Ssam		if (cc->rd->sku != rd->regdomain) {
468178354Ssam			const struct regdomain *rp;
469178354Ssam			/*
470178354Ssam			 * Check if country is incompatible with regdomain.
471178354Ssam			 * To enable multiple regdomains for a country code
472178354Ssam			 * we permit a mismatch between the regdomain and
473178354Ssam			 * the country's associated regdomain when the
474178354Ssam			 * regdomain is setup w/o a default country.  For
475178354Ssam			 * example, US is bound to the FCC regdomain but
476178354Ssam			 * we allow US to be combined with FCC3 because FCC3
477178354Ssam			 * has not default country.  This allows bogus
478178354Ssam			 * combinations like FCC3+DK which are resolved when
479178354Ssam			 * constructing the channel list by deferring to the
480178354Ssam			 * regdomain to construct the channel list.
481178354Ssam			 */
482178354Ssam			rp = lib80211_regdomain_findbysku(rdp, rd->regdomain);
483178354Ssam			if (rp == NULL)
484178354Ssam				errx(1, "country %s (%s) is not usable with "
485178354Ssam				    "regdomain %d", cc->isoname, cc->name,
486178354Ssam				    rd->regdomain);
487186102Ssam			else if (rp->cc != NULL && rp->cc != cc)
488178354Ssam				errx(1, "country %s (%s) is not usable with "
489178354Ssam				   "regdomain %s", cc->isoname, cc->name,
490178354Ssam				   rp->name);
491178354Ssam		}
492178354Ssam	}
493178354Ssam	/*
494178354Ssam	 * Fetch the device capabilities and calculate the
495178354Ssam	 * full set of netbands for which we request a new
496178354Ssam	 * channel list be constructed.  Once that's done we
497178354Ssam	 * push the regdomain info + channel list to the kernel.
498178354Ssam	 */
499187801Ssam	dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
500187801Ssam	if (dc == NULL)
501187801Ssam		errx(1, "no space for device capabilities");
502187801Ssam	dc->dc_chaninfo.ic_nchans = MAXCHAN;
503187801Ssam	getdevcaps(s, dc);
504178354Ssam#if 0
505178354Ssam	if (verbose) {
506187801Ssam		printf("drivercaps: 0x%x\n", dc->dc_drivercaps);
507187801Ssam		printf("cryptocaps: 0x%x\n", dc->dc_cryptocaps);
508187801Ssam		printf("htcaps    : 0x%x\n", dc->dc_htcaps);
509187801Ssam		memcpy(chaninfo, &dc->dc_chaninfo,
510187801Ssam		    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
511187801Ssam		print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, 1/*verbose*/);
512178354Ssam	}
513178354Ssam#endif
514187801Ssam	req = malloc(IEEE80211_REGDOMAIN_SIZE(dc->dc_chaninfo.ic_nchans));
515187801Ssam	if (req == NULL)
516187801Ssam		errx(1, "no space for regdomain request");
517187801Ssam	req->rd = *rd;
518187801Ssam	regdomain_makechannels(req, dc);
519178354Ssam	if (verbose) {
520178354Ssam		LINE_INIT(':');
521178354Ssam		print_regdomain(rd, 1/*verbose*/);
522178354Ssam		LINE_BREAK();
523187801Ssam		/* blech, reallocate channel list for new data */
524187801Ssam		if (chaninfo != NULL)
525187801Ssam			free(chaninfo);
526187801Ssam		chaninfo = malloc(IEEE80211_CHANINFO_SPACE(&req->chaninfo));
527187801Ssam		if (chaninfo == NULL)
528187801Ssam			errx(1, "no space for channel list");
529187801Ssam		memcpy(chaninfo, &req->chaninfo,
530187801Ssam		    IEEE80211_CHANINFO_SPACE(&req->chaninfo));
531187801Ssam		print_channels(s, &req->chaninfo, 1/*allchans*/, 1/*verbose*/);
532178354Ssam	}
533187801Ssam	if (req->chaninfo.ic_nchans == 0)
534178354Ssam		errx(1, "no channels calculated");
535187801Ssam	set80211(s, IEEE80211_IOC_REGDOMAIN, 0,
536187801Ssam	    IEEE80211_REGDOMAIN_SPACE(req), req);
537187801Ssam	free(req);
538187801Ssam	free(dc);
539178354Ssam}
540178354Ssam
541170531Ssamstatic int
542170531Ssamieee80211_mhz2ieee(int freq, int flags)
543170531Ssam{
544170531Ssam	struct ieee80211_channel chan;
545170531Ssam	mapfreq(&chan, freq, flags);
546170531Ssam	return chan.ic_ieee;
547170531Ssam}
548170531Ssam
549170531Ssamstatic int
550138593Ssamisanyarg(const char *arg)
551138593Ssam{
552173275Ssam	return (strncmp(arg, "-", 1) == 0 ||
553173275Ssam	    strncasecmp(arg, "any", 3) == 0 || strncasecmp(arg, "off", 3) == 0);
554138593Ssam}
555138593Ssam
556138593Ssamstatic void
55777218Sphkset80211ssid(const char *val, int d, int s, const struct afswtch *rafp)
55877218Sphk{
55977218Sphk	int		ssid;
56077218Sphk	int		len;
561151883Sbrooks	u_int8_t	data[IEEE80211_NWID_LEN];
56277218Sphk
56377218Sphk	ssid = 0;
564121827Sbrooks	len = strlen(val);
565178354Ssam	if (len > 2 && isdigit((int)val[0]) && val[1] == ':') {
56688748Sambrisko		ssid = atoi(val)-1;
56788748Sambrisko		val += 2;
56888748Sambrisko	}
56977218Sphk
57077218Sphk	bzero(data, sizeof(data));
57177218Sphk	len = sizeof(data);
572151883Sbrooks	if (get_string(val, NULL, data, &len) == NULL)
573151883Sbrooks		exit(1);
57477218Sphk
57577218Sphk	set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
57677218Sphk}
57777218Sphk
578138593Ssamstatic void
579195618Srpauloset80211meshid(const char *val, int d, int s, const struct afswtch *rafp)
580195618Srpaulo{
581195618Srpaulo	int		len;
582195618Srpaulo	u_int8_t	data[IEEE80211_NWID_LEN];
583195618Srpaulo
584195618Srpaulo	memset(data, 0, sizeof(data));
585195618Srpaulo	len = sizeof(data);
586195618Srpaulo	if (get_string(val, NULL, data, &len) == NULL)
587195618Srpaulo		exit(1);
588195618Srpaulo
589195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_ID, 0, len, data);
590195618Srpaulo}
591195618Srpaulo
592195618Srpaulostatic void
59377218Sphkset80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
59477218Sphk{
59577218Sphk	int			len;
59677218Sphk	u_int8_t		data[33];
59777218Sphk
59877218Sphk	bzero(data, sizeof(data));
59977218Sphk	len = sizeof(data);
60077218Sphk	get_string(val, NULL, data, &len);
60177218Sphk
60277218Sphk	set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
60377218Sphk}
60477218Sphk
605138593Ssam/*
606170531Ssam * Parse a channel specification for attributes/flags.
607170531Ssam * The syntax is:
608170531Ssam *	freq/xx		channel width (5,10,20,40,40+,40-)
609170531Ssam *	freq:mode	channel mode (a,b,g,h,n,t,s,d)
610170531Ssam *
611170531Ssam * These can be combined in either order; e.g. 2437:ng/40.
612170531Ssam * Modes are case insensitive.
613170531Ssam *
614170531Ssam * The result is not validated here; it's assumed to be
615170531Ssam * checked against the channel table fetched from the kernel.
616170531Ssam */
617170531Ssamstatic int
618173275Ssamgetchannelflags(const char *val, int freq)
619138593Ssam{
620170531Ssam#define	_CHAN_HT	0x80000000
621170531Ssam	const char *cp;
622170531Ssam	int flags;
623138593Ssam
624170531Ssam	flags = 0;
625166015Ssam
626170531Ssam	cp = strchr(val, ':');
627170531Ssam	if (cp != NULL) {
628170531Ssam		for (cp++; isalpha((int) *cp); cp++) {
629170531Ssam			/* accept mixed case */
630170531Ssam			int c = *cp;
631170531Ssam			if (isupper(c))
632170531Ssam				c = tolower(c);
633170531Ssam			switch (c) {
634170531Ssam			case 'a':		/* 802.11a */
635170531Ssam				flags |= IEEE80211_CHAN_A;
636170531Ssam				break;
637170531Ssam			case 'b':		/* 802.11b */
638170531Ssam				flags |= IEEE80211_CHAN_B;
639170531Ssam				break;
640170531Ssam			case 'g':		/* 802.11g */
641170531Ssam				flags |= IEEE80211_CHAN_G;
642170531Ssam				break;
643170531Ssam			case 'h':		/* ht = 802.11n */
644170531Ssam			case 'n':		/* 802.11n */
645170531Ssam				flags |= _CHAN_HT;	/* NB: private */
646170531Ssam				break;
647170531Ssam			case 'd':		/* dt = Atheros Dynamic Turbo */
648170531Ssam				flags |= IEEE80211_CHAN_TURBO;
649170531Ssam				break;
650170531Ssam			case 't':		/* ht, dt, st, t */
651170531Ssam				/* dt and unadorned t specify Dynamic Turbo */
652170531Ssam				if ((flags & (IEEE80211_CHAN_STURBO|_CHAN_HT)) == 0)
653170531Ssam					flags |= IEEE80211_CHAN_TURBO;
654170531Ssam				break;
655170531Ssam			case 's':		/* st = Atheros Static Turbo */
656170531Ssam				flags |= IEEE80211_CHAN_STURBO;
657170531Ssam				break;
658170531Ssam			default:
659173275Ssam				errx(-1, "%s: Invalid channel attribute %c\n",
660170531Ssam				    val, *cp);
661170531Ssam			}
662170531Ssam		}
663170531Ssam	}
664170531Ssam	cp = strchr(val, '/');
665170531Ssam	if (cp != NULL) {
666170531Ssam		char *ep;
667170531Ssam		u_long cw = strtoul(cp+1, &ep, 10);
668166015Ssam
669170531Ssam		switch (cw) {
670170531Ssam		case 5:
671170531Ssam			flags |= IEEE80211_CHAN_QUARTER;
672170531Ssam			break;
673170531Ssam		case 10:
674170531Ssam			flags |= IEEE80211_CHAN_HALF;
675170531Ssam			break;
676170531Ssam		case 20:
677170531Ssam			/* NB: this may be removed below */
678170531Ssam			flags |= IEEE80211_CHAN_HT20;
679170531Ssam			break;
680170531Ssam		case 40:
681170531Ssam			if (ep != NULL && *ep == '+')
682170531Ssam				flags |= IEEE80211_CHAN_HT40U;
683170531Ssam			else if (ep != NULL && *ep == '-')
684170531Ssam				flags |= IEEE80211_CHAN_HT40D;
685170531Ssam			break;
686170531Ssam		default:
687173275Ssam			errx(-1, "%s: Invalid channel width\n", val);
688170531Ssam		}
689165570Ssam	}
690170531Ssam	/*
691170531Ssam	 * Cleanup specifications.
692170531Ssam	 */
693170531Ssam	if ((flags & _CHAN_HT) == 0) {
694170531Ssam		/*
695170531Ssam		 * If user specified freq/20 or freq/40 quietly remove
696170531Ssam		 * HT cw attributes depending on channel use.  To give
697170531Ssam		 * an explicit 20/40 width for an HT channel you must
698170531Ssam		 * indicate it is an HT channel since all HT channels
699170531Ssam		 * are also usable for legacy operation; e.g. freq:n/40.
700170531Ssam		 */
701170531Ssam		flags &= ~IEEE80211_CHAN_HT;
702170531Ssam	} else {
703170531Ssam		/*
704170531Ssam		 * Remove private indicator that this is an HT channel
705170531Ssam		 * and if no explicit channel width has been given
706170531Ssam		 * provide the default settings.
707170531Ssam		 */
708170531Ssam		flags &= ~_CHAN_HT;
709173275Ssam		if ((flags & IEEE80211_CHAN_HT) == 0) {
710173275Ssam			struct ieee80211_channel chan;
711173275Ssam			/*
712173275Ssam			 * Consult the channel list to see if we can use
713173275Ssam			 * HT40+ or HT40- (if both the map routines choose).
714173275Ssam			 */
715173275Ssam			if (freq > 255)
716173275Ssam				mapfreq(&chan, freq, 0);
717173275Ssam			else
718173275Ssam				mapchan(&chan, freq, 0);
719173275Ssam			flags |= (chan.ic_flags & IEEE80211_CHAN_HT);
720173275Ssam		}
721170531Ssam	}
722170531Ssam	return flags;
723170531Ssam#undef _CHAN_HT
724138593Ssam}
725138593Ssam
726138593Ssamstatic void
727186105Ssamgetchannel(int s, struct ieee80211_channel *chan, const char *val)
728186105Ssam{
729186105Ssam	int v, flags;
730186105Ssam	char *eptr;
731186105Ssam
732186105Ssam	memset(chan, 0, sizeof(*chan));
733186105Ssam	if (isanyarg(val)) {
734186105Ssam		chan->ic_freq = IEEE80211_CHAN_ANY;
735186105Ssam		return;
736186105Ssam	}
737186105Ssam	getchaninfo(s);
738186105Ssam	errno = 0;
739186105Ssam	v = strtol(val, &eptr, 10);
740186105Ssam	if (val[0] == '\0' || val == eptr || errno == ERANGE ||
741186105Ssam	    /* channel may be suffixed with nothing, :flag, or /width */
742186105Ssam	    (eptr[0] != '\0' && eptr[0] != ':' && eptr[0] != '/'))
743186105Ssam		errx(1, "invalid channel specification%s",
744186105Ssam		    errno == ERANGE ? " (out of range)" : "");
745186105Ssam	flags = getchannelflags(val, v);
746186105Ssam	if (v > 255) {		/* treat as frequency */
747186105Ssam		mapfreq(chan, v, flags);
748186105Ssam	} else {
749186105Ssam		mapchan(chan, v, flags);
750186105Ssam	}
751186105Ssam}
752186105Ssam
753186105Ssamstatic void
75477218Sphkset80211channel(const char *val, int d, int s, const struct afswtch *rafp)
75577218Sphk{
756170531Ssam	struct ieee80211_channel chan;
757170531Ssam
758186105Ssam	getchannel(s, &chan, val);
759170531Ssam	set80211(s, IEEE80211_IOC_CURCHAN, 0, sizeof(chan), &chan);
76077218Sphk}
76177218Sphk
762138593Ssamstatic void
763178354Ssamset80211chanswitch(const char *val, int d, int s, const struct afswtch *rafp)
764178354Ssam{
765178354Ssam	struct ieee80211_chanswitch_req csr;
766178354Ssam
767186105Ssam	getchannel(s, &csr.csa_chan, val);
768178354Ssam	csr.csa_mode = 1;
769178354Ssam	csr.csa_count = 5;
770178354Ssam	set80211(s, IEEE80211_IOC_CHANSWITCH, 0, sizeof(csr), &csr);
771178354Ssam}
772178354Ssam
773178354Ssamstatic void
77477218Sphkset80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
77577218Sphk{
77677218Sphk	int	mode;
77777218Sphk
77891454Sbrooks	if (strcasecmp(val, "none") == 0) {
77977218Sphk		mode = IEEE80211_AUTH_NONE;
78091454Sbrooks	} else if (strcasecmp(val, "open") == 0) {
78177218Sphk		mode = IEEE80211_AUTH_OPEN;
78291454Sbrooks	} else if (strcasecmp(val, "shared") == 0) {
78377218Sphk		mode = IEEE80211_AUTH_SHARED;
784138593Ssam	} else if (strcasecmp(val, "8021x") == 0) {
785138593Ssam		mode = IEEE80211_AUTH_8021X;
786138593Ssam	} else if (strcasecmp(val, "wpa") == 0) {
787138593Ssam		mode = IEEE80211_AUTH_WPA;
78877218Sphk	} else {
789150708Sru		errx(1, "unknown authmode");
79077218Sphk	}
79177218Sphk
79277218Sphk	set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
79377218Sphk}
79477218Sphk
795138593Ssamstatic void
79677218Sphkset80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
79777218Sphk{
79877218Sphk	int	mode;
79977218Sphk
80091454Sbrooks	if (strcasecmp(val, "off") == 0) {
80177218Sphk		mode = IEEE80211_POWERSAVE_OFF;
80291454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
80377218Sphk		mode = IEEE80211_POWERSAVE_ON;
80491454Sbrooks	} else if (strcasecmp(val, "cam") == 0) {
80577218Sphk		mode = IEEE80211_POWERSAVE_CAM;
80691454Sbrooks	} else if (strcasecmp(val, "psp") == 0) {
80777218Sphk		mode = IEEE80211_POWERSAVE_PSP;
80891454Sbrooks	} else if (strcasecmp(val, "psp-cam") == 0) {
80977218Sphk		mode = IEEE80211_POWERSAVE_PSP_CAM;
81077218Sphk	} else {
811150708Sru		errx(1, "unknown powersavemode");
81277218Sphk	}
81377218Sphk
81477218Sphk	set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
81577218Sphk}
81677218Sphk
817138593Ssamstatic void
81877218Sphkset80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
81977218Sphk{
82077218Sphk	if (d == 0)
82177218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
82277218Sphk		    0, NULL);
82377218Sphk	else
82477218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
82577218Sphk		    0, NULL);
82677218Sphk}
82777218Sphk
828138593Ssamstatic void
82977218Sphkset80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
83077218Sphk{
83177218Sphk	set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
83277218Sphk}
83377218Sphk
834138593Ssamstatic void
83577218Sphkset80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
83677218Sphk{
83777218Sphk	int	mode;
83877218Sphk
83991454Sbrooks	if (strcasecmp(val, "off") == 0) {
84077218Sphk		mode = IEEE80211_WEP_OFF;
84191454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
84277218Sphk		mode = IEEE80211_WEP_ON;
84391454Sbrooks	} else if (strcasecmp(val, "mixed") == 0) {
84477218Sphk		mode = IEEE80211_WEP_MIXED;
84577218Sphk	} else {
846150708Sru		errx(1, "unknown wep mode");
84777218Sphk	}
84877218Sphk
84977218Sphk	set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
85077218Sphk}
85177218Sphk
852138593Ssamstatic void
85377218Sphkset80211wep(const char *val, int d, int s, const struct afswtch *rafp)
85477218Sphk{
85577218Sphk	set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
85677218Sphk}
85777218Sphk
858139493Ssamstatic int
859139493Ssamisundefarg(const char *arg)
860139493Ssam{
861139493Ssam	return (strcmp(arg, "-") == 0 || strncasecmp(arg, "undef", 5) == 0);
862139493Ssam}
863139493Ssam
864138593Ssamstatic void
86577218Sphkset80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
86677218Sphk{
867139493Ssam	if (isundefarg(val))
868139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, IEEE80211_KEYIX_NONE, 0, NULL);
869139493Ssam	else
870139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
87177218Sphk}
87277218Sphk
873138593Ssamstatic void
87477218Sphkset80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
87577218Sphk{
87677218Sphk	int		key = 0;
87777218Sphk	int		len;
878120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
87977218Sphk
880178354Ssam	if (isdigit((int)val[0]) && val[1] == ':') {
88177218Sphk		key = atoi(val)-1;
88277218Sphk		val += 2;
88377218Sphk	}
88477218Sphk
88577218Sphk	bzero(data, sizeof(data));
88677218Sphk	len = sizeof(data);
88777218Sphk	get_string(val, NULL, data, &len);
88877218Sphk
88977218Sphk	set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
89077218Sphk}
89177218Sphk
89277218Sphk/*
893229778Suqs * This function is purely a NetBSD compatibility interface.  The NetBSD
894148686Sstefanf * interface is too inflexible, but it's there so we'll support it since
89577218Sphk * it's not all that hard.
89677218Sphk */
897138593Ssamstatic void
89877218Sphkset80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
89977218Sphk{
90077218Sphk	int		txkey;
90177218Sphk	int		i, len;
902120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
90377218Sphk
90477218Sphk	set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
90577218Sphk
906178354Ssam	if (isdigit((int)val[0]) && val[1] == ':') {
90777218Sphk		txkey = val[0]-'0'-1;
90877218Sphk		val += 2;
90977218Sphk
91091454Sbrooks		for (i = 0; i < 4; i++) {
91177218Sphk			bzero(data, sizeof(data));
91277218Sphk			len = sizeof(data);
91377218Sphk			val = get_string(val, ",", data, &len);
914151827Sbrooks			if (val == NULL)
915151827Sbrooks				exit(1);
91677218Sphk
91777218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
91877218Sphk		}
91977218Sphk	} else {
92077218Sphk		bzero(data, sizeof(data));
92177218Sphk		len = sizeof(data);
92277218Sphk		get_string(val, NULL, data, &len);
92377218Sphk		txkey = 0;
92477218Sphk
92577218Sphk		set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
92677218Sphk
92777218Sphk		bzero(data, sizeof(data));
92891454Sbrooks		for (i = 1; i < 4; i++)
92977218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
93077218Sphk	}
93177218Sphk
93277218Sphk	set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
93377218Sphk}
93477218Sphk
935138593Ssamstatic void
936127649Ssamset80211rtsthreshold(const char *val, int d, int s, const struct afswtch *rafp)
937127649Ssam{
938148416Ssam	set80211(s, IEEE80211_IOC_RTSTHRESHOLD,
939148416Ssam		isundefarg(val) ? IEEE80211_RTS_MAX : atoi(val), 0, NULL);
940127649Ssam}
941127649Ssam
942138593Ssamstatic void
943127649Ssamset80211protmode(const char *val, int d, int s, const struct afswtch *rafp)
944127649Ssam{
945127649Ssam	int	mode;
946127649Ssam
947127649Ssam	if (strcasecmp(val, "off") == 0) {
948127649Ssam		mode = IEEE80211_PROTMODE_OFF;
949127649Ssam	} else if (strcasecmp(val, "cts") == 0) {
950127649Ssam		mode = IEEE80211_PROTMODE_CTS;
951173275Ssam	} else if (strncasecmp(val, "rtscts", 3) == 0) {
952127649Ssam		mode = IEEE80211_PROTMODE_RTSCTS;
953127649Ssam	} else {
954150708Sru		errx(1, "unknown protection mode");
955127649Ssam	}
956127649Ssam
957127649Ssam	set80211(s, IEEE80211_IOC_PROTMODE, mode, 0, NULL);
958127649Ssam}
959127649Ssam
960138593Ssamstatic void
961173275Ssamset80211htprotmode(const char *val, int d, int s, const struct afswtch *rafp)
962173275Ssam{
963173275Ssam	int	mode;
964173275Ssam
965173275Ssam	if (strcasecmp(val, "off") == 0) {
966173275Ssam		mode = IEEE80211_PROTMODE_OFF;
967173275Ssam	} else if (strncasecmp(val, "rts", 3) == 0) {
968173275Ssam		mode = IEEE80211_PROTMODE_RTSCTS;
969173275Ssam	} else {
970173275Ssam		errx(1, "unknown protection mode");
971173275Ssam	}
972173275Ssam
973173275Ssam	set80211(s, IEEE80211_IOC_HTPROTMODE, mode, 0, NULL);
974173275Ssam}
975173275Ssam
976173275Ssamstatic void
977127649Ssamset80211txpower(const char *val, int d, int s, const struct afswtch *rafp)
978127649Ssam{
979173275Ssam	double v = atof(val);
980173275Ssam	int txpow;
981173275Ssam
982173275Ssam	txpow = (int) (2*v);
983173275Ssam	if (txpow != 2*v)
984173275Ssam		errx(-1, "invalid tx power (must be .5 dBm units)");
985173275Ssam	set80211(s, IEEE80211_IOC_TXPOWER, txpow, 0, NULL);
986127649Ssam}
987127649Ssam
988138593Ssam#define	IEEE80211_ROAMING_DEVICE	0
989138593Ssam#define	IEEE80211_ROAMING_AUTO		1
990138593Ssam#define	IEEE80211_ROAMING_MANUAL	2
991138593Ssam
992138593Ssamstatic void
993138593Ssamset80211roaming(const char *val, int d, int s, const struct afswtch *rafp)
99477218Sphk{
995138593Ssam	int mode;
99677218Sphk
997138593Ssam	if (strcasecmp(val, "device") == 0) {
998138593Ssam		mode = IEEE80211_ROAMING_DEVICE;
999138593Ssam	} else if (strcasecmp(val, "auto") == 0) {
1000138593Ssam		mode = IEEE80211_ROAMING_AUTO;
1001138593Ssam	} else if (strcasecmp(val, "manual") == 0) {
1002138593Ssam		mode = IEEE80211_ROAMING_MANUAL;
1003138593Ssam	} else {
1004150708Sru		errx(1, "unknown roaming mode");
1005138593Ssam	}
1006138593Ssam	set80211(s, IEEE80211_IOC_ROAMING, mode, 0, NULL);
1007138593Ssam}
1008138593Ssam
1009138593Ssamstatic void
1010138593Ssamset80211wme(const char *val, int d, int s, const struct afswtch *rafp)
1011138593Ssam{
1012138593Ssam	set80211(s, IEEE80211_IOC_WME, d, 0, NULL);
1013138593Ssam}
1014138593Ssam
1015138593Ssamstatic void
1016138593Ssamset80211hidessid(const char *val, int d, int s, const struct afswtch *rafp)
1017138593Ssam{
1018138593Ssam	set80211(s, IEEE80211_IOC_HIDESSID, d, 0, NULL);
1019138593Ssam}
1020138593Ssam
1021138593Ssamstatic void
1022138593Ssamset80211apbridge(const char *val, int d, int s, const struct afswtch *rafp)
1023138593Ssam{
1024138593Ssam	set80211(s, IEEE80211_IOC_APBRIDGE, d, 0, NULL);
1025138593Ssam}
1026138593Ssam
1027138593Ssamstatic void
1028170531Ssamset80211fastframes(const char *val, int d, int s, const struct afswtch *rafp)
1029170531Ssam{
1030170531Ssam	set80211(s, IEEE80211_IOC_FF, d, 0, NULL);
1031170531Ssam}
1032170531Ssam
1033170531Ssamstatic void
1034170531Ssamset80211dturbo(const char *val, int d, int s, const struct afswtch *rafp)
1035170531Ssam{
1036170531Ssam	set80211(s, IEEE80211_IOC_TURBOP, d, 0, NULL);
1037170531Ssam}
1038170531Ssam
1039170531Ssamstatic void
1040138593Ssamset80211chanlist(const char *val, int d, int s, const struct afswtch *rafp)
1041138593Ssam{
1042138593Ssam	struct ieee80211req_chanlist chanlist;
1043138593Ssam	char *temp, *cp, *tp;
1044138593Ssam
1045138593Ssam	temp = malloc(strlen(val) + 1);
1046138593Ssam	if (temp == NULL)
1047138593Ssam		errx(1, "malloc failed");
1048138593Ssam	strcpy(temp, val);
1049138593Ssam	memset(&chanlist, 0, sizeof(chanlist));
1050138593Ssam	cp = temp;
1051138593Ssam	for (;;) {
1052173275Ssam		int first, last, f, c;
1053138593Ssam
1054138593Ssam		tp = strchr(cp, ',');
1055138593Ssam		if (tp != NULL)
1056138593Ssam			*tp++ = '\0';
1057138593Ssam		switch (sscanf(cp, "%u-%u", &first, &last)) {
1058138593Ssam		case 1:
1059187801Ssam			if (first > IEEE80211_CHAN_MAX)
1060188038Sdelphij				errx(-1, "channel %u out of range, max %u",
1061187801Ssam					first, IEEE80211_CHAN_MAX);
1062138593Ssam			setbit(chanlist.ic_channels, first);
1063138593Ssam			break;
1064138593Ssam		case 2:
1065187801Ssam			if (first > IEEE80211_CHAN_MAX)
1066188038Sdelphij				errx(-1, "channel %u out of range, max %u",
1067187801Ssam					first, IEEE80211_CHAN_MAX);
1068187801Ssam			if (last > IEEE80211_CHAN_MAX)
1069188038Sdelphij				errx(-1, "channel %u out of range, max %u",
1070187801Ssam					last, IEEE80211_CHAN_MAX);
1071138593Ssam			if (first > last)
1072138593Ssam				errx(-1, "void channel range, %u > %u",
1073138593Ssam					first, last);
1074138593Ssam			for (f = first; f <= last; f++)
1075138593Ssam				setbit(chanlist.ic_channels, f);
1076138593Ssam			break;
1077138593Ssam		}
1078138593Ssam		if (tp == NULL)
1079138593Ssam			break;
1080173275Ssam		c = *tp;
1081173275Ssam		while (isspace(c))
1082138593Ssam			tp++;
1083173275Ssam		if (!isdigit(c))
1084138593Ssam			break;
1085138593Ssam		cp = tp;
1086138593Ssam	}
1087170531Ssam	set80211(s, IEEE80211_IOC_CHANLIST, 0, sizeof(chanlist), &chanlist);
1088330468Seadler	free(temp);
1089138593Ssam}
1090138593Ssam
1091138593Ssamstatic void
1092138593Ssamset80211bssid(const char *val, int d, int s, const struct afswtch *rafp)
1093138593Ssam{
1094138593Ssam
1095138593Ssam	if (!isanyarg(val)) {
1096138593Ssam		char *temp;
1097138593Ssam		struct sockaddr_dl sdl;
1098138593Ssam
1099155702Ssam		temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1100138593Ssam		if (temp == NULL)
1101138593Ssam			errx(1, "malloc failed");
1102138593Ssam		temp[0] = ':';
1103138593Ssam		strcpy(temp + 1, val);
1104138593Ssam		sdl.sdl_len = sizeof(sdl);
1105138593Ssam		link_addr(temp, &sdl);
1106138593Ssam		free(temp);
1107138593Ssam		if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1108138593Ssam			errx(1, "malformed link-level address");
1109138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
1110138593Ssam			IEEE80211_ADDR_LEN, LLADDR(&sdl));
1111138593Ssam	} else {
1112138593Ssam		uint8_t zerobssid[IEEE80211_ADDR_LEN];
1113138593Ssam		memset(zerobssid, 0, sizeof(zerobssid));
1114138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
1115138593Ssam			IEEE80211_ADDR_LEN, zerobssid);
1116138593Ssam	}
1117138593Ssam}
1118138593Ssam
1119138593Ssamstatic int
1120138593Ssamgetac(const char *ac)
1121138593Ssam{
1122138593Ssam	if (strcasecmp(ac, "ac_be") == 0 || strcasecmp(ac, "be") == 0)
1123138593Ssam		return WME_AC_BE;
1124138593Ssam	if (strcasecmp(ac, "ac_bk") == 0 || strcasecmp(ac, "bk") == 0)
1125138593Ssam		return WME_AC_BK;
1126138593Ssam	if (strcasecmp(ac, "ac_vi") == 0 || strcasecmp(ac, "vi") == 0)
1127138593Ssam		return WME_AC_VI;
1128138593Ssam	if (strcasecmp(ac, "ac_vo") == 0 || strcasecmp(ac, "vo") == 0)
1129138593Ssam		return WME_AC_VO;
1130138593Ssam	errx(1, "unknown wme access class %s", ac);
1131138593Ssam}
1132138593Ssam
1133138593Ssamstatic
1134138593SsamDECL_CMD_FUNC2(set80211cwmin, ac, val)
1135138593Ssam{
1136138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val), getac(ac), NULL);
1137138593Ssam}
1138138593Ssam
1139138593Ssamstatic
1140138593SsamDECL_CMD_FUNC2(set80211cwmax, ac, val)
1141138593Ssam{
1142138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val), getac(ac), NULL);
1143138593Ssam}
1144138593Ssam
1145138593Ssamstatic
1146138593SsamDECL_CMD_FUNC2(set80211aifs, ac, val)
1147138593Ssam{
1148138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val), getac(ac), NULL);
1149138593Ssam}
1150138593Ssam
1151138593Ssamstatic
1152138593SsamDECL_CMD_FUNC2(set80211txoplimit, ac, val)
1153138593Ssam{
1154138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), getac(ac), NULL);
1155138593Ssam}
1156138593Ssam
1157138593Ssamstatic
1158148621SsamDECL_CMD_FUNC(set80211acm, ac, d)
1159138593Ssam{
1160148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 1, getac(ac), NULL);
1161138593Ssam}
1162148621Ssamstatic
1163148621SsamDECL_CMD_FUNC(set80211noacm, ac, d)
1164148621Ssam{
1165148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 0, getac(ac), NULL);
1166148621Ssam}
1167138593Ssam
1168138593Ssamstatic
1169148621SsamDECL_CMD_FUNC(set80211ackpolicy, ac, d)
1170138593Ssam{
1171148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 1, getac(ac), NULL);
1172138593Ssam}
1173148621Ssamstatic
1174148621SsamDECL_CMD_FUNC(set80211noackpolicy, ac, d)
1175148621Ssam{
1176148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 0, getac(ac), NULL);
1177148621Ssam}
1178138593Ssam
1179138593Ssamstatic
1180138593SsamDECL_CMD_FUNC2(set80211bsscwmin, ac, val)
1181138593Ssam{
1182138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val),
1183138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1184138593Ssam}
1185138593Ssam
1186138593Ssamstatic
1187138593SsamDECL_CMD_FUNC2(set80211bsscwmax, ac, val)
1188138593Ssam{
1189138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val),
1190138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1191138593Ssam}
1192138593Ssam
1193138593Ssamstatic
1194138593SsamDECL_CMD_FUNC2(set80211bssaifs, ac, val)
1195138593Ssam{
1196138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val),
1197138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1198138593Ssam}
1199138593Ssam
1200138593Ssamstatic
1201138593SsamDECL_CMD_FUNC2(set80211bsstxoplimit, ac, val)
1202138593Ssam{
1203138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val),
1204138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1205138593Ssam}
1206138593Ssam
1207138593Ssamstatic
1208138593SsamDECL_CMD_FUNC(set80211dtimperiod, val, d)
1209138593Ssam{
1210138593Ssam	set80211(s, IEEE80211_IOC_DTIM_PERIOD, atoi(val), 0, NULL);
1211138593Ssam}
1212138593Ssam
1213138593Ssamstatic
1214138593SsamDECL_CMD_FUNC(set80211bintval, val, d)
1215138593Ssam{
1216138593Ssam	set80211(s, IEEE80211_IOC_BEACON_INTERVAL, atoi(val), 0, NULL);
1217138593Ssam}
1218138593Ssam
1219138593Ssamstatic void
1220138593Ssamset80211macmac(int s, int op, const char *val)
1221138593Ssam{
1222138593Ssam	char *temp;
1223138593Ssam	struct sockaddr_dl sdl;
1224138593Ssam
1225155702Ssam	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1226138593Ssam	if (temp == NULL)
1227138593Ssam		errx(1, "malloc failed");
1228138593Ssam	temp[0] = ':';
1229138593Ssam	strcpy(temp + 1, val);
1230138593Ssam	sdl.sdl_len = sizeof(sdl);
1231138593Ssam	link_addr(temp, &sdl);
1232138593Ssam	free(temp);
1233138593Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1234138593Ssam		errx(1, "malformed link-level address");
1235138593Ssam	set80211(s, op, 0, IEEE80211_ADDR_LEN, LLADDR(&sdl));
1236138593Ssam}
1237138593Ssam
1238138593Ssamstatic
1239138593SsamDECL_CMD_FUNC(set80211addmac, val, d)
1240138593Ssam{
1241138593Ssam	set80211macmac(s, IEEE80211_IOC_ADDMAC, val);
1242138593Ssam}
1243138593Ssam
1244138593Ssamstatic
1245138593SsamDECL_CMD_FUNC(set80211delmac, val, d)
1246138593Ssam{
1247138593Ssam	set80211macmac(s, IEEE80211_IOC_DELMAC, val);
1248138593Ssam}
1249138593Ssam
1250138593Ssamstatic
1251149029SsamDECL_CMD_FUNC(set80211kickmac, val, d)
1252149029Ssam{
1253149029Ssam	char *temp;
1254149029Ssam	struct sockaddr_dl sdl;
1255149029Ssam	struct ieee80211req_mlme mlme;
1256149029Ssam
1257155702Ssam	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1258149029Ssam	if (temp == NULL)
1259149029Ssam		errx(1, "malloc failed");
1260149029Ssam	temp[0] = ':';
1261149029Ssam	strcpy(temp + 1, val);
1262149029Ssam	sdl.sdl_len = sizeof(sdl);
1263149029Ssam	link_addr(temp, &sdl);
1264149029Ssam	free(temp);
1265149029Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1266149029Ssam		errx(1, "malformed link-level address");
1267149029Ssam	memset(&mlme, 0, sizeof(mlme));
1268149029Ssam	mlme.im_op = IEEE80211_MLME_DEAUTH;
1269149029Ssam	mlme.im_reason = IEEE80211_REASON_AUTH_EXPIRE;
1270149029Ssam	memcpy(mlme.im_macaddr, LLADDR(&sdl), IEEE80211_ADDR_LEN);
1271170531Ssam	set80211(s, IEEE80211_IOC_MLME, 0, sizeof(mlme), &mlme);
1272149029Ssam}
1273149029Ssam
1274149029Ssamstatic
1275138593SsamDECL_CMD_FUNC(set80211maccmd, val, d)
1276138593Ssam{
1277138593Ssam	set80211(s, IEEE80211_IOC_MACCMD, d, 0, NULL);
1278138593Ssam}
1279138593Ssam
1280147795Ssamstatic void
1281195618Srpauloset80211meshrtmac(int s, int req, const char *val)
1282195618Srpaulo{
1283195618Srpaulo	char *temp;
1284195618Srpaulo	struct sockaddr_dl sdl;
1285195618Srpaulo
1286195618Srpaulo	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1287195618Srpaulo	if (temp == NULL)
1288195618Srpaulo		errx(1, "malloc failed");
1289195618Srpaulo	temp[0] = ':';
1290195618Srpaulo	strcpy(temp + 1, val);
1291195618Srpaulo	sdl.sdl_len = sizeof(sdl);
1292195618Srpaulo	link_addr(temp, &sdl);
1293195618Srpaulo	free(temp);
1294195618Srpaulo	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1295195618Srpaulo		errx(1, "malformed link-level address");
1296195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_RTCMD, req,
1297195618Srpaulo	    IEEE80211_ADDR_LEN, LLADDR(&sdl));
1298195618Srpaulo}
1299195618Srpaulo
1300195618Srpaulostatic
1301195618SrpauloDECL_CMD_FUNC(set80211addmeshrt, val, d)
1302195618Srpaulo{
1303195618Srpaulo	set80211meshrtmac(s, IEEE80211_MESH_RTCMD_ADD, val);
1304195618Srpaulo}
1305195618Srpaulo
1306195618Srpaulostatic
1307195618SrpauloDECL_CMD_FUNC(set80211delmeshrt, val, d)
1308195618Srpaulo{
1309195618Srpaulo	set80211meshrtmac(s, IEEE80211_MESH_RTCMD_DELETE, val);
1310195618Srpaulo}
1311195618Srpaulo
1312195618Srpaulostatic
1313195618SrpauloDECL_CMD_FUNC(set80211meshrtcmd, val, d)
1314195618Srpaulo{
1315195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_RTCMD, d, 0, NULL);
1316195618Srpaulo}
1317195618Srpaulo
1318195618Srpaulostatic
1319195618SrpauloDECL_CMD_FUNC(set80211hwmprootmode, val, d)
1320195618Srpaulo{
1321195618Srpaulo	int mode;
1322195618Srpaulo
1323195618Srpaulo	if (strcasecmp(val, "normal") == 0)
1324195618Srpaulo		mode = IEEE80211_HWMP_ROOTMODE_NORMAL;
1325195618Srpaulo	else if (strcasecmp(val, "proactive") == 0)
1326195618Srpaulo		mode = IEEE80211_HWMP_ROOTMODE_PROACTIVE;
1327195618Srpaulo	else if (strcasecmp(val, "rann") == 0)
1328195618Srpaulo		mode = IEEE80211_HWMP_ROOTMODE_RANN;
1329195618Srpaulo	else
1330195618Srpaulo		mode = IEEE80211_HWMP_ROOTMODE_DISABLED;
1331195618Srpaulo	set80211(s, IEEE80211_IOC_HWMP_ROOTMODE, mode, 0, NULL);
1332195618Srpaulo}
1333195618Srpaulo
1334195618Srpaulostatic
1335195618SrpauloDECL_CMD_FUNC(set80211hwmpmaxhops, val, d)
1336195618Srpaulo{
1337195618Srpaulo	set80211(s, IEEE80211_IOC_HWMP_MAXHOPS, atoi(val), 0, NULL);
1338195618Srpaulo}
1339195618Srpaulo
1340195618Srpaulostatic void
1341147795Ssamset80211pureg(const char *val, int d, int s, const struct afswtch *rafp)
1342147795Ssam{
1343147795Ssam	set80211(s, IEEE80211_IOC_PUREG, d, 0, NULL);
1344147795Ssam}
1345147795Ssam
1346153422Ssamstatic void
1347227336Sadrianset80211quiet(const char *val, int d, int s, const struct afswtch *rafp)
1348227336Sadrian{
1349227336Sadrian	set80211(s, IEEE80211_IOC_QUIET, d, 0, NULL);
1350227336Sadrian}
1351227336Sadrian
1352227336Sadrianstatic
1353227336SadrianDECL_CMD_FUNC(set80211quietperiod, val, d)
1354227336Sadrian{
1355227336Sadrian	set80211(s, IEEE80211_IOC_QUIET_PERIOD, atoi(val), 0, NULL);
1356227336Sadrian}
1357227336Sadrian
1358227336Sadrianstatic
1359227336SadrianDECL_CMD_FUNC(set80211quietcount, val, d)
1360227336Sadrian{
1361227336Sadrian	set80211(s, IEEE80211_IOC_QUIET_COUNT, atoi(val), 0, NULL);
1362227336Sadrian}
1363227336Sadrian
1364227336Sadrianstatic
1365227336SadrianDECL_CMD_FUNC(set80211quietduration, val, d)
1366227336Sadrian{
1367227336Sadrian	set80211(s, IEEE80211_IOC_QUIET_DUR, atoi(val), 0, NULL);
1368227336Sadrian}
1369227336Sadrian
1370227336Sadrianstatic
1371227336SadrianDECL_CMD_FUNC(set80211quietoffset, val, d)
1372227336Sadrian{
1373227336Sadrian	set80211(s, IEEE80211_IOC_QUIET_OFFSET, atoi(val), 0, NULL);
1374227336Sadrian}
1375227336Sadrian
1376227336Sadrianstatic void
1377170531Ssamset80211bgscan(const char *val, int d, int s, const struct afswtch *rafp)
1378153422Ssam{
1379170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN, d, 0, NULL);
1380153422Ssam}
1381153422Ssam
1382148416Ssamstatic
1383170531SsamDECL_CMD_FUNC(set80211bgscanidle, val, d)
1384170531Ssam{
1385170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN_IDLE, atoi(val), 0, NULL);
1386170531Ssam}
1387170531Ssam
1388170531Ssamstatic
1389170531SsamDECL_CMD_FUNC(set80211bgscanintvl, val, d)
1390170531Ssam{
1391170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN_INTERVAL, atoi(val), 0, NULL);
1392170531Ssam}
1393170531Ssam
1394170531Ssamstatic
1395170531SsamDECL_CMD_FUNC(set80211scanvalid, val, d)
1396170531Ssam{
1397170531Ssam	set80211(s, IEEE80211_IOC_SCANVALID, atoi(val), 0, NULL);
1398170531Ssam}
1399170531Ssam
1400178354Ssam/*
1401178354Ssam * Parse an optional trailing specification of which netbands
1402178354Ssam * to apply a parameter to.  This is basically the same syntax
1403178354Ssam * as used for channels but you can concatenate to specify
1404178354Ssam * multiple.  For example:
1405178354Ssam *	14:abg		apply to 11a, 11b, and 11g
1406178354Ssam *	6:ht		apply to 11na and 11ng
1407178354Ssam * We don't make a big effort to catch silly things; this is
1408178354Ssam * really a convenience mechanism.
1409178354Ssam */
1410178354Ssamstatic int
1411178354Ssamgetmodeflags(const char *val)
1412170531Ssam{
1413178354Ssam	const char *cp;
1414178354Ssam	int flags;
1415178354Ssam
1416178354Ssam	flags = 0;
1417178354Ssam
1418178354Ssam	cp = strchr(val, ':');
1419178354Ssam	if (cp != NULL) {
1420178354Ssam		for (cp++; isalpha((int) *cp); cp++) {
1421178354Ssam			/* accept mixed case */
1422178354Ssam			int c = *cp;
1423178354Ssam			if (isupper(c))
1424178354Ssam				c = tolower(c);
1425178354Ssam			switch (c) {
1426178354Ssam			case 'a':		/* 802.11a */
1427178354Ssam				flags |= IEEE80211_CHAN_A;
1428178354Ssam				break;
1429178354Ssam			case 'b':		/* 802.11b */
1430178354Ssam				flags |= IEEE80211_CHAN_B;
1431178354Ssam				break;
1432178354Ssam			case 'g':		/* 802.11g */
1433178354Ssam				flags |= IEEE80211_CHAN_G;
1434178354Ssam				break;
1435178354Ssam			case 'n':		/* 802.11n */
1436178354Ssam				flags |= IEEE80211_CHAN_HT;
1437178354Ssam				break;
1438178354Ssam			case 'd':		/* dt = Atheros Dynamic Turbo */
1439178354Ssam				flags |= IEEE80211_CHAN_TURBO;
1440178354Ssam				break;
1441178354Ssam			case 't':		/* ht, dt, st, t */
1442178354Ssam				/* dt and unadorned t specify Dynamic Turbo */
1443178354Ssam				if ((flags & (IEEE80211_CHAN_STURBO|IEEE80211_CHAN_HT)) == 0)
1444178354Ssam					flags |= IEEE80211_CHAN_TURBO;
1445178354Ssam				break;
1446178354Ssam			case 's':		/* st = Atheros Static Turbo */
1447178354Ssam				flags |= IEEE80211_CHAN_STURBO;
1448178354Ssam				break;
1449188784Ssam			case 'h':		/* 1/2-width channels */
1450188784Ssam				flags |= IEEE80211_CHAN_HALF;
1451188784Ssam				break;
1452188784Ssam			case 'q':		/* 1/4-width channels */
1453188784Ssam				flags |= IEEE80211_CHAN_QUARTER;
1454188784Ssam				break;
1455178354Ssam			default:
1456178354Ssam				errx(-1, "%s: Invalid mode attribute %c\n",
1457178354Ssam				    val, *cp);
1458178354Ssam			}
1459178354Ssam		}
1460178354Ssam	}
1461178354Ssam	return flags;
1462170531Ssam}
1463170531Ssam
1464178354Ssam#define	_APPLY(_flags, _base, _param, _v) do {				\
1465178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1466178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1467178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1468178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1469178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1470178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1471178354Ssam	    else							\
1472178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1473178354Ssam    }									\
1474178354Ssam    if (_flags & IEEE80211_CHAN_TURBO) {				\
1475178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1476178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;	\
1477178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;	\
1478178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1479178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;	\
1480178354Ssam	    else							\
1481178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;	\
1482178354Ssam    }									\
1483178354Ssam    if (_flags & IEEE80211_CHAN_STURBO)					\
1484178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = _v;		\
1485178354Ssam    if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1486178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = _v;		\
1487178354Ssam    if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1488178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = _v;		\
1489178354Ssam    if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1490178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = _v;		\
1491188784Ssam    if (_flags & IEEE80211_CHAN_HALF)					\
1492188784Ssam	    _base.params[IEEE80211_MODE_HALF]._param = _v;		\
1493188784Ssam    if (_flags & IEEE80211_CHAN_QUARTER)				\
1494188784Ssam	    _base.params[IEEE80211_MODE_QUARTER]._param = _v;		\
1495178354Ssam} while (0)
1496178354Ssam#define	_APPLY1(_flags, _base, _param, _v) do {				\
1497178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1498178354Ssam	    if (_flags & IEEE80211_CHAN_5GHZ)				\
1499178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1500178354Ssam	    else							\
1501178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1502178354Ssam    } else if ((_flags & IEEE80211_CHAN_108A) == IEEE80211_CHAN_108A)	\
1503178354Ssam	    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;		\
1504178354Ssam    else if ((_flags & IEEE80211_CHAN_108G) == IEEE80211_CHAN_108G)	\
1505178354Ssam	    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;		\
1506178354Ssam    else if ((_flags & IEEE80211_CHAN_ST) == IEEE80211_CHAN_ST)		\
1507178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = _v;		\
1508188784Ssam    else if (_flags & IEEE80211_CHAN_HALF)				\
1509188784Ssam	    _base.params[IEEE80211_MODE_HALF]._param = _v;		\
1510188784Ssam    else if (_flags & IEEE80211_CHAN_QUARTER)				\
1511188784Ssam	    _base.params[IEEE80211_MODE_QUARTER]._param = _v;		\
1512178354Ssam    else if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1513178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = _v;		\
1514178354Ssam    else if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1515178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = _v;		\
1516178354Ssam    else if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1517178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = _v;		\
1518178354Ssam} while (0)
1519178354Ssam#define	_APPLY_RATE(_flags, _base, _param, _v) do {			\
1520178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1521188784Ssam	(_v) = (_v / 2) | IEEE80211_RATE_MCS;				\
1522178354Ssam    }									\
1523188784Ssam    _APPLY(_flags, _base, _param, _v);					\
1524178354Ssam} while (0)
1525178354Ssam#define	_APPLY_RATE1(_flags, _base, _param, _v) do {			\
1526178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1527188784Ssam	(_v) = (_v / 2) | IEEE80211_RATE_MCS;				\
1528188784Ssam    }									\
1529188784Ssam    _APPLY1(_flags, _base, _param, _v);					\
1530178354Ssam} while (0)
1531178354Ssam
1532170531Ssamstatic
1533178354SsamDECL_CMD_FUNC(set80211roamrssi, val, d)
1534170531Ssam{
1535178354Ssam	double v = atof(val);
1536178354Ssam	int rssi, flags;
1537178354Ssam
1538178354Ssam	rssi = (int) (2*v);
1539178354Ssam	if (rssi != 2*v)
1540178354Ssam		errx(-1, "invalid rssi (must be .5 dBm units)");
1541178354Ssam	flags = getmodeflags(val);
1542178354Ssam	getroam(s);
1543178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1544178354Ssam		flags = getcurchan(s)->ic_flags;
1545178354Ssam		_APPLY1(flags, roamparams, rssi, rssi);
1546178354Ssam	} else
1547178354Ssam		_APPLY(flags, roamparams, rssi, rssi);
1548178354Ssam	callback_register(setroam_cb, &roamparams);
1549170531Ssam}
1550170531Ssam
1551188784Ssamstatic int
1552188784Ssamgetrate(const char *val, const char *tag)
1553188784Ssam{
1554188784Ssam	double v = atof(val);
1555188784Ssam	int rate;
1556188784Ssam
1557188784Ssam	rate = (int) (2*v);
1558188784Ssam	if (rate != 2*v)
1559188784Ssam		errx(-1, "invalid %s rate (must be .5 Mb/s units)", tag);
1560188784Ssam	return rate;		/* NB: returns 2x the specified value */
1561188784Ssam}
1562188784Ssam
1563170531Ssamstatic
1564178354SsamDECL_CMD_FUNC(set80211roamrate, val, d)
1565170531Ssam{
1566188784Ssam	int rate, flags;
1567178354Ssam
1568188784Ssam	rate = getrate(val, "roam");
1569178354Ssam	flags = getmodeflags(val);
1570178354Ssam	getroam(s);
1571178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1572178354Ssam		flags = getcurchan(s)->ic_flags;
1573188784Ssam		_APPLY_RATE1(flags, roamparams, rate, rate);
1574178354Ssam	} else
1575188784Ssam		_APPLY_RATE(flags, roamparams, rate, rate);
1576178354Ssam	callback_register(setroam_cb, &roamparams);
1577170531Ssam}
1578170531Ssam
1579170531Ssamstatic
1580178354SsamDECL_CMD_FUNC(set80211mcastrate, val, d)
1581170531Ssam{
1582188784Ssam	int rate, flags;
1583178354Ssam
1584188784Ssam	rate = getrate(val, "mcast");
1585178354Ssam	flags = getmodeflags(val);
1586178354Ssam	gettxparams(s);
1587178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1588178354Ssam		flags = getcurchan(s)->ic_flags;
1589188784Ssam		_APPLY_RATE1(flags, txparams, mcastrate, rate);
1590178354Ssam	} else
1591188784Ssam		_APPLY_RATE(flags, txparams, mcastrate, rate);
1592178354Ssam	callback_register(settxparams_cb, &txparams);
1593170531Ssam}
1594170531Ssam
1595170531Ssamstatic
1596178354SsamDECL_CMD_FUNC(set80211mgtrate, val, d)
1597170531Ssam{
1598188784Ssam	int rate, flags;
1599178354Ssam
1600188784Ssam	rate = getrate(val, "mgmt");
1601178354Ssam	flags = getmodeflags(val);
1602178354Ssam	gettxparams(s);
1603178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1604178354Ssam		flags = getcurchan(s)->ic_flags;
1605188784Ssam		_APPLY_RATE1(flags, txparams, mgmtrate, rate);
1606178354Ssam	} else
1607188784Ssam		_APPLY_RATE(flags, txparams, mgmtrate, rate);
1608178354Ssam	callback_register(settxparams_cb, &txparams);
1609170531Ssam}
1610170531Ssam
1611170531Ssamstatic
1612178354SsamDECL_CMD_FUNC(set80211ucastrate, val, d)
1613170531Ssam{
1614188784Ssam	int flags;
1615178354Ssam
1616178354Ssam	gettxparams(s);
1617178354Ssam	flags = getmodeflags(val);
1618178354Ssam	if (isanyarg(val)) {
1619178354Ssam		if (flags == 0) {	/* NB: no flags => current channel */
1620178354Ssam			flags = getcurchan(s)->ic_flags;
1621178354Ssam			_APPLY1(flags, txparams, ucastrate,
1622178354Ssam			    IEEE80211_FIXED_RATE_NONE);
1623178354Ssam		} else
1624178354Ssam			_APPLY(flags, txparams, ucastrate,
1625178354Ssam			    IEEE80211_FIXED_RATE_NONE);
1626178354Ssam	} else {
1627188784Ssam		int rate = getrate(val, "ucast");
1628178354Ssam		if (flags == 0) {	/* NB: no flags => current channel */
1629178354Ssam			flags = getcurchan(s)->ic_flags;
1630188784Ssam			_APPLY_RATE1(flags, txparams, ucastrate, rate);
1631178354Ssam		} else
1632188784Ssam			_APPLY_RATE(flags, txparams, ucastrate, rate);
1633178354Ssam	}
1634178354Ssam	callback_register(settxparams_cb, &txparams);
1635170531Ssam}
1636170531Ssam
1637170531Ssamstatic
1638178354SsamDECL_CMD_FUNC(set80211maxretry, val, d)
1639153354Ssam{
1640178354Ssam	int v = atoi(val), flags;
1641178354Ssam
1642178354Ssam	flags = getmodeflags(val);
1643178354Ssam	gettxparams(s);
1644178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1645178354Ssam		flags = getcurchan(s)->ic_flags;
1646178354Ssam		_APPLY1(flags, txparams, maxretry, v);
1647178354Ssam	} else
1648178354Ssam		_APPLY(flags, txparams, maxretry, v);
1649178354Ssam	callback_register(settxparams_cb, &txparams);
1650153354Ssam}
1651178354Ssam#undef _APPLY_RATE
1652178354Ssam#undef _APPLY
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
3000330448Seadler			/* some devices broadcast invalid WPS frames */
3001330448Seadler			if (tlv_len > len) {
3002330448Seadler				printf("bad frame length tlv_type=0x%02x "
3003330448Seadler				    "tlv_len=%d len=%d", tlv_type, tlv_len,
3004330448Seadler				    len);
3005330448Seadler				break;
3006330448Seadler			}
3007330448Seadler
3008181454Ssam			ie += 4, len -= 4;
3009181454Ssam
3010181454Ssam			switch (tlv_type) {
3011181454Ssam			case IEEE80211_WPS_VERSION:
3012181454Ssam				printf("v:%d.%d", *ie >> 4, *ie & 0xf);
3013181454Ssam				break;
3014181454Ssam			case IEEE80211_WPS_SETUP_STATE:
3015181454Ssam				/* Only 1 and 2 are valid */
3016181454Ssam				if (*ie == 0 || *ie >= 3)
3017181454Ssam					printf(" state:B");
3018181454Ssam				else
3019181454Ssam					printf(" st:%s", *ie == 1 ? "N" : "C");
3020181454Ssam				break;
3021181454Ssam			case IEEE80211_WPS_SELECTED_REG:
3022181454Ssam				printf(" sel:%s", *ie ? "T" : "F");
3023181454Ssam				break;
3024181454Ssam			case IEEE80211_WPS_DEV_PASS_ID:
3025181454Ssam				n = LE_READ_2(ie);
3026288305Sngie				if (n < nitems(dev_pass_id))
3027181454Ssam					printf(" dpi:%s", dev_pass_id[n]);
3028181454Ssam				break;
3029181454Ssam			case IEEE80211_WPS_UUID_E:
3030181454Ssam				printf(" uuid-e:");
3031181454Ssam				for (n = 0; n < (tlv_len - 1); n++)
3032181454Ssam					printf("%02x-", ie[n]);
3033181454Ssam				printf("%02x", ie[n]);
3034181454Ssam				break;
3035181454Ssam			}
3036181454Ssam			ie += tlv_len, len -= tlv_len;
3037181454Ssam		}
3038181454Ssam		printf(">");
3039181454Ssam	}
3040181454Ssam}
3041181454Ssam
3042186904Ssamstatic void
3043186904Ssamprinttdmaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
3044186904Ssam{
3045186904Ssam	printf("%s", tag);
3046186904Ssam	if (verbose && ielen >= sizeof(struct ieee80211_tdma_param)) {
3047186904Ssam		const struct ieee80211_tdma_param *tdma =
3048186904Ssam		   (const struct ieee80211_tdma_param *) ie;
3049186904Ssam
3050186904Ssam		/* XXX tstamp */
3051186904Ssam		printf("<v%u slot:%u slotcnt:%u slotlen:%u bintval:%u inuse:0x%x>",
3052186904Ssam		    tdma->tdma_version, tdma->tdma_slot, tdma->tdma_slotcnt,
3053186904Ssam		    LE_READ_2(&tdma->tdma_slotlen), tdma->tdma_bintval,
3054186904Ssam		    tdma->tdma_inuse[0]);
3055186904Ssam	}
3056186904Ssam}
3057186904Ssam
3058170531Ssam/*
3059138593Ssam * Copy the ssid string contents into buf, truncating to fit.  If the
3060138593Ssam * ssid is entirely printable then just copy intact.  Otherwise convert
3061138593Ssam * to hexadecimal.  If the result is truncated then replace the last
3062138593Ssam * three characters with "...".
3063138593Ssam */
3064146873Sjhbstatic int
3065138593Ssamcopy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
3066138593Ssam{
3067138593Ssam	const u_int8_t *p;
3068138593Ssam	size_t maxlen;
3069298556Spfg	u_int i;
3070138593Ssam
3071138593Ssam	if (essid_len > bufsize)
3072138593Ssam		maxlen = bufsize;
3073138593Ssam	else
3074138593Ssam		maxlen = essid_len;
3075138593Ssam	/* determine printable or not */
3076138593Ssam	for (i = 0, p = essid; i < maxlen; i++, p++) {
3077138593Ssam		if (*p < ' ' || *p > 0x7e)
3078138593Ssam			break;
3079138593Ssam	}
3080138593Ssam	if (i != maxlen) {		/* not printable, print as hex */
3081138593Ssam		if (bufsize < 3)
3082138593Ssam			return 0;
3083138593Ssam		strlcpy(buf, "0x", bufsize);
3084138593Ssam		bufsize -= 2;
3085138593Ssam		p = essid;
3086138593Ssam		for (i = 0; i < maxlen && bufsize >= 2; i++) {
3087147489Savatar			sprintf(&buf[2+2*i], "%02x", p[i]);
3088138593Ssam			bufsize -= 2;
3089138593Ssam		}
3090147489Savatar		if (i != essid_len)
3091147489Savatar			memcpy(&buf[2+2*i-3], "...", 3);
3092138593Ssam	} else {			/* printable, truncate as needed */
3093138593Ssam		memcpy(buf, essid, maxlen);
3094147489Savatar		if (maxlen != essid_len)
3095147489Savatar			memcpy(&buf[maxlen-3], "...", 3);
3096138593Ssam	}
3097138593Ssam	return maxlen;
3098138593Ssam}
3099138593Ssam
3100173275Ssamstatic void
3101173275Ssamprintssid(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
3102173275Ssam{
3103173275Ssam	char ssid[2*IEEE80211_NWID_LEN+1];
3104173275Ssam
3105173275Ssam	printf("%s<%.*s>", tag, copy_essid(ssid, maxlen, ie+2, ie[1]), ssid);
3106173275Ssam}
3107173275Ssam
3108173275Ssamstatic void
3109173275Ssamprintrates(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
3110173275Ssam{
3111173275Ssam	const char *sep;
3112173275Ssam	int i;
3113173275Ssam
3114173275Ssam	printf("%s", tag);
3115173275Ssam	sep = "<";
3116173275Ssam	for (i = 2; i < ielen; i++) {
3117173275Ssam		printf("%s%s%d", sep,
3118173275Ssam		    ie[i] & IEEE80211_RATE_BASIC ? "B" : "",
3119173275Ssam		    ie[i] & IEEE80211_RATE_VAL);
3120173275Ssam		sep = ",";
3121173275Ssam	}
3122173275Ssam	printf(">");
3123173275Ssam}
3124173275Ssam
3125173275Ssamstatic void
3126173275Ssamprintcountry(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
3127173275Ssam{
3128173275Ssam	const struct ieee80211_country_ie *cie =
3129173275Ssam	   (const struct ieee80211_country_ie *) ie;
3130173275Ssam	int i, nbands, schan, nchan;
3131173275Ssam
3132173275Ssam	printf("%s<%c%c%c", tag, cie->cc[0], cie->cc[1], cie->cc[2]);
3133173275Ssam	nbands = (cie->len - 3) / sizeof(cie->band[0]);
3134173275Ssam	for (i = 0; i < nbands; i++) {
3135173275Ssam		schan = cie->band[i].schan;
3136173275Ssam		nchan = cie->band[i].nchan;
3137173275Ssam		if (nchan != 1)
3138173275Ssam			printf(" %u-%u,%u", schan, schan + nchan-1,
3139173275Ssam			    cie->band[i].maxtxpwr);
3140173275Ssam		else
3141173275Ssam			printf(" %u,%u", schan, cie->band[i].maxtxpwr);
3142173275Ssam	}
3143173275Ssam	printf(">");
3144173275Ssam}
3145173275Ssam
3146178354Ssamstatic __inline int
3147138593Ssamiswpaoui(const u_int8_t *frm)
3148138593Ssam{
3149138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
3150138593Ssam}
3151138593Ssam
3152178354Ssamstatic __inline int
3153173275Ssamiswmeinfo(const u_int8_t *frm)
3154138593Ssam{
3155173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3156173275Ssam		frm[6] == WME_INFO_OUI_SUBTYPE;
3157138593Ssam}
3158138593Ssam
3159178354Ssamstatic __inline int
3160173275Ssamiswmeparam(const u_int8_t *frm)
3161173275Ssam{
3162173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3163173275Ssam		frm[6] == WME_PARAM_OUI_SUBTYPE;
3164173275Ssam}
3165173275Ssam
3166178354Ssamstatic __inline int
3167138593Ssamisatherosoui(const u_int8_t *frm)
3168138593Ssam{
3169138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
3170138593Ssam}
3171138593Ssam
3172181454Ssamstatic __inline int
3173186904Ssamistdmaoui(const uint8_t *frm)
3174186904Ssam{
3175186904Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((TDMA_OUI_TYPE<<24)|TDMA_OUI);
3176186904Ssam}
3177186904Ssam
3178186904Ssamstatic __inline int
3179181454Ssamiswpsoui(const uint8_t *frm)
3180181454Ssam{
3181181454Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPS_OUI_TYPE<<24)|WPA_OUI);
3182181454Ssam}
3183181454Ssam
3184173275Ssamstatic const char *
3185173275Ssamiename(int elemid)
3186173275Ssam{
3187173275Ssam	switch (elemid) {
3188173275Ssam	case IEEE80211_ELEMID_FHPARMS:	return " FHPARMS";
3189173275Ssam	case IEEE80211_ELEMID_CFPARMS:	return " CFPARMS";
3190173275Ssam	case IEEE80211_ELEMID_TIM:	return " TIM";
3191173275Ssam	case IEEE80211_ELEMID_IBSSPARMS:return " IBSSPARMS";
3192296823Sadrian	case IEEE80211_ELEMID_BSSLOAD:	return " BSSLOAD";
3193173275Ssam	case IEEE80211_ELEMID_CHALLENGE:return " CHALLENGE";
3194173275Ssam	case IEEE80211_ELEMID_PWRCNSTR:	return " PWRCNSTR";
3195173275Ssam	case IEEE80211_ELEMID_PWRCAP:	return " PWRCAP";
3196173275Ssam	case IEEE80211_ELEMID_TPCREQ:	return " TPCREQ";
3197173275Ssam	case IEEE80211_ELEMID_TPCREP:	return " TPCREP";
3198173275Ssam	case IEEE80211_ELEMID_SUPPCHAN:	return " SUPPCHAN";
3199193447Ssam	case IEEE80211_ELEMID_CSA:	return " CSA";
3200173275Ssam	case IEEE80211_ELEMID_MEASREQ:	return " MEASREQ";
3201173275Ssam	case IEEE80211_ELEMID_MEASREP:	return " MEASREP";
3202173275Ssam	case IEEE80211_ELEMID_QUIET:	return " QUIET";
3203173275Ssam	case IEEE80211_ELEMID_IBSSDFS:	return " IBSSDFS";
3204173275Ssam	case IEEE80211_ELEMID_TPC:	return " TPC";
3205173275Ssam	case IEEE80211_ELEMID_CCKM:	return " CCKM";
3206173275Ssam	}
3207173275Ssam	return " ???";
3208173275Ssam}
3209173275Ssam
3210138593Ssamstatic void
3211138593Ssamprinties(const u_int8_t *vp, int ielen, int maxcols)
3212138593Ssam{
3213138593Ssam	while (ielen > 0) {
3214138593Ssam		switch (vp[0]) {
3215173275Ssam		case IEEE80211_ELEMID_SSID:
3216173275Ssam			if (verbose)
3217173275Ssam				printssid(" SSID", vp, 2+vp[1], maxcols);
3218173275Ssam			break;
3219173275Ssam		case IEEE80211_ELEMID_RATES:
3220173275Ssam		case IEEE80211_ELEMID_XRATES:
3221173275Ssam			if (verbose)
3222173275Ssam				printrates(vp[0] == IEEE80211_ELEMID_RATES ?
3223173275Ssam				    " RATES" : " XRATES", vp, 2+vp[1], maxcols);
3224173275Ssam			break;
3225173275Ssam		case IEEE80211_ELEMID_DSPARMS:
3226173275Ssam			if (verbose)
3227173275Ssam				printf(" DSPARMS<%u>", vp[2]);
3228173275Ssam			break;
3229173275Ssam		case IEEE80211_ELEMID_COUNTRY:
3230173275Ssam			if (verbose)
3231173275Ssam				printcountry(" COUNTRY", vp, 2+vp[1], maxcols);
3232173275Ssam			break;
3233173275Ssam		case IEEE80211_ELEMID_ERP:
3234173275Ssam			if (verbose)
3235173275Ssam				printf(" ERP<0x%x>", vp[2]);
3236173275Ssam			break;
3237138593Ssam		case IEEE80211_ELEMID_VENDOR:
3238138593Ssam			if (iswpaoui(vp))
3239170531Ssam				printwpaie(" WPA", vp, 2+vp[1], maxcols);
3240173275Ssam			else if (iswmeinfo(vp))
3241173275Ssam				printwmeinfo(" WME", vp, 2+vp[1], maxcols);
3242173275Ssam			else if (iswmeparam(vp))
3243173275Ssam				printwmeparam(" WME", vp, 2+vp[1], maxcols);
3244139492Ssam			else if (isatherosoui(vp))
3245170531Ssam				printathie(" ATH", vp, 2+vp[1], maxcols);
3246181454Ssam			else if (iswpsoui(vp))
3247181454Ssam				printwpsie(" WPS", vp, 2+vp[1], maxcols);
3248186904Ssam			else if (istdmaoui(vp))
3249186904Ssam				printtdmaie(" TDMA", vp, 2+vp[1], maxcols);
3250173275Ssam			else if (verbose)
3251138593Ssam				printie(" VEN", vp, 2+vp[1], maxcols);
3252138593Ssam			break;
3253138593Ssam		case IEEE80211_ELEMID_RSN:
3254170531Ssam			printrsnie(" RSN", vp, 2+vp[1], maxcols);
3255138593Ssam			break;
3256173275Ssam		case IEEE80211_ELEMID_HTCAP:
3257173275Ssam			printhtcap(" HTCAP", vp, 2+vp[1], maxcols);
3258173275Ssam			break;
3259173275Ssam		case IEEE80211_ELEMID_HTINFO:
3260173275Ssam			if (verbose)
3261173275Ssam				printhtinfo(" HTINFO", vp, 2+vp[1], maxcols);
3262173275Ssam			break;
3263195618Srpaulo		case IEEE80211_ELEMID_MESHID:
3264195618Srpaulo			if (verbose)
3265195618Srpaulo				printssid(" MESHID", vp, 2+vp[1], maxcols);
3266195618Srpaulo			break;
3267195618Srpaulo		case IEEE80211_ELEMID_MESHCONF:
3268195618Srpaulo			printmeshconf(" MESHCONF", vp, 2+vp[1], maxcols);
3269195618Srpaulo			break;
3270297007Sadrian		case IEEE80211_ELEMID_VHT_CAP:
3271297007Sadrian			printvhtcap(" VHTCAP", vp, 2+vp[1], maxcols);
3272297007Sadrian			break;
3273297007Sadrian		case IEEE80211_ELEMID_VHT_OPMODE:
3274297007Sadrian			printvhtinfo(" VHTOPMODE", vp, 2+vp[1], maxcols);
3275297007Sadrian			break;
3276298252Sadrian		case IEEE80211_ELEMID_VHT_PWR_ENV:
3277298252Sadrian			printvhtpwrenv(" VHTPWRENV", vp, 2+vp[1], maxcols);
3278298252Sadrian			break;
3279297007Sadrian		case IEEE80211_ELEMID_BSSLOAD:
3280297007Sadrian			printbssload(" BSSLOAD", vp, 2+vp[1], maxcols);
3281297007Sadrian			break;
3282297010Sadrian		case IEEE80211_ELEMID_APCHANREP:
3283297010Sadrian			printapchanrep(" APCHANREP", vp, 2+vp[1], maxcols);
3284297010Sadrian			break;
3285138593Ssam		default:
3286173275Ssam			if (verbose)
3287173275Ssam				printie(iename(vp[0]), vp, 2+vp[1], maxcols);
3288138593Ssam			break;
3289138593Ssam		}
3290138593Ssam		ielen -= 2+vp[1];
3291138593Ssam		vp += 2+vp[1];
3292138593Ssam	}
3293138593Ssam}
3294138593Ssam
3295138593Ssamstatic void
3296178354Ssamprintmimo(const struct ieee80211_mimo_info *mi)
3297178354Ssam{
3298178354Ssam	/* NB: don't muddy display unless there's something to show */
3299220935Sadrian	if (mi->rssi[0] != 0 || mi->rssi[1] != 0 || mi->rssi[2] != 0) {
3300178354Ssam		/* XXX ignore EVM for now */
3301330459Seadler		printf(" (rssi %.1f:%.1f:%.1f nf %d:%d:%d)",
3302330459Seadler		    mi->rssi[0] / 2.0, mi->rssi[1] / 2.0, mi->rssi[2] / 2.0,
3303220935Sadrian		    mi->noise[0], mi->noise[1], mi->noise[2]);
3304178354Ssam	}
3305178354Ssam}
3306178354Ssam
3307178354Ssamstatic void
3308138593Ssamlist_scan(int s)
3309138593Ssam{
3310138593Ssam	uint8_t buf[24*1024];
3311153892Srwatson	char ssid[IEEE80211_NWID_LEN+1];
3312173275Ssam	const uint8_t *cp;
3313195618Srpaulo	int len, ssidmax, idlen;
3314138593Ssam
3315173275Ssam	if (get80211len(s, IEEE80211_IOC_SCAN_RESULTS, buf, sizeof(buf), &len) < 0)
3316138593Ssam		errx(1, "unable to get scan results");
3317138593Ssam	if (len < sizeof(struct ieee80211req_scan_result))
3318138593Ssam		return;
3319138593Ssam
3320170531Ssam	getchaninfo(s);
3321170531Ssam
3322310235Savos	ssidmax = verbose ? IEEE80211_NWID_LEN : 14;
3323290438Savos	printf("%-*.*s  %-17.17s  %4s %4s   %-7s  %3s %4s\n"
3324195618Srpaulo		, ssidmax, ssidmax, "SSID/MESH ID"
3325138593Ssam		, "BSSID"
3326138593Ssam		, "CHAN"
3327138593Ssam		, "RATE"
3328170531Ssam		, " S:N"
3329138593Ssam		, "INT"
3330138593Ssam		, "CAPS"
3331138593Ssam	);
3332138593Ssam	cp = buf;
3333138593Ssam	do {
3334170531Ssam		const struct ieee80211req_scan_result *sr;
3335195618Srpaulo		const uint8_t *vp, *idp;
3336138593Ssam
3337170531Ssam		sr = (const struct ieee80211req_scan_result *) cp;
3338173275Ssam		vp = cp + sr->isr_ie_off;
3339195618Srpaulo		if (sr->isr_meshid_len) {
3340195618Srpaulo			idp = vp + sr->isr_ssid_len;
3341195618Srpaulo			idlen = sr->isr_meshid_len;
3342195618Srpaulo		} else {
3343195618Srpaulo			idp = vp;
3344195618Srpaulo			idlen = sr->isr_ssid_len;
3345195618Srpaulo		}
3346290438Savos		printf("%-*.*s  %s  %3d  %3dM %4d:%-4d %4d %-4.4s"
3347154522Ssam			, ssidmax
3348195618Srpaulo			  , copy_essid(ssid, ssidmax, idp, idlen)
3349154522Ssam			  , ssid
3350138593Ssam			, ether_ntoa((const struct ether_addr *) sr->isr_bssid)
3351165570Ssam			, ieee80211_mhz2ieee(sr->isr_freq, sr->isr_flags)
3352138593Ssam			, getmaxrate(sr->isr_rates, sr->isr_nrates)
3353170531Ssam			, (sr->isr_rssi/2)+sr->isr_noise, sr->isr_noise
3354138593Ssam			, sr->isr_intval
3355138593Ssam			, getcaps(sr->isr_capinfo)
3356138593Ssam		);
3357195618Srpaulo		printies(vp + sr->isr_ssid_len + sr->isr_meshid_len,
3358195618Srpaulo		    sr->isr_ie_len, 24);
3359138593Ssam		printf("\n");
3360138593Ssam		cp += sr->isr_len, len -= sr->isr_len;
3361138593Ssam	} while (len >= sizeof(struct ieee80211req_scan_result));
3362138593Ssam}
3363138593Ssam
3364138593Ssamstatic void
3365138593Ssamscan_and_wait(int s)
3366138593Ssam{
3367178354Ssam	struct ieee80211_scan_req sr;
3368138593Ssam	struct ieee80211req ireq;
3369138593Ssam	int sroute;
3370138593Ssam
3371138593Ssam	sroute = socket(PF_ROUTE, SOCK_RAW, 0);
3372138593Ssam	if (sroute < 0) {
3373138593Ssam		perror("socket(PF_ROUTE,SOCK_RAW)");
3374138593Ssam		return;
3375138593Ssam	}
3376138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3377299873Struckman	(void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
3378138593Ssam	ireq.i_type = IEEE80211_IOC_SCAN_REQ;
3379178354Ssam
3380178354Ssam	memset(&sr, 0, sizeof(sr));
3381178354Ssam	sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE
3382218090Sbschmidt		    | IEEE80211_IOC_SCAN_BGSCAN
3383178354Ssam		    | IEEE80211_IOC_SCAN_NOPICK
3384178354Ssam		    | IEEE80211_IOC_SCAN_ONCE;
3385178354Ssam	sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
3386178354Ssam	sr.sr_nssid = 0;
3387178354Ssam
3388178354Ssam	ireq.i_data = &sr;
3389178354Ssam	ireq.i_len = sizeof(sr);
3390218093Sbschmidt	/*
3391218093Sbschmidt	 * NB: only root can trigger a scan so ignore errors. Also ignore
3392218093Sbschmidt	 * possible errors from net80211, even if no new scan could be
3393218093Sbschmidt	 * started there might still be a valid scan cache.
3394218093Sbschmidt	 */
3395218093Sbschmidt	if (ioctl(s, SIOCS80211, &ireq) == 0) {
3396138593Ssam		char buf[2048];
3397138593Ssam		struct if_announcemsghdr *ifan;
3398138593Ssam		struct rt_msghdr *rtm;
3399138593Ssam
3400138593Ssam		do {
3401138593Ssam			if (read(sroute, buf, sizeof(buf)) < 0) {
3402138593Ssam				perror("read(PF_ROUTE)");
3403138593Ssam				break;
3404138593Ssam			}
3405138593Ssam			rtm = (struct rt_msghdr *) buf;
3406138593Ssam			if (rtm->rtm_version != RTM_VERSION)
3407138593Ssam				break;
3408138593Ssam			ifan = (struct if_announcemsghdr *) rtm;
3409138593Ssam		} while (rtm->rtm_type != RTM_IEEE80211 ||
3410138593Ssam		    ifan->ifan_what != RTM_IEEE80211_SCAN);
3411138593Ssam	}
3412138593Ssam	close(sroute);
3413138593Ssam}
3414138593Ssam
3415138593Ssamstatic
3416138593SsamDECL_CMD_FUNC(set80211scan, val, d)
3417138593Ssam{
3418138593Ssam	scan_and_wait(s);
3419138593Ssam	list_scan(s);
3420138593Ssam}
3421138593Ssam
3422161147Ssamstatic enum ieee80211_opmode get80211opmode(int s);
3423161147Ssam
3424173275Ssamstatic int
3425173275Ssamgettxseq(const struct ieee80211req_sta_info *si)
3426173275Ssam{
3427173275Ssam	int i, txseq;
3428173275Ssam
3429173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3430173275Ssam		return si->isi_txseqs[0];
3431173275Ssam	/* XXX not right but usually what folks want */
3432173275Ssam	txseq = 0;
3433173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3434173275Ssam		if (si->isi_txseqs[i] > txseq)
3435173275Ssam			txseq = si->isi_txseqs[i];
3436173275Ssam	return txseq;
3437173275Ssam}
3438173275Ssam
3439173275Ssamstatic int
3440173275Ssamgetrxseq(const struct ieee80211req_sta_info *si)
3441173275Ssam{
3442173275Ssam	int i, rxseq;
3443173275Ssam
3444173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3445173275Ssam		return si->isi_rxseqs[0];
3446173275Ssam	/* XXX not right but usually what folks want */
3447173275Ssam	rxseq = 0;
3448173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3449173275Ssam		if (si->isi_rxseqs[i] > rxseq)
3450173275Ssam			rxseq = si->isi_rxseqs[i];
3451173275Ssam	return rxseq;
3452173275Ssam}
3453173275Ssam
3454138593Ssamstatic void
3455138593Ssamlist_stations(int s)
3456138593Ssam{
3457161147Ssam	union {
3458161147Ssam		struct ieee80211req_sta_req req;
3459161147Ssam		uint8_t buf[24*1024];
3460161147Ssam	} u;
3461161147Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
3462170531Ssam	const uint8_t *cp;
3463138593Ssam	int len;
3464138593Ssam
3465161147Ssam	/* broadcast address =>'s get all stations */
3466161147Ssam	(void) memset(u.req.is_u.macaddr, 0xff, IEEE80211_ADDR_LEN);
3467161147Ssam	if (opmode == IEEE80211_M_STA) {
3468161147Ssam		/*
3469161147Ssam		 * Get information about the associated AP.
3470161147Ssam		 */
3471173275Ssam		(void) get80211(s, IEEE80211_IOC_BSSID,
3472173275Ssam		    u.req.is_u.macaddr, IEEE80211_ADDR_LEN);
3473161147Ssam	}
3474173275Ssam	if (get80211len(s, IEEE80211_IOC_STA_INFO, &u, sizeof(u), &len) < 0)
3475138593Ssam		errx(1, "unable to get station information");
3476138593Ssam	if (len < sizeof(struct ieee80211req_sta_info))
3477138593Ssam		return;
3478138593Ssam
3479170531Ssam	getchaninfo(s);
3480170531Ssam
3481195618Srpaulo	if (opmode == IEEE80211_M_MBSS)
3482195618Srpaulo		printf("%-17.17s %4s %5s %5s %7s %4s %4s %4s %6s %6s\n"
3483195618Srpaulo			, "ADDR"
3484195618Srpaulo			, "CHAN"
3485195618Srpaulo			, "LOCAL"
3486195618Srpaulo			, "PEER"
3487195618Srpaulo			, "STATE"
3488195618Srpaulo			, "RATE"
3489195618Srpaulo			, "RSSI"
3490195618Srpaulo			, "IDLE"
3491195618Srpaulo			, "TXSEQ"
3492195618Srpaulo			, "RXSEQ"
3493195618Srpaulo		);
3494195618Srpaulo	else
3495195618Srpaulo		printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %-7s\n"
3496195618Srpaulo			, "ADDR"
3497195618Srpaulo			, "AID"
3498195618Srpaulo			, "CHAN"
3499195618Srpaulo			, "RATE"
3500195618Srpaulo			, "RSSI"
3501195618Srpaulo			, "IDLE"
3502195618Srpaulo			, "TXSEQ"
3503195618Srpaulo			, "RXSEQ"
3504195618Srpaulo			, "CAPS"
3505195618Srpaulo			, "FLAG"
3506195618Srpaulo		);
3507170531Ssam	cp = (const uint8_t *) u.req.info;
3508138593Ssam	do {
3509170531Ssam		const struct ieee80211req_sta_info *si;
3510138593Ssam
3511170531Ssam		si = (const struct ieee80211req_sta_info *) cp;
3512161147Ssam		if (si->isi_len < sizeof(*si))
3513161147Ssam			break;
3514195618Srpaulo		if (opmode == IEEE80211_M_MBSS)
3515195618Srpaulo			printf("%s %4d %5x %5x %7.7s %3dM %4.1f %4d %6d %6d"
3516195618Srpaulo				, ether_ntoa((const struct ether_addr*)
3517195618Srpaulo				    si->isi_macaddr)
3518195618Srpaulo				, ieee80211_mhz2ieee(si->isi_freq,
3519195618Srpaulo				    si->isi_flags)
3520195618Srpaulo				, si->isi_localid
3521195618Srpaulo				, si->isi_peerid
3522195618Srpaulo				, mesh_linkstate_string(si->isi_peerstate)
3523195618Srpaulo				, si->isi_txmbps/2
3524195618Srpaulo				, si->isi_rssi/2.
3525195618Srpaulo				, si->isi_inact
3526195618Srpaulo				, gettxseq(si)
3527195618Srpaulo				, getrxseq(si)
3528195618Srpaulo			);
3529195618Srpaulo		else
3530195618Srpaulo			printf("%s %4u %4d %3dM %4.1f %4d %6d %6d %-4.4s %-7.7s"
3531195618Srpaulo				, ether_ntoa((const struct ether_addr*)
3532195618Srpaulo				    si->isi_macaddr)
3533195618Srpaulo				, IEEE80211_AID(si->isi_associd)
3534195618Srpaulo				, ieee80211_mhz2ieee(si->isi_freq,
3535195618Srpaulo				    si->isi_flags)
3536195618Srpaulo				, si->isi_txmbps/2
3537195618Srpaulo				, si->isi_rssi/2.
3538195618Srpaulo				, si->isi_inact
3539195618Srpaulo				, gettxseq(si)
3540195618Srpaulo				, getrxseq(si)
3541195618Srpaulo				, getcaps(si->isi_capinfo)
3542195618Srpaulo				, getflags(si->isi_state)
3543195618Srpaulo			);
3544170531Ssam		printies(cp + si->isi_ie_off, si->isi_ie_len, 24);
3545178354Ssam		printmimo(&si->isi_mimo);
3546138593Ssam		printf("\n");
3547138593Ssam		cp += si->isi_len, len -= si->isi_len;
3548138593Ssam	} while (len >= sizeof(struct ieee80211req_sta_info));
3549138593Ssam}
3550138593Ssam
3551170531Ssamstatic const char *
3552195618Srpaulomesh_linkstate_string(uint8_t state)
3553195618Srpaulo{
3554195618Srpaulo	static const char *state_names[] = {
3555195618Srpaulo	    [0] = "IDLE",
3556195618Srpaulo	    [1] = "OPEN-TX",
3557195618Srpaulo	    [2] = "OPEN-RX",
3558195618Srpaulo	    [3] = "CONF-RX",
3559195618Srpaulo	    [4] = "ESTAB",
3560195618Srpaulo	    [5] = "HOLDING",
3561195618Srpaulo	};
3562195618Srpaulo
3563288305Sngie	if (state >= nitems(state_names)) {
3564195618Srpaulo		static char buf[10];
3565195618Srpaulo		snprintf(buf, sizeof(buf), "#%u", state);
3566195618Srpaulo		return buf;
3567195618Srpaulo	} else
3568195618Srpaulo		return state_names[state];
3569195618Srpaulo}
3570195618Srpaulo
3571195618Srpaulostatic const char *
3572170531Ssamget_chaninfo(const struct ieee80211_channel *c, int precise,
3573170531Ssam	char buf[], size_t bsize)
3574138593Ssam{
3575138593Ssam	buf[0] = '\0';
3576138593Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
3577170531Ssam		strlcat(buf, " FHSS", bsize);
3578187843Ssam	if (IEEE80211_IS_CHAN_A(c))
3579187843Ssam		strlcat(buf, " 11a", bsize);
3580187843Ssam	else if (IEEE80211_IS_CHAN_ANYG(c))
3581187843Ssam		strlcat(buf, " 11g", bsize);
3582187843Ssam	else if (IEEE80211_IS_CHAN_B(c))
3583170531Ssam		strlcat(buf, " 11b", bsize);
3584187843Ssam	if (IEEE80211_IS_CHAN_HALF(c))
3585202161Sgavin		strlcat(buf, "/10MHz", bsize);
3586187843Ssam	if (IEEE80211_IS_CHAN_QUARTER(c))
3587202161Sgavin		strlcat(buf, "/5MHz", bsize);
3588170531Ssam	if (IEEE80211_IS_CHAN_TURBO(c))
3589170531Ssam		strlcat(buf, " Turbo", bsize);
3590170531Ssam	if (precise) {
3591170531Ssam		if (IEEE80211_IS_CHAN_HT20(c))
3592170531Ssam			strlcat(buf, " ht/20", bsize);
3593170531Ssam		else if (IEEE80211_IS_CHAN_HT40D(c))
3594170531Ssam			strlcat(buf, " ht/40-", bsize);
3595170531Ssam		else if (IEEE80211_IS_CHAN_HT40U(c))
3596170531Ssam			strlcat(buf, " ht/40+", bsize);
3597170531Ssam	} else {
3598170531Ssam		if (IEEE80211_IS_CHAN_HT(c))
3599170531Ssam			strlcat(buf, " ht", bsize);
3600170531Ssam	}
3601170531Ssam	return buf;
3602170531Ssam}
3603170531Ssam
3604170531Ssamstatic void
3605173275Ssamprint_chaninfo(const struct ieee80211_channel *c, int verb)
3606170531Ssam{
3607170531Ssam	char buf[14];
3608170531Ssam
3609224219Sadrian	if (verb)
3610224219Sadrian		printf("Channel %3u : %u%c%c%c%c%c MHz%-14.14s",
3611224219Sadrian		    ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
3612224219Sadrian		    IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
3613224219Sadrian		    IEEE80211_IS_CHAN_DFS(c) ? 'D' : ' ',
3614224219Sadrian		    IEEE80211_IS_CHAN_RADAR(c) ? 'R' : ' ',
3615224219Sadrian		    IEEE80211_IS_CHAN_CWINT(c) ? 'I' : ' ',
3616224219Sadrian		    IEEE80211_IS_CHAN_CACDONE(c) ? 'C' : ' ',
3617224219Sadrian		    get_chaninfo(c, verb, buf, sizeof(buf)));
3618224219Sadrian	else
3619202161Sgavin	printf("Channel %3u : %u%c MHz%-14.14s",
3620224219Sadrian	    ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
3621224219Sadrian	    IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
3622224219Sadrian	    get_chaninfo(c, verb, buf, sizeof(buf)));
3623224219Sadrian
3624138593Ssam}
3625138593Ssam
3626187844Ssamstatic int
3627187844Ssamchanpref(const struct ieee80211_channel *c)
3628187844Ssam{
3629187844Ssam	if (IEEE80211_IS_CHAN_HT40(c))
3630187844Ssam		return 40;
3631187844Ssam	if (IEEE80211_IS_CHAN_HT20(c))
3632187844Ssam		return 30;
3633187844Ssam	if (IEEE80211_IS_CHAN_HALF(c))
3634187844Ssam		return 10;
3635187844Ssam	if (IEEE80211_IS_CHAN_QUARTER(c))
3636187844Ssam		return 5;
3637187844Ssam	if (IEEE80211_IS_CHAN_TURBO(c))
3638187844Ssam		return 25;
3639187844Ssam	if (IEEE80211_IS_CHAN_A(c))
3640187844Ssam		return 20;
3641187844Ssam	if (IEEE80211_IS_CHAN_G(c))
3642187844Ssam		return 20;
3643187844Ssam	if (IEEE80211_IS_CHAN_B(c))
3644187844Ssam		return 15;
3645187844Ssam	if (IEEE80211_IS_CHAN_PUREG(c))
3646187844Ssam		return 15;
3647187844Ssam	return 0;
3648187844Ssam}
3649187844Ssam
3650138593Ssamstatic void
3651173275Ssamprint_channels(int s, const struct ieee80211req_chaninfo *chans,
3652173275Ssam	int allchans, int verb)
3653138593Ssam{
3654187801Ssam	struct ieee80211req_chaninfo *achans;
3655170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3656138593Ssam	const struct ieee80211_channel *c;
3657170531Ssam	int i, half;
3658138593Ssam
3659187801Ssam	achans = malloc(IEEE80211_CHANINFO_SPACE(chans));
3660187801Ssam	if (achans == NULL)
3661187801Ssam		errx(1, "no space for active channel list");
3662187801Ssam	achans->ic_nchans = 0;
3663170531Ssam	memset(reported, 0, sizeof(reported));
3664138593Ssam	if (!allchans) {
3665138593Ssam		struct ieee80211req_chanlist active;
3666138593Ssam
3667173275Ssam		if (get80211(s, IEEE80211_IOC_CHANLIST, &active, sizeof(active)) < 0)
3668138593Ssam			errx(1, "unable to get active channel list");
3669173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3670173275Ssam			c = &chans->ic_chans[i];
3671170531Ssam			if (!isset(active.ic_channels, c->ic_ieee))
3672170531Ssam				continue;
3673170531Ssam			/*
3674170531Ssam			 * Suppress compatible duplicates unless
3675170531Ssam			 * verbose.  The kernel gives us it's
3676170531Ssam			 * complete channel list which has separate
3677170531Ssam			 * entries for 11g/11b and 11a/turbo.
3678170531Ssam			 */
3679173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3680170531Ssam				/* XXX we assume duplicates are adjacent */
3681187801Ssam				achans->ic_chans[achans->ic_nchans-1] = *c;
3682170531Ssam			} else {
3683187801Ssam				achans->ic_chans[achans->ic_nchans++] = *c;
3684170531Ssam				setbit(reported, c->ic_ieee);
3685170531Ssam			}
3686138593Ssam		}
3687170531Ssam	} else {
3688173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3689173275Ssam			c = &chans->ic_chans[i];
3690170531Ssam			/* suppress duplicates as above */
3691173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3692170531Ssam				/* XXX we assume duplicates are adjacent */
3693187844Ssam				struct ieee80211_channel *a =
3694187844Ssam				    &achans->ic_chans[achans->ic_nchans-1];
3695187844Ssam				if (chanpref(c) > chanpref(a))
3696187844Ssam					*a = *c;
3697170531Ssam			} else {
3698187801Ssam				achans->ic_chans[achans->ic_nchans++] = *c;
3699170531Ssam				setbit(reported, c->ic_ieee);
3700170531Ssam			}
3701170531Ssam		}
3702170531Ssam	}
3703187801Ssam	half = achans->ic_nchans / 2;
3704187801Ssam	if (achans->ic_nchans % 2)
3705138593Ssam		half++;
3706170531Ssam
3707187801Ssam	for (i = 0; i < achans->ic_nchans / 2; i++) {
3708187801Ssam		print_chaninfo(&achans->ic_chans[i], verb);
3709187801Ssam		print_chaninfo(&achans->ic_chans[half+i], verb);
3710138593Ssam		printf("\n");
3711138593Ssam	}
3712187801Ssam	if (achans->ic_nchans % 2) {
3713187801Ssam		print_chaninfo(&achans->ic_chans[i], verb);
3714138593Ssam		printf("\n");
3715138593Ssam	}
3716187801Ssam	free(achans);
3717138593Ssam}
3718138593Ssam
3719138593Ssamstatic void
3720173275Ssamlist_channels(int s, int allchans)
3721173275Ssam{
3722173275Ssam	getchaninfo(s);
3723187801Ssam	print_channels(s, chaninfo, allchans, verbose);
3724173275Ssam}
3725173275Ssam
3726173275Ssamstatic void
3727170531Ssamprint_txpow(const struct ieee80211_channel *c)
3728170531Ssam{
3729202161Sgavin	printf("Channel %3u : %u MHz %3.1f reg %2d  ",
3730170531Ssam	    c->ic_ieee, c->ic_freq,
3731170531Ssam	    c->ic_maxpower/2., c->ic_maxregpower);
3732170531Ssam}
3733170531Ssam
3734170531Ssamstatic void
3735170531Ssamprint_txpow_verbose(const struct ieee80211_channel *c)
3736170531Ssam{
3737173275Ssam	print_chaninfo(c, 1);
3738170531Ssam	printf("min %4.1f dBm  max %3.1f dBm  reg %2d dBm",
3739170531Ssam	    c->ic_minpower/2., c->ic_maxpower/2., c->ic_maxregpower);
3740170531Ssam	/* indicate where regulatory cap limits power use */
3741170531Ssam	if (c->ic_maxpower > 2*c->ic_maxregpower)
3742170531Ssam		printf(" <");
3743170531Ssam}
3744170531Ssam
3745170531Ssamstatic void
3746170531Ssamlist_txpow(int s)
3747170531Ssam{
3748187801Ssam	struct ieee80211req_chaninfo *achans;
3749170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3750170531Ssam	struct ieee80211_channel *c, *prev;
3751170531Ssam	int i, half;
3752170531Ssam
3753170531Ssam	getchaninfo(s);
3754187801Ssam	achans = malloc(IEEE80211_CHANINFO_SPACE(chaninfo));
3755187801Ssam	if (achans == NULL)
3756187801Ssam		errx(1, "no space for active channel list");
3757187801Ssam	achans->ic_nchans = 0;
3758170531Ssam	memset(reported, 0, sizeof(reported));
3759187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
3760187801Ssam		c = &chaninfo->ic_chans[i];
3761170531Ssam		/* suppress duplicates as above */
3762170531Ssam		if (isset(reported, c->ic_ieee) && !verbose) {
3763170531Ssam			/* XXX we assume duplicates are adjacent */
3764299921Struckman			assert(achans->ic_nchans > 0);
3765187801Ssam			prev = &achans->ic_chans[achans->ic_nchans-1];
3766170531Ssam			/* display highest power on channel */
3767170531Ssam			if (c->ic_maxpower > prev->ic_maxpower)
3768170531Ssam				*prev = *c;
3769170531Ssam		} else {
3770187801Ssam			achans->ic_chans[achans->ic_nchans++] = *c;
3771170531Ssam			setbit(reported, c->ic_ieee);
3772170531Ssam		}
3773170531Ssam	}
3774170531Ssam	if (!verbose) {
3775187801Ssam		half = achans->ic_nchans / 2;
3776187801Ssam		if (achans->ic_nchans % 2)
3777170531Ssam			half++;
3778170531Ssam
3779187801Ssam		for (i = 0; i < achans->ic_nchans / 2; i++) {
3780187801Ssam			print_txpow(&achans->ic_chans[i]);
3781187801Ssam			print_txpow(&achans->ic_chans[half+i]);
3782170531Ssam			printf("\n");
3783170531Ssam		}
3784187801Ssam		if (achans->ic_nchans % 2) {
3785187801Ssam			print_txpow(&achans->ic_chans[i]);
3786170531Ssam			printf("\n");
3787170531Ssam		}
3788170531Ssam	} else {
3789187801Ssam		for (i = 0; i < achans->ic_nchans; i++) {
3790187801Ssam			print_txpow_verbose(&achans->ic_chans[i]);
3791170531Ssam			printf("\n");
3792170531Ssam		}
3793170531Ssam	}
3794187801Ssam	free(achans);
3795170531Ssam}
3796170531Ssam
3797170531Ssamstatic void
3798138593Ssamlist_keys(int s)
3799138593Ssam{
3800138593Ssam}
3801138593Ssam
3802138593Ssam#define	IEEE80211_C_BITS \
3803195618Srpaulo	"\20\1STA\002803ENCAP\7FF\10TURBOP\11IBSS\12PMGT" \
3804178354Ssam	"\13HOSTAP\14AHDEMO\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE" \
3805195618Srpaulo	"\21MONITOR\22DFS\23MBSS\30WPA1\31WPA2\32BURST\33WME\34WDS\36BGSCAN" \
3806186904Ssam	"\37TXFRAG\40TDMA"
3807138593Ssam
3808138593Ssamstatic void
3809138593Ssamlist_capabilities(int s)
3810138593Ssam{
3811187801Ssam	struct ieee80211_devcaps_req *dc;
3812138593Ssam
3813187845Ssam	if (verbose)
3814187845Ssam		dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
3815187845Ssam	else
3816187845Ssam		dc = malloc(IEEE80211_DEVCAPS_SIZE(1));
3817187801Ssam	if (dc == NULL)
3818187801Ssam		errx(1, "no space for device capabilities");
3819187845Ssam	dc->dc_chaninfo.ic_nchans = verbose ? MAXCHAN : 1;
3820187801Ssam	getdevcaps(s, dc);
3821187801Ssam	printb("drivercaps", dc->dc_drivercaps, IEEE80211_C_BITS);
3822187801Ssam	if (dc->dc_cryptocaps != 0 || verbose) {
3823178354Ssam		putchar('\n');
3824187801Ssam		printb("cryptocaps", dc->dc_cryptocaps, IEEE80211_CRYPTO_BITS);
3825178354Ssam	}
3826187801Ssam	if (dc->dc_htcaps != 0 || verbose) {
3827178354Ssam		putchar('\n');
3828187801Ssam		printb("htcaps", dc->dc_htcaps, IEEE80211_HTCAP_BITS);
3829178354Ssam	}
3830138593Ssam	putchar('\n');
3831187845Ssam	if (verbose) {
3832187845Ssam		chaninfo = &dc->dc_chaninfo;	/* XXX */
3833187845Ssam		print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, verbose);
3834187845Ssam	}
3835187801Ssam	free(dc);
3836138593Ssam}
3837138593Ssam
3838173275Ssamstatic int
3839173275Ssamget80211wme(int s, int param, int ac, int *val)
3840173275Ssam{
3841173275Ssam	struct ieee80211req ireq;
3842173275Ssam
3843173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3844299873Struckman	(void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
3845173275Ssam	ireq.i_type = param;
3846173275Ssam	ireq.i_len = ac;
3847173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3848173275Ssam		warn("cannot get WME parameter %d, ac %d%s",
3849173275Ssam		    param, ac & IEEE80211_WMEPARAM_VAL,
3850173275Ssam		    ac & IEEE80211_WMEPARAM_BSS ? " (BSS)" : "");
3851173275Ssam		return -1;
3852173275Ssam	}
3853173275Ssam	*val = ireq.i_val;
3854173275Ssam	return 0;
3855173275Ssam}
3856173275Ssam
3857138593Ssamstatic void
3858181199Ssamlist_wme_aci(int s, const char *tag, int ac)
3859138593Ssam{
3860181199Ssam	int val;
3861138593Ssam
3862181199Ssam	printf("\t%s", tag);
3863138593Ssam
3864181199Ssam	/* show WME BSS parameters */
3865181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMIN, ac, &val) != -1)
3866181199Ssam		printf(" cwmin %2u", val);
3867181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMAX, ac, &val) != -1)
3868181199Ssam		printf(" cwmax %2u", val);
3869181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_AIFS, ac, &val) != -1)
3870181199Ssam		printf(" aifs %2u", val);
3871181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_TXOPLIMIT, ac, &val) != -1)
3872181199Ssam		printf(" txopLimit %3u", val);
3873181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_ACM, ac, &val) != -1) {
3874181199Ssam		if (val)
3875181199Ssam			printf(" acm");
3876181199Ssam		else if (verbose)
3877181199Ssam			printf(" -acm");
3878181199Ssam	}
3879181199Ssam	/* !BSS only */
3880181199Ssam	if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3881181199Ssam		if (get80211wme(s, IEEE80211_IOC_WME_ACKPOLICY, ac, &val) != -1) {
3882181199Ssam			if (!val)
3883181199Ssam				printf(" -ack");
3884138593Ssam			else if (verbose)
3885181199Ssam				printf(" ack");
3886138593Ssam		}
3887181199Ssam	}
3888181199Ssam	printf("\n");
3889181199Ssam}
3890181199Ssam
3891181199Ssamstatic void
3892181199Ssamlist_wme(int s)
3893181199Ssam{
3894181199Ssam	static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
3895181199Ssam	int ac;
3896181199Ssam
3897181199Ssam	if (verbose) {
3898181199Ssam		/* display both BSS and local settings */
3899181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
3900181199Ssam	again:
3901181199Ssam			if (ac & IEEE80211_WMEPARAM_BSS)
3902181199Ssam				list_wme_aci(s, "     ", ac);
3903181199Ssam			else
3904181199Ssam				list_wme_aci(s, acnames[ac], ac);
3905181199Ssam			if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3906181199Ssam				ac |= IEEE80211_WMEPARAM_BSS;
3907181199Ssam				goto again;
3908181199Ssam			} else
3909181199Ssam				ac &= ~IEEE80211_WMEPARAM_BSS;
3910138593Ssam		}
3911181199Ssam	} else {
3912181199Ssam		/* display only channel settings */
3913181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++)
3914181199Ssam			list_wme_aci(s, acnames[ac], ac);
3915138593Ssam	}
3916138593Ssam}
3917138593Ssam
3918149029Ssamstatic void
3919178354Ssamlist_roam(int s)
3920178354Ssam{
3921178354Ssam	const struct ieee80211_roamparam *rp;
3922178354Ssam	int mode;
3923178354Ssam
3924178354Ssam	getroam(s);
3925188784Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
3926178354Ssam		rp = &roamparams.params[mode];
3927178354Ssam		if (rp->rssi == 0 && rp->rate == 0)
3928178354Ssam			continue;
3929188784Ssam		if (mode == IEEE80211_MODE_11NA || mode == IEEE80211_MODE_11NG) {
3930188784Ssam			if (rp->rssi & 1)
3931188784Ssam				LINE_CHECK("roam:%-7.7s rssi %2u.5dBm  MCS %2u    ",
3932188784Ssam				    modename[mode], rp->rssi/2,
3933188784Ssam				    rp->rate &~ IEEE80211_RATE_MCS);
3934188784Ssam			else
3935188784Ssam				LINE_CHECK("roam:%-7.7s rssi %4udBm  MCS %2u    ",
3936188784Ssam				    modename[mode], rp->rssi/2,
3937188784Ssam				    rp->rate &~ IEEE80211_RATE_MCS);
3938188784Ssam		} else {
3939188784Ssam			if (rp->rssi & 1)
3940188784Ssam				LINE_CHECK("roam:%-7.7s rssi %2u.5dBm rate %2u Mb/s",
3941188784Ssam				    modename[mode], rp->rssi/2, rp->rate/2);
3942188784Ssam			else
3943188784Ssam				LINE_CHECK("roam:%-7.7s rssi %4udBm rate %2u Mb/s",
3944188784Ssam				    modename[mode], rp->rssi/2, rp->rate/2);
3945188784Ssam		}
3946178354Ssam	}
3947178354Ssam}
3948178354Ssam
3949343974Savos/* XXX TODO: rate-to-string method... */
3950343974Savosstatic const char*
3951343974Savosget_mcs_mbs_rate_str(uint8_t rate)
3952343974Savos{
3953343974Savos	return (rate & IEEE80211_RATE_MCS) ? "MCS " : "Mb/s";
3954343974Savos}
3955343974Savos
3956343974Savosstatic uint8_t
3957343974Savosget_rate_value(uint8_t rate)
3958343974Savos{
3959343974Savos	if (rate & IEEE80211_RATE_MCS)
3960343974Savos		return (rate &~ IEEE80211_RATE_MCS);
3961343974Savos	return (rate / 2);
3962343974Savos}
3963343974Savos
3964178354Ssamstatic void
3965178354Ssamlist_txparams(int s)
3966178354Ssam{
3967178354Ssam	const struct ieee80211_txparam *tp;
3968178354Ssam	int mode;
3969178354Ssam
3970178354Ssam	gettxparams(s);
3971188784Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
3972178354Ssam		tp = &txparams.params[mode];
3973178354Ssam		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
3974178354Ssam			continue;
3975188784Ssam		if (mode == IEEE80211_MODE_11NA || mode == IEEE80211_MODE_11NG) {
3976188784Ssam			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3977343974Savos				LINE_CHECK("%-7.7s ucast NONE    mgmt %2u %s "
3978343974Savos				    "mcast %2u %s maxretry %u",
3979188784Ssam				    modename[mode],
3980343974Savos				    get_rate_value(tp->mgmtrate),
3981343974Savos				    get_mcs_mbs_rate_str(tp->mgmtrate),
3982343974Savos				    get_rate_value(tp->mcastrate),
3983343974Savos				    get_mcs_mbs_rate_str(tp->mcastrate),
3984188784Ssam				    tp->maxretry);
3985188784Ssam			else
3986343974Savos				LINE_CHECK("%-7.7s ucast %2u MCS  mgmt %2u %s "
3987343974Savos				    "mcast %2u %s maxretry %u",
3988188784Ssam				    modename[mode],
3989188784Ssam				    tp->ucastrate &~ IEEE80211_RATE_MCS,
3990343974Savos				    get_rate_value(tp->mgmtrate),
3991343974Savos				    get_mcs_mbs_rate_str(tp->mgmtrate),
3992343974Savos				    get_rate_value(tp->mcastrate),
3993343974Savos				    get_mcs_mbs_rate_str(tp->mcastrate),
3994188784Ssam				    tp->maxretry);
3995188784Ssam		} else {
3996188784Ssam			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3997188784Ssam				LINE_CHECK("%-7.7s ucast NONE    mgmt %2u Mb/s "
3998188784Ssam				    "mcast %2u Mb/s maxretry %u",
3999188784Ssam				    modename[mode],
4000188784Ssam				    tp->mgmtrate/2,
4001188784Ssam				    tp->mcastrate/2, tp->maxretry);
4002188784Ssam			else
4003188784Ssam				LINE_CHECK("%-7.7s ucast %2u Mb/s mgmt %2u Mb/s "
4004188784Ssam				    "mcast %2u Mb/s maxretry %u",
4005188784Ssam				    modename[mode],
4006188784Ssam				    tp->ucastrate/2, tp->mgmtrate/2,
4007188784Ssam				    tp->mcastrate/2, tp->maxretry);
4008188784Ssam		}
4009178354Ssam	}
4010178354Ssam}
4011178354Ssam
4012178354Ssamstatic void
4013173275Ssamprintpolicy(int policy)
4014173275Ssam{
4015173275Ssam	switch (policy) {
4016173275Ssam	case IEEE80211_MACCMD_POLICY_OPEN:
4017173275Ssam		printf("policy: open\n");
4018173275Ssam		break;
4019173275Ssam	case IEEE80211_MACCMD_POLICY_ALLOW:
4020173275Ssam		printf("policy: allow\n");
4021173275Ssam		break;
4022173275Ssam	case IEEE80211_MACCMD_POLICY_DENY:
4023173275Ssam		printf("policy: deny\n");
4024173275Ssam		break;
4025178354Ssam	case IEEE80211_MACCMD_POLICY_RADIUS:
4026178354Ssam		printf("policy: radius\n");
4027178354Ssam		break;
4028173275Ssam	default:
4029173275Ssam		printf("policy: unknown (%u)\n", policy);
4030173275Ssam		break;
4031173275Ssam	}
4032173275Ssam}
4033173275Ssam
4034173275Ssamstatic void
4035149029Ssamlist_mac(int s)
4036149029Ssam{
4037149029Ssam	struct ieee80211req ireq;
4038149029Ssam	struct ieee80211req_maclist *acllist;
4039173275Ssam	int i, nacls, policy, len;
4040173275Ssam	uint8_t *data;
4041149029Ssam	char c;
4042149029Ssam
4043149029Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4044299873Struckman	(void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
4045149029Ssam	ireq.i_type = IEEE80211_IOC_MACCMD;
4046149029Ssam	ireq.i_val = IEEE80211_MACCMD_POLICY;
4047149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
4048149029Ssam		if (errno == EINVAL) {
4049149029Ssam			printf("No acl policy loaded\n");
4050149029Ssam			return;
4051149029Ssam		}
4052149029Ssam		err(1, "unable to get mac policy");
4053149029Ssam	}
4054149029Ssam	policy = ireq.i_val;
4055149029Ssam	if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
4056149029Ssam		c = '*';
4057149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
4058149029Ssam		c = '+';
4059149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
4060149029Ssam		c = '-';
4061178354Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_RADIUS) {
4062178354Ssam		c = 'r';		/* NB: should never have entries */
4063149029Ssam	} else {
4064149029Ssam		printf("policy: unknown (%u)\n", policy);
4065149029Ssam		c = '?';
4066149029Ssam	}
4067173275Ssam	if (verbose || c == '?')
4068173275Ssam		printpolicy(policy);
4069173275Ssam
4070175952Ssam	ireq.i_val = IEEE80211_MACCMD_LIST;
4071175952Ssam	ireq.i_len = 0;
4072175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4073173275Ssam		err(1, "unable to get mac acl list size");
4074175952Ssam	if (ireq.i_len == 0) {		/* NB: no acls */
4075173275Ssam		if (!(verbose || c == '?'))
4076173275Ssam			printpolicy(policy);
4077173275Ssam		return;
4078173275Ssam	}
4079175952Ssam	len = ireq.i_len;
4080173275Ssam
4081173275Ssam	data = malloc(len);
4082173275Ssam	if (data == NULL)
4083173275Ssam		err(1, "out of memory for acl list");
4084173275Ssam
4085175952Ssam	ireq.i_data = data;
4086175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4087173275Ssam		err(1, "unable to get mac acl list");
4088173275Ssam	nacls = len / sizeof(*acllist);
4089173275Ssam	acllist = (struct ieee80211req_maclist *) data;
4090149029Ssam	for (i = 0; i < nacls; i++)
4091149029Ssam		printf("%c%s\n", c, ether_ntoa(
4092149029Ssam			(const struct ether_addr *) acllist[i].ml_macaddr));
4093173275Ssam	free(data);
4094149029Ssam}
4095149029Ssam
4096178354Ssamstatic void
4097178354Ssamprint_regdomain(const struct ieee80211_regdomain *reg, int verb)
4098178354Ssam{
4099178354Ssam	if ((reg->regdomain != 0 &&
4100178354Ssam	    reg->regdomain != reg->country) || verb) {
4101178354Ssam		const struct regdomain *rd =
4102178354Ssam		    lib80211_regdomain_findbysku(getregdata(), reg->regdomain);
4103178354Ssam		if (rd == NULL)
4104178354Ssam			LINE_CHECK("regdomain %d", reg->regdomain);
4105178354Ssam		else
4106178354Ssam			LINE_CHECK("regdomain %s", rd->name);
4107178354Ssam	}
4108178354Ssam	if (reg->country != 0 || verb) {
4109178354Ssam		const struct country *cc =
4110178354Ssam		    lib80211_country_findbycc(getregdata(), reg->country);
4111178354Ssam		if (cc == NULL)
4112178354Ssam			LINE_CHECK("country %d", reg->country);
4113178354Ssam		else
4114178354Ssam			LINE_CHECK("country %s", cc->isoname);
4115178354Ssam	}
4116178354Ssam	if (reg->location == 'I')
4117178354Ssam		LINE_CHECK("indoor");
4118178354Ssam	else if (reg->location == 'O')
4119178354Ssam		LINE_CHECK("outdoor");
4120178354Ssam	else if (verb)
4121178354Ssam		LINE_CHECK("anywhere");
4122178354Ssam	if (reg->ecm)
4123178354Ssam		LINE_CHECK("ecm");
4124178354Ssam	else if (verb)
4125178354Ssam		LINE_CHECK("-ecm");
4126178354Ssam}
4127178354Ssam
4128178354Ssamstatic void
4129178354Ssamlist_regdomain(int s, int channelsalso)
4130178354Ssam{
4131178354Ssam	getregdomain(s);
4132178354Ssam	if (channelsalso) {
4133178354Ssam		getchaninfo(s);
4134178354Ssam		spacer = ':';
4135178354Ssam		print_regdomain(&regdomain, 1);
4136178354Ssam		LINE_BREAK();
4137187801Ssam		print_channels(s, chaninfo, 1/*allchans*/, 1/*verbose*/);
4138178354Ssam	} else
4139178354Ssam		print_regdomain(&regdomain, verbose);
4140178354Ssam}
4141178354Ssam
4142195618Srpaulostatic void
4143195618Srpaulolist_mesh(int s)
4144195618Srpaulo{
4145195618Srpaulo	struct ieee80211req ireq;
4146195618Srpaulo	struct ieee80211req_mesh_route routes[128];
4147195908Srpaulo	struct ieee80211req_mesh_route *rt;
4148195618Srpaulo
4149195618Srpaulo	(void) memset(&ireq, 0, sizeof(ireq));
4150299873Struckman	(void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
4151195618Srpaulo	ireq.i_type = IEEE80211_IOC_MESH_RTCMD;
4152195618Srpaulo	ireq.i_val = IEEE80211_MESH_RTCMD_LIST;
4153195618Srpaulo	ireq.i_data = &routes;
4154195618Srpaulo	ireq.i_len = sizeof(routes);
4155195618Srpaulo	if (ioctl(s, SIOCG80211, &ireq) < 0)
4156195618Srpaulo	 	err(1, "unable to get the Mesh routing table");
4157195618Srpaulo
4158195908Srpaulo	printf("%-17.17s %-17.17s %4s %4s %4s %6s %s\n"
4159195618Srpaulo		, "DEST"
4160195618Srpaulo		, "NEXT HOP"
4161195618Srpaulo		, "HOPS"
4162195618Srpaulo		, "METRIC"
4163195784Srpaulo		, "LIFETIME"
4164195908Srpaulo		, "MSEQ"
4165195908Srpaulo		, "FLAGS");
4166195618Srpaulo
4167195908Srpaulo	for (rt = &routes[0]; rt - &routes[0] < ireq.i_len / sizeof(*rt); rt++){
4168195618Srpaulo		printf("%s ",
4169195908Srpaulo		    ether_ntoa((const struct ether_addr *)rt->imr_dest));
4170195908Srpaulo		printf("%s %4u   %4u   %6u %6u    %c%c\n",
4171195908Srpaulo			ether_ntoa((const struct ether_addr *)rt->imr_nexthop),
4172195908Srpaulo			rt->imr_nhops, rt->imr_metric, rt->imr_lifetime,
4173195908Srpaulo			rt->imr_lastmseq,
4174234895Smonthadar			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_DISCOVER) ?
4175234895Smonthadar			    'D' :
4176195908Srpaulo			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_VALID) ?
4177195908Srpaulo			    'V' : '!',
4178195908Srpaulo			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_PROXY) ?
4179246505Smonthadar			    'P' :
4180246505Smonthadar			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_GATE) ?
4181246505Smonthadar			    'G' :' ');
4182195618Srpaulo	}
4183195618Srpaulo}
4184195618Srpaulo
4185138593Ssamstatic
4186138593SsamDECL_CMD_FUNC(set80211list, arg, d)
4187138593Ssam{
4188138593Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
4189138593Ssam
4190173275Ssam	LINE_INIT('\t');
4191173275Ssam
4192138593Ssam	if (iseq(arg, "sta"))
4193138593Ssam		list_stations(s);
4194138593Ssam	else if (iseq(arg, "scan") || iseq(arg, "ap"))
4195138593Ssam		list_scan(s);
4196138593Ssam	else if (iseq(arg, "chan") || iseq(arg, "freq"))
4197138593Ssam		list_channels(s, 1);
4198138593Ssam	else if (iseq(arg, "active"))
4199138593Ssam		list_channels(s, 0);
4200138593Ssam	else if (iseq(arg, "keys"))
4201138593Ssam		list_keys(s);
4202138593Ssam	else if (iseq(arg, "caps"))
4203138593Ssam		list_capabilities(s);
4204178354Ssam	else if (iseq(arg, "wme") || iseq(arg, "wmm"))
4205138593Ssam		list_wme(s);
4206149029Ssam	else if (iseq(arg, "mac"))
4207149029Ssam		list_mac(s);
4208170531Ssam	else if (iseq(arg, "txpow"))
4209170531Ssam		list_txpow(s);
4210178354Ssam	else if (iseq(arg, "roam"))
4211178354Ssam		list_roam(s);
4212178354Ssam	else if (iseq(arg, "txparam") || iseq(arg, "txparm"))
4213178354Ssam		list_txparams(s);
4214178354Ssam	else if (iseq(arg, "regdomain"))
4215178354Ssam		list_regdomain(s, 1);
4216178354Ssam	else if (iseq(arg, "countries"))
4217178354Ssam		list_countries();
4218195618Srpaulo	else if (iseq(arg, "mesh"))
4219195618Srpaulo		list_mesh(s);
4220138593Ssam	else
4221138593Ssam		errx(1, "Don't know how to list %s for %s", arg, name);
4222178354Ssam	LINE_BREAK();
4223138593Ssam#undef iseq
4224138593Ssam}
4225138593Ssam
4226138593Ssamstatic enum ieee80211_opmode
4227138593Ssamget80211opmode(int s)
4228138593Ssam{
4229138593Ssam	struct ifmediareq ifmr;
4230138593Ssam
4231138593Ssam	(void) memset(&ifmr, 0, sizeof(ifmr));
4232299873Struckman	(void) strlcpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
4233138593Ssam
4234138593Ssam	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
4235186101Ssam		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) {
4236186101Ssam			if (ifmr.ifm_current & IFM_FLAG0)
4237186101Ssam				return IEEE80211_M_AHDEMO;
4238186101Ssam			else
4239186101Ssam				return IEEE80211_M_IBSS;
4240186101Ssam		}
4241138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
4242138593Ssam			return IEEE80211_M_HOSTAP;
4243291352Sadrian		if (ifmr.ifm_current & IFM_IEEE80211_IBSS)
4244291352Sadrian			return IEEE80211_M_IBSS;
4245138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
4246138593Ssam			return IEEE80211_M_MONITOR;
4247195618Srpaulo		if (ifmr.ifm_current & IFM_IEEE80211_MBSS)
4248195618Srpaulo			return IEEE80211_M_MBSS;
4249138593Ssam	}
4250138593Ssam	return IEEE80211_M_STA;
4251138593Ssam}
4252138593Ssam
4253138593Ssam#if 0
4254138593Ssamstatic void
4255138593Ssamprintcipher(int s, struct ieee80211req *ireq, int keylenop)
4256138593Ssam{
4257138593Ssam	switch (ireq->i_val) {
4258138593Ssam	case IEEE80211_CIPHER_WEP:
4259138593Ssam		ireq->i_type = keylenop;
4260138593Ssam		if (ioctl(s, SIOCG80211, ireq) != -1)
4261138593Ssam			printf("WEP-%s",
4262138593Ssam			    ireq->i_len <= 5 ? "40" :
4263138593Ssam			    ireq->i_len <= 13 ? "104" : "128");
4264138593Ssam		else
4265138593Ssam			printf("WEP");
4266138593Ssam		break;
4267138593Ssam	case IEEE80211_CIPHER_TKIP:
4268138593Ssam		printf("TKIP");
4269138593Ssam		break;
4270138593Ssam	case IEEE80211_CIPHER_AES_OCB:
4271138593Ssam		printf("AES-OCB");
4272138593Ssam		break;
4273138593Ssam	case IEEE80211_CIPHER_AES_CCM:
4274138593Ssam		printf("AES-CCM");
4275138593Ssam		break;
4276138593Ssam	case IEEE80211_CIPHER_CKIP:
4277138593Ssam		printf("CKIP");
4278138593Ssam		break;
4279138593Ssam	case IEEE80211_CIPHER_NONE:
4280138593Ssam		printf("NONE");
4281138593Ssam		break;
4282138593Ssam	default:
4283138593Ssam		printf("UNKNOWN (0x%x)", ireq->i_val);
4284138593Ssam		break;
4285138593Ssam	}
4286138593Ssam}
4287138593Ssam#endif
4288138593Ssam
4289155931Ssamstatic void
4290138593Ssamprintkey(const struct ieee80211req_key *ik)
4291138593Ssam{
4292138593Ssam	static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
4293298556Spfg	u_int keylen = ik->ik_keylen;
4294138593Ssam	int printcontents;
4295138593Ssam
4296148001Srwatson	printcontents = printkeys &&
4297138593Ssam		(memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
4298138593Ssam	if (printcontents)
4299138593Ssam		LINE_BREAK();
4300138593Ssam	switch (ik->ik_type) {
4301138593Ssam	case IEEE80211_CIPHER_WEP:
4302138593Ssam		/* compatibility */
4303155931Ssam		LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
4304138593Ssam		    keylen <= 5 ? "40-bit" :
4305138593Ssam		    keylen <= 13 ? "104-bit" : "128-bit");
4306138593Ssam		break;
4307138593Ssam	case IEEE80211_CIPHER_TKIP:
4308138593Ssam		if (keylen > 128/8)
4309138593Ssam			keylen -= 128/8;	/* ignore MIC for now */
4310155931Ssam		LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4311138593Ssam		break;
4312138593Ssam	case IEEE80211_CIPHER_AES_OCB:
4313155931Ssam		LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4314138593Ssam		break;
4315138593Ssam	case IEEE80211_CIPHER_AES_CCM:
4316155931Ssam		LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4317138593Ssam		break;
4318138593Ssam	case IEEE80211_CIPHER_CKIP:
4319155931Ssam		LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4320138593Ssam		break;
4321138593Ssam	case IEEE80211_CIPHER_NONE:
4322155931Ssam		LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4323138593Ssam		break;
4324138593Ssam	default:
4325155931Ssam		LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
4326138593Ssam			ik->ik_type, ik->ik_keyix+1, 8*keylen);
4327138593Ssam		break;
4328138593Ssam	}
4329138593Ssam	if (printcontents) {
4330298556Spfg		u_int i;
4331138593Ssam
4332138593Ssam		printf(" <");
4333138593Ssam		for (i = 0; i < keylen; i++)
4334138593Ssam			printf("%02x", ik->ik_keydata[i]);
4335138593Ssam		printf(">");
4336138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4337138593Ssam		    (ik->ik_keyrsc != 0 || verbose))
4338146873Sjhb			printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
4339138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4340138593Ssam		    (ik->ik_keytsc != 0 || verbose))
4341146873Sjhb			printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
4342138593Ssam		if (ik->ik_flags != 0 && verbose) {
4343138593Ssam			const char *sep = " ";
4344138593Ssam
4345138593Ssam			if (ik->ik_flags & IEEE80211_KEY_XMIT)
4346138593Ssam				printf("%stx", sep), sep = "+";
4347138593Ssam			if (ik->ik_flags & IEEE80211_KEY_RECV)
4348138593Ssam				printf("%srx", sep), sep = "+";
4349138593Ssam			if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
4350138593Ssam				printf("%sdef", sep), sep = "+";
4351138593Ssam		}
4352138593Ssam		LINE_BREAK();
4353138593Ssam	}
4354138593Ssam}
4355138593Ssam
4356138593Ssamstatic void
4357173275Ssamprintrate(const char *tag, int v, int defrate, int defmcs)
4358138593Ssam{
4359188784Ssam	if ((v & IEEE80211_RATE_MCS) == 0) {
4360188784Ssam		if (v != defrate) {
4361188784Ssam			if (v & 1)
4362188784Ssam				LINE_CHECK("%s %d.5", tag, v/2);
4363188784Ssam			else
4364188784Ssam				LINE_CHECK("%s %d", tag, v/2);
4365188784Ssam		}
4366188784Ssam	} else {
4367173275Ssam		if (v != defmcs)
4368173275Ssam			LINE_CHECK("%s %d", tag, v &~ 0x80);
4369173275Ssam	}
4370173275Ssam}
4371173275Ssam
4372173275Ssamstatic int
4373195618Srpaulogetid(int s, int ix, void *data, size_t len, int *plen, int mesh)
4374173275Ssam{
4375138593Ssam	struct ieee80211req ireq;
4376138593Ssam
4377138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4378299873Struckman	(void) strlcpy(ireq.i_name, name, sizeof(ireq.i_name));
4379195618Srpaulo	ireq.i_type = (!mesh) ? IEEE80211_IOC_SSID : IEEE80211_IOC_MESH_ID;
4380173275Ssam	ireq.i_val = ix;
4381173275Ssam	ireq.i_data = data;
4382173275Ssam	ireq.i_len = len;
4383173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4384173275Ssam		return -1;
4385173275Ssam	*plen = ireq.i_len;
4386173275Ssam	return 0;
4387173275Ssam}
438877218Sphk
4389173275Ssamstatic void
4390173275Ssamieee80211_status(int s)
4391173275Ssam{
4392173275Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
4393173275Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
4394173275Ssam	int i, num, wpa, wme, bgscan, bgscaninterval, val, len, wepmode;
4395173275Ssam	uint8_t data[32];
4396173275Ssam	const struct ieee80211_channel *c;
4397178354Ssam	const struct ieee80211_roamparam *rp;
4398178354Ssam	const struct ieee80211_txparam *tp;
4399173275Ssam
4400195618Srpaulo	if (getid(s, -1, data, sizeof(data), &len, 0) < 0) {
4401148686Sstefanf		/* If we can't get the SSID, this isn't an 802.11 device. */
440277218Sphk		return;
440377218Sphk	}
4404173275Ssam
4405173275Ssam	/*
4406173275Ssam	 * Invalidate cached state so printing status for multiple
4407173275Ssam	 * if's doesn't reuse the first interfaces' cached state.
4408173275Ssam	 */
4409173275Ssam	gotcurchan = 0;
4410178354Ssam	gotroam = 0;
4411178354Ssam	gottxparams = 0;
4412173275Ssam	gothtconf = 0;
4413178354Ssam	gotregdomain = 0;
4414173275Ssam
4415195618Srpaulo	printf("\t");
4416195618Srpaulo	if (opmode == IEEE80211_M_MBSS) {
4417195618Srpaulo		printf("meshid ");
4418195618Srpaulo		getid(s, 0, data, sizeof(data), &len, 1);
4419195618Srpaulo		print_string(data, len);
4420195618Srpaulo	} else {
4421195618Srpaulo		if (get80211val(s, IEEE80211_IOC_NUMSSIDS, &num) < 0)
4422195618Srpaulo			num = 0;
4423195618Srpaulo		printf("ssid ");
4424195618Srpaulo		if (num > 1) {
4425195618Srpaulo			for (i = 0; i < num; i++) {
4426195618Srpaulo				if (getid(s, i, data, sizeof(data), &len, 0) >= 0 && len > 0) {
4427195618Srpaulo					printf(" %d:", i + 1);
4428195618Srpaulo					print_string(data, len);
4429195618Srpaulo				}
4430138593Ssam			}
4431195618Srpaulo		} else
4432195618Srpaulo			print_string(data, len);
4433195618Srpaulo	}
4434173275Ssam	c = getcurchan(s);
4435170531Ssam	if (c->ic_freq != IEEE80211_CHAN_ANY) {
4436170531Ssam		char buf[14];
4437202161Sgavin		printf(" channel %d (%u MHz%s)", c->ic_ieee, c->ic_freq,
4438170531Ssam			get_chaninfo(c, 1, buf, sizeof(buf)));
4439138593Ssam	} else if (verbose)
4440138593Ssam		printf(" channel UNDEF");
4441138593Ssam
4442173275Ssam	if (get80211(s, IEEE80211_IOC_BSSID, data, IEEE80211_ADDR_LEN) >= 0 &&
4443173275Ssam	    (memcmp(data, zerobssid, sizeof(zerobssid)) != 0 || verbose))
4444173275Ssam		printf(" bssid %s", ether_ntoa((struct ether_addr *)data));
4445138593Ssam
4446173275Ssam	if (get80211len(s, IEEE80211_IOC_STATIONNAME, data, sizeof(data), &len) != -1) {
4447138593Ssam		printf("\n\tstationname ");
4448173275Ssam		print_string(data, len);
444977218Sphk	}
445077218Sphk
4451138593Ssam	spacer = ' ';		/* force first break */
4452138593Ssam	LINE_BREAK();
445377218Sphk
4454178354Ssam	list_regdomain(s, 0);
4455178354Ssam
4456173275Ssam	wpa = 0;
4457173275Ssam	if (get80211val(s, IEEE80211_IOC_AUTHMODE, &val) != -1) {
4458173275Ssam		switch (val) {
4459173275Ssam		case IEEE80211_AUTH_NONE:
4460173275Ssam			LINE_CHECK("authmode NONE");
4461173275Ssam			break;
4462173275Ssam		case IEEE80211_AUTH_OPEN:
4463173275Ssam			LINE_CHECK("authmode OPEN");
4464173275Ssam			break;
4465173275Ssam		case IEEE80211_AUTH_SHARED:
4466173275Ssam			LINE_CHECK("authmode SHARED");
4467173275Ssam			break;
4468173275Ssam		case IEEE80211_AUTH_8021X:
4469173275Ssam			LINE_CHECK("authmode 802.1x");
4470173275Ssam			break;
4471173275Ssam		case IEEE80211_AUTH_WPA:
4472173275Ssam			if (get80211val(s, IEEE80211_IOC_WPA, &wpa) < 0)
4473173275Ssam				wpa = 1;	/* default to WPA1 */
4474173275Ssam			switch (wpa) {
4475173275Ssam			case 2:
4476173275Ssam				LINE_CHECK("authmode WPA2/802.11i");
447777218Sphk				break;
4478173275Ssam			case 3:
4479173275Ssam				LINE_CHECK("authmode WPA1+WPA2/802.11i");
448077218Sphk				break;
4481127649Ssam			default:
4482173275Ssam				LINE_CHECK("authmode WPA");
4483127649Ssam				break;
4484173275Ssam			}
4485173275Ssam			break;
4486173275Ssam		case IEEE80211_AUTH_AUTO:
4487173275Ssam			LINE_CHECK("authmode AUTO");
4488173275Ssam			break;
4489173275Ssam		default:
4490173275Ssam			LINE_CHECK("authmode UNKNOWN (0x%x)", val);
4491173275Ssam			break;
4492127649Ssam		}
4493127649Ssam	}
4494127649Ssam
4495173275Ssam	if (wpa || verbose) {
4496178354Ssam		if (get80211val(s, IEEE80211_IOC_WPS, &val) != -1) {
4497178354Ssam			if (val)
4498178354Ssam				LINE_CHECK("wps");
4499178354Ssam			else if (verbose)
4500178354Ssam				LINE_CHECK("-wps");
4501178354Ssam		}
4502178354Ssam		if (get80211val(s, IEEE80211_IOC_TSN, &val) != -1) {
4503178354Ssam			if (val)
4504178354Ssam				LINE_CHECK("tsn");
4505178354Ssam			else if (verbose)
4506178354Ssam				LINE_CHECK("-tsn");
4507178354Ssam		}
4508173275Ssam		if (ioctl(s, IEEE80211_IOC_COUNTERMEASURES, &val) != -1) {
4509173275Ssam			if (val)
4510173275Ssam				LINE_CHECK("countermeasures");
4511173275Ssam			else if (verbose)
4512173275Ssam				LINE_CHECK("-countermeasures");
4513173275Ssam		}
4514178354Ssam#if 0
4515178354Ssam		/* XXX not interesting with WPA done in user space */
4516178354Ssam		ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
4517178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4518178354Ssam		}
4519178354Ssam
4520178354Ssam		ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
4521178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4522178354Ssam			LINE_CHECK("mcastcipher ");
4523178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
4524178354Ssam			spacer = ' ';
4525178354Ssam		}
4526178354Ssam
4527178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
4528178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4529178354Ssam			LINE_CHECK("ucastcipher ");
4530178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
4531178354Ssam		}
4532178354Ssam
4533178354Ssam		if (wpa & 2) {
4534178354Ssam			ireq.i_type = IEEE80211_IOC_RSNCAPS;
4535178354Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1) {
4536178354Ssam				LINE_CHECK("RSN caps 0x%x", ireq.i_val);
4537178354Ssam				spacer = ' ';
4538178354Ssam			}
4539178354Ssam		}
4540178354Ssam
4541178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
4542178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4543178354Ssam		}
4544178354Ssam#endif
4545173275Ssam	}
4546138593Ssam
4547173275Ssam	if (get80211val(s, IEEE80211_IOC_WEP, &wepmode) != -1 &&
4548173275Ssam	    wepmode != IEEE80211_WEP_NOSUP) {
4549173275Ssam
4550138718Ssam		switch (wepmode) {
4551173275Ssam		case IEEE80211_WEP_OFF:
4552173275Ssam			LINE_CHECK("privacy OFF");
4553173275Ssam			break;
4554173275Ssam		case IEEE80211_WEP_ON:
4555173275Ssam			LINE_CHECK("privacy ON");
4556173275Ssam			break;
4557173275Ssam		case IEEE80211_WEP_MIXED:
4558173275Ssam			LINE_CHECK("privacy MIXED");
4559173275Ssam			break;
4560173275Ssam		default:
4561173275Ssam			LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
4562173275Ssam			break;
456377218Sphk		}
456477218Sphk
456577218Sphk		/*
456677218Sphk		 * If we get here then we've got WEP support so we need
456777218Sphk		 * to print WEP status.
456891454Sbrooks		 */
456977218Sphk
4570173275Ssam		if (get80211val(s, IEEE80211_IOC_WEPTXKEY, &val) < 0) {
457177218Sphk			warn("WEP support, but no tx key!");
457277218Sphk			goto end;
457377218Sphk		}
4574173275Ssam		if (val != -1)
4575173275Ssam			LINE_CHECK("deftxkey %d", val+1);
4576138718Ssam		else if (wepmode != IEEE80211_WEP_OFF || verbose)
4577155931Ssam			LINE_CHECK("deftxkey UNDEF");
457877218Sphk
4579173275Ssam		if (get80211val(s, IEEE80211_IOC_NUMWEPKEYS, &num) < 0) {
458077218Sphk			warn("WEP support, but no NUMWEPKEYS support!");
458177218Sphk			goto end;
458277218Sphk		}
458377218Sphk
4584138593Ssam		for (i = 0; i < num; i++) {
4585138593Ssam			struct ieee80211req_key ik;
458677218Sphk
4587138593Ssam			memset(&ik, 0, sizeof(ik));
4588138593Ssam			ik.ik_keyix = i;
4589173275Ssam			if (get80211(s, IEEE80211_IOC_WPAKEY, &ik, sizeof(ik)) < 0) {
459077218Sphk				warn("WEP support, but can get keys!");
459177218Sphk				goto end;
459277218Sphk			}
4593138593Ssam			if (ik.ik_keylen != 0) {
4594138593Ssam				if (verbose)
4595138593Ssam					LINE_BREAK();
4596138593Ssam				printkey(&ik);
4597138593Ssam			}
4598138593Ssam		}
4599173275Ssamend:
4600173275Ssam		;
4601138593Ssam	}
4602138593Ssam
4603173275Ssam	if (get80211val(s, IEEE80211_IOC_POWERSAVE, &val) != -1 &&
4604173275Ssam	    val != IEEE80211_POWERSAVE_NOSUP ) {
4605173275Ssam		if (val != IEEE80211_POWERSAVE_OFF || verbose) {
4606173275Ssam			switch (val) {
4607173275Ssam			case IEEE80211_POWERSAVE_OFF:
4608173275Ssam				LINE_CHECK("powersavemode OFF");
4609173275Ssam				break;
4610173275Ssam			case IEEE80211_POWERSAVE_CAM:
4611173275Ssam				LINE_CHECK("powersavemode CAM");
4612173275Ssam				break;
4613173275Ssam			case IEEE80211_POWERSAVE_PSP:
4614173275Ssam				LINE_CHECK("powersavemode PSP");
4615173275Ssam				break;
4616173275Ssam			case IEEE80211_POWERSAVE_PSP_CAM:
4617173275Ssam				LINE_CHECK("powersavemode PSP-CAM");
4618173275Ssam				break;
4619138593Ssam			}
4620173275Ssam			if (get80211val(s, IEEE80211_IOC_POWERSAVESLEEP, &val) != -1)
4621173275Ssam				LINE_CHECK("powersavesleep %d", val);
4622138593Ssam		}
4623138593Ssam	}
4624138593Ssam
4625173275Ssam	if (get80211val(s, IEEE80211_IOC_TXPOWER, &val) != -1) {
4626173275Ssam		if (val & 1)
4627173275Ssam			LINE_CHECK("txpower %d.5", val/2);
4628173275Ssam		else
4629173275Ssam			LINE_CHECK("txpower %d", val/2);
4630173275Ssam	}
4631138593Ssam	if (verbose) {
4632173275Ssam		if (get80211val(s, IEEE80211_IOC_TXPOWMAX, &val) != -1)
4633173275Ssam			LINE_CHECK("txpowmax %.1f", val/2.);
4634138593Ssam	}
4635138593Ssam
4636178354Ssam	if (get80211val(s, IEEE80211_IOC_DOTD, &val) != -1) {
4637178354Ssam		if (val)
4638178354Ssam			LINE_CHECK("dotd");
4639178354Ssam		else if (verbose)
4640178354Ssam			LINE_CHECK("-dotd");
4641178354Ssam	}
4642178354Ssam
4643173275Ssam	if (get80211val(s, IEEE80211_IOC_RTSTHRESHOLD, &val) != -1) {
4644173275Ssam		if (val != IEEE80211_RTS_MAX || verbose)
4645173275Ssam			LINE_CHECK("rtsthreshold %d", val);
4646138593Ssam	}
4647138593Ssam
4648173275Ssam	if (get80211val(s, IEEE80211_IOC_FRAGTHRESHOLD, &val) != -1) {
4649173275Ssam		if (val != IEEE80211_FRAG_MAX || verbose)
4650173275Ssam			LINE_CHECK("fragthreshold %d", val);
4651170531Ssam	}
4652173275Ssam	if (opmode == IEEE80211_M_STA || verbose) {
4653173275Ssam		if (get80211val(s, IEEE80211_IOC_BMISSTHRESHOLD, &val) != -1) {
4654173275Ssam			if (val != IEEE80211_HWBMISS_MAX || verbose)
4655173275Ssam				LINE_CHECK("bmiss %d", val);
4656153354Ssam		}
4657153354Ssam	}
4658153354Ssam
4659178354Ssam	if (!verbose) {
4660178354Ssam		gettxparams(s);
4661178354Ssam		tp = &txparams.params[chan2mode(c)];
4662178354Ssam		printrate("ucastrate", tp->ucastrate,
4663178354Ssam		    IEEE80211_FIXED_RATE_NONE, IEEE80211_FIXED_RATE_NONE);
4664188784Ssam		printrate("mcastrate", tp->mcastrate, 2*1,
4665188784Ssam		    IEEE80211_RATE_MCS|0);
4666188784Ssam		printrate("mgmtrate", tp->mgmtrate, 2*1,
4667188784Ssam		    IEEE80211_RATE_MCS|0);
4668178354Ssam		if (tp->maxretry != 6)		/* XXX */
4669178354Ssam			LINE_CHECK("maxretry %d", tp->maxretry);
4670178354Ssam	} else {
4671178354Ssam		LINE_BREAK();
4672178354Ssam		list_txparams(s);
4673178354Ssam	}
4674170531Ssam
4675173275Ssam	bgscaninterval = -1;
4676173275Ssam	(void) get80211val(s, IEEE80211_IOC_BGSCAN_INTERVAL, &bgscaninterval);
4677173275Ssam
4678173275Ssam	if (get80211val(s, IEEE80211_IOC_SCANVALID, &val) != -1) {
4679173275Ssam		if (val != bgscaninterval || verbose)
4680173275Ssam			LINE_CHECK("scanvalid %u", val);
4681148416Ssam	}
4682148416Ssam
4683173275Ssam	bgscan = 0;
4684173275Ssam	if (get80211val(s, IEEE80211_IOC_BGSCAN, &bgscan) != -1) {
4685173275Ssam		if (bgscan)
4686170531Ssam			LINE_CHECK("bgscan");
4687170531Ssam		else if (verbose)
4688170531Ssam			LINE_CHECK("-bgscan");
4689160687Ssam	}
4690170531Ssam	if (bgscan || verbose) {
4691170531Ssam		if (bgscaninterval != -1)
4692170531Ssam			LINE_CHECK("bgscanintvl %u", bgscaninterval);
4693173275Ssam		if (get80211val(s, IEEE80211_IOC_BGSCAN_IDLE, &val) != -1)
4694173275Ssam			LINE_CHECK("bgscanidle %u", val);
4695178354Ssam		if (!verbose) {
4696178354Ssam			getroam(s);
4697178354Ssam			rp = &roamparams.params[chan2mode(c)];
4698178354Ssam			if (rp->rssi & 1)
4699178354Ssam				LINE_CHECK("roam:rssi %u.5", rp->rssi/2);
4700178354Ssam			else
4701178354Ssam				LINE_CHECK("roam:rssi %u", rp->rssi/2);
4702344196Savos			LINE_CHECK("roam:rate %s%u",
4703344196Savos			    (rp->rate & IEEE80211_RATE_MCS) ? "MCS " : "",
4704344196Savos			    get_rate_value(rp->rate));
4705178354Ssam		} else {
4706178354Ssam			LINE_BREAK();
4707178354Ssam			list_roam(s);
4708205514Srpaulo			LINE_BREAK();
4709170531Ssam		}
4710170531Ssam	}
4711160687Ssam
4712165570Ssam	if (IEEE80211_IS_CHAN_ANYG(c) || verbose) {
4713173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREG, &val) != -1) {
4714173275Ssam			if (val)
4715155931Ssam				LINE_CHECK("pureg");
4716147795Ssam			else if (verbose)
4717155931Ssam				LINE_CHECK("-pureg");
4718147795Ssam		}
4719173275Ssam		if (get80211val(s, IEEE80211_IOC_PROTMODE, &val) != -1) {
4720173275Ssam			switch (val) {
4721173275Ssam			case IEEE80211_PROTMODE_OFF:
4722173275Ssam				LINE_CHECK("protmode OFF");
4723173275Ssam				break;
4724173275Ssam			case IEEE80211_PROTMODE_CTS:
4725173275Ssam				LINE_CHECK("protmode CTS");
4726173275Ssam				break;
4727173275Ssam			case IEEE80211_PROTMODE_RTSCTS:
4728173275Ssam				LINE_CHECK("protmode RTSCTS");
4729173275Ssam				break;
4730173275Ssam			default:
4731173275Ssam				LINE_CHECK("protmode UNKNOWN (0x%x)", val);
4732173275Ssam				break;
4733138593Ssam			}
4734138593Ssam		}
4735138593Ssam	}
4736138593Ssam
4737173275Ssam	if (IEEE80211_IS_CHAN_HT(c) || verbose) {
4738173275Ssam		gethtconf(s);
4739173275Ssam		switch (htconf & 3) {
4740173275Ssam		case 0:
4741173275Ssam		case 2:
4742173275Ssam			LINE_CHECK("-ht");
4743173275Ssam			break;
4744173275Ssam		case 1:
4745173275Ssam			LINE_CHECK("ht20");
4746173275Ssam			break;
4747173275Ssam		case 3:
4748173275Ssam			if (verbose)
4749173275Ssam				LINE_CHECK("ht");
4750173275Ssam			break;
4751173275Ssam		}
4752173275Ssam		if (get80211val(s, IEEE80211_IOC_HTCOMPAT, &val) != -1) {
4753173275Ssam			if (!val)
4754173275Ssam				LINE_CHECK("-htcompat");
4755173275Ssam			else if (verbose)
4756173275Ssam				LINE_CHECK("htcompat");
4757173275Ssam		}
4758173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU, &val) != -1) {
4759173275Ssam			switch (val) {
4760173275Ssam			case 0:
4761173275Ssam				LINE_CHECK("-ampdu");
4762173275Ssam				break;
4763173275Ssam			case 1:
4764173275Ssam				LINE_CHECK("ampdutx -ampdurx");
4765173275Ssam				break;
4766173275Ssam			case 2:
4767173275Ssam				LINE_CHECK("-ampdutx ampdurx");
4768173275Ssam				break;
4769173275Ssam			case 3:
4770173275Ssam				if (verbose)
4771173275Ssam					LINE_CHECK("ampdu");
4772173275Ssam				break;
4773173275Ssam			}
4774173275Ssam		}
4775173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_LIMIT, &val) != -1) {
4776173275Ssam			switch (val) {
4777173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_8K:
4778173275Ssam				LINE_CHECK("ampdulimit 8k");
4779173275Ssam				break;
4780173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_16K:
4781173275Ssam				LINE_CHECK("ampdulimit 16k");
4782173275Ssam				break;
4783173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_32K:
4784173275Ssam				LINE_CHECK("ampdulimit 32k");
4785173275Ssam				break;
4786173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_64K:
4787173275Ssam				LINE_CHECK("ampdulimit 64k");
4788173275Ssam				break;
4789173275Ssam			}
4790173275Ssam		}
4791173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_DENSITY, &val) != -1) {
4792173275Ssam			switch (val) {
4793173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_NA:
4794173275Ssam				if (verbose)
4795183260Ssam					LINE_CHECK("ampdudensity NA");
4796173275Ssam				break;
4797173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_025:
4798173275Ssam				LINE_CHECK("ampdudensity .25");
4799173275Ssam				break;
4800173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_05:
4801173275Ssam				LINE_CHECK("ampdudensity .5");
4802173275Ssam				break;
4803173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_1:
4804173275Ssam				LINE_CHECK("ampdudensity 1");
4805173275Ssam				break;
4806173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_2:
4807173275Ssam				LINE_CHECK("ampdudensity 2");
4808173275Ssam				break;
4809173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_4:
4810173275Ssam				LINE_CHECK("ampdudensity 4");
4811173275Ssam				break;
4812173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_8:
4813173275Ssam				LINE_CHECK("ampdudensity 8");
4814173275Ssam				break;
4815173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_16:
4816173275Ssam				LINE_CHECK("ampdudensity 16");
4817173275Ssam				break;
4818173275Ssam			}
4819173275Ssam		}
4820173275Ssam		if (get80211val(s, IEEE80211_IOC_AMSDU, &val) != -1) {
4821173275Ssam			switch (val) {
4822173275Ssam			case 0:
4823173275Ssam				LINE_CHECK("-amsdu");
4824173275Ssam				break;
4825173275Ssam			case 1:
4826173275Ssam				LINE_CHECK("amsdutx -amsdurx");
4827173275Ssam				break;
4828173275Ssam			case 2:
4829173275Ssam				LINE_CHECK("-amsdutx amsdurx");
4830173275Ssam				break;
4831173275Ssam			case 3:
4832173275Ssam				if (verbose)
4833173275Ssam					LINE_CHECK("amsdu");
4834173275Ssam				break;
4835173275Ssam			}
4836173275Ssam		}
4837173275Ssam		/* XXX amsdu limit */
4838173275Ssam		if (get80211val(s, IEEE80211_IOC_SHORTGI, &val) != -1) {
4839173275Ssam			if (val)
4840173275Ssam				LINE_CHECK("shortgi");
4841173275Ssam			else if (verbose)
4842173275Ssam				LINE_CHECK("-shortgi");
4843173275Ssam		}
4844173275Ssam		if (get80211val(s, IEEE80211_IOC_HTPROTMODE, &val) != -1) {
4845173275Ssam			if (val == IEEE80211_PROTMODE_OFF)
4846173275Ssam				LINE_CHECK("htprotmode OFF");
4847173275Ssam			else if (val != IEEE80211_PROTMODE_RTSCTS)
4848173275Ssam				LINE_CHECK("htprotmode UNKNOWN (0x%x)", val);
4849173275Ssam			else if (verbose)
4850173275Ssam				LINE_CHECK("htprotmode RTSCTS");
4851173275Ssam		}
4852173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREN, &val) != -1) {
4853173275Ssam			if (val)
4854173275Ssam				LINE_CHECK("puren");
4855173275Ssam			else if (verbose)
4856173275Ssam				LINE_CHECK("-puren");
4857173275Ssam		}
4858183261Ssam		if (get80211val(s, IEEE80211_IOC_SMPS, &val) != -1) {
4859183261Ssam			if (val == IEEE80211_HTCAP_SMPS_DYNAMIC)
4860183261Ssam				LINE_CHECK("smpsdyn");
4861183261Ssam			else if (val == IEEE80211_HTCAP_SMPS_ENA)
4862183261Ssam				LINE_CHECK("smps");
4863183261Ssam			else if (verbose)
4864183261Ssam				LINE_CHECK("-smps");
4865183261Ssam		}
4866183261Ssam		if (get80211val(s, IEEE80211_IOC_RIFS, &val) != -1) {
4867183261Ssam			if (val)
4868183261Ssam				LINE_CHECK("rifs");
4869183261Ssam			else if (verbose)
4870183261Ssam				LINE_CHECK("-rifs");
4871183261Ssam		}
4872298606Sadrian		if (get80211val(s, IEEE80211_IOC_STBC, &val) != -1) {
4873298606Sadrian			switch (val) {
4874298606Sadrian			case 0:
4875298606Sadrian				LINE_CHECK("-stbc");
4876298606Sadrian				break;
4877298606Sadrian			case 1:
4878298606Sadrian				LINE_CHECK("stbctx -stbcrx");
4879298606Sadrian				break;
4880298606Sadrian			case 2:
4881298606Sadrian				LINE_CHECK("-stbctx stbcrx");
4882298606Sadrian				break;
4883298606Sadrian			case 3:
4884298606Sadrian				if (verbose)
4885298606Sadrian					LINE_CHECK("stbc");
4886298606Sadrian				break;
4887298606Sadrian			}
4888298606Sadrian		}
4889173275Ssam	}
4890173275Ssam
4891173275Ssam	if (get80211val(s, IEEE80211_IOC_WME, &wme) != -1) {
4892138593Ssam		if (wme)
4893155931Ssam			LINE_CHECK("wme");
4894138593Ssam		else if (verbose)
4895155931Ssam			LINE_CHECK("-wme");
4896138593Ssam	} else
4897138593Ssam		wme = 0;
4898138593Ssam
4899173275Ssam	if (get80211val(s, IEEE80211_IOC_BURST, &val) != -1) {
4900173275Ssam		if (val)
4901155931Ssam			LINE_CHECK("burst");
4902153422Ssam		else if (verbose)
4903155931Ssam			LINE_CHECK("-burst");
4904153422Ssam	}
4905153422Ssam
4906173275Ssam	if (get80211val(s, IEEE80211_IOC_FF, &val) != -1) {
4907173275Ssam		if (val)
4908170531Ssam			LINE_CHECK("ff");
4909170531Ssam		else if (verbose)
4910170531Ssam			LINE_CHECK("-ff");
4911170531Ssam	}
4912173275Ssam	if (get80211val(s, IEEE80211_IOC_TURBOP, &val) != -1) {
4913173275Ssam		if (val)
4914170531Ssam			LINE_CHECK("dturbo");
4915170531Ssam		else if (verbose)
4916170531Ssam			LINE_CHECK("-dturbo");
4917170531Ssam	}
4918178354Ssam	if (get80211val(s, IEEE80211_IOC_DWDS, &val) != -1) {
4919178354Ssam		if (val)
4920178354Ssam			LINE_CHECK("dwds");
4921178354Ssam		else if (verbose)
4922178354Ssam			LINE_CHECK("-dwds");
4923178354Ssam	}
4924170531Ssam
4925138593Ssam	if (opmode == IEEE80211_M_HOSTAP) {
4926173275Ssam		if (get80211val(s, IEEE80211_IOC_HIDESSID, &val) != -1) {
4927173275Ssam			if (val)
4928168075Ssam				LINE_CHECK("hidessid");
4929138593Ssam			else if (verbose)
4930168075Ssam				LINE_CHECK("-hidessid");
4931138593Ssam		}
4932173275Ssam		if (get80211val(s, IEEE80211_IOC_APBRIDGE, &val) != -1) {
4933173275Ssam			if (!val)
4934155931Ssam				LINE_CHECK("-apbridge");
4935138593Ssam			else if (verbose)
4936155931Ssam				LINE_CHECK("apbridge");
4937138593Ssam		}
4938173275Ssam		if (get80211val(s, IEEE80211_IOC_DTIM_PERIOD, &val) != -1)
4939173275Ssam			LINE_CHECK("dtimperiod %u", val);
4940138593Ssam
4941173275Ssam		if (get80211val(s, IEEE80211_IOC_DOTH, &val) != -1) {
4942173275Ssam			if (!val)
4943170531Ssam				LINE_CHECK("-doth");
4944170531Ssam			else if (verbose)
4945170531Ssam				LINE_CHECK("doth");
4946170531Ssam		}
4947178354Ssam		if (get80211val(s, IEEE80211_IOC_DFS, &val) != -1) {
4948178354Ssam			if (!val)
4949178354Ssam				LINE_CHECK("-dfs");
4950178354Ssam			else if (verbose)
4951178354Ssam				LINE_CHECK("dfs");
4952178354Ssam		}
4953173275Ssam		if (get80211val(s, IEEE80211_IOC_INACTIVITY, &val) != -1) {
4954173275Ssam			if (!val)
4955173275Ssam				LINE_CHECK("-inact");
4956173275Ssam			else if (verbose)
4957173275Ssam				LINE_CHECK("inact");
4958173275Ssam		}
4959138593Ssam	} else {
4960173275Ssam		if (get80211val(s, IEEE80211_IOC_ROAMING, &val) != -1) {
4961173275Ssam			if (val != IEEE80211_ROAMING_AUTO || verbose) {
4962173275Ssam				switch (val) {
4963138593Ssam				case IEEE80211_ROAMING_DEVICE:
4964155931Ssam					LINE_CHECK("roaming DEVICE");
4965138593Ssam					break;
4966138593Ssam				case IEEE80211_ROAMING_AUTO:
4967155931Ssam					LINE_CHECK("roaming AUTO");
4968138593Ssam					break;
4969138593Ssam				case IEEE80211_ROAMING_MANUAL:
4970155931Ssam					LINE_CHECK("roaming MANUAL");
4971138593Ssam					break;
4972138593Ssam				default:
4973155931Ssam					LINE_CHECK("roaming UNKNOWN (0x%x)",
4974173275Ssam						val);
4975138593Ssam					break;
4976138593Ssam				}
4977138593Ssam			}
4978138593Ssam		}
4979138593Ssam	}
4980186101Ssam
4981186904Ssam	if (opmode == IEEE80211_M_AHDEMO) {
4982186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOT, &val) != -1)
4983186904Ssam			LINE_CHECK("tdmaslot %u", val);
4984186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTCNT, &val) != -1)
4985186904Ssam			LINE_CHECK("tdmaslotcnt %u", val);
4986186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTLEN, &val) != -1)
4987186904Ssam			LINE_CHECK("tdmaslotlen %u", val);
4988186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_BINTERVAL, &val) != -1)
4989186904Ssam			LINE_CHECK("tdmabintval %u", val);
4990186904Ssam	} else if (get80211val(s, IEEE80211_IOC_BEACON_INTERVAL, &val) != -1) {
4991173275Ssam		/* XXX default define not visible */
4992173275Ssam		if (val != 100 || verbose)
4993173275Ssam			LINE_CHECK("bintval %u", val);
4994138593Ssam	}
4995138593Ssam
4996138593Ssam	if (wme && verbose) {
4997138593Ssam		LINE_BREAK();
4998138593Ssam		list_wme(s);
4999138593Ssam	}
5000195618Srpaulo
5001195618Srpaulo	if (opmode == IEEE80211_M_MBSS) {
5002195618Srpaulo		if (get80211val(s, IEEE80211_IOC_MESH_TTL, &val) != -1) {
5003195618Srpaulo			LINE_CHECK("meshttl %u", val);
5004195618Srpaulo		}
5005195618Srpaulo		if (get80211val(s, IEEE80211_IOC_MESH_AP, &val) != -1) {
5006195618Srpaulo			if (val)
5007195618Srpaulo				LINE_CHECK("meshpeering");
5008195618Srpaulo			else
5009195618Srpaulo				LINE_CHECK("-meshpeering");
5010195618Srpaulo		}
5011195618Srpaulo		if (get80211val(s, IEEE80211_IOC_MESH_FWRD, &val) != -1) {
5012195618Srpaulo			if (val)
5013195618Srpaulo				LINE_CHECK("meshforward");
5014195618Srpaulo			else
5015195618Srpaulo				LINE_CHECK("-meshforward");
5016195618Srpaulo		}
5017234893Smonthadar		if (get80211val(s, IEEE80211_IOC_MESH_GATE, &val) != -1) {
5018234893Smonthadar			if (val)
5019234893Smonthadar				LINE_CHECK("meshgate");
5020234893Smonthadar			else
5021234893Smonthadar				LINE_CHECK("-meshgate");
5022234893Smonthadar		}
5023195618Srpaulo		if (get80211len(s, IEEE80211_IOC_MESH_PR_METRIC, data, 12,
5024195618Srpaulo		    &len) != -1) {
5025195618Srpaulo			data[len] = '\0';
5026195618Srpaulo			LINE_CHECK("meshmetric %s", data);
5027195618Srpaulo		}
5028195618Srpaulo		if (get80211len(s, IEEE80211_IOC_MESH_PR_PATH, data, 12,
5029195618Srpaulo		    &len) != -1) {
5030195618Srpaulo			data[len] = '\0';
5031195618Srpaulo			LINE_CHECK("meshpath %s", data);
5032195618Srpaulo		}
5033195618Srpaulo		if (get80211val(s, IEEE80211_IOC_HWMP_ROOTMODE, &val) != -1) {
5034195618Srpaulo			switch (val) {
5035195618Srpaulo			case IEEE80211_HWMP_ROOTMODE_DISABLED:
5036195618Srpaulo				LINE_CHECK("hwmprootmode DISABLED");
5037195618Srpaulo				break;
5038195618Srpaulo			case IEEE80211_HWMP_ROOTMODE_NORMAL:
5039195618Srpaulo				LINE_CHECK("hwmprootmode NORMAL");
5040195618Srpaulo				break;
5041195618Srpaulo			case IEEE80211_HWMP_ROOTMODE_PROACTIVE:
5042195618Srpaulo				LINE_CHECK("hwmprootmode PROACTIVE");
5043195618Srpaulo				break;
5044195618Srpaulo			case IEEE80211_HWMP_ROOTMODE_RANN:
5045195618Srpaulo				LINE_CHECK("hwmprootmode RANN");
5046195618Srpaulo				break;
5047195618Srpaulo			default:
5048195618Srpaulo				LINE_CHECK("hwmprootmode UNKNOWN(%d)", val);
5049195618Srpaulo				break;
5050195618Srpaulo			}
5051195618Srpaulo		}
5052195618Srpaulo		if (get80211val(s, IEEE80211_IOC_HWMP_MAXHOPS, &val) != -1) {
5053195618Srpaulo			LINE_CHECK("hwmpmaxhops %u", val);
5054195618Srpaulo		}
5055195618Srpaulo	}
5056195618Srpaulo
5057173275Ssam	LINE_BREAK();
5058173275Ssam}
5059138593Ssam
5060173275Ssamstatic int
5061173275Ssamget80211(int s, int type, void *data, int len)
5062173275Ssam{
5063138593Ssam
5064291470Sadrian	return (lib80211_get80211(s, name, type, data, len));
5065173275Ssam}
5066138593Ssam
5067173275Ssamstatic int
5068173275Ssamget80211len(int s, int type, void *data, int len, int *plen)
5069173275Ssam{
5070138593Ssam
5071291470Sadrian	return (lib80211_get80211len(s, name, type, data, len, plen));
5072173275Ssam}
5073138593Ssam
5074173275Ssamstatic int
5075173275Ssamget80211val(int s, int type, int *val)
5076173275Ssam{
507777218Sphk
5078291470Sadrian	return (lib80211_get80211val(s, name, type, val));
507977218Sphk}
508077218Sphk
508177218Sphkstatic void
5082170531Ssamset80211(int s, int type, int val, int len, void *data)
508377218Sphk{
5084291470Sadrian	int ret;
508577218Sphk
5086291470Sadrian	ret = lib80211_set80211(s, name, type, val, len, data);
5087291470Sadrian	if (ret < 0)
508877218Sphk		err(1, "SIOCS80211");
508977218Sphk}
509077218Sphk
509177218Sphkstatic const char *
509277218Sphkget_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
509377218Sphk{
509477218Sphk	int len;
509577218Sphk	int hexstr;
509677218Sphk	u_int8_t *p;
509777218Sphk
509877218Sphk	len = *lenp;
509977218Sphk	p = buf;
510077218Sphk	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
510177218Sphk	if (hexstr)
510277218Sphk		val += 2;
510377218Sphk	for (;;) {
510477218Sphk		if (*val == '\0')
510577218Sphk			break;
510677218Sphk		if (sep != NULL && strchr(sep, *val) != NULL) {
510777218Sphk			val++;
510877218Sphk			break;
510977218Sphk		}
511077218Sphk		if (hexstr) {
5111127831Sphk			if (!isxdigit((u_char)val[0])) {
511277218Sphk				warnx("bad hexadecimal digits");
511377218Sphk				return NULL;
511477218Sphk			}
5115127831Sphk			if (!isxdigit((u_char)val[1])) {
5116127831Sphk				warnx("odd count hexadecimal digits");
5117127831Sphk				return NULL;
5118127831Sphk			}
511977218Sphk		}
5120127831Sphk		if (p >= buf + len) {
512177218Sphk			if (hexstr)
512277218Sphk				warnx("hexadecimal digits too long");
512377218Sphk			else
5124127831Sphk				warnx("string too long");
512577218Sphk			return NULL;
512677218Sphk		}
512777218Sphk		if (hexstr) {
512877218Sphk#define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
512977218Sphk			*p++ = (tohex((u_char)val[0]) << 4) |
513077218Sphk			    tohex((u_char)val[1]);
513177218Sphk#undef tohex
513277218Sphk			val += 2;
513377218Sphk		} else
513477218Sphk			*p++ = *val++;
513577218Sphk	}
513677218Sphk	len = p - buf;
513777218Sphk	/* The string "-" is treated as the empty string. */
5138165045Ssam	if (!hexstr && len == 1 && buf[0] == '-') {
513977218Sphk		len = 0;
5140165045Ssam		memset(buf, 0, *lenp);
5141165045Ssam	} else if (len < *lenp)
514277218Sphk		memset(p, 0, *lenp - len);
514377218Sphk	*lenp = len;
514477218Sphk	return val;
514577218Sphk}
514677218Sphk
514777218Sphkstatic void
514877218Sphkprint_string(const u_int8_t *buf, int len)
514977218Sphk{
515077218Sphk	int i;
515177218Sphk	int hasspc;
5152336344Skevans	int utf8;
515377218Sphk
515477218Sphk	i = 0;
515577218Sphk	hasspc = 0;
5156336344Skevans
5157336344Skevans	setlocale(LC_CTYPE, "");
5158336344Skevans	utf8 = strncmp("UTF-8", nl_langinfo(CODESET), 5) == 0;
5159336344Skevans
516091454Sbrooks	for (; i < len; i++) {
5161336344Skevans		if (!isprint(buf[i]) && buf[i] != '\0' && !utf8)
516277218Sphk			break;
516377218Sphk		if (isspace(buf[i]))
516477218Sphk			hasspc++;
516577218Sphk	}
5166336344Skevans	if (i == len || utf8) {
516777218Sphk		if (hasspc || len == 0 || buf[0] == '\0')
516877218Sphk			printf("\"%.*s\"", len, buf);
516977218Sphk		else
517077218Sphk			printf("%.*s", len, buf);
517177218Sphk	} else {
517277218Sphk		printf("0x");
517377218Sphk		for (i = 0; i < len; i++)
517477218Sphk			printf("%02x", buf[i]);
517577218Sphk	}
517677218Sphk}
517777218Sphk
5178300738Savosstatic void
5179300738Savossetdefregdomain(int s)
5180300738Savos{
5181300738Savos	struct regdata *rdp = getregdata();
5182300738Savos	const struct regdomain *rd;
5183300738Savos
5184300738Savos	/* Check if regdomain/country was already set by a previous call. */
5185300738Savos	/* XXX is it possible? */
5186300738Savos	if (regdomain.regdomain != 0 ||
5187300738Savos	    regdomain.country != CTRY_DEFAULT)
5188300738Savos		return;
5189300738Savos
5190300738Savos	getregdomain(s);
5191300738Savos
5192300738Savos	/* Check if it was already set by the driver. */
5193300738Savos	if (regdomain.regdomain != 0 ||
5194300738Savos	    regdomain.country != CTRY_DEFAULT)
5195300738Savos		return;
5196300738Savos
5197300738Savos	/* Set FCC/US as default. */
5198300738Savos	rd = lib80211_regdomain_findbysku(rdp, SKU_FCC);
5199300738Savos	if (rd == NULL)
5200300738Savos		errx(1, "FCC regdomain was not found");
5201300738Savos
5202300738Savos	regdomain.regdomain = rd->sku;
5203300738Savos	if (rd->cc != NULL)
5204300738Savos		defaultcountry(rd);
5205300738Savos
5206300738Savos	/* Send changes to net80211. */
5207300738Savos	setregdomain_cb(s, &regdomain);
5208300738Savos
5209300738Savos	/* Cleanup (so it can be overriden by subsequent parameters). */
5210300738Savos	regdomain.regdomain = 0;
5211300738Savos	regdomain.country = CTRY_DEFAULT;
5212300738Savos	regdomain.isocc[0] = 0;
5213300738Savos	regdomain.isocc[1] = 0;
5214300738Savos}
5215300738Savos
5216178354Ssam/*
5217178354Ssam * Virtual AP cloning support.
5218178354Ssam */
5219178354Ssamstatic struct ieee80211_clone_params params = {
5220178354Ssam	.icp_opmode	= IEEE80211_M_STA,	/* default to station mode */
5221178354Ssam};
5222178354Ssam
5223178354Ssamstatic void
5224178354Ssamwlan_create(int s, struct ifreq *ifr)
5225178354Ssam{
5226178354Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
5227301651Savos	char orig_name[IFNAMSIZ];
5228178354Ssam
5229178354Ssam	if (params.icp_parent[0] == '\0')
5230189096Srpaulo		errx(1, "must specify a parent device (wlandev) when creating "
5231189096Srpaulo		    "a wlan device");
5232178354Ssam	if (params.icp_opmode == IEEE80211_M_WDS &&
5233178354Ssam	    memcmp(params.icp_bssid, zerobssid, sizeof(zerobssid)) == 0)
5234178354Ssam		errx(1, "no bssid specified for WDS (use wlanbssid)");
5235178354Ssam	ifr->ifr_data = (caddr_t) &params;
5236368298Shselasky	ioctl_ifcreate(s, ifr);
5237300738Savos
5238301651Savos	/* XXX preserve original name for ifclonecreate(). */
5239301651Savos	strlcpy(orig_name, name, sizeof(orig_name));
5240301651Savos	strlcpy(name, ifr->ifr_name, sizeof(name));
5241301651Savos
5242300738Savos	setdefregdomain(s);
5243301651Savos
5244301651Savos	strlcpy(name, orig_name, sizeof(name));
5245178354Ssam}
5246178354Ssam
5247178354Ssamstatic
5248178354SsamDECL_CMD_FUNC(set80211clone_wlandev, arg, d)
5249178354Ssam{
5250178354Ssam	strlcpy(params.icp_parent, arg, IFNAMSIZ);
5251178354Ssam}
5252178354Ssam
5253178354Ssamstatic
5254178354SsamDECL_CMD_FUNC(set80211clone_wlanbssid, arg, d)
5255178354Ssam{
5256178354Ssam	const struct ether_addr *ea;
5257178354Ssam
5258178354Ssam	ea = ether_aton(arg);
5259178354Ssam	if (ea == NULL)
5260178354Ssam		errx(1, "%s: cannot parse bssid", arg);
5261178354Ssam	memcpy(params.icp_bssid, ea->octet, IEEE80211_ADDR_LEN);
5262178354Ssam}
5263178354Ssam
5264178354Ssamstatic
5265178354SsamDECL_CMD_FUNC(set80211clone_wlanaddr, arg, d)
5266178354Ssam{
5267178354Ssam	const struct ether_addr *ea;
5268178354Ssam
5269178354Ssam	ea = ether_aton(arg);
5270178354Ssam	if (ea == NULL)
5271207320Srpaulo		errx(1, "%s: cannot parse address", arg);
5272178354Ssam	memcpy(params.icp_macaddr, ea->octet, IEEE80211_ADDR_LEN);
5273178354Ssam	params.icp_flags |= IEEE80211_CLONE_MACADDR;
5274178354Ssam}
5275178354Ssam
5276178354Ssamstatic
5277178354SsamDECL_CMD_FUNC(set80211clone_wlanmode, arg, d)
5278178354Ssam{
5279178354Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
5280178354Ssam	if (iseq(arg, "sta"))
5281178354Ssam		params.icp_opmode = IEEE80211_M_STA;
5282178354Ssam	else if (iseq(arg, "ahdemo") || iseq(arg, "adhoc-demo"))
5283178354Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
5284178354Ssam	else if (iseq(arg, "ibss") || iseq(arg, "adhoc"))
5285178354Ssam		params.icp_opmode = IEEE80211_M_IBSS;
5286178354Ssam	else if (iseq(arg, "ap") || iseq(arg, "host"))
5287178354Ssam		params.icp_opmode = IEEE80211_M_HOSTAP;
5288178354Ssam	else if (iseq(arg, "wds"))
5289178354Ssam		params.icp_opmode = IEEE80211_M_WDS;
5290178354Ssam	else if (iseq(arg, "monitor"))
5291178354Ssam		params.icp_opmode = IEEE80211_M_MONITOR;
5292186904Ssam	else if (iseq(arg, "tdma")) {
5293186904Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
5294186904Ssam		params.icp_flags |= IEEE80211_CLONE_TDMA;
5295195618Srpaulo	} else if (iseq(arg, "mesh") || iseq(arg, "mp")) /* mesh point */
5296195618Srpaulo		params.icp_opmode = IEEE80211_M_MBSS;
5297195618Srpaulo	else
5298178354Ssam		errx(1, "Don't know to create %s for %s", arg, name);
5299178354Ssam#undef iseq
5300178354Ssam}
5301178354Ssam
5302178354Ssamstatic void
5303178354Ssamset80211clone_beacons(const char *val, int d, int s, const struct afswtch *rafp)
5304178354Ssam{
5305178354Ssam	/* NB: inverted sense */
5306178354Ssam	if (d)
5307178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_NOBEACONS;
5308178354Ssam	else
5309178354Ssam		params.icp_flags |= IEEE80211_CLONE_NOBEACONS;
5310178354Ssam}
5311178354Ssam
5312178354Ssamstatic void
5313178354Ssamset80211clone_bssid(const char *val, int d, int s, const struct afswtch *rafp)
5314178354Ssam{
5315178354Ssam	if (d)
5316178354Ssam		params.icp_flags |= IEEE80211_CLONE_BSSID;
5317178354Ssam	else
5318178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_BSSID;
5319178354Ssam}
5320178354Ssam
5321178354Ssamstatic void
5322178354Ssamset80211clone_wdslegacy(const char *val, int d, int s, const struct afswtch *rafp)
5323178354Ssam{
5324178354Ssam	if (d)
5325178354Ssam		params.icp_flags |= IEEE80211_CLONE_WDSLEGACY;
5326178354Ssam	else
5327178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_WDSLEGACY;
5328178354Ssam}
5329178354Ssam
5330138593Ssamstatic struct cmd ieee80211_cmds[] = {
5331138593Ssam	DEF_CMD_ARG("ssid",		set80211ssid),
5332138593Ssam	DEF_CMD_ARG("nwid",		set80211ssid),
5333195618Srpaulo	DEF_CMD_ARG("meshid",		set80211meshid),
5334138593Ssam	DEF_CMD_ARG("stationname",	set80211stationname),
5335138593Ssam	DEF_CMD_ARG("station",		set80211stationname),	/* BSD/OS */
5336138593Ssam	DEF_CMD_ARG("channel",		set80211channel),
5337138593Ssam	DEF_CMD_ARG("authmode",		set80211authmode),
5338138593Ssam	DEF_CMD_ARG("powersavemode",	set80211powersavemode),
5339138593Ssam	DEF_CMD("powersave",	1,	set80211powersave),
5340138593Ssam	DEF_CMD("-powersave",	0,	set80211powersave),
5341138593Ssam	DEF_CMD_ARG("powersavesleep", 	set80211powersavesleep),
5342138593Ssam	DEF_CMD_ARG("wepmode",		set80211wepmode),
5343138593Ssam	DEF_CMD("wep",		1,	set80211wep),
5344138593Ssam	DEF_CMD("-wep",		0,	set80211wep),
5345139493Ssam	DEF_CMD_ARG("deftxkey",		set80211weptxkey),
5346138593Ssam	DEF_CMD_ARG("weptxkey",		set80211weptxkey),
5347138593Ssam	DEF_CMD_ARG("wepkey",		set80211wepkey),
5348138593Ssam	DEF_CMD_ARG("nwkey",		set80211nwkey),		/* NetBSD */
5349138593Ssam	DEF_CMD("-nwkey",	0,	set80211wep),		/* NetBSD */
5350138593Ssam	DEF_CMD_ARG("rtsthreshold",	set80211rtsthreshold),
5351138593Ssam	DEF_CMD_ARG("protmode",		set80211protmode),
5352138593Ssam	DEF_CMD_ARG("txpower",		set80211txpower),
5353138593Ssam	DEF_CMD_ARG("roaming",		set80211roaming),
5354138593Ssam	DEF_CMD("wme",		1,	set80211wme),
5355138593Ssam	DEF_CMD("-wme",		0,	set80211wme),
5356178354Ssam	DEF_CMD("wmm",		1,	set80211wme),
5357178354Ssam	DEF_CMD("-wmm",		0,	set80211wme),
5358138593Ssam	DEF_CMD("hidessid",	1,	set80211hidessid),
5359138593Ssam	DEF_CMD("-hidessid",	0,	set80211hidessid),
5360138593Ssam	DEF_CMD("apbridge",	1,	set80211apbridge),
5361138593Ssam	DEF_CMD("-apbridge",	0,	set80211apbridge),
5362138593Ssam	DEF_CMD_ARG("chanlist",		set80211chanlist),
5363138593Ssam	DEF_CMD_ARG("bssid",		set80211bssid),
5364138593Ssam	DEF_CMD_ARG("ap",		set80211bssid),
5365138593Ssam	DEF_CMD("scan",	0,		set80211scan),
5366138593Ssam	DEF_CMD_ARG("list",		set80211list),
5367138593Ssam	DEF_CMD_ARG2("cwmin",		set80211cwmin),
5368138593Ssam	DEF_CMD_ARG2("cwmax",		set80211cwmax),
5369138593Ssam	DEF_CMD_ARG2("aifs",		set80211aifs),
5370138593Ssam	DEF_CMD_ARG2("txoplimit",	set80211txoplimit),
5371148621Ssam	DEF_CMD_ARG("acm",		set80211acm),
5372148621Ssam	DEF_CMD_ARG("-acm",		set80211noacm),
5373148621Ssam	DEF_CMD_ARG("ack",		set80211ackpolicy),
5374148621Ssam	DEF_CMD_ARG("-ack",		set80211noackpolicy),
5375138593Ssam	DEF_CMD_ARG2("bss:cwmin",	set80211bsscwmin),
5376138593Ssam	DEF_CMD_ARG2("bss:cwmax",	set80211bsscwmax),
5377138593Ssam	DEF_CMD_ARG2("bss:aifs",	set80211bssaifs),
5378138593Ssam	DEF_CMD_ARG2("bss:txoplimit",	set80211bsstxoplimit),
5379138593Ssam	DEF_CMD_ARG("dtimperiod",	set80211dtimperiod),
5380138593Ssam	DEF_CMD_ARG("bintval",		set80211bintval),
5381138593Ssam	DEF_CMD("mac:open",	IEEE80211_MACCMD_POLICY_OPEN,	set80211maccmd),
5382138593Ssam	DEF_CMD("mac:allow",	IEEE80211_MACCMD_POLICY_ALLOW,	set80211maccmd),
5383138593Ssam	DEF_CMD("mac:deny",	IEEE80211_MACCMD_POLICY_DENY,	set80211maccmd),
5384178354Ssam	DEF_CMD("mac:radius",	IEEE80211_MACCMD_POLICY_RADIUS,	set80211maccmd),
5385138593Ssam	DEF_CMD("mac:flush",	IEEE80211_MACCMD_FLUSH,		set80211maccmd),
5386138593Ssam	DEF_CMD("mac:detach",	IEEE80211_MACCMD_DETACH,	set80211maccmd),
5387138593Ssam	DEF_CMD_ARG("mac:add",		set80211addmac),
5388138593Ssam	DEF_CMD_ARG("mac:del",		set80211delmac),
5389138593Ssam	DEF_CMD_ARG("mac:kick",		set80211kickmac),
5390147795Ssam	DEF_CMD("pureg",	1,	set80211pureg),
5391147795Ssam	DEF_CMD("-pureg",	0,	set80211pureg),
5392170531Ssam	DEF_CMD("ff",		1,	set80211fastframes),
5393170531Ssam	DEF_CMD("-ff",		0,	set80211fastframes),
5394170531Ssam	DEF_CMD("dturbo",	1,	set80211dturbo),
5395170531Ssam	DEF_CMD("-dturbo",	0,	set80211dturbo),
5396170531Ssam	DEF_CMD("bgscan",	1,	set80211bgscan),
5397170531Ssam	DEF_CMD("-bgscan",	0,	set80211bgscan),
5398170531Ssam	DEF_CMD_ARG("bgscanidle",	set80211bgscanidle),
5399170531Ssam	DEF_CMD_ARG("bgscanintvl",	set80211bgscanintvl),
5400170531Ssam	DEF_CMD_ARG("scanvalid",	set80211scanvalid),
5401227336Sadrian	DEF_CMD("quiet",        1,      set80211quiet),
5402227336Sadrian	DEF_CMD("-quiet",       0,      set80211quiet),
5403227336Sadrian	DEF_CMD_ARG("quiet_count",      set80211quietcount),
5404227336Sadrian	DEF_CMD_ARG("quiet_period",     set80211quietperiod),
5405227336Sadrian	DEF_CMD_ARG("quiet_dur",        set80211quietduration),
5406227336Sadrian	DEF_CMD_ARG("quiet_offset",     set80211quietoffset),
5407178354Ssam	DEF_CMD_ARG("roam:rssi",	set80211roamrssi),
5408178354Ssam	DEF_CMD_ARG("roam:rate",	set80211roamrate),
5409153354Ssam	DEF_CMD_ARG("mcastrate",	set80211mcastrate),
5410178354Ssam	DEF_CMD_ARG("ucastrate",	set80211ucastrate),
5411178354Ssam	DEF_CMD_ARG("mgtrate",		set80211mgtrate),
5412178354Ssam	DEF_CMD_ARG("mgmtrate",		set80211mgtrate),
5413178354Ssam	DEF_CMD_ARG("maxretry",		set80211maxretry),
5414148416Ssam	DEF_CMD_ARG("fragthreshold",	set80211fragthreshold),
5415153422Ssam	DEF_CMD("burst",	1,	set80211burst),
5416153422Ssam	DEF_CMD("-burst",	0,	set80211burst),
5417160687Ssam	DEF_CMD_ARG("bmiss",		set80211bmissthreshold),
5418160687Ssam	DEF_CMD_ARG("bmissthreshold",	set80211bmissthreshold),
5419173275Ssam	DEF_CMD("shortgi",	1,	set80211shortgi),
5420173275Ssam	DEF_CMD("-shortgi",	0,	set80211shortgi),
5421173275Ssam	DEF_CMD("ampdurx",	2,	set80211ampdu),
5422173275Ssam	DEF_CMD("-ampdurx",	-2,	set80211ampdu),
5423173275Ssam	DEF_CMD("ampdutx",	1,	set80211ampdu),
5424173275Ssam	DEF_CMD("-ampdutx",	-1,	set80211ampdu),
5425173275Ssam	DEF_CMD("ampdu",	3,	set80211ampdu),		/* NB: tx+rx */
5426173275Ssam	DEF_CMD("-ampdu",	-3,	set80211ampdu),
5427173275Ssam	DEF_CMD_ARG("ampdulimit",	set80211ampdulimit),
5428173275Ssam	DEF_CMD_ARG("ampdudensity",	set80211ampdudensity),
5429173275Ssam	DEF_CMD("amsdurx",	2,	set80211amsdu),
5430173275Ssam	DEF_CMD("-amsdurx",	-2,	set80211amsdu),
5431173275Ssam	DEF_CMD("amsdutx",	1,	set80211amsdu),
5432173275Ssam	DEF_CMD("-amsdutx",	-1,	set80211amsdu),
5433173275Ssam	DEF_CMD("amsdu",	3,	set80211amsdu),		/* NB: tx+rx */
5434173275Ssam	DEF_CMD("-amsdu",	-3,	set80211amsdu),
5435173275Ssam	DEF_CMD_ARG("amsdulimit",	set80211amsdulimit),
5436298606Sadrian	DEF_CMD("stbcrx",	2,	set80211stbc),
5437298606Sadrian	DEF_CMD("-stbcrx",	-2,	set80211stbc),
5438298606Sadrian	DEF_CMD("stbctx",	1,	set80211stbc),
5439298606Sadrian	DEF_CMD("-stbctx",	-1,	set80211stbc),
5440298606Sadrian	DEF_CMD("stbc",		3,	set80211stbc),		/* NB: tx+rx */
5441312778Savos	DEF_CMD("-stbc",	-3,	set80211stbc),
5442173275Ssam	DEF_CMD("puren",	1,	set80211puren),
5443173275Ssam	DEF_CMD("-puren",	0,	set80211puren),
5444170531Ssam	DEF_CMD("doth",		1,	set80211doth),
5445170531Ssam	DEF_CMD("-doth",	0,	set80211doth),
5446178354Ssam	DEF_CMD("dfs",		1,	set80211dfs),
5447178354Ssam	DEF_CMD("-dfs",		0,	set80211dfs),
5448173275Ssam	DEF_CMD("htcompat",	1,	set80211htcompat),
5449173275Ssam	DEF_CMD("-htcompat",	0,	set80211htcompat),
5450178354Ssam	DEF_CMD("dwds",		1,	set80211dwds),
5451178354Ssam	DEF_CMD("-dwds",	0,	set80211dwds),
5452173275Ssam	DEF_CMD("inact",	1,	set80211inact),
5453173275Ssam	DEF_CMD("-inact",	0,	set80211inact),
5454178354Ssam	DEF_CMD("tsn",		1,	set80211tsn),
5455178354Ssam	DEF_CMD("-tsn",		0,	set80211tsn),
5456178354Ssam	DEF_CMD_ARG("regdomain",	set80211regdomain),
5457178354Ssam	DEF_CMD_ARG("country",		set80211country),
5458178354Ssam	DEF_CMD("indoor",	'I',	set80211location),
5459178354Ssam	DEF_CMD("-indoor",	'O',	set80211location),
5460178354Ssam	DEF_CMD("outdoor",	'O',	set80211location),
5461178354Ssam	DEF_CMD("-outdoor",	'I',	set80211location),
5462178354Ssam	DEF_CMD("anywhere",	' ',	set80211location),
5463178354Ssam	DEF_CMD("ecm",		1,	set80211ecm),
5464178354Ssam	DEF_CMD("-ecm",		0,	set80211ecm),
5465178354Ssam	DEF_CMD("dotd",		1,	set80211dotd),
5466178354Ssam	DEF_CMD("-dotd",	0,	set80211dotd),
5467173275Ssam	DEF_CMD_ARG("htprotmode",	set80211htprotmode),
5468173275Ssam	DEF_CMD("ht20",		1,	set80211htconf),
5469173275Ssam	DEF_CMD("-ht20",	0,	set80211htconf),
5470173275Ssam	DEF_CMD("ht40",		3,	set80211htconf),	/* NB: 20+40 */
5471173275Ssam	DEF_CMD("-ht40",	0,	set80211htconf),
5472173275Ssam	DEF_CMD("ht",		3,	set80211htconf),	/* NB: 20+40 */
5473173275Ssam	DEF_CMD("-ht",		0,	set80211htconf),
5474183261Ssam	DEF_CMD("rifs",		1,	set80211rifs),
5475183261Ssam	DEF_CMD("-rifs",	0,	set80211rifs),
5476183261Ssam	DEF_CMD("smps",		IEEE80211_HTCAP_SMPS_ENA,	set80211smps),
5477183261Ssam	DEF_CMD("smpsdyn",	IEEE80211_HTCAP_SMPS_DYNAMIC,	set80211smps),
5478183261Ssam	DEF_CMD("-smps",	IEEE80211_HTCAP_SMPS_OFF,	set80211smps),
5479178354Ssam	/* XXX for testing */
5480178354Ssam	DEF_CMD_ARG("chanswitch",	set80211chanswitch),
5481178354Ssam
5482186904Ssam	DEF_CMD_ARG("tdmaslot",		set80211tdmaslot),
5483186904Ssam	DEF_CMD_ARG("tdmaslotcnt",	set80211tdmaslotcnt),
5484186904Ssam	DEF_CMD_ARG("tdmaslotlen",	set80211tdmaslotlen),
5485186904Ssam	DEF_CMD_ARG("tdmabintval",	set80211tdmabintval),
5486186904Ssam
5487195618Srpaulo	DEF_CMD_ARG("meshttl",		set80211meshttl),
5488195618Srpaulo	DEF_CMD("meshforward",	1,	set80211meshforward),
5489195618Srpaulo	DEF_CMD("-meshforward",	0,	set80211meshforward),
5490234893Smonthadar	DEF_CMD("meshgate",	1,	set80211meshgate),
5491234893Smonthadar	DEF_CMD("-meshgate",	0,	set80211meshgate),
5492195618Srpaulo	DEF_CMD("meshpeering",	1,	set80211meshpeering),
5493195618Srpaulo	DEF_CMD("-meshpeering",	0,	set80211meshpeering),
5494195618Srpaulo	DEF_CMD_ARG("meshmetric",	set80211meshmetric),
5495195618Srpaulo	DEF_CMD_ARG("meshpath",		set80211meshpath),
5496195618Srpaulo	DEF_CMD("meshrt:flush",	IEEE80211_MESH_RTCMD_FLUSH,	set80211meshrtcmd),
5497195618Srpaulo	DEF_CMD_ARG("meshrt:add",	set80211addmeshrt),
5498195618Srpaulo	DEF_CMD_ARG("meshrt:del",	set80211delmeshrt),
5499195618Srpaulo	DEF_CMD_ARG("hwmprootmode",	set80211hwmprootmode),
5500195618Srpaulo	DEF_CMD_ARG("hwmpmaxhops",	set80211hwmpmaxhops),
5501195618Srpaulo
5502178354Ssam	/* vap cloning support */
5503178354Ssam	DEF_CLONE_CMD_ARG("wlanaddr",	set80211clone_wlanaddr),
5504178354Ssam	DEF_CLONE_CMD_ARG("wlanbssid",	set80211clone_wlanbssid),
5505178354Ssam	DEF_CLONE_CMD_ARG("wlandev",	set80211clone_wlandev),
5506178354Ssam	DEF_CLONE_CMD_ARG("wlanmode",	set80211clone_wlanmode),
5507178354Ssam	DEF_CLONE_CMD("beacons", 1,	set80211clone_beacons),
5508178354Ssam	DEF_CLONE_CMD("-beacons", 0,	set80211clone_beacons),
5509178354Ssam	DEF_CLONE_CMD("bssid",	1,	set80211clone_bssid),
5510178354Ssam	DEF_CLONE_CMD("-bssid",	0,	set80211clone_bssid),
5511178354Ssam	DEF_CLONE_CMD("wdslegacy", 1,	set80211clone_wdslegacy),
5512178354Ssam	DEF_CLONE_CMD("-wdslegacy", 0,	set80211clone_wdslegacy),
5513138593Ssam};
5514138593Ssamstatic struct afswtch af_ieee80211 = {
5515138593Ssam	.af_name	= "af_ieee80211",
5516138593Ssam	.af_af		= AF_UNSPEC,
5517139494Ssam	.af_other_status = ieee80211_status,
5518138593Ssam};
5519138593Ssam
5520138593Ssamstatic __constructor void
5521138593Ssamieee80211_ctor(void)
5522138593Ssam{
5523138593Ssam	int i;
5524138593Ssam
5525288305Sngie	for (i = 0; i < nitems(ieee80211_cmds);  i++)
5526138593Ssam		cmd_register(&ieee80211_cmds[i]);
5527138593Ssam	af_register(&af_ieee80211);
5528189096Srpaulo	clone_setdefcallback("wlan", wlan_create);
5529138593Ssam}
5530