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