xref: /freebsd/sbin/ifconfig/ifieee80211.c (revision 42410c6d682c4e00ce6147f99b51a55f6f3fe075)
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_USEVHT160 0x000000008	/* CONF: Use VHT160 */
137 #define	IEEE80211_FVHT_USEVHT80P80 0x000000010	/* CONF: Use VHT 80+80 */
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 	}
3140 	return "?";
3141 #undef WPA_SEL
3142 }
3143 
3144 static const char *
rsn_keymgmt(const u_int8_t * sel)3145 rsn_keymgmt(const u_int8_t *sel)
3146 {
3147 #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
3148 	u_int32_t w = LE_READ_4(sel);
3149 
3150 	switch (w) {
3151 	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
3152 		return "8021X-UNSPEC";
3153 	case RSN_SEL(RSN_ASE_8021X_PSK):
3154 		return "8021X-PSK";
3155 	case RSN_SEL(RSN_ASE_NONE):
3156 		return "NONE";
3157 	}
3158 	return "?";
3159 #undef RSN_SEL
3160 }
3161 
3162 static void
printrsnie(if_ctx * ctx,const char * tag,const u_int8_t * ie,size_t ielen)3163 printrsnie(if_ctx *ctx, const char *tag, const u_int8_t *ie, size_t ielen)
3164 {
3165 	printf("%s", tag);
3166 	if (ctx->args->verbose) {
3167 		const char *sep;
3168 		int n;
3169 
3170 		ie += 2, ielen -= 2;
3171 
3172 		printf("<v%u", LE_READ_2(ie));
3173 		ie += 2, ielen -= 2;
3174 
3175 		printf(" mc:%s", rsn_cipher(ie));
3176 		ie += 4, ielen -= 4;
3177 
3178 		/* unicast ciphers */
3179 		n = LE_READ_2(ie);
3180 		ie += 2, ielen -= 2;
3181 		sep = " uc:";
3182 		for (; n > 0; n--) {
3183 			printf("%s%s", sep, rsn_cipher(ie));
3184 			ie += 4, ielen -= 4;
3185 			sep = "+";
3186 		}
3187 
3188 		/* key management algorithms */
3189 		n = LE_READ_2(ie);
3190 		ie += 2, ielen -= 2;
3191 		sep = " km:";
3192 		for (; n > 0; n--) {
3193 			printf("%s%s", sep, rsn_keymgmt(ie));
3194 			ie += 4, ielen -= 4;
3195 			sep = "+";
3196 		}
3197 
3198 		if (ielen > 2)		/* optional capabilities */
3199 			printf(", caps 0x%x", LE_READ_2(ie));
3200 		/* XXXPMKID */
3201 		printf(">");
3202 	}
3203 }
3204 
3205 #define BE_READ_2(p)					\
3206 	((u_int16_t)					\
3207 	 ((((const u_int8_t *)(p))[1]      ) |		\
3208 	  (((const u_int8_t *)(p))[0] <<  8)))
3209 
3210 static void
printwpsie(if_ctx * ctx,const char * tag,const u_int8_t * ie)3211 printwpsie(if_ctx *ctx, const char *tag, const u_int8_t *ie)
3212 {
3213 	u_int8_t len = ie[1];
3214 
3215 	printf("%s", tag);
3216 	if (ctx->args->verbose) {
3217 		static const char *dev_pass_id[] = {
3218 			"D",	/* Default (PIN) */
3219 			"U",	/* User-specified */
3220 			"M",	/* Machine-specified */
3221 			"K",	/* Rekey */
3222 			"P",	/* PushButton */
3223 			"R"	/* Registrar-specified */
3224 		};
3225 		int n;
3226 		int f;
3227 
3228 		ie +=6, len -= 4;		/* NB: len is payload only */
3229 
3230 		/* WPS IE in Beacon and Probe Resp frames have different fields */
3231 		printf("<");
3232 		while (len) {
3233 			uint16_t tlv_type = BE_READ_2(ie);
3234 			uint16_t tlv_len  = BE_READ_2(ie + 2);
3235 			uint16_t cfg_mthd;
3236 
3237 			/* some devices broadcast invalid WPS frames */
3238 			if (tlv_len > len) {
3239 				printf("bad frame length tlv_type=0x%02x "
3240 				    "tlv_len=%d len=%d", tlv_type, tlv_len,
3241 				    len);
3242 				break;
3243 			}
3244 
3245 			ie += 4, len -= 4;
3246 
3247 			switch (tlv_type) {
3248 			case IEEE80211_WPS_ATTR_VERSION:
3249 				printf("v:%d.%d", *ie >> 4, *ie & 0xf);
3250 				break;
3251 			case IEEE80211_WPS_ATTR_AP_SETUP_LOCKED:
3252 				printf(" ap_setup:%s", *ie ? "locked" :
3253 				    "unlocked");
3254 				break;
3255 			case IEEE80211_WPS_ATTR_CONFIG_METHODS:
3256 			case IEEE80211_WPS_ATTR_SELECTED_REGISTRAR_CONFIG_METHODS:
3257 				if (tlv_type == IEEE80211_WPS_ATTR_SELECTED_REGISTRAR_CONFIG_METHODS)
3258 					printf(" sel_reg_cfg_mthd:");
3259 				else
3260 					printf(" cfg_mthd:" );
3261 				cfg_mthd = BE_READ_2(ie);
3262 				f = 0;
3263 				for (n = 15; n >= 0; n--) {
3264 					if (f) {
3265 						printf(",");
3266 						f = 0;
3267 					}
3268 					switch (cfg_mthd & (1 << n)) {
3269 					case 0:
3270 						break;
3271 					case IEEE80211_WPS_CONFIG_USBA:
3272 						printf("usba");
3273 						f++;
3274 						break;
3275 					case IEEE80211_WPS_CONFIG_ETHERNET:
3276 						printf("ethernet");
3277 						f++;
3278 						break;
3279 					case IEEE80211_WPS_CONFIG_LABEL:
3280 						printf("label");
3281 						f++;
3282 						break;
3283 					case IEEE80211_WPS_CONFIG_DISPLAY:
3284 						if (!(cfg_mthd &
3285 						    (IEEE80211_WPS_CONFIG_VIRT_DISPLAY |
3286 						    IEEE80211_WPS_CONFIG_PHY_DISPLAY)))
3287 						    {
3288 							printf("display");
3289 							f++;
3290 						}
3291 						break;
3292 					case IEEE80211_WPS_CONFIG_EXT_NFC_TOKEN:
3293 						printf("ext_nfc_tokenk");
3294 						f++;
3295 						break;
3296 					case IEEE80211_WPS_CONFIG_INT_NFC_TOKEN:
3297 						printf("int_nfc_token");
3298 						f++;
3299 						break;
3300 					case IEEE80211_WPS_CONFIG_NFC_INTERFACE:
3301 						printf("nfc_interface");
3302 						f++;
3303 						break;
3304 					case IEEE80211_WPS_CONFIG_PUSHBUTTON:
3305 						if (!(cfg_mthd &
3306 						    (IEEE80211_WPS_CONFIG_VIRT_PUSHBUTTON |
3307 						    IEEE80211_WPS_CONFIG_PHY_PUSHBUTTON))) {
3308 							printf("push_button");
3309 							f++;
3310 						}
3311 						break;
3312 					case IEEE80211_WPS_CONFIG_KEYPAD:
3313 						printf("keypad");
3314 						f++;
3315 						break;
3316 					case IEEE80211_WPS_CONFIG_VIRT_PUSHBUTTON:
3317 						printf("virtual_push_button");
3318 						f++;
3319 						break;
3320 					case IEEE80211_WPS_CONFIG_PHY_PUSHBUTTON:
3321 						printf("physical_push_button");
3322 						f++;
3323 						break;
3324 					case IEEE80211_WPS_CONFIG_P2PS:
3325 						printf("p2ps");
3326 						f++;
3327 						break;
3328 					case IEEE80211_WPS_CONFIG_VIRT_DISPLAY:
3329 						printf("virtual_display");
3330 						f++;
3331 						break;
3332 					case IEEE80211_WPS_CONFIG_PHY_DISPLAY:
3333 						printf("physical_display");
3334 						f++;
3335 						break;
3336 					default:
3337 						printf("unknown_wps_config<%04x>",
3338 						    cfg_mthd & (1 << n));
3339 						f++;
3340 						break;
3341 					}
3342 				}
3343 				break;
3344 			case IEEE80211_WPS_ATTR_DEV_NAME:
3345 				printf(" device_name:<%.*s>", tlv_len, ie);
3346 				break;
3347 			case IEEE80211_WPS_ATTR_DEV_PASSWORD_ID:
3348 				n = LE_READ_2(ie);
3349 				if (n < (int)nitems(dev_pass_id))
3350 					printf(" dpi:%s", dev_pass_id[n]);
3351 				break;
3352 			case IEEE80211_WPS_ATTR_MANUFACTURER:
3353 				printf(" manufacturer:<%.*s>", tlv_len, ie);
3354 				break;
3355 			case IEEE80211_WPS_ATTR_MODEL_NAME:
3356 				printf(" model_name:<%.*s>", tlv_len, ie);
3357 				break;
3358 			case IEEE80211_WPS_ATTR_MODEL_NUMBER:
3359 				printf(" model_number:<%.*s>", tlv_len, ie);
3360 				break;
3361 			case IEEE80211_WPS_ATTR_PRIMARY_DEV_TYPE:
3362 				printf(" prim_dev:");
3363 				for (n = 0; n < tlv_len; n++)
3364 					printf("%02x", ie[n]);
3365 				break;
3366 			case IEEE80211_WPS_ATTR_RF_BANDS:
3367 				printf(" rf:");
3368 				f = 0;
3369 				for (n = 7; n >= 0; n--) {
3370 					if (f) {
3371 						printf(",");
3372 						f = 0;
3373 					}
3374 					switch (*ie & (1 << n)) {
3375 					case 0:
3376 						break;
3377 					case IEEE80211_WPS_RF_BAND_24GHZ:
3378 						printf("2.4Ghz");
3379 						f++;
3380 						break;
3381 					case IEEE80211_WPS_RF_BAND_50GHZ:
3382 						printf("5Ghz");
3383 						f++;
3384 						break;
3385 					case IEEE80211_WPS_RF_BAND_600GHZ:
3386 						printf("60Ghz");
3387 						f++;
3388 						break;
3389 					default:
3390 						printf("unknown<%02x>",
3391 						    *ie & (1 << n));
3392 						f++;
3393 						break;
3394 					}
3395 				}
3396 				break;
3397 			case IEEE80211_WPS_ATTR_RESPONSE_TYPE:
3398 				printf(" resp_type:0x%02x", *ie);
3399 				break;
3400 			case IEEE80211_WPS_ATTR_SELECTED_REGISTRAR:
3401 				printf(" sel:%s", *ie ? "T" : "F");
3402 				break;
3403 			case IEEE80211_WPS_ATTR_SERIAL_NUMBER:
3404 				printf(" serial_number:<%.*s>", tlv_len, ie);
3405 				break;
3406 			case IEEE80211_WPS_ATTR_UUID_E:
3407 				printf(" uuid-e:");
3408 				for (n = 0; n < (tlv_len - 1); n++)
3409 					printf("%02x-", ie[n]);
3410 				printf("%02x", ie[n]);
3411 				break;
3412 			case IEEE80211_WPS_ATTR_VENDOR_EXT:
3413 				printf(" vendor:");
3414 				for (n = 0; n < tlv_len; n++)
3415 					printf("%02x", ie[n]);
3416 				break;
3417 			case IEEE80211_WPS_ATTR_WPS_STATE:
3418 				switch (*ie) {
3419 				case IEEE80211_WPS_STATE_NOT_CONFIGURED:
3420 					printf(" state:N");
3421 					break;
3422 				case IEEE80211_WPS_STATE_CONFIGURED:
3423 					printf(" state:C");
3424 					break;
3425 				default:
3426 					printf(" state:B<%02x>", *ie);
3427 					break;
3428 				}
3429 				break;
3430 			default:
3431 				printf(" unknown_wps_attr:0x%x", tlv_type);
3432 				break;
3433 			}
3434 			ie += tlv_len, len -= tlv_len;
3435 		}
3436 		printf(">");
3437 	}
3438 }
3439 
3440 static void
printtdmaie(if_ctx * ctx,const char * tag,const u_int8_t * ie,size_t ielen)3441 printtdmaie(if_ctx *ctx, const char *tag, const u_int8_t *ie, size_t ielen)
3442 {
3443 	printf("%s", tag);
3444 	if (ctx->args->verbose && ielen >= sizeof(struct ieee80211_tdma_param)) {
3445 		const struct ieee80211_tdma_param *tdma =
3446 		   (const struct ieee80211_tdma_param *) ie;
3447 
3448 		/* XXX tstamp */
3449 		printf("<v%u slot:%u slotcnt:%u slotlen:%u bintval:%u inuse:0x%x>",
3450 		    tdma->tdma_version, tdma->tdma_slot, tdma->tdma_slotcnt,
3451 		    LE_READ_2(&tdma->tdma_slotlen), tdma->tdma_bintval,
3452 		    tdma->tdma_inuse[0]);
3453 	}
3454 }
3455 
3456 /*
3457  * Copy the ssid string contents into buf, truncating to fit.  If the
3458  * ssid is entirely printable then just copy intact.  Otherwise convert
3459  * to hexadecimal.  If the result is truncated then replace the last
3460  * three characters with "...".
3461  */
3462 static int
copy_essid(char buf[],size_t bufsize,const u_int8_t * essid,size_t essid_len)3463 copy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
3464 {
3465 	const u_int8_t *p;
3466 	size_t maxlen;
3467 	u_int i;
3468 
3469 	if (essid_len > bufsize)
3470 		maxlen = bufsize;
3471 	else
3472 		maxlen = essid_len;
3473 	/* determine printable or not */
3474 	for (i = 0, p = essid; i < maxlen; i++, p++) {
3475 		if (*p < ' ' || *p > 0x7e)
3476 			break;
3477 	}
3478 	if (i != maxlen) {		/* not printable, print as hex */
3479 		if (bufsize < 3)
3480 			return 0;
3481 		strlcpy(buf, "0x", bufsize);
3482 		bufsize -= 2;
3483 		p = essid;
3484 		for (i = 0; i < maxlen && bufsize >= 2; i++) {
3485 			sprintf(&buf[2+2*i], "%02x", p[i]);
3486 			bufsize -= 2;
3487 		}
3488 		if (i != essid_len)
3489 			memcpy(&buf[2+2*i-3], "...", 3);
3490 	} else {			/* printable, truncate as needed */
3491 		memcpy(buf, essid, maxlen);
3492 		if (maxlen != essid_len)
3493 			memcpy(&buf[maxlen-3], "...", 3);
3494 	}
3495 	return maxlen;
3496 }
3497 
3498 static void
printssid(const char * tag,const u_int8_t * ie,int maxlen)3499 printssid(const char *tag, const u_int8_t *ie, int maxlen)
3500 {
3501 	char ssid[2*IEEE80211_NWID_LEN+1];
3502 
3503 	printf("%s<%.*s>", tag, copy_essid(ssid, maxlen, ie+2, ie[1]), ssid);
3504 }
3505 
3506 static void
printrates(const char * tag,const u_int8_t * ie,size_t ielen)3507 printrates(const char *tag, const u_int8_t *ie, size_t ielen)
3508 {
3509 	const char *sep;
3510 
3511 	printf("%s", tag);
3512 	sep = "<";
3513 	for (size_t i = 2; i < ielen; i++) {
3514 		printf("%s%s%d", sep,
3515 		    ie[i] & IEEE80211_RATE_BASIC ? "B" : "",
3516 		    ie[i] & IEEE80211_RATE_VAL);
3517 		sep = ",";
3518 	}
3519 	printf(">");
3520 }
3521 
3522 static void
printcountry(const char * tag,const u_int8_t * ie)3523 printcountry(const char *tag, const u_int8_t *ie)
3524 {
3525 	const struct ieee80211_country_ie *cie =
3526 	   (const struct ieee80211_country_ie *) ie;
3527 	int i, nbands, schan, nchan;
3528 
3529 	printf("%s<%c%c%c", tag, cie->cc[0], cie->cc[1], cie->cc[2]);
3530 	nbands = (cie->len - 3) / sizeof(cie->band[0]);
3531 	for (i = 0; i < nbands; i++) {
3532 		schan = cie->band[i].schan;
3533 		nchan = cie->band[i].nchan;
3534 		if (nchan != 1)
3535 			printf(" %u-%u,%u", schan, schan + nchan-1,
3536 			    cie->band[i].maxtxpwr);
3537 		else
3538 			printf(" %u,%u", schan, cie->band[i].maxtxpwr);
3539 	}
3540 	printf(">");
3541 }
3542 
3543 static __inline int
iswpaoui(const u_int8_t * frm)3544 iswpaoui(const u_int8_t *frm)
3545 {
3546 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
3547 }
3548 
3549 static __inline int
iswmeinfo(const u_int8_t * frm)3550 iswmeinfo(const u_int8_t *frm)
3551 {
3552 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3553 		frm[6] == WME_INFO_OUI_SUBTYPE;
3554 }
3555 
3556 static __inline int
iswmeparam(const u_int8_t * frm)3557 iswmeparam(const u_int8_t *frm)
3558 {
3559 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
3560 		frm[6] == WME_PARAM_OUI_SUBTYPE;
3561 }
3562 
3563 static __inline int
isatherosoui(const u_int8_t * frm)3564 isatherosoui(const u_int8_t *frm)
3565 {
3566 	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
3567 }
3568 
3569 static __inline int
istdmaoui(const uint8_t * frm)3570 istdmaoui(const uint8_t *frm)
3571 {
3572 	return frm[1] > 3 && LE_READ_4(frm+2) == ((TDMA_OUI_TYPE<<24)|TDMA_OUI);
3573 }
3574 
3575 static __inline int
iswpsoui(const uint8_t * frm)3576 iswpsoui(const uint8_t *frm)
3577 {
3578 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPS_OUI_TYPE<<24)|WPA_OUI);
3579 }
3580 
3581 static const char *
iename(int elemid)3582 iename(int elemid)
3583 {
3584 	static char iename_buf[64];
3585 	switch (elemid) {
3586 	case IEEE80211_ELEMID_FHPARMS:	return " FHPARMS";
3587 	case IEEE80211_ELEMID_CFPARMS:	return " CFPARMS";
3588 	case IEEE80211_ELEMID_TIM:	return " TIM";
3589 	case IEEE80211_ELEMID_IBSSPARMS:return " IBSSPARMS";
3590 	case IEEE80211_ELEMID_BSSLOAD:	return " BSSLOAD";
3591 	case IEEE80211_ELEMID_CHALLENGE:return " CHALLENGE";
3592 	case IEEE80211_ELEMID_PWRCNSTR:	return " PWRCNSTR";
3593 	case IEEE80211_ELEMID_PWRCAP:	return " PWRCAP";
3594 	case IEEE80211_ELEMID_TPCREQ:	return " TPCREQ";
3595 	case IEEE80211_ELEMID_TPCREP:	return " TPCREP";
3596 	case IEEE80211_ELEMID_SUPPCHAN:	return " SUPPCHAN";
3597 	case IEEE80211_ELEMID_CSA:	return " CSA";
3598 	case IEEE80211_ELEMID_MEASREQ:	return " MEASREQ";
3599 	case IEEE80211_ELEMID_MEASREP:	return " MEASREP";
3600 	case IEEE80211_ELEMID_QUIET:	return " QUIET";
3601 	case IEEE80211_ELEMID_IBSSDFS:	return " IBSSDFS";
3602 	case IEEE80211_ELEMID_RESERVED_47:
3603 					return " RESERVED_47";
3604 	case IEEE80211_ELEMID_MOBILITY_DOMAIN:
3605 					return " MOBILITY_DOMAIN";
3606 	case IEEE80211_ELEMID_RRM_ENACAPS:
3607 					return " RRM_ENCAPS";
3608 	case IEEE80211_ELEMID_OVERLAP_BSS_SCAN_PARAM:
3609 					return " OVERLAP_BSS";
3610 	case IEEE80211_ELEMID_TPC:	return " TPC";
3611 	case IEEE80211_ELEMID_CCKM:	return " CCKM";
3612 	case IEEE80211_ELEMID_EXTCAP:	return " EXTCAP";
3613 	}
3614 	snprintf(iename_buf, sizeof(iename_buf), " UNKNOWN_ELEMID_%d",
3615 	    elemid);
3616 	return (const char *) iename_buf;
3617 }
3618 
3619 static void
printies(if_ctx * ctx,const u_int8_t * vp,int ielen,unsigned int maxcols)3620 printies(if_ctx *ctx, const u_int8_t *vp, int ielen, unsigned int maxcols)
3621 {
3622 	const int verbose = ctx->args->verbose;
3623 
3624 	while (ielen > 0) {
3625 		switch (vp[0]) {
3626 		case IEEE80211_ELEMID_SSID:
3627 			if (verbose)
3628 				printssid(" SSID", vp, maxcols);
3629 			break;
3630 		case IEEE80211_ELEMID_RATES:
3631 		case IEEE80211_ELEMID_XRATES:
3632 			if (verbose)
3633 				printrates(vp[0] == IEEE80211_ELEMID_RATES ?
3634 				    " RATES" : " XRATES", vp, 2+vp[1]);
3635 			break;
3636 		case IEEE80211_ELEMID_DSPARMS:
3637 			if (verbose)
3638 				printf(" DSPARMS<%u>", vp[2]);
3639 			break;
3640 		case IEEE80211_ELEMID_COUNTRY:
3641 			if (verbose)
3642 				printcountry(" COUNTRY", vp);
3643 			break;
3644 		case IEEE80211_ELEMID_ERP:
3645 			if (verbose)
3646 				printf(" ERP<0x%x>", vp[2]);
3647 			break;
3648 		case IEEE80211_ELEMID_VENDOR:
3649 			if (iswpaoui(vp))
3650 				printwpaie(ctx, " WPA", vp);
3651 			else if (iswmeinfo(vp))
3652 				printwmeinfo(ctx, " WME", vp);
3653 			else if (iswmeparam(vp))
3654 				printwmeparam(ctx, " WME", vp);
3655 			else if (isatherosoui(vp))
3656 				printathie(ctx, " ATH", vp);
3657 			else if (iswpsoui(vp))
3658 				printwpsie(ctx, " WPS", vp);
3659 			else if (istdmaoui(vp))
3660 				printtdmaie(ctx, " TDMA", vp, 2+vp[1]);
3661 			else if (verbose)
3662 					printie(ctx, " VEN", vp, 2+vp[1], maxcols);
3663 				break;
3664 		case IEEE80211_ELEMID_RSN:
3665 			printrsnie(ctx, " RSN", vp, 2+vp[1]);
3666 			break;
3667 		case IEEE80211_ELEMID_HTCAP:
3668 			printhtcap(ctx, " HTCAP", vp);
3669 			break;
3670 		case IEEE80211_ELEMID_HTINFO:
3671 			if (verbose)
3672 				printhtinfo(ctx, " HTINFO", vp);
3673 			break;
3674 		case IEEE80211_ELEMID_MESHID:
3675 			if (verbose)
3676 				printssid(" MESHID", vp, maxcols);
3677 			break;
3678 		case IEEE80211_ELEMID_MESHCONF:
3679 			printmeshconf(ctx, " MESHCONF", vp);
3680 			break;
3681 		case IEEE80211_ELEMID_VHT_CAP:
3682 			printvhtcap(ctx, " VHTCAP", vp);
3683 			break;
3684 		case IEEE80211_ELEMID_VHT_OPMODE:
3685 			printvhtinfo(ctx, " VHTOPMODE", vp);
3686 			break;
3687 		case IEEE80211_ELEMID_VHT_PWR_ENV:
3688 			printvhtpwrenv(ctx, " VHTPWRENV", vp, 2+vp[1]);
3689 			break;
3690 		case IEEE80211_ELEMID_BSSLOAD:
3691 			printbssload(ctx, " BSSLOAD", vp);
3692 			break;
3693 		case IEEE80211_ELEMID_APCHANREP:
3694 			printapchanrep(ctx, " APCHANREP", vp, 2+vp[1]);
3695 			break;
3696 		default:
3697 			if (verbose)
3698 				printie(ctx, iename(vp[0]), vp, 2+vp[1], maxcols);
3699 			break;
3700 		}
3701 		ielen -= 2+vp[1];
3702 		vp += 2+vp[1];
3703 	}
3704 }
3705 
3706 static void
printmimo(const struct ieee80211_mimo_info * mi)3707 printmimo(const struct ieee80211_mimo_info *mi)
3708 {
3709 	int i;
3710 	int r = 0;
3711 
3712 	for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3713 		if (mi->ch[i].rssi[0] != 0) {
3714 			r = 1;
3715 			break;
3716 		}
3717 	}
3718 
3719 	/* NB: don't muddy display unless there's something to show */
3720 	if (r == 0)
3721 		return;
3722 
3723 	/* XXX TODO: ignore EVM; secondary channels for now */
3724 	printf(" (rssi %.1f:%.1f:%.1f:%.1f nf %d:%d:%d:%d)",
3725 	    mi->ch[0].rssi[0] / 2.0,
3726 	    mi->ch[1].rssi[0] / 2.0,
3727 	    mi->ch[2].rssi[0] / 2.0,
3728 	    mi->ch[3].rssi[0] / 2.0,
3729 	    mi->ch[0].noise[0],
3730 	    mi->ch[1].noise[0],
3731 	    mi->ch[2].noise[0],
3732 	    mi->ch[3].noise[0]);
3733 }
3734 
3735 static void
printbssidname(const struct ether_addr * n)3736 printbssidname(const struct ether_addr *n)
3737 {
3738 	char name[MAXHOSTNAMELEN + 1];
3739 
3740 	if (ether_ntohost(name, n) != 0)
3741 		return;
3742 
3743 	printf(" (%s)", name);
3744 }
3745 
3746 static void
list_scan(if_ctx * ctx)3747 list_scan(if_ctx *ctx)
3748 {
3749 	uint8_t buf[24*1024];
3750 	char ssid[IEEE80211_NWID_LEN+1];
3751 	const uint8_t *cp;
3752 	int len, idlen;
3753 
3754 	if (get80211len(ctx, IEEE80211_IOC_SCAN_RESULTS, buf, sizeof(buf), &len) < 0)
3755 		errx(1, "unable to get scan results");
3756 	if (len < (int)sizeof(struct ieee80211req_scan_result))
3757 		return;
3758 
3759 	getchaninfo(ctx);
3760 
3761 	printf("%-*.*s  %-17.17s  %4s %4s   %-7s  %3s %4s\n"
3762 		, IEEE80211_NWID_LEN, IEEE80211_NWID_LEN, "SSID/MESH ID"
3763 		, "BSSID"
3764 		, "CHAN"
3765 		, "RATE"
3766 		, " S:N"
3767 		, "INT"
3768 		, "CAPS"
3769 	);
3770 	cp = buf;
3771 	do {
3772 		const struct ieee80211req_scan_result *sr;
3773 		const uint8_t *vp, *idp;
3774 
3775 		sr = (const struct ieee80211req_scan_result *)(const void *) cp;
3776 		vp = cp + sr->isr_ie_off;
3777 		if (sr->isr_meshid_len) {
3778 			idp = vp + sr->isr_ssid_len;
3779 			idlen = sr->isr_meshid_len;
3780 		} else {
3781 			idp = vp;
3782 			idlen = sr->isr_ssid_len;
3783 		}
3784 		printf("%-*.*s  %s  %3d  %3dM %4d:%-4d %4d %-4.4s"
3785 			, IEEE80211_NWID_LEN
3786 			  , copy_essid(ssid, IEEE80211_NWID_LEN, idp, idlen)
3787 			  , ssid
3788 			, ether_ntoa((const struct ether_addr *) sr->isr_bssid)
3789 			, ieee80211_mhz2ieee(sr->isr_freq, sr->isr_flags)
3790 			, getmaxrate(sr->isr_rates, sr->isr_nrates)
3791 			, (sr->isr_rssi/2)+sr->isr_noise, sr->isr_noise
3792 			, sr->isr_intval
3793 			, getcaps(sr->isr_capinfo)
3794 		);
3795 		printies(ctx, vp + sr->isr_ssid_len + sr->isr_meshid_len,
3796 		    sr->isr_ie_len, 24);
3797 		printbssidname((const struct ether_addr *)sr->isr_bssid);
3798 		printf("\n");
3799 		cp += sr->isr_len, len -= sr->isr_len;
3800 	} while (len >= (int)sizeof(struct ieee80211req_scan_result));
3801 }
3802 
3803 static void
scan_and_wait(if_ctx * ctx)3804 scan_and_wait(if_ctx *ctx)
3805 {
3806 	struct ieee80211_scan_req sr;
3807 	struct ieee80211req ireq;
3808 	int sroute;
3809 
3810 	sroute = socket(PF_ROUTE, SOCK_RAW, 0);
3811 	if (sroute < 0) {
3812 		perror("socket(PF_ROUTE,SOCK_RAW)");
3813 		return;
3814 	}
3815 	memset(&ireq, 0, sizeof(ireq));
3816 	strlcpy(ireq.i_name, ctx->ifname, sizeof(ireq.i_name));
3817 	ireq.i_type = IEEE80211_IOC_SCAN_REQ;
3818 
3819 	memset(&sr, 0, sizeof(sr));
3820 	sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE
3821 		    | IEEE80211_IOC_SCAN_BGSCAN
3822 		    | IEEE80211_IOC_SCAN_NOPICK
3823 		    | IEEE80211_IOC_SCAN_ONCE;
3824 	sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
3825 	sr.sr_nssid = 0;
3826 
3827 	ireq.i_data = &sr;
3828 	ireq.i_len = sizeof(sr);
3829 	/*
3830 	 * NB: only root can trigger a scan so ignore errors. Also ignore
3831 	 * possible errors from net80211, even if no new scan could be
3832 	 * started there might still be a valid scan cache.
3833 	 */
3834 	if (ioctl_ctx(ctx, SIOCS80211, &ireq) == 0) {
3835 		char buf[2048];
3836 		struct if_announcemsghdr *ifan;
3837 		struct rt_msghdr *rtm;
3838 
3839 		do {
3840 			if (read(sroute, buf, sizeof(buf)) < 0) {
3841 				perror("read(PF_ROUTE)");
3842 				break;
3843 			}
3844 			rtm = (struct rt_msghdr *)(void *)buf;
3845 			if (rtm->rtm_version != RTM_VERSION)
3846 				break;
3847 			ifan = (struct if_announcemsghdr *) rtm;
3848 		} while (rtm->rtm_type != RTM_IEEE80211 ||
3849 		    ifan->ifan_what != RTM_IEEE80211_SCAN);
3850 	}
3851 	close(sroute);
3852 }
3853 
3854 static void
set80211scan(if_ctx * ctx,const char * val __unused,int dummy __unused)3855 set80211scan(if_ctx *ctx, const char *val __unused, int dummy __unused)
3856 {
3857 	scan_and_wait(ctx);
3858 	list_scan(ctx);
3859 }
3860 
3861 static enum ieee80211_opmode get80211opmode(if_ctx *ctx);
3862 
3863 static int
gettxseq(const struct ieee80211req_sta_info * si)3864 gettxseq(const struct ieee80211req_sta_info *si)
3865 {
3866 	int i, txseq;
3867 
3868 	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3869 		return si->isi_txseqs[0];
3870 	/* XXX not right but usually what folks want */
3871 	txseq = 0;
3872 	for (i = 0; i < IEEE80211_TID_SIZE; i++)
3873 		if (si->isi_txseqs[i] > txseq)
3874 			txseq = si->isi_txseqs[i];
3875 	return txseq;
3876 }
3877 
3878 static int
getrxseq(const struct ieee80211req_sta_info * si)3879 getrxseq(const struct ieee80211req_sta_info *si)
3880 {
3881 	int rxseq;
3882 
3883 	if ((si->isi_state & IEEE80211_NODE_QOS) == 0)
3884 		return si->isi_rxseqs[0];
3885 	/* XXX not right but usually what folks want */
3886 	rxseq = 0;
3887 	for (unsigned int i = 0; i < IEEE80211_TID_SIZE; i++)
3888 		if (si->isi_rxseqs[i] > rxseq)
3889 			rxseq = si->isi_rxseqs[i];
3890 	return rxseq;
3891 }
3892 
3893 static void
list_stations(if_ctx * ctx)3894 list_stations(if_ctx *ctx)
3895 {
3896 	union {
3897 		struct ieee80211req_sta_req req;
3898 		uint8_t buf[24*1024];
3899 	} u;
3900 	enum ieee80211_opmode opmode = get80211opmode(ctx);
3901 	const uint8_t *cp;
3902 	int len;
3903 
3904 	/* broadcast address =>'s get all stations */
3905 	(void) memset(u.req.is_u.macaddr, 0xff, IEEE80211_ADDR_LEN);
3906 	if (opmode == IEEE80211_M_STA) {
3907 		/*
3908 		 * Get information about the associated AP.
3909 		 */
3910 		(void) get80211(ctx, IEEE80211_IOC_BSSID,
3911 		    u.req.is_u.macaddr, IEEE80211_ADDR_LEN);
3912 	}
3913 	if (get80211len(ctx, IEEE80211_IOC_STA_INFO, &u, sizeof(u), &len) < 0)
3914 		errx(1, "unable to get station information");
3915 	if (len < (int)sizeof(struct ieee80211req_sta_info))
3916 		return;
3917 
3918 	getchaninfo(ctx);
3919 
3920 	if (opmode == IEEE80211_M_MBSS)
3921 		printf("%-17.17s %4s %5s %5s %7s %4s %4s %4s %6s %6s\n"
3922 			, "ADDR"
3923 			, "CHAN"
3924 			, "LOCAL"
3925 			, "PEER"
3926 			, "STATE"
3927 			, "RATE"
3928 			, "RSSI"
3929 			, "IDLE"
3930 			, "TXSEQ"
3931 			, "RXSEQ"
3932 		);
3933 	else
3934 		printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %-12s\n"
3935 			, "ADDR"
3936 			, "AID"
3937 			, "CHAN"
3938 			, "RATE"
3939 			, "RSSI"
3940 			, "IDLE"
3941 			, "TXSEQ"
3942 			, "RXSEQ"
3943 			, "CAPS"
3944 			, "FLAG"
3945 		);
3946 	cp = (const uint8_t *) u.req.info;
3947 	do {
3948 		const struct ieee80211req_sta_info *si;
3949 
3950 		si = (const struct ieee80211req_sta_info *)(const void *)cp;
3951 		if (si->isi_len < sizeof(*si))
3952 			break;
3953 		if (opmode == IEEE80211_M_MBSS)
3954 			printf("%s %4d %5x %5x %7.7s %3dM %4.1f %4d %6d %6d"
3955 				, ether_ntoa((const struct ether_addr*)
3956 				    si->isi_macaddr)
3957 				, ieee80211_mhz2ieee(si->isi_freq,
3958 				    si->isi_flags)
3959 				, si->isi_localid
3960 				, si->isi_peerid
3961 				, mesh_linkstate_string(si->isi_peerstate)
3962 				, si->isi_txmbps/2
3963 				, si->isi_rssi/2.
3964 				, si->isi_inact
3965 				, gettxseq(si)
3966 				, getrxseq(si)
3967 			);
3968 		else
3969 			printf("%s %4u %4d %3dM %4.1f %4d %6d %6d %-4.4s %-12.12s"
3970 				, ether_ntoa((const struct ether_addr*)
3971 				    si->isi_macaddr)
3972 				, IEEE80211_AID(si->isi_associd)
3973 				, ieee80211_mhz2ieee(si->isi_freq,
3974 				    si->isi_flags)
3975 				, si->isi_txmbps/2
3976 				, si->isi_rssi/2.
3977 				, si->isi_inact
3978 				, gettxseq(si)
3979 				, getrxseq(si)
3980 				, getcaps(si->isi_capinfo)
3981 				, getflags(si->isi_state)
3982 			);
3983 		printies(ctx, cp + si->isi_ie_off, si->isi_ie_len, 24);
3984 		printmimo(&si->isi_mimo);
3985 		printf("\n");
3986 		cp += si->isi_len, len -= si->isi_len;
3987 	} while (len >= (int)sizeof(struct ieee80211req_sta_info));
3988 }
3989 
3990 static const char *
mesh_linkstate_string(uint8_t state)3991 mesh_linkstate_string(uint8_t state)
3992 {
3993 	static const char *state_names[] = {
3994 	    [0] = "IDLE",
3995 	    [1] = "OPEN-TX",
3996 	    [2] = "OPEN-RX",
3997 	    [3] = "CONF-RX",
3998 	    [4] = "ESTAB",
3999 	    [5] = "HOLDING",
4000 	};
4001 
4002 	if (state >= nitems(state_names)) {
4003 		static char buf[10];
4004 		snprintf(buf, sizeof(buf), "#%u", state);
4005 		return buf;
4006 	} else
4007 		return state_names[state];
4008 }
4009 
4010 static const char *
get_chaninfo(const struct ieee80211_channel * c,int precise,char buf[],size_t bsize)4011 get_chaninfo(const struct ieee80211_channel *c, int precise,
4012 	char buf[], size_t bsize)
4013 {
4014 	buf[0] = '\0';
4015 	if (IEEE80211_IS_CHAN_FHSS(c))
4016 		strlcat(buf, " FHSS", bsize);
4017 	if (IEEE80211_IS_CHAN_A(c))
4018 		strlcat(buf, " 11a", bsize);
4019 	else if (IEEE80211_IS_CHAN_ANYG(c))
4020 		strlcat(buf, " 11g", bsize);
4021 	else if (IEEE80211_IS_CHAN_B(c))
4022 		strlcat(buf, " 11b", bsize);
4023 	if (IEEE80211_IS_CHAN_HALF(c))
4024 		strlcat(buf, "/10MHz", bsize);
4025 	if (IEEE80211_IS_CHAN_QUARTER(c))
4026 		strlcat(buf, "/5MHz", bsize);
4027 	if (IEEE80211_IS_CHAN_TURBO(c))
4028 		strlcat(buf, " Turbo", bsize);
4029 	if (precise) {
4030 		if (IEEE80211_IS_CHAN_VHT80P80(c))
4031 			strlcat(buf, " vht/80p80", bsize);
4032 		else if (IEEE80211_IS_CHAN_VHT160(c))
4033 			strlcat(buf, " vht/160", bsize);
4034 		else if (IEEE80211_IS_CHAN_VHT80(c) &&
4035 		    IEEE80211_IS_CHAN_HT40D(c))
4036 			strlcat(buf, " vht/80-", bsize);
4037 		else if (IEEE80211_IS_CHAN_VHT80(c) &&
4038 		    IEEE80211_IS_CHAN_HT40U(c))
4039 			strlcat(buf, " vht/80+", bsize);
4040 		else if (IEEE80211_IS_CHAN_VHT80(c))
4041 			strlcat(buf, " vht/80", bsize);
4042 		else if (IEEE80211_IS_CHAN_VHT40D(c))
4043 			strlcat(buf, " vht/40-", bsize);
4044 		else if (IEEE80211_IS_CHAN_VHT40U(c))
4045 			strlcat(buf, " vht/40+", bsize);
4046 		else if (IEEE80211_IS_CHAN_VHT20(c))
4047 			strlcat(buf, " vht/20", bsize);
4048 		else if (IEEE80211_IS_CHAN_HT20(c))
4049 			strlcat(buf, " ht/20", bsize);
4050 		else if (IEEE80211_IS_CHAN_HT40D(c))
4051 			strlcat(buf, " ht/40-", bsize);
4052 		else if (IEEE80211_IS_CHAN_HT40U(c))
4053 			strlcat(buf, " ht/40+", bsize);
4054 	} else {
4055 		if (IEEE80211_IS_CHAN_VHT(c))
4056 			strlcat(buf, " vht", bsize);
4057 		else if (IEEE80211_IS_CHAN_HT(c))
4058 			strlcat(buf, " ht", bsize);
4059 	}
4060 	return buf;
4061 }
4062 
4063 static void
print_chaninfo(const struct ieee80211_channel * c,int verb)4064 print_chaninfo(const struct ieee80211_channel *c, int verb)
4065 {
4066 	char buf[14];
4067 
4068 	if (verb)
4069 		printf("Channel %3u : %u%c%c%c%c%c MHz%-14.14s",
4070 		    ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
4071 		    IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
4072 		    IEEE80211_IS_CHAN_DFS(c) ? 'D' : ' ',
4073 		    IEEE80211_IS_CHAN_RADAR(c) ? 'R' : ' ',
4074 		    IEEE80211_IS_CHAN_CWINT(c) ? 'I' : ' ',
4075 		    IEEE80211_IS_CHAN_CACDONE(c) ? 'C' : ' ',
4076 		    get_chaninfo(c, verb, buf, sizeof(buf)));
4077 	else
4078 	printf("Channel %3u : %u%c MHz%-14.14s",
4079 	    ieee80211_mhz2ieee(c->ic_freq, c->ic_flags), c->ic_freq,
4080 	    IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ',
4081 	    get_chaninfo(c, verb, buf, sizeof(buf)));
4082 
4083 }
4084 
4085 static int
chanpref(const struct ieee80211_channel * c)4086 chanpref(const struct ieee80211_channel *c)
4087 {
4088 
4089 	if (IEEE80211_IS_CHAN_VHT80P80(c))
4090 		return 90;
4091 	if (IEEE80211_IS_CHAN_VHT160(c))
4092 		return 80;
4093 	if (IEEE80211_IS_CHAN_VHT80(c))
4094 		return 70;
4095 	if (IEEE80211_IS_CHAN_VHT40(c))
4096 		return 60;
4097 	if (IEEE80211_IS_CHAN_VHT20(c))
4098 		return 50;
4099 	if (IEEE80211_IS_CHAN_HT40(c))
4100 		return 40;
4101 	if (IEEE80211_IS_CHAN_HT20(c))
4102 		return 30;
4103 	if (IEEE80211_IS_CHAN_HALF(c))
4104 		return 10;
4105 	if (IEEE80211_IS_CHAN_QUARTER(c))
4106 		return 5;
4107 	if (IEEE80211_IS_CHAN_TURBO(c))
4108 		return 25;
4109 	if (IEEE80211_IS_CHAN_A(c))
4110 		return 20;
4111 	if (IEEE80211_IS_CHAN_G(c))
4112 		return 20;
4113 	if (IEEE80211_IS_CHAN_B(c))
4114 		return 15;
4115 	if (IEEE80211_IS_CHAN_PUREG(c))
4116 		return 15;
4117 	return 0;
4118 }
4119 
4120 static void
print_channels(if_ctx * ctx,const struct ieee80211req_chaninfo * chans,int allchans,int verb)4121 print_channels(if_ctx *ctx, const struct ieee80211req_chaninfo *chans,
4122 	int allchans, int verb)
4123 {
4124 	struct ieee80211req_chaninfo *achans;
4125 	uint8_t reported[IEEE80211_CHAN_BYTES];
4126 	const struct ieee80211_channel *c;
4127 	unsigned int i, half;
4128 
4129 	achans = malloc(IEEE80211_CHANINFO_SPACE(chans));
4130 	if (achans == NULL)
4131 		errx(1, "no space for active channel list");
4132 	achans->ic_nchans = 0;
4133 	memset(reported, 0, sizeof(reported));
4134 	if (!allchans) {
4135 		struct ieee80211req_chanlist active;
4136 
4137 		if (get80211(ctx, IEEE80211_IOC_CHANLIST, &active, sizeof(active)) < 0)
4138 			errx(1, "unable to get active channel list");
4139 		for (i = 0; i < chans->ic_nchans; i++) {
4140 			c = &chans->ic_chans[i];
4141 			if (!isset(active.ic_channels, c->ic_ieee))
4142 				continue;
4143 			/*
4144 			 * Suppress compatible duplicates unless
4145 			 * verbose.  The kernel gives us it's
4146 			 * complete channel list which has separate
4147 			 * entries for 11g/11b and 11a/turbo.
4148 			 */
4149 			if (isset(reported, c->ic_ieee) && !verb) {
4150 				/* XXX we assume duplicates are adjacent */
4151 				achans->ic_chans[achans->ic_nchans-1] = *c;
4152 			} else {
4153 				achans->ic_chans[achans->ic_nchans++] = *c;
4154 				setbit(reported, c->ic_ieee);
4155 			}
4156 		}
4157 	} else {
4158 		for (i = 0; i < chans->ic_nchans; i++) {
4159 			c = &chans->ic_chans[i];
4160 			/* suppress duplicates as above */
4161 			if (isset(reported, c->ic_ieee) && !verb) {
4162 				/* XXX we assume duplicates are adjacent */
4163 				struct ieee80211_channel *a =
4164 				    &achans->ic_chans[achans->ic_nchans-1];
4165 				if (chanpref(c) > chanpref(a))
4166 					*a = *c;
4167 			} else {
4168 				achans->ic_chans[achans->ic_nchans++] = *c;
4169 				setbit(reported, c->ic_ieee);
4170 			}
4171 		}
4172 	}
4173 	half = achans->ic_nchans / 2;
4174 	if (achans->ic_nchans % 2)
4175 		half++;
4176 
4177 	for (i = 0; i < achans->ic_nchans / 2; i++) {
4178 		print_chaninfo(&achans->ic_chans[i], verb);
4179 		print_chaninfo(&achans->ic_chans[half+i], verb);
4180 		printf("\n");
4181 	}
4182 	if (achans->ic_nchans % 2) {
4183 		print_chaninfo(&achans->ic_chans[i], verb);
4184 		printf("\n");
4185 	}
4186 	free(achans);
4187 }
4188 
4189 static void
list_channels(if_ctx * ctx,int allchans)4190 list_channels(if_ctx *ctx, int allchans)
4191 {
4192 	getchaninfo(ctx);
4193 	print_channels(ctx, chaninfo, allchans, ctx->args->verbose);
4194 }
4195 
4196 static void
print_txpow(const struct ieee80211_channel * c)4197 print_txpow(const struct ieee80211_channel *c)
4198 {
4199 	printf("Channel %3u : %u MHz %3.1f reg %2d  ",
4200 	    c->ic_ieee, c->ic_freq,
4201 	    c->ic_maxpower/2., c->ic_maxregpower);
4202 }
4203 
4204 static void
print_txpow_verbose(const struct ieee80211_channel * c)4205 print_txpow_verbose(const struct ieee80211_channel *c)
4206 {
4207 	print_chaninfo(c, 1);
4208 	printf("min %4.1f dBm  max %3.1f dBm  reg %2d dBm",
4209 	    c->ic_minpower/2., c->ic_maxpower/2., c->ic_maxregpower);
4210 	/* indicate where regulatory cap limits power use */
4211 	if (c->ic_maxpower > 2*c->ic_maxregpower)
4212 		printf(" <");
4213 }
4214 
4215 static void
list_txpow(if_ctx * ctx)4216 list_txpow(if_ctx *ctx)
4217 {
4218 	struct ieee80211req_chaninfo *achans;
4219 	uint8_t reported[IEEE80211_CHAN_BYTES];
4220 	struct ieee80211_channel *c, *prev;
4221 	unsigned int i, half;
4222 
4223 	getchaninfo(ctx);
4224 	achans = malloc(IEEE80211_CHANINFO_SPACE(chaninfo));
4225 	if (achans == NULL)
4226 		errx(1, "no space for active channel list");
4227 	achans->ic_nchans = 0;
4228 	memset(reported, 0, sizeof(reported));
4229 	for (i = 0; i < chaninfo->ic_nchans; i++) {
4230 		c = &chaninfo->ic_chans[i];
4231 		/* suppress duplicates as above */
4232 		if (isset(reported, c->ic_ieee) && !ctx->args->verbose) {
4233 			/* XXX we assume duplicates are adjacent */
4234 			assert(achans->ic_nchans > 0);
4235 			prev = &achans->ic_chans[achans->ic_nchans-1];
4236 			/* display highest power on channel */
4237 			if (c->ic_maxpower > prev->ic_maxpower)
4238 				*prev = *c;
4239 		} else {
4240 			achans->ic_chans[achans->ic_nchans++] = *c;
4241 			setbit(reported, c->ic_ieee);
4242 		}
4243 	}
4244 	if (!ctx->args->verbose) {
4245 		half = achans->ic_nchans / 2;
4246 		if (achans->ic_nchans % 2)
4247 			half++;
4248 
4249 		for (i = 0; i < achans->ic_nchans / 2; i++) {
4250 			print_txpow(&achans->ic_chans[i]);
4251 			print_txpow(&achans->ic_chans[half+i]);
4252 			printf("\n");
4253 		}
4254 		if (achans->ic_nchans % 2) {
4255 			print_txpow(&achans->ic_chans[i]);
4256 			printf("\n");
4257 		}
4258 	} else {
4259 		for (i = 0; i < achans->ic_nchans; i++) {
4260 			print_txpow_verbose(&achans->ic_chans[i]);
4261 			printf("\n");
4262 		}
4263 	}
4264 	free(achans);
4265 }
4266 
4267 static void
list_keys(int s __unused)4268 list_keys(int s __unused)
4269 {
4270 }
4271 
4272 static void
list_capabilities(if_ctx * ctx)4273 list_capabilities(if_ctx *ctx)
4274 {
4275 	struct ieee80211_devcaps_req *dc;
4276 	const int verbose = ctx->args->verbose;
4277 
4278 	if (verbose)
4279 		dc = malloc(IEEE80211_DEVCAPS_SIZE(MAXCHAN));
4280 	else
4281 		dc = malloc(IEEE80211_DEVCAPS_SIZE(1));
4282 	if (dc == NULL)
4283 		errx(1, "no space for device capabilities");
4284 	dc->dc_chaninfo.ic_nchans = verbose ? MAXCHAN : 1;
4285 	getdevcaps(ctx, dc);
4286 	printb("drivercaps", dc->dc_drivercaps, IEEE80211_C_BITS);
4287 	if (dc->dc_cryptocaps != 0 || verbose) {
4288 		putchar('\n');
4289 		printb("cryptocaps", dc->dc_cryptocaps, IEEE80211_CRYPTO_BITS);
4290 	}
4291 	if (dc->dc_htcaps != 0 || verbose) {
4292 		putchar('\n');
4293 		printb("htcaps", dc->dc_htcaps, IEEE80211_HTCAP_BITS);
4294 	}
4295 	if (dc->dc_vhtcaps != 0 || verbose) {
4296 		putchar('\n');
4297 		printb("vhtcaps", dc->dc_vhtcaps, IEEE80211_VHTCAP_BITS);
4298 	}
4299 
4300 	putchar('\n');
4301 	if (verbose) {
4302 		chaninfo = &dc->dc_chaninfo;	/* XXX */
4303 		print_channels(ctx, &dc->dc_chaninfo, 1/*allchans*/, verbose);
4304 	}
4305 	free(dc);
4306 }
4307 
4308 static int
get80211wme(if_ctx * ctx,int param,int ac,int * val)4309 get80211wme(if_ctx *ctx, int param, int ac, int *val)
4310 {
4311 	struct ieee80211req ireq = {};
4312 
4313 	strlcpy(ireq.i_name, ctx->ifname, sizeof(ireq.i_name));
4314 	ireq.i_type = param;
4315 	ireq.i_len = ac;
4316 	if (ioctl_ctx(ctx, SIOCG80211, &ireq) < 0) {
4317 		warn("cannot get WME parameter %d, ac %d%s",
4318 		    param, ac & IEEE80211_WMEPARAM_VAL,
4319 		    ac & IEEE80211_WMEPARAM_BSS ? " (BSS)" : "");
4320 		return -1;
4321 	}
4322 	*val = ireq.i_val;
4323 	return 0;
4324 }
4325 
4326 static void
list_wme_aci(if_ctx * ctx,const char * tag,int ac)4327 list_wme_aci(if_ctx *ctx, const char *tag, int ac)
4328 {
4329 	int val;
4330 
4331 	printf("\t%s", tag);
4332 
4333 	/* show WME BSS parameters */
4334 	if (get80211wme(ctx, IEEE80211_IOC_WME_CWMIN, ac, &val) != -1)
4335 		printf(" cwmin %2u", val);
4336 	if (get80211wme(ctx, IEEE80211_IOC_WME_CWMAX, ac, &val) != -1)
4337 		printf(" cwmax %2u", val);
4338 	if (get80211wme(ctx, IEEE80211_IOC_WME_AIFS, ac, &val) != -1)
4339 		printf(" aifs %2u", val);
4340 	if (get80211wme(ctx, IEEE80211_IOC_WME_TXOPLIMIT, ac, &val) != -1)
4341 		printf(" txopLimit %3u", val);
4342 	if (get80211wme(ctx, IEEE80211_IOC_WME_ACM, ac, &val) != -1) {
4343 		if (val)
4344 			printf(" acm");
4345 		else if (ctx->args->verbose)
4346 			printf(" -acm");
4347 	}
4348 	/* !BSS only */
4349 	if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
4350 		if (get80211wme(ctx, IEEE80211_IOC_WME_ACKPOLICY, ac, &val) != -1) {
4351 			if (!val)
4352 				printf(" -ack");
4353 			else if (ctx->args->verbose)
4354 				printf(" ack");
4355 		}
4356 	}
4357 	printf("\n");
4358 }
4359 
4360 static void
list_wme(if_ctx * ctx)4361 list_wme(if_ctx *ctx)
4362 {
4363 	static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
4364 	int ac;
4365 
4366 	if (ctx->args->verbose) {
4367 		/* display both BSS and local settings */
4368 		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
4369 	again:
4370 			if (ac & IEEE80211_WMEPARAM_BSS)
4371 				list_wme_aci(ctx, "     ", ac);
4372 			else
4373 				list_wme_aci(ctx, acnames[ac], ac);
4374 			if ((ac & IEEE80211_WMEPARAM_BSS) == 0) {
4375 				ac |= IEEE80211_WMEPARAM_BSS;
4376 				goto again;
4377 			} else
4378 				ac &= ~IEEE80211_WMEPARAM_BSS;
4379 		}
4380 	} else {
4381 		/* display only channel settings */
4382 		for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++)
4383 			list_wme_aci(ctx, acnames[ac], ac);
4384 	}
4385 }
4386 
4387 static void
list_roam(if_ctx * ctx)4388 list_roam(if_ctx *ctx)
4389 {
4390 	const struct ieee80211_roamparam *rp;
4391 	int mode;
4392 
4393 	getroam(ctx);
4394 	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
4395 		rp = &roamparams.params[mode];
4396 		if (rp->rssi == 0 && rp->rate == 0)
4397 			continue;
4398 		if (mode == IEEE80211_MODE_11NA ||
4399 		    mode == IEEE80211_MODE_11NG ||
4400 		    mode == IEEE80211_MODE_VHT_2GHZ ||
4401 		    mode == IEEE80211_MODE_VHT_5GHZ) {
4402 			if (rp->rssi & 1)
4403 				LINE_CHECK("roam:%-7.7s rssi %2u.5dBm  MCS %2u    ",
4404 				    modename[mode], rp->rssi/2,
4405 				    rp->rate &~ IEEE80211_RATE_MCS);
4406 			else
4407 				LINE_CHECK("roam:%-7.7s rssi %4udBm  MCS %2u    ",
4408 				    modename[mode], rp->rssi/2,
4409 				    rp->rate &~ IEEE80211_RATE_MCS);
4410 		} else {
4411 			if (rp->rssi & 1)
4412 				LINE_CHECK("roam:%-7.7s rssi %2u.5dBm rate %2u Mb/s",
4413 				    modename[mode], rp->rssi/2, rp->rate/2);
4414 			else
4415 				LINE_CHECK("roam:%-7.7s rssi %4udBm rate %2u Mb/s",
4416 				    modename[mode], rp->rssi/2, rp->rate/2);
4417 		}
4418 	}
4419 }
4420 
4421 /* XXX TODO: rate-to-string method... */
4422 static const char*
get_mcs_mbs_rate_str(uint8_t rate)4423 get_mcs_mbs_rate_str(uint8_t rate)
4424 {
4425 	return (rate & IEEE80211_RATE_MCS) ? "MCS " : "Mb/s";
4426 }
4427 
4428 static uint8_t
get_rate_value(uint8_t rate)4429 get_rate_value(uint8_t rate)
4430 {
4431 	if (rate & IEEE80211_RATE_MCS)
4432 		return (rate &~ IEEE80211_RATE_MCS);
4433 	return (rate / 2);
4434 }
4435 
4436 static void
list_txparams(if_ctx * ctx)4437 list_txparams(if_ctx *ctx)
4438 {
4439 	const struct ieee80211_txparam *tp;
4440 	int mode;
4441 
4442 	gettxparams(ctx);
4443 	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
4444 		tp = &txparams.params[mode];
4445 		if (tp->mgmtrate == 0 && tp->mcastrate == 0)
4446 			continue;
4447 		if (mode == IEEE80211_MODE_11NA ||
4448 		    mode == IEEE80211_MODE_11NG ||
4449 		    mode == IEEE80211_MODE_VHT_2GHZ ||
4450 		    mode == IEEE80211_MODE_VHT_5GHZ) {
4451 			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
4452 				LINE_CHECK("%-7.7s ucast NONE    mgmt %2u %s "
4453 				    "mcast %2u %s maxretry %u",
4454 				    modename[mode],
4455 				    get_rate_value(tp->mgmtrate),
4456 				    get_mcs_mbs_rate_str(tp->mgmtrate),
4457 				    get_rate_value(tp->mcastrate),
4458 				    get_mcs_mbs_rate_str(tp->mcastrate),
4459 				    tp->maxretry);
4460 			else
4461 				LINE_CHECK("%-7.7s ucast %2u MCS  mgmt %2u %s "
4462 				    "mcast %2u %s maxretry %u",
4463 				    modename[mode],
4464 				    tp->ucastrate &~ IEEE80211_RATE_MCS,
4465 				    get_rate_value(tp->mgmtrate),
4466 				    get_mcs_mbs_rate_str(tp->mgmtrate),
4467 				    get_rate_value(tp->mcastrate),
4468 				    get_mcs_mbs_rate_str(tp->mcastrate),
4469 				    tp->maxretry);
4470 		} else {
4471 			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
4472 				LINE_CHECK("%-7.7s ucast NONE    mgmt %2u Mb/s "
4473 				    "mcast %2u Mb/s maxretry %u",
4474 				    modename[mode],
4475 				    tp->mgmtrate/2,
4476 				    tp->mcastrate/2, tp->maxretry);
4477 			else
4478 				LINE_CHECK("%-7.7s ucast %2u Mb/s mgmt %2u Mb/s "
4479 				    "mcast %2u Mb/s maxretry %u",
4480 				    modename[mode],
4481 				    tp->ucastrate/2, tp->mgmtrate/2,
4482 				    tp->mcastrate/2, tp->maxretry);
4483 		}
4484 	}
4485 }
4486 
4487 static void
printpolicy(int policy)4488 printpolicy(int policy)
4489 {
4490 	switch (policy) {
4491 	case IEEE80211_MACCMD_POLICY_OPEN:
4492 		printf("policy: open\n");
4493 		break;
4494 	case IEEE80211_MACCMD_POLICY_ALLOW:
4495 		printf("policy: allow\n");
4496 		break;
4497 	case IEEE80211_MACCMD_POLICY_DENY:
4498 		printf("policy: deny\n");
4499 		break;
4500 	case IEEE80211_MACCMD_POLICY_RADIUS:
4501 		printf("policy: radius\n");
4502 		break;
4503 	default:
4504 		printf("policy: unknown (%u)\n", policy);
4505 		break;
4506 	}
4507 }
4508 
4509 static void
list_mac(if_ctx * ctx)4510 list_mac(if_ctx *ctx)
4511 {
4512 	struct ieee80211req ireq = {};
4513 	struct ieee80211req_maclist *acllist;
4514 	int i, nacls, policy, len;
4515 	uint8_t *data;
4516 	char c;
4517 
4518 	strlcpy(ireq.i_name, ctx->ifname, sizeof(ireq.i_name)); /* XXX ?? */
4519 	ireq.i_type = IEEE80211_IOC_MACCMD;
4520 	ireq.i_val = IEEE80211_MACCMD_POLICY;
4521 	if (ioctl_ctx(ctx, SIOCG80211, &ireq) < 0) {
4522 		if (errno == EINVAL) {
4523 			printf("No acl policy loaded\n");
4524 			return;
4525 		}
4526 		err(1, "unable to get mac policy");
4527 	}
4528 	policy = ireq.i_val;
4529 	if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
4530 		c = '*';
4531 	} else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
4532 		c = '+';
4533 	} else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
4534 		c = '-';
4535 	} else if (policy == IEEE80211_MACCMD_POLICY_RADIUS) {
4536 		c = 'r';		/* NB: should never have entries */
4537 	} else {
4538 		printf("policy: unknown (%u)\n", policy);
4539 		c = '?';
4540 	}
4541 	if (ctx->args->verbose || c == '?')
4542 		printpolicy(policy);
4543 
4544 	ireq.i_val = IEEE80211_MACCMD_LIST;
4545 	ireq.i_len = 0;
4546 	if (ioctl_ctx(ctx, SIOCG80211, &ireq) < 0)
4547 		err(1, "unable to get mac acl list size");
4548 	if (ireq.i_len == 0) {		/* NB: no acls */
4549 		if (!(ctx->args->verbose || c == '?'))
4550 			printpolicy(policy);
4551 		return;
4552 	}
4553 	len = ireq.i_len;
4554 
4555 	data = malloc(len);
4556 	if (data == NULL)
4557 		err(1, "out of memory for acl list");
4558 
4559 	ireq.i_data = data;
4560 	if (ioctl_ctx(ctx, SIOCG80211, &ireq) < 0)
4561 		err(1, "unable to get mac acl list");
4562 	nacls = len / sizeof(*acllist);
4563 	acllist = (struct ieee80211req_maclist *) data;
4564 	for (i = 0; i < nacls; i++)
4565 		printf("%c%s\n", c, ether_ntoa(
4566 			(const struct ether_addr *) acllist[i].ml_macaddr));
4567 	free(data);
4568 }
4569 
4570 static void
print_regdomain(const struct ieee80211_regdomain * reg,int verb)4571 print_regdomain(const struct ieee80211_regdomain *reg, int verb)
4572 {
4573 	if ((reg->regdomain != 0 &&
4574 	    reg->regdomain != reg->country) || verb) {
4575 		const struct regdomain *rd =
4576 		    lib80211_regdomain_findbysku(getregdata(), reg->regdomain);
4577 		if (rd == NULL)
4578 			LINE_CHECK("regdomain %d", reg->regdomain);
4579 		else
4580 			LINE_CHECK("regdomain %s", rd->name);
4581 	}
4582 	if (reg->country != 0 || verb) {
4583 		const struct country *cc =
4584 		    lib80211_country_findbycc(getregdata(), reg->country);
4585 		if (cc == NULL)
4586 			LINE_CHECK("country %d", reg->country);
4587 		else
4588 			LINE_CHECK("country %s", cc->isoname);
4589 	}
4590 	if (reg->location == 'I')
4591 		LINE_CHECK("indoor");
4592 	else if (reg->location == 'O')
4593 		LINE_CHECK("outdoor");
4594 	else if (verb)
4595 		LINE_CHECK("anywhere");
4596 	if (reg->ecm)
4597 		LINE_CHECK("ecm");
4598 	else if (verb)
4599 		LINE_CHECK("-ecm");
4600 }
4601 
4602 static void
list_regdomain(if_ctx * ctx,int channelsalso)4603 list_regdomain(if_ctx *ctx, int channelsalso)
4604 {
4605 	getregdomain(ctx);
4606 	if (channelsalso) {
4607 		getchaninfo(ctx);
4608 		spacer = ':';
4609 		print_regdomain(&regdomain, 1);
4610 		LINE_BREAK();
4611 		print_channels(ctx, chaninfo, 1/*allchans*/, 1/*verbose*/);
4612 	} else
4613 		print_regdomain(&regdomain, ctx->args->verbose);
4614 }
4615 
4616 static void
list_mesh(if_ctx * ctx)4617 list_mesh(if_ctx *ctx)
4618 {
4619 	struct ieee80211req ireq = {};
4620 	struct ieee80211req_mesh_route routes[128];
4621 	struct ieee80211req_mesh_route *rt;
4622 
4623 	strlcpy(ireq.i_name, ctx->ifname, sizeof(ireq.i_name));
4624 	ireq.i_type = IEEE80211_IOC_MESH_RTCMD;
4625 	ireq.i_val = IEEE80211_MESH_RTCMD_LIST;
4626 	ireq.i_data = &routes;
4627 	ireq.i_len = sizeof(routes);
4628 	if (ioctl_ctx(ctx, SIOCG80211, &ireq) < 0)
4629 	 	err(1, "unable to get the Mesh routing table");
4630 
4631 	printf("%-17.17s %-17.17s %4s %4s %4s %6s %s\n"
4632 		, "DEST"
4633 		, "NEXT HOP"
4634 		, "HOPS"
4635 		, "METRIC"
4636 		, "LIFETIME"
4637 		, "MSEQ"
4638 		, "FLAGS");
4639 
4640 	for (unsigned int i = 0; i < ireq.i_len / sizeof(*rt); i++) {
4641 		rt = &routes[i];
4642 		printf("%s ",
4643 		    ether_ntoa((const struct ether_addr *)rt->imr_dest));
4644 		printf("%s %4u   %4u   %6u %6u    %c%c\n",
4645 			ether_ntoa((const struct ether_addr *)rt->imr_nexthop),
4646 			rt->imr_nhops, rt->imr_metric, rt->imr_lifetime,
4647 			rt->imr_lastmseq,
4648 			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_DISCOVER) ?
4649 			    'D' :
4650 			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_VALID) ?
4651 			    'V' : '!',
4652 			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_PROXY) ?
4653 			    'P' :
4654 			(rt->imr_flags & IEEE80211_MESHRT_FLAGS_GATE) ?
4655 			    'G' :' ');
4656 	}
4657 }
4658 
4659 static void
set80211list(if_ctx * ctx,const char * arg,int dummy __unused)4660 set80211list(if_ctx *ctx, const char *arg, int dummy __unused)
4661 {
4662 	int s = ctx->io_s;
4663 #define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
4664 
4665 	LINE_INIT('\t');
4666 
4667 	if (iseq(arg, "sta"))
4668 		list_stations(ctx);
4669 	else if (iseq(arg, "scan") || iseq(arg, "ap"))
4670 		list_scan(ctx);
4671 	else if (iseq(arg, "chan") || iseq(arg, "freq"))
4672 		list_channels(ctx, 1);
4673 	else if (iseq(arg, "active"))
4674 		list_channels(ctx, 0);
4675 	else if (iseq(arg, "keys"))
4676 		list_keys(s);
4677 	else if (iseq(arg, "caps"))
4678 		list_capabilities(ctx);
4679 	else if (iseq(arg, "wme") || iseq(arg, "wmm"))
4680 		list_wme(ctx);
4681 	else if (iseq(arg, "mac"))
4682 		list_mac(ctx);
4683 	else if (iseq(arg, "txpow"))
4684 		list_txpow(ctx);
4685 	else if (iseq(arg, "roam"))
4686 		list_roam(ctx);
4687 	else if (iseq(arg, "txparam") || iseq(arg, "txparm"))
4688 		list_txparams(ctx);
4689 	else if (iseq(arg, "regdomain"))
4690 		list_regdomain(ctx, 1);
4691 	else if (iseq(arg, "countries"))
4692 		list_countries();
4693 	else if (iseq(arg, "mesh"))
4694 		list_mesh(ctx);
4695 	else
4696 		errx(1, "Don't know how to list %s for %s", arg, ctx->ifname);
4697 	LINE_BREAK();
4698 #undef iseq
4699 }
4700 
4701 static enum ieee80211_opmode
get80211opmode(if_ctx * ctx)4702 get80211opmode(if_ctx *ctx)
4703 {
4704 	struct ifmediareq ifmr = {};
4705 
4706 	strlcpy(ifmr.ifm_name, ctx->ifname, sizeof(ifmr.ifm_name));
4707 
4708 	if (ioctl_ctx(ctx, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
4709 		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) {
4710 			if (ifmr.ifm_current & IFM_FLAG0)
4711 				return IEEE80211_M_AHDEMO;
4712 			else
4713 				return IEEE80211_M_IBSS;
4714 		}
4715 		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
4716 			return IEEE80211_M_HOSTAP;
4717 		if (ifmr.ifm_current & IFM_IEEE80211_IBSS)
4718 			return IEEE80211_M_IBSS;
4719 		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
4720 			return IEEE80211_M_MONITOR;
4721 		if (ifmr.ifm_current & IFM_IEEE80211_MBSS)
4722 			return IEEE80211_M_MBSS;
4723 	}
4724 	return IEEE80211_M_STA;
4725 }
4726 
4727 #if 0
4728 static void
4729 printcipher(int s, struct ieee80211req *ireq, int keylenop)
4730 {
4731 	switch (ireq->i_val) {
4732 	case IEEE80211_CIPHER_WEP:
4733 		ireq->i_type = keylenop;
4734 		if (ioctl(s, SIOCG80211, ireq) != -1)
4735 			printf("WEP-%s",
4736 			    ireq->i_len <= 5 ? "40" :
4737 			    ireq->i_len <= 13 ? "104" : "128");
4738 		else
4739 			printf("WEP");
4740 		break;
4741 	case IEEE80211_CIPHER_TKIP:
4742 		printf("TKIP");
4743 		break;
4744 	case IEEE80211_CIPHER_AES_OCB:
4745 		printf("AES-OCB");
4746 		break;
4747 	case IEEE80211_CIPHER_AES_CCM:
4748 		printf("AES-CCM");
4749 		break;
4750 	case IEEE80211_CIPHER_CKIP:
4751 		printf("CKIP");
4752 		break;
4753 	case IEEE80211_CIPHER_NONE:
4754 		printf("NONE");
4755 		break;
4756 	default:
4757 		printf("UNKNOWN (0x%x)", ireq->i_val);
4758 		break;
4759 	}
4760 }
4761 #endif
4762 
4763 static void
printkey(if_ctx * ctx,const struct ieee80211req_key * ik)4764 printkey(if_ctx *ctx, const struct ieee80211req_key *ik)
4765 {
4766 	static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
4767 	u_int keylen = ik->ik_keylen;
4768 	int printcontents;
4769 	const int verbose = ctx->args->verbose;
4770 	const bool printkeys = ctx->args->printkeys;
4771 
4772 	printcontents = printkeys &&
4773 		(memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
4774 	if (printcontents)
4775 		LINE_BREAK();
4776 	switch (ik->ik_type) {
4777 	case IEEE80211_CIPHER_WEP:
4778 		/* compatibility */
4779 		LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
4780 		    keylen <= 5 ? "40-bit" :
4781 		    keylen <= 13 ? "104-bit" : "128-bit");
4782 		break;
4783 	case IEEE80211_CIPHER_TKIP:
4784 		if (keylen > 128/8)
4785 			keylen -= 128/8;	/* ignore MIC for now */
4786 		LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4787 		break;
4788 	case IEEE80211_CIPHER_AES_OCB:
4789 		LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4790 		break;
4791 	case IEEE80211_CIPHER_AES_CCM:
4792 		LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4793 		break;
4794 	case IEEE80211_CIPHER_CKIP:
4795 		LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4796 		break;
4797 	case IEEE80211_CIPHER_NONE:
4798 		LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
4799 		break;
4800 	default:
4801 		LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
4802 			ik->ik_type, ik->ik_keyix+1, 8*keylen);
4803 		break;
4804 	}
4805 	if (printcontents) {
4806 		u_int i;
4807 
4808 		printf(" <");
4809 		for (i = 0; i < keylen; i++)
4810 			printf("%02x", ik->ik_keydata[i]);
4811 		printf(">");
4812 		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4813 		    (ik->ik_keyrsc != 0 || verbose))
4814 			printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
4815 		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
4816 		    (ik->ik_keytsc != 0 || verbose))
4817 			printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
4818 		if (ik->ik_flags != 0 && verbose) {
4819 			const char *sep = " ";
4820 
4821 			if (ik->ik_flags & IEEE80211_KEY_XMIT)
4822 				printf("%stx", sep), sep = "+";
4823 			if (ik->ik_flags & IEEE80211_KEY_RECV)
4824 				printf("%srx", sep), sep = "+";
4825 			if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
4826 				printf("%sdef", sep), sep = "+";
4827 		}
4828 		LINE_BREAK();
4829 	}
4830 }
4831 
4832 static void
printrate(const char * tag,int v,int defrate,int defmcs)4833 printrate(const char *tag, int v, int defrate, int defmcs)
4834 {
4835 	if ((v & IEEE80211_RATE_MCS) == 0) {
4836 		if (v != defrate) {
4837 			if (v & 1)
4838 				LINE_CHECK("%s %d.5", tag, v/2);
4839 			else
4840 				LINE_CHECK("%s %d", tag, v/2);
4841 		}
4842 	} else {
4843 		if (v != defmcs)
4844 			LINE_CHECK("%s %d", tag, v &~ 0x80);
4845 	}
4846 }
4847 
4848 static int
getid(if_ctx * ctx,int ix,void * data,size_t len,int * plen,int mesh)4849 getid(if_ctx *ctx, int ix, void *data, size_t len, int *plen, int mesh)
4850 {
4851 	struct ieee80211req ireq = {};
4852 
4853 	strlcpy(ireq.i_name, ctx->ifname, sizeof(ireq.i_name));
4854 	ireq.i_type = (!mesh) ? IEEE80211_IOC_SSID : IEEE80211_IOC_MESH_ID;
4855 	ireq.i_val = ix;
4856 	ireq.i_data = data;
4857 	ireq.i_len = len;
4858 	if (ioctl_ctx(ctx, SIOCG80211, &ireq) < 0)
4859 		return -1;
4860 	*plen = ireq.i_len;
4861 	return 0;
4862 }
4863 
4864 static int
getdevicename(if_ctx * ctx,void * data,size_t len,int * plen)4865 getdevicename(if_ctx *ctx, void *data, size_t len, int *plen)
4866 {
4867 	struct ieee80211req ireq = {};
4868 
4869 	strlcpy(ireq.i_name, ctx->ifname, sizeof(ireq.i_name));
4870 	ireq.i_type = IEEE80211_IOC_IC_NAME;
4871 	ireq.i_val = -1;
4872 	ireq.i_data = data;
4873 	ireq.i_len = len;
4874 	if (ioctl_ctx(ctx, SIOCG80211, &ireq) < 0)
4875 		return (-1);
4876 	*plen = ireq.i_len;
4877 	return (0);
4878 }
4879 
4880 static void
ieee80211_status(if_ctx * ctx)4881 ieee80211_status(if_ctx *ctx)
4882 {
4883 	int s = ctx->io_s;
4884 	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
4885 	enum ieee80211_opmode opmode = get80211opmode(ctx);
4886 	int i, num, wpa, wme, bgscan, bgscaninterval, val, len, wepmode;
4887 	uint8_t data[32];
4888 	const struct ieee80211_channel *c;
4889 	const struct ieee80211_roamparam *rp;
4890 	const struct ieee80211_txparam *tp;
4891 	const int verbose = ctx->args->verbose;
4892 
4893 	if (getid(ctx, -1, data, sizeof(data), &len, 0) < 0) {
4894 		/* If we can't get the SSID, this isn't an 802.11 device. */
4895 		return;
4896 	}
4897 
4898 	/*
4899 	 * Invalidate cached state so printing status for multiple
4900 	 * if's doesn't reuse the first interfaces' cached state.
4901 	 */
4902 	gotcurchan = 0;
4903 	gotroam = 0;
4904 	gottxparams = 0;
4905 	gothtconf = 0;
4906 	gotregdomain = 0;
4907 
4908 	printf("\t");
4909 	if (opmode == IEEE80211_M_MBSS) {
4910 		printf("meshid ");
4911 		getid(ctx, 0, data, sizeof(data), &len, 1);
4912 		print_string(data, len);
4913 	} else {
4914 		if (get80211val(ctx, IEEE80211_IOC_NUMSSIDS, &num) < 0)
4915 			num = 0;
4916 		printf("ssid ");
4917 		if (num > 1) {
4918 			for (i = 0; i < num; i++) {
4919 				if (getid(ctx, i, data, sizeof(data), &len, 0) >= 0 && len > 0) {
4920 					printf(" %d:", i + 1);
4921 					print_string(data, len);
4922 				}
4923 			}
4924 		} else
4925 			print_string(data, len);
4926 	}
4927 	c = getcurchan(ctx);
4928 	if (c->ic_freq != IEEE80211_CHAN_ANY) {
4929 		char buf[14];
4930 		printf(" channel %d (%u MHz%s)", c->ic_ieee, c->ic_freq,
4931 			get_chaninfo(c, 1, buf, sizeof(buf)));
4932 	} else if (verbose)
4933 		printf(" channel UNDEF");
4934 
4935 	if (get80211(ctx, IEEE80211_IOC_BSSID, data, IEEE80211_ADDR_LEN) >= 0 &&
4936 	    (memcmp(data, zerobssid, sizeof(zerobssid)) != 0 || verbose)) {
4937 		printf(" bssid %s", ether_ntoa((struct ether_addr *)data));
4938 		printbssidname((struct ether_addr *)data);
4939 	}
4940 
4941 	if (get80211len(ctx, IEEE80211_IOC_STATIONNAME, data, sizeof(data), &len) != -1) {
4942 		printf("\n\tstationname ");
4943 		print_string(data, len);
4944 	}
4945 
4946 	spacer = ' ';		/* force first break */
4947 	LINE_BREAK();
4948 
4949 	list_regdomain(ctx, 0);
4950 
4951 	wpa = 0;
4952 	if (get80211val(ctx, IEEE80211_IOC_AUTHMODE, &val) != -1) {
4953 		switch (val) {
4954 		case IEEE80211_AUTH_NONE:
4955 			LINE_CHECK("authmode NONE");
4956 			break;
4957 		case IEEE80211_AUTH_OPEN:
4958 			LINE_CHECK("authmode OPEN");
4959 			break;
4960 		case IEEE80211_AUTH_SHARED:
4961 			LINE_CHECK("authmode SHARED");
4962 			break;
4963 		case IEEE80211_AUTH_8021X:
4964 			LINE_CHECK("authmode 802.1x");
4965 			break;
4966 		case IEEE80211_AUTH_WPA:
4967 			if (get80211val(ctx, IEEE80211_IOC_WPA, &wpa) < 0)
4968 				wpa = 1;	/* default to WPA1 */
4969 			switch (wpa) {
4970 			case 2:
4971 				LINE_CHECK("authmode WPA2/802.11i");
4972 				break;
4973 			case 3:
4974 				LINE_CHECK("authmode WPA1+WPA2/802.11i");
4975 				break;
4976 			default:
4977 				LINE_CHECK("authmode WPA");
4978 				break;
4979 			}
4980 			break;
4981 		case IEEE80211_AUTH_AUTO:
4982 			LINE_CHECK("authmode AUTO");
4983 			break;
4984 		default:
4985 			LINE_CHECK("authmode UNKNOWN (0x%x)", val);
4986 			break;
4987 		}
4988 	}
4989 
4990 	if (wpa || verbose) {
4991 		if (get80211val(ctx, IEEE80211_IOC_WPS, &val) != -1) {
4992 			if (val)
4993 				LINE_CHECK("wps");
4994 			else if (verbose)
4995 				LINE_CHECK("-wps");
4996 		}
4997 		if (get80211val(ctx, IEEE80211_IOC_TSN, &val) != -1) {
4998 			if (val)
4999 				LINE_CHECK("tsn");
5000 			else if (verbose)
5001 				LINE_CHECK("-tsn");
5002 		}
5003 		if (ioctl(s, IEEE80211_IOC_COUNTERMEASURES, &val) != -1) {
5004 			if (val)
5005 				LINE_CHECK("countermeasures");
5006 			else if (verbose)
5007 				LINE_CHECK("-countermeasures");
5008 		}
5009 #if 0
5010 		/* XXX not interesting with WPA done in user space */
5011 		ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
5012 		if (ioctl(s, SIOCG80211, &ireq) != -1) {
5013 		}
5014 
5015 		ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
5016 		if (ioctl(s, SIOCG80211, &ireq) != -1) {
5017 			LINE_CHECK("mcastcipher ");
5018 			printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
5019 			spacer = ' ';
5020 		}
5021 
5022 		ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
5023 		if (ioctl(s, SIOCG80211, &ireq) != -1) {
5024 			LINE_CHECK("ucastcipher ");
5025 			printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
5026 		}
5027 
5028 		if (wpa & 2) {
5029 			ireq.i_type = IEEE80211_IOC_RSNCAPS;
5030 			if (ioctl(s, SIOCG80211, &ireq) != -1) {
5031 				LINE_CHECK("RSN caps 0x%x", ireq.i_val);
5032 				spacer = ' ';
5033 			}
5034 		}
5035 
5036 		ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
5037 		if (ioctl(s, SIOCG80211, &ireq) != -1) {
5038 		}
5039 #endif
5040 	}
5041 
5042 	if (get80211val(ctx, IEEE80211_IOC_WEP, &wepmode) != -1 &&
5043 	    wepmode != IEEE80211_WEP_NOSUP) {
5044 
5045 		switch (wepmode) {
5046 		case IEEE80211_WEP_OFF:
5047 			LINE_CHECK("privacy OFF");
5048 			break;
5049 		case IEEE80211_WEP_ON:
5050 			LINE_CHECK("privacy ON");
5051 			break;
5052 		case IEEE80211_WEP_MIXED:
5053 			LINE_CHECK("privacy MIXED");
5054 			break;
5055 		default:
5056 			LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
5057 			break;
5058 		}
5059 
5060 		/*
5061 		 * If we get here then we've got WEP support so we need
5062 		 * to print WEP status.
5063 		 */
5064 
5065 		if (get80211val(ctx, IEEE80211_IOC_WEPTXKEY, &val) < 0) {
5066 			warn("WEP support, but no tx key!");
5067 			goto end;
5068 		}
5069 		if (val != -1)
5070 			LINE_CHECK("deftxkey %d", val+1);
5071 		else if (wepmode != IEEE80211_WEP_OFF || verbose)
5072 			LINE_CHECK("deftxkey UNDEF");
5073 
5074 		if (get80211val(ctx, IEEE80211_IOC_NUMWEPKEYS, &num) < 0) {
5075 			warn("WEP support, but no NUMWEPKEYS support!");
5076 			goto end;
5077 		}
5078 
5079 		for (i = 0; i < num; i++) {
5080 			struct ieee80211req_key ik;
5081 
5082 			memset(&ik, 0, sizeof(ik));
5083 			ik.ik_keyix = i;
5084 			if (get80211(ctx, IEEE80211_IOC_WPAKEY, &ik, sizeof(ik)) < 0) {
5085 				warn("WEP support, but can get keys!");
5086 				goto end;
5087 			}
5088 			if (ik.ik_keylen != 0) {
5089 				if (verbose)
5090 					LINE_BREAK();
5091 				printkey(ctx, &ik);
5092 			}
5093 		}
5094 		if (i > 0 && verbose)
5095 			LINE_BREAK();
5096 end:
5097 		;
5098 	}
5099 
5100 	if (get80211val(ctx, IEEE80211_IOC_POWERSAVE, &val) != -1 &&
5101 	    val != IEEE80211_POWERSAVE_NOSUP ) {
5102 		if (val != IEEE80211_POWERSAVE_OFF || verbose) {
5103 			switch (val) {
5104 			case IEEE80211_POWERSAVE_OFF:
5105 				LINE_CHECK("powersavemode OFF");
5106 				break;
5107 			case IEEE80211_POWERSAVE_CAM:
5108 				LINE_CHECK("powersavemode CAM");
5109 				break;
5110 			case IEEE80211_POWERSAVE_PSP:
5111 				LINE_CHECK("powersavemode PSP");
5112 				break;
5113 			case IEEE80211_POWERSAVE_PSP_CAM:
5114 				LINE_CHECK("powersavemode PSP-CAM");
5115 				break;
5116 			}
5117 			if (get80211val(ctx, IEEE80211_IOC_POWERSAVESLEEP, &val) != -1)
5118 				LINE_CHECK("powersavesleep %d", val);
5119 		}
5120 	}
5121 
5122 	if (get80211val(ctx, IEEE80211_IOC_TXPOWER, &val) != -1) {
5123 		if (val & 1)
5124 			LINE_CHECK("txpower %d.5", val/2);
5125 		else
5126 			LINE_CHECK("txpower %d", val/2);
5127 	}
5128 	if (verbose) {
5129 		if (get80211val(ctx, IEEE80211_IOC_TXPOWMAX, &val) != -1)
5130 			LINE_CHECK("txpowmax %.1f", val/2.);
5131 	}
5132 
5133 	if (get80211val(ctx, IEEE80211_IOC_DOTD, &val) != -1) {
5134 		if (val)
5135 			LINE_CHECK("dotd");
5136 		else if (verbose)
5137 			LINE_CHECK("-dotd");
5138 	}
5139 
5140 	if (get80211val(ctx, IEEE80211_IOC_RTSTHRESHOLD, &val) != -1) {
5141 		if (val != IEEE80211_RTS_MAX || verbose)
5142 			LINE_CHECK("rtsthreshold %d", val);
5143 	}
5144 
5145 	if (get80211val(ctx, IEEE80211_IOC_FRAGTHRESHOLD, &val) != -1) {
5146 		if (val != IEEE80211_FRAG_MAX || verbose)
5147 			LINE_CHECK("fragthreshold %d", val);
5148 	}
5149 	if (opmode == IEEE80211_M_STA || verbose) {
5150 		if (get80211val(ctx, IEEE80211_IOC_BMISSTHRESHOLD, &val) != -1) {
5151 			if (val != IEEE80211_HWBMISS_MAX || verbose)
5152 				LINE_CHECK("bmiss %d", val);
5153 		}
5154 	}
5155 
5156 	if (!verbose) {
5157 		gettxparams(ctx);
5158 		tp = &txparams.params[chan2mode(c)];
5159 		printrate("ucastrate", tp->ucastrate,
5160 		    IEEE80211_FIXED_RATE_NONE, IEEE80211_FIXED_RATE_NONE);
5161 		printrate("mcastrate", tp->mcastrate, 2*1,
5162 		    IEEE80211_RATE_MCS|0);
5163 		printrate("mgmtrate", tp->mgmtrate, 2*1,
5164 		    IEEE80211_RATE_MCS|0);
5165 		if (tp->maxretry != 6)		/* XXX */
5166 			LINE_CHECK("maxretry %d", tp->maxretry);
5167 	} else {
5168 		LINE_BREAK();
5169 		list_txparams(ctx);
5170 	}
5171 
5172 	bgscaninterval = -1;
5173 	(void) get80211val(ctx, IEEE80211_IOC_BGSCAN_INTERVAL, &bgscaninterval);
5174 
5175 	if (get80211val(ctx, IEEE80211_IOC_SCANVALID, &val) != -1) {
5176 		if (val != bgscaninterval || verbose)
5177 			LINE_CHECK("scanvalid %u", val);
5178 	}
5179 
5180 	bgscan = 0;
5181 	if (get80211val(ctx, IEEE80211_IOC_BGSCAN, &bgscan) != -1) {
5182 		if (bgscan)
5183 			LINE_CHECK("bgscan");
5184 		else if (verbose)
5185 			LINE_CHECK("-bgscan");
5186 	}
5187 	if (bgscan || verbose) {
5188 		if (bgscaninterval != -1)
5189 			LINE_CHECK("bgscanintvl %u", bgscaninterval);
5190 		if (get80211val(ctx, IEEE80211_IOC_BGSCAN_IDLE, &val) != -1)
5191 			LINE_CHECK("bgscanidle %u", val);
5192 		if (!verbose) {
5193 			getroam(ctx);
5194 			rp = &roamparams.params[chan2mode(c)];
5195 			if (rp->rssi & 1)
5196 				LINE_CHECK("roam:rssi %u.5", rp->rssi/2);
5197 			else
5198 				LINE_CHECK("roam:rssi %u", rp->rssi/2);
5199 			LINE_CHECK("roam:rate %s%u",
5200 			    (rp->rate & IEEE80211_RATE_MCS) ? "MCS " : "",
5201 			    get_rate_value(rp->rate));
5202 		} else {
5203 			LINE_BREAK();
5204 			list_roam(ctx);
5205 			LINE_BREAK();
5206 		}
5207 	}
5208 
5209 	if (IEEE80211_IS_CHAN_ANYG(c) || verbose) {
5210 		if (get80211val(ctx, IEEE80211_IOC_PUREG, &val) != -1) {
5211 			if (val)
5212 				LINE_CHECK("pureg");
5213 			else if (verbose)
5214 				LINE_CHECK("-pureg");
5215 		}
5216 		if (get80211val(ctx, IEEE80211_IOC_PROTMODE, &val) != -1) {
5217 			switch (val) {
5218 			case IEEE80211_PROTMODE_OFF:
5219 				LINE_CHECK("protmode OFF");
5220 				break;
5221 			case IEEE80211_PROTMODE_CTS:
5222 				LINE_CHECK("protmode CTS");
5223 				break;
5224 			case IEEE80211_PROTMODE_RTSCTS:
5225 				LINE_CHECK("protmode RTSCTS");
5226 				break;
5227 			default:
5228 				LINE_CHECK("protmode UNKNOWN (0x%x)", val);
5229 				break;
5230 			}
5231 		}
5232 	}
5233 
5234 	if (IEEE80211_IS_CHAN_HT(c) || verbose) {
5235 		gethtconf(ctx);
5236 		switch (htconf & 3) {
5237 		case 0:
5238 		case 2:
5239 			LINE_CHECK("-ht");
5240 			break;
5241 		case 1:
5242 			LINE_CHECK("ht20");
5243 			break;
5244 		case 3:
5245 			if (verbose)
5246 				LINE_CHECK("ht");
5247 			break;
5248 		}
5249 		if (get80211val(ctx, IEEE80211_IOC_HTCOMPAT, &val) != -1) {
5250 			if (!val)
5251 				LINE_CHECK("-htcompat");
5252 			else if (verbose)
5253 				LINE_CHECK("htcompat");
5254 		}
5255 		if (get80211val(ctx, IEEE80211_IOC_AMPDU, &val) != -1) {
5256 			switch (val) {
5257 			case 0:
5258 				LINE_CHECK("-ampdu");
5259 				break;
5260 			case 1:
5261 				LINE_CHECK("ampdutx -ampdurx");
5262 				break;
5263 			case 2:
5264 				LINE_CHECK("-ampdutx ampdurx");
5265 				break;
5266 			case 3:
5267 				if (verbose)
5268 					LINE_CHECK("ampdu");
5269 				break;
5270 			}
5271 		}
5272 		/* XXX 11ac density/size is different */
5273 		if (get80211val(ctx, IEEE80211_IOC_AMPDU_LIMIT, &val) != -1) {
5274 			switch (val) {
5275 			case IEEE80211_HTCAP_MAXRXAMPDU_8K:
5276 				LINE_CHECK("ampdulimit 8k");
5277 				break;
5278 			case IEEE80211_HTCAP_MAXRXAMPDU_16K:
5279 				LINE_CHECK("ampdulimit 16k");
5280 				break;
5281 			case IEEE80211_HTCAP_MAXRXAMPDU_32K:
5282 				LINE_CHECK("ampdulimit 32k");
5283 				break;
5284 			case IEEE80211_HTCAP_MAXRXAMPDU_64K:
5285 				LINE_CHECK("ampdulimit 64k");
5286 				break;
5287 			}
5288 		}
5289 		/* XXX 11ac density/size is different */
5290 		if (get80211val(ctx, IEEE80211_IOC_AMPDU_DENSITY, &val) != -1) {
5291 			switch (val) {
5292 			case IEEE80211_HTCAP_MPDUDENSITY_NA:
5293 				if (verbose)
5294 					LINE_CHECK("ampdudensity NA");
5295 				break;
5296 			case IEEE80211_HTCAP_MPDUDENSITY_025:
5297 				LINE_CHECK("ampdudensity .25");
5298 				break;
5299 			case IEEE80211_HTCAP_MPDUDENSITY_05:
5300 				LINE_CHECK("ampdudensity .5");
5301 				break;
5302 			case IEEE80211_HTCAP_MPDUDENSITY_1:
5303 				LINE_CHECK("ampdudensity 1");
5304 				break;
5305 			case IEEE80211_HTCAP_MPDUDENSITY_2:
5306 				LINE_CHECK("ampdudensity 2");
5307 				break;
5308 			case IEEE80211_HTCAP_MPDUDENSITY_4:
5309 				LINE_CHECK("ampdudensity 4");
5310 				break;
5311 			case IEEE80211_HTCAP_MPDUDENSITY_8:
5312 				LINE_CHECK("ampdudensity 8");
5313 				break;
5314 			case IEEE80211_HTCAP_MPDUDENSITY_16:
5315 				LINE_CHECK("ampdudensity 16");
5316 				break;
5317 			}
5318 		}
5319 		if (get80211val(ctx, IEEE80211_IOC_AMSDU, &val) != -1) {
5320 			switch (val) {
5321 			case 0:
5322 				LINE_CHECK("-amsdu");
5323 				break;
5324 			case 1:
5325 				LINE_CHECK("amsdutx -amsdurx");
5326 				break;
5327 			case 2:
5328 				LINE_CHECK("-amsdutx amsdurx");
5329 				break;
5330 			case 3:
5331 				if (verbose)
5332 					LINE_CHECK("amsdu");
5333 				break;
5334 			}
5335 		}
5336 		/* XXX amsdu limit */
5337 		if (get80211val(ctx, IEEE80211_IOC_SHORTGI, &val) != -1) {
5338 			if (val)
5339 				LINE_CHECK("shortgi");
5340 			else if (verbose)
5341 				LINE_CHECK("-shortgi");
5342 		}
5343 		if (get80211val(ctx, IEEE80211_IOC_HTPROTMODE, &val) != -1) {
5344 			if (val == IEEE80211_PROTMODE_OFF)
5345 				LINE_CHECK("htprotmode OFF");
5346 			else if (val != IEEE80211_PROTMODE_RTSCTS)
5347 				LINE_CHECK("htprotmode UNKNOWN (0x%x)", val);
5348 			else if (verbose)
5349 				LINE_CHECK("htprotmode RTSCTS");
5350 		}
5351 		if (get80211val(ctx, IEEE80211_IOC_PUREN, &val) != -1) {
5352 			if (val)
5353 				LINE_CHECK("puren");
5354 			else if (verbose)
5355 				LINE_CHECK("-puren");
5356 		}
5357 		if (get80211val(ctx, IEEE80211_IOC_SMPS, &val) != -1) {
5358 			if (val == IEEE80211_HTCAP_SMPS_DYNAMIC)
5359 				LINE_CHECK("smpsdyn");
5360 			else if (val == IEEE80211_HTCAP_SMPS_ENA)
5361 				LINE_CHECK("smps");
5362 			else if (verbose)
5363 				LINE_CHECK("-smps");
5364 		}
5365 		if (get80211val(ctx, IEEE80211_IOC_RIFS, &val) != -1) {
5366 			if (val)
5367 				LINE_CHECK("rifs");
5368 			else if (verbose)
5369 				LINE_CHECK("-rifs");
5370 		}
5371 
5372 		/* XXX VHT STBC? */
5373 		if (get80211val(ctx, IEEE80211_IOC_STBC, &val) != -1) {
5374 			switch (val) {
5375 			case 0:
5376 				LINE_CHECK("-stbc");
5377 				break;
5378 			case 1:
5379 				LINE_CHECK("stbctx -stbcrx");
5380 				break;
5381 			case 2:
5382 				LINE_CHECK("-stbctx stbcrx");
5383 				break;
5384 			case 3:
5385 				if (verbose)
5386 					LINE_CHECK("stbc");
5387 				break;
5388 			}
5389 		}
5390 		if (get80211val(ctx, IEEE80211_IOC_LDPC, &val) != -1) {
5391 			switch (val) {
5392 			case 0:
5393 				LINE_CHECK("-ldpc");
5394 				break;
5395 			case 1:
5396 				LINE_CHECK("ldpctx -ldpcrx");
5397 				break;
5398 			case 2:
5399 				LINE_CHECK("-ldpctx ldpcrx");
5400 				break;
5401 			case 3:
5402 				if (verbose)
5403 					LINE_CHECK("ldpc");
5404 				break;
5405 			}
5406 		}
5407 		if (get80211val(ctx, IEEE80211_IOC_UAPSD, &val) != -1) {
5408 			switch (val) {
5409 			case 0:
5410 				LINE_CHECK("-uapsd");
5411 				break;
5412 			case 1:
5413 				LINE_CHECK("uapsd");
5414 				break;
5415 			}
5416 		}
5417 	}
5418 
5419 	if (IEEE80211_IS_CHAN_VHT(c) || verbose) {
5420 		getvhtconf(ctx);
5421 		if (vhtconf & IEEE80211_FVHT_VHT) {
5422 			LINE_CHECK("vht");
5423 
5424 			if (vhtconf & IEEE80211_FVHT_USEVHT40)
5425 				LINE_CHECK("vht40");
5426 			else
5427 				LINE_CHECK("-vht40");
5428 			if (vhtconf & IEEE80211_FVHT_USEVHT80)
5429 				LINE_CHECK("vht80");
5430 			else
5431 				LINE_CHECK("-vht80");
5432 			if (vhtconf & IEEE80211_FVHT_USEVHT160)
5433 				LINE_CHECK("vht160");
5434 			else
5435 				LINE_CHECK("-vht160");
5436 			if (vhtconf & IEEE80211_FVHT_USEVHT80P80)
5437 				LINE_CHECK("vht80p80");
5438 			else
5439 				LINE_CHECK("-vht80p80");
5440 		} else if (verbose)
5441 			LINE_CHECK("-vht");
5442 	}
5443 
5444 	if (get80211val(ctx, IEEE80211_IOC_WME, &wme) != -1) {
5445 		if (wme)
5446 			LINE_CHECK("wme");
5447 		else if (verbose)
5448 			LINE_CHECK("-wme");
5449 	} else
5450 		wme = 0;
5451 
5452 	if (get80211val(ctx, IEEE80211_IOC_BURST, &val) != -1) {
5453 		if (val)
5454 			LINE_CHECK("burst");
5455 		else if (verbose)
5456 			LINE_CHECK("-burst");
5457 	}
5458 
5459 	if (get80211val(ctx, IEEE80211_IOC_FF, &val) != -1) {
5460 		if (val)
5461 			LINE_CHECK("ff");
5462 		else if (verbose)
5463 			LINE_CHECK("-ff");
5464 	}
5465 	if (get80211val(ctx, IEEE80211_IOC_TURBOP, &val) != -1) {
5466 		if (val)
5467 			LINE_CHECK("dturbo");
5468 		else if (verbose)
5469 			LINE_CHECK("-dturbo");
5470 	}
5471 	if (get80211val(ctx, IEEE80211_IOC_DWDS, &val) != -1) {
5472 		if (val)
5473 			LINE_CHECK("dwds");
5474 		else if (verbose)
5475 			LINE_CHECK("-dwds");
5476 	}
5477 
5478 	if (opmode == IEEE80211_M_HOSTAP) {
5479 		if (get80211val(ctx, IEEE80211_IOC_HIDESSID, &val) != -1) {
5480 			if (val)
5481 				LINE_CHECK("hidessid");
5482 			else if (verbose)
5483 				LINE_CHECK("-hidessid");
5484 		}
5485 		if (get80211val(ctx, IEEE80211_IOC_APBRIDGE, &val) != -1) {
5486 			if (!val)
5487 				LINE_CHECK("-apbridge");
5488 			else if (verbose)
5489 				LINE_CHECK("apbridge");
5490 		}
5491 		if (get80211val(ctx, IEEE80211_IOC_DTIM_PERIOD, &val) != -1)
5492 			LINE_CHECK("dtimperiod %u", val);
5493 
5494 		if (get80211val(ctx, IEEE80211_IOC_DOTH, &val) != -1) {
5495 			if (!val)
5496 				LINE_CHECK("-doth");
5497 			else if (verbose)
5498 				LINE_CHECK("doth");
5499 		}
5500 		if (get80211val(ctx, IEEE80211_IOC_DFS, &val) != -1) {
5501 			if (!val)
5502 				LINE_CHECK("-dfs");
5503 			else if (verbose)
5504 				LINE_CHECK("dfs");
5505 		}
5506 		if (get80211val(ctx, IEEE80211_IOC_INACTIVITY, &val) != -1) {
5507 			if (!val)
5508 				LINE_CHECK("-inact");
5509 			else if (verbose)
5510 				LINE_CHECK("inact");
5511 		}
5512 	} else {
5513 		if (get80211val(ctx, IEEE80211_IOC_ROAMING, &val) != -1) {
5514 			if (val != IEEE80211_ROAMING_AUTO || verbose) {
5515 				switch (val) {
5516 				case IEEE80211_ROAMING_DEVICE:
5517 					LINE_CHECK("roaming DEVICE");
5518 					break;
5519 				case IEEE80211_ROAMING_AUTO:
5520 					LINE_CHECK("roaming AUTO");
5521 					break;
5522 				case IEEE80211_ROAMING_MANUAL:
5523 					LINE_CHECK("roaming MANUAL");
5524 					break;
5525 				default:
5526 					LINE_CHECK("roaming UNKNOWN (0x%x)",
5527 						val);
5528 					break;
5529 				}
5530 			}
5531 		}
5532 	}
5533 
5534 	if (opmode == IEEE80211_M_AHDEMO) {
5535 		if (get80211val(ctx, IEEE80211_IOC_TDMA_SLOT, &val) != -1)
5536 			LINE_CHECK("tdmaslot %u", val);
5537 		if (get80211val(ctx, IEEE80211_IOC_TDMA_SLOTCNT, &val) != -1)
5538 			LINE_CHECK("tdmaslotcnt %u", val);
5539 		if (get80211val(ctx, IEEE80211_IOC_TDMA_SLOTLEN, &val) != -1)
5540 			LINE_CHECK("tdmaslotlen %u", val);
5541 		if (get80211val(ctx, IEEE80211_IOC_TDMA_BINTERVAL, &val) != -1)
5542 			LINE_CHECK("tdmabintval %u", val);
5543 	} else if (get80211val(ctx, IEEE80211_IOC_BEACON_INTERVAL, &val) != -1) {
5544 		/* XXX default define not visible */
5545 		if (val != 100 || verbose)
5546 			LINE_CHECK("bintval %u", val);
5547 	}
5548 
5549 	if (wme && verbose) {
5550 		LINE_BREAK();
5551 		list_wme(ctx);
5552 	}
5553 
5554 	if (opmode == IEEE80211_M_MBSS) {
5555 		if (get80211val(ctx, IEEE80211_IOC_MESH_TTL, &val) != -1) {
5556 			LINE_CHECK("meshttl %u", val);
5557 		}
5558 		if (get80211val(ctx, IEEE80211_IOC_MESH_AP, &val) != -1) {
5559 			if (val)
5560 				LINE_CHECK("meshpeering");
5561 			else
5562 				LINE_CHECK("-meshpeering");
5563 		}
5564 		if (get80211val(ctx, IEEE80211_IOC_MESH_FWRD, &val) != -1) {
5565 			if (val)
5566 				LINE_CHECK("meshforward");
5567 			else
5568 				LINE_CHECK("-meshforward");
5569 		}
5570 		if (get80211val(ctx, IEEE80211_IOC_MESH_GATE, &val) != -1) {
5571 			if (val)
5572 				LINE_CHECK("meshgate");
5573 			else
5574 				LINE_CHECK("-meshgate");
5575 		}
5576 		if (get80211len(ctx, IEEE80211_IOC_MESH_PR_METRIC, data, 12,
5577 		    &len) != -1) {
5578 			data[len] = '\0';
5579 			LINE_CHECK("meshmetric %s", data);
5580 		}
5581 		if (get80211len(ctx, IEEE80211_IOC_MESH_PR_PATH, data, 12,
5582 		    &len) != -1) {
5583 			data[len] = '\0';
5584 			LINE_CHECK("meshpath %s", data);
5585 		}
5586 		if (get80211val(ctx, IEEE80211_IOC_HWMP_ROOTMODE, &val) != -1) {
5587 			switch (val) {
5588 			case IEEE80211_HWMP_ROOTMODE_DISABLED:
5589 				LINE_CHECK("hwmprootmode DISABLED");
5590 				break;
5591 			case IEEE80211_HWMP_ROOTMODE_NORMAL:
5592 				LINE_CHECK("hwmprootmode NORMAL");
5593 				break;
5594 			case IEEE80211_HWMP_ROOTMODE_PROACTIVE:
5595 				LINE_CHECK("hwmprootmode PROACTIVE");
5596 				break;
5597 			case IEEE80211_HWMP_ROOTMODE_RANN:
5598 				LINE_CHECK("hwmprootmode RANN");
5599 				break;
5600 			default:
5601 				LINE_CHECK("hwmprootmode UNKNOWN(%d)", val);
5602 				break;
5603 			}
5604 		}
5605 		if (get80211val(ctx, IEEE80211_IOC_HWMP_MAXHOPS, &val) != -1) {
5606 			LINE_CHECK("hwmpmaxhops %u", val);
5607 		}
5608 	}
5609 
5610 	LINE_BREAK();
5611 
5612 	if (getdevicename(ctx, data, sizeof(data), &len) < 0)
5613 		return;
5614 	LINE_CHECK("parent interface: %s", data);
5615 
5616 	LINE_BREAK();
5617 }
5618 
5619 static int
get80211(if_ctx * ctx,int type,void * data,int len)5620 get80211(if_ctx *ctx, int type, void *data, int len)
5621 {
5622 
5623 	return (lib80211_get80211(ctx->io_s, ctx->ifname, type, data, len));
5624 }
5625 
5626 static int
get80211len(if_ctx * ctx,int type,void * data,int len,int * plen)5627 get80211len(if_ctx *ctx, int type, void *data, int len, int *plen)
5628 {
5629 
5630 	return (lib80211_get80211len(ctx->io_s, ctx->ifname, type, data, len, plen));
5631 }
5632 
5633 static int
get80211val(if_ctx * ctx,int type,int * val)5634 get80211val(if_ctx *ctx, int type, int *val)
5635 {
5636 
5637 	return (lib80211_get80211val(ctx->io_s, ctx->ifname, type, val));
5638 }
5639 
5640 static void
set80211(if_ctx * ctx,int type,int val,int len,void * data)5641 set80211(if_ctx *ctx, int type, int val, int len, void *data)
5642 {
5643 	int ret;
5644 
5645 	ret = lib80211_set80211(ctx->io_s, ctx->ifname, type, val, len, data);
5646 	if (ret < 0)
5647 		err(1, "SIOCS80211");
5648 }
5649 
5650 static const char *
get_string(const char * val,const char * sep,u_int8_t * buf,int * lenp)5651 get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
5652 {
5653 	int len;
5654 	int hexstr;
5655 	u_int8_t *p;
5656 
5657 	len = *lenp;
5658 	p = buf;
5659 	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
5660 	if (hexstr)
5661 		val += 2;
5662 	for (;;) {
5663 		if (*val == '\0')
5664 			break;
5665 		if (sep != NULL && strchr(sep, *val) != NULL) {
5666 			val++;
5667 			break;
5668 		}
5669 		if (hexstr) {
5670 			if (!isxdigit((u_char)val[0])) {
5671 				warnx("bad hexadecimal digits");
5672 				return NULL;
5673 			}
5674 			if (!isxdigit((u_char)val[1])) {
5675 				warnx("odd count hexadecimal digits");
5676 				return NULL;
5677 			}
5678 		}
5679 		if (p >= buf + len) {
5680 			if (hexstr)
5681 				warnx("hexadecimal digits too long");
5682 			else
5683 				warnx("string too long");
5684 			return NULL;
5685 		}
5686 		if (hexstr) {
5687 #define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
5688 			*p++ = (tohex((u_char)val[0]) << 4) |
5689 			    tohex((u_char)val[1]);
5690 #undef tohex
5691 			val += 2;
5692 		} else
5693 			*p++ = *val++;
5694 	}
5695 	len = p - buf;
5696 	/* The string "-" is treated as the empty string. */
5697 	if (!hexstr && len == 1 && buf[0] == '-') {
5698 		len = 0;
5699 		memset(buf, 0, *lenp);
5700 	} else if (len < *lenp)
5701 		memset(p, 0, *lenp - len);
5702 	*lenp = len;
5703 	return val;
5704 }
5705 
5706 static void
print_string(const u_int8_t * buf,int len)5707 print_string(const u_int8_t *buf, int len)
5708 {
5709 	int i;
5710 	int hasspc;
5711 	int utf8;
5712 
5713 	i = 0;
5714 	hasspc = 0;
5715 
5716 	setlocale(LC_CTYPE, "");
5717 	utf8 = strncmp("UTF-8", nl_langinfo(CODESET), 5) == 0;
5718 
5719 	for (; i < len; i++) {
5720 		if (!isprint(buf[i]) && buf[i] != '\0' && !utf8)
5721 			break;
5722 		if (isspace(buf[i]))
5723 			hasspc++;
5724 	}
5725 	if (i == len || utf8) {
5726 		if (hasspc || len == 0 || buf[0] == '\0')
5727 			printf("\"%.*s\"", len, buf);
5728 		else
5729 			printf("%.*s", len, buf);
5730 	} else {
5731 		printf("0x");
5732 		for (i = 0; i < len; i++)
5733 			printf("%02x", buf[i]);
5734 	}
5735 }
5736 
5737 static void
setdefregdomain(if_ctx * ctx)5738 setdefregdomain(if_ctx *ctx)
5739 {
5740 	struct regdata *rdp = getregdata();
5741 	const struct regdomain *rd;
5742 
5743 	/* Check if regdomain/country was already set by a previous call. */
5744 	/* XXX is it possible? */
5745 	if (regdomain.regdomain != 0 ||
5746 	    regdomain.country != CTRY_DEFAULT)
5747 		return;
5748 
5749 	getregdomain(ctx);
5750 
5751 	/* Check if it was already set by the driver. */
5752 	if (regdomain.regdomain != 0 ||
5753 	    regdomain.country != CTRY_DEFAULT)
5754 		return;
5755 
5756 	/* Set FCC/US as default. */
5757 	rd = lib80211_regdomain_findbysku(rdp, SKU_FCC);
5758 	if (rd == NULL)
5759 		errx(1, "FCC regdomain was not found");
5760 
5761 	regdomain.regdomain = rd->sku;
5762 	if (rd->cc != NULL)
5763 		defaultcountry(rd);
5764 
5765 	/* Send changes to net80211. */
5766 	setregdomain_cb(ctx, &regdomain);
5767 
5768 	/* Cleanup (so it can be overridden by subsequent parameters). */
5769 	regdomain.regdomain = 0;
5770 	regdomain.country = CTRY_DEFAULT;
5771 	regdomain.isocc[0] = 0;
5772 	regdomain.isocc[1] = 0;
5773 }
5774 
5775 /*
5776  * Virtual AP cloning support.
5777  */
5778 static struct ieee80211_clone_params params = {
5779 	.icp_opmode	= IEEE80211_M_STA,	/* default to station mode */
5780 };
5781 
5782 static void
wlan_create(if_ctx * ctx,struct ifreq * ifr)5783 wlan_create(if_ctx *ctx, struct ifreq *ifr)
5784 {
5785 	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
5786 
5787 	if (params.icp_parent[0] == '\0')
5788 		errx(1, "must specify a parent device (wlandev) when creating "
5789 		    "a wlan device");
5790 	if (params.icp_opmode == IEEE80211_M_WDS &&
5791 	    memcmp(params.icp_bssid, zerobssid, sizeof(zerobssid)) == 0)
5792 		errx(1, "no bssid specified for WDS (use wlanbssid)");
5793 	ifr->ifr_data = (caddr_t) &params;
5794 	ifcreate_ioctl(ctx, ifr);
5795 
5796 	setdefregdomain(ctx);
5797 }
5798 
5799 static void
set80211clone_wlandev(if_ctx * ctx __unused,const char * arg,int dummy __unused)5800 set80211clone_wlandev(if_ctx *ctx __unused, const char *arg, int dummy __unused)
5801 {
5802 	strlcpy(params.icp_parent, arg, IFNAMSIZ);
5803 }
5804 
5805 static void
set80211clone_wlanbssid(if_ctx * ctx __unused,const char * arg,int dummy __unused)5806 set80211clone_wlanbssid(if_ctx *ctx __unused, const char *arg, int dummy __unused)
5807 {
5808 	const struct ether_addr *ea;
5809 
5810 	ea = ether_aton(arg);
5811 	if (ea == NULL)
5812 		errx(1, "%s: cannot parse bssid", arg);
5813 	memcpy(params.icp_bssid, ea->octet, IEEE80211_ADDR_LEN);
5814 }
5815 
5816 static void
set80211clone_wlanaddr(if_ctx * ctx __unused,const char * arg,int dummy __unused)5817 set80211clone_wlanaddr(if_ctx *ctx __unused, const char *arg, int dummy __unused)
5818 {
5819 	const struct ether_addr *ea;
5820 
5821 	ea = ether_aton(arg);
5822 	if (ea == NULL)
5823 		errx(1, "%s: cannot parse address", arg);
5824 	memcpy(params.icp_macaddr, ea->octet, IEEE80211_ADDR_LEN);
5825 	params.icp_flags |= IEEE80211_CLONE_MACADDR;
5826 }
5827 
5828 static void
set80211clone_wlanmode(if_ctx * ctx,const char * arg,int dummy __unused)5829 set80211clone_wlanmode(if_ctx *ctx, const char *arg, int dummy __unused)
5830 {
5831 #define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
5832 	if (iseq(arg, "sta"))
5833 		params.icp_opmode = IEEE80211_M_STA;
5834 	else if (iseq(arg, "ahdemo") || iseq(arg, "adhoc-demo"))
5835 		params.icp_opmode = IEEE80211_M_AHDEMO;
5836 	else if (iseq(arg, "ibss") || iseq(arg, "adhoc"))
5837 		params.icp_opmode = IEEE80211_M_IBSS;
5838 	else if (iseq(arg, "ap") || iseq(arg, "host"))
5839 		params.icp_opmode = IEEE80211_M_HOSTAP;
5840 	else if (iseq(arg, "wds"))
5841 		params.icp_opmode = IEEE80211_M_WDS;
5842 	else if (iseq(arg, "monitor"))
5843 		params.icp_opmode = IEEE80211_M_MONITOR;
5844 	else if (iseq(arg, "tdma")) {
5845 		params.icp_opmode = IEEE80211_M_AHDEMO;
5846 		params.icp_flags |= IEEE80211_CLONE_TDMA;
5847 	} else if (iseq(arg, "mesh") || iseq(arg, "mp")) /* mesh point */
5848 		params.icp_opmode = IEEE80211_M_MBSS;
5849 	else
5850 		errx(1, "Don't know to create %s for %s", arg, ctx->ifname);
5851 #undef iseq
5852 }
5853 
5854 static void
set80211clone_beacons(if_ctx * ctx __unused,const char * val __unused,int d)5855 set80211clone_beacons(if_ctx *ctx __unused, const char *val __unused, int d)
5856 {
5857 	/* NB: inverted sense */
5858 	if (d)
5859 		params.icp_flags &= ~IEEE80211_CLONE_NOBEACONS;
5860 	else
5861 		params.icp_flags |= IEEE80211_CLONE_NOBEACONS;
5862 }
5863 
5864 static void
set80211clone_bssid(if_ctx * ctx __unused,const char * val __unused,int d)5865 set80211clone_bssid(if_ctx *ctx __unused, const char *val __unused, int d)
5866 {
5867 	if (d)
5868 		params.icp_flags |= IEEE80211_CLONE_BSSID;
5869 	else
5870 		params.icp_flags &= ~IEEE80211_CLONE_BSSID;
5871 }
5872 
5873 static void
set80211clone_wdslegacy(if_ctx * ctx __unused,const char * val __unused,int d)5874 set80211clone_wdslegacy(if_ctx *ctx __unused, const char *val __unused, int d)
5875 {
5876 	if (d)
5877 		params.icp_flags |= IEEE80211_CLONE_WDSLEGACY;
5878 	else
5879 		params.icp_flags &= ~IEEE80211_CLONE_WDSLEGACY;
5880 }
5881 
5882 static struct cmd ieee80211_cmds[] = {
5883 	DEF_CMD_ARG("ssid",		set80211ssid),
5884 	DEF_CMD_ARG("nwid",		set80211ssid),
5885 	DEF_CMD_ARG("meshid",		set80211meshid),
5886 	DEF_CMD_ARG("stationname",	set80211stationname),
5887 	DEF_CMD_ARG("station",		set80211stationname),	/* BSD/OS */
5888 	DEF_CMD_ARG("channel",		set80211channel),
5889 	DEF_CMD_ARG("authmode",		set80211authmode),
5890 	DEF_CMD_ARG("powersavemode",	set80211powersavemode),
5891 	DEF_CMD("powersave",	1,	set80211powersave),
5892 	DEF_CMD("-powersave",	0,	set80211powersave),
5893 	DEF_CMD_ARG("powersavesleep", 	set80211powersavesleep),
5894 	DEF_CMD_ARG("wepmode",		set80211wepmode),
5895 	DEF_CMD("wep",		1,	set80211wep),
5896 	DEF_CMD("-wep",		0,	set80211wep),
5897 	DEF_CMD_ARG("deftxkey",		set80211weptxkey),
5898 	DEF_CMD_ARG("weptxkey",		set80211weptxkey),
5899 	DEF_CMD_ARG("wepkey",		set80211wepkey),
5900 	DEF_CMD_ARG("nwkey",		set80211nwkey),		/* NetBSD */
5901 	DEF_CMD("-nwkey",	0,	set80211wep),		/* NetBSD */
5902 	DEF_CMD_ARG("rtsthreshold",	set80211rtsthreshold),
5903 	DEF_CMD_ARG("protmode",		set80211protmode),
5904 	DEF_CMD_ARG("txpower",		set80211txpower),
5905 	DEF_CMD_ARG("roaming",		set80211roaming),
5906 	DEF_CMD("wme",		1,	set80211wme),
5907 	DEF_CMD("-wme",		0,	set80211wme),
5908 	DEF_CMD("wmm",		1,	set80211wme),
5909 	DEF_CMD("-wmm",		0,	set80211wme),
5910 	DEF_CMD("hidessid",	1,	set80211hidessid),
5911 	DEF_CMD("-hidessid",	0,	set80211hidessid),
5912 	DEF_CMD("apbridge",	1,	set80211apbridge),
5913 	DEF_CMD("-apbridge",	0,	set80211apbridge),
5914 	DEF_CMD_ARG("chanlist",		set80211chanlist),
5915 	DEF_CMD_ARG("bssid",		set80211bssid),
5916 	DEF_CMD_ARG("ap",		set80211bssid),
5917 	DEF_CMD("scan",	0,		set80211scan),
5918 	DEF_CMD_ARG("list",		set80211list),
5919 	DEF_CMD_ARG2("cwmin",		set80211cwmin),
5920 	DEF_CMD_ARG2("cwmax",		set80211cwmax),
5921 	DEF_CMD_ARG2("aifs",		set80211aifs),
5922 	DEF_CMD_ARG2("txoplimit",	set80211txoplimit),
5923 	DEF_CMD_ARG("acm",		set80211acm),
5924 	DEF_CMD_ARG("-acm",		set80211noacm),
5925 	DEF_CMD_ARG("ack",		set80211ackpolicy),
5926 	DEF_CMD_ARG("-ack",		set80211noackpolicy),
5927 	DEF_CMD_ARG2("bss:cwmin",	set80211bsscwmin),
5928 	DEF_CMD_ARG2("bss:cwmax",	set80211bsscwmax),
5929 	DEF_CMD_ARG2("bss:aifs",	set80211bssaifs),
5930 	DEF_CMD_ARG2("bss:txoplimit",	set80211bsstxoplimit),
5931 	DEF_CMD_ARG("dtimperiod",	set80211dtimperiod),
5932 	DEF_CMD_ARG("bintval",		set80211bintval),
5933 	DEF_CMD("mac:open",	IEEE80211_MACCMD_POLICY_OPEN,	set80211maccmd),
5934 	DEF_CMD("mac:allow",	IEEE80211_MACCMD_POLICY_ALLOW,	set80211maccmd),
5935 	DEF_CMD("mac:deny",	IEEE80211_MACCMD_POLICY_DENY,	set80211maccmd),
5936 	DEF_CMD("mac:radius",	IEEE80211_MACCMD_POLICY_RADIUS,	set80211maccmd),
5937 	DEF_CMD("mac:flush",	IEEE80211_MACCMD_FLUSH,		set80211maccmd),
5938 	DEF_CMD("mac:detach",	IEEE80211_MACCMD_DETACH,	set80211maccmd),
5939 	DEF_CMD_ARG("mac:add",		set80211addmac),
5940 	DEF_CMD_ARG("mac:del",		set80211delmac),
5941 	DEF_CMD_ARG("mac:kick",		set80211kickmac),
5942 	DEF_CMD("pureg",	1,	set80211pureg),
5943 	DEF_CMD("-pureg",	0,	set80211pureg),
5944 	DEF_CMD("ff",		1,	set80211fastframes),
5945 	DEF_CMD("-ff",		0,	set80211fastframes),
5946 	DEF_CMD("dturbo",	1,	set80211dturbo),
5947 	DEF_CMD("-dturbo",	0,	set80211dturbo),
5948 	DEF_CMD("bgscan",	1,	set80211bgscan),
5949 	DEF_CMD("-bgscan",	0,	set80211bgscan),
5950 	DEF_CMD_ARG("bgscanidle",	set80211bgscanidle),
5951 	DEF_CMD_ARG("bgscanintvl",	set80211bgscanintvl),
5952 	DEF_CMD_ARG("scanvalid",	set80211scanvalid),
5953 	DEF_CMD("quiet",	1,	set80211quiet),
5954 	DEF_CMD("-quiet",	0,	set80211quiet),
5955 	DEF_CMD_ARG("quiet_count",	set80211quietcount),
5956 	DEF_CMD_ARG("quiet_period",	set80211quietperiod),
5957 	DEF_CMD_ARG("quiet_duration",	set80211quietduration),
5958 	DEF_CMD_ARG("quiet_offset",	set80211quietoffset),
5959 	DEF_CMD_ARG("roam:rssi",	set80211roamrssi),
5960 	DEF_CMD_ARG("roam:rate",	set80211roamrate),
5961 	DEF_CMD_ARG("mcastrate",	set80211mcastrate),
5962 	DEF_CMD_ARG("ucastrate",	set80211ucastrate),
5963 	DEF_CMD_ARG("mgtrate",		set80211mgtrate),
5964 	DEF_CMD_ARG("mgmtrate",		set80211mgtrate),
5965 	DEF_CMD_ARG("maxretry",		set80211maxretry),
5966 	DEF_CMD_ARG("fragthreshold",	set80211fragthreshold),
5967 	DEF_CMD("burst",	1,	set80211burst),
5968 	DEF_CMD("-burst",	0,	set80211burst),
5969 	DEF_CMD_ARG("bmiss",		set80211bmissthreshold),
5970 	DEF_CMD_ARG("bmissthreshold",	set80211bmissthreshold),
5971 	DEF_CMD("shortgi",	1,	set80211shortgi),
5972 	DEF_CMD("-shortgi",	0,	set80211shortgi),
5973 	DEF_CMD("ampdurx",	2,	set80211ampdu),
5974 	DEF_CMD("-ampdurx",	-2,	set80211ampdu),
5975 	DEF_CMD("ampdutx",	1,	set80211ampdu),
5976 	DEF_CMD("-ampdutx",	-1,	set80211ampdu),
5977 	DEF_CMD("ampdu",	3,	set80211ampdu),		/* NB: tx+rx */
5978 	DEF_CMD("-ampdu",	-3,	set80211ampdu),
5979 	DEF_CMD_ARG("ampdulimit",	set80211ampdulimit),
5980 	DEF_CMD_ARG("ampdudensity",	set80211ampdudensity),
5981 	DEF_CMD("amsdurx",	2,	set80211amsdu),
5982 	DEF_CMD("-amsdurx",	-2,	set80211amsdu),
5983 	DEF_CMD("amsdutx",	1,	set80211amsdu),
5984 	DEF_CMD("-amsdutx",	-1,	set80211amsdu),
5985 	DEF_CMD("amsdu",	3,	set80211amsdu),		/* NB: tx+rx */
5986 	DEF_CMD("-amsdu",	-3,	set80211amsdu),
5987 	DEF_CMD_ARG("amsdulimit",	set80211amsdulimit),
5988 	DEF_CMD("stbcrx",	2,	set80211stbc),
5989 	DEF_CMD("-stbcrx",	-2,	set80211stbc),
5990 	DEF_CMD("stbctx",	1,	set80211stbc),
5991 	DEF_CMD("-stbctx",	-1,	set80211stbc),
5992 	DEF_CMD("stbc",		3,	set80211stbc),		/* NB: tx+rx */
5993 	DEF_CMD("-stbc",	-3,	set80211stbc),
5994 	DEF_CMD("ldpcrx",	2,	set80211ldpc),
5995 	DEF_CMD("-ldpcrx",	-2,	set80211ldpc),
5996 	DEF_CMD("ldpctx",	1,	set80211ldpc),
5997 	DEF_CMD("-ldpctx",	-1,	set80211ldpc),
5998 	DEF_CMD("ldpc",		3,	set80211ldpc),		/* NB: tx+rx */
5999 	DEF_CMD("-ldpc",	-3,	set80211ldpc),
6000 	DEF_CMD("uapsd",	1,	set80211uapsd),
6001 	DEF_CMD("-uapsd",	0,	set80211uapsd),
6002 	DEF_CMD("puren",	1,	set80211puren),
6003 	DEF_CMD("-puren",	0,	set80211puren),
6004 	DEF_CMD("doth",		1,	set80211doth),
6005 	DEF_CMD("-doth",	0,	set80211doth),
6006 	DEF_CMD("dfs",		1,	set80211dfs),
6007 	DEF_CMD("-dfs",		0,	set80211dfs),
6008 	DEF_CMD("htcompat",	1,	set80211htcompat),
6009 	DEF_CMD("-htcompat",	0,	set80211htcompat),
6010 	DEF_CMD("dwds",		1,	set80211dwds),
6011 	DEF_CMD("-dwds",	0,	set80211dwds),
6012 	DEF_CMD("inact",	1,	set80211inact),
6013 	DEF_CMD("-inact",	0,	set80211inact),
6014 	DEF_CMD("tsn",		1,	set80211tsn),
6015 	DEF_CMD("-tsn",		0,	set80211tsn),
6016 	DEF_CMD_ARG("regdomain",	set80211regdomain),
6017 	DEF_CMD_ARG("country",		set80211country),
6018 	DEF_CMD("indoor",	'I',	set80211location),
6019 	DEF_CMD("-indoor",	'O',	set80211location),
6020 	DEF_CMD("outdoor",	'O',	set80211location),
6021 	DEF_CMD("-outdoor",	'I',	set80211location),
6022 	DEF_CMD("anywhere",	' ',	set80211location),
6023 	DEF_CMD("ecm",		1,	set80211ecm),
6024 	DEF_CMD("-ecm",		0,	set80211ecm),
6025 	DEF_CMD("dotd",		1,	set80211dotd),
6026 	DEF_CMD("-dotd",	0,	set80211dotd),
6027 	DEF_CMD_ARG("htprotmode",	set80211htprotmode),
6028 	DEF_CMD("ht20",		1,	set80211htconf),
6029 	DEF_CMD("-ht20",	0,	set80211htconf),
6030 	DEF_CMD("ht40",		3,	set80211htconf),	/* NB: 20+40 */
6031 	DEF_CMD("-ht40",	0,	set80211htconf),
6032 	DEF_CMD("ht",		3,	set80211htconf),	/* NB: 20+40 */
6033 	DEF_CMD("-ht",		0,	set80211htconf),
6034 	DEF_CMD("vht",		IEEE80211_FVHT_VHT,		set80211vhtconf),
6035 	DEF_CMD("-vht",		-IEEE80211_FVHT_VHT,		set80211vhtconf),
6036 	DEF_CMD("vht40",	IEEE80211_FVHT_USEVHT40,	set80211vhtconf),
6037 	DEF_CMD("-vht40",	-IEEE80211_FVHT_USEVHT40,	set80211vhtconf),
6038 	DEF_CMD("vht80",	IEEE80211_FVHT_USEVHT80,	set80211vhtconf),
6039 	DEF_CMD("-vht80",	-IEEE80211_FVHT_USEVHT80,	set80211vhtconf),
6040 	DEF_CMD("vht160",	IEEE80211_FVHT_USEVHT160,	set80211vhtconf),
6041 	DEF_CMD("-vht160",	-IEEE80211_FVHT_USEVHT160,	set80211vhtconf),
6042 	DEF_CMD("vht80p80",	IEEE80211_FVHT_USEVHT80P80,	set80211vhtconf),
6043 	DEF_CMD("-vht80p80",	-IEEE80211_FVHT_USEVHT80P80,	set80211vhtconf),
6044 	DEF_CMD("vhtstbctx",	IEEE80211_FVHT_STBC_TX,		set80211vhtconf),
6045 	DEF_CMD("-vhtstbctx",	-IEEE80211_FVHT_STBC_TX,	set80211vhtconf),
6046 	DEF_CMD("vhtstbcrx",	IEEE80211_FVHT_STBC_RX,		set80211vhtconf),
6047 	DEF_CMD("-vhtstbcrx",	-IEEE80211_FVHT_STBC_RX,	set80211vhtconf),
6048 	DEF_CMD("vhtstbc",	(IEEE80211_FVHT_STBC_TX|IEEE80211_FVHT_STBC_RX),	set80211vhtconf),
6049 	DEF_CMD("-vhtstbc",	-(IEEE80211_FVHT_STBC_TX|IEEE80211_FVHT_STBC_RX),	set80211vhtconf),
6050 	DEF_CMD("rifs",		1,	set80211rifs),
6051 	DEF_CMD("-rifs",	0,	set80211rifs),
6052 	DEF_CMD("smps",		IEEE80211_HTCAP_SMPS_ENA,	set80211smps),
6053 	DEF_CMD("smpsdyn",	IEEE80211_HTCAP_SMPS_DYNAMIC,	set80211smps),
6054 	DEF_CMD("-smps",	IEEE80211_HTCAP_SMPS_OFF,	set80211smps),
6055 	/* XXX for testing */
6056 	DEF_CMD_ARG("chanswitch",	set80211chanswitch),
6057 
6058 	DEF_CMD_ARG("tdmaslot",		set80211tdmaslot),
6059 	DEF_CMD_ARG("tdmaslotcnt",	set80211tdmaslotcnt),
6060 	DEF_CMD_ARG("tdmaslotlen",	set80211tdmaslotlen),
6061 	DEF_CMD_ARG("tdmabintval",	set80211tdmabintval),
6062 
6063 	DEF_CMD_ARG("meshttl",		set80211meshttl),
6064 	DEF_CMD("meshforward",	1,	set80211meshforward),
6065 	DEF_CMD("-meshforward",	0,	set80211meshforward),
6066 	DEF_CMD("meshgate",	1,	set80211meshgate),
6067 	DEF_CMD("-meshgate",	0,	set80211meshgate),
6068 	DEF_CMD("meshpeering",	1,	set80211meshpeering),
6069 	DEF_CMD("-meshpeering",	0,	set80211meshpeering),
6070 	DEF_CMD_ARG("meshmetric",	set80211meshmetric),
6071 	DEF_CMD_ARG("meshpath",		set80211meshpath),
6072 	DEF_CMD("meshrt:flush",	IEEE80211_MESH_RTCMD_FLUSH,	set80211meshrtcmd),
6073 	DEF_CMD_ARG("meshrt:add",	set80211addmeshrt),
6074 	DEF_CMD_ARG("meshrt:del",	set80211delmeshrt),
6075 	DEF_CMD_ARG("hwmprootmode",	set80211hwmprootmode),
6076 	DEF_CMD_ARG("hwmpmaxhops",	set80211hwmpmaxhops),
6077 
6078 	/* vap cloning support */
6079 	DEF_CLONE_CMD_ARG("wlanaddr",	set80211clone_wlanaddr),
6080 	DEF_CLONE_CMD_ARG("wlanbssid",	set80211clone_wlanbssid),
6081 	DEF_CLONE_CMD_ARG("wlandev",	set80211clone_wlandev),
6082 	DEF_CLONE_CMD_ARG("wlanmode",	set80211clone_wlanmode),
6083 	DEF_CLONE_CMD("beacons", 1,	set80211clone_beacons),
6084 	DEF_CLONE_CMD("-beacons", 0,	set80211clone_beacons),
6085 	DEF_CLONE_CMD("bssid",	1,	set80211clone_bssid),
6086 	DEF_CLONE_CMD("-bssid",	0,	set80211clone_bssid),
6087 	DEF_CLONE_CMD("wdslegacy", 1,	set80211clone_wdslegacy),
6088 	DEF_CLONE_CMD("-wdslegacy", 0,	set80211clone_wdslegacy),
6089 };
6090 static struct afswtch af_ieee80211 = {
6091 	.af_name	= "af_ieee80211",
6092 	.af_af		= AF_UNSPEC,
6093 	.af_other_status = ieee80211_status,
6094 };
6095 
6096 static __constructor void
ieee80211_ctor(void)6097 ieee80211_ctor(void)
6098 {
6099 	for (size_t i = 0; i < nitems(ieee80211_cmds);  i++)
6100 		cmd_register(&ieee80211_cmds[i]);
6101 	af_register(&af_ieee80211);
6102 	clone_setdefcallback_prefix("wlan", wlan_create);
6103 }
6104