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