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