xref: /openbsd-src/sys/net80211/ieee80211.c (revision 4e1ee0786f11cc571bd0be17d38e46f635c719fc)
1 /*	$OpenBSD: ieee80211.c,v 1.85 2021/10/11 09:01:06 stsp Exp $	*/
2 /*	$NetBSD: ieee80211.c,v 1.19 2004/06/06 05:45:29 dyoung Exp $	*/
3 
4 /*-
5  * Copyright (c) 2001 Atsushi Onoe
6  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * IEEE 802.11 generic handler
34  */
35 
36 #include "bpfilter.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/mbuf.h>
41 #include <sys/kernel.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/endian.h>
45 #include <sys/errno.h>
46 #include <sys/sysctl.h>
47 
48 #include <net/if.h>
49 #include <net/if_dl.h>
50 #include <net/if_media.h>
51 
52 #if NBPFILTER > 0
53 #include <net/bpf.h>
54 #endif
55 
56 #include <netinet/in.h>
57 #include <netinet/if_ether.h>
58 
59 #include <net80211/ieee80211_var.h>
60 #include <net80211/ieee80211_priv.h>
61 
62 #ifdef IEEE80211_DEBUG
63 int	ieee80211_debug = 0;
64 #endif
65 
66 int ieee80211_cache_size = IEEE80211_CACHE_SIZE;
67 
68 void ieee80211_setbasicrates(struct ieee80211com *);
69 int ieee80211_findrate(struct ieee80211com *, enum ieee80211_phymode, int);
70 void ieee80211_configure_ampdu_tx(struct ieee80211com *, int);
71 
72 void
73 ieee80211_begin_bgscan(struct ifnet *ifp)
74 {
75 	struct ieee80211com *ic = (void *)ifp;
76 
77 	if ((ic->ic_flags & IEEE80211_F_BGSCAN) ||
78 	    ic->ic_state != IEEE80211_S_RUN || ic->ic_mgt_timer != 0)
79 		return;
80 
81 	if ((ic->ic_flags & IEEE80211_F_RSNON) && !ic->ic_bss->ni_port_valid)
82 		return;
83 
84 	if (ic->ic_bgscan_start != NULL && ic->ic_bgscan_start(ic) == 0) {
85 		/*
86 		 * Free the nodes table to ensure we get an up-to-date view
87 		 * of APs around us. In particular, we need to kick out the
88 		 * AP we are associated to. Otherwise, our current AP might
89 		 * stay cached if it is turned off while we are scanning, and
90 		 * we could end up picking a now non-existent AP over and over.
91 		 */
92 		ieee80211_free_allnodes(ic, 0 /* keep ic->ic_bss */);
93 
94 		ic->ic_flags |= IEEE80211_F_BGSCAN;
95 		if (ifp->if_flags & IFF_DEBUG)
96 			printf("%s: begin background scan\n", ifp->if_xname);
97 
98 		/* Driver calls ieee80211_end_scan() when done. */
99 	}
100 }
101 
102 void
103 ieee80211_bgscan_timeout(void *arg)
104 {
105 	struct ifnet *ifp = arg;
106 
107 	ieee80211_begin_bgscan(ifp);
108 }
109 
110 void
111 ieee80211_channel_init(struct ifnet *ifp)
112 {
113 	struct ieee80211com *ic = (void *)ifp;
114 	struct ieee80211_channel *c;
115 	int i;
116 
117 	/*
118 	 * Fill in 802.11 available channel set, mark
119 	 * all available channels as active, and pick
120 	 * a default channel if not already specified.
121 	 */
122 	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
123 	ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO;
124 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
125 		c = &ic->ic_channels[i];
126 		if (c->ic_flags) {
127 			/*
128 			 * Verify driver passed us valid data.
129 			 */
130 			if (i != ieee80211_chan2ieee(ic, c)) {
131 				printf("%s: bad channel ignored; "
132 					"freq %u flags %x number %u\n",
133 					ifp->if_xname, c->ic_freq, c->ic_flags,
134 					i);
135 				c->ic_flags = 0;	/* NB: remove */
136 				continue;
137 			}
138 			setbit(ic->ic_chan_avail, i);
139 			/*
140 			 * Identify mode capabilities.
141 			 */
142 			if (IEEE80211_IS_CHAN_A(c))
143 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11A;
144 			if (IEEE80211_IS_CHAN_B(c))
145 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11B;
146 			if (IEEE80211_IS_CHAN_PUREG(c))
147 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11G;
148 			if (IEEE80211_IS_CHAN_N(c))
149 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11N;
150 			if (IEEE80211_IS_CHAN_AC(c))
151 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11AC;
152 		}
153 	}
154 	/* validate ic->ic_curmode */
155 	if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0)
156 		ic->ic_curmode = IEEE80211_MODE_AUTO;
157 	ic->ic_des_chan = IEEE80211_CHAN_ANYC;	/* any channel is ok */
158 }
159 
160 void
161 ieee80211_ifattach(struct ifnet *ifp)
162 {
163 	struct ieee80211com *ic = (void *)ifp;
164 
165 	memcpy(((struct arpcom *)ifp)->ac_enaddr, ic->ic_myaddr,
166 		ETHER_ADDR_LEN);
167 	ether_ifattach(ifp);
168 
169 	ifp->if_output = ieee80211_output;
170 
171 #if NBPFILTER > 0
172 	bpfattach(&ic->ic_rawbpf, ifp, DLT_IEEE802_11,
173 	    sizeof(struct ieee80211_frame_addr4));
174 #endif
175 	ieee80211_crypto_attach(ifp);
176 
177 	ieee80211_channel_init(ifp);
178 
179 	/* IEEE 802.11 defines a MTU >= 2290 */
180 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
181 
182 	ieee80211_setbasicrates(ic);
183 	(void)ieee80211_setmode(ic, ic->ic_curmode);
184 
185 	if (ic->ic_lintval == 0)
186 		ic->ic_lintval = 100;		/* default sleep */
187 	ic->ic_bmissthres = IEEE80211_BEACON_MISS_THRES;
188 	ic->ic_dtim_period = 1;	/* all TIMs are DTIMs */
189 
190 	ieee80211_node_attach(ifp);
191 	ieee80211_proto_attach(ifp);
192 
193 	if_addgroup(ifp, "wlan");
194 	ifp->if_priority = IF_WIRELESS_DEFAULT_PRIORITY;
195 
196 	ieee80211_set_link_state(ic, LINK_STATE_DOWN);
197 
198 	timeout_set(&ic->ic_bgscan_timeout, ieee80211_bgscan_timeout, ifp);
199 }
200 
201 void
202 ieee80211_ifdetach(struct ifnet *ifp)
203 {
204 	struct ieee80211com *ic = (void *)ifp;
205 
206 	timeout_del(&ic->ic_bgscan_timeout);
207 
208 	/*
209 	 * Undo pseudo-driver changes. Pseudo-driver detach hooks could
210 	 * call back into the driver, e.g. via ioctl. So deactivate the
211 	 * interface before freeing net80211-specific data structures.
212 	 */
213 	if_deactivate(ifp);
214 
215 	ieee80211_proto_detach(ifp);
216 	ieee80211_crypto_detach(ifp);
217 	ieee80211_node_detach(ifp);
218 	ifmedia_delete_instance(&ic->ic_media, IFM_INST_ANY);
219 	ether_ifdetach(ifp);
220 }
221 
222 /*
223  * Convert MHz frequency to IEEE channel number.
224  */
225 u_int
226 ieee80211_mhz2ieee(u_int freq, u_int flags)
227 {
228 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
229 		if (freq == 2484)
230 			return 14;
231 		if (freq < 2484)
232 			return (freq - 2407) / 5;
233 		else
234 			return 15 + ((freq - 2512) / 20);
235 	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5GHz band */
236 		return (freq - 5000) / 5;
237 	} else {				/* either, guess */
238 		if (freq == 2484)
239 			return 14;
240 		if (freq < 2484)
241 			return (freq - 2407) / 5;
242 		if (freq < 5000)
243 			return 15 + ((freq - 2512) / 20);
244 		return (freq - 5000) / 5;
245 	}
246 }
247 
248 /*
249  * Convert channel to IEEE channel number.
250  */
251 u_int
252 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
253 {
254 	struct ifnet *ifp = &ic->ic_if;
255 	if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
256 		return c - ic->ic_channels;
257 	else if (c == IEEE80211_CHAN_ANYC)
258 		return IEEE80211_CHAN_ANY;
259 
260 	panic("%s: bogus channel pointer", ifp->if_xname);
261 }
262 
263 /*
264  * Convert IEEE channel number to MHz frequency.
265  */
266 u_int
267 ieee80211_ieee2mhz(u_int chan, u_int flags)
268 {
269 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
270 		if (chan == 14)
271 			return 2484;
272 		if (chan < 14)
273 			return 2407 + chan*5;
274 		else
275 			return 2512 + ((chan-15)*20);
276 	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5GHz band */
277 		return 5000 + (chan*5);
278 	} else {				/* either, guess */
279 		if (chan == 14)
280 			return 2484;
281 		if (chan < 14)			/* 0-13 */
282 			return 2407 + chan*5;
283 		if (chan < 27)			/* 15-26 */
284 			return 2512 + ((chan-15)*20);
285 		return 5000 + (chan*5);
286 	}
287 }
288 
289 void
290 ieee80211_configure_ampdu_tx(struct ieee80211com *ic, int enable)
291 {
292 	if ((ic->ic_caps & IEEE80211_C_TX_AMPDU) == 0)
293 		return;
294 
295 	/* Sending AMPDUs requires QoS support. */
296 	if ((ic->ic_caps & IEEE80211_C_QOS) == 0)
297 		return;
298 
299 	if (enable)
300 		ic->ic_flags |= IEEE80211_F_QOS;
301 	else
302 		ic->ic_flags &= ~IEEE80211_F_QOS;
303 }
304 
305 /*
306  * Setup the media data structures according to the channel and
307  * rate tables.  This must be called by the driver after
308  * ieee80211_attach and before most anything else.
309  */
310 void
311 ieee80211_media_init(struct ifnet *ifp,
312 	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
313 {
314 #define	ADD(_ic, _s, _o) \
315 	ifmedia_add(&(_ic)->ic_media, \
316 		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
317 	struct ieee80211com *ic = (void *)ifp;
318 	struct ifmediareq imr;
319 	int i, j, mode, rate, maxrate, r;
320 	uint64_t mword, mopt;
321 	const struct ieee80211_rateset *rs;
322 	struct ieee80211_rateset allrates;
323 
324 	/*
325 	 * Do late attach work that must wait for any subclass
326 	 * (i.e. driver) work such as overriding methods.
327 	 */
328 	ieee80211_node_lateattach(ifp);
329 
330 	/*
331 	 * Fill in media characteristics.
332 	 */
333 	ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
334 	maxrate = 0;
335 	memset(&allrates, 0, sizeof(allrates));
336 	for (mode = IEEE80211_MODE_AUTO; mode <= IEEE80211_MODE_11G; mode++) {
337 		static const uint64_t mopts[] = {
338 			IFM_AUTO,
339 			IFM_IEEE80211_11A,
340 			IFM_IEEE80211_11B,
341 			IFM_IEEE80211_11G,
342 		};
343 		if ((ic->ic_modecaps & (1<<mode)) == 0)
344 			continue;
345 		mopt = mopts[mode];
346 		ADD(ic, IFM_AUTO, mopt);	/* e.g. 11a auto */
347 #ifndef IEEE80211_STA_ONLY
348 		if (ic->ic_caps & IEEE80211_C_IBSS)
349 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_IBSS);
350 		if (ic->ic_caps & IEEE80211_C_HOSTAP)
351 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
352 		if (ic->ic_caps & IEEE80211_C_AHDEMO)
353 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
354 #endif
355 		if (ic->ic_caps & IEEE80211_C_MONITOR)
356 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
357 		if (mode == IEEE80211_MODE_AUTO)
358 			continue;
359 		rs = &ic->ic_sup_rates[mode];
360 		for (i = 0; i < rs->rs_nrates; i++) {
361 			rate = rs->rs_rates[i];
362 			mword = ieee80211_rate2media(ic, rate, mode);
363 			if (mword == 0)
364 				continue;
365 			ADD(ic, mword, mopt);
366 #ifndef IEEE80211_STA_ONLY
367 			if (ic->ic_caps & IEEE80211_C_IBSS)
368 				ADD(ic, mword, mopt | IFM_IEEE80211_IBSS);
369 			if (ic->ic_caps & IEEE80211_C_HOSTAP)
370 				ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
371 			if (ic->ic_caps & IEEE80211_C_AHDEMO)
372 				ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
373 #endif
374 			if (ic->ic_caps & IEEE80211_C_MONITOR)
375 				ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
376 			/*
377 			 * Add rate to the collection of all rates.
378 			 */
379 			r = rate & IEEE80211_RATE_VAL;
380 			for (j = 0; j < allrates.rs_nrates; j++)
381 				if (allrates.rs_rates[j] == r)
382 					break;
383 			if (j == allrates.rs_nrates) {
384 				/* unique, add to the set */
385 				allrates.rs_rates[j] = r;
386 				allrates.rs_nrates++;
387 			}
388 			rate = (rate & IEEE80211_RATE_VAL) / 2;
389 			if (rate > maxrate)
390 				maxrate = rate;
391 		}
392 	}
393 	for (i = 0; i < allrates.rs_nrates; i++) {
394 		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
395 				IEEE80211_MODE_AUTO);
396 		if (mword == 0)
397 			continue;
398 		mword = IFM_SUBTYPE(mword);	/* remove media options */
399 		ADD(ic, mword, 0);
400 #ifndef IEEE80211_STA_ONLY
401 		if (ic->ic_caps & IEEE80211_C_IBSS)
402 			ADD(ic, mword, IFM_IEEE80211_IBSS);
403 		if (ic->ic_caps & IEEE80211_C_HOSTAP)
404 			ADD(ic, mword, IFM_IEEE80211_HOSTAP);
405 		if (ic->ic_caps & IEEE80211_C_AHDEMO)
406 			ADD(ic, mword, IFM_IEEE80211_ADHOC);
407 #endif
408 		if (ic->ic_caps & IEEE80211_C_MONITOR)
409 			ADD(ic, mword, IFM_IEEE80211_MONITOR);
410 	}
411 
412 	if (ic->ic_modecaps & (1 << IEEE80211_MODE_11N)) {
413 		mopt = IFM_IEEE80211_11N;
414 		ADD(ic, IFM_AUTO, mopt);
415 #ifndef IEEE80211_STA_ONLY
416 		if (ic->ic_caps & IEEE80211_C_IBSS)
417 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_IBSS);
418 		if (ic->ic_caps & IEEE80211_C_HOSTAP)
419 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
420 #endif
421 		if (ic->ic_caps & IEEE80211_C_MONITOR)
422 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
423 		for (i = 0; i < IEEE80211_HT_NUM_MCS; i++) {
424 			if (!isset(ic->ic_sup_mcs, i))
425 				continue;
426 			ADD(ic, IFM_IEEE80211_HT_MCS0 + i, mopt);
427 #ifndef IEEE80211_STA_ONLY
428 			if (ic->ic_caps & IEEE80211_C_IBSS)
429 				ADD(ic, IFM_IEEE80211_HT_MCS0 + i,
430 				     mopt | IFM_IEEE80211_IBSS);
431 			if (ic->ic_caps & IEEE80211_C_HOSTAP)
432 				ADD(ic, IFM_IEEE80211_HT_MCS0 + i,
433 				    mopt | IFM_IEEE80211_HOSTAP);
434 #endif
435 			if (ic->ic_caps & IEEE80211_C_MONITOR)
436 				ADD(ic, IFM_IEEE80211_HT_MCS0 + i,
437 				    mopt | IFM_IEEE80211_MONITOR);
438 		}
439 		ic->ic_flags |= IEEE80211_F_HTON; /* enable 11n by default */
440 		ieee80211_configure_ampdu_tx(ic, 1);
441 	}
442 
443 	if (ic->ic_modecaps & (1 << IEEE80211_MODE_11AC)) {
444 		mopt = IFM_IEEE80211_11AC;
445 		ADD(ic, IFM_AUTO, mopt);
446 #ifndef IEEE80211_STA_ONLY
447 		if (ic->ic_caps & IEEE80211_C_IBSS)
448 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_IBSS);
449 		if (ic->ic_caps & IEEE80211_C_HOSTAP)
450 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
451 #endif
452 		if (ic->ic_caps & IEEE80211_C_MONITOR)
453 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
454 		for (i = 0; i < IEEE80211_VHT_NUM_MCS; i++) {
455 #if 0
456 			/* TODO: Obtain VHT MCS information from VHT CAP IE. */
457 			if (!vht_mcs_supported)
458 				continue;
459 #endif
460 			ADD(ic, IFM_IEEE80211_VHT_MCS0 + i, mopt);
461 #ifndef IEEE80211_STA_ONLY
462 			if (ic->ic_caps & IEEE80211_C_IBSS)
463 				ADD(ic, IFM_IEEE80211_VHT_MCS0 + i,
464 				     mopt | IFM_IEEE80211_IBSS);
465 			if (ic->ic_caps & IEEE80211_C_HOSTAP)
466 				ADD(ic, IFM_IEEE80211_VHT_MCS0 + i,
467 				    mopt | IFM_IEEE80211_HOSTAP);
468 #endif
469 			if (ic->ic_caps & IEEE80211_C_MONITOR)
470 				ADD(ic, IFM_IEEE80211_VHT_MCS0 + i,
471 				    mopt | IFM_IEEE80211_MONITOR);
472 		}
473 #if 0
474 		ic->ic_flags |= IEEE80211_F_VHTON; /* enable 11ac by default */
475 		if (ic->ic_caps & IEEE80211_C_QOS)
476 			ic->ic_flags |= IEEE80211_F_QOS;
477 #endif
478 	}
479 
480 	ieee80211_media_status(ifp, &imr);
481 	ifmedia_set(&ic->ic_media, imr.ifm_active);
482 
483 	if (maxrate)
484 		ifp->if_baudrate = IF_Mbps(maxrate);
485 
486 #undef ADD
487 }
488 
489 int
490 ieee80211_findrate(struct ieee80211com *ic, enum ieee80211_phymode mode,
491     int rate)
492 {
493 #define	IEEERATE(_ic,_m,_i) \
494 	((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
495 	int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
496 	for (i = 0; i < nrates; i++)
497 		if (IEEERATE(ic, mode, i) == rate)
498 			return i;
499 	return -1;
500 #undef IEEERATE
501 }
502 
503 /*
504  * Handle a media change request.
505  */
506 int
507 ieee80211_media_change(struct ifnet *ifp)
508 {
509 	struct ieee80211com *ic = (void *)ifp;
510 	struct ifmedia_entry *ime;
511 	enum ieee80211_opmode newopmode;
512 	enum ieee80211_phymode newphymode;
513 	int i, j, newrate, error = 0;
514 
515 	ime = ic->ic_media.ifm_cur;
516 	/*
517 	 * First, identify the phy mode.
518 	 */
519 	switch (IFM_MODE(ime->ifm_media)) {
520 	case IFM_IEEE80211_11A:
521 		newphymode = IEEE80211_MODE_11A;
522 		break;
523 	case IFM_IEEE80211_11B:
524 		newphymode = IEEE80211_MODE_11B;
525 		break;
526 	case IFM_IEEE80211_11G:
527 		newphymode = IEEE80211_MODE_11G;
528 		break;
529 	case IFM_IEEE80211_11N:
530 		newphymode = IEEE80211_MODE_11N;
531 		break;
532 	case IFM_IEEE80211_11AC:
533 		newphymode = IEEE80211_MODE_11AC;
534 		break;
535 	case IFM_AUTO:
536 		newphymode = IEEE80211_MODE_AUTO;
537 		break;
538 	default:
539 		return EINVAL;
540 	}
541 
542 	/*
543 	 * Validate requested mode is available.
544 	 */
545 	if ((ic->ic_modecaps & (1<<newphymode)) == 0)
546 		return EINVAL;
547 
548 	/*
549 	 * Next, the fixed/variable rate.
550 	 */
551 	i = -1;
552 	if (IFM_SUBTYPE(ime->ifm_media) >= IFM_IEEE80211_VHT_MCS0 &&
553 	    IFM_SUBTYPE(ime->ifm_media) <= IFM_IEEE80211_VHT_MCS9) {
554 		if ((ic->ic_modecaps & (1 << IEEE80211_MODE_11AC)) == 0)
555 			return EINVAL;
556 		if (newphymode != IEEE80211_MODE_AUTO &&
557 		    newphymode != IEEE80211_MODE_11AC)
558 			return EINVAL;
559 		i = ieee80211_media2mcs(ime->ifm_media);
560 		/* TODO: Obtain VHT MCS information from VHT CAP IE. */
561 		if (i == -1 /* || !vht_mcs_supported */)
562 			return EINVAL;
563 	} else if (IFM_SUBTYPE(ime->ifm_media) >= IFM_IEEE80211_HT_MCS0 &&
564 	    IFM_SUBTYPE(ime->ifm_media) <= IFM_IEEE80211_HT_MCS76) {
565 		if ((ic->ic_modecaps & (1 << IEEE80211_MODE_11N)) == 0)
566 			return EINVAL;
567 		if (newphymode != IEEE80211_MODE_AUTO &&
568 		    newphymode != IEEE80211_MODE_11N)
569 			return EINVAL;
570 		i = ieee80211_media2mcs(ime->ifm_media);
571 		if (i == -1 || isclr(ic->ic_sup_mcs, i))
572 			return EINVAL;
573 	} else if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
574 		/*
575 		 * Convert media subtype to rate.
576 		 */
577 		newrate = ieee80211_media2rate(ime->ifm_media);
578 		if (newrate == 0)
579 			return EINVAL;
580 		/*
581 		 * Check the rate table for the specified/current phy.
582 		 */
583 		if (newphymode == IEEE80211_MODE_AUTO) {
584 			/*
585 			 * In autoselect mode search for the rate.
586 			 */
587 			for (j = IEEE80211_MODE_11A;
588 			     j < IEEE80211_MODE_MAX; j++) {
589 				if ((ic->ic_modecaps & (1<<j)) == 0)
590 					continue;
591 				i = ieee80211_findrate(ic, j, newrate);
592 				if (i != -1) {
593 					/* lock mode too */
594 					newphymode = j;
595 					break;
596 				}
597 			}
598 		} else {
599 			i = ieee80211_findrate(ic, newphymode, newrate);
600 		}
601 		if (i == -1)			/* mode/rate mismatch */
602 			return EINVAL;
603 	}
604 	/* NB: defer rate setting to later */
605 
606 	/*
607 	 * Deduce new operating mode but don't install it just yet.
608 	 */
609 #ifndef IEEE80211_STA_ONLY
610 	if (ime->ifm_media & IFM_IEEE80211_ADHOC)
611 		newopmode = IEEE80211_M_AHDEMO;
612 	else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
613 		newopmode = IEEE80211_M_HOSTAP;
614 	else if (ime->ifm_media & IFM_IEEE80211_IBSS)
615 		newopmode = IEEE80211_M_IBSS;
616 	else
617 #endif
618 	if (ime->ifm_media & IFM_IEEE80211_MONITOR)
619 		newopmode = IEEE80211_M_MONITOR;
620 	else
621 		newopmode = IEEE80211_M_STA;
622 
623 #ifndef IEEE80211_STA_ONLY
624 	/*
625 	 * Autoselect doesn't make sense when operating as an AP.
626 	 * If no phy mode has been selected, pick one and lock it
627 	 * down so rate tables can be used in forming beacon frames
628 	 * and the like.
629 	 */
630 	if (newopmode == IEEE80211_M_HOSTAP &&
631 	    newphymode == IEEE80211_MODE_AUTO) {
632 		if (ic->ic_modecaps & (1 << IEEE80211_MODE_11AC))
633 			newphymode = IEEE80211_MODE_11AC;
634 		else if (ic->ic_modecaps & (1 << IEEE80211_MODE_11N))
635 			newphymode = IEEE80211_MODE_11N;
636 		else if (ic->ic_modecaps & (1 << IEEE80211_MODE_11A))
637 			newphymode = IEEE80211_MODE_11A;
638 		else if (ic->ic_modecaps & (1 << IEEE80211_MODE_11G))
639 			newphymode = IEEE80211_MODE_11G;
640 		else
641 			newphymode = IEEE80211_MODE_11B;
642 	}
643 #endif
644 
645 	/*
646 	 * Handle phy mode change.
647 	 */
648 	if (ic->ic_curmode != newphymode) {		/* change phy mode */
649 		error = ieee80211_setmode(ic, newphymode);
650 		if (error != 0)
651 			return error;
652 		error = ENETRESET;
653 	}
654 
655 	/*
656 	 * Committed to changes, install the MCS/rate setting.
657 	 */
658 	ic->ic_flags &= ~(IEEE80211_F_HTON | IEEE80211_F_VHTON);
659 	ieee80211_configure_ampdu_tx(ic, 0);
660 	if ((ic->ic_modecaps & (1 << IEEE80211_MODE_11AC)) &&
661 	    (newphymode == IEEE80211_MODE_AUTO ||
662 	    newphymode == IEEE80211_MODE_11AC)) {
663 		ic->ic_flags |= IEEE80211_F_VHTON;
664 		ieee80211_configure_ampdu_tx(ic, 1);
665 	} else if ((ic->ic_modecaps & (1 << IEEE80211_MODE_11N)) &&
666 	    (newphymode == IEEE80211_MODE_AUTO ||
667 	    newphymode == IEEE80211_MODE_11N)) {
668 		ic->ic_flags |= IEEE80211_F_HTON;
669 		ieee80211_configure_ampdu_tx(ic, 1);
670 	}
671 	if ((ic->ic_flags & (IEEE80211_F_HTON | IEEE80211_F_VHTON)) == 0) {
672 		ic->ic_fixed_mcs = -1;
673 	    	if (ic->ic_fixed_rate != i) {
674 			ic->ic_fixed_rate = i;		/* set fixed tx rate */
675 			error = ENETRESET;
676 		}
677 	} else {
678 		ic->ic_fixed_rate = -1;
679 		if (ic->ic_fixed_mcs != i) {
680 			ic->ic_fixed_mcs = i;		/* set fixed mcs */
681 			error = ENETRESET;
682 		}
683 	}
684 
685 	/*
686 	 * Handle operating mode change.
687 	 */
688 	if (ic->ic_opmode != newopmode) {
689 		ic->ic_opmode = newopmode;
690 #ifndef IEEE80211_STA_ONLY
691 		switch (newopmode) {
692 		case IEEE80211_M_AHDEMO:
693 		case IEEE80211_M_HOSTAP:
694 		case IEEE80211_M_STA:
695 		case IEEE80211_M_MONITOR:
696 			ic->ic_flags &= ~IEEE80211_F_IBSSON;
697 			break;
698 		case IEEE80211_M_IBSS:
699 			ic->ic_flags |= IEEE80211_F_IBSSON;
700 			break;
701 		}
702 #endif
703 		/*
704 		 * Yech, slot time may change depending on the
705 		 * operating mode so reset it to be sure everything
706 		 * is setup appropriately.
707 		 */
708 		ieee80211_reset_erp(ic);
709 		error = ENETRESET;
710 	}
711 #ifdef notdef
712 	if (error == 0)
713 		ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
714 #endif
715 	return error;
716 }
717 
718 void
719 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
720 {
721 	struct ieee80211com *ic = (void *)ifp;
722 	const struct ieee80211_node *ni = NULL;
723 
724 	imr->ifm_status = IFM_AVALID;
725 	imr->ifm_active = IFM_IEEE80211;
726 	if (ic->ic_state == IEEE80211_S_RUN &&
727 	    (ic->ic_opmode != IEEE80211_M_STA ||
728 	     !(ic->ic_flags & IEEE80211_F_RSNON) ||
729 	     ic->ic_bss->ni_port_valid))
730 		imr->ifm_status |= IFM_ACTIVE;
731 	imr->ifm_active |= IFM_AUTO;
732 	switch (ic->ic_opmode) {
733 	case IEEE80211_M_STA:
734 		ni = ic->ic_bss;
735 		if (ic->ic_curmode == IEEE80211_MODE_11N ||
736 		    ic->ic_curmode == IEEE80211_MODE_11AC)
737 			imr->ifm_active |= ieee80211_mcs2media(ic,
738 				ni->ni_txmcs, ic->ic_curmode);
739 		else if (ni->ni_flags & IEEE80211_NODE_VHT) /* in MODE_AUTO */
740 			imr->ifm_active |= ieee80211_mcs2media(ic,
741 				ni->ni_txmcs, IEEE80211_MODE_11AC);
742 		else if (ni->ni_flags & IEEE80211_NODE_HT) /* in MODE_AUTO */
743 			imr->ifm_active |= ieee80211_mcs2media(ic,
744 				ni->ni_txmcs, IEEE80211_MODE_11N);
745 		else
746 			/* calculate rate subtype */
747 			imr->ifm_active |= ieee80211_rate2media(ic,
748 				ni->ni_rates.rs_rates[ni->ni_txrate],
749 				ic->ic_curmode);
750 		break;
751 #ifndef IEEE80211_STA_ONLY
752 	case IEEE80211_M_IBSS:
753 		imr->ifm_active |= IFM_IEEE80211_IBSS;
754 		break;
755 	case IEEE80211_M_AHDEMO:
756 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
757 		break;
758 	case IEEE80211_M_HOSTAP:
759 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
760 		break;
761 #endif
762 	case IEEE80211_M_MONITOR:
763 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
764 		break;
765 	default:
766 		break;
767 	}
768 	switch (ic->ic_curmode) {
769 	case IEEE80211_MODE_11A:
770 		imr->ifm_active |= IFM_IEEE80211_11A;
771 		break;
772 	case IEEE80211_MODE_11B:
773 		imr->ifm_active |= IFM_IEEE80211_11B;
774 		break;
775 	case IEEE80211_MODE_11G:
776 		imr->ifm_active |= IFM_IEEE80211_11G;
777 		break;
778 	case IEEE80211_MODE_11N:
779 		imr->ifm_active |= IFM_IEEE80211_11N;
780 		break;
781 	case IEEE80211_MODE_11AC:
782 		imr->ifm_active |= IFM_IEEE80211_11AC;
783 		break;
784 	}
785 }
786 
787 void
788 ieee80211_watchdog(struct ifnet *ifp)
789 {
790 	struct ieee80211com *ic = (void *)ifp;
791 
792 	if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0) {
793 		if (ic->ic_opmode == IEEE80211_M_STA &&
794 		    (ic->ic_state == IEEE80211_S_AUTH ||
795 		    ic->ic_state == IEEE80211_S_ASSOC)) {
796 			struct ieee80211_node *ni;
797 			if (ifp->if_flags & IFF_DEBUG)
798 				printf("%s: %s timed out for %s\n",
799 				    ifp->if_xname,
800 				    ic->ic_state == IEEE80211_S_ASSOC ?
801 				    "association" : "authentication",
802 				    ether_sprintf(ic->ic_bss->ni_macaddr));
803 			ni = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr);
804 			if (ni)
805 				ni->ni_fails++;
806 			if (ISSET(ic->ic_flags, IEEE80211_F_AUTO_JOIN))
807 				ieee80211_deselect_ess(ic);
808 		}
809 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
810 	}
811 
812 	if (ic->ic_mgt_timer != 0)
813 		ifp->if_timer = 1;
814 }
815 
816 const struct ieee80211_rateset ieee80211_std_rateset_11a =
817 	{ 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
818 
819 const struct ieee80211_rateset ieee80211_std_rateset_11b =
820 	{ 4, { 2, 4, 11, 22 } };
821 
822 const struct ieee80211_rateset ieee80211_std_rateset_11g =
823 	{ 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
824 
825 const struct ieee80211_ht_rateset ieee80211_std_ratesets_11n[] = {
826 	/* MCS 0-7, 20MHz channel, no SGI */
827 	{ 8, { 13, 26, 39, 52, 78, 104, 117, 130 },
828 	    0x000000ff, 0, 7, 0, 0},
829 
830 	/* MCS 0-7, 20MHz channel, SGI */
831 	{ 8, { 14, 29, 43, 58, 87, 116, 130, 144 },
832 	    0x000000ff, 0, 7, 0, 1 },
833 
834 	/* MCS 8-15, 20MHz channel, no SGI */
835 	{ 8, { 26, 52, 78, 104, 156, 208, 234, 260 },
836 	    0x0000ff00, 8, 15, 0, 0 },
837 
838 	/* MCS 8-15, 20MHz channel, SGI */
839 	{ 8, { 29, 58, 87, 116, 173, 231, 261, 289 },
840 	    0x0000ff00, 8, 15, 0, 1 },
841 
842 	/* MCS 16-23, 20MHz channel, no SGI */
843 	{ 8, { 39, 78, 117, 156, 234, 312, 351, 390 },
844 	    0x00ff0000, 16, 23, 0, 0 },
845 
846 	/* MCS 16-23, 20MHz channel, SGI */
847 	{ 8, { 43, 87, 130, 173, 260, 347, 390, 433 },
848 	    0x00ff0000, 16, 23, 0, 1 },
849 
850 	/* MCS 24-31, 20MHz channel, no SGI */
851 	{ 8, { 52, 104, 156, 208, 312, 416, 468, 520 },
852 	    0xff000000, 24, 31, 0, 0 },
853 
854 	/* MCS 24-31, 20MHz channel, SGI */
855 	{ 8, { 58, 116, 173, 231, 347, 462, 520, 578 },
856 	    0xff000000, 24, 31, 0, 1 },
857 
858 	/* MCS 0-7, 40MHz channel, no SGI */
859 	{ 8, { 27, 54, 81, 108, 162, 216, 243, 270 },
860 	    0x000000ff, 0, 7, 1, 0 },
861 
862 	/* MCS 0-7, 40MHz channel, SGI */
863 	{ 8, { 30, 60, 90, 120, 180, 240, 270, 300 },
864 	    0x000000ff, 0, 7, 1, 1 },
865 
866 	/* MCS 8-15, 40MHz channel, no SGI */
867 	{ 8, { 54, 108, 192, 216, 324, 432, 486, 540 },
868 	    0x0000ff00, 8, 15, 1, 0 },
869 
870 	/* MCS 8-15, 40MHz channel, SGI */
871 	{ 8, { 60, 120, 180, 240, 360, 480, 540, 600 },
872 	    0x0000ff00, 8, 15, 1, 1 },
873 
874 	/* MCS 16-23, 40MHz channel, no SGI */
875 	{ 8, { 81, 162, 243, 324, 486, 648, 729, 810 },
876 	    0x00ff0000, 16, 23, 1, 0 },
877 
878 	/* MCS 16-23, 40MHz channel, SGI */
879 	{ 8, { 90, 180, 270, 360, 540, 720, 810, 900 },
880 	    0x00ff0000, 16, 23, 1, 1 },
881 
882 	/* MCS 24-31, 40MHz channel, no SGI */
883 	{ 8, { 108, 216, 324, 432, 324, 864, 972, 1080 },
884 	    0xff000000, 24, 31, 1, 0 },
885 
886 	/* MCS 24-31, 40MHz channel, SGI */
887 	{ 8, { 120, 240, 360, 480, 520, 960, 1080, 1200 },
888 	    0xff000000, 24, 31, 1, 1 },
889 };
890 
891 const struct ieee80211_vht_rateset ieee80211_std_ratesets_11ac[] = {
892 	/* MCS 0-8 (MCS 9 N/A), 1 SS, 20MHz channel, no SGI */
893 	{ 9, { 13, 26, 39, 52, 78, 104, 117, 130, 156 }, 1, 0 },
894 
895 	/* MCS 0-8 (MCS 9 N/A), 1 SS, 20MHz channel, SGI */
896 	{ 9, { 14, 29, 43, 58, 87, 116, 130, 144, 174 }, 1, 1 },
897 
898 	/* MCS 0-8 (MCS 9 N/A), 2 SS, 20MHz channel, no SGI */
899 	{ 9, { 26, 52, 78, 104, 156, 208, 234, 260, 312 }, 2, 0 },
900 
901 	/* MCS 0-8 (MCS 9 N/A), 2 SS, 20MHz channel, SGI */
902 	{ 9, { 29, 58, 87, 116, 173, 231, 261, 289, 347 }, 2, 1 },
903 
904 	/* MCS 0-9, 1 SS, 40MHz channel, no SGI */
905 	{ 10, { 27, 54, 81, 108, 162, 216, 243, 270, 324, 360 }, 1, 0 },
906 
907 	/* MCS 0-9, 1 SS, 40MHz channel, SGI */
908 	{ 10, { 30, 60, 90, 120, 180, 240, 270, 300, 360, 400 }, 1, 1 },
909 
910 	/* MCS 0-9, 2 SS, 40MHz channel, no SGI */
911 	{ 10, { 54, 108, 162, 216, 324, 432, 486, 540, 648, 720 }, 2, 0 },
912 
913 	/* MCS 0-9, 2 SS, 40MHz channel, SGI */
914 	{ 10, { 60, 120, 180, 240, 360, 480, 540, 600, 720, 800 }, 2, 1 },
915 
916 	/* MCS 0-9, 1 SS, 80MHz channel, no SGI */
917 	{ 10, { 59, 117, 176, 234, 351, 468, 527, 585, 702, 780 }, 1, 0 },
918 
919 	/* MCS 0-9, 1 SS, 80MHz channel, SGI */
920 	{ 10, { 65, 130, 195, 260, 390, 520, 585, 650, 780, 867 }, 1, 1 },
921 
922 	/* MCS 0-9, 2 SS, 80MHz channel, no SGI */
923 	{ 10, { 117, 234, 351, 468, 702, 936, 1053, 1404, 1560 }, 2, 0 },
924 
925 	/* MCS 0-9, 2 SS, 80MHz channel, SGI */
926 	{ 10, { 130, 260, 390, 520, 780, 1040, 1170, 1300, 1560, 1734 }, 2, 1 },
927 };
928 
929 /*
930  * Mark the basic rates for the 11g rate table based on the
931  * operating mode.  For real 11g we mark all the 11b rates
932  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
933  * 11b rates.  There's also a pseudo 11a-mode used to mark only
934  * the basic OFDM rates.
935  */
936 void
937 ieee80211_setbasicrates(struct ieee80211com *ic)
938 {
939 	static const struct ieee80211_rateset basic[] = {
940 	    { 0 },				/* IEEE80211_MODE_AUTO */
941 	    { 3, { 12, 24, 48 } },		/* IEEE80211_MODE_11A */
942 	    { 2, { 2, 4 } },			/* IEEE80211_MODE_11B */
943 	    { 4, { 2, 4, 11, 22 } },		/* IEEE80211_MODE_11G */
944 	    { 0 },				/* IEEE80211_MODE_11N	*/
945 	    { 0 },				/* IEEE80211_MODE_11AC	*/
946 	};
947 	enum ieee80211_phymode mode;
948 	struct ieee80211_rateset *rs;
949 	int i, j;
950 
951 	for (mode = 0; mode < IEEE80211_MODE_MAX; mode++) {
952 		rs = &ic->ic_sup_rates[mode];
953 		for (i = 0; i < rs->rs_nrates; i++) {
954 			rs->rs_rates[i] &= IEEE80211_RATE_VAL;
955 			for (j = 0; j < basic[mode].rs_nrates; j++) {
956 				if (basic[mode].rs_rates[j] ==
957 				    rs->rs_rates[i]) {
958 					rs->rs_rates[i] |=
959 					    IEEE80211_RATE_BASIC;
960 					break;
961 				}
962 			}
963 		}
964 	}
965 }
966 
967 int
968 ieee80211_min_basic_rate(struct ieee80211com *ic)
969 {
970 	struct ieee80211_rateset *rs = &ic->ic_bss->ni_rates;
971 	int i, min, rval;
972 
973 	min = -1;
974 
975 	for (i = 0; i < rs->rs_nrates; i++) {
976 		if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) == 0)
977 			continue;
978 		rval = (rs->rs_rates[i] & IEEE80211_RATE_VAL);
979 		if (min == -1)
980 			min = rval;
981 		else if (rval < min)
982 			min = rval;
983 	}
984 
985 	/* Default to 1 Mbit/s on 2GHz and 6 Mbit/s on 5GHz. */
986 	if (min == -1)
987 		min = IEEE80211_IS_CHAN_2GHZ(ic->ic_bss->ni_chan) ? 2 : 12;
988 
989 	return min;
990 }
991 
992 int
993 ieee80211_max_basic_rate(struct ieee80211com *ic)
994 {
995 	struct ieee80211_rateset *rs = &ic->ic_bss->ni_rates;
996 	int i, max, rval;
997 
998 	/* Default to 1 Mbit/s on 2GHz and 6 Mbit/s on 5GHz. */
999 	max = IEEE80211_IS_CHAN_2GHZ(ic->ic_bss->ni_chan) ? 2 : 12;
1000 
1001 	for (i = 0; i < rs->rs_nrates; i++) {
1002 		if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) == 0)
1003 			continue;
1004 		rval = (rs->rs_rates[i] & IEEE80211_RATE_VAL);
1005 		if (rval > max)
1006 			max = rval;
1007 	}
1008 
1009 	return max;
1010 }
1011 
1012 /*
1013  * Set the current phy mode and recalculate the active channel
1014  * set based on the available channels for this mode.  Also
1015  * select a new default/current channel if the current one is
1016  * inappropriate for this mode.
1017  */
1018 int
1019 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1020 {
1021 	struct ifnet *ifp = &ic->ic_if;
1022 	static const u_int chanflags[] = {
1023 		0,			/* IEEE80211_MODE_AUTO */
1024 		IEEE80211_CHAN_A,	/* IEEE80211_MODE_11A */
1025 		IEEE80211_CHAN_B,	/* IEEE80211_MODE_11B */
1026 		IEEE80211_CHAN_PUREG,	/* IEEE80211_MODE_11G */
1027 		IEEE80211_CHAN_HT,	/* IEEE80211_MODE_11N */
1028 		IEEE80211_CHAN_VHT,	/* IEEE80211_MODE_11AC */
1029 	};
1030 	const struct ieee80211_channel *c;
1031 	u_int modeflags;
1032 	int i;
1033 
1034 	/* validate new mode */
1035 	if ((ic->ic_modecaps & (1<<mode)) == 0) {
1036 		DPRINTF(("mode %u not supported (caps 0x%x)\n",
1037 		    mode, ic->ic_modecaps));
1038 		return EINVAL;
1039 	}
1040 
1041 	/*
1042 	 * Verify at least one channel is present in the available
1043 	 * channel list before committing to the new mode.
1044 	 */
1045 	if (mode >= nitems(chanflags))
1046 		panic("%s: unexpected mode %u", __func__, mode);
1047 	modeflags = chanflags[mode];
1048 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
1049 		c = &ic->ic_channels[i];
1050 		if (mode == IEEE80211_MODE_AUTO) {
1051 			if (c->ic_flags != 0)
1052 				break;
1053 		} else if ((c->ic_flags & modeflags) == modeflags)
1054 			break;
1055 	}
1056 	if (i > IEEE80211_CHAN_MAX) {
1057 		DPRINTF(("no channels found for mode %u\n", mode));
1058 		return EINVAL;
1059 	}
1060 
1061 	/*
1062 	 * Calculate the active channel set.
1063 	 */
1064 	memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
1065 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
1066 		c = &ic->ic_channels[i];
1067 		if (mode == IEEE80211_MODE_AUTO) {
1068 			if (c->ic_flags != 0)
1069 				setbit(ic->ic_chan_active, i);
1070 		} else if ((c->ic_flags & modeflags) == modeflags)
1071 			setbit(ic->ic_chan_active, i);
1072 	}
1073 	/*
1074 	 * If no current/default channel is setup or the current
1075 	 * channel is wrong for the mode then pick the first
1076 	 * available channel from the active list.  This is likely
1077 	 * not the right one.
1078 	 */
1079 	if (ic->ic_ibss_chan == NULL || isclr(ic->ic_chan_active,
1080 	    ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
1081 		for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
1082 			if (isset(ic->ic_chan_active, i)) {
1083 				ic->ic_ibss_chan = &ic->ic_channels[i];
1084 				break;
1085 			}
1086 		if ((ic->ic_ibss_chan == NULL) || isclr(ic->ic_chan_active,
1087 		    ieee80211_chan2ieee(ic, ic->ic_ibss_chan)))
1088 			panic("Bad IBSS channel %u",
1089 			    ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
1090 	}
1091 
1092 	/*
1093 	 * Reset the scan state for the new mode. This avoids scanning
1094 	 * of invalid channels, ie. 5GHz channels in 11b mode.
1095 	 */
1096 	ieee80211_reset_scan(ifp);
1097 
1098 	ic->ic_curmode = mode;
1099 	ieee80211_reset_erp(ic);	/* reset ERP state */
1100 
1101 	return 0;
1102 }
1103 
1104 enum ieee80211_phymode
1105 ieee80211_next_mode(struct ifnet *ifp)
1106 {
1107 	struct ieee80211com *ic = (void *)ifp;
1108 	uint16_t mode;
1109 
1110 	/*
1111 	 * Indicate a wrap-around if we're running in a fixed, user-specified
1112 	 * phy mode.
1113 	 */
1114 	if (IFM_MODE(ic->ic_media.ifm_cur->ifm_media) != IFM_AUTO)
1115 		return (IEEE80211_MODE_AUTO);
1116 
1117 	/*
1118 	 * Always scan in AUTO mode if the driver scans all bands.
1119 	 * The current mode might have changed during association
1120 	 * so we must reset it here.
1121 	 */
1122 	if (ic->ic_caps & IEEE80211_C_SCANALLBAND) {
1123 		ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
1124 		return (ic->ic_curmode);
1125 	}
1126 
1127 	/*
1128 	 * Get the next supported mode; effectively, this alternates between
1129 	 * the 11a (5GHz) and 11b/g (2GHz) modes. What matters is that each
1130 	 * supported channel gets scanned.
1131 	 */
1132 	for (mode = ic->ic_curmode + 1; mode <= IEEE80211_MODE_MAX; mode++) {
1133 		/*
1134 		 * Skip over 11n mode. Its set of channels is the superset
1135 		 * of all channels supported by the other modes.
1136 		 */
1137 		if (mode == IEEE80211_MODE_11N)
1138 			continue;
1139 		/*
1140 		 * Skip over 11ac mode. Its set of channels is the set
1141 		 * of all channels supported by 11a.
1142 		 */
1143 		if (mode == IEEE80211_MODE_11AC)
1144 			continue;
1145 
1146 		/* Start over if we have already tried all modes. */
1147 		if (mode == IEEE80211_MODE_MAX) {
1148 			mode = IEEE80211_MODE_AUTO;
1149 			break;
1150 		}
1151 
1152 		if (ic->ic_modecaps & (1 << mode))
1153 			break;
1154 	}
1155 
1156 	if (mode != ic->ic_curmode)
1157 		ieee80211_setmode(ic, mode);
1158 
1159 	return (ic->ic_curmode);
1160 }
1161 
1162 /*
1163  * Return the phy mode for with the specified channel so the
1164  * caller can select a rate set.  This is problematic and the
1165  * work here assumes how things work elsewhere in this code.
1166  *
1167  * Because the result of this function is ultimately used to select a
1168  * rate from the rate set of the returned mode, it must return one of the
1169  * legacy 11a/b/g modes; 11n and 11ac modes use MCS instead of rate sets.
1170  */
1171 enum ieee80211_phymode
1172 ieee80211_chan2mode(struct ieee80211com *ic,
1173     const struct ieee80211_channel *chan)
1174 {
1175 	/*
1176 	 * Are we fixed in 11a/b/g mode?
1177 	 * NB: this assumes the channel would not be supplied to us
1178 	 *     unless it was already compatible with the current mode.
1179 	 */
1180 	if (ic->ic_curmode == IEEE80211_MODE_11A ||
1181 	    ic->ic_curmode == IEEE80211_MODE_11B ||
1182 	    ic->ic_curmode == IEEE80211_MODE_11G)
1183 		return ic->ic_curmode;
1184 
1185 	/* If no channel was provided, return the most suitable legacy mode. */
1186 	if (chan == IEEE80211_CHAN_ANYC) {
1187 		switch (ic->ic_curmode) {
1188 		case IEEE80211_MODE_AUTO:
1189 		case IEEE80211_MODE_11N:
1190 			if (ic->ic_modecaps & (1 << IEEE80211_MODE_11A))
1191 				return IEEE80211_MODE_11A;
1192 			if (ic->ic_modecaps & (1 << IEEE80211_MODE_11G))
1193 				return IEEE80211_MODE_11G;
1194 			return IEEE80211_MODE_11B;
1195 		case IEEE80211_MODE_11AC:
1196 			return IEEE80211_MODE_11A;
1197 		default:
1198 			return ic->ic_curmode;
1199 		}
1200 	}
1201 
1202 	/* Deduce a legacy mode based on the channel characteristics. */
1203 	if (IEEE80211_IS_CHAN_5GHZ(chan))
1204 		return IEEE80211_MODE_11A;
1205 	else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN))
1206 		return IEEE80211_MODE_11G;
1207 	else
1208 		return IEEE80211_MODE_11B;
1209 }
1210 
1211 /*
1212  * Convert IEEE80211 MCS index to ifmedia subtype.
1213  */
1214 uint64_t
1215 ieee80211_mcs2media(struct ieee80211com *ic, int mcs,
1216     enum ieee80211_phymode mode)
1217 {
1218 	switch (mode) {
1219 	case IEEE80211_MODE_11A:
1220 	case IEEE80211_MODE_11B:
1221 	case IEEE80211_MODE_11G:
1222 		/* these modes use rates, not MCS */
1223 		panic("%s: unexpected mode %d", __func__, mode);
1224 		break;
1225 	case IEEE80211_MODE_11N:
1226 		if (mcs >= 0 && mcs < IEEE80211_HT_NUM_MCS)
1227 			return (IFM_IEEE80211_11N |
1228 			    (IFM_IEEE80211_HT_MCS0 + mcs));
1229 		break;
1230 	case IEEE80211_MODE_11AC:
1231 		if (mcs >= 0 && mcs < IEEE80211_VHT_NUM_MCS)
1232 			return (IFM_IEEE80211_11AC |
1233 			    (IFM_IEEE80211_VHT_MCS0 + mcs));
1234 		break;
1235 	case IEEE80211_MODE_AUTO:
1236 		break;
1237 	}
1238 
1239 	return IFM_AUTO;
1240 }
1241 
1242 /*
1243  * Convert ifmedia subtype to IEEE80211 MCS index.
1244  */
1245 int
1246 ieee80211_media2mcs(uint64_t mword)
1247 {
1248 	uint64_t subtype;
1249 
1250 	subtype = IFM_SUBTYPE(mword);
1251 
1252 	if (subtype == IFM_AUTO)
1253 		return -1;
1254 	else if (subtype == IFM_MANUAL || subtype == IFM_NONE)
1255 		return 0;
1256 
1257 	if (subtype >= IFM_IEEE80211_HT_MCS0 &&
1258 	    subtype <= IFM_IEEE80211_HT_MCS76)
1259 		return (int)(subtype - IFM_IEEE80211_HT_MCS0);
1260 
1261 	if (subtype >= IFM_IEEE80211_VHT_MCS0 &&
1262 	    subtype <= IFM_IEEE80211_VHT_MCS9)
1263 		return (int)(subtype - IFM_IEEE80211_VHT_MCS0);
1264 
1265 	return -1;
1266 }
1267 
1268 /*
1269  * convert IEEE80211 rate value to ifmedia subtype.
1270  * ieee80211 rate is in unit of 0.5Mbps.
1271  */
1272 uint64_t
1273 ieee80211_rate2media(struct ieee80211com *ic, int rate,
1274     enum ieee80211_phymode mode)
1275 {
1276 	static const struct {
1277 		uint64_t	m;	/* rate + mode */
1278 		uint64_t	r;	/* if_media rate */
1279 	} rates[] = {
1280 		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1281 		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1282 		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1283 		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1284 		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1285 		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1286 		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1287 		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1288 		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1289 		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1290 		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1291 		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1292 		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1293 		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1294 		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1295 		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1296 		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1297 		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1298 		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1299 		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1300 		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1301 		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1302 		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1303 		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1304 		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1305 		/* NB: OFDM72 doesn't really exist so we don't handle it */
1306 	};
1307 	uint64_t mask;
1308 	int i;
1309 
1310 	mask = rate & IEEE80211_RATE_VAL;
1311 	switch (mode) {
1312 	case IEEE80211_MODE_11A:
1313 		mask |= IFM_IEEE80211_11A;
1314 		break;
1315 	case IEEE80211_MODE_11B:
1316 		mask |= IFM_IEEE80211_11B;
1317 		break;
1318 	case IEEE80211_MODE_AUTO:
1319 		/* NB: hack, 11g matches both 11b+11a rates */
1320 		/* FALLTHROUGH */
1321 	case IEEE80211_MODE_11G:
1322 		mask |= IFM_IEEE80211_11G;
1323 		break;
1324 	case IEEE80211_MODE_11N:
1325 	case IEEE80211_MODE_11AC:
1326 		/* 11n/11ac uses MCS, not rates. */
1327 		panic("%s: unexpected mode %d", __func__, mode);
1328 		break;
1329 	}
1330 	for (i = 0; i < nitems(rates); i++)
1331 		if (rates[i].m == mask)
1332 			return rates[i].r;
1333 	return IFM_AUTO;
1334 }
1335 
1336 int
1337 ieee80211_media2rate(uint64_t mword)
1338 {
1339 	int i;
1340 	static const struct {
1341 		uint64_t subtype;
1342 		int rate;
1343 	} ieeerates[] = {
1344 		{ IFM_AUTO,		-1	},
1345 		{ IFM_MANUAL,		0	},
1346 		{ IFM_NONE,		0	},
1347 		{ IFM_IEEE80211_DS1,	2	},
1348 		{ IFM_IEEE80211_DS2,	4	},
1349 		{ IFM_IEEE80211_DS5,	11	},
1350 		{ IFM_IEEE80211_DS11,	22	},
1351 		{ IFM_IEEE80211_DS22,	44	},
1352 		{ IFM_IEEE80211_OFDM6,	12	},
1353 		{ IFM_IEEE80211_OFDM9,	18	},
1354 		{ IFM_IEEE80211_OFDM12,	24	},
1355 		{ IFM_IEEE80211_OFDM18,	36	},
1356 		{ IFM_IEEE80211_OFDM24,	48	},
1357 		{ IFM_IEEE80211_OFDM36,	72	},
1358 		{ IFM_IEEE80211_OFDM48,	96	},
1359 		{ IFM_IEEE80211_OFDM54,	108	},
1360 		{ IFM_IEEE80211_OFDM72,	144	},
1361 	};
1362 	for (i = 0; i < nitems(ieeerates); i++) {
1363 		if (ieeerates[i].subtype == IFM_SUBTYPE(mword))
1364 			return ieeerates[i].rate;
1365 	}
1366 	return 0;
1367 }
1368 
1369 /*
1370  * Convert bit rate (in 0.5Mbps units) to PLCP signal (R4-R1) and vice versa.
1371  */
1372 u_int8_t
1373 ieee80211_rate2plcp(u_int8_t rate, enum ieee80211_phymode mode)
1374 {
1375 	rate &= IEEE80211_RATE_VAL;
1376 
1377 	if (mode == IEEE80211_MODE_11B) {
1378 		/* IEEE Std 802.11b-1999 page 15, subclause 18.2.3.3 */
1379 		switch (rate) {
1380 		case 2:		return 10;
1381 		case 4:		return 20;
1382 		case 11:	return 55;
1383 		case 22:	return 110;
1384 		/* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */
1385 		case 44:	return 220;
1386 		}
1387 	} else if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11A) {
1388 		/* IEEE Std 802.11a-1999 page 14, subclause 17.3.4.1 */
1389 		switch (rate) {
1390 		case 12:	return 0x0b;
1391 		case 18:	return 0x0f;
1392 		case 24:	return 0x0a;
1393 		case 36:	return 0x0e;
1394 		case 48:	return 0x09;
1395 		case 72:	return 0x0d;
1396 		case 96:	return 0x08;
1397 		case 108:	return 0x0c;
1398 		}
1399         } else
1400 		panic("%s: unexpected mode %u", __func__, mode);
1401 
1402 	DPRINTF(("unsupported rate %u\n", rate));
1403 
1404 	return 0;
1405 }
1406 
1407 u_int8_t
1408 ieee80211_plcp2rate(u_int8_t plcp, enum ieee80211_phymode mode)
1409 {
1410 	if (mode == IEEE80211_MODE_11B) {
1411 		/* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */
1412 		switch (plcp) {
1413 		case 10:	return 2;
1414 		case 20:	return 4;
1415 		case 55:	return 11;
1416 		case 110:	return 22;
1417 		/* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */
1418 		case 220:	return 44;
1419 		}
1420 	} else if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11A) {
1421 		/* IEEE Std 802.11a-1999 page 14, subclause 17.3.4.1 */
1422 		switch (plcp) {
1423 		case 0x0b:	return 12;
1424 		case 0x0f:	return 18;
1425 		case 0x0a:	return 24;
1426 		case 0x0e:	return 36;
1427 		case 0x09:	return 48;
1428 		case 0x0d:	return 72;
1429 		case 0x08:	return 96;
1430 		case 0x0c:	return 108;
1431 		}
1432 	} else
1433 		panic("%s: unexpected mode %u", __func__, mode);
1434 
1435 	DPRINTF(("unsupported plcp %u\n", plcp));
1436 
1437 	return 0;
1438 }
1439