xref: /freebsd/sys/net80211/ieee80211.c (revision cc349066556bcdeed0d6cc72aad340d0f383e35c)
1 /*-
2  * Copyright (c) 2001 Atsushi Onoe
3  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 /*
31  * IEEE 802.11 generic handler
32  */
33 #include "opt_wlan.h"
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/socket.h>
40 #include <sys/sbuf.h>
41 
42 #include <machine/stdarg.h>
43 
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/if_dl.h>
47 #include <net/if_media.h>
48 #include <net/if_types.h>
49 #include <net/ethernet.h>
50 
51 #include <net80211/ieee80211_var.h>
52 #include <net80211/ieee80211_regdomain.h>
53 #ifdef IEEE80211_SUPPORT_SUPERG
54 #include <net80211/ieee80211_superg.h>
55 #endif
56 #include <net80211/ieee80211_ratectl.h>
57 
58 #include <net/bpf.h>
59 
60 const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
61 	[IEEE80211_MODE_AUTO]	  = "auto",
62 	[IEEE80211_MODE_11A]	  = "11a",
63 	[IEEE80211_MODE_11B]	  = "11b",
64 	[IEEE80211_MODE_11G]	  = "11g",
65 	[IEEE80211_MODE_FH]	  = "FH",
66 	[IEEE80211_MODE_TURBO_A]  = "turboA",
67 	[IEEE80211_MODE_TURBO_G]  = "turboG",
68 	[IEEE80211_MODE_STURBO_A] = "sturboA",
69 	[IEEE80211_MODE_HALF]	  = "half",
70 	[IEEE80211_MODE_QUARTER]  = "quarter",
71 	[IEEE80211_MODE_11NA]	  = "11na",
72 	[IEEE80211_MODE_11NG]	  = "11ng",
73 };
74 /* map ieee80211_opmode to the corresponding capability bit */
75 const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
76 	[IEEE80211_M_IBSS]	= IEEE80211_C_IBSS,
77 	[IEEE80211_M_WDS]	= IEEE80211_C_WDS,
78 	[IEEE80211_M_STA]	= IEEE80211_C_STA,
79 	[IEEE80211_M_AHDEMO]	= IEEE80211_C_AHDEMO,
80 	[IEEE80211_M_HOSTAP]	= IEEE80211_C_HOSTAP,
81 	[IEEE80211_M_MONITOR]	= IEEE80211_C_MONITOR,
82 #ifdef IEEE80211_SUPPORT_MESH
83 	[IEEE80211_M_MBSS]	= IEEE80211_C_MBSS,
84 #endif
85 };
86 
87 const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
88 	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
89 
90 static	void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
91 static	void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
92 static	void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
93 static	int ieee80211_media_setup(struct ieee80211com *ic,
94 		struct ifmedia *media, int caps, int addsta,
95 		ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
96 static	int media_status(enum ieee80211_opmode,
97 		const struct ieee80211_channel *);
98 static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
99 
100 MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
101 
102 /*
103  * Default supported rates for 802.11 operation (in IEEE .5Mb units).
104  */
105 #define	B(r)	((r) | IEEE80211_RATE_BASIC)
106 static const struct ieee80211_rateset ieee80211_rateset_11a =
107 	{ 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
108 static const struct ieee80211_rateset ieee80211_rateset_half =
109 	{ 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
110 static const struct ieee80211_rateset ieee80211_rateset_quarter =
111 	{ 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
112 static const struct ieee80211_rateset ieee80211_rateset_11b =
113 	{ 4, { B(2), B(4), B(11), B(22) } };
114 /* NB: OFDM rates are handled specially based on mode */
115 static const struct ieee80211_rateset ieee80211_rateset_11g =
116 	{ 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
117 #undef B
118 
119 /*
120  * Fill in 802.11 available channel set, mark
121  * all available channels as active, and pick
122  * a default channel if not already specified.
123  */
124 void
125 ieee80211_chan_init(struct ieee80211com *ic)
126 {
127 #define	DEFAULTRATES(m, def) do { \
128 	if (ic->ic_sup_rates[m].rs_nrates == 0) \
129 		ic->ic_sup_rates[m] = def; \
130 } while (0)
131 	struct ieee80211_channel *c;
132 	int i;
133 
134 	KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
135 		("invalid number of channels specified: %u", ic->ic_nchans));
136 	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
137 	memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
138 	setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
139 	for (i = 0; i < ic->ic_nchans; i++) {
140 		c = &ic->ic_channels[i];
141 		KASSERT(c->ic_flags != 0, ("channel with no flags"));
142 		/*
143 		 * Help drivers that work only with frequencies by filling
144 		 * in IEEE channel #'s if not already calculated.  Note this
145 		 * mimics similar work done in ieee80211_setregdomain when
146 		 * changing regulatory state.
147 		 */
148 		if (c->ic_ieee == 0)
149 			c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
150 		if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
151 			c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
152 			    (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
153 			    c->ic_flags);
154 		/* default max tx power to max regulatory */
155 		if (c->ic_maxpower == 0)
156 			c->ic_maxpower = 2*c->ic_maxregpower;
157 		setbit(ic->ic_chan_avail, c->ic_ieee);
158 		/*
159 		 * Identify mode capabilities.
160 		 */
161 		if (IEEE80211_IS_CHAN_A(c))
162 			setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
163 		if (IEEE80211_IS_CHAN_B(c))
164 			setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
165 		if (IEEE80211_IS_CHAN_ANYG(c))
166 			setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
167 		if (IEEE80211_IS_CHAN_FHSS(c))
168 			setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
169 		if (IEEE80211_IS_CHAN_108A(c))
170 			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
171 		if (IEEE80211_IS_CHAN_108G(c))
172 			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
173 		if (IEEE80211_IS_CHAN_ST(c))
174 			setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
175 		if (IEEE80211_IS_CHAN_HALF(c))
176 			setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
177 		if (IEEE80211_IS_CHAN_QUARTER(c))
178 			setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
179 		if (IEEE80211_IS_CHAN_HTA(c))
180 			setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
181 		if (IEEE80211_IS_CHAN_HTG(c))
182 			setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
183 	}
184 	/* initialize candidate channels to all available */
185 	memcpy(ic->ic_chan_active, ic->ic_chan_avail,
186 		sizeof(ic->ic_chan_avail));
187 
188 	/* sort channel table to allow lookup optimizations */
189 	ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
190 
191 	/* invalidate any previous state */
192 	ic->ic_bsschan = IEEE80211_CHAN_ANYC;
193 	ic->ic_prevchan = NULL;
194 	ic->ic_csa_newchan = NULL;
195 	/* arbitrarily pick the first channel */
196 	ic->ic_curchan = &ic->ic_channels[0];
197 	ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
198 
199 	/* fillin well-known rate sets if driver has not specified */
200 	DEFAULTRATES(IEEE80211_MODE_11B,	 ieee80211_rateset_11b);
201 	DEFAULTRATES(IEEE80211_MODE_11G,	 ieee80211_rateset_11g);
202 	DEFAULTRATES(IEEE80211_MODE_11A,	 ieee80211_rateset_11a);
203 	DEFAULTRATES(IEEE80211_MODE_TURBO_A,	 ieee80211_rateset_11a);
204 	DEFAULTRATES(IEEE80211_MODE_TURBO_G,	 ieee80211_rateset_11g);
205 	DEFAULTRATES(IEEE80211_MODE_STURBO_A,	 ieee80211_rateset_11a);
206 	DEFAULTRATES(IEEE80211_MODE_HALF,	 ieee80211_rateset_half);
207 	DEFAULTRATES(IEEE80211_MODE_QUARTER,	 ieee80211_rateset_quarter);
208 	DEFAULTRATES(IEEE80211_MODE_11NA,	 ieee80211_rateset_11a);
209 	DEFAULTRATES(IEEE80211_MODE_11NG,	 ieee80211_rateset_11g);
210 
211 	/*
212 	 * Setup required information to fill the mcsset field, if driver did
213 	 * not. Assume a 2T2R setup for historic reasons.
214 	 */
215 	if (ic->ic_rxstream == 0)
216 		ic->ic_rxstream = 2;
217 	if (ic->ic_txstream == 0)
218 		ic->ic_txstream = 2;
219 
220 	/*
221 	 * Set auto mode to reset active channel state and any desired channel.
222 	 */
223 	(void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
224 #undef DEFAULTRATES
225 }
226 
227 static void
228 null_update_mcast(struct ieee80211com *ic)
229 {
230 
231 	ic_printf(ic, "need multicast update callback\n");
232 }
233 
234 static void
235 null_update_promisc(struct ieee80211com *ic)
236 {
237 
238 	ic_printf(ic, "need promiscuous mode update callback\n");
239 }
240 
241 static void
242 null_update_chw(struct ieee80211com *ic)
243 {
244 
245 	ic_printf(ic, "%s: need callback\n", __func__);
246 }
247 
248 int
249 ic_printf(struct ieee80211com *ic, const char * fmt, ...)
250 {
251 	va_list ap;
252 	int retval;
253 
254 	retval = printf("%s: ", ic->ic_name);
255 	va_start(ap, fmt);
256 	retval += vprintf(fmt, ap);
257 	va_end(ap);
258 	return (retval);
259 }
260 
261 static LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head);
262 static struct mtx ic_list_mtx;
263 MTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF);
264 
265 static int
266 sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS)
267 {
268 	struct ieee80211com *ic;
269 	struct sbuf sb;
270 	char *sp;
271 	int error;
272 
273 	error = sysctl_wire_old_buffer(req, 0);
274 	if (error)
275 		return (error);
276 	sbuf_new_for_sysctl(&sb, NULL, 8, req);
277 	sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
278 	sp = "";
279 	mtx_lock(&ic_list_mtx);
280 	LIST_FOREACH(ic, &ic_head, ic_next) {
281 		sbuf_printf(&sb, "%s%s", sp, ic->ic_name);
282 		sp = " ";
283 	}
284 	mtx_unlock(&ic_list_mtx);
285 	error = sbuf_finish(&sb);
286 	sbuf_delete(&sb);
287 	return (error);
288 }
289 
290 SYSCTL_PROC(_net_wlan, OID_AUTO, devices,
291     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
292     sysctl_ieee80211coms, "A", "names of available 802.11 devices");
293 
294 /*
295  * Attach/setup the common net80211 state.  Called by
296  * the driver on attach to prior to creating any vap's.
297  */
298 void
299 ieee80211_ifattach(struct ieee80211com *ic)
300 {
301 
302 	IEEE80211_LOCK_INIT(ic, ic->ic_name);
303 	IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
304 	TAILQ_INIT(&ic->ic_vaps);
305 
306 	/* Create a taskqueue for all state changes */
307 	ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
308 	    taskqueue_thread_enqueue, &ic->ic_tq);
309 	taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
310 	    ic->ic_name);
311 	ic->ic_ierrors = counter_u64_alloc(M_WAITOK);
312 	ic->ic_oerrors = counter_u64_alloc(M_WAITOK);
313 	/*
314 	 * Fill in 802.11 available channel set, mark all
315 	 * available channels as active, and pick a default
316 	 * channel if not already specified.
317 	 */
318 	ieee80211_chan_init(ic);
319 
320 	ic->ic_update_mcast = null_update_mcast;
321 	ic->ic_update_promisc = null_update_promisc;
322 	ic->ic_update_chw = null_update_chw;
323 
324 	ic->ic_hash_key = arc4random();
325 	ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
326 	ic->ic_lintval = ic->ic_bintval;
327 	ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
328 
329 	ieee80211_crypto_attach(ic);
330 	ieee80211_node_attach(ic);
331 	ieee80211_power_attach(ic);
332 	ieee80211_proto_attach(ic);
333 #ifdef IEEE80211_SUPPORT_SUPERG
334 	ieee80211_superg_attach(ic);
335 #endif
336 	ieee80211_ht_attach(ic);
337 	ieee80211_scan_attach(ic);
338 	ieee80211_regdomain_attach(ic);
339 	ieee80211_dfs_attach(ic);
340 
341 	ieee80211_sysctl_attach(ic);
342 
343 	mtx_lock(&ic_list_mtx);
344 	LIST_INSERT_HEAD(&ic_head, ic, ic_next);
345 	mtx_unlock(&ic_list_mtx);
346 }
347 
348 /*
349  * Detach net80211 state on device detach.  Tear down
350  * all vap's and reclaim all common state prior to the
351  * device state going away.  Note we may call back into
352  * driver; it must be prepared for this.
353  */
354 void
355 ieee80211_ifdetach(struct ieee80211com *ic)
356 {
357 	struct ieee80211vap *vap;
358 
359 	mtx_lock(&ic_list_mtx);
360 	LIST_REMOVE(ic, ic_next);
361 	mtx_unlock(&ic_list_mtx);
362 
363 	taskqueue_drain(taskqueue_thread, &ic->ic_restart_task);
364 
365 	/*
366 	 * The VAP is responsible for setting and clearing
367 	 * the VIMAGE context.
368 	 */
369 	while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
370 		ieee80211_vap_destroy(vap);
371 	ieee80211_waitfor_parent(ic);
372 
373 	ieee80211_sysctl_detach(ic);
374 	ieee80211_dfs_detach(ic);
375 	ieee80211_regdomain_detach(ic);
376 	ieee80211_scan_detach(ic);
377 #ifdef IEEE80211_SUPPORT_SUPERG
378 	ieee80211_superg_detach(ic);
379 #endif
380 	ieee80211_ht_detach(ic);
381 	/* NB: must be called before ieee80211_node_detach */
382 	ieee80211_proto_detach(ic);
383 	ieee80211_crypto_detach(ic);
384 	ieee80211_power_detach(ic);
385 	ieee80211_node_detach(ic);
386 
387 	counter_u64_free(ic->ic_ierrors);
388 	counter_u64_free(ic->ic_oerrors);
389 
390 	taskqueue_free(ic->ic_tq);
391 	IEEE80211_TX_LOCK_DESTROY(ic);
392 	IEEE80211_LOCK_DESTROY(ic);
393 }
394 
395 struct ieee80211com *
396 ieee80211_find_com(const char *name)
397 {
398 	struct ieee80211com *ic;
399 
400 	mtx_lock(&ic_list_mtx);
401 	LIST_FOREACH(ic, &ic_head, ic_next)
402 		if (strcmp(ic->ic_name, name) == 0)
403 			break;
404 	mtx_unlock(&ic_list_mtx);
405 
406 	return (ic);
407 }
408 
409 void
410 ieee80211_iterate_coms(ieee80211_com_iter_func *f, void *arg)
411 {
412 	struct ieee80211com *ic;
413 
414 	mtx_lock(&ic_list_mtx);
415 	LIST_FOREACH(ic, &ic_head, ic_next)
416 		(*f)(arg, ic);
417 	mtx_unlock(&ic_list_mtx);
418 }
419 
420 /*
421  * Default reset method for use with the ioctl support.  This
422  * method is invoked after any state change in the 802.11
423  * layer that should be propagated to the hardware but not
424  * require re-initialization of the 802.11 state machine (e.g
425  * rescanning for an ap).  We always return ENETRESET which
426  * should cause the driver to re-initialize the device. Drivers
427  * can override this method to implement more optimized support.
428  */
429 static int
430 default_reset(struct ieee80211vap *vap, u_long cmd)
431 {
432 	return ENETRESET;
433 }
434 
435 /*
436  * Default for updating the VAP default TX key index.
437  *
438  * Drivers that support TX offload as well as hardware encryption offload
439  * may need to be informed of key index changes separate from the key
440  * update.
441  */
442 static void
443 default_update_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid)
444 {
445 
446 	/* XXX assert validity */
447 	/* XXX assert we're in a key update block */
448 	vap->iv_def_txkey = kid;
449 }
450 
451 /*
452  * Add underlying device errors to vap errors.
453  */
454 static uint64_t
455 ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
456 {
457 	struct ieee80211vap *vap = ifp->if_softc;
458 	struct ieee80211com *ic = vap->iv_ic;
459 	uint64_t rv;
460 
461 	rv = if_get_counter_default(ifp, cnt);
462 	switch (cnt) {
463 	case IFCOUNTER_OERRORS:
464 		rv += counter_u64_fetch(ic->ic_oerrors);
465 		break;
466 	case IFCOUNTER_IERRORS:
467 		rv += counter_u64_fetch(ic->ic_ierrors);
468 		break;
469 	default:
470 		break;
471 	}
472 
473 	return (rv);
474 }
475 
476 /*
477  * Prepare a vap for use.  Drivers use this call to
478  * setup net80211 state in new vap's prior attaching
479  * them with ieee80211_vap_attach (below).
480  */
481 int
482 ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
483     const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
484     int flags, const uint8_t bssid[IEEE80211_ADDR_LEN])
485 {
486 	struct ifnet *ifp;
487 
488 	ifp = if_alloc(IFT_ETHER);
489 	if (ifp == NULL) {
490 		ic_printf(ic, "%s: unable to allocate ifnet\n",
491 		    __func__);
492 		return ENOMEM;
493 	}
494 	if_initname(ifp, name, unit);
495 	ifp->if_softc = vap;			/* back pointer */
496 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
497 	ifp->if_transmit = ieee80211_vap_transmit;
498 	ifp->if_qflush = ieee80211_vap_qflush;
499 	ifp->if_ioctl = ieee80211_ioctl;
500 	ifp->if_init = ieee80211_init;
501 	ifp->if_get_counter = ieee80211_get_counter;
502 
503 	vap->iv_ifp = ifp;
504 	vap->iv_ic = ic;
505 	vap->iv_flags = ic->ic_flags;		/* propagate common flags */
506 	vap->iv_flags_ext = ic->ic_flags_ext;
507 	vap->iv_flags_ven = ic->ic_flags_ven;
508 	vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
509 	vap->iv_htcaps = ic->ic_htcaps;
510 	vap->iv_htextcaps = ic->ic_htextcaps;
511 	vap->iv_opmode = opmode;
512 	vap->iv_caps |= ieee80211_opcap[opmode];
513 	IEEE80211_ADDR_COPY(vap->iv_myaddr, ic->ic_macaddr);
514 	switch (opmode) {
515 	case IEEE80211_M_WDS:
516 		/*
517 		 * WDS links must specify the bssid of the far end.
518 		 * For legacy operation this is a static relationship.
519 		 * For non-legacy operation the station must associate
520 		 * and be authorized to pass traffic.  Plumbing the
521 		 * vap to the proper node happens when the vap
522 		 * transitions to RUN state.
523 		 */
524 		IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
525 		vap->iv_flags |= IEEE80211_F_DESBSSID;
526 		if (flags & IEEE80211_CLONE_WDSLEGACY)
527 			vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
528 		break;
529 #ifdef IEEE80211_SUPPORT_TDMA
530 	case IEEE80211_M_AHDEMO:
531 		if (flags & IEEE80211_CLONE_TDMA) {
532 			/* NB: checked before clone operation allowed */
533 			KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
534 			    ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
535 			/*
536 			 * Propagate TDMA capability to mark vap; this
537 			 * cannot be removed and is used to distinguish
538 			 * regular ahdemo operation from ahdemo+tdma.
539 			 */
540 			vap->iv_caps |= IEEE80211_C_TDMA;
541 		}
542 		break;
543 #endif
544 	default:
545 		break;
546 	}
547 	/* auto-enable s/w beacon miss support */
548 	if (flags & IEEE80211_CLONE_NOBEACONS)
549 		vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
550 	/* auto-generated or user supplied MAC address */
551 	if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
552 		vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
553 	/*
554 	 * Enable various functionality by default if we're
555 	 * capable; the driver can override us if it knows better.
556 	 */
557 	if (vap->iv_caps & IEEE80211_C_WME)
558 		vap->iv_flags |= IEEE80211_F_WME;
559 	if (vap->iv_caps & IEEE80211_C_BURST)
560 		vap->iv_flags |= IEEE80211_F_BURST;
561 	/* NB: bg scanning only makes sense for station mode right now */
562 	if (vap->iv_opmode == IEEE80211_M_STA &&
563 	    (vap->iv_caps & IEEE80211_C_BGSCAN))
564 		vap->iv_flags |= IEEE80211_F_BGSCAN;
565 	vap->iv_flags |= IEEE80211_F_DOTH;	/* XXX no cap, just ena */
566 	/* NB: DFS support only makes sense for ap mode right now */
567 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
568 	    (vap->iv_caps & IEEE80211_C_DFS))
569 		vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
570 
571 	vap->iv_des_chan = IEEE80211_CHAN_ANYC;		/* any channel is ok */
572 	vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
573 	vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
574 	/*
575 	 * Install a default reset method for the ioctl support;
576 	 * the driver can override this.
577 	 */
578 	vap->iv_reset = default_reset;
579 
580 	/*
581 	 * Install a default crypto key update method, the driver
582 	 * can override this.
583 	 */
584 	vap->iv_update_deftxkey = default_update_deftxkey;
585 
586 	ieee80211_sysctl_vattach(vap);
587 	ieee80211_crypto_vattach(vap);
588 	ieee80211_node_vattach(vap);
589 	ieee80211_power_vattach(vap);
590 	ieee80211_proto_vattach(vap);
591 #ifdef IEEE80211_SUPPORT_SUPERG
592 	ieee80211_superg_vattach(vap);
593 #endif
594 	ieee80211_ht_vattach(vap);
595 	ieee80211_scan_vattach(vap);
596 	ieee80211_regdomain_vattach(vap);
597 	ieee80211_radiotap_vattach(vap);
598 	ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
599 
600 	return 0;
601 }
602 
603 /*
604  * Activate a vap.  State should have been prepared with a
605  * call to ieee80211_vap_setup and by the driver.  On return
606  * from this call the vap is ready for use.
607  */
608 int
609 ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change,
610     ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN])
611 {
612 	struct ifnet *ifp = vap->iv_ifp;
613 	struct ieee80211com *ic = vap->iv_ic;
614 	struct ifmediareq imr;
615 	int maxrate;
616 
617 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
618 	    "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
619 	    __func__, ieee80211_opmode_name[vap->iv_opmode],
620 	    ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
621 
622 	/*
623 	 * Do late attach work that cannot happen until after
624 	 * the driver has had a chance to override defaults.
625 	 */
626 	ieee80211_node_latevattach(vap);
627 	ieee80211_power_latevattach(vap);
628 
629 	maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
630 	    vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
631 	ieee80211_media_status(ifp, &imr);
632 	/* NB: strip explicit mode; we're actually in autoselect */
633 	ifmedia_set(&vap->iv_media,
634 	    imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
635 	if (maxrate)
636 		ifp->if_baudrate = IF_Mbps(maxrate);
637 
638 	ether_ifattach(ifp, macaddr);
639 	IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
640 	/* hook output method setup by ether_ifattach */
641 	vap->iv_output = ifp->if_output;
642 	ifp->if_output = ieee80211_output;
643 	/* NB: if_mtu set by ether_ifattach to ETHERMTU */
644 
645 	IEEE80211_LOCK(ic);
646 	TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
647 	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
648 #ifdef IEEE80211_SUPPORT_SUPERG
649 	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
650 #endif
651 	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
652 	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
653 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
654 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
655 	IEEE80211_UNLOCK(ic);
656 
657 	return 1;
658 }
659 
660 /*
661  * Tear down vap state and reclaim the ifnet.
662  * The driver is assumed to have prepared for
663  * this; e.g. by turning off interrupts for the
664  * underlying device.
665  */
666 void
667 ieee80211_vap_detach(struct ieee80211vap *vap)
668 {
669 	struct ieee80211com *ic = vap->iv_ic;
670 	struct ifnet *ifp = vap->iv_ifp;
671 
672 	CURVNET_SET(ifp->if_vnet);
673 
674 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
675 	    __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
676 
677 	/* NB: bpfdetach is called by ether_ifdetach and claims all taps */
678 	ether_ifdetach(ifp);
679 
680 	ieee80211_stop(vap);
681 
682 	/*
683 	 * Flush any deferred vap tasks.
684 	 */
685 	ieee80211_draintask(ic, &vap->iv_nstate_task);
686 	ieee80211_draintask(ic, &vap->iv_swbmiss_task);
687 
688 	/* XXX band-aid until ifnet handles this for us */
689 	taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
690 
691 	IEEE80211_LOCK(ic);
692 	KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
693 	TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
694 	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
695 #ifdef IEEE80211_SUPPORT_SUPERG
696 	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
697 #endif
698 	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
699 	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
700 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
701 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
702 	/* NB: this handles the bpfdetach done below */
703 	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
704 	if (vap->iv_ifflags & IFF_PROMISC)
705 		ieee80211_promisc(vap, false);
706 	if (vap->iv_ifflags & IFF_ALLMULTI)
707 		ieee80211_allmulti(vap, false);
708 	IEEE80211_UNLOCK(ic);
709 
710 	ifmedia_removeall(&vap->iv_media);
711 
712 	ieee80211_radiotap_vdetach(vap);
713 	ieee80211_regdomain_vdetach(vap);
714 	ieee80211_scan_vdetach(vap);
715 #ifdef IEEE80211_SUPPORT_SUPERG
716 	ieee80211_superg_vdetach(vap);
717 #endif
718 	ieee80211_ht_vdetach(vap);
719 	/* NB: must be before ieee80211_node_vdetach */
720 	ieee80211_proto_vdetach(vap);
721 	ieee80211_crypto_vdetach(vap);
722 	ieee80211_power_vdetach(vap);
723 	ieee80211_node_vdetach(vap);
724 	ieee80211_sysctl_vdetach(vap);
725 
726 	if_free(ifp);
727 
728 	CURVNET_RESTORE();
729 }
730 
731 /*
732  * Count number of vaps in promisc, and issue promisc on
733  * parent respectively.
734  */
735 void
736 ieee80211_promisc(struct ieee80211vap *vap, bool on)
737 {
738 	struct ieee80211com *ic = vap->iv_ic;
739 
740 	IEEE80211_LOCK_ASSERT(ic);
741 
742 	if (on) {
743 		if (++ic->ic_promisc == 1)
744 			ieee80211_runtask(ic, &ic->ic_promisc_task);
745 	} else {
746 		KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc",
747 		    __func__, ic));
748 		if (--ic->ic_promisc == 0)
749 			ieee80211_runtask(ic, &ic->ic_promisc_task);
750 	}
751 }
752 
753 /*
754  * Count number of vaps in allmulti, and issue allmulti on
755  * parent respectively.
756  */
757 void
758 ieee80211_allmulti(struct ieee80211vap *vap, bool on)
759 {
760 	struct ieee80211com *ic = vap->iv_ic;
761 
762 	IEEE80211_LOCK_ASSERT(ic);
763 
764 	if (on) {
765 		if (++ic->ic_allmulti == 1)
766 			ieee80211_runtask(ic, &ic->ic_mcast_task);
767 	} else {
768 		KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti",
769 		    __func__, ic));
770 		if (--ic->ic_allmulti == 0)
771 			ieee80211_runtask(ic, &ic->ic_mcast_task);
772 	}
773 }
774 
775 /*
776  * Synchronize flag bit state in the com structure
777  * according to the state of all vap's.  This is used,
778  * for example, to handle state changes via ioctls.
779  */
780 static void
781 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
782 {
783 	struct ieee80211vap *vap;
784 	int bit;
785 
786 	IEEE80211_LOCK_ASSERT(ic);
787 
788 	bit = 0;
789 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
790 		if (vap->iv_flags & flag) {
791 			bit = 1;
792 			break;
793 		}
794 	if (bit)
795 		ic->ic_flags |= flag;
796 	else
797 		ic->ic_flags &= ~flag;
798 }
799 
800 void
801 ieee80211_syncflag(struct ieee80211vap *vap, int flag)
802 {
803 	struct ieee80211com *ic = vap->iv_ic;
804 
805 	IEEE80211_LOCK(ic);
806 	if (flag < 0) {
807 		flag = -flag;
808 		vap->iv_flags &= ~flag;
809 	} else
810 		vap->iv_flags |= flag;
811 	ieee80211_syncflag_locked(ic, flag);
812 	IEEE80211_UNLOCK(ic);
813 }
814 
815 /*
816  * Synchronize flags_ht bit state in the com structure
817  * according to the state of all vap's.  This is used,
818  * for example, to handle state changes via ioctls.
819  */
820 static void
821 ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
822 {
823 	struct ieee80211vap *vap;
824 	int bit;
825 
826 	IEEE80211_LOCK_ASSERT(ic);
827 
828 	bit = 0;
829 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
830 		if (vap->iv_flags_ht & flag) {
831 			bit = 1;
832 			break;
833 		}
834 	if (bit)
835 		ic->ic_flags_ht |= flag;
836 	else
837 		ic->ic_flags_ht &= ~flag;
838 }
839 
840 void
841 ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
842 {
843 	struct ieee80211com *ic = vap->iv_ic;
844 
845 	IEEE80211_LOCK(ic);
846 	if (flag < 0) {
847 		flag = -flag;
848 		vap->iv_flags_ht &= ~flag;
849 	} else
850 		vap->iv_flags_ht |= flag;
851 	ieee80211_syncflag_ht_locked(ic, flag);
852 	IEEE80211_UNLOCK(ic);
853 }
854 
855 /*
856  * Synchronize flags_ext bit state in the com structure
857  * according to the state of all vap's.  This is used,
858  * for example, to handle state changes via ioctls.
859  */
860 static void
861 ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
862 {
863 	struct ieee80211vap *vap;
864 	int bit;
865 
866 	IEEE80211_LOCK_ASSERT(ic);
867 
868 	bit = 0;
869 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
870 		if (vap->iv_flags_ext & flag) {
871 			bit = 1;
872 			break;
873 		}
874 	if (bit)
875 		ic->ic_flags_ext |= flag;
876 	else
877 		ic->ic_flags_ext &= ~flag;
878 }
879 
880 void
881 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
882 {
883 	struct ieee80211com *ic = vap->iv_ic;
884 
885 	IEEE80211_LOCK(ic);
886 	if (flag < 0) {
887 		flag = -flag;
888 		vap->iv_flags_ext &= ~flag;
889 	} else
890 		vap->iv_flags_ext |= flag;
891 	ieee80211_syncflag_ext_locked(ic, flag);
892 	IEEE80211_UNLOCK(ic);
893 }
894 
895 static __inline int
896 mapgsm(u_int freq, u_int flags)
897 {
898 	freq *= 10;
899 	if (flags & IEEE80211_CHAN_QUARTER)
900 		freq += 5;
901 	else if (flags & IEEE80211_CHAN_HALF)
902 		freq += 10;
903 	else
904 		freq += 20;
905 	/* NB: there is no 907/20 wide but leave room */
906 	return (freq - 906*10) / 5;
907 }
908 
909 static __inline int
910 mappsb(u_int freq, u_int flags)
911 {
912 	return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
913 }
914 
915 /*
916  * Convert MHz frequency to IEEE channel number.
917  */
918 int
919 ieee80211_mhz2ieee(u_int freq, u_int flags)
920 {
921 #define	IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
922 	if (flags & IEEE80211_CHAN_GSM)
923 		return mapgsm(freq, flags);
924 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
925 		if (freq == 2484)
926 			return 14;
927 		if (freq < 2484)
928 			return ((int) freq - 2407) / 5;
929 		else
930 			return 15 + ((freq - 2512) / 20);
931 	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
932 		if (freq <= 5000) {
933 			/* XXX check regdomain? */
934 			if (IS_FREQ_IN_PSB(freq))
935 				return mappsb(freq, flags);
936 			return (freq - 4000) / 5;
937 		} else
938 			return (freq - 5000) / 5;
939 	} else {				/* either, guess */
940 		if (freq == 2484)
941 			return 14;
942 		if (freq < 2484) {
943 			if (907 <= freq && freq <= 922)
944 				return mapgsm(freq, flags);
945 			return ((int) freq - 2407) / 5;
946 		}
947 		if (freq < 5000) {
948 			if (IS_FREQ_IN_PSB(freq))
949 				return mappsb(freq, flags);
950 			else if (freq > 4900)
951 				return (freq - 4000) / 5;
952 			else
953 				return 15 + ((freq - 2512) / 20);
954 		}
955 		return (freq - 5000) / 5;
956 	}
957 #undef IS_FREQ_IN_PSB
958 }
959 
960 /*
961  * Convert channel to IEEE channel number.
962  */
963 int
964 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
965 {
966 	if (c == NULL) {
967 		ic_printf(ic, "invalid channel (NULL)\n");
968 		return 0;		/* XXX */
969 	}
970 	return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
971 }
972 
973 /*
974  * Convert IEEE channel number to MHz frequency.
975  */
976 u_int
977 ieee80211_ieee2mhz(u_int chan, u_int flags)
978 {
979 	if (flags & IEEE80211_CHAN_GSM)
980 		return 907 + 5 * (chan / 10);
981 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
982 		if (chan == 14)
983 			return 2484;
984 		if (chan < 14)
985 			return 2407 + chan*5;
986 		else
987 			return 2512 + ((chan-15)*20);
988 	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
989 		if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
990 			chan -= 37;
991 			return 4940 + chan*5 + (chan % 5 ? 2 : 0);
992 		}
993 		return 5000 + (chan*5);
994 	} else {				/* either, guess */
995 		/* XXX can't distinguish PSB+GSM channels */
996 		if (chan == 14)
997 			return 2484;
998 		if (chan < 14)			/* 0-13 */
999 			return 2407 + chan*5;
1000 		if (chan < 27)			/* 15-26 */
1001 			return 2512 + ((chan-15)*20);
1002 		return 5000 + (chan*5);
1003 	}
1004 }
1005 
1006 static __inline void
1007 set_extchan(struct ieee80211_channel *c)
1008 {
1009 
1010 	/*
1011 	 * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4:
1012 	 * "the secondary channel number shall be 'N + [1,-1] * 4'
1013 	 */
1014 	if (c->ic_flags & IEEE80211_CHAN_HT40U)
1015 		c->ic_extieee = c->ic_ieee + 4;
1016 	else if (c->ic_flags & IEEE80211_CHAN_HT40D)
1017 		c->ic_extieee = c->ic_ieee - 4;
1018 	else
1019 		c->ic_extieee = 0;
1020 }
1021 
1022 static int
1023 addchan(struct ieee80211_channel chans[], int maxchans, int *nchans,
1024     uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags)
1025 {
1026 	struct ieee80211_channel *c;
1027 
1028 	if (*nchans >= maxchans)
1029 		return (ENOBUFS);
1030 
1031 	c = &chans[(*nchans)++];
1032 	c->ic_ieee = ieee;
1033 	c->ic_freq = freq != 0 ? freq : ieee80211_ieee2mhz(ieee, flags);
1034 	c->ic_maxregpower = maxregpower;
1035 	c->ic_maxpower = 2 * maxregpower;
1036 	c->ic_flags = flags;
1037 	set_extchan(c);
1038 
1039 	return (0);
1040 }
1041 
1042 static int
1043 copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans,
1044     uint32_t flags)
1045 {
1046 	struct ieee80211_channel *c;
1047 
1048 	KASSERT(*nchans > 0, ("channel list is empty\n"));
1049 
1050 	if (*nchans >= maxchans)
1051 		return (ENOBUFS);
1052 
1053 	c = &chans[(*nchans)++];
1054 	c[0] = c[-1];
1055 	c->ic_flags = flags;
1056 	set_extchan(c);
1057 
1058 	return (0);
1059 }
1060 
1061 static void
1062 getflags_2ghz(const uint8_t bands[], uint32_t flags[], int ht40)
1063 {
1064 	int nmodes;
1065 
1066 	nmodes = 0;
1067 	if (isset(bands, IEEE80211_MODE_11B))
1068 		flags[nmodes++] = IEEE80211_CHAN_B;
1069 	if (isset(bands, IEEE80211_MODE_11G))
1070 		flags[nmodes++] = IEEE80211_CHAN_G;
1071 	if (isset(bands, IEEE80211_MODE_11NG))
1072 		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
1073 	if (ht40) {
1074 		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U;
1075 		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D;
1076 	}
1077 	flags[nmodes] = 0;
1078 }
1079 
1080 static void
1081 getflags_5ghz(const uint8_t bands[], uint32_t flags[], int ht40)
1082 {
1083 	int nmodes;
1084 
1085 	nmodes = 0;
1086 	if (isset(bands, IEEE80211_MODE_11A))
1087 		flags[nmodes++] = IEEE80211_CHAN_A;
1088 	if (isset(bands, IEEE80211_MODE_11NA))
1089 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
1090 	if (ht40) {
1091 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U;
1092 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D;
1093 	}
1094 	flags[nmodes] = 0;
1095 }
1096 
1097 static void
1098 getflags(const uint8_t bands[], uint32_t flags[], int ht40)
1099 {
1100 
1101 	flags[0] = 0;
1102 	if (isset(bands, IEEE80211_MODE_11A) ||
1103 	    isset(bands, IEEE80211_MODE_11NA)) {
1104 		if (isset(bands, IEEE80211_MODE_11B) ||
1105 		    isset(bands, IEEE80211_MODE_11G) ||
1106 		    isset(bands, IEEE80211_MODE_11NG))
1107 			return;
1108 
1109 		getflags_5ghz(bands, flags, ht40);
1110 	} else
1111 		getflags_2ghz(bands, flags, ht40);
1112 }
1113 
1114 /*
1115  * Add one 20 MHz channel into specified channel list.
1116  */
1117 int
1118 ieee80211_add_channel(struct ieee80211_channel chans[], int maxchans,
1119     int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower,
1120     uint32_t chan_flags, const uint8_t bands[])
1121 {
1122 	uint32_t flags[IEEE80211_MODE_MAX];
1123 	int i, error;
1124 
1125 	getflags(bands, flags, 0);
1126 	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1127 
1128 	error = addchan(chans, maxchans, nchans, ieee, freq, maxregpower,
1129 	    flags[0] | chan_flags);
1130 	for (i = 1; flags[i] != 0 && error == 0; i++) {
1131 		error = copychan_prev(chans, maxchans, nchans,
1132 		    flags[i] | chan_flags);
1133 	}
1134 
1135 	return (error);
1136 }
1137 
1138 static struct ieee80211_channel *
1139 findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq,
1140     uint32_t flags)
1141 {
1142 	struct ieee80211_channel *c;
1143 	int i;
1144 
1145 	flags &= IEEE80211_CHAN_ALLTURBO;
1146 	/* brute force search */
1147 	for (i = 0; i < nchans; i++) {
1148 		c = &chans[i];
1149 		if (c->ic_freq == freq &&
1150 		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1151 			return c;
1152 	}
1153 	return NULL;
1154 }
1155 
1156 /*
1157  * Add 40 MHz channel pair into specified channel list.
1158  */
1159 int
1160 ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans,
1161     int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags)
1162 {
1163 	struct ieee80211_channel *cent, *extc;
1164 	uint16_t freq;
1165 	int error;
1166 
1167 	freq = ieee80211_ieee2mhz(ieee, flags);
1168 
1169 	/*
1170 	 * Each entry defines an HT40 channel pair; find the
1171 	 * center channel, then the extension channel above.
1172 	 */
1173 	flags |= IEEE80211_CHAN_HT20;
1174 	cent = findchannel(chans, *nchans, freq, flags);
1175 	if (cent == NULL)
1176 		return (EINVAL);
1177 
1178 	extc = findchannel(chans, *nchans, freq + 20, flags);
1179 	if (extc == NULL)
1180 		return (ENOENT);
1181 
1182 	flags &= ~IEEE80211_CHAN_HT;
1183 	error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq,
1184 	    maxregpower, flags | IEEE80211_CHAN_HT40U);
1185 	if (error != 0)
1186 		return (error);
1187 
1188 	error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq,
1189 	    maxregpower, flags | IEEE80211_CHAN_HT40D);
1190 
1191 	return (error);
1192 }
1193 
1194 /*
1195  * Fetch the center frequency for the primary channel.
1196  */
1197 uint32_t
1198 ieee80211_get_channel_center_freq(const struct ieee80211_channel *c)
1199 {
1200 
1201 	return (c->ic_freq);
1202 }
1203 
1204 /*
1205  * Fetch the center frequency for the primary BAND channel.
1206  *
1207  * For 5, 10, 20MHz channels it'll be the normally configured channel
1208  * frequency.
1209  *
1210  * For 40MHz, 80MHz, 160Mhz channels it'll the the centre of the
1211  * wide channel, not the centre of the primary channel (that's ic_freq).
1212  *
1213  * For 80+80MHz channels this will be the centre of the primary
1214  * 80MHz channel; the secondary 80MHz channel will be center_freq2().
1215  */
1216 
1217 uint32_t
1218 ieee80211_get_channel_center_freq1(const struct ieee80211_channel *c)
1219 {
1220 
1221 	if (IEEE80211_IS_CHAN_HT40U(c)) {
1222 		return (c->ic_freq + 10);
1223 	}
1224 	if (IEEE80211_IS_CHAN_HT40D(c)) {
1225 		return (c->ic_freq - 10);
1226 	}
1227 
1228 	return (c->ic_freq);
1229 }
1230 
1231 /*
1232  * For now, no 80+80 support; this is zero.
1233  */
1234 uint32_t
1235 ieee80211_get_channel_center_freq2(const struct ieee80211_channel *c)
1236 {
1237 
1238 	return (0);
1239 }
1240 
1241 /*
1242  * Adds channels into specified channel list (ieee[] array must be sorted).
1243  * Channels are already sorted.
1244  */
1245 static int
1246 add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans,
1247     const uint8_t ieee[], int nieee, uint32_t flags[])
1248 {
1249 	uint16_t freq;
1250 	int i, j, error;
1251 
1252 	for (i = 0; i < nieee; i++) {
1253 		freq = ieee80211_ieee2mhz(ieee[i], flags[0]);
1254 		for (j = 0; flags[j] != 0; j++) {
1255 			if (flags[j] & IEEE80211_CHAN_HT40D)
1256 				if (i == 0 || ieee[i] < ieee[0] + 4 ||
1257 				    freq - 20 !=
1258 				    ieee80211_ieee2mhz(ieee[i] - 4, flags[j]))
1259 					continue;
1260 			if (flags[j] & IEEE80211_CHAN_HT40U)
1261 				if (i == nieee - 1 ||
1262 				    ieee[i] + 4 > ieee[nieee - 1] ||
1263 				    freq + 20 !=
1264 				    ieee80211_ieee2mhz(ieee[i] + 4, flags[j]))
1265 					continue;
1266 
1267 			if (j == 0) {
1268 				error = addchan(chans, maxchans, nchans,
1269 				    ieee[i], freq, 0, flags[j]);
1270 			} else {
1271 				error = copychan_prev(chans, maxchans, nchans,
1272 				    flags[j]);
1273 			}
1274 			if (error != 0)
1275 				return (error);
1276 		}
1277 	}
1278 
1279 	return (0);
1280 }
1281 
1282 int
1283 ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[], int maxchans,
1284     int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1285     int ht40)
1286 {
1287 	uint32_t flags[IEEE80211_MODE_MAX];
1288 
1289 	getflags_2ghz(bands, flags, ht40);
1290 	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1291 
1292 	return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1293 }
1294 
1295 int
1296 ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[], int maxchans,
1297     int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1298     int ht40)
1299 {
1300 	uint32_t flags[IEEE80211_MODE_MAX];
1301 
1302 	getflags_5ghz(bands, flags, ht40);
1303 	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1304 
1305 	return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1306 }
1307 
1308 /*
1309  * Locate a channel given a frequency+flags.  We cache
1310  * the previous lookup to optimize switching between two
1311  * channels--as happens with dynamic turbo.
1312  */
1313 struct ieee80211_channel *
1314 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
1315 {
1316 	struct ieee80211_channel *c;
1317 
1318 	flags &= IEEE80211_CHAN_ALLTURBO;
1319 	c = ic->ic_prevchan;
1320 	if (c != NULL && c->ic_freq == freq &&
1321 	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1322 		return c;
1323 	/* brute force search */
1324 	return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags));
1325 }
1326 
1327 /*
1328  * Locate a channel given a channel number+flags.  We cache
1329  * the previous lookup to optimize switching between two
1330  * channels--as happens with dynamic turbo.
1331  */
1332 struct ieee80211_channel *
1333 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1334 {
1335 	struct ieee80211_channel *c;
1336 	int i;
1337 
1338 	flags &= IEEE80211_CHAN_ALLTURBO;
1339 	c = ic->ic_prevchan;
1340 	if (c != NULL && c->ic_ieee == ieee &&
1341 	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1342 		return c;
1343 	/* brute force search */
1344 	for (i = 0; i < ic->ic_nchans; i++) {
1345 		c = &ic->ic_channels[i];
1346 		if (c->ic_ieee == ieee &&
1347 		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1348 			return c;
1349 	}
1350 	return NULL;
1351 }
1352 
1353 /*
1354  * Lookup a channel suitable for the given rx status.
1355  *
1356  * This is used to find a channel for a frame (eg beacon, probe
1357  * response) based purely on the received PHY information.
1358  *
1359  * For now it tries to do it based on R_FREQ / R_IEEE.
1360  * This is enough for 11bg and 11a (and thus 11ng/11na)
1361  * but it will not be enough for GSM, PSB channels and the
1362  * like.  It also doesn't know about legacy-turbog and
1363  * legacy-turbo modes, which some offload NICs actually
1364  * support in weird ways.
1365  *
1366  * Takes the ic and rxstatus; returns the channel or NULL
1367  * if not found.
1368  *
1369  * XXX TODO: Add support for that when the need arises.
1370  */
1371 struct ieee80211_channel *
1372 ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1373     const struct ieee80211_rx_stats *rxs)
1374 {
1375 	struct ieee80211com *ic = vap->iv_ic;
1376 	uint32_t flags;
1377 	struct ieee80211_channel *c;
1378 
1379 	if (rxs == NULL)
1380 		return (NULL);
1381 
1382 	/*
1383 	 * Strictly speaking we only use freq for now,
1384 	 * however later on we may wish to just store
1385 	 * the ieee for verification.
1386 	 */
1387 	if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1388 		return (NULL);
1389 	if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1390 		return (NULL);
1391 
1392 	/*
1393 	 * If the rx status contains a valid ieee/freq, then
1394 	 * ensure we populate the correct channel information
1395 	 * in rxchan before passing it up to the scan infrastructure.
1396 	 * Offload NICs will pass up beacons from all channels
1397 	 * during background scans.
1398 	 */
1399 
1400 	/* Determine a band */
1401 	/* XXX should be done by the driver? */
1402 	if (rxs->c_freq < 3000) {
1403 		flags = IEEE80211_CHAN_G;
1404 	} else {
1405 		flags = IEEE80211_CHAN_A;
1406 	}
1407 
1408 	/* Channel lookup */
1409 	c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1410 
1411 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1412 	    "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1413 	    __func__,
1414 	    (int) rxs->c_freq,
1415 	    (int) rxs->c_ieee,
1416 	    flags,
1417 	    c);
1418 
1419 	return (c);
1420 }
1421 
1422 static void
1423 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1424 {
1425 #define	ADD(_ic, _s, _o) \
1426 	ifmedia_add(media, \
1427 		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1428 	static const u_int mopts[IEEE80211_MODE_MAX] = {
1429 	    [IEEE80211_MODE_AUTO]	= IFM_AUTO,
1430 	    [IEEE80211_MODE_11A]	= IFM_IEEE80211_11A,
1431 	    [IEEE80211_MODE_11B]	= IFM_IEEE80211_11B,
1432 	    [IEEE80211_MODE_11G]	= IFM_IEEE80211_11G,
1433 	    [IEEE80211_MODE_FH]		= IFM_IEEE80211_FH,
1434 	    [IEEE80211_MODE_TURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1435 	    [IEEE80211_MODE_TURBO_G]	= IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1436 	    [IEEE80211_MODE_STURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1437 	    [IEEE80211_MODE_HALF]	= IFM_IEEE80211_11A,	/* XXX */
1438 	    [IEEE80211_MODE_QUARTER]	= IFM_IEEE80211_11A,	/* XXX */
1439 	    [IEEE80211_MODE_11NA]	= IFM_IEEE80211_11NA,
1440 	    [IEEE80211_MODE_11NG]	= IFM_IEEE80211_11NG,
1441 	};
1442 	u_int mopt;
1443 
1444 	mopt = mopts[mode];
1445 	if (addsta)
1446 		ADD(ic, mword, mopt);	/* STA mode has no cap */
1447 	if (caps & IEEE80211_C_IBSS)
1448 		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1449 	if (caps & IEEE80211_C_HOSTAP)
1450 		ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1451 	if (caps & IEEE80211_C_AHDEMO)
1452 		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1453 	if (caps & IEEE80211_C_MONITOR)
1454 		ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1455 	if (caps & IEEE80211_C_WDS)
1456 		ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1457 	if (caps & IEEE80211_C_MBSS)
1458 		ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1459 #undef ADD
1460 }
1461 
1462 /*
1463  * Setup the media data structures according to the channel and
1464  * rate tables.
1465  */
1466 static int
1467 ieee80211_media_setup(struct ieee80211com *ic,
1468 	struct ifmedia *media, int caps, int addsta,
1469 	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1470 {
1471 	int i, j, rate, maxrate, mword, r;
1472 	enum ieee80211_phymode mode;
1473 	const struct ieee80211_rateset *rs;
1474 	struct ieee80211_rateset allrates;
1475 
1476 	/*
1477 	 * Fill in media characteristics.
1478 	 */
1479 	ifmedia_init(media, 0, media_change, media_stat);
1480 	maxrate = 0;
1481 	/*
1482 	 * Add media for legacy operating modes.
1483 	 */
1484 	memset(&allrates, 0, sizeof(allrates));
1485 	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1486 		if (isclr(ic->ic_modecaps, mode))
1487 			continue;
1488 		addmedia(media, caps, addsta, mode, IFM_AUTO);
1489 		if (mode == IEEE80211_MODE_AUTO)
1490 			continue;
1491 		rs = &ic->ic_sup_rates[mode];
1492 		for (i = 0; i < rs->rs_nrates; i++) {
1493 			rate = rs->rs_rates[i];
1494 			mword = ieee80211_rate2media(ic, rate, mode);
1495 			if (mword == 0)
1496 				continue;
1497 			addmedia(media, caps, addsta, mode, mword);
1498 			/*
1499 			 * Add legacy rate to the collection of all rates.
1500 			 */
1501 			r = rate & IEEE80211_RATE_VAL;
1502 			for (j = 0; j < allrates.rs_nrates; j++)
1503 				if (allrates.rs_rates[j] == r)
1504 					break;
1505 			if (j == allrates.rs_nrates) {
1506 				/* unique, add to the set */
1507 				allrates.rs_rates[j] = r;
1508 				allrates.rs_nrates++;
1509 			}
1510 			rate = (rate & IEEE80211_RATE_VAL) / 2;
1511 			if (rate > maxrate)
1512 				maxrate = rate;
1513 		}
1514 	}
1515 	for (i = 0; i < allrates.rs_nrates; i++) {
1516 		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1517 				IEEE80211_MODE_AUTO);
1518 		if (mword == 0)
1519 			continue;
1520 		/* NB: remove media options from mword */
1521 		addmedia(media, caps, addsta,
1522 		    IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1523 	}
1524 	/*
1525 	 * Add HT/11n media.  Note that we do not have enough
1526 	 * bits in the media subtype to express the MCS so we
1527 	 * use a "placeholder" media subtype and any fixed MCS
1528 	 * must be specified with a different mechanism.
1529 	 */
1530 	for (; mode <= IEEE80211_MODE_11NG; mode++) {
1531 		if (isclr(ic->ic_modecaps, mode))
1532 			continue;
1533 		addmedia(media, caps, addsta, mode, IFM_AUTO);
1534 		addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1535 	}
1536 	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1537 	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1538 		addmedia(media, caps, addsta,
1539 		    IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1540 		i = ic->ic_txstream * 8 - 1;
1541 		if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1542 		    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1543 			rate = ieee80211_htrates[i].ht40_rate_400ns;
1544 		else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1545 			rate = ieee80211_htrates[i].ht40_rate_800ns;
1546 		else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1547 			rate = ieee80211_htrates[i].ht20_rate_400ns;
1548 		else
1549 			rate = ieee80211_htrates[i].ht20_rate_800ns;
1550 		if (rate > maxrate)
1551 			maxrate = rate;
1552 	}
1553 	return maxrate;
1554 }
1555 
1556 /* XXX inline or eliminate? */
1557 const struct ieee80211_rateset *
1558 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1559 {
1560 	/* XXX does this work for 11ng basic rates? */
1561 	return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1562 }
1563 
1564 void
1565 ieee80211_announce(struct ieee80211com *ic)
1566 {
1567 	int i, rate, mword;
1568 	enum ieee80211_phymode mode;
1569 	const struct ieee80211_rateset *rs;
1570 
1571 	/* NB: skip AUTO since it has no rates */
1572 	for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1573 		if (isclr(ic->ic_modecaps, mode))
1574 			continue;
1575 		ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
1576 		rs = &ic->ic_sup_rates[mode];
1577 		for (i = 0; i < rs->rs_nrates; i++) {
1578 			mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1579 			if (mword == 0)
1580 				continue;
1581 			rate = ieee80211_media2rate(mword);
1582 			printf("%s%d%sMbps", (i != 0 ? " " : ""),
1583 			    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1584 		}
1585 		printf("\n");
1586 	}
1587 	ieee80211_ht_announce(ic);
1588 }
1589 
1590 void
1591 ieee80211_announce_channels(struct ieee80211com *ic)
1592 {
1593 	const struct ieee80211_channel *c;
1594 	char type;
1595 	int i, cw;
1596 
1597 	printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
1598 	for (i = 0; i < ic->ic_nchans; i++) {
1599 		c = &ic->ic_channels[i];
1600 		if (IEEE80211_IS_CHAN_ST(c))
1601 			type = 'S';
1602 		else if (IEEE80211_IS_CHAN_108A(c))
1603 			type = 'T';
1604 		else if (IEEE80211_IS_CHAN_108G(c))
1605 			type = 'G';
1606 		else if (IEEE80211_IS_CHAN_HT(c))
1607 			type = 'n';
1608 		else if (IEEE80211_IS_CHAN_A(c))
1609 			type = 'a';
1610 		else if (IEEE80211_IS_CHAN_ANYG(c))
1611 			type = 'g';
1612 		else if (IEEE80211_IS_CHAN_B(c))
1613 			type = 'b';
1614 		else
1615 			type = 'f';
1616 		if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1617 			cw = 40;
1618 		else if (IEEE80211_IS_CHAN_HALF(c))
1619 			cw = 10;
1620 		else if (IEEE80211_IS_CHAN_QUARTER(c))
1621 			cw = 5;
1622 		else
1623 			cw = 20;
1624 		printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
1625 			, c->ic_ieee, c->ic_freq, type
1626 			, cw
1627 			, IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1628 			  IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1629 			, c->ic_maxregpower
1630 			, c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1631 			, c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1632 		);
1633 	}
1634 }
1635 
1636 static int
1637 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1638 {
1639 	switch (IFM_MODE(ime->ifm_media)) {
1640 	case IFM_IEEE80211_11A:
1641 		*mode = IEEE80211_MODE_11A;
1642 		break;
1643 	case IFM_IEEE80211_11B:
1644 		*mode = IEEE80211_MODE_11B;
1645 		break;
1646 	case IFM_IEEE80211_11G:
1647 		*mode = IEEE80211_MODE_11G;
1648 		break;
1649 	case IFM_IEEE80211_FH:
1650 		*mode = IEEE80211_MODE_FH;
1651 		break;
1652 	case IFM_IEEE80211_11NA:
1653 		*mode = IEEE80211_MODE_11NA;
1654 		break;
1655 	case IFM_IEEE80211_11NG:
1656 		*mode = IEEE80211_MODE_11NG;
1657 		break;
1658 	case IFM_AUTO:
1659 		*mode = IEEE80211_MODE_AUTO;
1660 		break;
1661 	default:
1662 		return 0;
1663 	}
1664 	/*
1665 	 * Turbo mode is an ``option''.
1666 	 * XXX does not apply to AUTO
1667 	 */
1668 	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1669 		if (*mode == IEEE80211_MODE_11A) {
1670 			if (flags & IEEE80211_F_TURBOP)
1671 				*mode = IEEE80211_MODE_TURBO_A;
1672 			else
1673 				*mode = IEEE80211_MODE_STURBO_A;
1674 		} else if (*mode == IEEE80211_MODE_11G)
1675 			*mode = IEEE80211_MODE_TURBO_G;
1676 		else
1677 			return 0;
1678 	}
1679 	/* XXX HT40 +/- */
1680 	return 1;
1681 }
1682 
1683 /*
1684  * Handle a media change request on the vap interface.
1685  */
1686 int
1687 ieee80211_media_change(struct ifnet *ifp)
1688 {
1689 	struct ieee80211vap *vap = ifp->if_softc;
1690 	struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1691 	uint16_t newmode;
1692 
1693 	if (!media2mode(ime, vap->iv_flags, &newmode))
1694 		return EINVAL;
1695 	if (vap->iv_des_mode != newmode) {
1696 		vap->iv_des_mode = newmode;
1697 		/* XXX kick state machine if up+running */
1698 	}
1699 	return 0;
1700 }
1701 
1702 /*
1703  * Common code to calculate the media status word
1704  * from the operating mode and channel state.
1705  */
1706 static int
1707 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1708 {
1709 	int status;
1710 
1711 	status = IFM_IEEE80211;
1712 	switch (opmode) {
1713 	case IEEE80211_M_STA:
1714 		break;
1715 	case IEEE80211_M_IBSS:
1716 		status |= IFM_IEEE80211_ADHOC;
1717 		break;
1718 	case IEEE80211_M_HOSTAP:
1719 		status |= IFM_IEEE80211_HOSTAP;
1720 		break;
1721 	case IEEE80211_M_MONITOR:
1722 		status |= IFM_IEEE80211_MONITOR;
1723 		break;
1724 	case IEEE80211_M_AHDEMO:
1725 		status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1726 		break;
1727 	case IEEE80211_M_WDS:
1728 		status |= IFM_IEEE80211_WDS;
1729 		break;
1730 	case IEEE80211_M_MBSS:
1731 		status |= IFM_IEEE80211_MBSS;
1732 		break;
1733 	}
1734 	if (IEEE80211_IS_CHAN_HTA(chan)) {
1735 		status |= IFM_IEEE80211_11NA;
1736 	} else if (IEEE80211_IS_CHAN_HTG(chan)) {
1737 		status |= IFM_IEEE80211_11NG;
1738 	} else if (IEEE80211_IS_CHAN_A(chan)) {
1739 		status |= IFM_IEEE80211_11A;
1740 	} else if (IEEE80211_IS_CHAN_B(chan)) {
1741 		status |= IFM_IEEE80211_11B;
1742 	} else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1743 		status |= IFM_IEEE80211_11G;
1744 	} else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1745 		status |= IFM_IEEE80211_FH;
1746 	}
1747 	/* XXX else complain? */
1748 
1749 	if (IEEE80211_IS_CHAN_TURBO(chan))
1750 		status |= IFM_IEEE80211_TURBO;
1751 #if 0
1752 	if (IEEE80211_IS_CHAN_HT20(chan))
1753 		status |= IFM_IEEE80211_HT20;
1754 	if (IEEE80211_IS_CHAN_HT40(chan))
1755 		status |= IFM_IEEE80211_HT40;
1756 #endif
1757 	return status;
1758 }
1759 
1760 void
1761 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1762 {
1763 	struct ieee80211vap *vap = ifp->if_softc;
1764 	struct ieee80211com *ic = vap->iv_ic;
1765 	enum ieee80211_phymode mode;
1766 
1767 	imr->ifm_status = IFM_AVALID;
1768 	/*
1769 	 * NB: use the current channel's mode to lock down a xmit
1770 	 * rate only when running; otherwise we may have a mismatch
1771 	 * in which case the rate will not be convertible.
1772 	 */
1773 	if (vap->iv_state == IEEE80211_S_RUN ||
1774 	    vap->iv_state == IEEE80211_S_SLEEP) {
1775 		imr->ifm_status |= IFM_ACTIVE;
1776 		mode = ieee80211_chan2mode(ic->ic_curchan);
1777 	} else
1778 		mode = IEEE80211_MODE_AUTO;
1779 	imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1780 	/*
1781 	 * Calculate a current rate if possible.
1782 	 */
1783 	if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1784 		/*
1785 		 * A fixed rate is set, report that.
1786 		 */
1787 		imr->ifm_active |= ieee80211_rate2media(ic,
1788 			vap->iv_txparms[mode].ucastrate, mode);
1789 	} else if (vap->iv_opmode == IEEE80211_M_STA) {
1790 		/*
1791 		 * In station mode report the current transmit rate.
1792 		 */
1793 		imr->ifm_active |= ieee80211_rate2media(ic,
1794 			vap->iv_bss->ni_txrate, mode);
1795 	} else
1796 		imr->ifm_active |= IFM_AUTO;
1797 	if (imr->ifm_status & IFM_ACTIVE)
1798 		imr->ifm_current = imr->ifm_active;
1799 }
1800 
1801 /*
1802  * Set the current phy mode and recalculate the active channel
1803  * set based on the available channels for this mode.  Also
1804  * select a new default/current channel if the current one is
1805  * inappropriate for this mode.
1806  */
1807 int
1808 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1809 {
1810 	/*
1811 	 * Adjust basic rates in 11b/11g supported rate set.
1812 	 * Note that if operating on a hal/quarter rate channel
1813 	 * this is a noop as those rates sets are different
1814 	 * and used instead.
1815 	 */
1816 	if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1817 		ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1818 
1819 	ic->ic_curmode = mode;
1820 	ieee80211_reset_erp(ic);	/* reset ERP state */
1821 
1822 	return 0;
1823 }
1824 
1825 /*
1826  * Return the phy mode for with the specified channel.
1827  */
1828 enum ieee80211_phymode
1829 ieee80211_chan2mode(const struct ieee80211_channel *chan)
1830 {
1831 
1832 	if (IEEE80211_IS_CHAN_HTA(chan))
1833 		return IEEE80211_MODE_11NA;
1834 	else if (IEEE80211_IS_CHAN_HTG(chan))
1835 		return IEEE80211_MODE_11NG;
1836 	else if (IEEE80211_IS_CHAN_108G(chan))
1837 		return IEEE80211_MODE_TURBO_G;
1838 	else if (IEEE80211_IS_CHAN_ST(chan))
1839 		return IEEE80211_MODE_STURBO_A;
1840 	else if (IEEE80211_IS_CHAN_TURBO(chan))
1841 		return IEEE80211_MODE_TURBO_A;
1842 	else if (IEEE80211_IS_CHAN_HALF(chan))
1843 		return IEEE80211_MODE_HALF;
1844 	else if (IEEE80211_IS_CHAN_QUARTER(chan))
1845 		return IEEE80211_MODE_QUARTER;
1846 	else if (IEEE80211_IS_CHAN_A(chan))
1847 		return IEEE80211_MODE_11A;
1848 	else if (IEEE80211_IS_CHAN_ANYG(chan))
1849 		return IEEE80211_MODE_11G;
1850 	else if (IEEE80211_IS_CHAN_B(chan))
1851 		return IEEE80211_MODE_11B;
1852 	else if (IEEE80211_IS_CHAN_FHSS(chan))
1853 		return IEEE80211_MODE_FH;
1854 
1855 	/* NB: should not get here */
1856 	printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1857 		__func__, chan->ic_freq, chan->ic_flags);
1858 	return IEEE80211_MODE_11B;
1859 }
1860 
1861 struct ratemedia {
1862 	u_int	match;	/* rate + mode */
1863 	u_int	media;	/* if_media rate */
1864 };
1865 
1866 static int
1867 findmedia(const struct ratemedia rates[], int n, u_int match)
1868 {
1869 	int i;
1870 
1871 	for (i = 0; i < n; i++)
1872 		if (rates[i].match == match)
1873 			return rates[i].media;
1874 	return IFM_AUTO;
1875 }
1876 
1877 /*
1878  * Convert IEEE80211 rate value to ifmedia subtype.
1879  * Rate is either a legacy rate in units of 0.5Mbps
1880  * or an MCS index.
1881  */
1882 int
1883 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1884 {
1885 	static const struct ratemedia rates[] = {
1886 		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1887 		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1888 		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1889 		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1890 		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1891 		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1892 		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1893 		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1894 		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1895 		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1896 		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1897 		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1898 		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1899 		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1900 		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1901 		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1902 		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1903 		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1904 		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1905 		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1906 		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1907 		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1908 		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1909 		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1910 		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1911 		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1912 		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1913 		{   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1914 		{   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1915 		{  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1916 		/* NB: OFDM72 doesn't really exist so we don't handle it */
1917 	};
1918 	static const struct ratemedia htrates[] = {
1919 		{   0, IFM_IEEE80211_MCS },
1920 		{   1, IFM_IEEE80211_MCS },
1921 		{   2, IFM_IEEE80211_MCS },
1922 		{   3, IFM_IEEE80211_MCS },
1923 		{   4, IFM_IEEE80211_MCS },
1924 		{   5, IFM_IEEE80211_MCS },
1925 		{   6, IFM_IEEE80211_MCS },
1926 		{   7, IFM_IEEE80211_MCS },
1927 		{   8, IFM_IEEE80211_MCS },
1928 		{   9, IFM_IEEE80211_MCS },
1929 		{  10, IFM_IEEE80211_MCS },
1930 		{  11, IFM_IEEE80211_MCS },
1931 		{  12, IFM_IEEE80211_MCS },
1932 		{  13, IFM_IEEE80211_MCS },
1933 		{  14, IFM_IEEE80211_MCS },
1934 		{  15, IFM_IEEE80211_MCS },
1935 		{  16, IFM_IEEE80211_MCS },
1936 		{  17, IFM_IEEE80211_MCS },
1937 		{  18, IFM_IEEE80211_MCS },
1938 		{  19, IFM_IEEE80211_MCS },
1939 		{  20, IFM_IEEE80211_MCS },
1940 		{  21, IFM_IEEE80211_MCS },
1941 		{  22, IFM_IEEE80211_MCS },
1942 		{  23, IFM_IEEE80211_MCS },
1943 		{  24, IFM_IEEE80211_MCS },
1944 		{  25, IFM_IEEE80211_MCS },
1945 		{  26, IFM_IEEE80211_MCS },
1946 		{  27, IFM_IEEE80211_MCS },
1947 		{  28, IFM_IEEE80211_MCS },
1948 		{  29, IFM_IEEE80211_MCS },
1949 		{  30, IFM_IEEE80211_MCS },
1950 		{  31, IFM_IEEE80211_MCS },
1951 		{  32, IFM_IEEE80211_MCS },
1952 		{  33, IFM_IEEE80211_MCS },
1953 		{  34, IFM_IEEE80211_MCS },
1954 		{  35, IFM_IEEE80211_MCS },
1955 		{  36, IFM_IEEE80211_MCS },
1956 		{  37, IFM_IEEE80211_MCS },
1957 		{  38, IFM_IEEE80211_MCS },
1958 		{  39, IFM_IEEE80211_MCS },
1959 		{  40, IFM_IEEE80211_MCS },
1960 		{  41, IFM_IEEE80211_MCS },
1961 		{  42, IFM_IEEE80211_MCS },
1962 		{  43, IFM_IEEE80211_MCS },
1963 		{  44, IFM_IEEE80211_MCS },
1964 		{  45, IFM_IEEE80211_MCS },
1965 		{  46, IFM_IEEE80211_MCS },
1966 		{  47, IFM_IEEE80211_MCS },
1967 		{  48, IFM_IEEE80211_MCS },
1968 		{  49, IFM_IEEE80211_MCS },
1969 		{  50, IFM_IEEE80211_MCS },
1970 		{  51, IFM_IEEE80211_MCS },
1971 		{  52, IFM_IEEE80211_MCS },
1972 		{  53, IFM_IEEE80211_MCS },
1973 		{  54, IFM_IEEE80211_MCS },
1974 		{  55, IFM_IEEE80211_MCS },
1975 		{  56, IFM_IEEE80211_MCS },
1976 		{  57, IFM_IEEE80211_MCS },
1977 		{  58, IFM_IEEE80211_MCS },
1978 		{  59, IFM_IEEE80211_MCS },
1979 		{  60, IFM_IEEE80211_MCS },
1980 		{  61, IFM_IEEE80211_MCS },
1981 		{  62, IFM_IEEE80211_MCS },
1982 		{  63, IFM_IEEE80211_MCS },
1983 		{  64, IFM_IEEE80211_MCS },
1984 		{  65, IFM_IEEE80211_MCS },
1985 		{  66, IFM_IEEE80211_MCS },
1986 		{  67, IFM_IEEE80211_MCS },
1987 		{  68, IFM_IEEE80211_MCS },
1988 		{  69, IFM_IEEE80211_MCS },
1989 		{  70, IFM_IEEE80211_MCS },
1990 		{  71, IFM_IEEE80211_MCS },
1991 		{  72, IFM_IEEE80211_MCS },
1992 		{  73, IFM_IEEE80211_MCS },
1993 		{  74, IFM_IEEE80211_MCS },
1994 		{  75, IFM_IEEE80211_MCS },
1995 		{  76, IFM_IEEE80211_MCS },
1996 	};
1997 	int m;
1998 
1999 	/*
2000 	 * Check 11n rates first for match as an MCS.
2001 	 */
2002 	if (mode == IEEE80211_MODE_11NA) {
2003 		if (rate & IEEE80211_RATE_MCS) {
2004 			rate &= ~IEEE80211_RATE_MCS;
2005 			m = findmedia(htrates, nitems(htrates), rate);
2006 			if (m != IFM_AUTO)
2007 				return m | IFM_IEEE80211_11NA;
2008 		}
2009 	} else if (mode == IEEE80211_MODE_11NG) {
2010 		/* NB: 12 is ambiguous, it will be treated as an MCS */
2011 		if (rate & IEEE80211_RATE_MCS) {
2012 			rate &= ~IEEE80211_RATE_MCS;
2013 			m = findmedia(htrates, nitems(htrates), rate);
2014 			if (m != IFM_AUTO)
2015 				return m | IFM_IEEE80211_11NG;
2016 		}
2017 	}
2018 	rate &= IEEE80211_RATE_VAL;
2019 	switch (mode) {
2020 	case IEEE80211_MODE_11A:
2021 	case IEEE80211_MODE_HALF:		/* XXX good 'nuf */
2022 	case IEEE80211_MODE_QUARTER:
2023 	case IEEE80211_MODE_11NA:
2024 	case IEEE80211_MODE_TURBO_A:
2025 	case IEEE80211_MODE_STURBO_A:
2026 		return findmedia(rates, nitems(rates),
2027 		    rate | IFM_IEEE80211_11A);
2028 	case IEEE80211_MODE_11B:
2029 		return findmedia(rates, nitems(rates),
2030 		    rate | IFM_IEEE80211_11B);
2031 	case IEEE80211_MODE_FH:
2032 		return findmedia(rates, nitems(rates),
2033 		    rate | IFM_IEEE80211_FH);
2034 	case IEEE80211_MODE_AUTO:
2035 		/* NB: ic may be NULL for some drivers */
2036 		if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
2037 			return findmedia(rates, nitems(rates),
2038 			    rate | IFM_IEEE80211_FH);
2039 		/* NB: hack, 11g matches both 11b+11a rates */
2040 		/* fall thru... */
2041 	case IEEE80211_MODE_11G:
2042 	case IEEE80211_MODE_11NG:
2043 	case IEEE80211_MODE_TURBO_G:
2044 		return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
2045 	case IEEE80211_MODE_VHT_2GHZ:
2046 	case IEEE80211_MODE_VHT_5GHZ:
2047 		/* XXX TODO: need to figure out mapping for VHT rates */
2048 		return IFM_AUTO;
2049 	}
2050 	return IFM_AUTO;
2051 }
2052 
2053 int
2054 ieee80211_media2rate(int mword)
2055 {
2056 	static const int ieeerates[] = {
2057 		-1,		/* IFM_AUTO */
2058 		0,		/* IFM_MANUAL */
2059 		0,		/* IFM_NONE */
2060 		2,		/* IFM_IEEE80211_FH1 */
2061 		4,		/* IFM_IEEE80211_FH2 */
2062 		2,		/* IFM_IEEE80211_DS1 */
2063 		4,		/* IFM_IEEE80211_DS2 */
2064 		11,		/* IFM_IEEE80211_DS5 */
2065 		22,		/* IFM_IEEE80211_DS11 */
2066 		44,		/* IFM_IEEE80211_DS22 */
2067 		12,		/* IFM_IEEE80211_OFDM6 */
2068 		18,		/* IFM_IEEE80211_OFDM9 */
2069 		24,		/* IFM_IEEE80211_OFDM12 */
2070 		36,		/* IFM_IEEE80211_OFDM18 */
2071 		48,		/* IFM_IEEE80211_OFDM24 */
2072 		72,		/* IFM_IEEE80211_OFDM36 */
2073 		96,		/* IFM_IEEE80211_OFDM48 */
2074 		108,		/* IFM_IEEE80211_OFDM54 */
2075 		144,		/* IFM_IEEE80211_OFDM72 */
2076 		0,		/* IFM_IEEE80211_DS354k */
2077 		0,		/* IFM_IEEE80211_DS512k */
2078 		6,		/* IFM_IEEE80211_OFDM3 */
2079 		9,		/* IFM_IEEE80211_OFDM4 */
2080 		54,		/* IFM_IEEE80211_OFDM27 */
2081 		-1,		/* IFM_IEEE80211_MCS */
2082 		-1,		/* IFM_IEEE80211_VHT */
2083 	};
2084 	return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
2085 		ieeerates[IFM_SUBTYPE(mword)] : 0;
2086 }
2087 
2088 /*
2089  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2090  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2091  */
2092 #define	mix(a, b, c)							\
2093 do {									\
2094 	a -= b; a -= c; a ^= (c >> 13);					\
2095 	b -= c; b -= a; b ^= (a << 8);					\
2096 	c -= a; c -= b; c ^= (b >> 13);					\
2097 	a -= b; a -= c; a ^= (c >> 12);					\
2098 	b -= c; b -= a; b ^= (a << 16);					\
2099 	c -= a; c -= b; c ^= (b >> 5);					\
2100 	a -= b; a -= c; a ^= (c >> 3);					\
2101 	b -= c; b -= a; b ^= (a << 10);					\
2102 	c -= a; c -= b; c ^= (b >> 15);					\
2103 } while (/*CONSTCOND*/0)
2104 
2105 uint32_t
2106 ieee80211_mac_hash(const struct ieee80211com *ic,
2107 	const uint8_t addr[IEEE80211_ADDR_LEN])
2108 {
2109 	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
2110 
2111 	b += addr[5] << 8;
2112 	b += addr[4];
2113 	a += addr[3] << 24;
2114 	a += addr[2] << 16;
2115 	a += addr[1] << 8;
2116 	a += addr[0];
2117 
2118 	mix(a, b, c);
2119 
2120 	return c;
2121 }
2122 #undef mix
2123 
2124 char
2125 ieee80211_channel_type_char(const struct ieee80211_channel *c)
2126 {
2127 	if (IEEE80211_IS_CHAN_ST(c))
2128 		return 'S';
2129 	if (IEEE80211_IS_CHAN_108A(c))
2130 		return 'T';
2131 	if (IEEE80211_IS_CHAN_108G(c))
2132 		return 'G';
2133 	if (IEEE80211_IS_CHAN_VHT(c))
2134 		return 'v';
2135 	if (IEEE80211_IS_CHAN_HT(c))
2136 		return 'n';
2137 	if (IEEE80211_IS_CHAN_A(c))
2138 		return 'a';
2139 	if (IEEE80211_IS_CHAN_ANYG(c))
2140 		return 'g';
2141 	if (IEEE80211_IS_CHAN_B(c))
2142 		return 'b';
2143 	return 'f';
2144 }
2145