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