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