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