ieee80211.c revision 188774
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 188774 2009-02-19 04:33:36Z sam $");
29116742Ssam
30116742Ssam/*
31116742Ssam * IEEE 802.11 generic handler
32116742Ssam */
33178354Ssam#include "opt_wlan.h"
34116742Ssam
35116742Ssam#include <sys/param.h>
36116742Ssam#include <sys/systm.h>
37116742Ssam#include <sys/kernel.h>
38182742Sbrooks
39116742Ssam#include <sys/socket.h>
40116742Ssam
41116742Ssam#include <net/if.h>
42178354Ssam#include <net/if_dl.h>
43116742Ssam#include <net/if_media.h>
44178354Ssam#include <net/if_types.h>
45116742Ssam#include <net/ethernet.h>
46116742Ssam
47116742Ssam#include <net80211/ieee80211_var.h>
48178354Ssam#include <net80211/ieee80211_regdomain.h>
49116742Ssam
50116742Ssam#include <net/bpf.h>
51116742Ssam
52178955Ssamconst char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
53178955Ssam	[IEEE80211_MODE_AUTO]	  = "auto",
54178955Ssam	[IEEE80211_MODE_11A]	  = "11a",
55178955Ssam	[IEEE80211_MODE_11B]	  = "11b",
56178955Ssam	[IEEE80211_MODE_11G]	  = "11g",
57178955Ssam	[IEEE80211_MODE_FH]	  = "FH",
58178955Ssam	[IEEE80211_MODE_TURBO_A]  = "turboA",
59178955Ssam	[IEEE80211_MODE_TURBO_G]  = "turboG",
60178955Ssam	[IEEE80211_MODE_STURBO_A] = "sturboA",
61178955Ssam	[IEEE80211_MODE_11NA]	  = "11na",
62178955Ssam	[IEEE80211_MODE_11NG]	  = "11ng",
63116742Ssam};
64178957Ssam/* map ieee80211_opmode to the corresponding capability bit */
65178957Ssamconst int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
66178957Ssam	[IEEE80211_M_IBSS]	= IEEE80211_C_IBSS,
67178957Ssam	[IEEE80211_M_WDS]	= IEEE80211_C_WDS,
68178957Ssam	[IEEE80211_M_STA]	= IEEE80211_C_STA,
69178957Ssam	[IEEE80211_M_AHDEMO]	= IEEE80211_C_AHDEMO,
70178957Ssam	[IEEE80211_M_HOSTAP]	= IEEE80211_C_HOSTAP,
71178957Ssam	[IEEE80211_M_MONITOR]	= IEEE80211_C_MONITOR,
72178957Ssam};
73178957Ssam
74178354Ssamstatic const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
75178354Ssam	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
76116742Ssam
77178354Ssamstatic	void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
78178354Ssamstatic	void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
79178354Ssamstatic	int ieee80211_media_setup(struct ieee80211com *ic,
80178354Ssam		struct ifmedia *media, int caps, int addsta,
81178354Ssam		ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
82178354Ssamstatic	void ieee80211com_media_status(struct ifnet *, struct ifmediareq *);
83178354Ssamstatic	int ieee80211com_media_change(struct ifnet *);
84178354Ssamstatic	int media_status(enum ieee80211_opmode,
85178354Ssam		const struct ieee80211_channel *);
86178354Ssam
87178354SsamMALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
88178354Ssam
89164645Ssam/*
90164645Ssam * Default supported rates for 802.11 operation (in IEEE .5Mb units).
91164645Ssam */
92164645Ssam#define	B(r)	((r) | IEEE80211_RATE_BASIC)
93164645Ssamstatic const struct ieee80211_rateset ieee80211_rateset_11a =
94164645Ssam	{ 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
95165569Ssamstatic const struct ieee80211_rateset ieee80211_rateset_half =
96165569Ssam	{ 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
97165569Ssamstatic const struct ieee80211_rateset ieee80211_rateset_quarter =
98165569Ssam	{ 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
99164645Ssamstatic const struct ieee80211_rateset ieee80211_rateset_11b =
100164645Ssam	{ 4, { B(2), B(4), B(11), B(22) } };
101164645Ssam/* NB: OFDM rates are handled specially based on mode */
102164645Ssamstatic const struct ieee80211_rateset ieee80211_rateset_11g =
103164645Ssam	{ 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
104164645Ssam#undef B
105164645Ssam
106140915Ssam/*
107165569Ssam * Fill in 802.11 available channel set, mark
108165569Ssam * all available channels as active, and pick
109165569Ssam * a default channel if not already specified.
110165569Ssam */
111165569Ssamstatic void
112165569Ssamieee80211_chan_init(struct ieee80211com *ic)
113116742Ssam{
114165569Ssam#define	DEFAULTRATES(m, def) do { \
115167468Ssam	if (isset(ic->ic_modecaps, m) && ic->ic_sup_rates[m].rs_nrates == 0) \
116165574Ssam		ic->ic_sup_rates[m] = def; \
117165569Ssam} while (0)
118116742Ssam	struct ieee80211_channel *c;
119116742Ssam	int i;
120116742Ssam
121186107Ssam	KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
122170530Ssam		("invalid number of channels specified: %u", ic->ic_nchans));
123116742Ssam	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
124178354Ssam	memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
125167468Ssam	setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
126170530Ssam	for (i = 0; i < ic->ic_nchans; i++) {
127116742Ssam		c = &ic->ic_channels[i];
128170530Ssam		KASSERT(c->ic_flags != 0, ("channel with no flags"));
129187796Ssam		/*
130187796Ssam		 * Help drivers that work only with frequencies by filling
131187796Ssam		 * in IEEE channel #'s if not already calculated.  Note this
132187796Ssam		 * mimics similar work done in ieee80211_setregdomain when
133187796Ssam		 * changing regulatory state.
134187796Ssam		 */
135187796Ssam		if (c->ic_ieee == 0)
136187796Ssam			c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
137187796Ssam		if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
138187796Ssam			c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
139187796Ssam			    (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
140187796Ssam			    c->ic_flags);
141187796Ssam		/* default max tx power to max regulatory */
142187796Ssam		if (c->ic_maxpower == 0)
143187796Ssam			c->ic_maxpower = 2*c->ic_maxregpower;
144170530Ssam		setbit(ic->ic_chan_avail, c->ic_ieee);
145170530Ssam		/*
146170530Ssam		 * Identify mode capabilities.
147170530Ssam		 */
148170530Ssam		if (IEEE80211_IS_CHAN_A(c))
149170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
150170530Ssam		if (IEEE80211_IS_CHAN_B(c))
151170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
152170530Ssam		if (IEEE80211_IS_CHAN_ANYG(c))
153170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
154170530Ssam		if (IEEE80211_IS_CHAN_FHSS(c))
155170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
156170530Ssam		if (IEEE80211_IS_CHAN_108A(c))
157170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
158170530Ssam		if (IEEE80211_IS_CHAN_108G(c))
159170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
160170530Ssam		if (IEEE80211_IS_CHAN_ST(c))
161170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
162170530Ssam		if (IEEE80211_IS_CHAN_HTA(c))
163170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
164170530Ssam		if (IEEE80211_IS_CHAN_HTG(c))
165170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
166116742Ssam	}
167170530Ssam	/* initialize candidate channels to all available */
168170530Ssam	memcpy(ic->ic_chan_active, ic->ic_chan_avail,
169170530Ssam		sizeof(ic->ic_chan_avail));
170164645Ssam
171178354Ssam	/* sort channel table to allow lookup optimizations */
172178354Ssam	ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
173178354Ssam
174178354Ssam	/* invalidate any previous state */
175170530Ssam	ic->ic_bsschan = IEEE80211_CHAN_ANYC;
176172233Ssam	ic->ic_prevchan = NULL;
177178354Ssam	ic->ic_csa_newchan = NULL;
178170530Ssam	/* arbitrarily pick the first channel */
179170530Ssam	ic->ic_curchan = &ic->ic_channels[0];
180170530Ssam
181164645Ssam	/* fillin well-known rate sets if driver has not specified */
182165569Ssam	DEFAULTRATES(IEEE80211_MODE_11B,	 ieee80211_rateset_11b);
183165569Ssam	DEFAULTRATES(IEEE80211_MODE_11G,	 ieee80211_rateset_11g);
184165569Ssam	DEFAULTRATES(IEEE80211_MODE_11A,	 ieee80211_rateset_11a);
185165569Ssam	DEFAULTRATES(IEEE80211_MODE_TURBO_A,	 ieee80211_rateset_11a);
186165569Ssam	DEFAULTRATES(IEEE80211_MODE_TURBO_G,	 ieee80211_rateset_11g);
187187897Ssam	DEFAULTRATES(IEEE80211_MODE_STURBO_A,	 ieee80211_rateset_11a);
188188774Ssam	DEFAULTRATES(IEEE80211_MODE_11NA,	 ieee80211_rateset_11a);
189188774Ssam	DEFAULTRATES(IEEE80211_MODE_11NG,	 ieee80211_rateset_11g);
190165569Ssam
191165569Ssam	/*
192165569Ssam	 * Set auto mode to reset active channel state and any desired channel.
193165569Ssam	 */
194165569Ssam	(void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
195165569Ssam#undef DEFAULTRATES
196165569Ssam}
197165569Ssam
198178354Ssamstatic void
199178354Ssamnull_update_mcast(struct ifnet *ifp)
200178354Ssam{
201178354Ssam	if_printf(ifp, "need multicast update callback\n");
202178354Ssam}
203178354Ssam
204178354Ssamstatic void
205178354Ssamnull_update_promisc(struct ifnet *ifp)
206178354Ssam{
207178354Ssam	if_printf(ifp, "need promiscuous mode update callback\n");
208178354Ssam}
209178354Ssam
210178521Ssamstatic int
211178521Ssamnull_output(struct ifnet *ifp, struct mbuf *m,
212178521Ssam	struct sockaddr *dst, struct rtentry *rt0)
213178521Ssam{
214178521Ssam	if_printf(ifp, "discard raw packet\n");
215178521Ssam	m_freem(m);
216178521Ssam	return EIO;
217178521Ssam}
218178521Ssam
219178521Ssamstatic void
220178521Ssamnull_input(struct ifnet *ifp, struct mbuf *m)
221178521Ssam{
222178521Ssam	if_printf(ifp, "if_input should not be called\n");
223178521Ssam	m_freem(m);
224178521Ssam}
225178521Ssam
226178354Ssam/*
227178354Ssam * Attach/setup the common net80211 state.  Called by
228178354Ssam * the driver on attach to prior to creating any vap's.
229178354Ssam */
230165569Ssamvoid
231165569Ssamieee80211_ifattach(struct ieee80211com *ic)
232165569Ssam{
233165569Ssam	struct ifnet *ifp = ic->ic_ifp;
234178354Ssam	struct sockaddr_dl *sdl;
235178354Ssam	struct ifaddr *ifa;
236165569Ssam
237178354Ssam	KASSERT(ifp->if_type == IFT_IEEE80211, ("if_type %d", ifp->if_type));
238165569Ssam
239179388Ssam	IEEE80211_LOCK_INIT(ic, ifp->if_xname);
240178354Ssam	TAILQ_INIT(&ic->ic_vaps);
241165569Ssam	/*
242165569Ssam	 * Fill in 802.11 available channel set, mark all
243165569Ssam	 * available channels as active, and pick a default
244165569Ssam	 * channel if not already specified.
245165569Ssam	 */
246178354Ssam	ieee80211_media_init(ic);
247170530Ssam
248178354Ssam	ic->ic_update_mcast = null_update_mcast;
249178354Ssam	ic->ic_update_promisc = null_update_promisc;
250116742Ssam
251155688Ssam	ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
252155688Ssam	ic->ic_lintval = ic->ic_bintval;
253138568Ssam	ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
254138568Ssam
255170530Ssam	ieee80211_crypto_attach(ic);
256138568Ssam	ieee80211_node_attach(ic);
257170530Ssam	ieee80211_power_attach(ic);
258138568Ssam	ieee80211_proto_attach(ic);
259170530Ssam	ieee80211_ht_attach(ic);
260170530Ssam	ieee80211_scan_attach(ic);
261178354Ssam	ieee80211_regdomain_attach(ic);
262138568Ssam
263178354Ssam	ieee80211_sysctl_attach(ic);
264138568Ssam
265178354Ssam	ifp->if_addrlen = IEEE80211_ADDR_LEN;
266178354Ssam	ifp->if_hdrlen = 0;
267178354Ssam	if_attach(ifp);
268178354Ssam	ifp->if_mtu = IEEE80211_MTU_MAX;
269178354Ssam	ifp->if_broadcastaddr = ieee80211broadcastaddr;
270178521Ssam	ifp->if_output = null_output;
271178521Ssam	ifp->if_input = null_input;	/* just in case */
272178521Ssam	ifp->if_resolvemulti = NULL;	/* NB: callers check */
273140915Ssam
274178354Ssam	ifa = ifaddr_byindex(ifp->if_index);
275178354Ssam	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
276178354Ssam	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
277178354Ssam	sdl->sdl_type = IFT_ETHER;		/* XXX IFT_IEEE80211? */
278178354Ssam	sdl->sdl_alen = IEEE80211_ADDR_LEN;
279178354Ssam	IEEE80211_ADDR_COPY(LLADDR(sdl), ic->ic_myaddr);
280116742Ssam}
281116742Ssam
282178354Ssam/*
283178354Ssam * Detach net80211 state on device detach.  Tear down
284178354Ssam * all vap's and reclaim all common state prior to the
285178354Ssam * device state going away.  Note we may call back into
286178354Ssam * driver; it must be prepared for this.
287178354Ssam */
288116742Ssamvoid
289138568Ssamieee80211_ifdetach(struct ieee80211com *ic)
290116742Ssam{
291138568Ssam	struct ifnet *ifp = ic->ic_ifp;
292178354Ssam	struct ieee80211vap *vap;
293116742Ssam
294178354Ssam	while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
295178354Ssam		ieee80211_vap_destroy(vap);
296188533Sthompsa	ieee80211_waitfor_parent(ic);
297138568Ssam
298138568Ssam	ieee80211_sysctl_detach(ic);
299178354Ssam	ieee80211_regdomain_detach(ic);
300170530Ssam	ieee80211_scan_detach(ic);
301170530Ssam	ieee80211_ht_detach(ic);
302166012Ssam	/* NB: must be called before ieee80211_node_detach */
303138568Ssam	ieee80211_proto_detach(ic);
304138568Ssam	ieee80211_crypto_detach(ic);
305170530Ssam	ieee80211_power_detach(ic);
306138568Ssam	ieee80211_node_detach(ic);
307116742Ssam	ifmedia_removeall(&ic->ic_media);
308138568Ssam
309170530Ssam	IEEE80211_LOCK_DESTROY(ic);
310178354Ssam	if_detach(ifp);
311178354Ssam}
312138568Ssam
313178354Ssam/*
314178354Ssam * Default reset method for use with the ioctl support.  This
315178354Ssam * method is invoked after any state change in the 802.11
316178354Ssam * layer that should be propagated to the hardware but not
317178354Ssam * require re-initialization of the 802.11 state machine (e.g
318178354Ssam * rescanning for an ap).  We always return ENETRESET which
319178354Ssam * should cause the driver to re-initialize the device. Drivers
320178354Ssam * can override this method to implement more optimized support.
321178354Ssam */
322178354Ssamstatic int
323178354Ssamdefault_reset(struct ieee80211vap *vap, u_long cmd)
324178354Ssam{
325178354Ssam	return ENETRESET;
326178354Ssam}
327178354Ssam
328178354Ssam/*
329178354Ssam * Prepare a vap for use.  Drivers use this call to
330178354Ssam * setup net80211 state in new vap's prior attaching
331178354Ssam * them with ieee80211_vap_attach (below).
332178354Ssam */
333178354Ssamint
334178354Ssamieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
335178354Ssam	const char name[IFNAMSIZ], int unit, int opmode, int flags,
336178354Ssam	const uint8_t bssid[IEEE80211_ADDR_LEN],
337178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
338178354Ssam{
339178354Ssam	struct ifnet *ifp;
340178354Ssam
341178354Ssam	ifp = if_alloc(IFT_ETHER);
342178354Ssam	if (ifp == NULL) {
343178354Ssam		if_printf(ic->ic_ifp, "%s: unable to allocate ifnet\n",
344178354Ssam		    __func__);
345178354Ssam		return ENOMEM;
346178354Ssam	}
347178354Ssam	if_initname(ifp, name, unit);
348178354Ssam	ifp->if_softc = vap;			/* back pointer */
349178354Ssam	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
350178354Ssam	ifp->if_start = ieee80211_start;
351178354Ssam	ifp->if_ioctl = ieee80211_ioctl;
352178354Ssam	ifp->if_watchdog = NULL;		/* NB: no watchdog routine */
353178354Ssam	ifp->if_init = ieee80211_init;
354178354Ssam	/* NB: input+output filled in by ether_ifattach */
355178354Ssam	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
356178354Ssam	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
357178354Ssam	IFQ_SET_READY(&ifp->if_snd);
358178354Ssam
359178354Ssam	vap->iv_ifp = ifp;
360178354Ssam	vap->iv_ic = ic;
361178354Ssam	vap->iv_flags = ic->ic_flags;		/* propagate common flags */
362178354Ssam	vap->iv_flags_ext = ic->ic_flags_ext;
363178354Ssam	vap->iv_flags_ven = ic->ic_flags_ven;
364178354Ssam	vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
365178354Ssam	vap->iv_htcaps = ic->ic_htcaps;
366178354Ssam	vap->iv_opmode = opmode;
367178957Ssam	vap->iv_caps |= ieee80211_opcap[opmode];
368178354Ssam	switch (opmode) {
369178354Ssam	case IEEE80211_M_WDS:
370178354Ssam		/*
371178354Ssam		 * WDS links must specify the bssid of the far end.
372178354Ssam		 * For legacy operation this is a static relationship.
373178354Ssam		 * For non-legacy operation the station must associate
374178354Ssam		 * and be authorized to pass traffic.  Plumbing the
375178354Ssam		 * vap to the proper node happens when the vap
376178354Ssam		 * transitions to RUN state.
377178354Ssam		 */
378178354Ssam		IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
379178354Ssam		vap->iv_flags |= IEEE80211_F_DESBSSID;
380178354Ssam		if (flags & IEEE80211_CLONE_WDSLEGACY)
381178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
382178354Ssam		break;
383186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
384186904Ssam	case IEEE80211_M_AHDEMO:
385186904Ssam		if (flags & IEEE80211_CLONE_TDMA) {
386186904Ssam			/* NB: checked before clone operation allowed */
387186904Ssam			KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
388186904Ssam			    ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
389186904Ssam			/*
390186904Ssam			 * Propagate TDMA capability to mark vap; this
391186904Ssam			 * cannot be removed and is used to distinguish
392186904Ssam			 * regular ahdemo operation from ahdemo+tdma.
393186904Ssam			 */
394186904Ssam			vap->iv_caps |= IEEE80211_C_TDMA;
395186904Ssam		}
396186904Ssam		break;
397186904Ssam#endif
398178354Ssam	}
399184278Ssam	/* auto-enable s/w beacon miss support */
400184278Ssam	if (flags & IEEE80211_CLONE_NOBEACONS)
401184278Ssam		vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
402178354Ssam	/*
403178354Ssam	 * Enable various functionality by default if we're
404178354Ssam	 * capable; the driver can override us if it knows better.
405178354Ssam	 */
406178354Ssam	if (vap->iv_caps & IEEE80211_C_WME)
407178354Ssam		vap->iv_flags |= IEEE80211_F_WME;
408178354Ssam	if (vap->iv_caps & IEEE80211_C_BURST)
409178354Ssam		vap->iv_flags |= IEEE80211_F_BURST;
410178354Ssam	if (vap->iv_caps & IEEE80211_C_FF)
411178354Ssam		vap->iv_flags |= IEEE80211_F_FF;
412178354Ssam	if (vap->iv_caps & IEEE80211_C_TURBOP)
413178354Ssam		vap->iv_flags |= IEEE80211_F_TURBOP;
414178354Ssam	/* NB: bg scanning only makes sense for station mode right now */
415178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA &&
416178354Ssam	    (vap->iv_caps & IEEE80211_C_BGSCAN))
417178354Ssam		vap->iv_flags |= IEEE80211_F_BGSCAN;
418178957Ssam	vap->iv_flags |= IEEE80211_F_DOTH;	/* XXX no cap, just ena */
419178954Ssam	/* NB: DFS support only makes sense for ap mode right now */
420178954Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
421178954Ssam	    (vap->iv_caps & IEEE80211_C_DFS))
422178354Ssam		vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
423178354Ssam
424178354Ssam	vap->iv_des_chan = IEEE80211_CHAN_ANYC;		/* any channel is ok */
425178354Ssam	vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
426178354Ssam	vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
427178354Ssam	/*
428178354Ssam	 * Install a default reset method for the ioctl support;
429178354Ssam	 * the driver can override this.
430178354Ssam	 */
431178354Ssam	vap->iv_reset = default_reset;
432178354Ssam
433178354Ssam	IEEE80211_ADDR_COPY(vap->iv_myaddr, macaddr);
434178354Ssam
435178354Ssam	ieee80211_sysctl_vattach(vap);
436178354Ssam	ieee80211_crypto_vattach(vap);
437178354Ssam	ieee80211_node_vattach(vap);
438178354Ssam	ieee80211_power_vattach(vap);
439178354Ssam	ieee80211_proto_vattach(vap);
440178354Ssam	ieee80211_ht_vattach(vap);
441178354Ssam	ieee80211_scan_vattach(vap);
442178354Ssam	ieee80211_regdomain_vattach(vap);
443178354Ssam
444178354Ssam	return 0;
445178354Ssam}
446178354Ssam
447178354Ssam/*
448178354Ssam * Activate a vap.  State should have been prepared with a
449178354Ssam * call to ieee80211_vap_setup and by the driver.  On return
450178354Ssam * from this call the vap is ready for use.
451178354Ssam */
452178354Ssamint
453178354Ssamieee80211_vap_attach(struct ieee80211vap *vap,
454178354Ssam	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
455178354Ssam{
456178354Ssam	struct ifnet *ifp = vap->iv_ifp;
457178354Ssam	struct ieee80211com *ic = vap->iv_ic;
458178354Ssam	struct ifmediareq imr;
459178354Ssam	int maxrate;
460178354Ssam
461178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
462178354Ssam	    "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
463178354Ssam	    __func__, ieee80211_opmode_name[vap->iv_opmode],
464178354Ssam	    ic->ic_ifp->if_xname, vap->iv_flags, vap->iv_flags_ext);
465178354Ssam
466178354Ssam	/*
467178354Ssam	 * Do late attach work that cannot happen until after
468178354Ssam	 * the driver has had a chance to override defaults.
469178354Ssam	 */
470178354Ssam	ieee80211_node_latevattach(vap);
471178354Ssam	ieee80211_power_latevattach(vap);
472178354Ssam
473178354Ssam	maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
474178354Ssam	    vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
475178354Ssam	ieee80211_media_status(ifp, &imr);
476178354Ssam	/* NB: strip explicit mode; we're actually in autoselect */
477188106Ssam	ifmedia_set(&vap->iv_media,
478188106Ssam	    imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
479178354Ssam	if (maxrate)
480178354Ssam		ifp->if_baudrate = IF_Mbps(maxrate);
481178354Ssam
482178354Ssam	ether_ifattach(ifp, vap->iv_myaddr);
483178354Ssam	/* hook output method setup by ether_ifattach */
484178354Ssam	vap->iv_output = ifp->if_output;
485178354Ssam	ifp->if_output = ieee80211_output;
486178354Ssam	/* NB: if_mtu set by ether_ifattach to ETHERMTU */
487178354Ssam	bpfattach2(ifp, DLT_IEEE802_11, ifp->if_hdrlen, &vap->iv_rawbpf);
488178354Ssam
489178354Ssam	IEEE80211_LOCK(ic);
490178354Ssam	TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
491178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
492178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
493178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
494178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
495178354Ssam	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_HT);
496178354Ssam	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_USEHT40);
497178354Ssam	ieee80211_syncifflag_locked(ic, IFF_PROMISC);
498178354Ssam	ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
499178354Ssam	IEEE80211_UNLOCK(ic);
500178354Ssam
501178354Ssam	return 1;
502178354Ssam}
503178354Ssam
504178354Ssam/*
505178354Ssam * Tear down vap state and reclaim the ifnet.
506178354Ssam * The driver is assumed to have prepared for
507178354Ssam * this; e.g. by turning off interrupts for the
508178354Ssam * underlying device.
509178354Ssam */
510178354Ssamvoid
511178354Ssamieee80211_vap_detach(struct ieee80211vap *vap)
512178354Ssam{
513178354Ssam	struct ieee80211com *ic = vap->iv_ic;
514178354Ssam	struct ifnet *ifp = vap->iv_ifp;
515178354Ssam
516178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
517178354Ssam	    __func__, ieee80211_opmode_name[vap->iv_opmode],
518178354Ssam	    ic->ic_ifp->if_xname);
519178354Ssam
520178354Ssam	IEEE80211_LOCK(ic);
521178354Ssam	/* block traffic from above */
522178354Ssam	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
523178354Ssam	/*
524178354Ssam	 * Evil hack.  Clear the backpointer from the ifnet to the
525178354Ssam	 * vap so any requests from above will return an error or
526178354Ssam	 * be ignored.  In particular this short-circuits requests
527178354Ssam	 * by the bridge to turn off promiscuous mode as a result
528178354Ssam	 * of calling ether_ifdetach.
529178354Ssam	 */
530178354Ssam	ifp->if_softc = NULL;
531178354Ssam	/*
532178354Ssam	 * Stop the vap before detaching the ifnet.  Ideally we'd
533178354Ssam	 * do this in the other order so the ifnet is inaccessible
534178354Ssam	 * while we cleanup internal state but that is hard.
535178354Ssam	 */
536178354Ssam	ieee80211_stop_locked(vap);
537178354Ssam
538178354Ssam	TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
539178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
540178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
541178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
542178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
543178354Ssam	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_HT);
544178354Ssam	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_USEHT40);
545178354Ssam	ieee80211_syncifflag_locked(ic, IFF_PROMISC);
546178354Ssam	ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
547178354Ssam	IEEE80211_UNLOCK(ic);
548178354Ssam
549178354Ssam	/* XXX can't hold com lock */
550178354Ssam	/* NB: bpfattach is called by ether_ifdetach and claims all taps */
551116742Ssam	ether_ifdetach(ifp);
552178354Ssam
553178354Ssam	ifmedia_removeall(&vap->iv_media);
554178354Ssam
555178354Ssam	ieee80211_regdomain_vdetach(vap);
556178354Ssam	ieee80211_scan_vdetach(vap);
557178354Ssam	ieee80211_ht_vdetach(vap);
558178354Ssam	/* NB: must be before ieee80211_node_vdetach */
559178354Ssam	ieee80211_proto_vdetach(vap);
560178354Ssam	ieee80211_crypto_vdetach(vap);
561178354Ssam	ieee80211_power_vdetach(vap);
562178354Ssam	ieee80211_node_vdetach(vap);
563178354Ssam	ieee80211_sysctl_vdetach(vap);
564182674Sweongyo
565182674Sweongyo	if_free(ifp);
566116742Ssam}
567116742Ssam
568178354Ssam/*
569178354Ssam * Synchronize flag bit state in the parent ifnet structure
570178354Ssam * according to the state of all vap ifnet's.  This is used,
571178354Ssam * for example, to handle IFF_PROMISC and IFF_ALLMULTI.
572178354Ssam */
573178354Ssamvoid
574178354Ssamieee80211_syncifflag_locked(struct ieee80211com *ic, int flag)
575178354Ssam{
576178354Ssam	struct ifnet *ifp = ic->ic_ifp;
577178354Ssam	struct ieee80211vap *vap;
578178354Ssam	int bit, oflags;
579178354Ssam
580178354Ssam	IEEE80211_LOCK_ASSERT(ic);
581178354Ssam
582178354Ssam	bit = 0;
583178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
584178354Ssam		if (vap->iv_ifp->if_flags & flag) {
585178354Ssam			/*
586178354Ssam			 * XXX the bridge sets PROMISC but we don't want to
587178354Ssam			 * enable it on the device, discard here so all the
588178354Ssam			 * drivers don't need to special-case it
589178354Ssam			 */
590178354Ssam			if (flag == IFF_PROMISC &&
591178354Ssam			    vap->iv_opmode == IEEE80211_M_HOSTAP)
592178354Ssam				continue;
593178354Ssam			bit = 1;
594178354Ssam			break;
595178354Ssam		}
596178354Ssam	oflags = ifp->if_flags;
597178354Ssam	if (bit)
598178354Ssam		ifp->if_flags |= flag;
599178354Ssam	else
600178354Ssam		ifp->if_flags &= ~flag;
601178354Ssam	if ((ifp->if_flags ^ oflags) & flag) {
602178354Ssam		/* XXX should we return 1/0 and let caller do this? */
603178354Ssam		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
604178354Ssam			if (flag == IFF_PROMISC)
605178354Ssam				ic->ic_update_promisc(ifp);
606178354Ssam			else if (flag == IFF_ALLMULTI)
607178354Ssam				ic->ic_update_mcast(ifp);
608178354Ssam		}
609178354Ssam	}
610178354Ssam}
611178354Ssam
612178354Ssam/*
613178354Ssam * Synchronize flag bit state in the com structure
614178354Ssam * according to the state of all vap's.  This is used,
615178354Ssam * for example, to handle state changes via ioctls.
616178354Ssam */
617178354Ssamstatic void
618178354Ssamieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
619178354Ssam{
620178354Ssam	struct ieee80211vap *vap;
621178354Ssam	int bit;
622178354Ssam
623178354Ssam	IEEE80211_LOCK_ASSERT(ic);
624178354Ssam
625178354Ssam	bit = 0;
626178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
627178354Ssam		if (vap->iv_flags & flag) {
628178354Ssam			bit = 1;
629178354Ssam			break;
630178354Ssam		}
631178354Ssam	if (bit)
632178354Ssam		ic->ic_flags |= flag;
633178354Ssam	else
634178354Ssam		ic->ic_flags &= ~flag;
635178354Ssam}
636178354Ssam
637178354Ssamvoid
638178354Ssamieee80211_syncflag(struct ieee80211vap *vap, int flag)
639178354Ssam{
640178354Ssam	struct ieee80211com *ic = vap->iv_ic;
641178354Ssam
642178354Ssam	IEEE80211_LOCK(ic);
643178354Ssam	if (flag < 0) {
644178354Ssam		flag = -flag;
645178354Ssam		vap->iv_flags &= ~flag;
646178354Ssam	} else
647178354Ssam		vap->iv_flags |= flag;
648178354Ssam	ieee80211_syncflag_locked(ic, flag);
649178354Ssam	IEEE80211_UNLOCK(ic);
650178354Ssam}
651178354Ssam
652178354Ssam/*
653178354Ssam * Synchronize flag bit state in the com structure
654178354Ssam * according to the state of all vap's.  This is used,
655178354Ssam * for example, to handle state changes via ioctls.
656178354Ssam */
657178354Ssamstatic void
658178354Ssamieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
659178354Ssam{
660178354Ssam	struct ieee80211vap *vap;
661178354Ssam	int bit;
662178354Ssam
663178354Ssam	IEEE80211_LOCK_ASSERT(ic);
664178354Ssam
665178354Ssam	bit = 0;
666178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
667178354Ssam		if (vap->iv_flags_ext & flag) {
668178354Ssam			bit = 1;
669178354Ssam			break;
670178354Ssam		}
671178354Ssam	if (bit)
672178354Ssam		ic->ic_flags_ext |= flag;
673178354Ssam	else
674178354Ssam		ic->ic_flags_ext &= ~flag;
675178354Ssam}
676178354Ssam
677178354Ssamvoid
678178354Ssamieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
679178354Ssam{
680178354Ssam	struct ieee80211com *ic = vap->iv_ic;
681178354Ssam
682178354Ssam	IEEE80211_LOCK(ic);
683178354Ssam	if (flag < 0) {
684178354Ssam		flag = -flag;
685178354Ssam		vap->iv_flags_ext &= ~flag;
686178354Ssam	} else
687178354Ssam		vap->iv_flags_ext |= flag;
688178354Ssam	ieee80211_syncflag_ext_locked(ic, flag);
689178354Ssam	IEEE80211_UNLOCK(ic);
690178354Ssam}
691178354Ssam
692166012Ssamstatic __inline int
693166012Ssammapgsm(u_int freq, u_int flags)
694166012Ssam{
695166012Ssam	freq *= 10;
696166012Ssam	if (flags & IEEE80211_CHAN_QUARTER)
697166012Ssam		freq += 5;
698166012Ssam	else if (flags & IEEE80211_CHAN_HALF)
699166012Ssam		freq += 10;
700166012Ssam	else
701166012Ssam		freq += 20;
702166012Ssam	/* NB: there is no 907/20 wide but leave room */
703166012Ssam	return (freq - 906*10) / 5;
704166012Ssam}
705166012Ssam
706166012Ssamstatic __inline int
707166012Ssammappsb(u_int freq, u_int flags)
708166012Ssam{
709166012Ssam	return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
710166012Ssam}
711166012Ssam
712116742Ssam/*
713116742Ssam * Convert MHz frequency to IEEE channel number.
714116742Ssam */
715152450Ssamint
716116742Ssamieee80211_mhz2ieee(u_int freq, u_int flags)
717116742Ssam{
718167430Ssam#define	IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
719166012Ssam	if (flags & IEEE80211_CHAN_GSM)
720166012Ssam		return mapgsm(freq, flags);
721116742Ssam	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
722116742Ssam		if (freq == 2484)
723116742Ssam			return 14;
724116742Ssam		if (freq < 2484)
725152450Ssam			return ((int) freq - 2407) / 5;
726116742Ssam		else
727116742Ssam			return 15 + ((freq - 2512) / 20);
728116899Ssam	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
729165569Ssam		if (freq <= 5000) {
730170530Ssam			/* XXX check regdomain? */
731167430Ssam			if (IS_FREQ_IN_PSB(freq))
732166012Ssam				return mappsb(freq, flags);
733152450Ssam			return (freq - 4000) / 5;
734165569Ssam		} else
735152450Ssam			return (freq - 5000) / 5;
736116742Ssam	} else {				/* either, guess */
737116742Ssam		if (freq == 2484)
738116742Ssam			return 14;
739166012Ssam		if (freq < 2484) {
740166012Ssam			if (907 <= freq && freq <= 922)
741166012Ssam				return mapgsm(freq, flags);
742152450Ssam			return ((int) freq - 2407) / 5;
743166012Ssam		}
744152450Ssam		if (freq < 5000) {
745167430Ssam			if (IS_FREQ_IN_PSB(freq))
746166012Ssam				return mappsb(freq, flags);
747165569Ssam			else if (freq > 4900)
748152450Ssam				return (freq - 4000) / 5;
749152450Ssam			else
750152450Ssam				return 15 + ((freq - 2512) / 20);
751152450Ssam		}
752116742Ssam		return (freq - 5000) / 5;
753116742Ssam	}
754167430Ssam#undef IS_FREQ_IN_PSB
755116742Ssam}
756116742Ssam
757116742Ssam/*
758116742Ssam * Convert channel to IEEE channel number.
759116742Ssam */
760152450Ssamint
761165825Smjacobieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
762116742Ssam{
763170530Ssam	if (c == NULL) {
764138568Ssam		if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
765117039Ssam		return 0;		/* XXX */
766116742Ssam	}
767170530Ssam	return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
768116742Ssam}
769116742Ssam
770116742Ssam/*
771116742Ssam * Convert IEEE channel number to MHz frequency.
772116742Ssam */
773116742Ssamu_int
774116742Ssamieee80211_ieee2mhz(u_int chan, u_int flags)
775116742Ssam{
776166012Ssam	if (flags & IEEE80211_CHAN_GSM)
777166012Ssam		return 907 + 5 * (chan / 10);
778116742Ssam	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
779116742Ssam		if (chan == 14)
780116742Ssam			return 2484;
781116742Ssam		if (chan < 14)
782116742Ssam			return 2407 + chan*5;
783116742Ssam		else
784116742Ssam			return 2512 + ((chan-15)*20);
785116742Ssam	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
786165569Ssam		if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
787165569Ssam			chan -= 37;
788165569Ssam			return 4940 + chan*5 + (chan % 5 ? 2 : 0);
789165569Ssam		}
790116742Ssam		return 5000 + (chan*5);
791116742Ssam	} else {				/* either, guess */
792166012Ssam		/* XXX can't distinguish PSB+GSM channels */
793116742Ssam		if (chan == 14)
794116742Ssam			return 2484;
795116742Ssam		if (chan < 14)			/* 0-13 */
796116742Ssam			return 2407 + chan*5;
797116742Ssam		if (chan < 27)			/* 15-26 */
798116742Ssam			return 2512 + ((chan-15)*20);
799116742Ssam		return 5000 + (chan*5);
800116742Ssam	}
801116742Ssam}
802116742Ssam
803116742Ssam/*
804170530Ssam * Locate a channel given a frequency+flags.  We cache
805178354Ssam * the previous lookup to optimize switching between two
806170530Ssam * channels--as happens with dynamic turbo.
807170530Ssam */
808170530Ssamstruct ieee80211_channel *
809170530Ssamieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
810170530Ssam{
811170530Ssam	struct ieee80211_channel *c;
812170530Ssam	int i;
813170530Ssam
814170530Ssam	flags &= IEEE80211_CHAN_ALLTURBO;
815170530Ssam	c = ic->ic_prevchan;
816170530Ssam	if (c != NULL && c->ic_freq == freq &&
817170530Ssam	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
818170530Ssam		return c;
819170530Ssam	/* brute force search */
820170530Ssam	for (i = 0; i < ic->ic_nchans; i++) {
821170530Ssam		c = &ic->ic_channels[i];
822170530Ssam		if (c->ic_freq == freq &&
823170530Ssam		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
824170530Ssam			return c;
825170530Ssam	}
826170530Ssam	return NULL;
827170530Ssam}
828170530Ssam
829173861Ssam/*
830173861Ssam * Locate a channel given a channel number+flags.  We cache
831173861Ssam * the previous lookup to optimize switching between two
832173861Ssam * channels--as happens with dynamic turbo.
833173861Ssam */
834173861Ssamstruct ieee80211_channel *
835173861Ssamieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
836173861Ssam{
837173861Ssam	struct ieee80211_channel *c;
838173861Ssam	int i;
839173861Ssam
840173861Ssam	flags &= IEEE80211_CHAN_ALLTURBO;
841173861Ssam	c = ic->ic_prevchan;
842173861Ssam	if (c != NULL && c->ic_ieee == ieee &&
843173861Ssam	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
844173861Ssam		return c;
845173861Ssam	/* brute force search */
846173861Ssam	for (i = 0; i < ic->ic_nchans; i++) {
847173861Ssam		c = &ic->ic_channels[i];
848173861Ssam		if (c->ic_ieee == ieee &&
849173861Ssam		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
850173861Ssam			return c;
851173861Ssam	}
852173861Ssam	return NULL;
853173861Ssam}
854173861Ssam
855170530Ssamstatic void
856178354Ssamaddmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
857170530Ssam{
858170530Ssam#define	ADD(_ic, _s, _o) \
859178354Ssam	ifmedia_add(media, \
860170530Ssam		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
861170530Ssam	static const u_int mopts[IEEE80211_MODE_MAX] = {
862188106Ssam	    [IEEE80211_MODE_AUTO]	= IFM_AUTO,
863188106Ssam	    [IEEE80211_MODE_11A]	= IFM_IEEE80211_11A,
864188106Ssam	    [IEEE80211_MODE_11B]	= IFM_IEEE80211_11B,
865188106Ssam	    [IEEE80211_MODE_11G]	= IFM_IEEE80211_11G,
866188106Ssam	    [IEEE80211_MODE_FH]		= IFM_IEEE80211_FH,
867188106Ssam	    [IEEE80211_MODE_TURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
868188106Ssam	    [IEEE80211_MODE_TURBO_G]	= IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
869188106Ssam	    [IEEE80211_MODE_STURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
870188106Ssam	    [IEEE80211_MODE_11NA]	= IFM_IEEE80211_11NA,
871188106Ssam	    [IEEE80211_MODE_11NG]	= IFM_IEEE80211_11NG,
872170530Ssam	};
873170530Ssam	u_int mopt;
874170530Ssam
875170530Ssam	mopt = mopts[mode];
876178354Ssam	if (addsta)
877178354Ssam		ADD(ic, mword, mopt);	/* STA mode has no cap */
878178354Ssam	if (caps & IEEE80211_C_IBSS)
879178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
880178354Ssam	if (caps & IEEE80211_C_HOSTAP)
881178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
882178354Ssam	if (caps & IEEE80211_C_AHDEMO)
883178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
884178354Ssam	if (caps & IEEE80211_C_MONITOR)
885178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
886178354Ssam	if (caps & IEEE80211_C_WDS)
887178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_WDS);
888170530Ssam#undef ADD
889170530Ssam}
890170530Ssam
891170530Ssam/*
892116742Ssam * Setup the media data structures according to the channel and
893178354Ssam * rate tables.
894116742Ssam */
895178354Ssamstatic int
896178354Ssamieee80211_media_setup(struct ieee80211com *ic,
897178354Ssam	struct ifmedia *media, int caps, int addsta,
898116742Ssam	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
899116742Ssam{
900170530Ssam	int i, j, mode, rate, maxrate, mword, r;
901170530Ssam	const struct ieee80211_rateset *rs;
902116742Ssam	struct ieee80211_rateset allrates;
903116742Ssam
904118887Ssam	/*
905116742Ssam	 * Fill in media characteristics.
906116742Ssam	 */
907178354Ssam	ifmedia_init(media, 0, media_change, media_stat);
908116742Ssam	maxrate = 0;
909170530Ssam	/*
910170530Ssam	 * Add media for legacy operating modes.
911170530Ssam	 */
912116742Ssam	memset(&allrates, 0, sizeof(allrates));
913170530Ssam	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
914167468Ssam		if (isclr(ic->ic_modecaps, mode))
915116742Ssam			continue;
916178354Ssam		addmedia(media, caps, addsta, mode, IFM_AUTO);
917116742Ssam		if (mode == IEEE80211_MODE_AUTO)
918116742Ssam			continue;
919116742Ssam		rs = &ic->ic_sup_rates[mode];
920116742Ssam		for (i = 0; i < rs->rs_nrates; i++) {
921116742Ssam			rate = rs->rs_rates[i];
922116742Ssam			mword = ieee80211_rate2media(ic, rate, mode);
923116742Ssam			if (mword == 0)
924116742Ssam				continue;
925178354Ssam			addmedia(media, caps, addsta, mode, mword);
926116742Ssam			/*
927170530Ssam			 * Add legacy rate to the collection of all rates.
928116742Ssam			 */
929116742Ssam			r = rate & IEEE80211_RATE_VAL;
930116742Ssam			for (j = 0; j < allrates.rs_nrates; j++)
931116742Ssam				if (allrates.rs_rates[j] == r)
932116742Ssam					break;
933116742Ssam			if (j == allrates.rs_nrates) {
934116742Ssam				/* unique, add to the set */
935116742Ssam				allrates.rs_rates[j] = r;
936116742Ssam				allrates.rs_nrates++;
937116742Ssam			}
938116742Ssam			rate = (rate & IEEE80211_RATE_VAL) / 2;
939116742Ssam			if (rate > maxrate)
940116742Ssam				maxrate = rate;
941116742Ssam		}
942116742Ssam	}
943116742Ssam	for (i = 0; i < allrates.rs_nrates; i++) {
944116742Ssam		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
945116742Ssam				IEEE80211_MODE_AUTO);
946116742Ssam		if (mword == 0)
947116742Ssam			continue;
948170530Ssam		/* NB: remove media options from mword */
949178354Ssam		addmedia(media, caps, addsta,
950178354Ssam		    IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
951116742Ssam	}
952170530Ssam	/*
953170530Ssam	 * Add HT/11n media.  Note that we do not have enough
954170530Ssam	 * bits in the media subtype to express the MCS so we
955170530Ssam	 * use a "placeholder" media subtype and any fixed MCS
956170530Ssam	 * must be specified with a different mechanism.
957170530Ssam	 */
958170530Ssam	for (; mode < IEEE80211_MODE_MAX; mode++) {
959170530Ssam		if (isclr(ic->ic_modecaps, mode))
960170530Ssam			continue;
961178354Ssam		addmedia(media, caps, addsta, mode, IFM_AUTO);
962178354Ssam		addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
963170530Ssam	}
964170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
965170530Ssam	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
966178354Ssam		addmedia(media, caps, addsta,
967178354Ssam		    IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
968170530Ssam		/* XXX could walk htrates */
969170530Ssam		/* XXX known array size */
970178354Ssam		if (ieee80211_htrates[15].ht40_rate_400ns > maxrate)
971178354Ssam			maxrate = ieee80211_htrates[15].ht40_rate_400ns;
972170530Ssam	}
973178354Ssam	return maxrate;
974178354Ssam}
975116742Ssam
976178354Ssamvoid
977178354Ssamieee80211_media_init(struct ieee80211com *ic)
978178354Ssam{
979178354Ssam	struct ifnet *ifp = ic->ic_ifp;
980178354Ssam	int maxrate;
981178354Ssam
982178354Ssam	/* NB: this works because the structure is initialized to zero */
983178354Ssam	if (!LIST_EMPTY(&ic->ic_media.ifm_list)) {
984178354Ssam		/*
985178354Ssam		 * We are re-initializing the channel list; clear
986178354Ssam		 * the existing media state as the media routines
987178354Ssam		 * don't suppress duplicates.
988178354Ssam		 */
989178354Ssam		ifmedia_removeall(&ic->ic_media);
990178354Ssam	}
991178354Ssam	ieee80211_chan_init(ic);
992178354Ssam
993178354Ssam	/*
994178354Ssam	 * Recalculate media settings in case new channel list changes
995178354Ssam	 * the set of available modes.
996178354Ssam	 */
997178354Ssam	maxrate = ieee80211_media_setup(ic, &ic->ic_media, ic->ic_caps, 1,
998178354Ssam		ieee80211com_media_change, ieee80211com_media_status);
999170530Ssam	/* NB: strip explicit mode; we're actually in autoselect */
1000170530Ssam	ifmedia_set(&ic->ic_media,
1001188106Ssam	    media_status(ic->ic_opmode, ic->ic_curchan) &~
1002188106Ssam		(IFM_MMASK | IFM_IEEE80211_TURBO));
1003116742Ssam	if (maxrate)
1004116742Ssam		ifp->if_baudrate = IF_Mbps(maxrate);
1005178354Ssam
1006178354Ssam	/* XXX need to propagate new media settings to vap's */
1007116742Ssam}
1008116742Ssam
1009165569Ssamconst struct ieee80211_rateset *
1010165569Ssamieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1011165569Ssam{
1012166012Ssam	if (IEEE80211_IS_CHAN_HALF(c))
1013166012Ssam		return &ieee80211_rateset_half;
1014166012Ssam	if (IEEE80211_IS_CHAN_QUARTER(c))
1015166012Ssam		return &ieee80211_rateset_quarter;
1016188774Ssam	/* XXX does this work for 11ng basic rates? */
1017170530Ssam	return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1018165569Ssam}
1019165569Ssam
1020138568Ssamvoid
1021138568Ssamieee80211_announce(struct ieee80211com *ic)
1022138568Ssam{
1023138568Ssam	struct ifnet *ifp = ic->ic_ifp;
1024138568Ssam	int i, mode, rate, mword;
1025170530Ssam	const struct ieee80211_rateset *rs;
1026138568Ssam
1027172227Ssam	/* NB: skip AUTO since it has no rates */
1028172227Ssam	for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1029167468Ssam		if (isclr(ic->ic_modecaps, mode))
1030138568Ssam			continue;
1031138568Ssam		if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
1032138568Ssam		rs = &ic->ic_sup_rates[mode];
1033138568Ssam		for (i = 0; i < rs->rs_nrates; i++) {
1034170530Ssam			mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1035138568Ssam			if (mword == 0)
1036138568Ssam				continue;
1037170530Ssam			rate = ieee80211_media2rate(mword);
1038138568Ssam			printf("%s%d%sMbps", (i != 0 ? " " : ""),
1039170530Ssam			    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1040138568Ssam		}
1041138568Ssam		printf("\n");
1042138568Ssam	}
1043170530Ssam	ieee80211_ht_announce(ic);
1044138568Ssam}
1045138568Ssam
1046170530Ssamvoid
1047170530Ssamieee80211_announce_channels(struct ieee80211com *ic)
1048116742Ssam{
1049170530Ssam	const struct ieee80211_channel *c;
1050170530Ssam	char type;
1051170530Ssam	int i, cw;
1052170530Ssam
1053170530Ssam	printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
1054170530Ssam	for (i = 0; i < ic->ic_nchans; i++) {
1055170530Ssam		c = &ic->ic_channels[i];
1056170530Ssam		if (IEEE80211_IS_CHAN_ST(c))
1057170530Ssam			type = 'S';
1058170530Ssam		else if (IEEE80211_IS_CHAN_108A(c))
1059170530Ssam			type = 'T';
1060170530Ssam		else if (IEEE80211_IS_CHAN_108G(c))
1061170530Ssam			type = 'G';
1062170530Ssam		else if (IEEE80211_IS_CHAN_HT(c))
1063170530Ssam			type = 'n';
1064170530Ssam		else if (IEEE80211_IS_CHAN_A(c))
1065170530Ssam			type = 'a';
1066170530Ssam		else if (IEEE80211_IS_CHAN_ANYG(c))
1067170530Ssam			type = 'g';
1068170530Ssam		else if (IEEE80211_IS_CHAN_B(c))
1069170530Ssam			type = 'b';
1070170530Ssam		else
1071170530Ssam			type = 'f';
1072170530Ssam		if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1073170530Ssam			cw = 40;
1074170530Ssam		else if (IEEE80211_IS_CHAN_HALF(c))
1075170530Ssam			cw = 10;
1076170530Ssam		else if (IEEE80211_IS_CHAN_QUARTER(c))
1077170530Ssam			cw = 5;
1078170530Ssam		else
1079170530Ssam			cw = 20;
1080170530Ssam		printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
1081170530Ssam			, c->ic_ieee, c->ic_freq, type
1082170530Ssam			, cw
1083170530Ssam			, IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1084170530Ssam			  IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1085170530Ssam			, c->ic_maxregpower
1086170530Ssam			, c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1087170530Ssam			, c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1088170530Ssam		);
1089170530Ssam	}
1090116742Ssam}
1091116742Ssam
1092170530Ssamstatic int
1093184273Ssammedia2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1094170530Ssam{
1095116742Ssam	switch (IFM_MODE(ime->ifm_media)) {
1096116742Ssam	case IFM_IEEE80211_11A:
1097178354Ssam		*mode = IEEE80211_MODE_11A;
1098116742Ssam		break;
1099116742Ssam	case IFM_IEEE80211_11B:
1100178354Ssam		*mode = IEEE80211_MODE_11B;
1101116742Ssam		break;
1102116742Ssam	case IFM_IEEE80211_11G:
1103178354Ssam		*mode = IEEE80211_MODE_11G;
1104116742Ssam		break;
1105124543Sonoe	case IFM_IEEE80211_FH:
1106178354Ssam		*mode = IEEE80211_MODE_FH;
1107124543Sonoe		break;
1108170530Ssam	case IFM_IEEE80211_11NA:
1109178354Ssam		*mode = IEEE80211_MODE_11NA;
1110170530Ssam		break;
1111170530Ssam	case IFM_IEEE80211_11NG:
1112178354Ssam		*mode = IEEE80211_MODE_11NG;
1113170530Ssam		break;
1114116742Ssam	case IFM_AUTO:
1115178354Ssam		*mode = IEEE80211_MODE_AUTO;
1116116742Ssam		break;
1117116742Ssam	default:
1118178354Ssam		return 0;
1119116742Ssam	}
1120116742Ssam	/*
1121138568Ssam	 * Turbo mode is an ``option''.
1122138568Ssam	 * XXX does not apply to AUTO
1123116742Ssam	 */
1124116742Ssam	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1125178354Ssam		if (*mode == IEEE80211_MODE_11A) {
1126184273Ssam			if (flags & IEEE80211_F_TURBOP)
1127178354Ssam				*mode = IEEE80211_MODE_TURBO_A;
1128170530Ssam			else
1129178354Ssam				*mode = IEEE80211_MODE_STURBO_A;
1130178354Ssam		} else if (*mode == IEEE80211_MODE_11G)
1131178354Ssam			*mode = IEEE80211_MODE_TURBO_G;
1132138568Ssam		else
1133178354Ssam			return 0;
1134116742Ssam	}
1135170530Ssam	/* XXX HT40 +/- */
1136178354Ssam	return 1;
1137178354Ssam}
1138116742Ssam
1139178354Ssam/*
1140184273Ssam * Handle a media change request on the underlying interface.
1141178354Ssam */
1142178354Ssamint
1143178354Ssamieee80211com_media_change(struct ifnet *ifp)
1144178354Ssam{
1145184273Ssam	return EINVAL;
1146178354Ssam}
1147116742Ssam
1148178354Ssam/*
1149178354Ssam * Handle a media change request on the vap interface.
1150178354Ssam */
1151178354Ssamint
1152178354Ssamieee80211_media_change(struct ifnet *ifp)
1153178354Ssam{
1154178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
1155178354Ssam	struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1156184273Ssam	uint16_t newmode;
1157178354Ssam
1158184273Ssam	if (!media2mode(ime, vap->iv_flags, &newmode))
1159178354Ssam		return EINVAL;
1160184273Ssam	if (vap->iv_des_mode != newmode) {
1161184273Ssam		vap->iv_des_mode = newmode;
1162178354Ssam		return ENETRESET;
1163178354Ssam	}
1164178354Ssam	return 0;
1165116742Ssam}
1166116742Ssam
1167170530Ssam/*
1168170530Ssam * Common code to calculate the media status word
1169170530Ssam * from the operating mode and channel state.
1170170530Ssam */
1171170530Ssamstatic int
1172170530Ssammedia_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1173170530Ssam{
1174170530Ssam	int status;
1175170530Ssam
1176170530Ssam	status = IFM_IEEE80211;
1177170530Ssam	switch (opmode) {
1178170530Ssam	case IEEE80211_M_STA:
1179170530Ssam		break;
1180170530Ssam	case IEEE80211_M_IBSS:
1181170530Ssam		status |= IFM_IEEE80211_ADHOC;
1182170530Ssam		break;
1183170530Ssam	case IEEE80211_M_HOSTAP:
1184170530Ssam		status |= IFM_IEEE80211_HOSTAP;
1185170530Ssam		break;
1186170530Ssam	case IEEE80211_M_MONITOR:
1187170530Ssam		status |= IFM_IEEE80211_MONITOR;
1188170530Ssam		break;
1189170530Ssam	case IEEE80211_M_AHDEMO:
1190170530Ssam		status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1191170530Ssam		break;
1192170530Ssam	case IEEE80211_M_WDS:
1193178354Ssam		status |= IFM_IEEE80211_WDS;
1194170530Ssam		break;
1195170530Ssam	}
1196170530Ssam	if (IEEE80211_IS_CHAN_HTA(chan)) {
1197170530Ssam		status |= IFM_IEEE80211_11NA;
1198170530Ssam	} else if (IEEE80211_IS_CHAN_HTG(chan)) {
1199170530Ssam		status |= IFM_IEEE80211_11NG;
1200170530Ssam	} else if (IEEE80211_IS_CHAN_A(chan)) {
1201170530Ssam		status |= IFM_IEEE80211_11A;
1202170530Ssam	} else if (IEEE80211_IS_CHAN_B(chan)) {
1203170530Ssam		status |= IFM_IEEE80211_11B;
1204170530Ssam	} else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1205170530Ssam		status |= IFM_IEEE80211_11G;
1206170530Ssam	} else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1207170530Ssam		status |= IFM_IEEE80211_FH;
1208170530Ssam	}
1209170530Ssam	/* XXX else complain? */
1210170530Ssam
1211170530Ssam	if (IEEE80211_IS_CHAN_TURBO(chan))
1212170530Ssam		status |= IFM_IEEE80211_TURBO;
1213178354Ssam#if 0
1214178354Ssam	if (IEEE80211_IS_CHAN_HT20(chan))
1215178354Ssam		status |= IFM_IEEE80211_HT20;
1216178354Ssam	if (IEEE80211_IS_CHAN_HT40(chan))
1217178354Ssam		status |= IFM_IEEE80211_HT40;
1218178354Ssam#endif
1219170530Ssam	return status;
1220170530Ssam}
1221170530Ssam
1222178354Ssamstatic void
1223178354Ssamieee80211com_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1224178354Ssam{
1225178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1226178354Ssam	struct ieee80211vap *vap;
1227178354Ssam
1228178354Ssam	imr->ifm_status = IFM_AVALID;
1229178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1230178354Ssam		if (vap->iv_ifp->if_flags & IFF_UP) {
1231178354Ssam			imr->ifm_status |= IFM_ACTIVE;
1232178354Ssam			break;
1233178354Ssam		}
1234178354Ssam	imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
1235178354Ssam	if (imr->ifm_status & IFM_ACTIVE)
1236178354Ssam		imr->ifm_current = imr->ifm_active;
1237178354Ssam}
1238178354Ssam
1239116742Ssamvoid
1240116742Ssamieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1241116742Ssam{
1242178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
1243178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1244170530Ssam	enum ieee80211_phymode mode;
1245116742Ssam
1246116742Ssam	imr->ifm_status = IFM_AVALID;
1247170530Ssam	/*
1248170530Ssam	 * NB: use the current channel's mode to lock down a xmit
1249170530Ssam	 * rate only when running; otherwise we may have a mismatch
1250170530Ssam	 * in which case the rate will not be convertible.
1251170530Ssam	 */
1252178354Ssam	if (vap->iv_state == IEEE80211_S_RUN) {
1253116742Ssam		imr->ifm_status |= IFM_ACTIVE;
1254170530Ssam		mode = ieee80211_chan2mode(ic->ic_curchan);
1255170530Ssam	} else
1256170530Ssam		mode = IEEE80211_MODE_AUTO;
1257178354Ssam	imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1258138568Ssam	/*
1259138568Ssam	 * Calculate a current rate if possible.
1260138568Ssam	 */
1261178354Ssam	if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1262138568Ssam		/*
1263138568Ssam		 * A fixed rate is set, report that.
1264138568Ssam		 */
1265138568Ssam		imr->ifm_active |= ieee80211_rate2media(ic,
1266178354Ssam			vap->iv_txparms[mode].ucastrate, mode);
1267178354Ssam	} else if (vap->iv_opmode == IEEE80211_M_STA) {
1268138568Ssam		/*
1269138568Ssam		 * In station mode report the current transmit rate.
1270138568Ssam		 */
1271138568Ssam		imr->ifm_active |= ieee80211_rate2media(ic,
1272178354Ssam			vap->iv_bss->ni_txrate, mode);
1273128966Sandre	} else
1274138568Ssam		imr->ifm_active |= IFM_AUTO;
1275178354Ssam	if (imr->ifm_status & IFM_ACTIVE)
1276178354Ssam		imr->ifm_current = imr->ifm_active;
1277116742Ssam}
1278116742Ssam
1279116742Ssam/*
1280116742Ssam * Set the current phy mode and recalculate the active channel
1281116742Ssam * set based on the available channels for this mode.  Also
1282116742Ssam * select a new default/current channel if the current one is
1283116742Ssam * inappropriate for this mode.
1284116742Ssam */
1285116742Ssamint
1286116742Ssamieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1287116742Ssam{
1288116742Ssam	/*
1289166012Ssam	 * Adjust basic rates in 11b/11g supported rate set.
1290166012Ssam	 * Note that if operating on a hal/quarter rate channel
1291166012Ssam	 * this is a noop as those rates sets are different
1292166012Ssam	 * and used instead.
1293116742Ssam	 */
1294166012Ssam	if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1295178354Ssam		ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1296166012Ssam
1297116742Ssam	ic->ic_curmode = mode;
1298138568Ssam	ieee80211_reset_erp(ic);	/* reset ERP state */
1299138568Ssam
1300116742Ssam	return 0;
1301116742Ssam}
1302116742Ssam
1303116742Ssam/*
1304170530Ssam * Return the phy mode for with the specified channel.
1305116742Ssam */
1306116742Ssamenum ieee80211_phymode
1307170530Ssamieee80211_chan2mode(const struct ieee80211_channel *chan)
1308116742Ssam{
1309170530Ssam
1310170530Ssam	if (IEEE80211_IS_CHAN_HTA(chan))
1311170530Ssam		return IEEE80211_MODE_11NA;
1312170530Ssam	else if (IEEE80211_IS_CHAN_HTG(chan))
1313170530Ssam		return IEEE80211_MODE_11NG;
1314170530Ssam	else if (IEEE80211_IS_CHAN_108G(chan))
1315170530Ssam		return IEEE80211_MODE_TURBO_G;
1316170530Ssam	else if (IEEE80211_IS_CHAN_ST(chan))
1317170530Ssam		return IEEE80211_MODE_STURBO_A;
1318170530Ssam	else if (IEEE80211_IS_CHAN_TURBO(chan))
1319153350Ssam		return IEEE80211_MODE_TURBO_A;
1320170530Ssam	else if (IEEE80211_IS_CHAN_A(chan))
1321116742Ssam		return IEEE80211_MODE_11A;
1322170530Ssam	else if (IEEE80211_IS_CHAN_ANYG(chan))
1323116742Ssam		return IEEE80211_MODE_11G;
1324170530Ssam	else if (IEEE80211_IS_CHAN_B(chan))
1325116742Ssam		return IEEE80211_MODE_11B;
1326170530Ssam	else if (IEEE80211_IS_CHAN_FHSS(chan))
1327170530Ssam		return IEEE80211_MODE_FH;
1328170530Ssam
1329170530Ssam	/* NB: should not get here */
1330170530Ssam	printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1331170530Ssam		__func__, chan->ic_freq, chan->ic_flags);
1332170530Ssam	return IEEE80211_MODE_11B;
1333116742Ssam}
1334116742Ssam
1335170530Ssamstruct ratemedia {
1336170530Ssam	u_int	match;	/* rate + mode */
1337170530Ssam	u_int	media;	/* if_media rate */
1338170530Ssam};
1339170530Ssam
1340170530Ssamstatic int
1341170530Ssamfindmedia(const struct ratemedia rates[], int n, u_int match)
1342170530Ssam{
1343170530Ssam	int i;
1344170530Ssam
1345170530Ssam	for (i = 0; i < n; i++)
1346170530Ssam		if (rates[i].match == match)
1347170530Ssam			return rates[i].media;
1348170530Ssam	return IFM_AUTO;
1349170530Ssam}
1350170530Ssam
1351116742Ssam/*
1352170530Ssam * Convert IEEE80211 rate value to ifmedia subtype.
1353170530Ssam * Rate is either a legacy rate in units of 0.5Mbps
1354170530Ssam * or an MCS index.
1355116742Ssam */
1356116742Ssamint
1357116742Ssamieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1358116742Ssam{
1359116742Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
1360170530Ssam	static const struct ratemedia rates[] = {
1361124543Sonoe		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1362124543Sonoe		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1363124543Sonoe		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1364124543Sonoe		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1365124543Sonoe		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1366124543Sonoe		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1367124543Sonoe		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1368124543Sonoe		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1369124543Sonoe		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1370124543Sonoe		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1371124543Sonoe		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1372124543Sonoe		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1373124543Sonoe		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1374124543Sonoe		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1375124543Sonoe		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1376124543Sonoe		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1377124543Sonoe		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1378124543Sonoe		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1379124543Sonoe		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1380124543Sonoe		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1381124543Sonoe		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1382124543Sonoe		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1383124543Sonoe		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1384124543Sonoe		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1385124543Sonoe		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1386124543Sonoe		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1387124543Sonoe		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1388165569Ssam		{   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1389165569Ssam		{   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1390165569Ssam		{  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1391116742Ssam		/* NB: OFDM72 doesn't realy exist so we don't handle it */
1392116742Ssam	};
1393170530Ssam	static const struct ratemedia htrates[] = {
1394170530Ssam		{   0, IFM_IEEE80211_MCS },
1395170530Ssam		{   1, IFM_IEEE80211_MCS },
1396170530Ssam		{   2, IFM_IEEE80211_MCS },
1397170530Ssam		{   3, IFM_IEEE80211_MCS },
1398170530Ssam		{   4, IFM_IEEE80211_MCS },
1399170530Ssam		{   5, IFM_IEEE80211_MCS },
1400170530Ssam		{   6, IFM_IEEE80211_MCS },
1401170530Ssam		{   7, IFM_IEEE80211_MCS },
1402170530Ssam		{   8, IFM_IEEE80211_MCS },
1403170530Ssam		{   9, IFM_IEEE80211_MCS },
1404170530Ssam		{  10, IFM_IEEE80211_MCS },
1405170530Ssam		{  11, IFM_IEEE80211_MCS },
1406170530Ssam		{  12, IFM_IEEE80211_MCS },
1407170530Ssam		{  13, IFM_IEEE80211_MCS },
1408170530Ssam		{  14, IFM_IEEE80211_MCS },
1409170530Ssam		{  15, IFM_IEEE80211_MCS },
1410170530Ssam	};
1411170530Ssam	int m;
1412116742Ssam
1413170530Ssam	/*
1414170530Ssam	 * Check 11n rates first for match as an MCS.
1415170530Ssam	 */
1416170530Ssam	if (mode == IEEE80211_MODE_11NA) {
1417172226Ssam		if (rate & IEEE80211_RATE_MCS) {
1418172226Ssam			rate &= ~IEEE80211_RATE_MCS;
1419170530Ssam			m = findmedia(htrates, N(htrates), rate);
1420170530Ssam			if (m != IFM_AUTO)
1421170530Ssam				return m | IFM_IEEE80211_11NA;
1422170530Ssam		}
1423170530Ssam	} else if (mode == IEEE80211_MODE_11NG) {
1424170530Ssam		/* NB: 12 is ambiguous, it will be treated as an MCS */
1425172226Ssam		if (rate & IEEE80211_RATE_MCS) {
1426172226Ssam			rate &= ~IEEE80211_RATE_MCS;
1427170530Ssam			m = findmedia(htrates, N(htrates), rate);
1428170530Ssam			if (m != IFM_AUTO)
1429170530Ssam				return m | IFM_IEEE80211_11NG;
1430170530Ssam		}
1431170530Ssam	}
1432170530Ssam	rate &= IEEE80211_RATE_VAL;
1433116742Ssam	switch (mode) {
1434116742Ssam	case IEEE80211_MODE_11A:
1435170530Ssam	case IEEE80211_MODE_11NA:
1436138568Ssam	case IEEE80211_MODE_TURBO_A:
1437170530Ssam	case IEEE80211_MODE_STURBO_A:
1438170530Ssam		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11A);
1439116742Ssam	case IEEE80211_MODE_11B:
1440170530Ssam		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11B);
1441124543Sonoe	case IEEE80211_MODE_FH:
1442170530Ssam		return findmedia(rates, N(rates), rate | IFM_IEEE80211_FH);
1443116742Ssam	case IEEE80211_MODE_AUTO:
1444116742Ssam		/* NB: ic may be NULL for some drivers */
1445170530Ssam		if (ic && ic->ic_phytype == IEEE80211_T_FH)
1446170530Ssam			return findmedia(rates, N(rates),
1447170530Ssam			    rate | IFM_IEEE80211_FH);
1448116742Ssam		/* NB: hack, 11g matches both 11b+11a rates */
1449116742Ssam		/* fall thru... */
1450116742Ssam	case IEEE80211_MODE_11G:
1451170530Ssam	case IEEE80211_MODE_11NG:
1452138568Ssam	case IEEE80211_MODE_TURBO_G:
1453170530Ssam		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11G);
1454116742Ssam	}
1455116742Ssam	return IFM_AUTO;
1456116742Ssam#undef N
1457116742Ssam}
1458116742Ssam
1459116742Ssamint
1460116742Ssamieee80211_media2rate(int mword)
1461116742Ssam{
1462116742Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
1463116742Ssam	static const int ieeerates[] = {
1464116742Ssam		-1,		/* IFM_AUTO */
1465116742Ssam		0,		/* IFM_MANUAL */
1466116742Ssam		0,		/* IFM_NONE */
1467116742Ssam		2,		/* IFM_IEEE80211_FH1 */
1468116742Ssam		4,		/* IFM_IEEE80211_FH2 */
1469116742Ssam		2,		/* IFM_IEEE80211_DS1 */
1470116742Ssam		4,		/* IFM_IEEE80211_DS2 */
1471116742Ssam		11,		/* IFM_IEEE80211_DS5 */
1472116742Ssam		22,		/* IFM_IEEE80211_DS11 */
1473116742Ssam		44,		/* IFM_IEEE80211_DS22 */
1474116742Ssam		12,		/* IFM_IEEE80211_OFDM6 */
1475116742Ssam		18,		/* IFM_IEEE80211_OFDM9 */
1476116742Ssam		24,		/* IFM_IEEE80211_OFDM12 */
1477116742Ssam		36,		/* IFM_IEEE80211_OFDM18 */
1478116742Ssam		48,		/* IFM_IEEE80211_OFDM24 */
1479116742Ssam		72,		/* IFM_IEEE80211_OFDM36 */
1480116742Ssam		96,		/* IFM_IEEE80211_OFDM48 */
1481116742Ssam		108,		/* IFM_IEEE80211_OFDM54 */
1482116742Ssam		144,		/* IFM_IEEE80211_OFDM72 */
1483165569Ssam		0,		/* IFM_IEEE80211_DS354k */
1484165569Ssam		0,		/* IFM_IEEE80211_DS512k */
1485165569Ssam		6,		/* IFM_IEEE80211_OFDM3 */
1486165569Ssam		9,		/* IFM_IEEE80211_OFDM4 */
1487165569Ssam		54,		/* IFM_IEEE80211_OFDM27 */
1488170530Ssam		-1,		/* IFM_IEEE80211_MCS */
1489116742Ssam	};
1490116742Ssam	return IFM_SUBTYPE(mword) < N(ieeerates) ?
1491116742Ssam		ieeerates[IFM_SUBTYPE(mword)] : 0;
1492116742Ssam#undef N
1493116742Ssam}
1494