ifieee80211.c revision 202161
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 202161 2010-01-12 17:59:58Z gavin $
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>
84195618Srpaulo#include <net80211/ieee80211_mesh.h>
8577218Sphk
86187801Ssam#include <assert.h>
8777218Sphk#include <ctype.h>
8877218Sphk#include <err.h>
8977218Sphk#include <errno.h>
9077218Sphk#include <fcntl.h>
91146873Sjhb#include <inttypes.h>
9277218Sphk#include <stdio.h>
9377218Sphk#include <stdlib.h>
9477218Sphk#include <string.h>
9577218Sphk#include <unistd.h>
96155931Ssam#include <stdarg.h>
97173275Ssam#include <stddef.h>		/* NB: for offsetof */
9877218Sphk
9977218Sphk#include "ifconfig.h"
100178354Ssam#include "regdomain.h"
10177218Sphk
102178354Ssam#ifndef IEEE80211_FIXED_RATE_NONE
103178354Ssam#define	IEEE80211_FIXED_RATE_NONE	0xff
104178354Ssam#endif
105178354Ssam
106178354Ssam/* XXX need these publicly defined or similar */
107178354Ssam#ifndef IEEE80211_NODE_AUTH
108194136Ssam#define	IEEE80211_NODE_AUTH	0x000001	/* authorized for data */
109194136Ssam#define	IEEE80211_NODE_QOS	0x000002	/* QoS enabled */
110194136Ssam#define	IEEE80211_NODE_ERP	0x000004	/* ERP enabled */
111194136Ssam#define	IEEE80211_NODE_PWR_MGT	0x000010	/* power save mode enabled */
112194136Ssam#define	IEEE80211_NODE_AREF	0x000020	/* authentication ref held */
113194136Ssam#define	IEEE80211_NODE_HT	0x000040	/* HT enabled */
114194136Ssam#define	IEEE80211_NODE_HTCOMPAT	0x000080	/* HT setup w/ vendor OUI's */
115194136Ssam#define	IEEE80211_NODE_WPS	0x000100	/* WPS association */
116194136Ssam#define	IEEE80211_NODE_TSN	0x000200	/* TSN association */
117194136Ssam#define	IEEE80211_NODE_AMPDU_RX	0x000400	/* AMPDU rx enabled */
118194136Ssam#define	IEEE80211_NODE_AMPDU_TX	0x000800	/* AMPDU tx enabled */
119194136Ssam#define	IEEE80211_NODE_MIMO_PS	0x001000	/* MIMO power save enabled */
120194136Ssam#define	IEEE80211_NODE_MIMO_RTS	0x002000	/* send RTS in MIMO PS */
121194136Ssam#define	IEEE80211_NODE_RIFS	0x004000	/* RIFS enabled */
122194136Ssam#define	IEEE80211_NODE_SGI20	0x008000	/* Short GI in HT20 enabled */
123194136Ssam#define	IEEE80211_NODE_SGI40	0x010000	/* Short GI in HT40 enabled */
124194136Ssam#define	IEEE80211_NODE_ASSOCID	0x020000	/* xmit requires associd */
125194136Ssam#define	IEEE80211_NODE_AMSDU_RX	0x040000	/* AMSDU rx enabled */
126194136Ssam#define	IEEE80211_NODE_AMSDU_TX	0x080000	/* AMSDU tx enabled */
127178354Ssam#endif
128178354Ssam
129187801Ssam#define	MAXCHAN	1536		/* max 1.5K channels */
130187801Ssam
131173275Ssam#define	MAXCOL	78
132173275Ssamstatic	int col;
133173275Ssamstatic	char spacer;
134173275Ssam
135173275Ssamstatic void LINE_INIT(char c);
136173275Ssamstatic void LINE_BREAK(void);
137173275Ssamstatic void LINE_CHECK(const char *fmt, ...);
138173275Ssam
139188784Ssamstatic const char *modename[IEEE80211_MODE_MAX] = {
140188784Ssam	[IEEE80211_MODE_AUTO]	  = "auto",
141188784Ssam	[IEEE80211_MODE_11A]	  = "11a",
142188784Ssam	[IEEE80211_MODE_11B]	  = "11b",
143188784Ssam	[IEEE80211_MODE_11G]	  = "11g",
144188784Ssam	[IEEE80211_MODE_FH]	  = "fh",
145188784Ssam	[IEEE80211_MODE_TURBO_A]  = "turboA",
146188784Ssam	[IEEE80211_MODE_TURBO_G]  = "turboG",
147188784Ssam	[IEEE80211_MODE_STURBO_A] = "sturbo",
148188784Ssam	[IEEE80211_MODE_11NA]	  = "11na",
149188784Ssam	[IEEE80211_MODE_11NG]	  = "11ng",
150188784Ssam	[IEEE80211_MODE_HALF]	  = "half",
151188784Ssam	[IEEE80211_MODE_QUARTER]  = "quarter"
152173275Ssam};
153173275Ssam
154178354Ssamstatic void set80211(int s, int type, int val, int len, void *data);
155173275Ssamstatic int get80211(int s, int type, void *data, int len);
156173275Ssamstatic int get80211len(int s, int type, void *data, int len, int *plen);
157173275Ssamstatic int get80211val(int s, int type, int *val);
15877218Sphkstatic const char *get_string(const char *val, const char *sep,
15977218Sphk    u_int8_t *buf, int *lenp);
16077218Sphkstatic void print_string(const u_int8_t *buf, int len);
161178354Ssamstatic void print_regdomain(const struct ieee80211_regdomain *, int);
162178354Ssamstatic void print_channels(int, const struct ieee80211req_chaninfo *,
163178354Ssam    int allchans, int verbose);
164178354Ssamstatic void regdomain_makechannels(struct ieee80211_regdomain_req *,
165178354Ssam    const struct ieee80211_devcaps_req *);
166195618Srpaulostatic const char *mesh_linkstate_string(uint8_t state);
16777218Sphk
168187801Ssamstatic struct ieee80211req_chaninfo *chaninfo;
169178354Ssamstatic struct ieee80211_regdomain regdomain;
170178354Ssamstatic int gotregdomain = 0;
171178354Ssamstatic struct ieee80211_roamparams_req roamparams;
172178354Ssamstatic int gotroam = 0;
173178354Ssamstatic struct ieee80211_txparams_req txparams;
174178354Ssamstatic int gottxparams = 0;
175173275Ssamstatic struct ieee80211_channel curchan;
176173275Ssamstatic int gotcurchan = 0;
177178354Ssamstatic struct ifmediareq *ifmr;
178173275Ssamstatic int htconf = 0;
179173275Ssamstatic	int gothtconf = 0;
180170531Ssam
181173275Ssamstatic void
182173275Ssamgethtconf(int s)
183173275Ssam{
184173275Ssam	if (gothtconf)
185173275Ssam		return;
186173275Ssam	if (get80211val(s, IEEE80211_IOC_HTCONF, &htconf) < 0)
187173275Ssam		warn("unable to get HT configuration information");
188173275Ssam	gothtconf = 1;
189173275Ssam}
190173275Ssam
191170531Ssam/*
192170531Ssam * Collect channel info from the kernel.  We use this (mostly)
193170531Ssam * to handle mapping between frequency and IEEE channel number.
194170531Ssam */
195170531Ssamstatic void
196170531Ssamgetchaninfo(int s)
197170531Ssam{
198187801Ssam	if (chaninfo != NULL)
199170531Ssam		return;
200187801Ssam	chaninfo = malloc(IEEE80211_CHANINFO_SIZE(MAXCHAN));
201187801Ssam	if (chaninfo == NULL)
202187801Ssam		errx(1, "no space for channel list");
203187801Ssam	if (get80211(s, IEEE80211_IOC_CHANINFO, chaninfo,
204187801Ssam	    IEEE80211_CHANINFO_SIZE(MAXCHAN)) < 0)
205187801Ssam		err(1, "unable to get channel information");
206170531Ssam	ifmr = ifmedia_getstate(s);
207173275Ssam	gethtconf(s);
208170531Ssam}
209170531Ssam
210178354Ssamstatic struct regdata *
211178354Ssamgetregdata(void)
212178354Ssam{
213178354Ssam	static struct regdata *rdp = NULL;
214178354Ssam	if (rdp == NULL) {
215178354Ssam		rdp = lib80211_alloc_regdata();
216178354Ssam		if (rdp == NULL)
217181198Ssam			errx(-1, "missing or corrupted regdomain database");
218178354Ssam	}
219178354Ssam	return rdp;
220178354Ssam}
221178354Ssam
222170531Ssam/*
223170531Ssam * Given the channel at index i with attributes from,
224170531Ssam * check if there is a channel with attributes to in
225170531Ssam * the channel table.  With suitable attributes this
226170531Ssam * allows the caller to look for promotion; e.g. from
227170531Ssam * 11b > 11g.
228170531Ssam */
229138593Ssamstatic int
230170531Ssamcanpromote(int i, int from, int to)
231170531Ssam{
232187801Ssam	const struct ieee80211_channel *fc = &chaninfo->ic_chans[i];
233170531Ssam	int j;
234170531Ssam
235170531Ssam	if ((fc->ic_flags & from) != from)
236170531Ssam		return i;
237170531Ssam	/* NB: quick check exploiting ordering of chans w/ same frequency */
238187801Ssam	if (i+1 < chaninfo->ic_nchans &&
239187801Ssam	    chaninfo->ic_chans[i+1].ic_freq == fc->ic_freq &&
240187801Ssam	    (chaninfo->ic_chans[i+1].ic_flags & to) == to)
241170531Ssam		return i+1;
242170531Ssam	/* brute force search in case channel list is not ordered */
243187801Ssam	for (j = 0; j < chaninfo->ic_nchans; j++) {
244187801Ssam		const struct ieee80211_channel *tc = &chaninfo->ic_chans[j];
245170531Ssam		if (j != i &&
246170531Ssam		    tc->ic_freq == fc->ic_freq && (tc->ic_flags & to) == to)
247170531Ssam		return j;
248170531Ssam	}
249170531Ssam	return i;
250170531Ssam}
251170531Ssam
252170531Ssam/*
253170531Ssam * Handle channel promotion.  When a channel is specified with
254170531Ssam * only a frequency we want to promote it to the ``best'' channel
255170531Ssam * available.  The channel list has separate entries for 11b, 11g,
256170531Ssam * 11a, and 11n[ga] channels so specifying a frequency w/o any
257170531Ssam * attributes requires we upgrade, e.g. from 11b -> 11g.  This
258170531Ssam * gets complicated when the channel is specified on the same
259170531Ssam * command line with a media request that constrains the available
260170531Ssam * channe list (e.g. mode 11a); we want to honor that to avoid
261170531Ssam * confusing behaviour.
262170531Ssam */
263170531Ssamstatic int
264170531Ssampromote(int i)
265170531Ssam{
266170531Ssam	/*
267170531Ssam	 * Query the current mode of the interface in case it's
268170531Ssam	 * constrained (e.g. to 11a).  We must do this carefully
269170531Ssam	 * as there may be a pending ifmedia request in which case
270170531Ssam	 * asking the kernel will give us the wrong answer.  This
271170531Ssam	 * is an unfortunate side-effect of the way ifconfig is
272170531Ssam	 * structure for modularity (yech).
273170531Ssam	 *
274170531Ssam	 * NB: ifmr is actually setup in getchaninfo (above); we
275170531Ssam	 *     assume it's called coincident with to this call so
276170531Ssam	 *     we have a ``current setting''; otherwise we must pass
277170531Ssam	 *     the socket descriptor down to here so we can make
278170531Ssam	 *     the ifmedia_getstate call ourselves.
279170531Ssam	 */
280170531Ssam	int chanmode = ifmr != NULL ? IFM_MODE(ifmr->ifm_current) : IFM_AUTO;
281170531Ssam
282170531Ssam	/* when ambiguous promote to ``best'' */
283170531Ssam	/* NB: we abitrarily pick HT40+ over HT40- */
284170531Ssam	if (chanmode != IFM_IEEE80211_11B)
285170531Ssam		i = canpromote(i, IEEE80211_CHAN_B, IEEE80211_CHAN_G);
286173275Ssam	if (chanmode != IFM_IEEE80211_11G && (htconf & 1)) {
287170531Ssam		i = canpromote(i, IEEE80211_CHAN_G,
288170531Ssam			IEEE80211_CHAN_G | IEEE80211_CHAN_HT20);
289173275Ssam		if (htconf & 2) {
290173275Ssam			i = canpromote(i, IEEE80211_CHAN_G,
291173275Ssam				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D);
292173275Ssam			i = canpromote(i, IEEE80211_CHAN_G,
293173275Ssam				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U);
294173275Ssam		}
295170531Ssam	}
296173275Ssam	if (chanmode != IFM_IEEE80211_11A && (htconf & 1)) {
297170531Ssam		i = canpromote(i, IEEE80211_CHAN_A,
298170531Ssam			IEEE80211_CHAN_A | IEEE80211_CHAN_HT20);
299173275Ssam		if (htconf & 2) {
300173275Ssam			i = canpromote(i, IEEE80211_CHAN_A,
301173275Ssam				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D);
302173275Ssam			i = canpromote(i, IEEE80211_CHAN_A,
303173275Ssam				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U);
304173275Ssam		}
305170531Ssam	}
306170531Ssam	return i;
307170531Ssam}
308170531Ssam
309170531Ssamstatic void
310170531Ssammapfreq(struct ieee80211_channel *chan, int freq, int flags)
311170531Ssam{
312170531Ssam	int i;
313170531Ssam
314187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
315187801Ssam		const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
316170531Ssam
317170531Ssam		if (c->ic_freq == freq && (c->ic_flags & flags) == flags) {
318170531Ssam			if (flags == 0) {
319170531Ssam				/* when ambiguous promote to ``best'' */
320187801Ssam				c = &chaninfo->ic_chans[promote(i)];
321170531Ssam			}
322170531Ssam			*chan = *c;
323170531Ssam			return;
324170531Ssam		}
325170531Ssam	}
326170531Ssam	errx(1, "unknown/undefined frequency %u/0x%x", freq, flags);
327170531Ssam}
328170531Ssam
329170531Ssamstatic void
330170531Ssammapchan(struct ieee80211_channel *chan, int ieee, int flags)
331170531Ssam{
332170531Ssam	int i;
333170531Ssam
334187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
335187801Ssam		const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
336170531Ssam
337170531Ssam		if (c->ic_ieee == ieee && (c->ic_flags & flags) == flags) {
338170531Ssam			if (flags == 0) {
339170531Ssam				/* when ambiguous promote to ``best'' */
340187801Ssam				c = &chaninfo->ic_chans[promote(i)];
341170531Ssam			}
342170531Ssam			*chan = *c;
343170531Ssam			return;
344170531Ssam		}
345170531Ssam	}
346173275Ssam	errx(1, "unknown/undefined channel number %d flags 0x%x", ieee, flags);
347170531Ssam}
348170531Ssam
349173275Ssamstatic const struct ieee80211_channel *
350173275Ssamgetcurchan(int s)
351173275Ssam{
352173275Ssam	if (gotcurchan)
353173275Ssam		return &curchan;
354173275Ssam	if (get80211(s, IEEE80211_IOC_CURCHAN, &curchan, sizeof(curchan)) < 0) {
355173275Ssam		int val;
356173275Ssam		/* fall back to legacy ioctl */
357173275Ssam		if (get80211val(s, IEEE80211_IOC_CHANNEL, &val) < 0)
358187801Ssam			err(-1, "cannot figure out current channel");
359173275Ssam		getchaninfo(s);
360173275Ssam		mapchan(&curchan, val, 0);
361173275Ssam	}
362173275Ssam	gotcurchan = 1;
363173275Ssam	return &curchan;
364173275Ssam}
365173275Ssam
366178354Ssamstatic enum ieee80211_phymode
367178354Ssamchan2mode(const struct ieee80211_channel *c)
368178354Ssam{
369178354Ssam	if (IEEE80211_IS_CHAN_HTA(c))
370178354Ssam		return IEEE80211_MODE_11NA;
371178354Ssam	if (IEEE80211_IS_CHAN_HTG(c))
372178354Ssam		return IEEE80211_MODE_11NG;
373178354Ssam	if (IEEE80211_IS_CHAN_108A(c))
374178354Ssam		return IEEE80211_MODE_TURBO_A;
375178354Ssam	if (IEEE80211_IS_CHAN_108G(c))
376178354Ssam		return IEEE80211_MODE_TURBO_G;
377178354Ssam	if (IEEE80211_IS_CHAN_ST(c))
378178354Ssam		return IEEE80211_MODE_STURBO_A;
379178354Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
380178354Ssam		return IEEE80211_MODE_FH;
381188784Ssam	if (IEEE80211_IS_CHAN_HALF(c))
382188784Ssam		return IEEE80211_MODE_HALF;
383188784Ssam	if (IEEE80211_IS_CHAN_QUARTER(c))
384188784Ssam		return IEEE80211_MODE_QUARTER;
385178354Ssam	if (IEEE80211_IS_CHAN_A(c))
386178354Ssam		return IEEE80211_MODE_11A;
387178354Ssam	if (IEEE80211_IS_CHAN_ANYG(c))
388178354Ssam		return IEEE80211_MODE_11G;
389178354Ssam	if (IEEE80211_IS_CHAN_B(c))
390178354Ssam		return IEEE80211_MODE_11B;
391178354Ssam	return IEEE80211_MODE_AUTO;
392178354Ssam}
393178354Ssam
394178354Ssamstatic void
395178354Ssamgetroam(int s)
396178354Ssam{
397178354Ssam	if (gotroam)
398178354Ssam		return;
399178354Ssam	if (get80211(s, IEEE80211_IOC_ROAM,
400178354Ssam	    &roamparams, sizeof(roamparams)) < 0)
401187801Ssam		err(1, "unable to get roaming parameters");
402178354Ssam	gotroam = 1;
403178354Ssam}
404178354Ssam
405178354Ssamstatic void
406178354Ssamsetroam_cb(int s, void *arg)
407178354Ssam{
408178354Ssam	struct ieee80211_roamparams_req *roam = arg;
409178354Ssam	set80211(s, IEEE80211_IOC_ROAM, 0, sizeof(*roam), roam);
410178354Ssam}
411178354Ssam
412178354Ssamstatic void
413178354Ssamgettxparams(int s)
414178354Ssam{
415178354Ssam	if (gottxparams)
416178354Ssam		return;
417178354Ssam	if (get80211(s, IEEE80211_IOC_TXPARAMS,
418178354Ssam	    &txparams, sizeof(txparams)) < 0)
419187801Ssam		err(1, "unable to get transmit parameters");
420178354Ssam	gottxparams = 1;
421178354Ssam}
422178354Ssam
423178354Ssamstatic void
424178354Ssamsettxparams_cb(int s, void *arg)
425178354Ssam{
426178354Ssam	struct ieee80211_txparams_req *txp = arg;
427178354Ssam	set80211(s, IEEE80211_IOC_TXPARAMS, 0, sizeof(*txp), txp);
428178354Ssam}
429178354Ssam
430178354Ssamstatic void
431178354Ssamgetregdomain(int s)
432178354Ssam{
433178354Ssam	if (gotregdomain)
434178354Ssam		return;
435178354Ssam	if (get80211(s, IEEE80211_IOC_REGDOMAIN,
436178354Ssam	    &regdomain, sizeof(regdomain)) < 0)
437187801Ssam		err(1, "unable to get regulatory domain info");
438178354Ssam	gotregdomain = 1;
439178354Ssam}
440178354Ssam
441178354Ssamstatic void
442178354Ssamgetdevcaps(int s, struct ieee80211_devcaps_req *dc)
443178354Ssam{
444187801Ssam	if (get80211(s, IEEE80211_IOC_DEVCAPS, dc,
445187801Ssam	    IEEE80211_DEVCAPS_SPACE(dc)) < 0)
446187801Ssam		err(1, "unable to get device capabilities");
447178354Ssam}
448178354Ssam
449178354Ssamstatic void
450178354Ssamsetregdomain_cb(int s, void *arg)
451178354Ssam{
452187801Ssam	struct ieee80211_regdomain_req *req;
453178354Ssam	struct ieee80211_regdomain *rd = arg;
454187801Ssam	struct ieee80211_devcaps_req *dc;
455178354Ssam	struct regdata *rdp = getregdata();
456178354Ssam
457186102Ssam	if (rd->country != NO_COUNTRY) {
458178354Ssam		const struct country *cc;
459178354Ssam		/*
460178354Ssam		 * Check current country seting to make sure it's
461178354Ssam		 * compatible with the new regdomain.  If not, then
462178354Ssam		 * override it with any default country for this
463178354Ssam		 * SKU.  If we cannot arrange a match, then abort.
464178354Ssam		 */
465178354Ssam		cc = lib80211_country_findbycc(rdp, rd->country);
466178354Ssam		if (cc == NULL)
467178354Ssam			errx(1, "unknown ISO country code %d", rd->country);
468178354Ssam		if (cc->rd->sku != rd->regdomain) {
469178354Ssam			const struct regdomain *rp;
470178354Ssam			/*
471178354Ssam			 * Check if country is incompatible with regdomain.
472178354Ssam			 * To enable multiple regdomains for a country code
473178354Ssam			 * we permit a mismatch between the regdomain and
474178354Ssam			 * the country's associated regdomain when the
475178354Ssam			 * regdomain is setup w/o a default country.  For
476178354Ssam			 * example, US is bound to the FCC regdomain but
477178354Ssam			 * we allow US to be combined with FCC3 because FCC3
478178354Ssam			 * has not default country.  This allows bogus
479178354Ssam			 * combinations like FCC3+DK which are resolved when
480178354Ssam			 * constructing the channel list by deferring to the
481178354Ssam			 * regdomain to construct the channel list.
482178354Ssam			 */
483178354Ssam			rp = lib80211_regdomain_findbysku(rdp, rd->regdomain);
484178354Ssam			if (rp == NULL)
485178354Ssam				errx(1, "country %s (%s) is not usable with "
486178354Ssam				    "regdomain %d", cc->isoname, cc->name,
487178354Ssam				    rd->regdomain);
488186102Ssam			else if (rp->cc != NULL && rp->cc != cc)
489178354Ssam				errx(1, "country %s (%s) is not usable with "
490178354Ssam				   "regdomain %s", cc->isoname, cc->name,
491178354Ssam				   rp->name);
492178354Ssam		}
493178354Ssam	}
494178354Ssam	/*
495178354Ssam	 * Fetch the device capabilities and calculate the
496178354Ssam	 * full set of netbands for which we request a new
497178354Ssam	 * channel list be constructed.  Once that's done we
498178354Ssam	 * push the regdomain info + channel list to the kernel.
499178354Ssam	 */
500187801Ssam	dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
501187801Ssam	if (dc == NULL)
502187801Ssam		errx(1, "no space for device capabilities");
503187801Ssam	dc->dc_chaninfo.ic_nchans = MAXCHAN;
504187801Ssam	getdevcaps(s, dc);
505178354Ssam#if 0
506178354Ssam	if (verbose) {
507187801Ssam		printf("drivercaps: 0x%x\n", dc->dc_drivercaps);
508187801Ssam		printf("cryptocaps: 0x%x\n", dc->dc_cryptocaps);
509187801Ssam		printf("htcaps    : 0x%x\n", dc->dc_htcaps);
510187801Ssam		memcpy(chaninfo, &dc->dc_chaninfo,
511187801Ssam		    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
512187801Ssam		print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, 1/*verbose*/);
513178354Ssam	}
514178354Ssam#endif
515187801Ssam	req = malloc(IEEE80211_REGDOMAIN_SIZE(dc->dc_chaninfo.ic_nchans));
516187801Ssam	if (req == NULL)
517187801Ssam		errx(1, "no space for regdomain request");
518187801Ssam	req->rd = *rd;
519187801Ssam	regdomain_makechannels(req, dc);
520178354Ssam	if (verbose) {
521178354Ssam		LINE_INIT(':');
522178354Ssam		print_regdomain(rd, 1/*verbose*/);
523178354Ssam		LINE_BREAK();
524187801Ssam		/* blech, reallocate channel list for new data */
525187801Ssam		if (chaninfo != NULL)
526187801Ssam			free(chaninfo);
527187801Ssam		chaninfo = malloc(IEEE80211_CHANINFO_SPACE(&req->chaninfo));
528187801Ssam		if (chaninfo == NULL)
529187801Ssam			errx(1, "no space for channel list");
530187801Ssam		memcpy(chaninfo, &req->chaninfo,
531187801Ssam		    IEEE80211_CHANINFO_SPACE(&req->chaninfo));
532187801Ssam		print_channels(s, &req->chaninfo, 1/*allchans*/, 1/*verbose*/);
533178354Ssam	}
534187801Ssam	if (req->chaninfo.ic_nchans == 0)
535178354Ssam		errx(1, "no channels calculated");
536187801Ssam	set80211(s, IEEE80211_IOC_REGDOMAIN, 0,
537187801Ssam	    IEEE80211_REGDOMAIN_SPACE(req), req);
538187801Ssam	free(req);
539187801Ssam	free(dc);
540178354Ssam}
541178354Ssam
542170531Ssamstatic int
543170531Ssamieee80211_mhz2ieee(int freq, int flags)
544170531Ssam{
545170531Ssam	struct ieee80211_channel chan;
546170531Ssam	mapfreq(&chan, freq, flags);
547170531Ssam	return chan.ic_ieee;
548170531Ssam}
549170531Ssam
550170531Ssamstatic int
551138593Ssamisanyarg(const char *arg)
552138593Ssam{
553173275Ssam	return (strncmp(arg, "-", 1) == 0 ||
554173275Ssam	    strncasecmp(arg, "any", 3) == 0 || strncasecmp(arg, "off", 3) == 0);
555138593Ssam}
556138593Ssam
557138593Ssamstatic void
55877218Sphkset80211ssid(const char *val, int d, int s, const struct afswtch *rafp)
55977218Sphk{
56077218Sphk	int		ssid;
56177218Sphk	int		len;
562151883Sbrooks	u_int8_t	data[IEEE80211_NWID_LEN];
56377218Sphk
56477218Sphk	ssid = 0;
565121827Sbrooks	len = strlen(val);
566178354Ssam	if (len > 2 && isdigit((int)val[0]) && val[1] == ':') {
56788748Sambrisko		ssid = atoi(val)-1;
56888748Sambrisko		val += 2;
56988748Sambrisko	}
57077218Sphk
57177218Sphk	bzero(data, sizeof(data));
57277218Sphk	len = sizeof(data);
573151883Sbrooks	if (get_string(val, NULL, data, &len) == NULL)
574151883Sbrooks		exit(1);
57577218Sphk
57677218Sphk	set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
57777218Sphk}
57877218Sphk
579138593Ssamstatic void
580195618Srpauloset80211meshid(const char *val, int d, int s, const struct afswtch *rafp)
581195618Srpaulo{
582195618Srpaulo	int		len;
583195618Srpaulo	u_int8_t	data[IEEE80211_NWID_LEN];
584195618Srpaulo
585195618Srpaulo	memset(data, 0, sizeof(data));
586195618Srpaulo	len = sizeof(data);
587195618Srpaulo	if (get_string(val, NULL, data, &len) == NULL)
588195618Srpaulo		exit(1);
589195618Srpaulo
590195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_ID, 0, len, data);
591195618Srpaulo}
592195618Srpaulo
593195618Srpaulostatic void
59477218Sphkset80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
59577218Sphk{
59677218Sphk	int			len;
59777218Sphk	u_int8_t		data[33];
59877218Sphk
59977218Sphk	bzero(data, sizeof(data));
60077218Sphk	len = sizeof(data);
60177218Sphk	get_string(val, NULL, data, &len);
60277218Sphk
60377218Sphk	set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
60477218Sphk}
60577218Sphk
606138593Ssam/*
607170531Ssam * Parse a channel specification for attributes/flags.
608170531Ssam * The syntax is:
609170531Ssam *	freq/xx		channel width (5,10,20,40,40+,40-)
610170531Ssam *	freq:mode	channel mode (a,b,g,h,n,t,s,d)
611170531Ssam *
612170531Ssam * These can be combined in either order; e.g. 2437:ng/40.
613170531Ssam * Modes are case insensitive.
614170531Ssam *
615170531Ssam * The result is not validated here; it's assumed to be
616170531Ssam * checked against the channel table fetched from the kernel.
617170531Ssam */
618170531Ssamstatic int
619173275Ssamgetchannelflags(const char *val, int freq)
620138593Ssam{
621170531Ssam#define	_CHAN_HT	0x80000000
622170531Ssam	const char *cp;
623170531Ssam	int flags;
624138593Ssam
625170531Ssam	flags = 0;
626166015Ssam
627170531Ssam	cp = strchr(val, ':');
628170531Ssam	if (cp != NULL) {
629170531Ssam		for (cp++; isalpha((int) *cp); cp++) {
630170531Ssam			/* accept mixed case */
631170531Ssam			int c = *cp;
632170531Ssam			if (isupper(c))
633170531Ssam				c = tolower(c);
634170531Ssam			switch (c) {
635170531Ssam			case 'a':		/* 802.11a */
636170531Ssam				flags |= IEEE80211_CHAN_A;
637170531Ssam				break;
638170531Ssam			case 'b':		/* 802.11b */
639170531Ssam				flags |= IEEE80211_CHAN_B;
640170531Ssam				break;
641170531Ssam			case 'g':		/* 802.11g */
642170531Ssam				flags |= IEEE80211_CHAN_G;
643170531Ssam				break;
644170531Ssam			case 'h':		/* ht = 802.11n */
645170531Ssam			case 'n':		/* 802.11n */
646170531Ssam				flags |= _CHAN_HT;	/* NB: private */
647170531Ssam				break;
648170531Ssam			case 'd':		/* dt = Atheros Dynamic Turbo */
649170531Ssam				flags |= IEEE80211_CHAN_TURBO;
650170531Ssam				break;
651170531Ssam			case 't':		/* ht, dt, st, t */
652170531Ssam				/* dt and unadorned t specify Dynamic Turbo */
653170531Ssam				if ((flags & (IEEE80211_CHAN_STURBO|_CHAN_HT)) == 0)
654170531Ssam					flags |= IEEE80211_CHAN_TURBO;
655170531Ssam				break;
656170531Ssam			case 's':		/* st = Atheros Static Turbo */
657170531Ssam				flags |= IEEE80211_CHAN_STURBO;
658170531Ssam				break;
659170531Ssam			default:
660173275Ssam				errx(-1, "%s: Invalid channel attribute %c\n",
661170531Ssam				    val, *cp);
662170531Ssam			}
663170531Ssam		}
664170531Ssam	}
665170531Ssam	cp = strchr(val, '/');
666170531Ssam	if (cp != NULL) {
667170531Ssam		char *ep;
668170531Ssam		u_long cw = strtoul(cp+1, &ep, 10);
669166015Ssam
670170531Ssam		switch (cw) {
671170531Ssam		case 5:
672170531Ssam			flags |= IEEE80211_CHAN_QUARTER;
673170531Ssam			break;
674170531Ssam		case 10:
675170531Ssam			flags |= IEEE80211_CHAN_HALF;
676170531Ssam			break;
677170531Ssam		case 20:
678170531Ssam			/* NB: this may be removed below */
679170531Ssam			flags |= IEEE80211_CHAN_HT20;
680170531Ssam			break;
681170531Ssam		case 40:
682170531Ssam			if (ep != NULL && *ep == '+')
683170531Ssam				flags |= IEEE80211_CHAN_HT40U;
684170531Ssam			else if (ep != NULL && *ep == '-')
685170531Ssam				flags |= IEEE80211_CHAN_HT40D;
686170531Ssam			break;
687170531Ssam		default:
688173275Ssam			errx(-1, "%s: Invalid channel width\n", val);
689170531Ssam		}
690165570Ssam	}
691170531Ssam	/*
692170531Ssam	 * Cleanup specifications.
693170531Ssam	 */
694170531Ssam	if ((flags & _CHAN_HT) == 0) {
695170531Ssam		/*
696170531Ssam		 * If user specified freq/20 or freq/40 quietly remove
697170531Ssam		 * HT cw attributes depending on channel use.  To give
698170531Ssam		 * an explicit 20/40 width for an HT channel you must
699170531Ssam		 * indicate it is an HT channel since all HT channels
700170531Ssam		 * are also usable for legacy operation; e.g. freq:n/40.
701170531Ssam		 */
702170531Ssam		flags &= ~IEEE80211_CHAN_HT;
703170531Ssam	} else {
704170531Ssam		/*
705170531Ssam		 * Remove private indicator that this is an HT channel
706170531Ssam		 * and if no explicit channel width has been given
707170531Ssam		 * provide the default settings.
708170531Ssam		 */
709170531Ssam		flags &= ~_CHAN_HT;
710173275Ssam		if ((flags & IEEE80211_CHAN_HT) == 0) {
711173275Ssam			struct ieee80211_channel chan;
712173275Ssam			/*
713173275Ssam			 * Consult the channel list to see if we can use
714173275Ssam			 * HT40+ or HT40- (if both the map routines choose).
715173275Ssam			 */
716173275Ssam			if (freq > 255)
717173275Ssam				mapfreq(&chan, freq, 0);
718173275Ssam			else
719173275Ssam				mapchan(&chan, freq, 0);
720173275Ssam			flags |= (chan.ic_flags & IEEE80211_CHAN_HT);
721173275Ssam		}
722170531Ssam	}
723170531Ssam	return flags;
724170531Ssam#undef _CHAN_HT
725138593Ssam}
726138593Ssam
727138593Ssamstatic void
728186105Ssamgetchannel(int s, struct ieee80211_channel *chan, const char *val)
729186105Ssam{
730186105Ssam	int v, flags;
731186105Ssam	char *eptr;
732186105Ssam
733186105Ssam	memset(chan, 0, sizeof(*chan));
734186105Ssam	if (isanyarg(val)) {
735186105Ssam		chan->ic_freq = IEEE80211_CHAN_ANY;
736186105Ssam		return;
737186105Ssam	}
738186105Ssam	getchaninfo(s);
739186105Ssam	errno = 0;
740186105Ssam	v = strtol(val, &eptr, 10);
741186105Ssam	if (val[0] == '\0' || val == eptr || errno == ERANGE ||
742186105Ssam	    /* channel may be suffixed with nothing, :flag, or /width */
743186105Ssam	    (eptr[0] != '\0' && eptr[0] != ':' && eptr[0] != '/'))
744186105Ssam		errx(1, "invalid channel specification%s",
745186105Ssam		    errno == ERANGE ? " (out of range)" : "");
746186105Ssam	flags = getchannelflags(val, v);
747186105Ssam	if (v > 255) {		/* treat as frequency */
748186105Ssam		mapfreq(chan, v, flags);
749186105Ssam	} else {
750186105Ssam		mapchan(chan, v, flags);
751186105Ssam	}
752186105Ssam}
753186105Ssam
754186105Ssamstatic void
75577218Sphkset80211channel(const char *val, int d, int s, const struct afswtch *rafp)
75677218Sphk{
757170531Ssam	struct ieee80211_channel chan;
758170531Ssam
759186105Ssam	getchannel(s, &chan, val);
760170531Ssam	set80211(s, IEEE80211_IOC_CURCHAN, 0, sizeof(chan), &chan);
76177218Sphk}
76277218Sphk
763138593Ssamstatic void
764178354Ssamset80211chanswitch(const char *val, int d, int s, const struct afswtch *rafp)
765178354Ssam{
766178354Ssam	struct ieee80211_chanswitch_req csr;
767178354Ssam
768186105Ssam	getchannel(s, &csr.csa_chan, val);
769178354Ssam	csr.csa_mode = 1;
770178354Ssam	csr.csa_count = 5;
771178354Ssam	set80211(s, IEEE80211_IOC_CHANSWITCH, 0, sizeof(csr), &csr);
772178354Ssam}
773178354Ssam
774178354Ssamstatic void
77577218Sphkset80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
77677218Sphk{
77777218Sphk	int	mode;
77877218Sphk
77991454Sbrooks	if (strcasecmp(val, "none") == 0) {
78077218Sphk		mode = IEEE80211_AUTH_NONE;
78191454Sbrooks	} else if (strcasecmp(val, "open") == 0) {
78277218Sphk		mode = IEEE80211_AUTH_OPEN;
78391454Sbrooks	} else if (strcasecmp(val, "shared") == 0) {
78477218Sphk		mode = IEEE80211_AUTH_SHARED;
785138593Ssam	} else if (strcasecmp(val, "8021x") == 0) {
786138593Ssam		mode = IEEE80211_AUTH_8021X;
787138593Ssam	} else if (strcasecmp(val, "wpa") == 0) {
788138593Ssam		mode = IEEE80211_AUTH_WPA;
78977218Sphk	} else {
790150708Sru		errx(1, "unknown authmode");
79177218Sphk	}
79277218Sphk
79377218Sphk	set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
79477218Sphk}
79577218Sphk
796138593Ssamstatic void
79777218Sphkset80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
79877218Sphk{
79977218Sphk	int	mode;
80077218Sphk
80191454Sbrooks	if (strcasecmp(val, "off") == 0) {
80277218Sphk		mode = IEEE80211_POWERSAVE_OFF;
80391454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
80477218Sphk		mode = IEEE80211_POWERSAVE_ON;
80591454Sbrooks	} else if (strcasecmp(val, "cam") == 0) {
80677218Sphk		mode = IEEE80211_POWERSAVE_CAM;
80791454Sbrooks	} else if (strcasecmp(val, "psp") == 0) {
80877218Sphk		mode = IEEE80211_POWERSAVE_PSP;
80991454Sbrooks	} else if (strcasecmp(val, "psp-cam") == 0) {
81077218Sphk		mode = IEEE80211_POWERSAVE_PSP_CAM;
81177218Sphk	} else {
812150708Sru		errx(1, "unknown powersavemode");
81377218Sphk	}
81477218Sphk
81577218Sphk	set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
81677218Sphk}
81777218Sphk
818138593Ssamstatic void
81977218Sphkset80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
82077218Sphk{
82177218Sphk	if (d == 0)
82277218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
82377218Sphk		    0, NULL);
82477218Sphk	else
82577218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
82677218Sphk		    0, NULL);
82777218Sphk}
82877218Sphk
829138593Ssamstatic void
83077218Sphkset80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
83177218Sphk{
83277218Sphk	set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
83377218Sphk}
83477218Sphk
835138593Ssamstatic void
83677218Sphkset80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
83777218Sphk{
83877218Sphk	int	mode;
83977218Sphk
84091454Sbrooks	if (strcasecmp(val, "off") == 0) {
84177218Sphk		mode = IEEE80211_WEP_OFF;
84291454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
84377218Sphk		mode = IEEE80211_WEP_ON;
84491454Sbrooks	} else if (strcasecmp(val, "mixed") == 0) {
84577218Sphk		mode = IEEE80211_WEP_MIXED;
84677218Sphk	} else {
847150708Sru		errx(1, "unknown wep mode");
84877218Sphk	}
84977218Sphk
85077218Sphk	set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
85177218Sphk}
85277218Sphk
853138593Ssamstatic void
85477218Sphkset80211wep(const char *val, int d, int s, const struct afswtch *rafp)
85577218Sphk{
85677218Sphk	set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
85777218Sphk}
85877218Sphk
859139493Ssamstatic int
860139493Ssamisundefarg(const char *arg)
861139493Ssam{
862139493Ssam	return (strcmp(arg, "-") == 0 || strncasecmp(arg, "undef", 5) == 0);
863139493Ssam}
864139493Ssam
865138593Ssamstatic void
86677218Sphkset80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
86777218Sphk{
868139493Ssam	if (isundefarg(val))
869139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, IEEE80211_KEYIX_NONE, 0, NULL);
870139493Ssam	else
871139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
87277218Sphk}
87377218Sphk
874138593Ssamstatic void
87577218Sphkset80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
87677218Sphk{
87777218Sphk	int		key = 0;
87877218Sphk	int		len;
879120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
88077218Sphk
881178354Ssam	if (isdigit((int)val[0]) && val[1] == ':') {
88277218Sphk		key = atoi(val)-1;
88377218Sphk		val += 2;
88477218Sphk	}
88577218Sphk
88677218Sphk	bzero(data, sizeof(data));
88777218Sphk	len = sizeof(data);
88877218Sphk	get_string(val, NULL, data, &len);
88977218Sphk
89077218Sphk	set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
89177218Sphk}
89277218Sphk
89377218Sphk/*
894148686Sstefanf * This function is purely a NetBSD compatability interface.  The NetBSD
895148686Sstefanf * interface is too inflexible, but it's there so we'll support it since
89677218Sphk * it's not all that hard.
89777218Sphk */
898138593Ssamstatic void
89977218Sphkset80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
90077218Sphk{
90177218Sphk	int		txkey;
90277218Sphk	int		i, len;
903120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
90477218Sphk
90577218Sphk	set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
90677218Sphk
907178354Ssam	if (isdigit((int)val[0]) && val[1] == ':') {
90877218Sphk		txkey = val[0]-'0'-1;
90977218Sphk		val += 2;
91077218Sphk
91191454Sbrooks		for (i = 0; i < 4; i++) {
91277218Sphk			bzero(data, sizeof(data));
91377218Sphk			len = sizeof(data);
91477218Sphk			val = get_string(val, ",", data, &len);
915151827Sbrooks			if (val == NULL)
916151827Sbrooks				exit(1);
91777218Sphk
91877218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
91977218Sphk		}
92077218Sphk	} else {
92177218Sphk		bzero(data, sizeof(data));
92277218Sphk		len = sizeof(data);
92377218Sphk		get_string(val, NULL, data, &len);
92477218Sphk		txkey = 0;
92577218Sphk
92677218Sphk		set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
92777218Sphk
92877218Sphk		bzero(data, sizeof(data));
92991454Sbrooks		for (i = 1; i < 4; i++)
93077218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
93177218Sphk	}
93277218Sphk
93377218Sphk	set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
93477218Sphk}
93577218Sphk
936138593Ssamstatic void
937127649Ssamset80211rtsthreshold(const char *val, int d, int s, const struct afswtch *rafp)
938127649Ssam{
939148416Ssam	set80211(s, IEEE80211_IOC_RTSTHRESHOLD,
940148416Ssam		isundefarg(val) ? IEEE80211_RTS_MAX : atoi(val), 0, NULL);
941127649Ssam}
942127649Ssam
943138593Ssamstatic void
944127649Ssamset80211protmode(const char *val, int d, int s, const struct afswtch *rafp)
945127649Ssam{
946127649Ssam	int	mode;
947127649Ssam
948127649Ssam	if (strcasecmp(val, "off") == 0) {
949127649Ssam		mode = IEEE80211_PROTMODE_OFF;
950127649Ssam	} else if (strcasecmp(val, "cts") == 0) {
951127649Ssam		mode = IEEE80211_PROTMODE_CTS;
952173275Ssam	} else if (strncasecmp(val, "rtscts", 3) == 0) {
953127649Ssam		mode = IEEE80211_PROTMODE_RTSCTS;
954127649Ssam	} else {
955150708Sru		errx(1, "unknown protection mode");
956127649Ssam	}
957127649Ssam
958127649Ssam	set80211(s, IEEE80211_IOC_PROTMODE, mode, 0, NULL);
959127649Ssam}
960127649Ssam
961138593Ssamstatic void
962173275Ssamset80211htprotmode(const char *val, int d, int s, const struct afswtch *rafp)
963173275Ssam{
964173275Ssam	int	mode;
965173275Ssam
966173275Ssam	if (strcasecmp(val, "off") == 0) {
967173275Ssam		mode = IEEE80211_PROTMODE_OFF;
968173275Ssam	} else if (strncasecmp(val, "rts", 3) == 0) {
969173275Ssam		mode = IEEE80211_PROTMODE_RTSCTS;
970173275Ssam	} else {
971173275Ssam		errx(1, "unknown protection mode");
972173275Ssam	}
973173275Ssam
974173275Ssam	set80211(s, IEEE80211_IOC_HTPROTMODE, mode, 0, NULL);
975173275Ssam}
976173275Ssam
977173275Ssamstatic void
978127649Ssamset80211txpower(const char *val, int d, int s, const struct afswtch *rafp)
979127649Ssam{
980173275Ssam	double v = atof(val);
981173275Ssam	int txpow;
982173275Ssam
983173275Ssam	txpow = (int) (2*v);
984173275Ssam	if (txpow != 2*v)
985173275Ssam		errx(-1, "invalid tx power (must be .5 dBm units)");
986173275Ssam	set80211(s, IEEE80211_IOC_TXPOWER, txpow, 0, NULL);
987127649Ssam}
988127649Ssam
989138593Ssam#define	IEEE80211_ROAMING_DEVICE	0
990138593Ssam#define	IEEE80211_ROAMING_AUTO		1
991138593Ssam#define	IEEE80211_ROAMING_MANUAL	2
992138593Ssam
993138593Ssamstatic void
994138593Ssamset80211roaming(const char *val, int d, int s, const struct afswtch *rafp)
99577218Sphk{
996138593Ssam	int mode;
99777218Sphk
998138593Ssam	if (strcasecmp(val, "device") == 0) {
999138593Ssam		mode = IEEE80211_ROAMING_DEVICE;
1000138593Ssam	} else if (strcasecmp(val, "auto") == 0) {
1001138593Ssam		mode = IEEE80211_ROAMING_AUTO;
1002138593Ssam	} else if (strcasecmp(val, "manual") == 0) {
1003138593Ssam		mode = IEEE80211_ROAMING_MANUAL;
1004138593Ssam	} else {
1005150708Sru		errx(1, "unknown roaming mode");
1006138593Ssam	}
1007138593Ssam	set80211(s, IEEE80211_IOC_ROAMING, mode, 0, NULL);
1008138593Ssam}
1009138593Ssam
1010138593Ssamstatic void
1011138593Ssamset80211wme(const char *val, int d, int s, const struct afswtch *rafp)
1012138593Ssam{
1013138593Ssam	set80211(s, IEEE80211_IOC_WME, d, 0, NULL);
1014138593Ssam}
1015138593Ssam
1016138593Ssamstatic void
1017138593Ssamset80211hidessid(const char *val, int d, int s, const struct afswtch *rafp)
1018138593Ssam{
1019138593Ssam	set80211(s, IEEE80211_IOC_HIDESSID, d, 0, NULL);
1020138593Ssam}
1021138593Ssam
1022138593Ssamstatic void
1023138593Ssamset80211apbridge(const char *val, int d, int s, const struct afswtch *rafp)
1024138593Ssam{
1025138593Ssam	set80211(s, IEEE80211_IOC_APBRIDGE, d, 0, NULL);
1026138593Ssam}
1027138593Ssam
1028138593Ssamstatic void
1029170531Ssamset80211fastframes(const char *val, int d, int s, const struct afswtch *rafp)
1030170531Ssam{
1031170531Ssam	set80211(s, IEEE80211_IOC_FF, d, 0, NULL);
1032170531Ssam}
1033170531Ssam
1034170531Ssamstatic void
1035170531Ssamset80211dturbo(const char *val, int d, int s, const struct afswtch *rafp)
1036170531Ssam{
1037170531Ssam	set80211(s, IEEE80211_IOC_TURBOP, d, 0, NULL);
1038170531Ssam}
1039170531Ssam
1040170531Ssamstatic void
1041138593Ssamset80211chanlist(const char *val, int d, int s, const struct afswtch *rafp)
1042138593Ssam{
1043138593Ssam	struct ieee80211req_chanlist chanlist;
1044138593Ssam	char *temp, *cp, *tp;
1045138593Ssam
1046138593Ssam	temp = malloc(strlen(val) + 1);
1047138593Ssam	if (temp == NULL)
1048138593Ssam		errx(1, "malloc failed");
1049138593Ssam	strcpy(temp, val);
1050138593Ssam	memset(&chanlist, 0, sizeof(chanlist));
1051138593Ssam	cp = temp;
1052138593Ssam	for (;;) {
1053173275Ssam		int first, last, f, c;
1054138593Ssam
1055138593Ssam		tp = strchr(cp, ',');
1056138593Ssam		if (tp != NULL)
1057138593Ssam			*tp++ = '\0';
1058138593Ssam		switch (sscanf(cp, "%u-%u", &first, &last)) {
1059138593Ssam		case 1:
1060187801Ssam			if (first > IEEE80211_CHAN_MAX)
1061188038Sdelphij				errx(-1, "channel %u out of range, max %u",
1062187801Ssam					first, IEEE80211_CHAN_MAX);
1063138593Ssam			setbit(chanlist.ic_channels, first);
1064138593Ssam			break;
1065138593Ssam		case 2:
1066187801Ssam			if (first > IEEE80211_CHAN_MAX)
1067188038Sdelphij				errx(-1, "channel %u out of range, max %u",
1068187801Ssam					first, IEEE80211_CHAN_MAX);
1069187801Ssam			if (last > IEEE80211_CHAN_MAX)
1070188038Sdelphij				errx(-1, "channel %u out of range, max %u",
1071187801Ssam					last, IEEE80211_CHAN_MAX);
1072138593Ssam			if (first > last)
1073138593Ssam				errx(-1, "void channel range, %u > %u",
1074138593Ssam					first, last);
1075138593Ssam			for (f = first; f <= last; f++)
1076138593Ssam				setbit(chanlist.ic_channels, f);
1077138593Ssam			break;
1078138593Ssam		}
1079138593Ssam		if (tp == NULL)
1080138593Ssam			break;
1081173275Ssam		c = *tp;
1082173275Ssam		while (isspace(c))
1083138593Ssam			tp++;
1084173275Ssam		if (!isdigit(c))
1085138593Ssam			break;
1086138593Ssam		cp = tp;
1087138593Ssam	}
1088170531Ssam	set80211(s, IEEE80211_IOC_CHANLIST, 0, sizeof(chanlist), &chanlist);
1089138593Ssam}
1090138593Ssam
1091138593Ssamstatic void
1092138593Ssamset80211bssid(const char *val, int d, int s, const struct afswtch *rafp)
1093138593Ssam{
1094138593Ssam
1095138593Ssam	if (!isanyarg(val)) {
1096138593Ssam		char *temp;
1097138593Ssam		struct sockaddr_dl sdl;
1098138593Ssam
1099155702Ssam		temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1100138593Ssam		if (temp == NULL)
1101138593Ssam			errx(1, "malloc failed");
1102138593Ssam		temp[0] = ':';
1103138593Ssam		strcpy(temp + 1, val);
1104138593Ssam		sdl.sdl_len = sizeof(sdl);
1105138593Ssam		link_addr(temp, &sdl);
1106138593Ssam		free(temp);
1107138593Ssam		if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1108138593Ssam			errx(1, "malformed link-level address");
1109138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
1110138593Ssam			IEEE80211_ADDR_LEN, LLADDR(&sdl));
1111138593Ssam	} else {
1112138593Ssam		uint8_t zerobssid[IEEE80211_ADDR_LEN];
1113138593Ssam		memset(zerobssid, 0, sizeof(zerobssid));
1114138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
1115138593Ssam			IEEE80211_ADDR_LEN, zerobssid);
1116138593Ssam	}
1117138593Ssam}
1118138593Ssam
1119138593Ssamstatic int
1120138593Ssamgetac(const char *ac)
1121138593Ssam{
1122138593Ssam	if (strcasecmp(ac, "ac_be") == 0 || strcasecmp(ac, "be") == 0)
1123138593Ssam		return WME_AC_BE;
1124138593Ssam	if (strcasecmp(ac, "ac_bk") == 0 || strcasecmp(ac, "bk") == 0)
1125138593Ssam		return WME_AC_BK;
1126138593Ssam	if (strcasecmp(ac, "ac_vi") == 0 || strcasecmp(ac, "vi") == 0)
1127138593Ssam		return WME_AC_VI;
1128138593Ssam	if (strcasecmp(ac, "ac_vo") == 0 || strcasecmp(ac, "vo") == 0)
1129138593Ssam		return WME_AC_VO;
1130138593Ssam	errx(1, "unknown wme access class %s", ac);
1131138593Ssam}
1132138593Ssam
1133138593Ssamstatic
1134138593SsamDECL_CMD_FUNC2(set80211cwmin, ac, val)
1135138593Ssam{
1136138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val), getac(ac), NULL);
1137138593Ssam}
1138138593Ssam
1139138593Ssamstatic
1140138593SsamDECL_CMD_FUNC2(set80211cwmax, ac, val)
1141138593Ssam{
1142138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val), getac(ac), NULL);
1143138593Ssam}
1144138593Ssam
1145138593Ssamstatic
1146138593SsamDECL_CMD_FUNC2(set80211aifs, ac, val)
1147138593Ssam{
1148138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val), getac(ac), NULL);
1149138593Ssam}
1150138593Ssam
1151138593Ssamstatic
1152138593SsamDECL_CMD_FUNC2(set80211txoplimit, ac, val)
1153138593Ssam{
1154138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), getac(ac), NULL);
1155138593Ssam}
1156138593Ssam
1157138593Ssamstatic
1158148621SsamDECL_CMD_FUNC(set80211acm, ac, d)
1159138593Ssam{
1160148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 1, getac(ac), NULL);
1161138593Ssam}
1162148621Ssamstatic
1163148621SsamDECL_CMD_FUNC(set80211noacm, ac, d)
1164148621Ssam{
1165148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 0, getac(ac), NULL);
1166148621Ssam}
1167138593Ssam
1168138593Ssamstatic
1169148621SsamDECL_CMD_FUNC(set80211ackpolicy, ac, d)
1170138593Ssam{
1171148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 1, getac(ac), NULL);
1172138593Ssam}
1173148621Ssamstatic
1174148621SsamDECL_CMD_FUNC(set80211noackpolicy, ac, d)
1175148621Ssam{
1176148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 0, getac(ac), NULL);
1177148621Ssam}
1178138593Ssam
1179138593Ssamstatic
1180138593SsamDECL_CMD_FUNC2(set80211bsscwmin, ac, val)
1181138593Ssam{
1182138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val),
1183138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1184138593Ssam}
1185138593Ssam
1186138593Ssamstatic
1187138593SsamDECL_CMD_FUNC2(set80211bsscwmax, ac, val)
1188138593Ssam{
1189138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val),
1190138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1191138593Ssam}
1192138593Ssam
1193138593Ssamstatic
1194138593SsamDECL_CMD_FUNC2(set80211bssaifs, ac, val)
1195138593Ssam{
1196138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val),
1197138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1198138593Ssam}
1199138593Ssam
1200138593Ssamstatic
1201138593SsamDECL_CMD_FUNC2(set80211bsstxoplimit, ac, val)
1202138593Ssam{
1203138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val),
1204138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1205138593Ssam}
1206138593Ssam
1207138593Ssamstatic
1208138593SsamDECL_CMD_FUNC(set80211dtimperiod, val, d)
1209138593Ssam{
1210138593Ssam	set80211(s, IEEE80211_IOC_DTIM_PERIOD, atoi(val), 0, NULL);
1211138593Ssam}
1212138593Ssam
1213138593Ssamstatic
1214138593SsamDECL_CMD_FUNC(set80211bintval, val, d)
1215138593Ssam{
1216138593Ssam	set80211(s, IEEE80211_IOC_BEACON_INTERVAL, atoi(val), 0, NULL);
1217138593Ssam}
1218138593Ssam
1219138593Ssamstatic void
1220138593Ssamset80211macmac(int s, int op, const char *val)
1221138593Ssam{
1222138593Ssam	char *temp;
1223138593Ssam	struct sockaddr_dl sdl;
1224138593Ssam
1225155702Ssam	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1226138593Ssam	if (temp == NULL)
1227138593Ssam		errx(1, "malloc failed");
1228138593Ssam	temp[0] = ':';
1229138593Ssam	strcpy(temp + 1, val);
1230138593Ssam	sdl.sdl_len = sizeof(sdl);
1231138593Ssam	link_addr(temp, &sdl);
1232138593Ssam	free(temp);
1233138593Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1234138593Ssam		errx(1, "malformed link-level address");
1235138593Ssam	set80211(s, op, 0, IEEE80211_ADDR_LEN, LLADDR(&sdl));
1236138593Ssam}
1237138593Ssam
1238138593Ssamstatic
1239138593SsamDECL_CMD_FUNC(set80211addmac, val, d)
1240138593Ssam{
1241138593Ssam	set80211macmac(s, IEEE80211_IOC_ADDMAC, val);
1242138593Ssam}
1243138593Ssam
1244138593Ssamstatic
1245138593SsamDECL_CMD_FUNC(set80211delmac, val, d)
1246138593Ssam{
1247138593Ssam	set80211macmac(s, IEEE80211_IOC_DELMAC, val);
1248138593Ssam}
1249138593Ssam
1250138593Ssamstatic
1251149029SsamDECL_CMD_FUNC(set80211kickmac, val, d)
1252149029Ssam{
1253149029Ssam	char *temp;
1254149029Ssam	struct sockaddr_dl sdl;
1255149029Ssam	struct ieee80211req_mlme mlme;
1256149029Ssam
1257155702Ssam	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1258149029Ssam	if (temp == NULL)
1259149029Ssam		errx(1, "malloc failed");
1260149029Ssam	temp[0] = ':';
1261149029Ssam	strcpy(temp + 1, val);
1262149029Ssam	sdl.sdl_len = sizeof(sdl);
1263149029Ssam	link_addr(temp, &sdl);
1264149029Ssam	free(temp);
1265149029Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1266149029Ssam		errx(1, "malformed link-level address");
1267149029Ssam	memset(&mlme, 0, sizeof(mlme));
1268149029Ssam	mlme.im_op = IEEE80211_MLME_DEAUTH;
1269149029Ssam	mlme.im_reason = IEEE80211_REASON_AUTH_EXPIRE;
1270149029Ssam	memcpy(mlme.im_macaddr, LLADDR(&sdl), IEEE80211_ADDR_LEN);
1271170531Ssam	set80211(s, IEEE80211_IOC_MLME, 0, sizeof(mlme), &mlme);
1272149029Ssam}
1273149029Ssam
1274149029Ssamstatic
1275138593SsamDECL_CMD_FUNC(set80211maccmd, val, d)
1276138593Ssam{
1277138593Ssam	set80211(s, IEEE80211_IOC_MACCMD, d, 0, NULL);
1278138593Ssam}
1279138593Ssam
1280147795Ssamstatic void
1281195618Srpauloset80211meshrtmac(int s, int req, const char *val)
1282195618Srpaulo{
1283195618Srpaulo	char *temp;
1284195618Srpaulo	struct sockaddr_dl sdl;
1285195618Srpaulo
1286195618Srpaulo	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1287195618Srpaulo	if (temp == NULL)
1288195618Srpaulo		errx(1, "malloc failed");
1289195618Srpaulo	temp[0] = ':';
1290195618Srpaulo	strcpy(temp + 1, val);
1291195618Srpaulo	sdl.sdl_len = sizeof(sdl);
1292195618Srpaulo	link_addr(temp, &sdl);
1293195618Srpaulo	free(temp);
1294195618Srpaulo	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1295195618Srpaulo		errx(1, "malformed link-level address");
1296195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_RTCMD, req,
1297195618Srpaulo	    IEEE80211_ADDR_LEN, LLADDR(&sdl));
1298195618Srpaulo}
1299195618Srpaulo
1300195618Srpaulostatic
1301195618SrpauloDECL_CMD_FUNC(set80211addmeshrt, val, d)
1302195618Srpaulo{
1303195618Srpaulo	set80211meshrtmac(s, IEEE80211_MESH_RTCMD_ADD, val);
1304195618Srpaulo}
1305195618Srpaulo
1306195618Srpaulostatic
1307195618SrpauloDECL_CMD_FUNC(set80211delmeshrt, val, d)
1308195618Srpaulo{
1309195618Srpaulo	set80211meshrtmac(s, IEEE80211_MESH_RTCMD_DELETE, val);
1310195618Srpaulo}
1311195618Srpaulo
1312195618Srpaulostatic
1313195618SrpauloDECL_CMD_FUNC(set80211meshrtcmd, val, d)
1314195618Srpaulo{
1315195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_RTCMD, d, 0, NULL);
1316195618Srpaulo}
1317195618Srpaulo
1318195618Srpaulostatic
1319195618SrpauloDECL_CMD_FUNC(set80211hwmprootmode, val, d)
1320195618Srpaulo{
1321195618Srpaulo	int mode;
1322195618Srpaulo
1323195618Srpaulo	if (strcasecmp(val, "normal") == 0)
1324195618Srpaulo		mode = IEEE80211_HWMP_ROOTMODE_NORMAL;
1325195618Srpaulo	else if (strcasecmp(val, "proactive") == 0)
1326195618Srpaulo		mode = IEEE80211_HWMP_ROOTMODE_PROACTIVE;
1327195618Srpaulo	else if (strcasecmp(val, "rann") == 0)
1328195618Srpaulo		mode = IEEE80211_HWMP_ROOTMODE_RANN;
1329195618Srpaulo	else
1330195618Srpaulo		mode = IEEE80211_HWMP_ROOTMODE_DISABLED;
1331195618Srpaulo	set80211(s, IEEE80211_IOC_HWMP_ROOTMODE, mode, 0, NULL);
1332195618Srpaulo}
1333195618Srpaulo
1334195618Srpaulostatic
1335195618SrpauloDECL_CMD_FUNC(set80211hwmpmaxhops, val, d)
1336195618Srpaulo{
1337195618Srpaulo	set80211(s, IEEE80211_IOC_HWMP_MAXHOPS, atoi(val), 0, NULL);
1338195618Srpaulo}
1339195618Srpaulo
1340195618Srpaulostatic void
1341147795Ssamset80211pureg(const char *val, int d, int s, const struct afswtch *rafp)
1342147795Ssam{
1343147795Ssam	set80211(s, IEEE80211_IOC_PUREG, d, 0, NULL);
1344147795Ssam}
1345147795Ssam
1346153422Ssamstatic void
1347170531Ssamset80211bgscan(const char *val, int d, int s, const struct afswtch *rafp)
1348153422Ssam{
1349170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN, d, 0, NULL);
1350153422Ssam}
1351153422Ssam
1352148416Ssamstatic
1353170531SsamDECL_CMD_FUNC(set80211bgscanidle, val, d)
1354170531Ssam{
1355170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN_IDLE, atoi(val), 0, NULL);
1356170531Ssam}
1357170531Ssam
1358170531Ssamstatic
1359170531SsamDECL_CMD_FUNC(set80211bgscanintvl, val, d)
1360170531Ssam{
1361170531Ssam	set80211(s, IEEE80211_IOC_BGSCAN_INTERVAL, atoi(val), 0, NULL);
1362170531Ssam}
1363170531Ssam
1364170531Ssamstatic
1365170531SsamDECL_CMD_FUNC(set80211scanvalid, val, d)
1366170531Ssam{
1367170531Ssam	set80211(s, IEEE80211_IOC_SCANVALID, atoi(val), 0, NULL);
1368170531Ssam}
1369170531Ssam
1370178354Ssam/*
1371178354Ssam * Parse an optional trailing specification of which netbands
1372178354Ssam * to apply a parameter to.  This is basically the same syntax
1373178354Ssam * as used for channels but you can concatenate to specify
1374178354Ssam * multiple.  For example:
1375178354Ssam *	14:abg		apply to 11a, 11b, and 11g
1376178354Ssam *	6:ht		apply to 11na and 11ng
1377178354Ssam * We don't make a big effort to catch silly things; this is
1378178354Ssam * really a convenience mechanism.
1379178354Ssam */
1380178354Ssamstatic int
1381178354Ssamgetmodeflags(const char *val)
1382170531Ssam{
1383178354Ssam	const char *cp;
1384178354Ssam	int flags;
1385178354Ssam
1386178354Ssam	flags = 0;
1387178354Ssam
1388178354Ssam	cp = strchr(val, ':');
1389178354Ssam	if (cp != NULL) {
1390178354Ssam		for (cp++; isalpha((int) *cp); cp++) {
1391178354Ssam			/* accept mixed case */
1392178354Ssam			int c = *cp;
1393178354Ssam			if (isupper(c))
1394178354Ssam				c = tolower(c);
1395178354Ssam			switch (c) {
1396178354Ssam			case 'a':		/* 802.11a */
1397178354Ssam				flags |= IEEE80211_CHAN_A;
1398178354Ssam				break;
1399178354Ssam			case 'b':		/* 802.11b */
1400178354Ssam				flags |= IEEE80211_CHAN_B;
1401178354Ssam				break;
1402178354Ssam			case 'g':		/* 802.11g */
1403178354Ssam				flags |= IEEE80211_CHAN_G;
1404178354Ssam				break;
1405178354Ssam			case 'n':		/* 802.11n */
1406178354Ssam				flags |= IEEE80211_CHAN_HT;
1407178354Ssam				break;
1408178354Ssam			case 'd':		/* dt = Atheros Dynamic Turbo */
1409178354Ssam				flags |= IEEE80211_CHAN_TURBO;
1410178354Ssam				break;
1411178354Ssam			case 't':		/* ht, dt, st, t */
1412178354Ssam				/* dt and unadorned t specify Dynamic Turbo */
1413178354Ssam				if ((flags & (IEEE80211_CHAN_STURBO|IEEE80211_CHAN_HT)) == 0)
1414178354Ssam					flags |= IEEE80211_CHAN_TURBO;
1415178354Ssam				break;
1416178354Ssam			case 's':		/* st = Atheros Static Turbo */
1417178354Ssam				flags |= IEEE80211_CHAN_STURBO;
1418178354Ssam				break;
1419188784Ssam			case 'h':		/* 1/2-width channels */
1420188784Ssam				flags |= IEEE80211_CHAN_HALF;
1421188784Ssam				break;
1422188784Ssam			case 'q':		/* 1/4-width channels */
1423188784Ssam				flags |= IEEE80211_CHAN_QUARTER;
1424188784Ssam				break;
1425178354Ssam			default:
1426178354Ssam				errx(-1, "%s: Invalid mode attribute %c\n",
1427178354Ssam				    val, *cp);
1428178354Ssam			}
1429178354Ssam		}
1430178354Ssam	}
1431178354Ssam	return flags;
1432170531Ssam}
1433170531Ssam
1434178354Ssam#define	IEEE80211_CHAN_HTA	(IEEE80211_CHAN_HT|IEEE80211_CHAN_5GHZ)
1435178354Ssam#define	IEEE80211_CHAN_HTG	(IEEE80211_CHAN_HT|IEEE80211_CHAN_2GHZ)
1436178354Ssam
1437178354Ssam#define	_APPLY(_flags, _base, _param, _v) do {				\
1438178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1439178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1440178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1441178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1442178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1443178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1444178354Ssam	    else							\
1445178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1446178354Ssam    }									\
1447178354Ssam    if (_flags & IEEE80211_CHAN_TURBO) {				\
1448178354Ssam	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1449178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;	\
1450178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;	\
1451178354Ssam	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1452178354Ssam		    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;	\
1453178354Ssam	    else							\
1454178354Ssam		    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;	\
1455178354Ssam    }									\
1456178354Ssam    if (_flags & IEEE80211_CHAN_STURBO)					\
1457178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = _v;		\
1458178354Ssam    if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1459178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = _v;		\
1460178354Ssam    if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1461178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = _v;		\
1462178354Ssam    if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1463178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = _v;		\
1464188784Ssam    if (_flags & IEEE80211_CHAN_HALF)					\
1465188784Ssam	    _base.params[IEEE80211_MODE_HALF]._param = _v;		\
1466188784Ssam    if (_flags & IEEE80211_CHAN_QUARTER)				\
1467188784Ssam	    _base.params[IEEE80211_MODE_QUARTER]._param = _v;		\
1468178354Ssam} while (0)
1469178354Ssam#define	_APPLY1(_flags, _base, _param, _v) do {				\
1470178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1471178354Ssam	    if (_flags & IEEE80211_CHAN_5GHZ)				\
1472178354Ssam		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1473178354Ssam	    else							\
1474178354Ssam		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1475178354Ssam    } else if ((_flags & IEEE80211_CHAN_108A) == IEEE80211_CHAN_108A)	\
1476178354Ssam	    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;		\
1477178354Ssam    else if ((_flags & IEEE80211_CHAN_108G) == IEEE80211_CHAN_108G)	\
1478178354Ssam	    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;		\
1479178354Ssam    else if ((_flags & IEEE80211_CHAN_ST) == IEEE80211_CHAN_ST)		\
1480178354Ssam	    _base.params[IEEE80211_MODE_STURBO_A]._param = _v;		\
1481188784Ssam    else if (_flags & IEEE80211_CHAN_HALF)				\
1482188784Ssam	    _base.params[IEEE80211_MODE_HALF]._param = _v;		\
1483188784Ssam    else if (_flags & IEEE80211_CHAN_QUARTER)				\
1484188784Ssam	    _base.params[IEEE80211_MODE_QUARTER]._param = _v;		\
1485178354Ssam    else if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1486178354Ssam	    _base.params[IEEE80211_MODE_11A]._param = _v;		\
1487178354Ssam    else if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1488178354Ssam	    _base.params[IEEE80211_MODE_11G]._param = _v;		\
1489178354Ssam    else if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1490178354Ssam	    _base.params[IEEE80211_MODE_11B]._param = _v;		\
1491178354Ssam} while (0)
1492178354Ssam#define	_APPLY_RATE(_flags, _base, _param, _v) do {			\
1493178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1494188784Ssam	(_v) = (_v / 2) | IEEE80211_RATE_MCS;				\
1495178354Ssam    }									\
1496188784Ssam    _APPLY(_flags, _base, _param, _v);					\
1497178354Ssam} while (0)
1498178354Ssam#define	_APPLY_RATE1(_flags, _base, _param, _v) do {			\
1499178354Ssam    if (_flags & IEEE80211_CHAN_HT) {					\
1500188784Ssam	(_v) = (_v / 2) | IEEE80211_RATE_MCS;				\
1501188784Ssam    }									\
1502188784Ssam    _APPLY1(_flags, _base, _param, _v);					\
1503178354Ssam} while (0)
1504178354Ssam
1505170531Ssamstatic
1506178354SsamDECL_CMD_FUNC(set80211roamrssi, val, d)
1507170531Ssam{
1508178354Ssam	double v = atof(val);
1509178354Ssam	int rssi, flags;
1510178354Ssam
1511178354Ssam	rssi = (int) (2*v);
1512178354Ssam	if (rssi != 2*v)
1513178354Ssam		errx(-1, "invalid rssi (must be .5 dBm units)");
1514178354Ssam	flags = getmodeflags(val);
1515178354Ssam	getroam(s);
1516178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1517178354Ssam		flags = getcurchan(s)->ic_flags;
1518178354Ssam		_APPLY1(flags, roamparams, rssi, rssi);
1519178354Ssam	} else
1520178354Ssam		_APPLY(flags, roamparams, rssi, rssi);
1521178354Ssam	callback_register(setroam_cb, &roamparams);
1522170531Ssam}
1523170531Ssam
1524188784Ssamstatic int
1525188784Ssamgetrate(const char *val, const char *tag)
1526188784Ssam{
1527188784Ssam	double v = atof(val);
1528188784Ssam	int rate;
1529188784Ssam
1530188784Ssam	rate = (int) (2*v);
1531188784Ssam	if (rate != 2*v)
1532188784Ssam		errx(-1, "invalid %s rate (must be .5 Mb/s units)", tag);
1533188784Ssam	return rate;		/* NB: returns 2x the specified value */
1534188784Ssam}
1535188784Ssam
1536170531Ssamstatic
1537178354SsamDECL_CMD_FUNC(set80211roamrate, val, d)
1538170531Ssam{
1539188784Ssam	int rate, flags;
1540178354Ssam
1541188784Ssam	rate = getrate(val, "roam");
1542178354Ssam	flags = getmodeflags(val);
1543178354Ssam	getroam(s);
1544178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1545178354Ssam		flags = getcurchan(s)->ic_flags;
1546188784Ssam		_APPLY_RATE1(flags, roamparams, rate, rate);
1547178354Ssam	} else
1548188784Ssam		_APPLY_RATE(flags, roamparams, rate, rate);
1549178354Ssam	callback_register(setroam_cb, &roamparams);
1550170531Ssam}
1551170531Ssam
1552170531Ssamstatic
1553178354SsamDECL_CMD_FUNC(set80211mcastrate, val, d)
1554170531Ssam{
1555188784Ssam	int rate, flags;
1556178354Ssam
1557188784Ssam	rate = getrate(val, "mcast");
1558178354Ssam	flags = getmodeflags(val);
1559178354Ssam	gettxparams(s);
1560178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1561178354Ssam		flags = getcurchan(s)->ic_flags;
1562188784Ssam		_APPLY_RATE1(flags, txparams, mcastrate, rate);
1563178354Ssam	} else
1564188784Ssam		_APPLY_RATE(flags, txparams, mcastrate, rate);
1565178354Ssam	callback_register(settxparams_cb, &txparams);
1566170531Ssam}
1567170531Ssam
1568170531Ssamstatic
1569178354SsamDECL_CMD_FUNC(set80211mgtrate, val, d)
1570170531Ssam{
1571188784Ssam	int rate, flags;
1572178354Ssam
1573188784Ssam	rate = getrate(val, "mgmt");
1574178354Ssam	flags = getmodeflags(val);
1575178354Ssam	gettxparams(s);
1576178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1577178354Ssam		flags = getcurchan(s)->ic_flags;
1578188784Ssam		_APPLY_RATE1(flags, txparams, mgmtrate, rate);
1579178354Ssam	} else
1580188784Ssam		_APPLY_RATE(flags, txparams, mgmtrate, rate);
1581178354Ssam	callback_register(settxparams_cb, &txparams);
1582170531Ssam}
1583170531Ssam
1584170531Ssamstatic
1585178354SsamDECL_CMD_FUNC(set80211ucastrate, val, d)
1586170531Ssam{
1587188784Ssam	int flags;
1588178354Ssam
1589178354Ssam	gettxparams(s);
1590178354Ssam	flags = getmodeflags(val);
1591178354Ssam	if (isanyarg(val)) {
1592178354Ssam		if (flags == 0) {	/* NB: no flags => current channel */
1593178354Ssam			flags = getcurchan(s)->ic_flags;
1594178354Ssam			_APPLY1(flags, txparams, ucastrate,
1595178354Ssam			    IEEE80211_FIXED_RATE_NONE);
1596178354Ssam		} else
1597178354Ssam			_APPLY(flags, txparams, ucastrate,
1598178354Ssam			    IEEE80211_FIXED_RATE_NONE);
1599178354Ssam	} else {
1600188784Ssam		int rate = getrate(val, "ucast");
1601178354Ssam		if (flags == 0) {	/* NB: no flags => current channel */
1602178354Ssam			flags = getcurchan(s)->ic_flags;
1603188784Ssam			_APPLY_RATE1(flags, txparams, ucastrate, rate);
1604178354Ssam		} else
1605188784Ssam			_APPLY_RATE(flags, txparams, ucastrate, rate);
1606178354Ssam	}
1607178354Ssam	callback_register(settxparams_cb, &txparams);
1608170531Ssam}
1609170531Ssam
1610170531Ssamstatic
1611178354SsamDECL_CMD_FUNC(set80211maxretry, val, d)
1612153354Ssam{
1613178354Ssam	int v = atoi(val), flags;
1614178354Ssam
1615178354Ssam	flags = getmodeflags(val);
1616178354Ssam	gettxparams(s);
1617178354Ssam	if (flags == 0) {		/* NB: no flags => current channel */
1618178354Ssam		flags = getcurchan(s)->ic_flags;
1619178354Ssam		_APPLY1(flags, txparams, maxretry, v);
1620178354Ssam	} else
1621178354Ssam		_APPLY(flags, txparams, maxretry, v);
1622178354Ssam	callback_register(settxparams_cb, &txparams);
1623153354Ssam}
1624178354Ssam#undef _APPLY_RATE
1625178354Ssam#undef _APPLY
1626178354Ssam#undef IEEE80211_CHAN_HTA
1627178354Ssam#undef IEEE80211_CHAN_HTG
1628153354Ssam
1629153354Ssamstatic
1630148416SsamDECL_CMD_FUNC(set80211fragthreshold, val, d)
1631148416Ssam{
1632148416Ssam	set80211(s, IEEE80211_IOC_FRAGTHRESHOLD,
1633148416Ssam		isundefarg(val) ? IEEE80211_FRAG_MAX : atoi(val), 0, NULL);
1634148416Ssam}
1635148416Ssam
1636160687Ssamstatic
1637160687SsamDECL_CMD_FUNC(set80211bmissthreshold, val, d)
1638160687Ssam{
1639160687Ssam	set80211(s, IEEE80211_IOC_BMISSTHRESHOLD,
1640160687Ssam		isundefarg(val) ? IEEE80211_HWBMISS_MAX : atoi(val), 0, NULL);
1641160687Ssam}
1642160687Ssam
1643170531Ssamstatic void
1644170531Ssamset80211burst(const char *val, int d, int s, const struct afswtch *rafp)
1645170531Ssam{
1646170531Ssam	set80211(s, IEEE80211_IOC_BURST, d, 0, NULL);
1647170531Ssam}
1648170531Ssam
1649170531Ssamstatic void
1650170531Ssamset80211doth(const char *val, int d, int s, const struct afswtch *rafp)
1651170531Ssam{
1652170531Ssam	set80211(s, IEEE80211_IOC_DOTH, d, 0, NULL);
1653170531Ssam}
1654170531Ssam
1655173275Ssamstatic void
1656178354Ssamset80211dfs(const char *val, int d, int s, const struct afswtch *rafp)
1657178354Ssam{
1658178354Ssam	set80211(s, IEEE80211_IOC_DFS, d, 0, NULL);
1659178354Ssam}
1660178354Ssam
1661178354Ssamstatic void
1662173275Ssamset80211shortgi(const char *val, int d, int s, const struct afswtch *rafp)
1663173275Ssam{
1664173275Ssam	set80211(s, IEEE80211_IOC_SHORTGI,
1665173275Ssam		d ? (IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40) : 0,
1666173275Ssam		0, NULL);
1667173275Ssam}
1668173275Ssam
1669173275Ssamstatic void
1670173275Ssamset80211ampdu(const char *val, int d, int s, const struct afswtch *rafp)
1671173275Ssam{
1672173275Ssam	int ampdu;
1673173275Ssam
1674173275Ssam	if (get80211val(s, IEEE80211_IOC_AMPDU, &ampdu) < 0)
1675173275Ssam		errx(-1, "cannot get AMPDU setting");
1676173275Ssam	if (d < 0) {
1677173275Ssam		d = -d;
1678173275Ssam		ampdu &= ~d;
1679173275Ssam	} else
1680173275Ssam		ampdu |= d;
1681173275Ssam	set80211(s, IEEE80211_IOC_AMPDU, ampdu, 0, NULL);
1682173275Ssam}
1683173275Ssam
1684173275Ssamstatic
1685173275SsamDECL_CMD_FUNC(set80211ampdulimit, val, d)
1686173275Ssam{
1687173275Ssam	int v;
1688173275Ssam
1689173275Ssam	switch (atoi(val)) {
1690173275Ssam	case 8:
1691173275Ssam	case 8*1024:
1692173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_8K;
1693173275Ssam		break;
1694173275Ssam	case 16:
1695173275Ssam	case 16*1024:
1696173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_16K;
1697173275Ssam		break;
1698173275Ssam	case 32:
1699173275Ssam	case 32*1024:
1700173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_32K;
1701173275Ssam		break;
1702173275Ssam	case 64:
1703173275Ssam	case 64*1024:
1704173275Ssam		v = IEEE80211_HTCAP_MAXRXAMPDU_64K;
1705173275Ssam		break;
1706173275Ssam	default:
1707173275Ssam		errx(-1, "invalid A-MPDU limit %s", val);
1708173275Ssam	}
1709173275Ssam	set80211(s, IEEE80211_IOC_AMPDU_LIMIT, v, 0, NULL);
1710173275Ssam}
1711173275Ssam
1712173275Ssamstatic
1713173275SsamDECL_CMD_FUNC(set80211ampdudensity, val, d)
1714173275Ssam{
1715173275Ssam	int v;
1716173275Ssam
1717183260Ssam	if (isanyarg(val) || strcasecmp(val, "na") == 0)
1718173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1719173275Ssam	else switch ((int)(atof(val)*4)) {
1720173275Ssam	case 0:
1721173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1722173275Ssam		break;
1723173275Ssam	case 1:
1724173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_025;
1725173275Ssam		break;
1726173275Ssam	case 2:
1727173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_05;
1728173275Ssam		break;
1729173275Ssam	case 4:
1730173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_1;
1731173275Ssam		break;
1732173275Ssam	case 8:
1733173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_2;
1734173275Ssam		break;
1735173275Ssam	case 16:
1736173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_4;
1737173275Ssam		break;
1738173275Ssam	case 32:
1739173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_8;
1740173275Ssam		break;
1741173275Ssam	case 64:
1742173275Ssam		v = IEEE80211_HTCAP_MPDUDENSITY_16;
1743173275Ssam		break;
1744173275Ssam	default:
1745173275Ssam		errx(-1, "invalid A-MPDU density %s", val);
1746173275Ssam	}
1747173275Ssam	set80211(s, IEEE80211_IOC_AMPDU_DENSITY, v, 0, NULL);
1748173275Ssam}
1749173275Ssam
1750173275Ssamstatic void
1751173275Ssamset80211amsdu(const char *val, int d, int s, const struct afswtch *rafp)
1752173275Ssam{
1753173275Ssam	int amsdu;
1754173275Ssam
1755173275Ssam	if (get80211val(s, IEEE80211_IOC_AMSDU, &amsdu) < 0)
1756187801Ssam		err(-1, "cannot get AMSDU setting");
1757173275Ssam	if (d < 0) {
1758173275Ssam		d = -d;
1759173275Ssam		amsdu &= ~d;
1760173275Ssam	} else
1761173275Ssam		amsdu |= d;
1762173275Ssam	set80211(s, IEEE80211_IOC_AMSDU, amsdu, 0, NULL);
1763173275Ssam}
1764173275Ssam
1765173275Ssamstatic
1766173275SsamDECL_CMD_FUNC(set80211amsdulimit, val, d)
1767173275Ssam{
1768173275Ssam	set80211(s, IEEE80211_IOC_AMSDU_LIMIT, atoi(val), 0, NULL);
1769173275Ssam}
1770173275Ssam
1771173275Ssamstatic void
1772173275Ssamset80211puren(const char *val, int d, int s, const struct afswtch *rafp)
1773173275Ssam{
1774173275Ssam	set80211(s, IEEE80211_IOC_PUREN, d, 0, NULL);
1775173275Ssam}
1776173275Ssam
1777173275Ssamstatic void
1778173275Ssamset80211htcompat(const char *val, int d, int s, const struct afswtch *rafp)
1779173275Ssam{
1780173275Ssam	set80211(s, IEEE80211_IOC_HTCOMPAT, d, 0, NULL);
1781173275Ssam}
1782173275Ssam
1783173275Ssamstatic void
1784173275Ssamset80211htconf(const char *val, int d, int s, const struct afswtch *rafp)
1785173275Ssam{
1786173275Ssam	set80211(s, IEEE80211_IOC_HTCONF, d, 0, NULL);
1787173275Ssam	htconf = d;
1788173275Ssam}
1789173275Ssam
1790173275Ssamstatic void
1791178354Ssamset80211dwds(const char *val, int d, int s, const struct afswtch *rafp)
1792178354Ssam{
1793178354Ssam	set80211(s, IEEE80211_IOC_DWDS, d, 0, NULL);
1794178354Ssam}
1795178354Ssam
1796178354Ssamstatic void
1797173275Ssamset80211inact(const char *val, int d, int s, const struct afswtch *rafp)
1798173275Ssam{
1799173275Ssam	set80211(s, IEEE80211_IOC_INACTIVITY, d, 0, NULL);
1800173275Ssam}
1801173275Ssam
1802173275Ssamstatic void
1803178354Ssamset80211tsn(const char *val, int d, int s, const struct afswtch *rafp)
1804178354Ssam{
1805178354Ssam	set80211(s, IEEE80211_IOC_TSN, d, 0, NULL);
1806178354Ssam}
1807178354Ssam
1808178354Ssamstatic void
1809178354Ssamset80211dotd(const char *val, int d, int s, const struct afswtch *rafp)
1810178354Ssam{
1811178354Ssam	set80211(s, IEEE80211_IOC_DOTD, d, 0, NULL);
1812178354Ssam}
1813178354Ssam
1814183261Ssamstatic void
1815183261Ssamset80211smps(const char *val, int d, int s, const struct afswtch *rafp)
1816183261Ssam{
1817183261Ssam	set80211(s, IEEE80211_IOC_SMPS, d, 0, NULL);
1818183261Ssam}
1819183261Ssam
1820183261Ssamstatic void
1821183261Ssamset80211rifs(const char *val, int d, int s, const struct afswtch *rafp)
1822183261Ssam{
1823183261Ssam	set80211(s, IEEE80211_IOC_RIFS, d, 0, NULL);
1824183261Ssam}
1825183261Ssam
1826186904Ssamstatic
1827186904SsamDECL_CMD_FUNC(set80211tdmaslot, val, d)
1828186904Ssam{
1829186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOT, atoi(val), 0, NULL);
1830186904Ssam}
1831186904Ssam
1832186904Ssamstatic
1833186904SsamDECL_CMD_FUNC(set80211tdmaslotcnt, val, d)
1834186904Ssam{
1835186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOTCNT, atoi(val), 0, NULL);
1836186904Ssam}
1837186904Ssam
1838186904Ssamstatic
1839186904SsamDECL_CMD_FUNC(set80211tdmaslotlen, val, d)
1840186904Ssam{
1841186904Ssam	set80211(s, IEEE80211_IOC_TDMA_SLOTLEN, atoi(val), 0, NULL);
1842186904Ssam}
1843186904Ssam
1844186904Ssamstatic
1845186904SsamDECL_CMD_FUNC(set80211tdmabintval, val, d)
1846186904Ssam{
1847186904Ssam	set80211(s, IEEE80211_IOC_TDMA_BINTERVAL, atoi(val), 0, NULL);
1848186904Ssam}
1849186904Ssam
1850195618Srpaulostatic
1851195618SrpauloDECL_CMD_FUNC(set80211meshttl, val, d)
1852195618Srpaulo{
1853195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_TTL, atoi(val), 0, NULL);
1854195618Srpaulo}
1855195618Srpaulo
1856195618Srpaulostatic
1857195618SrpauloDECL_CMD_FUNC(set80211meshforward, val, d)
1858195618Srpaulo{
1859195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_FWRD, atoi(val), 0, NULL);
1860195618Srpaulo}
1861195618Srpaulo
1862195618Srpaulostatic
1863195618SrpauloDECL_CMD_FUNC(set80211meshpeering, val, d)
1864195618Srpaulo{
1865195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_AP, atoi(val), 0, NULL);
1866195618Srpaulo}
1867195618Srpaulo
1868195618Srpaulostatic
1869195618SrpauloDECL_CMD_FUNC(set80211meshmetric, val, d)
1870195618Srpaulo{
1871195618Srpaulo	char v[12];
1872195618Srpaulo
1873195618Srpaulo	memcpy(v, val, sizeof(v));
1874195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_PR_METRIC, 0, 0, v);
1875195618Srpaulo}
1876195618Srpaulo
1877195618Srpaulostatic
1878195618SrpauloDECL_CMD_FUNC(set80211meshpath, val, d)
1879195618Srpaulo{
1880195618Srpaulo	char v[12];
1881195618Srpaulo
1882195618Srpaulo	memcpy(v, val, sizeof(v));
1883195618Srpaulo	set80211(s, IEEE80211_IOC_MESH_PR_PATH, 0, 0, v);
1884195618Srpaulo}
1885195618Srpaulo
1886178354Ssamstatic int
1887178354Ssamregdomain_sort(const void *a, const void *b)
1888178354Ssam{
1889178354Ssam#define	CHAN_ALL \
1890178354Ssam	(IEEE80211_CHAN_ALLTURBO|IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)
1891178354Ssam	const struct ieee80211_channel *ca = a;
1892178354Ssam	const struct ieee80211_channel *cb = b;
1893178354Ssam
1894178354Ssam	return ca->ic_freq == cb->ic_freq ?
1895178354Ssam	    (ca->ic_flags & CHAN_ALL) - (cb->ic_flags & CHAN_ALL) :
1896178354Ssam	    ca->ic_freq - cb->ic_freq;
1897178354Ssam#undef CHAN_ALL
1898178354Ssam}
1899178354Ssam
1900178354Ssamstatic const struct ieee80211_channel *
1901178354Ssamchanlookup(const struct ieee80211_channel chans[], int nchans,
1902178354Ssam	int freq, int flags)
1903178354Ssam{
1904178354Ssam	int i;
1905178354Ssam
1906178354Ssam	flags &= IEEE80211_CHAN_ALLTURBO;
1907178354Ssam	for (i = 0; i < nchans; i++) {
1908178354Ssam		const struct ieee80211_channel *c = &chans[i];
1909178354Ssam		if (c->ic_freq == freq &&
1910178354Ssam		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1911178354Ssam			return c;
1912178354Ssam	}
1913178354Ssam	return NULL;
1914178354Ssam}
1915178354Ssam
1916187841Ssamstatic int
1917187841Ssamchanfind(const struct ieee80211_channel chans[], int nchans, int flags)
1918187841Ssam{
1919187841Ssam	int i;
1920187841Ssam
1921187841Ssam	for (i = 0; i < nchans; i++) {
1922187841Ssam		const struct ieee80211_channel *c = &chans[i];
1923187841Ssam		if ((c->ic_flags & flags) == flags)
1924187841Ssam			return 1;
1925187841Ssam	}
1926187841Ssam	return 0;
1927187841Ssam}
1928187841Ssam
1929188258Ssam/*
1930188258Ssam * Check channel compatibility.
1931188258Ssam */
1932188258Ssamstatic int
1933188258Ssamcheckchan(const struct ieee80211req_chaninfo *avail, int freq, int flags)
1934188258Ssam{
1935188258Ssam	flags &= ~REQ_FLAGS;
1936188258Ssam	/*
1937188258Ssam	 * Check if exact channel is in the calibration table;
1938188258Ssam	 * everything below is to deal with channels that we
1939188258Ssam	 * want to include but that are not explicitly listed.
1940188258Ssam	 */
1941188258Ssam	if (flags & IEEE80211_CHAN_HT40) {
1942188258Ssam		/* NB: we use an HT40 channel center that matches HT20 */
1943188258Ssam		flags = (flags &~ IEEE80211_CHAN_HT40) | IEEE80211_CHAN_HT20;
1944188258Ssam	}
1945188258Ssam	if (chanlookup(avail->ic_chans, avail->ic_nchans, freq, flags) != NULL)
1946188258Ssam		return 1;
1947188258Ssam	if (flags & IEEE80211_CHAN_GSM) {
1948188258Ssam		/*
1949188258Ssam		 * XXX GSM frequency mapping is handled in the kernel
1950188258Ssam		 * so we cannot find them in the calibration table;
1951188258Ssam		 * just accept the channel and the kernel will reject
1952188258Ssam		 * the channel list if it's wrong.
1953188258Ssam		 */
1954188258Ssam		return 1;
1955188258Ssam	}
1956188258Ssam	/*
1957188258Ssam	 * If this is a 1/2 or 1/4 width channel allow it if a full
1958188258Ssam	 * width channel is present for this frequency, and the device
1959188258Ssam	 * supports fractional channels on this band.  This is a hack
1960188258Ssam	 * that avoids bloating the calibration table; it may be better
1961188258Ssam	 * by per-band attributes though (we are effectively calculating
1962188258Ssam	 * this attribute by scanning the channel list ourself).
1963188258Ssam	 */
1964188258Ssam	if ((flags & (IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)) == 0)
1965188258Ssam		return 0;
1966188258Ssam	if (chanlookup(avail->ic_chans, avail->ic_nchans, freq,
1967188258Ssam	    flags &~ (IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)) == NULL)
1968188258Ssam		return 0;
1969188258Ssam	if (flags & IEEE80211_CHAN_HALF) {
1970188258Ssam		return chanfind(avail->ic_chans, avail->ic_nchans,
1971188258Ssam		    IEEE80211_CHAN_HALF |
1972188258Ssam		       (flags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ)));
1973188258Ssam	} else {
1974188258Ssam		return chanfind(avail->ic_chans, avail->ic_nchans,
1975188258Ssam		    IEEE80211_CHAN_QUARTER |
1976188258Ssam			(flags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ)));
1977188258Ssam	}
1978188258Ssam}
1979188258Ssam
1980178354Ssamstatic void
1981178354Ssamregdomain_addchans(struct ieee80211req_chaninfo *ci,
1982178354Ssam	const netband_head *bands,
1983178354Ssam	const struct ieee80211_regdomain *reg,
1984178354Ssam	uint32_t chanFlags,
1985178354Ssam	const struct ieee80211req_chaninfo *avail)
1986178354Ssam{
1987178354Ssam	const struct netband *nb;
1988178354Ssam	const struct freqband *b;
1989178354Ssam	struct ieee80211_channel *c, *prev;
1990188258Ssam	int freq, hi_adj, lo_adj, channelSep;
1991188258Ssam	uint32_t flags;
1992178354Ssam
1993188258Ssam	hi_adj = (chanFlags & IEEE80211_CHAN_HT40U) ? -20 : 0;
1994188258Ssam	lo_adj = (chanFlags & IEEE80211_CHAN_HT40D) ? 20 : 0;
1995178354Ssam	channelSep = (chanFlags & IEEE80211_CHAN_2GHZ) ? 0 : 40;
1996178354Ssam	LIST_FOREACH(nb, bands, next) {
1997178354Ssam		b = nb->band;
1998187842Ssam		if (verbose) {
1999187842Ssam			printf("%s:", __func__);
2000187842Ssam			printb(" chanFlags", chanFlags, IEEE80211_CHAN_BITS);
2001187842Ssam			printb(" bandFlags", nb->flags | b->flags,
2002187842Ssam			    IEEE80211_CHAN_BITS);
2003187842Ssam			putchar('\n');
2004187842Ssam		}
2005178354Ssam		prev = NULL;
2006188258Ssam		for (freq = b->freqStart + lo_adj;
2007188258Ssam		     freq <= b->freqEnd + hi_adj; freq += b->chanSep) {
2008187841Ssam			/*
2009188258Ssam			 * Construct flags for the new channel.  We take
2010188258Ssam			 * the attributes from the band descriptions except
2011188258Ssam			 * for HT40 which is enabled generically (i.e. +/-
2012188258Ssam			 * extension channel) in the band description and
2013188258Ssam			 * then constrained according by channel separation.
2014187841Ssam			 */
2015188258Ssam			flags = nb->flags | b->flags;
2016188258Ssam			if (flags & IEEE80211_CHAN_HT) {
2017188258Ssam				/*
2018188258Ssam				 * HT channels are generated specially; we're
2019188258Ssam				 * called to add HT20, HT40+, and HT40- chan's
2020188258Ssam				 * so we need to expand only band specs for
2021188258Ssam				 * the HT channel type being added.
2022188258Ssam				 */
2023188258Ssam				if ((chanFlags & IEEE80211_CHAN_HT20) &&
2024188258Ssam				    (flags & IEEE80211_CHAN_HT20) == 0) {
2025188258Ssam					if (verbose)
2026188258Ssam						printf("%u: skip, not an "
2027188258Ssam						    "HT20 channel\n", freq);
2028188258Ssam					continue;
2029188258Ssam				}
2030188258Ssam				if ((chanFlags & IEEE80211_CHAN_HT40) &&
2031188258Ssam				    (flags & IEEE80211_CHAN_HT40) == 0) {
2032188258Ssam					if (verbose)
2033188258Ssam						printf("%u: skip, not an "
2034188258Ssam						    "HT40 channel\n", freq);
2035188258Ssam					continue;
2036188258Ssam				}
2037188258Ssam				/*
2038188258Ssam				 * DFS and HT40 don't mix.  This should be
2039188258Ssam				 * expressed in the regdomain database but
2040188258Ssam				 * just in case enforce it here.
2041188258Ssam				 */
2042188258Ssam				if ((chanFlags & IEEE80211_CHAN_HT40) &&
2043188258Ssam				    (flags & IEEE80211_CHAN_DFS)) {
2044188258Ssam					if (verbose)
2045188258Ssam						printf("%u: skip, HT40+DFS "
2046188258Ssam						    "not permitted\n", freq);
2047188258Ssam					continue;
2048188258Ssam				}
2049188258Ssam				/* NB: HT attribute comes from caller */
2050188258Ssam				flags &= ~IEEE80211_CHAN_HT;
2051188258Ssam				flags |= chanFlags & IEEE80211_CHAN_HT;
2052188258Ssam			}
2053188258Ssam			/*
2054188258Ssam			 * Check if device can operate on this frequency.
2055188258Ssam			 */
2056188258Ssam			if (!checkchan(avail, freq, flags)) {
2057187842Ssam				if (verbose) {
2058187842Ssam					printf("%u: skip, ", freq);
2059188258Ssam					printb("flags", flags,
2060187842Ssam					    IEEE80211_CHAN_BITS);
2061187842Ssam					printf(" not available\n");
2062187842Ssam				}
2063178354Ssam				continue;
2064178354Ssam			}
2065188258Ssam			if ((flags & REQ_ECM) && !reg->ecm) {
2066178354Ssam				if (verbose)
2067188258Ssam					printf("%u: skip, ECM channel\n", freq);
2068178354Ssam				continue;
2069178354Ssam			}
2070188258Ssam			if ((flags & REQ_INDOOR) && reg->location == 'O') {
2071178354Ssam				if (verbose)
2072188258Ssam					printf("%u: skip, indoor channel\n",
2073187841Ssam					    freq);
2074178354Ssam				continue;
2075178354Ssam			}
2076178354Ssam			if ((flags & REQ_OUTDOOR) && reg->location == 'I') {
2077178354Ssam				if (verbose)
2078188258Ssam					printf("%u: skip, outdoor channel\n",
2079188258Ssam					    freq);
2080178354Ssam				continue;
2081178354Ssam			}
2082178354Ssam			if ((flags & IEEE80211_CHAN_HT40) &&
2083178354Ssam			    prev != NULL && (freq - prev->ic_freq) < channelSep) {
2084178354Ssam				if (verbose)
2085178354Ssam					printf("%u: skip, only %u channel "
2086178354Ssam					    "separation, need %d\n", freq,
2087178354Ssam					    freq - prev->ic_freq, channelSep);
2088178354Ssam				continue;
2089178354Ssam			}
2090178354Ssam			if (ci->ic_nchans == IEEE80211_CHAN_MAX) {
2091178354Ssam				if (verbose)
2092187842Ssam					printf("%u: skip, channel table full\n",
2093187842Ssam					    freq);
2094178354Ssam				break;
2095178354Ssam			}
2096178354Ssam			c = &ci->ic_chans[ci->ic_nchans++];
2097187801Ssam			memset(c, 0, sizeof(*c));
2098178354Ssam			c->ic_freq = freq;
2099188258Ssam			c->ic_flags = flags;
2100178354Ssam			if (c->ic_flags & IEEE80211_CHAN_DFS)
2101178354Ssam				c->ic_maxregpower = nb->maxPowerDFS;
2102178354Ssam			else
2103178354Ssam				c->ic_maxregpower = nb->maxPower;
2104187842Ssam			if (verbose) {
2105187842Ssam				printf("[%3d] add freq %u ",
2106187842Ssam				    ci->ic_nchans-1, c->ic_freq);
2107187842Ssam				printb("flags", c->ic_flags, IEEE80211_CHAN_BITS);
2108187842Ssam				printf(" power %u\n", c->ic_maxregpower);
2109187842Ssam			}
2110178354Ssam			/* NB: kernel fills in other fields */
2111178354Ssam			prev = c;
2112178354Ssam		}
2113178354Ssam	}
2114178354Ssam}
2115178354Ssam
2116178354Ssamstatic void
2117178354Ssamregdomain_makechannels(
2118178354Ssam	struct ieee80211_regdomain_req *req,
2119178354Ssam	const struct ieee80211_devcaps_req *dc)
2120178354Ssam{
2121178354Ssam	struct regdata *rdp = getregdata();
2122178354Ssam	const struct country *cc;
2123178354Ssam	const struct ieee80211_regdomain *reg = &req->rd;
2124178354Ssam	struct ieee80211req_chaninfo *ci = &req->chaninfo;
2125178354Ssam	const struct regdomain *rd;
2126178354Ssam
2127178354Ssam	/*
2128178354Ssam	 * Locate construction table for new channel list.  We treat
2129178354Ssam	 * the regdomain/SKU as definitive so a country can be in
2130178354Ssam	 * multiple with different properties (e.g. US in FCC+FCC3).
2131178354Ssam	 * If no regdomain is specified then we fallback on the country
2132178354Ssam	 * code to find the associated regdomain since countries always
2133178354Ssam	 * belong to at least one regdomain.
2134178354Ssam	 */
2135178354Ssam	if (reg->regdomain == 0) {
2136178354Ssam		cc = lib80211_country_findbycc(rdp, reg->country);
2137178354Ssam		if (cc == NULL)
2138178354Ssam			errx(1, "internal error, country %d not found",
2139178354Ssam			    reg->country);
2140178354Ssam		rd = cc->rd;
2141178354Ssam	} else
2142178354Ssam		rd = lib80211_regdomain_findbysku(rdp, reg->regdomain);
2143178354Ssam	if (rd == NULL)
2144178354Ssam		errx(1, "internal error, regdomain %d not found",
2145178354Ssam			    reg->regdomain);
2146178354Ssam	if (rd->sku != SKU_DEBUG) {
2147187801Ssam		/*
2148187801Ssam		 * regdomain_addchans incrememnts the channel count for
2149187801Ssam		 * each channel it adds so initialize ic_nchans to zero.
2150187801Ssam		 * Note that we know we have enough space to hold all possible
2151187801Ssam		 * channels because the devcaps list size was used to
2152187801Ssam		 * allocate our request.
2153187801Ssam		 */
2154187801Ssam		ci->ic_nchans = 0;
2155178354Ssam		if (!LIST_EMPTY(&rd->bands_11b))
2156178354Ssam			regdomain_addchans(ci, &rd->bands_11b, reg,
2157178354Ssam			    IEEE80211_CHAN_B, &dc->dc_chaninfo);
2158187841Ssam		if (!LIST_EMPTY(&rd->bands_11g))
2159178354Ssam			regdomain_addchans(ci, &rd->bands_11g, reg,
2160178354Ssam			    IEEE80211_CHAN_G, &dc->dc_chaninfo);
2161187841Ssam		if (!LIST_EMPTY(&rd->bands_11a))
2162178354Ssam			regdomain_addchans(ci, &rd->bands_11a, reg,
2163178354Ssam			    IEEE80211_CHAN_A, &dc->dc_chaninfo);
2164188258Ssam		if (!LIST_EMPTY(&rd->bands_11na) && dc->dc_htcaps != 0) {
2165178354Ssam			regdomain_addchans(ci, &rd->bands_11na, reg,
2166178354Ssam			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT20,
2167178354Ssam			    &dc->dc_chaninfo);
2168188258Ssam			if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2169188258Ssam				regdomain_addchans(ci, &rd->bands_11na, reg,
2170188258Ssam				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U,
2171188258Ssam				    &dc->dc_chaninfo);
2172188258Ssam				regdomain_addchans(ci, &rd->bands_11na, reg,
2173188258Ssam				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D,
2174188258Ssam				    &dc->dc_chaninfo);
2175188258Ssam			}
2176178354Ssam		}
2177188258Ssam		if (!LIST_EMPTY(&rd->bands_11ng) && dc->dc_htcaps != 0) {
2178178354Ssam			regdomain_addchans(ci, &rd->bands_11ng, reg,
2179178354Ssam			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT20,
2180178354Ssam			    &dc->dc_chaninfo);
2181188258Ssam			if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2182188258Ssam				regdomain_addchans(ci, &rd->bands_11ng, reg,
2183188258Ssam				    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U,
2184188258Ssam				    &dc->dc_chaninfo);
2185188258Ssam				regdomain_addchans(ci, &rd->bands_11ng, reg,
2186188258Ssam				    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D,
2187188258Ssam				    &dc->dc_chaninfo);
2188188258Ssam			}
2189178354Ssam		}
2190178354Ssam		qsort(ci->ic_chans, ci->ic_nchans, sizeof(ci->ic_chans[0]),
2191178354Ssam		    regdomain_sort);
2192178354Ssam	} else
2193187801Ssam		memcpy(ci, &dc->dc_chaninfo,
2194187801Ssam		    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
2195178354Ssam}
2196178354Ssam
2197178354Ssamstatic void
2198178354Ssamlist_countries(void)
2199178354Ssam{
2200178354Ssam	struct regdata *rdp = getregdata();
2201178354Ssam	const struct country *cp;
2202178354Ssam	const struct regdomain *dp;
2203178354Ssam	int i;
2204178354Ssam
2205178354Ssam	i = 0;
2206178354Ssam	printf("\nCountry codes:\n");
2207178354Ssam	LIST_FOREACH(cp, &rdp->countries, next) {
2208178354Ssam		printf("%2s %-15.15s%s", cp->isoname,
2209178354Ssam		    cp->name, ((i+1)%4) == 0 ? "\n" : " ");
2210178354Ssam		i++;
2211178354Ssam	}
2212178354Ssam	i = 0;
2213178354Ssam	printf("\nRegulatory domains:\n");
2214178354Ssam	LIST_FOREACH(dp, &rdp->domains, next) {
2215178354Ssam		printf("%-15.15s%s", dp->name, ((i+1)%4) == 0 ? "\n" : " ");
2216178354Ssam		i++;
2217178354Ssam	}
2218178354Ssam	printf("\n");
2219178354Ssam}
2220178354Ssam
2221178354Ssamstatic void
2222178354Ssamdefaultcountry(const struct regdomain *rd)
2223178354Ssam{
2224178354Ssam	struct regdata *rdp = getregdata();
2225178354Ssam	const struct country *cc;
2226178354Ssam
2227178354Ssam	cc = lib80211_country_findbycc(rdp, rd->cc->code);
2228178354Ssam	if (cc == NULL)
2229178354Ssam		errx(1, "internal error, ISO country code %d not "
2230178354Ssam		    "defined for regdomain %s", rd->cc->code, rd->name);
2231178354Ssam	regdomain.country = cc->code;
2232178354Ssam	regdomain.isocc[0] = cc->isoname[0];
2233178354Ssam	regdomain.isocc[1] = cc->isoname[1];
2234178354Ssam}
2235178354Ssam
2236178354Ssamstatic
2237178354SsamDECL_CMD_FUNC(set80211regdomain, val, d)
2238178354Ssam{
2239178354Ssam	struct regdata *rdp = getregdata();
2240178354Ssam	const struct regdomain *rd;
2241178354Ssam
2242178354Ssam	rd = lib80211_regdomain_findbyname(rdp, val);
2243178354Ssam	if (rd == NULL) {
2244186104Ssam		char *eptr;
2245186104Ssam		long sku = strtol(val, &eptr, 0);
2246186104Ssam
2247186104Ssam		if (eptr != val)
2248186104Ssam			rd = lib80211_regdomain_findbysku(rdp, sku);
2249186104Ssam		if (eptr == val || rd == NULL)
2250178354Ssam			errx(1, "unknown regdomain %s", val);
2251178354Ssam	}
2252178354Ssam	getregdomain(s);
2253178354Ssam	regdomain.regdomain = rd->sku;
2254178354Ssam	if (regdomain.country == 0 && rd->cc != NULL) {
2255178354Ssam		/*
2256178354Ssam		 * No country code setup and there's a default
2257178354Ssam		 * one for this regdomain fill it in.
2258178354Ssam		 */
2259178354Ssam		defaultcountry(rd);
2260178354Ssam	}
2261178354Ssam	callback_register(setregdomain_cb, &regdomain);
2262178354Ssam}
2263178354Ssam
2264178354Ssamstatic
2265178354SsamDECL_CMD_FUNC(set80211country, val, d)
2266178354Ssam{
2267178354Ssam	struct regdata *rdp = getregdata();
2268178354Ssam	const struct country *cc;
2269178354Ssam
2270178354Ssam	cc = lib80211_country_findbyname(rdp, val);
2271178354Ssam	if (cc == NULL) {
2272186103Ssam		char *eptr;
2273186103Ssam		long code = strtol(val, &eptr, 0);
2274186103Ssam
2275186103Ssam		if (eptr != val)
2276186103Ssam			cc = lib80211_country_findbycc(rdp, code);
2277186103Ssam		if (eptr == val || cc == NULL)
2278178354Ssam			errx(1, "unknown ISO country code %s", val);
2279178354Ssam	}
2280178354Ssam	getregdomain(s);
2281178354Ssam	regdomain.regdomain = cc->rd->sku;
2282178354Ssam	regdomain.country = cc->code;
2283178354Ssam	regdomain.isocc[0] = cc->isoname[0];
2284178354Ssam	regdomain.isocc[1] = cc->isoname[1];
2285178354Ssam	callback_register(setregdomain_cb, &regdomain);
2286178354Ssam}
2287178354Ssam
2288178354Ssamstatic void
2289178354Ssamset80211location(const char *val, int d, int s, const struct afswtch *rafp)
2290178354Ssam{
2291178354Ssam	getregdomain(s);
2292178354Ssam	regdomain.location = d;
2293178354Ssam	callback_register(setregdomain_cb, &regdomain);
2294178354Ssam}
2295178354Ssam
2296178354Ssamstatic void
2297178354Ssamset80211ecm(const char *val, int d, int s, const struct afswtch *rafp)
2298178354Ssam{
2299178354Ssam	getregdomain(s);
2300178354Ssam	regdomain.ecm = d;
2301178354Ssam	callback_register(setregdomain_cb, &regdomain);
2302178354Ssam}
2303178354Ssam
2304178354Ssamstatic void
2305173275SsamLINE_INIT(char c)
2306173275Ssam{
2307173275Ssam	spacer = c;
2308173275Ssam	if (c == '\t')
2309173275Ssam		col = 8;
2310173275Ssam	else
2311173275Ssam		col = 1;
2312173275Ssam}
2313173275Ssam
2314173275Ssamstatic void
2315173275SsamLINE_BREAK(void)
2316173275Ssam{
2317173275Ssam	if (spacer != '\t') {
2318173275Ssam		printf("\n");
2319173275Ssam		spacer = '\t';
2320173275Ssam	}
2321173275Ssam	col = 8;		/* 8-col tab */
2322173275Ssam}
2323173275Ssam
2324173275Ssamstatic void
2325173275SsamLINE_CHECK(const char *fmt, ...)
2326173275Ssam{
2327173275Ssam	char buf[80];
2328173275Ssam	va_list ap;
2329173275Ssam	int n;
2330173275Ssam
2331173275Ssam	va_start(ap, fmt);
2332173275Ssam	n = vsnprintf(buf+1, sizeof(buf)-1, fmt, ap);
2333173275Ssam	va_end(ap);
2334173275Ssam	col += 1+n;
2335173275Ssam	if (col > MAXCOL) {
2336173275Ssam		LINE_BREAK();
2337173275Ssam		col += n;
2338173275Ssam	}
2339173275Ssam	buf[0] = spacer;
2340173275Ssam	printf("%s", buf);
2341173275Ssam	spacer = ' ';
2342173275Ssam}
2343173275Ssam
2344138593Ssamstatic int
2345170531Ssamgetmaxrate(const uint8_t rates[15], uint8_t nrates)
2346138593Ssam{
2347138593Ssam	int i, maxrate = -1;
2348138593Ssam
2349138593Ssam	for (i = 0; i < nrates; i++) {
2350138593Ssam		int rate = rates[i] & IEEE80211_RATE_VAL;
2351138593Ssam		if (rate > maxrate)
2352138593Ssam			maxrate = rate;
2353138593Ssam	}
2354138593Ssam	return maxrate / 2;
2355138593Ssam}
2356138593Ssam
2357138593Ssamstatic const char *
2358138593Ssamgetcaps(int capinfo)
2359138593Ssam{
2360138593Ssam	static char capstring[32];
2361138593Ssam	char *cp = capstring;
2362138593Ssam
2363138593Ssam	if (capinfo & IEEE80211_CAPINFO_ESS)
2364138593Ssam		*cp++ = 'E';
2365138593Ssam	if (capinfo & IEEE80211_CAPINFO_IBSS)
2366138593Ssam		*cp++ = 'I';
2367138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
2368138593Ssam		*cp++ = 'c';
2369138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
2370138593Ssam		*cp++ = 'C';
2371138593Ssam	if (capinfo & IEEE80211_CAPINFO_PRIVACY)
2372138593Ssam		*cp++ = 'P';
2373138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
2374138593Ssam		*cp++ = 'S';
2375138593Ssam	if (capinfo & IEEE80211_CAPINFO_PBCC)
2376138593Ssam		*cp++ = 'B';
2377138593Ssam	if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
2378138593Ssam		*cp++ = 'A';
2379138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
2380138593Ssam		*cp++ = 's';
2381138593Ssam	if (capinfo & IEEE80211_CAPINFO_RSN)
2382138593Ssam		*cp++ = 'R';
2383138593Ssam	if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
2384138593Ssam		*cp++ = 'D';
2385138593Ssam	*cp = '\0';
2386138593Ssam	return capstring;
2387138593Ssam}
2388138593Ssam
2389159885Ssamstatic const char *
2390159885Ssamgetflags(int flags)
2391159885Ssam{
2392159885Ssam	static char flagstring[32];
2393159885Ssam	char *cp = flagstring;
2394159885Ssam
2395159885Ssam	if (flags & IEEE80211_NODE_AUTH)
2396159885Ssam		*cp++ = 'A';
2397159885Ssam	if (flags & IEEE80211_NODE_QOS)
2398159885Ssam		*cp++ = 'Q';
2399159885Ssam	if (flags & IEEE80211_NODE_ERP)
2400159885Ssam		*cp++ = 'E';
2401159885Ssam	if (flags & IEEE80211_NODE_PWR_MGT)
2402159885Ssam		*cp++ = 'P';
2403173275Ssam	if (flags & IEEE80211_NODE_HT) {
2404170531Ssam		*cp++ = 'H';
2405173275Ssam		if (flags & IEEE80211_NODE_HTCOMPAT)
2406173275Ssam			*cp++ = '+';
2407173275Ssam	}
2408173275Ssam	if (flags & IEEE80211_NODE_WPS)
2409173275Ssam		*cp++ = 'W';
2410173275Ssam	if (flags & IEEE80211_NODE_TSN)
2411183261Ssam		*cp++ = 'N';
2412183261Ssam	if (flags & IEEE80211_NODE_AMPDU_TX)
2413173275Ssam		*cp++ = 'T';
2414183261Ssam	if (flags & IEEE80211_NODE_AMPDU_RX)
2415183261Ssam		*cp++ = 'R';
2416183261Ssam	if (flags & IEEE80211_NODE_MIMO_PS) {
2417183261Ssam		*cp++ = 'M';
2418183261Ssam		if (flags & IEEE80211_NODE_MIMO_RTS)
2419183261Ssam			*cp++ = '+';
2420183261Ssam	}
2421183261Ssam	if (flags & IEEE80211_NODE_RIFS)
2422183261Ssam		*cp++ = 'I';
2423194136Ssam	if (flags & IEEE80211_NODE_SGI40) {
2424194136Ssam		*cp++ = 'S';
2425194136Ssam		if (flags & IEEE80211_NODE_SGI20)
2426194136Ssam			*cp++ = '+';
2427194136Ssam	} else if (flags & IEEE80211_NODE_SGI20)
2428194136Ssam		*cp++ = 's';
2429194136Ssam	if (flags & IEEE80211_NODE_AMSDU_TX)
2430194136Ssam		*cp++ = 't';
2431194136Ssam	if (flags & IEEE80211_NODE_AMSDU_RX)
2432194136Ssam		*cp++ = 'r';
2433159885Ssam	*cp = '\0';
2434159885Ssam	return flagstring;
2435159885Ssam}
2436159885Ssam
2437138593Ssamstatic void
2438138593Ssamprintie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
2439138593Ssam{
2440138593Ssam	printf("%s", tag);
2441138593Ssam	if (verbose) {
2442138593Ssam		maxlen -= strlen(tag)+2;
2443138593Ssam		if (2*ielen > maxlen)
2444138593Ssam			maxlen--;
2445138593Ssam		printf("<");
2446138593Ssam		for (; ielen > 0; ie++, ielen--) {
2447138593Ssam			if (maxlen-- <= 0)
2448138593Ssam				break;
2449138593Ssam			printf("%02x", *ie);
2450138593Ssam		}
2451138593Ssam		if (ielen != 0)
2452138593Ssam			printf("-");
2453138593Ssam		printf(">");
2454138593Ssam	}
2455138593Ssam}
2456138593Ssam
2457170531Ssam#define LE_READ_2(p)					\
2458170531Ssam	((u_int16_t)					\
2459170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2460170531Ssam	  (((const u_int8_t *)(p))[1] <<  8)))
2461170531Ssam#define LE_READ_4(p)					\
2462170531Ssam	((u_int32_t)					\
2463170531Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
2464170531Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
2465170531Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
2466170531Ssam	  (((const u_int8_t *)(p))[3] << 24)))
2467170531Ssam
2468138593Ssam/*
2469170531Ssam * NB: The decoding routines assume a properly formatted ie
2470170531Ssam *     which should be safe as the kernel only retains them
2471170531Ssam *     if they parse ok.
2472170531Ssam */
2473170531Ssam
2474170531Ssamstatic void
2475173275Ssamprintwmeparam(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2476170531Ssam{
2477170531Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
2478170531Ssam	static const char *acnames[] = { "BE", "BK", "VO", "VI" };
2479173275Ssam	const struct ieee80211_wme_param *wme =
2480173275Ssam	    (const struct ieee80211_wme_param *) ie;
2481170531Ssam	int i;
2482170531Ssam
2483170531Ssam	printf("%s", tag);
2484173275Ssam	if (!verbose)
2485173275Ssam		return;
2486173275Ssam	printf("<qosinfo 0x%x", wme->param_qosInfo);
2487173275Ssam	ie += offsetof(struct ieee80211_wme_param, params_acParams);
2488173275Ssam	for (i = 0; i < WME_NUM_AC; i++) {
2489173275Ssam		const struct ieee80211_wme_acparams *ac =
2490173275Ssam		    &wme->params_acParams[i];
2491173275Ssam
2492173275Ssam		printf(" %s[%saifsn %u cwmin %u cwmax %u txop %u]"
2493173275Ssam			, acnames[i]
2494173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_ACM) ? "acm " : ""
2495173275Ssam			, MS(ac->acp_aci_aifsn, WME_PARAM_AIFSN)
2496173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMIN)
2497173275Ssam			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMAX)
2498173275Ssam			, LE_READ_2(&ac->acp_txop)
2499173275Ssam		);
2500170531Ssam	}
2501173275Ssam	printf(">");
2502170531Ssam#undef MS
2503170531Ssam}
2504170531Ssam
2505170531Ssamstatic void
2506173275Ssamprintwmeinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2507173275Ssam{
2508173275Ssam	printf("%s", tag);
2509173275Ssam	if (verbose) {
2510173275Ssam		const struct ieee80211_wme_info *wme =
2511173275Ssam		    (const struct ieee80211_wme_info *) ie;
2512173275Ssam		printf("<version 0x%x info 0x%x>",
2513173275Ssam		    wme->wme_version, wme->wme_info);
2514173275Ssam	}
2515173275Ssam}
2516173275Ssam
2517173275Ssamstatic void
2518173275Ssamprinthtcap(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2519173275Ssam{
2520173275Ssam	printf("%s", tag);
2521173275Ssam	if (verbose) {
2522173275Ssam		const struct ieee80211_ie_htcap *htcap =
2523173275Ssam		    (const struct ieee80211_ie_htcap *) ie;
2524173275Ssam		const char *sep;
2525173275Ssam		int i, j;
2526173275Ssam
2527173275Ssam		printf("<cap 0x%x param 0x%x",
2528173275Ssam		    LE_READ_2(&htcap->hc_cap), htcap->hc_param);
2529173275Ssam		printf(" mcsset[");
2530173275Ssam		sep = "";
2531173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2532173275Ssam			if (isset(htcap->hc_mcsset, i)) {
2533173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2534173275Ssam					if (isclr(htcap->hc_mcsset, j))
2535173275Ssam						break;
2536173275Ssam				j--;
2537173275Ssam				if (i == j)
2538173275Ssam					printf("%s%u", sep, i);
2539173275Ssam				else
2540173275Ssam					printf("%s%u-%u", sep, i, j);
2541173275Ssam				i += j-i;
2542173275Ssam				sep = ",";
2543173275Ssam			}
2544173275Ssam		printf("] extcap 0x%x txbf 0x%x antenna 0x%x>",
2545173275Ssam		    LE_READ_2(&htcap->hc_extcap),
2546173275Ssam		    LE_READ_4(&htcap->hc_txbf),
2547173275Ssam		    htcap->hc_antenna);
2548173275Ssam	}
2549173275Ssam}
2550173275Ssam
2551173275Ssamstatic void
2552173275Ssamprinthtinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2553173275Ssam{
2554173275Ssam	printf("%s", tag);
2555173275Ssam	if (verbose) {
2556173275Ssam		const struct ieee80211_ie_htinfo *htinfo =
2557173275Ssam		    (const struct ieee80211_ie_htinfo *) ie;
2558173275Ssam		const char *sep;
2559173275Ssam		int i, j;
2560173275Ssam
2561173275Ssam		printf("<ctl %u, %x,%x,%x,%x", htinfo->hi_ctrlchannel,
2562173275Ssam		    htinfo->hi_byte1, htinfo->hi_byte2, htinfo->hi_byte3,
2563173275Ssam		    LE_READ_2(&htinfo->hi_byte45));
2564173275Ssam		printf(" basicmcs[");
2565173275Ssam		sep = "";
2566173275Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2567173275Ssam			if (isset(htinfo->hi_basicmcsset, i)) {
2568173275Ssam				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2569173275Ssam					if (isclr(htinfo->hi_basicmcsset, j))
2570173275Ssam						break;
2571173275Ssam				j--;
2572173275Ssam				if (i == j)
2573173275Ssam					printf("%s%u", sep, i);
2574173275Ssam				else
2575173275Ssam					printf("%s%u-%u", sep, i, j);
2576173275Ssam				i += j-i;
2577173275Ssam				sep = ",";
2578173275Ssam			}
2579173275Ssam		printf("]>");
2580173275Ssam	}
2581173275Ssam}
2582173275Ssam
2583173275Ssamstatic void
2584170531Ssamprintathie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2585170531Ssam{
2586170531Ssam
2587170531Ssam	printf("%s", tag);
2588170531Ssam	if (verbose) {
2589170531Ssam		const struct ieee80211_ath_ie *ath =
2590170531Ssam			(const struct ieee80211_ath_ie *)ie;
2591170531Ssam
2592170531Ssam		printf("<");
2593170531Ssam		if (ath->ath_capability & ATHEROS_CAP_TURBO_PRIME)
2594170531Ssam			printf("DTURBO,");
2595170531Ssam		if (ath->ath_capability & ATHEROS_CAP_COMPRESSION)
2596170531Ssam			printf("COMP,");
2597170531Ssam		if (ath->ath_capability & ATHEROS_CAP_FAST_FRAME)
2598170531Ssam			printf("FF,");
2599170531Ssam		if (ath->ath_capability & ATHEROS_CAP_XR)
2600170531Ssam			printf("XR,");
2601170531Ssam		if (ath->ath_capability & ATHEROS_CAP_AR)
2602170531Ssam			printf("AR,");
2603170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BURST)
2604170531Ssam			printf("BURST,");
2605170531Ssam		if (ath->ath_capability & ATHEROS_CAP_WME)
2606170531Ssam			printf("WME,");
2607170531Ssam		if (ath->ath_capability & ATHEROS_CAP_BOOST)
2608170531Ssam			printf("BOOST,");
2609170531Ssam		printf("0x%x>", LE_READ_2(ath->ath_defkeyix));
2610170531Ssam	}
2611170531Ssam}
2612170531Ssam
2613195618Srpaulo
2614195618Srpaulostatic void
2615195618Srpauloprintmeshconf(const char *tag, const uint8_t *ie, size_t ielen, int maxlen)
2616195618Srpaulo{
2617195618Srpaulo#define MATCHOUI(field, oui, string)					\
2618195618Srpaulodo {									\
2619195618Srpaulo	if (memcmp(field, oui, 4) == 0)					\
2620195618Srpaulo		printf("%s", string);					\
2621195618Srpaulo} while (0)
2622195618Srpaulo
2623195618Srpaulo	printf("%s", tag);
2624195618Srpaulo	if (verbose) {
2625195618Srpaulo		const struct ieee80211_meshconf_ie *mconf =
2626195618Srpaulo			(const struct ieee80211_meshconf_ie *)ie;
2627197980Srpaulo		printf("<PATH:");
2628197980Srpaulo		if (mconf->conf_pselid == IEEE80211_MESHCONF_PATH_HWMP)
2629197980Srpaulo			printf("HWMP");
2630197980Srpaulo		else
2631197980Srpaulo			printf("UNKNOWN");
2632195618Srpaulo		printf(" LINK:");
2633197980Srpaulo		if (mconf->conf_pmetid == IEEE80211_MESHCONF_METRIC_AIRTIME)
2634197980Srpaulo			printf("AIRTIME");
2635197980Srpaulo		else
2636197980Srpaulo			printf("UNKNOWN");
2637195618Srpaulo		printf(" CONGESTION:");
2638197980Srpaulo		if (mconf->conf_ccid == IEEE80211_MESHCONF_CC_DISABLED)
2639197980Srpaulo			printf("DISABLED");
2640197980Srpaulo		else
2641197980Srpaulo			printf("UNKNOWN");
2642195618Srpaulo		printf(" SYNC:");
2643197980Srpaulo		if (mconf->conf_syncid == IEEE80211_MESHCONF_SYNC_NEIGHOFF)
2644197980Srpaulo			printf("NEIGHOFF");
2645197980Srpaulo		else
2646197980Srpaulo			printf("UNKNOWN");
2647195618Srpaulo		printf(" AUTH:");
2648197980Srpaulo		if (mconf->conf_authid == IEEE80211_MESHCONF_AUTH_DISABLED)
2649197980Srpaulo			printf("DISABLED");
2650197980Srpaulo		else
2651197980Srpaulo			printf("UNKNOWN");
2652195618Srpaulo		printf(" FORM:0x%x CAPS:0x%x>", mconf->conf_form,
2653195618Srpaulo		    mconf->conf_cap);
2654195618Srpaulo	}
2655195618Srpaulo#undef MATCHOUI
2656195618Srpaulo}
2657195618Srpaulo
2658170531Ssamstatic const char *
2659170531Ssamwpa_cipher(const u_int8_t *sel)
2660170531Ssam{
2661170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2662170531Ssam	u_int32_t w = LE_READ_4(sel);
2663170531Ssam
2664170531Ssam	switch (w) {
2665170531Ssam	case WPA_SEL(WPA_CSE_NULL):
2666170531Ssam		return "NONE";
2667170531Ssam	case WPA_SEL(WPA_CSE_WEP40):
2668170531Ssam		return "WEP40";
2669170531Ssam	case WPA_SEL(WPA_CSE_WEP104):
2670170531Ssam		return "WEP104";
2671170531Ssam	case WPA_SEL(WPA_CSE_TKIP):
2672170531Ssam		return "TKIP";
2673170531Ssam	case WPA_SEL(WPA_CSE_CCMP):
2674170531Ssam		return "AES-CCMP";
2675170531Ssam	}
2676170531Ssam	return "?";		/* NB: so 1<< is discarded */
2677170531Ssam#undef WPA_SEL
2678170531Ssam}
2679170531Ssam
2680170531Ssamstatic const char *
2681170531Ssamwpa_keymgmt(const u_int8_t *sel)
2682170531Ssam{
2683170531Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2684170531Ssam	u_int32_t w = LE_READ_4(sel);
2685170531Ssam
2686170531Ssam	switch (w) {
2687170531Ssam	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
2688170531Ssam		return "8021X-UNSPEC";
2689170531Ssam	case WPA_SEL(WPA_ASE_8021X_PSK):
2690170531Ssam		return "8021X-PSK";
2691170531Ssam	case WPA_SEL(WPA_ASE_NONE):
2692170531Ssam		return "NONE";
2693170531Ssam	}
2694170531Ssam	return "?";
2695170531Ssam#undef WPA_SEL
2696170531Ssam}
2697170531Ssam
2698170531Ssamstatic void
2699170531Ssamprintwpaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2700170531Ssam{
2701170531Ssam	u_int8_t len = ie[1];
2702170531Ssam
2703170531Ssam	printf("%s", tag);
2704170531Ssam	if (verbose) {
2705170531Ssam		const char *sep;
2706170531Ssam		int n;
2707170531Ssam
2708170531Ssam		ie += 6, len -= 4;		/* NB: len is payload only */
2709170531Ssam
2710170531Ssam		printf("<v%u", LE_READ_2(ie));
2711170531Ssam		ie += 2, len -= 2;
2712170531Ssam
2713170531Ssam		printf(" mc:%s", wpa_cipher(ie));
2714170531Ssam		ie += 4, len -= 4;
2715170531Ssam
2716170531Ssam		/* unicast ciphers */
2717170531Ssam		n = LE_READ_2(ie);
2718170531Ssam		ie += 2, len -= 2;
2719170531Ssam		sep = " uc:";
2720170531Ssam		for (; n > 0; n--) {
2721170531Ssam			printf("%s%s", sep, wpa_cipher(ie));
2722170531Ssam			ie += 4, len -= 4;
2723170531Ssam			sep = "+";
2724170531Ssam		}
2725170531Ssam
2726170531Ssam		/* key management algorithms */
2727170531Ssam		n = LE_READ_2(ie);
2728170531Ssam		ie += 2, len -= 2;
2729170531Ssam		sep = " km:";
2730170531Ssam		for (; n > 0; n--) {
2731170531Ssam			printf("%s%s", sep, wpa_keymgmt(ie));
2732170531Ssam			ie += 4, len -= 4;
2733170531Ssam			sep = "+";
2734170531Ssam		}
2735170531Ssam
2736170531Ssam		if (len > 2)		/* optional capabilities */
2737170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2738170531Ssam		printf(">");
2739170531Ssam	}
2740170531Ssam}
2741170531Ssam
2742170531Ssamstatic const char *
2743170531Ssamrsn_cipher(const u_int8_t *sel)
2744170531Ssam{
2745170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2746170531Ssam	u_int32_t w = LE_READ_4(sel);
2747170531Ssam
2748170531Ssam	switch (w) {
2749170531Ssam	case RSN_SEL(RSN_CSE_NULL):
2750170531Ssam		return "NONE";
2751170531Ssam	case RSN_SEL(RSN_CSE_WEP40):
2752170531Ssam		return "WEP40";
2753170531Ssam	case RSN_SEL(RSN_CSE_WEP104):
2754170531Ssam		return "WEP104";
2755170531Ssam	case RSN_SEL(RSN_CSE_TKIP):
2756170531Ssam		return "TKIP";
2757170531Ssam	case RSN_SEL(RSN_CSE_CCMP):
2758170531Ssam		return "AES-CCMP";
2759170531Ssam	case RSN_SEL(RSN_CSE_WRAP):
2760170531Ssam		return "AES-OCB";
2761170531Ssam	}
2762170531Ssam	return "?";
2763170531Ssam#undef WPA_SEL
2764170531Ssam}
2765170531Ssam
2766170531Ssamstatic const char *
2767170531Ssamrsn_keymgmt(const u_int8_t *sel)
2768170531Ssam{
2769170531Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2770170531Ssam	u_int32_t w = LE_READ_4(sel);
2771170531Ssam
2772170531Ssam	switch (w) {
2773170531Ssam	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
2774170531Ssam		return "8021X-UNSPEC";
2775170531Ssam	case RSN_SEL(RSN_ASE_8021X_PSK):
2776170531Ssam		return "8021X-PSK";
2777170531Ssam	case RSN_SEL(RSN_ASE_NONE):
2778170531Ssam		return "NONE";
2779170531Ssam	}
2780170531Ssam	return "?";
2781170531Ssam#undef RSN_SEL
2782170531Ssam}
2783170531Ssam
2784170531Ssamstatic void
2785170531Ssamprintrsnie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2786170531Ssam{
2787170531Ssam	printf("%s", tag);
2788170531Ssam	if (verbose) {
2789170531Ssam		const char *sep;
2790170531Ssam		int n;
2791170531Ssam
2792173275Ssam		ie += 2, ielen -= 2;
2793170531Ssam
2794170531Ssam		printf("<v%u", LE_READ_2(ie));
2795173275Ssam		ie += 2, ielen -= 2;
2796170531Ssam
2797170531Ssam		printf(" mc:%s", rsn_cipher(ie));
2798173275Ssam		ie += 4, ielen -= 4;
2799170531Ssam
2800170531Ssam		/* unicast ciphers */
2801170531Ssam		n = LE_READ_2(ie);
2802173275Ssam		ie += 2, ielen -= 2;
2803170531Ssam		sep = " uc:";
2804170531Ssam		for (; n > 0; n--) {
2805170531Ssam			printf("%s%s", sep, rsn_cipher(ie));
2806173275Ssam			ie += 4, ielen -= 4;
2807170531Ssam			sep = "+";
2808170531Ssam		}
2809170531Ssam
2810170531Ssam		/* key management algorithms */
2811170531Ssam		n = LE_READ_2(ie);
2812173275Ssam		ie += 2, ielen -= 2;
2813170531Ssam		sep = " km:";
2814170531Ssam		for (; n > 0; n--) {
2815170531Ssam			printf("%s%s", sep, rsn_keymgmt(ie));
2816173275Ssam			ie += 4, ielen -= 4;
2817170531Ssam			sep = "+";
2818170531Ssam		}
2819170531Ssam
2820173275Ssam		if (ielen > 2)		/* optional capabilities */
2821170531Ssam			printf(", caps 0x%x", LE_READ_2(ie));
2822170531Ssam		/* XXXPMKID */
2823170531Ssam		printf(">");
2824170531Ssam	}
2825170531Ssam}
2826170531Ssam
2827181454Ssam/* XXX move to a public include file */
2828181454Ssam#define IEEE80211_WPS_DEV_PASS_ID	0x1012
2829181454Ssam#define IEEE80211_WPS_SELECTED_REG	0x1041
2830181454Ssam#define IEEE80211_WPS_SETUP_STATE	0x1044
2831181454Ssam#define IEEE80211_WPS_UUID_E		0x1047
2832181454Ssam#define IEEE80211_WPS_VERSION		0x104a
2833181454Ssam
2834181454Ssam#define BE_READ_2(p)					\
2835181454Ssam	((u_int16_t)					\
2836181454Ssam	 ((((const u_int8_t *)(p))[1]      ) |		\
2837181454Ssam	  (((const u_int8_t *)(p))[0] <<  8)))
2838181454Ssam
2839181454Ssamstatic void
2840181454Ssamprintwpsie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2841181454Ssam{
2842181454Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
2843181454Ssam	u_int8_t len = ie[1];
2844181454Ssam
2845181454Ssam	printf("%s", tag);
2846181454Ssam	if (verbose) {
2847181454Ssam		static const char *dev_pass_id[] = {
2848181454Ssam			"D",	/* Default (PIN) */
2849181454Ssam			"U",	/* User-specified */
2850181454Ssam			"M",	/* Machine-specified */
2851181454Ssam			"K",	/* Rekey */
2852181454Ssam			"P",	/* PushButton */
2853181454Ssam			"R"	/* Registrar-specified */
2854181454Ssam		};
2855181454Ssam		int n;
2856181454Ssam
2857181454Ssam		ie +=6, len -= 4;		/* NB: len is payload only */
2858181454Ssam
2859181454Ssam		/* WPS IE in Beacon and Probe Resp frames have different fields */
2860181454Ssam		printf("<");
2861181454Ssam		while (len) {
2862181454Ssam			uint16_t tlv_type = BE_READ_2(ie);
2863181454Ssam			uint16_t tlv_len  = BE_READ_2(ie + 2);
2864181454Ssam
2865181454Ssam			ie += 4, len -= 4;
2866181454Ssam
2867181454Ssam			switch (tlv_type) {
2868181454Ssam			case IEEE80211_WPS_VERSION:
2869181454Ssam				printf("v:%d.%d", *ie >> 4, *ie & 0xf);
2870181454Ssam				break;
2871181454Ssam			case IEEE80211_WPS_SETUP_STATE:
2872181454Ssam				/* Only 1 and 2 are valid */
2873181454Ssam				if (*ie == 0 || *ie >= 3)
2874181454Ssam					printf(" state:B");
2875181454Ssam				else
2876181454Ssam					printf(" st:%s", *ie == 1 ? "N" : "C");
2877181454Ssam				break;
2878181454Ssam			case IEEE80211_WPS_SELECTED_REG:
2879181454Ssam				printf(" sel:%s", *ie ? "T" : "F");
2880181454Ssam				break;
2881181454Ssam			case IEEE80211_WPS_DEV_PASS_ID:
2882181454Ssam				n = LE_READ_2(ie);
2883181454Ssam				if (n < N(dev_pass_id))
2884181454Ssam					printf(" dpi:%s", dev_pass_id[n]);
2885181454Ssam				break;
2886181454Ssam			case IEEE80211_WPS_UUID_E:
2887181454Ssam				printf(" uuid-e:");
2888181454Ssam				for (n = 0; n < (tlv_len - 1); n++)
2889181454Ssam					printf("%02x-", ie[n]);
2890181454Ssam				printf("%02x", ie[n]);
2891181454Ssam				break;
2892181454Ssam			}
2893181454Ssam			ie += tlv_len, len -= tlv_len;
2894181454Ssam		}
2895181454Ssam		printf(">");
2896181454Ssam	}
2897181454Ssam#undef N
2898181454Ssam}
2899181454Ssam
2900186904Ssamstatic void
2901186904Ssamprinttdmaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2902186904Ssam{
2903186904Ssam	printf("%s", tag);
2904186904Ssam	if (verbose && ielen >= sizeof(struct ieee80211_tdma_param)) {
2905186904Ssam		const struct ieee80211_tdma_param *tdma =
2906186904Ssam		   (const struct ieee80211_tdma_param *) ie;
2907186904Ssam
2908186904Ssam		/* XXX tstamp */
2909186904Ssam		printf("<v%u slot:%u slotcnt:%u slotlen:%u bintval:%u inuse:0x%x>",
2910186904Ssam		    tdma->tdma_version, tdma->tdma_slot, tdma->tdma_slotcnt,
2911186904Ssam		    LE_READ_2(&tdma->tdma_slotlen), tdma->tdma_bintval,
2912186904Ssam		    tdma->tdma_inuse[0]);
2913186904Ssam	}
2914186904Ssam}
2915186904Ssam
2916170531Ssam/*
2917138593Ssam * Copy the ssid string contents into buf, truncating to fit.  If the
2918138593Ssam * ssid is entirely printable then just copy intact.  Otherwise convert
2919138593Ssam * to hexadecimal.  If the result is truncated then replace the last
2920138593Ssam * three characters with "...".
2921138593Ssam */
2922146873Sjhbstatic int
2923138593Ssamcopy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
2924138593Ssam{
2925138593Ssam	const u_int8_t *p;
2926138593Ssam	size_t maxlen;
2927138593Ssam	int i;
2928138593Ssam
2929138593Ssam	if (essid_len > bufsize)
2930138593Ssam		maxlen = bufsize;
2931138593Ssam	else
2932138593Ssam		maxlen = essid_len;
2933138593Ssam	/* determine printable or not */
2934138593Ssam	for (i = 0, p = essid; i < maxlen; i++, p++) {
2935138593Ssam		if (*p < ' ' || *p > 0x7e)
2936138593Ssam			break;
2937138593Ssam	}
2938138593Ssam	if (i != maxlen) {		/* not printable, print as hex */
2939138593Ssam		if (bufsize < 3)
2940138593Ssam			return 0;
2941138593Ssam		strlcpy(buf, "0x", bufsize);
2942138593Ssam		bufsize -= 2;
2943138593Ssam		p = essid;
2944138593Ssam		for (i = 0; i < maxlen && bufsize >= 2; i++) {
2945147489Savatar			sprintf(&buf[2+2*i], "%02x", p[i]);
2946138593Ssam			bufsize -= 2;
2947138593Ssam		}
2948147489Savatar		if (i != essid_len)
2949147489Savatar			memcpy(&buf[2+2*i-3], "...", 3);
2950138593Ssam	} else {			/* printable, truncate as needed */
2951138593Ssam		memcpy(buf, essid, maxlen);
2952147489Savatar		if (maxlen != essid_len)
2953147489Savatar			memcpy(&buf[maxlen-3], "...", 3);
2954138593Ssam	}
2955138593Ssam	return maxlen;
2956138593Ssam}
2957138593Ssam
2958173275Ssamstatic void
2959173275Ssamprintssid(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2960173275Ssam{
2961173275Ssam	char ssid[2*IEEE80211_NWID_LEN+1];
2962173275Ssam
2963173275Ssam	printf("%s<%.*s>", tag, copy_essid(ssid, maxlen, ie+2, ie[1]), ssid);
2964173275Ssam}
2965173275Ssam
2966173275Ssamstatic void
2967173275Ssamprintrates(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2968173275Ssam{
2969173275Ssam	const char *sep;
2970173275Ssam	int i;
2971173275Ssam
2972173275Ssam	printf("%s", tag);
2973173275Ssam	sep = "<";
2974173275Ssam	for (i = 2; i < ielen; i++) {
2975173275Ssam		printf("%s%s%d", sep,
2976173275Ssam		    ie[i] & IEEE80211_RATE_BASIC ? "B" : "",
2977173275Ssam		    ie[i] & IEEE80211_RATE_VAL);
2978173275Ssam		sep = ",";
2979173275Ssam	}
2980173275Ssam	printf(">");
2981173275Ssam}
2982173275Ssam
2983173275Ssamstatic void
2984173275Ssamprintcountry(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2985173275Ssam{
2986173275Ssam	const struct ieee80211_country_ie *cie =
2987173275Ssam	   (const struct ieee80211_country_ie *) ie;
2988173275Ssam	int i, nbands, schan, nchan;
2989173275Ssam
2990173275Ssam	printf("%s<%c%c%c", tag, cie->cc[0], cie->cc[1], cie->cc[2]);
2991173275Ssam	nbands = (cie->len - 3) / sizeof(cie->band[0]);
2992173275Ssam	for (i = 0; i < nbands; i++) {
2993173275Ssam		schan = cie->band[i].schan;
2994173275Ssam		nchan = cie->band[i].nchan;
2995173275Ssam		if (nchan != 1)
2996173275Ssam			printf(" %u-%u,%u", schan, schan + nchan-1,
2997173275Ssam			    cie->band[i].maxtxpwr);
2998173275Ssam		else
2999173275Ssam			printf(" %u,%u", schan, cie->band[i].maxtxpwr);
3000173275Ssam	}
3001173275Ssam	printf(">");
3002173275Ssam}
3003173275Ssam
3004148686Sstefanf/* unaligned little endian access */
3005138593Ssam#define LE_READ_4(p)					\
3006138593Ssam	((u_int32_t)					\
3007138593Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
3008138593Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
3009138593Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
3010138593Ssam	  (((const u_int8_t *)(p))[3] << 24)))
3011138593Ssam
3012178354Ssamstatic __inline int
3013138593Ssamiswpaoui(const u_int8_t *frm)
3014138593Ssam{
3015138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
3016138593Ssam}
3017138593Ssam
3018178354Ssamstatic __inline int
3019173275Ssamiswmeinfo(const u_int8_t *frm)
3020138593Ssam{
3021173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3022173275Ssam		frm[6] == WME_INFO_OUI_SUBTYPE;
3023138593Ssam}
3024138593Ssam
3025178354Ssamstatic __inline int
3026173275Ssamiswmeparam(const u_int8_t *frm)
3027173275Ssam{
3028173275Ssam	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3029173275Ssam		frm[6] == WME_PARAM_OUI_SUBTYPE;
3030173275Ssam}
3031173275Ssam
3032178354Ssamstatic __inline int
3033138593Ssamisatherosoui(const u_int8_t *frm)
3034138593Ssam{
3035138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
3036138593Ssam}
3037138593Ssam
3038181454Ssamstatic __inline int
3039186904Ssamistdmaoui(const uint8_t *frm)
3040186904Ssam{
3041186904Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((TDMA_OUI_TYPE<<24)|TDMA_OUI);
3042186904Ssam}
3043186904Ssam
3044186904Ssamstatic __inline int
3045181454Ssamiswpsoui(const uint8_t *frm)
3046181454Ssam{
3047181454Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPS_OUI_TYPE<<24)|WPA_OUI);
3048181454Ssam}
3049181454Ssam
3050173275Ssamstatic const char *
3051173275Ssamiename(int elemid)
3052173275Ssam{
3053173275Ssam	switch (elemid) {
3054173275Ssam	case IEEE80211_ELEMID_FHPARMS:	return " FHPARMS";
3055173275Ssam	case IEEE80211_ELEMID_CFPARMS:	return " CFPARMS";
3056173275Ssam	case IEEE80211_ELEMID_TIM:	return " TIM";
3057173275Ssam	case IEEE80211_ELEMID_IBSSPARMS:return " IBSSPARMS";
3058173275Ssam	case IEEE80211_ELEMID_CHALLENGE:return " CHALLENGE";
3059173275Ssam	case IEEE80211_ELEMID_PWRCNSTR:	return " PWRCNSTR";
3060173275Ssam	case IEEE80211_ELEMID_PWRCAP:	return " PWRCAP";
3061173275Ssam	case IEEE80211_ELEMID_TPCREQ:	return " TPCREQ";
3062173275Ssam	case IEEE80211_ELEMID_TPCREP:	return " TPCREP";
3063173275Ssam	case IEEE80211_ELEMID_SUPPCHAN:	return " SUPPCHAN";
3064193447Ssam	case IEEE80211_ELEMID_CSA:	return " CSA";
3065173275Ssam	case IEEE80211_ELEMID_MEASREQ:	return " MEASREQ";
3066173275Ssam	case IEEE80211_ELEMID_MEASREP:	return " MEASREP";
3067173275Ssam	case IEEE80211_ELEMID_QUIET:	return " QUIET";
3068173275Ssam	case IEEE80211_ELEMID_IBSSDFS:	return " IBSSDFS";
3069173275Ssam	case IEEE80211_ELEMID_TPC:	return " TPC";
3070173275Ssam	case IEEE80211_ELEMID_CCKM:	return " CCKM";
3071173275Ssam	}
3072173275Ssam	return " ???";
3073173275Ssam}
3074173275Ssam
3075138593Ssamstatic void
3076138593Ssamprinties(const u_int8_t *vp, int ielen, int maxcols)
3077138593Ssam{
3078138593Ssam	while (ielen > 0) {
3079138593Ssam		switch (vp[0]) {
3080173275Ssam		case IEEE80211_ELEMID_SSID:
3081173275Ssam			if (verbose)
3082173275Ssam				printssid(" SSID", vp, 2+vp[1], maxcols);
3083173275Ssam			break;
3084173275Ssam		case IEEE80211_ELEMID_RATES:
3085173275Ssam		case IEEE80211_ELEMID_XRATES:
3086173275Ssam			if (verbose)
3087173275Ssam				printrates(vp[0] == IEEE80211_ELEMID_RATES ?
3088173275Ssam				    " RATES" : " XRATES", vp, 2+vp[1], maxcols);
3089173275Ssam			break;
3090173275Ssam		case IEEE80211_ELEMID_DSPARMS:
3091173275Ssam			if (verbose)
3092173275Ssam				printf(" DSPARMS<%u>", vp[2]);
3093173275Ssam			break;
3094173275Ssam		case IEEE80211_ELEMID_COUNTRY:
3095173275Ssam			if (verbose)
3096173275Ssam				printcountry(" COUNTRY", vp, 2+vp[1], maxcols);
3097173275Ssam			break;
3098173275Ssam		case IEEE80211_ELEMID_ERP:
3099173275Ssam			if (verbose)
3100173275Ssam				printf(" ERP<0x%x>", vp[2]);
3101173275Ssam			break;
3102138593Ssam		case IEEE80211_ELEMID_VENDOR:
3103138593Ssam			if (iswpaoui(vp))
3104170531Ssam				printwpaie(" WPA", vp, 2+vp[1], maxcols);
3105173275Ssam			else if (iswmeinfo(vp))
3106173275Ssam				printwmeinfo(" WME", vp, 2+vp[1], maxcols);
3107173275Ssam			else if (iswmeparam(vp))
3108173275Ssam				printwmeparam(" WME", vp, 2+vp[1], maxcols);
3109139492Ssam			else if (isatherosoui(vp))
3110170531Ssam				printathie(" ATH", vp, 2+vp[1], maxcols);
3111181454Ssam			else if (iswpsoui(vp))
3112181454Ssam				printwpsie(" WPS", vp, 2+vp[1], maxcols);
3113186904Ssam			else if (istdmaoui(vp))
3114186904Ssam				printtdmaie(" TDMA", vp, 2+vp[1], maxcols);
3115173275Ssam			else if (verbose)
3116138593Ssam				printie(" VEN", vp, 2+vp[1], maxcols);
3117138593Ssam			break;
3118138593Ssam		case IEEE80211_ELEMID_RSN:
3119170531Ssam			printrsnie(" RSN", vp, 2+vp[1], maxcols);
3120138593Ssam			break;
3121173275Ssam		case IEEE80211_ELEMID_HTCAP:
3122173275Ssam			printhtcap(" HTCAP", vp, 2+vp[1], maxcols);
3123173275Ssam			break;
3124173275Ssam		case IEEE80211_ELEMID_HTINFO:
3125173275Ssam			if (verbose)
3126173275Ssam				printhtinfo(" HTINFO", vp, 2+vp[1], maxcols);
3127173275Ssam			break;
3128195618Srpaulo		case IEEE80211_ELEMID_MESHID:
3129195618Srpaulo			if (verbose)
3130195618Srpaulo				printssid(" MESHID", vp, 2+vp[1], maxcols);
3131195618Srpaulo			break;
3132195618Srpaulo		case IEEE80211_ELEMID_MESHCONF:
3133195618Srpaulo			printmeshconf(" MESHCONF", vp, 2+vp[1], maxcols);
3134195618Srpaulo			break;
3135138593Ssam		default:
3136173275Ssam			if (verbose)
3137173275Ssam				printie(iename(vp[0]), vp, 2+vp[1], maxcols);
3138138593Ssam			break;
3139138593Ssam		}
3140138593Ssam		ielen -= 2+vp[1];
3141138593Ssam		vp += 2+vp[1];
3142138593Ssam	}
3143138593Ssam}
3144138593Ssam
3145138593Ssamstatic void
3146178354Ssamprintmimo(const struct ieee80211_mimo_info *mi)
3147178354Ssam{
3148178354Ssam	/* NB: don't muddy display unless there's something to show */
3149178354Ssam	if (mi->rssi[0] != 0 || mi->rssi[1] != 0 || mi->rssi[2] != 0) {
3150178354Ssam		/* XXX ignore EVM for now */
3151178354Ssam		printf(" (rssi %d:%d:%d nf %d:%d:%d)",
3152178354Ssam		    mi->rssi[0], mi->rssi[1], mi->rssi[2],
3153178354Ssam		    mi->noise[0], mi->noise[1], mi->noise[2]);
3154178354Ssam	}
3155178354Ssam}
3156178354Ssam
3157178354Ssamstatic void
3158138593Ssamlist_scan(int s)
3159138593Ssam{
3160138593Ssam	uint8_t buf[24*1024];
3161153892Srwatson	char ssid[IEEE80211_NWID_LEN+1];
3162173275Ssam	const uint8_t *cp;
3163195618Srpaulo	int len, ssidmax, idlen;
3164138593Ssam
3165173275Ssam	if (get80211len(s, IEEE80211_IOC_SCAN_RESULTS, buf, sizeof(buf), &len) < 0)
3166138593Ssam		errx(1, "unable to get scan results");
3167138593Ssam	if (len < sizeof(struct ieee80211req_scan_result))
3168138593Ssam		return;
3169138593Ssam
3170170531Ssam	getchaninfo(s);
3171170531Ssam
3172195618Srpaulo	ssidmax = verbose ? IEEE80211_NWID_LEN - 1 : 14;
3173170531Ssam	printf("%-*.*s  %-17.17s  %4s %4s  %-7s  %3s %4s\n"
3174195618Srpaulo		, ssidmax, ssidmax, "SSID/MESH ID"
3175138593Ssam		, "BSSID"
3176138593Ssam		, "CHAN"
3177138593Ssam		, "RATE"
3178170531Ssam		, " S:N"
3179138593Ssam		, "INT"
3180138593Ssam		, "CAPS"
3181138593Ssam	);
3182138593Ssam	cp = buf;
3183138593Ssam	do {
3184170531Ssam		const struct ieee80211req_scan_result *sr;
3185195618Srpaulo		const uint8_t *vp, *idp;
3186138593Ssam
3187170531Ssam		sr = (const struct ieee80211req_scan_result *) cp;
3188173275Ssam		vp = cp + sr->isr_ie_off;
3189195618Srpaulo		if (sr->isr_meshid_len) {
3190195618Srpaulo			idp = vp + sr->isr_ssid_len;
3191195618Srpaulo			idlen = sr->isr_meshid_len;
3192195618Srpaulo		} else {
3193195618Srpaulo			idp = vp;
3194195618Srpaulo			idlen = sr->isr_ssid_len;
3195195618Srpaulo		}
3196170531Ssam		printf("%-*.*s  %s  %3d  %3dM %3d:%-3d  %3d %-4.4s"
3197154522Ssam			, ssidmax
3198195618Srpaulo			  , copy_essid(ssid, ssidmax, idp, idlen)
3199154522Ssam			  , ssid
3200138593Ssam			, ether_ntoa((const struct ether_addr *) sr->isr_bssid)
3201165570Ssam			, ieee80211_mhz2ieee(sr->isr_freq, sr->isr_flags)
3202138593Ssam			, getmaxrate(sr->isr_rates, sr->isr_nrates)
3203170531Ssam			, (sr->isr_rssi/2)+sr->isr_noise, sr->isr_noise
3204138593Ssam			, sr->isr_intval
3205138593Ssam			, getcaps(sr->isr_capinfo)
3206138593Ssam		);
3207195618Srpaulo		printies(vp + sr->isr_ssid_len + sr->isr_meshid_len,
3208195618Srpaulo		    sr->isr_ie_len, 24);
3209138593Ssam		printf("\n");
3210138593Ssam		cp += sr->isr_len, len -= sr->isr_len;
3211138593Ssam	} while (len >= sizeof(struct ieee80211req_scan_result));
3212138593Ssam}
3213138593Ssam
3214138593Ssamstatic void
3215138593Ssamscan_and_wait(int s)
3216138593Ssam{
3217178354Ssam	struct ieee80211_scan_req sr;
3218138593Ssam	struct ieee80211req ireq;
3219138593Ssam	int sroute;
3220138593Ssam
3221138593Ssam	sroute = socket(PF_ROUTE, SOCK_RAW, 0);
3222138593Ssam	if (sroute < 0) {
3223138593Ssam		perror("socket(PF_ROUTE,SOCK_RAW)");
3224138593Ssam		return;
3225138593Ssam	}
3226138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3227138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3228138593Ssam	ireq.i_type = IEEE80211_IOC_SCAN_REQ;
3229178354Ssam
3230178354Ssam	memset(&sr, 0, sizeof(sr));
3231178354Ssam	sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE
3232178354Ssam		    | IEEE80211_IOC_SCAN_NOPICK
3233178354Ssam		    | IEEE80211_IOC_SCAN_ONCE;
3234178354Ssam	sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
3235178354Ssam	sr.sr_nssid = 0;
3236178354Ssam
3237178354Ssam	ireq.i_data = &sr;
3238178354Ssam	ireq.i_len = sizeof(sr);
3239138593Ssam	/* NB: only root can trigger a scan so ignore errors */
3240138593Ssam	if (ioctl(s, SIOCS80211, &ireq) >= 0) {
3241138593Ssam		char buf[2048];
3242138593Ssam		struct if_announcemsghdr *ifan;
3243138593Ssam		struct rt_msghdr *rtm;
3244138593Ssam
3245138593Ssam		do {
3246138593Ssam			if (read(sroute, buf, sizeof(buf)) < 0) {
3247138593Ssam				perror("read(PF_ROUTE)");
3248138593Ssam				break;
3249138593Ssam			}
3250138593Ssam			rtm = (struct rt_msghdr *) buf;
3251138593Ssam			if (rtm->rtm_version != RTM_VERSION)
3252138593Ssam				break;
3253138593Ssam			ifan = (struct if_announcemsghdr *) rtm;
3254138593Ssam		} while (rtm->rtm_type != RTM_IEEE80211 ||
3255138593Ssam		    ifan->ifan_what != RTM_IEEE80211_SCAN);
3256138593Ssam	}
3257138593Ssam	close(sroute);
3258138593Ssam}
3259138593Ssam
3260138593Ssamstatic
3261138593SsamDECL_CMD_FUNC(set80211scan, val, d)
3262138593Ssam{
3263138593Ssam	scan_and_wait(s);
3264138593Ssam	list_scan(s);
3265138593Ssam}
3266138593Ssam
3267161147Ssamstatic enum ieee80211_opmode get80211opmode(int s);
3268161147Ssam
3269173275Ssamstatic int
3270173275Ssamgettxseq(const struct ieee80211req_sta_info *si)
3271173275Ssam{
3272173275Ssam	int i, txseq;
3273173275Ssam
3274173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3275173275Ssam		return si->isi_txseqs[0];
3276173275Ssam	/* XXX not right but usually what folks want */
3277173275Ssam	txseq = 0;
3278173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3279173275Ssam		if (si->isi_txseqs[i] > txseq)
3280173275Ssam			txseq = si->isi_txseqs[i];
3281173275Ssam	return txseq;
3282173275Ssam}
3283173275Ssam
3284173275Ssamstatic int
3285173275Ssamgetrxseq(const struct ieee80211req_sta_info *si)
3286173275Ssam{
3287173275Ssam	int i, rxseq;
3288173275Ssam
3289173275Ssam	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3290173275Ssam		return si->isi_rxseqs[0];
3291173275Ssam	/* XXX not right but usually what folks want */
3292173275Ssam	rxseq = 0;
3293173275Ssam	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3294173275Ssam		if (si->isi_rxseqs[i] > rxseq)
3295173275Ssam			rxseq = si->isi_rxseqs[i];
3296173275Ssam	return rxseq;
3297173275Ssam}
3298173275Ssam
3299138593Ssamstatic void
3300138593Ssamlist_stations(int s)
3301138593Ssam{
3302161147Ssam	union {
3303161147Ssam		struct ieee80211req_sta_req req;
3304161147Ssam		uint8_t buf[24*1024];
3305161147Ssam	} u;
3306161147Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
3307170531Ssam	const uint8_t *cp;
3308138593Ssam	int len;
3309138593Ssam
3310161147Ssam	/* broadcast address =>'s get all stations */
3311161147Ssam	(void) memset(u.req.is_u.macaddr, 0xff, IEEE80211_ADDR_LEN);
3312161147Ssam	if (opmode == IEEE80211_M_STA) {
3313161147Ssam		/*
3314161147Ssam		 * Get information about the associated AP.
3315161147Ssam		 */
3316173275Ssam		(void) get80211(s, IEEE80211_IOC_BSSID,
3317173275Ssam		    u.req.is_u.macaddr, IEEE80211_ADDR_LEN);
3318161147Ssam	}
3319173275Ssam	if (get80211len(s, IEEE80211_IOC_STA_INFO, &u, sizeof(u), &len) < 0)
3320138593Ssam		errx(1, "unable to get station information");
3321138593Ssam	if (len < sizeof(struct ieee80211req_sta_info))
3322138593Ssam		return;
3323138593Ssam
3324170531Ssam	getchaninfo(s);
3325170531Ssam
3326195618Srpaulo	if (opmode == IEEE80211_M_MBSS)
3327195618Srpaulo		printf("%-17.17s %4s %5s %5s %7s %4s %4s %4s %6s %6s\n"
3328195618Srpaulo			, "ADDR"
3329195618Srpaulo			, "CHAN"
3330195618Srpaulo			, "LOCAL"
3331195618Srpaulo			, "PEER"
3332195618Srpaulo			, "STATE"
3333195618Srpaulo			, "RATE"
3334195618Srpaulo			, "RSSI"
3335195618Srpaulo			, "IDLE"
3336195618Srpaulo			, "TXSEQ"
3337195618Srpaulo			, "RXSEQ"
3338195618Srpaulo		);
3339195618Srpaulo	else
3340195618Srpaulo		printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %-7s\n"
3341195618Srpaulo			, "ADDR"
3342195618Srpaulo			, "AID"
3343195618Srpaulo			, "CHAN"
3344195618Srpaulo			, "RATE"
3345195618Srpaulo			, "RSSI"
3346195618Srpaulo			, "IDLE"
3347195618Srpaulo			, "TXSEQ"
3348195618Srpaulo			, "RXSEQ"
3349195618Srpaulo			, "CAPS"
3350195618Srpaulo			, "FLAG"
3351195618Srpaulo		);
3352170531Ssam	cp = (const uint8_t *) u.req.info;
3353138593Ssam	do {
3354170531Ssam		const struct ieee80211req_sta_info *si;
3355138593Ssam
3356170531Ssam		si = (const struct ieee80211req_sta_info *) cp;
3357161147Ssam		if (si->isi_len < sizeof(*si))
3358161147Ssam			break;
3359195618Srpaulo		if (opmode == IEEE80211_M_MBSS)
3360195618Srpaulo			printf("%s %4d %5x %5x %7.7s %3dM %4.1f %4d %6d %6d"
3361195618Srpaulo				, ether_ntoa((const struct ether_addr*)
3362195618Srpaulo				    si->isi_macaddr)
3363195618Srpaulo				, ieee80211_mhz2ieee(si->isi_freq,
3364195618Srpaulo				    si->isi_flags)
3365195618Srpaulo				, si->isi_localid
3366195618Srpaulo				, si->isi_peerid
3367195618Srpaulo				, mesh_linkstate_string(si->isi_peerstate)
3368195618Srpaulo				, si->isi_txmbps/2
3369195618Srpaulo				, si->isi_rssi/2.
3370195618Srpaulo				, si->isi_inact
3371195618Srpaulo				, gettxseq(si)
3372195618Srpaulo				, getrxseq(si)
3373195618Srpaulo			);
3374195618Srpaulo		else
3375195618Srpaulo			printf("%s %4u %4d %3dM %4.1f %4d %6d %6d %-4.4s %-7.7s"
3376195618Srpaulo				, ether_ntoa((const struct ether_addr*)
3377195618Srpaulo				    si->isi_macaddr)
3378195618Srpaulo				, IEEE80211_AID(si->isi_associd)
3379195618Srpaulo				, ieee80211_mhz2ieee(si->isi_freq,
3380195618Srpaulo				    si->isi_flags)
3381195618Srpaulo				, si->isi_txmbps/2
3382195618Srpaulo				, si->isi_rssi/2.
3383195618Srpaulo				, si->isi_inact
3384195618Srpaulo				, gettxseq(si)
3385195618Srpaulo				, getrxseq(si)
3386195618Srpaulo				, getcaps(si->isi_capinfo)
3387195618Srpaulo				, getflags(si->isi_state)
3388195618Srpaulo			);
3389170531Ssam		printies(cp + si->isi_ie_off, si->isi_ie_len, 24);
3390178354Ssam		printmimo(&si->isi_mimo);
3391138593Ssam		printf("\n");
3392138593Ssam		cp += si->isi_len, len -= si->isi_len;
3393138593Ssam	} while (len >= sizeof(struct ieee80211req_sta_info));
3394138593Ssam}
3395138593Ssam
3396170531Ssamstatic const char *
3397195618Srpaulomesh_linkstate_string(uint8_t state)
3398195618Srpaulo{
3399195618Srpaulo#define	N(a)	(sizeof(a) / sizeof(a[0]))
3400195618Srpaulo	static const char *state_names[] = {
3401195618Srpaulo	    [0] = "IDLE",
3402195618Srpaulo	    [1] = "OPEN-TX",
3403195618Srpaulo	    [2] = "OPEN-RX",
3404195618Srpaulo	    [3] = "CONF-RX",
3405195618Srpaulo	    [4] = "ESTAB",
3406195618Srpaulo	    [5] = "HOLDING",
3407195618Srpaulo	};
3408195618Srpaulo
3409195618Srpaulo	if (state >= N(state_names)) {
3410195618Srpaulo		static char buf[10];
3411195618Srpaulo		snprintf(buf, sizeof(buf), "#%u", state);
3412195618Srpaulo		return buf;
3413195618Srpaulo	} else
3414195618Srpaulo		return state_names[state];
3415195618Srpaulo#undef N
3416195618Srpaulo}
3417195618Srpaulo
3418195618Srpaulostatic const char *
3419170531Ssamget_chaninfo(const struct ieee80211_channel *c, int precise,
3420170531Ssam	char buf[], size_t bsize)
3421138593Ssam{
3422138593Ssam	buf[0] = '\0';
3423138593Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
3424170531Ssam		strlcat(buf, " FHSS", bsize);
3425187843Ssam	if (IEEE80211_IS_CHAN_A(c))
3426187843Ssam		strlcat(buf, " 11a", bsize);
3427187843Ssam	else if (IEEE80211_IS_CHAN_ANYG(c))
3428187843Ssam		strlcat(buf, " 11g", bsize);
3429187843Ssam	else if (IEEE80211_IS_CHAN_B(c))
3430170531Ssam		strlcat(buf, " 11b", bsize);
3431187843Ssam	if (IEEE80211_IS_CHAN_HALF(c))
3432202161Sgavin		strlcat(buf, "/10MHz", bsize);
3433187843Ssam	if (IEEE80211_IS_CHAN_QUARTER(c))
3434202161Sgavin		strlcat(buf, "/5MHz", bsize);
3435170531Ssam	if (IEEE80211_IS_CHAN_TURBO(c))
3436170531Ssam		strlcat(buf, " Turbo", bsize);
3437170531Ssam	if (precise) {
3438170531Ssam		if (IEEE80211_IS_CHAN_HT20(c))
3439170531Ssam			strlcat(buf, " ht/20", bsize);
3440170531Ssam		else if (IEEE80211_IS_CHAN_HT40D(c))
3441170531Ssam			strlcat(buf, " ht/40-", bsize);
3442170531Ssam		else if (IEEE80211_IS_CHAN_HT40U(c))
3443170531Ssam			strlcat(buf, " ht/40+", bsize);
3444170531Ssam	} else {
3445170531Ssam		if (IEEE80211_IS_CHAN_HT(c))
3446170531Ssam			strlcat(buf, " ht", bsize);
3447170531Ssam	}
3448170531Ssam	return buf;
3449170531Ssam}
3450170531Ssam
3451170531Ssamstatic void
3452173275Ssamprint_chaninfo(const struct ieee80211_channel *c, int verb)
3453170531Ssam{
3454170531Ssam	char buf[14];
3455170531Ssam
3456202161Sgavin	printf("Channel %3u : %u%c MHz%-14.14s",
3457165570Ssam		ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
3458170531Ssam		IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
3459173275Ssam		get_chaninfo(c, verb, buf, sizeof(buf)));
3460138593Ssam}
3461138593Ssam
3462187844Ssamstatic int
3463187844Ssamchanpref(const struct ieee80211_channel *c)
3464187844Ssam{
3465187844Ssam	if (IEEE80211_IS_CHAN_HT40(c))
3466187844Ssam		return 40;
3467187844Ssam	if (IEEE80211_IS_CHAN_HT20(c))
3468187844Ssam		return 30;
3469187844Ssam	if (IEEE80211_IS_CHAN_HALF(c))
3470187844Ssam		return 10;
3471187844Ssam	if (IEEE80211_IS_CHAN_QUARTER(c))
3472187844Ssam		return 5;
3473187844Ssam	if (IEEE80211_IS_CHAN_TURBO(c))
3474187844Ssam		return 25;
3475187844Ssam	if (IEEE80211_IS_CHAN_A(c))
3476187844Ssam		return 20;
3477187844Ssam	if (IEEE80211_IS_CHAN_G(c))
3478187844Ssam		return 20;
3479187844Ssam	if (IEEE80211_IS_CHAN_B(c))
3480187844Ssam		return 15;
3481187844Ssam	if (IEEE80211_IS_CHAN_PUREG(c))
3482187844Ssam		return 15;
3483187844Ssam	return 0;
3484187844Ssam}
3485187844Ssam
3486138593Ssamstatic void
3487173275Ssamprint_channels(int s, const struct ieee80211req_chaninfo *chans,
3488173275Ssam	int allchans, int verb)
3489138593Ssam{
3490187801Ssam	struct ieee80211req_chaninfo *achans;
3491170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3492138593Ssam	const struct ieee80211_channel *c;
3493170531Ssam	int i, half;
3494138593Ssam
3495187801Ssam	achans = malloc(IEEE80211_CHANINFO_SPACE(chans));
3496187801Ssam	if (achans == NULL)
3497187801Ssam		errx(1, "no space for active channel list");
3498187801Ssam	achans->ic_nchans = 0;
3499170531Ssam	memset(reported, 0, sizeof(reported));
3500138593Ssam	if (!allchans) {
3501138593Ssam		struct ieee80211req_chanlist active;
3502138593Ssam
3503173275Ssam		if (get80211(s, IEEE80211_IOC_CHANLIST, &active, sizeof(active)) < 0)
3504138593Ssam			errx(1, "unable to get active channel list");
3505173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3506173275Ssam			c = &chans->ic_chans[i];
3507170531Ssam			if (!isset(active.ic_channels, c->ic_ieee))
3508170531Ssam				continue;
3509170531Ssam			/*
3510170531Ssam			 * Suppress compatible duplicates unless
3511170531Ssam			 * verbose.  The kernel gives us it's
3512170531Ssam			 * complete channel list which has separate
3513170531Ssam			 * entries for 11g/11b and 11a/turbo.
3514170531Ssam			 */
3515173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3516170531Ssam				/* XXX we assume duplicates are adjacent */
3517187801Ssam				achans->ic_chans[achans->ic_nchans-1] = *c;
3518170531Ssam			} else {
3519187801Ssam				achans->ic_chans[achans->ic_nchans++] = *c;
3520170531Ssam				setbit(reported, c->ic_ieee);
3521170531Ssam			}
3522138593Ssam		}
3523170531Ssam	} else {
3524173275Ssam		for (i = 0; i < chans->ic_nchans; i++) {
3525173275Ssam			c = &chans->ic_chans[i];
3526170531Ssam			/* suppress duplicates as above */
3527173275Ssam			if (isset(reported, c->ic_ieee) && !verb) {
3528170531Ssam				/* XXX we assume duplicates are adjacent */
3529187844Ssam				struct ieee80211_channel *a =
3530187844Ssam				    &achans->ic_chans[achans->ic_nchans-1];
3531187844Ssam				if (chanpref(c) > chanpref(a))
3532187844Ssam					*a = *c;
3533170531Ssam			} else {
3534187801Ssam				achans->ic_chans[achans->ic_nchans++] = *c;
3535170531Ssam				setbit(reported, c->ic_ieee);
3536170531Ssam			}
3537170531Ssam		}
3538170531Ssam	}
3539187801Ssam	half = achans->ic_nchans / 2;
3540187801Ssam	if (achans->ic_nchans % 2)
3541138593Ssam		half++;
3542170531Ssam
3543187801Ssam	for (i = 0; i < achans->ic_nchans / 2; i++) {
3544187801Ssam		print_chaninfo(&achans->ic_chans[i], verb);
3545187801Ssam		print_chaninfo(&achans->ic_chans[half+i], verb);
3546138593Ssam		printf("\n");
3547138593Ssam	}
3548187801Ssam	if (achans->ic_nchans % 2) {
3549187801Ssam		print_chaninfo(&achans->ic_chans[i], verb);
3550138593Ssam		printf("\n");
3551138593Ssam	}
3552187801Ssam	free(achans);
3553138593Ssam}
3554138593Ssam
3555138593Ssamstatic void
3556173275Ssamlist_channels(int s, int allchans)
3557173275Ssam{
3558173275Ssam	getchaninfo(s);
3559187801Ssam	print_channels(s, chaninfo, allchans, verbose);
3560173275Ssam}
3561173275Ssam
3562173275Ssamstatic void
3563170531Ssamprint_txpow(const struct ieee80211_channel *c)
3564170531Ssam{
3565202161Sgavin	printf("Channel %3u : %u MHz %3.1f reg %2d  ",
3566170531Ssam	    c->ic_ieee, c->ic_freq,
3567170531Ssam	    c->ic_maxpower/2., c->ic_maxregpower);
3568170531Ssam}
3569170531Ssam
3570170531Ssamstatic void
3571170531Ssamprint_txpow_verbose(const struct ieee80211_channel *c)
3572170531Ssam{
3573173275Ssam	print_chaninfo(c, 1);
3574170531Ssam	printf("min %4.1f dBm  max %3.1f dBm  reg %2d dBm",
3575170531Ssam	    c->ic_minpower/2., c->ic_maxpower/2., c->ic_maxregpower);
3576170531Ssam	/* indicate where regulatory cap limits power use */
3577170531Ssam	if (c->ic_maxpower > 2*c->ic_maxregpower)
3578170531Ssam		printf(" <");
3579170531Ssam}
3580170531Ssam
3581170531Ssamstatic void
3582170531Ssamlist_txpow(int s)
3583170531Ssam{
3584187801Ssam	struct ieee80211req_chaninfo *achans;
3585170531Ssam	uint8_t reported[IEEE80211_CHAN_BYTES];
3586170531Ssam	struct ieee80211_channel *c, *prev;
3587170531Ssam	int i, half;
3588170531Ssam
3589170531Ssam	getchaninfo(s);
3590187801Ssam	achans = malloc(IEEE80211_CHANINFO_SPACE(chaninfo));
3591187801Ssam	if (achans == NULL)
3592187801Ssam		errx(1, "no space for active channel list");
3593187801Ssam	achans->ic_nchans = 0;
3594170531Ssam	memset(reported, 0, sizeof(reported));
3595187801Ssam	for (i = 0; i < chaninfo->ic_nchans; i++) {
3596187801Ssam		c = &chaninfo->ic_chans[i];
3597170531Ssam		/* suppress duplicates as above */
3598170531Ssam		if (isset(reported, c->ic_ieee) && !verbose) {
3599170531Ssam			/* XXX we assume duplicates are adjacent */
3600187801Ssam			prev = &achans->ic_chans[achans->ic_nchans-1];
3601170531Ssam			/* display highest power on channel */
3602170531Ssam			if (c->ic_maxpower > prev->ic_maxpower)
3603170531Ssam				*prev = *c;
3604170531Ssam		} else {
3605187801Ssam			achans->ic_chans[achans->ic_nchans++] = *c;
3606170531Ssam			setbit(reported, c->ic_ieee);
3607170531Ssam		}
3608170531Ssam	}
3609170531Ssam	if (!verbose) {
3610187801Ssam		half = achans->ic_nchans / 2;
3611187801Ssam		if (achans->ic_nchans % 2)
3612170531Ssam			half++;
3613170531Ssam
3614187801Ssam		for (i = 0; i < achans->ic_nchans / 2; i++) {
3615187801Ssam			print_txpow(&achans->ic_chans[i]);
3616187801Ssam			print_txpow(&achans->ic_chans[half+i]);
3617170531Ssam			printf("\n");
3618170531Ssam		}
3619187801Ssam		if (achans->ic_nchans % 2) {
3620187801Ssam			print_txpow(&achans->ic_chans[i]);
3621170531Ssam			printf("\n");
3622170531Ssam		}
3623170531Ssam	} else {
3624187801Ssam		for (i = 0; i < achans->ic_nchans; i++) {
3625187801Ssam			print_txpow_verbose(&achans->ic_chans[i]);
3626170531Ssam			printf("\n");
3627170531Ssam		}
3628170531Ssam	}
3629187801Ssam	free(achans);
3630170531Ssam}
3631170531Ssam
3632170531Ssamstatic void
3633138593Ssamlist_keys(int s)
3634138593Ssam{
3635138593Ssam}
3636138593Ssam
3637138593Ssam#define	IEEE80211_C_BITS \
3638195618Srpaulo	"\20\1STA\002803ENCAP\7FF\10TURBOP\11IBSS\12PMGT" \
3639178354Ssam	"\13HOSTAP\14AHDEMO\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE" \
3640195618Srpaulo	"\21MONITOR\22DFS\23MBSS\30WPA1\31WPA2\32BURST\33WME\34WDS\36BGSCAN" \
3641186904Ssam	"\37TXFRAG\40TDMA"
3642138593Ssam
3643138593Ssamstatic void
3644138593Ssamlist_capabilities(int s)
3645138593Ssam{
3646187801Ssam	struct ieee80211_devcaps_req *dc;
3647138593Ssam
3648187845Ssam	if (verbose)
3649187845Ssam		dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
3650187845Ssam	else
3651187845Ssam		dc = malloc(IEEE80211_DEVCAPS_SIZE(1));
3652187801Ssam	if (dc == NULL)
3653187801Ssam		errx(1, "no space for device capabilities");
3654187845Ssam	dc->dc_chaninfo.ic_nchans = verbose ? MAXCHAN : 1;
3655187801Ssam	getdevcaps(s, dc);
3656187801Ssam	printb("drivercaps", dc->dc_drivercaps, IEEE80211_C_BITS);
3657187801Ssam	if (dc->dc_cryptocaps != 0 || verbose) {
3658178354Ssam		putchar('\n');
3659187801Ssam		printb("cryptocaps", dc->dc_cryptocaps, IEEE80211_CRYPTO_BITS);
3660178354Ssam	}
3661187801Ssam	if (dc->dc_htcaps != 0 || verbose) {
3662178354Ssam		putchar('\n');
3663187801Ssam		printb("htcaps", dc->dc_htcaps, IEEE80211_HTCAP_BITS);
3664178354Ssam	}
3665138593Ssam	putchar('\n');
3666187845Ssam	if (verbose) {
3667187845Ssam		chaninfo = &dc->dc_chaninfo;	/* XXX */
3668187845Ssam		print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, verbose);
3669187845Ssam	}
3670187801Ssam	free(dc);
3671138593Ssam}
3672138593Ssam
3673173275Ssamstatic int
3674173275Ssamget80211wme(int s, int param, int ac, int *val)
3675173275Ssam{
3676173275Ssam	struct ieee80211req ireq;
3677173275Ssam
3678173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3679173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3680173275Ssam	ireq.i_type = param;
3681173275Ssam	ireq.i_len = ac;
3682173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3683173275Ssam		warn("cannot get WME parameter %d, ac %d%s",
3684173275Ssam		    param, ac & IEEE80211_WMEPARAM_VAL,
3685173275Ssam		    ac & IEEE80211_WMEPARAM_BSS ? " (BSS)" : "");
3686173275Ssam		return -1;
3687173275Ssam	}
3688173275Ssam	*val = ireq.i_val;
3689173275Ssam	return 0;
3690173275Ssam}
3691173275Ssam
3692138593Ssamstatic void
3693181199Ssamlist_wme_aci(int s, const char *tag, int ac)
3694138593Ssam{
3695181199Ssam	int val;
3696138593Ssam
3697181199Ssam	printf("\t%s", tag);
3698138593Ssam
3699181199Ssam	/* show WME BSS parameters */
3700181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMIN, ac, &val) != -1)
3701181199Ssam		printf(" cwmin %2u", val);
3702181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_CWMAX, ac, &val) != -1)
3703181199Ssam		printf(" cwmax %2u", val);
3704181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_AIFS, ac, &val) != -1)
3705181199Ssam		printf(" aifs %2u", val);
3706181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_TXOPLIMIT, ac, &val) != -1)
3707181199Ssam		printf(" txopLimit %3u", val);
3708181199Ssam	if (get80211wme(s, IEEE80211_IOC_WME_ACM, ac, &val) != -1) {
3709181199Ssam		if (val)
3710181199Ssam			printf(" acm");
3711181199Ssam		else if (verbose)
3712181199Ssam			printf(" -acm");
3713181199Ssam	}
3714181199Ssam	/* !BSS only */
3715181199Ssam	if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3716181199Ssam		if (get80211wme(s, IEEE80211_IOC_WME_ACKPOLICY, ac, &val) != -1) {
3717181199Ssam			if (!val)
3718181199Ssam				printf(" -ack");
3719138593Ssam			else if (verbose)
3720181199Ssam				printf(" ack");
3721138593Ssam		}
3722181199Ssam	}
3723181199Ssam	printf("\n");
3724181199Ssam}
3725181199Ssam
3726181199Ssamstatic void
3727181199Ssamlist_wme(int s)
3728181199Ssam{
3729181199Ssam	static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
3730181199Ssam	int ac;
3731181199Ssam
3732181199Ssam	if (verbose) {
3733181199Ssam		/* display both BSS and local settings */
3734181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
3735181199Ssam	again:
3736181199Ssam			if (ac & IEEE80211_WMEPARAM_BSS)
3737181199Ssam				list_wme_aci(s, "     ", ac);
3738181199Ssam			else
3739181199Ssam				list_wme_aci(s, acnames[ac], ac);
3740181199Ssam			if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3741181199Ssam				ac |= IEEE80211_WMEPARAM_BSS;
3742181199Ssam				goto again;
3743181199Ssam			} else
3744181199Ssam				ac &= ~IEEE80211_WMEPARAM_BSS;
3745138593Ssam		}
3746181199Ssam	} else {
3747181199Ssam		/* display only channel settings */
3748181199Ssam		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++)
3749181199Ssam			list_wme_aci(s, acnames[ac], ac);
3750138593Ssam	}
3751138593Ssam}
3752138593Ssam
3753149029Ssamstatic void
3754178354Ssamlist_roam(int s)
3755178354Ssam{
3756178354Ssam	const struct ieee80211_roamparam *rp;
3757178354Ssam	int mode;
3758178354Ssam
3759178354Ssam	getroam(s);
3760188784Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
3761178354Ssam		rp = &roamparams.params[mode];
3762178354Ssam		if (rp->rssi == 0 && rp->rate == 0)
3763178354Ssam			continue;
3764188784Ssam		if (mode == IEEE80211_MODE_11NA || mode == IEEE80211_MODE_11NG) {
3765188784Ssam			if (rp->rssi & 1)
3766188784Ssam				LINE_CHECK("roam:%-7.7s rssi %2u.5dBm  MCS %2u    ",
3767188784Ssam				    modename[mode], rp->rssi/2,
3768188784Ssam				    rp->rate &~ IEEE80211_RATE_MCS);
3769188784Ssam			else
3770188784Ssam				LINE_CHECK("roam:%-7.7s rssi %4udBm  MCS %2u    ",
3771188784Ssam				    modename[mode], rp->rssi/2,
3772188784Ssam				    rp->rate &~ IEEE80211_RATE_MCS);
3773188784Ssam		} else {
3774188784Ssam			if (rp->rssi & 1)
3775188784Ssam				LINE_CHECK("roam:%-7.7s rssi %2u.5dBm rate %2u Mb/s",
3776188784Ssam				    modename[mode], rp->rssi/2, rp->rate/2);
3777188784Ssam			else
3778188784Ssam				LINE_CHECK("roam:%-7.7s rssi %4udBm rate %2u Mb/s",
3779188784Ssam				    modename[mode], rp->rssi/2, rp->rate/2);
3780188784Ssam		}
3781178354Ssam	}
3782178354Ssam}
3783178354Ssam
3784178354Ssamstatic void
3785178354Ssamlist_txparams(int s)
3786178354Ssam{
3787178354Ssam	const struct ieee80211_txparam *tp;
3788178354Ssam	int mode;
3789178354Ssam
3790178354Ssam	gettxparams(s);
3791188784Ssam	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
3792178354Ssam		tp = &txparams.params[mode];
3793178354Ssam		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
3794178354Ssam			continue;
3795188784Ssam		if (mode == IEEE80211_MODE_11NA || mode == IEEE80211_MODE_11NG) {
3796188784Ssam			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3797188784Ssam				LINE_CHECK("%-7.7s ucast NONE    mgmt %2u MCS  "
3798188784Ssam				    "mcast %2u MCS  maxretry %u",
3799188784Ssam				    modename[mode],
3800188784Ssam				    tp->mgmtrate &~ IEEE80211_RATE_MCS,
3801188784Ssam				    tp->mcastrate &~ IEEE80211_RATE_MCS,
3802188784Ssam				    tp->maxretry);
3803188784Ssam			else
3804188784Ssam				LINE_CHECK("%-7.7s ucast %2u MCS  mgmt %2u MCS  "
3805188784Ssam				    "mcast %2u MCS  maxretry %u",
3806188784Ssam				    modename[mode],
3807188784Ssam				    tp->ucastrate &~ IEEE80211_RATE_MCS,
3808188784Ssam				    tp->mgmtrate &~ IEEE80211_RATE_MCS,
3809188784Ssam				    tp->mcastrate &~ IEEE80211_RATE_MCS,
3810188784Ssam				    tp->maxretry);
3811188784Ssam		} else {
3812188784Ssam			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3813188784Ssam				LINE_CHECK("%-7.7s ucast NONE    mgmt %2u Mb/s "
3814188784Ssam				    "mcast %2u Mb/s maxretry %u",
3815188784Ssam				    modename[mode],
3816188784Ssam				    tp->mgmtrate/2,
3817188784Ssam				    tp->mcastrate/2, tp->maxretry);
3818188784Ssam			else
3819188784Ssam				LINE_CHECK("%-7.7s ucast %2u Mb/s mgmt %2u Mb/s "
3820188784Ssam				    "mcast %2u Mb/s maxretry %u",
3821188784Ssam				    modename[mode],
3822188784Ssam				    tp->ucastrate/2, tp->mgmtrate/2,
3823188784Ssam				    tp->mcastrate/2, tp->maxretry);
3824188784Ssam		}
3825178354Ssam	}
3826178354Ssam}
3827178354Ssam
3828178354Ssamstatic void
3829173275Ssamprintpolicy(int policy)
3830173275Ssam{
3831173275Ssam	switch (policy) {
3832173275Ssam	case IEEE80211_MACCMD_POLICY_OPEN:
3833173275Ssam		printf("policy: open\n");
3834173275Ssam		break;
3835173275Ssam	case IEEE80211_MACCMD_POLICY_ALLOW:
3836173275Ssam		printf("policy: allow\n");
3837173275Ssam		break;
3838173275Ssam	case IEEE80211_MACCMD_POLICY_DENY:
3839173275Ssam		printf("policy: deny\n");
3840173275Ssam		break;
3841178354Ssam	case IEEE80211_MACCMD_POLICY_RADIUS:
3842178354Ssam		printf("policy: radius\n");
3843178354Ssam		break;
3844173275Ssam	default:
3845173275Ssam		printf("policy: unknown (%u)\n", policy);
3846173275Ssam		break;
3847173275Ssam	}
3848173275Ssam}
3849173275Ssam
3850173275Ssamstatic void
3851149029Ssamlist_mac(int s)
3852149029Ssam{
3853149029Ssam	struct ieee80211req ireq;
3854149029Ssam	struct ieee80211req_maclist *acllist;
3855173275Ssam	int i, nacls, policy, len;
3856173275Ssam	uint8_t *data;
3857149029Ssam	char c;
3858149029Ssam
3859149029Ssam	(void) memset(&ireq, 0, sizeof(ireq));
3860149029Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
3861149029Ssam	ireq.i_type = IEEE80211_IOC_MACCMD;
3862149029Ssam	ireq.i_val = IEEE80211_MACCMD_POLICY;
3863149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3864149029Ssam		if (errno == EINVAL) {
3865149029Ssam			printf("No acl policy loaded\n");
3866149029Ssam			return;
3867149029Ssam		}
3868149029Ssam		err(1, "unable to get mac policy");
3869149029Ssam	}
3870149029Ssam	policy = ireq.i_val;
3871149029Ssam	if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
3872149029Ssam		c = '*';
3873149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
3874149029Ssam		c = '+';
3875149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
3876149029Ssam		c = '-';
3877178354Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_RADIUS) {
3878178354Ssam		c = 'r';		/* NB: should never have entries */
3879149029Ssam	} else {
3880149029Ssam		printf("policy: unknown (%u)\n", policy);
3881149029Ssam		c = '?';
3882149029Ssam	}
3883173275Ssam	if (verbose || c == '?')
3884173275Ssam		printpolicy(policy);
3885173275Ssam
3886175952Ssam	ireq.i_val = IEEE80211_MACCMD_LIST;
3887175952Ssam	ireq.i_len = 0;
3888175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3889173275Ssam		err(1, "unable to get mac acl list size");
3890175952Ssam	if (ireq.i_len == 0) {		/* NB: no acls */
3891173275Ssam		if (!(verbose || c == '?'))
3892173275Ssam			printpolicy(policy);
3893173275Ssam		return;
3894173275Ssam	}
3895175952Ssam	len = ireq.i_len;
3896173275Ssam
3897173275Ssam	data = malloc(len);
3898173275Ssam	if (data == NULL)
3899173275Ssam		err(1, "out of memory for acl list");
3900173275Ssam
3901175952Ssam	ireq.i_data = data;
3902175952Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
3903173275Ssam		err(1, "unable to get mac acl list");
3904173275Ssam	nacls = len / sizeof(*acllist);
3905173275Ssam	acllist = (struct ieee80211req_maclist *) data;
3906149029Ssam	for (i = 0; i < nacls; i++)
3907149029Ssam		printf("%c%s\n", c, ether_ntoa(
3908149029Ssam			(const struct ether_addr *) acllist[i].ml_macaddr));
3909173275Ssam	free(data);
3910149029Ssam}
3911149029Ssam
3912178354Ssamstatic void
3913178354Ssamprint_regdomain(const struct ieee80211_regdomain *reg, int verb)
3914178354Ssam{
3915178354Ssam	if ((reg->regdomain != 0 &&
3916178354Ssam	    reg->regdomain != reg->country) || verb) {
3917178354Ssam		const struct regdomain *rd =
3918178354Ssam		    lib80211_regdomain_findbysku(getregdata(), reg->regdomain);
3919178354Ssam		if (rd == NULL)
3920178354Ssam			LINE_CHECK("regdomain %d", reg->regdomain);
3921178354Ssam		else
3922178354Ssam			LINE_CHECK("regdomain %s", rd->name);
3923178354Ssam	}
3924178354Ssam	if (reg->country != 0 || verb) {
3925178354Ssam		const struct country *cc =
3926178354Ssam		    lib80211_country_findbycc(getregdata(), reg->country);
3927178354Ssam		if (cc == NULL)
3928178354Ssam			LINE_CHECK("country %d", reg->country);
3929178354Ssam		else
3930178354Ssam			LINE_CHECK("country %s", cc->isoname);
3931178354Ssam	}
3932178354Ssam	if (reg->location == 'I')
3933178354Ssam		LINE_CHECK("indoor");
3934178354Ssam	else if (reg->location == 'O')
3935178354Ssam		LINE_CHECK("outdoor");
3936178354Ssam	else if (verb)
3937178354Ssam		LINE_CHECK("anywhere");
3938178354Ssam	if (reg->ecm)
3939178354Ssam		LINE_CHECK("ecm");
3940178354Ssam	else if (verb)
3941178354Ssam		LINE_CHECK("-ecm");
3942178354Ssam}
3943178354Ssam
3944178354Ssamstatic void
3945178354Ssamlist_regdomain(int s, int channelsalso)
3946178354Ssam{
3947178354Ssam	getregdomain(s);
3948178354Ssam	if (channelsalso) {
3949178354Ssam		getchaninfo(s);
3950178354Ssam		spacer = ':';
3951178354Ssam		print_regdomain(&regdomain, 1);
3952178354Ssam		LINE_BREAK();
3953187801Ssam		print_channels(s, chaninfo, 1/*allchans*/, 1/*verbose*/);
3954178354Ssam	} else
3955178354Ssam		print_regdomain(&regdomain, verbose);
3956178354Ssam}
3957178354Ssam
3958195618Srpaulostatic void
3959195618Srpaulolist_mesh(int s)
3960195618Srpaulo{
3961195618Srpaulo	struct ieee80211req ireq;
3962195618Srpaulo	struct ieee80211req_mesh_route routes[128];
3963195908Srpaulo	struct ieee80211req_mesh_route *rt;
3964195618Srpaulo
3965195618Srpaulo	(void) memset(&ireq, 0, sizeof(ireq));
3966195618Srpaulo	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3967195618Srpaulo	ireq.i_type = IEEE80211_IOC_MESH_RTCMD;
3968195618Srpaulo	ireq.i_val = IEEE80211_MESH_RTCMD_LIST;
3969195618Srpaulo	ireq.i_data = &routes;
3970195618Srpaulo	ireq.i_len = sizeof(routes);
3971195618Srpaulo	if (ioctl(s, SIOCG80211, &ireq) < 0)
3972195618Srpaulo	 	err(1, "unable to get the Mesh routing table");
3973195618Srpaulo
3974195908Srpaulo	printf("%-17.17s %-17.17s %4s %4s %4s %6s %s\n"
3975195618Srpaulo		, "DEST"
3976195618Srpaulo		, "NEXT HOP"
3977195618Srpaulo		, "HOPS"
3978195618Srpaulo		, "METRIC"
3979195784Srpaulo		, "LIFETIME"
3980195908Srpaulo		, "MSEQ"
3981195908Srpaulo		, "FLAGS");
3982195618Srpaulo
3983195908Srpaulo	for (rt = &routes[0]; rt - &routes[0] < ireq.i_len / sizeof(*rt); rt++){
3984195618Srpaulo		printf("%s ",
3985195908Srpaulo		    ether_ntoa((const struct ether_addr *)rt->imr_dest));
3986195908Srpaulo		printf("%s %4u   %4u   %6u %6u    %c%c\n",
3987195908Srpaulo			ether_ntoa((const struct ether_addr *)rt->imr_nexthop),
3988195908Srpaulo			rt->imr_nhops, rt->imr_metric, rt->imr_lifetime,
3989195908Srpaulo			rt->imr_lastmseq,
3990195908Srpaulo			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_VALID) ?
3991195908Srpaulo			    'V' : '!',
3992195908Srpaulo			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_PROXY) ?
3993195908Srpaulo			    'P' : ' ');
3994195618Srpaulo	}
3995195618Srpaulo}
3996195618Srpaulo
3997138593Ssamstatic
3998138593SsamDECL_CMD_FUNC(set80211list, arg, d)
3999138593Ssam{
4000138593Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
4001138593Ssam
4002173275Ssam	LINE_INIT('\t');
4003173275Ssam
4004138593Ssam	if (iseq(arg, "sta"))
4005138593Ssam		list_stations(s);
4006138593Ssam	else if (iseq(arg, "scan") || iseq(arg, "ap"))
4007138593Ssam		list_scan(s);
4008138593Ssam	else if (iseq(arg, "chan") || iseq(arg, "freq"))
4009138593Ssam		list_channels(s, 1);
4010138593Ssam	else if (iseq(arg, "active"))
4011138593Ssam		list_channels(s, 0);
4012138593Ssam	else if (iseq(arg, "keys"))
4013138593Ssam		list_keys(s);
4014138593Ssam	else if (iseq(arg, "caps"))
4015138593Ssam		list_capabilities(s);
4016178354Ssam	else if (iseq(arg, "wme") || iseq(arg, "wmm"))
4017138593Ssam		list_wme(s);
4018149029Ssam	else if (iseq(arg, "mac"))
4019149029Ssam		list_mac(s);
4020170531Ssam	else if (iseq(arg, "txpow"))
4021170531Ssam		list_txpow(s);
4022178354Ssam	else if (iseq(arg, "roam"))
4023178354Ssam		list_roam(s);
4024178354Ssam	else if (iseq(arg, "txparam") || iseq(arg, "txparm"))
4025178354Ssam		list_txparams(s);
4026178354Ssam	else if (iseq(arg, "regdomain"))
4027178354Ssam		list_regdomain(s, 1);
4028178354Ssam	else if (iseq(arg, "countries"))
4029178354Ssam		list_countries();
4030195618Srpaulo	else if (iseq(arg, "mesh"))
4031195618Srpaulo		list_mesh(s);
4032138593Ssam	else
4033138593Ssam		errx(1, "Don't know how to list %s for %s", arg, name);
4034178354Ssam	LINE_BREAK();
4035138593Ssam#undef iseq
4036138593Ssam}
4037138593Ssam
4038138593Ssamstatic enum ieee80211_opmode
4039138593Ssamget80211opmode(int s)
4040138593Ssam{
4041138593Ssam	struct ifmediareq ifmr;
4042138593Ssam
4043138593Ssam	(void) memset(&ifmr, 0, sizeof(ifmr));
4044138593Ssam	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
4045138593Ssam
4046138593Ssam	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
4047186101Ssam		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) {
4048186101Ssam			if (ifmr.ifm_current & IFM_FLAG0)
4049186101Ssam				return IEEE80211_M_AHDEMO;
4050186101Ssam			else
4051186101Ssam				return IEEE80211_M_IBSS;
4052186101Ssam		}
4053138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
4054138593Ssam			return IEEE80211_M_HOSTAP;
4055138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
4056138593Ssam			return IEEE80211_M_MONITOR;
4057195618Srpaulo		if (ifmr.ifm_current & IFM_IEEE80211_MBSS)
4058195618Srpaulo			return IEEE80211_M_MBSS;
4059138593Ssam	}
4060138593Ssam	return IEEE80211_M_STA;
4061138593Ssam}
4062138593Ssam
4063138593Ssam#if 0
4064138593Ssamstatic void
4065138593Ssamprintcipher(int s, struct ieee80211req *ireq, int keylenop)
4066138593Ssam{
4067138593Ssam	switch (ireq->i_val) {
4068138593Ssam	case IEEE80211_CIPHER_WEP:
4069138593Ssam		ireq->i_type = keylenop;
4070138593Ssam		if (ioctl(s, SIOCG80211, ireq) != -1)
4071138593Ssam			printf("WEP-%s",
4072138593Ssam			    ireq->i_len <= 5 ? "40" :
4073138593Ssam			    ireq->i_len <= 13 ? "104" : "128");
4074138593Ssam		else
4075138593Ssam			printf("WEP");
4076138593Ssam		break;
4077138593Ssam	case IEEE80211_CIPHER_TKIP:
4078138593Ssam		printf("TKIP");
4079138593Ssam		break;
4080138593Ssam	case IEEE80211_CIPHER_AES_OCB:
4081138593Ssam		printf("AES-OCB");
4082138593Ssam		break;
4083138593Ssam	case IEEE80211_CIPHER_AES_CCM:
4084138593Ssam		printf("AES-CCM");
4085138593Ssam		break;
4086138593Ssam	case IEEE80211_CIPHER_CKIP:
4087138593Ssam		printf("CKIP");
4088138593Ssam		break;
4089138593Ssam	case IEEE80211_CIPHER_NONE:
4090138593Ssam		printf("NONE");
4091138593Ssam		break;
4092138593Ssam	default:
4093138593Ssam		printf("UNKNOWN (0x%x)", ireq->i_val);
4094138593Ssam		break;
4095138593Ssam	}
4096138593Ssam}
4097138593Ssam#endif
4098138593Ssam
4099155931Ssamstatic void
4100138593Ssamprintkey(const struct ieee80211req_key *ik)
4101138593Ssam{
4102138593Ssam	static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
4103138593Ssam	int keylen = ik->ik_keylen;
4104138593Ssam	int printcontents;
4105138593Ssam
4106148001Srwatson	printcontents = printkeys &&
4107138593Ssam		(memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
4108138593Ssam	if (printcontents)
4109138593Ssam		LINE_BREAK();
4110138593Ssam	switch (ik->ik_type) {
4111138593Ssam	case IEEE80211_CIPHER_WEP:
4112138593Ssam		/* compatibility */
4113155931Ssam		LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
4114138593Ssam		    keylen <= 5 ? "40-bit" :
4115138593Ssam		    keylen <= 13 ? "104-bit" : "128-bit");
4116138593Ssam		break;
4117138593Ssam	case IEEE80211_CIPHER_TKIP:
4118138593Ssam		if (keylen > 128/8)
4119138593Ssam			keylen -= 128/8;	/* ignore MIC for now */
4120155931Ssam		LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4121138593Ssam		break;
4122138593Ssam	case IEEE80211_CIPHER_AES_OCB:
4123155931Ssam		LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4124138593Ssam		break;
4125138593Ssam	case IEEE80211_CIPHER_AES_CCM:
4126155931Ssam		LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4127138593Ssam		break;
4128138593Ssam	case IEEE80211_CIPHER_CKIP:
4129155931Ssam		LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4130138593Ssam		break;
4131138593Ssam	case IEEE80211_CIPHER_NONE:
4132155931Ssam		LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4133138593Ssam		break;
4134138593Ssam	default:
4135155931Ssam		LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
4136138593Ssam			ik->ik_type, ik->ik_keyix+1, 8*keylen);
4137138593Ssam		break;
4138138593Ssam	}
4139138593Ssam	if (printcontents) {
4140138593Ssam		int i;
4141138593Ssam
4142138593Ssam		printf(" <");
4143138593Ssam		for (i = 0; i < keylen; i++)
4144138593Ssam			printf("%02x", ik->ik_keydata[i]);
4145138593Ssam		printf(">");
4146138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4147138593Ssam		    (ik->ik_keyrsc != 0 || verbose))
4148146873Sjhb			printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
4149138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4150138593Ssam		    (ik->ik_keytsc != 0 || verbose))
4151146873Sjhb			printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
4152138593Ssam		if (ik->ik_flags != 0 && verbose) {
4153138593Ssam			const char *sep = " ";
4154138593Ssam
4155138593Ssam			if (ik->ik_flags & IEEE80211_KEY_XMIT)
4156138593Ssam				printf("%stx", sep), sep = "+";
4157138593Ssam			if (ik->ik_flags & IEEE80211_KEY_RECV)
4158138593Ssam				printf("%srx", sep), sep = "+";
4159138593Ssam			if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
4160138593Ssam				printf("%sdef", sep), sep = "+";
4161138593Ssam		}
4162138593Ssam		LINE_BREAK();
4163138593Ssam	}
4164138593Ssam}
4165138593Ssam
4166138593Ssamstatic void
4167173275Ssamprintrate(const char *tag, int v, int defrate, int defmcs)
4168138593Ssam{
4169188784Ssam	if ((v & IEEE80211_RATE_MCS) == 0) {
4170188784Ssam		if (v != defrate) {
4171188784Ssam			if (v & 1)
4172188784Ssam				LINE_CHECK("%s %d.5", tag, v/2);
4173188784Ssam			else
4174188784Ssam				LINE_CHECK("%s %d", tag, v/2);
4175188784Ssam		}
4176188784Ssam	} else {
4177173275Ssam		if (v != defmcs)
4178173275Ssam			LINE_CHECK("%s %d", tag, v &~ 0x80);
4179173275Ssam	}
4180173275Ssam}
4181173275Ssam
4182173275Ssamstatic int
4183195618Srpaulogetid(int s, int ix, void *data, size_t len, int *plen, int mesh)
4184173275Ssam{
4185138593Ssam	struct ieee80211req ireq;
4186138593Ssam
4187138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4188138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4189195618Srpaulo	ireq.i_type = (!mesh) ? IEEE80211_IOC_SSID : IEEE80211_IOC_MESH_ID;
4190173275Ssam	ireq.i_val = ix;
4191173275Ssam	ireq.i_data = data;
4192173275Ssam	ireq.i_len = len;
4193173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4194173275Ssam		return -1;
4195173275Ssam	*plen = ireq.i_len;
4196173275Ssam	return 0;
4197173275Ssam}
419877218Sphk
4199173275Ssamstatic void
4200173275Ssamieee80211_status(int s)
4201173275Ssam{
4202173275Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
4203173275Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
4204173275Ssam	int i, num, wpa, wme, bgscan, bgscaninterval, val, len, wepmode;
4205173275Ssam	uint8_t data[32];
4206173275Ssam	const struct ieee80211_channel *c;
4207178354Ssam	const struct ieee80211_roamparam *rp;
4208178354Ssam	const struct ieee80211_txparam *tp;
4209173275Ssam
4210195618Srpaulo	if (getid(s, -1, data, sizeof(data), &len, 0) < 0) {
4211148686Sstefanf		/* If we can't get the SSID, this isn't an 802.11 device. */
421277218Sphk		return;
421377218Sphk	}
4214173275Ssam
4215173275Ssam	/*
4216173275Ssam	 * Invalidate cached state so printing status for multiple
4217173275Ssam	 * if's doesn't reuse the first interfaces' cached state.
4218173275Ssam	 */
4219173275Ssam	gotcurchan = 0;
4220178354Ssam	gotroam = 0;
4221178354Ssam	gottxparams = 0;
4222173275Ssam	gothtconf = 0;
4223178354Ssam	gotregdomain = 0;
4224173275Ssam
4225195618Srpaulo	printf("\t");
4226195618Srpaulo	if (opmode == IEEE80211_M_MBSS) {
4227195618Srpaulo		printf("meshid ");
4228195618Srpaulo		getid(s, 0, data, sizeof(data), &len, 1);
4229195618Srpaulo		print_string(data, len);
4230195618Srpaulo	} else {
4231195618Srpaulo		if (get80211val(s, IEEE80211_IOC_NUMSSIDS, &num) < 0)
4232195618Srpaulo			num = 0;
4233195618Srpaulo		printf("ssid ");
4234195618Srpaulo		if (num > 1) {
4235195618Srpaulo			for (i = 0; i < num; i++) {
4236195618Srpaulo				if (getid(s, i, data, sizeof(data), &len, 0) >= 0 && len > 0) {
4237195618Srpaulo					printf(" %d:", i + 1);
4238195618Srpaulo					print_string(data, len);
4239195618Srpaulo				}
4240138593Ssam			}
4241195618Srpaulo		} else
4242195618Srpaulo			print_string(data, len);
4243195618Srpaulo	}
4244173275Ssam	c = getcurchan(s);
4245170531Ssam	if (c->ic_freq != IEEE80211_CHAN_ANY) {
4246170531Ssam		char buf[14];
4247202161Sgavin		printf(" channel %d (%u MHz%s)", c->ic_ieee, c->ic_freq,
4248170531Ssam			get_chaninfo(c, 1, buf, sizeof(buf)));
4249138593Ssam	} else if (verbose)
4250138593Ssam		printf(" channel UNDEF");
4251138593Ssam
4252173275Ssam	if (get80211(s, IEEE80211_IOC_BSSID, data, IEEE80211_ADDR_LEN) >= 0 &&
4253173275Ssam	    (memcmp(data, zerobssid, sizeof(zerobssid)) != 0 || verbose))
4254173275Ssam		printf(" bssid %s", ether_ntoa((struct ether_addr *)data));
4255138593Ssam
4256173275Ssam	if (get80211len(s, IEEE80211_IOC_STATIONNAME, data, sizeof(data), &len) != -1) {
4257138593Ssam		printf("\n\tstationname ");
4258173275Ssam		print_string(data, len);
425977218Sphk	}
426077218Sphk
4261138593Ssam	spacer = ' ';		/* force first break */
4262138593Ssam	LINE_BREAK();
426377218Sphk
4264178354Ssam	list_regdomain(s, 0);
4265178354Ssam
4266173275Ssam	wpa = 0;
4267173275Ssam	if (get80211val(s, IEEE80211_IOC_AUTHMODE, &val) != -1) {
4268173275Ssam		switch (val) {
4269173275Ssam		case IEEE80211_AUTH_NONE:
4270173275Ssam			LINE_CHECK("authmode NONE");
4271173275Ssam			break;
4272173275Ssam		case IEEE80211_AUTH_OPEN:
4273173275Ssam			LINE_CHECK("authmode OPEN");
4274173275Ssam			break;
4275173275Ssam		case IEEE80211_AUTH_SHARED:
4276173275Ssam			LINE_CHECK("authmode SHARED");
4277173275Ssam			break;
4278173275Ssam		case IEEE80211_AUTH_8021X:
4279173275Ssam			LINE_CHECK("authmode 802.1x");
4280173275Ssam			break;
4281173275Ssam		case IEEE80211_AUTH_WPA:
4282173275Ssam			if (get80211val(s, IEEE80211_IOC_WPA, &wpa) < 0)
4283173275Ssam				wpa = 1;	/* default to WPA1 */
4284173275Ssam			switch (wpa) {
4285173275Ssam			case 2:
4286173275Ssam				LINE_CHECK("authmode WPA2/802.11i");
428777218Sphk				break;
4288173275Ssam			case 3:
4289173275Ssam				LINE_CHECK("authmode WPA1+WPA2/802.11i");
429077218Sphk				break;
4291127649Ssam			default:
4292173275Ssam				LINE_CHECK("authmode WPA");
4293127649Ssam				break;
4294173275Ssam			}
4295173275Ssam			break;
4296173275Ssam		case IEEE80211_AUTH_AUTO:
4297173275Ssam			LINE_CHECK("authmode AUTO");
4298173275Ssam			break;
4299173275Ssam		default:
4300173275Ssam			LINE_CHECK("authmode UNKNOWN (0x%x)", val);
4301173275Ssam			break;
4302127649Ssam		}
4303127649Ssam	}
4304127649Ssam
4305173275Ssam	if (wpa || verbose) {
4306178354Ssam		if (get80211val(s, IEEE80211_IOC_WPS, &val) != -1) {
4307178354Ssam			if (val)
4308178354Ssam				LINE_CHECK("wps");
4309178354Ssam			else if (verbose)
4310178354Ssam				LINE_CHECK("-wps");
4311178354Ssam		}
4312178354Ssam		if (get80211val(s, IEEE80211_IOC_TSN, &val) != -1) {
4313178354Ssam			if (val)
4314178354Ssam				LINE_CHECK("tsn");
4315178354Ssam			else if (verbose)
4316178354Ssam				LINE_CHECK("-tsn");
4317178354Ssam		}
4318173275Ssam		if (ioctl(s, IEEE80211_IOC_COUNTERMEASURES, &val) != -1) {
4319173275Ssam			if (val)
4320173275Ssam				LINE_CHECK("countermeasures");
4321173275Ssam			else if (verbose)
4322173275Ssam				LINE_CHECK("-countermeasures");
4323173275Ssam		}
4324178354Ssam#if 0
4325178354Ssam		/* XXX not interesting with WPA done in user space */
4326178354Ssam		ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
4327178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4328178354Ssam		}
4329178354Ssam
4330178354Ssam		ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
4331178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4332178354Ssam			LINE_CHECK("mcastcipher ");
4333178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
4334178354Ssam			spacer = ' ';
4335178354Ssam		}
4336178354Ssam
4337178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
4338178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4339178354Ssam			LINE_CHECK("ucastcipher ");
4340178354Ssam			printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
4341178354Ssam		}
4342178354Ssam
4343178354Ssam		if (wpa & 2) {
4344178354Ssam			ireq.i_type = IEEE80211_IOC_RSNCAPS;
4345178354Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1) {
4346178354Ssam				LINE_CHECK("RSN caps 0x%x", ireq.i_val);
4347178354Ssam				spacer = ' ';
4348178354Ssam			}
4349178354Ssam		}
4350178354Ssam
4351178354Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
4352178354Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4353178354Ssam		}
4354178354Ssam#endif
4355173275Ssam	}
4356138593Ssam
4357173275Ssam	if (get80211val(s, IEEE80211_IOC_WEP, &wepmode) != -1 &&
4358173275Ssam	    wepmode != IEEE80211_WEP_NOSUP) {
4359173275Ssam		int firstkey;
4360173275Ssam
4361138718Ssam		switch (wepmode) {
4362173275Ssam		case IEEE80211_WEP_OFF:
4363173275Ssam			LINE_CHECK("privacy OFF");
4364173275Ssam			break;
4365173275Ssam		case IEEE80211_WEP_ON:
4366173275Ssam			LINE_CHECK("privacy ON");
4367173275Ssam			break;
4368173275Ssam		case IEEE80211_WEP_MIXED:
4369173275Ssam			LINE_CHECK("privacy MIXED");
4370173275Ssam			break;
4371173275Ssam		default:
4372173275Ssam			LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
4373173275Ssam			break;
437477218Sphk		}
437577218Sphk
437677218Sphk		/*
437777218Sphk		 * If we get here then we've got WEP support so we need
437877218Sphk		 * to print WEP status.
437991454Sbrooks		 */
438077218Sphk
4381173275Ssam		if (get80211val(s, IEEE80211_IOC_WEPTXKEY, &val) < 0) {
438277218Sphk			warn("WEP support, but no tx key!");
438377218Sphk			goto end;
438477218Sphk		}
4385173275Ssam		if (val != -1)
4386173275Ssam			LINE_CHECK("deftxkey %d", val+1);
4387138718Ssam		else if (wepmode != IEEE80211_WEP_OFF || verbose)
4388155931Ssam			LINE_CHECK("deftxkey UNDEF");
438977218Sphk
4390173275Ssam		if (get80211val(s, IEEE80211_IOC_NUMWEPKEYS, &num) < 0) {
439177218Sphk			warn("WEP support, but no NUMWEPKEYS support!");
439277218Sphk			goto end;
439377218Sphk		}
439477218Sphk
4395138593Ssam		firstkey = 1;
4396138593Ssam		for (i = 0; i < num; i++) {
4397138593Ssam			struct ieee80211req_key ik;
439877218Sphk
4399138593Ssam			memset(&ik, 0, sizeof(ik));
4400138593Ssam			ik.ik_keyix = i;
4401173275Ssam			if (get80211(s, IEEE80211_IOC_WPAKEY, &ik, sizeof(ik)) < 0) {
440277218Sphk				warn("WEP support, but can get keys!");
440377218Sphk				goto end;
440477218Sphk			}
4405138593Ssam			if (ik.ik_keylen != 0) {
4406138593Ssam				if (verbose)
4407138593Ssam					LINE_BREAK();
4408138593Ssam				printkey(&ik);
4409138593Ssam				firstkey = 0;
4410138593Ssam			}
4411138593Ssam		}
4412173275Ssamend:
4413173275Ssam		;
4414138593Ssam	}
4415138593Ssam
4416173275Ssam	if (get80211val(s, IEEE80211_IOC_POWERSAVE, &val) != -1 &&
4417173275Ssam	    val != IEEE80211_POWERSAVE_NOSUP ) {
4418173275Ssam		if (val != IEEE80211_POWERSAVE_OFF || verbose) {
4419173275Ssam			switch (val) {
4420173275Ssam			case IEEE80211_POWERSAVE_OFF:
4421173275Ssam				LINE_CHECK("powersavemode OFF");
4422173275Ssam				break;
4423173275Ssam			case IEEE80211_POWERSAVE_CAM:
4424173275Ssam				LINE_CHECK("powersavemode CAM");
4425173275Ssam				break;
4426173275Ssam			case IEEE80211_POWERSAVE_PSP:
4427173275Ssam				LINE_CHECK("powersavemode PSP");
4428173275Ssam				break;
4429173275Ssam			case IEEE80211_POWERSAVE_PSP_CAM:
4430173275Ssam				LINE_CHECK("powersavemode PSP-CAM");
4431173275Ssam				break;
4432138593Ssam			}
4433173275Ssam			if (get80211val(s, IEEE80211_IOC_POWERSAVESLEEP, &val) != -1)
4434173275Ssam				LINE_CHECK("powersavesleep %d", val);
4435138593Ssam		}
4436138593Ssam	}
4437138593Ssam
4438173275Ssam	if (get80211val(s, IEEE80211_IOC_TXPOWER, &val) != -1) {
4439173275Ssam		if (val & 1)
4440173275Ssam			LINE_CHECK("txpower %d.5", val/2);
4441173275Ssam		else
4442173275Ssam			LINE_CHECK("txpower %d", val/2);
4443173275Ssam	}
4444138593Ssam	if (verbose) {
4445173275Ssam		if (get80211val(s, IEEE80211_IOC_TXPOWMAX, &val) != -1)
4446173275Ssam			LINE_CHECK("txpowmax %.1f", val/2.);
4447138593Ssam	}
4448138593Ssam
4449178354Ssam	if (get80211val(s, IEEE80211_IOC_DOTD, &val) != -1) {
4450178354Ssam		if (val)
4451178354Ssam			LINE_CHECK("dotd");
4452178354Ssam		else if (verbose)
4453178354Ssam			LINE_CHECK("-dotd");
4454178354Ssam	}
4455178354Ssam
4456173275Ssam	if (get80211val(s, IEEE80211_IOC_RTSTHRESHOLD, &val) != -1) {
4457173275Ssam		if (val != IEEE80211_RTS_MAX || verbose)
4458173275Ssam			LINE_CHECK("rtsthreshold %d", val);
4459138593Ssam	}
4460138593Ssam
4461173275Ssam	if (get80211val(s, IEEE80211_IOC_FRAGTHRESHOLD, &val) != -1) {
4462173275Ssam		if (val != IEEE80211_FRAG_MAX || verbose)
4463173275Ssam			LINE_CHECK("fragthreshold %d", val);
4464170531Ssam	}
4465173275Ssam	if (opmode == IEEE80211_M_STA || verbose) {
4466173275Ssam		if (get80211val(s, IEEE80211_IOC_BMISSTHRESHOLD, &val) != -1) {
4467173275Ssam			if (val != IEEE80211_HWBMISS_MAX || verbose)
4468173275Ssam				LINE_CHECK("bmiss %d", val);
4469153354Ssam		}
4470153354Ssam	}
4471153354Ssam
4472178354Ssam	if (!verbose) {
4473178354Ssam		gettxparams(s);
4474178354Ssam		tp = &txparams.params[chan2mode(c)];
4475178354Ssam		printrate("ucastrate", tp->ucastrate,
4476178354Ssam		    IEEE80211_FIXED_RATE_NONE, IEEE80211_FIXED_RATE_NONE);
4477188784Ssam		printrate("mcastrate", tp->mcastrate, 2*1,
4478188784Ssam		    IEEE80211_RATE_MCS|0);
4479188784Ssam		printrate("mgmtrate", tp->mgmtrate, 2*1,
4480188784Ssam		    IEEE80211_RATE_MCS|0);
4481178354Ssam		if (tp->maxretry != 6)		/* XXX */
4482178354Ssam			LINE_CHECK("maxretry %d", tp->maxretry);
4483178354Ssam	} else {
4484178354Ssam		LINE_BREAK();
4485178354Ssam		list_txparams(s);
4486178354Ssam	}
4487170531Ssam
4488173275Ssam	bgscaninterval = -1;
4489173275Ssam	(void) get80211val(s, IEEE80211_IOC_BGSCAN_INTERVAL, &bgscaninterval);
4490173275Ssam
4491173275Ssam	if (get80211val(s, IEEE80211_IOC_SCANVALID, &val) != -1) {
4492173275Ssam		if (val != bgscaninterval || verbose)
4493173275Ssam			LINE_CHECK("scanvalid %u", val);
4494148416Ssam	}
4495148416Ssam
4496173275Ssam	bgscan = 0;
4497173275Ssam	if (get80211val(s, IEEE80211_IOC_BGSCAN, &bgscan) != -1) {
4498173275Ssam		if (bgscan)
4499170531Ssam			LINE_CHECK("bgscan");
4500170531Ssam		else if (verbose)
4501170531Ssam			LINE_CHECK("-bgscan");
4502160687Ssam	}
4503170531Ssam	if (bgscan || verbose) {
4504170531Ssam		if (bgscaninterval != -1)
4505170531Ssam			LINE_CHECK("bgscanintvl %u", bgscaninterval);
4506173275Ssam		if (get80211val(s, IEEE80211_IOC_BGSCAN_IDLE, &val) != -1)
4507173275Ssam			LINE_CHECK("bgscanidle %u", val);
4508178354Ssam		if (!verbose) {
4509178354Ssam			getroam(s);
4510178354Ssam			rp = &roamparams.params[chan2mode(c)];
4511178354Ssam			if (rp->rssi & 1)
4512178354Ssam				LINE_CHECK("roam:rssi %u.5", rp->rssi/2);
4513178354Ssam			else
4514178354Ssam				LINE_CHECK("roam:rssi %u", rp->rssi/2);
4515178354Ssam			LINE_CHECK("roam:rate %u", rp->rate/2);
4516178354Ssam		} else {
4517178354Ssam			LINE_BREAK();
4518178354Ssam			list_roam(s);
4519170531Ssam		}
4520170531Ssam	}
4521160687Ssam
4522165570Ssam	if (IEEE80211_IS_CHAN_ANYG(c) || verbose) {
4523173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREG, &val) != -1) {
4524173275Ssam			if (val)
4525155931Ssam				LINE_CHECK("pureg");
4526147795Ssam			else if (verbose)
4527155931Ssam				LINE_CHECK("-pureg");
4528147795Ssam		}
4529173275Ssam		if (get80211val(s, IEEE80211_IOC_PROTMODE, &val) != -1) {
4530173275Ssam			switch (val) {
4531173275Ssam			case IEEE80211_PROTMODE_OFF:
4532173275Ssam				LINE_CHECK("protmode OFF");
4533173275Ssam				break;
4534173275Ssam			case IEEE80211_PROTMODE_CTS:
4535173275Ssam				LINE_CHECK("protmode CTS");
4536173275Ssam				break;
4537173275Ssam			case IEEE80211_PROTMODE_RTSCTS:
4538173275Ssam				LINE_CHECK("protmode RTSCTS");
4539173275Ssam				break;
4540173275Ssam			default:
4541173275Ssam				LINE_CHECK("protmode UNKNOWN (0x%x)", val);
4542173275Ssam				break;
4543138593Ssam			}
4544138593Ssam		}
4545138593Ssam	}
4546138593Ssam
4547173275Ssam	if (IEEE80211_IS_CHAN_HT(c) || verbose) {
4548173275Ssam		gethtconf(s);
4549173275Ssam		switch (htconf & 3) {
4550173275Ssam		case 0:
4551173275Ssam		case 2:
4552173275Ssam			LINE_CHECK("-ht");
4553173275Ssam			break;
4554173275Ssam		case 1:
4555173275Ssam			LINE_CHECK("ht20");
4556173275Ssam			break;
4557173275Ssam		case 3:
4558173275Ssam			if (verbose)
4559173275Ssam				LINE_CHECK("ht");
4560173275Ssam			break;
4561173275Ssam		}
4562173275Ssam		if (get80211val(s, IEEE80211_IOC_HTCOMPAT, &val) != -1) {
4563173275Ssam			if (!val)
4564173275Ssam				LINE_CHECK("-htcompat");
4565173275Ssam			else if (verbose)
4566173275Ssam				LINE_CHECK("htcompat");
4567173275Ssam		}
4568173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU, &val) != -1) {
4569173275Ssam			switch (val) {
4570173275Ssam			case 0:
4571173275Ssam				LINE_CHECK("-ampdu");
4572173275Ssam				break;
4573173275Ssam			case 1:
4574173275Ssam				LINE_CHECK("ampdutx -ampdurx");
4575173275Ssam				break;
4576173275Ssam			case 2:
4577173275Ssam				LINE_CHECK("-ampdutx ampdurx");
4578173275Ssam				break;
4579173275Ssam			case 3:
4580173275Ssam				if (verbose)
4581173275Ssam					LINE_CHECK("ampdu");
4582173275Ssam				break;
4583173275Ssam			}
4584173275Ssam		}
4585173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_LIMIT, &val) != -1) {
4586173275Ssam			switch (val) {
4587173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_8K:
4588173275Ssam				LINE_CHECK("ampdulimit 8k");
4589173275Ssam				break;
4590173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_16K:
4591173275Ssam				LINE_CHECK("ampdulimit 16k");
4592173275Ssam				break;
4593173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_32K:
4594173275Ssam				LINE_CHECK("ampdulimit 32k");
4595173275Ssam				break;
4596173275Ssam			case IEEE80211_HTCAP_MAXRXAMPDU_64K:
4597173275Ssam				LINE_CHECK("ampdulimit 64k");
4598173275Ssam				break;
4599173275Ssam			}
4600173275Ssam		}
4601173275Ssam		if (get80211val(s, IEEE80211_IOC_AMPDU_DENSITY, &val) != -1) {
4602173275Ssam			switch (val) {
4603173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_NA:
4604173275Ssam				if (verbose)
4605183260Ssam					LINE_CHECK("ampdudensity NA");
4606173275Ssam				break;
4607173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_025:
4608173275Ssam				LINE_CHECK("ampdudensity .25");
4609173275Ssam				break;
4610173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_05:
4611173275Ssam				LINE_CHECK("ampdudensity .5");
4612173275Ssam				break;
4613173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_1:
4614173275Ssam				LINE_CHECK("ampdudensity 1");
4615173275Ssam				break;
4616173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_2:
4617173275Ssam				LINE_CHECK("ampdudensity 2");
4618173275Ssam				break;
4619173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_4:
4620173275Ssam				LINE_CHECK("ampdudensity 4");
4621173275Ssam				break;
4622173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_8:
4623173275Ssam				LINE_CHECK("ampdudensity 8");
4624173275Ssam				break;
4625173275Ssam			case IEEE80211_HTCAP_MPDUDENSITY_16:
4626173275Ssam				LINE_CHECK("ampdudensity 16");
4627173275Ssam				break;
4628173275Ssam			}
4629173275Ssam		}
4630173275Ssam		if (get80211val(s, IEEE80211_IOC_AMSDU, &val) != -1) {
4631173275Ssam			switch (val) {
4632173275Ssam			case 0:
4633173275Ssam				LINE_CHECK("-amsdu");
4634173275Ssam				break;
4635173275Ssam			case 1:
4636173275Ssam				LINE_CHECK("amsdutx -amsdurx");
4637173275Ssam				break;
4638173275Ssam			case 2:
4639173275Ssam				LINE_CHECK("-amsdutx amsdurx");
4640173275Ssam				break;
4641173275Ssam			case 3:
4642173275Ssam				if (verbose)
4643173275Ssam					LINE_CHECK("amsdu");
4644173275Ssam				break;
4645173275Ssam			}
4646173275Ssam		}
4647173275Ssam		/* XXX amsdu limit */
4648173275Ssam		if (get80211val(s, IEEE80211_IOC_SHORTGI, &val) != -1) {
4649173275Ssam			if (val)
4650173275Ssam				LINE_CHECK("shortgi");
4651173275Ssam			else if (verbose)
4652173275Ssam				LINE_CHECK("-shortgi");
4653173275Ssam		}
4654173275Ssam		if (get80211val(s, IEEE80211_IOC_HTPROTMODE, &val) != -1) {
4655173275Ssam			if (val == IEEE80211_PROTMODE_OFF)
4656173275Ssam				LINE_CHECK("htprotmode OFF");
4657173275Ssam			else if (val != IEEE80211_PROTMODE_RTSCTS)
4658173275Ssam				LINE_CHECK("htprotmode UNKNOWN (0x%x)", val);
4659173275Ssam			else if (verbose)
4660173275Ssam				LINE_CHECK("htprotmode RTSCTS");
4661173275Ssam		}
4662173275Ssam		if (get80211val(s, IEEE80211_IOC_PUREN, &val) != -1) {
4663173275Ssam			if (val)
4664173275Ssam				LINE_CHECK("puren");
4665173275Ssam			else if (verbose)
4666173275Ssam				LINE_CHECK("-puren");
4667173275Ssam		}
4668183261Ssam		if (get80211val(s, IEEE80211_IOC_SMPS, &val) != -1) {
4669183261Ssam			if (val == IEEE80211_HTCAP_SMPS_DYNAMIC)
4670183261Ssam				LINE_CHECK("smpsdyn");
4671183261Ssam			else if (val == IEEE80211_HTCAP_SMPS_ENA)
4672183261Ssam				LINE_CHECK("smps");
4673183261Ssam			else if (verbose)
4674183261Ssam				LINE_CHECK("-smps");
4675183261Ssam		}
4676183261Ssam		if (get80211val(s, IEEE80211_IOC_RIFS, &val) != -1) {
4677183261Ssam			if (val)
4678183261Ssam				LINE_CHECK("rifs");
4679183261Ssam			else if (verbose)
4680183261Ssam				LINE_CHECK("-rifs");
4681183261Ssam		}
4682173275Ssam	}
4683173275Ssam
4684173275Ssam	if (get80211val(s, IEEE80211_IOC_WME, &wme) != -1) {
4685138593Ssam		if (wme)
4686155931Ssam			LINE_CHECK("wme");
4687138593Ssam		else if (verbose)
4688155931Ssam			LINE_CHECK("-wme");
4689138593Ssam	} else
4690138593Ssam		wme = 0;
4691138593Ssam
4692173275Ssam	if (get80211val(s, IEEE80211_IOC_BURST, &val) != -1) {
4693173275Ssam		if (val)
4694155931Ssam			LINE_CHECK("burst");
4695153422Ssam		else if (verbose)
4696155931Ssam			LINE_CHECK("-burst");
4697153422Ssam	}
4698153422Ssam
4699173275Ssam	if (get80211val(s, IEEE80211_IOC_FF, &val) != -1) {
4700173275Ssam		if (val)
4701170531Ssam			LINE_CHECK("ff");
4702170531Ssam		else if (verbose)
4703170531Ssam			LINE_CHECK("-ff");
4704170531Ssam	}
4705173275Ssam	if (get80211val(s, IEEE80211_IOC_TURBOP, &val) != -1) {
4706173275Ssam		if (val)
4707170531Ssam			LINE_CHECK("dturbo");
4708170531Ssam		else if (verbose)
4709170531Ssam			LINE_CHECK("-dturbo");
4710170531Ssam	}
4711178354Ssam	if (get80211val(s, IEEE80211_IOC_DWDS, &val) != -1) {
4712178354Ssam		if (val)
4713178354Ssam			LINE_CHECK("dwds");
4714178354Ssam		else if (verbose)
4715178354Ssam			LINE_CHECK("-dwds");
4716178354Ssam	}
4717170531Ssam
4718138593Ssam	if (opmode == IEEE80211_M_HOSTAP) {
4719173275Ssam		if (get80211val(s, IEEE80211_IOC_HIDESSID, &val) != -1) {
4720173275Ssam			if (val)
4721168075Ssam				LINE_CHECK("hidessid");
4722138593Ssam			else if (verbose)
4723168075Ssam				LINE_CHECK("-hidessid");
4724138593Ssam		}
4725173275Ssam		if (get80211val(s, IEEE80211_IOC_APBRIDGE, &val) != -1) {
4726173275Ssam			if (!val)
4727155931Ssam				LINE_CHECK("-apbridge");
4728138593Ssam			else if (verbose)
4729155931Ssam				LINE_CHECK("apbridge");
4730138593Ssam		}
4731173275Ssam		if (get80211val(s, IEEE80211_IOC_DTIM_PERIOD, &val) != -1)
4732173275Ssam			LINE_CHECK("dtimperiod %u", val);
4733138593Ssam
4734173275Ssam		if (get80211val(s, IEEE80211_IOC_DOTH, &val) != -1) {
4735173275Ssam			if (!val)
4736170531Ssam				LINE_CHECK("-doth");
4737170531Ssam			else if (verbose)
4738170531Ssam				LINE_CHECK("doth");
4739170531Ssam		}
4740178354Ssam		if (get80211val(s, IEEE80211_IOC_DFS, &val) != -1) {
4741178354Ssam			if (!val)
4742178354Ssam				LINE_CHECK("-dfs");
4743178354Ssam			else if (verbose)
4744178354Ssam				LINE_CHECK("dfs");
4745178354Ssam		}
4746173275Ssam		if (get80211val(s, IEEE80211_IOC_INACTIVITY, &val) != -1) {
4747173275Ssam			if (!val)
4748173275Ssam				LINE_CHECK("-inact");
4749173275Ssam			else if (verbose)
4750173275Ssam				LINE_CHECK("inact");
4751173275Ssam		}
4752138593Ssam	} else {
4753173275Ssam		if (get80211val(s, IEEE80211_IOC_ROAMING, &val) != -1) {
4754173275Ssam			if (val != IEEE80211_ROAMING_AUTO || verbose) {
4755173275Ssam				switch (val) {
4756138593Ssam				case IEEE80211_ROAMING_DEVICE:
4757155931Ssam					LINE_CHECK("roaming DEVICE");
4758138593Ssam					break;
4759138593Ssam				case IEEE80211_ROAMING_AUTO:
4760155931Ssam					LINE_CHECK("roaming AUTO");
4761138593Ssam					break;
4762138593Ssam				case IEEE80211_ROAMING_MANUAL:
4763155931Ssam					LINE_CHECK("roaming MANUAL");
4764138593Ssam					break;
4765138593Ssam				default:
4766155931Ssam					LINE_CHECK("roaming UNKNOWN (0x%x)",
4767173275Ssam						val);
4768138593Ssam					break;
4769138593Ssam				}
4770138593Ssam			}
4771138593Ssam		}
4772138593Ssam	}
4773186101Ssam
4774186904Ssam	if (opmode == IEEE80211_M_AHDEMO) {
4775186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOT, &val) != -1)
4776186904Ssam			LINE_CHECK("tdmaslot %u", val);
4777186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTCNT, &val) != -1)
4778186904Ssam			LINE_CHECK("tdmaslotcnt %u", val);
4779186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTLEN, &val) != -1)
4780186904Ssam			LINE_CHECK("tdmaslotlen %u", val);
4781186904Ssam		if (get80211val(s, IEEE80211_IOC_TDMA_BINTERVAL, &val) != -1)
4782186904Ssam			LINE_CHECK("tdmabintval %u", val);
4783186904Ssam	} else if (get80211val(s, IEEE80211_IOC_BEACON_INTERVAL, &val) != -1) {
4784173275Ssam		/* XXX default define not visible */
4785173275Ssam		if (val != 100 || verbose)
4786173275Ssam			LINE_CHECK("bintval %u", val);
4787138593Ssam	}
4788138593Ssam
4789138593Ssam	if (wme && verbose) {
4790138593Ssam		LINE_BREAK();
4791138593Ssam		list_wme(s);
4792138593Ssam	}
4793195618Srpaulo
4794195618Srpaulo	if (opmode == IEEE80211_M_MBSS) {
4795195618Srpaulo		if (get80211val(s, IEEE80211_IOC_MESH_TTL, &val) != -1) {
4796195618Srpaulo			LINE_CHECK("meshttl %u", val);
4797195618Srpaulo		}
4798195618Srpaulo		if (get80211val(s, IEEE80211_IOC_MESH_AP, &val) != -1) {
4799195618Srpaulo			if (val)
4800195618Srpaulo				LINE_CHECK("meshpeering");
4801195618Srpaulo			else
4802195618Srpaulo				LINE_CHECK("-meshpeering");
4803195618Srpaulo		}
4804195618Srpaulo		if (get80211val(s, IEEE80211_IOC_MESH_FWRD, &val) != -1) {
4805195618Srpaulo			if (val)
4806195618Srpaulo				LINE_CHECK("meshforward");
4807195618Srpaulo			else
4808195618Srpaulo				LINE_CHECK("-meshforward");
4809195618Srpaulo		}
4810195618Srpaulo		if (get80211len(s, IEEE80211_IOC_MESH_PR_METRIC, data, 12,
4811195618Srpaulo		    &len) != -1) {
4812195618Srpaulo			data[len] = '\0';
4813195618Srpaulo			LINE_CHECK("meshmetric %s", data);
4814195618Srpaulo		}
4815195618Srpaulo		if (get80211len(s, IEEE80211_IOC_MESH_PR_PATH, data, 12,
4816195618Srpaulo		    &len) != -1) {
4817195618Srpaulo			data[len] = '\0';
4818195618Srpaulo			LINE_CHECK("meshpath %s", data);
4819195618Srpaulo		}
4820195618Srpaulo		if (get80211val(s, IEEE80211_IOC_HWMP_ROOTMODE, &val) != -1) {
4821195618Srpaulo			switch (val) {
4822195618Srpaulo			case IEEE80211_HWMP_ROOTMODE_DISABLED:
4823195618Srpaulo				LINE_CHECK("hwmprootmode DISABLED");
4824195618Srpaulo				break;
4825195618Srpaulo			case IEEE80211_HWMP_ROOTMODE_NORMAL:
4826195618Srpaulo				LINE_CHECK("hwmprootmode NORMAL");
4827195618Srpaulo				break;
4828195618Srpaulo			case IEEE80211_HWMP_ROOTMODE_PROACTIVE:
4829195618Srpaulo				LINE_CHECK("hwmprootmode PROACTIVE");
4830195618Srpaulo				break;
4831195618Srpaulo			case IEEE80211_HWMP_ROOTMODE_RANN:
4832195618Srpaulo				LINE_CHECK("hwmprootmode RANN");
4833195618Srpaulo				break;
4834195618Srpaulo			default:
4835195618Srpaulo				LINE_CHECK("hwmprootmode UNKNOWN(%d)", val);
4836195618Srpaulo				break;
4837195618Srpaulo			}
4838195618Srpaulo		}
4839195618Srpaulo		if (get80211val(s, IEEE80211_IOC_HWMP_MAXHOPS, &val) != -1) {
4840195618Srpaulo			LINE_CHECK("hwmpmaxhops %u", val);
4841195618Srpaulo		}
4842195618Srpaulo	}
4843195618Srpaulo
4844173275Ssam	LINE_BREAK();
4845173275Ssam}
4846138593Ssam
4847173275Ssamstatic int
4848173275Ssamget80211(int s, int type, void *data, int len)
4849173275Ssam{
4850173275Ssam	struct ieee80211req ireq;
4851138593Ssam
4852173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4853173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4854173275Ssam	ireq.i_type = type;
4855173275Ssam	ireq.i_data = data;
4856173275Ssam	ireq.i_len = len;
4857173275Ssam	return ioctl(s, SIOCG80211, &ireq);
4858173275Ssam}
4859138593Ssam
4860173275Ssamstatic int
4861173275Ssamget80211len(int s, int type, void *data, int len, int *plen)
4862173275Ssam{
4863173275Ssam	struct ieee80211req ireq;
4864138593Ssam
4865173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4866173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4867173275Ssam	ireq.i_type = type;
4868173275Ssam	ireq.i_len = len;
4869187801Ssam	assert(ireq.i_len == len);	/* NB: check for 16-bit truncation */
4870173275Ssam	ireq.i_data = data;
4871173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4872173275Ssam		return -1;
4873173275Ssam	*plen = ireq.i_len;
4874173275Ssam	return 0;
4875173275Ssam}
4876138593Ssam
4877173275Ssamstatic int
4878173275Ssamget80211val(int s, int type, int *val)
4879173275Ssam{
4880173275Ssam	struct ieee80211req ireq;
488177218Sphk
4882173275Ssam	(void) memset(&ireq, 0, sizeof(ireq));
4883173275Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4884173275Ssam	ireq.i_type = type;
4885173275Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
4886173275Ssam		return -1;
4887173275Ssam	*val = ireq.i_val;
4888173275Ssam	return 0;
488977218Sphk}
489077218Sphk
489177218Sphkstatic void
4892170531Ssamset80211(int s, int type, int val, int len, void *data)
489377218Sphk{
489477218Sphk	struct ieee80211req	ireq;
489577218Sphk
489677218Sphk	(void) memset(&ireq, 0, sizeof(ireq));
489777218Sphk	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
489877218Sphk	ireq.i_type = type;
489977218Sphk	ireq.i_val = val;
490077218Sphk	ireq.i_len = len;
4901187801Ssam	assert(ireq.i_len == len);	/* NB: check for 16-bit truncation */
490277218Sphk	ireq.i_data = data;
490391454Sbrooks	if (ioctl(s, SIOCS80211, &ireq) < 0)
490477218Sphk		err(1, "SIOCS80211");
490577218Sphk}
490677218Sphk
490777218Sphkstatic const char *
490877218Sphkget_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
490977218Sphk{
491077218Sphk	int len;
491177218Sphk	int hexstr;
491277218Sphk	u_int8_t *p;
491377218Sphk
491477218Sphk	len = *lenp;
491577218Sphk	p = buf;
491677218Sphk	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
491777218Sphk	if (hexstr)
491877218Sphk		val += 2;
491977218Sphk	for (;;) {
492077218Sphk		if (*val == '\0')
492177218Sphk			break;
492277218Sphk		if (sep != NULL && strchr(sep, *val) != NULL) {
492377218Sphk			val++;
492477218Sphk			break;
492577218Sphk		}
492677218Sphk		if (hexstr) {
4927127831Sphk			if (!isxdigit((u_char)val[0])) {
492877218Sphk				warnx("bad hexadecimal digits");
492977218Sphk				return NULL;
493077218Sphk			}
4931127831Sphk			if (!isxdigit((u_char)val[1])) {
4932127831Sphk				warnx("odd count hexadecimal digits");
4933127831Sphk				return NULL;
4934127831Sphk			}
493577218Sphk		}
4936127831Sphk		if (p >= buf + len) {
493777218Sphk			if (hexstr)
493877218Sphk				warnx("hexadecimal digits too long");
493977218Sphk			else
4940127831Sphk				warnx("string too long");
494177218Sphk			return NULL;
494277218Sphk		}
494377218Sphk		if (hexstr) {
494477218Sphk#define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
494577218Sphk			*p++ = (tohex((u_char)val[0]) << 4) |
494677218Sphk			    tohex((u_char)val[1]);
494777218Sphk#undef tohex
494877218Sphk			val += 2;
494977218Sphk		} else
495077218Sphk			*p++ = *val++;
495177218Sphk	}
495277218Sphk	len = p - buf;
495377218Sphk	/* The string "-" is treated as the empty string. */
4954165045Ssam	if (!hexstr && len == 1 && buf[0] == '-') {
495577218Sphk		len = 0;
4956165045Ssam		memset(buf, 0, *lenp);
4957165045Ssam	} else if (len < *lenp)
495877218Sphk		memset(p, 0, *lenp - len);
495977218Sphk	*lenp = len;
496077218Sphk	return val;
496177218Sphk}
496277218Sphk
496377218Sphkstatic void
496477218Sphkprint_string(const u_int8_t *buf, int len)
496577218Sphk{
496677218Sphk	int i;
496777218Sphk	int hasspc;
496877218Sphk
496977218Sphk	i = 0;
497077218Sphk	hasspc = 0;
497191454Sbrooks	for (; i < len; i++) {
497277218Sphk		if (!isprint(buf[i]) && buf[i] != '\0')
497377218Sphk			break;
497477218Sphk		if (isspace(buf[i]))
497577218Sphk			hasspc++;
497677218Sphk	}
497777218Sphk	if (i == len) {
497877218Sphk		if (hasspc || len == 0 || buf[0] == '\0')
497977218Sphk			printf("\"%.*s\"", len, buf);
498077218Sphk		else
498177218Sphk			printf("%.*s", len, buf);
498277218Sphk	} else {
498377218Sphk		printf("0x");
498477218Sphk		for (i = 0; i < len; i++)
498577218Sphk			printf("%02x", buf[i]);
498677218Sphk	}
498777218Sphk}
498877218Sphk
4989178354Ssam/*
4990178354Ssam * Virtual AP cloning support.
4991178354Ssam */
4992178354Ssamstatic struct ieee80211_clone_params params = {
4993178354Ssam	.icp_opmode	= IEEE80211_M_STA,	/* default to station mode */
4994178354Ssam};
4995178354Ssam
4996178354Ssamstatic void
4997178354Ssamwlan_create(int s, struct ifreq *ifr)
4998178354Ssam{
4999178354Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
5000178354Ssam
5001178354Ssam	if (params.icp_parent[0] == '\0')
5002189096Srpaulo		errx(1, "must specify a parent device (wlandev) when creating "
5003189096Srpaulo		    "a wlan device");
5004178354Ssam	if (params.icp_opmode == IEEE80211_M_WDS &&
5005178354Ssam	    memcmp(params.icp_bssid, zerobssid, sizeof(zerobssid)) == 0)
5006178354Ssam		errx(1, "no bssid specified for WDS (use wlanbssid)");
5007178354Ssam	ifr->ifr_data = (caddr_t) &params;
5008178354Ssam	if (ioctl(s, SIOCIFCREATE2, ifr) < 0)
5009178354Ssam		err(1, "SIOCIFCREATE2");
5010178354Ssam}
5011178354Ssam
5012178354Ssamstatic
5013178354SsamDECL_CMD_FUNC(set80211clone_wlandev, arg, d)
5014178354Ssam{
5015178354Ssam	strlcpy(params.icp_parent, arg, IFNAMSIZ);
5016178354Ssam}
5017178354Ssam
5018178354Ssamstatic
5019178354SsamDECL_CMD_FUNC(set80211clone_wlanbssid, arg, d)
5020178354Ssam{
5021178354Ssam	const struct ether_addr *ea;
5022178354Ssam
5023178354Ssam	ea = ether_aton(arg);
5024178354Ssam	if (ea == NULL)
5025178354Ssam		errx(1, "%s: cannot parse bssid", arg);
5026178354Ssam	memcpy(params.icp_bssid, ea->octet, IEEE80211_ADDR_LEN);
5027178354Ssam}
5028178354Ssam
5029178354Ssamstatic
5030178354SsamDECL_CMD_FUNC(set80211clone_wlanaddr, arg, d)
5031178354Ssam{
5032178354Ssam	const struct ether_addr *ea;
5033178354Ssam
5034178354Ssam	ea = ether_aton(arg);
5035178354Ssam	if (ea == NULL)
5036178354Ssam		errx(1, "%s: cannot parse addres", arg);
5037178354Ssam	memcpy(params.icp_macaddr, ea->octet, IEEE80211_ADDR_LEN);
5038178354Ssam	params.icp_flags |= IEEE80211_CLONE_MACADDR;
5039178354Ssam}
5040178354Ssam
5041178354Ssamstatic
5042178354SsamDECL_CMD_FUNC(set80211clone_wlanmode, arg, d)
5043178354Ssam{
5044178354Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
5045178354Ssam	if (iseq(arg, "sta"))
5046178354Ssam		params.icp_opmode = IEEE80211_M_STA;
5047178354Ssam	else if (iseq(arg, "ahdemo") || iseq(arg, "adhoc-demo"))
5048178354Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
5049178354Ssam	else if (iseq(arg, "ibss") || iseq(arg, "adhoc"))
5050178354Ssam		params.icp_opmode = IEEE80211_M_IBSS;
5051178354Ssam	else if (iseq(arg, "ap") || iseq(arg, "host"))
5052178354Ssam		params.icp_opmode = IEEE80211_M_HOSTAP;
5053178354Ssam	else if (iseq(arg, "wds"))
5054178354Ssam		params.icp_opmode = IEEE80211_M_WDS;
5055178354Ssam	else if (iseq(arg, "monitor"))
5056178354Ssam		params.icp_opmode = IEEE80211_M_MONITOR;
5057186904Ssam	else if (iseq(arg, "tdma")) {
5058186904Ssam		params.icp_opmode = IEEE80211_M_AHDEMO;
5059186904Ssam		params.icp_flags |= IEEE80211_CLONE_TDMA;
5060195618Srpaulo	} else if (iseq(arg, "mesh") || iseq(arg, "mp")) /* mesh point */
5061195618Srpaulo		params.icp_opmode = IEEE80211_M_MBSS;
5062195618Srpaulo	else
5063178354Ssam		errx(1, "Don't know to create %s for %s", arg, name);
5064178354Ssam#undef iseq
5065178354Ssam}
5066178354Ssam
5067178354Ssamstatic void
5068178354Ssamset80211clone_beacons(const char *val, int d, int s, const struct afswtch *rafp)
5069178354Ssam{
5070178354Ssam	/* NB: inverted sense */
5071178354Ssam	if (d)
5072178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_NOBEACONS;
5073178354Ssam	else
5074178354Ssam		params.icp_flags |= IEEE80211_CLONE_NOBEACONS;
5075178354Ssam}
5076178354Ssam
5077178354Ssamstatic void
5078178354Ssamset80211clone_bssid(const char *val, int d, int s, const struct afswtch *rafp)
5079178354Ssam{
5080178354Ssam	if (d)
5081178354Ssam		params.icp_flags |= IEEE80211_CLONE_BSSID;
5082178354Ssam	else
5083178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_BSSID;
5084178354Ssam}
5085178354Ssam
5086178354Ssamstatic void
5087178354Ssamset80211clone_wdslegacy(const char *val, int d, int s, const struct afswtch *rafp)
5088178354Ssam{
5089178354Ssam	if (d)
5090178354Ssam		params.icp_flags |= IEEE80211_CLONE_WDSLEGACY;
5091178354Ssam	else
5092178354Ssam		params.icp_flags &= ~IEEE80211_CLONE_WDSLEGACY;
5093178354Ssam}
5094178354Ssam
5095138593Ssamstatic struct cmd ieee80211_cmds[] = {
5096138593Ssam	DEF_CMD_ARG("ssid",		set80211ssid),
5097138593Ssam	DEF_CMD_ARG("nwid",		set80211ssid),
5098195618Srpaulo	DEF_CMD_ARG("meshid",		set80211meshid),
5099138593Ssam	DEF_CMD_ARG("stationname",	set80211stationname),
5100138593Ssam	DEF_CMD_ARG("station",		set80211stationname),	/* BSD/OS */
5101138593Ssam	DEF_CMD_ARG("channel",		set80211channel),
5102138593Ssam	DEF_CMD_ARG("authmode",		set80211authmode),
5103138593Ssam	DEF_CMD_ARG("powersavemode",	set80211powersavemode),
5104138593Ssam	DEF_CMD("powersave",	1,	set80211powersave),
5105138593Ssam	DEF_CMD("-powersave",	0,	set80211powersave),
5106138593Ssam	DEF_CMD_ARG("powersavesleep", 	set80211powersavesleep),
5107138593Ssam	DEF_CMD_ARG("wepmode",		set80211wepmode),
5108138593Ssam	DEF_CMD("wep",		1,	set80211wep),
5109138593Ssam	DEF_CMD("-wep",		0,	set80211wep),
5110139493Ssam	DEF_CMD_ARG("deftxkey",		set80211weptxkey),
5111138593Ssam	DEF_CMD_ARG("weptxkey",		set80211weptxkey),
5112138593Ssam	DEF_CMD_ARG("wepkey",		set80211wepkey),
5113138593Ssam	DEF_CMD_ARG("nwkey",		set80211nwkey),		/* NetBSD */
5114138593Ssam	DEF_CMD("-nwkey",	0,	set80211wep),		/* NetBSD */
5115138593Ssam	DEF_CMD_ARG("rtsthreshold",	set80211rtsthreshold),
5116138593Ssam	DEF_CMD_ARG("protmode",		set80211protmode),
5117138593Ssam	DEF_CMD_ARG("txpower",		set80211txpower),
5118138593Ssam	DEF_CMD_ARG("roaming",		set80211roaming),
5119138593Ssam	DEF_CMD("wme",		1,	set80211wme),
5120138593Ssam	DEF_CMD("-wme",		0,	set80211wme),
5121178354Ssam	DEF_CMD("wmm",		1,	set80211wme),
5122178354Ssam	DEF_CMD("-wmm",		0,	set80211wme),
5123138593Ssam	DEF_CMD("hidessid",	1,	set80211hidessid),
5124138593Ssam	DEF_CMD("-hidessid",	0,	set80211hidessid),
5125138593Ssam	DEF_CMD("apbridge",	1,	set80211apbridge),
5126138593Ssam	DEF_CMD("-apbridge",	0,	set80211apbridge),
5127138593Ssam	DEF_CMD_ARG("chanlist",		set80211chanlist),
5128138593Ssam	DEF_CMD_ARG("bssid",		set80211bssid),
5129138593Ssam	DEF_CMD_ARG("ap",		set80211bssid),
5130138593Ssam	DEF_CMD("scan",	0,		set80211scan),
5131138593Ssam	DEF_CMD_ARG("list",		set80211list),
5132138593Ssam	DEF_CMD_ARG2("cwmin",		set80211cwmin),
5133138593Ssam	DEF_CMD_ARG2("cwmax",		set80211cwmax),
5134138593Ssam	DEF_CMD_ARG2("aifs",		set80211aifs),
5135138593Ssam	DEF_CMD_ARG2("txoplimit",	set80211txoplimit),
5136148621Ssam	DEF_CMD_ARG("acm",		set80211acm),
5137148621Ssam	DEF_CMD_ARG("-acm",		set80211noacm),
5138148621Ssam	DEF_CMD_ARG("ack",		set80211ackpolicy),
5139148621Ssam	DEF_CMD_ARG("-ack",		set80211noackpolicy),
5140138593Ssam	DEF_CMD_ARG2("bss:cwmin",	set80211bsscwmin),
5141138593Ssam	DEF_CMD_ARG2("bss:cwmax",	set80211bsscwmax),
5142138593Ssam	DEF_CMD_ARG2("bss:aifs",	set80211bssaifs),
5143138593Ssam	DEF_CMD_ARG2("bss:txoplimit",	set80211bsstxoplimit),
5144138593Ssam	DEF_CMD_ARG("dtimperiod",	set80211dtimperiod),
5145138593Ssam	DEF_CMD_ARG("bintval",		set80211bintval),
5146138593Ssam	DEF_CMD("mac:open",	IEEE80211_MACCMD_POLICY_OPEN,	set80211maccmd),
5147138593Ssam	DEF_CMD("mac:allow",	IEEE80211_MACCMD_POLICY_ALLOW,	set80211maccmd),
5148138593Ssam	DEF_CMD("mac:deny",	IEEE80211_MACCMD_POLICY_DENY,	set80211maccmd),
5149178354Ssam	DEF_CMD("mac:radius",	IEEE80211_MACCMD_POLICY_RADIUS,	set80211maccmd),
5150138593Ssam	DEF_CMD("mac:flush",	IEEE80211_MACCMD_FLUSH,		set80211maccmd),
5151138593Ssam	DEF_CMD("mac:detach",	IEEE80211_MACCMD_DETACH,	set80211maccmd),
5152138593Ssam	DEF_CMD_ARG("mac:add",		set80211addmac),
5153138593Ssam	DEF_CMD_ARG("mac:del",		set80211delmac),
5154138593Ssam	DEF_CMD_ARG("mac:kick",		set80211kickmac),
5155147795Ssam	DEF_CMD("pureg",	1,	set80211pureg),
5156147795Ssam	DEF_CMD("-pureg",	0,	set80211pureg),
5157170531Ssam	DEF_CMD("ff",		1,	set80211fastframes),
5158170531Ssam	DEF_CMD("-ff",		0,	set80211fastframes),
5159170531Ssam	DEF_CMD("dturbo",	1,	set80211dturbo),
5160170531Ssam	DEF_CMD("-dturbo",	0,	set80211dturbo),
5161170531Ssam	DEF_CMD("bgscan",	1,	set80211bgscan),
5162170531Ssam	DEF_CMD("-bgscan",	0,	set80211bgscan),
5163170531Ssam	DEF_CMD_ARG("bgscanidle",	set80211bgscanidle),
5164170531Ssam	DEF_CMD_ARG("bgscanintvl",	set80211bgscanintvl),
5165170531Ssam	DEF_CMD_ARG("scanvalid",	set80211scanvalid),
5166178354Ssam	DEF_CMD_ARG("roam:rssi",	set80211roamrssi),
5167178354Ssam	DEF_CMD_ARG("roam:rate",	set80211roamrate),
5168153354Ssam	DEF_CMD_ARG("mcastrate",	set80211mcastrate),
5169178354Ssam	DEF_CMD_ARG("ucastrate",	set80211ucastrate),
5170178354Ssam	DEF_CMD_ARG("mgtrate",		set80211mgtrate),
5171178354Ssam	DEF_CMD_ARG("mgmtrate",		set80211mgtrate),
5172178354Ssam	DEF_CMD_ARG("maxretry",		set80211maxretry),
5173148416Ssam	DEF_CMD_ARG("fragthreshold",	set80211fragthreshold),
5174153422Ssam	DEF_CMD("burst",	1,	set80211burst),
5175153422Ssam	DEF_CMD("-burst",	0,	set80211burst),
5176160687Ssam	DEF_CMD_ARG("bmiss",		set80211bmissthreshold),
5177160687Ssam	DEF_CMD_ARG("bmissthreshold",	set80211bmissthreshold),
5178173275Ssam	DEF_CMD("shortgi",	1,	set80211shortgi),
5179173275Ssam	DEF_CMD("-shortgi",	0,	set80211shortgi),
5180173275Ssam	DEF_CMD("ampdurx",	2,	set80211ampdu),
5181173275Ssam	DEF_CMD("-ampdurx",	-2,	set80211ampdu),
5182173275Ssam	DEF_CMD("ampdutx",	1,	set80211ampdu),
5183173275Ssam	DEF_CMD("-ampdutx",	-1,	set80211ampdu),
5184173275Ssam	DEF_CMD("ampdu",	3,	set80211ampdu),		/* NB: tx+rx */
5185173275Ssam	DEF_CMD("-ampdu",	-3,	set80211ampdu),
5186173275Ssam	DEF_CMD_ARG("ampdulimit",	set80211ampdulimit),
5187173275Ssam	DEF_CMD_ARG("ampdudensity",	set80211ampdudensity),
5188173275Ssam	DEF_CMD("amsdurx",	2,	set80211amsdu),
5189173275Ssam	DEF_CMD("-amsdurx",	-2,	set80211amsdu),
5190173275Ssam	DEF_CMD("amsdutx",	1,	set80211amsdu),
5191173275Ssam	DEF_CMD("-amsdutx",	-1,	set80211amsdu),
5192173275Ssam	DEF_CMD("amsdu",	3,	set80211amsdu),		/* NB: tx+rx */
5193173275Ssam	DEF_CMD("-amsdu",	-3,	set80211amsdu),
5194173275Ssam	DEF_CMD_ARG("amsdulimit",	set80211amsdulimit),
5195173275Ssam	DEF_CMD("puren",	1,	set80211puren),
5196173275Ssam	DEF_CMD("-puren",	0,	set80211puren),
5197170531Ssam	DEF_CMD("doth",		1,	set80211doth),
5198170531Ssam	DEF_CMD("-doth",	0,	set80211doth),
5199178354Ssam	DEF_CMD("dfs",		1,	set80211dfs),
5200178354Ssam	DEF_CMD("-dfs",		0,	set80211dfs),
5201173275Ssam	DEF_CMD("htcompat",	1,	set80211htcompat),
5202173275Ssam	DEF_CMD("-htcompat",	0,	set80211htcompat),
5203178354Ssam	DEF_CMD("dwds",		1,	set80211dwds),
5204178354Ssam	DEF_CMD("-dwds",	0,	set80211dwds),
5205173275Ssam	DEF_CMD("inact",	1,	set80211inact),
5206173275Ssam	DEF_CMD("-inact",	0,	set80211inact),
5207178354Ssam	DEF_CMD("tsn",		1,	set80211tsn),
5208178354Ssam	DEF_CMD("-tsn",		0,	set80211tsn),
5209178354Ssam	DEF_CMD_ARG("regdomain",	set80211regdomain),
5210178354Ssam	DEF_CMD_ARG("country",		set80211country),
5211178354Ssam	DEF_CMD("indoor",	'I',	set80211location),
5212178354Ssam	DEF_CMD("-indoor",	'O',	set80211location),
5213178354Ssam	DEF_CMD("outdoor",	'O',	set80211location),
5214178354Ssam	DEF_CMD("-outdoor",	'I',	set80211location),
5215178354Ssam	DEF_CMD("anywhere",	' ',	set80211location),
5216178354Ssam	DEF_CMD("ecm",		1,	set80211ecm),
5217178354Ssam	DEF_CMD("-ecm",		0,	set80211ecm),
5218178354Ssam	DEF_CMD("dotd",		1,	set80211dotd),
5219178354Ssam	DEF_CMD("-dotd",	0,	set80211dotd),
5220173275Ssam	DEF_CMD_ARG("htprotmode",	set80211htprotmode),
5221173275Ssam	DEF_CMD("ht20",		1,	set80211htconf),
5222173275Ssam	DEF_CMD("-ht20",	0,	set80211htconf),
5223173275Ssam	DEF_CMD("ht40",		3,	set80211htconf),	/* NB: 20+40 */
5224173275Ssam	DEF_CMD("-ht40",	0,	set80211htconf),
5225173275Ssam	DEF_CMD("ht",		3,	set80211htconf),	/* NB: 20+40 */
5226173275Ssam	DEF_CMD("-ht",		0,	set80211htconf),
5227183261Ssam	DEF_CMD("rifs",		1,	set80211rifs),
5228183261Ssam	DEF_CMD("-rifs",	0,	set80211rifs),
5229183261Ssam	DEF_CMD("smps",		IEEE80211_HTCAP_SMPS_ENA,	set80211smps),
5230183261Ssam	DEF_CMD("smpsdyn",	IEEE80211_HTCAP_SMPS_DYNAMIC,	set80211smps),
5231183261Ssam	DEF_CMD("-smps",	IEEE80211_HTCAP_SMPS_OFF,	set80211smps),
5232178354Ssam	/* XXX for testing */
5233178354Ssam	DEF_CMD_ARG("chanswitch",	set80211chanswitch),
5234178354Ssam
5235186904Ssam	DEF_CMD_ARG("tdmaslot",		set80211tdmaslot),
5236186904Ssam	DEF_CMD_ARG("tdmaslotcnt",	set80211tdmaslotcnt),
5237186904Ssam	DEF_CMD_ARG("tdmaslotlen",	set80211tdmaslotlen),
5238186904Ssam	DEF_CMD_ARG("tdmabintval",	set80211tdmabintval),
5239186904Ssam
5240195618Srpaulo	DEF_CMD_ARG("meshttl",		set80211meshttl),
5241195618Srpaulo	DEF_CMD("meshforward",	1,	set80211meshforward),
5242195618Srpaulo	DEF_CMD("-meshforward",	0,	set80211meshforward),
5243195618Srpaulo	DEF_CMD("meshpeering",	1,	set80211meshpeering),
5244195618Srpaulo	DEF_CMD("-meshpeering",	0,	set80211meshpeering),
5245195618Srpaulo	DEF_CMD_ARG("meshmetric",	set80211meshmetric),
5246195618Srpaulo	DEF_CMD_ARG("meshpath",		set80211meshpath),
5247195618Srpaulo	DEF_CMD("meshrt:flush",	IEEE80211_MESH_RTCMD_FLUSH,	set80211meshrtcmd),
5248195618Srpaulo	DEF_CMD_ARG("meshrt:add",	set80211addmeshrt),
5249195618Srpaulo	DEF_CMD_ARG("meshrt:del",	set80211delmeshrt),
5250195618Srpaulo	DEF_CMD_ARG("hwmprootmode",	set80211hwmprootmode),
5251195618Srpaulo	DEF_CMD_ARG("hwmpmaxhops",	set80211hwmpmaxhops),
5252195618Srpaulo
5253178354Ssam	/* vap cloning support */
5254178354Ssam	DEF_CLONE_CMD_ARG("wlanaddr",	set80211clone_wlanaddr),
5255178354Ssam	DEF_CLONE_CMD_ARG("wlanbssid",	set80211clone_wlanbssid),
5256178354Ssam	DEF_CLONE_CMD_ARG("wlandev",	set80211clone_wlandev),
5257178354Ssam	DEF_CLONE_CMD_ARG("wlanmode",	set80211clone_wlanmode),
5258178354Ssam	DEF_CLONE_CMD("beacons", 1,	set80211clone_beacons),
5259178354Ssam	DEF_CLONE_CMD("-beacons", 0,	set80211clone_beacons),
5260178354Ssam	DEF_CLONE_CMD("bssid",	1,	set80211clone_bssid),
5261178354Ssam	DEF_CLONE_CMD("-bssid",	0,	set80211clone_bssid),
5262178354Ssam	DEF_CLONE_CMD("wdslegacy", 1,	set80211clone_wdslegacy),
5263178354Ssam	DEF_CLONE_CMD("-wdslegacy", 0,	set80211clone_wdslegacy),
5264138593Ssam};
5265138593Ssamstatic struct afswtch af_ieee80211 = {
5266138593Ssam	.af_name	= "af_ieee80211",
5267138593Ssam	.af_af		= AF_UNSPEC,
5268139494Ssam	.af_other_status = ieee80211_status,
5269138593Ssam};
5270138593Ssam
5271138593Ssamstatic __constructor void
5272138593Ssamieee80211_ctor(void)
5273138593Ssam{
5274138593Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
5275138593Ssam	int i;
5276138593Ssam
5277138593Ssam	for (i = 0; i < N(ieee80211_cmds);  i++)
5278138593Ssam		cmd_register(&ieee80211_cmds[i]);
5279138593Ssam	af_register(&af_ieee80211);
5280189096Srpaulo	clone_setdefcallback("wlan", wlan_create);
5281138593Ssam#undef N
5282138593Ssam}
5283