ieee80211.c revision 287197
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3186904Ssam * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
4116742Ssam * All rights reserved.
5116742Ssam *
6116742Ssam * Redistribution and use in source and binary forms, with or without
7116742Ssam * modification, are permitted provided that the following conditions
8116742Ssam * are met:
9116742Ssam * 1. Redistributions of source code must retain the above copyright
10116904Ssam *    notice, this list of conditions and the following disclaimer.
11116904Ssam * 2. Redistributions in binary form must reproduce the above copyright
12116904Ssam *    notice, this list of conditions and the following disclaimer in the
13116904Ssam *    documentation and/or other materials provided with the distribution.
14116742Ssam *
15116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25116742Ssam */
26116742Ssam
27116742Ssam#include <sys/cdefs.h>
28116742Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211.c 287197 2015-08-27 08:56:39Z glebius $");
29116742Ssam
30116742Ssam/*
31116742Ssam * IEEE 802.11 generic handler
32116742Ssam */
33178354Ssam#include "opt_wlan.h"
34116742Ssam
35116742Ssam#include <sys/param.h>
36191746Sthompsa#include <sys/systm.h>
37116742Ssam#include <sys/kernel.h>
38116742Ssam#include <sys/socket.h>
39287197Sglebius#include <sys/sbuf.h>
40116742Ssam
41283529Sglebius#include <machine/stdarg.h>
42283529Sglebius
43116742Ssam#include <net/if.h>
44257176Sglebius#include <net/if_var.h>
45178354Ssam#include <net/if_dl.h>
46116742Ssam#include <net/if_media.h>
47178354Ssam#include <net/if_types.h>
48116742Ssam#include <net/ethernet.h>
49116742Ssam
50116742Ssam#include <net80211/ieee80211_var.h>
51178354Ssam#include <net80211/ieee80211_regdomain.h>
52190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
53190391Ssam#include <net80211/ieee80211_superg.h>
54190391Ssam#endif
55206358Srpaulo#include <net80211/ieee80211_ratectl.h>
56116742Ssam
57116742Ssam#include <net/bpf.h>
58116742Ssam
59178955Ssamconst char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
60178955Ssam	[IEEE80211_MODE_AUTO]	  = "auto",
61178955Ssam	[IEEE80211_MODE_11A]	  = "11a",
62178955Ssam	[IEEE80211_MODE_11B]	  = "11b",
63178955Ssam	[IEEE80211_MODE_11G]	  = "11g",
64178955Ssam	[IEEE80211_MODE_FH]	  = "FH",
65178955Ssam	[IEEE80211_MODE_TURBO_A]  = "turboA",
66178955Ssam	[IEEE80211_MODE_TURBO_G]  = "turboG",
67178955Ssam	[IEEE80211_MODE_STURBO_A] = "sturboA",
68188782Ssam	[IEEE80211_MODE_HALF]	  = "half",
69188782Ssam	[IEEE80211_MODE_QUARTER]  = "quarter",
70178955Ssam	[IEEE80211_MODE_11NA]	  = "11na",
71178955Ssam	[IEEE80211_MODE_11NG]	  = "11ng",
72116742Ssam};
73178957Ssam/* map ieee80211_opmode to the corresponding capability bit */
74178957Ssamconst int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
75178957Ssam	[IEEE80211_M_IBSS]	= IEEE80211_C_IBSS,
76178957Ssam	[IEEE80211_M_WDS]	= IEEE80211_C_WDS,
77178957Ssam	[IEEE80211_M_STA]	= IEEE80211_C_STA,
78178957Ssam	[IEEE80211_M_AHDEMO]	= IEEE80211_C_AHDEMO,
79178957Ssam	[IEEE80211_M_HOSTAP]	= IEEE80211_C_HOSTAP,
80178957Ssam	[IEEE80211_M_MONITOR]	= IEEE80211_C_MONITOR,
81195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
82195618Srpaulo	[IEEE80211_M_MBSS]	= IEEE80211_C_MBSS,
83195618Srpaulo#endif
84178957Ssam};
85178957Ssam
86283566Sglebiusconst uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
87178354Ssam	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
88116742Ssam
89178354Ssamstatic	void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
90193655Ssamstatic	void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
91178354Ssamstatic	void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
92178354Ssamstatic	int ieee80211_media_setup(struct ieee80211com *ic,
93178354Ssam		struct ifmedia *media, int caps, int addsta,
94178354Ssam		ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
95178354Ssamstatic	int media_status(enum ieee80211_opmode,
96178354Ssam		const struct ieee80211_channel *);
97283568Sglebiusstatic uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
98178354Ssam
99178354SsamMALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
100178354Ssam
101164645Ssam/*
102164645Ssam * Default supported rates for 802.11 operation (in IEEE .5Mb units).
103164645Ssam */
104164645Ssam#define	B(r)	((r) | IEEE80211_RATE_BASIC)
105164645Ssamstatic const struct ieee80211_rateset ieee80211_rateset_11a =
106164645Ssam	{ 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
107165569Ssamstatic const struct ieee80211_rateset ieee80211_rateset_half =
108165569Ssam	{ 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
109165569Ssamstatic const struct ieee80211_rateset ieee80211_rateset_quarter =
110165569Ssam	{ 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
111164645Ssamstatic const struct ieee80211_rateset ieee80211_rateset_11b =
112164645Ssam	{ 4, { B(2), B(4), B(11), B(22) } };
113164645Ssam/* NB: OFDM rates are handled specially based on mode */
114164645Ssamstatic const struct ieee80211_rateset ieee80211_rateset_11g =
115164645Ssam	{ 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
116164645Ssam#undef B
117164645Ssam
118140915Ssam/*
119165569Ssam * Fill in 802.11 available channel set, mark
120165569Ssam * all available channels as active, and pick
121165569Ssam * a default channel if not already specified.
122165569Ssam */
123287197Sglebiusvoid
124165569Ssamieee80211_chan_init(struct ieee80211com *ic)
125116742Ssam{
126165569Ssam#define	DEFAULTRATES(m, def) do { \
127188782Ssam	if (ic->ic_sup_rates[m].rs_nrates == 0) \
128165574Ssam		ic->ic_sup_rates[m] = def; \
129165569Ssam} while (0)
130116742Ssam	struct ieee80211_channel *c;
131116742Ssam	int i;
132116742Ssam
133186107Ssam	KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
134170530Ssam		("invalid number of channels specified: %u", ic->ic_nchans));
135116742Ssam	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
136178354Ssam	memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
137167468Ssam	setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
138170530Ssam	for (i = 0; i < ic->ic_nchans; i++) {
139116742Ssam		c = &ic->ic_channels[i];
140170530Ssam		KASSERT(c->ic_flags != 0, ("channel with no flags"));
141187796Ssam		/*
142187796Ssam		 * Help drivers that work only with frequencies by filling
143187796Ssam		 * in IEEE channel #'s if not already calculated.  Note this
144187796Ssam		 * mimics similar work done in ieee80211_setregdomain when
145187796Ssam		 * changing regulatory state.
146187796Ssam		 */
147187796Ssam		if (c->ic_ieee == 0)
148187796Ssam			c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
149187796Ssam		if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
150187796Ssam			c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
151187796Ssam			    (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
152187796Ssam			    c->ic_flags);
153187796Ssam		/* default max tx power to max regulatory */
154187796Ssam		if (c->ic_maxpower == 0)
155187796Ssam			c->ic_maxpower = 2*c->ic_maxregpower;
156170530Ssam		setbit(ic->ic_chan_avail, c->ic_ieee);
157170530Ssam		/*
158170530Ssam		 * Identify mode capabilities.
159170530Ssam		 */
160170530Ssam		if (IEEE80211_IS_CHAN_A(c))
161170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
162170530Ssam		if (IEEE80211_IS_CHAN_B(c))
163170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
164170530Ssam		if (IEEE80211_IS_CHAN_ANYG(c))
165170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
166170530Ssam		if (IEEE80211_IS_CHAN_FHSS(c))
167170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
168170530Ssam		if (IEEE80211_IS_CHAN_108A(c))
169170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
170170530Ssam		if (IEEE80211_IS_CHAN_108G(c))
171170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
172170530Ssam		if (IEEE80211_IS_CHAN_ST(c))
173170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
174188782Ssam		if (IEEE80211_IS_CHAN_HALF(c))
175188782Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
176188782Ssam		if (IEEE80211_IS_CHAN_QUARTER(c))
177188782Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
178170530Ssam		if (IEEE80211_IS_CHAN_HTA(c))
179170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
180170530Ssam		if (IEEE80211_IS_CHAN_HTG(c))
181170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
182116742Ssam	}
183170530Ssam	/* initialize candidate channels to all available */
184170530Ssam	memcpy(ic->ic_chan_active, ic->ic_chan_avail,
185170530Ssam		sizeof(ic->ic_chan_avail));
186164645Ssam
187178354Ssam	/* sort channel table to allow lookup optimizations */
188178354Ssam	ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
189178354Ssam
190178354Ssam	/* invalidate any previous state */
191170530Ssam	ic->ic_bsschan = IEEE80211_CHAN_ANYC;
192172233Ssam	ic->ic_prevchan = NULL;
193178354Ssam	ic->ic_csa_newchan = NULL;
194170530Ssam	/* arbitrarily pick the first channel */
195170530Ssam	ic->ic_curchan = &ic->ic_channels[0];
196190532Ssam	ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
197170530Ssam
198164645Ssam	/* fillin well-known rate sets if driver has not specified */
199165569Ssam	DEFAULTRATES(IEEE80211_MODE_11B,	 ieee80211_rateset_11b);
200165569Ssam	DEFAULTRATES(IEEE80211_MODE_11G,	 ieee80211_rateset_11g);
201165569Ssam	DEFAULTRATES(IEEE80211_MODE_11A,	 ieee80211_rateset_11a);
202165569Ssam	DEFAULTRATES(IEEE80211_MODE_TURBO_A,	 ieee80211_rateset_11a);
203165569Ssam	DEFAULTRATES(IEEE80211_MODE_TURBO_G,	 ieee80211_rateset_11g);
204187897Ssam	DEFAULTRATES(IEEE80211_MODE_STURBO_A,	 ieee80211_rateset_11a);
205188782Ssam	DEFAULTRATES(IEEE80211_MODE_HALF,	 ieee80211_rateset_half);
206188782Ssam	DEFAULTRATES(IEEE80211_MODE_QUARTER,	 ieee80211_rateset_quarter);
207188774Ssam	DEFAULTRATES(IEEE80211_MODE_11NA,	 ieee80211_rateset_11a);
208188774Ssam	DEFAULTRATES(IEEE80211_MODE_11NG,	 ieee80211_rateset_11g);
209165569Ssam
210165569Ssam	/*
211219596Sbschmidt	 * Setup required information to fill the mcsset field, if driver did
212219596Sbschmidt	 * not. Assume a 2T2R setup for historic reasons.
213219596Sbschmidt	 */
214219596Sbschmidt	if (ic->ic_rxstream == 0)
215219596Sbschmidt		ic->ic_rxstream = 2;
216219596Sbschmidt	if (ic->ic_txstream == 0)
217219596Sbschmidt		ic->ic_txstream = 2;
218219596Sbschmidt
219219596Sbschmidt	/*
220165569Ssam	 * Set auto mode to reset active channel state and any desired channel.
221165569Ssam	 */
222165569Ssam	(void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
223165569Ssam#undef DEFAULTRATES
224165569Ssam}
225165569Ssam
226178354Ssamstatic void
227283540Sglebiusnull_update_mcast(struct ieee80211com *ic)
228178354Ssam{
229283540Sglebius
230283540Sglebius	ic_printf(ic, "need multicast update callback\n");
231178354Ssam}
232178354Ssam
233178354Ssamstatic void
234283540Sglebiusnull_update_promisc(struct ieee80211com *ic)
235178354Ssam{
236283540Sglebius
237283540Sglebius	ic_printf(ic, "need promiscuous mode update callback\n");
238178354Ssam}
239178354Ssam
240178521Ssamstatic void
241233452Sadriannull_update_chw(struct ieee80211com *ic)
242233452Sadrian{
243233452Sadrian
244283529Sglebius	ic_printf(ic, "%s: need callback\n", __func__);
245233452Sadrian}
246233452Sadrian
247283529Sglebiusint
248283529Sglebiusic_printf(struct ieee80211com *ic, const char * fmt, ...)
249283529Sglebius{
250283529Sglebius	va_list ap;
251283529Sglebius	int retval;
252283529Sglebius
253283529Sglebius	retval = printf("%s: ", ic->ic_name);
254283529Sglebius	va_start(ap, fmt);
255283529Sglebius	retval += vprintf(fmt, ap);
256283529Sglebius	va_end(ap);
257283529Sglebius	return (retval);
258283529Sglebius}
259283529Sglebius
260287197Sglebiusstatic LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head);
261287197Sglebiusstatic struct mtx ic_list_mtx;
262287197SglebiusMTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF);
263287197Sglebius
264287197Sglebiusstatic int
265287197Sglebiussysctl_ieee80211coms(SYSCTL_HANDLER_ARGS)
266287197Sglebius{
267287197Sglebius	struct ieee80211com *ic;
268287197Sglebius	struct sbuf *sb;
269287197Sglebius	char *sp;
270287197Sglebius	int error;
271287197Sglebius
272287197Sglebius	sb = sbuf_new_auto();
273287197Sglebius	sp = "";
274287197Sglebius	mtx_lock(&ic_list_mtx);
275287197Sglebius	LIST_FOREACH(ic, &ic_head, ic_next) {
276287197Sglebius		sbuf_printf(sb, "%s%s", sp, ic->ic_name);
277287197Sglebius		sp = " ";
278287197Sglebius	}
279287197Sglebius	mtx_unlock(&ic_list_mtx);
280287197Sglebius	sbuf_finish(sb);
281287197Sglebius	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
282287197Sglebius	sbuf_delete(sb);
283287197Sglebius	return (error);
284287197Sglebius}
285287197Sglebius
286287197SglebiusSYSCTL_PROC(_net_wlan, OID_AUTO, devices,
287287197Sglebius    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
288287197Sglebius    sysctl_ieee80211coms, "A", "names of available 802.11 devices");
289287197Sglebius
290178354Ssam/*
291178354Ssam * Attach/setup the common net80211 state.  Called by
292178354Ssam * the driver on attach to prior to creating any vap's.
293178354Ssam */
294165569Ssamvoid
295287197Sglebiusieee80211_ifattach(struct ieee80211com *ic)
296165569Ssam{
297165569Ssam
298283529Sglebius	IEEE80211_LOCK_INIT(ic, ic->ic_name);
299283529Sglebius	IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
300178354Ssam	TAILQ_INIT(&ic->ic_vaps);
301191746Sthompsa
302191746Sthompsa	/* Create a taskqueue for all state changes */
303191746Sthompsa	ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
304191746Sthompsa	    taskqueue_thread_enqueue, &ic->ic_tq);
305230447Sadrian	taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
306283565Sglebius	    ic->ic_name);
307283568Sglebius	ic->ic_ierrors = counter_u64_alloc(M_WAITOK);
308283568Sglebius	ic->ic_oerrors = counter_u64_alloc(M_WAITOK);
309165569Ssam	/*
310165569Ssam	 * Fill in 802.11 available channel set, mark all
311165569Ssam	 * available channels as active, and pick a default
312165569Ssam	 * channel if not already specified.
313165569Ssam	 */
314287197Sglebius	ieee80211_chan_init(ic);
315170530Ssam
316178354Ssam	ic->ic_update_mcast = null_update_mcast;
317178354Ssam	ic->ic_update_promisc = null_update_promisc;
318233452Sadrian	ic->ic_update_chw = null_update_chw;
319116742Ssam
320195379Ssam	ic->ic_hash_key = arc4random();
321155688Ssam	ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
322155688Ssam	ic->ic_lintval = ic->ic_bintval;
323138568Ssam	ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
324138568Ssam
325170530Ssam	ieee80211_crypto_attach(ic);
326138568Ssam	ieee80211_node_attach(ic);
327170530Ssam	ieee80211_power_attach(ic);
328138568Ssam	ieee80211_proto_attach(ic);
329190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
330190391Ssam	ieee80211_superg_attach(ic);
331190391Ssam#endif
332170530Ssam	ieee80211_ht_attach(ic);
333170530Ssam	ieee80211_scan_attach(ic);
334178354Ssam	ieee80211_regdomain_attach(ic);
335193843Ssam	ieee80211_dfs_attach(ic);
336138568Ssam
337178354Ssam	ieee80211_sysctl_attach(ic);
338138568Ssam
339287197Sglebius	mtx_lock(&ic_list_mtx);
340287197Sglebius	LIST_INSERT_HEAD(&ic_head, ic, ic_next);
341287197Sglebius	mtx_unlock(&ic_list_mtx);
342116742Ssam}
343116742Ssam
344178354Ssam/*
345178354Ssam * Detach net80211 state on device detach.  Tear down
346178354Ssam * all vap's and reclaim all common state prior to the
347178354Ssam * device state going away.  Note we may call back into
348178354Ssam * driver; it must be prepared for this.
349178354Ssam */
350116742Ssamvoid
351138568Ssamieee80211_ifdetach(struct ieee80211com *ic)
352116742Ssam{
353178354Ssam	struct ieee80211vap *vap;
354116742Ssam
355287197Sglebius	mtx_lock(&ic_list_mtx);
356287197Sglebius	LIST_REMOVE(ic, ic_next);
357287197Sglebius	mtx_unlock(&ic_list_mtx);
358193337Ssam
359242149Sadrian	/*
360242149Sadrian	 * The VAP is responsible for setting and clearing
361242149Sadrian	 * the VIMAGE context.
362242149Sadrian	 */
363178354Ssam	while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
364178354Ssam		ieee80211_vap_destroy(vap);
365188533Sthompsa	ieee80211_waitfor_parent(ic);
366138568Ssam
367138568Ssam	ieee80211_sysctl_detach(ic);
368193843Ssam	ieee80211_dfs_detach(ic);
369178354Ssam	ieee80211_regdomain_detach(ic);
370170530Ssam	ieee80211_scan_detach(ic);
371190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
372190391Ssam	ieee80211_superg_detach(ic);
373190391Ssam#endif
374170530Ssam	ieee80211_ht_detach(ic);
375166012Ssam	/* NB: must be called before ieee80211_node_detach */
376138568Ssam	ieee80211_proto_detach(ic);
377138568Ssam	ieee80211_crypto_detach(ic);
378170530Ssam	ieee80211_power_detach(ic);
379138568Ssam	ieee80211_node_detach(ic);
380193337Ssam
381283568Sglebius	counter_u64_free(ic->ic_ierrors);
382283568Sglebius	counter_u64_free(ic->ic_oerrors);
383242149Sadrian
384191746Sthompsa	taskqueue_free(ic->ic_tq);
385248069Sadrian	IEEE80211_TX_LOCK_DESTROY(ic);
386170530Ssam	IEEE80211_LOCK_DESTROY(ic);
387178354Ssam}
388138568Ssam
389287197Sglebiusstruct ieee80211com *
390287197Sglebiusieee80211_find_com(const char *name)
391287197Sglebius{
392287197Sglebius	struct ieee80211com *ic;
393287197Sglebius
394287197Sglebius	mtx_lock(&ic_list_mtx);
395287197Sglebius	LIST_FOREACH(ic, &ic_head, ic_next)
396287197Sglebius		if (strcmp(ic->ic_name, name) == 0)
397287197Sglebius			break;
398287197Sglebius	mtx_unlock(&ic_list_mtx);
399287197Sglebius
400287197Sglebius	return (ic);
401287197Sglebius}
402287197Sglebius
403178354Ssam/*
404178354Ssam * Default reset method for use with the ioctl support.  This
405178354Ssam * method is invoked after any state change in the 802.11
406178354Ssam * layer that should be propagated to the hardware but not
407178354Ssam * require re-initialization of the 802.11 state machine (e.g
408178354Ssam * rescanning for an ap).  We always return ENETRESET which
409178354Ssam * should cause the driver to re-initialize the device. Drivers
410178354Ssam * can override this method to implement more optimized support.
411178354Ssam */
412178354Ssamstatic int
413178354Ssamdefault_reset(struct ieee80211vap *vap, u_long cmd)
414178354Ssam{
415178354Ssam	return ENETRESET;
416178354Ssam}
417178354Ssam
418178354Ssam/*
419283568Sglebius * Add underlying device errors to vap errors.
420283568Sglebius */
421283568Sglebiusstatic uint64_t
422283568Sglebiusieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
423283568Sglebius{
424283568Sglebius	struct ieee80211vap *vap = ifp->if_softc;
425283568Sglebius	struct ieee80211com *ic = vap->iv_ic;
426283568Sglebius	uint64_t rv;
427283568Sglebius
428283568Sglebius	rv = if_get_counter_default(ifp, cnt);
429283568Sglebius	switch (cnt) {
430283568Sglebius	case IFCOUNTER_OERRORS:
431283568Sglebius		rv += counter_u64_fetch(ic->ic_oerrors);
432283568Sglebius		break;
433283568Sglebius	case IFCOUNTER_IERRORS:
434283568Sglebius		rv += counter_u64_fetch(ic->ic_ierrors);
435283568Sglebius		break;
436283568Sglebius	default:
437283568Sglebius		break;
438283568Sglebius	}
439283568Sglebius
440283568Sglebius	return (rv);
441283568Sglebius}
442283568Sglebius
443283568Sglebius/*
444178354Ssam * Prepare a vap for use.  Drivers use this call to
445178354Ssam * setup net80211 state in new vap's prior attaching
446178354Ssam * them with ieee80211_vap_attach (below).
447178354Ssam */
448178354Ssamint
449178354Ssamieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
450228621Sbschmidt    const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
451287197Sglebius    int flags, const uint8_t bssid[IEEE80211_ADDR_LEN])
452178354Ssam{
453178354Ssam	struct ifnet *ifp;
454178354Ssam
455178354Ssam	ifp = if_alloc(IFT_ETHER);
456178354Ssam	if (ifp == NULL) {
457283529Sglebius		ic_printf(ic, "%s: unable to allocate ifnet\n",
458178354Ssam		    __func__);
459178354Ssam		return ENOMEM;
460178354Ssam	}
461178354Ssam	if_initname(ifp, name, unit);
462178354Ssam	ifp->if_softc = vap;			/* back pointer */
463178354Ssam	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
464254082Sadrian	ifp->if_transmit = ieee80211_vap_transmit;
465254082Sadrian	ifp->if_qflush = ieee80211_vap_qflush;
466178354Ssam	ifp->if_ioctl = ieee80211_ioctl;
467178354Ssam	ifp->if_init = ieee80211_init;
468283568Sglebius	ifp->if_get_counter = ieee80211_get_counter;
469178354Ssam
470178354Ssam	vap->iv_ifp = ifp;
471178354Ssam	vap->iv_ic = ic;
472178354Ssam	vap->iv_flags = ic->ic_flags;		/* propagate common flags */
473178354Ssam	vap->iv_flags_ext = ic->ic_flags_ext;
474178354Ssam	vap->iv_flags_ven = ic->ic_flags_ven;
475178354Ssam	vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
476178354Ssam	vap->iv_htcaps = ic->ic_htcaps;
477205513Srpaulo	vap->iv_htextcaps = ic->ic_htextcaps;
478178354Ssam	vap->iv_opmode = opmode;
479178957Ssam	vap->iv_caps |= ieee80211_opcap[opmode];
480287197Sglebius	vap->iv_myaddr = ic->ic_macaddr;
481178354Ssam	switch (opmode) {
482178354Ssam	case IEEE80211_M_WDS:
483178354Ssam		/*
484178354Ssam		 * WDS links must specify the bssid of the far end.
485178354Ssam		 * For legacy operation this is a static relationship.
486178354Ssam		 * For non-legacy operation the station must associate
487178354Ssam		 * and be authorized to pass traffic.  Plumbing the
488178354Ssam		 * vap to the proper node happens when the vap
489178354Ssam		 * transitions to RUN state.
490178354Ssam		 */
491178354Ssam		IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
492178354Ssam		vap->iv_flags |= IEEE80211_F_DESBSSID;
493178354Ssam		if (flags & IEEE80211_CLONE_WDSLEGACY)
494178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
495178354Ssam		break;
496186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
497186904Ssam	case IEEE80211_M_AHDEMO:
498186904Ssam		if (flags & IEEE80211_CLONE_TDMA) {
499186904Ssam			/* NB: checked before clone operation allowed */
500186904Ssam			KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
501186904Ssam			    ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
502186904Ssam			/*
503186904Ssam			 * Propagate TDMA capability to mark vap; this
504186904Ssam			 * cannot be removed and is used to distinguish
505186904Ssam			 * regular ahdemo operation from ahdemo+tdma.
506186904Ssam			 */
507186904Ssam			vap->iv_caps |= IEEE80211_C_TDMA;
508186904Ssam		}
509186904Ssam		break;
510186904Ssam#endif
511228621Sbschmidt	default:
512228621Sbschmidt		break;
513178354Ssam	}
514184278Ssam	/* auto-enable s/w beacon miss support */
515184278Ssam	if (flags & IEEE80211_CLONE_NOBEACONS)
516184278Ssam		vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
517202612Sthompsa	/* auto-generated or user supplied MAC address */
518202612Sthompsa	if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
519202612Sthompsa		vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
520178354Ssam	/*
521178354Ssam	 * Enable various functionality by default if we're
522178354Ssam	 * capable; the driver can override us if it knows better.
523178354Ssam	 */
524178354Ssam	if (vap->iv_caps & IEEE80211_C_WME)
525178354Ssam		vap->iv_flags |= IEEE80211_F_WME;
526178354Ssam	if (vap->iv_caps & IEEE80211_C_BURST)
527178354Ssam		vap->iv_flags |= IEEE80211_F_BURST;
528178354Ssam	/* NB: bg scanning only makes sense for station mode right now */
529178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA &&
530178354Ssam	    (vap->iv_caps & IEEE80211_C_BGSCAN))
531178354Ssam		vap->iv_flags |= IEEE80211_F_BGSCAN;
532178957Ssam	vap->iv_flags |= IEEE80211_F_DOTH;	/* XXX no cap, just ena */
533178954Ssam	/* NB: DFS support only makes sense for ap mode right now */
534178954Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
535178954Ssam	    (vap->iv_caps & IEEE80211_C_DFS))
536178354Ssam		vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
537178354Ssam
538178354Ssam	vap->iv_des_chan = IEEE80211_CHAN_ANYC;		/* any channel is ok */
539178354Ssam	vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
540178354Ssam	vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
541178354Ssam	/*
542178354Ssam	 * Install a default reset method for the ioctl support;
543178354Ssam	 * the driver can override this.
544178354Ssam	 */
545178354Ssam	vap->iv_reset = default_reset;
546178354Ssam
547178354Ssam	ieee80211_sysctl_vattach(vap);
548178354Ssam	ieee80211_crypto_vattach(vap);
549178354Ssam	ieee80211_node_vattach(vap);
550178354Ssam	ieee80211_power_vattach(vap);
551178354Ssam	ieee80211_proto_vattach(vap);
552190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
553190391Ssam	ieee80211_superg_vattach(vap);
554190391Ssam#endif
555178354Ssam	ieee80211_ht_vattach(vap);
556178354Ssam	ieee80211_scan_vattach(vap);
557178354Ssam	ieee80211_regdomain_vattach(vap);
558192468Ssam	ieee80211_radiotap_vattach(vap);
559214894Sbschmidt	ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
560178354Ssam
561178354Ssam	return 0;
562178354Ssam}
563178354Ssam
564178354Ssam/*
565178354Ssam * Activate a vap.  State should have been prepared with a
566178354Ssam * call to ieee80211_vap_setup and by the driver.  On return
567178354Ssam * from this call the vap is ready for use.
568178354Ssam */
569178354Ssamint
570287197Sglebiusieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change,
571287197Sglebius    ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN])
572178354Ssam{
573178354Ssam	struct ifnet *ifp = vap->iv_ifp;
574178354Ssam	struct ieee80211com *ic = vap->iv_ic;
575178354Ssam	struct ifmediareq imr;
576178354Ssam	int maxrate;
577178354Ssam
578178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
579178354Ssam	    "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
580178354Ssam	    __func__, ieee80211_opmode_name[vap->iv_opmode],
581283529Sglebius	    ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
582178354Ssam
583178354Ssam	/*
584178354Ssam	 * Do late attach work that cannot happen until after
585178354Ssam	 * the driver has had a chance to override defaults.
586178354Ssam	 */
587178354Ssam	ieee80211_node_latevattach(vap);
588178354Ssam	ieee80211_power_latevattach(vap);
589178354Ssam
590178354Ssam	maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
591178354Ssam	    vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
592178354Ssam	ieee80211_media_status(ifp, &imr);
593178354Ssam	/* NB: strip explicit mode; we're actually in autoselect */
594188106Ssam	ifmedia_set(&vap->iv_media,
595188106Ssam	    imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
596178354Ssam	if (maxrate)
597178354Ssam		ifp->if_baudrate = IF_Mbps(maxrate);
598178354Ssam
599287197Sglebius	ether_ifattach(ifp, macaddr);
600287197Sglebius	vap->iv_myaddr = IF_LLADDR(ifp);
601269778Sadrian	/* hook output method setup by ether_ifattach */
602269778Sadrian	vap->iv_output = ifp->if_output;
603269778Sadrian	ifp->if_output = ieee80211_output;
604178354Ssam	/* NB: if_mtu set by ether_ifattach to ETHERMTU */
605178354Ssam
606178354Ssam	IEEE80211_LOCK(ic);
607178354Ssam	TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
608178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
609190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
610178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
611190391Ssam#endif
612178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
613178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
614193655Ssam	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
615193655Ssam	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
616178354Ssam	IEEE80211_UNLOCK(ic);
617178354Ssam
618178354Ssam	return 1;
619178354Ssam}
620178354Ssam
621178354Ssam/*
622178354Ssam * Tear down vap state and reclaim the ifnet.
623178354Ssam * The driver is assumed to have prepared for
624178354Ssam * this; e.g. by turning off interrupts for the
625178354Ssam * underlying device.
626178354Ssam */
627178354Ssamvoid
628178354Ssamieee80211_vap_detach(struct ieee80211vap *vap)
629178354Ssam{
630178354Ssam	struct ieee80211com *ic = vap->iv_ic;
631178354Ssam	struct ifnet *ifp = vap->iv_ifp;
632178354Ssam
633242149Sadrian	CURVNET_SET(ifp->if_vnet);
634242149Sadrian
635178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
636283565Sglebius	    __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
637178354Ssam
638193312Ssam	/* NB: bpfdetach is called by ether_ifdetach and claims all taps */
639193312Ssam	ether_ifdetach(ifp);
640178354Ssam
641193312Ssam	ieee80211_stop(vap);
642193312Ssam
643191746Sthompsa	/*
644191746Sthompsa	 * Flush any deferred vap tasks.
645191746Sthompsa	 */
646191746Sthompsa	ieee80211_draintask(ic, &vap->iv_nstate_task);
647191746Sthompsa	ieee80211_draintask(ic, &vap->iv_swbmiss_task);
648191746Sthompsa
649196159Ssam	/* XXX band-aid until ifnet handles this for us */
650196159Ssam	taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
651196159Ssam
652191746Sthompsa	IEEE80211_LOCK(ic);
653191746Sthompsa	KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
654178354Ssam	TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
655178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
656190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
657178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
658190391Ssam#endif
659178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
660178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
661193655Ssam	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
662193655Ssam	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
663192468Ssam	/* NB: this handles the bpfdetach done below */
664192468Ssam	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
665287197Sglebius	if (vap->iv_ifflags & IFF_PROMISC)
666287197Sglebius		ieee80211_promisc(vap, false);
667287197Sglebius	if (vap->iv_ifflags & IFF_ALLMULTI)
668287197Sglebius		ieee80211_allmulti(vap, false);
669178354Ssam	IEEE80211_UNLOCK(ic);
670178354Ssam
671178354Ssam	ifmedia_removeall(&vap->iv_media);
672178354Ssam
673192468Ssam	ieee80211_radiotap_vdetach(vap);
674178354Ssam	ieee80211_regdomain_vdetach(vap);
675178354Ssam	ieee80211_scan_vdetach(vap);
676190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
677190391Ssam	ieee80211_superg_vdetach(vap);
678190391Ssam#endif
679178354Ssam	ieee80211_ht_vdetach(vap);
680178354Ssam	/* NB: must be before ieee80211_node_vdetach */
681178354Ssam	ieee80211_proto_vdetach(vap);
682178354Ssam	ieee80211_crypto_vdetach(vap);
683178354Ssam	ieee80211_power_vdetach(vap);
684178354Ssam	ieee80211_node_vdetach(vap);
685178354Ssam	ieee80211_sysctl_vdetach(vap);
686182674Sweongyo
687182674Sweongyo	if_free(ifp);
688242149Sadrian
689242149Sadrian	CURVNET_RESTORE();
690116742Ssam}
691116742Ssam
692178354Ssam/*
693287197Sglebius * Count number of vaps in promisc, and issue promisc on
694287197Sglebius * parent respectively.
695178354Ssam */
696178354Ssamvoid
697287197Sglebiusieee80211_promisc(struct ieee80211vap *vap, bool on)
698178354Ssam{
699287197Sglebius	struct ieee80211com *ic = vap->iv_ic;
700178354Ssam
701287197Sglebius	/*
702287197Sglebius	 * XXX the bridge sets PROMISC but we don't want to
703287197Sglebius	 * enable it on the device, discard here so all the
704287197Sglebius	 * drivers don't need to special-case it
705287197Sglebius	 */
706287197Sglebius	if (!(vap->iv_opmode == IEEE80211_M_MONITOR ||
707287197Sglebius	      (vap->iv_opmode == IEEE80211_M_AHDEMO &&
708287197Sglebius	       (vap->iv_caps & IEEE80211_C_TDMA) == 0)))
709287197Sglebius			return;
710178354Ssam
711287197Sglebius	IEEE80211_LOCK(ic);
712287197Sglebius	if (on) {
713287197Sglebius		if (++ic->ic_promisc == 1)
714287197Sglebius			ieee80211_runtask(ic, &ic->ic_promisc_task);
715287197Sglebius	} else {
716287197Sglebius		KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc",
717287197Sglebius		    __func__, ic));
718287197Sglebius		if (--ic->ic_promisc == 0)
719287197Sglebius			ieee80211_runtask(ic, &ic->ic_promisc_task);
720178354Ssam	}
721287197Sglebius	IEEE80211_UNLOCK(ic);
722178354Ssam}
723178354Ssam
724178354Ssam/*
725287197Sglebius * Count number of vaps in allmulti, and issue allmulti on
726287197Sglebius * parent respectively.
727287197Sglebius */
728287197Sglebiusvoid
729287197Sglebiusieee80211_allmulti(struct ieee80211vap *vap, bool on)
730287197Sglebius{
731287197Sglebius	struct ieee80211com *ic = vap->iv_ic;
732287197Sglebius
733287197Sglebius	IEEE80211_LOCK(ic);
734287197Sglebius	if (on) {
735287197Sglebius		if (++ic->ic_allmulti == 1)
736287197Sglebius			ieee80211_runtask(ic, &ic->ic_mcast_task);
737287197Sglebius	} else {
738287197Sglebius		KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti",
739287197Sglebius		    __func__, ic));
740287197Sglebius		if (--ic->ic_allmulti == 0)
741287197Sglebius			ieee80211_runtask(ic, &ic->ic_mcast_task);
742287197Sglebius	}
743287197Sglebius	IEEE80211_UNLOCK(ic);
744287197Sglebius}
745287197Sglebius
746287197Sglebius/*
747178354Ssam * Synchronize flag bit state in the com structure
748178354Ssam * according to the state of all vap's.  This is used,
749178354Ssam * for example, to handle state changes via ioctls.
750178354Ssam */
751178354Ssamstatic void
752178354Ssamieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
753178354Ssam{
754178354Ssam	struct ieee80211vap *vap;
755178354Ssam	int bit;
756178354Ssam
757178354Ssam	IEEE80211_LOCK_ASSERT(ic);
758178354Ssam
759178354Ssam	bit = 0;
760178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
761178354Ssam		if (vap->iv_flags & flag) {
762178354Ssam			bit = 1;
763178354Ssam			break;
764178354Ssam		}
765178354Ssam	if (bit)
766178354Ssam		ic->ic_flags |= flag;
767178354Ssam	else
768178354Ssam		ic->ic_flags &= ~flag;
769178354Ssam}
770178354Ssam
771178354Ssamvoid
772178354Ssamieee80211_syncflag(struct ieee80211vap *vap, int flag)
773178354Ssam{
774178354Ssam	struct ieee80211com *ic = vap->iv_ic;
775178354Ssam
776178354Ssam	IEEE80211_LOCK(ic);
777178354Ssam	if (flag < 0) {
778178354Ssam		flag = -flag;
779178354Ssam		vap->iv_flags &= ~flag;
780178354Ssam	} else
781178354Ssam		vap->iv_flags |= flag;
782178354Ssam	ieee80211_syncflag_locked(ic, flag);
783178354Ssam	IEEE80211_UNLOCK(ic);
784178354Ssam}
785178354Ssam
786178354Ssam/*
787193655Ssam * Synchronize flags_ht bit state in the com structure
788178354Ssam * according to the state of all vap's.  This is used,
789178354Ssam * for example, to handle state changes via ioctls.
790178354Ssam */
791178354Ssamstatic void
792193655Ssamieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
793193655Ssam{
794193655Ssam	struct ieee80211vap *vap;
795193655Ssam	int bit;
796193655Ssam
797193655Ssam	IEEE80211_LOCK_ASSERT(ic);
798193655Ssam
799193655Ssam	bit = 0;
800193655Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
801193655Ssam		if (vap->iv_flags_ht & flag) {
802193655Ssam			bit = 1;
803193655Ssam			break;
804193655Ssam		}
805193655Ssam	if (bit)
806193655Ssam		ic->ic_flags_ht |= flag;
807193655Ssam	else
808193655Ssam		ic->ic_flags_ht &= ~flag;
809193655Ssam}
810193655Ssam
811193655Ssamvoid
812193655Ssamieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
813193655Ssam{
814193655Ssam	struct ieee80211com *ic = vap->iv_ic;
815193655Ssam
816193655Ssam	IEEE80211_LOCK(ic);
817193655Ssam	if (flag < 0) {
818193655Ssam		flag = -flag;
819193655Ssam		vap->iv_flags_ht &= ~flag;
820193655Ssam	} else
821193655Ssam		vap->iv_flags_ht |= flag;
822193655Ssam	ieee80211_syncflag_ht_locked(ic, flag);
823193655Ssam	IEEE80211_UNLOCK(ic);
824193655Ssam}
825193655Ssam
826193655Ssam/*
827193655Ssam * Synchronize flags_ext bit state in the com structure
828193655Ssam * according to the state of all vap's.  This is used,
829193655Ssam * for example, to handle state changes via ioctls.
830193655Ssam */
831193655Ssamstatic void
832178354Ssamieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
833178354Ssam{
834178354Ssam	struct ieee80211vap *vap;
835178354Ssam	int bit;
836178354Ssam
837178354Ssam	IEEE80211_LOCK_ASSERT(ic);
838178354Ssam
839178354Ssam	bit = 0;
840178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
841178354Ssam		if (vap->iv_flags_ext & flag) {
842178354Ssam			bit = 1;
843178354Ssam			break;
844178354Ssam		}
845178354Ssam	if (bit)
846178354Ssam		ic->ic_flags_ext |= flag;
847178354Ssam	else
848178354Ssam		ic->ic_flags_ext &= ~flag;
849178354Ssam}
850178354Ssam
851178354Ssamvoid
852178354Ssamieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
853178354Ssam{
854178354Ssam	struct ieee80211com *ic = vap->iv_ic;
855178354Ssam
856178354Ssam	IEEE80211_LOCK(ic);
857178354Ssam	if (flag < 0) {
858178354Ssam		flag = -flag;
859178354Ssam		vap->iv_flags_ext &= ~flag;
860178354Ssam	} else
861178354Ssam		vap->iv_flags_ext |= flag;
862178354Ssam	ieee80211_syncflag_ext_locked(ic, flag);
863178354Ssam	IEEE80211_UNLOCK(ic);
864178354Ssam}
865178354Ssam
866166012Ssamstatic __inline int
867166012Ssammapgsm(u_int freq, u_int flags)
868166012Ssam{
869166012Ssam	freq *= 10;
870166012Ssam	if (flags & IEEE80211_CHAN_QUARTER)
871166012Ssam		freq += 5;
872166012Ssam	else if (flags & IEEE80211_CHAN_HALF)
873166012Ssam		freq += 10;
874166012Ssam	else
875166012Ssam		freq += 20;
876166012Ssam	/* NB: there is no 907/20 wide but leave room */
877166012Ssam	return (freq - 906*10) / 5;
878166012Ssam}
879166012Ssam
880166012Ssamstatic __inline int
881166012Ssammappsb(u_int freq, u_int flags)
882166012Ssam{
883166012Ssam	return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
884166012Ssam}
885166012Ssam
886116742Ssam/*
887116742Ssam * Convert MHz frequency to IEEE channel number.
888116742Ssam */
889152450Ssamint
890116742Ssamieee80211_mhz2ieee(u_int freq, u_int flags)
891116742Ssam{
892167430Ssam#define	IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
893166012Ssam	if (flags & IEEE80211_CHAN_GSM)
894166012Ssam		return mapgsm(freq, flags);
895116742Ssam	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
896116742Ssam		if (freq == 2484)
897116742Ssam			return 14;
898116742Ssam		if (freq < 2484)
899152450Ssam			return ((int) freq - 2407) / 5;
900116742Ssam		else
901116742Ssam			return 15 + ((freq - 2512) / 20);
902116899Ssam	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
903165569Ssam		if (freq <= 5000) {
904170530Ssam			/* XXX check regdomain? */
905167430Ssam			if (IS_FREQ_IN_PSB(freq))
906166012Ssam				return mappsb(freq, flags);
907152450Ssam			return (freq - 4000) / 5;
908165569Ssam		} else
909152450Ssam			return (freq - 5000) / 5;
910116742Ssam	} else {				/* either, guess */
911116742Ssam		if (freq == 2484)
912116742Ssam			return 14;
913166012Ssam		if (freq < 2484) {
914166012Ssam			if (907 <= freq && freq <= 922)
915166012Ssam				return mapgsm(freq, flags);
916152450Ssam			return ((int) freq - 2407) / 5;
917166012Ssam		}
918152450Ssam		if (freq < 5000) {
919167430Ssam			if (IS_FREQ_IN_PSB(freq))
920166012Ssam				return mappsb(freq, flags);
921165569Ssam			else if (freq > 4900)
922152450Ssam				return (freq - 4000) / 5;
923152450Ssam			else
924152450Ssam				return 15 + ((freq - 2512) / 20);
925152450Ssam		}
926116742Ssam		return (freq - 5000) / 5;
927116742Ssam	}
928167430Ssam#undef IS_FREQ_IN_PSB
929116742Ssam}
930116742Ssam
931116742Ssam/*
932116742Ssam * Convert channel to IEEE channel number.
933116742Ssam */
934152450Ssamint
935165825Smjacobieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
936116742Ssam{
937170530Ssam	if (c == NULL) {
938283529Sglebius		ic_printf(ic, "invalid channel (NULL)\n");
939117039Ssam		return 0;		/* XXX */
940116742Ssam	}
941170530Ssam	return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
942116742Ssam}
943116742Ssam
944116742Ssam/*
945116742Ssam * Convert IEEE channel number to MHz frequency.
946116742Ssam */
947116742Ssamu_int
948116742Ssamieee80211_ieee2mhz(u_int chan, u_int flags)
949116742Ssam{
950166012Ssam	if (flags & IEEE80211_CHAN_GSM)
951166012Ssam		return 907 + 5 * (chan / 10);
952116742Ssam	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
953116742Ssam		if (chan == 14)
954116742Ssam			return 2484;
955116742Ssam		if (chan < 14)
956116742Ssam			return 2407 + chan*5;
957116742Ssam		else
958116742Ssam			return 2512 + ((chan-15)*20);
959116742Ssam	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
960165569Ssam		if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
961165569Ssam			chan -= 37;
962165569Ssam			return 4940 + chan*5 + (chan % 5 ? 2 : 0);
963165569Ssam		}
964116742Ssam		return 5000 + (chan*5);
965116742Ssam	} else {				/* either, guess */
966166012Ssam		/* XXX can't distinguish PSB+GSM channels */
967116742Ssam		if (chan == 14)
968116742Ssam			return 2484;
969116742Ssam		if (chan < 14)			/* 0-13 */
970116742Ssam			return 2407 + chan*5;
971116742Ssam		if (chan < 27)			/* 15-26 */
972116742Ssam			return 2512 + ((chan-15)*20);
973116742Ssam		return 5000 + (chan*5);
974116742Ssam	}
975116742Ssam}
976116742Ssam
977116742Ssam/*
978170530Ssam * Locate a channel given a frequency+flags.  We cache
979178354Ssam * the previous lookup to optimize switching between two
980170530Ssam * channels--as happens with dynamic turbo.
981170530Ssam */
982170530Ssamstruct ieee80211_channel *
983170530Ssamieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
984170530Ssam{
985170530Ssam	struct ieee80211_channel *c;
986170530Ssam	int i;
987170530Ssam
988170530Ssam	flags &= IEEE80211_CHAN_ALLTURBO;
989170530Ssam	c = ic->ic_prevchan;
990170530Ssam	if (c != NULL && c->ic_freq == freq &&
991170530Ssam	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
992170530Ssam		return c;
993170530Ssam	/* brute force search */
994170530Ssam	for (i = 0; i < ic->ic_nchans; i++) {
995170530Ssam		c = &ic->ic_channels[i];
996170530Ssam		if (c->ic_freq == freq &&
997170530Ssam		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
998170530Ssam			return c;
999170530Ssam	}
1000170530Ssam	return NULL;
1001170530Ssam}
1002170530Ssam
1003173861Ssam/*
1004173861Ssam * Locate a channel given a channel number+flags.  We cache
1005173861Ssam * the previous lookup to optimize switching between two
1006173861Ssam * channels--as happens with dynamic turbo.
1007173861Ssam */
1008173861Ssamstruct ieee80211_channel *
1009173861Ssamieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1010173861Ssam{
1011173861Ssam	struct ieee80211_channel *c;
1012173861Ssam	int i;
1013173861Ssam
1014173861Ssam	flags &= IEEE80211_CHAN_ALLTURBO;
1015173861Ssam	c = ic->ic_prevchan;
1016173861Ssam	if (c != NULL && c->ic_ieee == ieee &&
1017173861Ssam	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1018173861Ssam		return c;
1019173861Ssam	/* brute force search */
1020173861Ssam	for (i = 0; i < ic->ic_nchans; i++) {
1021173861Ssam		c = &ic->ic_channels[i];
1022173861Ssam		if (c->ic_ieee == ieee &&
1023173861Ssam		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1024173861Ssam			return c;
1025173861Ssam	}
1026173861Ssam	return NULL;
1027173861Ssam}
1028173861Ssam
1029283535Sadrian/*
1030283535Sadrian * Lookup a channel suitable for the given rx status.
1031283535Sadrian *
1032283535Sadrian * This is used to find a channel for a frame (eg beacon, probe
1033283535Sadrian * response) based purely on the received PHY information.
1034283535Sadrian *
1035283535Sadrian * For now it tries to do it based on R_FREQ / R_IEEE.
1036283535Sadrian * This is enough for 11bg and 11a (and thus 11ng/11na)
1037283535Sadrian * but it will not be enough for GSM, PSB channels and the
1038283535Sadrian * like.  It also doesn't know about legacy-turbog and
1039283535Sadrian * legacy-turbo modes, which some offload NICs actually
1040283535Sadrian * support in weird ways.
1041283535Sadrian *
1042283535Sadrian * Takes the ic and rxstatus; returns the channel or NULL
1043283535Sadrian * if not found.
1044283535Sadrian *
1045283535Sadrian * XXX TODO: Add support for that when the need arises.
1046283535Sadrian */
1047283535Sadrianstruct ieee80211_channel *
1048283535Sadrianieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1049283535Sadrian    const struct ieee80211_rx_stats *rxs)
1050283535Sadrian{
1051283535Sadrian	struct ieee80211com *ic = vap->iv_ic;
1052283535Sadrian	uint32_t flags;
1053283535Sadrian	struct ieee80211_channel *c;
1054283535Sadrian
1055283535Sadrian	if (rxs == NULL)
1056283535Sadrian		return (NULL);
1057283535Sadrian
1058283535Sadrian	/*
1059283535Sadrian	 * Strictly speaking we only use freq for now,
1060283535Sadrian	 * however later on we may wish to just store
1061283535Sadrian	 * the ieee for verification.
1062283535Sadrian	 */
1063283535Sadrian	if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1064283535Sadrian		return (NULL);
1065283535Sadrian	if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1066283535Sadrian		return (NULL);
1067283535Sadrian
1068283535Sadrian	/*
1069283535Sadrian	 * If the rx status contains a valid ieee/freq, then
1070283535Sadrian	 * ensure we populate the correct channel information
1071283535Sadrian	 * in rxchan before passing it up to the scan infrastructure.
1072283535Sadrian	 * Offload NICs will pass up beacons from all channels
1073283535Sadrian	 * during background scans.
1074283535Sadrian	 */
1075283535Sadrian
1076283535Sadrian	/* Determine a band */
1077283535Sadrian	/* XXX should be done by the driver? */
1078283535Sadrian	if (rxs->c_freq < 3000) {
1079284014Sadrian		flags = IEEE80211_CHAN_G;
1080283535Sadrian	} else {
1081283535Sadrian		flags = IEEE80211_CHAN_A;
1082283535Sadrian	}
1083283535Sadrian
1084283535Sadrian	/* Channel lookup */
1085283535Sadrian	c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1086283535Sadrian
1087283535Sadrian	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1088283535Sadrian	    "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1089283535Sadrian	    __func__,
1090283535Sadrian	    (int) rxs->c_freq,
1091283535Sadrian	    (int) rxs->c_ieee,
1092283535Sadrian	    flags,
1093283535Sadrian	    c);
1094283535Sadrian
1095283535Sadrian	return (c);
1096283535Sadrian}
1097283535Sadrian
1098170530Ssamstatic void
1099178354Ssamaddmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1100170530Ssam{
1101170530Ssam#define	ADD(_ic, _s, _o) \
1102178354Ssam	ifmedia_add(media, \
1103170530Ssam		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1104170530Ssam	static const u_int mopts[IEEE80211_MODE_MAX] = {
1105188106Ssam	    [IEEE80211_MODE_AUTO]	= IFM_AUTO,
1106188106Ssam	    [IEEE80211_MODE_11A]	= IFM_IEEE80211_11A,
1107188106Ssam	    [IEEE80211_MODE_11B]	= IFM_IEEE80211_11B,
1108188106Ssam	    [IEEE80211_MODE_11G]	= IFM_IEEE80211_11G,
1109188106Ssam	    [IEEE80211_MODE_FH]		= IFM_IEEE80211_FH,
1110188106Ssam	    [IEEE80211_MODE_TURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1111188106Ssam	    [IEEE80211_MODE_TURBO_G]	= IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1112188106Ssam	    [IEEE80211_MODE_STURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1113188782Ssam	    [IEEE80211_MODE_HALF]	= IFM_IEEE80211_11A,	/* XXX */
1114188782Ssam	    [IEEE80211_MODE_QUARTER]	= IFM_IEEE80211_11A,	/* XXX */
1115188106Ssam	    [IEEE80211_MODE_11NA]	= IFM_IEEE80211_11NA,
1116188106Ssam	    [IEEE80211_MODE_11NG]	= IFM_IEEE80211_11NG,
1117170530Ssam	};
1118170530Ssam	u_int mopt;
1119170530Ssam
1120170530Ssam	mopt = mopts[mode];
1121178354Ssam	if (addsta)
1122178354Ssam		ADD(ic, mword, mopt);	/* STA mode has no cap */
1123178354Ssam	if (caps & IEEE80211_C_IBSS)
1124178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1125178354Ssam	if (caps & IEEE80211_C_HOSTAP)
1126178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1127178354Ssam	if (caps & IEEE80211_C_AHDEMO)
1128178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1129178354Ssam	if (caps & IEEE80211_C_MONITOR)
1130178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1131178354Ssam	if (caps & IEEE80211_C_WDS)
1132178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1133195618Srpaulo	if (caps & IEEE80211_C_MBSS)
1134195618Srpaulo		ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1135170530Ssam#undef ADD
1136170530Ssam}
1137170530Ssam
1138170530Ssam/*
1139116742Ssam * Setup the media data structures according to the channel and
1140178354Ssam * rate tables.
1141116742Ssam */
1142178354Ssamstatic int
1143178354Ssamieee80211_media_setup(struct ieee80211com *ic,
1144178354Ssam	struct ifmedia *media, int caps, int addsta,
1145116742Ssam	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1146116742Ssam{
1147228621Sbschmidt	int i, j, rate, maxrate, mword, r;
1148228621Sbschmidt	enum ieee80211_phymode mode;
1149170530Ssam	const struct ieee80211_rateset *rs;
1150116742Ssam	struct ieee80211_rateset allrates;
1151116742Ssam
1152118887Ssam	/*
1153116742Ssam	 * Fill in media characteristics.
1154116742Ssam	 */
1155178354Ssam	ifmedia_init(media, 0, media_change, media_stat);
1156116742Ssam	maxrate = 0;
1157170530Ssam	/*
1158170530Ssam	 * Add media for legacy operating modes.
1159170530Ssam	 */
1160116742Ssam	memset(&allrates, 0, sizeof(allrates));
1161170530Ssam	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1162167468Ssam		if (isclr(ic->ic_modecaps, mode))
1163116742Ssam			continue;
1164178354Ssam		addmedia(media, caps, addsta, mode, IFM_AUTO);
1165116742Ssam		if (mode == IEEE80211_MODE_AUTO)
1166116742Ssam			continue;
1167116742Ssam		rs = &ic->ic_sup_rates[mode];
1168116742Ssam		for (i = 0; i < rs->rs_nrates; i++) {
1169116742Ssam			rate = rs->rs_rates[i];
1170116742Ssam			mword = ieee80211_rate2media(ic, rate, mode);
1171116742Ssam			if (mword == 0)
1172116742Ssam				continue;
1173178354Ssam			addmedia(media, caps, addsta, mode, mword);
1174116742Ssam			/*
1175170530Ssam			 * Add legacy rate to the collection of all rates.
1176116742Ssam			 */
1177116742Ssam			r = rate & IEEE80211_RATE_VAL;
1178116742Ssam			for (j = 0; j < allrates.rs_nrates; j++)
1179116742Ssam				if (allrates.rs_rates[j] == r)
1180116742Ssam					break;
1181116742Ssam			if (j == allrates.rs_nrates) {
1182116742Ssam				/* unique, add to the set */
1183116742Ssam				allrates.rs_rates[j] = r;
1184116742Ssam				allrates.rs_nrates++;
1185116742Ssam			}
1186116742Ssam			rate = (rate & IEEE80211_RATE_VAL) / 2;
1187116742Ssam			if (rate > maxrate)
1188116742Ssam				maxrate = rate;
1189116742Ssam		}
1190116742Ssam	}
1191116742Ssam	for (i = 0; i < allrates.rs_nrates; i++) {
1192116742Ssam		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1193116742Ssam				IEEE80211_MODE_AUTO);
1194116742Ssam		if (mword == 0)
1195116742Ssam			continue;
1196170530Ssam		/* NB: remove media options from mword */
1197178354Ssam		addmedia(media, caps, addsta,
1198178354Ssam		    IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1199116742Ssam	}
1200170530Ssam	/*
1201170530Ssam	 * Add HT/11n media.  Note that we do not have enough
1202170530Ssam	 * bits in the media subtype to express the MCS so we
1203170530Ssam	 * use a "placeholder" media subtype and any fixed MCS
1204170530Ssam	 * must be specified with a different mechanism.
1205170530Ssam	 */
1206188782Ssam	for (; mode <= IEEE80211_MODE_11NG; mode++) {
1207170530Ssam		if (isclr(ic->ic_modecaps, mode))
1208170530Ssam			continue;
1209178354Ssam		addmedia(media, caps, addsta, mode, IFM_AUTO);
1210178354Ssam		addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1211170530Ssam	}
1212170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1213170530Ssam	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1214178354Ssam		addmedia(media, caps, addsta,
1215178354Ssam		    IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1216219599Sbschmidt		i = ic->ic_txstream * 8 - 1;
1217219599Sbschmidt		if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1218219599Sbschmidt		    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1219219599Sbschmidt			rate = ieee80211_htrates[i].ht40_rate_400ns;
1220219599Sbschmidt		else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1221219599Sbschmidt			rate = ieee80211_htrates[i].ht40_rate_800ns;
1222219599Sbschmidt		else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1223219599Sbschmidt			rate = ieee80211_htrates[i].ht20_rate_400ns;
1224219599Sbschmidt		else
1225219599Sbschmidt			rate = ieee80211_htrates[i].ht20_rate_800ns;
1226219599Sbschmidt		if (rate > maxrate)
1227219599Sbschmidt			maxrate = rate;
1228170530Ssam	}
1229178354Ssam	return maxrate;
1230178354Ssam}
1231116742Ssam
1232188782Ssam/* XXX inline or eliminate? */
1233165569Ssamconst struct ieee80211_rateset *
1234165569Ssamieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1235165569Ssam{
1236188774Ssam	/* XXX does this work for 11ng basic rates? */
1237170530Ssam	return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1238165569Ssam}
1239165569Ssam
1240138568Ssamvoid
1241138568Ssamieee80211_announce(struct ieee80211com *ic)
1242138568Ssam{
1243228621Sbschmidt	int i, rate, mword;
1244228621Sbschmidt	enum ieee80211_phymode mode;
1245170530Ssam	const struct ieee80211_rateset *rs;
1246138568Ssam
1247172227Ssam	/* NB: skip AUTO since it has no rates */
1248172227Ssam	for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1249167468Ssam		if (isclr(ic->ic_modecaps, mode))
1250138568Ssam			continue;
1251283529Sglebius		ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
1252138568Ssam		rs = &ic->ic_sup_rates[mode];
1253138568Ssam		for (i = 0; i < rs->rs_nrates; i++) {
1254170530Ssam			mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1255138568Ssam			if (mword == 0)
1256138568Ssam				continue;
1257170530Ssam			rate = ieee80211_media2rate(mword);
1258138568Ssam			printf("%s%d%sMbps", (i != 0 ? " " : ""),
1259170530Ssam			    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1260138568Ssam		}
1261138568Ssam		printf("\n");
1262138568Ssam	}
1263170530Ssam	ieee80211_ht_announce(ic);
1264138568Ssam}
1265138568Ssam
1266170530Ssamvoid
1267170530Ssamieee80211_announce_channels(struct ieee80211com *ic)
1268116742Ssam{
1269170530Ssam	const struct ieee80211_channel *c;
1270170530Ssam	char type;
1271170530Ssam	int i, cw;
1272170530Ssam
1273170530Ssam	printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
1274170530Ssam	for (i = 0; i < ic->ic_nchans; i++) {
1275170530Ssam		c = &ic->ic_channels[i];
1276170530Ssam		if (IEEE80211_IS_CHAN_ST(c))
1277170530Ssam			type = 'S';
1278170530Ssam		else if (IEEE80211_IS_CHAN_108A(c))
1279170530Ssam			type = 'T';
1280170530Ssam		else if (IEEE80211_IS_CHAN_108G(c))
1281170530Ssam			type = 'G';
1282170530Ssam		else if (IEEE80211_IS_CHAN_HT(c))
1283170530Ssam			type = 'n';
1284170530Ssam		else if (IEEE80211_IS_CHAN_A(c))
1285170530Ssam			type = 'a';
1286170530Ssam		else if (IEEE80211_IS_CHAN_ANYG(c))
1287170530Ssam			type = 'g';
1288170530Ssam		else if (IEEE80211_IS_CHAN_B(c))
1289170530Ssam			type = 'b';
1290170530Ssam		else
1291170530Ssam			type = 'f';
1292170530Ssam		if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1293170530Ssam			cw = 40;
1294170530Ssam		else if (IEEE80211_IS_CHAN_HALF(c))
1295170530Ssam			cw = 10;
1296170530Ssam		else if (IEEE80211_IS_CHAN_QUARTER(c))
1297170530Ssam			cw = 5;
1298170530Ssam		else
1299170530Ssam			cw = 20;
1300170530Ssam		printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
1301170530Ssam			, c->ic_ieee, c->ic_freq, type
1302170530Ssam			, cw
1303170530Ssam			, IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1304170530Ssam			  IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1305170530Ssam			, c->ic_maxregpower
1306170530Ssam			, c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1307170530Ssam			, c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1308170530Ssam		);
1309170530Ssam	}
1310116742Ssam}
1311116742Ssam
1312170530Ssamstatic int
1313184273Ssammedia2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1314170530Ssam{
1315116742Ssam	switch (IFM_MODE(ime->ifm_media)) {
1316116742Ssam	case IFM_IEEE80211_11A:
1317178354Ssam		*mode = IEEE80211_MODE_11A;
1318116742Ssam		break;
1319116742Ssam	case IFM_IEEE80211_11B:
1320178354Ssam		*mode = IEEE80211_MODE_11B;
1321116742Ssam		break;
1322116742Ssam	case IFM_IEEE80211_11G:
1323178354Ssam		*mode = IEEE80211_MODE_11G;
1324116742Ssam		break;
1325124543Sonoe	case IFM_IEEE80211_FH:
1326178354Ssam		*mode = IEEE80211_MODE_FH;
1327124543Sonoe		break;
1328170530Ssam	case IFM_IEEE80211_11NA:
1329178354Ssam		*mode = IEEE80211_MODE_11NA;
1330170530Ssam		break;
1331170530Ssam	case IFM_IEEE80211_11NG:
1332178354Ssam		*mode = IEEE80211_MODE_11NG;
1333170530Ssam		break;
1334116742Ssam	case IFM_AUTO:
1335178354Ssam		*mode = IEEE80211_MODE_AUTO;
1336116742Ssam		break;
1337116742Ssam	default:
1338178354Ssam		return 0;
1339116742Ssam	}
1340116742Ssam	/*
1341138568Ssam	 * Turbo mode is an ``option''.
1342138568Ssam	 * XXX does not apply to AUTO
1343116742Ssam	 */
1344116742Ssam	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1345178354Ssam		if (*mode == IEEE80211_MODE_11A) {
1346184273Ssam			if (flags & IEEE80211_F_TURBOP)
1347178354Ssam				*mode = IEEE80211_MODE_TURBO_A;
1348170530Ssam			else
1349178354Ssam				*mode = IEEE80211_MODE_STURBO_A;
1350178354Ssam		} else if (*mode == IEEE80211_MODE_11G)
1351178354Ssam			*mode = IEEE80211_MODE_TURBO_G;
1352138568Ssam		else
1353178354Ssam			return 0;
1354116742Ssam	}
1355170530Ssam	/* XXX HT40 +/- */
1356178354Ssam	return 1;
1357178354Ssam}
1358116742Ssam
1359178354Ssam/*
1360178354Ssam * Handle a media change request on the vap interface.
1361178354Ssam */
1362178354Ssamint
1363178354Ssamieee80211_media_change(struct ifnet *ifp)
1364178354Ssam{
1365178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
1366178354Ssam	struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1367184273Ssam	uint16_t newmode;
1368178354Ssam
1369184273Ssam	if (!media2mode(ime, vap->iv_flags, &newmode))
1370178354Ssam		return EINVAL;
1371184273Ssam	if (vap->iv_des_mode != newmode) {
1372184273Ssam		vap->iv_des_mode = newmode;
1373193340Ssam		/* XXX kick state machine if up+running */
1374178354Ssam	}
1375178354Ssam	return 0;
1376116742Ssam}
1377116742Ssam
1378170530Ssam/*
1379170530Ssam * Common code to calculate the media status word
1380170530Ssam * from the operating mode and channel state.
1381170530Ssam */
1382170530Ssamstatic int
1383170530Ssammedia_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1384170530Ssam{
1385170530Ssam	int status;
1386170530Ssam
1387170530Ssam	status = IFM_IEEE80211;
1388170530Ssam	switch (opmode) {
1389170530Ssam	case IEEE80211_M_STA:
1390170530Ssam		break;
1391170530Ssam	case IEEE80211_M_IBSS:
1392170530Ssam		status |= IFM_IEEE80211_ADHOC;
1393170530Ssam		break;
1394170530Ssam	case IEEE80211_M_HOSTAP:
1395170530Ssam		status |= IFM_IEEE80211_HOSTAP;
1396170530Ssam		break;
1397170530Ssam	case IEEE80211_M_MONITOR:
1398170530Ssam		status |= IFM_IEEE80211_MONITOR;
1399170530Ssam		break;
1400170530Ssam	case IEEE80211_M_AHDEMO:
1401170530Ssam		status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1402170530Ssam		break;
1403170530Ssam	case IEEE80211_M_WDS:
1404178354Ssam		status |= IFM_IEEE80211_WDS;
1405170530Ssam		break;
1406195618Srpaulo	case IEEE80211_M_MBSS:
1407195618Srpaulo		status |= IFM_IEEE80211_MBSS;
1408195618Srpaulo		break;
1409170530Ssam	}
1410170530Ssam	if (IEEE80211_IS_CHAN_HTA(chan)) {
1411170530Ssam		status |= IFM_IEEE80211_11NA;
1412170530Ssam	} else if (IEEE80211_IS_CHAN_HTG(chan)) {
1413170530Ssam		status |= IFM_IEEE80211_11NG;
1414170530Ssam	} else if (IEEE80211_IS_CHAN_A(chan)) {
1415170530Ssam		status |= IFM_IEEE80211_11A;
1416170530Ssam	} else if (IEEE80211_IS_CHAN_B(chan)) {
1417170530Ssam		status |= IFM_IEEE80211_11B;
1418170530Ssam	} else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1419170530Ssam		status |= IFM_IEEE80211_11G;
1420170530Ssam	} else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1421170530Ssam		status |= IFM_IEEE80211_FH;
1422170530Ssam	}
1423170530Ssam	/* XXX else complain? */
1424170530Ssam
1425170530Ssam	if (IEEE80211_IS_CHAN_TURBO(chan))
1426170530Ssam		status |= IFM_IEEE80211_TURBO;
1427178354Ssam#if 0
1428178354Ssam	if (IEEE80211_IS_CHAN_HT20(chan))
1429178354Ssam		status |= IFM_IEEE80211_HT20;
1430178354Ssam	if (IEEE80211_IS_CHAN_HT40(chan))
1431178354Ssam		status |= IFM_IEEE80211_HT40;
1432178354Ssam#endif
1433170530Ssam	return status;
1434170530Ssam}
1435170530Ssam
1436116742Ssamvoid
1437116742Ssamieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1438116742Ssam{
1439178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
1440178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1441170530Ssam	enum ieee80211_phymode mode;
1442116742Ssam
1443116742Ssam	imr->ifm_status = IFM_AVALID;
1444170530Ssam	/*
1445170530Ssam	 * NB: use the current channel's mode to lock down a xmit
1446170530Ssam	 * rate only when running; otherwise we may have a mismatch
1447170530Ssam	 * in which case the rate will not be convertible.
1448170530Ssam	 */
1449264843Sadrian	if (vap->iv_state == IEEE80211_S_RUN ||
1450264843Sadrian	    vap->iv_state == IEEE80211_S_SLEEP) {
1451116742Ssam		imr->ifm_status |= IFM_ACTIVE;
1452170530Ssam		mode = ieee80211_chan2mode(ic->ic_curchan);
1453170530Ssam	} else
1454170530Ssam		mode = IEEE80211_MODE_AUTO;
1455178354Ssam	imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1456138568Ssam	/*
1457138568Ssam	 * Calculate a current rate if possible.
1458138568Ssam	 */
1459178354Ssam	if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1460138568Ssam		/*
1461138568Ssam		 * A fixed rate is set, report that.
1462138568Ssam		 */
1463138568Ssam		imr->ifm_active |= ieee80211_rate2media(ic,
1464178354Ssam			vap->iv_txparms[mode].ucastrate, mode);
1465178354Ssam	} else if (vap->iv_opmode == IEEE80211_M_STA) {
1466138568Ssam		/*
1467138568Ssam		 * In station mode report the current transmit rate.
1468138568Ssam		 */
1469138568Ssam		imr->ifm_active |= ieee80211_rate2media(ic,
1470178354Ssam			vap->iv_bss->ni_txrate, mode);
1471128966Sandre	} else
1472138568Ssam		imr->ifm_active |= IFM_AUTO;
1473178354Ssam	if (imr->ifm_status & IFM_ACTIVE)
1474178354Ssam		imr->ifm_current = imr->ifm_active;
1475116742Ssam}
1476116742Ssam
1477116742Ssam/*
1478116742Ssam * Set the current phy mode and recalculate the active channel
1479116742Ssam * set based on the available channels for this mode.  Also
1480116742Ssam * select a new default/current channel if the current one is
1481116742Ssam * inappropriate for this mode.
1482116742Ssam */
1483116742Ssamint
1484116742Ssamieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1485116742Ssam{
1486116742Ssam	/*
1487166012Ssam	 * Adjust basic rates in 11b/11g supported rate set.
1488166012Ssam	 * Note that if operating on a hal/quarter rate channel
1489166012Ssam	 * this is a noop as those rates sets are different
1490166012Ssam	 * and used instead.
1491116742Ssam	 */
1492166012Ssam	if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1493178354Ssam		ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1494166012Ssam
1495116742Ssam	ic->ic_curmode = mode;
1496138568Ssam	ieee80211_reset_erp(ic);	/* reset ERP state */
1497138568Ssam
1498116742Ssam	return 0;
1499116742Ssam}
1500116742Ssam
1501116742Ssam/*
1502170530Ssam * Return the phy mode for with the specified channel.
1503116742Ssam */
1504116742Ssamenum ieee80211_phymode
1505170530Ssamieee80211_chan2mode(const struct ieee80211_channel *chan)
1506116742Ssam{
1507170530Ssam
1508170530Ssam	if (IEEE80211_IS_CHAN_HTA(chan))
1509170530Ssam		return IEEE80211_MODE_11NA;
1510170530Ssam	else if (IEEE80211_IS_CHAN_HTG(chan))
1511170530Ssam		return IEEE80211_MODE_11NG;
1512170530Ssam	else if (IEEE80211_IS_CHAN_108G(chan))
1513170530Ssam		return IEEE80211_MODE_TURBO_G;
1514170530Ssam	else if (IEEE80211_IS_CHAN_ST(chan))
1515170530Ssam		return IEEE80211_MODE_STURBO_A;
1516170530Ssam	else if (IEEE80211_IS_CHAN_TURBO(chan))
1517153350Ssam		return IEEE80211_MODE_TURBO_A;
1518188782Ssam	else if (IEEE80211_IS_CHAN_HALF(chan))
1519188782Ssam		return IEEE80211_MODE_HALF;
1520188782Ssam	else if (IEEE80211_IS_CHAN_QUARTER(chan))
1521188782Ssam		return IEEE80211_MODE_QUARTER;
1522170530Ssam	else if (IEEE80211_IS_CHAN_A(chan))
1523116742Ssam		return IEEE80211_MODE_11A;
1524170530Ssam	else if (IEEE80211_IS_CHAN_ANYG(chan))
1525116742Ssam		return IEEE80211_MODE_11G;
1526170530Ssam	else if (IEEE80211_IS_CHAN_B(chan))
1527116742Ssam		return IEEE80211_MODE_11B;
1528170530Ssam	else if (IEEE80211_IS_CHAN_FHSS(chan))
1529170530Ssam		return IEEE80211_MODE_FH;
1530170530Ssam
1531170530Ssam	/* NB: should not get here */
1532170530Ssam	printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1533170530Ssam		__func__, chan->ic_freq, chan->ic_flags);
1534170530Ssam	return IEEE80211_MODE_11B;
1535116742Ssam}
1536116742Ssam
1537170530Ssamstruct ratemedia {
1538170530Ssam	u_int	match;	/* rate + mode */
1539170530Ssam	u_int	media;	/* if_media rate */
1540170530Ssam};
1541170530Ssam
1542170530Ssamstatic int
1543170530Ssamfindmedia(const struct ratemedia rates[], int n, u_int match)
1544170530Ssam{
1545170530Ssam	int i;
1546170530Ssam
1547170530Ssam	for (i = 0; i < n; i++)
1548170530Ssam		if (rates[i].match == match)
1549170530Ssam			return rates[i].media;
1550170530Ssam	return IFM_AUTO;
1551170530Ssam}
1552170530Ssam
1553116742Ssam/*
1554170530Ssam * Convert IEEE80211 rate value to ifmedia subtype.
1555170530Ssam * Rate is either a legacy rate in units of 0.5Mbps
1556170530Ssam * or an MCS index.
1557116742Ssam */
1558116742Ssamint
1559116742Ssamieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1560116742Ssam{
1561170530Ssam	static const struct ratemedia rates[] = {
1562124543Sonoe		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1563124543Sonoe		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1564124543Sonoe		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1565124543Sonoe		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1566124543Sonoe		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1567124543Sonoe		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1568124543Sonoe		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1569124543Sonoe		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1570124543Sonoe		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1571124543Sonoe		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1572124543Sonoe		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1573124543Sonoe		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1574124543Sonoe		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1575124543Sonoe		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1576124543Sonoe		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1577124543Sonoe		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1578124543Sonoe		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1579124543Sonoe		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1580124543Sonoe		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1581124543Sonoe		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1582124543Sonoe		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1583124543Sonoe		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1584124543Sonoe		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1585124543Sonoe		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1586124543Sonoe		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1587124543Sonoe		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1588124543Sonoe		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1589165569Ssam		{   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1590165569Ssam		{   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1591165569Ssam		{  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1592116742Ssam		/* NB: OFDM72 doesn't realy exist so we don't handle it */
1593116742Ssam	};
1594170530Ssam	static const struct ratemedia htrates[] = {
1595170530Ssam		{   0, IFM_IEEE80211_MCS },
1596170530Ssam		{   1, IFM_IEEE80211_MCS },
1597170530Ssam		{   2, IFM_IEEE80211_MCS },
1598170530Ssam		{   3, IFM_IEEE80211_MCS },
1599170530Ssam		{   4, IFM_IEEE80211_MCS },
1600170530Ssam		{   5, IFM_IEEE80211_MCS },
1601170530Ssam		{   6, IFM_IEEE80211_MCS },
1602170530Ssam		{   7, IFM_IEEE80211_MCS },
1603170530Ssam		{   8, IFM_IEEE80211_MCS },
1604170530Ssam		{   9, IFM_IEEE80211_MCS },
1605170530Ssam		{  10, IFM_IEEE80211_MCS },
1606170530Ssam		{  11, IFM_IEEE80211_MCS },
1607170530Ssam		{  12, IFM_IEEE80211_MCS },
1608170530Ssam		{  13, IFM_IEEE80211_MCS },
1609170530Ssam		{  14, IFM_IEEE80211_MCS },
1610170530Ssam		{  15, IFM_IEEE80211_MCS },
1611219456Sbschmidt		{  16, IFM_IEEE80211_MCS },
1612219456Sbschmidt		{  17, IFM_IEEE80211_MCS },
1613219456Sbschmidt		{  18, IFM_IEEE80211_MCS },
1614219456Sbschmidt		{  19, IFM_IEEE80211_MCS },
1615219456Sbschmidt		{  20, IFM_IEEE80211_MCS },
1616219456Sbschmidt		{  21, IFM_IEEE80211_MCS },
1617219456Sbschmidt		{  22, IFM_IEEE80211_MCS },
1618219456Sbschmidt		{  23, IFM_IEEE80211_MCS },
1619219456Sbschmidt		{  24, IFM_IEEE80211_MCS },
1620219456Sbschmidt		{  25, IFM_IEEE80211_MCS },
1621219456Sbschmidt		{  26, IFM_IEEE80211_MCS },
1622219456Sbschmidt		{  27, IFM_IEEE80211_MCS },
1623219456Sbschmidt		{  28, IFM_IEEE80211_MCS },
1624219456Sbschmidt		{  29, IFM_IEEE80211_MCS },
1625219456Sbschmidt		{  30, IFM_IEEE80211_MCS },
1626219456Sbschmidt		{  31, IFM_IEEE80211_MCS },
1627219456Sbschmidt		{  32, IFM_IEEE80211_MCS },
1628219456Sbschmidt		{  33, IFM_IEEE80211_MCS },
1629219456Sbschmidt		{  34, IFM_IEEE80211_MCS },
1630219456Sbschmidt		{  35, IFM_IEEE80211_MCS },
1631219456Sbschmidt		{  36, IFM_IEEE80211_MCS },
1632219456Sbschmidt		{  37, IFM_IEEE80211_MCS },
1633219456Sbschmidt		{  38, IFM_IEEE80211_MCS },
1634219456Sbschmidt		{  39, IFM_IEEE80211_MCS },
1635219456Sbschmidt		{  40, IFM_IEEE80211_MCS },
1636219456Sbschmidt		{  41, IFM_IEEE80211_MCS },
1637219456Sbschmidt		{  42, IFM_IEEE80211_MCS },
1638219456Sbschmidt		{  43, IFM_IEEE80211_MCS },
1639219456Sbschmidt		{  44, IFM_IEEE80211_MCS },
1640219456Sbschmidt		{  45, IFM_IEEE80211_MCS },
1641219456Sbschmidt		{  46, IFM_IEEE80211_MCS },
1642219456Sbschmidt		{  47, IFM_IEEE80211_MCS },
1643219456Sbschmidt		{  48, IFM_IEEE80211_MCS },
1644219456Sbschmidt		{  49, IFM_IEEE80211_MCS },
1645219456Sbschmidt		{  50, IFM_IEEE80211_MCS },
1646219456Sbschmidt		{  51, IFM_IEEE80211_MCS },
1647219456Sbschmidt		{  52, IFM_IEEE80211_MCS },
1648219456Sbschmidt		{  53, IFM_IEEE80211_MCS },
1649219456Sbschmidt		{  54, IFM_IEEE80211_MCS },
1650219456Sbschmidt		{  55, IFM_IEEE80211_MCS },
1651219456Sbschmidt		{  56, IFM_IEEE80211_MCS },
1652219456Sbschmidt		{  57, IFM_IEEE80211_MCS },
1653219456Sbschmidt		{  58, IFM_IEEE80211_MCS },
1654219456Sbschmidt		{  59, IFM_IEEE80211_MCS },
1655219456Sbschmidt		{  60, IFM_IEEE80211_MCS },
1656219456Sbschmidt		{  61, IFM_IEEE80211_MCS },
1657219456Sbschmidt		{  62, IFM_IEEE80211_MCS },
1658219456Sbschmidt		{  63, IFM_IEEE80211_MCS },
1659219456Sbschmidt		{  64, IFM_IEEE80211_MCS },
1660219456Sbschmidt		{  65, IFM_IEEE80211_MCS },
1661219456Sbschmidt		{  66, IFM_IEEE80211_MCS },
1662219456Sbschmidt		{  67, IFM_IEEE80211_MCS },
1663219456Sbschmidt		{  68, IFM_IEEE80211_MCS },
1664219456Sbschmidt		{  69, IFM_IEEE80211_MCS },
1665219456Sbschmidt		{  70, IFM_IEEE80211_MCS },
1666219456Sbschmidt		{  71, IFM_IEEE80211_MCS },
1667219456Sbschmidt		{  72, IFM_IEEE80211_MCS },
1668219456Sbschmidt		{  73, IFM_IEEE80211_MCS },
1669219456Sbschmidt		{  74, IFM_IEEE80211_MCS },
1670219456Sbschmidt		{  75, IFM_IEEE80211_MCS },
1671219456Sbschmidt		{  76, IFM_IEEE80211_MCS },
1672170530Ssam	};
1673170530Ssam	int m;
1674116742Ssam
1675170530Ssam	/*
1676170530Ssam	 * Check 11n rates first for match as an MCS.
1677170530Ssam	 */
1678170530Ssam	if (mode == IEEE80211_MODE_11NA) {
1679172226Ssam		if (rate & IEEE80211_RATE_MCS) {
1680172226Ssam			rate &= ~IEEE80211_RATE_MCS;
1681254315Srpaulo			m = findmedia(htrates, nitems(htrates), rate);
1682170530Ssam			if (m != IFM_AUTO)
1683170530Ssam				return m | IFM_IEEE80211_11NA;
1684170530Ssam		}
1685170530Ssam	} else if (mode == IEEE80211_MODE_11NG) {
1686170530Ssam		/* NB: 12 is ambiguous, it will be treated as an MCS */
1687172226Ssam		if (rate & IEEE80211_RATE_MCS) {
1688172226Ssam			rate &= ~IEEE80211_RATE_MCS;
1689254315Srpaulo			m = findmedia(htrates, nitems(htrates), rate);
1690170530Ssam			if (m != IFM_AUTO)
1691170530Ssam				return m | IFM_IEEE80211_11NG;
1692170530Ssam		}
1693170530Ssam	}
1694170530Ssam	rate &= IEEE80211_RATE_VAL;
1695116742Ssam	switch (mode) {
1696116742Ssam	case IEEE80211_MODE_11A:
1697188782Ssam	case IEEE80211_MODE_HALF:		/* XXX good 'nuf */
1698188782Ssam	case IEEE80211_MODE_QUARTER:
1699170530Ssam	case IEEE80211_MODE_11NA:
1700138568Ssam	case IEEE80211_MODE_TURBO_A:
1701170530Ssam	case IEEE80211_MODE_STURBO_A:
1702254315Srpaulo		return findmedia(rates, nitems(rates),
1703254315Srpaulo		    rate | IFM_IEEE80211_11A);
1704116742Ssam	case IEEE80211_MODE_11B:
1705254315Srpaulo		return findmedia(rates, nitems(rates),
1706254315Srpaulo		    rate | IFM_IEEE80211_11B);
1707124543Sonoe	case IEEE80211_MODE_FH:
1708254315Srpaulo		return findmedia(rates, nitems(rates),
1709254315Srpaulo		    rate | IFM_IEEE80211_FH);
1710116742Ssam	case IEEE80211_MODE_AUTO:
1711116742Ssam		/* NB: ic may be NULL for some drivers */
1712188775Ssam		if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
1713254315Srpaulo			return findmedia(rates, nitems(rates),
1714170530Ssam			    rate | IFM_IEEE80211_FH);
1715116742Ssam		/* NB: hack, 11g matches both 11b+11a rates */
1716116742Ssam		/* fall thru... */
1717116742Ssam	case IEEE80211_MODE_11G:
1718170530Ssam	case IEEE80211_MODE_11NG:
1719138568Ssam	case IEEE80211_MODE_TURBO_G:
1720254315Srpaulo		return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
1721116742Ssam	}
1722116742Ssam	return IFM_AUTO;
1723116742Ssam}
1724116742Ssam
1725116742Ssamint
1726116742Ssamieee80211_media2rate(int mword)
1727116742Ssam{
1728116742Ssam	static const int ieeerates[] = {
1729116742Ssam		-1,		/* IFM_AUTO */
1730116742Ssam		0,		/* IFM_MANUAL */
1731116742Ssam		0,		/* IFM_NONE */
1732116742Ssam		2,		/* IFM_IEEE80211_FH1 */
1733116742Ssam		4,		/* IFM_IEEE80211_FH2 */
1734116742Ssam		2,		/* IFM_IEEE80211_DS1 */
1735116742Ssam		4,		/* IFM_IEEE80211_DS2 */
1736116742Ssam		11,		/* IFM_IEEE80211_DS5 */
1737116742Ssam		22,		/* IFM_IEEE80211_DS11 */
1738116742Ssam		44,		/* IFM_IEEE80211_DS22 */
1739116742Ssam		12,		/* IFM_IEEE80211_OFDM6 */
1740116742Ssam		18,		/* IFM_IEEE80211_OFDM9 */
1741116742Ssam		24,		/* IFM_IEEE80211_OFDM12 */
1742116742Ssam		36,		/* IFM_IEEE80211_OFDM18 */
1743116742Ssam		48,		/* IFM_IEEE80211_OFDM24 */
1744116742Ssam		72,		/* IFM_IEEE80211_OFDM36 */
1745116742Ssam		96,		/* IFM_IEEE80211_OFDM48 */
1746116742Ssam		108,		/* IFM_IEEE80211_OFDM54 */
1747116742Ssam		144,		/* IFM_IEEE80211_OFDM72 */
1748165569Ssam		0,		/* IFM_IEEE80211_DS354k */
1749165569Ssam		0,		/* IFM_IEEE80211_DS512k */
1750165569Ssam		6,		/* IFM_IEEE80211_OFDM3 */
1751165569Ssam		9,		/* IFM_IEEE80211_OFDM4 */
1752165569Ssam		54,		/* IFM_IEEE80211_OFDM27 */
1753170530Ssam		-1,		/* IFM_IEEE80211_MCS */
1754116742Ssam	};
1755254315Srpaulo	return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
1756116742Ssam		ieeerates[IFM_SUBTYPE(mword)] : 0;
1757116742Ssam}
1758195379Ssam
1759195379Ssam/*
1760195379Ssam * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1761195379Ssam * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1762195379Ssam */
1763195379Ssam#define	mix(a, b, c)							\
1764195379Ssamdo {									\
1765195379Ssam	a -= b; a -= c; a ^= (c >> 13);					\
1766195379Ssam	b -= c; b -= a; b ^= (a << 8);					\
1767195379Ssam	c -= a; c -= b; c ^= (b >> 13);					\
1768195379Ssam	a -= b; a -= c; a ^= (c >> 12);					\
1769195379Ssam	b -= c; b -= a; b ^= (a << 16);					\
1770195379Ssam	c -= a; c -= b; c ^= (b >> 5);					\
1771195379Ssam	a -= b; a -= c; a ^= (c >> 3);					\
1772195379Ssam	b -= c; b -= a; b ^= (a << 10);					\
1773195379Ssam	c -= a; c -= b; c ^= (b >> 15);					\
1774195379Ssam} while (/*CONSTCOND*/0)
1775195379Ssam
1776195379Ssamuint32_t
1777195379Ssamieee80211_mac_hash(const struct ieee80211com *ic,
1778195379Ssam	const uint8_t addr[IEEE80211_ADDR_LEN])
1779195379Ssam{
1780195379Ssam	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
1781195379Ssam
1782195379Ssam	b += addr[5] << 8;
1783195379Ssam	b += addr[4];
1784195379Ssam	a += addr[3] << 24;
1785195379Ssam	a += addr[2] << 16;
1786195379Ssam	a += addr[1] << 8;
1787195379Ssam	a += addr[0];
1788195379Ssam
1789195379Ssam	mix(a, b, c);
1790195379Ssam
1791195379Ssam	return c;
1792195379Ssam}
1793195379Ssam#undef mix
1794276757Sadrian
1795276757Sadrianchar
1796276757Sadrianieee80211_channel_type_char(const struct ieee80211_channel *c)
1797276757Sadrian{
1798276757Sadrian	if (IEEE80211_IS_CHAN_ST(c))
1799276757Sadrian		return 'S';
1800276757Sadrian	if (IEEE80211_IS_CHAN_108A(c))
1801276757Sadrian		return 'T';
1802276757Sadrian	if (IEEE80211_IS_CHAN_108G(c))
1803276757Sadrian		return 'G';
1804276757Sadrian	if (IEEE80211_IS_CHAN_HT(c))
1805276757Sadrian		return 'n';
1806276757Sadrian	if (IEEE80211_IS_CHAN_A(c))
1807276757Sadrian		return 'a';
1808276757Sadrian	if (IEEE80211_IS_CHAN_ANYG(c))
1809276757Sadrian		return 'g';
1810276757Sadrian	if (IEEE80211_IS_CHAN_B(c))
1811276757Sadrian		return 'b';
1812276757Sadrian	return 'f';
1813276757Sadrian}
1814