ieee80211.c revision 298812
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: head/sys/net80211/ieee80211.c 298812 2016-04-29 21:18:14Z avos $");
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
60const 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 */
75const 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
87const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
88	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
89
90static	void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
91static	void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
92static	void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
93static	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);
96static	int media_status(enum ieee80211_opmode,
97		const struct ieee80211_channel *);
98static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
99
100MALLOC_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)
106static const struct ieee80211_rateset ieee80211_rateset_11a =
107	{ 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
108static const struct ieee80211_rateset ieee80211_rateset_half =
109	{ 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
110static const struct ieee80211_rateset ieee80211_rateset_quarter =
111	{ 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
112static 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 */
115static 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 */
124void
125ieee80211_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
227static void
228null_update_mcast(struct ieee80211com *ic)
229{
230
231	ic_printf(ic, "need multicast update callback\n");
232}
233
234static void
235null_update_promisc(struct ieee80211com *ic)
236{
237
238	ic_printf(ic, "need promiscuous mode update callback\n");
239}
240
241static void
242null_update_chw(struct ieee80211com *ic)
243{
244
245	ic_printf(ic, "%s: need callback\n", __func__);
246}
247
248int
249ic_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
261static LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head);
262static struct mtx ic_list_mtx;
263MTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF);
264
265static int
266sysctl_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
290SYSCTL_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 */
298void
299ieee80211_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 */
354void
355ieee80211_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
395struct ieee80211com *
396ieee80211_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/*
410 * Default reset method for use with the ioctl support.  This
411 * method is invoked after any state change in the 802.11
412 * layer that should be propagated to the hardware but not
413 * require re-initialization of the 802.11 state machine (e.g
414 * rescanning for an ap).  We always return ENETRESET which
415 * should cause the driver to re-initialize the device. Drivers
416 * can override this method to implement more optimized support.
417 */
418static int
419default_reset(struct ieee80211vap *vap, u_long cmd)
420{
421	return ENETRESET;
422}
423
424/*
425 * Add underlying device errors to vap errors.
426 */
427static uint64_t
428ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
429{
430	struct ieee80211vap *vap = ifp->if_softc;
431	struct ieee80211com *ic = vap->iv_ic;
432	uint64_t rv;
433
434	rv = if_get_counter_default(ifp, cnt);
435	switch (cnt) {
436	case IFCOUNTER_OERRORS:
437		rv += counter_u64_fetch(ic->ic_oerrors);
438		break;
439	case IFCOUNTER_IERRORS:
440		rv += counter_u64_fetch(ic->ic_ierrors);
441		break;
442	default:
443		break;
444	}
445
446	return (rv);
447}
448
449/*
450 * Prepare a vap for use.  Drivers use this call to
451 * setup net80211 state in new vap's prior attaching
452 * them with ieee80211_vap_attach (below).
453 */
454int
455ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
456    const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
457    int flags, const uint8_t bssid[IEEE80211_ADDR_LEN])
458{
459	struct ifnet *ifp;
460
461	ifp = if_alloc(IFT_ETHER);
462	if (ifp == NULL) {
463		ic_printf(ic, "%s: unable to allocate ifnet\n",
464		    __func__);
465		return ENOMEM;
466	}
467	if_initname(ifp, name, unit);
468	ifp->if_softc = vap;			/* back pointer */
469	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
470	ifp->if_transmit = ieee80211_vap_transmit;
471	ifp->if_qflush = ieee80211_vap_qflush;
472	ifp->if_ioctl = ieee80211_ioctl;
473	ifp->if_init = ieee80211_init;
474	ifp->if_get_counter = ieee80211_get_counter;
475
476	vap->iv_ifp = ifp;
477	vap->iv_ic = ic;
478	vap->iv_flags = ic->ic_flags;		/* propagate common flags */
479	vap->iv_flags_ext = ic->ic_flags_ext;
480	vap->iv_flags_ven = ic->ic_flags_ven;
481	vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
482	vap->iv_htcaps = ic->ic_htcaps;
483	vap->iv_htextcaps = ic->ic_htextcaps;
484	vap->iv_opmode = opmode;
485	vap->iv_caps |= ieee80211_opcap[opmode];
486	IEEE80211_ADDR_COPY(vap->iv_myaddr, ic->ic_macaddr);
487	switch (opmode) {
488	case IEEE80211_M_WDS:
489		/*
490		 * WDS links must specify the bssid of the far end.
491		 * For legacy operation this is a static relationship.
492		 * For non-legacy operation the station must associate
493		 * and be authorized to pass traffic.  Plumbing the
494		 * vap to the proper node happens when the vap
495		 * transitions to RUN state.
496		 */
497		IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
498		vap->iv_flags |= IEEE80211_F_DESBSSID;
499		if (flags & IEEE80211_CLONE_WDSLEGACY)
500			vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
501		break;
502#ifdef IEEE80211_SUPPORT_TDMA
503	case IEEE80211_M_AHDEMO:
504		if (flags & IEEE80211_CLONE_TDMA) {
505			/* NB: checked before clone operation allowed */
506			KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
507			    ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
508			/*
509			 * Propagate TDMA capability to mark vap; this
510			 * cannot be removed and is used to distinguish
511			 * regular ahdemo operation from ahdemo+tdma.
512			 */
513			vap->iv_caps |= IEEE80211_C_TDMA;
514		}
515		break;
516#endif
517	default:
518		break;
519	}
520	/* auto-enable s/w beacon miss support */
521	if (flags & IEEE80211_CLONE_NOBEACONS)
522		vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
523	/* auto-generated or user supplied MAC address */
524	if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
525		vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
526	/*
527	 * Enable various functionality by default if we're
528	 * capable; the driver can override us if it knows better.
529	 */
530	if (vap->iv_caps & IEEE80211_C_WME)
531		vap->iv_flags |= IEEE80211_F_WME;
532	if (vap->iv_caps & IEEE80211_C_BURST)
533		vap->iv_flags |= IEEE80211_F_BURST;
534	/* NB: bg scanning only makes sense for station mode right now */
535	if (vap->iv_opmode == IEEE80211_M_STA &&
536	    (vap->iv_caps & IEEE80211_C_BGSCAN))
537		vap->iv_flags |= IEEE80211_F_BGSCAN;
538	vap->iv_flags |= IEEE80211_F_DOTH;	/* XXX no cap, just ena */
539	/* NB: DFS support only makes sense for ap mode right now */
540	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
541	    (vap->iv_caps & IEEE80211_C_DFS))
542		vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
543
544	vap->iv_des_chan = IEEE80211_CHAN_ANYC;		/* any channel is ok */
545	vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
546	vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
547	/*
548	 * Install a default reset method for the ioctl support;
549	 * the driver can override this.
550	 */
551	vap->iv_reset = default_reset;
552
553	ieee80211_sysctl_vattach(vap);
554	ieee80211_crypto_vattach(vap);
555	ieee80211_node_vattach(vap);
556	ieee80211_power_vattach(vap);
557	ieee80211_proto_vattach(vap);
558#ifdef IEEE80211_SUPPORT_SUPERG
559	ieee80211_superg_vattach(vap);
560#endif
561	ieee80211_ht_vattach(vap);
562	ieee80211_scan_vattach(vap);
563	ieee80211_regdomain_vattach(vap);
564	ieee80211_radiotap_vattach(vap);
565	ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
566
567	return 0;
568}
569
570/*
571 * Activate a vap.  State should have been prepared with a
572 * call to ieee80211_vap_setup and by the driver.  On return
573 * from this call the vap is ready for use.
574 */
575int
576ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change,
577    ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN])
578{
579	struct ifnet *ifp = vap->iv_ifp;
580	struct ieee80211com *ic = vap->iv_ic;
581	struct ifmediareq imr;
582	int maxrate;
583
584	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
585	    "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
586	    __func__, ieee80211_opmode_name[vap->iv_opmode],
587	    ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
588
589	/*
590	 * Do late attach work that cannot happen until after
591	 * the driver has had a chance to override defaults.
592	 */
593	ieee80211_node_latevattach(vap);
594	ieee80211_power_latevattach(vap);
595
596	maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
597	    vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
598	ieee80211_media_status(ifp, &imr);
599	/* NB: strip explicit mode; we're actually in autoselect */
600	ifmedia_set(&vap->iv_media,
601	    imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
602	if (maxrate)
603		ifp->if_baudrate = IF_Mbps(maxrate);
604
605	ether_ifattach(ifp, macaddr);
606	IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
607	/* hook output method setup by ether_ifattach */
608	vap->iv_output = ifp->if_output;
609	ifp->if_output = ieee80211_output;
610	/* NB: if_mtu set by ether_ifattach to ETHERMTU */
611
612	IEEE80211_LOCK(ic);
613	TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
614	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
615#ifdef IEEE80211_SUPPORT_SUPERG
616	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
617#endif
618	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
619	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
620	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
621	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
622	IEEE80211_UNLOCK(ic);
623
624	return 1;
625}
626
627/*
628 * Tear down vap state and reclaim the ifnet.
629 * The driver is assumed to have prepared for
630 * this; e.g. by turning off interrupts for the
631 * underlying device.
632 */
633void
634ieee80211_vap_detach(struct ieee80211vap *vap)
635{
636	struct ieee80211com *ic = vap->iv_ic;
637	struct ifnet *ifp = vap->iv_ifp;
638
639	CURVNET_SET(ifp->if_vnet);
640
641	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
642	    __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
643
644	/* NB: bpfdetach is called by ether_ifdetach and claims all taps */
645	ether_ifdetach(ifp);
646
647	ieee80211_stop(vap);
648
649	/*
650	 * Flush any deferred vap tasks.
651	 */
652	ieee80211_draintask(ic, &vap->iv_nstate_task);
653	ieee80211_draintask(ic, &vap->iv_swbmiss_task);
654
655	/* XXX band-aid until ifnet handles this for us */
656	taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
657
658	IEEE80211_LOCK(ic);
659	KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
660	TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
661	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
662#ifdef IEEE80211_SUPPORT_SUPERG
663	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
664#endif
665	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
666	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
667	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
668	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
669	/* NB: this handles the bpfdetach done below */
670	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
671	if (vap->iv_ifflags & IFF_PROMISC)
672		ieee80211_promisc(vap, false);
673	if (vap->iv_ifflags & IFF_ALLMULTI)
674		ieee80211_allmulti(vap, false);
675	IEEE80211_UNLOCK(ic);
676
677	ifmedia_removeall(&vap->iv_media);
678
679	ieee80211_radiotap_vdetach(vap);
680	ieee80211_regdomain_vdetach(vap);
681	ieee80211_scan_vdetach(vap);
682#ifdef IEEE80211_SUPPORT_SUPERG
683	ieee80211_superg_vdetach(vap);
684#endif
685	ieee80211_ht_vdetach(vap);
686	/* NB: must be before ieee80211_node_vdetach */
687	ieee80211_proto_vdetach(vap);
688	ieee80211_crypto_vdetach(vap);
689	ieee80211_power_vdetach(vap);
690	ieee80211_node_vdetach(vap);
691	ieee80211_sysctl_vdetach(vap);
692
693	if_free(ifp);
694
695	CURVNET_RESTORE();
696}
697
698/*
699 * Count number of vaps in promisc, and issue promisc on
700 * parent respectively.
701 */
702void
703ieee80211_promisc(struct ieee80211vap *vap, bool on)
704{
705	struct ieee80211com *ic = vap->iv_ic;
706
707	IEEE80211_LOCK_ASSERT(ic);
708
709	if (on) {
710		if (++ic->ic_promisc == 1)
711			ieee80211_runtask(ic, &ic->ic_promisc_task);
712	} else {
713		KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc",
714		    __func__, ic));
715		if (--ic->ic_promisc == 0)
716			ieee80211_runtask(ic, &ic->ic_promisc_task);
717	}
718}
719
720/*
721 * Count number of vaps in allmulti, and issue allmulti on
722 * parent respectively.
723 */
724void
725ieee80211_allmulti(struct ieee80211vap *vap, bool on)
726{
727	struct ieee80211com *ic = vap->iv_ic;
728
729	IEEE80211_LOCK_ASSERT(ic);
730
731	if (on) {
732		if (++ic->ic_allmulti == 1)
733			ieee80211_runtask(ic, &ic->ic_mcast_task);
734	} else {
735		KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti",
736		    __func__, ic));
737		if (--ic->ic_allmulti == 0)
738			ieee80211_runtask(ic, &ic->ic_mcast_task);
739	}
740}
741
742/*
743 * Synchronize flag bit state in the com structure
744 * according to the state of all vap's.  This is used,
745 * for example, to handle state changes via ioctls.
746 */
747static void
748ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
749{
750	struct ieee80211vap *vap;
751	int bit;
752
753	IEEE80211_LOCK_ASSERT(ic);
754
755	bit = 0;
756	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
757		if (vap->iv_flags & flag) {
758			bit = 1;
759			break;
760		}
761	if (bit)
762		ic->ic_flags |= flag;
763	else
764		ic->ic_flags &= ~flag;
765}
766
767void
768ieee80211_syncflag(struct ieee80211vap *vap, int flag)
769{
770	struct ieee80211com *ic = vap->iv_ic;
771
772	IEEE80211_LOCK(ic);
773	if (flag < 0) {
774		flag = -flag;
775		vap->iv_flags &= ~flag;
776	} else
777		vap->iv_flags |= flag;
778	ieee80211_syncflag_locked(ic, flag);
779	IEEE80211_UNLOCK(ic);
780}
781
782/*
783 * Synchronize flags_ht bit state in the com structure
784 * according to the state of all vap's.  This is used,
785 * for example, to handle state changes via ioctls.
786 */
787static void
788ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
789{
790	struct ieee80211vap *vap;
791	int bit;
792
793	IEEE80211_LOCK_ASSERT(ic);
794
795	bit = 0;
796	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
797		if (vap->iv_flags_ht & flag) {
798			bit = 1;
799			break;
800		}
801	if (bit)
802		ic->ic_flags_ht |= flag;
803	else
804		ic->ic_flags_ht &= ~flag;
805}
806
807void
808ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
809{
810	struct ieee80211com *ic = vap->iv_ic;
811
812	IEEE80211_LOCK(ic);
813	if (flag < 0) {
814		flag = -flag;
815		vap->iv_flags_ht &= ~flag;
816	} else
817		vap->iv_flags_ht |= flag;
818	ieee80211_syncflag_ht_locked(ic, flag);
819	IEEE80211_UNLOCK(ic);
820}
821
822/*
823 * Synchronize flags_ext bit state in the com structure
824 * according to the state of all vap's.  This is used,
825 * for example, to handle state changes via ioctls.
826 */
827static void
828ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
829{
830	struct ieee80211vap *vap;
831	int bit;
832
833	IEEE80211_LOCK_ASSERT(ic);
834
835	bit = 0;
836	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
837		if (vap->iv_flags_ext & flag) {
838			bit = 1;
839			break;
840		}
841	if (bit)
842		ic->ic_flags_ext |= flag;
843	else
844		ic->ic_flags_ext &= ~flag;
845}
846
847void
848ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
849{
850	struct ieee80211com *ic = vap->iv_ic;
851
852	IEEE80211_LOCK(ic);
853	if (flag < 0) {
854		flag = -flag;
855		vap->iv_flags_ext &= ~flag;
856	} else
857		vap->iv_flags_ext |= flag;
858	ieee80211_syncflag_ext_locked(ic, flag);
859	IEEE80211_UNLOCK(ic);
860}
861
862static __inline int
863mapgsm(u_int freq, u_int flags)
864{
865	freq *= 10;
866	if (flags & IEEE80211_CHAN_QUARTER)
867		freq += 5;
868	else if (flags & IEEE80211_CHAN_HALF)
869		freq += 10;
870	else
871		freq += 20;
872	/* NB: there is no 907/20 wide but leave room */
873	return (freq - 906*10) / 5;
874}
875
876static __inline int
877mappsb(u_int freq, u_int flags)
878{
879	return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
880}
881
882/*
883 * Convert MHz frequency to IEEE channel number.
884 */
885int
886ieee80211_mhz2ieee(u_int freq, u_int flags)
887{
888#define	IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
889	if (flags & IEEE80211_CHAN_GSM)
890		return mapgsm(freq, flags);
891	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
892		if (freq == 2484)
893			return 14;
894		if (freq < 2484)
895			return ((int) freq - 2407) / 5;
896		else
897			return 15 + ((freq - 2512) / 20);
898	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
899		if (freq <= 5000) {
900			/* XXX check regdomain? */
901			if (IS_FREQ_IN_PSB(freq))
902				return mappsb(freq, flags);
903			return (freq - 4000) / 5;
904		} else
905			return (freq - 5000) / 5;
906	} else {				/* either, guess */
907		if (freq == 2484)
908			return 14;
909		if (freq < 2484) {
910			if (907 <= freq && freq <= 922)
911				return mapgsm(freq, flags);
912			return ((int) freq - 2407) / 5;
913		}
914		if (freq < 5000) {
915			if (IS_FREQ_IN_PSB(freq))
916				return mappsb(freq, flags);
917			else if (freq > 4900)
918				return (freq - 4000) / 5;
919			else
920				return 15 + ((freq - 2512) / 20);
921		}
922		return (freq - 5000) / 5;
923	}
924#undef IS_FREQ_IN_PSB
925}
926
927/*
928 * Convert channel to IEEE channel number.
929 */
930int
931ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
932{
933	if (c == NULL) {
934		ic_printf(ic, "invalid channel (NULL)\n");
935		return 0;		/* XXX */
936	}
937	return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
938}
939
940/*
941 * Convert IEEE channel number to MHz frequency.
942 */
943u_int
944ieee80211_ieee2mhz(u_int chan, u_int flags)
945{
946	if (flags & IEEE80211_CHAN_GSM)
947		return 907 + 5 * (chan / 10);
948	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
949		if (chan == 14)
950			return 2484;
951		if (chan < 14)
952			return 2407 + chan*5;
953		else
954			return 2512 + ((chan-15)*20);
955	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
956		if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
957			chan -= 37;
958			return 4940 + chan*5 + (chan % 5 ? 2 : 0);
959		}
960		return 5000 + (chan*5);
961	} else {				/* either, guess */
962		/* XXX can't distinguish PSB+GSM channels */
963		if (chan == 14)
964			return 2484;
965		if (chan < 14)			/* 0-13 */
966			return 2407 + chan*5;
967		if (chan < 27)			/* 15-26 */
968			return 2512 + ((chan-15)*20);
969		return 5000 + (chan*5);
970	}
971}
972
973static __inline void
974set_extchan(struct ieee80211_channel *c)
975{
976
977	/*
978	 * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4:
979	 * "the secondary channel number shall be 'N + [1,-1] * 4'
980	 */
981	if (c->ic_flags & IEEE80211_CHAN_HT40U)
982		c->ic_extieee = c->ic_ieee + 4;
983	else if (c->ic_flags & IEEE80211_CHAN_HT40D)
984		c->ic_extieee = c->ic_ieee - 4;
985	else
986		c->ic_extieee = 0;
987}
988
989static int
990addchan(struct ieee80211_channel chans[], int maxchans, int *nchans,
991    uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags)
992{
993	struct ieee80211_channel *c;
994
995	if (*nchans >= maxchans)
996		return (ENOBUFS);
997
998	c = &chans[(*nchans)++];
999	c->ic_ieee = ieee;
1000	c->ic_freq = freq != 0 ? freq : ieee80211_ieee2mhz(ieee, flags);
1001	c->ic_maxregpower = maxregpower;
1002	c->ic_maxpower = 2 * maxregpower;
1003	c->ic_flags = flags;
1004	set_extchan(c);
1005
1006	return (0);
1007}
1008
1009static int
1010copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans,
1011    uint32_t flags)
1012{
1013	struct ieee80211_channel *c;
1014
1015	KASSERT(*nchans > 0, ("channel list is empty\n"));
1016
1017	if (*nchans >= maxchans)
1018		return (ENOBUFS);
1019
1020	c = &chans[(*nchans)++];
1021	c[0] = c[-1];
1022	c->ic_flags = flags;
1023	set_extchan(c);
1024
1025	return (0);
1026}
1027
1028static void
1029getflags_2ghz(const uint8_t bands[], uint32_t flags[], int ht40)
1030{
1031	int nmodes;
1032
1033	nmodes = 0;
1034	if (isset(bands, IEEE80211_MODE_11B))
1035		flags[nmodes++] = IEEE80211_CHAN_B;
1036	if (isset(bands, IEEE80211_MODE_11G))
1037		flags[nmodes++] = IEEE80211_CHAN_G;
1038	if (isset(bands, IEEE80211_MODE_11NG))
1039		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
1040	if (ht40) {
1041		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U;
1042		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D;
1043	}
1044	flags[nmodes] = 0;
1045}
1046
1047static void
1048getflags_5ghz(const uint8_t bands[], uint32_t flags[], int ht40)
1049{
1050	int nmodes;
1051
1052	nmodes = 0;
1053	if (isset(bands, IEEE80211_MODE_11A))
1054		flags[nmodes++] = IEEE80211_CHAN_A;
1055	if (isset(bands, IEEE80211_MODE_11NA))
1056		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
1057	if (ht40) {
1058		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U;
1059		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D;
1060	}
1061	flags[nmodes] = 0;
1062}
1063
1064static void
1065getflags(const uint8_t bands[], uint32_t flags[], int ht40)
1066{
1067
1068	flags[0] = 0;
1069	if (isset(bands, IEEE80211_MODE_11A) ||
1070	    isset(bands, IEEE80211_MODE_11NA)) {
1071		if (isset(bands, IEEE80211_MODE_11B) ||
1072		    isset(bands, IEEE80211_MODE_11G) ||
1073		    isset(bands, IEEE80211_MODE_11NG))
1074			return;
1075
1076		getflags_5ghz(bands, flags, ht40);
1077	} else
1078		getflags_2ghz(bands, flags, ht40);
1079}
1080
1081/*
1082 * Add one 20 MHz channel into specified channel list.
1083 */
1084int
1085ieee80211_add_channel(struct ieee80211_channel chans[], int maxchans,
1086    int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower,
1087    uint32_t chan_flags, const uint8_t bands[])
1088{
1089	uint32_t flags[IEEE80211_MODE_MAX];
1090	int i, error;
1091
1092	getflags(bands, flags, 0);
1093	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1094
1095	error = addchan(chans, maxchans, nchans, ieee, freq, maxregpower,
1096	    flags[0] | chan_flags);
1097	for (i = 1; flags[i] != 0 && error == 0; i++) {
1098		error = copychan_prev(chans, maxchans, nchans,
1099		    flags[i] | chan_flags);
1100	}
1101
1102	return (error);
1103}
1104
1105static struct ieee80211_channel *
1106findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq,
1107    uint32_t flags)
1108{
1109	struct ieee80211_channel *c;
1110	int i;
1111
1112	flags &= IEEE80211_CHAN_ALLTURBO;
1113	/* brute force search */
1114	for (i = 0; i < nchans; i++) {
1115		c = &chans[i];
1116		if (c->ic_freq == freq &&
1117		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1118			return c;
1119	}
1120	return NULL;
1121}
1122
1123/*
1124 * Add 40 MHz channel pair into specified channel list.
1125 */
1126int
1127ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans,
1128    int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags)
1129{
1130	struct ieee80211_channel *cent, *extc;
1131	uint16_t freq;
1132	int error;
1133
1134	freq = ieee80211_ieee2mhz(ieee, flags);
1135
1136	/*
1137	 * Each entry defines an HT40 channel pair; find the
1138	 * center channel, then the extension channel above.
1139	 */
1140	flags |= IEEE80211_CHAN_HT20;
1141	cent = findchannel(chans, *nchans, freq, flags);
1142	if (cent == NULL)
1143		return (EINVAL);
1144
1145	extc = findchannel(chans, *nchans, freq + 20, flags);
1146	if (extc == NULL)
1147		return (ENOENT);
1148
1149	flags &= ~IEEE80211_CHAN_HT;
1150	error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq,
1151	    maxregpower, flags | IEEE80211_CHAN_HT40U);
1152	if (error != 0)
1153		return (error);
1154
1155	error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq,
1156	    maxregpower, flags | IEEE80211_CHAN_HT40D);
1157
1158	return (error);
1159}
1160
1161/*
1162 * Adds channels into specified channel list (ieee[] array must be sorted).
1163 * Channels are already sorted.
1164 */
1165static int
1166add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans,
1167    const uint8_t ieee[], int nieee, uint32_t flags[])
1168{
1169	uint16_t freq;
1170	int i, j, error;
1171
1172	for (i = 0; i < nieee; i++) {
1173		freq = ieee80211_ieee2mhz(ieee[i], flags[0]);
1174		for (j = 0; flags[j] != 0; j++) {
1175			if (flags[j] & IEEE80211_CHAN_HT40D)
1176				if (i == 0 || ieee[i] < ieee[0] + 4 ||
1177				    freq - 20 !=
1178				    ieee80211_ieee2mhz(ieee[i] - 4, flags[j]))
1179					continue;
1180			if (flags[j] & IEEE80211_CHAN_HT40U)
1181				if (i == nieee - 1 ||
1182				    ieee[i] + 4 > ieee[nieee - 1] ||
1183				    freq + 20 !=
1184				    ieee80211_ieee2mhz(ieee[i] + 4, flags[j]))
1185					continue;
1186
1187			if (j == 0) {
1188				error = addchan(chans, maxchans, nchans,
1189				    ieee[i], freq, 0, flags[j]);
1190			} else {
1191				error = copychan_prev(chans, maxchans, nchans,
1192				    flags[j]);
1193			}
1194			if (error != 0)
1195				return (error);
1196		}
1197	}
1198
1199	return (error);
1200}
1201
1202int
1203ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[], int maxchans,
1204    int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1205    int ht40)
1206{
1207	uint32_t flags[IEEE80211_MODE_MAX];
1208
1209	getflags_2ghz(bands, flags, ht40);
1210	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1211
1212	return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1213}
1214
1215int
1216ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[], int maxchans,
1217    int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1218    int ht40)
1219{
1220	uint32_t flags[IEEE80211_MODE_MAX];
1221
1222	getflags_5ghz(bands, flags, ht40);
1223	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1224
1225	return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1226}
1227
1228/*
1229 * Locate a channel given a frequency+flags.  We cache
1230 * the previous lookup to optimize switching between two
1231 * channels--as happens with dynamic turbo.
1232 */
1233struct ieee80211_channel *
1234ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
1235{
1236	struct ieee80211_channel *c;
1237
1238	flags &= IEEE80211_CHAN_ALLTURBO;
1239	c = ic->ic_prevchan;
1240	if (c != NULL && c->ic_freq == freq &&
1241	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1242		return c;
1243	/* brute force search */
1244	return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags));
1245}
1246
1247/*
1248 * Locate a channel given a channel number+flags.  We cache
1249 * the previous lookup to optimize switching between two
1250 * channels--as happens with dynamic turbo.
1251 */
1252struct ieee80211_channel *
1253ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1254{
1255	struct ieee80211_channel *c;
1256	int i;
1257
1258	flags &= IEEE80211_CHAN_ALLTURBO;
1259	c = ic->ic_prevchan;
1260	if (c != NULL && c->ic_ieee == ieee &&
1261	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1262		return c;
1263	/* brute force search */
1264	for (i = 0; i < ic->ic_nchans; i++) {
1265		c = &ic->ic_channels[i];
1266		if (c->ic_ieee == ieee &&
1267		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1268			return c;
1269	}
1270	return NULL;
1271}
1272
1273/*
1274 * Lookup a channel suitable for the given rx status.
1275 *
1276 * This is used to find a channel for a frame (eg beacon, probe
1277 * response) based purely on the received PHY information.
1278 *
1279 * For now it tries to do it based on R_FREQ / R_IEEE.
1280 * This is enough for 11bg and 11a (and thus 11ng/11na)
1281 * but it will not be enough for GSM, PSB channels and the
1282 * like.  It also doesn't know about legacy-turbog and
1283 * legacy-turbo modes, which some offload NICs actually
1284 * support in weird ways.
1285 *
1286 * Takes the ic and rxstatus; returns the channel or NULL
1287 * if not found.
1288 *
1289 * XXX TODO: Add support for that when the need arises.
1290 */
1291struct ieee80211_channel *
1292ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1293    const struct ieee80211_rx_stats *rxs)
1294{
1295	struct ieee80211com *ic = vap->iv_ic;
1296	uint32_t flags;
1297	struct ieee80211_channel *c;
1298
1299	if (rxs == NULL)
1300		return (NULL);
1301
1302	/*
1303	 * Strictly speaking we only use freq for now,
1304	 * however later on we may wish to just store
1305	 * the ieee for verification.
1306	 */
1307	if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1308		return (NULL);
1309	if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1310		return (NULL);
1311
1312	/*
1313	 * If the rx status contains a valid ieee/freq, then
1314	 * ensure we populate the correct channel information
1315	 * in rxchan before passing it up to the scan infrastructure.
1316	 * Offload NICs will pass up beacons from all channels
1317	 * during background scans.
1318	 */
1319
1320	/* Determine a band */
1321	/* XXX should be done by the driver? */
1322	if (rxs->c_freq < 3000) {
1323		flags = IEEE80211_CHAN_G;
1324	} else {
1325		flags = IEEE80211_CHAN_A;
1326	}
1327
1328	/* Channel lookup */
1329	c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1330
1331	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1332	    "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1333	    __func__,
1334	    (int) rxs->c_freq,
1335	    (int) rxs->c_ieee,
1336	    flags,
1337	    c);
1338
1339	return (c);
1340}
1341
1342static void
1343addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1344{
1345#define	ADD(_ic, _s, _o) \
1346	ifmedia_add(media, \
1347		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1348	static const u_int mopts[IEEE80211_MODE_MAX] = {
1349	    [IEEE80211_MODE_AUTO]	= IFM_AUTO,
1350	    [IEEE80211_MODE_11A]	= IFM_IEEE80211_11A,
1351	    [IEEE80211_MODE_11B]	= IFM_IEEE80211_11B,
1352	    [IEEE80211_MODE_11G]	= IFM_IEEE80211_11G,
1353	    [IEEE80211_MODE_FH]		= IFM_IEEE80211_FH,
1354	    [IEEE80211_MODE_TURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1355	    [IEEE80211_MODE_TURBO_G]	= IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1356	    [IEEE80211_MODE_STURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1357	    [IEEE80211_MODE_HALF]	= IFM_IEEE80211_11A,	/* XXX */
1358	    [IEEE80211_MODE_QUARTER]	= IFM_IEEE80211_11A,	/* XXX */
1359	    [IEEE80211_MODE_11NA]	= IFM_IEEE80211_11NA,
1360	    [IEEE80211_MODE_11NG]	= IFM_IEEE80211_11NG,
1361	};
1362	u_int mopt;
1363
1364	mopt = mopts[mode];
1365	if (addsta)
1366		ADD(ic, mword, mopt);	/* STA mode has no cap */
1367	if (caps & IEEE80211_C_IBSS)
1368		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1369	if (caps & IEEE80211_C_HOSTAP)
1370		ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1371	if (caps & IEEE80211_C_AHDEMO)
1372		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1373	if (caps & IEEE80211_C_MONITOR)
1374		ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1375	if (caps & IEEE80211_C_WDS)
1376		ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1377	if (caps & IEEE80211_C_MBSS)
1378		ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1379#undef ADD
1380}
1381
1382/*
1383 * Setup the media data structures according to the channel and
1384 * rate tables.
1385 */
1386static int
1387ieee80211_media_setup(struct ieee80211com *ic,
1388	struct ifmedia *media, int caps, int addsta,
1389	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1390{
1391	int i, j, rate, maxrate, mword, r;
1392	enum ieee80211_phymode mode;
1393	const struct ieee80211_rateset *rs;
1394	struct ieee80211_rateset allrates;
1395
1396	/*
1397	 * Fill in media characteristics.
1398	 */
1399	ifmedia_init(media, 0, media_change, media_stat);
1400	maxrate = 0;
1401	/*
1402	 * Add media for legacy operating modes.
1403	 */
1404	memset(&allrates, 0, sizeof(allrates));
1405	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1406		if (isclr(ic->ic_modecaps, mode))
1407			continue;
1408		addmedia(media, caps, addsta, mode, IFM_AUTO);
1409		if (mode == IEEE80211_MODE_AUTO)
1410			continue;
1411		rs = &ic->ic_sup_rates[mode];
1412		for (i = 0; i < rs->rs_nrates; i++) {
1413			rate = rs->rs_rates[i];
1414			mword = ieee80211_rate2media(ic, rate, mode);
1415			if (mword == 0)
1416				continue;
1417			addmedia(media, caps, addsta, mode, mword);
1418			/*
1419			 * Add legacy rate to the collection of all rates.
1420			 */
1421			r = rate & IEEE80211_RATE_VAL;
1422			for (j = 0; j < allrates.rs_nrates; j++)
1423				if (allrates.rs_rates[j] == r)
1424					break;
1425			if (j == allrates.rs_nrates) {
1426				/* unique, add to the set */
1427				allrates.rs_rates[j] = r;
1428				allrates.rs_nrates++;
1429			}
1430			rate = (rate & IEEE80211_RATE_VAL) / 2;
1431			if (rate > maxrate)
1432				maxrate = rate;
1433		}
1434	}
1435	for (i = 0; i < allrates.rs_nrates; i++) {
1436		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1437				IEEE80211_MODE_AUTO);
1438		if (mword == 0)
1439			continue;
1440		/* NB: remove media options from mword */
1441		addmedia(media, caps, addsta,
1442		    IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1443	}
1444	/*
1445	 * Add HT/11n media.  Note that we do not have enough
1446	 * bits in the media subtype to express the MCS so we
1447	 * use a "placeholder" media subtype and any fixed MCS
1448	 * must be specified with a different mechanism.
1449	 */
1450	for (; mode <= IEEE80211_MODE_11NG; mode++) {
1451		if (isclr(ic->ic_modecaps, mode))
1452			continue;
1453		addmedia(media, caps, addsta, mode, IFM_AUTO);
1454		addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1455	}
1456	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1457	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1458		addmedia(media, caps, addsta,
1459		    IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1460		i = ic->ic_txstream * 8 - 1;
1461		if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1462		    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1463			rate = ieee80211_htrates[i].ht40_rate_400ns;
1464		else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1465			rate = ieee80211_htrates[i].ht40_rate_800ns;
1466		else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1467			rate = ieee80211_htrates[i].ht20_rate_400ns;
1468		else
1469			rate = ieee80211_htrates[i].ht20_rate_800ns;
1470		if (rate > maxrate)
1471			maxrate = rate;
1472	}
1473	return maxrate;
1474}
1475
1476/* XXX inline or eliminate? */
1477const struct ieee80211_rateset *
1478ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1479{
1480	/* XXX does this work for 11ng basic rates? */
1481	return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1482}
1483
1484void
1485ieee80211_announce(struct ieee80211com *ic)
1486{
1487	int i, rate, mword;
1488	enum ieee80211_phymode mode;
1489	const struct ieee80211_rateset *rs;
1490
1491	/* NB: skip AUTO since it has no rates */
1492	for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1493		if (isclr(ic->ic_modecaps, mode))
1494			continue;
1495		ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
1496		rs = &ic->ic_sup_rates[mode];
1497		for (i = 0; i < rs->rs_nrates; i++) {
1498			mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1499			if (mword == 0)
1500				continue;
1501			rate = ieee80211_media2rate(mword);
1502			printf("%s%d%sMbps", (i != 0 ? " " : ""),
1503			    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1504		}
1505		printf("\n");
1506	}
1507	ieee80211_ht_announce(ic);
1508}
1509
1510void
1511ieee80211_announce_channels(struct ieee80211com *ic)
1512{
1513	const struct ieee80211_channel *c;
1514	char type;
1515	int i, cw;
1516
1517	printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
1518	for (i = 0; i < ic->ic_nchans; i++) {
1519		c = &ic->ic_channels[i];
1520		if (IEEE80211_IS_CHAN_ST(c))
1521			type = 'S';
1522		else if (IEEE80211_IS_CHAN_108A(c))
1523			type = 'T';
1524		else if (IEEE80211_IS_CHAN_108G(c))
1525			type = 'G';
1526		else if (IEEE80211_IS_CHAN_HT(c))
1527			type = 'n';
1528		else if (IEEE80211_IS_CHAN_A(c))
1529			type = 'a';
1530		else if (IEEE80211_IS_CHAN_ANYG(c))
1531			type = 'g';
1532		else if (IEEE80211_IS_CHAN_B(c))
1533			type = 'b';
1534		else
1535			type = 'f';
1536		if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1537			cw = 40;
1538		else if (IEEE80211_IS_CHAN_HALF(c))
1539			cw = 10;
1540		else if (IEEE80211_IS_CHAN_QUARTER(c))
1541			cw = 5;
1542		else
1543			cw = 20;
1544		printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
1545			, c->ic_ieee, c->ic_freq, type
1546			, cw
1547			, IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1548			  IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1549			, c->ic_maxregpower
1550			, c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1551			, c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1552		);
1553	}
1554}
1555
1556static int
1557media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1558{
1559	switch (IFM_MODE(ime->ifm_media)) {
1560	case IFM_IEEE80211_11A:
1561		*mode = IEEE80211_MODE_11A;
1562		break;
1563	case IFM_IEEE80211_11B:
1564		*mode = IEEE80211_MODE_11B;
1565		break;
1566	case IFM_IEEE80211_11G:
1567		*mode = IEEE80211_MODE_11G;
1568		break;
1569	case IFM_IEEE80211_FH:
1570		*mode = IEEE80211_MODE_FH;
1571		break;
1572	case IFM_IEEE80211_11NA:
1573		*mode = IEEE80211_MODE_11NA;
1574		break;
1575	case IFM_IEEE80211_11NG:
1576		*mode = IEEE80211_MODE_11NG;
1577		break;
1578	case IFM_AUTO:
1579		*mode = IEEE80211_MODE_AUTO;
1580		break;
1581	default:
1582		return 0;
1583	}
1584	/*
1585	 * Turbo mode is an ``option''.
1586	 * XXX does not apply to AUTO
1587	 */
1588	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1589		if (*mode == IEEE80211_MODE_11A) {
1590			if (flags & IEEE80211_F_TURBOP)
1591				*mode = IEEE80211_MODE_TURBO_A;
1592			else
1593				*mode = IEEE80211_MODE_STURBO_A;
1594		} else if (*mode == IEEE80211_MODE_11G)
1595			*mode = IEEE80211_MODE_TURBO_G;
1596		else
1597			return 0;
1598	}
1599	/* XXX HT40 +/- */
1600	return 1;
1601}
1602
1603/*
1604 * Handle a media change request on the vap interface.
1605 */
1606int
1607ieee80211_media_change(struct ifnet *ifp)
1608{
1609	struct ieee80211vap *vap = ifp->if_softc;
1610	struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1611	uint16_t newmode;
1612
1613	if (!media2mode(ime, vap->iv_flags, &newmode))
1614		return EINVAL;
1615	if (vap->iv_des_mode != newmode) {
1616		vap->iv_des_mode = newmode;
1617		/* XXX kick state machine if up+running */
1618	}
1619	return 0;
1620}
1621
1622/*
1623 * Common code to calculate the media status word
1624 * from the operating mode and channel state.
1625 */
1626static int
1627media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1628{
1629	int status;
1630
1631	status = IFM_IEEE80211;
1632	switch (opmode) {
1633	case IEEE80211_M_STA:
1634		break;
1635	case IEEE80211_M_IBSS:
1636		status |= IFM_IEEE80211_ADHOC;
1637		break;
1638	case IEEE80211_M_HOSTAP:
1639		status |= IFM_IEEE80211_HOSTAP;
1640		break;
1641	case IEEE80211_M_MONITOR:
1642		status |= IFM_IEEE80211_MONITOR;
1643		break;
1644	case IEEE80211_M_AHDEMO:
1645		status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1646		break;
1647	case IEEE80211_M_WDS:
1648		status |= IFM_IEEE80211_WDS;
1649		break;
1650	case IEEE80211_M_MBSS:
1651		status |= IFM_IEEE80211_MBSS;
1652		break;
1653	}
1654	if (IEEE80211_IS_CHAN_HTA(chan)) {
1655		status |= IFM_IEEE80211_11NA;
1656	} else if (IEEE80211_IS_CHAN_HTG(chan)) {
1657		status |= IFM_IEEE80211_11NG;
1658	} else if (IEEE80211_IS_CHAN_A(chan)) {
1659		status |= IFM_IEEE80211_11A;
1660	} else if (IEEE80211_IS_CHAN_B(chan)) {
1661		status |= IFM_IEEE80211_11B;
1662	} else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1663		status |= IFM_IEEE80211_11G;
1664	} else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1665		status |= IFM_IEEE80211_FH;
1666	}
1667	/* XXX else complain? */
1668
1669	if (IEEE80211_IS_CHAN_TURBO(chan))
1670		status |= IFM_IEEE80211_TURBO;
1671#if 0
1672	if (IEEE80211_IS_CHAN_HT20(chan))
1673		status |= IFM_IEEE80211_HT20;
1674	if (IEEE80211_IS_CHAN_HT40(chan))
1675		status |= IFM_IEEE80211_HT40;
1676#endif
1677	return status;
1678}
1679
1680void
1681ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1682{
1683	struct ieee80211vap *vap = ifp->if_softc;
1684	struct ieee80211com *ic = vap->iv_ic;
1685	enum ieee80211_phymode mode;
1686
1687	imr->ifm_status = IFM_AVALID;
1688	/*
1689	 * NB: use the current channel's mode to lock down a xmit
1690	 * rate only when running; otherwise we may have a mismatch
1691	 * in which case the rate will not be convertible.
1692	 */
1693	if (vap->iv_state == IEEE80211_S_RUN ||
1694	    vap->iv_state == IEEE80211_S_SLEEP) {
1695		imr->ifm_status |= IFM_ACTIVE;
1696		mode = ieee80211_chan2mode(ic->ic_curchan);
1697	} else
1698		mode = IEEE80211_MODE_AUTO;
1699	imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1700	/*
1701	 * Calculate a current rate if possible.
1702	 */
1703	if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1704		/*
1705		 * A fixed rate is set, report that.
1706		 */
1707		imr->ifm_active |= ieee80211_rate2media(ic,
1708			vap->iv_txparms[mode].ucastrate, mode);
1709	} else if (vap->iv_opmode == IEEE80211_M_STA) {
1710		/*
1711		 * In station mode report the current transmit rate.
1712		 */
1713		imr->ifm_active |= ieee80211_rate2media(ic,
1714			vap->iv_bss->ni_txrate, mode);
1715	} else
1716		imr->ifm_active |= IFM_AUTO;
1717	if (imr->ifm_status & IFM_ACTIVE)
1718		imr->ifm_current = imr->ifm_active;
1719}
1720
1721/*
1722 * Set the current phy mode and recalculate the active channel
1723 * set based on the available channels for this mode.  Also
1724 * select a new default/current channel if the current one is
1725 * inappropriate for this mode.
1726 */
1727int
1728ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1729{
1730	/*
1731	 * Adjust basic rates in 11b/11g supported rate set.
1732	 * Note that if operating on a hal/quarter rate channel
1733	 * this is a noop as those rates sets are different
1734	 * and used instead.
1735	 */
1736	if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1737		ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1738
1739	ic->ic_curmode = mode;
1740	ieee80211_reset_erp(ic);	/* reset ERP state */
1741
1742	return 0;
1743}
1744
1745/*
1746 * Return the phy mode for with the specified channel.
1747 */
1748enum ieee80211_phymode
1749ieee80211_chan2mode(const struct ieee80211_channel *chan)
1750{
1751
1752	if (IEEE80211_IS_CHAN_HTA(chan))
1753		return IEEE80211_MODE_11NA;
1754	else if (IEEE80211_IS_CHAN_HTG(chan))
1755		return IEEE80211_MODE_11NG;
1756	else if (IEEE80211_IS_CHAN_108G(chan))
1757		return IEEE80211_MODE_TURBO_G;
1758	else if (IEEE80211_IS_CHAN_ST(chan))
1759		return IEEE80211_MODE_STURBO_A;
1760	else if (IEEE80211_IS_CHAN_TURBO(chan))
1761		return IEEE80211_MODE_TURBO_A;
1762	else if (IEEE80211_IS_CHAN_HALF(chan))
1763		return IEEE80211_MODE_HALF;
1764	else if (IEEE80211_IS_CHAN_QUARTER(chan))
1765		return IEEE80211_MODE_QUARTER;
1766	else if (IEEE80211_IS_CHAN_A(chan))
1767		return IEEE80211_MODE_11A;
1768	else if (IEEE80211_IS_CHAN_ANYG(chan))
1769		return IEEE80211_MODE_11G;
1770	else if (IEEE80211_IS_CHAN_B(chan))
1771		return IEEE80211_MODE_11B;
1772	else if (IEEE80211_IS_CHAN_FHSS(chan))
1773		return IEEE80211_MODE_FH;
1774
1775	/* NB: should not get here */
1776	printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1777		__func__, chan->ic_freq, chan->ic_flags);
1778	return IEEE80211_MODE_11B;
1779}
1780
1781struct ratemedia {
1782	u_int	match;	/* rate + mode */
1783	u_int	media;	/* if_media rate */
1784};
1785
1786static int
1787findmedia(const struct ratemedia rates[], int n, u_int match)
1788{
1789	int i;
1790
1791	for (i = 0; i < n; i++)
1792		if (rates[i].match == match)
1793			return rates[i].media;
1794	return IFM_AUTO;
1795}
1796
1797/*
1798 * Convert IEEE80211 rate value to ifmedia subtype.
1799 * Rate is either a legacy rate in units of 0.5Mbps
1800 * or an MCS index.
1801 */
1802int
1803ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1804{
1805	static const struct ratemedia rates[] = {
1806		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1807		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1808		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1809		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1810		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1811		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1812		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1813		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1814		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1815		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1816		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1817		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1818		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1819		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1820		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1821		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1822		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1823		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1824		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1825		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1826		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1827		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1828		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1829		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1830		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1831		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1832		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1833		{   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1834		{   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1835		{  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1836		/* NB: OFDM72 doesn't realy exist so we don't handle it */
1837	};
1838	static const struct ratemedia htrates[] = {
1839		{   0, IFM_IEEE80211_MCS },
1840		{   1, IFM_IEEE80211_MCS },
1841		{   2, IFM_IEEE80211_MCS },
1842		{   3, IFM_IEEE80211_MCS },
1843		{   4, IFM_IEEE80211_MCS },
1844		{   5, IFM_IEEE80211_MCS },
1845		{   6, IFM_IEEE80211_MCS },
1846		{   7, IFM_IEEE80211_MCS },
1847		{   8, IFM_IEEE80211_MCS },
1848		{   9, IFM_IEEE80211_MCS },
1849		{  10, IFM_IEEE80211_MCS },
1850		{  11, IFM_IEEE80211_MCS },
1851		{  12, IFM_IEEE80211_MCS },
1852		{  13, IFM_IEEE80211_MCS },
1853		{  14, IFM_IEEE80211_MCS },
1854		{  15, IFM_IEEE80211_MCS },
1855		{  16, IFM_IEEE80211_MCS },
1856		{  17, IFM_IEEE80211_MCS },
1857		{  18, IFM_IEEE80211_MCS },
1858		{  19, IFM_IEEE80211_MCS },
1859		{  20, IFM_IEEE80211_MCS },
1860		{  21, IFM_IEEE80211_MCS },
1861		{  22, IFM_IEEE80211_MCS },
1862		{  23, IFM_IEEE80211_MCS },
1863		{  24, IFM_IEEE80211_MCS },
1864		{  25, IFM_IEEE80211_MCS },
1865		{  26, IFM_IEEE80211_MCS },
1866		{  27, IFM_IEEE80211_MCS },
1867		{  28, IFM_IEEE80211_MCS },
1868		{  29, IFM_IEEE80211_MCS },
1869		{  30, IFM_IEEE80211_MCS },
1870		{  31, IFM_IEEE80211_MCS },
1871		{  32, IFM_IEEE80211_MCS },
1872		{  33, IFM_IEEE80211_MCS },
1873		{  34, IFM_IEEE80211_MCS },
1874		{  35, IFM_IEEE80211_MCS },
1875		{  36, IFM_IEEE80211_MCS },
1876		{  37, IFM_IEEE80211_MCS },
1877		{  38, IFM_IEEE80211_MCS },
1878		{  39, IFM_IEEE80211_MCS },
1879		{  40, IFM_IEEE80211_MCS },
1880		{  41, IFM_IEEE80211_MCS },
1881		{  42, IFM_IEEE80211_MCS },
1882		{  43, IFM_IEEE80211_MCS },
1883		{  44, IFM_IEEE80211_MCS },
1884		{  45, IFM_IEEE80211_MCS },
1885		{  46, IFM_IEEE80211_MCS },
1886		{  47, IFM_IEEE80211_MCS },
1887		{  48, IFM_IEEE80211_MCS },
1888		{  49, IFM_IEEE80211_MCS },
1889		{  50, IFM_IEEE80211_MCS },
1890		{  51, IFM_IEEE80211_MCS },
1891		{  52, IFM_IEEE80211_MCS },
1892		{  53, IFM_IEEE80211_MCS },
1893		{  54, IFM_IEEE80211_MCS },
1894		{  55, IFM_IEEE80211_MCS },
1895		{  56, IFM_IEEE80211_MCS },
1896		{  57, IFM_IEEE80211_MCS },
1897		{  58, IFM_IEEE80211_MCS },
1898		{  59, IFM_IEEE80211_MCS },
1899		{  60, IFM_IEEE80211_MCS },
1900		{  61, IFM_IEEE80211_MCS },
1901		{  62, IFM_IEEE80211_MCS },
1902		{  63, IFM_IEEE80211_MCS },
1903		{  64, IFM_IEEE80211_MCS },
1904		{  65, IFM_IEEE80211_MCS },
1905		{  66, IFM_IEEE80211_MCS },
1906		{  67, IFM_IEEE80211_MCS },
1907		{  68, IFM_IEEE80211_MCS },
1908		{  69, IFM_IEEE80211_MCS },
1909		{  70, IFM_IEEE80211_MCS },
1910		{  71, IFM_IEEE80211_MCS },
1911		{  72, IFM_IEEE80211_MCS },
1912		{  73, IFM_IEEE80211_MCS },
1913		{  74, IFM_IEEE80211_MCS },
1914		{  75, IFM_IEEE80211_MCS },
1915		{  76, IFM_IEEE80211_MCS },
1916	};
1917	int m;
1918
1919	/*
1920	 * Check 11n rates first for match as an MCS.
1921	 */
1922	if (mode == IEEE80211_MODE_11NA) {
1923		if (rate & IEEE80211_RATE_MCS) {
1924			rate &= ~IEEE80211_RATE_MCS;
1925			m = findmedia(htrates, nitems(htrates), rate);
1926			if (m != IFM_AUTO)
1927				return m | IFM_IEEE80211_11NA;
1928		}
1929	} else if (mode == IEEE80211_MODE_11NG) {
1930		/* NB: 12 is ambiguous, it will be treated as an MCS */
1931		if (rate & IEEE80211_RATE_MCS) {
1932			rate &= ~IEEE80211_RATE_MCS;
1933			m = findmedia(htrates, nitems(htrates), rate);
1934			if (m != IFM_AUTO)
1935				return m | IFM_IEEE80211_11NG;
1936		}
1937	}
1938	rate &= IEEE80211_RATE_VAL;
1939	switch (mode) {
1940	case IEEE80211_MODE_11A:
1941	case IEEE80211_MODE_HALF:		/* XXX good 'nuf */
1942	case IEEE80211_MODE_QUARTER:
1943	case IEEE80211_MODE_11NA:
1944	case IEEE80211_MODE_TURBO_A:
1945	case IEEE80211_MODE_STURBO_A:
1946		return findmedia(rates, nitems(rates),
1947		    rate | IFM_IEEE80211_11A);
1948	case IEEE80211_MODE_11B:
1949		return findmedia(rates, nitems(rates),
1950		    rate | IFM_IEEE80211_11B);
1951	case IEEE80211_MODE_FH:
1952		return findmedia(rates, nitems(rates),
1953		    rate | IFM_IEEE80211_FH);
1954	case IEEE80211_MODE_AUTO:
1955		/* NB: ic may be NULL for some drivers */
1956		if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
1957			return findmedia(rates, nitems(rates),
1958			    rate | IFM_IEEE80211_FH);
1959		/* NB: hack, 11g matches both 11b+11a rates */
1960		/* fall thru... */
1961	case IEEE80211_MODE_11G:
1962	case IEEE80211_MODE_11NG:
1963	case IEEE80211_MODE_TURBO_G:
1964		return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
1965	}
1966	return IFM_AUTO;
1967}
1968
1969int
1970ieee80211_media2rate(int mword)
1971{
1972	static const int ieeerates[] = {
1973		-1,		/* IFM_AUTO */
1974		0,		/* IFM_MANUAL */
1975		0,		/* IFM_NONE */
1976		2,		/* IFM_IEEE80211_FH1 */
1977		4,		/* IFM_IEEE80211_FH2 */
1978		2,		/* IFM_IEEE80211_DS1 */
1979		4,		/* IFM_IEEE80211_DS2 */
1980		11,		/* IFM_IEEE80211_DS5 */
1981		22,		/* IFM_IEEE80211_DS11 */
1982		44,		/* IFM_IEEE80211_DS22 */
1983		12,		/* IFM_IEEE80211_OFDM6 */
1984		18,		/* IFM_IEEE80211_OFDM9 */
1985		24,		/* IFM_IEEE80211_OFDM12 */
1986		36,		/* IFM_IEEE80211_OFDM18 */
1987		48,		/* IFM_IEEE80211_OFDM24 */
1988		72,		/* IFM_IEEE80211_OFDM36 */
1989		96,		/* IFM_IEEE80211_OFDM48 */
1990		108,		/* IFM_IEEE80211_OFDM54 */
1991		144,		/* IFM_IEEE80211_OFDM72 */
1992		0,		/* IFM_IEEE80211_DS354k */
1993		0,		/* IFM_IEEE80211_DS512k */
1994		6,		/* IFM_IEEE80211_OFDM3 */
1995		9,		/* IFM_IEEE80211_OFDM4 */
1996		54,		/* IFM_IEEE80211_OFDM27 */
1997		-1,		/* IFM_IEEE80211_MCS */
1998	};
1999	return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
2000		ieeerates[IFM_SUBTYPE(mword)] : 0;
2001}
2002
2003/*
2004 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2005 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2006 */
2007#define	mix(a, b, c)							\
2008do {									\
2009	a -= b; a -= c; a ^= (c >> 13);					\
2010	b -= c; b -= a; b ^= (a << 8);					\
2011	c -= a; c -= b; c ^= (b >> 13);					\
2012	a -= b; a -= c; a ^= (c >> 12);					\
2013	b -= c; b -= a; b ^= (a << 16);					\
2014	c -= a; c -= b; c ^= (b >> 5);					\
2015	a -= b; a -= c; a ^= (c >> 3);					\
2016	b -= c; b -= a; b ^= (a << 10);					\
2017	c -= a; c -= b; c ^= (b >> 15);					\
2018} while (/*CONSTCOND*/0)
2019
2020uint32_t
2021ieee80211_mac_hash(const struct ieee80211com *ic,
2022	const uint8_t addr[IEEE80211_ADDR_LEN])
2023{
2024	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
2025
2026	b += addr[5] << 8;
2027	b += addr[4];
2028	a += addr[3] << 24;
2029	a += addr[2] << 16;
2030	a += addr[1] << 8;
2031	a += addr[0];
2032
2033	mix(a, b, c);
2034
2035	return c;
2036}
2037#undef mix
2038
2039char
2040ieee80211_channel_type_char(const struct ieee80211_channel *c)
2041{
2042	if (IEEE80211_IS_CHAN_ST(c))
2043		return 'S';
2044	if (IEEE80211_IS_CHAN_108A(c))
2045		return 'T';
2046	if (IEEE80211_IS_CHAN_108G(c))
2047		return 'G';
2048	if (IEEE80211_IS_CHAN_HT(c))
2049		return 'n';
2050	if (IEEE80211_IS_CHAN_A(c))
2051		return 'a';
2052	if (IEEE80211_IS_CHAN_ANYG(c))
2053		return 'g';
2054	if (IEEE80211_IS_CHAN_B(c))
2055		return 'b';
2056	return 'f';
2057}
2058