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