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