ieee80211.c revision 187897
1/*-
2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/net80211/ieee80211.c 187897 2009-01-29 23:09:12Z sam $");
29
30/*
31 * IEEE 802.11 generic handler
32 */
33#include "opt_wlan.h"
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38
39#include <sys/socket.h>
40
41#include <net/if.h>
42#include <net/if_dl.h>
43#include <net/if_media.h>
44#include <net/if_types.h>
45#include <net/ethernet.h>
46
47#include <net80211/ieee80211_var.h>
48#include <net80211/ieee80211_regdomain.h>
49
50#include <net/bpf.h>
51
52const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
53	[IEEE80211_MODE_AUTO]	  = "auto",
54	[IEEE80211_MODE_11A]	  = "11a",
55	[IEEE80211_MODE_11B]	  = "11b",
56	[IEEE80211_MODE_11G]	  = "11g",
57	[IEEE80211_MODE_FH]	  = "FH",
58	[IEEE80211_MODE_TURBO_A]  = "turboA",
59	[IEEE80211_MODE_TURBO_G]  = "turboG",
60	[IEEE80211_MODE_STURBO_A] = "sturboA",
61	[IEEE80211_MODE_11NA]	  = "11na",
62	[IEEE80211_MODE_11NG]	  = "11ng",
63};
64/* map ieee80211_opmode to the corresponding capability bit */
65const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
66	[IEEE80211_M_IBSS]	= IEEE80211_C_IBSS,
67	[IEEE80211_M_WDS]	= IEEE80211_C_WDS,
68	[IEEE80211_M_STA]	= IEEE80211_C_STA,
69	[IEEE80211_M_AHDEMO]	= IEEE80211_C_AHDEMO,
70	[IEEE80211_M_HOSTAP]	= IEEE80211_C_HOSTAP,
71	[IEEE80211_M_MONITOR]	= IEEE80211_C_MONITOR,
72};
73
74static const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
75	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
76
77static	void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
78static	void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
79static	int ieee80211_media_setup(struct ieee80211com *ic,
80		struct ifmedia *media, int caps, int addsta,
81		ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
82static	void ieee80211com_media_status(struct ifnet *, struct ifmediareq *);
83static	int ieee80211com_media_change(struct ifnet *);
84static	int media_status(enum ieee80211_opmode,
85		const struct ieee80211_channel *);
86
87MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
88
89/*
90 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
91 */
92#define	B(r)	((r) | IEEE80211_RATE_BASIC)
93static const struct ieee80211_rateset ieee80211_rateset_11a =
94	{ 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
95static const struct ieee80211_rateset ieee80211_rateset_half =
96	{ 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
97static const struct ieee80211_rateset ieee80211_rateset_quarter =
98	{ 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
99static const struct ieee80211_rateset ieee80211_rateset_11b =
100	{ 4, { B(2), B(4), B(11), B(22) } };
101/* NB: OFDM rates are handled specially based on mode */
102static const struct ieee80211_rateset ieee80211_rateset_11g =
103	{ 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
104#undef B
105
106/*
107 * Fill in 802.11 available channel set, mark
108 * all available channels as active, and pick
109 * a default channel if not already specified.
110 */
111static void
112ieee80211_chan_init(struct ieee80211com *ic)
113{
114#define	DEFAULTRATES(m, def) do { \
115	if (isset(ic->ic_modecaps, m) && ic->ic_sup_rates[m].rs_nrates == 0) \
116		ic->ic_sup_rates[m] = def; \
117} while (0)
118	struct ieee80211_channel *c;
119	int i;
120
121	KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
122		("invalid number of channels specified: %u", ic->ic_nchans));
123	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
124	memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
125	setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
126	for (i = 0; i < ic->ic_nchans; i++) {
127		c = &ic->ic_channels[i];
128		KASSERT(c->ic_flags != 0, ("channel with no flags"));
129		/*
130		 * Help drivers that work only with frequencies by filling
131		 * in IEEE channel #'s if not already calculated.  Note this
132		 * mimics similar work done in ieee80211_setregdomain when
133		 * changing regulatory state.
134		 */
135		if (c->ic_ieee == 0)
136			c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
137		if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
138			c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
139			    (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
140			    c->ic_flags);
141		/* default max tx power to max regulatory */
142		if (c->ic_maxpower == 0)
143			c->ic_maxpower = 2*c->ic_maxregpower;
144		setbit(ic->ic_chan_avail, c->ic_ieee);
145		/*
146		 * Identify mode capabilities.
147		 */
148		if (IEEE80211_IS_CHAN_A(c))
149			setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
150		if (IEEE80211_IS_CHAN_B(c))
151			setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
152		if (IEEE80211_IS_CHAN_ANYG(c))
153			setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
154		if (IEEE80211_IS_CHAN_FHSS(c))
155			setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
156		if (IEEE80211_IS_CHAN_108A(c))
157			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
158		if (IEEE80211_IS_CHAN_108G(c))
159			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
160		if (IEEE80211_IS_CHAN_ST(c))
161			setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
162		if (IEEE80211_IS_CHAN_HTA(c))
163			setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
164		if (IEEE80211_IS_CHAN_HTG(c))
165			setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
166	}
167	/* initialize candidate channels to all available */
168	memcpy(ic->ic_chan_active, ic->ic_chan_avail,
169		sizeof(ic->ic_chan_avail));
170
171	/* sort channel table to allow lookup optimizations */
172	ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
173
174	/* invalidate any previous state */
175	ic->ic_bsschan = IEEE80211_CHAN_ANYC;
176	ic->ic_prevchan = NULL;
177	ic->ic_csa_newchan = NULL;
178	/* arbitrarily pick the first channel */
179	ic->ic_curchan = &ic->ic_channels[0];
180
181	/* fillin well-known rate sets if driver has not specified */
182	DEFAULTRATES(IEEE80211_MODE_11B,	 ieee80211_rateset_11b);
183	DEFAULTRATES(IEEE80211_MODE_11G,	 ieee80211_rateset_11g);
184	DEFAULTRATES(IEEE80211_MODE_11A,	 ieee80211_rateset_11a);
185	DEFAULTRATES(IEEE80211_MODE_TURBO_A,	 ieee80211_rateset_11a);
186	DEFAULTRATES(IEEE80211_MODE_TURBO_G,	 ieee80211_rateset_11g);
187	DEFAULTRATES(IEEE80211_MODE_STURBO_A,	 ieee80211_rateset_11a);
188
189	/*
190	 * Set auto mode to reset active channel state and any desired channel.
191	 */
192	(void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
193#undef DEFAULTRATES
194}
195
196static void
197null_update_mcast(struct ifnet *ifp)
198{
199	if_printf(ifp, "need multicast update callback\n");
200}
201
202static void
203null_update_promisc(struct ifnet *ifp)
204{
205	if_printf(ifp, "need promiscuous mode update callback\n");
206}
207
208static int
209null_output(struct ifnet *ifp, struct mbuf *m,
210	struct sockaddr *dst, struct rtentry *rt0)
211{
212	if_printf(ifp, "discard raw packet\n");
213	m_freem(m);
214	return EIO;
215}
216
217static void
218null_input(struct ifnet *ifp, struct mbuf *m)
219{
220	if_printf(ifp, "if_input should not be called\n");
221	m_freem(m);
222}
223
224/*
225 * Attach/setup the common net80211 state.  Called by
226 * the driver on attach to prior to creating any vap's.
227 */
228void
229ieee80211_ifattach(struct ieee80211com *ic)
230{
231	struct ifnet *ifp = ic->ic_ifp;
232	struct sockaddr_dl *sdl;
233	struct ifaddr *ifa;
234
235	KASSERT(ifp->if_type == IFT_IEEE80211, ("if_type %d", ifp->if_type));
236
237	IEEE80211_LOCK_INIT(ic, ifp->if_xname);
238	TAILQ_INIT(&ic->ic_vaps);
239	/*
240	 * Fill in 802.11 available channel set, mark all
241	 * available channels as active, and pick a default
242	 * channel if not already specified.
243	 */
244	ieee80211_media_init(ic);
245
246	ic->ic_update_mcast = null_update_mcast;
247	ic->ic_update_promisc = null_update_promisc;
248
249	ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
250	ic->ic_lintval = ic->ic_bintval;
251	ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
252
253	ieee80211_crypto_attach(ic);
254	ieee80211_node_attach(ic);
255	ieee80211_power_attach(ic);
256	ieee80211_proto_attach(ic);
257	ieee80211_ht_attach(ic);
258	ieee80211_scan_attach(ic);
259	ieee80211_regdomain_attach(ic);
260
261	ieee80211_sysctl_attach(ic);
262
263	ifp->if_addrlen = IEEE80211_ADDR_LEN;
264	ifp->if_hdrlen = 0;
265	if_attach(ifp);
266	ifp->if_mtu = IEEE80211_MTU_MAX;
267	ifp->if_broadcastaddr = ieee80211broadcastaddr;
268	ifp->if_output = null_output;
269	ifp->if_input = null_input;	/* just in case */
270	ifp->if_resolvemulti = NULL;	/* NB: callers check */
271
272	ifa = ifaddr_byindex(ifp->if_index);
273	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
274	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
275	sdl->sdl_type = IFT_ETHER;		/* XXX IFT_IEEE80211? */
276	sdl->sdl_alen = IEEE80211_ADDR_LEN;
277	IEEE80211_ADDR_COPY(LLADDR(sdl), ic->ic_myaddr);
278}
279
280/*
281 * Detach net80211 state on device detach.  Tear down
282 * all vap's and reclaim all common state prior to the
283 * device state going away.  Note we may call back into
284 * driver; it must be prepared for this.
285 */
286void
287ieee80211_ifdetach(struct ieee80211com *ic)
288{
289	struct ifnet *ifp = ic->ic_ifp;
290	struct ieee80211vap *vap;
291
292	/* XXX ieee80211_stop_all? */
293	while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
294		ieee80211_vap_destroy(vap);
295
296	ieee80211_sysctl_detach(ic);
297	ieee80211_regdomain_detach(ic);
298	ieee80211_scan_detach(ic);
299	ieee80211_ht_detach(ic);
300	/* NB: must be called before ieee80211_node_detach */
301	ieee80211_proto_detach(ic);
302	ieee80211_crypto_detach(ic);
303	ieee80211_power_detach(ic);
304	ieee80211_node_detach(ic);
305	ifmedia_removeall(&ic->ic_media);
306
307	IEEE80211_LOCK_DESTROY(ic);
308	if_detach(ifp);
309}
310
311/*
312 * Default reset method for use with the ioctl support.  This
313 * method is invoked after any state change in the 802.11
314 * layer that should be propagated to the hardware but not
315 * require re-initialization of the 802.11 state machine (e.g
316 * rescanning for an ap).  We always return ENETRESET which
317 * should cause the driver to re-initialize the device. Drivers
318 * can override this method to implement more optimized support.
319 */
320static int
321default_reset(struct ieee80211vap *vap, u_long cmd)
322{
323	return ENETRESET;
324}
325
326/*
327 * Prepare a vap for use.  Drivers use this call to
328 * setup net80211 state in new vap's prior attaching
329 * them with ieee80211_vap_attach (below).
330 */
331int
332ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
333	const char name[IFNAMSIZ], int unit, int opmode, int flags,
334	const uint8_t bssid[IEEE80211_ADDR_LEN],
335	const uint8_t macaddr[IEEE80211_ADDR_LEN])
336{
337	struct ifnet *ifp;
338
339	ifp = if_alloc(IFT_ETHER);
340	if (ifp == NULL) {
341		if_printf(ic->ic_ifp, "%s: unable to allocate ifnet\n",
342		    __func__);
343		return ENOMEM;
344	}
345	if_initname(ifp, name, unit);
346	ifp->if_softc = vap;			/* back pointer */
347	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
348	ifp->if_start = ieee80211_start;
349	ifp->if_ioctl = ieee80211_ioctl;
350	ifp->if_watchdog = NULL;		/* NB: no watchdog routine */
351	ifp->if_init = ieee80211_init;
352	/* NB: input+output filled in by ether_ifattach */
353	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
354	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
355	IFQ_SET_READY(&ifp->if_snd);
356
357	vap->iv_ifp = ifp;
358	vap->iv_ic = ic;
359	vap->iv_flags = ic->ic_flags;		/* propagate common flags */
360	vap->iv_flags_ext = ic->ic_flags_ext;
361	vap->iv_flags_ven = ic->ic_flags_ven;
362	vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
363	vap->iv_htcaps = ic->ic_htcaps;
364	vap->iv_opmode = opmode;
365	vap->iv_caps |= ieee80211_opcap[opmode];
366	switch (opmode) {
367	case IEEE80211_M_WDS:
368		/*
369		 * WDS links must specify the bssid of the far end.
370		 * For legacy operation this is a static relationship.
371		 * For non-legacy operation the station must associate
372		 * and be authorized to pass traffic.  Plumbing the
373		 * vap to the proper node happens when the vap
374		 * transitions to RUN state.
375		 */
376		IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
377		vap->iv_flags |= IEEE80211_F_DESBSSID;
378		if (flags & IEEE80211_CLONE_WDSLEGACY)
379			vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
380		break;
381#ifdef IEEE80211_SUPPORT_TDMA
382	case IEEE80211_M_AHDEMO:
383		if (flags & IEEE80211_CLONE_TDMA) {
384			/* NB: checked before clone operation allowed */
385			KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
386			    ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
387			/*
388			 * Propagate TDMA capability to mark vap; this
389			 * cannot be removed and is used to distinguish
390			 * regular ahdemo operation from ahdemo+tdma.
391			 */
392			vap->iv_caps |= IEEE80211_C_TDMA;
393		}
394		break;
395#endif
396	}
397	/* auto-enable s/w beacon miss support */
398	if (flags & IEEE80211_CLONE_NOBEACONS)
399		vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
400	/*
401	 * Enable various functionality by default if we're
402	 * capable; the driver can override us if it knows better.
403	 */
404	if (vap->iv_caps & IEEE80211_C_WME)
405		vap->iv_flags |= IEEE80211_F_WME;
406	if (vap->iv_caps & IEEE80211_C_BURST)
407		vap->iv_flags |= IEEE80211_F_BURST;
408	if (vap->iv_caps & IEEE80211_C_FF)
409		vap->iv_flags |= IEEE80211_F_FF;
410	if (vap->iv_caps & IEEE80211_C_TURBOP)
411		vap->iv_flags |= IEEE80211_F_TURBOP;
412	/* NB: bg scanning only makes sense for station mode right now */
413	if (vap->iv_opmode == IEEE80211_M_STA &&
414	    (vap->iv_caps & IEEE80211_C_BGSCAN))
415		vap->iv_flags |= IEEE80211_F_BGSCAN;
416	vap->iv_flags |= IEEE80211_F_DOTH;	/* XXX no cap, just ena */
417	/* NB: DFS support only makes sense for ap mode right now */
418	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
419	    (vap->iv_caps & IEEE80211_C_DFS))
420		vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
421
422	vap->iv_des_chan = IEEE80211_CHAN_ANYC;		/* any channel is ok */
423	vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
424	vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
425	/*
426	 * Install a default reset method for the ioctl support;
427	 * the driver can override this.
428	 */
429	vap->iv_reset = default_reset;
430
431	IEEE80211_ADDR_COPY(vap->iv_myaddr, macaddr);
432
433	ieee80211_sysctl_vattach(vap);
434	ieee80211_crypto_vattach(vap);
435	ieee80211_node_vattach(vap);
436	ieee80211_power_vattach(vap);
437	ieee80211_proto_vattach(vap);
438	ieee80211_ht_vattach(vap);
439	ieee80211_scan_vattach(vap);
440	ieee80211_regdomain_vattach(vap);
441
442	return 0;
443}
444
445/*
446 * Activate a vap.  State should have been prepared with a
447 * call to ieee80211_vap_setup and by the driver.  On return
448 * from this call the vap is ready for use.
449 */
450int
451ieee80211_vap_attach(struct ieee80211vap *vap,
452	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
453{
454	struct ifnet *ifp = vap->iv_ifp;
455	struct ieee80211com *ic = vap->iv_ic;
456	struct ifmediareq imr;
457	int maxrate;
458
459	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
460	    "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
461	    __func__, ieee80211_opmode_name[vap->iv_opmode],
462	    ic->ic_ifp->if_xname, vap->iv_flags, vap->iv_flags_ext);
463
464	/*
465	 * Do late attach work that cannot happen until after
466	 * the driver has had a chance to override defaults.
467	 */
468	ieee80211_node_latevattach(vap);
469	ieee80211_power_latevattach(vap);
470
471	maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
472	    vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
473	ieee80211_media_status(ifp, &imr);
474	/* NB: strip explicit mode; we're actually in autoselect */
475	ifmedia_set(&vap->iv_media, imr.ifm_active &~ IFM_MMASK);
476	if (maxrate)
477		ifp->if_baudrate = IF_Mbps(maxrate);
478
479	ether_ifattach(ifp, vap->iv_myaddr);
480	/* hook output method setup by ether_ifattach */
481	vap->iv_output = ifp->if_output;
482	ifp->if_output = ieee80211_output;
483	/* NB: if_mtu set by ether_ifattach to ETHERMTU */
484	bpfattach2(ifp, DLT_IEEE802_11, ifp->if_hdrlen, &vap->iv_rawbpf);
485
486	IEEE80211_LOCK(ic);
487	TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
488	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
489	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
490	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
491	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
492	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_HT);
493	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_USEHT40);
494	ieee80211_syncifflag_locked(ic, IFF_PROMISC);
495	ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
496	IEEE80211_UNLOCK(ic);
497
498	return 1;
499}
500
501/*
502 * Tear down vap state and reclaim the ifnet.
503 * The driver is assumed to have prepared for
504 * this; e.g. by turning off interrupts for the
505 * underlying device.
506 */
507void
508ieee80211_vap_detach(struct ieee80211vap *vap)
509{
510	struct ieee80211com *ic = vap->iv_ic;
511	struct ifnet *ifp = vap->iv_ifp;
512
513	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
514	    __func__, ieee80211_opmode_name[vap->iv_opmode],
515	    ic->ic_ifp->if_xname);
516
517	IEEE80211_LOCK(ic);
518	/* block traffic from above */
519	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
520	/*
521	 * Evil hack.  Clear the backpointer from the ifnet to the
522	 * vap so any requests from above will return an error or
523	 * be ignored.  In particular this short-circuits requests
524	 * by the bridge to turn off promiscuous mode as a result
525	 * of calling ether_ifdetach.
526	 */
527	ifp->if_softc = NULL;
528	/*
529	 * Stop the vap before detaching the ifnet.  Ideally we'd
530	 * do this in the other order so the ifnet is inaccessible
531	 * while we cleanup internal state but that is hard.
532	 */
533	ieee80211_stop_locked(vap);
534
535	/* XXX accumulate iv_stats in ic_stats? */
536	TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
537	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
538	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
539	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
540	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
541	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_HT);
542	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_USEHT40);
543	ieee80211_syncifflag_locked(ic, IFF_PROMISC);
544	ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
545	IEEE80211_UNLOCK(ic);
546
547	/* XXX can't hold com lock */
548	/* NB: bpfattach is called by ether_ifdetach and claims all taps */
549	ether_ifdetach(ifp);
550
551	ifmedia_removeall(&vap->iv_media);
552
553	ieee80211_regdomain_vdetach(vap);
554	ieee80211_scan_vdetach(vap);
555	ieee80211_ht_vdetach(vap);
556	/* NB: must be before ieee80211_node_vdetach */
557	ieee80211_proto_vdetach(vap);
558	ieee80211_crypto_vdetach(vap);
559	ieee80211_power_vdetach(vap);
560	ieee80211_node_vdetach(vap);
561	ieee80211_sysctl_vdetach(vap);
562
563	if_free(ifp);
564}
565
566/*
567 * Synchronize flag bit state in the parent ifnet structure
568 * according to the state of all vap ifnet's.  This is used,
569 * for example, to handle IFF_PROMISC and IFF_ALLMULTI.
570 */
571void
572ieee80211_syncifflag_locked(struct ieee80211com *ic, int flag)
573{
574	struct ifnet *ifp = ic->ic_ifp;
575	struct ieee80211vap *vap;
576	int bit, oflags;
577
578	IEEE80211_LOCK_ASSERT(ic);
579
580	bit = 0;
581	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
582		if (vap->iv_ifp->if_flags & flag) {
583			/*
584			 * XXX the bridge sets PROMISC but we don't want to
585			 * enable it on the device, discard here so all the
586			 * drivers don't need to special-case it
587			 */
588			if (flag == IFF_PROMISC &&
589			    vap->iv_opmode == IEEE80211_M_HOSTAP)
590				continue;
591			bit = 1;
592			break;
593		}
594	oflags = ifp->if_flags;
595	if (bit)
596		ifp->if_flags |= flag;
597	else
598		ifp->if_flags &= ~flag;
599	if ((ifp->if_flags ^ oflags) & flag) {
600		/* XXX should we return 1/0 and let caller do this? */
601		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
602			if (flag == IFF_PROMISC)
603				ic->ic_update_promisc(ifp);
604			else if (flag == IFF_ALLMULTI)
605				ic->ic_update_mcast(ifp);
606		}
607	}
608}
609
610/*
611 * Synchronize flag bit state in the com structure
612 * according to the state of all vap's.  This is used,
613 * for example, to handle state changes via ioctls.
614 */
615static void
616ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
617{
618	struct ieee80211vap *vap;
619	int bit;
620
621	IEEE80211_LOCK_ASSERT(ic);
622
623	bit = 0;
624	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
625		if (vap->iv_flags & flag) {
626			bit = 1;
627			break;
628		}
629	if (bit)
630		ic->ic_flags |= flag;
631	else
632		ic->ic_flags &= ~flag;
633}
634
635void
636ieee80211_syncflag(struct ieee80211vap *vap, int flag)
637{
638	struct ieee80211com *ic = vap->iv_ic;
639
640	IEEE80211_LOCK(ic);
641	if (flag < 0) {
642		flag = -flag;
643		vap->iv_flags &= ~flag;
644	} else
645		vap->iv_flags |= flag;
646	ieee80211_syncflag_locked(ic, flag);
647	IEEE80211_UNLOCK(ic);
648}
649
650/*
651 * Synchronize flag bit state in the com structure
652 * according to the state of all vap's.  This is used,
653 * for example, to handle state changes via ioctls.
654 */
655static void
656ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
657{
658	struct ieee80211vap *vap;
659	int bit;
660
661	IEEE80211_LOCK_ASSERT(ic);
662
663	bit = 0;
664	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
665		if (vap->iv_flags_ext & flag) {
666			bit = 1;
667			break;
668		}
669	if (bit)
670		ic->ic_flags_ext |= flag;
671	else
672		ic->ic_flags_ext &= ~flag;
673}
674
675void
676ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
677{
678	struct ieee80211com *ic = vap->iv_ic;
679
680	IEEE80211_LOCK(ic);
681	if (flag < 0) {
682		flag = -flag;
683		vap->iv_flags_ext &= ~flag;
684	} else
685		vap->iv_flags_ext |= flag;
686	ieee80211_syncflag_ext_locked(ic, flag);
687	IEEE80211_UNLOCK(ic);
688}
689
690static __inline int
691mapgsm(u_int freq, u_int flags)
692{
693	freq *= 10;
694	if (flags & IEEE80211_CHAN_QUARTER)
695		freq += 5;
696	else if (flags & IEEE80211_CHAN_HALF)
697		freq += 10;
698	else
699		freq += 20;
700	/* NB: there is no 907/20 wide but leave room */
701	return (freq - 906*10) / 5;
702}
703
704static __inline int
705mappsb(u_int freq, u_int flags)
706{
707	return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
708}
709
710/*
711 * Convert MHz frequency to IEEE channel number.
712 */
713int
714ieee80211_mhz2ieee(u_int freq, u_int flags)
715{
716#define	IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
717	if (flags & IEEE80211_CHAN_GSM)
718		return mapgsm(freq, flags);
719	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
720		if (freq == 2484)
721			return 14;
722		if (freq < 2484)
723			return ((int) freq - 2407) / 5;
724		else
725			return 15 + ((freq - 2512) / 20);
726	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
727		if (freq <= 5000) {
728			/* XXX check regdomain? */
729			if (IS_FREQ_IN_PSB(freq))
730				return mappsb(freq, flags);
731			return (freq - 4000) / 5;
732		} else
733			return (freq - 5000) / 5;
734	} else {				/* either, guess */
735		if (freq == 2484)
736			return 14;
737		if (freq < 2484) {
738			if (907 <= freq && freq <= 922)
739				return mapgsm(freq, flags);
740			return ((int) freq - 2407) / 5;
741		}
742		if (freq < 5000) {
743			if (IS_FREQ_IN_PSB(freq))
744				return mappsb(freq, flags);
745			else if (freq > 4900)
746				return (freq - 4000) / 5;
747			else
748				return 15 + ((freq - 2512) / 20);
749		}
750		return (freq - 5000) / 5;
751	}
752#undef IS_FREQ_IN_PSB
753}
754
755/*
756 * Convert channel to IEEE channel number.
757 */
758int
759ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
760{
761	if (c == NULL) {
762		if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
763		return 0;		/* XXX */
764	}
765	return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
766}
767
768/*
769 * Convert IEEE channel number to MHz frequency.
770 */
771u_int
772ieee80211_ieee2mhz(u_int chan, u_int flags)
773{
774	if (flags & IEEE80211_CHAN_GSM)
775		return 907 + 5 * (chan / 10);
776	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
777		if (chan == 14)
778			return 2484;
779		if (chan < 14)
780			return 2407 + chan*5;
781		else
782			return 2512 + ((chan-15)*20);
783	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
784		if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
785			chan -= 37;
786			return 4940 + chan*5 + (chan % 5 ? 2 : 0);
787		}
788		return 5000 + (chan*5);
789	} else {				/* either, guess */
790		/* XXX can't distinguish PSB+GSM channels */
791		if (chan == 14)
792			return 2484;
793		if (chan < 14)			/* 0-13 */
794			return 2407 + chan*5;
795		if (chan < 27)			/* 15-26 */
796			return 2512 + ((chan-15)*20);
797		return 5000 + (chan*5);
798	}
799}
800
801/*
802 * Locate a channel given a frequency+flags.  We cache
803 * the previous lookup to optimize switching between two
804 * channels--as happens with dynamic turbo.
805 */
806struct ieee80211_channel *
807ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
808{
809	struct ieee80211_channel *c;
810	int i;
811
812	flags &= IEEE80211_CHAN_ALLTURBO;
813	c = ic->ic_prevchan;
814	if (c != NULL && c->ic_freq == freq &&
815	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
816		return c;
817	/* brute force search */
818	for (i = 0; i < ic->ic_nchans; i++) {
819		c = &ic->ic_channels[i];
820		if (c->ic_freq == freq &&
821		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
822			return c;
823	}
824	return NULL;
825}
826
827/*
828 * Locate a channel given a channel number+flags.  We cache
829 * the previous lookup to optimize switching between two
830 * channels--as happens with dynamic turbo.
831 */
832struct ieee80211_channel *
833ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
834{
835	struct ieee80211_channel *c;
836	int i;
837
838	flags &= IEEE80211_CHAN_ALLTURBO;
839	c = ic->ic_prevchan;
840	if (c != NULL && c->ic_ieee == ieee &&
841	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
842		return c;
843	/* brute force search */
844	for (i = 0; i < ic->ic_nchans; i++) {
845		c = &ic->ic_channels[i];
846		if (c->ic_ieee == ieee &&
847		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
848			return c;
849	}
850	return NULL;
851}
852
853static void
854addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
855{
856#define	ADD(_ic, _s, _o) \
857	ifmedia_add(media, \
858		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
859	static const u_int mopts[IEEE80211_MODE_MAX] = {
860		IFM_AUTO,
861		IFM_IEEE80211_11A,
862		IFM_IEEE80211_11B,
863		IFM_IEEE80211_11G,
864		IFM_IEEE80211_FH,
865		IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
866		IFM_IEEE80211_11G | IFM_IEEE80211_TURBO,
867		IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
868		IFM_IEEE80211_11NA,
869		IFM_IEEE80211_11NG,
870	};
871	u_int mopt;
872
873	mopt = mopts[mode];
874	if (addsta)
875		ADD(ic, mword, mopt);	/* STA mode has no cap */
876	if (caps & IEEE80211_C_IBSS)
877		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
878	if (caps & IEEE80211_C_HOSTAP)
879		ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
880	if (caps & IEEE80211_C_AHDEMO)
881		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
882	if (caps & IEEE80211_C_MONITOR)
883		ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
884	if (caps & IEEE80211_C_WDS)
885		ADD(media, mword, mopt | IFM_IEEE80211_WDS);
886#undef ADD
887}
888
889/*
890 * Setup the media data structures according to the channel and
891 * rate tables.
892 */
893static int
894ieee80211_media_setup(struct ieee80211com *ic,
895	struct ifmedia *media, int caps, int addsta,
896	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
897{
898	int i, j, mode, rate, maxrate, mword, r;
899	const struct ieee80211_rateset *rs;
900	struct ieee80211_rateset allrates;
901
902	/*
903	 * Fill in media characteristics.
904	 */
905	ifmedia_init(media, 0, media_change, media_stat);
906	maxrate = 0;
907	/*
908	 * Add media for legacy operating modes.
909	 */
910	memset(&allrates, 0, sizeof(allrates));
911	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
912		if (isclr(ic->ic_modecaps, mode))
913			continue;
914		addmedia(media, caps, addsta, mode, IFM_AUTO);
915		if (mode == IEEE80211_MODE_AUTO)
916			continue;
917		rs = &ic->ic_sup_rates[mode];
918		for (i = 0; i < rs->rs_nrates; i++) {
919			rate = rs->rs_rates[i];
920			mword = ieee80211_rate2media(ic, rate, mode);
921			if (mword == 0)
922				continue;
923			addmedia(media, caps, addsta, mode, mword);
924			/*
925			 * Add legacy rate to the collection of all rates.
926			 */
927			r = rate & IEEE80211_RATE_VAL;
928			for (j = 0; j < allrates.rs_nrates; j++)
929				if (allrates.rs_rates[j] == r)
930					break;
931			if (j == allrates.rs_nrates) {
932				/* unique, add to the set */
933				allrates.rs_rates[j] = r;
934				allrates.rs_nrates++;
935			}
936			rate = (rate & IEEE80211_RATE_VAL) / 2;
937			if (rate > maxrate)
938				maxrate = rate;
939		}
940	}
941	for (i = 0; i < allrates.rs_nrates; i++) {
942		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
943				IEEE80211_MODE_AUTO);
944		if (mword == 0)
945			continue;
946		/* NB: remove media options from mword */
947		addmedia(media, caps, addsta,
948		    IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
949	}
950	/*
951	 * Add HT/11n media.  Note that we do not have enough
952	 * bits in the media subtype to express the MCS so we
953	 * use a "placeholder" media subtype and any fixed MCS
954	 * must be specified with a different mechanism.
955	 */
956	for (; mode < IEEE80211_MODE_MAX; mode++) {
957		if (isclr(ic->ic_modecaps, mode))
958			continue;
959		addmedia(media, caps, addsta, mode, IFM_AUTO);
960		addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
961	}
962	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
963	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
964		addmedia(media, caps, addsta,
965		    IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
966		/* XXX could walk htrates */
967		/* XXX known array size */
968		if (ieee80211_htrates[15].ht40_rate_400ns > maxrate)
969			maxrate = ieee80211_htrates[15].ht40_rate_400ns;
970	}
971	return maxrate;
972}
973
974void
975ieee80211_media_init(struct ieee80211com *ic)
976{
977	struct ifnet *ifp = ic->ic_ifp;
978	int maxrate;
979
980	/* NB: this works because the structure is initialized to zero */
981	if (!LIST_EMPTY(&ic->ic_media.ifm_list)) {
982		/*
983		 * We are re-initializing the channel list; clear
984		 * the existing media state as the media routines
985		 * don't suppress duplicates.
986		 */
987		ifmedia_removeall(&ic->ic_media);
988	}
989	ieee80211_chan_init(ic);
990
991	/*
992	 * Recalculate media settings in case new channel list changes
993	 * the set of available modes.
994	 */
995	maxrate = ieee80211_media_setup(ic, &ic->ic_media, ic->ic_caps, 1,
996		ieee80211com_media_change, ieee80211com_media_status);
997	/* NB: strip explicit mode; we're actually in autoselect */
998	ifmedia_set(&ic->ic_media,
999		media_status(ic->ic_opmode, ic->ic_curchan) &~ IFM_MMASK);
1000	if (maxrate)
1001		ifp->if_baudrate = IF_Mbps(maxrate);
1002
1003	/* XXX need to propagate new media settings to vap's */
1004}
1005
1006const struct ieee80211_rateset *
1007ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1008{
1009	if (IEEE80211_IS_CHAN_HALF(c))
1010		return &ieee80211_rateset_half;
1011	if (IEEE80211_IS_CHAN_QUARTER(c))
1012		return &ieee80211_rateset_quarter;
1013	if (IEEE80211_IS_CHAN_HTA(c))
1014		return &ic->ic_sup_rates[IEEE80211_MODE_11A];
1015	if (IEEE80211_IS_CHAN_HTG(c)) {
1016		/* XXX does this work for basic rates? */
1017		return &ic->ic_sup_rates[IEEE80211_MODE_11G];
1018	}
1019	return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1020}
1021
1022void
1023ieee80211_announce(struct ieee80211com *ic)
1024{
1025	struct ifnet *ifp = ic->ic_ifp;
1026	int i, mode, rate, mword;
1027	const struct ieee80211_rateset *rs;
1028
1029	/* NB: skip AUTO since it has no rates */
1030	for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1031		if (isclr(ic->ic_modecaps, mode))
1032			continue;
1033		if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
1034		rs = &ic->ic_sup_rates[mode];
1035		for (i = 0; i < rs->rs_nrates; i++) {
1036			mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1037			if (mword == 0)
1038				continue;
1039			rate = ieee80211_media2rate(mword);
1040			printf("%s%d%sMbps", (i != 0 ? " " : ""),
1041			    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1042		}
1043		printf("\n");
1044	}
1045	ieee80211_ht_announce(ic);
1046}
1047
1048void
1049ieee80211_announce_channels(struct ieee80211com *ic)
1050{
1051	const struct ieee80211_channel *c;
1052	char type;
1053	int i, cw;
1054
1055	printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
1056	for (i = 0; i < ic->ic_nchans; i++) {
1057		c = &ic->ic_channels[i];
1058		if (IEEE80211_IS_CHAN_ST(c))
1059			type = 'S';
1060		else if (IEEE80211_IS_CHAN_108A(c))
1061			type = 'T';
1062		else if (IEEE80211_IS_CHAN_108G(c))
1063			type = 'G';
1064		else if (IEEE80211_IS_CHAN_HT(c))
1065			type = 'n';
1066		else if (IEEE80211_IS_CHAN_A(c))
1067			type = 'a';
1068		else if (IEEE80211_IS_CHAN_ANYG(c))
1069			type = 'g';
1070		else if (IEEE80211_IS_CHAN_B(c))
1071			type = 'b';
1072		else
1073			type = 'f';
1074		if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1075			cw = 40;
1076		else if (IEEE80211_IS_CHAN_HALF(c))
1077			cw = 10;
1078		else if (IEEE80211_IS_CHAN_QUARTER(c))
1079			cw = 5;
1080		else
1081			cw = 20;
1082		printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
1083			, c->ic_ieee, c->ic_freq, type
1084			, cw
1085			, IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1086			  IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1087			, c->ic_maxregpower
1088			, c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1089			, c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1090		);
1091	}
1092}
1093
1094static int
1095media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1096{
1097	switch (IFM_MODE(ime->ifm_media)) {
1098	case IFM_IEEE80211_11A:
1099		*mode = IEEE80211_MODE_11A;
1100		break;
1101	case IFM_IEEE80211_11B:
1102		*mode = IEEE80211_MODE_11B;
1103		break;
1104	case IFM_IEEE80211_11G:
1105		*mode = IEEE80211_MODE_11G;
1106		break;
1107	case IFM_IEEE80211_FH:
1108		*mode = IEEE80211_MODE_FH;
1109		break;
1110	case IFM_IEEE80211_11NA:
1111		*mode = IEEE80211_MODE_11NA;
1112		break;
1113	case IFM_IEEE80211_11NG:
1114		*mode = IEEE80211_MODE_11NG;
1115		break;
1116	case IFM_AUTO:
1117		*mode = IEEE80211_MODE_AUTO;
1118		break;
1119	default:
1120		return 0;
1121	}
1122	/*
1123	 * Turbo mode is an ``option''.
1124	 * XXX does not apply to AUTO
1125	 */
1126	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1127		if (*mode == IEEE80211_MODE_11A) {
1128			if (flags & IEEE80211_F_TURBOP)
1129				*mode = IEEE80211_MODE_TURBO_A;
1130			else
1131				*mode = IEEE80211_MODE_STURBO_A;
1132		} else if (*mode == IEEE80211_MODE_11G)
1133			*mode = IEEE80211_MODE_TURBO_G;
1134		else
1135			return 0;
1136	}
1137	/* XXX HT40 +/- */
1138	return 1;
1139}
1140
1141/*
1142 * Handle a media change request on the underlying interface.
1143 */
1144int
1145ieee80211com_media_change(struct ifnet *ifp)
1146{
1147	return EINVAL;
1148}
1149
1150/*
1151 * Handle a media change request on the vap interface.
1152 */
1153int
1154ieee80211_media_change(struct ifnet *ifp)
1155{
1156	struct ieee80211vap *vap = ifp->if_softc;
1157	struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1158	uint16_t newmode;
1159
1160	if (!media2mode(ime, vap->iv_flags, &newmode))
1161		return EINVAL;
1162	if (vap->iv_des_mode != newmode) {
1163		vap->iv_des_mode = newmode;
1164		return ENETRESET;
1165	}
1166	return 0;
1167}
1168
1169/*
1170 * Common code to calculate the media status word
1171 * from the operating mode and channel state.
1172 */
1173static int
1174media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1175{
1176	int status;
1177
1178	status = IFM_IEEE80211;
1179	switch (opmode) {
1180	case IEEE80211_M_STA:
1181		break;
1182	case IEEE80211_M_IBSS:
1183		status |= IFM_IEEE80211_ADHOC;
1184		break;
1185	case IEEE80211_M_HOSTAP:
1186		status |= IFM_IEEE80211_HOSTAP;
1187		break;
1188	case IEEE80211_M_MONITOR:
1189		status |= IFM_IEEE80211_MONITOR;
1190		break;
1191	case IEEE80211_M_AHDEMO:
1192		status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1193		break;
1194	case IEEE80211_M_WDS:
1195		status |= IFM_IEEE80211_WDS;
1196		break;
1197	}
1198	if (IEEE80211_IS_CHAN_HTA(chan)) {
1199		status |= IFM_IEEE80211_11NA;
1200	} else if (IEEE80211_IS_CHAN_HTG(chan)) {
1201		status |= IFM_IEEE80211_11NG;
1202	} else if (IEEE80211_IS_CHAN_A(chan)) {
1203		status |= IFM_IEEE80211_11A;
1204	} else if (IEEE80211_IS_CHAN_B(chan)) {
1205		status |= IFM_IEEE80211_11B;
1206	} else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1207		status |= IFM_IEEE80211_11G;
1208	} else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1209		status |= IFM_IEEE80211_FH;
1210	}
1211	/* XXX else complain? */
1212
1213	if (IEEE80211_IS_CHAN_TURBO(chan))
1214		status |= IFM_IEEE80211_TURBO;
1215#if 0
1216	if (IEEE80211_IS_CHAN_HT20(chan))
1217		status |= IFM_IEEE80211_HT20;
1218	if (IEEE80211_IS_CHAN_HT40(chan))
1219		status |= IFM_IEEE80211_HT40;
1220#endif
1221	return status;
1222}
1223
1224static void
1225ieee80211com_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1226{
1227	struct ieee80211com *ic = ifp->if_l2com;
1228	struct ieee80211vap *vap;
1229
1230	imr->ifm_status = IFM_AVALID;
1231	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1232		if (vap->iv_ifp->if_flags & IFF_UP) {
1233			imr->ifm_status |= IFM_ACTIVE;
1234			break;
1235		}
1236	imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
1237	if (imr->ifm_status & IFM_ACTIVE)
1238		imr->ifm_current = imr->ifm_active;
1239}
1240
1241void
1242ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1243{
1244	struct ieee80211vap *vap = ifp->if_softc;
1245	struct ieee80211com *ic = vap->iv_ic;
1246	enum ieee80211_phymode mode;
1247
1248	imr->ifm_status = IFM_AVALID;
1249	/*
1250	 * NB: use the current channel's mode to lock down a xmit
1251	 * rate only when running; otherwise we may have a mismatch
1252	 * in which case the rate will not be convertible.
1253	 */
1254	if (vap->iv_state == IEEE80211_S_RUN) {
1255		imr->ifm_status |= IFM_ACTIVE;
1256		mode = ieee80211_chan2mode(ic->ic_curchan);
1257	} else
1258		mode = IEEE80211_MODE_AUTO;
1259	imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1260	/*
1261	 * Calculate a current rate if possible.
1262	 */
1263	if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1264		/*
1265		 * A fixed rate is set, report that.
1266		 */
1267		imr->ifm_active |= ieee80211_rate2media(ic,
1268			vap->iv_txparms[mode].ucastrate, mode);
1269	} else if (vap->iv_opmode == IEEE80211_M_STA) {
1270		/*
1271		 * In station mode report the current transmit rate.
1272		 */
1273		imr->ifm_active |= ieee80211_rate2media(ic,
1274			vap->iv_bss->ni_txrate, mode);
1275	} else
1276		imr->ifm_active |= IFM_AUTO;
1277	if (imr->ifm_status & IFM_ACTIVE)
1278		imr->ifm_current = imr->ifm_active;
1279}
1280
1281/*
1282 * Set the current phy mode and recalculate the active channel
1283 * set based on the available channels for this mode.  Also
1284 * select a new default/current channel if the current one is
1285 * inappropriate for this mode.
1286 */
1287int
1288ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1289{
1290	/*
1291	 * Adjust basic rates in 11b/11g supported rate set.
1292	 * Note that if operating on a hal/quarter rate channel
1293	 * this is a noop as those rates sets are different
1294	 * and used instead.
1295	 */
1296	if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1297		ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1298
1299	ic->ic_curmode = mode;
1300	ieee80211_reset_erp(ic);	/* reset ERP state */
1301
1302	return 0;
1303}
1304
1305/*
1306 * Return the phy mode for with the specified channel.
1307 */
1308enum ieee80211_phymode
1309ieee80211_chan2mode(const struct ieee80211_channel *chan)
1310{
1311
1312	if (IEEE80211_IS_CHAN_HTA(chan))
1313		return IEEE80211_MODE_11NA;
1314	else if (IEEE80211_IS_CHAN_HTG(chan))
1315		return IEEE80211_MODE_11NG;
1316	else if (IEEE80211_IS_CHAN_108G(chan))
1317		return IEEE80211_MODE_TURBO_G;
1318	else if (IEEE80211_IS_CHAN_ST(chan))
1319		return IEEE80211_MODE_STURBO_A;
1320	else if (IEEE80211_IS_CHAN_TURBO(chan))
1321		return IEEE80211_MODE_TURBO_A;
1322	else if (IEEE80211_IS_CHAN_A(chan))
1323		return IEEE80211_MODE_11A;
1324	else if (IEEE80211_IS_CHAN_ANYG(chan))
1325		return IEEE80211_MODE_11G;
1326	else if (IEEE80211_IS_CHAN_B(chan))
1327		return IEEE80211_MODE_11B;
1328	else if (IEEE80211_IS_CHAN_FHSS(chan))
1329		return IEEE80211_MODE_FH;
1330
1331	/* NB: should not get here */
1332	printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1333		__func__, chan->ic_freq, chan->ic_flags);
1334	return IEEE80211_MODE_11B;
1335}
1336
1337struct ratemedia {
1338	u_int	match;	/* rate + mode */
1339	u_int	media;	/* if_media rate */
1340};
1341
1342static int
1343findmedia(const struct ratemedia rates[], int n, u_int match)
1344{
1345	int i;
1346
1347	for (i = 0; i < n; i++)
1348		if (rates[i].match == match)
1349			return rates[i].media;
1350	return IFM_AUTO;
1351}
1352
1353/*
1354 * Convert IEEE80211 rate value to ifmedia subtype.
1355 * Rate is either a legacy rate in units of 0.5Mbps
1356 * or an MCS index.
1357 */
1358int
1359ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1360{
1361#define	N(a)	(sizeof(a) / sizeof(a[0]))
1362	static const struct ratemedia rates[] = {
1363		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1364		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1365		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1366		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1367		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1368		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1369		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1370		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1371		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1372		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1373		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1374		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1375		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1376		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1377		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1378		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1379		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1380		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1381		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1382		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1383		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1384		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1385		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1386		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1387		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1388		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1389		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1390		{   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1391		{   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1392		{  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1393		/* NB: OFDM72 doesn't realy exist so we don't handle it */
1394	};
1395	static const struct ratemedia htrates[] = {
1396		{   0, IFM_IEEE80211_MCS },
1397		{   1, IFM_IEEE80211_MCS },
1398		{   2, IFM_IEEE80211_MCS },
1399		{   3, IFM_IEEE80211_MCS },
1400		{   4, IFM_IEEE80211_MCS },
1401		{   5, IFM_IEEE80211_MCS },
1402		{   6, IFM_IEEE80211_MCS },
1403		{   7, IFM_IEEE80211_MCS },
1404		{   8, IFM_IEEE80211_MCS },
1405		{   9, IFM_IEEE80211_MCS },
1406		{  10, IFM_IEEE80211_MCS },
1407		{  11, IFM_IEEE80211_MCS },
1408		{  12, IFM_IEEE80211_MCS },
1409		{  13, IFM_IEEE80211_MCS },
1410		{  14, IFM_IEEE80211_MCS },
1411		{  15, IFM_IEEE80211_MCS },
1412	};
1413	int m;
1414
1415	/*
1416	 * Check 11n rates first for match as an MCS.
1417	 */
1418	if (mode == IEEE80211_MODE_11NA) {
1419		if (rate & IEEE80211_RATE_MCS) {
1420			rate &= ~IEEE80211_RATE_MCS;
1421			m = findmedia(htrates, N(htrates), rate);
1422			if (m != IFM_AUTO)
1423				return m | IFM_IEEE80211_11NA;
1424		}
1425	} else if (mode == IEEE80211_MODE_11NG) {
1426		/* NB: 12 is ambiguous, it will be treated as an MCS */
1427		if (rate & IEEE80211_RATE_MCS) {
1428			rate &= ~IEEE80211_RATE_MCS;
1429			m = findmedia(htrates, N(htrates), rate);
1430			if (m != IFM_AUTO)
1431				return m | IFM_IEEE80211_11NG;
1432		}
1433	}
1434	rate &= IEEE80211_RATE_VAL;
1435	switch (mode) {
1436	case IEEE80211_MODE_11A:
1437	case IEEE80211_MODE_11NA:
1438	case IEEE80211_MODE_TURBO_A:
1439	case IEEE80211_MODE_STURBO_A:
1440		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11A);
1441	case IEEE80211_MODE_11B:
1442		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11B);
1443	case IEEE80211_MODE_FH:
1444		return findmedia(rates, N(rates), rate | IFM_IEEE80211_FH);
1445	case IEEE80211_MODE_AUTO:
1446		/* NB: ic may be NULL for some drivers */
1447		if (ic && ic->ic_phytype == IEEE80211_T_FH)
1448			return findmedia(rates, N(rates),
1449			    rate | IFM_IEEE80211_FH);
1450		/* NB: hack, 11g matches both 11b+11a rates */
1451		/* fall thru... */
1452	case IEEE80211_MODE_11G:
1453	case IEEE80211_MODE_11NG:
1454	case IEEE80211_MODE_TURBO_G:
1455		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11G);
1456	}
1457	return IFM_AUTO;
1458#undef N
1459}
1460
1461int
1462ieee80211_media2rate(int mword)
1463{
1464#define	N(a)	(sizeof(a) / sizeof(a[0]))
1465	static const int ieeerates[] = {
1466		-1,		/* IFM_AUTO */
1467		0,		/* IFM_MANUAL */
1468		0,		/* IFM_NONE */
1469		2,		/* IFM_IEEE80211_FH1 */
1470		4,		/* IFM_IEEE80211_FH2 */
1471		2,		/* IFM_IEEE80211_DS1 */
1472		4,		/* IFM_IEEE80211_DS2 */
1473		11,		/* IFM_IEEE80211_DS5 */
1474		22,		/* IFM_IEEE80211_DS11 */
1475		44,		/* IFM_IEEE80211_DS22 */
1476		12,		/* IFM_IEEE80211_OFDM6 */
1477		18,		/* IFM_IEEE80211_OFDM9 */
1478		24,		/* IFM_IEEE80211_OFDM12 */
1479		36,		/* IFM_IEEE80211_OFDM18 */
1480		48,		/* IFM_IEEE80211_OFDM24 */
1481		72,		/* IFM_IEEE80211_OFDM36 */
1482		96,		/* IFM_IEEE80211_OFDM48 */
1483		108,		/* IFM_IEEE80211_OFDM54 */
1484		144,		/* IFM_IEEE80211_OFDM72 */
1485		0,		/* IFM_IEEE80211_DS354k */
1486		0,		/* IFM_IEEE80211_DS512k */
1487		6,		/* IFM_IEEE80211_OFDM3 */
1488		9,		/* IFM_IEEE80211_OFDM4 */
1489		54,		/* IFM_IEEE80211_OFDM27 */
1490		-1,		/* IFM_IEEE80211_MCS */
1491	};
1492	return IFM_SUBTYPE(mword) < N(ieeerates) ?
1493		ieeerates[IFM_SUBTYPE(mword)] : 0;
1494#undef N
1495}
1496