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