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