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