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