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