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