ifieee80211.c revision 197980
1/*
2 * Copyright 2001 The Aerospace Corporation.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 * 3. The name of The Aerospace Corporation may not be used to endorse or
13 *    promote products derived from this software.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sbin/ifconfig/ifieee80211.c 197980 2009-10-12 14:51:19Z rpaulo $
28 */
29
30/*-
31 * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
32 * All rights reserved.
33 *
34 * This code is derived from software contributed to The NetBSD Foundation
35 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
36 * NASA Ames Research Center.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 *    notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 *    notice, this list of conditions and the following disclaimer in the
45 *    documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 *    must display the following acknowledgement:
48 *	This product includes software developed by the NetBSD
49 *	Foundation, Inc. and its contributors.
50 * 4. Neither the name of The NetBSD Foundation nor the names of its
51 *    contributors may be used to endorse or promote products derived
52 *    from this software without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
55 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
58 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 * POSSIBILITY OF SUCH DAMAGE.
65 */
66
67#include <sys/param.h>
68#include <sys/ioctl.h>
69#include <sys/socket.h>
70#include <sys/sysctl.h>
71#include <sys/time.h>
72
73#include <net/ethernet.h>
74#include <net/if.h>
75#include <net/if_dl.h>
76#include <net/if_types.h>
77#include <net/if_media.h>
78#include <net/route.h>
79
80#include <net80211/ieee80211_ioctl.h>
81#include <net80211/ieee80211_freebsd.h>
82#include <net80211/ieee80211_superg.h>
83#include <net80211/ieee80211_tdma.h>
84#include <net80211/ieee80211_mesh.h>
85
86#include <assert.h>
87#include <ctype.h>
88#include <err.h>
89#include <errno.h>
90#include <fcntl.h>
91#include <inttypes.h>
92#include <stdio.h>
93#include <stdlib.h>
94#include <string.h>
95#include <unistd.h>
96#include <stdarg.h>
97#include <stddef.h>		/* NB: for offsetof */
98
99#include "ifconfig.h"
100#include "regdomain.h"
101
102#ifndef IEEE80211_FIXED_RATE_NONE
103#define	IEEE80211_FIXED_RATE_NONE	0xff
104#endif
105
106/* XXX need these publicly defined or similar */
107#ifndef IEEE80211_NODE_AUTH
108#define	IEEE80211_NODE_AUTH	0x000001	/* authorized for data */
109#define	IEEE80211_NODE_QOS	0x000002	/* QoS enabled */
110#define	IEEE80211_NODE_ERP	0x000004	/* ERP enabled */
111#define	IEEE80211_NODE_PWR_MGT	0x000010	/* power save mode enabled */
112#define	IEEE80211_NODE_AREF	0x000020	/* authentication ref held */
113#define	IEEE80211_NODE_HT	0x000040	/* HT enabled */
114#define	IEEE80211_NODE_HTCOMPAT	0x000080	/* HT setup w/ vendor OUI's */
115#define	IEEE80211_NODE_WPS	0x000100	/* WPS association */
116#define	IEEE80211_NODE_TSN	0x000200	/* TSN association */
117#define	IEEE80211_NODE_AMPDU_RX	0x000400	/* AMPDU rx enabled */
118#define	IEEE80211_NODE_AMPDU_TX	0x000800	/* AMPDU tx enabled */
119#define	IEEE80211_NODE_MIMO_PS	0x001000	/* MIMO power save enabled */
120#define	IEEE80211_NODE_MIMO_RTS	0x002000	/* send RTS in MIMO PS */
121#define	IEEE80211_NODE_RIFS	0x004000	/* RIFS enabled */
122#define	IEEE80211_NODE_SGI20	0x008000	/* Short GI in HT20 enabled */
123#define	IEEE80211_NODE_SGI40	0x010000	/* Short GI in HT40 enabled */
124#define	IEEE80211_NODE_ASSOCID	0x020000	/* xmit requires associd */
125#define	IEEE80211_NODE_AMSDU_RX	0x040000	/* AMSDU rx enabled */
126#define	IEEE80211_NODE_AMSDU_TX	0x080000	/* AMSDU tx enabled */
127#endif
128
129#define	MAXCHAN	1536		/* max 1.5K channels */
130
131#define	MAXCOL	78
132static	int col;
133static	char spacer;
134
135static void LINE_INIT(char c);
136static void LINE_BREAK(void);
137static void LINE_CHECK(const char *fmt, ...);
138
139static const char *modename[IEEE80211_MODE_MAX] = {
140	[IEEE80211_MODE_AUTO]	  = "auto",
141	[IEEE80211_MODE_11A]	  = "11a",
142	[IEEE80211_MODE_11B]	  = "11b",
143	[IEEE80211_MODE_11G]	  = "11g",
144	[IEEE80211_MODE_FH]	  = "fh",
145	[IEEE80211_MODE_TURBO_A]  = "turboA",
146	[IEEE80211_MODE_TURBO_G]  = "turboG",
147	[IEEE80211_MODE_STURBO_A] = "sturbo",
148	[IEEE80211_MODE_11NA]	  = "11na",
149	[IEEE80211_MODE_11NG]	  = "11ng",
150	[IEEE80211_MODE_HALF]	  = "half",
151	[IEEE80211_MODE_QUARTER]  = "quarter"
152};
153
154static void set80211(int s, int type, int val, int len, void *data);
155static int get80211(int s, int type, void *data, int len);
156static int get80211len(int s, int type, void *data, int len, int *plen);
157static int get80211val(int s, int type, int *val);
158static const char *get_string(const char *val, const char *sep,
159    u_int8_t *buf, int *lenp);
160static void print_string(const u_int8_t *buf, int len);
161static void print_regdomain(const struct ieee80211_regdomain *, int);
162static void print_channels(int, const struct ieee80211req_chaninfo *,
163    int allchans, int verbose);
164static void regdomain_makechannels(struct ieee80211_regdomain_req *,
165    const struct ieee80211_devcaps_req *);
166static const char *mesh_linkstate_string(uint8_t state);
167
168static struct ieee80211req_chaninfo *chaninfo;
169static struct ieee80211_regdomain regdomain;
170static int gotregdomain = 0;
171static struct ieee80211_roamparams_req roamparams;
172static int gotroam = 0;
173static struct ieee80211_txparams_req txparams;
174static int gottxparams = 0;
175static struct ieee80211_channel curchan;
176static int gotcurchan = 0;
177static struct ifmediareq *ifmr;
178static int htconf = 0;
179static	int gothtconf = 0;
180
181static void
182gethtconf(int s)
183{
184	if (gothtconf)
185		return;
186	if (get80211val(s, IEEE80211_IOC_HTCONF, &htconf) < 0)
187		warn("unable to get HT configuration information");
188	gothtconf = 1;
189}
190
191/*
192 * Collect channel info from the kernel.  We use this (mostly)
193 * to handle mapping between frequency and IEEE channel number.
194 */
195static void
196getchaninfo(int s)
197{
198	if (chaninfo != NULL)
199		return;
200	chaninfo = malloc(IEEE80211_CHANINFO_SIZE(MAXCHAN));
201	if (chaninfo == NULL)
202		errx(1, "no space for channel list");
203	if (get80211(s, IEEE80211_IOC_CHANINFO, chaninfo,
204	    IEEE80211_CHANINFO_SIZE(MAXCHAN)) < 0)
205		err(1, "unable to get channel information");
206	ifmr = ifmedia_getstate(s);
207	gethtconf(s);
208}
209
210static struct regdata *
211getregdata(void)
212{
213	static struct regdata *rdp = NULL;
214	if (rdp == NULL) {
215		rdp = lib80211_alloc_regdata();
216		if (rdp == NULL)
217			errx(-1, "missing or corrupted regdomain database");
218	}
219	return rdp;
220}
221
222/*
223 * Given the channel at index i with attributes from,
224 * check if there is a channel with attributes to in
225 * the channel table.  With suitable attributes this
226 * allows the caller to look for promotion; e.g. from
227 * 11b > 11g.
228 */
229static int
230canpromote(int i, int from, int to)
231{
232	const struct ieee80211_channel *fc = &chaninfo->ic_chans[i];
233	int j;
234
235	if ((fc->ic_flags & from) != from)
236		return i;
237	/* NB: quick check exploiting ordering of chans w/ same frequency */
238	if (i+1 < chaninfo->ic_nchans &&
239	    chaninfo->ic_chans[i+1].ic_freq == fc->ic_freq &&
240	    (chaninfo->ic_chans[i+1].ic_flags & to) == to)
241		return i+1;
242	/* brute force search in case channel list is not ordered */
243	for (j = 0; j < chaninfo->ic_nchans; j++) {
244		const struct ieee80211_channel *tc = &chaninfo->ic_chans[j];
245		if (j != i &&
246		    tc->ic_freq == fc->ic_freq && (tc->ic_flags & to) == to)
247		return j;
248	}
249	return i;
250}
251
252/*
253 * Handle channel promotion.  When a channel is specified with
254 * only a frequency we want to promote it to the ``best'' channel
255 * available.  The channel list has separate entries for 11b, 11g,
256 * 11a, and 11n[ga] channels so specifying a frequency w/o any
257 * attributes requires we upgrade, e.g. from 11b -> 11g.  This
258 * gets complicated when the channel is specified on the same
259 * command line with a media request that constrains the available
260 * channe list (e.g. mode 11a); we want to honor that to avoid
261 * confusing behaviour.
262 */
263static int
264promote(int i)
265{
266	/*
267	 * Query the current mode of the interface in case it's
268	 * constrained (e.g. to 11a).  We must do this carefully
269	 * as there may be a pending ifmedia request in which case
270	 * asking the kernel will give us the wrong answer.  This
271	 * is an unfortunate side-effect of the way ifconfig is
272	 * structure for modularity (yech).
273	 *
274	 * NB: ifmr is actually setup in getchaninfo (above); we
275	 *     assume it's called coincident with to this call so
276	 *     we have a ``current setting''; otherwise we must pass
277	 *     the socket descriptor down to here so we can make
278	 *     the ifmedia_getstate call ourselves.
279	 */
280	int chanmode = ifmr != NULL ? IFM_MODE(ifmr->ifm_current) : IFM_AUTO;
281
282	/* when ambiguous promote to ``best'' */
283	/* NB: we abitrarily pick HT40+ over HT40- */
284	if (chanmode != IFM_IEEE80211_11B)
285		i = canpromote(i, IEEE80211_CHAN_B, IEEE80211_CHAN_G);
286	if (chanmode != IFM_IEEE80211_11G && (htconf & 1)) {
287		i = canpromote(i, IEEE80211_CHAN_G,
288			IEEE80211_CHAN_G | IEEE80211_CHAN_HT20);
289		if (htconf & 2) {
290			i = canpromote(i, IEEE80211_CHAN_G,
291				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D);
292			i = canpromote(i, IEEE80211_CHAN_G,
293				IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U);
294		}
295	}
296	if (chanmode != IFM_IEEE80211_11A && (htconf & 1)) {
297		i = canpromote(i, IEEE80211_CHAN_A,
298			IEEE80211_CHAN_A | IEEE80211_CHAN_HT20);
299		if (htconf & 2) {
300			i = canpromote(i, IEEE80211_CHAN_A,
301				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D);
302			i = canpromote(i, IEEE80211_CHAN_A,
303				IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U);
304		}
305	}
306	return i;
307}
308
309static void
310mapfreq(struct ieee80211_channel *chan, int freq, int flags)
311{
312	int i;
313
314	for (i = 0; i < chaninfo->ic_nchans; i++) {
315		const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
316
317		if (c->ic_freq == freq && (c->ic_flags & flags) == flags) {
318			if (flags == 0) {
319				/* when ambiguous promote to ``best'' */
320				c = &chaninfo->ic_chans[promote(i)];
321			}
322			*chan = *c;
323			return;
324		}
325	}
326	errx(1, "unknown/undefined frequency %u/0x%x", freq, flags);
327}
328
329static void
330mapchan(struct ieee80211_channel *chan, int ieee, int flags)
331{
332	int i;
333
334	for (i = 0; i < chaninfo->ic_nchans; i++) {
335		const struct ieee80211_channel *c = &chaninfo->ic_chans[i];
336
337		if (c->ic_ieee == ieee && (c->ic_flags & flags) == flags) {
338			if (flags == 0) {
339				/* when ambiguous promote to ``best'' */
340				c = &chaninfo->ic_chans[promote(i)];
341			}
342			*chan = *c;
343			return;
344		}
345	}
346	errx(1, "unknown/undefined channel number %d flags 0x%x", ieee, flags);
347}
348
349static const struct ieee80211_channel *
350getcurchan(int s)
351{
352	if (gotcurchan)
353		return &curchan;
354	if (get80211(s, IEEE80211_IOC_CURCHAN, &curchan, sizeof(curchan)) < 0) {
355		int val;
356		/* fall back to legacy ioctl */
357		if (get80211val(s, IEEE80211_IOC_CHANNEL, &val) < 0)
358			err(-1, "cannot figure out current channel");
359		getchaninfo(s);
360		mapchan(&curchan, val, 0);
361	}
362	gotcurchan = 1;
363	return &curchan;
364}
365
366static enum ieee80211_phymode
367chan2mode(const struct ieee80211_channel *c)
368{
369	if (IEEE80211_IS_CHAN_HTA(c))
370		return IEEE80211_MODE_11NA;
371	if (IEEE80211_IS_CHAN_HTG(c))
372		return IEEE80211_MODE_11NG;
373	if (IEEE80211_IS_CHAN_108A(c))
374		return IEEE80211_MODE_TURBO_A;
375	if (IEEE80211_IS_CHAN_108G(c))
376		return IEEE80211_MODE_TURBO_G;
377	if (IEEE80211_IS_CHAN_ST(c))
378		return IEEE80211_MODE_STURBO_A;
379	if (IEEE80211_IS_CHAN_FHSS(c))
380		return IEEE80211_MODE_FH;
381	if (IEEE80211_IS_CHAN_HALF(c))
382		return IEEE80211_MODE_HALF;
383	if (IEEE80211_IS_CHAN_QUARTER(c))
384		return IEEE80211_MODE_QUARTER;
385	if (IEEE80211_IS_CHAN_A(c))
386		return IEEE80211_MODE_11A;
387	if (IEEE80211_IS_CHAN_ANYG(c))
388		return IEEE80211_MODE_11G;
389	if (IEEE80211_IS_CHAN_B(c))
390		return IEEE80211_MODE_11B;
391	return IEEE80211_MODE_AUTO;
392}
393
394static void
395getroam(int s)
396{
397	if (gotroam)
398		return;
399	if (get80211(s, IEEE80211_IOC_ROAM,
400	    &roamparams, sizeof(roamparams)) < 0)
401		err(1, "unable to get roaming parameters");
402	gotroam = 1;
403}
404
405static void
406setroam_cb(int s, void *arg)
407{
408	struct ieee80211_roamparams_req *roam = arg;
409	set80211(s, IEEE80211_IOC_ROAM, 0, sizeof(*roam), roam);
410}
411
412static void
413gettxparams(int s)
414{
415	if (gottxparams)
416		return;
417	if (get80211(s, IEEE80211_IOC_TXPARAMS,
418	    &txparams, sizeof(txparams)) < 0)
419		err(1, "unable to get transmit parameters");
420	gottxparams = 1;
421}
422
423static void
424settxparams_cb(int s, void *arg)
425{
426	struct ieee80211_txparams_req *txp = arg;
427	set80211(s, IEEE80211_IOC_TXPARAMS, 0, sizeof(*txp), txp);
428}
429
430static void
431getregdomain(int s)
432{
433	if (gotregdomain)
434		return;
435	if (get80211(s, IEEE80211_IOC_REGDOMAIN,
436	    &regdomain, sizeof(regdomain)) < 0)
437		err(1, "unable to get regulatory domain info");
438	gotregdomain = 1;
439}
440
441static void
442getdevcaps(int s, struct ieee80211_devcaps_req *dc)
443{
444	if (get80211(s, IEEE80211_IOC_DEVCAPS, dc,
445	    IEEE80211_DEVCAPS_SPACE(dc)) < 0)
446		err(1, "unable to get device capabilities");
447}
448
449static void
450setregdomain_cb(int s, void *arg)
451{
452	struct ieee80211_regdomain_req *req;
453	struct ieee80211_regdomain *rd = arg;
454	struct ieee80211_devcaps_req *dc;
455	struct regdata *rdp = getregdata();
456
457	if (rd->country != NO_COUNTRY) {
458		const struct country *cc;
459		/*
460		 * Check current country seting to make sure it's
461		 * compatible with the new regdomain.  If not, then
462		 * override it with any default country for this
463		 * SKU.  If we cannot arrange a match, then abort.
464		 */
465		cc = lib80211_country_findbycc(rdp, rd->country);
466		if (cc == NULL)
467			errx(1, "unknown ISO country code %d", rd->country);
468		if (cc->rd->sku != rd->regdomain) {
469			const struct regdomain *rp;
470			/*
471			 * Check if country is incompatible with regdomain.
472			 * To enable multiple regdomains for a country code
473			 * we permit a mismatch between the regdomain and
474			 * the country's associated regdomain when the
475			 * regdomain is setup w/o a default country.  For
476			 * example, US is bound to the FCC regdomain but
477			 * we allow US to be combined with FCC3 because FCC3
478			 * has not default country.  This allows bogus
479			 * combinations like FCC3+DK which are resolved when
480			 * constructing the channel list by deferring to the
481			 * regdomain to construct the channel list.
482			 */
483			rp = lib80211_regdomain_findbysku(rdp, rd->regdomain);
484			if (rp == NULL)
485				errx(1, "country %s (%s) is not usable with "
486				    "regdomain %d", cc->isoname, cc->name,
487				    rd->regdomain);
488			else if (rp->cc != NULL && rp->cc != cc)
489				errx(1, "country %s (%s) is not usable with "
490				   "regdomain %s", cc->isoname, cc->name,
491				   rp->name);
492		}
493	}
494	/*
495	 * Fetch the device capabilities and calculate the
496	 * full set of netbands for which we request a new
497	 * channel list be constructed.  Once that's done we
498	 * push the regdomain info + channel list to the kernel.
499	 */
500	dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
501	if (dc == NULL)
502		errx(1, "no space for device capabilities");
503	dc->dc_chaninfo.ic_nchans = MAXCHAN;
504	getdevcaps(s, dc);
505#if 0
506	if (verbose) {
507		printf("drivercaps: 0x%x\n", dc->dc_drivercaps);
508		printf("cryptocaps: 0x%x\n", dc->dc_cryptocaps);
509		printf("htcaps    : 0x%x\n", dc->dc_htcaps);
510		memcpy(chaninfo, &dc->dc_chaninfo,
511		    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
512		print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, 1/*verbose*/);
513	}
514#endif
515	req = malloc(IEEE80211_REGDOMAIN_SIZE(dc->dc_chaninfo.ic_nchans));
516	if (req == NULL)
517		errx(1, "no space for regdomain request");
518	req->rd = *rd;
519	regdomain_makechannels(req, dc);
520	if (verbose) {
521		LINE_INIT(':');
522		print_regdomain(rd, 1/*verbose*/);
523		LINE_BREAK();
524		/* blech, reallocate channel list for new data */
525		if (chaninfo != NULL)
526			free(chaninfo);
527		chaninfo = malloc(IEEE80211_CHANINFO_SPACE(&req->chaninfo));
528		if (chaninfo == NULL)
529			errx(1, "no space for channel list");
530		memcpy(chaninfo, &req->chaninfo,
531		    IEEE80211_CHANINFO_SPACE(&req->chaninfo));
532		print_channels(s, &req->chaninfo, 1/*allchans*/, 1/*verbose*/);
533	}
534	if (req->chaninfo.ic_nchans == 0)
535		errx(1, "no channels calculated");
536	set80211(s, IEEE80211_IOC_REGDOMAIN, 0,
537	    IEEE80211_REGDOMAIN_SPACE(req), req);
538	free(req);
539	free(dc);
540}
541
542static int
543ieee80211_mhz2ieee(int freq, int flags)
544{
545	struct ieee80211_channel chan;
546	mapfreq(&chan, freq, flags);
547	return chan.ic_ieee;
548}
549
550static int
551isanyarg(const char *arg)
552{
553	return (strncmp(arg, "-", 1) == 0 ||
554	    strncasecmp(arg, "any", 3) == 0 || strncasecmp(arg, "off", 3) == 0);
555}
556
557static void
558set80211ssid(const char *val, int d, int s, const struct afswtch *rafp)
559{
560	int		ssid;
561	int		len;
562	u_int8_t	data[IEEE80211_NWID_LEN];
563
564	ssid = 0;
565	len = strlen(val);
566	if (len > 2 && isdigit((int)val[0]) && val[1] == ':') {
567		ssid = atoi(val)-1;
568		val += 2;
569	}
570
571	bzero(data, sizeof(data));
572	len = sizeof(data);
573	if (get_string(val, NULL, data, &len) == NULL)
574		exit(1);
575
576	set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
577}
578
579static void
580set80211meshid(const char *val, int d, int s, const struct afswtch *rafp)
581{
582	int		len;
583	u_int8_t	data[IEEE80211_NWID_LEN];
584
585	memset(data, 0, sizeof(data));
586	len = sizeof(data);
587	if (get_string(val, NULL, data, &len) == NULL)
588		exit(1);
589
590	set80211(s, IEEE80211_IOC_MESH_ID, 0, len, data);
591}
592
593static void
594set80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
595{
596	int			len;
597	u_int8_t		data[33];
598
599	bzero(data, sizeof(data));
600	len = sizeof(data);
601	get_string(val, NULL, data, &len);
602
603	set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
604}
605
606/*
607 * Parse a channel specification for attributes/flags.
608 * The syntax is:
609 *	freq/xx		channel width (5,10,20,40,40+,40-)
610 *	freq:mode	channel mode (a,b,g,h,n,t,s,d)
611 *
612 * These can be combined in either order; e.g. 2437:ng/40.
613 * Modes are case insensitive.
614 *
615 * The result is not validated here; it's assumed to be
616 * checked against the channel table fetched from the kernel.
617 */
618static int
619getchannelflags(const char *val, int freq)
620{
621#define	_CHAN_HT	0x80000000
622	const char *cp;
623	int flags;
624
625	flags = 0;
626
627	cp = strchr(val, ':');
628	if (cp != NULL) {
629		for (cp++; isalpha((int) *cp); cp++) {
630			/* accept mixed case */
631			int c = *cp;
632			if (isupper(c))
633				c = tolower(c);
634			switch (c) {
635			case 'a':		/* 802.11a */
636				flags |= IEEE80211_CHAN_A;
637				break;
638			case 'b':		/* 802.11b */
639				flags |= IEEE80211_CHAN_B;
640				break;
641			case 'g':		/* 802.11g */
642				flags |= IEEE80211_CHAN_G;
643				break;
644			case 'h':		/* ht = 802.11n */
645			case 'n':		/* 802.11n */
646				flags |= _CHAN_HT;	/* NB: private */
647				break;
648			case 'd':		/* dt = Atheros Dynamic Turbo */
649				flags |= IEEE80211_CHAN_TURBO;
650				break;
651			case 't':		/* ht, dt, st, t */
652				/* dt and unadorned t specify Dynamic Turbo */
653				if ((flags & (IEEE80211_CHAN_STURBO|_CHAN_HT)) == 0)
654					flags |= IEEE80211_CHAN_TURBO;
655				break;
656			case 's':		/* st = Atheros Static Turbo */
657				flags |= IEEE80211_CHAN_STURBO;
658				break;
659			default:
660				errx(-1, "%s: Invalid channel attribute %c\n",
661				    val, *cp);
662			}
663		}
664	}
665	cp = strchr(val, '/');
666	if (cp != NULL) {
667		char *ep;
668		u_long cw = strtoul(cp+1, &ep, 10);
669
670		switch (cw) {
671		case 5:
672			flags |= IEEE80211_CHAN_QUARTER;
673			break;
674		case 10:
675			flags |= IEEE80211_CHAN_HALF;
676			break;
677		case 20:
678			/* NB: this may be removed below */
679			flags |= IEEE80211_CHAN_HT20;
680			break;
681		case 40:
682			if (ep != NULL && *ep == '+')
683				flags |= IEEE80211_CHAN_HT40U;
684			else if (ep != NULL && *ep == '-')
685				flags |= IEEE80211_CHAN_HT40D;
686			break;
687		default:
688			errx(-1, "%s: Invalid channel width\n", val);
689		}
690	}
691	/*
692	 * Cleanup specifications.
693	 */
694	if ((flags & _CHAN_HT) == 0) {
695		/*
696		 * If user specified freq/20 or freq/40 quietly remove
697		 * HT cw attributes depending on channel use.  To give
698		 * an explicit 20/40 width for an HT channel you must
699		 * indicate it is an HT channel since all HT channels
700		 * are also usable for legacy operation; e.g. freq:n/40.
701		 */
702		flags &= ~IEEE80211_CHAN_HT;
703	} else {
704		/*
705		 * Remove private indicator that this is an HT channel
706		 * and if no explicit channel width has been given
707		 * provide the default settings.
708		 */
709		flags &= ~_CHAN_HT;
710		if ((flags & IEEE80211_CHAN_HT) == 0) {
711			struct ieee80211_channel chan;
712			/*
713			 * Consult the channel list to see if we can use
714			 * HT40+ or HT40- (if both the map routines choose).
715			 */
716			if (freq > 255)
717				mapfreq(&chan, freq, 0);
718			else
719				mapchan(&chan, freq, 0);
720			flags |= (chan.ic_flags & IEEE80211_CHAN_HT);
721		}
722	}
723	return flags;
724#undef _CHAN_HT
725}
726
727static void
728getchannel(int s, struct ieee80211_channel *chan, const char *val)
729{
730	int v, flags;
731	char *eptr;
732
733	memset(chan, 0, sizeof(*chan));
734	if (isanyarg(val)) {
735		chan->ic_freq = IEEE80211_CHAN_ANY;
736		return;
737	}
738	getchaninfo(s);
739	errno = 0;
740	v = strtol(val, &eptr, 10);
741	if (val[0] == '\0' || val == eptr || errno == ERANGE ||
742	    /* channel may be suffixed with nothing, :flag, or /width */
743	    (eptr[0] != '\0' && eptr[0] != ':' && eptr[0] != '/'))
744		errx(1, "invalid channel specification%s",
745		    errno == ERANGE ? " (out of range)" : "");
746	flags = getchannelflags(val, v);
747	if (v > 255) {		/* treat as frequency */
748		mapfreq(chan, v, flags);
749	} else {
750		mapchan(chan, v, flags);
751	}
752}
753
754static void
755set80211channel(const char *val, int d, int s, const struct afswtch *rafp)
756{
757	struct ieee80211_channel chan;
758
759	getchannel(s, &chan, val);
760	set80211(s, IEEE80211_IOC_CURCHAN, 0, sizeof(chan), &chan);
761}
762
763static void
764set80211chanswitch(const char *val, int d, int s, const struct afswtch *rafp)
765{
766	struct ieee80211_chanswitch_req csr;
767
768	getchannel(s, &csr.csa_chan, val);
769	csr.csa_mode = 1;
770	csr.csa_count = 5;
771	set80211(s, IEEE80211_IOC_CHANSWITCH, 0, sizeof(csr), &csr);
772}
773
774static void
775set80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
776{
777	int	mode;
778
779	if (strcasecmp(val, "none") == 0) {
780		mode = IEEE80211_AUTH_NONE;
781	} else if (strcasecmp(val, "open") == 0) {
782		mode = IEEE80211_AUTH_OPEN;
783	} else if (strcasecmp(val, "shared") == 0) {
784		mode = IEEE80211_AUTH_SHARED;
785	} else if (strcasecmp(val, "8021x") == 0) {
786		mode = IEEE80211_AUTH_8021X;
787	} else if (strcasecmp(val, "wpa") == 0) {
788		mode = IEEE80211_AUTH_WPA;
789	} else {
790		errx(1, "unknown authmode");
791	}
792
793	set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
794}
795
796static void
797set80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
798{
799	int	mode;
800
801	if (strcasecmp(val, "off") == 0) {
802		mode = IEEE80211_POWERSAVE_OFF;
803	} else if (strcasecmp(val, "on") == 0) {
804		mode = IEEE80211_POWERSAVE_ON;
805	} else if (strcasecmp(val, "cam") == 0) {
806		mode = IEEE80211_POWERSAVE_CAM;
807	} else if (strcasecmp(val, "psp") == 0) {
808		mode = IEEE80211_POWERSAVE_PSP;
809	} else if (strcasecmp(val, "psp-cam") == 0) {
810		mode = IEEE80211_POWERSAVE_PSP_CAM;
811	} else {
812		errx(1, "unknown powersavemode");
813	}
814
815	set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
816}
817
818static void
819set80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
820{
821	if (d == 0)
822		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
823		    0, NULL);
824	else
825		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
826		    0, NULL);
827}
828
829static void
830set80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
831{
832	set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
833}
834
835static void
836set80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
837{
838	int	mode;
839
840	if (strcasecmp(val, "off") == 0) {
841		mode = IEEE80211_WEP_OFF;
842	} else if (strcasecmp(val, "on") == 0) {
843		mode = IEEE80211_WEP_ON;
844	} else if (strcasecmp(val, "mixed") == 0) {
845		mode = IEEE80211_WEP_MIXED;
846	} else {
847		errx(1, "unknown wep mode");
848	}
849
850	set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
851}
852
853static void
854set80211wep(const char *val, int d, int s, const struct afswtch *rafp)
855{
856	set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
857}
858
859static int
860isundefarg(const char *arg)
861{
862	return (strcmp(arg, "-") == 0 || strncasecmp(arg, "undef", 5) == 0);
863}
864
865static void
866set80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
867{
868	if (isundefarg(val))
869		set80211(s, IEEE80211_IOC_WEPTXKEY, IEEE80211_KEYIX_NONE, 0, NULL);
870	else
871		set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
872}
873
874static void
875set80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
876{
877	int		key = 0;
878	int		len;
879	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
880
881	if (isdigit((int)val[0]) && val[1] == ':') {
882		key = atoi(val)-1;
883		val += 2;
884	}
885
886	bzero(data, sizeof(data));
887	len = sizeof(data);
888	get_string(val, NULL, data, &len);
889
890	set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
891}
892
893/*
894 * This function is purely a NetBSD compatability interface.  The NetBSD
895 * interface is too inflexible, but it's there so we'll support it since
896 * it's not all that hard.
897 */
898static void
899set80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
900{
901	int		txkey;
902	int		i, len;
903	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
904
905	set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
906
907	if (isdigit((int)val[0]) && val[1] == ':') {
908		txkey = val[0]-'0'-1;
909		val += 2;
910
911		for (i = 0; i < 4; i++) {
912			bzero(data, sizeof(data));
913			len = sizeof(data);
914			val = get_string(val, ",", data, &len);
915			if (val == NULL)
916				exit(1);
917
918			set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
919		}
920	} else {
921		bzero(data, sizeof(data));
922		len = sizeof(data);
923		get_string(val, NULL, data, &len);
924		txkey = 0;
925
926		set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
927
928		bzero(data, sizeof(data));
929		for (i = 1; i < 4; i++)
930			set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
931	}
932
933	set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
934}
935
936static void
937set80211rtsthreshold(const char *val, int d, int s, const struct afswtch *rafp)
938{
939	set80211(s, IEEE80211_IOC_RTSTHRESHOLD,
940		isundefarg(val) ? IEEE80211_RTS_MAX : atoi(val), 0, NULL);
941}
942
943static void
944set80211protmode(const char *val, int d, int s, const struct afswtch *rafp)
945{
946	int	mode;
947
948	if (strcasecmp(val, "off") == 0) {
949		mode = IEEE80211_PROTMODE_OFF;
950	} else if (strcasecmp(val, "cts") == 0) {
951		mode = IEEE80211_PROTMODE_CTS;
952	} else if (strncasecmp(val, "rtscts", 3) == 0) {
953		mode = IEEE80211_PROTMODE_RTSCTS;
954	} else {
955		errx(1, "unknown protection mode");
956	}
957
958	set80211(s, IEEE80211_IOC_PROTMODE, mode, 0, NULL);
959}
960
961static void
962set80211htprotmode(const char *val, int d, int s, const struct afswtch *rafp)
963{
964	int	mode;
965
966	if (strcasecmp(val, "off") == 0) {
967		mode = IEEE80211_PROTMODE_OFF;
968	} else if (strncasecmp(val, "rts", 3) == 0) {
969		mode = IEEE80211_PROTMODE_RTSCTS;
970	} else {
971		errx(1, "unknown protection mode");
972	}
973
974	set80211(s, IEEE80211_IOC_HTPROTMODE, mode, 0, NULL);
975}
976
977static void
978set80211txpower(const char *val, int d, int s, const struct afswtch *rafp)
979{
980	double v = atof(val);
981	int txpow;
982
983	txpow = (int) (2*v);
984	if (txpow != 2*v)
985		errx(-1, "invalid tx power (must be .5 dBm units)");
986	set80211(s, IEEE80211_IOC_TXPOWER, txpow, 0, NULL);
987}
988
989#define	IEEE80211_ROAMING_DEVICE	0
990#define	IEEE80211_ROAMING_AUTO		1
991#define	IEEE80211_ROAMING_MANUAL	2
992
993static void
994set80211roaming(const char *val, int d, int s, const struct afswtch *rafp)
995{
996	int mode;
997
998	if (strcasecmp(val, "device") == 0) {
999		mode = IEEE80211_ROAMING_DEVICE;
1000	} else if (strcasecmp(val, "auto") == 0) {
1001		mode = IEEE80211_ROAMING_AUTO;
1002	} else if (strcasecmp(val, "manual") == 0) {
1003		mode = IEEE80211_ROAMING_MANUAL;
1004	} else {
1005		errx(1, "unknown roaming mode");
1006	}
1007	set80211(s, IEEE80211_IOC_ROAMING, mode, 0, NULL);
1008}
1009
1010static void
1011set80211wme(const char *val, int d, int s, const struct afswtch *rafp)
1012{
1013	set80211(s, IEEE80211_IOC_WME, d, 0, NULL);
1014}
1015
1016static void
1017set80211hidessid(const char *val, int d, int s, const struct afswtch *rafp)
1018{
1019	set80211(s, IEEE80211_IOC_HIDESSID, d, 0, NULL);
1020}
1021
1022static void
1023set80211apbridge(const char *val, int d, int s, const struct afswtch *rafp)
1024{
1025	set80211(s, IEEE80211_IOC_APBRIDGE, d, 0, NULL);
1026}
1027
1028static void
1029set80211fastframes(const char *val, int d, int s, const struct afswtch *rafp)
1030{
1031	set80211(s, IEEE80211_IOC_FF, d, 0, NULL);
1032}
1033
1034static void
1035set80211dturbo(const char *val, int d, int s, const struct afswtch *rafp)
1036{
1037	set80211(s, IEEE80211_IOC_TURBOP, d, 0, NULL);
1038}
1039
1040static void
1041set80211chanlist(const char *val, int d, int s, const struct afswtch *rafp)
1042{
1043	struct ieee80211req_chanlist chanlist;
1044	char *temp, *cp, *tp;
1045
1046	temp = malloc(strlen(val) + 1);
1047	if (temp == NULL)
1048		errx(1, "malloc failed");
1049	strcpy(temp, val);
1050	memset(&chanlist, 0, sizeof(chanlist));
1051	cp = temp;
1052	for (;;) {
1053		int first, last, f, c;
1054
1055		tp = strchr(cp, ',');
1056		if (tp != NULL)
1057			*tp++ = '\0';
1058		switch (sscanf(cp, "%u-%u", &first, &last)) {
1059		case 1:
1060			if (first > IEEE80211_CHAN_MAX)
1061				errx(-1, "channel %u out of range, max %u",
1062					first, IEEE80211_CHAN_MAX);
1063			setbit(chanlist.ic_channels, first);
1064			break;
1065		case 2:
1066			if (first > IEEE80211_CHAN_MAX)
1067				errx(-1, "channel %u out of range, max %u",
1068					first, IEEE80211_CHAN_MAX);
1069			if (last > IEEE80211_CHAN_MAX)
1070				errx(-1, "channel %u out of range, max %u",
1071					last, IEEE80211_CHAN_MAX);
1072			if (first > last)
1073				errx(-1, "void channel range, %u > %u",
1074					first, last);
1075			for (f = first; f <= last; f++)
1076				setbit(chanlist.ic_channels, f);
1077			break;
1078		}
1079		if (tp == NULL)
1080			break;
1081		c = *tp;
1082		while (isspace(c))
1083			tp++;
1084		if (!isdigit(c))
1085			break;
1086		cp = tp;
1087	}
1088	set80211(s, IEEE80211_IOC_CHANLIST, 0, sizeof(chanlist), &chanlist);
1089}
1090
1091static void
1092set80211bssid(const char *val, int d, int s, const struct afswtch *rafp)
1093{
1094
1095	if (!isanyarg(val)) {
1096		char *temp;
1097		struct sockaddr_dl sdl;
1098
1099		temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1100		if (temp == NULL)
1101			errx(1, "malloc failed");
1102		temp[0] = ':';
1103		strcpy(temp + 1, val);
1104		sdl.sdl_len = sizeof(sdl);
1105		link_addr(temp, &sdl);
1106		free(temp);
1107		if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1108			errx(1, "malformed link-level address");
1109		set80211(s, IEEE80211_IOC_BSSID, 0,
1110			IEEE80211_ADDR_LEN, LLADDR(&sdl));
1111	} else {
1112		uint8_t zerobssid[IEEE80211_ADDR_LEN];
1113		memset(zerobssid, 0, sizeof(zerobssid));
1114		set80211(s, IEEE80211_IOC_BSSID, 0,
1115			IEEE80211_ADDR_LEN, zerobssid);
1116	}
1117}
1118
1119static int
1120getac(const char *ac)
1121{
1122	if (strcasecmp(ac, "ac_be") == 0 || strcasecmp(ac, "be") == 0)
1123		return WME_AC_BE;
1124	if (strcasecmp(ac, "ac_bk") == 0 || strcasecmp(ac, "bk") == 0)
1125		return WME_AC_BK;
1126	if (strcasecmp(ac, "ac_vi") == 0 || strcasecmp(ac, "vi") == 0)
1127		return WME_AC_VI;
1128	if (strcasecmp(ac, "ac_vo") == 0 || strcasecmp(ac, "vo") == 0)
1129		return WME_AC_VO;
1130	errx(1, "unknown wme access class %s", ac);
1131}
1132
1133static
1134DECL_CMD_FUNC2(set80211cwmin, ac, val)
1135{
1136	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val), getac(ac), NULL);
1137}
1138
1139static
1140DECL_CMD_FUNC2(set80211cwmax, ac, val)
1141{
1142	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val), getac(ac), NULL);
1143}
1144
1145static
1146DECL_CMD_FUNC2(set80211aifs, ac, val)
1147{
1148	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val), getac(ac), NULL);
1149}
1150
1151static
1152DECL_CMD_FUNC2(set80211txoplimit, ac, val)
1153{
1154	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), getac(ac), NULL);
1155}
1156
1157static
1158DECL_CMD_FUNC(set80211acm, ac, d)
1159{
1160	set80211(s, IEEE80211_IOC_WME_ACM, 1, getac(ac), NULL);
1161}
1162static
1163DECL_CMD_FUNC(set80211noacm, ac, d)
1164{
1165	set80211(s, IEEE80211_IOC_WME_ACM, 0, getac(ac), NULL);
1166}
1167
1168static
1169DECL_CMD_FUNC(set80211ackpolicy, ac, d)
1170{
1171	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 1, getac(ac), NULL);
1172}
1173static
1174DECL_CMD_FUNC(set80211noackpolicy, ac, d)
1175{
1176	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 0, getac(ac), NULL);
1177}
1178
1179static
1180DECL_CMD_FUNC2(set80211bsscwmin, ac, val)
1181{
1182	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val),
1183		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1184}
1185
1186static
1187DECL_CMD_FUNC2(set80211bsscwmax, ac, val)
1188{
1189	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val),
1190		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1191}
1192
1193static
1194DECL_CMD_FUNC2(set80211bssaifs, ac, val)
1195{
1196	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val),
1197		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1198}
1199
1200static
1201DECL_CMD_FUNC2(set80211bsstxoplimit, ac, val)
1202{
1203	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val),
1204		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
1205}
1206
1207static
1208DECL_CMD_FUNC(set80211dtimperiod, val, d)
1209{
1210	set80211(s, IEEE80211_IOC_DTIM_PERIOD, atoi(val), 0, NULL);
1211}
1212
1213static
1214DECL_CMD_FUNC(set80211bintval, val, d)
1215{
1216	set80211(s, IEEE80211_IOC_BEACON_INTERVAL, atoi(val), 0, NULL);
1217}
1218
1219static void
1220set80211macmac(int s, int op, const char *val)
1221{
1222	char *temp;
1223	struct sockaddr_dl sdl;
1224
1225	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1226	if (temp == NULL)
1227		errx(1, "malloc failed");
1228	temp[0] = ':';
1229	strcpy(temp + 1, val);
1230	sdl.sdl_len = sizeof(sdl);
1231	link_addr(temp, &sdl);
1232	free(temp);
1233	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1234		errx(1, "malformed link-level address");
1235	set80211(s, op, 0, IEEE80211_ADDR_LEN, LLADDR(&sdl));
1236}
1237
1238static
1239DECL_CMD_FUNC(set80211addmac, val, d)
1240{
1241	set80211macmac(s, IEEE80211_IOC_ADDMAC, val);
1242}
1243
1244static
1245DECL_CMD_FUNC(set80211delmac, val, d)
1246{
1247	set80211macmac(s, IEEE80211_IOC_DELMAC, val);
1248}
1249
1250static
1251DECL_CMD_FUNC(set80211kickmac, val, d)
1252{
1253	char *temp;
1254	struct sockaddr_dl sdl;
1255	struct ieee80211req_mlme mlme;
1256
1257	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1258	if (temp == NULL)
1259		errx(1, "malloc failed");
1260	temp[0] = ':';
1261	strcpy(temp + 1, val);
1262	sdl.sdl_len = sizeof(sdl);
1263	link_addr(temp, &sdl);
1264	free(temp);
1265	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1266		errx(1, "malformed link-level address");
1267	memset(&mlme, 0, sizeof(mlme));
1268	mlme.im_op = IEEE80211_MLME_DEAUTH;
1269	mlme.im_reason = IEEE80211_REASON_AUTH_EXPIRE;
1270	memcpy(mlme.im_macaddr, LLADDR(&sdl), IEEE80211_ADDR_LEN);
1271	set80211(s, IEEE80211_IOC_MLME, 0, sizeof(mlme), &mlme);
1272}
1273
1274static
1275DECL_CMD_FUNC(set80211maccmd, val, d)
1276{
1277	set80211(s, IEEE80211_IOC_MACCMD, d, 0, NULL);
1278}
1279
1280static void
1281set80211meshrtmac(int s, int req, const char *val)
1282{
1283	char *temp;
1284	struct sockaddr_dl sdl;
1285
1286	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
1287	if (temp == NULL)
1288		errx(1, "malloc failed");
1289	temp[0] = ':';
1290	strcpy(temp + 1, val);
1291	sdl.sdl_len = sizeof(sdl);
1292	link_addr(temp, &sdl);
1293	free(temp);
1294	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
1295		errx(1, "malformed link-level address");
1296	set80211(s, IEEE80211_IOC_MESH_RTCMD, req,
1297	    IEEE80211_ADDR_LEN, LLADDR(&sdl));
1298}
1299
1300static
1301DECL_CMD_FUNC(set80211addmeshrt, val, d)
1302{
1303	set80211meshrtmac(s, IEEE80211_MESH_RTCMD_ADD, val);
1304}
1305
1306static
1307DECL_CMD_FUNC(set80211delmeshrt, val, d)
1308{
1309	set80211meshrtmac(s, IEEE80211_MESH_RTCMD_DELETE, val);
1310}
1311
1312static
1313DECL_CMD_FUNC(set80211meshrtcmd, val, d)
1314{
1315	set80211(s, IEEE80211_IOC_MESH_RTCMD, d, 0, NULL);
1316}
1317
1318static
1319DECL_CMD_FUNC(set80211hwmprootmode, val, d)
1320{
1321	int mode;
1322
1323	if (strcasecmp(val, "normal") == 0)
1324		mode = IEEE80211_HWMP_ROOTMODE_NORMAL;
1325	else if (strcasecmp(val, "proactive") == 0)
1326		mode = IEEE80211_HWMP_ROOTMODE_PROACTIVE;
1327	else if (strcasecmp(val, "rann") == 0)
1328		mode = IEEE80211_HWMP_ROOTMODE_RANN;
1329	else
1330		mode = IEEE80211_HWMP_ROOTMODE_DISABLED;
1331	set80211(s, IEEE80211_IOC_HWMP_ROOTMODE, mode, 0, NULL);
1332}
1333
1334static
1335DECL_CMD_FUNC(set80211hwmpmaxhops, val, d)
1336{
1337	set80211(s, IEEE80211_IOC_HWMP_MAXHOPS, atoi(val), 0, NULL);
1338}
1339
1340static void
1341set80211pureg(const char *val, int d, int s, const struct afswtch *rafp)
1342{
1343	set80211(s, IEEE80211_IOC_PUREG, d, 0, NULL);
1344}
1345
1346static void
1347set80211bgscan(const char *val, int d, int s, const struct afswtch *rafp)
1348{
1349	set80211(s, IEEE80211_IOC_BGSCAN, d, 0, NULL);
1350}
1351
1352static
1353DECL_CMD_FUNC(set80211bgscanidle, val, d)
1354{
1355	set80211(s, IEEE80211_IOC_BGSCAN_IDLE, atoi(val), 0, NULL);
1356}
1357
1358static
1359DECL_CMD_FUNC(set80211bgscanintvl, val, d)
1360{
1361	set80211(s, IEEE80211_IOC_BGSCAN_INTERVAL, atoi(val), 0, NULL);
1362}
1363
1364static
1365DECL_CMD_FUNC(set80211scanvalid, val, d)
1366{
1367	set80211(s, IEEE80211_IOC_SCANVALID, atoi(val), 0, NULL);
1368}
1369
1370/*
1371 * Parse an optional trailing specification of which netbands
1372 * to apply a parameter to.  This is basically the same syntax
1373 * as used for channels but you can concatenate to specify
1374 * multiple.  For example:
1375 *	14:abg		apply to 11a, 11b, and 11g
1376 *	6:ht		apply to 11na and 11ng
1377 * We don't make a big effort to catch silly things; this is
1378 * really a convenience mechanism.
1379 */
1380static int
1381getmodeflags(const char *val)
1382{
1383	const char *cp;
1384	int flags;
1385
1386	flags = 0;
1387
1388	cp = strchr(val, ':');
1389	if (cp != NULL) {
1390		for (cp++; isalpha((int) *cp); cp++) {
1391			/* accept mixed case */
1392			int c = *cp;
1393			if (isupper(c))
1394				c = tolower(c);
1395			switch (c) {
1396			case 'a':		/* 802.11a */
1397				flags |= IEEE80211_CHAN_A;
1398				break;
1399			case 'b':		/* 802.11b */
1400				flags |= IEEE80211_CHAN_B;
1401				break;
1402			case 'g':		/* 802.11g */
1403				flags |= IEEE80211_CHAN_G;
1404				break;
1405			case 'n':		/* 802.11n */
1406				flags |= IEEE80211_CHAN_HT;
1407				break;
1408			case 'd':		/* dt = Atheros Dynamic Turbo */
1409				flags |= IEEE80211_CHAN_TURBO;
1410				break;
1411			case 't':		/* ht, dt, st, t */
1412				/* dt and unadorned t specify Dynamic Turbo */
1413				if ((flags & (IEEE80211_CHAN_STURBO|IEEE80211_CHAN_HT)) == 0)
1414					flags |= IEEE80211_CHAN_TURBO;
1415				break;
1416			case 's':		/* st = Atheros Static Turbo */
1417				flags |= IEEE80211_CHAN_STURBO;
1418				break;
1419			case 'h':		/* 1/2-width channels */
1420				flags |= IEEE80211_CHAN_HALF;
1421				break;
1422			case 'q':		/* 1/4-width channels */
1423				flags |= IEEE80211_CHAN_QUARTER;
1424				break;
1425			default:
1426				errx(-1, "%s: Invalid mode attribute %c\n",
1427				    val, *cp);
1428			}
1429		}
1430	}
1431	return flags;
1432}
1433
1434#define	IEEE80211_CHAN_HTA	(IEEE80211_CHAN_HT|IEEE80211_CHAN_5GHZ)
1435#define	IEEE80211_CHAN_HTG	(IEEE80211_CHAN_HT|IEEE80211_CHAN_2GHZ)
1436
1437#define	_APPLY(_flags, _base, _param, _v) do {				\
1438    if (_flags & IEEE80211_CHAN_HT) {					\
1439	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1440		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1441		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1442	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1443		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1444	    else							\
1445		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1446    }									\
1447    if (_flags & IEEE80211_CHAN_TURBO) {				\
1448	    if ((_flags & (IEEE80211_CHAN_5GHZ|IEEE80211_CHAN_2GHZ)) == 0) {\
1449		    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;	\
1450		    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;	\
1451	    } else if (_flags & IEEE80211_CHAN_5GHZ)			\
1452		    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;	\
1453	    else							\
1454		    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;	\
1455    }									\
1456    if (_flags & IEEE80211_CHAN_STURBO)					\
1457	    _base.params[IEEE80211_MODE_STURBO_A]._param = _v;		\
1458    if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1459	    _base.params[IEEE80211_MODE_11A]._param = _v;		\
1460    if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1461	    _base.params[IEEE80211_MODE_11G]._param = _v;		\
1462    if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1463	    _base.params[IEEE80211_MODE_11B]._param = _v;		\
1464    if (_flags & IEEE80211_CHAN_HALF)					\
1465	    _base.params[IEEE80211_MODE_HALF]._param = _v;		\
1466    if (_flags & IEEE80211_CHAN_QUARTER)				\
1467	    _base.params[IEEE80211_MODE_QUARTER]._param = _v;		\
1468} while (0)
1469#define	_APPLY1(_flags, _base, _param, _v) do {				\
1470    if (_flags & IEEE80211_CHAN_HT) {					\
1471	    if (_flags & IEEE80211_CHAN_5GHZ)				\
1472		    _base.params[IEEE80211_MODE_11NA]._param = _v;	\
1473	    else							\
1474		    _base.params[IEEE80211_MODE_11NG]._param = _v;	\
1475    } else if ((_flags & IEEE80211_CHAN_108A) == IEEE80211_CHAN_108A)	\
1476	    _base.params[IEEE80211_MODE_TURBO_A]._param = _v;		\
1477    else if ((_flags & IEEE80211_CHAN_108G) == IEEE80211_CHAN_108G)	\
1478	    _base.params[IEEE80211_MODE_TURBO_G]._param = _v;		\
1479    else if ((_flags & IEEE80211_CHAN_ST) == IEEE80211_CHAN_ST)		\
1480	    _base.params[IEEE80211_MODE_STURBO_A]._param = _v;		\
1481    else if (_flags & IEEE80211_CHAN_HALF)				\
1482	    _base.params[IEEE80211_MODE_HALF]._param = _v;		\
1483    else if (_flags & IEEE80211_CHAN_QUARTER)				\
1484	    _base.params[IEEE80211_MODE_QUARTER]._param = _v;		\
1485    else if ((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)		\
1486	    _base.params[IEEE80211_MODE_11A]._param = _v;		\
1487    else if ((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)		\
1488	    _base.params[IEEE80211_MODE_11G]._param = _v;		\
1489    else if ((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)		\
1490	    _base.params[IEEE80211_MODE_11B]._param = _v;		\
1491} while (0)
1492#define	_APPLY_RATE(_flags, _base, _param, _v) do {			\
1493    if (_flags & IEEE80211_CHAN_HT) {					\
1494	(_v) = (_v / 2) | IEEE80211_RATE_MCS;				\
1495    }									\
1496    _APPLY(_flags, _base, _param, _v);					\
1497} while (0)
1498#define	_APPLY_RATE1(_flags, _base, _param, _v) do {			\
1499    if (_flags & IEEE80211_CHAN_HT) {					\
1500	(_v) = (_v / 2) | IEEE80211_RATE_MCS;				\
1501    }									\
1502    _APPLY1(_flags, _base, _param, _v);					\
1503} while (0)
1504
1505static
1506DECL_CMD_FUNC(set80211roamrssi, val, d)
1507{
1508	double v = atof(val);
1509	int rssi, flags;
1510
1511	rssi = (int) (2*v);
1512	if (rssi != 2*v)
1513		errx(-1, "invalid rssi (must be .5 dBm units)");
1514	flags = getmodeflags(val);
1515	getroam(s);
1516	if (flags == 0) {		/* NB: no flags => current channel */
1517		flags = getcurchan(s)->ic_flags;
1518		_APPLY1(flags, roamparams, rssi, rssi);
1519	} else
1520		_APPLY(flags, roamparams, rssi, rssi);
1521	callback_register(setroam_cb, &roamparams);
1522}
1523
1524static int
1525getrate(const char *val, const char *tag)
1526{
1527	double v = atof(val);
1528	int rate;
1529
1530	rate = (int) (2*v);
1531	if (rate != 2*v)
1532		errx(-1, "invalid %s rate (must be .5 Mb/s units)", tag);
1533	return rate;		/* NB: returns 2x the specified value */
1534}
1535
1536static
1537DECL_CMD_FUNC(set80211roamrate, val, d)
1538{
1539	int rate, flags;
1540
1541	rate = getrate(val, "roam");
1542	flags = getmodeflags(val);
1543	getroam(s);
1544	if (flags == 0) {		/* NB: no flags => current channel */
1545		flags = getcurchan(s)->ic_flags;
1546		_APPLY_RATE1(flags, roamparams, rate, rate);
1547	} else
1548		_APPLY_RATE(flags, roamparams, rate, rate);
1549	callback_register(setroam_cb, &roamparams);
1550}
1551
1552static
1553DECL_CMD_FUNC(set80211mcastrate, val, d)
1554{
1555	int rate, flags;
1556
1557	rate = getrate(val, "mcast");
1558	flags = getmodeflags(val);
1559	gettxparams(s);
1560	if (flags == 0) {		/* NB: no flags => current channel */
1561		flags = getcurchan(s)->ic_flags;
1562		_APPLY_RATE1(flags, txparams, mcastrate, rate);
1563	} else
1564		_APPLY_RATE(flags, txparams, mcastrate, rate);
1565	callback_register(settxparams_cb, &txparams);
1566}
1567
1568static
1569DECL_CMD_FUNC(set80211mgtrate, val, d)
1570{
1571	int rate, flags;
1572
1573	rate = getrate(val, "mgmt");
1574	flags = getmodeflags(val);
1575	gettxparams(s);
1576	if (flags == 0) {		/* NB: no flags => current channel */
1577		flags = getcurchan(s)->ic_flags;
1578		_APPLY_RATE1(flags, txparams, mgmtrate, rate);
1579	} else
1580		_APPLY_RATE(flags, txparams, mgmtrate, rate);
1581	callback_register(settxparams_cb, &txparams);
1582}
1583
1584static
1585DECL_CMD_FUNC(set80211ucastrate, val, d)
1586{
1587	int flags;
1588
1589	gettxparams(s);
1590	flags = getmodeflags(val);
1591	if (isanyarg(val)) {
1592		if (flags == 0) {	/* NB: no flags => current channel */
1593			flags = getcurchan(s)->ic_flags;
1594			_APPLY1(flags, txparams, ucastrate,
1595			    IEEE80211_FIXED_RATE_NONE);
1596		} else
1597			_APPLY(flags, txparams, ucastrate,
1598			    IEEE80211_FIXED_RATE_NONE);
1599	} else {
1600		int rate = getrate(val, "ucast");
1601		if (flags == 0) {	/* NB: no flags => current channel */
1602			flags = getcurchan(s)->ic_flags;
1603			_APPLY_RATE1(flags, txparams, ucastrate, rate);
1604		} else
1605			_APPLY_RATE(flags, txparams, ucastrate, rate);
1606	}
1607	callback_register(settxparams_cb, &txparams);
1608}
1609
1610static
1611DECL_CMD_FUNC(set80211maxretry, val, d)
1612{
1613	int v = atoi(val), flags;
1614
1615	flags = getmodeflags(val);
1616	gettxparams(s);
1617	if (flags == 0) {		/* NB: no flags => current channel */
1618		flags = getcurchan(s)->ic_flags;
1619		_APPLY1(flags, txparams, maxretry, v);
1620	} else
1621		_APPLY(flags, txparams, maxretry, v);
1622	callback_register(settxparams_cb, &txparams);
1623}
1624#undef _APPLY_RATE
1625#undef _APPLY
1626#undef IEEE80211_CHAN_HTA
1627#undef IEEE80211_CHAN_HTG
1628
1629static
1630DECL_CMD_FUNC(set80211fragthreshold, val, d)
1631{
1632	set80211(s, IEEE80211_IOC_FRAGTHRESHOLD,
1633		isundefarg(val) ? IEEE80211_FRAG_MAX : atoi(val), 0, NULL);
1634}
1635
1636static
1637DECL_CMD_FUNC(set80211bmissthreshold, val, d)
1638{
1639	set80211(s, IEEE80211_IOC_BMISSTHRESHOLD,
1640		isundefarg(val) ? IEEE80211_HWBMISS_MAX : atoi(val), 0, NULL);
1641}
1642
1643static void
1644set80211burst(const char *val, int d, int s, const struct afswtch *rafp)
1645{
1646	set80211(s, IEEE80211_IOC_BURST, d, 0, NULL);
1647}
1648
1649static void
1650set80211doth(const char *val, int d, int s, const struct afswtch *rafp)
1651{
1652	set80211(s, IEEE80211_IOC_DOTH, d, 0, NULL);
1653}
1654
1655static void
1656set80211dfs(const char *val, int d, int s, const struct afswtch *rafp)
1657{
1658	set80211(s, IEEE80211_IOC_DFS, d, 0, NULL);
1659}
1660
1661static void
1662set80211shortgi(const char *val, int d, int s, const struct afswtch *rafp)
1663{
1664	set80211(s, IEEE80211_IOC_SHORTGI,
1665		d ? (IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40) : 0,
1666		0, NULL);
1667}
1668
1669static void
1670set80211ampdu(const char *val, int d, int s, const struct afswtch *rafp)
1671{
1672	int ampdu;
1673
1674	if (get80211val(s, IEEE80211_IOC_AMPDU, &ampdu) < 0)
1675		errx(-1, "cannot get AMPDU setting");
1676	if (d < 0) {
1677		d = -d;
1678		ampdu &= ~d;
1679	} else
1680		ampdu |= d;
1681	set80211(s, IEEE80211_IOC_AMPDU, ampdu, 0, NULL);
1682}
1683
1684static
1685DECL_CMD_FUNC(set80211ampdulimit, val, d)
1686{
1687	int v;
1688
1689	switch (atoi(val)) {
1690	case 8:
1691	case 8*1024:
1692		v = IEEE80211_HTCAP_MAXRXAMPDU_8K;
1693		break;
1694	case 16:
1695	case 16*1024:
1696		v = IEEE80211_HTCAP_MAXRXAMPDU_16K;
1697		break;
1698	case 32:
1699	case 32*1024:
1700		v = IEEE80211_HTCAP_MAXRXAMPDU_32K;
1701		break;
1702	case 64:
1703	case 64*1024:
1704		v = IEEE80211_HTCAP_MAXRXAMPDU_64K;
1705		break;
1706	default:
1707		errx(-1, "invalid A-MPDU limit %s", val);
1708	}
1709	set80211(s, IEEE80211_IOC_AMPDU_LIMIT, v, 0, NULL);
1710}
1711
1712static
1713DECL_CMD_FUNC(set80211ampdudensity, val, d)
1714{
1715	int v;
1716
1717	if (isanyarg(val) || strcasecmp(val, "na") == 0)
1718		v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1719	else switch ((int)(atof(val)*4)) {
1720	case 0:
1721		v = IEEE80211_HTCAP_MPDUDENSITY_NA;
1722		break;
1723	case 1:
1724		v = IEEE80211_HTCAP_MPDUDENSITY_025;
1725		break;
1726	case 2:
1727		v = IEEE80211_HTCAP_MPDUDENSITY_05;
1728		break;
1729	case 4:
1730		v = IEEE80211_HTCAP_MPDUDENSITY_1;
1731		break;
1732	case 8:
1733		v = IEEE80211_HTCAP_MPDUDENSITY_2;
1734		break;
1735	case 16:
1736		v = IEEE80211_HTCAP_MPDUDENSITY_4;
1737		break;
1738	case 32:
1739		v = IEEE80211_HTCAP_MPDUDENSITY_8;
1740		break;
1741	case 64:
1742		v = IEEE80211_HTCAP_MPDUDENSITY_16;
1743		break;
1744	default:
1745		errx(-1, "invalid A-MPDU density %s", val);
1746	}
1747	set80211(s, IEEE80211_IOC_AMPDU_DENSITY, v, 0, NULL);
1748}
1749
1750static void
1751set80211amsdu(const char *val, int d, int s, const struct afswtch *rafp)
1752{
1753	int amsdu;
1754
1755	if (get80211val(s, IEEE80211_IOC_AMSDU, &amsdu) < 0)
1756		err(-1, "cannot get AMSDU setting");
1757	if (d < 0) {
1758		d = -d;
1759		amsdu &= ~d;
1760	} else
1761		amsdu |= d;
1762	set80211(s, IEEE80211_IOC_AMSDU, amsdu, 0, NULL);
1763}
1764
1765static
1766DECL_CMD_FUNC(set80211amsdulimit, val, d)
1767{
1768	set80211(s, IEEE80211_IOC_AMSDU_LIMIT, atoi(val), 0, NULL);
1769}
1770
1771static void
1772set80211puren(const char *val, int d, int s, const struct afswtch *rafp)
1773{
1774	set80211(s, IEEE80211_IOC_PUREN, d, 0, NULL);
1775}
1776
1777static void
1778set80211htcompat(const char *val, int d, int s, const struct afswtch *rafp)
1779{
1780	set80211(s, IEEE80211_IOC_HTCOMPAT, d, 0, NULL);
1781}
1782
1783static void
1784set80211htconf(const char *val, int d, int s, const struct afswtch *rafp)
1785{
1786	set80211(s, IEEE80211_IOC_HTCONF, d, 0, NULL);
1787	htconf = d;
1788}
1789
1790static void
1791set80211dwds(const char *val, int d, int s, const struct afswtch *rafp)
1792{
1793	set80211(s, IEEE80211_IOC_DWDS, d, 0, NULL);
1794}
1795
1796static void
1797set80211inact(const char *val, int d, int s, const struct afswtch *rafp)
1798{
1799	set80211(s, IEEE80211_IOC_INACTIVITY, d, 0, NULL);
1800}
1801
1802static void
1803set80211tsn(const char *val, int d, int s, const struct afswtch *rafp)
1804{
1805	set80211(s, IEEE80211_IOC_TSN, d, 0, NULL);
1806}
1807
1808static void
1809set80211dotd(const char *val, int d, int s, const struct afswtch *rafp)
1810{
1811	set80211(s, IEEE80211_IOC_DOTD, d, 0, NULL);
1812}
1813
1814static void
1815set80211smps(const char *val, int d, int s, const struct afswtch *rafp)
1816{
1817	set80211(s, IEEE80211_IOC_SMPS, d, 0, NULL);
1818}
1819
1820static void
1821set80211rifs(const char *val, int d, int s, const struct afswtch *rafp)
1822{
1823	set80211(s, IEEE80211_IOC_RIFS, d, 0, NULL);
1824}
1825
1826static
1827DECL_CMD_FUNC(set80211tdmaslot, val, d)
1828{
1829	set80211(s, IEEE80211_IOC_TDMA_SLOT, atoi(val), 0, NULL);
1830}
1831
1832static
1833DECL_CMD_FUNC(set80211tdmaslotcnt, val, d)
1834{
1835	set80211(s, IEEE80211_IOC_TDMA_SLOTCNT, atoi(val), 0, NULL);
1836}
1837
1838static
1839DECL_CMD_FUNC(set80211tdmaslotlen, val, d)
1840{
1841	set80211(s, IEEE80211_IOC_TDMA_SLOTLEN, atoi(val), 0, NULL);
1842}
1843
1844static
1845DECL_CMD_FUNC(set80211tdmabintval, val, d)
1846{
1847	set80211(s, IEEE80211_IOC_TDMA_BINTERVAL, atoi(val), 0, NULL);
1848}
1849
1850static
1851DECL_CMD_FUNC(set80211meshttl, val, d)
1852{
1853	set80211(s, IEEE80211_IOC_MESH_TTL, atoi(val), 0, NULL);
1854}
1855
1856static
1857DECL_CMD_FUNC(set80211meshforward, val, d)
1858{
1859	set80211(s, IEEE80211_IOC_MESH_FWRD, atoi(val), 0, NULL);
1860}
1861
1862static
1863DECL_CMD_FUNC(set80211meshpeering, val, d)
1864{
1865	set80211(s, IEEE80211_IOC_MESH_AP, atoi(val), 0, NULL);
1866}
1867
1868static
1869DECL_CMD_FUNC(set80211meshmetric, val, d)
1870{
1871	char v[12];
1872
1873	memcpy(v, val, sizeof(v));
1874	set80211(s, IEEE80211_IOC_MESH_PR_METRIC, 0, 0, v);
1875}
1876
1877static
1878DECL_CMD_FUNC(set80211meshpath, val, d)
1879{
1880	char v[12];
1881
1882	memcpy(v, val, sizeof(v));
1883	set80211(s, IEEE80211_IOC_MESH_PR_PATH, 0, 0, v);
1884}
1885
1886static int
1887regdomain_sort(const void *a, const void *b)
1888{
1889#define	CHAN_ALL \
1890	(IEEE80211_CHAN_ALLTURBO|IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)
1891	const struct ieee80211_channel *ca = a;
1892	const struct ieee80211_channel *cb = b;
1893
1894	return ca->ic_freq == cb->ic_freq ?
1895	    (ca->ic_flags & CHAN_ALL) - (cb->ic_flags & CHAN_ALL) :
1896	    ca->ic_freq - cb->ic_freq;
1897#undef CHAN_ALL
1898}
1899
1900static const struct ieee80211_channel *
1901chanlookup(const struct ieee80211_channel chans[], int nchans,
1902	int freq, int flags)
1903{
1904	int i;
1905
1906	flags &= IEEE80211_CHAN_ALLTURBO;
1907	for (i = 0; i < nchans; i++) {
1908		const struct ieee80211_channel *c = &chans[i];
1909		if (c->ic_freq == freq &&
1910		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1911			return c;
1912	}
1913	return NULL;
1914}
1915
1916static int
1917chanfind(const struct ieee80211_channel chans[], int nchans, int flags)
1918{
1919	int i;
1920
1921	for (i = 0; i < nchans; i++) {
1922		const struct ieee80211_channel *c = &chans[i];
1923		if ((c->ic_flags & flags) == flags)
1924			return 1;
1925	}
1926	return 0;
1927}
1928
1929/*
1930 * Check channel compatibility.
1931 */
1932static int
1933checkchan(const struct ieee80211req_chaninfo *avail, int freq, int flags)
1934{
1935	flags &= ~REQ_FLAGS;
1936	/*
1937	 * Check if exact channel is in the calibration table;
1938	 * everything below is to deal with channels that we
1939	 * want to include but that are not explicitly listed.
1940	 */
1941	if (flags & IEEE80211_CHAN_HT40) {
1942		/* NB: we use an HT40 channel center that matches HT20 */
1943		flags = (flags &~ IEEE80211_CHAN_HT40) | IEEE80211_CHAN_HT20;
1944	}
1945	if (chanlookup(avail->ic_chans, avail->ic_nchans, freq, flags) != NULL)
1946		return 1;
1947	if (flags & IEEE80211_CHAN_GSM) {
1948		/*
1949		 * XXX GSM frequency mapping is handled in the kernel
1950		 * so we cannot find them in the calibration table;
1951		 * just accept the channel and the kernel will reject
1952		 * the channel list if it's wrong.
1953		 */
1954		return 1;
1955	}
1956	/*
1957	 * If this is a 1/2 or 1/4 width channel allow it if a full
1958	 * width channel is present for this frequency, and the device
1959	 * supports fractional channels on this band.  This is a hack
1960	 * that avoids bloating the calibration table; it may be better
1961	 * by per-band attributes though (we are effectively calculating
1962	 * this attribute by scanning the channel list ourself).
1963	 */
1964	if ((flags & (IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)) == 0)
1965		return 0;
1966	if (chanlookup(avail->ic_chans, avail->ic_nchans, freq,
1967	    flags &~ (IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)) == NULL)
1968		return 0;
1969	if (flags & IEEE80211_CHAN_HALF) {
1970		return chanfind(avail->ic_chans, avail->ic_nchans,
1971		    IEEE80211_CHAN_HALF |
1972		       (flags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ)));
1973	} else {
1974		return chanfind(avail->ic_chans, avail->ic_nchans,
1975		    IEEE80211_CHAN_QUARTER |
1976			(flags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ)));
1977	}
1978}
1979
1980static void
1981regdomain_addchans(struct ieee80211req_chaninfo *ci,
1982	const netband_head *bands,
1983	const struct ieee80211_regdomain *reg,
1984	uint32_t chanFlags,
1985	const struct ieee80211req_chaninfo *avail)
1986{
1987	const struct netband *nb;
1988	const struct freqband *b;
1989	struct ieee80211_channel *c, *prev;
1990	int freq, hi_adj, lo_adj, channelSep;
1991	uint32_t flags;
1992
1993	hi_adj = (chanFlags & IEEE80211_CHAN_HT40U) ? -20 : 0;
1994	lo_adj = (chanFlags & IEEE80211_CHAN_HT40D) ? 20 : 0;
1995	channelSep = (chanFlags & IEEE80211_CHAN_2GHZ) ? 0 : 40;
1996	LIST_FOREACH(nb, bands, next) {
1997		b = nb->band;
1998		if (verbose) {
1999			printf("%s:", __func__);
2000			printb(" chanFlags", chanFlags, IEEE80211_CHAN_BITS);
2001			printb(" bandFlags", nb->flags | b->flags,
2002			    IEEE80211_CHAN_BITS);
2003			putchar('\n');
2004		}
2005		prev = NULL;
2006		for (freq = b->freqStart + lo_adj;
2007		     freq <= b->freqEnd + hi_adj; freq += b->chanSep) {
2008			/*
2009			 * Construct flags for the new channel.  We take
2010			 * the attributes from the band descriptions except
2011			 * for HT40 which is enabled generically (i.e. +/-
2012			 * extension channel) in the band description and
2013			 * then constrained according by channel separation.
2014			 */
2015			flags = nb->flags | b->flags;
2016			if (flags & IEEE80211_CHAN_HT) {
2017				/*
2018				 * HT channels are generated specially; we're
2019				 * called to add HT20, HT40+, and HT40- chan's
2020				 * so we need to expand only band specs for
2021				 * the HT channel type being added.
2022				 */
2023				if ((chanFlags & IEEE80211_CHAN_HT20) &&
2024				    (flags & IEEE80211_CHAN_HT20) == 0) {
2025					if (verbose)
2026						printf("%u: skip, not an "
2027						    "HT20 channel\n", freq);
2028					continue;
2029				}
2030				if ((chanFlags & IEEE80211_CHAN_HT40) &&
2031				    (flags & IEEE80211_CHAN_HT40) == 0) {
2032					if (verbose)
2033						printf("%u: skip, not an "
2034						    "HT40 channel\n", freq);
2035					continue;
2036				}
2037				/*
2038				 * DFS and HT40 don't mix.  This should be
2039				 * expressed in the regdomain database but
2040				 * just in case enforce it here.
2041				 */
2042				if ((chanFlags & IEEE80211_CHAN_HT40) &&
2043				    (flags & IEEE80211_CHAN_DFS)) {
2044					if (verbose)
2045						printf("%u: skip, HT40+DFS "
2046						    "not permitted\n", freq);
2047					continue;
2048				}
2049				/* NB: HT attribute comes from caller */
2050				flags &= ~IEEE80211_CHAN_HT;
2051				flags |= chanFlags & IEEE80211_CHAN_HT;
2052			}
2053			/*
2054			 * Check if device can operate on this frequency.
2055			 */
2056			if (!checkchan(avail, freq, flags)) {
2057				if (verbose) {
2058					printf("%u: skip, ", freq);
2059					printb("flags", flags,
2060					    IEEE80211_CHAN_BITS);
2061					printf(" not available\n");
2062				}
2063				continue;
2064			}
2065			if ((flags & REQ_ECM) && !reg->ecm) {
2066				if (verbose)
2067					printf("%u: skip, ECM channel\n", freq);
2068				continue;
2069			}
2070			if ((flags & REQ_INDOOR) && reg->location == 'O') {
2071				if (verbose)
2072					printf("%u: skip, indoor channel\n",
2073					    freq);
2074				continue;
2075			}
2076			if ((flags & REQ_OUTDOOR) && reg->location == 'I') {
2077				if (verbose)
2078					printf("%u: skip, outdoor channel\n",
2079					    freq);
2080				continue;
2081			}
2082			if ((flags & IEEE80211_CHAN_HT40) &&
2083			    prev != NULL && (freq - prev->ic_freq) < channelSep) {
2084				if (verbose)
2085					printf("%u: skip, only %u channel "
2086					    "separation, need %d\n", freq,
2087					    freq - prev->ic_freq, channelSep);
2088				continue;
2089			}
2090			if (ci->ic_nchans == IEEE80211_CHAN_MAX) {
2091				if (verbose)
2092					printf("%u: skip, channel table full\n",
2093					    freq);
2094				break;
2095			}
2096			c = &ci->ic_chans[ci->ic_nchans++];
2097			memset(c, 0, sizeof(*c));
2098			c->ic_freq = freq;
2099			c->ic_flags = flags;
2100			if (c->ic_flags & IEEE80211_CHAN_DFS)
2101				c->ic_maxregpower = nb->maxPowerDFS;
2102			else
2103				c->ic_maxregpower = nb->maxPower;
2104			if (verbose) {
2105				printf("[%3d] add freq %u ",
2106				    ci->ic_nchans-1, c->ic_freq);
2107				printb("flags", c->ic_flags, IEEE80211_CHAN_BITS);
2108				printf(" power %u\n", c->ic_maxregpower);
2109			}
2110			/* NB: kernel fills in other fields */
2111			prev = c;
2112		}
2113	}
2114}
2115
2116static void
2117regdomain_makechannels(
2118	struct ieee80211_regdomain_req *req,
2119	const struct ieee80211_devcaps_req *dc)
2120{
2121	struct regdata *rdp = getregdata();
2122	const struct country *cc;
2123	const struct ieee80211_regdomain *reg = &req->rd;
2124	struct ieee80211req_chaninfo *ci = &req->chaninfo;
2125	const struct regdomain *rd;
2126
2127	/*
2128	 * Locate construction table for new channel list.  We treat
2129	 * the regdomain/SKU as definitive so a country can be in
2130	 * multiple with different properties (e.g. US in FCC+FCC3).
2131	 * If no regdomain is specified then we fallback on the country
2132	 * code to find the associated regdomain since countries always
2133	 * belong to at least one regdomain.
2134	 */
2135	if (reg->regdomain == 0) {
2136		cc = lib80211_country_findbycc(rdp, reg->country);
2137		if (cc == NULL)
2138			errx(1, "internal error, country %d not found",
2139			    reg->country);
2140		rd = cc->rd;
2141	} else
2142		rd = lib80211_regdomain_findbysku(rdp, reg->regdomain);
2143	if (rd == NULL)
2144		errx(1, "internal error, regdomain %d not found",
2145			    reg->regdomain);
2146	if (rd->sku != SKU_DEBUG) {
2147		/*
2148		 * regdomain_addchans incrememnts the channel count for
2149		 * each channel it adds so initialize ic_nchans to zero.
2150		 * Note that we know we have enough space to hold all possible
2151		 * channels because the devcaps list size was used to
2152		 * allocate our request.
2153		 */
2154		ci->ic_nchans = 0;
2155		if (!LIST_EMPTY(&rd->bands_11b))
2156			regdomain_addchans(ci, &rd->bands_11b, reg,
2157			    IEEE80211_CHAN_B, &dc->dc_chaninfo);
2158		if (!LIST_EMPTY(&rd->bands_11g))
2159			regdomain_addchans(ci, &rd->bands_11g, reg,
2160			    IEEE80211_CHAN_G, &dc->dc_chaninfo);
2161		if (!LIST_EMPTY(&rd->bands_11a))
2162			regdomain_addchans(ci, &rd->bands_11a, reg,
2163			    IEEE80211_CHAN_A, &dc->dc_chaninfo);
2164		if (!LIST_EMPTY(&rd->bands_11na) && dc->dc_htcaps != 0) {
2165			regdomain_addchans(ci, &rd->bands_11na, reg,
2166			    IEEE80211_CHAN_A | IEEE80211_CHAN_HT20,
2167			    &dc->dc_chaninfo);
2168			if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2169				regdomain_addchans(ci, &rd->bands_11na, reg,
2170				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U,
2171				    &dc->dc_chaninfo);
2172				regdomain_addchans(ci, &rd->bands_11na, reg,
2173				    IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D,
2174				    &dc->dc_chaninfo);
2175			}
2176		}
2177		if (!LIST_EMPTY(&rd->bands_11ng) && dc->dc_htcaps != 0) {
2178			regdomain_addchans(ci, &rd->bands_11ng, reg,
2179			    IEEE80211_CHAN_G | IEEE80211_CHAN_HT20,
2180			    &dc->dc_chaninfo);
2181			if (dc->dc_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
2182				regdomain_addchans(ci, &rd->bands_11ng, reg,
2183				    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U,
2184				    &dc->dc_chaninfo);
2185				regdomain_addchans(ci, &rd->bands_11ng, reg,
2186				    IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D,
2187				    &dc->dc_chaninfo);
2188			}
2189		}
2190		qsort(ci->ic_chans, ci->ic_nchans, sizeof(ci->ic_chans[0]),
2191		    regdomain_sort);
2192	} else
2193		memcpy(ci, &dc->dc_chaninfo,
2194		    IEEE80211_CHANINFO_SPACE(&dc->dc_chaninfo));
2195}
2196
2197static void
2198list_countries(void)
2199{
2200	struct regdata *rdp = getregdata();
2201	const struct country *cp;
2202	const struct regdomain *dp;
2203	int i;
2204
2205	i = 0;
2206	printf("\nCountry codes:\n");
2207	LIST_FOREACH(cp, &rdp->countries, next) {
2208		printf("%2s %-15.15s%s", cp->isoname,
2209		    cp->name, ((i+1)%4) == 0 ? "\n" : " ");
2210		i++;
2211	}
2212	i = 0;
2213	printf("\nRegulatory domains:\n");
2214	LIST_FOREACH(dp, &rdp->domains, next) {
2215		printf("%-15.15s%s", dp->name, ((i+1)%4) == 0 ? "\n" : " ");
2216		i++;
2217	}
2218	printf("\n");
2219}
2220
2221static void
2222defaultcountry(const struct regdomain *rd)
2223{
2224	struct regdata *rdp = getregdata();
2225	const struct country *cc;
2226
2227	cc = lib80211_country_findbycc(rdp, rd->cc->code);
2228	if (cc == NULL)
2229		errx(1, "internal error, ISO country code %d not "
2230		    "defined for regdomain %s", rd->cc->code, rd->name);
2231	regdomain.country = cc->code;
2232	regdomain.isocc[0] = cc->isoname[0];
2233	regdomain.isocc[1] = cc->isoname[1];
2234}
2235
2236static
2237DECL_CMD_FUNC(set80211regdomain, val, d)
2238{
2239	struct regdata *rdp = getregdata();
2240	const struct regdomain *rd;
2241
2242	rd = lib80211_regdomain_findbyname(rdp, val);
2243	if (rd == NULL) {
2244		char *eptr;
2245		long sku = strtol(val, &eptr, 0);
2246
2247		if (eptr != val)
2248			rd = lib80211_regdomain_findbysku(rdp, sku);
2249		if (eptr == val || rd == NULL)
2250			errx(1, "unknown regdomain %s", val);
2251	}
2252	getregdomain(s);
2253	regdomain.regdomain = rd->sku;
2254	if (regdomain.country == 0 && rd->cc != NULL) {
2255		/*
2256		 * No country code setup and there's a default
2257		 * one for this regdomain fill it in.
2258		 */
2259		defaultcountry(rd);
2260	}
2261	callback_register(setregdomain_cb, &regdomain);
2262}
2263
2264static
2265DECL_CMD_FUNC(set80211country, val, d)
2266{
2267	struct regdata *rdp = getregdata();
2268	const struct country *cc;
2269
2270	cc = lib80211_country_findbyname(rdp, val);
2271	if (cc == NULL) {
2272		char *eptr;
2273		long code = strtol(val, &eptr, 0);
2274
2275		if (eptr != val)
2276			cc = lib80211_country_findbycc(rdp, code);
2277		if (eptr == val || cc == NULL)
2278			errx(1, "unknown ISO country code %s", val);
2279	}
2280	getregdomain(s);
2281	regdomain.regdomain = cc->rd->sku;
2282	regdomain.country = cc->code;
2283	regdomain.isocc[0] = cc->isoname[0];
2284	regdomain.isocc[1] = cc->isoname[1];
2285	callback_register(setregdomain_cb, &regdomain);
2286}
2287
2288static void
2289set80211location(const char *val, int d, int s, const struct afswtch *rafp)
2290{
2291	getregdomain(s);
2292	regdomain.location = d;
2293	callback_register(setregdomain_cb, &regdomain);
2294}
2295
2296static void
2297set80211ecm(const char *val, int d, int s, const struct afswtch *rafp)
2298{
2299	getregdomain(s);
2300	regdomain.ecm = d;
2301	callback_register(setregdomain_cb, &regdomain);
2302}
2303
2304static void
2305LINE_INIT(char c)
2306{
2307	spacer = c;
2308	if (c == '\t')
2309		col = 8;
2310	else
2311		col = 1;
2312}
2313
2314static void
2315LINE_BREAK(void)
2316{
2317	if (spacer != '\t') {
2318		printf("\n");
2319		spacer = '\t';
2320	}
2321	col = 8;		/* 8-col tab */
2322}
2323
2324static void
2325LINE_CHECK(const char *fmt, ...)
2326{
2327	char buf[80];
2328	va_list ap;
2329	int n;
2330
2331	va_start(ap, fmt);
2332	n = vsnprintf(buf+1, sizeof(buf)-1, fmt, ap);
2333	va_end(ap);
2334	col += 1+n;
2335	if (col > MAXCOL) {
2336		LINE_BREAK();
2337		col += n;
2338	}
2339	buf[0] = spacer;
2340	printf("%s", buf);
2341	spacer = ' ';
2342}
2343
2344static int
2345getmaxrate(const uint8_t rates[15], uint8_t nrates)
2346{
2347	int i, maxrate = -1;
2348
2349	for (i = 0; i < nrates; i++) {
2350		int rate = rates[i] & IEEE80211_RATE_VAL;
2351		if (rate > maxrate)
2352			maxrate = rate;
2353	}
2354	return maxrate / 2;
2355}
2356
2357static const char *
2358getcaps(int capinfo)
2359{
2360	static char capstring[32];
2361	char *cp = capstring;
2362
2363	if (capinfo & IEEE80211_CAPINFO_ESS)
2364		*cp++ = 'E';
2365	if (capinfo & IEEE80211_CAPINFO_IBSS)
2366		*cp++ = 'I';
2367	if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
2368		*cp++ = 'c';
2369	if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
2370		*cp++ = 'C';
2371	if (capinfo & IEEE80211_CAPINFO_PRIVACY)
2372		*cp++ = 'P';
2373	if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
2374		*cp++ = 'S';
2375	if (capinfo & IEEE80211_CAPINFO_PBCC)
2376		*cp++ = 'B';
2377	if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
2378		*cp++ = 'A';
2379	if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
2380		*cp++ = 's';
2381	if (capinfo & IEEE80211_CAPINFO_RSN)
2382		*cp++ = 'R';
2383	if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
2384		*cp++ = 'D';
2385	*cp = '\0';
2386	return capstring;
2387}
2388
2389static const char *
2390getflags(int flags)
2391{
2392	static char flagstring[32];
2393	char *cp = flagstring;
2394
2395	if (flags & IEEE80211_NODE_AUTH)
2396		*cp++ = 'A';
2397	if (flags & IEEE80211_NODE_QOS)
2398		*cp++ = 'Q';
2399	if (flags & IEEE80211_NODE_ERP)
2400		*cp++ = 'E';
2401	if (flags & IEEE80211_NODE_PWR_MGT)
2402		*cp++ = 'P';
2403	if (flags & IEEE80211_NODE_HT) {
2404		*cp++ = 'H';
2405		if (flags & IEEE80211_NODE_HTCOMPAT)
2406			*cp++ = '+';
2407	}
2408	if (flags & IEEE80211_NODE_WPS)
2409		*cp++ = 'W';
2410	if (flags & IEEE80211_NODE_TSN)
2411		*cp++ = 'N';
2412	if (flags & IEEE80211_NODE_AMPDU_TX)
2413		*cp++ = 'T';
2414	if (flags & IEEE80211_NODE_AMPDU_RX)
2415		*cp++ = 'R';
2416	if (flags & IEEE80211_NODE_MIMO_PS) {
2417		*cp++ = 'M';
2418		if (flags & IEEE80211_NODE_MIMO_RTS)
2419			*cp++ = '+';
2420	}
2421	if (flags & IEEE80211_NODE_RIFS)
2422		*cp++ = 'I';
2423	if (flags & IEEE80211_NODE_SGI40) {
2424		*cp++ = 'S';
2425		if (flags & IEEE80211_NODE_SGI20)
2426			*cp++ = '+';
2427	} else if (flags & IEEE80211_NODE_SGI20)
2428		*cp++ = 's';
2429	if (flags & IEEE80211_NODE_AMSDU_TX)
2430		*cp++ = 't';
2431	if (flags & IEEE80211_NODE_AMSDU_RX)
2432		*cp++ = 'r';
2433	*cp = '\0';
2434	return flagstring;
2435}
2436
2437static void
2438printie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
2439{
2440	printf("%s", tag);
2441	if (verbose) {
2442		maxlen -= strlen(tag)+2;
2443		if (2*ielen > maxlen)
2444			maxlen--;
2445		printf("<");
2446		for (; ielen > 0; ie++, ielen--) {
2447			if (maxlen-- <= 0)
2448				break;
2449			printf("%02x", *ie);
2450		}
2451		if (ielen != 0)
2452			printf("-");
2453		printf(">");
2454	}
2455}
2456
2457#define LE_READ_2(p)					\
2458	((u_int16_t)					\
2459	 ((((const u_int8_t *)(p))[0]      ) |		\
2460	  (((const u_int8_t *)(p))[1] <<  8)))
2461#define LE_READ_4(p)					\
2462	((u_int32_t)					\
2463	 ((((const u_int8_t *)(p))[0]      ) |		\
2464	  (((const u_int8_t *)(p))[1] <<  8) |		\
2465	  (((const u_int8_t *)(p))[2] << 16) |		\
2466	  (((const u_int8_t *)(p))[3] << 24)))
2467
2468/*
2469 * NB: The decoding routines assume a properly formatted ie
2470 *     which should be safe as the kernel only retains them
2471 *     if they parse ok.
2472 */
2473
2474static void
2475printwmeparam(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2476{
2477#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
2478	static const char *acnames[] = { "BE", "BK", "VO", "VI" };
2479	const struct ieee80211_wme_param *wme =
2480	    (const struct ieee80211_wme_param *) ie;
2481	int i;
2482
2483	printf("%s", tag);
2484	if (!verbose)
2485		return;
2486	printf("<qosinfo 0x%x", wme->param_qosInfo);
2487	ie += offsetof(struct ieee80211_wme_param, params_acParams);
2488	for (i = 0; i < WME_NUM_AC; i++) {
2489		const struct ieee80211_wme_acparams *ac =
2490		    &wme->params_acParams[i];
2491
2492		printf(" %s[%saifsn %u cwmin %u cwmax %u txop %u]"
2493			, acnames[i]
2494			, MS(ac->acp_aci_aifsn, WME_PARAM_ACM) ? "acm " : ""
2495			, MS(ac->acp_aci_aifsn, WME_PARAM_AIFSN)
2496			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMIN)
2497			, MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMAX)
2498			, LE_READ_2(&ac->acp_txop)
2499		);
2500	}
2501	printf(">");
2502#undef MS
2503}
2504
2505static void
2506printwmeinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2507{
2508	printf("%s", tag);
2509	if (verbose) {
2510		const struct ieee80211_wme_info *wme =
2511		    (const struct ieee80211_wme_info *) ie;
2512		printf("<version 0x%x info 0x%x>",
2513		    wme->wme_version, wme->wme_info);
2514	}
2515}
2516
2517static void
2518printhtcap(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2519{
2520	printf("%s", tag);
2521	if (verbose) {
2522		const struct ieee80211_ie_htcap *htcap =
2523		    (const struct ieee80211_ie_htcap *) ie;
2524		const char *sep;
2525		int i, j;
2526
2527		printf("<cap 0x%x param 0x%x",
2528		    LE_READ_2(&htcap->hc_cap), htcap->hc_param);
2529		printf(" mcsset[");
2530		sep = "";
2531		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2532			if (isset(htcap->hc_mcsset, i)) {
2533				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2534					if (isclr(htcap->hc_mcsset, j))
2535						break;
2536				j--;
2537				if (i == j)
2538					printf("%s%u", sep, i);
2539				else
2540					printf("%s%u-%u", sep, i, j);
2541				i += j-i;
2542				sep = ",";
2543			}
2544		printf("] extcap 0x%x txbf 0x%x antenna 0x%x>",
2545		    LE_READ_2(&htcap->hc_extcap),
2546		    LE_READ_4(&htcap->hc_txbf),
2547		    htcap->hc_antenna);
2548	}
2549}
2550
2551static void
2552printhtinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2553{
2554	printf("%s", tag);
2555	if (verbose) {
2556		const struct ieee80211_ie_htinfo *htinfo =
2557		    (const struct ieee80211_ie_htinfo *) ie;
2558		const char *sep;
2559		int i, j;
2560
2561		printf("<ctl %u, %x,%x,%x,%x", htinfo->hi_ctrlchannel,
2562		    htinfo->hi_byte1, htinfo->hi_byte2, htinfo->hi_byte3,
2563		    LE_READ_2(&htinfo->hi_byte45));
2564		printf(" basicmcs[");
2565		sep = "";
2566		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++)
2567			if (isset(htinfo->hi_basicmcsset, i)) {
2568				for (j = i+1; j < IEEE80211_HTRATE_MAXSIZE; j++)
2569					if (isclr(htinfo->hi_basicmcsset, j))
2570						break;
2571				j--;
2572				if (i == j)
2573					printf("%s%u", sep, i);
2574				else
2575					printf("%s%u-%u", sep, i, j);
2576				i += j-i;
2577				sep = ",";
2578			}
2579		printf("]>");
2580	}
2581}
2582
2583static void
2584printathie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2585{
2586
2587	printf("%s", tag);
2588	if (verbose) {
2589		const struct ieee80211_ath_ie *ath =
2590			(const struct ieee80211_ath_ie *)ie;
2591
2592		printf("<");
2593		if (ath->ath_capability & ATHEROS_CAP_TURBO_PRIME)
2594			printf("DTURBO,");
2595		if (ath->ath_capability & ATHEROS_CAP_COMPRESSION)
2596			printf("COMP,");
2597		if (ath->ath_capability & ATHEROS_CAP_FAST_FRAME)
2598			printf("FF,");
2599		if (ath->ath_capability & ATHEROS_CAP_XR)
2600			printf("XR,");
2601		if (ath->ath_capability & ATHEROS_CAP_AR)
2602			printf("AR,");
2603		if (ath->ath_capability & ATHEROS_CAP_BURST)
2604			printf("BURST,");
2605		if (ath->ath_capability & ATHEROS_CAP_WME)
2606			printf("WME,");
2607		if (ath->ath_capability & ATHEROS_CAP_BOOST)
2608			printf("BOOST,");
2609		printf("0x%x>", LE_READ_2(ath->ath_defkeyix));
2610	}
2611}
2612
2613
2614static void
2615printmeshconf(const char *tag, const uint8_t *ie, size_t ielen, int maxlen)
2616{
2617#define MATCHOUI(field, oui, string)					\
2618do {									\
2619	if (memcmp(field, oui, 4) == 0)					\
2620		printf("%s", string);					\
2621} while (0)
2622
2623	printf("%s", tag);
2624	if (verbose) {
2625		const struct ieee80211_meshconf_ie *mconf =
2626			(const struct ieee80211_meshconf_ie *)ie;
2627		printf("<PATH:");
2628		if (mconf->conf_pselid == IEEE80211_MESHCONF_PATH_HWMP)
2629			printf("HWMP");
2630		else
2631			printf("UNKNOWN");
2632		printf(" LINK:");
2633		if (mconf->conf_pmetid == IEEE80211_MESHCONF_METRIC_AIRTIME)
2634			printf("AIRTIME");
2635		else
2636			printf("UNKNOWN");
2637		printf(" CONGESTION:");
2638		if (mconf->conf_ccid == IEEE80211_MESHCONF_CC_DISABLED)
2639			printf("DISABLED");
2640		else
2641			printf("UNKNOWN");
2642		printf(" SYNC:");
2643		if (mconf->conf_syncid == IEEE80211_MESHCONF_SYNC_NEIGHOFF)
2644			printf("NEIGHOFF");
2645		else
2646			printf("UNKNOWN");
2647		printf(" AUTH:");
2648		if (mconf->conf_authid == IEEE80211_MESHCONF_AUTH_DISABLED)
2649			printf("DISABLED");
2650		else
2651			printf("UNKNOWN");
2652		printf(" FORM:0x%x CAPS:0x%x>", mconf->conf_form,
2653		    mconf->conf_cap);
2654	}
2655#undef MATCHOUI
2656}
2657
2658static const char *
2659wpa_cipher(const u_int8_t *sel)
2660{
2661#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2662	u_int32_t w = LE_READ_4(sel);
2663
2664	switch (w) {
2665	case WPA_SEL(WPA_CSE_NULL):
2666		return "NONE";
2667	case WPA_SEL(WPA_CSE_WEP40):
2668		return "WEP40";
2669	case WPA_SEL(WPA_CSE_WEP104):
2670		return "WEP104";
2671	case WPA_SEL(WPA_CSE_TKIP):
2672		return "TKIP";
2673	case WPA_SEL(WPA_CSE_CCMP):
2674		return "AES-CCMP";
2675	}
2676	return "?";		/* NB: so 1<< is discarded */
2677#undef WPA_SEL
2678}
2679
2680static const char *
2681wpa_keymgmt(const u_int8_t *sel)
2682{
2683#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
2684	u_int32_t w = LE_READ_4(sel);
2685
2686	switch (w) {
2687	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
2688		return "8021X-UNSPEC";
2689	case WPA_SEL(WPA_ASE_8021X_PSK):
2690		return "8021X-PSK";
2691	case WPA_SEL(WPA_ASE_NONE):
2692		return "NONE";
2693	}
2694	return "?";
2695#undef WPA_SEL
2696}
2697
2698static void
2699printwpaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2700{
2701	u_int8_t len = ie[1];
2702
2703	printf("%s", tag);
2704	if (verbose) {
2705		const char *sep;
2706		int n;
2707
2708		ie += 6, len -= 4;		/* NB: len is payload only */
2709
2710		printf("<v%u", LE_READ_2(ie));
2711		ie += 2, len -= 2;
2712
2713		printf(" mc:%s", wpa_cipher(ie));
2714		ie += 4, len -= 4;
2715
2716		/* unicast ciphers */
2717		n = LE_READ_2(ie);
2718		ie += 2, len -= 2;
2719		sep = " uc:";
2720		for (; n > 0; n--) {
2721			printf("%s%s", sep, wpa_cipher(ie));
2722			ie += 4, len -= 4;
2723			sep = "+";
2724		}
2725
2726		/* key management algorithms */
2727		n = LE_READ_2(ie);
2728		ie += 2, len -= 2;
2729		sep = " km:";
2730		for (; n > 0; n--) {
2731			printf("%s%s", sep, wpa_keymgmt(ie));
2732			ie += 4, len -= 4;
2733			sep = "+";
2734		}
2735
2736		if (len > 2)		/* optional capabilities */
2737			printf(", caps 0x%x", LE_READ_2(ie));
2738		printf(">");
2739	}
2740}
2741
2742static const char *
2743rsn_cipher(const u_int8_t *sel)
2744{
2745#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2746	u_int32_t w = LE_READ_4(sel);
2747
2748	switch (w) {
2749	case RSN_SEL(RSN_CSE_NULL):
2750		return "NONE";
2751	case RSN_SEL(RSN_CSE_WEP40):
2752		return "WEP40";
2753	case RSN_SEL(RSN_CSE_WEP104):
2754		return "WEP104";
2755	case RSN_SEL(RSN_CSE_TKIP):
2756		return "TKIP";
2757	case RSN_SEL(RSN_CSE_CCMP):
2758		return "AES-CCMP";
2759	case RSN_SEL(RSN_CSE_WRAP):
2760		return "AES-OCB";
2761	}
2762	return "?";
2763#undef WPA_SEL
2764}
2765
2766static const char *
2767rsn_keymgmt(const u_int8_t *sel)
2768{
2769#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
2770	u_int32_t w = LE_READ_4(sel);
2771
2772	switch (w) {
2773	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
2774		return "8021X-UNSPEC";
2775	case RSN_SEL(RSN_ASE_8021X_PSK):
2776		return "8021X-PSK";
2777	case RSN_SEL(RSN_ASE_NONE):
2778		return "NONE";
2779	}
2780	return "?";
2781#undef RSN_SEL
2782}
2783
2784static void
2785printrsnie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2786{
2787	printf("%s", tag);
2788	if (verbose) {
2789		const char *sep;
2790		int n;
2791
2792		ie += 2, ielen -= 2;
2793
2794		printf("<v%u", LE_READ_2(ie));
2795		ie += 2, ielen -= 2;
2796
2797		printf(" mc:%s", rsn_cipher(ie));
2798		ie += 4, ielen -= 4;
2799
2800		/* unicast ciphers */
2801		n = LE_READ_2(ie);
2802		ie += 2, ielen -= 2;
2803		sep = " uc:";
2804		for (; n > 0; n--) {
2805			printf("%s%s", sep, rsn_cipher(ie));
2806			ie += 4, ielen -= 4;
2807			sep = "+";
2808		}
2809
2810		/* key management algorithms */
2811		n = LE_READ_2(ie);
2812		ie += 2, ielen -= 2;
2813		sep = " km:";
2814		for (; n > 0; n--) {
2815			printf("%s%s", sep, rsn_keymgmt(ie));
2816			ie += 4, ielen -= 4;
2817			sep = "+";
2818		}
2819
2820		if (ielen > 2)		/* optional capabilities */
2821			printf(", caps 0x%x", LE_READ_2(ie));
2822		/* XXXPMKID */
2823		printf(">");
2824	}
2825}
2826
2827/* XXX move to a public include file */
2828#define IEEE80211_WPS_DEV_PASS_ID	0x1012
2829#define IEEE80211_WPS_SELECTED_REG	0x1041
2830#define IEEE80211_WPS_SETUP_STATE	0x1044
2831#define IEEE80211_WPS_UUID_E		0x1047
2832#define IEEE80211_WPS_VERSION		0x104a
2833
2834#define BE_READ_2(p)					\
2835	((u_int16_t)					\
2836	 ((((const u_int8_t *)(p))[1]      ) |		\
2837	  (((const u_int8_t *)(p))[0] <<  8)))
2838
2839static void
2840printwpsie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2841{
2842#define	N(a)	(sizeof(a) / sizeof(a[0]))
2843	u_int8_t len = ie[1];
2844
2845	printf("%s", tag);
2846	if (verbose) {
2847		static const char *dev_pass_id[] = {
2848			"D",	/* Default (PIN) */
2849			"U",	/* User-specified */
2850			"M",	/* Machine-specified */
2851			"K",	/* Rekey */
2852			"P",	/* PushButton */
2853			"R"	/* Registrar-specified */
2854		};
2855		int n;
2856
2857		ie +=6, len -= 4;		/* NB: len is payload only */
2858
2859		/* WPS IE in Beacon and Probe Resp frames have different fields */
2860		printf("<");
2861		while (len) {
2862			uint16_t tlv_type = BE_READ_2(ie);
2863			uint16_t tlv_len  = BE_READ_2(ie + 2);
2864
2865			ie += 4, len -= 4;
2866
2867			switch (tlv_type) {
2868			case IEEE80211_WPS_VERSION:
2869				printf("v:%d.%d", *ie >> 4, *ie & 0xf);
2870				break;
2871			case IEEE80211_WPS_SETUP_STATE:
2872				/* Only 1 and 2 are valid */
2873				if (*ie == 0 || *ie >= 3)
2874					printf(" state:B");
2875				else
2876					printf(" st:%s", *ie == 1 ? "N" : "C");
2877				break;
2878			case IEEE80211_WPS_SELECTED_REG:
2879				printf(" sel:%s", *ie ? "T" : "F");
2880				break;
2881			case IEEE80211_WPS_DEV_PASS_ID:
2882				n = LE_READ_2(ie);
2883				if (n < N(dev_pass_id))
2884					printf(" dpi:%s", dev_pass_id[n]);
2885				break;
2886			case IEEE80211_WPS_UUID_E:
2887				printf(" uuid-e:");
2888				for (n = 0; n < (tlv_len - 1); n++)
2889					printf("%02x-", ie[n]);
2890				printf("%02x", ie[n]);
2891				break;
2892			}
2893			ie += tlv_len, len -= tlv_len;
2894		}
2895		printf(">");
2896	}
2897#undef N
2898}
2899
2900static void
2901printtdmaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2902{
2903	printf("%s", tag);
2904	if (verbose && ielen >= sizeof(struct ieee80211_tdma_param)) {
2905		const struct ieee80211_tdma_param *tdma =
2906		   (const struct ieee80211_tdma_param *) ie;
2907
2908		/* XXX tstamp */
2909		printf("<v%u slot:%u slotcnt:%u slotlen:%u bintval:%u inuse:0x%x>",
2910		    tdma->tdma_version, tdma->tdma_slot, tdma->tdma_slotcnt,
2911		    LE_READ_2(&tdma->tdma_slotlen), tdma->tdma_bintval,
2912		    tdma->tdma_inuse[0]);
2913	}
2914}
2915
2916/*
2917 * Copy the ssid string contents into buf, truncating to fit.  If the
2918 * ssid is entirely printable then just copy intact.  Otherwise convert
2919 * to hexadecimal.  If the result is truncated then replace the last
2920 * three characters with "...".
2921 */
2922static int
2923copy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
2924{
2925	const u_int8_t *p;
2926	size_t maxlen;
2927	int i;
2928
2929	if (essid_len > bufsize)
2930		maxlen = bufsize;
2931	else
2932		maxlen = essid_len;
2933	/* determine printable or not */
2934	for (i = 0, p = essid; i < maxlen; i++, p++) {
2935		if (*p < ' ' || *p > 0x7e)
2936			break;
2937	}
2938	if (i != maxlen) {		/* not printable, print as hex */
2939		if (bufsize < 3)
2940			return 0;
2941		strlcpy(buf, "0x", bufsize);
2942		bufsize -= 2;
2943		p = essid;
2944		for (i = 0; i < maxlen && bufsize >= 2; i++) {
2945			sprintf(&buf[2+2*i], "%02x", p[i]);
2946			bufsize -= 2;
2947		}
2948		if (i != essid_len)
2949			memcpy(&buf[2+2*i-3], "...", 3);
2950	} else {			/* printable, truncate as needed */
2951		memcpy(buf, essid, maxlen);
2952		if (maxlen != essid_len)
2953			memcpy(&buf[maxlen-3], "...", 3);
2954	}
2955	return maxlen;
2956}
2957
2958static void
2959printssid(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2960{
2961	char ssid[2*IEEE80211_NWID_LEN+1];
2962
2963	printf("%s<%.*s>", tag, copy_essid(ssid, maxlen, ie+2, ie[1]), ssid);
2964}
2965
2966static void
2967printrates(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2968{
2969	const char *sep;
2970	int i;
2971
2972	printf("%s", tag);
2973	sep = "<";
2974	for (i = 2; i < ielen; i++) {
2975		printf("%s%s%d", sep,
2976		    ie[i] & IEEE80211_RATE_BASIC ? "B" : "",
2977		    ie[i] & IEEE80211_RATE_VAL);
2978		sep = ",";
2979	}
2980	printf(">");
2981}
2982
2983static void
2984printcountry(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
2985{
2986	const struct ieee80211_country_ie *cie =
2987	   (const struct ieee80211_country_ie *) ie;
2988	int i, nbands, schan, nchan;
2989
2990	printf("%s<%c%c%c", tag, cie->cc[0], cie->cc[1], cie->cc[2]);
2991	nbands = (cie->len - 3) / sizeof(cie->band[0]);
2992	for (i = 0; i < nbands; i++) {
2993		schan = cie->band[i].schan;
2994		nchan = cie->band[i].nchan;
2995		if (nchan != 1)
2996			printf(" %u-%u,%u", schan, schan + nchan-1,
2997			    cie->band[i].maxtxpwr);
2998		else
2999			printf(" %u,%u", schan, cie->band[i].maxtxpwr);
3000	}
3001	printf(">");
3002}
3003
3004/* unaligned little endian access */
3005#define LE_READ_4(p)					\
3006	((u_int32_t)					\
3007	 ((((const u_int8_t *)(p))[0]      ) |		\
3008	  (((const u_int8_t *)(p))[1] <<  8) |		\
3009	  (((const u_int8_t *)(p))[2] << 16) |		\
3010	  (((const u_int8_t *)(p))[3] << 24)))
3011
3012static __inline int
3013iswpaoui(const u_int8_t *frm)
3014{
3015	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
3016}
3017
3018static __inline int
3019iswmeinfo(const u_int8_t *frm)
3020{
3021	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3022		frm[6] == WME_INFO_OUI_SUBTYPE;
3023}
3024
3025static __inline int
3026iswmeparam(const u_int8_t *frm)
3027{
3028	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3029		frm[6] == WME_PARAM_OUI_SUBTYPE;
3030}
3031
3032static __inline int
3033isatherosoui(const u_int8_t *frm)
3034{
3035	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
3036}
3037
3038static __inline int
3039istdmaoui(const uint8_t *frm)
3040{
3041	return frm[1] > 3 && LE_READ_4(frm+2) == ((TDMA_OUI_TYPE<<24)|TDMA_OUI);
3042}
3043
3044static __inline int
3045iswpsoui(const uint8_t *frm)
3046{
3047	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPS_OUI_TYPE<<24)|WPA_OUI);
3048}
3049
3050static const char *
3051iename(int elemid)
3052{
3053	switch (elemid) {
3054	case IEEE80211_ELEMID_FHPARMS:	return " FHPARMS";
3055	case IEEE80211_ELEMID_CFPARMS:	return " CFPARMS";
3056	case IEEE80211_ELEMID_TIM:	return " TIM";
3057	case IEEE80211_ELEMID_IBSSPARMS:return " IBSSPARMS";
3058	case IEEE80211_ELEMID_CHALLENGE:return " CHALLENGE";
3059	case IEEE80211_ELEMID_PWRCNSTR:	return " PWRCNSTR";
3060	case IEEE80211_ELEMID_PWRCAP:	return " PWRCAP";
3061	case IEEE80211_ELEMID_TPCREQ:	return " TPCREQ";
3062	case IEEE80211_ELEMID_TPCREP:	return " TPCREP";
3063	case IEEE80211_ELEMID_SUPPCHAN:	return " SUPPCHAN";
3064	case IEEE80211_ELEMID_CSA:	return " CSA";
3065	case IEEE80211_ELEMID_MEASREQ:	return " MEASREQ";
3066	case IEEE80211_ELEMID_MEASREP:	return " MEASREP";
3067	case IEEE80211_ELEMID_QUIET:	return " QUIET";
3068	case IEEE80211_ELEMID_IBSSDFS:	return " IBSSDFS";
3069	case IEEE80211_ELEMID_TPC:	return " TPC";
3070	case IEEE80211_ELEMID_CCKM:	return " CCKM";
3071	}
3072	return " ???";
3073}
3074
3075static void
3076printies(const u_int8_t *vp, int ielen, int maxcols)
3077{
3078	while (ielen > 0) {
3079		switch (vp[0]) {
3080		case IEEE80211_ELEMID_SSID:
3081			if (verbose)
3082				printssid(" SSID", vp, 2+vp[1], maxcols);
3083			break;
3084		case IEEE80211_ELEMID_RATES:
3085		case IEEE80211_ELEMID_XRATES:
3086			if (verbose)
3087				printrates(vp[0] == IEEE80211_ELEMID_RATES ?
3088				    " RATES" : " XRATES", vp, 2+vp[1], maxcols);
3089			break;
3090		case IEEE80211_ELEMID_DSPARMS:
3091			if (verbose)
3092				printf(" DSPARMS<%u>", vp[2]);
3093			break;
3094		case IEEE80211_ELEMID_COUNTRY:
3095			if (verbose)
3096				printcountry(" COUNTRY", vp, 2+vp[1], maxcols);
3097			break;
3098		case IEEE80211_ELEMID_ERP:
3099			if (verbose)
3100				printf(" ERP<0x%x>", vp[2]);
3101			break;
3102		case IEEE80211_ELEMID_VENDOR:
3103			if (iswpaoui(vp))
3104				printwpaie(" WPA", vp, 2+vp[1], maxcols);
3105			else if (iswmeinfo(vp))
3106				printwmeinfo(" WME", vp, 2+vp[1], maxcols);
3107			else if (iswmeparam(vp))
3108				printwmeparam(" WME", vp, 2+vp[1], maxcols);
3109			else if (isatherosoui(vp))
3110				printathie(" ATH", vp, 2+vp[1], maxcols);
3111			else if (iswpsoui(vp))
3112				printwpsie(" WPS", vp, 2+vp[1], maxcols);
3113			else if (istdmaoui(vp))
3114				printtdmaie(" TDMA", vp, 2+vp[1], maxcols);
3115			else if (verbose)
3116				printie(" VEN", vp, 2+vp[1], maxcols);
3117			break;
3118		case IEEE80211_ELEMID_RSN:
3119			printrsnie(" RSN", vp, 2+vp[1], maxcols);
3120			break;
3121		case IEEE80211_ELEMID_HTCAP:
3122			printhtcap(" HTCAP", vp, 2+vp[1], maxcols);
3123			break;
3124		case IEEE80211_ELEMID_HTINFO:
3125			if (verbose)
3126				printhtinfo(" HTINFO", vp, 2+vp[1], maxcols);
3127			break;
3128		case IEEE80211_ELEMID_MESHID:
3129			if (verbose)
3130				printssid(" MESHID", vp, 2+vp[1], maxcols);
3131			break;
3132		case IEEE80211_ELEMID_MESHCONF:
3133			printmeshconf(" MESHCONF", vp, 2+vp[1], maxcols);
3134			break;
3135		default:
3136			if (verbose)
3137				printie(iename(vp[0]), vp, 2+vp[1], maxcols);
3138			break;
3139		}
3140		ielen -= 2+vp[1];
3141		vp += 2+vp[1];
3142	}
3143}
3144
3145static void
3146printmimo(const struct ieee80211_mimo_info *mi)
3147{
3148	/* NB: don't muddy display unless there's something to show */
3149	if (mi->rssi[0] != 0 || mi->rssi[1] != 0 || mi->rssi[2] != 0) {
3150		/* XXX ignore EVM for now */
3151		printf(" (rssi %d:%d:%d nf %d:%d:%d)",
3152		    mi->rssi[0], mi->rssi[1], mi->rssi[2],
3153		    mi->noise[0], mi->noise[1], mi->noise[2]);
3154	}
3155}
3156
3157static void
3158list_scan(int s)
3159{
3160	uint8_t buf[24*1024];
3161	char ssid[IEEE80211_NWID_LEN+1];
3162	const uint8_t *cp;
3163	int len, ssidmax, idlen;
3164
3165	if (get80211len(s, IEEE80211_IOC_SCAN_RESULTS, buf, sizeof(buf), &len) < 0)
3166		errx(1, "unable to get scan results");
3167	if (len < sizeof(struct ieee80211req_scan_result))
3168		return;
3169
3170	getchaninfo(s);
3171
3172	ssidmax = verbose ? IEEE80211_NWID_LEN - 1 : 14;
3173	printf("%-*.*s  %-17.17s  %4s %4s  %-7s  %3s %4s\n"
3174		, ssidmax, ssidmax, "SSID/MESH ID"
3175		, "BSSID"
3176		, "CHAN"
3177		, "RATE"
3178		, " S:N"
3179		, "INT"
3180		, "CAPS"
3181	);
3182	cp = buf;
3183	do {
3184		const struct ieee80211req_scan_result *sr;
3185		const uint8_t *vp, *idp;
3186
3187		sr = (const struct ieee80211req_scan_result *) cp;
3188		vp = cp + sr->isr_ie_off;
3189		if (sr->isr_meshid_len) {
3190			idp = vp + sr->isr_ssid_len;
3191			idlen = sr->isr_meshid_len;
3192		} else {
3193			idp = vp;
3194			idlen = sr->isr_ssid_len;
3195		}
3196		printf("%-*.*s  %s  %3d  %3dM %3d:%-3d  %3d %-4.4s"
3197			, ssidmax
3198			  , copy_essid(ssid, ssidmax, idp, idlen)
3199			  , ssid
3200			, ether_ntoa((const struct ether_addr *) sr->isr_bssid)
3201			, ieee80211_mhz2ieee(sr->isr_freq, sr->isr_flags)
3202			, getmaxrate(sr->isr_rates, sr->isr_nrates)
3203			, (sr->isr_rssi/2)+sr->isr_noise, sr->isr_noise
3204			, sr->isr_intval
3205			, getcaps(sr->isr_capinfo)
3206		);
3207		printies(vp + sr->isr_ssid_len + sr->isr_meshid_len,
3208		    sr->isr_ie_len, 24);
3209		printf("\n");
3210		cp += sr->isr_len, len -= sr->isr_len;
3211	} while (len >= sizeof(struct ieee80211req_scan_result));
3212}
3213
3214static void
3215scan_and_wait(int s)
3216{
3217	struct ieee80211_scan_req sr;
3218	struct ieee80211req ireq;
3219	int sroute;
3220
3221	sroute = socket(PF_ROUTE, SOCK_RAW, 0);
3222	if (sroute < 0) {
3223		perror("socket(PF_ROUTE,SOCK_RAW)");
3224		return;
3225	}
3226	(void) memset(&ireq, 0, sizeof(ireq));
3227	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3228	ireq.i_type = IEEE80211_IOC_SCAN_REQ;
3229
3230	memset(&sr, 0, sizeof(sr));
3231	sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE
3232		    | IEEE80211_IOC_SCAN_NOPICK
3233		    | IEEE80211_IOC_SCAN_ONCE;
3234	sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
3235	sr.sr_nssid = 0;
3236
3237	ireq.i_data = &sr;
3238	ireq.i_len = sizeof(sr);
3239	/* NB: only root can trigger a scan so ignore errors */
3240	if (ioctl(s, SIOCS80211, &ireq) >= 0) {
3241		char buf[2048];
3242		struct if_announcemsghdr *ifan;
3243		struct rt_msghdr *rtm;
3244
3245		do {
3246			if (read(sroute, buf, sizeof(buf)) < 0) {
3247				perror("read(PF_ROUTE)");
3248				break;
3249			}
3250			rtm = (struct rt_msghdr *) buf;
3251			if (rtm->rtm_version != RTM_VERSION)
3252				break;
3253			ifan = (struct if_announcemsghdr *) rtm;
3254		} while (rtm->rtm_type != RTM_IEEE80211 ||
3255		    ifan->ifan_what != RTM_IEEE80211_SCAN);
3256	}
3257	close(sroute);
3258}
3259
3260static
3261DECL_CMD_FUNC(set80211scan, val, d)
3262{
3263	scan_and_wait(s);
3264	list_scan(s);
3265}
3266
3267static enum ieee80211_opmode get80211opmode(int s);
3268
3269static int
3270gettxseq(const struct ieee80211req_sta_info *si)
3271{
3272	int i, txseq;
3273
3274	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3275		return si->isi_txseqs[0];
3276	/* XXX not right but usually what folks want */
3277	txseq = 0;
3278	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3279		if (si->isi_txseqs[i] > txseq)
3280			txseq = si->isi_txseqs[i];
3281	return txseq;
3282}
3283
3284static int
3285getrxseq(const struct ieee80211req_sta_info *si)
3286{
3287	int i, rxseq;
3288
3289	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3290		return si->isi_rxseqs[0];
3291	/* XXX not right but usually what folks want */
3292	rxseq = 0;
3293	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3294		if (si->isi_rxseqs[i] > rxseq)
3295			rxseq = si->isi_rxseqs[i];
3296	return rxseq;
3297}
3298
3299static void
3300list_stations(int s)
3301{
3302	union {
3303		struct ieee80211req_sta_req req;
3304		uint8_t buf[24*1024];
3305	} u;
3306	enum ieee80211_opmode opmode = get80211opmode(s);
3307	const uint8_t *cp;
3308	int len;
3309
3310	/* broadcast address =>'s get all stations */
3311	(void) memset(u.req.is_u.macaddr, 0xff, IEEE80211_ADDR_LEN);
3312	if (opmode == IEEE80211_M_STA) {
3313		/*
3314		 * Get information about the associated AP.
3315		 */
3316		(void) get80211(s, IEEE80211_IOC_BSSID,
3317		    u.req.is_u.macaddr, IEEE80211_ADDR_LEN);
3318	}
3319	if (get80211len(s, IEEE80211_IOC_STA_INFO, &u, sizeof(u), &len) < 0)
3320		errx(1, "unable to get station information");
3321	if (len < sizeof(struct ieee80211req_sta_info))
3322		return;
3323
3324	getchaninfo(s);
3325
3326	if (opmode == IEEE80211_M_MBSS)
3327		printf("%-17.17s %4s %5s %5s %7s %4s %4s %4s %6s %6s\n"
3328			, "ADDR"
3329			, "CHAN"
3330			, "LOCAL"
3331			, "PEER"
3332			, "STATE"
3333			, "RATE"
3334			, "RSSI"
3335			, "IDLE"
3336			, "TXSEQ"
3337			, "RXSEQ"
3338		);
3339	else
3340		printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %-7s\n"
3341			, "ADDR"
3342			, "AID"
3343			, "CHAN"
3344			, "RATE"
3345			, "RSSI"
3346			, "IDLE"
3347			, "TXSEQ"
3348			, "RXSEQ"
3349			, "CAPS"
3350			, "FLAG"
3351		);
3352	cp = (const uint8_t *) u.req.info;
3353	do {
3354		const struct ieee80211req_sta_info *si;
3355
3356		si = (const struct ieee80211req_sta_info *) cp;
3357		if (si->isi_len < sizeof(*si))
3358			break;
3359		if (opmode == IEEE80211_M_MBSS)
3360			printf("%s %4d %5x %5x %7.7s %3dM %4.1f %4d %6d %6d"
3361				, ether_ntoa((const struct ether_addr*)
3362				    si->isi_macaddr)
3363				, ieee80211_mhz2ieee(si->isi_freq,
3364				    si->isi_flags)
3365				, si->isi_localid
3366				, si->isi_peerid
3367				, mesh_linkstate_string(si->isi_peerstate)
3368				, si->isi_txmbps/2
3369				, si->isi_rssi/2.
3370				, si->isi_inact
3371				, gettxseq(si)
3372				, getrxseq(si)
3373			);
3374		else
3375			printf("%s %4u %4d %3dM %4.1f %4d %6d %6d %-4.4s %-7.7s"
3376				, ether_ntoa((const struct ether_addr*)
3377				    si->isi_macaddr)
3378				, IEEE80211_AID(si->isi_associd)
3379				, ieee80211_mhz2ieee(si->isi_freq,
3380				    si->isi_flags)
3381				, si->isi_txmbps/2
3382				, si->isi_rssi/2.
3383				, si->isi_inact
3384				, gettxseq(si)
3385				, getrxseq(si)
3386				, getcaps(si->isi_capinfo)
3387				, getflags(si->isi_state)
3388			);
3389		printies(cp + si->isi_ie_off, si->isi_ie_len, 24);
3390		printmimo(&si->isi_mimo);
3391		printf("\n");
3392		cp += si->isi_len, len -= si->isi_len;
3393	} while (len >= sizeof(struct ieee80211req_sta_info));
3394}
3395
3396static const char *
3397mesh_linkstate_string(uint8_t state)
3398{
3399#define	N(a)	(sizeof(a) / sizeof(a[0]))
3400	static const char *state_names[] = {
3401	    [0] = "IDLE",
3402	    [1] = "OPEN-TX",
3403	    [2] = "OPEN-RX",
3404	    [3] = "CONF-RX",
3405	    [4] = "ESTAB",
3406	    [5] = "HOLDING",
3407	};
3408
3409	if (state >= N(state_names)) {
3410		static char buf[10];
3411		snprintf(buf, sizeof(buf), "#%u", state);
3412		return buf;
3413	} else
3414		return state_names[state];
3415#undef N
3416}
3417
3418static const char *
3419get_chaninfo(const struct ieee80211_channel *c, int precise,
3420	char buf[], size_t bsize)
3421{
3422	buf[0] = '\0';
3423	if (IEEE80211_IS_CHAN_FHSS(c))
3424		strlcat(buf, " FHSS", bsize);
3425	if (IEEE80211_IS_CHAN_A(c))
3426		strlcat(buf, " 11a", bsize);
3427	else if (IEEE80211_IS_CHAN_ANYG(c))
3428		strlcat(buf, " 11g", bsize);
3429	else if (IEEE80211_IS_CHAN_B(c))
3430		strlcat(buf, " 11b", bsize);
3431	if (IEEE80211_IS_CHAN_HALF(c))
3432		strlcat(buf, "/10Mhz", bsize);
3433	if (IEEE80211_IS_CHAN_QUARTER(c))
3434		strlcat(buf, "/5Mhz", bsize);
3435	if (IEEE80211_IS_CHAN_TURBO(c))
3436		strlcat(buf, " Turbo", bsize);
3437	if (precise) {
3438		if (IEEE80211_IS_CHAN_HT20(c))
3439			strlcat(buf, " ht/20", bsize);
3440		else if (IEEE80211_IS_CHAN_HT40D(c))
3441			strlcat(buf, " ht/40-", bsize);
3442		else if (IEEE80211_IS_CHAN_HT40U(c))
3443			strlcat(buf, " ht/40+", bsize);
3444	} else {
3445		if (IEEE80211_IS_CHAN_HT(c))
3446			strlcat(buf, " ht", bsize);
3447	}
3448	return buf;
3449}
3450
3451static void
3452print_chaninfo(const struct ieee80211_channel *c, int verb)
3453{
3454	char buf[14];
3455
3456	printf("Channel %3u : %u%c Mhz%-14.14s",
3457		ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
3458		IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
3459		get_chaninfo(c, verb, buf, sizeof(buf)));
3460}
3461
3462static int
3463chanpref(const struct ieee80211_channel *c)
3464{
3465	if (IEEE80211_IS_CHAN_HT40(c))
3466		return 40;
3467	if (IEEE80211_IS_CHAN_HT20(c))
3468		return 30;
3469	if (IEEE80211_IS_CHAN_HALF(c))
3470		return 10;
3471	if (IEEE80211_IS_CHAN_QUARTER(c))
3472		return 5;
3473	if (IEEE80211_IS_CHAN_TURBO(c))
3474		return 25;
3475	if (IEEE80211_IS_CHAN_A(c))
3476		return 20;
3477	if (IEEE80211_IS_CHAN_G(c))
3478		return 20;
3479	if (IEEE80211_IS_CHAN_B(c))
3480		return 15;
3481	if (IEEE80211_IS_CHAN_PUREG(c))
3482		return 15;
3483	return 0;
3484}
3485
3486static void
3487print_channels(int s, const struct ieee80211req_chaninfo *chans,
3488	int allchans, int verb)
3489{
3490	struct ieee80211req_chaninfo *achans;
3491	uint8_t reported[IEEE80211_CHAN_BYTES];
3492	const struct ieee80211_channel *c;
3493	int i, half;
3494
3495	achans = malloc(IEEE80211_CHANINFO_SPACE(chans));
3496	if (achans == NULL)
3497		errx(1, "no space for active channel list");
3498	achans->ic_nchans = 0;
3499	memset(reported, 0, sizeof(reported));
3500	if (!allchans) {
3501		struct ieee80211req_chanlist active;
3502
3503		if (get80211(s, IEEE80211_IOC_CHANLIST, &active, sizeof(active)) < 0)
3504			errx(1, "unable to get active channel list");
3505		for (i = 0; i < chans->ic_nchans; i++) {
3506			c = &chans->ic_chans[i];
3507			if (!isset(active.ic_channels, c->ic_ieee))
3508				continue;
3509			/*
3510			 * Suppress compatible duplicates unless
3511			 * verbose.  The kernel gives us it's
3512			 * complete channel list which has separate
3513			 * entries for 11g/11b and 11a/turbo.
3514			 */
3515			if (isset(reported, c->ic_ieee) && !verb) {
3516				/* XXX we assume duplicates are adjacent */
3517				achans->ic_chans[achans->ic_nchans-1] = *c;
3518			} else {
3519				achans->ic_chans[achans->ic_nchans++] = *c;
3520				setbit(reported, c->ic_ieee);
3521			}
3522		}
3523	} else {
3524		for (i = 0; i < chans->ic_nchans; i++) {
3525			c = &chans->ic_chans[i];
3526			/* suppress duplicates as above */
3527			if (isset(reported, c->ic_ieee) && !verb) {
3528				/* XXX we assume duplicates are adjacent */
3529				struct ieee80211_channel *a =
3530				    &achans->ic_chans[achans->ic_nchans-1];
3531				if (chanpref(c) > chanpref(a))
3532					*a = *c;
3533			} else {
3534				achans->ic_chans[achans->ic_nchans++] = *c;
3535				setbit(reported, c->ic_ieee);
3536			}
3537		}
3538	}
3539	half = achans->ic_nchans / 2;
3540	if (achans->ic_nchans % 2)
3541		half++;
3542
3543	for (i = 0; i < achans->ic_nchans / 2; i++) {
3544		print_chaninfo(&achans->ic_chans[i], verb);
3545		print_chaninfo(&achans->ic_chans[half+i], verb);
3546		printf("\n");
3547	}
3548	if (achans->ic_nchans % 2) {
3549		print_chaninfo(&achans->ic_chans[i], verb);
3550		printf("\n");
3551	}
3552	free(achans);
3553}
3554
3555static void
3556list_channels(int s, int allchans)
3557{
3558	getchaninfo(s);
3559	print_channels(s, chaninfo, allchans, verbose);
3560}
3561
3562static void
3563print_txpow(const struct ieee80211_channel *c)
3564{
3565	printf("Channel %3u : %u Mhz %3.1f reg %2d  ",
3566	    c->ic_ieee, c->ic_freq,
3567	    c->ic_maxpower/2., c->ic_maxregpower);
3568}
3569
3570static void
3571print_txpow_verbose(const struct ieee80211_channel *c)
3572{
3573	print_chaninfo(c, 1);
3574	printf("min %4.1f dBm  max %3.1f dBm  reg %2d dBm",
3575	    c->ic_minpower/2., c->ic_maxpower/2., c->ic_maxregpower);
3576	/* indicate where regulatory cap limits power use */
3577	if (c->ic_maxpower > 2*c->ic_maxregpower)
3578		printf(" <");
3579}
3580
3581static void
3582list_txpow(int s)
3583{
3584	struct ieee80211req_chaninfo *achans;
3585	uint8_t reported[IEEE80211_CHAN_BYTES];
3586	struct ieee80211_channel *c, *prev;
3587	int i, half;
3588
3589	getchaninfo(s);
3590	achans = malloc(IEEE80211_CHANINFO_SPACE(chaninfo));
3591	if (achans == NULL)
3592		errx(1, "no space for active channel list");
3593	achans->ic_nchans = 0;
3594	memset(reported, 0, sizeof(reported));
3595	for (i = 0; i < chaninfo->ic_nchans; i++) {
3596		c = &chaninfo->ic_chans[i];
3597		/* suppress duplicates as above */
3598		if (isset(reported, c->ic_ieee) && !verbose) {
3599			/* XXX we assume duplicates are adjacent */
3600			prev = &achans->ic_chans[achans->ic_nchans-1];
3601			/* display highest power on channel */
3602			if (c->ic_maxpower > prev->ic_maxpower)
3603				*prev = *c;
3604		} else {
3605			achans->ic_chans[achans->ic_nchans++] = *c;
3606			setbit(reported, c->ic_ieee);
3607		}
3608	}
3609	if (!verbose) {
3610		half = achans->ic_nchans / 2;
3611		if (achans->ic_nchans % 2)
3612			half++;
3613
3614		for (i = 0; i < achans->ic_nchans / 2; i++) {
3615			print_txpow(&achans->ic_chans[i]);
3616			print_txpow(&achans->ic_chans[half+i]);
3617			printf("\n");
3618		}
3619		if (achans->ic_nchans % 2) {
3620			print_txpow(&achans->ic_chans[i]);
3621			printf("\n");
3622		}
3623	} else {
3624		for (i = 0; i < achans->ic_nchans; i++) {
3625			print_txpow_verbose(&achans->ic_chans[i]);
3626			printf("\n");
3627		}
3628	}
3629	free(achans);
3630}
3631
3632static void
3633list_keys(int s)
3634{
3635}
3636
3637#define	IEEE80211_C_BITS \
3638	"\20\1STA\002803ENCAP\7FF\10TURBOP\11IBSS\12PMGT" \
3639	"\13HOSTAP\14AHDEMO\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE" \
3640	"\21MONITOR\22DFS\23MBSS\30WPA1\31WPA2\32BURST\33WME\34WDS\36BGSCAN" \
3641	"\37TXFRAG\40TDMA"
3642
3643static void
3644list_capabilities(int s)
3645{
3646	struct ieee80211_devcaps_req *dc;
3647
3648	if (verbose)
3649		dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
3650	else
3651		dc = malloc(IEEE80211_DEVCAPS_SIZE(1));
3652	if (dc == NULL)
3653		errx(1, "no space for device capabilities");
3654	dc->dc_chaninfo.ic_nchans = verbose ? MAXCHAN : 1;
3655	getdevcaps(s, dc);
3656	printb("drivercaps", dc->dc_drivercaps, IEEE80211_C_BITS);
3657	if (dc->dc_cryptocaps != 0 || verbose) {
3658		putchar('\n');
3659		printb("cryptocaps", dc->dc_cryptocaps, IEEE80211_CRYPTO_BITS);
3660	}
3661	if (dc->dc_htcaps != 0 || verbose) {
3662		putchar('\n');
3663		printb("htcaps", dc->dc_htcaps, IEEE80211_HTCAP_BITS);
3664	}
3665	putchar('\n');
3666	if (verbose) {
3667		chaninfo = &dc->dc_chaninfo;	/* XXX */
3668		print_channels(s, &dc->dc_chaninfo, 1/*allchans*/, verbose);
3669	}
3670	free(dc);
3671}
3672
3673static int
3674get80211wme(int s, int param, int ac, int *val)
3675{
3676	struct ieee80211req ireq;
3677
3678	(void) memset(&ireq, 0, sizeof(ireq));
3679	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3680	ireq.i_type = param;
3681	ireq.i_len = ac;
3682	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3683		warn("cannot get WME parameter %d, ac %d%s",
3684		    param, ac & IEEE80211_WMEPARAM_VAL,
3685		    ac & IEEE80211_WMEPARAM_BSS ? " (BSS)" : "");
3686		return -1;
3687	}
3688	*val = ireq.i_val;
3689	return 0;
3690}
3691
3692static void
3693list_wme_aci(int s, const char *tag, int ac)
3694{
3695	int val;
3696
3697	printf("\t%s", tag);
3698
3699	/* show WME BSS parameters */
3700	if (get80211wme(s, IEEE80211_IOC_WME_CWMIN, ac, &val) != -1)
3701		printf(" cwmin %2u", val);
3702	if (get80211wme(s, IEEE80211_IOC_WME_CWMAX, ac, &val) != -1)
3703		printf(" cwmax %2u", val);
3704	if (get80211wme(s, IEEE80211_IOC_WME_AIFS, ac, &val) != -1)
3705		printf(" aifs %2u", val);
3706	if (get80211wme(s, IEEE80211_IOC_WME_TXOPLIMIT, ac, &val) != -1)
3707		printf(" txopLimit %3u", val);
3708	if (get80211wme(s, IEEE80211_IOC_WME_ACM, ac, &val) != -1) {
3709		if (val)
3710			printf(" acm");
3711		else if (verbose)
3712			printf(" -acm");
3713	}
3714	/* !BSS only */
3715	if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3716		if (get80211wme(s, IEEE80211_IOC_WME_ACKPOLICY, ac, &val) != -1) {
3717			if (!val)
3718				printf(" -ack");
3719			else if (verbose)
3720				printf(" ack");
3721		}
3722	}
3723	printf("\n");
3724}
3725
3726static void
3727list_wme(int s)
3728{
3729	static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
3730	int ac;
3731
3732	if (verbose) {
3733		/* display both BSS and local settings */
3734		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
3735	again:
3736			if (ac & IEEE80211_WMEPARAM_BSS)
3737				list_wme_aci(s, "     ", ac);
3738			else
3739				list_wme_aci(s, acnames[ac], ac);
3740			if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
3741				ac |= IEEE80211_WMEPARAM_BSS;
3742				goto again;
3743			} else
3744				ac &= ~IEEE80211_WMEPARAM_BSS;
3745		}
3746	} else {
3747		/* display only channel settings */
3748		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++)
3749			list_wme_aci(s, acnames[ac], ac);
3750	}
3751}
3752
3753static void
3754list_roam(int s)
3755{
3756	const struct ieee80211_roamparam *rp;
3757	int mode;
3758
3759	getroam(s);
3760	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
3761		rp = &roamparams.params[mode];
3762		if (rp->rssi == 0 && rp->rate == 0)
3763			continue;
3764		if (mode == IEEE80211_MODE_11NA || mode == IEEE80211_MODE_11NG) {
3765			if (rp->rssi & 1)
3766				LINE_CHECK("roam:%-7.7s rssi %2u.5dBm  MCS %2u    ",
3767				    modename[mode], rp->rssi/2,
3768				    rp->rate &~ IEEE80211_RATE_MCS);
3769			else
3770				LINE_CHECK("roam:%-7.7s rssi %4udBm  MCS %2u    ",
3771				    modename[mode], rp->rssi/2,
3772				    rp->rate &~ IEEE80211_RATE_MCS);
3773		} else {
3774			if (rp->rssi & 1)
3775				LINE_CHECK("roam:%-7.7s rssi %2u.5dBm rate %2u Mb/s",
3776				    modename[mode], rp->rssi/2, rp->rate/2);
3777			else
3778				LINE_CHECK("roam:%-7.7s rssi %4udBm rate %2u Mb/s",
3779				    modename[mode], rp->rssi/2, rp->rate/2);
3780		}
3781	}
3782}
3783
3784static void
3785list_txparams(int s)
3786{
3787	const struct ieee80211_txparam *tp;
3788	int mode;
3789
3790	gettxparams(s);
3791	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
3792		tp = &txparams.params[mode];
3793		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
3794			continue;
3795		if (mode == IEEE80211_MODE_11NA || mode == IEEE80211_MODE_11NG) {
3796			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3797				LINE_CHECK("%-7.7s ucast NONE    mgmt %2u MCS  "
3798				    "mcast %2u MCS  maxretry %u",
3799				    modename[mode],
3800				    tp->mgmtrate &~ IEEE80211_RATE_MCS,
3801				    tp->mcastrate &~ IEEE80211_RATE_MCS,
3802				    tp->maxretry);
3803			else
3804				LINE_CHECK("%-7.7s ucast %2u MCS  mgmt %2u MCS  "
3805				    "mcast %2u MCS  maxretry %u",
3806				    modename[mode],
3807				    tp->ucastrate &~ IEEE80211_RATE_MCS,
3808				    tp->mgmtrate &~ IEEE80211_RATE_MCS,
3809				    tp->mcastrate &~ IEEE80211_RATE_MCS,
3810				    tp->maxretry);
3811		} else {
3812			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
3813				LINE_CHECK("%-7.7s ucast NONE    mgmt %2u Mb/s "
3814				    "mcast %2u Mb/s maxretry %u",
3815				    modename[mode],
3816				    tp->mgmtrate/2,
3817				    tp->mcastrate/2, tp->maxretry);
3818			else
3819				LINE_CHECK("%-7.7s ucast %2u Mb/s mgmt %2u Mb/s "
3820				    "mcast %2u Mb/s maxretry %u",
3821				    modename[mode],
3822				    tp->ucastrate/2, tp->mgmtrate/2,
3823				    tp->mcastrate/2, tp->maxretry);
3824		}
3825	}
3826}
3827
3828static void
3829printpolicy(int policy)
3830{
3831	switch (policy) {
3832	case IEEE80211_MACCMD_POLICY_OPEN:
3833		printf("policy: open\n");
3834		break;
3835	case IEEE80211_MACCMD_POLICY_ALLOW:
3836		printf("policy: allow\n");
3837		break;
3838	case IEEE80211_MACCMD_POLICY_DENY:
3839		printf("policy: deny\n");
3840		break;
3841	case IEEE80211_MACCMD_POLICY_RADIUS:
3842		printf("policy: radius\n");
3843		break;
3844	default:
3845		printf("policy: unknown (%u)\n", policy);
3846		break;
3847	}
3848}
3849
3850static void
3851list_mac(int s)
3852{
3853	struct ieee80211req ireq;
3854	struct ieee80211req_maclist *acllist;
3855	int i, nacls, policy, len;
3856	uint8_t *data;
3857	char c;
3858
3859	(void) memset(&ireq, 0, sizeof(ireq));
3860	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
3861	ireq.i_type = IEEE80211_IOC_MACCMD;
3862	ireq.i_val = IEEE80211_MACCMD_POLICY;
3863	if (ioctl(s, SIOCG80211, &ireq) < 0) {
3864		if (errno == EINVAL) {
3865			printf("No acl policy loaded\n");
3866			return;
3867		}
3868		err(1, "unable to get mac policy");
3869	}
3870	policy = ireq.i_val;
3871	if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
3872		c = '*';
3873	} else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
3874		c = '+';
3875	} else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
3876		c = '-';
3877	} else if (policy == IEEE80211_MACCMD_POLICY_RADIUS) {
3878		c = 'r';		/* NB: should never have entries */
3879	} else {
3880		printf("policy: unknown (%u)\n", policy);
3881		c = '?';
3882	}
3883	if (verbose || c == '?')
3884		printpolicy(policy);
3885
3886	ireq.i_val = IEEE80211_MACCMD_LIST;
3887	ireq.i_len = 0;
3888	if (ioctl(s, SIOCG80211, &ireq) < 0)
3889		err(1, "unable to get mac acl list size");
3890	if (ireq.i_len == 0) {		/* NB: no acls */
3891		if (!(verbose || c == '?'))
3892			printpolicy(policy);
3893		return;
3894	}
3895	len = ireq.i_len;
3896
3897	data = malloc(len);
3898	if (data == NULL)
3899		err(1, "out of memory for acl list");
3900
3901	ireq.i_data = data;
3902	if (ioctl(s, SIOCG80211, &ireq) < 0)
3903		err(1, "unable to get mac acl list");
3904	nacls = len / sizeof(*acllist);
3905	acllist = (struct ieee80211req_maclist *) data;
3906	for (i = 0; i < nacls; i++)
3907		printf("%c%s\n", c, ether_ntoa(
3908			(const struct ether_addr *) acllist[i].ml_macaddr));
3909	free(data);
3910}
3911
3912static void
3913print_regdomain(const struct ieee80211_regdomain *reg, int verb)
3914{
3915	if ((reg->regdomain != 0 &&
3916	    reg->regdomain != reg->country) || verb) {
3917		const struct regdomain *rd =
3918		    lib80211_regdomain_findbysku(getregdata(), reg->regdomain);
3919		if (rd == NULL)
3920			LINE_CHECK("regdomain %d", reg->regdomain);
3921		else
3922			LINE_CHECK("regdomain %s", rd->name);
3923	}
3924	if (reg->country != 0 || verb) {
3925		const struct country *cc =
3926		    lib80211_country_findbycc(getregdata(), reg->country);
3927		if (cc == NULL)
3928			LINE_CHECK("country %d", reg->country);
3929		else
3930			LINE_CHECK("country %s", cc->isoname);
3931	}
3932	if (reg->location == 'I')
3933		LINE_CHECK("indoor");
3934	else if (reg->location == 'O')
3935		LINE_CHECK("outdoor");
3936	else if (verb)
3937		LINE_CHECK("anywhere");
3938	if (reg->ecm)
3939		LINE_CHECK("ecm");
3940	else if (verb)
3941		LINE_CHECK("-ecm");
3942}
3943
3944static void
3945list_regdomain(int s, int channelsalso)
3946{
3947	getregdomain(s);
3948	if (channelsalso) {
3949		getchaninfo(s);
3950		spacer = ':';
3951		print_regdomain(&regdomain, 1);
3952		LINE_BREAK();
3953		print_channels(s, chaninfo, 1/*allchans*/, 1/*verbose*/);
3954	} else
3955		print_regdomain(&regdomain, verbose);
3956}
3957
3958static void
3959list_mesh(int s)
3960{
3961	struct ieee80211req ireq;
3962	struct ieee80211req_mesh_route routes[128];
3963	struct ieee80211req_mesh_route *rt;
3964
3965	(void) memset(&ireq, 0, sizeof(ireq));
3966	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
3967	ireq.i_type = IEEE80211_IOC_MESH_RTCMD;
3968	ireq.i_val = IEEE80211_MESH_RTCMD_LIST;
3969	ireq.i_data = &routes;
3970	ireq.i_len = sizeof(routes);
3971	if (ioctl(s, SIOCG80211, &ireq) < 0)
3972	 	err(1, "unable to get the Mesh routing table");
3973
3974	printf("%-17.17s %-17.17s %4s %4s %4s %6s %s\n"
3975		, "DEST"
3976		, "NEXT HOP"
3977		, "HOPS"
3978		, "METRIC"
3979		, "LIFETIME"
3980		, "MSEQ"
3981		, "FLAGS");
3982
3983	for (rt = &routes[0]; rt - &routes[0] < ireq.i_len / sizeof(*rt); rt++){
3984		printf("%s ",
3985		    ether_ntoa((const struct ether_addr *)rt->imr_dest));
3986		printf("%s %4u   %4u   %6u %6u    %c%c\n",
3987			ether_ntoa((const struct ether_addr *)rt->imr_nexthop),
3988			rt->imr_nhops, rt->imr_metric, rt->imr_lifetime,
3989			rt->imr_lastmseq,
3990			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_VALID) ?
3991			    'V' : '!',
3992			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_PROXY) ?
3993			    'P' : ' ');
3994	}
3995}
3996
3997static
3998DECL_CMD_FUNC(set80211list, arg, d)
3999{
4000#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
4001
4002	LINE_INIT('\t');
4003
4004	if (iseq(arg, "sta"))
4005		list_stations(s);
4006	else if (iseq(arg, "scan") || iseq(arg, "ap"))
4007		list_scan(s);
4008	else if (iseq(arg, "chan") || iseq(arg, "freq"))
4009		list_channels(s, 1);
4010	else if (iseq(arg, "active"))
4011		list_channels(s, 0);
4012	else if (iseq(arg, "keys"))
4013		list_keys(s);
4014	else if (iseq(arg, "caps"))
4015		list_capabilities(s);
4016	else if (iseq(arg, "wme") || iseq(arg, "wmm"))
4017		list_wme(s);
4018	else if (iseq(arg, "mac"))
4019		list_mac(s);
4020	else if (iseq(arg, "txpow"))
4021		list_txpow(s);
4022	else if (iseq(arg, "roam"))
4023		list_roam(s);
4024	else if (iseq(arg, "txparam") || iseq(arg, "txparm"))
4025		list_txparams(s);
4026	else if (iseq(arg, "regdomain"))
4027		list_regdomain(s, 1);
4028	else if (iseq(arg, "countries"))
4029		list_countries();
4030	else if (iseq(arg, "mesh"))
4031		list_mesh(s);
4032	else
4033		errx(1, "Don't know how to list %s for %s", arg, name);
4034	LINE_BREAK();
4035#undef iseq
4036}
4037
4038static enum ieee80211_opmode
4039get80211opmode(int s)
4040{
4041	struct ifmediareq ifmr;
4042
4043	(void) memset(&ifmr, 0, sizeof(ifmr));
4044	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
4045
4046	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
4047		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) {
4048			if (ifmr.ifm_current & IFM_FLAG0)
4049				return IEEE80211_M_AHDEMO;
4050			else
4051				return IEEE80211_M_IBSS;
4052		}
4053		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
4054			return IEEE80211_M_HOSTAP;
4055		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
4056			return IEEE80211_M_MONITOR;
4057		if (ifmr.ifm_current & IFM_IEEE80211_MBSS)
4058			return IEEE80211_M_MBSS;
4059	}
4060	return IEEE80211_M_STA;
4061}
4062
4063#if 0
4064static void
4065printcipher(int s, struct ieee80211req *ireq, int keylenop)
4066{
4067	switch (ireq->i_val) {
4068	case IEEE80211_CIPHER_WEP:
4069		ireq->i_type = keylenop;
4070		if (ioctl(s, SIOCG80211, ireq) != -1)
4071			printf("WEP-%s",
4072			    ireq->i_len <= 5 ? "40" :
4073			    ireq->i_len <= 13 ? "104" : "128");
4074		else
4075			printf("WEP");
4076		break;
4077	case IEEE80211_CIPHER_TKIP:
4078		printf("TKIP");
4079		break;
4080	case IEEE80211_CIPHER_AES_OCB:
4081		printf("AES-OCB");
4082		break;
4083	case IEEE80211_CIPHER_AES_CCM:
4084		printf("AES-CCM");
4085		break;
4086	case IEEE80211_CIPHER_CKIP:
4087		printf("CKIP");
4088		break;
4089	case IEEE80211_CIPHER_NONE:
4090		printf("NONE");
4091		break;
4092	default:
4093		printf("UNKNOWN (0x%x)", ireq->i_val);
4094		break;
4095	}
4096}
4097#endif
4098
4099static void
4100printkey(const struct ieee80211req_key *ik)
4101{
4102	static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
4103	int keylen = ik->ik_keylen;
4104	int printcontents;
4105
4106	printcontents = printkeys &&
4107		(memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
4108	if (printcontents)
4109		LINE_BREAK();
4110	switch (ik->ik_type) {
4111	case IEEE80211_CIPHER_WEP:
4112		/* compatibility */
4113		LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
4114		    keylen <= 5 ? "40-bit" :
4115		    keylen <= 13 ? "104-bit" : "128-bit");
4116		break;
4117	case IEEE80211_CIPHER_TKIP:
4118		if (keylen > 128/8)
4119			keylen -= 128/8;	/* ignore MIC for now */
4120		LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4121		break;
4122	case IEEE80211_CIPHER_AES_OCB:
4123		LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4124		break;
4125	case IEEE80211_CIPHER_AES_CCM:
4126		LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4127		break;
4128	case IEEE80211_CIPHER_CKIP:
4129		LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4130		break;
4131	case IEEE80211_CIPHER_NONE:
4132		LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4133		break;
4134	default:
4135		LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
4136			ik->ik_type, ik->ik_keyix+1, 8*keylen);
4137		break;
4138	}
4139	if (printcontents) {
4140		int i;
4141
4142		printf(" <");
4143		for (i = 0; i < keylen; i++)
4144			printf("%02x", ik->ik_keydata[i]);
4145		printf(">");
4146		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4147		    (ik->ik_keyrsc != 0 || verbose))
4148			printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
4149		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4150		    (ik->ik_keytsc != 0 || verbose))
4151			printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
4152		if (ik->ik_flags != 0 && verbose) {
4153			const char *sep = " ";
4154
4155			if (ik->ik_flags & IEEE80211_KEY_XMIT)
4156				printf("%stx", sep), sep = "+";
4157			if (ik->ik_flags & IEEE80211_KEY_RECV)
4158				printf("%srx", sep), sep = "+";
4159			if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
4160				printf("%sdef", sep), sep = "+";
4161		}
4162		LINE_BREAK();
4163	}
4164}
4165
4166static void
4167printrate(const char *tag, int v, int defrate, int defmcs)
4168{
4169	if ((v & IEEE80211_RATE_MCS) == 0) {
4170		if (v != defrate) {
4171			if (v & 1)
4172				LINE_CHECK("%s %d.5", tag, v/2);
4173			else
4174				LINE_CHECK("%s %d", tag, v/2);
4175		}
4176	} else {
4177		if (v != defmcs)
4178			LINE_CHECK("%s %d", tag, v &~ 0x80);
4179	}
4180}
4181
4182static int
4183getid(int s, int ix, void *data, size_t len, int *plen, int mesh)
4184{
4185	struct ieee80211req ireq;
4186
4187	(void) memset(&ireq, 0, sizeof(ireq));
4188	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4189	ireq.i_type = (!mesh) ? IEEE80211_IOC_SSID : IEEE80211_IOC_MESH_ID;
4190	ireq.i_val = ix;
4191	ireq.i_data = data;
4192	ireq.i_len = len;
4193	if (ioctl(s, SIOCG80211, &ireq) < 0)
4194		return -1;
4195	*plen = ireq.i_len;
4196	return 0;
4197}
4198
4199static void
4200ieee80211_status(int s)
4201{
4202	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
4203	enum ieee80211_opmode opmode = get80211opmode(s);
4204	int i, num, wpa, wme, bgscan, bgscaninterval, val, len, wepmode;
4205	uint8_t data[32];
4206	const struct ieee80211_channel *c;
4207	const struct ieee80211_roamparam *rp;
4208	const struct ieee80211_txparam *tp;
4209
4210	if (getid(s, -1, data, sizeof(data), &len, 0) < 0) {
4211		/* If we can't get the SSID, this isn't an 802.11 device. */
4212		return;
4213	}
4214
4215	/*
4216	 * Invalidate cached state so printing status for multiple
4217	 * if's doesn't reuse the first interfaces' cached state.
4218	 */
4219	gotcurchan = 0;
4220	gotroam = 0;
4221	gottxparams = 0;
4222	gothtconf = 0;
4223	gotregdomain = 0;
4224
4225	printf("\t");
4226	if (opmode == IEEE80211_M_MBSS) {
4227		printf("meshid ");
4228		getid(s, 0, data, sizeof(data), &len, 1);
4229		print_string(data, len);
4230	} else {
4231		if (get80211val(s, IEEE80211_IOC_NUMSSIDS, &num) < 0)
4232			num = 0;
4233		printf("ssid ");
4234		if (num > 1) {
4235			for (i = 0; i < num; i++) {
4236				if (getid(s, i, data, sizeof(data), &len, 0) >= 0 && len > 0) {
4237					printf(" %d:", i + 1);
4238					print_string(data, len);
4239				}
4240			}
4241		} else
4242			print_string(data, len);
4243	}
4244	c = getcurchan(s);
4245	if (c->ic_freq != IEEE80211_CHAN_ANY) {
4246		char buf[14];
4247		printf(" channel %d (%u Mhz%s)", c->ic_ieee, c->ic_freq,
4248			get_chaninfo(c, 1, buf, sizeof(buf)));
4249	} else if (verbose)
4250		printf(" channel UNDEF");
4251
4252	if (get80211(s, IEEE80211_IOC_BSSID, data, IEEE80211_ADDR_LEN) >= 0 &&
4253	    (memcmp(data, zerobssid, sizeof(zerobssid)) != 0 || verbose))
4254		printf(" bssid %s", ether_ntoa((struct ether_addr *)data));
4255
4256	if (get80211len(s, IEEE80211_IOC_STATIONNAME, data, sizeof(data), &len) != -1) {
4257		printf("\n\tstationname ");
4258		print_string(data, len);
4259	}
4260
4261	spacer = ' ';		/* force first break */
4262	LINE_BREAK();
4263
4264	list_regdomain(s, 0);
4265
4266	wpa = 0;
4267	if (get80211val(s, IEEE80211_IOC_AUTHMODE, &val) != -1) {
4268		switch (val) {
4269		case IEEE80211_AUTH_NONE:
4270			LINE_CHECK("authmode NONE");
4271			break;
4272		case IEEE80211_AUTH_OPEN:
4273			LINE_CHECK("authmode OPEN");
4274			break;
4275		case IEEE80211_AUTH_SHARED:
4276			LINE_CHECK("authmode SHARED");
4277			break;
4278		case IEEE80211_AUTH_8021X:
4279			LINE_CHECK("authmode 802.1x");
4280			break;
4281		case IEEE80211_AUTH_WPA:
4282			if (get80211val(s, IEEE80211_IOC_WPA, &wpa) < 0)
4283				wpa = 1;	/* default to WPA1 */
4284			switch (wpa) {
4285			case 2:
4286				LINE_CHECK("authmode WPA2/802.11i");
4287				break;
4288			case 3:
4289				LINE_CHECK("authmode WPA1+WPA2/802.11i");
4290				break;
4291			default:
4292				LINE_CHECK("authmode WPA");
4293				break;
4294			}
4295			break;
4296		case IEEE80211_AUTH_AUTO:
4297			LINE_CHECK("authmode AUTO");
4298			break;
4299		default:
4300			LINE_CHECK("authmode UNKNOWN (0x%x)", val);
4301			break;
4302		}
4303	}
4304
4305	if (wpa || verbose) {
4306		if (get80211val(s, IEEE80211_IOC_WPS, &val) != -1) {
4307			if (val)
4308				LINE_CHECK("wps");
4309			else if (verbose)
4310				LINE_CHECK("-wps");
4311		}
4312		if (get80211val(s, IEEE80211_IOC_TSN, &val) != -1) {
4313			if (val)
4314				LINE_CHECK("tsn");
4315			else if (verbose)
4316				LINE_CHECK("-tsn");
4317		}
4318		if (ioctl(s, IEEE80211_IOC_COUNTERMEASURES, &val) != -1) {
4319			if (val)
4320				LINE_CHECK("countermeasures");
4321			else if (verbose)
4322				LINE_CHECK("-countermeasures");
4323		}
4324#if 0
4325		/* XXX not interesting with WPA done in user space */
4326		ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
4327		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4328		}
4329
4330		ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
4331		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4332			LINE_CHECK("mcastcipher ");
4333			printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
4334			spacer = ' ';
4335		}
4336
4337		ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
4338		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4339			LINE_CHECK("ucastcipher ");
4340			printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
4341		}
4342
4343		if (wpa & 2) {
4344			ireq.i_type = IEEE80211_IOC_RSNCAPS;
4345			if (ioctl(s, SIOCG80211, &ireq) != -1) {
4346				LINE_CHECK("RSN caps 0x%x", ireq.i_val);
4347				spacer = ' ';
4348			}
4349		}
4350
4351		ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
4352		if (ioctl(s, SIOCG80211, &ireq) != -1) {
4353		}
4354#endif
4355	}
4356
4357	if (get80211val(s, IEEE80211_IOC_WEP, &wepmode) != -1 &&
4358	    wepmode != IEEE80211_WEP_NOSUP) {
4359		int firstkey;
4360
4361		switch (wepmode) {
4362		case IEEE80211_WEP_OFF:
4363			LINE_CHECK("privacy OFF");
4364			break;
4365		case IEEE80211_WEP_ON:
4366			LINE_CHECK("privacy ON");
4367			break;
4368		case IEEE80211_WEP_MIXED:
4369			LINE_CHECK("privacy MIXED");
4370			break;
4371		default:
4372			LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
4373			break;
4374		}
4375
4376		/*
4377		 * If we get here then we've got WEP support so we need
4378		 * to print WEP status.
4379		 */
4380
4381		if (get80211val(s, IEEE80211_IOC_WEPTXKEY, &val) < 0) {
4382			warn("WEP support, but no tx key!");
4383			goto end;
4384		}
4385		if (val != -1)
4386			LINE_CHECK("deftxkey %d", val+1);
4387		else if (wepmode != IEEE80211_WEP_OFF || verbose)
4388			LINE_CHECK("deftxkey UNDEF");
4389
4390		if (get80211val(s, IEEE80211_IOC_NUMWEPKEYS, &num) < 0) {
4391			warn("WEP support, but no NUMWEPKEYS support!");
4392			goto end;
4393		}
4394
4395		firstkey = 1;
4396		for (i = 0; i < num; i++) {
4397			struct ieee80211req_key ik;
4398
4399			memset(&ik, 0, sizeof(ik));
4400			ik.ik_keyix = i;
4401			if (get80211(s, IEEE80211_IOC_WPAKEY, &ik, sizeof(ik)) < 0) {
4402				warn("WEP support, but can get keys!");
4403				goto end;
4404			}
4405			if (ik.ik_keylen != 0) {
4406				if (verbose)
4407					LINE_BREAK();
4408				printkey(&ik);
4409				firstkey = 0;
4410			}
4411		}
4412end:
4413		;
4414	}
4415
4416	if (get80211val(s, IEEE80211_IOC_POWERSAVE, &val) != -1 &&
4417	    val != IEEE80211_POWERSAVE_NOSUP ) {
4418		if (val != IEEE80211_POWERSAVE_OFF || verbose) {
4419			switch (val) {
4420			case IEEE80211_POWERSAVE_OFF:
4421				LINE_CHECK("powersavemode OFF");
4422				break;
4423			case IEEE80211_POWERSAVE_CAM:
4424				LINE_CHECK("powersavemode CAM");
4425				break;
4426			case IEEE80211_POWERSAVE_PSP:
4427				LINE_CHECK("powersavemode PSP");
4428				break;
4429			case IEEE80211_POWERSAVE_PSP_CAM:
4430				LINE_CHECK("powersavemode PSP-CAM");
4431				break;
4432			}
4433			if (get80211val(s, IEEE80211_IOC_POWERSAVESLEEP, &val) != -1)
4434				LINE_CHECK("powersavesleep %d", val);
4435		}
4436	}
4437
4438	if (get80211val(s, IEEE80211_IOC_TXPOWER, &val) != -1) {
4439		if (val & 1)
4440			LINE_CHECK("txpower %d.5", val/2);
4441		else
4442			LINE_CHECK("txpower %d", val/2);
4443	}
4444	if (verbose) {
4445		if (get80211val(s, IEEE80211_IOC_TXPOWMAX, &val) != -1)
4446			LINE_CHECK("txpowmax %.1f", val/2.);
4447	}
4448
4449	if (get80211val(s, IEEE80211_IOC_DOTD, &val) != -1) {
4450		if (val)
4451			LINE_CHECK("dotd");
4452		else if (verbose)
4453			LINE_CHECK("-dotd");
4454	}
4455
4456	if (get80211val(s, IEEE80211_IOC_RTSTHRESHOLD, &val) != -1) {
4457		if (val != IEEE80211_RTS_MAX || verbose)
4458			LINE_CHECK("rtsthreshold %d", val);
4459	}
4460
4461	if (get80211val(s, IEEE80211_IOC_FRAGTHRESHOLD, &val) != -1) {
4462		if (val != IEEE80211_FRAG_MAX || verbose)
4463			LINE_CHECK("fragthreshold %d", val);
4464	}
4465	if (opmode == IEEE80211_M_STA || verbose) {
4466		if (get80211val(s, IEEE80211_IOC_BMISSTHRESHOLD, &val) != -1) {
4467			if (val != IEEE80211_HWBMISS_MAX || verbose)
4468				LINE_CHECK("bmiss %d", val);
4469		}
4470	}
4471
4472	if (!verbose) {
4473		gettxparams(s);
4474		tp = &txparams.params[chan2mode(c)];
4475		printrate("ucastrate", tp->ucastrate,
4476		    IEEE80211_FIXED_RATE_NONE, IEEE80211_FIXED_RATE_NONE);
4477		printrate("mcastrate", tp->mcastrate, 2*1,
4478		    IEEE80211_RATE_MCS|0);
4479		printrate("mgmtrate", tp->mgmtrate, 2*1,
4480		    IEEE80211_RATE_MCS|0);
4481		if (tp->maxretry != 6)		/* XXX */
4482			LINE_CHECK("maxretry %d", tp->maxretry);
4483	} else {
4484		LINE_BREAK();
4485		list_txparams(s);
4486	}
4487
4488	bgscaninterval = -1;
4489	(void) get80211val(s, IEEE80211_IOC_BGSCAN_INTERVAL, &bgscaninterval);
4490
4491	if (get80211val(s, IEEE80211_IOC_SCANVALID, &val) != -1) {
4492		if (val != bgscaninterval || verbose)
4493			LINE_CHECK("scanvalid %u", val);
4494	}
4495
4496	bgscan = 0;
4497	if (get80211val(s, IEEE80211_IOC_BGSCAN, &bgscan) != -1) {
4498		if (bgscan)
4499			LINE_CHECK("bgscan");
4500		else if (verbose)
4501			LINE_CHECK("-bgscan");
4502	}
4503	if (bgscan || verbose) {
4504		if (bgscaninterval != -1)
4505			LINE_CHECK("bgscanintvl %u", bgscaninterval);
4506		if (get80211val(s, IEEE80211_IOC_BGSCAN_IDLE, &val) != -1)
4507			LINE_CHECK("bgscanidle %u", val);
4508		if (!verbose) {
4509			getroam(s);
4510			rp = &roamparams.params[chan2mode(c)];
4511			if (rp->rssi & 1)
4512				LINE_CHECK("roam:rssi %u.5", rp->rssi/2);
4513			else
4514				LINE_CHECK("roam:rssi %u", rp->rssi/2);
4515			LINE_CHECK("roam:rate %u", rp->rate/2);
4516		} else {
4517			LINE_BREAK();
4518			list_roam(s);
4519		}
4520	}
4521
4522	if (IEEE80211_IS_CHAN_ANYG(c) || verbose) {
4523		if (get80211val(s, IEEE80211_IOC_PUREG, &val) != -1) {
4524			if (val)
4525				LINE_CHECK("pureg");
4526			else if (verbose)
4527				LINE_CHECK("-pureg");
4528		}
4529		if (get80211val(s, IEEE80211_IOC_PROTMODE, &val) != -1) {
4530			switch (val) {
4531			case IEEE80211_PROTMODE_OFF:
4532				LINE_CHECK("protmode OFF");
4533				break;
4534			case IEEE80211_PROTMODE_CTS:
4535				LINE_CHECK("protmode CTS");
4536				break;
4537			case IEEE80211_PROTMODE_RTSCTS:
4538				LINE_CHECK("protmode RTSCTS");
4539				break;
4540			default:
4541				LINE_CHECK("protmode UNKNOWN (0x%x)", val);
4542				break;
4543			}
4544		}
4545	}
4546
4547	if (IEEE80211_IS_CHAN_HT(c) || verbose) {
4548		gethtconf(s);
4549		switch (htconf & 3) {
4550		case 0:
4551		case 2:
4552			LINE_CHECK("-ht");
4553			break;
4554		case 1:
4555			LINE_CHECK("ht20");
4556			break;
4557		case 3:
4558			if (verbose)
4559				LINE_CHECK("ht");
4560			break;
4561		}
4562		if (get80211val(s, IEEE80211_IOC_HTCOMPAT, &val) != -1) {
4563			if (!val)
4564				LINE_CHECK("-htcompat");
4565			else if (verbose)
4566				LINE_CHECK("htcompat");
4567		}
4568		if (get80211val(s, IEEE80211_IOC_AMPDU, &val) != -1) {
4569			switch (val) {
4570			case 0:
4571				LINE_CHECK("-ampdu");
4572				break;
4573			case 1:
4574				LINE_CHECK("ampdutx -ampdurx");
4575				break;
4576			case 2:
4577				LINE_CHECK("-ampdutx ampdurx");
4578				break;
4579			case 3:
4580				if (verbose)
4581					LINE_CHECK("ampdu");
4582				break;
4583			}
4584		}
4585		if (get80211val(s, IEEE80211_IOC_AMPDU_LIMIT, &val) != -1) {
4586			switch (val) {
4587			case IEEE80211_HTCAP_MAXRXAMPDU_8K:
4588				LINE_CHECK("ampdulimit 8k");
4589				break;
4590			case IEEE80211_HTCAP_MAXRXAMPDU_16K:
4591				LINE_CHECK("ampdulimit 16k");
4592				break;
4593			case IEEE80211_HTCAP_MAXRXAMPDU_32K:
4594				LINE_CHECK("ampdulimit 32k");
4595				break;
4596			case IEEE80211_HTCAP_MAXRXAMPDU_64K:
4597				LINE_CHECK("ampdulimit 64k");
4598				break;
4599			}
4600		}
4601		if (get80211val(s, IEEE80211_IOC_AMPDU_DENSITY, &val) != -1) {
4602			switch (val) {
4603			case IEEE80211_HTCAP_MPDUDENSITY_NA:
4604				if (verbose)
4605					LINE_CHECK("ampdudensity NA");
4606				break;
4607			case IEEE80211_HTCAP_MPDUDENSITY_025:
4608				LINE_CHECK("ampdudensity .25");
4609				break;
4610			case IEEE80211_HTCAP_MPDUDENSITY_05:
4611				LINE_CHECK("ampdudensity .5");
4612				break;
4613			case IEEE80211_HTCAP_MPDUDENSITY_1:
4614				LINE_CHECK("ampdudensity 1");
4615				break;
4616			case IEEE80211_HTCAP_MPDUDENSITY_2:
4617				LINE_CHECK("ampdudensity 2");
4618				break;
4619			case IEEE80211_HTCAP_MPDUDENSITY_4:
4620				LINE_CHECK("ampdudensity 4");
4621				break;
4622			case IEEE80211_HTCAP_MPDUDENSITY_8:
4623				LINE_CHECK("ampdudensity 8");
4624				break;
4625			case IEEE80211_HTCAP_MPDUDENSITY_16:
4626				LINE_CHECK("ampdudensity 16");
4627				break;
4628			}
4629		}
4630		if (get80211val(s, IEEE80211_IOC_AMSDU, &val) != -1) {
4631			switch (val) {
4632			case 0:
4633				LINE_CHECK("-amsdu");
4634				break;
4635			case 1:
4636				LINE_CHECK("amsdutx -amsdurx");
4637				break;
4638			case 2:
4639				LINE_CHECK("-amsdutx amsdurx");
4640				break;
4641			case 3:
4642				if (verbose)
4643					LINE_CHECK("amsdu");
4644				break;
4645			}
4646		}
4647		/* XXX amsdu limit */
4648		if (get80211val(s, IEEE80211_IOC_SHORTGI, &val) != -1) {
4649			if (val)
4650				LINE_CHECK("shortgi");
4651			else if (verbose)
4652				LINE_CHECK("-shortgi");
4653		}
4654		if (get80211val(s, IEEE80211_IOC_HTPROTMODE, &val) != -1) {
4655			if (val == IEEE80211_PROTMODE_OFF)
4656				LINE_CHECK("htprotmode OFF");
4657			else if (val != IEEE80211_PROTMODE_RTSCTS)
4658				LINE_CHECK("htprotmode UNKNOWN (0x%x)", val);
4659			else if (verbose)
4660				LINE_CHECK("htprotmode RTSCTS");
4661		}
4662		if (get80211val(s, IEEE80211_IOC_PUREN, &val) != -1) {
4663			if (val)
4664				LINE_CHECK("puren");
4665			else if (verbose)
4666				LINE_CHECK("-puren");
4667		}
4668		if (get80211val(s, IEEE80211_IOC_SMPS, &val) != -1) {
4669			if (val == IEEE80211_HTCAP_SMPS_DYNAMIC)
4670				LINE_CHECK("smpsdyn");
4671			else if (val == IEEE80211_HTCAP_SMPS_ENA)
4672				LINE_CHECK("smps");
4673			else if (verbose)
4674				LINE_CHECK("-smps");
4675		}
4676		if (get80211val(s, IEEE80211_IOC_RIFS, &val) != -1) {
4677			if (val)
4678				LINE_CHECK("rifs");
4679			else if (verbose)
4680				LINE_CHECK("-rifs");
4681		}
4682	}
4683
4684	if (get80211val(s, IEEE80211_IOC_WME, &wme) != -1) {
4685		if (wme)
4686			LINE_CHECK("wme");
4687		else if (verbose)
4688			LINE_CHECK("-wme");
4689	} else
4690		wme = 0;
4691
4692	if (get80211val(s, IEEE80211_IOC_BURST, &val) != -1) {
4693		if (val)
4694			LINE_CHECK("burst");
4695		else if (verbose)
4696			LINE_CHECK("-burst");
4697	}
4698
4699	if (get80211val(s, IEEE80211_IOC_FF, &val) != -1) {
4700		if (val)
4701			LINE_CHECK("ff");
4702		else if (verbose)
4703			LINE_CHECK("-ff");
4704	}
4705	if (get80211val(s, IEEE80211_IOC_TURBOP, &val) != -1) {
4706		if (val)
4707			LINE_CHECK("dturbo");
4708		else if (verbose)
4709			LINE_CHECK("-dturbo");
4710	}
4711	if (get80211val(s, IEEE80211_IOC_DWDS, &val) != -1) {
4712		if (val)
4713			LINE_CHECK("dwds");
4714		else if (verbose)
4715			LINE_CHECK("-dwds");
4716	}
4717
4718	if (opmode == IEEE80211_M_HOSTAP) {
4719		if (get80211val(s, IEEE80211_IOC_HIDESSID, &val) != -1) {
4720			if (val)
4721				LINE_CHECK("hidessid");
4722			else if (verbose)
4723				LINE_CHECK("-hidessid");
4724		}
4725		if (get80211val(s, IEEE80211_IOC_APBRIDGE, &val) != -1) {
4726			if (!val)
4727				LINE_CHECK("-apbridge");
4728			else if (verbose)
4729				LINE_CHECK("apbridge");
4730		}
4731		if (get80211val(s, IEEE80211_IOC_DTIM_PERIOD, &val) != -1)
4732			LINE_CHECK("dtimperiod %u", val);
4733
4734		if (get80211val(s, IEEE80211_IOC_DOTH, &val) != -1) {
4735			if (!val)
4736				LINE_CHECK("-doth");
4737			else if (verbose)
4738				LINE_CHECK("doth");
4739		}
4740		if (get80211val(s, IEEE80211_IOC_DFS, &val) != -1) {
4741			if (!val)
4742				LINE_CHECK("-dfs");
4743			else if (verbose)
4744				LINE_CHECK("dfs");
4745		}
4746		if (get80211val(s, IEEE80211_IOC_INACTIVITY, &val) != -1) {
4747			if (!val)
4748				LINE_CHECK("-inact");
4749			else if (verbose)
4750				LINE_CHECK("inact");
4751		}
4752	} else {
4753		if (get80211val(s, IEEE80211_IOC_ROAMING, &val) != -1) {
4754			if (val != IEEE80211_ROAMING_AUTO || verbose) {
4755				switch (val) {
4756				case IEEE80211_ROAMING_DEVICE:
4757					LINE_CHECK("roaming DEVICE");
4758					break;
4759				case IEEE80211_ROAMING_AUTO:
4760					LINE_CHECK("roaming AUTO");
4761					break;
4762				case IEEE80211_ROAMING_MANUAL:
4763					LINE_CHECK("roaming MANUAL");
4764					break;
4765				default:
4766					LINE_CHECK("roaming UNKNOWN (0x%x)",
4767						val);
4768					break;
4769				}
4770			}
4771		}
4772	}
4773
4774	if (opmode == IEEE80211_M_AHDEMO) {
4775		if (get80211val(s, IEEE80211_IOC_TDMA_SLOT, &val) != -1)
4776			LINE_CHECK("tdmaslot %u", val);
4777		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTCNT, &val) != -1)
4778			LINE_CHECK("tdmaslotcnt %u", val);
4779		if (get80211val(s, IEEE80211_IOC_TDMA_SLOTLEN, &val) != -1)
4780			LINE_CHECK("tdmaslotlen %u", val);
4781		if (get80211val(s, IEEE80211_IOC_TDMA_BINTERVAL, &val) != -1)
4782			LINE_CHECK("tdmabintval %u", val);
4783	} else if (get80211val(s, IEEE80211_IOC_BEACON_INTERVAL, &val) != -1) {
4784		/* XXX default define not visible */
4785		if (val != 100 || verbose)
4786			LINE_CHECK("bintval %u", val);
4787	}
4788
4789	if (wme && verbose) {
4790		LINE_BREAK();
4791		list_wme(s);
4792	}
4793
4794	if (opmode == IEEE80211_M_MBSS) {
4795		if (get80211val(s, IEEE80211_IOC_MESH_TTL, &val) != -1) {
4796			LINE_CHECK("meshttl %u", val);
4797		}
4798		if (get80211val(s, IEEE80211_IOC_MESH_AP, &val) != -1) {
4799			if (val)
4800				LINE_CHECK("meshpeering");
4801			else
4802				LINE_CHECK("-meshpeering");
4803		}
4804		if (get80211val(s, IEEE80211_IOC_MESH_FWRD, &val) != -1) {
4805			if (val)
4806				LINE_CHECK("meshforward");
4807			else
4808				LINE_CHECK("-meshforward");
4809		}
4810		if (get80211len(s, IEEE80211_IOC_MESH_PR_METRIC, data, 12,
4811		    &len) != -1) {
4812			data[len] = '\0';
4813			LINE_CHECK("meshmetric %s", data);
4814		}
4815		if (get80211len(s, IEEE80211_IOC_MESH_PR_PATH, data, 12,
4816		    &len) != -1) {
4817			data[len] = '\0';
4818			LINE_CHECK("meshpath %s", data);
4819		}
4820		if (get80211val(s, IEEE80211_IOC_HWMP_ROOTMODE, &val) != -1) {
4821			switch (val) {
4822			case IEEE80211_HWMP_ROOTMODE_DISABLED:
4823				LINE_CHECK("hwmprootmode DISABLED");
4824				break;
4825			case IEEE80211_HWMP_ROOTMODE_NORMAL:
4826				LINE_CHECK("hwmprootmode NORMAL");
4827				break;
4828			case IEEE80211_HWMP_ROOTMODE_PROACTIVE:
4829				LINE_CHECK("hwmprootmode PROACTIVE");
4830				break;
4831			case IEEE80211_HWMP_ROOTMODE_RANN:
4832				LINE_CHECK("hwmprootmode RANN");
4833				break;
4834			default:
4835				LINE_CHECK("hwmprootmode UNKNOWN(%d)", val);
4836				break;
4837			}
4838		}
4839		if (get80211val(s, IEEE80211_IOC_HWMP_MAXHOPS, &val) != -1) {
4840			LINE_CHECK("hwmpmaxhops %u", val);
4841		}
4842	}
4843
4844	LINE_BREAK();
4845}
4846
4847static int
4848get80211(int s, int type, void *data, int len)
4849{
4850	struct ieee80211req ireq;
4851
4852	(void) memset(&ireq, 0, sizeof(ireq));
4853	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4854	ireq.i_type = type;
4855	ireq.i_data = data;
4856	ireq.i_len = len;
4857	return ioctl(s, SIOCG80211, &ireq);
4858}
4859
4860static int
4861get80211len(int s, int type, void *data, int len, int *plen)
4862{
4863	struct ieee80211req ireq;
4864
4865	(void) memset(&ireq, 0, sizeof(ireq));
4866	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4867	ireq.i_type = type;
4868	ireq.i_len = len;
4869	assert(ireq.i_len == len);	/* NB: check for 16-bit truncation */
4870	ireq.i_data = data;
4871	if (ioctl(s, SIOCG80211, &ireq) < 0)
4872		return -1;
4873	*plen = ireq.i_len;
4874	return 0;
4875}
4876
4877static int
4878get80211val(int s, int type, int *val)
4879{
4880	struct ieee80211req ireq;
4881
4882	(void) memset(&ireq, 0, sizeof(ireq));
4883	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4884	ireq.i_type = type;
4885	if (ioctl(s, SIOCG80211, &ireq) < 0)
4886		return -1;
4887	*val = ireq.i_val;
4888	return 0;
4889}
4890
4891static void
4892set80211(int s, int type, int val, int len, void *data)
4893{
4894	struct ieee80211req	ireq;
4895
4896	(void) memset(&ireq, 0, sizeof(ireq));
4897	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
4898	ireq.i_type = type;
4899	ireq.i_val = val;
4900	ireq.i_len = len;
4901	assert(ireq.i_len == len);	/* NB: check for 16-bit truncation */
4902	ireq.i_data = data;
4903	if (ioctl(s, SIOCS80211, &ireq) < 0)
4904		err(1, "SIOCS80211");
4905}
4906
4907static const char *
4908get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
4909{
4910	int len;
4911	int hexstr;
4912	u_int8_t *p;
4913
4914	len = *lenp;
4915	p = buf;
4916	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
4917	if (hexstr)
4918		val += 2;
4919	for (;;) {
4920		if (*val == '\0')
4921			break;
4922		if (sep != NULL && strchr(sep, *val) != NULL) {
4923			val++;
4924			break;
4925		}
4926		if (hexstr) {
4927			if (!isxdigit((u_char)val[0])) {
4928				warnx("bad hexadecimal digits");
4929				return NULL;
4930			}
4931			if (!isxdigit((u_char)val[1])) {
4932				warnx("odd count hexadecimal digits");
4933				return NULL;
4934			}
4935		}
4936		if (p >= buf + len) {
4937			if (hexstr)
4938				warnx("hexadecimal digits too long");
4939			else
4940				warnx("string too long");
4941			return NULL;
4942		}
4943		if (hexstr) {
4944#define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
4945			*p++ = (tohex((u_char)val[0]) << 4) |
4946			    tohex((u_char)val[1]);
4947#undef tohex
4948			val += 2;
4949		} else
4950			*p++ = *val++;
4951	}
4952	len = p - buf;
4953	/* The string "-" is treated as the empty string. */
4954	if (!hexstr && len == 1 && buf[0] == '-') {
4955		len = 0;
4956		memset(buf, 0, *lenp);
4957	} else if (len < *lenp)
4958		memset(p, 0, *lenp - len);
4959	*lenp = len;
4960	return val;
4961}
4962
4963static void
4964print_string(const u_int8_t *buf, int len)
4965{
4966	int i;
4967	int hasspc;
4968
4969	i = 0;
4970	hasspc = 0;
4971	for (; i < len; i++) {
4972		if (!isprint(buf[i]) && buf[i] != '\0')
4973			break;
4974		if (isspace(buf[i]))
4975			hasspc++;
4976	}
4977	if (i == len) {
4978		if (hasspc || len == 0 || buf[0] == '\0')
4979			printf("\"%.*s\"", len, buf);
4980		else
4981			printf("%.*s", len, buf);
4982	} else {
4983		printf("0x");
4984		for (i = 0; i < len; i++)
4985			printf("%02x", buf[i]);
4986	}
4987}
4988
4989/*
4990 * Virtual AP cloning support.
4991 */
4992static struct ieee80211_clone_params params = {
4993	.icp_opmode	= IEEE80211_M_STA,	/* default to station mode */
4994};
4995
4996static void
4997wlan_create(int s, struct ifreq *ifr)
4998{
4999	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
5000
5001	if (params.icp_parent[0] == '\0')
5002		errx(1, "must specify a parent device (wlandev) when creating "
5003		    "a wlan device");
5004	if (params.icp_opmode == IEEE80211_M_WDS &&
5005	    memcmp(params.icp_bssid, zerobssid, sizeof(zerobssid)) == 0)
5006		errx(1, "no bssid specified for WDS (use wlanbssid)");
5007	ifr->ifr_data = (caddr_t) &params;
5008	if (ioctl(s, SIOCIFCREATE2, ifr) < 0)
5009		err(1, "SIOCIFCREATE2");
5010}
5011
5012static
5013DECL_CMD_FUNC(set80211clone_wlandev, arg, d)
5014{
5015	strlcpy(params.icp_parent, arg, IFNAMSIZ);
5016}
5017
5018static
5019DECL_CMD_FUNC(set80211clone_wlanbssid, arg, d)
5020{
5021	const struct ether_addr *ea;
5022
5023	ea = ether_aton(arg);
5024	if (ea == NULL)
5025		errx(1, "%s: cannot parse bssid", arg);
5026	memcpy(params.icp_bssid, ea->octet, IEEE80211_ADDR_LEN);
5027}
5028
5029static
5030DECL_CMD_FUNC(set80211clone_wlanaddr, arg, d)
5031{
5032	const struct ether_addr *ea;
5033
5034	ea = ether_aton(arg);
5035	if (ea == NULL)
5036		errx(1, "%s: cannot parse addres", arg);
5037	memcpy(params.icp_macaddr, ea->octet, IEEE80211_ADDR_LEN);
5038	params.icp_flags |= IEEE80211_CLONE_MACADDR;
5039}
5040
5041static
5042DECL_CMD_FUNC(set80211clone_wlanmode, arg, d)
5043{
5044#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
5045	if (iseq(arg, "sta"))
5046		params.icp_opmode = IEEE80211_M_STA;
5047	else if (iseq(arg, "ahdemo") || iseq(arg, "adhoc-demo"))
5048		params.icp_opmode = IEEE80211_M_AHDEMO;
5049	else if (iseq(arg, "ibss") || iseq(arg, "adhoc"))
5050		params.icp_opmode = IEEE80211_M_IBSS;
5051	else if (iseq(arg, "ap") || iseq(arg, "host"))
5052		params.icp_opmode = IEEE80211_M_HOSTAP;
5053	else if (iseq(arg, "wds"))
5054		params.icp_opmode = IEEE80211_M_WDS;
5055	else if (iseq(arg, "monitor"))
5056		params.icp_opmode = IEEE80211_M_MONITOR;
5057	else if (iseq(arg, "tdma")) {
5058		params.icp_opmode = IEEE80211_M_AHDEMO;
5059		params.icp_flags |= IEEE80211_CLONE_TDMA;
5060	} else if (iseq(arg, "mesh") || iseq(arg, "mp")) /* mesh point */
5061		params.icp_opmode = IEEE80211_M_MBSS;
5062	else
5063		errx(1, "Don't know to create %s for %s", arg, name);
5064#undef iseq
5065}
5066
5067static void
5068set80211clone_beacons(const char *val, int d, int s, const struct afswtch *rafp)
5069{
5070	/* NB: inverted sense */
5071	if (d)
5072		params.icp_flags &= ~IEEE80211_CLONE_NOBEACONS;
5073	else
5074		params.icp_flags |= IEEE80211_CLONE_NOBEACONS;
5075}
5076
5077static void
5078set80211clone_bssid(const char *val, int d, int s, const struct afswtch *rafp)
5079{
5080	if (d)
5081		params.icp_flags |= IEEE80211_CLONE_BSSID;
5082	else
5083		params.icp_flags &= ~IEEE80211_CLONE_BSSID;
5084}
5085
5086static void
5087set80211clone_wdslegacy(const char *val, int d, int s, const struct afswtch *rafp)
5088{
5089	if (d)
5090		params.icp_flags |= IEEE80211_CLONE_WDSLEGACY;
5091	else
5092		params.icp_flags &= ~IEEE80211_CLONE_WDSLEGACY;
5093}
5094
5095static struct cmd ieee80211_cmds[] = {
5096	DEF_CMD_ARG("ssid",		set80211ssid),
5097	DEF_CMD_ARG("nwid",		set80211ssid),
5098	DEF_CMD_ARG("meshid",		set80211meshid),
5099	DEF_CMD_ARG("stationname",	set80211stationname),
5100	DEF_CMD_ARG("station",		set80211stationname),	/* BSD/OS */
5101	DEF_CMD_ARG("channel",		set80211channel),
5102	DEF_CMD_ARG("authmode",		set80211authmode),
5103	DEF_CMD_ARG("powersavemode",	set80211powersavemode),
5104	DEF_CMD("powersave",	1,	set80211powersave),
5105	DEF_CMD("-powersave",	0,	set80211powersave),
5106	DEF_CMD_ARG("powersavesleep", 	set80211powersavesleep),
5107	DEF_CMD_ARG("wepmode",		set80211wepmode),
5108	DEF_CMD("wep",		1,	set80211wep),
5109	DEF_CMD("-wep",		0,	set80211wep),
5110	DEF_CMD_ARG("deftxkey",		set80211weptxkey),
5111	DEF_CMD_ARG("weptxkey",		set80211weptxkey),
5112	DEF_CMD_ARG("wepkey",		set80211wepkey),
5113	DEF_CMD_ARG("nwkey",		set80211nwkey),		/* NetBSD */
5114	DEF_CMD("-nwkey",	0,	set80211wep),		/* NetBSD */
5115	DEF_CMD_ARG("rtsthreshold",	set80211rtsthreshold),
5116	DEF_CMD_ARG("protmode",		set80211protmode),
5117	DEF_CMD_ARG("txpower",		set80211txpower),
5118	DEF_CMD_ARG("roaming",		set80211roaming),
5119	DEF_CMD("wme",		1,	set80211wme),
5120	DEF_CMD("-wme",		0,	set80211wme),
5121	DEF_CMD("wmm",		1,	set80211wme),
5122	DEF_CMD("-wmm",		0,	set80211wme),
5123	DEF_CMD("hidessid",	1,	set80211hidessid),
5124	DEF_CMD("-hidessid",	0,	set80211hidessid),
5125	DEF_CMD("apbridge",	1,	set80211apbridge),
5126	DEF_CMD("-apbridge",	0,	set80211apbridge),
5127	DEF_CMD_ARG("chanlist",		set80211chanlist),
5128	DEF_CMD_ARG("bssid",		set80211bssid),
5129	DEF_CMD_ARG("ap",		set80211bssid),
5130	DEF_CMD("scan",	0,		set80211scan),
5131	DEF_CMD_ARG("list",		set80211list),
5132	DEF_CMD_ARG2("cwmin",		set80211cwmin),
5133	DEF_CMD_ARG2("cwmax",		set80211cwmax),
5134	DEF_CMD_ARG2("aifs",		set80211aifs),
5135	DEF_CMD_ARG2("txoplimit",	set80211txoplimit),
5136	DEF_CMD_ARG("acm",		set80211acm),
5137	DEF_CMD_ARG("-acm",		set80211noacm),
5138	DEF_CMD_ARG("ack",		set80211ackpolicy),
5139	DEF_CMD_ARG("-ack",		set80211noackpolicy),
5140	DEF_CMD_ARG2("bss:cwmin",	set80211bsscwmin),
5141	DEF_CMD_ARG2("bss:cwmax",	set80211bsscwmax),
5142	DEF_CMD_ARG2("bss:aifs",	set80211bssaifs),
5143	DEF_CMD_ARG2("bss:txoplimit",	set80211bsstxoplimit),
5144	DEF_CMD_ARG("dtimperiod",	set80211dtimperiod),
5145	DEF_CMD_ARG("bintval",		set80211bintval),
5146	DEF_CMD("mac:open",	IEEE80211_MACCMD_POLICY_OPEN,	set80211maccmd),
5147	DEF_CMD("mac:allow",	IEEE80211_MACCMD_POLICY_ALLOW,	set80211maccmd),
5148	DEF_CMD("mac:deny",	IEEE80211_MACCMD_POLICY_DENY,	set80211maccmd),
5149	DEF_CMD("mac:radius",	IEEE80211_MACCMD_POLICY_RADIUS,	set80211maccmd),
5150	DEF_CMD("mac:flush",	IEEE80211_MACCMD_FLUSH,		set80211maccmd),
5151	DEF_CMD("mac:detach",	IEEE80211_MACCMD_DETACH,	set80211maccmd),
5152	DEF_CMD_ARG("mac:add",		set80211addmac),
5153	DEF_CMD_ARG("mac:del",		set80211delmac),
5154	DEF_CMD_ARG("mac:kick",		set80211kickmac),
5155	DEF_CMD("pureg",	1,	set80211pureg),
5156	DEF_CMD("-pureg",	0,	set80211pureg),
5157	DEF_CMD("ff",		1,	set80211fastframes),
5158	DEF_CMD("-ff",		0,	set80211fastframes),
5159	DEF_CMD("dturbo",	1,	set80211dturbo),
5160	DEF_CMD("-dturbo",	0,	set80211dturbo),
5161	DEF_CMD("bgscan",	1,	set80211bgscan),
5162	DEF_CMD("-bgscan",	0,	set80211bgscan),
5163	DEF_CMD_ARG("bgscanidle",	set80211bgscanidle),
5164	DEF_CMD_ARG("bgscanintvl",	set80211bgscanintvl),
5165	DEF_CMD_ARG("scanvalid",	set80211scanvalid),
5166	DEF_CMD_ARG("roam:rssi",	set80211roamrssi),
5167	DEF_CMD_ARG("roam:rate",	set80211roamrate),
5168	DEF_CMD_ARG("mcastrate",	set80211mcastrate),
5169	DEF_CMD_ARG("ucastrate",	set80211ucastrate),
5170	DEF_CMD_ARG("mgtrate",		set80211mgtrate),
5171	DEF_CMD_ARG("mgmtrate",		set80211mgtrate),
5172	DEF_CMD_ARG("maxretry",		set80211maxretry),
5173	DEF_CMD_ARG("fragthreshold",	set80211fragthreshold),
5174	DEF_CMD("burst",	1,	set80211burst),
5175	DEF_CMD("-burst",	0,	set80211burst),
5176	DEF_CMD_ARG("bmiss",		set80211bmissthreshold),
5177	DEF_CMD_ARG("bmissthreshold",	set80211bmissthreshold),
5178	DEF_CMD("shortgi",	1,	set80211shortgi),
5179	DEF_CMD("-shortgi",	0,	set80211shortgi),
5180	DEF_CMD("ampdurx",	2,	set80211ampdu),
5181	DEF_CMD("-ampdurx",	-2,	set80211ampdu),
5182	DEF_CMD("ampdutx",	1,	set80211ampdu),
5183	DEF_CMD("-ampdutx",	-1,	set80211ampdu),
5184	DEF_CMD("ampdu",	3,	set80211ampdu),		/* NB: tx+rx */
5185	DEF_CMD("-ampdu",	-3,	set80211ampdu),
5186	DEF_CMD_ARG("ampdulimit",	set80211ampdulimit),
5187	DEF_CMD_ARG("ampdudensity",	set80211ampdudensity),
5188	DEF_CMD("amsdurx",	2,	set80211amsdu),
5189	DEF_CMD("-amsdurx",	-2,	set80211amsdu),
5190	DEF_CMD("amsdutx",	1,	set80211amsdu),
5191	DEF_CMD("-amsdutx",	-1,	set80211amsdu),
5192	DEF_CMD("amsdu",	3,	set80211amsdu),		/* NB: tx+rx */
5193	DEF_CMD("-amsdu",	-3,	set80211amsdu),
5194	DEF_CMD_ARG("amsdulimit",	set80211amsdulimit),
5195	DEF_CMD("puren",	1,	set80211puren),
5196	DEF_CMD("-puren",	0,	set80211puren),
5197	DEF_CMD("doth",		1,	set80211doth),
5198	DEF_CMD("-doth",	0,	set80211doth),
5199	DEF_CMD("dfs",		1,	set80211dfs),
5200	DEF_CMD("-dfs",		0,	set80211dfs),
5201	DEF_CMD("htcompat",	1,	set80211htcompat),
5202	DEF_CMD("-htcompat",	0,	set80211htcompat),
5203	DEF_CMD("dwds",		1,	set80211dwds),
5204	DEF_CMD("-dwds",	0,	set80211dwds),
5205	DEF_CMD("inact",	1,	set80211inact),
5206	DEF_CMD("-inact",	0,	set80211inact),
5207	DEF_CMD("tsn",		1,	set80211tsn),
5208	DEF_CMD("-tsn",		0,	set80211tsn),
5209	DEF_CMD_ARG("regdomain",	set80211regdomain),
5210	DEF_CMD_ARG("country",		set80211country),
5211	DEF_CMD("indoor",	'I',	set80211location),
5212	DEF_CMD("-indoor",	'O',	set80211location),
5213	DEF_CMD("outdoor",	'O',	set80211location),
5214	DEF_CMD("-outdoor",	'I',	set80211location),
5215	DEF_CMD("anywhere",	' ',	set80211location),
5216	DEF_CMD("ecm",		1,	set80211ecm),
5217	DEF_CMD("-ecm",		0,	set80211ecm),
5218	DEF_CMD("dotd",		1,	set80211dotd),
5219	DEF_CMD("-dotd",	0,	set80211dotd),
5220	DEF_CMD_ARG("htprotmode",	set80211htprotmode),
5221	DEF_CMD("ht20",		1,	set80211htconf),
5222	DEF_CMD("-ht20",	0,	set80211htconf),
5223	DEF_CMD("ht40",		3,	set80211htconf),	/* NB: 20+40 */
5224	DEF_CMD("-ht40",	0,	set80211htconf),
5225	DEF_CMD("ht",		3,	set80211htconf),	/* NB: 20+40 */
5226	DEF_CMD("-ht",		0,	set80211htconf),
5227	DEF_CMD("rifs",		1,	set80211rifs),
5228	DEF_CMD("-rifs",	0,	set80211rifs),
5229	DEF_CMD("smps",		IEEE80211_HTCAP_SMPS_ENA,	set80211smps),
5230	DEF_CMD("smpsdyn",	IEEE80211_HTCAP_SMPS_DYNAMIC,	set80211smps),
5231	DEF_CMD("-smps",	IEEE80211_HTCAP_SMPS_OFF,	set80211smps),
5232	/* XXX for testing */
5233	DEF_CMD_ARG("chanswitch",	set80211chanswitch),
5234
5235	DEF_CMD_ARG("tdmaslot",		set80211tdmaslot),
5236	DEF_CMD_ARG("tdmaslotcnt",	set80211tdmaslotcnt),
5237	DEF_CMD_ARG("tdmaslotlen",	set80211tdmaslotlen),
5238	DEF_CMD_ARG("tdmabintval",	set80211tdmabintval),
5239
5240	DEF_CMD_ARG("meshttl",		set80211meshttl),
5241	DEF_CMD("meshforward",	1,	set80211meshforward),
5242	DEF_CMD("-meshforward",	0,	set80211meshforward),
5243	DEF_CMD("meshpeering",	1,	set80211meshpeering),
5244	DEF_CMD("-meshpeering",	0,	set80211meshpeering),
5245	DEF_CMD_ARG("meshmetric",	set80211meshmetric),
5246	DEF_CMD_ARG("meshpath",		set80211meshpath),
5247	DEF_CMD("meshrt:flush",	IEEE80211_MESH_RTCMD_FLUSH,	set80211meshrtcmd),
5248	DEF_CMD_ARG("meshrt:add",	set80211addmeshrt),
5249	DEF_CMD_ARG("meshrt:del",	set80211delmeshrt),
5250	DEF_CMD_ARG("hwmprootmode",	set80211hwmprootmode),
5251	DEF_CMD_ARG("hwmpmaxhops",	set80211hwmpmaxhops),
5252
5253	/* vap cloning support */
5254	DEF_CLONE_CMD_ARG("wlanaddr",	set80211clone_wlanaddr),
5255	DEF_CLONE_CMD_ARG("wlanbssid",	set80211clone_wlanbssid),
5256	DEF_CLONE_CMD_ARG("wlandev",	set80211clone_wlandev),
5257	DEF_CLONE_CMD_ARG("wlanmode",	set80211clone_wlanmode),
5258	DEF_CLONE_CMD("beacons", 1,	set80211clone_beacons),
5259	DEF_CLONE_CMD("-beacons", 0,	set80211clone_beacons),
5260	DEF_CLONE_CMD("bssid",	1,	set80211clone_bssid),
5261	DEF_CLONE_CMD("-bssid",	0,	set80211clone_bssid),
5262	DEF_CLONE_CMD("wdslegacy", 1,	set80211clone_wdslegacy),
5263	DEF_CLONE_CMD("-wdslegacy", 0,	set80211clone_wdslegacy),
5264};
5265static struct afswtch af_ieee80211 = {
5266	.af_name	= "af_ieee80211",
5267	.af_af		= AF_UNSPEC,
5268	.af_other_status = ieee80211_status,
5269};
5270
5271static __constructor void
5272ieee80211_ctor(void)
5273{
5274#define	N(a)	(sizeof(a) / sizeof(a[0]))
5275	int i;
5276
5277	for (i = 0; i < N(ieee80211_cmds);  i++)
5278		cmd_register(&ieee80211_cmds[i]);
5279	af_register(&af_ieee80211);
5280	clone_setdefcallback("wlan", wlan_create);
5281#undef N
5282}
5283