ieee80211.c revision 178694
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 178694 2008-04-30 16:05:57Z 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 0
383	/* XXX temp disable until we resolve regressions */
384	if (vap->iv_caps & IEEE80211_C_WME)
385		vap->iv_flags |= IEEE80211_F_WME;
386#endif
387	if (vap->iv_caps & IEEE80211_C_BURST)
388		vap->iv_flags |= IEEE80211_F_BURST;
389	if (vap->iv_caps & IEEE80211_C_FF)
390		vap->iv_flags |= IEEE80211_F_FF;
391	if (vap->iv_caps & IEEE80211_C_TURBOP)
392		vap->iv_flags |= IEEE80211_F_TURBOP;
393	/* NB: bg scanning only makes sense for station mode right now */
394	if (vap->iv_opmode == IEEE80211_M_STA &&
395	    (vap->iv_caps & IEEE80211_C_BGSCAN))
396		vap->iv_flags |= IEEE80211_F_BGSCAN;
397	vap->iv_flags |= IEEE80211_F_DOTH;	/* XXX out of caps, just ena */
398	/* XXX out of caps, just ena */
399	if (vap->iv_opmode == IEEE80211_M_HOSTAP)
400		vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
401
402	vap->iv_des_chan = IEEE80211_CHAN_ANYC;		/* any channel is ok */
403	vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
404	vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
405	/*
406	 * Install a default reset method for the ioctl support;
407	 * the driver can override this.
408	 */
409	vap->iv_reset = default_reset;
410
411	IEEE80211_ADDR_COPY(vap->iv_myaddr, macaddr);
412
413	ieee80211_sysctl_vattach(vap);
414	ieee80211_crypto_vattach(vap);
415	ieee80211_node_vattach(vap);
416	ieee80211_power_vattach(vap);
417	ieee80211_proto_vattach(vap);
418	ieee80211_ht_vattach(vap);
419	ieee80211_scan_vattach(vap);
420	ieee80211_regdomain_vattach(vap);
421
422	return 0;
423#undef IEEE80211_C_OPMODE
424}
425
426/*
427 * Activate a vap.  State should have been prepared with a
428 * call to ieee80211_vap_setup and by the driver.  On return
429 * from this call the vap is ready for use.
430 */
431int
432ieee80211_vap_attach(struct ieee80211vap *vap,
433	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
434{
435	struct ifnet *ifp = vap->iv_ifp;
436	struct ieee80211com *ic = vap->iv_ic;
437	struct ifmediareq imr;
438	int maxrate;
439
440	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
441	    "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
442	    __func__, ieee80211_opmode_name[vap->iv_opmode],
443	    ic->ic_ifp->if_xname, vap->iv_flags, vap->iv_flags_ext);
444
445	/*
446	 * Do late attach work that cannot happen until after
447	 * the driver has had a chance to override defaults.
448	 */
449	ieee80211_node_latevattach(vap);
450	ieee80211_power_latevattach(vap);
451
452	maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
453	    vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
454	ieee80211_media_status(ifp, &imr);
455	/* NB: strip explicit mode; we're actually in autoselect */
456	ifmedia_set(&vap->iv_media, imr.ifm_active &~ IFM_MMASK);
457	if (maxrate)
458		ifp->if_baudrate = IF_Mbps(maxrate);
459
460	ether_ifattach(ifp, vap->iv_myaddr);
461	/* hook output method setup by ether_ifattach */
462	vap->iv_output = ifp->if_output;
463	ifp->if_output = ieee80211_output;
464	/* NB: if_mtu set by ether_ifattach to ETHERMTU */
465	bpfattach2(ifp, DLT_IEEE802_11, ifp->if_hdrlen, &vap->iv_rawbpf);
466
467	IEEE80211_LOCK(ic);
468	TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
469	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
470	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
471	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
472	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
473	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_HT);
474	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_USEHT40);
475	ieee80211_syncifflag_locked(ic, IFF_PROMISC);
476	ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
477	IEEE80211_UNLOCK(ic);
478
479	return 1;
480}
481
482/*
483 * Tear down vap state and reclaim the ifnet.
484 * The driver is assumed to have prepared for
485 * this; e.g. by turning off interrupts for the
486 * underlying device.
487 */
488void
489ieee80211_vap_detach(struct ieee80211vap *vap)
490{
491	struct ieee80211com *ic = vap->iv_ic;
492	struct ifnet *ifp = vap->iv_ifp;
493
494	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
495	    __func__, ieee80211_opmode_name[vap->iv_opmode],
496	    ic->ic_ifp->if_xname);
497
498	IEEE80211_LOCK(ic);
499	/* block traffic from above */
500	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
501	/*
502	 * Evil hack.  Clear the backpointer from the ifnet to the
503	 * vap so any requests from above will return an error or
504	 * be ignored.  In particular this short-circuits requests
505	 * by the bridge to turn off promiscuous mode as a result
506	 * of calling ether_ifdetach.
507	 */
508	ifp->if_softc = NULL;
509	/*
510	 * Stop the vap before detaching the ifnet.  Ideally we'd
511	 * do this in the other order so the ifnet is inaccessible
512	 * while we cleanup internal state but that is hard.
513	 */
514	ieee80211_stop_locked(vap);
515
516	/* XXX accumulate iv_stats in ic_stats? */
517	TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
518	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
519	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
520	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
521	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
522	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_HT);
523	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_USEHT40);
524	ieee80211_syncifflag_locked(ic, IFF_PROMISC);
525	ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
526	IEEE80211_UNLOCK(ic);
527
528	/* XXX can't hold com lock */
529	/* NB: bpfattach is called by ether_ifdetach and claims all taps */
530	ether_ifdetach(ifp);
531
532	ifmedia_removeall(&vap->iv_media);
533
534	ieee80211_regdomain_vdetach(vap);
535	ieee80211_scan_vdetach(vap);
536	ieee80211_ht_vdetach(vap);
537	/* NB: must be before ieee80211_node_vdetach */
538	ieee80211_proto_vdetach(vap);
539	ieee80211_crypto_vdetach(vap);
540	ieee80211_power_vdetach(vap);
541	ieee80211_node_vdetach(vap);
542	ieee80211_sysctl_vdetach(vap);
543}
544
545/*
546 * Synchronize flag bit state in the parent ifnet structure
547 * according to the state of all vap ifnet's.  This is used,
548 * for example, to handle IFF_PROMISC and IFF_ALLMULTI.
549 */
550void
551ieee80211_syncifflag_locked(struct ieee80211com *ic, int flag)
552{
553	struct ifnet *ifp = ic->ic_ifp;
554	struct ieee80211vap *vap;
555	int bit, oflags;
556
557	IEEE80211_LOCK_ASSERT(ic);
558
559	bit = 0;
560	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
561		if (vap->iv_ifp->if_flags & flag) {
562			/*
563			 * XXX the bridge sets PROMISC but we don't want to
564			 * enable it on the device, discard here so all the
565			 * drivers don't need to special-case it
566			 */
567			if (flag == IFF_PROMISC &&
568			    vap->iv_opmode == IEEE80211_M_HOSTAP)
569				continue;
570			bit = 1;
571			break;
572		}
573	oflags = ifp->if_flags;
574	if (bit)
575		ifp->if_flags |= flag;
576	else
577		ifp->if_flags &= ~flag;
578	if ((ifp->if_flags ^ oflags) & flag) {
579		/* XXX should we return 1/0 and let caller do this? */
580		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
581			if (flag == IFF_PROMISC)
582				ic->ic_update_promisc(ifp);
583			else if (flag == IFF_ALLMULTI)
584				ic->ic_update_mcast(ifp);
585		}
586	}
587}
588
589/*
590 * Synchronize flag bit state in the com structure
591 * according to the state of all vap's.  This is used,
592 * for example, to handle state changes via ioctls.
593 */
594static void
595ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
596{
597	struct ieee80211vap *vap;
598	int bit;
599
600	IEEE80211_LOCK_ASSERT(ic);
601
602	bit = 0;
603	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
604		if (vap->iv_flags & flag) {
605			bit = 1;
606			break;
607		}
608	if (bit)
609		ic->ic_flags |= flag;
610	else
611		ic->ic_flags &= ~flag;
612}
613
614void
615ieee80211_syncflag(struct ieee80211vap *vap, int flag)
616{
617	struct ieee80211com *ic = vap->iv_ic;
618
619	IEEE80211_LOCK(ic);
620	if (flag < 0) {
621		flag = -flag;
622		vap->iv_flags &= ~flag;
623	} else
624		vap->iv_flags |= flag;
625	ieee80211_syncflag_locked(ic, flag);
626	IEEE80211_UNLOCK(ic);
627}
628
629/*
630 * Synchronize flag bit state in the com structure
631 * according to the state of all vap's.  This is used,
632 * for example, to handle state changes via ioctls.
633 */
634static void
635ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
636{
637	struct ieee80211vap *vap;
638	int bit;
639
640	IEEE80211_LOCK_ASSERT(ic);
641
642	bit = 0;
643	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
644		if (vap->iv_flags_ext & flag) {
645			bit = 1;
646			break;
647		}
648	if (bit)
649		ic->ic_flags_ext |= flag;
650	else
651		ic->ic_flags_ext &= ~flag;
652}
653
654void
655ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
656{
657	struct ieee80211com *ic = vap->iv_ic;
658
659	IEEE80211_LOCK(ic);
660	if (flag < 0) {
661		flag = -flag;
662		vap->iv_flags_ext &= ~flag;
663	} else
664		vap->iv_flags_ext |= flag;
665	ieee80211_syncflag_ext_locked(ic, flag);
666	IEEE80211_UNLOCK(ic);
667}
668
669static __inline int
670mapgsm(u_int freq, u_int flags)
671{
672	freq *= 10;
673	if (flags & IEEE80211_CHAN_QUARTER)
674		freq += 5;
675	else if (flags & IEEE80211_CHAN_HALF)
676		freq += 10;
677	else
678		freq += 20;
679	/* NB: there is no 907/20 wide but leave room */
680	return (freq - 906*10) / 5;
681}
682
683static __inline int
684mappsb(u_int freq, u_int flags)
685{
686	return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
687}
688
689/*
690 * Convert MHz frequency to IEEE channel number.
691 */
692int
693ieee80211_mhz2ieee(u_int freq, u_int flags)
694{
695#define	IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
696	if (flags & IEEE80211_CHAN_GSM)
697		return mapgsm(freq, flags);
698	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
699		if (freq == 2484)
700			return 14;
701		if (freq < 2484)
702			return ((int) freq - 2407) / 5;
703		else
704			return 15 + ((freq - 2512) / 20);
705	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
706		if (freq <= 5000) {
707			/* XXX check regdomain? */
708			if (IS_FREQ_IN_PSB(freq))
709				return mappsb(freq, flags);
710			return (freq - 4000) / 5;
711		} else
712			return (freq - 5000) / 5;
713	} else {				/* either, guess */
714		if (freq == 2484)
715			return 14;
716		if (freq < 2484) {
717			if (907 <= freq && freq <= 922)
718				return mapgsm(freq, flags);
719			return ((int) freq - 2407) / 5;
720		}
721		if (freq < 5000) {
722			if (IS_FREQ_IN_PSB(freq))
723				return mappsb(freq, flags);
724			else if (freq > 4900)
725				return (freq - 4000) / 5;
726			else
727				return 15 + ((freq - 2512) / 20);
728		}
729		return (freq - 5000) / 5;
730	}
731#undef IS_FREQ_IN_PSB
732}
733
734/*
735 * Convert channel to IEEE channel number.
736 */
737int
738ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
739{
740	if (c == NULL) {
741		if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
742		return 0;		/* XXX */
743	}
744	return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
745}
746
747/*
748 * Convert IEEE channel number to MHz frequency.
749 */
750u_int
751ieee80211_ieee2mhz(u_int chan, u_int flags)
752{
753	if (flags & IEEE80211_CHAN_GSM)
754		return 907 + 5 * (chan / 10);
755	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
756		if (chan == 14)
757			return 2484;
758		if (chan < 14)
759			return 2407 + chan*5;
760		else
761			return 2512 + ((chan-15)*20);
762	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
763		if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
764			chan -= 37;
765			return 4940 + chan*5 + (chan % 5 ? 2 : 0);
766		}
767		return 5000 + (chan*5);
768	} else {				/* either, guess */
769		/* XXX can't distinguish PSB+GSM channels */
770		if (chan == 14)
771			return 2484;
772		if (chan < 14)			/* 0-13 */
773			return 2407 + chan*5;
774		if (chan < 27)			/* 15-26 */
775			return 2512 + ((chan-15)*20);
776		return 5000 + (chan*5);
777	}
778}
779
780/*
781 * Locate a channel given a frequency+flags.  We cache
782 * the previous lookup to optimize switching between two
783 * channels--as happens with dynamic turbo.
784 */
785struct ieee80211_channel *
786ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
787{
788	struct ieee80211_channel *c;
789	int i;
790
791	flags &= IEEE80211_CHAN_ALLTURBO;
792	c = ic->ic_prevchan;
793	if (c != NULL && c->ic_freq == freq &&
794	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
795		return c;
796	/* brute force search */
797	for (i = 0; i < ic->ic_nchans; i++) {
798		c = &ic->ic_channels[i];
799		if (c->ic_freq == freq &&
800		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
801			return c;
802	}
803	return NULL;
804}
805
806/*
807 * Locate a channel given a channel number+flags.  We cache
808 * the previous lookup to optimize switching between two
809 * channels--as happens with dynamic turbo.
810 */
811struct ieee80211_channel *
812ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
813{
814	struct ieee80211_channel *c;
815	int i;
816
817	flags &= IEEE80211_CHAN_ALLTURBO;
818	c = ic->ic_prevchan;
819	if (c != NULL && c->ic_ieee == ieee &&
820	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
821		return c;
822	/* brute force search */
823	for (i = 0; i < ic->ic_nchans; i++) {
824		c = &ic->ic_channels[i];
825		if (c->ic_ieee == ieee &&
826		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
827			return c;
828	}
829	return NULL;
830}
831
832static void
833addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
834{
835#define	ADD(_ic, _s, _o) \
836	ifmedia_add(media, \
837		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
838	static const u_int mopts[IEEE80211_MODE_MAX] = {
839		IFM_AUTO,
840		IFM_IEEE80211_11A,
841		IFM_IEEE80211_11B,
842		IFM_IEEE80211_11G,
843		IFM_IEEE80211_FH,
844		IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
845		IFM_IEEE80211_11G | IFM_IEEE80211_TURBO,
846		IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
847		IFM_IEEE80211_11NA,
848		IFM_IEEE80211_11NG,
849	};
850	u_int mopt;
851
852	mopt = mopts[mode];
853	if (addsta)
854		ADD(ic, mword, mopt);	/* STA mode has no cap */
855	if (caps & IEEE80211_C_IBSS)
856		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
857	if (caps & IEEE80211_C_HOSTAP)
858		ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
859	if (caps & IEEE80211_C_AHDEMO)
860		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
861	if (caps & IEEE80211_C_MONITOR)
862		ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
863	if (caps & IEEE80211_C_WDS)
864		ADD(media, mword, mopt | IFM_IEEE80211_WDS);
865#undef ADD
866}
867
868/*
869 * Setup the media data structures according to the channel and
870 * rate tables.
871 */
872static int
873ieee80211_media_setup(struct ieee80211com *ic,
874	struct ifmedia *media, int caps, int addsta,
875	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
876{
877	int i, j, mode, rate, maxrate, mword, r;
878	const struct ieee80211_rateset *rs;
879	struct ieee80211_rateset allrates;
880
881	/*
882	 * Fill in media characteristics.
883	 */
884	ifmedia_init(media, 0, media_change, media_stat);
885	maxrate = 0;
886	/*
887	 * Add media for legacy operating modes.
888	 */
889	memset(&allrates, 0, sizeof(allrates));
890	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
891		if (isclr(ic->ic_modecaps, mode))
892			continue;
893		addmedia(media, caps, addsta, mode, IFM_AUTO);
894		if (mode == IEEE80211_MODE_AUTO)
895			continue;
896		rs = &ic->ic_sup_rates[mode];
897		for (i = 0; i < rs->rs_nrates; i++) {
898			rate = rs->rs_rates[i];
899			mword = ieee80211_rate2media(ic, rate, mode);
900			if (mword == 0)
901				continue;
902			addmedia(media, caps, addsta, mode, mword);
903			/*
904			 * Add legacy rate to the collection of all rates.
905			 */
906			r = rate & IEEE80211_RATE_VAL;
907			for (j = 0; j < allrates.rs_nrates; j++)
908				if (allrates.rs_rates[j] == r)
909					break;
910			if (j == allrates.rs_nrates) {
911				/* unique, add to the set */
912				allrates.rs_rates[j] = r;
913				allrates.rs_nrates++;
914			}
915			rate = (rate & IEEE80211_RATE_VAL) / 2;
916			if (rate > maxrate)
917				maxrate = rate;
918		}
919	}
920	for (i = 0; i < allrates.rs_nrates; i++) {
921		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
922				IEEE80211_MODE_AUTO);
923		if (mword == 0)
924			continue;
925		/* NB: remove media options from mword */
926		addmedia(media, caps, addsta,
927		    IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
928	}
929	/*
930	 * Add HT/11n media.  Note that we do not have enough
931	 * bits in the media subtype to express the MCS so we
932	 * use a "placeholder" media subtype and any fixed MCS
933	 * must be specified with a different mechanism.
934	 */
935	for (; mode < IEEE80211_MODE_MAX; mode++) {
936		if (isclr(ic->ic_modecaps, mode))
937			continue;
938		addmedia(media, caps, addsta, mode, IFM_AUTO);
939		addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
940	}
941	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
942	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
943		addmedia(media, caps, addsta,
944		    IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
945		/* XXX could walk htrates */
946		/* XXX known array size */
947		if (ieee80211_htrates[15].ht40_rate_400ns > maxrate)
948			maxrate = ieee80211_htrates[15].ht40_rate_400ns;
949	}
950	return maxrate;
951}
952
953void
954ieee80211_media_init(struct ieee80211com *ic)
955{
956	struct ifnet *ifp = ic->ic_ifp;
957	int maxrate;
958
959	/* NB: this works because the structure is initialized to zero */
960	if (!LIST_EMPTY(&ic->ic_media.ifm_list)) {
961		/*
962		 * We are re-initializing the channel list; clear
963		 * the existing media state as the media routines
964		 * don't suppress duplicates.
965		 */
966		ifmedia_removeall(&ic->ic_media);
967	}
968	ieee80211_chan_init(ic);
969
970	/*
971	 * Recalculate media settings in case new channel list changes
972	 * the set of available modes.
973	 */
974	maxrate = ieee80211_media_setup(ic, &ic->ic_media, ic->ic_caps, 1,
975		ieee80211com_media_change, ieee80211com_media_status);
976	/* NB: strip explicit mode; we're actually in autoselect */
977	ifmedia_set(&ic->ic_media,
978		media_status(ic->ic_opmode, ic->ic_curchan) &~ IFM_MMASK);
979	if (maxrate)
980		ifp->if_baudrate = IF_Mbps(maxrate);
981
982	/* XXX need to propagate new media settings to vap's */
983}
984
985const struct ieee80211_rateset *
986ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
987{
988	if (IEEE80211_IS_CHAN_HALF(c))
989		return &ieee80211_rateset_half;
990	if (IEEE80211_IS_CHAN_QUARTER(c))
991		return &ieee80211_rateset_quarter;
992	if (IEEE80211_IS_CHAN_HTA(c))
993		return &ic->ic_sup_rates[IEEE80211_MODE_11A];
994	if (IEEE80211_IS_CHAN_HTG(c)) {
995		/* XXX does this work for basic rates? */
996		return &ic->ic_sup_rates[IEEE80211_MODE_11G];
997	}
998	return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
999}
1000
1001void
1002ieee80211_announce(struct ieee80211com *ic)
1003{
1004	struct ifnet *ifp = ic->ic_ifp;
1005	int i, mode, rate, mword;
1006	const struct ieee80211_rateset *rs;
1007
1008	/* NB: skip AUTO since it has no rates */
1009	for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1010		if (isclr(ic->ic_modecaps, mode))
1011			continue;
1012		if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
1013		rs = &ic->ic_sup_rates[mode];
1014		for (i = 0; i < rs->rs_nrates; i++) {
1015			mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1016			if (mword == 0)
1017				continue;
1018			rate = ieee80211_media2rate(mword);
1019			printf("%s%d%sMbps", (i != 0 ? " " : ""),
1020			    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1021		}
1022		printf("\n");
1023	}
1024	ieee80211_ht_announce(ic);
1025}
1026
1027void
1028ieee80211_announce_channels(struct ieee80211com *ic)
1029{
1030	const struct ieee80211_channel *c;
1031	char type;
1032	int i, cw;
1033
1034	printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
1035	for (i = 0; i < ic->ic_nchans; i++) {
1036		c = &ic->ic_channels[i];
1037		if (IEEE80211_IS_CHAN_ST(c))
1038			type = 'S';
1039		else if (IEEE80211_IS_CHAN_108A(c))
1040			type = 'T';
1041		else if (IEEE80211_IS_CHAN_108G(c))
1042			type = 'G';
1043		else if (IEEE80211_IS_CHAN_HT(c))
1044			type = 'n';
1045		else if (IEEE80211_IS_CHAN_A(c))
1046			type = 'a';
1047		else if (IEEE80211_IS_CHAN_ANYG(c))
1048			type = 'g';
1049		else if (IEEE80211_IS_CHAN_B(c))
1050			type = 'b';
1051		else
1052			type = 'f';
1053		if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1054			cw = 40;
1055		else if (IEEE80211_IS_CHAN_HALF(c))
1056			cw = 10;
1057		else if (IEEE80211_IS_CHAN_QUARTER(c))
1058			cw = 5;
1059		else
1060			cw = 20;
1061		printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
1062			, c->ic_ieee, c->ic_freq, type
1063			, cw
1064			, IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1065			  IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1066			, c->ic_maxregpower
1067			, c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1068			, c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1069		);
1070	}
1071}
1072
1073static int
1074media2mode(const struct ieee80211com *ic,
1075	const struct ifmedia_entry *ime, enum ieee80211_phymode *mode)
1076{
1077	switch (IFM_MODE(ime->ifm_media)) {
1078	case IFM_IEEE80211_11A:
1079		*mode = IEEE80211_MODE_11A;
1080		break;
1081	case IFM_IEEE80211_11B:
1082		*mode = IEEE80211_MODE_11B;
1083		break;
1084	case IFM_IEEE80211_11G:
1085		*mode = IEEE80211_MODE_11G;
1086		break;
1087	case IFM_IEEE80211_FH:
1088		*mode = IEEE80211_MODE_FH;
1089		break;
1090	case IFM_IEEE80211_11NA:
1091		*mode = IEEE80211_MODE_11NA;
1092		break;
1093	case IFM_IEEE80211_11NG:
1094		*mode = IEEE80211_MODE_11NG;
1095		break;
1096	case IFM_AUTO:
1097		*mode = IEEE80211_MODE_AUTO;
1098		break;
1099	default:
1100		return 0;
1101	}
1102	/*
1103	 * Turbo mode is an ``option''.
1104	 * XXX does not apply to AUTO
1105	 */
1106	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1107		if (*mode == IEEE80211_MODE_11A) {
1108			if (ic->ic_flags & IEEE80211_F_TURBOP)
1109				*mode = IEEE80211_MODE_TURBO_A;
1110			else
1111				*mode = IEEE80211_MODE_STURBO_A;
1112		} else if (*mode == IEEE80211_MODE_11G)
1113			*mode = IEEE80211_MODE_TURBO_G;
1114		else
1115			return 0;
1116	}
1117	/* XXX HT40 +/- */
1118	return 1;
1119}
1120
1121/*
1122 * Handle a media change request on the underlying
1123 * interface; we accept mode changes only.
1124 */
1125int
1126ieee80211com_media_change(struct ifnet *ifp)
1127{
1128	struct ieee80211com *ic = ifp->if_l2com;
1129	struct ifmedia_entry *ime = ic->ic_media.ifm_cur;
1130	enum ieee80211_phymode newphymode;
1131	int error = 0;
1132
1133	/*
1134	 * First, identify the phy mode.
1135	 */
1136	if (!media2mode(ic, ime, &newphymode))
1137		return EINVAL;
1138	/* NB: mode must be supported, no need to check */
1139
1140	/*
1141	 * Handle phy mode change.
1142	 */
1143	IEEE80211_LOCK(ic);
1144	if (ic->ic_curmode != newphymode) {		/* change phy mode */
1145		struct ieee80211vap *vap;
1146
1147		(void) ieee80211_setmode(ic, newphymode);
1148		/*
1149		 * Propagate new state to each vap.
1150		 */
1151		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1152		}
1153	}
1154	IEEE80211_UNLOCK(ic);
1155	return error;
1156}
1157
1158static int
1159findrate(const struct ieee80211com *ic, enum ieee80211_phymode m, int r)
1160{
1161	int i, nrates;
1162
1163	for (i = 0, nrates = ic->ic_sup_rates[m].rs_nrates; i < nrates; i++)
1164		if ((ic->ic_sup_rates[m].rs_rates[i] & IEEE80211_RATE_VAL) == r)
1165			return i;
1166	return -1;
1167}
1168
1169/*
1170 * Handle a media change request on the vap interface.
1171 */
1172int
1173ieee80211_media_change(struct ifnet *ifp)
1174{
1175	struct ieee80211vap *vap = ifp->if_softc;
1176	struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1177	struct ieee80211com *ic = vap->iv_ic;
1178	int newrate;
1179
1180	/* XXX this won't work unless ic_curmode is != IEEE80211_MODE_AUTO */
1181	if (ic->ic_curmode == IEEE80211_MODE_AUTO)
1182		return EINVAL;
1183	if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
1184		/*
1185		 * NB: this can only be used to specify a legacy rate.
1186		 */
1187		newrate = ieee80211_media2rate(ime->ifm_media);
1188		if (newrate == 0)
1189			return EINVAL;
1190		if (findrate(ic, ic->ic_curmode, newrate) == -1)
1191			return EINVAL;
1192	} else {
1193		newrate = IEEE80211_FIXED_RATE_NONE;
1194	}
1195	if (newrate != vap->iv_txparms[ic->ic_curmode].ucastrate) {
1196		vap->iv_txparms[ic->ic_curmode].ucastrate = newrate;
1197		return ENETRESET;
1198	}
1199	return 0;
1200}
1201
1202/*
1203 * Common code to calculate the media status word
1204 * from the operating mode and channel state.
1205 */
1206static int
1207media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1208{
1209	int status;
1210
1211	status = IFM_IEEE80211;
1212	switch (opmode) {
1213	case IEEE80211_M_STA:
1214		break;
1215	case IEEE80211_M_IBSS:
1216		status |= IFM_IEEE80211_ADHOC;
1217		break;
1218	case IEEE80211_M_HOSTAP:
1219		status |= IFM_IEEE80211_HOSTAP;
1220		break;
1221	case IEEE80211_M_MONITOR:
1222		status |= IFM_IEEE80211_MONITOR;
1223		break;
1224	case IEEE80211_M_AHDEMO:
1225		status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1226		break;
1227	case IEEE80211_M_WDS:
1228		status |= IFM_IEEE80211_WDS;
1229		break;
1230	}
1231	if (IEEE80211_IS_CHAN_HTA(chan)) {
1232		status |= IFM_IEEE80211_11NA;
1233	} else if (IEEE80211_IS_CHAN_HTG(chan)) {
1234		status |= IFM_IEEE80211_11NG;
1235	} else if (IEEE80211_IS_CHAN_A(chan)) {
1236		status |= IFM_IEEE80211_11A;
1237	} else if (IEEE80211_IS_CHAN_B(chan)) {
1238		status |= IFM_IEEE80211_11B;
1239	} else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1240		status |= IFM_IEEE80211_11G;
1241	} else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1242		status |= IFM_IEEE80211_FH;
1243	}
1244	/* XXX else complain? */
1245
1246	if (IEEE80211_IS_CHAN_TURBO(chan))
1247		status |= IFM_IEEE80211_TURBO;
1248#if 0
1249	if (IEEE80211_IS_CHAN_HT20(chan))
1250		status |= IFM_IEEE80211_HT20;
1251	if (IEEE80211_IS_CHAN_HT40(chan))
1252		status |= IFM_IEEE80211_HT40;
1253#endif
1254	return status;
1255}
1256
1257static void
1258ieee80211com_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1259{
1260	struct ieee80211com *ic = ifp->if_l2com;
1261	struct ieee80211vap *vap;
1262
1263	imr->ifm_status = IFM_AVALID;
1264	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1265		if (vap->iv_ifp->if_flags & IFF_UP) {
1266			imr->ifm_status |= IFM_ACTIVE;
1267			break;
1268		}
1269	imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
1270	if (imr->ifm_status & IFM_ACTIVE)
1271		imr->ifm_current = imr->ifm_active;
1272}
1273
1274void
1275ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1276{
1277	struct ieee80211vap *vap = ifp->if_softc;
1278	struct ieee80211com *ic = vap->iv_ic;
1279	enum ieee80211_phymode mode;
1280
1281	imr->ifm_status = IFM_AVALID;
1282	/*
1283	 * NB: use the current channel's mode to lock down a xmit
1284	 * rate only when running; otherwise we may have a mismatch
1285	 * in which case the rate will not be convertible.
1286	 */
1287	if (vap->iv_state == IEEE80211_S_RUN) {
1288		imr->ifm_status |= IFM_ACTIVE;
1289		mode = ieee80211_chan2mode(ic->ic_curchan);
1290	} else
1291		mode = IEEE80211_MODE_AUTO;
1292	imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1293	/*
1294	 * Calculate a current rate if possible.
1295	 */
1296	if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1297		/*
1298		 * A fixed rate is set, report that.
1299		 */
1300		imr->ifm_active |= ieee80211_rate2media(ic,
1301			vap->iv_txparms[mode].ucastrate, mode);
1302	} else if (vap->iv_opmode == IEEE80211_M_STA) {
1303		/*
1304		 * In station mode report the current transmit rate.
1305		 */
1306		imr->ifm_active |= ieee80211_rate2media(ic,
1307			vap->iv_bss->ni_txrate, mode);
1308	} else
1309		imr->ifm_active |= IFM_AUTO;
1310	if (imr->ifm_status & IFM_ACTIVE)
1311		imr->ifm_current = imr->ifm_active;
1312}
1313
1314/*
1315 * Set the current phy mode and recalculate the active channel
1316 * set based on the available channels for this mode.  Also
1317 * select a new default/current channel if the current one is
1318 * inappropriate for this mode.
1319 */
1320int
1321ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1322{
1323	/*
1324	 * Adjust basic rates in 11b/11g supported rate set.
1325	 * Note that if operating on a hal/quarter rate channel
1326	 * this is a noop as those rates sets are different
1327	 * and used instead.
1328	 */
1329	if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1330		ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1331
1332	ic->ic_curmode = mode;
1333	ieee80211_reset_erp(ic);	/* reset ERP state */
1334
1335	return 0;
1336}
1337
1338/*
1339 * Return the phy mode for with the specified channel.
1340 */
1341enum ieee80211_phymode
1342ieee80211_chan2mode(const struct ieee80211_channel *chan)
1343{
1344
1345	if (IEEE80211_IS_CHAN_HTA(chan))
1346		return IEEE80211_MODE_11NA;
1347	else if (IEEE80211_IS_CHAN_HTG(chan))
1348		return IEEE80211_MODE_11NG;
1349	else if (IEEE80211_IS_CHAN_108G(chan))
1350		return IEEE80211_MODE_TURBO_G;
1351	else if (IEEE80211_IS_CHAN_ST(chan))
1352		return IEEE80211_MODE_STURBO_A;
1353	else if (IEEE80211_IS_CHAN_TURBO(chan))
1354		return IEEE80211_MODE_TURBO_A;
1355	else if (IEEE80211_IS_CHAN_A(chan))
1356		return IEEE80211_MODE_11A;
1357	else if (IEEE80211_IS_CHAN_ANYG(chan))
1358		return IEEE80211_MODE_11G;
1359	else if (IEEE80211_IS_CHAN_B(chan))
1360		return IEEE80211_MODE_11B;
1361	else if (IEEE80211_IS_CHAN_FHSS(chan))
1362		return IEEE80211_MODE_FH;
1363
1364	/* NB: should not get here */
1365	printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1366		__func__, chan->ic_freq, chan->ic_flags);
1367	return IEEE80211_MODE_11B;
1368}
1369
1370struct ratemedia {
1371	u_int	match;	/* rate + mode */
1372	u_int	media;	/* if_media rate */
1373};
1374
1375static int
1376findmedia(const struct ratemedia rates[], int n, u_int match)
1377{
1378	int i;
1379
1380	for (i = 0; i < n; i++)
1381		if (rates[i].match == match)
1382			return rates[i].media;
1383	return IFM_AUTO;
1384}
1385
1386/*
1387 * Convert IEEE80211 rate value to ifmedia subtype.
1388 * Rate is either a legacy rate in units of 0.5Mbps
1389 * or an MCS index.
1390 */
1391int
1392ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1393{
1394#define	N(a)	(sizeof(a) / sizeof(a[0]))
1395	static const struct ratemedia rates[] = {
1396		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1397		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1398		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1399		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1400		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1401		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1402		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1403		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1404		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1405		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1406		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1407		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1408		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1409		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1410		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1411		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1412		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1413		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1414		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1415		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1416		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1417		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1418		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1419		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1420		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1421		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1422		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1423		{   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1424		{   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1425		{  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1426		/* NB: OFDM72 doesn't realy exist so we don't handle it */
1427	};
1428	static const struct ratemedia htrates[] = {
1429		{   0, IFM_IEEE80211_MCS },
1430		{   1, IFM_IEEE80211_MCS },
1431		{   2, IFM_IEEE80211_MCS },
1432		{   3, IFM_IEEE80211_MCS },
1433		{   4, IFM_IEEE80211_MCS },
1434		{   5, IFM_IEEE80211_MCS },
1435		{   6, IFM_IEEE80211_MCS },
1436		{   7, IFM_IEEE80211_MCS },
1437		{   8, IFM_IEEE80211_MCS },
1438		{   9, IFM_IEEE80211_MCS },
1439		{  10, IFM_IEEE80211_MCS },
1440		{  11, IFM_IEEE80211_MCS },
1441		{  12, IFM_IEEE80211_MCS },
1442		{  13, IFM_IEEE80211_MCS },
1443		{  14, IFM_IEEE80211_MCS },
1444		{  15, IFM_IEEE80211_MCS },
1445	};
1446	int m;
1447
1448	/*
1449	 * Check 11n rates first for match as an MCS.
1450	 */
1451	if (mode == IEEE80211_MODE_11NA) {
1452		if (rate & IEEE80211_RATE_MCS) {
1453			rate &= ~IEEE80211_RATE_MCS;
1454			m = findmedia(htrates, N(htrates), rate);
1455			if (m != IFM_AUTO)
1456				return m | IFM_IEEE80211_11NA;
1457		}
1458	} else if (mode == IEEE80211_MODE_11NG) {
1459		/* NB: 12 is ambiguous, it will be treated as an MCS */
1460		if (rate & IEEE80211_RATE_MCS) {
1461			rate &= ~IEEE80211_RATE_MCS;
1462			m = findmedia(htrates, N(htrates), rate);
1463			if (m != IFM_AUTO)
1464				return m | IFM_IEEE80211_11NG;
1465		}
1466	}
1467	rate &= IEEE80211_RATE_VAL;
1468	switch (mode) {
1469	case IEEE80211_MODE_11A:
1470	case IEEE80211_MODE_11NA:
1471	case IEEE80211_MODE_TURBO_A:
1472	case IEEE80211_MODE_STURBO_A:
1473		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11A);
1474	case IEEE80211_MODE_11B:
1475		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11B);
1476	case IEEE80211_MODE_FH:
1477		return findmedia(rates, N(rates), rate | IFM_IEEE80211_FH);
1478	case IEEE80211_MODE_AUTO:
1479		/* NB: ic may be NULL for some drivers */
1480		if (ic && ic->ic_phytype == IEEE80211_T_FH)
1481			return findmedia(rates, N(rates),
1482			    rate | IFM_IEEE80211_FH);
1483		/* NB: hack, 11g matches both 11b+11a rates */
1484		/* fall thru... */
1485	case IEEE80211_MODE_11G:
1486	case IEEE80211_MODE_11NG:
1487	case IEEE80211_MODE_TURBO_G:
1488		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11G);
1489	}
1490	return IFM_AUTO;
1491#undef N
1492}
1493
1494int
1495ieee80211_media2rate(int mword)
1496{
1497#define	N(a)	(sizeof(a) / sizeof(a[0]))
1498	static const int ieeerates[] = {
1499		-1,		/* IFM_AUTO */
1500		0,		/* IFM_MANUAL */
1501		0,		/* IFM_NONE */
1502		2,		/* IFM_IEEE80211_FH1 */
1503		4,		/* IFM_IEEE80211_FH2 */
1504		2,		/* IFM_IEEE80211_DS1 */
1505		4,		/* IFM_IEEE80211_DS2 */
1506		11,		/* IFM_IEEE80211_DS5 */
1507		22,		/* IFM_IEEE80211_DS11 */
1508		44,		/* IFM_IEEE80211_DS22 */
1509		12,		/* IFM_IEEE80211_OFDM6 */
1510		18,		/* IFM_IEEE80211_OFDM9 */
1511		24,		/* IFM_IEEE80211_OFDM12 */
1512		36,		/* IFM_IEEE80211_OFDM18 */
1513		48,		/* IFM_IEEE80211_OFDM24 */
1514		72,		/* IFM_IEEE80211_OFDM36 */
1515		96,		/* IFM_IEEE80211_OFDM48 */
1516		108,		/* IFM_IEEE80211_OFDM54 */
1517		144,		/* IFM_IEEE80211_OFDM72 */
1518		0,		/* IFM_IEEE80211_DS354k */
1519		0,		/* IFM_IEEE80211_DS512k */
1520		6,		/* IFM_IEEE80211_OFDM3 */
1521		9,		/* IFM_IEEE80211_OFDM4 */
1522		54,		/* IFM_IEEE80211_OFDM27 */
1523		-1,		/* IFM_IEEE80211_MCS */
1524	};
1525	return IFM_SUBTYPE(mword) < N(ieeerates) ?
1526		ieeerates[IFM_SUBTYPE(mword)] : 0;
1527#undef N
1528}
1529