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