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