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