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