1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2001 Atsushi Onoe
5 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 /*
31 * IEEE 802.11 generic handler
32 */
33 #include "opt_wlan.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/socket.h>
40 #include <sys/sbuf.h>
41
42 #include <machine/stdarg.h>
43
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/if_dl.h>
47 #include <net/if_media.h>
48 #include <net/if_private.h>
49 #include <net/if_types.h>
50 #include <net/ethernet.h>
51
52 #include <net80211/ieee80211_var.h>
53 #include <net80211/ieee80211_regdomain.h>
54 #ifdef IEEE80211_SUPPORT_SUPERG
55 #include <net80211/ieee80211_superg.h>
56 #endif
57 #include <net80211/ieee80211_ratectl.h>
58 #include <net80211/ieee80211_vht.h>
59
60 #include <net/bpf.h>
61
62 const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
63 [IEEE80211_MODE_AUTO] = "auto",
64 [IEEE80211_MODE_11A] = "11a",
65 [IEEE80211_MODE_11B] = "11b",
66 [IEEE80211_MODE_11G] = "11g",
67 [IEEE80211_MODE_FH] = "FH",
68 [IEEE80211_MODE_TURBO_A] = "turboA",
69 [IEEE80211_MODE_TURBO_G] = "turboG",
70 [IEEE80211_MODE_STURBO_A] = "sturboA",
71 [IEEE80211_MODE_HALF] = "half",
72 [IEEE80211_MODE_QUARTER] = "quarter",
73 [IEEE80211_MODE_11NA] = "11na",
74 [IEEE80211_MODE_11NG] = "11ng",
75 [IEEE80211_MODE_VHT_2GHZ] = "11acg",
76 [IEEE80211_MODE_VHT_5GHZ] = "11ac",
77 };
78 /* map ieee80211_opmode to the corresponding capability bit */
79 const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
80 [IEEE80211_M_IBSS] = IEEE80211_C_IBSS,
81 [IEEE80211_M_WDS] = IEEE80211_C_WDS,
82 [IEEE80211_M_STA] = IEEE80211_C_STA,
83 [IEEE80211_M_AHDEMO] = IEEE80211_C_AHDEMO,
84 [IEEE80211_M_HOSTAP] = IEEE80211_C_HOSTAP,
85 [IEEE80211_M_MONITOR] = IEEE80211_C_MONITOR,
86 #ifdef IEEE80211_SUPPORT_MESH
87 [IEEE80211_M_MBSS] = IEEE80211_C_MBSS,
88 #endif
89 };
90
91 const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
92 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
93
94 static void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
95 static void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
96 static void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
97 static void ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag);
98 static int ieee80211_media_setup(struct ieee80211com *ic,
99 struct ifmedia *media, int caps, int addsta,
100 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
101 static int media_status(enum ieee80211_opmode,
102 const struct ieee80211_channel *);
103 static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
104
105 MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
106
107 /*
108 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
109 */
110 #define B(r) ((r) | IEEE80211_RATE_BASIC)
111 static const struct ieee80211_rateset ieee80211_rateset_11a =
112 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
113 static const struct ieee80211_rateset ieee80211_rateset_half =
114 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
115 static const struct ieee80211_rateset ieee80211_rateset_quarter =
116 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
117 static const struct ieee80211_rateset ieee80211_rateset_11b =
118 { 4, { B(2), B(4), B(11), B(22) } };
119 /* NB: OFDM rates are handled specially based on mode */
120 static const struct ieee80211_rateset ieee80211_rateset_11g =
121 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
122 #undef B
123
124 static int set_vht_extchan(struct ieee80211_channel *c);
125
126 /*
127 * Fill in 802.11 available channel set, mark
128 * all available channels as active, and pick
129 * a default channel if not already specified.
130 */
131 void
ieee80211_chan_init(struct ieee80211com * ic)132 ieee80211_chan_init(struct ieee80211com *ic)
133 {
134 #define DEFAULTRATES(m, def) do { \
135 if (ic->ic_sup_rates[m].rs_nrates == 0) \
136 ic->ic_sup_rates[m] = def; \
137 } while (0)
138 struct ieee80211_channel *c;
139 int i;
140
141 KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
142 ("invalid number of channels specified: %u", ic->ic_nchans));
143 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
144 memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
145 setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
146 for (i = 0; i < ic->ic_nchans; i++) {
147 c = &ic->ic_channels[i];
148 KASSERT(c->ic_flags != 0, ("channel with no flags"));
149 /*
150 * Help drivers that work only with frequencies by filling
151 * in IEEE channel #'s if not already calculated. Note this
152 * mimics similar work done in ieee80211_setregdomain when
153 * changing regulatory state.
154 */
155 if (c->ic_ieee == 0)
156 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
157
158 /*
159 * Setup the HT40/VHT40 upper/lower bits.
160 * The VHT80/... math is done elsewhere.
161 */
162 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
163 c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
164 (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
165 c->ic_flags);
166
167 /* Update VHT math */
168 /*
169 * XXX VHT again, note that this assumes VHT80/... channels
170 * are legit already.
171 */
172 set_vht_extchan(c);
173
174 /* default max tx power to max regulatory */
175 if (c->ic_maxpower == 0)
176 c->ic_maxpower = 2*c->ic_maxregpower;
177 setbit(ic->ic_chan_avail, c->ic_ieee);
178 /*
179 * Identify mode capabilities.
180 */
181 if (IEEE80211_IS_CHAN_A(c))
182 setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
183 if (IEEE80211_IS_CHAN_B(c))
184 setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
185 if (IEEE80211_IS_CHAN_ANYG(c))
186 setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
187 if (IEEE80211_IS_CHAN_FHSS(c))
188 setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
189 if (IEEE80211_IS_CHAN_108A(c))
190 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
191 if (IEEE80211_IS_CHAN_108G(c))
192 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
193 if (IEEE80211_IS_CHAN_ST(c))
194 setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
195 if (IEEE80211_IS_CHAN_HALF(c))
196 setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
197 if (IEEE80211_IS_CHAN_QUARTER(c))
198 setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
199 if (IEEE80211_IS_CHAN_HTA(c))
200 setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
201 if (IEEE80211_IS_CHAN_HTG(c))
202 setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
203 if (IEEE80211_IS_CHAN_VHTA(c))
204 setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_5GHZ);
205 if (IEEE80211_IS_CHAN_VHTG(c))
206 setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_2GHZ);
207 }
208 /* initialize candidate channels to all available */
209 memcpy(ic->ic_chan_active, ic->ic_chan_avail,
210 sizeof(ic->ic_chan_avail));
211
212 /* sort channel table to allow lookup optimizations */
213 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
214
215 /* invalidate any previous state */
216 ic->ic_bsschan = IEEE80211_CHAN_ANYC;
217 ic->ic_prevchan = NULL;
218 ic->ic_csa_newchan = NULL;
219 /* arbitrarily pick the first channel */
220 ic->ic_curchan = &ic->ic_channels[0];
221 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
222
223 /* fillin well-known rate sets if driver has not specified */
224 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b);
225 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g);
226 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a);
227 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a);
228 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g);
229 DEFAULTRATES(IEEE80211_MODE_STURBO_A, ieee80211_rateset_11a);
230 DEFAULTRATES(IEEE80211_MODE_HALF, ieee80211_rateset_half);
231 DEFAULTRATES(IEEE80211_MODE_QUARTER, ieee80211_rateset_quarter);
232 DEFAULTRATES(IEEE80211_MODE_11NA, ieee80211_rateset_11a);
233 DEFAULTRATES(IEEE80211_MODE_11NG, ieee80211_rateset_11g);
234 DEFAULTRATES(IEEE80211_MODE_VHT_2GHZ, ieee80211_rateset_11g);
235 DEFAULTRATES(IEEE80211_MODE_VHT_5GHZ, ieee80211_rateset_11a);
236
237 /*
238 * Setup required information to fill the mcsset field, if driver did
239 * not. Assume a 2T2R setup for historic reasons.
240 */
241 if (ic->ic_rxstream == 0)
242 ic->ic_rxstream = 2;
243 if (ic->ic_txstream == 0)
244 ic->ic_txstream = 2;
245
246 ieee80211_init_suphtrates(ic);
247
248 /*
249 * Set auto mode to reset active channel state and any desired channel.
250 */
251 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
252 #undef DEFAULTRATES
253 }
254
255 static void
null_update_mcast(struct ieee80211com * ic)256 null_update_mcast(struct ieee80211com *ic)
257 {
258
259 ic_printf(ic, "need multicast update callback\n");
260 }
261
262 static void
null_update_promisc(struct ieee80211com * ic)263 null_update_promisc(struct ieee80211com *ic)
264 {
265
266 ic_printf(ic, "need promiscuous mode update callback\n");
267 }
268
269 static void
null_update_chw(struct ieee80211com * ic)270 null_update_chw(struct ieee80211com *ic)
271 {
272
273 ic_printf(ic, "%s: need callback\n", __func__);
274 }
275
276 int
ic_printf(struct ieee80211com * ic,const char * fmt,...)277 ic_printf(struct ieee80211com *ic, const char * fmt, ...)
278 {
279 va_list ap;
280 int retval;
281
282 retval = printf("%s: ", ic->ic_name);
283 va_start(ap, fmt);
284 retval += vprintf(fmt, ap);
285 va_end(ap);
286 return (retval);
287 }
288
289 static LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head);
290 static struct mtx ic_list_mtx;
291 MTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF);
292
293 static int
sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS)294 sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS)
295 {
296 struct ieee80211com *ic;
297 struct sbuf sb;
298 char *sp;
299 int error;
300
301 error = sysctl_wire_old_buffer(req, 0);
302 if (error)
303 return (error);
304 sbuf_new_for_sysctl(&sb, NULL, 8, req);
305 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
306 sp = "";
307 mtx_lock(&ic_list_mtx);
308 LIST_FOREACH(ic, &ic_head, ic_next) {
309 sbuf_printf(&sb, "%s%s", sp, ic->ic_name);
310 sp = " ";
311 }
312 mtx_unlock(&ic_list_mtx);
313 error = sbuf_finish(&sb);
314 sbuf_delete(&sb);
315 return (error);
316 }
317
318 SYSCTL_PROC(_net_wlan, OID_AUTO, devices,
319 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
320 sysctl_ieee80211coms, "A", "names of available 802.11 devices");
321
322 /*
323 * Attach/setup the common net80211 state. Called by
324 * the driver on attach to prior to creating any vap's.
325 */
326 void
ieee80211_ifattach(struct ieee80211com * ic)327 ieee80211_ifattach(struct ieee80211com *ic)
328 {
329
330 IEEE80211_LOCK_INIT(ic, ic->ic_name);
331 IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
332 TAILQ_INIT(&ic->ic_vaps);
333
334 /* Create a taskqueue for all state changes */
335 ic->ic_tq = taskqueue_create("ic_taskq",
336 IEEE80211_M_WAITOK | IEEE80211_M_ZERO,
337 taskqueue_thread_enqueue, &ic->ic_tq);
338 taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
339 ic->ic_name);
340 ic->ic_ierrors = counter_u64_alloc(IEEE80211_M_WAITOK);
341 ic->ic_oerrors = counter_u64_alloc(IEEE80211_M_WAITOK);
342 /*
343 * Fill in 802.11 available channel set, mark all
344 * available channels as active, and pick a default
345 * channel if not already specified.
346 */
347 ieee80211_chan_init(ic);
348
349 ic->ic_update_mcast = null_update_mcast;
350 ic->ic_update_promisc = null_update_promisc;
351 ic->ic_update_chw = null_update_chw;
352
353 ic->ic_hash_key = arc4random();
354 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
355 ic->ic_lintval = ic->ic_bintval;
356 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
357
358 ieee80211_crypto_attach(ic);
359 ieee80211_node_attach(ic);
360 ieee80211_power_attach(ic);
361 ieee80211_proto_attach(ic);
362 #ifdef IEEE80211_SUPPORT_SUPERG
363 ieee80211_superg_attach(ic);
364 #endif
365 ieee80211_ht_attach(ic);
366 ieee80211_vht_attach(ic);
367 ieee80211_scan_attach(ic);
368 ieee80211_regdomain_attach(ic);
369 ieee80211_dfs_attach(ic);
370
371 ieee80211_sysctl_attach(ic);
372
373 mtx_lock(&ic_list_mtx);
374 LIST_INSERT_HEAD(&ic_head, ic, ic_next);
375 mtx_unlock(&ic_list_mtx);
376 }
377
378 /*
379 * Detach net80211 state on device detach. Tear down
380 * all vap's and reclaim all common state prior to the
381 * device state going away. Note we may call back into
382 * driver; it must be prepared for this.
383 */
384 void
ieee80211_ifdetach(struct ieee80211com * ic)385 ieee80211_ifdetach(struct ieee80211com *ic)
386 {
387 struct ieee80211vap *vap;
388
389 /*
390 * We use this as an indicator that ifattach never had a chance to be
391 * called, e.g. early driver attach failed and ifdetach was called
392 * during subsequent detach. Never fear, for we have nothing to do
393 * here.
394 */
395 if (ic->ic_tq == NULL)
396 return;
397
398 mtx_lock(&ic_list_mtx);
399 LIST_REMOVE(ic, ic_next);
400 mtx_unlock(&ic_list_mtx);
401
402 taskqueue_drain(taskqueue_thread, &ic->ic_restart_task);
403
404 /*
405 * The VAP is responsible for setting and clearing
406 * the VIMAGE context.
407 */
408 while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL) {
409 ieee80211_com_vdetach(vap);
410 ieee80211_vap_destroy(vap);
411 }
412 ieee80211_waitfor_parent(ic);
413
414 ieee80211_sysctl_detach(ic);
415 ieee80211_dfs_detach(ic);
416 ieee80211_regdomain_detach(ic);
417 ieee80211_scan_detach(ic);
418 #ifdef IEEE80211_SUPPORT_SUPERG
419 ieee80211_superg_detach(ic);
420 #endif
421 ieee80211_vht_detach(ic);
422 ieee80211_ht_detach(ic);
423 /* NB: must be called before ieee80211_node_detach */
424 ieee80211_proto_detach(ic);
425 ieee80211_crypto_detach(ic);
426 ieee80211_power_detach(ic);
427 ieee80211_node_detach(ic);
428
429 counter_u64_free(ic->ic_ierrors);
430 counter_u64_free(ic->ic_oerrors);
431
432 taskqueue_free(ic->ic_tq);
433 IEEE80211_TX_LOCK_DESTROY(ic);
434 IEEE80211_LOCK_DESTROY(ic);
435 }
436
437 /*
438 * Called by drivers during attach to set the supported
439 * cipher set for software encryption.
440 */
441 void
ieee80211_set_software_ciphers(struct ieee80211com * ic,uint32_t cipher_suite)442 ieee80211_set_software_ciphers(struct ieee80211com *ic,
443 uint32_t cipher_suite)
444 {
445 ieee80211_crypto_set_supported_software_ciphers(ic, cipher_suite);
446 }
447
448 /*
449 * Called by drivers during attach to set the supported
450 * cipher set for hardware encryption.
451 */
452 void
ieee80211_set_hardware_ciphers(struct ieee80211com * ic,uint32_t cipher_suite)453 ieee80211_set_hardware_ciphers(struct ieee80211com *ic,
454 uint32_t cipher_suite)
455 {
456 ieee80211_crypto_set_supported_hardware_ciphers(ic, cipher_suite);
457 }
458
459 /*
460 * Called by drivers during attach to set the supported
461 * key management suites by the driver/hardware.
462 */
463 void
ieee80211_set_driver_keymgmt_suites(struct ieee80211com * ic,uint32_t keymgmt_set)464 ieee80211_set_driver_keymgmt_suites(struct ieee80211com *ic,
465 uint32_t keymgmt_set)
466 {
467 ieee80211_crypto_set_supported_driver_keymgmt(ic,
468 keymgmt_set);
469 }
470
471 struct ieee80211com *
ieee80211_find_com(const char * name)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
ieee80211_iterate_coms(ieee80211_com_iter_func * f,void * arg)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
default_reset(struct ieee80211vap * vap,u_long cmd)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
default_update_deftxkey(struct ieee80211vap * vap,ieee80211_keyix kid)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 uint64_t
ieee80211_get_counter(struct ifnet * ifp,ift_counter cnt)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
ieee80211_vap_setup(struct ieee80211com * ic,struct ieee80211vap * vap,const char name[IFNAMSIZ],int unit,enum ieee80211_opmode opmode,int flags,const uint8_t bssid[IEEE80211_ADDR_LEN])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_initname(ifp, name, unit);
566 ifp->if_softc = vap; /* back pointer */
567 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
568 ifp->if_transmit = ieee80211_vap_transmit;
569 ifp->if_qflush = ieee80211_vap_qflush;
570 ifp->if_ioctl = ieee80211_ioctl;
571 ifp->if_init = ieee80211_init;
572 ifp->if_get_counter = ieee80211_get_counter;
573
574 vap->iv_ifp = ifp;
575 vap->iv_ic = ic;
576 vap->iv_flags = ic->ic_flags; /* propagate common flags */
577 vap->iv_flags_ext = ic->ic_flags_ext;
578 vap->iv_flags_ven = ic->ic_flags_ven;
579 vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
580
581 /* 11n capabilities - XXX methodize */
582 vap->iv_htcaps = ic->ic_htcaps;
583 vap->iv_htextcaps = ic->ic_htextcaps;
584
585 /* 11ac capabilities - XXX methodize */
586 vap->iv_vht_cap.vht_cap_info = ic->ic_vht_cap.vht_cap_info;
587 vap->iv_vhtextcaps = ic->ic_vhtextcaps;
588
589 vap->iv_opmode = opmode;
590 vap->iv_caps |= ieee80211_opcap[opmode];
591 IEEE80211_ADDR_COPY(vap->iv_myaddr, ic->ic_macaddr);
592 switch (opmode) {
593 case IEEE80211_M_WDS:
594 /*
595 * WDS links must specify the bssid of the far end.
596 * For legacy operation this is a static relationship.
597 * For non-legacy operation the station must associate
598 * and be authorized to pass traffic. Plumbing the
599 * vap to the proper node happens when the vap
600 * transitions to RUN state.
601 */
602 IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
603 vap->iv_flags |= IEEE80211_F_DESBSSID;
604 if (flags & IEEE80211_CLONE_WDSLEGACY)
605 vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
606 break;
607 #ifdef IEEE80211_SUPPORT_TDMA
608 case IEEE80211_M_AHDEMO:
609 if (flags & IEEE80211_CLONE_TDMA) {
610 /* NB: checked before clone operation allowed */
611 KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
612 ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
613 /*
614 * Propagate TDMA capability to mark vap; this
615 * cannot be removed and is used to distinguish
616 * regular ahdemo operation from ahdemo+tdma.
617 */
618 vap->iv_caps |= IEEE80211_C_TDMA;
619 }
620 break;
621 #endif
622 default:
623 break;
624 }
625 /* auto-enable s/w beacon miss support */
626 if (flags & IEEE80211_CLONE_NOBEACONS)
627 vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
628 /* auto-generated or user supplied MAC address */
629 if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
630 vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
631 /*
632 * Enable various functionality by default if we're
633 * capable; the driver can override us if it knows better.
634 */
635 if (vap->iv_caps & IEEE80211_C_WME)
636 vap->iv_flags |= IEEE80211_F_WME;
637 if (vap->iv_caps & IEEE80211_C_BURST)
638 vap->iv_flags |= IEEE80211_F_BURST;
639 /* NB: bg scanning only makes sense for station mode right now */
640 if (vap->iv_opmode == IEEE80211_M_STA &&
641 (vap->iv_caps & IEEE80211_C_BGSCAN))
642 vap->iv_flags |= IEEE80211_F_BGSCAN;
643 vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */
644 /* NB: DFS support only makes sense for ap mode right now */
645 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
646 (vap->iv_caps & IEEE80211_C_DFS))
647 vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
648 /* NB: only flip on U-APSD for hostap/sta for now */
649 if ((vap->iv_opmode == IEEE80211_M_STA)
650 || (vap->iv_opmode == IEEE80211_M_HOSTAP)) {
651 if (vap->iv_caps & IEEE80211_C_UAPSD)
652 vap->iv_flags_ext |= IEEE80211_FEXT_UAPSD;
653 }
654
655 vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
656 vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
657 vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
658 /*
659 * Install a default reset method for the ioctl support;
660 * the driver can override this.
661 */
662 vap->iv_reset = default_reset;
663
664 /*
665 * Install a default crypto key update method, the driver
666 * can override this.
667 */
668 vap->iv_update_deftxkey = default_update_deftxkey;
669
670 ieee80211_sysctl_vattach(vap);
671 ieee80211_crypto_vattach(vap);
672 ieee80211_node_vattach(vap);
673 ieee80211_power_vattach(vap);
674 ieee80211_proto_vattach(vap);
675 #ifdef IEEE80211_SUPPORT_SUPERG
676 ieee80211_superg_vattach(vap);
677 #endif
678 ieee80211_ht_vattach(vap);
679 ieee80211_vht_vattach(vap);
680 ieee80211_scan_vattach(vap);
681 ieee80211_regdomain_vattach(vap);
682 ieee80211_radiotap_vattach(vap);
683 ieee80211_vap_reset_erp(vap);
684 ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
685
686 return 0;
687 }
688
689 /*
690 * Activate a vap. State should have been prepared with a
691 * call to ieee80211_vap_setup and by the driver. On return
692 * from this call the vap is ready for use.
693 */
694 int
ieee80211_vap_attach(struct ieee80211vap * vap,ifm_change_cb_t media_change,ifm_stat_cb_t media_stat,const uint8_t macaddr[IEEE80211_ADDR_LEN])695 ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change,
696 ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN])
697 {
698 struct ifnet *ifp = vap->iv_ifp;
699 struct ieee80211com *ic = vap->iv_ic;
700 struct ifmediareq imr;
701 int maxrate;
702
703 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
704 "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
705 __func__, ieee80211_opmode_name[vap->iv_opmode],
706 ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
707
708 /*
709 * Do late attach work that cannot happen until after
710 * the driver has had a chance to override defaults.
711 */
712 ieee80211_node_latevattach(vap);
713 ieee80211_power_latevattach(vap);
714
715 maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
716 vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
717 ieee80211_media_status(ifp, &imr);
718 /* NB: strip explicit mode; we're actually in autoselect */
719 ifmedia_set(&vap->iv_media,
720 imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
721 if (maxrate)
722 ifp->if_baudrate = IF_Mbps(maxrate);
723
724 ether_ifattach(ifp, macaddr);
725 IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
726 /* hook output method setup by ether_ifattach */
727 vap->iv_output = ifp->if_output;
728 ifp->if_output = ieee80211_output;
729 /* NB: if_mtu set by ether_ifattach to ETHERMTU */
730
731 IEEE80211_LOCK(ic);
732 TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
733 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
734 #ifdef IEEE80211_SUPPORT_SUPERG
735 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
736 #endif
737 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
738 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
739 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
740 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
741
742 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
743 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
744 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
745 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
746 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
747 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_STBC_TX);
748 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_STBC_RX);
749 IEEE80211_UNLOCK(ic);
750
751 return 1;
752 }
753
754 /*
755 * Tear down vap state and reclaim the ifnet.
756 * The driver is assumed to have prepared for
757 * this; e.g. by turning off interrupts for the
758 * underlying device.
759 */
760 void
ieee80211_vap_detach(struct ieee80211vap * vap)761 ieee80211_vap_detach(struct ieee80211vap *vap)
762 {
763 struct ieee80211com *ic = vap->iv_ic;
764 struct ifnet *ifp = vap->iv_ifp;
765 int i;
766
767 CURVNET_SET(ifp->if_vnet);
768
769 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
770 __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
771
772 /* NB: bpfdetach is called by ether_ifdetach and claims all taps */
773 ether_ifdetach(ifp);
774
775 ieee80211_stop(vap);
776
777 /*
778 * Flush any deferred vap tasks.
779 */
780 for (i = 0; i < NET80211_IV_NSTATE_NUM; i++)
781 ieee80211_draintask(ic, &vap->iv_nstate_task[i]);
782 ieee80211_draintask(ic, &vap->iv_swbmiss_task);
783 ieee80211_draintask(ic, &vap->iv_wme_task);
784 ieee80211_draintask(ic, &ic->ic_parent_task);
785
786 /* XXX band-aid until ifnet handles this for us */
787 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
788
789 IEEE80211_LOCK(ic);
790 KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
791 TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
792 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
793 #ifdef IEEE80211_SUPPORT_SUPERG
794 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
795 #endif
796 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
797 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
798 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
799 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
800
801 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
802 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
803 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
804 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
805 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
806 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_STBC_TX);
807 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_STBC_RX);
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
ieee80211_promisc(struct ieee80211vap * vap,bool on)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
ieee80211_allmulti(struct ieee80211vap * vap,bool on)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
ieee80211_syncflag_locked(struct ieee80211com * ic,int flag)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
ieee80211_syncflag(struct ieee80211vap * vap,int flag)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
ieee80211_syncflag_ht_locked(struct ieee80211com * ic,int flag)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
ieee80211_syncflag_ht(struct ieee80211vap * vap,int flag)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
ieee80211_syncflag_vht_locked(struct ieee80211com * ic,int flag)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_vht_flags & flag) {
979 bit = 1;
980 break;
981 }
982 if (bit)
983 ic->ic_vht_flags |= flag;
984 else
985 ic->ic_vht_flags &= ~flag;
986 }
987
988 void
ieee80211_syncflag_vht(struct ieee80211vap * vap,int flag)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_vht_flags &= ~flag;
997 } else
998 vap->iv_vht_flags |= 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
ieee80211_syncflag_ext_locked(struct ieee80211com * ic,int flag)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
ieee80211_syncflag_ext(struct ieee80211vap * vap,int flag)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
mapgsm(u_int freq,u_int flags)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
mappsb(u_int freq,u_int flags)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
ieee80211_mhz2ieee(u_int freq,u_int flags)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
ieee80211_chan2ieee(struct ieee80211com * ic,const struct ieee80211_channel * c)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
ieee80211_ieee2mhz(u_int chan,u_int flags)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
set_extchan(struct ieee80211_channel * c)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 { 5815, 5895 },
1200 { 0, 0 }
1201 };
1202
1203 struct vht_chan_range vht160_chan_ranges[] = {
1204 { 5170, 5330 },
1205 { 5490, 5650 },
1206 { 5735, 5895 },
1207 { 0, 0 }
1208 };
1209
1210 static int
set_vht_extchan(struct ieee80211_channel * c)1211 set_vht_extchan(struct ieee80211_channel *c)
1212 {
1213 int i;
1214
1215 if (! IEEE80211_IS_CHAN_VHT(c))
1216 return (0);
1217
1218 if (IEEE80211_IS_CHAN_VHT80P80(c)) {
1219 printf("%s: TODO VHT80+80 channel (ieee=%d, flags=0x%08x)\n",
1220 __func__, c->ic_ieee, c->ic_flags);
1221 }
1222
1223 if (IEEE80211_IS_CHAN_VHT160(c)) {
1224 for (i = 0; vht160_chan_ranges[i].freq_start != 0; i++) {
1225 if (c->ic_freq >= vht160_chan_ranges[i].freq_start &&
1226 c->ic_freq < vht160_chan_ranges[i].freq_end) {
1227 int midpoint;
1228
1229 midpoint = vht160_chan_ranges[i].freq_start + 80;
1230 c->ic_vht_ch_freq1 =
1231 ieee80211_mhz2ieee(midpoint, c->ic_flags);
1232 c->ic_vht_ch_freq2 = 0;
1233 #if 0
1234 printf("%s: %d, freq=%d, midpoint=%d, freq1=%d, freq2=%d\n",
1235 __func__, c->ic_ieee, c->ic_freq, midpoint,
1236 c->ic_vht_ch_freq1, c->ic_vht_ch_freq2);
1237 #endif
1238 return (1);
1239 }
1240 }
1241 return (0);
1242 }
1243
1244 if (IEEE80211_IS_CHAN_VHT80(c)) {
1245 for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
1246 if (c->ic_freq >= vht80_chan_ranges[i].freq_start &&
1247 c->ic_freq < vht80_chan_ranges[i].freq_end) {
1248 int midpoint;
1249
1250 midpoint = vht80_chan_ranges[i].freq_start + 40;
1251 c->ic_vht_ch_freq1 =
1252 ieee80211_mhz2ieee(midpoint, c->ic_flags);
1253 c->ic_vht_ch_freq2 = 0;
1254 #if 0
1255 printf("%s: %d, freq=%d, midpoint=%d, freq1=%d, freq2=%d\n",
1256 __func__, c->ic_ieee, c->ic_freq, midpoint,
1257 c->ic_vht_ch_freq1, c->ic_vht_ch_freq2);
1258 #endif
1259 return (1);
1260 }
1261 }
1262 return (0);
1263 }
1264
1265 if (IEEE80211_IS_CHAN_VHT40(c)) {
1266 if (IEEE80211_IS_CHAN_HT40U(c))
1267 c->ic_vht_ch_freq1 = c->ic_ieee + 2;
1268 else if (IEEE80211_IS_CHAN_HT40D(c))
1269 c->ic_vht_ch_freq1 = c->ic_ieee - 2;
1270 else
1271 return (0);
1272 return (1);
1273 }
1274
1275 if (IEEE80211_IS_CHAN_VHT20(c)) {
1276 c->ic_vht_ch_freq1 = c->ic_ieee;
1277 return (1);
1278 }
1279
1280 printf("%s: unknown VHT channel type (ieee=%d, flags=0x%08x)\n",
1281 __func__, c->ic_ieee, c->ic_flags);
1282
1283 return (0);
1284 }
1285
1286 /*
1287 * Return whether the current channel could possibly be a part of
1288 * a VHT80/VHT160 channel.
1289 *
1290 * This doesn't check that the whole range is in the allowed list
1291 * according to regulatory.
1292 */
1293 static bool
is_vht160_valid_freq(uint16_t freq)1294 is_vht160_valid_freq(uint16_t freq)
1295 {
1296 int i;
1297
1298 for (i = 0; vht160_chan_ranges[i].freq_start != 0; i++) {
1299 if (freq >= vht160_chan_ranges[i].freq_start &&
1300 freq < vht160_chan_ranges[i].freq_end)
1301 return (true);
1302 }
1303 return (false);
1304 }
1305
1306 static int
is_vht80_valid_freq(uint16_t freq)1307 is_vht80_valid_freq(uint16_t freq)
1308 {
1309 int i;
1310 for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
1311 if (freq >= vht80_chan_ranges[i].freq_start &&
1312 freq < vht80_chan_ranges[i].freq_end)
1313 return (1);
1314 }
1315 return (0);
1316 }
1317
1318 static int
addchan(struct ieee80211_channel chans[],int maxchans,int * nchans,uint8_t ieee,uint16_t freq,int8_t maxregpower,uint32_t flags)1319 addchan(struct ieee80211_channel chans[], int maxchans, int *nchans,
1320 uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags)
1321 {
1322 struct ieee80211_channel *c;
1323
1324 if (*nchans >= maxchans)
1325 return (ENOBUFS);
1326
1327 #if 0
1328 printf("%s: %d of %d: ieee=%d, freq=%d, flags=0x%08x\n",
1329 __func__, *nchans, maxchans, ieee, freq, flags);
1330 #endif
1331
1332 c = &chans[(*nchans)++];
1333 c->ic_ieee = ieee;
1334 c->ic_freq = freq != 0 ? freq : ieee80211_ieee2mhz(ieee, flags);
1335 c->ic_maxregpower = maxregpower;
1336 c->ic_maxpower = 2 * maxregpower;
1337 c->ic_flags = flags;
1338 c->ic_vht_ch_freq1 = 0;
1339 c->ic_vht_ch_freq2 = 0;
1340 set_extchan(c);
1341 set_vht_extchan(c);
1342
1343 return (0);
1344 }
1345
1346 static int
copychan_prev(struct ieee80211_channel chans[],int maxchans,int * nchans,uint32_t flags)1347 copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans,
1348 uint32_t flags)
1349 {
1350 struct ieee80211_channel *c;
1351
1352 KASSERT(*nchans > 0, ("channel list is empty\n"));
1353
1354 if (*nchans >= maxchans)
1355 return (ENOBUFS);
1356
1357 #if 0
1358 printf("%s: %d of %d: flags=0x%08x\n",
1359 __func__, *nchans, maxchans, flags);
1360 #endif
1361
1362 c = &chans[(*nchans)++];
1363 c[0] = c[-1];
1364 c->ic_flags = flags;
1365 c->ic_vht_ch_freq1 = 0;
1366 c->ic_vht_ch_freq2 = 0;
1367 set_extchan(c);
1368 set_vht_extchan(c);
1369
1370 return (0);
1371 }
1372
1373 /*
1374 * XXX VHT-2GHz
1375 */
1376 static void
getflags_2ghz(const uint8_t bands[],uint32_t flags[],int cbw_flags)1377 getflags_2ghz(const uint8_t bands[], uint32_t flags[], int cbw_flags)
1378 {
1379 int nmodes;
1380
1381 nmodes = 0;
1382 if (isset(bands, IEEE80211_MODE_11B))
1383 flags[nmodes++] = IEEE80211_CHAN_B;
1384 if (isset(bands, IEEE80211_MODE_11G))
1385 flags[nmodes++] = IEEE80211_CHAN_G;
1386 if (isset(bands, IEEE80211_MODE_11NG))
1387 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
1388 if (cbw_flags & NET80211_CBW_FLAG_HT40) {
1389 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U;
1390 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D;
1391 }
1392 flags[nmodes] = 0;
1393 }
1394
1395 static void
getflags_5ghz(const uint8_t bands[],uint32_t flags[],int cbw_flags)1396 getflags_5ghz(const uint8_t bands[], uint32_t flags[], int cbw_flags)
1397 {
1398 int nmodes;
1399
1400 /*
1401 * The addchan_list() function seems to expect the flags array to
1402 * be in channel width order, so the VHT bits are interspersed
1403 * as appropriate to maintain said order.
1404 *
1405 * It also assumes HT40U is before HT40D.
1406 */
1407 nmodes = 0;
1408
1409 /* 20MHz */
1410 if (isset(bands, IEEE80211_MODE_11A))
1411 flags[nmodes++] = IEEE80211_CHAN_A;
1412 if (isset(bands, IEEE80211_MODE_11NA))
1413 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
1414 if (isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1415 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20 |
1416 IEEE80211_CHAN_VHT20;
1417 }
1418
1419 /* 40MHz */
1420 if (cbw_flags & NET80211_CBW_FLAG_HT40)
1421 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U;
1422 if ((cbw_flags & NET80211_CBW_FLAG_HT40) &&
1423 isset(bands, IEEE80211_MODE_VHT_5GHZ))
1424 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
1425 IEEE80211_CHAN_VHT40U;
1426 if (cbw_flags & NET80211_CBW_FLAG_HT40)
1427 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D;
1428 if ((cbw_flags & NET80211_CBW_FLAG_HT40) &&
1429 isset(bands, IEEE80211_MODE_VHT_5GHZ))
1430 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
1431 IEEE80211_CHAN_VHT40D;
1432
1433 /* 80MHz */
1434 if ((cbw_flags & NET80211_CBW_FLAG_VHT80) &&
1435 isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1436 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
1437 IEEE80211_CHAN_VHT80;
1438 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
1439 IEEE80211_CHAN_VHT80;
1440 }
1441
1442 /* VHT160 */
1443 if ((cbw_flags & NET80211_CBW_FLAG_VHT160) &&
1444 isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1445 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
1446 IEEE80211_CHAN_VHT160;
1447 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
1448 IEEE80211_CHAN_VHT160;
1449 }
1450
1451 /* VHT80+80 */
1452 if ((cbw_flags & NET80211_CBW_FLAG_VHT80P80) &&
1453 isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1454 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
1455 IEEE80211_CHAN_VHT80P80;
1456 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
1457 IEEE80211_CHAN_VHT80P80;
1458 }
1459
1460 flags[nmodes] = 0;
1461 }
1462
1463 static void
getflags(const uint8_t bands[],uint32_t flags[],int cbw_flags)1464 getflags(const uint8_t bands[], uint32_t flags[], int cbw_flags)
1465 {
1466
1467 flags[0] = 0;
1468 if (isset(bands, IEEE80211_MODE_11A) ||
1469 isset(bands, IEEE80211_MODE_11NA) ||
1470 isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1471 if (isset(bands, IEEE80211_MODE_11B) ||
1472 isset(bands, IEEE80211_MODE_11G) ||
1473 isset(bands, IEEE80211_MODE_11NG) ||
1474 isset(bands, IEEE80211_MODE_VHT_2GHZ))
1475 return;
1476
1477 getflags_5ghz(bands, flags, cbw_flags);
1478 } else
1479 getflags_2ghz(bands, flags, cbw_flags);
1480 }
1481
1482 /*
1483 * Add one 20 MHz channel into specified channel list.
1484 * You MUST NOT mix bands when calling this. It will not add 5ghz
1485 * channels if you have any B/G/N band bit set.
1486 * The _cbw() variant does also support HT40/VHT80/160/80+80.
1487 */
1488 int
ieee80211_add_channel_cbw(struct ieee80211_channel chans[],int maxchans,int * nchans,uint8_t ieee,uint16_t freq,int8_t maxregpower,uint32_t chan_flags,const uint8_t bands[],int cbw_flags)1489 ieee80211_add_channel_cbw(struct ieee80211_channel chans[], int maxchans,
1490 int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower,
1491 uint32_t chan_flags, const uint8_t bands[], int cbw_flags)
1492 {
1493 uint32_t flags[IEEE80211_MODE_MAX];
1494 int i, error;
1495
1496 getflags(bands, flags, cbw_flags);
1497 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1498
1499 error = addchan(chans, maxchans, nchans, ieee, freq, maxregpower,
1500 flags[0] | chan_flags);
1501 for (i = 1; flags[i] != 0 && error == 0; i++) {
1502 error = copychan_prev(chans, maxchans, nchans,
1503 flags[i] | chan_flags);
1504 }
1505
1506 return (error);
1507 }
1508
1509 int
ieee80211_add_channel(struct ieee80211_channel chans[],int maxchans,int * nchans,uint8_t ieee,uint16_t freq,int8_t maxregpower,uint32_t chan_flags,const uint8_t bands[])1510 ieee80211_add_channel(struct ieee80211_channel chans[], int maxchans,
1511 int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower,
1512 uint32_t chan_flags, const uint8_t bands[])
1513 {
1514
1515 return (ieee80211_add_channel_cbw(chans, maxchans, nchans, ieee, freq,
1516 maxregpower, chan_flags, bands, 0));
1517 }
1518
1519 static struct ieee80211_channel *
findchannel(struct ieee80211_channel chans[],int nchans,uint16_t freq,uint32_t flags)1520 findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq,
1521 uint32_t flags)
1522 {
1523 struct ieee80211_channel *c;
1524 int i;
1525
1526 flags &= IEEE80211_CHAN_ALLTURBO;
1527 /* brute force search */
1528 for (i = 0; i < nchans; i++) {
1529 c = &chans[i];
1530 if (c->ic_freq == freq &&
1531 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1532 return c;
1533 }
1534 return NULL;
1535 }
1536
1537 /*
1538 * Add 40 MHz channel pair into specified channel list.
1539 */
1540 /* XXX VHT */
1541 int
ieee80211_add_channel_ht40(struct ieee80211_channel chans[],int maxchans,int * nchans,uint8_t ieee,int8_t maxregpower,uint32_t flags)1542 ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans,
1543 int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags)
1544 {
1545 struct ieee80211_channel *cent, *extc;
1546 uint16_t freq;
1547 int error;
1548
1549 freq = ieee80211_ieee2mhz(ieee, flags);
1550
1551 /*
1552 * Each entry defines an HT40 channel pair; find the
1553 * center channel, then the extension channel above.
1554 */
1555 flags |= IEEE80211_CHAN_HT20;
1556 cent = findchannel(chans, *nchans, freq, flags);
1557 if (cent == NULL)
1558 return (EINVAL);
1559
1560 extc = findchannel(chans, *nchans, freq + 20, flags);
1561 if (extc == NULL)
1562 return (ENOENT);
1563
1564 flags &= ~IEEE80211_CHAN_HT;
1565 error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq,
1566 maxregpower, flags | IEEE80211_CHAN_HT40U);
1567 if (error != 0)
1568 return (error);
1569
1570 error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq,
1571 maxregpower, flags | IEEE80211_CHAN_HT40D);
1572
1573 return (error);
1574 }
1575
1576 /*
1577 * Fetch the center frequency for the primary channel.
1578 */
1579 uint32_t
ieee80211_get_channel_center_freq(const struct ieee80211_channel * c)1580 ieee80211_get_channel_center_freq(const struct ieee80211_channel *c)
1581 {
1582
1583 return (c->ic_freq);
1584 }
1585
1586 /*
1587 * Fetch the center frequency for the primary BAND channel.
1588 *
1589 * For 5, 10, 20MHz channels it'll be the normally configured channel
1590 * frequency.
1591 *
1592 * For 40MHz, 80MHz, 160MHz channels it will be the centre of the
1593 * wide channel, not the centre of the primary channel (that's ic_freq).
1594 *
1595 * For 80+80MHz channels this will be the centre of the primary
1596 * 80MHz channel; the secondary 80MHz channel will be center_freq2().
1597 */
1598 uint32_t
ieee80211_get_channel_center_freq1(const struct ieee80211_channel * c)1599 ieee80211_get_channel_center_freq1(const struct ieee80211_channel *c)
1600 {
1601
1602 /*
1603 * VHT - use the pre-calculated centre frequency
1604 * of the given channel.
1605 */
1606 if (IEEE80211_IS_CHAN_VHT(c))
1607 return (ieee80211_ieee2mhz(c->ic_vht_ch_freq1, c->ic_flags));
1608
1609 if (IEEE80211_IS_CHAN_HT40U(c)) {
1610 return (c->ic_freq + 10);
1611 }
1612 if (IEEE80211_IS_CHAN_HT40D(c)) {
1613 return (c->ic_freq - 10);
1614 }
1615
1616 return (c->ic_freq);
1617 }
1618
1619 /*
1620 * For now, no 80+80 support; it will likely always return 0.
1621 */
1622 uint32_t
ieee80211_get_channel_center_freq2(const struct ieee80211_channel * c)1623 ieee80211_get_channel_center_freq2(const struct ieee80211_channel *c)
1624 {
1625
1626 if (IEEE80211_IS_CHAN_VHT(c) && (c->ic_vht_ch_freq2 != 0))
1627 return (ieee80211_ieee2mhz(c->ic_vht_ch_freq2, c->ic_flags));
1628
1629 return (0);
1630 }
1631
1632 /*
1633 * Adds channels into specified channel list (ieee[] array must be sorted).
1634 * Channels are already sorted.
1635 */
1636 static int
add_chanlist(struct ieee80211_channel chans[],int maxchans,int * nchans,const uint8_t ieee[],int nieee,uint32_t flags[])1637 add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans,
1638 const uint8_t ieee[], int nieee, uint32_t flags[])
1639 {
1640 uint16_t freq;
1641 int i, j, error;
1642 int is_vht;
1643
1644 for (i = 0; i < nieee; i++) {
1645 freq = ieee80211_ieee2mhz(ieee[i], flags[0]);
1646 for (j = 0; flags[j] != 0; j++) {
1647 /*
1648 * Notes:
1649 * + HT40 and VHT40 channels occur together, so
1650 * we need to be careful that we actually allow that.
1651 * + VHT80, VHT160 will coexist with HT40/VHT40, so
1652 * make sure it's not skipped because of the overlap
1653 * check used for (V)HT40.
1654 */
1655 is_vht = !! (flags[j] & IEEE80211_CHAN_VHT);
1656
1657 /* XXX TODO FIXME VHT80P80. */
1658
1659 /* Test for VHT160 analogue to the VHT80 below. */
1660 if (is_vht && flags[j] & IEEE80211_CHAN_VHT160)
1661 if (! is_vht160_valid_freq(freq))
1662 continue;
1663
1664 /*
1665 * Test for VHT80.
1666 * XXX This is all very broken right now.
1667 * What we /should/ do is:
1668 *
1669 * + check that the frequency is in the list of
1670 * allowed VHT80 ranges; and
1671 * + the other 3 channels in the list are actually
1672 * also available.
1673 */
1674 if (is_vht && flags[j] & IEEE80211_CHAN_VHT80)
1675 if (! is_vht80_valid_freq(freq))
1676 continue;
1677
1678 /*
1679 * Test for (V)HT40.
1680 *
1681 * This is also a fall through from VHT80; as we only
1682 * allow a VHT80 channel if the VHT40 combination is
1683 * also valid. If the VHT40 form is not valid then
1684 * we certainly can't do VHT80..
1685 */
1686 if (flags[j] & IEEE80211_CHAN_HT40D)
1687 /*
1688 * Can't have a "lower" channel if we are the
1689 * first channel.
1690 *
1691 * Can't have a "lower" channel if it's below/
1692 * within 20MHz of the first channel.
1693 *
1694 * Can't have a "lower" channel if the channel
1695 * below it is not 20MHz away.
1696 */
1697 if (i == 0 || ieee[i] < ieee[0] + 4 ||
1698 freq - 20 !=
1699 ieee80211_ieee2mhz(ieee[i] - 4, flags[j]))
1700 continue;
1701 if (flags[j] & IEEE80211_CHAN_HT40U)
1702 /*
1703 * Can't have an "upper" channel if we are
1704 * the last channel.
1705 *
1706 * Can't have an "upper" channel be above the
1707 * last channel in the list.
1708 *
1709 * Can't have an "upper" channel if the next
1710 * channel according to the math isn't 20MHz
1711 * away. (Likely for channel 13/14.)
1712 */
1713 if (i == nieee - 1 ||
1714 ieee[i] + 4 > ieee[nieee - 1] ||
1715 freq + 20 !=
1716 ieee80211_ieee2mhz(ieee[i] + 4, flags[j]))
1717 continue;
1718
1719 if (j == 0) {
1720 error = addchan(chans, maxchans, nchans,
1721 ieee[i], freq, 0, flags[j]);
1722 } else {
1723 error = copychan_prev(chans, maxchans, nchans,
1724 flags[j]);
1725 }
1726 if (error != 0)
1727 return (error);
1728 }
1729 }
1730
1731 return (0);
1732 }
1733
1734 int
ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[],int maxchans,int * nchans,const uint8_t ieee[],int nieee,const uint8_t bands[],int cbw_flags)1735 ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[], int maxchans,
1736 int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1737 int cbw_flags)
1738 {
1739 uint32_t flags[IEEE80211_MODE_MAX];
1740
1741 /* XXX no VHT for now */
1742 getflags_2ghz(bands, flags, cbw_flags);
1743 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1744
1745 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1746 }
1747
1748 int
ieee80211_add_channels_default_2ghz(struct ieee80211_channel chans[],int maxchans,int * nchans,const uint8_t bands[],int cbw_flags)1749 ieee80211_add_channels_default_2ghz(struct ieee80211_channel chans[],
1750 int maxchans, int *nchans, const uint8_t bands[], int cbw_flags)
1751 {
1752 const uint8_t default_chan_list[] =
1753 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
1754
1755 return (ieee80211_add_channel_list_2ghz(chans, maxchans, nchans,
1756 default_chan_list, nitems(default_chan_list), bands, cbw_flags));
1757 }
1758
1759 int
ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[],int maxchans,int * nchans,const uint8_t ieee[],int nieee,const uint8_t bands[],int cbw_flags)1760 ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[], int maxchans,
1761 int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1762 int cbw_flags)
1763 {
1764 /*
1765 * XXX-BZ with HT and VHT there is no 1:1 mapping anymore. Review all
1766 * uses of IEEE80211_MODE_MAX and add a new #define name for array size.
1767 */
1768 uint32_t flags[2 * IEEE80211_MODE_MAX];
1769
1770 getflags_5ghz(bands, flags, cbw_flags);
1771 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1772
1773 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1774 }
1775
1776 /*
1777 * Locate a channel given a frequency+flags. We cache
1778 * the previous lookup to optimize switching between two
1779 * channels--as happens with dynamic turbo.
1780 */
1781 struct ieee80211_channel *
ieee80211_find_channel(struct ieee80211com * ic,int freq,int flags)1782 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
1783 {
1784 struct ieee80211_channel *c;
1785
1786 flags &= IEEE80211_CHAN_ALLTURBO;
1787 c = ic->ic_prevchan;
1788 if (c != NULL && c->ic_freq == freq &&
1789 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1790 return c;
1791 /* brute force search */
1792 return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags));
1793 }
1794
1795 /*
1796 * Locate a channel given a channel number+flags. We cache
1797 * the previous lookup to optimize switching between two
1798 * channels--as happens with dynamic turbo.
1799 */
1800 struct ieee80211_channel *
ieee80211_find_channel_byieee(struct ieee80211com * ic,int ieee,int flags)1801 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1802 {
1803 struct ieee80211_channel *c;
1804 int i;
1805
1806 flags &= IEEE80211_CHAN_ALLTURBO;
1807 c = ic->ic_prevchan;
1808 if (c != NULL && c->ic_ieee == ieee &&
1809 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1810 return c;
1811 /* brute force search */
1812 for (i = 0; i < ic->ic_nchans; i++) {
1813 c = &ic->ic_channels[i];
1814 if (c->ic_ieee == ieee &&
1815 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1816 return c;
1817 }
1818 return NULL;
1819 }
1820
1821 /*
1822 * Lookup a channel suitable for the given rx status.
1823 *
1824 * This is used to find a channel for a frame (eg beacon, probe
1825 * response) based purely on the received PHY information.
1826 *
1827 * For now it tries to do it based on R_FREQ / R_IEEE.
1828 * This is enough for 11bg and 11a (and thus 11ng/11na)
1829 * but it will not be enough for GSM, PSB channels and the
1830 * like. It also doesn't know about legacy-turbog and
1831 * legacy-turbo modes, which some offload NICs actually
1832 * support in weird ways.
1833 *
1834 * Takes the ic and rxstatus; returns the channel or NULL
1835 * if not found.
1836 *
1837 * XXX TODO: Add support for that when the need arises.
1838 */
1839 struct ieee80211_channel *
ieee80211_lookup_channel_rxstatus(struct ieee80211vap * vap,const struct ieee80211_rx_stats * rxs)1840 ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1841 const struct ieee80211_rx_stats *rxs)
1842 {
1843 struct ieee80211com *ic = vap->iv_ic;
1844 uint32_t flags;
1845 struct ieee80211_channel *c;
1846
1847 if (rxs == NULL)
1848 return (NULL);
1849
1850 /*
1851 * Strictly speaking we only use freq for now,
1852 * however later on we may wish to just store
1853 * the ieee for verification.
1854 */
1855 if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1856 return (NULL);
1857 if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1858 return (NULL);
1859 if ((rxs->r_flags & IEEE80211_R_BAND) == 0)
1860 return (NULL);
1861
1862 /*
1863 * If the rx status contains a valid ieee/freq, then
1864 * ensure we populate the correct channel information
1865 * in rxchan before passing it up to the scan infrastructure.
1866 * Offload NICs will pass up beacons from all channels
1867 * during background scans.
1868 */
1869
1870 /* Determine a band */
1871 switch (rxs->c_band) {
1872 case IEEE80211_CHAN_2GHZ:
1873 flags = IEEE80211_CHAN_G;
1874 break;
1875 case IEEE80211_CHAN_5GHZ:
1876 flags = IEEE80211_CHAN_A;
1877 break;
1878 default:
1879 if (rxs->c_freq < 3000) {
1880 flags = IEEE80211_CHAN_G;
1881 } else {
1882 flags = IEEE80211_CHAN_A;
1883 }
1884 break;
1885 }
1886
1887 /* Channel lookup */
1888 c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1889
1890 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1891 "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1892 __func__, (int) rxs->c_freq, (int) rxs->c_ieee, flags, c);
1893
1894 return (c);
1895 }
1896
1897 static void
addmedia(struct ifmedia * media,int caps,int addsta,int mode,int mword)1898 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1899 {
1900 #define ADD(_ic, _s, _o) \
1901 ifmedia_add(media, \
1902 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1903 static const u_int mopts[IEEE80211_MODE_MAX] = {
1904 [IEEE80211_MODE_AUTO] = IFM_AUTO,
1905 [IEEE80211_MODE_11A] = IFM_IEEE80211_11A,
1906 [IEEE80211_MODE_11B] = IFM_IEEE80211_11B,
1907 [IEEE80211_MODE_11G] = IFM_IEEE80211_11G,
1908 [IEEE80211_MODE_FH] = IFM_IEEE80211_FH,
1909 [IEEE80211_MODE_TURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1910 [IEEE80211_MODE_TURBO_G] = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1911 [IEEE80211_MODE_STURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1912 [IEEE80211_MODE_HALF] = IFM_IEEE80211_11A, /* XXX */
1913 [IEEE80211_MODE_QUARTER] = IFM_IEEE80211_11A, /* XXX */
1914 [IEEE80211_MODE_11NA] = IFM_IEEE80211_11NA,
1915 [IEEE80211_MODE_11NG] = IFM_IEEE80211_11NG,
1916 [IEEE80211_MODE_VHT_2GHZ] = IFM_IEEE80211_VHT2G,
1917 [IEEE80211_MODE_VHT_5GHZ] = IFM_IEEE80211_VHT5G,
1918 };
1919 u_int mopt;
1920
1921 mopt = mopts[mode];
1922 if (addsta)
1923 ADD(ic, mword, mopt); /* STA mode has no cap */
1924 if (caps & IEEE80211_C_IBSS)
1925 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1926 if (caps & IEEE80211_C_HOSTAP)
1927 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1928 if (caps & IEEE80211_C_AHDEMO)
1929 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1930 if (caps & IEEE80211_C_MONITOR)
1931 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1932 if (caps & IEEE80211_C_WDS)
1933 ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1934 if (caps & IEEE80211_C_MBSS)
1935 ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1936 #undef ADD
1937 }
1938
1939 /*
1940 * Setup the media data structures according to the channel and
1941 * rate tables.
1942 */
1943 static int
ieee80211_media_setup(struct ieee80211com * ic,struct ifmedia * media,int caps,int addsta,ifm_change_cb_t media_change,ifm_stat_cb_t media_stat)1944 ieee80211_media_setup(struct ieee80211com *ic,
1945 struct ifmedia *media, int caps, int addsta,
1946 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1947 {
1948 int i, j, rate, maxrate, mword, r;
1949 enum ieee80211_phymode mode;
1950 const struct ieee80211_rateset *rs;
1951 struct ieee80211_rateset allrates;
1952 struct ieee80211_node_txrate tn;
1953
1954 /*
1955 * Fill in media characteristics.
1956 */
1957 ifmedia_init(media, 0, media_change, media_stat);
1958 maxrate = 0;
1959 /*
1960 * Add media for legacy operating modes.
1961 */
1962 memset(&allrates, 0, sizeof(allrates));
1963 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1964 if (isclr(ic->ic_modecaps, mode))
1965 continue;
1966 addmedia(media, caps, addsta, mode, IFM_AUTO);
1967 if (mode == IEEE80211_MODE_AUTO)
1968 continue;
1969 rs = &ic->ic_sup_rates[mode];
1970 for (i = 0; i < rs->rs_nrates; i++) {
1971 rate = rs->rs_rates[i];
1972 tn = IEEE80211_NODE_TXRATE_INIT_LEGACY(rate);
1973 mword = ieee80211_rate2media(ic, &tn, mode);
1974 if (mword == 0)
1975 continue;
1976 addmedia(media, caps, addsta, mode, mword);
1977 /*
1978 * Add legacy rate to the collection of all rates.
1979 */
1980 r = rate & IEEE80211_RATE_VAL;
1981 for (j = 0; j < allrates.rs_nrates; j++)
1982 if (allrates.rs_rates[j] == r)
1983 break;
1984 if (j == allrates.rs_nrates) {
1985 /* unique, add to the set */
1986 allrates.rs_rates[j] = r;
1987 allrates.rs_nrates++;
1988 }
1989 rate = (rate & IEEE80211_RATE_VAL) / 2;
1990 if (rate > maxrate)
1991 maxrate = rate;
1992 }
1993 }
1994 for (i = 0; i < allrates.rs_nrates; i++) {
1995 tn = IEEE80211_NODE_TXRATE_INIT_LEGACY(allrates.rs_rates[i]);
1996 mword = ieee80211_rate2media(ic, &tn, IEEE80211_MODE_AUTO);
1997 if (mword == 0)
1998 continue;
1999 /* NB: remove media options from mword */
2000 addmedia(media, caps, addsta,
2001 IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
2002 }
2003 /*
2004 * Add HT/11n media. Note that we do not have enough
2005 * bits in the media subtype to express the MCS so we
2006 * use a "placeholder" media subtype and any fixed MCS
2007 * must be specified with a different mechanism.
2008 */
2009 for (; mode <= IEEE80211_MODE_11NG; mode++) {
2010 if (isclr(ic->ic_modecaps, mode))
2011 continue;
2012 addmedia(media, caps, addsta, mode, IFM_AUTO);
2013 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
2014 }
2015 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
2016 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
2017 addmedia(media, caps, addsta,
2018 IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
2019 i = ic->ic_txstream * 8 - 1;
2020 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
2021 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
2022 rate = ieee80211_htrates[i].ht40_rate_400ns;
2023 else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
2024 rate = ieee80211_htrates[i].ht40_rate_800ns;
2025 else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
2026 rate = ieee80211_htrates[i].ht20_rate_400ns;
2027 else
2028 rate = ieee80211_htrates[i].ht20_rate_800ns;
2029 if (rate > maxrate)
2030 maxrate = rate;
2031 }
2032
2033 /*
2034 * Add VHT media.
2035 * XXX-BZ skip "VHT_2GHZ" for now.
2036 */
2037 for (mode = IEEE80211_MODE_VHT_5GHZ; mode <= IEEE80211_MODE_VHT_5GHZ;
2038 mode++) {
2039 if (isclr(ic->ic_modecaps, mode))
2040 continue;
2041 addmedia(media, caps, addsta, mode, IFM_AUTO);
2042 addmedia(media, caps, addsta, mode, IFM_IEEE80211_VHT);
2043 }
2044 if (isset(ic->ic_modecaps, IEEE80211_MODE_VHT_5GHZ)) {
2045 addmedia(media, caps, addsta,
2046 IEEE80211_MODE_AUTO, IFM_IEEE80211_VHT);
2047
2048 /* XXX TODO: VHT maxrate */
2049 }
2050
2051 return maxrate;
2052 }
2053
2054 /* XXX inline or eliminate? */
2055 const struct ieee80211_rateset *
ieee80211_get_suprates(struct ieee80211com * ic,const struct ieee80211_channel * c)2056 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
2057 {
2058 /* XXX does this work for 11ng basic rates? */
2059 return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
2060 }
2061
2062 /* XXX inline or eliminate? */
2063 const struct ieee80211_htrateset *
ieee80211_get_suphtrates(struct ieee80211com * ic,const struct ieee80211_channel * c)2064 ieee80211_get_suphtrates(struct ieee80211com *ic,
2065 const struct ieee80211_channel *c)
2066 {
2067 return &ic->ic_sup_htrates;
2068 }
2069
2070 void
ieee80211_announce(struct ieee80211com * ic)2071 ieee80211_announce(struct ieee80211com *ic)
2072 {
2073 int i, rate, mword;
2074 enum ieee80211_phymode mode;
2075 const struct ieee80211_rateset *rs;
2076 struct ieee80211_node_txrate tn;
2077
2078 /* NB: skip AUTO since it has no rates */
2079 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
2080 if (isclr(ic->ic_modecaps, mode))
2081 continue;
2082 ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
2083 rs = &ic->ic_sup_rates[mode];
2084 for (i = 0; i < rs->rs_nrates; i++) {
2085 tn = IEEE80211_NODE_TXRATE_INIT_LEGACY(rs->rs_rates[i]);
2086 mword = ieee80211_rate2media(ic, &tn, mode);
2087 if (mword == 0)
2088 continue;
2089 rate = ieee80211_media2rate(mword);
2090 printf("%s%d%sMbps", (i != 0 ? " " : ""),
2091 rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
2092 }
2093 printf("\n");
2094 }
2095 ieee80211_ht_announce(ic);
2096 ieee80211_vht_announce(ic);
2097 }
2098
2099 void
ieee80211_announce_channels(struct ieee80211com * ic)2100 ieee80211_announce_channels(struct ieee80211com *ic)
2101 {
2102 const struct ieee80211_channel *c;
2103 char type;
2104 int i, cw;
2105
2106 printf("Chan Freq CW RegPwr MinPwr MaxPwr\n");
2107 for (i = 0; i < ic->ic_nchans; i++) {
2108 c = &ic->ic_channels[i];
2109 if (IEEE80211_IS_CHAN_ST(c))
2110 type = 'S';
2111 else if (IEEE80211_IS_CHAN_108A(c))
2112 type = 'T';
2113 else if (IEEE80211_IS_CHAN_108G(c))
2114 type = 'G';
2115 else if (IEEE80211_IS_CHAN_HT(c))
2116 type = 'n';
2117 else if (IEEE80211_IS_CHAN_A(c))
2118 type = 'a';
2119 else if (IEEE80211_IS_CHAN_ANYG(c))
2120 type = 'g';
2121 else if (IEEE80211_IS_CHAN_B(c))
2122 type = 'b';
2123 else
2124 type = 'f';
2125 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
2126 cw = 40;
2127 else if (IEEE80211_IS_CHAN_HALF(c))
2128 cw = 10;
2129 else if (IEEE80211_IS_CHAN_QUARTER(c))
2130 cw = 5;
2131 else
2132 cw = 20;
2133 printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
2134 , c->ic_ieee, c->ic_freq, type
2135 , cw
2136 , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
2137 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
2138 , c->ic_maxregpower
2139 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
2140 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
2141 );
2142 }
2143 }
2144
2145 static int
media2mode(const struct ifmedia_entry * ime,uint32_t flags,uint16_t * mode)2146 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
2147 {
2148 switch (IFM_MODE(ime->ifm_media)) {
2149 case IFM_IEEE80211_11A:
2150 *mode = IEEE80211_MODE_11A;
2151 break;
2152 case IFM_IEEE80211_11B:
2153 *mode = IEEE80211_MODE_11B;
2154 break;
2155 case IFM_IEEE80211_11G:
2156 *mode = IEEE80211_MODE_11G;
2157 break;
2158 case IFM_IEEE80211_FH:
2159 *mode = IEEE80211_MODE_FH;
2160 break;
2161 case IFM_IEEE80211_11NA:
2162 *mode = IEEE80211_MODE_11NA;
2163 break;
2164 case IFM_IEEE80211_11NG:
2165 *mode = IEEE80211_MODE_11NG;
2166 break;
2167 case IFM_IEEE80211_VHT2G:
2168 *mode = IEEE80211_MODE_VHT_2GHZ;
2169 break;
2170 case IFM_IEEE80211_VHT5G:
2171 *mode = IEEE80211_MODE_VHT_5GHZ;
2172 break;
2173 case IFM_AUTO:
2174 *mode = IEEE80211_MODE_AUTO;
2175 break;
2176 default:
2177 return 0;
2178 }
2179 /*
2180 * Turbo mode is an ``option''.
2181 * XXX does not apply to AUTO
2182 */
2183 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
2184 if (*mode == IEEE80211_MODE_11A) {
2185 if (flags & IEEE80211_F_TURBOP)
2186 *mode = IEEE80211_MODE_TURBO_A;
2187 else
2188 *mode = IEEE80211_MODE_STURBO_A;
2189 } else if (*mode == IEEE80211_MODE_11G)
2190 *mode = IEEE80211_MODE_TURBO_G;
2191 else
2192 return 0;
2193 }
2194 /* XXX HT40 +/- */
2195 return 1;
2196 }
2197
2198 /*
2199 * Handle a media change request on the vap interface.
2200 */
2201 int
ieee80211_media_change(struct ifnet * ifp)2202 ieee80211_media_change(struct ifnet *ifp)
2203 {
2204 struct ieee80211vap *vap = ifp->if_softc;
2205 struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
2206 uint16_t newmode;
2207
2208 if (!media2mode(ime, vap->iv_flags, &newmode))
2209 return EINVAL;
2210 if (vap->iv_des_mode != newmode) {
2211 vap->iv_des_mode = newmode;
2212 /* XXX kick state machine if up+running */
2213 }
2214 return 0;
2215 }
2216
2217 /*
2218 * Common code to calculate the media status word
2219 * from the operating mode and channel state.
2220 */
2221 static int
media_status(enum ieee80211_opmode opmode,const struct ieee80211_channel * chan)2222 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
2223 {
2224 int status;
2225
2226 status = IFM_IEEE80211;
2227 switch (opmode) {
2228 case IEEE80211_M_STA:
2229 break;
2230 case IEEE80211_M_IBSS:
2231 status |= IFM_IEEE80211_ADHOC;
2232 break;
2233 case IEEE80211_M_HOSTAP:
2234 status |= IFM_IEEE80211_HOSTAP;
2235 break;
2236 case IEEE80211_M_MONITOR:
2237 status |= IFM_IEEE80211_MONITOR;
2238 break;
2239 case IEEE80211_M_AHDEMO:
2240 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
2241 break;
2242 case IEEE80211_M_WDS:
2243 status |= IFM_IEEE80211_WDS;
2244 break;
2245 case IEEE80211_M_MBSS:
2246 status |= IFM_IEEE80211_MBSS;
2247 break;
2248 }
2249 if (IEEE80211_IS_CHAN_VHT_5GHZ(chan)) {
2250 status |= IFM_IEEE80211_VHT5G;
2251 } else if (IEEE80211_IS_CHAN_VHT_2GHZ(chan)) {
2252 status |= IFM_IEEE80211_VHT2G;
2253 } else if (IEEE80211_IS_CHAN_HTA(chan)) {
2254 status |= IFM_IEEE80211_11NA;
2255 } else if (IEEE80211_IS_CHAN_HTG(chan)) {
2256 status |= IFM_IEEE80211_11NG;
2257 } else if (IEEE80211_IS_CHAN_A(chan)) {
2258 status |= IFM_IEEE80211_11A;
2259 } else if (IEEE80211_IS_CHAN_B(chan)) {
2260 status |= IFM_IEEE80211_11B;
2261 } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
2262 status |= IFM_IEEE80211_11G;
2263 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
2264 status |= IFM_IEEE80211_FH;
2265 }
2266 /* XXX else complain? */
2267
2268 if (IEEE80211_IS_CHAN_TURBO(chan))
2269 status |= IFM_IEEE80211_TURBO;
2270 #if 0
2271 if (IEEE80211_IS_CHAN_HT20(chan))
2272 status |= IFM_IEEE80211_HT20;
2273 if (IEEE80211_IS_CHAN_HT40(chan))
2274 status |= IFM_IEEE80211_HT40;
2275 #endif
2276 return status;
2277 }
2278
2279 void
ieee80211_media_status(struct ifnet * ifp,struct ifmediareq * imr)2280 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
2281 {
2282 struct ieee80211vap *vap = ifp->if_softc;
2283 struct ieee80211com *ic = vap->iv_ic;
2284 enum ieee80211_phymode mode;
2285 struct ieee80211_node_txrate tn;
2286
2287 imr->ifm_status = IFM_AVALID;
2288 /*
2289 * NB: use the current channel's mode to lock down a xmit
2290 * rate only when running; otherwise we may have a mismatch
2291 * in which case the rate will not be convertible.
2292 */
2293 if (vap->iv_state == IEEE80211_S_RUN ||
2294 vap->iv_state == IEEE80211_S_SLEEP) {
2295 imr->ifm_status |= IFM_ACTIVE;
2296 mode = ieee80211_chan2mode(ic->ic_curchan);
2297 } else
2298 mode = IEEE80211_MODE_AUTO;
2299 imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
2300 /*
2301 * Calculate a current rate if possible.
2302 */
2303 if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
2304 /*
2305 * A fixed rate is set, report that.
2306 */
2307 tn = IEEE80211_NODE_TXRATE_INIT_LEGACY(
2308 vap->iv_txparms[mode].ucastrate);
2309 imr->ifm_active |= ieee80211_rate2media(ic, &tn, mode);
2310 } else if (vap->iv_opmode == IEEE80211_M_STA) {
2311 /*
2312 * In station mode report the current transmit rate.
2313 */
2314 ieee80211_node_get_txrate(vap->iv_bss, &tn);
2315 imr->ifm_active |= ieee80211_rate2media(ic, &tn, mode);
2316 } else
2317 imr->ifm_active |= IFM_AUTO;
2318 if (imr->ifm_status & IFM_ACTIVE)
2319 imr->ifm_current = imr->ifm_active;
2320 }
2321
2322 /*
2323 * Set the current phy mode and recalculate the active channel
2324 * set based on the available channels for this mode. Also
2325 * select a new default/current channel if the current one is
2326 * inappropriate for this mode.
2327 */
2328 int
ieee80211_setmode(struct ieee80211com * ic,enum ieee80211_phymode mode)2329 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
2330 {
2331 /*
2332 * Adjust basic rates in 11b/11g supported rate set.
2333 * Note that if operating on a hal/quarter rate channel
2334 * this is a noop as those rates sets are different
2335 * and used instead.
2336 */
2337 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
2338 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
2339
2340 ic->ic_curmode = mode;
2341 ieee80211_reset_erp(ic); /* reset global ERP state */
2342
2343 return 0;
2344 }
2345
2346 /*
2347 * Return the phy mode for with the specified channel.
2348 */
2349 enum ieee80211_phymode
ieee80211_chan2mode(const struct ieee80211_channel * chan)2350 ieee80211_chan2mode(const struct ieee80211_channel *chan)
2351 {
2352
2353 if (IEEE80211_IS_CHAN_VHT_2GHZ(chan))
2354 return IEEE80211_MODE_VHT_2GHZ;
2355 else if (IEEE80211_IS_CHAN_VHT_5GHZ(chan))
2356 return IEEE80211_MODE_VHT_5GHZ;
2357 else if (IEEE80211_IS_CHAN_HTA(chan))
2358 return IEEE80211_MODE_11NA;
2359 else if (IEEE80211_IS_CHAN_HTG(chan))
2360 return IEEE80211_MODE_11NG;
2361 else if (IEEE80211_IS_CHAN_108G(chan))
2362 return IEEE80211_MODE_TURBO_G;
2363 else if (IEEE80211_IS_CHAN_ST(chan))
2364 return IEEE80211_MODE_STURBO_A;
2365 else if (IEEE80211_IS_CHAN_TURBO(chan))
2366 return IEEE80211_MODE_TURBO_A;
2367 else if (IEEE80211_IS_CHAN_HALF(chan))
2368 return IEEE80211_MODE_HALF;
2369 else if (IEEE80211_IS_CHAN_QUARTER(chan))
2370 return IEEE80211_MODE_QUARTER;
2371 else if (IEEE80211_IS_CHAN_A(chan))
2372 return IEEE80211_MODE_11A;
2373 else if (IEEE80211_IS_CHAN_ANYG(chan))
2374 return IEEE80211_MODE_11G;
2375 else if (IEEE80211_IS_CHAN_B(chan))
2376 return IEEE80211_MODE_11B;
2377 else if (IEEE80211_IS_CHAN_FHSS(chan))
2378 return IEEE80211_MODE_FH;
2379
2380 /* NB: should not get here */
2381 printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
2382 __func__, chan->ic_freq, chan->ic_flags);
2383 return IEEE80211_MODE_11B;
2384 }
2385
2386 struct ratemedia {
2387 u_int match; /* rate + mode */
2388 u_int media; /* if_media rate */
2389 };
2390
2391 static int
findmedia(const struct ratemedia rates[],int n,u_int match)2392 findmedia(const struct ratemedia rates[], int n, u_int match)
2393 {
2394 int i;
2395
2396 for (i = 0; i < n; i++)
2397 if (rates[i].match == match)
2398 return rates[i].media;
2399 return IFM_AUTO;
2400 }
2401
2402 /*
2403 * Convert IEEE80211 rate value to ifmedia subtype.
2404 * Rate is either a legacy rate in units of 0.5Mbps
2405 * or an MCS index.
2406 */
2407 int
ieee80211_rate2media(struct ieee80211com * ic,const struct ieee80211_node_txrate * tr,enum ieee80211_phymode mode)2408 ieee80211_rate2media(struct ieee80211com *ic,
2409 const struct ieee80211_node_txrate *tr, enum ieee80211_phymode mode)
2410 {
2411 static const struct ratemedia rates[] = {
2412 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
2413 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
2414 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
2415 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
2416 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
2417 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
2418 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
2419 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
2420 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
2421 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
2422 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
2423 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
2424 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
2425 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
2426 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
2427 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
2428 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
2429 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
2430 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
2431 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
2432 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
2433 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
2434 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
2435 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
2436 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
2437 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
2438 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
2439 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
2440 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
2441 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
2442 /* NB: OFDM72 doesn't really exist so we don't handle it */
2443 };
2444 static const struct ratemedia htrates[] = {
2445 { 0, IFM_IEEE80211_MCS },
2446 { 1, IFM_IEEE80211_MCS },
2447 { 2, IFM_IEEE80211_MCS },
2448 { 3, IFM_IEEE80211_MCS },
2449 { 4, IFM_IEEE80211_MCS },
2450 { 5, IFM_IEEE80211_MCS },
2451 { 6, IFM_IEEE80211_MCS },
2452 { 7, IFM_IEEE80211_MCS },
2453 { 8, IFM_IEEE80211_MCS },
2454 { 9, IFM_IEEE80211_MCS },
2455 { 10, IFM_IEEE80211_MCS },
2456 { 11, IFM_IEEE80211_MCS },
2457 { 12, IFM_IEEE80211_MCS },
2458 { 13, IFM_IEEE80211_MCS },
2459 { 14, IFM_IEEE80211_MCS },
2460 { 15, IFM_IEEE80211_MCS },
2461 { 16, IFM_IEEE80211_MCS },
2462 { 17, IFM_IEEE80211_MCS },
2463 { 18, IFM_IEEE80211_MCS },
2464 { 19, IFM_IEEE80211_MCS },
2465 { 20, IFM_IEEE80211_MCS },
2466 { 21, IFM_IEEE80211_MCS },
2467 { 22, IFM_IEEE80211_MCS },
2468 { 23, IFM_IEEE80211_MCS },
2469 { 24, IFM_IEEE80211_MCS },
2470 { 25, IFM_IEEE80211_MCS },
2471 { 26, IFM_IEEE80211_MCS },
2472 { 27, IFM_IEEE80211_MCS },
2473 { 28, IFM_IEEE80211_MCS },
2474 { 29, IFM_IEEE80211_MCS },
2475 { 30, IFM_IEEE80211_MCS },
2476 { 31, IFM_IEEE80211_MCS },
2477 { 32, IFM_IEEE80211_MCS },
2478 { 33, IFM_IEEE80211_MCS },
2479 { 34, IFM_IEEE80211_MCS },
2480 { 35, IFM_IEEE80211_MCS },
2481 { 36, IFM_IEEE80211_MCS },
2482 { 37, IFM_IEEE80211_MCS },
2483 { 38, IFM_IEEE80211_MCS },
2484 { 39, IFM_IEEE80211_MCS },
2485 { 40, IFM_IEEE80211_MCS },
2486 { 41, IFM_IEEE80211_MCS },
2487 { 42, IFM_IEEE80211_MCS },
2488 { 43, IFM_IEEE80211_MCS },
2489 { 44, IFM_IEEE80211_MCS },
2490 { 45, IFM_IEEE80211_MCS },
2491 { 46, IFM_IEEE80211_MCS },
2492 { 47, IFM_IEEE80211_MCS },
2493 { 48, IFM_IEEE80211_MCS },
2494 { 49, IFM_IEEE80211_MCS },
2495 { 50, IFM_IEEE80211_MCS },
2496 { 51, IFM_IEEE80211_MCS },
2497 { 52, IFM_IEEE80211_MCS },
2498 { 53, IFM_IEEE80211_MCS },
2499 { 54, IFM_IEEE80211_MCS },
2500 { 55, IFM_IEEE80211_MCS },
2501 { 56, IFM_IEEE80211_MCS },
2502 { 57, IFM_IEEE80211_MCS },
2503 { 58, IFM_IEEE80211_MCS },
2504 { 59, IFM_IEEE80211_MCS },
2505 { 60, IFM_IEEE80211_MCS },
2506 { 61, IFM_IEEE80211_MCS },
2507 { 62, IFM_IEEE80211_MCS },
2508 { 63, IFM_IEEE80211_MCS },
2509 { 64, IFM_IEEE80211_MCS },
2510 { 65, IFM_IEEE80211_MCS },
2511 { 66, IFM_IEEE80211_MCS },
2512 { 67, IFM_IEEE80211_MCS },
2513 { 68, IFM_IEEE80211_MCS },
2514 { 69, IFM_IEEE80211_MCS },
2515 { 70, IFM_IEEE80211_MCS },
2516 { 71, IFM_IEEE80211_MCS },
2517 { 72, IFM_IEEE80211_MCS },
2518 { 73, IFM_IEEE80211_MCS },
2519 { 74, IFM_IEEE80211_MCS },
2520 { 75, IFM_IEEE80211_MCS },
2521 { 76, IFM_IEEE80211_MCS },
2522 };
2523 static const struct ratemedia vhtrates[] = {
2524 { 0, IFM_IEEE80211_VHT },
2525 { 1, IFM_IEEE80211_VHT },
2526 { 2, IFM_IEEE80211_VHT },
2527 { 3, IFM_IEEE80211_VHT },
2528 { 4, IFM_IEEE80211_VHT },
2529 { 5, IFM_IEEE80211_VHT },
2530 { 6, IFM_IEEE80211_VHT },
2531 { 7, IFM_IEEE80211_VHT },
2532 { 8, IFM_IEEE80211_VHT }, /* Optional. */
2533 { 9, IFM_IEEE80211_VHT }, /* Optional. */
2534 #if 0
2535 /* Some QCA and BRCM seem to support this; offspec. */
2536 { 10, IFM_IEEE80211_VHT },
2537 { 11, IFM_IEEE80211_VHT },
2538 #endif
2539 };
2540 int m, rate;
2541
2542 /*
2543 * Check 11ac/11n rates first for match as an MCS.
2544 */
2545 if (mode == IEEE80211_MODE_VHT_5GHZ) {
2546 if (tr->type == IEEE80211_NODE_TXRATE_VHT) {
2547 m = findmedia(vhtrates, nitems(vhtrates), tr->mcs);
2548 if (m != IFM_AUTO)
2549 return (m | IFM_IEEE80211_VHT);
2550 }
2551 } else if (mode == IEEE80211_MODE_11NA) {
2552 /* NB: 12 is ambiguous, it will be treated as an MCS */
2553 if (tr->type == IEEE80211_NODE_TXRATE_HT) {
2554 m = findmedia(htrates, nitems(htrates),
2555 tr->dot11rate & ~IEEE80211_RATE_MCS);
2556 if (m != IFM_AUTO)
2557 return m | IFM_IEEE80211_11NA;
2558 }
2559 } else if (mode == IEEE80211_MODE_11NG) {
2560 /* NB: 12 is ambiguous, it will be treated as an MCS */
2561 if (tr->type == IEEE80211_NODE_TXRATE_HT) {
2562 m = findmedia(htrates, nitems(htrates),
2563 tr->dot11rate & ~IEEE80211_RATE_MCS);
2564 if (m != IFM_AUTO)
2565 return m | IFM_IEEE80211_11NG;
2566 }
2567 }
2568
2569 /*
2570 * At this point it needs to be a dot11rate (legacy/HT) for the
2571 * rest of the logic to work.
2572 */
2573 if ((tr->type != IEEE80211_NODE_TXRATE_LEGACY) &&
2574 (tr->type != IEEE80211_NODE_TXRATE_HT))
2575 return (IFM_AUTO);
2576 rate = tr->dot11rate & IEEE80211_RATE_VAL;
2577
2578 switch (mode) {
2579 case IEEE80211_MODE_11A:
2580 case IEEE80211_MODE_HALF: /* XXX good 'nuf */
2581 case IEEE80211_MODE_QUARTER:
2582 case IEEE80211_MODE_11NA:
2583 case IEEE80211_MODE_TURBO_A:
2584 case IEEE80211_MODE_STURBO_A:
2585 return findmedia(rates, nitems(rates),
2586 rate | IFM_IEEE80211_11A);
2587 case IEEE80211_MODE_11B:
2588 return findmedia(rates, nitems(rates),
2589 rate | IFM_IEEE80211_11B);
2590 case IEEE80211_MODE_FH:
2591 return findmedia(rates, nitems(rates),
2592 rate | IFM_IEEE80211_FH);
2593 case IEEE80211_MODE_AUTO:
2594 /* NB: ic may be NULL for some drivers */
2595 if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
2596 return findmedia(rates, nitems(rates),
2597 rate | IFM_IEEE80211_FH);
2598 /* NB: hack, 11g matches both 11b+11a rates */
2599 /* fall thru... */
2600 case IEEE80211_MODE_11G:
2601 case IEEE80211_MODE_11NG:
2602 case IEEE80211_MODE_TURBO_G:
2603 return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
2604 case IEEE80211_MODE_VHT_2GHZ:
2605 case IEEE80211_MODE_VHT_5GHZ:
2606 /* XXX TODO: need to figure out mapping for VHT rates */
2607 return IFM_AUTO;
2608 }
2609 return IFM_AUTO;
2610 }
2611
2612 int
ieee80211_media2rate(int mword)2613 ieee80211_media2rate(int mword)
2614 {
2615 static const int ieeerates[] = {
2616 -1, /* IFM_AUTO */
2617 0, /* IFM_MANUAL */
2618 0, /* IFM_NONE */
2619 2, /* IFM_IEEE80211_FH1 */
2620 4, /* IFM_IEEE80211_FH2 */
2621 2, /* IFM_IEEE80211_DS1 */
2622 4, /* IFM_IEEE80211_DS2 */
2623 11, /* IFM_IEEE80211_DS5 */
2624 22, /* IFM_IEEE80211_DS11 */
2625 44, /* IFM_IEEE80211_DS22 */
2626 12, /* IFM_IEEE80211_OFDM6 */
2627 18, /* IFM_IEEE80211_OFDM9 */
2628 24, /* IFM_IEEE80211_OFDM12 */
2629 36, /* IFM_IEEE80211_OFDM18 */
2630 48, /* IFM_IEEE80211_OFDM24 */
2631 72, /* IFM_IEEE80211_OFDM36 */
2632 96, /* IFM_IEEE80211_OFDM48 */
2633 108, /* IFM_IEEE80211_OFDM54 */
2634 144, /* IFM_IEEE80211_OFDM72 */
2635 0, /* IFM_IEEE80211_DS354k */
2636 0, /* IFM_IEEE80211_DS512k */
2637 6, /* IFM_IEEE80211_OFDM3 */
2638 9, /* IFM_IEEE80211_OFDM4 */
2639 54, /* IFM_IEEE80211_OFDM27 */
2640 -1, /* IFM_IEEE80211_MCS */
2641 -1, /* IFM_IEEE80211_VHT */
2642 };
2643 return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
2644 ieeerates[IFM_SUBTYPE(mword)] : 0;
2645 }
2646
2647 /*
2648 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2649 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2650 */
2651 #define mix(a, b, c) \
2652 do { \
2653 a -= b; a -= c; a ^= (c >> 13); \
2654 b -= c; b -= a; b ^= (a << 8); \
2655 c -= a; c -= b; c ^= (b >> 13); \
2656 a -= b; a -= c; a ^= (c >> 12); \
2657 b -= c; b -= a; b ^= (a << 16); \
2658 c -= a; c -= b; c ^= (b >> 5); \
2659 a -= b; a -= c; a ^= (c >> 3); \
2660 b -= c; b -= a; b ^= (a << 10); \
2661 c -= a; c -= b; c ^= (b >> 15); \
2662 } while (/*CONSTCOND*/0)
2663
2664 uint32_t
ieee80211_mac_hash(const struct ieee80211com * ic,const uint8_t addr[IEEE80211_ADDR_LEN])2665 ieee80211_mac_hash(const struct ieee80211com *ic,
2666 const uint8_t addr[IEEE80211_ADDR_LEN])
2667 {
2668 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
2669
2670 b += addr[5] << 8;
2671 b += addr[4];
2672 a += addr[3] << 24;
2673 a += addr[2] << 16;
2674 a += addr[1] << 8;
2675 a += addr[0];
2676
2677 mix(a, b, c);
2678
2679 return c;
2680 }
2681 #undef mix
2682
2683 char
ieee80211_channel_type_char(const struct ieee80211_channel * c)2684 ieee80211_channel_type_char(const struct ieee80211_channel *c)
2685 {
2686 if (IEEE80211_IS_CHAN_ST(c))
2687 return 'S';
2688 if (IEEE80211_IS_CHAN_108A(c))
2689 return 'T';
2690 if (IEEE80211_IS_CHAN_108G(c))
2691 return 'G';
2692 if (IEEE80211_IS_CHAN_VHT(c))
2693 return 'v';
2694 if (IEEE80211_IS_CHAN_HT(c))
2695 return 'n';
2696 if (IEEE80211_IS_CHAN_A(c))
2697 return 'a';
2698 if (IEEE80211_IS_CHAN_ANYG(c))
2699 return 'g';
2700 if (IEEE80211_IS_CHAN_B(c))
2701 return 'b';
2702 return 'f';
2703 }
2704
2705 /*
2706 * Determine whether the given key in the given VAP is a global key.
2707 * (key index 0..3, shared between all stations on a VAP.)
2708 *
2709 * This is either a WEP key or a GROUP key.
2710 *
2711 * Note this will NOT return true if it is a IGTK key.
2712 */
2713 bool
ieee80211_is_key_global(const struct ieee80211vap * vap,const struct ieee80211_key * key)2714 ieee80211_is_key_global(const struct ieee80211vap *vap,
2715 const struct ieee80211_key *key)
2716 {
2717 return (&vap->iv_nw_keys[0] <= key &&
2718 key < &vap->iv_nw_keys[IEEE80211_WEP_NKID]);
2719 }
2720
2721 /*
2722 * Determine whether the given key in the given VAP is a unicast key.
2723 */
2724 bool
ieee80211_is_key_unicast(const struct ieee80211vap * vap,const struct ieee80211_key * key)2725 ieee80211_is_key_unicast(const struct ieee80211vap *vap,
2726 const struct ieee80211_key *key)
2727 {
2728 /*
2729 * This is a short-cut for now; eventually we will need
2730 * to support multiple unicast keys, IGTK, etc) so we
2731 * will absolutely need to fix the key flags.
2732 */
2733 return (!ieee80211_is_key_global(vap, key));
2734 }
2735