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