ieee80211.c revision 1.56.18.8 1 /* $NetBSD: ieee80211.c,v 1.56.18.8 2020/04/13 08:05:15 martin Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 *
6 * Copyright (c) 2001 Atsushi Onoe
7 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 #ifdef __NetBSD__
33 __KERNEL_RCSID(0, "$NetBSD: ieee80211.c,v 1.56.18.8 2020/04/13 08:05:15 martin Exp $");
34 #endif
35
36 /*
37 * IEEE 802.11 generic handler
38 */
39 #ifdef _KERNEL_OPT
40 #include "opt_wlan.h"
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/socket.h>
48 #include <sys/sbuf.h>
49
50 #ifdef __FreeBSD__
51 #include <machine/stdarg.h>
52 #elif __NetBSD__
53 #include <sys/once.h>
54 #include <sys/stdarg.h>
55 #else
56 #error
57 #endif
58
59 #include <net/if.h>
60 #ifdef __FreeBSD__
61 #include <net/if_var.h>
62 #endif
63 #include <net/if_dl.h>
64 #include <net/if_media.h>
65 #include <net/if_types.h>
66 #ifdef __FreeBSD__
67 #include <net/ethernet.h>
68 #endif
69 #ifdef __NetBSD__
70 #include <net/route.h>
71 #include <net/if_ether.h>
72 #endif
73
74 #include <net80211/ieee80211_var.h>
75 #include <net80211/ieee80211_regdomain.h>
76 #ifdef IEEE80211_SUPPORT_SUPERG
77 #include <net80211/ieee80211_superg.h>
78 #endif
79 #include <net80211/ieee80211_ratectl.h>
80 #include <net80211/ieee80211_vht.h>
81
82 #include <net/bpf.h>
83
84 #ifdef __NetBSD__
85 #undef KASSERT
86 #define KASSERT(__cond, __complaint) FBSDKASSERT(__cond, __complaint)
87 #endif
88
89 const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
90 [IEEE80211_MODE_AUTO] = "auto",
91 [IEEE80211_MODE_11A] = "11a",
92 [IEEE80211_MODE_11B] = "11b",
93 [IEEE80211_MODE_11G] = "11g",
94 [IEEE80211_MODE_FH] = "FH",
95 [IEEE80211_MODE_TURBO_A] = "turboA",
96 [IEEE80211_MODE_TURBO_G] = "turboG",
97 [IEEE80211_MODE_STURBO_A] = "sturboA",
98 [IEEE80211_MODE_HALF] = "half",
99 [IEEE80211_MODE_QUARTER] = "quarter",
100 [IEEE80211_MODE_11NA] = "11na",
101 [IEEE80211_MODE_11NG] = "11ng",
102 [IEEE80211_MODE_VHT_2GHZ] = "11acg",
103 [IEEE80211_MODE_VHT_5GHZ] = "11ac",
104 };
105 /* map ieee80211_opmode to the corresponding capability bit */
106 const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
107 [IEEE80211_M_IBSS] = IEEE80211_C_IBSS,
108 [IEEE80211_M_WDS] = IEEE80211_C_WDS,
109 [IEEE80211_M_STA] = IEEE80211_C_STA,
110 [IEEE80211_M_AHDEMO] = IEEE80211_C_AHDEMO,
111 [IEEE80211_M_HOSTAP] = IEEE80211_C_HOSTAP,
112 [IEEE80211_M_MONITOR] = IEEE80211_C_MONITOR,
113 #ifdef IEEE80211_SUPPORT_MESH
114 [IEEE80211_M_MBSS] = IEEE80211_C_MBSS,
115 #endif
116 };
117
118 const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
119 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
120
121 static void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
122 static void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
123 static void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
124 static void ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag);
125 static int ieee80211_media_setup(struct ieee80211com *ic,
126 struct ifmedia *media, int caps, int addsta,
127 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
128 static int media_status(enum ieee80211_opmode,
129 const struct ieee80211_channel *);
130 static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
131
132
133 MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
134
135 /*
136 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
137 */
138 #define B(r) ((r) | IEEE80211_RATE_BASIC)
139 static const struct ieee80211_rateset ieee80211_rateset_11a =
140 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
141 static const struct ieee80211_rateset ieee80211_rateset_half =
142 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
143 static const struct ieee80211_rateset ieee80211_rateset_quarter =
144 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
145 static const struct ieee80211_rateset ieee80211_rateset_11b =
146 { 4, { B(2), B(4), B(11), B(22) } };
147 /* NB: OFDM rates are handled specially based on mode */
148 static const struct ieee80211_rateset ieee80211_rateset_11g =
149 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
150 #undef B
151
152 static int set_vht_extchan(struct ieee80211_channel *c);
153
154 /*
155 * Fill in 802.11 available channel set, mark
156 * all available channels as active, and pick
157 * a default channel if not already specified.
158 */
159 void
160 ieee80211_chan_init(struct ieee80211com *ic)
161 {
162 #define DEFAULTRATES(m, def) do { \
163 if (ic->ic_sup_rates[m].rs_nrates == 0) \
164 ic->ic_sup_rates[m] = def; \
165 } while (0)
166 struct ieee80211_channel *c;
167 int i;
168
169 KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
170 ("invalid number of channels specified: %u", ic->ic_nchans));
171 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
172 memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
173 setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
174 for (i = 0; i < ic->ic_nchans; i++) {
175 c = &ic->ic_channels[i];
176 KASSERT(c->ic_flags != 0, ("channel with no flags"));
177 /*
178 * Help drivers that work only with frequencies by filling
179 * in IEEE channel #'s if not already calculated. Note this
180 * mimics similar work done in ieee80211_setregdomain when
181 * changing regulatory state.
182 */
183 if (c->ic_ieee == 0)
184 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
185
186 /*
187 * Setup the HT40/VHT40 upper/lower bits.
188 * The VHT80 math is done elsewhere.
189 */
190 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
191 c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
192 (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
193 c->ic_flags);
194
195 /* Update VHT math */
196 /*
197 * XXX VHT again, note that this assumes VHT80 channels
198 * are legit already
199 */
200 set_vht_extchan(c);
201
202 /* default max tx power to max regulatory */
203 if (c->ic_maxpower == 0)
204 c->ic_maxpower = 2*c->ic_maxregpower;
205 setbit(ic->ic_chan_avail, c->ic_ieee);
206 /*
207 * Identify mode capabilities.
208 */
209 if (IEEE80211_IS_CHAN_A(c))
210 setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
211 if (IEEE80211_IS_CHAN_B(c))
212 setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
213 if (IEEE80211_IS_CHAN_ANYG(c))
214 setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
215 if (IEEE80211_IS_CHAN_FHSS(c))
216 setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
217 if (IEEE80211_IS_CHAN_108A(c))
218 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
219 if (IEEE80211_IS_CHAN_108G(c))
220 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
221 if (IEEE80211_IS_CHAN_ST(c))
222 setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
223 if (IEEE80211_IS_CHAN_HALF(c))
224 setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
225 if (IEEE80211_IS_CHAN_QUARTER(c))
226 setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
227 if (IEEE80211_IS_CHAN_HTA(c))
228 setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
229 if (IEEE80211_IS_CHAN_HTG(c))
230 setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
231 if (IEEE80211_IS_CHAN_VHTA(c))
232 setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_5GHZ);
233 if (IEEE80211_IS_CHAN_VHTG(c))
234 setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_2GHZ);
235 }
236 /* initialize candidate channels to all available */
237 memcpy(ic->ic_chan_active, ic->ic_chan_avail,
238 sizeof(ic->ic_chan_avail));
239
240 /* sort channel table to allow lookup optimizations */
241 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
242
243 /* invalidate any previous state */
244 ic->ic_bsschan = IEEE80211_CHAN_ANYC;
245 ic->ic_prevchan = NULL;
246 ic->ic_csa_newchan = NULL;
247 /* arbitrarily pick the first channel */
248 ic->ic_curchan = &ic->ic_channels[0];
249 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
250
251 /* fillin well-known rate sets if driver has not specified */
252 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b);
253 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g);
254 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a);
255 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a);
256 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g);
257 DEFAULTRATES(IEEE80211_MODE_STURBO_A, ieee80211_rateset_11a);
258 DEFAULTRATES(IEEE80211_MODE_HALF, ieee80211_rateset_half);
259 DEFAULTRATES(IEEE80211_MODE_QUARTER, ieee80211_rateset_quarter);
260 DEFAULTRATES(IEEE80211_MODE_11NA, ieee80211_rateset_11a);
261 DEFAULTRATES(IEEE80211_MODE_11NG, ieee80211_rateset_11g);
262 DEFAULTRATES(IEEE80211_MODE_VHT_2GHZ, ieee80211_rateset_11g);
263 DEFAULTRATES(IEEE80211_MODE_VHT_5GHZ, ieee80211_rateset_11a);
264
265 /*
266 * Setup required information to fill the mcsset field, if driver did
267 * not. Assume a 2T2R setup for historic reasons.
268 */
269 if (ic->ic_rxstream == 0)
270 ic->ic_rxstream = 2;
271 if (ic->ic_txstream == 0)
272 ic->ic_txstream = 2;
273
274 ieee80211_init_suphtrates(ic);
275
276 /*
277 * Set auto mode to reset active channel state and any desired channel.
278 */
279 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
280 #undef DEFAULTRATES
281 }
282
283 static void
284 null_update_mcast(struct ieee80211com *ic)
285 {
286
287 ic_printf(ic, "need multicast update callback\n");
288 }
289
290 static void
291 null_update_promisc(struct ieee80211com *ic)
292 {
293
294 ic_printf(ic, "need promiscuous mode update callback\n");
295 }
296
297 static void
298 null_update_chw(struct ieee80211com *ic)
299 {
300
301 ic_printf(ic, "%s: need callback\n", __func__);
302 }
303
304 #ifdef __FreeBSD__
305 int
306 ic_printf(struct ieee80211com *ic, const char * fmt, ...)
307 {
308 va_list ap;
309 int retval;
310
311 retval = printf("%s: ", ic->ic_name);
312 va_start(ap, fmt);
313 retval += vprintf(fmt, ap);
314 va_end(ap);
315 return (retval);
316 }
317 #elif __NetBSD__
318 void
319 ic_printf(struct ieee80211com *ic, const char * fmt, ...)
320 {
321 va_list ap;
322
323 printf("%s: ", ic->ic_name);
324 va_start(ap, fmt);
325 vprintf(fmt, ap);
326 va_end(ap);
327 }
328 #endif
329
330 static LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head);
331 #ifdef __FreeBSD__
332 static struct mtx ic_list_mtx;
333 MTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF);
334 #elif __NetBSD__
335 static kmutex_t ic_list_mtx;
336 #endif
337
338 #if notyet
339 static int
340 sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS)
341 {
342 struct ieee80211com *ic;
343 struct sbuf sb;
344 char *sp;
345 int error;
346
347 error = sysctl_wire_old_buffer(req, 0);
348 if (error)
349 return (error);
350 sbuf_new_for_sysctl(&sb, NULL, 8, req);
351 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
352 sp = "";
353 mtx_lock(&ic_list_mtx);
354 LIST_FOREACH(ic, &ic_head, ic_next) {
355 sbuf_printf(&sb, "%s%s", sp, ic->ic_name);
356 sp = " ";
357 }
358 mtx_unlock(&ic_list_mtx);
359 error = sbuf_finish(&sb);
360 sbuf_delete(&sb);
361 return (error);
362 }
363
364 SYSCTL_PROC(_net_wlan, OID_AUTO, devices,
365 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
366 sysctl_ieee80211coms, "A", "names of available 802.11 devices");
367 #endif
368
369 #if __NetBSD__
370 static int
371 ic_list_mtx_init (void)
372 {
373 mutex_init(&ic_list_mtx, MUTEX_DEFAULT, IPL_NET);
374 ieee80211_auth_setup();
375 return 0;
376 }
377 #endif
378
379 /*
380 * Attach/setup the common net80211 state. Called by
381 * the driver on attach to prior to creating any vap's.
382 */
383 void
384 ieee80211_ifattach(struct ieee80211com *ic)
385 {
386 #if __NetBSD__
387 static ONCE_DECL(ic_list_mtx_once);
388 RUN_ONCE(&ic_list_mtx_once, ic_list_mtx_init);
389 #endif
390
391 IEEE80211_LOCK_INIT(ic, ic->ic_name);
392 IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
393 TAILQ_INIT(&ic->ic_vaps);
394
395 #if __FreeBSD__
396 /* Create a taskqueue for all state changes */
397 ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
398 taskqueue_thread_enqueue, &ic->ic_tq);
399 taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
400 ic->ic_name);
401 ic->ic_ierrors = counter_u64_alloc(M_WAITOK);
402 ic->ic_oerrors = counter_u64_alloc(M_WAITOK);
403 #elif __NetBSD__
404 /*
405 * Create a workqueue for all state changes, ieee80211_netbsd.*
406 * has glue to translate taskqueue functions to workqueue.
407 */
408 if (workqueue_create(&ic->ic_tq, "net80211_wq",
409 ieee80211_runwork, ic, PRI_SOFTNET, IPL_NET, WQ_MPSAFE))
410 panic("net80211 workqueue not created");
411 ic->ic_ierrors = 0;
412 ic->ic_oerrors = 0;
413 #endif
414
415 /*
416 * Fill in 802.11 available channel set, mark all
417 * available channels as active, and pick a default
418 * channel if not already specified.
419 */
420 ieee80211_chan_init(ic);
421
422 ic->ic_update_mcast = null_update_mcast;
423 ic->ic_update_promisc = null_update_promisc;
424 ic->ic_update_chw = null_update_chw;
425
426 ic->ic_hash_key = arc4random();
427 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
428 ic->ic_lintval = ic->ic_bintval;
429 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
430
431 ieee80211_crypto_attach(ic);
432 ieee80211_node_attach(ic);
433 ieee80211_power_attach(ic);
434 ieee80211_proto_attach(ic);
435 #ifdef IEEE80211_SUPPORT_SUPERG
436 ieee80211_superg_attach(ic);
437 #endif
438 ieee80211_ht_attach(ic);
439 ieee80211_vht_attach(ic);
440 ieee80211_scan_attach(ic);
441 ieee80211_regdomain_attach(ic);
442 ieee80211_dfs_attach(ic);
443
444 ieee80211_sysctl_attach(ic);
445
446 mtx_lock(&ic_list_mtx);
447 LIST_INSERT_HEAD(&ic_head, ic, ic_next);
448 mtx_unlock(&ic_list_mtx);
449 }
450
451 /*
452 * Detach net80211 state on device detach. Tear down
453 * all vap's and reclaim all common state prior to the
454 * device state going away. Note we may call back into
455 * driver; it must be prepared for this.
456 */
457 void
458 ieee80211_ifdetach(struct ieee80211com *ic)
459 {
460 struct ieee80211vap *vap;
461
462 mtx_lock(&ic_list_mtx);
463 LIST_REMOVE(ic, ic_next);
464 mtx_unlock(&ic_list_mtx);
465
466 #if __FreeBSD__
467 taskqueue_drain(taskqueue_thread, &ic->ic_restart_task);
468 #endif
469
470 /*
471 * The VAP is responsible for setting and clearing
472 * the VIMAGE context.
473 */
474 while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
475 ieee80211_vap_destroy(vap);
476 ieee80211_waitfor_parent(ic);
477
478 ieee80211_sysctl_detach(ic);
479 ieee80211_dfs_detach(ic);
480 ieee80211_regdomain_detach(ic);
481 ieee80211_scan_detach(ic);
482 #ifdef IEEE80211_SUPPORT_SUPERG
483 ieee80211_superg_detach(ic);
484 #endif
485 ieee80211_vht_detach(ic);
486 ieee80211_ht_detach(ic);
487 /* NB: must be called before ieee80211_node_detach */
488 ieee80211_proto_detach(ic);
489 ieee80211_crypto_detach(ic);
490 ieee80211_power_detach(ic);
491 ieee80211_node_detach(ic);
492
493 counter_u64_free(ic->ic_ierrors);
494 counter_u64_free(ic->ic_oerrors);
495
496 taskqueue_free(ic->ic_tq);
497 IEEE80211_TX_LOCK_DESTROY(ic);
498 IEEE80211_LOCK_DESTROY(ic);
499 }
500
501 struct ieee80211com *
502 ieee80211_find_com(const char *name)
503 {
504 struct ieee80211com *ic;
505
506 mtx_lock(&ic_list_mtx);
507 LIST_FOREACH(ic, &ic_head, ic_next)
508 if (strcmp(ic->ic_name, name) == 0)
509 break;
510 mtx_unlock(&ic_list_mtx);
511
512 return (ic);
513 }
514
515 void
516 ieee80211_iterate_coms(ieee80211_com_iter_func *f, void *arg)
517 {
518 struct ieee80211com *ic;
519
520 mtx_lock(&ic_list_mtx);
521 LIST_FOREACH(ic, &ic_head, ic_next)
522 (*f)(arg, ic);
523 mtx_unlock(&ic_list_mtx);
524 }
525
526 /*
527 * Default reset method for use with the ioctl support. This
528 * method is invoked after any state change in the 802.11
529 * layer that should be propagated to the hardware but not
530 * require re-initialization of the 802.11 state machine (e.g
531 * rescanning for an ap). We always return ENETRESET which
532 * should cause the driver to re-initialize the device. Drivers
533 * can override this method to implement more optimized support.
534 */
535 static int
536 default_reset(struct ieee80211vap *vap, u_long cmd)
537 {
538 return ENETRESET;
539 }
540
541 /*
542 * Default for updating the VAP default TX key index.
543 *
544 * Drivers that support TX offload as well as hardware encryption offload
545 * may need to be informed of key index changes separate from the key
546 * update.
547 */
548 static void
549 default_update_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid)
550 {
551
552 /* XXX assert validity */
553 /* XXX assert we're in a key update block */
554 vap->iv_def_txkey = kid;
555 }
556
557 /*
558 * Add underlying device errors to vap errors.
559 */
560 static __unused uint64_t
561 ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
562 {
563 struct ieee80211vap *vap = ifp->if_softc;
564 struct ieee80211com *ic = vap->iv_ic;
565 uint64_t rv;
566
567 rv = if_get_counter_default(ifp, cnt);
568 switch (cnt) {
569 case IFCOUNTER_OERRORS:
570 rv += counter_u64_fetch(ic->ic_oerrors);
571 break;
572 case IFCOUNTER_IERRORS:
573 rv += counter_u64_fetch(ic->ic_ierrors);
574 break;
575 default:
576 break;
577 }
578
579 return (rv);
580 }
581
582 /*
583 * Prepare a vap for use. Drivers use this call to
584 * setup net80211 state in new vap's prior attaching
585 * them with ieee80211_vap_attach (below).
586 */
587 int
588 ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
589 const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
590 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN])
591 {
592 struct ifnet *ifp;
593
594 ifp = if_alloc(IFT_ETHER);
595 if (ifp == NULL) {
596 ic_printf(ic, "%s: unable to allocate ifnet\n",
597 __func__);
598 return ENOMEM;
599 }
600 #if __NetBSD__
601 if_initialize(ifp);
602 #endif
603 if_initname(ifp, name, unit);
604 ifp->if_softc = vap; /* back pointer */
605 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
606 ifp->if_transmit = ieee80211_vap_transmit;
607 #if __FreeBSD__
608 ifp->if_qflush = ieee80211_vap_qflush;
609 #endif
610 ifp->if_ioctl = ieee80211_ioctl;
611 ifp->if_init = ieee80211_init;
612
613 #if notyet
614 ifp->if_get_counter = ieee80211_get_counter;
615 #endif
616 vap->iv_ifp = ifp;
617 vap->iv_ic = ic;
618 vap->iv_flags = ic->ic_flags; /* propagate common flags */
619 vap->iv_flags_ext = ic->ic_flags_ext;
620 vap->iv_flags_ven = ic->ic_flags_ven;
621 vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
622
623 /* 11n capabilities - XXX methodize */
624 vap->iv_htcaps = ic->ic_htcaps;
625 vap->iv_htextcaps = ic->ic_htextcaps;
626
627 /* 11ac capabilities - XXX methodize */
628 vap->iv_vhtcaps = ic->ic_vhtcaps;
629 vap->iv_vhtextcaps = ic->ic_vhtextcaps;
630
631 vap->iv_opmode = opmode;
632 vap->iv_caps |= ieee80211_opcap[opmode];
633 /* NNN Done in vap_attach, where is the correct place? */
634 IEEE80211_ADDR_COPY(vap->iv_myaddr, ic->ic_macaddr);
635 switch (opmode) {
636 case IEEE80211_M_WDS:
637 /*
638 * WDS links must specify the bssid of the far end.
639 * For legacy operation this is a static relationship.
640 * For non-legacy operation the station must associate
641 * and be authorized to pass traffic. Plumbing the
642 * vap to the proper node happens when the vap
643 * transitions to RUN state.
644 */
645 IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
646 vap->iv_flags |= IEEE80211_F_DESBSSID;
647 if (flags & IEEE80211_CLONE_WDSLEGACY)
648 vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
649 break;
650 #ifdef IEEE80211_SUPPORT_TDMA
651 case IEEE80211_M_AHDEMO:
652 if (flags & IEEE80211_CLONE_TDMA) {
653 /* NB: checked before clone operation allowed */
654 KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
655 ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
656 /*
657 * Propagate TDMA capability to mark vap; this
658 * cannot be removed and is used to distinguish
659 * regular ahdemo operation from ahdemo+tdma.
660 */
661 vap->iv_caps |= IEEE80211_C_TDMA;
662 }
663 break;
664 #endif
665 default:
666 break;
667 }
668 /* auto-enable s/w beacon miss support */
669 if (flags & IEEE80211_CLONE_NOBEACONS)
670 vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
671 /* auto-generated or user supplied MAC address */
672 if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
673 vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
674 /*
675 * Enable various functionality by default if we're
676 * capable; the driver can override us if it knows better.
677 */
678 if (vap->iv_caps & IEEE80211_C_WME)
679 vap->iv_flags |= IEEE80211_F_WME;
680 if (vap->iv_caps & IEEE80211_C_BURST)
681 vap->iv_flags |= IEEE80211_F_BURST;
682 /* NB: bg scanning only makes sense for station mode right now */
683 if (vap->iv_opmode == IEEE80211_M_STA &&
684 (vap->iv_caps & IEEE80211_C_BGSCAN))
685 vap->iv_flags |= IEEE80211_F_BGSCAN;
686 vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */
687 /* NB: DFS support only makes sense for ap mode right now */
688 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
689 (vap->iv_caps & IEEE80211_C_DFS))
690 vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
691
692 vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
693 vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
694 vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
695 /*
696 * Install a default reset method for the ioctl support;
697 * the driver can override this.
698 */
699 vap->iv_reset = default_reset;
700
701 /*
702 * Install a default crypto key update method, the driver
703 * can override this.
704 */
705 vap->iv_update_deftxkey = default_update_deftxkey;
706
707 ieee80211_sysctl_vattach(vap);
708 ieee80211_crypto_vattach(vap);
709 ieee80211_node_vattach(vap);
710 ieee80211_power_vattach(vap);
711 ieee80211_proto_vattach(vap);
712 #ifdef IEEE80211_SUPPORT_SUPERG
713 ieee80211_superg_vattach(vap);
714 #endif
715 ieee80211_ht_vattach(vap);
716 ieee80211_vht_vattach(vap);
717 ieee80211_scan_vattach(vap);
718 ieee80211_regdomain_vattach(vap);
719 ieee80211_radiotap_vattach(vap);
720 ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
721
722 return 0;
723 }
724
725 /*
726 * Activate a vap. State should have been prepared with a
727 * call to ieee80211_vap_setup and by the driver. On return
728 * from this call the vap is ready for use.
729 */
730 int
731 ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change,
732 ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN])
733 {
734 struct ifnet *ifp = vap->iv_ifp;
735 struct ieee80211com *ic = vap->iv_ic;
736 struct ifmediareq imr;
737 int maxrate;
738
739 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
740 "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
741 __func__, ieee80211_opmode_name[vap->iv_opmode],
742 ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
743
744 /*
745 * Do late attach work that cannot happen until after
746 * the driver has had a chance to override defaults.
747 */
748 ieee80211_node_latevattach(vap);
749 ieee80211_power_latevattach(vap);
750
751 maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
752 vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
753 ieee80211_media_status(ifp, &imr);
754 /* NB: strip explicit mode; we're actually in autoselect */
755 ifmedia_set(&vap->iv_media,
756 imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
757 if (maxrate)
758 ifp->if_baudrate = IF_Mbps(maxrate);
759
760 ether_ifattach(ifp, macaddr);
761 /* NNN also done in vap_setup, which is correct? */
762 IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
763
764 /* hook output method setup by ether_ifattach */
765 vap->iv_output = ifp->if_output;
766 ifp->if_output = ieee80211_output;
767
768 /* NB: if_mtu set by ether_ifattach to ETHERMTU */
769
770 IEEE80211_LOCK(ic);
771 TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
772 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
773 #ifdef IEEE80211_SUPPORT_SUPERG
774 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
775 #endif
776 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
777 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
778 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
779 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
780
781 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
782 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
783 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
784 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
785 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
786 IEEE80211_UNLOCK(ic);
787
788 #if __NetBSD__
789 if_register(ifp);
790 #endif
791
792 return 1;
793 }
794
795 /*
796 * Tear down vap state and reclaim the ifnet.
797 * The driver is assumed to have prepared for
798 * this; e.g. by turning off interrupts for the
799 * underlying device.
800 */
801 void
802 ieee80211_vap_detach(struct ieee80211vap *vap)
803 {
804 struct ieee80211com *ic = vap->iv_ic;
805 struct ifnet *ifp = vap->iv_ifp;
806
807 CURVNET_SET(ifp->if_vnet);
808
809 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
810 __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
811
812 /* NB: bpfdetach is called by ether_ifdetach and claims all taps */
813 ether_ifdetach(ifp);
814
815 ieee80211_stop(vap);
816
817 /*
818 * Flush any deferred vap tasks.
819 */
820 ieee80211_draintask(ic, &vap->iv_nstate_task);
821 ieee80211_draintask(ic, &vap->iv_swbmiss_task);
822 ieee80211_draintask(ic, &vap->iv_wme_task);
823 ieee80211_draintask(ic, &ic->ic_parent_task);
824
825 #if __FreeBSD__
826 /* XXX band-aid until ifnet handles this for us */
827 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
828 #endif
829
830 IEEE80211_LOCK(ic);
831 KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
832 TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
833 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
834 #ifdef IEEE80211_SUPPORT_SUPERG
835 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
836 #endif
837 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
838 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
839 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
840 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
841
842 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
843 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
844 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
845 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
846 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
847
848 /* NB: this handles the bpfdetach done below */
849 ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
850 if (vap->iv_ifflags & IFF_PROMISC)
851 ieee80211_promisc(vap, false);
852 if (vap->iv_ifflags & IFF_ALLMULTI)
853 ieee80211_allmulti(vap, false);
854 IEEE80211_UNLOCK(ic);
855
856 ifmedia_removeall(&vap->iv_media);
857
858 ieee80211_radiotap_vdetach(vap);
859 ieee80211_regdomain_vdetach(vap);
860 ieee80211_scan_vdetach(vap);
861 #ifdef IEEE80211_SUPPORT_SUPERG
862 ieee80211_superg_vdetach(vap);
863 #endif
864 ieee80211_vht_vdetach(vap);
865 ieee80211_ht_vdetach(vap);
866 /* NB: must be before ieee80211_node_vdetach */
867 ieee80211_proto_vdetach(vap);
868 ieee80211_crypto_vdetach(vap);
869 ieee80211_power_vdetach(vap);
870 ieee80211_node_vdetach(vap);
871 ieee80211_sysctl_vdetach(vap);
872
873 if_free(ifp);
874
875 CURVNET_RESTORE();
876 }
877
878 /*
879 * Count number of vaps in promisc, and issue promisc on
880 * parent respectively.
881 */
882 void
883 ieee80211_promisc(struct ieee80211vap *vap, bool on)
884 {
885 struct ieee80211com *ic = vap->iv_ic;
886
887 IEEE80211_LOCK_ASSERT(ic);
888
889 if (on) {
890 if (++ic->ic_promisc == 1)
891 ieee80211_runtask(ic, &ic->ic_promisc_task);
892 } else {
893 KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc",
894 __func__, ic));
895 if (--ic->ic_promisc == 0)
896 ieee80211_runtask(ic, &ic->ic_promisc_task);
897 }
898 }
899
900 /*
901 * Count number of vaps in allmulti, and issue allmulti on
902 * parent respectively.
903 */
904 void
905 ieee80211_allmulti(struct ieee80211vap *vap, bool on)
906 {
907 struct ieee80211com *ic = vap->iv_ic;
908
909 IEEE80211_LOCK_ASSERT(ic);
910
911 if (on) {
912 if (++ic->ic_allmulti == 1)
913 ieee80211_runtask(ic, &ic->ic_mcast_task);
914 } else {
915 KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti",
916 __func__, ic));
917 if (--ic->ic_allmulti == 0)
918 ieee80211_runtask(ic, &ic->ic_mcast_task);
919 }
920 }
921
922 /*
923 * Synchronize flag bit state in the com structure
924 * according to the state of all vap's. This is used,
925 * for example, to handle state changes via ioctls.
926 */
927 static void
928 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
929 {
930 struct ieee80211vap *vap;
931 int bit;
932
933 IEEE80211_LOCK_ASSERT(ic);
934
935 bit = 0;
936 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
937 if (vap->iv_flags & flag) {
938 bit = 1;
939 break;
940 }
941 if (bit)
942 ic->ic_flags |= flag;
943 else
944 ic->ic_flags &= ~flag;
945 }
946
947 void
948 ieee80211_syncflag(struct ieee80211vap *vap, int flag)
949 {
950 struct ieee80211com *ic = vap->iv_ic;
951
952 IEEE80211_LOCK(ic);
953 if (flag < 0) {
954 flag = -flag;
955 vap->iv_flags &= ~flag;
956 } else
957 vap->iv_flags |= flag;
958 ieee80211_syncflag_locked(ic, flag);
959 IEEE80211_UNLOCK(ic);
960 }
961
962 /*
963 * Synchronize flags_ht bit state in the com structure
964 * according to the state of all vap's. This is used,
965 * for example, to handle state changes via ioctls.
966 */
967 static void
968 ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
969 {
970 struct ieee80211vap *vap;
971 int bit;
972
973 IEEE80211_LOCK_ASSERT(ic);
974
975 bit = 0;
976 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
977 if (vap->iv_flags_ht & flag) {
978 bit = 1;
979 break;
980 }
981 if (bit)
982 ic->ic_flags_ht |= flag;
983 else
984 ic->ic_flags_ht &= ~flag;
985 }
986
987 void
988 ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
989 {
990 struct ieee80211com *ic = vap->iv_ic;
991
992 IEEE80211_LOCK(ic);
993 if (flag < 0) {
994 flag = -flag;
995 vap->iv_flags_ht &= ~flag;
996 } else
997 vap->iv_flags_ht |= flag;
998 ieee80211_syncflag_ht_locked(ic, flag);
999 IEEE80211_UNLOCK(ic);
1000 }
1001
1002 /*
1003 * Synchronize flags_vht bit state in the com structure
1004 * according to the state of all vap's. This is used,
1005 * for example, to handle state changes via ioctls.
1006 */
1007 static void
1008 ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag)
1009 {
1010 struct ieee80211vap *vap;
1011 int bit;
1012
1013 IEEE80211_LOCK_ASSERT(ic);
1014
1015 bit = 0;
1016 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1017 if (vap->iv_flags_vht & flag) {
1018 bit = 1;
1019 break;
1020 }
1021 if (bit)
1022 ic->ic_flags_vht |= flag;
1023 else
1024 ic->ic_flags_vht &= ~flag;
1025 }
1026
1027 void
1028 ieee80211_syncflag_vht(struct ieee80211vap *vap, int flag)
1029 {
1030 struct ieee80211com *ic = vap->iv_ic;
1031
1032 IEEE80211_LOCK(ic);
1033 if (flag < 0) {
1034 flag = -flag;
1035 vap->iv_flags_vht &= ~flag;
1036 } else
1037 vap->iv_flags_vht |= flag;
1038 ieee80211_syncflag_vht_locked(ic, flag);
1039 IEEE80211_UNLOCK(ic);
1040 }
1041
1042 /*
1043 * Synchronize flags_ext bit state in the com structure
1044 * according to the state of all vap's. This is used,
1045 * for example, to handle state changes via ioctls.
1046 */
1047 static void
1048 ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
1049 {
1050 struct ieee80211vap *vap;
1051 int bit;
1052
1053 IEEE80211_LOCK_ASSERT(ic);
1054
1055 bit = 0;
1056 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1057 if (vap->iv_flags_ext & flag) {
1058 bit = 1;
1059 break;
1060 }
1061 if (bit)
1062 ic->ic_flags_ext |= flag;
1063 else
1064 ic->ic_flags_ext &= ~flag;
1065 }
1066
1067 void
1068 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
1069 {
1070 struct ieee80211com *ic = vap->iv_ic;
1071
1072 IEEE80211_LOCK(ic);
1073 if (flag < 0) {
1074 flag = -flag;
1075 vap->iv_flags_ext &= ~flag;
1076 } else
1077 vap->iv_flags_ext |= flag;
1078 ieee80211_syncflag_ext_locked(ic, flag);
1079 IEEE80211_UNLOCK(ic);
1080 }
1081
1082 static __inline int
1083 mapgsm(u_int freq, u_int flags)
1084 {
1085 freq *= 10;
1086 if (flags & IEEE80211_CHAN_QUARTER)
1087 freq += 5;
1088 else if (flags & IEEE80211_CHAN_HALF)
1089 freq += 10;
1090 else
1091 freq += 20;
1092 /* NB: there is no 907/20 wide but leave room */
1093 return (freq - 906*10) / 5;
1094 }
1095
1096 static __inline int
1097 mappsb(u_int freq, u_int flags)
1098 {
1099 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
1100 }
1101
1102 /*
1103 * Convert MHz frequency to IEEE channel number.
1104 */
1105 int
1106 ieee80211_mhz2ieee(u_int freq, u_int flags)
1107 {
1108 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
1109 if (flags & IEEE80211_CHAN_GSM)
1110 return mapgsm(freq, flags);
1111 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
1112 if (freq == 2484)
1113 return 14;
1114 if (freq < 2484)
1115 return ((int) freq - 2407) / 5;
1116 else
1117 return 15 + ((freq - 2512) / 20);
1118 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
1119 if (freq <= 5000) {
1120 /* XXX check regdomain? */
1121 if (IS_FREQ_IN_PSB(freq))
1122 return mappsb(freq, flags);
1123 return (freq - 4000) / 5;
1124 } else
1125 return (freq - 5000) / 5;
1126 } else { /* either, guess */
1127 if (freq == 2484)
1128 return 14;
1129 if (freq < 2484) {
1130 if (907 <= freq && freq <= 922)
1131 return mapgsm(freq, flags);
1132 return ((int) freq - 2407) / 5;
1133 }
1134 if (freq < 5000) {
1135 if (IS_FREQ_IN_PSB(freq))
1136 return mappsb(freq, flags);
1137 else if (freq > 4900)
1138 return (freq - 4000) / 5;
1139 else
1140 return 15 + ((freq - 2512) / 20);
1141 }
1142 return (freq - 5000) / 5;
1143 }
1144 #undef IS_FREQ_IN_PSB
1145 }
1146
1147 /*
1148 * Convert channel to IEEE channel number.
1149 */
1150 int
1151 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
1152 {
1153 if (c == NULL) {
1154 ic_printf(ic, "invalid channel (NULL)\n");
1155 return 0; /* XXX */
1156 }
1157 return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee);
1158 }
1159
1160 /*
1161 * Convert IEEE channel number to MHz frequency.
1162 */
1163 u_int
1164 ieee80211_ieee2mhz(u_int chan, u_int flags)
1165 {
1166 if (flags & IEEE80211_CHAN_GSM)
1167 return 907 + 5 * (chan / 10);
1168 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
1169 if (chan == 14)
1170 return 2484;
1171 if (chan < 14)
1172 return 2407 + chan*5;
1173 else
1174 return 2512 + ((chan-15)*20);
1175 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
1176 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
1177 chan -= 37;
1178 return 4940 + chan*5 + (chan % 5 ? 2 : 0);
1179 }
1180 return 5000 + (chan*5);
1181 } else { /* either, guess */
1182 /* XXX can't distinguish PSB+GSM channels */
1183 if (chan == 14)
1184 return 2484;
1185 if (chan < 14) /* 0-13 */
1186 return 2407 + chan*5;
1187 if (chan < 27) /* 15-26 */
1188 return 2512 + ((chan-15)*20);
1189 return 5000 + (chan*5);
1190 }
1191 }
1192
1193 static __inline void
1194 set_extchan(struct ieee80211_channel *c)
1195 {
1196
1197 /*
1198 * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4:
1199 * "the secondary channel number shall be 'N + [1,-1] * 4'
1200 */
1201 if (c->ic_flags & IEEE80211_CHAN_HT40U)
1202 c->ic_extieee = c->ic_ieee + 4;
1203 else if (c->ic_flags & IEEE80211_CHAN_HT40D)
1204 c->ic_extieee = c->ic_ieee - 4;
1205 else
1206 c->ic_extieee = 0;
1207 }
1208
1209 /*
1210 * Populate the freq1/freq2 fields as appropriate for VHT channels.
1211 *
1212 * This for now uses a hard-coded list of 80MHz wide channels.
1213 *
1214 * For HT20/HT40, freq1 just is the centre frequency of the 40MHz
1215 * wide channel we've already decided upon.
1216 *
1217 * For VHT80 and VHT160, there are only a small number of fixed
1218 * 80/160MHz wide channels, so we just use those.
1219 *
1220 * This is all likely very very wrong - both the regulatory code
1221 * and this code needs to ensure that all four channels are
1222 * available and valid before the VHT80 (and eight for VHT160) channel
1223 * is created.
1224 */
1225
1226 struct vht_chan_range {
1227 uint16_t freq_start;
1228 uint16_t freq_end;
1229 };
1230
1231 struct vht_chan_range vht80_chan_ranges[] = {
1232 { 5170, 5250 },
1233 { 5250, 5330 },
1234 { 5490, 5570 },
1235 { 5570, 5650 },
1236 { 5650, 5730 },
1237 { 5735, 5815 },
1238 { 0, 0, }
1239 };
1240
1241 static int
1242 set_vht_extchan(struct ieee80211_channel *c)
1243 {
1244 int i;
1245
1246 if (! IEEE80211_IS_CHAN_VHT(c)) {
1247 return (0);
1248 }
1249
1250 if (IEEE80211_IS_CHAN_VHT20(c)) {
1251 c->ic_vht_ch_freq1 = c->ic_ieee;
1252 return (1);
1253 }
1254
1255 if (IEEE80211_IS_CHAN_VHT40(c)) {
1256 if (IEEE80211_IS_CHAN_HT40U(c))
1257 c->ic_vht_ch_freq1 = c->ic_ieee + 2;
1258 else if (IEEE80211_IS_CHAN_HT40D(c))
1259 c->ic_vht_ch_freq1 = c->ic_ieee - 2;
1260 else
1261 return (0);
1262 return (1);
1263 }
1264
1265 if (IEEE80211_IS_CHAN_VHT80(c)) {
1266 for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
1267 if (c->ic_freq >= vht80_chan_ranges[i].freq_start &&
1268 c->ic_freq < vht80_chan_ranges[i].freq_end) {
1269 int midpoint;
1270
1271 midpoint = vht80_chan_ranges[i].freq_start + 40;
1272 c->ic_vht_ch_freq1 =
1273 ieee80211_mhz2ieee(midpoint, c->ic_flags);
1274 c->ic_vht_ch_freq2 = 0;
1275 #if 0
1276 printf("%s: %d, freq=%d, midpoint=%d, freq1=%d, freq2=%d\n",
1277 __func__, c->ic_ieee, c->ic_freq, midpoint,
1278 c->ic_vht_ch_freq1, c->ic_vht_ch_freq2);
1279 #endif
1280 return (1);
1281 }
1282 }
1283 return (0);
1284 }
1285
1286 printf("%s: unknown VHT channel type (ieee=%d, flags=0x%08x)\n",
1287 __func__,
1288 c->ic_ieee,
1289 c->ic_flags);
1290
1291 return (0);
1292 }
1293
1294 /*
1295 * Return whether the current channel could possibly be a part of
1296 * a VHT80 channel.
1297 *
1298 * This doesn't check that the whole range is in the allowed list
1299 * according to regulatory.
1300 */
1301 static int
1302 is_vht80_valid_freq(uint16_t freq)
1303 {
1304 int i;
1305 for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
1306 if (freq >= vht80_chan_ranges[i].freq_start &&
1307 freq < vht80_chan_ranges[i].freq_end)
1308 return (1);
1309 }
1310 return (0);
1311 }
1312
1313 static int
1314 addchan(struct ieee80211_channel chans[], int maxchans, int *nchans,
1315 uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags)
1316 {
1317 struct ieee80211_channel *c;
1318
1319 if (*nchans >= maxchans)
1320 return (ENOBUFS);
1321
1322 #if 0
1323 printf("%s: %d: ieee=%d, freq=%d, flags=0x%08x\n",
1324 __func__,
1325 *nchans,
1326 ieee,
1327 freq,
1328 flags);
1329 #endif
1330
1331 c = &chans[(*nchans)++];
1332 c->ic_ieee = ieee;
1333 c->ic_freq = freq != 0 ? freq : ieee80211_ieee2mhz(ieee, flags);
1334 c->ic_maxregpower = maxregpower;
1335 c->ic_maxpower = 2 * maxregpower;
1336 c->ic_flags = flags;
1337 c->ic_vht_ch_freq1 = 0;
1338 c->ic_vht_ch_freq2 = 0;
1339 set_extchan(c);
1340 set_vht_extchan(c);
1341
1342 return (0);
1343 }
1344
1345 static int
1346 copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans,
1347 uint32_t flags)
1348 {
1349 struct ieee80211_channel *c;
1350
1351 KASSERT(*nchans > 0, ("channel list is empty\n"));
1352
1353 if (*nchans >= maxchans)
1354 return (ENOBUFS);
1355
1356 #if 0
1357 printf("%s: %d: flags=0x%08x\n",
1358 __func__,
1359 *nchans,
1360 flags);
1361 #endif
1362
1363 c = &chans[(*nchans)++];
1364 c[0] = c[-1];
1365 c->ic_flags = flags;
1366 c->ic_vht_ch_freq1 = 0;
1367 c->ic_vht_ch_freq2 = 0;
1368 set_extchan(c);
1369 set_vht_extchan(c);
1370
1371 return (0);
1372 }
1373
1374 /*
1375 * XXX VHT-2GHz
1376 */
1377 static void
1378 getflags_2ghz(const uint8_t bands[], uint32_t flags[], int ht40)
1379 {
1380 int nmodes;
1381
1382 nmodes = 0;
1383 if (isset(bands, IEEE80211_MODE_11B))
1384 flags[nmodes++] = IEEE80211_CHAN_B;
1385 if (isset(bands, IEEE80211_MODE_11G))
1386 flags[nmodes++] = IEEE80211_CHAN_G;
1387 if (isset(bands, IEEE80211_MODE_11NG))
1388 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
1389 if (ht40) {
1390 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U;
1391 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D;
1392 }
1393 flags[nmodes] = 0;
1394 }
1395
1396 static void
1397 getflags_5ghz(const uint8_t bands[], uint32_t flags[], int ht40, int vht80)
1398 {
1399 int nmodes;
1400
1401 /*
1402 * the addchan_list function seems to expect the flags array to
1403 * be in channel width order, so the VHT bits are interspersed
1404 * as appropriate to maintain said order.
1405 *
1406 * It also assumes HT40U is before HT40D.
1407 */
1408 nmodes = 0;
1409
1410 /* 20MHz */
1411 if (isset(bands, IEEE80211_MODE_11A))
1412 flags[nmodes++] = IEEE80211_CHAN_A;
1413 if (isset(bands, IEEE80211_MODE_11NA))
1414 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
1415 if (isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1416 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20 |
1417 IEEE80211_CHAN_VHT20;
1418 }
1419
1420 /* 40MHz */
1421 if (ht40) {
1422 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U;
1423 }
1424 if (ht40 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1425 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U
1426 | IEEE80211_CHAN_VHT40U;
1427 }
1428 if (ht40) {
1429 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D;
1430 }
1431 if (ht40 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1432 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D
1433 | IEEE80211_CHAN_VHT40D;
1434 }
1435
1436 /* 80MHz */
1437 if (vht80 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1438 flags[nmodes++] = IEEE80211_CHAN_A |
1439 IEEE80211_CHAN_HT40U | IEEE80211_CHAN_VHT80;
1440 flags[nmodes++] = IEEE80211_CHAN_A |
1441 IEEE80211_CHAN_HT40D | IEEE80211_CHAN_VHT80;
1442 }
1443
1444 /* XXX VHT80+80 */
1445 /* XXX VHT160 */
1446 flags[nmodes] = 0;
1447 }
1448
1449 static void
1450 getflags(const uint8_t bands[], uint32_t flags[], int ht40, int vht80)
1451 {
1452
1453 flags[0] = 0;
1454 if (isset(bands, IEEE80211_MODE_11A) ||
1455 isset(bands, IEEE80211_MODE_11NA) ||
1456 isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1457 if (isset(bands, IEEE80211_MODE_11B) ||
1458 isset(bands, IEEE80211_MODE_11G) ||
1459 isset(bands, IEEE80211_MODE_11NG) ||
1460 isset(bands, IEEE80211_MODE_VHT_2GHZ))
1461 return;
1462
1463 getflags_5ghz(bands, flags, ht40, vht80);
1464 } else
1465 getflags_2ghz(bands, flags, ht40);
1466 }
1467
1468 /*
1469 * Add one 20 MHz channel into specified channel list.
1470 */
1471 /* XXX VHT */
1472 int
1473 ieee80211_add_channel(struct ieee80211_channel chans[], int maxchans,
1474 int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower,
1475 uint32_t chan_flags, const uint8_t bands[])
1476 {
1477 uint32_t flags[IEEE80211_MODE_MAX];
1478 int i, error;
1479
1480 getflags(bands, flags, 0, 0);
1481 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1482
1483 error = addchan(chans, maxchans, nchans, ieee, freq, maxregpower,
1484 flags[0] | chan_flags);
1485 for (i = 1; flags[i] != 0 && error == 0; i++) {
1486 error = copychan_prev(chans, maxchans, nchans,
1487 flags[i] | chan_flags);
1488 }
1489
1490 return (error);
1491 }
1492
1493 static struct ieee80211_channel *
1494 findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq,
1495 uint32_t flags)
1496 {
1497 struct ieee80211_channel *c;
1498 int i;
1499
1500 flags &= IEEE80211_CHAN_ALLTURBO;
1501 /* brute force search */
1502 for (i = 0; i < nchans; i++) {
1503 c = &chans[i];
1504 if (c->ic_freq == freq &&
1505 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1506 return c;
1507 }
1508 return NULL;
1509 }
1510
1511 /*
1512 * Add 40 MHz channel pair into specified channel list.
1513 */
1514 /* XXX VHT */
1515 int
1516 ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans,
1517 int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags)
1518 {
1519 struct ieee80211_channel *cent, *extc;
1520 uint16_t freq;
1521 int error;
1522
1523 freq = ieee80211_ieee2mhz(ieee, flags);
1524
1525 /*
1526 * Each entry defines an HT40 channel pair; find the
1527 * center channel, then the extension channel above.
1528 */
1529 flags |= IEEE80211_CHAN_HT20;
1530 cent = findchannel(chans, *nchans, freq, flags);
1531 if (cent == NULL)
1532 return (EINVAL);
1533
1534 extc = findchannel(chans, *nchans, freq + 20, flags);
1535 if (extc == NULL)
1536 return (ENOENT);
1537
1538 flags &= ~IEEE80211_CHAN_HT;
1539 error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq,
1540 maxregpower, flags | IEEE80211_CHAN_HT40U);
1541 if (error != 0)
1542 return (error);
1543
1544 error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq,
1545 maxregpower, flags | IEEE80211_CHAN_HT40D);
1546
1547 return (error);
1548 }
1549
1550 /*
1551 * Fetch the center frequency for the primary channel.
1552 */
1553 uint32_t
1554 ieee80211_get_channel_center_freq(const struct ieee80211_channel *c)
1555 {
1556
1557 return (c->ic_freq);
1558 }
1559
1560 /*
1561 * Fetch the center frequency for the primary BAND channel.
1562 *
1563 * For 5, 10, 20MHz channels it'll be the normally configured channel
1564 * frequency.
1565 *
1566 * For 40MHz, 80MHz, 160Mhz channels it'll the the centre of the
1567 * wide channel, not the centre of the primary channel (that's ic_freq).
1568 *
1569 * For 80+80MHz channels this will be the centre of the primary
1570 * 80MHz channel; the secondary 80MHz channel will be center_freq2().
1571 */
1572 uint32_t
1573 ieee80211_get_channel_center_freq1(const struct ieee80211_channel *c)
1574 {
1575
1576 /*
1577 * VHT - use the pre-calculated centre frequency
1578 * of the given channel.
1579 */
1580 if (IEEE80211_IS_CHAN_VHT(c))
1581 return (ieee80211_ieee2mhz(c->ic_vht_ch_freq1, c->ic_flags));
1582
1583 if (IEEE80211_IS_CHAN_HT40U(c)) {
1584 return (c->ic_freq + 10);
1585 }
1586 if (IEEE80211_IS_CHAN_HT40D(c)) {
1587 return (c->ic_freq - 10);
1588 }
1589
1590 return (c->ic_freq);
1591 }
1592
1593 /*
1594 * For now, no 80+80 support; it will likely always return 0.
1595 */
1596 uint32_t
1597 ieee80211_get_channel_center_freq2(const struct ieee80211_channel *c)
1598 {
1599
1600 if (IEEE80211_IS_CHAN_VHT(c) && (c->ic_vht_ch_freq2 != 0))
1601 return (ieee80211_ieee2mhz(c->ic_vht_ch_freq2, c->ic_flags));
1602
1603 return (0);
1604 }
1605
1606 /*
1607 * Adds channels into specified channel list (ieee[] array must be sorted).
1608 * Channels are already sorted.
1609 */
1610 static int
1611 add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans,
1612 const uint8_t ieee[], int nieee, uint32_t flags[])
1613 {
1614 uint16_t freq;
1615 int i, j, error;
1616 int is_vht;
1617
1618 for (i = 0; i < nieee; i++) {
1619 freq = ieee80211_ieee2mhz(ieee[i], flags[0]);
1620 for (j = 0; flags[j] != 0; j++) {
1621 /*
1622 * Notes:
1623 * + HT40 and VHT40 channels occur together, so
1624 * we need to be careful that we actually allow that.
1625 * + VHT80, VHT160 will coexist with HT40/VHT40, so
1626 * make sure it's not skipped because of the overlap
1627 * check used for (V)HT40.
1628 */
1629 is_vht = !! (flags[j] & IEEE80211_CHAN_VHT);
1630
1631 /*
1632 * Test for VHT80.
1633 * XXX This is all very broken right now.
1634 * What we /should/ do is:
1635 *
1636 * + check that the frequency is in the list of
1637 * allowed VHT80 ranges; and
1638 * + the other 3 channels in the list are actually
1639 * also available.
1640 */
1641 if (is_vht && flags[j] & IEEE80211_CHAN_VHT80)
1642 if (! is_vht80_valid_freq(freq))
1643 continue;
1644
1645 /*
1646 * Test for (V)HT40.
1647 *
1648 * This is also a fall through from VHT80; as we only
1649 * allow a VHT80 channel if the VHT40 combination is
1650 * also valid. If the VHT40 form is not valid then
1651 * we certainly can't do VHT80..
1652 */
1653 if (flags[j] & IEEE80211_CHAN_HT40D)
1654 /*
1655 * Can't have a "lower" channel if we are the
1656 * first channel.
1657 *
1658 * Can't have a "lower" channel if it's below/
1659 * within 20MHz of the first channel.
1660 *
1661 * Can't have a "lower" channel if the channel
1662 * below it is not 20MHz away.
1663 */
1664 if (i == 0 || ieee[i] < ieee[0] + 4 ||
1665 freq - 20 !=
1666 ieee80211_ieee2mhz(ieee[i] - 4, flags[j]))
1667 continue;
1668 if (flags[j] & IEEE80211_CHAN_HT40U)
1669 /*
1670 * Can't have an "upper" channel if we are
1671 * the last channel.
1672 *
1673 * Can't have an "upper" channel be above the
1674 * last channel in the list.
1675 *
1676 * Can't have an "upper" channel if the next
1677 * channel according to the math isn't 20MHz
1678 * away. (Likely for channel 13/14.)
1679 */
1680 if (i == nieee - 1 ||
1681 ieee[i] + 4 > ieee[nieee - 1] ||
1682 freq + 20 !=
1683 ieee80211_ieee2mhz(ieee[i] + 4, flags[j]))
1684 continue;
1685
1686 if (j == 0) {
1687 error = addchan(chans, maxchans, nchans,
1688 ieee[i], freq, 0, flags[j]);
1689 } else {
1690 error = copychan_prev(chans, maxchans, nchans,
1691 flags[j]);
1692 }
1693 if (error != 0)
1694 return (error);
1695 }
1696 }
1697
1698 return (0);
1699 }
1700
1701 int
1702 ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[], int maxchans,
1703 int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1704 int ht40)
1705 {
1706 uint32_t flags[IEEE80211_MODE_MAX];
1707
1708 /* XXX no VHT for now */
1709 getflags_2ghz(bands, flags, ht40);
1710 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1711
1712 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1713 }
1714
1715 int
1716 ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[], int maxchans,
1717 int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1718 int ht40)
1719 {
1720 uint32_t flags[IEEE80211_MODE_MAX];
1721 int vht80 = 0;
1722
1723 /*
1724 * For now, assume VHT == VHT80 support as a minimum.
1725 */
1726 if (isset(bands, IEEE80211_MODE_VHT_5GHZ))
1727 vht80 = 1;
1728
1729 getflags_5ghz(bands, flags, ht40, vht80);
1730 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1731
1732 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1733 }
1734
1735 /*
1736 * Locate a channel given a frequency+flags. We cache
1737 * the previous lookup to optimize switching between two
1738 * channels--as happens with dynamic turbo.
1739 */
1740 struct ieee80211_channel *
1741 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
1742 {
1743 struct ieee80211_channel *c;
1744
1745 flags &= IEEE80211_CHAN_ALLTURBO;
1746 c = ic->ic_prevchan;
1747 if (c != NULL && c->ic_freq == freq &&
1748 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1749 return c;
1750 /* brute force search */
1751 return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags));
1752 }
1753
1754 /*
1755 * Locate a channel given a channel number+flags. We cache
1756 * the previous lookup to optimize switching between two
1757 * channels--as happens with dynamic turbo.
1758 */
1759 struct ieee80211_channel *
1760 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1761 {
1762 struct ieee80211_channel *c;
1763 int i;
1764
1765 flags &= IEEE80211_CHAN_ALLTURBO;
1766 c = ic->ic_prevchan;
1767 if (c != NULL && c->ic_ieee == ieee &&
1768 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1769 return c;
1770 /* brute force search */
1771 for (i = 0; i < ic->ic_nchans; i++) {
1772 c = &ic->ic_channels[i];
1773 if (c->ic_ieee == ieee &&
1774 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1775 return c;
1776 }
1777 return NULL;
1778 }
1779
1780 /*
1781 * Lookup a channel suitable for the given rx status.
1782 *
1783 * This is used to find a channel for a frame (eg beacon, probe
1784 * response) based purely on the received PHY information.
1785 *
1786 * For now it tries to do it based on R_FREQ / R_IEEE.
1787 * This is enough for 11bg and 11a (and thus 11ng/11na)
1788 * but it will not be enough for GSM, PSB channels and the
1789 * like. It also doesn't know about legacy-turbog and
1790 * legacy-turbo modes, which some offload NICs actually
1791 * support in weird ways.
1792 *
1793 * Takes the ic and rxstatus; returns the channel or NULL
1794 * if not found.
1795 *
1796 * XXX TODO: Add support for that when the need arises.
1797 */
1798 struct ieee80211_channel *
1799 ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1800 const struct ieee80211_rx_stats *rxs)
1801 {
1802 struct ieee80211com *ic = vap->iv_ic;
1803 uint32_t flags;
1804 struct ieee80211_channel *c;
1805
1806 if (rxs == NULL)
1807 return (NULL);
1808
1809 /*
1810 * Strictly speaking we only use freq for now,
1811 * however later on we may wish to just store
1812 * the ieee for verification.
1813 */
1814 if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1815 return (NULL);
1816 if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1817 return (NULL);
1818
1819 /*
1820 * If the rx status contains a valid ieee/freq, then
1821 * ensure we populate the correct channel information
1822 * in rxchan before passing it up to the scan infrastructure.
1823 * Offload NICs will pass up beacons from all channels
1824 * during background scans.
1825 */
1826
1827 /* Determine a band */
1828 /* XXX should be done by the driver? */
1829 if (rxs->c_freq < 3000) {
1830 flags = IEEE80211_CHAN_G;
1831 } else {
1832 flags = IEEE80211_CHAN_A;
1833 }
1834
1835 /* Channel lookup */
1836 c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1837
1838 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1839 "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1840 __func__,
1841 (int) rxs->c_freq,
1842 (int) rxs->c_ieee,
1843 flags,
1844 c);
1845
1846 return (c);
1847 }
1848
1849 static void
1850 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1851 {
1852 #define ADD(_ic, _s, _o) \
1853 ifmedia_add(media, \
1854 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1855 static const u_int mopts[IEEE80211_MODE_MAX] = {
1856 [IEEE80211_MODE_AUTO] = IFM_AUTO,
1857 [IEEE80211_MODE_11A] = IFM_IEEE80211_11A,
1858 [IEEE80211_MODE_11B] = IFM_IEEE80211_11B,
1859 [IEEE80211_MODE_11G] = IFM_IEEE80211_11G,
1860 [IEEE80211_MODE_FH] = IFM_IEEE80211_FH,
1861 [IEEE80211_MODE_TURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1862 [IEEE80211_MODE_TURBO_G] = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1863 [IEEE80211_MODE_STURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1864 [IEEE80211_MODE_HALF] = IFM_IEEE80211_11A, /* XXX */
1865 [IEEE80211_MODE_QUARTER] = IFM_IEEE80211_11A, /* XXX */
1866 [IEEE80211_MODE_11NA] = IFM_IEEE80211_11NA,
1867 [IEEE80211_MODE_11NG] = IFM_IEEE80211_11NG,
1868 [IEEE80211_MODE_VHT_2GHZ] = IFM_IEEE80211_VHT2G,
1869 [IEEE80211_MODE_VHT_5GHZ] = IFM_IEEE80211_VHT5G,
1870 };
1871 u_int mopt;
1872
1873 mopt = mopts[mode];
1874 if (addsta)
1875 ADD(ic, mword, mopt); /* STA mode has no cap */
1876 if (caps & IEEE80211_C_IBSS)
1877 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1878 if (caps & IEEE80211_C_HOSTAP)
1879 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1880 if (caps & IEEE80211_C_AHDEMO)
1881 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1882 if (caps & IEEE80211_C_MONITOR)
1883 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1884 if (caps & IEEE80211_C_WDS)
1885 ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1886 if (caps & IEEE80211_C_MBSS)
1887 ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1888 #undef ADD
1889 }
1890
1891 /*
1892 * Setup the media data structures according to the channel and
1893 * rate tables.
1894 */
1895 static int
1896 ieee80211_media_setup(struct ieee80211com *ic,
1897 struct ifmedia *media, int caps, int addsta,
1898 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1899 {
1900 int i, j, rate, maxrate, mword, r;
1901 enum ieee80211_phymode mode;
1902 const struct ieee80211_rateset *rs;
1903 struct ieee80211_rateset allrates;
1904
1905 /*
1906 * Fill in media characteristics.
1907 */
1908 ifmedia_init(media, 0, media_change, media_stat);
1909 maxrate = 0;
1910 /*
1911 * Add media for legacy operating modes.
1912 */
1913 memset(&allrates, 0, sizeof(allrates));
1914 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1915 if (isclr(ic->ic_modecaps, mode))
1916 continue;
1917 addmedia(media, caps, addsta, mode, IFM_AUTO);
1918 if (mode == IEEE80211_MODE_AUTO)
1919 continue;
1920 rs = &ic->ic_sup_rates[mode];
1921 for (i = 0; i < rs->rs_nrates; i++) {
1922 rate = rs->rs_rates[i];
1923 mword = ieee80211_rate2media(ic, rate, mode);
1924 if (mword == 0)
1925 continue;
1926 addmedia(media, caps, addsta, mode, mword);
1927 /*
1928 * Add legacy rate to the collection of all rates.
1929 */
1930 r = rate & IEEE80211_RATE_VAL;
1931 for (j = 0; j < allrates.rs_nrates; j++)
1932 if (allrates.rs_rates[j] == r)
1933 break;
1934 if (j == allrates.rs_nrates) {
1935 /* unique, add to the set */
1936 allrates.rs_rates[j] = r;
1937 allrates.rs_nrates++;
1938 }
1939 rate = (rate & IEEE80211_RATE_VAL) / 2;
1940 if (rate > maxrate)
1941 maxrate = rate;
1942 }
1943 }
1944 for (i = 0; i < allrates.rs_nrates; i++) {
1945 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1946 IEEE80211_MODE_AUTO);
1947 if (mword == 0)
1948 continue;
1949 /* NB: remove media options from mword */
1950 addmedia(media, caps, addsta,
1951 IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1952 }
1953 /*
1954 * Add HT/11n media. Note that we do not have enough
1955 * bits in the media subtype to express the MCS so we
1956 * use a "placeholder" media subtype and any fixed MCS
1957 * must be specified with a different mechanism.
1958 */
1959 for (; mode <= IEEE80211_MODE_11NG; mode++) {
1960 if (isclr(ic->ic_modecaps, mode))
1961 continue;
1962 addmedia(media, caps, addsta, mode, IFM_AUTO);
1963 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1964 }
1965 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1966 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1967 addmedia(media, caps, addsta,
1968 IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1969 i = ic->ic_txstream * 8 - 1;
1970 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1971 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1972 rate = ieee80211_htrates[i].ht40_rate_400ns;
1973 else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1974 rate = ieee80211_htrates[i].ht40_rate_800ns;
1975 else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1976 rate = ieee80211_htrates[i].ht20_rate_400ns;
1977 else
1978 rate = ieee80211_htrates[i].ht20_rate_800ns;
1979 if (rate > maxrate)
1980 maxrate = rate;
1981 }
1982
1983 /*
1984 * Add VHT media.
1985 */
1986 for (; mode <= IEEE80211_MODE_VHT_5GHZ; mode++) {
1987 if (isclr(ic->ic_modecaps, mode))
1988 continue;
1989 addmedia(media, caps, addsta, mode, IFM_AUTO);
1990 addmedia(media, caps, addsta, mode, IFM_IEEE80211_VHT);
1991
1992 /* XXX TODO: VHT maxrate */
1993 }
1994
1995 return maxrate;
1996 }
1997
1998 /* XXX inline or eliminate? */
1999 const struct ieee80211_rateset *
2000 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
2001 {
2002 /* XXX does this work for 11ng basic rates? */
2003 return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
2004 }
2005
2006 /* XXX inline or eliminate? */
2007 const struct ieee80211_htrateset *
2008 ieee80211_get_suphtrates(struct ieee80211com *ic,
2009 const struct ieee80211_channel *c)
2010 {
2011 return &ic->ic_sup_htrates;
2012 }
2013
2014 void
2015 ieee80211_media_init(struct ieee80211com *ic,
2016 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
2017 {
2018
2019 ieee80211_media_init_with_lock(ic, media_change, media_stat, NULL);
2020 }
2021
2022 void
2023 ieee80211_announce(struct ieee80211com *ic)
2024 {
2025 int i, rate, mword;
2026 enum ieee80211_phymode mode;
2027 const struct ieee80211_rateset *rs;
2028
2029 /* NB: skip AUTO since it has no rates */
2030 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
2031 if (isclr(ic->ic_modecaps, mode))
2032 continue;
2033 aprint_debug("%s: %s rates: ", ifp->if_xname,
2034 ieee80211_phymode_name[mode]);
2035 ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
2036 rs = &ic->ic_sup_rates[mode];
2037 for (i = 0; i < rs->rs_nrates; i++) {
2038 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
2039 if (mword == 0)
2040 continue;
2041 rate = ieee80211_media2rate(mword);
2042 aprint_debug("%s%d%sMbps", (i != 0 ? " " : ""),
2043 rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
2044 }
2045 aprint_debug("\n");
2046 }
2047 ieee80211_ht_announce(ic);
2048 ieee80211_vht_announce(ic);
2049 }
2050
2051 void
2052 ieee80211_announce_channels(struct ieee80211com *ic)
2053 {
2054 const struct ieee80211_channel *c;
2055 char type;
2056 int i, cw;
2057
2058 printf("Chan Freq CW RegPwr MinPwr MaxPwr\n");
2059 for (i = 0; i < ic->ic_nchans; i++) {
2060 c = &ic->ic_channels[i];
2061 if (IEEE80211_IS_CHAN_ST(c))
2062 type = 'S';
2063 else if (IEEE80211_IS_CHAN_108A(c))
2064 type = 'T';
2065 else if (IEEE80211_IS_CHAN_108G(c))
2066 type = 'G';
2067 else if (IEEE80211_IS_CHAN_HT(c))
2068 type = 'n';
2069 else if (IEEE80211_IS_CHAN_A(c))
2070 type = 'a';
2071 else if (IEEE80211_IS_CHAN_ANYG(c))
2072 type = 'g';
2073 else if (IEEE80211_IS_CHAN_B(c))
2074 type = 'b';
2075 else
2076 type = 'f';
2077 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
2078 cw = 40;
2079 else if (IEEE80211_IS_CHAN_HALF(c))
2080 cw = 10;
2081 else if (IEEE80211_IS_CHAN_QUARTER(c))
2082 cw = 5;
2083 else
2084 cw = 20;
2085 printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
2086 , c->ic_ieee, c->ic_freq, type
2087 , cw
2088 , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
2089 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
2090 , c->ic_maxregpower
2091 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
2092 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
2093 );
2094 }
2095 }
2096
2097 static int
2098 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
2099 {
2100 switch (IFM_MODE(ime->ifm_media)) {
2101 case IFM_IEEE80211_11A:
2102 *mode = IEEE80211_MODE_11A;
2103 break;
2104 case IFM_IEEE80211_11B:
2105 *mode = IEEE80211_MODE_11B;
2106 break;
2107 case IFM_IEEE80211_11G:
2108 *mode = IEEE80211_MODE_11G;
2109 break;
2110 case IFM_IEEE80211_FH:
2111 *mode = IEEE80211_MODE_FH;
2112 break;
2113 case IFM_IEEE80211_11NA:
2114 *mode = IEEE80211_MODE_11NA;
2115 break;
2116 case IFM_IEEE80211_11NG:
2117 *mode = IEEE80211_MODE_11NG;
2118 break;
2119 case IFM_AUTO:
2120 *mode = IEEE80211_MODE_AUTO;
2121 break;
2122 default:
2123 return 0;
2124 }
2125 /*
2126 * Turbo mode is an ``option''.
2127 * XXX does not apply to AUTO
2128 */
2129 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
2130 if (*mode == IEEE80211_MODE_11A) {
2131 if (flags & IEEE80211_F_TURBOP)
2132 *mode = IEEE80211_MODE_TURBO_A;
2133 else
2134 *mode = IEEE80211_MODE_STURBO_A;
2135 } else if (*mode == IEEE80211_MODE_11G)
2136 *mode = IEEE80211_MODE_TURBO_G;
2137 else
2138 return 0;
2139 }
2140 /* XXX HT40 +/- */
2141 return 1;
2142 }
2143
2144 /*
2145 * Handle a media change request on the vap interface.
2146 */
2147 int
2148 ieee80211_media_change(struct ifnet *ifp)
2149 {
2150 struct ieee80211vap *vap = ifp->if_softc;
2151 struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
2152 uint16_t newmode;
2153
2154 if (!media2mode(ime, vap->iv_flags, &newmode))
2155 return EINVAL;
2156 if (vap->iv_des_mode != newmode) {
2157 vap->iv_des_mode = newmode;
2158 /* XXX kick state machine if up+running */
2159 }
2160 return 0;
2161 }
2162
2163 /*
2164 * Common code to calculate the media status word
2165 * from the operating mode and channel state.
2166 */
2167 static int
2168 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
2169 {
2170 int status;
2171
2172 status = IFM_IEEE80211;
2173 switch (opmode) {
2174 case IEEE80211_M_STA:
2175 break;
2176 case IEEE80211_M_IBSS:
2177 status |= IFM_IEEE80211_ADHOC;
2178 break;
2179 case IEEE80211_M_HOSTAP:
2180 status |= IFM_IEEE80211_HOSTAP;
2181 break;
2182 case IEEE80211_M_MONITOR:
2183 status |= IFM_IEEE80211_MONITOR;
2184 break;
2185 case IEEE80211_M_AHDEMO:
2186 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
2187 break;
2188 case IEEE80211_M_WDS:
2189 status |= IFM_IEEE80211_WDS;
2190 break;
2191 case IEEE80211_M_MBSS:
2192 status |= IFM_IEEE80211_MBSS;
2193 break;
2194 }
2195 if (IEEE80211_IS_CHAN_HTA(chan)) {
2196 status |= IFM_IEEE80211_11NA;
2197 } else if (IEEE80211_IS_CHAN_HTG(chan)) {
2198 status |= IFM_IEEE80211_11NG;
2199 } else if (IEEE80211_IS_CHAN_A(chan)) {
2200 status |= IFM_IEEE80211_11A;
2201 } else if (IEEE80211_IS_CHAN_B(chan)) {
2202 status |= IFM_IEEE80211_11B;
2203 } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
2204 status |= IFM_IEEE80211_11G;
2205 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
2206 status |= IFM_IEEE80211_FH;
2207 }
2208 /* XXX else complain? */
2209
2210 if (IEEE80211_IS_CHAN_TURBO(chan))
2211 status |= IFM_IEEE80211_TURBO;
2212 #if 0
2213 if (IEEE80211_IS_CHAN_HT20(chan))
2214 status |= IFM_IEEE80211_HT20;
2215 if (IEEE80211_IS_CHAN_HT40(chan))
2216 status |= IFM_IEEE80211_HT40;
2217 #endif
2218 return status;
2219 }
2220
2221 void
2222 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
2223 {
2224 struct ieee80211vap *vap = ifp->if_softc;
2225 struct ieee80211com *ic = vap->iv_ic;
2226 enum ieee80211_phymode mode;
2227
2228 imr->ifm_status = IFM_AVALID;
2229 /*
2230 * NB: use the current channel's mode to lock down a xmit
2231 * rate only when running; otherwise we may have a mismatch
2232 * in which case the rate will not be convertible.
2233 */
2234 if (vap->iv_state == IEEE80211_S_RUN ||
2235 vap->iv_state == IEEE80211_S_SLEEP) {
2236 imr->ifm_status |= IFM_ACTIVE;
2237 mode = ieee80211_chan2mode(ic->ic_curchan);
2238 } else
2239 mode = IEEE80211_MODE_AUTO;
2240 imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
2241 /*
2242 * Calculate a current rate if possible.
2243 */
2244 if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
2245 /*
2246 * A fixed rate is set, report that.
2247 */
2248 imr->ifm_active |= ieee80211_rate2media(ic,
2249 vap->iv_txparms[mode].ucastrate, mode);
2250 } else if (vap->iv_opmode == IEEE80211_M_STA) {
2251 /*
2252 * In station mode report the current transmit rate.
2253 */
2254 imr->ifm_active |= ieee80211_rate2media(ic,
2255 vap->iv_bss->ni_txrate, mode);
2256 } else
2257 imr->ifm_active |= IFM_AUTO;
2258 if (imr->ifm_status & IFM_ACTIVE)
2259 imr->ifm_current = imr->ifm_active;
2260 }
2261
2262 /*
2263 * Set the current phy mode and recalculate the active channel
2264 * set based on the available channels for this mode. Also
2265 * select a new default/current channel if the current one is
2266 * inappropriate for this mode.
2267 */
2268 int
2269 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
2270 {
2271 /*
2272 * Adjust basic rates in 11b/11g supported rate set.
2273 * Note that if operating on a hal/quarter rate channel
2274 * this is a noop as those rates sets are different
2275 * and used instead.
2276 */
2277 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
2278 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
2279
2280 ic->ic_curmode = mode;
2281 ieee80211_reset_erp(ic); /* reset ERP state */
2282
2283 return 0;
2284 }
2285
2286 /*
2287 * Return the phy mode for with the specified channel.
2288 */
2289 enum ieee80211_phymode
2290 ieee80211_chan2mode(const struct ieee80211_channel *chan)
2291 {
2292
2293 if (IEEE80211_IS_CHAN_VHT_2GHZ(chan))
2294 return IEEE80211_MODE_VHT_2GHZ;
2295 else if (IEEE80211_IS_CHAN_VHT_5GHZ(chan))
2296 return IEEE80211_MODE_VHT_5GHZ;
2297 else if (IEEE80211_IS_CHAN_HTA(chan))
2298 return IEEE80211_MODE_11NA;
2299 else if (IEEE80211_IS_CHAN_HTG(chan))
2300 return IEEE80211_MODE_11NG;
2301 else if (IEEE80211_IS_CHAN_108G(chan))
2302 return IEEE80211_MODE_TURBO_G;
2303 else if (IEEE80211_IS_CHAN_ST(chan))
2304 return IEEE80211_MODE_STURBO_A;
2305 else if (IEEE80211_IS_CHAN_TURBO(chan))
2306 return IEEE80211_MODE_TURBO_A;
2307 else if (IEEE80211_IS_CHAN_HALF(chan))
2308 return IEEE80211_MODE_HALF;
2309 else if (IEEE80211_IS_CHAN_QUARTER(chan))
2310 return IEEE80211_MODE_QUARTER;
2311 else if (IEEE80211_IS_CHAN_A(chan))
2312 return IEEE80211_MODE_11A;
2313 else if (IEEE80211_IS_CHAN_ANYG(chan))
2314 return IEEE80211_MODE_11G;
2315 else if (IEEE80211_IS_CHAN_B(chan))
2316 return IEEE80211_MODE_11B;
2317 else if (IEEE80211_IS_CHAN_FHSS(chan))
2318 return IEEE80211_MODE_FH;
2319
2320 /* NB: should not get here */
2321 printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
2322 __func__, chan->ic_freq, chan->ic_flags);
2323 return IEEE80211_MODE_11B;
2324 }
2325
2326 struct ratemedia {
2327 u_int match; /* rate + mode */
2328 u_int media; /* if_media rate */
2329 };
2330
2331 static int
2332 findmedia(const struct ratemedia rates[], int n, u_int match)
2333 {
2334 int i;
2335
2336 for (i = 0; i < n; i++)
2337 if (rates[i].match == match)
2338 return rates[i].media;
2339 return IFM_AUTO;
2340 }
2341
2342 /*
2343 * Convert IEEE80211 rate value to ifmedia subtype.
2344 * Rate is either a legacy rate in units of 0.5Mbps
2345 * or an MCS index.
2346 */
2347 int
2348 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
2349 {
2350 static const struct ratemedia rates[] = {
2351 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
2352 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
2353 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
2354 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
2355 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
2356 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
2357 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
2358 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
2359 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
2360 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
2361 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
2362 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
2363 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
2364 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
2365 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
2366 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
2367 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
2368 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
2369 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
2370 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
2371 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
2372 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
2373 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
2374 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
2375 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
2376 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
2377 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
2378 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
2379 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
2380 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
2381 /* NB: OFDM72 doesn't really exist so we don't handle it */
2382 };
2383 static const struct ratemedia htrates[] = {
2384 { 0, IFM_IEEE80211_MCS },
2385 { 1, IFM_IEEE80211_MCS },
2386 { 2, IFM_IEEE80211_MCS },
2387 { 3, IFM_IEEE80211_MCS },
2388 { 4, IFM_IEEE80211_MCS },
2389 { 5, IFM_IEEE80211_MCS },
2390 { 6, IFM_IEEE80211_MCS },
2391 { 7, IFM_IEEE80211_MCS },
2392 { 8, IFM_IEEE80211_MCS },
2393 { 9, IFM_IEEE80211_MCS },
2394 { 10, IFM_IEEE80211_MCS },
2395 { 11, IFM_IEEE80211_MCS },
2396 { 12, IFM_IEEE80211_MCS },
2397 { 13, IFM_IEEE80211_MCS },
2398 { 14, IFM_IEEE80211_MCS },
2399 { 15, IFM_IEEE80211_MCS },
2400 { 16, IFM_IEEE80211_MCS },
2401 { 17, IFM_IEEE80211_MCS },
2402 { 18, IFM_IEEE80211_MCS },
2403 { 19, IFM_IEEE80211_MCS },
2404 { 20, IFM_IEEE80211_MCS },
2405 { 21, IFM_IEEE80211_MCS },
2406 { 22, IFM_IEEE80211_MCS },
2407 { 23, IFM_IEEE80211_MCS },
2408 { 24, IFM_IEEE80211_MCS },
2409 { 25, IFM_IEEE80211_MCS },
2410 { 26, IFM_IEEE80211_MCS },
2411 { 27, IFM_IEEE80211_MCS },
2412 { 28, IFM_IEEE80211_MCS },
2413 { 29, IFM_IEEE80211_MCS },
2414 { 30, IFM_IEEE80211_MCS },
2415 { 31, IFM_IEEE80211_MCS },
2416 { 32, IFM_IEEE80211_MCS },
2417 { 33, IFM_IEEE80211_MCS },
2418 { 34, IFM_IEEE80211_MCS },
2419 { 35, IFM_IEEE80211_MCS },
2420 { 36, IFM_IEEE80211_MCS },
2421 { 37, IFM_IEEE80211_MCS },
2422 { 38, IFM_IEEE80211_MCS },
2423 { 39, IFM_IEEE80211_MCS },
2424 { 40, IFM_IEEE80211_MCS },
2425 { 41, IFM_IEEE80211_MCS },
2426 { 42, IFM_IEEE80211_MCS },
2427 { 43, IFM_IEEE80211_MCS },
2428 { 44, IFM_IEEE80211_MCS },
2429 { 45, IFM_IEEE80211_MCS },
2430 { 46, IFM_IEEE80211_MCS },
2431 { 47, IFM_IEEE80211_MCS },
2432 { 48, IFM_IEEE80211_MCS },
2433 { 49, IFM_IEEE80211_MCS },
2434 { 50, IFM_IEEE80211_MCS },
2435 { 51, IFM_IEEE80211_MCS },
2436 { 52, IFM_IEEE80211_MCS },
2437 { 53, IFM_IEEE80211_MCS },
2438 { 54, IFM_IEEE80211_MCS },
2439 { 55, IFM_IEEE80211_MCS },
2440 { 56, IFM_IEEE80211_MCS },
2441 { 57, IFM_IEEE80211_MCS },
2442 { 58, IFM_IEEE80211_MCS },
2443 { 59, IFM_IEEE80211_MCS },
2444 { 60, IFM_IEEE80211_MCS },
2445 { 61, IFM_IEEE80211_MCS },
2446 { 62, IFM_IEEE80211_MCS },
2447 { 63, IFM_IEEE80211_MCS },
2448 { 64, IFM_IEEE80211_MCS },
2449 { 65, IFM_IEEE80211_MCS },
2450 { 66, IFM_IEEE80211_MCS },
2451 { 67, IFM_IEEE80211_MCS },
2452 { 68, IFM_IEEE80211_MCS },
2453 { 69, IFM_IEEE80211_MCS },
2454 { 70, IFM_IEEE80211_MCS },
2455 { 71, IFM_IEEE80211_MCS },
2456 { 72, IFM_IEEE80211_MCS },
2457 { 73, IFM_IEEE80211_MCS },
2458 { 74, IFM_IEEE80211_MCS },
2459 { 75, IFM_IEEE80211_MCS },
2460 { 76, IFM_IEEE80211_MCS },
2461 };
2462 int m;
2463
2464 /*
2465 * Check 11n rates first for match as an MCS.
2466 */
2467 if (mode == IEEE80211_MODE_11NA) {
2468 if (rate & IEEE80211_RATE_MCS) {
2469 rate &= ~IEEE80211_RATE_MCS;
2470 m = findmedia(htrates, nitems(htrates), rate);
2471 if (m != IFM_AUTO)
2472 return m | IFM_IEEE80211_11NA;
2473 }
2474 } else if (mode == IEEE80211_MODE_11NG) {
2475 /* NB: 12 is ambiguous, it will be treated as an MCS */
2476 if (rate & IEEE80211_RATE_MCS) {
2477 rate &= ~IEEE80211_RATE_MCS;
2478 m = findmedia(htrates, nitems(htrates), rate);
2479 if (m != IFM_AUTO)
2480 return m | IFM_IEEE80211_11NG;
2481 }
2482 }
2483 rate &= IEEE80211_RATE_VAL;
2484 switch (mode) {
2485 case IEEE80211_MODE_11A:
2486 case IEEE80211_MODE_HALF: /* XXX good 'nuf */
2487 case IEEE80211_MODE_QUARTER:
2488 case IEEE80211_MODE_11NA:
2489 case IEEE80211_MODE_TURBO_A:
2490 case IEEE80211_MODE_STURBO_A:
2491 return findmedia(rates, nitems(rates),
2492 rate | IFM_IEEE80211_11A);
2493 case IEEE80211_MODE_11B:
2494 return findmedia(rates, nitems(rates),
2495 rate | IFM_IEEE80211_11B);
2496 case IEEE80211_MODE_FH:
2497 return findmedia(rates, nitems(rates),
2498 rate | IFM_IEEE80211_FH);
2499 case IEEE80211_MODE_AUTO:
2500 /* NB: ic may be NULL for some drivers */
2501 if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
2502 return findmedia(rates, nitems(rates),
2503 rate | IFM_IEEE80211_FH);
2504 /* NB: hack, 11g matches both 11b+11a rates */
2505 /* fall thru... */
2506 case IEEE80211_MODE_11G:
2507 case IEEE80211_MODE_11NG:
2508 case IEEE80211_MODE_TURBO_G:
2509 return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
2510 case IEEE80211_MODE_VHT_2GHZ:
2511 case IEEE80211_MODE_VHT_5GHZ:
2512 /* XXX TODO: need to figure out mapping for VHT rates */
2513 return IFM_AUTO;
2514 }
2515 return IFM_AUTO;
2516 }
2517
2518 int
2519 ieee80211_media2rate(int mword)
2520 {
2521 static const int ieeerates[] = {
2522 -1, /* IFM_AUTO */
2523 0, /* IFM_MANUAL */
2524 0, /* IFM_NONE */
2525 2, /* IFM_IEEE80211_FH1 */
2526 4, /* IFM_IEEE80211_FH2 */
2527 2, /* IFM_IEEE80211_DS1 */
2528 4, /* IFM_IEEE80211_DS2 */
2529 11, /* IFM_IEEE80211_DS5 */
2530 22, /* IFM_IEEE80211_DS11 */
2531 44, /* IFM_IEEE80211_DS22 */
2532 12, /* IFM_IEEE80211_OFDM6 */
2533 18, /* IFM_IEEE80211_OFDM9 */
2534 24, /* IFM_IEEE80211_OFDM12 */
2535 36, /* IFM_IEEE80211_OFDM18 */
2536 48, /* IFM_IEEE80211_OFDM24 */
2537 72, /* IFM_IEEE80211_OFDM36 */
2538 96, /* IFM_IEEE80211_OFDM48 */
2539 108, /* IFM_IEEE80211_OFDM54 */
2540 144, /* IFM_IEEE80211_OFDM72 */
2541 0, /* IFM_IEEE80211_DS354k */
2542 0, /* IFM_IEEE80211_DS512k */
2543 6, /* IFM_IEEE80211_OFDM3 */
2544 9, /* IFM_IEEE80211_OFDM4 */
2545 54, /* IFM_IEEE80211_OFDM27 */
2546 -1, /* IFM_IEEE80211_MCS */
2547 -1, /* IFM_IEEE80211_VHT */
2548 };
2549 return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
2550 ieeerates[IFM_SUBTYPE(mword)] : 0;
2551 }
2552
2553 /*
2554 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2555 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2556 */
2557 #define mix(a, b, c) \
2558 do { \
2559 a -= b; a -= c; a ^= (c >> 13); \
2560 b -= c; b -= a; b ^= (a << 8); \
2561 c -= a; c -= b; c ^= (b >> 13); \
2562 a -= b; a -= c; a ^= (c >> 12); \
2563 b -= c; b -= a; b ^= (a << 16); \
2564 c -= a; c -= b; c ^= (b >> 5); \
2565 a -= b; a -= c; a ^= (c >> 3); \
2566 b -= c; b -= a; b ^= (a << 10); \
2567 c -= a; c -= b; c ^= (b >> 15); \
2568 } while (/*CONSTCOND*/0)
2569
2570 uint32_t
2571 ieee80211_mac_hash(const struct ieee80211com *ic,
2572 const uint8_t addr[IEEE80211_ADDR_LEN])
2573 {
2574 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
2575
2576 b += addr[5] << 8;
2577 b += addr[4];
2578 a += addr[3] << 24;
2579 a += addr[2] << 16;
2580 a += addr[1] << 8;
2581 a += addr[0];
2582
2583 mix(a, b, c);
2584
2585 return c;
2586 }
2587 #undef mix
2588
2589 char
2590 ieee80211_channel_type_char(const struct ieee80211_channel *c)
2591 {
2592 if (IEEE80211_IS_CHAN_ST(c))
2593 return 'S';
2594 if (IEEE80211_IS_CHAN_108A(c))
2595 return 'T';
2596 if (IEEE80211_IS_CHAN_108G(c))
2597 return 'G';
2598 if (IEEE80211_IS_CHAN_VHT(c))
2599 return 'v';
2600 if (IEEE80211_IS_CHAN_HT(c))
2601 return 'n';
2602 if (IEEE80211_IS_CHAN_A(c))
2603 return 'a';
2604 if (IEEE80211_IS_CHAN_ANYG(c))
2605 return 'g';
2606 if (IEEE80211_IS_CHAN_B(c))
2607 return 'b';
2608 return 'f';
2609 }
2610