ifieee80211.c revision 190456
177218Sphk/*
277218Sphk * Copyright 2001 The Aerospace Corporation.  All rights reserved.
377218Sphk *
477218Sphk * Redistribution and use in source and binary forms, with or without
577218Sphk * modification, are permitted provided that the following conditions
677218Sphk * are met:
777218Sphk * 1. Redistributions of source code must retain the above copyright
877218Sphk *    notice, this list of conditions and the following disclaimer.
977218Sphk * 2. Redistributions in binary form must reproduce the above copyright
1077218Sphk *    notice, this list of conditions and the following disclaimer in the
1177218Sphk *    documentation and/or other materials provided with the distribution.
1291454Sbrooks * 3. The name of The Aerospace Corporation may not be used to endorse or
1391454Sbrooks *    promote products derived from this software.
1477218Sphk *
1577218Sphk * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION ``AS IS'' AND
1677218Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1777218Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1877218Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE
1977218Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2077218Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2177218Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2277218Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2377218Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2477218Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2577218Sphk * SUCH DAMAGE.
2677218Sphk *
2777218Sphk * $FreeBSD: head/sbin/ifconfig/ifieee80211.c 190456 2009-03-26 21:29:38Z sam $
2877218Sphk */
2977218Sphk
3077218Sphk/*-
3177218Sphk * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
3277218Sphk * All rights reserved.
3377218Sphk *
3477218Sphk * This code is derived from software contributed to The NetBSD Foundation
3577218Sphk * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
3677218Sphk * NASA Ames Research Center.
3777218Sphk *
3877218Sphk * Redistribution and use in source and binary forms, with or without
3977218Sphk * modification, are permitted provided that the following conditions
4077218Sphk * are met:
4177218Sphk * 1. Redistributions of source code must retain the above copyright
4277218Sphk *    notice, this list of conditions and the following disclaimer.
4377218Sphk * 2. Redistributions in binary form must reproduce the above copyright
4477218Sphk *    notice, this list of conditions and the following disclaimer in the
4577218Sphk *    documentation and/or other materials provided with the distribution.
4677218Sphk * 3. All advertising materials mentioning features or use of this software
4777218Sphk *    must display the following acknowledgement:
4877218Sphk *	This product includes software developed by the NetBSD
4977218Sphk *	Foundation, Inc. and its contributors.
5077218Sphk * 4. Neither the name of The NetBSD Foundation nor the names of its
5177218Sphk *    contributors may be used to endorse or promote products derived
5277218Sphk *    from this software without specific prior written permission.
5377218Sphk *
5477218Sphk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
5577218Sphk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
5677218Sphk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5777218Sphk * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
5877218Sphk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5977218Sphk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
6077218Sphk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
6177218Sphk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
6277218Sphk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
6377218Sphk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
6477218Sphk * POSSIBILITY OF SUCH DAMAGE.
6577218Sphk */
6677218Sphk
6777218Sphk#include <sys/param.h>
6877218Sphk#include <sys/ioctl.h>
6977218Sphk#include <sys/socket.h>
7077218Sphk#include <sys/sysctl.h>
7177218Sphk#include <sys/time.h>
7277218Sphk
7377218Sphk#include <net/ethernet.h>
7477218Sphk#include <net/if.h>
7577218Sphk#include <net/if_dl.h>
7677218Sphk#include <net/if_types.h>
77138593Ssam#include <net/if_media.h>
7877218Sphk#include <net/route.h>
79138593Ssam
80116957Ssam#include <net80211/ieee80211_ioctl.h>
81190456Ssam#include <net80211/ieee80211_freebsd.h>
82190456Ssam#include <net80211/ieee80211_superg.h>
83190456Ssam#include <net80211/ieee80211_tdma.h>
8477218Sphk
85187801Ssam#include <assert.h>
8677218Sphk#include <ctype.h>
8777218Sphk#include <err.h>
8877218Sphk#include <errno.h>
8977218Sphk#include <fcntl.h>
90146873Sjhb#include <inttypes.h>
9177218Sphk#include <stdio.h>
9277218Sphk#include <stdlib.h>
9377218Sphk#include <string.h>
9477218Sphk#include <unistd.h>
95155931Ssam#include <stdarg.h>
96173275Ssam#include <stddef.h>		/* NB: for offsetof */
9777218Sphk
9877218Sphk#include "ifconfig.h"
99178354Ssam#include "regdomain.h"
10077218Sphk
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
107178354Ssam#define	IEEE80211_NODE_AUTH	0x0001		/* authorized for data */
108178354Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
109178354Ssam#define	IEEE80211_NODE_ERP	0x0004		/* ERP enabled */
110178354Ssam#define	IEEE80211_NODE_PWR_MGT	0x0010		/* power save mode enabled */
111178354Ssam#define	IEEE80211_NODE_HT	0x0040		/* HT enabled */
112178354Ssam#define	IEEE80211_NODE_HTCOMPAT	0x0080		/* HT setup w/ vendor OUI's */
113178354Ssam#define	IEEE80211_NODE_WPS	0x0100		/* WPS association */
114178354Ssam#define	IEEE80211_NODE_TSN	0x0200		/* TSN association */
115183261Ssam#define	IEEE80211_NODE_AMPDU_RX	0x0400		/* AMPDU rx enabled */
116183261Ssam#define	IEEE80211_NODE_AMPDU_TX	0x0800		/* AMPDU tx enabled */
117183261Ssam#define	IEEE80211_NODE_MIMO_PS	0x1000		/* MIMO power save enabled */
118183261Ssam#define	IEEE80211_NODE_MIMO_RTS	0x2000		/* send RTS in MIMO PS */
119183261Ssam#define	IEEE80211_NODE_RIFS	0x4000		/* RIFS enabled */
120178354Ssam#endif
121178354Ssam
122187801Ssam#define	MAXCHAN	1536		/* max 1.5K channels */
123187801Ssam
124173275Ssam#define	MAXCOL	78
125173275Ssamstatic	int col;
126173275Ssamstatic	char spacer;
127173275Ssam
128173275Ssamstatic void LINE_INIT(char c);
129173275Ssamstatic void LINE_BREAK(void);
130173275Ssamstatic void LINE_CHECK(const char *fmt, ...);
131173275Ssam
132188784Ssamstatic const char *modename[IEEE80211_MODE_MAX] = {
133188784Ssam	[IEEE80211_MODE_AUTO]	  = "auto",
134188784Ssam	[IEEE80211_MODE_11A]	  = "11a",
135188784Ssam	[IEEE80211_MODE_11B]	  = "11b",
136188784Ssam	[IEEE80211_MODE_11G]	  = "11g",
137188784Ssam	[IEEE80211_MODE_FH]	  = "fh",
138188784Ssam	[IEEE80211_MODE_TURBO_A]  = "turboA",
139188784Ssam	[IEEE80211_MODE_TURBO_G]  = "turboG",
140188784Ssam	[IEEE80211_MODE_STURBO_A] = "sturbo",
141188784Ssam	[IEEE80211_MODE_11NA]	  = "11na",
142188784Ssam	[IEEE80211_MODE_11NG]	  = "11ng",
143188784Ssam	[IEEE80211_MODE_HALF]	  = "half",
144188784Ssam	[IEEE80211_MODE_QUARTER]  = "quarter"
145173275Ssam};
146173275Ssam
147178354Ssamstatic void set80211(int s, int type, int val, int len, void *data);
148173275Ssamstatic int get80211(int s, int type, void *data, int len);
149173275Ssamstatic int get80211len(int s, int type, void *data, int len, int *plen);
150173275Ssamstatic int get80211val(int s, int type, int *val);
15177218Sphkstatic const char *get_string(const char *val, const char *sep,
15277218Sphk    u_int8_t *buf, int *lenp);
15377218Sphkstatic void print_string(const u_int8_t *buf, int len);
154178354Ssamstatic void print_regdomain(const struct ieee80211_regdomain *, int);
155178354Ssamstatic void print_channels(int, const struct ieee80211req_chaninfo *,
156178354Ssam    int allchans, int verbose);
157178354Ssamstatic void regdomain_makechannels(struct ieee80211_regdomain_req *,
158178354Ssam    const struct ieee80211_devcaps_req *);
15977218Sphk
160187801Ssamstatic struct ieee80211req_chaninfo *chaninfo;
161178354Ssamstatic struct ieee80211_regdomain regdomain;
162178354Ssamstatic int gotregdomain = 0;
163178354Ssamstatic struct ieee80211_roamparams_req roamparams;
164178354Ssamstatic int gotroam = 0;
165178354Ssamstatic struct ieee80211_txparams_req txparams;
166178354Ssamstatic int gottxparams = 0;
167173275Ssamstatic struct ieee80211_channel curchan;
168173275Ssamstatic int gotcurchan = 0;
169178354Ssamstatic struct ifmediareq *ifmr;
170173275Ssamstatic int htconf = 0;
171173275Ssamstatic	int gothtconf = 0;
172170531Ssam
173173275Ssamstatic void
174173275Ssamgethtconf(int s)
175173275Ssam{
176173275Ssam	if (gothtconf)
177173275Ssam		return;
178173275Ssam	if (get80211val(s, IEEE80211_IOC_HTCONF, &htconf) < 0)
179173275Ssam		warn("unable to get HT configuration information");
180173275Ssam	gothtconf = 1;
181173275Ssam}
182173275Ssam
183170531Ssam/*
184170531Ssam * Collect channel info from the kernel.  We use this (mostly)
185170531Ssam * to handle mapping between frequency and IEEE channel number.
186170531Ssam */
187170531Ssamstatic void
188170531Ssamgetchaninfo(int s)
189170531Ssam{
190187801Ssam	if (chaninfo != NULL)
191170531Ssam		return;
192187801Ssam	chaninfo = malloc(IEEE80211_CHANINFO_SIZE(MAXCHAN));
193187801Ssam	if (chaninfo == NULL)
194187801Ssam		errx(1, "no space for channel list");
195187801Ssam	if (get80211(s, IEEE80211_IOC_CHANINFO, chaninfo,
196187801Ssam	    IEEE80211_CHANINFO_SIZE(MAXCHAN)) < 0)
197187801Ssam		err(1, "unable to get channel information");
198170531Ssam	ifmr = ifmedia_getstate(s);
199173275Ssam	gethtconf(s);
200170531Ssam}
201170531Ssam
202178354Ssamstatic struct regdata *
203178354Ssamgetregdata(void)
204178354Ssam{
205178354Ssam	static struct regdata *rdp = NULL;
206178354Ssam	if (rdp == NULL) {
207178354Ssam		rdp = lib80211_alloc_regdata();
208178354Ssam		if (rdp == NULL)
209181198Ssam			errx(-1, "missing or corrupted regdomain database");
210178354Ssam	}
211178354Ssam	return rdp;
212178354Ssam}
213178354Ssam
214170531Ssam/*
215170531Ssam * Given the channel at index i with attributes from,
216170531Ssam * check if there is a channel with attributes to in
217170531Ssam * the channel table.  With suitable attributes this
218170531Ssam * allows the caller to look for promotion; e.g. from
219170531Ssam * 11b > 11g.
220170531Ssam */
221138593Ssamstatic int
222170531Ssamcanpromote(int i, int from, int to)
223170531Ssam{
224187801Ssam	const struct ieee80211_channel *fc = &chaninfo->ic_chans[i];
225170531Ssam	int j;
226170531Ssam
227170531Ssam	if ((fc->ic_flags & from) != from)
228170531Ssam		return i;
229170531Ssam	/* NB: quick check exploiting ordering of chans w/ same frequency */
230187801Ssam	if (i+1 < chaninfo->ic_nchans &&
231187801Ssam	    chaninfo->ic_chans[i+1].ic_freq == fc->ic_freq &&
232187801Ssam	    (chaninfo->ic_chans[i+1].ic_flags & to) == to)
233170531Ssam		return i+1;
234170531Ssam	/* brute force search in case channel list is not ordered */
235187801Ssam	for (j = 0; j < chaninfo->ic_nchans; j++) {
236187801Ssam		const struct ieee80211_channel *tc = &chaninfo->ic_chans[j];
237170531Ssam		if (j != i &&
238170531Ssam		    tc->ic_freq == fc->ic_freq && (tc->ic_flags & to) == to)
239170531Ssam		return j;
240170531Ssam	}
241170531Ssam	return i;
242170531Ssam}
243170531Ssam
244170531Ssam/*
245170531Ssam * Handle channel promotion.  When a channel is specified with
246170531Ssam * only a frequency we want to promote it to the ``best'' channel
247170531Ssam * available.  The channel list has separate entries for 11b, 11g,
248170531Ssam * 11a, and 11n[ga] channels so specifying a frequency w/o any
249170531Ssam * attributes requires we upgrade, e.g. from 11b -> 11g.  This
250170531Ssam * gets complicated when the channel is specified on the same
251170531Ssam * command line with a media request that constrains the available
252170531Ssam * channe list (e.g. mode 11a); we want to honor that to avoid
253170531Ssam * confusing behaviour.
254170531Ssam */
255170531Ssamstatic int
256170531Ssampromote(int i)
257170531Ssam{
258170531Ssam	/*
259170531Ssam	 * Query the current mode of the interface in case it's
260170531Ssam	 * constrained (e.g. to 11a).  We must do this carefully
261170531Ssam	 * as there may be a pending ifmedia request in which case
262170531Ssam	 * asking the kernel will give us the wrong answer.  This
263170531Ssam	 * is an unfortunate side-effect of the way ifconfig is
264170531Ssam	 * structure for modularity (yech).
265170531Ssam	 *
266170531Ssam	 * NB: ifmr is actually setup in getchaninfo (above); we
267170531Ssam	 *     assume it's called coincident with to this call so
268170531Ssam	 *     we have a ``current setting''; otherwise we must pass
269170531Ssam	 *     the socket descriptor down to here so we can make
270170531Ssam	 *     the ifmedia_getstate call ourselves.
271170531Ssam	 */
272170531Ssam	int chanmode = ifmr != NULL ? IFM_MODE(ifmr->ifm_current) : IFM_AUTO;
273170531Ssam
274170531Ssam	/* when ambiguous promote to ``best'' */
275170531Ssam	/* NB: we abitrarily pick HT40+ over HT40- */
276170531Ssam	if (chanmode != IFM_IEEE80211_11B)
277170531Ssam		i = canpromote(i, IEEE80211_CHAN_B, IEEE80211_CHAN_G);
278173275Ssam	if (chanmode != IFM_IEEE80211_11G && (htconf & 1)) {
279170531Ssam		i = canpromote(i, IEEE80211_CHAN_G,
280170531Ssam			IEEE80211_CHAN_G | IEEE80211_CHAN_HT20);
281173275Ssam		if (htconf & 2) {
282173275Ssam			i = canpromote(i, IEEE80211_CHAN_G,
283173275Ssam				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D);
284173275Ssam			i = canpromote(i, IEEE80211_CHAN_G,
285173275Ssam				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U);
286173275Ssam		}
287170531Ssam	}
288173275Ssam	if (chanmode != IFM_IEEE80211_11A && (htconf & 1)) {
289170531Ssam		i = canpromote(i, IEEE80211_CHAN_A,
290170531Ssam			IEEE80211_CHAN_A | IEEE80211_CHAN_HT20);
291173275Ssam		if (htconf & 2) {
292173275Ssam			i = canpromote(i, IEEE80211_CHAN_A,
293173275Ssam				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D);
294173275Ssam			i = canpromote(i, IEEE80211_CHAN_A,
295173275Ssam				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U);
296173275Ssam		}
297170531Ssam	}
298170531Ssam	return i;
299170531Ssam}
300170531Ssam
301170531Ssamstatic void
302170531Ssammapfreq(struct ieee80211_channel *chan, int freq, int flags)
303170531Ssam{
304170531Ssam	int i;
305170531Ssam
306187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
307187801Ssam		const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
308170531Ssam
309170531Ssam		if (c->ic_freq == freq && (c->ic_flags & flags) == flags) {
310170531Ssam			if (flags == 0) {
311170531Ssam				/* when ambiguous promote to ``best'' */
312187801Ssam				c = &chaninfo->ic_chans[promote(i)];
313170531Ssam			}
314170531Ssam			*chan = *c;
315170531Ssam			return;
316170531Ssam		}
317170531Ssam	}
318170531Ssam	errx(1, "unknown/undefined frequency %u/0x%x", freq, flags);
319170531Ssam}
320170531Ssam
321170531Ssamstatic void
322170531Ssammapchan(struct ieee80211_channel *chan, int ieee, int flags)
323170531Ssam{
324170531Ssam	int i;
325170531Ssam
326187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
327187801Ssam		const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
328170531Ssam
329170531Ssam		if (c->ic_ieee == ieee && (c->ic_flags & flags) == flags) {
330170531Ssam			if (flags == 0) {
331170531Ssam				/* when ambiguous promote to ``best'' */
332187801Ssam				c = &chaninfo->ic_chans[promote(i)];
333170531Ssam			}
334170531Ssam			*chan = *c;
335170531Ssam			return;
336170531Ssam		}
337170531Ssam	}
338173275Ssam	errx(1, "unknown/undefined channel number %d flags 0x%x", ieee, flags);
339170531Ssam}
340170531Ssam
341173275Ssamstatic const struct ieee80211_channel *
342173275Ssamgetcurchan(int s)
343173275Ssam{
344173275Ssam	if (gotcurchan)
345173275Ssam		return &curchan;
346173275Ssam	if (get80211(s, IEEE80211_IOC_CURCHAN, &curchan, sizeof(curchan)) < 0) {
347173275Ssam		int val;
348173275Ssam		/* fall back to legacy ioctl */
349173275Ssam		if (get80211val(s, IEEE80211_IOC_CHANNEL, &val) < 0)
350187801Ssam			err(-1, "cannot figure out current channel");
351173275Ssam		getchaninfo(s);
352173275Ssam		mapchan(&curchan, val, 0);
353173275Ssam	}
354173275Ssam	gotcurchan = 1;
355173275Ssam	return &curchan;
356173275Ssam}
357173275Ssam
358178354Ssamstatic enum ieee80211_phymode
359178354Ssamchan2mode(const struct ieee80211_channel *c)
360178354Ssam{
361178354Ssam	if (IEEE80211_IS_CHAN_HTA(c))
362178354Ssam		return IEEE80211_MODE_11NA;
363178354Ssam	if (IEEE80211_IS_CHAN_HTG(c))
364178354Ssam		return IEEE80211_MODE_11NG;
365178354Ssam	if (IEEE80211_IS_CHAN_108A(c))
366178354Ssam		return IEEE80211_MODE_TURBO_A;
367178354Ssam	if (IEEE80211_IS_CHAN_108G(c))
368178354Ssam		return IEEE80211_MODE_TURBO_G;
369178354Ssam	if (IEEE80211_IS_CHAN_ST(c))
370178354Ssam		return IEEE80211_MODE_STURBO_A;
371178354Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
372178354Ssam		return IEEE80211_MODE_FH;
373188784Ssam	if (IEEE80211_IS_CHAN_HALF(c))
374188784Ssam		return IEEE80211_MODE_HALF;
375188784Ssam	if (IEEE80211_IS_CHAN_QUARTER(c))
376188784Ssam		return IEEE80211_MODE_QUARTER;
377178354Ssam	if (IEEE80211_IS_CHAN_A(c))
378178354Ssam		return IEEE80211_MODE_11A;
379178354Ssam	if (IEEE80211_IS_CHAN_ANYG(c))
380178354Ssam		return IEEE80211_MODE_11G;
381178354Ssam	if (IEEE80211_IS_CHAN_B(c))
382178354Ssam		return IEEE80211_MODE_11B;
383178354Ssam	return IEEE80211_MODE_AUTO;
384178354Ssam}
385178354Ssam
386178354Ssamstatic void
387178354Ssamgetroam(int s)
388178354Ssam{
389178354Ssam	if (gotroam)
390178354Ssam		return;
391178354Ssam	if (get80211(s, IEEE80211_IOC_ROAM,
392178354Ssam	    &roamparams, sizeof(roamparams)) < 0)
393187801Ssam		err(1, "unable to get roaming parameters");
394178354Ssam	gotroam = 1;
395178354Ssam}
396178354Ssam
397178354Ssamstatic void
398178354Ssamsetroam_cb(int s, void *arg)
399178354Ssam{
400178354Ssam	struct ieee80211_roamparams_req *roam = arg;
401178354Ssam	set80211(s, IEEE80211_IOC_ROAM, 0, sizeof(*roam), roam);
402178354Ssam}
403178354Ssam
404178354Ssamstatic void
405178354Ssamgettxparams(int s)
406178354Ssam{
407178354Ssam	if (gottxparams)
408178354Ssam		return;
409178354Ssam	if (get80211(s, IEEE80211_IOC_TXPARAMS,
410178354Ssam	    &txparams, sizeof(txparams)) < 0)
411187801Ssam		err(1, "unable to get transmit parameters");
412178354Ssam	gottxparams = 1;
413178354Ssam}
414178354Ssam
415178354Ssamstatic void
416178354Ssamsettxparams_cb(int s, void *arg)
417178354Ssam{
418178354Ssam	struct ieee80211_txparams_req *txp = arg;
419178354Ssam	set80211(s, IEEE80211_IOC_TXPARAMS, 0, sizeof(*txp), txp);
420178354Ssam}
421178354Ssam
422178354Ssamstatic void
423178354Ssamgetregdomain(int s)
424178354Ssam{
425178354Ssam	if (gotregdomain)
426178354Ssam		return;
427178354Ssam	if (get80211(s, IEEE80211_IOC_REGDOMAIN,
428178354Ssam	    &regdomain, sizeof(regdomain)) < 0)
429187801Ssam		err(1, "unable to get regulatory domain info");
430178354Ssam	gotregdomain = 1;
431178354Ssam}
432178354Ssam
433178354Ssamstatic void
434178354Ssamgetdevcaps(int s, struct ieee80211_devcaps_req *dc)
435178354Ssam{
436187801Ssam	if (get80211(s, IEEE80211_IOC_DEVCAPS, dc,
437187801Ssam	    IEEE80211_DEVCAPS_SPACE(dc)) < 0)
438187801Ssam		err(1, "unable to get device capabilities");
439178354Ssam}
440178354Ssam
441178354Ssamstatic void
442178354Ssamsetregdomain_cb(int s, void *arg)
443178354Ssam{
444187801Ssam	struct ieee80211_regdomain_req *req;
445178354Ssam	struct ieee80211_regdomain *rd = arg;
446187801Ssam	struct ieee80211_devcaps_req *dc;
447178354Ssam	struct regdata *rdp = getregdata();
448178354Ssam
449186102Ssam	if (rd->country != NO_COUNTRY) {
450178354Ssam		const struct country *cc;
451178354Ssam		/*
452178354Ssam		 * Check current country seting to make sure it's
453178354Ssam		 * compatible with the new regdomain.  If not, then
454178354Ssam		 * override it with any default country for this
455178354Ssam		 * SKU.  If we cannot arrange a match, then abort.
456178354Ssam		 */
457178354Ssam		cc = lib80211_country_findbycc(rdp, rd->country);
458178354Ssam		if (cc == NULL)
459178354Ssam			errx(1, "unknown ISO country code %d", rd->country);
460178354Ssam		if (cc->rd->sku != rd->regdomain) {
461178354Ssam			const struct regdomain *rp;
462178354Ssam			/*
463178354Ssam			 * Check if country is incompatible with regdomain.
464178354Ssam			 * To enable multiple regdomains for a country code
465178354Ssam			 * we permit a mismatch between the regdomain and
466178354Ssam			 * the country's associated regdomain when the
467178354Ssam			 * regdomain is setup w/o a default country.  For
468178354Ssam			 * example, US is bound to the FCC regdomain but
469178354Ssam			 * we allow US to be combined with FCC3 because FCC3
470178354Ssam			 * has not default country.  This allows bogus
471178354Ssam			 * combinations like FCC3+DK which are resolved when
472178354Ssam			 * constructing the channel list by deferring to the
473178354Ssam			 * regdomain to construct the channel list.
474178354Ssam			 */
475178354Ssam			rp = lib80211_regdomain_findbysku(rdp, rd->regdomain);
476178354Ssam			if (rp == NULL)
477178354Ssam				errx(1, "country %s (%s) is not usable with "
478178354Ssam				    "regdomain %d", cc->isoname, cc->name,
479178354Ssam				    rd->regdomain);
480186102Ssam			else if (rp->cc != NULL && rp->cc != cc)
481178354Ssam				errx(1, "country %s (%s) is not usable with "
482178354Ssam				   "regdomain %s", cc->isoname, cc->name,
483178354Ssam				   rp->name);
484178354Ssam		}
485178354Ssam	}
486178354Ssam	/*
487178354Ssam	 * Fetch the device capabilities and calculate the
488178354Ssam	 * full set of netbands for which we request a new
489178354Ssam	 * channel list be constructed.  Once that's done we
490178354Ssam	 * push the regdomain info + channel list to the kernel.
491178354Ssam	 */
492187801Ssam	dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
493187801Ssam	if (dc == NULL)
494187801Ssam		errx(1, "no space for device capabilities");
495187801Ssam	dc->dc_chaninfo.ic_nchans = MAXCHAN;
496187801Ssam	getdevcaps(s, dc);
497178354Ssam#if 0
498178354Ssam	if (verbose) {
499187801Ssam		printf("drivercaps: 0x%x\n", dc->dc_drivercaps);
500187801Ssam		printf("cryptocaps: 0x%x\n", dc->dc_cryptocaps);
501187801Ssam		printf("htcaps    : 0x%x\n", dc->dc_htcaps);
502187801Ssam		memcpy(chaninfo, &dc->dc_chaninfo,
503187801Ssam		    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
504187801Ssam		print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, 1/*verbose*/);
505178354Ssam	}
506178354Ssam#endif
507187801Ssam	req = malloc(IEEE80211_REGDOMAIN_SIZE(dc->dc_chaninfo.ic_nchans));
508187801Ssam	if (req == NULL)
509187801Ssam		errx(1, "no space for regdomain request");
510187801Ssam	req->rd = *rd;
511187801Ssam	regdomain_makechannels(req, dc);
512178354Ssam	if (verbose) {
513178354Ssam		LINE_INIT(':');
514178354Ssam		print_regdomain(rd, 1/*verbose*/);
515178354Ssam		LINE_BREAK();
516187801Ssam		/* blech, reallocate channel list for new data */
517187801Ssam		if (chaninfo != NULL)
518187801Ssam			free(chaninfo);
519187801Ssam		chaninfo = malloc(IEEE80211_CHANINFO_SPACE(&req->chaninfo));
520187801Ssam		if (chaninfo == NULL)
521187801Ssam			errx(1, "no space for channel list");
522187801Ssam		memcpy(chaninfo, &req->chaninfo,
523187801Ssam		    IEEE80211_CHANINFO_SPACE(&req->chaninfo));
524187801Ssam		print_channels(s, &req->chaninfo, 1/*allchans*/, 1/*verbose*/);
525178354Ssam	}
526187801Ssam	if (req->chaninfo.ic_nchans == 0)
527178354Ssam		errx(1, "no channels calculated");
528187801Ssam	set80211(s, IEEE80211_IOC_REGDOMAIN, 0,
529187801Ssam	    IEEE80211_REGDOMAIN_SPACE(req), req);
530187801Ssam	free(req);
531187801Ssam	free(dc);
532178354Ssam}
533178354Ssam
534170531Ssamstatic int
535170531Ssamieee80211_mhz2ieee(int freq, int flags)
536170531Ssam{
537170531Ssam	struct ieee80211_channel chan;
538170531Ssam	mapfreq(&chan, freq, flags);
539170531Ssam	return chan.ic_ieee;
540170531Ssam}
541170531Ssam
542170531Ssamstatic int
543138593Ssamisanyarg(const char *arg)
544138593Ssam{
545173275Ssam	return (strncmp(arg, "-", 1) == 0 ||
546173275Ssam	    strncasecmp(arg, "any", 3) == 0 || strncasecmp(arg, "off", 3) == 0);
547138593Ssam}
548138593Ssam
549138593Ssamstatic void
55077218Sphkset80211ssid(const char *val, int d, int s, const struct afswtch *rafp)
55177218Sphk{
55277218Sphk	int		ssid;
55377218Sphk	int		len;
554151883Sbrooks	u_int8_t	data[IEEE80211_NWID_LEN];
55577218Sphk
55677218Sphk	ssid = 0;
557121827Sbrooks	len = strlen(val);
558178354Ssam	if (len > 2 && isdigit((int)val[0]) && val[1] == ':') {
55988748Sambrisko		ssid = atoi(val)-1;
56088748Sambrisko		val += 2;
56188748Sambrisko	}
56277218Sphk
56377218Sphk	bzero(data, sizeof(data));
56477218Sphk	len = sizeof(data);
565151883Sbrooks	if (get_string(val, NULL, data, &len) == NULL)
566151883Sbrooks		exit(1);
56777218Sphk
56877218Sphk	set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
56977218Sphk}
57077218Sphk
571138593Ssamstatic void
57277218Sphkset80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
57377218Sphk{
57477218Sphk	int			len;
57577218Sphk	u_int8_t		data[33];
57677218Sphk
57777218Sphk	bzero(data, sizeof(data));
57877218Sphk	len = sizeof(data);
57977218Sphk	get_string(val, NULL, data, &len);
58077218Sphk
58177218Sphk	set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
58277218Sphk}
58377218Sphk
584138593Ssam/*
585170531Ssam * Parse a channel specification for attributes/flags.
586170531Ssam * The syntax is:
587170531Ssam *	freq/xx		channel width (5,10,20,40,40+,40-)
588170531Ssam *	freq:mode	channel mode (a,b,g,h,n,t,s,d)
589170531Ssam *
590170531Ssam * These can be combined in either order; e.g. 2437:ng/40.
591170531Ssam * Modes are case insensitive.
592170531Ssam *
593170531Ssam * The result is not validated here; it's assumed to be
594170531Ssam * checked against the channel table fetched from the kernel.
595170531Ssam */
596170531Ssamstatic int
597173275Ssamgetchannelflags(const char *val, int freq)
598138593Ssam{
599170531Ssam#define	_CHAN_HT	0x80000000
600170531Ssam	const char *cp;
601170531Ssam	int flags;
602138593Ssam
603170531Ssam	flags = 0;
604166015Ssam
605170531Ssam	cp = strchr(val, ':');
606170531Ssam	if (cp != NULL) {
607170531Ssam		for (cp++; isalpha((int) *cp); cp++) {
608170531Ssam			/* accept mixed case */
609170531Ssam			int c = *cp;
610170531Ssam			if (isupper(c))
611170531Ssam				c = tolower(c);
612170531Ssam			switch (c) {
613170531Ssam			case 'a':		/* 802.11a */
614170531Ssam				flags |= IEEE80211_CHAN_A;
615170531Ssam				break;
616170531Ssam			case 'b':		/* 802.11b */
617170531Ssam				flags |= IEEE80211_CHAN_B;
618170531Ssam				break;
619170531Ssam			case 'g':		/* 802.11g */
620170531Ssam				flags |= IEEE80211_CHAN_G;
621170531Ssam				break;
622170531Ssam			case 'h':		/* ht = 802.11n */
623170531Ssam			case 'n':		/* 802.11n */
624170531Ssam				flags |= _CHAN_HT;	/* NB: private */
625170531Ssam				break;
626170531Ssam			case 'd':		/* dt = Atheros Dynamic Turbo */
627170531Ssam				flags |= IEEE80211_CHAN_TURBO;
628170531Ssam				break;
629170531Ssam			case 't':		/* ht, dt, st, t */
630170531Ssam				/* dt and unadorned t specify Dynamic Turbo */
631170531Ssam				if ((flags & (IEEE80211_CHAN_STURBO|_CHAN_HT)) == 0)
632170531Ssam					flags |= IEEE80211_CHAN_TURBO;
633170531Ssam				break;
634170531Ssam			case 's':		/* st = Atheros Static Turbo */
635170531Ssam				flags |= IEEE80211_CHAN_STURBO;
636170531Ssam				break;
637170531Ssam			default:
638173275Ssam				errx(-1, "%s: Invalid channel attribute %c\n",
639170531Ssam				    val, *cp);
640170531Ssam			}
641170531Ssam		}
642170531Ssam	}
643170531Ssam	cp = strchr(val, '/');
644170531Ssam	if (cp != NULL) {
645170531Ssam		char *ep;
646170531Ssam		u_long cw = strtoul(cp+1, &ep, 10);
647166015Ssam
648170531Ssam		switch (cw) {
649170531Ssam		case 5:
650170531Ssam			flags |= IEEE80211_CHAN_QUARTER;
651170531Ssam			break;
652170531Ssam		case 10:
653170531Ssam			flags |= IEEE80211_CHAN_HALF;
654170531Ssam			break;
655170531Ssam		case 20:
656170531Ssam			/* NB: this may be removed below */
657170531Ssam			flags |= IEEE80211_CHAN_HT20;
658170531Ssam			break;
659170531Ssam		case 40:
660170531Ssam			if (ep != NULL && *ep == '+')
661170531Ssam				flags |= IEEE80211_CHAN_HT40U;
662170531Ssam			else if (ep != NULL && *ep == '-')
663170531Ssam				flags |= IEEE80211_CHAN_HT40D;
664170531Ssam			break;
665170531Ssam		default:
666173275Ssam			errx(-1, "%s: Invalid channel width\n", val);
667170531Ssam		}
668165570Ssam	}
669170531Ssam	/*
670170531Ssam	 * Cleanup specifications.
671170531Ssam	 */
672170531Ssam	if ((flags & _CHAN_HT) == 0) {
673170531Ssam		/*
674170531Ssam		 * If user specified freq/20 or freq/40 quietly remove
675170531Ssam		 * HT cw attributes depending on channel use.  To give
676170531Ssam		 * an explicit 20/40 width for an HT channel you must
677170531Ssam		 * indicate it is an HT channel since all HT channels
678170531Ssam		 * are also usable for legacy operation; e.g. freq:n/40.
679170531Ssam		 */
680170531Ssam		flags &= ~IEEE80211_CHAN_HT;
681170531Ssam	} else {
682170531Ssam		/*
683170531Ssam		 * Remove private indicator that this is an HT channel
684170531Ssam		 * and if no explicit channel width has been given
685170531Ssam		 * provide the default settings.
686170531Ssam		 */
687170531Ssam		flags &= ~_CHAN_HT;
688173275Ssam		if ((flags & IEEE80211_CHAN_HT) == 0) {
689173275Ssam			struct ieee80211_channel chan;
690173275Ssam			/*
691173275Ssam			 * Consult the channel list to see if we can use
692173275Ssam			 * HT40+ or HT40- (if both the map routines choose).
693173275Ssam			 */
694173275Ssam			if (freq > 255)
695173275Ssam				mapfreq(&chan, freq, 0);
696173275Ssam			else
697173275Ssam				mapchan(&chan, freq, 0);
698173275Ssam			flags |= (chan.ic_flags & IEEE80211_CHAN_HT);
699173275Ssam		}
700170531Ssam	}
701170531Ssam	return flags;
702170531Ssam#undef _CHAN_HT
703138593Ssam}
704138593Ssam
705138593Ssamstatic void
706186105Ssamgetchannel(int s, struct ieee80211_channel *chan, const char *val)
707186105Ssam{
708186105Ssam	int v, flags;
709186105Ssam	char *eptr;
710186105Ssam
711186105Ssam	memset(chan, 0, sizeof(*chan));
712186105Ssam	if (isanyarg(val)) {
713186105Ssam		chan->ic_freq = IEEE80211_CHAN_ANY;
714186105Ssam		return;
715186105Ssam	}
716186105Ssam	getchaninfo(s);
717186105Ssam	errno = 0;
718186105Ssam	v = strtol(val, &eptr, 10);
719186105Ssam	if (val[0] == '\0' || val == eptr || errno == ERANGE ||
720186105Ssam	    /* channel may be suffixed with nothing, :flag, or /width */
721186105Ssam	    (eptr[0] != '\0' && eptr[0] != ':' && eptr[0] != '/'))
722186105Ssam		errx(1, "invalid channel specification%s",
723186105Ssam		    errno == ERANGE ? " (out of range)" : "");
724186105Ssam	flags = getchannelflags(val, v);
725186105Ssam	if (v > 255) {		/* treat as frequency */
726186105Ssam		mapfreq(chan, v, flags);
727186105Ssam	} else {
728186105Ssam		mapchan(chan, v, flags);
729186105Ssam	}
730186105Ssam}
731186105Ssam
732186105Ssamstatic void
73377218Sphkset80211channel(const char *val, int d, int s, const struct afswtch *rafp)
73477218Sphk{
735170531Ssam	struct ieee80211_channel chan;
736170531Ssam
737186105Ssam	getchannel(s, &chan, val);
738170531Ssam	set80211(s, IEEE80211_IOC_CURCHAN, 0, sizeof(chan), &chan);
73977218Sphk}
74077218Sphk
741138593Ssamstatic void
742178354Ssamset80211chanswitch(const char *val, int d, int s, const struct afswtch *rafp)
743178354Ssam{
744178354Ssam	struct ieee80211_chanswitch_req csr;
745178354Ssam
746186105Ssam	getchannel(s, &csr.csa_chan, val);
747178354Ssam	csr.csa_mode = 1;
748178354Ssam	csr.csa_count = 5;
749178354Ssam	set80211(s, IEEE80211_IOC_CHANSWITCH, 0, sizeof(csr), &csr);
750178354Ssam}
751178354Ssam
752178354Ssamstatic void
75377218Sphkset80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
75477218Sphk{
75577218Sphk	int	mode;
75677218Sphk
75791454Sbrooks	if (strcasecmp(val, "none") == 0) {
75877218Sphk		mode = IEEE80211_AUTH_NONE;
75991454Sbrooks	} else if (strcasecmp(val, "open") == 0) {
76077218Sphk		mode = IEEE80211_AUTH_OPEN;
76191454Sbrooks	} else if (strcasecmp(val, "shared") == 0) {
76277218Sphk		mode = IEEE80211_AUTH_SHARED;
763138593Ssam	} else if (strcasecmp(val, "8021x") == 0) {
764138593Ssam		mode = IEEE80211_AUTH_8021X;
765138593Ssam	} else if (strcasecmp(val, "wpa") == 0) {
766138593Ssam		mode = IEEE80211_AUTH_WPA;
76777218Sphk	} else {
768150708Sru		errx(1, "unknown authmode");
76977218Sphk	}
77077218Sphk
77177218Sphk	set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
77277218Sphk}
77377218Sphk
774138593Ssamstatic void
77577218Sphkset80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
77677218Sphk{
77777218Sphk	int	mode;
77877218Sphk
77991454Sbrooks	if (strcasecmp(val, "off") == 0) {
78077218Sphk		mode = IEEE80211_POWERSAVE_OFF;
78191454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
78277218Sphk		mode = IEEE80211_POWERSAVE_ON;
78391454Sbrooks	} else if (strcasecmp(val, "cam") == 0) {
78477218Sphk		mode = IEEE80211_POWERSAVE_CAM;
78591454Sbrooks	} else if (strcasecmp(val, "psp") == 0) {
78677218Sphk		mode = IEEE80211_POWERSAVE_PSP;
78791454Sbrooks	} else if (strcasecmp(val, "psp-cam") == 0) {
78877218Sphk		mode = IEEE80211_POWERSAVE_PSP_CAM;
78977218Sphk	} else {
790150708Sru		errx(1, "unknown powersavemode");
79177218Sphk	}
79277218Sphk
79377218Sphk	set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
79477218Sphk}
79577218Sphk
796138593Ssamstatic void
79777218Sphkset80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
79877218Sphk{
79977218Sphk	if (d == 0)
80077218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
80177218Sphk		    0, NULL);
80277218Sphk	else
80377218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
80477218Sphk		    0, NULL);
80577218Sphk}
80677218Sphk
807138593Ssamstatic void
80877218Sphkset80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
80977218Sphk{
81077218Sphk	set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
81177218Sphk}
81277218Sphk
813138593Ssamstatic void
81477218Sphkset80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
81577218Sphk{
81677218Sphk	int	mode;
81777218Sphk
81891454Sbrooks	if (strcasecmp(val, "off") == 0) {
81977218Sphk		mode = IEEE80211_WEP_OFF;
82091454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
82177218Sphk		mode = IEEE80211_WEP_ON;
82291454Sbrooks	} else if (strcasecmp(val, "mixed") == 0) {
82377218Sphk		mode = IEEE80211_WEP_MIXED;
82477218Sphk	} else {
825150708Sru		errx(1, "unknown wep mode");
82677218Sphk	}
82777218Sphk
82877218Sphk	set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
82977218Sphk}
83077218Sphk
831138593Ssamstatic void
83277218Sphkset80211wep(const char *val, int d, int s, const struct afswtch *rafp)
83377218Sphk{
83477218Sphk	set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
83577218Sphk}
83677218Sphk
837139493Ssamstatic int
838139493Ssamisundefarg(const char *arg)
839139493Ssam{
840139493Ssam	return (strcmp(arg, "-") == 0 || strncasecmp(arg, "undef", 5) == 0);
841139493Ssam}
842139493Ssam
843138593Ssamstatic void
84477218Sphkset80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
84577218Sphk{
846139493Ssam	if (isundefarg(val))
847139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, IEEE80211_KEYIX_NONE, 0, NULL);
848139493Ssam	else
849139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
85077218Sphk}
85177218Sphk
852138593Ssamstatic void
85377218Sphkset80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
85477218Sphk{
85577218Sphk	int		key = 0;
85677218Sphk	int		len;
857120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
85877218Sphk
859178354Ssam	if (isdigit((int)val[0]) && val[1] == ':') {
86077218Sphk		key = atoi(val)-1;
86177218Sphk		val += 2;
86277218Sphk	}
86377218Sphk
86477218Sphk	bzero(data, sizeof(data));
86577218Sphk	len = sizeof(data);
86677218Sphk	get_string(val, NULL, data, &len);
86777218Sphk
86877218Sphk	set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
86977218Sphk}
87077218Sphk
87177218Sphk/*
872148686Sstefanf * This function is purely a NetBSD compatability interface.  The NetBSD
873148686Sstefanf * interface is too inflexible, but it's there so we'll support it since
87477218Sphk * it's not all that hard.
87577218Sphk */
876138593Ssamstatic void
87777218Sphkset80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
87877218Sphk{
87977218Sphk	int		txkey;
88077218Sphk	int		i, len;
881120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
88277218Sphk
88377218Sphk	set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
88477218Sphk
885178354Ssam	if (isdigit((int)val[0]) && val[1] == ':') {
88677218Sphk		txkey = val[0]-'0'-1;
88777218Sphk		val += 2;
88877218Sphk
88991454Sbrooks		for (i = 0; i < 4; i++) {
89077218Sphk			bzero(data, sizeof(data));
89177218Sphk			len = sizeof(data);
89277218Sphk			val = get_string(val, ",", data, &len);
893151827Sbrooks			if (val == NULL)
894151827Sbrooks				exit(1);
89577218Sphk
89677218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
89777218Sphk		}
89877218Sphk	} else {
89977218Sphk		bzero(data, sizeof(data));
90077218Sphk		len = sizeof(data);
90177218Sphk		get_string(val, NULL, data, &len);
90277218Sphk		txkey = 0;
90377218Sphk
90477218Sphk		set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
90577218Sphk
90677218Sphk		bzero(data, sizeof(data));
90791454Sbrooks		for (i = 1; i < 4; i++)
90877218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
90977218Sphk	}
91077218Sphk
91177218Sphk	set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
91277218Sphk}
91377218Sphk
914138593Ssamstatic void
915127649Ssamset80211rtsthreshold(const char *val, int d, int s, const struct afswtch *rafp)
916127649Ssam{
917148416Ssam	set80211(s, IEEE80211_IOC_RTSTHRESHOLD,
918148416Ssam		isundefarg(val) ? IEEE80211_RTS_MAX : atoi(val), 0, NULL);
919127649Ssam}
920127649Ssam
921138593Ssamstatic void
922127649Ssamset80211protmode(const char *val, int d, int s, const struct afswtch *rafp)
923127649Ssam{
924127649Ssam	int	mode;
925127649Ssam
926127649Ssam	if (strcasecmp(val, "off") == 0) {
927127649Ssam		mode = IEEE80211_PROTMODE_OFF;
928127649Ssam	} else if (strcasecmp(val, "cts") == 0) {
929127649Ssam		mode = IEEE80211_PROTMODE_CTS;
930173275Ssam	} else if (strncasecmp(val, "rtscts", 3) == 0) {
931127649Ssam		mode = IEEE80211_PROTMODE_RTSCTS;
932127649Ssam	} else {
933150708Sru		errx(1, "unknown protection mode");
934127649Ssam	}
935127649Ssam
936127649Ssam	set80211(s, IEEE80211_IOC_PROTMODE, mode, 0, NULL);
937127649Ssam}
938127649Ssam
939138593Ssamstatic void
940173275Ssamset80211htprotmode(const char *val, int d, int s, const struct afswtch *rafp)
941173275Ssam{
942173275Ssam	int	mode;
943173275Ssam
944173275Ssam	if (strcasecmp(val, "off") == 0) {
945173275Ssam		mode = IEEE80211_PROTMODE_OFF;
946173275Ssam	} else if (strncasecmp(val, "rts", 3) == 0) {
947173275Ssam		mode = IEEE80211_PROTMODE_RTSCTS;
948173275Ssam	} else {
949173275Ssam		errx(1, "unknown protection mode");
950173275Ssam	}
951173275Ssam
952173275Ssam	set80211(s, IEEE80211_IOC_HTPROTMODE, mode, 0, NULL);
953173275Ssam}
954173275Ssam
955173275Ssamstatic void
956127649Ssamset80211txpower(const char *val, int d, int s, const struct afswtch *rafp)
957127649Ssam{
958173275Ssam	double v = atof(val);
959173275Ssam	int txpow;
960173275Ssam
961173275Ssam	txpow = (int) (2*v);
962173275Ssam	if (txpow != 2*v)
963173275Ssam		errx(-1, "invalid tx power (must be .5 dBm units)");
964173275Ssam	set80211(s, IEEE80211_IOC_TXPOWER, txpow, 0, NULL);
965127649Ssam}
966127649Ssam
967138593Ssam#define	IEEE80211_ROAMING_DEVICE	0
968138593Ssam#define	IEEE80211_ROAMING_AUTO		1
969138593Ssam#define	IEEE80211_ROAMING_MANUAL	2
970138593Ssam
971138593Ssamstatic void
972138593Ssamset80211roaming(const char *val, int d, int s, const struct afswtch *rafp)
97377218Sphk{
974138593Ssam	int mode;
97577218Sphk
976138593Ssam	if (strcasecmp(val, "device") == 0) {
977138593Ssam		mode = IEEE80211_ROAMING_DEVICE;
978138593Ssam	} else if (strcasecmp(val, "auto") == 0) {
979138593Ssam		mode = IEEE80211_ROAMING_AUTO;
980138593Ssam	} else if (strcasecmp(val, "manual") == 0) {
981138593Ssam		mode = IEEE80211_ROAMING_MANUAL;
982138593Ssam	} else {
983150708Sru		errx(1, "unknown roaming mode");
984138593Ssam	}
985138593Ssam	set80211(s, IEEE80211_IOC_ROAMING, mode, 0, NULL);
986138593Ssam}
987138593Ssam
988138593Ssamstatic void
989138593Ssamset80211wme(const char *val, int d, int s, const struct afswtch *rafp)
990138593Ssam{
991138593Ssam	set80211(s, IEEE80211_IOC_WME, d, 0, NULL);
992138593Ssam}
993138593Ssam
994138593Ssamstatic void
995138593Ssamset80211hidessid(const char *val, int d, int s, const struct afswtch *rafp)
996138593Ssam{
997138593Ssam	set80211(s, IEEE80211_IOC_HIDESSID, d, 0, NULL);
998138593Ssam}
999138593Ssam
1000138593Ssamstatic void
1001138593Ssamset80211apbridge(const char *val, int d, int s, const struct afswtch *rafp)
1002138593Ssam{
1003138593Ssam	set80211(s, IEEE80211_IOC_APBRIDGE, d, 0, NULL);
1004138593Ssam}
1005138593Ssam
1006138593Ssamstatic void
1007170531Ssamset80211fastframes(const char *val, int d, int s, const struct afswtch *rafp)
1008170531Ssam{
1009170531Ssam	set80211(s, IEEE80211_IOC_FF, d, 0, NULL);
1010170531Ssam}
1011170531Ssam
1012170531Ssamstatic void
1013170531Ssamset80211dturbo(const char *val, int d, int s, const struct afswtch *rafp)
1014170531Ssam{
1015170531Ssam	set80211(s, IEEE80211_IOC_TURBOP, d, 0, NULL);
1016170531Ssam}
1017170531Ssam
1018170531Ssamstatic void
1019138593Ssamset80211chanlist(const char *val, int d, int s, const struct afswtch *rafp)
1020138593Ssam{
1021138593Ssam	struct ieee80211req_chanlist chanlist;
1022138593Ssam	char *temp, *cp, *tp;
1023138593Ssam
1024138593Ssam	temp = malloc(strlen(val) + 1);
1025138593Ssam	if (temp == NULL)
1026138593Ssam		errx(1, "malloc failed");
1027138593Ssam	strcpy(temp, val);
1028138593Ssam	memset(&chanlist, 0, sizeof(chanlist));
1029138593Ssam	cp = temp;
1030138593Ssam	for (;;) {
1031173275Ssam		int first, last, f, c;
1032138593Ssam
1033138593Ssam		tp = strchr(cp, ',');
1034138593Ssam		if (tp != NULL)
1035138593Ssam			*tp++ = '\0';
1036138593Ssam		switch (sscanf(cp, "%u-%u", &first, &last)) {
1037138593Ssam		case 1:
1038187801Ssam			if (first > IEEE80211_CHAN_MAX)
1039188038Sdelphij				errx(-1, "channel %u out of range, max %u",
1040187801Ssam					first, IEEE80211_CHAN_MAX);
1041138593Ssam			setbit(chanlist.ic_channels, first);
1042138593Ssam			break;
1043138593Ssam		case 2:
1044187801Ssam			if (first > IEEE80211_CHAN_MAX)
1045188038Sdelphij				errx(-1, "channel %u out of range, max %u",
1046187801Ssam					first, IEEE80211_CHAN_MAX);
1047187801Ssam			if (last > IEEE80211_CHAN_MAX)
1048188038Sdelphij				errx(-1, "channel %u out of range, max %u",
1049187801Ssam					last, IEEE80211_CHAN_MAX);
1050138593Ssam			if (first > last)
1051138593Ssam				errx(-1, "void channel range, %u > %u",
1052138593Ssam					first, last);
1053138593Ssam			for (f = first; f <= last; f++)
1054138593Ssam				setbit(chanlist.ic_channels, f);
1055138593Ssam			break;
1056138593Ssam		}
1057138593Ssam		if (tp == NULL)
1058138593Ssam			break;
1059173275Ssam		c = *tp;
1060173275Ssam		while (isspace(c))
1061138593Ssam			tp++;
1062173275Ssam		if (!isdigit(c))
1063138593Ssam			break;
1064138593Ssam		cp = tp;
1065138593Ssam	}
1066170531Ssam	set80211(s, IEEE80211_IOC_CHANLIST, 0, sizeof(chanlist), &chanlist);
1067138593Ssam}
1068138593Ssam
1069138593Ssamstatic void
1070138593Ssamset80211bssid(const char *val, int d, int s, const struct afswtch *rafp)
1071138593Ssam{
1072138593Ssam
1073138593Ssam	if (!isanyarg(val)) {
1074138593Ssam		char *temp;
1075138593Ssam		struct sockaddr_dl sdl;
1076138593Ssam
1077155702Ssam		temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1078138593Ssam		if (temp == NULL)
1079138593Ssam			errx(1, "malloc failed");
1080138593Ssam		temp[0] = ':';
1081138593Ssam		strcpy(temp + 1, val);
1082138593Ssam		sdl.sdl_len = sizeof(sdl);
1083138593Ssam		link_addr(temp, &sdl);
1084138593Ssam		free(temp);
1085138593Ssam		if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1086138593Ssam			errx(1, "malformed link-level address");
1087138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
1088138593Ssam			IEEE80211_ADDR_LEN, LLADDR(&sdl));
1089138593Ssam	} else {
1090138593Ssam		uint8_t zerobssid[IEEE80211_ADDR_LEN];
1091138593Ssam		memset(zerobssid, 0, sizeof(zerobssid));
1092138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
1093138593Ssam			IEEE80211_ADDR_LEN, zerobssid);
1094138593Ssam	}
1095138593Ssam}
1096138593Ssam
1097138593Ssamstatic int
1098138593Ssamgetac(const char *ac)
1099138593Ssam{
1100138593Ssam	if (strcasecmp(ac, "ac_be") == 0 || strcasecmp(ac, "be") == 0)
1101138593Ssam		return WME_AC_BE;
1102138593Ssam	if (strcasecmp(ac, "ac_bk") == 0 || strcasecmp(ac, "bk") == 0)
1103138593Ssam		return WME_AC_BK;
1104138593Ssam	if (strcasecmp(ac, "ac_vi") == 0 || strcasecmp(ac, "vi") == 0)
1105138593Ssam		return WME_AC_VI;
1106138593Ssam	if (strcasecmp(ac, "ac_vo") == 0 || strcasecmp(ac, "vo") == 0)
1107138593Ssam		return WME_AC_VO;
1108138593Ssam	errx(1, "unknown wme access class %s", ac);
1109138593Ssam}
1110138593Ssam
1111138593Ssamstatic
1112138593SsamDECL_CMD_FUNC2(set80211cwmin, ac, val)
1113138593Ssam{
1114138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val), getac(ac), NULL);
1115138593Ssam}
1116138593Ssam
1117138593Ssamstatic
1118138593SsamDECL_CMD_FUNC2(set80211cwmax, ac, val)
1119138593Ssam{
1120138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val), getac(ac), NULL);
1121138593Ssam}
1122138593Ssam
1123138593Ssamstatic
1124138593SsamDECL_CMD_FUNC2(set80211aifs, ac, val)
1125138593Ssam{
1126138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val), getac(ac), NULL);
1127138593Ssam}
1128138593Ssam
1129138593Ssamstatic
1130138593SsamDECL_CMD_FUNC2(set80211txoplimit, ac, val)
1131138593Ssam{
1132138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), getac(ac), NULL);
1133138593Ssam}
1134138593Ssam
1135138593Ssamstatic
1136148621SsamDECL_CMD_FUNC(set80211acm, ac, d)
1137138593Ssam{
1138148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 1, getac(ac), NULL);
1139138593Ssam}
1140148621Ssamstatic
1141148621SsamDECL_CMD_FUNC(set80211noacm, ac, d)
1142148621Ssam{
1143148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 0, getac(ac), NULL);
1144148621Ssam}
1145138593Ssam
1146138593Ssamstatic
1147148621SsamDECL_CMD_FUNC(set80211ackpolicy, ac, d)
1148138593Ssam{
1149148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 1, getac(ac), NULL);
1150138593Ssam}
1151148621Ssamstatic
1152148621SsamDECL_CMD_FUNC(set80211noackpolicy, ac, d)
1153148621Ssam{
1154148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 0, getac(ac), NULL);
1155148621Ssam}
1156138593Ssam
1157138593Ssamstatic
1158138593SsamDECL_CMD_FUNC2(set80211bsscwmin, ac, val)
1159138593Ssam{
1160138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val),
1161138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1162138593Ssam}
1163138593Ssam
1164138593Ssamstatic
1165138593SsamDECL_CMD_FUNC2(set80211bsscwmax, ac, val)
1166138593Ssam{
1167138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val),
1168138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1169138593Ssam}
1170138593Ssam
1171138593Ssamstatic
1172138593SsamDECL_CMD_FUNC2(set80211bssaifs, ac, val)
1173138593Ssam{
1174138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val),
1175138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1176138593Ssam}
1177138593Ssam
1178138593Ssamstatic
1179138593SsamDECL_CMD_FUNC2(set80211bsstxoplimit, ac, val)
1180138593Ssam{
1181138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val),
1182138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1183138593Ssam}
1184138593Ssam
1185138593Ssamstatic
1186138593SsamDECL_CMD_FUNC(set80211dtimperiod, val, d)
1187138593Ssam{
1188138593Ssam	set80211(s, IEEE80211_IOC_DTIM_PERIOD, atoi(val), 0, NULL);
1189138593Ssam}
1190138593Ssam
1191138593Ssamstatic
1192138593SsamDECL_CMD_FUNC(set80211bintval, val, d)
1193138593Ssam{
1194138593Ssam	set80211(s, IEEE80211_IOC_BEACON_INTERVAL, atoi(val), 0, NULL);
1195138593Ssam}
1196138593Ssam
1197138593Ssamstatic void
1198138593Ssamset80211macmac(int s, int op, const char *val)
1199138593Ssam{
1200138593Ssam	char *temp;
1201138593Ssam	struct sockaddr_dl sdl;
1202138593Ssam
1203155702Ssam	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1204138593Ssam	if (temp == NULL)
1205138593Ssam		errx(1, "malloc failed");
1206138593Ssam	temp[0] = ':';
1207138593Ssam	strcpy(temp + 1, val);
1208138593Ssam	sdl.sdl_len = sizeof(sdl);
1209138593Ssam	link_addr(temp, &sdl);
1210138593Ssam	free(temp);
1211138593Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1212138593Ssam		errx(1, "malformed link-level address");
1213138593Ssam	set80211(s, op, 0, IEEE80211_ADDR_LEN, LLADDR(&sdl));
1214138593Ssam}
1215138593Ssam
1216138593Ssamstatic
1217138593SsamDECL_CMD_FUNC(set80211addmac, val, d)
1218138593Ssam{
1219138593Ssam	set80211macmac(s, IEEE80211_IOC_ADDMAC, val);
1220138593Ssam}
1221138593Ssam
1222138593Ssamstatic
1223138593SsamDECL_CMD_FUNC(set80211delmac, val, d)
1224138593Ssam{
1225138593Ssam	set80211macmac(s, IEEE80211_IOC_DELMAC, val);
1226138593Ssam}
1227138593Ssam
1228138593Ssamstatic
1229149029SsamDECL_CMD_FUNC(set80211kickmac, val, d)
1230149029Ssam{
1231149029Ssam	char *temp;
1232149029Ssam	struct sockaddr_dl sdl;
1233149029Ssam	struct ieee80211req_mlme mlme;
1234149029Ssam
1235155702Ssam	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1236149029Ssam	if (temp == NULL)
1237149029Ssam		errx(1, "malloc failed");
1238149029Ssam	temp[0] = ':';
1239149029Ssam	strcpy(temp + 1, val);
1240149029Ssam	sdl.sdl_len = sizeof(sdl);
1241149029Ssam	link_addr(temp, &sdl);
1242149029Ssam	free(temp);
1243149029Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1244149029Ssam		errx(1, "malformed link-level address");
1245149029Ssam	memset(&mlme, 0, sizeof(mlme));
1246149029Ssam	mlme.im_op = IEEE80211_MLME_DEAUTH;
1247149029Ssam	mlme.im_reason = IEEE80211_REASON_AUTH_EXPIRE;
1248149029Ssam	memcpy(mlme.im_macaddr, LLADDR(&sdl), IEEE80211_ADDR_LEN);
1249170531Ssam	set80211(s, IEEE80211_IOC_MLME, 0, sizeof(mlme), &mlme);
1250149029Ssam}
1251149029Ssam
1252149029Ssamstatic
1253138593SsamDECL_CMD_FUNC(set80211maccmd, val, d)
1254138593Ssam{
1255138593Ssam	set80211(s, IEEE80211_IOC_MACCMD, d, 0, NULL);
1256138593Ssam}
1257138593Ssam
1258147795Ssamstatic void
1259147795Ssamset80211pureg(const char *val, int d, int s, const struct afswtch *rafp)
1260147795Ssam{
1261147795Ssam	set80211(s, IEEE80211_IOC_PUREG, d, 0, NULL);
1262147795Ssam}
1263147795Ssam
1264153422Ssamstatic void
1265170531Ssamset80211bgscan(const char *val, int d, int s, const struct afswtch *rafp)
1266153422Ssam{
1267170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN, d, 0, NULL);
1268153422Ssam}
1269153422Ssam
1270148416Ssamstatic
1271170531SsamDECL_CMD_FUNC(set80211bgscanidle, val, d)
1272170531Ssam{
1273170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN_IDLE, atoi(val), 0, NULL);
1274170531Ssam}
1275170531Ssam
1276170531Ssamstatic
1277170531SsamDECL_CMD_FUNC(set80211bgscanintvl, val, d)
1278170531Ssam{
1279170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN_INTERVAL, atoi(val), 0, NULL);
1280170531Ssam}
1281170531Ssam
1282170531Ssamstatic
1283170531SsamDECL_CMD_FUNC(set80211scanvalid, val, d)
1284170531Ssam{
1285170531Ssam	set80211(s, IEEE80211_IOC_SCANVALID, atoi(val), 0, NULL);
1286170531Ssam}
1287170531Ssam
1288178354Ssam/*
1289178354Ssam * Parse an optional trailing specification of which netbands
1290178354Ssam * to apply a parameter to.  This is basically the same syntax
1291178354Ssam * as used for channels but you can concatenate to specify
1292178354Ssam * multiple.  For example:
1293178354Ssam *	14:abg		apply to 11a, 11b, and 11g
1294178354Ssam *	6:ht		apply to 11na and 11ng
1295178354Ssam * We don't make a big effort to catch silly things; this is
1296178354Ssam * really a convenience mechanism.
1297178354Ssam */
1298178354Ssamstatic int
1299178354Ssamgetmodeflags(const char *val)
1300170531Ssam{
1301178354Ssam	const char *cp;
1302178354Ssam	int flags;
1303178354Ssam
1304178354Ssam	flags = 0;
1305178354Ssam
1306178354Ssam	cp = strchr(val, ':');
1307178354Ssam	if (cp != NULL) {
1308178354Ssam		for (cp++; isalpha((int) *cp); cp++) {
1309178354Ssam			/* accept mixed case */
1310178354Ssam			int c = *cp;
1311178354Ssam			if (isupper(c))
1312178354Ssam				c = tolower(c);
1313178354Ssam			switch (c) {
1314178354Ssam			case 'a':		/* 802.11a */
1315178354Ssam				flags |= IEEE80211_CHAN_A;
1316178354Ssam				break;
1317178354Ssam			case 'b':		/* 802.11b */
1318178354Ssam				flags |= IEEE80211_CHAN_B;
1319178354Ssam				break;
1320178354Ssam			case 'g':		/* 802.11g */
1321178354Ssam				flags |= IEEE80211_CHAN_G;
1322178354Ssam				break;
1323178354Ssam			case 'n':		/* 802.11n */
1324178354Ssam				flags |= IEEE80211_CHAN_HT;
1325178354Ssam				break;
1326178354Ssam			case 'd':		/* dt = Atheros Dynamic Turbo */
1327178354Ssam				flags |= IEEE80211_CHAN_TURBO;
1328178354Ssam				break;
1329178354Ssam			case 't':		/* ht, dt, st, t */
1330178354Ssam				/* dt and unadorned t specify Dynamic Turbo */
1331178354Ssam				if ((flags & (IEEE80211_CHAN_STURBO|IEEE80211_CHAN_HT)) == 0)
1332178354Ssam					flags |= IEEE80211_CHAN_TURBO;
1333178354Ssam				break;
1334178354Ssam			case 's':		/* st = Atheros Static Turbo */
1335178354Ssam				flags |= IEEE80211_CHAN_STURBO;
1336178354Ssam				break;
1337188784Ssam			case 'h':		/* 1/2-width channels */
1338188784Ssam				flags |= IEEE80211_CHAN_HALF;
1339188784Ssam				break;
1340188784Ssam			case 'q':		/* 1/4-width channels */
1341188784Ssam				flags |= IEEE80211_CHAN_QUARTER;
1342188784Ssam				break;
1343178354Ssam			default:
1344178354Ssam				errx(-1, "%s: Invalid mode attribute %c\n",
1345178354Ssam				    val, *cp);
1346178354Ssam			}
1347178354Ssam		}
1348178354Ssam	}
1349178354Ssam	return flags;
1350170531Ssam}
1351170531Ssam
1352178354Ssam#define	IEEE80211_CHAN_HTA	(IEEE80211_CHAN_HT|IEEE80211_CHAN_5GHZ)
1353178354Ssam#define	IEEE80211_CHAN_HTG	(IEEE80211_CHAN_HT|IEEE80211_CHAN_2GHZ)
1354178354Ssam
1355178354Ssam#define	_APPLY(_flags, _base, _param, _v) do {				\
1356178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1357178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1358178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1359178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1360178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1361178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1362178354Ssam	    else							\
1363178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1364178354Ssam    }									\
1365178354Ssam    if (_flags & IEEE80211_CHAN_TURBO) {				\
1366178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1367178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;	\
1368178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;	\
1369178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1370178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;	\
1371178354Ssam	    else							\
1372178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;	\
1373178354Ssam    }									\
1374178354Ssam    if (_flags & IEEE80211_CHAN_STURBO)					\
1375178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = _v;		\
1376178354Ssam    if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1377178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = _v;		\
1378178354Ssam    if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1379178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = _v;		\
1380178354Ssam    if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1381178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = _v;		\
1382188784Ssam    if (_flags & IEEE80211_CHAN_HALF)					\
1383188784Ssam	    _base.params[IEEE80211_MODE_HALF]._param = _v;		\
1384188784Ssam    if (_flags & IEEE80211_CHAN_QUARTER)				\
1385188784Ssam	    _base.params[IEEE80211_MODE_QUARTER]._param = _v;		\
1386178354Ssam} while (0)
1387178354Ssam#define	_APPLY1(_flags, _base, _param, _v) do {				\
1388178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1389178354Ssam	    if (_flags & IEEE80211_CHAN_5GHZ)				\
1390178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1391178354Ssam	    else							\
1392178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1393178354Ssam    } else if ((_flags & IEEE80211_CHAN_108A) == IEEE80211_CHAN_108A)	\
1394178354Ssam	    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;		\
1395178354Ssam    else if ((_flags & IEEE80211_CHAN_108G) == IEEE80211_CHAN_108G)	\
1396178354Ssam	    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;		\
1397178354Ssam    else if ((_flags & IEEE80211_CHAN_ST) == IEEE80211_CHAN_ST)		\
1398178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = _v;		\
1399188784Ssam    else if (_flags & IEEE80211_CHAN_HALF)				\
1400188784Ssam	    _base.params[IEEE80211_MODE_HALF]._param = _v;		\
1401188784Ssam    else if (_flags & IEEE80211_CHAN_QUARTER)				\
1402188784Ssam	    _base.params[IEEE80211_MODE_QUARTER]._param = _v;		\
1403178354Ssam    else if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1404178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = _v;		\
1405178354Ssam    else if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1406178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = _v;		\
1407178354Ssam    else if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1408178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = _v;		\
1409178354Ssam} while (0)
1410178354Ssam#define	_APPLY_RATE(_flags, _base, _param, _v) do {			\
1411178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1412188784Ssam	(_v) = (_v / 2) | IEEE80211_RATE_MCS;				\
1413178354Ssam    }									\
1414188784Ssam    _APPLY(_flags, _base, _param, _v);					\
1415178354Ssam} while (0)
1416178354Ssam#define	_APPLY_RATE1(_flags, _base, _param, _v) do {			\
1417178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1418188784Ssam	(_v) = (_v / 2) | IEEE80211_RATE_MCS;				\
1419188784Ssam    }									\
1420188784Ssam    _APPLY1(_flags, _base, _param, _v);					\
1421178354Ssam} while (0)
1422178354Ssam
1423170531Ssamstatic
1424178354SsamDECL_CMD_FUNC(set80211roamrssi, val, d)
1425170531Ssam{
1426178354Ssam	double v = atof(val);
1427178354Ssam	int rssi, flags;
1428178354Ssam
1429178354Ssam	rssi = (int) (2*v);
1430178354Ssam	if (rssi != 2*v)
1431178354Ssam		errx(-1, "invalid rssi (must be .5 dBm units)");
1432178354Ssam	flags = getmodeflags(val);
1433178354Ssam	getroam(s);
1434178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1435178354Ssam		flags = getcurchan(s)->ic_flags;
1436178354Ssam		_APPLY1(flags, roamparams, rssi, rssi);
1437178354Ssam	} else
1438178354Ssam		_APPLY(flags, roamparams, rssi, rssi);
1439178354Ssam	callback_register(setroam_cb, &roamparams);
1440170531Ssam}
1441170531Ssam
1442188784Ssamstatic int
1443188784Ssamgetrate(const char *val, const char *tag)
1444188784Ssam{
1445188784Ssam	double v = atof(val);
1446188784Ssam	int rate;
1447188784Ssam
1448188784Ssam	rate = (int) (2*v);
1449188784Ssam	if (rate != 2*v)
1450188784Ssam		errx(-1, "invalid %s rate (must be .5 Mb/s units)", tag);
1451188784Ssam	return rate;		/* NB: returns 2x the specified value */
1452188784Ssam}
1453188784Ssam
1454170531Ssamstatic
1455178354SsamDECL_CMD_FUNC(set80211roamrate, val, d)
1456170531Ssam{
1457188784Ssam	int rate, flags;
1458178354Ssam
1459188784Ssam	rate = getrate(val, "roam");
1460178354Ssam	flags = getmodeflags(val);
1461178354Ssam	getroam(s);
1462178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1463178354Ssam		flags = getcurchan(s)->ic_flags;
1464188784Ssam		_APPLY_RATE1(flags, roamparams, rate, rate);
1465178354Ssam	} else
1466188784Ssam		_APPLY_RATE(flags, roamparams, rate, rate);
1467178354Ssam	callback_register(setroam_cb, &roamparams);
1468170531Ssam}
1469170531Ssam
1470170531Ssamstatic
1471178354SsamDECL_CMD_FUNC(set80211mcastrate, val, d)
1472170531Ssam{
1473188784Ssam	int rate, flags;
1474178354Ssam
1475188784Ssam	rate = getrate(val, "mcast");
1476178354Ssam	flags = getmodeflags(val);
1477178354Ssam	gettxparams(s);
1478178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1479178354Ssam		flags = getcurchan(s)->ic_flags;
1480188784Ssam		_APPLY_RATE1(flags, txparams, mcastrate, rate);
1481178354Ssam	} else
1482188784Ssam		_APPLY_RATE(flags, txparams, mcastrate, rate);
1483178354Ssam	callback_register(settxparams_cb, &txparams);
1484170531Ssam}
1485170531Ssam
1486170531Ssamstatic
1487178354SsamDECL_CMD_FUNC(set80211mgtrate, val, d)
1488170531Ssam{
1489188784Ssam	int rate, flags;
1490178354Ssam
1491188784Ssam	rate = getrate(val, "mgmt");
1492178354Ssam	flags = getmodeflags(val);
1493178354Ssam	gettxparams(s);
1494178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1495178354Ssam		flags = getcurchan(s)->ic_flags;
1496188784Ssam		_APPLY_RATE1(flags, txparams, mgmtrate, rate);
1497178354Ssam	} else
1498188784Ssam		_APPLY_RATE(flags, txparams, mgmtrate, rate);
1499178354Ssam	callback_register(settxparams_cb, &txparams);
1500170531Ssam}
1501170531Ssam
1502170531Ssamstatic
1503178354SsamDECL_CMD_FUNC(set80211ucastrate, val, d)
1504170531Ssam{
1505188784Ssam	int flags;
1506178354Ssam
1507178354Ssam	gettxparams(s);
1508178354Ssam	flags = getmodeflags(val);
1509178354Ssam	if (isanyarg(val)) {
1510178354Ssam		if (flags == 0) {	/* NB: no flags => current channel */
1511178354Ssam			flags = getcurchan(s)->ic_flags;
1512178354Ssam			_APPLY1(flags, txparams, ucastrate,
1513178354Ssam			    IEEE80211_FIXED_RATE_NONE);
1514178354Ssam		} else
1515178354Ssam			_APPLY(flags, txparams, ucastrate,
1516178354Ssam			    IEEE80211_FIXED_RATE_NONE);
1517178354Ssam	} else {
1518188784Ssam		int rate = getrate(val, "ucast");
1519178354Ssam		if (flags == 0) {	/* NB: no flags => current channel */
1520178354Ssam			flags = getcurchan(s)->ic_flags;
1521188784Ssam			_APPLY_RATE1(flags, txparams, ucastrate, rate);
1522178354Ssam		} else
1523188784Ssam			_APPLY_RATE(flags, txparams, ucastrate, rate);
1524178354Ssam	}
1525178354Ssam	callback_register(settxparams_cb, &txparams);
1526170531Ssam}
1527170531Ssam
1528170531Ssamstatic
1529178354SsamDECL_CMD_FUNC(set80211maxretry, val, d)
1530153354Ssam{
1531178354Ssam	int v = atoi(val), flags;
1532178354Ssam
1533178354Ssam	flags = getmodeflags(val);
1534178354Ssam	gettxparams(s);
1535178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1536178354Ssam		flags = getcurchan(s)->ic_flags;
1537178354Ssam		_APPLY1(flags, txparams, maxretry, v);
1538178354Ssam	} else
1539178354Ssam		_APPLY(flags, txparams, maxretry, v);
1540178354Ssam	callback_register(settxparams_cb, &txparams);
1541153354Ssam}
1542178354Ssam#undef _APPLY_RATE
1543178354Ssam#undef _APPLY
1544178354Ssam#undef IEEE80211_CHAN_HTA
1545178354Ssam#undef IEEE80211_CHAN_HTG
1546153354Ssam
1547153354Ssamstatic
1548148416SsamDECL_CMD_FUNC(set80211fragthreshold, val, d)
1549148416Ssam{
1550148416Ssam	set80211(s, IEEE80211_IOC_FRAGTHRESHOLD,
1551148416Ssam		isundefarg(val) ? IEEE80211_FRAG_MAX : atoi(val), 0, NULL);
1552148416Ssam}
1553148416Ssam
1554160687Ssamstatic
1555160687SsamDECL_CMD_FUNC(set80211bmissthreshold, val, d)
1556160687Ssam{
1557160687Ssam	set80211(s, IEEE80211_IOC_BMISSTHRESHOLD,
1558160687Ssam		isundefarg(val) ? IEEE80211_HWBMISS_MAX : atoi(val), 0, NULL);
1559160687Ssam}
1560160687Ssam
1561170531Ssamstatic void
1562170531Ssamset80211burst(const char *val, int d, int s, const struct afswtch *rafp)
1563170531Ssam{
1564170531Ssam	set80211(s, IEEE80211_IOC_BURST, d, 0, NULL);
1565170531Ssam}
1566170531Ssam
1567170531Ssamstatic void
1568170531Ssamset80211doth(const char *val, int d, int s, const struct afswtch *rafp)
1569170531Ssam{
1570170531Ssam	set80211(s, IEEE80211_IOC_DOTH, d, 0, NULL);
1571170531Ssam}
1572170531Ssam
1573173275Ssamstatic void
1574178354Ssamset80211dfs(const char *val, int d, int s, const struct afswtch *rafp)
1575178354Ssam{
1576178354Ssam	set80211(s, IEEE80211_IOC_DFS, d, 0, NULL);
1577178354Ssam}
1578178354Ssam
1579178354Ssamstatic void
1580173275Ssamset80211shortgi(const char *val, int d, int s, const struct afswtch *rafp)
1581173275Ssam{
1582173275Ssam	set80211(s, IEEE80211_IOC_SHORTGI,
1583173275Ssam		d ? (IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40) : 0,
1584173275Ssam		0, NULL);
1585173275Ssam}
1586173275Ssam
1587173275Ssamstatic void
1588173275Ssamset80211ampdu(const char *val, int d, int s, const struct afswtch *rafp)
1589173275Ssam{
1590173275Ssam	int ampdu;
1591173275Ssam
1592173275Ssam	if (get80211val(s, IEEE80211_IOC_AMPDU, &ampdu) < 0)
1593173275Ssam		errx(-1, "cannot get AMPDU setting");
1594173275Ssam	if (d < 0) {
1595173275Ssam		d = -d;
1596173275Ssam		ampdu &= ~d;
1597173275Ssam	} else
1598173275Ssam		ampdu |= d;
1599173275Ssam	set80211(s, IEEE80211_IOC_AMPDU, ampdu, 0, NULL);
1600173275Ssam}
1601173275Ssam
1602173275Ssamstatic
1603173275SsamDECL_CMD_FUNC(set80211ampdulimit, val, d)
1604173275Ssam{
1605173275Ssam	int v;
1606173275Ssam
1607173275Ssam	switch (atoi(val)) {
1608173275Ssam	case 8:
1609173275Ssam	case 8*1024:
1610173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_8K;
1611173275Ssam		break;
1612173275Ssam	case 16:
1613173275Ssam	case 16*1024:
1614173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_16K;
1615173275Ssam		break;
1616173275Ssam	case 32:
1617173275Ssam	case 32*1024:
1618173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_32K;
1619173275Ssam		break;
1620173275Ssam	case 64:
1621173275Ssam	case 64*1024:
1622173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_64K;
1623173275Ssam		break;
1624173275Ssam	default:
1625173275Ssam		errx(-1, "invalid A-MPDU limit %s", val);
1626173275Ssam	}
1627173275Ssam	set80211(s, IEEE80211_IOC_AMPDU_LIMIT, v, 0, NULL);
1628173275Ssam}
1629173275Ssam
1630173275Ssamstatic
1631173275SsamDECL_CMD_FUNC(set80211ampdudensity, val, d)
1632173275Ssam{
1633173275Ssam	int v;
1634173275Ssam
1635183260Ssam	if (isanyarg(val) || strcasecmp(val, "na") == 0)
1636173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1637173275Ssam	else switch ((int)(atof(val)*4)) {
1638173275Ssam	case 0:
1639173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1640173275Ssam		break;
1641173275Ssam	case 1:
1642173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_025;
1643173275Ssam		break;
1644173275Ssam	case 2:
1645173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_05;
1646173275Ssam		break;
1647173275Ssam	case 4:
1648173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_1;
1649173275Ssam		break;
1650173275Ssam	case 8:
1651173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_2;
1652173275Ssam		break;
1653173275Ssam	case 16:
1654173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_4;
1655173275Ssam		break;
1656173275Ssam	case 32:
1657173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_8;
1658173275Ssam		break;
1659173275Ssam	case 64:
1660173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_16;
1661173275Ssam		break;
1662173275Ssam	default:
1663173275Ssam		errx(-1, "invalid A-MPDU density %s", val);
1664173275Ssam	}
1665173275Ssam	set80211(s, IEEE80211_IOC_AMPDU_DENSITY, v, 0, NULL);
1666173275Ssam}
1667173275Ssam
1668173275Ssamstatic void
1669173275Ssamset80211amsdu(const char *val, int d, int s, const struct afswtch *rafp)
1670173275Ssam{
1671173275Ssam	int amsdu;
1672173275Ssam
1673173275Ssam	if (get80211val(s, IEEE80211_IOC_AMSDU, &amsdu) < 0)
1674187801Ssam		err(-1, "cannot get AMSDU setting");
1675173275Ssam	if (d < 0) {
1676173275Ssam		d = -d;
1677173275Ssam		amsdu &= ~d;
1678173275Ssam	} else
1679173275Ssam		amsdu |= d;
1680173275Ssam	set80211(s, IEEE80211_IOC_AMSDU, amsdu, 0, NULL);
1681173275Ssam}
1682173275Ssam
1683173275Ssamstatic
1684173275SsamDECL_CMD_FUNC(set80211amsdulimit, val, d)
1685173275Ssam{
1686173275Ssam	set80211(s, IEEE80211_IOC_AMSDU_LIMIT, atoi(val), 0, NULL);
1687173275Ssam}
1688173275Ssam
1689173275Ssamstatic void
1690173275Ssamset80211puren(const char *val, int d, int s, const struct afswtch *rafp)
1691173275Ssam{
1692173275Ssam	set80211(s, IEEE80211_IOC_PUREN, d, 0, NULL);
1693173275Ssam}
1694173275Ssam
1695173275Ssamstatic void
1696173275Ssamset80211htcompat(const char *val, int d, int s, const struct afswtch *rafp)
1697173275Ssam{
1698173275Ssam	set80211(s, IEEE80211_IOC_HTCOMPAT, d, 0, NULL);
1699173275Ssam}
1700173275Ssam
1701173275Ssamstatic void
1702173275Ssamset80211htconf(const char *val, int d, int s, const struct afswtch *rafp)
1703173275Ssam{
1704173275Ssam	set80211(s, IEEE80211_IOC_HTCONF, d, 0, NULL);
1705173275Ssam	htconf = d;
1706173275Ssam}
1707173275Ssam
1708173275Ssamstatic void
1709178354Ssamset80211dwds(const char *val, int d, int s, const struct afswtch *rafp)
1710178354Ssam{
1711178354Ssam	set80211(s, IEEE80211_IOC_DWDS, d, 0, NULL);
1712178354Ssam}
1713178354Ssam
1714178354Ssamstatic void
1715173275Ssamset80211inact(const char *val, int d, int s, const struct afswtch *rafp)
1716173275Ssam{
1717173275Ssam	set80211(s, IEEE80211_IOC_INACTIVITY, d, 0, NULL);
1718173275Ssam}
1719173275Ssam
1720173275Ssamstatic void
1721178354Ssamset80211tsn(const char *val, int d, int s, const struct afswtch *rafp)
1722178354Ssam{
1723178354Ssam	set80211(s, IEEE80211_IOC_TSN, d, 0, NULL);
1724178354Ssam}
1725178354Ssam
1726178354Ssamstatic void
1727178354Ssamset80211dotd(const char *val, int d, int s, const struct afswtch *rafp)
1728178354Ssam{
1729178354Ssam	set80211(s, IEEE80211_IOC_DOTD, d, 0, NULL);
1730178354Ssam}
1731178354Ssam
1732183261Ssamstatic void
1733183261Ssamset80211smps(const char *val, int d, int s, const struct afswtch *rafp)
1734183261Ssam{
1735183261Ssam	set80211(s, IEEE80211_IOC_SMPS, d, 0, NULL);
1736183261Ssam}
1737183261Ssam
1738183261Ssamstatic void
1739183261Ssamset80211rifs(const char *val, int d, int s, const struct afswtch *rafp)
1740183261Ssam{
1741183261Ssam	set80211(s, IEEE80211_IOC_RIFS, d, 0, NULL);
1742183261Ssam}
1743183261Ssam
1744186904Ssamstatic
1745186904SsamDECL_CMD_FUNC(set80211tdmaslot, val, d)
1746186904Ssam{
1747186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOT, atoi(val), 0, NULL);
1748186904Ssam}
1749186904Ssam
1750186904Ssamstatic
1751186904SsamDECL_CMD_FUNC(set80211tdmaslotcnt, val, d)
1752186904Ssam{
1753186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOTCNT, atoi(val), 0, NULL);
1754186904Ssam}
1755186904Ssam
1756186904Ssamstatic
1757186904SsamDECL_CMD_FUNC(set80211tdmaslotlen, val, d)
1758186904Ssam{
1759186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOTLEN, atoi(val), 0, NULL);
1760186904Ssam}
1761186904Ssam
1762186904Ssamstatic
1763186904SsamDECL_CMD_FUNC(set80211tdmabintval, val, d)
1764186904Ssam{
1765186904Ssam	set80211(s, IEEE80211_IOC_TDMA_BINTERVAL, atoi(val), 0, NULL);
1766186904Ssam}
1767186904Ssam
1768178354Ssamstatic int
1769178354Ssamregdomain_sort(const void *a, const void *b)
1770178354Ssam{
1771178354Ssam#define	CHAN_ALL \
1772178354Ssam	(IEEE80211_CHAN_ALLTURBO|IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)
1773178354Ssam	const struct ieee80211_channel *ca = a;
1774178354Ssam	const struct ieee80211_channel *cb = b;
1775178354Ssam
1776178354Ssam	return ca->ic_freq == cb->ic_freq ?
1777178354Ssam	    (ca->ic_flags & CHAN_ALL) - (cb->ic_flags & CHAN_ALL) :
1778178354Ssam	    ca->ic_freq - cb->ic_freq;
1779178354Ssam#undef CHAN_ALL
1780178354Ssam}
1781178354Ssam
1782178354Ssamstatic const struct ieee80211_channel *
1783178354Ssamchanlookup(const struct ieee80211_channel chans[], int nchans,
1784178354Ssam	int freq, int flags)
1785178354Ssam{
1786178354Ssam	int i;
1787178354Ssam
1788178354Ssam	flags &= IEEE80211_CHAN_ALLTURBO;
1789178354Ssam	for (i = 0; i < nchans; i++) {
1790178354Ssam		const struct ieee80211_channel *c = &chans[i];
1791178354Ssam		if (c->ic_freq == freq &&
1792178354Ssam		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1793178354Ssam			return c;
1794178354Ssam	}
1795178354Ssam	return NULL;
1796178354Ssam}
1797178354Ssam
1798187841Ssamstatic int
1799187841Ssamchanfind(const struct ieee80211_channel chans[], int nchans, int flags)
1800187841Ssam{
1801187841Ssam	int i;
1802187841Ssam
1803187841Ssam	for (i = 0; i < nchans; i++) {
1804187841Ssam		const struct ieee80211_channel *c = &chans[i];
1805187841Ssam		if ((c->ic_flags & flags) == flags)
1806187841Ssam			return 1;
1807187841Ssam	}
1808187841Ssam	return 0;
1809187841Ssam}
1810187841Ssam
1811188258Ssam/*
1812188258Ssam * Check channel compatibility.
1813188258Ssam */
1814188258Ssamstatic int
1815188258Ssamcheckchan(const struct ieee80211req_chaninfo *avail, int freq, int flags)
1816188258Ssam{
1817188258Ssam	flags &= ~REQ_FLAGS;
1818188258Ssam	/*
1819188258Ssam	 * Check if exact channel is in the calibration table;
1820188258Ssam	 * everything below is to deal with channels that we
1821188258Ssam	 * want to include but that are not explicitly listed.
1822188258Ssam	 */
1823188258Ssam	if (flags & IEEE80211_CHAN_HT40) {
1824188258Ssam		/* NB: we use an HT40 channel center that matches HT20 */
1825188258Ssam		flags = (flags &~ IEEE80211_CHAN_HT40) | IEEE80211_CHAN_HT20;
1826188258Ssam	}
1827188258Ssam	if (chanlookup(avail->ic_chans, avail->ic_nchans, freq, flags) != NULL)
1828188258Ssam		return 1;
1829188258Ssam	if (flags & IEEE80211_CHAN_GSM) {
1830188258Ssam		/*
1831188258Ssam		 * XXX GSM frequency mapping is handled in the kernel
1832188258Ssam		 * so we cannot find them in the calibration table;
1833188258Ssam		 * just accept the channel and the kernel will reject
1834188258Ssam		 * the channel list if it's wrong.
1835188258Ssam		 */
1836188258Ssam		return 1;
1837188258Ssam	}
1838188258Ssam	/*
1839188258Ssam	 * If this is a 1/2 or 1/4 width channel allow it if a full
1840188258Ssam	 * width channel is present for this frequency, and the device
1841188258Ssam	 * supports fractional channels on this band.  This is a hack
1842188258Ssam	 * that avoids bloating the calibration table; it may be better
1843188258Ssam	 * by per-band attributes though (we are effectively calculating
1844188258Ssam	 * this attribute by scanning the channel list ourself).
1845188258Ssam	 */
1846188258Ssam	if ((flags & (IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)) == 0)
1847188258Ssam		return 0;
1848188258Ssam	if (chanlookup(avail->ic_chans, avail->ic_nchans, freq,
1849188258Ssam	    flags &~ (IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)) == NULL)
1850188258Ssam		return 0;
1851188258Ssam	if (flags & IEEE80211_CHAN_HALF) {
1852188258Ssam		return chanfind(avail->ic_chans, avail->ic_nchans,
1853188258Ssam		    IEEE80211_CHAN_HALF |
1854188258Ssam		       (flags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ)));
1855188258Ssam	} else {
1856188258Ssam		return chanfind(avail->ic_chans, avail->ic_nchans,
1857188258Ssam		    IEEE80211_CHAN_QUARTER |
1858188258Ssam			(flags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ)));
1859188258Ssam	}
1860188258Ssam}
1861188258Ssam
1862178354Ssamstatic void
1863178354Ssamregdomain_addchans(struct ieee80211req_chaninfo *ci,
1864178354Ssam	const netband_head *bands,
1865178354Ssam	const struct ieee80211_regdomain *reg,
1866178354Ssam	uint32_t chanFlags,
1867178354Ssam	const struct ieee80211req_chaninfo *avail)
1868178354Ssam{
1869178354Ssam	const struct netband *nb;
1870178354Ssam	const struct freqband *b;
1871178354Ssam	struct ieee80211_channel *c, *prev;
1872188258Ssam	int freq, hi_adj, lo_adj, channelSep;
1873188258Ssam	uint32_t flags;
1874178354Ssam
1875188258Ssam	hi_adj = (chanFlags & IEEE80211_CHAN_HT40U) ? -20 : 0;
1876188258Ssam	lo_adj = (chanFlags & IEEE80211_CHAN_HT40D) ? 20 : 0;
1877178354Ssam	channelSep = (chanFlags & IEEE80211_CHAN_2GHZ) ? 0 : 40;
1878178354Ssam	LIST_FOREACH(nb, bands, next) {
1879178354Ssam		b = nb->band;
1880187842Ssam		if (verbose) {
1881187842Ssam			printf("%s:", __func__);
1882187842Ssam			printb(" chanFlags", chanFlags, IEEE80211_CHAN_BITS);
1883187842Ssam			printb(" bandFlags", nb->flags | b->flags,
1884187842Ssam			    IEEE80211_CHAN_BITS);
1885187842Ssam			putchar('\n');
1886187842Ssam		}
1887178354Ssam		prev = NULL;
1888188258Ssam		for (freq = b->freqStart + lo_adj;
1889188258Ssam		     freq <= b->freqEnd + hi_adj; freq += b->chanSep) {
1890187841Ssam			/*
1891188258Ssam			 * Construct flags for the new channel.  We take
1892188258Ssam			 * the attributes from the band descriptions except
1893188258Ssam			 * for HT40 which is enabled generically (i.e. +/-
1894188258Ssam			 * extension channel) in the band description and
1895188258Ssam			 * then constrained according by channel separation.
1896187841Ssam			 */
1897188258Ssam			flags = nb->flags | b->flags;
1898188258Ssam			if (flags & IEEE80211_CHAN_HT) {
1899188258Ssam				/*
1900188258Ssam				 * HT channels are generated specially; we're
1901188258Ssam				 * called to add HT20, HT40+, and HT40- chan's
1902188258Ssam				 * so we need to expand only band specs for
1903188258Ssam				 * the HT channel type being added.
1904188258Ssam				 */
1905188258Ssam				if ((chanFlags & IEEE80211_CHAN_HT20) &&
1906188258Ssam				    (flags & IEEE80211_CHAN_HT20) == 0) {
1907188258Ssam					if (verbose)
1908188258Ssam						printf("%u: skip, not an "
1909188258Ssam						    "HT20 channel\n", freq);
1910188258Ssam					continue;
1911188258Ssam				}
1912188258Ssam				if ((chanFlags & IEEE80211_CHAN_HT40) &&
1913188258Ssam				    (flags & IEEE80211_CHAN_HT40) == 0) {
1914188258Ssam					if (verbose)
1915188258Ssam						printf("%u: skip, not an "
1916188258Ssam						    "HT40 channel\n", freq);
1917188258Ssam					continue;
1918188258Ssam				}
1919188258Ssam				/*
1920188258Ssam				 * DFS and HT40 don't mix.  This should be
1921188258Ssam				 * expressed in the regdomain database but
1922188258Ssam				 * just in case enforce it here.
1923188258Ssam				 */
1924188258Ssam				if ((chanFlags & IEEE80211_CHAN_HT40) &&
1925188258Ssam				    (flags & IEEE80211_CHAN_DFS)) {
1926188258Ssam					if (verbose)
1927188258Ssam						printf("%u: skip, HT40+DFS "
1928188258Ssam						    "not permitted\n", freq);
1929188258Ssam					continue;
1930188258Ssam				}
1931188258Ssam				/* NB: HT attribute comes from caller */
1932188258Ssam				flags &= ~IEEE80211_CHAN_HT;
1933188258Ssam				flags |= chanFlags & IEEE80211_CHAN_HT;
1934188258Ssam			}
1935188258Ssam			/*
1936188258Ssam			 * Check if device can operate on this frequency.
1937188258Ssam			 */
1938188258Ssam			if (!checkchan(avail, freq, flags)) {
1939187842Ssam				if (verbose) {
1940187842Ssam					printf("%u: skip, ", freq);
1941188258Ssam					printb("flags", flags,
1942187842Ssam					    IEEE80211_CHAN_BITS);
1943187842Ssam					printf(" not available\n");
1944187842Ssam				}
1945178354Ssam				continue;
1946178354Ssam			}
1947188258Ssam			if ((flags & REQ_ECM) && !reg->ecm) {
1948178354Ssam				if (verbose)
1949188258Ssam					printf("%u: skip, ECM channel\n", freq);
1950178354Ssam				continue;
1951178354Ssam			}
1952188258Ssam			if ((flags & REQ_INDOOR) && reg->location == 'O') {
1953178354Ssam				if (verbose)
1954188258Ssam					printf("%u: skip, indoor channel\n",
1955187841Ssam					    freq);
1956178354Ssam				continue;
1957178354Ssam			}
1958178354Ssam			if ((flags & REQ_OUTDOOR) && reg->location == 'I') {
1959178354Ssam				if (verbose)
1960188258Ssam					printf("%u: skip, outdoor channel\n",
1961188258Ssam					    freq);
1962178354Ssam				continue;
1963178354Ssam			}
1964178354Ssam			if ((flags & IEEE80211_CHAN_HT40) &&
1965178354Ssam			    prev != NULL && (freq - prev->ic_freq) < channelSep) {
1966178354Ssam				if (verbose)
1967178354Ssam					printf("%u: skip, only %u channel "
1968178354Ssam					    "separation, need %d\n", freq,
1969178354Ssam					    freq - prev->ic_freq, channelSep);
1970178354Ssam				continue;
1971178354Ssam			}
1972178354Ssam			if (ci->ic_nchans == IEEE80211_CHAN_MAX) {
1973178354Ssam				if (verbose)
1974187842Ssam					printf("%u: skip, channel table full\n",
1975187842Ssam					    freq);
1976178354Ssam				break;
1977178354Ssam			}
1978178354Ssam			c = &ci->ic_chans[ci->ic_nchans++];
1979187801Ssam			memset(c, 0, sizeof(*c));
1980178354Ssam			c->ic_freq = freq;
1981188258Ssam			c->ic_flags = flags;
1982178354Ssam			if (c->ic_flags & IEEE80211_CHAN_DFS)
1983178354Ssam				c->ic_maxregpower = nb->maxPowerDFS;
1984178354Ssam			else
1985178354Ssam				c->ic_maxregpower = nb->maxPower;
1986187842Ssam			if (verbose) {
1987187842Ssam				printf("[%3d] add freq %u ",
1988187842Ssam				    ci->ic_nchans-1, c->ic_freq);
1989187842Ssam				printb("flags", c->ic_flags, IEEE80211_CHAN_BITS);
1990187842Ssam				printf(" power %u\n", c->ic_maxregpower);
1991187842Ssam			}
1992178354Ssam			/* NB: kernel fills in other fields */
1993178354Ssam			prev = c;
1994178354Ssam		}
1995178354Ssam	}
1996178354Ssam}
1997178354Ssam
1998178354Ssamstatic void
1999178354Ssamregdomain_makechannels(
2000178354Ssam	struct ieee80211_regdomain_req *req,
2001178354Ssam	const struct ieee80211_devcaps_req *dc)
2002178354Ssam{
2003178354Ssam	struct regdata *rdp = getregdata();
2004178354Ssam	const struct country *cc;
2005178354Ssam	const struct ieee80211_regdomain *reg = &req->rd;
2006178354Ssam	struct ieee80211req_chaninfo *ci = &req->chaninfo;
2007178354Ssam	const struct regdomain *rd;
2008178354Ssam
2009178354Ssam	/*
2010178354Ssam	 * Locate construction table for new channel list.  We treat
2011178354Ssam	 * the regdomain/SKU as definitive so a country can be in
2012178354Ssam	 * multiple with different properties (e.g. US in FCC+FCC3).
2013178354Ssam	 * If no regdomain is specified then we fallback on the country
2014178354Ssam	 * code to find the associated regdomain since countries always
2015178354Ssam	 * belong to at least one regdomain.
2016178354Ssam	 */
2017178354Ssam	if (reg->regdomain == 0) {
2018178354Ssam		cc = lib80211_country_findbycc(rdp, reg->country);
2019178354Ssam		if (cc == NULL)
2020178354Ssam			errx(1, "internal error, country %d not found",
2021178354Ssam			    reg->country);
2022178354Ssam		rd = cc->rd;
2023178354Ssam	} else
2024178354Ssam		rd = lib80211_regdomain_findbysku(rdp, reg->regdomain);
2025178354Ssam	if (rd == NULL)
2026178354Ssam		errx(1, "internal error, regdomain %d not found",
2027178354Ssam			    reg->regdomain);
2028178354Ssam	if (rd->sku != SKU_DEBUG) {
2029187801Ssam		/*
2030187801Ssam		 * regdomain_addchans incrememnts the channel count for
2031187801Ssam		 * each channel it adds so initialize ic_nchans to zero.
2032187801Ssam		 * Note that we know we have enough space to hold all possible
2033187801Ssam		 * channels because the devcaps list size was used to
2034187801Ssam		 * allocate our request.
2035187801Ssam		 */
2036187801Ssam		ci->ic_nchans = 0;
2037178354Ssam		if (!LIST_EMPTY(&rd->bands_11b))
2038178354Ssam			regdomain_addchans(ci, &rd->bands_11b, reg,
2039178354Ssam			    IEEE80211_CHAN_B, &dc->dc_chaninfo);
2040187841Ssam		if (!LIST_EMPTY(&rd->bands_11g))
2041178354Ssam			regdomain_addchans(ci, &rd->bands_11g, reg,
2042178354Ssam			    IEEE80211_CHAN_G, &dc->dc_chaninfo);
2043187841Ssam		if (!LIST_EMPTY(&rd->bands_11a))
2044178354Ssam			regdomain_addchans(ci, &rd->bands_11a, reg,
2045178354Ssam			    IEEE80211_CHAN_A, &dc->dc_chaninfo);
2046188258Ssam		if (!LIST_EMPTY(&rd->bands_11na) && dc->dc_htcaps != 0) {
2047178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
2048178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT20,
2049178354Ssam			    &dc->dc_chaninfo);
2050188258Ssam			if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2051188258Ssam				regdomain_addchans(ci, &rd->bands_11na, reg,
2052188258Ssam				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U,
2053188258Ssam				    &dc->dc_chaninfo);
2054188258Ssam				regdomain_addchans(ci, &rd->bands_11na, reg,
2055188258Ssam				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D,
2056188258Ssam				    &dc->dc_chaninfo);
2057188258Ssam			}
2058178354Ssam		}
2059188258Ssam		if (!LIST_EMPTY(&rd->bands_11ng) && dc->dc_htcaps != 0) {
2060178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
2061178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT20,
2062178354Ssam			    &dc->dc_chaninfo);
2063188258Ssam			if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2064188258Ssam				regdomain_addchans(ci, &rd->bands_11ng, reg,
2065188258Ssam				    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U,
2066188258Ssam				    &dc->dc_chaninfo);
2067188258Ssam				regdomain_addchans(ci, &rd->bands_11ng, reg,
2068188258Ssam				    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D,
2069188258Ssam				    &dc->dc_chaninfo);
2070188258Ssam			}
2071178354Ssam		}
2072178354Ssam		qsort(ci->ic_chans, ci->ic_nchans, sizeof(ci->ic_chans[0]),
2073178354Ssam		    regdomain_sort);
2074178354Ssam	} else
2075187801Ssam		memcpy(ci, &dc->dc_chaninfo,
2076187801Ssam		    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
2077178354Ssam}
2078178354Ssam
2079178354Ssamstatic void
2080178354Ssamlist_countries(void)
2081178354Ssam{
2082178354Ssam	struct regdata *rdp = getregdata();
2083178354Ssam	const struct country *cp;
2084178354Ssam	const struct regdomain *dp;
2085178354Ssam	int i;
2086178354Ssam
2087178354Ssam	i = 0;
2088178354Ssam	printf("\nCountry codes:\n");
2089178354Ssam	LIST_FOREACH(cp, &rdp->countries, next) {
2090178354Ssam		printf("%2s %-15.15s%s", cp->isoname,
2091178354Ssam		    cp->name, ((i+1)%4) == 0 ? "\n" : " ");
2092178354Ssam		i++;
2093178354Ssam	}
2094178354Ssam	i = 0;
2095178354Ssam	printf("\nRegulatory domains:\n");
2096178354Ssam	LIST_FOREACH(dp, &rdp->domains, next) {
2097178354Ssam		printf("%-15.15s%s", dp->name, ((i+1)%4) == 0 ? "\n" : " ");
2098178354Ssam		i++;
2099178354Ssam	}
2100178354Ssam	printf("\n");
2101178354Ssam}
2102178354Ssam
2103178354Ssamstatic void
2104178354Ssamdefaultcountry(const struct regdomain *rd)
2105178354Ssam{
2106178354Ssam	struct regdata *rdp = getregdata();
2107178354Ssam	const struct country *cc;
2108178354Ssam
2109178354Ssam	cc = lib80211_country_findbycc(rdp, rd->cc->code);
2110178354Ssam	if (cc == NULL)
2111178354Ssam		errx(1, "internal error, ISO country code %d not "
2112178354Ssam		    "defined for regdomain %s", rd->cc->code, rd->name);
2113178354Ssam	regdomain.country = cc->code;
2114178354Ssam	regdomain.isocc[0] = cc->isoname[0];
2115178354Ssam	regdomain.isocc[1] = cc->isoname[1];
2116178354Ssam}
2117178354Ssam
2118178354Ssamstatic
2119178354SsamDECL_CMD_FUNC(set80211regdomain, val, d)
2120178354Ssam{
2121178354Ssam	struct regdata *rdp = getregdata();
2122178354Ssam	const struct regdomain *rd;
2123178354Ssam
2124178354Ssam	rd = lib80211_regdomain_findbyname(rdp, val);
2125178354Ssam	if (rd == NULL) {
2126186104Ssam		char *eptr;
2127186104Ssam		long sku = strtol(val, &eptr, 0);
2128186104Ssam
2129186104Ssam		if (eptr != val)
2130186104Ssam			rd = lib80211_regdomain_findbysku(rdp, sku);
2131186104Ssam		if (eptr == val || rd == NULL)
2132178354Ssam			errx(1, "unknown regdomain %s", val);
2133178354Ssam	}
2134178354Ssam	getregdomain(s);
2135178354Ssam	regdomain.regdomain = rd->sku;
2136178354Ssam	if (regdomain.country == 0 && rd->cc != NULL) {
2137178354Ssam		/*
2138178354Ssam		 * No country code setup and there's a default
2139178354Ssam		 * one for this regdomain fill it in.
2140178354Ssam		 */
2141178354Ssam		defaultcountry(rd);
2142178354Ssam	}
2143178354Ssam	callback_register(setregdomain_cb, &regdomain);
2144178354Ssam}
2145178354Ssam
2146178354Ssamstatic
2147178354SsamDECL_CMD_FUNC(set80211country, val, d)
2148178354Ssam{
2149178354Ssam	struct regdata *rdp = getregdata();
2150178354Ssam	const struct country *cc;
2151178354Ssam
2152178354Ssam	cc = lib80211_country_findbyname(rdp, val);
2153178354Ssam	if (cc == NULL) {
2154186103Ssam		char *eptr;
2155186103Ssam		long code = strtol(val, &eptr, 0);
2156186103Ssam
2157186103Ssam		if (eptr != val)
2158186103Ssam			cc = lib80211_country_findbycc(rdp, code);
2159186103Ssam		if (eptr == val || cc == NULL)
2160178354Ssam			errx(1, "unknown ISO country code %s", val);
2161178354Ssam	}
2162178354Ssam	getregdomain(s);
2163178354Ssam	regdomain.regdomain = cc->rd->sku;
2164178354Ssam	regdomain.country = cc->code;
2165178354Ssam	regdomain.isocc[0] = cc->isoname[0];
2166178354Ssam	regdomain.isocc[1] = cc->isoname[1];
2167178354Ssam	callback_register(setregdomain_cb, &regdomain);
2168178354Ssam}
2169178354Ssam
2170178354Ssamstatic void
2171178354Ssamset80211location(const char *val, int d, int s, const struct afswtch *rafp)
2172178354Ssam{
2173178354Ssam	getregdomain(s);
2174178354Ssam	regdomain.location = d;
2175178354Ssam	callback_register(setregdomain_cb, &regdomain);
2176178354Ssam}
2177178354Ssam
2178178354Ssamstatic void
2179178354Ssamset80211ecm(const char *val, int d, int s, const struct afswtch *rafp)
2180178354Ssam{
2181178354Ssam	getregdomain(s);
2182178354Ssam	regdomain.ecm = d;
2183178354Ssam	callback_register(setregdomain_cb, &regdomain);
2184178354Ssam}
2185178354Ssam
2186178354Ssamstatic void
2187173275SsamLINE_INIT(char c)
2188173275Ssam{
2189173275Ssam	spacer = c;
2190173275Ssam	if (c == '\t')
2191173275Ssam		col = 8;
2192173275Ssam	else
2193173275Ssam		col = 1;
2194173275Ssam}
2195173275Ssam
2196173275Ssamstatic void
2197173275SsamLINE_BREAK(void)
2198173275Ssam{
2199173275Ssam	if (spacer != '\t') {
2200173275Ssam		printf("\n");
2201173275Ssam		spacer = '\t';
2202173275Ssam	}
2203173275Ssam	col = 8;		/* 8-col tab */
2204173275Ssam}
2205173275Ssam
2206173275Ssamstatic void
2207173275SsamLINE_CHECK(const char *fmt, ...)
2208173275Ssam{
2209173275Ssam	char buf[80];
2210173275Ssam	va_list ap;
2211173275Ssam	int n;
2212173275Ssam
2213173275Ssam	va_start(ap, fmt);
2214173275Ssam	n = vsnprintf(buf+1, sizeof(buf)-1, fmt, ap);
2215173275Ssam	va_end(ap);
2216173275Ssam	col += 1+n;
2217173275Ssam	if (col > MAXCOL) {
2218173275Ssam		LINE_BREAK();
2219173275Ssam		col += n;
2220173275Ssam	}
2221173275Ssam	buf[0] = spacer;
2222173275Ssam	printf("%s", buf);
2223173275Ssam	spacer = ' ';
2224173275Ssam}
2225173275Ssam
2226138593Ssamstatic int
2227170531Ssamgetmaxrate(const uint8_t rates[15], uint8_t nrates)
2228138593Ssam{
2229138593Ssam	int i, maxrate = -1;
2230138593Ssam
2231138593Ssam	for (i = 0; i < nrates; i++) {
2232138593Ssam		int rate = rates[i] & IEEE80211_RATE_VAL;
2233138593Ssam		if (rate > maxrate)
2234138593Ssam			maxrate = rate;
2235138593Ssam	}
2236138593Ssam	return maxrate / 2;
2237138593Ssam}
2238138593Ssam
2239138593Ssamstatic const char *
2240138593Ssamgetcaps(int capinfo)
2241138593Ssam{
2242138593Ssam	static char capstring[32];
2243138593Ssam	char *cp = capstring;
2244138593Ssam
2245138593Ssam	if (capinfo & IEEE80211_CAPINFO_ESS)
2246138593Ssam		*cp++ = 'E';
2247138593Ssam	if (capinfo & IEEE80211_CAPINFO_IBSS)
2248138593Ssam		*cp++ = 'I';
2249138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
2250138593Ssam		*cp++ = 'c';
2251138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
2252138593Ssam		*cp++ = 'C';
2253138593Ssam	if (capinfo & IEEE80211_CAPINFO_PRIVACY)
2254138593Ssam		*cp++ = 'P';
2255138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
2256138593Ssam		*cp++ = 'S';
2257138593Ssam	if (capinfo & IEEE80211_CAPINFO_PBCC)
2258138593Ssam		*cp++ = 'B';
2259138593Ssam	if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
2260138593Ssam		*cp++ = 'A';
2261138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
2262138593Ssam		*cp++ = 's';
2263138593Ssam	if (capinfo & IEEE80211_CAPINFO_RSN)
2264138593Ssam		*cp++ = 'R';
2265138593Ssam	if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
2266138593Ssam		*cp++ = 'D';
2267138593Ssam	*cp = '\0';
2268138593Ssam	return capstring;
2269138593Ssam}
2270138593Ssam
2271159885Ssamstatic const char *
2272159885Ssamgetflags(int flags)
2273159885Ssam{
2274159885Ssam	static char flagstring[32];
2275159885Ssam	char *cp = flagstring;
2276159885Ssam
2277159885Ssam	if (flags & IEEE80211_NODE_AUTH)
2278159885Ssam		*cp++ = 'A';
2279159885Ssam	if (flags & IEEE80211_NODE_QOS)
2280159885Ssam		*cp++ = 'Q';
2281159885Ssam	if (flags & IEEE80211_NODE_ERP)
2282159885Ssam		*cp++ = 'E';
2283159885Ssam	if (flags & IEEE80211_NODE_PWR_MGT)
2284159885Ssam		*cp++ = 'P';
2285173275Ssam	if (flags & IEEE80211_NODE_HT) {
2286170531Ssam		*cp++ = 'H';
2287173275Ssam		if (flags & IEEE80211_NODE_HTCOMPAT)
2288173275Ssam			*cp++ = '+';
2289173275Ssam	}
2290173275Ssam	if (flags & IEEE80211_NODE_WPS)
2291173275Ssam		*cp++ = 'W';
2292173275Ssam	if (flags & IEEE80211_NODE_TSN)
2293183261Ssam		*cp++ = 'N';
2294183261Ssam	if (flags & IEEE80211_NODE_AMPDU_TX)
2295173275Ssam		*cp++ = 'T';
2296183261Ssam	if (flags & IEEE80211_NODE_AMPDU_RX)
2297183261Ssam		*cp++ = 'R';
2298183261Ssam	if (flags & IEEE80211_NODE_MIMO_PS) {
2299183261Ssam		*cp++ = 'M';
2300183261Ssam		if (flags & IEEE80211_NODE_MIMO_RTS)
2301183261Ssam			*cp++ = '+';
2302183261Ssam	}
2303183261Ssam	if (flags & IEEE80211_NODE_RIFS)
2304183261Ssam		*cp++ = 'I';
2305159885Ssam	*cp = '\0';
2306159885Ssam	return flagstring;
2307159885Ssam}
2308159885Ssam
2309138593Ssamstatic void
2310138593Ssamprintie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
2311138593Ssam{
2312138593Ssam	printf("%s", tag);
2313138593Ssam	if (verbose) {
2314138593Ssam		maxlen -= strlen(tag)+2;
2315138593Ssam		if (2*ielen > maxlen)
2316138593Ssam			maxlen--;
2317138593Ssam		printf("<");
2318138593Ssam		for (; ielen > 0; ie++, ielen--) {
2319138593Ssam			if (maxlen-- <= 0)
2320138593Ssam				break;
2321138593Ssam			printf("%02x", *ie);
2322138593Ssam		}
2323138593Ssam		if (ielen != 0)
2324138593Ssam			printf("-");
2325138593Ssam		printf(">");
2326138593Ssam	}
2327138593Ssam}
2328138593Ssam
2329170531Ssam#define LE_READ_2(p)					\
2330170531Ssam	((u_int16_t)					\
2331170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2332170531Ssam	  (((const u_int8_t *)(p))[1] <<  8)))
2333170531Ssam#define LE_READ_4(p)					\
2334170531Ssam	((u_int32_t)					\
2335170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2336170531Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
2337170531Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
2338170531Ssam	  (((const u_int8_t *)(p))[3] << 24)))
2339170531Ssam
2340138593Ssam/*
2341170531Ssam * NB: The decoding routines assume a properly formatted ie
2342170531Ssam *     which should be safe as the kernel only retains them
2343170531Ssam *     if they parse ok.
2344170531Ssam */
2345170531Ssam
2346170531Ssamstatic void
2347173275Ssamprintwmeparam(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2348170531Ssam{
2349170531Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
2350170531Ssam	static const char *acnames[] = { "BE", "BK", "VO", "VI" };
2351173275Ssam	const struct ieee80211_wme_param *wme =
2352173275Ssam	    (const struct ieee80211_wme_param *) ie;
2353170531Ssam	int i;
2354170531Ssam
2355170531Ssam	printf("%s", tag);
2356173275Ssam	if (!verbose)
2357173275Ssam		return;
2358173275Ssam	printf("<qosinfo 0x%x", wme->param_qosInfo);
2359173275Ssam	ie += offsetof(struct ieee80211_wme_param, params_acParams);
2360173275Ssam	for (i = 0; i < WME_NUM_AC; i++) {
2361173275Ssam		const struct ieee80211_wme_acparams *ac =
2362173275Ssam		    &wme->params_acParams[i];
2363173275Ssam
2364173275Ssam		printf(" %s[%saifsn %u cwmin %u cwmax %u txop %u]"
2365173275Ssam			, acnames[i]
2366173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_ACM) ? "acm " : ""
2367173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_AIFSN)
2368173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMIN)
2369173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMAX)
2370173275Ssam			, LE_READ_2(&ac->acp_txop)
2371173275Ssam		);
2372170531Ssam	}
2373173275Ssam	printf(">");
2374170531Ssam#undef MS
2375170531Ssam}
2376170531Ssam
2377170531Ssamstatic void
2378173275Ssamprintwmeinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2379173275Ssam{
2380173275Ssam	printf("%s", tag);
2381173275Ssam	if (verbose) {
2382173275Ssam		const struct ieee80211_wme_info *wme =
2383173275Ssam		    (const struct ieee80211_wme_info *) ie;
2384173275Ssam		printf("<version 0x%x info 0x%x>",
2385173275Ssam		    wme->wme_version, wme->wme_info);
2386173275Ssam	}
2387173275Ssam}
2388173275Ssam
2389173275Ssamstatic void
2390173275Ssamprinthtcap(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2391173275Ssam{
2392173275Ssam	printf("%s", tag);
2393173275Ssam	if (verbose) {
2394173275Ssam		const struct ieee80211_ie_htcap *htcap =
2395173275Ssam		    (const struct ieee80211_ie_htcap *) ie;
2396173275Ssam		const char *sep;
2397173275Ssam		int i, j;
2398173275Ssam
2399173275Ssam		printf("<cap 0x%x param 0x%x",
2400173275Ssam		    LE_READ_2(&htcap->hc_cap), htcap->hc_param);
2401173275Ssam		printf(" mcsset[");
2402173275Ssam		sep = "";
2403173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2404173275Ssam			if (isset(htcap->hc_mcsset, i)) {
2405173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2406173275Ssam					if (isclr(htcap->hc_mcsset, j))
2407173275Ssam						break;
2408173275Ssam				j--;
2409173275Ssam				if (i == j)
2410173275Ssam					printf("%s%u", sep, i);
2411173275Ssam				else
2412173275Ssam					printf("%s%u-%u", sep, i, j);
2413173275Ssam				i += j-i;
2414173275Ssam				sep = ",";
2415173275Ssam			}
2416173275Ssam		printf("] extcap 0x%x txbf 0x%x antenna 0x%x>",
2417173275Ssam		    LE_READ_2(&htcap->hc_extcap),
2418173275Ssam		    LE_READ_4(&htcap->hc_txbf),
2419173275Ssam		    htcap->hc_antenna);
2420173275Ssam	}
2421173275Ssam}
2422173275Ssam
2423173275Ssamstatic void
2424173275Ssamprinthtinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2425173275Ssam{
2426173275Ssam	printf("%s", tag);
2427173275Ssam	if (verbose) {
2428173275Ssam		const struct ieee80211_ie_htinfo *htinfo =
2429173275Ssam		    (const struct ieee80211_ie_htinfo *) ie;
2430173275Ssam		const char *sep;
2431173275Ssam		int i, j;
2432173275Ssam
2433173275Ssam		printf("<ctl %u, %x,%x,%x,%x", htinfo->hi_ctrlchannel,
2434173275Ssam		    htinfo->hi_byte1, htinfo->hi_byte2, htinfo->hi_byte3,
2435173275Ssam		    LE_READ_2(&htinfo->hi_byte45));
2436173275Ssam		printf(" basicmcs[");
2437173275Ssam		sep = "";
2438173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2439173275Ssam			if (isset(htinfo->hi_basicmcsset, i)) {
2440173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2441173275Ssam					if (isclr(htinfo->hi_basicmcsset, j))
2442173275Ssam						break;
2443173275Ssam				j--;
2444173275Ssam				if (i == j)
2445173275Ssam					printf("%s%u", sep, i);
2446173275Ssam				else
2447173275Ssam					printf("%s%u-%u", sep, i, j);
2448173275Ssam				i += j-i;
2449173275Ssam				sep = ",";
2450173275Ssam			}
2451173275Ssam		printf("]>");
2452173275Ssam	}
2453173275Ssam}
2454173275Ssam
2455173275Ssamstatic void
2456170531Ssamprintathie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2457170531Ssam{
2458170531Ssam
2459170531Ssam	printf("%s", tag);
2460170531Ssam	if (verbose) {
2461170531Ssam		const struct ieee80211_ath_ie *ath =
2462170531Ssam			(const struct ieee80211_ath_ie *)ie;
2463170531Ssam
2464170531Ssam		printf("<");
2465170531Ssam		if (ath->ath_capability & ATHEROS_CAP_TURBO_PRIME)
2466170531Ssam			printf("DTURBO,");
2467170531Ssam		if (ath->ath_capability & ATHEROS_CAP_COMPRESSION)
2468170531Ssam			printf("COMP,");
2469170531Ssam		if (ath->ath_capability & ATHEROS_CAP_FAST_FRAME)
2470170531Ssam			printf("FF,");
2471170531Ssam		if (ath->ath_capability & ATHEROS_CAP_XR)
2472170531Ssam			printf("XR,");
2473170531Ssam		if (ath->ath_capability & ATHEROS_CAP_AR)
2474170531Ssam			printf("AR,");
2475170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BURST)
2476170531Ssam			printf("BURST,");
2477170531Ssam		if (ath->ath_capability & ATHEROS_CAP_WME)
2478170531Ssam			printf("WME,");
2479170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BOOST)
2480170531Ssam			printf("BOOST,");
2481170531Ssam		printf("0x%x>", LE_READ_2(ath->ath_defkeyix));
2482170531Ssam	}
2483170531Ssam}
2484170531Ssam
2485170531Ssamstatic const char *
2486170531Ssamwpa_cipher(const u_int8_t *sel)
2487170531Ssam{
2488170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2489170531Ssam	u_int32_t w = LE_READ_4(sel);
2490170531Ssam
2491170531Ssam	switch (w) {
2492170531Ssam	case WPA_SEL(WPA_CSE_NULL):
2493170531Ssam		return "NONE";
2494170531Ssam	case WPA_SEL(WPA_CSE_WEP40):
2495170531Ssam		return "WEP40";
2496170531Ssam	case WPA_SEL(WPA_CSE_WEP104):
2497170531Ssam		return "WEP104";
2498170531Ssam	case WPA_SEL(WPA_CSE_TKIP):
2499170531Ssam		return "TKIP";
2500170531Ssam	case WPA_SEL(WPA_CSE_CCMP):
2501170531Ssam		return "AES-CCMP";
2502170531Ssam	}
2503170531Ssam	return "?";		/* NB: so 1<< is discarded */
2504170531Ssam#undef WPA_SEL
2505170531Ssam}
2506170531Ssam
2507170531Ssamstatic const char *
2508170531Ssamwpa_keymgmt(const u_int8_t *sel)
2509170531Ssam{
2510170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2511170531Ssam	u_int32_t w = LE_READ_4(sel);
2512170531Ssam
2513170531Ssam	switch (w) {
2514170531Ssam	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
2515170531Ssam		return "8021X-UNSPEC";
2516170531Ssam	case WPA_SEL(WPA_ASE_8021X_PSK):
2517170531Ssam		return "8021X-PSK";
2518170531Ssam	case WPA_SEL(WPA_ASE_NONE):
2519170531Ssam		return "NONE";
2520170531Ssam	}
2521170531Ssam	return "?";
2522170531Ssam#undef WPA_SEL
2523170531Ssam}
2524170531Ssam
2525170531Ssamstatic void
2526170531Ssamprintwpaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2527170531Ssam{
2528170531Ssam	u_int8_t len = ie[1];
2529170531Ssam
2530170531Ssam	printf("%s", tag);
2531170531Ssam	if (verbose) {
2532170531Ssam		const char *sep;
2533170531Ssam		int n;
2534170531Ssam
2535170531Ssam		ie += 6, len -= 4;		/* NB: len is payload only */
2536170531Ssam
2537170531Ssam		printf("<v%u", LE_READ_2(ie));
2538170531Ssam		ie += 2, len -= 2;
2539170531Ssam
2540170531Ssam		printf(" mc:%s", wpa_cipher(ie));
2541170531Ssam		ie += 4, len -= 4;
2542170531Ssam
2543170531Ssam		/* unicast ciphers */
2544170531Ssam		n = LE_READ_2(ie);
2545170531Ssam		ie += 2, len -= 2;
2546170531Ssam		sep = " uc:";
2547170531Ssam		for (; n > 0; n--) {
2548170531Ssam			printf("%s%s", sep, wpa_cipher(ie));
2549170531Ssam			ie += 4, len -= 4;
2550170531Ssam			sep = "+";
2551170531Ssam		}
2552170531Ssam
2553170531Ssam		/* key management algorithms */
2554170531Ssam		n = LE_READ_2(ie);
2555170531Ssam		ie += 2, len -= 2;
2556170531Ssam		sep = " km:";
2557170531Ssam		for (; n > 0; n--) {
2558170531Ssam			printf("%s%s", sep, wpa_keymgmt(ie));
2559170531Ssam			ie += 4, len -= 4;
2560170531Ssam			sep = "+";
2561170531Ssam		}
2562170531Ssam
2563170531Ssam		if (len > 2)		/* optional capabilities */
2564170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2565170531Ssam		printf(">");
2566170531Ssam	}
2567170531Ssam}
2568170531Ssam
2569170531Ssamstatic const char *
2570170531Ssamrsn_cipher(const u_int8_t *sel)
2571170531Ssam{
2572170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2573170531Ssam	u_int32_t w = LE_READ_4(sel);
2574170531Ssam
2575170531Ssam	switch (w) {
2576170531Ssam	case RSN_SEL(RSN_CSE_NULL):
2577170531Ssam		return "NONE";
2578170531Ssam	case RSN_SEL(RSN_CSE_WEP40):
2579170531Ssam		return "WEP40";
2580170531Ssam	case RSN_SEL(RSN_CSE_WEP104):
2581170531Ssam		return "WEP104";
2582170531Ssam	case RSN_SEL(RSN_CSE_TKIP):
2583170531Ssam		return "TKIP";
2584170531Ssam	case RSN_SEL(RSN_CSE_CCMP):
2585170531Ssam		return "AES-CCMP";
2586170531Ssam	case RSN_SEL(RSN_CSE_WRAP):
2587170531Ssam		return "AES-OCB";
2588170531Ssam	}
2589170531Ssam	return "?";
2590170531Ssam#undef WPA_SEL
2591170531Ssam}
2592170531Ssam
2593170531Ssamstatic const char *
2594170531Ssamrsn_keymgmt(const u_int8_t *sel)
2595170531Ssam{
2596170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2597170531Ssam	u_int32_t w = LE_READ_4(sel);
2598170531Ssam
2599170531Ssam	switch (w) {
2600170531Ssam	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
2601170531Ssam		return "8021X-UNSPEC";
2602170531Ssam	case RSN_SEL(RSN_ASE_8021X_PSK):
2603170531Ssam		return "8021X-PSK";
2604170531Ssam	case RSN_SEL(RSN_ASE_NONE):
2605170531Ssam		return "NONE";
2606170531Ssam	}
2607170531Ssam	return "?";
2608170531Ssam#undef RSN_SEL
2609170531Ssam}
2610170531Ssam
2611170531Ssamstatic void
2612170531Ssamprintrsnie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2613170531Ssam{
2614170531Ssam	printf("%s", tag);
2615170531Ssam	if (verbose) {
2616170531Ssam		const char *sep;
2617170531Ssam		int n;
2618170531Ssam
2619173275Ssam		ie += 2, ielen -= 2;
2620170531Ssam
2621170531Ssam		printf("<v%u", LE_READ_2(ie));
2622173275Ssam		ie += 2, ielen -= 2;
2623170531Ssam
2624170531Ssam		printf(" mc:%s", rsn_cipher(ie));
2625173275Ssam		ie += 4, ielen -= 4;
2626170531Ssam
2627170531Ssam		/* unicast ciphers */
2628170531Ssam		n = LE_READ_2(ie);
2629173275Ssam		ie += 2, ielen -= 2;
2630170531Ssam		sep = " uc:";
2631170531Ssam		for (; n > 0; n--) {
2632170531Ssam			printf("%s%s", sep, rsn_cipher(ie));
2633173275Ssam			ie += 4, ielen -= 4;
2634170531Ssam			sep = "+";
2635170531Ssam		}
2636170531Ssam
2637170531Ssam		/* key management algorithms */
2638170531Ssam		n = LE_READ_2(ie);
2639173275Ssam		ie += 2, ielen -= 2;
2640170531Ssam		sep = " km:";
2641170531Ssam		for (; n > 0; n--) {
2642170531Ssam			printf("%s%s", sep, rsn_keymgmt(ie));
2643173275Ssam			ie += 4, ielen -= 4;
2644170531Ssam			sep = "+";
2645170531Ssam		}
2646170531Ssam
2647173275Ssam		if (ielen > 2)		/* optional capabilities */
2648170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2649170531Ssam		/* XXXPMKID */
2650170531Ssam		printf(">");
2651170531Ssam	}
2652170531Ssam}
2653170531Ssam
2654181454Ssam/* XXX move to a public include file */
2655181454Ssam#define IEEE80211_WPS_DEV_PASS_ID	0x1012
2656181454Ssam#define IEEE80211_WPS_SELECTED_REG	0x1041
2657181454Ssam#define IEEE80211_WPS_SETUP_STATE	0x1044
2658181454Ssam#define IEEE80211_WPS_UUID_E		0x1047
2659181454Ssam#define IEEE80211_WPS_VERSION		0x104a
2660181454Ssam
2661181454Ssam#define BE_READ_2(p)					\
2662181454Ssam	((u_int16_t)					\
2663181454Ssam	 ((((const u_int8_t *)(p))[1]      ) |		\
2664181454Ssam	  (((const u_int8_t *)(p))[0] <<  8)))
2665181454Ssam
2666181454Ssamstatic void
2667181454Ssamprintwpsie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2668181454Ssam{
2669181454Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
2670181454Ssam	u_int8_t len = ie[1];
2671181454Ssam
2672181454Ssam	printf("%s", tag);
2673181454Ssam	if (verbose) {
2674181454Ssam		static const char *dev_pass_id[] = {
2675181454Ssam			"D",	/* Default (PIN) */
2676181454Ssam			"U",	/* User-specified */
2677181454Ssam			"M",	/* Machine-specified */
2678181454Ssam			"K",	/* Rekey */
2679181454Ssam			"P",	/* PushButton */
2680181454Ssam			"R"	/* Registrar-specified */
2681181454Ssam		};
2682181454Ssam		int n;
2683181454Ssam
2684181454Ssam		ie +=6, len -= 4;		/* NB: len is payload only */
2685181454Ssam
2686181454Ssam		/* WPS IE in Beacon and Probe Resp frames have different fields */
2687181454Ssam		printf("<");
2688181454Ssam		while (len) {
2689181454Ssam			uint16_t tlv_type = BE_READ_2(ie);
2690181454Ssam			uint16_t tlv_len  = BE_READ_2(ie + 2);
2691181454Ssam
2692181454Ssam			ie += 4, len -= 4;
2693181454Ssam
2694181454Ssam			switch (tlv_type) {
2695181454Ssam			case IEEE80211_WPS_VERSION:
2696181454Ssam				printf("v:%d.%d", *ie >> 4, *ie & 0xf);
2697181454Ssam				break;
2698181454Ssam			case IEEE80211_WPS_SETUP_STATE:
2699181454Ssam				/* Only 1 and 2 are valid */
2700181454Ssam				if (*ie == 0 || *ie >= 3)
2701181454Ssam					printf(" state:B");
2702181454Ssam				else
2703181454Ssam					printf(" st:%s", *ie == 1 ? "N" : "C");
2704181454Ssam				break;
2705181454Ssam			case IEEE80211_WPS_SELECTED_REG:
2706181454Ssam				printf(" sel:%s", *ie ? "T" : "F");
2707181454Ssam				break;
2708181454Ssam			case IEEE80211_WPS_DEV_PASS_ID:
2709181454Ssam				n = LE_READ_2(ie);
2710181454Ssam				if (n < N(dev_pass_id))
2711181454Ssam					printf(" dpi:%s", dev_pass_id[n]);
2712181454Ssam				break;
2713181454Ssam			case IEEE80211_WPS_UUID_E:
2714181454Ssam				printf(" uuid-e:");
2715181454Ssam				for (n = 0; n < (tlv_len - 1); n++)
2716181454Ssam					printf("%02x-", ie[n]);
2717181454Ssam				printf("%02x", ie[n]);
2718181454Ssam				break;
2719181454Ssam			}
2720181454Ssam			ie += tlv_len, len -= tlv_len;
2721181454Ssam		}
2722181454Ssam		printf(">");
2723181454Ssam	}
2724181454Ssam#undef N
2725181454Ssam}
2726181454Ssam
2727186904Ssamstatic void
2728186904Ssamprinttdmaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2729186904Ssam{
2730186904Ssam	printf("%s", tag);
2731186904Ssam	if (verbose && ielen >= sizeof(struct ieee80211_tdma_param)) {
2732186904Ssam		const struct ieee80211_tdma_param *tdma =
2733186904Ssam		   (const struct ieee80211_tdma_param *) ie;
2734186904Ssam
2735186904Ssam		/* XXX tstamp */
2736186904Ssam		printf("<v%u slot:%u slotcnt:%u slotlen:%u bintval:%u inuse:0x%x>",
2737186904Ssam		    tdma->tdma_version, tdma->tdma_slot, tdma->tdma_slotcnt,
2738186904Ssam		    LE_READ_2(&tdma->tdma_slotlen), tdma->tdma_bintval,
2739186904Ssam		    tdma->tdma_inuse[0]);
2740186904Ssam	}
2741186904Ssam}
2742186904Ssam
2743170531Ssam/*
2744138593Ssam * Copy the ssid string contents into buf, truncating to fit.  If the
2745138593Ssam * ssid is entirely printable then just copy intact.  Otherwise convert
2746138593Ssam * to hexadecimal.  If the result is truncated then replace the last
2747138593Ssam * three characters with "...".
2748138593Ssam */
2749146873Sjhbstatic int
2750138593Ssamcopy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
2751138593Ssam{
2752138593Ssam	const u_int8_t *p;
2753138593Ssam	size_t maxlen;
2754138593Ssam	int i;
2755138593Ssam
2756138593Ssam	if (essid_len > bufsize)
2757138593Ssam		maxlen = bufsize;
2758138593Ssam	else
2759138593Ssam		maxlen = essid_len;
2760138593Ssam	/* determine printable or not */
2761138593Ssam	for (i = 0, p = essid; i < maxlen; i++, p++) {
2762138593Ssam		if (*p < ' ' || *p > 0x7e)
2763138593Ssam			break;
2764138593Ssam	}
2765138593Ssam	if (i != maxlen) {		/* not printable, print as hex */
2766138593Ssam		if (bufsize < 3)
2767138593Ssam			return 0;
2768138593Ssam		strlcpy(buf, "0x", bufsize);
2769138593Ssam		bufsize -= 2;
2770138593Ssam		p = essid;
2771138593Ssam		for (i = 0; i < maxlen && bufsize >= 2; i++) {
2772147489Savatar			sprintf(&buf[2+2*i], "%02x", p[i]);
2773138593Ssam			bufsize -= 2;
2774138593Ssam		}
2775147489Savatar		if (i != essid_len)
2776147489Savatar			memcpy(&buf[2+2*i-3], "...", 3);
2777138593Ssam	} else {			/* printable, truncate as needed */
2778138593Ssam		memcpy(buf, essid, maxlen);
2779147489Savatar		if (maxlen != essid_len)
2780147489Savatar			memcpy(&buf[maxlen-3], "...", 3);
2781138593Ssam	}
2782138593Ssam	return maxlen;
2783138593Ssam}
2784138593Ssam
2785173275Ssamstatic void
2786173275Ssamprintssid(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2787173275Ssam{
2788173275Ssam	char ssid[2*IEEE80211_NWID_LEN+1];
2789173275Ssam
2790173275Ssam	printf("%s<%.*s>", tag, copy_essid(ssid, maxlen, ie+2, ie[1]), ssid);
2791173275Ssam}
2792173275Ssam
2793173275Ssamstatic void
2794173275Ssamprintrates(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2795173275Ssam{
2796173275Ssam	const char *sep;
2797173275Ssam	int i;
2798173275Ssam
2799173275Ssam	printf("%s", tag);
2800173275Ssam	sep = "<";
2801173275Ssam	for (i = 2; i < ielen; i++) {
2802173275Ssam		printf("%s%s%d", sep,
2803173275Ssam		    ie[i] & IEEE80211_RATE_BASIC ? "B" : "",
2804173275Ssam		    ie[i] & IEEE80211_RATE_VAL);
2805173275Ssam		sep = ",";
2806173275Ssam	}
2807173275Ssam	printf(">");
2808173275Ssam}
2809173275Ssam
2810173275Ssamstatic void
2811173275Ssamprintcountry(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2812173275Ssam{
2813173275Ssam	const struct ieee80211_country_ie *cie =
2814173275Ssam	   (const struct ieee80211_country_ie *) ie;
2815173275Ssam	int i, nbands, schan, nchan;
2816173275Ssam
2817173275Ssam	printf("%s<%c%c%c", tag, cie->cc[0], cie->cc[1], cie->cc[2]);
2818173275Ssam	nbands = (cie->len - 3) / sizeof(cie->band[0]);
2819173275Ssam	for (i = 0; i < nbands; i++) {
2820173275Ssam		schan = cie->band[i].schan;
2821173275Ssam		nchan = cie->band[i].nchan;
2822173275Ssam		if (nchan != 1)
2823173275Ssam			printf(" %u-%u,%u", schan, schan + nchan-1,
2824173275Ssam			    cie->band[i].maxtxpwr);
2825173275Ssam		else
2826173275Ssam			printf(" %u,%u", schan, cie->band[i].maxtxpwr);
2827173275Ssam	}
2828173275Ssam	printf(">");
2829173275Ssam}
2830173275Ssam
2831148686Sstefanf/* unaligned little endian access */
2832138593Ssam#define LE_READ_4(p)					\
2833138593Ssam	((u_int32_t)					\
2834138593Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2835138593Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
2836138593Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
2837138593Ssam	  (((const u_int8_t *)(p))[3] << 24)))
2838138593Ssam
2839178354Ssamstatic __inline int
2840138593Ssamiswpaoui(const u_int8_t *frm)
2841138593Ssam{
2842138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
2843138593Ssam}
2844138593Ssam
2845178354Ssamstatic __inline int
2846173275Ssamiswmeinfo(const u_int8_t *frm)
2847138593Ssam{
2848173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
2849173275Ssam		frm[6] == WME_INFO_OUI_SUBTYPE;
2850138593Ssam}
2851138593Ssam
2852178354Ssamstatic __inline int
2853173275Ssamiswmeparam(const u_int8_t *frm)
2854173275Ssam{
2855173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
2856173275Ssam		frm[6] == WME_PARAM_OUI_SUBTYPE;
2857173275Ssam}
2858173275Ssam
2859178354Ssamstatic __inline int
2860138593Ssamisatherosoui(const u_int8_t *frm)
2861138593Ssam{
2862138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
2863138593Ssam}
2864138593Ssam
2865181454Ssamstatic __inline int
2866186904Ssamistdmaoui(const uint8_t *frm)
2867186904Ssam{
2868186904Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((TDMA_OUI_TYPE<<24)|TDMA_OUI);
2869186904Ssam}
2870186904Ssam
2871186904Ssamstatic __inline int
2872181454Ssamiswpsoui(const uint8_t *frm)
2873181454Ssam{
2874181454Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPS_OUI_TYPE<<24)|WPA_OUI);
2875181454Ssam}
2876181454Ssam
2877173275Ssamstatic const char *
2878173275Ssamiename(int elemid)
2879173275Ssam{
2880173275Ssam	switch (elemid) {
2881173275Ssam	case IEEE80211_ELEMID_FHPARMS:	return " FHPARMS";
2882173275Ssam	case IEEE80211_ELEMID_CFPARMS:	return " CFPARMS";
2883173275Ssam	case IEEE80211_ELEMID_TIM:	return " TIM";
2884173275Ssam	case IEEE80211_ELEMID_IBSSPARMS:return " IBSSPARMS";
2885173275Ssam	case IEEE80211_ELEMID_CHALLENGE:return " CHALLENGE";
2886173275Ssam	case IEEE80211_ELEMID_PWRCNSTR:	return " PWRCNSTR";
2887173275Ssam	case IEEE80211_ELEMID_PWRCAP:	return " PWRCAP";
2888173275Ssam	case IEEE80211_ELEMID_TPCREQ:	return " TPCREQ";
2889173275Ssam	case IEEE80211_ELEMID_TPCREP:	return " TPCREP";
2890173275Ssam	case IEEE80211_ELEMID_SUPPCHAN:	return " SUPPCHAN";
2891173275Ssam	case IEEE80211_ELEMID_CHANSWITCHANN:return " CSA";
2892173275Ssam	case IEEE80211_ELEMID_MEASREQ:	return " MEASREQ";
2893173275Ssam	case IEEE80211_ELEMID_MEASREP:	return " MEASREP";
2894173275Ssam	case IEEE80211_ELEMID_QUIET:	return " QUIET";
2895173275Ssam	case IEEE80211_ELEMID_IBSSDFS:	return " IBSSDFS";
2896173275Ssam	case IEEE80211_ELEMID_TPC:	return " TPC";
2897173275Ssam	case IEEE80211_ELEMID_CCKM:	return " CCKM";
2898173275Ssam	}
2899173275Ssam	return " ???";
2900173275Ssam}
2901173275Ssam
2902138593Ssamstatic void
2903138593Ssamprinties(const u_int8_t *vp, int ielen, int maxcols)
2904138593Ssam{
2905138593Ssam	while (ielen > 0) {
2906138593Ssam		switch (vp[0]) {
2907173275Ssam		case IEEE80211_ELEMID_SSID:
2908173275Ssam			if (verbose)
2909173275Ssam				printssid(" SSID", vp, 2+vp[1], maxcols);
2910173275Ssam			break;
2911173275Ssam		case IEEE80211_ELEMID_RATES:
2912173275Ssam		case IEEE80211_ELEMID_XRATES:
2913173275Ssam			if (verbose)
2914173275Ssam				printrates(vp[0] == IEEE80211_ELEMID_RATES ?
2915173275Ssam				    " RATES" : " XRATES", vp, 2+vp[1], maxcols);
2916173275Ssam			break;
2917173275Ssam		case IEEE80211_ELEMID_DSPARMS:
2918173275Ssam			if (verbose)
2919173275Ssam				printf(" DSPARMS<%u>", vp[2]);
2920173275Ssam			break;
2921173275Ssam		case IEEE80211_ELEMID_COUNTRY:
2922173275Ssam			if (verbose)
2923173275Ssam				printcountry(" COUNTRY", vp, 2+vp[1], maxcols);
2924173275Ssam			break;
2925173275Ssam		case IEEE80211_ELEMID_ERP:
2926173275Ssam			if (verbose)
2927173275Ssam				printf(" ERP<0x%x>", vp[2]);
2928173275Ssam			break;
2929138593Ssam		case IEEE80211_ELEMID_VENDOR:
2930138593Ssam			if (iswpaoui(vp))
2931170531Ssam				printwpaie(" WPA", vp, 2+vp[1], maxcols);
2932173275Ssam			else if (iswmeinfo(vp))
2933173275Ssam				printwmeinfo(" WME", vp, 2+vp[1], maxcols);
2934173275Ssam			else if (iswmeparam(vp))
2935173275Ssam				printwmeparam(" WME", vp, 2+vp[1], maxcols);
2936139492Ssam			else if (isatherosoui(vp))
2937170531Ssam				printathie(" ATH", vp, 2+vp[1], maxcols);
2938181454Ssam			else if (iswpsoui(vp))
2939181454Ssam				printwpsie(" WPS", vp, 2+vp[1], maxcols);
2940186904Ssam			else if (istdmaoui(vp))
2941186904Ssam				printtdmaie(" TDMA", vp, 2+vp[1], maxcols);
2942173275Ssam			else if (verbose)
2943138593Ssam				printie(" VEN", vp, 2+vp[1], maxcols);
2944138593Ssam			break;
2945138593Ssam		case IEEE80211_ELEMID_RSN:
2946170531Ssam			printrsnie(" RSN", vp, 2+vp[1], maxcols);
2947138593Ssam			break;
2948173275Ssam		case IEEE80211_ELEMID_HTCAP:
2949173275Ssam			printhtcap(" HTCAP", vp, 2+vp[1], maxcols);
2950173275Ssam			break;
2951173275Ssam		case IEEE80211_ELEMID_HTINFO:
2952173275Ssam			if (verbose)
2953173275Ssam				printhtinfo(" HTINFO", vp, 2+vp[1], maxcols);
2954173275Ssam			break;
2955138593Ssam		default:
2956173275Ssam			if (verbose)
2957173275Ssam				printie(iename(vp[0]), vp, 2+vp[1], maxcols);
2958138593Ssam			break;
2959138593Ssam		}
2960138593Ssam		ielen -= 2+vp[1];
2961138593Ssam		vp += 2+vp[1];
2962138593Ssam	}
2963138593Ssam}
2964138593Ssam
2965138593Ssamstatic void
2966178354Ssamprintmimo(const struct ieee80211_mimo_info *mi)
2967178354Ssam{
2968178354Ssam	/* NB: don't muddy display unless there's something to show */
2969178354Ssam	if (mi->rssi[0] != 0 || mi->rssi[1] != 0 || mi->rssi[2] != 0) {
2970178354Ssam		/* XXX ignore EVM for now */
2971178354Ssam		printf(" (rssi %d:%d:%d nf %d:%d:%d)",
2972178354Ssam		    mi->rssi[0], mi->rssi[1], mi->rssi[2],
2973178354Ssam		    mi->noise[0], mi->noise[1], mi->noise[2]);
2974178354Ssam	}
2975178354Ssam}
2976178354Ssam
2977178354Ssamstatic void
2978138593Ssamlist_scan(int s)
2979138593Ssam{
2980138593Ssam	uint8_t buf[24*1024];
2981153892Srwatson	char ssid[IEEE80211_NWID_LEN+1];
2982173275Ssam	const uint8_t *cp;
2983154522Ssam	int len, ssidmax;
2984138593Ssam
2985173275Ssam	if (get80211len(s, IEEE80211_IOC_SCAN_RESULTS, buf, sizeof(buf), &len) < 0)
2986138593Ssam		errx(1, "unable to get scan results");
2987138593Ssam	if (len < sizeof(struct ieee80211req_scan_result))
2988138593Ssam		return;
2989138593Ssam
2990170531Ssam	getchaninfo(s);
2991170531Ssam
2992154522Ssam	ssidmax = verbose ? IEEE80211_NWID_LEN : 14;
2993170531Ssam	printf("%-*.*s  %-17.17s  %4s %4s  %-7s  %3s %4s\n"
2994154522Ssam		, ssidmax, ssidmax, "SSID"
2995138593Ssam		, "BSSID"
2996138593Ssam		, "CHAN"
2997138593Ssam		, "RATE"
2998170531Ssam		, " S:N"
2999138593Ssam		, "INT"
3000138593Ssam		, "CAPS"
3001138593Ssam	);
3002138593Ssam	cp = buf;
3003138593Ssam	do {
3004170531Ssam		const struct ieee80211req_scan_result *sr;
3005170531Ssam		const uint8_t *vp;
3006138593Ssam
3007170531Ssam		sr = (const struct ieee80211req_scan_result *) cp;
3008173275Ssam		vp = cp + sr->isr_ie_off;
3009170531Ssam		printf("%-*.*s  %s  %3d  %3dM %3d:%-3d  %3d %-4.4s"
3010154522Ssam			, ssidmax
3011155461Ssam			  , copy_essid(ssid, ssidmax, vp, sr->isr_ssid_len)
3012154522Ssam			  , ssid
3013138593Ssam			, ether_ntoa((const struct ether_addr *) sr->isr_bssid)
3014165570Ssam			, ieee80211_mhz2ieee(sr->isr_freq, sr->isr_flags)
3015138593Ssam			, getmaxrate(sr->isr_rates, sr->isr_nrates)
3016170531Ssam			, (sr->isr_rssi/2)+sr->isr_noise, sr->isr_noise
3017138593Ssam			, sr->isr_intval
3018138593Ssam			, getcaps(sr->isr_capinfo)
3019138593Ssam		);
3020170531Ssam		printies(vp + sr->isr_ssid_len, sr->isr_ie_len, 24);
3021138593Ssam		printf("\n");
3022138593Ssam		cp += sr->isr_len, len -= sr->isr_len;
3023138593Ssam	} while (len >= sizeof(struct ieee80211req_scan_result));
3024138593Ssam}
3025138593Ssam
3026138593Ssamstatic void
3027138593Ssamscan_and_wait(int s)
3028138593Ssam{
3029178354Ssam	struct ieee80211_scan_req sr;
3030138593Ssam	struct ieee80211req ireq;
3031138593Ssam	int sroute;
3032138593Ssam
3033138593Ssam	sroute = socket(PF_ROUTE, SOCK_RAW, 0);
3034138593Ssam	if (sroute < 0) {
3035138593Ssam		perror("socket(PF_ROUTE,SOCK_RAW)");
3036138593Ssam		return;
3037138593Ssam	}
3038138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3039138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3040138593Ssam	ireq.i_type = IEEE80211_IOC_SCAN_REQ;
3041178354Ssam
3042178354Ssam	memset(&sr, 0, sizeof(sr));
3043178354Ssam	sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE
3044178354Ssam		    | IEEE80211_IOC_SCAN_NOPICK
3045178354Ssam		    | IEEE80211_IOC_SCAN_ONCE;
3046178354Ssam	sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
3047178354Ssam	sr.sr_nssid = 0;
3048178354Ssam
3049178354Ssam	ireq.i_data = &sr;
3050178354Ssam	ireq.i_len = sizeof(sr);
3051138593Ssam	/* NB: only root can trigger a scan so ignore errors */
3052138593Ssam	if (ioctl(s, SIOCS80211, &ireq) >= 0) {
3053138593Ssam		char buf[2048];
3054138593Ssam		struct if_announcemsghdr *ifan;
3055138593Ssam		struct rt_msghdr *rtm;
3056138593Ssam
3057138593Ssam		do {
3058138593Ssam			if (read(sroute, buf, sizeof(buf)) < 0) {
3059138593Ssam				perror("read(PF_ROUTE)");
3060138593Ssam				break;
3061138593Ssam			}
3062138593Ssam			rtm = (struct rt_msghdr *) buf;
3063138593Ssam			if (rtm->rtm_version != RTM_VERSION)
3064138593Ssam				break;
3065138593Ssam			ifan = (struct if_announcemsghdr *) rtm;
3066138593Ssam		} while (rtm->rtm_type != RTM_IEEE80211 ||
3067138593Ssam		    ifan->ifan_what != RTM_IEEE80211_SCAN);
3068138593Ssam	}
3069138593Ssam	close(sroute);
3070138593Ssam}
3071138593Ssam
3072138593Ssamstatic
3073138593SsamDECL_CMD_FUNC(set80211scan, val, d)
3074138593Ssam{
3075138593Ssam	scan_and_wait(s);
3076138593Ssam	list_scan(s);
3077138593Ssam}
3078138593Ssam
3079161147Ssamstatic enum ieee80211_opmode get80211opmode(int s);
3080161147Ssam
3081173275Ssamstatic int
3082173275Ssamgettxseq(const struct ieee80211req_sta_info *si)
3083173275Ssam{
3084173275Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
3085173275Ssam
3086173275Ssam	int i, txseq;
3087173275Ssam
3088173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3089173275Ssam		return si->isi_txseqs[0];
3090173275Ssam	/* XXX not right but usually what folks want */
3091173275Ssam	txseq = 0;
3092173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3093173275Ssam		if (si->isi_txseqs[i] > txseq)
3094173275Ssam			txseq = si->isi_txseqs[i];
3095173275Ssam	return txseq;
3096173275Ssam#undef IEEE80211_NODE_QOS
3097173275Ssam}
3098173275Ssam
3099173275Ssamstatic int
3100173275Ssamgetrxseq(const struct ieee80211req_sta_info *si)
3101173275Ssam{
3102173275Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
3103173275Ssam
3104173275Ssam	int i, rxseq;
3105173275Ssam
3106173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3107173275Ssam		return si->isi_rxseqs[0];
3108173275Ssam	/* XXX not right but usually what folks want */
3109173275Ssam	rxseq = 0;
3110173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3111173275Ssam		if (si->isi_rxseqs[i] > rxseq)
3112173275Ssam			rxseq = si->isi_rxseqs[i];
3113173275Ssam	return rxseq;
3114173275Ssam#undef IEEE80211_NODE_QOS
3115173275Ssam}
3116173275Ssam
3117138593Ssamstatic void
3118138593Ssamlist_stations(int s)
3119138593Ssam{
3120161147Ssam	union {
3121161147Ssam		struct ieee80211req_sta_req req;
3122161147Ssam		uint8_t buf[24*1024];
3123161147Ssam	} u;
3124161147Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
3125170531Ssam	const uint8_t *cp;
3126138593Ssam	int len;
3127138593Ssam
3128161147Ssam	/* broadcast address =>'s get all stations */
3129161147Ssam	(void) memset(u.req.is_u.macaddr, 0xff, IEEE80211_ADDR_LEN);
3130161147Ssam	if (opmode == IEEE80211_M_STA) {
3131161147Ssam		/*
3132161147Ssam		 * Get information about the associated AP.
3133161147Ssam		 */
3134173275Ssam		(void) get80211(s, IEEE80211_IOC_BSSID,
3135173275Ssam		    u.req.is_u.macaddr, IEEE80211_ADDR_LEN);
3136161147Ssam	}
3137173275Ssam	if (get80211len(s, IEEE80211_IOC_STA_INFO, &u, sizeof(u), &len) < 0)
3138138593Ssam		errx(1, "unable to get station information");
3139138593Ssam	if (len < sizeof(struct ieee80211req_sta_info))
3140138593Ssam		return;
3141138593Ssam
3142170531Ssam	getchaninfo(s);
3143170531Ssam
3144159885Ssam	printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %4s\n"
3145138593Ssam		, "ADDR"
3146138593Ssam		, "AID"
3147138593Ssam		, "CHAN"
3148138593Ssam		, "RATE"
3149138593Ssam		, "RSSI"
3150138593Ssam		, "IDLE"
3151138593Ssam		, "TXSEQ"
3152138593Ssam		, "RXSEQ"
3153138593Ssam		, "CAPS"
3154159885Ssam		, "FLAG"
3155138593Ssam	);
3156170531Ssam	cp = (const uint8_t *) u.req.info;
3157138593Ssam	do {
3158170531Ssam		const struct ieee80211req_sta_info *si;
3159138593Ssam
3160170531Ssam		si = (const struct ieee80211req_sta_info *) cp;
3161161147Ssam		if (si->isi_len < sizeof(*si))
3162161147Ssam			break;
3163170531Ssam		printf("%s %4u %4d %3dM %3.1f %4d %6d %6d %-4.4s %-4.4s"
3164138593Ssam			, ether_ntoa((const struct ether_addr*) si->isi_macaddr)
3165138593Ssam			, IEEE80211_AID(si->isi_associd)
3166166015Ssam			, ieee80211_mhz2ieee(si->isi_freq, si->isi_flags)
3167178354Ssam			, si->isi_txmbps/2
3168170531Ssam			, si->isi_rssi/2.
3169138593Ssam			, si->isi_inact
3170173275Ssam			, gettxseq(si)
3171173275Ssam			, getrxseq(si)
3172138593Ssam			, getcaps(si->isi_capinfo)
3173159885Ssam			, getflags(si->isi_state)
3174138593Ssam		);
3175170531Ssam		printies(cp + si->isi_ie_off, si->isi_ie_len, 24);
3176178354Ssam		printmimo(&si->isi_mimo);
3177138593Ssam		printf("\n");
3178138593Ssam		cp += si->isi_len, len -= si->isi_len;
3179138593Ssam	} while (len >= sizeof(struct ieee80211req_sta_info));
3180138593Ssam}
3181138593Ssam
3182170531Ssamstatic const char *
3183170531Ssamget_chaninfo(const struct ieee80211_channel *c, int precise,
3184170531Ssam	char buf[], size_t bsize)
3185138593Ssam{
3186138593Ssam	buf[0] = '\0';
3187138593Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
3188170531Ssam		strlcat(buf, " FHSS", bsize);
3189187843Ssam	if (IEEE80211_IS_CHAN_A(c))
3190187843Ssam		strlcat(buf, " 11a", bsize);
3191187843Ssam	else if (IEEE80211_IS_CHAN_ANYG(c))
3192187843Ssam		strlcat(buf, " 11g", bsize);
3193187843Ssam	else if (IEEE80211_IS_CHAN_B(c))
3194170531Ssam		strlcat(buf, " 11b", bsize);
3195187843Ssam	if (IEEE80211_IS_CHAN_HALF(c))
3196187843Ssam		strlcat(buf, "/10Mhz", bsize);
3197187843Ssam	if (IEEE80211_IS_CHAN_QUARTER(c))
3198187843Ssam		strlcat(buf, "/5Mhz", bsize);
3199170531Ssam	if (IEEE80211_IS_CHAN_TURBO(c))
3200170531Ssam		strlcat(buf, " Turbo", bsize);
3201170531Ssam	if (precise) {
3202170531Ssam		if (IEEE80211_IS_CHAN_HT20(c))
3203170531Ssam			strlcat(buf, " ht/20", bsize);
3204170531Ssam		else if (IEEE80211_IS_CHAN_HT40D(c))
3205170531Ssam			strlcat(buf, " ht/40-", bsize);
3206170531Ssam		else if (IEEE80211_IS_CHAN_HT40U(c))
3207170531Ssam			strlcat(buf, " ht/40+", bsize);
3208170531Ssam	} else {
3209170531Ssam		if (IEEE80211_IS_CHAN_HT(c))
3210170531Ssam			strlcat(buf, " ht", bsize);
3211170531Ssam	}
3212170531Ssam	return buf;
3213170531Ssam}
3214170531Ssam
3215170531Ssamstatic void
3216173275Ssamprint_chaninfo(const struct ieee80211_channel *c, int verb)
3217170531Ssam{
3218170531Ssam	char buf[14];
3219170531Ssam
3220138593Ssam	printf("Channel %3u : %u%c Mhz%-14.14s",
3221165570Ssam		ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
3222170531Ssam		IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
3223173275Ssam		get_chaninfo(c, verb, buf, sizeof(buf)));
3224138593Ssam}
3225138593Ssam
3226187844Ssamstatic int
3227187844Ssamchanpref(const struct ieee80211_channel *c)
3228187844Ssam{
3229187844Ssam	if (IEEE80211_IS_CHAN_HT40(c))
3230187844Ssam		return 40;
3231187844Ssam	if (IEEE80211_IS_CHAN_HT20(c))
3232187844Ssam		return 30;
3233187844Ssam	if (IEEE80211_IS_CHAN_HALF(c))
3234187844Ssam		return 10;
3235187844Ssam	if (IEEE80211_IS_CHAN_QUARTER(c))
3236187844Ssam		return 5;
3237187844Ssam	if (IEEE80211_IS_CHAN_TURBO(c))
3238187844Ssam		return 25;
3239187844Ssam	if (IEEE80211_IS_CHAN_A(c))
3240187844Ssam		return 20;
3241187844Ssam	if (IEEE80211_IS_CHAN_G(c))
3242187844Ssam		return 20;
3243187844Ssam	if (IEEE80211_IS_CHAN_B(c))
3244187844Ssam		return 15;
3245187844Ssam	if (IEEE80211_IS_CHAN_PUREG(c))
3246187844Ssam		return 15;
3247187844Ssam	return 0;
3248187844Ssam}
3249187844Ssam
3250138593Ssamstatic void
3251173275Ssamprint_channels(int s, const struct ieee80211req_chaninfo *chans,
3252173275Ssam	int allchans, int verb)
3253138593Ssam{
3254187801Ssam	struct ieee80211req_chaninfo *achans;
3255170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3256138593Ssam	const struct ieee80211_channel *c;
3257170531Ssam	int i, half;
3258138593Ssam
3259187801Ssam	achans = malloc(IEEE80211_CHANINFO_SPACE(chans));
3260187801Ssam	if (achans == NULL)
3261187801Ssam		errx(1, "no space for active channel list");
3262187801Ssam	achans->ic_nchans = 0;
3263170531Ssam	memset(reported, 0, sizeof(reported));
3264138593Ssam	if (!allchans) {
3265138593Ssam		struct ieee80211req_chanlist active;
3266138593Ssam
3267173275Ssam		if (get80211(s, IEEE80211_IOC_CHANLIST, &active, sizeof(active)) < 0)
3268138593Ssam			errx(1, "unable to get active channel list");
3269173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3270173275Ssam			c = &chans->ic_chans[i];
3271170531Ssam			if (!isset(active.ic_channels, c->ic_ieee))
3272170531Ssam				continue;
3273170531Ssam			/*
3274170531Ssam			 * Suppress compatible duplicates unless
3275170531Ssam			 * verbose.  The kernel gives us it's
3276170531Ssam			 * complete channel list which has separate
3277170531Ssam			 * entries for 11g/11b and 11a/turbo.
3278170531Ssam			 */
3279173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3280170531Ssam				/* XXX we assume duplicates are adjacent */
3281187801Ssam				achans->ic_chans[achans->ic_nchans-1] = *c;
3282170531Ssam			} else {
3283187801Ssam				achans->ic_chans[achans->ic_nchans++] = *c;
3284170531Ssam				setbit(reported, c->ic_ieee);
3285170531Ssam			}
3286138593Ssam		}
3287170531Ssam	} else {
3288173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3289173275Ssam			c = &chans->ic_chans[i];
3290170531Ssam			/* suppress duplicates as above */
3291173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3292170531Ssam				/* XXX we assume duplicates are adjacent */
3293187844Ssam				struct ieee80211_channel *a =
3294187844Ssam				    &achans->ic_chans[achans->ic_nchans-1];
3295187844Ssam				if (chanpref(c) > chanpref(a))
3296187844Ssam					*a = *c;
3297170531Ssam			} else {
3298187801Ssam				achans->ic_chans[achans->ic_nchans++] = *c;
3299170531Ssam				setbit(reported, c->ic_ieee);
3300170531Ssam			}
3301170531Ssam		}
3302170531Ssam	}
3303187801Ssam	half = achans->ic_nchans / 2;
3304187801Ssam	if (achans->ic_nchans % 2)
3305138593Ssam		half++;
3306170531Ssam
3307187801Ssam	for (i = 0; i < achans->ic_nchans / 2; i++) {
3308187801Ssam		print_chaninfo(&achans->ic_chans[i], verb);
3309187801Ssam		print_chaninfo(&achans->ic_chans[half+i], verb);
3310138593Ssam		printf("\n");
3311138593Ssam	}
3312187801Ssam	if (achans->ic_nchans % 2) {
3313187801Ssam		print_chaninfo(&achans->ic_chans[i], verb);
3314138593Ssam		printf("\n");
3315138593Ssam	}
3316187801Ssam	free(achans);
3317138593Ssam}
3318138593Ssam
3319138593Ssamstatic void
3320173275Ssamlist_channels(int s, int allchans)
3321173275Ssam{
3322173275Ssam	getchaninfo(s);
3323187801Ssam	print_channels(s, chaninfo, allchans, verbose);
3324173275Ssam}
3325173275Ssam
3326173275Ssamstatic void
3327170531Ssamprint_txpow(const struct ieee80211_channel *c)
3328170531Ssam{
3329170531Ssam	printf("Channel %3u : %u Mhz %3.1f reg %2d  ",
3330170531Ssam	    c->ic_ieee, c->ic_freq,
3331170531Ssam	    c->ic_maxpower/2., c->ic_maxregpower);
3332170531Ssam}
3333170531Ssam
3334170531Ssamstatic void
3335170531Ssamprint_txpow_verbose(const struct ieee80211_channel *c)
3336170531Ssam{
3337173275Ssam	print_chaninfo(c, 1);
3338170531Ssam	printf("min %4.1f dBm  max %3.1f dBm  reg %2d dBm",
3339170531Ssam	    c->ic_minpower/2., c->ic_maxpower/2., c->ic_maxregpower);
3340170531Ssam	/* indicate where regulatory cap limits power use */
3341170531Ssam	if (c->ic_maxpower > 2*c->ic_maxregpower)
3342170531Ssam		printf(" <");
3343170531Ssam}
3344170531Ssam
3345170531Ssamstatic void
3346170531Ssamlist_txpow(int s)
3347170531Ssam{
3348187801Ssam	struct ieee80211req_chaninfo *achans;
3349170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3350170531Ssam	struct ieee80211_channel *c, *prev;
3351170531Ssam	int i, half;
3352170531Ssam
3353170531Ssam	getchaninfo(s);
3354187801Ssam	achans = malloc(IEEE80211_CHANINFO_SPACE(chaninfo));
3355187801Ssam	if (achans == NULL)
3356187801Ssam		errx(1, "no space for active channel list");
3357187801Ssam	achans->ic_nchans = 0;
3358170531Ssam	memset(reported, 0, sizeof(reported));
3359187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
3360187801Ssam		c = &chaninfo->ic_chans[i];
3361170531Ssam		/* suppress duplicates as above */
3362170531Ssam		if (isset(reported, c->ic_ieee) && !verbose) {
3363170531Ssam			/* XXX we assume duplicates are adjacent */
3364187801Ssam			prev = &achans->ic_chans[achans->ic_nchans-1];
3365170531Ssam			/* display highest power on channel */
3366170531Ssam			if (c->ic_maxpower > prev->ic_maxpower)
3367170531Ssam				*prev = *c;
3368170531Ssam		} else {
3369187801Ssam			achans->ic_chans[achans->ic_nchans++] = *c;
3370170531Ssam			setbit(reported, c->ic_ieee);
3371170531Ssam		}
3372170531Ssam	}
3373170531Ssam	if (!verbose) {
3374187801Ssam		half = achans->ic_nchans / 2;
3375187801Ssam		if (achans->ic_nchans % 2)
3376170531Ssam			half++;
3377170531Ssam
3378187801Ssam		for (i = 0; i < achans->ic_nchans / 2; i++) {
3379187801Ssam			print_txpow(&achans->ic_chans[i]);
3380187801Ssam			print_txpow(&achans->ic_chans[half+i]);
3381170531Ssam			printf("\n");
3382170531Ssam		}
3383187801Ssam		if (achans->ic_nchans % 2) {
3384187801Ssam			print_txpow(&achans->ic_chans[i]);
3385170531Ssam			printf("\n");
3386170531Ssam		}
3387170531Ssam	} else {
3388187801Ssam		for (i = 0; i < achans->ic_nchans; i++) {
3389187801Ssam			print_txpow_verbose(&achans->ic_chans[i]);
3390170531Ssam			printf("\n");
3391170531Ssam		}
3392170531Ssam	}
3393187801Ssam	free(achans);
3394170531Ssam}
3395170531Ssam
3396170531Ssamstatic void
3397138593Ssamlist_keys(int s)
3398138593Ssam{
3399138593Ssam}
3400138593Ssam
3401138593Ssam#define	IEEE80211_C_BITS \
3402181102Ssam	"\20\1STA\7FF\10TURBOP\11IBSS\12PMGT" \
3403178354Ssam	"\13HOSTAP\14AHDEMO\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE" \
3404181102Ssam	"\21MONITOR\22DFS\30WPA1\31WPA2\32BURST\33WME\34WDS\36BGSCAN" \
3405186904Ssam	"\37TXFRAG\40TDMA"
3406138593Ssam
3407138593Ssamstatic void
3408138593Ssamlist_capabilities(int s)
3409138593Ssam{
3410187801Ssam	struct ieee80211_devcaps_req *dc;
3411138593Ssam
3412187845Ssam	if (verbose)
3413187845Ssam		dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
3414187845Ssam	else
3415187845Ssam		dc = malloc(IEEE80211_DEVCAPS_SIZE(1));
3416187801Ssam	if (dc == NULL)
3417187801Ssam		errx(1, "no space for device capabilities");
3418187845Ssam	dc->dc_chaninfo.ic_nchans = verbose ? MAXCHAN : 1;
3419187801Ssam	getdevcaps(s, dc);
3420187801Ssam	printb("drivercaps", dc->dc_drivercaps, IEEE80211_C_BITS);
3421187801Ssam	if (dc->dc_cryptocaps != 0 || verbose) {
3422178354Ssam		putchar('\n');
3423187801Ssam		printb("cryptocaps", dc->dc_cryptocaps, IEEE80211_CRYPTO_BITS);
3424178354Ssam	}
3425187801Ssam	if (dc->dc_htcaps != 0 || verbose) {
3426178354Ssam		putchar('\n');
3427187801Ssam		printb("htcaps", dc->dc_htcaps, IEEE80211_HTCAP_BITS);
3428178354Ssam	}
3429138593Ssam	putchar('\n');
3430187845Ssam	if (verbose) {
3431187845Ssam		chaninfo = &dc->dc_chaninfo;	/* XXX */
3432187845Ssam		print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, verbose);
3433187845Ssam	}
3434187801Ssam	free(dc);
3435138593Ssam}
3436138593Ssam
3437173275Ssamstatic int
3438173275Ssamget80211wme(int s, int param, int ac, int *val)
3439173275Ssam{
3440173275Ssam	struct ieee80211req ireq;
3441173275Ssam
3442173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3443173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3444173275Ssam	ireq.i_type = param;
3445173275Ssam	ireq.i_len = ac;
3446173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3447173275Ssam		warn("cannot get WME parameter %d, ac %d%s",
3448173275Ssam		    param, ac & IEEE80211_WMEPARAM_VAL,
3449173275Ssam		    ac & IEEE80211_WMEPARAM_BSS ? " (BSS)" : "");
3450173275Ssam		return -1;
3451173275Ssam	}
3452173275Ssam	*val = ireq.i_val;
3453173275Ssam	return 0;
3454173275Ssam}
3455173275Ssam
3456138593Ssamstatic void
3457181199Ssamlist_wme_aci(int s, const char *tag, int ac)
3458138593Ssam{
3459181199Ssam	int val;
3460138593Ssam
3461181199Ssam	printf("\t%s", tag);
3462138593Ssam
3463181199Ssam	/* show WME BSS parameters */
3464181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMIN, ac, &val) != -1)
3465181199Ssam		printf(" cwmin %2u", val);
3466181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMAX, ac, &val) != -1)
3467181199Ssam		printf(" cwmax %2u", val);
3468181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_AIFS, ac, &val) != -1)
3469181199Ssam		printf(" aifs %2u", val);
3470181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_TXOPLIMIT, ac, &val) != -1)
3471181199Ssam		printf(" txopLimit %3u", val);
3472181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_ACM, ac, &val) != -1) {
3473181199Ssam		if (val)
3474181199Ssam			printf(" acm");
3475181199Ssam		else if (verbose)
3476181199Ssam			printf(" -acm");
3477181199Ssam	}
3478181199Ssam	/* !BSS only */
3479181199Ssam	if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3480181199Ssam		if (get80211wme(s, IEEE80211_IOC_WME_ACKPOLICY, ac, &val) != -1) {
3481181199Ssam			if (!val)
3482181199Ssam				printf(" -ack");
3483138593Ssam			else if (verbose)
3484181199Ssam				printf(" ack");
3485138593Ssam		}
3486181199Ssam	}
3487181199Ssam	printf("\n");
3488181199Ssam}
3489181199Ssam
3490181199Ssamstatic void
3491181199Ssamlist_wme(int s)
3492181199Ssam{
3493181199Ssam	static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
3494181199Ssam	int ac;
3495181199Ssam
3496181199Ssam	if (verbose) {
3497181199Ssam		/* display both BSS and local settings */
3498181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
3499181199Ssam	again:
3500181199Ssam			if (ac & IEEE80211_WMEPARAM_BSS)
3501181199Ssam				list_wme_aci(s, "     ", ac);
3502181199Ssam			else
3503181199Ssam				list_wme_aci(s, acnames[ac], ac);
3504181199Ssam			if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3505181199Ssam				ac |= IEEE80211_WMEPARAM_BSS;
3506181199Ssam				goto again;
3507181199Ssam			} else
3508181199Ssam				ac &= ~IEEE80211_WMEPARAM_BSS;
3509138593Ssam		}
3510181199Ssam	} else {
3511181199Ssam		/* display only channel settings */
3512181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++)
3513181199Ssam			list_wme_aci(s, acnames[ac], ac);
3514138593Ssam	}
3515138593Ssam}
3516138593Ssam
3517149029Ssamstatic void
3518178354Ssamlist_roam(int s)
3519178354Ssam{
3520178354Ssam	const struct ieee80211_roamparam *rp;
3521178354Ssam	int mode;
3522178354Ssam
3523178354Ssam	getroam(s);
3524188784Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
3525178354Ssam		rp = &roamparams.params[mode];
3526178354Ssam		if (rp->rssi == 0 && rp->rate == 0)
3527178354Ssam			continue;
3528188784Ssam		if (mode == IEEE80211_MODE_11NA || mode == IEEE80211_MODE_11NG) {
3529188784Ssam			if (rp->rssi & 1)
3530188784Ssam				LINE_CHECK("roam:%-7.7s rssi %2u.5dBm  MCS %2u    ",
3531188784Ssam				    modename[mode], rp->rssi/2,
3532188784Ssam				    rp->rate &~ IEEE80211_RATE_MCS);
3533188784Ssam			else
3534188784Ssam				LINE_CHECK("roam:%-7.7s rssi %4udBm  MCS %2u    ",
3535188784Ssam				    modename[mode], rp->rssi/2,
3536188784Ssam				    rp->rate &~ IEEE80211_RATE_MCS);
3537188784Ssam		} else {
3538188784Ssam			if (rp->rssi & 1)
3539188784Ssam				LINE_CHECK("roam:%-7.7s rssi %2u.5dBm rate %2u Mb/s",
3540188784Ssam				    modename[mode], rp->rssi/2, rp->rate/2);
3541188784Ssam			else
3542188784Ssam				LINE_CHECK("roam:%-7.7s rssi %4udBm rate %2u Mb/s",
3543188784Ssam				    modename[mode], rp->rssi/2, rp->rate/2);
3544188784Ssam		}
3545178354Ssam	}
3546178354Ssam}
3547178354Ssam
3548178354Ssamstatic void
3549178354Ssamlist_txparams(int s)
3550178354Ssam{
3551178354Ssam	const struct ieee80211_txparam *tp;
3552178354Ssam	int mode;
3553178354Ssam
3554178354Ssam	gettxparams(s);
3555188784Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
3556178354Ssam		tp = &txparams.params[mode];
3557178354Ssam		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
3558178354Ssam			continue;
3559188784Ssam		if (mode == IEEE80211_MODE_11NA || mode == IEEE80211_MODE_11NG) {
3560188784Ssam			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3561188784Ssam				LINE_CHECK("%-7.7s ucast NONE    mgmt %2u MCS  "
3562188784Ssam				    "mcast %2u MCS  maxretry %u",
3563188784Ssam				    modename[mode],
3564188784Ssam				    tp->mgmtrate &~ IEEE80211_RATE_MCS,
3565188784Ssam				    tp->mcastrate &~ IEEE80211_RATE_MCS,
3566188784Ssam				    tp->maxretry);
3567188784Ssam			else
3568188784Ssam				LINE_CHECK("%-7.7s ucast %2u MCS  mgmt %2u MCS  "
3569188784Ssam				    "mcast %2u MCS  maxretry %u",
3570188784Ssam				    modename[mode],
3571188784Ssam				    tp->ucastrate &~ IEEE80211_RATE_MCS,
3572188784Ssam				    tp->mgmtrate &~ IEEE80211_RATE_MCS,
3573188784Ssam				    tp->mcastrate &~ IEEE80211_RATE_MCS,
3574188784Ssam				    tp->maxretry);
3575188784Ssam		} else {
3576188784Ssam			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3577188784Ssam				LINE_CHECK("%-7.7s ucast NONE    mgmt %2u Mb/s "
3578188784Ssam				    "mcast %2u Mb/s maxretry %u",
3579188784Ssam				    modename[mode],
3580188784Ssam				    tp->mgmtrate/2,
3581188784Ssam				    tp->mcastrate/2, tp->maxretry);
3582188784Ssam			else
3583188784Ssam				LINE_CHECK("%-7.7s ucast %2u Mb/s mgmt %2u Mb/s "
3584188784Ssam				    "mcast %2u Mb/s maxretry %u",
3585188784Ssam				    modename[mode],
3586188784Ssam				    tp->ucastrate/2, tp->mgmtrate/2,
3587188784Ssam				    tp->mcastrate/2, tp->maxretry);
3588188784Ssam		}
3589178354Ssam	}
3590178354Ssam}
3591178354Ssam
3592178354Ssamstatic void
3593173275Ssamprintpolicy(int policy)
3594173275Ssam{
3595173275Ssam	switch (policy) {
3596173275Ssam	case IEEE80211_MACCMD_POLICY_OPEN:
3597173275Ssam		printf("policy: open\n");
3598173275Ssam		break;
3599173275Ssam	case IEEE80211_MACCMD_POLICY_ALLOW:
3600173275Ssam		printf("policy: allow\n");
3601173275Ssam		break;
3602173275Ssam	case IEEE80211_MACCMD_POLICY_DENY:
3603173275Ssam		printf("policy: deny\n");
3604173275Ssam		break;
3605178354Ssam	case IEEE80211_MACCMD_POLICY_RADIUS:
3606178354Ssam		printf("policy: radius\n");
3607178354Ssam		break;
3608173275Ssam	default:
3609173275Ssam		printf("policy: unknown (%u)\n", policy);
3610173275Ssam		break;
3611173275Ssam	}
3612173275Ssam}
3613173275Ssam
3614173275Ssamstatic void
3615149029Ssamlist_mac(int s)
3616149029Ssam{
3617149029Ssam	struct ieee80211req ireq;
3618149029Ssam	struct ieee80211req_maclist *acllist;
3619173275Ssam	int i, nacls, policy, len;
3620173275Ssam	uint8_t *data;
3621149029Ssam	char c;
3622149029Ssam
3623149029Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3624149029Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
3625149029Ssam	ireq.i_type = IEEE80211_IOC_MACCMD;
3626149029Ssam	ireq.i_val = IEEE80211_MACCMD_POLICY;
3627149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3628149029Ssam		if (errno == EINVAL) {
3629149029Ssam			printf("No acl policy loaded\n");
3630149029Ssam			return;
3631149029Ssam		}
3632149029Ssam		err(1, "unable to get mac policy");
3633149029Ssam	}
3634149029Ssam	policy = ireq.i_val;
3635149029Ssam	if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
3636149029Ssam		c = '*';
3637149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
3638149029Ssam		c = '+';
3639149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
3640149029Ssam		c = '-';
3641178354Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_RADIUS) {
3642178354Ssam		c = 'r';		/* NB: should never have entries */
3643149029Ssam	} else {
3644149029Ssam		printf("policy: unknown (%u)\n", policy);
3645149029Ssam		c = '?';
3646149029Ssam	}
3647173275Ssam	if (verbose || c == '?')
3648173275Ssam		printpolicy(policy);
3649173275Ssam
3650175952Ssam	ireq.i_val = IEEE80211_MACCMD_LIST;
3651175952Ssam	ireq.i_len = 0;
3652175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3653173275Ssam		err(1, "unable to get mac acl list size");
3654175952Ssam	if (ireq.i_len == 0) {		/* NB: no acls */
3655173275Ssam		if (!(verbose || c == '?'))
3656173275Ssam			printpolicy(policy);
3657173275Ssam		return;
3658173275Ssam	}
3659175952Ssam	len = ireq.i_len;
3660173275Ssam
3661173275Ssam	data = malloc(len);
3662173275Ssam	if (data == NULL)
3663173275Ssam		err(1, "out of memory for acl list");
3664173275Ssam
3665175952Ssam	ireq.i_data = data;
3666175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3667173275Ssam		err(1, "unable to get mac acl list");
3668173275Ssam	nacls = len / sizeof(*acllist);
3669173275Ssam	acllist = (struct ieee80211req_maclist *) data;
3670149029Ssam	for (i = 0; i < nacls; i++)
3671149029Ssam		printf("%c%s\n", c, ether_ntoa(
3672149029Ssam			(const struct ether_addr *) acllist[i].ml_macaddr));
3673173275Ssam	free(data);
3674149029Ssam}
3675149029Ssam
3676178354Ssamstatic void
3677178354Ssamprint_regdomain(const struct ieee80211_regdomain *reg, int verb)
3678178354Ssam{
3679178354Ssam	if ((reg->regdomain != 0 &&
3680178354Ssam	    reg->regdomain != reg->country) || verb) {
3681178354Ssam		const struct regdomain *rd =
3682178354Ssam		    lib80211_regdomain_findbysku(getregdata(), reg->regdomain);
3683178354Ssam		if (rd == NULL)
3684178354Ssam			LINE_CHECK("regdomain %d", reg->regdomain);
3685178354Ssam		else
3686178354Ssam			LINE_CHECK("regdomain %s", rd->name);
3687178354Ssam	}
3688178354Ssam	if (reg->country != 0 || verb) {
3689178354Ssam		const struct country *cc =
3690178354Ssam		    lib80211_country_findbycc(getregdata(), reg->country);
3691178354Ssam		if (cc == NULL)
3692178354Ssam			LINE_CHECK("country %d", reg->country);
3693178354Ssam		else
3694178354Ssam			LINE_CHECK("country %s", cc->isoname);
3695178354Ssam	}
3696178354Ssam	if (reg->location == 'I')
3697178354Ssam		LINE_CHECK("indoor");
3698178354Ssam	else if (reg->location == 'O')
3699178354Ssam		LINE_CHECK("outdoor");
3700178354Ssam	else if (verb)
3701178354Ssam		LINE_CHECK("anywhere");
3702178354Ssam	if (reg->ecm)
3703178354Ssam		LINE_CHECK("ecm");
3704178354Ssam	else if (verb)
3705178354Ssam		LINE_CHECK("-ecm");
3706178354Ssam}
3707178354Ssam
3708178354Ssamstatic void
3709178354Ssamlist_regdomain(int s, int channelsalso)
3710178354Ssam{
3711178354Ssam	getregdomain(s);
3712178354Ssam	if (channelsalso) {
3713178354Ssam		getchaninfo(s);
3714178354Ssam		spacer = ':';
3715178354Ssam		print_regdomain(&regdomain, 1);
3716178354Ssam		LINE_BREAK();
3717187801Ssam		print_channels(s, chaninfo, 1/*allchans*/, 1/*verbose*/);
3718178354Ssam	} else
3719178354Ssam		print_regdomain(&regdomain, verbose);
3720178354Ssam}
3721178354Ssam
3722138593Ssamstatic
3723138593SsamDECL_CMD_FUNC(set80211list, arg, d)
3724138593Ssam{
3725138593Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
3726138593Ssam
3727173275Ssam	LINE_INIT('\t');
3728173275Ssam
3729138593Ssam	if (iseq(arg, "sta"))
3730138593Ssam		list_stations(s);
3731138593Ssam	else if (iseq(arg, "scan") || iseq(arg, "ap"))
3732138593Ssam		list_scan(s);
3733138593Ssam	else if (iseq(arg, "chan") || iseq(arg, "freq"))
3734138593Ssam		list_channels(s, 1);
3735138593Ssam	else if (iseq(arg, "active"))
3736138593Ssam		list_channels(s, 0);
3737138593Ssam	else if (iseq(arg, "keys"))
3738138593Ssam		list_keys(s);
3739138593Ssam	else if (iseq(arg, "caps"))
3740138593Ssam		list_capabilities(s);
3741178354Ssam	else if (iseq(arg, "wme") || iseq(arg, "wmm"))
3742138593Ssam		list_wme(s);
3743149029Ssam	else if (iseq(arg, "mac"))
3744149029Ssam		list_mac(s);
3745170531Ssam	else if (iseq(arg, "txpow"))
3746170531Ssam		list_txpow(s);
3747178354Ssam	else if (iseq(arg, "roam"))
3748178354Ssam		list_roam(s);
3749178354Ssam	else if (iseq(arg, "txparam") || iseq(arg, "txparm"))
3750178354Ssam		list_txparams(s);
3751178354Ssam	else if (iseq(arg, "regdomain"))
3752178354Ssam		list_regdomain(s, 1);
3753178354Ssam	else if (iseq(arg, "countries"))
3754178354Ssam		list_countries();
3755138593Ssam	else
3756138593Ssam		errx(1, "Don't know how to list %s for %s", arg, name);
3757178354Ssam	LINE_BREAK();
3758138593Ssam#undef iseq
3759138593Ssam}
3760138593Ssam
3761138593Ssamstatic enum ieee80211_opmode
3762138593Ssamget80211opmode(int s)
3763138593Ssam{
3764138593Ssam	struct ifmediareq ifmr;
3765138593Ssam
3766138593Ssam	(void) memset(&ifmr, 0, sizeof(ifmr));
3767138593Ssam	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
3768138593Ssam
3769138593Ssam	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
3770186101Ssam		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) {
3771186101Ssam			if (ifmr.ifm_current & IFM_FLAG0)
3772186101Ssam				return IEEE80211_M_AHDEMO;
3773186101Ssam			else
3774186101Ssam				return IEEE80211_M_IBSS;
3775186101Ssam		}
3776138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
3777138593Ssam			return IEEE80211_M_HOSTAP;
3778138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
3779138593Ssam			return IEEE80211_M_MONITOR;
3780138593Ssam	}
3781138593Ssam	return IEEE80211_M_STA;
3782138593Ssam}
3783138593Ssam
3784138593Ssam#if 0
3785138593Ssamstatic void
3786138593Ssamprintcipher(int s, struct ieee80211req *ireq, int keylenop)
3787138593Ssam{
3788138593Ssam	switch (ireq->i_val) {
3789138593Ssam	case IEEE80211_CIPHER_WEP:
3790138593Ssam		ireq->i_type = keylenop;
3791138593Ssam		if (ioctl(s, SIOCG80211, ireq) != -1)
3792138593Ssam			printf("WEP-%s",
3793138593Ssam			    ireq->i_len <= 5 ? "40" :
3794138593Ssam			    ireq->i_len <= 13 ? "104" : "128");
3795138593Ssam		else
3796138593Ssam			printf("WEP");
3797138593Ssam		break;
3798138593Ssam	case IEEE80211_CIPHER_TKIP:
3799138593Ssam		printf("TKIP");
3800138593Ssam		break;
3801138593Ssam	case IEEE80211_CIPHER_AES_OCB:
3802138593Ssam		printf("AES-OCB");
3803138593Ssam		break;
3804138593Ssam	case IEEE80211_CIPHER_AES_CCM:
3805138593Ssam		printf("AES-CCM");
3806138593Ssam		break;
3807138593Ssam	case IEEE80211_CIPHER_CKIP:
3808138593Ssam		printf("CKIP");
3809138593Ssam		break;
3810138593Ssam	case IEEE80211_CIPHER_NONE:
3811138593Ssam		printf("NONE");
3812138593Ssam		break;
3813138593Ssam	default:
3814138593Ssam		printf("UNKNOWN (0x%x)", ireq->i_val);
3815138593Ssam		break;
3816138593Ssam	}
3817138593Ssam}
3818138593Ssam#endif
3819138593Ssam
3820155931Ssamstatic void
3821138593Ssamprintkey(const struct ieee80211req_key *ik)
3822138593Ssam{
3823138593Ssam	static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
3824138593Ssam	int keylen = ik->ik_keylen;
3825138593Ssam	int printcontents;
3826138593Ssam
3827148001Srwatson	printcontents = printkeys &&
3828138593Ssam		(memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
3829138593Ssam	if (printcontents)
3830138593Ssam		LINE_BREAK();
3831138593Ssam	switch (ik->ik_type) {
3832138593Ssam	case IEEE80211_CIPHER_WEP:
3833138593Ssam		/* compatibility */
3834155931Ssam		LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
3835138593Ssam		    keylen <= 5 ? "40-bit" :
3836138593Ssam		    keylen <= 13 ? "104-bit" : "128-bit");
3837138593Ssam		break;
3838138593Ssam	case IEEE80211_CIPHER_TKIP:
3839138593Ssam		if (keylen > 128/8)
3840138593Ssam			keylen -= 128/8;	/* ignore MIC for now */
3841155931Ssam		LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3842138593Ssam		break;
3843138593Ssam	case IEEE80211_CIPHER_AES_OCB:
3844155931Ssam		LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3845138593Ssam		break;
3846138593Ssam	case IEEE80211_CIPHER_AES_CCM:
3847155931Ssam		LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3848138593Ssam		break;
3849138593Ssam	case IEEE80211_CIPHER_CKIP:
3850155931Ssam		LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3851138593Ssam		break;
3852138593Ssam	case IEEE80211_CIPHER_NONE:
3853155931Ssam		LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
3854138593Ssam		break;
3855138593Ssam	default:
3856155931Ssam		LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
3857138593Ssam			ik->ik_type, ik->ik_keyix+1, 8*keylen);
3858138593Ssam		break;
3859138593Ssam	}
3860138593Ssam	if (printcontents) {
3861138593Ssam		int i;
3862138593Ssam
3863138593Ssam		printf(" <");
3864138593Ssam		for (i = 0; i < keylen; i++)
3865138593Ssam			printf("%02x", ik->ik_keydata[i]);
3866138593Ssam		printf(">");
3867138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
3868138593Ssam		    (ik->ik_keyrsc != 0 || verbose))
3869146873Sjhb			printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
3870138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
3871138593Ssam		    (ik->ik_keytsc != 0 || verbose))
3872146873Sjhb			printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
3873138593Ssam		if (ik->ik_flags != 0 && verbose) {
3874138593Ssam			const char *sep = " ";
3875138593Ssam
3876138593Ssam			if (ik->ik_flags & IEEE80211_KEY_XMIT)
3877138593Ssam				printf("%stx", sep), sep = "+";
3878138593Ssam			if (ik->ik_flags & IEEE80211_KEY_RECV)
3879138593Ssam				printf("%srx", sep), sep = "+";
3880138593Ssam			if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
3881138593Ssam				printf("%sdef", sep), sep = "+";
3882138593Ssam		}
3883138593Ssam		LINE_BREAK();
3884138593Ssam	}
3885138593Ssam}
3886138593Ssam
3887138593Ssamstatic void
3888173275Ssamprintrate(const char *tag, int v, int defrate, int defmcs)
3889138593Ssam{
3890188784Ssam	if ((v & IEEE80211_RATE_MCS) == 0) {
3891188784Ssam		if (v != defrate) {
3892188784Ssam			if (v & 1)
3893188784Ssam				LINE_CHECK("%s %d.5", tag, v/2);
3894188784Ssam			else
3895188784Ssam				LINE_CHECK("%s %d", tag, v/2);
3896188784Ssam		}
3897188784Ssam	} else {
3898173275Ssam		if (v != defmcs)
3899173275Ssam			LINE_CHECK("%s %d", tag, v &~ 0x80);
3900173275Ssam	}
3901173275Ssam}
3902173275Ssam
3903173275Ssamstatic int
3904173275Ssamgetssid(int s, int ix, void *data, size_t len, int *plen)
3905173275Ssam{
3906138593Ssam	struct ieee80211req ireq;
3907138593Ssam
3908138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3909138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3910173275Ssam	ireq.i_type = IEEE80211_IOC_SSID;
3911173275Ssam	ireq.i_val = ix;
3912173275Ssam	ireq.i_data = data;
3913173275Ssam	ireq.i_len = len;
3914173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3915173275Ssam		return -1;
3916173275Ssam	*plen = ireq.i_len;
3917173275Ssam	return 0;
3918173275Ssam}
391977218Sphk
3920173275Ssamstatic void
3921173275Ssamieee80211_status(int s)
3922173275Ssam{
3923173275Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
3924173275Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
3925173275Ssam	int i, num, wpa, wme, bgscan, bgscaninterval, val, len, wepmode;
3926173275Ssam	uint8_t data[32];
3927173275Ssam	const struct ieee80211_channel *c;
3928178354Ssam	const struct ieee80211_roamparam *rp;
3929178354Ssam	const struct ieee80211_txparam *tp;
3930173275Ssam
3931173275Ssam	if (getssid(s, -1, data, sizeof(data), &len) < 0) {
3932148686Sstefanf		/* If we can't get the SSID, this isn't an 802.11 device. */
393377218Sphk		return;
393477218Sphk	}
3935173275Ssam
3936173275Ssam	/*
3937173275Ssam	 * Invalidate cached state so printing status for multiple
3938173275Ssam	 * if's doesn't reuse the first interfaces' cached state.
3939173275Ssam	 */
3940173275Ssam	gotcurchan = 0;
3941178354Ssam	gotroam = 0;
3942178354Ssam	gottxparams = 0;
3943173275Ssam	gothtconf = 0;
3944178354Ssam	gotregdomain = 0;
3945173275Ssam
3946173275Ssam	if (get80211val(s, IEEE80211_IOC_NUMSSIDS, &num) < 0)
3947173275Ssam		num = 0;
3948138593Ssam	printf("\tssid ");
3949138593Ssam	if (num > 1) {
3950173275Ssam		for (i = 0; i < num; i++) {
3951173275Ssam			if (getssid(s, i, data, sizeof(data), &len) >= 0 && len > 0) {
3952173275Ssam				printf(" %d:", i + 1);
3953173275Ssam				print_string(data, len);
3954138593Ssam			}
395588748Sambrisko		}
3956138593Ssam	} else
3957173275Ssam		print_string(data, len);
395877218Sphk
3959173275Ssam	c = getcurchan(s);
3960170531Ssam	if (c->ic_freq != IEEE80211_CHAN_ANY) {
3961170531Ssam		char buf[14];
3962170531Ssam		printf(" channel %d (%u Mhz%s)", c->ic_ieee, c->ic_freq,
3963170531Ssam			get_chaninfo(c, 1, buf, sizeof(buf)));
3964138593Ssam	} else if (verbose)
3965138593Ssam		printf(" channel UNDEF");
3966138593Ssam
3967173275Ssam	if (get80211(s, IEEE80211_IOC_BSSID, data, IEEE80211_ADDR_LEN) >= 0 &&
3968173275Ssam	    (memcmp(data, zerobssid, sizeof(zerobssid)) != 0 || verbose))
3969173275Ssam		printf(" bssid %s", ether_ntoa((struct ether_addr *)data));
3970138593Ssam
3971173275Ssam	if (get80211len(s, IEEE80211_IOC_STATIONNAME, data, sizeof(data), &len) != -1) {
3972138593Ssam		printf("\n\tstationname ");
3973173275Ssam		print_string(data, len);
397477218Sphk	}
397577218Sphk
3976138593Ssam	spacer = ' ';		/* force first break */
3977138593Ssam	LINE_BREAK();
397877218Sphk
3979178354Ssam	list_regdomain(s, 0);
3980178354Ssam
3981173275Ssam	wpa = 0;
3982173275Ssam	if (get80211val(s, IEEE80211_IOC_AUTHMODE, &val) != -1) {
3983173275Ssam		switch (val) {
3984173275Ssam		case IEEE80211_AUTH_NONE:
3985173275Ssam			LINE_CHECK("authmode NONE");
3986173275Ssam			break;
3987173275Ssam		case IEEE80211_AUTH_OPEN:
3988173275Ssam			LINE_CHECK("authmode OPEN");
3989173275Ssam			break;
3990173275Ssam		case IEEE80211_AUTH_SHARED:
3991173275Ssam			LINE_CHECK("authmode SHARED");
3992173275Ssam			break;
3993173275Ssam		case IEEE80211_AUTH_8021X:
3994173275Ssam			LINE_CHECK("authmode 802.1x");
3995173275Ssam			break;
3996173275Ssam		case IEEE80211_AUTH_WPA:
3997173275Ssam			if (get80211val(s, IEEE80211_IOC_WPA, &wpa) < 0)
3998173275Ssam				wpa = 1;	/* default to WPA1 */
3999173275Ssam			switch (wpa) {
4000173275Ssam			case 2:
4001173275Ssam				LINE_CHECK("authmode WPA2/802.11i");
400277218Sphk				break;
4003173275Ssam			case 3:
4004173275Ssam				LINE_CHECK("authmode WPA1+WPA2/802.11i");
400577218Sphk				break;
4006127649Ssam			default:
4007173275Ssam				LINE_CHECK("authmode WPA");
4008127649Ssam				break;
4009173275Ssam			}
4010173275Ssam			break;
4011173275Ssam		case IEEE80211_AUTH_AUTO:
4012173275Ssam			LINE_CHECK("authmode AUTO");
4013173275Ssam			break;
4014173275Ssam		default:
4015173275Ssam			LINE_CHECK("authmode UNKNOWN (0x%x)", val);
4016173275Ssam			break;
4017127649Ssam		}
4018127649Ssam	}
4019127649Ssam
4020173275Ssam	if (wpa || verbose) {
4021178354Ssam		if (get80211val(s, IEEE80211_IOC_WPS, &val) != -1) {
4022178354Ssam			if (val)
4023178354Ssam				LINE_CHECK("wps");
4024178354Ssam			else if (verbose)
4025178354Ssam				LINE_CHECK("-wps");
4026178354Ssam		}
4027178354Ssam		if (get80211val(s, IEEE80211_IOC_TSN, &val) != -1) {
4028178354Ssam			if (val)
4029178354Ssam				LINE_CHECK("tsn");
4030178354Ssam			else if (verbose)
4031178354Ssam				LINE_CHECK("-tsn");
4032178354Ssam		}
4033173275Ssam		if (ioctl(s, IEEE80211_IOC_COUNTERMEASURES, &val) != -1) {
4034173275Ssam			if (val)
4035173275Ssam				LINE_CHECK("countermeasures");
4036173275Ssam			else if (verbose)
4037173275Ssam				LINE_CHECK("-countermeasures");
4038173275Ssam		}
4039178354Ssam#if 0
4040178354Ssam		/* XXX not interesting with WPA done in user space */
4041178354Ssam		ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
4042178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4043178354Ssam		}
4044178354Ssam
4045178354Ssam		ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
4046178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4047178354Ssam			LINE_CHECK("mcastcipher ");
4048178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
4049178354Ssam			spacer = ' ';
4050178354Ssam		}
4051178354Ssam
4052178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
4053178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4054178354Ssam			LINE_CHECK("ucastcipher ");
4055178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
4056178354Ssam		}
4057178354Ssam
4058178354Ssam		if (wpa & 2) {
4059178354Ssam			ireq.i_type = IEEE80211_IOC_RSNCAPS;
4060178354Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1) {
4061178354Ssam				LINE_CHECK("RSN caps 0x%x", ireq.i_val);
4062178354Ssam				spacer = ' ';
4063178354Ssam			}
4064178354Ssam		}
4065178354Ssam
4066178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
4067178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4068178354Ssam		}
4069178354Ssam#endif
4070173275Ssam	}
4071138593Ssam
4072173275Ssam	if (get80211val(s, IEEE80211_IOC_WEP, &wepmode) != -1 &&
4073173275Ssam	    wepmode != IEEE80211_WEP_NOSUP) {
4074173275Ssam		int firstkey;
4075173275Ssam
4076138718Ssam		switch (wepmode) {
4077173275Ssam		case IEEE80211_WEP_OFF:
4078173275Ssam			LINE_CHECK("privacy OFF");
4079173275Ssam			break;
4080173275Ssam		case IEEE80211_WEP_ON:
4081173275Ssam			LINE_CHECK("privacy ON");
4082173275Ssam			break;
4083173275Ssam		case IEEE80211_WEP_MIXED:
4084173275Ssam			LINE_CHECK("privacy MIXED");
4085173275Ssam			break;
4086173275Ssam		default:
4087173275Ssam			LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
4088173275Ssam			break;
408977218Sphk		}
409077218Sphk
409177218Sphk		/*
409277218Sphk		 * If we get here then we've got WEP support so we need
409377218Sphk		 * to print WEP status.
409491454Sbrooks		 */
409577218Sphk
4096173275Ssam		if (get80211val(s, IEEE80211_IOC_WEPTXKEY, &val) < 0) {
409777218Sphk			warn("WEP support, but no tx key!");
409877218Sphk			goto end;
409977218Sphk		}
4100173275Ssam		if (val != -1)
4101173275Ssam			LINE_CHECK("deftxkey %d", val+1);
4102138718Ssam		else if (wepmode != IEEE80211_WEP_OFF || verbose)
4103155931Ssam			LINE_CHECK("deftxkey UNDEF");
410477218Sphk
4105173275Ssam		if (get80211val(s, IEEE80211_IOC_NUMWEPKEYS, &num) < 0) {
410677218Sphk			warn("WEP support, but no NUMWEPKEYS support!");
410777218Sphk			goto end;
410877218Sphk		}
410977218Sphk
4110138593Ssam		firstkey = 1;
4111138593Ssam		for (i = 0; i < num; i++) {
4112138593Ssam			struct ieee80211req_key ik;
411377218Sphk
4114138593Ssam			memset(&ik, 0, sizeof(ik));
4115138593Ssam			ik.ik_keyix = i;
4116173275Ssam			if (get80211(s, IEEE80211_IOC_WPAKEY, &ik, sizeof(ik)) < 0) {
411777218Sphk				warn("WEP support, but can get keys!");
411877218Sphk				goto end;
411977218Sphk			}
4120138593Ssam			if (ik.ik_keylen != 0) {
4121138593Ssam				if (verbose)
4122138593Ssam					LINE_BREAK();
4123138593Ssam				printkey(&ik);
4124138593Ssam				firstkey = 0;
4125138593Ssam			}
4126138593Ssam		}
4127173275Ssamend:
4128173275Ssam		;
4129138593Ssam	}
4130138593Ssam
4131173275Ssam	if (get80211val(s, IEEE80211_IOC_POWERSAVE, &val) != -1 &&
4132173275Ssam	    val != IEEE80211_POWERSAVE_NOSUP ) {
4133173275Ssam		if (val != IEEE80211_POWERSAVE_OFF || verbose) {
4134173275Ssam			switch (val) {
4135173275Ssam			case IEEE80211_POWERSAVE_OFF:
4136173275Ssam				LINE_CHECK("powersavemode OFF");
4137173275Ssam				break;
4138173275Ssam			case IEEE80211_POWERSAVE_CAM:
4139173275Ssam				LINE_CHECK("powersavemode CAM");
4140173275Ssam				break;
4141173275Ssam			case IEEE80211_POWERSAVE_PSP:
4142173275Ssam				LINE_CHECK("powersavemode PSP");
4143173275Ssam				break;
4144173275Ssam			case IEEE80211_POWERSAVE_PSP_CAM:
4145173275Ssam				LINE_CHECK("powersavemode PSP-CAM");
4146173275Ssam				break;
4147138593Ssam			}
4148173275Ssam			if (get80211val(s, IEEE80211_IOC_POWERSAVESLEEP, &val) != -1)
4149173275Ssam				LINE_CHECK("powersavesleep %d", val);
4150138593Ssam		}
4151138593Ssam	}
4152138593Ssam
4153173275Ssam	if (get80211val(s, IEEE80211_IOC_TXPOWER, &val) != -1) {
4154173275Ssam		if (val & 1)
4155173275Ssam			LINE_CHECK("txpower %d.5", val/2);
4156173275Ssam		else
4157173275Ssam			LINE_CHECK("txpower %d", val/2);
4158173275Ssam	}
4159138593Ssam	if (verbose) {
4160173275Ssam		if (get80211val(s, IEEE80211_IOC_TXPOWMAX, &val) != -1)
4161173275Ssam			LINE_CHECK("txpowmax %.1f", val/2.);
4162138593Ssam	}
4163138593Ssam
4164178354Ssam	if (get80211val(s, IEEE80211_IOC_DOTD, &val) != -1) {
4165178354Ssam		if (val)
4166178354Ssam			LINE_CHECK("dotd");
4167178354Ssam		else if (verbose)
4168178354Ssam			LINE_CHECK("-dotd");
4169178354Ssam	}
4170178354Ssam
4171173275Ssam	if (get80211val(s, IEEE80211_IOC_RTSTHRESHOLD, &val) != -1) {
4172173275Ssam		if (val != IEEE80211_RTS_MAX || verbose)
4173173275Ssam			LINE_CHECK("rtsthreshold %d", val);
4174138593Ssam	}
4175138593Ssam
4176173275Ssam	if (get80211val(s, IEEE80211_IOC_FRAGTHRESHOLD, &val) != -1) {
4177173275Ssam		if (val != IEEE80211_FRAG_MAX || verbose)
4178173275Ssam			LINE_CHECK("fragthreshold %d", val);
4179170531Ssam	}
4180173275Ssam	if (opmode == IEEE80211_M_STA || verbose) {
4181173275Ssam		if (get80211val(s, IEEE80211_IOC_BMISSTHRESHOLD, &val) != -1) {
4182173275Ssam			if (val != IEEE80211_HWBMISS_MAX || verbose)
4183173275Ssam				LINE_CHECK("bmiss %d", val);
4184153354Ssam		}
4185153354Ssam	}
4186153354Ssam
4187178354Ssam	if (!verbose) {
4188178354Ssam		gettxparams(s);
4189178354Ssam		tp = &txparams.params[chan2mode(c)];
4190178354Ssam		printrate("ucastrate", tp->ucastrate,
4191178354Ssam		    IEEE80211_FIXED_RATE_NONE, IEEE80211_FIXED_RATE_NONE);
4192188784Ssam		printrate("mcastrate", tp->mcastrate, 2*1,
4193188784Ssam		    IEEE80211_RATE_MCS|0);
4194188784Ssam		printrate("mgmtrate", tp->mgmtrate, 2*1,
4195188784Ssam		    IEEE80211_RATE_MCS|0);
4196178354Ssam		if (tp->maxretry != 6)		/* XXX */
4197178354Ssam			LINE_CHECK("maxretry %d", tp->maxretry);
4198178354Ssam	} else {
4199178354Ssam		LINE_BREAK();
4200178354Ssam		list_txparams(s);
4201178354Ssam	}
4202170531Ssam
4203173275Ssam	bgscaninterval = -1;
4204173275Ssam	(void) get80211val(s, IEEE80211_IOC_BGSCAN_INTERVAL, &bgscaninterval);
4205173275Ssam
4206173275Ssam	if (get80211val(s, IEEE80211_IOC_SCANVALID, &val) != -1) {
4207173275Ssam		if (val != bgscaninterval || verbose)
4208173275Ssam			LINE_CHECK("scanvalid %u", val);
4209148416Ssam	}
4210148416Ssam
4211173275Ssam	bgscan = 0;
4212173275Ssam	if (get80211val(s, IEEE80211_IOC_BGSCAN, &bgscan) != -1) {
4213173275Ssam		if (bgscan)
4214170531Ssam			LINE_CHECK("bgscan");
4215170531Ssam		else if (verbose)
4216170531Ssam			LINE_CHECK("-bgscan");
4217160687Ssam	}
4218170531Ssam	if (bgscan || verbose) {
4219170531Ssam		if (bgscaninterval != -1)
4220170531Ssam			LINE_CHECK("bgscanintvl %u", bgscaninterval);
4221173275Ssam		if (get80211val(s, IEEE80211_IOC_BGSCAN_IDLE, &val) != -1)
4222173275Ssam			LINE_CHECK("bgscanidle %u", val);
4223178354Ssam		if (!verbose) {
4224178354Ssam			getroam(s);
4225178354Ssam			rp = &roamparams.params[chan2mode(c)];
4226178354Ssam			if (rp->rssi & 1)
4227178354Ssam				LINE_CHECK("roam:rssi %u.5", rp->rssi/2);
4228178354Ssam			else
4229178354Ssam				LINE_CHECK("roam:rssi %u", rp->rssi/2);
4230178354Ssam			LINE_CHECK("roam:rate %u", rp->rate/2);
4231178354Ssam		} else {
4232178354Ssam			LINE_BREAK();
4233178354Ssam			list_roam(s);
4234170531Ssam		}
4235170531Ssam	}
4236160687Ssam
4237165570Ssam	if (IEEE80211_IS_CHAN_ANYG(c) || verbose) {
4238173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREG, &val) != -1) {
4239173275Ssam			if (val)
4240155931Ssam				LINE_CHECK("pureg");
4241147795Ssam			else if (verbose)
4242155931Ssam				LINE_CHECK("-pureg");
4243147795Ssam		}
4244173275Ssam		if (get80211val(s, IEEE80211_IOC_PROTMODE, &val) != -1) {
4245173275Ssam			switch (val) {
4246173275Ssam			case IEEE80211_PROTMODE_OFF:
4247173275Ssam				LINE_CHECK("protmode OFF");
4248173275Ssam				break;
4249173275Ssam			case IEEE80211_PROTMODE_CTS:
4250173275Ssam				LINE_CHECK("protmode CTS");
4251173275Ssam				break;
4252173275Ssam			case IEEE80211_PROTMODE_RTSCTS:
4253173275Ssam				LINE_CHECK("protmode RTSCTS");
4254173275Ssam				break;
4255173275Ssam			default:
4256173275Ssam				LINE_CHECK("protmode UNKNOWN (0x%x)", val);
4257173275Ssam				break;
4258138593Ssam			}
4259138593Ssam		}
4260138593Ssam	}
4261138593Ssam
4262173275Ssam	if (IEEE80211_IS_CHAN_HT(c) || verbose) {
4263173275Ssam		gethtconf(s);
4264173275Ssam		switch (htconf & 3) {
4265173275Ssam		case 0:
4266173275Ssam		case 2:
4267173275Ssam			LINE_CHECK("-ht");
4268173275Ssam			break;
4269173275Ssam		case 1:
4270173275Ssam			LINE_CHECK("ht20");
4271173275Ssam			break;
4272173275Ssam		case 3:
4273173275Ssam			if (verbose)
4274173275Ssam				LINE_CHECK("ht");
4275173275Ssam			break;
4276173275Ssam		}
4277173275Ssam		if (get80211val(s, IEEE80211_IOC_HTCOMPAT, &val) != -1) {
4278173275Ssam			if (!val)
4279173275Ssam				LINE_CHECK("-htcompat");
4280173275Ssam			else if (verbose)
4281173275Ssam				LINE_CHECK("htcompat");
4282173275Ssam		}
4283173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU, &val) != -1) {
4284173275Ssam			switch (val) {
4285173275Ssam			case 0:
4286173275Ssam				LINE_CHECK("-ampdu");
4287173275Ssam				break;
4288173275Ssam			case 1:
4289173275Ssam				LINE_CHECK("ampdutx -ampdurx");
4290173275Ssam				break;
4291173275Ssam			case 2:
4292173275Ssam				LINE_CHECK("-ampdutx ampdurx");
4293173275Ssam				break;
4294173275Ssam			case 3:
4295173275Ssam				if (verbose)
4296173275Ssam					LINE_CHECK("ampdu");
4297173275Ssam				break;
4298173275Ssam			}
4299173275Ssam		}
4300173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_LIMIT, &val) != -1) {
4301173275Ssam			switch (val) {
4302173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_8K:
4303173275Ssam				LINE_CHECK("ampdulimit 8k");
4304173275Ssam				break;
4305173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_16K:
4306173275Ssam				LINE_CHECK("ampdulimit 16k");
4307173275Ssam				break;
4308173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_32K:
4309173275Ssam				LINE_CHECK("ampdulimit 32k");
4310173275Ssam				break;
4311173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_64K:
4312173275Ssam				LINE_CHECK("ampdulimit 64k");
4313173275Ssam				break;
4314173275Ssam			}
4315173275Ssam		}
4316173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_DENSITY, &val) != -1) {
4317173275Ssam			switch (val) {
4318173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_NA:
4319173275Ssam				if (verbose)
4320183260Ssam					LINE_CHECK("ampdudensity NA");
4321173275Ssam				break;
4322173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_025:
4323173275Ssam				LINE_CHECK("ampdudensity .25");
4324173275Ssam				break;
4325173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_05:
4326173275Ssam				LINE_CHECK("ampdudensity .5");
4327173275Ssam				break;
4328173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_1:
4329173275Ssam				LINE_CHECK("ampdudensity 1");
4330173275Ssam				break;
4331173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_2:
4332173275Ssam				LINE_CHECK("ampdudensity 2");
4333173275Ssam				break;
4334173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_4:
4335173275Ssam				LINE_CHECK("ampdudensity 4");
4336173275Ssam				break;
4337173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_8:
4338173275Ssam				LINE_CHECK("ampdudensity 8");
4339173275Ssam				break;
4340173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_16:
4341173275Ssam				LINE_CHECK("ampdudensity 16");
4342173275Ssam				break;
4343173275Ssam			}
4344173275Ssam		}
4345173275Ssam		if (get80211val(s, IEEE80211_IOC_AMSDU, &val) != -1) {
4346173275Ssam			switch (val) {
4347173275Ssam			case 0:
4348173275Ssam				LINE_CHECK("-amsdu");
4349173275Ssam				break;
4350173275Ssam			case 1:
4351173275Ssam				LINE_CHECK("amsdutx -amsdurx");
4352173275Ssam				break;
4353173275Ssam			case 2:
4354173275Ssam				LINE_CHECK("-amsdutx amsdurx");
4355173275Ssam				break;
4356173275Ssam			case 3:
4357173275Ssam				if (verbose)
4358173275Ssam					LINE_CHECK("amsdu");
4359173275Ssam				break;
4360173275Ssam			}
4361173275Ssam		}
4362173275Ssam		/* XXX amsdu limit */
4363173275Ssam		if (get80211val(s, IEEE80211_IOC_SHORTGI, &val) != -1) {
4364173275Ssam			if (val)
4365173275Ssam				LINE_CHECK("shortgi");
4366173275Ssam			else if (verbose)
4367173275Ssam				LINE_CHECK("-shortgi");
4368173275Ssam		}
4369173275Ssam		if (get80211val(s, IEEE80211_IOC_HTPROTMODE, &val) != -1) {
4370173275Ssam			if (val == IEEE80211_PROTMODE_OFF)
4371173275Ssam				LINE_CHECK("htprotmode OFF");
4372173275Ssam			else if (val != IEEE80211_PROTMODE_RTSCTS)
4373173275Ssam				LINE_CHECK("htprotmode UNKNOWN (0x%x)", val);
4374173275Ssam			else if (verbose)
4375173275Ssam				LINE_CHECK("htprotmode RTSCTS");
4376173275Ssam		}
4377173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREN, &val) != -1) {
4378173275Ssam			if (val)
4379173275Ssam				LINE_CHECK("puren");
4380173275Ssam			else if (verbose)
4381173275Ssam				LINE_CHECK("-puren");
4382173275Ssam		}
4383183261Ssam		if (get80211val(s, IEEE80211_IOC_SMPS, &val) != -1) {
4384183261Ssam			if (val == IEEE80211_HTCAP_SMPS_DYNAMIC)
4385183261Ssam				LINE_CHECK("smpsdyn");
4386183261Ssam			else if (val == IEEE80211_HTCAP_SMPS_ENA)
4387183261Ssam				LINE_CHECK("smps");
4388183261Ssam			else if (verbose)
4389183261Ssam				LINE_CHECK("-smps");
4390183261Ssam		}
4391183261Ssam		if (get80211val(s, IEEE80211_IOC_RIFS, &val) != -1) {
4392183261Ssam			if (val)
4393183261Ssam				LINE_CHECK("rifs");
4394183261Ssam			else if (verbose)
4395183261Ssam				LINE_CHECK("-rifs");
4396183261Ssam		}
4397173275Ssam	}
4398173275Ssam
4399173275Ssam	if (get80211val(s, IEEE80211_IOC_WME, &wme) != -1) {
4400138593Ssam		if (wme)
4401155931Ssam			LINE_CHECK("wme");
4402138593Ssam		else if (verbose)
4403155931Ssam			LINE_CHECK("-wme");
4404138593Ssam	} else
4405138593Ssam		wme = 0;
4406138593Ssam
4407173275Ssam	if (get80211val(s, IEEE80211_IOC_BURST, &val) != -1) {
4408173275Ssam		if (val)
4409155931Ssam			LINE_CHECK("burst");
4410153422Ssam		else if (verbose)
4411155931Ssam			LINE_CHECK("-burst");
4412153422Ssam	}
4413153422Ssam
4414173275Ssam	if (get80211val(s, IEEE80211_IOC_FF, &val) != -1) {
4415173275Ssam		if (val)
4416170531Ssam			LINE_CHECK("ff");
4417170531Ssam		else if (verbose)
4418170531Ssam			LINE_CHECK("-ff");
4419170531Ssam	}
4420173275Ssam	if (get80211val(s, IEEE80211_IOC_TURBOP, &val) != -1) {
4421173275Ssam		if (val)
4422170531Ssam			LINE_CHECK("dturbo");
4423170531Ssam		else if (verbose)
4424170531Ssam			LINE_CHECK("-dturbo");
4425170531Ssam	}
4426178354Ssam	if (get80211val(s, IEEE80211_IOC_DWDS, &val) != -1) {
4427178354Ssam		if (val)
4428178354Ssam			LINE_CHECK("dwds");
4429178354Ssam		else if (verbose)
4430178354Ssam			LINE_CHECK("-dwds");
4431178354Ssam	}
4432170531Ssam
4433138593Ssam	if (opmode == IEEE80211_M_HOSTAP) {
4434173275Ssam		if (get80211val(s, IEEE80211_IOC_HIDESSID, &val) != -1) {
4435173275Ssam			if (val)
4436168075Ssam				LINE_CHECK("hidessid");
4437138593Ssam			else if (verbose)
4438168075Ssam				LINE_CHECK("-hidessid");
4439138593Ssam		}
4440173275Ssam		if (get80211val(s, IEEE80211_IOC_APBRIDGE, &val) != -1) {
4441173275Ssam			if (!val)
4442155931Ssam				LINE_CHECK("-apbridge");
4443138593Ssam			else if (verbose)
4444155931Ssam				LINE_CHECK("apbridge");
4445138593Ssam		}
4446173275Ssam		if (get80211val(s, IEEE80211_IOC_DTIM_PERIOD, &val) != -1)
4447173275Ssam			LINE_CHECK("dtimperiod %u", val);
4448138593Ssam
4449173275Ssam		if (get80211val(s, IEEE80211_IOC_DOTH, &val) != -1) {
4450173275Ssam			if (!val)
4451170531Ssam				LINE_CHECK("-doth");
4452170531Ssam			else if (verbose)
4453170531Ssam				LINE_CHECK("doth");
4454170531Ssam		}
4455178354Ssam		if (get80211val(s, IEEE80211_IOC_DFS, &val) != -1) {
4456178354Ssam			if (!val)
4457178354Ssam				LINE_CHECK("-dfs");
4458178354Ssam			else if (verbose)
4459178354Ssam				LINE_CHECK("dfs");
4460178354Ssam		}
4461173275Ssam		if (get80211val(s, IEEE80211_IOC_INACTIVITY, &val) != -1) {
4462173275Ssam			if (!val)
4463173275Ssam				LINE_CHECK("-inact");
4464173275Ssam			else if (verbose)
4465173275Ssam				LINE_CHECK("inact");
4466173275Ssam		}
4467138593Ssam	} else {
4468173275Ssam		if (get80211val(s, IEEE80211_IOC_ROAMING, &val) != -1) {
4469173275Ssam			if (val != IEEE80211_ROAMING_AUTO || verbose) {
4470173275Ssam				switch (val) {
4471138593Ssam				case IEEE80211_ROAMING_DEVICE:
4472155931Ssam					LINE_CHECK("roaming DEVICE");
4473138593Ssam					break;
4474138593Ssam				case IEEE80211_ROAMING_AUTO:
4475155931Ssam					LINE_CHECK("roaming AUTO");
4476138593Ssam					break;
4477138593Ssam				case IEEE80211_ROAMING_MANUAL:
4478155931Ssam					LINE_CHECK("roaming MANUAL");
4479138593Ssam					break;
4480138593Ssam				default:
4481155931Ssam					LINE_CHECK("roaming UNKNOWN (0x%x)",
4482173275Ssam						val);
4483138593Ssam					break;
4484138593Ssam				}
4485138593Ssam			}
4486138593Ssam		}
4487138593Ssam	}
4488186101Ssam
4489186904Ssam	if (opmode == IEEE80211_M_AHDEMO) {
4490186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOT, &val) != -1)
4491186904Ssam			LINE_CHECK("tdmaslot %u", val);
4492186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTCNT, &val) != -1)
4493186904Ssam			LINE_CHECK("tdmaslotcnt %u", val);
4494186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTLEN, &val) != -1)
4495186904Ssam			LINE_CHECK("tdmaslotlen %u", val);
4496186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_BINTERVAL, &val) != -1)
4497186904Ssam			LINE_CHECK("tdmabintval %u", val);
4498186904Ssam	} else if (get80211val(s, IEEE80211_IOC_BEACON_INTERVAL, &val) != -1) {
4499173275Ssam		/* XXX default define not visible */
4500173275Ssam		if (val != 100 || verbose)
4501173275Ssam			LINE_CHECK("bintval %u", val);
4502138593Ssam	}
4503138593Ssam
4504138593Ssam	if (wme && verbose) {
4505138593Ssam		LINE_BREAK();
4506138593Ssam		list_wme(s);
4507138593Ssam	}
4508173275Ssam	LINE_BREAK();
4509173275Ssam}
4510138593Ssam
4511173275Ssamstatic int
4512173275Ssamget80211(int s, int type, void *data, int len)
4513173275Ssam{
4514173275Ssam	struct ieee80211req ireq;
4515138593Ssam
4516173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4517173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4518173275Ssam	ireq.i_type = type;
4519173275Ssam	ireq.i_data = data;
4520173275Ssam	ireq.i_len = len;
4521173275Ssam	return ioctl(s, SIOCG80211, &ireq);
4522173275Ssam}
4523138593Ssam
4524173275Ssamstatic int
4525173275Ssamget80211len(int s, int type, void *data, int len, int *plen)
4526173275Ssam{
4527173275Ssam	struct ieee80211req ireq;
4528138593Ssam
4529173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4530173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4531173275Ssam	ireq.i_type = type;
4532173275Ssam	ireq.i_len = len;
4533187801Ssam	assert(ireq.i_len == len);	/* NB: check for 16-bit truncation */
4534173275Ssam	ireq.i_data = data;
4535173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4536173275Ssam		return -1;
4537173275Ssam	*plen = ireq.i_len;
4538173275Ssam	return 0;
4539173275Ssam}
4540138593Ssam
4541173275Ssamstatic int
4542173275Ssamget80211val(int s, int type, int *val)
4543173275Ssam{
4544173275Ssam	struct ieee80211req ireq;
454577218Sphk
4546173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4547173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4548173275Ssam	ireq.i_type = type;
4549173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4550173275Ssam		return -1;
4551173275Ssam	*val = ireq.i_val;
4552173275Ssam	return 0;
455377218Sphk}
455477218Sphk
455577218Sphkstatic void
4556170531Ssamset80211(int s, int type, int val, int len, void *data)
455777218Sphk{
455877218Sphk	struct ieee80211req	ireq;
455977218Sphk
456077218Sphk	(void) memset(&ireq, 0, sizeof(ireq));
456177218Sphk	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
456277218Sphk	ireq.i_type = type;
456377218Sphk	ireq.i_val = val;
456477218Sphk	ireq.i_len = len;
4565187801Ssam	assert(ireq.i_len == len);	/* NB: check for 16-bit truncation */
456677218Sphk	ireq.i_data = data;
456791454Sbrooks	if (ioctl(s, SIOCS80211, &ireq) < 0)
456877218Sphk		err(1, "SIOCS80211");
456977218Sphk}
457077218Sphk
457177218Sphkstatic const char *
457277218Sphkget_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
457377218Sphk{
457477218Sphk	int len;
457577218Sphk	int hexstr;
457677218Sphk	u_int8_t *p;
457777218Sphk
457877218Sphk	len = *lenp;
457977218Sphk	p = buf;
458077218Sphk	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
458177218Sphk	if (hexstr)
458277218Sphk		val += 2;
458377218Sphk	for (;;) {
458477218Sphk		if (*val == '\0')
458577218Sphk			break;
458677218Sphk		if (sep != NULL && strchr(sep, *val) != NULL) {
458777218Sphk			val++;
458877218Sphk			break;
458977218Sphk		}
459077218Sphk		if (hexstr) {
4591127831Sphk			if (!isxdigit((u_char)val[0])) {
459277218Sphk				warnx("bad hexadecimal digits");
459377218Sphk				return NULL;
459477218Sphk			}
4595127831Sphk			if (!isxdigit((u_char)val[1])) {
4596127831Sphk				warnx("odd count hexadecimal digits");
4597127831Sphk				return NULL;
4598127831Sphk			}
459977218Sphk		}
4600127831Sphk		if (p >= buf + len) {
460177218Sphk			if (hexstr)
460277218Sphk				warnx("hexadecimal digits too long");
460377218Sphk			else
4604127831Sphk				warnx("string too long");
460577218Sphk			return NULL;
460677218Sphk		}
460777218Sphk		if (hexstr) {
460877218Sphk#define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
460977218Sphk			*p++ = (tohex((u_char)val[0]) << 4) |
461077218Sphk			    tohex((u_char)val[1]);
461177218Sphk#undef tohex
461277218Sphk			val += 2;
461377218Sphk		} else
461477218Sphk			*p++ = *val++;
461577218Sphk	}
461677218Sphk	len = p - buf;
461777218Sphk	/* The string "-" is treated as the empty string. */
4618165045Ssam	if (!hexstr && len == 1 && buf[0] == '-') {
461977218Sphk		len = 0;
4620165045Ssam		memset(buf, 0, *lenp);
4621165045Ssam	} else if (len < *lenp)
462277218Sphk		memset(p, 0, *lenp - len);
462377218Sphk	*lenp = len;
462477218Sphk	return val;
462577218Sphk}
462677218Sphk
462777218Sphkstatic void
462877218Sphkprint_string(const u_int8_t *buf, int len)
462977218Sphk{
463077218Sphk	int i;
463177218Sphk	int hasspc;
463277218Sphk
463377218Sphk	i = 0;
463477218Sphk	hasspc = 0;
463591454Sbrooks	for (; i < len; i++) {
463677218Sphk		if (!isprint(buf[i]) && buf[i] != '\0')
463777218Sphk			break;
463877218Sphk		if (isspace(buf[i]))
463977218Sphk			hasspc++;
464077218Sphk	}
464177218Sphk	if (i == len) {
464277218Sphk		if (hasspc || len == 0 || buf[0] == '\0')
464377218Sphk			printf("\"%.*s\"", len, buf);
464477218Sphk		else
464577218Sphk			printf("%.*s", len, buf);
464677218Sphk	} else {
464777218Sphk		printf("0x");
464877218Sphk		for (i = 0; i < len; i++)
464977218Sphk			printf("%02x", buf[i]);
465077218Sphk	}
465177218Sphk}
465277218Sphk
4653178354Ssam/*
4654178354Ssam * Virtual AP cloning support.
4655178354Ssam */
4656178354Ssamstatic struct ieee80211_clone_params params = {
4657178354Ssam	.icp_opmode	= IEEE80211_M_STA,	/* default to station mode */
4658178354Ssam};
4659178354Ssam
4660178354Ssamstatic void
4661178354Ssamwlan_create(int s, struct ifreq *ifr)
4662178354Ssam{
4663178354Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
4664178354Ssam
4665178354Ssam	if (params.icp_parent[0] == '\0')
4666189096Srpaulo		errx(1, "must specify a parent device (wlandev) when creating "
4667189096Srpaulo		    "a wlan device");
4668178354Ssam	if (params.icp_opmode == IEEE80211_M_WDS &&
4669178354Ssam	    memcmp(params.icp_bssid, zerobssid, sizeof(zerobssid)) == 0)
4670178354Ssam		errx(1, "no bssid specified for WDS (use wlanbssid)");
4671178354Ssam	ifr->ifr_data = (caddr_t) &params;
4672178354Ssam	if (ioctl(s, SIOCIFCREATE2, ifr) < 0)
4673178354Ssam		err(1, "SIOCIFCREATE2");
4674178354Ssam}
4675178354Ssam
4676178354Ssamstatic
4677178354SsamDECL_CMD_FUNC(set80211clone_wlandev, arg, d)
4678178354Ssam{
4679178354Ssam	strlcpy(params.icp_parent, arg, IFNAMSIZ);
4680178354Ssam}
4681178354Ssam
4682178354Ssamstatic
4683178354SsamDECL_CMD_FUNC(set80211clone_wlanbssid, arg, d)
4684178354Ssam{
4685178354Ssam	const struct ether_addr *ea;
4686178354Ssam
4687178354Ssam	ea = ether_aton(arg);
4688178354Ssam	if (ea == NULL)
4689178354Ssam		errx(1, "%s: cannot parse bssid", arg);
4690178354Ssam	memcpy(params.icp_bssid, ea->octet, IEEE80211_ADDR_LEN);
4691178354Ssam}
4692178354Ssam
4693178354Ssamstatic
4694178354SsamDECL_CMD_FUNC(set80211clone_wlanaddr, arg, d)
4695178354Ssam{
4696178354Ssam	const struct ether_addr *ea;
4697178354Ssam
4698178354Ssam	ea = ether_aton(arg);
4699178354Ssam	if (ea == NULL)
4700178354Ssam		errx(1, "%s: cannot parse addres", arg);
4701178354Ssam	memcpy(params.icp_macaddr, ea->octet, IEEE80211_ADDR_LEN);
4702178354Ssam	params.icp_flags |= IEEE80211_CLONE_MACADDR;
4703178354Ssam}
4704178354Ssam
4705178354Ssamstatic
4706178354SsamDECL_CMD_FUNC(set80211clone_wlanmode, arg, d)
4707178354Ssam{
4708178354Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
4709178354Ssam	if (iseq(arg, "sta"))
4710178354Ssam		params.icp_opmode = IEEE80211_M_STA;
4711178354Ssam	else if (iseq(arg, "ahdemo") || iseq(arg, "adhoc-demo"))
4712178354Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
4713178354Ssam	else if (iseq(arg, "ibss") || iseq(arg, "adhoc"))
4714178354Ssam		params.icp_opmode = IEEE80211_M_IBSS;
4715178354Ssam	else if (iseq(arg, "ap") || iseq(arg, "host"))
4716178354Ssam		params.icp_opmode = IEEE80211_M_HOSTAP;
4717178354Ssam	else if (iseq(arg, "wds"))
4718178354Ssam		params.icp_opmode = IEEE80211_M_WDS;
4719178354Ssam	else if (iseq(arg, "monitor"))
4720178354Ssam		params.icp_opmode = IEEE80211_M_MONITOR;
4721186904Ssam	else if (iseq(arg, "tdma")) {
4722186904Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
4723186904Ssam		params.icp_flags |= IEEE80211_CLONE_TDMA;
4724186904Ssam	} else
4725178354Ssam		errx(1, "Don't know to create %s for %s", arg, name);
4726178354Ssam#undef iseq
4727178354Ssam}
4728178354Ssam
4729178354Ssamstatic void
4730178354Ssamset80211clone_beacons(const char *val, int d, int s, const struct afswtch *rafp)
4731178354Ssam{
4732178354Ssam	/* NB: inverted sense */
4733178354Ssam	if (d)
4734178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_NOBEACONS;
4735178354Ssam	else
4736178354Ssam		params.icp_flags |= IEEE80211_CLONE_NOBEACONS;
4737178354Ssam}
4738178354Ssam
4739178354Ssamstatic void
4740178354Ssamset80211clone_bssid(const char *val, int d, int s, const struct afswtch *rafp)
4741178354Ssam{
4742178354Ssam	if (d)
4743178354Ssam		params.icp_flags |= IEEE80211_CLONE_BSSID;
4744178354Ssam	else
4745178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_BSSID;
4746178354Ssam}
4747178354Ssam
4748178354Ssamstatic void
4749178354Ssamset80211clone_wdslegacy(const char *val, int d, int s, const struct afswtch *rafp)
4750178354Ssam{
4751178354Ssam	if (d)
4752178354Ssam		params.icp_flags |= IEEE80211_CLONE_WDSLEGACY;
4753178354Ssam	else
4754178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_WDSLEGACY;
4755178354Ssam}
4756178354Ssam
4757138593Ssamstatic struct cmd ieee80211_cmds[] = {
4758138593Ssam	DEF_CMD_ARG("ssid",		set80211ssid),
4759138593Ssam	DEF_CMD_ARG("nwid",		set80211ssid),
4760138593Ssam	DEF_CMD_ARG("stationname",	set80211stationname),
4761138593Ssam	DEF_CMD_ARG("station",		set80211stationname),	/* BSD/OS */
4762138593Ssam	DEF_CMD_ARG("channel",		set80211channel),
4763138593Ssam	DEF_CMD_ARG("authmode",		set80211authmode),
4764138593Ssam	DEF_CMD_ARG("powersavemode",	set80211powersavemode),
4765138593Ssam	DEF_CMD("powersave",	1,	set80211powersave),
4766138593Ssam	DEF_CMD("-powersave",	0,	set80211powersave),
4767138593Ssam	DEF_CMD_ARG("powersavesleep", 	set80211powersavesleep),
4768138593Ssam	DEF_CMD_ARG("wepmode",		set80211wepmode),
4769138593Ssam	DEF_CMD("wep",		1,	set80211wep),
4770138593Ssam	DEF_CMD("-wep",		0,	set80211wep),
4771139493Ssam	DEF_CMD_ARG("deftxkey",		set80211weptxkey),
4772138593Ssam	DEF_CMD_ARG("weptxkey",		set80211weptxkey),
4773138593Ssam	DEF_CMD_ARG("wepkey",		set80211wepkey),
4774138593Ssam	DEF_CMD_ARG("nwkey",		set80211nwkey),		/* NetBSD */
4775138593Ssam	DEF_CMD("-nwkey",	0,	set80211wep),		/* NetBSD */
4776138593Ssam	DEF_CMD_ARG("rtsthreshold",	set80211rtsthreshold),
4777138593Ssam	DEF_CMD_ARG("protmode",		set80211protmode),
4778138593Ssam	DEF_CMD_ARG("txpower",		set80211txpower),
4779138593Ssam	DEF_CMD_ARG("roaming",		set80211roaming),
4780138593Ssam	DEF_CMD("wme",		1,	set80211wme),
4781138593Ssam	DEF_CMD("-wme",		0,	set80211wme),
4782178354Ssam	DEF_CMD("wmm",		1,	set80211wme),
4783178354Ssam	DEF_CMD("-wmm",		0,	set80211wme),
4784138593Ssam	DEF_CMD("hidessid",	1,	set80211hidessid),
4785138593Ssam	DEF_CMD("-hidessid",	0,	set80211hidessid),
4786138593Ssam	DEF_CMD("apbridge",	1,	set80211apbridge),
4787138593Ssam	DEF_CMD("-apbridge",	0,	set80211apbridge),
4788138593Ssam	DEF_CMD_ARG("chanlist",		set80211chanlist),
4789138593Ssam	DEF_CMD_ARG("bssid",		set80211bssid),
4790138593Ssam	DEF_CMD_ARG("ap",		set80211bssid),
4791138593Ssam	DEF_CMD("scan",	0,		set80211scan),
4792138593Ssam	DEF_CMD_ARG("list",		set80211list),
4793138593Ssam	DEF_CMD_ARG2("cwmin",		set80211cwmin),
4794138593Ssam	DEF_CMD_ARG2("cwmax",		set80211cwmax),
4795138593Ssam	DEF_CMD_ARG2("aifs",		set80211aifs),
4796138593Ssam	DEF_CMD_ARG2("txoplimit",	set80211txoplimit),
4797148621Ssam	DEF_CMD_ARG("acm",		set80211acm),
4798148621Ssam	DEF_CMD_ARG("-acm",		set80211noacm),
4799148621Ssam	DEF_CMD_ARG("ack",		set80211ackpolicy),
4800148621Ssam	DEF_CMD_ARG("-ack",		set80211noackpolicy),
4801138593Ssam	DEF_CMD_ARG2("bss:cwmin",	set80211bsscwmin),
4802138593Ssam	DEF_CMD_ARG2("bss:cwmax",	set80211bsscwmax),
4803138593Ssam	DEF_CMD_ARG2("bss:aifs",	set80211bssaifs),
4804138593Ssam	DEF_CMD_ARG2("bss:txoplimit",	set80211bsstxoplimit),
4805138593Ssam	DEF_CMD_ARG("dtimperiod",	set80211dtimperiod),
4806138593Ssam	DEF_CMD_ARG("bintval",		set80211bintval),
4807138593Ssam	DEF_CMD("mac:open",	IEEE80211_MACCMD_POLICY_OPEN,	set80211maccmd),
4808138593Ssam	DEF_CMD("mac:allow",	IEEE80211_MACCMD_POLICY_ALLOW,	set80211maccmd),
4809138593Ssam	DEF_CMD("mac:deny",	IEEE80211_MACCMD_POLICY_DENY,	set80211maccmd),
4810178354Ssam	DEF_CMD("mac:radius",	IEEE80211_MACCMD_POLICY_RADIUS,	set80211maccmd),
4811138593Ssam	DEF_CMD("mac:flush",	IEEE80211_MACCMD_FLUSH,		set80211maccmd),
4812138593Ssam	DEF_CMD("mac:detach",	IEEE80211_MACCMD_DETACH,	set80211maccmd),
4813138593Ssam	DEF_CMD_ARG("mac:add",		set80211addmac),
4814138593Ssam	DEF_CMD_ARG("mac:del",		set80211delmac),
4815138593Ssam	DEF_CMD_ARG("mac:kick",		set80211kickmac),
4816147795Ssam	DEF_CMD("pureg",	1,	set80211pureg),
4817147795Ssam	DEF_CMD("-pureg",	0,	set80211pureg),
4818170531Ssam	DEF_CMD("ff",		1,	set80211fastframes),
4819170531Ssam	DEF_CMD("-ff",		0,	set80211fastframes),
4820170531Ssam	DEF_CMD("dturbo",	1,	set80211dturbo),
4821170531Ssam	DEF_CMD("-dturbo",	0,	set80211dturbo),
4822170531Ssam	DEF_CMD("bgscan",	1,	set80211bgscan),
4823170531Ssam	DEF_CMD("-bgscan",	0,	set80211bgscan),
4824170531Ssam	DEF_CMD_ARG("bgscanidle",	set80211bgscanidle),
4825170531Ssam	DEF_CMD_ARG("bgscanintvl",	set80211bgscanintvl),
4826170531Ssam	DEF_CMD_ARG("scanvalid",	set80211scanvalid),
4827178354Ssam	DEF_CMD_ARG("roam:rssi",	set80211roamrssi),
4828178354Ssam	DEF_CMD_ARG("roam:rate",	set80211roamrate),
4829153354Ssam	DEF_CMD_ARG("mcastrate",	set80211mcastrate),
4830178354Ssam	DEF_CMD_ARG("ucastrate",	set80211ucastrate),
4831178354Ssam	DEF_CMD_ARG("mgtrate",		set80211mgtrate),
4832178354Ssam	DEF_CMD_ARG("mgmtrate",		set80211mgtrate),
4833178354Ssam	DEF_CMD_ARG("maxretry",		set80211maxretry),
4834148416Ssam	DEF_CMD_ARG("fragthreshold",	set80211fragthreshold),
4835153422Ssam	DEF_CMD("burst",	1,	set80211burst),
4836153422Ssam	DEF_CMD("-burst",	0,	set80211burst),
4837160687Ssam	DEF_CMD_ARG("bmiss",		set80211bmissthreshold),
4838160687Ssam	DEF_CMD_ARG("bmissthreshold",	set80211bmissthreshold),
4839173275Ssam	DEF_CMD("shortgi",	1,	set80211shortgi),
4840173275Ssam	DEF_CMD("-shortgi",	0,	set80211shortgi),
4841173275Ssam	DEF_CMD("ampdurx",	2,	set80211ampdu),
4842173275Ssam	DEF_CMD("-ampdurx",	-2,	set80211ampdu),
4843173275Ssam	DEF_CMD("ampdutx",	1,	set80211ampdu),
4844173275Ssam	DEF_CMD("-ampdutx",	-1,	set80211ampdu),
4845173275Ssam	DEF_CMD("ampdu",	3,	set80211ampdu),		/* NB: tx+rx */
4846173275Ssam	DEF_CMD("-ampdu",	-3,	set80211ampdu),
4847173275Ssam	DEF_CMD_ARG("ampdulimit",	set80211ampdulimit),
4848173275Ssam	DEF_CMD_ARG("ampdudensity",	set80211ampdudensity),
4849173275Ssam	DEF_CMD("amsdurx",	2,	set80211amsdu),
4850173275Ssam	DEF_CMD("-amsdurx",	-2,	set80211amsdu),
4851173275Ssam	DEF_CMD("amsdutx",	1,	set80211amsdu),
4852173275Ssam	DEF_CMD("-amsdutx",	-1,	set80211amsdu),
4853173275Ssam	DEF_CMD("amsdu",	3,	set80211amsdu),		/* NB: tx+rx */
4854173275Ssam	DEF_CMD("-amsdu",	-3,	set80211amsdu),
4855173275Ssam	DEF_CMD_ARG("amsdulimit",	set80211amsdulimit),
4856173275Ssam	DEF_CMD("puren",	1,	set80211puren),
4857173275Ssam	DEF_CMD("-puren",	0,	set80211puren),
4858170531Ssam	DEF_CMD("doth",		1,	set80211doth),
4859170531Ssam	DEF_CMD("-doth",	0,	set80211doth),
4860178354Ssam	DEF_CMD("dfs",		1,	set80211dfs),
4861178354Ssam	DEF_CMD("-dfs",		0,	set80211dfs),
4862173275Ssam	DEF_CMD("htcompat",	1,	set80211htcompat),
4863173275Ssam	DEF_CMD("-htcompat",	0,	set80211htcompat),
4864178354Ssam	DEF_CMD("dwds",		1,	set80211dwds),
4865178354Ssam	DEF_CMD("-dwds",	0,	set80211dwds),
4866173275Ssam	DEF_CMD("inact",	1,	set80211inact),
4867173275Ssam	DEF_CMD("-inact",	0,	set80211inact),
4868178354Ssam	DEF_CMD("tsn",		1,	set80211tsn),
4869178354Ssam	DEF_CMD("-tsn",		0,	set80211tsn),
4870178354Ssam	DEF_CMD_ARG("regdomain",	set80211regdomain),
4871178354Ssam	DEF_CMD_ARG("country",		set80211country),
4872178354Ssam	DEF_CMD("indoor",	'I',	set80211location),
4873178354Ssam	DEF_CMD("-indoor",	'O',	set80211location),
4874178354Ssam	DEF_CMD("outdoor",	'O',	set80211location),
4875178354Ssam	DEF_CMD("-outdoor",	'I',	set80211location),
4876178354Ssam	DEF_CMD("anywhere",	' ',	set80211location),
4877178354Ssam	DEF_CMD("ecm",		1,	set80211ecm),
4878178354Ssam	DEF_CMD("-ecm",		0,	set80211ecm),
4879178354Ssam	DEF_CMD("dotd",		1,	set80211dotd),
4880178354Ssam	DEF_CMD("-dotd",	0,	set80211dotd),
4881173275Ssam	DEF_CMD_ARG("htprotmode",	set80211htprotmode),
4882173275Ssam	DEF_CMD("ht20",		1,	set80211htconf),
4883173275Ssam	DEF_CMD("-ht20",	0,	set80211htconf),
4884173275Ssam	DEF_CMD("ht40",		3,	set80211htconf),	/* NB: 20+40 */
4885173275Ssam	DEF_CMD("-ht40",	0,	set80211htconf),
4886173275Ssam	DEF_CMD("ht",		3,	set80211htconf),	/* NB: 20+40 */
4887173275Ssam	DEF_CMD("-ht",		0,	set80211htconf),
4888183261Ssam	DEF_CMD("rifs",		1,	set80211rifs),
4889183261Ssam	DEF_CMD("-rifs",	0,	set80211rifs),
4890183261Ssam	DEF_CMD("smps",		IEEE80211_HTCAP_SMPS_ENA,	set80211smps),
4891183261Ssam	DEF_CMD("smpsdyn",	IEEE80211_HTCAP_SMPS_DYNAMIC,	set80211smps),
4892183261Ssam	DEF_CMD("-smps",	IEEE80211_HTCAP_SMPS_OFF,	set80211smps),
4893178354Ssam	/* XXX for testing */
4894178354Ssam	DEF_CMD_ARG("chanswitch",	set80211chanswitch),
4895178354Ssam
4896186904Ssam	DEF_CMD_ARG("tdmaslot",		set80211tdmaslot),
4897186904Ssam	DEF_CMD_ARG("tdmaslotcnt",	set80211tdmaslotcnt),
4898186904Ssam	DEF_CMD_ARG("tdmaslotlen",	set80211tdmaslotlen),
4899186904Ssam	DEF_CMD_ARG("tdmabintval",	set80211tdmabintval),
4900186904Ssam
4901178354Ssam	/* vap cloning support */
4902178354Ssam	DEF_CLONE_CMD_ARG("wlanaddr",	set80211clone_wlanaddr),
4903178354Ssam	DEF_CLONE_CMD_ARG("wlanbssid",	set80211clone_wlanbssid),
4904178354Ssam	DEF_CLONE_CMD_ARG("wlandev",	set80211clone_wlandev),
4905178354Ssam	DEF_CLONE_CMD_ARG("wlanmode",	set80211clone_wlanmode),
4906178354Ssam	DEF_CLONE_CMD("beacons", 1,	set80211clone_beacons),
4907178354Ssam	DEF_CLONE_CMD("-beacons", 0,	set80211clone_beacons),
4908178354Ssam	DEF_CLONE_CMD("bssid",	1,	set80211clone_bssid),
4909178354Ssam	DEF_CLONE_CMD("-bssid",	0,	set80211clone_bssid),
4910178354Ssam	DEF_CLONE_CMD("wdslegacy", 1,	set80211clone_wdslegacy),
4911178354Ssam	DEF_CLONE_CMD("-wdslegacy", 0,	set80211clone_wdslegacy),
4912138593Ssam};
4913138593Ssamstatic struct afswtch af_ieee80211 = {
4914138593Ssam	.af_name	= "af_ieee80211",
4915138593Ssam	.af_af		= AF_UNSPEC,
4916139494Ssam	.af_other_status = ieee80211_status,
4917138593Ssam};
4918138593Ssam
4919138593Ssamstatic __constructor void
4920138593Ssamieee80211_ctor(void)
4921138593Ssam{
4922138593Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
4923138593Ssam	int i;
4924138593Ssam
4925138593Ssam	for (i = 0; i < N(ieee80211_cmds);  i++)
4926138593Ssam		cmd_register(&ieee80211_cmds[i]);
4927138593Ssam	af_register(&af_ieee80211);
4928189096Srpaulo	clone_setdefcallback("wlan", wlan_create);
4929138593Ssam#undef N
4930138593Ssam}
4931